2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/mutex.h>
27 #include <linux/moduleparam.h>
28 #include <linux/string.h>
29 #include <sound/core.h>
30 #include <sound/timer.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/minors.h>
34 #include <sound/initval.h>
35 #include <linux/kmod.h>
37 #if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
38 #define DEFAULT_TIMER_LIMIT 3
39 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
40 #define DEFAULT_TIMER_LIMIT 2
42 #define DEFAULT_TIMER_LIMIT 1
45 static int timer_limit = DEFAULT_TIMER_LIMIT;
46 static int timer_tstamp_monotonic = 1;
47 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
48 MODULE_DESCRIPTION("ALSA timer interface");
49 MODULE_LICENSE("GPL");
50 module_param(timer_limit, int, 0444);
51 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
52 module_param(timer_tstamp_monotonic, int, 0444);
53 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
55 struct snd_timer_user {
56 struct snd_timer_instance *timeri;
57 int tread; /* enhanced read with timestamps and events */
59 unsigned long overrun;
64 struct snd_timer_read *queue;
65 struct snd_timer_tread *tqueue;
67 unsigned long last_resolution;
69 struct timespec tstamp; /* trigger tstamp */
70 wait_queue_head_t qchange_sleep;
71 struct fasync_struct *fasync;
72 struct mutex tread_sem;
76 static LIST_HEAD(snd_timer_list);
78 /* list of slave instances */
79 static LIST_HEAD(snd_timer_slave_list);
81 /* lock for slave active lists */
82 static DEFINE_SPINLOCK(slave_active_lock);
84 static DEFINE_MUTEX(register_mutex);
86 static int snd_timer_free(struct snd_timer *timer);
87 static int snd_timer_dev_free(struct snd_device *device);
88 static int snd_timer_dev_register(struct snd_device *device);
89 static int snd_timer_dev_disconnect(struct snd_device *device);
91 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
94 * create a timer instance with the given owner string.
95 * when timer is not NULL, increments the module counter
97 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
98 struct snd_timer *timer)
100 struct snd_timer_instance *timeri;
101 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
104 timeri->owner = kstrdup(owner, GFP_KERNEL);
105 if (! timeri->owner) {
109 INIT_LIST_HEAD(&timeri->open_list);
110 INIT_LIST_HEAD(&timeri->active_list);
111 INIT_LIST_HEAD(&timeri->ack_list);
112 INIT_LIST_HEAD(&timeri->slave_list_head);
113 INIT_LIST_HEAD(&timeri->slave_active_head);
115 timeri->timer = timer;
116 if (timer && !try_module_get(timer->module)) {
117 kfree(timeri->owner);
126 * find a timer instance from the given timer id
128 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
130 struct snd_timer *timer = NULL;
132 list_for_each_entry(timer, &snd_timer_list, device_list) {
133 if (timer->tmr_class != tid->dev_class)
135 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
136 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
137 (timer->card == NULL ||
138 timer->card->number != tid->card))
140 if (timer->tmr_device != tid->device)
142 if (timer->tmr_subdevice != tid->subdevice)
151 static void snd_timer_request(struct snd_timer_id *tid)
153 switch (tid->dev_class) {
154 case SNDRV_TIMER_CLASS_GLOBAL:
155 if (tid->device < timer_limit)
156 request_module("snd-timer-%i", tid->device);
158 case SNDRV_TIMER_CLASS_CARD:
159 case SNDRV_TIMER_CLASS_PCM:
160 if (tid->card < snd_ecards_limit)
161 request_module("snd-card-%i", tid->card);
171 * look for a master instance matching with the slave id of the given slave.
172 * when found, relink the open_link of the slave.
174 * call this with register_mutex down.
176 static void snd_timer_check_slave(struct snd_timer_instance *slave)
178 struct snd_timer *timer;
179 struct snd_timer_instance *master;
181 /* FIXME: it's really dumb to look up all entries.. */
182 list_for_each_entry(timer, &snd_timer_list, device_list) {
183 list_for_each_entry(master, &timer->open_list_head, open_list) {
184 if (slave->slave_class == master->slave_class &&
185 slave->slave_id == master->slave_id) {
186 list_del(&slave->open_list);
187 list_add_tail(&slave->open_list,
188 &master->slave_list_head);
189 spin_lock_irq(&slave_active_lock);
190 slave->master = master;
191 slave->timer = master->timer;
192 spin_unlock_irq(&slave_active_lock);
200 * look for slave instances matching with the slave id of the given master.
201 * when found, relink the open_link of slaves.
203 * call this with register_mutex down.
205 static void snd_timer_check_master(struct snd_timer_instance *master)
207 struct snd_timer_instance *slave, *tmp;
209 /* check all pending slaves */
210 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
211 if (slave->slave_class == master->slave_class &&
212 slave->slave_id == master->slave_id) {
213 list_move_tail(&slave->open_list, &master->slave_list_head);
214 spin_lock_irq(&slave_active_lock);
215 slave->master = master;
216 slave->timer = master->timer;
217 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
218 list_add_tail(&slave->active_list,
219 &master->slave_active_head);
220 spin_unlock_irq(&slave_active_lock);
226 * open a timer instance
227 * when opening a master, the slave id must be here given.
229 int snd_timer_open(struct snd_timer_instance **ti,
230 char *owner, struct snd_timer_id *tid,
231 unsigned int slave_id)
233 struct snd_timer *timer;
234 struct snd_timer_instance *timeri = NULL;
236 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
237 /* open a slave instance */
238 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
239 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
240 snd_printd("invalid slave class %i\n", tid->dev_sclass);
243 mutex_lock(®ister_mutex);
244 timeri = snd_timer_instance_new(owner, NULL);
246 mutex_unlock(®ister_mutex);
249 timeri->slave_class = tid->dev_sclass;
250 timeri->slave_id = tid->device;
251 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
252 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
253 snd_timer_check_slave(timeri);
254 mutex_unlock(®ister_mutex);
259 /* open a master instance */
260 mutex_lock(®ister_mutex);
261 timer = snd_timer_find(tid);
264 mutex_unlock(®ister_mutex);
265 snd_timer_request(tid);
266 mutex_lock(®ister_mutex);
267 timer = snd_timer_find(tid);
271 mutex_unlock(®ister_mutex);
274 if (!list_empty(&timer->open_list_head)) {
275 timeri = list_entry(timer->open_list_head.next,
276 struct snd_timer_instance, open_list);
277 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
278 mutex_unlock(®ister_mutex);
282 timeri = snd_timer_instance_new(owner, timer);
284 mutex_unlock(®ister_mutex);
287 timeri->slave_class = tid->dev_sclass;
288 timeri->slave_id = slave_id;
289 if (list_empty(&timer->open_list_head) && timer->hw.open)
290 timer->hw.open(timer);
291 list_add_tail(&timeri->open_list, &timer->open_list_head);
292 snd_timer_check_master(timeri);
293 mutex_unlock(®ister_mutex);
298 static int _snd_timer_stop(struct snd_timer_instance *timeri,
299 int keep_flag, int event);
302 * close a timer instance
304 int snd_timer_close(struct snd_timer_instance *timeri)
306 struct snd_timer *timer = NULL;
307 struct snd_timer_instance *slave, *tmp;
309 snd_assert(timeri != NULL, return -ENXIO);
311 /* force to stop the timer */
312 snd_timer_stop(timeri);
314 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
315 /* wait, until the active callback is finished */
316 spin_lock_irq(&slave_active_lock);
317 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
318 spin_unlock_irq(&slave_active_lock);
320 spin_lock_irq(&slave_active_lock);
322 spin_unlock_irq(&slave_active_lock);
323 mutex_lock(®ister_mutex);
324 list_del(&timeri->open_list);
325 mutex_unlock(®ister_mutex);
327 timer = timeri->timer;
328 /* wait, until the active callback is finished */
329 spin_lock_irq(&timer->lock);
330 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
331 spin_unlock_irq(&timer->lock);
333 spin_lock_irq(&timer->lock);
335 spin_unlock_irq(&timer->lock);
336 mutex_lock(®ister_mutex);
337 list_del(&timeri->open_list);
338 if (timer && list_empty(&timer->open_list_head) &&
340 timer->hw.close(timer);
341 /* remove slave links */
342 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
344 spin_lock_irq(&slave_active_lock);
345 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
346 list_move_tail(&slave->open_list, &snd_timer_slave_list);
347 slave->master = NULL;
349 spin_unlock_irq(&slave_active_lock);
351 mutex_unlock(®ister_mutex);
353 if (timeri->private_free)
354 timeri->private_free(timeri);
355 kfree(timeri->owner);
358 module_put(timer->module);
362 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
364 struct snd_timer * timer;
368 if ((timer = timeri->timer) != NULL) {
369 if (timer->hw.c_resolution)
370 return timer->hw.c_resolution(timer);
371 return timer->hw.resolution;
376 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
378 struct snd_timer *timer;
380 unsigned long resolution = 0;
381 struct snd_timer_instance *ts;
382 struct timespec tstamp;
384 if (timer_tstamp_monotonic)
385 do_posix_clock_monotonic_gettime(&tstamp);
387 getnstimeofday(&tstamp);
388 snd_assert(event >= SNDRV_TIMER_EVENT_START &&
389 event <= SNDRV_TIMER_EVENT_PAUSE, return);
390 if (event == SNDRV_TIMER_EVENT_START ||
391 event == SNDRV_TIMER_EVENT_CONTINUE)
392 resolution = snd_timer_resolution(ti);
394 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
395 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
400 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
402 spin_lock_irqsave(&timer->lock, flags);
403 list_for_each_entry(ts, &ti->slave_active_head, active_list)
405 ts->ccallback(ti, event + 100, &tstamp, resolution);
406 spin_unlock_irqrestore(&timer->lock, flags);
409 static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
410 unsigned long sticks)
412 list_del(&timeri->active_list);
413 list_add_tail(&timeri->active_list, &timer->active_list_head);
414 if (timer->running) {
415 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
417 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
418 timeri->flags |= SNDRV_TIMER_IFLG_START;
419 return 1; /* delayed start */
421 timer->sticks = sticks;
422 timer->hw.start(timer);
425 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
430 static int snd_timer_start_slave(struct snd_timer_instance *timeri)
434 spin_lock_irqsave(&slave_active_lock, flags);
435 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
437 list_add_tail(&timeri->active_list,
438 &timeri->master->slave_active_head);
439 spin_unlock_irqrestore(&slave_active_lock, flags);
440 return 1; /* delayed start */
444 * start the timer instance
446 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
448 struct snd_timer *timer;
449 int result = -EINVAL;
452 if (timeri == NULL || ticks < 1)
454 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
455 result = snd_timer_start_slave(timeri);
456 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
459 timer = timeri->timer;
462 spin_lock_irqsave(&timer->lock, flags);
463 timeri->ticks = timeri->cticks = ticks;
465 result = snd_timer_start1(timer, timeri, ticks);
466 spin_unlock_irqrestore(&timer->lock, flags);
467 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
471 static int _snd_timer_stop(struct snd_timer_instance * timeri,
472 int keep_flag, int event)
474 struct snd_timer *timer;
477 snd_assert(timeri != NULL, return -ENXIO);
479 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
481 spin_lock_irqsave(&slave_active_lock, flags);
482 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
483 spin_unlock_irqrestore(&slave_active_lock, flags);
487 timer = timeri->timer;
490 spin_lock_irqsave(&timer->lock, flags);
491 list_del_init(&timeri->ack_list);
492 list_del_init(&timeri->active_list);
493 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
494 !(--timer->running)) {
495 timer->hw.stop(timer);
496 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
497 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
498 snd_timer_reschedule(timer, 0);
499 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
500 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
501 timer->hw.start(timer);
507 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
508 spin_unlock_irqrestore(&timer->lock, flags);
510 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
511 snd_timer_notify1(timeri, event);
516 * stop the timer instance.
518 * do not call this from the timer callback!
520 int snd_timer_stop(struct snd_timer_instance *timeri)
522 struct snd_timer *timer;
526 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
529 timer = timeri->timer;
530 spin_lock_irqsave(&timer->lock, flags);
531 timeri->cticks = timeri->ticks;
533 spin_unlock_irqrestore(&timer->lock, flags);
538 * start again.. the tick is kept.
540 int snd_timer_continue(struct snd_timer_instance *timeri)
542 struct snd_timer *timer;
543 int result = -EINVAL;
548 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
549 return snd_timer_start_slave(timeri);
550 timer = timeri->timer;
553 spin_lock_irqsave(&timer->lock, flags);
557 result = snd_timer_start1(timer, timeri, timer->sticks);
558 spin_unlock_irqrestore(&timer->lock, flags);
559 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
564 * pause.. remember the ticks left
566 int snd_timer_pause(struct snd_timer_instance * timeri)
568 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
572 * reschedule the timer
574 * start pending instances and check the scheduling ticks.
575 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
577 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
579 struct snd_timer_instance *ti;
580 unsigned long ticks = ~0UL;
582 list_for_each_entry(ti, &timer->active_list_head, active_list) {
583 if (ti->flags & SNDRV_TIMER_IFLG_START) {
584 ti->flags &= ~SNDRV_TIMER_IFLG_START;
585 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
588 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
589 if (ticks > ti->cticks)
594 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
597 if (ticks > timer->hw.ticks)
598 ticks = timer->hw.ticks;
599 if (ticks_left != ticks)
600 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
601 timer->sticks = ticks;
608 static void snd_timer_tasklet(unsigned long arg)
610 struct snd_timer *timer = (struct snd_timer *) arg;
611 struct snd_timer_instance *ti;
613 unsigned long resolution, ticks;
616 spin_lock_irqsave(&timer->lock, flags);
617 /* now process all callbacks */
618 while (!list_empty(&timer->sack_list_head)) {
619 p = timer->sack_list_head.next; /* get first item */
620 ti = list_entry(p, struct snd_timer_instance, ack_list);
622 /* remove from ack_list and make empty */
627 resolution = ti->resolution;
629 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
630 spin_unlock(&timer->lock);
632 ti->callback(ti, resolution, ticks);
633 spin_lock(&timer->lock);
634 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
636 spin_unlock_irqrestore(&timer->lock, flags);
642 * ticks_left is usually equal to timer->sticks.
645 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
647 struct snd_timer_instance *ti, *ts, *tmp;
648 unsigned long resolution, ticks;
649 struct list_head *p, *ack_list_head;
656 spin_lock_irqsave(&timer->lock, flags);
658 /* remember the current resolution */
659 if (timer->hw.c_resolution)
660 resolution = timer->hw.c_resolution(timer);
662 resolution = timer->hw.resolution;
664 /* loop for all active instances
665 * Here we cannot use list_for_each_entry because the active_list of a
666 * processed instance is relinked to done_list_head before the callback
669 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
671 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
673 ti->pticks += ticks_left;
674 ti->resolution = resolution;
675 if (ti->cticks < ticks_left)
678 ti->cticks -= ticks_left;
679 if (ti->cticks) /* not expired */
681 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
682 ti->cticks = ti->ticks;
684 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
685 if (--timer->running)
686 list_del(&ti->active_list);
688 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
689 (ti->flags & SNDRV_TIMER_IFLG_FAST))
690 ack_list_head = &timer->ack_list_head;
692 ack_list_head = &timer->sack_list_head;
693 if (list_empty(&ti->ack_list))
694 list_add_tail(&ti->ack_list, ack_list_head);
695 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
696 ts->pticks = ti->pticks;
697 ts->resolution = resolution;
698 if (list_empty(&ts->ack_list))
699 list_add_tail(&ts->ack_list, ack_list_head);
702 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
703 snd_timer_reschedule(timer, timer->sticks);
704 if (timer->running) {
705 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
706 timer->hw.stop(timer);
707 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
709 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
710 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
712 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
713 timer->hw.start(timer);
716 timer->hw.stop(timer);
719 /* now process all fast callbacks */
720 while (!list_empty(&timer->ack_list_head)) {
721 p = timer->ack_list_head.next; /* get first item */
722 ti = list_entry(p, struct snd_timer_instance, ack_list);
724 /* remove from ack_list and make empty */
730 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
731 spin_unlock(&timer->lock);
733 ti->callback(ti, resolution, ticks);
734 spin_lock(&timer->lock);
735 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
738 /* do we have any slow callbacks? */
739 use_tasklet = !list_empty(&timer->sack_list_head);
740 spin_unlock_irqrestore(&timer->lock, flags);
743 tasklet_hi_schedule(&timer->task_queue);
750 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
751 struct snd_timer **rtimer)
753 struct snd_timer *timer;
755 static struct snd_device_ops ops = {
756 .dev_free = snd_timer_dev_free,
757 .dev_register = snd_timer_dev_register,
758 .dev_disconnect = snd_timer_dev_disconnect,
761 snd_assert(tid != NULL, return -EINVAL);
762 snd_assert(rtimer != NULL, return -EINVAL);
764 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
766 snd_printk(KERN_ERR "timer: cannot allocate\n");
769 timer->tmr_class = tid->dev_class;
771 timer->tmr_device = tid->device;
772 timer->tmr_subdevice = tid->subdevice;
774 strlcpy(timer->id, id, sizeof(timer->id));
775 INIT_LIST_HEAD(&timer->device_list);
776 INIT_LIST_HEAD(&timer->open_list_head);
777 INIT_LIST_HEAD(&timer->active_list_head);
778 INIT_LIST_HEAD(&timer->ack_list_head);
779 INIT_LIST_HEAD(&timer->sack_list_head);
780 spin_lock_init(&timer->lock);
781 tasklet_init(&timer->task_queue, snd_timer_tasklet,
782 (unsigned long)timer);
784 timer->module = card->module;
785 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
787 snd_timer_free(timer);
795 static int snd_timer_free(struct snd_timer *timer)
797 snd_assert(timer != NULL, return -ENXIO);
799 mutex_lock(®ister_mutex);
800 if (! list_empty(&timer->open_list_head)) {
801 struct list_head *p, *n;
802 struct snd_timer_instance *ti;
803 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
804 list_for_each_safe(p, n, &timer->open_list_head) {
806 ti = list_entry(p, struct snd_timer_instance, open_list);
810 list_del(&timer->device_list);
811 mutex_unlock(®ister_mutex);
813 if (timer->private_free)
814 timer->private_free(timer);
819 static int snd_timer_dev_free(struct snd_device *device)
821 struct snd_timer *timer = device->device_data;
822 return snd_timer_free(timer);
825 static int snd_timer_dev_register(struct snd_device *dev)
827 struct snd_timer *timer = dev->device_data;
828 struct snd_timer *timer1;
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 mutex_lock(®ister_mutex);
837 list_for_each_entry(timer1, &snd_timer_list, 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)
857 mutex_unlock(®ister_mutex);
860 list_add_tail(&timer->device_list, &timer1->device_list);
861 mutex_unlock(®ister_mutex);
865 static int snd_timer_dev_disconnect(struct snd_device *device)
867 struct snd_timer *timer = device->device_data;
868 mutex_lock(®ister_mutex);
869 list_del_init(&timer->device_list);
870 mutex_unlock(®ister_mutex);
874 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
877 unsigned long resolution = 0;
878 struct snd_timer_instance *ti, *ts;
880 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
882 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
883 event <= SNDRV_TIMER_EVENT_MRESUME, return);
884 spin_lock_irqsave(&timer->lock, flags);
885 if (event == SNDRV_TIMER_EVENT_MSTART ||
886 event == SNDRV_TIMER_EVENT_MCONTINUE ||
887 event == SNDRV_TIMER_EVENT_MRESUME) {
888 if (timer->hw.c_resolution)
889 resolution = timer->hw.c_resolution(timer);
891 resolution = timer->hw.resolution;
893 list_for_each_entry(ti, &timer->active_list_head, active_list) {
895 ti->ccallback(ti, event, tstamp, resolution);
896 list_for_each_entry(ts, &ti->slave_active_head, active_list)
898 ts->ccallback(ts, event, tstamp, resolution);
900 spin_unlock_irqrestore(&timer->lock, flags);
904 * exported functions for global timers
906 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
908 struct snd_timer_id tid;
910 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
911 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
915 return snd_timer_new(NULL, id, &tid, rtimer);
918 int snd_timer_global_free(struct snd_timer *timer)
920 return snd_timer_free(timer);
923 int snd_timer_global_register(struct snd_timer *timer)
925 struct snd_device dev;
927 memset(&dev, 0, sizeof(dev));
928 dev.device_data = timer;
929 return snd_timer_dev_register(&dev);
936 struct snd_timer_system_private {
937 struct timer_list tlist;
938 unsigned long last_expires;
939 unsigned long last_jiffies;
940 unsigned long correction;
943 static void snd_timer_s_function(unsigned long data)
945 struct snd_timer *timer = (struct snd_timer *)data;
946 struct snd_timer_system_private *priv = timer->private_data;
947 unsigned long jiff = jiffies;
948 if (time_after(jiff, priv->last_expires))
949 priv->correction += (long)jiff - (long)priv->last_expires;
950 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
953 static int snd_timer_s_start(struct snd_timer * timer)
955 struct snd_timer_system_private *priv;
958 priv = (struct snd_timer_system_private *) timer->private_data;
959 njiff = (priv->last_jiffies = jiffies);
960 if (priv->correction > timer->sticks - 1) {
961 priv->correction -= timer->sticks - 1;
964 njiff += timer->sticks - priv->correction;
965 priv->correction = 0;
967 priv->last_expires = priv->tlist.expires = njiff;
968 add_timer(&priv->tlist);
972 static int snd_timer_s_stop(struct snd_timer * timer)
974 struct snd_timer_system_private *priv;
977 priv = (struct snd_timer_system_private *) timer->private_data;
978 del_timer(&priv->tlist);
980 if (time_before(jiff, priv->last_expires))
981 timer->sticks = priv->last_expires - jiff;
984 priv->correction = 0;
988 static struct snd_timer_hardware snd_timer_system =
990 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
991 .resolution = 1000000000L / HZ,
993 .start = snd_timer_s_start,
994 .stop = snd_timer_s_stop
997 static void snd_timer_free_system(struct snd_timer *timer)
999 kfree(timer->private_data);
1002 static int snd_timer_register_system(void)
1004 struct snd_timer *timer;
1005 struct snd_timer_system_private *priv;
1008 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1011 strcpy(timer->name, "system timer");
1012 timer->hw = snd_timer_system;
1013 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1015 snd_timer_free(timer);
1018 init_timer(&priv->tlist);
1019 priv->tlist.function = snd_timer_s_function;
1020 priv->tlist.data = (unsigned long) timer;
1021 timer->private_data = priv;
1022 timer->private_free = snd_timer_free_system;
1023 return snd_timer_global_register(timer);
1026 #ifdef CONFIG_PROC_FS
1031 static void snd_timer_proc_read(struct snd_info_entry *entry,
1032 struct snd_info_buffer *buffer)
1034 struct snd_timer *timer;
1035 struct snd_timer_instance *ti;
1037 mutex_lock(®ister_mutex);
1038 list_for_each_entry(timer, &snd_timer_list, device_list) {
1039 switch (timer->tmr_class) {
1040 case SNDRV_TIMER_CLASS_GLOBAL:
1041 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1043 case SNDRV_TIMER_CLASS_CARD:
1044 snd_iprintf(buffer, "C%i-%i: ",
1045 timer->card->number, timer->tmr_device);
1047 case SNDRV_TIMER_CLASS_PCM:
1048 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1049 timer->tmr_device, timer->tmr_subdevice);
1052 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1053 timer->card ? timer->card->number : -1,
1054 timer->tmr_device, timer->tmr_subdevice);
1056 snd_iprintf(buffer, "%s :", timer->name);
1057 if (timer->hw.resolution)
1058 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1059 timer->hw.resolution / 1000,
1060 timer->hw.resolution % 1000,
1062 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1063 snd_iprintf(buffer, " SLAVE");
1064 snd_iprintf(buffer, "\n");
1065 list_for_each_entry(ti, &timer->open_list_head, open_list)
1066 snd_iprintf(buffer, " Client %s : %s\n",
1067 ti->owner ? ti->owner : "unknown",
1068 ti->flags & (SNDRV_TIMER_IFLG_START |
1069 SNDRV_TIMER_IFLG_RUNNING)
1070 ? "running" : "stopped");
1072 mutex_unlock(®ister_mutex);
1075 static struct snd_info_entry *snd_timer_proc_entry;
1077 static void __init snd_timer_proc_init(void)
1079 struct snd_info_entry *entry;
1081 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1082 if (entry != NULL) {
1083 entry->c.text.read = snd_timer_proc_read;
1084 if (snd_info_register(entry) < 0) {
1085 snd_info_free_entry(entry);
1089 snd_timer_proc_entry = entry;
1092 static void __exit snd_timer_proc_done(void)
1094 snd_info_free_entry(snd_timer_proc_entry);
1096 #else /* !CONFIG_PROC_FS */
1097 #define snd_timer_proc_init()
1098 #define snd_timer_proc_done()
1102 * USER SPACE interface
1105 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1106 unsigned long resolution,
1107 unsigned long ticks)
1109 struct snd_timer_user *tu = timeri->callback_data;
1110 struct snd_timer_read *r;
1113 spin_lock(&tu->qlock);
1114 if (tu->qused > 0) {
1115 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1116 r = &tu->queue[prev];
1117 if (r->resolution == resolution) {
1122 if (tu->qused >= tu->queue_size) {
1125 r = &tu->queue[tu->qtail++];
1126 tu->qtail %= tu->queue_size;
1127 r->resolution = resolution;
1132 spin_unlock(&tu->qlock);
1133 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1134 wake_up(&tu->qchange_sleep);
1137 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1138 struct snd_timer_tread *tread)
1140 if (tu->qused >= tu->queue_size) {
1143 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1144 tu->qtail %= tu->queue_size;
1149 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1151 struct timespec *tstamp,
1152 unsigned long resolution)
1154 struct snd_timer_user *tu = timeri->callback_data;
1155 struct snd_timer_tread r1;
1157 if (event >= SNDRV_TIMER_EVENT_START &&
1158 event <= SNDRV_TIMER_EVENT_PAUSE)
1159 tu->tstamp = *tstamp;
1160 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1163 r1.tstamp = *tstamp;
1164 r1.val = resolution;
1165 spin_lock(&tu->qlock);
1166 snd_timer_user_append_to_tqueue(tu, &r1);
1167 spin_unlock(&tu->qlock);
1168 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1169 wake_up(&tu->qchange_sleep);
1172 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1173 unsigned long resolution,
1174 unsigned long ticks)
1176 struct snd_timer_user *tu = timeri->callback_data;
1177 struct snd_timer_tread *r, r1;
1178 struct timespec tstamp;
1179 int prev, append = 0;
1181 memset(&tstamp, 0, sizeof(tstamp));
1182 spin_lock(&tu->qlock);
1183 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1184 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1185 spin_unlock(&tu->qlock);
1188 if (tu->last_resolution != resolution || ticks > 0) {
1189 if (timer_tstamp_monotonic)
1190 do_posix_clock_monotonic_gettime(&tstamp);
1192 getnstimeofday(&tstamp);
1194 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1195 tu->last_resolution != resolution) {
1196 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1198 r1.val = resolution;
1199 snd_timer_user_append_to_tqueue(tu, &r1);
1200 tu->last_resolution = resolution;
1203 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1207 if (tu->qused > 0) {
1208 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1209 r = &tu->tqueue[prev];
1210 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1217 r1.event = SNDRV_TIMER_EVENT_TICK;
1220 snd_timer_user_append_to_tqueue(tu, &r1);
1223 spin_unlock(&tu->qlock);
1226 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1227 wake_up(&tu->qchange_sleep);
1230 static int snd_timer_user_open(struct inode *inode, struct file *file)
1232 struct snd_timer_user *tu;
1234 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1237 spin_lock_init(&tu->qlock);
1238 init_waitqueue_head(&tu->qchange_sleep);
1239 mutex_init(&tu->tread_sem);
1241 tu->queue_size = 128;
1242 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1244 if (tu->queue == NULL) {
1248 file->private_data = tu;
1252 static int snd_timer_user_release(struct inode *inode, struct file *file)
1254 struct snd_timer_user *tu;
1256 if (file->private_data) {
1257 tu = file->private_data;
1258 file->private_data = NULL;
1259 fasync_helper(-1, file, 0, &tu->fasync);
1261 snd_timer_close(tu->timeri);
1269 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1271 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1272 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1278 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1280 id->dev_class = timer->tmr_class;
1281 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1282 id->card = timer->card ? timer->card->number : -1;
1283 id->device = timer->tmr_device;
1284 id->subdevice = timer->tmr_subdevice;
1287 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1289 struct snd_timer_id id;
1290 struct snd_timer *timer;
1291 struct list_head *p;
1293 if (copy_from_user(&id, _tid, sizeof(id)))
1295 mutex_lock(®ister_mutex);
1296 if (id.dev_class < 0) { /* first item */
1297 if (list_empty(&snd_timer_list))
1298 snd_timer_user_zero_id(&id);
1300 timer = list_entry(snd_timer_list.next,
1301 struct snd_timer, device_list);
1302 snd_timer_user_copy_id(&id, timer);
1305 switch (id.dev_class) {
1306 case SNDRV_TIMER_CLASS_GLOBAL:
1307 id.device = id.device < 0 ? 0 : id.device + 1;
1308 list_for_each(p, &snd_timer_list) {
1309 timer = list_entry(p, struct snd_timer, device_list);
1310 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1311 snd_timer_user_copy_id(&id, timer);
1314 if (timer->tmr_device >= id.device) {
1315 snd_timer_user_copy_id(&id, timer);
1319 if (p == &snd_timer_list)
1320 snd_timer_user_zero_id(&id);
1322 case SNDRV_TIMER_CLASS_CARD:
1323 case SNDRV_TIMER_CLASS_PCM:
1330 if (id.device < 0) {
1333 if (id.subdevice < 0) {
1341 list_for_each(p, &snd_timer_list) {
1342 timer = list_entry(p, struct snd_timer, device_list);
1343 if (timer->tmr_class > id.dev_class) {
1344 snd_timer_user_copy_id(&id, timer);
1347 if (timer->tmr_class < id.dev_class)
1349 if (timer->card->number > id.card) {
1350 snd_timer_user_copy_id(&id, timer);
1353 if (timer->card->number < id.card)
1355 if (timer->tmr_device > id.device) {
1356 snd_timer_user_copy_id(&id, timer);
1359 if (timer->tmr_device < id.device)
1361 if (timer->tmr_subdevice > id.subdevice) {
1362 snd_timer_user_copy_id(&id, timer);
1365 if (timer->tmr_subdevice < id.subdevice)
1367 snd_timer_user_copy_id(&id, timer);
1370 if (p == &snd_timer_list)
1371 snd_timer_user_zero_id(&id);
1374 snd_timer_user_zero_id(&id);
1377 mutex_unlock(®ister_mutex);
1378 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1383 static int snd_timer_user_ginfo(struct file *file,
1384 struct snd_timer_ginfo __user *_ginfo)
1386 struct snd_timer_ginfo *ginfo;
1387 struct snd_timer_id tid;
1388 struct snd_timer *t;
1389 struct list_head *p;
1392 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1395 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1400 memset(ginfo, 0, sizeof(*ginfo));
1402 mutex_lock(®ister_mutex);
1403 t = snd_timer_find(&tid);
1405 ginfo->card = t->card ? t->card->number : -1;
1406 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1407 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1408 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1409 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1410 ginfo->resolution = t->hw.resolution;
1411 if (t->hw.resolution_min > 0) {
1412 ginfo->resolution_min = t->hw.resolution_min;
1413 ginfo->resolution_max = t->hw.resolution_max;
1415 list_for_each(p, &t->open_list_head) {
1421 mutex_unlock(®ister_mutex);
1422 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1428 static int snd_timer_user_gparams(struct file *file,
1429 struct snd_timer_gparams __user *_gparams)
1431 struct snd_timer_gparams gparams;
1432 struct snd_timer *t;
1435 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1437 mutex_lock(®ister_mutex);
1438 t = snd_timer_find(&gparams.tid);
1443 if (!list_empty(&t->open_list_head)) {
1447 if (!t->hw.set_period) {
1451 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1453 mutex_unlock(®ister_mutex);
1457 static int snd_timer_user_gstatus(struct file *file,
1458 struct snd_timer_gstatus __user *_gstatus)
1460 struct snd_timer_gstatus gstatus;
1461 struct snd_timer_id tid;
1462 struct snd_timer *t;
1465 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1468 memset(&gstatus, 0, sizeof(gstatus));
1470 mutex_lock(®ister_mutex);
1471 t = snd_timer_find(&tid);
1473 if (t->hw.c_resolution)
1474 gstatus.resolution = t->hw.c_resolution(t);
1476 gstatus.resolution = t->hw.resolution;
1477 if (t->hw.precise_resolution) {
1478 t->hw.precise_resolution(t, &gstatus.resolution_num,
1479 &gstatus.resolution_den);
1481 gstatus.resolution_num = gstatus.resolution;
1482 gstatus.resolution_den = 1000000000uL;
1487 mutex_unlock(®ister_mutex);
1488 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1493 static int snd_timer_user_tselect(struct file *file,
1494 struct snd_timer_select __user *_tselect)
1496 struct snd_timer_user *tu;
1497 struct snd_timer_select tselect;
1501 tu = file->private_data;
1502 mutex_lock(&tu->tread_sem);
1504 snd_timer_close(tu->timeri);
1507 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1511 sprintf(str, "application %i", current->pid);
1512 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1513 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1514 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1523 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1525 if (tu->tqueue == NULL)
1528 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1530 if (tu->queue == NULL)
1535 snd_timer_close(tu->timeri);
1538 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1539 tu->timeri->callback = tu->tread
1540 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1541 tu->timeri->ccallback = snd_timer_user_ccallback;
1542 tu->timeri->callback_data = (void *)tu;
1546 mutex_unlock(&tu->tread_sem);
1550 static int snd_timer_user_info(struct file *file,
1551 struct snd_timer_info __user *_info)
1553 struct snd_timer_user *tu;
1554 struct snd_timer_info *info;
1555 struct snd_timer *t;
1558 tu = file->private_data;
1561 t = tu->timeri->timer;
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;
1593 t = tu->timeri->timer;
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 struct snd_timer_tread 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 struct snd_timer_read *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 struct snd_timer_status __user *_status)
1685 struct snd_timer_user *tu;
1686 struct snd_timer_status status;
1688 tu = file->private_data;
1691 memset(&status, 0, sizeof(status));
1692 status.tstamp = tu->tstamp;
1693 status.resolution = snd_timer_resolution(tu->timeri);
1694 status.lost = tu->timeri->lost;
1695 status.overrun = tu->overrun;
1696 spin_lock_irq(&tu->qlock);
1697 status.queue = tu->qused;
1698 spin_unlock_irq(&tu->qlock);
1699 if (copy_to_user(_status, &status, sizeof(status)))
1704 static int snd_timer_user_start(struct file *file)
1707 struct snd_timer_user *tu;
1709 tu = file->private_data;
1712 snd_timer_stop(tu->timeri);
1713 tu->timeri->lost = 0;
1714 tu->last_resolution = 0;
1715 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1718 static int snd_timer_user_stop(struct file *file)
1721 struct snd_timer_user *tu;
1723 tu = file->private_data;
1726 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1729 static int snd_timer_user_continue(struct file *file)
1732 struct snd_timer_user *tu;
1734 tu = file->private_data;
1737 tu->timeri->lost = 0;
1738 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1741 static int snd_timer_user_pause(struct file *file)
1744 struct snd_timer_user *tu;
1746 tu = file->private_data;
1749 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1753 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1754 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1755 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1756 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1759 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1762 struct snd_timer_user *tu;
1763 void __user *argp = (void __user *)arg;
1764 int __user *p = argp;
1766 tu = file->private_data;
1768 case SNDRV_TIMER_IOCTL_PVERSION:
1769 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1770 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1771 return snd_timer_user_next_device(argp);
1772 case SNDRV_TIMER_IOCTL_TREAD:
1776 mutex_lock(&tu->tread_sem);
1777 if (tu->timeri) { /* too late */
1778 mutex_unlock(&tu->tread_sem);
1781 if (get_user(xarg, p)) {
1782 mutex_unlock(&tu->tread_sem);
1785 tu->tread = xarg ? 1 : 0;
1786 mutex_unlock(&tu->tread_sem);
1789 case SNDRV_TIMER_IOCTL_GINFO:
1790 return snd_timer_user_ginfo(file, argp);
1791 case SNDRV_TIMER_IOCTL_GPARAMS:
1792 return snd_timer_user_gparams(file, argp);
1793 case SNDRV_TIMER_IOCTL_GSTATUS:
1794 return snd_timer_user_gstatus(file, argp);
1795 case SNDRV_TIMER_IOCTL_SELECT:
1796 return snd_timer_user_tselect(file, argp);
1797 case SNDRV_TIMER_IOCTL_INFO:
1798 return snd_timer_user_info(file, argp);
1799 case SNDRV_TIMER_IOCTL_PARAMS:
1800 return snd_timer_user_params(file, argp);
1801 case SNDRV_TIMER_IOCTL_STATUS:
1802 return snd_timer_user_status(file, argp);
1803 case SNDRV_TIMER_IOCTL_START:
1804 case SNDRV_TIMER_IOCTL_START_OLD:
1805 return snd_timer_user_start(file);
1806 case SNDRV_TIMER_IOCTL_STOP:
1807 case SNDRV_TIMER_IOCTL_STOP_OLD:
1808 return snd_timer_user_stop(file);
1809 case SNDRV_TIMER_IOCTL_CONTINUE:
1810 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1811 return snd_timer_user_continue(file);
1812 case SNDRV_TIMER_IOCTL_PAUSE:
1813 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1814 return snd_timer_user_pause(file);
1819 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1821 struct snd_timer_user *tu;
1824 tu = file->private_data;
1825 err = fasync_helper(fd, file, on, &tu->fasync);
1831 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1832 size_t count, loff_t *offset)
1834 struct snd_timer_user *tu;
1835 long result = 0, unit;
1838 tu = file->private_data;
1839 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1840 spin_lock_irq(&tu->qlock);
1841 while ((long)count - result >= unit) {
1842 while (!tu->qused) {
1845 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1850 set_current_state(TASK_INTERRUPTIBLE);
1851 init_waitqueue_entry(&wait, current);
1852 add_wait_queue(&tu->qchange_sleep, &wait);
1854 spin_unlock_irq(&tu->qlock);
1856 spin_lock_irq(&tu->qlock);
1858 remove_wait_queue(&tu->qchange_sleep, &wait);
1860 if (signal_pending(current)) {
1866 spin_unlock_irq(&tu->qlock);
1871 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1872 sizeof(struct snd_timer_tread))) {
1877 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1878 sizeof(struct snd_timer_read))) {
1884 tu->qhead %= tu->queue_size;
1889 spin_lock_irq(&tu->qlock);
1892 spin_unlock_irq(&tu->qlock);
1894 return result > 0 ? result : err;
1897 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1900 struct snd_timer_user *tu;
1902 tu = file->private_data;
1904 poll_wait(file, &tu->qchange_sleep, wait);
1908 mask |= POLLIN | POLLRDNORM;
1913 #ifdef CONFIG_COMPAT
1914 #include "timer_compat.c"
1916 #define snd_timer_user_ioctl_compat NULL
1919 static const struct file_operations snd_timer_f_ops =
1921 .owner = THIS_MODULE,
1922 .read = snd_timer_user_read,
1923 .open = snd_timer_user_open,
1924 .release = snd_timer_user_release,
1925 .poll = snd_timer_user_poll,
1926 .unlocked_ioctl = snd_timer_user_ioctl,
1927 .compat_ioctl = snd_timer_user_ioctl_compat,
1928 .fasync = snd_timer_user_fasync,
1935 static int __init alsa_timer_init(void)
1939 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1940 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1944 if ((err = snd_timer_register_system()) < 0)
1945 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1947 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1948 &snd_timer_f_ops, NULL, "timer")) < 0)
1949 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1951 snd_timer_proc_init();
1955 static void __exit alsa_timer_exit(void)
1957 struct list_head *p, *n;
1959 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1960 /* unregister the system timer */
1961 list_for_each_safe(p, n, &snd_timer_list) {
1962 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
1963 snd_timer_free(timer);
1965 snd_timer_proc_done();
1966 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1967 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1971 module_init(alsa_timer_init)
1972 module_exit(alsa_timer_exit)
1974 EXPORT_SYMBOL(snd_timer_open);
1975 EXPORT_SYMBOL(snd_timer_close);
1976 EXPORT_SYMBOL(snd_timer_resolution);
1977 EXPORT_SYMBOL(snd_timer_start);
1978 EXPORT_SYMBOL(snd_timer_stop);
1979 EXPORT_SYMBOL(snd_timer_continue);
1980 EXPORT_SYMBOL(snd_timer_pause);
1981 EXPORT_SYMBOL(snd_timer_new);
1982 EXPORT_SYMBOL(snd_timer_notify);
1983 EXPORT_SYMBOL(snd_timer_global_new);
1984 EXPORT_SYMBOL(snd_timer_global_free);
1985 EXPORT_SYMBOL(snd_timer_global_register);
1986 EXPORT_SYMBOL(snd_timer_interrupt);