BKL: Remove BKL from ext4 filesystem
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / core / timer.c
blob13afb60999b9f69263f461108a5f58aeeca4cae6
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 /* FIXME: it's really dumb to look up all entries.. */
182 list_for_each_entry(timer, &snd_timer_list, device_list) {
183 list_for_each_entry(master, &timer->open_list_head, open_list) {
184 if (slave->slave_class == master->slave_class &&
185 slave->slave_id == master->slave_id) {
186 list_del(&slave->open_list);
187 list_add_tail(&slave->open_list,
188 &master->slave_list_head);
189 spin_lock_irq(&slave_active_lock);
190 slave->master = master;
191 slave->timer = master->timer;
192 spin_unlock_irq(&slave_active_lock);
193 return;
200 * look for slave instances matching with the slave id of the given master.
201 * when found, relink the open_link of slaves.
203 * call this with register_mutex down.
205 static void snd_timer_check_master(struct snd_timer_instance *master)
207 struct snd_timer_instance *slave, *tmp;
209 /* check all pending slaves */
210 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
211 if (slave->slave_class == master->slave_class &&
212 slave->slave_id == master->slave_id) {
213 list_move_tail(&slave->open_list, &master->slave_list_head);
214 spin_lock_irq(&slave_active_lock);
215 slave->master = master;
216 slave->timer = master->timer;
217 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
218 list_add_tail(&slave->active_list,
219 &master->slave_active_head);
220 spin_unlock_irq(&slave_active_lock);
226 * open a timer instance
227 * when opening a master, the slave id must be here given.
229 int snd_timer_open(struct snd_timer_instance **ti,
230 char *owner, struct snd_timer_id *tid,
231 unsigned int slave_id)
233 struct snd_timer *timer;
234 struct snd_timer_instance *timeri = NULL;
236 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
237 /* open a slave instance */
238 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
239 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
240 snd_printd("invalid slave class %i\n", tid->dev_sclass);
241 return -EINVAL;
243 mutex_lock(&register_mutex);
244 timeri = snd_timer_instance_new(owner, NULL);
245 if (!timeri) {
246 mutex_unlock(&register_mutex);
247 return -ENOMEM;
249 timeri->slave_class = tid->dev_sclass;
250 timeri->slave_id = tid->device;
251 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
252 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
253 snd_timer_check_slave(timeri);
254 mutex_unlock(&register_mutex);
255 *ti = timeri;
256 return 0;
259 /* open a master instance */
260 mutex_lock(&register_mutex);
261 timer = snd_timer_find(tid);
262 #ifdef CONFIG_MODULES
263 if (!timer) {
264 mutex_unlock(&register_mutex);
265 snd_timer_request(tid);
266 mutex_lock(&register_mutex);
267 timer = snd_timer_find(tid);
269 #endif
270 if (!timer) {
271 mutex_unlock(&register_mutex);
272 return -ENODEV;
274 if (!list_empty(&timer->open_list_head)) {
275 timeri = list_entry(timer->open_list_head.next,
276 struct snd_timer_instance, open_list);
277 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
278 mutex_unlock(&register_mutex);
279 return -EBUSY;
282 timeri = snd_timer_instance_new(owner, timer);
283 if (!timeri) {
284 mutex_unlock(&register_mutex);
285 return -ENOMEM;
287 timeri->slave_class = tid->dev_sclass;
288 timeri->slave_id = slave_id;
289 if (list_empty(&timer->open_list_head) && timer->hw.open)
290 timer->hw.open(timer);
291 list_add_tail(&timeri->open_list, &timer->open_list_head);
292 snd_timer_check_master(timeri);
293 mutex_unlock(&register_mutex);
294 *ti = timeri;
295 return 0;
298 static int _snd_timer_stop(struct snd_timer_instance *timeri,
299 int keep_flag, int event);
302 * close a timer instance
304 int snd_timer_close(struct snd_timer_instance *timeri)
306 struct snd_timer *timer = NULL;
307 struct snd_timer_instance *slave, *tmp;
309 if (snd_BUG_ON(!timeri))
310 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 mutex_lock(&register_mutex);
325 list_del(&timeri->open_list);
326 mutex_unlock(&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 mutex_lock(&register_mutex);
338 list_del(&timeri->open_list);
339 if (timer && list_empty(&timer->open_list_head) &&
340 timer->hw.close)
341 timer->hw.close(timer);
342 /* remove slave links */
343 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
344 open_list) {
345 spin_lock_irq(&slave_active_lock);
346 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
347 list_move_tail(&slave->open_list, &snd_timer_slave_list);
348 slave->master = NULL;
349 slave->timer = NULL;
350 spin_unlock_irq(&slave_active_lock);
352 mutex_unlock(&register_mutex);
354 if (timeri->private_free)
355 timeri->private_free(timeri);
356 kfree(timeri->owner);
357 kfree(timeri);
358 if (timer)
359 module_put(timer->module);
360 return 0;
363 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
365 struct snd_timer * 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(struct snd_timer_instance *ti, int event)
379 struct snd_timer *timer;
380 unsigned long flags;
381 unsigned long resolution = 0;
382 struct snd_timer_instance *ts;
383 struct timespec tstamp;
385 if (timer_tstamp_monotonic)
386 do_posix_clock_monotonic_gettime(&tstamp);
387 else
388 getnstimeofday(&tstamp);
389 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
390 event > SNDRV_TIMER_EVENT_PAUSE))
391 return;
392 if (event == SNDRV_TIMER_EVENT_START ||
393 event == SNDRV_TIMER_EVENT_CONTINUE)
394 resolution = snd_timer_resolution(ti);
395 if (ti->ccallback)
396 ti->ccallback(ti, event, &tstamp, resolution);
397 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
398 return;
399 timer = ti->timer;
400 if (timer == NULL)
401 return;
402 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
403 return;
404 spin_lock_irqsave(&timer->lock, flags);
405 list_for_each_entry(ts, &ti->slave_active_head, active_list)
406 if (ts->ccallback)
407 ts->ccallback(ti, event + 100, &tstamp, resolution);
408 spin_unlock_irqrestore(&timer->lock, flags);
411 static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
412 unsigned long sticks)
414 list_del(&timeri->active_list);
415 list_add_tail(&timeri->active_list, &timer->active_list_head);
416 if (timer->running) {
417 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
418 goto __start_now;
419 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
420 timeri->flags |= SNDRV_TIMER_IFLG_START;
421 return 1; /* delayed start */
422 } else {
423 timer->sticks = sticks;
424 timer->hw.start(timer);
425 __start_now:
426 timer->running++;
427 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
428 return 0;
432 static int snd_timer_start_slave(struct snd_timer_instance *timeri)
434 unsigned long flags;
436 spin_lock_irqsave(&slave_active_lock, flags);
437 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
438 if (timeri->master)
439 list_add_tail(&timeri->active_list,
440 &timeri->master->slave_active_head);
441 spin_unlock_irqrestore(&slave_active_lock, flags);
442 return 1; /* delayed start */
446 * start the timer instance
448 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
450 struct snd_timer *timer;
451 int result = -EINVAL;
452 unsigned long flags;
454 if (timeri == NULL || ticks < 1)
455 return -EINVAL;
456 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
457 result = snd_timer_start_slave(timeri);
458 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
459 return result;
461 timer = timeri->timer;
462 if (timer == NULL)
463 return -EINVAL;
464 spin_lock_irqsave(&timer->lock, flags);
465 timeri->ticks = timeri->cticks = ticks;
466 timeri->pticks = 0;
467 result = snd_timer_start1(timer, timeri, ticks);
468 spin_unlock_irqrestore(&timer->lock, flags);
469 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
470 return result;
473 static int _snd_timer_stop(struct snd_timer_instance * timeri,
474 int keep_flag, int event)
476 struct snd_timer *timer;
477 unsigned long flags;
479 if (snd_BUG_ON(!timeri))
480 return -ENXIO;
482 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
483 if (!keep_flag) {
484 spin_lock_irqsave(&slave_active_lock, flags);
485 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
486 spin_unlock_irqrestore(&slave_active_lock, flags);
488 goto __end;
490 timer = timeri->timer;
491 if (!timer)
492 return -EINVAL;
493 spin_lock_irqsave(&timer->lock, flags);
494 list_del_init(&timeri->ack_list);
495 list_del_init(&timeri->active_list);
496 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
497 !(--timer->running)) {
498 timer->hw.stop(timer);
499 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
500 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
501 snd_timer_reschedule(timer, 0);
502 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
503 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
504 timer->hw.start(timer);
508 if (!keep_flag)
509 timeri->flags &=
510 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
511 spin_unlock_irqrestore(&timer->lock, flags);
512 __end:
513 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
514 snd_timer_notify1(timeri, event);
515 return 0;
519 * stop the timer instance.
521 * do not call this from the timer callback!
523 int snd_timer_stop(struct snd_timer_instance *timeri)
525 struct snd_timer *timer;
526 unsigned long flags;
527 int err;
529 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
530 if (err < 0)
531 return err;
532 timer = timeri->timer;
533 spin_lock_irqsave(&timer->lock, flags);
534 timeri->cticks = timeri->ticks;
535 timeri->pticks = 0;
536 spin_unlock_irqrestore(&timer->lock, flags);
537 return 0;
541 * start again.. the tick is kept.
543 int snd_timer_continue(struct snd_timer_instance *timeri)
545 struct snd_timer *timer;
546 int result = -EINVAL;
547 unsigned long flags;
549 if (timeri == NULL)
550 return result;
551 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
552 return snd_timer_start_slave(timeri);
553 timer = timeri->timer;
554 if (! timer)
555 return -EINVAL;
556 spin_lock_irqsave(&timer->lock, flags);
557 if (!timeri->cticks)
558 timeri->cticks = 1;
559 timeri->pticks = 0;
560 result = snd_timer_start1(timer, timeri, timer->sticks);
561 spin_unlock_irqrestore(&timer->lock, flags);
562 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
563 return result;
567 * pause.. remember the ticks left
569 int snd_timer_pause(struct snd_timer_instance * timeri)
571 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
575 * reschedule the timer
577 * start pending instances and check the scheduling ticks.
578 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
580 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
582 struct snd_timer_instance *ti;
583 unsigned long ticks = ~0UL;
585 list_for_each_entry(ti, &timer->active_list_head, active_list) {
586 if (ti->flags & SNDRV_TIMER_IFLG_START) {
587 ti->flags &= ~SNDRV_TIMER_IFLG_START;
588 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
589 timer->running++;
591 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
592 if (ticks > ti->cticks)
593 ticks = ti->cticks;
596 if (ticks == ~0UL) {
597 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
598 return;
600 if (ticks > timer->hw.ticks)
601 ticks = timer->hw.ticks;
602 if (ticks_left != ticks)
603 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
604 timer->sticks = ticks;
608 * timer tasklet
611 static void snd_timer_tasklet(unsigned long arg)
613 struct snd_timer *timer = (struct snd_timer *) arg;
614 struct snd_timer_instance *ti;
615 struct list_head *p;
616 unsigned long resolution, ticks;
617 unsigned long flags;
619 spin_lock_irqsave(&timer->lock, flags);
620 /* now process all callbacks */
621 while (!list_empty(&timer->sack_list_head)) {
622 p = timer->sack_list_head.next; /* get first item */
623 ti = list_entry(p, struct snd_timer_instance, ack_list);
625 /* remove from ack_list and make empty */
626 list_del_init(p);
628 ticks = ti->pticks;
629 ti->pticks = 0;
630 resolution = ti->resolution;
632 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
633 spin_unlock(&timer->lock);
634 if (ti->callback)
635 ti->callback(ti, resolution, ticks);
636 spin_lock(&timer->lock);
637 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
639 spin_unlock_irqrestore(&timer->lock, flags);
643 * timer interrupt
645 * ticks_left is usually equal to timer->sticks.
648 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
650 struct snd_timer_instance *ti, *ts, *tmp;
651 unsigned long resolution, ticks;
652 struct list_head *p, *ack_list_head;
653 unsigned long flags;
654 int use_tasklet = 0;
656 if (timer == NULL)
657 return;
659 spin_lock_irqsave(&timer->lock, flags);
661 /* remember the current resolution */
662 if (timer->hw.c_resolution)
663 resolution = timer->hw.c_resolution(timer);
664 else
665 resolution = timer->hw.resolution;
667 /* loop for all active instances
668 * Here we cannot use list_for_each_entry because the active_list of a
669 * processed instance is relinked to done_list_head before the callback
670 * is called.
672 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
673 active_list) {
674 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
675 continue;
676 ti->pticks += ticks_left;
677 ti->resolution = resolution;
678 if (ti->cticks < ticks_left)
679 ti->cticks = 0;
680 else
681 ti->cticks -= ticks_left;
682 if (ti->cticks) /* not expired */
683 continue;
684 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
685 ti->cticks = ti->ticks;
686 } else {
687 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
688 if (--timer->running)
689 list_del(&ti->active_list);
691 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
692 (ti->flags & SNDRV_TIMER_IFLG_FAST))
693 ack_list_head = &timer->ack_list_head;
694 else
695 ack_list_head = &timer->sack_list_head;
696 if (list_empty(&ti->ack_list))
697 list_add_tail(&ti->ack_list, ack_list_head);
698 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
699 ts->pticks = ti->pticks;
700 ts->resolution = resolution;
701 if (list_empty(&ts->ack_list))
702 list_add_tail(&ts->ack_list, ack_list_head);
705 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
706 snd_timer_reschedule(timer, timer->sticks);
707 if (timer->running) {
708 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
709 timer->hw.stop(timer);
710 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
712 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
713 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
714 /* restart timer */
715 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
716 timer->hw.start(timer);
718 } else {
719 timer->hw.stop(timer);
722 /* now process all fast callbacks */
723 while (!list_empty(&timer->ack_list_head)) {
724 p = timer->ack_list_head.next; /* get first item */
725 ti = list_entry(p, struct snd_timer_instance, ack_list);
727 /* remove from ack_list and make empty */
728 list_del_init(p);
730 ticks = ti->pticks;
731 ti->pticks = 0;
733 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
734 spin_unlock(&timer->lock);
735 if (ti->callback)
736 ti->callback(ti, resolution, ticks);
737 spin_lock(&timer->lock);
738 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
741 /* do we have any slow callbacks? */
742 use_tasklet = !list_empty(&timer->sack_list_head);
743 spin_unlock_irqrestore(&timer->lock, flags);
745 if (use_tasklet)
746 tasklet_schedule(&timer->task_queue);
753 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
754 struct snd_timer **rtimer)
756 struct snd_timer *timer;
757 int err;
758 static struct snd_device_ops ops = {
759 .dev_free = snd_timer_dev_free,
760 .dev_register = snd_timer_dev_register,
761 .dev_disconnect = snd_timer_dev_disconnect,
764 if (snd_BUG_ON(!tid))
765 return -EINVAL;
766 if (rtimer)
767 *rtimer = NULL;
768 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
769 if (timer == NULL) {
770 snd_printk(KERN_ERR "timer: cannot allocate\n");
771 return -ENOMEM;
773 timer->tmr_class = tid->dev_class;
774 timer->card = card;
775 timer->tmr_device = tid->device;
776 timer->tmr_subdevice = tid->subdevice;
777 if (id)
778 strlcpy(timer->id, id, sizeof(timer->id));
779 INIT_LIST_HEAD(&timer->device_list);
780 INIT_LIST_HEAD(&timer->open_list_head);
781 INIT_LIST_HEAD(&timer->active_list_head);
782 INIT_LIST_HEAD(&timer->ack_list_head);
783 INIT_LIST_HEAD(&timer->sack_list_head);
784 spin_lock_init(&timer->lock);
785 tasklet_init(&timer->task_queue, snd_timer_tasklet,
786 (unsigned long)timer);
787 if (card != NULL) {
788 timer->module = card->module;
789 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
790 if (err < 0) {
791 snd_timer_free(timer);
792 return err;
795 if (rtimer)
796 *rtimer = timer;
797 return 0;
800 static int snd_timer_free(struct snd_timer *timer)
802 if (!timer)
803 return 0;
805 mutex_lock(&register_mutex);
806 if (! list_empty(&timer->open_list_head)) {
807 struct list_head *p, *n;
808 struct snd_timer_instance *ti;
809 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
810 list_for_each_safe(p, n, &timer->open_list_head) {
811 list_del_init(p);
812 ti = list_entry(p, struct snd_timer_instance, open_list);
813 ti->timer = NULL;
816 list_del(&timer->device_list);
817 mutex_unlock(&register_mutex);
819 if (timer->private_free)
820 timer->private_free(timer);
821 kfree(timer);
822 return 0;
825 static int snd_timer_dev_free(struct snd_device *device)
827 struct snd_timer *timer = device->device_data;
828 return snd_timer_free(timer);
831 static int snd_timer_dev_register(struct snd_device *dev)
833 struct snd_timer *timer = dev->device_data;
834 struct snd_timer *timer1;
836 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
837 return -ENXIO;
838 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
839 !timer->hw.resolution && timer->hw.c_resolution == NULL)
840 return -EINVAL;
842 mutex_lock(&register_mutex);
843 list_for_each_entry(timer1, &snd_timer_list, device_list) {
844 if (timer1->tmr_class > timer->tmr_class)
845 break;
846 if (timer1->tmr_class < timer->tmr_class)
847 continue;
848 if (timer1->card && timer->card) {
849 if (timer1->card->number > timer->card->number)
850 break;
851 if (timer1->card->number < timer->card->number)
852 continue;
854 if (timer1->tmr_device > timer->tmr_device)
855 break;
856 if (timer1->tmr_device < timer->tmr_device)
857 continue;
858 if (timer1->tmr_subdevice > timer->tmr_subdevice)
859 break;
860 if (timer1->tmr_subdevice < timer->tmr_subdevice)
861 continue;
862 /* conflicts.. */
863 mutex_unlock(&register_mutex);
864 return -EBUSY;
866 list_add_tail(&timer->device_list, &timer1->device_list);
867 mutex_unlock(&register_mutex);
868 return 0;
871 static int snd_timer_dev_disconnect(struct snd_device *device)
873 struct snd_timer *timer = device->device_data;
874 mutex_lock(&register_mutex);
875 list_del_init(&timer->device_list);
876 mutex_unlock(&register_mutex);
877 return 0;
880 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
882 unsigned long flags;
883 unsigned long resolution = 0;
884 struct snd_timer_instance *ti, *ts;
886 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
887 return;
888 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
889 event > SNDRV_TIMER_EVENT_MRESUME))
890 return;
891 spin_lock_irqsave(&timer->lock, flags);
892 if (event == SNDRV_TIMER_EVENT_MSTART ||
893 event == SNDRV_TIMER_EVENT_MCONTINUE ||
894 event == SNDRV_TIMER_EVENT_MRESUME) {
895 if (timer->hw.c_resolution)
896 resolution = timer->hw.c_resolution(timer);
897 else
898 resolution = timer->hw.resolution;
900 list_for_each_entry(ti, &timer->active_list_head, active_list) {
901 if (ti->ccallback)
902 ti->ccallback(ti, event, tstamp, resolution);
903 list_for_each_entry(ts, &ti->slave_active_head, active_list)
904 if (ts->ccallback)
905 ts->ccallback(ts, event, tstamp, resolution);
907 spin_unlock_irqrestore(&timer->lock, flags);
911 * exported functions for global timers
913 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
915 struct snd_timer_id tid;
917 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
918 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
919 tid.card = -1;
920 tid.device = device;
921 tid.subdevice = 0;
922 return snd_timer_new(NULL, id, &tid, rtimer);
925 int snd_timer_global_free(struct snd_timer *timer)
927 return snd_timer_free(timer);
930 int snd_timer_global_register(struct snd_timer *timer)
932 struct snd_device dev;
934 memset(&dev, 0, sizeof(dev));
935 dev.device_data = timer;
936 return snd_timer_dev_register(&dev);
940 * System timer
943 struct snd_timer_system_private {
944 struct timer_list tlist;
945 unsigned long last_expires;
946 unsigned long last_jiffies;
947 unsigned long correction;
950 static void snd_timer_s_function(unsigned long data)
952 struct snd_timer *timer = (struct snd_timer *)data;
953 struct snd_timer_system_private *priv = timer->private_data;
954 unsigned long jiff = jiffies;
955 if (time_after(jiff, priv->last_expires))
956 priv->correction += (long)jiff - (long)priv->last_expires;
957 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
960 static int snd_timer_s_start(struct snd_timer * timer)
962 struct snd_timer_system_private *priv;
963 unsigned long njiff;
965 priv = (struct snd_timer_system_private *) timer->private_data;
966 njiff = (priv->last_jiffies = jiffies);
967 if (priv->correction > timer->sticks - 1) {
968 priv->correction -= timer->sticks - 1;
969 njiff++;
970 } else {
971 njiff += timer->sticks - priv->correction;
972 priv->correction = 0;
974 priv->last_expires = priv->tlist.expires = njiff;
975 add_timer(&priv->tlist);
976 return 0;
979 static int snd_timer_s_stop(struct snd_timer * timer)
981 struct snd_timer_system_private *priv;
982 unsigned long jiff;
984 priv = (struct snd_timer_system_private *) timer->private_data;
985 del_timer(&priv->tlist);
986 jiff = jiffies;
987 if (time_before(jiff, priv->last_expires))
988 timer->sticks = priv->last_expires - jiff;
989 else
990 timer->sticks = 1;
991 priv->correction = 0;
992 return 0;
995 static struct snd_timer_hardware snd_timer_system =
997 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
998 .resolution = 1000000000L / HZ,
999 .ticks = 10000000L,
1000 .start = snd_timer_s_start,
1001 .stop = snd_timer_s_stop
1004 static void snd_timer_free_system(struct snd_timer *timer)
1006 kfree(timer->private_data);
1009 static int snd_timer_register_system(void)
1011 struct snd_timer *timer;
1012 struct snd_timer_system_private *priv;
1013 int err;
1015 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1016 if (err < 0)
1017 return err;
1018 strcpy(timer->name, "system timer");
1019 timer->hw = snd_timer_system;
1020 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1021 if (priv == NULL) {
1022 snd_timer_free(timer);
1023 return -ENOMEM;
1025 init_timer(&priv->tlist);
1026 priv->tlist.function = snd_timer_s_function;
1027 priv->tlist.data = (unsigned long) timer;
1028 timer->private_data = priv;
1029 timer->private_free = snd_timer_free_system;
1030 return snd_timer_global_register(timer);
1033 #ifdef CONFIG_PROC_FS
1035 * Info interface
1038 static void snd_timer_proc_read(struct snd_info_entry *entry,
1039 struct snd_info_buffer *buffer)
1041 struct snd_timer *timer;
1042 struct snd_timer_instance *ti;
1044 mutex_lock(&register_mutex);
1045 list_for_each_entry(timer, &snd_timer_list, device_list) {
1046 switch (timer->tmr_class) {
1047 case SNDRV_TIMER_CLASS_GLOBAL:
1048 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1049 break;
1050 case SNDRV_TIMER_CLASS_CARD:
1051 snd_iprintf(buffer, "C%i-%i: ",
1052 timer->card->number, timer->tmr_device);
1053 break;
1054 case SNDRV_TIMER_CLASS_PCM:
1055 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1056 timer->tmr_device, timer->tmr_subdevice);
1057 break;
1058 default:
1059 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1060 timer->card ? timer->card->number : -1,
1061 timer->tmr_device, timer->tmr_subdevice);
1063 snd_iprintf(buffer, "%s :", timer->name);
1064 if (timer->hw.resolution)
1065 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1066 timer->hw.resolution / 1000,
1067 timer->hw.resolution % 1000,
1068 timer->hw.ticks);
1069 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1070 snd_iprintf(buffer, " SLAVE");
1071 snd_iprintf(buffer, "\n");
1072 list_for_each_entry(ti, &timer->open_list_head, open_list)
1073 snd_iprintf(buffer, " Client %s : %s\n",
1074 ti->owner ? ti->owner : "unknown",
1075 ti->flags & (SNDRV_TIMER_IFLG_START |
1076 SNDRV_TIMER_IFLG_RUNNING)
1077 ? "running" : "stopped");
1079 mutex_unlock(&register_mutex);
1082 static struct snd_info_entry *snd_timer_proc_entry;
1084 static void __init snd_timer_proc_init(void)
1086 struct snd_info_entry *entry;
1088 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1089 if (entry != NULL) {
1090 entry->c.text.read = snd_timer_proc_read;
1091 if (snd_info_register(entry) < 0) {
1092 snd_info_free_entry(entry);
1093 entry = NULL;
1096 snd_timer_proc_entry = entry;
1099 static void __exit snd_timer_proc_done(void)
1101 snd_info_free_entry(snd_timer_proc_entry);
1103 #else /* !CONFIG_PROC_FS */
1104 #define snd_timer_proc_init()
1105 #define snd_timer_proc_done()
1106 #endif
1109 * USER SPACE interface
1112 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1113 unsigned long resolution,
1114 unsigned long ticks)
1116 struct snd_timer_user *tu = timeri->callback_data;
1117 struct snd_timer_read *r;
1118 int prev;
1120 spin_lock(&tu->qlock);
1121 if (tu->qused > 0) {
1122 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1123 r = &tu->queue[prev];
1124 if (r->resolution == resolution) {
1125 r->ticks += ticks;
1126 goto __wake;
1129 if (tu->qused >= tu->queue_size) {
1130 tu->overrun++;
1131 } else {
1132 r = &tu->queue[tu->qtail++];
1133 tu->qtail %= tu->queue_size;
1134 r->resolution = resolution;
1135 r->ticks = ticks;
1136 tu->qused++;
1138 __wake:
1139 spin_unlock(&tu->qlock);
1140 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1141 wake_up(&tu->qchange_sleep);
1144 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1145 struct snd_timer_tread *tread)
1147 if (tu->qused >= tu->queue_size) {
1148 tu->overrun++;
1149 } else {
1150 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1151 tu->qtail %= tu->queue_size;
1152 tu->qused++;
1156 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1157 int event,
1158 struct timespec *tstamp,
1159 unsigned long resolution)
1161 struct snd_timer_user *tu = timeri->callback_data;
1162 struct snd_timer_tread r1;
1163 unsigned long flags;
1165 if (event >= SNDRV_TIMER_EVENT_START &&
1166 event <= SNDRV_TIMER_EVENT_PAUSE)
1167 tu->tstamp = *tstamp;
1168 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1169 return;
1170 r1.event = event;
1171 r1.tstamp = *tstamp;
1172 r1.val = resolution;
1173 spin_lock_irqsave(&tu->qlock, flags);
1174 snd_timer_user_append_to_tqueue(tu, &r1);
1175 spin_unlock_irqrestore(&tu->qlock, flags);
1176 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1177 wake_up(&tu->qchange_sleep);
1180 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1181 unsigned long resolution,
1182 unsigned long ticks)
1184 struct snd_timer_user *tu = timeri->callback_data;
1185 struct snd_timer_tread *r, r1;
1186 struct timespec tstamp;
1187 int prev, append = 0;
1189 memset(&tstamp, 0, sizeof(tstamp));
1190 spin_lock(&tu->qlock);
1191 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1192 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1193 spin_unlock(&tu->qlock);
1194 return;
1196 if (tu->last_resolution != resolution || ticks > 0) {
1197 if (timer_tstamp_monotonic)
1198 do_posix_clock_monotonic_gettime(&tstamp);
1199 else
1200 getnstimeofday(&tstamp);
1202 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1203 tu->last_resolution != resolution) {
1204 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1205 r1.tstamp = tstamp;
1206 r1.val = resolution;
1207 snd_timer_user_append_to_tqueue(tu, &r1);
1208 tu->last_resolution = resolution;
1209 append++;
1211 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1212 goto __wake;
1213 if (ticks == 0)
1214 goto __wake;
1215 if (tu->qused > 0) {
1216 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1217 r = &tu->tqueue[prev];
1218 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1219 r->tstamp = tstamp;
1220 r->val += ticks;
1221 append++;
1222 goto __wake;
1225 r1.event = SNDRV_TIMER_EVENT_TICK;
1226 r1.tstamp = tstamp;
1227 r1.val = ticks;
1228 snd_timer_user_append_to_tqueue(tu, &r1);
1229 append++;
1230 __wake:
1231 spin_unlock(&tu->qlock);
1232 if (append == 0)
1233 return;
1234 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1235 wake_up(&tu->qchange_sleep);
1238 static int snd_timer_user_open(struct inode *inode, struct file *file)
1240 struct snd_timer_user *tu;
1241 int err;
1243 err = nonseekable_open(inode, file);
1244 if (err < 0)
1245 return err;
1247 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1248 if (tu == NULL)
1249 return -ENOMEM;
1250 spin_lock_init(&tu->qlock);
1251 init_waitqueue_head(&tu->qchange_sleep);
1252 mutex_init(&tu->tread_sem);
1253 tu->ticks = 1;
1254 tu->queue_size = 128;
1255 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1256 GFP_KERNEL);
1257 if (tu->queue == NULL) {
1258 kfree(tu);
1259 return -ENOMEM;
1261 file->private_data = tu;
1262 return 0;
1265 static int snd_timer_user_release(struct inode *inode, struct file *file)
1267 struct snd_timer_user *tu;
1269 if (file->private_data) {
1270 tu = file->private_data;
1271 file->private_data = NULL;
1272 if (tu->timeri)
1273 snd_timer_close(tu->timeri);
1274 kfree(tu->queue);
1275 kfree(tu->tqueue);
1276 kfree(tu);
1278 return 0;
1281 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1283 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1284 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1285 id->card = -1;
1286 id->device = -1;
1287 id->subdevice = -1;
1290 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1292 id->dev_class = timer->tmr_class;
1293 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1294 id->card = timer->card ? timer->card->number : -1;
1295 id->device = timer->tmr_device;
1296 id->subdevice = timer->tmr_subdevice;
1299 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1301 struct snd_timer_id id;
1302 struct snd_timer *timer;
1303 struct list_head *p;
1305 if (copy_from_user(&id, _tid, sizeof(id)))
1306 return -EFAULT;
1307 mutex_lock(&register_mutex);
1308 if (id.dev_class < 0) { /* first item */
1309 if (list_empty(&snd_timer_list))
1310 snd_timer_user_zero_id(&id);
1311 else {
1312 timer = list_entry(snd_timer_list.next,
1313 struct snd_timer, device_list);
1314 snd_timer_user_copy_id(&id, timer);
1316 } else {
1317 switch (id.dev_class) {
1318 case SNDRV_TIMER_CLASS_GLOBAL:
1319 id.device = id.device < 0 ? 0 : id.device + 1;
1320 list_for_each(p, &snd_timer_list) {
1321 timer = list_entry(p, struct snd_timer, device_list);
1322 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1323 snd_timer_user_copy_id(&id, timer);
1324 break;
1326 if (timer->tmr_device >= id.device) {
1327 snd_timer_user_copy_id(&id, timer);
1328 break;
1331 if (p == &snd_timer_list)
1332 snd_timer_user_zero_id(&id);
1333 break;
1334 case SNDRV_TIMER_CLASS_CARD:
1335 case SNDRV_TIMER_CLASS_PCM:
1336 if (id.card < 0) {
1337 id.card = 0;
1338 } else {
1339 if (id.card < 0) {
1340 id.card = 0;
1341 } else {
1342 if (id.device < 0) {
1343 id.device = 0;
1344 } else {
1345 if (id.subdevice < 0) {
1346 id.subdevice = 0;
1347 } else {
1348 id.subdevice++;
1353 list_for_each(p, &snd_timer_list) {
1354 timer = list_entry(p, struct snd_timer, device_list);
1355 if (timer->tmr_class > id.dev_class) {
1356 snd_timer_user_copy_id(&id, timer);
1357 break;
1359 if (timer->tmr_class < id.dev_class)
1360 continue;
1361 if (timer->card->number > id.card) {
1362 snd_timer_user_copy_id(&id, timer);
1363 break;
1365 if (timer->card->number < id.card)
1366 continue;
1367 if (timer->tmr_device > id.device) {
1368 snd_timer_user_copy_id(&id, timer);
1369 break;
1371 if (timer->tmr_device < id.device)
1372 continue;
1373 if (timer->tmr_subdevice > id.subdevice) {
1374 snd_timer_user_copy_id(&id, timer);
1375 break;
1377 if (timer->tmr_subdevice < id.subdevice)
1378 continue;
1379 snd_timer_user_copy_id(&id, timer);
1380 break;
1382 if (p == &snd_timer_list)
1383 snd_timer_user_zero_id(&id);
1384 break;
1385 default:
1386 snd_timer_user_zero_id(&id);
1389 mutex_unlock(&register_mutex);
1390 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1391 return -EFAULT;
1392 return 0;
1395 static int snd_timer_user_ginfo(struct file *file,
1396 struct snd_timer_ginfo __user *_ginfo)
1398 struct snd_timer_ginfo *ginfo;
1399 struct snd_timer_id tid;
1400 struct snd_timer *t;
1401 struct list_head *p;
1402 int err = 0;
1404 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1405 if (IS_ERR(ginfo))
1406 return PTR_ERR(ginfo);
1408 tid = ginfo->tid;
1409 memset(ginfo, 0, sizeof(*ginfo));
1410 ginfo->tid = tid;
1411 mutex_lock(&register_mutex);
1412 t = snd_timer_find(&tid);
1413 if (t != NULL) {
1414 ginfo->card = t->card ? t->card->number : -1;
1415 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1416 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1417 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1418 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1419 ginfo->resolution = t->hw.resolution;
1420 if (t->hw.resolution_min > 0) {
1421 ginfo->resolution_min = t->hw.resolution_min;
1422 ginfo->resolution_max = t->hw.resolution_max;
1424 list_for_each(p, &t->open_list_head) {
1425 ginfo->clients++;
1427 } else {
1428 err = -ENODEV;
1430 mutex_unlock(&register_mutex);
1431 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1432 err = -EFAULT;
1433 kfree(ginfo);
1434 return err;
1437 static int snd_timer_user_gparams(struct file *file,
1438 struct snd_timer_gparams __user *_gparams)
1440 struct snd_timer_gparams gparams;
1441 struct snd_timer *t;
1442 int err;
1444 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1445 return -EFAULT;
1446 mutex_lock(&register_mutex);
1447 t = snd_timer_find(&gparams.tid);
1448 if (!t) {
1449 err = -ENODEV;
1450 goto _error;
1452 if (!list_empty(&t->open_list_head)) {
1453 err = -EBUSY;
1454 goto _error;
1456 if (!t->hw.set_period) {
1457 err = -ENOSYS;
1458 goto _error;
1460 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1461 _error:
1462 mutex_unlock(&register_mutex);
1463 return err;
1466 static int snd_timer_user_gstatus(struct file *file,
1467 struct snd_timer_gstatus __user *_gstatus)
1469 struct snd_timer_gstatus gstatus;
1470 struct snd_timer_id tid;
1471 struct snd_timer *t;
1472 int err = 0;
1474 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1475 return -EFAULT;
1476 tid = gstatus.tid;
1477 memset(&gstatus, 0, sizeof(gstatus));
1478 gstatus.tid = tid;
1479 mutex_lock(&register_mutex);
1480 t = snd_timer_find(&tid);
1481 if (t != NULL) {
1482 if (t->hw.c_resolution)
1483 gstatus.resolution = t->hw.c_resolution(t);
1484 else
1485 gstatus.resolution = t->hw.resolution;
1486 if (t->hw.precise_resolution) {
1487 t->hw.precise_resolution(t, &gstatus.resolution_num,
1488 &gstatus.resolution_den);
1489 } else {
1490 gstatus.resolution_num = gstatus.resolution;
1491 gstatus.resolution_den = 1000000000uL;
1493 } else {
1494 err = -ENODEV;
1496 mutex_unlock(&register_mutex);
1497 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1498 err = -EFAULT;
1499 return err;
1502 static int snd_timer_user_tselect(struct file *file,
1503 struct snd_timer_select __user *_tselect)
1505 struct snd_timer_user *tu;
1506 struct snd_timer_select tselect;
1507 char str[32];
1508 int err = 0;
1510 tu = file->private_data;
1511 mutex_lock(&tu->tread_sem);
1512 if (tu->timeri) {
1513 snd_timer_close(tu->timeri);
1514 tu->timeri = NULL;
1516 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1517 err = -EFAULT;
1518 goto __err;
1520 sprintf(str, "application %i", current->pid);
1521 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1522 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1523 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1524 if (err < 0)
1525 goto __err;
1527 kfree(tu->queue);
1528 tu->queue = NULL;
1529 kfree(tu->tqueue);
1530 tu->tqueue = NULL;
1531 if (tu->tread) {
1532 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1533 GFP_KERNEL);
1534 if (tu->tqueue == NULL)
1535 err = -ENOMEM;
1536 } else {
1537 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1538 GFP_KERNEL);
1539 if (tu->queue == NULL)
1540 err = -ENOMEM;
1543 if (err < 0) {
1544 snd_timer_close(tu->timeri);
1545 tu->timeri = NULL;
1546 } else {
1547 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1548 tu->timeri->callback = tu->tread
1549 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1550 tu->timeri->ccallback = snd_timer_user_ccallback;
1551 tu->timeri->callback_data = (void *)tu;
1554 __err:
1555 mutex_unlock(&tu->tread_sem);
1556 return err;
1559 static int snd_timer_user_info(struct file *file,
1560 struct snd_timer_info __user *_info)
1562 struct snd_timer_user *tu;
1563 struct snd_timer_info *info;
1564 struct snd_timer *t;
1565 int err = 0;
1567 tu = file->private_data;
1568 if (!tu->timeri)
1569 return -EBADFD;
1570 t = tu->timeri->timer;
1571 if (!t)
1572 return -EBADFD;
1574 info = kzalloc(sizeof(*info), GFP_KERNEL);
1575 if (! info)
1576 return -ENOMEM;
1577 info->card = t->card ? t->card->number : -1;
1578 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1579 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1580 strlcpy(info->id, t->id, sizeof(info->id));
1581 strlcpy(info->name, t->name, sizeof(info->name));
1582 info->resolution = t->hw.resolution;
1583 if (copy_to_user(_info, info, sizeof(*_info)))
1584 err = -EFAULT;
1585 kfree(info);
1586 return err;
1589 static int snd_timer_user_params(struct file *file,
1590 struct snd_timer_params __user *_params)
1592 struct snd_timer_user *tu;
1593 struct snd_timer_params params;
1594 struct snd_timer *t;
1595 struct snd_timer_read *tr;
1596 struct snd_timer_tread *ttr;
1597 int err;
1599 tu = file->private_data;
1600 if (!tu->timeri)
1601 return -EBADFD;
1602 t = tu->timeri->timer;
1603 if (!t)
1604 return -EBADFD;
1605 if (copy_from_user(&params, _params, sizeof(params)))
1606 return -EFAULT;
1607 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1608 err = -EINVAL;
1609 goto _end;
1611 if (params.queue_size > 0 &&
1612 (params.queue_size < 32 || params.queue_size > 1024)) {
1613 err = -EINVAL;
1614 goto _end;
1616 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1617 (1<<SNDRV_TIMER_EVENT_TICK)|
1618 (1<<SNDRV_TIMER_EVENT_START)|
1619 (1<<SNDRV_TIMER_EVENT_STOP)|
1620 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1621 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1622 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1623 (1<<SNDRV_TIMER_EVENT_RESUME)|
1624 (1<<SNDRV_TIMER_EVENT_MSTART)|
1625 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1626 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1627 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1628 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1629 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1630 err = -EINVAL;
1631 goto _end;
1633 snd_timer_stop(tu->timeri);
1634 spin_lock_irq(&t->lock);
1635 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1636 SNDRV_TIMER_IFLG_EXCLUSIVE|
1637 SNDRV_TIMER_IFLG_EARLY_EVENT);
1638 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1639 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1640 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1641 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1642 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1643 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1644 spin_unlock_irq(&t->lock);
1645 if (params.queue_size > 0 &&
1646 (unsigned int)tu->queue_size != params.queue_size) {
1647 if (tu->tread) {
1648 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1649 GFP_KERNEL);
1650 if (ttr) {
1651 kfree(tu->tqueue);
1652 tu->queue_size = params.queue_size;
1653 tu->tqueue = ttr;
1655 } else {
1656 tr = kmalloc(params.queue_size * sizeof(*tr),
1657 GFP_KERNEL);
1658 if (tr) {
1659 kfree(tu->queue);
1660 tu->queue_size = params.queue_size;
1661 tu->queue = tr;
1665 tu->qhead = tu->qtail = tu->qused = 0;
1666 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1667 if (tu->tread) {
1668 struct snd_timer_tread tread;
1669 tread.event = SNDRV_TIMER_EVENT_EARLY;
1670 tread.tstamp.tv_sec = 0;
1671 tread.tstamp.tv_nsec = 0;
1672 tread.val = 0;
1673 snd_timer_user_append_to_tqueue(tu, &tread);
1674 } else {
1675 struct snd_timer_read *r = &tu->queue[0];
1676 r->resolution = 0;
1677 r->ticks = 0;
1678 tu->qused++;
1679 tu->qtail++;
1682 tu->filter = params.filter;
1683 tu->ticks = params.ticks;
1684 err = 0;
1685 _end:
1686 if (copy_to_user(_params, &params, sizeof(params)))
1687 return -EFAULT;
1688 return err;
1691 static int snd_timer_user_status(struct file *file,
1692 struct snd_timer_status __user *_status)
1694 struct snd_timer_user *tu;
1695 struct snd_timer_status status;
1697 tu = file->private_data;
1698 if (!tu->timeri)
1699 return -EBADFD;
1700 memset(&status, 0, sizeof(status));
1701 status.tstamp = tu->tstamp;
1702 status.resolution = snd_timer_resolution(tu->timeri);
1703 status.lost = tu->timeri->lost;
1704 status.overrun = tu->overrun;
1705 spin_lock_irq(&tu->qlock);
1706 status.queue = tu->qused;
1707 spin_unlock_irq(&tu->qlock);
1708 if (copy_to_user(_status, &status, sizeof(status)))
1709 return -EFAULT;
1710 return 0;
1713 static int snd_timer_user_start(struct file *file)
1715 int err;
1716 struct snd_timer_user *tu;
1718 tu = file->private_data;
1719 if (!tu->timeri)
1720 return -EBADFD;
1721 snd_timer_stop(tu->timeri);
1722 tu->timeri->lost = 0;
1723 tu->last_resolution = 0;
1724 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1727 static int snd_timer_user_stop(struct file *file)
1729 int err;
1730 struct snd_timer_user *tu;
1732 tu = file->private_data;
1733 if (!tu->timeri)
1734 return -EBADFD;
1735 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1738 static int snd_timer_user_continue(struct file *file)
1740 int err;
1741 struct snd_timer_user *tu;
1743 tu = file->private_data;
1744 if (!tu->timeri)
1745 return -EBADFD;
1746 tu->timeri->lost = 0;
1747 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1750 static int snd_timer_user_pause(struct file *file)
1752 int err;
1753 struct snd_timer_user *tu;
1755 tu = file->private_data;
1756 if (!tu->timeri)
1757 return -EBADFD;
1758 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1761 enum {
1762 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1763 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1764 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1765 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1768 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1769 unsigned long arg)
1771 struct snd_timer_user *tu;
1772 void __user *argp = (void __user *)arg;
1773 int __user *p = argp;
1775 tu = file->private_data;
1776 switch (cmd) {
1777 case SNDRV_TIMER_IOCTL_PVERSION:
1778 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1779 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1780 return snd_timer_user_next_device(argp);
1781 case SNDRV_TIMER_IOCTL_TREAD:
1783 int xarg;
1785 mutex_lock(&tu->tread_sem);
1786 if (tu->timeri) { /* too late */
1787 mutex_unlock(&tu->tread_sem);
1788 return -EBUSY;
1790 if (get_user(xarg, p)) {
1791 mutex_unlock(&tu->tread_sem);
1792 return -EFAULT;
1794 tu->tread = xarg ? 1 : 0;
1795 mutex_unlock(&tu->tread_sem);
1796 return 0;
1798 case SNDRV_TIMER_IOCTL_GINFO:
1799 return snd_timer_user_ginfo(file, argp);
1800 case SNDRV_TIMER_IOCTL_GPARAMS:
1801 return snd_timer_user_gparams(file, argp);
1802 case SNDRV_TIMER_IOCTL_GSTATUS:
1803 return snd_timer_user_gstatus(file, argp);
1804 case SNDRV_TIMER_IOCTL_SELECT:
1805 return snd_timer_user_tselect(file, argp);
1806 case SNDRV_TIMER_IOCTL_INFO:
1807 return snd_timer_user_info(file, argp);
1808 case SNDRV_TIMER_IOCTL_PARAMS:
1809 return snd_timer_user_params(file, argp);
1810 case SNDRV_TIMER_IOCTL_STATUS:
1811 return snd_timer_user_status(file, argp);
1812 case SNDRV_TIMER_IOCTL_START:
1813 case SNDRV_TIMER_IOCTL_START_OLD:
1814 return snd_timer_user_start(file);
1815 case SNDRV_TIMER_IOCTL_STOP:
1816 case SNDRV_TIMER_IOCTL_STOP_OLD:
1817 return snd_timer_user_stop(file);
1818 case SNDRV_TIMER_IOCTL_CONTINUE:
1819 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1820 return snd_timer_user_continue(file);
1821 case SNDRV_TIMER_IOCTL_PAUSE:
1822 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1823 return snd_timer_user_pause(file);
1825 return -ENOTTY;
1828 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1830 struct snd_timer_user *tu;
1832 tu = file->private_data;
1833 return fasync_helper(fd, file, on, &tu->fasync);
1836 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1837 size_t count, loff_t *offset)
1839 struct snd_timer_user *tu;
1840 long result = 0, unit;
1841 int err = 0;
1843 tu = file->private_data;
1844 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1845 spin_lock_irq(&tu->qlock);
1846 while ((long)count - result >= unit) {
1847 while (!tu->qused) {
1848 wait_queue_t wait;
1850 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1851 err = -EAGAIN;
1852 break;
1855 set_current_state(TASK_INTERRUPTIBLE);
1856 init_waitqueue_entry(&wait, current);
1857 add_wait_queue(&tu->qchange_sleep, &wait);
1859 spin_unlock_irq(&tu->qlock);
1860 schedule();
1861 spin_lock_irq(&tu->qlock);
1863 remove_wait_queue(&tu->qchange_sleep, &wait);
1865 if (signal_pending(current)) {
1866 err = -ERESTARTSYS;
1867 break;
1871 spin_unlock_irq(&tu->qlock);
1872 if (err < 0)
1873 goto _error;
1875 if (tu->tread) {
1876 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1877 sizeof(struct snd_timer_tread))) {
1878 err = -EFAULT;
1879 goto _error;
1881 } else {
1882 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1883 sizeof(struct snd_timer_read))) {
1884 err = -EFAULT;
1885 goto _error;
1889 tu->qhead %= tu->queue_size;
1891 result += unit;
1892 buffer += unit;
1894 spin_lock_irq(&tu->qlock);
1895 tu->qused--;
1897 spin_unlock_irq(&tu->qlock);
1898 _error:
1899 return result > 0 ? result : err;
1902 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1904 unsigned int mask;
1905 struct snd_timer_user *tu;
1907 tu = file->private_data;
1909 poll_wait(file, &tu->qchange_sleep, wait);
1911 mask = 0;
1912 if (tu->qused)
1913 mask |= POLLIN | POLLRDNORM;
1915 return mask;
1918 #ifdef CONFIG_COMPAT
1919 #include "timer_compat.c"
1920 #else
1921 #define snd_timer_user_ioctl_compat NULL
1922 #endif
1924 static const struct file_operations snd_timer_f_ops =
1926 .owner = THIS_MODULE,
1927 .read = snd_timer_user_read,
1928 .open = snd_timer_user_open,
1929 .release = snd_timer_user_release,
1930 .llseek = no_llseek,
1931 .poll = snd_timer_user_poll,
1932 .unlocked_ioctl = snd_timer_user_ioctl,
1933 .compat_ioctl = snd_timer_user_ioctl_compat,
1934 .fasync = snd_timer_user_fasync,
1938 * ENTRY functions
1941 static int __init alsa_timer_init(void)
1943 int err;
1945 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1946 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1947 "system timer");
1948 #endif
1950 if ((err = snd_timer_register_system()) < 0)
1951 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1952 err);
1953 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1954 &snd_timer_f_ops, NULL, "timer")) < 0)
1955 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1956 err);
1957 snd_timer_proc_init();
1958 return 0;
1961 static void __exit alsa_timer_exit(void)
1963 struct list_head *p, *n;
1965 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1966 /* unregister the system timer */
1967 list_for_each_safe(p, n, &snd_timer_list) {
1968 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
1969 snd_timer_free(timer);
1971 snd_timer_proc_done();
1972 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1973 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1974 #endif
1977 module_init(alsa_timer_init)
1978 module_exit(alsa_timer_exit)
1980 EXPORT_SYMBOL(snd_timer_open);
1981 EXPORT_SYMBOL(snd_timer_close);
1982 EXPORT_SYMBOL(snd_timer_resolution);
1983 EXPORT_SYMBOL(snd_timer_start);
1984 EXPORT_SYMBOL(snd_timer_stop);
1985 EXPORT_SYMBOL(snd_timer_continue);
1986 EXPORT_SYMBOL(snd_timer_pause);
1987 EXPORT_SYMBOL(snd_timer_new);
1988 EXPORT_SYMBOL(snd_timer_notify);
1989 EXPORT_SYMBOL(snd_timer_global_new);
1990 EXPORT_SYMBOL(snd_timer_global_free);
1991 EXPORT_SYMBOL(snd_timer_global_register);
1992 EXPORT_SYMBOL(snd_timer_interrupt);