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/moduleparam.h>
29 #include <linux/string.h>
30 #include <sound/core.h>
31 #include <sound/timer.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/minors.h>
35 #include <sound/initval.h>
36 #include <linux/kmod.h>
38 #include <linux/kerneld.h>
41 #if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
42 #define DEFAULT_TIMER_LIMIT 3
43 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
44 #define DEFAULT_TIMER_LIMIT 2
46 #define DEFAULT_TIMER_LIMIT 1
49 static int timer_limit = DEFAULT_TIMER_LIMIT;
50 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
51 MODULE_DESCRIPTION("ALSA timer interface");
52 MODULE_LICENSE("GPL");
53 module_param(timer_limit, int, 0444);
54 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
57 snd_timer_instance_t *timeri;
58 int tread; /* enhanced read with timestamps and events */
60 unsigned long overrun;
65 snd_timer_read_t *queue;
66 snd_timer_tread_t *tqueue;
68 unsigned long last_resolution;
70 struct timespec tstamp; /* trigger tstamp */
71 wait_queue_head_t qchange_sleep;
72 struct fasync_struct *fasync;
73 struct semaphore tread_sem;
77 static LIST_HEAD(snd_timer_list);
79 /* list of slave instances */
80 static LIST_HEAD(snd_timer_slave_list);
82 /* lock for slave active lists */
83 static DEFINE_SPINLOCK(slave_active_lock);
85 static DECLARE_MUTEX(register_mutex);
87 static int snd_timer_free(snd_timer_t *timer);
88 static int snd_timer_dev_free(snd_device_t *device);
89 static int snd_timer_dev_register(snd_device_t *device);
90 static int snd_timer_dev_unregister(snd_device_t *device);
92 static void snd_timer_reschedule(snd_timer_t * timer, unsigned long ticks_left);
95 * create a timer instance with the given owner string.
96 * when timer is not NULL, increments the module counter
98 static snd_timer_instance_t *snd_timer_instance_new(char *owner,
101 snd_timer_instance_t *timeri;
102 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
105 timeri->owner = kstrdup(owner, GFP_KERNEL);
106 if (! timeri->owner) {
110 INIT_LIST_HEAD(&timeri->open_list);
111 INIT_LIST_HEAD(&timeri->active_list);
112 INIT_LIST_HEAD(&timeri->ack_list);
113 INIT_LIST_HEAD(&timeri->slave_list_head);
114 INIT_LIST_HEAD(&timeri->slave_active_head);
116 timeri->timer = timer;
117 if (timer && !try_module_get(timer->module)) {
118 kfree(timeri->owner);
127 * find a timer instance from the given timer id
129 static snd_timer_t *snd_timer_find(snd_timer_id_t *tid)
131 snd_timer_t *timer = NULL;
134 list_for_each(p, &snd_timer_list) {
135 timer = list_entry(p, snd_timer_t, device_list);
137 if (timer->tmr_class != tid->dev_class)
139 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
140 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
141 (timer->card == NULL ||
142 timer->card->number != tid->card))
144 if (timer->tmr_device != tid->device)
146 if (timer->tmr_subdevice != tid->subdevice)
155 static void snd_timer_request(snd_timer_id_t *tid)
157 if (! current->fs->root)
159 switch (tid->dev_class) {
160 case SNDRV_TIMER_CLASS_GLOBAL:
161 if (tid->device < timer_limit)
162 request_module("snd-timer-%i", tid->device);
164 case SNDRV_TIMER_CLASS_CARD:
165 case SNDRV_TIMER_CLASS_PCM:
166 if (tid->card < snd_ecards_limit)
167 request_module("snd-card-%i", tid->card);
177 * look for a master instance matching with the slave id of the given slave.
178 * when found, relink the open_link of the slave.
180 * call this with register_mutex down.
182 static void snd_timer_check_slave(snd_timer_instance_t *slave)
185 snd_timer_instance_t *master;
186 struct list_head *p, *q;
188 /* FIXME: it's really dumb to look up all entries.. */
189 list_for_each(p, &snd_timer_list) {
190 timer = list_entry(p, snd_timer_t, device_list);
191 list_for_each(q, &timer->open_list_head) {
192 master = list_entry(q, snd_timer_instance_t, open_list);
193 if (slave->slave_class == master->slave_class &&
194 slave->slave_id == master->slave_id) {
195 list_del(&slave->open_list);
196 list_add_tail(&slave->open_list,
197 &master->slave_list_head);
198 spin_lock_irq(&slave_active_lock);
199 slave->master = master;
200 slave->timer = master->timer;
201 spin_unlock_irq(&slave_active_lock);
209 * look for slave instances matching with the slave id of the given master.
210 * when found, relink the open_link of slaves.
212 * call this with register_mutex down.
214 static void snd_timer_check_master(snd_timer_instance_t *master)
216 snd_timer_instance_t *slave;
217 struct list_head *p, *n;
219 /* check all pending slaves */
220 list_for_each_safe(p, n, &snd_timer_slave_list) {
221 slave = list_entry(p, snd_timer_instance_t, open_list);
222 if (slave->slave_class == master->slave_class &&
223 slave->slave_id == master->slave_id) {
225 list_add_tail(p, &master->slave_list_head);
226 spin_lock_irq(&slave_active_lock);
227 slave->master = master;
228 slave->timer = master->timer;
229 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
230 list_add_tail(&slave->active_list,
231 &master->slave_active_head);
232 spin_unlock_irq(&slave_active_lock);
238 * open a timer instance
239 * when opening a master, the slave id must be here given.
241 int snd_timer_open(snd_timer_instance_t **ti,
242 char *owner, snd_timer_id_t *tid,
243 unsigned int slave_id)
246 snd_timer_instance_t *timeri = NULL;
248 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
249 /* open a slave instance */
250 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
251 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
252 snd_printd("invalid slave class %i\n", tid->dev_sclass);
255 down(®ister_mutex);
256 timeri = snd_timer_instance_new(owner, NULL);
261 timeri->slave_class = tid->dev_sclass;
262 timeri->slave_id = tid->device;
263 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
264 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
265 snd_timer_check_slave(timeri);
271 /* open a master instance */
272 down(®ister_mutex);
273 timer = snd_timer_find(tid);
277 snd_timer_request(tid);
278 down(®ister_mutex);
279 timer = snd_timer_find(tid);
286 if (!list_empty(&timer->open_list_head)) {
287 timeri = list_entry(timer->open_list_head.next,
288 snd_timer_instance_t, open_list);
289 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
294 timeri = snd_timer_instance_new(owner, timer);
299 timeri->slave_class = tid->dev_sclass;
300 timeri->slave_id = slave_id;
301 if (list_empty(&timer->open_list_head) && timer->hw.open)
302 timer->hw.open(timer);
303 list_add_tail(&timeri->open_list, &timer->open_list_head);
304 snd_timer_check_master(timeri);
310 static int _snd_timer_stop(snd_timer_instance_t * timeri,
311 int keep_flag, enum sndrv_timer_event event);
314 * close a timer instance
316 int snd_timer_close(snd_timer_instance_t * timeri)
318 snd_timer_t *timer = NULL;
319 struct list_head *p, *n;
320 snd_timer_instance_t *slave;
322 snd_assert(timeri != NULL, return -ENXIO);
324 /* force to stop the timer */
325 snd_timer_stop(timeri);
327 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
328 /* wait, until the active callback is finished */
329 spin_lock_irq(&slave_active_lock);
330 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
331 spin_unlock_irq(&slave_active_lock);
333 spin_lock_irq(&slave_active_lock);
335 spin_unlock_irq(&slave_active_lock);
336 down(®ister_mutex);
337 list_del(&timeri->open_list);
340 timer = timeri->timer;
341 /* wait, until the active callback is finished */
342 spin_lock_irq(&timer->lock);
343 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
344 spin_unlock_irq(&timer->lock);
346 spin_lock_irq(&timer->lock);
348 spin_unlock_irq(&timer->lock);
349 down(®ister_mutex);
350 list_del(&timeri->open_list);
351 if (timer && list_empty(&timer->open_list_head) &&
353 timer->hw.close(timer);
354 /* remove slave links */
355 list_for_each_safe(p, n, &timeri->slave_list_head) {
356 slave = list_entry(p, snd_timer_instance_t, open_list);
357 spin_lock_irq(&slave_active_lock);
358 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
360 list_add_tail(p, &snd_timer_slave_list);
361 slave->master = NULL;
363 spin_unlock_irq(&slave_active_lock);
367 if (timeri->private_free)
368 timeri->private_free(timeri);
369 kfree(timeri->owner);
372 module_put(timer->module);
376 unsigned long snd_timer_resolution(snd_timer_instance_t * timeri)
382 if ((timer = timeri->timer) != NULL) {
383 if (timer->hw.c_resolution)
384 return timer->hw.c_resolution(timer);
385 return timer->hw.resolution;
390 static void snd_timer_notify1(snd_timer_instance_t *ti,
391 enum sndrv_timer_event event)
395 unsigned long resolution = 0;
396 snd_timer_instance_t *ts;
398 struct timespec tstamp;
400 getnstimeofday(&tstamp);
401 snd_assert(event >= SNDRV_TIMER_EVENT_START &&
402 event <= SNDRV_TIMER_EVENT_PAUSE, return);
403 if (event == SNDRV_TIMER_EVENT_START ||
404 event == SNDRV_TIMER_EVENT_CONTINUE)
405 resolution = snd_timer_resolution(ti);
407 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
408 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
413 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
415 spin_lock_irqsave(&timer->lock, flags);
416 list_for_each(n, &ti->slave_active_head) {
417 ts = list_entry(n, snd_timer_instance_t, active_list);
419 ts->ccallback(ti, event + 100, &tstamp, resolution);
421 spin_unlock_irqrestore(&timer->lock, flags);
424 static int snd_timer_start1(snd_timer_t *timer, snd_timer_instance_t *timeri,
425 unsigned long sticks)
427 list_del(&timeri->active_list);
428 list_add_tail(&timeri->active_list, &timer->active_list_head);
429 if (timer->running) {
430 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
432 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
433 timeri->flags |= SNDRV_TIMER_IFLG_START;
434 return 1; /* delayed start */
436 timer->sticks = sticks;
437 timer->hw.start(timer);
440 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
445 static int snd_timer_start_slave(snd_timer_instance_t *timeri)
449 spin_lock_irqsave(&slave_active_lock, flags);
450 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
452 list_add_tail(&timeri->active_list,
453 &timeri->master->slave_active_head);
454 spin_unlock_irqrestore(&slave_active_lock, flags);
455 return 1; /* delayed start */
459 * start the timer instance
461 int snd_timer_start(snd_timer_instance_t * timeri, unsigned int ticks)
464 int result = -EINVAL;
467 if (timeri == NULL || ticks < 1)
469 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
470 result = snd_timer_start_slave(timeri);
471 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
474 timer = timeri->timer;
477 spin_lock_irqsave(&timer->lock, flags);
478 timeri->ticks = timeri->cticks = ticks;
480 result = snd_timer_start1(timer, timeri, ticks);
481 spin_unlock_irqrestore(&timer->lock, flags);
482 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
486 static int _snd_timer_stop(snd_timer_instance_t * timeri,
487 int keep_flag, enum sndrv_timer_event event)
492 snd_assert(timeri != NULL, return -ENXIO);
494 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
496 spin_lock_irqsave(&slave_active_lock, flags);
497 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
498 spin_unlock_irqrestore(&slave_active_lock, flags);
502 timer = timeri->timer;
505 spin_lock_irqsave(&timer->lock, flags);
506 list_del_init(&timeri->ack_list);
507 list_del_init(&timeri->active_list);
508 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
509 !(--timer->running)) {
510 timer->hw.stop(timer);
511 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
512 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
513 snd_timer_reschedule(timer, 0);
514 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
515 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
516 timer->hw.start(timer);
522 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
523 spin_unlock_irqrestore(&timer->lock, flags);
525 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
526 snd_timer_notify1(timeri, event);
531 * stop the timer instance.
533 * do not call this from the timer callback!
535 int snd_timer_stop(snd_timer_instance_t * timeri)
541 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
544 timer = timeri->timer;
545 spin_lock_irqsave(&timer->lock, flags);
546 timeri->cticks = timeri->ticks;
548 spin_unlock_irqrestore(&timer->lock, flags);
553 * start again.. the tick is kept.
555 int snd_timer_continue(snd_timer_instance_t * timeri)
558 int result = -EINVAL;
563 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
564 return snd_timer_start_slave(timeri);
565 timer = timeri->timer;
568 spin_lock_irqsave(&timer->lock, flags);
572 result = snd_timer_start1(timer, timeri, timer->sticks);
573 spin_unlock_irqrestore(&timer->lock, flags);
574 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
579 * pause.. remember the ticks left
581 int snd_timer_pause(snd_timer_instance_t * timeri)
583 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
587 * reschedule the timer
589 * start pending instances and check the scheduling ticks.
590 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
592 static void snd_timer_reschedule(snd_timer_t * timer, unsigned long ticks_left)
594 snd_timer_instance_t *ti;
595 unsigned long ticks = ~0UL;
598 list_for_each(p, &timer->active_list_head) {
599 ti = list_entry(p, snd_timer_instance_t, active_list);
600 if (ti->flags & SNDRV_TIMER_IFLG_START) {
601 ti->flags &= ~SNDRV_TIMER_IFLG_START;
602 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
605 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
606 if (ticks > ti->cticks)
611 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
614 if (ticks > timer->hw.ticks)
615 ticks = timer->hw.ticks;
616 if (ticks_left != ticks)
617 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
618 timer->sticks = ticks;
625 static void snd_timer_tasklet(unsigned long arg)
627 snd_timer_t *timer = (snd_timer_t *) arg;
628 snd_timer_instance_t *ti;
630 unsigned long resolution, ticks;
632 spin_lock(&timer->lock);
633 /* now process all callbacks */
634 while (!list_empty(&timer->sack_list_head)) {
635 p = timer->sack_list_head.next; /* get first item */
636 ti = list_entry(p, snd_timer_instance_t, ack_list);
638 /* remove from ack_list and make empty */
643 resolution = ti->resolution;
645 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
646 spin_unlock(&timer->lock);
648 ti->callback(ti, resolution, ticks);
649 spin_lock(&timer->lock);
650 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
652 spin_unlock(&timer->lock);
658 * ticks_left is usually equal to timer->sticks.
661 void snd_timer_interrupt(snd_timer_t * timer, unsigned long ticks_left)
663 snd_timer_instance_t *ti, *ts;
664 unsigned long resolution, ticks;
665 struct list_head *p, *q, *n, *ack_list_head;
671 spin_lock(&timer->lock);
673 /* remember the current resolution */
674 if (timer->hw.c_resolution)
675 resolution = timer->hw.c_resolution(timer);
677 resolution = timer->hw.resolution;
679 /* loop for all active instances
680 * Here we cannot use list_for_each because the active_list of a
681 * processed instance is relinked to done_list_head before the callback
684 list_for_each_safe(p, n, &timer->active_list_head) {
685 ti = list_entry(p, snd_timer_instance_t, active_list);
686 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
688 ti->pticks += ticks_left;
689 ti->resolution = resolution;
690 if (ti->cticks < ticks_left)
693 ti->cticks -= ticks_left;
694 if (ti->cticks) /* not expired */
696 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
697 ti->cticks = ti->ticks;
699 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
700 if (--timer->running)
703 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
704 (ti->flags & SNDRV_TIMER_IFLG_FAST))
705 ack_list_head = &timer->ack_list_head;
707 ack_list_head = &timer->sack_list_head;
708 if (list_empty(&ti->ack_list))
709 list_add_tail(&ti->ack_list, ack_list_head);
710 list_for_each(q, &ti->slave_active_head) {
711 ts = list_entry(q, snd_timer_instance_t, active_list);
712 ts->pticks = ti->pticks;
713 ts->resolution = resolution;
714 if (list_empty(&ts->ack_list))
715 list_add_tail(&ts->ack_list, ack_list_head);
718 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
719 snd_timer_reschedule(timer, ticks_left);
720 if (timer->running) {
721 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
722 timer->hw.stop(timer);
723 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
725 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
726 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
728 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
729 timer->hw.start(timer);
732 timer->hw.stop(timer);
735 /* now process all fast callbacks */
736 while (!list_empty(&timer->ack_list_head)) {
737 p = timer->ack_list_head.next; /* get first item */
738 ti = list_entry(p, snd_timer_instance_t, ack_list);
740 /* remove from ack_list and make empty */
746 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
747 spin_unlock(&timer->lock);
749 ti->callback(ti, resolution, ticks);
750 spin_lock(&timer->lock);
751 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
754 /* do we have any slow callbacks? */
755 use_tasklet = !list_empty(&timer->sack_list_head);
756 spin_unlock(&timer->lock);
759 tasklet_hi_schedule(&timer->task_queue);
766 int snd_timer_new(snd_card_t *card, char *id, snd_timer_id_t *tid,
767 snd_timer_t **rtimer)
771 static snd_device_ops_t ops = {
772 .dev_free = snd_timer_dev_free,
773 .dev_register = snd_timer_dev_register,
774 .dev_unregister = snd_timer_dev_unregister
777 snd_assert(tid != NULL, return -EINVAL);
778 snd_assert(rtimer != NULL, return -EINVAL);
780 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
783 timer->tmr_class = tid->dev_class;
785 timer->tmr_device = tid->device;
786 timer->tmr_subdevice = tid->subdevice;
788 strlcpy(timer->id, id, sizeof(timer->id));
789 INIT_LIST_HEAD(&timer->device_list);
790 INIT_LIST_HEAD(&timer->open_list_head);
791 INIT_LIST_HEAD(&timer->active_list_head);
792 INIT_LIST_HEAD(&timer->ack_list_head);
793 INIT_LIST_HEAD(&timer->sack_list_head);
794 spin_lock_init(&timer->lock);
795 tasklet_init(&timer->task_queue, snd_timer_tasklet,
796 (unsigned long)timer);
798 timer->module = card->module;
799 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
801 snd_timer_free(timer);
809 static int snd_timer_free(snd_timer_t *timer)
811 snd_assert(timer != NULL, return -ENXIO);
812 if (timer->private_free)
813 timer->private_free(timer);
818 static int snd_timer_dev_free(snd_device_t *device)
820 snd_timer_t *timer = device->device_data;
821 return snd_timer_free(timer);
824 static int snd_timer_dev_register(snd_device_t *dev)
826 snd_timer_t *timer = dev->device_data;
830 snd_assert(timer != NULL && timer->hw.start != NULL &&
831 timer->hw.stop != NULL, return -ENXIO);
832 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
833 !timer->hw.resolution && timer->hw.c_resolution == NULL)
836 down(®ister_mutex);
837 list_for_each(p, &snd_timer_list) {
838 timer1 = list_entry(p, snd_timer_t, 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)
861 list_add_tail(&timer->device_list, p);
866 static int snd_timer_unregister(snd_timer_t *timer)
868 struct list_head *p, *n;
869 snd_timer_instance_t *ti;
871 snd_assert(timer != NULL, return -ENXIO);
872 down(®ister_mutex);
873 if (! list_empty(&timer->open_list_head)) {
874 snd_printk(KERN_WARNING "timer 0x%lx is busy?\n", (long)timer);
875 list_for_each_safe(p, n, &timer->open_list_head) {
877 ti = list_entry(p, snd_timer_instance_t, open_list);
881 list_del(&timer->device_list);
883 return snd_timer_free(timer);
886 static int snd_timer_dev_unregister(snd_device_t *device)
888 snd_timer_t *timer = device->device_data;
889 return snd_timer_unregister(timer);
892 void snd_timer_notify(snd_timer_t *timer, enum sndrv_timer_event event,
893 struct timespec *tstamp)
896 unsigned long resolution = 0;
897 snd_timer_instance_t *ti, *ts;
898 struct list_head *p, *n;
900 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
902 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
903 event <= SNDRV_TIMER_EVENT_MRESUME, return);
904 spin_lock_irqsave(&timer->lock, flags);
905 if (event == SNDRV_TIMER_EVENT_MSTART ||
906 event == SNDRV_TIMER_EVENT_MCONTINUE ||
907 event == SNDRV_TIMER_EVENT_MRESUME) {
908 if (timer->hw.c_resolution)
909 resolution = timer->hw.c_resolution(timer);
911 resolution = timer->hw.resolution;
913 list_for_each(p, &timer->active_list_head) {
914 ti = list_entry(p, snd_timer_instance_t, active_list);
916 ti->ccallback(ti, event, tstamp, resolution);
917 list_for_each(n, &ti->slave_active_head) {
918 ts = list_entry(n, snd_timer_instance_t, active_list);
920 ts->ccallback(ts, event, tstamp, resolution);
923 spin_unlock_irqrestore(&timer->lock, flags);
927 * exported functions for global timers
929 int snd_timer_global_new(char *id, int device, snd_timer_t **rtimer)
933 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
934 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
938 return snd_timer_new(NULL, id, &tid, rtimer);
941 int snd_timer_global_free(snd_timer_t *timer)
943 return snd_timer_free(timer);
946 int snd_timer_global_register(snd_timer_t *timer)
950 memset(&dev, 0, sizeof(dev));
951 dev.device_data = timer;
952 return snd_timer_dev_register(&dev);
955 int snd_timer_global_unregister(snd_timer_t *timer)
957 return snd_timer_unregister(timer);
964 struct snd_timer_system_private {
965 struct timer_list tlist;
966 struct timer * timer;
967 unsigned long last_expires;
968 unsigned long last_jiffies;
969 unsigned long correction;
972 static void snd_timer_s_function(unsigned long data)
974 snd_timer_t *timer = (snd_timer_t *)data;
975 struct snd_timer_system_private *priv = timer->private_data;
976 unsigned long jiff = jiffies;
977 if (time_after(jiff, priv->last_expires))
978 priv->correction = (long)jiff - (long)priv->last_expires;
979 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
982 static int snd_timer_s_start(snd_timer_t * timer)
984 struct snd_timer_system_private *priv;
987 priv = (struct snd_timer_system_private *) timer->private_data;
988 njiff = (priv->last_jiffies = jiffies);
989 if (priv->correction > timer->sticks - 1) {
990 priv->correction -= timer->sticks - 1;
993 njiff += timer->sticks - priv->correction;
994 priv->correction -= timer->sticks;
996 priv->last_expires = priv->tlist.expires = njiff;
997 add_timer(&priv->tlist);
1001 static int snd_timer_s_stop(snd_timer_t * timer)
1003 struct snd_timer_system_private *priv;
1006 priv = (struct snd_timer_system_private *) timer->private_data;
1007 del_timer(&priv->tlist);
1009 if (time_before(jiff, priv->last_expires))
1010 timer->sticks = priv->last_expires - jiff;
1016 static struct _snd_timer_hardware snd_timer_system =
1018 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1019 .resolution = 1000000000L / HZ,
1021 .start = snd_timer_s_start,
1022 .stop = snd_timer_s_stop
1025 static void snd_timer_free_system(snd_timer_t *timer)
1027 kfree(timer->private_data);
1030 static int snd_timer_register_system(void)
1033 struct snd_timer_system_private *priv;
1036 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1039 strcpy(timer->name, "system timer");
1040 timer->hw = snd_timer_system;
1041 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1043 snd_timer_free(timer);
1046 init_timer(&priv->tlist);
1047 priv->tlist.function = snd_timer_s_function;
1048 priv->tlist.data = (unsigned long) timer;
1049 timer->private_data = priv;
1050 timer->private_free = snd_timer_free_system;
1051 return snd_timer_global_register(timer);
1058 static void snd_timer_proc_read(snd_info_entry_t *entry,
1059 snd_info_buffer_t * buffer)
1061 unsigned long flags;
1063 snd_timer_instance_t *ti;
1064 struct list_head *p, *q;
1066 down(®ister_mutex);
1067 list_for_each(p, &snd_timer_list) {
1068 timer = list_entry(p, snd_timer_t, device_list);
1069 switch (timer->tmr_class) {
1070 case SNDRV_TIMER_CLASS_GLOBAL:
1071 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1073 case SNDRV_TIMER_CLASS_CARD:
1074 snd_iprintf(buffer, "C%i-%i: ",
1075 timer->card->number, timer->tmr_device);
1077 case SNDRV_TIMER_CLASS_PCM:
1078 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1079 timer->tmr_device, timer->tmr_subdevice);
1082 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1083 timer->card ? timer->card->number : -1,
1084 timer->tmr_device, timer->tmr_subdevice);
1086 snd_iprintf(buffer, "%s :", timer->name);
1087 if (timer->hw.resolution)
1088 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1089 timer->hw.resolution / 1000,
1090 timer->hw.resolution % 1000,
1092 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1093 snd_iprintf(buffer, " SLAVE");
1094 snd_iprintf(buffer, "\n");
1095 spin_lock_irqsave(&timer->lock, flags);
1096 list_for_each(q, &timer->open_list_head) {
1097 ti = list_entry(q, snd_timer_instance_t, open_list);
1098 snd_iprintf(buffer, " Client %s : %s\n",
1099 ti->owner ? ti->owner : "unknown",
1100 ti->flags & (SNDRV_TIMER_IFLG_START |
1101 SNDRV_TIMER_IFLG_RUNNING)
1102 ? "running" : "stopped");
1104 spin_unlock_irqrestore(&timer->lock, flags);
1106 up(®ister_mutex);
1110 * USER SPACE interface
1113 static void snd_timer_user_interrupt(snd_timer_instance_t *timeri,
1114 unsigned long resolution,
1115 unsigned long ticks)
1117 snd_timer_user_t *tu = timeri->callback_data;
1118 snd_timer_read_t *r;
1121 spin_lock(&tu->qlock);
1122 if (tu->qused > 0) {
1123 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1124 r = &tu->queue[prev];
1125 if (r->resolution == resolution) {
1130 if (tu->qused >= tu->queue_size) {
1133 r = &tu->queue[tu->qtail++];
1134 tu->qtail %= tu->queue_size;
1135 r->resolution = resolution;
1140 spin_unlock(&tu->qlock);
1141 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1142 wake_up(&tu->qchange_sleep);
1145 static void snd_timer_user_append_to_tqueue(snd_timer_user_t *tu,
1146 snd_timer_tread_t *tread)
1148 if (tu->qused >= tu->queue_size) {
1151 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1152 tu->qtail %= tu->queue_size;
1157 static void snd_timer_user_ccallback(snd_timer_instance_t *timeri,
1158 enum sndrv_timer_event event,
1159 struct timespec *tstamp,
1160 unsigned long resolution)
1162 snd_timer_user_t *tu = timeri->callback_data;
1163 snd_timer_tread_t r1;
1165 if (event >= SNDRV_TIMER_EVENT_START &&
1166 event <= SNDRV_TIMER_EVENT_PAUSE)
1167 tu->tstamp = *tstamp;
1168 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1171 r1.tstamp = *tstamp;
1172 r1.val = resolution;
1173 spin_lock(&tu->qlock);
1174 snd_timer_user_append_to_tqueue(tu, &r1);
1175 spin_unlock(&tu->qlock);
1176 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1177 wake_up(&tu->qchange_sleep);
1180 static void snd_timer_user_tinterrupt(snd_timer_instance_t *timeri,
1181 unsigned long resolution,
1182 unsigned long ticks)
1184 snd_timer_user_t *tu = timeri->callback_data;
1185 snd_timer_tread_t *r, r1;
1186 struct timespec tstamp;
1187 int prev, append = 0;
1189 memset(&tstamp, 0, sizeof(tstamp));
1190 spin_lock(&tu->qlock);
1191 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1192 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1193 spin_unlock(&tu->qlock);
1196 if (tu->last_resolution != resolution || ticks > 0)
1197 getnstimeofday(&tstamp);
1198 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1199 tu->last_resolution != resolution) {
1200 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1202 r1.val = resolution;
1203 snd_timer_user_append_to_tqueue(tu, &r1);
1204 tu->last_resolution = resolution;
1207 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1211 if (tu->qused > 0) {
1212 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1213 r = &tu->tqueue[prev];
1214 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1221 r1.event = SNDRV_TIMER_EVENT_TICK;
1224 snd_timer_user_append_to_tqueue(tu, &r1);
1227 spin_unlock(&tu->qlock);
1230 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1231 wake_up(&tu->qchange_sleep);
1234 static int snd_timer_user_open(struct inode *inode, struct file *file)
1236 snd_timer_user_t *tu;
1238 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1241 spin_lock_init(&tu->qlock);
1242 init_waitqueue_head(&tu->qchange_sleep);
1243 init_MUTEX(&tu->tread_sem);
1245 tu->queue_size = 128;
1246 tu->queue = kmalloc(tu->queue_size * sizeof(snd_timer_read_t),
1248 if (tu->queue == NULL) {
1252 file->private_data = tu;
1256 static int snd_timer_user_release(struct inode *inode, struct file *file)
1258 snd_timer_user_t *tu;
1260 if (file->private_data) {
1261 tu = file->private_data;
1262 file->private_data = NULL;
1263 fasync_helper(-1, file, 0, &tu->fasync);
1265 snd_timer_close(tu->timeri);
1273 static void snd_timer_user_zero_id(snd_timer_id_t *id)
1275 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1276 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1282 static void snd_timer_user_copy_id(snd_timer_id_t *id, snd_timer_t *timer)
1284 id->dev_class = timer->tmr_class;
1285 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1286 id->card = timer->card ? timer->card->number : -1;
1287 id->device = timer->tmr_device;
1288 id->subdevice = timer->tmr_subdevice;
1291 static int snd_timer_user_next_device(snd_timer_id_t __user *_tid)
1295 struct list_head *p;
1297 if (copy_from_user(&id, _tid, sizeof(id)))
1299 down(®ister_mutex);
1300 if (id.dev_class < 0) { /* first item */
1301 if (list_empty(&snd_timer_list))
1302 snd_timer_user_zero_id(&id);
1304 timer = list_entry(snd_timer_list.next,
1305 snd_timer_t, device_list);
1306 snd_timer_user_copy_id(&id, timer);
1309 switch (id.dev_class) {
1310 case SNDRV_TIMER_CLASS_GLOBAL:
1311 id.device = id.device < 0 ? 0 : id.device + 1;
1312 list_for_each(p, &snd_timer_list) {
1313 timer = list_entry(p, snd_timer_t, device_list);
1314 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1315 snd_timer_user_copy_id(&id, timer);
1318 if (timer->tmr_device >= id.device) {
1319 snd_timer_user_copy_id(&id, timer);
1323 if (p == &snd_timer_list)
1324 snd_timer_user_zero_id(&id);
1326 case SNDRV_TIMER_CLASS_CARD:
1327 case SNDRV_TIMER_CLASS_PCM:
1334 if (id.device < 0) {
1337 if (id.subdevice < 0) {
1345 list_for_each(p, &snd_timer_list) {
1346 timer = list_entry(p, snd_timer_t, device_list);
1347 if (timer->tmr_class > id.dev_class) {
1348 snd_timer_user_copy_id(&id, timer);
1351 if (timer->tmr_class < id.dev_class)
1353 if (timer->card->number > id.card) {
1354 snd_timer_user_copy_id(&id, timer);
1357 if (timer->card->number < id.card)
1359 if (timer->tmr_device > id.device) {
1360 snd_timer_user_copy_id(&id, timer);
1363 if (timer->tmr_device < id.device)
1365 if (timer->tmr_subdevice > id.subdevice) {
1366 snd_timer_user_copy_id(&id, timer);
1369 if (timer->tmr_subdevice < id.subdevice)
1371 snd_timer_user_copy_id(&id, timer);
1374 if (p == &snd_timer_list)
1375 snd_timer_user_zero_id(&id);
1378 snd_timer_user_zero_id(&id);
1381 up(®ister_mutex);
1382 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1387 static int snd_timer_user_ginfo(struct file *file,
1388 snd_timer_ginfo_t __user *_ginfo)
1390 snd_timer_ginfo_t *ginfo;
1393 struct list_head *p;
1396 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1399 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1404 memset(ginfo, 0, sizeof(*ginfo));
1406 down(®ister_mutex);
1407 t = snd_timer_find(&tid);
1409 ginfo->card = t->card ? t->card->number : -1;
1410 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1411 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1412 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1413 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1414 ginfo->resolution = t->hw.resolution;
1415 if (t->hw.resolution_min > 0) {
1416 ginfo->resolution_min = t->hw.resolution_min;
1417 ginfo->resolution_max = t->hw.resolution_max;
1419 list_for_each(p, &t->open_list_head) {
1425 up(®ister_mutex);
1426 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1432 static int snd_timer_user_gparams(struct file *file,
1433 snd_timer_gparams_t __user *_gparams)
1435 snd_timer_gparams_t gparams;
1439 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1441 down(®ister_mutex);
1442 t = snd_timer_find(&gparams.tid);
1447 if (!list_empty(&t->open_list_head)) {
1451 if (!t->hw.set_period) {
1455 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1457 up(®ister_mutex);
1461 static int snd_timer_user_gstatus(struct file *file,
1462 snd_timer_gstatus_t __user *_gstatus)
1464 snd_timer_gstatus_t gstatus;
1469 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1472 memset(&gstatus, 0, sizeof(gstatus));
1474 down(®ister_mutex);
1475 t = snd_timer_find(&tid);
1477 if (t->hw.c_resolution)
1478 gstatus.resolution = t->hw.c_resolution(t);
1480 gstatus.resolution = t->hw.resolution;
1481 if (t->hw.precise_resolution) {
1482 t->hw.precise_resolution(t, &gstatus.resolution_num,
1483 &gstatus.resolution_den);
1485 gstatus.resolution_num = gstatus.resolution;
1486 gstatus.resolution_den = 1000000000uL;
1491 up(®ister_mutex);
1492 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1497 static int snd_timer_user_tselect(struct file *file,
1498 snd_timer_select_t __user *_tselect)
1500 snd_timer_user_t *tu;
1501 snd_timer_select_t tselect;
1505 tu = file->private_data;
1506 down(&tu->tread_sem);
1508 snd_timer_close(tu->timeri);
1511 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1515 sprintf(str, "application %i", current->pid);
1516 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1517 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1518 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1527 tu->tqueue = kmalloc(tu->queue_size * sizeof(snd_timer_tread_t),
1529 if (tu->tqueue == NULL)
1532 tu->queue = kmalloc(tu->queue_size * sizeof(snd_timer_read_t),
1534 if (tu->queue == NULL)
1539 snd_timer_close(tu->timeri);
1542 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1543 tu->timeri->callback = tu->tread
1544 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1545 tu->timeri->ccallback = snd_timer_user_ccallback;
1546 tu->timeri->callback_data = (void *)tu;
1554 static int snd_timer_user_info(struct file *file,
1555 snd_timer_info_t __user *_info)
1557 snd_timer_user_t *tu;
1558 snd_timer_info_t *info;
1562 tu = file->private_data;
1563 snd_assert(tu->timeri != NULL, return -ENXIO);
1564 t = tu->timeri->timer;
1565 snd_assert(t != NULL, return -ENXIO);
1567 info = kzalloc(sizeof(*info), GFP_KERNEL);
1570 info->card = t->card ? t->card->number : -1;
1571 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1572 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1573 strlcpy(info->id, t->id, sizeof(info->id));
1574 strlcpy(info->name, t->name, sizeof(info->name));
1575 info->resolution = t->hw.resolution;
1576 if (copy_to_user(_info, info, sizeof(*_info)))
1582 static int snd_timer_user_params(struct file *file,
1583 snd_timer_params_t __user *_params)
1585 snd_timer_user_t *tu;
1586 snd_timer_params_t params;
1588 snd_timer_read_t *tr;
1589 snd_timer_tread_t *ttr;
1592 tu = file->private_data;
1593 snd_assert(tu->timeri != NULL, return -ENXIO);
1594 t = tu->timeri->timer;
1595 snd_assert(t != NULL, return -ENXIO);
1596 if (copy_from_user(¶ms, _params, sizeof(params)))
1598 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1602 if (params.queue_size > 0 &&
1603 (params.queue_size < 32 || params.queue_size > 1024)) {
1607 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1608 (1<<SNDRV_TIMER_EVENT_TICK)|
1609 (1<<SNDRV_TIMER_EVENT_START)|
1610 (1<<SNDRV_TIMER_EVENT_STOP)|
1611 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1612 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1613 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1614 (1<<SNDRV_TIMER_EVENT_RESUME)|
1615 (1<<SNDRV_TIMER_EVENT_MSTART)|
1616 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1617 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1618 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1619 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1620 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1624 snd_timer_stop(tu->timeri);
1625 spin_lock_irq(&t->lock);
1626 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1627 SNDRV_TIMER_IFLG_EXCLUSIVE|
1628 SNDRV_TIMER_IFLG_EARLY_EVENT);
1629 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1630 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1631 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1632 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1633 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1634 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1635 spin_unlock_irq(&t->lock);
1636 if (params.queue_size > 0 &&
1637 (unsigned int)tu->queue_size != params.queue_size) {
1639 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1643 tu->queue_size = params.queue_size;
1647 tr = kmalloc(params.queue_size * sizeof(*tr),
1651 tu->queue_size = params.queue_size;
1656 tu->qhead = tu->qtail = tu->qused = 0;
1657 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1659 snd_timer_tread_t tread;
1660 tread.event = SNDRV_TIMER_EVENT_EARLY;
1661 tread.tstamp.tv_sec = 0;
1662 tread.tstamp.tv_nsec = 0;
1664 snd_timer_user_append_to_tqueue(tu, &tread);
1666 snd_timer_read_t *r = &tu->queue[0];
1673 tu->filter = params.filter;
1674 tu->ticks = params.ticks;
1677 if (copy_to_user(_params, ¶ms, sizeof(params)))
1682 static int snd_timer_user_status(struct file *file,
1683 snd_timer_status_t __user *_status)
1685 snd_timer_user_t *tu;
1686 snd_timer_status_t status;
1688 tu = file->private_data;
1689 snd_assert(tu->timeri != NULL, return -ENXIO);
1690 memset(&status, 0, sizeof(status));
1691 status.tstamp = tu->tstamp;
1692 status.resolution = snd_timer_resolution(tu->timeri);
1693 status.lost = tu->timeri->lost;
1694 status.overrun = tu->overrun;
1695 spin_lock_irq(&tu->qlock);
1696 status.queue = tu->qused;
1697 spin_unlock_irq(&tu->qlock);
1698 if (copy_to_user(_status, &status, sizeof(status)))
1703 static int snd_timer_user_start(struct file *file)
1706 snd_timer_user_t *tu;
1708 tu = file->private_data;
1709 snd_assert(tu->timeri != NULL, return -ENXIO);
1710 snd_timer_stop(tu->timeri);
1711 tu->timeri->lost = 0;
1712 tu->last_resolution = 0;
1713 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1716 static int snd_timer_user_stop(struct file *file)
1719 snd_timer_user_t *tu;
1721 tu = file->private_data;
1722 snd_assert(tu->timeri != NULL, return -ENXIO);
1723 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1726 static int snd_timer_user_continue(struct file *file)
1729 snd_timer_user_t *tu;
1731 tu = file->private_data;
1732 snd_assert(tu->timeri != NULL, return -ENXIO);
1733 tu->timeri->lost = 0;
1734 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1737 static int snd_timer_user_pause(struct file *file)
1740 snd_timer_user_t *tu;
1742 tu = file->private_data;
1743 snd_assert(tu->timeri != NULL, return -ENXIO);
1744 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1748 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1749 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1750 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1751 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1754 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1757 snd_timer_user_t *tu;
1758 void __user *argp = (void __user *)arg;
1759 int __user *p = argp;
1761 tu = file->private_data;
1763 case SNDRV_TIMER_IOCTL_PVERSION:
1764 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1765 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1766 return snd_timer_user_next_device(argp);
1767 case SNDRV_TIMER_IOCTL_TREAD:
1771 down(&tu->tread_sem);
1772 if (tu->timeri) { /* too late */
1776 if (get_user(xarg, p)) {
1780 tu->tread = xarg ? 1 : 0;
1784 case SNDRV_TIMER_IOCTL_GINFO:
1785 return snd_timer_user_ginfo(file, argp);
1786 case SNDRV_TIMER_IOCTL_GPARAMS:
1787 return snd_timer_user_gparams(file, argp);
1788 case SNDRV_TIMER_IOCTL_GSTATUS:
1789 return snd_timer_user_gstatus(file, argp);
1790 case SNDRV_TIMER_IOCTL_SELECT:
1791 return snd_timer_user_tselect(file, argp);
1792 case SNDRV_TIMER_IOCTL_INFO:
1793 return snd_timer_user_info(file, argp);
1794 case SNDRV_TIMER_IOCTL_PARAMS:
1795 return snd_timer_user_params(file, argp);
1796 case SNDRV_TIMER_IOCTL_STATUS:
1797 return snd_timer_user_status(file, argp);
1798 case SNDRV_TIMER_IOCTL_START:
1799 case SNDRV_TIMER_IOCTL_START_OLD:
1800 return snd_timer_user_start(file);
1801 case SNDRV_TIMER_IOCTL_STOP:
1802 case SNDRV_TIMER_IOCTL_STOP_OLD:
1803 return snd_timer_user_stop(file);
1804 case SNDRV_TIMER_IOCTL_CONTINUE:
1805 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1806 return snd_timer_user_continue(file);
1807 case SNDRV_TIMER_IOCTL_PAUSE:
1808 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1809 return snd_timer_user_pause(file);
1814 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1816 snd_timer_user_t *tu;
1819 tu = file->private_data;
1820 err = fasync_helper(fd, file, on, &tu->fasync);
1826 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1827 size_t count, loff_t *offset)
1829 snd_timer_user_t *tu;
1830 long result = 0, unit;
1833 tu = file->private_data;
1834 unit = tu->tread ? sizeof(snd_timer_tread_t) : sizeof(snd_timer_read_t);
1835 spin_lock_irq(&tu->qlock);
1836 while ((long)count - result >= unit) {
1837 while (!tu->qused) {
1840 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1845 set_current_state(TASK_INTERRUPTIBLE);
1846 init_waitqueue_entry(&wait, current);
1847 add_wait_queue(&tu->qchange_sleep, &wait);
1849 spin_unlock_irq(&tu->qlock);
1851 spin_lock_irq(&tu->qlock);
1853 remove_wait_queue(&tu->qchange_sleep, &wait);
1855 if (signal_pending(current)) {
1861 spin_unlock_irq(&tu->qlock);
1866 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1867 sizeof(snd_timer_tread_t))) {
1872 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1873 sizeof(snd_timer_read_t))) {
1879 tu->qhead %= tu->queue_size;
1884 spin_lock_irq(&tu->qlock);
1887 spin_unlock_irq(&tu->qlock);
1889 return result > 0 ? result : err;
1892 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1895 snd_timer_user_t *tu;
1897 tu = file->private_data;
1899 poll_wait(file, &tu->qchange_sleep, wait);
1903 mask |= POLLIN | POLLRDNORM;
1908 #ifdef CONFIG_COMPAT
1909 #include "timer_compat.c"
1911 #define snd_timer_user_ioctl_compat NULL
1914 static struct file_operations snd_timer_f_ops =
1916 .owner = THIS_MODULE,
1917 .read = snd_timer_user_read,
1918 .open = snd_timer_user_open,
1919 .release = snd_timer_user_release,
1920 .poll = snd_timer_user_poll,
1921 .unlocked_ioctl = snd_timer_user_ioctl,
1922 .compat_ioctl = snd_timer_user_ioctl_compat,
1923 .fasync = snd_timer_user_fasync,
1926 static snd_minor_t snd_timer_reg =
1929 .f_ops = &snd_timer_f_ops,
1936 static snd_info_entry_t *snd_timer_proc_entry = NULL;
1938 static int __init alsa_timer_init(void)
1941 snd_info_entry_t *entry;
1943 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1944 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1947 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1948 if (entry != NULL) {
1949 entry->c.text.read_size = SNDRV_TIMER_DEVICES * 128;
1950 entry->c.text.read = snd_timer_proc_read;
1951 if (snd_info_register(entry) < 0) {
1952 snd_info_free_entry(entry);
1956 snd_timer_proc_entry = entry;
1957 if ((err = snd_timer_register_system()) < 0)
1958 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1960 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER,
1961 NULL, 0, &snd_timer_reg, "timer"))<0)
1962 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1967 static void __exit alsa_timer_exit(void)
1969 struct list_head *p, *n;
1971 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1972 /* unregister the system timer */
1973 list_for_each_safe(p, n, &snd_timer_list) {
1974 snd_timer_t *timer = list_entry(p, snd_timer_t, device_list);
1975 snd_timer_unregister(timer);
1977 if (snd_timer_proc_entry) {
1978 snd_info_unregister(snd_timer_proc_entry);
1979 snd_timer_proc_entry = NULL;
1981 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1982 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1986 module_init(alsa_timer_init)
1987 module_exit(alsa_timer_exit)
1989 EXPORT_SYMBOL(snd_timer_open);
1990 EXPORT_SYMBOL(snd_timer_close);
1991 EXPORT_SYMBOL(snd_timer_resolution);
1992 EXPORT_SYMBOL(snd_timer_start);
1993 EXPORT_SYMBOL(snd_timer_stop);
1994 EXPORT_SYMBOL(snd_timer_continue);
1995 EXPORT_SYMBOL(snd_timer_pause);
1996 EXPORT_SYMBOL(snd_timer_new);
1997 EXPORT_SYMBOL(snd_timer_notify);
1998 EXPORT_SYMBOL(snd_timer_global_new);
1999 EXPORT_SYMBOL(snd_timer_global_free);
2000 EXPORT_SYMBOL(snd_timer_global_register);
2001 EXPORT_SYMBOL(snd_timer_global_unregister);
2002 EXPORT_SYMBOL(snd_timer_interrupt);