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 <sound/core.h>
31 #include <sound/timer.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/minors.h>
35 #include <sound/initval.h>
36 #include <linux/kmod.h>
39 #define SNDRV_TIMER_IFLG_PAUSED 0x00010000
41 #if IS_ENABLED(CONFIG_SND_HRTIMER)
42 #define DEFAULT_TIMER_LIMIT 4
44 #define DEFAULT_TIMER_LIMIT 1
47 static int timer_limit = DEFAULT_TIMER_LIMIT;
48 static int timer_tstamp_monotonic = 1;
49 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
50 MODULE_DESCRIPTION("ALSA timer interface");
51 MODULE_LICENSE("GPL");
52 module_param(timer_limit, int, 0444);
53 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
54 module_param(timer_tstamp_monotonic, int, 0444);
55 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
57 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
58 MODULE_ALIAS("devname:snd/timer");
60 struct snd_timer_user {
61 struct snd_timer_instance *timeri;
62 int tread; /* enhanced read with timestamps and events */
64 unsigned long overrun;
70 struct snd_timer_read *queue;
71 struct snd_timer_tread *tqueue;
73 unsigned long last_resolution;
75 struct timespec tstamp; /* trigger tstamp */
76 wait_queue_head_t qchange_sleep;
77 struct fasync_struct *fasync;
78 struct mutex ioctl_lock;
82 static LIST_HEAD(snd_timer_list);
84 /* list of slave instances */
85 static LIST_HEAD(snd_timer_slave_list);
87 /* lock for slave active lists */
88 static DEFINE_SPINLOCK(slave_active_lock);
90 #define MAX_SLAVE_INSTANCES 1000
91 static int num_slaves;
93 static DEFINE_MUTEX(register_mutex);
95 static int snd_timer_free(struct snd_timer *timer);
96 static int snd_timer_dev_free(struct snd_device *device);
97 static int snd_timer_dev_register(struct snd_device *device);
98 static int snd_timer_dev_disconnect(struct snd_device *device);
100 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
103 * create a timer instance with the given owner string.
104 * when timer is not NULL, increments the module counter
106 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
107 struct snd_timer *timer)
109 struct snd_timer_instance *timeri;
110 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
113 timeri->owner = kstrdup(owner, GFP_KERNEL);
114 if (! timeri->owner) {
118 INIT_LIST_HEAD(&timeri->open_list);
119 INIT_LIST_HEAD(&timeri->active_list);
120 INIT_LIST_HEAD(&timeri->ack_list);
121 INIT_LIST_HEAD(&timeri->slave_list_head);
122 INIT_LIST_HEAD(&timeri->slave_active_head);
124 timeri->timer = timer;
125 if (timer && !try_module_get(timer->module)) {
126 kfree(timeri->owner);
135 * find a timer instance from the given timer id
137 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
139 struct snd_timer *timer = NULL;
141 list_for_each_entry(timer, &snd_timer_list, device_list) {
142 if (timer->tmr_class != tid->dev_class)
144 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
145 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
146 (timer->card == NULL ||
147 timer->card->number != tid->card))
149 if (timer->tmr_device != tid->device)
151 if (timer->tmr_subdevice != tid->subdevice)
158 #ifdef CONFIG_MODULES
160 static void snd_timer_request(struct snd_timer_id *tid)
162 switch (tid->dev_class) {
163 case SNDRV_TIMER_CLASS_GLOBAL:
164 if (tid->device < timer_limit)
165 request_module("snd-timer-%i", tid->device);
167 case SNDRV_TIMER_CLASS_CARD:
168 case SNDRV_TIMER_CLASS_PCM:
169 if (tid->card < snd_ecards_limit)
170 request_module("snd-card-%i", tid->card);
180 * look for a master instance matching with the slave id of the given slave.
181 * when found, relink the open_link of the slave.
183 * call this with register_mutex down.
185 static int snd_timer_check_slave(struct snd_timer_instance *slave)
187 struct snd_timer *timer;
188 struct snd_timer_instance *master;
190 /* FIXME: it's really dumb to look up all entries.. */
191 list_for_each_entry(timer, &snd_timer_list, device_list) {
192 list_for_each_entry(master, &timer->open_list_head, open_list) {
193 if (slave->slave_class == master->slave_class &&
194 slave->slave_id == master->slave_id) {
195 if (master->timer->num_instances >=
196 master->timer->max_instances)
198 list_move_tail(&slave->open_list,
199 &master->slave_list_head);
200 master->timer->num_instances++;
201 spin_lock_irq(&slave_active_lock);
202 slave->master = master;
203 slave->timer = master->timer;
204 spin_unlock_irq(&slave_active_lock);
213 * look for slave instances matching with the slave id of the given master.
214 * when found, relink the open_link of slaves.
216 * call this with register_mutex down.
218 static int snd_timer_check_master(struct snd_timer_instance *master)
220 struct snd_timer_instance *slave, *tmp;
222 /* check all pending slaves */
223 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
224 if (slave->slave_class == master->slave_class &&
225 slave->slave_id == master->slave_id) {
226 if (master->timer->num_instances >=
227 master->timer->max_instances)
229 list_move_tail(&slave->open_list, &master->slave_list_head);
230 master->timer->num_instances++;
231 spin_lock_irq(&slave_active_lock);
232 spin_lock(&master->timer->lock);
233 slave->master = master;
234 slave->timer = master->timer;
235 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
236 list_add_tail(&slave->active_list,
237 &master->slave_active_head);
238 spin_unlock(&master->timer->lock);
239 spin_unlock_irq(&slave_active_lock);
245 static int snd_timer_close_locked(struct snd_timer_instance *timeri,
246 struct device **card_devp_to_put);
249 * open a timer instance
250 * when opening a master, the slave id must be here given.
252 int snd_timer_open(struct snd_timer_instance **ti,
253 char *owner, struct snd_timer_id *tid,
254 unsigned int slave_id)
256 struct snd_timer *timer;
257 struct snd_timer_instance *timeri = NULL;
258 struct device *card_dev_to_put = NULL;
261 mutex_lock(®ister_mutex);
262 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
263 /* open a slave instance */
264 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
265 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
266 pr_debug("ALSA: timer: invalid slave class %i\n",
271 if (num_slaves >= MAX_SLAVE_INSTANCES) {
275 timeri = snd_timer_instance_new(owner, NULL);
280 timeri->slave_class = tid->dev_sclass;
281 timeri->slave_id = tid->device;
282 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
283 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
285 err = snd_timer_check_slave(timeri);
287 snd_timer_close_locked(timeri, &card_dev_to_put);
293 /* open a master instance */
294 timer = snd_timer_find(tid);
295 #ifdef CONFIG_MODULES
297 mutex_unlock(®ister_mutex);
298 snd_timer_request(tid);
299 mutex_lock(®ister_mutex);
300 timer = snd_timer_find(tid);
307 if (!list_empty(&timer->open_list_head)) {
308 struct snd_timer_instance *t =
309 list_entry(timer->open_list_head.next,
310 struct snd_timer_instance, open_list);
311 if (t->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
316 if (timer->num_instances >= timer->max_instances) {
320 timeri = snd_timer_instance_new(owner, timer);
325 /* take a card refcount for safe disconnection */
327 get_device(&timer->card->card_dev);
328 timeri->slave_class = tid->dev_sclass;
329 timeri->slave_id = slave_id;
331 if (list_empty(&timer->open_list_head) && timer->hw.open) {
332 err = timer->hw.open(timer);
334 kfree(timeri->owner);
339 card_dev_to_put = &timer->card->card_dev;
340 module_put(timer->module);
345 list_add_tail(&timeri->open_list, &timer->open_list_head);
346 timer->num_instances++;
347 err = snd_timer_check_master(timeri);
349 snd_timer_close_locked(timeri, &card_dev_to_put);
354 mutex_unlock(®ister_mutex);
355 /* put_device() is called after unlock for avoiding deadlock */
357 put_device(card_dev_to_put);
361 EXPORT_SYMBOL(snd_timer_open);
364 * close a timer instance
365 * call this with register_mutex down.
367 static int snd_timer_close_locked(struct snd_timer_instance *timeri,
368 struct device **card_devp_to_put)
370 struct snd_timer *timer = NULL;
371 struct snd_timer_instance *slave, *tmp;
373 list_del(&timeri->open_list);
374 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
377 /* force to stop the timer */
378 snd_timer_stop(timeri);
380 timer = timeri->timer;
382 timer->num_instances--;
383 /* wait, until the active callback is finished */
384 spin_lock_irq(&timer->lock);
385 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
386 spin_unlock_irq(&timer->lock);
388 spin_lock_irq(&timer->lock);
390 spin_unlock_irq(&timer->lock);
392 /* remove slave links */
393 spin_lock_irq(&slave_active_lock);
394 spin_lock(&timer->lock);
395 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
397 list_move_tail(&slave->open_list, &snd_timer_slave_list);
398 timer->num_instances--;
399 slave->master = NULL;
401 list_del_init(&slave->ack_list);
402 list_del_init(&slave->active_list);
404 spin_unlock(&timer->lock);
405 spin_unlock_irq(&slave_active_lock);
407 /* slave doesn't need to release timer resources below */
408 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
412 if (timeri->private_free)
413 timeri->private_free(timeri);
414 kfree(timeri->owner);
418 if (list_empty(&timer->open_list_head) && timer->hw.close)
419 timer->hw.close(timer);
420 /* release a card refcount for safe disconnection */
422 *card_devp_to_put = &timer->card->card_dev;
423 module_put(timer->module);
430 * close a timer instance
432 int snd_timer_close(struct snd_timer_instance *timeri)
434 struct device *card_dev_to_put = NULL;
437 if (snd_BUG_ON(!timeri))
440 mutex_lock(®ister_mutex);
441 err = snd_timer_close_locked(timeri, &card_dev_to_put);
442 mutex_unlock(®ister_mutex);
443 /* put_device() is called after unlock for avoiding deadlock */
445 put_device(card_dev_to_put);
448 EXPORT_SYMBOL(snd_timer_close);
450 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
452 struct snd_timer * timer;
456 if ((timer = timeri->timer) != NULL) {
457 if (timer->hw.c_resolution)
458 return timer->hw.c_resolution(timer);
459 return timer->hw.resolution;
463 EXPORT_SYMBOL(snd_timer_resolution);
465 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
467 struct snd_timer *timer;
468 unsigned long resolution = 0;
469 struct snd_timer_instance *ts;
470 struct timespec tstamp;
472 if (timer_tstamp_monotonic)
473 ktime_get_ts(&tstamp);
475 getnstimeofday(&tstamp);
476 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
477 event > SNDRV_TIMER_EVENT_PAUSE))
479 if (event == SNDRV_TIMER_EVENT_START ||
480 event == SNDRV_TIMER_EVENT_CONTINUE)
481 resolution = snd_timer_resolution(ti);
483 ti->ccallback(ti, event, &tstamp, resolution);
484 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
489 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
491 event += 10; /* convert to SNDRV_TIMER_EVENT_MXXX */
492 list_for_each_entry(ts, &ti->slave_active_head, active_list)
494 ts->ccallback(ts, event, &tstamp, resolution);
497 /* start/continue a master timer */
498 static int snd_timer_start1(struct snd_timer_instance *timeri,
499 bool start, unsigned long ticks)
501 struct snd_timer *timer;
505 timer = timeri->timer;
509 spin_lock_irqsave(&timer->lock, flags);
510 if (timer->card && timer->card->shutdown) {
514 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
515 SNDRV_TIMER_IFLG_START)) {
521 timeri->ticks = timeri->cticks = ticks;
522 else if (!timeri->cticks)
526 list_move_tail(&timeri->active_list, &timer->active_list_head);
527 if (timer->running) {
528 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
530 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
531 timeri->flags |= SNDRV_TIMER_IFLG_START;
532 result = 1; /* delayed start */
535 timer->sticks = ticks;
536 timer->hw.start(timer);
539 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
542 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
543 SNDRV_TIMER_EVENT_CONTINUE);
545 spin_unlock_irqrestore(&timer->lock, flags);
549 /* start/continue a slave timer */
550 static int snd_timer_start_slave(struct snd_timer_instance *timeri,
555 spin_lock_irqsave(&slave_active_lock, flags);
556 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
557 spin_unlock_irqrestore(&slave_active_lock, flags);
560 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
561 if (timeri->master && timeri->timer) {
562 spin_lock(&timeri->timer->lock);
563 list_add_tail(&timeri->active_list,
564 &timeri->master->slave_active_head);
565 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
566 SNDRV_TIMER_EVENT_CONTINUE);
567 spin_unlock(&timeri->timer->lock);
569 spin_unlock_irqrestore(&slave_active_lock, flags);
570 return 1; /* delayed start */
573 /* stop/pause a master timer */
574 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
576 struct snd_timer *timer;
580 timer = timeri->timer;
583 spin_lock_irqsave(&timer->lock, flags);
584 list_del_init(&timeri->ack_list);
585 list_del_init(&timeri->active_list);
586 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
587 SNDRV_TIMER_IFLG_START))) {
591 if (timer->card && timer->card->shutdown)
594 timeri->cticks = timeri->ticks;
597 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
598 !(--timer->running)) {
599 timer->hw.stop(timer);
600 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
601 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
602 snd_timer_reschedule(timer, 0);
603 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
604 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
605 timer->hw.start(timer);
609 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
611 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
613 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
614 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
615 SNDRV_TIMER_EVENT_PAUSE);
617 spin_unlock_irqrestore(&timer->lock, flags);
621 /* stop/pause a slave timer */
622 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
627 spin_lock_irqsave(&slave_active_lock, flags);
628 running = timeri->flags & SNDRV_TIMER_IFLG_RUNNING;
629 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
631 spin_lock(&timeri->timer->lock);
632 list_del_init(&timeri->ack_list);
633 list_del_init(&timeri->active_list);
635 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
636 SNDRV_TIMER_EVENT_PAUSE);
637 spin_unlock(&timeri->timer->lock);
639 spin_unlock_irqrestore(&slave_active_lock, flags);
640 return running ? 0 : -EBUSY;
644 * start the timer instance
646 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
648 if (timeri == NULL || ticks < 1)
650 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
651 return snd_timer_start_slave(timeri, true);
653 return snd_timer_start1(timeri, true, ticks);
655 EXPORT_SYMBOL(snd_timer_start);
658 * stop the timer instance.
660 * do not call this from the timer callback!
662 int snd_timer_stop(struct snd_timer_instance *timeri)
664 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
665 return snd_timer_stop_slave(timeri, true);
667 return snd_timer_stop1(timeri, true);
669 EXPORT_SYMBOL(snd_timer_stop);
672 * start again.. the tick is kept.
674 int snd_timer_continue(struct snd_timer_instance *timeri)
676 /* timer can continue only after pause */
677 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
680 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
681 return snd_timer_start_slave(timeri, false);
683 return snd_timer_start1(timeri, false, 0);
685 EXPORT_SYMBOL(snd_timer_continue);
688 * pause.. remember the ticks left
690 int snd_timer_pause(struct snd_timer_instance * timeri)
692 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
693 return snd_timer_stop_slave(timeri, false);
695 return snd_timer_stop1(timeri, false);
697 EXPORT_SYMBOL(snd_timer_pause);
700 * reschedule the timer
702 * start pending instances and check the scheduling ticks.
703 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
705 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
707 struct snd_timer_instance *ti;
708 unsigned long ticks = ~0UL;
710 list_for_each_entry(ti, &timer->active_list_head, active_list) {
711 if (ti->flags & SNDRV_TIMER_IFLG_START) {
712 ti->flags &= ~SNDRV_TIMER_IFLG_START;
713 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
716 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
717 if (ticks > ti->cticks)
722 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
725 if (ticks > timer->hw.ticks)
726 ticks = timer->hw.ticks;
727 if (ticks_left != ticks)
728 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
729 timer->sticks = ticks;
736 static void snd_timer_tasklet(unsigned long arg)
738 struct snd_timer *timer = (struct snd_timer *) arg;
739 struct snd_timer_instance *ti;
741 unsigned long resolution, ticks;
744 if (timer->card && timer->card->shutdown)
747 spin_lock_irqsave(&timer->lock, flags);
748 /* now process all callbacks */
749 while (!list_empty(&timer->sack_list_head)) {
750 p = timer->sack_list_head.next; /* get first item */
751 ti = list_entry(p, struct snd_timer_instance, ack_list);
753 /* remove from ack_list and make empty */
758 resolution = ti->resolution;
760 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
761 spin_unlock(&timer->lock);
763 ti->callback(ti, resolution, ticks);
764 spin_lock(&timer->lock);
765 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
767 spin_unlock_irqrestore(&timer->lock, flags);
773 * ticks_left is usually equal to timer->sticks.
776 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
778 struct snd_timer_instance *ti, *ts, *tmp;
779 unsigned long resolution, ticks;
780 struct list_head *p, *ack_list_head;
787 if (timer->card && timer->card->shutdown)
790 spin_lock_irqsave(&timer->lock, flags);
792 /* remember the current resolution */
793 if (timer->hw.c_resolution)
794 resolution = timer->hw.c_resolution(timer);
796 resolution = timer->hw.resolution;
798 /* loop for all active instances
799 * Here we cannot use list_for_each_entry because the active_list of a
800 * processed instance is relinked to done_list_head before the callback
803 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
805 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
807 ti->pticks += ticks_left;
808 ti->resolution = resolution;
809 if (ti->cticks < ticks_left)
812 ti->cticks -= ticks_left;
813 if (ti->cticks) /* not expired */
815 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
816 ti->cticks = ti->ticks;
818 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
820 list_del_init(&ti->active_list);
822 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
823 (ti->flags & SNDRV_TIMER_IFLG_FAST))
824 ack_list_head = &timer->ack_list_head;
826 ack_list_head = &timer->sack_list_head;
827 if (list_empty(&ti->ack_list))
828 list_add_tail(&ti->ack_list, ack_list_head);
829 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
830 ts->pticks = ti->pticks;
831 ts->resolution = resolution;
832 if (list_empty(&ts->ack_list))
833 list_add_tail(&ts->ack_list, ack_list_head);
836 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
837 snd_timer_reschedule(timer, timer->sticks);
838 if (timer->running) {
839 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
840 timer->hw.stop(timer);
841 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
843 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
844 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
846 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
847 timer->hw.start(timer);
850 timer->hw.stop(timer);
853 /* now process all fast callbacks */
854 while (!list_empty(&timer->ack_list_head)) {
855 p = timer->ack_list_head.next; /* get first item */
856 ti = list_entry(p, struct snd_timer_instance, ack_list);
858 /* remove from ack_list and make empty */
864 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
865 spin_unlock(&timer->lock);
867 ti->callback(ti, resolution, ticks);
868 spin_lock(&timer->lock);
869 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
872 /* do we have any slow callbacks? */
873 use_tasklet = !list_empty(&timer->sack_list_head);
874 spin_unlock_irqrestore(&timer->lock, flags);
877 tasklet_schedule(&timer->task_queue);
879 EXPORT_SYMBOL(snd_timer_interrupt);
885 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
886 struct snd_timer **rtimer)
888 struct snd_timer *timer;
890 static struct snd_device_ops ops = {
891 .dev_free = snd_timer_dev_free,
892 .dev_register = snd_timer_dev_register,
893 .dev_disconnect = snd_timer_dev_disconnect,
896 if (snd_BUG_ON(!tid))
900 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
903 timer->tmr_class = tid->dev_class;
905 timer->tmr_device = tid->device;
906 timer->tmr_subdevice = tid->subdevice;
908 strlcpy(timer->id, id, sizeof(timer->id));
910 INIT_LIST_HEAD(&timer->device_list);
911 INIT_LIST_HEAD(&timer->open_list_head);
912 INIT_LIST_HEAD(&timer->active_list_head);
913 INIT_LIST_HEAD(&timer->ack_list_head);
914 INIT_LIST_HEAD(&timer->sack_list_head);
915 spin_lock_init(&timer->lock);
916 tasklet_init(&timer->task_queue, snd_timer_tasklet,
917 (unsigned long)timer);
918 timer->max_instances = 1000; /* default limit per timer */
920 timer->module = card->module;
921 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
923 snd_timer_free(timer);
931 EXPORT_SYMBOL(snd_timer_new);
933 static int snd_timer_free(struct snd_timer *timer)
938 mutex_lock(®ister_mutex);
939 if (! list_empty(&timer->open_list_head)) {
940 struct list_head *p, *n;
941 struct snd_timer_instance *ti;
942 pr_warn("ALSA: timer %p is busy?\n", timer);
943 list_for_each_safe(p, n, &timer->open_list_head) {
945 ti = list_entry(p, struct snd_timer_instance, open_list);
949 list_del(&timer->device_list);
950 mutex_unlock(®ister_mutex);
952 if (timer->private_free)
953 timer->private_free(timer);
958 static int snd_timer_dev_free(struct snd_device *device)
960 struct snd_timer *timer = device->device_data;
961 return snd_timer_free(timer);
964 static int snd_timer_dev_register(struct snd_device *dev)
966 struct snd_timer *timer = dev->device_data;
967 struct snd_timer *timer1;
969 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
971 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
972 !timer->hw.resolution && timer->hw.c_resolution == NULL)
975 mutex_lock(®ister_mutex);
976 list_for_each_entry(timer1, &snd_timer_list, device_list) {
977 if (timer1->tmr_class > timer->tmr_class)
979 if (timer1->tmr_class < timer->tmr_class)
981 if (timer1->card && timer->card) {
982 if (timer1->card->number > timer->card->number)
984 if (timer1->card->number < timer->card->number)
987 if (timer1->tmr_device > timer->tmr_device)
989 if (timer1->tmr_device < timer->tmr_device)
991 if (timer1->tmr_subdevice > timer->tmr_subdevice)
993 if (timer1->tmr_subdevice < timer->tmr_subdevice)
996 mutex_unlock(®ister_mutex);
999 list_add_tail(&timer->device_list, &timer1->device_list);
1000 mutex_unlock(®ister_mutex);
1004 static int snd_timer_dev_disconnect(struct snd_device *device)
1006 struct snd_timer *timer = device->device_data;
1007 struct snd_timer_instance *ti;
1009 mutex_lock(®ister_mutex);
1010 list_del_init(&timer->device_list);
1011 /* wake up pending sleepers */
1012 list_for_each_entry(ti, &timer->open_list_head, open_list) {
1016 mutex_unlock(®ister_mutex);
1020 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
1022 unsigned long flags;
1023 unsigned long resolution = 0;
1024 struct snd_timer_instance *ti, *ts;
1026 if (timer->card && timer->card->shutdown)
1028 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1030 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1031 event > SNDRV_TIMER_EVENT_MRESUME))
1033 spin_lock_irqsave(&timer->lock, flags);
1034 if (event == SNDRV_TIMER_EVENT_MSTART ||
1035 event == SNDRV_TIMER_EVENT_MCONTINUE ||
1036 event == SNDRV_TIMER_EVENT_MRESUME) {
1037 if (timer->hw.c_resolution)
1038 resolution = timer->hw.c_resolution(timer);
1040 resolution = timer->hw.resolution;
1042 list_for_each_entry(ti, &timer->active_list_head, active_list) {
1044 ti->ccallback(ti, event, tstamp, resolution);
1045 list_for_each_entry(ts, &ti->slave_active_head, active_list)
1047 ts->ccallback(ts, event, tstamp, resolution);
1049 spin_unlock_irqrestore(&timer->lock, flags);
1051 EXPORT_SYMBOL(snd_timer_notify);
1054 * exported functions for global timers
1056 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1058 struct snd_timer_id tid;
1060 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1061 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1063 tid.device = device;
1065 return snd_timer_new(NULL, id, &tid, rtimer);
1067 EXPORT_SYMBOL(snd_timer_global_new);
1069 int snd_timer_global_free(struct snd_timer *timer)
1071 return snd_timer_free(timer);
1073 EXPORT_SYMBOL(snd_timer_global_free);
1075 int snd_timer_global_register(struct snd_timer *timer)
1077 struct snd_device dev;
1079 memset(&dev, 0, sizeof(dev));
1080 dev.device_data = timer;
1081 return snd_timer_dev_register(&dev);
1083 EXPORT_SYMBOL(snd_timer_global_register);
1089 struct snd_timer_system_private {
1090 struct timer_list tlist;
1091 unsigned long last_expires;
1092 unsigned long last_jiffies;
1093 unsigned long correction;
1096 static void snd_timer_s_function(unsigned long data)
1098 struct snd_timer *timer = (struct snd_timer *)data;
1099 struct snd_timer_system_private *priv = timer->private_data;
1100 unsigned long jiff = jiffies;
1101 if (time_after(jiff, priv->last_expires))
1102 priv->correction += (long)jiff - (long)priv->last_expires;
1103 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1106 static int snd_timer_s_start(struct snd_timer * timer)
1108 struct snd_timer_system_private *priv;
1109 unsigned long njiff;
1111 priv = (struct snd_timer_system_private *) timer->private_data;
1112 njiff = (priv->last_jiffies = jiffies);
1113 if (priv->correction > timer->sticks - 1) {
1114 priv->correction -= timer->sticks - 1;
1117 njiff += timer->sticks - priv->correction;
1118 priv->correction = 0;
1120 priv->last_expires = njiff;
1121 mod_timer(&priv->tlist, njiff);
1125 static int snd_timer_s_stop(struct snd_timer * timer)
1127 struct snd_timer_system_private *priv;
1130 priv = (struct snd_timer_system_private *) timer->private_data;
1131 del_timer(&priv->tlist);
1133 if (time_before(jiff, priv->last_expires))
1134 timer->sticks = priv->last_expires - jiff;
1137 priv->correction = 0;
1141 static int snd_timer_s_close(struct snd_timer *timer)
1143 struct snd_timer_system_private *priv;
1145 priv = (struct snd_timer_system_private *)timer->private_data;
1146 del_timer_sync(&priv->tlist);
1150 static struct snd_timer_hardware snd_timer_system =
1152 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1153 .resolution = 1000000000L / HZ,
1155 .close = snd_timer_s_close,
1156 .start = snd_timer_s_start,
1157 .stop = snd_timer_s_stop
1160 static void snd_timer_free_system(struct snd_timer *timer)
1162 kfree(timer->private_data);
1165 static int snd_timer_register_system(void)
1167 struct snd_timer *timer;
1168 struct snd_timer_system_private *priv;
1171 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1174 strcpy(timer->name, "system timer");
1175 timer->hw = snd_timer_system;
1176 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1178 snd_timer_free(timer);
1181 setup_timer(&priv->tlist, snd_timer_s_function, (unsigned long) timer);
1182 timer->private_data = priv;
1183 timer->private_free = snd_timer_free_system;
1184 return snd_timer_global_register(timer);
1187 #ifdef CONFIG_SND_PROC_FS
1192 static void snd_timer_proc_read(struct snd_info_entry *entry,
1193 struct snd_info_buffer *buffer)
1195 struct snd_timer *timer;
1196 struct snd_timer_instance *ti;
1198 mutex_lock(®ister_mutex);
1199 list_for_each_entry(timer, &snd_timer_list, device_list) {
1200 if (timer->card && timer->card->shutdown)
1202 switch (timer->tmr_class) {
1203 case SNDRV_TIMER_CLASS_GLOBAL:
1204 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1206 case SNDRV_TIMER_CLASS_CARD:
1207 snd_iprintf(buffer, "C%i-%i: ",
1208 timer->card->number, timer->tmr_device);
1210 case SNDRV_TIMER_CLASS_PCM:
1211 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1212 timer->tmr_device, timer->tmr_subdevice);
1215 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1216 timer->card ? timer->card->number : -1,
1217 timer->tmr_device, timer->tmr_subdevice);
1219 snd_iprintf(buffer, "%s :", timer->name);
1220 if (timer->hw.resolution)
1221 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1222 timer->hw.resolution / 1000,
1223 timer->hw.resolution % 1000,
1225 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1226 snd_iprintf(buffer, " SLAVE");
1227 snd_iprintf(buffer, "\n");
1228 list_for_each_entry(ti, &timer->open_list_head, open_list)
1229 snd_iprintf(buffer, " Client %s : %s\n",
1230 ti->owner ? ti->owner : "unknown",
1231 ti->flags & (SNDRV_TIMER_IFLG_START |
1232 SNDRV_TIMER_IFLG_RUNNING)
1233 ? "running" : "stopped");
1235 mutex_unlock(®ister_mutex);
1238 static struct snd_info_entry *snd_timer_proc_entry;
1240 static void __init snd_timer_proc_init(void)
1242 struct snd_info_entry *entry;
1244 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1245 if (entry != NULL) {
1246 entry->c.text.read = snd_timer_proc_read;
1247 if (snd_info_register(entry) < 0) {
1248 snd_info_free_entry(entry);
1252 snd_timer_proc_entry = entry;
1255 static void __exit snd_timer_proc_done(void)
1257 snd_info_free_entry(snd_timer_proc_entry);
1259 #else /* !CONFIG_SND_PROC_FS */
1260 #define snd_timer_proc_init()
1261 #define snd_timer_proc_done()
1265 * USER SPACE interface
1268 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1269 unsigned long resolution,
1270 unsigned long ticks)
1272 struct snd_timer_user *tu = timeri->callback_data;
1273 struct snd_timer_read *r;
1276 spin_lock(&tu->qlock);
1277 if (tu->qused > 0) {
1278 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1279 r = &tu->queue[prev];
1280 if (r->resolution == resolution) {
1285 if (tu->qused >= tu->queue_size) {
1288 r = &tu->queue[tu->qtail++];
1289 tu->qtail %= tu->queue_size;
1290 r->resolution = resolution;
1295 spin_unlock(&tu->qlock);
1296 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1297 wake_up(&tu->qchange_sleep);
1300 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1301 struct snd_timer_tread *tread)
1303 if (tu->qused >= tu->queue_size) {
1306 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1307 tu->qtail %= tu->queue_size;
1312 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1314 struct timespec *tstamp,
1315 unsigned long resolution)
1317 struct snd_timer_user *tu = timeri->callback_data;
1318 struct snd_timer_tread r1;
1319 unsigned long flags;
1321 if (event >= SNDRV_TIMER_EVENT_START &&
1322 event <= SNDRV_TIMER_EVENT_PAUSE)
1323 tu->tstamp = *tstamp;
1324 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1326 memset(&r1, 0, sizeof(r1));
1328 r1.tstamp = *tstamp;
1329 r1.val = resolution;
1330 spin_lock_irqsave(&tu->qlock, flags);
1331 snd_timer_user_append_to_tqueue(tu, &r1);
1332 spin_unlock_irqrestore(&tu->qlock, flags);
1333 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1334 wake_up(&tu->qchange_sleep);
1337 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1339 struct snd_timer_user *tu = timeri->callback_data;
1341 tu->disconnected = true;
1342 wake_up(&tu->qchange_sleep);
1345 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1346 unsigned long resolution,
1347 unsigned long ticks)
1349 struct snd_timer_user *tu = timeri->callback_data;
1350 struct snd_timer_tread *r, r1;
1351 struct timespec tstamp;
1352 int prev, append = 0;
1354 memset(&tstamp, 0, sizeof(tstamp));
1355 spin_lock(&tu->qlock);
1356 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1357 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1358 spin_unlock(&tu->qlock);
1361 if (tu->last_resolution != resolution || ticks > 0) {
1362 if (timer_tstamp_monotonic)
1363 ktime_get_ts(&tstamp);
1365 getnstimeofday(&tstamp);
1367 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1368 tu->last_resolution != resolution) {
1369 memset(&r1, 0, sizeof(r1));
1370 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1372 r1.val = resolution;
1373 snd_timer_user_append_to_tqueue(tu, &r1);
1374 tu->last_resolution = resolution;
1377 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1381 if (tu->qused > 0) {
1382 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1383 r = &tu->tqueue[prev];
1384 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1391 r1.event = SNDRV_TIMER_EVENT_TICK;
1394 snd_timer_user_append_to_tqueue(tu, &r1);
1397 spin_unlock(&tu->qlock);
1400 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1401 wake_up(&tu->qchange_sleep);
1404 static int snd_timer_user_open(struct inode *inode, struct file *file)
1406 struct snd_timer_user *tu;
1409 err = nonseekable_open(inode, file);
1413 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1416 spin_lock_init(&tu->qlock);
1417 init_waitqueue_head(&tu->qchange_sleep);
1418 mutex_init(&tu->ioctl_lock);
1420 tu->queue_size = 128;
1421 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1423 if (tu->queue == NULL) {
1427 file->private_data = tu;
1431 static int snd_timer_user_release(struct inode *inode, struct file *file)
1433 struct snd_timer_user *tu;
1435 if (file->private_data) {
1436 tu = file->private_data;
1437 file->private_data = NULL;
1438 mutex_lock(&tu->ioctl_lock);
1440 snd_timer_close(tu->timeri);
1441 mutex_unlock(&tu->ioctl_lock);
1449 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1451 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1452 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1458 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1460 id->dev_class = timer->tmr_class;
1461 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1462 id->card = timer->card ? timer->card->number : -1;
1463 id->device = timer->tmr_device;
1464 id->subdevice = timer->tmr_subdevice;
1467 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1469 struct snd_timer_id id;
1470 struct snd_timer *timer;
1471 struct list_head *p;
1473 if (copy_from_user(&id, _tid, sizeof(id)))
1475 mutex_lock(®ister_mutex);
1476 if (id.dev_class < 0) { /* first item */
1477 if (list_empty(&snd_timer_list))
1478 snd_timer_user_zero_id(&id);
1480 timer = list_entry(snd_timer_list.next,
1481 struct snd_timer, device_list);
1482 snd_timer_user_copy_id(&id, timer);
1485 switch (id.dev_class) {
1486 case SNDRV_TIMER_CLASS_GLOBAL:
1487 id.device = id.device < 0 ? 0 : id.device + 1;
1488 list_for_each(p, &snd_timer_list) {
1489 timer = list_entry(p, struct snd_timer, device_list);
1490 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1491 snd_timer_user_copy_id(&id, timer);
1494 if (timer->tmr_device >= id.device) {
1495 snd_timer_user_copy_id(&id, timer);
1499 if (p == &snd_timer_list)
1500 snd_timer_user_zero_id(&id);
1502 case SNDRV_TIMER_CLASS_CARD:
1503 case SNDRV_TIMER_CLASS_PCM:
1510 if (id.device < 0) {
1513 if (id.subdevice < 0) {
1521 list_for_each(p, &snd_timer_list) {
1522 timer = list_entry(p, struct snd_timer, device_list);
1523 if (timer->tmr_class > id.dev_class) {
1524 snd_timer_user_copy_id(&id, timer);
1527 if (timer->tmr_class < id.dev_class)
1529 if (timer->card->number > id.card) {
1530 snd_timer_user_copy_id(&id, timer);
1533 if (timer->card->number < id.card)
1535 if (timer->tmr_device > id.device) {
1536 snd_timer_user_copy_id(&id, timer);
1539 if (timer->tmr_device < id.device)
1541 if (timer->tmr_subdevice > id.subdevice) {
1542 snd_timer_user_copy_id(&id, timer);
1545 if (timer->tmr_subdevice < id.subdevice)
1547 snd_timer_user_copy_id(&id, timer);
1550 if (p == &snd_timer_list)
1551 snd_timer_user_zero_id(&id);
1554 snd_timer_user_zero_id(&id);
1557 mutex_unlock(®ister_mutex);
1558 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1563 static int snd_timer_user_ginfo(struct file *file,
1564 struct snd_timer_ginfo __user *_ginfo)
1566 struct snd_timer_ginfo *ginfo;
1567 struct snd_timer_id tid;
1568 struct snd_timer *t;
1569 struct list_head *p;
1572 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1574 return PTR_ERR(ginfo);
1577 memset(ginfo, 0, sizeof(*ginfo));
1579 mutex_lock(®ister_mutex);
1580 t = snd_timer_find(&tid);
1582 ginfo->card = t->card ? t->card->number : -1;
1583 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1584 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1585 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1586 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1587 ginfo->resolution = t->hw.resolution;
1588 if (t->hw.resolution_min > 0) {
1589 ginfo->resolution_min = t->hw.resolution_min;
1590 ginfo->resolution_max = t->hw.resolution_max;
1592 list_for_each(p, &t->open_list_head) {
1598 mutex_unlock(®ister_mutex);
1599 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1605 static int timer_set_gparams(struct snd_timer_gparams *gparams)
1607 struct snd_timer *t;
1610 mutex_lock(®ister_mutex);
1611 t = snd_timer_find(&gparams->tid);
1616 if (!list_empty(&t->open_list_head)) {
1620 if (!t->hw.set_period) {
1624 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
1626 mutex_unlock(®ister_mutex);
1630 static int snd_timer_user_gparams(struct file *file,
1631 struct snd_timer_gparams __user *_gparams)
1633 struct snd_timer_gparams gparams;
1635 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1637 return timer_set_gparams(&gparams);
1640 static int snd_timer_user_gstatus(struct file *file,
1641 struct snd_timer_gstatus __user *_gstatus)
1643 struct snd_timer_gstatus gstatus;
1644 struct snd_timer_id tid;
1645 struct snd_timer *t;
1648 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1651 memset(&gstatus, 0, sizeof(gstatus));
1653 mutex_lock(®ister_mutex);
1654 t = snd_timer_find(&tid);
1656 if (t->hw.c_resolution)
1657 gstatus.resolution = t->hw.c_resolution(t);
1659 gstatus.resolution = t->hw.resolution;
1660 if (t->hw.precise_resolution) {
1661 t->hw.precise_resolution(t, &gstatus.resolution_num,
1662 &gstatus.resolution_den);
1664 gstatus.resolution_num = gstatus.resolution;
1665 gstatus.resolution_den = 1000000000uL;
1670 mutex_unlock(®ister_mutex);
1671 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1676 static int snd_timer_user_tselect(struct file *file,
1677 struct snd_timer_select __user *_tselect)
1679 struct snd_timer_user *tu;
1680 struct snd_timer_select tselect;
1684 tu = file->private_data;
1686 snd_timer_close(tu->timeri);
1689 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1693 sprintf(str, "application %i", current->pid);
1694 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1695 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1696 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1700 tu->qhead = tu->qtail = tu->qused = 0;
1706 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1708 if (tu->tqueue == NULL)
1711 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1713 if (tu->queue == NULL)
1718 snd_timer_close(tu->timeri);
1721 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1722 tu->timeri->callback = tu->tread
1723 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1724 tu->timeri->ccallback = snd_timer_user_ccallback;
1725 tu->timeri->callback_data = (void *)tu;
1726 tu->timeri->disconnect = snd_timer_user_disconnect;
1733 static int snd_timer_user_info(struct file *file,
1734 struct snd_timer_info __user *_info)
1736 struct snd_timer_user *tu;
1737 struct snd_timer_info *info;
1738 struct snd_timer *t;
1741 tu = file->private_data;
1744 t = tu->timeri->timer;
1748 info = kzalloc(sizeof(*info), GFP_KERNEL);
1751 info->card = t->card ? t->card->number : -1;
1752 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1753 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1754 strlcpy(info->id, t->id, sizeof(info->id));
1755 strlcpy(info->name, t->name, sizeof(info->name));
1756 info->resolution = t->hw.resolution;
1757 if (copy_to_user(_info, info, sizeof(*_info)))
1763 static int snd_timer_user_params(struct file *file,
1764 struct snd_timer_params __user *_params)
1766 struct snd_timer_user *tu;
1767 struct snd_timer_params params;
1768 struct snd_timer *t;
1769 struct snd_timer_read *tr;
1770 struct snd_timer_tread *ttr;
1773 tu = file->private_data;
1776 t = tu->timeri->timer;
1779 if (copy_from_user(¶ms, _params, sizeof(params)))
1781 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1784 if (params.ticks < 1) {
1789 /* Don't allow resolution less than 1ms */
1790 resolution = snd_timer_resolution(tu->timeri);
1791 resolution *= params.ticks;
1792 if (resolution < 1000000) {
1797 if (params.queue_size > 0 &&
1798 (params.queue_size < 32 || params.queue_size > 1024)) {
1802 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1803 (1<<SNDRV_TIMER_EVENT_TICK)|
1804 (1<<SNDRV_TIMER_EVENT_START)|
1805 (1<<SNDRV_TIMER_EVENT_STOP)|
1806 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1807 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1808 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1809 (1<<SNDRV_TIMER_EVENT_RESUME)|
1810 (1<<SNDRV_TIMER_EVENT_MSTART)|
1811 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1812 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1813 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1814 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1815 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1819 snd_timer_stop(tu->timeri);
1820 spin_lock_irq(&t->lock);
1821 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1822 SNDRV_TIMER_IFLG_EXCLUSIVE|
1823 SNDRV_TIMER_IFLG_EARLY_EVENT);
1824 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1825 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1826 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1827 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1828 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1829 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1830 spin_unlock_irq(&t->lock);
1831 if (params.queue_size > 0 &&
1832 (unsigned int)tu->queue_size != params.queue_size) {
1834 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1838 tu->queue_size = params.queue_size;
1842 tr = kmalloc(params.queue_size * sizeof(*tr),
1846 tu->queue_size = params.queue_size;
1851 tu->qhead = tu->qtail = tu->qused = 0;
1852 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1854 struct snd_timer_tread tread;
1855 memset(&tread, 0, sizeof(tread));
1856 tread.event = SNDRV_TIMER_EVENT_EARLY;
1857 tread.tstamp.tv_sec = 0;
1858 tread.tstamp.tv_nsec = 0;
1860 snd_timer_user_append_to_tqueue(tu, &tread);
1862 struct snd_timer_read *r = &tu->queue[0];
1869 tu->filter = params.filter;
1870 tu->ticks = params.ticks;
1873 if (copy_to_user(_params, ¶ms, sizeof(params)))
1878 static int snd_timer_user_status(struct file *file,
1879 struct snd_timer_status __user *_status)
1881 struct snd_timer_user *tu;
1882 struct snd_timer_status status;
1884 tu = file->private_data;
1887 memset(&status, 0, sizeof(status));
1888 status.tstamp = tu->tstamp;
1889 status.resolution = snd_timer_resolution(tu->timeri);
1890 status.lost = tu->timeri->lost;
1891 status.overrun = tu->overrun;
1892 spin_lock_irq(&tu->qlock);
1893 status.queue = tu->qused;
1894 spin_unlock_irq(&tu->qlock);
1895 if (copy_to_user(_status, &status, sizeof(status)))
1900 static int snd_timer_user_start(struct file *file)
1903 struct snd_timer_user *tu;
1905 tu = file->private_data;
1908 snd_timer_stop(tu->timeri);
1909 tu->timeri->lost = 0;
1910 tu->last_resolution = 0;
1911 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1914 static int snd_timer_user_stop(struct file *file)
1917 struct snd_timer_user *tu;
1919 tu = file->private_data;
1922 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1925 static int snd_timer_user_continue(struct file *file)
1928 struct snd_timer_user *tu;
1930 tu = file->private_data;
1933 /* start timer instead of continue if it's not used before */
1934 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1935 return snd_timer_user_start(file);
1936 tu->timeri->lost = 0;
1937 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1940 static int snd_timer_user_pause(struct file *file)
1943 struct snd_timer_user *tu;
1945 tu = file->private_data;
1948 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1952 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1953 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1954 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1955 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1958 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1961 struct snd_timer_user *tu;
1962 void __user *argp = (void __user *)arg;
1963 int __user *p = argp;
1965 tu = file->private_data;
1967 case SNDRV_TIMER_IOCTL_PVERSION:
1968 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1969 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1970 return snd_timer_user_next_device(argp);
1971 case SNDRV_TIMER_IOCTL_TREAD:
1975 if (tu->timeri) /* too late */
1977 if (get_user(xarg, p))
1979 tu->tread = xarg ? 1 : 0;
1982 case SNDRV_TIMER_IOCTL_GINFO:
1983 return snd_timer_user_ginfo(file, argp);
1984 case SNDRV_TIMER_IOCTL_GPARAMS:
1985 return snd_timer_user_gparams(file, argp);
1986 case SNDRV_TIMER_IOCTL_GSTATUS:
1987 return snd_timer_user_gstatus(file, argp);
1988 case SNDRV_TIMER_IOCTL_SELECT:
1989 return snd_timer_user_tselect(file, argp);
1990 case SNDRV_TIMER_IOCTL_INFO:
1991 return snd_timer_user_info(file, argp);
1992 case SNDRV_TIMER_IOCTL_PARAMS:
1993 return snd_timer_user_params(file, argp);
1994 case SNDRV_TIMER_IOCTL_STATUS:
1995 return snd_timer_user_status(file, argp);
1996 case SNDRV_TIMER_IOCTL_START:
1997 case SNDRV_TIMER_IOCTL_START_OLD:
1998 return snd_timer_user_start(file);
1999 case SNDRV_TIMER_IOCTL_STOP:
2000 case SNDRV_TIMER_IOCTL_STOP_OLD:
2001 return snd_timer_user_stop(file);
2002 case SNDRV_TIMER_IOCTL_CONTINUE:
2003 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
2004 return snd_timer_user_continue(file);
2005 case SNDRV_TIMER_IOCTL_PAUSE:
2006 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
2007 return snd_timer_user_pause(file);
2012 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
2015 struct snd_timer_user *tu = file->private_data;
2018 mutex_lock(&tu->ioctl_lock);
2019 ret = __snd_timer_user_ioctl(file, cmd, arg);
2020 mutex_unlock(&tu->ioctl_lock);
2024 static int snd_timer_user_fasync(int fd, struct file * file, int on)
2026 struct snd_timer_user *tu;
2028 tu = file->private_data;
2029 return fasync_helper(fd, file, on, &tu->fasync);
2032 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2033 size_t count, loff_t *offset)
2035 struct snd_timer_user *tu;
2036 long result = 0, unit;
2040 tu = file->private_data;
2041 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
2042 mutex_lock(&tu->ioctl_lock);
2043 spin_lock_irq(&tu->qlock);
2044 while ((long)count - result >= unit) {
2045 while (!tu->qused) {
2048 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2053 set_current_state(TASK_INTERRUPTIBLE);
2054 init_waitqueue_entry(&wait, current);
2055 add_wait_queue(&tu->qchange_sleep, &wait);
2057 spin_unlock_irq(&tu->qlock);
2058 mutex_unlock(&tu->ioctl_lock);
2060 mutex_lock(&tu->ioctl_lock);
2061 spin_lock_irq(&tu->qlock);
2063 remove_wait_queue(&tu->qchange_sleep, &wait);
2065 if (tu->disconnected) {
2069 if (signal_pending(current)) {
2075 qhead = tu->qhead++;
2076 tu->qhead %= tu->queue_size;
2078 spin_unlock_irq(&tu->qlock);
2081 if (copy_to_user(buffer, &tu->tqueue[qhead],
2082 sizeof(struct snd_timer_tread)))
2085 if (copy_to_user(buffer, &tu->queue[qhead],
2086 sizeof(struct snd_timer_read)))
2090 spin_lock_irq(&tu->qlock);
2097 spin_unlock_irq(&tu->qlock);
2098 mutex_unlock(&tu->ioctl_lock);
2099 return result > 0 ? result : err;
2102 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
2105 struct snd_timer_user *tu;
2107 tu = file->private_data;
2109 poll_wait(file, &tu->qchange_sleep, wait);
2113 mask |= POLLIN | POLLRDNORM;
2114 if (tu->disconnected)
2120 #ifdef CONFIG_COMPAT
2121 #include "timer_compat.c"
2123 #define snd_timer_user_ioctl_compat NULL
2126 static const struct file_operations snd_timer_f_ops =
2128 .owner = THIS_MODULE,
2129 .read = snd_timer_user_read,
2130 .open = snd_timer_user_open,
2131 .release = snd_timer_user_release,
2132 .llseek = no_llseek,
2133 .poll = snd_timer_user_poll,
2134 .unlocked_ioctl = snd_timer_user_ioctl,
2135 .compat_ioctl = snd_timer_user_ioctl_compat,
2136 .fasync = snd_timer_user_fasync,
2139 /* unregister the system timer */
2140 static void snd_timer_free_all(void)
2142 struct snd_timer *timer, *n;
2144 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2145 snd_timer_free(timer);
2148 static struct device timer_dev;
2154 static int __init alsa_timer_init(void)
2158 snd_device_initialize(&timer_dev, NULL);
2159 dev_set_name(&timer_dev, "timer");
2161 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2162 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2166 err = snd_timer_register_system();
2168 pr_err("ALSA: unable to register system timer (%i)\n", err);
2169 put_device(&timer_dev);
2173 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2174 &snd_timer_f_ops, NULL, &timer_dev);
2176 pr_err("ALSA: unable to register timer device (%i)\n", err);
2177 snd_timer_free_all();
2178 put_device(&timer_dev);
2182 snd_timer_proc_init();
2186 static void __exit alsa_timer_exit(void)
2188 snd_unregister_device(&timer_dev);
2189 snd_timer_free_all();
2190 put_device(&timer_dev);
2191 snd_timer_proc_done();
2192 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2193 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2197 module_init(alsa_timer_init)
2198 module_exit(alsa_timer_exit)