percpu: fix chunk range calculation
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / core / timer.c
blob4a138441f7992d6b05d0753781a6165f70782587
1 /*
2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/mutex.h>
27 #include <linux/moduleparam.h>
28 #include <linux/string.h>
29 #include <sound/core.h>
30 #include <sound/timer.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/minors.h>
34 #include <sound/initval.h>
35 #include <linux/kmod.h>
37 #if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
38 #define DEFAULT_TIMER_LIMIT 3
39 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
40 #define DEFAULT_TIMER_LIMIT 2
41 #else
42 #define DEFAULT_TIMER_LIMIT 1
43 #endif
45 static int timer_limit = DEFAULT_TIMER_LIMIT;
46 static int timer_tstamp_monotonic = 1;
47 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
48 MODULE_DESCRIPTION("ALSA timer interface");
49 MODULE_LICENSE("GPL");
50 module_param(timer_limit, int, 0444);
51 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
52 module_param(timer_tstamp_monotonic, int, 0444);
53 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
55 struct snd_timer_user {
56 struct snd_timer_instance *timeri;
57 int tread; /* enhanced read with timestamps and events */
58 unsigned long ticks;
59 unsigned long overrun;
60 int qhead;
61 int qtail;
62 int qused;
63 int queue_size;
64 struct snd_timer_read *queue;
65 struct snd_timer_tread *tqueue;
66 spinlock_t qlock;
67 unsigned long last_resolution;
68 unsigned int filter;
69 struct timespec tstamp; /* trigger tstamp */
70 wait_queue_head_t qchange_sleep;
71 struct fasync_struct *fasync;
72 struct mutex tread_sem;
75 /* list of timers */
76 static LIST_HEAD(snd_timer_list);
78 /* list of slave instances */
79 static LIST_HEAD(snd_timer_slave_list);
81 /* lock for slave active lists */
82 static DEFINE_SPINLOCK(slave_active_lock);
84 static DEFINE_MUTEX(register_mutex);
86 static int snd_timer_free(struct snd_timer *timer);
87 static int snd_timer_dev_free(struct snd_device *device);
88 static int snd_timer_dev_register(struct snd_device *device);
89 static int snd_timer_dev_disconnect(struct snd_device *device);
91 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
94 * create a timer instance with the given owner string.
95 * when timer is not NULL, increments the module counter
97 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
98 struct snd_timer *timer)
100 struct snd_timer_instance *timeri;
101 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
102 if (timeri == NULL)
103 return NULL;
104 timeri->owner = kstrdup(owner, GFP_KERNEL);
105 if (! timeri->owner) {
106 kfree(timeri);
107 return NULL;
109 INIT_LIST_HEAD(&timeri->open_list);
110 INIT_LIST_HEAD(&timeri->active_list);
111 INIT_LIST_HEAD(&timeri->ack_list);
112 INIT_LIST_HEAD(&timeri->slave_list_head);
113 INIT_LIST_HEAD(&timeri->slave_active_head);
115 timeri->timer = timer;
116 if (timer && !try_module_get(timer->module)) {
117 kfree(timeri->owner);
118 kfree(timeri);
119 return NULL;
122 return timeri;
126 * find a timer instance from the given timer id
128 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
130 struct snd_timer *timer = NULL;
132 list_for_each_entry(timer, &snd_timer_list, device_list) {
133 if (timer->tmr_class != tid->dev_class)
134 continue;
135 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
136 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
137 (timer->card == NULL ||
138 timer->card->number != tid->card))
139 continue;
140 if (timer->tmr_device != tid->device)
141 continue;
142 if (timer->tmr_subdevice != tid->subdevice)
143 continue;
144 return timer;
146 return NULL;
149 #ifdef CONFIG_MODULES
151 static void snd_timer_request(struct snd_timer_id *tid)
153 switch (tid->dev_class) {
154 case SNDRV_TIMER_CLASS_GLOBAL:
155 if (tid->device < timer_limit)
156 request_module("snd-timer-%i", tid->device);
157 break;
158 case SNDRV_TIMER_CLASS_CARD:
159 case SNDRV_TIMER_CLASS_PCM:
160 if (tid->card < snd_ecards_limit)
161 request_module("snd-card-%i", tid->card);
162 break;
163 default:
164 break;
168 #endif
171 * look for a master instance matching with the slave id of the given slave.
172 * when found, relink the open_link of the slave.
174 * call this with register_mutex down.
176 static void snd_timer_check_slave(struct snd_timer_instance *slave)
178 struct snd_timer *timer;
179 struct snd_timer_instance *master;
181 /* FIXME: it's really dumb to look up all entries.. */
182 list_for_each_entry(timer, &snd_timer_list, device_list) {
183 list_for_each_entry(master, &timer->open_list_head, open_list) {
184 if (slave->slave_class == master->slave_class &&
185 slave->slave_id == master->slave_id) {
186 list_del(&slave->open_list);
187 list_add_tail(&slave->open_list,
188 &master->slave_list_head);
189 spin_lock_irq(&slave_active_lock);
190 slave->master = master;
191 slave->timer = master->timer;
192 spin_unlock_irq(&slave_active_lock);
193 return;
200 * look for slave instances matching with the slave id of the given master.
201 * when found, relink the open_link of slaves.
203 * call this with register_mutex down.
205 static void snd_timer_check_master(struct snd_timer_instance *master)
207 struct snd_timer_instance *slave, *tmp;
209 /* check all pending slaves */
210 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
211 if (slave->slave_class == master->slave_class &&
212 slave->slave_id == master->slave_id) {
213 list_move_tail(&slave->open_list, &master->slave_list_head);
214 spin_lock_irq(&slave_active_lock);
215 slave->master = master;
216 slave->timer = master->timer;
217 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
218 list_add_tail(&slave->active_list,
219 &master->slave_active_head);
220 spin_unlock_irq(&slave_active_lock);
226 * open a timer instance
227 * when opening a master, the slave id must be here given.
229 int snd_timer_open(struct snd_timer_instance **ti,
230 char *owner, struct snd_timer_id *tid,
231 unsigned int slave_id)
233 struct snd_timer *timer;
234 struct snd_timer_instance *timeri = NULL;
236 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
237 /* open a slave instance */
238 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
239 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
240 snd_printd("invalid slave class %i\n", tid->dev_sclass);
241 return -EINVAL;
243 mutex_lock(&register_mutex);
244 timeri = snd_timer_instance_new(owner, NULL);
245 if (!timeri) {
246 mutex_unlock(&register_mutex);
247 return -ENOMEM;
249 timeri->slave_class = tid->dev_sclass;
250 timeri->slave_id = tid->device;
251 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
252 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
253 snd_timer_check_slave(timeri);
254 mutex_unlock(&register_mutex);
255 *ti = timeri;
256 return 0;
259 /* open a master instance */
260 mutex_lock(&register_mutex);
261 timer = snd_timer_find(tid);
262 #ifdef CONFIG_MODULES
263 if (!timer) {
264 mutex_unlock(&register_mutex);
265 snd_timer_request(tid);
266 mutex_lock(&register_mutex);
267 timer = snd_timer_find(tid);
269 #endif
270 if (!timer) {
271 mutex_unlock(&register_mutex);
272 return -ENODEV;
274 if (!list_empty(&timer->open_list_head)) {
275 timeri = list_entry(timer->open_list_head.next,
276 struct snd_timer_instance, open_list);
277 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
278 mutex_unlock(&register_mutex);
279 return -EBUSY;
282 timeri = snd_timer_instance_new(owner, timer);
283 if (!timeri) {
284 mutex_unlock(&register_mutex);
285 return -ENOMEM;
287 timeri->slave_class = tid->dev_sclass;
288 timeri->slave_id = slave_id;
289 if (list_empty(&timer->open_list_head) && timer->hw.open)
290 timer->hw.open(timer);
291 list_add_tail(&timeri->open_list, &timer->open_list_head);
292 snd_timer_check_master(timeri);
293 mutex_unlock(&register_mutex);
294 *ti = timeri;
295 return 0;
298 static int _snd_timer_stop(struct snd_timer_instance *timeri,
299 int keep_flag, int event);
302 * close a timer instance
304 int snd_timer_close(struct snd_timer_instance *timeri)
306 struct snd_timer *timer = NULL;
307 struct snd_timer_instance *slave, *tmp;
309 if (snd_BUG_ON(!timeri))
310 return -ENXIO;
312 /* force to stop the timer */
313 snd_timer_stop(timeri);
315 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
316 /* wait, until the active callback is finished */
317 spin_lock_irq(&slave_active_lock);
318 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
319 spin_unlock_irq(&slave_active_lock);
320 udelay(10);
321 spin_lock_irq(&slave_active_lock);
323 spin_unlock_irq(&slave_active_lock);
324 mutex_lock(&register_mutex);
325 list_del(&timeri->open_list);
326 mutex_unlock(&register_mutex);
327 } else {
328 timer = timeri->timer;
329 /* wait, until the active callback is finished */
330 spin_lock_irq(&timer->lock);
331 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
332 spin_unlock_irq(&timer->lock);
333 udelay(10);
334 spin_lock_irq(&timer->lock);
336 spin_unlock_irq(&timer->lock);
337 mutex_lock(&register_mutex);
338 list_del(&timeri->open_list);
339 if (timer && list_empty(&timer->open_list_head) &&
340 timer->hw.close)
341 timer->hw.close(timer);
342 /* remove slave links */
343 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
344 open_list) {
345 spin_lock_irq(&slave_active_lock);
346 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
347 list_move_tail(&slave->open_list, &snd_timer_slave_list);
348 slave->master = NULL;
349 slave->timer = NULL;
350 spin_unlock_irq(&slave_active_lock);
352 mutex_unlock(&register_mutex);
354 if (timeri->private_free)
355 timeri->private_free(timeri);
356 kfree(timeri->owner);
357 kfree(timeri);
358 if (timer)
359 module_put(timer->module);
360 return 0;
363 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
365 struct snd_timer * timer;
367 if (timeri == NULL)
368 return 0;
369 if ((timer = timeri->timer) != NULL) {
370 if (timer->hw.c_resolution)
371 return timer->hw.c_resolution(timer);
372 return timer->hw.resolution;
374 return 0;
377 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
379 struct snd_timer *timer;
380 unsigned long flags;
381 unsigned long resolution = 0;
382 struct snd_timer_instance *ts;
383 struct timespec tstamp;
385 if (timer_tstamp_monotonic)
386 do_posix_clock_monotonic_gettime(&tstamp);
387 else
388 getnstimeofday(&tstamp);
389 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
390 event > SNDRV_TIMER_EVENT_PAUSE))
391 return;
392 if (event == SNDRV_TIMER_EVENT_START ||
393 event == SNDRV_TIMER_EVENT_CONTINUE)
394 resolution = snd_timer_resolution(ti);
395 if (ti->ccallback)
396 ti->ccallback(ti, 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 if (snd_BUG_ON(!timeri))
480 return -ENXIO;
482 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
483 if (!keep_flag) {
484 spin_lock_irqsave(&slave_active_lock, flags);
485 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
486 spin_unlock_irqrestore(&slave_active_lock, flags);
488 goto __end;
490 timer = timeri->timer;
491 if (!timer)
492 return -EINVAL;
493 spin_lock_irqsave(&timer->lock, flags);
494 list_del_init(&timeri->ack_list);
495 list_del_init(&timeri->active_list);
496 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
497 !(--timer->running)) {
498 timer->hw.stop(timer);
499 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
500 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
501 snd_timer_reschedule(timer, 0);
502 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
503 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
504 timer->hw.start(timer);
508 if (!keep_flag)
509 timeri->flags &=
510 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
511 spin_unlock_irqrestore(&timer->lock, flags);
512 __end:
513 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
514 snd_timer_notify1(timeri, event);
515 return 0;
519 * stop the timer instance.
521 * do not call this from the timer callback!
523 int snd_timer_stop(struct snd_timer_instance *timeri)
525 struct snd_timer *timer;
526 unsigned long flags;
527 int err;
529 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
530 if (err < 0)
531 return err;
532 timer = timeri->timer;
533 if (!timer)
534 return -EINVAL;
535 spin_lock_irqsave(&timer->lock, flags);
536 timeri->cticks = timeri->ticks;
537 timeri->pticks = 0;
538 spin_unlock_irqrestore(&timer->lock, flags);
539 return 0;
543 * start again.. the tick is kept.
545 int snd_timer_continue(struct snd_timer_instance *timeri)
547 struct snd_timer *timer;
548 int result = -EINVAL;
549 unsigned long flags;
551 if (timeri == NULL)
552 return result;
553 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
554 return snd_timer_start_slave(timeri);
555 timer = timeri->timer;
556 if (! timer)
557 return -EINVAL;
558 spin_lock_irqsave(&timer->lock, flags);
559 if (!timeri->cticks)
560 timeri->cticks = 1;
561 timeri->pticks = 0;
562 result = snd_timer_start1(timer, timeri, timer->sticks);
563 spin_unlock_irqrestore(&timer->lock, flags);
564 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
565 return result;
569 * pause.. remember the ticks left
571 int snd_timer_pause(struct snd_timer_instance * timeri)
573 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
577 * reschedule the timer
579 * start pending instances and check the scheduling ticks.
580 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
582 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
584 struct snd_timer_instance *ti;
585 unsigned long ticks = ~0UL;
587 list_for_each_entry(ti, &timer->active_list_head, active_list) {
588 if (ti->flags & SNDRV_TIMER_IFLG_START) {
589 ti->flags &= ~SNDRV_TIMER_IFLG_START;
590 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
591 timer->running++;
593 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
594 if (ticks > ti->cticks)
595 ticks = ti->cticks;
598 if (ticks == ~0UL) {
599 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
600 return;
602 if (ticks > timer->hw.ticks)
603 ticks = timer->hw.ticks;
604 if (ticks_left != ticks)
605 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
606 timer->sticks = ticks;
610 * timer tasklet
613 static void snd_timer_tasklet(unsigned long arg)
615 struct snd_timer *timer = (struct snd_timer *) arg;
616 struct snd_timer_instance *ti;
617 struct list_head *p;
618 unsigned long resolution, ticks;
619 unsigned long flags;
621 spin_lock_irqsave(&timer->lock, flags);
622 /* now process all callbacks */
623 while (!list_empty(&timer->sack_list_head)) {
624 p = timer->sack_list_head.next; /* get first item */
625 ti = list_entry(p, struct snd_timer_instance, ack_list);
627 /* remove from ack_list and make empty */
628 list_del_init(p);
630 ticks = ti->pticks;
631 ti->pticks = 0;
632 resolution = ti->resolution;
634 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
635 spin_unlock(&timer->lock);
636 if (ti->callback)
637 ti->callback(ti, resolution, ticks);
638 spin_lock(&timer->lock);
639 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
641 spin_unlock_irqrestore(&timer->lock, flags);
645 * timer interrupt
647 * ticks_left is usually equal to timer->sticks.
650 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
652 struct snd_timer_instance *ti, *ts, *tmp;
653 unsigned long resolution, ticks;
654 struct list_head *p, *ack_list_head;
655 unsigned long flags;
656 int use_tasklet = 0;
658 if (timer == NULL)
659 return;
661 spin_lock_irqsave(&timer->lock, flags);
663 /* remember the current resolution */
664 if (timer->hw.c_resolution)
665 resolution = timer->hw.c_resolution(timer);
666 else
667 resolution = timer->hw.resolution;
669 /* loop for all active instances
670 * Here we cannot use list_for_each_entry because the active_list of a
671 * processed instance is relinked to done_list_head before the callback
672 * is called.
674 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
675 active_list) {
676 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
677 continue;
678 ti->pticks += ticks_left;
679 ti->resolution = resolution;
680 if (ti->cticks < ticks_left)
681 ti->cticks = 0;
682 else
683 ti->cticks -= ticks_left;
684 if (ti->cticks) /* not expired */
685 continue;
686 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
687 ti->cticks = ti->ticks;
688 } else {
689 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
690 if (--timer->running)
691 list_del(&ti->active_list);
693 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
694 (ti->flags & SNDRV_TIMER_IFLG_FAST))
695 ack_list_head = &timer->ack_list_head;
696 else
697 ack_list_head = &timer->sack_list_head;
698 if (list_empty(&ti->ack_list))
699 list_add_tail(&ti->ack_list, ack_list_head);
700 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
701 ts->pticks = ti->pticks;
702 ts->resolution = resolution;
703 if (list_empty(&ts->ack_list))
704 list_add_tail(&ts->ack_list, ack_list_head);
707 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
708 snd_timer_reschedule(timer, timer->sticks);
709 if (timer->running) {
710 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
711 timer->hw.stop(timer);
712 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
714 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
715 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
716 /* restart timer */
717 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
718 timer->hw.start(timer);
720 } else {
721 timer->hw.stop(timer);
724 /* now process all fast callbacks */
725 while (!list_empty(&timer->ack_list_head)) {
726 p = timer->ack_list_head.next; /* get first item */
727 ti = list_entry(p, struct snd_timer_instance, ack_list);
729 /* remove from ack_list and make empty */
730 list_del_init(p);
732 ticks = ti->pticks;
733 ti->pticks = 0;
735 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
736 spin_unlock(&timer->lock);
737 if (ti->callback)
738 ti->callback(ti, resolution, ticks);
739 spin_lock(&timer->lock);
740 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
743 /* do we have any slow callbacks? */
744 use_tasklet = !list_empty(&timer->sack_list_head);
745 spin_unlock_irqrestore(&timer->lock, flags);
747 if (use_tasklet)
748 tasklet_schedule(&timer->task_queue);
755 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
756 struct snd_timer **rtimer)
758 struct snd_timer *timer;
759 int err;
760 static struct snd_device_ops ops = {
761 .dev_free = snd_timer_dev_free,
762 .dev_register = snd_timer_dev_register,
763 .dev_disconnect = snd_timer_dev_disconnect,
766 if (snd_BUG_ON(!tid))
767 return -EINVAL;
768 if (rtimer)
769 *rtimer = NULL;
770 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
771 if (timer == NULL) {
772 snd_printk(KERN_ERR "timer: cannot allocate\n");
773 return -ENOMEM;
775 timer->tmr_class = tid->dev_class;
776 timer->card = card;
777 timer->tmr_device = tid->device;
778 timer->tmr_subdevice = tid->subdevice;
779 if (id)
780 strlcpy(timer->id, id, sizeof(timer->id));
781 INIT_LIST_HEAD(&timer->device_list);
782 INIT_LIST_HEAD(&timer->open_list_head);
783 INIT_LIST_HEAD(&timer->active_list_head);
784 INIT_LIST_HEAD(&timer->ack_list_head);
785 INIT_LIST_HEAD(&timer->sack_list_head);
786 spin_lock_init(&timer->lock);
787 tasklet_init(&timer->task_queue, snd_timer_tasklet,
788 (unsigned long)timer);
789 if (card != NULL) {
790 timer->module = card->module;
791 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
792 if (err < 0) {
793 snd_timer_free(timer);
794 return err;
797 if (rtimer)
798 *rtimer = timer;
799 return 0;
802 static int snd_timer_free(struct snd_timer *timer)
804 if (!timer)
805 return 0;
807 mutex_lock(&register_mutex);
808 if (! list_empty(&timer->open_list_head)) {
809 struct list_head *p, *n;
810 struct snd_timer_instance *ti;
811 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
812 list_for_each_safe(p, n, &timer->open_list_head) {
813 list_del_init(p);
814 ti = list_entry(p, struct snd_timer_instance, open_list);
815 ti->timer = NULL;
818 list_del(&timer->device_list);
819 mutex_unlock(&register_mutex);
821 if (timer->private_free)
822 timer->private_free(timer);
823 kfree(timer);
824 return 0;
827 static int snd_timer_dev_free(struct snd_device *device)
829 struct snd_timer *timer = device->device_data;
830 return snd_timer_free(timer);
833 static int snd_timer_dev_register(struct snd_device *dev)
835 struct snd_timer *timer = dev->device_data;
836 struct snd_timer *timer1;
838 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
839 return -ENXIO;
840 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
841 !timer->hw.resolution && timer->hw.c_resolution == NULL)
842 return -EINVAL;
844 mutex_lock(&register_mutex);
845 list_for_each_entry(timer1, &snd_timer_list, device_list) {
846 if (timer1->tmr_class > timer->tmr_class)
847 break;
848 if (timer1->tmr_class < timer->tmr_class)
849 continue;
850 if (timer1->card && timer->card) {
851 if (timer1->card->number > timer->card->number)
852 break;
853 if (timer1->card->number < timer->card->number)
854 continue;
856 if (timer1->tmr_device > timer->tmr_device)
857 break;
858 if (timer1->tmr_device < timer->tmr_device)
859 continue;
860 if (timer1->tmr_subdevice > timer->tmr_subdevice)
861 break;
862 if (timer1->tmr_subdevice < timer->tmr_subdevice)
863 continue;
864 /* conflicts.. */
865 mutex_unlock(&register_mutex);
866 return -EBUSY;
868 list_add_tail(&timer->device_list, &timer1->device_list);
869 mutex_unlock(&register_mutex);
870 return 0;
873 static int snd_timer_dev_disconnect(struct snd_device *device)
875 struct snd_timer *timer = device->device_data;
876 mutex_lock(&register_mutex);
877 list_del_init(&timer->device_list);
878 mutex_unlock(&register_mutex);
879 return 0;
882 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
884 unsigned long flags;
885 unsigned long resolution = 0;
886 struct snd_timer_instance *ti, *ts;
888 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
889 return;
890 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
891 event > SNDRV_TIMER_EVENT_MRESUME))
892 return;
893 spin_lock_irqsave(&timer->lock, flags);
894 if (event == SNDRV_TIMER_EVENT_MSTART ||
895 event == SNDRV_TIMER_EVENT_MCONTINUE ||
896 event == SNDRV_TIMER_EVENT_MRESUME) {
897 if (timer->hw.c_resolution)
898 resolution = timer->hw.c_resolution(timer);
899 else
900 resolution = timer->hw.resolution;
902 list_for_each_entry(ti, &timer->active_list_head, active_list) {
903 if (ti->ccallback)
904 ti->ccallback(ti, event, tstamp, resolution);
905 list_for_each_entry(ts, &ti->slave_active_head, active_list)
906 if (ts->ccallback)
907 ts->ccallback(ts, event, tstamp, resolution);
909 spin_unlock_irqrestore(&timer->lock, flags);
913 * exported functions for global timers
915 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
917 struct snd_timer_id tid;
919 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
920 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
921 tid.card = -1;
922 tid.device = device;
923 tid.subdevice = 0;
924 return snd_timer_new(NULL, id, &tid, rtimer);
927 int snd_timer_global_free(struct snd_timer *timer)
929 return snd_timer_free(timer);
932 int snd_timer_global_register(struct snd_timer *timer)
934 struct snd_device dev;
936 memset(&dev, 0, sizeof(dev));
937 dev.device_data = timer;
938 return snd_timer_dev_register(&dev);
942 * System timer
945 struct snd_timer_system_private {
946 struct timer_list tlist;
947 unsigned long last_expires;
948 unsigned long last_jiffies;
949 unsigned long correction;
952 static void snd_timer_s_function(unsigned long data)
954 struct snd_timer *timer = (struct snd_timer *)data;
955 struct snd_timer_system_private *priv = timer->private_data;
956 unsigned long jiff = jiffies;
957 if (time_after(jiff, priv->last_expires))
958 priv->correction += (long)jiff - (long)priv->last_expires;
959 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
962 static int snd_timer_s_start(struct snd_timer * timer)
964 struct snd_timer_system_private *priv;
965 unsigned long njiff;
967 priv = (struct snd_timer_system_private *) timer->private_data;
968 njiff = (priv->last_jiffies = jiffies);
969 if (priv->correction > timer->sticks - 1) {
970 priv->correction -= timer->sticks - 1;
971 njiff++;
972 } else {
973 njiff += timer->sticks - priv->correction;
974 priv->correction = 0;
976 priv->last_expires = priv->tlist.expires = njiff;
977 add_timer(&priv->tlist);
978 return 0;
981 static int snd_timer_s_stop(struct snd_timer * timer)
983 struct snd_timer_system_private *priv;
984 unsigned long jiff;
986 priv = (struct snd_timer_system_private *) timer->private_data;
987 del_timer(&priv->tlist);
988 jiff = jiffies;
989 if (time_before(jiff, priv->last_expires))
990 timer->sticks = priv->last_expires - jiff;
991 else
992 timer->sticks = 1;
993 priv->correction = 0;
994 return 0;
997 static struct snd_timer_hardware snd_timer_system =
999 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1000 .resolution = 1000000000L / HZ,
1001 .ticks = 10000000L,
1002 .start = snd_timer_s_start,
1003 .stop = snd_timer_s_stop
1006 static void snd_timer_free_system(struct snd_timer *timer)
1008 kfree(timer->private_data);
1011 static int snd_timer_register_system(void)
1013 struct snd_timer *timer;
1014 struct snd_timer_system_private *priv;
1015 int err;
1017 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1018 if (err < 0)
1019 return err;
1020 strcpy(timer->name, "system timer");
1021 timer->hw = snd_timer_system;
1022 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1023 if (priv == NULL) {
1024 snd_timer_free(timer);
1025 return -ENOMEM;
1027 init_timer(&priv->tlist);
1028 priv->tlist.function = snd_timer_s_function;
1029 priv->tlist.data = (unsigned long) timer;
1030 timer->private_data = priv;
1031 timer->private_free = snd_timer_free_system;
1032 return snd_timer_global_register(timer);
1035 #ifdef CONFIG_PROC_FS
1037 * Info interface
1040 static void snd_timer_proc_read(struct snd_info_entry *entry,
1041 struct snd_info_buffer *buffer)
1043 struct snd_timer *timer;
1044 struct snd_timer_instance *ti;
1046 mutex_lock(&register_mutex);
1047 list_for_each_entry(timer, &snd_timer_list, device_list) {
1048 switch (timer->tmr_class) {
1049 case SNDRV_TIMER_CLASS_GLOBAL:
1050 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1051 break;
1052 case SNDRV_TIMER_CLASS_CARD:
1053 snd_iprintf(buffer, "C%i-%i: ",
1054 timer->card->number, timer->tmr_device);
1055 break;
1056 case SNDRV_TIMER_CLASS_PCM:
1057 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1058 timer->tmr_device, timer->tmr_subdevice);
1059 break;
1060 default:
1061 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1062 timer->card ? timer->card->number : -1,
1063 timer->tmr_device, timer->tmr_subdevice);
1065 snd_iprintf(buffer, "%s :", timer->name);
1066 if (timer->hw.resolution)
1067 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1068 timer->hw.resolution / 1000,
1069 timer->hw.resolution % 1000,
1070 timer->hw.ticks);
1071 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1072 snd_iprintf(buffer, " SLAVE");
1073 snd_iprintf(buffer, "\n");
1074 list_for_each_entry(ti, &timer->open_list_head, open_list)
1075 snd_iprintf(buffer, " Client %s : %s\n",
1076 ti->owner ? ti->owner : "unknown",
1077 ti->flags & (SNDRV_TIMER_IFLG_START |
1078 SNDRV_TIMER_IFLG_RUNNING)
1079 ? "running" : "stopped");
1081 mutex_unlock(&register_mutex);
1084 static struct snd_info_entry *snd_timer_proc_entry;
1086 static void __init snd_timer_proc_init(void)
1088 struct snd_info_entry *entry;
1090 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1091 if (entry != NULL) {
1092 entry->c.text.read = snd_timer_proc_read;
1093 if (snd_info_register(entry) < 0) {
1094 snd_info_free_entry(entry);
1095 entry = NULL;
1098 snd_timer_proc_entry = entry;
1101 static void __exit snd_timer_proc_done(void)
1103 snd_info_free_entry(snd_timer_proc_entry);
1105 #else /* !CONFIG_PROC_FS */
1106 #define snd_timer_proc_init()
1107 #define snd_timer_proc_done()
1108 #endif
1111 * USER SPACE interface
1114 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1115 unsigned long resolution,
1116 unsigned long ticks)
1118 struct snd_timer_user *tu = timeri->callback_data;
1119 struct snd_timer_read *r;
1120 int prev;
1122 spin_lock(&tu->qlock);
1123 if (tu->qused > 0) {
1124 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1125 r = &tu->queue[prev];
1126 if (r->resolution == resolution) {
1127 r->ticks += ticks;
1128 goto __wake;
1131 if (tu->qused >= tu->queue_size) {
1132 tu->overrun++;
1133 } else {
1134 r = &tu->queue[tu->qtail++];
1135 tu->qtail %= tu->queue_size;
1136 r->resolution = resolution;
1137 r->ticks = ticks;
1138 tu->qused++;
1140 __wake:
1141 spin_unlock(&tu->qlock);
1142 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1143 wake_up(&tu->qchange_sleep);
1146 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1147 struct snd_timer_tread *tread)
1149 if (tu->qused >= tu->queue_size) {
1150 tu->overrun++;
1151 } else {
1152 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1153 tu->qtail %= tu->queue_size;
1154 tu->qused++;
1158 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1159 int event,
1160 struct timespec *tstamp,
1161 unsigned long resolution)
1163 struct snd_timer_user *tu = timeri->callback_data;
1164 struct snd_timer_tread r1;
1166 if (event >= SNDRV_TIMER_EVENT_START &&
1167 event <= SNDRV_TIMER_EVENT_PAUSE)
1168 tu->tstamp = *tstamp;
1169 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1170 return;
1171 r1.event = event;
1172 r1.tstamp = *tstamp;
1173 r1.val = resolution;
1174 spin_lock(&tu->qlock);
1175 snd_timer_user_append_to_tqueue(tu, &r1);
1176 spin_unlock(&tu->qlock);
1177 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1178 wake_up(&tu->qchange_sleep);
1181 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1182 unsigned long resolution,
1183 unsigned long ticks)
1185 struct snd_timer_user *tu = timeri->callback_data;
1186 struct snd_timer_tread *r, r1;
1187 struct timespec tstamp;
1188 int prev, append = 0;
1190 memset(&tstamp, 0, sizeof(tstamp));
1191 spin_lock(&tu->qlock);
1192 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1193 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1194 spin_unlock(&tu->qlock);
1195 return;
1197 if (tu->last_resolution != resolution || ticks > 0) {
1198 if (timer_tstamp_monotonic)
1199 do_posix_clock_monotonic_gettime(&tstamp);
1200 else
1201 getnstimeofday(&tstamp);
1203 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1204 tu->last_resolution != resolution) {
1205 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1206 r1.tstamp = tstamp;
1207 r1.val = resolution;
1208 snd_timer_user_append_to_tqueue(tu, &r1);
1209 tu->last_resolution = resolution;
1210 append++;
1212 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1213 goto __wake;
1214 if (ticks == 0)
1215 goto __wake;
1216 if (tu->qused > 0) {
1217 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1218 r = &tu->tqueue[prev];
1219 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1220 r->tstamp = tstamp;
1221 r->val += ticks;
1222 append++;
1223 goto __wake;
1226 r1.event = SNDRV_TIMER_EVENT_TICK;
1227 r1.tstamp = tstamp;
1228 r1.val = ticks;
1229 snd_timer_user_append_to_tqueue(tu, &r1);
1230 append++;
1231 __wake:
1232 spin_unlock(&tu->qlock);
1233 if (append == 0)
1234 return;
1235 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1236 wake_up(&tu->qchange_sleep);
1239 static int snd_timer_user_open(struct inode *inode, struct file *file)
1241 struct snd_timer_user *tu;
1243 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1244 if (tu == NULL)
1245 return -ENOMEM;
1246 spin_lock_init(&tu->qlock);
1247 init_waitqueue_head(&tu->qchange_sleep);
1248 mutex_init(&tu->tread_sem);
1249 tu->ticks = 1;
1250 tu->queue_size = 128;
1251 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1252 GFP_KERNEL);
1253 if (tu->queue == NULL) {
1254 kfree(tu);
1255 return -ENOMEM;
1257 file->private_data = tu;
1258 return 0;
1261 static int snd_timer_user_release(struct inode *inode, struct file *file)
1263 struct snd_timer_user *tu;
1265 if (file->private_data) {
1266 tu = file->private_data;
1267 file->private_data = NULL;
1268 if (tu->timeri)
1269 snd_timer_close(tu->timeri);
1270 kfree(tu->queue);
1271 kfree(tu->tqueue);
1272 kfree(tu);
1274 return 0;
1277 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1279 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1280 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1281 id->card = -1;
1282 id->device = -1;
1283 id->subdevice = -1;
1286 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1288 id->dev_class = timer->tmr_class;
1289 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1290 id->card = timer->card ? timer->card->number : -1;
1291 id->device = timer->tmr_device;
1292 id->subdevice = timer->tmr_subdevice;
1295 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1297 struct snd_timer_id id;
1298 struct snd_timer *timer;
1299 struct list_head *p;
1301 if (copy_from_user(&id, _tid, sizeof(id)))
1302 return -EFAULT;
1303 mutex_lock(&register_mutex);
1304 if (id.dev_class < 0) { /* first item */
1305 if (list_empty(&snd_timer_list))
1306 snd_timer_user_zero_id(&id);
1307 else {
1308 timer = list_entry(snd_timer_list.next,
1309 struct snd_timer, device_list);
1310 snd_timer_user_copy_id(&id, timer);
1312 } else {
1313 switch (id.dev_class) {
1314 case SNDRV_TIMER_CLASS_GLOBAL:
1315 id.device = id.device < 0 ? 0 : id.device + 1;
1316 list_for_each(p, &snd_timer_list) {
1317 timer = list_entry(p, struct snd_timer, device_list);
1318 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1319 snd_timer_user_copy_id(&id, timer);
1320 break;
1322 if (timer->tmr_device >= id.device) {
1323 snd_timer_user_copy_id(&id, timer);
1324 break;
1327 if (p == &snd_timer_list)
1328 snd_timer_user_zero_id(&id);
1329 break;
1330 case SNDRV_TIMER_CLASS_CARD:
1331 case SNDRV_TIMER_CLASS_PCM:
1332 if (id.card < 0) {
1333 id.card = 0;
1334 } else {
1335 if (id.card < 0) {
1336 id.card = 0;
1337 } else {
1338 if (id.device < 0) {
1339 id.device = 0;
1340 } else {
1341 if (id.subdevice < 0) {
1342 id.subdevice = 0;
1343 } else {
1344 id.subdevice++;
1349 list_for_each(p, &snd_timer_list) {
1350 timer = list_entry(p, struct snd_timer, device_list);
1351 if (timer->tmr_class > id.dev_class) {
1352 snd_timer_user_copy_id(&id, timer);
1353 break;
1355 if (timer->tmr_class < id.dev_class)
1356 continue;
1357 if (timer->card->number > id.card) {
1358 snd_timer_user_copy_id(&id, timer);
1359 break;
1361 if (timer->card->number < id.card)
1362 continue;
1363 if (timer->tmr_device > id.device) {
1364 snd_timer_user_copy_id(&id, timer);
1365 break;
1367 if (timer->tmr_device < id.device)
1368 continue;
1369 if (timer->tmr_subdevice > id.subdevice) {
1370 snd_timer_user_copy_id(&id, timer);
1371 break;
1373 if (timer->tmr_subdevice < id.subdevice)
1374 continue;
1375 snd_timer_user_copy_id(&id, timer);
1376 break;
1378 if (p == &snd_timer_list)
1379 snd_timer_user_zero_id(&id);
1380 break;
1381 default:
1382 snd_timer_user_zero_id(&id);
1385 mutex_unlock(&register_mutex);
1386 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1387 return -EFAULT;
1388 return 0;
1391 static int snd_timer_user_ginfo(struct file *file,
1392 struct snd_timer_ginfo __user *_ginfo)
1394 struct snd_timer_ginfo *ginfo;
1395 struct snd_timer_id tid;
1396 struct snd_timer *t;
1397 struct list_head *p;
1398 int err = 0;
1400 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1401 if (IS_ERR(ginfo))
1402 return PTR_ERR(ginfo);
1404 tid = ginfo->tid;
1405 memset(ginfo, 0, sizeof(*ginfo));
1406 ginfo->tid = tid;
1407 mutex_lock(&register_mutex);
1408 t = snd_timer_find(&tid);
1409 if (t != NULL) {
1410 ginfo->card = t->card ? t->card->number : -1;
1411 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1412 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1413 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1414 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1415 ginfo->resolution = t->hw.resolution;
1416 if (t->hw.resolution_min > 0) {
1417 ginfo->resolution_min = t->hw.resolution_min;
1418 ginfo->resolution_max = t->hw.resolution_max;
1420 list_for_each(p, &t->open_list_head) {
1421 ginfo->clients++;
1423 } else {
1424 err = -ENODEV;
1426 mutex_unlock(&register_mutex);
1427 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1428 err = -EFAULT;
1429 kfree(ginfo);
1430 return err;
1433 static int snd_timer_user_gparams(struct file *file,
1434 struct snd_timer_gparams __user *_gparams)
1436 struct snd_timer_gparams gparams;
1437 struct snd_timer *t;
1438 int err;
1440 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1441 return -EFAULT;
1442 mutex_lock(&register_mutex);
1443 t = snd_timer_find(&gparams.tid);
1444 if (!t) {
1445 err = -ENODEV;
1446 goto _error;
1448 if (!list_empty(&t->open_list_head)) {
1449 err = -EBUSY;
1450 goto _error;
1452 if (!t->hw.set_period) {
1453 err = -ENOSYS;
1454 goto _error;
1456 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1457 _error:
1458 mutex_unlock(&register_mutex);
1459 return err;
1462 static int snd_timer_user_gstatus(struct file *file,
1463 struct snd_timer_gstatus __user *_gstatus)
1465 struct snd_timer_gstatus gstatus;
1466 struct snd_timer_id tid;
1467 struct snd_timer *t;
1468 int err = 0;
1470 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1471 return -EFAULT;
1472 tid = gstatus.tid;
1473 memset(&gstatus, 0, sizeof(gstatus));
1474 gstatus.tid = tid;
1475 mutex_lock(&register_mutex);
1476 t = snd_timer_find(&tid);
1477 if (t != NULL) {
1478 if (t->hw.c_resolution)
1479 gstatus.resolution = t->hw.c_resolution(t);
1480 else
1481 gstatus.resolution = t->hw.resolution;
1482 if (t->hw.precise_resolution) {
1483 t->hw.precise_resolution(t, &gstatus.resolution_num,
1484 &gstatus.resolution_den);
1485 } else {
1486 gstatus.resolution_num = gstatus.resolution;
1487 gstatus.resolution_den = 1000000000uL;
1489 } else {
1490 err = -ENODEV;
1492 mutex_unlock(&register_mutex);
1493 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1494 err = -EFAULT;
1495 return err;
1498 static int snd_timer_user_tselect(struct file *file,
1499 struct snd_timer_select __user *_tselect)
1501 struct snd_timer_user *tu;
1502 struct snd_timer_select tselect;
1503 char str[32];
1504 int err = 0;
1506 tu = file->private_data;
1507 mutex_lock(&tu->tread_sem);
1508 if (tu->timeri) {
1509 snd_timer_close(tu->timeri);
1510 tu->timeri = NULL;
1512 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1513 err = -EFAULT;
1514 goto __err;
1516 sprintf(str, "application %i", current->pid);
1517 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1518 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1519 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1520 if (err < 0)
1521 goto __err;
1523 kfree(tu->queue);
1524 tu->queue = NULL;
1525 kfree(tu->tqueue);
1526 tu->tqueue = NULL;
1527 if (tu->tread) {
1528 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1529 GFP_KERNEL);
1530 if (tu->tqueue == NULL)
1531 err = -ENOMEM;
1532 } else {
1533 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1534 GFP_KERNEL);
1535 if (tu->queue == NULL)
1536 err = -ENOMEM;
1539 if (err < 0) {
1540 snd_timer_close(tu->timeri);
1541 tu->timeri = NULL;
1542 } else {
1543 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1544 tu->timeri->callback = tu->tread
1545 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1546 tu->timeri->ccallback = snd_timer_user_ccallback;
1547 tu->timeri->callback_data = (void *)tu;
1550 __err:
1551 mutex_unlock(&tu->tread_sem);
1552 return err;
1555 static int snd_timer_user_info(struct file *file,
1556 struct snd_timer_info __user *_info)
1558 struct snd_timer_user *tu;
1559 struct snd_timer_info *info;
1560 struct snd_timer *t;
1561 int err = 0;
1563 tu = file->private_data;
1564 if (!tu->timeri)
1565 return -EBADFD;
1566 t = tu->timeri->timer;
1567 if (!t)
1568 return -EBADFD;
1570 info = kzalloc(sizeof(*info), GFP_KERNEL);
1571 if (! info)
1572 return -ENOMEM;
1573 info->card = t->card ? t->card->number : -1;
1574 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1575 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1576 strlcpy(info->id, t->id, sizeof(info->id));
1577 strlcpy(info->name, t->name, sizeof(info->name));
1578 info->resolution = t->hw.resolution;
1579 if (copy_to_user(_info, info, sizeof(*_info)))
1580 err = -EFAULT;
1581 kfree(info);
1582 return err;
1585 static int snd_timer_user_params(struct file *file,
1586 struct snd_timer_params __user *_params)
1588 struct snd_timer_user *tu;
1589 struct snd_timer_params params;
1590 struct snd_timer *t;
1591 struct snd_timer_read *tr;
1592 struct snd_timer_tread *ttr;
1593 int err;
1595 tu = file->private_data;
1596 if (!tu->timeri)
1597 return -EBADFD;
1598 t = tu->timeri->timer;
1599 if (!t)
1600 return -EBADFD;
1601 if (copy_from_user(&params, _params, sizeof(params)))
1602 return -EFAULT;
1603 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1604 err = -EINVAL;
1605 goto _end;
1607 if (params.queue_size > 0 &&
1608 (params.queue_size < 32 || params.queue_size > 1024)) {
1609 err = -EINVAL;
1610 goto _end;
1612 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1613 (1<<SNDRV_TIMER_EVENT_TICK)|
1614 (1<<SNDRV_TIMER_EVENT_START)|
1615 (1<<SNDRV_TIMER_EVENT_STOP)|
1616 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1617 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1618 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1619 (1<<SNDRV_TIMER_EVENT_RESUME)|
1620 (1<<SNDRV_TIMER_EVENT_MSTART)|
1621 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1622 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1623 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1624 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1625 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1626 err = -EINVAL;
1627 goto _end;
1629 snd_timer_stop(tu->timeri);
1630 spin_lock_irq(&t->lock);
1631 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1632 SNDRV_TIMER_IFLG_EXCLUSIVE|
1633 SNDRV_TIMER_IFLG_EARLY_EVENT);
1634 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1635 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1636 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1637 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1638 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1639 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1640 spin_unlock_irq(&t->lock);
1641 if (params.queue_size > 0 &&
1642 (unsigned int)tu->queue_size != params.queue_size) {
1643 if (tu->tread) {
1644 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1645 GFP_KERNEL);
1646 if (ttr) {
1647 kfree(tu->tqueue);
1648 tu->queue_size = params.queue_size;
1649 tu->tqueue = ttr;
1651 } else {
1652 tr = kmalloc(params.queue_size * sizeof(*tr),
1653 GFP_KERNEL);
1654 if (tr) {
1655 kfree(tu->queue);
1656 tu->queue_size = params.queue_size;
1657 tu->queue = tr;
1661 tu->qhead = tu->qtail = tu->qused = 0;
1662 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1663 if (tu->tread) {
1664 struct snd_timer_tread tread;
1665 tread.event = SNDRV_TIMER_EVENT_EARLY;
1666 tread.tstamp.tv_sec = 0;
1667 tread.tstamp.tv_nsec = 0;
1668 tread.val = 0;
1669 snd_timer_user_append_to_tqueue(tu, &tread);
1670 } else {
1671 struct snd_timer_read *r = &tu->queue[0];
1672 r->resolution = 0;
1673 r->ticks = 0;
1674 tu->qused++;
1675 tu->qtail++;
1678 tu->filter = params.filter;
1679 tu->ticks = params.ticks;
1680 err = 0;
1681 _end:
1682 if (copy_to_user(_params, &params, sizeof(params)))
1683 return -EFAULT;
1684 return err;
1687 static int snd_timer_user_status(struct file *file,
1688 struct snd_timer_status __user *_status)
1690 struct snd_timer_user *tu;
1691 struct snd_timer_status status;
1693 tu = file->private_data;
1694 if (!tu->timeri)
1695 return -EBADFD;
1696 memset(&status, 0, sizeof(status));
1697 status.tstamp = tu->tstamp;
1698 status.resolution = snd_timer_resolution(tu->timeri);
1699 status.lost = tu->timeri->lost;
1700 status.overrun = tu->overrun;
1701 spin_lock_irq(&tu->qlock);
1702 status.queue = tu->qused;
1703 spin_unlock_irq(&tu->qlock);
1704 if (copy_to_user(_status, &status, sizeof(status)))
1705 return -EFAULT;
1706 return 0;
1709 static int snd_timer_user_start(struct file *file)
1711 int err;
1712 struct snd_timer_user *tu;
1714 tu = file->private_data;
1715 if (!tu->timeri)
1716 return -EBADFD;
1717 snd_timer_stop(tu->timeri);
1718 tu->timeri->lost = 0;
1719 tu->last_resolution = 0;
1720 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1723 static int snd_timer_user_stop(struct file *file)
1725 int err;
1726 struct snd_timer_user *tu;
1728 tu = file->private_data;
1729 if (!tu->timeri)
1730 return -EBADFD;
1731 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1734 static int snd_timer_user_continue(struct file *file)
1736 int err;
1737 struct snd_timer_user *tu;
1739 tu = file->private_data;
1740 if (!tu->timeri)
1741 return -EBADFD;
1742 tu->timeri->lost = 0;
1743 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1746 static int snd_timer_user_pause(struct file *file)
1748 int err;
1749 struct snd_timer_user *tu;
1751 tu = file->private_data;
1752 if (!tu->timeri)
1753 return -EBADFD;
1754 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1757 enum {
1758 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1759 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1760 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1761 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1764 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1765 unsigned long arg)
1767 struct snd_timer_user *tu;
1768 void __user *argp = (void __user *)arg;
1769 int __user *p = argp;
1771 tu = file->private_data;
1772 switch (cmd) {
1773 case SNDRV_TIMER_IOCTL_PVERSION:
1774 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1775 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1776 return snd_timer_user_next_device(argp);
1777 case SNDRV_TIMER_IOCTL_TREAD:
1779 int xarg;
1781 mutex_lock(&tu->tread_sem);
1782 if (tu->timeri) { /* too late */
1783 mutex_unlock(&tu->tread_sem);
1784 return -EBUSY;
1786 if (get_user(xarg, p)) {
1787 mutex_unlock(&tu->tread_sem);
1788 return -EFAULT;
1790 tu->tread = xarg ? 1 : 0;
1791 mutex_unlock(&tu->tread_sem);
1792 return 0;
1794 case SNDRV_TIMER_IOCTL_GINFO:
1795 return snd_timer_user_ginfo(file, argp);
1796 case SNDRV_TIMER_IOCTL_GPARAMS:
1797 return snd_timer_user_gparams(file, argp);
1798 case SNDRV_TIMER_IOCTL_GSTATUS:
1799 return snd_timer_user_gstatus(file, argp);
1800 case SNDRV_TIMER_IOCTL_SELECT:
1801 return snd_timer_user_tselect(file, argp);
1802 case SNDRV_TIMER_IOCTL_INFO:
1803 return snd_timer_user_info(file, argp);
1804 case SNDRV_TIMER_IOCTL_PARAMS:
1805 return snd_timer_user_params(file, argp);
1806 case SNDRV_TIMER_IOCTL_STATUS:
1807 return snd_timer_user_status(file, argp);
1808 case SNDRV_TIMER_IOCTL_START:
1809 case SNDRV_TIMER_IOCTL_START_OLD:
1810 return snd_timer_user_start(file);
1811 case SNDRV_TIMER_IOCTL_STOP:
1812 case SNDRV_TIMER_IOCTL_STOP_OLD:
1813 return snd_timer_user_stop(file);
1814 case SNDRV_TIMER_IOCTL_CONTINUE:
1815 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1816 return snd_timer_user_continue(file);
1817 case SNDRV_TIMER_IOCTL_PAUSE:
1818 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1819 return snd_timer_user_pause(file);
1821 return -ENOTTY;
1824 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1826 struct snd_timer_user *tu;
1828 tu = file->private_data;
1829 return fasync_helper(fd, file, on, &tu->fasync);
1832 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1833 size_t count, loff_t *offset)
1835 struct snd_timer_user *tu;
1836 long result = 0, unit;
1837 int err = 0;
1839 tu = file->private_data;
1840 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1841 spin_lock_irq(&tu->qlock);
1842 while ((long)count - result >= unit) {
1843 while (!tu->qused) {
1844 wait_queue_t wait;
1846 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1847 err = -EAGAIN;
1848 break;
1851 set_current_state(TASK_INTERRUPTIBLE);
1852 init_waitqueue_entry(&wait, current);
1853 add_wait_queue(&tu->qchange_sleep, &wait);
1855 spin_unlock_irq(&tu->qlock);
1856 schedule();
1857 spin_lock_irq(&tu->qlock);
1859 remove_wait_queue(&tu->qchange_sleep, &wait);
1861 if (signal_pending(current)) {
1862 err = -ERESTARTSYS;
1863 break;
1867 spin_unlock_irq(&tu->qlock);
1868 if (err < 0)
1869 goto _error;
1871 if (tu->tread) {
1872 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1873 sizeof(struct snd_timer_tread))) {
1874 err = -EFAULT;
1875 goto _error;
1877 } else {
1878 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1879 sizeof(struct snd_timer_read))) {
1880 err = -EFAULT;
1881 goto _error;
1885 tu->qhead %= tu->queue_size;
1887 result += unit;
1888 buffer += unit;
1890 spin_lock_irq(&tu->qlock);
1891 tu->qused--;
1893 spin_unlock_irq(&tu->qlock);
1894 _error:
1895 return result > 0 ? result : err;
1898 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1900 unsigned int mask;
1901 struct snd_timer_user *tu;
1903 tu = file->private_data;
1905 poll_wait(file, &tu->qchange_sleep, wait);
1907 mask = 0;
1908 if (tu->qused)
1909 mask |= POLLIN | POLLRDNORM;
1911 return mask;
1914 #ifdef CONFIG_COMPAT
1915 #include "timer_compat.c"
1916 #else
1917 #define snd_timer_user_ioctl_compat NULL
1918 #endif
1920 static const struct file_operations snd_timer_f_ops =
1922 .owner = THIS_MODULE,
1923 .read = snd_timer_user_read,
1924 .open = snd_timer_user_open,
1925 .release = snd_timer_user_release,
1926 .poll = snd_timer_user_poll,
1927 .unlocked_ioctl = snd_timer_user_ioctl,
1928 .compat_ioctl = snd_timer_user_ioctl_compat,
1929 .fasync = snd_timer_user_fasync,
1933 * ENTRY functions
1936 static int __init alsa_timer_init(void)
1938 int err;
1940 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1941 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1942 "system timer");
1943 #endif
1945 if ((err = snd_timer_register_system()) < 0)
1946 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1947 err);
1948 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1949 &snd_timer_f_ops, NULL, "timer")) < 0)
1950 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1951 err);
1952 snd_timer_proc_init();
1953 return 0;
1956 static void __exit alsa_timer_exit(void)
1958 struct list_head *p, *n;
1960 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1961 /* unregister the system timer */
1962 list_for_each_safe(p, n, &snd_timer_list) {
1963 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
1964 snd_timer_free(timer);
1966 snd_timer_proc_done();
1967 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1968 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1969 #endif
1972 module_init(alsa_timer_init)
1973 module_exit(alsa_timer_exit)
1975 EXPORT_SYMBOL(snd_timer_open);
1976 EXPORT_SYMBOL(snd_timer_close);
1977 EXPORT_SYMBOL(snd_timer_resolution);
1978 EXPORT_SYMBOL(snd_timer_start);
1979 EXPORT_SYMBOL(snd_timer_stop);
1980 EXPORT_SYMBOL(snd_timer_continue);
1981 EXPORT_SYMBOL(snd_timer_pause);
1982 EXPORT_SYMBOL(snd_timer_new);
1983 EXPORT_SYMBOL(snd_timer_notify);
1984 EXPORT_SYMBOL(snd_timer_global_new);
1985 EXPORT_SYMBOL(snd_timer_global_free);
1986 EXPORT_SYMBOL(snd_timer_global_register);
1987 EXPORT_SYMBOL(snd_timer_interrupt);