ALSA: timer: Fix mutex deadlock at releasing card
[linux/fpc-iii.git] / sound / core / timer.c
blob19d90aa082184a500b3886ee3edab6ab0629f91c
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,
243 struct device **card_devp_to_put);
246 * open a timer instance
247 * when opening a master, the slave id must be here given.
249 int snd_timer_open(struct snd_timer_instance **ti,
250 char *owner, struct snd_timer_id *tid,
251 unsigned int slave_id)
253 struct snd_timer *timer;
254 struct snd_timer_instance *timeri = NULL;
255 struct device *card_dev_to_put = NULL;
256 int err;
258 mutex_lock(&register_mutex);
259 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
260 /* open a slave instance */
261 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
262 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
263 pr_debug("ALSA: timer: invalid slave class %i\n",
264 tid->dev_sclass);
265 err = -EINVAL;
266 goto unlock;
268 timeri = snd_timer_instance_new(owner, NULL);
269 if (!timeri) {
270 err = -ENOMEM;
271 goto unlock;
273 timeri->slave_class = tid->dev_sclass;
274 timeri->slave_id = tid->device;
275 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
276 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
277 err = snd_timer_check_slave(timeri);
278 if (err < 0) {
279 snd_timer_close_locked(timeri, &card_dev_to_put);
280 timeri = NULL;
282 goto unlock;
285 /* open a master instance */
286 timer = snd_timer_find(tid);
287 #ifdef CONFIG_MODULES
288 if (!timer) {
289 mutex_unlock(&register_mutex);
290 snd_timer_request(tid);
291 mutex_lock(&register_mutex);
292 timer = snd_timer_find(tid);
294 #endif
295 if (!timer) {
296 err = -ENODEV;
297 goto unlock;
299 if (!list_empty(&timer->open_list_head)) {
300 timeri = list_entry(timer->open_list_head.next,
301 struct snd_timer_instance, open_list);
302 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
303 err = -EBUSY;
304 timeri = NULL;
305 goto unlock;
308 if (timer->num_instances >= timer->max_instances) {
309 err = -EBUSY;
310 goto unlock;
312 timeri = snd_timer_instance_new(owner, timer);
313 if (!timeri) {
314 err = -ENOMEM;
315 goto unlock;
317 /* take a card refcount for safe disconnection */
318 if (timer->card)
319 get_device(&timer->card->card_dev);
320 timeri->slave_class = tid->dev_sclass;
321 timeri->slave_id = slave_id;
323 if (list_empty(&timer->open_list_head) && timer->hw.open) {
324 err = timer->hw.open(timer);
325 if (err) {
326 kfree(timeri->owner);
327 kfree(timeri);
328 timeri = NULL;
330 if (timer->card)
331 card_dev_to_put = &timer->card->card_dev;
332 module_put(timer->module);
333 goto unlock;
337 list_add_tail(&timeri->open_list, &timer->open_list_head);
338 timer->num_instances++;
339 err = snd_timer_check_master(timeri);
340 if (err < 0) {
341 snd_timer_close_locked(timeri, &card_dev_to_put);
342 timeri = NULL;
345 unlock:
346 mutex_unlock(&register_mutex);
347 /* put_device() is called after unlock for avoiding deadlock */
348 if (card_dev_to_put)
349 put_device(card_dev_to_put);
350 *ti = timeri;
351 return err;
353 EXPORT_SYMBOL(snd_timer_open);
356 * close a timer instance
357 * call this with register_mutex down.
359 static int snd_timer_close_locked(struct snd_timer_instance *timeri,
360 struct device **card_devp_to_put)
362 struct snd_timer *timer = NULL;
363 struct snd_timer_instance *slave, *tmp;
365 list_del(&timeri->open_list);
367 /* force to stop the timer */
368 snd_timer_stop(timeri);
370 timer = timeri->timer;
371 if (timer) {
372 timer->num_instances--;
373 /* wait, until the active callback is finished */
374 spin_lock_irq(&timer->lock);
375 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
376 spin_unlock_irq(&timer->lock);
377 udelay(10);
378 spin_lock_irq(&timer->lock);
380 spin_unlock_irq(&timer->lock);
382 /* remove slave links */
383 spin_lock_irq(&slave_active_lock);
384 spin_lock(&timer->lock);
385 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
386 open_list) {
387 list_move_tail(&slave->open_list, &snd_timer_slave_list);
388 timer->num_instances--;
389 slave->master = NULL;
390 slave->timer = NULL;
391 list_del_init(&slave->ack_list);
392 list_del_init(&slave->active_list);
394 spin_unlock(&timer->lock);
395 spin_unlock_irq(&slave_active_lock);
397 /* slave doesn't need to release timer resources below */
398 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
399 timer = NULL;
402 if (timeri->private_free)
403 timeri->private_free(timeri);
404 kfree(timeri->owner);
405 kfree(timeri);
407 if (timer) {
408 if (list_empty(&timer->open_list_head) && timer->hw.close)
409 timer->hw.close(timer);
410 /* release a card refcount for safe disconnection */
411 if (timer->card)
412 *card_devp_to_put = &timer->card->card_dev;
413 module_put(timer->module);
416 return 0;
420 * close a timer instance
422 int snd_timer_close(struct snd_timer_instance *timeri)
424 struct device *card_dev_to_put = NULL;
425 int err;
427 if (snd_BUG_ON(!timeri))
428 return -ENXIO;
430 mutex_lock(&register_mutex);
431 err = snd_timer_close_locked(timeri, &card_dev_to_put);
432 mutex_unlock(&register_mutex);
433 /* put_device() is called after unlock for avoiding deadlock */
434 if (card_dev_to_put)
435 put_device(card_dev_to_put);
436 return err;
438 EXPORT_SYMBOL(snd_timer_close);
440 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
442 struct snd_timer * timer;
444 if (timeri == NULL)
445 return 0;
446 if ((timer = timeri->timer) != NULL) {
447 if (timer->hw.c_resolution)
448 return timer->hw.c_resolution(timer);
449 return timer->hw.resolution;
451 return 0;
453 EXPORT_SYMBOL(snd_timer_resolution);
455 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
457 struct snd_timer *timer;
458 unsigned long resolution = 0;
459 struct snd_timer_instance *ts;
460 struct timespec tstamp;
462 if (timer_tstamp_monotonic)
463 ktime_get_ts(&tstamp);
464 else
465 getnstimeofday(&tstamp);
466 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
467 event > SNDRV_TIMER_EVENT_PAUSE))
468 return;
469 if (event == SNDRV_TIMER_EVENT_START ||
470 event == SNDRV_TIMER_EVENT_CONTINUE)
471 resolution = snd_timer_resolution(ti);
472 if (ti->ccallback)
473 ti->ccallback(ti, event, &tstamp, resolution);
474 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
475 return;
476 timer = ti->timer;
477 if (timer == NULL)
478 return;
479 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
480 return;
481 list_for_each_entry(ts, &ti->slave_active_head, active_list)
482 if (ts->ccallback)
483 ts->ccallback(ts, event + 100, &tstamp, resolution);
486 /* start/continue a master timer */
487 static int snd_timer_start1(struct snd_timer_instance *timeri,
488 bool start, unsigned long ticks)
490 struct snd_timer *timer;
491 int result;
492 unsigned long flags;
494 timer = timeri->timer;
495 if (!timer)
496 return -EINVAL;
498 spin_lock_irqsave(&timer->lock, flags);
499 if (timer->card && timer->card->shutdown) {
500 result = -ENODEV;
501 goto unlock;
503 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
504 SNDRV_TIMER_IFLG_START)) {
505 result = -EBUSY;
506 goto unlock;
509 if (start)
510 timeri->ticks = timeri->cticks = ticks;
511 else if (!timeri->cticks)
512 timeri->cticks = 1;
513 timeri->pticks = 0;
515 list_move_tail(&timeri->active_list, &timer->active_list_head);
516 if (timer->running) {
517 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
518 goto __start_now;
519 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
520 timeri->flags |= SNDRV_TIMER_IFLG_START;
521 result = 1; /* delayed start */
522 } else {
523 if (start)
524 timer->sticks = ticks;
525 timer->hw.start(timer);
526 __start_now:
527 timer->running++;
528 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
529 result = 0;
531 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
532 SNDRV_TIMER_EVENT_CONTINUE);
533 unlock:
534 spin_unlock_irqrestore(&timer->lock, flags);
535 return result;
538 /* start/continue a slave timer */
539 static int snd_timer_start_slave(struct snd_timer_instance *timeri,
540 bool start)
542 unsigned long flags;
544 spin_lock_irqsave(&slave_active_lock, flags);
545 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
546 spin_unlock_irqrestore(&slave_active_lock, flags);
547 return -EBUSY;
549 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
550 if (timeri->master && timeri->timer) {
551 spin_lock(&timeri->timer->lock);
552 list_add_tail(&timeri->active_list,
553 &timeri->master->slave_active_head);
554 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
555 SNDRV_TIMER_EVENT_CONTINUE);
556 spin_unlock(&timeri->timer->lock);
558 spin_unlock_irqrestore(&slave_active_lock, flags);
559 return 1; /* delayed start */
562 /* stop/pause a master timer */
563 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
565 struct snd_timer *timer;
566 int result = 0;
567 unsigned long flags;
569 timer = timeri->timer;
570 if (!timer)
571 return -EINVAL;
572 spin_lock_irqsave(&timer->lock, flags);
573 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
574 SNDRV_TIMER_IFLG_START))) {
575 result = -EBUSY;
576 goto unlock;
578 list_del_init(&timeri->ack_list);
579 list_del_init(&timeri->active_list);
580 if (timer->card && timer->card->shutdown)
581 goto unlock;
582 if (stop) {
583 timeri->cticks = timeri->ticks;
584 timeri->pticks = 0;
586 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
587 !(--timer->running)) {
588 timer->hw.stop(timer);
589 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
590 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
591 snd_timer_reschedule(timer, 0);
592 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
593 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
594 timer->hw.start(timer);
598 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
599 if (stop)
600 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
601 else
602 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
603 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
604 SNDRV_TIMER_EVENT_PAUSE);
605 unlock:
606 spin_unlock_irqrestore(&timer->lock, flags);
607 return result;
610 /* stop/pause a slave timer */
611 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
613 unsigned long flags;
615 spin_lock_irqsave(&slave_active_lock, flags);
616 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
617 spin_unlock_irqrestore(&slave_active_lock, flags);
618 return -EBUSY;
620 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
621 if (timeri->timer) {
622 spin_lock(&timeri->timer->lock);
623 list_del_init(&timeri->ack_list);
624 list_del_init(&timeri->active_list);
625 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
626 SNDRV_TIMER_EVENT_PAUSE);
627 spin_unlock(&timeri->timer->lock);
629 spin_unlock_irqrestore(&slave_active_lock, flags);
630 return 0;
634 * start the timer instance
636 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
638 if (timeri == NULL || ticks < 1)
639 return -EINVAL;
640 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
641 return snd_timer_start_slave(timeri, true);
642 else
643 return snd_timer_start1(timeri, true, ticks);
645 EXPORT_SYMBOL(snd_timer_start);
648 * stop the timer instance.
650 * do not call this from the timer callback!
652 int snd_timer_stop(struct snd_timer_instance *timeri)
654 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
655 return snd_timer_stop_slave(timeri, true);
656 else
657 return snd_timer_stop1(timeri, true);
659 EXPORT_SYMBOL(snd_timer_stop);
662 * start again.. the tick is kept.
664 int snd_timer_continue(struct snd_timer_instance *timeri)
666 /* timer can continue only after pause */
667 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
668 return -EINVAL;
670 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
671 return snd_timer_start_slave(timeri, false);
672 else
673 return snd_timer_start1(timeri, false, 0);
675 EXPORT_SYMBOL(snd_timer_continue);
678 * pause.. remember the ticks left
680 int snd_timer_pause(struct snd_timer_instance * timeri)
682 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
683 return snd_timer_stop_slave(timeri, false);
684 else
685 return snd_timer_stop1(timeri, false);
687 EXPORT_SYMBOL(snd_timer_pause);
690 * reschedule the timer
692 * start pending instances and check the scheduling ticks.
693 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
695 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
697 struct snd_timer_instance *ti;
698 unsigned long ticks = ~0UL;
700 list_for_each_entry(ti, &timer->active_list_head, active_list) {
701 if (ti->flags & SNDRV_TIMER_IFLG_START) {
702 ti->flags &= ~SNDRV_TIMER_IFLG_START;
703 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
704 timer->running++;
706 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
707 if (ticks > ti->cticks)
708 ticks = ti->cticks;
711 if (ticks == ~0UL) {
712 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
713 return;
715 if (ticks > timer->hw.ticks)
716 ticks = timer->hw.ticks;
717 if (ticks_left != ticks)
718 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
719 timer->sticks = ticks;
723 * timer tasklet
726 static void snd_timer_tasklet(unsigned long arg)
728 struct snd_timer *timer = (struct snd_timer *) arg;
729 struct snd_timer_instance *ti;
730 struct list_head *p;
731 unsigned long resolution, ticks;
732 unsigned long flags;
734 if (timer->card && timer->card->shutdown)
735 return;
737 spin_lock_irqsave(&timer->lock, flags);
738 /* now process all callbacks */
739 while (!list_empty(&timer->sack_list_head)) {
740 p = timer->sack_list_head.next; /* get first item */
741 ti = list_entry(p, struct snd_timer_instance, ack_list);
743 /* remove from ack_list and make empty */
744 list_del_init(p);
746 ticks = ti->pticks;
747 ti->pticks = 0;
748 resolution = ti->resolution;
750 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
751 spin_unlock(&timer->lock);
752 if (ti->callback)
753 ti->callback(ti, resolution, ticks);
754 spin_lock(&timer->lock);
755 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
757 spin_unlock_irqrestore(&timer->lock, flags);
761 * timer interrupt
763 * ticks_left is usually equal to timer->sticks.
766 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
768 struct snd_timer_instance *ti, *ts, *tmp;
769 unsigned long resolution, ticks;
770 struct list_head *p, *ack_list_head;
771 unsigned long flags;
772 int use_tasklet = 0;
774 if (timer == NULL)
775 return;
777 if (timer->card && timer->card->shutdown)
778 return;
780 spin_lock_irqsave(&timer->lock, flags);
782 /* remember the current resolution */
783 if (timer->hw.c_resolution)
784 resolution = timer->hw.c_resolution(timer);
785 else
786 resolution = timer->hw.resolution;
788 /* loop for all active instances
789 * Here we cannot use list_for_each_entry because the active_list of a
790 * processed instance is relinked to done_list_head before the callback
791 * is called.
793 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
794 active_list) {
795 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
796 continue;
797 ti->pticks += ticks_left;
798 ti->resolution = resolution;
799 if (ti->cticks < ticks_left)
800 ti->cticks = 0;
801 else
802 ti->cticks -= ticks_left;
803 if (ti->cticks) /* not expired */
804 continue;
805 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
806 ti->cticks = ti->ticks;
807 } else {
808 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
809 --timer->running;
810 list_del_init(&ti->active_list);
812 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
813 (ti->flags & SNDRV_TIMER_IFLG_FAST))
814 ack_list_head = &timer->ack_list_head;
815 else
816 ack_list_head = &timer->sack_list_head;
817 if (list_empty(&ti->ack_list))
818 list_add_tail(&ti->ack_list, ack_list_head);
819 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
820 ts->pticks = ti->pticks;
821 ts->resolution = resolution;
822 if (list_empty(&ts->ack_list))
823 list_add_tail(&ts->ack_list, ack_list_head);
826 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
827 snd_timer_reschedule(timer, timer->sticks);
828 if (timer->running) {
829 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
830 timer->hw.stop(timer);
831 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
833 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
834 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
835 /* restart timer */
836 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
837 timer->hw.start(timer);
839 } else {
840 timer->hw.stop(timer);
843 /* now process all fast callbacks */
844 while (!list_empty(&timer->ack_list_head)) {
845 p = timer->ack_list_head.next; /* get first item */
846 ti = list_entry(p, struct snd_timer_instance, ack_list);
848 /* remove from ack_list and make empty */
849 list_del_init(p);
851 ticks = ti->pticks;
852 ti->pticks = 0;
854 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
855 spin_unlock(&timer->lock);
856 if (ti->callback)
857 ti->callback(ti, resolution, ticks);
858 spin_lock(&timer->lock);
859 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
862 /* do we have any slow callbacks? */
863 use_tasklet = !list_empty(&timer->sack_list_head);
864 spin_unlock_irqrestore(&timer->lock, flags);
866 if (use_tasklet)
867 tasklet_schedule(&timer->task_queue);
869 EXPORT_SYMBOL(snd_timer_interrupt);
875 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
876 struct snd_timer **rtimer)
878 struct snd_timer *timer;
879 int err;
880 static struct snd_device_ops ops = {
881 .dev_free = snd_timer_dev_free,
882 .dev_register = snd_timer_dev_register,
883 .dev_disconnect = snd_timer_dev_disconnect,
886 if (snd_BUG_ON(!tid))
887 return -EINVAL;
888 if (rtimer)
889 *rtimer = NULL;
890 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
891 if (!timer)
892 return -ENOMEM;
893 timer->tmr_class = tid->dev_class;
894 timer->card = card;
895 timer->tmr_device = tid->device;
896 timer->tmr_subdevice = tid->subdevice;
897 if (id)
898 strlcpy(timer->id, id, sizeof(timer->id));
899 timer->sticks = 1;
900 INIT_LIST_HEAD(&timer->device_list);
901 INIT_LIST_HEAD(&timer->open_list_head);
902 INIT_LIST_HEAD(&timer->active_list_head);
903 INIT_LIST_HEAD(&timer->ack_list_head);
904 INIT_LIST_HEAD(&timer->sack_list_head);
905 spin_lock_init(&timer->lock);
906 tasklet_init(&timer->task_queue, snd_timer_tasklet,
907 (unsigned long)timer);
908 timer->max_instances = 1000; /* default limit per timer */
909 if (card != NULL) {
910 timer->module = card->module;
911 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
912 if (err < 0) {
913 snd_timer_free(timer);
914 return err;
917 if (rtimer)
918 *rtimer = timer;
919 return 0;
921 EXPORT_SYMBOL(snd_timer_new);
923 static int snd_timer_free(struct snd_timer *timer)
925 if (!timer)
926 return 0;
928 mutex_lock(&register_mutex);
929 if (! list_empty(&timer->open_list_head)) {
930 struct list_head *p, *n;
931 struct snd_timer_instance *ti;
932 pr_warn("ALSA: timer %p is busy?\n", timer);
933 list_for_each_safe(p, n, &timer->open_list_head) {
934 list_del_init(p);
935 ti = list_entry(p, struct snd_timer_instance, open_list);
936 ti->timer = NULL;
939 list_del(&timer->device_list);
940 mutex_unlock(&register_mutex);
942 if (timer->private_free)
943 timer->private_free(timer);
944 kfree(timer);
945 return 0;
948 static int snd_timer_dev_free(struct snd_device *device)
950 struct snd_timer *timer = device->device_data;
951 return snd_timer_free(timer);
954 static int snd_timer_dev_register(struct snd_device *dev)
956 struct snd_timer *timer = dev->device_data;
957 struct snd_timer *timer1;
959 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
960 return -ENXIO;
961 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
962 !timer->hw.resolution && timer->hw.c_resolution == NULL)
963 return -EINVAL;
965 mutex_lock(&register_mutex);
966 list_for_each_entry(timer1, &snd_timer_list, device_list) {
967 if (timer1->tmr_class > timer->tmr_class)
968 break;
969 if (timer1->tmr_class < timer->tmr_class)
970 continue;
971 if (timer1->card && timer->card) {
972 if (timer1->card->number > timer->card->number)
973 break;
974 if (timer1->card->number < timer->card->number)
975 continue;
977 if (timer1->tmr_device > timer->tmr_device)
978 break;
979 if (timer1->tmr_device < timer->tmr_device)
980 continue;
981 if (timer1->tmr_subdevice > timer->tmr_subdevice)
982 break;
983 if (timer1->tmr_subdevice < timer->tmr_subdevice)
984 continue;
985 /* conflicts.. */
986 mutex_unlock(&register_mutex);
987 return -EBUSY;
989 list_add_tail(&timer->device_list, &timer1->device_list);
990 mutex_unlock(&register_mutex);
991 return 0;
994 static int snd_timer_dev_disconnect(struct snd_device *device)
996 struct snd_timer *timer = device->device_data;
997 struct snd_timer_instance *ti;
999 mutex_lock(&register_mutex);
1000 list_del_init(&timer->device_list);
1001 /* wake up pending sleepers */
1002 list_for_each_entry(ti, &timer->open_list_head, open_list) {
1003 if (ti->disconnect)
1004 ti->disconnect(ti);
1006 mutex_unlock(&register_mutex);
1007 return 0;
1010 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
1012 unsigned long flags;
1013 unsigned long resolution = 0;
1014 struct snd_timer_instance *ti, *ts;
1016 if (timer->card && timer->card->shutdown)
1017 return;
1018 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1019 return;
1020 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1021 event > SNDRV_TIMER_EVENT_MRESUME))
1022 return;
1023 spin_lock_irqsave(&timer->lock, flags);
1024 if (event == SNDRV_TIMER_EVENT_MSTART ||
1025 event == SNDRV_TIMER_EVENT_MCONTINUE ||
1026 event == SNDRV_TIMER_EVENT_MRESUME) {
1027 if (timer->hw.c_resolution)
1028 resolution = timer->hw.c_resolution(timer);
1029 else
1030 resolution = timer->hw.resolution;
1032 list_for_each_entry(ti, &timer->active_list_head, active_list) {
1033 if (ti->ccallback)
1034 ti->ccallback(ti, event, tstamp, resolution);
1035 list_for_each_entry(ts, &ti->slave_active_head, active_list)
1036 if (ts->ccallback)
1037 ts->ccallback(ts, event, tstamp, resolution);
1039 spin_unlock_irqrestore(&timer->lock, flags);
1041 EXPORT_SYMBOL(snd_timer_notify);
1044 * exported functions for global timers
1046 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1048 struct snd_timer_id tid;
1050 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1051 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1052 tid.card = -1;
1053 tid.device = device;
1054 tid.subdevice = 0;
1055 return snd_timer_new(NULL, id, &tid, rtimer);
1057 EXPORT_SYMBOL(snd_timer_global_new);
1059 int snd_timer_global_free(struct snd_timer *timer)
1061 return snd_timer_free(timer);
1063 EXPORT_SYMBOL(snd_timer_global_free);
1065 int snd_timer_global_register(struct snd_timer *timer)
1067 struct snd_device dev;
1069 memset(&dev, 0, sizeof(dev));
1070 dev.device_data = timer;
1071 return snd_timer_dev_register(&dev);
1073 EXPORT_SYMBOL(snd_timer_global_register);
1076 * System timer
1079 struct snd_timer_system_private {
1080 struct timer_list tlist;
1081 unsigned long last_expires;
1082 unsigned long last_jiffies;
1083 unsigned long correction;
1086 static void snd_timer_s_function(unsigned long data)
1088 struct snd_timer *timer = (struct snd_timer *)data;
1089 struct snd_timer_system_private *priv = timer->private_data;
1090 unsigned long jiff = jiffies;
1091 if (time_after(jiff, priv->last_expires))
1092 priv->correction += (long)jiff - (long)priv->last_expires;
1093 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1096 static int snd_timer_s_start(struct snd_timer * timer)
1098 struct snd_timer_system_private *priv;
1099 unsigned long njiff;
1101 priv = (struct snd_timer_system_private *) timer->private_data;
1102 njiff = (priv->last_jiffies = jiffies);
1103 if (priv->correction > timer->sticks - 1) {
1104 priv->correction -= timer->sticks - 1;
1105 njiff++;
1106 } else {
1107 njiff += timer->sticks - priv->correction;
1108 priv->correction = 0;
1110 priv->last_expires = njiff;
1111 mod_timer(&priv->tlist, njiff);
1112 return 0;
1115 static int snd_timer_s_stop(struct snd_timer * timer)
1117 struct snd_timer_system_private *priv;
1118 unsigned long jiff;
1120 priv = (struct snd_timer_system_private *) timer->private_data;
1121 del_timer(&priv->tlist);
1122 jiff = jiffies;
1123 if (time_before(jiff, priv->last_expires))
1124 timer->sticks = priv->last_expires - jiff;
1125 else
1126 timer->sticks = 1;
1127 priv->correction = 0;
1128 return 0;
1131 static int snd_timer_s_close(struct snd_timer *timer)
1133 struct snd_timer_system_private *priv;
1135 priv = (struct snd_timer_system_private *)timer->private_data;
1136 del_timer_sync(&priv->tlist);
1137 return 0;
1140 static struct snd_timer_hardware snd_timer_system =
1142 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1143 .resolution = 1000000000L / HZ,
1144 .ticks = 10000000L,
1145 .close = snd_timer_s_close,
1146 .start = snd_timer_s_start,
1147 .stop = snd_timer_s_stop
1150 static void snd_timer_free_system(struct snd_timer *timer)
1152 kfree(timer->private_data);
1155 static int snd_timer_register_system(void)
1157 struct snd_timer *timer;
1158 struct snd_timer_system_private *priv;
1159 int err;
1161 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1162 if (err < 0)
1163 return err;
1164 strcpy(timer->name, "system timer");
1165 timer->hw = snd_timer_system;
1166 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1167 if (priv == NULL) {
1168 snd_timer_free(timer);
1169 return -ENOMEM;
1171 setup_timer(&priv->tlist, snd_timer_s_function, (unsigned long) timer);
1172 timer->private_data = priv;
1173 timer->private_free = snd_timer_free_system;
1174 return snd_timer_global_register(timer);
1177 #ifdef CONFIG_SND_PROC_FS
1179 * Info interface
1182 static void snd_timer_proc_read(struct snd_info_entry *entry,
1183 struct snd_info_buffer *buffer)
1185 struct snd_timer *timer;
1186 struct snd_timer_instance *ti;
1188 mutex_lock(&register_mutex);
1189 list_for_each_entry(timer, &snd_timer_list, device_list) {
1190 if (timer->card && timer->card->shutdown)
1191 continue;
1192 switch (timer->tmr_class) {
1193 case SNDRV_TIMER_CLASS_GLOBAL:
1194 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1195 break;
1196 case SNDRV_TIMER_CLASS_CARD:
1197 snd_iprintf(buffer, "C%i-%i: ",
1198 timer->card->number, timer->tmr_device);
1199 break;
1200 case SNDRV_TIMER_CLASS_PCM:
1201 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1202 timer->tmr_device, timer->tmr_subdevice);
1203 break;
1204 default:
1205 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1206 timer->card ? timer->card->number : -1,
1207 timer->tmr_device, timer->tmr_subdevice);
1209 snd_iprintf(buffer, "%s :", timer->name);
1210 if (timer->hw.resolution)
1211 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1212 timer->hw.resolution / 1000,
1213 timer->hw.resolution % 1000,
1214 timer->hw.ticks);
1215 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1216 snd_iprintf(buffer, " SLAVE");
1217 snd_iprintf(buffer, "\n");
1218 list_for_each_entry(ti, &timer->open_list_head, open_list)
1219 snd_iprintf(buffer, " Client %s : %s\n",
1220 ti->owner ? ti->owner : "unknown",
1221 ti->flags & (SNDRV_TIMER_IFLG_START |
1222 SNDRV_TIMER_IFLG_RUNNING)
1223 ? "running" : "stopped");
1225 mutex_unlock(&register_mutex);
1228 static struct snd_info_entry *snd_timer_proc_entry;
1230 static void __init snd_timer_proc_init(void)
1232 struct snd_info_entry *entry;
1234 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1235 if (entry != NULL) {
1236 entry->c.text.read = snd_timer_proc_read;
1237 if (snd_info_register(entry) < 0) {
1238 snd_info_free_entry(entry);
1239 entry = NULL;
1242 snd_timer_proc_entry = entry;
1245 static void __exit snd_timer_proc_done(void)
1247 snd_info_free_entry(snd_timer_proc_entry);
1249 #else /* !CONFIG_SND_PROC_FS */
1250 #define snd_timer_proc_init()
1251 #define snd_timer_proc_done()
1252 #endif
1255 * USER SPACE interface
1258 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1259 unsigned long resolution,
1260 unsigned long ticks)
1262 struct snd_timer_user *tu = timeri->callback_data;
1263 struct snd_timer_read *r;
1264 int prev;
1266 spin_lock(&tu->qlock);
1267 if (tu->qused > 0) {
1268 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1269 r = &tu->queue[prev];
1270 if (r->resolution == resolution) {
1271 r->ticks += ticks;
1272 goto __wake;
1275 if (tu->qused >= tu->queue_size) {
1276 tu->overrun++;
1277 } else {
1278 r = &tu->queue[tu->qtail++];
1279 tu->qtail %= tu->queue_size;
1280 r->resolution = resolution;
1281 r->ticks = ticks;
1282 tu->qused++;
1284 __wake:
1285 spin_unlock(&tu->qlock);
1286 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1287 wake_up(&tu->qchange_sleep);
1290 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1291 struct snd_timer_tread *tread)
1293 if (tu->qused >= tu->queue_size) {
1294 tu->overrun++;
1295 } else {
1296 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1297 tu->qtail %= tu->queue_size;
1298 tu->qused++;
1302 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1303 int event,
1304 struct timespec *tstamp,
1305 unsigned long resolution)
1307 struct snd_timer_user *tu = timeri->callback_data;
1308 struct snd_timer_tread r1;
1309 unsigned long flags;
1311 if (event >= SNDRV_TIMER_EVENT_START &&
1312 event <= SNDRV_TIMER_EVENT_PAUSE)
1313 tu->tstamp = *tstamp;
1314 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1315 return;
1316 memset(&r1, 0, sizeof(r1));
1317 r1.event = event;
1318 r1.tstamp = *tstamp;
1319 r1.val = resolution;
1320 spin_lock_irqsave(&tu->qlock, flags);
1321 snd_timer_user_append_to_tqueue(tu, &r1);
1322 spin_unlock_irqrestore(&tu->qlock, flags);
1323 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1324 wake_up(&tu->qchange_sleep);
1327 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1329 struct snd_timer_user *tu = timeri->callback_data;
1331 tu->disconnected = true;
1332 wake_up(&tu->qchange_sleep);
1335 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1336 unsigned long resolution,
1337 unsigned long ticks)
1339 struct snd_timer_user *tu = timeri->callback_data;
1340 struct snd_timer_tread *r, r1;
1341 struct timespec tstamp;
1342 int prev, append = 0;
1344 memset(&tstamp, 0, sizeof(tstamp));
1345 spin_lock(&tu->qlock);
1346 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1347 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1348 spin_unlock(&tu->qlock);
1349 return;
1351 if (tu->last_resolution != resolution || ticks > 0) {
1352 if (timer_tstamp_monotonic)
1353 ktime_get_ts(&tstamp);
1354 else
1355 getnstimeofday(&tstamp);
1357 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1358 tu->last_resolution != resolution) {
1359 memset(&r1, 0, sizeof(r1));
1360 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1361 r1.tstamp = tstamp;
1362 r1.val = resolution;
1363 snd_timer_user_append_to_tqueue(tu, &r1);
1364 tu->last_resolution = resolution;
1365 append++;
1367 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1368 goto __wake;
1369 if (ticks == 0)
1370 goto __wake;
1371 if (tu->qused > 0) {
1372 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1373 r = &tu->tqueue[prev];
1374 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1375 r->tstamp = tstamp;
1376 r->val += ticks;
1377 append++;
1378 goto __wake;
1381 r1.event = SNDRV_TIMER_EVENT_TICK;
1382 r1.tstamp = tstamp;
1383 r1.val = ticks;
1384 snd_timer_user_append_to_tqueue(tu, &r1);
1385 append++;
1386 __wake:
1387 spin_unlock(&tu->qlock);
1388 if (append == 0)
1389 return;
1390 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1391 wake_up(&tu->qchange_sleep);
1394 static int snd_timer_user_open(struct inode *inode, struct file *file)
1396 struct snd_timer_user *tu;
1397 int err;
1399 err = nonseekable_open(inode, file);
1400 if (err < 0)
1401 return err;
1403 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1404 if (tu == NULL)
1405 return -ENOMEM;
1406 spin_lock_init(&tu->qlock);
1407 init_waitqueue_head(&tu->qchange_sleep);
1408 mutex_init(&tu->ioctl_lock);
1409 tu->ticks = 1;
1410 tu->queue_size = 128;
1411 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1412 GFP_KERNEL);
1413 if (tu->queue == NULL) {
1414 kfree(tu);
1415 return -ENOMEM;
1417 file->private_data = tu;
1418 return 0;
1421 static int snd_timer_user_release(struct inode *inode, struct file *file)
1423 struct snd_timer_user *tu;
1425 if (file->private_data) {
1426 tu = file->private_data;
1427 file->private_data = NULL;
1428 mutex_lock(&tu->ioctl_lock);
1429 if (tu->timeri)
1430 snd_timer_close(tu->timeri);
1431 mutex_unlock(&tu->ioctl_lock);
1432 kfree(tu->queue);
1433 kfree(tu->tqueue);
1434 kfree(tu);
1436 return 0;
1439 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1441 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1442 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1443 id->card = -1;
1444 id->device = -1;
1445 id->subdevice = -1;
1448 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1450 id->dev_class = timer->tmr_class;
1451 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1452 id->card = timer->card ? timer->card->number : -1;
1453 id->device = timer->tmr_device;
1454 id->subdevice = timer->tmr_subdevice;
1457 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1459 struct snd_timer_id id;
1460 struct snd_timer *timer;
1461 struct list_head *p;
1463 if (copy_from_user(&id, _tid, sizeof(id)))
1464 return -EFAULT;
1465 mutex_lock(&register_mutex);
1466 if (id.dev_class < 0) { /* first item */
1467 if (list_empty(&snd_timer_list))
1468 snd_timer_user_zero_id(&id);
1469 else {
1470 timer = list_entry(snd_timer_list.next,
1471 struct snd_timer, device_list);
1472 snd_timer_user_copy_id(&id, timer);
1474 } else {
1475 switch (id.dev_class) {
1476 case SNDRV_TIMER_CLASS_GLOBAL:
1477 id.device = id.device < 0 ? 0 : id.device + 1;
1478 list_for_each(p, &snd_timer_list) {
1479 timer = list_entry(p, struct snd_timer, device_list);
1480 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1481 snd_timer_user_copy_id(&id, timer);
1482 break;
1484 if (timer->tmr_device >= id.device) {
1485 snd_timer_user_copy_id(&id, timer);
1486 break;
1489 if (p == &snd_timer_list)
1490 snd_timer_user_zero_id(&id);
1491 break;
1492 case SNDRV_TIMER_CLASS_CARD:
1493 case SNDRV_TIMER_CLASS_PCM:
1494 if (id.card < 0) {
1495 id.card = 0;
1496 } else {
1497 if (id.card < 0) {
1498 id.card = 0;
1499 } else {
1500 if (id.device < 0) {
1501 id.device = 0;
1502 } else {
1503 if (id.subdevice < 0) {
1504 id.subdevice = 0;
1505 } else {
1506 id.subdevice++;
1511 list_for_each(p, &snd_timer_list) {
1512 timer = list_entry(p, struct snd_timer, device_list);
1513 if (timer->tmr_class > id.dev_class) {
1514 snd_timer_user_copy_id(&id, timer);
1515 break;
1517 if (timer->tmr_class < id.dev_class)
1518 continue;
1519 if (timer->card->number > id.card) {
1520 snd_timer_user_copy_id(&id, timer);
1521 break;
1523 if (timer->card->number < id.card)
1524 continue;
1525 if (timer->tmr_device > id.device) {
1526 snd_timer_user_copy_id(&id, timer);
1527 break;
1529 if (timer->tmr_device < id.device)
1530 continue;
1531 if (timer->tmr_subdevice > id.subdevice) {
1532 snd_timer_user_copy_id(&id, timer);
1533 break;
1535 if (timer->tmr_subdevice < id.subdevice)
1536 continue;
1537 snd_timer_user_copy_id(&id, timer);
1538 break;
1540 if (p == &snd_timer_list)
1541 snd_timer_user_zero_id(&id);
1542 break;
1543 default:
1544 snd_timer_user_zero_id(&id);
1547 mutex_unlock(&register_mutex);
1548 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1549 return -EFAULT;
1550 return 0;
1553 static int snd_timer_user_ginfo(struct file *file,
1554 struct snd_timer_ginfo __user *_ginfo)
1556 struct snd_timer_ginfo *ginfo;
1557 struct snd_timer_id tid;
1558 struct snd_timer *t;
1559 struct list_head *p;
1560 int err = 0;
1562 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1563 if (IS_ERR(ginfo))
1564 return PTR_ERR(ginfo);
1566 tid = ginfo->tid;
1567 memset(ginfo, 0, sizeof(*ginfo));
1568 ginfo->tid = tid;
1569 mutex_lock(&register_mutex);
1570 t = snd_timer_find(&tid);
1571 if (t != NULL) {
1572 ginfo->card = t->card ? t->card->number : -1;
1573 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1574 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1575 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1576 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1577 ginfo->resolution = t->hw.resolution;
1578 if (t->hw.resolution_min > 0) {
1579 ginfo->resolution_min = t->hw.resolution_min;
1580 ginfo->resolution_max = t->hw.resolution_max;
1582 list_for_each(p, &t->open_list_head) {
1583 ginfo->clients++;
1585 } else {
1586 err = -ENODEV;
1588 mutex_unlock(&register_mutex);
1589 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1590 err = -EFAULT;
1591 kfree(ginfo);
1592 return err;
1595 static int timer_set_gparams(struct snd_timer_gparams *gparams)
1597 struct snd_timer *t;
1598 int err;
1600 mutex_lock(&register_mutex);
1601 t = snd_timer_find(&gparams->tid);
1602 if (!t) {
1603 err = -ENODEV;
1604 goto _error;
1606 if (!list_empty(&t->open_list_head)) {
1607 err = -EBUSY;
1608 goto _error;
1610 if (!t->hw.set_period) {
1611 err = -ENOSYS;
1612 goto _error;
1614 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
1615 _error:
1616 mutex_unlock(&register_mutex);
1617 return err;
1620 static int snd_timer_user_gparams(struct file *file,
1621 struct snd_timer_gparams __user *_gparams)
1623 struct snd_timer_gparams gparams;
1625 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1626 return -EFAULT;
1627 return timer_set_gparams(&gparams);
1630 static int snd_timer_user_gstatus(struct file *file,
1631 struct snd_timer_gstatus __user *_gstatus)
1633 struct snd_timer_gstatus gstatus;
1634 struct snd_timer_id tid;
1635 struct snd_timer *t;
1636 int err = 0;
1638 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1639 return -EFAULT;
1640 tid = gstatus.tid;
1641 memset(&gstatus, 0, sizeof(gstatus));
1642 gstatus.tid = tid;
1643 mutex_lock(&register_mutex);
1644 t = snd_timer_find(&tid);
1645 if (t != NULL) {
1646 if (t->hw.c_resolution)
1647 gstatus.resolution = t->hw.c_resolution(t);
1648 else
1649 gstatus.resolution = t->hw.resolution;
1650 if (t->hw.precise_resolution) {
1651 t->hw.precise_resolution(t, &gstatus.resolution_num,
1652 &gstatus.resolution_den);
1653 } else {
1654 gstatus.resolution_num = gstatus.resolution;
1655 gstatus.resolution_den = 1000000000uL;
1657 } else {
1658 err = -ENODEV;
1660 mutex_unlock(&register_mutex);
1661 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1662 err = -EFAULT;
1663 return err;
1666 static int snd_timer_user_tselect(struct file *file,
1667 struct snd_timer_select __user *_tselect)
1669 struct snd_timer_user *tu;
1670 struct snd_timer_select tselect;
1671 char str[32];
1672 int err = 0;
1674 tu = file->private_data;
1675 if (tu->timeri) {
1676 snd_timer_close(tu->timeri);
1677 tu->timeri = NULL;
1679 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1680 err = -EFAULT;
1681 goto __err;
1683 sprintf(str, "application %i", current->pid);
1684 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1685 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1686 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1687 if (err < 0)
1688 goto __err;
1690 tu->qhead = tu->qtail = tu->qused = 0;
1691 kfree(tu->queue);
1692 tu->queue = NULL;
1693 kfree(tu->tqueue);
1694 tu->tqueue = NULL;
1695 if (tu->tread) {
1696 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1697 GFP_KERNEL);
1698 if (tu->tqueue == NULL)
1699 err = -ENOMEM;
1700 } else {
1701 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1702 GFP_KERNEL);
1703 if (tu->queue == NULL)
1704 err = -ENOMEM;
1707 if (err < 0) {
1708 snd_timer_close(tu->timeri);
1709 tu->timeri = NULL;
1710 } else {
1711 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1712 tu->timeri->callback = tu->tread
1713 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1714 tu->timeri->ccallback = snd_timer_user_ccallback;
1715 tu->timeri->callback_data = (void *)tu;
1716 tu->timeri->disconnect = snd_timer_user_disconnect;
1719 __err:
1720 return err;
1723 static int snd_timer_user_info(struct file *file,
1724 struct snd_timer_info __user *_info)
1726 struct snd_timer_user *tu;
1727 struct snd_timer_info *info;
1728 struct snd_timer *t;
1729 int err = 0;
1731 tu = file->private_data;
1732 if (!tu->timeri)
1733 return -EBADFD;
1734 t = tu->timeri->timer;
1735 if (!t)
1736 return -EBADFD;
1738 info = kzalloc(sizeof(*info), GFP_KERNEL);
1739 if (! info)
1740 return -ENOMEM;
1741 info->card = t->card ? t->card->number : -1;
1742 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1743 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1744 strlcpy(info->id, t->id, sizeof(info->id));
1745 strlcpy(info->name, t->name, sizeof(info->name));
1746 info->resolution = t->hw.resolution;
1747 if (copy_to_user(_info, info, sizeof(*_info)))
1748 err = -EFAULT;
1749 kfree(info);
1750 return err;
1753 static int snd_timer_user_params(struct file *file,
1754 struct snd_timer_params __user *_params)
1756 struct snd_timer_user *tu;
1757 struct snd_timer_params params;
1758 struct snd_timer *t;
1759 struct snd_timer_read *tr;
1760 struct snd_timer_tread *ttr;
1761 int err;
1763 tu = file->private_data;
1764 if (!tu->timeri)
1765 return -EBADFD;
1766 t = tu->timeri->timer;
1767 if (!t)
1768 return -EBADFD;
1769 if (copy_from_user(&params, _params, sizeof(params)))
1770 return -EFAULT;
1771 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1772 u64 resolution;
1774 if (params.ticks < 1) {
1775 err = -EINVAL;
1776 goto _end;
1779 /* Don't allow resolution less than 1ms */
1780 resolution = snd_timer_resolution(tu->timeri);
1781 resolution *= params.ticks;
1782 if (resolution < 1000000) {
1783 err = -EINVAL;
1784 goto _end;
1787 if (params.queue_size > 0 &&
1788 (params.queue_size < 32 || params.queue_size > 1024)) {
1789 err = -EINVAL;
1790 goto _end;
1792 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1793 (1<<SNDRV_TIMER_EVENT_TICK)|
1794 (1<<SNDRV_TIMER_EVENT_START)|
1795 (1<<SNDRV_TIMER_EVENT_STOP)|
1796 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1797 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1798 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1799 (1<<SNDRV_TIMER_EVENT_RESUME)|
1800 (1<<SNDRV_TIMER_EVENT_MSTART)|
1801 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1802 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1803 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1804 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1805 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1806 err = -EINVAL;
1807 goto _end;
1809 snd_timer_stop(tu->timeri);
1810 spin_lock_irq(&t->lock);
1811 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1812 SNDRV_TIMER_IFLG_EXCLUSIVE|
1813 SNDRV_TIMER_IFLG_EARLY_EVENT);
1814 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1815 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1816 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1817 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1818 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1819 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1820 spin_unlock_irq(&t->lock);
1821 if (params.queue_size > 0 &&
1822 (unsigned int)tu->queue_size != params.queue_size) {
1823 if (tu->tread) {
1824 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1825 GFP_KERNEL);
1826 if (ttr) {
1827 kfree(tu->tqueue);
1828 tu->queue_size = params.queue_size;
1829 tu->tqueue = ttr;
1831 } else {
1832 tr = kmalloc(params.queue_size * sizeof(*tr),
1833 GFP_KERNEL);
1834 if (tr) {
1835 kfree(tu->queue);
1836 tu->queue_size = params.queue_size;
1837 tu->queue = tr;
1841 tu->qhead = tu->qtail = tu->qused = 0;
1842 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1843 if (tu->tread) {
1844 struct snd_timer_tread tread;
1845 memset(&tread, 0, sizeof(tread));
1846 tread.event = SNDRV_TIMER_EVENT_EARLY;
1847 tread.tstamp.tv_sec = 0;
1848 tread.tstamp.tv_nsec = 0;
1849 tread.val = 0;
1850 snd_timer_user_append_to_tqueue(tu, &tread);
1851 } else {
1852 struct snd_timer_read *r = &tu->queue[0];
1853 r->resolution = 0;
1854 r->ticks = 0;
1855 tu->qused++;
1856 tu->qtail++;
1859 tu->filter = params.filter;
1860 tu->ticks = params.ticks;
1861 err = 0;
1862 _end:
1863 if (copy_to_user(_params, &params, sizeof(params)))
1864 return -EFAULT;
1865 return err;
1868 static int snd_timer_user_status(struct file *file,
1869 struct snd_timer_status __user *_status)
1871 struct snd_timer_user *tu;
1872 struct snd_timer_status status;
1874 tu = file->private_data;
1875 if (!tu->timeri)
1876 return -EBADFD;
1877 memset(&status, 0, sizeof(status));
1878 status.tstamp = tu->tstamp;
1879 status.resolution = snd_timer_resolution(tu->timeri);
1880 status.lost = tu->timeri->lost;
1881 status.overrun = tu->overrun;
1882 spin_lock_irq(&tu->qlock);
1883 status.queue = tu->qused;
1884 spin_unlock_irq(&tu->qlock);
1885 if (copy_to_user(_status, &status, sizeof(status)))
1886 return -EFAULT;
1887 return 0;
1890 static int snd_timer_user_start(struct file *file)
1892 int err;
1893 struct snd_timer_user *tu;
1895 tu = file->private_data;
1896 if (!tu->timeri)
1897 return -EBADFD;
1898 snd_timer_stop(tu->timeri);
1899 tu->timeri->lost = 0;
1900 tu->last_resolution = 0;
1901 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1904 static int snd_timer_user_stop(struct file *file)
1906 int err;
1907 struct snd_timer_user *tu;
1909 tu = file->private_data;
1910 if (!tu->timeri)
1911 return -EBADFD;
1912 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1915 static int snd_timer_user_continue(struct file *file)
1917 int err;
1918 struct snd_timer_user *tu;
1920 tu = file->private_data;
1921 if (!tu->timeri)
1922 return -EBADFD;
1923 /* start timer instead of continue if it's not used before */
1924 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1925 return snd_timer_user_start(file);
1926 tu->timeri->lost = 0;
1927 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1930 static int snd_timer_user_pause(struct file *file)
1932 int err;
1933 struct snd_timer_user *tu;
1935 tu = file->private_data;
1936 if (!tu->timeri)
1937 return -EBADFD;
1938 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1941 enum {
1942 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1943 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1944 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1945 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1948 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1949 unsigned long arg)
1951 struct snd_timer_user *tu;
1952 void __user *argp = (void __user *)arg;
1953 int __user *p = argp;
1955 tu = file->private_data;
1956 switch (cmd) {
1957 case SNDRV_TIMER_IOCTL_PVERSION:
1958 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1959 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1960 return snd_timer_user_next_device(argp);
1961 case SNDRV_TIMER_IOCTL_TREAD:
1963 int xarg;
1965 if (tu->timeri) /* too late */
1966 return -EBUSY;
1967 if (get_user(xarg, p))
1968 return -EFAULT;
1969 tu->tread = xarg ? 1 : 0;
1970 return 0;
1972 case SNDRV_TIMER_IOCTL_GINFO:
1973 return snd_timer_user_ginfo(file, argp);
1974 case SNDRV_TIMER_IOCTL_GPARAMS:
1975 return snd_timer_user_gparams(file, argp);
1976 case SNDRV_TIMER_IOCTL_GSTATUS:
1977 return snd_timer_user_gstatus(file, argp);
1978 case SNDRV_TIMER_IOCTL_SELECT:
1979 return snd_timer_user_tselect(file, argp);
1980 case SNDRV_TIMER_IOCTL_INFO:
1981 return snd_timer_user_info(file, argp);
1982 case SNDRV_TIMER_IOCTL_PARAMS:
1983 return snd_timer_user_params(file, argp);
1984 case SNDRV_TIMER_IOCTL_STATUS:
1985 return snd_timer_user_status(file, argp);
1986 case SNDRV_TIMER_IOCTL_START:
1987 case SNDRV_TIMER_IOCTL_START_OLD:
1988 return snd_timer_user_start(file);
1989 case SNDRV_TIMER_IOCTL_STOP:
1990 case SNDRV_TIMER_IOCTL_STOP_OLD:
1991 return snd_timer_user_stop(file);
1992 case SNDRV_TIMER_IOCTL_CONTINUE:
1993 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1994 return snd_timer_user_continue(file);
1995 case SNDRV_TIMER_IOCTL_PAUSE:
1996 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1997 return snd_timer_user_pause(file);
1999 return -ENOTTY;
2002 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
2003 unsigned long arg)
2005 struct snd_timer_user *tu = file->private_data;
2006 long ret;
2008 mutex_lock(&tu->ioctl_lock);
2009 ret = __snd_timer_user_ioctl(file, cmd, arg);
2010 mutex_unlock(&tu->ioctl_lock);
2011 return ret;
2014 static int snd_timer_user_fasync(int fd, struct file * file, int on)
2016 struct snd_timer_user *tu;
2018 tu = file->private_data;
2019 return fasync_helper(fd, file, on, &tu->fasync);
2022 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2023 size_t count, loff_t *offset)
2025 struct snd_timer_user *tu;
2026 long result = 0, unit;
2027 int qhead;
2028 int err = 0;
2030 tu = file->private_data;
2031 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
2032 mutex_lock(&tu->ioctl_lock);
2033 spin_lock_irq(&tu->qlock);
2034 while ((long)count - result >= unit) {
2035 while (!tu->qused) {
2036 wait_queue_t wait;
2038 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2039 err = -EAGAIN;
2040 goto _error;
2043 set_current_state(TASK_INTERRUPTIBLE);
2044 init_waitqueue_entry(&wait, current);
2045 add_wait_queue(&tu->qchange_sleep, &wait);
2047 spin_unlock_irq(&tu->qlock);
2048 mutex_unlock(&tu->ioctl_lock);
2049 schedule();
2050 mutex_lock(&tu->ioctl_lock);
2051 spin_lock_irq(&tu->qlock);
2053 remove_wait_queue(&tu->qchange_sleep, &wait);
2055 if (tu->disconnected) {
2056 err = -ENODEV;
2057 goto _error;
2059 if (signal_pending(current)) {
2060 err = -ERESTARTSYS;
2061 goto _error;
2065 qhead = tu->qhead++;
2066 tu->qhead %= tu->queue_size;
2067 tu->qused--;
2068 spin_unlock_irq(&tu->qlock);
2070 if (tu->tread) {
2071 if (copy_to_user(buffer, &tu->tqueue[qhead],
2072 sizeof(struct snd_timer_tread)))
2073 err = -EFAULT;
2074 } else {
2075 if (copy_to_user(buffer, &tu->queue[qhead],
2076 sizeof(struct snd_timer_read)))
2077 err = -EFAULT;
2080 spin_lock_irq(&tu->qlock);
2081 if (err < 0)
2082 goto _error;
2083 result += unit;
2084 buffer += unit;
2086 _error:
2087 spin_unlock_irq(&tu->qlock);
2088 mutex_unlock(&tu->ioctl_lock);
2089 return result > 0 ? result : err;
2092 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
2094 unsigned int mask;
2095 struct snd_timer_user *tu;
2097 tu = file->private_data;
2099 poll_wait(file, &tu->qchange_sleep, wait);
2101 mask = 0;
2102 if (tu->qused)
2103 mask |= POLLIN | POLLRDNORM;
2104 if (tu->disconnected)
2105 mask |= POLLERR;
2107 return mask;
2110 #ifdef CONFIG_COMPAT
2111 #include "timer_compat.c"
2112 #else
2113 #define snd_timer_user_ioctl_compat NULL
2114 #endif
2116 static const struct file_operations snd_timer_f_ops =
2118 .owner = THIS_MODULE,
2119 .read = snd_timer_user_read,
2120 .open = snd_timer_user_open,
2121 .release = snd_timer_user_release,
2122 .llseek = no_llseek,
2123 .poll = snd_timer_user_poll,
2124 .unlocked_ioctl = snd_timer_user_ioctl,
2125 .compat_ioctl = snd_timer_user_ioctl_compat,
2126 .fasync = snd_timer_user_fasync,
2129 /* unregister the system timer */
2130 static void snd_timer_free_all(void)
2132 struct snd_timer *timer, *n;
2134 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2135 snd_timer_free(timer);
2138 static struct device timer_dev;
2141 * ENTRY functions
2144 static int __init alsa_timer_init(void)
2146 int err;
2148 snd_device_initialize(&timer_dev, NULL);
2149 dev_set_name(&timer_dev, "timer");
2151 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2152 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2153 "system timer");
2154 #endif
2156 err = snd_timer_register_system();
2157 if (err < 0) {
2158 pr_err("ALSA: unable to register system timer (%i)\n", err);
2159 put_device(&timer_dev);
2160 return err;
2163 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2164 &snd_timer_f_ops, NULL, &timer_dev);
2165 if (err < 0) {
2166 pr_err("ALSA: unable to register timer device (%i)\n", err);
2167 snd_timer_free_all();
2168 put_device(&timer_dev);
2169 return err;
2172 snd_timer_proc_init();
2173 return 0;
2176 static void __exit alsa_timer_exit(void)
2178 snd_unregister_device(&timer_dev);
2179 snd_timer_free_all();
2180 put_device(&timer_dev);
2181 snd_timer_proc_done();
2182 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2183 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2184 #endif
2187 module_init(alsa_timer_init)
2188 module_exit(alsa_timer_exit)