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
43 #elif IS_ENABLED(CONFIG_SND_RTCTIMER)
44 #define DEFAULT_TIMER_LIMIT 2
46 #define DEFAULT_TIMER_LIMIT 1
49 static int timer_limit = DEFAULT_TIMER_LIMIT;
50 static int timer_tstamp_monotonic = 1;
51 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
52 MODULE_DESCRIPTION("ALSA timer interface");
53 MODULE_LICENSE("GPL");
54 module_param(timer_limit, int, 0444);
55 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
56 module_param(timer_tstamp_monotonic, int, 0444);
57 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
59 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
60 MODULE_ALIAS("devname:snd/timer");
62 struct snd_timer_user {
63 struct snd_timer_instance *timeri;
64 int tread; /* enhanced read with timestamps and events */
66 unsigned long overrun;
72 struct snd_timer_read *queue;
73 struct snd_timer_tread *tqueue;
75 unsigned long last_resolution;
77 struct timespec tstamp; /* trigger tstamp */
78 wait_queue_head_t qchange_sleep;
79 struct fasync_struct *fasync;
80 struct mutex ioctl_lock;
84 static LIST_HEAD(snd_timer_list);
86 /* list of slave instances */
87 static LIST_HEAD(snd_timer_slave_list);
89 /* lock for slave active lists */
90 static DEFINE_SPINLOCK(slave_active_lock);
92 static DEFINE_MUTEX(register_mutex);
94 static int snd_timer_free(struct snd_timer *timer);
95 static int snd_timer_dev_free(struct snd_device *device);
96 static int snd_timer_dev_register(struct snd_device *device);
97 static int snd_timer_dev_disconnect(struct snd_device *device);
99 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
102 * create a timer instance with the given owner string.
103 * when timer is not NULL, increments the module counter
105 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
106 struct snd_timer *timer)
108 struct snd_timer_instance *timeri;
109 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
112 timeri->owner = kstrdup(owner, GFP_KERNEL);
113 if (! timeri->owner) {
117 INIT_LIST_HEAD(&timeri->open_list);
118 INIT_LIST_HEAD(&timeri->active_list);
119 INIT_LIST_HEAD(&timeri->ack_list);
120 INIT_LIST_HEAD(&timeri->slave_list_head);
121 INIT_LIST_HEAD(&timeri->slave_active_head);
123 timeri->timer = timer;
124 if (timer && !try_module_get(timer->module)) {
125 kfree(timeri->owner);
134 * find a timer instance from the given timer id
136 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
138 struct snd_timer *timer = NULL;
140 list_for_each_entry(timer, &snd_timer_list, device_list) {
141 if (timer->tmr_class != tid->dev_class)
143 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
144 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
145 (timer->card == NULL ||
146 timer->card->number != tid->card))
148 if (timer->tmr_device != tid->device)
150 if (timer->tmr_subdevice != tid->subdevice)
157 #ifdef CONFIG_MODULES
159 static void snd_timer_request(struct snd_timer_id *tid)
161 switch (tid->dev_class) {
162 case SNDRV_TIMER_CLASS_GLOBAL:
163 if (tid->device < timer_limit)
164 request_module("snd-timer-%i", tid->device);
166 case SNDRV_TIMER_CLASS_CARD:
167 case SNDRV_TIMER_CLASS_PCM:
168 if (tid->card < snd_ecards_limit)
169 request_module("snd-card-%i", tid->card);
179 * look for a master instance matching with the slave id of the given slave.
180 * when found, relink the open_link of the slave.
182 * call this with register_mutex down.
184 static void snd_timer_check_slave(struct snd_timer_instance *slave)
186 struct snd_timer *timer;
187 struct snd_timer_instance *master;
189 /* FIXME: it's really dumb to look up all entries.. */
190 list_for_each_entry(timer, &snd_timer_list, device_list) {
191 list_for_each_entry(master, &timer->open_list_head, open_list) {
192 if (slave->slave_class == master->slave_class &&
193 slave->slave_id == master->slave_id) {
194 list_move_tail(&slave->open_list,
195 &master->slave_list_head);
196 spin_lock_irq(&slave_active_lock);
197 slave->master = master;
198 slave->timer = master->timer;
199 spin_unlock_irq(&slave_active_lock);
207 * look for slave instances matching with the slave id of the given master.
208 * when found, relink the open_link of slaves.
210 * call this with register_mutex down.
212 static void snd_timer_check_master(struct snd_timer_instance *master)
214 struct snd_timer_instance *slave, *tmp;
216 /* check all pending slaves */
217 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
218 if (slave->slave_class == master->slave_class &&
219 slave->slave_id == master->slave_id) {
220 list_move_tail(&slave->open_list, &master->slave_list_head);
221 spin_lock_irq(&slave_active_lock);
222 spin_lock(&master->timer->lock);
223 slave->master = master;
224 slave->timer = master->timer;
225 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
226 list_add_tail(&slave->active_list,
227 &master->slave_active_head);
228 spin_unlock(&master->timer->lock);
229 spin_unlock_irq(&slave_active_lock);
235 * open a timer instance
236 * when opening a master, the slave id must be here given.
238 int snd_timer_open(struct snd_timer_instance **ti,
239 char *owner, struct snd_timer_id *tid,
240 unsigned int slave_id)
242 struct snd_timer *timer;
243 struct snd_timer_instance *timeri = NULL;
245 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
246 /* open a slave instance */
247 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
248 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
249 pr_debug("ALSA: timer: invalid slave class %i\n",
253 mutex_lock(®ister_mutex);
254 timeri = snd_timer_instance_new(owner, NULL);
256 mutex_unlock(®ister_mutex);
259 timeri->slave_class = tid->dev_sclass;
260 timeri->slave_id = tid->device;
261 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
262 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
263 snd_timer_check_slave(timeri);
264 mutex_unlock(®ister_mutex);
269 /* open a master instance */
270 mutex_lock(®ister_mutex);
271 timer = snd_timer_find(tid);
272 #ifdef CONFIG_MODULES
274 mutex_unlock(®ister_mutex);
275 snd_timer_request(tid);
276 mutex_lock(®ister_mutex);
277 timer = snd_timer_find(tid);
281 mutex_unlock(®ister_mutex);
284 if (!list_empty(&timer->open_list_head)) {
285 timeri = list_entry(timer->open_list_head.next,
286 struct snd_timer_instance, open_list);
287 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
288 mutex_unlock(®ister_mutex);
292 timeri = snd_timer_instance_new(owner, timer);
294 mutex_unlock(®ister_mutex);
297 /* take a card refcount for safe disconnection */
299 get_device(&timer->card->card_dev);
300 timeri->slave_class = tid->dev_sclass;
301 timeri->slave_id = slave_id;
303 if (list_empty(&timer->open_list_head) && timer->hw.open) {
304 int err = timer->hw.open(timer);
306 kfree(timeri->owner);
310 put_device(&timer->card->card_dev);
311 module_put(timer->module);
312 mutex_unlock(®ister_mutex);
317 list_add_tail(&timeri->open_list, &timer->open_list_head);
318 snd_timer_check_master(timeri);
319 mutex_unlock(®ister_mutex);
325 * close a timer instance
327 int snd_timer_close(struct snd_timer_instance *timeri)
329 struct snd_timer *timer = NULL;
330 struct snd_timer_instance *slave, *tmp;
332 if (snd_BUG_ON(!timeri))
335 /* force to stop the timer */
336 snd_timer_stop(timeri);
338 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
339 /* wait, until the active callback is finished */
340 spin_lock_irq(&slave_active_lock);
341 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
342 spin_unlock_irq(&slave_active_lock);
344 spin_lock_irq(&slave_active_lock);
346 spin_unlock_irq(&slave_active_lock);
347 mutex_lock(®ister_mutex);
348 list_del(&timeri->open_list);
349 mutex_unlock(®ister_mutex);
351 timer = timeri->timer;
352 if (snd_BUG_ON(!timer))
354 /* wait, until the active callback is finished */
355 spin_lock_irq(&timer->lock);
356 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
357 spin_unlock_irq(&timer->lock);
359 spin_lock_irq(&timer->lock);
361 spin_unlock_irq(&timer->lock);
362 mutex_lock(®ister_mutex);
363 list_del(&timeri->open_list);
364 if (list_empty(&timer->open_list_head) &&
366 timer->hw.close(timer);
367 /* remove slave links */
368 spin_lock_irq(&slave_active_lock);
369 spin_lock(&timer->lock);
370 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
372 list_move_tail(&slave->open_list, &snd_timer_slave_list);
373 slave->master = NULL;
375 list_del_init(&slave->ack_list);
376 list_del_init(&slave->active_list);
378 spin_unlock(&timer->lock);
379 spin_unlock_irq(&slave_active_lock);
380 /* release a card refcount for safe disconnection */
382 put_device(&timer->card->card_dev);
383 mutex_unlock(®ister_mutex);
386 if (timeri->private_free)
387 timeri->private_free(timeri);
388 kfree(timeri->owner);
391 module_put(timer->module);
395 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
397 struct snd_timer * timer;
401 if ((timer = timeri->timer) != NULL) {
402 if (timer->hw.c_resolution)
403 return timer->hw.c_resolution(timer);
404 return timer->hw.resolution;
409 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
411 struct snd_timer *timer;
412 unsigned long resolution = 0;
413 struct snd_timer_instance *ts;
414 struct timespec tstamp;
416 if (timer_tstamp_monotonic)
417 ktime_get_ts(&tstamp);
419 getnstimeofday(&tstamp);
420 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
421 event > SNDRV_TIMER_EVENT_PAUSE))
423 if (event == SNDRV_TIMER_EVENT_START ||
424 event == SNDRV_TIMER_EVENT_CONTINUE)
425 resolution = snd_timer_resolution(ti);
427 ti->ccallback(ti, event, &tstamp, resolution);
428 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
433 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
435 event += 10; /* convert to SNDRV_TIMER_EVENT_MXXX */
436 list_for_each_entry(ts, &ti->slave_active_head, active_list)
438 ts->ccallback(ts, event, &tstamp, resolution);
441 /* start/continue a master timer */
442 static int snd_timer_start1(struct snd_timer_instance *timeri,
443 bool start, unsigned long ticks)
445 struct snd_timer *timer;
449 timer = timeri->timer;
453 spin_lock_irqsave(&timer->lock, flags);
454 if (timer->card && timer->card->shutdown) {
458 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
459 SNDRV_TIMER_IFLG_START)) {
465 timeri->ticks = timeri->cticks = ticks;
466 else if (!timeri->cticks)
470 list_move_tail(&timeri->active_list, &timer->active_list_head);
471 if (timer->running) {
472 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
474 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
475 timeri->flags |= SNDRV_TIMER_IFLG_START;
476 result = 1; /* delayed start */
479 timer->sticks = ticks;
480 timer->hw.start(timer);
483 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
486 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
487 SNDRV_TIMER_EVENT_CONTINUE);
489 spin_unlock_irqrestore(&timer->lock, flags);
493 /* start/continue a slave timer */
494 static int snd_timer_start_slave(struct snd_timer_instance *timeri,
499 spin_lock_irqsave(&slave_active_lock, flags);
500 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
501 spin_unlock_irqrestore(&slave_active_lock, flags);
504 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
505 if (timeri->master && timeri->timer) {
506 spin_lock(&timeri->timer->lock);
507 list_add_tail(&timeri->active_list,
508 &timeri->master->slave_active_head);
509 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
510 SNDRV_TIMER_EVENT_CONTINUE);
511 spin_unlock(&timeri->timer->lock);
513 spin_unlock_irqrestore(&slave_active_lock, flags);
514 return 1; /* delayed start */
517 /* stop/pause a master timer */
518 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
520 struct snd_timer *timer;
524 timer = timeri->timer;
527 spin_lock_irqsave(&timer->lock, flags);
528 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
529 SNDRV_TIMER_IFLG_START))) {
533 list_del_init(&timeri->ack_list);
534 list_del_init(&timeri->active_list);
535 if (timer->card && timer->card->shutdown)
538 timeri->cticks = timeri->ticks;
541 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
542 !(--timer->running)) {
543 timer->hw.stop(timer);
544 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
545 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
546 snd_timer_reschedule(timer, 0);
547 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
548 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
549 timer->hw.start(timer);
553 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
555 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
557 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
558 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
559 SNDRV_TIMER_EVENT_PAUSE);
561 spin_unlock_irqrestore(&timer->lock, flags);
565 /* stop/pause a slave timer */
566 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
570 spin_lock_irqsave(&slave_active_lock, flags);
571 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
572 spin_unlock_irqrestore(&slave_active_lock, flags);
575 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
577 spin_lock(&timeri->timer->lock);
578 list_del_init(&timeri->ack_list);
579 list_del_init(&timeri->active_list);
580 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
581 SNDRV_TIMER_EVENT_PAUSE);
582 spin_unlock(&timeri->timer->lock);
584 spin_unlock_irqrestore(&slave_active_lock, flags);
589 * start the timer instance
591 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
593 if (timeri == NULL || ticks < 1)
595 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
596 return snd_timer_start_slave(timeri, true);
598 return snd_timer_start1(timeri, true, ticks);
602 * stop the timer instance.
604 * do not call this from the timer callback!
606 int snd_timer_stop(struct snd_timer_instance *timeri)
608 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
609 return snd_timer_stop_slave(timeri, true);
611 return snd_timer_stop1(timeri, true);
615 * start again.. the tick is kept.
617 int snd_timer_continue(struct snd_timer_instance *timeri)
619 /* timer can continue only after pause */
620 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
623 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
624 return snd_timer_start_slave(timeri, false);
626 return snd_timer_start1(timeri, false, 0);
630 * pause.. remember the ticks left
632 int snd_timer_pause(struct snd_timer_instance * timeri)
634 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
635 return snd_timer_stop_slave(timeri, false);
637 return snd_timer_stop1(timeri, false);
641 * reschedule the timer
643 * start pending instances and check the scheduling ticks.
644 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
646 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
648 struct snd_timer_instance *ti;
649 unsigned long ticks = ~0UL;
651 list_for_each_entry(ti, &timer->active_list_head, active_list) {
652 if (ti->flags & SNDRV_TIMER_IFLG_START) {
653 ti->flags &= ~SNDRV_TIMER_IFLG_START;
654 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
657 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
658 if (ticks > ti->cticks)
663 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
666 if (ticks > timer->hw.ticks)
667 ticks = timer->hw.ticks;
668 if (ticks_left != ticks)
669 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
670 timer->sticks = ticks;
677 static void snd_timer_tasklet(unsigned long arg)
679 struct snd_timer *timer = (struct snd_timer *) arg;
680 struct snd_timer_instance *ti;
682 unsigned long resolution, ticks;
685 if (timer->card && timer->card->shutdown)
688 spin_lock_irqsave(&timer->lock, flags);
689 /* now process all callbacks */
690 while (!list_empty(&timer->sack_list_head)) {
691 p = timer->sack_list_head.next; /* get first item */
692 ti = list_entry(p, struct snd_timer_instance, ack_list);
694 /* remove from ack_list and make empty */
699 resolution = ti->resolution;
701 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
702 spin_unlock(&timer->lock);
704 ti->callback(ti, resolution, ticks);
705 spin_lock(&timer->lock);
706 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
708 spin_unlock_irqrestore(&timer->lock, flags);
714 * ticks_left is usually equal to timer->sticks.
717 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
719 struct snd_timer_instance *ti, *ts, *tmp;
720 unsigned long resolution, ticks;
721 struct list_head *p, *ack_list_head;
728 if (timer->card && timer->card->shutdown)
731 spin_lock_irqsave(&timer->lock, flags);
733 /* remember the current resolution */
734 if (timer->hw.c_resolution)
735 resolution = timer->hw.c_resolution(timer);
737 resolution = timer->hw.resolution;
739 /* loop for all active instances
740 * Here we cannot use list_for_each_entry because the active_list of a
741 * processed instance is relinked to done_list_head before the callback
744 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
746 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
748 ti->pticks += ticks_left;
749 ti->resolution = resolution;
750 if (ti->cticks < ticks_left)
753 ti->cticks -= ticks_left;
754 if (ti->cticks) /* not expired */
756 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
757 ti->cticks = ti->ticks;
759 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
761 list_del_init(&ti->active_list);
763 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
764 (ti->flags & SNDRV_TIMER_IFLG_FAST))
765 ack_list_head = &timer->ack_list_head;
767 ack_list_head = &timer->sack_list_head;
768 if (list_empty(&ti->ack_list))
769 list_add_tail(&ti->ack_list, ack_list_head);
770 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
771 ts->pticks = ti->pticks;
772 ts->resolution = resolution;
773 if (list_empty(&ts->ack_list))
774 list_add_tail(&ts->ack_list, ack_list_head);
777 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
778 snd_timer_reschedule(timer, timer->sticks);
779 if (timer->running) {
780 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
781 timer->hw.stop(timer);
782 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
784 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
785 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
787 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
788 timer->hw.start(timer);
791 timer->hw.stop(timer);
794 /* now process all fast callbacks */
795 while (!list_empty(&timer->ack_list_head)) {
796 p = timer->ack_list_head.next; /* get first item */
797 ti = list_entry(p, struct snd_timer_instance, ack_list);
799 /* remove from ack_list and make empty */
805 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
806 spin_unlock(&timer->lock);
808 ti->callback(ti, resolution, ticks);
809 spin_lock(&timer->lock);
810 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
813 /* do we have any slow callbacks? */
814 use_tasklet = !list_empty(&timer->sack_list_head);
815 spin_unlock_irqrestore(&timer->lock, flags);
818 tasklet_schedule(&timer->task_queue);
825 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
826 struct snd_timer **rtimer)
828 struct snd_timer *timer;
830 static struct snd_device_ops ops = {
831 .dev_free = snd_timer_dev_free,
832 .dev_register = snd_timer_dev_register,
833 .dev_disconnect = snd_timer_dev_disconnect,
836 if (snd_BUG_ON(!tid))
840 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
843 timer->tmr_class = tid->dev_class;
845 timer->tmr_device = tid->device;
846 timer->tmr_subdevice = tid->subdevice;
848 strlcpy(timer->id, id, sizeof(timer->id));
850 INIT_LIST_HEAD(&timer->device_list);
851 INIT_LIST_HEAD(&timer->open_list_head);
852 INIT_LIST_HEAD(&timer->active_list_head);
853 INIT_LIST_HEAD(&timer->ack_list_head);
854 INIT_LIST_HEAD(&timer->sack_list_head);
855 spin_lock_init(&timer->lock);
856 tasklet_init(&timer->task_queue, snd_timer_tasklet,
857 (unsigned long)timer);
859 timer->module = card->module;
860 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
862 snd_timer_free(timer);
871 static int snd_timer_free(struct snd_timer *timer)
876 mutex_lock(®ister_mutex);
877 if (! list_empty(&timer->open_list_head)) {
878 struct list_head *p, *n;
879 struct snd_timer_instance *ti;
880 pr_warn("ALSA: timer %p is busy?\n", timer);
881 list_for_each_safe(p, n, &timer->open_list_head) {
883 ti = list_entry(p, struct snd_timer_instance, open_list);
887 list_del(&timer->device_list);
888 mutex_unlock(®ister_mutex);
890 if (timer->private_free)
891 timer->private_free(timer);
896 static int snd_timer_dev_free(struct snd_device *device)
898 struct snd_timer *timer = device->device_data;
899 return snd_timer_free(timer);
902 static int snd_timer_dev_register(struct snd_device *dev)
904 struct snd_timer *timer = dev->device_data;
905 struct snd_timer *timer1;
907 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
909 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
910 !timer->hw.resolution && timer->hw.c_resolution == NULL)
913 mutex_lock(®ister_mutex);
914 list_for_each_entry(timer1, &snd_timer_list, device_list) {
915 if (timer1->tmr_class > timer->tmr_class)
917 if (timer1->tmr_class < timer->tmr_class)
919 if (timer1->card && timer->card) {
920 if (timer1->card->number > timer->card->number)
922 if (timer1->card->number < timer->card->number)
925 if (timer1->tmr_device > timer->tmr_device)
927 if (timer1->tmr_device < timer->tmr_device)
929 if (timer1->tmr_subdevice > timer->tmr_subdevice)
931 if (timer1->tmr_subdevice < timer->tmr_subdevice)
934 mutex_unlock(®ister_mutex);
937 list_add_tail(&timer->device_list, &timer1->device_list);
938 mutex_unlock(®ister_mutex);
942 /* just for reference in snd_timer_dev_disconnect() below */
943 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
944 int event, struct timespec *tstamp,
945 unsigned long resolution);
947 static int snd_timer_dev_disconnect(struct snd_device *device)
949 struct snd_timer *timer = device->device_data;
950 struct snd_timer_instance *ti;
952 mutex_lock(®ister_mutex);
953 list_del_init(&timer->device_list);
954 /* wake up pending sleepers */
955 list_for_each_entry(ti, &timer->open_list_head, open_list) {
956 /* FIXME: better to have a ti.disconnect() op */
957 if (ti->ccallback == snd_timer_user_ccallback) {
958 struct snd_timer_user *tu = ti->callback_data;
960 tu->disconnected = true;
961 wake_up(&tu->qchange_sleep);
964 mutex_unlock(®ister_mutex);
968 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
971 unsigned long resolution = 0;
972 struct snd_timer_instance *ti, *ts;
974 if (timer->card && timer->card->shutdown)
976 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
978 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
979 event > SNDRV_TIMER_EVENT_MRESUME))
981 spin_lock_irqsave(&timer->lock, flags);
982 if (event == SNDRV_TIMER_EVENT_MSTART ||
983 event == SNDRV_TIMER_EVENT_MCONTINUE ||
984 event == SNDRV_TIMER_EVENT_MRESUME) {
985 if (timer->hw.c_resolution)
986 resolution = timer->hw.c_resolution(timer);
988 resolution = timer->hw.resolution;
990 list_for_each_entry(ti, &timer->active_list_head, active_list) {
992 ti->ccallback(ti, event, tstamp, resolution);
993 list_for_each_entry(ts, &ti->slave_active_head, active_list)
995 ts->ccallback(ts, event, tstamp, resolution);
997 spin_unlock_irqrestore(&timer->lock, flags);
1001 * exported functions for global timers
1003 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1005 struct snd_timer_id tid;
1007 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1008 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1010 tid.device = device;
1012 return snd_timer_new(NULL, id, &tid, rtimer);
1015 int snd_timer_global_free(struct snd_timer *timer)
1017 return snd_timer_free(timer);
1020 int snd_timer_global_register(struct snd_timer *timer)
1022 struct snd_device dev;
1024 memset(&dev, 0, sizeof(dev));
1025 dev.device_data = timer;
1026 return snd_timer_dev_register(&dev);
1033 struct snd_timer_system_private {
1034 struct timer_list tlist;
1035 unsigned long last_expires;
1036 unsigned long last_jiffies;
1037 unsigned long correction;
1040 static void snd_timer_s_function(unsigned long data)
1042 struct snd_timer *timer = (struct snd_timer *)data;
1043 struct snd_timer_system_private *priv = timer->private_data;
1044 unsigned long jiff = jiffies;
1045 if (time_after(jiff, priv->last_expires))
1046 priv->correction += (long)jiff - (long)priv->last_expires;
1047 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1050 static int snd_timer_s_start(struct snd_timer * timer)
1052 struct snd_timer_system_private *priv;
1053 unsigned long njiff;
1055 priv = (struct snd_timer_system_private *) timer->private_data;
1056 njiff = (priv->last_jiffies = jiffies);
1057 if (priv->correction > timer->sticks - 1) {
1058 priv->correction -= timer->sticks - 1;
1061 njiff += timer->sticks - priv->correction;
1062 priv->correction = 0;
1064 priv->last_expires = njiff;
1065 mod_timer(&priv->tlist, njiff);
1069 static int snd_timer_s_stop(struct snd_timer * timer)
1071 struct snd_timer_system_private *priv;
1074 priv = (struct snd_timer_system_private *) timer->private_data;
1075 del_timer(&priv->tlist);
1077 if (time_before(jiff, priv->last_expires))
1078 timer->sticks = priv->last_expires - jiff;
1081 priv->correction = 0;
1085 static struct snd_timer_hardware snd_timer_system =
1087 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1088 .resolution = 1000000000L / HZ,
1090 .start = snd_timer_s_start,
1091 .stop = snd_timer_s_stop
1094 static void snd_timer_free_system(struct snd_timer *timer)
1096 kfree(timer->private_data);
1099 static int snd_timer_register_system(void)
1101 struct snd_timer *timer;
1102 struct snd_timer_system_private *priv;
1105 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1108 strcpy(timer->name, "system timer");
1109 timer->hw = snd_timer_system;
1110 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1112 snd_timer_free(timer);
1115 setup_timer(&priv->tlist, snd_timer_s_function, (unsigned long) timer);
1116 timer->private_data = priv;
1117 timer->private_free = snd_timer_free_system;
1118 return snd_timer_global_register(timer);
1121 #ifdef CONFIG_SND_PROC_FS
1126 static void snd_timer_proc_read(struct snd_info_entry *entry,
1127 struct snd_info_buffer *buffer)
1129 struct snd_timer *timer;
1130 struct snd_timer_instance *ti;
1132 mutex_lock(®ister_mutex);
1133 list_for_each_entry(timer, &snd_timer_list, device_list) {
1134 if (timer->card && timer->card->shutdown)
1136 switch (timer->tmr_class) {
1137 case SNDRV_TIMER_CLASS_GLOBAL:
1138 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1140 case SNDRV_TIMER_CLASS_CARD:
1141 snd_iprintf(buffer, "C%i-%i: ",
1142 timer->card->number, timer->tmr_device);
1144 case SNDRV_TIMER_CLASS_PCM:
1145 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1146 timer->tmr_device, timer->tmr_subdevice);
1149 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1150 timer->card ? timer->card->number : -1,
1151 timer->tmr_device, timer->tmr_subdevice);
1153 snd_iprintf(buffer, "%s :", timer->name);
1154 if (timer->hw.resolution)
1155 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1156 timer->hw.resolution / 1000,
1157 timer->hw.resolution % 1000,
1159 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1160 snd_iprintf(buffer, " SLAVE");
1161 snd_iprintf(buffer, "\n");
1162 list_for_each_entry(ti, &timer->open_list_head, open_list)
1163 snd_iprintf(buffer, " Client %s : %s\n",
1164 ti->owner ? ti->owner : "unknown",
1165 ti->flags & (SNDRV_TIMER_IFLG_START |
1166 SNDRV_TIMER_IFLG_RUNNING)
1167 ? "running" : "stopped");
1169 mutex_unlock(®ister_mutex);
1172 static struct snd_info_entry *snd_timer_proc_entry;
1174 static void __init snd_timer_proc_init(void)
1176 struct snd_info_entry *entry;
1178 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1179 if (entry != NULL) {
1180 entry->c.text.read = snd_timer_proc_read;
1181 if (snd_info_register(entry) < 0) {
1182 snd_info_free_entry(entry);
1186 snd_timer_proc_entry = entry;
1189 static void __exit snd_timer_proc_done(void)
1191 snd_info_free_entry(snd_timer_proc_entry);
1193 #else /* !CONFIG_SND_PROC_FS */
1194 #define snd_timer_proc_init()
1195 #define snd_timer_proc_done()
1199 * USER SPACE interface
1202 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1203 unsigned long resolution,
1204 unsigned long ticks)
1206 struct snd_timer_user *tu = timeri->callback_data;
1207 struct snd_timer_read *r;
1210 spin_lock(&tu->qlock);
1211 if (tu->qused > 0) {
1212 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1213 r = &tu->queue[prev];
1214 if (r->resolution == resolution) {
1219 if (tu->qused >= tu->queue_size) {
1222 r = &tu->queue[tu->qtail++];
1223 tu->qtail %= tu->queue_size;
1224 r->resolution = resolution;
1229 spin_unlock(&tu->qlock);
1230 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1231 wake_up(&tu->qchange_sleep);
1234 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1235 struct snd_timer_tread *tread)
1237 if (tu->qused >= tu->queue_size) {
1240 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1241 tu->qtail %= tu->queue_size;
1246 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1248 struct timespec *tstamp,
1249 unsigned long resolution)
1251 struct snd_timer_user *tu = timeri->callback_data;
1252 struct snd_timer_tread r1;
1253 unsigned long flags;
1255 if (event >= SNDRV_TIMER_EVENT_START &&
1256 event <= SNDRV_TIMER_EVENT_PAUSE)
1257 tu->tstamp = *tstamp;
1258 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1260 memset(&r1, 0, sizeof(r1));
1262 r1.tstamp = *tstamp;
1263 r1.val = resolution;
1264 spin_lock_irqsave(&tu->qlock, flags);
1265 snd_timer_user_append_to_tqueue(tu, &r1);
1266 spin_unlock_irqrestore(&tu->qlock, flags);
1267 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1268 wake_up(&tu->qchange_sleep);
1271 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1272 unsigned long resolution,
1273 unsigned long ticks)
1275 struct snd_timer_user *tu = timeri->callback_data;
1276 struct snd_timer_tread *r, r1;
1277 struct timespec tstamp;
1278 int prev, append = 0;
1280 memset(&tstamp, 0, sizeof(tstamp));
1281 spin_lock(&tu->qlock);
1282 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1283 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1284 spin_unlock(&tu->qlock);
1287 if (tu->last_resolution != resolution || ticks > 0) {
1288 if (timer_tstamp_monotonic)
1289 ktime_get_ts(&tstamp);
1291 getnstimeofday(&tstamp);
1293 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1294 tu->last_resolution != resolution) {
1295 memset(&r1, 0, sizeof(r1));
1296 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1298 r1.val = resolution;
1299 snd_timer_user_append_to_tqueue(tu, &r1);
1300 tu->last_resolution = resolution;
1303 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1307 if (tu->qused > 0) {
1308 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1309 r = &tu->tqueue[prev];
1310 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1317 r1.event = SNDRV_TIMER_EVENT_TICK;
1320 snd_timer_user_append_to_tqueue(tu, &r1);
1323 spin_unlock(&tu->qlock);
1326 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1327 wake_up(&tu->qchange_sleep);
1330 static int snd_timer_user_open(struct inode *inode, struct file *file)
1332 struct snd_timer_user *tu;
1335 err = nonseekable_open(inode, file);
1339 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1342 spin_lock_init(&tu->qlock);
1343 init_waitqueue_head(&tu->qchange_sleep);
1344 mutex_init(&tu->ioctl_lock);
1346 tu->queue_size = 128;
1347 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1349 if (tu->queue == NULL) {
1353 file->private_data = tu;
1357 static int snd_timer_user_release(struct inode *inode, struct file *file)
1359 struct snd_timer_user *tu;
1361 if (file->private_data) {
1362 tu = file->private_data;
1363 file->private_data = NULL;
1364 mutex_lock(&tu->ioctl_lock);
1366 snd_timer_close(tu->timeri);
1367 mutex_unlock(&tu->ioctl_lock);
1375 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1377 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1378 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1384 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1386 id->dev_class = timer->tmr_class;
1387 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1388 id->card = timer->card ? timer->card->number : -1;
1389 id->device = timer->tmr_device;
1390 id->subdevice = timer->tmr_subdevice;
1393 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1395 struct snd_timer_id id;
1396 struct snd_timer *timer;
1397 struct list_head *p;
1399 if (copy_from_user(&id, _tid, sizeof(id)))
1401 mutex_lock(®ister_mutex);
1402 if (id.dev_class < 0) { /* first item */
1403 if (list_empty(&snd_timer_list))
1404 snd_timer_user_zero_id(&id);
1406 timer = list_entry(snd_timer_list.next,
1407 struct snd_timer, device_list);
1408 snd_timer_user_copy_id(&id, timer);
1411 switch (id.dev_class) {
1412 case SNDRV_TIMER_CLASS_GLOBAL:
1413 id.device = id.device < 0 ? 0 : id.device + 1;
1414 list_for_each(p, &snd_timer_list) {
1415 timer = list_entry(p, struct snd_timer, device_list);
1416 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1417 snd_timer_user_copy_id(&id, timer);
1420 if (timer->tmr_device >= id.device) {
1421 snd_timer_user_copy_id(&id, timer);
1425 if (p == &snd_timer_list)
1426 snd_timer_user_zero_id(&id);
1428 case SNDRV_TIMER_CLASS_CARD:
1429 case SNDRV_TIMER_CLASS_PCM:
1436 if (id.device < 0) {
1439 if (id.subdevice < 0) {
1447 list_for_each(p, &snd_timer_list) {
1448 timer = list_entry(p, struct snd_timer, device_list);
1449 if (timer->tmr_class > id.dev_class) {
1450 snd_timer_user_copy_id(&id, timer);
1453 if (timer->tmr_class < id.dev_class)
1455 if (timer->card->number > id.card) {
1456 snd_timer_user_copy_id(&id, timer);
1459 if (timer->card->number < id.card)
1461 if (timer->tmr_device > id.device) {
1462 snd_timer_user_copy_id(&id, timer);
1465 if (timer->tmr_device < id.device)
1467 if (timer->tmr_subdevice > id.subdevice) {
1468 snd_timer_user_copy_id(&id, timer);
1471 if (timer->tmr_subdevice < id.subdevice)
1473 snd_timer_user_copy_id(&id, timer);
1476 if (p == &snd_timer_list)
1477 snd_timer_user_zero_id(&id);
1480 snd_timer_user_zero_id(&id);
1483 mutex_unlock(®ister_mutex);
1484 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1489 static int snd_timer_user_ginfo(struct file *file,
1490 struct snd_timer_ginfo __user *_ginfo)
1492 struct snd_timer_ginfo *ginfo;
1493 struct snd_timer_id tid;
1494 struct snd_timer *t;
1495 struct list_head *p;
1498 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1500 return PTR_ERR(ginfo);
1503 memset(ginfo, 0, sizeof(*ginfo));
1505 mutex_lock(®ister_mutex);
1506 t = snd_timer_find(&tid);
1508 ginfo->card = t->card ? t->card->number : -1;
1509 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1510 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1511 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1512 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1513 ginfo->resolution = t->hw.resolution;
1514 if (t->hw.resolution_min > 0) {
1515 ginfo->resolution_min = t->hw.resolution_min;
1516 ginfo->resolution_max = t->hw.resolution_max;
1518 list_for_each(p, &t->open_list_head) {
1524 mutex_unlock(®ister_mutex);
1525 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1531 static int snd_timer_user_gparams(struct file *file,
1532 struct snd_timer_gparams __user *_gparams)
1534 struct snd_timer_gparams gparams;
1535 struct snd_timer *t;
1538 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1540 mutex_lock(®ister_mutex);
1541 t = snd_timer_find(&gparams.tid);
1546 if (!list_empty(&t->open_list_head)) {
1550 if (!t->hw.set_period) {
1554 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1556 mutex_unlock(®ister_mutex);
1560 static int snd_timer_user_gstatus(struct file *file,
1561 struct snd_timer_gstatus __user *_gstatus)
1563 struct snd_timer_gstatus gstatus;
1564 struct snd_timer_id tid;
1565 struct snd_timer *t;
1568 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1571 memset(&gstatus, 0, sizeof(gstatus));
1573 mutex_lock(®ister_mutex);
1574 t = snd_timer_find(&tid);
1576 if (t->hw.c_resolution)
1577 gstatus.resolution = t->hw.c_resolution(t);
1579 gstatus.resolution = t->hw.resolution;
1580 if (t->hw.precise_resolution) {
1581 t->hw.precise_resolution(t, &gstatus.resolution_num,
1582 &gstatus.resolution_den);
1584 gstatus.resolution_num = gstatus.resolution;
1585 gstatus.resolution_den = 1000000000uL;
1590 mutex_unlock(®ister_mutex);
1591 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1596 static int snd_timer_user_tselect(struct file *file,
1597 struct snd_timer_select __user *_tselect)
1599 struct snd_timer_user *tu;
1600 struct snd_timer_select tselect;
1604 tu = file->private_data;
1606 snd_timer_close(tu->timeri);
1609 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1613 sprintf(str, "application %i", current->pid);
1614 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1615 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1616 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1620 tu->qhead = tu->qtail = tu->qused = 0;
1626 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1628 if (tu->tqueue == NULL)
1631 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1633 if (tu->queue == NULL)
1638 snd_timer_close(tu->timeri);
1641 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1642 tu->timeri->callback = tu->tread
1643 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1644 tu->timeri->ccallback = snd_timer_user_ccallback;
1645 tu->timeri->callback_data = (void *)tu;
1652 static int snd_timer_user_info(struct file *file,
1653 struct snd_timer_info __user *_info)
1655 struct snd_timer_user *tu;
1656 struct snd_timer_info *info;
1657 struct snd_timer *t;
1660 tu = file->private_data;
1663 t = tu->timeri->timer;
1667 info = kzalloc(sizeof(*info), GFP_KERNEL);
1670 info->card = t->card ? t->card->number : -1;
1671 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1672 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1673 strlcpy(info->id, t->id, sizeof(info->id));
1674 strlcpy(info->name, t->name, sizeof(info->name));
1675 info->resolution = t->hw.resolution;
1676 if (copy_to_user(_info, info, sizeof(*_info)))
1682 static int snd_timer_user_params(struct file *file,
1683 struct snd_timer_params __user *_params)
1685 struct snd_timer_user *tu;
1686 struct snd_timer_params params;
1687 struct snd_timer *t;
1688 struct snd_timer_read *tr;
1689 struct snd_timer_tread *ttr;
1692 tu = file->private_data;
1695 t = tu->timeri->timer;
1698 if (copy_from_user(¶ms, _params, sizeof(params)))
1700 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1703 if (params.ticks < 1) {
1708 /* Don't allow resolution less than 1ms */
1709 resolution = snd_timer_resolution(tu->timeri);
1710 resolution *= params.ticks;
1711 if (resolution < 1000000) {
1716 if (params.queue_size > 0 &&
1717 (params.queue_size < 32 || params.queue_size > 1024)) {
1721 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1722 (1<<SNDRV_TIMER_EVENT_TICK)|
1723 (1<<SNDRV_TIMER_EVENT_START)|
1724 (1<<SNDRV_TIMER_EVENT_STOP)|
1725 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1726 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1727 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1728 (1<<SNDRV_TIMER_EVENT_RESUME)|
1729 (1<<SNDRV_TIMER_EVENT_MSTART)|
1730 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1731 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1732 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1733 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1734 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1738 snd_timer_stop(tu->timeri);
1739 spin_lock_irq(&t->lock);
1740 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1741 SNDRV_TIMER_IFLG_EXCLUSIVE|
1742 SNDRV_TIMER_IFLG_EARLY_EVENT);
1743 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1744 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1745 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1746 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1747 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1748 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1749 spin_unlock_irq(&t->lock);
1750 if (params.queue_size > 0 &&
1751 (unsigned int)tu->queue_size != params.queue_size) {
1753 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1757 tu->queue_size = params.queue_size;
1761 tr = kmalloc(params.queue_size * sizeof(*tr),
1765 tu->queue_size = params.queue_size;
1770 tu->qhead = tu->qtail = tu->qused = 0;
1771 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1773 struct snd_timer_tread tread;
1774 memset(&tread, 0, sizeof(tread));
1775 tread.event = SNDRV_TIMER_EVENT_EARLY;
1776 tread.tstamp.tv_sec = 0;
1777 tread.tstamp.tv_nsec = 0;
1779 snd_timer_user_append_to_tqueue(tu, &tread);
1781 struct snd_timer_read *r = &tu->queue[0];
1788 tu->filter = params.filter;
1789 tu->ticks = params.ticks;
1792 if (copy_to_user(_params, ¶ms, sizeof(params)))
1797 static int snd_timer_user_status(struct file *file,
1798 struct snd_timer_status __user *_status)
1800 struct snd_timer_user *tu;
1801 struct snd_timer_status status;
1803 tu = file->private_data;
1806 memset(&status, 0, sizeof(status));
1807 status.tstamp = tu->tstamp;
1808 status.resolution = snd_timer_resolution(tu->timeri);
1809 status.lost = tu->timeri->lost;
1810 status.overrun = tu->overrun;
1811 spin_lock_irq(&tu->qlock);
1812 status.queue = tu->qused;
1813 spin_unlock_irq(&tu->qlock);
1814 if (copy_to_user(_status, &status, sizeof(status)))
1819 static int snd_timer_user_start(struct file *file)
1822 struct snd_timer_user *tu;
1824 tu = file->private_data;
1827 snd_timer_stop(tu->timeri);
1828 tu->timeri->lost = 0;
1829 tu->last_resolution = 0;
1830 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1833 static int snd_timer_user_stop(struct file *file)
1836 struct snd_timer_user *tu;
1838 tu = file->private_data;
1841 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1844 static int snd_timer_user_continue(struct file *file)
1847 struct snd_timer_user *tu;
1849 tu = file->private_data;
1852 /* start timer instead of continue if it's not used before */
1853 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1854 return snd_timer_user_start(file);
1855 tu->timeri->lost = 0;
1856 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1859 static int snd_timer_user_pause(struct file *file)
1862 struct snd_timer_user *tu;
1864 tu = file->private_data;
1867 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1871 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1872 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1873 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1874 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1877 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1880 struct snd_timer_user *tu;
1881 void __user *argp = (void __user *)arg;
1882 int __user *p = argp;
1884 tu = file->private_data;
1886 case SNDRV_TIMER_IOCTL_PVERSION:
1887 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1888 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1889 return snd_timer_user_next_device(argp);
1890 case SNDRV_TIMER_IOCTL_TREAD:
1894 if (tu->timeri) /* too late */
1896 if (get_user(xarg, p))
1898 tu->tread = xarg ? 1 : 0;
1901 case SNDRV_TIMER_IOCTL_GINFO:
1902 return snd_timer_user_ginfo(file, argp);
1903 case SNDRV_TIMER_IOCTL_GPARAMS:
1904 return snd_timer_user_gparams(file, argp);
1905 case SNDRV_TIMER_IOCTL_GSTATUS:
1906 return snd_timer_user_gstatus(file, argp);
1907 case SNDRV_TIMER_IOCTL_SELECT:
1908 return snd_timer_user_tselect(file, argp);
1909 case SNDRV_TIMER_IOCTL_INFO:
1910 return snd_timer_user_info(file, argp);
1911 case SNDRV_TIMER_IOCTL_PARAMS:
1912 return snd_timer_user_params(file, argp);
1913 case SNDRV_TIMER_IOCTL_STATUS:
1914 return snd_timer_user_status(file, argp);
1915 case SNDRV_TIMER_IOCTL_START:
1916 case SNDRV_TIMER_IOCTL_START_OLD:
1917 return snd_timer_user_start(file);
1918 case SNDRV_TIMER_IOCTL_STOP:
1919 case SNDRV_TIMER_IOCTL_STOP_OLD:
1920 return snd_timer_user_stop(file);
1921 case SNDRV_TIMER_IOCTL_CONTINUE:
1922 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1923 return snd_timer_user_continue(file);
1924 case SNDRV_TIMER_IOCTL_PAUSE:
1925 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1926 return snd_timer_user_pause(file);
1931 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1934 struct snd_timer_user *tu = file->private_data;
1937 mutex_lock(&tu->ioctl_lock);
1938 ret = __snd_timer_user_ioctl(file, cmd, arg);
1939 mutex_unlock(&tu->ioctl_lock);
1943 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1945 struct snd_timer_user *tu;
1947 tu = file->private_data;
1948 return fasync_helper(fd, file, on, &tu->fasync);
1951 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1952 size_t count, loff_t *offset)
1954 struct snd_timer_user *tu;
1955 long result = 0, unit;
1959 tu = file->private_data;
1960 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1961 mutex_lock(&tu->ioctl_lock);
1962 spin_lock_irq(&tu->qlock);
1963 while ((long)count - result >= unit) {
1964 while (!tu->qused) {
1967 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1972 set_current_state(TASK_INTERRUPTIBLE);
1973 init_waitqueue_entry(&wait, current);
1974 add_wait_queue(&tu->qchange_sleep, &wait);
1976 spin_unlock_irq(&tu->qlock);
1977 mutex_unlock(&tu->ioctl_lock);
1979 mutex_lock(&tu->ioctl_lock);
1980 spin_lock_irq(&tu->qlock);
1982 remove_wait_queue(&tu->qchange_sleep, &wait);
1984 if (tu->disconnected) {
1988 if (signal_pending(current)) {
1994 qhead = tu->qhead++;
1995 tu->qhead %= tu->queue_size;
1997 spin_unlock_irq(&tu->qlock);
2000 if (copy_to_user(buffer, &tu->tqueue[qhead],
2001 sizeof(struct snd_timer_tread)))
2004 if (copy_to_user(buffer, &tu->queue[qhead],
2005 sizeof(struct snd_timer_read)))
2009 spin_lock_irq(&tu->qlock);
2016 spin_unlock_irq(&tu->qlock);
2017 mutex_unlock(&tu->ioctl_lock);
2018 return result > 0 ? result : err;
2021 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
2024 struct snd_timer_user *tu;
2026 tu = file->private_data;
2028 poll_wait(file, &tu->qchange_sleep, wait);
2032 mask |= POLLIN | POLLRDNORM;
2033 if (tu->disconnected)
2039 #ifdef CONFIG_COMPAT
2040 #include "timer_compat.c"
2042 #define snd_timer_user_ioctl_compat NULL
2045 static const struct file_operations snd_timer_f_ops =
2047 .owner = THIS_MODULE,
2048 .read = snd_timer_user_read,
2049 .open = snd_timer_user_open,
2050 .release = snd_timer_user_release,
2051 .llseek = no_llseek,
2052 .poll = snd_timer_user_poll,
2053 .unlocked_ioctl = snd_timer_user_ioctl,
2054 .compat_ioctl = snd_timer_user_ioctl_compat,
2055 .fasync = snd_timer_user_fasync,
2058 /* unregister the system timer */
2059 static void snd_timer_free_all(void)
2061 struct snd_timer *timer, *n;
2063 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2064 snd_timer_free(timer);
2067 static struct device timer_dev;
2073 static int __init alsa_timer_init(void)
2077 snd_device_initialize(&timer_dev, NULL);
2078 dev_set_name(&timer_dev, "timer");
2080 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2081 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2085 err = snd_timer_register_system();
2087 pr_err("ALSA: unable to register system timer (%i)\n", err);
2088 put_device(&timer_dev);
2092 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2093 &snd_timer_f_ops, NULL, &timer_dev);
2095 pr_err("ALSA: unable to register timer device (%i)\n", err);
2096 snd_timer_free_all();
2097 put_device(&timer_dev);
2101 snd_timer_proc_init();
2105 static void __exit alsa_timer_exit(void)
2107 snd_unregister_device(&timer_dev);
2108 snd_timer_free_all();
2109 put_device(&timer_dev);
2110 snd_timer_proc_done();
2111 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2112 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2116 module_init(alsa_timer_init)
2117 module_exit(alsa_timer_exit)
2119 EXPORT_SYMBOL(snd_timer_open);
2120 EXPORT_SYMBOL(snd_timer_close);
2121 EXPORT_SYMBOL(snd_timer_resolution);
2122 EXPORT_SYMBOL(snd_timer_start);
2123 EXPORT_SYMBOL(snd_timer_stop);
2124 EXPORT_SYMBOL(snd_timer_continue);
2125 EXPORT_SYMBOL(snd_timer_pause);
2126 EXPORT_SYMBOL(snd_timer_new);
2127 EXPORT_SYMBOL(snd_timer_notify);
2128 EXPORT_SYMBOL(snd_timer_global_new);
2129 EXPORT_SYMBOL(snd_timer_global_free);
2130 EXPORT_SYMBOL(snd_timer_global_register);
2131 EXPORT_SYMBOL(snd_timer_interrupt);