ALSA: timer: Fix double unlink of active_list
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / core / timer.c
1 /*
2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 *
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
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>
37
38 #if defined(CONFIG_SND_HRTIMER) || defined(CONFIG_SND_HRTIMER_MODULE)
39 #define DEFAULT_TIMER_LIMIT 4
40 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
41 #define DEFAULT_TIMER_LIMIT 2
42 #else
43 #define DEFAULT_TIMER_LIMIT 1
44 #endif
45
46 static int timer_limit = DEFAULT_TIMER_LIMIT;
47 static int timer_tstamp_monotonic = 1;
48 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
49 MODULE_DESCRIPTION("ALSA timer interface");
50 MODULE_LICENSE("GPL");
51 module_param(timer_limit, int, 0444);
52 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
53 module_param(timer_tstamp_monotonic, int, 0444);
54 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
55
56 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
57 MODULE_ALIAS("devname:snd/timer");
58
59 struct snd_timer_user {
60 struct snd_timer_instance *timeri;
61 int tread; /* enhanced read with timestamps and events */
62 unsigned long ticks;
63 unsigned long overrun;
64 int qhead;
65 int qtail;
66 int qused;
67 int queue_size;
68 struct snd_timer_read *queue;
69 struct snd_timer_tread *tqueue;
70 spinlock_t qlock;
71 unsigned long last_resolution;
72 unsigned int filter;
73 struct timespec tstamp; /* trigger tstamp */
74 wait_queue_head_t qchange_sleep;
75 struct fasync_struct *fasync;
76 struct mutex ioctl_lock;
77 };
78
79 /* list of timers */
80 static LIST_HEAD(snd_timer_list);
81
82 /* list of slave instances */
83 static LIST_HEAD(snd_timer_slave_list);
84
85 /* lock for slave active lists */
86 static DEFINE_SPINLOCK(slave_active_lock);
87
88 static DEFINE_MUTEX(register_mutex);
89
90 static int snd_timer_free(struct snd_timer *timer);
91 static int snd_timer_dev_free(struct snd_device *device);
92 static int snd_timer_dev_register(struct snd_device *device);
93 static int snd_timer_dev_disconnect(struct snd_device *device);
94
95 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
96
97 /*
98 * create a timer instance with the given owner string.
99 * when timer is not NULL, increments the module counter
100 */
101 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
102 struct snd_timer *timer)
103 {
104 struct snd_timer_instance *timeri;
105 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
106 if (timeri == NULL)
107 return NULL;
108 timeri->owner = kstrdup(owner, GFP_KERNEL);
109 if (! timeri->owner) {
110 kfree(timeri);
111 return NULL;
112 }
113 INIT_LIST_HEAD(&timeri->open_list);
114 INIT_LIST_HEAD(&timeri->active_list);
115 INIT_LIST_HEAD(&timeri->ack_list);
116 INIT_LIST_HEAD(&timeri->slave_list_head);
117 INIT_LIST_HEAD(&timeri->slave_active_head);
118
119 timeri->timer = timer;
120 if (timer && !try_module_get(timer->module)) {
121 kfree(timeri->owner);
122 kfree(timeri);
123 return NULL;
124 }
125
126 return timeri;
127 }
128
129 /*
130 * find a timer instance from the given timer id
131 */
132 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
133 {
134 struct snd_timer *timer = NULL;
135
136 list_for_each_entry(timer, &snd_timer_list, device_list) {
137 if (timer->tmr_class != tid->dev_class)
138 continue;
139 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
140 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
141 (timer->card == NULL ||
142 timer->card->number != tid->card))
143 continue;
144 if (timer->tmr_device != tid->device)
145 continue;
146 if (timer->tmr_subdevice != tid->subdevice)
147 continue;
148 return timer;
149 }
150 return NULL;
151 }
152
153 #ifdef CONFIG_MODULES
154
155 static void snd_timer_request(struct snd_timer_id *tid)
156 {
157 switch (tid->dev_class) {
158 case SNDRV_TIMER_CLASS_GLOBAL:
159 if (tid->device < timer_limit)
160 request_module("snd-timer-%i", tid->device);
161 break;
162 case SNDRV_TIMER_CLASS_CARD:
163 case SNDRV_TIMER_CLASS_PCM:
164 if (tid->card < snd_ecards_limit)
165 request_module("snd-card-%i", tid->card);
166 break;
167 default:
168 break;
169 }
170 }
171
172 #endif
173
174 /*
175 * look for a master instance matching with the slave id of the given slave.
176 * when found, relink the open_link of the slave.
177 *
178 * call this with register_mutex down.
179 */
180 static void snd_timer_check_slave(struct snd_timer_instance *slave)
181 {
182 struct snd_timer *timer;
183 struct snd_timer_instance *master;
184
185 /* FIXME: it's really dumb to look up all entries.. */
186 list_for_each_entry(timer, &snd_timer_list, device_list) {
187 list_for_each_entry(master, &timer->open_list_head, open_list) {
188 if (slave->slave_class == master->slave_class &&
189 slave->slave_id == master->slave_id) {
190 list_move_tail(&slave->open_list,
191 &master->slave_list_head);
192 spin_lock_irq(&slave_active_lock);
193 slave->master = master;
194 slave->timer = master->timer;
195 spin_unlock_irq(&slave_active_lock);
196 return;
197 }
198 }
199 }
200 }
201
202 /*
203 * look for slave instances matching with the slave id of the given master.
204 * when found, relink the open_link of slaves.
205 *
206 * call this with register_mutex down.
207 */
208 static void snd_timer_check_master(struct snd_timer_instance *master)
209 {
210 struct snd_timer_instance *slave, *tmp;
211
212 /* check all pending slaves */
213 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
214 if (slave->slave_class == master->slave_class &&
215 slave->slave_id == master->slave_id) {
216 list_move_tail(&slave->open_list, &master->slave_list_head);
217 spin_lock_irq(&slave_active_lock);
218 spin_lock(&master->timer->lock);
219 slave->master = master;
220 slave->timer = master->timer;
221 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
222 list_add_tail(&slave->active_list,
223 &master->slave_active_head);
224 spin_unlock(&master->timer->lock);
225 spin_unlock_irq(&slave_active_lock);
226 }
227 }
228 }
229
230 /*
231 * open a timer instance
232 * when opening a master, the slave id must be here given.
233 */
234 int snd_timer_open(struct snd_timer_instance **ti,
235 char *owner, struct snd_timer_id *tid,
236 unsigned int slave_id)
237 {
238 struct snd_timer *timer;
239 struct snd_timer_instance *timeri = NULL;
240
241 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
242 /* open a slave instance */
243 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
244 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
245 snd_printd("invalid slave class %i\n", tid->dev_sclass);
246 return -EINVAL;
247 }
248 mutex_lock(&register_mutex);
249 timeri = snd_timer_instance_new(owner, NULL);
250 if (!timeri) {
251 mutex_unlock(&register_mutex);
252 return -ENOMEM;
253 }
254 timeri->slave_class = tid->dev_sclass;
255 timeri->slave_id = tid->device;
256 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
257 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
258 snd_timer_check_slave(timeri);
259 mutex_unlock(&register_mutex);
260 *ti = timeri;
261 return 0;
262 }
263
264 /* open a master instance */
265 mutex_lock(&register_mutex);
266 timer = snd_timer_find(tid);
267 #ifdef CONFIG_MODULES
268 if (!timer) {
269 mutex_unlock(&register_mutex);
270 snd_timer_request(tid);
271 mutex_lock(&register_mutex);
272 timer = snd_timer_find(tid);
273 }
274 #endif
275 if (!timer) {
276 mutex_unlock(&register_mutex);
277 return -ENODEV;
278 }
279 if (!list_empty(&timer->open_list_head)) {
280 timeri = list_entry(timer->open_list_head.next,
281 struct snd_timer_instance, open_list);
282 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
283 mutex_unlock(&register_mutex);
284 return -EBUSY;
285 }
286 }
287 timeri = snd_timer_instance_new(owner, timer);
288 if (!timeri) {
289 mutex_unlock(&register_mutex);
290 return -ENOMEM;
291 }
292 timeri->slave_class = tid->dev_sclass;
293 timeri->slave_id = slave_id;
294 if (list_empty(&timer->open_list_head) && timer->hw.open)
295 timer->hw.open(timer);
296 list_add_tail(&timeri->open_list, &timer->open_list_head);
297 snd_timer_check_master(timeri);
298 mutex_unlock(&register_mutex);
299 *ti = timeri;
300 return 0;
301 }
302
303 static int _snd_timer_stop(struct snd_timer_instance *timeri,
304 int keep_flag, int event);
305
306 /*
307 * close a timer instance
308 */
309 int snd_timer_close(struct snd_timer_instance *timeri)
310 {
311 struct snd_timer *timer = NULL;
312 struct snd_timer_instance *slave, *tmp;
313
314 if (snd_BUG_ON(!timeri))
315 return -ENXIO;
316
317 /* force to stop the timer */
318 snd_timer_stop(timeri);
319
320 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
321 /* wait, until the active callback is finished */
322 spin_lock_irq(&slave_active_lock);
323 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
324 spin_unlock_irq(&slave_active_lock);
325 udelay(10);
326 spin_lock_irq(&slave_active_lock);
327 }
328 spin_unlock_irq(&slave_active_lock);
329 mutex_lock(&register_mutex);
330 list_del(&timeri->open_list);
331 mutex_unlock(&register_mutex);
332 } else {
333 timer = timeri->timer;
334 if (snd_BUG_ON(!timer))
335 goto out;
336 /* wait, until the active callback is finished */
337 spin_lock_irq(&timer->lock);
338 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
339 spin_unlock_irq(&timer->lock);
340 udelay(10);
341 spin_lock_irq(&timer->lock);
342 }
343 spin_unlock_irq(&timer->lock);
344 mutex_lock(&register_mutex);
345 list_del(&timeri->open_list);
346 if (timer && list_empty(&timer->open_list_head) &&
347 timer->hw.close)
348 timer->hw.close(timer);
349 /* remove slave links */
350 spin_lock_irq(&slave_active_lock);
351 spin_lock(&timer->lock);
352 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
353 open_list) {
354 list_move_tail(&slave->open_list, &snd_timer_slave_list);
355 slave->master = NULL;
356 slave->timer = NULL;
357 list_del_init(&slave->ack_list);
358 list_del_init(&slave->active_list);
359 }
360 spin_unlock(&timer->lock);
361 spin_unlock_irq(&slave_active_lock);
362 mutex_unlock(&register_mutex);
363 }
364 out:
365 if (timeri->private_free)
366 timeri->private_free(timeri);
367 kfree(timeri->owner);
368 kfree(timeri);
369 if (timer)
370 module_put(timer->module);
371 return 0;
372 }
373
374 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
375 {
376 struct snd_timer * timer;
377
378 if (timeri == NULL)
379 return 0;
380 if ((timer = timeri->timer) != NULL) {
381 if (timer->hw.c_resolution)
382 return timer->hw.c_resolution(timer);
383 return timer->hw.resolution;
384 }
385 return 0;
386 }
387
388 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
389 {
390 struct snd_timer *timer;
391 unsigned long flags;
392 unsigned long resolution = 0;
393 struct snd_timer_instance *ts;
394 struct timespec tstamp;
395
396 if (timer_tstamp_monotonic)
397 do_posix_clock_monotonic_gettime(&tstamp);
398 else
399 getnstimeofday(&tstamp);
400 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
401 event > SNDRV_TIMER_EVENT_PAUSE))
402 return;
403 if (event == SNDRV_TIMER_EVENT_START ||
404 event == SNDRV_TIMER_EVENT_CONTINUE)
405 resolution = snd_timer_resolution(ti);
406 if (ti->ccallback)
407 ti->ccallback(ti, event, &tstamp, resolution);
408 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
409 return;
410 timer = ti->timer;
411 if (timer == NULL)
412 return;
413 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
414 return;
415 spin_lock_irqsave(&timer->lock, flags);
416 list_for_each_entry(ts, &ti->slave_active_head, active_list)
417 if (ts->ccallback)
418 ts->ccallback(ti, event + 100, &tstamp, resolution);
419 spin_unlock_irqrestore(&timer->lock, flags);
420 }
421
422 static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
423 unsigned long sticks)
424 {
425 list_move_tail(&timeri->active_list, &timer->active_list_head);
426 if (timer->running) {
427 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
428 goto __start_now;
429 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
430 timeri->flags |= SNDRV_TIMER_IFLG_START;
431 return 1; /* delayed start */
432 } else {
433 timer->sticks = sticks;
434 timer->hw.start(timer);
435 __start_now:
436 timer->running++;
437 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
438 return 0;
439 }
440 }
441
442 static int snd_timer_start_slave(struct snd_timer_instance *timeri)
443 {
444 unsigned long flags;
445
446 spin_lock_irqsave(&slave_active_lock, flags);
447 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
448 if (timeri->master && timeri->timer) {
449 spin_lock(&timeri->timer->lock);
450 list_add_tail(&timeri->active_list,
451 &timeri->master->slave_active_head);
452 spin_unlock(&timeri->timer->lock);
453 }
454 spin_unlock_irqrestore(&slave_active_lock, flags);
455 return 1; /* delayed start */
456 }
457
458 /*
459 * start the timer instance
460 */
461 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
462 {
463 struct snd_timer *timer;
464 int result = -EINVAL;
465 unsigned long flags;
466
467 if (timeri == NULL || ticks < 1)
468 return -EINVAL;
469 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
470 result = snd_timer_start_slave(timeri);
471 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
472 return result;
473 }
474 timer = timeri->timer;
475 if (timer == NULL)
476 return -EINVAL;
477 spin_lock_irqsave(&timer->lock, flags);
478 timeri->ticks = timeri->cticks = ticks;
479 timeri->pticks = 0;
480 result = snd_timer_start1(timer, timeri, ticks);
481 spin_unlock_irqrestore(&timer->lock, flags);
482 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
483 return result;
484 }
485
486 static int _snd_timer_stop(struct snd_timer_instance * timeri,
487 int keep_flag, int event)
488 {
489 struct snd_timer *timer;
490 unsigned long flags;
491
492 if (snd_BUG_ON(!timeri))
493 return -ENXIO;
494
495 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
496 if (!keep_flag) {
497 spin_lock_irqsave(&slave_active_lock, flags);
498 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
499 list_del_init(&timeri->ack_list);
500 list_del_init(&timeri->active_list);
501 spin_unlock_irqrestore(&slave_active_lock, flags);
502 }
503 goto __end;
504 }
505 timer = timeri->timer;
506 if (!timer)
507 return -EINVAL;
508 spin_lock_irqsave(&timer->lock, flags);
509 list_del_init(&timeri->ack_list);
510 list_del_init(&timeri->active_list);
511 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
512 !(--timer->running)) {
513 timer->hw.stop(timer);
514 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
515 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
516 snd_timer_reschedule(timer, 0);
517 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
518 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
519 timer->hw.start(timer);
520 }
521 }
522 }
523 if (!keep_flag)
524 timeri->flags &=
525 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
526 spin_unlock_irqrestore(&timer->lock, flags);
527 __end:
528 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
529 snd_timer_notify1(timeri, event);
530 return 0;
531 }
532
533 /*
534 * stop the timer instance.
535 *
536 * do not call this from the timer callback!
537 */
538 int snd_timer_stop(struct snd_timer_instance *timeri)
539 {
540 struct snd_timer *timer;
541 unsigned long flags;
542 int err;
543
544 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
545 if (err < 0)
546 return err;
547 timer = timeri->timer;
548 if (!timer)
549 return -EINVAL;
550 spin_lock_irqsave(&timer->lock, flags);
551 timeri->cticks = timeri->ticks;
552 timeri->pticks = 0;
553 spin_unlock_irqrestore(&timer->lock, flags);
554 return 0;
555 }
556
557 /*
558 * start again.. the tick is kept.
559 */
560 int snd_timer_continue(struct snd_timer_instance *timeri)
561 {
562 struct snd_timer *timer;
563 int result = -EINVAL;
564 unsigned long flags;
565
566 if (timeri == NULL)
567 return result;
568 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
569 return snd_timer_start_slave(timeri);
570 timer = timeri->timer;
571 if (! timer)
572 return -EINVAL;
573 spin_lock_irqsave(&timer->lock, flags);
574 if (!timeri->cticks)
575 timeri->cticks = 1;
576 timeri->pticks = 0;
577 result = snd_timer_start1(timer, timeri, timer->sticks);
578 spin_unlock_irqrestore(&timer->lock, flags);
579 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
580 return result;
581 }
582
583 /*
584 * pause.. remember the ticks left
585 */
586 int snd_timer_pause(struct snd_timer_instance * timeri)
587 {
588 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
589 }
590
591 /*
592 * reschedule the timer
593 *
594 * start pending instances and check the scheduling ticks.
595 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
596 */
597 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
598 {
599 struct snd_timer_instance *ti;
600 unsigned long ticks = ~0UL;
601
602 list_for_each_entry(ti, &timer->active_list_head, active_list) {
603 if (ti->flags & SNDRV_TIMER_IFLG_START) {
604 ti->flags &= ~SNDRV_TIMER_IFLG_START;
605 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
606 timer->running++;
607 }
608 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
609 if (ticks > ti->cticks)
610 ticks = ti->cticks;
611 }
612 }
613 if (ticks == ~0UL) {
614 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
615 return;
616 }
617 if (ticks > timer->hw.ticks)
618 ticks = timer->hw.ticks;
619 if (ticks_left != ticks)
620 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
621 timer->sticks = ticks;
622 }
623
624 /*
625 * timer tasklet
626 *
627 */
628 static void snd_timer_tasklet(unsigned long arg)
629 {
630 struct snd_timer *timer = (struct snd_timer *) arg;
631 struct snd_timer_instance *ti;
632 struct list_head *p;
633 unsigned long resolution, ticks;
634 unsigned long flags;
635
636 spin_lock_irqsave(&timer->lock, flags);
637 /* now process all callbacks */
638 while (!list_empty(&timer->sack_list_head)) {
639 p = timer->sack_list_head.next; /* get first item */
640 ti = list_entry(p, struct snd_timer_instance, ack_list);
641
642 /* remove from ack_list and make empty */
643 list_del_init(p);
644
645 ticks = ti->pticks;
646 ti->pticks = 0;
647 resolution = ti->resolution;
648
649 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
650 spin_unlock(&timer->lock);
651 if (ti->callback)
652 ti->callback(ti, resolution, ticks);
653 spin_lock(&timer->lock);
654 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
655 }
656 spin_unlock_irqrestore(&timer->lock, flags);
657 }
658
659 /*
660 * timer interrupt
661 *
662 * ticks_left is usually equal to timer->sticks.
663 *
664 */
665 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
666 {
667 struct snd_timer_instance *ti, *ts, *tmp;
668 unsigned long resolution, ticks;
669 struct list_head *p, *ack_list_head;
670 unsigned long flags;
671 int use_tasklet = 0;
672
673 if (timer == NULL)
674 return;
675
676 spin_lock_irqsave(&timer->lock, flags);
677
678 /* remember the current resolution */
679 if (timer->hw.c_resolution)
680 resolution = timer->hw.c_resolution(timer);
681 else
682 resolution = timer->hw.resolution;
683
684 /* loop for all active instances
685 * Here we cannot use list_for_each_entry because the active_list of a
686 * processed instance is relinked to done_list_head before the callback
687 * is called.
688 */
689 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
690 active_list) {
691 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
692 continue;
693 ti->pticks += ticks_left;
694 ti->resolution = resolution;
695 if (ti->cticks < ticks_left)
696 ti->cticks = 0;
697 else
698 ti->cticks -= ticks_left;
699 if (ti->cticks) /* not expired */
700 continue;
701 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
702 ti->cticks = ti->ticks;
703 } else {
704 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
705 if (--timer->running)
706 list_del_init(&ti->active_list);
707 }
708 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
709 (ti->flags & SNDRV_TIMER_IFLG_FAST))
710 ack_list_head = &timer->ack_list_head;
711 else
712 ack_list_head = &timer->sack_list_head;
713 if (list_empty(&ti->ack_list))
714 list_add_tail(&ti->ack_list, ack_list_head);
715 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
716 ts->pticks = ti->pticks;
717 ts->resolution = resolution;
718 if (list_empty(&ts->ack_list))
719 list_add_tail(&ts->ack_list, ack_list_head);
720 }
721 }
722 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
723 snd_timer_reschedule(timer, timer->sticks);
724 if (timer->running) {
725 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
726 timer->hw.stop(timer);
727 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
728 }
729 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
730 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
731 /* restart timer */
732 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
733 timer->hw.start(timer);
734 }
735 } else {
736 timer->hw.stop(timer);
737 }
738
739 /* now process all fast callbacks */
740 while (!list_empty(&timer->ack_list_head)) {
741 p = timer->ack_list_head.next; /* get first item */
742 ti = list_entry(p, struct snd_timer_instance, ack_list);
743
744 /* remove from ack_list and make empty */
745 list_del_init(p);
746
747 ticks = ti->pticks;
748 ti->pticks = 0;
749
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;
756 }
757
758 /* do we have any slow callbacks? */
759 use_tasklet = !list_empty(&timer->sack_list_head);
760 spin_unlock_irqrestore(&timer->lock, flags);
761
762 if (use_tasklet)
763 tasklet_schedule(&timer->task_queue);
764 }
765
766 /*
767
768 */
769
770 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
771 struct snd_timer **rtimer)
772 {
773 struct snd_timer *timer;
774 int err;
775 static struct snd_device_ops ops = {
776 .dev_free = snd_timer_dev_free,
777 .dev_register = snd_timer_dev_register,
778 .dev_disconnect = snd_timer_dev_disconnect,
779 };
780
781 if (snd_BUG_ON(!tid))
782 return -EINVAL;
783 if (rtimer)
784 *rtimer = NULL;
785 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
786 if (timer == NULL) {
787 snd_printk(KERN_ERR "timer: cannot allocate\n");
788 return -ENOMEM;
789 }
790 timer->tmr_class = tid->dev_class;
791 timer->card = card;
792 timer->tmr_device = tid->device;
793 timer->tmr_subdevice = tid->subdevice;
794 if (id)
795 strlcpy(timer->id, id, sizeof(timer->id));
796 INIT_LIST_HEAD(&timer->device_list);
797 INIT_LIST_HEAD(&timer->open_list_head);
798 INIT_LIST_HEAD(&timer->active_list_head);
799 INIT_LIST_HEAD(&timer->ack_list_head);
800 INIT_LIST_HEAD(&timer->sack_list_head);
801 spin_lock_init(&timer->lock);
802 tasklet_init(&timer->task_queue, snd_timer_tasklet,
803 (unsigned long)timer);
804 if (card != NULL) {
805 timer->module = card->module;
806 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
807 if (err < 0) {
808 snd_timer_free(timer);
809 return err;
810 }
811 }
812 if (rtimer)
813 *rtimer = timer;
814 return 0;
815 }
816
817 static int snd_timer_free(struct snd_timer *timer)
818 {
819 if (!timer)
820 return 0;
821
822 mutex_lock(&register_mutex);
823 if (! list_empty(&timer->open_list_head)) {
824 struct list_head *p, *n;
825 struct snd_timer_instance *ti;
826 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
827 list_for_each_safe(p, n, &timer->open_list_head) {
828 list_del_init(p);
829 ti = list_entry(p, struct snd_timer_instance, open_list);
830 ti->timer = NULL;
831 }
832 }
833 list_del(&timer->device_list);
834 mutex_unlock(&register_mutex);
835
836 if (timer->private_free)
837 timer->private_free(timer);
838 kfree(timer);
839 return 0;
840 }
841
842 static int snd_timer_dev_free(struct snd_device *device)
843 {
844 struct snd_timer *timer = device->device_data;
845 return snd_timer_free(timer);
846 }
847
848 static int snd_timer_dev_register(struct snd_device *dev)
849 {
850 struct snd_timer *timer = dev->device_data;
851 struct snd_timer *timer1;
852
853 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
854 return -ENXIO;
855 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
856 !timer->hw.resolution && timer->hw.c_resolution == NULL)
857 return -EINVAL;
858
859 mutex_lock(&register_mutex);
860 list_for_each_entry(timer1, &snd_timer_list, device_list) {
861 if (timer1->tmr_class > timer->tmr_class)
862 break;
863 if (timer1->tmr_class < timer->tmr_class)
864 continue;
865 if (timer1->card && timer->card) {
866 if (timer1->card->number > timer->card->number)
867 break;
868 if (timer1->card->number < timer->card->number)
869 continue;
870 }
871 if (timer1->tmr_device > timer->tmr_device)
872 break;
873 if (timer1->tmr_device < timer->tmr_device)
874 continue;
875 if (timer1->tmr_subdevice > timer->tmr_subdevice)
876 break;
877 if (timer1->tmr_subdevice < timer->tmr_subdevice)
878 continue;
879 /* conflicts.. */
880 mutex_unlock(&register_mutex);
881 return -EBUSY;
882 }
883 list_add_tail(&timer->device_list, &timer1->device_list);
884 mutex_unlock(&register_mutex);
885 return 0;
886 }
887
888 static int snd_timer_dev_disconnect(struct snd_device *device)
889 {
890 struct snd_timer *timer = device->device_data;
891 mutex_lock(&register_mutex);
892 list_del_init(&timer->device_list);
893 mutex_unlock(&register_mutex);
894 return 0;
895 }
896
897 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
898 {
899 unsigned long flags;
900 unsigned long resolution = 0;
901 struct snd_timer_instance *ti, *ts;
902
903 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
904 return;
905 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
906 event > SNDRV_TIMER_EVENT_MRESUME))
907 return;
908 spin_lock_irqsave(&timer->lock, flags);
909 if (event == SNDRV_TIMER_EVENT_MSTART ||
910 event == SNDRV_TIMER_EVENT_MCONTINUE ||
911 event == SNDRV_TIMER_EVENT_MRESUME) {
912 if (timer->hw.c_resolution)
913 resolution = timer->hw.c_resolution(timer);
914 else
915 resolution = timer->hw.resolution;
916 }
917 list_for_each_entry(ti, &timer->active_list_head, active_list) {
918 if (ti->ccallback)
919 ti->ccallback(ti, event, tstamp, resolution);
920 list_for_each_entry(ts, &ti->slave_active_head, active_list)
921 if (ts->ccallback)
922 ts->ccallback(ts, event, tstamp, resolution);
923 }
924 spin_unlock_irqrestore(&timer->lock, flags);
925 }
926
927 /*
928 * exported functions for global timers
929 */
930 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
931 {
932 struct snd_timer_id tid;
933
934 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
935 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
936 tid.card = -1;
937 tid.device = device;
938 tid.subdevice = 0;
939 return snd_timer_new(NULL, id, &tid, rtimer);
940 }
941
942 int snd_timer_global_free(struct snd_timer *timer)
943 {
944 return snd_timer_free(timer);
945 }
946
947 int snd_timer_global_register(struct snd_timer *timer)
948 {
949 struct snd_device dev;
950
951 memset(&dev, 0, sizeof(dev));
952 dev.device_data = timer;
953 return snd_timer_dev_register(&dev);
954 }
955
956 /*
957 * System timer
958 */
959
960 struct snd_timer_system_private {
961 struct timer_list tlist;
962 unsigned long last_expires;
963 unsigned long last_jiffies;
964 unsigned long correction;
965 };
966
967 static void snd_timer_s_function(unsigned long data)
968 {
969 struct snd_timer *timer = (struct snd_timer *)data;
970 struct snd_timer_system_private *priv = timer->private_data;
971 unsigned long jiff = jiffies;
972 if (time_after(jiff, priv->last_expires))
973 priv->correction += (long)jiff - (long)priv->last_expires;
974 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
975 }
976
977 static int snd_timer_s_start(struct snd_timer * timer)
978 {
979 struct snd_timer_system_private *priv;
980 unsigned long njiff;
981
982 priv = (struct snd_timer_system_private *) timer->private_data;
983 njiff = (priv->last_jiffies = jiffies);
984 if (priv->correction > timer->sticks - 1) {
985 priv->correction -= timer->sticks - 1;
986 njiff++;
987 } else {
988 njiff += timer->sticks - priv->correction;
989 priv->correction = 0;
990 }
991 priv->last_expires = priv->tlist.expires = njiff;
992 add_timer(&priv->tlist);
993 return 0;
994 }
995
996 static int snd_timer_s_stop(struct snd_timer * timer)
997 {
998 struct snd_timer_system_private *priv;
999 unsigned long jiff;
1000
1001 priv = (struct snd_timer_system_private *) timer->private_data;
1002 del_timer(&priv->tlist);
1003 jiff = jiffies;
1004 if (time_before(jiff, priv->last_expires))
1005 timer->sticks = priv->last_expires - jiff;
1006 else
1007 timer->sticks = 1;
1008 priv->correction = 0;
1009 return 0;
1010 }
1011
1012 static struct snd_timer_hardware snd_timer_system =
1013 {
1014 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1015 .resolution = 1000000000L / HZ,
1016 .ticks = 10000000L,
1017 .start = snd_timer_s_start,
1018 .stop = snd_timer_s_stop
1019 };
1020
1021 static void snd_timer_free_system(struct snd_timer *timer)
1022 {
1023 kfree(timer->private_data);
1024 }
1025
1026 static int snd_timer_register_system(void)
1027 {
1028 struct snd_timer *timer;
1029 struct snd_timer_system_private *priv;
1030 int err;
1031
1032 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1033 if (err < 0)
1034 return err;
1035 strcpy(timer->name, "system timer");
1036 timer->hw = snd_timer_system;
1037 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1038 if (priv == NULL) {
1039 snd_timer_free(timer);
1040 return -ENOMEM;
1041 }
1042 init_timer(&priv->tlist);
1043 priv->tlist.function = snd_timer_s_function;
1044 priv->tlist.data = (unsigned long) timer;
1045 timer->private_data = priv;
1046 timer->private_free = snd_timer_free_system;
1047 return snd_timer_global_register(timer);
1048 }
1049
1050 #ifdef CONFIG_PROC_FS
1051 /*
1052 * Info interface
1053 */
1054
1055 static void snd_timer_proc_read(struct snd_info_entry *entry,
1056 struct snd_info_buffer *buffer)
1057 {
1058 struct snd_timer *timer;
1059 struct snd_timer_instance *ti;
1060
1061 mutex_lock(&register_mutex);
1062 list_for_each_entry(timer, &snd_timer_list, device_list) {
1063 switch (timer->tmr_class) {
1064 case SNDRV_TIMER_CLASS_GLOBAL:
1065 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1066 break;
1067 case SNDRV_TIMER_CLASS_CARD:
1068 snd_iprintf(buffer, "C%i-%i: ",
1069 timer->card->number, timer->tmr_device);
1070 break;
1071 case SNDRV_TIMER_CLASS_PCM:
1072 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1073 timer->tmr_device, timer->tmr_subdevice);
1074 break;
1075 default:
1076 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1077 timer->card ? timer->card->number : -1,
1078 timer->tmr_device, timer->tmr_subdevice);
1079 }
1080 snd_iprintf(buffer, "%s :", timer->name);
1081 if (timer->hw.resolution)
1082 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1083 timer->hw.resolution / 1000,
1084 timer->hw.resolution % 1000,
1085 timer->hw.ticks);
1086 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1087 snd_iprintf(buffer, " SLAVE");
1088 snd_iprintf(buffer, "\n");
1089 list_for_each_entry(ti, &timer->open_list_head, open_list)
1090 snd_iprintf(buffer, " Client %s : %s\n",
1091 ti->owner ? ti->owner : "unknown",
1092 ti->flags & (SNDRV_TIMER_IFLG_START |
1093 SNDRV_TIMER_IFLG_RUNNING)
1094 ? "running" : "stopped");
1095 }
1096 mutex_unlock(&register_mutex);
1097 }
1098
1099 static struct snd_info_entry *snd_timer_proc_entry;
1100
1101 static void __init snd_timer_proc_init(void)
1102 {
1103 struct snd_info_entry *entry;
1104
1105 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1106 if (entry != NULL) {
1107 entry->c.text.read = snd_timer_proc_read;
1108 if (snd_info_register(entry) < 0) {
1109 snd_info_free_entry(entry);
1110 entry = NULL;
1111 }
1112 }
1113 snd_timer_proc_entry = entry;
1114 }
1115
1116 static void __exit snd_timer_proc_done(void)
1117 {
1118 snd_info_free_entry(snd_timer_proc_entry);
1119 }
1120 #else /* !CONFIG_PROC_FS */
1121 #define snd_timer_proc_init()
1122 #define snd_timer_proc_done()
1123 #endif
1124
1125 /*
1126 * USER SPACE interface
1127 */
1128
1129 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1130 unsigned long resolution,
1131 unsigned long ticks)
1132 {
1133 struct snd_timer_user *tu = timeri->callback_data;
1134 struct snd_timer_read *r;
1135 int prev;
1136
1137 spin_lock(&tu->qlock);
1138 if (tu->qused > 0) {
1139 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1140 r = &tu->queue[prev];
1141 if (r->resolution == resolution) {
1142 r->ticks += ticks;
1143 goto __wake;
1144 }
1145 }
1146 if (tu->qused >= tu->queue_size) {
1147 tu->overrun++;
1148 } else {
1149 r = &tu->queue[tu->qtail++];
1150 tu->qtail %= tu->queue_size;
1151 r->resolution = resolution;
1152 r->ticks = ticks;
1153 tu->qused++;
1154 }
1155 __wake:
1156 spin_unlock(&tu->qlock);
1157 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1158 wake_up(&tu->qchange_sleep);
1159 }
1160
1161 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1162 struct snd_timer_tread *tread)
1163 {
1164 if (tu->qused >= tu->queue_size) {
1165 tu->overrun++;
1166 } else {
1167 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1168 tu->qtail %= tu->queue_size;
1169 tu->qused++;
1170 }
1171 }
1172
1173 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1174 int event,
1175 struct timespec *tstamp,
1176 unsigned long resolution)
1177 {
1178 struct snd_timer_user *tu = timeri->callback_data;
1179 struct snd_timer_tread r1;
1180 unsigned long flags;
1181
1182 if (event >= SNDRV_TIMER_EVENT_START &&
1183 event <= SNDRV_TIMER_EVENT_PAUSE)
1184 tu->tstamp = *tstamp;
1185 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1186 return;
1187 r1.event = event;
1188 r1.tstamp = *tstamp;
1189 r1.val = resolution;
1190 spin_lock_irqsave(&tu->qlock, flags);
1191 snd_timer_user_append_to_tqueue(tu, &r1);
1192 spin_unlock_irqrestore(&tu->qlock, flags);
1193 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1194 wake_up(&tu->qchange_sleep);
1195 }
1196
1197 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1198 unsigned long resolution,
1199 unsigned long ticks)
1200 {
1201 struct snd_timer_user *tu = timeri->callback_data;
1202 struct snd_timer_tread *r, r1;
1203 struct timespec tstamp;
1204 int prev, append = 0;
1205
1206 memset(&tstamp, 0, sizeof(tstamp));
1207 spin_lock(&tu->qlock);
1208 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1209 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1210 spin_unlock(&tu->qlock);
1211 return;
1212 }
1213 if (tu->last_resolution != resolution || ticks > 0) {
1214 if (timer_tstamp_monotonic)
1215 do_posix_clock_monotonic_gettime(&tstamp);
1216 else
1217 getnstimeofday(&tstamp);
1218 }
1219 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1220 tu->last_resolution != resolution) {
1221 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1222 r1.tstamp = tstamp;
1223 r1.val = resolution;
1224 snd_timer_user_append_to_tqueue(tu, &r1);
1225 tu->last_resolution = resolution;
1226 append++;
1227 }
1228 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1229 goto __wake;
1230 if (ticks == 0)
1231 goto __wake;
1232 if (tu->qused > 0) {
1233 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1234 r = &tu->tqueue[prev];
1235 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1236 r->tstamp = tstamp;
1237 r->val += ticks;
1238 append++;
1239 goto __wake;
1240 }
1241 }
1242 r1.event = SNDRV_TIMER_EVENT_TICK;
1243 r1.tstamp = tstamp;
1244 r1.val = ticks;
1245 snd_timer_user_append_to_tqueue(tu, &r1);
1246 append++;
1247 __wake:
1248 spin_unlock(&tu->qlock);
1249 if (append == 0)
1250 return;
1251 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1252 wake_up(&tu->qchange_sleep);
1253 }
1254
1255 static int snd_timer_user_open(struct inode *inode, struct file *file)
1256 {
1257 struct snd_timer_user *tu;
1258 int err;
1259
1260 err = nonseekable_open(inode, file);
1261 if (err < 0)
1262 return err;
1263
1264 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1265 if (tu == NULL)
1266 return -ENOMEM;
1267 spin_lock_init(&tu->qlock);
1268 init_waitqueue_head(&tu->qchange_sleep);
1269 mutex_init(&tu->ioctl_lock);
1270 tu->ticks = 1;
1271 tu->queue_size = 128;
1272 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1273 GFP_KERNEL);
1274 if (tu->queue == NULL) {
1275 kfree(tu);
1276 return -ENOMEM;
1277 }
1278 file->private_data = tu;
1279 return 0;
1280 }
1281
1282 static int snd_timer_user_release(struct inode *inode, struct file *file)
1283 {
1284 struct snd_timer_user *tu;
1285
1286 if (file->private_data) {
1287 tu = file->private_data;
1288 file->private_data = NULL;
1289 mutex_lock(&tu->ioctl_lock);
1290 if (tu->timeri)
1291 snd_timer_close(tu->timeri);
1292 mutex_unlock(&tu->ioctl_lock);
1293 kfree(tu->queue);
1294 kfree(tu->tqueue);
1295 kfree(tu);
1296 }
1297 return 0;
1298 }
1299
1300 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1301 {
1302 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1303 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1304 id->card = -1;
1305 id->device = -1;
1306 id->subdevice = -1;
1307 }
1308
1309 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1310 {
1311 id->dev_class = timer->tmr_class;
1312 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1313 id->card = timer->card ? timer->card->number : -1;
1314 id->device = timer->tmr_device;
1315 id->subdevice = timer->tmr_subdevice;
1316 }
1317
1318 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1319 {
1320 struct snd_timer_id id;
1321 struct snd_timer *timer;
1322 struct list_head *p;
1323
1324 if (copy_from_user(&id, _tid, sizeof(id)))
1325 return -EFAULT;
1326 mutex_lock(&register_mutex);
1327 if (id.dev_class < 0) { /* first item */
1328 if (list_empty(&snd_timer_list))
1329 snd_timer_user_zero_id(&id);
1330 else {
1331 timer = list_entry(snd_timer_list.next,
1332 struct snd_timer, device_list);
1333 snd_timer_user_copy_id(&id, timer);
1334 }
1335 } else {
1336 switch (id.dev_class) {
1337 case SNDRV_TIMER_CLASS_GLOBAL:
1338 id.device = id.device < 0 ? 0 : id.device + 1;
1339 list_for_each(p, &snd_timer_list) {
1340 timer = list_entry(p, struct snd_timer, device_list);
1341 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1342 snd_timer_user_copy_id(&id, timer);
1343 break;
1344 }
1345 if (timer->tmr_device >= id.device) {
1346 snd_timer_user_copy_id(&id, timer);
1347 break;
1348 }
1349 }
1350 if (p == &snd_timer_list)
1351 snd_timer_user_zero_id(&id);
1352 break;
1353 case SNDRV_TIMER_CLASS_CARD:
1354 case SNDRV_TIMER_CLASS_PCM:
1355 if (id.card < 0) {
1356 id.card = 0;
1357 } else {
1358 if (id.card < 0) {
1359 id.card = 0;
1360 } else {
1361 if (id.device < 0) {
1362 id.device = 0;
1363 } else {
1364 if (id.subdevice < 0) {
1365 id.subdevice = 0;
1366 } else {
1367 id.subdevice++;
1368 }
1369 }
1370 }
1371 }
1372 list_for_each(p, &snd_timer_list) {
1373 timer = list_entry(p, struct snd_timer, device_list);
1374 if (timer->tmr_class > id.dev_class) {
1375 snd_timer_user_copy_id(&id, timer);
1376 break;
1377 }
1378 if (timer->tmr_class < id.dev_class)
1379 continue;
1380 if (timer->card->number > id.card) {
1381 snd_timer_user_copy_id(&id, timer);
1382 break;
1383 }
1384 if (timer->card->number < id.card)
1385 continue;
1386 if (timer->tmr_device > id.device) {
1387 snd_timer_user_copy_id(&id, timer);
1388 break;
1389 }
1390 if (timer->tmr_device < id.device)
1391 continue;
1392 if (timer->tmr_subdevice > id.subdevice) {
1393 snd_timer_user_copy_id(&id, timer);
1394 break;
1395 }
1396 if (timer->tmr_subdevice < id.subdevice)
1397 continue;
1398 snd_timer_user_copy_id(&id, timer);
1399 break;
1400 }
1401 if (p == &snd_timer_list)
1402 snd_timer_user_zero_id(&id);
1403 break;
1404 default:
1405 snd_timer_user_zero_id(&id);
1406 }
1407 }
1408 mutex_unlock(&register_mutex);
1409 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1410 return -EFAULT;
1411 return 0;
1412 }
1413
1414 static int snd_timer_user_ginfo(struct file *file,
1415 struct snd_timer_ginfo __user *_ginfo)
1416 {
1417 struct snd_timer_ginfo *ginfo;
1418 struct snd_timer_id tid;
1419 struct snd_timer *t;
1420 struct list_head *p;
1421 int err = 0;
1422
1423 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1424 if (IS_ERR(ginfo))
1425 return PTR_ERR(ginfo);
1426
1427 tid = ginfo->tid;
1428 memset(ginfo, 0, sizeof(*ginfo));
1429 ginfo->tid = tid;
1430 mutex_lock(&register_mutex);
1431 t = snd_timer_find(&tid);
1432 if (t != NULL) {
1433 ginfo->card = t->card ? t->card->number : -1;
1434 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1435 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1436 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1437 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1438 ginfo->resolution = t->hw.resolution;
1439 if (t->hw.resolution_min > 0) {
1440 ginfo->resolution_min = t->hw.resolution_min;
1441 ginfo->resolution_max = t->hw.resolution_max;
1442 }
1443 list_for_each(p, &t->open_list_head) {
1444 ginfo->clients++;
1445 }
1446 } else {
1447 err = -ENODEV;
1448 }
1449 mutex_unlock(&register_mutex);
1450 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1451 err = -EFAULT;
1452 kfree(ginfo);
1453 return err;
1454 }
1455
1456 static int snd_timer_user_gparams(struct file *file,
1457 struct snd_timer_gparams __user *_gparams)
1458 {
1459 struct snd_timer_gparams gparams;
1460 struct snd_timer *t;
1461 int err;
1462
1463 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1464 return -EFAULT;
1465 mutex_lock(&register_mutex);
1466 t = snd_timer_find(&gparams.tid);
1467 if (!t) {
1468 err = -ENODEV;
1469 goto _error;
1470 }
1471 if (!list_empty(&t->open_list_head)) {
1472 err = -EBUSY;
1473 goto _error;
1474 }
1475 if (!t->hw.set_period) {
1476 err = -ENOSYS;
1477 goto _error;
1478 }
1479 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1480 _error:
1481 mutex_unlock(&register_mutex);
1482 return err;
1483 }
1484
1485 static int snd_timer_user_gstatus(struct file *file,
1486 struct snd_timer_gstatus __user *_gstatus)
1487 {
1488 struct snd_timer_gstatus gstatus;
1489 struct snd_timer_id tid;
1490 struct snd_timer *t;
1491 int err = 0;
1492
1493 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1494 return -EFAULT;
1495 tid = gstatus.tid;
1496 memset(&gstatus, 0, sizeof(gstatus));
1497 gstatus.tid = tid;
1498 mutex_lock(&register_mutex);
1499 t = snd_timer_find(&tid);
1500 if (t != NULL) {
1501 if (t->hw.c_resolution)
1502 gstatus.resolution = t->hw.c_resolution(t);
1503 else
1504 gstatus.resolution = t->hw.resolution;
1505 if (t->hw.precise_resolution) {
1506 t->hw.precise_resolution(t, &gstatus.resolution_num,
1507 &gstatus.resolution_den);
1508 } else {
1509 gstatus.resolution_num = gstatus.resolution;
1510 gstatus.resolution_den = 1000000000uL;
1511 }
1512 } else {
1513 err = -ENODEV;
1514 }
1515 mutex_unlock(&register_mutex);
1516 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1517 err = -EFAULT;
1518 return err;
1519 }
1520
1521 static int snd_timer_user_tselect(struct file *file,
1522 struct snd_timer_select __user *_tselect)
1523 {
1524 struct snd_timer_user *tu;
1525 struct snd_timer_select tselect;
1526 char str[32];
1527 int err = 0;
1528
1529 tu = file->private_data;
1530 if (tu->timeri) {
1531 snd_timer_close(tu->timeri);
1532 tu->timeri = NULL;
1533 }
1534 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1535 err = -EFAULT;
1536 goto __err;
1537 }
1538 sprintf(str, "application %i", current->pid);
1539 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1540 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1541 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1542 if (err < 0)
1543 goto __err;
1544
1545 kfree(tu->queue);
1546 tu->queue = NULL;
1547 kfree(tu->tqueue);
1548 tu->tqueue = NULL;
1549 if (tu->tread) {
1550 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1551 GFP_KERNEL);
1552 if (tu->tqueue == NULL)
1553 err = -ENOMEM;
1554 } else {
1555 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1556 GFP_KERNEL);
1557 if (tu->queue == NULL)
1558 err = -ENOMEM;
1559 }
1560
1561 if (err < 0) {
1562 snd_timer_close(tu->timeri);
1563 tu->timeri = NULL;
1564 } else {
1565 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1566 tu->timeri->callback = tu->tread
1567 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1568 tu->timeri->ccallback = snd_timer_user_ccallback;
1569 tu->timeri->callback_data = (void *)tu;
1570 }
1571
1572 __err:
1573 return err;
1574 }
1575
1576 static int snd_timer_user_info(struct file *file,
1577 struct snd_timer_info __user *_info)
1578 {
1579 struct snd_timer_user *tu;
1580 struct snd_timer_info *info;
1581 struct snd_timer *t;
1582 int err = 0;
1583
1584 tu = file->private_data;
1585 if (!tu->timeri)
1586 return -EBADFD;
1587 t = tu->timeri->timer;
1588 if (!t)
1589 return -EBADFD;
1590
1591 info = kzalloc(sizeof(*info), GFP_KERNEL);
1592 if (! info)
1593 return -ENOMEM;
1594 info->card = t->card ? t->card->number : -1;
1595 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1596 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1597 strlcpy(info->id, t->id, sizeof(info->id));
1598 strlcpy(info->name, t->name, sizeof(info->name));
1599 info->resolution = t->hw.resolution;
1600 if (copy_to_user(_info, info, sizeof(*_info)))
1601 err = -EFAULT;
1602 kfree(info);
1603 return err;
1604 }
1605
1606 static int snd_timer_user_params(struct file *file,
1607 struct snd_timer_params __user *_params)
1608 {
1609 struct snd_timer_user *tu;
1610 struct snd_timer_params params;
1611 struct snd_timer *t;
1612 struct snd_timer_read *tr;
1613 struct snd_timer_tread *ttr;
1614 int err;
1615
1616 tu = file->private_data;
1617 if (!tu->timeri)
1618 return -EBADFD;
1619 t = tu->timeri->timer;
1620 if (!t)
1621 return -EBADFD;
1622 if (copy_from_user(&params, _params, sizeof(params)))
1623 return -EFAULT;
1624 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1625 err = -EINVAL;
1626 goto _end;
1627 }
1628 if (params.queue_size > 0 &&
1629 (params.queue_size < 32 || params.queue_size > 1024)) {
1630 err = -EINVAL;
1631 goto _end;
1632 }
1633 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1634 (1<<SNDRV_TIMER_EVENT_TICK)|
1635 (1<<SNDRV_TIMER_EVENT_START)|
1636 (1<<SNDRV_TIMER_EVENT_STOP)|
1637 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1638 (1<<SNDRV_TIMER_EVENT_PAUSE)|
1639 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1640 (1<<SNDRV_TIMER_EVENT_RESUME)|
1641 (1<<SNDRV_TIMER_EVENT_MSTART)|
1642 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1643 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1644 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1645 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1646 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1647 err = -EINVAL;
1648 goto _end;
1649 }
1650 snd_timer_stop(tu->timeri);
1651 spin_lock_irq(&t->lock);
1652 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1653 SNDRV_TIMER_IFLG_EXCLUSIVE|
1654 SNDRV_TIMER_IFLG_EARLY_EVENT);
1655 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1656 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1657 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1658 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1659 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1660 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1661 spin_unlock_irq(&t->lock);
1662 if (params.queue_size > 0 &&
1663 (unsigned int)tu->queue_size != params.queue_size) {
1664 if (tu->tread) {
1665 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1666 GFP_KERNEL);
1667 if (ttr) {
1668 kfree(tu->tqueue);
1669 tu->queue_size = params.queue_size;
1670 tu->tqueue = ttr;
1671 }
1672 } else {
1673 tr = kmalloc(params.queue_size * sizeof(*tr),
1674 GFP_KERNEL);
1675 if (tr) {
1676 kfree(tu->queue);
1677 tu->queue_size = params.queue_size;
1678 tu->queue = tr;
1679 }
1680 }
1681 }
1682 tu->qhead = tu->qtail = tu->qused = 0;
1683 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1684 if (tu->tread) {
1685 struct snd_timer_tread tread;
1686 tread.event = SNDRV_TIMER_EVENT_EARLY;
1687 tread.tstamp.tv_sec = 0;
1688 tread.tstamp.tv_nsec = 0;
1689 tread.val = 0;
1690 snd_timer_user_append_to_tqueue(tu, &tread);
1691 } else {
1692 struct snd_timer_read *r = &tu->queue[0];
1693 r->resolution = 0;
1694 r->ticks = 0;
1695 tu->qused++;
1696 tu->qtail++;
1697 }
1698 }
1699 tu->filter = params.filter;
1700 tu->ticks = params.ticks;
1701 err = 0;
1702 _end:
1703 if (copy_to_user(_params, &params, sizeof(params)))
1704 return -EFAULT;
1705 return err;
1706 }
1707
1708 static int snd_timer_user_status(struct file *file,
1709 struct snd_timer_status __user *_status)
1710 {
1711 struct snd_timer_user *tu;
1712 struct snd_timer_status status;
1713
1714 tu = file->private_data;
1715 if (!tu->timeri)
1716 return -EBADFD;
1717 memset(&status, 0, sizeof(status));
1718 status.tstamp = tu->tstamp;
1719 status.resolution = snd_timer_resolution(tu->timeri);
1720 status.lost = tu->timeri->lost;
1721 status.overrun = tu->overrun;
1722 spin_lock_irq(&tu->qlock);
1723 status.queue = tu->qused;
1724 spin_unlock_irq(&tu->qlock);
1725 if (copy_to_user(_status, &status, sizeof(status)))
1726 return -EFAULT;
1727 return 0;
1728 }
1729
1730 static int snd_timer_user_start(struct file *file)
1731 {
1732 int err;
1733 struct snd_timer_user *tu;
1734
1735 tu = file->private_data;
1736 if (!tu->timeri)
1737 return -EBADFD;
1738 snd_timer_stop(tu->timeri);
1739 tu->timeri->lost = 0;
1740 tu->last_resolution = 0;
1741 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1742 }
1743
1744 static int snd_timer_user_stop(struct file *file)
1745 {
1746 int err;
1747 struct snd_timer_user *tu;
1748
1749 tu = file->private_data;
1750 if (!tu->timeri)
1751 return -EBADFD;
1752 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1753 }
1754
1755 static int snd_timer_user_continue(struct file *file)
1756 {
1757 int err;
1758 struct snd_timer_user *tu;
1759
1760 tu = file->private_data;
1761 if (!tu->timeri)
1762 return -EBADFD;
1763 tu->timeri->lost = 0;
1764 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1765 }
1766
1767 static int snd_timer_user_pause(struct file *file)
1768 {
1769 int err;
1770 struct snd_timer_user *tu;
1771
1772 tu = file->private_data;
1773 if (!tu->timeri)
1774 return -EBADFD;
1775 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1776 }
1777
1778 enum {
1779 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1780 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1781 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1782 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1783 };
1784
1785 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1786 unsigned long arg)
1787 {
1788 struct snd_timer_user *tu;
1789 void __user *argp = (void __user *)arg;
1790 int __user *p = argp;
1791
1792 tu = file->private_data;
1793 switch (cmd) {
1794 case SNDRV_TIMER_IOCTL_PVERSION:
1795 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1796 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1797 return snd_timer_user_next_device(argp);
1798 case SNDRV_TIMER_IOCTL_TREAD:
1799 {
1800 int xarg;
1801
1802 if (tu->timeri) /* too late */
1803 return -EBUSY;
1804 if (get_user(xarg, p))
1805 return -EFAULT;
1806 tu->tread = xarg ? 1 : 0;
1807 return 0;
1808 }
1809 case SNDRV_TIMER_IOCTL_GINFO:
1810 return snd_timer_user_ginfo(file, argp);
1811 case SNDRV_TIMER_IOCTL_GPARAMS:
1812 return snd_timer_user_gparams(file, argp);
1813 case SNDRV_TIMER_IOCTL_GSTATUS:
1814 return snd_timer_user_gstatus(file, argp);
1815 case SNDRV_TIMER_IOCTL_SELECT:
1816 return snd_timer_user_tselect(file, argp);
1817 case SNDRV_TIMER_IOCTL_INFO:
1818 return snd_timer_user_info(file, argp);
1819 case SNDRV_TIMER_IOCTL_PARAMS:
1820 return snd_timer_user_params(file, argp);
1821 case SNDRV_TIMER_IOCTL_STATUS:
1822 return snd_timer_user_status(file, argp);
1823 case SNDRV_TIMER_IOCTL_START:
1824 case SNDRV_TIMER_IOCTL_START_OLD:
1825 return snd_timer_user_start(file);
1826 case SNDRV_TIMER_IOCTL_STOP:
1827 case SNDRV_TIMER_IOCTL_STOP_OLD:
1828 return snd_timer_user_stop(file);
1829 case SNDRV_TIMER_IOCTL_CONTINUE:
1830 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1831 return snd_timer_user_continue(file);
1832 case SNDRV_TIMER_IOCTL_PAUSE:
1833 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1834 return snd_timer_user_pause(file);
1835 }
1836 return -ENOTTY;
1837 }
1838
1839 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1840 unsigned long arg)
1841 {
1842 struct snd_timer_user *tu = file->private_data;
1843 long ret;
1844
1845 mutex_lock(&tu->ioctl_lock);
1846 ret = __snd_timer_user_ioctl(file, cmd, arg);
1847 mutex_unlock(&tu->ioctl_lock);
1848 return ret;
1849 }
1850
1851 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1852 {
1853 struct snd_timer_user *tu;
1854
1855 tu = file->private_data;
1856 return fasync_helper(fd, file, on, &tu->fasync);
1857 }
1858
1859 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1860 size_t count, loff_t *offset)
1861 {
1862 struct snd_timer_user *tu;
1863 long result = 0, unit;
1864 int err = 0;
1865
1866 tu = file->private_data;
1867 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1868 spin_lock_irq(&tu->qlock);
1869 while ((long)count - result >= unit) {
1870 while (!tu->qused) {
1871 wait_queue_t wait;
1872
1873 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1874 err = -EAGAIN;
1875 break;
1876 }
1877
1878 set_current_state(TASK_INTERRUPTIBLE);
1879 init_waitqueue_entry(&wait, current);
1880 add_wait_queue(&tu->qchange_sleep, &wait);
1881
1882 spin_unlock_irq(&tu->qlock);
1883 schedule();
1884 spin_lock_irq(&tu->qlock);
1885
1886 remove_wait_queue(&tu->qchange_sleep, &wait);
1887
1888 if (signal_pending(current)) {
1889 err = -ERESTARTSYS;
1890 break;
1891 }
1892 }
1893
1894 spin_unlock_irq(&tu->qlock);
1895 if (err < 0)
1896 goto _error;
1897
1898 if (tu->tread) {
1899 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1900 sizeof(struct snd_timer_tread))) {
1901 err = -EFAULT;
1902 goto _error;
1903 }
1904 } else {
1905 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1906 sizeof(struct snd_timer_read))) {
1907 err = -EFAULT;
1908 goto _error;
1909 }
1910 }
1911
1912 tu->qhead %= tu->queue_size;
1913
1914 result += unit;
1915 buffer += unit;
1916
1917 spin_lock_irq(&tu->qlock);
1918 tu->qused--;
1919 }
1920 spin_unlock_irq(&tu->qlock);
1921 _error:
1922 return result > 0 ? result : err;
1923 }
1924
1925 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1926 {
1927 unsigned int mask;
1928 struct snd_timer_user *tu;
1929
1930 tu = file->private_data;
1931
1932 poll_wait(file, &tu->qchange_sleep, wait);
1933
1934 mask = 0;
1935 if (tu->qused)
1936 mask |= POLLIN | POLLRDNORM;
1937
1938 return mask;
1939 }
1940
1941 #ifdef CONFIG_COMPAT
1942 #include "timer_compat.c"
1943 #else
1944 #define snd_timer_user_ioctl_compat NULL
1945 #endif
1946
1947 static const struct file_operations snd_timer_f_ops =
1948 {
1949 .owner = THIS_MODULE,
1950 .read = snd_timer_user_read,
1951 .open = snd_timer_user_open,
1952 .release = snd_timer_user_release,
1953 .llseek = no_llseek,
1954 .poll = snd_timer_user_poll,
1955 .unlocked_ioctl = snd_timer_user_ioctl,
1956 .compat_ioctl = snd_timer_user_ioctl_compat,
1957 .fasync = snd_timer_user_fasync,
1958 };
1959
1960 /*
1961 * ENTRY functions
1962 */
1963
1964 static int __init alsa_timer_init(void)
1965 {
1966 int err;
1967
1968 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1969 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1970 "system timer");
1971 #endif
1972
1973 if ((err = snd_timer_register_system()) < 0)
1974 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1975 err);
1976 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1977 &snd_timer_f_ops, NULL, "timer")) < 0)
1978 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1979 err);
1980 snd_timer_proc_init();
1981 return 0;
1982 }
1983
1984 static void __exit alsa_timer_exit(void)
1985 {
1986 struct list_head *p, *n;
1987
1988 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1989 /* unregister the system timer */
1990 list_for_each_safe(p, n, &snd_timer_list) {
1991 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
1992 snd_timer_free(timer);
1993 }
1994 snd_timer_proc_done();
1995 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1996 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1997 #endif
1998 }
1999
2000 module_init(alsa_timer_init)
2001 module_exit(alsa_timer_exit)
2002
2003 EXPORT_SYMBOL(snd_timer_open);
2004 EXPORT_SYMBOL(snd_timer_close);
2005 EXPORT_SYMBOL(snd_timer_resolution);
2006 EXPORT_SYMBOL(snd_timer_start);
2007 EXPORT_SYMBOL(snd_timer_stop);
2008 EXPORT_SYMBOL(snd_timer_continue);
2009 EXPORT_SYMBOL(snd_timer_pause);
2010 EXPORT_SYMBOL(snd_timer_new);
2011 EXPORT_SYMBOL(snd_timer_notify);
2012 EXPORT_SYMBOL(snd_timer_global_new);
2013 EXPORT_SYMBOL(snd_timer_global_free);
2014 EXPORT_SYMBOL(snd_timer_global_register);
2015 EXPORT_SYMBOL(snd_timer_interrupt);