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/slab.h>
26 #include <linux/time.h>
27 #include <linux/mutex.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 #if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
39 #define DEFAULT_TIMER_LIMIT 3
40 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
41 #define DEFAULT_TIMER_LIMIT 2
43 #define DEFAULT_TIMER_LIMIT 1
46 static int timer_limit
= DEFAULT_TIMER_LIMIT
;
47 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.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.");
53 struct snd_timer_user
{
54 struct snd_timer_instance
*timeri
;
55 int tread
; /* enhanced read with timestamps and events */
57 unsigned long overrun
;
62 struct snd_timer_read
*queue
;
63 struct snd_timer_tread
*tqueue
;
65 unsigned long last_resolution
;
67 struct timespec tstamp
; /* trigger tstamp */
68 wait_queue_head_t qchange_sleep
;
69 struct fasync_struct
*fasync
;
70 struct mutex tread_sem
;
74 static LIST_HEAD(snd_timer_list
);
76 /* list of slave instances */
77 static LIST_HEAD(snd_timer_slave_list
);
79 /* lock for slave active lists */
80 static DEFINE_SPINLOCK(slave_active_lock
);
82 static DEFINE_MUTEX(register_mutex
);
84 static int snd_timer_free(struct snd_timer
*timer
);
85 static int snd_timer_dev_free(struct snd_device
*device
);
86 static int snd_timer_dev_register(struct snd_device
*device
);
87 static int snd_timer_dev_disconnect(struct snd_device
*device
);
89 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
);
92 * create a timer instance with the given owner string.
93 * when timer is not NULL, increments the module counter
95 static struct snd_timer_instance
*snd_timer_instance_new(char *owner
,
96 struct snd_timer
*timer
)
98 struct snd_timer_instance
*timeri
;
99 timeri
= kzalloc(sizeof(*timeri
), GFP_KERNEL
);
102 timeri
->owner
= kstrdup(owner
, GFP_KERNEL
);
103 if (! timeri
->owner
) {
107 INIT_LIST_HEAD(&timeri
->open_list
);
108 INIT_LIST_HEAD(&timeri
->active_list
);
109 INIT_LIST_HEAD(&timeri
->ack_list
);
110 INIT_LIST_HEAD(&timeri
->slave_list_head
);
111 INIT_LIST_HEAD(&timeri
->slave_active_head
);
113 timeri
->timer
= timer
;
114 if (timer
&& !try_module_get(timer
->module
)) {
115 kfree(timeri
->owner
);
124 * find a timer instance from the given timer id
126 static struct snd_timer
*snd_timer_find(struct snd_timer_id
*tid
)
128 struct snd_timer
*timer
= NULL
;
130 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
131 if (timer
->tmr_class
!= tid
->dev_class
)
133 if ((timer
->tmr_class
== SNDRV_TIMER_CLASS_CARD
||
134 timer
->tmr_class
== SNDRV_TIMER_CLASS_PCM
) &&
135 (timer
->card
== NULL
||
136 timer
->card
->number
!= tid
->card
))
138 if (timer
->tmr_device
!= tid
->device
)
140 if (timer
->tmr_subdevice
!= tid
->subdevice
)
149 static void snd_timer_request(struct snd_timer_id
*tid
)
151 if (! current
->fs
->root
)
153 switch (tid
->dev_class
) {
154 case SNDRV_TIMER_CLASS_GLOBAL
:
155 if (tid
->device
< timer_limit
)
156 request_module("snd-timer-%i", tid
->device
);
158 case SNDRV_TIMER_CLASS_CARD
:
159 case SNDRV_TIMER_CLASS_PCM
:
160 if (tid
->card
< snd_ecards_limit
)
161 request_module("snd-card-%i", tid
->card
);
171 * look for a master instance matching with the slave id of the given slave.
172 * when found, relink the open_link of the slave.
174 * call this with register_mutex down.
176 static void snd_timer_check_slave(struct snd_timer_instance
*slave
)
178 struct snd_timer
*timer
;
179 struct snd_timer_instance
*master
;
181 /* FIXME: it's really dumb to look up all entries.. */
182 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
183 list_for_each_entry(master
, &timer
->open_list_head
, open_list
) {
184 if (slave
->slave_class
== master
->slave_class
&&
185 slave
->slave_id
== master
->slave_id
) {
186 list_del(&slave
->open_list
);
187 list_add_tail(&slave
->open_list
,
188 &master
->slave_list_head
);
189 spin_lock_irq(&slave_active_lock
);
190 slave
->master
= master
;
191 slave
->timer
= master
->timer
;
192 spin_unlock_irq(&slave_active_lock
);
200 * look for slave instances matching with the slave id of the given master.
201 * when found, relink the open_link of slaves.
203 * call this with register_mutex down.
205 static void snd_timer_check_master(struct snd_timer_instance
*master
)
207 struct snd_timer_instance
*slave
, *tmp
;
209 /* check all pending slaves */
210 list_for_each_entry_safe(slave
, tmp
, &snd_timer_slave_list
, open_list
) {
211 if (slave
->slave_class
== master
->slave_class
&&
212 slave
->slave_id
== master
->slave_id
) {
213 list_move_tail(&slave
->open_list
, &master
->slave_list_head
);
214 spin_lock_irq(&slave_active_lock
);
215 slave
->master
= master
;
216 slave
->timer
= master
->timer
;
217 if (slave
->flags
& SNDRV_TIMER_IFLG_RUNNING
)
218 list_add_tail(&slave
->active_list
,
219 &master
->slave_active_head
);
220 spin_unlock_irq(&slave_active_lock
);
226 * open a timer instance
227 * when opening a master, the slave id must be here given.
229 int snd_timer_open(struct snd_timer_instance
**ti
,
230 char *owner
, struct snd_timer_id
*tid
,
231 unsigned int slave_id
)
233 struct snd_timer
*timer
;
234 struct snd_timer_instance
*timeri
= NULL
;
236 if (tid
->dev_class
== SNDRV_TIMER_CLASS_SLAVE
) {
237 /* open a slave instance */
238 if (tid
->dev_sclass
<= SNDRV_TIMER_SCLASS_NONE
||
239 tid
->dev_sclass
> SNDRV_TIMER_SCLASS_OSS_SEQUENCER
) {
240 snd_printd("invalid slave class %i\n", tid
->dev_sclass
);
243 mutex_lock(®ister_mutex
);
244 timeri
= snd_timer_instance_new(owner
, NULL
);
246 mutex_unlock(®ister_mutex
);
249 timeri
->slave_class
= tid
->dev_sclass
;
250 timeri
->slave_id
= tid
->device
;
251 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
252 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
253 snd_timer_check_slave(timeri
);
254 mutex_unlock(®ister_mutex
);
259 /* open a master instance */
260 mutex_lock(®ister_mutex
);
261 timer
= snd_timer_find(tid
);
264 mutex_unlock(®ister_mutex
);
265 snd_timer_request(tid
);
266 mutex_lock(®ister_mutex
);
267 timer
= snd_timer_find(tid
);
271 mutex_unlock(®ister_mutex
);
274 if (!list_empty(&timer
->open_list_head
)) {
275 timeri
= list_entry(timer
->open_list_head
.next
,
276 struct snd_timer_instance
, open_list
);
277 if (timeri
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
278 mutex_unlock(®ister_mutex
);
282 timeri
= snd_timer_instance_new(owner
, timer
);
284 mutex_unlock(®ister_mutex
);
287 timeri
->slave_class
= tid
->dev_sclass
;
288 timeri
->slave_id
= slave_id
;
289 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
)
290 timer
->hw
.open(timer
);
291 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
292 snd_timer_check_master(timeri
);
293 mutex_unlock(®ister_mutex
);
298 static int _snd_timer_stop(struct snd_timer_instance
*timeri
,
299 int keep_flag
, int event
);
302 * close a timer instance
304 int snd_timer_close(struct snd_timer_instance
*timeri
)
306 struct snd_timer
*timer
= NULL
;
307 struct snd_timer_instance
*slave
, *tmp
;
309 snd_assert(timeri
!= NULL
, return -ENXIO
);
311 /* force to stop the timer */
312 snd_timer_stop(timeri
);
314 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
315 /* wait, until the active callback is finished */
316 spin_lock_irq(&slave_active_lock
);
317 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
318 spin_unlock_irq(&slave_active_lock
);
320 spin_lock_irq(&slave_active_lock
);
322 spin_unlock_irq(&slave_active_lock
);
323 mutex_lock(®ister_mutex
);
324 list_del(&timeri
->open_list
);
325 mutex_unlock(®ister_mutex
);
327 timer
= timeri
->timer
;
328 /* wait, until the active callback is finished */
329 spin_lock_irq(&timer
->lock
);
330 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
331 spin_unlock_irq(&timer
->lock
);
333 spin_lock_irq(&timer
->lock
);
335 spin_unlock_irq(&timer
->lock
);
336 mutex_lock(®ister_mutex
);
337 list_del(&timeri
->open_list
);
338 if (timer
&& list_empty(&timer
->open_list_head
) &&
340 timer
->hw
.close(timer
);
341 /* remove slave links */
342 list_for_each_entry_safe(slave
, tmp
, &timeri
->slave_list_head
,
344 spin_lock_irq(&slave_active_lock
);
345 _snd_timer_stop(slave
, 1, SNDRV_TIMER_EVENT_RESOLUTION
);
346 list_move_tail(&slave
->open_list
, &snd_timer_slave_list
);
347 slave
->master
= NULL
;
349 spin_unlock_irq(&slave_active_lock
);
351 mutex_unlock(®ister_mutex
);
353 if (timeri
->private_free
)
354 timeri
->private_free(timeri
);
355 kfree(timeri
->owner
);
358 module_put(timer
->module
);
362 unsigned long snd_timer_resolution(struct snd_timer_instance
*timeri
)
364 struct snd_timer
* timer
;
368 if ((timer
= timeri
->timer
) != NULL
) {
369 if (timer
->hw
.c_resolution
)
370 return timer
->hw
.c_resolution(timer
);
371 return timer
->hw
.resolution
;
376 static void snd_timer_notify1(struct snd_timer_instance
*ti
, int event
)
378 struct snd_timer
*timer
;
380 unsigned long resolution
= 0;
381 struct snd_timer_instance
*ts
;
382 struct timespec tstamp
;
384 getnstimeofday(&tstamp
);
385 snd_assert(event
>= SNDRV_TIMER_EVENT_START
&&
386 event
<= SNDRV_TIMER_EVENT_PAUSE
, return);
387 if (event
== SNDRV_TIMER_EVENT_START
||
388 event
== SNDRV_TIMER_EVENT_CONTINUE
)
389 resolution
= snd_timer_resolution(ti
);
391 ti
->ccallback(ti
, SNDRV_TIMER_EVENT_START
, &tstamp
, resolution
);
392 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
397 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
399 spin_lock_irqsave(&timer
->lock
, flags
);
400 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
402 ts
->ccallback(ti
, event
+ 100, &tstamp
, resolution
);
403 spin_unlock_irqrestore(&timer
->lock
, flags
);
406 static int snd_timer_start1(struct snd_timer
*timer
, struct snd_timer_instance
*timeri
,
407 unsigned long sticks
)
409 list_del(&timeri
->active_list
);
410 list_add_tail(&timeri
->active_list
, &timer
->active_list_head
);
411 if (timer
->running
) {
412 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
414 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
415 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
416 return 1; /* delayed start */
418 timer
->sticks
= sticks
;
419 timer
->hw
.start(timer
);
422 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
427 static int snd_timer_start_slave(struct snd_timer_instance
*timeri
)
431 spin_lock_irqsave(&slave_active_lock
, flags
);
432 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
434 list_add_tail(&timeri
->active_list
,
435 &timeri
->master
->slave_active_head
);
436 spin_unlock_irqrestore(&slave_active_lock
, flags
);
437 return 1; /* delayed start */
441 * start the timer instance
443 int snd_timer_start(struct snd_timer_instance
*timeri
, unsigned int ticks
)
445 struct snd_timer
*timer
;
446 int result
= -EINVAL
;
449 if (timeri
== NULL
|| ticks
< 1)
451 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
452 result
= snd_timer_start_slave(timeri
);
453 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
456 timer
= timeri
->timer
;
459 spin_lock_irqsave(&timer
->lock
, flags
);
460 timeri
->ticks
= timeri
->cticks
= ticks
;
462 result
= snd_timer_start1(timer
, timeri
, ticks
);
463 spin_unlock_irqrestore(&timer
->lock
, flags
);
464 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
468 static int _snd_timer_stop(struct snd_timer_instance
* timeri
,
469 int keep_flag
, int event
)
471 struct snd_timer
*timer
;
474 snd_assert(timeri
!= NULL
, return -ENXIO
);
476 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
478 spin_lock_irqsave(&slave_active_lock
, flags
);
479 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
480 spin_unlock_irqrestore(&slave_active_lock
, flags
);
484 timer
= timeri
->timer
;
487 spin_lock_irqsave(&timer
->lock
, flags
);
488 list_del_init(&timeri
->ack_list
);
489 list_del_init(&timeri
->active_list
);
490 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
491 !(--timer
->running
)) {
492 timer
->hw
.stop(timer
);
493 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
494 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
495 snd_timer_reschedule(timer
, 0);
496 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
497 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
498 timer
->hw
.start(timer
);
504 ~(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(struct snd_timer_instance
*timeri
)
519 struct snd_timer
*timer
;
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(struct snd_timer_instance
*timeri
)
539 struct snd_timer
*timer
;
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(struct snd_timer_instance
* 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(struct snd_timer
* timer
, unsigned long ticks_left
)
576 struct snd_timer_instance
*ti
;
577 unsigned long ticks
= ~0UL;
579 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
580 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
581 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
582 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
585 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
586 if (ticks
> ti
->cticks
)
591 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
594 if (ticks
> timer
->hw
.ticks
)
595 ticks
= timer
->hw
.ticks
;
596 if (ticks_left
!= ticks
)
597 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
598 timer
->sticks
= ticks
;
605 static void snd_timer_tasklet(unsigned long arg
)
607 struct snd_timer
*timer
= (struct snd_timer
*) arg
;
608 struct snd_timer_instance
*ti
;
610 unsigned long resolution
, ticks
;
613 spin_lock_irqsave(&timer
->lock
, flags
);
614 /* now process all callbacks */
615 while (!list_empty(&timer
->sack_list_head
)) {
616 p
= timer
->sack_list_head
.next
; /* get first item */
617 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
619 /* remove from ack_list and make empty */
624 resolution
= ti
->resolution
;
626 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
627 spin_unlock(&timer
->lock
);
629 ti
->callback(ti
, resolution
, ticks
);
630 spin_lock(&timer
->lock
);
631 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
633 spin_unlock_irqrestore(&timer
->lock
, flags
);
639 * ticks_left is usually equal to timer->sticks.
642 void snd_timer_interrupt(struct snd_timer
* timer
, unsigned long ticks_left
)
644 struct snd_timer_instance
*ti
, *ts
, *tmp
;
645 unsigned long resolution
, ticks
;
646 struct list_head
*p
, *ack_list_head
;
653 spin_lock_irqsave(&timer
->lock
, flags
);
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_entry because the active_list of a
663 * processed instance is relinked to done_list_head before the callback
666 list_for_each_entry_safe(ti
, tmp
, &timer
->active_list_head
,
668 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
670 ti
->pticks
+= ticks_left
;
671 ti
->resolution
= resolution
;
672 if (ti
->cticks
< ticks_left
)
675 ti
->cticks
-= ticks_left
;
676 if (ti
->cticks
) /* not expired */
678 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
679 ti
->cticks
= ti
->ticks
;
681 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
682 if (--timer
->running
)
683 list_del(&ti
->active_list
);
685 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
686 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
))
687 ack_list_head
= &timer
->ack_list_head
;
689 ack_list_head
= &timer
->sack_list_head
;
690 if (list_empty(&ti
->ack_list
))
691 list_add_tail(&ti
->ack_list
, ack_list_head
);
692 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
) {
693 ts
->pticks
= ti
->pticks
;
694 ts
->resolution
= resolution
;
695 if (list_empty(&ts
->ack_list
))
696 list_add_tail(&ts
->ack_list
, ack_list_head
);
699 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
700 snd_timer_reschedule(timer
, timer
->sticks
);
701 if (timer
->running
) {
702 if (timer
->hw
.flags
& SNDRV_TIMER_HW_STOP
) {
703 timer
->hw
.stop(timer
);
704 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
706 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_AUTO
) ||
707 (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
)) {
709 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
710 timer
->hw
.start(timer
);
713 timer
->hw
.stop(timer
);
716 /* now process all fast callbacks */
717 while (!list_empty(&timer
->ack_list_head
)) {
718 p
= timer
->ack_list_head
.next
; /* get first item */
719 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
721 /* remove from ack_list and make empty */
727 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
728 spin_unlock(&timer
->lock
);
730 ti
->callback(ti
, resolution
, ticks
);
731 spin_lock(&timer
->lock
);
732 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
735 /* do we have any slow callbacks? */
736 use_tasklet
= !list_empty(&timer
->sack_list_head
);
737 spin_unlock_irqrestore(&timer
->lock
, flags
);
740 tasklet_hi_schedule(&timer
->task_queue
);
747 int snd_timer_new(struct snd_card
*card
, char *id
, struct snd_timer_id
*tid
,
748 struct snd_timer
**rtimer
)
750 struct snd_timer
*timer
;
752 static struct snd_device_ops ops
= {
753 .dev_free
= snd_timer_dev_free
,
754 .dev_register
= snd_timer_dev_register
,
755 .dev_disconnect
= snd_timer_dev_disconnect
,
758 snd_assert(tid
!= NULL
, return -EINVAL
);
759 snd_assert(rtimer
!= NULL
, return -EINVAL
);
761 timer
= kzalloc(sizeof(*timer
), GFP_KERNEL
);
763 snd_printk(KERN_ERR
"timer: cannot allocate\n");
766 timer
->tmr_class
= tid
->dev_class
;
768 timer
->tmr_device
= tid
->device
;
769 timer
->tmr_subdevice
= tid
->subdevice
;
771 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
772 INIT_LIST_HEAD(&timer
->device_list
);
773 INIT_LIST_HEAD(&timer
->open_list_head
);
774 INIT_LIST_HEAD(&timer
->active_list_head
);
775 INIT_LIST_HEAD(&timer
->ack_list_head
);
776 INIT_LIST_HEAD(&timer
->sack_list_head
);
777 spin_lock_init(&timer
->lock
);
778 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
,
779 (unsigned long)timer
);
781 timer
->module
= card
->module
;
782 err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
);
784 snd_timer_free(timer
);
792 static int snd_timer_free(struct snd_timer
*timer
)
794 snd_assert(timer
!= NULL
, return -ENXIO
);
796 mutex_lock(®ister_mutex
);
797 if (! list_empty(&timer
->open_list_head
)) {
798 struct list_head
*p
, *n
;
799 struct snd_timer_instance
*ti
;
800 snd_printk(KERN_WARNING
"timer %p is busy?\n", timer
);
801 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
803 ti
= list_entry(p
, struct snd_timer_instance
, open_list
);
807 list_del(&timer
->device_list
);
808 mutex_unlock(®ister_mutex
);
810 if (timer
->private_free
)
811 timer
->private_free(timer
);
816 static int snd_timer_dev_free(struct snd_device
*device
)
818 struct snd_timer
*timer
= device
->device_data
;
819 return snd_timer_free(timer
);
822 static int snd_timer_dev_register(struct snd_device
*dev
)
824 struct snd_timer
*timer
= dev
->device_data
;
825 struct snd_timer
*timer1
;
827 snd_assert(timer
!= NULL
&& timer
->hw
.start
!= NULL
&&
828 timer
->hw
.stop
!= NULL
, return -ENXIO
);
829 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
830 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
833 mutex_lock(®ister_mutex
);
834 list_for_each_entry(timer1
, &snd_timer_list
, device_list
) {
835 if (timer1
->tmr_class
> timer
->tmr_class
)
837 if (timer1
->tmr_class
< timer
->tmr_class
)
839 if (timer1
->card
&& timer
->card
) {
840 if (timer1
->card
->number
> timer
->card
->number
)
842 if (timer1
->card
->number
< timer
->card
->number
)
845 if (timer1
->tmr_device
> timer
->tmr_device
)
847 if (timer1
->tmr_device
< timer
->tmr_device
)
849 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
851 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
854 mutex_unlock(®ister_mutex
);
857 list_add_tail(&timer
->device_list
, &timer1
->device_list
);
858 mutex_unlock(®ister_mutex
);
862 static int snd_timer_dev_disconnect(struct snd_device
*device
)
864 struct snd_timer
*timer
= device
->device_data
;
865 mutex_lock(®ister_mutex
);
866 list_del_init(&timer
->device_list
);
867 mutex_unlock(®ister_mutex
);
871 void snd_timer_notify(struct snd_timer
*timer
, int event
, struct timespec
*tstamp
)
874 unsigned long resolution
= 0;
875 struct snd_timer_instance
*ti
, *ts
;
877 if (! (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
))
879 snd_assert(event
>= SNDRV_TIMER_EVENT_MSTART
&&
880 event
<= SNDRV_TIMER_EVENT_MRESUME
, return);
881 spin_lock_irqsave(&timer
->lock
, flags
);
882 if (event
== SNDRV_TIMER_EVENT_MSTART
||
883 event
== SNDRV_TIMER_EVENT_MCONTINUE
||
884 event
== SNDRV_TIMER_EVENT_MRESUME
) {
885 if (timer
->hw
.c_resolution
)
886 resolution
= timer
->hw
.c_resolution(timer
);
888 resolution
= timer
->hw
.resolution
;
890 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
892 ti
->ccallback(ti
, event
, tstamp
, resolution
);
893 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
895 ts
->ccallback(ts
, event
, tstamp
, resolution
);
897 spin_unlock_irqrestore(&timer
->lock
, flags
);
901 * exported functions for global timers
903 int snd_timer_global_new(char *id
, int device
, struct snd_timer
**rtimer
)
905 struct snd_timer_id tid
;
907 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
908 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
912 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
915 int snd_timer_global_free(struct snd_timer
*timer
)
917 return snd_timer_free(timer
);
920 int snd_timer_global_register(struct snd_timer
*timer
)
922 struct snd_device dev
;
924 memset(&dev
, 0, sizeof(dev
));
925 dev
.device_data
= timer
;
926 return snd_timer_dev_register(&dev
);
933 struct snd_timer_system_private
{
934 struct timer_list tlist
;
935 unsigned long last_expires
;
936 unsigned long last_jiffies
;
937 unsigned long correction
;
940 static void snd_timer_s_function(unsigned long data
)
942 struct snd_timer
*timer
= (struct snd_timer
*)data
;
943 struct snd_timer_system_private
*priv
= timer
->private_data
;
944 unsigned long jiff
= jiffies
;
945 if (time_after(jiff
, priv
->last_expires
))
946 priv
->correction
+= (long)jiff
- (long)priv
->last_expires
;
947 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
950 static int snd_timer_s_start(struct snd_timer
* timer
)
952 struct snd_timer_system_private
*priv
;
955 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
956 njiff
= (priv
->last_jiffies
= jiffies
);
957 if (priv
->correction
> timer
->sticks
- 1) {
958 priv
->correction
-= timer
->sticks
- 1;
961 njiff
+= timer
->sticks
- priv
->correction
;
962 priv
->correction
= 0;
964 priv
->last_expires
= priv
->tlist
.expires
= njiff
;
965 add_timer(&priv
->tlist
);
969 static int snd_timer_s_stop(struct snd_timer
* timer
)
971 struct snd_timer_system_private
*priv
;
974 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
975 del_timer(&priv
->tlist
);
977 if (time_before(jiff
, priv
->last_expires
))
978 timer
->sticks
= priv
->last_expires
- jiff
;
981 priv
->correction
= 0;
985 static struct snd_timer_hardware snd_timer_system
=
987 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
988 .resolution
= 1000000000L / HZ
,
990 .start
= snd_timer_s_start
,
991 .stop
= snd_timer_s_stop
994 static void snd_timer_free_system(struct snd_timer
*timer
)
996 kfree(timer
->private_data
);
999 static int snd_timer_register_system(void)
1001 struct snd_timer
*timer
;
1002 struct snd_timer_system_private
*priv
;
1005 err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
);
1008 strcpy(timer
->name
, "system timer");
1009 timer
->hw
= snd_timer_system
;
1010 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1012 snd_timer_free(timer
);
1015 init_timer(&priv
->tlist
);
1016 priv
->tlist
.function
= snd_timer_s_function
;
1017 priv
->tlist
.data
= (unsigned long) timer
;
1018 timer
->private_data
= priv
;
1019 timer
->private_free
= snd_timer_free_system
;
1020 return snd_timer_global_register(timer
);
1023 #ifdef CONFIG_PROC_FS
1028 static void snd_timer_proc_read(struct snd_info_entry
*entry
,
1029 struct snd_info_buffer
*buffer
)
1031 struct snd_timer
*timer
;
1032 struct snd_timer_instance
*ti
;
1034 mutex_lock(®ister_mutex
);
1035 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
1036 switch (timer
->tmr_class
) {
1037 case SNDRV_TIMER_CLASS_GLOBAL
:
1038 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1040 case SNDRV_TIMER_CLASS_CARD
:
1041 snd_iprintf(buffer
, "C%i-%i: ",
1042 timer
->card
->number
, timer
->tmr_device
);
1044 case SNDRV_TIMER_CLASS_PCM
:
1045 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
,
1046 timer
->tmr_device
, timer
->tmr_subdevice
);
1049 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
,
1050 timer
->card
? timer
->card
->number
: -1,
1051 timer
->tmr_device
, timer
->tmr_subdevice
);
1053 snd_iprintf(buffer
, "%s :", timer
->name
);
1054 if (timer
->hw
.resolution
)
1055 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)",
1056 timer
->hw
.resolution
/ 1000,
1057 timer
->hw
.resolution
% 1000,
1059 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1060 snd_iprintf(buffer
, " SLAVE");
1061 snd_iprintf(buffer
, "\n");
1062 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
)
1063 snd_iprintf(buffer
, " Client %s : %s\n",
1064 ti
->owner
? ti
->owner
: "unknown",
1065 ti
->flags
& (SNDRV_TIMER_IFLG_START
|
1066 SNDRV_TIMER_IFLG_RUNNING
)
1067 ? "running" : "stopped");
1069 mutex_unlock(®ister_mutex
);
1072 static struct snd_info_entry
*snd_timer_proc_entry
;
1074 static void __init
snd_timer_proc_init(void)
1076 struct snd_info_entry
*entry
;
1078 entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
);
1079 if (entry
!= NULL
) {
1080 entry
->c
.text
.read
= snd_timer_proc_read
;
1081 if (snd_info_register(entry
) < 0) {
1082 snd_info_free_entry(entry
);
1086 snd_timer_proc_entry
= entry
;
1089 static void __exit
snd_timer_proc_done(void)
1091 snd_info_free_entry(snd_timer_proc_entry
);
1093 #else /* !CONFIG_PROC_FS */
1094 #define snd_timer_proc_init()
1095 #define snd_timer_proc_done()
1099 * USER SPACE interface
1102 static void snd_timer_user_interrupt(struct snd_timer_instance
*timeri
,
1103 unsigned long resolution
,
1104 unsigned long ticks
)
1106 struct snd_timer_user
*tu
= timeri
->callback_data
;
1107 struct snd_timer_read
*r
;
1110 spin_lock(&tu
->qlock
);
1111 if (tu
->qused
> 0) {
1112 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1113 r
= &tu
->queue
[prev
];
1114 if (r
->resolution
== resolution
) {
1119 if (tu
->qused
>= tu
->queue_size
) {
1122 r
= &tu
->queue
[tu
->qtail
++];
1123 tu
->qtail
%= tu
->queue_size
;
1124 r
->resolution
= resolution
;
1129 spin_unlock(&tu
->qlock
);
1130 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1131 wake_up(&tu
->qchange_sleep
);
1134 static void snd_timer_user_append_to_tqueue(struct snd_timer_user
*tu
,
1135 struct snd_timer_tread
*tread
)
1137 if (tu
->qused
>= tu
->queue_size
) {
1140 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1141 tu
->qtail
%= tu
->queue_size
;
1146 static void snd_timer_user_ccallback(struct snd_timer_instance
*timeri
,
1148 struct timespec
*tstamp
,
1149 unsigned long resolution
)
1151 struct snd_timer_user
*tu
= timeri
->callback_data
;
1152 struct snd_timer_tread r1
;
1154 if (event
>= SNDRV_TIMER_EVENT_START
&&
1155 event
<= SNDRV_TIMER_EVENT_PAUSE
)
1156 tu
->tstamp
= *tstamp
;
1157 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1160 r1
.tstamp
= *tstamp
;
1161 r1
.val
= resolution
;
1162 spin_lock(&tu
->qlock
);
1163 snd_timer_user_append_to_tqueue(tu
, &r1
);
1164 spin_unlock(&tu
->qlock
);
1165 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1166 wake_up(&tu
->qchange_sleep
);
1169 static void snd_timer_user_tinterrupt(struct snd_timer_instance
*timeri
,
1170 unsigned long resolution
,
1171 unsigned long ticks
)
1173 struct snd_timer_user
*tu
= timeri
->callback_data
;
1174 struct snd_timer_tread
*r
, r1
;
1175 struct timespec tstamp
;
1176 int prev
, append
= 0;
1178 memset(&tstamp
, 0, sizeof(tstamp
));
1179 spin_lock(&tu
->qlock
);
1180 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
) |
1181 (1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1182 spin_unlock(&tu
->qlock
);
1185 if (tu
->last_resolution
!= resolution
|| ticks
> 0)
1186 getnstimeofday(&tstamp
);
1187 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) &&
1188 tu
->last_resolution
!= resolution
) {
1189 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1191 r1
.val
= resolution
;
1192 snd_timer_user_append_to_tqueue(tu
, &r1
);
1193 tu
->last_resolution
= resolution
;
1196 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1200 if (tu
->qused
> 0) {
1201 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1202 r
= &tu
->tqueue
[prev
];
1203 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1210 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1213 snd_timer_user_append_to_tqueue(tu
, &r1
);
1216 spin_unlock(&tu
->qlock
);
1219 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1220 wake_up(&tu
->qchange_sleep
);
1223 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1225 struct snd_timer_user
*tu
;
1227 tu
= kzalloc(sizeof(*tu
), GFP_KERNEL
);
1230 spin_lock_init(&tu
->qlock
);
1231 init_waitqueue_head(&tu
->qchange_sleep
);
1232 mutex_init(&tu
->tread_sem
);
1234 tu
->queue_size
= 128;
1235 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1237 if (tu
->queue
== NULL
) {
1241 file
->private_data
= tu
;
1245 static int snd_timer_user_release(struct inode
*inode
, struct file
*file
)
1247 struct snd_timer_user
*tu
;
1249 if (file
->private_data
) {
1250 tu
= file
->private_data
;
1251 file
->private_data
= NULL
;
1252 fasync_helper(-1, file
, 0, &tu
->fasync
);
1254 snd_timer_close(tu
->timeri
);
1262 static void snd_timer_user_zero_id(struct snd_timer_id
*id
)
1264 id
->dev_class
= SNDRV_TIMER_CLASS_NONE
;
1265 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1271 static void snd_timer_user_copy_id(struct snd_timer_id
*id
, struct snd_timer
*timer
)
1273 id
->dev_class
= timer
->tmr_class
;
1274 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1275 id
->card
= timer
->card
? timer
->card
->number
: -1;
1276 id
->device
= timer
->tmr_device
;
1277 id
->subdevice
= timer
->tmr_subdevice
;
1280 static int snd_timer_user_next_device(struct snd_timer_id __user
*_tid
)
1282 struct snd_timer_id id
;
1283 struct snd_timer
*timer
;
1284 struct list_head
*p
;
1286 if (copy_from_user(&id
, _tid
, sizeof(id
)))
1288 mutex_lock(®ister_mutex
);
1289 if (id
.dev_class
< 0) { /* first item */
1290 if (list_empty(&snd_timer_list
))
1291 snd_timer_user_zero_id(&id
);
1293 timer
= list_entry(snd_timer_list
.next
,
1294 struct snd_timer
, device_list
);
1295 snd_timer_user_copy_id(&id
, timer
);
1298 switch (id
.dev_class
) {
1299 case SNDRV_TIMER_CLASS_GLOBAL
:
1300 id
.device
= id
.device
< 0 ? 0 : id
.device
+ 1;
1301 list_for_each(p
, &snd_timer_list
) {
1302 timer
= list_entry(p
, struct snd_timer
, device_list
);
1303 if (timer
->tmr_class
> SNDRV_TIMER_CLASS_GLOBAL
) {
1304 snd_timer_user_copy_id(&id
, timer
);
1307 if (timer
->tmr_device
>= id
.device
) {
1308 snd_timer_user_copy_id(&id
, timer
);
1312 if (p
== &snd_timer_list
)
1313 snd_timer_user_zero_id(&id
);
1315 case SNDRV_TIMER_CLASS_CARD
:
1316 case SNDRV_TIMER_CLASS_PCM
:
1323 if (id
.device
< 0) {
1326 if (id
.subdevice
< 0) {
1334 list_for_each(p
, &snd_timer_list
) {
1335 timer
= list_entry(p
, struct snd_timer
, device_list
);
1336 if (timer
->tmr_class
> id
.dev_class
) {
1337 snd_timer_user_copy_id(&id
, timer
);
1340 if (timer
->tmr_class
< id
.dev_class
)
1342 if (timer
->card
->number
> id
.card
) {
1343 snd_timer_user_copy_id(&id
, timer
);
1346 if (timer
->card
->number
< id
.card
)
1348 if (timer
->tmr_device
> id
.device
) {
1349 snd_timer_user_copy_id(&id
, timer
);
1352 if (timer
->tmr_device
< id
.device
)
1354 if (timer
->tmr_subdevice
> id
.subdevice
) {
1355 snd_timer_user_copy_id(&id
, timer
);
1358 if (timer
->tmr_subdevice
< id
.subdevice
)
1360 snd_timer_user_copy_id(&id
, timer
);
1363 if (p
== &snd_timer_list
)
1364 snd_timer_user_zero_id(&id
);
1367 snd_timer_user_zero_id(&id
);
1370 mutex_unlock(®ister_mutex
);
1371 if (copy_to_user(_tid
, &id
, sizeof(*_tid
)))
1376 static int snd_timer_user_ginfo(struct file
*file
,
1377 struct snd_timer_ginfo __user
*_ginfo
)
1379 struct snd_timer_ginfo
*ginfo
;
1380 struct snd_timer_id tid
;
1381 struct snd_timer
*t
;
1382 struct list_head
*p
;
1385 ginfo
= kmalloc(sizeof(*ginfo
), GFP_KERNEL
);
1388 if (copy_from_user(ginfo
, _ginfo
, sizeof(*ginfo
))) {
1393 memset(ginfo
, 0, sizeof(*ginfo
));
1395 mutex_lock(®ister_mutex
);
1396 t
= snd_timer_find(&tid
);
1398 ginfo
->card
= t
->card
? t
->card
->number
: -1;
1399 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1400 ginfo
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1401 strlcpy(ginfo
->id
, t
->id
, sizeof(ginfo
->id
));
1402 strlcpy(ginfo
->name
, t
->name
, sizeof(ginfo
->name
));
1403 ginfo
->resolution
= t
->hw
.resolution
;
1404 if (t
->hw
.resolution_min
> 0) {
1405 ginfo
->resolution_min
= t
->hw
.resolution_min
;
1406 ginfo
->resolution_max
= t
->hw
.resolution_max
;
1408 list_for_each(p
, &t
->open_list_head
) {
1414 mutex_unlock(®ister_mutex
);
1415 if (err
>= 0 && copy_to_user(_ginfo
, ginfo
, sizeof(*ginfo
)))
1421 static int snd_timer_user_gparams(struct file
*file
,
1422 struct snd_timer_gparams __user
*_gparams
)
1424 struct snd_timer_gparams gparams
;
1425 struct snd_timer
*t
;
1428 if (copy_from_user(&gparams
, _gparams
, sizeof(gparams
)))
1430 mutex_lock(®ister_mutex
);
1431 t
= snd_timer_find(&gparams
.tid
);
1436 if (!list_empty(&t
->open_list_head
)) {
1440 if (!t
->hw
.set_period
) {
1444 err
= t
->hw
.set_period(t
, gparams
.period_num
, gparams
.period_den
);
1446 mutex_unlock(®ister_mutex
);
1450 static int snd_timer_user_gstatus(struct file
*file
,
1451 struct snd_timer_gstatus __user
*_gstatus
)
1453 struct snd_timer_gstatus gstatus
;
1454 struct snd_timer_id tid
;
1455 struct snd_timer
*t
;
1458 if (copy_from_user(&gstatus
, _gstatus
, sizeof(gstatus
)))
1461 memset(&gstatus
, 0, sizeof(gstatus
));
1463 mutex_lock(®ister_mutex
);
1464 t
= snd_timer_find(&tid
);
1466 if (t
->hw
.c_resolution
)
1467 gstatus
.resolution
= t
->hw
.c_resolution(t
);
1469 gstatus
.resolution
= t
->hw
.resolution
;
1470 if (t
->hw
.precise_resolution
) {
1471 t
->hw
.precise_resolution(t
, &gstatus
.resolution_num
,
1472 &gstatus
.resolution_den
);
1474 gstatus
.resolution_num
= gstatus
.resolution
;
1475 gstatus
.resolution_den
= 1000000000uL;
1480 mutex_unlock(®ister_mutex
);
1481 if (err
>= 0 && copy_to_user(_gstatus
, &gstatus
, sizeof(gstatus
)))
1486 static int snd_timer_user_tselect(struct file
*file
,
1487 struct snd_timer_select __user
*_tselect
)
1489 struct snd_timer_user
*tu
;
1490 struct snd_timer_select tselect
;
1494 tu
= file
->private_data
;
1495 mutex_lock(&tu
->tread_sem
);
1497 snd_timer_close(tu
->timeri
);
1500 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
))) {
1504 sprintf(str
, "application %i", current
->pid
);
1505 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1506 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1507 err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
);
1516 tu
->tqueue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_tread
),
1518 if (tu
->tqueue
== NULL
)
1521 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1523 if (tu
->queue
== NULL
)
1528 snd_timer_close(tu
->timeri
);
1531 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1532 tu
->timeri
->callback
= tu
->tread
1533 ? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1534 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1535 tu
->timeri
->callback_data
= (void *)tu
;
1539 mutex_unlock(&tu
->tread_sem
);
1543 static int snd_timer_user_info(struct file
*file
,
1544 struct snd_timer_info __user
*_info
)
1546 struct snd_timer_user
*tu
;
1547 struct snd_timer_info
*info
;
1548 struct snd_timer
*t
;
1551 tu
= file
->private_data
;
1552 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1553 t
= tu
->timeri
->timer
;
1554 snd_assert(t
!= NULL
, return -ENXIO
);
1556 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1559 info
->card
= t
->card
? t
->card
->number
: -1;
1560 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1561 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1562 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1563 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1564 info
->resolution
= t
->hw
.resolution
;
1565 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1571 static int snd_timer_user_params(struct file
*file
,
1572 struct snd_timer_params __user
*_params
)
1574 struct snd_timer_user
*tu
;
1575 struct snd_timer_params params
;
1576 struct snd_timer
*t
;
1577 struct snd_timer_read
*tr
;
1578 struct snd_timer_tread
*ttr
;
1581 tu
= file
->private_data
;
1582 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1583 t
= tu
->timeri
->timer
;
1584 snd_assert(t
!= NULL
, return -ENXIO
);
1585 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
1587 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) && params
.ticks
< 1) {
1591 if (params
.queue_size
> 0 &&
1592 (params
.queue_size
< 32 || params
.queue_size
> 1024)) {
1596 if (params
.filter
& ~((1<<SNDRV_TIMER_EVENT_RESOLUTION
)|
1597 (1<<SNDRV_TIMER_EVENT_TICK
)|
1598 (1<<SNDRV_TIMER_EVENT_START
)|
1599 (1<<SNDRV_TIMER_EVENT_STOP
)|
1600 (1<<SNDRV_TIMER_EVENT_CONTINUE
)|
1601 (1<<SNDRV_TIMER_EVENT_PAUSE
)|
1602 (1<<SNDRV_TIMER_EVENT_SUSPEND
)|
1603 (1<<SNDRV_TIMER_EVENT_RESUME
)|
1604 (1<<SNDRV_TIMER_EVENT_MSTART
)|
1605 (1<<SNDRV_TIMER_EVENT_MSTOP
)|
1606 (1<<SNDRV_TIMER_EVENT_MCONTINUE
)|
1607 (1<<SNDRV_TIMER_EVENT_MPAUSE
)|
1608 (1<<SNDRV_TIMER_EVENT_MSUSPEND
)|
1609 (1<<SNDRV_TIMER_EVENT_MRESUME
))) {
1613 snd_timer_stop(tu
->timeri
);
1614 spin_lock_irq(&t
->lock
);
1615 tu
->timeri
->flags
&= ~(SNDRV_TIMER_IFLG_AUTO
|
1616 SNDRV_TIMER_IFLG_EXCLUSIVE
|
1617 SNDRV_TIMER_IFLG_EARLY_EVENT
);
1618 if (params
.flags
& SNDRV_TIMER_PSFLG_AUTO
)
1619 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
1620 if (params
.flags
& SNDRV_TIMER_PSFLG_EXCLUSIVE
)
1621 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EXCLUSIVE
;
1622 if (params
.flags
& SNDRV_TIMER_PSFLG_EARLY_EVENT
)
1623 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EARLY_EVENT
;
1624 spin_unlock_irq(&t
->lock
);
1625 if (params
.queue_size
> 0 &&
1626 (unsigned int)tu
->queue_size
!= params
.queue_size
) {
1628 ttr
= kmalloc(params
.queue_size
* sizeof(*ttr
),
1632 tu
->queue_size
= params
.queue_size
;
1636 tr
= kmalloc(params
.queue_size
* sizeof(*tr
),
1640 tu
->queue_size
= params
.queue_size
;
1645 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1646 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1648 struct snd_timer_tread tread
;
1649 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1650 tread
.tstamp
.tv_sec
= 0;
1651 tread
.tstamp
.tv_nsec
= 0;
1653 snd_timer_user_append_to_tqueue(tu
, &tread
);
1655 struct snd_timer_read
*r
= &tu
->queue
[0];
1662 tu
->filter
= params
.filter
;
1663 tu
->ticks
= params
.ticks
;
1666 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1671 static int snd_timer_user_status(struct file
*file
,
1672 struct snd_timer_status __user
*_status
)
1674 struct snd_timer_user
*tu
;
1675 struct snd_timer_status status
;
1677 tu
= file
->private_data
;
1678 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1679 memset(&status
, 0, sizeof(status
));
1680 status
.tstamp
= tu
->tstamp
;
1681 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1682 status
.lost
= tu
->timeri
->lost
;
1683 status
.overrun
= tu
->overrun
;
1684 spin_lock_irq(&tu
->qlock
);
1685 status
.queue
= tu
->qused
;
1686 spin_unlock_irq(&tu
->qlock
);
1687 if (copy_to_user(_status
, &status
, sizeof(status
)))
1692 static int snd_timer_user_start(struct file
*file
)
1695 struct snd_timer_user
*tu
;
1697 tu
= file
->private_data
;
1698 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1699 snd_timer_stop(tu
->timeri
);
1700 tu
->timeri
->lost
= 0;
1701 tu
->last_resolution
= 0;
1702 return (err
= snd_timer_start(tu
->timeri
, tu
->ticks
)) < 0 ? err
: 0;
1705 static int snd_timer_user_stop(struct file
*file
)
1708 struct snd_timer_user
*tu
;
1710 tu
= file
->private_data
;
1711 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1712 return (err
= snd_timer_stop(tu
->timeri
)) < 0 ? err
: 0;
1715 static int snd_timer_user_continue(struct file
*file
)
1718 struct snd_timer_user
*tu
;
1720 tu
= file
->private_data
;
1721 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1722 tu
->timeri
->lost
= 0;
1723 return (err
= snd_timer_continue(tu
->timeri
)) < 0 ? err
: 0;
1726 static int snd_timer_user_pause(struct file
*file
)
1729 struct snd_timer_user
*tu
;
1731 tu
= file
->private_data
;
1732 snd_assert(tu
->timeri
!= NULL
, return -ENXIO
);
1733 return (err
= snd_timer_pause(tu
->timeri
)) < 0 ? err
: 0;
1737 SNDRV_TIMER_IOCTL_START_OLD
= _IO('T', 0x20),
1738 SNDRV_TIMER_IOCTL_STOP_OLD
= _IO('T', 0x21),
1739 SNDRV_TIMER_IOCTL_CONTINUE_OLD
= _IO('T', 0x22),
1740 SNDRV_TIMER_IOCTL_PAUSE_OLD
= _IO('T', 0x23),
1743 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1746 struct snd_timer_user
*tu
;
1747 void __user
*argp
= (void __user
*)arg
;
1748 int __user
*p
= argp
;
1750 tu
= file
->private_data
;
1752 case SNDRV_TIMER_IOCTL_PVERSION
:
1753 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1754 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1755 return snd_timer_user_next_device(argp
);
1756 case SNDRV_TIMER_IOCTL_TREAD
:
1760 mutex_lock(&tu
->tread_sem
);
1761 if (tu
->timeri
) { /* too late */
1762 mutex_unlock(&tu
->tread_sem
);
1765 if (get_user(xarg
, p
)) {
1766 mutex_unlock(&tu
->tread_sem
);
1769 tu
->tread
= xarg
? 1 : 0;
1770 mutex_unlock(&tu
->tread_sem
);
1773 case SNDRV_TIMER_IOCTL_GINFO
:
1774 return snd_timer_user_ginfo(file
, argp
);
1775 case SNDRV_TIMER_IOCTL_GPARAMS
:
1776 return snd_timer_user_gparams(file
, argp
);
1777 case SNDRV_TIMER_IOCTL_GSTATUS
:
1778 return snd_timer_user_gstatus(file
, argp
);
1779 case SNDRV_TIMER_IOCTL_SELECT
:
1780 return snd_timer_user_tselect(file
, argp
);
1781 case SNDRV_TIMER_IOCTL_INFO
:
1782 return snd_timer_user_info(file
, argp
);
1783 case SNDRV_TIMER_IOCTL_PARAMS
:
1784 return snd_timer_user_params(file
, argp
);
1785 case SNDRV_TIMER_IOCTL_STATUS
:
1786 return snd_timer_user_status(file
, argp
);
1787 case SNDRV_TIMER_IOCTL_START
:
1788 case SNDRV_TIMER_IOCTL_START_OLD
:
1789 return snd_timer_user_start(file
);
1790 case SNDRV_TIMER_IOCTL_STOP
:
1791 case SNDRV_TIMER_IOCTL_STOP_OLD
:
1792 return snd_timer_user_stop(file
);
1793 case SNDRV_TIMER_IOCTL_CONTINUE
:
1794 case SNDRV_TIMER_IOCTL_CONTINUE_OLD
:
1795 return snd_timer_user_continue(file
);
1796 case SNDRV_TIMER_IOCTL_PAUSE
:
1797 case SNDRV_TIMER_IOCTL_PAUSE_OLD
:
1798 return snd_timer_user_pause(file
);
1803 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
1805 struct snd_timer_user
*tu
;
1808 tu
= file
->private_data
;
1809 err
= fasync_helper(fd
, file
, on
, &tu
->fasync
);
1815 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
,
1816 size_t count
, loff_t
*offset
)
1818 struct snd_timer_user
*tu
;
1819 long result
= 0, unit
;
1822 tu
= file
->private_data
;
1823 unit
= tu
->tread
? sizeof(struct snd_timer_tread
) : sizeof(struct snd_timer_read
);
1824 spin_lock_irq(&tu
->qlock
);
1825 while ((long)count
- result
>= unit
) {
1826 while (!tu
->qused
) {
1829 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1834 set_current_state(TASK_INTERRUPTIBLE
);
1835 init_waitqueue_entry(&wait
, current
);
1836 add_wait_queue(&tu
->qchange_sleep
, &wait
);
1838 spin_unlock_irq(&tu
->qlock
);
1840 spin_lock_irq(&tu
->qlock
);
1842 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
1844 if (signal_pending(current
)) {
1850 spin_unlock_irq(&tu
->qlock
);
1855 if (copy_to_user(buffer
, &tu
->tqueue
[tu
->qhead
++],
1856 sizeof(struct snd_timer_tread
))) {
1861 if (copy_to_user(buffer
, &tu
->queue
[tu
->qhead
++],
1862 sizeof(struct snd_timer_read
))) {
1868 tu
->qhead
%= tu
->queue_size
;
1873 spin_lock_irq(&tu
->qlock
);
1876 spin_unlock_irq(&tu
->qlock
);
1878 return result
> 0 ? result
: err
;
1881 static unsigned int snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
1884 struct snd_timer_user
*tu
;
1886 tu
= file
->private_data
;
1888 poll_wait(file
, &tu
->qchange_sleep
, wait
);
1892 mask
|= POLLIN
| POLLRDNORM
;
1897 #ifdef CONFIG_COMPAT
1898 #include "timer_compat.c"
1900 #define snd_timer_user_ioctl_compat NULL
1903 static const struct file_operations snd_timer_f_ops
=
1905 .owner
= THIS_MODULE
,
1906 .read
= snd_timer_user_read
,
1907 .open
= snd_timer_user_open
,
1908 .release
= snd_timer_user_release
,
1909 .poll
= snd_timer_user_poll
,
1910 .unlocked_ioctl
= snd_timer_user_ioctl
,
1911 .compat_ioctl
= snd_timer_user_ioctl_compat
,
1912 .fasync
= snd_timer_user_fasync
,
1919 static int __init
alsa_timer_init(void)
1923 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1924 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1,
1928 if ((err
= snd_timer_register_system()) < 0)
1929 snd_printk(KERN_ERR
"unable to register system timer (%i)\n",
1931 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0,
1932 &snd_timer_f_ops
, NULL
, "timer")) < 0)
1933 snd_printk(KERN_ERR
"unable to register timer device (%i)\n",
1935 snd_timer_proc_init();
1939 static void __exit
alsa_timer_exit(void)
1941 struct list_head
*p
, *n
;
1943 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0);
1944 /* unregister the system timer */
1945 list_for_each_safe(p
, n
, &snd_timer_list
) {
1946 struct snd_timer
*timer
= list_entry(p
, struct snd_timer
, device_list
);
1947 snd_timer_free(timer
);
1949 snd_timer_proc_done();
1950 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1951 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
1955 module_init(alsa_timer_init
)
1956 module_exit(alsa_timer_exit
)
1958 EXPORT_SYMBOL(snd_timer_open
);
1959 EXPORT_SYMBOL(snd_timer_close
);
1960 EXPORT_SYMBOL(snd_timer_resolution
);
1961 EXPORT_SYMBOL(snd_timer_start
);
1962 EXPORT_SYMBOL(snd_timer_stop
);
1963 EXPORT_SYMBOL(snd_timer_continue
);
1964 EXPORT_SYMBOL(snd_timer_pause
);
1965 EXPORT_SYMBOL(snd_timer_new
);
1966 EXPORT_SYMBOL(snd_timer_notify
);
1967 EXPORT_SYMBOL(snd_timer_global_new
);
1968 EXPORT_SYMBOL(snd_timer_global_free
);
1969 EXPORT_SYMBOL(snd_timer_global_register
);
1970 EXPORT_SYMBOL(snd_timer_interrupt
);