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 <linux/string.h>
30 #include <sound/core.h>
31 #include <sound/timer.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/minors.h>
35 #include <sound/initval.h>
36 #include <linux/kmod.h>
38 #include <linux/kerneld.h>
41 #if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
42 #define DEFAULT_TIMER_LIMIT 3
43 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
44 #define DEFAULT_TIMER_LIMIT 2
46 #define DEFAULT_TIMER_LIMIT 1
49 static int timer_limit
= DEFAULT_TIMER_LIMIT
;
50 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
51 MODULE_DESCRIPTION("ALSA timer interface");
52 MODULE_LICENSE("GPL");
53 module_param(timer_limit
, int, 0444);
54 MODULE_PARM_DESC(timer_limit
, "Maximum global timers in system.");
57 snd_timer_instance_t
*timeri
;
58 int tread
; /* enhanced read with timestamps and events */
60 unsigned long overrun
;
65 snd_timer_read_t
*queue
;
66 snd_timer_tread_t
*tqueue
;
68 unsigned long last_resolution
;
70 struct timespec tstamp
; /* trigger tstamp */
71 wait_queue_head_t qchange_sleep
;
72 struct fasync_struct
*fasync
;
73 struct semaphore tread_sem
;
77 static LIST_HEAD(snd_timer_list
);
79 /* list of slave instances */
80 static LIST_HEAD(snd_timer_slave_list
);
82 /* lock for slave active lists */
83 static DEFINE_SPINLOCK(slave_active_lock
);
85 static DECLARE_MUTEX(register_mutex
);
87 static int snd_timer_free(snd_timer_t
*timer
);
88 static int snd_timer_dev_free(snd_device_t
*device
);
89 static int snd_timer_dev_register(snd_device_t
*device
);
90 static int snd_timer_dev_unregister(snd_device_t
*device
);
92 static void snd_timer_reschedule(snd_timer_t
* timer
, unsigned long ticks_left
);
95 * create a timer instance with the given owner string.
96 * when timer is not NULL, increments the module counter
98 static snd_timer_instance_t
*snd_timer_instance_new(char *owner
, snd_timer_t
*timer
)
100 snd_timer_instance_t
*timeri
;
101 timeri
= kcalloc(1, sizeof(*timeri
), GFP_KERNEL
);
104 timeri
->owner
= kstrdup(owner
, GFP_KERNEL
);
105 if (! timeri
->owner
) {
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
);
115 timeri
->timer
= timer
;
116 if (timer
&& timer
->card
&& !try_module_get(timer
->card
->module
)) {
117 kfree(timeri
->owner
);
126 * find a timer instance from the given timer id
128 static snd_timer_t
*snd_timer_find(snd_timer_id_t
*tid
)
130 snd_timer_t
*timer
= NULL
;
133 list_for_each(p
, &snd_timer_list
) {
134 timer
= (snd_timer_t
*)list_entry(p
, snd_timer_t
, device_list
);
136 if (timer
->tmr_class
!= tid
->dev_class
)
138 if ((timer
->tmr_class
== SNDRV_TIMER_CLASS_CARD
||
139 timer
->tmr_class
== SNDRV_TIMER_CLASS_PCM
) &&
140 (timer
->card
== NULL
||
141 timer
->card
->number
!= tid
->card
))
143 if (timer
->tmr_device
!= tid
->device
)
145 if (timer
->tmr_subdevice
!= tid
->subdevice
)
154 static void snd_timer_request(snd_timer_id_t
*tid
)
156 if (! current
->fs
->root
)
158 switch (tid
->dev_class
) {
159 case SNDRV_TIMER_CLASS_GLOBAL
:
160 if (tid
->device
< timer_limit
)
161 request_module("snd-timer-%i", tid
->device
);
163 case SNDRV_TIMER_CLASS_CARD
:
164 case SNDRV_TIMER_CLASS_PCM
:
165 if (tid
->card
< snd_ecards_limit
)
166 request_module("snd-card-%i", tid
->card
);
176 * look for a master instance matching with the slave id of the given slave.
177 * when found, relink the open_link of the slave.
179 * call this with register_mutex down.
181 static void snd_timer_check_slave(snd_timer_instance_t
*slave
)
184 snd_timer_instance_t
*master
;
185 struct list_head
*p
, *q
;
187 /* FIXME: it's really dumb to look up all entries.. */
188 list_for_each(p
, &snd_timer_list
) {
189 timer
= (snd_timer_t
*)list_entry(p
, snd_timer_t
, device_list
);
190 list_for_each(q
, &timer
->open_list_head
) {
191 master
= (snd_timer_instance_t
*)list_entry(q
, snd_timer_instance_t
, open_list
);
192 if (slave
->slave_class
== master
->slave_class
&&
193 slave
->slave_id
== master
->slave_id
) {
194 list_del(&slave
->open_list
);
195 list_add_tail(&slave
->open_list
, &master
->slave_list_head
);
196 spin_lock_irq(&slave_active_lock
);
197 slave
->master
= master
;
198 slave
->timer
= master
->timer
;
199 spin_unlock_irq(&slave_active_lock
);
207 * look for slave instances matching with the slave id of the given master.
208 * when found, relink the open_link of slaves.
210 * call this with register_mutex down.
212 static void snd_timer_check_master(snd_timer_instance_t
*master
)
214 snd_timer_instance_t
*slave
;
215 struct list_head
*p
, *n
;
217 /* check all pending slaves */
218 list_for_each_safe(p
, n
, &snd_timer_slave_list
) {
219 slave
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, open_list
);
220 if (slave
->slave_class
== master
->slave_class
&&
221 slave
->slave_id
== master
->slave_id
) {
223 list_add_tail(p
, &master
->slave_list_head
);
224 spin_lock_irq(&slave_active_lock
);
225 slave
->master
= master
;
226 slave
->timer
= master
->timer
;
227 if (slave
->flags
& SNDRV_TIMER_IFLG_RUNNING
)
228 list_add_tail(&slave
->active_list
, &master
->slave_active_head
);
229 spin_unlock_irq(&slave_active_lock
);
235 * open a timer instance
236 * when opening a master, the slave id must be here given.
238 int snd_timer_open(snd_timer_instance_t
**ti
,
239 char *owner
, snd_timer_id_t
*tid
,
240 unsigned int slave_id
)
243 snd_timer_instance_t
*timeri
= NULL
;
245 if (tid
->dev_class
== SNDRV_TIMER_CLASS_SLAVE
) {
246 /* open a slave instance */
247 if (tid
->dev_sclass
<= SNDRV_TIMER_SCLASS_NONE
||
248 tid
->dev_sclass
> SNDRV_TIMER_SCLASS_OSS_SEQUENCER
) {
249 snd_printd("invalid slave class %i\n", tid
->dev_sclass
);
252 down(®ister_mutex
);
253 timeri
= snd_timer_instance_new(owner
, NULL
);
254 timeri
->slave_class
= tid
->dev_sclass
;
255 timeri
->slave_id
= tid
->device
;
256 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
257 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
258 snd_timer_check_slave(timeri
);
264 /* open a master instance */
265 down(®ister_mutex
);
266 timer
= snd_timer_find(tid
);
270 snd_timer_request(tid
);
271 down(®ister_mutex
);
272 timer
= snd_timer_find(tid
);
276 if (!list_empty(&timer
->open_list_head
)) {
277 timeri
= (snd_timer_instance_t
*)list_entry(timer
->open_list_head
.next
, snd_timer_instance_t
, open_list
);
278 if (timeri
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
283 timeri
= snd_timer_instance_new(owner
, timer
);
285 timeri
->slave_class
= tid
->dev_sclass
;
286 timeri
->slave_id
= slave_id
;
287 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
)
288 timer
->hw
.open(timer
);
289 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
290 snd_timer_check_master(timeri
);
301 static int _snd_timer_stop(snd_timer_instance_t
* timeri
, int keep_flag
, enum sndrv_timer_event event
);
304 * close a timer instance
306 int snd_timer_close(snd_timer_instance_t
* timeri
)
308 snd_timer_t
*timer
= NULL
;
309 struct list_head
*p
, *n
;
310 snd_timer_instance_t
*slave
;
312 snd_assert(timeri
!= NULL
, return -ENXIO
);
314 /* force to stop the timer */
315 snd_timer_stop(timeri
);
317 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
318 /* wait, until the active callback is finished */
319 spin_lock_irq(&slave_active_lock
);
320 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
321 spin_unlock_irq(&slave_active_lock
);
323 spin_lock_irq(&slave_active_lock
);
325 spin_unlock_irq(&slave_active_lock
);
326 down(®ister_mutex
);
327 list_del(&timeri
->open_list
);
330 timer
= timeri
->timer
;
331 /* wait, until the active callback is finished */
332 spin_lock_irq(&timer
->lock
);
333 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
334 spin_unlock_irq(&timer
->lock
);
336 spin_lock_irq(&timer
->lock
);
338 spin_unlock_irq(&timer
->lock
);
339 down(®ister_mutex
);
340 list_del(&timeri
->open_list
);
341 if (timer
&& list_empty(&timer
->open_list_head
) && timer
->hw
.close
)
342 timer
->hw
.close(timer
);
343 /* remove slave links */
344 list_for_each_safe(p
, n
, &timeri
->slave_list_head
) {
345 slave
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, open_list
);
346 spin_lock_irq(&slave_active_lock
);
347 _snd_timer_stop(slave
, 1, SNDRV_TIMER_EVENT_RESOLUTION
);
349 list_add_tail(p
, &snd_timer_slave_list
);
350 slave
->master
= NULL
;
352 spin_unlock_irq(&slave_active_lock
);
356 if (timeri
->private_free
)
357 timeri
->private_free(timeri
);
358 kfree(timeri
->owner
);
360 if (timer
&& timer
->card
)
361 module_put(timer
->card
->module
);
365 unsigned long snd_timer_resolution(snd_timer_instance_t
* timeri
)
371 if ((timer
= timeri
->timer
) != NULL
) {
372 if (timer
->hw
.c_resolution
)
373 return timer
->hw
.c_resolution(timer
);
374 return timer
->hw
.resolution
;
379 static void snd_timer_notify1(snd_timer_instance_t
*ti
, enum sndrv_timer_event event
)
383 unsigned long resolution
= 0;
384 snd_timer_instance_t
*ts
;
386 struct timespec tstamp
;
388 snd_timestamp_now(&tstamp
, 1);
389 snd_assert(event
>= SNDRV_TIMER_EVENT_START
&& event
<= SNDRV_TIMER_EVENT_PAUSE
, return);
390 if (event
== SNDRV_TIMER_EVENT_START
|| event
== SNDRV_TIMER_EVENT_CONTINUE
)
391 resolution
= snd_timer_resolution(ti
);
393 ti
->ccallback(ti
, SNDRV_TIMER_EVENT_START
, &tstamp
, resolution
);
394 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
399 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
401 spin_lock_irqsave(&timer
->lock
, flags
);
402 list_for_each(n
, &ti
->slave_active_head
) {
403 ts
= (snd_timer_instance_t
*)list_entry(n
, snd_timer_instance_t
, active_list
);
405 ts
->ccallback(ti
, event
+ 100, &tstamp
, resolution
);
407 spin_unlock_irqrestore(&timer
->lock
, flags
);
410 static int snd_timer_start1(snd_timer_t
*timer
, snd_timer_instance_t
*timeri
, unsigned long sticks
)
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
)
417 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
418 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
419 return 1; /* delayed start */
421 timer
->sticks
= sticks
;
422 timer
->hw
.start(timer
);
425 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
430 static int snd_timer_start_slave(snd_timer_instance_t
*timeri
)
434 spin_lock_irqsave(&slave_active_lock
, flags
);
435 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
437 list_add_tail(&timeri
->active_list
, &timeri
->master
->slave_active_head
);
438 spin_unlock_irqrestore(&slave_active_lock
, flags
);
439 return 1; /* delayed start */
443 * start the timer instance
445 int snd_timer_start(snd_timer_instance_t
* timeri
, unsigned int ticks
)
448 int result
= -EINVAL
;
451 if (timeri
== NULL
|| ticks
< 1)
453 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
454 result
= snd_timer_start_slave(timeri
);
455 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
458 timer
= timeri
->timer
;
461 spin_lock_irqsave(&timer
->lock
, flags
);
462 timeri
->ticks
= timeri
->cticks
= ticks
;
464 result
= snd_timer_start1(timer
, timeri
, ticks
);
465 spin_unlock_irqrestore(&timer
->lock
, flags
);
466 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
470 static int _snd_timer_stop(snd_timer_instance_t
* timeri
, int keep_flag
, enum sndrv_timer_event event
)
475 snd_assert(timeri
!= NULL
, return -ENXIO
);
477 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
479 spin_lock_irqsave(&slave_active_lock
, flags
);
480 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
481 spin_unlock_irqrestore(&slave_active_lock
, flags
);
485 timer
= timeri
->timer
;
488 spin_lock_irqsave(&timer
->lock
, flags
);
489 list_del_init(&timeri
->ack_list
);
490 list_del_init(&timeri
->active_list
);
491 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
492 !(--timer
->running
)) {
493 timer
->hw
.stop(timer
);
494 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
495 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
496 snd_timer_reschedule(timer
, 0);
497 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
498 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
499 timer
->hw
.start(timer
);
504 timeri
->flags
&= ~(SNDRV_TIMER_IFLG_RUNNING
|SNDRV_TIMER_IFLG_START
);
505 spin_unlock_irqrestore(&timer
->lock
, flags
);
507 if (event
!= SNDRV_TIMER_EVENT_RESOLUTION
)
508 snd_timer_notify1(timeri
, event
);
513 * stop the timer instance.
515 * do not call this from the timer callback!
517 int snd_timer_stop(snd_timer_instance_t
* timeri
)
523 err
= _snd_timer_stop(timeri
, 0, SNDRV_TIMER_EVENT_STOP
);
526 timer
= timeri
->timer
;
527 spin_lock_irqsave(&timer
->lock
, flags
);
528 timeri
->cticks
= timeri
->ticks
;
530 spin_unlock_irqrestore(&timer
->lock
, flags
);
535 * start again.. the tick is kept.
537 int snd_timer_continue(snd_timer_instance_t
* timeri
)
540 int result
= -EINVAL
;
545 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
546 return snd_timer_start_slave(timeri
);
547 timer
= timeri
->timer
;
550 spin_lock_irqsave(&timer
->lock
, flags
);
554 result
= snd_timer_start1(timer
, timeri
, timer
->sticks
);
555 spin_unlock_irqrestore(&timer
->lock
, flags
);
556 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_CONTINUE
);
561 * pause.. remember the ticks left
563 int snd_timer_pause(snd_timer_instance_t
* timeri
)
565 return _snd_timer_stop(timeri
, 0, SNDRV_TIMER_EVENT_PAUSE
);
569 * reschedule the timer
571 * start pending instances and check the scheduling ticks.
572 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
574 static void snd_timer_reschedule(snd_timer_t
* timer
, unsigned long ticks_left
)
576 snd_timer_instance_t
*ti
;
577 unsigned long ticks
= ~0UL;
580 list_for_each(p
, &timer
->active_list_head
) {
581 ti
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, active_list
);
582 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
583 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
584 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
587 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
588 if (ticks
> ti
->cticks
)
593 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
596 if (ticks
> timer
->hw
.ticks
)
597 ticks
= timer
->hw
.ticks
;
598 if (ticks_left
!= ticks
)
599 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
600 timer
->sticks
= ticks
;
607 static void snd_timer_tasklet(unsigned long arg
)
609 snd_timer_t
*timer
= (snd_timer_t
*) arg
;
610 snd_timer_instance_t
*ti
;
612 unsigned long resolution
, ticks
;
614 spin_lock(&timer
->lock
);
615 /* now process all callbacks */
616 while (!list_empty(&timer
->sack_list_head
)) {
617 p
= timer
->sack_list_head
.next
; /* get first item */
618 ti
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, ack_list
);
620 /* remove from ack_list and make empty */
625 resolution
= ti
->resolution
;
627 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
628 spin_unlock(&timer
->lock
);
630 ti
->callback(ti
, resolution
, ticks
);
631 spin_lock(&timer
->lock
);
632 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
634 spin_unlock(&timer
->lock
);
640 * ticks_left is usually equal to timer->sticks.
643 void snd_timer_interrupt(snd_timer_t
* timer
, unsigned long ticks_left
)
645 snd_timer_instance_t
*ti
, *ts
;
646 unsigned long resolution
, ticks
;
647 struct list_head
*p
, *q
, *n
;
653 spin_lock(&timer
->lock
);
655 /* remember the current resolution */
656 if (timer
->hw
.c_resolution
)
657 resolution
= timer
->hw
.c_resolution(timer
);
659 resolution
= timer
->hw
.resolution
;
661 /* loop for all active instances
662 * here we cannot use list_for_each because the active_list of a processed
663 * instance is relinked to done_list_head before callback is called.
665 list_for_each_safe(p
, n
, &timer
->active_list_head
) {
666 ti
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, active_list
);
667 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
669 ti
->pticks
+= ticks_left
;
670 ti
->resolution
= resolution
;
671 if (ti
->cticks
< ticks_left
)
674 ti
->cticks
-= ticks_left
;
675 if (ti
->cticks
) /* not expired */
677 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
678 ti
->cticks
= ti
->ticks
;
680 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
681 if (--timer
->running
)
684 if (list_empty(&ti
->ack_list
)) {
685 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
686 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
)) {
687 list_add_tail(&ti
->ack_list
, &timer
->ack_list_head
);
689 list_add_tail(&ti
->ack_list
, &timer
->sack_list_head
);
692 list_for_each(q
, &ti
->slave_active_head
) {
693 ts
= (snd_timer_instance_t
*)list_entry(q
, snd_timer_instance_t
, active_list
);
694 ts
->pticks
= ti
->pticks
;
695 ts
->resolution
= resolution
;
696 if (list_empty(&ts
->ack_list
)) {
697 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
698 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
)) {
699 list_add_tail(&ts
->ack_list
, &timer
->ack_list_head
);
701 list_add_tail(&ts
->ack_list
, &timer
->sack_list_head
);
706 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
707 snd_timer_reschedule(timer
, ticks_left
);
708 if (timer
->running
) {
709 if (timer
->hw
.flags
& SNDRV_TIMER_HW_STOP
) {
710 timer
->hw
.stop(timer
);
711 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
713 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_AUTO
) ||
714 (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
)) {
716 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
717 timer
->hw
.start(timer
);
720 timer
->hw
.stop(timer
);
723 /* now process all fast callbacks */
724 while (!list_empty(&timer
->ack_list_head
)) {
725 p
= timer
->ack_list_head
.next
; /* get first item */
726 ti
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, ack_list
);
728 /* remove from ack_list and make empty */
734 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
735 spin_unlock(&timer
->lock
);
737 ti
->callback(ti
, resolution
, ticks
);
738 spin_lock(&timer
->lock
);
739 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
742 /* do we have any slow callbacks? */
743 use_tasklet
= !list_empty(&timer
->sack_list_head
);
744 spin_unlock(&timer
->lock
);
747 tasklet_hi_schedule(&timer
->task_queue
);
754 int snd_timer_new(snd_card_t
*card
, char *id
, snd_timer_id_t
*tid
, snd_timer_t
** rtimer
)
758 static snd_device_ops_t ops
= {
759 .dev_free
= snd_timer_dev_free
,
760 .dev_register
= snd_timer_dev_register
,
761 .dev_unregister
= snd_timer_dev_unregister
764 snd_assert(tid
!= NULL
, return -EINVAL
);
765 snd_assert(rtimer
!= NULL
, return -EINVAL
);
767 timer
= kcalloc(1, sizeof(*timer
), GFP_KERNEL
);
770 timer
->tmr_class
= tid
->dev_class
;
772 timer
->tmr_device
= tid
->device
;
773 timer
->tmr_subdevice
= tid
->subdevice
;
775 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
776 INIT_LIST_HEAD(&timer
->device_list
);
777 INIT_LIST_HEAD(&timer
->open_list_head
);
778 INIT_LIST_HEAD(&timer
->active_list_head
);
779 INIT_LIST_HEAD(&timer
->ack_list_head
);
780 INIT_LIST_HEAD(&timer
->sack_list_head
);
781 spin_lock_init(&timer
->lock
);
782 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
, (unsigned long)timer
);
784 if ((err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
)) < 0) {
785 snd_timer_free(timer
);
793 static int snd_timer_free(snd_timer_t
*timer
)
795 snd_assert(timer
!= NULL
, return -ENXIO
);
796 if (timer
->private_free
)
797 timer
->private_free(timer
);
802 static int snd_timer_dev_free(snd_device_t
*device
)
804 snd_timer_t
*timer
= device
->device_data
;
805 return snd_timer_free(timer
);
808 static int snd_timer_dev_register(snd_device_t
*dev
)
810 snd_timer_t
*timer
= dev
->device_data
;
814 snd_assert(timer
!= NULL
&& timer
->hw
.start
!= NULL
&& timer
->hw
.stop
!= NULL
, return -ENXIO
);
815 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
816 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
819 down(®ister_mutex
);
820 list_for_each(p
, &snd_timer_list
) {
821 timer1
= (snd_timer_t
*)list_entry(p
, snd_timer_t
, device_list
);
822 if (timer1
->tmr_class
> timer
->tmr_class
)
824 if (timer1
->tmr_class
< timer
->tmr_class
)
826 if (timer1
->card
&& timer
->card
) {
827 if (timer1
->card
->number
> timer
->card
->number
)
829 if (timer1
->card
->number
< timer
->card
->number
)
832 if (timer1
->tmr_device
> timer
->tmr_device
)
834 if (timer1
->tmr_device
< timer
->tmr_device
)
836 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
838 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
844 list_add_tail(&timer
->device_list
, p
);
849 static int snd_timer_unregister(snd_timer_t
*timer
)
851 struct list_head
*p
, *n
;
852 snd_timer_instance_t
*ti
;
854 snd_assert(timer
!= NULL
, return -ENXIO
);
855 down(®ister_mutex
);
856 if (! list_empty(&timer
->open_list_head
)) {
857 snd_printk(KERN_WARNING
"timer 0x%lx is busy?\n", (long)timer
);
858 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
860 ti
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, open_list
);
864 list_del(&timer
->device_list
);
866 return snd_timer_free(timer
);
869 static int snd_timer_dev_unregister(snd_device_t
*device
)
871 snd_timer_t
*timer
= device
->device_data
;
872 return snd_timer_unregister(timer
);
875 void snd_timer_notify(snd_timer_t
*timer
, enum sndrv_timer_event event
, struct timespec
*tstamp
)
878 unsigned long resolution
= 0;
879 snd_timer_instance_t
*ti
, *ts
;
880 struct list_head
*p
, *n
;
882 snd_runtime_check(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
, return);
883 snd_assert(event
>= SNDRV_TIMER_EVENT_MSTART
&& event
<= SNDRV_TIMER_EVENT_MRESUME
, return);
884 spin_lock_irqsave(&timer
->lock
, flags
);
885 if (event
== SNDRV_TIMER_EVENT_MSTART
||
886 event
== SNDRV_TIMER_EVENT_MCONTINUE
||
887 event
== SNDRV_TIMER_EVENT_MRESUME
) {
888 if (timer
->hw
.c_resolution
)
889 resolution
= timer
->hw
.c_resolution(timer
);
891 resolution
= timer
->hw
.resolution
;
893 list_for_each(p
, &timer
->active_list_head
) {
894 ti
= (snd_timer_instance_t
*)list_entry(p
, snd_timer_instance_t
, active_list
);
896 ti
->ccallback(ti
, event
, tstamp
, resolution
);
897 list_for_each(n
, &ti
->slave_active_head
) {
898 ts
= (snd_timer_instance_t
*)list_entry(n
, snd_timer_instance_t
, active_list
);
900 ts
->ccallback(ts
, event
, tstamp
, resolution
);
903 spin_unlock_irqrestore(&timer
->lock
, flags
);
907 * exported functions for global timers
909 int snd_timer_global_new(char *id
, int device
, snd_timer_t
**rtimer
)
913 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
914 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
918 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
921 int snd_timer_global_free(snd_timer_t
*timer
)
923 return snd_timer_free(timer
);
926 int snd_timer_global_register(snd_timer_t
*timer
)
930 memset(&dev
, 0, sizeof(dev
));
931 dev
.device_data
= timer
;
932 return snd_timer_dev_register(&dev
);
935 int snd_timer_global_unregister(snd_timer_t
*timer
)
937 return snd_timer_unregister(timer
);
944 struct snd_timer_system_private
{
945 struct timer_list tlist
;
946 struct timer
* timer
;
947 unsigned long last_expires
;
948 unsigned long last_jiffies
;
949 unsigned long correction
;
952 static void snd_timer_s_function(unsigned long data
)
954 snd_timer_t
*timer
= (snd_timer_t
*)data
;
955 struct snd_timer_system_private
*priv
= timer
->private_data
;
956 unsigned long jiff
= jiffies
;
957 if (time_after(jiff
, priv
->last_expires
))
958 priv
->correction
= (long)jiff
- (long)priv
->last_expires
;
959 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
962 static int snd_timer_s_start(snd_timer_t
* timer
)
964 struct snd_timer_system_private
*priv
;
967 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
968 njiff
= (priv
->last_jiffies
= jiffies
);
969 if (priv
->correction
> timer
->sticks
- 1) {
970 priv
->correction
-= timer
->sticks
- 1;
973 njiff
+= timer
->sticks
- priv
->correction
;
974 priv
->correction
-= timer
->sticks
;
976 priv
->last_expires
= priv
->tlist
.expires
= njiff
;
977 add_timer(&priv
->tlist
);
981 static int snd_timer_s_stop(snd_timer_t
* timer
)
983 struct snd_timer_system_private
*priv
;
986 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
987 del_timer(&priv
->tlist
);
989 if (time_before(jiff
, priv
->last_expires
))
990 timer
->sticks
= priv
->last_expires
- jiff
;
996 static struct _snd_timer_hardware snd_timer_system
=
998 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
999 .resolution
= 1000000000L / HZ
,
1001 .start
= snd_timer_s_start
,
1002 .stop
= snd_timer_s_stop
1005 static void snd_timer_free_system(snd_timer_t
*timer
)
1007 kfree(timer
->private_data
);
1010 static int snd_timer_register_system(void)
1013 struct snd_timer_system_private
*priv
;
1016 if ((err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
)) < 0)
1018 strcpy(timer
->name
, "system timer");
1019 timer
->hw
= snd_timer_system
;
1020 priv
= kcalloc(1, sizeof(*priv
), GFP_KERNEL
);
1022 snd_timer_free(timer
);
1025 init_timer(&priv
->tlist
);
1026 priv
->tlist
.function
= snd_timer_s_function
;
1027 priv
->tlist
.data
= (unsigned long) timer
;
1028 timer
->private_data
= priv
;
1029 timer
->private_free
= snd_timer_free_system
;
1030 return snd_timer_global_register(timer
);
1037 static void snd_timer_proc_read(snd_info_entry_t
*entry
,
1038 snd_info_buffer_t
* buffer
)
1040 unsigned long flags
;
1042 snd_timer_instance_t
*ti
;
1043 struct list_head
*p
, *q
;
1045 down(®ister_mutex
);
1046 list_for_each(p
, &snd_timer_list
) {
1047 timer
= (snd_timer_t
*)list_entry(p
, snd_timer_t
, device_list
);
1048 switch (timer
->tmr_class
) {
1049 case SNDRV_TIMER_CLASS_GLOBAL
:
1050 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1052 case SNDRV_TIMER_CLASS_CARD
:
1053 snd_iprintf(buffer
, "C%i-%i: ", timer
->card
->number
, timer
->tmr_device
);
1055 case SNDRV_TIMER_CLASS_PCM
:
1056 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
, timer
->tmr_device
, timer
->tmr_subdevice
);
1059 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
, timer
->card
? timer
->card
->number
: -1, timer
->tmr_device
, timer
->tmr_subdevice
);
1061 snd_iprintf(buffer
, "%s :", timer
->name
);
1062 if (timer
->hw
.resolution
)
1063 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)", timer
->hw
.resolution
/ 1000, timer
->hw
.resolution
% 1000, timer
->hw
.ticks
);
1064 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1065 snd_iprintf(buffer
, " SLAVE");
1066 snd_iprintf(buffer
, "\n");
1067 spin_lock_irqsave(&timer
->lock
, flags
);
1068 list_for_each(q
, &timer
->open_list_head
) {
1069 ti
= (snd_timer_instance_t
*)list_entry(q
, snd_timer_instance_t
, open_list
);
1070 snd_iprintf(buffer
, " Client %s : %s : lost interrupts %li\n",
1071 ti
->owner
? ti
->owner
: "unknown",
1072 ti
->flags
& (SNDRV_TIMER_IFLG_START
|SNDRV_TIMER_IFLG_RUNNING
) ? "running" : "stopped",
1075 spin_unlock_irqrestore(&timer
->lock
, flags
);
1077 up(®ister_mutex
);
1081 * USER SPACE interface
1084 static void snd_timer_user_interrupt(snd_timer_instance_t
*timeri
,
1085 unsigned long resolution
,
1086 unsigned long ticks
)
1088 snd_timer_user_t
*tu
= timeri
->callback_data
;
1089 snd_timer_read_t
*r
;
1092 spin_lock(&tu
->qlock
);
1093 if (tu
->qused
> 0) {
1094 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1095 r
= &tu
->queue
[prev
];
1096 if (r
->resolution
== resolution
) {
1101 if (tu
->qused
>= tu
->queue_size
) {
1104 r
= &tu
->queue
[tu
->qtail
++];
1105 tu
->qtail
%= tu
->queue_size
;
1106 r
->resolution
= resolution
;
1111 spin_unlock(&tu
->qlock
);
1112 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1113 wake_up(&tu
->qchange_sleep
);
1116 static void snd_timer_user_append_to_tqueue(snd_timer_user_t
*tu
, snd_timer_tread_t
*tread
)
1118 if (tu
->qused
>= tu
->queue_size
) {
1121 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1122 tu
->qtail
%= tu
->queue_size
;
1127 static void snd_timer_user_ccallback(snd_timer_instance_t
*timeri
,
1128 enum sndrv_timer_event event
,
1129 struct timespec
*tstamp
,
1130 unsigned long resolution
)
1132 snd_timer_user_t
*tu
= timeri
->callback_data
;
1133 snd_timer_tread_t r1
;
1135 if (event
>= SNDRV_TIMER_EVENT_START
&& event
<= SNDRV_TIMER_EVENT_PAUSE
)
1136 tu
->tstamp
= *tstamp
;
1137 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1140 r1
.tstamp
= *tstamp
;
1141 r1
.val
= resolution
;
1142 spin_lock(&tu
->qlock
);
1143 snd_timer_user_append_to_tqueue(tu
, &r1
);
1144 spin_unlock(&tu
->qlock
);
1145 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1146 wake_up(&tu
->qchange_sleep
);
1149 static void snd_timer_user_tinterrupt(snd_timer_instance_t
*timeri
,
1150 unsigned long resolution
,
1151 unsigned long ticks
)
1153 snd_timer_user_t
*tu
= timeri
->callback_data
;
1154 snd_timer_tread_t
*r
, r1
;
1155 struct timespec tstamp
;
1156 int prev
, append
= 0;
1158 snd_timestamp_zero(&tstamp
);
1159 spin_lock(&tu
->qlock
);
1160 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
)|(1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1161 spin_unlock(&tu
->qlock
);
1164 if (tu
->last_resolution
!= resolution
|| ticks
> 0)
1165 snd_timestamp_now(&tstamp
, 1);
1166 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) && tu
->last_resolution
!= resolution
) {
1167 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1169 r1
.val
= resolution
;
1170 snd_timer_user_append_to_tqueue(tu
, &r1
);
1171 tu
->last_resolution
= resolution
;
1174 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1178 if (tu
->qused
> 0) {
1179 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1180 r
= &tu
->tqueue
[prev
];
1181 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1188 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1191 snd_timer_user_append_to_tqueue(tu
, &r1
);
1194 spin_unlock(&tu
->qlock
);
1197 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1198 wake_up(&tu
->qchange_sleep
);
1201 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1203 snd_timer_user_t
*tu
;
1205 tu
= kcalloc(1, sizeof(*tu
), GFP_KERNEL
);
1208 spin_lock_init(&tu
->qlock
);
1209 init_waitqueue_head(&tu
->qchange_sleep
);
1210 init_MUTEX(&tu
->tread_sem
);
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
;
1460 down(&tu
->tread_sem
);
1462 snd_timer_close(tu
->timeri
);
1465 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
))) {
1469 sprintf(str
, "application %i", current
->pid
);
1470 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1471 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1472 if ((err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
)) < 0)
1480 tu
->tqueue
= (snd_timer_tread_t
*)kmalloc(tu
->queue_size
* sizeof(snd_timer_tread_t
), GFP_KERNEL
);
1481 if (tu
->tqueue
== NULL
)
1484 tu
->queue
= (snd_timer_read_t
*)kmalloc(tu
->queue_size
* sizeof(snd_timer_read_t
), GFP_KERNEL
);
1485 if (tu
->queue
== NULL
)
1490 snd_timer_close(tu
->timeri
);
1493 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1494 tu
->timeri
->callback
= tu
->tread
? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1495 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1496 tu
->timeri
->callback_data
= (void *)tu
;
1504 static int snd_timer_user_info(struct file
*file
, snd_timer_info_t __user
*_info
)
1506 snd_timer_user_t
*tu
;
1507 snd_timer_info_t
*info
;
1511 tu
= file
->private_data
;
1512 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1513 t
= tu
->timeri
->timer
;
1514 snd_assert(t
!= NULL
, return -ENXIO
);
1516 info
= kcalloc(1, sizeof(*info
), GFP_KERNEL
);
1519 info
->card
= t
->card
? t
->card
->number
: -1;
1520 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1521 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1522 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1523 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1524 info
->resolution
= t
->hw
.resolution
;
1525 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1531 static int snd_timer_user_params(struct file
*file
, snd_timer_params_t __user
*_params
)
1533 snd_timer_user_t
*tu
;
1534 snd_timer_params_t params
;
1536 snd_timer_read_t
*tr
;
1537 snd_timer_tread_t
*ttr
;
1540 tu
= file
->private_data
;
1541 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1542 t
= tu
->timeri
->timer
;
1543 snd_assert(t
!= NULL
, return -ENXIO
);
1544 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
1546 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) && params
.ticks
< 1) {
1550 if (params
.queue_size
> 0 && (params
.queue_size
< 32 || params
.queue_size
> 1024)) {
1554 if (params
.filter
& ~((1<<SNDRV_TIMER_EVENT_RESOLUTION
)|
1555 (1<<SNDRV_TIMER_EVENT_TICK
)|
1556 (1<<SNDRV_TIMER_EVENT_START
)|
1557 (1<<SNDRV_TIMER_EVENT_STOP
)|
1558 (1<<SNDRV_TIMER_EVENT_CONTINUE
)|
1559 (1<<SNDRV_TIMER_EVENT_PAUSE
)|
1560 (1<<SNDRV_TIMER_EVENT_SUSPEND
)|
1561 (1<<SNDRV_TIMER_EVENT_RESUME
)|
1562 (1<<SNDRV_TIMER_EVENT_MSTART
)|
1563 (1<<SNDRV_TIMER_EVENT_MSTOP
)|
1564 (1<<SNDRV_TIMER_EVENT_MCONTINUE
)|
1565 (1<<SNDRV_TIMER_EVENT_MPAUSE
)|
1566 (1<<SNDRV_TIMER_EVENT_MSUSPEND
)|
1567 (1<<SNDRV_TIMER_EVENT_MRESUME
))) {
1571 snd_timer_stop(tu
->timeri
);
1572 spin_lock_irq(&t
->lock
);
1573 tu
->timeri
->flags
&= ~(SNDRV_TIMER_IFLG_AUTO
|
1574 SNDRV_TIMER_IFLG_EXCLUSIVE
|
1575 SNDRV_TIMER_IFLG_EARLY_EVENT
);
1576 if (params
.flags
& SNDRV_TIMER_PSFLG_AUTO
)
1577 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
1578 if (params
.flags
& SNDRV_TIMER_PSFLG_EXCLUSIVE
)
1579 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EXCLUSIVE
;
1580 if (params
.flags
& SNDRV_TIMER_PSFLG_EARLY_EVENT
)
1581 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EARLY_EVENT
;
1582 spin_unlock_irq(&t
->lock
);
1583 if (params
.queue_size
> 0 && (unsigned int)tu
->queue_size
!= params
.queue_size
) {
1585 ttr
= (snd_timer_tread_t
*)kmalloc(params
.queue_size
* sizeof(snd_timer_tread_t
), GFP_KERNEL
);
1588 tu
->queue_size
= params
.queue_size
;
1592 tr
= (snd_timer_read_t
*)kmalloc(params
.queue_size
* sizeof(snd_timer_read_t
), GFP_KERNEL
);
1595 tu
->queue_size
= params
.queue_size
;
1600 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1601 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1603 snd_timer_tread_t tread
;
1604 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1605 tread
.tstamp
.tv_sec
= 0;
1606 tread
.tstamp
.tv_nsec
= 0;
1608 snd_timer_user_append_to_tqueue(tu
, &tread
);
1610 snd_timer_read_t
*r
= &tu
->queue
[0];
1618 tu
->filter
= params
.filter
;
1619 tu
->ticks
= params
.ticks
;
1622 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1627 static int snd_timer_user_status(struct file
*file
, snd_timer_status_t __user
*_status
)
1629 snd_timer_user_t
*tu
;
1630 snd_timer_status_t status
;
1632 tu
= file
->private_data
;
1633 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1634 memset(&status
, 0, sizeof(status
));
1635 status
.tstamp
= tu
->tstamp
;
1636 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1637 status
.lost
= tu
->timeri
->lost
;
1638 status
.overrun
= tu
->overrun
;
1639 spin_lock_irq(&tu
->qlock
);
1640 status
.queue
= tu
->qused
;
1641 spin_unlock_irq(&tu
->qlock
);
1642 if (copy_to_user(_status
, &status
, sizeof(status
)))
1647 static int snd_timer_user_start(struct file
*file
)
1650 snd_timer_user_t
*tu
;
1652 tu
= file
->private_data
;
1653 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1654 snd_timer_stop(tu
->timeri
);
1655 tu
->timeri
->lost
= 0;
1656 tu
->last_resolution
= 0;
1657 return (err
= snd_timer_start(tu
->timeri
, tu
->ticks
)) < 0 ? err
: 0;
1660 static int snd_timer_user_stop(struct file
*file
)
1663 snd_timer_user_t
*tu
;
1665 tu
= file
->private_data
;
1666 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1667 return (err
= snd_timer_stop(tu
->timeri
)) < 0 ? err
: 0;
1670 static int snd_timer_user_continue(struct file
*file
)
1673 snd_timer_user_t
*tu
;
1675 tu
= file
->private_data
;
1676 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1677 tu
->timeri
->lost
= 0;
1678 return (err
= snd_timer_continue(tu
->timeri
)) < 0 ? err
: 0;
1681 static int snd_timer_user_pause(struct file
*file
)
1684 snd_timer_user_t
*tu
;
1686 tu
= file
->private_data
;
1687 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1688 return (err
= snd_timer_pause(tu
->timeri
)) < 0 ? err
: 0;
1692 SNDRV_TIMER_IOCTL_START_OLD
= _IO('T', 0x20),
1693 SNDRV_TIMER_IOCTL_STOP_OLD
= _IO('T', 0x21),
1694 SNDRV_TIMER_IOCTL_CONTINUE_OLD
= _IO('T', 0x22),
1695 SNDRV_TIMER_IOCTL_PAUSE_OLD
= _IO('T', 0x23),
1698 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1700 snd_timer_user_t
*tu
;
1701 void __user
*argp
= (void __user
*)arg
;
1702 int __user
*p
= argp
;
1704 tu
= file
->private_data
;
1706 case SNDRV_TIMER_IOCTL_PVERSION
:
1707 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1708 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1709 return snd_timer_user_next_device(argp
);
1710 case SNDRV_TIMER_IOCTL_TREAD
:
1714 down(&tu
->tread_sem
);
1715 if (tu
->timeri
) { /* too late */
1719 if (get_user(xarg
, p
)) {
1723 tu
->tread
= xarg
? 1 : 0;
1727 case SNDRV_TIMER_IOCTL_GINFO
:
1728 return snd_timer_user_ginfo(file
, argp
);
1729 case SNDRV_TIMER_IOCTL_GPARAMS
:
1730 return snd_timer_user_gparams(file
, argp
);
1731 case SNDRV_TIMER_IOCTL_GSTATUS
:
1732 return snd_timer_user_gstatus(file
, argp
);
1733 case SNDRV_TIMER_IOCTL_SELECT
:
1734 return snd_timer_user_tselect(file
, argp
);
1735 case SNDRV_TIMER_IOCTL_INFO
:
1736 return snd_timer_user_info(file
, argp
);
1737 case SNDRV_TIMER_IOCTL_PARAMS
:
1738 return snd_timer_user_params(file
, argp
);
1739 case SNDRV_TIMER_IOCTL_STATUS
:
1740 return snd_timer_user_status(file
, argp
);
1741 case SNDRV_TIMER_IOCTL_START
:
1742 case SNDRV_TIMER_IOCTL_START_OLD
:
1743 return snd_timer_user_start(file
);
1744 case SNDRV_TIMER_IOCTL_STOP
:
1745 case SNDRV_TIMER_IOCTL_STOP_OLD
:
1746 return snd_timer_user_stop(file
);
1747 case SNDRV_TIMER_IOCTL_CONTINUE
:
1748 case SNDRV_TIMER_IOCTL_CONTINUE_OLD
:
1749 return snd_timer_user_continue(file
);
1750 case SNDRV_TIMER_IOCTL_PAUSE
:
1751 case SNDRV_TIMER_IOCTL_PAUSE_OLD
:
1752 return snd_timer_user_pause(file
);
1757 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
1759 snd_timer_user_t
*tu
;
1762 tu
= file
->private_data
;
1763 err
= fasync_helper(fd
, file
, on
, &tu
->fasync
);
1769 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*offset
)
1771 snd_timer_user_t
*tu
;
1772 long result
= 0, unit
;
1775 tu
= file
->private_data
;
1776 unit
= tu
->tread
? sizeof(snd_timer_tread_t
) : sizeof(snd_timer_read_t
);
1777 spin_lock_irq(&tu
->qlock
);
1778 while ((long)count
- result
>= unit
) {
1779 while (!tu
->qused
) {
1782 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1787 set_current_state(TASK_INTERRUPTIBLE
);
1788 init_waitqueue_entry(&wait
, current
);
1789 add_wait_queue(&tu
->qchange_sleep
, &wait
);
1791 spin_unlock_irq(&tu
->qlock
);
1793 spin_lock_irq(&tu
->qlock
);
1795 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
1797 if (signal_pending(current
)) {
1803 spin_unlock_irq(&tu
->qlock
);
1808 if (copy_to_user(buffer
, &tu
->tqueue
[tu
->qhead
++], sizeof(snd_timer_tread_t
))) {
1813 if (copy_to_user(buffer
, &tu
->queue
[tu
->qhead
++], sizeof(snd_timer_read_t
))) {
1819 tu
->qhead
%= tu
->queue_size
;
1824 spin_lock_irq(&tu
->qlock
);
1827 spin_unlock_irq(&tu
->qlock
);
1829 return result
> 0 ? result
: err
;
1832 static unsigned int snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
1835 snd_timer_user_t
*tu
;
1837 tu
= file
->private_data
;
1839 poll_wait(file
, &tu
->qchange_sleep
, wait
);
1843 mask
|= POLLIN
| POLLRDNORM
;
1848 #ifdef CONFIG_COMPAT
1849 #include "timer_compat.c"
1851 #define snd_timer_user_ioctl_compat NULL
1854 static struct file_operations snd_timer_f_ops
=
1856 .owner
= THIS_MODULE
,
1857 .read
= snd_timer_user_read
,
1858 .open
= snd_timer_user_open
,
1859 .release
= snd_timer_user_release
,
1860 .poll
= snd_timer_user_poll
,
1861 .unlocked_ioctl
= snd_timer_user_ioctl
,
1862 .compat_ioctl
= snd_timer_user_ioctl_compat
,
1863 .fasync
= snd_timer_user_fasync
,
1866 static snd_minor_t snd_timer_reg
=
1869 .f_ops
= &snd_timer_f_ops
,
1876 static snd_info_entry_t
*snd_timer_proc_entry
= NULL
;
1878 static int __init
alsa_timer_init(void)
1881 snd_info_entry_t
*entry
;
1883 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1884 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1, "system timer");
1886 if ((entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
)) != NULL
) {
1887 entry
->c
.text
.read_size
= SNDRV_TIMER_DEVICES
* 128;
1888 entry
->c
.text
.read
= snd_timer_proc_read
;
1889 if (snd_info_register(entry
) < 0) {
1890 snd_info_free_entry(entry
);
1894 snd_timer_proc_entry
= entry
;
1895 if ((err
= snd_timer_register_system()) < 0)
1896 snd_printk(KERN_ERR
"unable to register system timer (%i)\n", err
);
1897 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
,
1898 NULL
, 0, &snd_timer_reg
, "timer"))<0)
1899 snd_printk(KERN_ERR
"unable to register timer device (%i)\n", err
);
1903 static void __exit
alsa_timer_exit(void)
1905 struct list_head
*p
, *n
;
1907 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0);
1908 /* unregister the system timer */
1909 list_for_each_safe(p
, n
, &snd_timer_list
) {
1910 snd_timer_t
*timer
= (snd_timer_t
*)list_entry(p
, snd_timer_t
, device_list
);
1911 snd_timer_unregister(timer
);
1913 if (snd_timer_proc_entry
) {
1914 snd_info_unregister(snd_timer_proc_entry
);
1915 snd_timer_proc_entry
= NULL
;
1917 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1918 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
1922 module_init(alsa_timer_init
)
1923 module_exit(alsa_timer_exit
)
1925 EXPORT_SYMBOL(snd_timer_open
);
1926 EXPORT_SYMBOL(snd_timer_close
);
1927 EXPORT_SYMBOL(snd_timer_resolution
);
1928 EXPORT_SYMBOL(snd_timer_start
);
1929 EXPORT_SYMBOL(snd_timer_stop
);
1930 EXPORT_SYMBOL(snd_timer_continue
);
1931 EXPORT_SYMBOL(snd_timer_pause
);
1932 EXPORT_SYMBOL(snd_timer_new
);
1933 EXPORT_SYMBOL(snd_timer_notify
);
1934 EXPORT_SYMBOL(snd_timer_global_new
);
1935 EXPORT_SYMBOL(snd_timer_global_free
);
1936 EXPORT_SYMBOL(snd_timer_global_register
);
1937 EXPORT_SYMBOL(snd_timer_global_unregister
);
1938 EXPORT_SYMBOL(snd_timer_interrupt
);