[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / sound / core / timer.c
blobfa762ca439be43573f9bd2bfaac8d361be4fea82
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 <sound/core.h>
30 #include <sound/timer.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/minors.h>
34 #include <sound/initval.h>
35 #include <linux/kmod.h>
36 #ifdef CONFIG_KERNELD
37 #include <linux/kerneld.h>
38 #endif
40 #if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
41 #define DEFAULT_TIMER_LIMIT 3
42 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
43 #define DEFAULT_TIMER_LIMIT 2
44 #else
45 #define DEFAULT_TIMER_LIMIT 1
46 #endif
48 static int timer_limit = DEFAULT_TIMER_LIMIT;
49 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
50 MODULE_DESCRIPTION("ALSA timer interface");
51 MODULE_LICENSE("GPL");
52 module_param(timer_limit, int, 0444);
53 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
55 typedef struct {
56 snd_timer_instance_t *timeri;
57 int tread; /* enhanced read with timestamps and events */
58 unsigned long ticks;
59 unsigned long overrun;
60 int qhead;
61 int qtail;
62 int qused;
63 int queue_size;
64 snd_timer_read_t *queue;
65 snd_timer_tread_t *tqueue;
66 spinlock_t qlock;
67 unsigned long last_resolution;
68 unsigned int filter;
69 struct timespec tstamp; /* trigger tstamp */
70 wait_queue_head_t qchange_sleep;
71 struct fasync_struct *fasync;
72 } snd_timer_user_t;
74 /* list of timers */
75 static LIST_HEAD(snd_timer_list);
77 /* list of slave instances */
78 static LIST_HEAD(snd_timer_slave_list);
80 /* lock for slave active lists */
81 static DEFINE_SPINLOCK(slave_active_lock);
83 static DECLARE_MUTEX(register_mutex);
85 static int snd_timer_free(snd_timer_t *timer);
86 static int snd_timer_dev_free(snd_device_t *device);
87 static int snd_timer_dev_register(snd_device_t *device);
88 static int snd_timer_dev_unregister(snd_device_t *device);
90 static void snd_timer_reschedule(snd_timer_t * timer, unsigned long ticks_left);
93 * create a timer instance with the given owner string.
94 * when timer is not NULL, increments the module counter
96 static snd_timer_instance_t *snd_timer_instance_new(char *owner, snd_timer_t *timer)
98 snd_timer_instance_t *timeri;
99 timeri = kcalloc(1, sizeof(*timeri), GFP_KERNEL);
100 if (timeri == NULL)
101 return NULL;
102 timeri->owner = snd_kmalloc_strdup(owner, GFP_KERNEL);
103 if (! timeri->owner) {
104 kfree(timeri);
105 return NULL;
107 INIT_LIST_HEAD(&timeri->open_list);
108 INIT_LIST_HEAD(&timeri->active_list);
109 INIT_LIST_HEAD(&timeri->ack_list);
110 INIT_LIST_HEAD(&timeri->slave_list_head);
111 INIT_LIST_HEAD(&timeri->slave_active_head);
113 timeri->timer = timer;
114 if (timer && timer->card && !try_module_get(timer->card->module)) {
115 kfree(timeri->owner);
116 kfree(timeri);
117 return NULL;
120 return timeri;
124 * find a timer instance from the given timer id
126 static snd_timer_t *snd_timer_find(snd_timer_id_t *tid)
128 snd_timer_t *timer = NULL;
129 struct list_head *p;
131 list_for_each(p, &snd_timer_list) {
132 timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
134 if (timer->tmr_class != tid->dev_class)
135 continue;
136 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
137 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
138 (timer->card == NULL ||
139 timer->card->number != tid->card))
140 continue;
141 if (timer->tmr_device != tid->device)
142 continue;
143 if (timer->tmr_subdevice != tid->subdevice)
144 continue;
145 return timer;
147 return NULL;
150 #ifdef CONFIG_KMOD
152 static void snd_timer_request(snd_timer_id_t *tid)
154 if (! current->fs->root)
155 return;
156 switch (tid->dev_class) {
157 case SNDRV_TIMER_CLASS_GLOBAL:
158 if (tid->device < timer_limit)
159 request_module("snd-timer-%i", tid->device);
160 break;
161 case SNDRV_TIMER_CLASS_CARD:
162 case SNDRV_TIMER_CLASS_PCM:
163 if (tid->card < snd_ecards_limit)
164 request_module("snd-card-%i", tid->card);
165 break;
166 default:
167 break;
171 #endif
174 * look for a master instance matching with the slave id of the given slave.
175 * when found, relink the open_link of the slave.
177 * call this with register_mutex down.
179 static void snd_timer_check_slave(snd_timer_instance_t *slave)
181 snd_timer_t *timer;
182 snd_timer_instance_t *master;
183 struct list_head *p, *q;
185 /* FIXME: it's really dumb to look up all entries.. */
186 list_for_each(p, &snd_timer_list) {
187 timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
188 list_for_each(q, &timer->open_list_head) {
189 master = (snd_timer_instance_t *)list_entry(q, snd_timer_instance_t, open_list);
190 if (slave->slave_class == master->slave_class &&
191 slave->slave_id == master->slave_id) {
192 list_del(&slave->open_list);
193 list_add_tail(&slave->open_list, &master->slave_list_head);
194 spin_lock_irq(&slave_active_lock);
195 slave->master = master;
196 slave->timer = master->timer;
197 spin_unlock_irq(&slave_active_lock);
198 return;
205 * look for slave instances matching with the slave id of the given master.
206 * when found, relink the open_link of slaves.
208 * call this with register_mutex down.
210 static void snd_timer_check_master(snd_timer_instance_t *master)
212 snd_timer_instance_t *slave;
213 struct list_head *p, *n;
215 /* check all pending slaves */
216 list_for_each_safe(p, n, &snd_timer_slave_list) {
217 slave = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, open_list);
218 if (slave->slave_class == master->slave_class &&
219 slave->slave_id == master->slave_id) {
220 list_del(p);
221 list_add_tail(p, &master->slave_list_head);
222 spin_lock_irq(&slave_active_lock);
223 slave->master = master;
224 slave->timer = master->timer;
225 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
226 list_add_tail(&slave->active_list, &master->slave_active_head);
227 spin_unlock_irq(&slave_active_lock);
233 * open a timer instance
234 * when opening a master, the slave id must be here given.
236 int snd_timer_open(snd_timer_instance_t **ti,
237 char *owner, snd_timer_id_t *tid,
238 unsigned int slave_id)
240 snd_timer_t *timer;
241 snd_timer_instance_t *timeri = NULL;
243 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
244 /* open a slave instance */
245 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
246 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
247 snd_printd("invalid slave class %i\n", tid->dev_sclass);
248 return -EINVAL;
250 down(&register_mutex);
251 timeri = snd_timer_instance_new(owner, NULL);
252 timeri->slave_class = tid->dev_sclass;
253 timeri->slave_id = tid->device;
254 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
255 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
256 snd_timer_check_slave(timeri);
257 up(&register_mutex);
258 *ti = timeri;
259 return 0;
262 /* open a master instance */
263 down(&register_mutex);
264 timer = snd_timer_find(tid);
265 #ifdef CONFIG_KMOD
266 if (timer == NULL) {
267 up(&register_mutex);
268 snd_timer_request(tid);
269 down(&register_mutex);
270 timer = snd_timer_find(tid);
272 #endif
273 if (timer) {
274 if (!list_empty(&timer->open_list_head)) {
275 timeri = (snd_timer_instance_t *)list_entry(timer->open_list_head.next, snd_timer_instance_t, open_list);
276 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
277 up(&register_mutex);
278 return -EBUSY;
281 timeri = snd_timer_instance_new(owner, timer);
282 if (timeri) {
283 timeri->slave_class = tid->dev_sclass;
284 timeri->slave_id = slave_id;
285 if (list_empty(&timer->open_list_head) && timer->hw.open)
286 timer->hw.open(timer);
287 list_add_tail(&timeri->open_list, &timer->open_list_head);
288 snd_timer_check_master(timeri);
290 } else {
291 up(&register_mutex);
292 return -ENODEV;
294 up(&register_mutex);
295 *ti = timeri;
296 return 0;
299 static int _snd_timer_stop(snd_timer_instance_t * timeri, int keep_flag, enum sndrv_timer_event event);
302 * close a timer instance
304 int snd_timer_close(snd_timer_instance_t * timeri)
306 snd_timer_t *timer = NULL;
307 struct list_head *p, *n;
308 snd_timer_instance_t *slave;
310 snd_assert(timeri != NULL, return -ENXIO);
312 /* force to stop the timer */
313 snd_timer_stop(timeri);
315 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
316 /* wait, until the active callback is finished */
317 spin_lock_irq(&slave_active_lock);
318 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
319 spin_unlock_irq(&slave_active_lock);
320 udelay(10);
321 spin_lock_irq(&slave_active_lock);
323 spin_unlock_irq(&slave_active_lock);
324 down(&register_mutex);
325 list_del(&timeri->open_list);
326 up(&register_mutex);
327 } else {
328 timer = timeri->timer;
329 /* wait, until the active callback is finished */
330 spin_lock_irq(&timer->lock);
331 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
332 spin_unlock_irq(&timer->lock);
333 udelay(10);
334 spin_lock_irq(&timer->lock);
336 spin_unlock_irq(&timer->lock);
337 down(&register_mutex);
338 list_del(&timeri->open_list);
339 if (timer && list_empty(&timer->open_list_head) && timer->hw.close)
340 timer->hw.close(timer);
341 /* remove slave links */
342 list_for_each_safe(p, n, &timeri->slave_list_head) {
343 slave = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, open_list);
344 spin_lock_irq(&slave_active_lock);
345 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
346 list_del(p);
347 list_add_tail(p, &snd_timer_slave_list);
348 slave->master = NULL;
349 slave->timer = NULL;
350 spin_unlock_irq(&slave_active_lock);
352 up(&register_mutex);
354 if (timeri->private_free)
355 timeri->private_free(timeri);
356 kfree(timeri->owner);
357 kfree(timeri);
358 if (timer && timer->card)
359 module_put(timer->card->module);
360 return 0;
363 unsigned long snd_timer_resolution(snd_timer_instance_t * timeri)
365 snd_timer_t * timer;
367 if (timeri == NULL)
368 return 0;
369 if ((timer = timeri->timer) != NULL) {
370 if (timer->hw.c_resolution)
371 return timer->hw.c_resolution(timer);
372 return timer->hw.resolution;
374 return 0;
377 static void snd_timer_notify1(snd_timer_instance_t *ti, enum sndrv_timer_event event)
379 snd_timer_t *timer;
380 unsigned long flags;
381 unsigned long resolution = 0;
382 snd_timer_instance_t *ts;
383 struct list_head *n;
384 struct timespec tstamp;
386 snd_timestamp_now(&tstamp, 1);
387 snd_assert(event >= SNDRV_TIMER_EVENT_START && event <= SNDRV_TIMER_EVENT_PAUSE, return);
388 if (event == SNDRV_TIMER_EVENT_START || event == SNDRV_TIMER_EVENT_CONTINUE)
389 resolution = snd_timer_resolution(ti);
390 if (ti->ccallback)
391 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
392 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
393 return;
394 timer = ti->timer;
395 if (timer == NULL)
396 return;
397 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
398 return;
399 spin_lock_irqsave(&timer->lock, flags);
400 list_for_each(n, &ti->slave_active_head) {
401 ts = (snd_timer_instance_t *)list_entry(n, snd_timer_instance_t, active_list);
402 if (ts->ccallback)
403 ts->ccallback(ti, event + 100, &tstamp, resolution);
405 spin_unlock_irqrestore(&timer->lock, flags);
408 static int snd_timer_start1(snd_timer_t *timer, snd_timer_instance_t *timeri, unsigned long sticks)
410 list_del(&timeri->active_list);
411 list_add_tail(&timeri->active_list, &timer->active_list_head);
412 if (timer->running) {
413 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
414 goto __start_now;
415 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
416 timeri->flags |= SNDRV_TIMER_IFLG_START;
417 return 1; /* delayed start */
418 } else {
419 timer->sticks = sticks;
420 timer->hw.start(timer);
421 __start_now:
422 timer->running++;
423 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
424 return 0;
428 static int snd_timer_start_slave(snd_timer_instance_t *timeri)
430 unsigned long flags;
432 spin_lock_irqsave(&slave_active_lock, flags);
433 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
434 if (timeri->master)
435 list_add_tail(&timeri->active_list, &timeri->master->slave_active_head);
436 spin_unlock_irqrestore(&slave_active_lock, flags);
437 return 1; /* delayed start */
441 * start the timer instance
443 int snd_timer_start(snd_timer_instance_t * timeri, unsigned int ticks)
445 snd_timer_t *timer;
446 int result = -EINVAL;
447 unsigned long flags;
449 if (timeri == NULL || ticks < 1)
450 return -EINVAL;
451 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
452 result = snd_timer_start_slave(timeri);
453 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
454 return result;
456 timer = timeri->timer;
457 if (timer == NULL)
458 return -EINVAL;
459 spin_lock_irqsave(&timer->lock, flags);
460 timeri->ticks = timeri->cticks = ticks;
461 timeri->pticks = 0;
462 result = snd_timer_start1(timer, timeri, ticks);
463 spin_unlock_irqrestore(&timer->lock, flags);
464 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
465 return result;
468 static int _snd_timer_stop(snd_timer_instance_t * timeri, int keep_flag, enum sndrv_timer_event event)
470 snd_timer_t *timer;
471 unsigned long flags;
473 snd_assert(timeri != NULL, return -ENXIO);
475 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
476 if (!keep_flag) {
477 spin_lock_irqsave(&slave_active_lock, flags);
478 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
479 spin_unlock_irqrestore(&slave_active_lock, flags);
481 goto __end;
483 timer = timeri->timer;
484 if (!timer)
485 return -EINVAL;
486 spin_lock_irqsave(&timer->lock, flags);
487 list_del_init(&timeri->ack_list);
488 list_del_init(&timeri->active_list);
489 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
490 !(--timer->running)) {
491 timer->hw.stop(timer);
492 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
493 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
494 snd_timer_reschedule(timer, 0);
495 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
496 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
497 timer->hw.start(timer);
501 if (!keep_flag)
502 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING|SNDRV_TIMER_IFLG_START);
503 spin_unlock_irqrestore(&timer->lock, flags);
504 __end:
505 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
506 snd_timer_notify1(timeri, event);
507 return 0;
511 * stop the timer instance.
513 * do not call this from the timer callback!
515 int snd_timer_stop(snd_timer_instance_t * timeri)
517 snd_timer_t *timer;
518 unsigned long flags;
519 int err;
521 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
522 if (err < 0)
523 return err;
524 timer = timeri->timer;
525 spin_lock_irqsave(&timer->lock, flags);
526 timeri->cticks = timeri->ticks;
527 timeri->pticks = 0;
528 spin_unlock_irqrestore(&timer->lock, flags);
529 return 0;
533 * start again.. the tick is kept.
535 int snd_timer_continue(snd_timer_instance_t * timeri)
537 snd_timer_t *timer;
538 int result = -EINVAL;
539 unsigned long flags;
541 if (timeri == NULL)
542 return result;
543 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
544 return snd_timer_start_slave(timeri);
545 timer = timeri->timer;
546 if (! timer)
547 return -EINVAL;
548 spin_lock_irqsave(&timer->lock, flags);
549 if (!timeri->cticks)
550 timeri->cticks = 1;
551 timeri->pticks = 0;
552 result = snd_timer_start1(timer, timeri, timer->sticks);
553 spin_unlock_irqrestore(&timer->lock, flags);
554 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
555 return result;
559 * pause.. remember the ticks left
561 int snd_timer_pause(snd_timer_instance_t * timeri)
563 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
567 * reschedule the timer
569 * start pending instances and check the scheduling ticks.
570 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
572 static void snd_timer_reschedule(snd_timer_t * timer, unsigned long ticks_left)
574 snd_timer_instance_t *ti;
575 unsigned long ticks = ~0UL;
576 struct list_head *p;
578 list_for_each(p, &timer->active_list_head) {
579 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, active_list);
580 if (ti->flags & SNDRV_TIMER_IFLG_START) {
581 ti->flags &= ~SNDRV_TIMER_IFLG_START;
582 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
583 timer->running++;
585 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
586 if (ticks > ti->cticks)
587 ticks = ti->cticks;
590 if (ticks == ~0UL) {
591 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
592 return;
594 if (ticks > timer->hw.ticks)
595 ticks = timer->hw.ticks;
596 if (ticks_left != ticks)
597 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
598 timer->sticks = ticks;
602 * timer tasklet
605 static void snd_timer_tasklet(unsigned long arg)
607 snd_timer_t *timer = (snd_timer_t *) arg;
608 snd_timer_instance_t *ti;
609 struct list_head *p;
610 unsigned long resolution, ticks;
612 spin_lock(&timer->lock);
613 /* now process all callbacks */
614 while (!list_empty(&timer->sack_list_head)) {
615 p = timer->sack_list_head.next; /* get first item */
616 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, ack_list);
618 /* remove from ack_list and make empty */
619 list_del_init(p);
621 ticks = ti->pticks;
622 ti->pticks = 0;
623 resolution = ti->resolution;
625 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
626 spin_unlock(&timer->lock);
627 if (ti->callback)
628 ti->callback(ti, resolution, ticks);
629 spin_lock(&timer->lock);
630 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
632 spin_unlock(&timer->lock);
636 * timer interrupt
638 * ticks_left is usually equal to timer->sticks.
641 void snd_timer_interrupt(snd_timer_t * timer, unsigned long ticks_left)
643 snd_timer_instance_t *ti, *ts;
644 unsigned long resolution, ticks;
645 struct list_head *p, *q, *n;
646 int use_tasklet = 0;
648 if (timer == NULL)
649 return;
651 spin_lock(&timer->lock);
653 /* remember the current resolution */
654 if (timer->hw.c_resolution)
655 resolution = timer->hw.c_resolution(timer);
656 else
657 resolution = timer->hw.resolution;
659 /* loop for all active instances
660 * here we cannot use list_for_each because the active_list of a processed
661 * instance is relinked to done_list_head before callback is called.
663 list_for_each_safe(p, n, &timer->active_list_head) {
664 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, active_list);
665 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
666 continue;
667 ti->pticks += ticks_left;
668 ti->resolution = resolution;
669 if (ti->cticks < ticks_left)
670 ti->cticks = 0;
671 else
672 ti->cticks -= ticks_left;
673 if (ti->cticks) /* not expired */
674 continue;
675 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
676 ti->cticks = ti->ticks;
677 } else {
678 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
679 if (--timer->running)
680 list_del(p);
682 if (list_empty(&ti->ack_list)) {
683 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
684 (ti->flags & SNDRV_TIMER_IFLG_FAST)) {
685 list_add_tail(&ti->ack_list, &timer->ack_list_head);
686 } else {
687 list_add_tail(&ti->ack_list, &timer->sack_list_head);
690 list_for_each(q, &ti->slave_active_head) {
691 ts = (snd_timer_instance_t *)list_entry(q, snd_timer_instance_t, active_list);
692 ts->pticks = ti->pticks;
693 ts->resolution = resolution;
694 if (list_empty(&ts->ack_list)) {
695 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
696 (ti->flags & SNDRV_TIMER_IFLG_FAST)) {
697 list_add_tail(&ts->ack_list, &timer->ack_list_head);
698 } else {
699 list_add_tail(&ts->ack_list, &timer->sack_list_head);
704 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
705 snd_timer_reschedule(timer, ticks_left);
706 if (timer->running) {
707 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
708 timer->hw.stop(timer);
709 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
711 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
712 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
713 /* restart timer */
714 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
715 timer->hw.start(timer);
717 } else {
718 timer->hw.stop(timer);
721 /* now process all fast callbacks */
722 while (!list_empty(&timer->ack_list_head)) {
723 p = timer->ack_list_head.next; /* get first item */
724 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, ack_list);
726 /* remove from ack_list and make empty */
727 list_del_init(p);
729 ticks = ti->pticks;
730 ti->pticks = 0;
732 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
733 spin_unlock(&timer->lock);
734 if (ti->callback)
735 ti->callback(ti, resolution, ticks);
736 spin_lock(&timer->lock);
737 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
740 /* do we have any slow callbacks? */
741 use_tasklet = !list_empty(&timer->sack_list_head);
742 spin_unlock(&timer->lock);
744 if (use_tasklet)
745 tasklet_hi_schedule(&timer->task_queue);
752 int snd_timer_new(snd_card_t *card, char *id, snd_timer_id_t *tid, snd_timer_t ** rtimer)
754 snd_timer_t *timer;
755 int err;
756 static snd_device_ops_t ops = {
757 .dev_free = snd_timer_dev_free,
758 .dev_register = snd_timer_dev_register,
759 .dev_unregister = snd_timer_dev_unregister
762 snd_assert(tid != NULL, return -EINVAL);
763 snd_assert(rtimer != NULL, return -EINVAL);
764 *rtimer = NULL;
765 timer = kcalloc(1, sizeof(*timer), GFP_KERNEL);
766 if (timer == NULL)
767 return -ENOMEM;
768 timer->tmr_class = tid->dev_class;
769 timer->card = card;
770 timer->tmr_device = tid->device;
771 timer->tmr_subdevice = tid->subdevice;
772 if (id)
773 strlcpy(timer->id, id, sizeof(timer->id));
774 INIT_LIST_HEAD(&timer->device_list);
775 INIT_LIST_HEAD(&timer->open_list_head);
776 INIT_LIST_HEAD(&timer->active_list_head);
777 INIT_LIST_HEAD(&timer->ack_list_head);
778 INIT_LIST_HEAD(&timer->sack_list_head);
779 spin_lock_init(&timer->lock);
780 tasklet_init(&timer->task_queue, snd_timer_tasklet, (unsigned long)timer);
781 if (card != NULL) {
782 if ((err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops)) < 0) {
783 snd_timer_free(timer);
784 return err;
787 *rtimer = timer;
788 return 0;
791 static int snd_timer_free(snd_timer_t *timer)
793 snd_assert(timer != NULL, return -ENXIO);
794 if (timer->private_free)
795 timer->private_free(timer);
796 kfree(timer);
797 return 0;
800 int snd_timer_dev_free(snd_device_t *device)
802 snd_timer_t *timer = device->device_data;
803 return snd_timer_free(timer);
806 int snd_timer_dev_register(snd_device_t *dev)
808 snd_timer_t *timer = dev->device_data;
809 snd_timer_t *timer1;
810 struct list_head *p;
812 snd_assert(timer != NULL && timer->hw.start != NULL && timer->hw.stop != NULL, return -ENXIO);
813 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
814 !timer->hw.resolution && timer->hw.c_resolution == NULL)
815 return -EINVAL;
817 down(&register_mutex);
818 list_for_each(p, &snd_timer_list) {
819 timer1 = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
820 if (timer1->tmr_class > timer->tmr_class)
821 break;
822 if (timer1->tmr_class < timer->tmr_class)
823 continue;
824 if (timer1->card && timer->card) {
825 if (timer1->card->number > timer->card->number)
826 break;
827 if (timer1->card->number < timer->card->number)
828 continue;
830 if (timer1->tmr_device > timer->tmr_device)
831 break;
832 if (timer1->tmr_device < timer->tmr_device)
833 continue;
834 if (timer1->tmr_subdevice > timer->tmr_subdevice)
835 break;
836 if (timer1->tmr_subdevice < timer->tmr_subdevice)
837 continue;
838 /* conflicts.. */
839 up(&register_mutex);
840 return -EBUSY;
842 list_add_tail(&timer->device_list, p);
843 up(&register_mutex);
844 return 0;
847 int snd_timer_unregister(snd_timer_t *timer)
849 struct list_head *p, *n;
850 snd_timer_instance_t *ti;
852 snd_assert(timer != NULL, return -ENXIO);
853 down(&register_mutex);
854 if (! list_empty(&timer->open_list_head)) {
855 snd_printk(KERN_WARNING "timer 0x%lx is busy?\n", (long)timer);
856 list_for_each_safe(p, n, &timer->open_list_head) {
857 list_del_init(p);
858 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, open_list);
859 ti->timer = NULL;
862 list_del(&timer->device_list);
863 up(&register_mutex);
864 return snd_timer_free(timer);
867 static int snd_timer_dev_unregister(snd_device_t *device)
869 snd_timer_t *timer = device->device_data;
870 return snd_timer_unregister(timer);
873 void snd_timer_notify(snd_timer_t *timer, enum sndrv_timer_event event, struct timespec *tstamp)
875 unsigned long flags;
876 unsigned long resolution = 0;
877 snd_timer_instance_t *ti, *ts;
878 struct list_head *p, *n;
880 snd_runtime_check(timer->hw.flags & SNDRV_TIMER_HW_SLAVE, return);
881 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART && event <= SNDRV_TIMER_EVENT_MPAUSE, return);
882 spin_lock_irqsave(&timer->lock, flags);
883 if (event == SNDRV_TIMER_EVENT_MSTART || event == SNDRV_TIMER_EVENT_MCONTINUE) {
884 if (timer->hw.c_resolution)
885 resolution = timer->hw.c_resolution(timer);
886 else
887 resolution = timer->hw.resolution;
889 list_for_each(p, &timer->active_list_head) {
890 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, active_list);
891 if (ti->ccallback)
892 ti->ccallback(ti, event, tstamp, resolution);
893 list_for_each(n, &ti->slave_active_head) {
894 ts = (snd_timer_instance_t *)list_entry(n, snd_timer_instance_t, active_list);
895 if (ts->ccallback)
896 ts->ccallback(ts, event, tstamp, resolution);
899 spin_unlock_irqrestore(&timer->lock, flags);
903 * exported functions for global timers
905 int snd_timer_global_new(char *id, int device, snd_timer_t **rtimer)
907 snd_timer_id_t tid;
909 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
910 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
911 tid.card = -1;
912 tid.device = device;
913 tid.subdevice = 0;
914 return snd_timer_new(NULL, id, &tid, rtimer);
917 int snd_timer_global_free(snd_timer_t *timer)
919 return snd_timer_free(timer);
922 int snd_timer_global_register(snd_timer_t *timer)
924 snd_device_t dev;
926 memset(&dev, 0, sizeof(dev));
927 dev.device_data = timer;
928 return snd_timer_dev_register(&dev);
931 int snd_timer_global_unregister(snd_timer_t *timer)
933 return snd_timer_unregister(timer);
937 * System timer
940 struct snd_timer_system_private {
941 struct timer_list tlist;
942 struct timer * timer;
943 unsigned long last_expires;
944 unsigned long last_jiffies;
945 unsigned long correction;
948 unsigned int snd_timer_system_resolution(void)
950 return 1000000000L / HZ;
953 static void snd_timer_s_function(unsigned long data)
955 snd_timer_t *timer = (snd_timer_t *)data;
956 struct snd_timer_system_private *priv = timer->private_data;
957 unsigned long jiff = jiffies;
958 if (time_after(jiff, priv->last_expires))
959 priv->correction = (long)jiff - (long)priv->last_expires;
960 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
963 static int snd_timer_s_start(snd_timer_t * timer)
965 struct snd_timer_system_private *priv;
966 unsigned long njiff;
968 priv = (struct snd_timer_system_private *) timer->private_data;
969 njiff = (priv->last_jiffies = jiffies);
970 if (priv->correction > timer->sticks - 1) {
971 priv->correction -= timer->sticks - 1;
972 njiff++;
973 } else {
974 njiff += timer->sticks - priv->correction;
975 priv->correction -= timer->sticks;
977 priv->last_expires = priv->tlist.expires = njiff;
978 add_timer(&priv->tlist);
979 return 0;
982 static int snd_timer_s_stop(snd_timer_t * timer)
984 struct snd_timer_system_private *priv;
985 unsigned long jiff;
987 priv = (struct snd_timer_system_private *) timer->private_data;
988 del_timer(&priv->tlist);
989 jiff = jiffies;
990 if (time_before(jiff, priv->last_expires))
991 timer->sticks = priv->last_expires - jiff;
992 else
993 timer->sticks = 1;
994 return 0;
997 static struct _snd_timer_hardware snd_timer_system =
999 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1000 .resolution = 1000000000L / HZ,
1001 .ticks = 10000000L,
1002 .start = snd_timer_s_start,
1003 .stop = snd_timer_s_stop
1006 static void snd_timer_free_system(snd_timer_t *timer)
1008 kfree(timer->private_data);
1011 static int snd_timer_register_system(void)
1013 snd_timer_t *timer;
1014 struct snd_timer_system_private *priv;
1015 int err;
1017 if ((err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer)) < 0)
1018 return err;
1019 strcpy(timer->name, "system timer");
1020 timer->hw = snd_timer_system;
1021 priv = kcalloc(1, sizeof(*priv), GFP_KERNEL);
1022 if (priv == NULL) {
1023 snd_timer_free(timer);
1024 return -ENOMEM;
1026 init_timer(&priv->tlist);
1027 priv->tlist.function = snd_timer_s_function;
1028 priv->tlist.data = (unsigned long) timer;
1029 timer->private_data = priv;
1030 timer->private_free = snd_timer_free_system;
1031 return snd_timer_global_register(timer);
1035 * Info interface
1038 static void snd_timer_proc_read(snd_info_entry_t *entry,
1039 snd_info_buffer_t * buffer)
1041 unsigned long flags;
1042 snd_timer_t *timer;
1043 snd_timer_instance_t *ti;
1044 struct list_head *p, *q;
1046 down(&register_mutex);
1047 list_for_each(p, &snd_timer_list) {
1048 timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
1049 switch (timer->tmr_class) {
1050 case SNDRV_TIMER_CLASS_GLOBAL:
1051 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1052 break;
1053 case SNDRV_TIMER_CLASS_CARD:
1054 snd_iprintf(buffer, "C%i-%i: ", timer->card->number, timer->tmr_device);
1055 break;
1056 case SNDRV_TIMER_CLASS_PCM:
1057 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number, timer->tmr_device, timer->tmr_subdevice);
1058 break;
1059 default:
1060 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class, timer->card ? timer->card->number : -1, timer->tmr_device, timer->tmr_subdevice);
1062 snd_iprintf(buffer, "%s :", timer->name);
1063 if (timer->hw.resolution)
1064 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)", timer->hw.resolution / 1000, timer->hw.resolution % 1000, timer->hw.ticks);
1065 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1066 snd_iprintf(buffer, " SLAVE");
1067 snd_iprintf(buffer, "\n");
1068 spin_lock_irqsave(&timer->lock, flags);
1069 list_for_each(q, &timer->open_list_head) {
1070 ti = (snd_timer_instance_t *)list_entry(q, snd_timer_instance_t, open_list);
1071 snd_iprintf(buffer, " Client %s : %s : lost interrupts %li\n",
1072 ti->owner ? ti->owner : "unknown",
1073 ti->flags & (SNDRV_TIMER_IFLG_START|SNDRV_TIMER_IFLG_RUNNING) ? "running" : "stopped",
1074 ti->lost);
1076 spin_unlock_irqrestore(&timer->lock, flags);
1078 up(&register_mutex);
1082 * USER SPACE interface
1085 static void snd_timer_user_interrupt(snd_timer_instance_t *timeri,
1086 unsigned long resolution,
1087 unsigned long ticks)
1089 snd_timer_user_t *tu = timeri->callback_data;
1090 snd_timer_read_t *r;
1091 int prev;
1093 spin_lock(&tu->qlock);
1094 if (tu->qused > 0) {
1095 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1096 r = &tu->queue[prev];
1097 if (r->resolution == resolution) {
1098 r->ticks += ticks;
1099 goto __wake;
1102 if (tu->qused >= tu->queue_size) {
1103 tu->overrun++;
1104 } else {
1105 r = &tu->queue[tu->qtail++];
1106 tu->qtail %= tu->queue_size;
1107 r->resolution = resolution;
1108 r->ticks = ticks;
1109 tu->qused++;
1111 __wake:
1112 spin_unlock(&tu->qlock);
1113 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1114 wake_up(&tu->qchange_sleep);
1117 static void snd_timer_user_append_to_tqueue(snd_timer_user_t *tu, snd_timer_tread_t *tread)
1119 if (tu->qused >= tu->queue_size) {
1120 tu->overrun++;
1121 } else {
1122 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1123 tu->qtail %= tu->queue_size;
1124 tu->qused++;
1128 static void snd_timer_user_ccallback(snd_timer_instance_t *timeri,
1129 enum sndrv_timer_event event,
1130 struct timespec *tstamp,
1131 unsigned long resolution)
1133 snd_timer_user_t *tu = timeri->callback_data;
1134 snd_timer_tread_t r1;
1136 if (event >= SNDRV_TIMER_EVENT_START && event <= SNDRV_TIMER_EVENT_PAUSE)
1137 tu->tstamp = *tstamp;
1138 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1139 return;
1140 r1.event = event;
1141 r1.tstamp = *tstamp;
1142 r1.val = resolution;
1143 spin_lock(&tu->qlock);
1144 snd_timer_user_append_to_tqueue(tu, &r1);
1145 spin_unlock(&tu->qlock);
1146 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1147 wake_up(&tu->qchange_sleep);
1150 static void snd_timer_user_tinterrupt(snd_timer_instance_t *timeri,
1151 unsigned long resolution,
1152 unsigned long ticks)
1154 snd_timer_user_t *tu = timeri->callback_data;
1155 snd_timer_tread_t *r, r1;
1156 struct timespec tstamp;
1157 int prev, append = 0;
1159 snd_timestamp_zero(&tstamp);
1160 spin_lock(&tu->qlock);
1161 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION)|(1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1162 spin_unlock(&tu->qlock);
1163 return;
1165 if (tu->last_resolution != resolution || ticks > 0)
1166 snd_timestamp_now(&tstamp, 1);
1167 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) && tu->last_resolution != resolution) {
1168 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1169 r1.tstamp = tstamp;
1170 r1.val = resolution;
1171 snd_timer_user_append_to_tqueue(tu, &r1);
1172 tu->last_resolution = resolution;
1173 append++;
1175 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1176 goto __wake;
1177 if (ticks == 0)
1178 goto __wake;
1179 if (tu->qused > 0) {
1180 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1181 r = &tu->tqueue[prev];
1182 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1183 r->tstamp = tstamp;
1184 r->val += ticks;
1185 append++;
1186 goto __wake;
1189 r1.event = SNDRV_TIMER_EVENT_TICK;
1190 r1.tstamp = tstamp;
1191 r1.val = ticks;
1192 snd_timer_user_append_to_tqueue(tu, &r1);
1193 append++;
1194 __wake:
1195 spin_unlock(&tu->qlock);
1196 if (append == 0)
1197 return;
1198 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1199 wake_up(&tu->qchange_sleep);
1202 static int snd_timer_user_open(struct inode *inode, struct file *file)
1204 snd_timer_user_t *tu;
1206 tu = kcalloc(1, sizeof(*tu), GFP_KERNEL);
1207 if (tu == NULL)
1208 return -ENOMEM;
1209 spin_lock_init(&tu->qlock);
1210 init_waitqueue_head(&tu->qchange_sleep);
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;
1459 tu = file->private_data;
1460 if (tu->timeri)
1461 snd_timer_close(tu->timeri);
1462 if (copy_from_user(&tselect, _tselect, sizeof(tselect)))
1463 return -EFAULT;
1464 sprintf(str, "application %i", current->pid);
1465 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1466 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1467 if ((err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid)) < 0)
1468 return err;
1470 if (tu->queue) {
1471 kfree(tu->queue);
1472 tu->queue = NULL;
1474 if (tu->tqueue) {
1475 kfree(tu->tqueue);
1476 tu->tqueue = NULL;
1478 if (tu->tread) {
1479 tu->tqueue = (snd_timer_tread_t *)kmalloc(tu->queue_size * sizeof(snd_timer_tread_t), GFP_KERNEL);
1480 if (tu->tqueue == NULL) {
1481 snd_timer_close(tu->timeri);
1482 return -ENOMEM;
1484 } else {
1485 tu->queue = (snd_timer_read_t *)kmalloc(tu->queue_size * sizeof(snd_timer_read_t), GFP_KERNEL);
1486 if (tu->queue == NULL) {
1487 snd_timer_close(tu->timeri);
1488 return -ENOMEM;
1492 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1493 tu->timeri->callback = tu->tread ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1494 tu->timeri->ccallback = snd_timer_user_ccallback;
1495 tu->timeri->callback_data = (void *)tu;
1496 return 0;
1499 static int snd_timer_user_info(struct file *file, snd_timer_info_t __user *_info)
1501 snd_timer_user_t *tu;
1502 snd_timer_info_t *info;
1503 snd_timer_t *t;
1504 int err = 0;
1506 tu = file->private_data;
1507 snd_assert(tu->timeri != NULL, return -ENXIO);
1508 t = tu->timeri->timer;
1509 snd_assert(t != NULL, return -ENXIO);
1511 info = kcalloc(1, sizeof(*info), GFP_KERNEL);
1512 if (! info)
1513 return -ENOMEM;
1514 info->card = t->card ? t->card->number : -1;
1515 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1516 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1517 strlcpy(info->id, t->id, sizeof(info->id));
1518 strlcpy(info->name, t->name, sizeof(info->name));
1519 info->resolution = t->hw.resolution;
1520 if (copy_to_user(_info, info, sizeof(*_info)))
1521 err = -EFAULT;
1522 kfree(info);
1523 return err;
1526 static int snd_timer_user_params(struct file *file, snd_timer_params_t __user *_params)
1528 snd_timer_user_t *tu;
1529 snd_timer_params_t params;
1530 snd_timer_t *t;
1531 snd_timer_read_t *tr;
1532 snd_timer_tread_t *ttr;
1533 int err;
1535 tu = file->private_data;
1536 snd_assert(tu->timeri != NULL, return -ENXIO);
1537 t = tu->timeri->timer;
1538 snd_assert(t != NULL, return -ENXIO);
1539 if (copy_from_user(&params, _params, sizeof(params)))
1540 return -EFAULT;
1541 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1542 err = -EINVAL;
1543 goto _end;
1545 if (params.queue_size > 0 && (params.queue_size < 32 || params.queue_size > 1024)) {
1546 err = -EINVAL;
1547 goto _end;
1549 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1550 (1<<SNDRV_TIMER_EVENT_TICK)|
1551 (1<<SNDRV_TIMER_EVENT_START)|
1552 (1<<SNDRV_TIMER_EVENT_STOP)|
1553 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1554 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1555 (1<<SNDRV_TIMER_EVENT_MSTART)|
1556 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1557 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1558 (1<<SNDRV_TIMER_EVENT_MPAUSE))) {
1559 err = -EINVAL;
1560 goto _end;
1562 snd_timer_stop(tu->timeri);
1563 spin_lock_irq(&t->lock);
1564 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1565 SNDRV_TIMER_IFLG_EXCLUSIVE|
1566 SNDRV_TIMER_IFLG_EARLY_EVENT);
1567 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1568 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1569 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1570 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1571 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1572 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1573 spin_unlock_irq(&t->lock);
1574 if (params.queue_size > 0 && (unsigned int)tu->queue_size != params.queue_size) {
1575 if (tu->tread) {
1576 ttr = (snd_timer_tread_t *)kmalloc(params.queue_size * sizeof(snd_timer_tread_t), GFP_KERNEL);
1577 if (ttr) {
1578 kfree(tu->tqueue);
1579 tu->queue_size = params.queue_size;
1580 tu->tqueue = ttr;
1582 } else {
1583 tr = (snd_timer_read_t *)kmalloc(params.queue_size * sizeof(snd_timer_read_t), GFP_KERNEL);
1584 if (tr) {
1585 kfree(tu->queue);
1586 tu->queue_size = params.queue_size;
1587 tu->queue = tr;
1591 tu->qhead = tu->qtail = tu->qused = 0;
1592 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1593 if (tu->tread) {
1594 snd_timer_tread_t tread;
1595 tread.event = SNDRV_TIMER_EVENT_EARLY;
1596 tread.tstamp.tv_sec = 0;
1597 tread.tstamp.tv_nsec = 0;
1598 tread.val = 0;
1599 snd_timer_user_append_to_tqueue(tu, &tread);
1600 } else {
1601 snd_timer_read_t *r = &tu->queue[0];
1602 r->resolution = 0;
1603 r->ticks = 0;
1604 tu->qused++;
1605 tu->qtail++;
1609 tu->filter = params.filter;
1610 tu->ticks = params.ticks;
1611 err = 0;
1612 _end:
1613 if (copy_to_user(_params, &params, sizeof(params)))
1614 return -EFAULT;
1615 return err;
1618 static int snd_timer_user_status(struct file *file, snd_timer_status_t __user *_status)
1620 snd_timer_user_t *tu;
1621 snd_timer_status_t status;
1623 tu = file->private_data;
1624 snd_assert(tu->timeri != NULL, return -ENXIO);
1625 memset(&status, 0, sizeof(status));
1626 status.tstamp = tu->tstamp;
1627 status.resolution = snd_timer_resolution(tu->timeri);
1628 status.lost = tu->timeri->lost;
1629 status.overrun = tu->overrun;
1630 spin_lock_irq(&tu->qlock);
1631 status.queue = tu->qused;
1632 spin_unlock_irq(&tu->qlock);
1633 if (copy_to_user(_status, &status, sizeof(status)))
1634 return -EFAULT;
1635 return 0;
1638 static int snd_timer_user_start(struct file *file)
1640 int err;
1641 snd_timer_user_t *tu;
1643 tu = file->private_data;
1644 snd_assert(tu->timeri != NULL, return -ENXIO);
1645 snd_timer_stop(tu->timeri);
1646 tu->timeri->lost = 0;
1647 tu->last_resolution = 0;
1648 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1651 static int snd_timer_user_stop(struct file *file)
1653 int err;
1654 snd_timer_user_t *tu;
1656 tu = file->private_data;
1657 snd_assert(tu->timeri != NULL, return -ENXIO);
1658 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1661 static int snd_timer_user_continue(struct file *file)
1663 int err;
1664 snd_timer_user_t *tu;
1666 tu = file->private_data;
1667 snd_assert(tu->timeri != NULL, return -ENXIO);
1668 tu->timeri->lost = 0;
1669 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1672 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1674 snd_timer_user_t *tu;
1675 void __user *argp = (void __user *)arg;
1676 int __user *p = argp;
1678 tu = file->private_data;
1679 switch (cmd) {
1680 case SNDRV_TIMER_IOCTL_PVERSION:
1681 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1682 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1683 return snd_timer_user_next_device(argp);
1684 case SNDRV_TIMER_IOCTL_TREAD:
1686 int xarg;
1688 if (tu->timeri) /* too late */
1689 return -EBUSY;
1690 if (get_user(xarg, p))
1691 return -EFAULT;
1692 tu->tread = xarg ? 1 : 0;
1693 return 0;
1695 case SNDRV_TIMER_IOCTL_GINFO:
1696 return snd_timer_user_ginfo(file, argp);
1697 case SNDRV_TIMER_IOCTL_GPARAMS:
1698 return snd_timer_user_gparams(file, argp);
1699 case SNDRV_TIMER_IOCTL_GSTATUS:
1700 return snd_timer_user_gstatus(file, argp);
1701 case SNDRV_TIMER_IOCTL_SELECT:
1702 return snd_timer_user_tselect(file, argp);
1703 case SNDRV_TIMER_IOCTL_INFO:
1704 return snd_timer_user_info(file, argp);
1705 case SNDRV_TIMER_IOCTL_PARAMS:
1706 return snd_timer_user_params(file, argp);
1707 case SNDRV_TIMER_IOCTL_STATUS:
1708 return snd_timer_user_status(file, argp);
1709 case SNDRV_TIMER_IOCTL_START:
1710 return snd_timer_user_start(file);
1711 case SNDRV_TIMER_IOCTL_STOP:
1712 return snd_timer_user_stop(file);
1713 case SNDRV_TIMER_IOCTL_CONTINUE:
1714 return snd_timer_user_continue(file);
1716 return -ENOTTY;
1719 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1721 snd_timer_user_t *tu;
1722 int err;
1724 tu = file->private_data;
1725 err = fasync_helper(fd, file, on, &tu->fasync);
1726 if (err < 0)
1727 return err;
1728 return 0;
1731 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
1733 snd_timer_user_t *tu;
1734 long result = 0, unit;
1735 int err = 0;
1737 tu = file->private_data;
1738 unit = tu->tread ? sizeof(snd_timer_tread_t) : sizeof(snd_timer_read_t);
1739 spin_lock_irq(&tu->qlock);
1740 while ((long)count - result >= unit) {
1741 while (!tu->qused) {
1742 wait_queue_t wait;
1744 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1745 err = -EAGAIN;
1746 break;
1749 set_current_state(TASK_INTERRUPTIBLE);
1750 init_waitqueue_entry(&wait, current);
1751 add_wait_queue(&tu->qchange_sleep, &wait);
1753 spin_unlock_irq(&tu->qlock);
1754 schedule();
1755 spin_lock_irq(&tu->qlock);
1757 remove_wait_queue(&tu->qchange_sleep, &wait);
1759 if (signal_pending(current)) {
1760 err = -ERESTARTSYS;
1761 break;
1765 spin_unlock_irq(&tu->qlock);
1766 if (err < 0)
1767 goto _error;
1769 if (tu->tread) {
1770 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++], sizeof(snd_timer_tread_t))) {
1771 err = -EFAULT;
1772 goto _error;
1774 } else {
1775 if (copy_to_user(buffer, &tu->queue[tu->qhead++], sizeof(snd_timer_read_t))) {
1776 err = -EFAULT;
1777 goto _error;
1781 tu->qhead %= tu->queue_size;
1783 result += unit;
1784 buffer += unit;
1786 spin_lock_irq(&tu->qlock);
1787 tu->qused--;
1789 spin_unlock_irq(&tu->qlock);
1790 _error:
1791 return result > 0 ? result : err;
1794 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1796 unsigned int mask;
1797 snd_timer_user_t *tu;
1799 tu = file->private_data;
1801 poll_wait(file, &tu->qchange_sleep, wait);
1803 mask = 0;
1804 if (tu->qused)
1805 mask |= POLLIN | POLLRDNORM;
1807 return mask;
1810 #ifdef CONFIG_COMPAT
1811 #include "timer_compat.c"
1812 #else
1813 #define snd_timer_user_ioctl_compat NULL
1814 #endif
1816 static struct file_operations snd_timer_f_ops =
1818 .owner = THIS_MODULE,
1819 .read = snd_timer_user_read,
1820 .open = snd_timer_user_open,
1821 .release = snd_timer_user_release,
1822 .poll = snd_timer_user_poll,
1823 .unlocked_ioctl = snd_timer_user_ioctl,
1824 .compat_ioctl = snd_timer_user_ioctl_compat,
1825 .fasync = snd_timer_user_fasync,
1828 static snd_minor_t snd_timer_reg =
1830 .comment = "timer",
1831 .f_ops = &snd_timer_f_ops,
1835 * ENTRY functions
1838 static snd_info_entry_t *snd_timer_proc_entry = NULL;
1840 static int __init alsa_timer_init(void)
1842 int err;
1843 snd_info_entry_t *entry;
1845 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1846 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1, "system timer");
1847 #endif
1848 if ((entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL)) != NULL) {
1849 entry->c.text.read_size = SNDRV_TIMER_DEVICES * 128;
1850 entry->c.text.read = snd_timer_proc_read;
1851 if (snd_info_register(entry) < 0) {
1852 snd_info_free_entry(entry);
1853 entry = NULL;
1856 snd_timer_proc_entry = entry;
1857 if ((err = snd_timer_register_system()) < 0)
1858 snd_printk(KERN_ERR "unable to register system timer (%i)\n", err);
1859 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER,
1860 NULL, 0, &snd_timer_reg, "timer"))<0)
1861 snd_printk(KERN_ERR "unable to register timer device (%i)\n", err);
1862 return 0;
1865 static void __exit alsa_timer_exit(void)
1867 struct list_head *p, *n;
1869 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1870 /* unregister the system timer */
1871 list_for_each_safe(p, n, &snd_timer_list) {
1872 snd_timer_t *timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
1873 snd_timer_unregister(timer);
1875 if (snd_timer_proc_entry) {
1876 snd_info_unregister(snd_timer_proc_entry);
1877 snd_timer_proc_entry = NULL;
1879 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1880 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1881 #endif
1884 module_init(alsa_timer_init)
1885 module_exit(alsa_timer_exit)
1887 EXPORT_SYMBOL(snd_timer_open);
1888 EXPORT_SYMBOL(snd_timer_close);
1889 EXPORT_SYMBOL(snd_timer_resolution);
1890 EXPORT_SYMBOL(snd_timer_start);
1891 EXPORT_SYMBOL(snd_timer_stop);
1892 EXPORT_SYMBOL(snd_timer_continue);
1893 EXPORT_SYMBOL(snd_timer_pause);
1894 EXPORT_SYMBOL(snd_timer_new);
1895 EXPORT_SYMBOL(snd_timer_notify);
1896 EXPORT_SYMBOL(snd_timer_global_new);
1897 EXPORT_SYMBOL(snd_timer_global_free);
1898 EXPORT_SYMBOL(snd_timer_global_register);
1899 EXPORT_SYMBOL(snd_timer_global_unregister);
1900 EXPORT_SYMBOL(snd_timer_interrupt);
1901 EXPORT_SYMBOL(snd_timer_system_resolution);