2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/smp_lock.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/mutex.h>
29 #include <linux/moduleparam.h>
30 #include <linux/string.h>
31 #include <sound/core.h>
32 #include <sound/timer.h>
33 #include <sound/control.h>
34 #include <sound/info.h>
35 #include <sound/minors.h>
36 #include <sound/initval.h>
37 #include <linux/kmod.h>
39 #include <linux/kerneld.h>
42 #if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
43 #define DEFAULT_TIMER_LIMIT 3
44 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
45 #define DEFAULT_TIMER_LIMIT 2
47 #define DEFAULT_TIMER_LIMIT 1
50 static int timer_limit = DEFAULT_TIMER_LIMIT;
51 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
52 MODULE_DESCRIPTION("ALSA timer interface");
53 MODULE_LICENSE("GPL");
54 module_param(timer_limit, int, 0444);
55 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
57 struct snd_timer_user {
58 struct snd_timer_instance *timeri;
59 int tread; /* enhanced read with timestamps and events */
61 unsigned long overrun;
66 struct snd_timer_read *queue;
67 struct snd_timer_tread *tqueue;
69 unsigned long last_resolution;
71 struct timespec tstamp; /* trigger tstamp */
72 wait_queue_head_t qchange_sleep;
73 struct fasync_struct *fasync;
74 struct mutex tread_sem;
78 static LIST_HEAD(snd_timer_list);
80 /* list of slave instances */
81 static LIST_HEAD(snd_timer_slave_list);
83 /* lock for slave active lists */
84 static DEFINE_SPINLOCK(slave_active_lock);
86 static DEFINE_MUTEX(register_mutex);
88 static int snd_timer_free(struct snd_timer *timer);
89 static int snd_timer_dev_free(struct snd_device *device);
90 static int snd_timer_dev_register(struct snd_device *device);
91 static int snd_timer_dev_disconnect(struct snd_device *device);
93 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
96 * create a timer instance with the given owner string.
97 * when timer is not NULL, increments the module counter
99 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
100 struct snd_timer *timer)
102 struct snd_timer_instance *timeri;
103 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
106 timeri->owner = kstrdup(owner, GFP_KERNEL);
107 if (! timeri->owner) {
111 INIT_LIST_HEAD(&timeri->open_list);
112 INIT_LIST_HEAD(&timeri->active_list);
113 INIT_LIST_HEAD(&timeri->ack_list);
114 INIT_LIST_HEAD(&timeri->slave_list_head);
115 INIT_LIST_HEAD(&timeri->slave_active_head);
117 timeri->timer = timer;
118 if (timer && !try_module_get(timer->module)) {
119 kfree(timeri->owner);
128 * find a timer instance from the given timer id
130 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
132 struct snd_timer *timer = NULL;
134 list_for_each_entry(timer, &snd_timer_list, device_list) {
135 if (timer->tmr_class != tid->dev_class)
137 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
138 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
139 (timer->card == NULL ||
140 timer->card->number != tid->card))
142 if (timer->tmr_device != tid->device)
144 if (timer->tmr_subdevice != tid->subdevice)
153 static void snd_timer_request(struct snd_timer_id *tid)
155 if (! current->fs->root)
157 switch (tid->dev_class) {
158 case SNDRV_TIMER_CLASS_GLOBAL:
159 if (tid->device < timer_limit)
160 request_module("snd-timer-%i", tid->device);
162 case SNDRV_TIMER_CLASS_CARD:
163 case SNDRV_TIMER_CLASS_PCM:
164 if (tid->card < snd_ecards_limit)
165 request_module("snd-card-%i", tid->card);
175 * look for a master instance matching with the slave id of the given slave.
176 * when found, relink the open_link of the slave.
178 * call this with register_mutex down.
180 static void snd_timer_check_slave(struct snd_timer_instance *slave)
182 struct snd_timer *timer;
183 struct snd_timer_instance *master;
185 /* FIXME: it's really dumb to look up all entries.. */
186 list_for_each_entry(timer, &snd_timer_list, device_list) {
187 list_for_each_entry(master, &timer->open_list_head, open_list) {
188 if (slave->slave_class == master->slave_class &&
189 slave->slave_id == master->slave_id) {
190 list_del(&slave->open_list);
191 list_add_tail(&slave->open_list,
192 &master->slave_list_head);
193 spin_lock_irq(&slave_active_lock);
194 slave->master = master;
195 slave->timer = master->timer;
196 spin_unlock_irq(&slave_active_lock);
204 * look for slave instances matching with the slave id of the given master.
205 * when found, relink the open_link of slaves.
207 * call this with register_mutex down.
209 static void snd_timer_check_master(struct snd_timer_instance *master)
211 struct snd_timer_instance *slave, *tmp;
213 /* check all pending slaves */
214 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
215 if (slave->slave_class == master->slave_class &&
216 slave->slave_id == master->slave_id) {
217 list_move_tail(&slave->open_list, &master->slave_list_head);
218 spin_lock_irq(&slave_active_lock);
219 slave->master = master;
220 slave->timer = master->timer;
221 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
222 list_add_tail(&slave->active_list,
223 &master->slave_active_head);
224 spin_unlock_irq(&slave_active_lock);
230 * open a timer instance
231 * when opening a master, the slave id must be here given.
233 int snd_timer_open(struct snd_timer_instance **ti,
234 char *owner, struct snd_timer_id *tid,
235 unsigned int slave_id)
237 struct snd_timer *timer;
238 struct snd_timer_instance *timeri = NULL;
240 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
241 /* open a slave instance */
242 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
243 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
244 snd_printd("invalid slave class %i\n", tid->dev_sclass);
247 mutex_lock(®ister_mutex);
248 timeri = snd_timer_instance_new(owner, NULL);
250 mutex_unlock(®ister_mutex);
253 timeri->slave_class = tid->dev_sclass;
254 timeri->slave_id = tid->device;
255 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
256 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
257 snd_timer_check_slave(timeri);
258 mutex_unlock(®ister_mutex);
263 /* open a master instance */
264 mutex_lock(®ister_mutex);
265 timer = snd_timer_find(tid);
268 mutex_unlock(®ister_mutex);
269 snd_timer_request(tid);
270 mutex_lock(®ister_mutex);
271 timer = snd_timer_find(tid);
275 mutex_unlock(®ister_mutex);
278 if (!list_empty(&timer->open_list_head)) {
279 timeri = list_entry(timer->open_list_head.next,
280 struct snd_timer_instance, open_list);
281 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
282 mutex_unlock(®ister_mutex);
286 timeri = snd_timer_instance_new(owner, timer);
288 mutex_unlock(®ister_mutex);
291 timeri->slave_class = tid->dev_sclass;
292 timeri->slave_id = slave_id;
293 if (list_empty(&timer->open_list_head) && timer->hw.open)
294 timer->hw.open(timer);
295 list_add_tail(&timeri->open_list, &timer->open_list_head);
296 snd_timer_check_master(timeri);
297 mutex_unlock(®ister_mutex);
302 static int _snd_timer_stop(struct snd_timer_instance *timeri,
303 int keep_flag, int event);
306 * close a timer instance
308 int snd_timer_close(struct snd_timer_instance *timeri)
310 struct snd_timer *timer = NULL;
311 struct snd_timer_instance *slave, *tmp;
313 snd_assert(timeri != NULL, return -ENXIO);
315 /* force to stop the timer */
316 snd_timer_stop(timeri);
318 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
319 /* wait, until the active callback is finished */
320 spin_lock_irq(&slave_active_lock);
321 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
322 spin_unlock_irq(&slave_active_lock);
324 spin_lock_irq(&slave_active_lock);
326 spin_unlock_irq(&slave_active_lock);
327 mutex_lock(®ister_mutex);
328 list_del(&timeri->open_list);
329 mutex_unlock(®ister_mutex);
331 timer = timeri->timer;
332 /* wait, until the active callback is finished */
333 spin_lock_irq(&timer->lock);
334 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
335 spin_unlock_irq(&timer->lock);
337 spin_lock_irq(&timer->lock);
339 spin_unlock_irq(&timer->lock);
340 mutex_lock(®ister_mutex);
341 list_del(&timeri->open_list);
342 if (timer && list_empty(&timer->open_list_head) &&
344 timer->hw.close(timer);
345 /* remove slave links */
346 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
348 spin_lock_irq(&slave_active_lock);
349 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
350 list_move_tail(&slave->open_list, &snd_timer_slave_list);
351 slave->master = NULL;
353 spin_unlock_irq(&slave_active_lock);
355 mutex_unlock(®ister_mutex);
357 if (timeri->private_free)
358 timeri->private_free(timeri);
359 kfree(timeri->owner);
362 module_put(timer->module);
366 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
368 struct snd_timer * timer;
372 if ((timer = timeri->timer) != NULL) {
373 if (timer->hw.c_resolution)
374 return timer->hw.c_resolution(timer);
375 return timer->hw.resolution;
380 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
382 struct snd_timer *timer;
384 unsigned long resolution = 0;
385 struct snd_timer_instance *ts;
386 struct timespec tstamp;
388 getnstimeofday(&tstamp);
389 snd_assert(event >= SNDRV_TIMER_EVENT_START &&
390 event <= SNDRV_TIMER_EVENT_PAUSE, return);
391 if (event == SNDRV_TIMER_EVENT_START ||
392 event == SNDRV_TIMER_EVENT_CONTINUE)
393 resolution = snd_timer_resolution(ti);
395 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
396 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
401 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
403 spin_lock_irqsave(&timer->lock, flags);
404 list_for_each_entry(ts, &ti->slave_active_head, active_list)
406 ts->ccallback(ti, event + 100, &tstamp, resolution);
407 spin_unlock_irqrestore(&timer->lock, flags);
410 static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
411 unsigned long sticks)
413 list_del(&timeri->active_list);
414 list_add_tail(&timeri->active_list, &timer->active_list_head);
415 if (timer->running) {
416 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
418 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
419 timeri->flags |= SNDRV_TIMER_IFLG_START;
420 return 1; /* delayed start */
422 timer->sticks = sticks;
423 timer->hw.start(timer);
426 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
431 static int snd_timer_start_slave(struct snd_timer_instance *timeri)
435 spin_lock_irqsave(&slave_active_lock, flags);
436 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
438 list_add_tail(&timeri->active_list,
439 &timeri->master->slave_active_head);
440 spin_unlock_irqrestore(&slave_active_lock, flags);
441 return 1; /* delayed start */
445 * start the timer instance
447 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
449 struct snd_timer *timer;
450 int result = -EINVAL;
453 if (timeri == NULL || ticks < 1)
455 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
456 result = snd_timer_start_slave(timeri);
457 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
460 timer = timeri->timer;
463 spin_lock_irqsave(&timer->lock, flags);
464 timeri->ticks = timeri->cticks = ticks;
466 result = snd_timer_start1(timer, timeri, ticks);
467 spin_unlock_irqrestore(&timer->lock, flags);
468 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
472 static int _snd_timer_stop(struct snd_timer_instance * timeri,
473 int keep_flag, int event)
475 struct snd_timer *timer;
478 snd_assert(timeri != NULL, return -ENXIO);
480 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
482 spin_lock_irqsave(&slave_active_lock, flags);
483 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
484 spin_unlock_irqrestore(&slave_active_lock, flags);
488 timer = timeri->timer;
491 spin_lock_irqsave(&timer->lock, flags);
492 list_del_init(&timeri->ack_list);
493 list_del_init(&timeri->active_list);
494 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
495 !(--timer->running)) {
496 timer->hw.stop(timer);
497 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
498 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
499 snd_timer_reschedule(timer, 0);
500 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
501 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
502 timer->hw.start(timer);
508 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
509 spin_unlock_irqrestore(&timer->lock, flags);
511 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
512 snd_timer_notify1(timeri, event);
517 * stop the timer instance.
519 * do not call this from the timer callback!
521 int snd_timer_stop(struct snd_timer_instance *timeri)
523 struct snd_timer *timer;
527 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
530 timer = timeri->timer;
531 spin_lock_irqsave(&timer->lock, flags);
532 timeri->cticks = timeri->ticks;
534 spin_unlock_irqrestore(&timer->lock, flags);
539 * start again.. the tick is kept.
541 int snd_timer_continue(struct snd_timer_instance *timeri)
543 struct snd_timer *timer;
544 int result = -EINVAL;
549 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
550 return snd_timer_start_slave(timeri);
551 timer = timeri->timer;
554 spin_lock_irqsave(&timer->lock, flags);
558 result = snd_timer_start1(timer, timeri, timer->sticks);
559 spin_unlock_irqrestore(&timer->lock, flags);
560 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
565 * pause.. remember the ticks left
567 int snd_timer_pause(struct snd_timer_instance * timeri)
569 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
573 * reschedule the timer
575 * start pending instances and check the scheduling ticks.
576 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
578 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
580 struct snd_timer_instance *ti;
581 unsigned long ticks = ~0UL;
583 list_for_each_entry(ti, &timer->active_list_head, active_list) {
584 if (ti->flags & SNDRV_TIMER_IFLG_START) {
585 ti->flags &= ~SNDRV_TIMER_IFLG_START;
586 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
589 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
590 if (ticks > ti->cticks)
595 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
598 if (ticks > timer->hw.ticks)
599 ticks = timer->hw.ticks;
600 if (ticks_left != ticks)
601 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
602 timer->sticks = ticks;
609 static void snd_timer_tasklet(unsigned long arg)
611 struct snd_timer *timer = (struct snd_timer *) arg;
612 struct snd_timer_instance *ti;
614 unsigned long resolution, ticks;
617 spin_lock_irqsave(&timer->lock, flags);
618 /* now process all callbacks */
619 while (!list_empty(&timer->sack_list_head)) {
620 p = timer->sack_list_head.next; /* get first item */
621 ti = list_entry(p, struct snd_timer_instance, ack_list);
623 /* remove from ack_list and make empty */
628 resolution = ti->resolution;
630 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
631 spin_unlock(&timer->lock);
633 ti->callback(ti, resolution, ticks);
634 spin_lock(&timer->lock);
635 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
637 spin_unlock_irqrestore(&timer->lock, flags);
643 * ticks_left is usually equal to timer->sticks.
646 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
648 struct snd_timer_instance *ti, *ts, *tmp;
649 unsigned long resolution, ticks;
650 struct list_head *p, *ack_list_head;
657 spin_lock_irqsave(&timer->lock, flags);
659 /* remember the current resolution */
660 if (timer->hw.c_resolution)
661 resolution = timer->hw.c_resolution(timer);
663 resolution = timer->hw.resolution;
665 /* loop for all active instances
666 * Here we cannot use list_for_each_entry because the active_list of a
667 * processed instance is relinked to done_list_head before the callback
670 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
672 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
674 ti->pticks += ticks_left;
675 ti->resolution = resolution;
676 if (ti->cticks < ticks_left)
679 ti->cticks -= ticks_left;
680 if (ti->cticks) /* not expired */
682 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
683 ti->cticks = ti->ticks;
685 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
686 if (--timer->running)
687 list_del(&ti->active_list);
689 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
690 (ti->flags & SNDRV_TIMER_IFLG_FAST))
691 ack_list_head = &timer->ack_list_head;
693 ack_list_head = &timer->sack_list_head;
694 if (list_empty(&ti->ack_list))
695 list_add_tail(&ti->ack_list, ack_list_head);
696 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
697 ts->pticks = ti->pticks;
698 ts->resolution = resolution;
699 if (list_empty(&ts->ack_list))
700 list_add_tail(&ts->ack_list, ack_list_head);
703 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
704 snd_timer_reschedule(timer, timer->sticks);
705 if (timer->running) {
706 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
707 timer->hw.stop(timer);
708 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
710 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
711 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
713 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
714 timer->hw.start(timer);
717 timer->hw.stop(timer);
720 /* now process all fast callbacks */
721 while (!list_empty(&timer->ack_list_head)) {
722 p = timer->ack_list_head.next; /* get first item */
723 ti = list_entry(p, struct snd_timer_instance, ack_list);
725 /* remove from ack_list and make empty */
731 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
732 spin_unlock(&timer->lock);
734 ti->callback(ti, resolution, ticks);
735 spin_lock(&timer->lock);
736 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
739 /* do we have any slow callbacks? */
740 use_tasklet = !list_empty(&timer->sack_list_head);
741 spin_unlock_irqrestore(&timer->lock, flags);
744 tasklet_hi_schedule(&timer->task_queue);
751 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
752 struct snd_timer **rtimer)
754 struct snd_timer *timer;
756 static struct snd_device_ops ops = {
757 .dev_free = snd_timer_dev_free,
758 .dev_register = snd_timer_dev_register,
759 .dev_disconnect = snd_timer_dev_disconnect,
762 snd_assert(tid != NULL, return -EINVAL);
763 snd_assert(rtimer != NULL, return -EINVAL);
765 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
767 snd_printk(KERN_ERR "timer: cannot allocate\n");
770 timer->tmr_class = tid->dev_class;
772 timer->tmr_device = tid->device;
773 timer->tmr_subdevice = tid->subdevice;
775 strlcpy(timer->id, id, sizeof(timer->id));
776 INIT_LIST_HEAD(&timer->device_list);
777 INIT_LIST_HEAD(&timer->open_list_head);
778 INIT_LIST_HEAD(&timer->active_list_head);
779 INIT_LIST_HEAD(&timer->ack_list_head);
780 INIT_LIST_HEAD(&timer->sack_list_head);
781 spin_lock_init(&timer->lock);
782 tasklet_init(&timer->task_queue, snd_timer_tasklet,
783 (unsigned long)timer);
785 timer->module = card->module;
786 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
788 snd_timer_free(timer);
796 static int snd_timer_free(struct snd_timer *timer)
798 snd_assert(timer != NULL, return -ENXIO);
800 mutex_lock(®ister_mutex);
801 if (! list_empty(&timer->open_list_head)) {
802 struct list_head *p, *n;
803 struct snd_timer_instance *ti;
804 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
805 list_for_each_safe(p, n, &timer->open_list_head) {
807 ti = list_entry(p, struct snd_timer_instance, open_list);
811 list_del(&timer->device_list);
812 mutex_unlock(®ister_mutex);
814 if (timer->private_free)
815 timer->private_free(timer);
820 static int snd_timer_dev_free(struct snd_device *device)
822 struct snd_timer *timer = device->device_data;
823 return snd_timer_free(timer);
826 static int snd_timer_dev_register(struct snd_device *dev)
828 struct snd_timer *timer = dev->device_data;
829 struct snd_timer *timer1;
831 snd_assert(timer != NULL && timer->hw.start != NULL &&
832 timer->hw.stop != NULL, return -ENXIO);
833 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
834 !timer->hw.resolution && timer->hw.c_resolution == NULL)
837 mutex_lock(®ister_mutex);
838 list_for_each_entry(timer1, &snd_timer_list, device_list) {
839 if (timer1->tmr_class > timer->tmr_class)
841 if (timer1->tmr_class < timer->tmr_class)
843 if (timer1->card && timer->card) {
844 if (timer1->card->number > timer->card->number)
846 if (timer1->card->number < timer->card->number)
849 if (timer1->tmr_device > timer->tmr_device)
851 if (timer1->tmr_device < timer->tmr_device)
853 if (timer1->tmr_subdevice > timer->tmr_subdevice)
855 if (timer1->tmr_subdevice < timer->tmr_subdevice)
858 mutex_unlock(®ister_mutex);
861 list_add_tail(&timer->device_list, &timer1->device_list);
862 mutex_unlock(®ister_mutex);
866 static int snd_timer_dev_disconnect(struct snd_device *device)
868 struct snd_timer *timer = device->device_data;
869 mutex_lock(®ister_mutex);
870 list_del_init(&timer->device_list);
871 mutex_unlock(®ister_mutex);
875 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
878 unsigned long resolution = 0;
879 struct snd_timer_instance *ti, *ts;
881 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
883 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
884 event <= SNDRV_TIMER_EVENT_MRESUME, return);
885 spin_lock_irqsave(&timer->lock, flags);
886 if (event == SNDRV_TIMER_EVENT_MSTART ||
887 event == SNDRV_TIMER_EVENT_MCONTINUE ||
888 event == SNDRV_TIMER_EVENT_MRESUME) {
889 if (timer->hw.c_resolution)
890 resolution = timer->hw.c_resolution(timer);
892 resolution = timer->hw.resolution;
894 list_for_each_entry(ti, &timer->active_list_head, active_list) {
896 ti->ccallback(ti, event, tstamp, resolution);
897 list_for_each_entry(ts, &ti->slave_active_head, active_list)
899 ts->ccallback(ts, event, tstamp, resolution);
901 spin_unlock_irqrestore(&timer->lock, flags);
905 * exported functions for global timers
907 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
909 struct snd_timer_id tid;
911 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
912 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
916 return snd_timer_new(NULL, id, &tid, rtimer);
919 int snd_timer_global_free(struct snd_timer *timer)
921 return snd_timer_free(timer);
924 int snd_timer_global_register(struct snd_timer *timer)
926 struct snd_device dev;
928 memset(&dev, 0, sizeof(dev));
929 dev.device_data = timer;
930 return snd_timer_dev_register(&dev);
937 struct snd_timer_system_private {
938 struct timer_list tlist;
939 unsigned long last_expires;
940 unsigned long last_jiffies;
941 unsigned long correction;
944 static void snd_timer_s_function(unsigned long data)
946 struct snd_timer *timer = (struct snd_timer *)data;
947 struct snd_timer_system_private *priv = timer->private_data;
948 unsigned long jiff = jiffies;
949 if (time_after(jiff, priv->last_expires))
950 priv->correction += (long)jiff - (long)priv->last_expires;
951 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
954 static int snd_timer_s_start(struct snd_timer * timer)
956 struct snd_timer_system_private *priv;
959 priv = (struct snd_timer_system_private *) timer->private_data;
960 njiff = (priv->last_jiffies = jiffies);
961 if (priv->correction > timer->sticks - 1) {
962 priv->correction -= timer->sticks - 1;
965 njiff += timer->sticks - priv->correction;
966 priv->correction = 0;
968 priv->last_expires = priv->tlist.expires = njiff;
969 add_timer(&priv->tlist);
973 static int snd_timer_s_stop(struct snd_timer * timer)
975 struct snd_timer_system_private *priv;
978 priv = (struct snd_timer_system_private *) timer->private_data;
979 del_timer(&priv->tlist);
981 if (time_before(jiff, priv->last_expires))
982 timer->sticks = priv->last_expires - jiff;
985 priv->correction = 0;
989 static struct snd_timer_hardware snd_timer_system =
991 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
992 .resolution = 1000000000L / HZ,
994 .start = snd_timer_s_start,
995 .stop = snd_timer_s_stop
998 static void snd_timer_free_system(struct snd_timer *timer)
1000 kfree(timer->private_data);
1003 static int snd_timer_register_system(void)
1005 struct snd_timer *timer;
1006 struct snd_timer_system_private *priv;
1009 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1012 strcpy(timer->name, "system timer");
1013 timer->hw = snd_timer_system;
1014 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1016 snd_timer_free(timer);
1019 init_timer(&priv->tlist);
1020 priv->tlist.function = snd_timer_s_function;
1021 priv->tlist.data = (unsigned long) timer;
1022 timer->private_data = priv;
1023 timer->private_free = snd_timer_free_system;
1024 return snd_timer_global_register(timer);
1027 #ifdef CONFIG_PROC_FS
1032 static void snd_timer_proc_read(struct snd_info_entry *entry,
1033 struct snd_info_buffer *buffer)
1035 struct snd_timer *timer;
1036 struct snd_timer_instance *ti;
1038 mutex_lock(®ister_mutex);
1039 list_for_each_entry(timer, &snd_timer_list, device_list) {
1040 switch (timer->tmr_class) {
1041 case SNDRV_TIMER_CLASS_GLOBAL:
1042 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1044 case SNDRV_TIMER_CLASS_CARD:
1045 snd_iprintf(buffer, "C%i-%i: ",
1046 timer->card->number, timer->tmr_device);
1048 case SNDRV_TIMER_CLASS_PCM:
1049 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1050 timer->tmr_device, timer->tmr_subdevice);
1053 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1054 timer->card ? timer->card->number : -1,
1055 timer->tmr_device, timer->tmr_subdevice);
1057 snd_iprintf(buffer, "%s :", timer->name);
1058 if (timer->hw.resolution)
1059 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1060 timer->hw.resolution / 1000,
1061 timer->hw.resolution % 1000,
1063 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1064 snd_iprintf(buffer, " SLAVE");
1065 snd_iprintf(buffer, "\n");
1066 list_for_each_entry(ti, &timer->open_list_head, open_list)
1067 snd_iprintf(buffer, " Client %s : %s\n",
1068 ti->owner ? ti->owner : "unknown",
1069 ti->flags & (SNDRV_TIMER_IFLG_START |
1070 SNDRV_TIMER_IFLG_RUNNING)
1071 ? "running" : "stopped");
1073 mutex_unlock(®ister_mutex);
1076 static struct snd_info_entry *snd_timer_proc_entry;
1078 static void __init snd_timer_proc_init(void)
1080 struct snd_info_entry *entry;
1082 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1083 if (entry != NULL) {
1084 entry->c.text.read = snd_timer_proc_read;
1085 if (snd_info_register(entry) < 0) {
1086 snd_info_free_entry(entry);
1090 snd_timer_proc_entry = entry;
1093 static void __exit snd_timer_proc_done(void)
1095 snd_info_free_entry(snd_timer_proc_entry);
1097 #else /* !CONFIG_PROC_FS */
1098 #define snd_timer_proc_init()
1099 #define snd_timer_proc_done()
1103 * USER SPACE interface
1106 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1107 unsigned long resolution,
1108 unsigned long ticks)
1110 struct snd_timer_user *tu = timeri->callback_data;
1111 struct snd_timer_read *r;
1114 spin_lock(&tu->qlock);
1115 if (tu->qused > 0) {
1116 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1117 r = &tu->queue[prev];
1118 if (r->resolution == resolution) {
1123 if (tu->qused >= tu->queue_size) {
1126 r = &tu->queue[tu->qtail++];
1127 tu->qtail %= tu->queue_size;
1128 r->resolution = resolution;
1133 spin_unlock(&tu->qlock);
1134 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1135 wake_up(&tu->qchange_sleep);
1138 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1139 struct snd_timer_tread *tread)
1141 if (tu->qused >= tu->queue_size) {
1144 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1145 tu->qtail %= tu->queue_size;
1150 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1152 struct timespec *tstamp,
1153 unsigned long resolution)
1155 struct snd_timer_user *tu = timeri->callback_data;
1156 struct snd_timer_tread r1;
1158 if (event >= SNDRV_TIMER_EVENT_START &&
1159 event <= SNDRV_TIMER_EVENT_PAUSE)
1160 tu->tstamp = *tstamp;
1161 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1164 r1.tstamp = *tstamp;
1165 r1.val = resolution;
1166 spin_lock(&tu->qlock);
1167 snd_timer_user_append_to_tqueue(tu, &r1);
1168 spin_unlock(&tu->qlock);
1169 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1170 wake_up(&tu->qchange_sleep);
1173 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1174 unsigned long resolution,
1175 unsigned long ticks)
1177 struct snd_timer_user *tu = timeri->callback_data;
1178 struct snd_timer_tread *r, r1;
1179 struct timespec tstamp;
1180 int prev, append = 0;
1182 memset(&tstamp, 0, sizeof(tstamp));
1183 spin_lock(&tu->qlock);
1184 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1185 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1186 spin_unlock(&tu->qlock);
1189 if (tu->last_resolution != resolution || ticks > 0)
1190 getnstimeofday(&tstamp);
1191 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1192 tu->last_resolution != resolution) {
1193 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1195 r1.val = resolution;
1196 snd_timer_user_append_to_tqueue(tu, &r1);
1197 tu->last_resolution = resolution;
1200 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1204 if (tu->qused > 0) {
1205 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1206 r = &tu->tqueue[prev];
1207 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1214 r1.event = SNDRV_TIMER_EVENT_TICK;
1217 snd_timer_user_append_to_tqueue(tu, &r1);
1220 spin_unlock(&tu->qlock);
1223 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1224 wake_up(&tu->qchange_sleep);
1227 static int snd_timer_user_open(struct inode *inode, struct file *file)
1229 struct snd_timer_user *tu;
1231 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1234 spin_lock_init(&tu->qlock);
1235 init_waitqueue_head(&tu->qchange_sleep);
1236 mutex_init(&tu->tread_sem);
1238 tu->queue_size = 128;
1239 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1241 if (tu->queue == NULL) {
1245 file->private_data = tu;
1249 static int snd_timer_user_release(struct inode *inode, struct file *file)
1251 struct snd_timer_user *tu;
1253 if (file->private_data) {
1254 tu = file->private_data;
1255 file->private_data = NULL;
1256 fasync_helper(-1, file, 0, &tu->fasync);
1258 snd_timer_close(tu->timeri);
1266 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1268 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1269 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1275 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1277 id->dev_class = timer->tmr_class;
1278 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1279 id->card = timer->card ? timer->card->number : -1;
1280 id->device = timer->tmr_device;
1281 id->subdevice = timer->tmr_subdevice;
1284 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1286 struct snd_timer_id id;
1287 struct snd_timer *timer;
1288 struct list_head *p;
1290 if (copy_from_user(&id, _tid, sizeof(id)))
1292 mutex_lock(®ister_mutex);
1293 if (id.dev_class < 0) { /* first item */
1294 if (list_empty(&snd_timer_list))
1295 snd_timer_user_zero_id(&id);
1297 timer = list_entry(snd_timer_list.next,
1298 struct snd_timer, device_list);
1299 snd_timer_user_copy_id(&id, timer);
1302 switch (id.dev_class) {
1303 case SNDRV_TIMER_CLASS_GLOBAL:
1304 id.device = id.device < 0 ? 0 : id.device + 1;
1305 list_for_each(p, &snd_timer_list) {
1306 timer = list_entry(p, struct snd_timer, device_list);
1307 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1308 snd_timer_user_copy_id(&id, timer);
1311 if (timer->tmr_device >= id.device) {
1312 snd_timer_user_copy_id(&id, timer);
1316 if (p == &snd_timer_list)
1317 snd_timer_user_zero_id(&id);
1319 case SNDRV_TIMER_CLASS_CARD:
1320 case SNDRV_TIMER_CLASS_PCM:
1327 if (id.device < 0) {
1330 if (id.subdevice < 0) {
1338 list_for_each(p, &snd_timer_list) {
1339 timer = list_entry(p, struct snd_timer, device_list);
1340 if (timer->tmr_class > id.dev_class) {
1341 snd_timer_user_copy_id(&id, timer);
1344 if (timer->tmr_class < id.dev_class)
1346 if (timer->card->number > id.card) {
1347 snd_timer_user_copy_id(&id, timer);
1350 if (timer->card->number < id.card)
1352 if (timer->tmr_device > id.device) {
1353 snd_timer_user_copy_id(&id, timer);
1356 if (timer->tmr_device < id.device)
1358 if (timer->tmr_subdevice > id.subdevice) {
1359 snd_timer_user_copy_id(&id, timer);
1362 if (timer->tmr_subdevice < id.subdevice)
1364 snd_timer_user_copy_id(&id, timer);
1367 if (p == &snd_timer_list)
1368 snd_timer_user_zero_id(&id);
1371 snd_timer_user_zero_id(&id);
1374 mutex_unlock(®ister_mutex);
1375 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1380 static int snd_timer_user_ginfo(struct file *file,
1381 struct snd_timer_ginfo __user *_ginfo)
1383 struct snd_timer_ginfo *ginfo;
1384 struct snd_timer_id tid;
1385 struct snd_timer *t;
1386 struct list_head *p;
1389 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1392 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1397 memset(ginfo, 0, sizeof(*ginfo));
1399 mutex_lock(®ister_mutex);
1400 t = snd_timer_find(&tid);
1402 ginfo->card = t->card ? t->card->number : -1;
1403 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1404 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1405 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1406 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1407 ginfo->resolution = t->hw.resolution;
1408 if (t->hw.resolution_min > 0) {
1409 ginfo->resolution_min = t->hw.resolution_min;
1410 ginfo->resolution_max = t->hw.resolution_max;
1412 list_for_each(p, &t->open_list_head) {
1418 mutex_unlock(®ister_mutex);
1419 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1425 static int snd_timer_user_gparams(struct file *file,
1426 struct snd_timer_gparams __user *_gparams)
1428 struct snd_timer_gparams gparams;
1429 struct snd_timer *t;
1432 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1434 mutex_lock(®ister_mutex);
1435 t = snd_timer_find(&gparams.tid);
1440 if (!list_empty(&t->open_list_head)) {
1444 if (!t->hw.set_period) {
1448 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1450 mutex_unlock(®ister_mutex);
1454 static int snd_timer_user_gstatus(struct file *file,
1455 struct snd_timer_gstatus __user *_gstatus)
1457 struct snd_timer_gstatus gstatus;
1458 struct snd_timer_id tid;
1459 struct snd_timer *t;
1462 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1465 memset(&gstatus, 0, sizeof(gstatus));
1467 mutex_lock(®ister_mutex);
1468 t = snd_timer_find(&tid);
1470 if (t->hw.c_resolution)
1471 gstatus.resolution = t->hw.c_resolution(t);
1473 gstatus.resolution = t->hw.resolution;
1474 if (t->hw.precise_resolution) {
1475 t->hw.precise_resolution(t, &gstatus.resolution_num,
1476 &gstatus.resolution_den);
1478 gstatus.resolution_num = gstatus.resolution;
1479 gstatus.resolution_den = 1000000000uL;
1484 mutex_unlock(®ister_mutex);
1485 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1490 static int snd_timer_user_tselect(struct file *file,
1491 struct snd_timer_select __user *_tselect)
1493 struct snd_timer_user *tu;
1494 struct snd_timer_select tselect;
1498 tu = file->private_data;
1499 mutex_lock(&tu->tread_sem);
1501 snd_timer_close(tu->timeri);
1504 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1508 sprintf(str, "application %i", current->pid);
1509 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1510 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1511 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1520 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1522 if (tu->tqueue == NULL)
1525 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1527 if (tu->queue == NULL)
1532 snd_timer_close(tu->timeri);
1535 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1536 tu->timeri->callback = tu->tread
1537 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1538 tu->timeri->ccallback = snd_timer_user_ccallback;
1539 tu->timeri->callback_data = (void *)tu;
1543 mutex_unlock(&tu->tread_sem);
1547 static int snd_timer_user_info(struct file *file,
1548 struct snd_timer_info __user *_info)
1550 struct snd_timer_user *tu;
1551 struct snd_timer_info *info;
1552 struct snd_timer *t;
1555 tu = file->private_data;
1556 snd_assert(tu->timeri != NULL, return -ENXIO);
1557 t = tu->timeri->timer;
1558 snd_assert(t != NULL, return -ENXIO);
1560 info = kzalloc(sizeof(*info), GFP_KERNEL);
1563 info->card = t->card ? t->card->number : -1;
1564 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1565 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1566 strlcpy(info->id, t->id, sizeof(info->id));
1567 strlcpy(info->name, t->name, sizeof(info->name));
1568 info->resolution = t->hw.resolution;
1569 if (copy_to_user(_info, info, sizeof(*_info)))
1575 static int snd_timer_user_params(struct file *file,
1576 struct snd_timer_params __user *_params)
1578 struct snd_timer_user *tu;
1579 struct snd_timer_params params;
1580 struct snd_timer *t;
1581 struct snd_timer_read *tr;
1582 struct snd_timer_tread *ttr;
1585 tu = file->private_data;
1586 snd_assert(tu->timeri != NULL, return -ENXIO);
1587 t = tu->timeri->timer;
1588 snd_assert(t != NULL, return -ENXIO);
1589 if (copy_from_user(¶ms, _params, sizeof(params)))
1591 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1595 if (params.queue_size > 0 &&
1596 (params.queue_size < 32 || params.queue_size > 1024)) {
1600 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1601 (1<<SNDRV_TIMER_EVENT_TICK)|
1602 (1<<SNDRV_TIMER_EVENT_START)|
1603 (1<<SNDRV_TIMER_EVENT_STOP)|
1604 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1605 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1606 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1607 (1<<SNDRV_TIMER_EVENT_RESUME)|
1608 (1<<SNDRV_TIMER_EVENT_MSTART)|
1609 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1610 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1611 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1612 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1613 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1617 snd_timer_stop(tu->timeri);
1618 spin_lock_irq(&t->lock);
1619 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1620 SNDRV_TIMER_IFLG_EXCLUSIVE|
1621 SNDRV_TIMER_IFLG_EARLY_EVENT);
1622 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1623 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1624 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1625 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1626 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1627 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1628 spin_unlock_irq(&t->lock);
1629 if (params.queue_size > 0 &&
1630 (unsigned int)tu->queue_size != params.queue_size) {
1632 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1636 tu->queue_size = params.queue_size;
1640 tr = kmalloc(params.queue_size * sizeof(*tr),
1644 tu->queue_size = params.queue_size;
1649 tu->qhead = tu->qtail = tu->qused = 0;
1650 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1652 struct snd_timer_tread tread;
1653 tread.event = SNDRV_TIMER_EVENT_EARLY;
1654 tread.tstamp.tv_sec = 0;
1655 tread.tstamp.tv_nsec = 0;
1657 snd_timer_user_append_to_tqueue(tu, &tread);
1659 struct snd_timer_read *r = &tu->queue[0];
1666 tu->filter = params.filter;
1667 tu->ticks = params.ticks;
1670 if (copy_to_user(_params, ¶ms, sizeof(params)))
1675 static int snd_timer_user_status(struct file *file,
1676 struct snd_timer_status __user *_status)
1678 struct snd_timer_user *tu;
1679 struct snd_timer_status status;
1681 tu = file->private_data;
1682 snd_assert(tu->timeri != NULL, return -ENXIO);
1683 memset(&status, 0, sizeof(status));
1684 status.tstamp = tu->tstamp;
1685 status.resolution = snd_timer_resolution(tu->timeri);
1686 status.lost = tu->timeri->lost;
1687 status.overrun = tu->overrun;
1688 spin_lock_irq(&tu->qlock);
1689 status.queue = tu->qused;
1690 spin_unlock_irq(&tu->qlock);
1691 if (copy_to_user(_status, &status, sizeof(status)))
1696 static int snd_timer_user_start(struct file *file)
1699 struct snd_timer_user *tu;
1701 tu = file->private_data;
1702 snd_assert(tu->timeri != NULL, return -ENXIO);
1703 snd_timer_stop(tu->timeri);
1704 tu->timeri->lost = 0;
1705 tu->last_resolution = 0;
1706 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1709 static int snd_timer_user_stop(struct file *file)
1712 struct snd_timer_user *tu;
1714 tu = file->private_data;
1715 snd_assert(tu->timeri != NULL, return -ENXIO);
1716 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1719 static int snd_timer_user_continue(struct file *file)
1722 struct snd_timer_user *tu;
1724 tu = file->private_data;
1725 snd_assert(tu->timeri != NULL, return -ENXIO);
1726 tu->timeri->lost = 0;
1727 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1730 static int snd_timer_user_pause(struct file *file)
1733 struct snd_timer_user *tu;
1735 tu = file->private_data;
1736 snd_assert(tu->timeri != NULL, return -ENXIO);
1737 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1741 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1742 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1743 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1744 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1747 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1750 struct snd_timer_user *tu;
1751 void __user *argp = (void __user *)arg;
1752 int __user *p = argp;
1754 tu = file->private_data;
1756 case SNDRV_TIMER_IOCTL_PVERSION:
1757 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1758 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1759 return snd_timer_user_next_device(argp);
1760 case SNDRV_TIMER_IOCTL_TREAD:
1764 mutex_lock(&tu->tread_sem);
1765 if (tu->timeri) { /* too late */
1766 mutex_unlock(&tu->tread_sem);
1769 if (get_user(xarg, p)) {
1770 mutex_unlock(&tu->tread_sem);
1773 tu->tread = xarg ? 1 : 0;
1774 mutex_unlock(&tu->tread_sem);
1777 case SNDRV_TIMER_IOCTL_GINFO:
1778 return snd_timer_user_ginfo(file, argp);
1779 case SNDRV_TIMER_IOCTL_GPARAMS:
1780 return snd_timer_user_gparams(file, argp);
1781 case SNDRV_TIMER_IOCTL_GSTATUS:
1782 return snd_timer_user_gstatus(file, argp);
1783 case SNDRV_TIMER_IOCTL_SELECT:
1784 return snd_timer_user_tselect(file, argp);
1785 case SNDRV_TIMER_IOCTL_INFO:
1786 return snd_timer_user_info(file, argp);
1787 case SNDRV_TIMER_IOCTL_PARAMS:
1788 return snd_timer_user_params(file, argp);
1789 case SNDRV_TIMER_IOCTL_STATUS:
1790 return snd_timer_user_status(file, argp);
1791 case SNDRV_TIMER_IOCTL_START:
1792 case SNDRV_TIMER_IOCTL_START_OLD:
1793 return snd_timer_user_start(file);
1794 case SNDRV_TIMER_IOCTL_STOP:
1795 case SNDRV_TIMER_IOCTL_STOP_OLD:
1796 return snd_timer_user_stop(file);
1797 case SNDRV_TIMER_IOCTL_CONTINUE:
1798 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1799 return snd_timer_user_continue(file);
1800 case SNDRV_TIMER_IOCTL_PAUSE:
1801 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1802 return snd_timer_user_pause(file);
1807 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1809 struct snd_timer_user *tu;
1812 tu = file->private_data;
1813 err = fasync_helper(fd, file, on, &tu->fasync);
1819 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1820 size_t count, loff_t *offset)
1822 struct snd_timer_user *tu;
1823 long result = 0, unit;
1826 tu = file->private_data;
1827 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1828 spin_lock_irq(&tu->qlock);
1829 while ((long)count - result >= unit) {
1830 while (!tu->qused) {
1833 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1838 set_current_state(TASK_INTERRUPTIBLE);
1839 init_waitqueue_entry(&wait, current);
1840 add_wait_queue(&tu->qchange_sleep, &wait);
1842 spin_unlock_irq(&tu->qlock);
1844 spin_lock_irq(&tu->qlock);
1846 remove_wait_queue(&tu->qchange_sleep, &wait);
1848 if (signal_pending(current)) {
1854 spin_unlock_irq(&tu->qlock);
1859 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1860 sizeof(struct snd_timer_tread))) {
1865 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1866 sizeof(struct snd_timer_read))) {
1872 tu->qhead %= tu->queue_size;
1877 spin_lock_irq(&tu->qlock);
1880 spin_unlock_irq(&tu->qlock);
1882 return result > 0 ? result : err;
1885 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1888 struct snd_timer_user *tu;
1890 tu = file->private_data;
1892 poll_wait(file, &tu->qchange_sleep, wait);
1896 mask |= POLLIN | POLLRDNORM;
1901 #ifdef CONFIG_COMPAT
1902 #include "timer_compat.c"
1904 #define snd_timer_user_ioctl_compat NULL
1907 static struct file_operations snd_timer_f_ops =
1909 .owner = THIS_MODULE,
1910 .read = snd_timer_user_read,
1911 .open = snd_timer_user_open,
1912 .release = snd_timer_user_release,
1913 .poll = snd_timer_user_poll,
1914 .unlocked_ioctl = snd_timer_user_ioctl,
1915 .compat_ioctl = snd_timer_user_ioctl_compat,
1916 .fasync = snd_timer_user_fasync,
1923 static int __init alsa_timer_init(void)
1927 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1928 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1932 if ((err = snd_timer_register_system()) < 0)
1933 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1935 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1936 &snd_timer_f_ops, NULL, "timer")) < 0)
1937 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1939 snd_timer_proc_init();
1943 static void __exit alsa_timer_exit(void)
1945 struct list_head *p, *n;
1947 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1948 /* unregister the system timer */
1949 list_for_each_safe(p, n, &snd_timer_list) {
1950 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
1951 snd_timer_free(timer);
1953 snd_timer_proc_done();
1954 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1955 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1959 module_init(alsa_timer_init)
1960 module_exit(alsa_timer_exit)
1962 EXPORT_SYMBOL(snd_timer_open);
1963 EXPORT_SYMBOL(snd_timer_close);
1964 EXPORT_SYMBOL(snd_timer_resolution);
1965 EXPORT_SYMBOL(snd_timer_start);
1966 EXPORT_SYMBOL(snd_timer_stop);
1967 EXPORT_SYMBOL(snd_timer_continue);
1968 EXPORT_SYMBOL(snd_timer_pause);
1969 EXPORT_SYMBOL(snd_timer_new);
1970 EXPORT_SYMBOL(snd_timer_notify);
1971 EXPORT_SYMBOL(snd_timer_global_new);
1972 EXPORT_SYMBOL(snd_timer_global_free);
1973 EXPORT_SYMBOL(snd_timer_global_register);
1974 EXPORT_SYMBOL(snd_timer_interrupt);