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.");
56 struct snd_timer_user {
57 struct snd_timer_instance *timeri;
58 int tread; /* enhanced read with timestamps and events */
60 unsigned long overrun;
65 struct snd_timer_read *queue;
66 struct snd_timer_tread *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(struct snd_timer *timer);
88 static int snd_timer_dev_free(struct snd_device *device);
89 static int snd_timer_dev_register(struct snd_device *device);
90 static int snd_timer_dev_unregister(struct snd_device *device);
92 static void snd_timer_reschedule(struct snd_timer * 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 struct snd_timer_instance *snd_timer_instance_new(char *owner,
99 struct snd_timer *timer)
101 struct snd_timer_instance *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 struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
131 struct snd_timer *timer = NULL;
134 list_for_each(p, &snd_timer_list) {
135 timer = list_entry(p, struct snd_timer, 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(struct snd_timer_id *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(struct snd_timer_instance *slave)
184 struct snd_timer *timer;
185 struct snd_timer_instance *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, struct snd_timer, device_list);
191 list_for_each(q, &timer->open_list_head) {
192 master = list_entry(q, struct snd_timer_instance, 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(struct snd_timer_instance *master)
216 struct snd_timer_instance *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, struct snd_timer_instance, 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(struct snd_timer_instance **ti,
242 char *owner, struct snd_timer_id *tid,
243 unsigned int slave_id)
245 struct snd_timer *timer;
246 struct snd_timer_instance *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 struct snd_timer_instance, 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(struct snd_timer_instance *timeri,
311 int keep_flag, int event);
314 * close a timer instance
316 int snd_timer_close(struct snd_timer_instance *timeri)
318 struct snd_timer *timer = NULL;
319 struct list_head *p, *n;
320 struct snd_timer_instance *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, struct snd_timer_instance, 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(struct snd_timer_instance *timeri)
378 struct snd_timer * timer;
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(struct snd_timer_instance *ti, int event)
392 struct snd_timer *timer;
394 unsigned long resolution = 0;
395 struct snd_timer_instance *ts;
397 struct timespec tstamp;
399 getnstimeofday(&tstamp);
400 snd_assert(event >= SNDRV_TIMER_EVENT_START &&
401 event <= SNDRV_TIMER_EVENT_PAUSE, return);
402 if (event == SNDRV_TIMER_EVENT_START ||
403 event == SNDRV_TIMER_EVENT_CONTINUE)
404 resolution = snd_timer_resolution(ti);
406 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
407 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
412 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
414 spin_lock_irqsave(&timer->lock, flags);
415 list_for_each(n, &ti->slave_active_head) {
416 ts = list_entry(n, struct snd_timer_instance, active_list);
418 ts->ccallback(ti, event + 100, &tstamp, resolution);
420 spin_unlock_irqrestore(&timer->lock, flags);
423 static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
424 unsigned long sticks)
426 list_del(&timeri->active_list);
427 list_add_tail(&timeri->active_list, &timer->active_list_head);
428 if (timer->running) {
429 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
431 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
432 timeri->flags |= SNDRV_TIMER_IFLG_START;
433 return 1; /* delayed start */
435 timer->sticks = sticks;
436 timer->hw.start(timer);
439 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
444 static int snd_timer_start_slave(struct snd_timer_instance *timeri)
448 spin_lock_irqsave(&slave_active_lock, flags);
449 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
451 list_add_tail(&timeri->active_list,
452 &timeri->master->slave_active_head);
453 spin_unlock_irqrestore(&slave_active_lock, flags);
454 return 1; /* delayed start */
458 * start the timer instance
460 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
462 struct snd_timer *timer;
463 int result = -EINVAL;
466 if (timeri == NULL || ticks < 1)
468 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
469 result = snd_timer_start_slave(timeri);
470 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
473 timer = timeri->timer;
476 spin_lock_irqsave(&timer->lock, flags);
477 timeri->ticks = timeri->cticks = ticks;
479 result = snd_timer_start1(timer, timeri, ticks);
480 spin_unlock_irqrestore(&timer->lock, flags);
481 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
485 static int _snd_timer_stop(struct snd_timer_instance * timeri,
486 int keep_flag, int event)
488 struct snd_timer *timer;
491 snd_assert(timeri != NULL, return -ENXIO);
493 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
495 spin_lock_irqsave(&slave_active_lock, flags);
496 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
497 spin_unlock_irqrestore(&slave_active_lock, flags);
501 timer = timeri->timer;
504 spin_lock_irqsave(&timer->lock, flags);
505 list_del_init(&timeri->ack_list);
506 list_del_init(&timeri->active_list);
507 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
508 !(--timer->running)) {
509 timer->hw.stop(timer);
510 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
511 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
512 snd_timer_reschedule(timer, 0);
513 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
514 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
515 timer->hw.start(timer);
521 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
522 spin_unlock_irqrestore(&timer->lock, flags);
524 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
525 snd_timer_notify1(timeri, event);
530 * stop the timer instance.
532 * do not call this from the timer callback!
534 int snd_timer_stop(struct snd_timer_instance *timeri)
536 struct snd_timer *timer;
540 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
543 timer = timeri->timer;
544 spin_lock_irqsave(&timer->lock, flags);
545 timeri->cticks = timeri->ticks;
547 spin_unlock_irqrestore(&timer->lock, flags);
552 * start again.. the tick is kept.
554 int snd_timer_continue(struct snd_timer_instance *timeri)
556 struct snd_timer *timer;
557 int result = -EINVAL;
562 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
563 return snd_timer_start_slave(timeri);
564 timer = timeri->timer;
567 spin_lock_irqsave(&timer->lock, flags);
571 result = snd_timer_start1(timer, timeri, timer->sticks);
572 spin_unlock_irqrestore(&timer->lock, flags);
573 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
578 * pause.. remember the ticks left
580 int snd_timer_pause(struct snd_timer_instance * timeri)
582 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
586 * reschedule the timer
588 * start pending instances and check the scheduling ticks.
589 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
591 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
593 struct snd_timer_instance *ti;
594 unsigned long ticks = ~0UL;
597 list_for_each(p, &timer->active_list_head) {
598 ti = list_entry(p, struct snd_timer_instance, active_list);
599 if (ti->flags & SNDRV_TIMER_IFLG_START) {
600 ti->flags &= ~SNDRV_TIMER_IFLG_START;
601 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
604 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
605 if (ticks > ti->cticks)
610 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
613 if (ticks > timer->hw.ticks)
614 ticks = timer->hw.ticks;
615 if (ticks_left != ticks)
616 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
617 timer->sticks = ticks;
624 static void snd_timer_tasklet(unsigned long arg)
626 struct snd_timer *timer = (struct snd_timer *) arg;
627 struct snd_timer_instance *ti;
629 unsigned long resolution, ticks;
631 spin_lock(&timer->lock);
632 /* now process all callbacks */
633 while (!list_empty(&timer->sack_list_head)) {
634 p = timer->sack_list_head.next; /* get first item */
635 ti = list_entry(p, struct snd_timer_instance, ack_list);
637 /* remove from ack_list and make empty */
642 resolution = ti->resolution;
644 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
645 spin_unlock(&timer->lock);
647 ti->callback(ti, resolution, ticks);
648 spin_lock(&timer->lock);
649 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
651 spin_unlock(&timer->lock);
657 * ticks_left is usually equal to timer->sticks.
660 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
662 struct snd_timer_instance *ti, *ts;
663 unsigned long resolution, ticks;
664 struct list_head *p, *q, *n, *ack_list_head;
670 spin_lock(&timer->lock);
672 /* remember the current resolution */
673 if (timer->hw.c_resolution)
674 resolution = timer->hw.c_resolution(timer);
676 resolution = timer->hw.resolution;
678 /* loop for all active instances
679 * Here we cannot use list_for_each because the active_list of a
680 * processed instance is relinked to done_list_head before the callback
683 list_for_each_safe(p, n, &timer->active_list_head) {
684 ti = list_entry(p, struct snd_timer_instance, active_list);
685 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
687 ti->pticks += ticks_left;
688 ti->resolution = resolution;
689 if (ti->cticks < ticks_left)
692 ti->cticks -= ticks_left;
693 if (ti->cticks) /* not expired */
695 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
696 ti->cticks = ti->ticks;
698 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
699 if (--timer->running)
702 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
703 (ti->flags & SNDRV_TIMER_IFLG_FAST))
704 ack_list_head = &timer->ack_list_head;
706 ack_list_head = &timer->sack_list_head;
707 if (list_empty(&ti->ack_list))
708 list_add_tail(&ti->ack_list, ack_list_head);
709 list_for_each(q, &ti->slave_active_head) {
710 ts = list_entry(q, struct snd_timer_instance, active_list);
711 ts->pticks = ti->pticks;
712 ts->resolution = resolution;
713 if (list_empty(&ts->ack_list))
714 list_add_tail(&ts->ack_list, ack_list_head);
717 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
718 snd_timer_reschedule(timer, ticks_left);
719 if (timer->running) {
720 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
721 timer->hw.stop(timer);
722 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
724 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
725 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
727 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
728 timer->hw.start(timer);
731 timer->hw.stop(timer);
734 /* now process all fast callbacks */
735 while (!list_empty(&timer->ack_list_head)) {
736 p = timer->ack_list_head.next; /* get first item */
737 ti = list_entry(p, struct snd_timer_instance, ack_list);
739 /* remove from ack_list and make empty */
745 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
746 spin_unlock(&timer->lock);
748 ti->callback(ti, resolution, ticks);
749 spin_lock(&timer->lock);
750 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
753 /* do we have any slow callbacks? */
754 use_tasklet = !list_empty(&timer->sack_list_head);
755 spin_unlock(&timer->lock);
758 tasklet_hi_schedule(&timer->task_queue);
765 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
766 struct snd_timer **rtimer)
768 struct snd_timer *timer;
770 static struct snd_device_ops ops = {
771 .dev_free = snd_timer_dev_free,
772 .dev_register = snd_timer_dev_register,
773 .dev_unregister = snd_timer_dev_unregister
776 snd_assert(tid != NULL, return -EINVAL);
777 snd_assert(rtimer != NULL, return -EINVAL);
779 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
782 timer->tmr_class = tid->dev_class;
784 timer->tmr_device = tid->device;
785 timer->tmr_subdevice = tid->subdevice;
787 strlcpy(timer->id, id, sizeof(timer->id));
788 INIT_LIST_HEAD(&timer->device_list);
789 INIT_LIST_HEAD(&timer->open_list_head);
790 INIT_LIST_HEAD(&timer->active_list_head);
791 INIT_LIST_HEAD(&timer->ack_list_head);
792 INIT_LIST_HEAD(&timer->sack_list_head);
793 spin_lock_init(&timer->lock);
794 tasklet_init(&timer->task_queue, snd_timer_tasklet,
795 (unsigned long)timer);
797 timer->module = card->module;
798 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
800 snd_timer_free(timer);
808 static int snd_timer_free(struct snd_timer *timer)
810 snd_assert(timer != NULL, return -ENXIO);
811 if (timer->private_free)
812 timer->private_free(timer);
817 static int snd_timer_dev_free(struct snd_device *device)
819 struct snd_timer *timer = device->device_data;
820 return snd_timer_free(timer);
823 static int snd_timer_dev_register(struct snd_device *dev)
825 struct snd_timer *timer = dev->device_data;
826 struct snd_timer *timer1;
829 snd_assert(timer != NULL && timer->hw.start != NULL &&
830 timer->hw.stop != NULL, return -ENXIO);
831 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
832 !timer->hw.resolution && timer->hw.c_resolution == NULL)
835 down(®ister_mutex);
836 list_for_each(p, &snd_timer_list) {
837 timer1 = list_entry(p, struct snd_timer, device_list);
838 if (timer1->tmr_class > timer->tmr_class)
840 if (timer1->tmr_class < timer->tmr_class)
842 if (timer1->card && timer->card) {
843 if (timer1->card->number > timer->card->number)
845 if (timer1->card->number < timer->card->number)
848 if (timer1->tmr_device > timer->tmr_device)
850 if (timer1->tmr_device < timer->tmr_device)
852 if (timer1->tmr_subdevice > timer->tmr_subdevice)
854 if (timer1->tmr_subdevice < timer->tmr_subdevice)
860 list_add_tail(&timer->device_list, p);
865 static int snd_timer_unregister(struct snd_timer *timer)
867 struct list_head *p, *n;
868 struct snd_timer_instance *ti;
870 snd_assert(timer != NULL, return -ENXIO);
871 down(®ister_mutex);
872 if (! list_empty(&timer->open_list_head)) {
873 snd_printk(KERN_WARNING "timer 0x%lx is busy?\n", (long)timer);
874 list_for_each_safe(p, n, &timer->open_list_head) {
876 ti = list_entry(p, struct snd_timer_instance, open_list);
880 list_del(&timer->device_list);
882 return snd_timer_free(timer);
885 static int snd_timer_dev_unregister(struct snd_device *device)
887 struct snd_timer *timer = device->device_data;
888 return snd_timer_unregister(timer);
891 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
894 unsigned long resolution = 0;
895 struct snd_timer_instance *ti, *ts;
896 struct list_head *p, *n;
898 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
900 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
901 event <= SNDRV_TIMER_EVENT_MRESUME, return);
902 spin_lock_irqsave(&timer->lock, flags);
903 if (event == SNDRV_TIMER_EVENT_MSTART ||
904 event == SNDRV_TIMER_EVENT_MCONTINUE ||
905 event == SNDRV_TIMER_EVENT_MRESUME) {
906 if (timer->hw.c_resolution)
907 resolution = timer->hw.c_resolution(timer);
909 resolution = timer->hw.resolution;
911 list_for_each(p, &timer->active_list_head) {
912 ti = list_entry(p, struct snd_timer_instance, active_list);
914 ti->ccallback(ti, event, tstamp, resolution);
915 list_for_each(n, &ti->slave_active_head) {
916 ts = list_entry(n, struct snd_timer_instance, active_list);
918 ts->ccallback(ts, event, tstamp, resolution);
921 spin_unlock_irqrestore(&timer->lock, flags);
925 * exported functions for global timers
927 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
929 struct snd_timer_id tid;
931 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
932 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
936 return snd_timer_new(NULL, id, &tid, rtimer);
939 int snd_timer_global_free(struct snd_timer *timer)
941 return snd_timer_free(timer);
944 int snd_timer_global_register(struct snd_timer *timer)
946 struct snd_device dev;
948 memset(&dev, 0, sizeof(dev));
949 dev.device_data = timer;
950 return snd_timer_dev_register(&dev);
953 int snd_timer_global_unregister(struct snd_timer *timer)
955 return snd_timer_unregister(timer);
962 struct snd_timer_system_private {
963 struct timer_list tlist;
964 struct timer * timer;
965 unsigned long last_expires;
966 unsigned long last_jiffies;
967 unsigned long correction;
970 static void snd_timer_s_function(unsigned long data)
972 struct snd_timer *timer = (struct snd_timer *)data;
973 struct snd_timer_system_private *priv = timer->private_data;
974 unsigned long jiff = jiffies;
975 if (time_after(jiff, priv->last_expires))
976 priv->correction = (long)jiff - (long)priv->last_expires;
977 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
980 static int snd_timer_s_start(struct snd_timer * timer)
982 struct snd_timer_system_private *priv;
985 priv = (struct snd_timer_system_private *) timer->private_data;
986 njiff = (priv->last_jiffies = jiffies);
987 if (priv->correction > timer->sticks - 1) {
988 priv->correction -= timer->sticks - 1;
991 njiff += timer->sticks - priv->correction;
992 priv->correction -= timer->sticks;
994 priv->last_expires = priv->tlist.expires = njiff;
995 add_timer(&priv->tlist);
999 static int snd_timer_s_stop(struct snd_timer * timer)
1001 struct snd_timer_system_private *priv;
1004 priv = (struct snd_timer_system_private *) timer->private_data;
1005 del_timer(&priv->tlist);
1007 if (time_before(jiff, priv->last_expires))
1008 timer->sticks = priv->last_expires - jiff;
1014 static struct snd_timer_hardware snd_timer_system =
1016 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1017 .resolution = 1000000000L / HZ,
1019 .start = snd_timer_s_start,
1020 .stop = snd_timer_s_stop
1023 static void snd_timer_free_system(struct snd_timer *timer)
1025 kfree(timer->private_data);
1028 static int snd_timer_register_system(void)
1030 struct snd_timer *timer;
1031 struct snd_timer_system_private *priv;
1034 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1037 strcpy(timer->name, "system timer");
1038 timer->hw = snd_timer_system;
1039 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1041 snd_timer_free(timer);
1044 init_timer(&priv->tlist);
1045 priv->tlist.function = snd_timer_s_function;
1046 priv->tlist.data = (unsigned long) timer;
1047 timer->private_data = priv;
1048 timer->private_free = snd_timer_free_system;
1049 return snd_timer_global_register(timer);
1056 static void snd_timer_proc_read(struct snd_info_entry *entry,
1057 struct snd_info_buffer *buffer)
1059 unsigned long flags;
1060 struct snd_timer *timer;
1061 struct snd_timer_instance *ti;
1062 struct list_head *p, *q;
1064 down(®ister_mutex);
1065 list_for_each(p, &snd_timer_list) {
1066 timer = list_entry(p, struct snd_timer, device_list);
1067 switch (timer->tmr_class) {
1068 case SNDRV_TIMER_CLASS_GLOBAL:
1069 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1071 case SNDRV_TIMER_CLASS_CARD:
1072 snd_iprintf(buffer, "C%i-%i: ",
1073 timer->card->number, timer->tmr_device);
1075 case SNDRV_TIMER_CLASS_PCM:
1076 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1077 timer->tmr_device, timer->tmr_subdevice);
1080 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1081 timer->card ? timer->card->number : -1,
1082 timer->tmr_device, timer->tmr_subdevice);
1084 snd_iprintf(buffer, "%s :", timer->name);
1085 if (timer->hw.resolution)
1086 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1087 timer->hw.resolution / 1000,
1088 timer->hw.resolution % 1000,
1090 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1091 snd_iprintf(buffer, " SLAVE");
1092 snd_iprintf(buffer, "\n");
1093 spin_lock_irqsave(&timer->lock, flags);
1094 list_for_each(q, &timer->open_list_head) {
1095 ti = list_entry(q, struct snd_timer_instance, open_list);
1096 snd_iprintf(buffer, " Client %s : %s\n",
1097 ti->owner ? ti->owner : "unknown",
1098 ti->flags & (SNDRV_TIMER_IFLG_START |
1099 SNDRV_TIMER_IFLG_RUNNING)
1100 ? "running" : "stopped");
1102 spin_unlock_irqrestore(&timer->lock, flags);
1104 up(®ister_mutex);
1108 * USER SPACE interface
1111 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1112 unsigned long resolution,
1113 unsigned long ticks)
1115 struct snd_timer_user *tu = timeri->callback_data;
1116 struct snd_timer_read *r;
1119 spin_lock(&tu->qlock);
1120 if (tu->qused > 0) {
1121 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1122 r = &tu->queue[prev];
1123 if (r->resolution == resolution) {
1128 if (tu->qused >= tu->queue_size) {
1131 r = &tu->queue[tu->qtail++];
1132 tu->qtail %= tu->queue_size;
1133 r->resolution = resolution;
1138 spin_unlock(&tu->qlock);
1139 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1140 wake_up(&tu->qchange_sleep);
1143 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1144 struct snd_timer_tread *tread)
1146 if (tu->qused >= tu->queue_size) {
1149 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1150 tu->qtail %= tu->queue_size;
1155 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1157 struct timespec *tstamp,
1158 unsigned long resolution)
1160 struct snd_timer_user *tu = timeri->callback_data;
1161 struct snd_timer_tread r1;
1163 if (event >= SNDRV_TIMER_EVENT_START &&
1164 event <= SNDRV_TIMER_EVENT_PAUSE)
1165 tu->tstamp = *tstamp;
1166 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1169 r1.tstamp = *tstamp;
1170 r1.val = resolution;
1171 spin_lock(&tu->qlock);
1172 snd_timer_user_append_to_tqueue(tu, &r1);
1173 spin_unlock(&tu->qlock);
1174 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1175 wake_up(&tu->qchange_sleep);
1178 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1179 unsigned long resolution,
1180 unsigned long ticks)
1182 struct snd_timer_user *tu = timeri->callback_data;
1183 struct snd_timer_tread *r, r1;
1184 struct timespec tstamp;
1185 int prev, append = 0;
1187 memset(&tstamp, 0, sizeof(tstamp));
1188 spin_lock(&tu->qlock);
1189 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1190 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1191 spin_unlock(&tu->qlock);
1194 if (tu->last_resolution != resolution || ticks > 0)
1195 getnstimeofday(&tstamp);
1196 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1197 tu->last_resolution != resolution) {
1198 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1200 r1.val = resolution;
1201 snd_timer_user_append_to_tqueue(tu, &r1);
1202 tu->last_resolution = resolution;
1205 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1209 if (tu->qused > 0) {
1210 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1211 r = &tu->tqueue[prev];
1212 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1219 r1.event = SNDRV_TIMER_EVENT_TICK;
1222 snd_timer_user_append_to_tqueue(tu, &r1);
1225 spin_unlock(&tu->qlock);
1228 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1229 wake_up(&tu->qchange_sleep);
1232 static int snd_timer_user_open(struct inode *inode, struct file *file)
1234 struct snd_timer_user *tu;
1236 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1239 spin_lock_init(&tu->qlock);
1240 init_waitqueue_head(&tu->qchange_sleep);
1241 init_MUTEX(&tu->tread_sem);
1243 tu->queue_size = 128;
1244 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1246 if (tu->queue == NULL) {
1250 file->private_data = tu;
1254 static int snd_timer_user_release(struct inode *inode, struct file *file)
1256 struct snd_timer_user *tu;
1258 if (file->private_data) {
1259 tu = file->private_data;
1260 file->private_data = NULL;
1261 fasync_helper(-1, file, 0, &tu->fasync);
1263 snd_timer_close(tu->timeri);
1271 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1273 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1274 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1280 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1282 id->dev_class = timer->tmr_class;
1283 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1284 id->card = timer->card ? timer->card->number : -1;
1285 id->device = timer->tmr_device;
1286 id->subdevice = timer->tmr_subdevice;
1289 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1291 struct snd_timer_id id;
1292 struct snd_timer *timer;
1293 struct list_head *p;
1295 if (copy_from_user(&id, _tid, sizeof(id)))
1297 down(®ister_mutex);
1298 if (id.dev_class < 0) { /* first item */
1299 if (list_empty(&snd_timer_list))
1300 snd_timer_user_zero_id(&id);
1302 timer = list_entry(snd_timer_list.next,
1303 struct snd_timer, device_list);
1304 snd_timer_user_copy_id(&id, timer);
1307 switch (id.dev_class) {
1308 case SNDRV_TIMER_CLASS_GLOBAL:
1309 id.device = id.device < 0 ? 0 : id.device + 1;
1310 list_for_each(p, &snd_timer_list) {
1311 timer = list_entry(p, struct snd_timer, device_list);
1312 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1313 snd_timer_user_copy_id(&id, timer);
1316 if (timer->tmr_device >= id.device) {
1317 snd_timer_user_copy_id(&id, timer);
1321 if (p == &snd_timer_list)
1322 snd_timer_user_zero_id(&id);
1324 case SNDRV_TIMER_CLASS_CARD:
1325 case SNDRV_TIMER_CLASS_PCM:
1332 if (id.device < 0) {
1335 if (id.subdevice < 0) {
1343 list_for_each(p, &snd_timer_list) {
1344 timer = list_entry(p, struct snd_timer, device_list);
1345 if (timer->tmr_class > id.dev_class) {
1346 snd_timer_user_copy_id(&id, timer);
1349 if (timer->tmr_class < id.dev_class)
1351 if (timer->card->number > id.card) {
1352 snd_timer_user_copy_id(&id, timer);
1355 if (timer->card->number < id.card)
1357 if (timer->tmr_device > id.device) {
1358 snd_timer_user_copy_id(&id, timer);
1361 if (timer->tmr_device < id.device)
1363 if (timer->tmr_subdevice > id.subdevice) {
1364 snd_timer_user_copy_id(&id, timer);
1367 if (timer->tmr_subdevice < id.subdevice)
1369 snd_timer_user_copy_id(&id, timer);
1372 if (p == &snd_timer_list)
1373 snd_timer_user_zero_id(&id);
1376 snd_timer_user_zero_id(&id);
1379 up(®ister_mutex);
1380 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1385 static int snd_timer_user_ginfo(struct file *file,
1386 struct snd_timer_ginfo __user *_ginfo)
1388 struct snd_timer_ginfo *ginfo;
1389 struct snd_timer_id tid;
1390 struct snd_timer *t;
1391 struct list_head *p;
1394 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1397 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1402 memset(ginfo, 0, sizeof(*ginfo));
1404 down(®ister_mutex);
1405 t = snd_timer_find(&tid);
1407 ginfo->card = t->card ? t->card->number : -1;
1408 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1409 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1410 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1411 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1412 ginfo->resolution = t->hw.resolution;
1413 if (t->hw.resolution_min > 0) {
1414 ginfo->resolution_min = t->hw.resolution_min;
1415 ginfo->resolution_max = t->hw.resolution_max;
1417 list_for_each(p, &t->open_list_head) {
1423 up(®ister_mutex);
1424 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1430 static int snd_timer_user_gparams(struct file *file,
1431 struct snd_timer_gparams __user *_gparams)
1433 struct snd_timer_gparams gparams;
1434 struct snd_timer *t;
1437 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1439 down(®ister_mutex);
1440 t = snd_timer_find(&gparams.tid);
1445 if (!list_empty(&t->open_list_head)) {
1449 if (!t->hw.set_period) {
1453 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1455 up(®ister_mutex);
1459 static int snd_timer_user_gstatus(struct file *file,
1460 struct snd_timer_gstatus __user *_gstatus)
1462 struct snd_timer_gstatus gstatus;
1463 struct snd_timer_id tid;
1464 struct snd_timer *t;
1467 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1470 memset(&gstatus, 0, sizeof(gstatus));
1472 down(®ister_mutex);
1473 t = snd_timer_find(&tid);
1475 if (t->hw.c_resolution)
1476 gstatus.resolution = t->hw.c_resolution(t);
1478 gstatus.resolution = t->hw.resolution;
1479 if (t->hw.precise_resolution) {
1480 t->hw.precise_resolution(t, &gstatus.resolution_num,
1481 &gstatus.resolution_den);
1483 gstatus.resolution_num = gstatus.resolution;
1484 gstatus.resolution_den = 1000000000uL;
1489 up(®ister_mutex);
1490 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1495 static int snd_timer_user_tselect(struct file *file,
1496 struct snd_timer_select __user *_tselect)
1498 struct snd_timer_user *tu;
1499 struct snd_timer_select tselect;
1503 tu = file->private_data;
1504 down(&tu->tread_sem);
1506 snd_timer_close(tu->timeri);
1509 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1513 sprintf(str, "application %i", current->pid);
1514 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1515 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1516 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1525 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1527 if (tu->tqueue == NULL)
1530 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1532 if (tu->queue == NULL)
1537 snd_timer_close(tu->timeri);
1540 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1541 tu->timeri->callback = tu->tread
1542 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1543 tu->timeri->ccallback = snd_timer_user_ccallback;
1544 tu->timeri->callback_data = (void *)tu;
1552 static int snd_timer_user_info(struct file *file,
1553 struct snd_timer_info __user *_info)
1555 struct snd_timer_user *tu;
1556 struct snd_timer_info *info;
1557 struct snd_timer *t;
1560 tu = file->private_data;
1561 snd_assert(tu->timeri != NULL, return -ENXIO);
1562 t = tu->timeri->timer;
1563 snd_assert(t != NULL, return -ENXIO);
1565 info = kzalloc(sizeof(*info), GFP_KERNEL);
1568 info->card = t->card ? t->card->number : -1;
1569 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1570 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1571 strlcpy(info->id, t->id, sizeof(info->id));
1572 strlcpy(info->name, t->name, sizeof(info->name));
1573 info->resolution = t->hw.resolution;
1574 if (copy_to_user(_info, info, sizeof(*_info)))
1580 static int snd_timer_user_params(struct file *file,
1581 struct snd_timer_params __user *_params)
1583 struct snd_timer_user *tu;
1584 struct snd_timer_params params;
1585 struct snd_timer *t;
1586 struct snd_timer_read *tr;
1587 struct snd_timer_tread *ttr;
1590 tu = file->private_data;
1591 snd_assert(tu->timeri != NULL, return -ENXIO);
1592 t = tu->timeri->timer;
1593 snd_assert(t != NULL, return -ENXIO);
1594 if (copy_from_user(¶ms, _params, sizeof(params)))
1596 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1600 if (params.queue_size > 0 &&
1601 (params.queue_size < 32 || params.queue_size > 1024)) {
1605 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1606 (1<<SNDRV_TIMER_EVENT_TICK)|
1607 (1<<SNDRV_TIMER_EVENT_START)|
1608 (1<<SNDRV_TIMER_EVENT_STOP)|
1609 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1610 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1611 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1612 (1<<SNDRV_TIMER_EVENT_RESUME)|
1613 (1<<SNDRV_TIMER_EVENT_MSTART)|
1614 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1615 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1616 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1617 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1618 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1622 snd_timer_stop(tu->timeri);
1623 spin_lock_irq(&t->lock);
1624 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1625 SNDRV_TIMER_IFLG_EXCLUSIVE|
1626 SNDRV_TIMER_IFLG_EARLY_EVENT);
1627 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1628 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1629 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1630 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1631 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1632 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1633 spin_unlock_irq(&t->lock);
1634 if (params.queue_size > 0 &&
1635 (unsigned int)tu->queue_size != params.queue_size) {
1637 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1641 tu->queue_size = params.queue_size;
1645 tr = kmalloc(params.queue_size * sizeof(*tr),
1649 tu->queue_size = params.queue_size;
1654 tu->qhead = tu->qtail = tu->qused = 0;
1655 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1657 struct snd_timer_tread tread;
1658 tread.event = SNDRV_TIMER_EVENT_EARLY;
1659 tread.tstamp.tv_sec = 0;
1660 tread.tstamp.tv_nsec = 0;
1662 snd_timer_user_append_to_tqueue(tu, &tread);
1664 struct snd_timer_read *r = &tu->queue[0];
1671 tu->filter = params.filter;
1672 tu->ticks = params.ticks;
1675 if (copy_to_user(_params, ¶ms, sizeof(params)))
1680 static int snd_timer_user_status(struct file *file,
1681 struct snd_timer_status __user *_status)
1683 struct snd_timer_user *tu;
1684 struct snd_timer_status status;
1686 tu = file->private_data;
1687 snd_assert(tu->timeri != NULL, return -ENXIO);
1688 memset(&status, 0, sizeof(status));
1689 status.tstamp = tu->tstamp;
1690 status.resolution = snd_timer_resolution(tu->timeri);
1691 status.lost = tu->timeri->lost;
1692 status.overrun = tu->overrun;
1693 spin_lock_irq(&tu->qlock);
1694 status.queue = tu->qused;
1695 spin_unlock_irq(&tu->qlock);
1696 if (copy_to_user(_status, &status, sizeof(status)))
1701 static int snd_timer_user_start(struct file *file)
1704 struct snd_timer_user *tu;
1706 tu = file->private_data;
1707 snd_assert(tu->timeri != NULL, return -ENXIO);
1708 snd_timer_stop(tu->timeri);
1709 tu->timeri->lost = 0;
1710 tu->last_resolution = 0;
1711 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1714 static int snd_timer_user_stop(struct file *file)
1717 struct snd_timer_user *tu;
1719 tu = file->private_data;
1720 snd_assert(tu->timeri != NULL, return -ENXIO);
1721 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1724 static int snd_timer_user_continue(struct file *file)
1727 struct snd_timer_user *tu;
1729 tu = file->private_data;
1730 snd_assert(tu->timeri != NULL, return -ENXIO);
1731 tu->timeri->lost = 0;
1732 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1735 static int snd_timer_user_pause(struct file *file)
1738 struct snd_timer_user *tu;
1740 tu = file->private_data;
1741 snd_assert(tu->timeri != NULL, return -ENXIO);
1742 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1746 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1747 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1748 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1749 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1752 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1755 struct snd_timer_user *tu;
1756 void __user *argp = (void __user *)arg;
1757 int __user *p = argp;
1759 tu = file->private_data;
1761 case SNDRV_TIMER_IOCTL_PVERSION:
1762 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1763 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1764 return snd_timer_user_next_device(argp);
1765 case SNDRV_TIMER_IOCTL_TREAD:
1769 down(&tu->tread_sem);
1770 if (tu->timeri) { /* too late */
1774 if (get_user(xarg, p)) {
1778 tu->tread = xarg ? 1 : 0;
1782 case SNDRV_TIMER_IOCTL_GINFO:
1783 return snd_timer_user_ginfo(file, argp);
1784 case SNDRV_TIMER_IOCTL_GPARAMS:
1785 return snd_timer_user_gparams(file, argp);
1786 case SNDRV_TIMER_IOCTL_GSTATUS:
1787 return snd_timer_user_gstatus(file, argp);
1788 case SNDRV_TIMER_IOCTL_SELECT:
1789 return snd_timer_user_tselect(file, argp);
1790 case SNDRV_TIMER_IOCTL_INFO:
1791 return snd_timer_user_info(file, argp);
1792 case SNDRV_TIMER_IOCTL_PARAMS:
1793 return snd_timer_user_params(file, argp);
1794 case SNDRV_TIMER_IOCTL_STATUS:
1795 return snd_timer_user_status(file, argp);
1796 case SNDRV_TIMER_IOCTL_START:
1797 case SNDRV_TIMER_IOCTL_START_OLD:
1798 return snd_timer_user_start(file);
1799 case SNDRV_TIMER_IOCTL_STOP:
1800 case SNDRV_TIMER_IOCTL_STOP_OLD:
1801 return snd_timer_user_stop(file);
1802 case SNDRV_TIMER_IOCTL_CONTINUE:
1803 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1804 return snd_timer_user_continue(file);
1805 case SNDRV_TIMER_IOCTL_PAUSE:
1806 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1807 return snd_timer_user_pause(file);
1812 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1814 struct snd_timer_user *tu;
1817 tu = file->private_data;
1818 err = fasync_helper(fd, file, on, &tu->fasync);
1824 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1825 size_t count, loff_t *offset)
1827 struct snd_timer_user *tu;
1828 long result = 0, unit;
1831 tu = file->private_data;
1832 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1833 spin_lock_irq(&tu->qlock);
1834 while ((long)count - result >= unit) {
1835 while (!tu->qused) {
1838 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1843 set_current_state(TASK_INTERRUPTIBLE);
1844 init_waitqueue_entry(&wait, current);
1845 add_wait_queue(&tu->qchange_sleep, &wait);
1847 spin_unlock_irq(&tu->qlock);
1849 spin_lock_irq(&tu->qlock);
1851 remove_wait_queue(&tu->qchange_sleep, &wait);
1853 if (signal_pending(current)) {
1859 spin_unlock_irq(&tu->qlock);
1864 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1865 sizeof(struct snd_timer_tread))) {
1870 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1871 sizeof(struct snd_timer_read))) {
1877 tu->qhead %= tu->queue_size;
1882 spin_lock_irq(&tu->qlock);
1885 spin_unlock_irq(&tu->qlock);
1887 return result > 0 ? result : err;
1890 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1893 struct snd_timer_user *tu;
1895 tu = file->private_data;
1897 poll_wait(file, &tu->qchange_sleep, wait);
1901 mask |= POLLIN | POLLRDNORM;
1906 #ifdef CONFIG_COMPAT
1907 #include "timer_compat.c"
1909 #define snd_timer_user_ioctl_compat NULL
1912 static struct file_operations snd_timer_f_ops =
1914 .owner = THIS_MODULE,
1915 .read = snd_timer_user_read,
1916 .open = snd_timer_user_open,
1917 .release = snd_timer_user_release,
1918 .poll = snd_timer_user_poll,
1919 .unlocked_ioctl = snd_timer_user_ioctl,
1920 .compat_ioctl = snd_timer_user_ioctl_compat,
1921 .fasync = snd_timer_user_fasync,
1924 static struct snd_minor snd_timer_reg =
1927 .f_ops = &snd_timer_f_ops,
1934 static struct snd_info_entry *snd_timer_proc_entry = NULL;
1936 static int __init alsa_timer_init(void)
1939 struct snd_info_entry *entry;
1941 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1942 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1945 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1946 if (entry != NULL) {
1947 entry->c.text.read_size = SNDRV_TIMER_DEVICES * 128;
1948 entry->c.text.read = snd_timer_proc_read;
1949 if (snd_info_register(entry) < 0) {
1950 snd_info_free_entry(entry);
1954 snd_timer_proc_entry = entry;
1955 if ((err = snd_timer_register_system()) < 0)
1956 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1958 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER,
1959 NULL, 0, &snd_timer_reg, "timer"))<0)
1960 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1965 static void __exit alsa_timer_exit(void)
1967 struct list_head *p, *n;
1969 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1970 /* unregister the system timer */
1971 list_for_each_safe(p, n, &snd_timer_list) {
1972 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
1973 snd_timer_unregister(timer);
1975 if (snd_timer_proc_entry) {
1976 snd_info_unregister(snd_timer_proc_entry);
1977 snd_timer_proc_entry = NULL;
1979 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1980 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1984 module_init(alsa_timer_init)
1985 module_exit(alsa_timer_exit)
1987 EXPORT_SYMBOL(snd_timer_open);
1988 EXPORT_SYMBOL(snd_timer_close);
1989 EXPORT_SYMBOL(snd_timer_resolution);
1990 EXPORT_SYMBOL(snd_timer_start);
1991 EXPORT_SYMBOL(snd_timer_stop);
1992 EXPORT_SYMBOL(snd_timer_continue);
1993 EXPORT_SYMBOL(snd_timer_pause);
1994 EXPORT_SYMBOL(snd_timer_new);
1995 EXPORT_SYMBOL(snd_timer_notify);
1996 EXPORT_SYMBOL(snd_timer_global_new);
1997 EXPORT_SYMBOL(snd_timer_global_free);
1998 EXPORT_SYMBOL(snd_timer_global_register);
1999 EXPORT_SYMBOL(snd_timer_global_unregister);
2000 EXPORT_SYMBOL(snd_timer_interrupt);