2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/mutex.h>
27 #include <linux/device.h>
28 #include <linux/module.h>
29 #include <linux/string.h>
30 #include <linux/sched/signal.h>
31 #include <sound/core.h>
32 #include <sound/timer.h>
33 #include <sound/control.h>
34 #include <sound/info.h>
35 #include <sound/minors.h>
36 #include <sound/initval.h>
37 #include <linux/kmod.h>
40 #define SNDRV_TIMER_IFLG_PAUSED 0x00010000
42 #if IS_ENABLED(CONFIG_SND_HRTIMER)
43 #define DEFAULT_TIMER_LIMIT 4
45 #define DEFAULT_TIMER_LIMIT 1
48 static int timer_limit = DEFAULT_TIMER_LIMIT;
49 static int timer_tstamp_monotonic = 1;
50 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
51 MODULE_DESCRIPTION("ALSA timer interface");
52 MODULE_LICENSE("GPL");
53 module_param(timer_limit, int, 0444);
54 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
55 module_param(timer_tstamp_monotonic, int, 0444);
56 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
58 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
59 MODULE_ALIAS("devname:snd/timer");
61 struct snd_timer_user {
62 struct snd_timer_instance *timeri;
63 int tread; /* enhanced read with timestamps and events */
65 unsigned long overrun;
71 struct snd_timer_read *queue;
72 struct snd_timer_tread *tqueue;
74 unsigned long last_resolution;
76 struct timespec tstamp; /* trigger tstamp */
77 wait_queue_head_t qchange_sleep;
78 struct fasync_struct *fasync;
79 struct mutex ioctl_lock;
83 static LIST_HEAD(snd_timer_list);
85 /* list of slave instances */
86 static LIST_HEAD(snd_timer_slave_list);
88 /* lock for slave active lists */
89 static DEFINE_SPINLOCK(slave_active_lock);
91 static DEFINE_MUTEX(register_mutex);
93 static int snd_timer_free(struct snd_timer *timer);
94 static int snd_timer_dev_free(struct snd_device *device);
95 static int snd_timer_dev_register(struct snd_device *device);
96 static int snd_timer_dev_disconnect(struct snd_device *device);
98 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
101 * create a timer instance with the given owner string.
102 * when timer is not NULL, increments the module counter
104 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
105 struct snd_timer *timer)
107 struct snd_timer_instance *timeri;
108 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
111 timeri->owner = kstrdup(owner, GFP_KERNEL);
112 if (! timeri->owner) {
116 INIT_LIST_HEAD(&timeri->open_list);
117 INIT_LIST_HEAD(&timeri->active_list);
118 INIT_LIST_HEAD(&timeri->ack_list);
119 INIT_LIST_HEAD(&timeri->slave_list_head);
120 INIT_LIST_HEAD(&timeri->slave_active_head);
122 timeri->timer = timer;
123 if (timer && !try_module_get(timer->module)) {
124 kfree(timeri->owner);
133 * find a timer instance from the given timer id
135 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
137 struct snd_timer *timer = NULL;
139 list_for_each_entry(timer, &snd_timer_list, device_list) {
140 if (timer->tmr_class != tid->dev_class)
142 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
143 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
144 (timer->card == NULL ||
145 timer->card->number != tid->card))
147 if (timer->tmr_device != tid->device)
149 if (timer->tmr_subdevice != tid->subdevice)
156 #ifdef CONFIG_MODULES
158 static void snd_timer_request(struct snd_timer_id *tid)
160 switch (tid->dev_class) {
161 case SNDRV_TIMER_CLASS_GLOBAL:
162 if (tid->device < timer_limit)
163 request_module("snd-timer-%i", tid->device);
165 case SNDRV_TIMER_CLASS_CARD:
166 case SNDRV_TIMER_CLASS_PCM:
167 if (tid->card < snd_ecards_limit)
168 request_module("snd-card-%i", tid->card);
178 * look for a master instance matching with the slave id of the given slave.
179 * when found, relink the open_link of the slave.
181 * call this with register_mutex down.
183 static void snd_timer_check_slave(struct snd_timer_instance *slave)
185 struct snd_timer *timer;
186 struct snd_timer_instance *master;
188 /* FIXME: it's really dumb to look up all entries.. */
189 list_for_each_entry(timer, &snd_timer_list, device_list) {
190 list_for_each_entry(master, &timer->open_list_head, open_list) {
191 if (slave->slave_class == master->slave_class &&
192 slave->slave_id == master->slave_id) {
193 list_move_tail(&slave->open_list,
194 &master->slave_list_head);
195 spin_lock_irq(&slave_active_lock);
196 slave->master = master;
197 slave->timer = master->timer;
198 spin_unlock_irq(&slave_active_lock);
206 * look for slave instances matching with the slave id of the given master.
207 * when found, relink the open_link of slaves.
209 * call this with register_mutex down.
211 static void snd_timer_check_master(struct snd_timer_instance *master)
213 struct snd_timer_instance *slave, *tmp;
215 /* check all pending slaves */
216 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
217 if (slave->slave_class == master->slave_class &&
218 slave->slave_id == master->slave_id) {
219 list_move_tail(&slave->open_list, &master->slave_list_head);
220 spin_lock_irq(&slave_active_lock);
221 spin_lock(&master->timer->lock);
222 slave->master = master;
223 slave->timer = master->timer;
224 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
225 list_add_tail(&slave->active_list,
226 &master->slave_active_head);
227 spin_unlock(&master->timer->lock);
228 spin_unlock_irq(&slave_active_lock);
234 * open a timer instance
235 * when opening a master, the slave id must be here given.
237 int snd_timer_open(struct snd_timer_instance **ti,
238 char *owner, struct snd_timer_id *tid,
239 unsigned int slave_id)
241 struct snd_timer *timer;
242 struct snd_timer_instance *timeri = NULL;
244 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
245 /* open a slave instance */
246 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
247 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
248 pr_debug("ALSA: timer: invalid slave class %i\n",
252 mutex_lock(®ister_mutex);
253 timeri = snd_timer_instance_new(owner, NULL);
255 mutex_unlock(®ister_mutex);
258 timeri->slave_class = tid->dev_sclass;
259 timeri->slave_id = tid->device;
260 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
261 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
262 snd_timer_check_slave(timeri);
263 mutex_unlock(®ister_mutex);
268 /* open a master instance */
269 mutex_lock(®ister_mutex);
270 timer = snd_timer_find(tid);
271 #ifdef CONFIG_MODULES
273 mutex_unlock(®ister_mutex);
274 snd_timer_request(tid);
275 mutex_lock(®ister_mutex);
276 timer = snd_timer_find(tid);
280 mutex_unlock(®ister_mutex);
283 if (!list_empty(&timer->open_list_head)) {
284 timeri = list_entry(timer->open_list_head.next,
285 struct snd_timer_instance, open_list);
286 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
287 mutex_unlock(®ister_mutex);
291 timeri = snd_timer_instance_new(owner, timer);
293 mutex_unlock(®ister_mutex);
296 /* take a card refcount for safe disconnection */
298 get_device(&timer->card->card_dev);
299 timeri->slave_class = tid->dev_sclass;
300 timeri->slave_id = slave_id;
302 if (list_empty(&timer->open_list_head) && timer->hw.open) {
303 int err = timer->hw.open(timer);
305 kfree(timeri->owner);
309 put_device(&timer->card->card_dev);
310 module_put(timer->module);
311 mutex_unlock(®ister_mutex);
316 list_add_tail(&timeri->open_list, &timer->open_list_head);
317 snd_timer_check_master(timeri);
318 mutex_unlock(®ister_mutex);
322 EXPORT_SYMBOL(snd_timer_open);
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 mutex_lock(®ister_mutex);
336 list_del(&timeri->open_list);
338 /* force to stop the timer */
339 snd_timer_stop(timeri);
341 timer = timeri->timer;
343 /* wait, until the active callback is finished */
344 spin_lock_irq(&timer->lock);
345 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
346 spin_unlock_irq(&timer->lock);
348 spin_lock_irq(&timer->lock);
350 spin_unlock_irq(&timer->lock);
352 /* remove slave links */
353 spin_lock_irq(&slave_active_lock);
354 spin_lock(&timer->lock);
355 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
357 list_move_tail(&slave->open_list, &snd_timer_slave_list);
358 slave->master = NULL;
360 list_del_init(&slave->ack_list);
361 list_del_init(&slave->active_list);
363 spin_unlock(&timer->lock);
364 spin_unlock_irq(&slave_active_lock);
366 /* slave doesn't need to release timer resources below */
367 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
371 if (timeri->private_free)
372 timeri->private_free(timeri);
373 kfree(timeri->owner);
377 if (list_empty(&timer->open_list_head) && timer->hw.close)
378 timer->hw.close(timer);
379 /* release a card refcount for safe disconnection */
381 put_device(&timer->card->card_dev);
382 module_put(timer->module);
385 mutex_unlock(®ister_mutex);
388 EXPORT_SYMBOL(snd_timer_close);
390 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
392 struct snd_timer * timer;
396 if ((timer = timeri->timer) != NULL) {
397 if (timer->hw.c_resolution)
398 return timer->hw.c_resolution(timer);
399 return timer->hw.resolution;
403 EXPORT_SYMBOL(snd_timer_resolution);
405 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
407 struct snd_timer *timer;
408 unsigned long resolution = 0;
409 struct snd_timer_instance *ts;
410 struct timespec tstamp;
412 if (timer_tstamp_monotonic)
413 ktime_get_ts(&tstamp);
415 getnstimeofday(&tstamp);
416 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
417 event > SNDRV_TIMER_EVENT_PAUSE))
419 if (event == SNDRV_TIMER_EVENT_START ||
420 event == SNDRV_TIMER_EVENT_CONTINUE)
421 resolution = snd_timer_resolution(ti);
423 ti->ccallback(ti, event, &tstamp, resolution);
424 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
429 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
431 list_for_each_entry(ts, &ti->slave_active_head, active_list)
433 ts->ccallback(ts, event + 100, &tstamp, resolution);
436 /* start/continue a master timer */
437 static int snd_timer_start1(struct snd_timer_instance *timeri,
438 bool start, unsigned long ticks)
440 struct snd_timer *timer;
444 timer = timeri->timer;
448 spin_lock_irqsave(&timer->lock, flags);
449 if (timer->card && timer->card->shutdown) {
453 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
454 SNDRV_TIMER_IFLG_START)) {
460 timeri->ticks = timeri->cticks = ticks;
461 else if (!timeri->cticks)
465 list_move_tail(&timeri->active_list, &timer->active_list_head);
466 if (timer->running) {
467 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
469 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
470 timeri->flags |= SNDRV_TIMER_IFLG_START;
471 result = 1; /* delayed start */
474 timer->sticks = ticks;
475 timer->hw.start(timer);
478 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
481 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
482 SNDRV_TIMER_EVENT_CONTINUE);
484 spin_unlock_irqrestore(&timer->lock, flags);
488 /* start/continue a slave timer */
489 static int snd_timer_start_slave(struct snd_timer_instance *timeri,
494 spin_lock_irqsave(&slave_active_lock, flags);
495 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
496 spin_unlock_irqrestore(&slave_active_lock, flags);
499 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
500 if (timeri->master && timeri->timer) {
501 spin_lock(&timeri->timer->lock);
502 list_add_tail(&timeri->active_list,
503 &timeri->master->slave_active_head);
504 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
505 SNDRV_TIMER_EVENT_CONTINUE);
506 spin_unlock(&timeri->timer->lock);
508 spin_unlock_irqrestore(&slave_active_lock, flags);
509 return 1; /* delayed start */
512 /* stop/pause a master timer */
513 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
515 struct snd_timer *timer;
519 timer = timeri->timer;
522 spin_lock_irqsave(&timer->lock, flags);
523 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
524 SNDRV_TIMER_IFLG_START))) {
528 list_del_init(&timeri->ack_list);
529 list_del_init(&timeri->active_list);
530 if (timer->card && timer->card->shutdown)
533 timeri->cticks = timeri->ticks;
536 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
537 !(--timer->running)) {
538 timer->hw.stop(timer);
539 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
540 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
541 snd_timer_reschedule(timer, 0);
542 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
543 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
544 timer->hw.start(timer);
548 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
550 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
552 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
553 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
554 SNDRV_TIMER_EVENT_CONTINUE);
556 spin_unlock_irqrestore(&timer->lock, flags);
560 /* stop/pause a slave timer */
561 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
565 spin_lock_irqsave(&slave_active_lock, flags);
566 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
567 spin_unlock_irqrestore(&slave_active_lock, flags);
570 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
572 spin_lock(&timeri->timer->lock);
573 list_del_init(&timeri->ack_list);
574 list_del_init(&timeri->active_list);
575 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
576 SNDRV_TIMER_EVENT_CONTINUE);
577 spin_unlock(&timeri->timer->lock);
579 spin_unlock_irqrestore(&slave_active_lock, flags);
584 * start the timer instance
586 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
588 if (timeri == NULL || ticks < 1)
590 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
591 return snd_timer_start_slave(timeri, true);
593 return snd_timer_start1(timeri, true, ticks);
595 EXPORT_SYMBOL(snd_timer_start);
598 * stop the timer instance.
600 * do not call this from the timer callback!
602 int snd_timer_stop(struct snd_timer_instance *timeri)
604 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
605 return snd_timer_stop_slave(timeri, true);
607 return snd_timer_stop1(timeri, true);
609 EXPORT_SYMBOL(snd_timer_stop);
612 * start again.. the tick is kept.
614 int snd_timer_continue(struct snd_timer_instance *timeri)
616 /* timer can continue only after pause */
617 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
620 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
621 return snd_timer_start_slave(timeri, false);
623 return snd_timer_start1(timeri, false, 0);
625 EXPORT_SYMBOL(snd_timer_continue);
628 * pause.. remember the ticks left
630 int snd_timer_pause(struct snd_timer_instance * timeri)
632 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
633 return snd_timer_stop_slave(timeri, false);
635 return snd_timer_stop1(timeri, false);
637 EXPORT_SYMBOL(snd_timer_pause);
640 * reschedule the timer
642 * start pending instances and check the scheduling ticks.
643 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
645 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
647 struct snd_timer_instance *ti;
648 unsigned long ticks = ~0UL;
650 list_for_each_entry(ti, &timer->active_list_head, active_list) {
651 if (ti->flags & SNDRV_TIMER_IFLG_START) {
652 ti->flags &= ~SNDRV_TIMER_IFLG_START;
653 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
656 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
657 if (ticks > ti->cticks)
662 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
665 if (ticks > timer->hw.ticks)
666 ticks = timer->hw.ticks;
667 if (ticks_left != ticks)
668 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
669 timer->sticks = ticks;
676 static void snd_timer_tasklet(unsigned long arg)
678 struct snd_timer *timer = (struct snd_timer *) arg;
679 struct snd_timer_instance *ti;
681 unsigned long resolution, ticks;
684 if (timer->card && timer->card->shutdown)
687 spin_lock_irqsave(&timer->lock, flags);
688 /* now process all callbacks */
689 while (!list_empty(&timer->sack_list_head)) {
690 p = timer->sack_list_head.next; /* get first item */
691 ti = list_entry(p, struct snd_timer_instance, ack_list);
693 /* remove from ack_list and make empty */
698 resolution = ti->resolution;
700 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
701 spin_unlock(&timer->lock);
703 ti->callback(ti, resolution, ticks);
704 spin_lock(&timer->lock);
705 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
707 spin_unlock_irqrestore(&timer->lock, flags);
713 * ticks_left is usually equal to timer->sticks.
716 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
718 struct snd_timer_instance *ti, *ts, *tmp;
719 unsigned long resolution, ticks;
720 struct list_head *p, *ack_list_head;
727 if (timer->card && timer->card->shutdown)
730 spin_lock_irqsave(&timer->lock, flags);
732 /* remember the current resolution */
733 if (timer->hw.c_resolution)
734 resolution = timer->hw.c_resolution(timer);
736 resolution = timer->hw.resolution;
738 /* loop for all active instances
739 * Here we cannot use list_for_each_entry because the active_list of a
740 * processed instance is relinked to done_list_head before the callback
743 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
745 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
747 ti->pticks += ticks_left;
748 ti->resolution = resolution;
749 if (ti->cticks < ticks_left)
752 ti->cticks -= ticks_left;
753 if (ti->cticks) /* not expired */
755 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
756 ti->cticks = ti->ticks;
758 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
760 list_del_init(&ti->active_list);
762 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
763 (ti->flags & SNDRV_TIMER_IFLG_FAST))
764 ack_list_head = &timer->ack_list_head;
766 ack_list_head = &timer->sack_list_head;
767 if (list_empty(&ti->ack_list))
768 list_add_tail(&ti->ack_list, ack_list_head);
769 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
770 ts->pticks = ti->pticks;
771 ts->resolution = resolution;
772 if (list_empty(&ts->ack_list))
773 list_add_tail(&ts->ack_list, ack_list_head);
776 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
777 snd_timer_reschedule(timer, timer->sticks);
778 if (timer->running) {
779 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
780 timer->hw.stop(timer);
781 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
783 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
784 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
786 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
787 timer->hw.start(timer);
790 timer->hw.stop(timer);
793 /* now process all fast callbacks */
794 while (!list_empty(&timer->ack_list_head)) {
795 p = timer->ack_list_head.next; /* get first item */
796 ti = list_entry(p, struct snd_timer_instance, ack_list);
798 /* remove from ack_list and make empty */
804 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
805 spin_unlock(&timer->lock);
807 ti->callback(ti, resolution, ticks);
808 spin_lock(&timer->lock);
809 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
812 /* do we have any slow callbacks? */
813 use_tasklet = !list_empty(&timer->sack_list_head);
814 spin_unlock_irqrestore(&timer->lock, flags);
817 tasklet_schedule(&timer->task_queue);
819 EXPORT_SYMBOL(snd_timer_interrupt);
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);
870 EXPORT_SYMBOL(snd_timer_new);
872 static int snd_timer_free(struct snd_timer *timer)
877 mutex_lock(®ister_mutex);
878 if (! list_empty(&timer->open_list_head)) {
879 struct list_head *p, *n;
880 struct snd_timer_instance *ti;
881 pr_warn("ALSA: timer %p is busy?\n", timer);
882 list_for_each_safe(p, n, &timer->open_list_head) {
884 ti = list_entry(p, struct snd_timer_instance, open_list);
888 list_del(&timer->device_list);
889 mutex_unlock(®ister_mutex);
891 if (timer->private_free)
892 timer->private_free(timer);
897 static int snd_timer_dev_free(struct snd_device *device)
899 struct snd_timer *timer = device->device_data;
900 return snd_timer_free(timer);
903 static int snd_timer_dev_register(struct snd_device *dev)
905 struct snd_timer *timer = dev->device_data;
906 struct snd_timer *timer1;
908 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
910 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
911 !timer->hw.resolution && timer->hw.c_resolution == NULL)
914 mutex_lock(®ister_mutex);
915 list_for_each_entry(timer1, &snd_timer_list, device_list) {
916 if (timer1->tmr_class > timer->tmr_class)
918 if (timer1->tmr_class < timer->tmr_class)
920 if (timer1->card && timer->card) {
921 if (timer1->card->number > timer->card->number)
923 if (timer1->card->number < timer->card->number)
926 if (timer1->tmr_device > timer->tmr_device)
928 if (timer1->tmr_device < timer->tmr_device)
930 if (timer1->tmr_subdevice > timer->tmr_subdevice)
932 if (timer1->tmr_subdevice < timer->tmr_subdevice)
935 mutex_unlock(®ister_mutex);
938 list_add_tail(&timer->device_list, &timer1->device_list);
939 mutex_unlock(®ister_mutex);
943 static int snd_timer_dev_disconnect(struct snd_device *device)
945 struct snd_timer *timer = device->device_data;
946 struct snd_timer_instance *ti;
948 mutex_lock(®ister_mutex);
949 list_del_init(&timer->device_list);
950 /* wake up pending sleepers */
951 list_for_each_entry(ti, &timer->open_list_head, open_list) {
955 mutex_unlock(®ister_mutex);
959 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
962 unsigned long resolution = 0;
963 struct snd_timer_instance *ti, *ts;
965 if (timer->card && timer->card->shutdown)
967 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
969 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
970 event > SNDRV_TIMER_EVENT_MRESUME))
972 spin_lock_irqsave(&timer->lock, flags);
973 if (event == SNDRV_TIMER_EVENT_MSTART ||
974 event == SNDRV_TIMER_EVENT_MCONTINUE ||
975 event == SNDRV_TIMER_EVENT_MRESUME) {
976 if (timer->hw.c_resolution)
977 resolution = timer->hw.c_resolution(timer);
979 resolution = timer->hw.resolution;
981 list_for_each_entry(ti, &timer->active_list_head, active_list) {
983 ti->ccallback(ti, event, tstamp, resolution);
984 list_for_each_entry(ts, &ti->slave_active_head, active_list)
986 ts->ccallback(ts, event, tstamp, resolution);
988 spin_unlock_irqrestore(&timer->lock, flags);
990 EXPORT_SYMBOL(snd_timer_notify);
993 * exported functions for global timers
995 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
997 struct snd_timer_id tid;
999 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1000 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1002 tid.device = device;
1004 return snd_timer_new(NULL, id, &tid, rtimer);
1006 EXPORT_SYMBOL(snd_timer_global_new);
1008 int snd_timer_global_free(struct snd_timer *timer)
1010 return snd_timer_free(timer);
1012 EXPORT_SYMBOL(snd_timer_global_free);
1014 int snd_timer_global_register(struct snd_timer *timer)
1016 struct snd_device dev;
1018 memset(&dev, 0, sizeof(dev));
1019 dev.device_data = timer;
1020 return snd_timer_dev_register(&dev);
1022 EXPORT_SYMBOL(snd_timer_global_register);
1028 struct snd_timer_system_private {
1029 struct timer_list tlist;
1030 unsigned long last_expires;
1031 unsigned long last_jiffies;
1032 unsigned long correction;
1035 static void snd_timer_s_function(unsigned long data)
1037 struct snd_timer *timer = (struct snd_timer *)data;
1038 struct snd_timer_system_private *priv = timer->private_data;
1039 unsigned long jiff = jiffies;
1040 if (time_after(jiff, priv->last_expires))
1041 priv->correction += (long)jiff - (long)priv->last_expires;
1042 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1045 static int snd_timer_s_start(struct snd_timer * timer)
1047 struct snd_timer_system_private *priv;
1048 unsigned long njiff;
1050 priv = (struct snd_timer_system_private *) timer->private_data;
1051 njiff = (priv->last_jiffies = jiffies);
1052 if (priv->correction > timer->sticks - 1) {
1053 priv->correction -= timer->sticks - 1;
1056 njiff += timer->sticks - priv->correction;
1057 priv->correction = 0;
1059 priv->last_expires = njiff;
1060 mod_timer(&priv->tlist, njiff);
1064 static int snd_timer_s_stop(struct snd_timer * timer)
1066 struct snd_timer_system_private *priv;
1069 priv = (struct snd_timer_system_private *) timer->private_data;
1070 del_timer(&priv->tlist);
1072 if (time_before(jiff, priv->last_expires))
1073 timer->sticks = priv->last_expires - jiff;
1076 priv->correction = 0;
1080 static int snd_timer_s_close(struct snd_timer *timer)
1082 struct snd_timer_system_private *priv;
1084 priv = (struct snd_timer_system_private *)timer->private_data;
1085 del_timer_sync(&priv->tlist);
1089 static struct snd_timer_hardware snd_timer_system =
1091 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1092 .resolution = 1000000000L / HZ,
1094 .close = snd_timer_s_close,
1095 .start = snd_timer_s_start,
1096 .stop = snd_timer_s_stop
1099 static void snd_timer_free_system(struct snd_timer *timer)
1101 kfree(timer->private_data);
1104 static int snd_timer_register_system(void)
1106 struct snd_timer *timer;
1107 struct snd_timer_system_private *priv;
1110 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1113 strcpy(timer->name, "system timer");
1114 timer->hw = snd_timer_system;
1115 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1117 snd_timer_free(timer);
1120 setup_timer(&priv->tlist, snd_timer_s_function, (unsigned long) timer);
1121 timer->private_data = priv;
1122 timer->private_free = snd_timer_free_system;
1123 return snd_timer_global_register(timer);
1126 #ifdef CONFIG_SND_PROC_FS
1131 static void snd_timer_proc_read(struct snd_info_entry *entry,
1132 struct snd_info_buffer *buffer)
1134 struct snd_timer *timer;
1135 struct snd_timer_instance *ti;
1137 mutex_lock(®ister_mutex);
1138 list_for_each_entry(timer, &snd_timer_list, device_list) {
1139 if (timer->card && timer->card->shutdown)
1141 switch (timer->tmr_class) {
1142 case SNDRV_TIMER_CLASS_GLOBAL:
1143 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1145 case SNDRV_TIMER_CLASS_CARD:
1146 snd_iprintf(buffer, "C%i-%i: ",
1147 timer->card->number, timer->tmr_device);
1149 case SNDRV_TIMER_CLASS_PCM:
1150 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1151 timer->tmr_device, timer->tmr_subdevice);
1154 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1155 timer->card ? timer->card->number : -1,
1156 timer->tmr_device, timer->tmr_subdevice);
1158 snd_iprintf(buffer, "%s :", timer->name);
1159 if (timer->hw.resolution)
1160 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1161 timer->hw.resolution / 1000,
1162 timer->hw.resolution % 1000,
1164 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1165 snd_iprintf(buffer, " SLAVE");
1166 snd_iprintf(buffer, "\n");
1167 list_for_each_entry(ti, &timer->open_list_head, open_list)
1168 snd_iprintf(buffer, " Client %s : %s\n",
1169 ti->owner ? ti->owner : "unknown",
1170 ti->flags & (SNDRV_TIMER_IFLG_START |
1171 SNDRV_TIMER_IFLG_RUNNING)
1172 ? "running" : "stopped");
1174 mutex_unlock(®ister_mutex);
1177 static struct snd_info_entry *snd_timer_proc_entry;
1179 static void __init snd_timer_proc_init(void)
1181 struct snd_info_entry *entry;
1183 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1184 if (entry != NULL) {
1185 entry->c.text.read = snd_timer_proc_read;
1186 if (snd_info_register(entry) < 0) {
1187 snd_info_free_entry(entry);
1191 snd_timer_proc_entry = entry;
1194 static void __exit snd_timer_proc_done(void)
1196 snd_info_free_entry(snd_timer_proc_entry);
1198 #else /* !CONFIG_SND_PROC_FS */
1199 #define snd_timer_proc_init()
1200 #define snd_timer_proc_done()
1204 * USER SPACE interface
1207 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1208 unsigned long resolution,
1209 unsigned long ticks)
1211 struct snd_timer_user *tu = timeri->callback_data;
1212 struct snd_timer_read *r;
1215 spin_lock(&tu->qlock);
1216 if (tu->qused > 0) {
1217 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1218 r = &tu->queue[prev];
1219 if (r->resolution == resolution) {
1224 if (tu->qused >= tu->queue_size) {
1227 r = &tu->queue[tu->qtail++];
1228 tu->qtail %= tu->queue_size;
1229 r->resolution = resolution;
1234 spin_unlock(&tu->qlock);
1235 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1236 wake_up(&tu->qchange_sleep);
1239 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1240 struct snd_timer_tread *tread)
1242 if (tu->qused >= tu->queue_size) {
1245 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1246 tu->qtail %= tu->queue_size;
1251 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1253 struct timespec *tstamp,
1254 unsigned long resolution)
1256 struct snd_timer_user *tu = timeri->callback_data;
1257 struct snd_timer_tread r1;
1258 unsigned long flags;
1260 if (event >= SNDRV_TIMER_EVENT_START &&
1261 event <= SNDRV_TIMER_EVENT_PAUSE)
1262 tu->tstamp = *tstamp;
1263 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1265 memset(&r1, 0, sizeof(r1));
1267 r1.tstamp = *tstamp;
1268 r1.val = resolution;
1269 spin_lock_irqsave(&tu->qlock, flags);
1270 snd_timer_user_append_to_tqueue(tu, &r1);
1271 spin_unlock_irqrestore(&tu->qlock, flags);
1272 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1273 wake_up(&tu->qchange_sleep);
1276 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1278 struct snd_timer_user *tu = timeri->callback_data;
1280 tu->disconnected = true;
1281 wake_up(&tu->qchange_sleep);
1284 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1285 unsigned long resolution,
1286 unsigned long ticks)
1288 struct snd_timer_user *tu = timeri->callback_data;
1289 struct snd_timer_tread *r, r1;
1290 struct timespec tstamp;
1291 int prev, append = 0;
1293 memset(&r1, 0, sizeof(r1));
1294 memset(&tstamp, 0, sizeof(tstamp));
1295 spin_lock(&tu->qlock);
1296 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1297 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1298 spin_unlock(&tu->qlock);
1301 if (tu->last_resolution != resolution || ticks > 0) {
1302 if (timer_tstamp_monotonic)
1303 ktime_get_ts(&tstamp);
1305 getnstimeofday(&tstamp);
1307 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1308 tu->last_resolution != resolution) {
1309 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1311 r1.val = resolution;
1312 snd_timer_user_append_to_tqueue(tu, &r1);
1313 tu->last_resolution = resolution;
1316 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1320 if (tu->qused > 0) {
1321 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1322 r = &tu->tqueue[prev];
1323 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1330 r1.event = SNDRV_TIMER_EVENT_TICK;
1333 snd_timer_user_append_to_tqueue(tu, &r1);
1336 spin_unlock(&tu->qlock);
1339 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1340 wake_up(&tu->qchange_sleep);
1343 static int realloc_user_queue(struct snd_timer_user *tu, int size)
1345 struct snd_timer_read *queue = NULL;
1346 struct snd_timer_tread *tqueue = NULL;
1349 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
1353 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
1358 spin_lock_irq(&tu->qlock);
1361 tu->queue_size = size;
1363 tu->tqueue = tqueue;
1364 tu->qhead = tu->qtail = tu->qused = 0;
1365 spin_unlock_irq(&tu->qlock);
1370 static int snd_timer_user_open(struct inode *inode, struct file *file)
1372 struct snd_timer_user *tu;
1375 err = nonseekable_open(inode, file);
1379 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1382 spin_lock_init(&tu->qlock);
1383 init_waitqueue_head(&tu->qchange_sleep);
1384 mutex_init(&tu->ioctl_lock);
1386 if (realloc_user_queue(tu, 128) < 0) {
1390 file->private_data = tu;
1394 static int snd_timer_user_release(struct inode *inode, struct file *file)
1396 struct snd_timer_user *tu;
1398 if (file->private_data) {
1399 tu = file->private_data;
1400 file->private_data = NULL;
1401 mutex_lock(&tu->ioctl_lock);
1403 snd_timer_close(tu->timeri);
1404 mutex_unlock(&tu->ioctl_lock);
1412 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1414 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1415 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1421 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1423 id->dev_class = timer->tmr_class;
1424 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1425 id->card = timer->card ? timer->card->number : -1;
1426 id->device = timer->tmr_device;
1427 id->subdevice = timer->tmr_subdevice;
1430 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1432 struct snd_timer_id id;
1433 struct snd_timer *timer;
1434 struct list_head *p;
1436 if (copy_from_user(&id, _tid, sizeof(id)))
1438 mutex_lock(®ister_mutex);
1439 if (id.dev_class < 0) { /* first item */
1440 if (list_empty(&snd_timer_list))
1441 snd_timer_user_zero_id(&id);
1443 timer = list_entry(snd_timer_list.next,
1444 struct snd_timer, device_list);
1445 snd_timer_user_copy_id(&id, timer);
1448 switch (id.dev_class) {
1449 case SNDRV_TIMER_CLASS_GLOBAL:
1450 id.device = id.device < 0 ? 0 : id.device + 1;
1451 list_for_each(p, &snd_timer_list) {
1452 timer = list_entry(p, struct snd_timer, device_list);
1453 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1454 snd_timer_user_copy_id(&id, timer);
1457 if (timer->tmr_device >= id.device) {
1458 snd_timer_user_copy_id(&id, timer);
1462 if (p == &snd_timer_list)
1463 snd_timer_user_zero_id(&id);
1465 case SNDRV_TIMER_CLASS_CARD:
1466 case SNDRV_TIMER_CLASS_PCM:
1470 if (id.device < 0) {
1473 if (id.subdevice < 0)
1479 list_for_each(p, &snd_timer_list) {
1480 timer = list_entry(p, struct snd_timer, device_list);
1481 if (timer->tmr_class > id.dev_class) {
1482 snd_timer_user_copy_id(&id, timer);
1485 if (timer->tmr_class < id.dev_class)
1487 if (timer->card->number > id.card) {
1488 snd_timer_user_copy_id(&id, timer);
1491 if (timer->card->number < id.card)
1493 if (timer->tmr_device > id.device) {
1494 snd_timer_user_copy_id(&id, timer);
1497 if (timer->tmr_device < id.device)
1499 if (timer->tmr_subdevice > id.subdevice) {
1500 snd_timer_user_copy_id(&id, timer);
1503 if (timer->tmr_subdevice < id.subdevice)
1505 snd_timer_user_copy_id(&id, timer);
1508 if (p == &snd_timer_list)
1509 snd_timer_user_zero_id(&id);
1512 snd_timer_user_zero_id(&id);
1515 mutex_unlock(®ister_mutex);
1516 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1521 static int snd_timer_user_ginfo(struct file *file,
1522 struct snd_timer_ginfo __user *_ginfo)
1524 struct snd_timer_ginfo *ginfo;
1525 struct snd_timer_id tid;
1526 struct snd_timer *t;
1527 struct list_head *p;
1530 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1532 return PTR_ERR(ginfo);
1535 memset(ginfo, 0, sizeof(*ginfo));
1537 mutex_lock(®ister_mutex);
1538 t = snd_timer_find(&tid);
1540 ginfo->card = t->card ? t->card->number : -1;
1541 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1542 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1543 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1544 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1545 ginfo->resolution = t->hw.resolution;
1546 if (t->hw.resolution_min > 0) {
1547 ginfo->resolution_min = t->hw.resolution_min;
1548 ginfo->resolution_max = t->hw.resolution_max;
1550 list_for_each(p, &t->open_list_head) {
1556 mutex_unlock(®ister_mutex);
1557 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1563 static int timer_set_gparams(struct snd_timer_gparams *gparams)
1565 struct snd_timer *t;
1568 mutex_lock(®ister_mutex);
1569 t = snd_timer_find(&gparams->tid);
1574 if (!list_empty(&t->open_list_head)) {
1578 if (!t->hw.set_period) {
1582 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
1584 mutex_unlock(®ister_mutex);
1588 static int snd_timer_user_gparams(struct file *file,
1589 struct snd_timer_gparams __user *_gparams)
1591 struct snd_timer_gparams gparams;
1593 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1595 return timer_set_gparams(&gparams);
1598 static int snd_timer_user_gstatus(struct file *file,
1599 struct snd_timer_gstatus __user *_gstatus)
1601 struct snd_timer_gstatus gstatus;
1602 struct snd_timer_id tid;
1603 struct snd_timer *t;
1606 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1609 memset(&gstatus, 0, sizeof(gstatus));
1611 mutex_lock(®ister_mutex);
1612 t = snd_timer_find(&tid);
1614 if (t->hw.c_resolution)
1615 gstatus.resolution = t->hw.c_resolution(t);
1617 gstatus.resolution = t->hw.resolution;
1618 if (t->hw.precise_resolution) {
1619 t->hw.precise_resolution(t, &gstatus.resolution_num,
1620 &gstatus.resolution_den);
1622 gstatus.resolution_num = gstatus.resolution;
1623 gstatus.resolution_den = 1000000000uL;
1628 mutex_unlock(®ister_mutex);
1629 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1634 static int snd_timer_user_tselect(struct file *file,
1635 struct snd_timer_select __user *_tselect)
1637 struct snd_timer_user *tu;
1638 struct snd_timer_select tselect;
1642 tu = file->private_data;
1644 snd_timer_close(tu->timeri);
1647 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1651 sprintf(str, "application %i", current->pid);
1652 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1653 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1654 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1658 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1659 tu->timeri->callback = tu->tread
1660 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1661 tu->timeri->ccallback = snd_timer_user_ccallback;
1662 tu->timeri->callback_data = (void *)tu;
1663 tu->timeri->disconnect = snd_timer_user_disconnect;
1669 static int snd_timer_user_info(struct file *file,
1670 struct snd_timer_info __user *_info)
1672 struct snd_timer_user *tu;
1673 struct snd_timer_info *info;
1674 struct snd_timer *t;
1677 tu = file->private_data;
1680 t = tu->timeri->timer;
1684 info = kzalloc(sizeof(*info), GFP_KERNEL);
1687 info->card = t->card ? t->card->number : -1;
1688 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1689 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1690 strlcpy(info->id, t->id, sizeof(info->id));
1691 strlcpy(info->name, t->name, sizeof(info->name));
1692 info->resolution = t->hw.resolution;
1693 if (copy_to_user(_info, info, sizeof(*_info)))
1699 static int snd_timer_user_params(struct file *file,
1700 struct snd_timer_params __user *_params)
1702 struct snd_timer_user *tu;
1703 struct snd_timer_params params;
1704 struct snd_timer *t;
1707 tu = file->private_data;
1710 t = tu->timeri->timer;
1713 if (copy_from_user(¶ms, _params, sizeof(params)))
1715 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1718 if (params.ticks < 1) {
1723 /* Don't allow resolution less than 1ms */
1724 resolution = snd_timer_resolution(tu->timeri);
1725 resolution *= params.ticks;
1726 if (resolution < 1000000) {
1731 if (params.queue_size > 0 &&
1732 (params.queue_size < 32 || params.queue_size > 1024)) {
1736 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1737 (1<<SNDRV_TIMER_EVENT_TICK)|
1738 (1<<SNDRV_TIMER_EVENT_START)|
1739 (1<<SNDRV_TIMER_EVENT_STOP)|
1740 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1741 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1742 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1743 (1<<SNDRV_TIMER_EVENT_RESUME)|
1744 (1<<SNDRV_TIMER_EVENT_MSTART)|
1745 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1746 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1747 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1748 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1749 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1753 snd_timer_stop(tu->timeri);
1754 spin_lock_irq(&t->lock);
1755 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1756 SNDRV_TIMER_IFLG_EXCLUSIVE|
1757 SNDRV_TIMER_IFLG_EARLY_EVENT);
1758 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1759 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1760 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1761 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1762 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1763 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1764 spin_unlock_irq(&t->lock);
1765 if (params.queue_size > 0 &&
1766 (unsigned int)tu->queue_size != params.queue_size) {
1767 err = realloc_user_queue(tu, params.queue_size);
1771 spin_lock_irq(&tu->qlock);
1772 tu->qhead = tu->qtail = tu->qused = 0;
1773 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1775 struct snd_timer_tread tread;
1776 memset(&tread, 0, sizeof(tread));
1777 tread.event = SNDRV_TIMER_EVENT_EARLY;
1778 tread.tstamp.tv_sec = 0;
1779 tread.tstamp.tv_nsec = 0;
1781 snd_timer_user_append_to_tqueue(tu, &tread);
1783 struct snd_timer_read *r = &tu->queue[0];
1790 tu->filter = params.filter;
1791 tu->ticks = params.ticks;
1792 spin_unlock_irq(&tu->qlock);
1795 if (copy_to_user(_params, ¶ms, sizeof(params)))
1800 static int snd_timer_user_status(struct file *file,
1801 struct snd_timer_status __user *_status)
1803 struct snd_timer_user *tu;
1804 struct snd_timer_status status;
1806 tu = file->private_data;
1809 memset(&status, 0, sizeof(status));
1810 status.tstamp = tu->tstamp;
1811 status.resolution = snd_timer_resolution(tu->timeri);
1812 status.lost = tu->timeri->lost;
1813 status.overrun = tu->overrun;
1814 spin_lock_irq(&tu->qlock);
1815 status.queue = tu->qused;
1816 spin_unlock_irq(&tu->qlock);
1817 if (copy_to_user(_status, &status, sizeof(status)))
1822 static int snd_timer_user_start(struct file *file)
1825 struct snd_timer_user *tu;
1827 tu = file->private_data;
1830 snd_timer_stop(tu->timeri);
1831 tu->timeri->lost = 0;
1832 tu->last_resolution = 0;
1833 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1836 static int snd_timer_user_stop(struct file *file)
1839 struct snd_timer_user *tu;
1841 tu = file->private_data;
1844 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1847 static int snd_timer_user_continue(struct file *file)
1850 struct snd_timer_user *tu;
1852 tu = file->private_data;
1855 /* start timer instead of continue if it's not used before */
1856 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1857 return snd_timer_user_start(file);
1858 tu->timeri->lost = 0;
1859 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1862 static int snd_timer_user_pause(struct file *file)
1865 struct snd_timer_user *tu;
1867 tu = file->private_data;
1870 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1874 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1875 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1876 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1877 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1880 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1883 struct snd_timer_user *tu;
1884 void __user *argp = (void __user *)arg;
1885 int __user *p = argp;
1887 tu = file->private_data;
1889 case SNDRV_TIMER_IOCTL_PVERSION:
1890 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1891 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1892 return snd_timer_user_next_device(argp);
1893 case SNDRV_TIMER_IOCTL_TREAD:
1895 int xarg, old_tread;
1897 if (tu->timeri) /* too late */
1899 if (get_user(xarg, p))
1901 old_tread = tu->tread;
1902 tu->tread = xarg ? 1 : 0;
1903 if (tu->tread != old_tread &&
1904 realloc_user_queue(tu, tu->queue_size) < 0) {
1905 tu->tread = old_tread;
1910 case SNDRV_TIMER_IOCTL_GINFO:
1911 return snd_timer_user_ginfo(file, argp);
1912 case SNDRV_TIMER_IOCTL_GPARAMS:
1913 return snd_timer_user_gparams(file, argp);
1914 case SNDRV_TIMER_IOCTL_GSTATUS:
1915 return snd_timer_user_gstatus(file, argp);
1916 case SNDRV_TIMER_IOCTL_SELECT:
1917 return snd_timer_user_tselect(file, argp);
1918 case SNDRV_TIMER_IOCTL_INFO:
1919 return snd_timer_user_info(file, argp);
1920 case SNDRV_TIMER_IOCTL_PARAMS:
1921 return snd_timer_user_params(file, argp);
1922 case SNDRV_TIMER_IOCTL_STATUS:
1923 return snd_timer_user_status(file, argp);
1924 case SNDRV_TIMER_IOCTL_START:
1925 case SNDRV_TIMER_IOCTL_START_OLD:
1926 return snd_timer_user_start(file);
1927 case SNDRV_TIMER_IOCTL_STOP:
1928 case SNDRV_TIMER_IOCTL_STOP_OLD:
1929 return snd_timer_user_stop(file);
1930 case SNDRV_TIMER_IOCTL_CONTINUE:
1931 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1932 return snd_timer_user_continue(file);
1933 case SNDRV_TIMER_IOCTL_PAUSE:
1934 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1935 return snd_timer_user_pause(file);
1940 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1943 struct snd_timer_user *tu = file->private_data;
1946 mutex_lock(&tu->ioctl_lock);
1947 ret = __snd_timer_user_ioctl(file, cmd, arg);
1948 mutex_unlock(&tu->ioctl_lock);
1952 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1954 struct snd_timer_user *tu;
1956 tu = file->private_data;
1957 return fasync_helper(fd, file, on, &tu->fasync);
1960 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1961 size_t count, loff_t *offset)
1963 struct snd_timer_user *tu;
1964 long result = 0, unit;
1968 tu = file->private_data;
1969 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1970 mutex_lock(&tu->ioctl_lock);
1971 spin_lock_irq(&tu->qlock);
1972 while ((long)count - result >= unit) {
1973 while (!tu->qused) {
1974 wait_queue_entry_t wait;
1976 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1981 set_current_state(TASK_INTERRUPTIBLE);
1982 init_waitqueue_entry(&wait, current);
1983 add_wait_queue(&tu->qchange_sleep, &wait);
1985 spin_unlock_irq(&tu->qlock);
1986 mutex_unlock(&tu->ioctl_lock);
1988 mutex_lock(&tu->ioctl_lock);
1989 spin_lock_irq(&tu->qlock);
1991 remove_wait_queue(&tu->qchange_sleep, &wait);
1993 if (tu->disconnected) {
1997 if (signal_pending(current)) {
2003 qhead = tu->qhead++;
2004 tu->qhead %= tu->queue_size;
2006 spin_unlock_irq(&tu->qlock);
2009 if (copy_to_user(buffer, &tu->tqueue[qhead],
2010 sizeof(struct snd_timer_tread)))
2013 if (copy_to_user(buffer, &tu->queue[qhead],
2014 sizeof(struct snd_timer_read)))
2018 spin_lock_irq(&tu->qlock);
2025 spin_unlock_irq(&tu->qlock);
2026 mutex_unlock(&tu->ioctl_lock);
2027 return result > 0 ? result : err;
2030 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
2033 struct snd_timer_user *tu;
2035 tu = file->private_data;
2037 poll_wait(file, &tu->qchange_sleep, wait);
2040 spin_lock_irq(&tu->qlock);
2042 mask |= POLLIN | POLLRDNORM;
2043 if (tu->disconnected)
2045 spin_unlock_irq(&tu->qlock);
2050 #ifdef CONFIG_COMPAT
2051 #include "timer_compat.c"
2053 #define snd_timer_user_ioctl_compat NULL
2056 static const struct file_operations snd_timer_f_ops =
2058 .owner = THIS_MODULE,
2059 .read = snd_timer_user_read,
2060 .open = snd_timer_user_open,
2061 .release = snd_timer_user_release,
2062 .llseek = no_llseek,
2063 .poll = snd_timer_user_poll,
2064 .unlocked_ioctl = snd_timer_user_ioctl,
2065 .compat_ioctl = snd_timer_user_ioctl_compat,
2066 .fasync = snd_timer_user_fasync,
2069 /* unregister the system timer */
2070 static void snd_timer_free_all(void)
2072 struct snd_timer *timer, *n;
2074 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2075 snd_timer_free(timer);
2078 static struct device timer_dev;
2084 static int __init alsa_timer_init(void)
2088 snd_device_initialize(&timer_dev, NULL);
2089 dev_set_name(&timer_dev, "timer");
2091 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2092 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2096 err = snd_timer_register_system();
2098 pr_err("ALSA: unable to register system timer (%i)\n", err);
2099 put_device(&timer_dev);
2103 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2104 &snd_timer_f_ops, NULL, &timer_dev);
2106 pr_err("ALSA: unable to register timer device (%i)\n", err);
2107 snd_timer_free_all();
2108 put_device(&timer_dev);
2112 snd_timer_proc_init();
2116 static void __exit alsa_timer_exit(void)
2118 snd_unregister_device(&timer_dev);
2119 snd_timer_free_all();
2120 put_device(&timer_dev);
2121 snd_timer_proc_done();
2122 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2123 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2127 module_init(alsa_timer_init)
2128 module_exit(alsa_timer_exit)