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 fasync_struct *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 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
597 SNDRV_TIMER_IFLG_START))) {
601 list_del_init(&timeri->ack_list);
602 list_del_init(&timeri->active_list);
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)
638 spin_lock_irqsave(&slave_active_lock, flags);
639 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
640 spin_unlock_irqrestore(&slave_active_lock, flags);
643 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
645 spin_lock(&timeri->timer->lock);
646 list_del_init(&timeri->ack_list);
647 list_del_init(&timeri->active_list);
648 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
649 SNDRV_TIMER_EVENT_PAUSE);
650 spin_unlock(&timeri->timer->lock);
652 spin_unlock_irqrestore(&slave_active_lock, flags);
657 * start the timer instance
659 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
661 if (timeri == NULL || ticks < 1)
663 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
664 return snd_timer_start_slave(timeri, true);
666 return snd_timer_start1(timeri, true, ticks);
668 EXPORT_SYMBOL(snd_timer_start);
671 * stop the timer instance.
673 * do not call this from the timer callback!
675 int snd_timer_stop(struct snd_timer_instance *timeri)
677 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
678 return snd_timer_stop_slave(timeri, true);
680 return snd_timer_stop1(timeri, true);
682 EXPORT_SYMBOL(snd_timer_stop);
685 * start again.. the tick is kept.
687 int snd_timer_continue(struct snd_timer_instance *timeri)
689 /* timer can continue only after pause */
690 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
693 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
694 return snd_timer_start_slave(timeri, false);
696 return snd_timer_start1(timeri, false, 0);
698 EXPORT_SYMBOL(snd_timer_continue);
701 * pause.. remember the ticks left
703 int snd_timer_pause(struct snd_timer_instance * timeri)
705 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
706 return snd_timer_stop_slave(timeri, false);
708 return snd_timer_stop1(timeri, false);
710 EXPORT_SYMBOL(snd_timer_pause);
713 * reschedule the timer
715 * start pending instances and check the scheduling ticks.
716 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
718 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
720 struct snd_timer_instance *ti;
721 unsigned long ticks = ~0UL;
723 list_for_each_entry(ti, &timer->active_list_head, active_list) {
724 if (ti->flags & SNDRV_TIMER_IFLG_START) {
725 ti->flags &= ~SNDRV_TIMER_IFLG_START;
726 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
729 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
730 if (ticks > ti->cticks)
735 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
738 if (ticks > timer->hw.ticks)
739 ticks = timer->hw.ticks;
740 if (ticks_left != ticks)
741 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
742 timer->sticks = ticks;
749 static void snd_timer_tasklet(unsigned long arg)
751 struct snd_timer *timer = (struct snd_timer *) arg;
752 struct snd_timer_instance *ti;
754 unsigned long resolution, ticks;
757 if (timer->card && timer->card->shutdown)
760 spin_lock_irqsave(&timer->lock, flags);
761 /* now process all callbacks */
762 while (!list_empty(&timer->sack_list_head)) {
763 p = timer->sack_list_head.next; /* get first item */
764 ti = list_entry(p, struct snd_timer_instance, ack_list);
766 /* remove from ack_list and make empty */
771 resolution = ti->resolution;
773 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
774 spin_unlock(&timer->lock);
776 ti->callback(ti, resolution, ticks);
777 spin_lock(&timer->lock);
778 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
780 spin_unlock_irqrestore(&timer->lock, flags);
786 * ticks_left is usually equal to timer->sticks.
789 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
791 struct snd_timer_instance *ti, *ts, *tmp;
792 unsigned long resolution, ticks;
793 struct list_head *p, *ack_list_head;
800 if (timer->card && timer->card->shutdown)
803 spin_lock_irqsave(&timer->lock, flags);
805 /* remember the current resolution */
806 resolution = snd_timer_hw_resolution(timer);
808 /* loop for all active instances
809 * Here we cannot use list_for_each_entry because the active_list of a
810 * processed instance is relinked to done_list_head before the callback
813 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
815 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
817 ti->pticks += ticks_left;
818 ti->resolution = resolution;
819 if (ti->cticks < ticks_left)
822 ti->cticks -= ticks_left;
823 if (ti->cticks) /* not expired */
825 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
826 ti->cticks = ti->ticks;
828 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
830 list_del_init(&ti->active_list);
832 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
833 (ti->flags & SNDRV_TIMER_IFLG_FAST))
834 ack_list_head = &timer->ack_list_head;
836 ack_list_head = &timer->sack_list_head;
837 if (list_empty(&ti->ack_list))
838 list_add_tail(&ti->ack_list, ack_list_head);
839 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
840 ts->pticks = ti->pticks;
841 ts->resolution = resolution;
842 if (list_empty(&ts->ack_list))
843 list_add_tail(&ts->ack_list, ack_list_head);
846 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
847 snd_timer_reschedule(timer, timer->sticks);
848 if (timer->running) {
849 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
850 timer->hw.stop(timer);
851 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
853 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
854 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
856 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
857 timer->hw.start(timer);
860 timer->hw.stop(timer);
863 /* now process all fast callbacks */
864 while (!list_empty(&timer->ack_list_head)) {
865 p = timer->ack_list_head.next; /* get first item */
866 ti = list_entry(p, struct snd_timer_instance, ack_list);
868 /* remove from ack_list and make empty */
874 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
875 spin_unlock(&timer->lock);
877 ti->callback(ti, resolution, ticks);
878 spin_lock(&timer->lock);
879 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
882 /* do we have any slow callbacks? */
883 use_tasklet = !list_empty(&timer->sack_list_head);
884 spin_unlock_irqrestore(&timer->lock, flags);
887 tasklet_schedule(&timer->task_queue);
889 EXPORT_SYMBOL(snd_timer_interrupt);
895 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
896 struct snd_timer **rtimer)
898 struct snd_timer *timer;
900 static struct snd_device_ops ops = {
901 .dev_free = snd_timer_dev_free,
902 .dev_register = snd_timer_dev_register,
903 .dev_disconnect = snd_timer_dev_disconnect,
906 if (snd_BUG_ON(!tid))
908 if (tid->dev_class == SNDRV_TIMER_CLASS_CARD ||
909 tid->dev_class == SNDRV_TIMER_CLASS_PCM) {
915 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
918 timer->tmr_class = tid->dev_class;
920 timer->tmr_device = tid->device;
921 timer->tmr_subdevice = tid->subdevice;
923 strlcpy(timer->id, id, sizeof(timer->id));
925 INIT_LIST_HEAD(&timer->device_list);
926 INIT_LIST_HEAD(&timer->open_list_head);
927 INIT_LIST_HEAD(&timer->active_list_head);
928 INIT_LIST_HEAD(&timer->ack_list_head);
929 INIT_LIST_HEAD(&timer->sack_list_head);
930 spin_lock_init(&timer->lock);
931 tasklet_init(&timer->task_queue, snd_timer_tasklet,
932 (unsigned long)timer);
933 timer->max_instances = 1000; /* default limit per timer */
935 timer->module = card->module;
936 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
938 snd_timer_free(timer);
946 EXPORT_SYMBOL(snd_timer_new);
948 static int snd_timer_free(struct snd_timer *timer)
953 mutex_lock(®ister_mutex);
954 if (! list_empty(&timer->open_list_head)) {
955 struct list_head *p, *n;
956 struct snd_timer_instance *ti;
957 pr_warn("ALSA: timer %p is busy?\n", timer);
958 list_for_each_safe(p, n, &timer->open_list_head) {
960 ti = list_entry(p, struct snd_timer_instance, open_list);
964 list_del(&timer->device_list);
965 mutex_unlock(®ister_mutex);
967 if (timer->private_free)
968 timer->private_free(timer);
973 static int snd_timer_dev_free(struct snd_device *device)
975 struct snd_timer *timer = device->device_data;
976 return snd_timer_free(timer);
979 static int snd_timer_dev_register(struct snd_device *dev)
981 struct snd_timer *timer = dev->device_data;
982 struct snd_timer *timer1;
984 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
986 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
987 !timer->hw.resolution && timer->hw.c_resolution == NULL)
990 mutex_lock(®ister_mutex);
991 list_for_each_entry(timer1, &snd_timer_list, device_list) {
992 if (timer1->tmr_class > timer->tmr_class)
994 if (timer1->tmr_class < timer->tmr_class)
996 if (timer1->card && timer->card) {
997 if (timer1->card->number > timer->card->number)
999 if (timer1->card->number < timer->card->number)
1002 if (timer1->tmr_device > timer->tmr_device)
1004 if (timer1->tmr_device < timer->tmr_device)
1006 if (timer1->tmr_subdevice > timer->tmr_subdevice)
1008 if (timer1->tmr_subdevice < timer->tmr_subdevice)
1011 mutex_unlock(®ister_mutex);
1014 list_add_tail(&timer->device_list, &timer1->device_list);
1015 mutex_unlock(®ister_mutex);
1019 static int snd_timer_dev_disconnect(struct snd_device *device)
1021 struct snd_timer *timer = device->device_data;
1022 struct snd_timer_instance *ti;
1024 mutex_lock(®ister_mutex);
1025 list_del_init(&timer->device_list);
1026 /* wake up pending sleepers */
1027 list_for_each_entry(ti, &timer->open_list_head, open_list) {
1031 mutex_unlock(®ister_mutex);
1035 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
1037 unsigned long flags;
1038 unsigned long resolution = 0;
1039 struct snd_timer_instance *ti, *ts;
1041 if (timer->card && timer->card->shutdown)
1043 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1045 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1046 event > SNDRV_TIMER_EVENT_MRESUME))
1048 spin_lock_irqsave(&timer->lock, flags);
1049 if (event == SNDRV_TIMER_EVENT_MSTART ||
1050 event == SNDRV_TIMER_EVENT_MCONTINUE ||
1051 event == SNDRV_TIMER_EVENT_MRESUME)
1052 resolution = snd_timer_hw_resolution(timer);
1053 list_for_each_entry(ti, &timer->active_list_head, active_list) {
1055 ti->ccallback(ti, event, tstamp, resolution);
1056 list_for_each_entry(ts, &ti->slave_active_head, active_list)
1058 ts->ccallback(ts, event, tstamp, resolution);
1060 spin_unlock_irqrestore(&timer->lock, flags);
1062 EXPORT_SYMBOL(snd_timer_notify);
1065 * exported functions for global timers
1067 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1069 struct snd_timer_id tid;
1071 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1072 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1074 tid.device = device;
1076 return snd_timer_new(NULL, id, &tid, rtimer);
1078 EXPORT_SYMBOL(snd_timer_global_new);
1080 int snd_timer_global_free(struct snd_timer *timer)
1082 return snd_timer_free(timer);
1084 EXPORT_SYMBOL(snd_timer_global_free);
1086 int snd_timer_global_register(struct snd_timer *timer)
1088 struct snd_device dev;
1090 memset(&dev, 0, sizeof(dev));
1091 dev.device_data = timer;
1092 return snd_timer_dev_register(&dev);
1094 EXPORT_SYMBOL(snd_timer_global_register);
1100 struct snd_timer_system_private {
1101 struct timer_list tlist;
1102 struct snd_timer *snd_timer;
1103 unsigned long last_expires;
1104 unsigned long last_jiffies;
1105 unsigned long correction;
1108 static void snd_timer_s_function(struct timer_list *t)
1110 struct snd_timer_system_private *priv = from_timer(priv, t,
1112 struct snd_timer *timer = priv->snd_timer;
1113 unsigned long jiff = jiffies;
1114 if (time_after(jiff, priv->last_expires))
1115 priv->correction += (long)jiff - (long)priv->last_expires;
1116 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1119 static int snd_timer_s_start(struct snd_timer * timer)
1121 struct snd_timer_system_private *priv;
1122 unsigned long njiff;
1124 priv = (struct snd_timer_system_private *) timer->private_data;
1125 njiff = (priv->last_jiffies = jiffies);
1126 if (priv->correction > timer->sticks - 1) {
1127 priv->correction -= timer->sticks - 1;
1130 njiff += timer->sticks - priv->correction;
1131 priv->correction = 0;
1133 priv->last_expires = njiff;
1134 mod_timer(&priv->tlist, njiff);
1138 static int snd_timer_s_stop(struct snd_timer * timer)
1140 struct snd_timer_system_private *priv;
1143 priv = (struct snd_timer_system_private *) timer->private_data;
1144 del_timer(&priv->tlist);
1146 if (time_before(jiff, priv->last_expires))
1147 timer->sticks = priv->last_expires - jiff;
1150 priv->correction = 0;
1154 static int snd_timer_s_close(struct snd_timer *timer)
1156 struct snd_timer_system_private *priv;
1158 priv = (struct snd_timer_system_private *)timer->private_data;
1159 del_timer_sync(&priv->tlist);
1163 static struct snd_timer_hardware snd_timer_system =
1165 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1166 .resolution = 1000000000L / HZ,
1168 .close = snd_timer_s_close,
1169 .start = snd_timer_s_start,
1170 .stop = snd_timer_s_stop
1173 static void snd_timer_free_system(struct snd_timer *timer)
1175 kfree(timer->private_data);
1178 static int snd_timer_register_system(void)
1180 struct snd_timer *timer;
1181 struct snd_timer_system_private *priv;
1184 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1187 strcpy(timer->name, "system timer");
1188 timer->hw = snd_timer_system;
1189 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1191 snd_timer_free(timer);
1194 priv->snd_timer = timer;
1195 timer_setup(&priv->tlist, snd_timer_s_function, 0);
1196 timer->private_data = priv;
1197 timer->private_free = snd_timer_free_system;
1198 return snd_timer_global_register(timer);
1201 #ifdef CONFIG_SND_PROC_FS
1206 static void snd_timer_proc_read(struct snd_info_entry *entry,
1207 struct snd_info_buffer *buffer)
1209 struct snd_timer *timer;
1210 struct snd_timer_instance *ti;
1212 mutex_lock(®ister_mutex);
1213 list_for_each_entry(timer, &snd_timer_list, device_list) {
1214 if (timer->card && timer->card->shutdown)
1216 switch (timer->tmr_class) {
1217 case SNDRV_TIMER_CLASS_GLOBAL:
1218 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1220 case SNDRV_TIMER_CLASS_CARD:
1221 snd_iprintf(buffer, "C%i-%i: ",
1222 timer->card->number, timer->tmr_device);
1224 case SNDRV_TIMER_CLASS_PCM:
1225 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1226 timer->tmr_device, timer->tmr_subdevice);
1229 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1230 timer->card ? timer->card->number : -1,
1231 timer->tmr_device, timer->tmr_subdevice);
1233 snd_iprintf(buffer, "%s :", timer->name);
1234 if (timer->hw.resolution)
1235 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1236 timer->hw.resolution / 1000,
1237 timer->hw.resolution % 1000,
1239 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1240 snd_iprintf(buffer, " SLAVE");
1241 snd_iprintf(buffer, "\n");
1242 list_for_each_entry(ti, &timer->open_list_head, open_list)
1243 snd_iprintf(buffer, " Client %s : %s\n",
1244 ti->owner ? ti->owner : "unknown",
1245 ti->flags & (SNDRV_TIMER_IFLG_START |
1246 SNDRV_TIMER_IFLG_RUNNING)
1247 ? "running" : "stopped");
1249 mutex_unlock(®ister_mutex);
1252 static struct snd_info_entry *snd_timer_proc_entry;
1254 static void __init snd_timer_proc_init(void)
1256 struct snd_info_entry *entry;
1258 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1259 if (entry != NULL) {
1260 entry->c.text.read = snd_timer_proc_read;
1261 if (snd_info_register(entry) < 0) {
1262 snd_info_free_entry(entry);
1266 snd_timer_proc_entry = entry;
1269 static void __exit snd_timer_proc_done(void)
1271 snd_info_free_entry(snd_timer_proc_entry);
1273 #else /* !CONFIG_SND_PROC_FS */
1274 #define snd_timer_proc_init()
1275 #define snd_timer_proc_done()
1279 * USER SPACE interface
1282 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1283 unsigned long resolution,
1284 unsigned long ticks)
1286 struct snd_timer_user *tu = timeri->callback_data;
1287 struct snd_timer_read *r;
1290 spin_lock(&tu->qlock);
1291 if (tu->qused > 0) {
1292 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1293 r = &tu->queue[prev];
1294 if (r->resolution == resolution) {
1299 if (tu->qused >= tu->queue_size) {
1302 r = &tu->queue[tu->qtail++];
1303 tu->qtail %= tu->queue_size;
1304 r->resolution = resolution;
1309 spin_unlock(&tu->qlock);
1310 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1311 wake_up(&tu->qchange_sleep);
1314 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1315 struct snd_timer_tread *tread)
1317 if (tu->qused >= tu->queue_size) {
1320 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1321 tu->qtail %= tu->queue_size;
1326 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1328 struct timespec *tstamp,
1329 unsigned long resolution)
1331 struct snd_timer_user *tu = timeri->callback_data;
1332 struct snd_timer_tread r1;
1333 unsigned long flags;
1335 if (event >= SNDRV_TIMER_EVENT_START &&
1336 event <= SNDRV_TIMER_EVENT_PAUSE)
1337 tu->tstamp = *tstamp;
1338 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1340 memset(&r1, 0, sizeof(r1));
1342 r1.tstamp = *tstamp;
1343 r1.val = resolution;
1344 spin_lock_irqsave(&tu->qlock, flags);
1345 snd_timer_user_append_to_tqueue(tu, &r1);
1346 spin_unlock_irqrestore(&tu->qlock, flags);
1347 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1348 wake_up(&tu->qchange_sleep);
1351 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1353 struct snd_timer_user *tu = timeri->callback_data;
1355 tu->disconnected = true;
1356 wake_up(&tu->qchange_sleep);
1359 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1360 unsigned long resolution,
1361 unsigned long ticks)
1363 struct snd_timer_user *tu = timeri->callback_data;
1364 struct snd_timer_tread *r, r1;
1365 struct timespec tstamp;
1366 int prev, append = 0;
1368 memset(&r1, 0, sizeof(r1));
1369 memset(&tstamp, 0, sizeof(tstamp));
1370 spin_lock(&tu->qlock);
1371 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1372 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1373 spin_unlock(&tu->qlock);
1376 if (tu->last_resolution != resolution || ticks > 0) {
1377 if (timer_tstamp_monotonic)
1378 ktime_get_ts(&tstamp);
1380 getnstimeofday(&tstamp);
1382 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1383 tu->last_resolution != resolution) {
1384 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1386 r1.val = resolution;
1387 snd_timer_user_append_to_tqueue(tu, &r1);
1388 tu->last_resolution = resolution;
1391 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1395 if (tu->qused > 0) {
1396 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1397 r = &tu->tqueue[prev];
1398 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1405 r1.event = SNDRV_TIMER_EVENT_TICK;
1408 snd_timer_user_append_to_tqueue(tu, &r1);
1411 spin_unlock(&tu->qlock);
1414 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1415 wake_up(&tu->qchange_sleep);
1418 static int realloc_user_queue(struct snd_timer_user *tu, int size)
1420 struct snd_timer_read *queue = NULL;
1421 struct snd_timer_tread *tqueue = NULL;
1424 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
1428 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
1433 spin_lock_irq(&tu->qlock);
1436 tu->queue_size = size;
1438 tu->tqueue = tqueue;
1439 tu->qhead = tu->qtail = tu->qused = 0;
1440 spin_unlock_irq(&tu->qlock);
1445 static int snd_timer_user_open(struct inode *inode, struct file *file)
1447 struct snd_timer_user *tu;
1450 err = nonseekable_open(inode, file);
1454 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1457 spin_lock_init(&tu->qlock);
1458 init_waitqueue_head(&tu->qchange_sleep);
1459 mutex_init(&tu->ioctl_lock);
1461 if (realloc_user_queue(tu, 128) < 0) {
1465 file->private_data = tu;
1469 static int snd_timer_user_release(struct inode *inode, struct file *file)
1471 struct snd_timer_user *tu;
1473 if (file->private_data) {
1474 tu = file->private_data;
1475 file->private_data = NULL;
1476 mutex_lock(&tu->ioctl_lock);
1478 snd_timer_close(tu->timeri);
1479 mutex_unlock(&tu->ioctl_lock);
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 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)