RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / core / timer.c
blobb732f91536048dbb3819401a95ce760dcc50ed48
1 /*
2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/mutex.h>
27 #include <linux/moduleparam.h>
28 #include <linux/string.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>
37 #if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
38 #define DEFAULT_TIMER_LIMIT 3
39 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
40 #define DEFAULT_TIMER_LIMIT 2
41 #else
42 #define DEFAULT_TIMER_LIMIT 1
43 #endif
45 static int timer_limit = DEFAULT_TIMER_LIMIT;
46 static int timer_tstamp_monotonic = 1;
47 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
48 MODULE_DESCRIPTION("ALSA timer interface");
49 MODULE_LICENSE("GPL");
50 module_param(timer_limit, int, 0444);
51 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
52 module_param(timer_tstamp_monotonic, int, 0444);
53 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
55 struct snd_timer_user {
56 struct snd_timer_instance *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 struct snd_timer_read *queue;
65 struct snd_timer_tread *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 struct mutex tread_sem;
75 /* list of timers */
76 static LIST_HEAD(snd_timer_list);
78 /* list of slave instances */
79 static LIST_HEAD(snd_timer_slave_list);
81 /* lock for slave active lists */
82 static DEFINE_SPINLOCK(slave_active_lock);
84 static DEFINE_MUTEX(register_mutex);
86 static int snd_timer_free(struct snd_timer *timer);
87 static int snd_timer_dev_free(struct snd_device *device);
88 static int snd_timer_dev_register(struct snd_device *device);
89 static int snd_timer_dev_disconnect(struct snd_device *device);
91 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
94 * create a timer instance with the given owner string.
95 * when timer is not NULL, increments the module counter
97 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
98 struct snd_timer *timer)
100 struct snd_timer_instance *timeri;
101 timeri = kzalloc(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 && !try_module_get(timer->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 struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
130 struct snd_timer *timer = NULL;
132 list_for_each_entry(timer, &snd_timer_list, device_list) {
133 if (timer->tmr_class != tid->dev_class)
134 continue;
135 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
136 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
137 (timer->card == NULL ||
138 timer->card->number != tid->card))
139 continue;
140 if (timer->tmr_device != tid->device)
141 continue;
142 if (timer->tmr_subdevice != tid->subdevice)
143 continue;
144 return timer;
146 return NULL;
149 #ifdef CONFIG_MODULES
151 static void snd_timer_request(struct snd_timer_id *tid)
153 switch (tid->dev_class) {
154 case SNDRV_TIMER_CLASS_GLOBAL:
155 if (tid->device < timer_limit)
156 request_module("snd-timer-%i", tid->device);
157 break;
158 case SNDRV_TIMER_CLASS_CARD:
159 case SNDRV_TIMER_CLASS_PCM:
160 if (tid->card < snd_ecards_limit)
161 request_module("snd-card-%i", tid->card);
162 break;
163 default:
164 break;
168 #endif
171 * look for a master instance matching with the slave id of the given slave.
172 * when found, relink the open_link of the slave.
174 * call this with register_mutex down.
176 static void snd_timer_check_slave(struct snd_timer_instance *slave)
178 struct snd_timer *timer;
179 struct snd_timer_instance *master;
181 list_for_each_entry(timer, &snd_timer_list, device_list) {
182 list_for_each_entry(master, &timer->open_list_head, open_list) {
183 if (slave->slave_class == master->slave_class &&
184 slave->slave_id == master->slave_id) {
185 list_del(&slave->open_list);
186 list_add_tail(&slave->open_list,
187 &master->slave_list_head);
188 spin_lock_irq(&slave_active_lock);
189 slave->master = master;
190 slave->timer = master->timer;
191 spin_unlock_irq(&slave_active_lock);
192 return;
199 * look for slave instances matching with the slave id of the given master.
200 * when found, relink the open_link of slaves.
202 * call this with register_mutex down.
204 static void snd_timer_check_master(struct snd_timer_instance *master)
206 struct snd_timer_instance *slave, *tmp;
208 /* check all pending slaves */
209 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
210 if (slave->slave_class == master->slave_class &&
211 slave->slave_id == master->slave_id) {
212 list_move_tail(&slave->open_list, &master->slave_list_head);
213 spin_lock_irq(&slave_active_lock);
214 slave->master = master;
215 slave->timer = master->timer;
216 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
217 list_add_tail(&slave->active_list,
218 &master->slave_active_head);
219 spin_unlock_irq(&slave_active_lock);
225 * open a timer instance
226 * when opening a master, the slave id must be here given.
228 int snd_timer_open(struct snd_timer_instance **ti,
229 char *owner, struct snd_timer_id *tid,
230 unsigned int slave_id)
232 struct snd_timer *timer;
233 struct snd_timer_instance *timeri = NULL;
235 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
236 /* open a slave instance */
237 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
238 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
239 snd_printd("invalid slave class %i\n", tid->dev_sclass);
240 return -EINVAL;
242 mutex_lock(&register_mutex);
243 timeri = snd_timer_instance_new(owner, NULL);
244 if (!timeri) {
245 mutex_unlock(&register_mutex);
246 return -ENOMEM;
248 timeri->slave_class = tid->dev_sclass;
249 timeri->slave_id = tid->device;
250 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
251 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
252 snd_timer_check_slave(timeri);
253 mutex_unlock(&register_mutex);
254 *ti = timeri;
255 return 0;
258 /* open a master instance */
259 mutex_lock(&register_mutex);
260 timer = snd_timer_find(tid);
261 #ifdef CONFIG_MODULES
262 if (!timer) {
263 mutex_unlock(&register_mutex);
264 snd_timer_request(tid);
265 mutex_lock(&register_mutex);
266 timer = snd_timer_find(tid);
268 #endif
269 if (!timer) {
270 mutex_unlock(&register_mutex);
271 return -ENODEV;
273 if (!list_empty(&timer->open_list_head)) {
274 timeri = list_entry(timer->open_list_head.next,
275 struct snd_timer_instance, open_list);
276 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
277 mutex_unlock(&register_mutex);
278 return -EBUSY;
281 timeri = snd_timer_instance_new(owner, timer);
282 if (!timeri) {
283 mutex_unlock(&register_mutex);
284 return -ENOMEM;
286 timeri->slave_class = tid->dev_sclass;
287 timeri->slave_id = slave_id;
288 if (list_empty(&timer->open_list_head) && timer->hw.open)
289 timer->hw.open(timer);
290 list_add_tail(&timeri->open_list, &timer->open_list_head);
291 snd_timer_check_master(timeri);
292 mutex_unlock(&register_mutex);
293 *ti = timeri;
294 return 0;
297 static int _snd_timer_stop(struct snd_timer_instance *timeri,
298 int keep_flag, int event);
301 * close a timer instance
303 int snd_timer_close(struct snd_timer_instance *timeri)
305 struct snd_timer *timer = NULL;
306 struct snd_timer_instance *slave, *tmp;
308 if (snd_BUG_ON(!timeri))
309 return -ENXIO;
311 /* force to stop the timer */
312 snd_timer_stop(timeri);
314 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
315 /* wait, until the active callback is finished */
316 spin_lock_irq(&slave_active_lock);
317 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
318 spin_unlock_irq(&slave_active_lock);
319 udelay(10);
320 spin_lock_irq(&slave_active_lock);
322 spin_unlock_irq(&slave_active_lock);
323 mutex_lock(&register_mutex);
324 list_del(&timeri->open_list);
325 mutex_unlock(&register_mutex);
326 } else {
327 timer = timeri->timer;
328 /* wait, until the active callback is finished */
329 spin_lock_irq(&timer->lock);
330 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
331 spin_unlock_irq(&timer->lock);
332 udelay(10);
333 spin_lock_irq(&timer->lock);
335 spin_unlock_irq(&timer->lock);
336 mutex_lock(&register_mutex);
337 list_del(&timeri->open_list);
338 if (timer && list_empty(&timer->open_list_head) &&
339 timer->hw.close)
340 timer->hw.close(timer);
341 /* remove slave links */
342 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
343 open_list) {
344 spin_lock_irq(&slave_active_lock);
345 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
346 list_move_tail(&slave->open_list, &snd_timer_slave_list);
347 slave->master = NULL;
348 slave->timer = NULL;
349 spin_unlock_irq(&slave_active_lock);
351 mutex_unlock(&register_mutex);
353 if (timeri->private_free)
354 timeri->private_free(timeri);
355 kfree(timeri->owner);
356 kfree(timeri);
357 if (timer)
358 module_put(timer->module);
359 return 0;
362 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
364 struct snd_timer * timer;
366 if (timeri == NULL)
367 return 0;
368 if ((timer = timeri->timer) != NULL) {
369 if (timer->hw.c_resolution)
370 return timer->hw.c_resolution(timer);
371 return timer->hw.resolution;
373 return 0;
376 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
378 struct snd_timer *timer;
379 unsigned long flags;
380 unsigned long resolution = 0;
381 struct snd_timer_instance *ts;
382 struct timespec tstamp;
384 if (timer_tstamp_monotonic)
385 do_posix_clock_monotonic_gettime(&tstamp);
386 else
387 getnstimeofday(&tstamp);
388 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
389 event > SNDRV_TIMER_EVENT_PAUSE))
390 return;
391 if (event == SNDRV_TIMER_EVENT_START ||
392 event == SNDRV_TIMER_EVENT_CONTINUE)
393 resolution = snd_timer_resolution(ti);
394 if (ti->ccallback)
395 ti->ccallback(ti, event, &tstamp, resolution);
396 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
397 return;
398 timer = ti->timer;
399 if (timer == NULL)
400 return;
401 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
402 return;
403 spin_lock_irqsave(&timer->lock, flags);
404 list_for_each_entry(ts, &ti->slave_active_head, active_list)
405 if (ts->ccallback)
406 ts->ccallback(ti, event + 100, &tstamp, resolution);
407 spin_unlock_irqrestore(&timer->lock, flags);
410 static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
411 unsigned long sticks)
413 list_del(&timeri->active_list);
414 list_add_tail(&timeri->active_list, &timer->active_list_head);
415 if (timer->running) {
416 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
417 goto __start_now;
418 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
419 timeri->flags |= SNDRV_TIMER_IFLG_START;
420 return 1; /* delayed start */
421 } else {
422 timer->sticks = sticks;
423 timer->hw.start(timer);
424 __start_now:
425 timer->running++;
426 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
427 return 0;
431 static int snd_timer_start_slave(struct snd_timer_instance *timeri)
433 unsigned long flags;
435 spin_lock_irqsave(&slave_active_lock, flags);
436 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
437 if (timeri->master)
438 list_add_tail(&timeri->active_list,
439 &timeri->master->slave_active_head);
440 spin_unlock_irqrestore(&slave_active_lock, flags);
441 return 1; /* delayed start */
445 * start the timer instance
447 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
449 struct snd_timer *timer;
450 int result = -EINVAL;
451 unsigned long flags;
453 if (timeri == NULL || ticks < 1)
454 return -EINVAL;
455 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
456 result = snd_timer_start_slave(timeri);
457 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
458 return result;
460 timer = timeri->timer;
461 if (timer == NULL)
462 return -EINVAL;
463 spin_lock_irqsave(&timer->lock, flags);
464 timeri->ticks = timeri->cticks = ticks;
465 timeri->pticks = 0;
466 result = snd_timer_start1(timer, timeri, ticks);
467 spin_unlock_irqrestore(&timer->lock, flags);
468 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
469 return result;
472 static int _snd_timer_stop(struct snd_timer_instance * timeri,
473 int keep_flag, int event)
475 struct snd_timer *timer;
476 unsigned long flags;
478 if (snd_BUG_ON(!timeri))
479 return -ENXIO;
481 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
482 if (!keep_flag) {
483 spin_lock_irqsave(&slave_active_lock, flags);
484 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
485 spin_unlock_irqrestore(&slave_active_lock, flags);
487 goto __end;
489 timer = timeri->timer;
490 if (!timer)
491 return -EINVAL;
492 spin_lock_irqsave(&timer->lock, flags);
493 list_del_init(&timeri->ack_list);
494 list_del_init(&timeri->active_list);
495 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
496 !(--timer->running)) {
497 timer->hw.stop(timer);
498 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
499 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
500 snd_timer_reschedule(timer, 0);
501 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
502 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
503 timer->hw.start(timer);
507 if (!keep_flag)
508 timeri->flags &=
509 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
510 spin_unlock_irqrestore(&timer->lock, flags);
511 __end:
512 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
513 snd_timer_notify1(timeri, event);
514 return 0;
518 * stop the timer instance.
520 * do not call this from the timer callback!
522 int snd_timer_stop(struct snd_timer_instance *timeri)
524 struct snd_timer *timer;
525 unsigned long flags;
526 int err;
528 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
529 if (err < 0)
530 return err;
531 timer = timeri->timer;
532 spin_lock_irqsave(&timer->lock, flags);
533 timeri->cticks = timeri->ticks;
534 timeri->pticks = 0;
535 spin_unlock_irqrestore(&timer->lock, flags);
536 return 0;
540 * start again.. the tick is kept.
542 int snd_timer_continue(struct snd_timer_instance *timeri)
544 struct snd_timer *timer;
545 int result = -EINVAL;
546 unsigned long flags;
548 if (timeri == NULL)
549 return result;
550 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
551 return snd_timer_start_slave(timeri);
552 timer = timeri->timer;
553 if (! timer)
554 return -EINVAL;
555 spin_lock_irqsave(&timer->lock, flags);
556 if (!timeri->cticks)
557 timeri->cticks = 1;
558 timeri->pticks = 0;
559 result = snd_timer_start1(timer, timeri, timer->sticks);
560 spin_unlock_irqrestore(&timer->lock, flags);
561 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
562 return result;
566 * pause.. remember the ticks left
568 int snd_timer_pause(struct snd_timer_instance * timeri)
570 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
574 * reschedule the timer
576 * start pending instances and check the scheduling ticks.
577 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
579 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
581 struct snd_timer_instance *ti;
582 unsigned long ticks = ~0UL;
584 list_for_each_entry(ti, &timer->active_list_head, active_list) {
585 if (ti->flags & SNDRV_TIMER_IFLG_START) {
586 ti->flags &= ~SNDRV_TIMER_IFLG_START;
587 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
588 timer->running++;
590 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
591 if (ticks > ti->cticks)
592 ticks = ti->cticks;
595 if (ticks == ~0UL) {
596 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
597 return;
599 if (ticks > timer->hw.ticks)
600 ticks = timer->hw.ticks;
601 if (ticks_left != ticks)
602 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
603 timer->sticks = ticks;
607 * timer tasklet
610 static void snd_timer_tasklet(unsigned long arg)
612 struct snd_timer *timer = (struct snd_timer *) arg;
613 struct snd_timer_instance *ti;
614 struct list_head *p;
615 unsigned long resolution, ticks;
616 unsigned long flags;
618 spin_lock_irqsave(&timer->lock, flags);
619 /* now process all callbacks */
620 while (!list_empty(&timer->sack_list_head)) {
621 p = timer->sack_list_head.next; /* get first item */
622 ti = list_entry(p, struct snd_timer_instance, ack_list);
624 /* remove from ack_list and make empty */
625 list_del_init(p);
627 ticks = ti->pticks;
628 ti->pticks = 0;
629 resolution = ti->resolution;
631 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
632 spin_unlock(&timer->lock);
633 if (ti->callback)
634 ti->callback(ti, resolution, ticks);
635 spin_lock(&timer->lock);
636 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
638 spin_unlock_irqrestore(&timer->lock, flags);
642 * timer interrupt
644 * ticks_left is usually equal to timer->sticks.
647 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
649 struct snd_timer_instance *ti, *ts, *tmp;
650 unsigned long resolution, ticks;
651 struct list_head *p, *ack_list_head;
652 unsigned long flags;
653 int use_tasklet = 0;
655 if (timer == NULL)
656 return;
658 spin_lock_irqsave(&timer->lock, flags);
660 /* remember the current resolution */
661 if (timer->hw.c_resolution)
662 resolution = timer->hw.c_resolution(timer);
663 else
664 resolution = timer->hw.resolution;
666 /* loop for all active instances
667 * Here we cannot use list_for_each_entry because the active_list of a
668 * processed instance is relinked to done_list_head before the callback
669 * is called.
671 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
672 active_list) {
673 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
674 continue;
675 ti->pticks += ticks_left;
676 ti->resolution = resolution;
677 if (ti->cticks < ticks_left)
678 ti->cticks = 0;
679 else
680 ti->cticks -= ticks_left;
681 if (ti->cticks) /* not expired */
682 continue;
683 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
684 ti->cticks = ti->ticks;
685 } else {
686 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
687 if (--timer->running)
688 list_del(&ti->active_list);
690 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
691 (ti->flags & SNDRV_TIMER_IFLG_FAST))
692 ack_list_head = &timer->ack_list_head;
693 else
694 ack_list_head = &timer->sack_list_head;
695 if (list_empty(&ti->ack_list))
696 list_add_tail(&ti->ack_list, ack_list_head);
697 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
698 ts->pticks = ti->pticks;
699 ts->resolution = resolution;
700 if (list_empty(&ts->ack_list))
701 list_add_tail(&ts->ack_list, ack_list_head);
704 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
705 snd_timer_reschedule(timer, timer->sticks);
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 = list_entry(p, struct snd_timer_instance, 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_irqrestore(&timer->lock, flags);
744 if (use_tasklet)
745 tasklet_schedule(&timer->task_queue);
752 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
753 struct snd_timer **rtimer)
755 struct snd_timer *timer;
756 int err;
757 static struct snd_device_ops ops = {
758 .dev_free = snd_timer_dev_free,
759 .dev_register = snd_timer_dev_register,
760 .dev_disconnect = snd_timer_dev_disconnect,
763 if (snd_BUG_ON(!tid))
764 return -EINVAL;
765 if (rtimer)
766 *rtimer = NULL;
767 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
768 if (timer == NULL) {
769 snd_printk(KERN_ERR "timer: cannot allocate\n");
770 return -ENOMEM;
772 timer->tmr_class = tid->dev_class;
773 timer->card = card;
774 timer->tmr_device = tid->device;
775 timer->tmr_subdevice = tid->subdevice;
776 if (id)
777 strlcpy(timer->id, id, sizeof(timer->id));
778 INIT_LIST_HEAD(&timer->device_list);
779 INIT_LIST_HEAD(&timer->open_list_head);
780 INIT_LIST_HEAD(&timer->active_list_head);
781 INIT_LIST_HEAD(&timer->ack_list_head);
782 INIT_LIST_HEAD(&timer->sack_list_head);
783 spin_lock_init(&timer->lock);
784 tasklet_init(&timer->task_queue, snd_timer_tasklet,
785 (unsigned long)timer);
786 if (card != NULL) {
787 timer->module = card->module;
788 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
789 if (err < 0) {
790 snd_timer_free(timer);
791 return err;
794 if (rtimer)
795 *rtimer = timer;
796 return 0;
799 static int snd_timer_free(struct snd_timer *timer)
801 if (!timer)
802 return 0;
804 mutex_lock(&register_mutex);
805 if (! list_empty(&timer->open_list_head)) {
806 struct list_head *p, *n;
807 struct snd_timer_instance *ti;
808 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
809 list_for_each_safe(p, n, &timer->open_list_head) {
810 list_del_init(p);
811 ti = list_entry(p, struct snd_timer_instance, open_list);
812 ti->timer = NULL;
815 list_del(&timer->device_list);
816 mutex_unlock(&register_mutex);
818 if (timer->private_free)
819 timer->private_free(timer);
820 kfree(timer);
821 return 0;
824 static int snd_timer_dev_free(struct snd_device *device)
826 struct snd_timer *timer = device->device_data;
827 return snd_timer_free(timer);
830 static int snd_timer_dev_register(struct snd_device *dev)
832 struct snd_timer *timer = dev->device_data;
833 struct snd_timer *timer1;
835 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
836 return -ENXIO;
837 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
838 !timer->hw.resolution && timer->hw.c_resolution == NULL)
839 return -EINVAL;
841 mutex_lock(&register_mutex);
842 list_for_each_entry(timer1, &snd_timer_list, device_list) {
843 if (timer1->tmr_class > timer->tmr_class)
844 break;
845 if (timer1->tmr_class < timer->tmr_class)
846 continue;
847 if (timer1->card && timer->card) {
848 if (timer1->card->number > timer->card->number)
849 break;
850 if (timer1->card->number < timer->card->number)
851 continue;
853 if (timer1->tmr_device > timer->tmr_device)
854 break;
855 if (timer1->tmr_device < timer->tmr_device)
856 continue;
857 if (timer1->tmr_subdevice > timer->tmr_subdevice)
858 break;
859 if (timer1->tmr_subdevice < timer->tmr_subdevice)
860 continue;
861 /* conflicts.. */
862 mutex_unlock(&register_mutex);
863 return -EBUSY;
865 list_add_tail(&timer->device_list, &timer1->device_list);
866 mutex_unlock(&register_mutex);
867 return 0;
870 static int snd_timer_dev_disconnect(struct snd_device *device)
872 struct snd_timer *timer = device->device_data;
873 mutex_lock(&register_mutex);
874 list_del_init(&timer->device_list);
875 mutex_unlock(&register_mutex);
876 return 0;
879 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
881 unsigned long flags;
882 unsigned long resolution = 0;
883 struct snd_timer_instance *ti, *ts;
885 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
886 return;
887 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
888 event > SNDRV_TIMER_EVENT_MRESUME))
889 return;
890 spin_lock_irqsave(&timer->lock, flags);
891 if (event == SNDRV_TIMER_EVENT_MSTART ||
892 event == SNDRV_TIMER_EVENT_MCONTINUE ||
893 event == SNDRV_TIMER_EVENT_MRESUME) {
894 if (timer->hw.c_resolution)
895 resolution = timer->hw.c_resolution(timer);
896 else
897 resolution = timer->hw.resolution;
899 list_for_each_entry(ti, &timer->active_list_head, active_list) {
900 if (ti->ccallback)
901 ti->ccallback(ti, event, tstamp, resolution);
902 list_for_each_entry(ts, &ti->slave_active_head, active_list)
903 if (ts->ccallback)
904 ts->ccallback(ts, event, tstamp, resolution);
906 spin_unlock_irqrestore(&timer->lock, flags);
910 * exported functions for global timers
912 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
914 struct snd_timer_id tid;
916 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
917 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
918 tid.card = -1;
919 tid.device = device;
920 tid.subdevice = 0;
921 return snd_timer_new(NULL, id, &tid, rtimer);
924 int snd_timer_global_free(struct snd_timer *timer)
926 return snd_timer_free(timer);
929 int snd_timer_global_register(struct snd_timer *timer)
931 struct snd_device dev;
933 memset(&dev, 0, sizeof(dev));
934 dev.device_data = timer;
935 return snd_timer_dev_register(&dev);
939 * System timer
942 struct snd_timer_system_private {
943 struct timer_list tlist;
944 unsigned long last_expires;
945 unsigned long last_jiffies;
946 unsigned long correction;
949 static void snd_timer_s_function(unsigned long data)
951 struct snd_timer *timer = (struct snd_timer *)data;
952 struct snd_timer_system_private *priv = timer->private_data;
953 unsigned long jiff = jiffies;
954 if (time_after(jiff, priv->last_expires))
955 priv->correction += (long)jiff - (long)priv->last_expires;
956 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
959 static int snd_timer_s_start(struct snd_timer * timer)
961 struct snd_timer_system_private *priv;
962 unsigned long njiff;
964 priv = (struct snd_timer_system_private *) timer->private_data;
965 njiff = (priv->last_jiffies = jiffies);
966 if (priv->correction > timer->sticks - 1) {
967 priv->correction -= timer->sticks - 1;
968 njiff++;
969 } else {
970 njiff += timer->sticks - priv->correction;
971 priv->correction = 0;
973 priv->last_expires = priv->tlist.expires = njiff;
974 add_timer(&priv->tlist);
975 return 0;
978 static int snd_timer_s_stop(struct snd_timer * timer)
980 struct snd_timer_system_private *priv;
981 unsigned long jiff;
983 priv = (struct snd_timer_system_private *) timer->private_data;
984 del_timer(&priv->tlist);
985 jiff = jiffies;
986 if (time_before(jiff, priv->last_expires))
987 timer->sticks = priv->last_expires - jiff;
988 else
989 timer->sticks = 1;
990 priv->correction = 0;
991 return 0;
994 static struct snd_timer_hardware snd_timer_system =
996 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
997 .resolution = 1000000000L / HZ,
998 .ticks = 10000000L,
999 .start = snd_timer_s_start,
1000 .stop = snd_timer_s_stop
1003 static void snd_timer_free_system(struct snd_timer *timer)
1005 kfree(timer->private_data);
1008 static int snd_timer_register_system(void)
1010 struct snd_timer *timer;
1011 struct snd_timer_system_private *priv;
1012 int err;
1014 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1015 if (err < 0)
1016 return err;
1017 strcpy(timer->name, "system timer");
1018 timer->hw = snd_timer_system;
1019 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1020 if (priv == NULL) {
1021 snd_timer_free(timer);
1022 return -ENOMEM;
1024 init_timer(&priv->tlist);
1025 priv->tlist.function = snd_timer_s_function;
1026 priv->tlist.data = (unsigned long) timer;
1027 timer->private_data = priv;
1028 timer->private_free = snd_timer_free_system;
1029 return snd_timer_global_register(timer);
1032 #ifdef CONFIG_PROC_FS
1034 * Info interface
1037 static void snd_timer_proc_read(struct snd_info_entry *entry,
1038 struct snd_info_buffer *buffer)
1040 struct snd_timer *timer;
1041 struct snd_timer_instance *ti;
1043 mutex_lock(&register_mutex);
1044 list_for_each_entry(timer, &snd_timer_list, device_list) {
1045 switch (timer->tmr_class) {
1046 case SNDRV_TIMER_CLASS_GLOBAL:
1047 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1048 break;
1049 case SNDRV_TIMER_CLASS_CARD:
1050 snd_iprintf(buffer, "C%i-%i: ",
1051 timer->card->number, timer->tmr_device);
1052 break;
1053 case SNDRV_TIMER_CLASS_PCM:
1054 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1055 timer->tmr_device, timer->tmr_subdevice);
1056 break;
1057 default:
1058 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1059 timer->card ? timer->card->number : -1,
1060 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)",
1065 timer->hw.resolution / 1000,
1066 timer->hw.resolution % 1000,
1067 timer->hw.ticks);
1068 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1069 snd_iprintf(buffer, " SLAVE");
1070 snd_iprintf(buffer, "\n");
1071 list_for_each_entry(ti, &timer->open_list_head, open_list)
1072 snd_iprintf(buffer, " Client %s : %s\n",
1073 ti->owner ? ti->owner : "unknown",
1074 ti->flags & (SNDRV_TIMER_IFLG_START |
1075 SNDRV_TIMER_IFLG_RUNNING)
1076 ? "running" : "stopped");
1078 mutex_unlock(&register_mutex);
1081 static struct snd_info_entry *snd_timer_proc_entry;
1083 static void __init snd_timer_proc_init(void)
1085 struct snd_info_entry *entry;
1087 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1088 if (entry != NULL) {
1089 entry->c.text.read = snd_timer_proc_read;
1090 if (snd_info_register(entry) < 0) {
1091 snd_info_free_entry(entry);
1092 entry = NULL;
1095 snd_timer_proc_entry = entry;
1098 static void __exit snd_timer_proc_done(void)
1100 snd_info_free_entry(snd_timer_proc_entry);
1102 #else /* !CONFIG_PROC_FS */
1103 #define snd_timer_proc_init()
1104 #define snd_timer_proc_done()
1105 #endif
1108 * USER SPACE interface
1111 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1112 unsigned long resolution,
1113 unsigned long ticks)
1115 struct snd_timer_user *tu = timeri->callback_data;
1116 struct snd_timer_read *r;
1117 int prev;
1119 spin_lock(&tu->qlock);
1120 if (tu->qused > 0) {
1121 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1122 r = &tu->queue[prev];
1123 if (r->resolution == resolution) {
1124 r->ticks += ticks;
1125 goto __wake;
1128 if (tu->qused >= tu->queue_size) {
1129 tu->overrun++;
1130 } else {
1131 r = &tu->queue[tu->qtail++];
1132 tu->qtail %= tu->queue_size;
1133 r->resolution = resolution;
1134 r->ticks = ticks;
1135 tu->qused++;
1137 __wake:
1138 spin_unlock(&tu->qlock);
1139 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1140 wake_up(&tu->qchange_sleep);
1143 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1144 struct snd_timer_tread *tread)
1146 if (tu->qused >= tu->queue_size) {
1147 tu->overrun++;
1148 } else {
1149 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1150 tu->qtail %= tu->queue_size;
1151 tu->qused++;
1155 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1156 int event,
1157 struct timespec *tstamp,
1158 unsigned long resolution)
1160 struct snd_timer_user *tu = timeri->callback_data;
1161 struct snd_timer_tread r1;
1162 unsigned long flags;
1164 if (event >= SNDRV_TIMER_EVENT_START &&
1165 event <= SNDRV_TIMER_EVENT_PAUSE)
1166 tu->tstamp = *tstamp;
1167 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1168 return;
1169 r1.event = event;
1170 r1.tstamp = *tstamp;
1171 r1.val = resolution;
1172 spin_lock_irqsave(&tu->qlock, flags);
1173 snd_timer_user_append_to_tqueue(tu, &r1);
1174 spin_unlock_irqrestore(&tu->qlock, flags);
1175 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1176 wake_up(&tu->qchange_sleep);
1179 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1180 unsigned long resolution,
1181 unsigned long ticks)
1183 struct snd_timer_user *tu = timeri->callback_data;
1184 struct snd_timer_tread *r, r1;
1185 struct timespec tstamp;
1186 int prev, append = 0;
1188 memset(&tstamp, 0, sizeof(tstamp));
1189 spin_lock(&tu->qlock);
1190 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1191 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1192 spin_unlock(&tu->qlock);
1193 return;
1195 if (tu->last_resolution != resolution || ticks > 0) {
1196 if (timer_tstamp_monotonic)
1197 do_posix_clock_monotonic_gettime(&tstamp);
1198 else
1199 getnstimeofday(&tstamp);
1201 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1202 tu->last_resolution != resolution) {
1203 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1204 r1.tstamp = tstamp;
1205 r1.val = resolution;
1206 snd_timer_user_append_to_tqueue(tu, &r1);
1207 tu->last_resolution = resolution;
1208 append++;
1210 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1211 goto __wake;
1212 if (ticks == 0)
1213 goto __wake;
1214 if (tu->qused > 0) {
1215 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1216 r = &tu->tqueue[prev];
1217 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1218 r->tstamp = tstamp;
1219 r->val += ticks;
1220 append++;
1221 goto __wake;
1224 r1.event = SNDRV_TIMER_EVENT_TICK;
1225 r1.tstamp = tstamp;
1226 r1.val = ticks;
1227 snd_timer_user_append_to_tqueue(tu, &r1);
1228 append++;
1229 __wake:
1230 spin_unlock(&tu->qlock);
1231 if (append == 0)
1232 return;
1233 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1234 wake_up(&tu->qchange_sleep);
1237 static int snd_timer_user_open(struct inode *inode, struct file *file)
1239 struct snd_timer_user *tu;
1240 int err;
1242 err = nonseekable_open(inode, file);
1243 if (err < 0)
1244 return err;
1246 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1247 if (tu == NULL)
1248 return -ENOMEM;
1249 spin_lock_init(&tu->qlock);
1250 init_waitqueue_head(&tu->qchange_sleep);
1251 mutex_init(&tu->tread_sem);
1252 tu->ticks = 1;
1253 tu->queue_size = 128;
1254 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1255 GFP_KERNEL);
1256 if (tu->queue == NULL) {
1257 kfree(tu);
1258 return -ENOMEM;
1260 file->private_data = tu;
1261 return 0;
1264 static int snd_timer_user_release(struct inode *inode, struct file *file)
1266 struct snd_timer_user *tu;
1268 if (file->private_data) {
1269 tu = file->private_data;
1270 file->private_data = NULL;
1271 if (tu->timeri)
1272 snd_timer_close(tu->timeri);
1273 kfree(tu->queue);
1274 kfree(tu->tqueue);
1275 kfree(tu);
1277 return 0;
1280 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1282 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1283 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1284 id->card = -1;
1285 id->device = -1;
1286 id->subdevice = -1;
1289 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1291 id->dev_class = timer->tmr_class;
1292 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1293 id->card = timer->card ? timer->card->number : -1;
1294 id->device = timer->tmr_device;
1295 id->subdevice = timer->tmr_subdevice;
1298 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1300 struct snd_timer_id id;
1301 struct snd_timer *timer;
1302 struct list_head *p;
1304 if (copy_from_user(&id, _tid, sizeof(id)))
1305 return -EFAULT;
1306 mutex_lock(&register_mutex);
1307 if (id.dev_class < 0) { /* first item */
1308 if (list_empty(&snd_timer_list))
1309 snd_timer_user_zero_id(&id);
1310 else {
1311 timer = list_entry(snd_timer_list.next,
1312 struct snd_timer, device_list);
1313 snd_timer_user_copy_id(&id, timer);
1315 } else {
1316 switch (id.dev_class) {
1317 case SNDRV_TIMER_CLASS_GLOBAL:
1318 id.device = id.device < 0 ? 0 : id.device + 1;
1319 list_for_each(p, &snd_timer_list) {
1320 timer = list_entry(p, struct snd_timer, device_list);
1321 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1322 snd_timer_user_copy_id(&id, timer);
1323 break;
1325 if (timer->tmr_device >= id.device) {
1326 snd_timer_user_copy_id(&id, timer);
1327 break;
1330 if (p == &snd_timer_list)
1331 snd_timer_user_zero_id(&id);
1332 break;
1333 case SNDRV_TIMER_CLASS_CARD:
1334 case SNDRV_TIMER_CLASS_PCM:
1335 if (id.card < 0) {
1336 id.card = 0;
1337 } else {
1338 if (id.card < 0) {
1339 id.card = 0;
1340 } else {
1341 if (id.device < 0) {
1342 id.device = 0;
1343 } else {
1344 if (id.subdevice < 0) {
1345 id.subdevice = 0;
1346 } else {
1347 id.subdevice++;
1352 list_for_each(p, &snd_timer_list) {
1353 timer = list_entry(p, struct snd_timer, device_list);
1354 if (timer->tmr_class > id.dev_class) {
1355 snd_timer_user_copy_id(&id, timer);
1356 break;
1358 if (timer->tmr_class < id.dev_class)
1359 continue;
1360 if (timer->card->number > id.card) {
1361 snd_timer_user_copy_id(&id, timer);
1362 break;
1364 if (timer->card->number < id.card)
1365 continue;
1366 if (timer->tmr_device > id.device) {
1367 snd_timer_user_copy_id(&id, timer);
1368 break;
1370 if (timer->tmr_device < id.device)
1371 continue;
1372 if (timer->tmr_subdevice > id.subdevice) {
1373 snd_timer_user_copy_id(&id, timer);
1374 break;
1376 if (timer->tmr_subdevice < id.subdevice)
1377 continue;
1378 snd_timer_user_copy_id(&id, timer);
1379 break;
1381 if (p == &snd_timer_list)
1382 snd_timer_user_zero_id(&id);
1383 break;
1384 default:
1385 snd_timer_user_zero_id(&id);
1388 mutex_unlock(&register_mutex);
1389 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1390 return -EFAULT;
1391 return 0;
1394 static int snd_timer_user_ginfo(struct file *file,
1395 struct snd_timer_ginfo __user *_ginfo)
1397 struct snd_timer_ginfo *ginfo;
1398 struct snd_timer_id tid;
1399 struct snd_timer *t;
1400 struct list_head *p;
1401 int err = 0;
1403 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1404 if (IS_ERR(ginfo))
1405 return PTR_ERR(ginfo);
1407 tid = ginfo->tid;
1408 memset(ginfo, 0, sizeof(*ginfo));
1409 ginfo->tid = tid;
1410 mutex_lock(&register_mutex);
1411 t = snd_timer_find(&tid);
1412 if (t != NULL) {
1413 ginfo->card = t->card ? t->card->number : -1;
1414 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1415 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1416 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1417 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1418 ginfo->resolution = t->hw.resolution;
1419 if (t->hw.resolution_min > 0) {
1420 ginfo->resolution_min = t->hw.resolution_min;
1421 ginfo->resolution_max = t->hw.resolution_max;
1423 list_for_each(p, &t->open_list_head) {
1424 ginfo->clients++;
1426 } else {
1427 err = -ENODEV;
1429 mutex_unlock(&register_mutex);
1430 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1431 err = -EFAULT;
1432 kfree(ginfo);
1433 return err;
1436 static int snd_timer_user_gparams(struct file *file,
1437 struct snd_timer_gparams __user *_gparams)
1439 struct snd_timer_gparams gparams;
1440 struct snd_timer *t;
1441 int err;
1443 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1444 return -EFAULT;
1445 mutex_lock(&register_mutex);
1446 t = snd_timer_find(&gparams.tid);
1447 if (!t) {
1448 err = -ENODEV;
1449 goto _error;
1451 if (!list_empty(&t->open_list_head)) {
1452 err = -EBUSY;
1453 goto _error;
1455 if (!t->hw.set_period) {
1456 err = -ENOSYS;
1457 goto _error;
1459 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1460 _error:
1461 mutex_unlock(&register_mutex);
1462 return err;
1465 static int snd_timer_user_gstatus(struct file *file,
1466 struct snd_timer_gstatus __user *_gstatus)
1468 struct snd_timer_gstatus gstatus;
1469 struct snd_timer_id tid;
1470 struct snd_timer *t;
1471 int err = 0;
1473 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1474 return -EFAULT;
1475 tid = gstatus.tid;
1476 memset(&gstatus, 0, sizeof(gstatus));
1477 gstatus.tid = tid;
1478 mutex_lock(&register_mutex);
1479 t = snd_timer_find(&tid);
1480 if (t != NULL) {
1481 if (t->hw.c_resolution)
1482 gstatus.resolution = t->hw.c_resolution(t);
1483 else
1484 gstatus.resolution = t->hw.resolution;
1485 if (t->hw.precise_resolution) {
1486 t->hw.precise_resolution(t, &gstatus.resolution_num,
1487 &gstatus.resolution_den);
1488 } else {
1489 gstatus.resolution_num = gstatus.resolution;
1490 gstatus.resolution_den = 1000000000uL;
1492 } else {
1493 err = -ENODEV;
1495 mutex_unlock(&register_mutex);
1496 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1497 err = -EFAULT;
1498 return err;
1501 static int snd_timer_user_tselect(struct file *file,
1502 struct snd_timer_select __user *_tselect)
1504 struct snd_timer_user *tu;
1505 struct snd_timer_select tselect;
1506 char str[32];
1507 int err = 0;
1509 tu = file->private_data;
1510 mutex_lock(&tu->tread_sem);
1511 if (tu->timeri) {
1512 snd_timer_close(tu->timeri);
1513 tu->timeri = NULL;
1515 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1516 err = -EFAULT;
1517 goto __err;
1519 sprintf(str, "application %i", current->pid);
1520 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1521 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1522 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1523 if (err < 0)
1524 goto __err;
1526 kfree(tu->queue);
1527 tu->queue = NULL;
1528 kfree(tu->tqueue);
1529 tu->tqueue = NULL;
1530 if (tu->tread) {
1531 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1532 GFP_KERNEL);
1533 if (tu->tqueue == NULL)
1534 err = -ENOMEM;
1535 } else {
1536 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1537 GFP_KERNEL);
1538 if (tu->queue == NULL)
1539 err = -ENOMEM;
1542 if (err < 0) {
1543 snd_timer_close(tu->timeri);
1544 tu->timeri = NULL;
1545 } else {
1546 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1547 tu->timeri->callback = tu->tread
1548 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1549 tu->timeri->ccallback = snd_timer_user_ccallback;
1550 tu->timeri->callback_data = (void *)tu;
1553 __err:
1554 mutex_unlock(&tu->tread_sem);
1555 return err;
1558 static int snd_timer_user_info(struct file *file,
1559 struct snd_timer_info __user *_info)
1561 struct snd_timer_user *tu;
1562 struct snd_timer_info *info;
1563 struct snd_timer *t;
1564 int err = 0;
1566 tu = file->private_data;
1567 if (!tu->timeri)
1568 return -EBADFD;
1569 t = tu->timeri->timer;
1570 if (!t)
1571 return -EBADFD;
1573 info = kzalloc(sizeof(*info), GFP_KERNEL);
1574 if (! info)
1575 return -ENOMEM;
1576 info->card = t->card ? t->card->number : -1;
1577 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1578 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1579 strlcpy(info->id, t->id, sizeof(info->id));
1580 strlcpy(info->name, t->name, sizeof(info->name));
1581 info->resolution = t->hw.resolution;
1582 if (copy_to_user(_info, info, sizeof(*_info)))
1583 err = -EFAULT;
1584 kfree(info);
1585 return err;
1588 static int snd_timer_user_params(struct file *file,
1589 struct snd_timer_params __user *_params)
1591 struct snd_timer_user *tu;
1592 struct snd_timer_params params;
1593 struct snd_timer *t;
1594 struct snd_timer_read *tr;
1595 struct snd_timer_tread *ttr;
1596 int err;
1598 tu = file->private_data;
1599 if (!tu->timeri)
1600 return -EBADFD;
1601 t = tu->timeri->timer;
1602 if (!t)
1603 return -EBADFD;
1604 if (copy_from_user(&params, _params, sizeof(params)))
1605 return -EFAULT;
1606 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1607 err = -EINVAL;
1608 goto _end;
1610 if (params.queue_size > 0 &&
1611 (params.queue_size < 32 || params.queue_size > 1024)) {
1612 err = -EINVAL;
1613 goto _end;
1615 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1616 (1<<SNDRV_TIMER_EVENT_TICK)|
1617 (1<<SNDRV_TIMER_EVENT_START)|
1618 (1<<SNDRV_TIMER_EVENT_STOP)|
1619 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1620 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1621 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1622 (1<<SNDRV_TIMER_EVENT_RESUME)|
1623 (1<<SNDRV_TIMER_EVENT_MSTART)|
1624 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1625 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1626 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1627 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1628 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1629 err = -EINVAL;
1630 goto _end;
1632 snd_timer_stop(tu->timeri);
1633 spin_lock_irq(&t->lock);
1634 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1635 SNDRV_TIMER_IFLG_EXCLUSIVE|
1636 SNDRV_TIMER_IFLG_EARLY_EVENT);
1637 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1638 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1639 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1640 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1641 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1642 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1643 spin_unlock_irq(&t->lock);
1644 if (params.queue_size > 0 &&
1645 (unsigned int)tu->queue_size != params.queue_size) {
1646 if (tu->tread) {
1647 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1648 GFP_KERNEL);
1649 if (ttr) {
1650 kfree(tu->tqueue);
1651 tu->queue_size = params.queue_size;
1652 tu->tqueue = ttr;
1654 } else {
1655 tr = kmalloc(params.queue_size * sizeof(*tr),
1656 GFP_KERNEL);
1657 if (tr) {
1658 kfree(tu->queue);
1659 tu->queue_size = params.queue_size;
1660 tu->queue = tr;
1664 tu->qhead = tu->qtail = tu->qused = 0;
1665 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1666 if (tu->tread) {
1667 struct snd_timer_tread tread;
1668 tread.event = SNDRV_TIMER_EVENT_EARLY;
1669 tread.tstamp.tv_sec = 0;
1670 tread.tstamp.tv_nsec = 0;
1671 tread.val = 0;
1672 snd_timer_user_append_to_tqueue(tu, &tread);
1673 } else {
1674 struct snd_timer_read *r = &tu->queue[0];
1675 r->resolution = 0;
1676 r->ticks = 0;
1677 tu->qused++;
1678 tu->qtail++;
1681 tu->filter = params.filter;
1682 tu->ticks = params.ticks;
1683 err = 0;
1684 _end:
1685 if (copy_to_user(_params, &params, sizeof(params)))
1686 return -EFAULT;
1687 return err;
1690 static int snd_timer_user_status(struct file *file,
1691 struct snd_timer_status __user *_status)
1693 struct snd_timer_user *tu;
1694 struct snd_timer_status status;
1696 tu = file->private_data;
1697 if (!tu->timeri)
1698 return -EBADFD;
1699 memset(&status, 0, sizeof(status));
1700 status.tstamp = tu->tstamp;
1701 status.resolution = snd_timer_resolution(tu->timeri);
1702 status.lost = tu->timeri->lost;
1703 status.overrun = tu->overrun;
1704 spin_lock_irq(&tu->qlock);
1705 status.queue = tu->qused;
1706 spin_unlock_irq(&tu->qlock);
1707 if (copy_to_user(_status, &status, sizeof(status)))
1708 return -EFAULT;
1709 return 0;
1712 static int snd_timer_user_start(struct file *file)
1714 int err;
1715 struct snd_timer_user *tu;
1717 tu = file->private_data;
1718 if (!tu->timeri)
1719 return -EBADFD;
1720 snd_timer_stop(tu->timeri);
1721 tu->timeri->lost = 0;
1722 tu->last_resolution = 0;
1723 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1726 static int snd_timer_user_stop(struct file *file)
1728 int err;
1729 struct snd_timer_user *tu;
1731 tu = file->private_data;
1732 if (!tu->timeri)
1733 return -EBADFD;
1734 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1737 static int snd_timer_user_continue(struct file *file)
1739 int err;
1740 struct snd_timer_user *tu;
1742 tu = file->private_data;
1743 if (!tu->timeri)
1744 return -EBADFD;
1745 tu->timeri->lost = 0;
1746 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1749 static int snd_timer_user_pause(struct file *file)
1751 int err;
1752 struct snd_timer_user *tu;
1754 tu = file->private_data;
1755 if (!tu->timeri)
1756 return -EBADFD;
1757 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1760 enum {
1761 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1762 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1763 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1764 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1767 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1768 unsigned long arg)
1770 struct snd_timer_user *tu;
1771 void __user *argp = (void __user *)arg;
1772 int __user *p = argp;
1774 tu = file->private_data;
1775 switch (cmd) {
1776 case SNDRV_TIMER_IOCTL_PVERSION:
1777 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1778 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1779 return snd_timer_user_next_device(argp);
1780 case SNDRV_TIMER_IOCTL_TREAD:
1782 int xarg;
1784 mutex_lock(&tu->tread_sem);
1785 if (tu->timeri) { /* too late */
1786 mutex_unlock(&tu->tread_sem);
1787 return -EBUSY;
1789 if (get_user(xarg, p)) {
1790 mutex_unlock(&tu->tread_sem);
1791 return -EFAULT;
1793 tu->tread = xarg ? 1 : 0;
1794 mutex_unlock(&tu->tread_sem);
1795 return 0;
1797 case SNDRV_TIMER_IOCTL_GINFO:
1798 return snd_timer_user_ginfo(file, argp);
1799 case SNDRV_TIMER_IOCTL_GPARAMS:
1800 return snd_timer_user_gparams(file, argp);
1801 case SNDRV_TIMER_IOCTL_GSTATUS:
1802 return snd_timer_user_gstatus(file, argp);
1803 case SNDRV_TIMER_IOCTL_SELECT:
1804 return snd_timer_user_tselect(file, argp);
1805 case SNDRV_TIMER_IOCTL_INFO:
1806 return snd_timer_user_info(file, argp);
1807 case SNDRV_TIMER_IOCTL_PARAMS:
1808 return snd_timer_user_params(file, argp);
1809 case SNDRV_TIMER_IOCTL_STATUS:
1810 return snd_timer_user_status(file, argp);
1811 case SNDRV_TIMER_IOCTL_START:
1812 case SNDRV_TIMER_IOCTL_START_OLD:
1813 return snd_timer_user_start(file);
1814 case SNDRV_TIMER_IOCTL_STOP:
1815 case SNDRV_TIMER_IOCTL_STOP_OLD:
1816 return snd_timer_user_stop(file);
1817 case SNDRV_TIMER_IOCTL_CONTINUE:
1818 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1819 return snd_timer_user_continue(file);
1820 case SNDRV_TIMER_IOCTL_PAUSE:
1821 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1822 return snd_timer_user_pause(file);
1824 return -ENOTTY;
1827 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1829 struct snd_timer_user *tu;
1831 tu = file->private_data;
1832 return fasync_helper(fd, file, on, &tu->fasync);
1835 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1836 size_t count, loff_t *offset)
1838 struct snd_timer_user *tu;
1839 long result = 0, unit;
1840 int err = 0;
1842 tu = file->private_data;
1843 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1844 spin_lock_irq(&tu->qlock);
1845 while ((long)count - result >= unit) {
1846 while (!tu->qused) {
1847 wait_queue_t wait;
1849 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1850 err = -EAGAIN;
1851 break;
1854 set_current_state(TASK_INTERRUPTIBLE);
1855 init_waitqueue_entry(&wait, current);
1856 add_wait_queue(&tu->qchange_sleep, &wait);
1858 spin_unlock_irq(&tu->qlock);
1859 schedule();
1860 spin_lock_irq(&tu->qlock);
1862 remove_wait_queue(&tu->qchange_sleep, &wait);
1864 if (signal_pending(current)) {
1865 err = -ERESTARTSYS;
1866 break;
1870 spin_unlock_irq(&tu->qlock);
1871 if (err < 0)
1872 goto _error;
1874 if (tu->tread) {
1875 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1876 sizeof(struct snd_timer_tread))) {
1877 err = -EFAULT;
1878 goto _error;
1880 } else {
1881 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1882 sizeof(struct snd_timer_read))) {
1883 err = -EFAULT;
1884 goto _error;
1888 tu->qhead %= tu->queue_size;
1890 result += unit;
1891 buffer += unit;
1893 spin_lock_irq(&tu->qlock);
1894 tu->qused--;
1896 spin_unlock_irq(&tu->qlock);
1897 _error:
1898 return result > 0 ? result : err;
1901 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1903 unsigned int mask;
1904 struct snd_timer_user *tu;
1906 tu = file->private_data;
1908 poll_wait(file, &tu->qchange_sleep, wait);
1910 mask = 0;
1911 if (tu->qused)
1912 mask |= POLLIN | POLLRDNORM;
1914 return mask;
1917 #ifdef CONFIG_COMPAT
1918 #include "timer_compat.c"
1919 #else
1920 #define snd_timer_user_ioctl_compat NULL
1921 #endif
1923 static const struct file_operations snd_timer_f_ops =
1925 .owner = THIS_MODULE,
1926 .read = snd_timer_user_read,
1927 .open = snd_timer_user_open,
1928 .release = snd_timer_user_release,
1929 .llseek = no_llseek,
1930 .poll = snd_timer_user_poll,
1931 .unlocked_ioctl = snd_timer_user_ioctl,
1932 .compat_ioctl = snd_timer_user_ioctl_compat,
1933 .fasync = snd_timer_user_fasync,
1937 * ENTRY functions
1940 static int __init alsa_timer_init(void)
1942 int err;
1944 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1945 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1946 "system timer");
1947 #endif
1949 if ((err = snd_timer_register_system()) < 0)
1950 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1951 err);
1952 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1953 &snd_timer_f_ops, NULL, "timer")) < 0)
1954 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1955 err);
1956 snd_timer_proc_init();
1957 return 0;
1960 static void __exit alsa_timer_exit(void)
1962 struct list_head *p, *n;
1964 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1965 /* unregister the system timer */
1966 list_for_each_safe(p, n, &snd_timer_list) {
1967 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
1968 snd_timer_free(timer);
1970 snd_timer_proc_done();
1971 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1972 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1973 #endif
1976 module_init(alsa_timer_init)
1977 module_exit(alsa_timer_exit)
1979 EXPORT_SYMBOL(snd_timer_open);
1980 EXPORT_SYMBOL(snd_timer_close);
1981 EXPORT_SYMBOL(snd_timer_resolution);
1982 EXPORT_SYMBOL(snd_timer_start);
1983 EXPORT_SYMBOL(snd_timer_stop);
1984 EXPORT_SYMBOL(snd_timer_continue);
1985 EXPORT_SYMBOL(snd_timer_pause);
1986 EXPORT_SYMBOL(snd_timer_new);
1987 EXPORT_SYMBOL(snd_timer_notify);
1988 EXPORT_SYMBOL(snd_timer_global_new);
1989 EXPORT_SYMBOL(snd_timer_global_free);
1990 EXPORT_SYMBOL(snd_timer_global_register);
1991 EXPORT_SYMBOL(snd_timer_interrupt);