2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/mutex.h>
27 #include <linux/device.h>
28 #include <linux/module.h>
29 #include <linux/string.h>
30 #include <linux/sched/signal.h>
31 #include <sound/core.h>
32 #include <sound/timer.h>
33 #include <sound/control.h>
34 #include <sound/info.h>
35 #include <sound/minors.h>
36 #include <sound/initval.h>
37 #include <linux/kmod.h>
40 #define SNDRV_TIMER_IFLG_PAUSED 0x00010000
41 #define SNDRV_TIMER_IFLG_DEAD 0x00020000
43 #if IS_ENABLED(CONFIG_SND_HRTIMER)
44 #define DEFAULT_TIMER_LIMIT 4
46 #define DEFAULT_TIMER_LIMIT 1
49 static int timer_limit = DEFAULT_TIMER_LIMIT;
50 static int timer_tstamp_monotonic = 1;
51 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
52 MODULE_DESCRIPTION("ALSA timer interface");
53 MODULE_LICENSE("GPL");
54 module_param(timer_limit, int, 0444);
55 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
56 module_param(timer_tstamp_monotonic, int, 0444);
57 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
59 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
60 MODULE_ALIAS("devname:snd/timer");
62 struct snd_timer_user {
63 struct snd_timer_instance *timeri;
64 int tread; /* enhanced read with timestamps and events */
66 unsigned long overrun;
72 struct snd_timer_read *queue;
73 struct snd_timer_tread *tqueue;
75 unsigned long last_resolution;
77 struct timespec tstamp; /* trigger tstamp */
78 wait_queue_head_t qchange_sleep;
79 struct fasync_struct *fasync;
80 struct mutex ioctl_lock;
84 static LIST_HEAD(snd_timer_list);
86 /* list of slave instances */
87 static LIST_HEAD(snd_timer_slave_list);
89 /* lock for slave active lists */
90 static DEFINE_SPINLOCK(slave_active_lock);
92 static DEFINE_MUTEX(register_mutex);
94 static int snd_timer_free(struct snd_timer *timer);
95 static int snd_timer_dev_free(struct snd_device *device);
96 static int snd_timer_dev_register(struct snd_device *device);
97 static int snd_timer_dev_disconnect(struct snd_device *device);
99 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
102 * create a timer instance with the given owner string.
103 * when timer is not NULL, increments the module counter
105 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
106 struct snd_timer *timer)
108 struct snd_timer_instance *timeri;
109 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
112 timeri->owner = kstrdup(owner, GFP_KERNEL);
113 if (! timeri->owner) {
117 INIT_LIST_HEAD(&timeri->open_list);
118 INIT_LIST_HEAD(&timeri->active_list);
119 INIT_LIST_HEAD(&timeri->ack_list);
120 INIT_LIST_HEAD(&timeri->slave_list_head);
121 INIT_LIST_HEAD(&timeri->slave_active_head);
123 timeri->timer = timer;
124 if (timer && !try_module_get(timer->module)) {
125 kfree(timeri->owner);
134 * find a timer instance from the given timer id
136 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
138 struct snd_timer *timer = NULL;
140 list_for_each_entry(timer, &snd_timer_list, device_list) {
141 if (timer->tmr_class != tid->dev_class)
143 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
144 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
145 (timer->card == NULL ||
146 timer->card->number != tid->card))
148 if (timer->tmr_device != tid->device)
150 if (timer->tmr_subdevice != tid->subdevice)
157 #ifdef CONFIG_MODULES
159 static void snd_timer_request(struct snd_timer_id *tid)
161 switch (tid->dev_class) {
162 case SNDRV_TIMER_CLASS_GLOBAL:
163 if (tid->device < timer_limit)
164 request_module("snd-timer-%i", tid->device);
166 case SNDRV_TIMER_CLASS_CARD:
167 case SNDRV_TIMER_CLASS_PCM:
168 if (tid->card < snd_ecards_limit)
169 request_module("snd-card-%i", tid->card);
179 * look for a master instance matching with the slave id of the given slave.
180 * when found, relink the open_link of the slave.
182 * call this with register_mutex down.
184 static int snd_timer_check_slave(struct snd_timer_instance *slave)
186 struct snd_timer *timer;
187 struct snd_timer_instance *master;
189 /* FIXME: it's really dumb to look up all entries.. */
190 list_for_each_entry(timer, &snd_timer_list, device_list) {
191 list_for_each_entry(master, &timer->open_list_head, open_list) {
192 if (slave->slave_class == master->slave_class &&
193 slave->slave_id == master->slave_id) {
194 if (master->timer->num_instances >=
195 master->timer->max_instances)
197 list_move_tail(&slave->open_list,
198 &master->slave_list_head);
199 master->timer->num_instances++;
200 spin_lock_irq(&slave_active_lock);
201 slave->master = master;
202 slave->timer = master->timer;
203 spin_unlock_irq(&slave_active_lock);
212 * look for slave instances matching with the slave id of the given master.
213 * when found, relink the open_link of slaves.
215 * call this with register_mutex down.
217 static int snd_timer_check_master(struct snd_timer_instance *master)
219 struct snd_timer_instance *slave, *tmp;
221 /* check all pending slaves */
222 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
223 if (slave->slave_class == master->slave_class &&
224 slave->slave_id == master->slave_id) {
225 if (master->timer->num_instances >=
226 master->timer->max_instances)
228 list_move_tail(&slave->open_list, &master->slave_list_head);
229 master->timer->num_instances++;
230 spin_lock_irq(&slave_active_lock);
231 spin_lock(&master->timer->lock);
232 slave->master = master;
233 slave->timer = master->timer;
234 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
235 list_add_tail(&slave->active_list,
236 &master->slave_active_head);
237 spin_unlock(&master->timer->lock);
238 spin_unlock_irq(&slave_active_lock);
244 static int snd_timer_close_locked(struct snd_timer_instance *timeri);
247 * open a timer instance
248 * when opening a master, the slave id must be here given.
250 int snd_timer_open(struct snd_timer_instance **ti,
251 char *owner, struct snd_timer_id *tid,
252 unsigned int slave_id)
254 struct snd_timer *timer;
255 struct snd_timer_instance *timeri = NULL;
258 mutex_lock(®ister_mutex);
259 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
260 /* open a slave instance */
261 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
262 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
263 pr_debug("ALSA: timer: invalid slave class %i\n",
268 timeri = snd_timer_instance_new(owner, NULL);
273 timeri->slave_class = tid->dev_sclass;
274 timeri->slave_id = tid->device;
275 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
276 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
277 err = snd_timer_check_slave(timeri);
279 snd_timer_close_locked(timeri);
285 /* open a master instance */
286 timer = snd_timer_find(tid);
287 #ifdef CONFIG_MODULES
289 mutex_unlock(®ister_mutex);
290 snd_timer_request(tid);
291 mutex_lock(®ister_mutex);
292 timer = snd_timer_find(tid);
299 if (!list_empty(&timer->open_list_head)) {
300 timeri = list_entry(timer->open_list_head.next,
301 struct snd_timer_instance, open_list);
302 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
308 if (timer->num_instances >= timer->max_instances) {
312 timeri = snd_timer_instance_new(owner, timer);
317 /* take a card refcount for safe disconnection */
319 get_device(&timer->card->card_dev);
320 timeri->slave_class = tid->dev_sclass;
321 timeri->slave_id = slave_id;
323 if (list_empty(&timer->open_list_head) && timer->hw.open) {
324 err = timer->hw.open(timer);
326 kfree(timeri->owner);
331 put_device(&timer->card->card_dev);
332 module_put(timer->module);
337 list_add_tail(&timeri->open_list, &timer->open_list_head);
338 timer->num_instances++;
339 err = snd_timer_check_master(timeri);
341 snd_timer_close_locked(timeri);
346 mutex_unlock(®ister_mutex);
350 EXPORT_SYMBOL(snd_timer_open);
353 * close a timer instance
354 * call this with register_mutex down.
356 static int snd_timer_close_locked(struct snd_timer_instance *timeri)
358 struct snd_timer *timer = timeri->timer;
359 struct snd_timer_instance *slave, *tmp;
362 spin_lock_irq(&timer->lock);
363 timeri->flags |= SNDRV_TIMER_IFLG_DEAD;
364 spin_unlock_irq(&timer->lock);
367 list_del(&timeri->open_list);
369 /* force to stop the timer */
370 snd_timer_stop(timeri);
373 timer->num_instances--;
374 /* wait, until the active callback is finished */
375 spin_lock_irq(&timer->lock);
376 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
377 spin_unlock_irq(&timer->lock);
379 spin_lock_irq(&timer->lock);
381 spin_unlock_irq(&timer->lock);
383 /* remove slave links */
384 spin_lock_irq(&slave_active_lock);
385 spin_lock(&timer->lock);
386 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
388 list_move_tail(&slave->open_list, &snd_timer_slave_list);
389 timer->num_instances--;
390 slave->master = NULL;
392 list_del_init(&slave->ack_list);
393 list_del_init(&slave->active_list);
395 spin_unlock(&timer->lock);
396 spin_unlock_irq(&slave_active_lock);
398 /* slave doesn't need to release timer resources below */
399 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
403 if (timeri->private_free)
404 timeri->private_free(timeri);
405 kfree(timeri->owner);
409 if (list_empty(&timer->open_list_head) && timer->hw.close)
410 timer->hw.close(timer);
411 /* release a card refcount for safe disconnection */
413 put_device(&timer->card->card_dev);
414 module_put(timer->module);
421 * close a timer instance
423 int snd_timer_close(struct snd_timer_instance *timeri)
427 if (snd_BUG_ON(!timeri))
430 mutex_lock(®ister_mutex);
431 err = snd_timer_close_locked(timeri);
432 mutex_unlock(®ister_mutex);
435 EXPORT_SYMBOL(snd_timer_close);
437 static unsigned long snd_timer_hw_resolution(struct snd_timer *timer)
439 if (timer->hw.c_resolution)
440 return timer->hw.c_resolution(timer);
442 return timer->hw.resolution;
445 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
447 struct snd_timer * timer;
448 unsigned long ret = 0;
453 timer = timeri->timer;
455 spin_lock_irqsave(&timer->lock, flags);
456 ret = snd_timer_hw_resolution(timer);
457 spin_unlock_irqrestore(&timer->lock, flags);
461 EXPORT_SYMBOL(snd_timer_resolution);
463 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
465 struct snd_timer *timer = ti->timer;
466 unsigned long resolution = 0;
467 struct snd_timer_instance *ts;
468 struct timespec tstamp;
470 if (timer_tstamp_monotonic)
471 ktime_get_ts(&tstamp);
473 getnstimeofday(&tstamp);
474 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
475 event > SNDRV_TIMER_EVENT_PAUSE))
478 (event == SNDRV_TIMER_EVENT_START ||
479 event == SNDRV_TIMER_EVENT_CONTINUE))
480 resolution = snd_timer_hw_resolution(timer);
482 ti->ccallback(ti, event, &tstamp, resolution);
483 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
487 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
489 list_for_each_entry(ts, &ti->slave_active_head, active_list)
491 ts->ccallback(ts, event + 100, &tstamp, resolution);
494 /* start/continue a master timer */
495 static int snd_timer_start1(struct snd_timer_instance *timeri,
496 bool start, unsigned long ticks)
498 struct snd_timer *timer;
502 timer = timeri->timer;
506 spin_lock_irqsave(&timer->lock, flags);
507 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
511 if (timer->card && timer->card->shutdown) {
515 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
516 SNDRV_TIMER_IFLG_START)) {
522 timeri->ticks = timeri->cticks = ticks;
523 else if (!timeri->cticks)
527 list_move_tail(&timeri->active_list, &timer->active_list_head);
528 if (timer->running) {
529 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
531 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
532 timeri->flags |= SNDRV_TIMER_IFLG_START;
533 result = 1; /* delayed start */
536 timer->sticks = ticks;
537 timer->hw.start(timer);
540 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
543 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
544 SNDRV_TIMER_EVENT_CONTINUE);
546 spin_unlock_irqrestore(&timer->lock, flags);
550 /* start/continue a slave timer */
551 static int snd_timer_start_slave(struct snd_timer_instance *timeri,
557 spin_lock_irqsave(&slave_active_lock, flags);
558 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
562 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
566 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
567 if (timeri->master && timeri->timer) {
568 spin_lock(&timeri->timer->lock);
569 list_add_tail(&timeri->active_list,
570 &timeri->master->slave_active_head);
571 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
572 SNDRV_TIMER_EVENT_CONTINUE);
573 spin_unlock(&timeri->timer->lock);
575 err = 1; /* delayed start */
577 spin_unlock_irqrestore(&slave_active_lock, flags);
581 /* stop/pause a master timer */
582 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
584 struct snd_timer *timer;
588 timer = timeri->timer;
591 spin_lock_irqsave(&timer->lock, flags);
592 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
593 SNDRV_TIMER_IFLG_START))) {
597 list_del_init(&timeri->ack_list);
598 list_del_init(&timeri->active_list);
599 if (timer->card && timer->card->shutdown)
602 timeri->cticks = timeri->ticks;
605 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
606 !(--timer->running)) {
607 timer->hw.stop(timer);
608 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
609 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
610 snd_timer_reschedule(timer, 0);
611 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
612 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
613 timer->hw.start(timer);
617 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
619 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
621 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
622 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
623 SNDRV_TIMER_EVENT_PAUSE);
625 spin_unlock_irqrestore(&timer->lock, flags);
629 /* stop/pause a slave timer */
630 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
634 spin_lock_irqsave(&slave_active_lock, flags);
635 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
636 spin_unlock_irqrestore(&slave_active_lock, flags);
639 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
641 spin_lock(&timeri->timer->lock);
642 list_del_init(&timeri->ack_list);
643 list_del_init(&timeri->active_list);
644 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
645 SNDRV_TIMER_EVENT_PAUSE);
646 spin_unlock(&timeri->timer->lock);
648 spin_unlock_irqrestore(&slave_active_lock, flags);
653 * start the timer instance
655 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
657 if (timeri == NULL || ticks < 1)
659 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
660 return snd_timer_start_slave(timeri, true);
662 return snd_timer_start1(timeri, true, ticks);
664 EXPORT_SYMBOL(snd_timer_start);
667 * stop the timer instance.
669 * do not call this from the timer callback!
671 int snd_timer_stop(struct snd_timer_instance *timeri)
673 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
674 return snd_timer_stop_slave(timeri, true);
676 return snd_timer_stop1(timeri, true);
678 EXPORT_SYMBOL(snd_timer_stop);
681 * start again.. the tick is kept.
683 int snd_timer_continue(struct snd_timer_instance *timeri)
685 /* timer can continue only after pause */
686 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
689 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
690 return snd_timer_start_slave(timeri, false);
692 return snd_timer_start1(timeri, false, 0);
694 EXPORT_SYMBOL(snd_timer_continue);
697 * pause.. remember the ticks left
699 int snd_timer_pause(struct snd_timer_instance * timeri)
701 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
702 return snd_timer_stop_slave(timeri, false);
704 return snd_timer_stop1(timeri, false);
706 EXPORT_SYMBOL(snd_timer_pause);
709 * reschedule the timer
711 * start pending instances and check the scheduling ticks.
712 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
714 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
716 struct snd_timer_instance *ti;
717 unsigned long ticks = ~0UL;
719 list_for_each_entry(ti, &timer->active_list_head, active_list) {
720 if (ti->flags & SNDRV_TIMER_IFLG_START) {
721 ti->flags &= ~SNDRV_TIMER_IFLG_START;
722 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
725 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
726 if (ticks > ti->cticks)
731 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
734 if (ticks > timer->hw.ticks)
735 ticks = timer->hw.ticks;
736 if (ticks_left != ticks)
737 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
738 timer->sticks = ticks;
741 /* call callbacks in timer ack list */
742 static void snd_timer_process_callbacks(struct snd_timer *timer,
743 struct list_head *head)
745 struct snd_timer_instance *ti;
746 unsigned long resolution, ticks;
748 while (!list_empty(head)) {
749 ti = list_first_entry(head, struct snd_timer_instance,
752 /* remove from ack_list and make empty */
753 list_del_init(&ti->ack_list);
755 if (!(ti->flags & SNDRV_TIMER_IFLG_DEAD)) {
758 resolution = ti->resolution;
759 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
760 spin_unlock(&timer->lock);
762 ti->callback(ti, resolution, ticks);
763 spin_lock(&timer->lock);
764 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
769 /* clear pending instances from ack list */
770 static void snd_timer_clear_callbacks(struct snd_timer *timer,
771 struct list_head *head)
775 spin_lock_irqsave(&timer->lock, flags);
776 while (!list_empty(head))
777 list_del_init(head->next);
778 spin_unlock_irqrestore(&timer->lock, flags);
785 static void snd_timer_tasklet(unsigned long arg)
787 struct snd_timer *timer = (struct snd_timer *) arg;
790 if (timer->card && timer->card->shutdown) {
791 snd_timer_clear_callbacks(timer, &timer->sack_list_head);
795 spin_lock_irqsave(&timer->lock, flags);
796 snd_timer_process_callbacks(timer, &timer->sack_list_head);
797 spin_unlock_irqrestore(&timer->lock, flags);
803 * ticks_left is usually equal to timer->sticks.
806 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
808 struct snd_timer_instance *ti, *ts, *tmp;
809 unsigned long resolution;
810 struct list_head *ack_list_head;
817 if (timer->card && timer->card->shutdown) {
818 snd_timer_clear_callbacks(timer, &timer->ack_list_head);
822 spin_lock_irqsave(&timer->lock, flags);
824 /* remember the current resolution */
825 resolution = snd_timer_hw_resolution(timer);
827 /* loop for all active instances
828 * Here we cannot use list_for_each_entry because the active_list of a
829 * processed instance is relinked to done_list_head before the callback
832 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
834 if (ti->flags & SNDRV_TIMER_IFLG_DEAD)
836 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
838 ti->pticks += ticks_left;
839 ti->resolution = resolution;
840 if (ti->cticks < ticks_left)
843 ti->cticks -= ticks_left;
844 if (ti->cticks) /* not expired */
846 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
847 ti->cticks = ti->ticks;
849 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
851 list_del_init(&ti->active_list);
853 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
854 (ti->flags & SNDRV_TIMER_IFLG_FAST))
855 ack_list_head = &timer->ack_list_head;
857 ack_list_head = &timer->sack_list_head;
858 if (list_empty(&ti->ack_list))
859 list_add_tail(&ti->ack_list, ack_list_head);
860 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
861 ts->pticks = ti->pticks;
862 ts->resolution = resolution;
863 if (list_empty(&ts->ack_list))
864 list_add_tail(&ts->ack_list, ack_list_head);
867 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
868 snd_timer_reschedule(timer, timer->sticks);
869 if (timer->running) {
870 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
871 timer->hw.stop(timer);
872 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
874 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
875 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
877 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
878 timer->hw.start(timer);
881 timer->hw.stop(timer);
884 /* now process all fast callbacks */
885 snd_timer_process_callbacks(timer, &timer->ack_list_head);
887 /* do we have any slow callbacks? */
888 use_tasklet = !list_empty(&timer->sack_list_head);
889 spin_unlock_irqrestore(&timer->lock, flags);
892 tasklet_schedule(&timer->task_queue);
894 EXPORT_SYMBOL(snd_timer_interrupt);
900 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
901 struct snd_timer **rtimer)
903 struct snd_timer *timer;
905 static struct snd_device_ops ops = {
906 .dev_free = snd_timer_dev_free,
907 .dev_register = snd_timer_dev_register,
908 .dev_disconnect = snd_timer_dev_disconnect,
911 if (snd_BUG_ON(!tid))
913 if (tid->dev_class == SNDRV_TIMER_CLASS_CARD ||
914 tid->dev_class == SNDRV_TIMER_CLASS_PCM) {
920 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
923 timer->tmr_class = tid->dev_class;
925 timer->tmr_device = tid->device;
926 timer->tmr_subdevice = tid->subdevice;
928 strlcpy(timer->id, id, sizeof(timer->id));
930 INIT_LIST_HEAD(&timer->device_list);
931 INIT_LIST_HEAD(&timer->open_list_head);
932 INIT_LIST_HEAD(&timer->active_list_head);
933 INIT_LIST_HEAD(&timer->ack_list_head);
934 INIT_LIST_HEAD(&timer->sack_list_head);
935 spin_lock_init(&timer->lock);
936 tasklet_init(&timer->task_queue, snd_timer_tasklet,
937 (unsigned long)timer);
938 timer->max_instances = 1000; /* default limit per timer */
940 timer->module = card->module;
941 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
943 snd_timer_free(timer);
951 EXPORT_SYMBOL(snd_timer_new);
953 static int snd_timer_free(struct snd_timer *timer)
958 mutex_lock(®ister_mutex);
959 if (! list_empty(&timer->open_list_head)) {
960 struct list_head *p, *n;
961 struct snd_timer_instance *ti;
962 pr_warn("ALSA: timer %p is busy?\n", timer);
963 list_for_each_safe(p, n, &timer->open_list_head) {
965 ti = list_entry(p, struct snd_timer_instance, open_list);
969 list_del(&timer->device_list);
970 mutex_unlock(®ister_mutex);
972 if (timer->private_free)
973 timer->private_free(timer);
978 static int snd_timer_dev_free(struct snd_device *device)
980 struct snd_timer *timer = device->device_data;
981 return snd_timer_free(timer);
984 static int snd_timer_dev_register(struct snd_device *dev)
986 struct snd_timer *timer = dev->device_data;
987 struct snd_timer *timer1;
989 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
991 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
992 !timer->hw.resolution && timer->hw.c_resolution == NULL)
995 mutex_lock(®ister_mutex);
996 list_for_each_entry(timer1, &snd_timer_list, device_list) {
997 if (timer1->tmr_class > timer->tmr_class)
999 if (timer1->tmr_class < timer->tmr_class)
1001 if (timer1->card && timer->card) {
1002 if (timer1->card->number > timer->card->number)
1004 if (timer1->card->number < timer->card->number)
1007 if (timer1->tmr_device > timer->tmr_device)
1009 if (timer1->tmr_device < timer->tmr_device)
1011 if (timer1->tmr_subdevice > timer->tmr_subdevice)
1013 if (timer1->tmr_subdevice < timer->tmr_subdevice)
1016 mutex_unlock(®ister_mutex);
1019 list_add_tail(&timer->device_list, &timer1->device_list);
1020 mutex_unlock(®ister_mutex);
1024 static int snd_timer_dev_disconnect(struct snd_device *device)
1026 struct snd_timer *timer = device->device_data;
1027 struct snd_timer_instance *ti;
1029 mutex_lock(®ister_mutex);
1030 list_del_init(&timer->device_list);
1031 /* wake up pending sleepers */
1032 list_for_each_entry(ti, &timer->open_list_head, open_list) {
1036 mutex_unlock(®ister_mutex);
1040 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
1042 unsigned long flags;
1043 unsigned long resolution = 0;
1044 struct snd_timer_instance *ti, *ts;
1046 if (timer->card && timer->card->shutdown)
1048 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1050 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1051 event > SNDRV_TIMER_EVENT_MRESUME))
1053 spin_lock_irqsave(&timer->lock, flags);
1054 if (event == SNDRV_TIMER_EVENT_MSTART ||
1055 event == SNDRV_TIMER_EVENT_MCONTINUE ||
1056 event == SNDRV_TIMER_EVENT_MRESUME)
1057 resolution = snd_timer_hw_resolution(timer);
1058 list_for_each_entry(ti, &timer->active_list_head, active_list) {
1060 ti->ccallback(ti, event, tstamp, resolution);
1061 list_for_each_entry(ts, &ti->slave_active_head, active_list)
1063 ts->ccallback(ts, event, tstamp, resolution);
1065 spin_unlock_irqrestore(&timer->lock, flags);
1067 EXPORT_SYMBOL(snd_timer_notify);
1070 * exported functions for global timers
1072 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1074 struct snd_timer_id tid;
1076 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1077 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1079 tid.device = device;
1081 return snd_timer_new(NULL, id, &tid, rtimer);
1083 EXPORT_SYMBOL(snd_timer_global_new);
1085 int snd_timer_global_free(struct snd_timer *timer)
1087 return snd_timer_free(timer);
1089 EXPORT_SYMBOL(snd_timer_global_free);
1091 int snd_timer_global_register(struct snd_timer *timer)
1093 struct snd_device dev;
1095 memset(&dev, 0, sizeof(dev));
1096 dev.device_data = timer;
1097 return snd_timer_dev_register(&dev);
1099 EXPORT_SYMBOL(snd_timer_global_register);
1105 struct snd_timer_system_private {
1106 struct timer_list tlist;
1107 struct snd_timer *snd_timer;
1108 unsigned long last_expires;
1109 unsigned long last_jiffies;
1110 unsigned long correction;
1113 static void snd_timer_s_function(struct timer_list *t)
1115 struct snd_timer_system_private *priv = from_timer(priv, t,
1117 struct snd_timer *timer = priv->snd_timer;
1118 unsigned long jiff = jiffies;
1119 if (time_after(jiff, priv->last_expires))
1120 priv->correction += (long)jiff - (long)priv->last_expires;
1121 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1124 static int snd_timer_s_start(struct snd_timer * timer)
1126 struct snd_timer_system_private *priv;
1127 unsigned long njiff;
1129 priv = (struct snd_timer_system_private *) timer->private_data;
1130 njiff = (priv->last_jiffies = jiffies);
1131 if (priv->correction > timer->sticks - 1) {
1132 priv->correction -= timer->sticks - 1;
1135 njiff += timer->sticks - priv->correction;
1136 priv->correction = 0;
1138 priv->last_expires = njiff;
1139 mod_timer(&priv->tlist, njiff);
1143 static int snd_timer_s_stop(struct snd_timer * timer)
1145 struct snd_timer_system_private *priv;
1148 priv = (struct snd_timer_system_private *) timer->private_data;
1149 del_timer(&priv->tlist);
1151 if (time_before(jiff, priv->last_expires))
1152 timer->sticks = priv->last_expires - jiff;
1155 priv->correction = 0;
1159 static int snd_timer_s_close(struct snd_timer *timer)
1161 struct snd_timer_system_private *priv;
1163 priv = (struct snd_timer_system_private *)timer->private_data;
1164 del_timer_sync(&priv->tlist);
1168 static struct snd_timer_hardware snd_timer_system =
1170 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1171 .resolution = 1000000000L / HZ,
1173 .close = snd_timer_s_close,
1174 .start = snd_timer_s_start,
1175 .stop = snd_timer_s_stop
1178 static void snd_timer_free_system(struct snd_timer *timer)
1180 kfree(timer->private_data);
1183 static int snd_timer_register_system(void)
1185 struct snd_timer *timer;
1186 struct snd_timer_system_private *priv;
1189 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1192 strcpy(timer->name, "system timer");
1193 timer->hw = snd_timer_system;
1194 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1196 snd_timer_free(timer);
1199 priv->snd_timer = timer;
1200 timer_setup(&priv->tlist, snd_timer_s_function, 0);
1201 timer->private_data = priv;
1202 timer->private_free = snd_timer_free_system;
1203 return snd_timer_global_register(timer);
1206 #ifdef CONFIG_SND_PROC_FS
1211 static void snd_timer_proc_read(struct snd_info_entry *entry,
1212 struct snd_info_buffer *buffer)
1214 struct snd_timer *timer;
1215 struct snd_timer_instance *ti;
1217 mutex_lock(®ister_mutex);
1218 list_for_each_entry(timer, &snd_timer_list, device_list) {
1219 if (timer->card && timer->card->shutdown)
1221 switch (timer->tmr_class) {
1222 case SNDRV_TIMER_CLASS_GLOBAL:
1223 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1225 case SNDRV_TIMER_CLASS_CARD:
1226 snd_iprintf(buffer, "C%i-%i: ",
1227 timer->card->number, timer->tmr_device);
1229 case SNDRV_TIMER_CLASS_PCM:
1230 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1231 timer->tmr_device, timer->tmr_subdevice);
1234 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1235 timer->card ? timer->card->number : -1,
1236 timer->tmr_device, timer->tmr_subdevice);
1238 snd_iprintf(buffer, "%s :", timer->name);
1239 if (timer->hw.resolution)
1240 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1241 timer->hw.resolution / 1000,
1242 timer->hw.resolution % 1000,
1244 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1245 snd_iprintf(buffer, " SLAVE");
1246 snd_iprintf(buffer, "\n");
1247 list_for_each_entry(ti, &timer->open_list_head, open_list)
1248 snd_iprintf(buffer, " Client %s : %s\n",
1249 ti->owner ? ti->owner : "unknown",
1250 ti->flags & (SNDRV_TIMER_IFLG_START |
1251 SNDRV_TIMER_IFLG_RUNNING)
1252 ? "running" : "stopped");
1254 mutex_unlock(®ister_mutex);
1257 static struct snd_info_entry *snd_timer_proc_entry;
1259 static void __init snd_timer_proc_init(void)
1261 struct snd_info_entry *entry;
1263 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1264 if (entry != NULL) {
1265 entry->c.text.read = snd_timer_proc_read;
1266 if (snd_info_register(entry) < 0) {
1267 snd_info_free_entry(entry);
1271 snd_timer_proc_entry = entry;
1274 static void __exit snd_timer_proc_done(void)
1276 snd_info_free_entry(snd_timer_proc_entry);
1278 #else /* !CONFIG_SND_PROC_FS */
1279 #define snd_timer_proc_init()
1280 #define snd_timer_proc_done()
1284 * USER SPACE interface
1287 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1288 unsigned long resolution,
1289 unsigned long ticks)
1291 struct snd_timer_user *tu = timeri->callback_data;
1292 struct snd_timer_read *r;
1295 spin_lock(&tu->qlock);
1296 if (tu->qused > 0) {
1297 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1298 r = &tu->queue[prev];
1299 if (r->resolution == resolution) {
1304 if (tu->qused >= tu->queue_size) {
1307 r = &tu->queue[tu->qtail++];
1308 tu->qtail %= tu->queue_size;
1309 r->resolution = resolution;
1314 spin_unlock(&tu->qlock);
1315 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1316 wake_up(&tu->qchange_sleep);
1319 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1320 struct snd_timer_tread *tread)
1322 if (tu->qused >= tu->queue_size) {
1325 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1326 tu->qtail %= tu->queue_size;
1331 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1333 struct timespec *tstamp,
1334 unsigned long resolution)
1336 struct snd_timer_user *tu = timeri->callback_data;
1337 struct snd_timer_tread r1;
1338 unsigned long flags;
1340 if (event >= SNDRV_TIMER_EVENT_START &&
1341 event <= SNDRV_TIMER_EVENT_PAUSE)
1342 tu->tstamp = *tstamp;
1343 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1345 memset(&r1, 0, sizeof(r1));
1347 r1.tstamp = *tstamp;
1348 r1.val = resolution;
1349 spin_lock_irqsave(&tu->qlock, flags);
1350 snd_timer_user_append_to_tqueue(tu, &r1);
1351 spin_unlock_irqrestore(&tu->qlock, flags);
1352 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1353 wake_up(&tu->qchange_sleep);
1356 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1358 struct snd_timer_user *tu = timeri->callback_data;
1360 tu->disconnected = true;
1361 wake_up(&tu->qchange_sleep);
1364 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1365 unsigned long resolution,
1366 unsigned long ticks)
1368 struct snd_timer_user *tu = timeri->callback_data;
1369 struct snd_timer_tread *r, r1;
1370 struct timespec tstamp;
1371 int prev, append = 0;
1373 memset(&r1, 0, sizeof(r1));
1374 memset(&tstamp, 0, sizeof(tstamp));
1375 spin_lock(&tu->qlock);
1376 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1377 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1378 spin_unlock(&tu->qlock);
1381 if (tu->last_resolution != resolution || ticks > 0) {
1382 if (timer_tstamp_monotonic)
1383 ktime_get_ts(&tstamp);
1385 getnstimeofday(&tstamp);
1387 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1388 tu->last_resolution != resolution) {
1389 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1391 r1.val = resolution;
1392 snd_timer_user_append_to_tqueue(tu, &r1);
1393 tu->last_resolution = resolution;
1396 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1400 if (tu->qused > 0) {
1401 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1402 r = &tu->tqueue[prev];
1403 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1410 r1.event = SNDRV_TIMER_EVENT_TICK;
1413 snd_timer_user_append_to_tqueue(tu, &r1);
1416 spin_unlock(&tu->qlock);
1419 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1420 wake_up(&tu->qchange_sleep);
1423 static int realloc_user_queue(struct snd_timer_user *tu, int size)
1425 struct snd_timer_read *queue = NULL;
1426 struct snd_timer_tread *tqueue = NULL;
1429 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
1433 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
1438 spin_lock_irq(&tu->qlock);
1441 tu->queue_size = size;
1443 tu->tqueue = tqueue;
1444 tu->qhead = tu->qtail = tu->qused = 0;
1445 spin_unlock_irq(&tu->qlock);
1450 static int snd_timer_user_open(struct inode *inode, struct file *file)
1452 struct snd_timer_user *tu;
1455 err = stream_open(inode, file);
1459 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1462 spin_lock_init(&tu->qlock);
1463 init_waitqueue_head(&tu->qchange_sleep);
1464 mutex_init(&tu->ioctl_lock);
1466 if (realloc_user_queue(tu, 128) < 0) {
1470 file->private_data = tu;
1474 static int snd_timer_user_release(struct inode *inode, struct file *file)
1476 struct snd_timer_user *tu;
1478 if (file->private_data) {
1479 tu = file->private_data;
1480 file->private_data = NULL;
1481 mutex_lock(&tu->ioctl_lock);
1483 snd_timer_close(tu->timeri);
1484 mutex_unlock(&tu->ioctl_lock);
1492 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1494 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1495 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1501 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1503 id->dev_class = timer->tmr_class;
1504 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1505 id->card = timer->card ? timer->card->number : -1;
1506 id->device = timer->tmr_device;
1507 id->subdevice = timer->tmr_subdevice;
1510 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1512 struct snd_timer_id id;
1513 struct snd_timer *timer;
1514 struct list_head *p;
1516 if (copy_from_user(&id, _tid, sizeof(id)))
1518 mutex_lock(®ister_mutex);
1519 if (id.dev_class < 0) { /* first item */
1520 if (list_empty(&snd_timer_list))
1521 snd_timer_user_zero_id(&id);
1523 timer = list_entry(snd_timer_list.next,
1524 struct snd_timer, device_list);
1525 snd_timer_user_copy_id(&id, timer);
1528 switch (id.dev_class) {
1529 case SNDRV_TIMER_CLASS_GLOBAL:
1530 id.device = id.device < 0 ? 0 : id.device + 1;
1531 list_for_each(p, &snd_timer_list) {
1532 timer = list_entry(p, struct snd_timer, device_list);
1533 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1534 snd_timer_user_copy_id(&id, timer);
1537 if (timer->tmr_device >= id.device) {
1538 snd_timer_user_copy_id(&id, timer);
1542 if (p == &snd_timer_list)
1543 snd_timer_user_zero_id(&id);
1545 case SNDRV_TIMER_CLASS_CARD:
1546 case SNDRV_TIMER_CLASS_PCM:
1550 if (id.device < 0) {
1553 if (id.subdevice < 0)
1555 else if (id.subdevice < INT_MAX)
1559 list_for_each(p, &snd_timer_list) {
1560 timer = list_entry(p, struct snd_timer, device_list);
1561 if (timer->tmr_class > id.dev_class) {
1562 snd_timer_user_copy_id(&id, timer);
1565 if (timer->tmr_class < id.dev_class)
1567 if (timer->card->number > id.card) {
1568 snd_timer_user_copy_id(&id, timer);
1571 if (timer->card->number < id.card)
1573 if (timer->tmr_device > id.device) {
1574 snd_timer_user_copy_id(&id, timer);
1577 if (timer->tmr_device < id.device)
1579 if (timer->tmr_subdevice > id.subdevice) {
1580 snd_timer_user_copy_id(&id, timer);
1583 if (timer->tmr_subdevice < id.subdevice)
1585 snd_timer_user_copy_id(&id, timer);
1588 if (p == &snd_timer_list)
1589 snd_timer_user_zero_id(&id);
1592 snd_timer_user_zero_id(&id);
1595 mutex_unlock(®ister_mutex);
1596 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1601 static int snd_timer_user_ginfo(struct file *file,
1602 struct snd_timer_ginfo __user *_ginfo)
1604 struct snd_timer_ginfo *ginfo;
1605 struct snd_timer_id tid;
1606 struct snd_timer *t;
1607 struct list_head *p;
1610 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1612 return PTR_ERR(ginfo);
1615 memset(ginfo, 0, sizeof(*ginfo));
1617 mutex_lock(®ister_mutex);
1618 t = snd_timer_find(&tid);
1620 ginfo->card = t->card ? t->card->number : -1;
1621 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1622 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1623 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1624 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1625 ginfo->resolution = t->hw.resolution;
1626 if (t->hw.resolution_min > 0) {
1627 ginfo->resolution_min = t->hw.resolution_min;
1628 ginfo->resolution_max = t->hw.resolution_max;
1630 list_for_each(p, &t->open_list_head) {
1636 mutex_unlock(®ister_mutex);
1637 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1643 static int timer_set_gparams(struct snd_timer_gparams *gparams)
1645 struct snd_timer *t;
1648 mutex_lock(®ister_mutex);
1649 t = snd_timer_find(&gparams->tid);
1654 if (!list_empty(&t->open_list_head)) {
1658 if (!t->hw.set_period) {
1662 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
1664 mutex_unlock(®ister_mutex);
1668 static int snd_timer_user_gparams(struct file *file,
1669 struct snd_timer_gparams __user *_gparams)
1671 struct snd_timer_gparams gparams;
1673 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1675 return timer_set_gparams(&gparams);
1678 static int snd_timer_user_gstatus(struct file *file,
1679 struct snd_timer_gstatus __user *_gstatus)
1681 struct snd_timer_gstatus gstatus;
1682 struct snd_timer_id tid;
1683 struct snd_timer *t;
1686 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1689 memset(&gstatus, 0, sizeof(gstatus));
1691 mutex_lock(®ister_mutex);
1692 t = snd_timer_find(&tid);
1694 spin_lock_irq(&t->lock);
1695 gstatus.resolution = snd_timer_hw_resolution(t);
1696 if (t->hw.precise_resolution) {
1697 t->hw.precise_resolution(t, &gstatus.resolution_num,
1698 &gstatus.resolution_den);
1700 gstatus.resolution_num = gstatus.resolution;
1701 gstatus.resolution_den = 1000000000uL;
1703 spin_unlock_irq(&t->lock);
1707 mutex_unlock(®ister_mutex);
1708 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1713 static int snd_timer_user_tselect(struct file *file,
1714 struct snd_timer_select __user *_tselect)
1716 struct snd_timer_user *tu;
1717 struct snd_timer_select tselect;
1721 tu = file->private_data;
1723 snd_timer_close(tu->timeri);
1726 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1730 sprintf(str, "application %i", current->pid);
1731 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1732 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1733 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1737 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1738 tu->timeri->callback = tu->tread
1739 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1740 tu->timeri->ccallback = snd_timer_user_ccallback;
1741 tu->timeri->callback_data = (void *)tu;
1742 tu->timeri->disconnect = snd_timer_user_disconnect;
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)