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