1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Timers abstract layer
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <linux/delay.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/time.h>
11 #include <linux/mutex.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/sched/signal.h>
16 #include <sound/core.h>
17 #include <sound/timer.h>
18 #include <sound/control.h>
19 #include <sound/info.h>
20 #include <sound/minors.h>
21 #include <sound/initval.h>
22 #include <linux/kmod.h>
25 #define SNDRV_TIMER_IFLG_PAUSED 0x00010000
26 #define SNDRV_TIMER_IFLG_DEAD 0x00020000
28 #if IS_ENABLED(CONFIG_SND_HRTIMER)
29 #define DEFAULT_TIMER_LIMIT 4
31 #define DEFAULT_TIMER_LIMIT 1
34 static int timer_limit = DEFAULT_TIMER_LIMIT;
35 static int timer_tstamp_monotonic = 1;
36 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
37 MODULE_DESCRIPTION("ALSA timer interface");
38 MODULE_LICENSE("GPL");
39 module_param(timer_limit, int, 0444);
40 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
41 module_param(timer_tstamp_monotonic, int, 0444);
42 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
44 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
45 MODULE_ALIAS("devname:snd/timer");
47 struct snd_timer_user {
48 struct snd_timer_instance *timeri;
49 int tread; /* enhanced read with timestamps and events */
51 unsigned long overrun;
57 struct snd_timer_read *queue;
58 struct snd_timer_tread *tqueue;
60 unsigned long last_resolution;
62 struct timespec tstamp; /* trigger tstamp */
63 wait_queue_head_t qchange_sleep;
64 struct fasync_struct *fasync;
65 struct mutex ioctl_lock;
69 static LIST_HEAD(snd_timer_list);
71 /* list of slave instances */
72 static LIST_HEAD(snd_timer_slave_list);
74 /* lock for slave active lists */
75 static DEFINE_SPINLOCK(slave_active_lock);
77 #define MAX_SLAVE_INSTANCES 1000
78 static int num_slaves;
80 static DEFINE_MUTEX(register_mutex);
82 static int snd_timer_free(struct snd_timer *timer);
83 static int snd_timer_dev_free(struct snd_device *device);
84 static int snd_timer_dev_register(struct snd_device *device);
85 static int snd_timer_dev_disconnect(struct snd_device *device);
87 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
90 * create a timer instance with the given owner string.
91 * when timer is not NULL, increments the module counter
93 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
94 struct snd_timer *timer)
96 struct snd_timer_instance *timeri;
97 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
100 timeri->owner = kstrdup(owner, GFP_KERNEL);
101 if (! timeri->owner) {
105 INIT_LIST_HEAD(&timeri->open_list);
106 INIT_LIST_HEAD(&timeri->active_list);
107 INIT_LIST_HEAD(&timeri->ack_list);
108 INIT_LIST_HEAD(&timeri->slave_list_head);
109 INIT_LIST_HEAD(&timeri->slave_active_head);
111 timeri->timer = timer;
112 if (timer && !try_module_get(timer->module)) {
113 kfree(timeri->owner);
122 * find a timer instance from the given timer id
124 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
126 struct snd_timer *timer = NULL;
128 list_for_each_entry(timer, &snd_timer_list, device_list) {
129 if (timer->tmr_class != tid->dev_class)
131 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
132 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
133 (timer->card == NULL ||
134 timer->card->number != tid->card))
136 if (timer->tmr_device != tid->device)
138 if (timer->tmr_subdevice != tid->subdevice)
145 #ifdef CONFIG_MODULES
147 static void snd_timer_request(struct snd_timer_id *tid)
149 switch (tid->dev_class) {
150 case SNDRV_TIMER_CLASS_GLOBAL:
151 if (tid->device < timer_limit)
152 request_module("snd-timer-%i", tid->device);
154 case SNDRV_TIMER_CLASS_CARD:
155 case SNDRV_TIMER_CLASS_PCM:
156 if (tid->card < snd_ecards_limit)
157 request_module("snd-card-%i", tid->card);
167 * look for a master instance matching with the slave id of the given slave.
168 * when found, relink the open_link of the slave.
170 * call this with register_mutex down.
172 static int snd_timer_check_slave(struct snd_timer_instance *slave)
174 struct snd_timer *timer;
175 struct snd_timer_instance *master;
177 /* FIXME: it's really dumb to look up all entries.. */
178 list_for_each_entry(timer, &snd_timer_list, device_list) {
179 list_for_each_entry(master, &timer->open_list_head, open_list) {
180 if (slave->slave_class == master->slave_class &&
181 slave->slave_id == master->slave_id) {
182 if (master->timer->num_instances >=
183 master->timer->max_instances)
185 list_move_tail(&slave->open_list,
186 &master->slave_list_head);
187 master->timer->num_instances++;
188 spin_lock_irq(&slave_active_lock);
189 slave->master = master;
190 slave->timer = master->timer;
191 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 int 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 if (master->timer->num_instances >=
214 master->timer->max_instances)
216 list_move_tail(&slave->open_list, &master->slave_list_head);
217 master->timer->num_instances++;
218 spin_lock_irq(&slave_active_lock);
219 spin_lock(&master->timer->lock);
220 slave->master = master;
221 slave->timer = master->timer;
222 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
223 list_add_tail(&slave->active_list,
224 &master->slave_active_head);
225 spin_unlock(&master->timer->lock);
226 spin_unlock_irq(&slave_active_lock);
232 static int snd_timer_close_locked(struct snd_timer_instance *timeri,
233 struct device **card_devp_to_put);
236 * open a timer instance
237 * when opening a master, the slave id must be here given.
239 int snd_timer_open(struct snd_timer_instance **ti,
240 char *owner, struct snd_timer_id *tid,
241 unsigned int slave_id)
243 struct snd_timer *timer;
244 struct snd_timer_instance *timeri = NULL;
245 struct device *card_dev_to_put = NULL;
248 mutex_lock(®ister_mutex);
249 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
250 /* open a slave instance */
251 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
252 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
253 pr_debug("ALSA: timer: invalid slave class %i\n",
258 if (num_slaves >= MAX_SLAVE_INSTANCES) {
262 timeri = snd_timer_instance_new(owner, NULL);
267 timeri->slave_class = tid->dev_sclass;
268 timeri->slave_id = tid->device;
269 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
270 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
272 err = snd_timer_check_slave(timeri);
274 snd_timer_close_locked(timeri, &card_dev_to_put);
280 /* open a master instance */
281 timer = snd_timer_find(tid);
282 #ifdef CONFIG_MODULES
284 mutex_unlock(®ister_mutex);
285 snd_timer_request(tid);
286 mutex_lock(®ister_mutex);
287 timer = snd_timer_find(tid);
294 if (!list_empty(&timer->open_list_head)) {
295 struct snd_timer_instance *t =
296 list_entry(timer->open_list_head.next,
297 struct snd_timer_instance, open_list);
298 if (t->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
303 if (timer->num_instances >= timer->max_instances) {
307 timeri = snd_timer_instance_new(owner, timer);
312 /* take a card refcount for safe disconnection */
314 get_device(&timer->card->card_dev);
315 timeri->slave_class = tid->dev_sclass;
316 timeri->slave_id = slave_id;
318 if (list_empty(&timer->open_list_head) && timer->hw.open) {
319 err = timer->hw.open(timer);
321 kfree(timeri->owner);
326 card_dev_to_put = &timer->card->card_dev;
327 module_put(timer->module);
332 list_add_tail(&timeri->open_list, &timer->open_list_head);
333 timer->num_instances++;
334 err = snd_timer_check_master(timeri);
336 snd_timer_close_locked(timeri, &card_dev_to_put);
341 mutex_unlock(®ister_mutex);
342 /* put_device() is called after unlock for avoiding deadlock */
344 put_device(card_dev_to_put);
348 EXPORT_SYMBOL(snd_timer_open);
351 * close a timer instance
352 * call this with register_mutex down.
354 static int snd_timer_close_locked(struct snd_timer_instance *timeri,
355 struct device **card_devp_to_put)
357 struct snd_timer *timer = timeri->timer;
358 struct snd_timer_instance *slave, *tmp;
361 spin_lock_irq(&timer->lock);
362 timeri->flags |= SNDRV_TIMER_IFLG_DEAD;
363 spin_unlock_irq(&timer->lock);
366 list_del(&timeri->open_list);
367 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
370 /* force to stop the timer */
371 snd_timer_stop(timeri);
374 timer->num_instances--;
375 /* wait, until the active callback is finished */
376 spin_lock_irq(&timer->lock);
377 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
378 spin_unlock_irq(&timer->lock);
380 spin_lock_irq(&timer->lock);
382 spin_unlock_irq(&timer->lock);
384 /* remove slave links */
385 spin_lock_irq(&slave_active_lock);
386 spin_lock(&timer->lock);
387 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
389 list_move_tail(&slave->open_list, &snd_timer_slave_list);
390 timer->num_instances--;
391 slave->master = NULL;
393 list_del_init(&slave->ack_list);
394 list_del_init(&slave->active_list);
396 spin_unlock(&timer->lock);
397 spin_unlock_irq(&slave_active_lock);
399 /* slave doesn't need to release timer resources below */
400 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
404 if (timeri->private_free)
405 timeri->private_free(timeri);
406 kfree(timeri->owner);
410 if (list_empty(&timer->open_list_head) && timer->hw.close)
411 timer->hw.close(timer);
412 /* release a card refcount for safe disconnection */
414 *card_devp_to_put = &timer->card->card_dev;
415 module_put(timer->module);
422 * close a timer instance
424 int snd_timer_close(struct snd_timer_instance *timeri)
426 struct device *card_dev_to_put = NULL;
429 if (snd_BUG_ON(!timeri))
432 mutex_lock(®ister_mutex);
433 err = snd_timer_close_locked(timeri, &card_dev_to_put);
434 mutex_unlock(®ister_mutex);
435 /* put_device() is called after unlock for avoiding deadlock */
437 put_device(card_dev_to_put);
440 EXPORT_SYMBOL(snd_timer_close);
442 static unsigned long snd_timer_hw_resolution(struct snd_timer *timer)
444 if (timer->hw.c_resolution)
445 return timer->hw.c_resolution(timer);
447 return timer->hw.resolution;
450 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
452 struct snd_timer * timer;
453 unsigned long ret = 0;
458 timer = timeri->timer;
460 spin_lock_irqsave(&timer->lock, flags);
461 ret = snd_timer_hw_resolution(timer);
462 spin_unlock_irqrestore(&timer->lock, flags);
466 EXPORT_SYMBOL(snd_timer_resolution);
468 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
470 struct snd_timer *timer = ti->timer;
471 unsigned long resolution = 0;
472 struct snd_timer_instance *ts;
473 struct timespec tstamp;
475 if (timer_tstamp_monotonic)
476 ktime_get_ts(&tstamp);
478 getnstimeofday(&tstamp);
479 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
480 event > SNDRV_TIMER_EVENT_PAUSE))
483 (event == SNDRV_TIMER_EVENT_START ||
484 event == SNDRV_TIMER_EVENT_CONTINUE))
485 resolution = snd_timer_hw_resolution(timer);
487 ti->ccallback(ti, event, &tstamp, resolution);
488 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
492 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
494 list_for_each_entry(ts, &ti->slave_active_head, active_list)
496 ts->ccallback(ts, event + 100, &tstamp, resolution);
499 /* start/continue a master timer */
500 static int snd_timer_start1(struct snd_timer_instance *timeri,
501 bool start, unsigned long ticks)
503 struct snd_timer *timer;
507 timer = timeri->timer;
511 spin_lock_irqsave(&timer->lock, flags);
512 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
516 if (timer->card && timer->card->shutdown) {
520 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
521 SNDRV_TIMER_IFLG_START)) {
527 timeri->ticks = timeri->cticks = ticks;
528 else if (!timeri->cticks)
532 list_move_tail(&timeri->active_list, &timer->active_list_head);
533 if (timer->running) {
534 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
536 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
537 timeri->flags |= SNDRV_TIMER_IFLG_START;
538 result = 1; /* delayed start */
541 timer->sticks = ticks;
542 timer->hw.start(timer);
545 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
548 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
549 SNDRV_TIMER_EVENT_CONTINUE);
551 spin_unlock_irqrestore(&timer->lock, flags);
555 /* start/continue a slave timer */
556 static int snd_timer_start_slave(struct snd_timer_instance *timeri,
562 spin_lock_irqsave(&slave_active_lock, flags);
563 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
567 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
571 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
572 if (timeri->master && timeri->timer) {
573 spin_lock(&timeri->timer->lock);
574 list_add_tail(&timeri->active_list,
575 &timeri->master->slave_active_head);
576 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
577 SNDRV_TIMER_EVENT_CONTINUE);
578 spin_unlock(&timeri->timer->lock);
580 err = 1; /* delayed start */
582 spin_unlock_irqrestore(&slave_active_lock, flags);
586 /* stop/pause a master timer */
587 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
589 struct snd_timer *timer;
593 timer = timeri->timer;
596 spin_lock_irqsave(&timer->lock, flags);
597 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
598 SNDRV_TIMER_IFLG_START))) {
602 list_del_init(&timeri->ack_list);
603 list_del_init(&timeri->active_list);
604 if (timer->card && timer->card->shutdown)
607 timeri->cticks = timeri->ticks;
610 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
611 !(--timer->running)) {
612 timer->hw.stop(timer);
613 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
614 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
615 snd_timer_reschedule(timer, 0);
616 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
617 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
618 timer->hw.start(timer);
622 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
624 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
626 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
627 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
628 SNDRV_TIMER_EVENT_PAUSE);
630 spin_unlock_irqrestore(&timer->lock, flags);
634 /* stop/pause a slave timer */
635 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
639 spin_lock_irqsave(&slave_active_lock, flags);
640 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
641 spin_unlock_irqrestore(&slave_active_lock, flags);
644 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
646 spin_lock(&timeri->timer->lock);
647 list_del_init(&timeri->ack_list);
648 list_del_init(&timeri->active_list);
649 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
650 SNDRV_TIMER_EVENT_PAUSE);
651 spin_unlock(&timeri->timer->lock);
653 spin_unlock_irqrestore(&slave_active_lock, flags);
658 * start the timer instance
660 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
662 if (timeri == NULL || ticks < 1)
664 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
665 return snd_timer_start_slave(timeri, true);
667 return snd_timer_start1(timeri, true, ticks);
669 EXPORT_SYMBOL(snd_timer_start);
672 * stop the timer instance.
674 * do not call this from the timer callback!
676 int snd_timer_stop(struct snd_timer_instance *timeri)
678 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
679 return snd_timer_stop_slave(timeri, true);
681 return snd_timer_stop1(timeri, true);
683 EXPORT_SYMBOL(snd_timer_stop);
686 * start again.. the tick is kept.
688 int snd_timer_continue(struct snd_timer_instance *timeri)
690 /* timer can continue only after pause */
691 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
694 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
695 return snd_timer_start_slave(timeri, false);
697 return snd_timer_start1(timeri, false, 0);
699 EXPORT_SYMBOL(snd_timer_continue);
702 * pause.. remember the ticks left
704 int snd_timer_pause(struct snd_timer_instance * timeri)
706 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
707 return snd_timer_stop_slave(timeri, false);
709 return snd_timer_stop1(timeri, false);
711 EXPORT_SYMBOL(snd_timer_pause);
714 * reschedule the timer
716 * start pending instances and check the scheduling ticks.
717 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
719 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
721 struct snd_timer_instance *ti;
722 unsigned long ticks = ~0UL;
724 list_for_each_entry(ti, &timer->active_list_head, active_list) {
725 if (ti->flags & SNDRV_TIMER_IFLG_START) {
726 ti->flags &= ~SNDRV_TIMER_IFLG_START;
727 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
730 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
731 if (ticks > ti->cticks)
736 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
739 if (ticks > timer->hw.ticks)
740 ticks = timer->hw.ticks;
741 if (ticks_left != ticks)
742 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
743 timer->sticks = ticks;
746 /* call callbacks in timer ack list */
747 static void snd_timer_process_callbacks(struct snd_timer *timer,
748 struct list_head *head)
750 struct snd_timer_instance *ti;
751 unsigned long resolution, ticks;
753 while (!list_empty(head)) {
754 ti = list_first_entry(head, struct snd_timer_instance,
757 /* remove from ack_list and make empty */
758 list_del_init(&ti->ack_list);
760 if (!(ti->flags & SNDRV_TIMER_IFLG_DEAD)) {
763 resolution = ti->resolution;
764 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
765 spin_unlock(&timer->lock);
767 ti->callback(ti, resolution, ticks);
768 spin_lock(&timer->lock);
769 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
774 /* clear pending instances from ack list */
775 static void snd_timer_clear_callbacks(struct snd_timer *timer,
776 struct list_head *head)
780 spin_lock_irqsave(&timer->lock, flags);
781 while (!list_empty(head))
782 list_del_init(head->next);
783 spin_unlock_irqrestore(&timer->lock, flags);
790 static void snd_timer_tasklet(unsigned long arg)
792 struct snd_timer *timer = (struct snd_timer *) arg;
795 if (timer->card && timer->card->shutdown) {
796 snd_timer_clear_callbacks(timer, &timer->sack_list_head);
800 spin_lock_irqsave(&timer->lock, flags);
801 snd_timer_process_callbacks(timer, &timer->sack_list_head);
802 spin_unlock_irqrestore(&timer->lock, flags);
808 * ticks_left is usually equal to timer->sticks.
811 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
813 struct snd_timer_instance *ti, *ts, *tmp;
814 unsigned long resolution;
815 struct list_head *ack_list_head;
822 if (timer->card && timer->card->shutdown) {
823 snd_timer_clear_callbacks(timer, &timer->ack_list_head);
827 spin_lock_irqsave(&timer->lock, flags);
829 /* remember the current resolution */
830 resolution = snd_timer_hw_resolution(timer);
832 /* loop for all active instances
833 * Here we cannot use list_for_each_entry because the active_list of a
834 * processed instance is relinked to done_list_head before the callback
837 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
839 if (ti->flags & SNDRV_TIMER_IFLG_DEAD)
841 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
843 ti->pticks += ticks_left;
844 ti->resolution = resolution;
845 if (ti->cticks < ticks_left)
848 ti->cticks -= ticks_left;
849 if (ti->cticks) /* not expired */
851 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
852 ti->cticks = ti->ticks;
854 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
856 list_del_init(&ti->active_list);
858 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
859 (ti->flags & SNDRV_TIMER_IFLG_FAST))
860 ack_list_head = &timer->ack_list_head;
862 ack_list_head = &timer->sack_list_head;
863 if (list_empty(&ti->ack_list))
864 list_add_tail(&ti->ack_list, ack_list_head);
865 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
866 ts->pticks = ti->pticks;
867 ts->resolution = resolution;
868 if (list_empty(&ts->ack_list))
869 list_add_tail(&ts->ack_list, ack_list_head);
872 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
873 snd_timer_reschedule(timer, timer->sticks);
874 if (timer->running) {
875 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
876 timer->hw.stop(timer);
877 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
879 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
880 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
882 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
883 timer->hw.start(timer);
886 timer->hw.stop(timer);
889 /* now process all fast callbacks */
890 snd_timer_process_callbacks(timer, &timer->ack_list_head);
892 /* do we have any slow callbacks? */
893 use_tasklet = !list_empty(&timer->sack_list_head);
894 spin_unlock_irqrestore(&timer->lock, flags);
897 tasklet_schedule(&timer->task_queue);
899 EXPORT_SYMBOL(snd_timer_interrupt);
905 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
906 struct snd_timer **rtimer)
908 struct snd_timer *timer;
910 static struct snd_device_ops ops = {
911 .dev_free = snd_timer_dev_free,
912 .dev_register = snd_timer_dev_register,
913 .dev_disconnect = snd_timer_dev_disconnect,
916 if (snd_BUG_ON(!tid))
918 if (tid->dev_class == SNDRV_TIMER_CLASS_CARD ||
919 tid->dev_class == SNDRV_TIMER_CLASS_PCM) {
925 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
928 timer->tmr_class = tid->dev_class;
930 timer->tmr_device = tid->device;
931 timer->tmr_subdevice = tid->subdevice;
933 strlcpy(timer->id, id, sizeof(timer->id));
935 INIT_LIST_HEAD(&timer->device_list);
936 INIT_LIST_HEAD(&timer->open_list_head);
937 INIT_LIST_HEAD(&timer->active_list_head);
938 INIT_LIST_HEAD(&timer->ack_list_head);
939 INIT_LIST_HEAD(&timer->sack_list_head);
940 spin_lock_init(&timer->lock);
941 tasklet_init(&timer->task_queue, snd_timer_tasklet,
942 (unsigned long)timer);
943 timer->max_instances = 1000; /* default limit per timer */
945 timer->module = card->module;
946 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
948 snd_timer_free(timer);
956 EXPORT_SYMBOL(snd_timer_new);
958 static int snd_timer_free(struct snd_timer *timer)
963 mutex_lock(®ister_mutex);
964 if (! list_empty(&timer->open_list_head)) {
965 struct list_head *p, *n;
966 struct snd_timer_instance *ti;
967 pr_warn("ALSA: timer %p is busy?\n", timer);
968 list_for_each_safe(p, n, &timer->open_list_head) {
970 ti = list_entry(p, struct snd_timer_instance, open_list);
974 list_del(&timer->device_list);
975 mutex_unlock(®ister_mutex);
977 if (timer->private_free)
978 timer->private_free(timer);
983 static int snd_timer_dev_free(struct snd_device *device)
985 struct snd_timer *timer = device->device_data;
986 return snd_timer_free(timer);
989 static int snd_timer_dev_register(struct snd_device *dev)
991 struct snd_timer *timer = dev->device_data;
992 struct snd_timer *timer1;
994 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
996 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
997 !timer->hw.resolution && timer->hw.c_resolution == NULL)
1000 mutex_lock(®ister_mutex);
1001 list_for_each_entry(timer1, &snd_timer_list, device_list) {
1002 if (timer1->tmr_class > timer->tmr_class)
1004 if (timer1->tmr_class < timer->tmr_class)
1006 if (timer1->card && timer->card) {
1007 if (timer1->card->number > timer->card->number)
1009 if (timer1->card->number < timer->card->number)
1012 if (timer1->tmr_device > timer->tmr_device)
1014 if (timer1->tmr_device < timer->tmr_device)
1016 if (timer1->tmr_subdevice > timer->tmr_subdevice)
1018 if (timer1->tmr_subdevice < timer->tmr_subdevice)
1021 mutex_unlock(®ister_mutex);
1024 list_add_tail(&timer->device_list, &timer1->device_list);
1025 mutex_unlock(®ister_mutex);
1029 static int snd_timer_dev_disconnect(struct snd_device *device)
1031 struct snd_timer *timer = device->device_data;
1032 struct snd_timer_instance *ti;
1034 mutex_lock(®ister_mutex);
1035 list_del_init(&timer->device_list);
1036 /* wake up pending sleepers */
1037 list_for_each_entry(ti, &timer->open_list_head, open_list) {
1041 mutex_unlock(®ister_mutex);
1045 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
1047 unsigned long flags;
1048 unsigned long resolution = 0;
1049 struct snd_timer_instance *ti, *ts;
1051 if (timer->card && timer->card->shutdown)
1053 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1055 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1056 event > SNDRV_TIMER_EVENT_MRESUME))
1058 spin_lock_irqsave(&timer->lock, flags);
1059 if (event == SNDRV_TIMER_EVENT_MSTART ||
1060 event == SNDRV_TIMER_EVENT_MCONTINUE ||
1061 event == SNDRV_TIMER_EVENT_MRESUME)
1062 resolution = snd_timer_hw_resolution(timer);
1063 list_for_each_entry(ti, &timer->active_list_head, active_list) {
1065 ti->ccallback(ti, event, tstamp, resolution);
1066 list_for_each_entry(ts, &ti->slave_active_head, active_list)
1068 ts->ccallback(ts, event, tstamp, resolution);
1070 spin_unlock_irqrestore(&timer->lock, flags);
1072 EXPORT_SYMBOL(snd_timer_notify);
1075 * exported functions for global timers
1077 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1079 struct snd_timer_id tid;
1081 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1082 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1084 tid.device = device;
1086 return snd_timer_new(NULL, id, &tid, rtimer);
1088 EXPORT_SYMBOL(snd_timer_global_new);
1090 int snd_timer_global_free(struct snd_timer *timer)
1092 return snd_timer_free(timer);
1094 EXPORT_SYMBOL(snd_timer_global_free);
1096 int snd_timer_global_register(struct snd_timer *timer)
1098 struct snd_device dev;
1100 memset(&dev, 0, sizeof(dev));
1101 dev.device_data = timer;
1102 return snd_timer_dev_register(&dev);
1104 EXPORT_SYMBOL(snd_timer_global_register);
1110 struct snd_timer_system_private {
1111 struct timer_list tlist;
1112 struct snd_timer *snd_timer;
1113 unsigned long last_expires;
1114 unsigned long last_jiffies;
1115 unsigned long correction;
1118 static void snd_timer_s_function(struct timer_list *t)
1120 struct snd_timer_system_private *priv = from_timer(priv, t,
1122 struct snd_timer *timer = priv->snd_timer;
1123 unsigned long jiff = jiffies;
1124 if (time_after(jiff, priv->last_expires))
1125 priv->correction += (long)jiff - (long)priv->last_expires;
1126 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1129 static int snd_timer_s_start(struct snd_timer * timer)
1131 struct snd_timer_system_private *priv;
1132 unsigned long njiff;
1134 priv = (struct snd_timer_system_private *) timer->private_data;
1135 njiff = (priv->last_jiffies = jiffies);
1136 if (priv->correction > timer->sticks - 1) {
1137 priv->correction -= timer->sticks - 1;
1140 njiff += timer->sticks - priv->correction;
1141 priv->correction = 0;
1143 priv->last_expires = njiff;
1144 mod_timer(&priv->tlist, njiff);
1148 static int snd_timer_s_stop(struct snd_timer * timer)
1150 struct snd_timer_system_private *priv;
1153 priv = (struct snd_timer_system_private *) timer->private_data;
1154 del_timer(&priv->tlist);
1156 if (time_before(jiff, priv->last_expires))
1157 timer->sticks = priv->last_expires - jiff;
1160 priv->correction = 0;
1164 static int snd_timer_s_close(struct snd_timer *timer)
1166 struct snd_timer_system_private *priv;
1168 priv = (struct snd_timer_system_private *)timer->private_data;
1169 del_timer_sync(&priv->tlist);
1173 static struct snd_timer_hardware snd_timer_system =
1175 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1176 .resolution = 1000000000L / HZ,
1178 .close = snd_timer_s_close,
1179 .start = snd_timer_s_start,
1180 .stop = snd_timer_s_stop
1183 static void snd_timer_free_system(struct snd_timer *timer)
1185 kfree(timer->private_data);
1188 static int snd_timer_register_system(void)
1190 struct snd_timer *timer;
1191 struct snd_timer_system_private *priv;
1194 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1197 strcpy(timer->name, "system timer");
1198 timer->hw = snd_timer_system;
1199 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1201 snd_timer_free(timer);
1204 priv->snd_timer = timer;
1205 timer_setup(&priv->tlist, snd_timer_s_function, 0);
1206 timer->private_data = priv;
1207 timer->private_free = snd_timer_free_system;
1208 return snd_timer_global_register(timer);
1211 #ifdef CONFIG_SND_PROC_FS
1216 static void snd_timer_proc_read(struct snd_info_entry *entry,
1217 struct snd_info_buffer *buffer)
1219 struct snd_timer *timer;
1220 struct snd_timer_instance *ti;
1222 mutex_lock(®ister_mutex);
1223 list_for_each_entry(timer, &snd_timer_list, device_list) {
1224 if (timer->card && timer->card->shutdown)
1226 switch (timer->tmr_class) {
1227 case SNDRV_TIMER_CLASS_GLOBAL:
1228 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1230 case SNDRV_TIMER_CLASS_CARD:
1231 snd_iprintf(buffer, "C%i-%i: ",
1232 timer->card->number, timer->tmr_device);
1234 case SNDRV_TIMER_CLASS_PCM:
1235 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1236 timer->tmr_device, timer->tmr_subdevice);
1239 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1240 timer->card ? timer->card->number : -1,
1241 timer->tmr_device, timer->tmr_subdevice);
1243 snd_iprintf(buffer, "%s :", timer->name);
1244 if (timer->hw.resolution)
1245 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1246 timer->hw.resolution / 1000,
1247 timer->hw.resolution % 1000,
1249 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1250 snd_iprintf(buffer, " SLAVE");
1251 snd_iprintf(buffer, "\n");
1252 list_for_each_entry(ti, &timer->open_list_head, open_list)
1253 snd_iprintf(buffer, " Client %s : %s\n",
1254 ti->owner ? ti->owner : "unknown",
1255 ti->flags & (SNDRV_TIMER_IFLG_START |
1256 SNDRV_TIMER_IFLG_RUNNING)
1257 ? "running" : "stopped");
1259 mutex_unlock(®ister_mutex);
1262 static struct snd_info_entry *snd_timer_proc_entry;
1264 static void __init snd_timer_proc_init(void)
1266 struct snd_info_entry *entry;
1268 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1269 if (entry != NULL) {
1270 entry->c.text.read = snd_timer_proc_read;
1271 if (snd_info_register(entry) < 0) {
1272 snd_info_free_entry(entry);
1276 snd_timer_proc_entry = entry;
1279 static void __exit snd_timer_proc_done(void)
1281 snd_info_free_entry(snd_timer_proc_entry);
1283 #else /* !CONFIG_SND_PROC_FS */
1284 #define snd_timer_proc_init()
1285 #define snd_timer_proc_done()
1289 * USER SPACE interface
1292 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1293 unsigned long resolution,
1294 unsigned long ticks)
1296 struct snd_timer_user *tu = timeri->callback_data;
1297 struct snd_timer_read *r;
1300 spin_lock(&tu->qlock);
1301 if (tu->qused > 0) {
1302 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1303 r = &tu->queue[prev];
1304 if (r->resolution == resolution) {
1309 if (tu->qused >= tu->queue_size) {
1312 r = &tu->queue[tu->qtail++];
1313 tu->qtail %= tu->queue_size;
1314 r->resolution = resolution;
1319 spin_unlock(&tu->qlock);
1320 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1321 wake_up(&tu->qchange_sleep);
1324 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1325 struct snd_timer_tread *tread)
1327 if (tu->qused >= tu->queue_size) {
1330 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1331 tu->qtail %= tu->queue_size;
1336 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1338 struct timespec *tstamp,
1339 unsigned long resolution)
1341 struct snd_timer_user *tu = timeri->callback_data;
1342 struct snd_timer_tread r1;
1343 unsigned long flags;
1345 if (event >= SNDRV_TIMER_EVENT_START &&
1346 event <= SNDRV_TIMER_EVENT_PAUSE)
1347 tu->tstamp = *tstamp;
1348 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1350 memset(&r1, 0, sizeof(r1));
1352 r1.tstamp = *tstamp;
1353 r1.val = resolution;
1354 spin_lock_irqsave(&tu->qlock, flags);
1355 snd_timer_user_append_to_tqueue(tu, &r1);
1356 spin_unlock_irqrestore(&tu->qlock, flags);
1357 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1358 wake_up(&tu->qchange_sleep);
1361 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1363 struct snd_timer_user *tu = timeri->callback_data;
1365 tu->disconnected = true;
1366 wake_up(&tu->qchange_sleep);
1369 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1370 unsigned long resolution,
1371 unsigned long ticks)
1373 struct snd_timer_user *tu = timeri->callback_data;
1374 struct snd_timer_tread *r, r1;
1375 struct timespec tstamp;
1376 int prev, append = 0;
1378 memset(&r1, 0, sizeof(r1));
1379 memset(&tstamp, 0, sizeof(tstamp));
1380 spin_lock(&tu->qlock);
1381 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1382 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1383 spin_unlock(&tu->qlock);
1386 if (tu->last_resolution != resolution || ticks > 0) {
1387 if (timer_tstamp_monotonic)
1388 ktime_get_ts(&tstamp);
1390 getnstimeofday(&tstamp);
1392 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1393 tu->last_resolution != resolution) {
1394 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1396 r1.val = resolution;
1397 snd_timer_user_append_to_tqueue(tu, &r1);
1398 tu->last_resolution = resolution;
1401 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1405 if (tu->qused > 0) {
1406 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1407 r = &tu->tqueue[prev];
1408 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1415 r1.event = SNDRV_TIMER_EVENT_TICK;
1418 snd_timer_user_append_to_tqueue(tu, &r1);
1421 spin_unlock(&tu->qlock);
1424 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1425 wake_up(&tu->qchange_sleep);
1428 static int realloc_user_queue(struct snd_timer_user *tu, int size)
1430 struct snd_timer_read *queue = NULL;
1431 struct snd_timer_tread *tqueue = NULL;
1434 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
1438 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
1443 spin_lock_irq(&tu->qlock);
1446 tu->queue_size = size;
1448 tu->tqueue = tqueue;
1449 tu->qhead = tu->qtail = tu->qused = 0;
1450 spin_unlock_irq(&tu->qlock);
1455 static int snd_timer_user_open(struct inode *inode, struct file *file)
1457 struct snd_timer_user *tu;
1460 err = stream_open(inode, file);
1464 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1467 spin_lock_init(&tu->qlock);
1468 init_waitqueue_head(&tu->qchange_sleep);
1469 mutex_init(&tu->ioctl_lock);
1471 if (realloc_user_queue(tu, 128) < 0) {
1475 file->private_data = tu;
1479 static int snd_timer_user_release(struct inode *inode, struct file *file)
1481 struct snd_timer_user *tu;
1483 if (file->private_data) {
1484 tu = file->private_data;
1485 file->private_data = NULL;
1486 mutex_lock(&tu->ioctl_lock);
1488 snd_timer_close(tu->timeri);
1489 mutex_unlock(&tu->ioctl_lock);
1497 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1499 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1500 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1506 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1508 id->dev_class = timer->tmr_class;
1509 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1510 id->card = timer->card ? timer->card->number : -1;
1511 id->device = timer->tmr_device;
1512 id->subdevice = timer->tmr_subdevice;
1515 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1517 struct snd_timer_id id;
1518 struct snd_timer *timer;
1519 struct list_head *p;
1521 if (copy_from_user(&id, _tid, sizeof(id)))
1523 mutex_lock(®ister_mutex);
1524 if (id.dev_class < 0) { /* first item */
1525 if (list_empty(&snd_timer_list))
1526 snd_timer_user_zero_id(&id);
1528 timer = list_entry(snd_timer_list.next,
1529 struct snd_timer, device_list);
1530 snd_timer_user_copy_id(&id, timer);
1533 switch (id.dev_class) {
1534 case SNDRV_TIMER_CLASS_GLOBAL:
1535 id.device = id.device < 0 ? 0 : id.device + 1;
1536 list_for_each(p, &snd_timer_list) {
1537 timer = list_entry(p, struct snd_timer, device_list);
1538 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1539 snd_timer_user_copy_id(&id, timer);
1542 if (timer->tmr_device >= id.device) {
1543 snd_timer_user_copy_id(&id, timer);
1547 if (p == &snd_timer_list)
1548 snd_timer_user_zero_id(&id);
1550 case SNDRV_TIMER_CLASS_CARD:
1551 case SNDRV_TIMER_CLASS_PCM:
1555 if (id.device < 0) {
1558 if (id.subdevice < 0)
1560 else if (id.subdevice < INT_MAX)
1564 list_for_each(p, &snd_timer_list) {
1565 timer = list_entry(p, struct snd_timer, device_list);
1566 if (timer->tmr_class > id.dev_class) {
1567 snd_timer_user_copy_id(&id, timer);
1570 if (timer->tmr_class < id.dev_class)
1572 if (timer->card->number > id.card) {
1573 snd_timer_user_copy_id(&id, timer);
1576 if (timer->card->number < id.card)
1578 if (timer->tmr_device > id.device) {
1579 snd_timer_user_copy_id(&id, timer);
1582 if (timer->tmr_device < id.device)
1584 if (timer->tmr_subdevice > id.subdevice) {
1585 snd_timer_user_copy_id(&id, timer);
1588 if (timer->tmr_subdevice < id.subdevice)
1590 snd_timer_user_copy_id(&id, timer);
1593 if (p == &snd_timer_list)
1594 snd_timer_user_zero_id(&id);
1597 snd_timer_user_zero_id(&id);
1600 mutex_unlock(®ister_mutex);
1601 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1606 static int snd_timer_user_ginfo(struct file *file,
1607 struct snd_timer_ginfo __user *_ginfo)
1609 struct snd_timer_ginfo *ginfo;
1610 struct snd_timer_id tid;
1611 struct snd_timer *t;
1612 struct list_head *p;
1615 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1617 return PTR_ERR(ginfo);
1620 memset(ginfo, 0, sizeof(*ginfo));
1622 mutex_lock(®ister_mutex);
1623 t = snd_timer_find(&tid);
1625 ginfo->card = t->card ? t->card->number : -1;
1626 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1627 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1628 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1629 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1630 ginfo->resolution = t->hw.resolution;
1631 if (t->hw.resolution_min > 0) {
1632 ginfo->resolution_min = t->hw.resolution_min;
1633 ginfo->resolution_max = t->hw.resolution_max;
1635 list_for_each(p, &t->open_list_head) {
1641 mutex_unlock(®ister_mutex);
1642 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1648 static int timer_set_gparams(struct snd_timer_gparams *gparams)
1650 struct snd_timer *t;
1653 mutex_lock(®ister_mutex);
1654 t = snd_timer_find(&gparams->tid);
1659 if (!list_empty(&t->open_list_head)) {
1663 if (!t->hw.set_period) {
1667 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
1669 mutex_unlock(®ister_mutex);
1673 static int snd_timer_user_gparams(struct file *file,
1674 struct snd_timer_gparams __user *_gparams)
1676 struct snd_timer_gparams gparams;
1678 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1680 return timer_set_gparams(&gparams);
1683 static int snd_timer_user_gstatus(struct file *file,
1684 struct snd_timer_gstatus __user *_gstatus)
1686 struct snd_timer_gstatus gstatus;
1687 struct snd_timer_id tid;
1688 struct snd_timer *t;
1691 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1694 memset(&gstatus, 0, sizeof(gstatus));
1696 mutex_lock(®ister_mutex);
1697 t = snd_timer_find(&tid);
1699 spin_lock_irq(&t->lock);
1700 gstatus.resolution = snd_timer_hw_resolution(t);
1701 if (t->hw.precise_resolution) {
1702 t->hw.precise_resolution(t, &gstatus.resolution_num,
1703 &gstatus.resolution_den);
1705 gstatus.resolution_num = gstatus.resolution;
1706 gstatus.resolution_den = 1000000000uL;
1708 spin_unlock_irq(&t->lock);
1712 mutex_unlock(®ister_mutex);
1713 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1718 static int snd_timer_user_tselect(struct file *file,
1719 struct snd_timer_select __user *_tselect)
1721 struct snd_timer_user *tu;
1722 struct snd_timer_select tselect;
1726 tu = file->private_data;
1728 snd_timer_close(tu->timeri);
1731 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1735 sprintf(str, "application %i", current->pid);
1736 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1737 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1738 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1742 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1743 tu->timeri->callback = tu->tread
1744 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1745 tu->timeri->ccallback = snd_timer_user_ccallback;
1746 tu->timeri->callback_data = (void *)tu;
1747 tu->timeri->disconnect = snd_timer_user_disconnect;
1753 static int snd_timer_user_info(struct file *file,
1754 struct snd_timer_info __user *_info)
1756 struct snd_timer_user *tu;
1757 struct snd_timer_info *info;
1758 struct snd_timer *t;
1761 tu = file->private_data;
1764 t = tu->timeri->timer;
1768 info = kzalloc(sizeof(*info), GFP_KERNEL);
1771 info->card = t->card ? t->card->number : -1;
1772 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1773 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1774 strlcpy(info->id, t->id, sizeof(info->id));
1775 strlcpy(info->name, t->name, sizeof(info->name));
1776 info->resolution = t->hw.resolution;
1777 if (copy_to_user(_info, info, sizeof(*_info)))
1783 static int snd_timer_user_params(struct file *file,
1784 struct snd_timer_params __user *_params)
1786 struct snd_timer_user *tu;
1787 struct snd_timer_params params;
1788 struct snd_timer *t;
1791 tu = file->private_data;
1794 t = tu->timeri->timer;
1797 if (copy_from_user(¶ms, _params, sizeof(params)))
1799 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1802 if (params.ticks < 1) {
1807 /* Don't allow resolution less than 1ms */
1808 resolution = snd_timer_resolution(tu->timeri);
1809 resolution *= params.ticks;
1810 if (resolution < 1000000) {
1815 if (params.queue_size > 0 &&
1816 (params.queue_size < 32 || params.queue_size > 1024)) {
1820 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1821 (1<<SNDRV_TIMER_EVENT_TICK)|
1822 (1<<SNDRV_TIMER_EVENT_START)|
1823 (1<<SNDRV_TIMER_EVENT_STOP)|
1824 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1825 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1826 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1827 (1<<SNDRV_TIMER_EVENT_RESUME)|
1828 (1<<SNDRV_TIMER_EVENT_MSTART)|
1829 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1830 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1831 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1832 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1833 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1837 snd_timer_stop(tu->timeri);
1838 spin_lock_irq(&t->lock);
1839 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1840 SNDRV_TIMER_IFLG_EXCLUSIVE|
1841 SNDRV_TIMER_IFLG_EARLY_EVENT);
1842 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1843 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1844 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1845 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1846 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1847 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1848 spin_unlock_irq(&t->lock);
1849 if (params.queue_size > 0 &&
1850 (unsigned int)tu->queue_size != params.queue_size) {
1851 err = realloc_user_queue(tu, params.queue_size);
1855 spin_lock_irq(&tu->qlock);
1856 tu->qhead = tu->qtail = tu->qused = 0;
1857 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1859 struct snd_timer_tread tread;
1860 memset(&tread, 0, sizeof(tread));
1861 tread.event = SNDRV_TIMER_EVENT_EARLY;
1862 tread.tstamp.tv_sec = 0;
1863 tread.tstamp.tv_nsec = 0;
1865 snd_timer_user_append_to_tqueue(tu, &tread);
1867 struct snd_timer_read *r = &tu->queue[0];
1874 tu->filter = params.filter;
1875 tu->ticks = params.ticks;
1876 spin_unlock_irq(&tu->qlock);
1879 if (copy_to_user(_params, ¶ms, sizeof(params)))
1884 static int snd_timer_user_status(struct file *file,
1885 struct snd_timer_status __user *_status)
1887 struct snd_timer_user *tu;
1888 struct snd_timer_status status;
1890 tu = file->private_data;
1893 memset(&status, 0, sizeof(status));
1894 status.tstamp = tu->tstamp;
1895 status.resolution = snd_timer_resolution(tu->timeri);
1896 status.lost = tu->timeri->lost;
1897 status.overrun = tu->overrun;
1898 spin_lock_irq(&tu->qlock);
1899 status.queue = tu->qused;
1900 spin_unlock_irq(&tu->qlock);
1901 if (copy_to_user(_status, &status, sizeof(status)))
1906 static int snd_timer_user_start(struct file *file)
1909 struct snd_timer_user *tu;
1911 tu = file->private_data;
1914 snd_timer_stop(tu->timeri);
1915 tu->timeri->lost = 0;
1916 tu->last_resolution = 0;
1917 err = snd_timer_start(tu->timeri, tu->ticks);
1923 static int snd_timer_user_stop(struct file *file)
1926 struct snd_timer_user *tu;
1928 tu = file->private_data;
1931 err = snd_timer_stop(tu->timeri);
1937 static int snd_timer_user_continue(struct file *file)
1940 struct snd_timer_user *tu;
1942 tu = file->private_data;
1945 /* start timer instead of continue if it's not used before */
1946 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1947 return snd_timer_user_start(file);
1948 tu->timeri->lost = 0;
1949 err = snd_timer_continue(tu->timeri);
1955 static int snd_timer_user_pause(struct file *file)
1958 struct snd_timer_user *tu;
1960 tu = file->private_data;
1963 err = snd_timer_pause(tu->timeri);
1970 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1971 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1972 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1973 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1976 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1979 struct snd_timer_user *tu;
1980 void __user *argp = (void __user *)arg;
1981 int __user *p = argp;
1983 tu = file->private_data;
1985 case SNDRV_TIMER_IOCTL_PVERSION:
1986 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1987 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1988 return snd_timer_user_next_device(argp);
1989 case SNDRV_TIMER_IOCTL_TREAD:
1991 int xarg, old_tread;
1993 if (tu->timeri) /* too late */
1995 if (get_user(xarg, p))
1997 old_tread = tu->tread;
1998 tu->tread = xarg ? 1 : 0;
1999 if (tu->tread != old_tread &&
2000 realloc_user_queue(tu, tu->queue_size) < 0) {
2001 tu->tread = old_tread;
2006 case SNDRV_TIMER_IOCTL_GINFO:
2007 return snd_timer_user_ginfo(file, argp);
2008 case SNDRV_TIMER_IOCTL_GPARAMS:
2009 return snd_timer_user_gparams(file, argp);
2010 case SNDRV_TIMER_IOCTL_GSTATUS:
2011 return snd_timer_user_gstatus(file, argp);
2012 case SNDRV_TIMER_IOCTL_SELECT:
2013 return snd_timer_user_tselect(file, argp);
2014 case SNDRV_TIMER_IOCTL_INFO:
2015 return snd_timer_user_info(file, argp);
2016 case SNDRV_TIMER_IOCTL_PARAMS:
2017 return snd_timer_user_params(file, argp);
2018 case SNDRV_TIMER_IOCTL_STATUS:
2019 return snd_timer_user_status(file, argp);
2020 case SNDRV_TIMER_IOCTL_START:
2021 case SNDRV_TIMER_IOCTL_START_OLD:
2022 return snd_timer_user_start(file);
2023 case SNDRV_TIMER_IOCTL_STOP:
2024 case SNDRV_TIMER_IOCTL_STOP_OLD:
2025 return snd_timer_user_stop(file);
2026 case SNDRV_TIMER_IOCTL_CONTINUE:
2027 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
2028 return snd_timer_user_continue(file);
2029 case SNDRV_TIMER_IOCTL_PAUSE:
2030 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
2031 return snd_timer_user_pause(file);
2036 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
2039 struct snd_timer_user *tu = file->private_data;
2042 mutex_lock(&tu->ioctl_lock);
2043 ret = __snd_timer_user_ioctl(file, cmd, arg);
2044 mutex_unlock(&tu->ioctl_lock);
2048 static int snd_timer_user_fasync(int fd, struct file * file, int on)
2050 struct snd_timer_user *tu;
2052 tu = file->private_data;
2053 return fasync_helper(fd, file, on, &tu->fasync);
2056 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2057 size_t count, loff_t *offset)
2059 struct snd_timer_user *tu;
2060 long result = 0, unit;
2064 tu = file->private_data;
2065 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
2066 mutex_lock(&tu->ioctl_lock);
2067 spin_lock_irq(&tu->qlock);
2068 while ((long)count - result >= unit) {
2069 while (!tu->qused) {
2070 wait_queue_entry_t wait;
2072 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2077 set_current_state(TASK_INTERRUPTIBLE);
2078 init_waitqueue_entry(&wait, current);
2079 add_wait_queue(&tu->qchange_sleep, &wait);
2081 spin_unlock_irq(&tu->qlock);
2082 mutex_unlock(&tu->ioctl_lock);
2084 mutex_lock(&tu->ioctl_lock);
2085 spin_lock_irq(&tu->qlock);
2087 remove_wait_queue(&tu->qchange_sleep, &wait);
2089 if (tu->disconnected) {
2093 if (signal_pending(current)) {
2099 qhead = tu->qhead++;
2100 tu->qhead %= tu->queue_size;
2102 spin_unlock_irq(&tu->qlock);
2105 if (copy_to_user(buffer, &tu->tqueue[qhead],
2106 sizeof(struct snd_timer_tread)))
2109 if (copy_to_user(buffer, &tu->queue[qhead],
2110 sizeof(struct snd_timer_read)))
2114 spin_lock_irq(&tu->qlock);
2121 spin_unlock_irq(&tu->qlock);
2122 mutex_unlock(&tu->ioctl_lock);
2123 return result > 0 ? result : err;
2126 static __poll_t snd_timer_user_poll(struct file *file, poll_table * wait)
2129 struct snd_timer_user *tu;
2131 tu = file->private_data;
2133 poll_wait(file, &tu->qchange_sleep, wait);
2136 spin_lock_irq(&tu->qlock);
2138 mask |= EPOLLIN | EPOLLRDNORM;
2139 if (tu->disconnected)
2141 spin_unlock_irq(&tu->qlock);
2146 #ifdef CONFIG_COMPAT
2147 #include "timer_compat.c"
2149 #define snd_timer_user_ioctl_compat NULL
2152 static const struct file_operations snd_timer_f_ops =
2154 .owner = THIS_MODULE,
2155 .read = snd_timer_user_read,
2156 .open = snd_timer_user_open,
2157 .release = snd_timer_user_release,
2158 .llseek = no_llseek,
2159 .poll = snd_timer_user_poll,
2160 .unlocked_ioctl = snd_timer_user_ioctl,
2161 .compat_ioctl = snd_timer_user_ioctl_compat,
2162 .fasync = snd_timer_user_fasync,
2165 /* unregister the system timer */
2166 static void snd_timer_free_all(void)
2168 struct snd_timer *timer, *n;
2170 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2171 snd_timer_free(timer);
2174 static struct device timer_dev;
2180 static int __init alsa_timer_init(void)
2184 snd_device_initialize(&timer_dev, NULL);
2185 dev_set_name(&timer_dev, "timer");
2187 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2188 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2192 err = snd_timer_register_system();
2194 pr_err("ALSA: unable to register system timer (%i)\n", err);
2198 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2199 &snd_timer_f_ops, NULL, &timer_dev);
2201 pr_err("ALSA: unable to register timer device (%i)\n", err);
2202 snd_timer_free_all();
2206 snd_timer_proc_init();
2210 put_device(&timer_dev);
2214 static void __exit alsa_timer_exit(void)
2216 snd_unregister_device(&timer_dev);
2217 snd_timer_free_all();
2218 put_device(&timer_dev);
2219 snd_timer_proc_done();
2220 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2221 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2225 module_init(alsa_timer_init)
2226 module_exit(alsa_timer_exit)