2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/smp_lock.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/mutex.h>
29 #include <linux/moduleparam.h>
30 #include <linux/string.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>
39 #include <linux/kerneld.h>
42 #if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
43 #define DEFAULT_TIMER_LIMIT 3
44 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
45 #define DEFAULT_TIMER_LIMIT 2
47 #define DEFAULT_TIMER_LIMIT 1
50 static int timer_limit = DEFAULT_TIMER_LIMIT;
51 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
52 MODULE_DESCRIPTION("ALSA timer interface");
53 MODULE_LICENSE("GPL");
54 module_param(timer_limit, int, 0444);
55 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
57 struct snd_timer_user {
58 struct snd_timer_instance *timeri;
59 int tread; /* enhanced read with timestamps and events */
61 unsigned long overrun;
66 struct snd_timer_read *queue;
67 struct snd_timer_tread *tqueue;
69 unsigned long last_resolution;
71 struct timespec tstamp; /* trigger tstamp */
72 wait_queue_head_t qchange_sleep;
73 struct fasync_struct *fasync;
74 struct mutex tread_sem;
78 static LIST_HEAD(snd_timer_list);
80 /* list of slave instances */
81 static LIST_HEAD(snd_timer_slave_list);
83 /* lock for slave active lists */
84 static DEFINE_SPINLOCK(slave_active_lock);
86 static DEFINE_MUTEX(register_mutex);
88 static int snd_timer_free(struct snd_timer *timer);
89 static int snd_timer_dev_free(struct snd_device *device);
90 static int snd_timer_dev_register(struct snd_device *device);
91 static int snd_timer_dev_unregister(struct snd_device *device);
93 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
96 * create a timer instance with the given owner string.
97 * when timer is not NULL, increments the module counter
99 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
100 struct snd_timer *timer)
102 struct snd_timer_instance *timeri;
103 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
106 timeri->owner = kstrdup(owner, GFP_KERNEL);
107 if (! timeri->owner) {
111 INIT_LIST_HEAD(&timeri->open_list);
112 INIT_LIST_HEAD(&timeri->active_list);
113 INIT_LIST_HEAD(&timeri->ack_list);
114 INIT_LIST_HEAD(&timeri->slave_list_head);
115 INIT_LIST_HEAD(&timeri->slave_active_head);
117 timeri->timer = timer;
118 if (timer && !try_module_get(timer->module)) {
119 kfree(timeri->owner);
128 * find a timer instance from the given timer id
130 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
132 struct snd_timer *timer = NULL;
135 list_for_each(p, &snd_timer_list) {
136 timer = list_entry(p, struct snd_timer, 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)
156 static void snd_timer_request(struct snd_timer_id *tid)
158 if (! current->fs->root)
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;
187 struct list_head *p, *q;
189 /* FIXME: it's really dumb to look up all entries.. */
190 list_for_each(p, &snd_timer_list) {
191 timer = list_entry(p, struct snd_timer, device_list);
192 list_for_each(q, &timer->open_list_head) {
193 master = list_entry(q, struct snd_timer_instance, open_list);
194 if (slave->slave_class == master->slave_class &&
195 slave->slave_id == master->slave_id) {
196 list_del(&slave->open_list);
197 list_add_tail(&slave->open_list,
198 &master->slave_list_head);
199 spin_lock_irq(&slave_active_lock);
200 slave->master = master;
201 slave->timer = master->timer;
202 spin_unlock_irq(&slave_active_lock);
210 * look for slave instances matching with the slave id of the given master.
211 * when found, relink the open_link of slaves.
213 * call this with register_mutex down.
215 static void snd_timer_check_master(struct snd_timer_instance *master)
217 struct snd_timer_instance *slave;
218 struct list_head *p, *n;
220 /* check all pending slaves */
221 list_for_each_safe(p, n, &snd_timer_slave_list) {
222 slave = list_entry(p, struct snd_timer_instance, open_list);
223 if (slave->slave_class == master->slave_class &&
224 slave->slave_id == master->slave_id) {
226 list_add_tail(p, &master->slave_list_head);
227 spin_lock_irq(&slave_active_lock);
228 slave->master = master;
229 slave->timer = master->timer;
230 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
231 list_add_tail(&slave->active_list,
232 &master->slave_active_head);
233 spin_unlock_irq(&slave_active_lock);
239 * open a timer instance
240 * when opening a master, the slave id must be here given.
242 int snd_timer_open(struct snd_timer_instance **ti,
243 char *owner, struct snd_timer_id *tid,
244 unsigned int slave_id)
246 struct snd_timer *timer;
247 struct snd_timer_instance *timeri = NULL;
249 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
250 /* open a slave instance */
251 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
252 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
253 snd_printd("invalid slave class %i\n", tid->dev_sclass);
256 mutex_lock(®ister_mutex);
257 timeri = snd_timer_instance_new(owner, NULL);
259 mutex_unlock(®ister_mutex);
262 timeri->slave_class = tid->dev_sclass;
263 timeri->slave_id = tid->device;
264 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
265 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
266 snd_timer_check_slave(timeri);
267 mutex_unlock(®ister_mutex);
272 /* open a master instance */
273 mutex_lock(®ister_mutex);
274 timer = snd_timer_find(tid);
277 mutex_unlock(®ister_mutex);
278 snd_timer_request(tid);
279 mutex_lock(®ister_mutex);
280 timer = snd_timer_find(tid);
284 mutex_unlock(®ister_mutex);
287 if (!list_empty(&timer->open_list_head)) {
288 timeri = list_entry(timer->open_list_head.next,
289 struct snd_timer_instance, open_list);
290 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
291 mutex_unlock(®ister_mutex);
295 timeri = snd_timer_instance_new(owner, timer);
297 mutex_unlock(®ister_mutex);
300 timeri->slave_class = tid->dev_sclass;
301 timeri->slave_id = slave_id;
302 if (list_empty(&timer->open_list_head) && timer->hw.open)
303 timer->hw.open(timer);
304 list_add_tail(&timeri->open_list, &timer->open_list_head);
305 snd_timer_check_master(timeri);
306 mutex_unlock(®ister_mutex);
311 static int _snd_timer_stop(struct snd_timer_instance *timeri,
312 int keep_flag, int event);
315 * close a timer instance
317 int snd_timer_close(struct snd_timer_instance *timeri)
319 struct snd_timer *timer = NULL;
320 struct list_head *p, *n;
321 struct snd_timer_instance *slave;
323 snd_assert(timeri != NULL, return -ENXIO);
325 /* force to stop the timer */
326 snd_timer_stop(timeri);
328 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
329 /* wait, until the active callback is finished */
330 spin_lock_irq(&slave_active_lock);
331 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
332 spin_unlock_irq(&slave_active_lock);
334 spin_lock_irq(&slave_active_lock);
336 spin_unlock_irq(&slave_active_lock);
337 mutex_lock(®ister_mutex);
338 list_del(&timeri->open_list);
339 mutex_unlock(®ister_mutex);
341 timer = timeri->timer;
342 /* wait, until the active callback is finished */
343 spin_lock_irq(&timer->lock);
344 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
345 spin_unlock_irq(&timer->lock);
347 spin_lock_irq(&timer->lock);
349 spin_unlock_irq(&timer->lock);
350 mutex_lock(®ister_mutex);
351 list_del(&timeri->open_list);
352 if (timer && list_empty(&timer->open_list_head) &&
354 timer->hw.close(timer);
355 /* remove slave links */
356 list_for_each_safe(p, n, &timeri->slave_list_head) {
357 slave = list_entry(p, struct snd_timer_instance, open_list);
358 spin_lock_irq(&slave_active_lock);
359 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
361 list_add_tail(p, &snd_timer_slave_list);
362 slave->master = NULL;
364 spin_unlock_irq(&slave_active_lock);
366 mutex_unlock(®ister_mutex);
368 if (timeri->private_free)
369 timeri->private_free(timeri);
370 kfree(timeri->owner);
373 module_put(timer->module);
377 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
379 struct snd_timer * timer;
383 if ((timer = timeri->timer) != NULL) {
384 if (timer->hw.c_resolution)
385 return timer->hw.c_resolution(timer);
386 return timer->hw.resolution;
391 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
393 struct snd_timer *timer;
395 unsigned long resolution = 0;
396 struct snd_timer_instance *ts;
398 struct timespec tstamp;
400 getnstimeofday(&tstamp);
401 snd_assert(event >= SNDRV_TIMER_EVENT_START &&
402 event <= SNDRV_TIMER_EVENT_PAUSE, return);
403 if (event == SNDRV_TIMER_EVENT_START ||
404 event == SNDRV_TIMER_EVENT_CONTINUE)
405 resolution = snd_timer_resolution(ti);
407 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
408 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
413 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
415 spin_lock_irqsave(&timer->lock, flags);
416 list_for_each(n, &ti->slave_active_head) {
417 ts = list_entry(n, struct snd_timer_instance, active_list);
419 ts->ccallback(ti, event + 100, &tstamp, resolution);
421 spin_unlock_irqrestore(&timer->lock, flags);
424 static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
425 unsigned long sticks)
427 list_del(&timeri->active_list);
428 list_add_tail(&timeri->active_list, &timer->active_list_head);
429 if (timer->running) {
430 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
432 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
433 timeri->flags |= SNDRV_TIMER_IFLG_START;
434 return 1; /* delayed start */
436 timer->sticks = sticks;
437 timer->hw.start(timer);
440 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
445 static int snd_timer_start_slave(struct snd_timer_instance *timeri)
449 spin_lock_irqsave(&slave_active_lock, flags);
450 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
452 list_add_tail(&timeri->active_list,
453 &timeri->master->slave_active_head);
454 spin_unlock_irqrestore(&slave_active_lock, flags);
455 return 1; /* delayed start */
459 * start the timer instance
461 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
463 struct snd_timer *timer;
464 int result = -EINVAL;
467 if (timeri == NULL || ticks < 1)
469 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
470 result = snd_timer_start_slave(timeri);
471 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
474 timer = timeri->timer;
477 spin_lock_irqsave(&timer->lock, flags);
478 timeri->ticks = timeri->cticks = ticks;
480 result = snd_timer_start1(timer, timeri, ticks);
481 spin_unlock_irqrestore(&timer->lock, flags);
482 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
486 static int _snd_timer_stop(struct snd_timer_instance * timeri,
487 int keep_flag, int event)
489 struct snd_timer *timer;
492 snd_assert(timeri != NULL, return -ENXIO);
494 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
496 spin_lock_irqsave(&slave_active_lock, flags);
497 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
498 spin_unlock_irqrestore(&slave_active_lock, flags);
502 timer = timeri->timer;
505 spin_lock_irqsave(&timer->lock, flags);
506 list_del_init(&timeri->ack_list);
507 list_del_init(&timeri->active_list);
508 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
509 !(--timer->running)) {
510 timer->hw.stop(timer);
511 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
512 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
513 snd_timer_reschedule(timer, 0);
514 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
515 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
516 timer->hw.start(timer);
522 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
523 spin_unlock_irqrestore(&timer->lock, flags);
525 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
526 snd_timer_notify1(timeri, event);
531 * stop the timer instance.
533 * do not call this from the timer callback!
535 int snd_timer_stop(struct snd_timer_instance *timeri)
537 struct snd_timer *timer;
541 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
544 timer = timeri->timer;
545 spin_lock_irqsave(&timer->lock, flags);
546 timeri->cticks = timeri->ticks;
548 spin_unlock_irqrestore(&timer->lock, flags);
553 * start again.. the tick is kept.
555 int snd_timer_continue(struct snd_timer_instance *timeri)
557 struct snd_timer *timer;
558 int result = -EINVAL;
563 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
564 return snd_timer_start_slave(timeri);
565 timer = timeri->timer;
568 spin_lock_irqsave(&timer->lock, flags);
572 result = snd_timer_start1(timer, timeri, timer->sticks);
573 spin_unlock_irqrestore(&timer->lock, flags);
574 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
579 * pause.. remember the ticks left
581 int snd_timer_pause(struct snd_timer_instance * timeri)
583 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
587 * reschedule the timer
589 * start pending instances and check the scheduling ticks.
590 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
592 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
594 struct snd_timer_instance *ti;
595 unsigned long ticks = ~0UL;
598 list_for_each(p, &timer->active_list_head) {
599 ti = list_entry(p, struct snd_timer_instance, active_list);
600 if (ti->flags & SNDRV_TIMER_IFLG_START) {
601 ti->flags &= ~SNDRV_TIMER_IFLG_START;
602 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
605 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
606 if (ticks > ti->cticks)
611 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
614 if (ticks > timer->hw.ticks)
615 ticks = timer->hw.ticks;
616 if (ticks_left != ticks)
617 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
618 timer->sticks = ticks;
625 static void snd_timer_tasklet(unsigned long arg)
627 struct snd_timer *timer = (struct snd_timer *) arg;
628 struct snd_timer_instance *ti;
630 unsigned long resolution, ticks;
632 spin_lock(&timer->lock);
633 /* now process all callbacks */
634 while (!list_empty(&timer->sack_list_head)) {
635 p = timer->sack_list_head.next; /* get first item */
636 ti = list_entry(p, struct snd_timer_instance, ack_list);
638 /* remove from ack_list and make empty */
643 resolution = ti->resolution;
645 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
646 spin_unlock(&timer->lock);
648 ti->callback(ti, resolution, ticks);
649 spin_lock(&timer->lock);
650 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
652 spin_unlock(&timer->lock);
658 * ticks_left is usually equal to timer->sticks.
661 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
663 struct snd_timer_instance *ti, *ts;
664 unsigned long resolution, ticks;
665 struct list_head *p, *q, *n, *ack_list_head;
672 spin_lock_irqsave(&timer->lock, flags);
674 /* remember the current resolution */
675 if (timer->hw.c_resolution)
676 resolution = timer->hw.c_resolution(timer);
678 resolution = timer->hw.resolution;
680 /* loop for all active instances
681 * Here we cannot use list_for_each because the active_list of a
682 * processed instance is relinked to done_list_head before the callback
685 list_for_each_safe(p, n, &timer->active_list_head) {
686 ti = list_entry(p, struct snd_timer_instance, active_list);
687 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
689 ti->pticks += ticks_left;
690 ti->resolution = resolution;
691 if (ti->cticks < ticks_left)
694 ti->cticks -= ticks_left;
695 if (ti->cticks) /* not expired */
697 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
698 ti->cticks = ti->ticks;
700 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
701 if (--timer->running)
704 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
705 (ti->flags & SNDRV_TIMER_IFLG_FAST))
706 ack_list_head = &timer->ack_list_head;
708 ack_list_head = &timer->sack_list_head;
709 if (list_empty(&ti->ack_list))
710 list_add_tail(&ti->ack_list, ack_list_head);
711 list_for_each(q, &ti->slave_active_head) {
712 ts = list_entry(q, struct snd_timer_instance, active_list);
713 ts->pticks = ti->pticks;
714 ts->resolution = resolution;
715 if (list_empty(&ts->ack_list))
716 list_add_tail(&ts->ack_list, ack_list_head);
719 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
720 snd_timer_reschedule(timer, ticks_left);
721 if (timer->running) {
722 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
723 timer->hw.stop(timer);
724 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
726 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
727 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
729 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
730 timer->hw.start(timer);
733 timer->hw.stop(timer);
736 /* now process all fast callbacks */
737 while (!list_empty(&timer->ack_list_head)) {
738 p = timer->ack_list_head.next; /* get first item */
739 ti = list_entry(p, struct snd_timer_instance, ack_list);
741 /* remove from ack_list and make empty */
747 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
748 spin_unlock(&timer->lock);
750 ti->callback(ti, resolution, ticks);
751 spin_lock(&timer->lock);
752 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
755 /* do we have any slow callbacks? */
756 use_tasklet = !list_empty(&timer->sack_list_head);
757 spin_unlock_irqrestore(&timer->lock, flags);
760 tasklet_hi_schedule(&timer->task_queue);
767 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
768 struct snd_timer **rtimer)
770 struct snd_timer *timer;
772 static struct snd_device_ops ops = {
773 .dev_free = snd_timer_dev_free,
774 .dev_register = snd_timer_dev_register,
775 .dev_unregister = snd_timer_dev_unregister
778 snd_assert(tid != NULL, return -EINVAL);
779 snd_assert(rtimer != NULL, return -EINVAL);
781 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
783 snd_printk(KERN_ERR "timer: cannot allocate\n");
786 timer->tmr_class = tid->dev_class;
788 timer->tmr_device = tid->device;
789 timer->tmr_subdevice = tid->subdevice;
791 strlcpy(timer->id, id, sizeof(timer->id));
792 INIT_LIST_HEAD(&timer->device_list);
793 INIT_LIST_HEAD(&timer->open_list_head);
794 INIT_LIST_HEAD(&timer->active_list_head);
795 INIT_LIST_HEAD(&timer->ack_list_head);
796 INIT_LIST_HEAD(&timer->sack_list_head);
797 spin_lock_init(&timer->lock);
798 tasklet_init(&timer->task_queue, snd_timer_tasklet,
799 (unsigned long)timer);
801 timer->module = card->module;
802 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
804 snd_timer_free(timer);
812 static int snd_timer_free(struct snd_timer *timer)
814 snd_assert(timer != NULL, return -ENXIO);
815 if (timer->private_free)
816 timer->private_free(timer);
821 static int snd_timer_dev_free(struct snd_device *device)
823 struct snd_timer *timer = device->device_data;
824 return snd_timer_free(timer);
827 static int snd_timer_dev_register(struct snd_device *dev)
829 struct snd_timer *timer = dev->device_data;
830 struct snd_timer *timer1;
833 snd_assert(timer != NULL && timer->hw.start != NULL &&
834 timer->hw.stop != NULL, return -ENXIO);
835 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
836 !timer->hw.resolution && timer->hw.c_resolution == NULL)
839 mutex_lock(®ister_mutex);
840 list_for_each(p, &snd_timer_list) {
841 timer1 = list_entry(p, struct snd_timer, device_list);
842 if (timer1->tmr_class > timer->tmr_class)
844 if (timer1->tmr_class < timer->tmr_class)
846 if (timer1->card && timer->card) {
847 if (timer1->card->number > timer->card->number)
849 if (timer1->card->number < timer->card->number)
852 if (timer1->tmr_device > timer->tmr_device)
854 if (timer1->tmr_device < timer->tmr_device)
856 if (timer1->tmr_subdevice > timer->tmr_subdevice)
858 if (timer1->tmr_subdevice < timer->tmr_subdevice)
861 mutex_unlock(®ister_mutex);
864 list_add_tail(&timer->device_list, p);
865 mutex_unlock(®ister_mutex);
869 static int snd_timer_unregister(struct snd_timer *timer)
871 struct list_head *p, *n;
872 struct snd_timer_instance *ti;
874 snd_assert(timer != NULL, return -ENXIO);
875 mutex_lock(®ister_mutex);
876 if (! list_empty(&timer->open_list_head)) {
877 snd_printk(KERN_WARNING "timer 0x%lx is busy?\n", (long)timer);
878 list_for_each_safe(p, n, &timer->open_list_head) {
880 ti = list_entry(p, struct snd_timer_instance, open_list);
884 list_del(&timer->device_list);
885 mutex_unlock(®ister_mutex);
886 return snd_timer_free(timer);
889 static int snd_timer_dev_unregister(struct snd_device *device)
891 struct snd_timer *timer = device->device_data;
892 return snd_timer_unregister(timer);
895 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
898 unsigned long resolution = 0;
899 struct snd_timer_instance *ti, *ts;
900 struct list_head *p, *n;
902 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
904 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
905 event <= SNDRV_TIMER_EVENT_MRESUME, return);
906 spin_lock_irqsave(&timer->lock, flags);
907 if (event == SNDRV_TIMER_EVENT_MSTART ||
908 event == SNDRV_TIMER_EVENT_MCONTINUE ||
909 event == SNDRV_TIMER_EVENT_MRESUME) {
910 if (timer->hw.c_resolution)
911 resolution = timer->hw.c_resolution(timer);
913 resolution = timer->hw.resolution;
915 list_for_each(p, &timer->active_list_head) {
916 ti = list_entry(p, struct snd_timer_instance, active_list);
918 ti->ccallback(ti, event, tstamp, resolution);
919 list_for_each(n, &ti->slave_active_head) {
920 ts = list_entry(n, struct snd_timer_instance, active_list);
922 ts->ccallback(ts, event, tstamp, resolution);
925 spin_unlock_irqrestore(&timer->lock, flags);
929 * exported functions for global timers
931 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
933 struct snd_timer_id tid;
935 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
936 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
940 return snd_timer_new(NULL, id, &tid, rtimer);
943 int snd_timer_global_free(struct snd_timer *timer)
945 return snd_timer_free(timer);
948 int snd_timer_global_register(struct snd_timer *timer)
950 struct snd_device dev;
952 memset(&dev, 0, sizeof(dev));
953 dev.device_data = timer;
954 return snd_timer_dev_register(&dev);
957 int snd_timer_global_unregister(struct snd_timer *timer)
959 return snd_timer_unregister(timer);
966 struct snd_timer_system_private {
967 struct timer_list tlist;
968 struct timer * timer;
969 unsigned long last_expires;
970 unsigned long last_jiffies;
971 unsigned long correction;
974 static void snd_timer_s_function(unsigned long data)
976 struct snd_timer *timer = (struct snd_timer *)data;
977 struct snd_timer_system_private *priv = timer->private_data;
978 unsigned long jiff = jiffies;
979 if (time_after(jiff, priv->last_expires))
980 priv->correction = (long)jiff - (long)priv->last_expires;
981 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
984 static int snd_timer_s_start(struct snd_timer * timer)
986 struct snd_timer_system_private *priv;
989 priv = (struct snd_timer_system_private *) timer->private_data;
990 njiff = (priv->last_jiffies = jiffies);
991 if (priv->correction > timer->sticks - 1) {
992 priv->correction -= timer->sticks - 1;
995 njiff += timer->sticks - priv->correction;
996 priv->correction -= timer->sticks;
998 priv->last_expires = priv->tlist.expires = njiff;
999 add_timer(&priv->tlist);
1003 static int snd_timer_s_stop(struct snd_timer * timer)
1005 struct snd_timer_system_private *priv;
1008 priv = (struct snd_timer_system_private *) timer->private_data;
1009 del_timer(&priv->tlist);
1011 if (time_before(jiff, priv->last_expires))
1012 timer->sticks = priv->last_expires - jiff;
1018 static struct snd_timer_hardware snd_timer_system =
1020 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1021 .resolution = 1000000000L / HZ,
1023 .start = snd_timer_s_start,
1024 .stop = snd_timer_s_stop
1027 static void snd_timer_free_system(struct snd_timer *timer)
1029 kfree(timer->private_data);
1032 static int snd_timer_register_system(void)
1034 struct snd_timer *timer;
1035 struct snd_timer_system_private *priv;
1038 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1041 strcpy(timer->name, "system timer");
1042 timer->hw = snd_timer_system;
1043 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1045 snd_timer_free(timer);
1048 init_timer(&priv->tlist);
1049 priv->tlist.function = snd_timer_s_function;
1050 priv->tlist.data = (unsigned long) timer;
1051 timer->private_data = priv;
1052 timer->private_free = snd_timer_free_system;
1053 return snd_timer_global_register(timer);
1056 #ifdef CONFIG_PROC_FS
1061 static void snd_timer_proc_read(struct snd_info_entry *entry,
1062 struct snd_info_buffer *buffer)
1064 unsigned long flags;
1065 struct snd_timer *timer;
1066 struct snd_timer_instance *ti;
1067 struct list_head *p, *q;
1069 mutex_lock(®ister_mutex);
1070 list_for_each(p, &snd_timer_list) {
1071 timer = list_entry(p, struct snd_timer, device_list);
1072 switch (timer->tmr_class) {
1073 case SNDRV_TIMER_CLASS_GLOBAL:
1074 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1076 case SNDRV_TIMER_CLASS_CARD:
1077 snd_iprintf(buffer, "C%i-%i: ",
1078 timer->card->number, timer->tmr_device);
1080 case SNDRV_TIMER_CLASS_PCM:
1081 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1082 timer->tmr_device, timer->tmr_subdevice);
1085 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1086 timer->card ? timer->card->number : -1,
1087 timer->tmr_device, timer->tmr_subdevice);
1089 snd_iprintf(buffer, "%s :", timer->name);
1090 if (timer->hw.resolution)
1091 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1092 timer->hw.resolution / 1000,
1093 timer->hw.resolution % 1000,
1095 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1096 snd_iprintf(buffer, " SLAVE");
1097 snd_iprintf(buffer, "\n");
1098 spin_lock_irqsave(&timer->lock, flags);
1099 list_for_each(q, &timer->open_list_head) {
1100 ti = list_entry(q, struct snd_timer_instance, open_list);
1101 snd_iprintf(buffer, " Client %s : %s\n",
1102 ti->owner ? ti->owner : "unknown",
1103 ti->flags & (SNDRV_TIMER_IFLG_START |
1104 SNDRV_TIMER_IFLG_RUNNING)
1105 ? "running" : "stopped");
1107 spin_unlock_irqrestore(&timer->lock, flags);
1109 mutex_unlock(®ister_mutex);
1112 static struct snd_info_entry *snd_timer_proc_entry = NULL;
1114 static void __init snd_timer_proc_init(void)
1116 struct snd_info_entry *entry;
1118 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1119 if (entry != NULL) {
1120 entry->c.text.read = snd_timer_proc_read;
1121 if (snd_info_register(entry) < 0) {
1122 snd_info_free_entry(entry);
1126 snd_timer_proc_entry = entry;
1129 static void __exit snd_timer_proc_done(void)
1131 snd_info_unregister(snd_timer_proc_entry);
1133 #else /* !CONFIG_PROC_FS */
1134 #define snd_timer_proc_init()
1135 #define snd_timer_proc_done()
1139 * USER SPACE interface
1142 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1143 unsigned long resolution,
1144 unsigned long ticks)
1146 struct snd_timer_user *tu = timeri->callback_data;
1147 struct snd_timer_read *r;
1150 spin_lock(&tu->qlock);
1151 if (tu->qused > 0) {
1152 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1153 r = &tu->queue[prev];
1154 if (r->resolution == resolution) {
1159 if (tu->qused >= tu->queue_size) {
1162 r = &tu->queue[tu->qtail++];
1163 tu->qtail %= tu->queue_size;
1164 r->resolution = resolution;
1169 spin_unlock(&tu->qlock);
1170 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1171 wake_up(&tu->qchange_sleep);
1174 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1175 struct snd_timer_tread *tread)
1177 if (tu->qused >= tu->queue_size) {
1180 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1181 tu->qtail %= tu->queue_size;
1186 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1188 struct timespec *tstamp,
1189 unsigned long resolution)
1191 struct snd_timer_user *tu = timeri->callback_data;
1192 struct snd_timer_tread r1;
1194 if (event >= SNDRV_TIMER_EVENT_START &&
1195 event <= SNDRV_TIMER_EVENT_PAUSE)
1196 tu->tstamp = *tstamp;
1197 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1200 r1.tstamp = *tstamp;
1201 r1.val = resolution;
1202 spin_lock(&tu->qlock);
1203 snd_timer_user_append_to_tqueue(tu, &r1);
1204 spin_unlock(&tu->qlock);
1205 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1206 wake_up(&tu->qchange_sleep);
1209 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1210 unsigned long resolution,
1211 unsigned long ticks)
1213 struct snd_timer_user *tu = timeri->callback_data;
1214 struct snd_timer_tread *r, r1;
1215 struct timespec tstamp;
1216 int prev, append = 0;
1218 memset(&tstamp, 0, sizeof(tstamp));
1219 spin_lock(&tu->qlock);
1220 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1221 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1222 spin_unlock(&tu->qlock);
1225 if (tu->last_resolution != resolution || ticks > 0)
1226 getnstimeofday(&tstamp);
1227 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1228 tu->last_resolution != resolution) {
1229 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1231 r1.val = resolution;
1232 snd_timer_user_append_to_tqueue(tu, &r1);
1233 tu->last_resolution = resolution;
1236 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1240 if (tu->qused > 0) {
1241 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1242 r = &tu->tqueue[prev];
1243 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1250 r1.event = SNDRV_TIMER_EVENT_TICK;
1253 snd_timer_user_append_to_tqueue(tu, &r1);
1256 spin_unlock(&tu->qlock);
1259 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1260 wake_up(&tu->qchange_sleep);
1263 static int snd_timer_user_open(struct inode *inode, struct file *file)
1265 struct snd_timer_user *tu;
1267 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1270 spin_lock_init(&tu->qlock);
1271 init_waitqueue_head(&tu->qchange_sleep);
1272 mutex_init(&tu->tread_sem);
1274 tu->queue_size = 128;
1275 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1277 if (tu->queue == NULL) {
1281 file->private_data = tu;
1285 static int snd_timer_user_release(struct inode *inode, struct file *file)
1287 struct snd_timer_user *tu;
1289 if (file->private_data) {
1290 tu = file->private_data;
1291 file->private_data = NULL;
1292 fasync_helper(-1, file, 0, &tu->fasync);
1294 snd_timer_close(tu->timeri);
1302 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1304 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1305 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1311 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1313 id->dev_class = timer->tmr_class;
1314 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1315 id->card = timer->card ? timer->card->number : -1;
1316 id->device = timer->tmr_device;
1317 id->subdevice = timer->tmr_subdevice;
1320 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1322 struct snd_timer_id id;
1323 struct snd_timer *timer;
1324 struct list_head *p;
1326 if (copy_from_user(&id, _tid, sizeof(id)))
1328 mutex_lock(®ister_mutex);
1329 if (id.dev_class < 0) { /* first item */
1330 if (list_empty(&snd_timer_list))
1331 snd_timer_user_zero_id(&id);
1333 timer = list_entry(snd_timer_list.next,
1334 struct snd_timer, device_list);
1335 snd_timer_user_copy_id(&id, timer);
1338 switch (id.dev_class) {
1339 case SNDRV_TIMER_CLASS_GLOBAL:
1340 id.device = id.device < 0 ? 0 : id.device + 1;
1341 list_for_each(p, &snd_timer_list) {
1342 timer = list_entry(p, struct snd_timer, device_list);
1343 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1344 snd_timer_user_copy_id(&id, timer);
1347 if (timer->tmr_device >= id.device) {
1348 snd_timer_user_copy_id(&id, timer);
1352 if (p == &snd_timer_list)
1353 snd_timer_user_zero_id(&id);
1355 case SNDRV_TIMER_CLASS_CARD:
1356 case SNDRV_TIMER_CLASS_PCM:
1363 if (id.device < 0) {
1366 if (id.subdevice < 0) {
1374 list_for_each(p, &snd_timer_list) {
1375 timer = list_entry(p, struct snd_timer, device_list);
1376 if (timer->tmr_class > id.dev_class) {
1377 snd_timer_user_copy_id(&id, timer);
1380 if (timer->tmr_class < id.dev_class)
1382 if (timer->card->number > id.card) {
1383 snd_timer_user_copy_id(&id, timer);
1386 if (timer->card->number < id.card)
1388 if (timer->tmr_device > id.device) {
1389 snd_timer_user_copy_id(&id, timer);
1392 if (timer->tmr_device < id.device)
1394 if (timer->tmr_subdevice > id.subdevice) {
1395 snd_timer_user_copy_id(&id, timer);
1398 if (timer->tmr_subdevice < id.subdevice)
1400 snd_timer_user_copy_id(&id, timer);
1403 if (p == &snd_timer_list)
1404 snd_timer_user_zero_id(&id);
1407 snd_timer_user_zero_id(&id);
1410 mutex_unlock(®ister_mutex);
1411 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1416 static int snd_timer_user_ginfo(struct file *file,
1417 struct snd_timer_ginfo __user *_ginfo)
1419 struct snd_timer_ginfo *ginfo;
1420 struct snd_timer_id tid;
1421 struct snd_timer *t;
1422 struct list_head *p;
1425 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1428 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1433 memset(ginfo, 0, sizeof(*ginfo));
1435 mutex_lock(®ister_mutex);
1436 t = snd_timer_find(&tid);
1438 ginfo->card = t->card ? t->card->number : -1;
1439 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1440 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1441 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1442 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1443 ginfo->resolution = t->hw.resolution;
1444 if (t->hw.resolution_min > 0) {
1445 ginfo->resolution_min = t->hw.resolution_min;
1446 ginfo->resolution_max = t->hw.resolution_max;
1448 list_for_each(p, &t->open_list_head) {
1454 mutex_unlock(®ister_mutex);
1455 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1461 static int snd_timer_user_gparams(struct file *file,
1462 struct snd_timer_gparams __user *_gparams)
1464 struct snd_timer_gparams gparams;
1465 struct snd_timer *t;
1468 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1470 mutex_lock(®ister_mutex);
1471 t = snd_timer_find(&gparams.tid);
1476 if (!list_empty(&t->open_list_head)) {
1480 if (!t->hw.set_period) {
1484 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1486 mutex_unlock(®ister_mutex);
1490 static int snd_timer_user_gstatus(struct file *file,
1491 struct snd_timer_gstatus __user *_gstatus)
1493 struct snd_timer_gstatus gstatus;
1494 struct snd_timer_id tid;
1495 struct snd_timer *t;
1498 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1501 memset(&gstatus, 0, sizeof(gstatus));
1503 mutex_lock(®ister_mutex);
1504 t = snd_timer_find(&tid);
1506 if (t->hw.c_resolution)
1507 gstatus.resolution = t->hw.c_resolution(t);
1509 gstatus.resolution = t->hw.resolution;
1510 if (t->hw.precise_resolution) {
1511 t->hw.precise_resolution(t, &gstatus.resolution_num,
1512 &gstatus.resolution_den);
1514 gstatus.resolution_num = gstatus.resolution;
1515 gstatus.resolution_den = 1000000000uL;
1520 mutex_unlock(®ister_mutex);
1521 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1526 static int snd_timer_user_tselect(struct file *file,
1527 struct snd_timer_select __user *_tselect)
1529 struct snd_timer_user *tu;
1530 struct snd_timer_select tselect;
1534 tu = file->private_data;
1535 mutex_lock(&tu->tread_sem);
1537 snd_timer_close(tu->timeri);
1540 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1544 sprintf(str, "application %i", current->pid);
1545 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1546 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1547 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1556 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1558 if (tu->tqueue == NULL)
1561 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1563 if (tu->queue == NULL)
1568 snd_timer_close(tu->timeri);
1571 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1572 tu->timeri->callback = tu->tread
1573 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1574 tu->timeri->ccallback = snd_timer_user_ccallback;
1575 tu->timeri->callback_data = (void *)tu;
1579 mutex_unlock(&tu->tread_sem);
1583 static int snd_timer_user_info(struct file *file,
1584 struct snd_timer_info __user *_info)
1586 struct snd_timer_user *tu;
1587 struct snd_timer_info *info;
1588 struct snd_timer *t;
1591 tu = file->private_data;
1592 snd_assert(tu->timeri != NULL, return -ENXIO);
1593 t = tu->timeri->timer;
1594 snd_assert(t != NULL, return -ENXIO);
1596 info = kzalloc(sizeof(*info), GFP_KERNEL);
1599 info->card = t->card ? t->card->number : -1;
1600 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1601 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1602 strlcpy(info->id, t->id, sizeof(info->id));
1603 strlcpy(info->name, t->name, sizeof(info->name));
1604 info->resolution = t->hw.resolution;
1605 if (copy_to_user(_info, info, sizeof(*_info)))
1611 static int snd_timer_user_params(struct file *file,
1612 struct snd_timer_params __user *_params)
1614 struct snd_timer_user *tu;
1615 struct snd_timer_params params;
1616 struct snd_timer *t;
1617 struct snd_timer_read *tr;
1618 struct snd_timer_tread *ttr;
1621 tu = file->private_data;
1622 snd_assert(tu->timeri != NULL, return -ENXIO);
1623 t = tu->timeri->timer;
1624 snd_assert(t != NULL, return -ENXIO);
1625 if (copy_from_user(¶ms, _params, sizeof(params)))
1627 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1631 if (params.queue_size > 0 &&
1632 (params.queue_size < 32 || params.queue_size > 1024)) {
1636 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1637 (1<<SNDRV_TIMER_EVENT_TICK)|
1638 (1<<SNDRV_TIMER_EVENT_START)|
1639 (1<<SNDRV_TIMER_EVENT_STOP)|
1640 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1641 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1642 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1643 (1<<SNDRV_TIMER_EVENT_RESUME)|
1644 (1<<SNDRV_TIMER_EVENT_MSTART)|
1645 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1646 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1647 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1648 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1649 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1653 snd_timer_stop(tu->timeri);
1654 spin_lock_irq(&t->lock);
1655 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1656 SNDRV_TIMER_IFLG_EXCLUSIVE|
1657 SNDRV_TIMER_IFLG_EARLY_EVENT);
1658 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1659 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1660 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1661 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1662 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1663 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1664 spin_unlock_irq(&t->lock);
1665 if (params.queue_size > 0 &&
1666 (unsigned int)tu->queue_size != params.queue_size) {
1668 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1672 tu->queue_size = params.queue_size;
1676 tr = kmalloc(params.queue_size * sizeof(*tr),
1680 tu->queue_size = params.queue_size;
1685 tu->qhead = tu->qtail = tu->qused = 0;
1686 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1688 struct snd_timer_tread tread;
1689 tread.event = SNDRV_TIMER_EVENT_EARLY;
1690 tread.tstamp.tv_sec = 0;
1691 tread.tstamp.tv_nsec = 0;
1693 snd_timer_user_append_to_tqueue(tu, &tread);
1695 struct snd_timer_read *r = &tu->queue[0];
1702 tu->filter = params.filter;
1703 tu->ticks = params.ticks;
1706 if (copy_to_user(_params, ¶ms, sizeof(params)))
1711 static int snd_timer_user_status(struct file *file,
1712 struct snd_timer_status __user *_status)
1714 struct snd_timer_user *tu;
1715 struct snd_timer_status status;
1717 tu = file->private_data;
1718 snd_assert(tu->timeri != NULL, return -ENXIO);
1719 memset(&status, 0, sizeof(status));
1720 status.tstamp = tu->tstamp;
1721 status.resolution = snd_timer_resolution(tu->timeri);
1722 status.lost = tu->timeri->lost;
1723 status.overrun = tu->overrun;
1724 spin_lock_irq(&tu->qlock);
1725 status.queue = tu->qused;
1726 spin_unlock_irq(&tu->qlock);
1727 if (copy_to_user(_status, &status, sizeof(status)))
1732 static int snd_timer_user_start(struct file *file)
1735 struct snd_timer_user *tu;
1737 tu = file->private_data;
1738 snd_assert(tu->timeri != NULL, return -ENXIO);
1739 snd_timer_stop(tu->timeri);
1740 tu->timeri->lost = 0;
1741 tu->last_resolution = 0;
1742 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1745 static int snd_timer_user_stop(struct file *file)
1748 struct snd_timer_user *tu;
1750 tu = file->private_data;
1751 snd_assert(tu->timeri != NULL, return -ENXIO);
1752 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1755 static int snd_timer_user_continue(struct file *file)
1758 struct snd_timer_user *tu;
1760 tu = file->private_data;
1761 snd_assert(tu->timeri != NULL, return -ENXIO);
1762 tu->timeri->lost = 0;
1763 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1766 static int snd_timer_user_pause(struct file *file)
1769 struct snd_timer_user *tu;
1771 tu = file->private_data;
1772 snd_assert(tu->timeri != NULL, return -ENXIO);
1773 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1777 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1778 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1779 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1780 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1783 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1786 struct snd_timer_user *tu;
1787 void __user *argp = (void __user *)arg;
1788 int __user *p = argp;
1790 tu = file->private_data;
1792 case SNDRV_TIMER_IOCTL_PVERSION:
1793 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1794 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1795 return snd_timer_user_next_device(argp);
1796 case SNDRV_TIMER_IOCTL_TREAD:
1800 mutex_lock(&tu->tread_sem);
1801 if (tu->timeri) { /* too late */
1802 mutex_unlock(&tu->tread_sem);
1805 if (get_user(xarg, p)) {
1806 mutex_unlock(&tu->tread_sem);
1809 tu->tread = xarg ? 1 : 0;
1810 mutex_unlock(&tu->tread_sem);
1813 case SNDRV_TIMER_IOCTL_GINFO:
1814 return snd_timer_user_ginfo(file, argp);
1815 case SNDRV_TIMER_IOCTL_GPARAMS:
1816 return snd_timer_user_gparams(file, argp);
1817 case SNDRV_TIMER_IOCTL_GSTATUS:
1818 return snd_timer_user_gstatus(file, argp);
1819 case SNDRV_TIMER_IOCTL_SELECT:
1820 return snd_timer_user_tselect(file, argp);
1821 case SNDRV_TIMER_IOCTL_INFO:
1822 return snd_timer_user_info(file, argp);
1823 case SNDRV_TIMER_IOCTL_PARAMS:
1824 return snd_timer_user_params(file, argp);
1825 case SNDRV_TIMER_IOCTL_STATUS:
1826 return snd_timer_user_status(file, argp);
1827 case SNDRV_TIMER_IOCTL_START:
1828 case SNDRV_TIMER_IOCTL_START_OLD:
1829 return snd_timer_user_start(file);
1830 case SNDRV_TIMER_IOCTL_STOP:
1831 case SNDRV_TIMER_IOCTL_STOP_OLD:
1832 return snd_timer_user_stop(file);
1833 case SNDRV_TIMER_IOCTL_CONTINUE:
1834 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1835 return snd_timer_user_continue(file);
1836 case SNDRV_TIMER_IOCTL_PAUSE:
1837 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1838 return snd_timer_user_pause(file);
1843 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1845 struct snd_timer_user *tu;
1848 tu = file->private_data;
1849 err = fasync_helper(fd, file, on, &tu->fasync);
1855 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1856 size_t count, loff_t *offset)
1858 struct snd_timer_user *tu;
1859 long result = 0, unit;
1862 tu = file->private_data;
1863 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1864 spin_lock_irq(&tu->qlock);
1865 while ((long)count - result >= unit) {
1866 while (!tu->qused) {
1869 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1874 set_current_state(TASK_INTERRUPTIBLE);
1875 init_waitqueue_entry(&wait, current);
1876 add_wait_queue(&tu->qchange_sleep, &wait);
1878 spin_unlock_irq(&tu->qlock);
1880 spin_lock_irq(&tu->qlock);
1882 remove_wait_queue(&tu->qchange_sleep, &wait);
1884 if (signal_pending(current)) {
1890 spin_unlock_irq(&tu->qlock);
1895 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1896 sizeof(struct snd_timer_tread))) {
1901 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1902 sizeof(struct snd_timer_read))) {
1908 tu->qhead %= tu->queue_size;
1913 spin_lock_irq(&tu->qlock);
1916 spin_unlock_irq(&tu->qlock);
1918 return result > 0 ? result : err;
1921 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1924 struct snd_timer_user *tu;
1926 tu = file->private_data;
1928 poll_wait(file, &tu->qchange_sleep, wait);
1932 mask |= POLLIN | POLLRDNORM;
1937 #ifdef CONFIG_COMPAT
1938 #include "timer_compat.c"
1940 #define snd_timer_user_ioctl_compat NULL
1943 static struct file_operations snd_timer_f_ops =
1945 .owner = THIS_MODULE,
1946 .read = snd_timer_user_read,
1947 .open = snd_timer_user_open,
1948 .release = snd_timer_user_release,
1949 .poll = snd_timer_user_poll,
1950 .unlocked_ioctl = snd_timer_user_ioctl,
1951 .compat_ioctl = snd_timer_user_ioctl_compat,
1952 .fasync = snd_timer_user_fasync,
1959 static int __init alsa_timer_init(void)
1963 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1964 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1968 if ((err = snd_timer_register_system()) < 0)
1969 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1971 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1972 &snd_timer_f_ops, NULL, "timer")) < 0)
1973 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1975 snd_timer_proc_init();
1979 static void __exit alsa_timer_exit(void)
1981 struct list_head *p, *n;
1983 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1984 /* unregister the system timer */
1985 list_for_each_safe(p, n, &snd_timer_list) {
1986 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
1987 snd_timer_unregister(timer);
1989 snd_timer_proc_done();
1990 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1991 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1995 module_init(alsa_timer_init)
1996 module_exit(alsa_timer_exit)
1998 EXPORT_SYMBOL(snd_timer_open);
1999 EXPORT_SYMBOL(snd_timer_close);
2000 EXPORT_SYMBOL(snd_timer_resolution);
2001 EXPORT_SYMBOL(snd_timer_start);
2002 EXPORT_SYMBOL(snd_timer_stop);
2003 EXPORT_SYMBOL(snd_timer_continue);
2004 EXPORT_SYMBOL(snd_timer_pause);
2005 EXPORT_SYMBOL(snd_timer_new);
2006 EXPORT_SYMBOL(snd_timer_notify);
2007 EXPORT_SYMBOL(snd_timer_global_new);
2008 EXPORT_SYMBOL(snd_timer_global_free);
2009 EXPORT_SYMBOL(snd_timer_global_register);
2010 EXPORT_SYMBOL(snd_timer_global_unregister);
2011 EXPORT_SYMBOL(snd_timer_interrupt);