ALSA: timer: Simplify error path in snd_timer_open()
[linux/fpc-iii.git] / sound / core / timer.c
blob6eb4e97662d9c04593f42cf3c84ee913c4466965
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/device.h>
28 #include <linux/module.h>
29 #include <linux/string.h>
30 #include <sound/core.h>
31 #include <sound/timer.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/minors.h>
35 #include <sound/initval.h>
36 #include <linux/kmod.h>
38 /* internal flags */
39 #define SNDRV_TIMER_IFLG_PAUSED 0x00010000
41 #if IS_ENABLED(CONFIG_SND_HRTIMER)
42 #define DEFAULT_TIMER_LIMIT 4
43 #else
44 #define DEFAULT_TIMER_LIMIT 1
45 #endif
47 static int timer_limit = DEFAULT_TIMER_LIMIT;
48 static int timer_tstamp_monotonic = 1;
49 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
50 MODULE_DESCRIPTION("ALSA timer interface");
51 MODULE_LICENSE("GPL");
52 module_param(timer_limit, int, 0444);
53 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
54 module_param(timer_tstamp_monotonic, int, 0444);
55 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
57 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
58 MODULE_ALIAS("devname:snd/timer");
60 struct snd_timer_user {
61 struct snd_timer_instance *timeri;
62 int tread; /* enhanced read with timestamps and events */
63 unsigned long ticks;
64 unsigned long overrun;
65 int qhead;
66 int qtail;
67 int qused;
68 int queue_size;
69 bool disconnected;
70 struct snd_timer_read *queue;
71 struct snd_timer_tread *tqueue;
72 spinlock_t qlock;
73 unsigned long last_resolution;
74 unsigned int filter;
75 struct timespec tstamp; /* trigger tstamp */
76 wait_queue_head_t qchange_sleep;
77 struct fasync_struct *fasync;
78 struct mutex ioctl_lock;
81 /* list of timers */
82 static LIST_HEAD(snd_timer_list);
84 /* list of slave instances */
85 static LIST_HEAD(snd_timer_slave_list);
87 /* lock for slave active lists */
88 static DEFINE_SPINLOCK(slave_active_lock);
90 static DEFINE_MUTEX(register_mutex);
92 static int snd_timer_free(struct snd_timer *timer);
93 static int snd_timer_dev_free(struct snd_device *device);
94 static int snd_timer_dev_register(struct snd_device *device);
95 static int snd_timer_dev_disconnect(struct snd_device *device);
97 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
100 * create a timer instance with the given owner string.
101 * when timer is not NULL, increments the module counter
103 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
104 struct snd_timer *timer)
106 struct snd_timer_instance *timeri;
107 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
108 if (timeri == NULL)
109 return NULL;
110 timeri->owner = kstrdup(owner, GFP_KERNEL);
111 if (! timeri->owner) {
112 kfree(timeri);
113 return NULL;
115 INIT_LIST_HEAD(&timeri->open_list);
116 INIT_LIST_HEAD(&timeri->active_list);
117 INIT_LIST_HEAD(&timeri->ack_list);
118 INIT_LIST_HEAD(&timeri->slave_list_head);
119 INIT_LIST_HEAD(&timeri->slave_active_head);
121 timeri->timer = timer;
122 if (timer && !try_module_get(timer->module)) {
123 kfree(timeri->owner);
124 kfree(timeri);
125 return NULL;
128 return timeri;
132 * find a timer instance from the given timer id
134 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
136 struct snd_timer *timer = NULL;
138 list_for_each_entry(timer, &snd_timer_list, device_list) {
139 if (timer->tmr_class != tid->dev_class)
140 continue;
141 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
142 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
143 (timer->card == NULL ||
144 timer->card->number != tid->card))
145 continue;
146 if (timer->tmr_device != tid->device)
147 continue;
148 if (timer->tmr_subdevice != tid->subdevice)
149 continue;
150 return timer;
152 return NULL;
155 #ifdef CONFIG_MODULES
157 static void snd_timer_request(struct snd_timer_id *tid)
159 switch (tid->dev_class) {
160 case SNDRV_TIMER_CLASS_GLOBAL:
161 if (tid->device < timer_limit)
162 request_module("snd-timer-%i", tid->device);
163 break;
164 case SNDRV_TIMER_CLASS_CARD:
165 case SNDRV_TIMER_CLASS_PCM:
166 if (tid->card < snd_ecards_limit)
167 request_module("snd-card-%i", tid->card);
168 break;
169 default:
170 break;
174 #endif
177 * look for a master instance matching with the slave id of the given slave.
178 * when found, relink the open_link of the slave.
180 * call this with register_mutex down.
182 static int snd_timer_check_slave(struct snd_timer_instance *slave)
184 struct snd_timer *timer;
185 struct snd_timer_instance *master;
187 /* FIXME: it's really dumb to look up all entries.. */
188 list_for_each_entry(timer, &snd_timer_list, device_list) {
189 list_for_each_entry(master, &timer->open_list_head, open_list) {
190 if (slave->slave_class == master->slave_class &&
191 slave->slave_id == master->slave_id) {
192 if (master->timer->num_instances >=
193 master->timer->max_instances)
194 return -EBUSY;
195 list_move_tail(&slave->open_list,
196 &master->slave_list_head);
197 master->timer->num_instances++;
198 spin_lock_irq(&slave_active_lock);
199 slave->master = master;
200 slave->timer = master->timer;
201 spin_unlock_irq(&slave_active_lock);
202 return 0;
206 return 0;
210 * look for slave instances matching with the slave id of the given master.
211 * when found, relink the open_link of slaves.
213 * call this with register_mutex down.
215 static int snd_timer_check_master(struct snd_timer_instance *master)
217 struct snd_timer_instance *slave, *tmp;
219 /* check all pending slaves */
220 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
221 if (slave->slave_class == master->slave_class &&
222 slave->slave_id == master->slave_id) {
223 if (master->timer->num_instances >=
224 master->timer->max_instances)
225 return -EBUSY;
226 list_move_tail(&slave->open_list, &master->slave_list_head);
227 master->timer->num_instances++;
228 spin_lock_irq(&slave_active_lock);
229 spin_lock(&master->timer->lock);
230 slave->master = master;
231 slave->timer = master->timer;
232 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
233 list_add_tail(&slave->active_list,
234 &master->slave_active_head);
235 spin_unlock(&master->timer->lock);
236 spin_unlock_irq(&slave_active_lock);
239 return 0;
242 static int snd_timer_close_locked(struct snd_timer_instance *timeri);
245 * open a timer instance
246 * when opening a master, the slave id must be here given.
248 int snd_timer_open(struct snd_timer_instance **ti,
249 char *owner, struct snd_timer_id *tid,
250 unsigned int slave_id)
252 struct snd_timer *timer;
253 struct snd_timer_instance *timeri = NULL;
254 int err;
256 mutex_lock(&register_mutex);
257 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
258 /* open a slave instance */
259 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
260 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
261 pr_debug("ALSA: timer: invalid slave class %i\n",
262 tid->dev_sclass);
263 err = -EINVAL;
264 goto unlock;
266 timeri = snd_timer_instance_new(owner, NULL);
267 if (!timeri) {
268 err = -ENOMEM;
269 goto unlock;
271 timeri->slave_class = tid->dev_sclass;
272 timeri->slave_id = tid->device;
273 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
274 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
275 err = snd_timer_check_slave(timeri);
276 if (err < 0) {
277 snd_timer_close_locked(timeri);
278 timeri = NULL;
280 goto unlock;
283 /* open a master instance */
284 timer = snd_timer_find(tid);
285 #ifdef CONFIG_MODULES
286 if (!timer) {
287 mutex_unlock(&register_mutex);
288 snd_timer_request(tid);
289 mutex_lock(&register_mutex);
290 timer = snd_timer_find(tid);
292 #endif
293 if (!timer) {
294 err = -ENODEV;
295 goto unlock;
297 if (!list_empty(&timer->open_list_head)) {
298 timeri = list_entry(timer->open_list_head.next,
299 struct snd_timer_instance, open_list);
300 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
301 err = -EBUSY;
302 timeri = NULL;
303 goto unlock;
306 if (timer->num_instances >= timer->max_instances) {
307 err = -EBUSY;
308 goto unlock;
310 timeri = snd_timer_instance_new(owner, timer);
311 if (!timeri) {
312 err = -ENOMEM;
313 goto unlock;
315 /* take a card refcount for safe disconnection */
316 if (timer->card)
317 get_device(&timer->card->card_dev);
318 timeri->slave_class = tid->dev_sclass;
319 timeri->slave_id = slave_id;
321 if (list_empty(&timer->open_list_head) && timer->hw.open) {
322 err = timer->hw.open(timer);
323 if (err) {
324 kfree(timeri->owner);
325 kfree(timeri);
326 timeri = NULL;
328 if (timer->card)
329 put_device(&timer->card->card_dev);
330 module_put(timer->module);
331 goto unlock;
335 list_add_tail(&timeri->open_list, &timer->open_list_head);
336 timer->num_instances++;
337 err = snd_timer_check_master(timeri);
338 if (err < 0) {
339 snd_timer_close_locked(timeri);
340 timeri = NULL;
343 unlock:
344 mutex_unlock(&register_mutex);
345 *ti = timeri;
346 return err;
348 EXPORT_SYMBOL(snd_timer_open);
351 * close a timer instance
352 * call this with register_mutex down.
354 static int snd_timer_close_locked(struct snd_timer_instance *timeri)
356 struct snd_timer *timer = NULL;
357 struct snd_timer_instance *slave, *tmp;
359 list_del(&timeri->open_list);
361 /* force to stop the timer */
362 snd_timer_stop(timeri);
364 timer = timeri->timer;
365 if (timer) {
366 timer->num_instances--;
367 /* wait, until the active callback is finished */
368 spin_lock_irq(&timer->lock);
369 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
370 spin_unlock_irq(&timer->lock);
371 udelay(10);
372 spin_lock_irq(&timer->lock);
374 spin_unlock_irq(&timer->lock);
376 /* remove slave links */
377 spin_lock_irq(&slave_active_lock);
378 spin_lock(&timer->lock);
379 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
380 open_list) {
381 list_move_tail(&slave->open_list, &snd_timer_slave_list);
382 timer->num_instances--;
383 slave->master = NULL;
384 slave->timer = NULL;
385 list_del_init(&slave->ack_list);
386 list_del_init(&slave->active_list);
388 spin_unlock(&timer->lock);
389 spin_unlock_irq(&slave_active_lock);
391 /* slave doesn't need to release timer resources below */
392 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
393 timer = NULL;
396 if (timeri->private_free)
397 timeri->private_free(timeri);
398 kfree(timeri->owner);
399 kfree(timeri);
401 if (timer) {
402 if (list_empty(&timer->open_list_head) && timer->hw.close)
403 timer->hw.close(timer);
404 /* release a card refcount for safe disconnection */
405 if (timer->card)
406 put_device(&timer->card->card_dev);
407 module_put(timer->module);
410 return 0;
414 * close a timer instance
416 int snd_timer_close(struct snd_timer_instance *timeri)
418 int err;
420 if (snd_BUG_ON(!timeri))
421 return -ENXIO;
423 mutex_lock(&register_mutex);
424 err = snd_timer_close_locked(timeri);
425 mutex_unlock(&register_mutex);
426 return err;
428 EXPORT_SYMBOL(snd_timer_close);
430 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
432 struct snd_timer * timer;
434 if (timeri == NULL)
435 return 0;
436 if ((timer = timeri->timer) != NULL) {
437 if (timer->hw.c_resolution)
438 return timer->hw.c_resolution(timer);
439 return timer->hw.resolution;
441 return 0;
443 EXPORT_SYMBOL(snd_timer_resolution);
445 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
447 struct snd_timer *timer;
448 unsigned long resolution = 0;
449 struct snd_timer_instance *ts;
450 struct timespec tstamp;
452 if (timer_tstamp_monotonic)
453 ktime_get_ts(&tstamp);
454 else
455 getnstimeofday(&tstamp);
456 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
457 event > SNDRV_TIMER_EVENT_PAUSE))
458 return;
459 if (event == SNDRV_TIMER_EVENT_START ||
460 event == SNDRV_TIMER_EVENT_CONTINUE)
461 resolution = snd_timer_resolution(ti);
462 if (ti->ccallback)
463 ti->ccallback(ti, event, &tstamp, resolution);
464 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
465 return;
466 timer = ti->timer;
467 if (timer == NULL)
468 return;
469 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
470 return;
471 list_for_each_entry(ts, &ti->slave_active_head, active_list)
472 if (ts->ccallback)
473 ts->ccallback(ts, event + 100, &tstamp, resolution);
476 /* start/continue a master timer */
477 static int snd_timer_start1(struct snd_timer_instance *timeri,
478 bool start, unsigned long ticks)
480 struct snd_timer *timer;
481 int result;
482 unsigned long flags;
484 timer = timeri->timer;
485 if (!timer)
486 return -EINVAL;
488 spin_lock_irqsave(&timer->lock, flags);
489 if (timer->card && timer->card->shutdown) {
490 result = -ENODEV;
491 goto unlock;
493 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
494 SNDRV_TIMER_IFLG_START)) {
495 result = -EBUSY;
496 goto unlock;
499 if (start)
500 timeri->ticks = timeri->cticks = ticks;
501 else if (!timeri->cticks)
502 timeri->cticks = 1;
503 timeri->pticks = 0;
505 list_move_tail(&timeri->active_list, &timer->active_list_head);
506 if (timer->running) {
507 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
508 goto __start_now;
509 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
510 timeri->flags |= SNDRV_TIMER_IFLG_START;
511 result = 1; /* delayed start */
512 } else {
513 if (start)
514 timer->sticks = ticks;
515 timer->hw.start(timer);
516 __start_now:
517 timer->running++;
518 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
519 result = 0;
521 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
522 SNDRV_TIMER_EVENT_CONTINUE);
523 unlock:
524 spin_unlock_irqrestore(&timer->lock, flags);
525 return result;
528 /* start/continue a slave timer */
529 static int snd_timer_start_slave(struct snd_timer_instance *timeri,
530 bool start)
532 unsigned long flags;
534 spin_lock_irqsave(&slave_active_lock, flags);
535 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
536 spin_unlock_irqrestore(&slave_active_lock, flags);
537 return -EBUSY;
539 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
540 if (timeri->master && timeri->timer) {
541 spin_lock(&timeri->timer->lock);
542 list_add_tail(&timeri->active_list,
543 &timeri->master->slave_active_head);
544 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
545 SNDRV_TIMER_EVENT_CONTINUE);
546 spin_unlock(&timeri->timer->lock);
548 spin_unlock_irqrestore(&slave_active_lock, flags);
549 return 1; /* delayed start */
552 /* stop/pause a master timer */
553 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
555 struct snd_timer *timer;
556 int result = 0;
557 unsigned long flags;
559 timer = timeri->timer;
560 if (!timer)
561 return -EINVAL;
562 spin_lock_irqsave(&timer->lock, flags);
563 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
564 SNDRV_TIMER_IFLG_START))) {
565 result = -EBUSY;
566 goto unlock;
568 list_del_init(&timeri->ack_list);
569 list_del_init(&timeri->active_list);
570 if (timer->card && timer->card->shutdown)
571 goto unlock;
572 if (stop) {
573 timeri->cticks = timeri->ticks;
574 timeri->pticks = 0;
576 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
577 !(--timer->running)) {
578 timer->hw.stop(timer);
579 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
580 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
581 snd_timer_reschedule(timer, 0);
582 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
583 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
584 timer->hw.start(timer);
588 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
589 if (stop)
590 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
591 else
592 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
593 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
594 SNDRV_TIMER_EVENT_PAUSE);
595 unlock:
596 spin_unlock_irqrestore(&timer->lock, flags);
597 return result;
600 /* stop/pause a slave timer */
601 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
603 unsigned long flags;
605 spin_lock_irqsave(&slave_active_lock, flags);
606 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
607 spin_unlock_irqrestore(&slave_active_lock, flags);
608 return -EBUSY;
610 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
611 if (timeri->timer) {
612 spin_lock(&timeri->timer->lock);
613 list_del_init(&timeri->ack_list);
614 list_del_init(&timeri->active_list);
615 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
616 SNDRV_TIMER_EVENT_PAUSE);
617 spin_unlock(&timeri->timer->lock);
619 spin_unlock_irqrestore(&slave_active_lock, flags);
620 return 0;
624 * start the timer instance
626 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
628 if (timeri == NULL || ticks < 1)
629 return -EINVAL;
630 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
631 return snd_timer_start_slave(timeri, true);
632 else
633 return snd_timer_start1(timeri, true, ticks);
635 EXPORT_SYMBOL(snd_timer_start);
638 * stop the timer instance.
640 * do not call this from the timer callback!
642 int snd_timer_stop(struct snd_timer_instance *timeri)
644 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
645 return snd_timer_stop_slave(timeri, true);
646 else
647 return snd_timer_stop1(timeri, true);
649 EXPORT_SYMBOL(snd_timer_stop);
652 * start again.. the tick is kept.
654 int snd_timer_continue(struct snd_timer_instance *timeri)
656 /* timer can continue only after pause */
657 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
658 return -EINVAL;
660 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
661 return snd_timer_start_slave(timeri, false);
662 else
663 return snd_timer_start1(timeri, false, 0);
665 EXPORT_SYMBOL(snd_timer_continue);
668 * pause.. remember the ticks left
670 int snd_timer_pause(struct snd_timer_instance * timeri)
672 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
673 return snd_timer_stop_slave(timeri, false);
674 else
675 return snd_timer_stop1(timeri, false);
677 EXPORT_SYMBOL(snd_timer_pause);
680 * reschedule the timer
682 * start pending instances and check the scheduling ticks.
683 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
685 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
687 struct snd_timer_instance *ti;
688 unsigned long ticks = ~0UL;
690 list_for_each_entry(ti, &timer->active_list_head, active_list) {
691 if (ti->flags & SNDRV_TIMER_IFLG_START) {
692 ti->flags &= ~SNDRV_TIMER_IFLG_START;
693 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
694 timer->running++;
696 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
697 if (ticks > ti->cticks)
698 ticks = ti->cticks;
701 if (ticks == ~0UL) {
702 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
703 return;
705 if (ticks > timer->hw.ticks)
706 ticks = timer->hw.ticks;
707 if (ticks_left != ticks)
708 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
709 timer->sticks = ticks;
713 * timer tasklet
716 static void snd_timer_tasklet(unsigned long arg)
718 struct snd_timer *timer = (struct snd_timer *) arg;
719 struct snd_timer_instance *ti;
720 struct list_head *p;
721 unsigned long resolution, ticks;
722 unsigned long flags;
724 if (timer->card && timer->card->shutdown)
725 return;
727 spin_lock_irqsave(&timer->lock, flags);
728 /* now process all callbacks */
729 while (!list_empty(&timer->sack_list_head)) {
730 p = timer->sack_list_head.next; /* get first item */
731 ti = list_entry(p, struct snd_timer_instance, ack_list);
733 /* remove from ack_list and make empty */
734 list_del_init(p);
736 ticks = ti->pticks;
737 ti->pticks = 0;
738 resolution = ti->resolution;
740 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
741 spin_unlock(&timer->lock);
742 if (ti->callback)
743 ti->callback(ti, resolution, ticks);
744 spin_lock(&timer->lock);
745 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
747 spin_unlock_irqrestore(&timer->lock, flags);
751 * timer interrupt
753 * ticks_left is usually equal to timer->sticks.
756 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
758 struct snd_timer_instance *ti, *ts, *tmp;
759 unsigned long resolution, ticks;
760 struct list_head *p, *ack_list_head;
761 unsigned long flags;
762 int use_tasklet = 0;
764 if (timer == NULL)
765 return;
767 if (timer->card && timer->card->shutdown)
768 return;
770 spin_lock_irqsave(&timer->lock, flags);
772 /* remember the current resolution */
773 if (timer->hw.c_resolution)
774 resolution = timer->hw.c_resolution(timer);
775 else
776 resolution = timer->hw.resolution;
778 /* loop for all active instances
779 * Here we cannot use list_for_each_entry because the active_list of a
780 * processed instance is relinked to done_list_head before the callback
781 * is called.
783 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
784 active_list) {
785 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
786 continue;
787 ti->pticks += ticks_left;
788 ti->resolution = resolution;
789 if (ti->cticks < ticks_left)
790 ti->cticks = 0;
791 else
792 ti->cticks -= ticks_left;
793 if (ti->cticks) /* not expired */
794 continue;
795 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
796 ti->cticks = ti->ticks;
797 } else {
798 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
799 --timer->running;
800 list_del_init(&ti->active_list);
802 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
803 (ti->flags & SNDRV_TIMER_IFLG_FAST))
804 ack_list_head = &timer->ack_list_head;
805 else
806 ack_list_head = &timer->sack_list_head;
807 if (list_empty(&ti->ack_list))
808 list_add_tail(&ti->ack_list, ack_list_head);
809 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
810 ts->pticks = ti->pticks;
811 ts->resolution = resolution;
812 if (list_empty(&ts->ack_list))
813 list_add_tail(&ts->ack_list, ack_list_head);
816 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
817 snd_timer_reschedule(timer, timer->sticks);
818 if (timer->running) {
819 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
820 timer->hw.stop(timer);
821 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
823 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
824 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
825 /* restart timer */
826 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
827 timer->hw.start(timer);
829 } else {
830 timer->hw.stop(timer);
833 /* now process all fast callbacks */
834 while (!list_empty(&timer->ack_list_head)) {
835 p = timer->ack_list_head.next; /* get first item */
836 ti = list_entry(p, struct snd_timer_instance, ack_list);
838 /* remove from ack_list and make empty */
839 list_del_init(p);
841 ticks = ti->pticks;
842 ti->pticks = 0;
844 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
845 spin_unlock(&timer->lock);
846 if (ti->callback)
847 ti->callback(ti, resolution, ticks);
848 spin_lock(&timer->lock);
849 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
852 /* do we have any slow callbacks? */
853 use_tasklet = !list_empty(&timer->sack_list_head);
854 spin_unlock_irqrestore(&timer->lock, flags);
856 if (use_tasklet)
857 tasklet_schedule(&timer->task_queue);
859 EXPORT_SYMBOL(snd_timer_interrupt);
865 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
866 struct snd_timer **rtimer)
868 struct snd_timer *timer;
869 int err;
870 static struct snd_device_ops ops = {
871 .dev_free = snd_timer_dev_free,
872 .dev_register = snd_timer_dev_register,
873 .dev_disconnect = snd_timer_dev_disconnect,
876 if (snd_BUG_ON(!tid))
877 return -EINVAL;
878 if (rtimer)
879 *rtimer = NULL;
880 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
881 if (!timer)
882 return -ENOMEM;
883 timer->tmr_class = tid->dev_class;
884 timer->card = card;
885 timer->tmr_device = tid->device;
886 timer->tmr_subdevice = tid->subdevice;
887 if (id)
888 strlcpy(timer->id, id, sizeof(timer->id));
889 timer->sticks = 1;
890 INIT_LIST_HEAD(&timer->device_list);
891 INIT_LIST_HEAD(&timer->open_list_head);
892 INIT_LIST_HEAD(&timer->active_list_head);
893 INIT_LIST_HEAD(&timer->ack_list_head);
894 INIT_LIST_HEAD(&timer->sack_list_head);
895 spin_lock_init(&timer->lock);
896 tasklet_init(&timer->task_queue, snd_timer_tasklet,
897 (unsigned long)timer);
898 timer->max_instances = 1000; /* default limit per timer */
899 if (card != NULL) {
900 timer->module = card->module;
901 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
902 if (err < 0) {
903 snd_timer_free(timer);
904 return err;
907 if (rtimer)
908 *rtimer = timer;
909 return 0;
911 EXPORT_SYMBOL(snd_timer_new);
913 static int snd_timer_free(struct snd_timer *timer)
915 if (!timer)
916 return 0;
918 mutex_lock(&register_mutex);
919 if (! list_empty(&timer->open_list_head)) {
920 struct list_head *p, *n;
921 struct snd_timer_instance *ti;
922 pr_warn("ALSA: timer %p is busy?\n", timer);
923 list_for_each_safe(p, n, &timer->open_list_head) {
924 list_del_init(p);
925 ti = list_entry(p, struct snd_timer_instance, open_list);
926 ti->timer = NULL;
929 list_del(&timer->device_list);
930 mutex_unlock(&register_mutex);
932 if (timer->private_free)
933 timer->private_free(timer);
934 kfree(timer);
935 return 0;
938 static int snd_timer_dev_free(struct snd_device *device)
940 struct snd_timer *timer = device->device_data;
941 return snd_timer_free(timer);
944 static int snd_timer_dev_register(struct snd_device *dev)
946 struct snd_timer *timer = dev->device_data;
947 struct snd_timer *timer1;
949 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
950 return -ENXIO;
951 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
952 !timer->hw.resolution && timer->hw.c_resolution == NULL)
953 return -EINVAL;
955 mutex_lock(&register_mutex);
956 list_for_each_entry(timer1, &snd_timer_list, device_list) {
957 if (timer1->tmr_class > timer->tmr_class)
958 break;
959 if (timer1->tmr_class < timer->tmr_class)
960 continue;
961 if (timer1->card && timer->card) {
962 if (timer1->card->number > timer->card->number)
963 break;
964 if (timer1->card->number < timer->card->number)
965 continue;
967 if (timer1->tmr_device > timer->tmr_device)
968 break;
969 if (timer1->tmr_device < timer->tmr_device)
970 continue;
971 if (timer1->tmr_subdevice > timer->tmr_subdevice)
972 break;
973 if (timer1->tmr_subdevice < timer->tmr_subdevice)
974 continue;
975 /* conflicts.. */
976 mutex_unlock(&register_mutex);
977 return -EBUSY;
979 list_add_tail(&timer->device_list, &timer1->device_list);
980 mutex_unlock(&register_mutex);
981 return 0;
984 static int snd_timer_dev_disconnect(struct snd_device *device)
986 struct snd_timer *timer = device->device_data;
987 struct snd_timer_instance *ti;
989 mutex_lock(&register_mutex);
990 list_del_init(&timer->device_list);
991 /* wake up pending sleepers */
992 list_for_each_entry(ti, &timer->open_list_head, open_list) {
993 if (ti->disconnect)
994 ti->disconnect(ti);
996 mutex_unlock(&register_mutex);
997 return 0;
1000 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
1002 unsigned long flags;
1003 unsigned long resolution = 0;
1004 struct snd_timer_instance *ti, *ts;
1006 if (timer->card && timer->card->shutdown)
1007 return;
1008 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1009 return;
1010 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1011 event > SNDRV_TIMER_EVENT_MRESUME))
1012 return;
1013 spin_lock_irqsave(&timer->lock, flags);
1014 if (event == SNDRV_TIMER_EVENT_MSTART ||
1015 event == SNDRV_TIMER_EVENT_MCONTINUE ||
1016 event == SNDRV_TIMER_EVENT_MRESUME) {
1017 if (timer->hw.c_resolution)
1018 resolution = timer->hw.c_resolution(timer);
1019 else
1020 resolution = timer->hw.resolution;
1022 list_for_each_entry(ti, &timer->active_list_head, active_list) {
1023 if (ti->ccallback)
1024 ti->ccallback(ti, event, tstamp, resolution);
1025 list_for_each_entry(ts, &ti->slave_active_head, active_list)
1026 if (ts->ccallback)
1027 ts->ccallback(ts, event, tstamp, resolution);
1029 spin_unlock_irqrestore(&timer->lock, flags);
1031 EXPORT_SYMBOL(snd_timer_notify);
1034 * exported functions for global timers
1036 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1038 struct snd_timer_id tid;
1040 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1041 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1042 tid.card = -1;
1043 tid.device = device;
1044 tid.subdevice = 0;
1045 return snd_timer_new(NULL, id, &tid, rtimer);
1047 EXPORT_SYMBOL(snd_timer_global_new);
1049 int snd_timer_global_free(struct snd_timer *timer)
1051 return snd_timer_free(timer);
1053 EXPORT_SYMBOL(snd_timer_global_free);
1055 int snd_timer_global_register(struct snd_timer *timer)
1057 struct snd_device dev;
1059 memset(&dev, 0, sizeof(dev));
1060 dev.device_data = timer;
1061 return snd_timer_dev_register(&dev);
1063 EXPORT_SYMBOL(snd_timer_global_register);
1066 * System timer
1069 struct snd_timer_system_private {
1070 struct timer_list tlist;
1071 unsigned long last_expires;
1072 unsigned long last_jiffies;
1073 unsigned long correction;
1076 static void snd_timer_s_function(unsigned long data)
1078 struct snd_timer *timer = (struct snd_timer *)data;
1079 struct snd_timer_system_private *priv = timer->private_data;
1080 unsigned long jiff = jiffies;
1081 if (time_after(jiff, priv->last_expires))
1082 priv->correction += (long)jiff - (long)priv->last_expires;
1083 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1086 static int snd_timer_s_start(struct snd_timer * timer)
1088 struct snd_timer_system_private *priv;
1089 unsigned long njiff;
1091 priv = (struct snd_timer_system_private *) timer->private_data;
1092 njiff = (priv->last_jiffies = jiffies);
1093 if (priv->correction > timer->sticks - 1) {
1094 priv->correction -= timer->sticks - 1;
1095 njiff++;
1096 } else {
1097 njiff += timer->sticks - priv->correction;
1098 priv->correction = 0;
1100 priv->last_expires = njiff;
1101 mod_timer(&priv->tlist, njiff);
1102 return 0;
1105 static int snd_timer_s_stop(struct snd_timer * timer)
1107 struct snd_timer_system_private *priv;
1108 unsigned long jiff;
1110 priv = (struct snd_timer_system_private *) timer->private_data;
1111 del_timer(&priv->tlist);
1112 jiff = jiffies;
1113 if (time_before(jiff, priv->last_expires))
1114 timer->sticks = priv->last_expires - jiff;
1115 else
1116 timer->sticks = 1;
1117 priv->correction = 0;
1118 return 0;
1121 static int snd_timer_s_close(struct snd_timer *timer)
1123 struct snd_timer_system_private *priv;
1125 priv = (struct snd_timer_system_private *)timer->private_data;
1126 del_timer_sync(&priv->tlist);
1127 return 0;
1130 static struct snd_timer_hardware snd_timer_system =
1132 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1133 .resolution = 1000000000L / HZ,
1134 .ticks = 10000000L,
1135 .close = snd_timer_s_close,
1136 .start = snd_timer_s_start,
1137 .stop = snd_timer_s_stop
1140 static void snd_timer_free_system(struct snd_timer *timer)
1142 kfree(timer->private_data);
1145 static int snd_timer_register_system(void)
1147 struct snd_timer *timer;
1148 struct snd_timer_system_private *priv;
1149 int err;
1151 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1152 if (err < 0)
1153 return err;
1154 strcpy(timer->name, "system timer");
1155 timer->hw = snd_timer_system;
1156 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1157 if (priv == NULL) {
1158 snd_timer_free(timer);
1159 return -ENOMEM;
1161 setup_timer(&priv->tlist, snd_timer_s_function, (unsigned long) timer);
1162 timer->private_data = priv;
1163 timer->private_free = snd_timer_free_system;
1164 return snd_timer_global_register(timer);
1167 #ifdef CONFIG_SND_PROC_FS
1169 * Info interface
1172 static void snd_timer_proc_read(struct snd_info_entry *entry,
1173 struct snd_info_buffer *buffer)
1175 struct snd_timer *timer;
1176 struct snd_timer_instance *ti;
1178 mutex_lock(&register_mutex);
1179 list_for_each_entry(timer, &snd_timer_list, device_list) {
1180 if (timer->card && timer->card->shutdown)
1181 continue;
1182 switch (timer->tmr_class) {
1183 case SNDRV_TIMER_CLASS_GLOBAL:
1184 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1185 break;
1186 case SNDRV_TIMER_CLASS_CARD:
1187 snd_iprintf(buffer, "C%i-%i: ",
1188 timer->card->number, timer->tmr_device);
1189 break;
1190 case SNDRV_TIMER_CLASS_PCM:
1191 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1192 timer->tmr_device, timer->tmr_subdevice);
1193 break;
1194 default:
1195 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1196 timer->card ? timer->card->number : -1,
1197 timer->tmr_device, timer->tmr_subdevice);
1199 snd_iprintf(buffer, "%s :", timer->name);
1200 if (timer->hw.resolution)
1201 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1202 timer->hw.resolution / 1000,
1203 timer->hw.resolution % 1000,
1204 timer->hw.ticks);
1205 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1206 snd_iprintf(buffer, " SLAVE");
1207 snd_iprintf(buffer, "\n");
1208 list_for_each_entry(ti, &timer->open_list_head, open_list)
1209 snd_iprintf(buffer, " Client %s : %s\n",
1210 ti->owner ? ti->owner : "unknown",
1211 ti->flags & (SNDRV_TIMER_IFLG_START |
1212 SNDRV_TIMER_IFLG_RUNNING)
1213 ? "running" : "stopped");
1215 mutex_unlock(&register_mutex);
1218 static struct snd_info_entry *snd_timer_proc_entry;
1220 static void __init snd_timer_proc_init(void)
1222 struct snd_info_entry *entry;
1224 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1225 if (entry != NULL) {
1226 entry->c.text.read = snd_timer_proc_read;
1227 if (snd_info_register(entry) < 0) {
1228 snd_info_free_entry(entry);
1229 entry = NULL;
1232 snd_timer_proc_entry = entry;
1235 static void __exit snd_timer_proc_done(void)
1237 snd_info_free_entry(snd_timer_proc_entry);
1239 #else /* !CONFIG_SND_PROC_FS */
1240 #define snd_timer_proc_init()
1241 #define snd_timer_proc_done()
1242 #endif
1245 * USER SPACE interface
1248 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1249 unsigned long resolution,
1250 unsigned long ticks)
1252 struct snd_timer_user *tu = timeri->callback_data;
1253 struct snd_timer_read *r;
1254 int prev;
1256 spin_lock(&tu->qlock);
1257 if (tu->qused > 0) {
1258 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1259 r = &tu->queue[prev];
1260 if (r->resolution == resolution) {
1261 r->ticks += ticks;
1262 goto __wake;
1265 if (tu->qused >= tu->queue_size) {
1266 tu->overrun++;
1267 } else {
1268 r = &tu->queue[tu->qtail++];
1269 tu->qtail %= tu->queue_size;
1270 r->resolution = resolution;
1271 r->ticks = ticks;
1272 tu->qused++;
1274 __wake:
1275 spin_unlock(&tu->qlock);
1276 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1277 wake_up(&tu->qchange_sleep);
1280 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1281 struct snd_timer_tread *tread)
1283 if (tu->qused >= tu->queue_size) {
1284 tu->overrun++;
1285 } else {
1286 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1287 tu->qtail %= tu->queue_size;
1288 tu->qused++;
1292 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1293 int event,
1294 struct timespec *tstamp,
1295 unsigned long resolution)
1297 struct snd_timer_user *tu = timeri->callback_data;
1298 struct snd_timer_tread r1;
1299 unsigned long flags;
1301 if (event >= SNDRV_TIMER_EVENT_START &&
1302 event <= SNDRV_TIMER_EVENT_PAUSE)
1303 tu->tstamp = *tstamp;
1304 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1305 return;
1306 memset(&r1, 0, sizeof(r1));
1307 r1.event = event;
1308 r1.tstamp = *tstamp;
1309 r1.val = resolution;
1310 spin_lock_irqsave(&tu->qlock, flags);
1311 snd_timer_user_append_to_tqueue(tu, &r1);
1312 spin_unlock_irqrestore(&tu->qlock, flags);
1313 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1314 wake_up(&tu->qchange_sleep);
1317 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1319 struct snd_timer_user *tu = timeri->callback_data;
1321 tu->disconnected = true;
1322 wake_up(&tu->qchange_sleep);
1325 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1326 unsigned long resolution,
1327 unsigned long ticks)
1329 struct snd_timer_user *tu = timeri->callback_data;
1330 struct snd_timer_tread *r, r1;
1331 struct timespec tstamp;
1332 int prev, append = 0;
1334 memset(&tstamp, 0, sizeof(tstamp));
1335 spin_lock(&tu->qlock);
1336 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1337 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1338 spin_unlock(&tu->qlock);
1339 return;
1341 if (tu->last_resolution != resolution || ticks > 0) {
1342 if (timer_tstamp_monotonic)
1343 ktime_get_ts(&tstamp);
1344 else
1345 getnstimeofday(&tstamp);
1347 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1348 tu->last_resolution != resolution) {
1349 memset(&r1, 0, sizeof(r1));
1350 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1351 r1.tstamp = tstamp;
1352 r1.val = resolution;
1353 snd_timer_user_append_to_tqueue(tu, &r1);
1354 tu->last_resolution = resolution;
1355 append++;
1357 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1358 goto __wake;
1359 if (ticks == 0)
1360 goto __wake;
1361 if (tu->qused > 0) {
1362 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1363 r = &tu->tqueue[prev];
1364 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1365 r->tstamp = tstamp;
1366 r->val += ticks;
1367 append++;
1368 goto __wake;
1371 r1.event = SNDRV_TIMER_EVENT_TICK;
1372 r1.tstamp = tstamp;
1373 r1.val = ticks;
1374 snd_timer_user_append_to_tqueue(tu, &r1);
1375 append++;
1376 __wake:
1377 spin_unlock(&tu->qlock);
1378 if (append == 0)
1379 return;
1380 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1381 wake_up(&tu->qchange_sleep);
1384 static int snd_timer_user_open(struct inode *inode, struct file *file)
1386 struct snd_timer_user *tu;
1387 int err;
1389 err = nonseekable_open(inode, file);
1390 if (err < 0)
1391 return err;
1393 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1394 if (tu == NULL)
1395 return -ENOMEM;
1396 spin_lock_init(&tu->qlock);
1397 init_waitqueue_head(&tu->qchange_sleep);
1398 mutex_init(&tu->ioctl_lock);
1399 tu->ticks = 1;
1400 tu->queue_size = 128;
1401 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1402 GFP_KERNEL);
1403 if (tu->queue == NULL) {
1404 kfree(tu);
1405 return -ENOMEM;
1407 file->private_data = tu;
1408 return 0;
1411 static int snd_timer_user_release(struct inode *inode, struct file *file)
1413 struct snd_timer_user *tu;
1415 if (file->private_data) {
1416 tu = file->private_data;
1417 file->private_data = NULL;
1418 mutex_lock(&tu->ioctl_lock);
1419 if (tu->timeri)
1420 snd_timer_close(tu->timeri);
1421 mutex_unlock(&tu->ioctl_lock);
1422 kfree(tu->queue);
1423 kfree(tu->tqueue);
1424 kfree(tu);
1426 return 0;
1429 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1431 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1432 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1433 id->card = -1;
1434 id->device = -1;
1435 id->subdevice = -1;
1438 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1440 id->dev_class = timer->tmr_class;
1441 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1442 id->card = timer->card ? timer->card->number : -1;
1443 id->device = timer->tmr_device;
1444 id->subdevice = timer->tmr_subdevice;
1447 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1449 struct snd_timer_id id;
1450 struct snd_timer *timer;
1451 struct list_head *p;
1453 if (copy_from_user(&id, _tid, sizeof(id)))
1454 return -EFAULT;
1455 mutex_lock(&register_mutex);
1456 if (id.dev_class < 0) { /* first item */
1457 if (list_empty(&snd_timer_list))
1458 snd_timer_user_zero_id(&id);
1459 else {
1460 timer = list_entry(snd_timer_list.next,
1461 struct snd_timer, device_list);
1462 snd_timer_user_copy_id(&id, timer);
1464 } else {
1465 switch (id.dev_class) {
1466 case SNDRV_TIMER_CLASS_GLOBAL:
1467 id.device = id.device < 0 ? 0 : id.device + 1;
1468 list_for_each(p, &snd_timer_list) {
1469 timer = list_entry(p, struct snd_timer, device_list);
1470 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1471 snd_timer_user_copy_id(&id, timer);
1472 break;
1474 if (timer->tmr_device >= id.device) {
1475 snd_timer_user_copy_id(&id, timer);
1476 break;
1479 if (p == &snd_timer_list)
1480 snd_timer_user_zero_id(&id);
1481 break;
1482 case SNDRV_TIMER_CLASS_CARD:
1483 case SNDRV_TIMER_CLASS_PCM:
1484 if (id.card < 0) {
1485 id.card = 0;
1486 } else {
1487 if (id.card < 0) {
1488 id.card = 0;
1489 } else {
1490 if (id.device < 0) {
1491 id.device = 0;
1492 } else {
1493 if (id.subdevice < 0) {
1494 id.subdevice = 0;
1495 } else {
1496 id.subdevice++;
1501 list_for_each(p, &snd_timer_list) {
1502 timer = list_entry(p, struct snd_timer, device_list);
1503 if (timer->tmr_class > id.dev_class) {
1504 snd_timer_user_copy_id(&id, timer);
1505 break;
1507 if (timer->tmr_class < id.dev_class)
1508 continue;
1509 if (timer->card->number > id.card) {
1510 snd_timer_user_copy_id(&id, timer);
1511 break;
1513 if (timer->card->number < id.card)
1514 continue;
1515 if (timer->tmr_device > id.device) {
1516 snd_timer_user_copy_id(&id, timer);
1517 break;
1519 if (timer->tmr_device < id.device)
1520 continue;
1521 if (timer->tmr_subdevice > id.subdevice) {
1522 snd_timer_user_copy_id(&id, timer);
1523 break;
1525 if (timer->tmr_subdevice < id.subdevice)
1526 continue;
1527 snd_timer_user_copy_id(&id, timer);
1528 break;
1530 if (p == &snd_timer_list)
1531 snd_timer_user_zero_id(&id);
1532 break;
1533 default:
1534 snd_timer_user_zero_id(&id);
1537 mutex_unlock(&register_mutex);
1538 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1539 return -EFAULT;
1540 return 0;
1543 static int snd_timer_user_ginfo(struct file *file,
1544 struct snd_timer_ginfo __user *_ginfo)
1546 struct snd_timer_ginfo *ginfo;
1547 struct snd_timer_id tid;
1548 struct snd_timer *t;
1549 struct list_head *p;
1550 int err = 0;
1552 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1553 if (IS_ERR(ginfo))
1554 return PTR_ERR(ginfo);
1556 tid = ginfo->tid;
1557 memset(ginfo, 0, sizeof(*ginfo));
1558 ginfo->tid = tid;
1559 mutex_lock(&register_mutex);
1560 t = snd_timer_find(&tid);
1561 if (t != NULL) {
1562 ginfo->card = t->card ? t->card->number : -1;
1563 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1564 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1565 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1566 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1567 ginfo->resolution = t->hw.resolution;
1568 if (t->hw.resolution_min > 0) {
1569 ginfo->resolution_min = t->hw.resolution_min;
1570 ginfo->resolution_max = t->hw.resolution_max;
1572 list_for_each(p, &t->open_list_head) {
1573 ginfo->clients++;
1575 } else {
1576 err = -ENODEV;
1578 mutex_unlock(&register_mutex);
1579 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1580 err = -EFAULT;
1581 kfree(ginfo);
1582 return err;
1585 static int timer_set_gparams(struct snd_timer_gparams *gparams)
1587 struct snd_timer *t;
1588 int err;
1590 mutex_lock(&register_mutex);
1591 t = snd_timer_find(&gparams->tid);
1592 if (!t) {
1593 err = -ENODEV;
1594 goto _error;
1596 if (!list_empty(&t->open_list_head)) {
1597 err = -EBUSY;
1598 goto _error;
1600 if (!t->hw.set_period) {
1601 err = -ENOSYS;
1602 goto _error;
1604 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
1605 _error:
1606 mutex_unlock(&register_mutex);
1607 return err;
1610 static int snd_timer_user_gparams(struct file *file,
1611 struct snd_timer_gparams __user *_gparams)
1613 struct snd_timer_gparams gparams;
1615 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1616 return -EFAULT;
1617 return timer_set_gparams(&gparams);
1620 static int snd_timer_user_gstatus(struct file *file,
1621 struct snd_timer_gstatus __user *_gstatus)
1623 struct snd_timer_gstatus gstatus;
1624 struct snd_timer_id tid;
1625 struct snd_timer *t;
1626 int err = 0;
1628 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1629 return -EFAULT;
1630 tid = gstatus.tid;
1631 memset(&gstatus, 0, sizeof(gstatus));
1632 gstatus.tid = tid;
1633 mutex_lock(&register_mutex);
1634 t = snd_timer_find(&tid);
1635 if (t != NULL) {
1636 if (t->hw.c_resolution)
1637 gstatus.resolution = t->hw.c_resolution(t);
1638 else
1639 gstatus.resolution = t->hw.resolution;
1640 if (t->hw.precise_resolution) {
1641 t->hw.precise_resolution(t, &gstatus.resolution_num,
1642 &gstatus.resolution_den);
1643 } else {
1644 gstatus.resolution_num = gstatus.resolution;
1645 gstatus.resolution_den = 1000000000uL;
1647 } else {
1648 err = -ENODEV;
1650 mutex_unlock(&register_mutex);
1651 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1652 err = -EFAULT;
1653 return err;
1656 static int snd_timer_user_tselect(struct file *file,
1657 struct snd_timer_select __user *_tselect)
1659 struct snd_timer_user *tu;
1660 struct snd_timer_select tselect;
1661 char str[32];
1662 int err = 0;
1664 tu = file->private_data;
1665 if (tu->timeri) {
1666 snd_timer_close(tu->timeri);
1667 tu->timeri = NULL;
1669 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1670 err = -EFAULT;
1671 goto __err;
1673 sprintf(str, "application %i", current->pid);
1674 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1675 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1676 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1677 if (err < 0)
1678 goto __err;
1680 tu->qhead = tu->qtail = tu->qused = 0;
1681 kfree(tu->queue);
1682 tu->queue = NULL;
1683 kfree(tu->tqueue);
1684 tu->tqueue = NULL;
1685 if (tu->tread) {
1686 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1687 GFP_KERNEL);
1688 if (tu->tqueue == NULL)
1689 err = -ENOMEM;
1690 } else {
1691 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1692 GFP_KERNEL);
1693 if (tu->queue == NULL)
1694 err = -ENOMEM;
1697 if (err < 0) {
1698 snd_timer_close(tu->timeri);
1699 tu->timeri = NULL;
1700 } else {
1701 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1702 tu->timeri->callback = tu->tread
1703 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1704 tu->timeri->ccallback = snd_timer_user_ccallback;
1705 tu->timeri->callback_data = (void *)tu;
1706 tu->timeri->disconnect = snd_timer_user_disconnect;
1709 __err:
1710 return err;
1713 static int snd_timer_user_info(struct file *file,
1714 struct snd_timer_info __user *_info)
1716 struct snd_timer_user *tu;
1717 struct snd_timer_info *info;
1718 struct snd_timer *t;
1719 int err = 0;
1721 tu = file->private_data;
1722 if (!tu->timeri)
1723 return -EBADFD;
1724 t = tu->timeri->timer;
1725 if (!t)
1726 return -EBADFD;
1728 info = kzalloc(sizeof(*info), GFP_KERNEL);
1729 if (! info)
1730 return -ENOMEM;
1731 info->card = t->card ? t->card->number : -1;
1732 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1733 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1734 strlcpy(info->id, t->id, sizeof(info->id));
1735 strlcpy(info->name, t->name, sizeof(info->name));
1736 info->resolution = t->hw.resolution;
1737 if (copy_to_user(_info, info, sizeof(*_info)))
1738 err = -EFAULT;
1739 kfree(info);
1740 return err;
1743 static int snd_timer_user_params(struct file *file,
1744 struct snd_timer_params __user *_params)
1746 struct snd_timer_user *tu;
1747 struct snd_timer_params params;
1748 struct snd_timer *t;
1749 struct snd_timer_read *tr;
1750 struct snd_timer_tread *ttr;
1751 int err;
1753 tu = file->private_data;
1754 if (!tu->timeri)
1755 return -EBADFD;
1756 t = tu->timeri->timer;
1757 if (!t)
1758 return -EBADFD;
1759 if (copy_from_user(&params, _params, sizeof(params)))
1760 return -EFAULT;
1761 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1762 u64 resolution;
1764 if (params.ticks < 1) {
1765 err = -EINVAL;
1766 goto _end;
1769 /* Don't allow resolution less than 1ms */
1770 resolution = snd_timer_resolution(tu->timeri);
1771 resolution *= params.ticks;
1772 if (resolution < 1000000) {
1773 err = -EINVAL;
1774 goto _end;
1777 if (params.queue_size > 0 &&
1778 (params.queue_size < 32 || params.queue_size > 1024)) {
1779 err = -EINVAL;
1780 goto _end;
1782 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1783 (1<<SNDRV_TIMER_EVENT_TICK)|
1784 (1<<SNDRV_TIMER_EVENT_START)|
1785 (1<<SNDRV_TIMER_EVENT_STOP)|
1786 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1787 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1788 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1789 (1<<SNDRV_TIMER_EVENT_RESUME)|
1790 (1<<SNDRV_TIMER_EVENT_MSTART)|
1791 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1792 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1793 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1794 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1795 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1796 err = -EINVAL;
1797 goto _end;
1799 snd_timer_stop(tu->timeri);
1800 spin_lock_irq(&t->lock);
1801 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1802 SNDRV_TIMER_IFLG_EXCLUSIVE|
1803 SNDRV_TIMER_IFLG_EARLY_EVENT);
1804 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1805 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1806 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1807 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1808 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1809 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1810 spin_unlock_irq(&t->lock);
1811 if (params.queue_size > 0 &&
1812 (unsigned int)tu->queue_size != params.queue_size) {
1813 if (tu->tread) {
1814 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1815 GFP_KERNEL);
1816 if (ttr) {
1817 kfree(tu->tqueue);
1818 tu->queue_size = params.queue_size;
1819 tu->tqueue = ttr;
1821 } else {
1822 tr = kmalloc(params.queue_size * sizeof(*tr),
1823 GFP_KERNEL);
1824 if (tr) {
1825 kfree(tu->queue);
1826 tu->queue_size = params.queue_size;
1827 tu->queue = tr;
1831 tu->qhead = tu->qtail = tu->qused = 0;
1832 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1833 if (tu->tread) {
1834 struct snd_timer_tread tread;
1835 memset(&tread, 0, sizeof(tread));
1836 tread.event = SNDRV_TIMER_EVENT_EARLY;
1837 tread.tstamp.tv_sec = 0;
1838 tread.tstamp.tv_nsec = 0;
1839 tread.val = 0;
1840 snd_timer_user_append_to_tqueue(tu, &tread);
1841 } else {
1842 struct snd_timer_read *r = &tu->queue[0];
1843 r->resolution = 0;
1844 r->ticks = 0;
1845 tu->qused++;
1846 tu->qtail++;
1849 tu->filter = params.filter;
1850 tu->ticks = params.ticks;
1851 err = 0;
1852 _end:
1853 if (copy_to_user(_params, &params, sizeof(params)))
1854 return -EFAULT;
1855 return err;
1858 static int snd_timer_user_status(struct file *file,
1859 struct snd_timer_status __user *_status)
1861 struct snd_timer_user *tu;
1862 struct snd_timer_status status;
1864 tu = file->private_data;
1865 if (!tu->timeri)
1866 return -EBADFD;
1867 memset(&status, 0, sizeof(status));
1868 status.tstamp = tu->tstamp;
1869 status.resolution = snd_timer_resolution(tu->timeri);
1870 status.lost = tu->timeri->lost;
1871 status.overrun = tu->overrun;
1872 spin_lock_irq(&tu->qlock);
1873 status.queue = tu->qused;
1874 spin_unlock_irq(&tu->qlock);
1875 if (copy_to_user(_status, &status, sizeof(status)))
1876 return -EFAULT;
1877 return 0;
1880 static int snd_timer_user_start(struct file *file)
1882 int err;
1883 struct snd_timer_user *tu;
1885 tu = file->private_data;
1886 if (!tu->timeri)
1887 return -EBADFD;
1888 snd_timer_stop(tu->timeri);
1889 tu->timeri->lost = 0;
1890 tu->last_resolution = 0;
1891 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1894 static int snd_timer_user_stop(struct file *file)
1896 int err;
1897 struct snd_timer_user *tu;
1899 tu = file->private_data;
1900 if (!tu->timeri)
1901 return -EBADFD;
1902 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1905 static int snd_timer_user_continue(struct file *file)
1907 int err;
1908 struct snd_timer_user *tu;
1910 tu = file->private_data;
1911 if (!tu->timeri)
1912 return -EBADFD;
1913 /* start timer instead of continue if it's not used before */
1914 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1915 return snd_timer_user_start(file);
1916 tu->timeri->lost = 0;
1917 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1920 static int snd_timer_user_pause(struct file *file)
1922 int err;
1923 struct snd_timer_user *tu;
1925 tu = file->private_data;
1926 if (!tu->timeri)
1927 return -EBADFD;
1928 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1931 enum {
1932 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1933 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1934 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1935 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1938 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1939 unsigned long arg)
1941 struct snd_timer_user *tu;
1942 void __user *argp = (void __user *)arg;
1943 int __user *p = argp;
1945 tu = file->private_data;
1946 switch (cmd) {
1947 case SNDRV_TIMER_IOCTL_PVERSION:
1948 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1949 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1950 return snd_timer_user_next_device(argp);
1951 case SNDRV_TIMER_IOCTL_TREAD:
1953 int xarg;
1955 if (tu->timeri) /* too late */
1956 return -EBUSY;
1957 if (get_user(xarg, p))
1958 return -EFAULT;
1959 tu->tread = xarg ? 1 : 0;
1960 return 0;
1962 case SNDRV_TIMER_IOCTL_GINFO:
1963 return snd_timer_user_ginfo(file, argp);
1964 case SNDRV_TIMER_IOCTL_GPARAMS:
1965 return snd_timer_user_gparams(file, argp);
1966 case SNDRV_TIMER_IOCTL_GSTATUS:
1967 return snd_timer_user_gstatus(file, argp);
1968 case SNDRV_TIMER_IOCTL_SELECT:
1969 return snd_timer_user_tselect(file, argp);
1970 case SNDRV_TIMER_IOCTL_INFO:
1971 return snd_timer_user_info(file, argp);
1972 case SNDRV_TIMER_IOCTL_PARAMS:
1973 return snd_timer_user_params(file, argp);
1974 case SNDRV_TIMER_IOCTL_STATUS:
1975 return snd_timer_user_status(file, argp);
1976 case SNDRV_TIMER_IOCTL_START:
1977 case SNDRV_TIMER_IOCTL_START_OLD:
1978 return snd_timer_user_start(file);
1979 case SNDRV_TIMER_IOCTL_STOP:
1980 case SNDRV_TIMER_IOCTL_STOP_OLD:
1981 return snd_timer_user_stop(file);
1982 case SNDRV_TIMER_IOCTL_CONTINUE:
1983 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1984 return snd_timer_user_continue(file);
1985 case SNDRV_TIMER_IOCTL_PAUSE:
1986 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1987 return snd_timer_user_pause(file);
1989 return -ENOTTY;
1992 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1993 unsigned long arg)
1995 struct snd_timer_user *tu = file->private_data;
1996 long ret;
1998 mutex_lock(&tu->ioctl_lock);
1999 ret = __snd_timer_user_ioctl(file, cmd, arg);
2000 mutex_unlock(&tu->ioctl_lock);
2001 return ret;
2004 static int snd_timer_user_fasync(int fd, struct file * file, int on)
2006 struct snd_timer_user *tu;
2008 tu = file->private_data;
2009 return fasync_helper(fd, file, on, &tu->fasync);
2012 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2013 size_t count, loff_t *offset)
2015 struct snd_timer_user *tu;
2016 long result = 0, unit;
2017 int qhead;
2018 int err = 0;
2020 tu = file->private_data;
2021 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
2022 mutex_lock(&tu->ioctl_lock);
2023 spin_lock_irq(&tu->qlock);
2024 while ((long)count - result >= unit) {
2025 while (!tu->qused) {
2026 wait_queue_t wait;
2028 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2029 err = -EAGAIN;
2030 goto _error;
2033 set_current_state(TASK_INTERRUPTIBLE);
2034 init_waitqueue_entry(&wait, current);
2035 add_wait_queue(&tu->qchange_sleep, &wait);
2037 spin_unlock_irq(&tu->qlock);
2038 mutex_unlock(&tu->ioctl_lock);
2039 schedule();
2040 mutex_lock(&tu->ioctl_lock);
2041 spin_lock_irq(&tu->qlock);
2043 remove_wait_queue(&tu->qchange_sleep, &wait);
2045 if (tu->disconnected) {
2046 err = -ENODEV;
2047 goto _error;
2049 if (signal_pending(current)) {
2050 err = -ERESTARTSYS;
2051 goto _error;
2055 qhead = tu->qhead++;
2056 tu->qhead %= tu->queue_size;
2057 tu->qused--;
2058 spin_unlock_irq(&tu->qlock);
2060 if (tu->tread) {
2061 if (copy_to_user(buffer, &tu->tqueue[qhead],
2062 sizeof(struct snd_timer_tread)))
2063 err = -EFAULT;
2064 } else {
2065 if (copy_to_user(buffer, &tu->queue[qhead],
2066 sizeof(struct snd_timer_read)))
2067 err = -EFAULT;
2070 spin_lock_irq(&tu->qlock);
2071 if (err < 0)
2072 goto _error;
2073 result += unit;
2074 buffer += unit;
2076 _error:
2077 spin_unlock_irq(&tu->qlock);
2078 mutex_unlock(&tu->ioctl_lock);
2079 return result > 0 ? result : err;
2082 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
2084 unsigned int mask;
2085 struct snd_timer_user *tu;
2087 tu = file->private_data;
2089 poll_wait(file, &tu->qchange_sleep, wait);
2091 mask = 0;
2092 if (tu->qused)
2093 mask |= POLLIN | POLLRDNORM;
2094 if (tu->disconnected)
2095 mask |= POLLERR;
2097 return mask;
2100 #ifdef CONFIG_COMPAT
2101 #include "timer_compat.c"
2102 #else
2103 #define snd_timer_user_ioctl_compat NULL
2104 #endif
2106 static const struct file_operations snd_timer_f_ops =
2108 .owner = THIS_MODULE,
2109 .read = snd_timer_user_read,
2110 .open = snd_timer_user_open,
2111 .release = snd_timer_user_release,
2112 .llseek = no_llseek,
2113 .poll = snd_timer_user_poll,
2114 .unlocked_ioctl = snd_timer_user_ioctl,
2115 .compat_ioctl = snd_timer_user_ioctl_compat,
2116 .fasync = snd_timer_user_fasync,
2119 /* unregister the system timer */
2120 static void snd_timer_free_all(void)
2122 struct snd_timer *timer, *n;
2124 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2125 snd_timer_free(timer);
2128 static struct device timer_dev;
2131 * ENTRY functions
2134 static int __init alsa_timer_init(void)
2136 int err;
2138 snd_device_initialize(&timer_dev, NULL);
2139 dev_set_name(&timer_dev, "timer");
2141 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2142 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2143 "system timer");
2144 #endif
2146 err = snd_timer_register_system();
2147 if (err < 0) {
2148 pr_err("ALSA: unable to register system timer (%i)\n", err);
2149 put_device(&timer_dev);
2150 return err;
2153 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2154 &snd_timer_f_ops, NULL, &timer_dev);
2155 if (err < 0) {
2156 pr_err("ALSA: unable to register timer device (%i)\n", err);
2157 snd_timer_free_all();
2158 put_device(&timer_dev);
2159 return err;
2162 snd_timer_proc_init();
2163 return 0;
2166 static void __exit alsa_timer_exit(void)
2168 snd_unregister_device(&timer_dev);
2169 snd_timer_free_all();
2170 put_device(&timer_dev);
2171 snd_timer_proc_done();
2172 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2173 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2174 #endif
2177 module_init(alsa_timer_init)
2178 module_exit(alsa_timer_exit)