2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/smp_lock.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/moduleparam.h>
29 #include <sound/core.h>
30 #include <sound/timer.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/minors.h>
34 #include <sound/initval.h>
35 #include <linux/kmod.h>
37 #include <linux/kerneld.h>
40 #if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
41 #define DEFAULT_TIMER_LIMIT 3
42 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
43 #define DEFAULT_TIMER_LIMIT 2
45 #define DEFAULT_TIMER_LIMIT 1
48 static int timer_limit
= DEFAULT_TIMER_LIMIT
;
49 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
50 MODULE_DESCRIPTION("ALSA timer interface");
51 MODULE_LICENSE("GPL");
52 module_param(timer_limit
, int, 0444);
53 MODULE_PARM_DESC(timer_limit
, "Maximum global timers in system.");
56 snd_timer_instance_t
*timeri
;
57 int tread
; /* enhanced read with timestamps and events */
59 unsigned long overrun
;
64 snd_timer_read_t
*queue
;
65 snd_timer_tread_t
*tqueue
;
67 unsigned long last_resolution
;
69 struct timespec tstamp
; /* trigger tstamp */
70 wait_queue_head_t qchange_sleep
;
71 struct fasync_struct
*fasync
;
75 static LIST_HEAD(snd_timer_list
);
77 /* list of slave instances */
78 static LIST_HEAD(snd_timer_slave_list
);
80 /* lock for slave active lists */
81 static DEFINE_SPINLOCK(slave_active_lock
);
83 static DECLARE_MUTEX(register_mutex
);
85 static int snd_timer_free(snd_timer_t
*timer
);
86 static int snd_timer_dev_free(snd_device_t
*device
);
87 static int snd_timer_dev_register(snd_device_t
*device
);
88 static int snd_timer_dev_unregister(snd_device_t
*device
);
90 static void snd_timer_reschedule(snd_timer_t
* timer
, unsigned long ticks_left
);
93 * create a timer instance with the given owner string.
94 * when timer is not NULL, increments the module counter
96 static snd_timer_instance_t
*snd_timer_instance_new(char *owner
, snd_timer_t
*timer
)
98 snd_timer_instance_t
*timeri
;
99 timeri
= kcalloc(1, sizeof(*timeri
), GFP_KERNEL
);
102 timeri
->owner
= snd_kmalloc_strdup(owner
, GFP_KERNEL
);
103 if (! timeri
->owner
) {
107 INIT_LIST_HEAD(&timeri
->open_list
);
108 INIT_LIST_HEAD(&timeri
->active_list
);
109 INIT_LIST_HEAD(&timeri
->ack_list
);
110 INIT_LIST_HEAD(&timeri
->slave_list_head
);
111 INIT_LIST_HEAD(&timeri
->slave_active_head
);
113 timeri
->timer
= timer
;
114 if (timer
&& timer
->card
&& !try_module_get(timer
->card
->module
)) {
115 kfree(timeri
->owner
);
124 * find a timer instance from the given timer id
126 static snd_timer_t
*snd_timer_find(snd_timer_id_t
*tid
)
128 snd_timer_t
*timer
= NULL
;
131 list_for_each(p
, &snd_timer_list
) {
132 timer
= (snd_timer_t
*)list_entry(p
, snd_timer_t
, device_list
);
134 if (timer
->tmr_class
!= tid
->dev_class
)
136 if ((timer
->tmr_class
== SNDRV_TIMER_CLASS_CARD
||
137 timer
->tmr_class
== SNDRV_TIMER_CLASS_PCM
) &&
138 (timer
->card
== NULL
||
139 timer
->card
->number
!= tid
->card
))
141 if (timer
->tmr_device
!= tid
->device
)
143 if (timer
->tmr_subdevice
!= tid
->subdevice
)
152 static void snd_timer_request(snd_timer_id_t
*tid
)
154 if (! current
->fs
->root
)
156 switch (tid
->dev_class
) {
157 case SNDRV_TIMER_CLASS_GLOBAL
:
158 if (tid
->device
< timer_limit
)
159 request_module("snd-timer-%i", tid
->device
);
161 case SNDRV_TIMER_CLASS_CARD
:
162 case SNDRV_TIMER_CLASS_PCM
:
163 if (tid
->card
< snd_ecards_limit
)
164 request_module("snd-card-%i", tid
->card
);
174 * look for a master instance matching with the slave id of the given slave.
175 * when found, relink the open_link of the slave.
177 * call this with register_mutex down.
179 static void snd_timer_check_slave(snd_timer_instance_t
*slave
)
182 snd_timer_instance_t
*master
;
183 struct list_head
*p
, *q
;
185 /* FIXME: it's really dumb to look up all entries.. */
186 list_for_each(p
, &snd_timer_list
) {
187 timer
= (snd_timer_t
*)list_entry(p
, snd_timer_t
, device_list
);
188 list_for_each(q
, &timer
->open_list_head
) {
189 master
= (snd_timer_instance_t
*)list_entry(q
, snd_timer_instance_t
, open_list
);
190 if (slave
->slave_class
== master
->slave_class
&&
191 slave
->slave_id
== master
->slave_id
) {
192 list_del(&slave
->open_list
);
193 list_add_tail(&slave
->open_list
, &master
->slave_list_head
);
194 spin_lock_irq(&slave_active_lock
);
195 slave
->master
= master
;
196 slave
->timer
= master
->timer
;
197 spin_unlock_irq(&slave_active_lock
);
205 * look for slave instances matching with the slave id of the given master.
206 * when found, relink the open_link of slaves.
208 * call this with register_mutex down.
210 static void snd_timer_check_master(snd_timer_instance_t
*master
)
212 snd_timer_instance_t
*slave
;
213 struct list_head
*p
, *n
;
215 /* check all pending slaves */
216 list_for_each_safe(p
, n
, &snd_timer_slave_list
) {
217 slave
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, open_list
);
218 if (slave
->slave_class
== master
->slave_class
&&
219 slave
->slave_id
== master
->slave_id
) {
221 list_add_tail(p
, &master
->slave_list_head
);
222 spin_lock_irq(&slave_active_lock
);
223 slave
->master
= master
;
224 slave
->timer
= master
->timer
;
225 if (slave
->flags
& SNDRV_TIMER_IFLG_RUNNING
)
226 list_add_tail(&slave
->active_list
, &master
->slave_active_head
);
227 spin_unlock_irq(&slave_active_lock
);
233 * open a timer instance
234 * when opening a master, the slave id must be here given.
236 int snd_timer_open(snd_timer_instance_t
**ti
,
237 char *owner
, snd_timer_id_t
*tid
,
238 unsigned int slave_id
)
241 snd_timer_instance_t
*timeri
= NULL
;
243 if (tid
->dev_class
== SNDRV_TIMER_CLASS_SLAVE
) {
244 /* open a slave instance */
245 if (tid
->dev_sclass
<= SNDRV_TIMER_SCLASS_NONE
||
246 tid
->dev_sclass
> SNDRV_TIMER_SCLASS_OSS_SEQUENCER
) {
247 snd_printd("invalid slave class %i\n", tid
->dev_sclass
);
250 down(®ister_mutex
);
251 timeri
= snd_timer_instance_new(owner
, NULL
);
252 timeri
->slave_class
= tid
->dev_sclass
;
253 timeri
->slave_id
= tid
->device
;
254 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
255 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
256 snd_timer_check_slave(timeri
);
262 /* open a master instance */
263 down(®ister_mutex
);
264 timer
= snd_timer_find(tid
);
268 snd_timer_request(tid
);
269 down(®ister_mutex
);
270 timer
= snd_timer_find(tid
);
274 if (!list_empty(&timer
->open_list_head
)) {
275 timeri
= (snd_timer_instance_t
*)list_entry(timer
->open_list_head
.next
, snd_timer_instance_t
, open_list
);
276 if (timeri
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
281 timeri
= snd_timer_instance_new(owner
, timer
);
283 timeri
->slave_class
= tid
->dev_sclass
;
284 timeri
->slave_id
= slave_id
;
285 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
)
286 timer
->hw
.open(timer
);
287 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
288 snd_timer_check_master(timeri
);
299 static int _snd_timer_stop(snd_timer_instance_t
* timeri
, int keep_flag
, enum sndrv_timer_event event
);
302 * close a timer instance
304 int snd_timer_close(snd_timer_instance_t
* timeri
)
306 snd_timer_t
*timer
= NULL
;
307 struct list_head
*p
, *n
;
308 snd_timer_instance_t
*slave
;
310 snd_assert(timeri
!= NULL
, return -ENXIO
);
312 /* force to stop the timer */
313 snd_timer_stop(timeri
);
315 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
316 /* wait, until the active callback is finished */
317 spin_lock_irq(&slave_active_lock
);
318 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
319 spin_unlock_irq(&slave_active_lock
);
321 spin_lock_irq(&slave_active_lock
);
323 spin_unlock_irq(&slave_active_lock
);
324 down(®ister_mutex
);
325 list_del(&timeri
->open_list
);
328 timer
= timeri
->timer
;
329 /* wait, until the active callback is finished */
330 spin_lock_irq(&timer
->lock
);
331 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
332 spin_unlock_irq(&timer
->lock
);
334 spin_lock_irq(&timer
->lock
);
336 spin_unlock_irq(&timer
->lock
);
337 down(®ister_mutex
);
338 list_del(&timeri
->open_list
);
339 if (timer
&& list_empty(&timer
->open_list_head
) && timer
->hw
.close
)
340 timer
->hw
.close(timer
);
341 /* remove slave links */
342 list_for_each_safe(p
, n
, &timeri
->slave_list_head
) {
343 slave
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, open_list
);
344 spin_lock_irq(&slave_active_lock
);
345 _snd_timer_stop(slave
, 1, SNDRV_TIMER_EVENT_RESOLUTION
);
347 list_add_tail(p
, &snd_timer_slave_list
);
348 slave
->master
= NULL
;
350 spin_unlock_irq(&slave_active_lock
);
354 if (timeri
->private_free
)
355 timeri
->private_free(timeri
);
356 kfree(timeri
->owner
);
358 if (timer
&& timer
->card
)
359 module_put(timer
->card
->module
);
363 unsigned long snd_timer_resolution(snd_timer_instance_t
* timeri
)
369 if ((timer
= timeri
->timer
) != NULL
) {
370 if (timer
->hw
.c_resolution
)
371 return timer
->hw
.c_resolution(timer
);
372 return timer
->hw
.resolution
;
377 static void snd_timer_notify1(snd_timer_instance_t
*ti
, enum sndrv_timer_event event
)
381 unsigned long resolution
= 0;
382 snd_timer_instance_t
*ts
;
384 struct timespec tstamp
;
386 snd_timestamp_now(&tstamp
, 1);
387 snd_assert(event
>= SNDRV_TIMER_EVENT_START
&& event
<= SNDRV_TIMER_EVENT_PAUSE
, return);
388 if (event
== SNDRV_TIMER_EVENT_START
|| event
== SNDRV_TIMER_EVENT_CONTINUE
)
389 resolution
= snd_timer_resolution(ti
);
391 ti
->ccallback(ti
, SNDRV_TIMER_EVENT_START
, &tstamp
, resolution
);
392 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
397 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
399 spin_lock_irqsave(&timer
->lock
, flags
);
400 list_for_each(n
, &ti
->slave_active_head
) {
401 ts
= (snd_timer_instance_t
*)list_entry(n
, snd_timer_instance_t
, active_list
);
403 ts
->ccallback(ti
, event
+ 100, &tstamp
, resolution
);
405 spin_unlock_irqrestore(&timer
->lock
, flags
);
408 static int snd_timer_start1(snd_timer_t
*timer
, snd_timer_instance_t
*timeri
, unsigned long sticks
)
410 list_del(&timeri
->active_list
);
411 list_add_tail(&timeri
->active_list
, &timer
->active_list_head
);
412 if (timer
->running
) {
413 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
415 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
416 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
417 return 1; /* delayed start */
419 timer
->sticks
= sticks
;
420 timer
->hw
.start(timer
);
423 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
428 static int snd_timer_start_slave(snd_timer_instance_t
*timeri
)
432 spin_lock_irqsave(&slave_active_lock
, flags
);
433 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
435 list_add_tail(&timeri
->active_list
, &timeri
->master
->slave_active_head
);
436 spin_unlock_irqrestore(&slave_active_lock
, flags
);
437 return 1; /* delayed start */
441 * start the timer instance
443 int snd_timer_start(snd_timer_instance_t
* timeri
, unsigned int ticks
)
446 int result
= -EINVAL
;
449 if (timeri
== NULL
|| ticks
< 1)
451 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
452 result
= snd_timer_start_slave(timeri
);
453 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
456 timer
= timeri
->timer
;
459 spin_lock_irqsave(&timer
->lock
, flags
);
460 timeri
->ticks
= timeri
->cticks
= ticks
;
462 result
= snd_timer_start1(timer
, timeri
, ticks
);
463 spin_unlock_irqrestore(&timer
->lock
, flags
);
464 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
468 static int _snd_timer_stop(snd_timer_instance_t
* timeri
, int keep_flag
, enum sndrv_timer_event event
)
473 snd_assert(timeri
!= NULL
, return -ENXIO
);
475 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
477 spin_lock_irqsave(&slave_active_lock
, flags
);
478 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
479 spin_unlock_irqrestore(&slave_active_lock
, flags
);
483 timer
= timeri
->timer
;
486 spin_lock_irqsave(&timer
->lock
, flags
);
487 list_del_init(&timeri
->ack_list
);
488 list_del_init(&timeri
->active_list
);
489 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
490 !(--timer
->running
)) {
491 timer
->hw
.stop(timer
);
492 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
493 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
494 snd_timer_reschedule(timer
, 0);
495 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
496 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
497 timer
->hw
.start(timer
);
502 timeri
->flags
&= ~(SNDRV_TIMER_IFLG_RUNNING
|SNDRV_TIMER_IFLG_START
);
503 spin_unlock_irqrestore(&timer
->lock
, flags
);
505 if (event
!= SNDRV_TIMER_EVENT_RESOLUTION
)
506 snd_timer_notify1(timeri
, event
);
511 * stop the timer instance.
513 * do not call this from the timer callback!
515 int snd_timer_stop(snd_timer_instance_t
* timeri
)
521 err
= _snd_timer_stop(timeri
, 0, SNDRV_TIMER_EVENT_STOP
);
524 timer
= timeri
->timer
;
525 spin_lock_irqsave(&timer
->lock
, flags
);
526 timeri
->cticks
= timeri
->ticks
;
528 spin_unlock_irqrestore(&timer
->lock
, flags
);
533 * start again.. the tick is kept.
535 int snd_timer_continue(snd_timer_instance_t
* timeri
)
538 int result
= -EINVAL
;
543 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
544 return snd_timer_start_slave(timeri
);
545 timer
= timeri
->timer
;
548 spin_lock_irqsave(&timer
->lock
, flags
);
552 result
= snd_timer_start1(timer
, timeri
, timer
->sticks
);
553 spin_unlock_irqrestore(&timer
->lock
, flags
);
554 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_CONTINUE
);
559 * pause.. remember the ticks left
561 int snd_timer_pause(snd_timer_instance_t
* timeri
)
563 return _snd_timer_stop(timeri
, 0, SNDRV_TIMER_EVENT_PAUSE
);
567 * reschedule the timer
569 * start pending instances and check the scheduling ticks.
570 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
572 static void snd_timer_reschedule(snd_timer_t
* timer
, unsigned long ticks_left
)
574 snd_timer_instance_t
*ti
;
575 unsigned long ticks
= ~0UL;
578 list_for_each(p
, &timer
->active_list_head
) {
579 ti
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, active_list
);
580 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
581 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
582 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
585 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
586 if (ticks
> ti
->cticks
)
591 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
594 if (ticks
> timer
->hw
.ticks
)
595 ticks
= timer
->hw
.ticks
;
596 if (ticks_left
!= ticks
)
597 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
598 timer
->sticks
= ticks
;
605 static void snd_timer_tasklet(unsigned long arg
)
607 snd_timer_t
*timer
= (snd_timer_t
*) arg
;
608 snd_timer_instance_t
*ti
;
610 unsigned long resolution
, ticks
;
612 spin_lock(&timer
->lock
);
613 /* now process all callbacks */
614 while (!list_empty(&timer
->sack_list_head
)) {
615 p
= timer
->sack_list_head
.next
; /* get first item */
616 ti
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, ack_list
);
618 /* remove from ack_list and make empty */
623 resolution
= ti
->resolution
;
625 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
626 spin_unlock(&timer
->lock
);
628 ti
->callback(ti
, resolution
, ticks
);
629 spin_lock(&timer
->lock
);
630 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
632 spin_unlock(&timer
->lock
);
638 * ticks_left is usually equal to timer->sticks.
641 void snd_timer_interrupt(snd_timer_t
* timer
, unsigned long ticks_left
)
643 snd_timer_instance_t
*ti
, *ts
;
644 unsigned long resolution
, ticks
;
645 struct list_head
*p
, *q
, *n
;
651 spin_lock(&timer
->lock
);
653 /* remember the current resolution */
654 if (timer
->hw
.c_resolution
)
655 resolution
= timer
->hw
.c_resolution(timer
);
657 resolution
= timer
->hw
.resolution
;
659 /* loop for all active instances
660 * here we cannot use list_for_each because the active_list of a processed
661 * instance is relinked to done_list_head before callback is called.
663 list_for_each_safe(p
, n
, &timer
->active_list_head
) {
664 ti
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, active_list
);
665 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
667 ti
->pticks
+= ticks_left
;
668 ti
->resolution
= resolution
;
669 if (ti
->cticks
< ticks_left
)
672 ti
->cticks
-= ticks_left
;
673 if (ti
->cticks
) /* not expired */
675 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
676 ti
->cticks
= ti
->ticks
;
678 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
679 if (--timer
->running
)
682 if (list_empty(&ti
->ack_list
)) {
683 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
684 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
)) {
685 list_add_tail(&ti
->ack_list
, &timer
->ack_list_head
);
687 list_add_tail(&ti
->ack_list
, &timer
->sack_list_head
);
690 list_for_each(q
, &ti
->slave_active_head
) {
691 ts
= (snd_timer_instance_t
*)list_entry(q
, snd_timer_instance_t
, active_list
);
692 ts
->pticks
= ti
->pticks
;
693 ts
->resolution
= resolution
;
694 if (list_empty(&ts
->ack_list
)) {
695 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
696 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
)) {
697 list_add_tail(&ts
->ack_list
, &timer
->ack_list_head
);
699 list_add_tail(&ts
->ack_list
, &timer
->sack_list_head
);
704 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
705 snd_timer_reschedule(timer
, ticks_left
);
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
;
711 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_AUTO
) ||
712 (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
)) {
714 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
715 timer
->hw
.start(timer
);
718 timer
->hw
.stop(timer
);
721 /* now process all fast callbacks */
722 while (!list_empty(&timer
->ack_list_head
)) {
723 p
= timer
->ack_list_head
.next
; /* get first item */
724 ti
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, ack_list
);
726 /* remove from ack_list and make empty */
732 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
733 spin_unlock(&timer
->lock
);
735 ti
->callback(ti
, resolution
, ticks
);
736 spin_lock(&timer
->lock
);
737 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
740 /* do we have any slow callbacks? */
741 use_tasklet
= !list_empty(&timer
->sack_list_head
);
742 spin_unlock(&timer
->lock
);
745 tasklet_hi_schedule(&timer
->task_queue
);
752 int snd_timer_new(snd_card_t
*card
, char *id
, snd_timer_id_t
*tid
, snd_timer_t
** rtimer
)
756 static snd_device_ops_t ops
= {
757 .dev_free
= snd_timer_dev_free
,
758 .dev_register
= snd_timer_dev_register
,
759 .dev_unregister
= snd_timer_dev_unregister
762 snd_assert(tid
!= NULL
, return -EINVAL
);
763 snd_assert(rtimer
!= NULL
, return -EINVAL
);
765 timer
= kcalloc(1, sizeof(*timer
), GFP_KERNEL
);
768 timer
->tmr_class
= tid
->dev_class
;
770 timer
->tmr_device
= tid
->device
;
771 timer
->tmr_subdevice
= tid
->subdevice
;
773 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
774 INIT_LIST_HEAD(&timer
->device_list
);
775 INIT_LIST_HEAD(&timer
->open_list_head
);
776 INIT_LIST_HEAD(&timer
->active_list_head
);
777 INIT_LIST_HEAD(&timer
->ack_list_head
);
778 INIT_LIST_HEAD(&timer
->sack_list_head
);
779 spin_lock_init(&timer
->lock
);
780 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
, (unsigned long)timer
);
782 if ((err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
)) < 0) {
783 snd_timer_free(timer
);
791 static int snd_timer_free(snd_timer_t
*timer
)
793 snd_assert(timer
!= NULL
, return -ENXIO
);
794 if (timer
->private_free
)
795 timer
->private_free(timer
);
800 int snd_timer_dev_free(snd_device_t
*device
)
802 snd_timer_t
*timer
= device
->device_data
;
803 return snd_timer_free(timer
);
806 int snd_timer_dev_register(snd_device_t
*dev
)
808 snd_timer_t
*timer
= dev
->device_data
;
812 snd_assert(timer
!= NULL
&& timer
->hw
.start
!= NULL
&& timer
->hw
.stop
!= NULL
, return -ENXIO
);
813 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
814 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
817 down(®ister_mutex
);
818 list_for_each(p
, &snd_timer_list
) {
819 timer1
= (snd_timer_t
*)list_entry(p
, snd_timer_t
, device_list
);
820 if (timer1
->tmr_class
> timer
->tmr_class
)
822 if (timer1
->tmr_class
< timer
->tmr_class
)
824 if (timer1
->card
&& timer
->card
) {
825 if (timer1
->card
->number
> timer
->card
->number
)
827 if (timer1
->card
->number
< timer
->card
->number
)
830 if (timer1
->tmr_device
> timer
->tmr_device
)
832 if (timer1
->tmr_device
< timer
->tmr_device
)
834 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
836 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
842 list_add_tail(&timer
->device_list
, p
);
847 int snd_timer_unregister(snd_timer_t
*timer
)
849 struct list_head
*p
, *n
;
850 snd_timer_instance_t
*ti
;
852 snd_assert(timer
!= NULL
, return -ENXIO
);
853 down(®ister_mutex
);
854 if (! list_empty(&timer
->open_list_head
)) {
855 snd_printk(KERN_WARNING
"timer 0x%lx is busy?\n", (long)timer
);
856 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
858 ti
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, open_list
);
862 list_del(&timer
->device_list
);
864 return snd_timer_free(timer
);
867 static int snd_timer_dev_unregister(snd_device_t
*device
)
869 snd_timer_t
*timer
= device
->device_data
;
870 return snd_timer_unregister(timer
);
873 void snd_timer_notify(snd_timer_t
*timer
, enum sndrv_timer_event event
, struct timespec
*tstamp
)
876 unsigned long resolution
= 0;
877 snd_timer_instance_t
*ti
, *ts
;
878 struct list_head
*p
, *n
;
880 snd_runtime_check(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
, return);
881 snd_assert(event
>= SNDRV_TIMER_EVENT_MSTART
&& event
<= SNDRV_TIMER_EVENT_MPAUSE
, return);
882 spin_lock_irqsave(&timer
->lock
, flags
);
883 if (event
== SNDRV_TIMER_EVENT_MSTART
|| event
== SNDRV_TIMER_EVENT_MCONTINUE
) {
884 if (timer
->hw
.c_resolution
)
885 resolution
= timer
->hw
.c_resolution(timer
);
887 resolution
= timer
->hw
.resolution
;
889 list_for_each(p
, &timer
->active_list_head
) {
890 ti
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, active_list
);
892 ti
->ccallback(ti
, event
, tstamp
, resolution
);
893 list_for_each(n
, &ti
->slave_active_head
) {
894 ts
= (snd_timer_instance_t
*)list_entry(n
, snd_timer_instance_t
, active_list
);
896 ts
->ccallback(ts
, event
, tstamp
, resolution
);
899 spin_unlock_irqrestore(&timer
->lock
, flags
);
903 * exported functions for global timers
905 int snd_timer_global_new(char *id
, int device
, snd_timer_t
**rtimer
)
909 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
910 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
914 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
917 int snd_timer_global_free(snd_timer_t
*timer
)
919 return snd_timer_free(timer
);
922 int snd_timer_global_register(snd_timer_t
*timer
)
926 memset(&dev
, 0, sizeof(dev
));
927 dev
.device_data
= timer
;
928 return snd_timer_dev_register(&dev
);
931 int snd_timer_global_unregister(snd_timer_t
*timer
)
933 return snd_timer_unregister(timer
);
940 struct snd_timer_system_private
{
941 struct timer_list tlist
;
942 struct timer
* timer
;
943 unsigned long last_expires
;
944 unsigned long last_jiffies
;
945 unsigned long correction
;
948 unsigned int snd_timer_system_resolution(void)
950 return 1000000000L / HZ
;
953 static void snd_timer_s_function(unsigned long data
)
955 snd_timer_t
*timer
= (snd_timer_t
*)data
;
956 struct snd_timer_system_private
*priv
= timer
->private_data
;
957 unsigned long jiff
= jiffies
;
958 if (time_after(jiff
, priv
->last_expires
))
959 priv
->correction
= (long)jiff
- (long)priv
->last_expires
;
960 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
963 static int snd_timer_s_start(snd_timer_t
* timer
)
965 struct snd_timer_system_private
*priv
;
968 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
969 njiff
= (priv
->last_jiffies
= jiffies
);
970 if (priv
->correction
> timer
->sticks
- 1) {
971 priv
->correction
-= timer
->sticks
- 1;
974 njiff
+= timer
->sticks
- priv
->correction
;
975 priv
->correction
-= timer
->sticks
;
977 priv
->last_expires
= priv
->tlist
.expires
= njiff
;
978 add_timer(&priv
->tlist
);
982 static int snd_timer_s_stop(snd_timer_t
* timer
)
984 struct snd_timer_system_private
*priv
;
987 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
988 del_timer(&priv
->tlist
);
990 if (time_before(jiff
, priv
->last_expires
))
991 timer
->sticks
= priv
->last_expires
- jiff
;
997 static struct _snd_timer_hardware snd_timer_system
=
999 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
1000 .resolution
= 1000000000L / HZ
,
1002 .start
= snd_timer_s_start
,
1003 .stop
= snd_timer_s_stop
1006 static void snd_timer_free_system(snd_timer_t
*timer
)
1008 kfree(timer
->private_data
);
1011 static int snd_timer_register_system(void)
1014 struct snd_timer_system_private
*priv
;
1017 if ((err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
)) < 0)
1019 strcpy(timer
->name
, "system timer");
1020 timer
->hw
= snd_timer_system
;
1021 priv
= kcalloc(1, sizeof(*priv
), GFP_KERNEL
);
1023 snd_timer_free(timer
);
1026 init_timer(&priv
->tlist
);
1027 priv
->tlist
.function
= snd_timer_s_function
;
1028 priv
->tlist
.data
= (unsigned long) timer
;
1029 timer
->private_data
= priv
;
1030 timer
->private_free
= snd_timer_free_system
;
1031 return snd_timer_global_register(timer
);
1038 static void snd_timer_proc_read(snd_info_entry_t
*entry
,
1039 snd_info_buffer_t
* buffer
)
1041 unsigned long flags
;
1043 snd_timer_instance_t
*ti
;
1044 struct list_head
*p
, *q
;
1046 down(®ister_mutex
);
1047 list_for_each(p
, &snd_timer_list
) {
1048 timer
= (snd_timer_t
*)list_entry(p
, snd_timer_t
, device_list
);
1049 switch (timer
->tmr_class
) {
1050 case SNDRV_TIMER_CLASS_GLOBAL
:
1051 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1053 case SNDRV_TIMER_CLASS_CARD
:
1054 snd_iprintf(buffer
, "C%i-%i: ", timer
->card
->number
, timer
->tmr_device
);
1056 case SNDRV_TIMER_CLASS_PCM
:
1057 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
, timer
->tmr_device
, timer
->tmr_subdevice
);
1060 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
, timer
->card
? timer
->card
->number
: -1, timer
->tmr_device
, timer
->tmr_subdevice
);
1062 snd_iprintf(buffer
, "%s :", timer
->name
);
1063 if (timer
->hw
.resolution
)
1064 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)", timer
->hw
.resolution
/ 1000, timer
->hw
.resolution
% 1000, timer
->hw
.ticks
);
1065 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1066 snd_iprintf(buffer
, " SLAVE");
1067 snd_iprintf(buffer
, "\n");
1068 spin_lock_irqsave(&timer
->lock
, flags
);
1069 list_for_each(q
, &timer
->open_list_head
) {
1070 ti
= (snd_timer_instance_t
*)list_entry(q
, snd_timer_instance_t
, open_list
);
1071 snd_iprintf(buffer
, " Client %s : %s : lost interrupts %li\n",
1072 ti
->owner
? ti
->owner
: "unknown",
1073 ti
->flags
& (SNDRV_TIMER_IFLG_START
|SNDRV_TIMER_IFLG_RUNNING
) ? "running" : "stopped",
1076 spin_unlock_irqrestore(&timer
->lock
, flags
);
1078 up(®ister_mutex
);
1082 * USER SPACE interface
1085 static void snd_timer_user_interrupt(snd_timer_instance_t
*timeri
,
1086 unsigned long resolution
,
1087 unsigned long ticks
)
1089 snd_timer_user_t
*tu
= timeri
->callback_data
;
1090 snd_timer_read_t
*r
;
1093 spin_lock(&tu
->qlock
);
1094 if (tu
->qused
> 0) {
1095 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1096 r
= &tu
->queue
[prev
];
1097 if (r
->resolution
== resolution
) {
1102 if (tu
->qused
>= tu
->queue_size
) {
1105 r
= &tu
->queue
[tu
->qtail
++];
1106 tu
->qtail
%= tu
->queue_size
;
1107 r
->resolution
= resolution
;
1112 spin_unlock(&tu
->qlock
);
1113 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1114 wake_up(&tu
->qchange_sleep
);
1117 static void snd_timer_user_append_to_tqueue(snd_timer_user_t
*tu
, snd_timer_tread_t
*tread
)
1119 if (tu
->qused
>= tu
->queue_size
) {
1122 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1123 tu
->qtail
%= tu
->queue_size
;
1128 static void snd_timer_user_ccallback(snd_timer_instance_t
*timeri
,
1129 enum sndrv_timer_event event
,
1130 struct timespec
*tstamp
,
1131 unsigned long resolution
)
1133 snd_timer_user_t
*tu
= timeri
->callback_data
;
1134 snd_timer_tread_t r1
;
1136 if (event
>= SNDRV_TIMER_EVENT_START
&& event
<= SNDRV_TIMER_EVENT_PAUSE
)
1137 tu
->tstamp
= *tstamp
;
1138 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1141 r1
.tstamp
= *tstamp
;
1142 r1
.val
= resolution
;
1143 spin_lock(&tu
->qlock
);
1144 snd_timer_user_append_to_tqueue(tu
, &r1
);
1145 spin_unlock(&tu
->qlock
);
1146 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1147 wake_up(&tu
->qchange_sleep
);
1150 static void snd_timer_user_tinterrupt(snd_timer_instance_t
*timeri
,
1151 unsigned long resolution
,
1152 unsigned long ticks
)
1154 snd_timer_user_t
*tu
= timeri
->callback_data
;
1155 snd_timer_tread_t
*r
, r1
;
1156 struct timespec tstamp
;
1157 int prev
, append
= 0;
1159 snd_timestamp_zero(&tstamp
);
1160 spin_lock(&tu
->qlock
);
1161 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
)|(1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1162 spin_unlock(&tu
->qlock
);
1165 if (tu
->last_resolution
!= resolution
|| ticks
> 0)
1166 snd_timestamp_now(&tstamp
, 1);
1167 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) && tu
->last_resolution
!= resolution
) {
1168 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1170 r1
.val
= resolution
;
1171 snd_timer_user_append_to_tqueue(tu
, &r1
);
1172 tu
->last_resolution
= resolution
;
1175 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1179 if (tu
->qused
> 0) {
1180 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1181 r
= &tu
->tqueue
[prev
];
1182 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1189 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1192 snd_timer_user_append_to_tqueue(tu
, &r1
);
1195 spin_unlock(&tu
->qlock
);
1198 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1199 wake_up(&tu
->qchange_sleep
);
1202 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1204 snd_timer_user_t
*tu
;
1206 tu
= kcalloc(1, sizeof(*tu
), GFP_KERNEL
);
1209 spin_lock_init(&tu
->qlock
);
1210 init_waitqueue_head(&tu
->qchange_sleep
);
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
) {
1218 file
->private_data
= tu
;
1222 static int snd_timer_user_release(struct inode
*inode
, struct file
*file
)
1224 snd_timer_user_t
*tu
;
1226 if (file
->private_data
) {
1227 tu
= file
->private_data
;
1228 file
->private_data
= NULL
;
1229 fasync_helper(-1, file
, 0, &tu
->fasync
);
1231 snd_timer_close(tu
->timeri
);
1239 static void snd_timer_user_zero_id(snd_timer_id_t
*id
)
1241 id
->dev_class
= SNDRV_TIMER_CLASS_NONE
;
1242 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1248 static void snd_timer_user_copy_id(snd_timer_id_t
*id
, snd_timer_t
*timer
)
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
;
1257 static int snd_timer_user_next_device(snd_timer_id_t __user
*_tid
)
1261 struct list_head
*p
;
1263 if (copy_from_user(&id
, _tid
, sizeof(id
)))
1265 down(®ister_mutex
);
1266 if (id
.dev_class
< 0) { /* first item */
1267 if (list_empty(&snd_timer_list
))
1268 snd_timer_user_zero_id(&id
);
1270 timer
= (snd_timer_t
*)list_entry(snd_timer_list
.next
, snd_timer_t
, device_list
);
1271 snd_timer_user_copy_id(&id
, timer
);
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
);
1283 if (timer
->tmr_device
>= id
.device
) {
1284 snd_timer_user_copy_id(&id
, timer
);
1288 if (p
== &snd_timer_list
)
1289 snd_timer_user_zero_id(&id
);
1291 case SNDRV_TIMER_CLASS_CARD
:
1292 case SNDRV_TIMER_CLASS_PCM
:
1299 if (id
.device
< 0) {
1302 id
.subdevice
= id
.subdevice
< 0 ? 0 : id
.subdevice
+ 1;
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
);
1312 if (timer
->tmr_class
< id
.dev_class
)
1314 if (timer
->card
->number
> id
.card
) {
1315 snd_timer_user_copy_id(&id
, timer
);
1318 if (timer
->card
->number
< id
.card
)
1320 if (timer
->tmr_device
> id
.device
) {
1321 snd_timer_user_copy_id(&id
, timer
);
1324 if (timer
->tmr_device
< id
.device
)
1326 if (timer
->tmr_subdevice
> id
.subdevice
) {
1327 snd_timer_user_copy_id(&id
, timer
);
1330 if (timer
->tmr_subdevice
< id
.subdevice
)
1332 snd_timer_user_copy_id(&id
, timer
);
1335 if (p
== &snd_timer_list
)
1336 snd_timer_user_zero_id(&id
);
1339 snd_timer_user_zero_id(&id
);
1342 up(®ister_mutex
);
1343 if (copy_to_user(_tid
, &id
, sizeof(*_tid
)))
1348 static int snd_timer_user_ginfo(struct file
*file
, snd_timer_ginfo_t __user
*_ginfo
)
1350 snd_timer_ginfo_t
*ginfo
;
1353 struct list_head
*p
;
1356 ginfo
= kmalloc(sizeof(*ginfo
), GFP_KERNEL
);
1359 if (copy_from_user(ginfo
, _ginfo
, sizeof(*ginfo
))) {
1364 memset(ginfo
, 0, sizeof(*ginfo
));
1366 down(®ister_mutex
);
1367 t
= snd_timer_find(&tid
);
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
;
1379 list_for_each(p
, &t
->open_list_head
) {
1385 up(®ister_mutex
);
1386 if (err
>= 0 && copy_to_user(_ginfo
, ginfo
, sizeof(*ginfo
)))
1392 static int snd_timer_user_gparams(struct file
*file
, snd_timer_gparams_t __user
*_gparams
)
1394 snd_timer_gparams_t gparams
;
1398 if (copy_from_user(&gparams
, _gparams
, sizeof(gparams
)))
1400 down(®ister_mutex
);
1401 t
= snd_timer_find(&gparams
.tid
);
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
);
1414 up(®ister_mutex
);
1418 static int snd_timer_user_gstatus(struct file
*file
, snd_timer_gstatus_t __user
*_gstatus
)
1420 snd_timer_gstatus_t gstatus
;
1425 if (copy_from_user(&gstatus
, _gstatus
, sizeof(gstatus
)))
1428 memset(&gstatus
, 0, sizeof(gstatus
));
1430 down(®ister_mutex
);
1431 t
= snd_timer_find(&tid
);
1433 if (t
->hw
.c_resolution
)
1434 gstatus
.resolution
= t
->hw
.c_resolution(t
);
1436 gstatus
.resolution
= t
->hw
.resolution
;
1437 if (t
->hw
.precise_resolution
) {
1438 t
->hw
.precise_resolution(t
, &gstatus
.resolution_num
, &gstatus
.resolution_den
);
1440 gstatus
.resolution_num
= gstatus
.resolution
;
1441 gstatus
.resolution_den
= 1000000000uL;
1446 up(®ister_mutex
);
1447 if (err
>= 0 && copy_to_user(_gstatus
, &gstatus
, sizeof(gstatus
)))
1452 static int snd_timer_user_tselect(struct file
*file
, snd_timer_select_t __user
*_tselect
)
1454 snd_timer_user_t
*tu
;
1455 snd_timer_select_t tselect
;
1459 tu
= file
->private_data
;
1461 snd_timer_close(tu
->timeri
);
1462 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
)))
1464 sprintf(str
, "application %i", current
->pid
);
1465 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1466 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1467 if ((err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
)) < 0)
1479 tu
->tqueue
= (snd_timer_tread_t
*)kmalloc(tu
->queue_size
* sizeof(snd_timer_tread_t
), GFP_KERNEL
);
1480 if (tu
->tqueue
== NULL
) {
1481 snd_timer_close(tu
->timeri
);
1485 tu
->queue
= (snd_timer_read_t
*)kmalloc(tu
->queue_size
* sizeof(snd_timer_read_t
), GFP_KERNEL
);
1486 if (tu
->queue
== NULL
) {
1487 snd_timer_close(tu
->timeri
);
1492 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1493 tu
->timeri
->callback
= tu
->tread
? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1494 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1495 tu
->timeri
->callback_data
= (void *)tu
;
1499 static int snd_timer_user_info(struct file
*file
, snd_timer_info_t __user
*_info
)
1501 snd_timer_user_t
*tu
;
1502 snd_timer_info_t
*info
;
1506 tu
= file
->private_data
;
1507 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1508 t
= tu
->timeri
->timer
;
1509 snd_assert(t
!= NULL
, return -ENXIO
);
1511 info
= kcalloc(1, sizeof(*info
), GFP_KERNEL
);
1514 info
->card
= t
->card
? t
->card
->number
: -1;
1515 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1516 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1517 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1518 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1519 info
->resolution
= t
->hw
.resolution
;
1520 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1526 static int snd_timer_user_params(struct file
*file
, snd_timer_params_t __user
*_params
)
1528 snd_timer_user_t
*tu
;
1529 snd_timer_params_t params
;
1531 snd_timer_read_t
*tr
;
1532 snd_timer_tread_t
*ttr
;
1535 tu
= file
->private_data
;
1536 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1537 t
= tu
->timeri
->timer
;
1538 snd_assert(t
!= NULL
, return -ENXIO
);
1539 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
1541 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) && params
.ticks
< 1) {
1545 if (params
.queue_size
> 0 && (params
.queue_size
< 32 || params
.queue_size
> 1024)) {
1549 if (params
.filter
& ~((1<<SNDRV_TIMER_EVENT_RESOLUTION
)|
1550 (1<<SNDRV_TIMER_EVENT_TICK
)|
1551 (1<<SNDRV_TIMER_EVENT_START
)|
1552 (1<<SNDRV_TIMER_EVENT_STOP
)|
1553 (1<<SNDRV_TIMER_EVENT_CONTINUE
)|
1554 (1<<SNDRV_TIMER_EVENT_PAUSE
)|
1555 (1<<SNDRV_TIMER_EVENT_MSTART
)|
1556 (1<<SNDRV_TIMER_EVENT_MSTOP
)|
1557 (1<<SNDRV_TIMER_EVENT_MCONTINUE
)|
1558 (1<<SNDRV_TIMER_EVENT_MPAUSE
))) {
1562 snd_timer_stop(tu
->timeri
);
1563 spin_lock_irq(&t
->lock
);
1564 tu
->timeri
->flags
&= ~(SNDRV_TIMER_IFLG_AUTO
|
1565 SNDRV_TIMER_IFLG_EXCLUSIVE
|
1566 SNDRV_TIMER_IFLG_EARLY_EVENT
);
1567 if (params
.flags
& SNDRV_TIMER_PSFLG_AUTO
)
1568 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
1569 if (params
.flags
& SNDRV_TIMER_PSFLG_EXCLUSIVE
)
1570 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EXCLUSIVE
;
1571 if (params
.flags
& SNDRV_TIMER_PSFLG_EARLY_EVENT
)
1572 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EARLY_EVENT
;
1573 spin_unlock_irq(&t
->lock
);
1574 if (params
.queue_size
> 0 && (unsigned int)tu
->queue_size
!= params
.queue_size
) {
1576 ttr
= (snd_timer_tread_t
*)kmalloc(params
.queue_size
* sizeof(snd_timer_tread_t
), GFP_KERNEL
);
1579 tu
->queue_size
= params
.queue_size
;
1583 tr
= (snd_timer_read_t
*)kmalloc(params
.queue_size
* sizeof(snd_timer_read_t
), GFP_KERNEL
);
1586 tu
->queue_size
= params
.queue_size
;
1591 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1592 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1594 snd_timer_tread_t tread
;
1595 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1596 tread
.tstamp
.tv_sec
= 0;
1597 tread
.tstamp
.tv_nsec
= 0;
1599 snd_timer_user_append_to_tqueue(tu
, &tread
);
1601 snd_timer_read_t
*r
= &tu
->queue
[0];
1609 tu
->filter
= params
.filter
;
1610 tu
->ticks
= params
.ticks
;
1613 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1618 static int snd_timer_user_status(struct file
*file
, snd_timer_status_t __user
*_status
)
1620 snd_timer_user_t
*tu
;
1621 snd_timer_status_t status
;
1623 tu
= file
->private_data
;
1624 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1625 memset(&status
, 0, sizeof(status
));
1626 status
.tstamp
= tu
->tstamp
;
1627 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1628 status
.lost
= tu
->timeri
->lost
;
1629 status
.overrun
= tu
->overrun
;
1630 spin_lock_irq(&tu
->qlock
);
1631 status
.queue
= tu
->qused
;
1632 spin_unlock_irq(&tu
->qlock
);
1633 if (copy_to_user(_status
, &status
, sizeof(status
)))
1638 static int snd_timer_user_start(struct file
*file
)
1641 snd_timer_user_t
*tu
;
1643 tu
= file
->private_data
;
1644 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1645 snd_timer_stop(tu
->timeri
);
1646 tu
->timeri
->lost
= 0;
1647 tu
->last_resolution
= 0;
1648 return (err
= snd_timer_start(tu
->timeri
, tu
->ticks
)) < 0 ? err
: 0;
1651 static int snd_timer_user_stop(struct file
*file
)
1654 snd_timer_user_t
*tu
;
1656 tu
= file
->private_data
;
1657 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1658 return (err
= snd_timer_stop(tu
->timeri
)) < 0 ? err
: 0;
1661 static int snd_timer_user_continue(struct file
*file
)
1664 snd_timer_user_t
*tu
;
1666 tu
= file
->private_data
;
1667 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1668 tu
->timeri
->lost
= 0;
1669 return (err
= snd_timer_continue(tu
->timeri
)) < 0 ? err
: 0;
1672 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1674 snd_timer_user_t
*tu
;
1675 void __user
*argp
= (void __user
*)arg
;
1676 int __user
*p
= argp
;
1678 tu
= file
->private_data
;
1680 case SNDRV_TIMER_IOCTL_PVERSION
:
1681 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1682 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1683 return snd_timer_user_next_device(argp
);
1684 case SNDRV_TIMER_IOCTL_TREAD
:
1688 if (tu
->timeri
) /* too late */
1690 if (get_user(xarg
, p
))
1692 tu
->tread
= xarg
? 1 : 0;
1695 case SNDRV_TIMER_IOCTL_GINFO
:
1696 return snd_timer_user_ginfo(file
, argp
);
1697 case SNDRV_TIMER_IOCTL_GPARAMS
:
1698 return snd_timer_user_gparams(file
, argp
);
1699 case SNDRV_TIMER_IOCTL_GSTATUS
:
1700 return snd_timer_user_gstatus(file
, argp
);
1701 case SNDRV_TIMER_IOCTL_SELECT
:
1702 return snd_timer_user_tselect(file
, argp
);
1703 case SNDRV_TIMER_IOCTL_INFO
:
1704 return snd_timer_user_info(file
, argp
);
1705 case SNDRV_TIMER_IOCTL_PARAMS
:
1706 return snd_timer_user_params(file
, argp
);
1707 case SNDRV_TIMER_IOCTL_STATUS
:
1708 return snd_timer_user_status(file
, argp
);
1709 case SNDRV_TIMER_IOCTL_START
:
1710 return snd_timer_user_start(file
);
1711 case SNDRV_TIMER_IOCTL_STOP
:
1712 return snd_timer_user_stop(file
);
1713 case SNDRV_TIMER_IOCTL_CONTINUE
:
1714 return snd_timer_user_continue(file
);
1719 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
1721 snd_timer_user_t
*tu
;
1724 tu
= file
->private_data
;
1725 err
= fasync_helper(fd
, file
, on
, &tu
->fasync
);
1731 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*offset
)
1733 snd_timer_user_t
*tu
;
1734 long result
= 0, unit
;
1737 tu
= file
->private_data
;
1738 unit
= tu
->tread
? sizeof(snd_timer_tread_t
) : sizeof(snd_timer_read_t
);
1739 spin_lock_irq(&tu
->qlock
);
1740 while ((long)count
- result
>= unit
) {
1741 while (!tu
->qused
) {
1744 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1749 set_current_state(TASK_INTERRUPTIBLE
);
1750 init_waitqueue_entry(&wait
, current
);
1751 add_wait_queue(&tu
->qchange_sleep
, &wait
);
1753 spin_unlock_irq(&tu
->qlock
);
1755 spin_lock_irq(&tu
->qlock
);
1757 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
1759 if (signal_pending(current
)) {
1765 spin_unlock_irq(&tu
->qlock
);
1770 if (copy_to_user(buffer
, &tu
->tqueue
[tu
->qhead
++], sizeof(snd_timer_tread_t
))) {
1775 if (copy_to_user(buffer
, &tu
->queue
[tu
->qhead
++], sizeof(snd_timer_read_t
))) {
1781 tu
->qhead
%= tu
->queue_size
;
1786 spin_lock_irq(&tu
->qlock
);
1789 spin_unlock_irq(&tu
->qlock
);
1791 return result
> 0 ? result
: err
;
1794 static unsigned int snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
1797 snd_timer_user_t
*tu
;
1799 tu
= file
->private_data
;
1801 poll_wait(file
, &tu
->qchange_sleep
, wait
);
1805 mask
|= POLLIN
| POLLRDNORM
;
1810 #ifdef CONFIG_COMPAT
1811 #include "timer_compat.c"
1813 #define snd_timer_user_ioctl_compat NULL
1816 static struct file_operations snd_timer_f_ops
=
1818 .owner
= THIS_MODULE
,
1819 .read
= snd_timer_user_read
,
1820 .open
= snd_timer_user_open
,
1821 .release
= snd_timer_user_release
,
1822 .poll
= snd_timer_user_poll
,
1823 .unlocked_ioctl
= snd_timer_user_ioctl
,
1824 .compat_ioctl
= snd_timer_user_ioctl_compat
,
1825 .fasync
= snd_timer_user_fasync
,
1828 static snd_minor_t snd_timer_reg
=
1831 .f_ops
= &snd_timer_f_ops
,
1838 static snd_info_entry_t
*snd_timer_proc_entry
= NULL
;
1840 static int __init
alsa_timer_init(void)
1843 snd_info_entry_t
*entry
;
1845 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1846 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1, "system timer");
1848 if ((entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
)) != NULL
) {
1849 entry
->c
.text
.read_size
= SNDRV_TIMER_DEVICES
* 128;
1850 entry
->c
.text
.read
= snd_timer_proc_read
;
1851 if (snd_info_register(entry
) < 0) {
1852 snd_info_free_entry(entry
);
1856 snd_timer_proc_entry
= entry
;
1857 if ((err
= snd_timer_register_system()) < 0)
1858 snd_printk(KERN_ERR
"unable to register system timer (%i)\n", err
);
1859 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
,
1860 NULL
, 0, &snd_timer_reg
, "timer"))<0)
1861 snd_printk(KERN_ERR
"unable to register timer device (%i)\n", err
);
1865 static void __exit
alsa_timer_exit(void)
1867 struct list_head
*p
, *n
;
1869 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0);
1870 /* unregister the system timer */
1871 list_for_each_safe(p
, n
, &snd_timer_list
) {
1872 snd_timer_t
*timer
= (snd_timer_t
*)list_entry(p
, snd_timer_t
, device_list
);
1873 snd_timer_unregister(timer
);
1875 if (snd_timer_proc_entry
) {
1876 snd_info_unregister(snd_timer_proc_entry
);
1877 snd_timer_proc_entry
= NULL
;
1879 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1880 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
1884 module_init(alsa_timer_init
)
1885 module_exit(alsa_timer_exit
)
1887 EXPORT_SYMBOL(snd_timer_open
);
1888 EXPORT_SYMBOL(snd_timer_close
);
1889 EXPORT_SYMBOL(snd_timer_resolution
);
1890 EXPORT_SYMBOL(snd_timer_start
);
1891 EXPORT_SYMBOL(snd_timer_stop
);
1892 EXPORT_SYMBOL(snd_timer_continue
);
1893 EXPORT_SYMBOL(snd_timer_pause
);
1894 EXPORT_SYMBOL(snd_timer_new
);
1895 EXPORT_SYMBOL(snd_timer_notify
);
1896 EXPORT_SYMBOL(snd_timer_global_new
);
1897 EXPORT_SYMBOL(snd_timer_global_free
);
1898 EXPORT_SYMBOL(snd_timer_global_register
);
1899 EXPORT_SYMBOL(snd_timer_global_unregister
);
1900 EXPORT_SYMBOL(snd_timer_interrupt
);
1901 EXPORT_SYMBOL(snd_timer_system_resolution
);