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