x86: fix section mismatch warnings when referencing notifiers
[linux-2.6/lfs.git] / sound / core / timer.c
blobaece465934b86aa5f4266870b7e3cc7c1e745b1b
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 if (! current->fs->root)
154 return;
155 switch (tid->dev_class) {
156 case SNDRV_TIMER_CLASS_GLOBAL:
157 if (tid->device < timer_limit)
158 request_module("snd-timer-%i", tid->device);
159 break;
160 case SNDRV_TIMER_CLASS_CARD:
161 case SNDRV_TIMER_CLASS_PCM:
162 if (tid->card < snd_ecards_limit)
163 request_module("snd-card-%i", tid->card);
164 break;
165 default:
166 break;
170 #endif
173 * look for a master instance matching with the slave id of the given slave.
174 * when found, relink the open_link of the slave.
176 * call this with register_mutex down.
178 static void snd_timer_check_slave(struct snd_timer_instance *slave)
180 struct snd_timer *timer;
181 struct snd_timer_instance *master;
183 /* FIXME: it's really dumb to look up all entries.. */
184 list_for_each_entry(timer, &snd_timer_list, device_list) {
185 list_for_each_entry(master, &timer->open_list_head, open_list) {
186 if (slave->slave_class == master->slave_class &&
187 slave->slave_id == master->slave_id) {
188 list_del(&slave->open_list);
189 list_add_tail(&slave->open_list,
190 &master->slave_list_head);
191 spin_lock_irq(&slave_active_lock);
192 slave->master = master;
193 slave->timer = master->timer;
194 spin_unlock_irq(&slave_active_lock);
195 return;
202 * look for slave instances matching with the slave id of the given master.
203 * when found, relink the open_link of slaves.
205 * call this with register_mutex down.
207 static void snd_timer_check_master(struct snd_timer_instance *master)
209 struct snd_timer_instance *slave, *tmp;
211 /* check all pending slaves */
212 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
213 if (slave->slave_class == master->slave_class &&
214 slave->slave_id == master->slave_id) {
215 list_move_tail(&slave->open_list, &master->slave_list_head);
216 spin_lock_irq(&slave_active_lock);
217 slave->master = master;
218 slave->timer = master->timer;
219 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
220 list_add_tail(&slave->active_list,
221 &master->slave_active_head);
222 spin_unlock_irq(&slave_active_lock);
228 * open a timer instance
229 * when opening a master, the slave id must be here given.
231 int snd_timer_open(struct snd_timer_instance **ti,
232 char *owner, struct snd_timer_id *tid,
233 unsigned int slave_id)
235 struct snd_timer *timer;
236 struct snd_timer_instance *timeri = NULL;
238 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
239 /* open a slave instance */
240 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
241 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
242 snd_printd("invalid slave class %i\n", tid->dev_sclass);
243 return -EINVAL;
245 mutex_lock(&register_mutex);
246 timeri = snd_timer_instance_new(owner, NULL);
247 if (!timeri) {
248 mutex_unlock(&register_mutex);
249 return -ENOMEM;
251 timeri->slave_class = tid->dev_sclass;
252 timeri->slave_id = tid->device;
253 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
254 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
255 snd_timer_check_slave(timeri);
256 mutex_unlock(&register_mutex);
257 *ti = timeri;
258 return 0;
261 /* open a master instance */
262 mutex_lock(&register_mutex);
263 timer = snd_timer_find(tid);
264 #ifdef CONFIG_KMOD
265 if (timer == NULL) {
266 mutex_unlock(&register_mutex);
267 snd_timer_request(tid);
268 mutex_lock(&register_mutex);
269 timer = snd_timer_find(tid);
271 #endif
272 if (!timer) {
273 mutex_unlock(&register_mutex);
274 return -ENODEV;
276 if (!list_empty(&timer->open_list_head)) {
277 timeri = list_entry(timer->open_list_head.next,
278 struct snd_timer_instance, open_list);
279 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
280 mutex_unlock(&register_mutex);
281 return -EBUSY;
284 timeri = snd_timer_instance_new(owner, timer);
285 if (!timeri) {
286 mutex_unlock(&register_mutex);
287 return -ENOMEM;
289 timeri->slave_class = tid->dev_sclass;
290 timeri->slave_id = slave_id;
291 if (list_empty(&timer->open_list_head) && timer->hw.open)
292 timer->hw.open(timer);
293 list_add_tail(&timeri->open_list, &timer->open_list_head);
294 snd_timer_check_master(timeri);
295 mutex_unlock(&register_mutex);
296 *ti = timeri;
297 return 0;
300 static int _snd_timer_stop(struct snd_timer_instance *timeri,
301 int keep_flag, int event);
304 * close a timer instance
306 int snd_timer_close(struct snd_timer_instance *timeri)
308 struct snd_timer *timer = NULL;
309 struct snd_timer_instance *slave, *tmp;
311 snd_assert(timeri != NULL, return -ENXIO);
313 /* force to stop the timer */
314 snd_timer_stop(timeri);
316 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
317 /* wait, until the active callback is finished */
318 spin_lock_irq(&slave_active_lock);
319 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
320 spin_unlock_irq(&slave_active_lock);
321 udelay(10);
322 spin_lock_irq(&slave_active_lock);
324 spin_unlock_irq(&slave_active_lock);
325 mutex_lock(&register_mutex);
326 list_del(&timeri->open_list);
327 mutex_unlock(&register_mutex);
328 } else {
329 timer = timeri->timer;
330 /* wait, until the active callback is finished */
331 spin_lock_irq(&timer->lock);
332 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
333 spin_unlock_irq(&timer->lock);
334 udelay(10);
335 spin_lock_irq(&timer->lock);
337 spin_unlock_irq(&timer->lock);
338 mutex_lock(&register_mutex);
339 list_del(&timeri->open_list);
340 if (timer && list_empty(&timer->open_list_head) &&
341 timer->hw.close)
342 timer->hw.close(timer);
343 /* remove slave links */
344 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
345 open_list) {
346 spin_lock_irq(&slave_active_lock);
347 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
348 list_move_tail(&slave->open_list, &snd_timer_slave_list);
349 slave->master = NULL;
350 slave->timer = NULL;
351 spin_unlock_irq(&slave_active_lock);
353 mutex_unlock(&register_mutex);
355 if (timeri->private_free)
356 timeri->private_free(timeri);
357 kfree(timeri->owner);
358 kfree(timeri);
359 if (timer)
360 module_put(timer->module);
361 return 0;
364 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
366 struct snd_timer * timer;
368 if (timeri == NULL)
369 return 0;
370 if ((timer = timeri->timer) != NULL) {
371 if (timer->hw.c_resolution)
372 return timer->hw.c_resolution(timer);
373 return timer->hw.resolution;
375 return 0;
378 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
380 struct snd_timer *timer;
381 unsigned long flags;
382 unsigned long resolution = 0;
383 struct snd_timer_instance *ts;
384 struct timespec tstamp;
386 if (timer_tstamp_monotonic)
387 do_posix_clock_monotonic_gettime(&tstamp);
388 else
389 getnstimeofday(&tstamp);
390 snd_assert(event >= SNDRV_TIMER_EVENT_START &&
391 event <= SNDRV_TIMER_EVENT_PAUSE, 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, SNDRV_TIMER_EVENT_START, &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 snd_assert(timeri != NULL, return -ENXIO);
481 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
482 if (!keep_flag) {
483 spin_lock_irqsave(&slave_active_lock, flags);
484 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
485 spin_unlock_irqrestore(&slave_active_lock, flags);
487 goto __end;
489 timer = timeri->timer;
490 if (!timer)
491 return -EINVAL;
492 spin_lock_irqsave(&timer->lock, flags);
493 list_del_init(&timeri->ack_list);
494 list_del_init(&timeri->active_list);
495 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
496 !(--timer->running)) {
497 timer->hw.stop(timer);
498 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
499 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
500 snd_timer_reschedule(timer, 0);
501 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
502 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
503 timer->hw.start(timer);
507 if (!keep_flag)
508 timeri->flags &=
509 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
510 spin_unlock_irqrestore(&timer->lock, flags);
511 __end:
512 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
513 snd_timer_notify1(timeri, event);
514 return 0;
518 * stop the timer instance.
520 * do not call this from the timer callback!
522 int snd_timer_stop(struct snd_timer_instance *timeri)
524 struct snd_timer *timer;
525 unsigned long flags;
526 int err;
528 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
529 if (err < 0)
530 return err;
531 timer = timeri->timer;
532 spin_lock_irqsave(&timer->lock, flags);
533 timeri->cticks = timeri->ticks;
534 timeri->pticks = 0;
535 spin_unlock_irqrestore(&timer->lock, flags);
536 return 0;
540 * start again.. the tick is kept.
542 int snd_timer_continue(struct snd_timer_instance *timeri)
544 struct snd_timer *timer;
545 int result = -EINVAL;
546 unsigned long flags;
548 if (timeri == NULL)
549 return result;
550 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
551 return snd_timer_start_slave(timeri);
552 timer = timeri->timer;
553 if (! timer)
554 return -EINVAL;
555 spin_lock_irqsave(&timer->lock, flags);
556 if (!timeri->cticks)
557 timeri->cticks = 1;
558 timeri->pticks = 0;
559 result = snd_timer_start1(timer, timeri, timer->sticks);
560 spin_unlock_irqrestore(&timer->lock, flags);
561 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
562 return result;
566 * pause.. remember the ticks left
568 int snd_timer_pause(struct snd_timer_instance * timeri)
570 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
574 * reschedule the timer
576 * start pending instances and check the scheduling ticks.
577 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
579 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
581 struct snd_timer_instance *ti;
582 unsigned long ticks = ~0UL;
584 list_for_each_entry(ti, &timer->active_list_head, active_list) {
585 if (ti->flags & SNDRV_TIMER_IFLG_START) {
586 ti->flags &= ~SNDRV_TIMER_IFLG_START;
587 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
588 timer->running++;
590 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
591 if (ticks > ti->cticks)
592 ticks = ti->cticks;
595 if (ticks == ~0UL) {
596 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
597 return;
599 if (ticks > timer->hw.ticks)
600 ticks = timer->hw.ticks;
601 if (ticks_left != ticks)
602 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
603 timer->sticks = ticks;
607 * timer tasklet
610 static void snd_timer_tasklet(unsigned long arg)
612 struct snd_timer *timer = (struct snd_timer *) arg;
613 struct snd_timer_instance *ti;
614 struct list_head *p;
615 unsigned long resolution, ticks;
616 unsigned long flags;
618 spin_lock_irqsave(&timer->lock, flags);
619 /* now process all callbacks */
620 while (!list_empty(&timer->sack_list_head)) {
621 p = timer->sack_list_head.next; /* get first item */
622 ti = list_entry(p, struct snd_timer_instance, ack_list);
624 /* remove from ack_list and make empty */
625 list_del_init(p);
627 ticks = ti->pticks;
628 ti->pticks = 0;
629 resolution = ti->resolution;
631 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
632 spin_unlock(&timer->lock);
633 if (ti->callback)
634 ti->callback(ti, resolution, ticks);
635 spin_lock(&timer->lock);
636 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
638 spin_unlock_irqrestore(&timer->lock, flags);
642 * timer interrupt
644 * ticks_left is usually equal to timer->sticks.
647 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
649 struct snd_timer_instance *ti, *ts, *tmp;
650 unsigned long resolution, ticks;
651 struct list_head *p, *ack_list_head;
652 unsigned long flags;
653 int use_tasklet = 0;
655 if (timer == NULL)
656 return;
658 spin_lock_irqsave(&timer->lock, flags);
660 /* remember the current resolution */
661 if (timer->hw.c_resolution)
662 resolution = timer->hw.c_resolution(timer);
663 else
664 resolution = timer->hw.resolution;
666 /* loop for all active instances
667 * Here we cannot use list_for_each_entry because the active_list of a
668 * processed instance is relinked to done_list_head before the callback
669 * is called.
671 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
672 active_list) {
673 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
674 continue;
675 ti->pticks += ticks_left;
676 ti->resolution = resolution;
677 if (ti->cticks < ticks_left)
678 ti->cticks = 0;
679 else
680 ti->cticks -= ticks_left;
681 if (ti->cticks) /* not expired */
682 continue;
683 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
684 ti->cticks = ti->ticks;
685 } else {
686 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
687 if (--timer->running)
688 list_del(&ti->active_list);
690 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
691 (ti->flags & SNDRV_TIMER_IFLG_FAST))
692 ack_list_head = &timer->ack_list_head;
693 else
694 ack_list_head = &timer->sack_list_head;
695 if (list_empty(&ti->ack_list))
696 list_add_tail(&ti->ack_list, ack_list_head);
697 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
698 ts->pticks = ti->pticks;
699 ts->resolution = resolution;
700 if (list_empty(&ts->ack_list))
701 list_add_tail(&ts->ack_list, ack_list_head);
704 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
705 snd_timer_reschedule(timer, timer->sticks);
706 if (timer->running) {
707 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
708 timer->hw.stop(timer);
709 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
711 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
712 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
713 /* restart timer */
714 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
715 timer->hw.start(timer);
717 } else {
718 timer->hw.stop(timer);
721 /* now process all fast callbacks */
722 while (!list_empty(&timer->ack_list_head)) {
723 p = timer->ack_list_head.next; /* get first item */
724 ti = list_entry(p, struct snd_timer_instance, ack_list);
726 /* remove from ack_list and make empty */
727 list_del_init(p);
729 ticks = ti->pticks;
730 ti->pticks = 0;
732 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
733 spin_unlock(&timer->lock);
734 if (ti->callback)
735 ti->callback(ti, resolution, ticks);
736 spin_lock(&timer->lock);
737 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
740 /* do we have any slow callbacks? */
741 use_tasklet = !list_empty(&timer->sack_list_head);
742 spin_unlock_irqrestore(&timer->lock, flags);
744 if (use_tasklet)
745 tasklet_hi_schedule(&timer->task_queue);
752 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
753 struct snd_timer **rtimer)
755 struct snd_timer *timer;
756 int err;
757 static struct snd_device_ops ops = {
758 .dev_free = snd_timer_dev_free,
759 .dev_register = snd_timer_dev_register,
760 .dev_disconnect = snd_timer_dev_disconnect,
763 snd_assert(tid != NULL, return -EINVAL);
764 snd_assert(rtimer != NULL, return -EINVAL);
765 *rtimer = NULL;
766 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
767 if (timer == NULL) {
768 snd_printk(KERN_ERR "timer: cannot allocate\n");
769 return -ENOMEM;
771 timer->tmr_class = tid->dev_class;
772 timer->card = card;
773 timer->tmr_device = tid->device;
774 timer->tmr_subdevice = tid->subdevice;
775 if (id)
776 strlcpy(timer->id, id, sizeof(timer->id));
777 INIT_LIST_HEAD(&timer->device_list);
778 INIT_LIST_HEAD(&timer->open_list_head);
779 INIT_LIST_HEAD(&timer->active_list_head);
780 INIT_LIST_HEAD(&timer->ack_list_head);
781 INIT_LIST_HEAD(&timer->sack_list_head);
782 spin_lock_init(&timer->lock);
783 tasklet_init(&timer->task_queue, snd_timer_tasklet,
784 (unsigned long)timer);
785 if (card != NULL) {
786 timer->module = card->module;
787 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
788 if (err < 0) {
789 snd_timer_free(timer);
790 return err;
793 *rtimer = timer;
794 return 0;
797 static int snd_timer_free(struct snd_timer *timer)
799 snd_assert(timer != NULL, return -ENXIO);
801 mutex_lock(&register_mutex);
802 if (! list_empty(&timer->open_list_head)) {
803 struct list_head *p, *n;
804 struct snd_timer_instance *ti;
805 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
806 list_for_each_safe(p, n, &timer->open_list_head) {
807 list_del_init(p);
808 ti = list_entry(p, struct snd_timer_instance, open_list);
809 ti->timer = NULL;
812 list_del(&timer->device_list);
813 mutex_unlock(&register_mutex);
815 if (timer->private_free)
816 timer->private_free(timer);
817 kfree(timer);
818 return 0;
821 static int snd_timer_dev_free(struct snd_device *device)
823 struct snd_timer *timer = device->device_data;
824 return snd_timer_free(timer);
827 static int snd_timer_dev_register(struct snd_device *dev)
829 struct snd_timer *timer = dev->device_data;
830 struct snd_timer *timer1;
832 snd_assert(timer != NULL && timer->hw.start != NULL &&
833 timer->hw.stop != NULL, return -ENXIO);
834 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
835 !timer->hw.resolution && timer->hw.c_resolution == NULL)
836 return -EINVAL;
838 mutex_lock(&register_mutex);
839 list_for_each_entry(timer1, &snd_timer_list, device_list) {
840 if (timer1->tmr_class > timer->tmr_class)
841 break;
842 if (timer1->tmr_class < timer->tmr_class)
843 continue;
844 if (timer1->card && timer->card) {
845 if (timer1->card->number > timer->card->number)
846 break;
847 if (timer1->card->number < timer->card->number)
848 continue;
850 if (timer1->tmr_device > timer->tmr_device)
851 break;
852 if (timer1->tmr_device < timer->tmr_device)
853 continue;
854 if (timer1->tmr_subdevice > timer->tmr_subdevice)
855 break;
856 if (timer1->tmr_subdevice < timer->tmr_subdevice)
857 continue;
858 /* conflicts.. */
859 mutex_unlock(&register_mutex);
860 return -EBUSY;
862 list_add_tail(&timer->device_list, &timer1->device_list);
863 mutex_unlock(&register_mutex);
864 return 0;
867 static int snd_timer_dev_disconnect(struct snd_device *device)
869 struct snd_timer *timer = device->device_data;
870 mutex_lock(&register_mutex);
871 list_del_init(&timer->device_list);
872 mutex_unlock(&register_mutex);
873 return 0;
876 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
878 unsigned long flags;
879 unsigned long resolution = 0;
880 struct snd_timer_instance *ti, *ts;
882 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
883 return;
884 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
885 event <= SNDRV_TIMER_EVENT_MRESUME, return);
886 spin_lock_irqsave(&timer->lock, flags);
887 if (event == SNDRV_TIMER_EVENT_MSTART ||
888 event == SNDRV_TIMER_EVENT_MCONTINUE ||
889 event == SNDRV_TIMER_EVENT_MRESUME) {
890 if (timer->hw.c_resolution)
891 resolution = timer->hw.c_resolution(timer);
892 else
893 resolution = timer->hw.resolution;
895 list_for_each_entry(ti, &timer->active_list_head, active_list) {
896 if (ti->ccallback)
897 ti->ccallback(ti, event, tstamp, resolution);
898 list_for_each_entry(ts, &ti->slave_active_head, active_list)
899 if (ts->ccallback)
900 ts->ccallback(ts, event, tstamp, resolution);
902 spin_unlock_irqrestore(&timer->lock, flags);
906 * exported functions for global timers
908 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
910 struct snd_timer_id tid;
912 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
913 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
914 tid.card = -1;
915 tid.device = device;
916 tid.subdevice = 0;
917 return snd_timer_new(NULL, id, &tid, rtimer);
920 int snd_timer_global_free(struct snd_timer *timer)
922 return snd_timer_free(timer);
925 int snd_timer_global_register(struct snd_timer *timer)
927 struct snd_device dev;
929 memset(&dev, 0, sizeof(dev));
930 dev.device_data = timer;
931 return snd_timer_dev_register(&dev);
935 * System timer
938 struct snd_timer_system_private {
939 struct timer_list tlist;
940 unsigned long last_expires;
941 unsigned long last_jiffies;
942 unsigned long correction;
945 static void snd_timer_s_function(unsigned long data)
947 struct snd_timer *timer = (struct snd_timer *)data;
948 struct snd_timer_system_private *priv = timer->private_data;
949 unsigned long jiff = jiffies;
950 if (time_after(jiff, priv->last_expires))
951 priv->correction += (long)jiff - (long)priv->last_expires;
952 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
955 static int snd_timer_s_start(struct snd_timer * timer)
957 struct snd_timer_system_private *priv;
958 unsigned long njiff;
960 priv = (struct snd_timer_system_private *) timer->private_data;
961 njiff = (priv->last_jiffies = jiffies);
962 if (priv->correction > timer->sticks - 1) {
963 priv->correction -= timer->sticks - 1;
964 njiff++;
965 } else {
966 njiff += timer->sticks - priv->correction;
967 priv->correction = 0;
969 priv->last_expires = priv->tlist.expires = njiff;
970 add_timer(&priv->tlist);
971 return 0;
974 static int snd_timer_s_stop(struct snd_timer * timer)
976 struct snd_timer_system_private *priv;
977 unsigned long jiff;
979 priv = (struct snd_timer_system_private *) timer->private_data;
980 del_timer(&priv->tlist);
981 jiff = jiffies;
982 if (time_before(jiff, priv->last_expires))
983 timer->sticks = priv->last_expires - jiff;
984 else
985 timer->sticks = 1;
986 priv->correction = 0;
987 return 0;
990 static struct snd_timer_hardware snd_timer_system =
992 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
993 .resolution = 1000000000L / HZ,
994 .ticks = 10000000L,
995 .start = snd_timer_s_start,
996 .stop = snd_timer_s_stop
999 static void snd_timer_free_system(struct snd_timer *timer)
1001 kfree(timer->private_data);
1004 static int snd_timer_register_system(void)
1006 struct snd_timer *timer;
1007 struct snd_timer_system_private *priv;
1008 int err;
1010 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1011 if (err < 0)
1012 return err;
1013 strcpy(timer->name, "system timer");
1014 timer->hw = snd_timer_system;
1015 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1016 if (priv == NULL) {
1017 snd_timer_free(timer);
1018 return -ENOMEM;
1020 init_timer(&priv->tlist);
1021 priv->tlist.function = snd_timer_s_function;
1022 priv->tlist.data = (unsigned long) timer;
1023 timer->private_data = priv;
1024 timer->private_free = snd_timer_free_system;
1025 return snd_timer_global_register(timer);
1028 #ifdef CONFIG_PROC_FS
1030 * Info interface
1033 static void snd_timer_proc_read(struct snd_info_entry *entry,
1034 struct snd_info_buffer *buffer)
1036 struct snd_timer *timer;
1037 struct snd_timer_instance *ti;
1039 mutex_lock(&register_mutex);
1040 list_for_each_entry(timer, &snd_timer_list, device_list) {
1041 switch (timer->tmr_class) {
1042 case SNDRV_TIMER_CLASS_GLOBAL:
1043 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1044 break;
1045 case SNDRV_TIMER_CLASS_CARD:
1046 snd_iprintf(buffer, "C%i-%i: ",
1047 timer->card->number, timer->tmr_device);
1048 break;
1049 case SNDRV_TIMER_CLASS_PCM:
1050 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1051 timer->tmr_device, timer->tmr_subdevice);
1052 break;
1053 default:
1054 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1055 timer->card ? timer->card->number : -1,
1056 timer->tmr_device, timer->tmr_subdevice);
1058 snd_iprintf(buffer, "%s :", timer->name);
1059 if (timer->hw.resolution)
1060 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1061 timer->hw.resolution / 1000,
1062 timer->hw.resolution % 1000,
1063 timer->hw.ticks);
1064 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1065 snd_iprintf(buffer, " SLAVE");
1066 snd_iprintf(buffer, "\n");
1067 list_for_each_entry(ti, &timer->open_list_head, open_list)
1068 snd_iprintf(buffer, " Client %s : %s\n",
1069 ti->owner ? ti->owner : "unknown",
1070 ti->flags & (SNDRV_TIMER_IFLG_START |
1071 SNDRV_TIMER_IFLG_RUNNING)
1072 ? "running" : "stopped");
1074 mutex_unlock(&register_mutex);
1077 static struct snd_info_entry *snd_timer_proc_entry;
1079 static void __init snd_timer_proc_init(void)
1081 struct snd_info_entry *entry;
1083 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1084 if (entry != NULL) {
1085 entry->c.text.read = snd_timer_proc_read;
1086 if (snd_info_register(entry) < 0) {
1087 snd_info_free_entry(entry);
1088 entry = NULL;
1091 snd_timer_proc_entry = entry;
1094 static void __exit snd_timer_proc_done(void)
1096 snd_info_free_entry(snd_timer_proc_entry);
1098 #else /* !CONFIG_PROC_FS */
1099 #define snd_timer_proc_init()
1100 #define snd_timer_proc_done()
1101 #endif
1104 * USER SPACE interface
1107 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1108 unsigned long resolution,
1109 unsigned long ticks)
1111 struct snd_timer_user *tu = timeri->callback_data;
1112 struct snd_timer_read *r;
1113 int prev;
1115 spin_lock(&tu->qlock);
1116 if (tu->qused > 0) {
1117 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1118 r = &tu->queue[prev];
1119 if (r->resolution == resolution) {
1120 r->ticks += ticks;
1121 goto __wake;
1124 if (tu->qused >= tu->queue_size) {
1125 tu->overrun++;
1126 } else {
1127 r = &tu->queue[tu->qtail++];
1128 tu->qtail %= tu->queue_size;
1129 r->resolution = resolution;
1130 r->ticks = ticks;
1131 tu->qused++;
1133 __wake:
1134 spin_unlock(&tu->qlock);
1135 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1136 wake_up(&tu->qchange_sleep);
1139 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1140 struct snd_timer_tread *tread)
1142 if (tu->qused >= tu->queue_size) {
1143 tu->overrun++;
1144 } else {
1145 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1146 tu->qtail %= tu->queue_size;
1147 tu->qused++;
1151 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1152 int event,
1153 struct timespec *tstamp,
1154 unsigned long resolution)
1156 struct snd_timer_user *tu = timeri->callback_data;
1157 struct snd_timer_tread r1;
1159 if (event >= SNDRV_TIMER_EVENT_START &&
1160 event <= SNDRV_TIMER_EVENT_PAUSE)
1161 tu->tstamp = *tstamp;
1162 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1163 return;
1164 r1.event = event;
1165 r1.tstamp = *tstamp;
1166 r1.val = resolution;
1167 spin_lock(&tu->qlock);
1168 snd_timer_user_append_to_tqueue(tu, &r1);
1169 spin_unlock(&tu->qlock);
1170 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1171 wake_up(&tu->qchange_sleep);
1174 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1175 unsigned long resolution,
1176 unsigned long ticks)
1178 struct snd_timer_user *tu = timeri->callback_data;
1179 struct snd_timer_tread *r, r1;
1180 struct timespec tstamp;
1181 int prev, append = 0;
1183 memset(&tstamp, 0, sizeof(tstamp));
1184 spin_lock(&tu->qlock);
1185 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1186 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1187 spin_unlock(&tu->qlock);
1188 return;
1190 if (tu->last_resolution != resolution || ticks > 0) {
1191 if (timer_tstamp_monotonic)
1192 do_posix_clock_monotonic_gettime(&tstamp);
1193 else
1194 getnstimeofday(&tstamp);
1196 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1197 tu->last_resolution != resolution) {
1198 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1199 r1.tstamp = tstamp;
1200 r1.val = resolution;
1201 snd_timer_user_append_to_tqueue(tu, &r1);
1202 tu->last_resolution = resolution;
1203 append++;
1205 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1206 goto __wake;
1207 if (ticks == 0)
1208 goto __wake;
1209 if (tu->qused > 0) {
1210 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1211 r = &tu->tqueue[prev];
1212 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1213 r->tstamp = tstamp;
1214 r->val += ticks;
1215 append++;
1216 goto __wake;
1219 r1.event = SNDRV_TIMER_EVENT_TICK;
1220 r1.tstamp = tstamp;
1221 r1.val = ticks;
1222 snd_timer_user_append_to_tqueue(tu, &r1);
1223 append++;
1224 __wake:
1225 spin_unlock(&tu->qlock);
1226 if (append == 0)
1227 return;
1228 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1229 wake_up(&tu->qchange_sleep);
1232 static int snd_timer_user_open(struct inode *inode, struct file *file)
1234 struct snd_timer_user *tu;
1236 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1237 if (tu == NULL)
1238 return -ENOMEM;
1239 spin_lock_init(&tu->qlock);
1240 init_waitqueue_head(&tu->qchange_sleep);
1241 mutex_init(&tu->tread_sem);
1242 tu->ticks = 1;
1243 tu->queue_size = 128;
1244 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1245 GFP_KERNEL);
1246 if (tu->queue == NULL) {
1247 kfree(tu);
1248 return -ENOMEM;
1250 file->private_data = tu;
1251 return 0;
1254 static int snd_timer_user_release(struct inode *inode, struct file *file)
1256 struct snd_timer_user *tu;
1258 if (file->private_data) {
1259 tu = file->private_data;
1260 file->private_data = NULL;
1261 fasync_helper(-1, file, 0, &tu->fasync);
1262 if (tu->timeri)
1263 snd_timer_close(tu->timeri);
1264 kfree(tu->queue);
1265 kfree(tu->tqueue);
1266 kfree(tu);
1268 return 0;
1271 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1273 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1274 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1275 id->card = -1;
1276 id->device = -1;
1277 id->subdevice = -1;
1280 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1282 id->dev_class = timer->tmr_class;
1283 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1284 id->card = timer->card ? timer->card->number : -1;
1285 id->device = timer->tmr_device;
1286 id->subdevice = timer->tmr_subdevice;
1289 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1291 struct snd_timer_id id;
1292 struct snd_timer *timer;
1293 struct list_head *p;
1295 if (copy_from_user(&id, _tid, sizeof(id)))
1296 return -EFAULT;
1297 mutex_lock(&register_mutex);
1298 if (id.dev_class < 0) { /* first item */
1299 if (list_empty(&snd_timer_list))
1300 snd_timer_user_zero_id(&id);
1301 else {
1302 timer = list_entry(snd_timer_list.next,
1303 struct snd_timer, device_list);
1304 snd_timer_user_copy_id(&id, timer);
1306 } else {
1307 switch (id.dev_class) {
1308 case SNDRV_TIMER_CLASS_GLOBAL:
1309 id.device = id.device < 0 ? 0 : id.device + 1;
1310 list_for_each(p, &snd_timer_list) {
1311 timer = list_entry(p, struct snd_timer, device_list);
1312 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1313 snd_timer_user_copy_id(&id, timer);
1314 break;
1316 if (timer->tmr_device >= id.device) {
1317 snd_timer_user_copy_id(&id, timer);
1318 break;
1321 if (p == &snd_timer_list)
1322 snd_timer_user_zero_id(&id);
1323 break;
1324 case SNDRV_TIMER_CLASS_CARD:
1325 case SNDRV_TIMER_CLASS_PCM:
1326 if (id.card < 0) {
1327 id.card = 0;
1328 } else {
1329 if (id.card < 0) {
1330 id.card = 0;
1331 } else {
1332 if (id.device < 0) {
1333 id.device = 0;
1334 } else {
1335 if (id.subdevice < 0) {
1336 id.subdevice = 0;
1337 } else {
1338 id.subdevice++;
1343 list_for_each(p, &snd_timer_list) {
1344 timer = list_entry(p, struct snd_timer, device_list);
1345 if (timer->tmr_class > id.dev_class) {
1346 snd_timer_user_copy_id(&id, timer);
1347 break;
1349 if (timer->tmr_class < id.dev_class)
1350 continue;
1351 if (timer->card->number > id.card) {
1352 snd_timer_user_copy_id(&id, timer);
1353 break;
1355 if (timer->card->number < id.card)
1356 continue;
1357 if (timer->tmr_device > id.device) {
1358 snd_timer_user_copy_id(&id, timer);
1359 break;
1361 if (timer->tmr_device < id.device)
1362 continue;
1363 if (timer->tmr_subdevice > id.subdevice) {
1364 snd_timer_user_copy_id(&id, timer);
1365 break;
1367 if (timer->tmr_subdevice < id.subdevice)
1368 continue;
1369 snd_timer_user_copy_id(&id, timer);
1370 break;
1372 if (p == &snd_timer_list)
1373 snd_timer_user_zero_id(&id);
1374 break;
1375 default:
1376 snd_timer_user_zero_id(&id);
1379 mutex_unlock(&register_mutex);
1380 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1381 return -EFAULT;
1382 return 0;
1385 static int snd_timer_user_ginfo(struct file *file,
1386 struct snd_timer_ginfo __user *_ginfo)
1388 struct snd_timer_ginfo *ginfo;
1389 struct snd_timer_id tid;
1390 struct snd_timer *t;
1391 struct list_head *p;
1392 int err = 0;
1394 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1395 if (! ginfo)
1396 return -ENOMEM;
1397 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1398 kfree(ginfo);
1399 return -EFAULT;
1401 tid = ginfo->tid;
1402 memset(ginfo, 0, sizeof(*ginfo));
1403 ginfo->tid = tid;
1404 mutex_lock(&register_mutex);
1405 t = snd_timer_find(&tid);
1406 if (t != NULL) {
1407 ginfo->card = t->card ? t->card->number : -1;
1408 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1409 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1410 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1411 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1412 ginfo->resolution = t->hw.resolution;
1413 if (t->hw.resolution_min > 0) {
1414 ginfo->resolution_min = t->hw.resolution_min;
1415 ginfo->resolution_max = t->hw.resolution_max;
1417 list_for_each(p, &t->open_list_head) {
1418 ginfo->clients++;
1420 } else {
1421 err = -ENODEV;
1423 mutex_unlock(&register_mutex);
1424 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1425 err = -EFAULT;
1426 kfree(ginfo);
1427 return err;
1430 static int snd_timer_user_gparams(struct file *file,
1431 struct snd_timer_gparams __user *_gparams)
1433 struct snd_timer_gparams gparams;
1434 struct snd_timer *t;
1435 int err;
1437 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1438 return -EFAULT;
1439 mutex_lock(&register_mutex);
1440 t = snd_timer_find(&gparams.tid);
1441 if (!t) {
1442 err = -ENODEV;
1443 goto _error;
1445 if (!list_empty(&t->open_list_head)) {
1446 err = -EBUSY;
1447 goto _error;
1449 if (!t->hw.set_period) {
1450 err = -ENOSYS;
1451 goto _error;
1453 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1454 _error:
1455 mutex_unlock(&register_mutex);
1456 return err;
1459 static int snd_timer_user_gstatus(struct file *file,
1460 struct snd_timer_gstatus __user *_gstatus)
1462 struct snd_timer_gstatus gstatus;
1463 struct snd_timer_id tid;
1464 struct snd_timer *t;
1465 int err = 0;
1467 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1468 return -EFAULT;
1469 tid = gstatus.tid;
1470 memset(&gstatus, 0, sizeof(gstatus));
1471 gstatus.tid = tid;
1472 mutex_lock(&register_mutex);
1473 t = snd_timer_find(&tid);
1474 if (t != NULL) {
1475 if (t->hw.c_resolution)
1476 gstatus.resolution = t->hw.c_resolution(t);
1477 else
1478 gstatus.resolution = t->hw.resolution;
1479 if (t->hw.precise_resolution) {
1480 t->hw.precise_resolution(t, &gstatus.resolution_num,
1481 &gstatus.resolution_den);
1482 } else {
1483 gstatus.resolution_num = gstatus.resolution;
1484 gstatus.resolution_den = 1000000000uL;
1486 } else {
1487 err = -ENODEV;
1489 mutex_unlock(&register_mutex);
1490 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1491 err = -EFAULT;
1492 return err;
1495 static int snd_timer_user_tselect(struct file *file,
1496 struct snd_timer_select __user *_tselect)
1498 struct snd_timer_user *tu;
1499 struct snd_timer_select tselect;
1500 char str[32];
1501 int err = 0;
1503 tu = file->private_data;
1504 mutex_lock(&tu->tread_sem);
1505 if (tu->timeri) {
1506 snd_timer_close(tu->timeri);
1507 tu->timeri = NULL;
1509 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1510 err = -EFAULT;
1511 goto __err;
1513 sprintf(str, "application %i", current->pid);
1514 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1515 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1516 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1517 if (err < 0)
1518 goto __err;
1520 kfree(tu->queue);
1521 tu->queue = NULL;
1522 kfree(tu->tqueue);
1523 tu->tqueue = NULL;
1524 if (tu->tread) {
1525 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1526 GFP_KERNEL);
1527 if (tu->tqueue == NULL)
1528 err = -ENOMEM;
1529 } else {
1530 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1531 GFP_KERNEL);
1532 if (tu->queue == NULL)
1533 err = -ENOMEM;
1536 if (err < 0) {
1537 snd_timer_close(tu->timeri);
1538 tu->timeri = NULL;
1539 } else {
1540 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1541 tu->timeri->callback = tu->tread
1542 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1543 tu->timeri->ccallback = snd_timer_user_ccallback;
1544 tu->timeri->callback_data = (void *)tu;
1547 __err:
1548 mutex_unlock(&tu->tread_sem);
1549 return err;
1552 static int snd_timer_user_info(struct file *file,
1553 struct snd_timer_info __user *_info)
1555 struct snd_timer_user *tu;
1556 struct snd_timer_info *info;
1557 struct snd_timer *t;
1558 int err = 0;
1560 tu = file->private_data;
1561 if (!tu->timeri)
1562 return -EBADFD;
1563 t = tu->timeri->timer;
1564 if (!t)
1565 return -EBADFD;
1567 info = kzalloc(sizeof(*info), GFP_KERNEL);
1568 if (! info)
1569 return -ENOMEM;
1570 info->card = t->card ? t->card->number : -1;
1571 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1572 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1573 strlcpy(info->id, t->id, sizeof(info->id));
1574 strlcpy(info->name, t->name, sizeof(info->name));
1575 info->resolution = t->hw.resolution;
1576 if (copy_to_user(_info, info, sizeof(*_info)))
1577 err = -EFAULT;
1578 kfree(info);
1579 return err;
1582 static int snd_timer_user_params(struct file *file,
1583 struct snd_timer_params __user *_params)
1585 struct snd_timer_user *tu;
1586 struct snd_timer_params params;
1587 struct snd_timer *t;
1588 struct snd_timer_read *tr;
1589 struct snd_timer_tread *ttr;
1590 int err;
1592 tu = file->private_data;
1593 if (!tu->timeri)
1594 return -EBADFD;
1595 t = tu->timeri->timer;
1596 if (!t)
1597 return -EBADFD;
1598 if (copy_from_user(&params, _params, sizeof(params)))
1599 return -EFAULT;
1600 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1601 err = -EINVAL;
1602 goto _end;
1604 if (params.queue_size > 0 &&
1605 (params.queue_size < 32 || params.queue_size > 1024)) {
1606 err = -EINVAL;
1607 goto _end;
1609 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1610 (1<<SNDRV_TIMER_EVENT_TICK)|
1611 (1<<SNDRV_TIMER_EVENT_START)|
1612 (1<<SNDRV_TIMER_EVENT_STOP)|
1613 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1614 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1615 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1616 (1<<SNDRV_TIMER_EVENT_RESUME)|
1617 (1<<SNDRV_TIMER_EVENT_MSTART)|
1618 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1619 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1620 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1621 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1622 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1623 err = -EINVAL;
1624 goto _end;
1626 snd_timer_stop(tu->timeri);
1627 spin_lock_irq(&t->lock);
1628 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1629 SNDRV_TIMER_IFLG_EXCLUSIVE|
1630 SNDRV_TIMER_IFLG_EARLY_EVENT);
1631 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1632 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1633 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1634 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1635 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1636 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1637 spin_unlock_irq(&t->lock);
1638 if (params.queue_size > 0 &&
1639 (unsigned int)tu->queue_size != params.queue_size) {
1640 if (tu->tread) {
1641 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1642 GFP_KERNEL);
1643 if (ttr) {
1644 kfree(tu->tqueue);
1645 tu->queue_size = params.queue_size;
1646 tu->tqueue = ttr;
1648 } else {
1649 tr = kmalloc(params.queue_size * sizeof(*tr),
1650 GFP_KERNEL);
1651 if (tr) {
1652 kfree(tu->queue);
1653 tu->queue_size = params.queue_size;
1654 tu->queue = tr;
1658 tu->qhead = tu->qtail = tu->qused = 0;
1659 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1660 if (tu->tread) {
1661 struct snd_timer_tread tread;
1662 tread.event = SNDRV_TIMER_EVENT_EARLY;
1663 tread.tstamp.tv_sec = 0;
1664 tread.tstamp.tv_nsec = 0;
1665 tread.val = 0;
1666 snd_timer_user_append_to_tqueue(tu, &tread);
1667 } else {
1668 struct snd_timer_read *r = &tu->queue[0];
1669 r->resolution = 0;
1670 r->ticks = 0;
1671 tu->qused++;
1672 tu->qtail++;
1675 tu->filter = params.filter;
1676 tu->ticks = params.ticks;
1677 err = 0;
1678 _end:
1679 if (copy_to_user(_params, &params, sizeof(params)))
1680 return -EFAULT;
1681 return err;
1684 static int snd_timer_user_status(struct file *file,
1685 struct snd_timer_status __user *_status)
1687 struct snd_timer_user *tu;
1688 struct snd_timer_status status;
1690 tu = file->private_data;
1691 if (!tu->timeri)
1692 return -EBADFD;
1693 memset(&status, 0, sizeof(status));
1694 status.tstamp = tu->tstamp;
1695 status.resolution = snd_timer_resolution(tu->timeri);
1696 status.lost = tu->timeri->lost;
1697 status.overrun = tu->overrun;
1698 spin_lock_irq(&tu->qlock);
1699 status.queue = tu->qused;
1700 spin_unlock_irq(&tu->qlock);
1701 if (copy_to_user(_status, &status, sizeof(status)))
1702 return -EFAULT;
1703 return 0;
1706 static int snd_timer_user_start(struct file *file)
1708 int err;
1709 struct snd_timer_user *tu;
1711 tu = file->private_data;
1712 if (!tu->timeri)
1713 return -EBADFD;
1714 snd_timer_stop(tu->timeri);
1715 tu->timeri->lost = 0;
1716 tu->last_resolution = 0;
1717 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1720 static int snd_timer_user_stop(struct file *file)
1722 int err;
1723 struct snd_timer_user *tu;
1725 tu = file->private_data;
1726 if (!tu->timeri)
1727 return -EBADFD;
1728 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1731 static int snd_timer_user_continue(struct file *file)
1733 int err;
1734 struct snd_timer_user *tu;
1736 tu = file->private_data;
1737 if (!tu->timeri)
1738 return -EBADFD;
1739 tu->timeri->lost = 0;
1740 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1743 static int snd_timer_user_pause(struct file *file)
1745 int err;
1746 struct snd_timer_user *tu;
1748 tu = file->private_data;
1749 if (!tu->timeri)
1750 return -EBADFD;
1751 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1754 enum {
1755 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1756 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1757 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1758 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1761 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1762 unsigned long arg)
1764 struct snd_timer_user *tu;
1765 void __user *argp = (void __user *)arg;
1766 int __user *p = argp;
1768 tu = file->private_data;
1769 switch (cmd) {
1770 case SNDRV_TIMER_IOCTL_PVERSION:
1771 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1772 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1773 return snd_timer_user_next_device(argp);
1774 case SNDRV_TIMER_IOCTL_TREAD:
1776 int xarg;
1778 mutex_lock(&tu->tread_sem);
1779 if (tu->timeri) { /* too late */
1780 mutex_unlock(&tu->tread_sem);
1781 return -EBUSY;
1783 if (get_user(xarg, p)) {
1784 mutex_unlock(&tu->tread_sem);
1785 return -EFAULT;
1787 tu->tread = xarg ? 1 : 0;
1788 mutex_unlock(&tu->tread_sem);
1789 return 0;
1791 case SNDRV_TIMER_IOCTL_GINFO:
1792 return snd_timer_user_ginfo(file, argp);
1793 case SNDRV_TIMER_IOCTL_GPARAMS:
1794 return snd_timer_user_gparams(file, argp);
1795 case SNDRV_TIMER_IOCTL_GSTATUS:
1796 return snd_timer_user_gstatus(file, argp);
1797 case SNDRV_TIMER_IOCTL_SELECT:
1798 return snd_timer_user_tselect(file, argp);
1799 case SNDRV_TIMER_IOCTL_INFO:
1800 return snd_timer_user_info(file, argp);
1801 case SNDRV_TIMER_IOCTL_PARAMS:
1802 return snd_timer_user_params(file, argp);
1803 case SNDRV_TIMER_IOCTL_STATUS:
1804 return snd_timer_user_status(file, argp);
1805 case SNDRV_TIMER_IOCTL_START:
1806 case SNDRV_TIMER_IOCTL_START_OLD:
1807 return snd_timer_user_start(file);
1808 case SNDRV_TIMER_IOCTL_STOP:
1809 case SNDRV_TIMER_IOCTL_STOP_OLD:
1810 return snd_timer_user_stop(file);
1811 case SNDRV_TIMER_IOCTL_CONTINUE:
1812 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1813 return snd_timer_user_continue(file);
1814 case SNDRV_TIMER_IOCTL_PAUSE:
1815 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1816 return snd_timer_user_pause(file);
1818 return -ENOTTY;
1821 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1823 struct snd_timer_user *tu;
1824 int err;
1826 tu = file->private_data;
1827 err = fasync_helper(fd, file, on, &tu->fasync);
1828 if (err < 0)
1829 return err;
1830 return 0;
1833 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1834 size_t count, loff_t *offset)
1836 struct snd_timer_user *tu;
1837 long result = 0, unit;
1838 int err = 0;
1840 tu = file->private_data;
1841 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1842 spin_lock_irq(&tu->qlock);
1843 while ((long)count - result >= unit) {
1844 while (!tu->qused) {
1845 wait_queue_t wait;
1847 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1848 err = -EAGAIN;
1849 break;
1852 set_current_state(TASK_INTERRUPTIBLE);
1853 init_waitqueue_entry(&wait, current);
1854 add_wait_queue(&tu->qchange_sleep, &wait);
1856 spin_unlock_irq(&tu->qlock);
1857 schedule();
1858 spin_lock_irq(&tu->qlock);
1860 remove_wait_queue(&tu->qchange_sleep, &wait);
1862 if (signal_pending(current)) {
1863 err = -ERESTARTSYS;
1864 break;
1868 spin_unlock_irq(&tu->qlock);
1869 if (err < 0)
1870 goto _error;
1872 if (tu->tread) {
1873 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1874 sizeof(struct snd_timer_tread))) {
1875 err = -EFAULT;
1876 goto _error;
1878 } else {
1879 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1880 sizeof(struct snd_timer_read))) {
1881 err = -EFAULT;
1882 goto _error;
1886 tu->qhead %= tu->queue_size;
1888 result += unit;
1889 buffer += unit;
1891 spin_lock_irq(&tu->qlock);
1892 tu->qused--;
1894 spin_unlock_irq(&tu->qlock);
1895 _error:
1896 return result > 0 ? result : err;
1899 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1901 unsigned int mask;
1902 struct snd_timer_user *tu;
1904 tu = file->private_data;
1906 poll_wait(file, &tu->qchange_sleep, wait);
1908 mask = 0;
1909 if (tu->qused)
1910 mask |= POLLIN | POLLRDNORM;
1912 return mask;
1915 #ifdef CONFIG_COMPAT
1916 #include "timer_compat.c"
1917 #else
1918 #define snd_timer_user_ioctl_compat NULL
1919 #endif
1921 static const struct file_operations snd_timer_f_ops =
1923 .owner = THIS_MODULE,
1924 .read = snd_timer_user_read,
1925 .open = snd_timer_user_open,
1926 .release = snd_timer_user_release,
1927 .poll = snd_timer_user_poll,
1928 .unlocked_ioctl = snd_timer_user_ioctl,
1929 .compat_ioctl = snd_timer_user_ioctl_compat,
1930 .fasync = snd_timer_user_fasync,
1934 * ENTRY functions
1937 static int __init alsa_timer_init(void)
1939 int err;
1941 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1942 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1943 "system timer");
1944 #endif
1946 if ((err = snd_timer_register_system()) < 0)
1947 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1948 err);
1949 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1950 &snd_timer_f_ops, NULL, "timer")) < 0)
1951 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1952 err);
1953 snd_timer_proc_init();
1954 return 0;
1957 static void __exit alsa_timer_exit(void)
1959 struct list_head *p, *n;
1961 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1962 /* unregister the system timer */
1963 list_for_each_safe(p, n, &snd_timer_list) {
1964 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
1965 snd_timer_free(timer);
1967 snd_timer_proc_done();
1968 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1969 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1970 #endif
1973 module_init(alsa_timer_init)
1974 module_exit(alsa_timer_exit)
1976 EXPORT_SYMBOL(snd_timer_open);
1977 EXPORT_SYMBOL(snd_timer_close);
1978 EXPORT_SYMBOL(snd_timer_resolution);
1979 EXPORT_SYMBOL(snd_timer_start);
1980 EXPORT_SYMBOL(snd_timer_stop);
1981 EXPORT_SYMBOL(snd_timer_continue);
1982 EXPORT_SYMBOL(snd_timer_pause);
1983 EXPORT_SYMBOL(snd_timer_new);
1984 EXPORT_SYMBOL(snd_timer_notify);
1985 EXPORT_SYMBOL(snd_timer_global_new);
1986 EXPORT_SYMBOL(snd_timer_global_free);
1987 EXPORT_SYMBOL(snd_timer_global_register);
1988 EXPORT_SYMBOL(snd_timer_interrupt);