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
42 #if IS_ENABLED(CONFIG_SND_HRTIMER)
43 #define DEFAULT_TIMER_LIMIT 4
45 #define DEFAULT_TIMER_LIMIT 1
48 static int timer_limit = DEFAULT_TIMER_LIMIT;
49 static int timer_tstamp_monotonic = 1;
50 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
51 MODULE_DESCRIPTION("ALSA timer interface");
52 MODULE_LICENSE("GPL");
53 module_param(timer_limit, int, 0444);
54 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
55 module_param(timer_tstamp_monotonic, int, 0444);
56 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
58 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
59 MODULE_ALIAS("devname:snd/timer");
61 struct snd_timer_user {
62 struct snd_timer_instance *timeri;
63 int tread; /* enhanced read with timestamps and events */
65 unsigned long overrun;
71 struct snd_timer_read *queue;
72 struct snd_timer_tread *tqueue;
74 unsigned long last_resolution;
76 struct timespec tstamp; /* trigger tstamp */
77 wait_queue_head_t qchange_sleep;
78 struct snd_fasync *fasync;
79 struct mutex ioctl_lock;
83 static LIST_HEAD(snd_timer_list);
85 /* list of slave instances */
86 static LIST_HEAD(snd_timer_slave_list);
88 /* lock for slave active lists */
89 static DEFINE_SPINLOCK(slave_active_lock);
91 #define MAX_SLAVE_INSTANCES 1000
92 static int num_slaves;
94 static DEFINE_MUTEX(register_mutex);
96 static int snd_timer_free(struct snd_timer *timer);
97 static int snd_timer_dev_free(struct snd_device *device);
98 static int snd_timer_dev_register(struct snd_device *device);
99 static int snd_timer_dev_disconnect(struct snd_device *device);
101 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
104 * create a timer instance with the given owner string.
105 * when timer is not NULL, increments the module counter
107 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
108 struct snd_timer *timer)
110 struct snd_timer_instance *timeri;
111 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
114 timeri->owner = kstrdup(owner, GFP_KERNEL);
115 if (! timeri->owner) {
119 INIT_LIST_HEAD(&timeri->open_list);
120 INIT_LIST_HEAD(&timeri->active_list);
121 INIT_LIST_HEAD(&timeri->ack_list);
122 INIT_LIST_HEAD(&timeri->slave_list_head);
123 INIT_LIST_HEAD(&timeri->slave_active_head);
125 timeri->timer = timer;
126 if (timer && !try_module_get(timer->module)) {
127 kfree(timeri->owner);
136 * find a timer instance from the given timer id
138 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
140 struct snd_timer *timer = NULL;
142 list_for_each_entry(timer, &snd_timer_list, device_list) {
143 if (timer->tmr_class != tid->dev_class)
145 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
146 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
147 (timer->card == NULL ||
148 timer->card->number != tid->card))
150 if (timer->tmr_device != tid->device)
152 if (timer->tmr_subdevice != tid->subdevice)
159 #ifdef CONFIG_MODULES
161 static void snd_timer_request(struct snd_timer_id *tid)
163 switch (tid->dev_class) {
164 case SNDRV_TIMER_CLASS_GLOBAL:
165 if (tid->device < timer_limit)
166 request_module("snd-timer-%i", tid->device);
168 case SNDRV_TIMER_CLASS_CARD:
169 case SNDRV_TIMER_CLASS_PCM:
170 if (tid->card < snd_ecards_limit)
171 request_module("snd-card-%i", tid->card);
181 * look for a master instance matching with the slave id of the given slave.
182 * when found, relink the open_link of the slave.
184 * call this with register_mutex down.
186 static int snd_timer_check_slave(struct snd_timer_instance *slave)
188 struct snd_timer *timer;
189 struct snd_timer_instance *master;
191 /* FIXME: it's really dumb to look up all entries.. */
192 list_for_each_entry(timer, &snd_timer_list, device_list) {
193 list_for_each_entry(master, &timer->open_list_head, open_list) {
194 if (slave->slave_class == master->slave_class &&
195 slave->slave_id == master->slave_id) {
196 if (master->timer->num_instances >=
197 master->timer->max_instances)
199 list_move_tail(&slave->open_list,
200 &master->slave_list_head);
201 master->timer->num_instances++;
202 spin_lock_irq(&slave_active_lock);
203 slave->master = master;
204 slave->timer = master->timer;
205 spin_unlock_irq(&slave_active_lock);
214 * look for slave instances matching with the slave id of the given master.
215 * when found, relink the open_link of slaves.
217 * call this with register_mutex down.
219 static int snd_timer_check_master(struct snd_timer_instance *master)
221 struct snd_timer_instance *slave, *tmp;
223 /* check all pending slaves */
224 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
225 if (slave->slave_class == master->slave_class &&
226 slave->slave_id == master->slave_id) {
227 if (master->timer->num_instances >=
228 master->timer->max_instances)
230 list_move_tail(&slave->open_list, &master->slave_list_head);
231 master->timer->num_instances++;
232 spin_lock_irq(&slave_active_lock);
233 spin_lock(&master->timer->lock);
234 slave->master = master;
235 slave->timer = master->timer;
236 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
237 list_add_tail(&slave->active_list,
238 &master->slave_active_head);
239 spin_unlock(&master->timer->lock);
240 spin_unlock_irq(&slave_active_lock);
246 static int snd_timer_close_locked(struct snd_timer_instance *timeri,
247 struct device **card_devp_to_put);
250 * open a timer instance
251 * when opening a master, the slave id must be here given.
253 int snd_timer_open(struct snd_timer_instance **ti,
254 char *owner, struct snd_timer_id *tid,
255 unsigned int slave_id)
257 struct snd_timer *timer;
258 struct snd_timer_instance *timeri = NULL;
259 struct device *card_dev_to_put = NULL;
262 mutex_lock(®ister_mutex);
263 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
264 /* open a slave instance */
265 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
266 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
267 pr_debug("ALSA: timer: invalid slave class %i\n",
272 if (num_slaves >= MAX_SLAVE_INSTANCES) {
276 timeri = snd_timer_instance_new(owner, NULL);
281 timeri->slave_class = tid->dev_sclass;
282 timeri->slave_id = tid->device;
283 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
284 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
286 err = snd_timer_check_slave(timeri);
288 snd_timer_close_locked(timeri, &card_dev_to_put);
294 /* open a master instance */
295 timer = snd_timer_find(tid);
296 #ifdef CONFIG_MODULES
298 mutex_unlock(®ister_mutex);
299 snd_timer_request(tid);
300 mutex_lock(®ister_mutex);
301 timer = snd_timer_find(tid);
308 if (!list_empty(&timer->open_list_head)) {
309 struct snd_timer_instance *t =
310 list_entry(timer->open_list_head.next,
311 struct snd_timer_instance, open_list);
312 if (t->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
317 if (timer->num_instances >= timer->max_instances) {
321 timeri = snd_timer_instance_new(owner, timer);
326 /* take a card refcount for safe disconnection */
328 get_device(&timer->card->card_dev);
329 timeri->slave_class = tid->dev_sclass;
330 timeri->slave_id = slave_id;
332 if (list_empty(&timer->open_list_head) && timer->hw.open) {
333 err = timer->hw.open(timer);
335 kfree(timeri->owner);
340 card_dev_to_put = &timer->card->card_dev;
341 module_put(timer->module);
346 list_add_tail(&timeri->open_list, &timer->open_list_head);
347 timer->num_instances++;
348 err = snd_timer_check_master(timeri);
350 snd_timer_close_locked(timeri, &card_dev_to_put);
355 mutex_unlock(®ister_mutex);
356 /* put_device() is called after unlock for avoiding deadlock */
358 put_device(card_dev_to_put);
362 EXPORT_SYMBOL(snd_timer_open);
365 * close a timer instance
366 * call this with register_mutex down.
368 static int snd_timer_close_locked(struct snd_timer_instance *timeri,
369 struct device **card_devp_to_put)
371 struct snd_timer *timer = NULL;
372 struct snd_timer_instance *slave, *tmp;
374 list_del(&timeri->open_list);
375 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
378 /* force to stop the timer */
379 snd_timer_stop(timeri);
381 timer = timeri->timer;
383 timer->num_instances--;
384 /* wait, until the active callback is finished */
385 spin_lock_irq(&timer->lock);
386 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
387 spin_unlock_irq(&timer->lock);
389 spin_lock_irq(&timer->lock);
391 spin_unlock_irq(&timer->lock);
393 /* remove slave links */
394 spin_lock_irq(&slave_active_lock);
395 spin_lock(&timer->lock);
396 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
398 list_move_tail(&slave->open_list, &snd_timer_slave_list);
399 timer->num_instances--;
400 slave->master = NULL;
402 list_del_init(&slave->ack_list);
403 list_del_init(&slave->active_list);
405 spin_unlock(&timer->lock);
406 spin_unlock_irq(&slave_active_lock);
408 /* slave doesn't need to release timer resources below */
409 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
413 if (timeri->private_free)
414 timeri->private_free(timeri);
415 kfree(timeri->owner);
419 if (list_empty(&timer->open_list_head) && timer->hw.close)
420 timer->hw.close(timer);
421 /* release a card refcount for safe disconnection */
423 *card_devp_to_put = &timer->card->card_dev;
424 module_put(timer->module);
431 * close a timer instance
433 int snd_timer_close(struct snd_timer_instance *timeri)
435 struct device *card_dev_to_put = NULL;
438 if (snd_BUG_ON(!timeri))
441 mutex_lock(®ister_mutex);
442 err = snd_timer_close_locked(timeri, &card_dev_to_put);
443 mutex_unlock(®ister_mutex);
444 /* put_device() is called after unlock for avoiding deadlock */
446 put_device(card_dev_to_put);
449 EXPORT_SYMBOL(snd_timer_close);
451 static unsigned long snd_timer_hw_resolution(struct snd_timer *timer)
453 if (timer->hw.c_resolution)
454 return timer->hw.c_resolution(timer);
456 return timer->hw.resolution;
459 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
461 struct snd_timer * timer;
462 unsigned long ret = 0;
467 timer = timeri->timer;
469 spin_lock_irqsave(&timer->lock, flags);
470 ret = snd_timer_hw_resolution(timer);
471 spin_unlock_irqrestore(&timer->lock, flags);
475 EXPORT_SYMBOL(snd_timer_resolution);
477 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
479 struct snd_timer *timer = ti->timer;
480 unsigned long resolution = 0;
481 struct snd_timer_instance *ts;
482 struct timespec tstamp;
484 if (timer_tstamp_monotonic)
485 ktime_get_ts(&tstamp);
487 getnstimeofday(&tstamp);
488 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
489 event > SNDRV_TIMER_EVENT_PAUSE))
492 (event == SNDRV_TIMER_EVENT_START ||
493 event == SNDRV_TIMER_EVENT_CONTINUE))
494 resolution = snd_timer_hw_resolution(timer);
496 ti->ccallback(ti, event, &tstamp, resolution);
497 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
501 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
503 event += 10; /* convert to SNDRV_TIMER_EVENT_MXXX */
504 list_for_each_entry(ts, &ti->slave_active_head, active_list)
506 ts->ccallback(ts, event, &tstamp, resolution);
509 /* start/continue a master timer */
510 static int snd_timer_start1(struct snd_timer_instance *timeri,
511 bool start, unsigned long ticks)
513 struct snd_timer *timer;
517 timer = timeri->timer;
521 spin_lock_irqsave(&timer->lock, flags);
522 if (timer->card && timer->card->shutdown) {
526 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
527 SNDRV_TIMER_IFLG_START)) {
533 timeri->ticks = timeri->cticks = ticks;
534 else if (!timeri->cticks)
538 list_move_tail(&timeri->active_list, &timer->active_list_head);
539 if (timer->running) {
540 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
542 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
543 timeri->flags |= SNDRV_TIMER_IFLG_START;
544 result = 1; /* delayed start */
547 timer->sticks = ticks;
548 timer->hw.start(timer);
551 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
554 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
555 SNDRV_TIMER_EVENT_CONTINUE);
557 spin_unlock_irqrestore(&timer->lock, flags);
561 /* start/continue a slave timer */
562 static int snd_timer_start_slave(struct snd_timer_instance *timeri,
567 spin_lock_irqsave(&slave_active_lock, flags);
568 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
569 spin_unlock_irqrestore(&slave_active_lock, flags);
572 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
573 if (timeri->master && timeri->timer) {
574 spin_lock(&timeri->timer->lock);
575 list_add_tail(&timeri->active_list,
576 &timeri->master->slave_active_head);
577 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
578 SNDRV_TIMER_EVENT_CONTINUE);
579 spin_unlock(&timeri->timer->lock);
581 spin_unlock_irqrestore(&slave_active_lock, flags);
582 return 1; /* delayed start */
585 /* stop/pause a master timer */
586 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
588 struct snd_timer *timer;
592 timer = timeri->timer;
595 spin_lock_irqsave(&timer->lock, flags);
596 list_del_init(&timeri->ack_list);
597 list_del_init(&timeri->active_list);
598 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
599 SNDRV_TIMER_IFLG_START))) {
603 if (timer->card && timer->card->shutdown)
606 timeri->cticks = timeri->ticks;
609 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
610 !(--timer->running)) {
611 timer->hw.stop(timer);
612 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
613 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
614 snd_timer_reschedule(timer, 0);
615 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
616 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
617 timer->hw.start(timer);
621 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
623 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
625 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
626 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
627 SNDRV_TIMER_EVENT_PAUSE);
629 spin_unlock_irqrestore(&timer->lock, flags);
633 /* stop/pause a slave timer */
634 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
639 spin_lock_irqsave(&slave_active_lock, flags);
640 running = timeri->flags & SNDRV_TIMER_IFLG_RUNNING;
641 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
643 spin_lock(&timeri->timer->lock);
644 list_del_init(&timeri->ack_list);
645 list_del_init(&timeri->active_list);
647 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
648 SNDRV_TIMER_EVENT_PAUSE);
649 spin_unlock(&timeri->timer->lock);
651 spin_unlock_irqrestore(&slave_active_lock, flags);
652 return running ? 0 : -EBUSY;
656 * start the timer instance
658 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
660 if (timeri == NULL || ticks < 1)
662 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
663 return snd_timer_start_slave(timeri, true);
665 return snd_timer_start1(timeri, true, ticks);
667 EXPORT_SYMBOL(snd_timer_start);
670 * stop the timer instance.
672 * do not call this from the timer callback!
674 int snd_timer_stop(struct snd_timer_instance *timeri)
676 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
677 return snd_timer_stop_slave(timeri, true);
679 return snd_timer_stop1(timeri, true);
681 EXPORT_SYMBOL(snd_timer_stop);
684 * start again.. the tick is kept.
686 int snd_timer_continue(struct snd_timer_instance *timeri)
688 /* timer can continue only after pause */
689 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
692 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
693 return snd_timer_start_slave(timeri, false);
695 return snd_timer_start1(timeri, false, 0);
697 EXPORT_SYMBOL(snd_timer_continue);
700 * pause.. remember the ticks left
702 int snd_timer_pause(struct snd_timer_instance * timeri)
704 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
705 return snd_timer_stop_slave(timeri, false);
707 return snd_timer_stop1(timeri, false);
709 EXPORT_SYMBOL(snd_timer_pause);
712 * reschedule the timer
714 * start pending instances and check the scheduling ticks.
715 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
717 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
719 struct snd_timer_instance *ti;
720 unsigned long ticks = ~0UL;
722 list_for_each_entry(ti, &timer->active_list_head, active_list) {
723 if (ti->flags & SNDRV_TIMER_IFLG_START) {
724 ti->flags &= ~SNDRV_TIMER_IFLG_START;
725 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
728 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
729 if (ticks > ti->cticks)
734 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
737 if (ticks > timer->hw.ticks)
738 ticks = timer->hw.ticks;
739 if (ticks_left != ticks)
740 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
741 timer->sticks = ticks;
748 static void snd_timer_tasklet(unsigned long arg)
750 struct snd_timer *timer = (struct snd_timer *) arg;
751 struct snd_timer_instance *ti;
753 unsigned long resolution, ticks;
756 if (timer->card && timer->card->shutdown)
759 spin_lock_irqsave(&timer->lock, flags);
760 /* now process all callbacks */
761 while (!list_empty(&timer->sack_list_head)) {
762 p = timer->sack_list_head.next; /* get first item */
763 ti = list_entry(p, struct snd_timer_instance, ack_list);
765 /* remove from ack_list and make empty */
770 resolution = ti->resolution;
772 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
773 spin_unlock(&timer->lock);
775 ti->callback(ti, resolution, ticks);
776 spin_lock(&timer->lock);
777 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
779 spin_unlock_irqrestore(&timer->lock, flags);
785 * ticks_left is usually equal to timer->sticks.
788 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
790 struct snd_timer_instance *ti, *ts, *tmp;
791 unsigned long resolution, ticks;
792 struct list_head *p, *ack_list_head;
799 if (timer->card && timer->card->shutdown)
802 spin_lock_irqsave(&timer->lock, flags);
804 /* remember the current resolution */
805 resolution = snd_timer_hw_resolution(timer);
807 /* loop for all active instances
808 * Here we cannot use list_for_each_entry because the active_list of a
809 * processed instance is relinked to done_list_head before the callback
812 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
814 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
816 ti->pticks += ticks_left;
817 ti->resolution = resolution;
818 if (ti->cticks < ticks_left)
821 ti->cticks -= ticks_left;
822 if (ti->cticks) /* not expired */
824 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
825 ti->cticks = ti->ticks;
827 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
829 list_del_init(&ti->active_list);
831 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
832 (ti->flags & SNDRV_TIMER_IFLG_FAST))
833 ack_list_head = &timer->ack_list_head;
835 ack_list_head = &timer->sack_list_head;
836 if (list_empty(&ti->ack_list))
837 list_add_tail(&ti->ack_list, ack_list_head);
838 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
839 ts->pticks = ti->pticks;
840 ts->resolution = resolution;
841 if (list_empty(&ts->ack_list))
842 list_add_tail(&ts->ack_list, ack_list_head);
845 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
846 snd_timer_reschedule(timer, timer->sticks);
847 if (timer->running) {
848 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
849 timer->hw.stop(timer);
850 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
852 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
853 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
855 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
856 timer->hw.start(timer);
859 timer->hw.stop(timer);
862 /* now process all fast callbacks */
863 while (!list_empty(&timer->ack_list_head)) {
864 p = timer->ack_list_head.next; /* get first item */
865 ti = list_entry(p, struct snd_timer_instance, ack_list);
867 /* remove from ack_list and make empty */
873 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
874 spin_unlock(&timer->lock);
876 ti->callback(ti, resolution, ticks);
877 spin_lock(&timer->lock);
878 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
881 /* do we have any slow callbacks? */
882 use_tasklet = !list_empty(&timer->sack_list_head);
883 spin_unlock_irqrestore(&timer->lock, flags);
886 tasklet_schedule(&timer->task_queue);
888 EXPORT_SYMBOL(snd_timer_interrupt);
894 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
895 struct snd_timer **rtimer)
897 struct snd_timer *timer;
899 static struct snd_device_ops ops = {
900 .dev_free = snd_timer_dev_free,
901 .dev_register = snd_timer_dev_register,
902 .dev_disconnect = snd_timer_dev_disconnect,
905 if (snd_BUG_ON(!tid))
907 if (tid->dev_class == SNDRV_TIMER_CLASS_CARD ||
908 tid->dev_class == SNDRV_TIMER_CLASS_PCM) {
914 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
917 timer->tmr_class = tid->dev_class;
919 timer->tmr_device = tid->device;
920 timer->tmr_subdevice = tid->subdevice;
922 strlcpy(timer->id, id, sizeof(timer->id));
924 INIT_LIST_HEAD(&timer->device_list);
925 INIT_LIST_HEAD(&timer->open_list_head);
926 INIT_LIST_HEAD(&timer->active_list_head);
927 INIT_LIST_HEAD(&timer->ack_list_head);
928 INIT_LIST_HEAD(&timer->sack_list_head);
929 spin_lock_init(&timer->lock);
930 tasklet_init(&timer->task_queue, snd_timer_tasklet,
931 (unsigned long)timer);
932 timer->max_instances = 1000; /* default limit per timer */
934 timer->module = card->module;
935 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
937 snd_timer_free(timer);
945 EXPORT_SYMBOL(snd_timer_new);
947 static int snd_timer_free(struct snd_timer *timer)
952 mutex_lock(®ister_mutex);
953 if (! list_empty(&timer->open_list_head)) {
954 struct list_head *p, *n;
955 struct snd_timer_instance *ti;
956 pr_warn("ALSA: timer %p is busy?\n", timer);
957 list_for_each_safe(p, n, &timer->open_list_head) {
959 ti = list_entry(p, struct snd_timer_instance, open_list);
963 list_del(&timer->device_list);
964 mutex_unlock(®ister_mutex);
966 if (timer->private_free)
967 timer->private_free(timer);
972 static int snd_timer_dev_free(struct snd_device *device)
974 struct snd_timer *timer = device->device_data;
975 return snd_timer_free(timer);
978 static int snd_timer_dev_register(struct snd_device *dev)
980 struct snd_timer *timer = dev->device_data;
981 struct snd_timer *timer1;
983 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
985 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
986 !timer->hw.resolution && timer->hw.c_resolution == NULL)
989 mutex_lock(®ister_mutex);
990 list_for_each_entry(timer1, &snd_timer_list, device_list) {
991 if (timer1->tmr_class > timer->tmr_class)
993 if (timer1->tmr_class < timer->tmr_class)
995 if (timer1->card && timer->card) {
996 if (timer1->card->number > timer->card->number)
998 if (timer1->card->number < timer->card->number)
1001 if (timer1->tmr_device > timer->tmr_device)
1003 if (timer1->tmr_device < timer->tmr_device)
1005 if (timer1->tmr_subdevice > timer->tmr_subdevice)
1007 if (timer1->tmr_subdevice < timer->tmr_subdevice)
1010 mutex_unlock(®ister_mutex);
1013 list_add_tail(&timer->device_list, &timer1->device_list);
1014 mutex_unlock(®ister_mutex);
1018 static int snd_timer_dev_disconnect(struct snd_device *device)
1020 struct snd_timer *timer = device->device_data;
1021 struct snd_timer_instance *ti;
1023 mutex_lock(®ister_mutex);
1024 list_del_init(&timer->device_list);
1025 /* wake up pending sleepers */
1026 list_for_each_entry(ti, &timer->open_list_head, open_list) {
1030 mutex_unlock(®ister_mutex);
1034 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
1036 unsigned long flags;
1037 unsigned long resolution = 0;
1038 struct snd_timer_instance *ti, *ts;
1040 if (timer->card && timer->card->shutdown)
1042 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1044 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1045 event > SNDRV_TIMER_EVENT_MRESUME))
1047 spin_lock_irqsave(&timer->lock, flags);
1048 if (event == SNDRV_TIMER_EVENT_MSTART ||
1049 event == SNDRV_TIMER_EVENT_MCONTINUE ||
1050 event == SNDRV_TIMER_EVENT_MRESUME)
1051 resolution = snd_timer_hw_resolution(timer);
1052 list_for_each_entry(ti, &timer->active_list_head, active_list) {
1054 ti->ccallback(ti, event, tstamp, resolution);
1055 list_for_each_entry(ts, &ti->slave_active_head, active_list)
1057 ts->ccallback(ts, event, tstamp, resolution);
1059 spin_unlock_irqrestore(&timer->lock, flags);
1061 EXPORT_SYMBOL(snd_timer_notify);
1064 * exported functions for global timers
1066 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1068 struct snd_timer_id tid;
1070 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1071 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1073 tid.device = device;
1075 return snd_timer_new(NULL, id, &tid, rtimer);
1077 EXPORT_SYMBOL(snd_timer_global_new);
1079 int snd_timer_global_free(struct snd_timer *timer)
1081 return snd_timer_free(timer);
1083 EXPORT_SYMBOL(snd_timer_global_free);
1085 int snd_timer_global_register(struct snd_timer *timer)
1087 struct snd_device dev;
1089 memset(&dev, 0, sizeof(dev));
1090 dev.device_data = timer;
1091 return snd_timer_dev_register(&dev);
1093 EXPORT_SYMBOL(snd_timer_global_register);
1099 struct snd_timer_system_private {
1100 struct timer_list tlist;
1101 struct snd_timer *snd_timer;
1102 unsigned long last_expires;
1103 unsigned long last_jiffies;
1104 unsigned long correction;
1107 static void snd_timer_s_function(struct timer_list *t)
1109 struct snd_timer_system_private *priv = from_timer(priv, t,
1111 struct snd_timer *timer = priv->snd_timer;
1112 unsigned long jiff = jiffies;
1113 if (time_after(jiff, priv->last_expires))
1114 priv->correction += (long)jiff - (long)priv->last_expires;
1115 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1118 static int snd_timer_s_start(struct snd_timer * timer)
1120 struct snd_timer_system_private *priv;
1121 unsigned long njiff;
1123 priv = (struct snd_timer_system_private *) timer->private_data;
1124 njiff = (priv->last_jiffies = jiffies);
1125 if (priv->correction > timer->sticks - 1) {
1126 priv->correction -= timer->sticks - 1;
1129 njiff += timer->sticks - priv->correction;
1130 priv->correction = 0;
1132 priv->last_expires = njiff;
1133 mod_timer(&priv->tlist, njiff);
1137 static int snd_timer_s_stop(struct snd_timer * timer)
1139 struct snd_timer_system_private *priv;
1142 priv = (struct snd_timer_system_private *) timer->private_data;
1143 del_timer(&priv->tlist);
1145 if (time_before(jiff, priv->last_expires))
1146 timer->sticks = priv->last_expires - jiff;
1149 priv->correction = 0;
1153 static int snd_timer_s_close(struct snd_timer *timer)
1155 struct snd_timer_system_private *priv;
1157 priv = (struct snd_timer_system_private *)timer->private_data;
1158 del_timer_sync(&priv->tlist);
1162 static struct snd_timer_hardware snd_timer_system =
1164 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1165 .resolution = 1000000000L / HZ,
1167 .close = snd_timer_s_close,
1168 .start = snd_timer_s_start,
1169 .stop = snd_timer_s_stop
1172 static void snd_timer_free_system(struct snd_timer *timer)
1174 kfree(timer->private_data);
1177 static int snd_timer_register_system(void)
1179 struct snd_timer *timer;
1180 struct snd_timer_system_private *priv;
1183 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1186 strcpy(timer->name, "system timer");
1187 timer->hw = snd_timer_system;
1188 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1190 snd_timer_free(timer);
1193 priv->snd_timer = timer;
1194 timer_setup(&priv->tlist, snd_timer_s_function, 0);
1195 timer->private_data = priv;
1196 timer->private_free = snd_timer_free_system;
1197 return snd_timer_global_register(timer);
1200 #ifdef CONFIG_SND_PROC_FS
1205 static void snd_timer_proc_read(struct snd_info_entry *entry,
1206 struct snd_info_buffer *buffer)
1208 struct snd_timer *timer;
1209 struct snd_timer_instance *ti;
1211 mutex_lock(®ister_mutex);
1212 list_for_each_entry(timer, &snd_timer_list, device_list) {
1213 if (timer->card && timer->card->shutdown)
1215 switch (timer->tmr_class) {
1216 case SNDRV_TIMER_CLASS_GLOBAL:
1217 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1219 case SNDRV_TIMER_CLASS_CARD:
1220 snd_iprintf(buffer, "C%i-%i: ",
1221 timer->card->number, timer->tmr_device);
1223 case SNDRV_TIMER_CLASS_PCM:
1224 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1225 timer->tmr_device, timer->tmr_subdevice);
1228 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1229 timer->card ? timer->card->number : -1,
1230 timer->tmr_device, timer->tmr_subdevice);
1232 snd_iprintf(buffer, "%s :", timer->name);
1233 if (timer->hw.resolution)
1234 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1235 timer->hw.resolution / 1000,
1236 timer->hw.resolution % 1000,
1238 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1239 snd_iprintf(buffer, " SLAVE");
1240 snd_iprintf(buffer, "\n");
1241 list_for_each_entry(ti, &timer->open_list_head, open_list)
1242 snd_iprintf(buffer, " Client %s : %s\n",
1243 ti->owner ? ti->owner : "unknown",
1244 ti->flags & (SNDRV_TIMER_IFLG_START |
1245 SNDRV_TIMER_IFLG_RUNNING)
1246 ? "running" : "stopped");
1248 mutex_unlock(®ister_mutex);
1251 static struct snd_info_entry *snd_timer_proc_entry;
1253 static void __init snd_timer_proc_init(void)
1255 struct snd_info_entry *entry;
1257 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1258 if (entry != NULL) {
1259 entry->c.text.read = snd_timer_proc_read;
1260 if (snd_info_register(entry) < 0) {
1261 snd_info_free_entry(entry);
1265 snd_timer_proc_entry = entry;
1268 static void __exit snd_timer_proc_done(void)
1270 snd_info_free_entry(snd_timer_proc_entry);
1272 #else /* !CONFIG_SND_PROC_FS */
1273 #define snd_timer_proc_init()
1274 #define snd_timer_proc_done()
1278 * USER SPACE interface
1281 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1282 unsigned long resolution,
1283 unsigned long ticks)
1285 struct snd_timer_user *tu = timeri->callback_data;
1286 struct snd_timer_read *r;
1289 spin_lock(&tu->qlock);
1290 if (tu->qused > 0) {
1291 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1292 r = &tu->queue[prev];
1293 if (r->resolution == resolution) {
1298 if (tu->qused >= tu->queue_size) {
1301 r = &tu->queue[tu->qtail++];
1302 tu->qtail %= tu->queue_size;
1303 r->resolution = resolution;
1308 spin_unlock(&tu->qlock);
1309 snd_kill_fasync(tu->fasync, SIGIO, POLL_IN);
1310 wake_up(&tu->qchange_sleep);
1313 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1314 struct snd_timer_tread *tread)
1316 if (tu->qused >= tu->queue_size) {
1319 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1320 tu->qtail %= tu->queue_size;
1325 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1327 struct timespec *tstamp,
1328 unsigned long resolution)
1330 struct snd_timer_user *tu = timeri->callback_data;
1331 struct snd_timer_tread r1;
1332 unsigned long flags;
1334 if (event >= SNDRV_TIMER_EVENT_START &&
1335 event <= SNDRV_TIMER_EVENT_PAUSE)
1336 tu->tstamp = *tstamp;
1337 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1339 memset(&r1, 0, sizeof(r1));
1341 r1.tstamp = *tstamp;
1342 r1.val = resolution;
1343 spin_lock_irqsave(&tu->qlock, flags);
1344 snd_timer_user_append_to_tqueue(tu, &r1);
1345 spin_unlock_irqrestore(&tu->qlock, flags);
1346 snd_kill_fasync(tu->fasync, SIGIO, POLL_IN);
1347 wake_up(&tu->qchange_sleep);
1350 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1352 struct snd_timer_user *tu = timeri->callback_data;
1354 tu->disconnected = true;
1355 wake_up(&tu->qchange_sleep);
1358 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1359 unsigned long resolution,
1360 unsigned long ticks)
1362 struct snd_timer_user *tu = timeri->callback_data;
1363 struct snd_timer_tread *r, r1;
1364 struct timespec tstamp;
1365 int prev, append = 0;
1367 memset(&r1, 0, sizeof(r1));
1368 memset(&tstamp, 0, sizeof(tstamp));
1369 spin_lock(&tu->qlock);
1370 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1371 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1372 spin_unlock(&tu->qlock);
1375 if (tu->last_resolution != resolution || ticks > 0) {
1376 if (timer_tstamp_monotonic)
1377 ktime_get_ts(&tstamp);
1379 getnstimeofday(&tstamp);
1381 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1382 tu->last_resolution != resolution) {
1383 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1385 r1.val = resolution;
1386 snd_timer_user_append_to_tqueue(tu, &r1);
1387 tu->last_resolution = resolution;
1390 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1394 if (tu->qused > 0) {
1395 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1396 r = &tu->tqueue[prev];
1397 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1404 r1.event = SNDRV_TIMER_EVENT_TICK;
1407 snd_timer_user_append_to_tqueue(tu, &r1);
1410 spin_unlock(&tu->qlock);
1413 snd_kill_fasync(tu->fasync, SIGIO, POLL_IN);
1414 wake_up(&tu->qchange_sleep);
1417 static int realloc_user_queue(struct snd_timer_user *tu, int size)
1419 struct snd_timer_read *queue = NULL;
1420 struct snd_timer_tread *tqueue = NULL;
1423 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
1427 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
1432 spin_lock_irq(&tu->qlock);
1435 tu->queue_size = size;
1437 tu->tqueue = tqueue;
1438 tu->qhead = tu->qtail = tu->qused = 0;
1439 spin_unlock_irq(&tu->qlock);
1444 static int snd_timer_user_open(struct inode *inode, struct file *file)
1446 struct snd_timer_user *tu;
1449 err = nonseekable_open(inode, file);
1453 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1456 spin_lock_init(&tu->qlock);
1457 init_waitqueue_head(&tu->qchange_sleep);
1458 mutex_init(&tu->ioctl_lock);
1460 if (realloc_user_queue(tu, 128) < 0) {
1464 file->private_data = tu;
1468 static int snd_timer_user_release(struct inode *inode, struct file *file)
1470 struct snd_timer_user *tu;
1472 if (file->private_data) {
1473 tu = file->private_data;
1474 file->private_data = NULL;
1475 mutex_lock(&tu->ioctl_lock);
1477 snd_timer_close(tu->timeri);
1478 mutex_unlock(&tu->ioctl_lock);
1479 snd_fasync_free(tu->fasync);
1487 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1489 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1490 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1496 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1498 id->dev_class = timer->tmr_class;
1499 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1500 id->card = timer->card ? timer->card->number : -1;
1501 id->device = timer->tmr_device;
1502 id->subdevice = timer->tmr_subdevice;
1505 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1507 struct snd_timer_id id;
1508 struct snd_timer *timer;
1509 struct list_head *p;
1511 if (copy_from_user(&id, _tid, sizeof(id)))
1513 mutex_lock(®ister_mutex);
1514 if (id.dev_class < 0) { /* first item */
1515 if (list_empty(&snd_timer_list))
1516 snd_timer_user_zero_id(&id);
1518 timer = list_entry(snd_timer_list.next,
1519 struct snd_timer, device_list);
1520 snd_timer_user_copy_id(&id, timer);
1523 switch (id.dev_class) {
1524 case SNDRV_TIMER_CLASS_GLOBAL:
1525 id.device = id.device < 0 ? 0 : id.device + 1;
1526 list_for_each(p, &snd_timer_list) {
1527 timer = list_entry(p, struct snd_timer, device_list);
1528 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1529 snd_timer_user_copy_id(&id, timer);
1532 if (timer->tmr_device >= id.device) {
1533 snd_timer_user_copy_id(&id, timer);
1537 if (p == &snd_timer_list)
1538 snd_timer_user_zero_id(&id);
1540 case SNDRV_TIMER_CLASS_CARD:
1541 case SNDRV_TIMER_CLASS_PCM:
1545 if (id.device < 0) {
1548 if (id.subdevice < 0)
1550 else if (id.subdevice < INT_MAX)
1554 list_for_each(p, &snd_timer_list) {
1555 timer = list_entry(p, struct snd_timer, device_list);
1556 if (timer->tmr_class > id.dev_class) {
1557 snd_timer_user_copy_id(&id, timer);
1560 if (timer->tmr_class < id.dev_class)
1562 if (timer->card->number > id.card) {
1563 snd_timer_user_copy_id(&id, timer);
1566 if (timer->card->number < id.card)
1568 if (timer->tmr_device > id.device) {
1569 snd_timer_user_copy_id(&id, timer);
1572 if (timer->tmr_device < id.device)
1574 if (timer->tmr_subdevice > id.subdevice) {
1575 snd_timer_user_copy_id(&id, timer);
1578 if (timer->tmr_subdevice < id.subdevice)
1580 snd_timer_user_copy_id(&id, timer);
1583 if (p == &snd_timer_list)
1584 snd_timer_user_zero_id(&id);
1587 snd_timer_user_zero_id(&id);
1590 mutex_unlock(®ister_mutex);
1591 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1596 static int snd_timer_user_ginfo(struct file *file,
1597 struct snd_timer_ginfo __user *_ginfo)
1599 struct snd_timer_ginfo *ginfo;
1600 struct snd_timer_id tid;
1601 struct snd_timer *t;
1602 struct list_head *p;
1605 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1607 return PTR_ERR(ginfo);
1610 memset(ginfo, 0, sizeof(*ginfo));
1612 mutex_lock(®ister_mutex);
1613 t = snd_timer_find(&tid);
1615 ginfo->card = t->card ? t->card->number : -1;
1616 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1617 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1618 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1619 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1620 ginfo->resolution = t->hw.resolution;
1621 if (t->hw.resolution_min > 0) {
1622 ginfo->resolution_min = t->hw.resolution_min;
1623 ginfo->resolution_max = t->hw.resolution_max;
1625 list_for_each(p, &t->open_list_head) {
1631 mutex_unlock(®ister_mutex);
1632 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1638 static int timer_set_gparams(struct snd_timer_gparams *gparams)
1640 struct snd_timer *t;
1643 mutex_lock(®ister_mutex);
1644 t = snd_timer_find(&gparams->tid);
1649 if (!list_empty(&t->open_list_head)) {
1653 if (!t->hw.set_period) {
1657 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
1659 mutex_unlock(®ister_mutex);
1663 static int snd_timer_user_gparams(struct file *file,
1664 struct snd_timer_gparams __user *_gparams)
1666 struct snd_timer_gparams gparams;
1668 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1670 return timer_set_gparams(&gparams);
1673 static int snd_timer_user_gstatus(struct file *file,
1674 struct snd_timer_gstatus __user *_gstatus)
1676 struct snd_timer_gstatus gstatus;
1677 struct snd_timer_id tid;
1678 struct snd_timer *t;
1681 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1684 memset(&gstatus, 0, sizeof(gstatus));
1686 mutex_lock(®ister_mutex);
1687 t = snd_timer_find(&tid);
1689 spin_lock_irq(&t->lock);
1690 gstatus.resolution = snd_timer_hw_resolution(t);
1691 if (t->hw.precise_resolution) {
1692 t->hw.precise_resolution(t, &gstatus.resolution_num,
1693 &gstatus.resolution_den);
1695 gstatus.resolution_num = gstatus.resolution;
1696 gstatus.resolution_den = 1000000000uL;
1698 spin_unlock_irq(&t->lock);
1702 mutex_unlock(®ister_mutex);
1703 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1708 static int snd_timer_user_tselect(struct file *file,
1709 struct snd_timer_select __user *_tselect)
1711 struct snd_timer_user *tu;
1712 struct snd_timer_select tselect;
1716 tu = file->private_data;
1718 snd_timer_close(tu->timeri);
1721 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1725 sprintf(str, "application %i", current->pid);
1726 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1727 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1728 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1732 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1733 tu->timeri->callback = tu->tread
1734 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1735 tu->timeri->ccallback = snd_timer_user_ccallback;
1736 tu->timeri->callback_data = (void *)tu;
1737 tu->timeri->disconnect = snd_timer_user_disconnect;
1743 static int snd_timer_user_info(struct file *file,
1744 struct snd_timer_info __user *_info)
1746 struct snd_timer_user *tu;
1747 struct snd_timer_info *info;
1748 struct snd_timer *t;
1751 tu = file->private_data;
1754 t = tu->timeri->timer;
1758 info = kzalloc(sizeof(*info), GFP_KERNEL);
1761 info->card = t->card ? t->card->number : -1;
1762 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1763 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1764 strlcpy(info->id, t->id, sizeof(info->id));
1765 strlcpy(info->name, t->name, sizeof(info->name));
1766 info->resolution = t->hw.resolution;
1767 if (copy_to_user(_info, info, sizeof(*_info)))
1773 static int snd_timer_user_params(struct file *file,
1774 struct snd_timer_params __user *_params)
1776 struct snd_timer_user *tu;
1777 struct snd_timer_params params;
1778 struct snd_timer *t;
1781 tu = file->private_data;
1784 t = tu->timeri->timer;
1787 if (copy_from_user(¶ms, _params, sizeof(params)))
1789 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1792 if (params.ticks < 1) {
1797 /* Don't allow resolution less than 1ms */
1798 resolution = snd_timer_resolution(tu->timeri);
1799 resolution *= params.ticks;
1800 if (resolution < 1000000) {
1805 if (params.queue_size > 0 &&
1806 (params.queue_size < 32 || params.queue_size > 1024)) {
1810 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1811 (1<<SNDRV_TIMER_EVENT_TICK)|
1812 (1<<SNDRV_TIMER_EVENT_START)|
1813 (1<<SNDRV_TIMER_EVENT_STOP)|
1814 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1815 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1816 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1817 (1<<SNDRV_TIMER_EVENT_RESUME)|
1818 (1<<SNDRV_TIMER_EVENT_MSTART)|
1819 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1820 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1821 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1822 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1823 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1827 snd_timer_stop(tu->timeri);
1828 spin_lock_irq(&t->lock);
1829 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1830 SNDRV_TIMER_IFLG_EXCLUSIVE|
1831 SNDRV_TIMER_IFLG_EARLY_EVENT);
1832 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1833 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1834 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1835 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1836 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1837 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1838 spin_unlock_irq(&t->lock);
1839 if (params.queue_size > 0 &&
1840 (unsigned int)tu->queue_size != params.queue_size) {
1841 err = realloc_user_queue(tu, params.queue_size);
1845 spin_lock_irq(&tu->qlock);
1846 tu->qhead = tu->qtail = tu->qused = 0;
1847 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1849 struct snd_timer_tread tread;
1850 memset(&tread, 0, sizeof(tread));
1851 tread.event = SNDRV_TIMER_EVENT_EARLY;
1852 tread.tstamp.tv_sec = 0;
1853 tread.tstamp.tv_nsec = 0;
1855 snd_timer_user_append_to_tqueue(tu, &tread);
1857 struct snd_timer_read *r = &tu->queue[0];
1864 tu->filter = params.filter;
1865 tu->ticks = params.ticks;
1866 spin_unlock_irq(&tu->qlock);
1869 if (copy_to_user(_params, ¶ms, sizeof(params)))
1874 static int snd_timer_user_status(struct file *file,
1875 struct snd_timer_status __user *_status)
1877 struct snd_timer_user *tu;
1878 struct snd_timer_status status;
1880 tu = file->private_data;
1883 memset(&status, 0, sizeof(status));
1884 status.tstamp = tu->tstamp;
1885 status.resolution = snd_timer_resolution(tu->timeri);
1886 status.lost = tu->timeri->lost;
1887 status.overrun = tu->overrun;
1888 spin_lock_irq(&tu->qlock);
1889 status.queue = tu->qused;
1890 spin_unlock_irq(&tu->qlock);
1891 if (copy_to_user(_status, &status, sizeof(status)))
1896 static int snd_timer_user_start(struct file *file)
1899 struct snd_timer_user *tu;
1901 tu = file->private_data;
1904 snd_timer_stop(tu->timeri);
1905 tu->timeri->lost = 0;
1906 tu->last_resolution = 0;
1907 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1910 static int snd_timer_user_stop(struct file *file)
1913 struct snd_timer_user *tu;
1915 tu = file->private_data;
1918 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1921 static int snd_timer_user_continue(struct file *file)
1924 struct snd_timer_user *tu;
1926 tu = file->private_data;
1929 /* start timer instead of continue if it's not used before */
1930 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1931 return snd_timer_user_start(file);
1932 tu->timeri->lost = 0;
1933 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1936 static int snd_timer_user_pause(struct file *file)
1939 struct snd_timer_user *tu;
1941 tu = file->private_data;
1944 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1948 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1949 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1950 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1951 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1954 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1957 struct snd_timer_user *tu;
1958 void __user *argp = (void __user *)arg;
1959 int __user *p = argp;
1961 tu = file->private_data;
1963 case SNDRV_TIMER_IOCTL_PVERSION:
1964 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1965 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1966 return snd_timer_user_next_device(argp);
1967 case SNDRV_TIMER_IOCTL_TREAD:
1969 int xarg, old_tread;
1971 if (tu->timeri) /* too late */
1973 if (get_user(xarg, p))
1975 old_tread = tu->tread;
1976 tu->tread = xarg ? 1 : 0;
1977 if (tu->tread != old_tread &&
1978 realloc_user_queue(tu, tu->queue_size) < 0) {
1979 tu->tread = old_tread;
1984 case SNDRV_TIMER_IOCTL_GINFO:
1985 return snd_timer_user_ginfo(file, argp);
1986 case SNDRV_TIMER_IOCTL_GPARAMS:
1987 return snd_timer_user_gparams(file, argp);
1988 case SNDRV_TIMER_IOCTL_GSTATUS:
1989 return snd_timer_user_gstatus(file, argp);
1990 case SNDRV_TIMER_IOCTL_SELECT:
1991 return snd_timer_user_tselect(file, argp);
1992 case SNDRV_TIMER_IOCTL_INFO:
1993 return snd_timer_user_info(file, argp);
1994 case SNDRV_TIMER_IOCTL_PARAMS:
1995 return snd_timer_user_params(file, argp);
1996 case SNDRV_TIMER_IOCTL_STATUS:
1997 return snd_timer_user_status(file, argp);
1998 case SNDRV_TIMER_IOCTL_START:
1999 case SNDRV_TIMER_IOCTL_START_OLD:
2000 return snd_timer_user_start(file);
2001 case SNDRV_TIMER_IOCTL_STOP:
2002 case SNDRV_TIMER_IOCTL_STOP_OLD:
2003 return snd_timer_user_stop(file);
2004 case SNDRV_TIMER_IOCTL_CONTINUE:
2005 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
2006 return snd_timer_user_continue(file);
2007 case SNDRV_TIMER_IOCTL_PAUSE:
2008 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
2009 return snd_timer_user_pause(file);
2014 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
2017 struct snd_timer_user *tu = file->private_data;
2020 mutex_lock(&tu->ioctl_lock);
2021 ret = __snd_timer_user_ioctl(file, cmd, arg);
2022 mutex_unlock(&tu->ioctl_lock);
2026 static int snd_timer_user_fasync(int fd, struct file * file, int on)
2028 struct snd_timer_user *tu;
2030 tu = file->private_data;
2031 return snd_fasync_helper(fd, file, on, &tu->fasync);
2034 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2035 size_t count, loff_t *offset)
2037 struct snd_timer_user *tu;
2038 long result = 0, unit;
2042 tu = file->private_data;
2043 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
2044 mutex_lock(&tu->ioctl_lock);
2045 spin_lock_irq(&tu->qlock);
2046 while ((long)count - result >= unit) {
2047 while (!tu->qused) {
2048 wait_queue_entry_t wait;
2050 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2055 set_current_state(TASK_INTERRUPTIBLE);
2056 init_waitqueue_entry(&wait, current);
2057 add_wait_queue(&tu->qchange_sleep, &wait);
2059 spin_unlock_irq(&tu->qlock);
2060 mutex_unlock(&tu->ioctl_lock);
2062 mutex_lock(&tu->ioctl_lock);
2063 spin_lock_irq(&tu->qlock);
2065 remove_wait_queue(&tu->qchange_sleep, &wait);
2067 if (tu->disconnected) {
2071 if (signal_pending(current)) {
2077 qhead = tu->qhead++;
2078 tu->qhead %= tu->queue_size;
2080 spin_unlock_irq(&tu->qlock);
2083 if (copy_to_user(buffer, &tu->tqueue[qhead],
2084 sizeof(struct snd_timer_tread)))
2087 if (copy_to_user(buffer, &tu->queue[qhead],
2088 sizeof(struct snd_timer_read)))
2092 spin_lock_irq(&tu->qlock);
2099 spin_unlock_irq(&tu->qlock);
2100 mutex_unlock(&tu->ioctl_lock);
2101 return result > 0 ? result : err;
2104 static __poll_t snd_timer_user_poll(struct file *file, poll_table * wait)
2107 struct snd_timer_user *tu;
2109 tu = file->private_data;
2111 poll_wait(file, &tu->qchange_sleep, wait);
2114 spin_lock_irq(&tu->qlock);
2116 mask |= EPOLLIN | EPOLLRDNORM;
2117 if (tu->disconnected)
2119 spin_unlock_irq(&tu->qlock);
2124 #ifdef CONFIG_COMPAT
2125 #include "timer_compat.c"
2127 #define snd_timer_user_ioctl_compat NULL
2130 static const struct file_operations snd_timer_f_ops =
2132 .owner = THIS_MODULE,
2133 .read = snd_timer_user_read,
2134 .open = snd_timer_user_open,
2135 .release = snd_timer_user_release,
2136 .llseek = no_llseek,
2137 .poll = snd_timer_user_poll,
2138 .unlocked_ioctl = snd_timer_user_ioctl,
2139 .compat_ioctl = snd_timer_user_ioctl_compat,
2140 .fasync = snd_timer_user_fasync,
2143 /* unregister the system timer */
2144 static void snd_timer_free_all(void)
2146 struct snd_timer *timer, *n;
2148 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2149 snd_timer_free(timer);
2152 static struct device timer_dev;
2158 static int __init alsa_timer_init(void)
2162 snd_device_initialize(&timer_dev, NULL);
2163 dev_set_name(&timer_dev, "timer");
2165 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2166 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2170 err = snd_timer_register_system();
2172 pr_err("ALSA: unable to register system timer (%i)\n", err);
2176 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2177 &snd_timer_f_ops, NULL, &timer_dev);
2179 pr_err("ALSA: unable to register timer device (%i)\n", err);
2180 snd_timer_free_all();
2184 snd_timer_proc_init();
2188 put_device(&timer_dev);
2192 static void __exit alsa_timer_exit(void)
2194 snd_unregister_device(&timer_dev);
2195 snd_timer_free_all();
2196 put_device(&timer_dev);
2197 snd_timer_proc_done();
2198 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2199 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2203 module_init(alsa_timer_init)
2204 module_exit(alsa_timer_exit)