2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/mutex.h>
27 #include <linux/moduleparam.h>
28 #include <linux/string.h>
29 #include <sound/core.h>
30 #include <sound/timer.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/minors.h>
34 #include <sound/initval.h>
35 #include <linux/kmod.h>
37 #if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
38 #define DEFAULT_TIMER_LIMIT 3
39 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
40 #define DEFAULT_TIMER_LIMIT 2
42 #define DEFAULT_TIMER_LIMIT 1
45 static int timer_limit
= DEFAULT_TIMER_LIMIT
;
46 static int timer_tstamp_monotonic
= 1;
47 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
48 MODULE_DESCRIPTION("ALSA timer interface");
49 MODULE_LICENSE("GPL");
50 module_param(timer_limit
, int, 0444);
51 MODULE_PARM_DESC(timer_limit
, "Maximum global timers in system.");
52 module_param(timer_tstamp_monotonic
, int, 0444);
53 MODULE_PARM_DESC(timer_tstamp_monotonic
, "Use posix monotonic clock source for timestamps (default).");
55 struct snd_timer_user
{
56 struct snd_timer_instance
*timeri
;
57 int tread
; /* enhanced read with timestamps and events */
59 unsigned long overrun
;
64 struct snd_timer_read
*queue
;
65 struct snd_timer_tread
*tqueue
;
67 unsigned long last_resolution
;
69 struct timespec tstamp
; /* trigger tstamp */
70 wait_queue_head_t qchange_sleep
;
71 struct fasync_struct
*fasync
;
72 struct mutex tread_sem
;
76 static LIST_HEAD(snd_timer_list
);
78 /* list of slave instances */
79 static LIST_HEAD(snd_timer_slave_list
);
81 /* lock for slave active lists */
82 static DEFINE_SPINLOCK(slave_active_lock
);
84 static DEFINE_MUTEX(register_mutex
);
86 static int snd_timer_free(struct snd_timer
*timer
);
87 static int snd_timer_dev_free(struct snd_device
*device
);
88 static int snd_timer_dev_register(struct snd_device
*device
);
89 static int snd_timer_dev_disconnect(struct snd_device
*device
);
91 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
);
94 * create a timer instance with the given owner string.
95 * when timer is not NULL, increments the module counter
97 static struct snd_timer_instance
*snd_timer_instance_new(char *owner
,
98 struct snd_timer
*timer
)
100 struct snd_timer_instance
*timeri
;
101 timeri
= kzalloc(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
&& !try_module_get(timer
->module
)) {
117 kfree(timeri
->owner
);
126 * find a timer instance from the given timer id
128 static struct snd_timer
*snd_timer_find(struct snd_timer_id
*tid
)
130 struct snd_timer
*timer
= NULL
;
132 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
133 if (timer
->tmr_class
!= tid
->dev_class
)
135 if ((timer
->tmr_class
== SNDRV_TIMER_CLASS_CARD
||
136 timer
->tmr_class
== SNDRV_TIMER_CLASS_PCM
) &&
137 (timer
->card
== NULL
||
138 timer
->card
->number
!= tid
->card
))
140 if (timer
->tmr_device
!= tid
->device
)
142 if (timer
->tmr_subdevice
!= tid
->subdevice
)
151 static void snd_timer_request(struct snd_timer_id
*tid
)
153 if (! current
->fs
->root
)
155 switch (tid
->dev_class
) {
156 case SNDRV_TIMER_CLASS_GLOBAL
:
157 if (tid
->device
< timer_limit
)
158 request_module("snd-timer-%i", tid
->device
);
160 case SNDRV_TIMER_CLASS_CARD
:
161 case SNDRV_TIMER_CLASS_PCM
:
162 if (tid
->card
< snd_ecards_limit
)
163 request_module("snd-card-%i", tid
->card
);
173 * look for a master instance matching with the slave id of the given slave.
174 * when found, relink the open_link of the slave.
176 * call this with register_mutex down.
178 static void snd_timer_check_slave(struct snd_timer_instance
*slave
)
180 struct snd_timer
*timer
;
181 struct snd_timer_instance
*master
;
183 /* FIXME: it's really dumb to look up all entries.. */
184 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
185 list_for_each_entry(master
, &timer
->open_list_head
, open_list
) {
186 if (slave
->slave_class
== master
->slave_class
&&
187 slave
->slave_id
== master
->slave_id
) {
188 list_del(&slave
->open_list
);
189 list_add_tail(&slave
->open_list
,
190 &master
->slave_list_head
);
191 spin_lock_irq(&slave_active_lock
);
192 slave
->master
= master
;
193 slave
->timer
= master
->timer
;
194 spin_unlock_irq(&slave_active_lock
);
202 * look for slave instances matching with the slave id of the given master.
203 * when found, relink the open_link of slaves.
205 * call this with register_mutex down.
207 static void snd_timer_check_master(struct snd_timer_instance
*master
)
209 struct snd_timer_instance
*slave
, *tmp
;
211 /* check all pending slaves */
212 list_for_each_entry_safe(slave
, tmp
, &snd_timer_slave_list
, open_list
) {
213 if (slave
->slave_class
== master
->slave_class
&&
214 slave
->slave_id
== master
->slave_id
) {
215 list_move_tail(&slave
->open_list
, &master
->slave_list_head
);
216 spin_lock_irq(&slave_active_lock
);
217 slave
->master
= master
;
218 slave
->timer
= master
->timer
;
219 if (slave
->flags
& SNDRV_TIMER_IFLG_RUNNING
)
220 list_add_tail(&slave
->active_list
,
221 &master
->slave_active_head
);
222 spin_unlock_irq(&slave_active_lock
);
228 * open a timer instance
229 * when opening a master, the slave id must be here given.
231 int snd_timer_open(struct snd_timer_instance
**ti
,
232 char *owner
, struct snd_timer_id
*tid
,
233 unsigned int slave_id
)
235 struct snd_timer
*timer
;
236 struct snd_timer_instance
*timeri
= NULL
;
238 if (tid
->dev_class
== SNDRV_TIMER_CLASS_SLAVE
) {
239 /* open a slave instance */
240 if (tid
->dev_sclass
<= SNDRV_TIMER_SCLASS_NONE
||
241 tid
->dev_sclass
> SNDRV_TIMER_SCLASS_OSS_SEQUENCER
) {
242 snd_printd("invalid slave class %i\n", tid
->dev_sclass
);
245 mutex_lock(®ister_mutex
);
246 timeri
= snd_timer_instance_new(owner
, NULL
);
248 mutex_unlock(®ister_mutex
);
251 timeri
->slave_class
= tid
->dev_sclass
;
252 timeri
->slave_id
= tid
->device
;
253 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
254 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
255 snd_timer_check_slave(timeri
);
256 mutex_unlock(®ister_mutex
);
261 /* open a master instance */
262 mutex_lock(®ister_mutex
);
263 timer
= snd_timer_find(tid
);
266 mutex_unlock(®ister_mutex
);
267 snd_timer_request(tid
);
268 mutex_lock(®ister_mutex
);
269 timer
= snd_timer_find(tid
);
273 mutex_unlock(®ister_mutex
);
276 if (!list_empty(&timer
->open_list_head
)) {
277 timeri
= list_entry(timer
->open_list_head
.next
,
278 struct snd_timer_instance
, open_list
);
279 if (timeri
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
280 mutex_unlock(®ister_mutex
);
284 timeri
= snd_timer_instance_new(owner
, timer
);
286 mutex_unlock(®ister_mutex
);
289 timeri
->slave_class
= tid
->dev_sclass
;
290 timeri
->slave_id
= slave_id
;
291 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
)
292 timer
->hw
.open(timer
);
293 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
294 snd_timer_check_master(timeri
);
295 mutex_unlock(®ister_mutex
);
300 static int _snd_timer_stop(struct snd_timer_instance
*timeri
,
301 int keep_flag
, int event
);
304 * close a timer instance
306 int snd_timer_close(struct snd_timer_instance
*timeri
)
308 struct snd_timer
*timer
= NULL
;
309 struct snd_timer_instance
*slave
, *tmp
;
311 snd_assert(timeri
!= NULL
, return -ENXIO
);
313 /* force to stop the timer */
314 snd_timer_stop(timeri
);
316 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
317 /* wait, until the active callback is finished */
318 spin_lock_irq(&slave_active_lock
);
319 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
320 spin_unlock_irq(&slave_active_lock
);
322 spin_lock_irq(&slave_active_lock
);
324 spin_unlock_irq(&slave_active_lock
);
325 mutex_lock(®ister_mutex
);
326 list_del(&timeri
->open_list
);
327 mutex_unlock(®ister_mutex
);
329 timer
= timeri
->timer
;
330 /* wait, until the active callback is finished */
331 spin_lock_irq(&timer
->lock
);
332 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
333 spin_unlock_irq(&timer
->lock
);
335 spin_lock_irq(&timer
->lock
);
337 spin_unlock_irq(&timer
->lock
);
338 mutex_lock(®ister_mutex
);
339 list_del(&timeri
->open_list
);
340 if (timer
&& list_empty(&timer
->open_list_head
) &&
342 timer
->hw
.close(timer
);
343 /* remove slave links */
344 list_for_each_entry_safe(slave
, tmp
, &timeri
->slave_list_head
,
346 spin_lock_irq(&slave_active_lock
);
347 _snd_timer_stop(slave
, 1, SNDRV_TIMER_EVENT_RESOLUTION
);
348 list_move_tail(&slave
->open_list
, &snd_timer_slave_list
);
349 slave
->master
= NULL
;
351 spin_unlock_irq(&slave_active_lock
);
353 mutex_unlock(®ister_mutex
);
355 if (timeri
->private_free
)
356 timeri
->private_free(timeri
);
357 kfree(timeri
->owner
);
360 module_put(timer
->module
);
364 unsigned long snd_timer_resolution(struct snd_timer_instance
*timeri
)
366 struct snd_timer
* timer
;
370 if ((timer
= timeri
->timer
) != NULL
) {
371 if (timer
->hw
.c_resolution
)
372 return timer
->hw
.c_resolution(timer
);
373 return timer
->hw
.resolution
;
378 static void snd_timer_notify1(struct snd_timer_instance
*ti
, int event
)
380 struct snd_timer
*timer
;
382 unsigned long resolution
= 0;
383 struct snd_timer_instance
*ts
;
384 struct timespec tstamp
;
386 if (timer_tstamp_monotonic
)
387 do_posix_clock_monotonic_gettime(&tstamp
);
389 getnstimeofday(&tstamp
);
390 snd_assert(event
>= SNDRV_TIMER_EVENT_START
&&
391 event
<= SNDRV_TIMER_EVENT_PAUSE
, return);
392 if (event
== SNDRV_TIMER_EVENT_START
||
393 event
== SNDRV_TIMER_EVENT_CONTINUE
)
394 resolution
= snd_timer_resolution(ti
);
396 ti
->ccallback(ti
, SNDRV_TIMER_EVENT_START
, &tstamp
, resolution
);
397 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
402 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
404 spin_lock_irqsave(&timer
->lock
, flags
);
405 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
407 ts
->ccallback(ti
, event
+ 100, &tstamp
, resolution
);
408 spin_unlock_irqrestore(&timer
->lock
, flags
);
411 static int snd_timer_start1(struct snd_timer
*timer
, struct snd_timer_instance
*timeri
,
412 unsigned long sticks
)
414 list_del(&timeri
->active_list
);
415 list_add_tail(&timeri
->active_list
, &timer
->active_list_head
);
416 if (timer
->running
) {
417 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
419 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
420 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
421 return 1; /* delayed start */
423 timer
->sticks
= sticks
;
424 timer
->hw
.start(timer
);
427 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
432 static int snd_timer_start_slave(struct snd_timer_instance
*timeri
)
436 spin_lock_irqsave(&slave_active_lock
, flags
);
437 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
439 list_add_tail(&timeri
->active_list
,
440 &timeri
->master
->slave_active_head
);
441 spin_unlock_irqrestore(&slave_active_lock
, flags
);
442 return 1; /* delayed start */
446 * start the timer instance
448 int snd_timer_start(struct snd_timer_instance
*timeri
, unsigned int ticks
)
450 struct snd_timer
*timer
;
451 int result
= -EINVAL
;
454 if (timeri
== NULL
|| ticks
< 1)
456 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
457 result
= snd_timer_start_slave(timeri
);
458 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
461 timer
= timeri
->timer
;
464 spin_lock_irqsave(&timer
->lock
, flags
);
465 timeri
->ticks
= timeri
->cticks
= ticks
;
467 result
= snd_timer_start1(timer
, timeri
, ticks
);
468 spin_unlock_irqrestore(&timer
->lock
, flags
);
469 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
473 static int _snd_timer_stop(struct snd_timer_instance
* timeri
,
474 int keep_flag
, int event
)
476 struct snd_timer
*timer
;
479 snd_assert(timeri
!= NULL
, return -ENXIO
);
481 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
483 spin_lock_irqsave(&slave_active_lock
, flags
);
484 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
485 spin_unlock_irqrestore(&slave_active_lock
, flags
);
489 timer
= timeri
->timer
;
492 spin_lock_irqsave(&timer
->lock
, flags
);
493 list_del_init(&timeri
->ack_list
);
494 list_del_init(&timeri
->active_list
);
495 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
496 !(--timer
->running
)) {
497 timer
->hw
.stop(timer
);
498 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
499 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
500 snd_timer_reschedule(timer
, 0);
501 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
502 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
503 timer
->hw
.start(timer
);
509 ~(SNDRV_TIMER_IFLG_RUNNING
| SNDRV_TIMER_IFLG_START
);
510 spin_unlock_irqrestore(&timer
->lock
, flags
);
512 if (event
!= SNDRV_TIMER_EVENT_RESOLUTION
)
513 snd_timer_notify1(timeri
, event
);
518 * stop the timer instance.
520 * do not call this from the timer callback!
522 int snd_timer_stop(struct snd_timer_instance
*timeri
)
524 struct snd_timer
*timer
;
528 err
= _snd_timer_stop(timeri
, 0, SNDRV_TIMER_EVENT_STOP
);
531 timer
= timeri
->timer
;
532 spin_lock_irqsave(&timer
->lock
, flags
);
533 timeri
->cticks
= timeri
->ticks
;
535 spin_unlock_irqrestore(&timer
->lock
, flags
);
540 * start again.. the tick is kept.
542 int snd_timer_continue(struct snd_timer_instance
*timeri
)
544 struct snd_timer
*timer
;
545 int result
= -EINVAL
;
550 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
551 return snd_timer_start_slave(timeri
);
552 timer
= timeri
->timer
;
555 spin_lock_irqsave(&timer
->lock
, flags
);
559 result
= snd_timer_start1(timer
, timeri
, timer
->sticks
);
560 spin_unlock_irqrestore(&timer
->lock
, flags
);
561 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_CONTINUE
);
566 * pause.. remember the ticks left
568 int snd_timer_pause(struct snd_timer_instance
* timeri
)
570 return _snd_timer_stop(timeri
, 0, SNDRV_TIMER_EVENT_PAUSE
);
574 * reschedule the timer
576 * start pending instances and check the scheduling ticks.
577 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
579 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
)
581 struct snd_timer_instance
*ti
;
582 unsigned long ticks
= ~0UL;
584 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
585 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
586 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
587 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
590 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
591 if (ticks
> ti
->cticks
)
596 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
599 if (ticks
> timer
->hw
.ticks
)
600 ticks
= timer
->hw
.ticks
;
601 if (ticks_left
!= ticks
)
602 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
603 timer
->sticks
= ticks
;
610 static void snd_timer_tasklet(unsigned long arg
)
612 struct snd_timer
*timer
= (struct snd_timer
*) arg
;
613 struct snd_timer_instance
*ti
;
615 unsigned long resolution
, ticks
;
618 spin_lock_irqsave(&timer
->lock
, flags
);
619 /* now process all callbacks */
620 while (!list_empty(&timer
->sack_list_head
)) {
621 p
= timer
->sack_list_head
.next
; /* get first item */
622 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
624 /* remove from ack_list and make empty */
629 resolution
= ti
->resolution
;
631 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
632 spin_unlock(&timer
->lock
);
634 ti
->callback(ti
, resolution
, ticks
);
635 spin_lock(&timer
->lock
);
636 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
638 spin_unlock_irqrestore(&timer
->lock
, flags
);
644 * ticks_left is usually equal to timer->sticks.
647 void snd_timer_interrupt(struct snd_timer
* timer
, unsigned long ticks_left
)
649 struct snd_timer_instance
*ti
, *ts
, *tmp
;
650 unsigned long resolution
, ticks
;
651 struct list_head
*p
, *ack_list_head
;
658 spin_lock_irqsave(&timer
->lock
, flags
);
660 /* remember the current resolution */
661 if (timer
->hw
.c_resolution
)
662 resolution
= timer
->hw
.c_resolution(timer
);
664 resolution
= timer
->hw
.resolution
;
666 /* loop for all active instances
667 * Here we cannot use list_for_each_entry because the active_list of a
668 * processed instance is relinked to done_list_head before the callback
671 list_for_each_entry_safe(ti
, tmp
, &timer
->active_list_head
,
673 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
675 ti
->pticks
+= ticks_left
;
676 ti
->resolution
= resolution
;
677 if (ti
->cticks
< ticks_left
)
680 ti
->cticks
-= ticks_left
;
681 if (ti
->cticks
) /* not expired */
683 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
684 ti
->cticks
= ti
->ticks
;
686 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
687 if (--timer
->running
)
688 list_del(&ti
->active_list
);
690 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
691 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
))
692 ack_list_head
= &timer
->ack_list_head
;
694 ack_list_head
= &timer
->sack_list_head
;
695 if (list_empty(&ti
->ack_list
))
696 list_add_tail(&ti
->ack_list
, ack_list_head
);
697 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
) {
698 ts
->pticks
= ti
->pticks
;
699 ts
->resolution
= resolution
;
700 if (list_empty(&ts
->ack_list
))
701 list_add_tail(&ts
->ack_list
, ack_list_head
);
704 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
705 snd_timer_reschedule(timer
, timer
->sticks
);
706 if (timer
->running
) {
707 if (timer
->hw
.flags
& SNDRV_TIMER_HW_STOP
) {
708 timer
->hw
.stop(timer
);
709 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
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
= list_entry(p
, struct snd_timer_instance
, 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_irqrestore(&timer
->lock
, flags
);
745 tasklet_hi_schedule(&timer
->task_queue
);
752 int snd_timer_new(struct snd_card
*card
, char *id
, struct snd_timer_id
*tid
,
753 struct snd_timer
**rtimer
)
755 struct snd_timer
*timer
;
757 static struct snd_device_ops ops
= {
758 .dev_free
= snd_timer_dev_free
,
759 .dev_register
= snd_timer_dev_register
,
760 .dev_disconnect
= snd_timer_dev_disconnect
,
763 snd_assert(tid
!= NULL
, return -EINVAL
);
764 snd_assert(rtimer
!= NULL
, return -EINVAL
);
766 timer
= kzalloc(sizeof(*timer
), GFP_KERNEL
);
768 snd_printk(KERN_ERR
"timer: cannot allocate\n");
771 timer
->tmr_class
= tid
->dev_class
;
773 timer
->tmr_device
= tid
->device
;
774 timer
->tmr_subdevice
= tid
->subdevice
;
776 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
777 INIT_LIST_HEAD(&timer
->device_list
);
778 INIT_LIST_HEAD(&timer
->open_list_head
);
779 INIT_LIST_HEAD(&timer
->active_list_head
);
780 INIT_LIST_HEAD(&timer
->ack_list_head
);
781 INIT_LIST_HEAD(&timer
->sack_list_head
);
782 spin_lock_init(&timer
->lock
);
783 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
,
784 (unsigned long)timer
);
786 timer
->module
= card
->module
;
787 err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
);
789 snd_timer_free(timer
);
797 static int snd_timer_free(struct snd_timer
*timer
)
799 snd_assert(timer
!= NULL
, return -ENXIO
);
801 mutex_lock(®ister_mutex
);
802 if (! list_empty(&timer
->open_list_head
)) {
803 struct list_head
*p
, *n
;
804 struct snd_timer_instance
*ti
;
805 snd_printk(KERN_WARNING
"timer %p is busy?\n", timer
);
806 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
808 ti
= list_entry(p
, struct snd_timer_instance
, open_list
);
812 list_del(&timer
->device_list
);
813 mutex_unlock(®ister_mutex
);
815 if (timer
->private_free
)
816 timer
->private_free(timer
);
821 static int snd_timer_dev_free(struct snd_device
*device
)
823 struct snd_timer
*timer
= device
->device_data
;
824 return snd_timer_free(timer
);
827 static int snd_timer_dev_register(struct snd_device
*dev
)
829 struct snd_timer
*timer
= dev
->device_data
;
830 struct snd_timer
*timer1
;
832 snd_assert(timer
!= NULL
&& timer
->hw
.start
!= NULL
&&
833 timer
->hw
.stop
!= NULL
, return -ENXIO
);
834 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
835 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
838 mutex_lock(®ister_mutex
);
839 list_for_each_entry(timer1
, &snd_timer_list
, device_list
) {
840 if (timer1
->tmr_class
> timer
->tmr_class
)
842 if (timer1
->tmr_class
< timer
->tmr_class
)
844 if (timer1
->card
&& timer
->card
) {
845 if (timer1
->card
->number
> timer
->card
->number
)
847 if (timer1
->card
->number
< timer
->card
->number
)
850 if (timer1
->tmr_device
> timer
->tmr_device
)
852 if (timer1
->tmr_device
< timer
->tmr_device
)
854 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
856 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
859 mutex_unlock(®ister_mutex
);
862 list_add_tail(&timer
->device_list
, &timer1
->device_list
);
863 mutex_unlock(®ister_mutex
);
867 static int snd_timer_dev_disconnect(struct snd_device
*device
)
869 struct snd_timer
*timer
= device
->device_data
;
870 mutex_lock(®ister_mutex
);
871 list_del_init(&timer
->device_list
);
872 mutex_unlock(®ister_mutex
);
876 void snd_timer_notify(struct snd_timer
*timer
, int event
, struct timespec
*tstamp
)
879 unsigned long resolution
= 0;
880 struct snd_timer_instance
*ti
, *ts
;
882 if (! (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
))
884 snd_assert(event
>= SNDRV_TIMER_EVENT_MSTART
&&
885 event
<= SNDRV_TIMER_EVENT_MRESUME
, return);
886 spin_lock_irqsave(&timer
->lock
, flags
);
887 if (event
== SNDRV_TIMER_EVENT_MSTART
||
888 event
== SNDRV_TIMER_EVENT_MCONTINUE
||
889 event
== SNDRV_TIMER_EVENT_MRESUME
) {
890 if (timer
->hw
.c_resolution
)
891 resolution
= timer
->hw
.c_resolution(timer
);
893 resolution
= timer
->hw
.resolution
;
895 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
897 ti
->ccallback(ti
, event
, tstamp
, resolution
);
898 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
900 ts
->ccallback(ts
, event
, tstamp
, resolution
);
902 spin_unlock_irqrestore(&timer
->lock
, flags
);
906 * exported functions for global timers
908 int snd_timer_global_new(char *id
, int device
, struct snd_timer
**rtimer
)
910 struct snd_timer_id tid
;
912 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
913 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
917 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
920 int snd_timer_global_free(struct snd_timer
*timer
)
922 return snd_timer_free(timer
);
925 int snd_timer_global_register(struct snd_timer
*timer
)
927 struct snd_device dev
;
929 memset(&dev
, 0, sizeof(dev
));
930 dev
.device_data
= timer
;
931 return snd_timer_dev_register(&dev
);
938 struct snd_timer_system_private
{
939 struct timer_list tlist
;
940 unsigned long last_expires
;
941 unsigned long last_jiffies
;
942 unsigned long correction
;
945 static void snd_timer_s_function(unsigned long data
)
947 struct snd_timer
*timer
= (struct snd_timer
*)data
;
948 struct snd_timer_system_private
*priv
= timer
->private_data
;
949 unsigned long jiff
= jiffies
;
950 if (time_after(jiff
, priv
->last_expires
))
951 priv
->correction
+= (long)jiff
- (long)priv
->last_expires
;
952 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
955 static int snd_timer_s_start(struct snd_timer
* timer
)
957 struct snd_timer_system_private
*priv
;
960 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
961 njiff
= (priv
->last_jiffies
= jiffies
);
962 if (priv
->correction
> timer
->sticks
- 1) {
963 priv
->correction
-= timer
->sticks
- 1;
966 njiff
+= timer
->sticks
- priv
->correction
;
967 priv
->correction
= 0;
969 priv
->last_expires
= priv
->tlist
.expires
= njiff
;
970 add_timer(&priv
->tlist
);
974 static int snd_timer_s_stop(struct snd_timer
* timer
)
976 struct snd_timer_system_private
*priv
;
979 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
980 del_timer(&priv
->tlist
);
982 if (time_before(jiff
, priv
->last_expires
))
983 timer
->sticks
= priv
->last_expires
- jiff
;
986 priv
->correction
= 0;
990 static struct snd_timer_hardware snd_timer_system
=
992 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
993 .resolution
= 1000000000L / HZ
,
995 .start
= snd_timer_s_start
,
996 .stop
= snd_timer_s_stop
999 static void snd_timer_free_system(struct snd_timer
*timer
)
1001 kfree(timer
->private_data
);
1004 static int snd_timer_register_system(void)
1006 struct snd_timer
*timer
;
1007 struct snd_timer_system_private
*priv
;
1010 err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
);
1013 strcpy(timer
->name
, "system timer");
1014 timer
->hw
= snd_timer_system
;
1015 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1017 snd_timer_free(timer
);
1020 init_timer(&priv
->tlist
);
1021 priv
->tlist
.function
= snd_timer_s_function
;
1022 priv
->tlist
.data
= (unsigned long) timer
;
1023 timer
->private_data
= priv
;
1024 timer
->private_free
= snd_timer_free_system
;
1025 return snd_timer_global_register(timer
);
1028 #ifdef CONFIG_PROC_FS
1033 static void snd_timer_proc_read(struct snd_info_entry
*entry
,
1034 struct snd_info_buffer
*buffer
)
1036 struct snd_timer
*timer
;
1037 struct snd_timer_instance
*ti
;
1039 mutex_lock(®ister_mutex
);
1040 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
1041 switch (timer
->tmr_class
) {
1042 case SNDRV_TIMER_CLASS_GLOBAL
:
1043 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1045 case SNDRV_TIMER_CLASS_CARD
:
1046 snd_iprintf(buffer
, "C%i-%i: ",
1047 timer
->card
->number
, timer
->tmr_device
);
1049 case SNDRV_TIMER_CLASS_PCM
:
1050 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
,
1051 timer
->tmr_device
, timer
->tmr_subdevice
);
1054 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
,
1055 timer
->card
? timer
->card
->number
: -1,
1056 timer
->tmr_device
, timer
->tmr_subdevice
);
1058 snd_iprintf(buffer
, "%s :", timer
->name
);
1059 if (timer
->hw
.resolution
)
1060 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)",
1061 timer
->hw
.resolution
/ 1000,
1062 timer
->hw
.resolution
% 1000,
1064 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1065 snd_iprintf(buffer
, " SLAVE");
1066 snd_iprintf(buffer
, "\n");
1067 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
)
1068 snd_iprintf(buffer
, " Client %s : %s\n",
1069 ti
->owner
? ti
->owner
: "unknown",
1070 ti
->flags
& (SNDRV_TIMER_IFLG_START
|
1071 SNDRV_TIMER_IFLG_RUNNING
)
1072 ? "running" : "stopped");
1074 mutex_unlock(®ister_mutex
);
1077 static struct snd_info_entry
*snd_timer_proc_entry
;
1079 static void __init
snd_timer_proc_init(void)
1081 struct snd_info_entry
*entry
;
1083 entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
);
1084 if (entry
!= NULL
) {
1085 entry
->c
.text
.read
= snd_timer_proc_read
;
1086 if (snd_info_register(entry
) < 0) {
1087 snd_info_free_entry(entry
);
1091 snd_timer_proc_entry
= entry
;
1094 static void __exit
snd_timer_proc_done(void)
1096 snd_info_free_entry(snd_timer_proc_entry
);
1098 #else /* !CONFIG_PROC_FS */
1099 #define snd_timer_proc_init()
1100 #define snd_timer_proc_done()
1104 * USER SPACE interface
1107 static void snd_timer_user_interrupt(struct snd_timer_instance
*timeri
,
1108 unsigned long resolution
,
1109 unsigned long ticks
)
1111 struct snd_timer_user
*tu
= timeri
->callback_data
;
1112 struct snd_timer_read
*r
;
1115 spin_lock(&tu
->qlock
);
1116 if (tu
->qused
> 0) {
1117 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1118 r
= &tu
->queue
[prev
];
1119 if (r
->resolution
== resolution
) {
1124 if (tu
->qused
>= tu
->queue_size
) {
1127 r
= &tu
->queue
[tu
->qtail
++];
1128 tu
->qtail
%= tu
->queue_size
;
1129 r
->resolution
= resolution
;
1134 spin_unlock(&tu
->qlock
);
1135 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1136 wake_up(&tu
->qchange_sleep
);
1139 static void snd_timer_user_append_to_tqueue(struct snd_timer_user
*tu
,
1140 struct snd_timer_tread
*tread
)
1142 if (tu
->qused
>= tu
->queue_size
) {
1145 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1146 tu
->qtail
%= tu
->queue_size
;
1151 static void snd_timer_user_ccallback(struct snd_timer_instance
*timeri
,
1153 struct timespec
*tstamp
,
1154 unsigned long resolution
)
1156 struct snd_timer_user
*tu
= timeri
->callback_data
;
1157 struct snd_timer_tread r1
;
1159 if (event
>= SNDRV_TIMER_EVENT_START
&&
1160 event
<= SNDRV_TIMER_EVENT_PAUSE
)
1161 tu
->tstamp
= *tstamp
;
1162 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1165 r1
.tstamp
= *tstamp
;
1166 r1
.val
= resolution
;
1167 spin_lock(&tu
->qlock
);
1168 snd_timer_user_append_to_tqueue(tu
, &r1
);
1169 spin_unlock(&tu
->qlock
);
1170 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1171 wake_up(&tu
->qchange_sleep
);
1174 static void snd_timer_user_tinterrupt(struct snd_timer_instance
*timeri
,
1175 unsigned long resolution
,
1176 unsigned long ticks
)
1178 struct snd_timer_user
*tu
= timeri
->callback_data
;
1179 struct snd_timer_tread
*r
, r1
;
1180 struct timespec tstamp
;
1181 int prev
, append
= 0;
1183 memset(&tstamp
, 0, sizeof(tstamp
));
1184 spin_lock(&tu
->qlock
);
1185 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
) |
1186 (1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1187 spin_unlock(&tu
->qlock
);
1190 if (tu
->last_resolution
!= resolution
|| ticks
> 0) {
1191 if (timer_tstamp_monotonic
)
1192 do_posix_clock_monotonic_gettime(&tstamp
);
1194 getnstimeofday(&tstamp
);
1196 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) &&
1197 tu
->last_resolution
!= resolution
) {
1198 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1200 r1
.val
= resolution
;
1201 snd_timer_user_append_to_tqueue(tu
, &r1
);
1202 tu
->last_resolution
= resolution
;
1205 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1209 if (tu
->qused
> 0) {
1210 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1211 r
= &tu
->tqueue
[prev
];
1212 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1219 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1222 snd_timer_user_append_to_tqueue(tu
, &r1
);
1225 spin_unlock(&tu
->qlock
);
1228 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1229 wake_up(&tu
->qchange_sleep
);
1232 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1234 struct snd_timer_user
*tu
;
1236 tu
= kzalloc(sizeof(*tu
), GFP_KERNEL
);
1239 spin_lock_init(&tu
->qlock
);
1240 init_waitqueue_head(&tu
->qchange_sleep
);
1241 mutex_init(&tu
->tread_sem
);
1243 tu
->queue_size
= 128;
1244 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1246 if (tu
->queue
== NULL
) {
1250 file
->private_data
= tu
;
1254 static int snd_timer_user_release(struct inode
*inode
, struct file
*file
)
1256 struct snd_timer_user
*tu
;
1258 if (file
->private_data
) {
1259 tu
= file
->private_data
;
1260 file
->private_data
= NULL
;
1261 fasync_helper(-1, file
, 0, &tu
->fasync
);
1263 snd_timer_close(tu
->timeri
);
1271 static void snd_timer_user_zero_id(struct snd_timer_id
*id
)
1273 id
->dev_class
= SNDRV_TIMER_CLASS_NONE
;
1274 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1280 static void snd_timer_user_copy_id(struct snd_timer_id
*id
, struct snd_timer
*timer
)
1282 id
->dev_class
= timer
->tmr_class
;
1283 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1284 id
->card
= timer
->card
? timer
->card
->number
: -1;
1285 id
->device
= timer
->tmr_device
;
1286 id
->subdevice
= timer
->tmr_subdevice
;
1289 static int snd_timer_user_next_device(struct snd_timer_id __user
*_tid
)
1291 struct snd_timer_id id
;
1292 struct snd_timer
*timer
;
1293 struct list_head
*p
;
1295 if (copy_from_user(&id
, _tid
, sizeof(id
)))
1297 mutex_lock(®ister_mutex
);
1298 if (id
.dev_class
< 0) { /* first item */
1299 if (list_empty(&snd_timer_list
))
1300 snd_timer_user_zero_id(&id
);
1302 timer
= list_entry(snd_timer_list
.next
,
1303 struct snd_timer
, device_list
);
1304 snd_timer_user_copy_id(&id
, timer
);
1307 switch (id
.dev_class
) {
1308 case SNDRV_TIMER_CLASS_GLOBAL
:
1309 id
.device
= id
.device
< 0 ? 0 : id
.device
+ 1;
1310 list_for_each(p
, &snd_timer_list
) {
1311 timer
= list_entry(p
, struct snd_timer
, device_list
);
1312 if (timer
->tmr_class
> SNDRV_TIMER_CLASS_GLOBAL
) {
1313 snd_timer_user_copy_id(&id
, timer
);
1316 if (timer
->tmr_device
>= id
.device
) {
1317 snd_timer_user_copy_id(&id
, timer
);
1321 if (p
== &snd_timer_list
)
1322 snd_timer_user_zero_id(&id
);
1324 case SNDRV_TIMER_CLASS_CARD
:
1325 case SNDRV_TIMER_CLASS_PCM
:
1332 if (id
.device
< 0) {
1335 if (id
.subdevice
< 0) {
1343 list_for_each(p
, &snd_timer_list
) {
1344 timer
= list_entry(p
, struct snd_timer
, device_list
);
1345 if (timer
->tmr_class
> id
.dev_class
) {
1346 snd_timer_user_copy_id(&id
, timer
);
1349 if (timer
->tmr_class
< id
.dev_class
)
1351 if (timer
->card
->number
> id
.card
) {
1352 snd_timer_user_copy_id(&id
, timer
);
1355 if (timer
->card
->number
< id
.card
)
1357 if (timer
->tmr_device
> id
.device
) {
1358 snd_timer_user_copy_id(&id
, timer
);
1361 if (timer
->tmr_device
< id
.device
)
1363 if (timer
->tmr_subdevice
> id
.subdevice
) {
1364 snd_timer_user_copy_id(&id
, timer
);
1367 if (timer
->tmr_subdevice
< id
.subdevice
)
1369 snd_timer_user_copy_id(&id
, timer
);
1372 if (p
== &snd_timer_list
)
1373 snd_timer_user_zero_id(&id
);
1376 snd_timer_user_zero_id(&id
);
1379 mutex_unlock(®ister_mutex
);
1380 if (copy_to_user(_tid
, &id
, sizeof(*_tid
)))
1385 static int snd_timer_user_ginfo(struct file
*file
,
1386 struct snd_timer_ginfo __user
*_ginfo
)
1388 struct snd_timer_ginfo
*ginfo
;
1389 struct snd_timer_id tid
;
1390 struct snd_timer
*t
;
1391 struct list_head
*p
;
1394 ginfo
= kmalloc(sizeof(*ginfo
), GFP_KERNEL
);
1397 if (copy_from_user(ginfo
, _ginfo
, sizeof(*ginfo
))) {
1402 memset(ginfo
, 0, sizeof(*ginfo
));
1404 mutex_lock(®ister_mutex
);
1405 t
= snd_timer_find(&tid
);
1407 ginfo
->card
= t
->card
? t
->card
->number
: -1;
1408 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1409 ginfo
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1410 strlcpy(ginfo
->id
, t
->id
, sizeof(ginfo
->id
));
1411 strlcpy(ginfo
->name
, t
->name
, sizeof(ginfo
->name
));
1412 ginfo
->resolution
= t
->hw
.resolution
;
1413 if (t
->hw
.resolution_min
> 0) {
1414 ginfo
->resolution_min
= t
->hw
.resolution_min
;
1415 ginfo
->resolution_max
= t
->hw
.resolution_max
;
1417 list_for_each(p
, &t
->open_list_head
) {
1423 mutex_unlock(®ister_mutex
);
1424 if (err
>= 0 && copy_to_user(_ginfo
, ginfo
, sizeof(*ginfo
)))
1430 static int snd_timer_user_gparams(struct file
*file
,
1431 struct snd_timer_gparams __user
*_gparams
)
1433 struct snd_timer_gparams gparams
;
1434 struct snd_timer
*t
;
1437 if (copy_from_user(&gparams
, _gparams
, sizeof(gparams
)))
1439 mutex_lock(®ister_mutex
);
1440 t
= snd_timer_find(&gparams
.tid
);
1445 if (!list_empty(&t
->open_list_head
)) {
1449 if (!t
->hw
.set_period
) {
1453 err
= t
->hw
.set_period(t
, gparams
.period_num
, gparams
.period_den
);
1455 mutex_unlock(®ister_mutex
);
1459 static int snd_timer_user_gstatus(struct file
*file
,
1460 struct snd_timer_gstatus __user
*_gstatus
)
1462 struct snd_timer_gstatus gstatus
;
1463 struct snd_timer_id tid
;
1464 struct snd_timer
*t
;
1467 if (copy_from_user(&gstatus
, _gstatus
, sizeof(gstatus
)))
1470 memset(&gstatus
, 0, sizeof(gstatus
));
1472 mutex_lock(®ister_mutex
);
1473 t
= snd_timer_find(&tid
);
1475 if (t
->hw
.c_resolution
)
1476 gstatus
.resolution
= t
->hw
.c_resolution(t
);
1478 gstatus
.resolution
= t
->hw
.resolution
;
1479 if (t
->hw
.precise_resolution
) {
1480 t
->hw
.precise_resolution(t
, &gstatus
.resolution_num
,
1481 &gstatus
.resolution_den
);
1483 gstatus
.resolution_num
= gstatus
.resolution
;
1484 gstatus
.resolution_den
= 1000000000uL;
1489 mutex_unlock(®ister_mutex
);
1490 if (err
>= 0 && copy_to_user(_gstatus
, &gstatus
, sizeof(gstatus
)))
1495 static int snd_timer_user_tselect(struct file
*file
,
1496 struct snd_timer_select __user
*_tselect
)
1498 struct snd_timer_user
*tu
;
1499 struct snd_timer_select tselect
;
1503 tu
= file
->private_data
;
1504 mutex_lock(&tu
->tread_sem
);
1506 snd_timer_close(tu
->timeri
);
1509 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
))) {
1513 sprintf(str
, "application %i", current
->pid
);
1514 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1515 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1516 err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
);
1525 tu
->tqueue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_tread
),
1527 if (tu
->tqueue
== NULL
)
1530 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1532 if (tu
->queue
== NULL
)
1537 snd_timer_close(tu
->timeri
);
1540 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1541 tu
->timeri
->callback
= tu
->tread
1542 ? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1543 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1544 tu
->timeri
->callback_data
= (void *)tu
;
1548 mutex_unlock(&tu
->tread_sem
);
1552 static int snd_timer_user_info(struct file
*file
,
1553 struct snd_timer_info __user
*_info
)
1555 struct snd_timer_user
*tu
;
1556 struct snd_timer_info
*info
;
1557 struct snd_timer
*t
;
1560 tu
= file
->private_data
;
1563 t
= tu
->timeri
->timer
;
1567 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1570 info
->card
= t
->card
? t
->card
->number
: -1;
1571 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1572 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1573 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1574 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1575 info
->resolution
= t
->hw
.resolution
;
1576 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1582 static int snd_timer_user_params(struct file
*file
,
1583 struct snd_timer_params __user
*_params
)
1585 struct snd_timer_user
*tu
;
1586 struct snd_timer_params params
;
1587 struct snd_timer
*t
;
1588 struct snd_timer_read
*tr
;
1589 struct snd_timer_tread
*ttr
;
1592 tu
= file
->private_data
;
1595 t
= tu
->timeri
->timer
;
1598 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
1600 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) && params
.ticks
< 1) {
1604 if (params
.queue_size
> 0 &&
1605 (params
.queue_size
< 32 || params
.queue_size
> 1024)) {
1609 if (params
.filter
& ~((1<<SNDRV_TIMER_EVENT_RESOLUTION
)|
1610 (1<<SNDRV_TIMER_EVENT_TICK
)|
1611 (1<<SNDRV_TIMER_EVENT_START
)|
1612 (1<<SNDRV_TIMER_EVENT_STOP
)|
1613 (1<<SNDRV_TIMER_EVENT_CONTINUE
)|
1614 (1<<SNDRV_TIMER_EVENT_PAUSE
)|
1615 (1<<SNDRV_TIMER_EVENT_SUSPEND
)|
1616 (1<<SNDRV_TIMER_EVENT_RESUME
)|
1617 (1<<SNDRV_TIMER_EVENT_MSTART
)|
1618 (1<<SNDRV_TIMER_EVENT_MSTOP
)|
1619 (1<<SNDRV_TIMER_EVENT_MCONTINUE
)|
1620 (1<<SNDRV_TIMER_EVENT_MPAUSE
)|
1621 (1<<SNDRV_TIMER_EVENT_MSUSPEND
)|
1622 (1<<SNDRV_TIMER_EVENT_MRESUME
))) {
1626 snd_timer_stop(tu
->timeri
);
1627 spin_lock_irq(&t
->lock
);
1628 tu
->timeri
->flags
&= ~(SNDRV_TIMER_IFLG_AUTO
|
1629 SNDRV_TIMER_IFLG_EXCLUSIVE
|
1630 SNDRV_TIMER_IFLG_EARLY_EVENT
);
1631 if (params
.flags
& SNDRV_TIMER_PSFLG_AUTO
)
1632 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
1633 if (params
.flags
& SNDRV_TIMER_PSFLG_EXCLUSIVE
)
1634 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EXCLUSIVE
;
1635 if (params
.flags
& SNDRV_TIMER_PSFLG_EARLY_EVENT
)
1636 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EARLY_EVENT
;
1637 spin_unlock_irq(&t
->lock
);
1638 if (params
.queue_size
> 0 &&
1639 (unsigned int)tu
->queue_size
!= params
.queue_size
) {
1641 ttr
= kmalloc(params
.queue_size
* sizeof(*ttr
),
1645 tu
->queue_size
= params
.queue_size
;
1649 tr
= kmalloc(params
.queue_size
* sizeof(*tr
),
1653 tu
->queue_size
= params
.queue_size
;
1658 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1659 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1661 struct snd_timer_tread tread
;
1662 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1663 tread
.tstamp
.tv_sec
= 0;
1664 tread
.tstamp
.tv_nsec
= 0;
1666 snd_timer_user_append_to_tqueue(tu
, &tread
);
1668 struct snd_timer_read
*r
= &tu
->queue
[0];
1675 tu
->filter
= params
.filter
;
1676 tu
->ticks
= params
.ticks
;
1679 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1684 static int snd_timer_user_status(struct file
*file
,
1685 struct snd_timer_status __user
*_status
)
1687 struct snd_timer_user
*tu
;
1688 struct snd_timer_status status
;
1690 tu
= file
->private_data
;
1693 memset(&status
, 0, sizeof(status
));
1694 status
.tstamp
= tu
->tstamp
;
1695 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1696 status
.lost
= tu
->timeri
->lost
;
1697 status
.overrun
= tu
->overrun
;
1698 spin_lock_irq(&tu
->qlock
);
1699 status
.queue
= tu
->qused
;
1700 spin_unlock_irq(&tu
->qlock
);
1701 if (copy_to_user(_status
, &status
, sizeof(status
)))
1706 static int snd_timer_user_start(struct file
*file
)
1709 struct snd_timer_user
*tu
;
1711 tu
= file
->private_data
;
1714 snd_timer_stop(tu
->timeri
);
1715 tu
->timeri
->lost
= 0;
1716 tu
->last_resolution
= 0;
1717 return (err
= snd_timer_start(tu
->timeri
, tu
->ticks
)) < 0 ? err
: 0;
1720 static int snd_timer_user_stop(struct file
*file
)
1723 struct snd_timer_user
*tu
;
1725 tu
= file
->private_data
;
1728 return (err
= snd_timer_stop(tu
->timeri
)) < 0 ? err
: 0;
1731 static int snd_timer_user_continue(struct file
*file
)
1734 struct snd_timer_user
*tu
;
1736 tu
= file
->private_data
;
1739 tu
->timeri
->lost
= 0;
1740 return (err
= snd_timer_continue(tu
->timeri
)) < 0 ? err
: 0;
1743 static int snd_timer_user_pause(struct file
*file
)
1746 struct snd_timer_user
*tu
;
1748 tu
= file
->private_data
;
1751 return (err
= snd_timer_pause(tu
->timeri
)) < 0 ? err
: 0;
1755 SNDRV_TIMER_IOCTL_START_OLD
= _IO('T', 0x20),
1756 SNDRV_TIMER_IOCTL_STOP_OLD
= _IO('T', 0x21),
1757 SNDRV_TIMER_IOCTL_CONTINUE_OLD
= _IO('T', 0x22),
1758 SNDRV_TIMER_IOCTL_PAUSE_OLD
= _IO('T', 0x23),
1761 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1764 struct snd_timer_user
*tu
;
1765 void __user
*argp
= (void __user
*)arg
;
1766 int __user
*p
= argp
;
1768 tu
= file
->private_data
;
1770 case SNDRV_TIMER_IOCTL_PVERSION
:
1771 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1772 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1773 return snd_timer_user_next_device(argp
);
1774 case SNDRV_TIMER_IOCTL_TREAD
:
1778 mutex_lock(&tu
->tread_sem
);
1779 if (tu
->timeri
) { /* too late */
1780 mutex_unlock(&tu
->tread_sem
);
1783 if (get_user(xarg
, p
)) {
1784 mutex_unlock(&tu
->tread_sem
);
1787 tu
->tread
= xarg
? 1 : 0;
1788 mutex_unlock(&tu
->tread_sem
);
1791 case SNDRV_TIMER_IOCTL_GINFO
:
1792 return snd_timer_user_ginfo(file
, argp
);
1793 case SNDRV_TIMER_IOCTL_GPARAMS
:
1794 return snd_timer_user_gparams(file
, argp
);
1795 case SNDRV_TIMER_IOCTL_GSTATUS
:
1796 return snd_timer_user_gstatus(file
, argp
);
1797 case SNDRV_TIMER_IOCTL_SELECT
:
1798 return snd_timer_user_tselect(file
, argp
);
1799 case SNDRV_TIMER_IOCTL_INFO
:
1800 return snd_timer_user_info(file
, argp
);
1801 case SNDRV_TIMER_IOCTL_PARAMS
:
1802 return snd_timer_user_params(file
, argp
);
1803 case SNDRV_TIMER_IOCTL_STATUS
:
1804 return snd_timer_user_status(file
, argp
);
1805 case SNDRV_TIMER_IOCTL_START
:
1806 case SNDRV_TIMER_IOCTL_START_OLD
:
1807 return snd_timer_user_start(file
);
1808 case SNDRV_TIMER_IOCTL_STOP
:
1809 case SNDRV_TIMER_IOCTL_STOP_OLD
:
1810 return snd_timer_user_stop(file
);
1811 case SNDRV_TIMER_IOCTL_CONTINUE
:
1812 case SNDRV_TIMER_IOCTL_CONTINUE_OLD
:
1813 return snd_timer_user_continue(file
);
1814 case SNDRV_TIMER_IOCTL_PAUSE
:
1815 case SNDRV_TIMER_IOCTL_PAUSE_OLD
:
1816 return snd_timer_user_pause(file
);
1821 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
1823 struct snd_timer_user
*tu
;
1826 tu
= file
->private_data
;
1827 err
= fasync_helper(fd
, file
, on
, &tu
->fasync
);
1833 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
,
1834 size_t count
, loff_t
*offset
)
1836 struct snd_timer_user
*tu
;
1837 long result
= 0, unit
;
1840 tu
= file
->private_data
;
1841 unit
= tu
->tread
? sizeof(struct snd_timer_tread
) : sizeof(struct snd_timer_read
);
1842 spin_lock_irq(&tu
->qlock
);
1843 while ((long)count
- result
>= unit
) {
1844 while (!tu
->qused
) {
1847 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1852 set_current_state(TASK_INTERRUPTIBLE
);
1853 init_waitqueue_entry(&wait
, current
);
1854 add_wait_queue(&tu
->qchange_sleep
, &wait
);
1856 spin_unlock_irq(&tu
->qlock
);
1858 spin_lock_irq(&tu
->qlock
);
1860 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
1862 if (signal_pending(current
)) {
1868 spin_unlock_irq(&tu
->qlock
);
1873 if (copy_to_user(buffer
, &tu
->tqueue
[tu
->qhead
++],
1874 sizeof(struct snd_timer_tread
))) {
1879 if (copy_to_user(buffer
, &tu
->queue
[tu
->qhead
++],
1880 sizeof(struct snd_timer_read
))) {
1886 tu
->qhead
%= tu
->queue_size
;
1891 spin_lock_irq(&tu
->qlock
);
1894 spin_unlock_irq(&tu
->qlock
);
1896 return result
> 0 ? result
: err
;
1899 static unsigned int snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
1902 struct snd_timer_user
*tu
;
1904 tu
= file
->private_data
;
1906 poll_wait(file
, &tu
->qchange_sleep
, wait
);
1910 mask
|= POLLIN
| POLLRDNORM
;
1915 #ifdef CONFIG_COMPAT
1916 #include "timer_compat.c"
1918 #define snd_timer_user_ioctl_compat NULL
1921 static const struct file_operations snd_timer_f_ops
=
1923 .owner
= THIS_MODULE
,
1924 .read
= snd_timer_user_read
,
1925 .open
= snd_timer_user_open
,
1926 .release
= snd_timer_user_release
,
1927 .poll
= snd_timer_user_poll
,
1928 .unlocked_ioctl
= snd_timer_user_ioctl
,
1929 .compat_ioctl
= snd_timer_user_ioctl_compat
,
1930 .fasync
= snd_timer_user_fasync
,
1937 static int __init
alsa_timer_init(void)
1941 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1942 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1,
1946 if ((err
= snd_timer_register_system()) < 0)
1947 snd_printk(KERN_ERR
"unable to register system timer (%i)\n",
1949 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0,
1950 &snd_timer_f_ops
, NULL
, "timer")) < 0)
1951 snd_printk(KERN_ERR
"unable to register timer device (%i)\n",
1953 snd_timer_proc_init();
1957 static void __exit
alsa_timer_exit(void)
1959 struct list_head
*p
, *n
;
1961 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0);
1962 /* unregister the system timer */
1963 list_for_each_safe(p
, n
, &snd_timer_list
) {
1964 struct snd_timer
*timer
= list_entry(p
, struct snd_timer
, device_list
);
1965 snd_timer_free(timer
);
1967 snd_timer_proc_done();
1968 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1969 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
1973 module_init(alsa_timer_init
)
1974 module_exit(alsa_timer_exit
)
1976 EXPORT_SYMBOL(snd_timer_open
);
1977 EXPORT_SYMBOL(snd_timer_close
);
1978 EXPORT_SYMBOL(snd_timer_resolution
);
1979 EXPORT_SYMBOL(snd_timer_start
);
1980 EXPORT_SYMBOL(snd_timer_stop
);
1981 EXPORT_SYMBOL(snd_timer_continue
);
1982 EXPORT_SYMBOL(snd_timer_pause
);
1983 EXPORT_SYMBOL(snd_timer_new
);
1984 EXPORT_SYMBOL(snd_timer_notify
);
1985 EXPORT_SYMBOL(snd_timer_global_new
);
1986 EXPORT_SYMBOL(snd_timer_global_free
);
1987 EXPORT_SYMBOL(snd_timer_global_register
);
1988 EXPORT_SYMBOL(snd_timer_interrupt
);