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.
92 struct snd_timer_instance *snd_timer_instance_new(const char *owner)
94 struct snd_timer_instance *timeri;
96 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
99 timeri->owner = kstrdup(owner, GFP_KERNEL);
100 if (! timeri->owner) {
104 INIT_LIST_HEAD(&timeri->open_list);
105 INIT_LIST_HEAD(&timeri->active_list);
106 INIT_LIST_HEAD(&timeri->ack_list);
107 INIT_LIST_HEAD(&timeri->slave_list_head);
108 INIT_LIST_HEAD(&timeri->slave_active_head);
112 EXPORT_SYMBOL(snd_timer_instance_new);
114 void snd_timer_instance_free(struct snd_timer_instance *timeri)
117 if (timeri->private_free)
118 timeri->private_free(timeri);
119 kfree(timeri->owner);
123 EXPORT_SYMBOL(snd_timer_instance_free);
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)
149 #ifdef CONFIG_MODULES
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);
170 /* move the slave if it belongs to the master; return 1 if match */
171 static int check_matching_master_slave(struct snd_timer_instance *master,
172 struct snd_timer_instance *slave)
174 if (slave->slave_class != master->slave_class ||
175 slave->slave_id != master->slave_id)
177 if (master->timer->num_instances >= master->timer->max_instances)
179 list_move_tail(&slave->open_list, &master->slave_list_head);
180 master->timer->num_instances++;
181 spin_lock_irq(&slave_active_lock);
182 spin_lock(&master->timer->lock);
183 slave->master = master;
184 slave->timer = master->timer;
185 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
186 list_add_tail(&slave->active_list, &master->slave_active_head);
187 spin_unlock(&master->timer->lock);
188 spin_unlock_irq(&slave_active_lock);
193 * look for a master instance matching with the slave id of the given slave.
194 * when found, relink the open_link of the slave.
196 * call this with register_mutex down.
198 static int snd_timer_check_slave(struct snd_timer_instance *slave)
200 struct snd_timer *timer;
201 struct snd_timer_instance *master;
204 /* FIXME: it's really dumb to look up all entries.. */
205 list_for_each_entry(timer, &snd_timer_list, device_list) {
206 list_for_each_entry(master, &timer->open_list_head, open_list) {
207 err = check_matching_master_slave(master, slave);
208 if (err != 0) /* match found or error */
213 return err < 0 ? err : 0;
217 * look for slave instances matching with the slave id of the given master.
218 * when found, relink the open_link of slaves.
220 * call this with register_mutex down.
222 static int snd_timer_check_master(struct snd_timer_instance *master)
224 struct snd_timer_instance *slave, *tmp;
227 /* check all pending slaves */
228 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
229 err = check_matching_master_slave(master, slave);
233 return err < 0 ? err : 0;
236 static void snd_timer_close_locked(struct snd_timer_instance *timeri,
237 struct device **card_devp_to_put);
240 * open a timer instance
241 * when opening a master, the slave id must be here given.
243 int snd_timer_open(struct snd_timer_instance *timeri,
244 struct snd_timer_id *tid,
245 unsigned int slave_id)
247 struct snd_timer *timer;
248 struct device *card_dev_to_put = NULL;
251 mutex_lock(®ister_mutex);
252 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
253 /* open a slave instance */
254 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
255 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
256 pr_debug("ALSA: timer: invalid slave class %i\n",
261 if (num_slaves >= MAX_SLAVE_INSTANCES) {
265 timeri->slave_class = tid->dev_sclass;
266 timeri->slave_id = tid->device;
267 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
268 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
270 err = snd_timer_check_slave(timeri);
274 /* open a master instance */
275 timer = snd_timer_find(tid);
276 #ifdef CONFIG_MODULES
278 mutex_unlock(®ister_mutex);
279 snd_timer_request(tid);
280 mutex_lock(®ister_mutex);
281 timer = snd_timer_find(tid);
288 if (!list_empty(&timer->open_list_head)) {
289 struct snd_timer_instance *t =
290 list_entry(timer->open_list_head.next,
291 struct snd_timer_instance, open_list);
292 if (t->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
297 if (timer->num_instances >= timer->max_instances) {
301 if (!try_module_get(timer->module)) {
305 /* take a card refcount for safe disconnection */
307 get_device(&timer->card->card_dev);
308 card_dev_to_put = &timer->card->card_dev;
311 if (list_empty(&timer->open_list_head) && timer->hw.open) {
312 err = timer->hw.open(timer);
314 module_put(timer->module);
319 timeri->timer = timer;
320 timeri->slave_class = tid->dev_sclass;
321 timeri->slave_id = slave_id;
323 list_add_tail(&timeri->open_list, &timer->open_list_head);
324 timer->num_instances++;
325 err = snd_timer_check_master(timeri);
328 snd_timer_close_locked(timeri, &card_dev_to_put);
331 mutex_unlock(®ister_mutex);
332 /* put_device() is called after unlock for avoiding deadlock */
333 if (err < 0 && card_dev_to_put)
334 put_device(card_dev_to_put);
337 EXPORT_SYMBOL(snd_timer_open);
340 * close a timer instance
341 * call this with register_mutex down.
343 static void snd_timer_close_locked(struct snd_timer_instance *timeri,
344 struct device **card_devp_to_put)
346 struct snd_timer *timer = timeri->timer;
347 struct snd_timer_instance *slave, *tmp;
350 spin_lock_irq(&timer->lock);
351 timeri->flags |= SNDRV_TIMER_IFLG_DEAD;
352 spin_unlock_irq(&timer->lock);
355 if (!list_empty(&timeri->open_list)) {
356 list_del_init(&timeri->open_list);
357 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
361 /* force to stop the timer */
362 snd_timer_stop(timeri);
365 timer->num_instances--;
366 /* wait, until the active callback is finished */
367 spin_lock_irq(&timer->lock);
368 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
369 spin_unlock_irq(&timer->lock);
371 spin_lock_irq(&timer->lock);
373 spin_unlock_irq(&timer->lock);
375 /* remove slave links */
376 spin_lock_irq(&slave_active_lock);
377 spin_lock(&timer->lock);
378 timeri->timer = NULL;
379 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
381 list_move_tail(&slave->open_list, &snd_timer_slave_list);
382 timer->num_instances--;
383 slave->master = NULL;
385 list_del_init(&slave->ack_list);
386 list_del_init(&slave->active_list);
388 spin_unlock(&timer->lock);
389 spin_unlock_irq(&slave_active_lock);
391 /* slave doesn't need to release timer resources below */
392 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
397 if (list_empty(&timer->open_list_head) && timer->hw.close)
398 timer->hw.close(timer);
399 /* release a card refcount for safe disconnection */
401 *card_devp_to_put = &timer->card->card_dev;
402 module_put(timer->module);
407 * close a timer instance
409 void snd_timer_close(struct snd_timer_instance *timeri)
411 struct device *card_dev_to_put = NULL;
413 if (snd_BUG_ON(!timeri))
416 mutex_lock(®ister_mutex);
417 snd_timer_close_locked(timeri, &card_dev_to_put);
418 mutex_unlock(®ister_mutex);
419 /* put_device() is called after unlock for avoiding deadlock */
421 put_device(card_dev_to_put);
423 EXPORT_SYMBOL(snd_timer_close);
425 static unsigned long snd_timer_hw_resolution(struct snd_timer *timer)
427 if (timer->hw.c_resolution)
428 return timer->hw.c_resolution(timer);
430 return timer->hw.resolution;
433 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
435 struct snd_timer * timer;
436 unsigned long ret = 0;
441 timer = timeri->timer;
443 spin_lock_irqsave(&timer->lock, flags);
444 ret = snd_timer_hw_resolution(timer);
445 spin_unlock_irqrestore(&timer->lock, flags);
449 EXPORT_SYMBOL(snd_timer_resolution);
451 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
453 struct snd_timer *timer = ti->timer;
454 unsigned long resolution = 0;
455 struct snd_timer_instance *ts;
456 struct timespec tstamp;
458 if (timer_tstamp_monotonic)
459 ktime_get_ts(&tstamp);
461 getnstimeofday(&tstamp);
462 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
463 event > SNDRV_TIMER_EVENT_PAUSE))
466 (event == SNDRV_TIMER_EVENT_START ||
467 event == SNDRV_TIMER_EVENT_CONTINUE))
468 resolution = snd_timer_hw_resolution(timer);
470 ti->ccallback(ti, event, &tstamp, resolution);
471 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
475 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
477 list_for_each_entry(ts, &ti->slave_active_head, active_list)
479 ts->ccallback(ts, event + 100, &tstamp, resolution);
482 /* start/continue a master timer */
483 static int snd_timer_start1(struct snd_timer_instance *timeri,
484 bool start, unsigned long ticks)
486 struct snd_timer *timer;
490 timer = timeri->timer;
494 spin_lock_irqsave(&timer->lock, flags);
495 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
499 if (timer->card && timer->card->shutdown) {
503 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
504 SNDRV_TIMER_IFLG_START)) {
510 timeri->ticks = timeri->cticks = ticks;
511 else if (!timeri->cticks)
515 list_move_tail(&timeri->active_list, &timer->active_list_head);
516 if (timer->running) {
517 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
519 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
520 timeri->flags |= SNDRV_TIMER_IFLG_START;
521 result = 1; /* delayed start */
524 timer->sticks = ticks;
525 timer->hw.start(timer);
528 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
531 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
532 SNDRV_TIMER_EVENT_CONTINUE);
534 spin_unlock_irqrestore(&timer->lock, flags);
538 /* start/continue a slave timer */
539 static int snd_timer_start_slave(struct snd_timer_instance *timeri,
545 spin_lock_irqsave(&slave_active_lock, flags);
546 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
550 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
554 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
555 if (timeri->master && timeri->timer) {
556 spin_lock(&timeri->timer->lock);
557 list_add_tail(&timeri->active_list,
558 &timeri->master->slave_active_head);
559 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
560 SNDRV_TIMER_EVENT_CONTINUE);
561 spin_unlock(&timeri->timer->lock);
563 err = 1; /* delayed start */
565 spin_unlock_irqrestore(&slave_active_lock, flags);
569 /* stop/pause a master timer */
570 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
572 struct snd_timer *timer;
576 timer = timeri->timer;
579 spin_lock_irqsave(&timer->lock, flags);
580 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
581 SNDRV_TIMER_IFLG_START))) {
585 list_del_init(&timeri->ack_list);
586 list_del_init(&timeri->active_list);
587 if (timer->card && timer->card->shutdown)
590 timeri->cticks = timeri->ticks;
593 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
594 !(--timer->running)) {
595 timer->hw.stop(timer);
596 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
597 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
598 snd_timer_reschedule(timer, 0);
599 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
600 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
601 timer->hw.start(timer);
605 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
607 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
609 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
610 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
611 SNDRV_TIMER_EVENT_PAUSE);
613 spin_unlock_irqrestore(&timer->lock, flags);
617 /* stop/pause a slave timer */
618 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
622 spin_lock_irqsave(&slave_active_lock, flags);
623 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
624 spin_unlock_irqrestore(&slave_active_lock, flags);
627 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
629 spin_lock(&timeri->timer->lock);
630 list_del_init(&timeri->ack_list);
631 list_del_init(&timeri->active_list);
632 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
633 SNDRV_TIMER_EVENT_PAUSE);
634 spin_unlock(&timeri->timer->lock);
636 spin_unlock_irqrestore(&slave_active_lock, flags);
641 * start the timer instance
643 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
645 if (timeri == NULL || ticks < 1)
647 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
648 return snd_timer_start_slave(timeri, true);
650 return snd_timer_start1(timeri, true, ticks);
652 EXPORT_SYMBOL(snd_timer_start);
655 * stop the timer instance.
657 * do not call this from the timer callback!
659 int snd_timer_stop(struct snd_timer_instance *timeri)
661 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
662 return snd_timer_stop_slave(timeri, true);
664 return snd_timer_stop1(timeri, true);
666 EXPORT_SYMBOL(snd_timer_stop);
669 * start again.. the tick is kept.
671 int snd_timer_continue(struct snd_timer_instance *timeri)
673 /* timer can continue only after pause */
674 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
677 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
678 return snd_timer_start_slave(timeri, false);
680 return snd_timer_start1(timeri, false, 0);
682 EXPORT_SYMBOL(snd_timer_continue);
685 * pause.. remember the ticks left
687 int snd_timer_pause(struct snd_timer_instance * timeri)
689 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
690 return snd_timer_stop_slave(timeri, false);
692 return snd_timer_stop1(timeri, false);
694 EXPORT_SYMBOL(snd_timer_pause);
697 * reschedule the timer
699 * start pending instances and check the scheduling ticks.
700 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
702 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
704 struct snd_timer_instance *ti;
705 unsigned long ticks = ~0UL;
707 list_for_each_entry(ti, &timer->active_list_head, active_list) {
708 if (ti->flags & SNDRV_TIMER_IFLG_START) {
709 ti->flags &= ~SNDRV_TIMER_IFLG_START;
710 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
713 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
714 if (ticks > ti->cticks)
719 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
722 if (ticks > timer->hw.ticks)
723 ticks = timer->hw.ticks;
724 if (ticks_left != ticks)
725 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
726 timer->sticks = ticks;
729 /* call callbacks in timer ack list */
730 static void snd_timer_process_callbacks(struct snd_timer *timer,
731 struct list_head *head)
733 struct snd_timer_instance *ti;
734 unsigned long resolution, ticks;
736 while (!list_empty(head)) {
737 ti = list_first_entry(head, struct snd_timer_instance,
740 /* remove from ack_list and make empty */
741 list_del_init(&ti->ack_list);
743 if (!(ti->flags & SNDRV_TIMER_IFLG_DEAD)) {
746 resolution = ti->resolution;
747 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
748 spin_unlock(&timer->lock);
750 ti->callback(ti, resolution, ticks);
751 spin_lock(&timer->lock);
752 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
757 /* clear pending instances from ack list */
758 static void snd_timer_clear_callbacks(struct snd_timer *timer,
759 struct list_head *head)
763 spin_lock_irqsave(&timer->lock, flags);
764 while (!list_empty(head))
765 list_del_init(head->next);
766 spin_unlock_irqrestore(&timer->lock, flags);
773 static void snd_timer_tasklet(unsigned long arg)
775 struct snd_timer *timer = (struct snd_timer *) arg;
778 if (timer->card && timer->card->shutdown) {
779 snd_timer_clear_callbacks(timer, &timer->sack_list_head);
783 spin_lock_irqsave(&timer->lock, flags);
784 snd_timer_process_callbacks(timer, &timer->sack_list_head);
785 spin_unlock_irqrestore(&timer->lock, flags);
791 * ticks_left is usually equal to timer->sticks.
794 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
796 struct snd_timer_instance *ti, *ts, *tmp;
797 unsigned long resolution;
798 struct list_head *ack_list_head;
805 if (timer->card && timer->card->shutdown) {
806 snd_timer_clear_callbacks(timer, &timer->ack_list_head);
810 spin_lock_irqsave(&timer->lock, flags);
812 /* remember the current resolution */
813 resolution = snd_timer_hw_resolution(timer);
815 /* loop for all active instances
816 * Here we cannot use list_for_each_entry because the active_list of a
817 * processed instance is relinked to done_list_head before the callback
820 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
822 if (ti->flags & SNDRV_TIMER_IFLG_DEAD)
824 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
826 ti->pticks += ticks_left;
827 ti->resolution = resolution;
828 if (ti->cticks < ticks_left)
831 ti->cticks -= ticks_left;
832 if (ti->cticks) /* not expired */
834 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
835 ti->cticks = ti->ticks;
837 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
839 list_del_init(&ti->active_list);
841 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
842 (ti->flags & SNDRV_TIMER_IFLG_FAST))
843 ack_list_head = &timer->ack_list_head;
845 ack_list_head = &timer->sack_list_head;
846 if (list_empty(&ti->ack_list))
847 list_add_tail(&ti->ack_list, ack_list_head);
848 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
849 ts->pticks = ti->pticks;
850 ts->resolution = resolution;
851 if (list_empty(&ts->ack_list))
852 list_add_tail(&ts->ack_list, ack_list_head);
855 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
856 snd_timer_reschedule(timer, timer->sticks);
857 if (timer->running) {
858 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
859 timer->hw.stop(timer);
860 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
862 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
863 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
865 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
866 timer->hw.start(timer);
869 timer->hw.stop(timer);
872 /* now process all fast callbacks */
873 snd_timer_process_callbacks(timer, &timer->ack_list_head);
875 /* do we have any slow callbacks? */
876 use_tasklet = !list_empty(&timer->sack_list_head);
877 spin_unlock_irqrestore(&timer->lock, flags);
880 tasklet_schedule(&timer->task_queue);
882 EXPORT_SYMBOL(snd_timer_interrupt);
888 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
889 struct snd_timer **rtimer)
891 struct snd_timer *timer;
893 static struct snd_device_ops ops = {
894 .dev_free = snd_timer_dev_free,
895 .dev_register = snd_timer_dev_register,
896 .dev_disconnect = snd_timer_dev_disconnect,
899 if (snd_BUG_ON(!tid))
901 if (tid->dev_class == SNDRV_TIMER_CLASS_CARD ||
902 tid->dev_class == SNDRV_TIMER_CLASS_PCM) {
908 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
911 timer->tmr_class = tid->dev_class;
913 timer->tmr_device = tid->device;
914 timer->tmr_subdevice = tid->subdevice;
916 strlcpy(timer->id, id, sizeof(timer->id));
918 INIT_LIST_HEAD(&timer->device_list);
919 INIT_LIST_HEAD(&timer->open_list_head);
920 INIT_LIST_HEAD(&timer->active_list_head);
921 INIT_LIST_HEAD(&timer->ack_list_head);
922 INIT_LIST_HEAD(&timer->sack_list_head);
923 spin_lock_init(&timer->lock);
924 tasklet_init(&timer->task_queue, snd_timer_tasklet,
925 (unsigned long)timer);
926 timer->max_instances = 1000; /* default limit per timer */
928 timer->module = card->module;
929 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
931 snd_timer_free(timer);
939 EXPORT_SYMBOL(snd_timer_new);
941 static int snd_timer_free(struct snd_timer *timer)
946 mutex_lock(®ister_mutex);
947 if (! list_empty(&timer->open_list_head)) {
948 struct list_head *p, *n;
949 struct snd_timer_instance *ti;
950 pr_warn("ALSA: timer %p is busy?\n", timer);
951 list_for_each_safe(p, n, &timer->open_list_head) {
953 ti = list_entry(p, struct snd_timer_instance, open_list);
957 list_del(&timer->device_list);
958 mutex_unlock(®ister_mutex);
960 if (timer->private_free)
961 timer->private_free(timer);
966 static int snd_timer_dev_free(struct snd_device *device)
968 struct snd_timer *timer = device->device_data;
969 return snd_timer_free(timer);
972 static int snd_timer_dev_register(struct snd_device *dev)
974 struct snd_timer *timer = dev->device_data;
975 struct snd_timer *timer1;
977 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
979 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
980 !timer->hw.resolution && timer->hw.c_resolution == NULL)
983 mutex_lock(®ister_mutex);
984 list_for_each_entry(timer1, &snd_timer_list, device_list) {
985 if (timer1->tmr_class > timer->tmr_class)
987 if (timer1->tmr_class < timer->tmr_class)
989 if (timer1->card && timer->card) {
990 if (timer1->card->number > timer->card->number)
992 if (timer1->card->number < timer->card->number)
995 if (timer1->tmr_device > timer->tmr_device)
997 if (timer1->tmr_device < timer->tmr_device)
999 if (timer1->tmr_subdevice > timer->tmr_subdevice)
1001 if (timer1->tmr_subdevice < timer->tmr_subdevice)
1004 mutex_unlock(®ister_mutex);
1007 list_add_tail(&timer->device_list, &timer1->device_list);
1008 mutex_unlock(®ister_mutex);
1012 static int snd_timer_dev_disconnect(struct snd_device *device)
1014 struct snd_timer *timer = device->device_data;
1015 struct snd_timer_instance *ti;
1017 mutex_lock(®ister_mutex);
1018 list_del_init(&timer->device_list);
1019 /* wake up pending sleepers */
1020 list_for_each_entry(ti, &timer->open_list_head, open_list) {
1024 mutex_unlock(®ister_mutex);
1028 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
1030 unsigned long flags;
1031 unsigned long resolution = 0;
1032 struct snd_timer_instance *ti, *ts;
1034 if (timer->card && timer->card->shutdown)
1036 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1038 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1039 event > SNDRV_TIMER_EVENT_MRESUME))
1041 spin_lock_irqsave(&timer->lock, flags);
1042 if (event == SNDRV_TIMER_EVENT_MSTART ||
1043 event == SNDRV_TIMER_EVENT_MCONTINUE ||
1044 event == SNDRV_TIMER_EVENT_MRESUME)
1045 resolution = snd_timer_hw_resolution(timer);
1046 list_for_each_entry(ti, &timer->active_list_head, active_list) {
1048 ti->ccallback(ti, event, tstamp, resolution);
1049 list_for_each_entry(ts, &ti->slave_active_head, active_list)
1051 ts->ccallback(ts, event, tstamp, resolution);
1053 spin_unlock_irqrestore(&timer->lock, flags);
1055 EXPORT_SYMBOL(snd_timer_notify);
1058 * exported functions for global timers
1060 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1062 struct snd_timer_id tid;
1064 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1065 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1067 tid.device = device;
1069 return snd_timer_new(NULL, id, &tid, rtimer);
1071 EXPORT_SYMBOL(snd_timer_global_new);
1073 int snd_timer_global_free(struct snd_timer *timer)
1075 return snd_timer_free(timer);
1077 EXPORT_SYMBOL(snd_timer_global_free);
1079 int snd_timer_global_register(struct snd_timer *timer)
1081 struct snd_device dev;
1083 memset(&dev, 0, sizeof(dev));
1084 dev.device_data = timer;
1085 return snd_timer_dev_register(&dev);
1087 EXPORT_SYMBOL(snd_timer_global_register);
1093 struct snd_timer_system_private {
1094 struct timer_list tlist;
1095 struct snd_timer *snd_timer;
1096 unsigned long last_expires;
1097 unsigned long last_jiffies;
1098 unsigned long correction;
1101 static void snd_timer_s_function(struct timer_list *t)
1103 struct snd_timer_system_private *priv = from_timer(priv, t,
1105 struct snd_timer *timer = priv->snd_timer;
1106 unsigned long jiff = jiffies;
1107 if (time_after(jiff, priv->last_expires))
1108 priv->correction += (long)jiff - (long)priv->last_expires;
1109 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1112 static int snd_timer_s_start(struct snd_timer * timer)
1114 struct snd_timer_system_private *priv;
1115 unsigned long njiff;
1117 priv = (struct snd_timer_system_private *) timer->private_data;
1118 njiff = (priv->last_jiffies = jiffies);
1119 if (priv->correction > timer->sticks - 1) {
1120 priv->correction -= timer->sticks - 1;
1123 njiff += timer->sticks - priv->correction;
1124 priv->correction = 0;
1126 priv->last_expires = njiff;
1127 mod_timer(&priv->tlist, njiff);
1131 static int snd_timer_s_stop(struct snd_timer * timer)
1133 struct snd_timer_system_private *priv;
1136 priv = (struct snd_timer_system_private *) timer->private_data;
1137 del_timer(&priv->tlist);
1139 if (time_before(jiff, priv->last_expires))
1140 timer->sticks = priv->last_expires - jiff;
1143 priv->correction = 0;
1147 static int snd_timer_s_close(struct snd_timer *timer)
1149 struct snd_timer_system_private *priv;
1151 priv = (struct snd_timer_system_private *)timer->private_data;
1152 del_timer_sync(&priv->tlist);
1156 static struct snd_timer_hardware snd_timer_system =
1158 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1159 .resolution = 1000000000L / HZ,
1161 .close = snd_timer_s_close,
1162 .start = snd_timer_s_start,
1163 .stop = snd_timer_s_stop
1166 static void snd_timer_free_system(struct snd_timer *timer)
1168 kfree(timer->private_data);
1171 static int snd_timer_register_system(void)
1173 struct snd_timer *timer;
1174 struct snd_timer_system_private *priv;
1177 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1180 strcpy(timer->name, "system timer");
1181 timer->hw = snd_timer_system;
1182 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1184 snd_timer_free(timer);
1187 priv->snd_timer = timer;
1188 timer_setup(&priv->tlist, snd_timer_s_function, 0);
1189 timer->private_data = priv;
1190 timer->private_free = snd_timer_free_system;
1191 return snd_timer_global_register(timer);
1194 #ifdef CONFIG_SND_PROC_FS
1199 static void snd_timer_proc_read(struct snd_info_entry *entry,
1200 struct snd_info_buffer *buffer)
1202 struct snd_timer *timer;
1203 struct snd_timer_instance *ti;
1205 mutex_lock(®ister_mutex);
1206 list_for_each_entry(timer, &snd_timer_list, device_list) {
1207 if (timer->card && timer->card->shutdown)
1209 switch (timer->tmr_class) {
1210 case SNDRV_TIMER_CLASS_GLOBAL:
1211 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1213 case SNDRV_TIMER_CLASS_CARD:
1214 snd_iprintf(buffer, "C%i-%i: ",
1215 timer->card->number, timer->tmr_device);
1217 case SNDRV_TIMER_CLASS_PCM:
1218 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1219 timer->tmr_device, timer->tmr_subdevice);
1222 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1223 timer->card ? timer->card->number : -1,
1224 timer->tmr_device, timer->tmr_subdevice);
1226 snd_iprintf(buffer, "%s :", timer->name);
1227 if (timer->hw.resolution)
1228 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1229 timer->hw.resolution / 1000,
1230 timer->hw.resolution % 1000,
1232 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1233 snd_iprintf(buffer, " SLAVE");
1234 snd_iprintf(buffer, "\n");
1235 list_for_each_entry(ti, &timer->open_list_head, open_list)
1236 snd_iprintf(buffer, " Client %s : %s\n",
1237 ti->owner ? ti->owner : "unknown",
1238 ti->flags & (SNDRV_TIMER_IFLG_START |
1239 SNDRV_TIMER_IFLG_RUNNING)
1240 ? "running" : "stopped");
1242 mutex_unlock(®ister_mutex);
1245 static struct snd_info_entry *snd_timer_proc_entry;
1247 static void __init snd_timer_proc_init(void)
1249 struct snd_info_entry *entry;
1251 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1252 if (entry != NULL) {
1253 entry->c.text.read = snd_timer_proc_read;
1254 if (snd_info_register(entry) < 0) {
1255 snd_info_free_entry(entry);
1259 snd_timer_proc_entry = entry;
1262 static void __exit snd_timer_proc_done(void)
1264 snd_info_free_entry(snd_timer_proc_entry);
1266 #else /* !CONFIG_SND_PROC_FS */
1267 #define snd_timer_proc_init()
1268 #define snd_timer_proc_done()
1272 * USER SPACE interface
1275 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1276 unsigned long resolution,
1277 unsigned long ticks)
1279 struct snd_timer_user *tu = timeri->callback_data;
1280 struct snd_timer_read *r;
1283 spin_lock(&tu->qlock);
1284 if (tu->qused > 0) {
1285 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1286 r = &tu->queue[prev];
1287 if (r->resolution == resolution) {
1292 if (tu->qused >= tu->queue_size) {
1295 r = &tu->queue[tu->qtail++];
1296 tu->qtail %= tu->queue_size;
1297 r->resolution = resolution;
1302 spin_unlock(&tu->qlock);
1303 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1304 wake_up(&tu->qchange_sleep);
1307 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1308 struct snd_timer_tread *tread)
1310 if (tu->qused >= tu->queue_size) {
1313 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1314 tu->qtail %= tu->queue_size;
1319 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1321 struct timespec *tstamp,
1322 unsigned long resolution)
1324 struct snd_timer_user *tu = timeri->callback_data;
1325 struct snd_timer_tread r1;
1326 unsigned long flags;
1328 if (event >= SNDRV_TIMER_EVENT_START &&
1329 event <= SNDRV_TIMER_EVENT_PAUSE)
1330 tu->tstamp = *tstamp;
1331 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1333 memset(&r1, 0, sizeof(r1));
1335 r1.tstamp = *tstamp;
1336 r1.val = resolution;
1337 spin_lock_irqsave(&tu->qlock, flags);
1338 snd_timer_user_append_to_tqueue(tu, &r1);
1339 spin_unlock_irqrestore(&tu->qlock, flags);
1340 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1341 wake_up(&tu->qchange_sleep);
1344 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1346 struct snd_timer_user *tu = timeri->callback_data;
1348 tu->disconnected = true;
1349 wake_up(&tu->qchange_sleep);
1352 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1353 unsigned long resolution,
1354 unsigned long ticks)
1356 struct snd_timer_user *tu = timeri->callback_data;
1357 struct snd_timer_tread *r, r1;
1358 struct timespec tstamp;
1359 int prev, append = 0;
1361 memset(&r1, 0, sizeof(r1));
1362 memset(&tstamp, 0, sizeof(tstamp));
1363 spin_lock(&tu->qlock);
1364 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1365 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1366 spin_unlock(&tu->qlock);
1369 if (tu->last_resolution != resolution || ticks > 0) {
1370 if (timer_tstamp_monotonic)
1371 ktime_get_ts(&tstamp);
1373 getnstimeofday(&tstamp);
1375 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1376 tu->last_resolution != resolution) {
1377 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1379 r1.val = resolution;
1380 snd_timer_user_append_to_tqueue(tu, &r1);
1381 tu->last_resolution = resolution;
1384 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1388 if (tu->qused > 0) {
1389 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1390 r = &tu->tqueue[prev];
1391 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1398 r1.event = SNDRV_TIMER_EVENT_TICK;
1401 snd_timer_user_append_to_tqueue(tu, &r1);
1404 spin_unlock(&tu->qlock);
1407 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1408 wake_up(&tu->qchange_sleep);
1411 static int realloc_user_queue(struct snd_timer_user *tu, int size)
1413 struct snd_timer_read *queue = NULL;
1414 struct snd_timer_tread *tqueue = NULL;
1417 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
1421 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
1426 spin_lock_irq(&tu->qlock);
1429 tu->queue_size = size;
1431 tu->tqueue = tqueue;
1432 tu->qhead = tu->qtail = tu->qused = 0;
1433 spin_unlock_irq(&tu->qlock);
1438 static int snd_timer_user_open(struct inode *inode, struct file *file)
1440 struct snd_timer_user *tu;
1443 err = stream_open(inode, file);
1447 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1450 spin_lock_init(&tu->qlock);
1451 init_waitqueue_head(&tu->qchange_sleep);
1452 mutex_init(&tu->ioctl_lock);
1454 if (realloc_user_queue(tu, 128) < 0) {
1458 file->private_data = tu;
1462 static int snd_timer_user_release(struct inode *inode, struct file *file)
1464 struct snd_timer_user *tu;
1466 if (file->private_data) {
1467 tu = file->private_data;
1468 file->private_data = NULL;
1469 mutex_lock(&tu->ioctl_lock);
1471 snd_timer_close(tu->timeri);
1472 snd_timer_instance_free(tu->timeri);
1474 mutex_unlock(&tu->ioctl_lock);
1482 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1484 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1485 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1491 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1493 id->dev_class = timer->tmr_class;
1494 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1495 id->card = timer->card ? timer->card->number : -1;
1496 id->device = timer->tmr_device;
1497 id->subdevice = timer->tmr_subdevice;
1500 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1502 struct snd_timer_id id;
1503 struct snd_timer *timer;
1504 struct list_head *p;
1506 if (copy_from_user(&id, _tid, sizeof(id)))
1508 mutex_lock(®ister_mutex);
1509 if (id.dev_class < 0) { /* first item */
1510 if (list_empty(&snd_timer_list))
1511 snd_timer_user_zero_id(&id);
1513 timer = list_entry(snd_timer_list.next,
1514 struct snd_timer, device_list);
1515 snd_timer_user_copy_id(&id, timer);
1518 switch (id.dev_class) {
1519 case SNDRV_TIMER_CLASS_GLOBAL:
1520 id.device = id.device < 0 ? 0 : id.device + 1;
1521 list_for_each(p, &snd_timer_list) {
1522 timer = list_entry(p, struct snd_timer, device_list);
1523 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1524 snd_timer_user_copy_id(&id, timer);
1527 if (timer->tmr_device >= id.device) {
1528 snd_timer_user_copy_id(&id, timer);
1532 if (p == &snd_timer_list)
1533 snd_timer_user_zero_id(&id);
1535 case SNDRV_TIMER_CLASS_CARD:
1536 case SNDRV_TIMER_CLASS_PCM:
1540 if (id.device < 0) {
1543 if (id.subdevice < 0)
1545 else if (id.subdevice < INT_MAX)
1549 list_for_each(p, &snd_timer_list) {
1550 timer = list_entry(p, struct snd_timer, device_list);
1551 if (timer->tmr_class > id.dev_class) {
1552 snd_timer_user_copy_id(&id, timer);
1555 if (timer->tmr_class < id.dev_class)
1557 if (timer->card->number > id.card) {
1558 snd_timer_user_copy_id(&id, timer);
1561 if (timer->card->number < id.card)
1563 if (timer->tmr_device > id.device) {
1564 snd_timer_user_copy_id(&id, timer);
1567 if (timer->tmr_device < id.device)
1569 if (timer->tmr_subdevice > id.subdevice) {
1570 snd_timer_user_copy_id(&id, timer);
1573 if (timer->tmr_subdevice < id.subdevice)
1575 snd_timer_user_copy_id(&id, timer);
1578 if (p == &snd_timer_list)
1579 snd_timer_user_zero_id(&id);
1582 snd_timer_user_zero_id(&id);
1585 mutex_unlock(®ister_mutex);
1586 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1591 static int snd_timer_user_ginfo(struct file *file,
1592 struct snd_timer_ginfo __user *_ginfo)
1594 struct snd_timer_ginfo *ginfo;
1595 struct snd_timer_id tid;
1596 struct snd_timer *t;
1597 struct list_head *p;
1600 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1602 return PTR_ERR(ginfo);
1605 memset(ginfo, 0, sizeof(*ginfo));
1607 mutex_lock(®ister_mutex);
1608 t = snd_timer_find(&tid);
1610 ginfo->card = t->card ? t->card->number : -1;
1611 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1612 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1613 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1614 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1615 ginfo->resolution = t->hw.resolution;
1616 if (t->hw.resolution_min > 0) {
1617 ginfo->resolution_min = t->hw.resolution_min;
1618 ginfo->resolution_max = t->hw.resolution_max;
1620 list_for_each(p, &t->open_list_head) {
1626 mutex_unlock(®ister_mutex);
1627 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1633 static int timer_set_gparams(struct snd_timer_gparams *gparams)
1635 struct snd_timer *t;
1638 mutex_lock(®ister_mutex);
1639 t = snd_timer_find(&gparams->tid);
1644 if (!list_empty(&t->open_list_head)) {
1648 if (!t->hw.set_period) {
1652 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
1654 mutex_unlock(®ister_mutex);
1658 static int snd_timer_user_gparams(struct file *file,
1659 struct snd_timer_gparams __user *_gparams)
1661 struct snd_timer_gparams gparams;
1663 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1665 return timer_set_gparams(&gparams);
1668 static int snd_timer_user_gstatus(struct file *file,
1669 struct snd_timer_gstatus __user *_gstatus)
1671 struct snd_timer_gstatus gstatus;
1672 struct snd_timer_id tid;
1673 struct snd_timer *t;
1676 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1679 memset(&gstatus, 0, sizeof(gstatus));
1681 mutex_lock(®ister_mutex);
1682 t = snd_timer_find(&tid);
1684 spin_lock_irq(&t->lock);
1685 gstatus.resolution = snd_timer_hw_resolution(t);
1686 if (t->hw.precise_resolution) {
1687 t->hw.precise_resolution(t, &gstatus.resolution_num,
1688 &gstatus.resolution_den);
1690 gstatus.resolution_num = gstatus.resolution;
1691 gstatus.resolution_den = 1000000000uL;
1693 spin_unlock_irq(&t->lock);
1697 mutex_unlock(®ister_mutex);
1698 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1703 static int snd_timer_user_tselect(struct file *file,
1704 struct snd_timer_select __user *_tselect)
1706 struct snd_timer_user *tu;
1707 struct snd_timer_select tselect;
1711 tu = file->private_data;
1713 snd_timer_close(tu->timeri);
1714 snd_timer_instance_free(tu->timeri);
1717 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1721 sprintf(str, "application %i", current->pid);
1722 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1723 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1724 tu->timeri = snd_timer_instance_new(str);
1730 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1731 tu->timeri->callback = tu->tread
1732 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1733 tu->timeri->ccallback = snd_timer_user_ccallback;
1734 tu->timeri->callback_data = (void *)tu;
1735 tu->timeri->disconnect = snd_timer_user_disconnect;
1737 err = snd_timer_open(tu->timeri, &tselect.id, current->pid);
1739 snd_timer_instance_free(tu->timeri);
1747 static int snd_timer_user_info(struct file *file,
1748 struct snd_timer_info __user *_info)
1750 struct snd_timer_user *tu;
1751 struct snd_timer_info *info;
1752 struct snd_timer *t;
1755 tu = file->private_data;
1758 t = tu->timeri->timer;
1762 info = kzalloc(sizeof(*info), GFP_KERNEL);
1765 info->card = t->card ? t->card->number : -1;
1766 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1767 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1768 strlcpy(info->id, t->id, sizeof(info->id));
1769 strlcpy(info->name, t->name, sizeof(info->name));
1770 info->resolution = t->hw.resolution;
1771 if (copy_to_user(_info, info, sizeof(*_info)))
1777 static int snd_timer_user_params(struct file *file,
1778 struct snd_timer_params __user *_params)
1780 struct snd_timer_user *tu;
1781 struct snd_timer_params params;
1782 struct snd_timer *t;
1785 tu = file->private_data;
1788 t = tu->timeri->timer;
1791 if (copy_from_user(¶ms, _params, sizeof(params)))
1793 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1796 if (params.ticks < 1) {
1801 /* Don't allow resolution less than 1ms */
1802 resolution = snd_timer_resolution(tu->timeri);
1803 resolution *= params.ticks;
1804 if (resolution < 1000000) {
1809 if (params.queue_size > 0 &&
1810 (params.queue_size < 32 || params.queue_size > 1024)) {
1814 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1815 (1<<SNDRV_TIMER_EVENT_TICK)|
1816 (1<<SNDRV_TIMER_EVENT_START)|
1817 (1<<SNDRV_TIMER_EVENT_STOP)|
1818 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1819 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1820 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1821 (1<<SNDRV_TIMER_EVENT_RESUME)|
1822 (1<<SNDRV_TIMER_EVENT_MSTART)|
1823 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1824 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1825 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1826 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1827 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1831 snd_timer_stop(tu->timeri);
1832 spin_lock_irq(&t->lock);
1833 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1834 SNDRV_TIMER_IFLG_EXCLUSIVE|
1835 SNDRV_TIMER_IFLG_EARLY_EVENT);
1836 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1837 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1838 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1839 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1840 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1841 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1842 spin_unlock_irq(&t->lock);
1843 if (params.queue_size > 0 &&
1844 (unsigned int)tu->queue_size != params.queue_size) {
1845 err = realloc_user_queue(tu, params.queue_size);
1849 spin_lock_irq(&tu->qlock);
1850 tu->qhead = tu->qtail = tu->qused = 0;
1851 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1853 struct snd_timer_tread tread;
1854 memset(&tread, 0, sizeof(tread));
1855 tread.event = SNDRV_TIMER_EVENT_EARLY;
1856 tread.tstamp.tv_sec = 0;
1857 tread.tstamp.tv_nsec = 0;
1859 snd_timer_user_append_to_tqueue(tu, &tread);
1861 struct snd_timer_read *r = &tu->queue[0];
1868 tu->filter = params.filter;
1869 tu->ticks = params.ticks;
1870 spin_unlock_irq(&tu->qlock);
1873 if (copy_to_user(_params, ¶ms, sizeof(params)))
1878 static int snd_timer_user_status(struct file *file,
1879 struct snd_timer_status __user *_status)
1881 struct snd_timer_user *tu;
1882 struct snd_timer_status status;
1884 tu = file->private_data;
1887 memset(&status, 0, sizeof(status));
1888 status.tstamp = tu->tstamp;
1889 status.resolution = snd_timer_resolution(tu->timeri);
1890 status.lost = tu->timeri->lost;
1891 status.overrun = tu->overrun;
1892 spin_lock_irq(&tu->qlock);
1893 status.queue = tu->qused;
1894 spin_unlock_irq(&tu->qlock);
1895 if (copy_to_user(_status, &status, sizeof(status)))
1900 static int snd_timer_user_start(struct file *file)
1903 struct snd_timer_user *tu;
1905 tu = file->private_data;
1908 snd_timer_stop(tu->timeri);
1909 tu->timeri->lost = 0;
1910 tu->last_resolution = 0;
1911 err = snd_timer_start(tu->timeri, tu->ticks);
1917 static int snd_timer_user_stop(struct file *file)
1920 struct snd_timer_user *tu;
1922 tu = file->private_data;
1925 err = snd_timer_stop(tu->timeri);
1931 static int snd_timer_user_continue(struct file *file)
1934 struct snd_timer_user *tu;
1936 tu = file->private_data;
1939 /* start timer instead of continue if it's not used before */
1940 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1941 return snd_timer_user_start(file);
1942 tu->timeri->lost = 0;
1943 err = snd_timer_continue(tu->timeri);
1949 static int snd_timer_user_pause(struct file *file)
1952 struct snd_timer_user *tu;
1954 tu = file->private_data;
1957 err = snd_timer_pause(tu->timeri);
1964 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1965 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1966 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1967 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1970 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1973 struct snd_timer_user *tu;
1974 void __user *argp = (void __user *)arg;
1975 int __user *p = argp;
1977 tu = file->private_data;
1979 case SNDRV_TIMER_IOCTL_PVERSION:
1980 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1981 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1982 return snd_timer_user_next_device(argp);
1983 case SNDRV_TIMER_IOCTL_TREAD:
1985 int xarg, old_tread;
1987 if (tu->timeri) /* too late */
1989 if (get_user(xarg, p))
1991 old_tread = tu->tread;
1992 tu->tread = xarg ? 1 : 0;
1993 if (tu->tread != old_tread &&
1994 realloc_user_queue(tu, tu->queue_size) < 0) {
1995 tu->tread = old_tread;
2000 case SNDRV_TIMER_IOCTL_GINFO:
2001 return snd_timer_user_ginfo(file, argp);
2002 case SNDRV_TIMER_IOCTL_GPARAMS:
2003 return snd_timer_user_gparams(file, argp);
2004 case SNDRV_TIMER_IOCTL_GSTATUS:
2005 return snd_timer_user_gstatus(file, argp);
2006 case SNDRV_TIMER_IOCTL_SELECT:
2007 return snd_timer_user_tselect(file, argp);
2008 case SNDRV_TIMER_IOCTL_INFO:
2009 return snd_timer_user_info(file, argp);
2010 case SNDRV_TIMER_IOCTL_PARAMS:
2011 return snd_timer_user_params(file, argp);
2012 case SNDRV_TIMER_IOCTL_STATUS:
2013 return snd_timer_user_status(file, argp);
2014 case SNDRV_TIMER_IOCTL_START:
2015 case SNDRV_TIMER_IOCTL_START_OLD:
2016 return snd_timer_user_start(file);
2017 case SNDRV_TIMER_IOCTL_STOP:
2018 case SNDRV_TIMER_IOCTL_STOP_OLD:
2019 return snd_timer_user_stop(file);
2020 case SNDRV_TIMER_IOCTL_CONTINUE:
2021 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
2022 return snd_timer_user_continue(file);
2023 case SNDRV_TIMER_IOCTL_PAUSE:
2024 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
2025 return snd_timer_user_pause(file);
2030 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
2033 struct snd_timer_user *tu = file->private_data;
2036 mutex_lock(&tu->ioctl_lock);
2037 ret = __snd_timer_user_ioctl(file, cmd, arg);
2038 mutex_unlock(&tu->ioctl_lock);
2042 static int snd_timer_user_fasync(int fd, struct file * file, int on)
2044 struct snd_timer_user *tu;
2046 tu = file->private_data;
2047 return fasync_helper(fd, file, on, &tu->fasync);
2050 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2051 size_t count, loff_t *offset)
2053 struct snd_timer_user *tu;
2054 long result = 0, unit;
2058 tu = file->private_data;
2059 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
2060 mutex_lock(&tu->ioctl_lock);
2061 spin_lock_irq(&tu->qlock);
2062 while ((long)count - result >= unit) {
2063 while (!tu->qused) {
2064 wait_queue_entry_t wait;
2066 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2071 set_current_state(TASK_INTERRUPTIBLE);
2072 init_waitqueue_entry(&wait, current);
2073 add_wait_queue(&tu->qchange_sleep, &wait);
2075 spin_unlock_irq(&tu->qlock);
2076 mutex_unlock(&tu->ioctl_lock);
2078 mutex_lock(&tu->ioctl_lock);
2079 spin_lock_irq(&tu->qlock);
2081 remove_wait_queue(&tu->qchange_sleep, &wait);
2083 if (tu->disconnected) {
2087 if (signal_pending(current)) {
2093 qhead = tu->qhead++;
2094 tu->qhead %= tu->queue_size;
2096 spin_unlock_irq(&tu->qlock);
2099 if (copy_to_user(buffer, &tu->tqueue[qhead],
2100 sizeof(struct snd_timer_tread)))
2103 if (copy_to_user(buffer, &tu->queue[qhead],
2104 sizeof(struct snd_timer_read)))
2108 spin_lock_irq(&tu->qlock);
2115 spin_unlock_irq(&tu->qlock);
2116 mutex_unlock(&tu->ioctl_lock);
2117 return result > 0 ? result : err;
2120 static __poll_t snd_timer_user_poll(struct file *file, poll_table * wait)
2123 struct snd_timer_user *tu;
2125 tu = file->private_data;
2127 poll_wait(file, &tu->qchange_sleep, wait);
2130 spin_lock_irq(&tu->qlock);
2132 mask |= EPOLLIN | EPOLLRDNORM;
2133 if (tu->disconnected)
2135 spin_unlock_irq(&tu->qlock);
2140 #ifdef CONFIG_COMPAT
2141 #include "timer_compat.c"
2143 #define snd_timer_user_ioctl_compat NULL
2146 static const struct file_operations snd_timer_f_ops =
2148 .owner = THIS_MODULE,
2149 .read = snd_timer_user_read,
2150 .open = snd_timer_user_open,
2151 .release = snd_timer_user_release,
2152 .llseek = no_llseek,
2153 .poll = snd_timer_user_poll,
2154 .unlocked_ioctl = snd_timer_user_ioctl,
2155 .compat_ioctl = snd_timer_user_ioctl_compat,
2156 .fasync = snd_timer_user_fasync,
2159 /* unregister the system timer */
2160 static void snd_timer_free_all(void)
2162 struct snd_timer *timer, *n;
2164 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2165 snd_timer_free(timer);
2168 static struct device timer_dev;
2174 static int __init alsa_timer_init(void)
2178 snd_device_initialize(&timer_dev, NULL);
2179 dev_set_name(&timer_dev, "timer");
2181 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2182 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2186 err = snd_timer_register_system();
2188 pr_err("ALSA: unable to register system timer (%i)\n", err);
2192 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2193 &snd_timer_f_ops, NULL, &timer_dev);
2195 pr_err("ALSA: unable to register timer device (%i)\n", err);
2196 snd_timer_free_all();
2200 snd_timer_proc_init();
2204 put_device(&timer_dev);
2208 static void __exit alsa_timer_exit(void)
2210 snd_unregister_device(&timer_dev);
2211 snd_timer_free_all();
2212 put_device(&timer_dev);
2213 snd_timer_proc_done();
2214 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2215 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2219 module_init(alsa_timer_init)
2220 module_exit(alsa_timer_exit)