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