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>
38 #if IS_ENABLED(CONFIG_SND_HRTIMER)
39 #define DEFAULT_TIMER_LIMIT 4
40 #elif IS_ENABLED(CONFIG_SND_RTCTIMER)
41 #define DEFAULT_TIMER_LIMIT 2
43 #define DEFAULT_TIMER_LIMIT 1
46 static int timer_limit = DEFAULT_TIMER_LIMIT;
47 static int timer_tstamp_monotonic = 1;
48 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
49 MODULE_DESCRIPTION("ALSA timer interface");
50 MODULE_LICENSE("GPL");
51 module_param(timer_limit, int, 0444);
52 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
53 module_param(timer_tstamp_monotonic, int, 0444);
54 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
56 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
57 MODULE_ALIAS("devname:snd/timer");
59 struct snd_timer_user {
60 struct snd_timer_instance *timeri;
61 int tread; /* enhanced read with timestamps and events */
63 unsigned long overrun;
69 struct snd_timer_read *queue;
70 struct snd_timer_tread *tqueue;
72 unsigned long last_resolution;
74 struct timespec tstamp; /* trigger tstamp */
75 wait_queue_head_t qchange_sleep;
76 struct fasync_struct *fasync;
77 struct mutex ioctl_lock;
81 static LIST_HEAD(snd_timer_list);
83 /* list of slave instances */
84 static LIST_HEAD(snd_timer_slave_list);
86 /* lock for slave active lists */
87 static DEFINE_SPINLOCK(slave_active_lock);
89 static DEFINE_MUTEX(register_mutex);
91 static int snd_timer_free(struct snd_timer *timer);
92 static int snd_timer_dev_free(struct snd_device *device);
93 static int snd_timer_dev_register(struct snd_device *device);
94 static int snd_timer_dev_disconnect(struct snd_device *device);
96 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
99 * create a timer instance with the given owner string.
100 * when timer is not NULL, increments the module counter
102 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
103 struct snd_timer *timer)
105 struct snd_timer_instance *timeri;
106 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
109 timeri->owner = kstrdup(owner, GFP_KERNEL);
110 if (! timeri->owner) {
114 INIT_LIST_HEAD(&timeri->open_list);
115 INIT_LIST_HEAD(&timeri->active_list);
116 INIT_LIST_HEAD(&timeri->ack_list);
117 INIT_LIST_HEAD(&timeri->slave_list_head);
118 INIT_LIST_HEAD(&timeri->slave_active_head);
120 timeri->timer = timer;
121 if (timer && !try_module_get(timer->module)) {
122 kfree(timeri->owner);
131 * find a timer instance from the given timer id
133 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
135 struct snd_timer *timer = NULL;
137 list_for_each_entry(timer, &snd_timer_list, device_list) {
138 if (timer->tmr_class != tid->dev_class)
140 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
141 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
142 (timer->card == NULL ||
143 timer->card->number != tid->card))
145 if (timer->tmr_device != tid->device)
147 if (timer->tmr_subdevice != tid->subdevice)
154 #ifdef CONFIG_MODULES
156 static void snd_timer_request(struct snd_timer_id *tid)
158 switch (tid->dev_class) {
159 case SNDRV_TIMER_CLASS_GLOBAL:
160 if (tid->device < timer_limit)
161 request_module("snd-timer-%i", tid->device);
163 case SNDRV_TIMER_CLASS_CARD:
164 case SNDRV_TIMER_CLASS_PCM:
165 if (tid->card < snd_ecards_limit)
166 request_module("snd-card-%i", tid->card);
176 * look for a master instance matching with the slave id of the given slave.
177 * when found, relink the open_link of the slave.
179 * call this with register_mutex down.
181 static void snd_timer_check_slave(struct snd_timer_instance *slave)
183 struct snd_timer *timer;
184 struct snd_timer_instance *master;
186 /* FIXME: it's really dumb to look up all entries.. */
187 list_for_each_entry(timer, &snd_timer_list, device_list) {
188 list_for_each_entry(master, &timer->open_list_head, open_list) {
189 if (slave->slave_class == master->slave_class &&
190 slave->slave_id == master->slave_id) {
191 list_move_tail(&slave->open_list,
192 &master->slave_list_head);
193 spin_lock_irq(&slave_active_lock);
194 slave->master = master;
195 slave->timer = master->timer;
196 spin_unlock_irq(&slave_active_lock);
204 * look for slave instances matching with the slave id of the given master.
205 * when found, relink the open_link of slaves.
207 * call this with register_mutex down.
209 static void snd_timer_check_master(struct snd_timer_instance *master)
211 struct snd_timer_instance *slave, *tmp;
213 /* check all pending slaves */
214 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
215 if (slave->slave_class == master->slave_class &&
216 slave->slave_id == master->slave_id) {
217 list_move_tail(&slave->open_list, &master->slave_list_head);
218 spin_lock_irq(&slave_active_lock);
219 spin_lock(&master->timer->lock);
220 slave->master = master;
221 slave->timer = master->timer;
222 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
223 list_add_tail(&slave->active_list,
224 &master->slave_active_head);
225 spin_unlock(&master->timer->lock);
226 spin_unlock_irq(&slave_active_lock);
232 * open a timer instance
233 * when opening a master, the slave id must be here given.
235 int snd_timer_open(struct snd_timer_instance **ti,
236 char *owner, struct snd_timer_id *tid,
237 unsigned int slave_id)
239 struct snd_timer *timer;
240 struct snd_timer_instance *timeri = NULL;
242 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
243 /* open a slave instance */
244 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
245 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
246 pr_debug("ALSA: timer: invalid slave class %i\n",
250 mutex_lock(®ister_mutex);
251 timeri = snd_timer_instance_new(owner, NULL);
253 mutex_unlock(®ister_mutex);
256 timeri->slave_class = tid->dev_sclass;
257 timeri->slave_id = tid->device;
258 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
259 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
260 snd_timer_check_slave(timeri);
261 mutex_unlock(®ister_mutex);
266 /* open a master instance */
267 mutex_lock(®ister_mutex);
268 timer = snd_timer_find(tid);
269 #ifdef CONFIG_MODULES
271 mutex_unlock(®ister_mutex);
272 snd_timer_request(tid);
273 mutex_lock(®ister_mutex);
274 timer = snd_timer_find(tid);
278 mutex_unlock(®ister_mutex);
281 if (!list_empty(&timer->open_list_head)) {
282 timeri = list_entry(timer->open_list_head.next,
283 struct snd_timer_instance, open_list);
284 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
285 mutex_unlock(®ister_mutex);
289 timeri = snd_timer_instance_new(owner, timer);
291 mutex_unlock(®ister_mutex);
294 /* take a card refcount for safe disconnection */
296 get_device(&timer->card->card_dev);
297 timeri->slave_class = tid->dev_sclass;
298 timeri->slave_id = slave_id;
300 if (list_empty(&timer->open_list_head) && timer->hw.open) {
301 int err = timer->hw.open(timer);
303 kfree(timeri->owner);
307 put_device(&timer->card->card_dev);
308 module_put(timer->module);
309 mutex_unlock(®ister_mutex);
314 list_add_tail(&timeri->open_list, &timer->open_list_head);
315 snd_timer_check_master(timeri);
316 mutex_unlock(®ister_mutex);
321 static int _snd_timer_stop(struct snd_timer_instance *timeri, int event);
324 * close a timer instance
326 int snd_timer_close(struct snd_timer_instance *timeri)
328 struct snd_timer *timer = NULL;
329 struct snd_timer_instance *slave, *tmp;
331 if (snd_BUG_ON(!timeri))
334 /* force to stop the timer */
335 snd_timer_stop(timeri);
337 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
338 /* wait, until the active callback is finished */
339 spin_lock_irq(&slave_active_lock);
340 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
341 spin_unlock_irq(&slave_active_lock);
343 spin_lock_irq(&slave_active_lock);
345 spin_unlock_irq(&slave_active_lock);
346 mutex_lock(®ister_mutex);
347 list_del(&timeri->open_list);
348 mutex_unlock(®ister_mutex);
350 timer = timeri->timer;
351 if (snd_BUG_ON(!timer))
353 /* wait, until the active callback is finished */
354 spin_lock_irq(&timer->lock);
355 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
356 spin_unlock_irq(&timer->lock);
358 spin_lock_irq(&timer->lock);
360 spin_unlock_irq(&timer->lock);
361 mutex_lock(®ister_mutex);
362 list_del(&timeri->open_list);
363 if (list_empty(&timer->open_list_head) &&
365 timer->hw.close(timer);
366 /* remove slave links */
367 spin_lock_irq(&slave_active_lock);
368 spin_lock(&timer->lock);
369 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
371 list_move_tail(&slave->open_list, &snd_timer_slave_list);
372 slave->master = NULL;
374 list_del_init(&slave->ack_list);
375 list_del_init(&slave->active_list);
377 spin_unlock(&timer->lock);
378 spin_unlock_irq(&slave_active_lock);
379 /* release a card refcount for safe disconnection */
381 put_device(&timer->card->card_dev);
382 mutex_unlock(®ister_mutex);
385 if (timeri->private_free)
386 timeri->private_free(timeri);
387 kfree(timeri->owner);
390 module_put(timer->module);
394 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
396 struct snd_timer * timer;
400 if ((timer = timeri->timer) != NULL) {
401 if (timer->hw.c_resolution)
402 return timer->hw.c_resolution(timer);
403 return timer->hw.resolution;
408 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
410 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 spin_lock_irqsave(&timer->lock, flags);
436 list_for_each_entry(ts, &ti->slave_active_head, active_list)
438 ts->ccallback(ts, event + 100, &tstamp, resolution);
439 spin_unlock_irqrestore(&timer->lock, flags);
442 static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
443 unsigned long sticks)
445 list_move_tail(&timeri->active_list, &timer->active_list_head);
446 if (timer->running) {
447 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
449 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
450 timeri->flags |= SNDRV_TIMER_IFLG_START;
451 return 1; /* delayed start */
453 timer->sticks = sticks;
454 timer->hw.start(timer);
457 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
462 static int snd_timer_start_slave(struct snd_timer_instance *timeri)
466 spin_lock_irqsave(&slave_active_lock, flags);
467 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
468 spin_unlock_irqrestore(&slave_active_lock, flags);
471 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
472 if (timeri->master && timeri->timer) {
473 spin_lock(&timeri->timer->lock);
474 list_add_tail(&timeri->active_list,
475 &timeri->master->slave_active_head);
476 spin_unlock(&timeri->timer->lock);
478 spin_unlock_irqrestore(&slave_active_lock, flags);
479 return 1; /* delayed start */
483 * start the timer instance
485 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
487 struct snd_timer *timer;
488 int result = -EINVAL;
491 if (timeri == NULL || ticks < 1)
493 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
494 result = snd_timer_start_slave(timeri);
496 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
499 timer = timeri->timer;
502 if (timer->card && timer->card->shutdown)
504 spin_lock_irqsave(&timer->lock, flags);
505 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
506 SNDRV_TIMER_IFLG_START)) {
510 timeri->ticks = timeri->cticks = ticks;
512 result = snd_timer_start1(timer, timeri, ticks);
514 spin_unlock_irqrestore(&timer->lock, flags);
516 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
520 static int _snd_timer_stop(struct snd_timer_instance *timeri, int event)
522 struct snd_timer *timer;
525 if (snd_BUG_ON(!timeri))
528 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
529 spin_lock_irqsave(&slave_active_lock, flags);
530 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
531 spin_unlock_irqrestore(&slave_active_lock, flags);
535 spin_lock(&timeri->timer->lock);
536 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
537 list_del_init(&timeri->ack_list);
538 list_del_init(&timeri->active_list);
540 spin_unlock(&timeri->timer->lock);
541 spin_unlock_irqrestore(&slave_active_lock, flags);
544 timer = timeri->timer;
547 spin_lock_irqsave(&timer->lock, flags);
548 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
549 SNDRV_TIMER_IFLG_START))) {
550 spin_unlock_irqrestore(&timer->lock, flags);
553 list_del_init(&timeri->ack_list);
554 list_del_init(&timeri->active_list);
555 if (timer->card && timer->card->shutdown) {
556 spin_unlock_irqrestore(&timer->lock, flags);
559 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
560 !(--timer->running)) {
561 timer->hw.stop(timer);
562 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
563 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
564 snd_timer_reschedule(timer, 0);
565 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
566 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
567 timer->hw.start(timer);
571 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
572 spin_unlock_irqrestore(&timer->lock, flags);
574 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
575 snd_timer_notify1(timeri, event);
580 * stop the timer instance.
582 * do not call this from the timer callback!
584 int snd_timer_stop(struct snd_timer_instance *timeri)
586 struct snd_timer *timer;
590 err = _snd_timer_stop(timeri, SNDRV_TIMER_EVENT_STOP);
593 timer = timeri->timer;
596 spin_lock_irqsave(&timer->lock, flags);
597 timeri->cticks = timeri->ticks;
599 spin_unlock_irqrestore(&timer->lock, flags);
604 * start again.. the tick is kept.
606 int snd_timer_continue(struct snd_timer_instance *timeri)
608 struct snd_timer *timer;
609 int result = -EINVAL;
614 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
615 return snd_timer_start_slave(timeri);
616 timer = timeri->timer;
619 if (timer->card && timer->card->shutdown)
621 spin_lock_irqsave(&timer->lock, flags);
622 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
629 result = snd_timer_start1(timer, timeri, timer->sticks);
631 spin_unlock_irqrestore(&timer->lock, flags);
632 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
637 * pause.. remember the ticks left
639 int snd_timer_pause(struct snd_timer_instance * timeri)
641 return _snd_timer_stop(timeri, SNDRV_TIMER_EVENT_PAUSE);
645 * reschedule the timer
647 * start pending instances and check the scheduling ticks.
648 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
650 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
652 struct snd_timer_instance *ti;
653 unsigned long ticks = ~0UL;
655 list_for_each_entry(ti, &timer->active_list_head, active_list) {
656 if (ti->flags & SNDRV_TIMER_IFLG_START) {
657 ti->flags &= ~SNDRV_TIMER_IFLG_START;
658 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
661 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
662 if (ticks > ti->cticks)
667 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
670 if (ticks > timer->hw.ticks)
671 ticks = timer->hw.ticks;
672 if (ticks_left != ticks)
673 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
674 timer->sticks = ticks;
681 static void snd_timer_tasklet(unsigned long arg)
683 struct snd_timer *timer = (struct snd_timer *) arg;
684 struct snd_timer_instance *ti;
686 unsigned long resolution, ticks;
689 if (timer->card && timer->card->shutdown)
692 spin_lock_irqsave(&timer->lock, flags);
693 /* now process all callbacks */
694 while (!list_empty(&timer->sack_list_head)) {
695 p = timer->sack_list_head.next; /* get first item */
696 ti = list_entry(p, struct snd_timer_instance, ack_list);
698 /* remove from ack_list and make empty */
703 resolution = ti->resolution;
705 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
706 spin_unlock(&timer->lock);
708 ti->callback(ti, resolution, ticks);
709 spin_lock(&timer->lock);
710 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
712 spin_unlock_irqrestore(&timer->lock, flags);
718 * ticks_left is usually equal to timer->sticks.
721 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
723 struct snd_timer_instance *ti, *ts, *tmp;
724 unsigned long resolution, ticks;
725 struct list_head *p, *ack_list_head;
732 if (timer->card && timer->card->shutdown)
735 spin_lock_irqsave(&timer->lock, flags);
737 /* remember the current resolution */
738 if (timer->hw.c_resolution)
739 resolution = timer->hw.c_resolution(timer);
741 resolution = timer->hw.resolution;
743 /* loop for all active instances
744 * Here we cannot use list_for_each_entry because the active_list of a
745 * processed instance is relinked to done_list_head before the callback
748 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
750 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
752 ti->pticks += ticks_left;
753 ti->resolution = resolution;
754 if (ti->cticks < ticks_left)
757 ti->cticks -= ticks_left;
758 if (ti->cticks) /* not expired */
760 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
761 ti->cticks = ti->ticks;
763 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
765 list_del_init(&ti->active_list);
767 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
768 (ti->flags & SNDRV_TIMER_IFLG_FAST))
769 ack_list_head = &timer->ack_list_head;
771 ack_list_head = &timer->sack_list_head;
772 if (list_empty(&ti->ack_list))
773 list_add_tail(&ti->ack_list, ack_list_head);
774 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
775 ts->pticks = ti->pticks;
776 ts->resolution = resolution;
777 if (list_empty(&ts->ack_list))
778 list_add_tail(&ts->ack_list, ack_list_head);
781 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
782 snd_timer_reschedule(timer, timer->sticks);
783 if (timer->running) {
784 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
785 timer->hw.stop(timer);
786 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
788 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
789 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
791 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
792 timer->hw.start(timer);
795 timer->hw.stop(timer);
798 /* now process all fast callbacks */
799 while (!list_empty(&timer->ack_list_head)) {
800 p = timer->ack_list_head.next; /* get first item */
801 ti = list_entry(p, struct snd_timer_instance, ack_list);
803 /* remove from ack_list and make empty */
809 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
810 spin_unlock(&timer->lock);
812 ti->callback(ti, resolution, ticks);
813 spin_lock(&timer->lock);
814 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
817 /* do we have any slow callbacks? */
818 use_tasklet = !list_empty(&timer->sack_list_head);
819 spin_unlock_irqrestore(&timer->lock, flags);
822 tasklet_schedule(&timer->task_queue);
829 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
830 struct snd_timer **rtimer)
832 struct snd_timer *timer;
834 static struct snd_device_ops ops = {
835 .dev_free = snd_timer_dev_free,
836 .dev_register = snd_timer_dev_register,
837 .dev_disconnect = snd_timer_dev_disconnect,
840 if (snd_BUG_ON(!tid))
844 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
847 timer->tmr_class = tid->dev_class;
849 timer->tmr_device = tid->device;
850 timer->tmr_subdevice = tid->subdevice;
852 strlcpy(timer->id, id, sizeof(timer->id));
854 INIT_LIST_HEAD(&timer->device_list);
855 INIT_LIST_HEAD(&timer->open_list_head);
856 INIT_LIST_HEAD(&timer->active_list_head);
857 INIT_LIST_HEAD(&timer->ack_list_head);
858 INIT_LIST_HEAD(&timer->sack_list_head);
859 spin_lock_init(&timer->lock);
860 tasklet_init(&timer->task_queue, snd_timer_tasklet,
861 (unsigned long)timer);
863 timer->module = card->module;
864 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
866 snd_timer_free(timer);
875 static int snd_timer_free(struct snd_timer *timer)
880 mutex_lock(®ister_mutex);
881 if (! list_empty(&timer->open_list_head)) {
882 struct list_head *p, *n;
883 struct snd_timer_instance *ti;
884 pr_warn("ALSA: timer %p is busy?\n", timer);
885 list_for_each_safe(p, n, &timer->open_list_head) {
887 ti = list_entry(p, struct snd_timer_instance, open_list);
891 list_del(&timer->device_list);
892 mutex_unlock(®ister_mutex);
894 if (timer->private_free)
895 timer->private_free(timer);
900 static int snd_timer_dev_free(struct snd_device *device)
902 struct snd_timer *timer = device->device_data;
903 return snd_timer_free(timer);
906 static int snd_timer_dev_register(struct snd_device *dev)
908 struct snd_timer *timer = dev->device_data;
909 struct snd_timer *timer1;
911 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
913 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
914 !timer->hw.resolution && timer->hw.c_resolution == NULL)
917 mutex_lock(®ister_mutex);
918 list_for_each_entry(timer1, &snd_timer_list, device_list) {
919 if (timer1->tmr_class > timer->tmr_class)
921 if (timer1->tmr_class < timer->tmr_class)
923 if (timer1->card && timer->card) {
924 if (timer1->card->number > timer->card->number)
926 if (timer1->card->number < timer->card->number)
929 if (timer1->tmr_device > timer->tmr_device)
931 if (timer1->tmr_device < timer->tmr_device)
933 if (timer1->tmr_subdevice > timer->tmr_subdevice)
935 if (timer1->tmr_subdevice < timer->tmr_subdevice)
938 mutex_unlock(®ister_mutex);
941 list_add_tail(&timer->device_list, &timer1->device_list);
942 mutex_unlock(®ister_mutex);
946 /* just for reference in snd_timer_dev_disconnect() below */
947 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
948 int event, struct timespec *tstamp,
949 unsigned long resolution);
951 static int snd_timer_dev_disconnect(struct snd_device *device)
953 struct snd_timer *timer = device->device_data;
954 struct snd_timer_instance *ti;
956 mutex_lock(®ister_mutex);
957 list_del_init(&timer->device_list);
958 /* wake up pending sleepers */
959 list_for_each_entry(ti, &timer->open_list_head, open_list) {
960 /* FIXME: better to have a ti.disconnect() op */
961 if (ti->ccallback == snd_timer_user_ccallback) {
962 struct snd_timer_user *tu = ti->callback_data;
964 tu->disconnected = true;
965 wake_up(&tu->qchange_sleep);
968 mutex_unlock(®ister_mutex);
972 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
975 unsigned long resolution = 0;
976 struct snd_timer_instance *ti, *ts;
978 if (timer->card && timer->card->shutdown)
980 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
982 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
983 event > SNDRV_TIMER_EVENT_MRESUME))
985 spin_lock_irqsave(&timer->lock, flags);
986 if (event == SNDRV_TIMER_EVENT_MSTART ||
987 event == SNDRV_TIMER_EVENT_MCONTINUE ||
988 event == SNDRV_TIMER_EVENT_MRESUME) {
989 if (timer->hw.c_resolution)
990 resolution = timer->hw.c_resolution(timer);
992 resolution = timer->hw.resolution;
994 list_for_each_entry(ti, &timer->active_list_head, active_list) {
996 ti->ccallback(ti, event, tstamp, resolution);
997 list_for_each_entry(ts, &ti->slave_active_head, active_list)
999 ts->ccallback(ts, event, tstamp, resolution);
1001 spin_unlock_irqrestore(&timer->lock, flags);
1005 * exported functions for global timers
1007 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1009 struct snd_timer_id tid;
1011 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1012 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1014 tid.device = device;
1016 return snd_timer_new(NULL, id, &tid, rtimer);
1019 int snd_timer_global_free(struct snd_timer *timer)
1021 return snd_timer_free(timer);
1024 int snd_timer_global_register(struct snd_timer *timer)
1026 struct snd_device dev;
1028 memset(&dev, 0, sizeof(dev));
1029 dev.device_data = timer;
1030 return snd_timer_dev_register(&dev);
1037 struct snd_timer_system_private {
1038 struct timer_list tlist;
1039 unsigned long last_expires;
1040 unsigned long last_jiffies;
1041 unsigned long correction;
1044 static void snd_timer_s_function(unsigned long data)
1046 struct snd_timer *timer = (struct snd_timer *)data;
1047 struct snd_timer_system_private *priv = timer->private_data;
1048 unsigned long jiff = jiffies;
1049 if (time_after(jiff, priv->last_expires))
1050 priv->correction += (long)jiff - (long)priv->last_expires;
1051 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1054 static int snd_timer_s_start(struct snd_timer * timer)
1056 struct snd_timer_system_private *priv;
1057 unsigned long njiff;
1059 priv = (struct snd_timer_system_private *) timer->private_data;
1060 njiff = (priv->last_jiffies = jiffies);
1061 if (priv->correction > timer->sticks - 1) {
1062 priv->correction -= timer->sticks - 1;
1065 njiff += timer->sticks - priv->correction;
1066 priv->correction = 0;
1068 priv->last_expires = njiff;
1069 mod_timer(&priv->tlist, njiff);
1073 static int snd_timer_s_stop(struct snd_timer * timer)
1075 struct snd_timer_system_private *priv;
1078 priv = (struct snd_timer_system_private *) timer->private_data;
1079 del_timer(&priv->tlist);
1081 if (time_before(jiff, priv->last_expires))
1082 timer->sticks = priv->last_expires - jiff;
1085 priv->correction = 0;
1089 static struct snd_timer_hardware snd_timer_system =
1091 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1092 .resolution = 1000000000L / HZ,
1094 .start = snd_timer_s_start,
1095 .stop = snd_timer_s_stop
1098 static void snd_timer_free_system(struct snd_timer *timer)
1100 kfree(timer->private_data);
1103 static int snd_timer_register_system(void)
1105 struct snd_timer *timer;
1106 struct snd_timer_system_private *priv;
1109 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1112 strcpy(timer->name, "system timer");
1113 timer->hw = snd_timer_system;
1114 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1116 snd_timer_free(timer);
1119 setup_timer(&priv->tlist, snd_timer_s_function, (unsigned long) timer);
1120 timer->private_data = priv;
1121 timer->private_free = snd_timer_free_system;
1122 return snd_timer_global_register(timer);
1125 #ifdef CONFIG_SND_PROC_FS
1130 static void snd_timer_proc_read(struct snd_info_entry *entry,
1131 struct snd_info_buffer *buffer)
1133 struct snd_timer *timer;
1134 struct snd_timer_instance *ti;
1136 mutex_lock(®ister_mutex);
1137 list_for_each_entry(timer, &snd_timer_list, device_list) {
1138 if (timer->card && timer->card->shutdown)
1140 switch (timer->tmr_class) {
1141 case SNDRV_TIMER_CLASS_GLOBAL:
1142 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1144 case SNDRV_TIMER_CLASS_CARD:
1145 snd_iprintf(buffer, "C%i-%i: ",
1146 timer->card->number, timer->tmr_device);
1148 case SNDRV_TIMER_CLASS_PCM:
1149 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1150 timer->tmr_device, timer->tmr_subdevice);
1153 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1154 timer->card ? timer->card->number : -1,
1155 timer->tmr_device, timer->tmr_subdevice);
1157 snd_iprintf(buffer, "%s :", timer->name);
1158 if (timer->hw.resolution)
1159 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1160 timer->hw.resolution / 1000,
1161 timer->hw.resolution % 1000,
1163 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1164 snd_iprintf(buffer, " SLAVE");
1165 snd_iprintf(buffer, "\n");
1166 list_for_each_entry(ti, &timer->open_list_head, open_list)
1167 snd_iprintf(buffer, " Client %s : %s\n",
1168 ti->owner ? ti->owner : "unknown",
1169 ti->flags & (SNDRV_TIMER_IFLG_START |
1170 SNDRV_TIMER_IFLG_RUNNING)
1171 ? "running" : "stopped");
1173 mutex_unlock(®ister_mutex);
1176 static struct snd_info_entry *snd_timer_proc_entry;
1178 static void __init snd_timer_proc_init(void)
1180 struct snd_info_entry *entry;
1182 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1183 if (entry != NULL) {
1184 entry->c.text.read = snd_timer_proc_read;
1185 if (snd_info_register(entry) < 0) {
1186 snd_info_free_entry(entry);
1190 snd_timer_proc_entry = entry;
1193 static void __exit snd_timer_proc_done(void)
1195 snd_info_free_entry(snd_timer_proc_entry);
1197 #else /* !CONFIG_SND_PROC_FS */
1198 #define snd_timer_proc_init()
1199 #define snd_timer_proc_done()
1203 * USER SPACE interface
1206 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1207 unsigned long resolution,
1208 unsigned long ticks)
1210 struct snd_timer_user *tu = timeri->callback_data;
1211 struct snd_timer_read *r;
1214 spin_lock(&tu->qlock);
1215 if (tu->qused > 0) {
1216 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1217 r = &tu->queue[prev];
1218 if (r->resolution == resolution) {
1223 if (tu->qused >= tu->queue_size) {
1226 r = &tu->queue[tu->qtail++];
1227 tu->qtail %= tu->queue_size;
1228 r->resolution = resolution;
1233 spin_unlock(&tu->qlock);
1234 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1235 wake_up(&tu->qchange_sleep);
1238 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1239 struct snd_timer_tread *tread)
1241 if (tu->qused >= tu->queue_size) {
1244 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1245 tu->qtail %= tu->queue_size;
1250 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1252 struct timespec *tstamp,
1253 unsigned long resolution)
1255 struct snd_timer_user *tu = timeri->callback_data;
1256 struct snd_timer_tread r1;
1257 unsigned long flags;
1259 if (event >= SNDRV_TIMER_EVENT_START &&
1260 event <= SNDRV_TIMER_EVENT_PAUSE)
1261 tu->tstamp = *tstamp;
1262 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1264 memset(&r1, 0, sizeof(r1));
1266 r1.tstamp = *tstamp;
1267 r1.val = resolution;
1268 spin_lock_irqsave(&tu->qlock, flags);
1269 snd_timer_user_append_to_tqueue(tu, &r1);
1270 spin_unlock_irqrestore(&tu->qlock, flags);
1271 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1272 wake_up(&tu->qchange_sleep);
1275 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1276 unsigned long resolution,
1277 unsigned long ticks)
1279 struct snd_timer_user *tu = timeri->callback_data;
1280 struct snd_timer_tread *r, r1;
1281 struct timespec tstamp;
1282 int prev, append = 0;
1284 memset(&tstamp, 0, sizeof(tstamp));
1285 spin_lock(&tu->qlock);
1286 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1287 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1288 spin_unlock(&tu->qlock);
1291 if (tu->last_resolution != resolution || ticks > 0) {
1292 if (timer_tstamp_monotonic)
1293 ktime_get_ts(&tstamp);
1295 getnstimeofday(&tstamp);
1297 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1298 tu->last_resolution != resolution) {
1299 memset(&r1, 0, sizeof(r1));
1300 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1302 r1.val = resolution;
1303 snd_timer_user_append_to_tqueue(tu, &r1);
1304 tu->last_resolution = resolution;
1307 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1311 if (tu->qused > 0) {
1312 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1313 r = &tu->tqueue[prev];
1314 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1321 r1.event = SNDRV_TIMER_EVENT_TICK;
1324 snd_timer_user_append_to_tqueue(tu, &r1);
1327 spin_unlock(&tu->qlock);
1330 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1331 wake_up(&tu->qchange_sleep);
1334 static int snd_timer_user_open(struct inode *inode, struct file *file)
1336 struct snd_timer_user *tu;
1339 err = nonseekable_open(inode, file);
1343 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1346 spin_lock_init(&tu->qlock);
1347 init_waitqueue_head(&tu->qchange_sleep);
1348 mutex_init(&tu->ioctl_lock);
1350 tu->queue_size = 128;
1351 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1353 if (tu->queue == NULL) {
1357 file->private_data = tu;
1361 static int snd_timer_user_release(struct inode *inode, struct file *file)
1363 struct snd_timer_user *tu;
1365 if (file->private_data) {
1366 tu = file->private_data;
1367 file->private_data = NULL;
1368 mutex_lock(&tu->ioctl_lock);
1370 snd_timer_close(tu->timeri);
1371 mutex_unlock(&tu->ioctl_lock);
1379 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1381 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1382 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1388 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1390 id->dev_class = timer->tmr_class;
1391 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1392 id->card = timer->card ? timer->card->number : -1;
1393 id->device = timer->tmr_device;
1394 id->subdevice = timer->tmr_subdevice;
1397 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1399 struct snd_timer_id id;
1400 struct snd_timer *timer;
1401 struct list_head *p;
1403 if (copy_from_user(&id, _tid, sizeof(id)))
1405 mutex_lock(®ister_mutex);
1406 if (id.dev_class < 0) { /* first item */
1407 if (list_empty(&snd_timer_list))
1408 snd_timer_user_zero_id(&id);
1410 timer = list_entry(snd_timer_list.next,
1411 struct snd_timer, device_list);
1412 snd_timer_user_copy_id(&id, timer);
1415 switch (id.dev_class) {
1416 case SNDRV_TIMER_CLASS_GLOBAL:
1417 id.device = id.device < 0 ? 0 : id.device + 1;
1418 list_for_each(p, &snd_timer_list) {
1419 timer = list_entry(p, struct snd_timer, device_list);
1420 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1421 snd_timer_user_copy_id(&id, timer);
1424 if (timer->tmr_device >= id.device) {
1425 snd_timer_user_copy_id(&id, timer);
1429 if (p == &snd_timer_list)
1430 snd_timer_user_zero_id(&id);
1432 case SNDRV_TIMER_CLASS_CARD:
1433 case SNDRV_TIMER_CLASS_PCM:
1440 if (id.device < 0) {
1443 if (id.subdevice < 0) {
1451 list_for_each(p, &snd_timer_list) {
1452 timer = list_entry(p, struct snd_timer, device_list);
1453 if (timer->tmr_class > id.dev_class) {
1454 snd_timer_user_copy_id(&id, timer);
1457 if (timer->tmr_class < id.dev_class)
1459 if (timer->card->number > id.card) {
1460 snd_timer_user_copy_id(&id, timer);
1463 if (timer->card->number < id.card)
1465 if (timer->tmr_device > id.device) {
1466 snd_timer_user_copy_id(&id, timer);
1469 if (timer->tmr_device < id.device)
1471 if (timer->tmr_subdevice > id.subdevice) {
1472 snd_timer_user_copy_id(&id, timer);
1475 if (timer->tmr_subdevice < id.subdevice)
1477 snd_timer_user_copy_id(&id, timer);
1480 if (p == &snd_timer_list)
1481 snd_timer_user_zero_id(&id);
1484 snd_timer_user_zero_id(&id);
1487 mutex_unlock(®ister_mutex);
1488 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1493 static int snd_timer_user_ginfo(struct file *file,
1494 struct snd_timer_ginfo __user *_ginfo)
1496 struct snd_timer_ginfo *ginfo;
1497 struct snd_timer_id tid;
1498 struct snd_timer *t;
1499 struct list_head *p;
1502 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1504 return PTR_ERR(ginfo);
1507 memset(ginfo, 0, sizeof(*ginfo));
1509 mutex_lock(®ister_mutex);
1510 t = snd_timer_find(&tid);
1512 ginfo->card = t->card ? t->card->number : -1;
1513 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1514 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1515 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1516 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1517 ginfo->resolution = t->hw.resolution;
1518 if (t->hw.resolution_min > 0) {
1519 ginfo->resolution_min = t->hw.resolution_min;
1520 ginfo->resolution_max = t->hw.resolution_max;
1522 list_for_each(p, &t->open_list_head) {
1528 mutex_unlock(®ister_mutex);
1529 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1535 static int snd_timer_user_gparams(struct file *file,
1536 struct snd_timer_gparams __user *_gparams)
1538 struct snd_timer_gparams gparams;
1539 struct snd_timer *t;
1542 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1544 mutex_lock(®ister_mutex);
1545 t = snd_timer_find(&gparams.tid);
1550 if (!list_empty(&t->open_list_head)) {
1554 if (!t->hw.set_period) {
1558 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1560 mutex_unlock(®ister_mutex);
1564 static int snd_timer_user_gstatus(struct file *file,
1565 struct snd_timer_gstatus __user *_gstatus)
1567 struct snd_timer_gstatus gstatus;
1568 struct snd_timer_id tid;
1569 struct snd_timer *t;
1572 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1575 memset(&gstatus, 0, sizeof(gstatus));
1577 mutex_lock(®ister_mutex);
1578 t = snd_timer_find(&tid);
1580 if (t->hw.c_resolution)
1581 gstatus.resolution = t->hw.c_resolution(t);
1583 gstatus.resolution = t->hw.resolution;
1584 if (t->hw.precise_resolution) {
1585 t->hw.precise_resolution(t, &gstatus.resolution_num,
1586 &gstatus.resolution_den);
1588 gstatus.resolution_num = gstatus.resolution;
1589 gstatus.resolution_den = 1000000000uL;
1594 mutex_unlock(®ister_mutex);
1595 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1600 static int snd_timer_user_tselect(struct file *file,
1601 struct snd_timer_select __user *_tselect)
1603 struct snd_timer_user *tu;
1604 struct snd_timer_select tselect;
1608 tu = file->private_data;
1610 snd_timer_close(tu->timeri);
1613 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1617 sprintf(str, "application %i", current->pid);
1618 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1619 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1620 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1629 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1631 if (tu->tqueue == NULL)
1634 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1636 if (tu->queue == NULL)
1641 snd_timer_close(tu->timeri);
1644 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1645 tu->timeri->callback = tu->tread
1646 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1647 tu->timeri->ccallback = snd_timer_user_ccallback;
1648 tu->timeri->callback_data = (void *)tu;
1655 static int snd_timer_user_info(struct file *file,
1656 struct snd_timer_info __user *_info)
1658 struct snd_timer_user *tu;
1659 struct snd_timer_info *info;
1660 struct snd_timer *t;
1663 tu = file->private_data;
1666 t = tu->timeri->timer;
1670 info = kzalloc(sizeof(*info), GFP_KERNEL);
1673 info->card = t->card ? t->card->number : -1;
1674 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1675 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1676 strlcpy(info->id, t->id, sizeof(info->id));
1677 strlcpy(info->name, t->name, sizeof(info->name));
1678 info->resolution = t->hw.resolution;
1679 if (copy_to_user(_info, info, sizeof(*_info)))
1685 static int snd_timer_user_params(struct file *file,
1686 struct snd_timer_params __user *_params)
1688 struct snd_timer_user *tu;
1689 struct snd_timer_params params;
1690 struct snd_timer *t;
1691 struct snd_timer_read *tr;
1692 struct snd_timer_tread *ttr;
1695 tu = file->private_data;
1698 t = tu->timeri->timer;
1701 if (copy_from_user(¶ms, _params, sizeof(params)))
1703 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1707 if (params.queue_size > 0 &&
1708 (params.queue_size < 32 || params.queue_size > 1024)) {
1712 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1713 (1<<SNDRV_TIMER_EVENT_TICK)|
1714 (1<<SNDRV_TIMER_EVENT_START)|
1715 (1<<SNDRV_TIMER_EVENT_STOP)|
1716 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1717 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1718 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1719 (1<<SNDRV_TIMER_EVENT_RESUME)|
1720 (1<<SNDRV_TIMER_EVENT_MSTART)|
1721 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1722 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1723 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1724 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1725 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1729 snd_timer_stop(tu->timeri);
1730 spin_lock_irq(&t->lock);
1731 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1732 SNDRV_TIMER_IFLG_EXCLUSIVE|
1733 SNDRV_TIMER_IFLG_EARLY_EVENT);
1734 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1735 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1736 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1737 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1738 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1739 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1740 spin_unlock_irq(&t->lock);
1741 if (params.queue_size > 0 &&
1742 (unsigned int)tu->queue_size != params.queue_size) {
1744 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1748 tu->queue_size = params.queue_size;
1752 tr = kmalloc(params.queue_size * sizeof(*tr),
1756 tu->queue_size = params.queue_size;
1761 tu->qhead = tu->qtail = tu->qused = 0;
1762 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1764 struct snd_timer_tread tread;
1765 memset(&tread, 0, sizeof(tread));
1766 tread.event = SNDRV_TIMER_EVENT_EARLY;
1767 tread.tstamp.tv_sec = 0;
1768 tread.tstamp.tv_nsec = 0;
1770 snd_timer_user_append_to_tqueue(tu, &tread);
1772 struct snd_timer_read *r = &tu->queue[0];
1779 tu->filter = params.filter;
1780 tu->ticks = params.ticks;
1783 if (copy_to_user(_params, ¶ms, sizeof(params)))
1788 static int snd_timer_user_status(struct file *file,
1789 struct snd_timer_status __user *_status)
1791 struct snd_timer_user *tu;
1792 struct snd_timer_status status;
1794 tu = file->private_data;
1797 memset(&status, 0, sizeof(status));
1798 status.tstamp = tu->tstamp;
1799 status.resolution = snd_timer_resolution(tu->timeri);
1800 status.lost = tu->timeri->lost;
1801 status.overrun = tu->overrun;
1802 spin_lock_irq(&tu->qlock);
1803 status.queue = tu->qused;
1804 spin_unlock_irq(&tu->qlock);
1805 if (copy_to_user(_status, &status, sizeof(status)))
1810 static int snd_timer_user_start(struct file *file)
1813 struct snd_timer_user *tu;
1815 tu = file->private_data;
1818 snd_timer_stop(tu->timeri);
1819 tu->timeri->lost = 0;
1820 tu->last_resolution = 0;
1821 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1824 static int snd_timer_user_stop(struct file *file)
1827 struct snd_timer_user *tu;
1829 tu = file->private_data;
1832 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1835 static int snd_timer_user_continue(struct file *file)
1838 struct snd_timer_user *tu;
1840 tu = file->private_data;
1843 tu->timeri->lost = 0;
1844 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1847 static int snd_timer_user_pause(struct file *file)
1850 struct snd_timer_user *tu;
1852 tu = file->private_data;
1855 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1859 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1860 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1861 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1862 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1865 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1868 struct snd_timer_user *tu;
1869 void __user *argp = (void __user *)arg;
1870 int __user *p = argp;
1872 tu = file->private_data;
1874 case SNDRV_TIMER_IOCTL_PVERSION:
1875 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1876 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1877 return snd_timer_user_next_device(argp);
1878 case SNDRV_TIMER_IOCTL_TREAD:
1882 if (tu->timeri) /* too late */
1884 if (get_user(xarg, p))
1886 tu->tread = xarg ? 1 : 0;
1889 case SNDRV_TIMER_IOCTL_GINFO:
1890 return snd_timer_user_ginfo(file, argp);
1891 case SNDRV_TIMER_IOCTL_GPARAMS:
1892 return snd_timer_user_gparams(file, argp);
1893 case SNDRV_TIMER_IOCTL_GSTATUS:
1894 return snd_timer_user_gstatus(file, argp);
1895 case SNDRV_TIMER_IOCTL_SELECT:
1896 return snd_timer_user_tselect(file, argp);
1897 case SNDRV_TIMER_IOCTL_INFO:
1898 return snd_timer_user_info(file, argp);
1899 case SNDRV_TIMER_IOCTL_PARAMS:
1900 return snd_timer_user_params(file, argp);
1901 case SNDRV_TIMER_IOCTL_STATUS:
1902 return snd_timer_user_status(file, argp);
1903 case SNDRV_TIMER_IOCTL_START:
1904 case SNDRV_TIMER_IOCTL_START_OLD:
1905 return snd_timer_user_start(file);
1906 case SNDRV_TIMER_IOCTL_STOP:
1907 case SNDRV_TIMER_IOCTL_STOP_OLD:
1908 return snd_timer_user_stop(file);
1909 case SNDRV_TIMER_IOCTL_CONTINUE:
1910 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1911 return snd_timer_user_continue(file);
1912 case SNDRV_TIMER_IOCTL_PAUSE:
1913 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1914 return snd_timer_user_pause(file);
1919 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1922 struct snd_timer_user *tu = file->private_data;
1925 mutex_lock(&tu->ioctl_lock);
1926 ret = __snd_timer_user_ioctl(file, cmd, arg);
1927 mutex_unlock(&tu->ioctl_lock);
1931 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1933 struct snd_timer_user *tu;
1935 tu = file->private_data;
1936 return fasync_helper(fd, file, on, &tu->fasync);
1939 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1940 size_t count, loff_t *offset)
1942 struct snd_timer_user *tu;
1943 long result = 0, unit;
1947 tu = file->private_data;
1948 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1949 spin_lock_irq(&tu->qlock);
1950 while ((long)count - result >= unit) {
1951 while (!tu->qused) {
1954 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1959 set_current_state(TASK_INTERRUPTIBLE);
1960 init_waitqueue_entry(&wait, current);
1961 add_wait_queue(&tu->qchange_sleep, &wait);
1963 spin_unlock_irq(&tu->qlock);
1965 spin_lock_irq(&tu->qlock);
1967 remove_wait_queue(&tu->qchange_sleep, &wait);
1969 if (tu->disconnected) {
1973 if (signal_pending(current)) {
1979 qhead = tu->qhead++;
1980 tu->qhead %= tu->queue_size;
1982 spin_unlock_irq(&tu->qlock);
1984 mutex_lock(&tu->ioctl_lock);
1986 if (copy_to_user(buffer, &tu->tqueue[qhead],
1987 sizeof(struct snd_timer_tread)))
1990 if (copy_to_user(buffer, &tu->queue[qhead],
1991 sizeof(struct snd_timer_read)))
1994 mutex_unlock(&tu->ioctl_lock);
1996 spin_lock_irq(&tu->qlock);
2003 spin_unlock_irq(&tu->qlock);
2004 return result > 0 ? result : err;
2007 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
2010 struct snd_timer_user *tu;
2012 tu = file->private_data;
2014 poll_wait(file, &tu->qchange_sleep, wait);
2018 mask |= POLLIN | POLLRDNORM;
2019 if (tu->disconnected)
2025 #ifdef CONFIG_COMPAT
2026 #include "timer_compat.c"
2028 #define snd_timer_user_ioctl_compat NULL
2031 static const struct file_operations snd_timer_f_ops =
2033 .owner = THIS_MODULE,
2034 .read = snd_timer_user_read,
2035 .open = snd_timer_user_open,
2036 .release = snd_timer_user_release,
2037 .llseek = no_llseek,
2038 .poll = snd_timer_user_poll,
2039 .unlocked_ioctl = snd_timer_user_ioctl,
2040 .compat_ioctl = snd_timer_user_ioctl_compat,
2041 .fasync = snd_timer_user_fasync,
2044 /* unregister the system timer */
2045 static void snd_timer_free_all(void)
2047 struct snd_timer *timer, *n;
2049 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2050 snd_timer_free(timer);
2053 static struct device timer_dev;
2059 static int __init alsa_timer_init(void)
2063 snd_device_initialize(&timer_dev, NULL);
2064 dev_set_name(&timer_dev, "timer");
2066 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2067 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2071 err = snd_timer_register_system();
2073 pr_err("ALSA: unable to register system timer (%i)\n", err);
2074 put_device(&timer_dev);
2078 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2079 &snd_timer_f_ops, NULL, &timer_dev);
2081 pr_err("ALSA: unable to register timer device (%i)\n", err);
2082 snd_timer_free_all();
2083 put_device(&timer_dev);
2087 snd_timer_proc_init();
2091 static void __exit alsa_timer_exit(void)
2093 snd_unregister_device(&timer_dev);
2094 snd_timer_free_all();
2095 put_device(&timer_dev);
2096 snd_timer_proc_done();
2097 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2098 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2102 module_init(alsa_timer_init)
2103 module_exit(alsa_timer_exit)
2105 EXPORT_SYMBOL(snd_timer_open);
2106 EXPORT_SYMBOL(snd_timer_close);
2107 EXPORT_SYMBOL(snd_timer_resolution);
2108 EXPORT_SYMBOL(snd_timer_start);
2109 EXPORT_SYMBOL(snd_timer_stop);
2110 EXPORT_SYMBOL(snd_timer_continue);
2111 EXPORT_SYMBOL(snd_timer_pause);
2112 EXPORT_SYMBOL(snd_timer_new);
2113 EXPORT_SYMBOL(snd_timer_notify);
2114 EXPORT_SYMBOL(snd_timer_global_new);
2115 EXPORT_SYMBOL(snd_timer_global_free);
2116 EXPORT_SYMBOL(snd_timer_global_register);
2117 EXPORT_SYMBOL(snd_timer_interrupt);