m68knommu: add info about removing mcfserial
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / core / timer.c
blob9d8184a2c2d06350f44d294fdabd3d4797c6b638
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_KMOD
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_KMOD
263 if (timer == NULL) {
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 snd_assert(timeri != NULL, 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 snd_assert(event >= SNDRV_TIMER_EVENT_START &&
389 event <= SNDRV_TIMER_EVENT_PAUSE, return);
390 if (event == SNDRV_TIMER_EVENT_START ||
391 event == SNDRV_TIMER_EVENT_CONTINUE)
392 resolution = snd_timer_resolution(ti);
393 if (ti->ccallback)
394 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
395 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
396 return;
397 timer = ti->timer;
398 if (timer == NULL)
399 return;
400 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
401 return;
402 spin_lock_irqsave(&timer->lock, flags);
403 list_for_each_entry(ts, &ti->slave_active_head, active_list)
404 if (ts->ccallback)
405 ts->ccallback(ti, event + 100, &tstamp, resolution);
406 spin_unlock_irqrestore(&timer->lock, flags);
409 static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
410 unsigned long sticks)
412 list_del(&timeri->active_list);
413 list_add_tail(&timeri->active_list, &timer->active_list_head);
414 if (timer->running) {
415 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
416 goto __start_now;
417 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
418 timeri->flags |= SNDRV_TIMER_IFLG_START;
419 return 1; /* delayed start */
420 } else {
421 timer->sticks = sticks;
422 timer->hw.start(timer);
423 __start_now:
424 timer->running++;
425 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
426 return 0;
430 static int snd_timer_start_slave(struct snd_timer_instance *timeri)
432 unsigned long flags;
434 spin_lock_irqsave(&slave_active_lock, flags);
435 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
436 if (timeri->master)
437 list_add_tail(&timeri->active_list,
438 &timeri->master->slave_active_head);
439 spin_unlock_irqrestore(&slave_active_lock, flags);
440 return 1; /* delayed start */
444 * start the timer instance
446 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
448 struct snd_timer *timer;
449 int result = -EINVAL;
450 unsigned long flags;
452 if (timeri == NULL || ticks < 1)
453 return -EINVAL;
454 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
455 result = snd_timer_start_slave(timeri);
456 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
457 return result;
459 timer = timeri->timer;
460 if (timer == NULL)
461 return -EINVAL;
462 spin_lock_irqsave(&timer->lock, flags);
463 timeri->ticks = timeri->cticks = ticks;
464 timeri->pticks = 0;
465 result = snd_timer_start1(timer, timeri, ticks);
466 spin_unlock_irqrestore(&timer->lock, flags);
467 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
468 return result;
471 static int _snd_timer_stop(struct snd_timer_instance * timeri,
472 int keep_flag, int event)
474 struct snd_timer *timer;
475 unsigned long flags;
477 snd_assert(timeri != NULL, return -ENXIO);
479 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
480 if (!keep_flag) {
481 spin_lock_irqsave(&slave_active_lock, flags);
482 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
483 spin_unlock_irqrestore(&slave_active_lock, flags);
485 goto __end;
487 timer = timeri->timer;
488 if (!timer)
489 return -EINVAL;
490 spin_lock_irqsave(&timer->lock, flags);
491 list_del_init(&timeri->ack_list);
492 list_del_init(&timeri->active_list);
493 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
494 !(--timer->running)) {
495 timer->hw.stop(timer);
496 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
497 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
498 snd_timer_reschedule(timer, 0);
499 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
500 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
501 timer->hw.start(timer);
505 if (!keep_flag)
506 timeri->flags &=
507 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
508 spin_unlock_irqrestore(&timer->lock, flags);
509 __end:
510 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
511 snd_timer_notify1(timeri, event);
512 return 0;
516 * stop the timer instance.
518 * do not call this from the timer callback!
520 int snd_timer_stop(struct snd_timer_instance *timeri)
522 struct snd_timer *timer;
523 unsigned long flags;
524 int err;
526 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
527 if (err < 0)
528 return err;
529 timer = timeri->timer;
530 spin_lock_irqsave(&timer->lock, flags);
531 timeri->cticks = timeri->ticks;
532 timeri->pticks = 0;
533 spin_unlock_irqrestore(&timer->lock, flags);
534 return 0;
538 * start again.. the tick is kept.
540 int snd_timer_continue(struct snd_timer_instance *timeri)
542 struct snd_timer *timer;
543 int result = -EINVAL;
544 unsigned long flags;
546 if (timeri == NULL)
547 return result;
548 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
549 return snd_timer_start_slave(timeri);
550 timer = timeri->timer;
551 if (! timer)
552 return -EINVAL;
553 spin_lock_irqsave(&timer->lock, flags);
554 if (!timeri->cticks)
555 timeri->cticks = 1;
556 timeri->pticks = 0;
557 result = snd_timer_start1(timer, timeri, timer->sticks);
558 spin_unlock_irqrestore(&timer->lock, flags);
559 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
560 return result;
564 * pause.. remember the ticks left
566 int snd_timer_pause(struct snd_timer_instance * timeri)
568 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
572 * reschedule the timer
574 * start pending instances and check the scheduling ticks.
575 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
577 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
579 struct snd_timer_instance *ti;
580 unsigned long ticks = ~0UL;
582 list_for_each_entry(ti, &timer->active_list_head, active_list) {
583 if (ti->flags & SNDRV_TIMER_IFLG_START) {
584 ti->flags &= ~SNDRV_TIMER_IFLG_START;
585 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
586 timer->running++;
588 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
589 if (ticks > ti->cticks)
590 ticks = ti->cticks;
593 if (ticks == ~0UL) {
594 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
595 return;
597 if (ticks > timer->hw.ticks)
598 ticks = timer->hw.ticks;
599 if (ticks_left != ticks)
600 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
601 timer->sticks = ticks;
605 * timer tasklet
608 static void snd_timer_tasklet(unsigned long arg)
610 struct snd_timer *timer = (struct snd_timer *) arg;
611 struct snd_timer_instance *ti;
612 struct list_head *p;
613 unsigned long resolution, ticks;
614 unsigned long flags;
616 spin_lock_irqsave(&timer->lock, flags);
617 /* now process all callbacks */
618 while (!list_empty(&timer->sack_list_head)) {
619 p = timer->sack_list_head.next; /* get first item */
620 ti = list_entry(p, struct snd_timer_instance, ack_list);
622 /* remove from ack_list and make empty */
623 list_del_init(p);
625 ticks = ti->pticks;
626 ti->pticks = 0;
627 resolution = ti->resolution;
629 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
630 spin_unlock(&timer->lock);
631 if (ti->callback)
632 ti->callback(ti, resolution, ticks);
633 spin_lock(&timer->lock);
634 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
636 spin_unlock_irqrestore(&timer->lock, flags);
640 * timer interrupt
642 * ticks_left is usually equal to timer->sticks.
645 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
647 struct snd_timer_instance *ti, *ts, *tmp;
648 unsigned long resolution, ticks;
649 struct list_head *p, *ack_list_head;
650 unsigned long flags;
651 int use_tasklet = 0;
653 if (timer == NULL)
654 return;
656 spin_lock_irqsave(&timer->lock, flags);
658 /* remember the current resolution */
659 if (timer->hw.c_resolution)
660 resolution = timer->hw.c_resolution(timer);
661 else
662 resolution = timer->hw.resolution;
664 /* loop for all active instances
665 * Here we cannot use list_for_each_entry because the active_list of a
666 * processed instance is relinked to done_list_head before the callback
667 * is called.
669 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
670 active_list) {
671 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
672 continue;
673 ti->pticks += ticks_left;
674 ti->resolution = resolution;
675 if (ti->cticks < ticks_left)
676 ti->cticks = 0;
677 else
678 ti->cticks -= ticks_left;
679 if (ti->cticks) /* not expired */
680 continue;
681 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
682 ti->cticks = ti->ticks;
683 } else {
684 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
685 if (--timer->running)
686 list_del(&ti->active_list);
688 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
689 (ti->flags & SNDRV_TIMER_IFLG_FAST))
690 ack_list_head = &timer->ack_list_head;
691 else
692 ack_list_head = &timer->sack_list_head;
693 if (list_empty(&ti->ack_list))
694 list_add_tail(&ti->ack_list, ack_list_head);
695 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
696 ts->pticks = ti->pticks;
697 ts->resolution = resolution;
698 if (list_empty(&ts->ack_list))
699 list_add_tail(&ts->ack_list, ack_list_head);
702 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
703 snd_timer_reschedule(timer, timer->sticks);
704 if (timer->running) {
705 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
706 timer->hw.stop(timer);
707 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
709 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
710 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
711 /* restart timer */
712 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
713 timer->hw.start(timer);
715 } else {
716 timer->hw.stop(timer);
719 /* now process all fast callbacks */
720 while (!list_empty(&timer->ack_list_head)) {
721 p = timer->ack_list_head.next; /* get first item */
722 ti = list_entry(p, struct snd_timer_instance, ack_list);
724 /* remove from ack_list and make empty */
725 list_del_init(p);
727 ticks = ti->pticks;
728 ti->pticks = 0;
730 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
731 spin_unlock(&timer->lock);
732 if (ti->callback)
733 ti->callback(ti, resolution, ticks);
734 spin_lock(&timer->lock);
735 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
738 /* do we have any slow callbacks? */
739 use_tasklet = !list_empty(&timer->sack_list_head);
740 spin_unlock_irqrestore(&timer->lock, flags);
742 if (use_tasklet)
743 tasklet_hi_schedule(&timer->task_queue);
750 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
751 struct snd_timer **rtimer)
753 struct snd_timer *timer;
754 int err;
755 static struct snd_device_ops ops = {
756 .dev_free = snd_timer_dev_free,
757 .dev_register = snd_timer_dev_register,
758 .dev_disconnect = snd_timer_dev_disconnect,
761 snd_assert(tid != NULL, return -EINVAL);
762 snd_assert(rtimer != NULL, return -EINVAL);
763 *rtimer = NULL;
764 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
765 if (timer == NULL) {
766 snd_printk(KERN_ERR "timer: cannot allocate\n");
767 return -ENOMEM;
769 timer->tmr_class = tid->dev_class;
770 timer->card = card;
771 timer->tmr_device = tid->device;
772 timer->tmr_subdevice = tid->subdevice;
773 if (id)
774 strlcpy(timer->id, id, sizeof(timer->id));
775 INIT_LIST_HEAD(&timer->device_list);
776 INIT_LIST_HEAD(&timer->open_list_head);
777 INIT_LIST_HEAD(&timer->active_list_head);
778 INIT_LIST_HEAD(&timer->ack_list_head);
779 INIT_LIST_HEAD(&timer->sack_list_head);
780 spin_lock_init(&timer->lock);
781 tasklet_init(&timer->task_queue, snd_timer_tasklet,
782 (unsigned long)timer);
783 if (card != NULL) {
784 timer->module = card->module;
785 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
786 if (err < 0) {
787 snd_timer_free(timer);
788 return err;
791 *rtimer = timer;
792 return 0;
795 static int snd_timer_free(struct snd_timer *timer)
797 snd_assert(timer != NULL, return -ENXIO);
799 mutex_lock(&register_mutex);
800 if (! list_empty(&timer->open_list_head)) {
801 struct list_head *p, *n;
802 struct snd_timer_instance *ti;
803 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
804 list_for_each_safe(p, n, &timer->open_list_head) {
805 list_del_init(p);
806 ti = list_entry(p, struct snd_timer_instance, open_list);
807 ti->timer = NULL;
810 list_del(&timer->device_list);
811 mutex_unlock(&register_mutex);
813 if (timer->private_free)
814 timer->private_free(timer);
815 kfree(timer);
816 return 0;
819 static int snd_timer_dev_free(struct snd_device *device)
821 struct snd_timer *timer = device->device_data;
822 return snd_timer_free(timer);
825 static int snd_timer_dev_register(struct snd_device *dev)
827 struct snd_timer *timer = dev->device_data;
828 struct snd_timer *timer1;
830 snd_assert(timer != NULL && timer->hw.start != NULL &&
831 timer->hw.stop != NULL, return -ENXIO);
832 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
833 !timer->hw.resolution && timer->hw.c_resolution == NULL)
834 return -EINVAL;
836 mutex_lock(&register_mutex);
837 list_for_each_entry(timer1, &snd_timer_list, device_list) {
838 if (timer1->tmr_class > timer->tmr_class)
839 break;
840 if (timer1->tmr_class < timer->tmr_class)
841 continue;
842 if (timer1->card && timer->card) {
843 if (timer1->card->number > timer->card->number)
844 break;
845 if (timer1->card->number < timer->card->number)
846 continue;
848 if (timer1->tmr_device > timer->tmr_device)
849 break;
850 if (timer1->tmr_device < timer->tmr_device)
851 continue;
852 if (timer1->tmr_subdevice > timer->tmr_subdevice)
853 break;
854 if (timer1->tmr_subdevice < timer->tmr_subdevice)
855 continue;
856 /* conflicts.. */
857 mutex_unlock(&register_mutex);
858 return -EBUSY;
860 list_add_tail(&timer->device_list, &timer1->device_list);
861 mutex_unlock(&register_mutex);
862 return 0;
865 static int snd_timer_dev_disconnect(struct snd_device *device)
867 struct snd_timer *timer = device->device_data;
868 mutex_lock(&register_mutex);
869 list_del_init(&timer->device_list);
870 mutex_unlock(&register_mutex);
871 return 0;
874 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
876 unsigned long flags;
877 unsigned long resolution = 0;
878 struct snd_timer_instance *ti, *ts;
880 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
881 return;
882 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
883 event <= SNDRV_TIMER_EVENT_MRESUME, return);
884 spin_lock_irqsave(&timer->lock, flags);
885 if (event == SNDRV_TIMER_EVENT_MSTART ||
886 event == SNDRV_TIMER_EVENT_MCONTINUE ||
887 event == SNDRV_TIMER_EVENT_MRESUME) {
888 if (timer->hw.c_resolution)
889 resolution = timer->hw.c_resolution(timer);
890 else
891 resolution = timer->hw.resolution;
893 list_for_each_entry(ti, &timer->active_list_head, active_list) {
894 if (ti->ccallback)
895 ti->ccallback(ti, event, tstamp, resolution);
896 list_for_each_entry(ts, &ti->slave_active_head, active_list)
897 if (ts->ccallback)
898 ts->ccallback(ts, event, tstamp, resolution);
900 spin_unlock_irqrestore(&timer->lock, flags);
904 * exported functions for global timers
906 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
908 struct snd_timer_id tid;
910 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
911 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
912 tid.card = -1;
913 tid.device = device;
914 tid.subdevice = 0;
915 return snd_timer_new(NULL, id, &tid, rtimer);
918 int snd_timer_global_free(struct snd_timer *timer)
920 return snd_timer_free(timer);
923 int snd_timer_global_register(struct snd_timer *timer)
925 struct snd_device dev;
927 memset(&dev, 0, sizeof(dev));
928 dev.device_data = timer;
929 return snd_timer_dev_register(&dev);
933 * System timer
936 struct snd_timer_system_private {
937 struct timer_list tlist;
938 unsigned long last_expires;
939 unsigned long last_jiffies;
940 unsigned long correction;
943 static void snd_timer_s_function(unsigned long data)
945 struct snd_timer *timer = (struct snd_timer *)data;
946 struct snd_timer_system_private *priv = timer->private_data;
947 unsigned long jiff = jiffies;
948 if (time_after(jiff, priv->last_expires))
949 priv->correction += (long)jiff - (long)priv->last_expires;
950 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
953 static int snd_timer_s_start(struct snd_timer * timer)
955 struct snd_timer_system_private *priv;
956 unsigned long njiff;
958 priv = (struct snd_timer_system_private *) timer->private_data;
959 njiff = (priv->last_jiffies = jiffies);
960 if (priv->correction > timer->sticks - 1) {
961 priv->correction -= timer->sticks - 1;
962 njiff++;
963 } else {
964 njiff += timer->sticks - priv->correction;
965 priv->correction = 0;
967 priv->last_expires = priv->tlist.expires = njiff;
968 add_timer(&priv->tlist);
969 return 0;
972 static int snd_timer_s_stop(struct snd_timer * timer)
974 struct snd_timer_system_private *priv;
975 unsigned long jiff;
977 priv = (struct snd_timer_system_private *) timer->private_data;
978 del_timer(&priv->tlist);
979 jiff = jiffies;
980 if (time_before(jiff, priv->last_expires))
981 timer->sticks = priv->last_expires - jiff;
982 else
983 timer->sticks = 1;
984 priv->correction = 0;
985 return 0;
988 static struct snd_timer_hardware snd_timer_system =
990 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
991 .resolution = 1000000000L / HZ,
992 .ticks = 10000000L,
993 .start = snd_timer_s_start,
994 .stop = snd_timer_s_stop
997 static void snd_timer_free_system(struct snd_timer *timer)
999 kfree(timer->private_data);
1002 static int snd_timer_register_system(void)
1004 struct snd_timer *timer;
1005 struct snd_timer_system_private *priv;
1006 int err;
1008 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1009 if (err < 0)
1010 return err;
1011 strcpy(timer->name, "system timer");
1012 timer->hw = snd_timer_system;
1013 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1014 if (priv == NULL) {
1015 snd_timer_free(timer);
1016 return -ENOMEM;
1018 init_timer(&priv->tlist);
1019 priv->tlist.function = snd_timer_s_function;
1020 priv->tlist.data = (unsigned long) timer;
1021 timer->private_data = priv;
1022 timer->private_free = snd_timer_free_system;
1023 return snd_timer_global_register(timer);
1026 #ifdef CONFIG_PROC_FS
1028 * Info interface
1031 static void snd_timer_proc_read(struct snd_info_entry *entry,
1032 struct snd_info_buffer *buffer)
1034 struct snd_timer *timer;
1035 struct snd_timer_instance *ti;
1037 mutex_lock(&register_mutex);
1038 list_for_each_entry(timer, &snd_timer_list, device_list) {
1039 switch (timer->tmr_class) {
1040 case SNDRV_TIMER_CLASS_GLOBAL:
1041 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1042 break;
1043 case SNDRV_TIMER_CLASS_CARD:
1044 snd_iprintf(buffer, "C%i-%i: ",
1045 timer->card->number, timer->tmr_device);
1046 break;
1047 case SNDRV_TIMER_CLASS_PCM:
1048 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1049 timer->tmr_device, timer->tmr_subdevice);
1050 break;
1051 default:
1052 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1053 timer->card ? timer->card->number : -1,
1054 timer->tmr_device, timer->tmr_subdevice);
1056 snd_iprintf(buffer, "%s :", timer->name);
1057 if (timer->hw.resolution)
1058 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1059 timer->hw.resolution / 1000,
1060 timer->hw.resolution % 1000,
1061 timer->hw.ticks);
1062 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1063 snd_iprintf(buffer, " SLAVE");
1064 snd_iprintf(buffer, "\n");
1065 list_for_each_entry(ti, &timer->open_list_head, open_list)
1066 snd_iprintf(buffer, " Client %s : %s\n",
1067 ti->owner ? ti->owner : "unknown",
1068 ti->flags & (SNDRV_TIMER_IFLG_START |
1069 SNDRV_TIMER_IFLG_RUNNING)
1070 ? "running" : "stopped");
1072 mutex_unlock(&register_mutex);
1075 static struct snd_info_entry *snd_timer_proc_entry;
1077 static void __init snd_timer_proc_init(void)
1079 struct snd_info_entry *entry;
1081 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1082 if (entry != NULL) {
1083 entry->c.text.read = snd_timer_proc_read;
1084 if (snd_info_register(entry) < 0) {
1085 snd_info_free_entry(entry);
1086 entry = NULL;
1089 snd_timer_proc_entry = entry;
1092 static void __exit snd_timer_proc_done(void)
1094 snd_info_free_entry(snd_timer_proc_entry);
1096 #else /* !CONFIG_PROC_FS */
1097 #define snd_timer_proc_init()
1098 #define snd_timer_proc_done()
1099 #endif
1102 * USER SPACE interface
1105 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1106 unsigned long resolution,
1107 unsigned long ticks)
1109 struct snd_timer_user *tu = timeri->callback_data;
1110 struct snd_timer_read *r;
1111 int prev;
1113 spin_lock(&tu->qlock);
1114 if (tu->qused > 0) {
1115 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1116 r = &tu->queue[prev];
1117 if (r->resolution == resolution) {
1118 r->ticks += ticks;
1119 goto __wake;
1122 if (tu->qused >= tu->queue_size) {
1123 tu->overrun++;
1124 } else {
1125 r = &tu->queue[tu->qtail++];
1126 tu->qtail %= tu->queue_size;
1127 r->resolution = resolution;
1128 r->ticks = ticks;
1129 tu->qused++;
1131 __wake:
1132 spin_unlock(&tu->qlock);
1133 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1134 wake_up(&tu->qchange_sleep);
1137 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1138 struct snd_timer_tread *tread)
1140 if (tu->qused >= tu->queue_size) {
1141 tu->overrun++;
1142 } else {
1143 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1144 tu->qtail %= tu->queue_size;
1145 tu->qused++;
1149 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1150 int event,
1151 struct timespec *tstamp,
1152 unsigned long resolution)
1154 struct snd_timer_user *tu = timeri->callback_data;
1155 struct snd_timer_tread r1;
1157 if (event >= SNDRV_TIMER_EVENT_START &&
1158 event <= SNDRV_TIMER_EVENT_PAUSE)
1159 tu->tstamp = *tstamp;
1160 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1161 return;
1162 r1.event = event;
1163 r1.tstamp = *tstamp;
1164 r1.val = resolution;
1165 spin_lock(&tu->qlock);
1166 snd_timer_user_append_to_tqueue(tu, &r1);
1167 spin_unlock(&tu->qlock);
1168 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1169 wake_up(&tu->qchange_sleep);
1172 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1173 unsigned long resolution,
1174 unsigned long ticks)
1176 struct snd_timer_user *tu = timeri->callback_data;
1177 struct snd_timer_tread *r, r1;
1178 struct timespec tstamp;
1179 int prev, append = 0;
1181 memset(&tstamp, 0, sizeof(tstamp));
1182 spin_lock(&tu->qlock);
1183 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1184 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1185 spin_unlock(&tu->qlock);
1186 return;
1188 if (tu->last_resolution != resolution || ticks > 0) {
1189 if (timer_tstamp_monotonic)
1190 do_posix_clock_monotonic_gettime(&tstamp);
1191 else
1192 getnstimeofday(&tstamp);
1194 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1195 tu->last_resolution != resolution) {
1196 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1197 r1.tstamp = tstamp;
1198 r1.val = resolution;
1199 snd_timer_user_append_to_tqueue(tu, &r1);
1200 tu->last_resolution = resolution;
1201 append++;
1203 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1204 goto __wake;
1205 if (ticks == 0)
1206 goto __wake;
1207 if (tu->qused > 0) {
1208 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1209 r = &tu->tqueue[prev];
1210 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1211 r->tstamp = tstamp;
1212 r->val += ticks;
1213 append++;
1214 goto __wake;
1217 r1.event = SNDRV_TIMER_EVENT_TICK;
1218 r1.tstamp = tstamp;
1219 r1.val = ticks;
1220 snd_timer_user_append_to_tqueue(tu, &r1);
1221 append++;
1222 __wake:
1223 spin_unlock(&tu->qlock);
1224 if (append == 0)
1225 return;
1226 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1227 wake_up(&tu->qchange_sleep);
1230 static int snd_timer_user_open(struct inode *inode, struct file *file)
1232 struct snd_timer_user *tu;
1234 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1235 if (tu == NULL)
1236 return -ENOMEM;
1237 spin_lock_init(&tu->qlock);
1238 init_waitqueue_head(&tu->qchange_sleep);
1239 mutex_init(&tu->tread_sem);
1240 tu->ticks = 1;
1241 tu->queue_size = 128;
1242 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1243 GFP_KERNEL);
1244 if (tu->queue == NULL) {
1245 kfree(tu);
1246 return -ENOMEM;
1248 file->private_data = tu;
1249 return 0;
1252 static int snd_timer_user_release(struct inode *inode, struct file *file)
1254 struct snd_timer_user *tu;
1256 if (file->private_data) {
1257 tu = file->private_data;
1258 file->private_data = NULL;
1259 fasync_helper(-1, file, 0, &tu->fasync);
1260 if (tu->timeri)
1261 snd_timer_close(tu->timeri);
1262 kfree(tu->queue);
1263 kfree(tu->tqueue);
1264 kfree(tu);
1266 return 0;
1269 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1271 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1272 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1273 id->card = -1;
1274 id->device = -1;
1275 id->subdevice = -1;
1278 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1280 id->dev_class = timer->tmr_class;
1281 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1282 id->card = timer->card ? timer->card->number : -1;
1283 id->device = timer->tmr_device;
1284 id->subdevice = timer->tmr_subdevice;
1287 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1289 struct snd_timer_id id;
1290 struct snd_timer *timer;
1291 struct list_head *p;
1293 if (copy_from_user(&id, _tid, sizeof(id)))
1294 return -EFAULT;
1295 mutex_lock(&register_mutex);
1296 if (id.dev_class < 0) { /* first item */
1297 if (list_empty(&snd_timer_list))
1298 snd_timer_user_zero_id(&id);
1299 else {
1300 timer = list_entry(snd_timer_list.next,
1301 struct snd_timer, device_list);
1302 snd_timer_user_copy_id(&id, timer);
1304 } else {
1305 switch (id.dev_class) {
1306 case SNDRV_TIMER_CLASS_GLOBAL:
1307 id.device = id.device < 0 ? 0 : id.device + 1;
1308 list_for_each(p, &snd_timer_list) {
1309 timer = list_entry(p, struct snd_timer, device_list);
1310 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1311 snd_timer_user_copy_id(&id, timer);
1312 break;
1314 if (timer->tmr_device >= id.device) {
1315 snd_timer_user_copy_id(&id, timer);
1316 break;
1319 if (p == &snd_timer_list)
1320 snd_timer_user_zero_id(&id);
1321 break;
1322 case SNDRV_TIMER_CLASS_CARD:
1323 case SNDRV_TIMER_CLASS_PCM:
1324 if (id.card < 0) {
1325 id.card = 0;
1326 } else {
1327 if (id.card < 0) {
1328 id.card = 0;
1329 } else {
1330 if (id.device < 0) {
1331 id.device = 0;
1332 } else {
1333 if (id.subdevice < 0) {
1334 id.subdevice = 0;
1335 } else {
1336 id.subdevice++;
1341 list_for_each(p, &snd_timer_list) {
1342 timer = list_entry(p, struct snd_timer, device_list);
1343 if (timer->tmr_class > id.dev_class) {
1344 snd_timer_user_copy_id(&id, timer);
1345 break;
1347 if (timer->tmr_class < id.dev_class)
1348 continue;
1349 if (timer->card->number > id.card) {
1350 snd_timer_user_copy_id(&id, timer);
1351 break;
1353 if (timer->card->number < id.card)
1354 continue;
1355 if (timer->tmr_device > id.device) {
1356 snd_timer_user_copy_id(&id, timer);
1357 break;
1359 if (timer->tmr_device < id.device)
1360 continue;
1361 if (timer->tmr_subdevice > id.subdevice) {
1362 snd_timer_user_copy_id(&id, timer);
1363 break;
1365 if (timer->tmr_subdevice < id.subdevice)
1366 continue;
1367 snd_timer_user_copy_id(&id, timer);
1368 break;
1370 if (p == &snd_timer_list)
1371 snd_timer_user_zero_id(&id);
1372 break;
1373 default:
1374 snd_timer_user_zero_id(&id);
1377 mutex_unlock(&register_mutex);
1378 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1379 return -EFAULT;
1380 return 0;
1383 static int snd_timer_user_ginfo(struct file *file,
1384 struct snd_timer_ginfo __user *_ginfo)
1386 struct snd_timer_ginfo *ginfo;
1387 struct snd_timer_id tid;
1388 struct snd_timer *t;
1389 struct list_head *p;
1390 int err = 0;
1392 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1393 if (! ginfo)
1394 return -ENOMEM;
1395 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1396 kfree(ginfo);
1397 return -EFAULT;
1399 tid = ginfo->tid;
1400 memset(ginfo, 0, sizeof(*ginfo));
1401 ginfo->tid = tid;
1402 mutex_lock(&register_mutex);
1403 t = snd_timer_find(&tid);
1404 if (t != NULL) {
1405 ginfo->card = t->card ? t->card->number : -1;
1406 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1407 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1408 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1409 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1410 ginfo->resolution = t->hw.resolution;
1411 if (t->hw.resolution_min > 0) {
1412 ginfo->resolution_min = t->hw.resolution_min;
1413 ginfo->resolution_max = t->hw.resolution_max;
1415 list_for_each(p, &t->open_list_head) {
1416 ginfo->clients++;
1418 } else {
1419 err = -ENODEV;
1421 mutex_unlock(&register_mutex);
1422 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1423 err = -EFAULT;
1424 kfree(ginfo);
1425 return err;
1428 static int snd_timer_user_gparams(struct file *file,
1429 struct snd_timer_gparams __user *_gparams)
1431 struct snd_timer_gparams gparams;
1432 struct snd_timer *t;
1433 int err;
1435 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1436 return -EFAULT;
1437 mutex_lock(&register_mutex);
1438 t = snd_timer_find(&gparams.tid);
1439 if (!t) {
1440 err = -ENODEV;
1441 goto _error;
1443 if (!list_empty(&t->open_list_head)) {
1444 err = -EBUSY;
1445 goto _error;
1447 if (!t->hw.set_period) {
1448 err = -ENOSYS;
1449 goto _error;
1451 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1452 _error:
1453 mutex_unlock(&register_mutex);
1454 return err;
1457 static int snd_timer_user_gstatus(struct file *file,
1458 struct snd_timer_gstatus __user *_gstatus)
1460 struct snd_timer_gstatus gstatus;
1461 struct snd_timer_id tid;
1462 struct snd_timer *t;
1463 int err = 0;
1465 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1466 return -EFAULT;
1467 tid = gstatus.tid;
1468 memset(&gstatus, 0, sizeof(gstatus));
1469 gstatus.tid = tid;
1470 mutex_lock(&register_mutex);
1471 t = snd_timer_find(&tid);
1472 if (t != NULL) {
1473 if (t->hw.c_resolution)
1474 gstatus.resolution = t->hw.c_resolution(t);
1475 else
1476 gstatus.resolution = t->hw.resolution;
1477 if (t->hw.precise_resolution) {
1478 t->hw.precise_resolution(t, &gstatus.resolution_num,
1479 &gstatus.resolution_den);
1480 } else {
1481 gstatus.resolution_num = gstatus.resolution;
1482 gstatus.resolution_den = 1000000000uL;
1484 } else {
1485 err = -ENODEV;
1487 mutex_unlock(&register_mutex);
1488 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1489 err = -EFAULT;
1490 return err;
1493 static int snd_timer_user_tselect(struct file *file,
1494 struct snd_timer_select __user *_tselect)
1496 struct snd_timer_user *tu;
1497 struct snd_timer_select tselect;
1498 char str[32];
1499 int err = 0;
1501 tu = file->private_data;
1502 mutex_lock(&tu->tread_sem);
1503 if (tu->timeri) {
1504 snd_timer_close(tu->timeri);
1505 tu->timeri = NULL;
1507 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1508 err = -EFAULT;
1509 goto __err;
1511 sprintf(str, "application %i", current->pid);
1512 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1513 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1514 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1515 if (err < 0)
1516 goto __err;
1518 kfree(tu->queue);
1519 tu->queue = NULL;
1520 kfree(tu->tqueue);
1521 tu->tqueue = NULL;
1522 if (tu->tread) {
1523 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1524 GFP_KERNEL);
1525 if (tu->tqueue == NULL)
1526 err = -ENOMEM;
1527 } else {
1528 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1529 GFP_KERNEL);
1530 if (tu->queue == NULL)
1531 err = -ENOMEM;
1534 if (err < 0) {
1535 snd_timer_close(tu->timeri);
1536 tu->timeri = NULL;
1537 } else {
1538 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1539 tu->timeri->callback = tu->tread
1540 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1541 tu->timeri->ccallback = snd_timer_user_ccallback;
1542 tu->timeri->callback_data = (void *)tu;
1545 __err:
1546 mutex_unlock(&tu->tread_sem);
1547 return err;
1550 static int snd_timer_user_info(struct file *file,
1551 struct snd_timer_info __user *_info)
1553 struct snd_timer_user *tu;
1554 struct snd_timer_info *info;
1555 struct snd_timer *t;
1556 int err = 0;
1558 tu = file->private_data;
1559 if (!tu->timeri)
1560 return -EBADFD;
1561 t = tu->timeri->timer;
1562 if (!t)
1563 return -EBADFD;
1565 info = kzalloc(sizeof(*info), GFP_KERNEL);
1566 if (! info)
1567 return -ENOMEM;
1568 info->card = t->card ? t->card->number : -1;
1569 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1570 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1571 strlcpy(info->id, t->id, sizeof(info->id));
1572 strlcpy(info->name, t->name, sizeof(info->name));
1573 info->resolution = t->hw.resolution;
1574 if (copy_to_user(_info, info, sizeof(*_info)))
1575 err = -EFAULT;
1576 kfree(info);
1577 return err;
1580 static int snd_timer_user_params(struct file *file,
1581 struct snd_timer_params __user *_params)
1583 struct snd_timer_user *tu;
1584 struct snd_timer_params params;
1585 struct snd_timer *t;
1586 struct snd_timer_read *tr;
1587 struct snd_timer_tread *ttr;
1588 int err;
1590 tu = file->private_data;
1591 if (!tu->timeri)
1592 return -EBADFD;
1593 t = tu->timeri->timer;
1594 if (!t)
1595 return -EBADFD;
1596 if (copy_from_user(&params, _params, sizeof(params)))
1597 return -EFAULT;
1598 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1599 err = -EINVAL;
1600 goto _end;
1602 if (params.queue_size > 0 &&
1603 (params.queue_size < 32 || params.queue_size > 1024)) {
1604 err = -EINVAL;
1605 goto _end;
1607 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1608 (1<<SNDRV_TIMER_EVENT_TICK)|
1609 (1<<SNDRV_TIMER_EVENT_START)|
1610 (1<<SNDRV_TIMER_EVENT_STOP)|
1611 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1612 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1613 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1614 (1<<SNDRV_TIMER_EVENT_RESUME)|
1615 (1<<SNDRV_TIMER_EVENT_MSTART)|
1616 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1617 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1618 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1619 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1620 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1621 err = -EINVAL;
1622 goto _end;
1624 snd_timer_stop(tu->timeri);
1625 spin_lock_irq(&t->lock);
1626 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1627 SNDRV_TIMER_IFLG_EXCLUSIVE|
1628 SNDRV_TIMER_IFLG_EARLY_EVENT);
1629 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1630 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1631 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1632 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1633 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1634 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1635 spin_unlock_irq(&t->lock);
1636 if (params.queue_size > 0 &&
1637 (unsigned int)tu->queue_size != params.queue_size) {
1638 if (tu->tread) {
1639 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1640 GFP_KERNEL);
1641 if (ttr) {
1642 kfree(tu->tqueue);
1643 tu->queue_size = params.queue_size;
1644 tu->tqueue = ttr;
1646 } else {
1647 tr = kmalloc(params.queue_size * sizeof(*tr),
1648 GFP_KERNEL);
1649 if (tr) {
1650 kfree(tu->queue);
1651 tu->queue_size = params.queue_size;
1652 tu->queue = tr;
1656 tu->qhead = tu->qtail = tu->qused = 0;
1657 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1658 if (tu->tread) {
1659 struct snd_timer_tread tread;
1660 tread.event = SNDRV_TIMER_EVENT_EARLY;
1661 tread.tstamp.tv_sec = 0;
1662 tread.tstamp.tv_nsec = 0;
1663 tread.val = 0;
1664 snd_timer_user_append_to_tqueue(tu, &tread);
1665 } else {
1666 struct snd_timer_read *r = &tu->queue[0];
1667 r->resolution = 0;
1668 r->ticks = 0;
1669 tu->qused++;
1670 tu->qtail++;
1673 tu->filter = params.filter;
1674 tu->ticks = params.ticks;
1675 err = 0;
1676 _end:
1677 if (copy_to_user(_params, &params, sizeof(params)))
1678 return -EFAULT;
1679 return err;
1682 static int snd_timer_user_status(struct file *file,
1683 struct snd_timer_status __user *_status)
1685 struct snd_timer_user *tu;
1686 struct snd_timer_status status;
1688 tu = file->private_data;
1689 if (!tu->timeri)
1690 return -EBADFD;
1691 memset(&status, 0, sizeof(status));
1692 status.tstamp = tu->tstamp;
1693 status.resolution = snd_timer_resolution(tu->timeri);
1694 status.lost = tu->timeri->lost;
1695 status.overrun = tu->overrun;
1696 spin_lock_irq(&tu->qlock);
1697 status.queue = tu->qused;
1698 spin_unlock_irq(&tu->qlock);
1699 if (copy_to_user(_status, &status, sizeof(status)))
1700 return -EFAULT;
1701 return 0;
1704 static int snd_timer_user_start(struct file *file)
1706 int err;
1707 struct snd_timer_user *tu;
1709 tu = file->private_data;
1710 if (!tu->timeri)
1711 return -EBADFD;
1712 snd_timer_stop(tu->timeri);
1713 tu->timeri->lost = 0;
1714 tu->last_resolution = 0;
1715 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1718 static int snd_timer_user_stop(struct file *file)
1720 int err;
1721 struct snd_timer_user *tu;
1723 tu = file->private_data;
1724 if (!tu->timeri)
1725 return -EBADFD;
1726 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1729 static int snd_timer_user_continue(struct file *file)
1731 int err;
1732 struct snd_timer_user *tu;
1734 tu = file->private_data;
1735 if (!tu->timeri)
1736 return -EBADFD;
1737 tu->timeri->lost = 0;
1738 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1741 static int snd_timer_user_pause(struct file *file)
1743 int err;
1744 struct snd_timer_user *tu;
1746 tu = file->private_data;
1747 if (!tu->timeri)
1748 return -EBADFD;
1749 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1752 enum {
1753 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1754 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1755 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1756 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1759 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1760 unsigned long arg)
1762 struct snd_timer_user *tu;
1763 void __user *argp = (void __user *)arg;
1764 int __user *p = argp;
1766 tu = file->private_data;
1767 switch (cmd) {
1768 case SNDRV_TIMER_IOCTL_PVERSION:
1769 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1770 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1771 return snd_timer_user_next_device(argp);
1772 case SNDRV_TIMER_IOCTL_TREAD:
1774 int xarg;
1776 mutex_lock(&tu->tread_sem);
1777 if (tu->timeri) { /* too late */
1778 mutex_unlock(&tu->tread_sem);
1779 return -EBUSY;
1781 if (get_user(xarg, p)) {
1782 mutex_unlock(&tu->tread_sem);
1783 return -EFAULT;
1785 tu->tread = xarg ? 1 : 0;
1786 mutex_unlock(&tu->tread_sem);
1787 return 0;
1789 case SNDRV_TIMER_IOCTL_GINFO:
1790 return snd_timer_user_ginfo(file, argp);
1791 case SNDRV_TIMER_IOCTL_GPARAMS:
1792 return snd_timer_user_gparams(file, argp);
1793 case SNDRV_TIMER_IOCTL_GSTATUS:
1794 return snd_timer_user_gstatus(file, argp);
1795 case SNDRV_TIMER_IOCTL_SELECT:
1796 return snd_timer_user_tselect(file, argp);
1797 case SNDRV_TIMER_IOCTL_INFO:
1798 return snd_timer_user_info(file, argp);
1799 case SNDRV_TIMER_IOCTL_PARAMS:
1800 return snd_timer_user_params(file, argp);
1801 case SNDRV_TIMER_IOCTL_STATUS:
1802 return snd_timer_user_status(file, argp);
1803 case SNDRV_TIMER_IOCTL_START:
1804 case SNDRV_TIMER_IOCTL_START_OLD:
1805 return snd_timer_user_start(file);
1806 case SNDRV_TIMER_IOCTL_STOP:
1807 case SNDRV_TIMER_IOCTL_STOP_OLD:
1808 return snd_timer_user_stop(file);
1809 case SNDRV_TIMER_IOCTL_CONTINUE:
1810 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1811 return snd_timer_user_continue(file);
1812 case SNDRV_TIMER_IOCTL_PAUSE:
1813 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1814 return snd_timer_user_pause(file);
1816 return -ENOTTY;
1819 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1821 struct snd_timer_user *tu;
1822 int err;
1824 tu = file->private_data;
1825 err = fasync_helper(fd, file, on, &tu->fasync);
1826 if (err < 0)
1827 return err;
1828 return 0;
1831 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1832 size_t count, loff_t *offset)
1834 struct snd_timer_user *tu;
1835 long result = 0, unit;
1836 int err = 0;
1838 tu = file->private_data;
1839 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1840 spin_lock_irq(&tu->qlock);
1841 while ((long)count - result >= unit) {
1842 while (!tu->qused) {
1843 wait_queue_t wait;
1845 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1846 err = -EAGAIN;
1847 break;
1850 set_current_state(TASK_INTERRUPTIBLE);
1851 init_waitqueue_entry(&wait, current);
1852 add_wait_queue(&tu->qchange_sleep, &wait);
1854 spin_unlock_irq(&tu->qlock);
1855 schedule();
1856 spin_lock_irq(&tu->qlock);
1858 remove_wait_queue(&tu->qchange_sleep, &wait);
1860 if (signal_pending(current)) {
1861 err = -ERESTARTSYS;
1862 break;
1866 spin_unlock_irq(&tu->qlock);
1867 if (err < 0)
1868 goto _error;
1870 if (tu->tread) {
1871 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1872 sizeof(struct snd_timer_tread))) {
1873 err = -EFAULT;
1874 goto _error;
1876 } else {
1877 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1878 sizeof(struct snd_timer_read))) {
1879 err = -EFAULT;
1880 goto _error;
1884 tu->qhead %= tu->queue_size;
1886 result += unit;
1887 buffer += unit;
1889 spin_lock_irq(&tu->qlock);
1890 tu->qused--;
1892 spin_unlock_irq(&tu->qlock);
1893 _error:
1894 return result > 0 ? result : err;
1897 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1899 unsigned int mask;
1900 struct snd_timer_user *tu;
1902 tu = file->private_data;
1904 poll_wait(file, &tu->qchange_sleep, wait);
1906 mask = 0;
1907 if (tu->qused)
1908 mask |= POLLIN | POLLRDNORM;
1910 return mask;
1913 #ifdef CONFIG_COMPAT
1914 #include "timer_compat.c"
1915 #else
1916 #define snd_timer_user_ioctl_compat NULL
1917 #endif
1919 static const struct file_operations snd_timer_f_ops =
1921 .owner = THIS_MODULE,
1922 .read = snd_timer_user_read,
1923 .open = snd_timer_user_open,
1924 .release = snd_timer_user_release,
1925 .poll = snd_timer_user_poll,
1926 .unlocked_ioctl = snd_timer_user_ioctl,
1927 .compat_ioctl = snd_timer_user_ioctl_compat,
1928 .fasync = snd_timer_user_fasync,
1932 * ENTRY functions
1935 static int __init alsa_timer_init(void)
1937 int err;
1939 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1940 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1941 "system timer");
1942 #endif
1944 if ((err = snd_timer_register_system()) < 0)
1945 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1946 err);
1947 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1948 &snd_timer_f_ops, NULL, "timer")) < 0)
1949 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1950 err);
1951 snd_timer_proc_init();
1952 return 0;
1955 static void __exit alsa_timer_exit(void)
1957 struct list_head *p, *n;
1959 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1960 /* unregister the system timer */
1961 list_for_each_safe(p, n, &snd_timer_list) {
1962 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
1963 snd_timer_free(timer);
1965 snd_timer_proc_done();
1966 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1967 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1968 #endif
1971 module_init(alsa_timer_init)
1972 module_exit(alsa_timer_exit)
1974 EXPORT_SYMBOL(snd_timer_open);
1975 EXPORT_SYMBOL(snd_timer_close);
1976 EXPORT_SYMBOL(snd_timer_resolution);
1977 EXPORT_SYMBOL(snd_timer_start);
1978 EXPORT_SYMBOL(snd_timer_stop);
1979 EXPORT_SYMBOL(snd_timer_continue);
1980 EXPORT_SYMBOL(snd_timer_pause);
1981 EXPORT_SYMBOL(snd_timer_new);
1982 EXPORT_SYMBOL(snd_timer_notify);
1983 EXPORT_SYMBOL(snd_timer_global_new);
1984 EXPORT_SYMBOL(snd_timer_global_free);
1985 EXPORT_SYMBOL(snd_timer_global_register);
1986 EXPORT_SYMBOL(snd_timer_interrupt);