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);
166 /* move the slave if it belongs to the master; return 1 if match */
167 static int check_matching_master_slave(struct snd_timer_instance *master,
168 struct snd_timer_instance *slave)
170 if (slave->slave_class != master->slave_class ||
171 slave->slave_id != master->slave_id)
173 if (master->timer->num_instances >= master->timer->max_instances)
175 list_move_tail(&slave->open_list, &master->slave_list_head);
176 master->timer->num_instances++;
177 spin_lock_irq(&slave_active_lock);
178 spin_lock(&master->timer->lock);
179 slave->master = master;
180 slave->timer = master->timer;
181 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
182 list_add_tail(&slave->active_list, &master->slave_active_head);
183 spin_unlock(&master->timer->lock);
184 spin_unlock_irq(&slave_active_lock);
189 * look for a master instance matching with the slave id of the given slave.
190 * when found, relink the open_link of the slave.
192 * call this with register_mutex down.
194 static int snd_timer_check_slave(struct snd_timer_instance *slave)
196 struct snd_timer *timer;
197 struct snd_timer_instance *master;
200 /* FIXME: it's really dumb to look up all entries.. */
201 list_for_each_entry(timer, &snd_timer_list, device_list) {
202 list_for_each_entry(master, &timer->open_list_head, open_list) {
203 err = check_matching_master_slave(master, slave);
204 if (err != 0) /* match found or error */
209 return err < 0 ? err : 0;
213 * look for slave instances matching with the slave id of the given master.
214 * when found, relink the open_link of slaves.
216 * call this with register_mutex down.
218 static int snd_timer_check_master(struct snd_timer_instance *master)
220 struct snd_timer_instance *slave, *tmp;
223 /* check all pending slaves */
224 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
225 err = check_matching_master_slave(master, slave);
229 return err < 0 ? err : 0;
232 static void 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 void 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);
420 * close a timer instance
422 void snd_timer_close(struct snd_timer_instance *timeri)
424 struct device *card_dev_to_put = NULL;
426 if (snd_BUG_ON(!timeri))
429 mutex_lock(®ister_mutex);
430 snd_timer_close_locked(timeri, &card_dev_to_put);
431 mutex_unlock(®ister_mutex);
432 /* put_device() is called after unlock for avoiding deadlock */
434 put_device(card_dev_to_put);
436 EXPORT_SYMBOL(snd_timer_close);
438 static unsigned long snd_timer_hw_resolution(struct snd_timer *timer)
440 if (timer->hw.c_resolution)
441 return timer->hw.c_resolution(timer);
443 return timer->hw.resolution;
446 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
448 struct snd_timer * timer;
449 unsigned long ret = 0;
454 timer = timeri->timer;
456 spin_lock_irqsave(&timer->lock, flags);
457 ret = snd_timer_hw_resolution(timer);
458 spin_unlock_irqrestore(&timer->lock, flags);
462 EXPORT_SYMBOL(snd_timer_resolution);
464 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
466 struct snd_timer *timer = ti->timer;
467 unsigned long resolution = 0;
468 struct snd_timer_instance *ts;
469 struct timespec tstamp;
471 if (timer_tstamp_monotonic)
472 ktime_get_ts(&tstamp);
474 getnstimeofday(&tstamp);
475 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
476 event > SNDRV_TIMER_EVENT_PAUSE))
479 (event == SNDRV_TIMER_EVENT_START ||
480 event == SNDRV_TIMER_EVENT_CONTINUE))
481 resolution = snd_timer_hw_resolution(timer);
483 ti->ccallback(ti, event, &tstamp, resolution);
484 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
488 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
490 list_for_each_entry(ts, &ti->slave_active_head, active_list)
492 ts->ccallback(ts, event + 100, &tstamp, resolution);
495 /* start/continue a master timer */
496 static int snd_timer_start1(struct snd_timer_instance *timeri,
497 bool start, unsigned long ticks)
499 struct snd_timer *timer;
503 timer = timeri->timer;
507 spin_lock_irqsave(&timer->lock, flags);
508 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
512 if (timer->card && timer->card->shutdown) {
516 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
517 SNDRV_TIMER_IFLG_START)) {
523 timeri->ticks = timeri->cticks = ticks;
524 else if (!timeri->cticks)
528 list_move_tail(&timeri->active_list, &timer->active_list_head);
529 if (timer->running) {
530 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
532 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
533 timeri->flags |= SNDRV_TIMER_IFLG_START;
534 result = 1; /* delayed start */
537 timer->sticks = ticks;
538 timer->hw.start(timer);
541 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
544 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
545 SNDRV_TIMER_EVENT_CONTINUE);
547 spin_unlock_irqrestore(&timer->lock, flags);
551 /* start/continue a slave timer */
552 static int snd_timer_start_slave(struct snd_timer_instance *timeri,
558 spin_lock_irqsave(&slave_active_lock, flags);
559 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
563 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
567 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
568 if (timeri->master && timeri->timer) {
569 spin_lock(&timeri->timer->lock);
570 list_add_tail(&timeri->active_list,
571 &timeri->master->slave_active_head);
572 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
573 SNDRV_TIMER_EVENT_CONTINUE);
574 spin_unlock(&timeri->timer->lock);
576 err = 1; /* delayed start */
578 spin_unlock_irqrestore(&slave_active_lock, flags);
582 /* stop/pause a master timer */
583 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
585 struct snd_timer *timer;
589 timer = timeri->timer;
592 spin_lock_irqsave(&timer->lock, flags);
593 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
594 SNDRV_TIMER_IFLG_START))) {
598 list_del_init(&timeri->ack_list);
599 list_del_init(&timeri->active_list);
600 if (timer->card && timer->card->shutdown)
603 timeri->cticks = timeri->ticks;
606 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
607 !(--timer->running)) {
608 timer->hw.stop(timer);
609 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
610 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
611 snd_timer_reschedule(timer, 0);
612 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
613 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
614 timer->hw.start(timer);
618 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
620 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
622 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
623 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
624 SNDRV_TIMER_EVENT_PAUSE);
626 spin_unlock_irqrestore(&timer->lock, flags);
630 /* stop/pause a slave timer */
631 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
635 spin_lock_irqsave(&slave_active_lock, flags);
636 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
637 spin_unlock_irqrestore(&slave_active_lock, flags);
640 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
642 spin_lock(&timeri->timer->lock);
643 list_del_init(&timeri->ack_list);
644 list_del_init(&timeri->active_list);
645 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
646 SNDRV_TIMER_EVENT_PAUSE);
647 spin_unlock(&timeri->timer->lock);
649 spin_unlock_irqrestore(&slave_active_lock, flags);
654 * start the timer instance
656 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
658 if (timeri == NULL || ticks < 1)
660 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
661 return snd_timer_start_slave(timeri, true);
663 return snd_timer_start1(timeri, true, ticks);
665 EXPORT_SYMBOL(snd_timer_start);
668 * stop the timer instance.
670 * do not call this from the timer callback!
672 int snd_timer_stop(struct snd_timer_instance *timeri)
674 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
675 return snd_timer_stop_slave(timeri, true);
677 return snd_timer_stop1(timeri, true);
679 EXPORT_SYMBOL(snd_timer_stop);
682 * start again.. the tick is kept.
684 int snd_timer_continue(struct snd_timer_instance *timeri)
686 /* timer can continue only after pause */
687 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
690 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
691 return snd_timer_start_slave(timeri, false);
693 return snd_timer_start1(timeri, false, 0);
695 EXPORT_SYMBOL(snd_timer_continue);
698 * pause.. remember the ticks left
700 int snd_timer_pause(struct snd_timer_instance * timeri)
702 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
703 return snd_timer_stop_slave(timeri, false);
705 return snd_timer_stop1(timeri, false);
707 EXPORT_SYMBOL(snd_timer_pause);
710 * reschedule the timer
712 * start pending instances and check the scheduling ticks.
713 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
715 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
717 struct snd_timer_instance *ti;
718 unsigned long ticks = ~0UL;
720 list_for_each_entry(ti, &timer->active_list_head, active_list) {
721 if (ti->flags & SNDRV_TIMER_IFLG_START) {
722 ti->flags &= ~SNDRV_TIMER_IFLG_START;
723 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
726 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
727 if (ticks > ti->cticks)
732 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
735 if (ticks > timer->hw.ticks)
736 ticks = timer->hw.ticks;
737 if (ticks_left != ticks)
738 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
739 timer->sticks = ticks;
742 /* call callbacks in timer ack list */
743 static void snd_timer_process_callbacks(struct snd_timer *timer,
744 struct list_head *head)
746 struct snd_timer_instance *ti;
747 unsigned long resolution, ticks;
749 while (!list_empty(head)) {
750 ti = list_first_entry(head, struct snd_timer_instance,
753 /* remove from ack_list and make empty */
754 list_del_init(&ti->ack_list);
756 if (!(ti->flags & SNDRV_TIMER_IFLG_DEAD)) {
759 resolution = ti->resolution;
760 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
761 spin_unlock(&timer->lock);
763 ti->callback(ti, resolution, ticks);
764 spin_lock(&timer->lock);
765 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
770 /* clear pending instances from ack list */
771 static void snd_timer_clear_callbacks(struct snd_timer *timer,
772 struct list_head *head)
776 spin_lock_irqsave(&timer->lock, flags);
777 while (!list_empty(head))
778 list_del_init(head->next);
779 spin_unlock_irqrestore(&timer->lock, flags);
786 static void snd_timer_tasklet(unsigned long arg)
788 struct snd_timer *timer = (struct snd_timer *) arg;
791 if (timer->card && timer->card->shutdown) {
792 snd_timer_clear_callbacks(timer, &timer->sack_list_head);
796 spin_lock_irqsave(&timer->lock, flags);
797 snd_timer_process_callbacks(timer, &timer->sack_list_head);
798 spin_unlock_irqrestore(&timer->lock, flags);
804 * ticks_left is usually equal to timer->sticks.
807 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
809 struct snd_timer_instance *ti, *ts, *tmp;
810 unsigned long resolution;
811 struct list_head *ack_list_head;
818 if (timer->card && timer->card->shutdown) {
819 snd_timer_clear_callbacks(timer, &timer->ack_list_head);
823 spin_lock_irqsave(&timer->lock, flags);
825 /* remember the current resolution */
826 resolution = snd_timer_hw_resolution(timer);
828 /* loop for all active instances
829 * Here we cannot use list_for_each_entry because the active_list of a
830 * processed instance is relinked to done_list_head before the callback
833 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
835 if (ti->flags & SNDRV_TIMER_IFLG_DEAD)
837 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
839 ti->pticks += ticks_left;
840 ti->resolution = resolution;
841 if (ti->cticks < ticks_left)
844 ti->cticks -= ticks_left;
845 if (ti->cticks) /* not expired */
847 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
848 ti->cticks = ti->ticks;
850 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
852 list_del_init(&ti->active_list);
854 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
855 (ti->flags & SNDRV_TIMER_IFLG_FAST))
856 ack_list_head = &timer->ack_list_head;
858 ack_list_head = &timer->sack_list_head;
859 if (list_empty(&ti->ack_list))
860 list_add_tail(&ti->ack_list, ack_list_head);
861 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
862 ts->pticks = ti->pticks;
863 ts->resolution = resolution;
864 if (list_empty(&ts->ack_list))
865 list_add_tail(&ts->ack_list, ack_list_head);
868 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
869 snd_timer_reschedule(timer, timer->sticks);
870 if (timer->running) {
871 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
872 timer->hw.stop(timer);
873 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
875 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
876 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
878 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
879 timer->hw.start(timer);
882 timer->hw.stop(timer);
885 /* now process all fast callbacks */
886 snd_timer_process_callbacks(timer, &timer->ack_list_head);
888 /* do we have any slow callbacks? */
889 use_tasklet = !list_empty(&timer->sack_list_head);
890 spin_unlock_irqrestore(&timer->lock, flags);
893 tasklet_schedule(&timer->task_queue);
895 EXPORT_SYMBOL(snd_timer_interrupt);
901 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
902 struct snd_timer **rtimer)
904 struct snd_timer *timer;
906 static struct snd_device_ops ops = {
907 .dev_free = snd_timer_dev_free,
908 .dev_register = snd_timer_dev_register,
909 .dev_disconnect = snd_timer_dev_disconnect,
912 if (snd_BUG_ON(!tid))
914 if (tid->dev_class == SNDRV_TIMER_CLASS_CARD ||
915 tid->dev_class == SNDRV_TIMER_CLASS_PCM) {
921 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
924 timer->tmr_class = tid->dev_class;
926 timer->tmr_device = tid->device;
927 timer->tmr_subdevice = tid->subdevice;
929 strlcpy(timer->id, id, sizeof(timer->id));
931 INIT_LIST_HEAD(&timer->device_list);
932 INIT_LIST_HEAD(&timer->open_list_head);
933 INIT_LIST_HEAD(&timer->active_list_head);
934 INIT_LIST_HEAD(&timer->ack_list_head);
935 INIT_LIST_HEAD(&timer->sack_list_head);
936 spin_lock_init(&timer->lock);
937 tasklet_init(&timer->task_queue, snd_timer_tasklet,
938 (unsigned long)timer);
939 timer->max_instances = 1000; /* default limit per timer */
941 timer->module = card->module;
942 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
944 snd_timer_free(timer);
952 EXPORT_SYMBOL(snd_timer_new);
954 static int snd_timer_free(struct snd_timer *timer)
959 mutex_lock(®ister_mutex);
960 if (! list_empty(&timer->open_list_head)) {
961 struct list_head *p, *n;
962 struct snd_timer_instance *ti;
963 pr_warn("ALSA: timer %p is busy?\n", timer);
964 list_for_each_safe(p, n, &timer->open_list_head) {
966 ti = list_entry(p, struct snd_timer_instance, open_list);
970 list_del(&timer->device_list);
971 mutex_unlock(®ister_mutex);
973 if (timer->private_free)
974 timer->private_free(timer);
979 static int snd_timer_dev_free(struct snd_device *device)
981 struct snd_timer *timer = device->device_data;
982 return snd_timer_free(timer);
985 static int snd_timer_dev_register(struct snd_device *dev)
987 struct snd_timer *timer = dev->device_data;
988 struct snd_timer *timer1;
990 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
992 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
993 !timer->hw.resolution && timer->hw.c_resolution == NULL)
996 mutex_lock(®ister_mutex);
997 list_for_each_entry(timer1, &snd_timer_list, device_list) {
998 if (timer1->tmr_class > timer->tmr_class)
1000 if (timer1->tmr_class < timer->tmr_class)
1002 if (timer1->card && timer->card) {
1003 if (timer1->card->number > timer->card->number)
1005 if (timer1->card->number < timer->card->number)
1008 if (timer1->tmr_device > timer->tmr_device)
1010 if (timer1->tmr_device < timer->tmr_device)
1012 if (timer1->tmr_subdevice > timer->tmr_subdevice)
1014 if (timer1->tmr_subdevice < timer->tmr_subdevice)
1017 mutex_unlock(®ister_mutex);
1020 list_add_tail(&timer->device_list, &timer1->device_list);
1021 mutex_unlock(®ister_mutex);
1025 static int snd_timer_dev_disconnect(struct snd_device *device)
1027 struct snd_timer *timer = device->device_data;
1028 struct snd_timer_instance *ti;
1030 mutex_lock(®ister_mutex);
1031 list_del_init(&timer->device_list);
1032 /* wake up pending sleepers */
1033 list_for_each_entry(ti, &timer->open_list_head, open_list) {
1037 mutex_unlock(®ister_mutex);
1041 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
1043 unsigned long flags;
1044 unsigned long resolution = 0;
1045 struct snd_timer_instance *ti, *ts;
1047 if (timer->card && timer->card->shutdown)
1049 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1051 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1052 event > SNDRV_TIMER_EVENT_MRESUME))
1054 spin_lock_irqsave(&timer->lock, flags);
1055 if (event == SNDRV_TIMER_EVENT_MSTART ||
1056 event == SNDRV_TIMER_EVENT_MCONTINUE ||
1057 event == SNDRV_TIMER_EVENT_MRESUME)
1058 resolution = snd_timer_hw_resolution(timer);
1059 list_for_each_entry(ti, &timer->active_list_head, active_list) {
1061 ti->ccallback(ti, event, tstamp, resolution);
1062 list_for_each_entry(ts, &ti->slave_active_head, active_list)
1064 ts->ccallback(ts, event, tstamp, resolution);
1066 spin_unlock_irqrestore(&timer->lock, flags);
1068 EXPORT_SYMBOL(snd_timer_notify);
1071 * exported functions for global timers
1073 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1075 struct snd_timer_id tid;
1077 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1078 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1080 tid.device = device;
1082 return snd_timer_new(NULL, id, &tid, rtimer);
1084 EXPORT_SYMBOL(snd_timer_global_new);
1086 int snd_timer_global_free(struct snd_timer *timer)
1088 return snd_timer_free(timer);
1090 EXPORT_SYMBOL(snd_timer_global_free);
1092 int snd_timer_global_register(struct snd_timer *timer)
1094 struct snd_device dev;
1096 memset(&dev, 0, sizeof(dev));
1097 dev.device_data = timer;
1098 return snd_timer_dev_register(&dev);
1100 EXPORT_SYMBOL(snd_timer_global_register);
1106 struct snd_timer_system_private {
1107 struct timer_list tlist;
1108 struct snd_timer *snd_timer;
1109 unsigned long last_expires;
1110 unsigned long last_jiffies;
1111 unsigned long correction;
1114 static void snd_timer_s_function(struct timer_list *t)
1116 struct snd_timer_system_private *priv = from_timer(priv, t,
1118 struct snd_timer *timer = priv->snd_timer;
1119 unsigned long jiff = jiffies;
1120 if (time_after(jiff, priv->last_expires))
1121 priv->correction += (long)jiff - (long)priv->last_expires;
1122 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1125 static int snd_timer_s_start(struct snd_timer * timer)
1127 struct snd_timer_system_private *priv;
1128 unsigned long njiff;
1130 priv = (struct snd_timer_system_private *) timer->private_data;
1131 njiff = (priv->last_jiffies = jiffies);
1132 if (priv->correction > timer->sticks - 1) {
1133 priv->correction -= timer->sticks - 1;
1136 njiff += timer->sticks - priv->correction;
1137 priv->correction = 0;
1139 priv->last_expires = njiff;
1140 mod_timer(&priv->tlist, njiff);
1144 static int snd_timer_s_stop(struct snd_timer * timer)
1146 struct snd_timer_system_private *priv;
1149 priv = (struct snd_timer_system_private *) timer->private_data;
1150 del_timer(&priv->tlist);
1152 if (time_before(jiff, priv->last_expires))
1153 timer->sticks = priv->last_expires - jiff;
1156 priv->correction = 0;
1160 static int snd_timer_s_close(struct snd_timer *timer)
1162 struct snd_timer_system_private *priv;
1164 priv = (struct snd_timer_system_private *)timer->private_data;
1165 del_timer_sync(&priv->tlist);
1169 static struct snd_timer_hardware snd_timer_system =
1171 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1172 .resolution = 1000000000L / HZ,
1174 .close = snd_timer_s_close,
1175 .start = snd_timer_s_start,
1176 .stop = snd_timer_s_stop
1179 static void snd_timer_free_system(struct snd_timer *timer)
1181 kfree(timer->private_data);
1184 static int snd_timer_register_system(void)
1186 struct snd_timer *timer;
1187 struct snd_timer_system_private *priv;
1190 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1193 strcpy(timer->name, "system timer");
1194 timer->hw = snd_timer_system;
1195 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1197 snd_timer_free(timer);
1200 priv->snd_timer = timer;
1201 timer_setup(&priv->tlist, snd_timer_s_function, 0);
1202 timer->private_data = priv;
1203 timer->private_free = snd_timer_free_system;
1204 return snd_timer_global_register(timer);
1207 #ifdef CONFIG_SND_PROC_FS
1212 static void snd_timer_proc_read(struct snd_info_entry *entry,
1213 struct snd_info_buffer *buffer)
1215 struct snd_timer *timer;
1216 struct snd_timer_instance *ti;
1218 mutex_lock(®ister_mutex);
1219 list_for_each_entry(timer, &snd_timer_list, device_list) {
1220 if (timer->card && timer->card->shutdown)
1222 switch (timer->tmr_class) {
1223 case SNDRV_TIMER_CLASS_GLOBAL:
1224 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1226 case SNDRV_TIMER_CLASS_CARD:
1227 snd_iprintf(buffer, "C%i-%i: ",
1228 timer->card->number, timer->tmr_device);
1230 case SNDRV_TIMER_CLASS_PCM:
1231 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1232 timer->tmr_device, timer->tmr_subdevice);
1235 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1236 timer->card ? timer->card->number : -1,
1237 timer->tmr_device, timer->tmr_subdevice);
1239 snd_iprintf(buffer, "%s :", timer->name);
1240 if (timer->hw.resolution)
1241 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1242 timer->hw.resolution / 1000,
1243 timer->hw.resolution % 1000,
1245 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1246 snd_iprintf(buffer, " SLAVE");
1247 snd_iprintf(buffer, "\n");
1248 list_for_each_entry(ti, &timer->open_list_head, open_list)
1249 snd_iprintf(buffer, " Client %s : %s\n",
1250 ti->owner ? ti->owner : "unknown",
1251 ti->flags & (SNDRV_TIMER_IFLG_START |
1252 SNDRV_TIMER_IFLG_RUNNING)
1253 ? "running" : "stopped");
1255 mutex_unlock(®ister_mutex);
1258 static struct snd_info_entry *snd_timer_proc_entry;
1260 static void __init snd_timer_proc_init(void)
1262 struct snd_info_entry *entry;
1264 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1265 if (entry != NULL) {
1266 entry->c.text.read = snd_timer_proc_read;
1267 if (snd_info_register(entry) < 0) {
1268 snd_info_free_entry(entry);
1272 snd_timer_proc_entry = entry;
1275 static void __exit snd_timer_proc_done(void)
1277 snd_info_free_entry(snd_timer_proc_entry);
1279 #else /* !CONFIG_SND_PROC_FS */
1280 #define snd_timer_proc_init()
1281 #define snd_timer_proc_done()
1285 * USER SPACE interface
1288 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1289 unsigned long resolution,
1290 unsigned long ticks)
1292 struct snd_timer_user *tu = timeri->callback_data;
1293 struct snd_timer_read *r;
1296 spin_lock(&tu->qlock);
1297 if (tu->qused > 0) {
1298 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1299 r = &tu->queue[prev];
1300 if (r->resolution == resolution) {
1305 if (tu->qused >= tu->queue_size) {
1308 r = &tu->queue[tu->qtail++];
1309 tu->qtail %= tu->queue_size;
1310 r->resolution = resolution;
1315 spin_unlock(&tu->qlock);
1316 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1317 wake_up(&tu->qchange_sleep);
1320 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1321 struct snd_timer_tread *tread)
1323 if (tu->qused >= tu->queue_size) {
1326 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1327 tu->qtail %= tu->queue_size;
1332 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1334 struct timespec *tstamp,
1335 unsigned long resolution)
1337 struct snd_timer_user *tu = timeri->callback_data;
1338 struct snd_timer_tread r1;
1339 unsigned long flags;
1341 if (event >= SNDRV_TIMER_EVENT_START &&
1342 event <= SNDRV_TIMER_EVENT_PAUSE)
1343 tu->tstamp = *tstamp;
1344 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1346 memset(&r1, 0, sizeof(r1));
1348 r1.tstamp = *tstamp;
1349 r1.val = resolution;
1350 spin_lock_irqsave(&tu->qlock, flags);
1351 snd_timer_user_append_to_tqueue(tu, &r1);
1352 spin_unlock_irqrestore(&tu->qlock, flags);
1353 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1354 wake_up(&tu->qchange_sleep);
1357 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1359 struct snd_timer_user *tu = timeri->callback_data;
1361 tu->disconnected = true;
1362 wake_up(&tu->qchange_sleep);
1365 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1366 unsigned long resolution,
1367 unsigned long ticks)
1369 struct snd_timer_user *tu = timeri->callback_data;
1370 struct snd_timer_tread *r, r1;
1371 struct timespec tstamp;
1372 int prev, append = 0;
1374 memset(&r1, 0, sizeof(r1));
1375 memset(&tstamp, 0, sizeof(tstamp));
1376 spin_lock(&tu->qlock);
1377 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1378 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1379 spin_unlock(&tu->qlock);
1382 if (tu->last_resolution != resolution || ticks > 0) {
1383 if (timer_tstamp_monotonic)
1384 ktime_get_ts(&tstamp);
1386 getnstimeofday(&tstamp);
1388 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1389 tu->last_resolution != resolution) {
1390 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1392 r1.val = resolution;
1393 snd_timer_user_append_to_tqueue(tu, &r1);
1394 tu->last_resolution = resolution;
1397 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1401 if (tu->qused > 0) {
1402 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1403 r = &tu->tqueue[prev];
1404 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1411 r1.event = SNDRV_TIMER_EVENT_TICK;
1414 snd_timer_user_append_to_tqueue(tu, &r1);
1417 spin_unlock(&tu->qlock);
1420 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1421 wake_up(&tu->qchange_sleep);
1424 static int realloc_user_queue(struct snd_timer_user *tu, int size)
1426 struct snd_timer_read *queue = NULL;
1427 struct snd_timer_tread *tqueue = NULL;
1430 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
1434 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
1439 spin_lock_irq(&tu->qlock);
1442 tu->queue_size = size;
1444 tu->tqueue = tqueue;
1445 tu->qhead = tu->qtail = tu->qused = 0;
1446 spin_unlock_irq(&tu->qlock);
1451 static int snd_timer_user_open(struct inode *inode, struct file *file)
1453 struct snd_timer_user *tu;
1456 err = stream_open(inode, file);
1460 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1463 spin_lock_init(&tu->qlock);
1464 init_waitqueue_head(&tu->qchange_sleep);
1465 mutex_init(&tu->ioctl_lock);
1467 if (realloc_user_queue(tu, 128) < 0) {
1471 file->private_data = tu;
1475 static int snd_timer_user_release(struct inode *inode, struct file *file)
1477 struct snd_timer_user *tu;
1479 if (file->private_data) {
1480 tu = file->private_data;
1481 file->private_data = NULL;
1482 mutex_lock(&tu->ioctl_lock);
1484 snd_timer_close(tu->timeri);
1485 mutex_unlock(&tu->ioctl_lock);
1493 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1495 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1496 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1502 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1504 id->dev_class = timer->tmr_class;
1505 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1506 id->card = timer->card ? timer->card->number : -1;
1507 id->device = timer->tmr_device;
1508 id->subdevice = timer->tmr_subdevice;
1511 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1513 struct snd_timer_id id;
1514 struct snd_timer *timer;
1515 struct list_head *p;
1517 if (copy_from_user(&id, _tid, sizeof(id)))
1519 mutex_lock(®ister_mutex);
1520 if (id.dev_class < 0) { /* first item */
1521 if (list_empty(&snd_timer_list))
1522 snd_timer_user_zero_id(&id);
1524 timer = list_entry(snd_timer_list.next,
1525 struct snd_timer, device_list);
1526 snd_timer_user_copy_id(&id, timer);
1529 switch (id.dev_class) {
1530 case SNDRV_TIMER_CLASS_GLOBAL:
1531 id.device = id.device < 0 ? 0 : id.device + 1;
1532 list_for_each(p, &snd_timer_list) {
1533 timer = list_entry(p, struct snd_timer, device_list);
1534 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1535 snd_timer_user_copy_id(&id, timer);
1538 if (timer->tmr_device >= id.device) {
1539 snd_timer_user_copy_id(&id, timer);
1543 if (p == &snd_timer_list)
1544 snd_timer_user_zero_id(&id);
1546 case SNDRV_TIMER_CLASS_CARD:
1547 case SNDRV_TIMER_CLASS_PCM:
1551 if (id.device < 0) {
1554 if (id.subdevice < 0)
1556 else if (id.subdevice < INT_MAX)
1560 list_for_each(p, &snd_timer_list) {
1561 timer = list_entry(p, struct snd_timer, device_list);
1562 if (timer->tmr_class > id.dev_class) {
1563 snd_timer_user_copy_id(&id, timer);
1566 if (timer->tmr_class < id.dev_class)
1568 if (timer->card->number > id.card) {
1569 snd_timer_user_copy_id(&id, timer);
1572 if (timer->card->number < id.card)
1574 if (timer->tmr_device > id.device) {
1575 snd_timer_user_copy_id(&id, timer);
1578 if (timer->tmr_device < id.device)
1580 if (timer->tmr_subdevice > id.subdevice) {
1581 snd_timer_user_copy_id(&id, timer);
1584 if (timer->tmr_subdevice < id.subdevice)
1586 snd_timer_user_copy_id(&id, timer);
1589 if (p == &snd_timer_list)
1590 snd_timer_user_zero_id(&id);
1593 snd_timer_user_zero_id(&id);
1596 mutex_unlock(®ister_mutex);
1597 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1602 static int snd_timer_user_ginfo(struct file *file,
1603 struct snd_timer_ginfo __user *_ginfo)
1605 struct snd_timer_ginfo *ginfo;
1606 struct snd_timer_id tid;
1607 struct snd_timer *t;
1608 struct list_head *p;
1611 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1613 return PTR_ERR(ginfo);
1616 memset(ginfo, 0, sizeof(*ginfo));
1618 mutex_lock(®ister_mutex);
1619 t = snd_timer_find(&tid);
1621 ginfo->card = t->card ? t->card->number : -1;
1622 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1623 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1624 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1625 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1626 ginfo->resolution = t->hw.resolution;
1627 if (t->hw.resolution_min > 0) {
1628 ginfo->resolution_min = t->hw.resolution_min;
1629 ginfo->resolution_max = t->hw.resolution_max;
1631 list_for_each(p, &t->open_list_head) {
1637 mutex_unlock(®ister_mutex);
1638 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1644 static int timer_set_gparams(struct snd_timer_gparams *gparams)
1646 struct snd_timer *t;
1649 mutex_lock(®ister_mutex);
1650 t = snd_timer_find(&gparams->tid);
1655 if (!list_empty(&t->open_list_head)) {
1659 if (!t->hw.set_period) {
1663 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
1665 mutex_unlock(®ister_mutex);
1669 static int snd_timer_user_gparams(struct file *file,
1670 struct snd_timer_gparams __user *_gparams)
1672 struct snd_timer_gparams gparams;
1674 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1676 return timer_set_gparams(&gparams);
1679 static int snd_timer_user_gstatus(struct file *file,
1680 struct snd_timer_gstatus __user *_gstatus)
1682 struct snd_timer_gstatus gstatus;
1683 struct snd_timer_id tid;
1684 struct snd_timer *t;
1687 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1690 memset(&gstatus, 0, sizeof(gstatus));
1692 mutex_lock(®ister_mutex);
1693 t = snd_timer_find(&tid);
1695 spin_lock_irq(&t->lock);
1696 gstatus.resolution = snd_timer_hw_resolution(t);
1697 if (t->hw.precise_resolution) {
1698 t->hw.precise_resolution(t, &gstatus.resolution_num,
1699 &gstatus.resolution_den);
1701 gstatus.resolution_num = gstatus.resolution;
1702 gstatus.resolution_den = 1000000000uL;
1704 spin_unlock_irq(&t->lock);
1708 mutex_unlock(®ister_mutex);
1709 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1714 static int snd_timer_user_tselect(struct file *file,
1715 struct snd_timer_select __user *_tselect)
1717 struct snd_timer_user *tu;
1718 struct snd_timer_select tselect;
1722 tu = file->private_data;
1724 snd_timer_close(tu->timeri);
1727 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1731 sprintf(str, "application %i", current->pid);
1732 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1733 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1734 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1738 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1739 tu->timeri->callback = tu->tread
1740 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1741 tu->timeri->ccallback = snd_timer_user_ccallback;
1742 tu->timeri->callback_data = (void *)tu;
1743 tu->timeri->disconnect = snd_timer_user_disconnect;
1749 static int snd_timer_user_info(struct file *file,
1750 struct snd_timer_info __user *_info)
1752 struct snd_timer_user *tu;
1753 struct snd_timer_info *info;
1754 struct snd_timer *t;
1757 tu = file->private_data;
1760 t = tu->timeri->timer;
1764 info = kzalloc(sizeof(*info), GFP_KERNEL);
1767 info->card = t->card ? t->card->number : -1;
1768 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1769 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1770 strlcpy(info->id, t->id, sizeof(info->id));
1771 strlcpy(info->name, t->name, sizeof(info->name));
1772 info->resolution = t->hw.resolution;
1773 if (copy_to_user(_info, info, sizeof(*_info)))
1779 static int snd_timer_user_params(struct file *file,
1780 struct snd_timer_params __user *_params)
1782 struct snd_timer_user *tu;
1783 struct snd_timer_params params;
1784 struct snd_timer *t;
1787 tu = file->private_data;
1790 t = tu->timeri->timer;
1793 if (copy_from_user(¶ms, _params, sizeof(params)))
1795 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1798 if (params.ticks < 1) {
1803 /* Don't allow resolution less than 1ms */
1804 resolution = snd_timer_resolution(tu->timeri);
1805 resolution *= params.ticks;
1806 if (resolution < 1000000) {
1811 if (params.queue_size > 0 &&
1812 (params.queue_size < 32 || params.queue_size > 1024)) {
1816 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1817 (1<<SNDRV_TIMER_EVENT_TICK)|
1818 (1<<SNDRV_TIMER_EVENT_START)|
1819 (1<<SNDRV_TIMER_EVENT_STOP)|
1820 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1821 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1822 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1823 (1<<SNDRV_TIMER_EVENT_RESUME)|
1824 (1<<SNDRV_TIMER_EVENT_MSTART)|
1825 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1826 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1827 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1828 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1829 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1833 snd_timer_stop(tu->timeri);
1834 spin_lock_irq(&t->lock);
1835 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1836 SNDRV_TIMER_IFLG_EXCLUSIVE|
1837 SNDRV_TIMER_IFLG_EARLY_EVENT);
1838 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1839 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1840 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1841 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1842 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1843 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1844 spin_unlock_irq(&t->lock);
1845 if (params.queue_size > 0 &&
1846 (unsigned int)tu->queue_size != params.queue_size) {
1847 err = realloc_user_queue(tu, params.queue_size);
1851 spin_lock_irq(&tu->qlock);
1852 tu->qhead = tu->qtail = tu->qused = 0;
1853 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1855 struct snd_timer_tread tread;
1856 memset(&tread, 0, sizeof(tread));
1857 tread.event = SNDRV_TIMER_EVENT_EARLY;
1858 tread.tstamp.tv_sec = 0;
1859 tread.tstamp.tv_nsec = 0;
1861 snd_timer_user_append_to_tqueue(tu, &tread);
1863 struct snd_timer_read *r = &tu->queue[0];
1870 tu->filter = params.filter;
1871 tu->ticks = params.ticks;
1872 spin_unlock_irq(&tu->qlock);
1875 if (copy_to_user(_params, ¶ms, sizeof(params)))
1880 static int snd_timer_user_status(struct file *file,
1881 struct snd_timer_status __user *_status)
1883 struct snd_timer_user *tu;
1884 struct snd_timer_status status;
1886 tu = file->private_data;
1889 memset(&status, 0, sizeof(status));
1890 status.tstamp = tu->tstamp;
1891 status.resolution = snd_timer_resolution(tu->timeri);
1892 status.lost = tu->timeri->lost;
1893 status.overrun = tu->overrun;
1894 spin_lock_irq(&tu->qlock);
1895 status.queue = tu->qused;
1896 spin_unlock_irq(&tu->qlock);
1897 if (copy_to_user(_status, &status, sizeof(status)))
1902 static int snd_timer_user_start(struct file *file)
1905 struct snd_timer_user *tu;
1907 tu = file->private_data;
1910 snd_timer_stop(tu->timeri);
1911 tu->timeri->lost = 0;
1912 tu->last_resolution = 0;
1913 err = snd_timer_start(tu->timeri, tu->ticks);
1919 static int snd_timer_user_stop(struct file *file)
1922 struct snd_timer_user *tu;
1924 tu = file->private_data;
1927 err = snd_timer_stop(tu->timeri);
1933 static int snd_timer_user_continue(struct file *file)
1936 struct snd_timer_user *tu;
1938 tu = file->private_data;
1941 /* start timer instead of continue if it's not used before */
1942 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1943 return snd_timer_user_start(file);
1944 tu->timeri->lost = 0;
1945 err = snd_timer_continue(tu->timeri);
1951 static int snd_timer_user_pause(struct file *file)
1954 struct snd_timer_user *tu;
1956 tu = file->private_data;
1959 err = snd_timer_pause(tu->timeri);
1966 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1967 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1968 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1969 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1972 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1975 struct snd_timer_user *tu;
1976 void __user *argp = (void __user *)arg;
1977 int __user *p = argp;
1979 tu = file->private_data;
1981 case SNDRV_TIMER_IOCTL_PVERSION:
1982 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1983 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1984 return snd_timer_user_next_device(argp);
1985 case SNDRV_TIMER_IOCTL_TREAD:
1987 int xarg, old_tread;
1989 if (tu->timeri) /* too late */
1991 if (get_user(xarg, p))
1993 old_tread = tu->tread;
1994 tu->tread = xarg ? 1 : 0;
1995 if (tu->tread != old_tread &&
1996 realloc_user_queue(tu, tu->queue_size) < 0) {
1997 tu->tread = old_tread;
2002 case SNDRV_TIMER_IOCTL_GINFO:
2003 return snd_timer_user_ginfo(file, argp);
2004 case SNDRV_TIMER_IOCTL_GPARAMS:
2005 return snd_timer_user_gparams(file, argp);
2006 case SNDRV_TIMER_IOCTL_GSTATUS:
2007 return snd_timer_user_gstatus(file, argp);
2008 case SNDRV_TIMER_IOCTL_SELECT:
2009 return snd_timer_user_tselect(file, argp);
2010 case SNDRV_TIMER_IOCTL_INFO:
2011 return snd_timer_user_info(file, argp);
2012 case SNDRV_TIMER_IOCTL_PARAMS:
2013 return snd_timer_user_params(file, argp);
2014 case SNDRV_TIMER_IOCTL_STATUS:
2015 return snd_timer_user_status(file, argp);
2016 case SNDRV_TIMER_IOCTL_START:
2017 case SNDRV_TIMER_IOCTL_START_OLD:
2018 return snd_timer_user_start(file);
2019 case SNDRV_TIMER_IOCTL_STOP:
2020 case SNDRV_TIMER_IOCTL_STOP_OLD:
2021 return snd_timer_user_stop(file);
2022 case SNDRV_TIMER_IOCTL_CONTINUE:
2023 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
2024 return snd_timer_user_continue(file);
2025 case SNDRV_TIMER_IOCTL_PAUSE:
2026 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
2027 return snd_timer_user_pause(file);
2032 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
2035 struct snd_timer_user *tu = file->private_data;
2038 mutex_lock(&tu->ioctl_lock);
2039 ret = __snd_timer_user_ioctl(file, cmd, arg);
2040 mutex_unlock(&tu->ioctl_lock);
2044 static int snd_timer_user_fasync(int fd, struct file * file, int on)
2046 struct snd_timer_user *tu;
2048 tu = file->private_data;
2049 return fasync_helper(fd, file, on, &tu->fasync);
2052 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2053 size_t count, loff_t *offset)
2055 struct snd_timer_user *tu;
2056 long result = 0, unit;
2060 tu = file->private_data;
2061 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
2062 mutex_lock(&tu->ioctl_lock);
2063 spin_lock_irq(&tu->qlock);
2064 while ((long)count - result >= unit) {
2065 while (!tu->qused) {
2066 wait_queue_entry_t wait;
2068 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2073 set_current_state(TASK_INTERRUPTIBLE);
2074 init_waitqueue_entry(&wait, current);
2075 add_wait_queue(&tu->qchange_sleep, &wait);
2077 spin_unlock_irq(&tu->qlock);
2078 mutex_unlock(&tu->ioctl_lock);
2080 mutex_lock(&tu->ioctl_lock);
2081 spin_lock_irq(&tu->qlock);
2083 remove_wait_queue(&tu->qchange_sleep, &wait);
2085 if (tu->disconnected) {
2089 if (signal_pending(current)) {
2095 qhead = tu->qhead++;
2096 tu->qhead %= tu->queue_size;
2098 spin_unlock_irq(&tu->qlock);
2101 if (copy_to_user(buffer, &tu->tqueue[qhead],
2102 sizeof(struct snd_timer_tread)))
2105 if (copy_to_user(buffer, &tu->queue[qhead],
2106 sizeof(struct snd_timer_read)))
2110 spin_lock_irq(&tu->qlock);
2117 spin_unlock_irq(&tu->qlock);
2118 mutex_unlock(&tu->ioctl_lock);
2119 return result > 0 ? result : err;
2122 static __poll_t snd_timer_user_poll(struct file *file, poll_table * wait)
2125 struct snd_timer_user *tu;
2127 tu = file->private_data;
2129 poll_wait(file, &tu->qchange_sleep, wait);
2132 spin_lock_irq(&tu->qlock);
2134 mask |= EPOLLIN | EPOLLRDNORM;
2135 if (tu->disconnected)
2137 spin_unlock_irq(&tu->qlock);
2142 #ifdef CONFIG_COMPAT
2143 #include "timer_compat.c"
2145 #define snd_timer_user_ioctl_compat NULL
2148 static const struct file_operations snd_timer_f_ops =
2150 .owner = THIS_MODULE,
2151 .read = snd_timer_user_read,
2152 .open = snd_timer_user_open,
2153 .release = snd_timer_user_release,
2154 .llseek = no_llseek,
2155 .poll = snd_timer_user_poll,
2156 .unlocked_ioctl = snd_timer_user_ioctl,
2157 .compat_ioctl = snd_timer_user_ioctl_compat,
2158 .fasync = snd_timer_user_fasync,
2161 /* unregister the system timer */
2162 static void snd_timer_free_all(void)
2164 struct snd_timer *timer, *n;
2166 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2167 snd_timer_free(timer);
2170 static struct device timer_dev;
2176 static int __init alsa_timer_init(void)
2180 snd_device_initialize(&timer_dev, NULL);
2181 dev_set_name(&timer_dev, "timer");
2183 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2184 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2188 err = snd_timer_register_system();
2190 pr_err("ALSA: unable to register system timer (%i)\n", err);
2194 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2195 &snd_timer_f_ops, NULL, &timer_dev);
2197 pr_err("ALSA: unable to register timer device (%i)\n", err);
2198 snd_timer_free_all();
2202 snd_timer_proc_init();
2206 put_device(&timer_dev);
2210 static void __exit alsa_timer_exit(void)
2212 snd_unregister_device(&timer_dev);
2213 snd_timer_free_all();
2214 put_device(&timer_dev);
2215 snd_timer_proc_done();
2216 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2217 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2221 module_init(alsa_timer_init)
2222 module_exit(alsa_timer_exit)