[PATCH] dvb: saa7134-dvb must select tda1004x
[linux-ginger.git] / sound / core / timer.c
blob4104f6e292e959ead04756ccd52d18ee52e49326
1 /*
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/moduleparam.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>
37 #ifdef CONFIG_KERNELD
38 #include <linux/kerneld.h>
39 #endif
41 #if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
42 #define DEFAULT_TIMER_LIMIT 3
43 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
44 #define DEFAULT_TIMER_LIMIT 2
45 #else
46 #define DEFAULT_TIMER_LIMIT 1
47 #endif
49 static int timer_limit = DEFAULT_TIMER_LIMIT;
50 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
51 MODULE_DESCRIPTION("ALSA timer interface");
52 MODULE_LICENSE("GPL");
53 module_param(timer_limit, int, 0444);
54 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
56 typedef struct {
57 snd_timer_instance_t *timeri;
58 int tread; /* enhanced read with timestamps and events */
59 unsigned long ticks;
60 unsigned long overrun;
61 int qhead;
62 int qtail;
63 int qused;
64 int queue_size;
65 snd_timer_read_t *queue;
66 snd_timer_tread_t *tqueue;
67 spinlock_t qlock;
68 unsigned long last_resolution;
69 unsigned int filter;
70 struct timespec tstamp; /* trigger tstamp */
71 wait_queue_head_t qchange_sleep;
72 struct fasync_struct *fasync;
73 struct semaphore tread_sem;
74 } snd_timer_user_t;
76 /* list of timers */
77 static LIST_HEAD(snd_timer_list);
79 /* list of slave instances */
80 static LIST_HEAD(snd_timer_slave_list);
82 /* lock for slave active lists */
83 static DEFINE_SPINLOCK(slave_active_lock);
85 static DECLARE_MUTEX(register_mutex);
87 static int snd_timer_free(snd_timer_t *timer);
88 static int snd_timer_dev_free(snd_device_t *device);
89 static int snd_timer_dev_register(snd_device_t *device);
90 static int snd_timer_dev_unregister(snd_device_t *device);
92 static void snd_timer_reschedule(snd_timer_t * timer, unsigned long ticks_left);
95 * create a timer instance with the given owner string.
96 * when timer is not NULL, increments the module counter
98 static snd_timer_instance_t *snd_timer_instance_new(char *owner, snd_timer_t *timer)
100 snd_timer_instance_t *timeri;
101 timeri = kcalloc(1, sizeof(*timeri), GFP_KERNEL);
102 if (timeri == NULL)
103 return NULL;
104 timeri->owner = kstrdup(owner, GFP_KERNEL);
105 if (! timeri->owner) {
106 kfree(timeri);
107 return NULL;
109 INIT_LIST_HEAD(&timeri->open_list);
110 INIT_LIST_HEAD(&timeri->active_list);
111 INIT_LIST_HEAD(&timeri->ack_list);
112 INIT_LIST_HEAD(&timeri->slave_list_head);
113 INIT_LIST_HEAD(&timeri->slave_active_head);
115 timeri->timer = timer;
116 if (timer && timer->card && !try_module_get(timer->card->module)) {
117 kfree(timeri->owner);
118 kfree(timeri);
119 return NULL;
122 return timeri;
126 * find a timer instance from the given timer id
128 static snd_timer_t *snd_timer_find(snd_timer_id_t *tid)
130 snd_timer_t *timer = NULL;
131 struct list_head *p;
133 list_for_each(p, &snd_timer_list) {
134 timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
136 if (timer->tmr_class != tid->dev_class)
137 continue;
138 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
139 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
140 (timer->card == NULL ||
141 timer->card->number != tid->card))
142 continue;
143 if (timer->tmr_device != tid->device)
144 continue;
145 if (timer->tmr_subdevice != tid->subdevice)
146 continue;
147 return timer;
149 return NULL;
152 #ifdef CONFIG_KMOD
154 static void snd_timer_request(snd_timer_id_t *tid)
156 if (! current->fs->root)
157 return;
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);
162 break;
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);
167 break;
168 default:
169 break;
173 #endif
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(snd_timer_instance_t *slave)
183 snd_timer_t *timer;
184 snd_timer_instance_t *master;
185 struct list_head *p, *q;
187 /* FIXME: it's really dumb to look up all entries.. */
188 list_for_each(p, &snd_timer_list) {
189 timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
190 list_for_each(q, &timer->open_list_head) {
191 master = (snd_timer_instance_t *)list_entry(q, snd_timer_instance_t, open_list);
192 if (slave->slave_class == master->slave_class &&
193 slave->slave_id == master->slave_id) {
194 list_del(&slave->open_list);
195 list_add_tail(&slave->open_list, &master->slave_list_head);
196 spin_lock_irq(&slave_active_lock);
197 slave->master = master;
198 slave->timer = master->timer;
199 spin_unlock_irq(&slave_active_lock);
200 return;
207 * look for slave instances matching with the slave id of the given master.
208 * when found, relink the open_link of slaves.
210 * call this with register_mutex down.
212 static void snd_timer_check_master(snd_timer_instance_t *master)
214 snd_timer_instance_t *slave;
215 struct list_head *p, *n;
217 /* check all pending slaves */
218 list_for_each_safe(p, n, &snd_timer_slave_list) {
219 slave = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, open_list);
220 if (slave->slave_class == master->slave_class &&
221 slave->slave_id == master->slave_id) {
222 list_del(p);
223 list_add_tail(p, &master->slave_list_head);
224 spin_lock_irq(&slave_active_lock);
225 slave->master = master;
226 slave->timer = master->timer;
227 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
228 list_add_tail(&slave->active_list, &master->slave_active_head);
229 spin_unlock_irq(&slave_active_lock);
235 * open a timer instance
236 * when opening a master, the slave id must be here given.
238 int snd_timer_open(snd_timer_instance_t **ti,
239 char *owner, snd_timer_id_t *tid,
240 unsigned int slave_id)
242 snd_timer_t *timer;
243 snd_timer_instance_t *timeri = NULL;
245 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
246 /* open a slave instance */
247 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
248 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
249 snd_printd("invalid slave class %i\n", tid->dev_sclass);
250 return -EINVAL;
252 down(&register_mutex);
253 timeri = snd_timer_instance_new(owner, NULL);
254 timeri->slave_class = tid->dev_sclass;
255 timeri->slave_id = tid->device;
256 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
257 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
258 snd_timer_check_slave(timeri);
259 up(&register_mutex);
260 *ti = timeri;
261 return 0;
264 /* open a master instance */
265 down(&register_mutex);
266 timer = snd_timer_find(tid);
267 #ifdef CONFIG_KMOD
268 if (timer == NULL) {
269 up(&register_mutex);
270 snd_timer_request(tid);
271 down(&register_mutex);
272 timer = snd_timer_find(tid);
274 #endif
275 if (timer) {
276 if (!list_empty(&timer->open_list_head)) {
277 timeri = (snd_timer_instance_t *)list_entry(timer->open_list_head.next, snd_timer_instance_t, open_list);
278 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
279 up(&register_mutex);
280 return -EBUSY;
283 timeri = snd_timer_instance_new(owner, timer);
284 if (timeri) {
285 timeri->slave_class = tid->dev_sclass;
286 timeri->slave_id = slave_id;
287 if (list_empty(&timer->open_list_head) && timer->hw.open)
288 timer->hw.open(timer);
289 list_add_tail(&timeri->open_list, &timer->open_list_head);
290 snd_timer_check_master(timeri);
292 } else {
293 up(&register_mutex);
294 return -ENODEV;
296 up(&register_mutex);
297 *ti = timeri;
298 return 0;
301 static int _snd_timer_stop(snd_timer_instance_t * timeri, int keep_flag, enum sndrv_timer_event event);
304 * close a timer instance
306 int snd_timer_close(snd_timer_instance_t * timeri)
308 snd_timer_t *timer = NULL;
309 struct list_head *p, *n;
310 snd_timer_instance_t *slave;
312 snd_assert(timeri != NULL, return -ENXIO);
314 /* force to stop the timer */
315 snd_timer_stop(timeri);
317 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
318 /* wait, until the active callback is finished */
319 spin_lock_irq(&slave_active_lock);
320 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
321 spin_unlock_irq(&slave_active_lock);
322 udelay(10);
323 spin_lock_irq(&slave_active_lock);
325 spin_unlock_irq(&slave_active_lock);
326 down(&register_mutex);
327 list_del(&timeri->open_list);
328 up(&register_mutex);
329 } else {
330 timer = timeri->timer;
331 /* wait, until the active callback is finished */
332 spin_lock_irq(&timer->lock);
333 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
334 spin_unlock_irq(&timer->lock);
335 udelay(10);
336 spin_lock_irq(&timer->lock);
338 spin_unlock_irq(&timer->lock);
339 down(&register_mutex);
340 list_del(&timeri->open_list);
341 if (timer && list_empty(&timer->open_list_head) && timer->hw.close)
342 timer->hw.close(timer);
343 /* remove slave links */
344 list_for_each_safe(p, n, &timeri->slave_list_head) {
345 slave = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, open_list);
346 spin_lock_irq(&slave_active_lock);
347 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
348 list_del(p);
349 list_add_tail(p, &snd_timer_slave_list);
350 slave->master = NULL;
351 slave->timer = NULL;
352 spin_unlock_irq(&slave_active_lock);
354 up(&register_mutex);
356 if (timeri->private_free)
357 timeri->private_free(timeri);
358 kfree(timeri->owner);
359 kfree(timeri);
360 if (timer && timer->card)
361 module_put(timer->card->module);
362 return 0;
365 unsigned long snd_timer_resolution(snd_timer_instance_t * timeri)
367 snd_timer_t * timer;
369 if (timeri == NULL)
370 return 0;
371 if ((timer = timeri->timer) != NULL) {
372 if (timer->hw.c_resolution)
373 return timer->hw.c_resolution(timer);
374 return timer->hw.resolution;
376 return 0;
379 static void snd_timer_notify1(snd_timer_instance_t *ti, enum sndrv_timer_event event)
381 snd_timer_t *timer;
382 unsigned long flags;
383 unsigned long resolution = 0;
384 snd_timer_instance_t *ts;
385 struct list_head *n;
386 struct timespec tstamp;
388 snd_timestamp_now(&tstamp, 1);
389 snd_assert(event >= SNDRV_TIMER_EVENT_START && event <= SNDRV_TIMER_EVENT_PAUSE, return);
390 if (event == SNDRV_TIMER_EVENT_START || event == SNDRV_TIMER_EVENT_CONTINUE)
391 resolution = snd_timer_resolution(ti);
392 if (ti->ccallback)
393 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
394 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
395 return;
396 timer = ti->timer;
397 if (timer == NULL)
398 return;
399 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
400 return;
401 spin_lock_irqsave(&timer->lock, flags);
402 list_for_each(n, &ti->slave_active_head) {
403 ts = (snd_timer_instance_t *)list_entry(n, snd_timer_instance_t, active_list);
404 if (ts->ccallback)
405 ts->ccallback(ti, event + 100, &tstamp, resolution);
407 spin_unlock_irqrestore(&timer->lock, flags);
410 static int snd_timer_start1(snd_timer_t *timer, snd_timer_instance_t *timeri, unsigned long sticks)
412 list_del(&timeri->active_list);
413 list_add_tail(&timeri->active_list, &timer->active_list_head);
414 if (timer->running) {
415 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
416 goto __start_now;
417 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
418 timeri->flags |= SNDRV_TIMER_IFLG_START;
419 return 1; /* delayed start */
420 } else {
421 timer->sticks = sticks;
422 timer->hw.start(timer);
423 __start_now:
424 timer->running++;
425 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
426 return 0;
430 static int snd_timer_start_slave(snd_timer_instance_t *timeri)
432 unsigned long flags;
434 spin_lock_irqsave(&slave_active_lock, flags);
435 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
436 if (timeri->master)
437 list_add_tail(&timeri->active_list, &timeri->master->slave_active_head);
438 spin_unlock_irqrestore(&slave_active_lock, flags);
439 return 1; /* delayed start */
443 * start the timer instance
445 int snd_timer_start(snd_timer_instance_t * timeri, unsigned int ticks)
447 snd_timer_t *timer;
448 int result = -EINVAL;
449 unsigned long flags;
451 if (timeri == NULL || ticks < 1)
452 return -EINVAL;
453 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
454 result = snd_timer_start_slave(timeri);
455 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
456 return result;
458 timer = timeri->timer;
459 if (timer == NULL)
460 return -EINVAL;
461 spin_lock_irqsave(&timer->lock, flags);
462 timeri->ticks = timeri->cticks = ticks;
463 timeri->pticks = 0;
464 result = snd_timer_start1(timer, timeri, ticks);
465 spin_unlock_irqrestore(&timer->lock, flags);
466 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
467 return result;
470 static int _snd_timer_stop(snd_timer_instance_t * timeri, int keep_flag, enum sndrv_timer_event event)
472 snd_timer_t *timer;
473 unsigned long flags;
475 snd_assert(timeri != NULL, return -ENXIO);
477 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
478 if (!keep_flag) {
479 spin_lock_irqsave(&slave_active_lock, flags);
480 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
481 spin_unlock_irqrestore(&slave_active_lock, flags);
483 goto __end;
485 timer = timeri->timer;
486 if (!timer)
487 return -EINVAL;
488 spin_lock_irqsave(&timer->lock, flags);
489 list_del_init(&timeri->ack_list);
490 list_del_init(&timeri->active_list);
491 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
492 !(--timer->running)) {
493 timer->hw.stop(timer);
494 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
495 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
496 snd_timer_reschedule(timer, 0);
497 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
498 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
499 timer->hw.start(timer);
503 if (!keep_flag)
504 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING|SNDRV_TIMER_IFLG_START);
505 spin_unlock_irqrestore(&timer->lock, flags);
506 __end:
507 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
508 snd_timer_notify1(timeri, event);
509 return 0;
513 * stop the timer instance.
515 * do not call this from the timer callback!
517 int snd_timer_stop(snd_timer_instance_t * timeri)
519 snd_timer_t *timer;
520 unsigned long flags;
521 int err;
523 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
524 if (err < 0)
525 return err;
526 timer = timeri->timer;
527 spin_lock_irqsave(&timer->lock, flags);
528 timeri->cticks = timeri->ticks;
529 timeri->pticks = 0;
530 spin_unlock_irqrestore(&timer->lock, flags);
531 return 0;
535 * start again.. the tick is kept.
537 int snd_timer_continue(snd_timer_instance_t * timeri)
539 snd_timer_t *timer;
540 int result = -EINVAL;
541 unsigned long flags;
543 if (timeri == NULL)
544 return result;
545 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
546 return snd_timer_start_slave(timeri);
547 timer = timeri->timer;
548 if (! timer)
549 return -EINVAL;
550 spin_lock_irqsave(&timer->lock, flags);
551 if (!timeri->cticks)
552 timeri->cticks = 1;
553 timeri->pticks = 0;
554 result = snd_timer_start1(timer, timeri, timer->sticks);
555 spin_unlock_irqrestore(&timer->lock, flags);
556 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
557 return result;
561 * pause.. remember the ticks left
563 int snd_timer_pause(snd_timer_instance_t * timeri)
565 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
569 * reschedule the timer
571 * start pending instances and check the scheduling ticks.
572 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
574 static void snd_timer_reschedule(snd_timer_t * timer, unsigned long ticks_left)
576 snd_timer_instance_t *ti;
577 unsigned long ticks = ~0UL;
578 struct list_head *p;
580 list_for_each(p, &timer->active_list_head) {
581 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, active_list);
582 if (ti->flags & SNDRV_TIMER_IFLG_START) {
583 ti->flags &= ~SNDRV_TIMER_IFLG_START;
584 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
585 timer->running++;
587 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
588 if (ticks > ti->cticks)
589 ticks = ti->cticks;
592 if (ticks == ~0UL) {
593 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
594 return;
596 if (ticks > timer->hw.ticks)
597 ticks = timer->hw.ticks;
598 if (ticks_left != ticks)
599 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
600 timer->sticks = ticks;
604 * timer tasklet
607 static void snd_timer_tasklet(unsigned long arg)
609 snd_timer_t *timer = (snd_timer_t *) arg;
610 snd_timer_instance_t *ti;
611 struct list_head *p;
612 unsigned long resolution, ticks;
614 spin_lock(&timer->lock);
615 /* now process all callbacks */
616 while (!list_empty(&timer->sack_list_head)) {
617 p = timer->sack_list_head.next; /* get first item */
618 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, ack_list);
620 /* remove from ack_list and make empty */
621 list_del_init(p);
623 ticks = ti->pticks;
624 ti->pticks = 0;
625 resolution = ti->resolution;
627 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
628 spin_unlock(&timer->lock);
629 if (ti->callback)
630 ti->callback(ti, resolution, ticks);
631 spin_lock(&timer->lock);
632 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
634 spin_unlock(&timer->lock);
638 * timer interrupt
640 * ticks_left is usually equal to timer->sticks.
643 void snd_timer_interrupt(snd_timer_t * timer, unsigned long ticks_left)
645 snd_timer_instance_t *ti, *ts;
646 unsigned long resolution, ticks;
647 struct list_head *p, *q, *n;
648 int use_tasklet = 0;
650 if (timer == NULL)
651 return;
653 spin_lock(&timer->lock);
655 /* remember the current resolution */
656 if (timer->hw.c_resolution)
657 resolution = timer->hw.c_resolution(timer);
658 else
659 resolution = timer->hw.resolution;
661 /* loop for all active instances
662 * here we cannot use list_for_each because the active_list of a processed
663 * instance is relinked to done_list_head before callback is called.
665 list_for_each_safe(p, n, &timer->active_list_head) {
666 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, active_list);
667 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
668 continue;
669 ti->pticks += ticks_left;
670 ti->resolution = resolution;
671 if (ti->cticks < ticks_left)
672 ti->cticks = 0;
673 else
674 ti->cticks -= ticks_left;
675 if (ti->cticks) /* not expired */
676 continue;
677 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
678 ti->cticks = ti->ticks;
679 } else {
680 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
681 if (--timer->running)
682 list_del(p);
684 if (list_empty(&ti->ack_list)) {
685 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
686 (ti->flags & SNDRV_TIMER_IFLG_FAST)) {
687 list_add_tail(&ti->ack_list, &timer->ack_list_head);
688 } else {
689 list_add_tail(&ti->ack_list, &timer->sack_list_head);
692 list_for_each(q, &ti->slave_active_head) {
693 ts = (snd_timer_instance_t *)list_entry(q, snd_timer_instance_t, active_list);
694 ts->pticks = ti->pticks;
695 ts->resolution = resolution;
696 if (list_empty(&ts->ack_list)) {
697 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
698 (ti->flags & SNDRV_TIMER_IFLG_FAST)) {
699 list_add_tail(&ts->ack_list, &timer->ack_list_head);
700 } else {
701 list_add_tail(&ts->ack_list, &timer->sack_list_head);
706 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
707 snd_timer_reschedule(timer, ticks_left);
708 if (timer->running) {
709 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
710 timer->hw.stop(timer);
711 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
713 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
714 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
715 /* restart timer */
716 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
717 timer->hw.start(timer);
719 } else {
720 timer->hw.stop(timer);
723 /* now process all fast callbacks */
724 while (!list_empty(&timer->ack_list_head)) {
725 p = timer->ack_list_head.next; /* get first item */
726 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, ack_list);
728 /* remove from ack_list and make empty */
729 list_del_init(p);
731 ticks = ti->pticks;
732 ti->pticks = 0;
734 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
735 spin_unlock(&timer->lock);
736 if (ti->callback)
737 ti->callback(ti, resolution, ticks);
738 spin_lock(&timer->lock);
739 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
742 /* do we have any slow callbacks? */
743 use_tasklet = !list_empty(&timer->sack_list_head);
744 spin_unlock(&timer->lock);
746 if (use_tasklet)
747 tasklet_hi_schedule(&timer->task_queue);
754 int snd_timer_new(snd_card_t *card, char *id, snd_timer_id_t *tid, snd_timer_t ** rtimer)
756 snd_timer_t *timer;
757 int err;
758 static snd_device_ops_t ops = {
759 .dev_free = snd_timer_dev_free,
760 .dev_register = snd_timer_dev_register,
761 .dev_unregister = snd_timer_dev_unregister
764 snd_assert(tid != NULL, return -EINVAL);
765 snd_assert(rtimer != NULL, return -EINVAL);
766 *rtimer = NULL;
767 timer = kcalloc(1, sizeof(*timer), GFP_KERNEL);
768 if (timer == NULL)
769 return -ENOMEM;
770 timer->tmr_class = tid->dev_class;
771 timer->card = card;
772 timer->tmr_device = tid->device;
773 timer->tmr_subdevice = tid->subdevice;
774 if (id)
775 strlcpy(timer->id, id, sizeof(timer->id));
776 INIT_LIST_HEAD(&timer->device_list);
777 INIT_LIST_HEAD(&timer->open_list_head);
778 INIT_LIST_HEAD(&timer->active_list_head);
779 INIT_LIST_HEAD(&timer->ack_list_head);
780 INIT_LIST_HEAD(&timer->sack_list_head);
781 spin_lock_init(&timer->lock);
782 tasklet_init(&timer->task_queue, snd_timer_tasklet, (unsigned long)timer);
783 if (card != NULL) {
784 if ((err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops)) < 0) {
785 snd_timer_free(timer);
786 return err;
789 *rtimer = timer;
790 return 0;
793 static int snd_timer_free(snd_timer_t *timer)
795 snd_assert(timer != NULL, return -ENXIO);
796 if (timer->private_free)
797 timer->private_free(timer);
798 kfree(timer);
799 return 0;
802 static int snd_timer_dev_free(snd_device_t *device)
804 snd_timer_t *timer = device->device_data;
805 return snd_timer_free(timer);
808 static int snd_timer_dev_register(snd_device_t *dev)
810 snd_timer_t *timer = dev->device_data;
811 snd_timer_t *timer1;
812 struct list_head *p;
814 snd_assert(timer != NULL && timer->hw.start != NULL && timer->hw.stop != NULL, return -ENXIO);
815 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
816 !timer->hw.resolution && timer->hw.c_resolution == NULL)
817 return -EINVAL;
819 down(&register_mutex);
820 list_for_each(p, &snd_timer_list) {
821 timer1 = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
822 if (timer1->tmr_class > timer->tmr_class)
823 break;
824 if (timer1->tmr_class < timer->tmr_class)
825 continue;
826 if (timer1->card && timer->card) {
827 if (timer1->card->number > timer->card->number)
828 break;
829 if (timer1->card->number < timer->card->number)
830 continue;
832 if (timer1->tmr_device > timer->tmr_device)
833 break;
834 if (timer1->tmr_device < timer->tmr_device)
835 continue;
836 if (timer1->tmr_subdevice > timer->tmr_subdevice)
837 break;
838 if (timer1->tmr_subdevice < timer->tmr_subdevice)
839 continue;
840 /* conflicts.. */
841 up(&register_mutex);
842 return -EBUSY;
844 list_add_tail(&timer->device_list, p);
845 up(&register_mutex);
846 return 0;
849 static int snd_timer_unregister(snd_timer_t *timer)
851 struct list_head *p, *n;
852 snd_timer_instance_t *ti;
854 snd_assert(timer != NULL, return -ENXIO);
855 down(&register_mutex);
856 if (! list_empty(&timer->open_list_head)) {
857 snd_printk(KERN_WARNING "timer 0x%lx is busy?\n", (long)timer);
858 list_for_each_safe(p, n, &timer->open_list_head) {
859 list_del_init(p);
860 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, open_list);
861 ti->timer = NULL;
864 list_del(&timer->device_list);
865 up(&register_mutex);
866 return snd_timer_free(timer);
869 static int snd_timer_dev_unregister(snd_device_t *device)
871 snd_timer_t *timer = device->device_data;
872 return snd_timer_unregister(timer);
875 void snd_timer_notify(snd_timer_t *timer, enum sndrv_timer_event event, struct timespec *tstamp)
877 unsigned long flags;
878 unsigned long resolution = 0;
879 snd_timer_instance_t *ti, *ts;
880 struct list_head *p, *n;
882 snd_runtime_check(timer->hw.flags & SNDRV_TIMER_HW_SLAVE, return);
883 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART && event <= SNDRV_TIMER_EVENT_MRESUME, return);
884 spin_lock_irqsave(&timer->lock, flags);
885 if (event == SNDRV_TIMER_EVENT_MSTART ||
886 event == SNDRV_TIMER_EVENT_MCONTINUE ||
887 event == SNDRV_TIMER_EVENT_MRESUME) {
888 if (timer->hw.c_resolution)
889 resolution = timer->hw.c_resolution(timer);
890 else
891 resolution = timer->hw.resolution;
893 list_for_each(p, &timer->active_list_head) {
894 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, active_list);
895 if (ti->ccallback)
896 ti->ccallback(ti, event, tstamp, resolution);
897 list_for_each(n, &ti->slave_active_head) {
898 ts = (snd_timer_instance_t *)list_entry(n, snd_timer_instance_t, active_list);
899 if (ts->ccallback)
900 ts->ccallback(ts, event, tstamp, resolution);
903 spin_unlock_irqrestore(&timer->lock, flags);
907 * exported functions for global timers
909 int snd_timer_global_new(char *id, int device, snd_timer_t **rtimer)
911 snd_timer_id_t tid;
913 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
914 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
915 tid.card = -1;
916 tid.device = device;
917 tid.subdevice = 0;
918 return snd_timer_new(NULL, id, &tid, rtimer);
921 int snd_timer_global_free(snd_timer_t *timer)
923 return snd_timer_free(timer);
926 int snd_timer_global_register(snd_timer_t *timer)
928 snd_device_t dev;
930 memset(&dev, 0, sizeof(dev));
931 dev.device_data = timer;
932 return snd_timer_dev_register(&dev);
935 int snd_timer_global_unregister(snd_timer_t *timer)
937 return snd_timer_unregister(timer);
941 * System timer
944 struct snd_timer_system_private {
945 struct timer_list tlist;
946 struct timer * timer;
947 unsigned long last_expires;
948 unsigned long last_jiffies;
949 unsigned long correction;
952 static void snd_timer_s_function(unsigned long data)
954 snd_timer_t *timer = (snd_timer_t *)data;
955 struct snd_timer_system_private *priv = timer->private_data;
956 unsigned long jiff = jiffies;
957 if (time_after(jiff, priv->last_expires))
958 priv->correction = (long)jiff - (long)priv->last_expires;
959 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
962 static int snd_timer_s_start(snd_timer_t * timer)
964 struct snd_timer_system_private *priv;
965 unsigned long njiff;
967 priv = (struct snd_timer_system_private *) timer->private_data;
968 njiff = (priv->last_jiffies = jiffies);
969 if (priv->correction > timer->sticks - 1) {
970 priv->correction -= timer->sticks - 1;
971 njiff++;
972 } else {
973 njiff += timer->sticks - priv->correction;
974 priv->correction -= timer->sticks;
976 priv->last_expires = priv->tlist.expires = njiff;
977 add_timer(&priv->tlist);
978 return 0;
981 static int snd_timer_s_stop(snd_timer_t * timer)
983 struct snd_timer_system_private *priv;
984 unsigned long jiff;
986 priv = (struct snd_timer_system_private *) timer->private_data;
987 del_timer(&priv->tlist);
988 jiff = jiffies;
989 if (time_before(jiff, priv->last_expires))
990 timer->sticks = priv->last_expires - jiff;
991 else
992 timer->sticks = 1;
993 return 0;
996 static struct _snd_timer_hardware snd_timer_system =
998 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
999 .resolution = 1000000000L / HZ,
1000 .ticks = 10000000L,
1001 .start = snd_timer_s_start,
1002 .stop = snd_timer_s_stop
1005 static void snd_timer_free_system(snd_timer_t *timer)
1007 kfree(timer->private_data);
1010 static int snd_timer_register_system(void)
1012 snd_timer_t *timer;
1013 struct snd_timer_system_private *priv;
1014 int err;
1016 if ((err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer)) < 0)
1017 return err;
1018 strcpy(timer->name, "system timer");
1019 timer->hw = snd_timer_system;
1020 priv = kcalloc(1, sizeof(*priv), GFP_KERNEL);
1021 if (priv == NULL) {
1022 snd_timer_free(timer);
1023 return -ENOMEM;
1025 init_timer(&priv->tlist);
1026 priv->tlist.function = snd_timer_s_function;
1027 priv->tlist.data = (unsigned long) timer;
1028 timer->private_data = priv;
1029 timer->private_free = snd_timer_free_system;
1030 return snd_timer_global_register(timer);
1034 * Info interface
1037 static void snd_timer_proc_read(snd_info_entry_t *entry,
1038 snd_info_buffer_t * buffer)
1040 unsigned long flags;
1041 snd_timer_t *timer;
1042 snd_timer_instance_t *ti;
1043 struct list_head *p, *q;
1045 down(&register_mutex);
1046 list_for_each(p, &snd_timer_list) {
1047 timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
1048 switch (timer->tmr_class) {
1049 case SNDRV_TIMER_CLASS_GLOBAL:
1050 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1051 break;
1052 case SNDRV_TIMER_CLASS_CARD:
1053 snd_iprintf(buffer, "C%i-%i: ", timer->card->number, timer->tmr_device);
1054 break;
1055 case SNDRV_TIMER_CLASS_PCM:
1056 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number, timer->tmr_device, timer->tmr_subdevice);
1057 break;
1058 default:
1059 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class, timer->card ? timer->card->number : -1, timer->tmr_device, timer->tmr_subdevice);
1061 snd_iprintf(buffer, "%s :", timer->name);
1062 if (timer->hw.resolution)
1063 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)", timer->hw.resolution / 1000, timer->hw.resolution % 1000, timer->hw.ticks);
1064 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1065 snd_iprintf(buffer, " SLAVE");
1066 snd_iprintf(buffer, "\n");
1067 spin_lock_irqsave(&timer->lock, flags);
1068 list_for_each(q, &timer->open_list_head) {
1069 ti = (snd_timer_instance_t *)list_entry(q, snd_timer_instance_t, open_list);
1070 snd_iprintf(buffer, " Client %s : %s : lost interrupts %li\n",
1071 ti->owner ? ti->owner : "unknown",
1072 ti->flags & (SNDRV_TIMER_IFLG_START|SNDRV_TIMER_IFLG_RUNNING) ? "running" : "stopped",
1073 ti->lost);
1075 spin_unlock_irqrestore(&timer->lock, flags);
1077 up(&register_mutex);
1081 * USER SPACE interface
1084 static void snd_timer_user_interrupt(snd_timer_instance_t *timeri,
1085 unsigned long resolution,
1086 unsigned long ticks)
1088 snd_timer_user_t *tu = timeri->callback_data;
1089 snd_timer_read_t *r;
1090 int prev;
1092 spin_lock(&tu->qlock);
1093 if (tu->qused > 0) {
1094 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1095 r = &tu->queue[prev];
1096 if (r->resolution == resolution) {
1097 r->ticks += ticks;
1098 goto __wake;
1101 if (tu->qused >= tu->queue_size) {
1102 tu->overrun++;
1103 } else {
1104 r = &tu->queue[tu->qtail++];
1105 tu->qtail %= tu->queue_size;
1106 r->resolution = resolution;
1107 r->ticks = ticks;
1108 tu->qused++;
1110 __wake:
1111 spin_unlock(&tu->qlock);
1112 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1113 wake_up(&tu->qchange_sleep);
1116 static void snd_timer_user_append_to_tqueue(snd_timer_user_t *tu, snd_timer_tread_t *tread)
1118 if (tu->qused >= tu->queue_size) {
1119 tu->overrun++;
1120 } else {
1121 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1122 tu->qtail %= tu->queue_size;
1123 tu->qused++;
1127 static void snd_timer_user_ccallback(snd_timer_instance_t *timeri,
1128 enum sndrv_timer_event event,
1129 struct timespec *tstamp,
1130 unsigned long resolution)
1132 snd_timer_user_t *tu = timeri->callback_data;
1133 snd_timer_tread_t r1;
1135 if (event >= SNDRV_TIMER_EVENT_START && event <= SNDRV_TIMER_EVENT_PAUSE)
1136 tu->tstamp = *tstamp;
1137 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1138 return;
1139 r1.event = event;
1140 r1.tstamp = *tstamp;
1141 r1.val = resolution;
1142 spin_lock(&tu->qlock);
1143 snd_timer_user_append_to_tqueue(tu, &r1);
1144 spin_unlock(&tu->qlock);
1145 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1146 wake_up(&tu->qchange_sleep);
1149 static void snd_timer_user_tinterrupt(snd_timer_instance_t *timeri,
1150 unsigned long resolution,
1151 unsigned long ticks)
1153 snd_timer_user_t *tu = timeri->callback_data;
1154 snd_timer_tread_t *r, r1;
1155 struct timespec tstamp;
1156 int prev, append = 0;
1158 snd_timestamp_zero(&tstamp);
1159 spin_lock(&tu->qlock);
1160 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION)|(1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1161 spin_unlock(&tu->qlock);
1162 return;
1164 if (tu->last_resolution != resolution || ticks > 0)
1165 snd_timestamp_now(&tstamp, 1);
1166 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) && tu->last_resolution != resolution) {
1167 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1168 r1.tstamp = tstamp;
1169 r1.val = resolution;
1170 snd_timer_user_append_to_tqueue(tu, &r1);
1171 tu->last_resolution = resolution;
1172 append++;
1174 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1175 goto __wake;
1176 if (ticks == 0)
1177 goto __wake;
1178 if (tu->qused > 0) {
1179 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1180 r = &tu->tqueue[prev];
1181 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1182 r->tstamp = tstamp;
1183 r->val += ticks;
1184 append++;
1185 goto __wake;
1188 r1.event = SNDRV_TIMER_EVENT_TICK;
1189 r1.tstamp = tstamp;
1190 r1.val = ticks;
1191 snd_timer_user_append_to_tqueue(tu, &r1);
1192 append++;
1193 __wake:
1194 spin_unlock(&tu->qlock);
1195 if (append == 0)
1196 return;
1197 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1198 wake_up(&tu->qchange_sleep);
1201 static int snd_timer_user_open(struct inode *inode, struct file *file)
1203 snd_timer_user_t *tu;
1205 tu = kcalloc(1, sizeof(*tu), GFP_KERNEL);
1206 if (tu == NULL)
1207 return -ENOMEM;
1208 spin_lock_init(&tu->qlock);
1209 init_waitqueue_head(&tu->qchange_sleep);
1210 init_MUTEX(&tu->tread_sem);
1211 tu->ticks = 1;
1212 tu->queue_size = 128;
1213 tu->queue = (snd_timer_read_t *)kmalloc(tu->queue_size * sizeof(snd_timer_read_t), GFP_KERNEL);
1214 if (tu->queue == NULL) {
1215 kfree(tu);
1216 return -ENOMEM;
1218 file->private_data = tu;
1219 return 0;
1222 static int snd_timer_user_release(struct inode *inode, struct file *file)
1224 snd_timer_user_t *tu;
1226 if (file->private_data) {
1227 tu = file->private_data;
1228 file->private_data = NULL;
1229 fasync_helper(-1, file, 0, &tu->fasync);
1230 if (tu->timeri)
1231 snd_timer_close(tu->timeri);
1232 kfree(tu->queue);
1233 kfree(tu->tqueue);
1234 kfree(tu);
1236 return 0;
1239 static void snd_timer_user_zero_id(snd_timer_id_t *id)
1241 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1242 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1243 id->card = -1;
1244 id->device = -1;
1245 id->subdevice = -1;
1248 static void snd_timer_user_copy_id(snd_timer_id_t *id, snd_timer_t *timer)
1250 id->dev_class = timer->tmr_class;
1251 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1252 id->card = timer->card ? timer->card->number : -1;
1253 id->device = timer->tmr_device;
1254 id->subdevice = timer->tmr_subdevice;
1257 static int snd_timer_user_next_device(snd_timer_id_t __user *_tid)
1259 snd_timer_id_t id;
1260 snd_timer_t *timer;
1261 struct list_head *p;
1263 if (copy_from_user(&id, _tid, sizeof(id)))
1264 return -EFAULT;
1265 down(&register_mutex);
1266 if (id.dev_class < 0) { /* first item */
1267 if (list_empty(&snd_timer_list))
1268 snd_timer_user_zero_id(&id);
1269 else {
1270 timer = (snd_timer_t *)list_entry(snd_timer_list.next, snd_timer_t, device_list);
1271 snd_timer_user_copy_id(&id, timer);
1273 } else {
1274 switch (id.dev_class) {
1275 case SNDRV_TIMER_CLASS_GLOBAL:
1276 id.device = id.device < 0 ? 0 : id.device + 1;
1277 list_for_each(p, &snd_timer_list) {
1278 timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
1279 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1280 snd_timer_user_copy_id(&id, timer);
1281 break;
1283 if (timer->tmr_device >= id.device) {
1284 snd_timer_user_copy_id(&id, timer);
1285 break;
1288 if (p == &snd_timer_list)
1289 snd_timer_user_zero_id(&id);
1290 break;
1291 case SNDRV_TIMER_CLASS_CARD:
1292 case SNDRV_TIMER_CLASS_PCM:
1293 if (id.card < 0) {
1294 id.card = 0;
1295 } else {
1296 if (id.card < 0) {
1297 id.card = 0;
1298 } else {
1299 if (id.device < 0) {
1300 id.device = 0;
1301 } else {
1302 id.subdevice = id.subdevice < 0 ? 0 : id.subdevice + 1;
1306 list_for_each(p, &snd_timer_list) {
1307 timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
1308 if (timer->tmr_class > id.dev_class) {
1309 snd_timer_user_copy_id(&id, timer);
1310 break;
1312 if (timer->tmr_class < id.dev_class)
1313 continue;
1314 if (timer->card->number > id.card) {
1315 snd_timer_user_copy_id(&id, timer);
1316 break;
1318 if (timer->card->number < id.card)
1319 continue;
1320 if (timer->tmr_device > id.device) {
1321 snd_timer_user_copy_id(&id, timer);
1322 break;
1324 if (timer->tmr_device < id.device)
1325 continue;
1326 if (timer->tmr_subdevice > id.subdevice) {
1327 snd_timer_user_copy_id(&id, timer);
1328 break;
1330 if (timer->tmr_subdevice < id.subdevice)
1331 continue;
1332 snd_timer_user_copy_id(&id, timer);
1333 break;
1335 if (p == &snd_timer_list)
1336 snd_timer_user_zero_id(&id);
1337 break;
1338 default:
1339 snd_timer_user_zero_id(&id);
1342 up(&register_mutex);
1343 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1344 return -EFAULT;
1345 return 0;
1348 static int snd_timer_user_ginfo(struct file *file, snd_timer_ginfo_t __user *_ginfo)
1350 snd_timer_ginfo_t *ginfo;
1351 snd_timer_id_t tid;
1352 snd_timer_t *t;
1353 struct list_head *p;
1354 int err = 0;
1356 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1357 if (! ginfo)
1358 return -ENOMEM;
1359 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1360 kfree(ginfo);
1361 return -EFAULT;
1363 tid = ginfo->tid;
1364 memset(ginfo, 0, sizeof(*ginfo));
1365 ginfo->tid = tid;
1366 down(&register_mutex);
1367 t = snd_timer_find(&tid);
1368 if (t != NULL) {
1369 ginfo->card = t->card ? t->card->number : -1;
1370 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1371 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1372 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1373 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1374 ginfo->resolution = t->hw.resolution;
1375 if (t->hw.resolution_min > 0) {
1376 ginfo->resolution_min = t->hw.resolution_min;
1377 ginfo->resolution_max = t->hw.resolution_max;
1379 list_for_each(p, &t->open_list_head) {
1380 ginfo->clients++;
1382 } else {
1383 err = -ENODEV;
1385 up(&register_mutex);
1386 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1387 err = -EFAULT;
1388 kfree(ginfo);
1389 return err;
1392 static int snd_timer_user_gparams(struct file *file, snd_timer_gparams_t __user *_gparams)
1394 snd_timer_gparams_t gparams;
1395 snd_timer_t *t;
1396 int err;
1398 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1399 return -EFAULT;
1400 down(&register_mutex);
1401 t = snd_timer_find(&gparams.tid);
1402 if (t != NULL) {
1403 if (list_empty(&t->open_list_head)) {
1404 if (t->hw.set_period)
1405 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1406 else
1407 err = -ENOSYS;
1408 } else {
1409 err = -EBUSY;
1411 } else {
1412 err = -ENODEV;
1414 up(&register_mutex);
1415 return err;
1418 static int snd_timer_user_gstatus(struct file *file, snd_timer_gstatus_t __user *_gstatus)
1420 snd_timer_gstatus_t gstatus;
1421 snd_timer_id_t tid;
1422 snd_timer_t *t;
1423 int err = 0;
1425 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1426 return -EFAULT;
1427 tid = gstatus.tid;
1428 memset(&gstatus, 0, sizeof(gstatus));
1429 gstatus.tid = tid;
1430 down(&register_mutex);
1431 t = snd_timer_find(&tid);
1432 if (t != NULL) {
1433 if (t->hw.c_resolution)
1434 gstatus.resolution = t->hw.c_resolution(t);
1435 else
1436 gstatus.resolution = t->hw.resolution;
1437 if (t->hw.precise_resolution) {
1438 t->hw.precise_resolution(t, &gstatus.resolution_num, &gstatus.resolution_den);
1439 } else {
1440 gstatus.resolution_num = gstatus.resolution;
1441 gstatus.resolution_den = 1000000000uL;
1443 } else {
1444 err = -ENODEV;
1446 up(&register_mutex);
1447 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1448 err = -EFAULT;
1449 return err;
1452 static int snd_timer_user_tselect(struct file *file, snd_timer_select_t __user *_tselect)
1454 snd_timer_user_t *tu;
1455 snd_timer_select_t tselect;
1456 char str[32];
1457 int err = 0;
1459 tu = file->private_data;
1460 down(&tu->tread_sem);
1461 if (tu->timeri) {
1462 snd_timer_close(tu->timeri);
1463 tu->timeri = NULL;
1465 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1466 err = -EFAULT;
1467 goto __err;
1469 sprintf(str, "application %i", current->pid);
1470 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1471 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1472 if ((err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid)) < 0)
1473 goto __err;
1475 kfree(tu->queue);
1476 tu->queue = NULL;
1477 kfree(tu->tqueue);
1478 tu->tqueue = NULL;
1479 if (tu->tread) {
1480 tu->tqueue = (snd_timer_tread_t *)kmalloc(tu->queue_size * sizeof(snd_timer_tread_t), GFP_KERNEL);
1481 if (tu->tqueue == NULL)
1482 err = -ENOMEM;
1483 } else {
1484 tu->queue = (snd_timer_read_t *)kmalloc(tu->queue_size * sizeof(snd_timer_read_t), GFP_KERNEL);
1485 if (tu->queue == NULL)
1486 err = -ENOMEM;
1489 if (err < 0) {
1490 snd_timer_close(tu->timeri);
1491 tu->timeri = NULL;
1492 } else {
1493 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1494 tu->timeri->callback = tu->tread ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1495 tu->timeri->ccallback = snd_timer_user_ccallback;
1496 tu->timeri->callback_data = (void *)tu;
1499 __err:
1500 up(&tu->tread_sem);
1501 return err;
1504 static int snd_timer_user_info(struct file *file, snd_timer_info_t __user *_info)
1506 snd_timer_user_t *tu;
1507 snd_timer_info_t *info;
1508 snd_timer_t *t;
1509 int err = 0;
1511 tu = file->private_data;
1512 snd_assert(tu->timeri != NULL, return -ENXIO);
1513 t = tu->timeri->timer;
1514 snd_assert(t != NULL, return -ENXIO);
1516 info = kcalloc(1, sizeof(*info), GFP_KERNEL);
1517 if (! info)
1518 return -ENOMEM;
1519 info->card = t->card ? t->card->number : -1;
1520 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1521 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1522 strlcpy(info->id, t->id, sizeof(info->id));
1523 strlcpy(info->name, t->name, sizeof(info->name));
1524 info->resolution = t->hw.resolution;
1525 if (copy_to_user(_info, info, sizeof(*_info)))
1526 err = -EFAULT;
1527 kfree(info);
1528 return err;
1531 static int snd_timer_user_params(struct file *file, snd_timer_params_t __user *_params)
1533 snd_timer_user_t *tu;
1534 snd_timer_params_t params;
1535 snd_timer_t *t;
1536 snd_timer_read_t *tr;
1537 snd_timer_tread_t *ttr;
1538 int err;
1540 tu = file->private_data;
1541 snd_assert(tu->timeri != NULL, return -ENXIO);
1542 t = tu->timeri->timer;
1543 snd_assert(t != NULL, return -ENXIO);
1544 if (copy_from_user(&params, _params, sizeof(params)))
1545 return -EFAULT;
1546 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1547 err = -EINVAL;
1548 goto _end;
1550 if (params.queue_size > 0 && (params.queue_size < 32 || params.queue_size > 1024)) {
1551 err = -EINVAL;
1552 goto _end;
1554 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1555 (1<<SNDRV_TIMER_EVENT_TICK)|
1556 (1<<SNDRV_TIMER_EVENT_START)|
1557 (1<<SNDRV_TIMER_EVENT_STOP)|
1558 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1559 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1560 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1561 (1<<SNDRV_TIMER_EVENT_RESUME)|
1562 (1<<SNDRV_TIMER_EVENT_MSTART)|
1563 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1564 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1565 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1566 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1567 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1568 err = -EINVAL;
1569 goto _end;
1571 snd_timer_stop(tu->timeri);
1572 spin_lock_irq(&t->lock);
1573 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1574 SNDRV_TIMER_IFLG_EXCLUSIVE|
1575 SNDRV_TIMER_IFLG_EARLY_EVENT);
1576 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1577 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1578 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1579 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1580 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1581 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1582 spin_unlock_irq(&t->lock);
1583 if (params.queue_size > 0 && (unsigned int)tu->queue_size != params.queue_size) {
1584 if (tu->tread) {
1585 ttr = (snd_timer_tread_t *)kmalloc(params.queue_size * sizeof(snd_timer_tread_t), GFP_KERNEL);
1586 if (ttr) {
1587 kfree(tu->tqueue);
1588 tu->queue_size = params.queue_size;
1589 tu->tqueue = ttr;
1591 } else {
1592 tr = (snd_timer_read_t *)kmalloc(params.queue_size * sizeof(snd_timer_read_t), GFP_KERNEL);
1593 if (tr) {
1594 kfree(tu->queue);
1595 tu->queue_size = params.queue_size;
1596 tu->queue = tr;
1600 tu->qhead = tu->qtail = tu->qused = 0;
1601 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1602 if (tu->tread) {
1603 snd_timer_tread_t tread;
1604 tread.event = SNDRV_TIMER_EVENT_EARLY;
1605 tread.tstamp.tv_sec = 0;
1606 tread.tstamp.tv_nsec = 0;
1607 tread.val = 0;
1608 snd_timer_user_append_to_tqueue(tu, &tread);
1609 } else {
1610 snd_timer_read_t *r = &tu->queue[0];
1611 r->resolution = 0;
1612 r->ticks = 0;
1613 tu->qused++;
1614 tu->qtail++;
1618 tu->filter = params.filter;
1619 tu->ticks = params.ticks;
1620 err = 0;
1621 _end:
1622 if (copy_to_user(_params, &params, sizeof(params)))
1623 return -EFAULT;
1624 return err;
1627 static int snd_timer_user_status(struct file *file, snd_timer_status_t __user *_status)
1629 snd_timer_user_t *tu;
1630 snd_timer_status_t status;
1632 tu = file->private_data;
1633 snd_assert(tu->timeri != NULL, return -ENXIO);
1634 memset(&status, 0, sizeof(status));
1635 status.tstamp = tu->tstamp;
1636 status.resolution = snd_timer_resolution(tu->timeri);
1637 status.lost = tu->timeri->lost;
1638 status.overrun = tu->overrun;
1639 spin_lock_irq(&tu->qlock);
1640 status.queue = tu->qused;
1641 spin_unlock_irq(&tu->qlock);
1642 if (copy_to_user(_status, &status, sizeof(status)))
1643 return -EFAULT;
1644 return 0;
1647 static int snd_timer_user_start(struct file *file)
1649 int err;
1650 snd_timer_user_t *tu;
1652 tu = file->private_data;
1653 snd_assert(tu->timeri != NULL, return -ENXIO);
1654 snd_timer_stop(tu->timeri);
1655 tu->timeri->lost = 0;
1656 tu->last_resolution = 0;
1657 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1660 static int snd_timer_user_stop(struct file *file)
1662 int err;
1663 snd_timer_user_t *tu;
1665 tu = file->private_data;
1666 snd_assert(tu->timeri != NULL, return -ENXIO);
1667 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1670 static int snd_timer_user_continue(struct file *file)
1672 int err;
1673 snd_timer_user_t *tu;
1675 tu = file->private_data;
1676 snd_assert(tu->timeri != NULL, return -ENXIO);
1677 tu->timeri->lost = 0;
1678 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1681 static int snd_timer_user_pause(struct file *file)
1683 int err;
1684 snd_timer_user_t *tu;
1686 tu = file->private_data;
1687 snd_assert(tu->timeri != NULL, return -ENXIO);
1688 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1691 enum {
1692 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1693 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1694 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1695 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1698 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1700 snd_timer_user_t *tu;
1701 void __user *argp = (void __user *)arg;
1702 int __user *p = argp;
1704 tu = file->private_data;
1705 switch (cmd) {
1706 case SNDRV_TIMER_IOCTL_PVERSION:
1707 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1708 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1709 return snd_timer_user_next_device(argp);
1710 case SNDRV_TIMER_IOCTL_TREAD:
1712 int xarg;
1714 down(&tu->tread_sem);
1715 if (tu->timeri) { /* too late */
1716 up(&tu->tread_sem);
1717 return -EBUSY;
1719 if (get_user(xarg, p)) {
1720 up(&tu->tread_sem);
1721 return -EFAULT;
1723 tu->tread = xarg ? 1 : 0;
1724 up(&tu->tread_sem);
1725 return 0;
1727 case SNDRV_TIMER_IOCTL_GINFO:
1728 return snd_timer_user_ginfo(file, argp);
1729 case SNDRV_TIMER_IOCTL_GPARAMS:
1730 return snd_timer_user_gparams(file, argp);
1731 case SNDRV_TIMER_IOCTL_GSTATUS:
1732 return snd_timer_user_gstatus(file, argp);
1733 case SNDRV_TIMER_IOCTL_SELECT:
1734 return snd_timer_user_tselect(file, argp);
1735 case SNDRV_TIMER_IOCTL_INFO:
1736 return snd_timer_user_info(file, argp);
1737 case SNDRV_TIMER_IOCTL_PARAMS:
1738 return snd_timer_user_params(file, argp);
1739 case SNDRV_TIMER_IOCTL_STATUS:
1740 return snd_timer_user_status(file, argp);
1741 case SNDRV_TIMER_IOCTL_START:
1742 case SNDRV_TIMER_IOCTL_START_OLD:
1743 return snd_timer_user_start(file);
1744 case SNDRV_TIMER_IOCTL_STOP:
1745 case SNDRV_TIMER_IOCTL_STOP_OLD:
1746 return snd_timer_user_stop(file);
1747 case SNDRV_TIMER_IOCTL_CONTINUE:
1748 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1749 return snd_timer_user_continue(file);
1750 case SNDRV_TIMER_IOCTL_PAUSE:
1751 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1752 return snd_timer_user_pause(file);
1754 return -ENOTTY;
1757 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1759 snd_timer_user_t *tu;
1760 int err;
1762 tu = file->private_data;
1763 err = fasync_helper(fd, file, on, &tu->fasync);
1764 if (err < 0)
1765 return err;
1766 return 0;
1769 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
1771 snd_timer_user_t *tu;
1772 long result = 0, unit;
1773 int err = 0;
1775 tu = file->private_data;
1776 unit = tu->tread ? sizeof(snd_timer_tread_t) : sizeof(snd_timer_read_t);
1777 spin_lock_irq(&tu->qlock);
1778 while ((long)count - result >= unit) {
1779 while (!tu->qused) {
1780 wait_queue_t wait;
1782 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1783 err = -EAGAIN;
1784 break;
1787 set_current_state(TASK_INTERRUPTIBLE);
1788 init_waitqueue_entry(&wait, current);
1789 add_wait_queue(&tu->qchange_sleep, &wait);
1791 spin_unlock_irq(&tu->qlock);
1792 schedule();
1793 spin_lock_irq(&tu->qlock);
1795 remove_wait_queue(&tu->qchange_sleep, &wait);
1797 if (signal_pending(current)) {
1798 err = -ERESTARTSYS;
1799 break;
1803 spin_unlock_irq(&tu->qlock);
1804 if (err < 0)
1805 goto _error;
1807 if (tu->tread) {
1808 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++], sizeof(snd_timer_tread_t))) {
1809 err = -EFAULT;
1810 goto _error;
1812 } else {
1813 if (copy_to_user(buffer, &tu->queue[tu->qhead++], sizeof(snd_timer_read_t))) {
1814 err = -EFAULT;
1815 goto _error;
1819 tu->qhead %= tu->queue_size;
1821 result += unit;
1822 buffer += unit;
1824 spin_lock_irq(&tu->qlock);
1825 tu->qused--;
1827 spin_unlock_irq(&tu->qlock);
1828 _error:
1829 return result > 0 ? result : err;
1832 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1834 unsigned int mask;
1835 snd_timer_user_t *tu;
1837 tu = file->private_data;
1839 poll_wait(file, &tu->qchange_sleep, wait);
1841 mask = 0;
1842 if (tu->qused)
1843 mask |= POLLIN | POLLRDNORM;
1845 return mask;
1848 #ifdef CONFIG_COMPAT
1849 #include "timer_compat.c"
1850 #else
1851 #define snd_timer_user_ioctl_compat NULL
1852 #endif
1854 static struct file_operations snd_timer_f_ops =
1856 .owner = THIS_MODULE,
1857 .read = snd_timer_user_read,
1858 .open = snd_timer_user_open,
1859 .release = snd_timer_user_release,
1860 .poll = snd_timer_user_poll,
1861 .unlocked_ioctl = snd_timer_user_ioctl,
1862 .compat_ioctl = snd_timer_user_ioctl_compat,
1863 .fasync = snd_timer_user_fasync,
1866 static snd_minor_t snd_timer_reg =
1868 .comment = "timer",
1869 .f_ops = &snd_timer_f_ops,
1873 * ENTRY functions
1876 static snd_info_entry_t *snd_timer_proc_entry = NULL;
1878 static int __init alsa_timer_init(void)
1880 int err;
1881 snd_info_entry_t *entry;
1883 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1884 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1, "system timer");
1885 #endif
1886 if ((entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL)) != NULL) {
1887 entry->c.text.read_size = SNDRV_TIMER_DEVICES * 128;
1888 entry->c.text.read = snd_timer_proc_read;
1889 if (snd_info_register(entry) < 0) {
1890 snd_info_free_entry(entry);
1891 entry = NULL;
1894 snd_timer_proc_entry = entry;
1895 if ((err = snd_timer_register_system()) < 0)
1896 snd_printk(KERN_ERR "unable to register system timer (%i)\n", err);
1897 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER,
1898 NULL, 0, &snd_timer_reg, "timer"))<0)
1899 snd_printk(KERN_ERR "unable to register timer device (%i)\n", err);
1900 return 0;
1903 static void __exit alsa_timer_exit(void)
1905 struct list_head *p, *n;
1907 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1908 /* unregister the system timer */
1909 list_for_each_safe(p, n, &snd_timer_list) {
1910 snd_timer_t *timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
1911 snd_timer_unregister(timer);
1913 if (snd_timer_proc_entry) {
1914 snd_info_unregister(snd_timer_proc_entry);
1915 snd_timer_proc_entry = NULL;
1917 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1918 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1919 #endif
1922 module_init(alsa_timer_init)
1923 module_exit(alsa_timer_exit)
1925 EXPORT_SYMBOL(snd_timer_open);
1926 EXPORT_SYMBOL(snd_timer_close);
1927 EXPORT_SYMBOL(snd_timer_resolution);
1928 EXPORT_SYMBOL(snd_timer_start);
1929 EXPORT_SYMBOL(snd_timer_stop);
1930 EXPORT_SYMBOL(snd_timer_continue);
1931 EXPORT_SYMBOL(snd_timer_pause);
1932 EXPORT_SYMBOL(snd_timer_new);
1933 EXPORT_SYMBOL(snd_timer_notify);
1934 EXPORT_SYMBOL(snd_timer_global_new);
1935 EXPORT_SYMBOL(snd_timer_global_free);
1936 EXPORT_SYMBOL(snd_timer_global_register);
1937 EXPORT_SYMBOL(snd_timer_global_unregister);
1938 EXPORT_SYMBOL(snd_timer_interrupt);