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);
275 /* open a master instance */
276 timer = snd_timer_find(tid);
277 #ifdef CONFIG_MODULES
279 mutex_unlock(®ister_mutex);
280 snd_timer_request(tid);
281 mutex_lock(®ister_mutex);
282 timer = snd_timer_find(tid);
289 if (!list_empty(&timer->open_list_head)) {
290 struct snd_timer_instance *t =
291 list_entry(timer->open_list_head.next,
292 struct snd_timer_instance, open_list);
293 if (t->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
298 if (timer->num_instances >= timer->max_instances) {
302 if (!try_module_get(timer->module)) {
306 /* take a card refcount for safe disconnection */
308 get_device(&timer->card->card_dev);
309 card_dev_to_put = &timer->card->card_dev;
312 if (list_empty(&timer->open_list_head) && timer->hw.open) {
313 err = timer->hw.open(timer);
315 module_put(timer->module);
320 timeri->timer = timer;
321 timeri->slave_class = tid->dev_sclass;
322 timeri->slave_id = slave_id;
324 list_add_tail(&timeri->open_list, &timer->open_list_head);
325 timer->num_instances++;
326 err = snd_timer_check_master(timeri);
329 snd_timer_close_locked(timeri, &card_dev_to_put);
332 mutex_unlock(®ister_mutex);
333 /* put_device() is called after unlock for avoiding deadlock */
334 if (err < 0 && card_dev_to_put)
335 put_device(card_dev_to_put);
338 EXPORT_SYMBOL(snd_timer_open);
341 * close a timer instance
342 * call this with register_mutex down.
344 static void snd_timer_close_locked(struct snd_timer_instance *timeri,
345 struct device **card_devp_to_put)
347 struct snd_timer *timer = timeri->timer;
348 struct snd_timer_instance *slave, *tmp;
351 spin_lock_irq(&timer->lock);
352 timeri->flags |= SNDRV_TIMER_IFLG_DEAD;
353 spin_unlock_irq(&timer->lock);
356 if (!list_empty(&timeri->open_list)) {
357 list_del_init(&timeri->open_list);
358 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
362 /* force to stop the timer */
363 snd_timer_stop(timeri);
366 timer->num_instances--;
367 /* wait, until the active callback is finished */
368 spin_lock_irq(&timer->lock);
369 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
370 spin_unlock_irq(&timer->lock);
372 spin_lock_irq(&timer->lock);
374 spin_unlock_irq(&timer->lock);
376 /* remove slave links */
377 spin_lock_irq(&slave_active_lock);
378 spin_lock(&timer->lock);
379 timeri->timer = NULL;
380 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
382 list_move_tail(&slave->open_list, &snd_timer_slave_list);
383 timer->num_instances--;
384 slave->master = NULL;
386 list_del_init(&slave->ack_list);
387 list_del_init(&slave->active_list);
389 spin_unlock(&timer->lock);
390 spin_unlock_irq(&slave_active_lock);
392 /* slave doesn't need to release timer resources below */
393 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
398 if (list_empty(&timer->open_list_head) && timer->hw.close)
399 timer->hw.close(timer);
400 /* release a card refcount for safe disconnection */
402 *card_devp_to_put = &timer->card->card_dev;
403 module_put(timer->module);
408 * close a timer instance
410 void snd_timer_close(struct snd_timer_instance *timeri)
412 struct device *card_dev_to_put = NULL;
414 if (snd_BUG_ON(!timeri))
417 mutex_lock(®ister_mutex);
418 snd_timer_close_locked(timeri, &card_dev_to_put);
419 mutex_unlock(®ister_mutex);
420 /* put_device() is called after unlock for avoiding deadlock */
422 put_device(card_dev_to_put);
424 EXPORT_SYMBOL(snd_timer_close);
426 static unsigned long snd_timer_hw_resolution(struct snd_timer *timer)
428 if (timer->hw.c_resolution)
429 return timer->hw.c_resolution(timer);
431 return timer->hw.resolution;
434 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
436 struct snd_timer * timer;
437 unsigned long ret = 0;
442 timer = timeri->timer;
444 spin_lock_irqsave(&timer->lock, flags);
445 ret = snd_timer_hw_resolution(timer);
446 spin_unlock_irqrestore(&timer->lock, flags);
450 EXPORT_SYMBOL(snd_timer_resolution);
452 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
454 struct snd_timer *timer = ti->timer;
455 unsigned long resolution = 0;
456 struct snd_timer_instance *ts;
457 struct timespec tstamp;
459 if (timer_tstamp_monotonic)
460 ktime_get_ts(&tstamp);
462 getnstimeofday(&tstamp);
463 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
464 event > SNDRV_TIMER_EVENT_PAUSE))
467 (event == SNDRV_TIMER_EVENT_START ||
468 event == SNDRV_TIMER_EVENT_CONTINUE))
469 resolution = snd_timer_hw_resolution(timer);
471 ti->ccallback(ti, event, &tstamp, resolution);
472 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
476 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
478 list_for_each_entry(ts, &ti->slave_active_head, active_list)
480 ts->ccallback(ts, event + 100, &tstamp, resolution);
483 /* start/continue a master timer */
484 static int snd_timer_start1(struct snd_timer_instance *timeri,
485 bool start, unsigned long ticks)
487 struct snd_timer *timer;
491 timer = timeri->timer;
495 spin_lock_irqsave(&timer->lock, flags);
496 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
500 if (timer->card && timer->card->shutdown) {
504 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
505 SNDRV_TIMER_IFLG_START)) {
511 timeri->ticks = timeri->cticks = ticks;
512 else if (!timeri->cticks)
516 list_move_tail(&timeri->active_list, &timer->active_list_head);
517 if (timer->running) {
518 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
520 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
521 timeri->flags |= SNDRV_TIMER_IFLG_START;
522 result = 1; /* delayed start */
525 timer->sticks = ticks;
526 timer->hw.start(timer);
529 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
532 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
533 SNDRV_TIMER_EVENT_CONTINUE);
535 spin_unlock_irqrestore(&timer->lock, flags);
539 /* start/continue a slave timer */
540 static int snd_timer_start_slave(struct snd_timer_instance *timeri,
546 spin_lock_irqsave(&slave_active_lock, flags);
547 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
551 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
555 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
556 if (timeri->master && timeri->timer) {
557 spin_lock(&timeri->timer->lock);
558 list_add_tail(&timeri->active_list,
559 &timeri->master->slave_active_head);
560 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
561 SNDRV_TIMER_EVENT_CONTINUE);
562 spin_unlock(&timeri->timer->lock);
564 err = 1; /* delayed start */
566 spin_unlock_irqrestore(&slave_active_lock, flags);
570 /* stop/pause a master timer */
571 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
573 struct snd_timer *timer;
577 timer = timeri->timer;
580 spin_lock_irqsave(&timer->lock, flags);
581 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
582 SNDRV_TIMER_IFLG_START))) {
586 list_del_init(&timeri->ack_list);
587 list_del_init(&timeri->active_list);
588 if (timer->card && timer->card->shutdown)
591 timeri->cticks = timeri->ticks;
594 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
595 !(--timer->running)) {
596 timer->hw.stop(timer);
597 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
598 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
599 snd_timer_reschedule(timer, 0);
600 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
601 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
602 timer->hw.start(timer);
606 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
608 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
610 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
611 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
612 SNDRV_TIMER_EVENT_PAUSE);
614 spin_unlock_irqrestore(&timer->lock, flags);
618 /* stop/pause a slave timer */
619 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
623 spin_lock_irqsave(&slave_active_lock, flags);
624 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
625 spin_unlock_irqrestore(&slave_active_lock, flags);
628 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
630 spin_lock(&timeri->timer->lock);
631 list_del_init(&timeri->ack_list);
632 list_del_init(&timeri->active_list);
633 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
634 SNDRV_TIMER_EVENT_PAUSE);
635 spin_unlock(&timeri->timer->lock);
637 spin_unlock_irqrestore(&slave_active_lock, flags);
642 * start the timer instance
644 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
646 if (timeri == NULL || ticks < 1)
648 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
649 return snd_timer_start_slave(timeri, true);
651 return snd_timer_start1(timeri, true, ticks);
653 EXPORT_SYMBOL(snd_timer_start);
656 * stop the timer instance.
658 * do not call this from the timer callback!
660 int snd_timer_stop(struct snd_timer_instance *timeri)
662 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
663 return snd_timer_stop_slave(timeri, true);
665 return snd_timer_stop1(timeri, true);
667 EXPORT_SYMBOL(snd_timer_stop);
670 * start again.. the tick is kept.
672 int snd_timer_continue(struct snd_timer_instance *timeri)
674 /* timer can continue only after pause */
675 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
678 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
679 return snd_timer_start_slave(timeri, false);
681 return snd_timer_start1(timeri, false, 0);
683 EXPORT_SYMBOL(snd_timer_continue);
686 * pause.. remember the ticks left
688 int snd_timer_pause(struct snd_timer_instance * timeri)
690 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
691 return snd_timer_stop_slave(timeri, false);
693 return snd_timer_stop1(timeri, false);
695 EXPORT_SYMBOL(snd_timer_pause);
698 * reschedule the timer
700 * start pending instances and check the scheduling ticks.
701 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
703 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
705 struct snd_timer_instance *ti;
706 unsigned long ticks = ~0UL;
708 list_for_each_entry(ti, &timer->active_list_head, active_list) {
709 if (ti->flags & SNDRV_TIMER_IFLG_START) {
710 ti->flags &= ~SNDRV_TIMER_IFLG_START;
711 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
714 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
715 if (ticks > ti->cticks)
720 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
723 if (ticks > timer->hw.ticks)
724 ticks = timer->hw.ticks;
725 if (ticks_left != ticks)
726 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
727 timer->sticks = ticks;
730 /* call callbacks in timer ack list */
731 static void snd_timer_process_callbacks(struct snd_timer *timer,
732 struct list_head *head)
734 struct snd_timer_instance *ti;
735 unsigned long resolution, ticks;
737 while (!list_empty(head)) {
738 ti = list_first_entry(head, struct snd_timer_instance,
741 /* remove from ack_list and make empty */
742 list_del_init(&ti->ack_list);
744 if (!(ti->flags & SNDRV_TIMER_IFLG_DEAD)) {
747 resolution = ti->resolution;
748 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
749 spin_unlock(&timer->lock);
751 ti->callback(ti, resolution, ticks);
752 spin_lock(&timer->lock);
753 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
758 /* clear pending instances from ack list */
759 static void snd_timer_clear_callbacks(struct snd_timer *timer,
760 struct list_head *head)
764 spin_lock_irqsave(&timer->lock, flags);
765 while (!list_empty(head))
766 list_del_init(head->next);
767 spin_unlock_irqrestore(&timer->lock, flags);
774 static void snd_timer_tasklet(unsigned long arg)
776 struct snd_timer *timer = (struct snd_timer *) arg;
779 if (timer->card && timer->card->shutdown) {
780 snd_timer_clear_callbacks(timer, &timer->sack_list_head);
784 spin_lock_irqsave(&timer->lock, flags);
785 snd_timer_process_callbacks(timer, &timer->sack_list_head);
786 spin_unlock_irqrestore(&timer->lock, flags);
792 * ticks_left is usually equal to timer->sticks.
795 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
797 struct snd_timer_instance *ti, *ts, *tmp;
798 unsigned long resolution;
799 struct list_head *ack_list_head;
806 if (timer->card && timer->card->shutdown) {
807 snd_timer_clear_callbacks(timer, &timer->ack_list_head);
811 spin_lock_irqsave(&timer->lock, flags);
813 /* remember the current resolution */
814 resolution = snd_timer_hw_resolution(timer);
816 /* loop for all active instances
817 * Here we cannot use list_for_each_entry because the active_list of a
818 * processed instance is relinked to done_list_head before the callback
821 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
823 if (ti->flags & SNDRV_TIMER_IFLG_DEAD)
825 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
827 ti->pticks += ticks_left;
828 ti->resolution = resolution;
829 if (ti->cticks < ticks_left)
832 ti->cticks -= ticks_left;
833 if (ti->cticks) /* not expired */
835 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
836 ti->cticks = ti->ticks;
838 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
840 list_del_init(&ti->active_list);
842 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
843 (ti->flags & SNDRV_TIMER_IFLG_FAST))
844 ack_list_head = &timer->ack_list_head;
846 ack_list_head = &timer->sack_list_head;
847 if (list_empty(&ti->ack_list))
848 list_add_tail(&ti->ack_list, ack_list_head);
849 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
850 ts->pticks = ti->pticks;
851 ts->resolution = resolution;
852 if (list_empty(&ts->ack_list))
853 list_add_tail(&ts->ack_list, ack_list_head);
856 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
857 snd_timer_reschedule(timer, timer->sticks);
858 if (timer->running) {
859 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
860 timer->hw.stop(timer);
861 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
863 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
864 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
866 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
867 timer->hw.start(timer);
870 timer->hw.stop(timer);
873 /* now process all fast callbacks */
874 snd_timer_process_callbacks(timer, &timer->ack_list_head);
876 /* do we have any slow callbacks? */
877 use_tasklet = !list_empty(&timer->sack_list_head);
878 spin_unlock_irqrestore(&timer->lock, flags);
881 tasklet_schedule(&timer->task_queue);
883 EXPORT_SYMBOL(snd_timer_interrupt);
889 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
890 struct snd_timer **rtimer)
892 struct snd_timer *timer;
894 static struct snd_device_ops ops = {
895 .dev_free = snd_timer_dev_free,
896 .dev_register = snd_timer_dev_register,
897 .dev_disconnect = snd_timer_dev_disconnect,
900 if (snd_BUG_ON(!tid))
902 if (tid->dev_class == SNDRV_TIMER_CLASS_CARD ||
903 tid->dev_class == SNDRV_TIMER_CLASS_PCM) {
909 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
912 timer->tmr_class = tid->dev_class;
914 timer->tmr_device = tid->device;
915 timer->tmr_subdevice = tid->subdevice;
917 strlcpy(timer->id, id, sizeof(timer->id));
919 INIT_LIST_HEAD(&timer->device_list);
920 INIT_LIST_HEAD(&timer->open_list_head);
921 INIT_LIST_HEAD(&timer->active_list_head);
922 INIT_LIST_HEAD(&timer->ack_list_head);
923 INIT_LIST_HEAD(&timer->sack_list_head);
924 spin_lock_init(&timer->lock);
925 tasklet_init(&timer->task_queue, snd_timer_tasklet,
926 (unsigned long)timer);
927 timer->max_instances = 1000; /* default limit per timer */
929 timer->module = card->module;
930 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
932 snd_timer_free(timer);
940 EXPORT_SYMBOL(snd_timer_new);
942 static int snd_timer_free(struct snd_timer *timer)
947 mutex_lock(®ister_mutex);
948 if (! list_empty(&timer->open_list_head)) {
949 struct list_head *p, *n;
950 struct snd_timer_instance *ti;
951 pr_warn("ALSA: timer %p is busy?\n", timer);
952 list_for_each_safe(p, n, &timer->open_list_head) {
954 ti = list_entry(p, struct snd_timer_instance, open_list);
958 list_del(&timer->device_list);
959 mutex_unlock(®ister_mutex);
961 if (timer->private_free)
962 timer->private_free(timer);
967 static int snd_timer_dev_free(struct snd_device *device)
969 struct snd_timer *timer = device->device_data;
970 return snd_timer_free(timer);
973 static int snd_timer_dev_register(struct snd_device *dev)
975 struct snd_timer *timer = dev->device_data;
976 struct snd_timer *timer1;
978 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
980 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
981 !timer->hw.resolution && timer->hw.c_resolution == NULL)
984 mutex_lock(®ister_mutex);
985 list_for_each_entry(timer1, &snd_timer_list, device_list) {
986 if (timer1->tmr_class > timer->tmr_class)
988 if (timer1->tmr_class < timer->tmr_class)
990 if (timer1->card && timer->card) {
991 if (timer1->card->number > timer->card->number)
993 if (timer1->card->number < timer->card->number)
996 if (timer1->tmr_device > timer->tmr_device)
998 if (timer1->tmr_device < timer->tmr_device)
1000 if (timer1->tmr_subdevice > timer->tmr_subdevice)
1002 if (timer1->tmr_subdevice < timer->tmr_subdevice)
1005 mutex_unlock(®ister_mutex);
1008 list_add_tail(&timer->device_list, &timer1->device_list);
1009 mutex_unlock(®ister_mutex);
1013 static int snd_timer_dev_disconnect(struct snd_device *device)
1015 struct snd_timer *timer = device->device_data;
1016 struct snd_timer_instance *ti;
1018 mutex_lock(®ister_mutex);
1019 list_del_init(&timer->device_list);
1020 /* wake up pending sleepers */
1021 list_for_each_entry(ti, &timer->open_list_head, open_list) {
1025 mutex_unlock(®ister_mutex);
1029 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
1031 unsigned long flags;
1032 unsigned long resolution = 0;
1033 struct snd_timer_instance *ti, *ts;
1035 if (timer->card && timer->card->shutdown)
1037 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1039 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1040 event > SNDRV_TIMER_EVENT_MRESUME))
1042 spin_lock_irqsave(&timer->lock, flags);
1043 if (event == SNDRV_TIMER_EVENT_MSTART ||
1044 event == SNDRV_TIMER_EVENT_MCONTINUE ||
1045 event == SNDRV_TIMER_EVENT_MRESUME)
1046 resolution = snd_timer_hw_resolution(timer);
1047 list_for_each_entry(ti, &timer->active_list_head, active_list) {
1049 ti->ccallback(ti, event, tstamp, resolution);
1050 list_for_each_entry(ts, &ti->slave_active_head, active_list)
1052 ts->ccallback(ts, event, tstamp, resolution);
1054 spin_unlock_irqrestore(&timer->lock, flags);
1056 EXPORT_SYMBOL(snd_timer_notify);
1059 * exported functions for global timers
1061 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1063 struct snd_timer_id tid;
1065 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1066 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1068 tid.device = device;
1070 return snd_timer_new(NULL, id, &tid, rtimer);
1072 EXPORT_SYMBOL(snd_timer_global_new);
1074 int snd_timer_global_free(struct snd_timer *timer)
1076 return snd_timer_free(timer);
1078 EXPORT_SYMBOL(snd_timer_global_free);
1080 int snd_timer_global_register(struct snd_timer *timer)
1082 struct snd_device dev;
1084 memset(&dev, 0, sizeof(dev));
1085 dev.device_data = timer;
1086 return snd_timer_dev_register(&dev);
1088 EXPORT_SYMBOL(snd_timer_global_register);
1094 struct snd_timer_system_private {
1095 struct timer_list tlist;
1096 struct snd_timer *snd_timer;
1097 unsigned long last_expires;
1098 unsigned long last_jiffies;
1099 unsigned long correction;
1102 static void snd_timer_s_function(struct timer_list *t)
1104 struct snd_timer_system_private *priv = from_timer(priv, t,
1106 struct snd_timer *timer = priv->snd_timer;
1107 unsigned long jiff = jiffies;
1108 if (time_after(jiff, priv->last_expires))
1109 priv->correction += (long)jiff - (long)priv->last_expires;
1110 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1113 static int snd_timer_s_start(struct snd_timer * timer)
1115 struct snd_timer_system_private *priv;
1116 unsigned long njiff;
1118 priv = (struct snd_timer_system_private *) timer->private_data;
1119 njiff = (priv->last_jiffies = jiffies);
1120 if (priv->correction > timer->sticks - 1) {
1121 priv->correction -= timer->sticks - 1;
1124 njiff += timer->sticks - priv->correction;
1125 priv->correction = 0;
1127 priv->last_expires = njiff;
1128 mod_timer(&priv->tlist, njiff);
1132 static int snd_timer_s_stop(struct snd_timer * timer)
1134 struct snd_timer_system_private *priv;
1137 priv = (struct snd_timer_system_private *) timer->private_data;
1138 del_timer(&priv->tlist);
1140 if (time_before(jiff, priv->last_expires))
1141 timer->sticks = priv->last_expires - jiff;
1144 priv->correction = 0;
1148 static int snd_timer_s_close(struct snd_timer *timer)
1150 struct snd_timer_system_private *priv;
1152 priv = (struct snd_timer_system_private *)timer->private_data;
1153 del_timer_sync(&priv->tlist);
1157 static struct snd_timer_hardware snd_timer_system =
1159 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1160 .resolution = 1000000000L / HZ,
1162 .close = snd_timer_s_close,
1163 .start = snd_timer_s_start,
1164 .stop = snd_timer_s_stop
1167 static void snd_timer_free_system(struct snd_timer *timer)
1169 kfree(timer->private_data);
1172 static int snd_timer_register_system(void)
1174 struct snd_timer *timer;
1175 struct snd_timer_system_private *priv;
1178 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1181 strcpy(timer->name, "system timer");
1182 timer->hw = snd_timer_system;
1183 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1185 snd_timer_free(timer);
1188 priv->snd_timer = timer;
1189 timer_setup(&priv->tlist, snd_timer_s_function, 0);
1190 timer->private_data = priv;
1191 timer->private_free = snd_timer_free_system;
1192 return snd_timer_global_register(timer);
1195 #ifdef CONFIG_SND_PROC_FS
1200 static void snd_timer_proc_read(struct snd_info_entry *entry,
1201 struct snd_info_buffer *buffer)
1203 struct snd_timer *timer;
1204 struct snd_timer_instance *ti;
1206 mutex_lock(®ister_mutex);
1207 list_for_each_entry(timer, &snd_timer_list, device_list) {
1208 if (timer->card && timer->card->shutdown)
1210 switch (timer->tmr_class) {
1211 case SNDRV_TIMER_CLASS_GLOBAL:
1212 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1214 case SNDRV_TIMER_CLASS_CARD:
1215 snd_iprintf(buffer, "C%i-%i: ",
1216 timer->card->number, timer->tmr_device);
1218 case SNDRV_TIMER_CLASS_PCM:
1219 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1220 timer->tmr_device, timer->tmr_subdevice);
1223 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1224 timer->card ? timer->card->number : -1,
1225 timer->tmr_device, timer->tmr_subdevice);
1227 snd_iprintf(buffer, "%s :", timer->name);
1228 if (timer->hw.resolution)
1229 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1230 timer->hw.resolution / 1000,
1231 timer->hw.resolution % 1000,
1233 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1234 snd_iprintf(buffer, " SLAVE");
1235 snd_iprintf(buffer, "\n");
1236 list_for_each_entry(ti, &timer->open_list_head, open_list)
1237 snd_iprintf(buffer, " Client %s : %s\n",
1238 ti->owner ? ti->owner : "unknown",
1239 ti->flags & (SNDRV_TIMER_IFLG_START |
1240 SNDRV_TIMER_IFLG_RUNNING)
1241 ? "running" : "stopped");
1243 mutex_unlock(®ister_mutex);
1246 static struct snd_info_entry *snd_timer_proc_entry;
1248 static void __init snd_timer_proc_init(void)
1250 struct snd_info_entry *entry;
1252 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1253 if (entry != NULL) {
1254 entry->c.text.read = snd_timer_proc_read;
1255 if (snd_info_register(entry) < 0) {
1256 snd_info_free_entry(entry);
1260 snd_timer_proc_entry = entry;
1263 static void __exit snd_timer_proc_done(void)
1265 snd_info_free_entry(snd_timer_proc_entry);
1267 #else /* !CONFIG_SND_PROC_FS */
1268 #define snd_timer_proc_init()
1269 #define snd_timer_proc_done()
1273 * USER SPACE interface
1276 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1277 unsigned long resolution,
1278 unsigned long ticks)
1280 struct snd_timer_user *tu = timeri->callback_data;
1281 struct snd_timer_read *r;
1284 spin_lock(&tu->qlock);
1285 if (tu->qused > 0) {
1286 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1287 r = &tu->queue[prev];
1288 if (r->resolution == resolution) {
1293 if (tu->qused >= tu->queue_size) {
1296 r = &tu->queue[tu->qtail++];
1297 tu->qtail %= tu->queue_size;
1298 r->resolution = resolution;
1303 spin_unlock(&tu->qlock);
1304 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1305 wake_up(&tu->qchange_sleep);
1308 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1309 struct snd_timer_tread *tread)
1311 if (tu->qused >= tu->queue_size) {
1314 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1315 tu->qtail %= tu->queue_size;
1320 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1322 struct timespec *tstamp,
1323 unsigned long resolution)
1325 struct snd_timer_user *tu = timeri->callback_data;
1326 struct snd_timer_tread r1;
1327 unsigned long flags;
1329 if (event >= SNDRV_TIMER_EVENT_START &&
1330 event <= SNDRV_TIMER_EVENT_PAUSE)
1331 tu->tstamp = *tstamp;
1332 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1334 memset(&r1, 0, sizeof(r1));
1336 r1.tstamp = *tstamp;
1337 r1.val = resolution;
1338 spin_lock_irqsave(&tu->qlock, flags);
1339 snd_timer_user_append_to_tqueue(tu, &r1);
1340 spin_unlock_irqrestore(&tu->qlock, flags);
1341 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1342 wake_up(&tu->qchange_sleep);
1345 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1347 struct snd_timer_user *tu = timeri->callback_data;
1349 tu->disconnected = true;
1350 wake_up(&tu->qchange_sleep);
1353 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1354 unsigned long resolution,
1355 unsigned long ticks)
1357 struct snd_timer_user *tu = timeri->callback_data;
1358 struct snd_timer_tread *r, r1;
1359 struct timespec tstamp;
1360 int prev, append = 0;
1362 memset(&r1, 0, sizeof(r1));
1363 memset(&tstamp, 0, sizeof(tstamp));
1364 spin_lock(&tu->qlock);
1365 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1366 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1367 spin_unlock(&tu->qlock);
1370 if (tu->last_resolution != resolution || ticks > 0) {
1371 if (timer_tstamp_monotonic)
1372 ktime_get_ts(&tstamp);
1374 getnstimeofday(&tstamp);
1376 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1377 tu->last_resolution != resolution) {
1378 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1380 r1.val = resolution;
1381 snd_timer_user_append_to_tqueue(tu, &r1);
1382 tu->last_resolution = resolution;
1385 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1389 if (tu->qused > 0) {
1390 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1391 r = &tu->tqueue[prev];
1392 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1399 r1.event = SNDRV_TIMER_EVENT_TICK;
1402 snd_timer_user_append_to_tqueue(tu, &r1);
1405 spin_unlock(&tu->qlock);
1408 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1409 wake_up(&tu->qchange_sleep);
1412 static int realloc_user_queue(struct snd_timer_user *tu, int size)
1414 struct snd_timer_read *queue = NULL;
1415 struct snd_timer_tread *tqueue = NULL;
1418 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
1422 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
1427 spin_lock_irq(&tu->qlock);
1430 tu->queue_size = size;
1432 tu->tqueue = tqueue;
1433 tu->qhead = tu->qtail = tu->qused = 0;
1434 spin_unlock_irq(&tu->qlock);
1439 static int snd_timer_user_open(struct inode *inode, struct file *file)
1441 struct snd_timer_user *tu;
1444 err = stream_open(inode, file);
1448 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1451 spin_lock_init(&tu->qlock);
1452 init_waitqueue_head(&tu->qchange_sleep);
1453 mutex_init(&tu->ioctl_lock);
1455 if (realloc_user_queue(tu, 128) < 0) {
1459 file->private_data = tu;
1463 static int snd_timer_user_release(struct inode *inode, struct file *file)
1465 struct snd_timer_user *tu;
1467 if (file->private_data) {
1468 tu = file->private_data;
1469 file->private_data = NULL;
1470 mutex_lock(&tu->ioctl_lock);
1472 snd_timer_close(tu->timeri);
1473 snd_timer_instance_free(tu->timeri);
1475 mutex_unlock(&tu->ioctl_lock);
1483 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1485 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1486 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1492 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1494 id->dev_class = timer->tmr_class;
1495 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1496 id->card = timer->card ? timer->card->number : -1;
1497 id->device = timer->tmr_device;
1498 id->subdevice = timer->tmr_subdevice;
1501 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1503 struct snd_timer_id id;
1504 struct snd_timer *timer;
1505 struct list_head *p;
1507 if (copy_from_user(&id, _tid, sizeof(id)))
1509 mutex_lock(®ister_mutex);
1510 if (id.dev_class < 0) { /* first item */
1511 if (list_empty(&snd_timer_list))
1512 snd_timer_user_zero_id(&id);
1514 timer = list_entry(snd_timer_list.next,
1515 struct snd_timer, device_list);
1516 snd_timer_user_copy_id(&id, timer);
1519 switch (id.dev_class) {
1520 case SNDRV_TIMER_CLASS_GLOBAL:
1521 id.device = id.device < 0 ? 0 : id.device + 1;
1522 list_for_each(p, &snd_timer_list) {
1523 timer = list_entry(p, struct snd_timer, device_list);
1524 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1525 snd_timer_user_copy_id(&id, timer);
1528 if (timer->tmr_device >= id.device) {
1529 snd_timer_user_copy_id(&id, timer);
1533 if (p == &snd_timer_list)
1534 snd_timer_user_zero_id(&id);
1536 case SNDRV_TIMER_CLASS_CARD:
1537 case SNDRV_TIMER_CLASS_PCM:
1541 if (id.device < 0) {
1544 if (id.subdevice < 0)
1546 else if (id.subdevice < INT_MAX)
1550 list_for_each(p, &snd_timer_list) {
1551 timer = list_entry(p, struct snd_timer, device_list);
1552 if (timer->tmr_class > id.dev_class) {
1553 snd_timer_user_copy_id(&id, timer);
1556 if (timer->tmr_class < id.dev_class)
1558 if (timer->card->number > id.card) {
1559 snd_timer_user_copy_id(&id, timer);
1562 if (timer->card->number < id.card)
1564 if (timer->tmr_device > id.device) {
1565 snd_timer_user_copy_id(&id, timer);
1568 if (timer->tmr_device < id.device)
1570 if (timer->tmr_subdevice > id.subdevice) {
1571 snd_timer_user_copy_id(&id, timer);
1574 if (timer->tmr_subdevice < id.subdevice)
1576 snd_timer_user_copy_id(&id, timer);
1579 if (p == &snd_timer_list)
1580 snd_timer_user_zero_id(&id);
1583 snd_timer_user_zero_id(&id);
1586 mutex_unlock(®ister_mutex);
1587 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1592 static int snd_timer_user_ginfo(struct file *file,
1593 struct snd_timer_ginfo __user *_ginfo)
1595 struct snd_timer_ginfo *ginfo;
1596 struct snd_timer_id tid;
1597 struct snd_timer *t;
1598 struct list_head *p;
1601 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1603 return PTR_ERR(ginfo);
1606 memset(ginfo, 0, sizeof(*ginfo));
1608 mutex_lock(®ister_mutex);
1609 t = snd_timer_find(&tid);
1611 ginfo->card = t->card ? t->card->number : -1;
1612 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1613 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1614 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1615 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1616 ginfo->resolution = t->hw.resolution;
1617 if (t->hw.resolution_min > 0) {
1618 ginfo->resolution_min = t->hw.resolution_min;
1619 ginfo->resolution_max = t->hw.resolution_max;
1621 list_for_each(p, &t->open_list_head) {
1627 mutex_unlock(®ister_mutex);
1628 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1634 static int timer_set_gparams(struct snd_timer_gparams *gparams)
1636 struct snd_timer *t;
1639 mutex_lock(®ister_mutex);
1640 t = snd_timer_find(&gparams->tid);
1645 if (!list_empty(&t->open_list_head)) {
1649 if (!t->hw.set_period) {
1653 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
1655 mutex_unlock(®ister_mutex);
1659 static int snd_timer_user_gparams(struct file *file,
1660 struct snd_timer_gparams __user *_gparams)
1662 struct snd_timer_gparams gparams;
1664 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1666 return timer_set_gparams(&gparams);
1669 static int snd_timer_user_gstatus(struct file *file,
1670 struct snd_timer_gstatus __user *_gstatus)
1672 struct snd_timer_gstatus gstatus;
1673 struct snd_timer_id tid;
1674 struct snd_timer *t;
1677 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1680 memset(&gstatus, 0, sizeof(gstatus));
1682 mutex_lock(®ister_mutex);
1683 t = snd_timer_find(&tid);
1685 spin_lock_irq(&t->lock);
1686 gstatus.resolution = snd_timer_hw_resolution(t);
1687 if (t->hw.precise_resolution) {
1688 t->hw.precise_resolution(t, &gstatus.resolution_num,
1689 &gstatus.resolution_den);
1691 gstatus.resolution_num = gstatus.resolution;
1692 gstatus.resolution_den = 1000000000uL;
1694 spin_unlock_irq(&t->lock);
1698 mutex_unlock(®ister_mutex);
1699 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1704 static int snd_timer_user_tselect(struct file *file,
1705 struct snd_timer_select __user *_tselect)
1707 struct snd_timer_user *tu;
1708 struct snd_timer_select tselect;
1712 tu = file->private_data;
1714 snd_timer_close(tu->timeri);
1715 snd_timer_instance_free(tu->timeri);
1718 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1722 sprintf(str, "application %i", current->pid);
1723 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1724 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1725 tu->timeri = snd_timer_instance_new(str);
1731 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1732 tu->timeri->callback = tu->tread
1733 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1734 tu->timeri->ccallback = snd_timer_user_ccallback;
1735 tu->timeri->callback_data = (void *)tu;
1736 tu->timeri->disconnect = snd_timer_user_disconnect;
1738 err = snd_timer_open(tu->timeri, &tselect.id, current->pid);
1740 snd_timer_instance_free(tu->timeri);
1748 static int snd_timer_user_info(struct file *file,
1749 struct snd_timer_info __user *_info)
1751 struct snd_timer_user *tu;
1752 struct snd_timer_info *info;
1753 struct snd_timer *t;
1756 tu = file->private_data;
1759 t = tu->timeri->timer;
1763 info = kzalloc(sizeof(*info), GFP_KERNEL);
1766 info->card = t->card ? t->card->number : -1;
1767 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1768 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1769 strlcpy(info->id, t->id, sizeof(info->id));
1770 strlcpy(info->name, t->name, sizeof(info->name));
1771 info->resolution = t->hw.resolution;
1772 if (copy_to_user(_info, info, sizeof(*_info)))
1778 static int snd_timer_user_params(struct file *file,
1779 struct snd_timer_params __user *_params)
1781 struct snd_timer_user *tu;
1782 struct snd_timer_params params;
1783 struct snd_timer *t;
1786 tu = file->private_data;
1789 t = tu->timeri->timer;
1792 if (copy_from_user(¶ms, _params, sizeof(params)))
1794 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1797 if (params.ticks < 1) {
1802 /* Don't allow resolution less than 1ms */
1803 resolution = snd_timer_resolution(tu->timeri);
1804 resolution *= params.ticks;
1805 if (resolution < 1000000) {
1810 if (params.queue_size > 0 &&
1811 (params.queue_size < 32 || params.queue_size > 1024)) {
1815 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1816 (1<<SNDRV_TIMER_EVENT_TICK)|
1817 (1<<SNDRV_TIMER_EVENT_START)|
1818 (1<<SNDRV_TIMER_EVENT_STOP)|
1819 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1820 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1821 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1822 (1<<SNDRV_TIMER_EVENT_RESUME)|
1823 (1<<SNDRV_TIMER_EVENT_MSTART)|
1824 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1825 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1826 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1827 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1828 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1832 snd_timer_stop(tu->timeri);
1833 spin_lock_irq(&t->lock);
1834 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1835 SNDRV_TIMER_IFLG_EXCLUSIVE|
1836 SNDRV_TIMER_IFLG_EARLY_EVENT);
1837 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1838 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1839 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1840 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1841 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1842 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1843 spin_unlock_irq(&t->lock);
1844 if (params.queue_size > 0 &&
1845 (unsigned int)tu->queue_size != params.queue_size) {
1846 err = realloc_user_queue(tu, params.queue_size);
1850 spin_lock_irq(&tu->qlock);
1851 tu->qhead = tu->qtail = tu->qused = 0;
1852 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1854 struct snd_timer_tread tread;
1855 memset(&tread, 0, sizeof(tread));
1856 tread.event = SNDRV_TIMER_EVENT_EARLY;
1857 tread.tstamp.tv_sec = 0;
1858 tread.tstamp.tv_nsec = 0;
1860 snd_timer_user_append_to_tqueue(tu, &tread);
1862 struct snd_timer_read *r = &tu->queue[0];
1869 tu->filter = params.filter;
1870 tu->ticks = params.ticks;
1871 spin_unlock_irq(&tu->qlock);
1874 if (copy_to_user(_params, ¶ms, sizeof(params)))
1879 static int snd_timer_user_status(struct file *file,
1880 struct snd_timer_status __user *_status)
1882 struct snd_timer_user *tu;
1883 struct snd_timer_status status;
1885 tu = file->private_data;
1888 memset(&status, 0, sizeof(status));
1889 status.tstamp = tu->tstamp;
1890 status.resolution = snd_timer_resolution(tu->timeri);
1891 status.lost = tu->timeri->lost;
1892 status.overrun = tu->overrun;
1893 spin_lock_irq(&tu->qlock);
1894 status.queue = tu->qused;
1895 spin_unlock_irq(&tu->qlock);
1896 if (copy_to_user(_status, &status, sizeof(status)))
1901 static int snd_timer_user_start(struct file *file)
1904 struct snd_timer_user *tu;
1906 tu = file->private_data;
1909 snd_timer_stop(tu->timeri);
1910 tu->timeri->lost = 0;
1911 tu->last_resolution = 0;
1912 err = snd_timer_start(tu->timeri, tu->ticks);
1918 static int snd_timer_user_stop(struct file *file)
1921 struct snd_timer_user *tu;
1923 tu = file->private_data;
1926 err = snd_timer_stop(tu->timeri);
1932 static int snd_timer_user_continue(struct file *file)
1935 struct snd_timer_user *tu;
1937 tu = file->private_data;
1940 /* start timer instead of continue if it's not used before */
1941 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1942 return snd_timer_user_start(file);
1943 tu->timeri->lost = 0;
1944 err = snd_timer_continue(tu->timeri);
1950 static int snd_timer_user_pause(struct file *file)
1953 struct snd_timer_user *tu;
1955 tu = file->private_data;
1958 err = snd_timer_pause(tu->timeri);
1965 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1966 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1967 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1968 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1971 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1974 struct snd_timer_user *tu;
1975 void __user *argp = (void __user *)arg;
1976 int __user *p = argp;
1978 tu = file->private_data;
1980 case SNDRV_TIMER_IOCTL_PVERSION:
1981 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1982 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1983 return snd_timer_user_next_device(argp);
1984 case SNDRV_TIMER_IOCTL_TREAD:
1986 int xarg, old_tread;
1988 if (tu->timeri) /* too late */
1990 if (get_user(xarg, p))
1992 old_tread = tu->tread;
1993 tu->tread = xarg ? 1 : 0;
1994 if (tu->tread != old_tread &&
1995 realloc_user_queue(tu, tu->queue_size) < 0) {
1996 tu->tread = old_tread;
2001 case SNDRV_TIMER_IOCTL_GINFO:
2002 return snd_timer_user_ginfo(file, argp);
2003 case SNDRV_TIMER_IOCTL_GPARAMS:
2004 return snd_timer_user_gparams(file, argp);
2005 case SNDRV_TIMER_IOCTL_GSTATUS:
2006 return snd_timer_user_gstatus(file, argp);
2007 case SNDRV_TIMER_IOCTL_SELECT:
2008 return snd_timer_user_tselect(file, argp);
2009 case SNDRV_TIMER_IOCTL_INFO:
2010 return snd_timer_user_info(file, argp);
2011 case SNDRV_TIMER_IOCTL_PARAMS:
2012 return snd_timer_user_params(file, argp);
2013 case SNDRV_TIMER_IOCTL_STATUS:
2014 return snd_timer_user_status(file, argp);
2015 case SNDRV_TIMER_IOCTL_START:
2016 case SNDRV_TIMER_IOCTL_START_OLD:
2017 return snd_timer_user_start(file);
2018 case SNDRV_TIMER_IOCTL_STOP:
2019 case SNDRV_TIMER_IOCTL_STOP_OLD:
2020 return snd_timer_user_stop(file);
2021 case SNDRV_TIMER_IOCTL_CONTINUE:
2022 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
2023 return snd_timer_user_continue(file);
2024 case SNDRV_TIMER_IOCTL_PAUSE:
2025 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
2026 return snd_timer_user_pause(file);
2031 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
2034 struct snd_timer_user *tu = file->private_data;
2037 mutex_lock(&tu->ioctl_lock);
2038 ret = __snd_timer_user_ioctl(file, cmd, arg);
2039 mutex_unlock(&tu->ioctl_lock);
2043 static int snd_timer_user_fasync(int fd, struct file * file, int on)
2045 struct snd_timer_user *tu;
2047 tu = file->private_data;
2048 return fasync_helper(fd, file, on, &tu->fasync);
2051 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2052 size_t count, loff_t *offset)
2054 struct snd_timer_user *tu;
2055 long result = 0, unit;
2059 tu = file->private_data;
2060 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
2061 mutex_lock(&tu->ioctl_lock);
2062 spin_lock_irq(&tu->qlock);
2063 while ((long)count - result >= unit) {
2064 while (!tu->qused) {
2065 wait_queue_entry_t wait;
2067 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2072 set_current_state(TASK_INTERRUPTIBLE);
2073 init_waitqueue_entry(&wait, current);
2074 add_wait_queue(&tu->qchange_sleep, &wait);
2076 spin_unlock_irq(&tu->qlock);
2077 mutex_unlock(&tu->ioctl_lock);
2079 mutex_lock(&tu->ioctl_lock);
2080 spin_lock_irq(&tu->qlock);
2082 remove_wait_queue(&tu->qchange_sleep, &wait);
2084 if (tu->disconnected) {
2088 if (signal_pending(current)) {
2094 qhead = tu->qhead++;
2095 tu->qhead %= tu->queue_size;
2097 spin_unlock_irq(&tu->qlock);
2100 if (copy_to_user(buffer, &tu->tqueue[qhead],
2101 sizeof(struct snd_timer_tread)))
2104 if (copy_to_user(buffer, &tu->queue[qhead],
2105 sizeof(struct snd_timer_read)))
2109 spin_lock_irq(&tu->qlock);
2116 spin_unlock_irq(&tu->qlock);
2117 mutex_unlock(&tu->ioctl_lock);
2118 return result > 0 ? result : err;
2121 static __poll_t snd_timer_user_poll(struct file *file, poll_table * wait)
2124 struct snd_timer_user *tu;
2126 tu = file->private_data;
2128 poll_wait(file, &tu->qchange_sleep, wait);
2131 spin_lock_irq(&tu->qlock);
2133 mask |= EPOLLIN | EPOLLRDNORM;
2134 if (tu->disconnected)
2136 spin_unlock_irq(&tu->qlock);
2141 #ifdef CONFIG_COMPAT
2142 #include "timer_compat.c"
2144 #define snd_timer_user_ioctl_compat NULL
2147 static const struct file_operations snd_timer_f_ops =
2149 .owner = THIS_MODULE,
2150 .read = snd_timer_user_read,
2151 .open = snd_timer_user_open,
2152 .release = snd_timer_user_release,
2153 .llseek = no_llseek,
2154 .poll = snd_timer_user_poll,
2155 .unlocked_ioctl = snd_timer_user_ioctl,
2156 .compat_ioctl = snd_timer_user_ioctl_compat,
2157 .fasync = snd_timer_user_fasync,
2160 /* unregister the system timer */
2161 static void snd_timer_free_all(void)
2163 struct snd_timer *timer, *n;
2165 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2166 snd_timer_free(timer);
2169 static struct device timer_dev;
2175 static int __init alsa_timer_init(void)
2179 snd_device_initialize(&timer_dev, NULL);
2180 dev_set_name(&timer_dev, "timer");
2182 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2183 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2187 err = snd_timer_register_system();
2189 pr_err("ALSA: unable to register system timer (%i)\n", err);
2193 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2194 &snd_timer_f_ops, NULL, &timer_dev);
2196 pr_err("ALSA: unable to register timer device (%i)\n", err);
2197 snd_timer_free_all();
2201 snd_timer_proc_init();
2205 put_device(&timer_dev);
2209 static void __exit alsa_timer_exit(void)
2211 snd_unregister_device(&timer_dev);
2212 snd_timer_free_all();
2213 put_device(&timer_dev);
2214 snd_timer_proc_done();
2215 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2216 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2220 module_init(alsa_timer_init)
2221 module_exit(alsa_timer_exit)