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/device.h>
28 #include <linux/module.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_HRTIMER) || defined(CONFIG_SND_HRTIMER_MODULE)
39 #define DEFAULT_TIMER_LIMIT 4
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 static int timer_tstamp_monotonic
= 1;
48 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
49 MODULE_DESCRIPTION("ALSA timer interface");
50 MODULE_LICENSE("GPL");
51 module_param(timer_limit
, int, 0444);
52 MODULE_PARM_DESC(timer_limit
, "Maximum global timers in system.");
53 module_param(timer_tstamp_monotonic
, int, 0444);
54 MODULE_PARM_DESC(timer_tstamp_monotonic
, "Use posix monotonic clock source for timestamps (default).");
56 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR
, SNDRV_MINOR_TIMER
);
57 MODULE_ALIAS("devname:snd/timer");
59 struct snd_timer_user
{
60 struct snd_timer_instance
*timeri
;
61 int tread
; /* enhanced read with timestamps and events */
63 unsigned long overrun
;
68 struct snd_timer_read
*queue
;
69 struct snd_timer_tread
*tqueue
;
71 unsigned long last_resolution
;
73 struct timespec tstamp
; /* trigger tstamp */
74 wait_queue_head_t qchange_sleep
;
75 struct fasync_struct
*fasync
;
76 struct mutex ioctl_lock
;
80 static LIST_HEAD(snd_timer_list
);
82 /* list of slave instances */
83 static LIST_HEAD(snd_timer_slave_list
);
85 /* lock for slave active lists */
86 static DEFINE_SPINLOCK(slave_active_lock
);
88 static DEFINE_MUTEX(register_mutex
);
90 static int snd_timer_free(struct snd_timer
*timer
);
91 static int snd_timer_dev_free(struct snd_device
*device
);
92 static int snd_timer_dev_register(struct snd_device
*device
);
93 static int snd_timer_dev_disconnect(struct snd_device
*device
);
95 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
);
98 * create a timer instance with the given owner string.
99 * when timer is not NULL, increments the module counter
101 static struct snd_timer_instance
*snd_timer_instance_new(char *owner
,
102 struct snd_timer
*timer
)
104 struct snd_timer_instance
*timeri
;
105 timeri
= kzalloc(sizeof(*timeri
), GFP_KERNEL
);
108 timeri
->owner
= kstrdup(owner
, GFP_KERNEL
);
109 if (! timeri
->owner
) {
113 INIT_LIST_HEAD(&timeri
->open_list
);
114 INIT_LIST_HEAD(&timeri
->active_list
);
115 INIT_LIST_HEAD(&timeri
->ack_list
);
116 INIT_LIST_HEAD(&timeri
->slave_list_head
);
117 INIT_LIST_HEAD(&timeri
->slave_active_head
);
119 timeri
->timer
= timer
;
120 if (timer
&& !try_module_get(timer
->module
)) {
121 kfree(timeri
->owner
);
130 * find a timer instance from the given timer id
132 static struct snd_timer
*snd_timer_find(struct snd_timer_id
*tid
)
134 struct snd_timer
*timer
= NULL
;
136 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
137 if (timer
->tmr_class
!= tid
->dev_class
)
139 if ((timer
->tmr_class
== SNDRV_TIMER_CLASS_CARD
||
140 timer
->tmr_class
== SNDRV_TIMER_CLASS_PCM
) &&
141 (timer
->card
== NULL
||
142 timer
->card
->number
!= tid
->card
))
144 if (timer
->tmr_device
!= tid
->device
)
146 if (timer
->tmr_subdevice
!= tid
->subdevice
)
153 #ifdef CONFIG_MODULES
155 static void snd_timer_request(struct snd_timer_id
*tid
)
157 switch (tid
->dev_class
) {
158 case SNDRV_TIMER_CLASS_GLOBAL
:
159 if (tid
->device
< timer_limit
)
160 request_module("snd-timer-%i", tid
->device
);
162 case SNDRV_TIMER_CLASS_CARD
:
163 case SNDRV_TIMER_CLASS_PCM
:
164 if (tid
->card
< snd_ecards_limit
)
165 request_module("snd-card-%i", tid
->card
);
175 * look for a master instance matching with the slave id of the given slave.
176 * when found, relink the open_link of the slave.
178 * call this with register_mutex down.
180 static void snd_timer_check_slave(struct snd_timer_instance
*slave
)
182 struct snd_timer
*timer
;
183 struct snd_timer_instance
*master
;
185 /* FIXME: it's really dumb to look up all entries.. */
186 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
187 list_for_each_entry(master
, &timer
->open_list_head
, open_list
) {
188 if (slave
->slave_class
== master
->slave_class
&&
189 slave
->slave_id
== master
->slave_id
) {
190 list_move_tail(&slave
->open_list
,
191 &master
->slave_list_head
);
192 spin_lock_irq(&slave_active_lock
);
193 slave
->master
= master
;
194 slave
->timer
= master
->timer
;
195 spin_unlock_irq(&slave_active_lock
);
203 * look for slave instances matching with the slave id of the given master.
204 * when found, relink the open_link of slaves.
206 * call this with register_mutex down.
208 static void snd_timer_check_master(struct snd_timer_instance
*master
)
210 struct snd_timer_instance
*slave
, *tmp
;
212 /* check all pending slaves */
213 list_for_each_entry_safe(slave
, tmp
, &snd_timer_slave_list
, open_list
) {
214 if (slave
->slave_class
== master
->slave_class
&&
215 slave
->slave_id
== master
->slave_id
) {
216 list_move_tail(&slave
->open_list
, &master
->slave_list_head
);
217 spin_lock_irq(&slave_active_lock
);
218 spin_lock(&master
->timer
->lock
);
219 slave
->master
= master
;
220 slave
->timer
= master
->timer
;
221 if (slave
->flags
& SNDRV_TIMER_IFLG_RUNNING
)
222 list_add_tail(&slave
->active_list
,
223 &master
->slave_active_head
);
224 spin_unlock(&master
->timer
->lock
);
225 spin_unlock_irq(&slave_active_lock
);
231 * open a timer instance
232 * when opening a master, the slave id must be here given.
234 int snd_timer_open(struct snd_timer_instance
**ti
,
235 char *owner
, struct snd_timer_id
*tid
,
236 unsigned int slave_id
)
238 struct snd_timer
*timer
;
239 struct snd_timer_instance
*timeri
= NULL
;
241 if (tid
->dev_class
== SNDRV_TIMER_CLASS_SLAVE
) {
242 /* open a slave instance */
243 if (tid
->dev_sclass
<= SNDRV_TIMER_SCLASS_NONE
||
244 tid
->dev_sclass
> SNDRV_TIMER_SCLASS_OSS_SEQUENCER
) {
245 snd_printd("invalid slave class %i\n", tid
->dev_sclass
);
248 mutex_lock(®ister_mutex
);
249 timeri
= snd_timer_instance_new(owner
, NULL
);
251 mutex_unlock(®ister_mutex
);
254 timeri
->slave_class
= tid
->dev_sclass
;
255 timeri
->slave_id
= tid
->device
;
256 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
257 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
258 snd_timer_check_slave(timeri
);
259 mutex_unlock(®ister_mutex
);
264 /* open a master instance */
265 mutex_lock(®ister_mutex
);
266 timer
= snd_timer_find(tid
);
267 #ifdef CONFIG_MODULES
269 mutex_unlock(®ister_mutex
);
270 snd_timer_request(tid
);
271 mutex_lock(®ister_mutex
);
272 timer
= snd_timer_find(tid
);
276 mutex_unlock(®ister_mutex
);
279 if (!list_empty(&timer
->open_list_head
)) {
280 timeri
= list_entry(timer
->open_list_head
.next
,
281 struct snd_timer_instance
, open_list
);
282 if (timeri
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
283 mutex_unlock(®ister_mutex
);
287 timeri
= snd_timer_instance_new(owner
, timer
);
289 mutex_unlock(®ister_mutex
);
292 timeri
->slave_class
= tid
->dev_sclass
;
293 timeri
->slave_id
= slave_id
;
294 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
)
295 timer
->hw
.open(timer
);
296 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
297 snd_timer_check_master(timeri
);
298 mutex_unlock(®ister_mutex
);
303 static int _snd_timer_stop(struct snd_timer_instance
*timeri
, int event
);
306 * close a timer instance
308 int snd_timer_close(struct snd_timer_instance
*timeri
)
310 struct snd_timer
*timer
= NULL
;
311 struct snd_timer_instance
*slave
, *tmp
;
313 if (snd_BUG_ON(!timeri
))
316 /* force to stop the timer */
317 snd_timer_stop(timeri
);
319 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
320 /* wait, until the active callback is finished */
321 spin_lock_irq(&slave_active_lock
);
322 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
323 spin_unlock_irq(&slave_active_lock
);
325 spin_lock_irq(&slave_active_lock
);
327 spin_unlock_irq(&slave_active_lock
);
328 mutex_lock(®ister_mutex
);
329 list_del(&timeri
->open_list
);
330 mutex_unlock(®ister_mutex
);
332 timer
= timeri
->timer
;
333 if (snd_BUG_ON(!timer
))
335 /* wait, until the active callback is finished */
336 spin_lock_irq(&timer
->lock
);
337 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
338 spin_unlock_irq(&timer
->lock
);
340 spin_lock_irq(&timer
->lock
);
342 spin_unlock_irq(&timer
->lock
);
343 mutex_lock(®ister_mutex
);
344 list_del(&timeri
->open_list
);
345 if (list_empty(&timer
->open_list_head
) &&
347 timer
->hw
.close(timer
);
348 /* remove slave links */
349 spin_lock_irq(&slave_active_lock
);
350 spin_lock(&timer
->lock
);
351 list_for_each_entry_safe(slave
, tmp
, &timeri
->slave_list_head
,
353 list_move_tail(&slave
->open_list
, &snd_timer_slave_list
);
354 slave
->master
= NULL
;
356 list_del_init(&slave
->ack_list
);
357 list_del_init(&slave
->active_list
);
359 spin_unlock(&timer
->lock
);
360 spin_unlock_irq(&slave_active_lock
);
361 mutex_unlock(®ister_mutex
);
364 if (timeri
->private_free
)
365 timeri
->private_free(timeri
);
366 kfree(timeri
->owner
);
369 module_put(timer
->module
);
373 unsigned long snd_timer_resolution(struct snd_timer_instance
*timeri
)
375 struct snd_timer
* timer
;
379 if ((timer
= timeri
->timer
) != NULL
) {
380 if (timer
->hw
.c_resolution
)
381 return timer
->hw
.c_resolution(timer
);
382 return timer
->hw
.resolution
;
387 static void snd_timer_notify1(struct snd_timer_instance
*ti
, int event
)
389 struct snd_timer
*timer
;
391 unsigned long resolution
= 0;
392 struct snd_timer_instance
*ts
;
393 struct timespec tstamp
;
395 if (timer_tstamp_monotonic
)
396 do_posix_clock_monotonic_gettime(&tstamp
);
398 getnstimeofday(&tstamp
);
399 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_START
||
400 event
> SNDRV_TIMER_EVENT_PAUSE
))
402 if (event
== SNDRV_TIMER_EVENT_START
||
403 event
== SNDRV_TIMER_EVENT_CONTINUE
)
404 resolution
= snd_timer_resolution(ti
);
406 ti
->ccallback(ti
, event
, &tstamp
, resolution
);
407 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
412 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
414 spin_lock_irqsave(&timer
->lock
, flags
);
415 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
417 ts
->ccallback(ts
, event
+ 100, &tstamp
, resolution
);
418 spin_unlock_irqrestore(&timer
->lock
, flags
);
421 static int snd_timer_start1(struct snd_timer
*timer
, struct snd_timer_instance
*timeri
,
422 unsigned long sticks
)
424 list_move_tail(&timeri
->active_list
, &timer
->active_list_head
);
425 if (timer
->running
) {
426 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
428 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
429 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
430 return 1; /* delayed start */
432 timer
->sticks
= sticks
;
433 timer
->hw
.start(timer
);
436 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
441 static int snd_timer_start_slave(struct snd_timer_instance
*timeri
)
445 spin_lock_irqsave(&slave_active_lock
, flags
);
446 if (timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
447 spin_unlock_irqrestore(&slave_active_lock
, flags
);
450 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
451 if (timeri
->master
&& timeri
->timer
) {
452 spin_lock(&timeri
->timer
->lock
);
453 list_add_tail(&timeri
->active_list
,
454 &timeri
->master
->slave_active_head
);
455 spin_unlock(&timeri
->timer
->lock
);
457 spin_unlock_irqrestore(&slave_active_lock
, flags
);
458 return 1; /* delayed start */
462 * start the timer instance
464 int snd_timer_start(struct snd_timer_instance
*timeri
, unsigned int ticks
)
466 struct snd_timer
*timer
;
467 int result
= -EINVAL
;
470 if (timeri
== NULL
|| ticks
< 1)
472 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
473 result
= snd_timer_start_slave(timeri
);
475 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
478 timer
= timeri
->timer
;
481 spin_lock_irqsave(&timer
->lock
, flags
);
482 if (timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
483 SNDRV_TIMER_IFLG_START
)) {
487 timeri
->ticks
= timeri
->cticks
= ticks
;
489 result
= snd_timer_start1(timer
, timeri
, ticks
);
491 spin_unlock_irqrestore(&timer
->lock
, flags
);
493 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
497 static int _snd_timer_stop(struct snd_timer_instance
*timeri
, int event
)
499 struct snd_timer
*timer
;
502 if (snd_BUG_ON(!timeri
))
505 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
506 spin_lock_irqsave(&slave_active_lock
, flags
);
507 if (!(timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
)) {
508 spin_unlock_irqrestore(&slave_active_lock
, flags
);
512 spin_lock(&timeri
->timer
->lock
);
513 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
514 list_del_init(&timeri
->ack_list
);
515 list_del_init(&timeri
->active_list
);
517 spin_unlock(&timeri
->timer
->lock
);
518 spin_unlock_irqrestore(&slave_active_lock
, flags
);
521 timer
= timeri
->timer
;
524 spin_lock_irqsave(&timer
->lock
, flags
);
525 if (!(timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
526 SNDRV_TIMER_IFLG_START
))) {
527 spin_unlock_irqrestore(&timer
->lock
, flags
);
530 list_del_init(&timeri
->ack_list
);
531 list_del_init(&timeri
->active_list
);
532 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
533 !(--timer
->running
)) {
534 timer
->hw
.stop(timer
);
535 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
536 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
537 snd_timer_reschedule(timer
, 0);
538 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
539 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
540 timer
->hw
.start(timer
);
544 timeri
->flags
&= ~(SNDRV_TIMER_IFLG_RUNNING
| SNDRV_TIMER_IFLG_START
);
545 spin_unlock_irqrestore(&timer
->lock
, flags
);
547 if (event
!= SNDRV_TIMER_EVENT_RESOLUTION
)
548 snd_timer_notify1(timeri
, event
);
553 * stop the timer instance.
555 * do not call this from the timer callback!
557 int snd_timer_stop(struct snd_timer_instance
*timeri
)
559 struct snd_timer
*timer
;
563 err
= _snd_timer_stop(timeri
, SNDRV_TIMER_EVENT_STOP
);
566 timer
= timeri
->timer
;
569 spin_lock_irqsave(&timer
->lock
, flags
);
570 timeri
->cticks
= timeri
->ticks
;
572 spin_unlock_irqrestore(&timer
->lock
, flags
);
577 * start again.. the tick is kept.
579 int snd_timer_continue(struct snd_timer_instance
*timeri
)
581 struct snd_timer
*timer
;
582 int result
= -EINVAL
;
587 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
588 return snd_timer_start_slave(timeri
);
589 timer
= timeri
->timer
;
592 spin_lock_irqsave(&timer
->lock
, flags
);
593 if (timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
600 result
= snd_timer_start1(timer
, timeri
, timer
->sticks
);
602 spin_unlock_irqrestore(&timer
->lock
, flags
);
603 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_CONTINUE
);
608 * pause.. remember the ticks left
610 int snd_timer_pause(struct snd_timer_instance
* timeri
)
612 return _snd_timer_stop(timeri
, SNDRV_TIMER_EVENT_PAUSE
);
616 * reschedule the timer
618 * start pending instances and check the scheduling ticks.
619 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
621 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
)
623 struct snd_timer_instance
*ti
;
624 unsigned long ticks
= ~0UL;
626 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
627 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
628 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
629 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
632 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
633 if (ticks
> ti
->cticks
)
638 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
641 if (ticks
> timer
->hw
.ticks
)
642 ticks
= timer
->hw
.ticks
;
643 if (ticks_left
!= ticks
)
644 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
645 timer
->sticks
= ticks
;
652 static void snd_timer_tasklet(unsigned long arg
)
654 struct snd_timer
*timer
= (struct snd_timer
*) arg
;
655 struct snd_timer_instance
*ti
;
657 unsigned long resolution
, ticks
;
660 spin_lock_irqsave(&timer
->lock
, flags
);
661 /* now process all callbacks */
662 while (!list_empty(&timer
->sack_list_head
)) {
663 p
= timer
->sack_list_head
.next
; /* get first item */
664 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
666 /* remove from ack_list and make empty */
671 resolution
= ti
->resolution
;
673 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
674 spin_unlock(&timer
->lock
);
676 ti
->callback(ti
, resolution
, ticks
);
677 spin_lock(&timer
->lock
);
678 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
680 spin_unlock_irqrestore(&timer
->lock
, flags
);
686 * ticks_left is usually equal to timer->sticks.
689 void snd_timer_interrupt(struct snd_timer
* timer
, unsigned long ticks_left
)
691 struct snd_timer_instance
*ti
, *ts
, *tmp
;
692 unsigned long resolution
, ticks
;
693 struct list_head
*p
, *ack_list_head
;
700 spin_lock_irqsave(&timer
->lock
, flags
);
702 /* remember the current resolution */
703 if (timer
->hw
.c_resolution
)
704 resolution
= timer
->hw
.c_resolution(timer
);
706 resolution
= timer
->hw
.resolution
;
708 /* loop for all active instances
709 * Here we cannot use list_for_each_entry because the active_list of a
710 * processed instance is relinked to done_list_head before the callback
713 list_for_each_entry_safe(ti
, tmp
, &timer
->active_list_head
,
715 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
717 ti
->pticks
+= ticks_left
;
718 ti
->resolution
= resolution
;
719 if (ti
->cticks
< ticks_left
)
722 ti
->cticks
-= ticks_left
;
723 if (ti
->cticks
) /* not expired */
725 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
726 ti
->cticks
= ti
->ticks
;
728 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
730 list_del_init(&ti
->active_list
);
732 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
733 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
))
734 ack_list_head
= &timer
->ack_list_head
;
736 ack_list_head
= &timer
->sack_list_head
;
737 if (list_empty(&ti
->ack_list
))
738 list_add_tail(&ti
->ack_list
, ack_list_head
);
739 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
) {
740 ts
->pticks
= ti
->pticks
;
741 ts
->resolution
= resolution
;
742 if (list_empty(&ts
->ack_list
))
743 list_add_tail(&ts
->ack_list
, ack_list_head
);
746 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
747 snd_timer_reschedule(timer
, timer
->sticks
);
748 if (timer
->running
) {
749 if (timer
->hw
.flags
& SNDRV_TIMER_HW_STOP
) {
750 timer
->hw
.stop(timer
);
751 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
753 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_AUTO
) ||
754 (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
)) {
756 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
757 timer
->hw
.start(timer
);
760 timer
->hw
.stop(timer
);
763 /* now process all fast callbacks */
764 while (!list_empty(&timer
->ack_list_head
)) {
765 p
= timer
->ack_list_head
.next
; /* get first item */
766 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
768 /* remove from ack_list and make empty */
774 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
775 spin_unlock(&timer
->lock
);
777 ti
->callback(ti
, resolution
, ticks
);
778 spin_lock(&timer
->lock
);
779 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
782 /* do we have any slow callbacks? */
783 use_tasklet
= !list_empty(&timer
->sack_list_head
);
784 spin_unlock_irqrestore(&timer
->lock
, flags
);
787 tasklet_schedule(&timer
->task_queue
);
794 int snd_timer_new(struct snd_card
*card
, char *id
, struct snd_timer_id
*tid
,
795 struct snd_timer
**rtimer
)
797 struct snd_timer
*timer
;
799 static struct snd_device_ops ops
= {
800 .dev_free
= snd_timer_dev_free
,
801 .dev_register
= snd_timer_dev_register
,
802 .dev_disconnect
= snd_timer_dev_disconnect
,
805 if (snd_BUG_ON(!tid
))
809 timer
= kzalloc(sizeof(*timer
), GFP_KERNEL
);
811 snd_printk(KERN_ERR
"timer: cannot allocate\n");
814 timer
->tmr_class
= tid
->dev_class
;
816 timer
->tmr_device
= tid
->device
;
817 timer
->tmr_subdevice
= tid
->subdevice
;
819 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
820 INIT_LIST_HEAD(&timer
->device_list
);
821 INIT_LIST_HEAD(&timer
->open_list_head
);
822 INIT_LIST_HEAD(&timer
->active_list_head
);
823 INIT_LIST_HEAD(&timer
->ack_list_head
);
824 INIT_LIST_HEAD(&timer
->sack_list_head
);
825 spin_lock_init(&timer
->lock
);
826 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
,
827 (unsigned long)timer
);
829 timer
->module
= card
->module
;
830 err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
);
832 snd_timer_free(timer
);
841 static int snd_timer_free(struct snd_timer
*timer
)
846 mutex_lock(®ister_mutex
);
847 if (! list_empty(&timer
->open_list_head
)) {
848 struct list_head
*p
, *n
;
849 struct snd_timer_instance
*ti
;
850 snd_printk(KERN_WARNING
"timer %p is busy?\n", timer
);
851 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
853 ti
= list_entry(p
, struct snd_timer_instance
, open_list
);
857 list_del(&timer
->device_list
);
858 mutex_unlock(®ister_mutex
);
860 if (timer
->private_free
)
861 timer
->private_free(timer
);
866 static int snd_timer_dev_free(struct snd_device
*device
)
868 struct snd_timer
*timer
= device
->device_data
;
869 return snd_timer_free(timer
);
872 static int snd_timer_dev_register(struct snd_device
*dev
)
874 struct snd_timer
*timer
= dev
->device_data
;
875 struct snd_timer
*timer1
;
877 if (snd_BUG_ON(!timer
|| !timer
->hw
.start
|| !timer
->hw
.stop
))
879 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
880 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
883 mutex_lock(®ister_mutex
);
884 list_for_each_entry(timer1
, &snd_timer_list
, device_list
) {
885 if (timer1
->tmr_class
> timer
->tmr_class
)
887 if (timer1
->tmr_class
< timer
->tmr_class
)
889 if (timer1
->card
&& timer
->card
) {
890 if (timer1
->card
->number
> timer
->card
->number
)
892 if (timer1
->card
->number
< timer
->card
->number
)
895 if (timer1
->tmr_device
> timer
->tmr_device
)
897 if (timer1
->tmr_device
< timer
->tmr_device
)
899 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
901 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
904 mutex_unlock(®ister_mutex
);
907 list_add_tail(&timer
->device_list
, &timer1
->device_list
);
908 mutex_unlock(®ister_mutex
);
912 static int snd_timer_dev_disconnect(struct snd_device
*device
)
914 struct snd_timer
*timer
= device
->device_data
;
915 mutex_lock(®ister_mutex
);
916 list_del_init(&timer
->device_list
);
917 mutex_unlock(®ister_mutex
);
921 void snd_timer_notify(struct snd_timer
*timer
, int event
, struct timespec
*tstamp
)
924 unsigned long resolution
= 0;
925 struct snd_timer_instance
*ti
, *ts
;
927 if (! (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
))
929 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_MSTART
||
930 event
> SNDRV_TIMER_EVENT_MRESUME
))
932 spin_lock_irqsave(&timer
->lock
, flags
);
933 if (event
== SNDRV_TIMER_EVENT_MSTART
||
934 event
== SNDRV_TIMER_EVENT_MCONTINUE
||
935 event
== SNDRV_TIMER_EVENT_MRESUME
) {
936 if (timer
->hw
.c_resolution
)
937 resolution
= timer
->hw
.c_resolution(timer
);
939 resolution
= timer
->hw
.resolution
;
941 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
943 ti
->ccallback(ti
, event
, tstamp
, resolution
);
944 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
946 ts
->ccallback(ts
, event
, tstamp
, resolution
);
948 spin_unlock_irqrestore(&timer
->lock
, flags
);
952 * exported functions for global timers
954 int snd_timer_global_new(char *id
, int device
, struct snd_timer
**rtimer
)
956 struct snd_timer_id tid
;
958 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
959 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
963 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
966 int snd_timer_global_free(struct snd_timer
*timer
)
968 return snd_timer_free(timer
);
971 int snd_timer_global_register(struct snd_timer
*timer
)
973 struct snd_device dev
;
975 memset(&dev
, 0, sizeof(dev
));
976 dev
.device_data
= timer
;
977 return snd_timer_dev_register(&dev
);
984 struct snd_timer_system_private
{
985 struct timer_list tlist
;
986 unsigned long last_expires
;
987 unsigned long last_jiffies
;
988 unsigned long correction
;
991 static void snd_timer_s_function(unsigned long data
)
993 struct snd_timer
*timer
= (struct snd_timer
*)data
;
994 struct snd_timer_system_private
*priv
= timer
->private_data
;
995 unsigned long jiff
= jiffies
;
996 if (time_after(jiff
, priv
->last_expires
))
997 priv
->correction
+= (long)jiff
- (long)priv
->last_expires
;
998 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
1001 static int snd_timer_s_start(struct snd_timer
* timer
)
1003 struct snd_timer_system_private
*priv
;
1004 unsigned long njiff
;
1006 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1007 njiff
= (priv
->last_jiffies
= jiffies
);
1008 if (priv
->correction
> timer
->sticks
- 1) {
1009 priv
->correction
-= timer
->sticks
- 1;
1012 njiff
+= timer
->sticks
- priv
->correction
;
1013 priv
->correction
= 0;
1015 priv
->last_expires
= priv
->tlist
.expires
= njiff
;
1016 add_timer(&priv
->tlist
);
1020 static int snd_timer_s_stop(struct snd_timer
* timer
)
1022 struct snd_timer_system_private
*priv
;
1025 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1026 del_timer(&priv
->tlist
);
1028 if (time_before(jiff
, priv
->last_expires
))
1029 timer
->sticks
= priv
->last_expires
- jiff
;
1032 priv
->correction
= 0;
1036 static struct snd_timer_hardware snd_timer_system
=
1038 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
1039 .resolution
= 1000000000L / HZ
,
1041 .start
= snd_timer_s_start
,
1042 .stop
= snd_timer_s_stop
1045 static void snd_timer_free_system(struct snd_timer
*timer
)
1047 kfree(timer
->private_data
);
1050 static int snd_timer_register_system(void)
1052 struct snd_timer
*timer
;
1053 struct snd_timer_system_private
*priv
;
1056 err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
);
1059 strcpy(timer
->name
, "system timer");
1060 timer
->hw
= snd_timer_system
;
1061 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1063 snd_timer_free(timer
);
1066 init_timer(&priv
->tlist
);
1067 priv
->tlist
.function
= snd_timer_s_function
;
1068 priv
->tlist
.data
= (unsigned long) timer
;
1069 timer
->private_data
= priv
;
1070 timer
->private_free
= snd_timer_free_system
;
1071 return snd_timer_global_register(timer
);
1074 #ifdef CONFIG_PROC_FS
1079 static void snd_timer_proc_read(struct snd_info_entry
*entry
,
1080 struct snd_info_buffer
*buffer
)
1082 struct snd_timer
*timer
;
1083 struct snd_timer_instance
*ti
;
1085 mutex_lock(®ister_mutex
);
1086 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
1087 switch (timer
->tmr_class
) {
1088 case SNDRV_TIMER_CLASS_GLOBAL
:
1089 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1091 case SNDRV_TIMER_CLASS_CARD
:
1092 snd_iprintf(buffer
, "C%i-%i: ",
1093 timer
->card
->number
, timer
->tmr_device
);
1095 case SNDRV_TIMER_CLASS_PCM
:
1096 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
,
1097 timer
->tmr_device
, timer
->tmr_subdevice
);
1100 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
,
1101 timer
->card
? timer
->card
->number
: -1,
1102 timer
->tmr_device
, timer
->tmr_subdevice
);
1104 snd_iprintf(buffer
, "%s :", timer
->name
);
1105 if (timer
->hw
.resolution
)
1106 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)",
1107 timer
->hw
.resolution
/ 1000,
1108 timer
->hw
.resolution
% 1000,
1110 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1111 snd_iprintf(buffer
, " SLAVE");
1112 snd_iprintf(buffer
, "\n");
1113 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
)
1114 snd_iprintf(buffer
, " Client %s : %s\n",
1115 ti
->owner
? ti
->owner
: "unknown",
1116 ti
->flags
& (SNDRV_TIMER_IFLG_START
|
1117 SNDRV_TIMER_IFLG_RUNNING
)
1118 ? "running" : "stopped");
1120 mutex_unlock(®ister_mutex
);
1123 static struct snd_info_entry
*snd_timer_proc_entry
;
1125 static void __init
snd_timer_proc_init(void)
1127 struct snd_info_entry
*entry
;
1129 entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
);
1130 if (entry
!= NULL
) {
1131 entry
->c
.text
.read
= snd_timer_proc_read
;
1132 if (snd_info_register(entry
) < 0) {
1133 snd_info_free_entry(entry
);
1137 snd_timer_proc_entry
= entry
;
1140 static void __exit
snd_timer_proc_done(void)
1142 snd_info_free_entry(snd_timer_proc_entry
);
1144 #else /* !CONFIG_PROC_FS */
1145 #define snd_timer_proc_init()
1146 #define snd_timer_proc_done()
1150 * USER SPACE interface
1153 static void snd_timer_user_interrupt(struct snd_timer_instance
*timeri
,
1154 unsigned long resolution
,
1155 unsigned long ticks
)
1157 struct snd_timer_user
*tu
= timeri
->callback_data
;
1158 struct snd_timer_read
*r
;
1161 spin_lock(&tu
->qlock
);
1162 if (tu
->qused
> 0) {
1163 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1164 r
= &tu
->queue
[prev
];
1165 if (r
->resolution
== resolution
) {
1170 if (tu
->qused
>= tu
->queue_size
) {
1173 r
= &tu
->queue
[tu
->qtail
++];
1174 tu
->qtail
%= tu
->queue_size
;
1175 r
->resolution
= resolution
;
1180 spin_unlock(&tu
->qlock
);
1181 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1182 wake_up(&tu
->qchange_sleep
);
1185 static void snd_timer_user_append_to_tqueue(struct snd_timer_user
*tu
,
1186 struct snd_timer_tread
*tread
)
1188 if (tu
->qused
>= tu
->queue_size
) {
1191 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1192 tu
->qtail
%= tu
->queue_size
;
1197 static void snd_timer_user_ccallback(struct snd_timer_instance
*timeri
,
1199 struct timespec
*tstamp
,
1200 unsigned long resolution
)
1202 struct snd_timer_user
*tu
= timeri
->callback_data
;
1203 struct snd_timer_tread r1
;
1204 unsigned long flags
;
1206 if (event
>= SNDRV_TIMER_EVENT_START
&&
1207 event
<= SNDRV_TIMER_EVENT_PAUSE
)
1208 tu
->tstamp
= *tstamp
;
1209 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1211 memset(&r1
, 0, sizeof(r1
));
1213 r1
.tstamp
= *tstamp
;
1214 r1
.val
= resolution
;
1215 spin_lock_irqsave(&tu
->qlock
, flags
);
1216 snd_timer_user_append_to_tqueue(tu
, &r1
);
1217 spin_unlock_irqrestore(&tu
->qlock
, flags
);
1218 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1219 wake_up(&tu
->qchange_sleep
);
1222 static void snd_timer_user_tinterrupt(struct snd_timer_instance
*timeri
,
1223 unsigned long resolution
,
1224 unsigned long ticks
)
1226 struct snd_timer_user
*tu
= timeri
->callback_data
;
1227 struct snd_timer_tread
*r
, r1
;
1228 struct timespec tstamp
;
1229 int prev
, append
= 0;
1231 memset(&tstamp
, 0, sizeof(tstamp
));
1232 spin_lock(&tu
->qlock
);
1233 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
) |
1234 (1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1235 spin_unlock(&tu
->qlock
);
1238 if (tu
->last_resolution
!= resolution
|| ticks
> 0) {
1239 if (timer_tstamp_monotonic
)
1240 do_posix_clock_monotonic_gettime(&tstamp
);
1242 getnstimeofday(&tstamp
);
1244 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) &&
1245 tu
->last_resolution
!= resolution
) {
1246 memset(&r1
, 0, sizeof(r1
));
1247 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1249 r1
.val
= resolution
;
1250 snd_timer_user_append_to_tqueue(tu
, &r1
);
1251 tu
->last_resolution
= resolution
;
1254 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1258 if (tu
->qused
> 0) {
1259 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1260 r
= &tu
->tqueue
[prev
];
1261 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1268 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1271 snd_timer_user_append_to_tqueue(tu
, &r1
);
1274 spin_unlock(&tu
->qlock
);
1277 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1278 wake_up(&tu
->qchange_sleep
);
1281 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1283 struct snd_timer_user
*tu
;
1286 err
= nonseekable_open(inode
, file
);
1290 tu
= kzalloc(sizeof(*tu
), GFP_KERNEL
);
1293 spin_lock_init(&tu
->qlock
);
1294 init_waitqueue_head(&tu
->qchange_sleep
);
1295 mutex_init(&tu
->ioctl_lock
);
1297 tu
->queue_size
= 128;
1298 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1300 if (tu
->queue
== NULL
) {
1304 file
->private_data
= tu
;
1308 static int snd_timer_user_release(struct inode
*inode
, struct file
*file
)
1310 struct snd_timer_user
*tu
;
1312 if (file
->private_data
) {
1313 tu
= file
->private_data
;
1314 file
->private_data
= NULL
;
1315 mutex_lock(&tu
->ioctl_lock
);
1317 snd_timer_close(tu
->timeri
);
1318 mutex_unlock(&tu
->ioctl_lock
);
1326 static void snd_timer_user_zero_id(struct snd_timer_id
*id
)
1328 id
->dev_class
= SNDRV_TIMER_CLASS_NONE
;
1329 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1335 static void snd_timer_user_copy_id(struct snd_timer_id
*id
, struct snd_timer
*timer
)
1337 id
->dev_class
= timer
->tmr_class
;
1338 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1339 id
->card
= timer
->card
? timer
->card
->number
: -1;
1340 id
->device
= timer
->tmr_device
;
1341 id
->subdevice
= timer
->tmr_subdevice
;
1344 static int snd_timer_user_next_device(struct snd_timer_id __user
*_tid
)
1346 struct snd_timer_id id
;
1347 struct snd_timer
*timer
;
1348 struct list_head
*p
;
1350 if (copy_from_user(&id
, _tid
, sizeof(id
)))
1352 mutex_lock(®ister_mutex
);
1353 if (id
.dev_class
< 0) { /* first item */
1354 if (list_empty(&snd_timer_list
))
1355 snd_timer_user_zero_id(&id
);
1357 timer
= list_entry(snd_timer_list
.next
,
1358 struct snd_timer
, device_list
);
1359 snd_timer_user_copy_id(&id
, timer
);
1362 switch (id
.dev_class
) {
1363 case SNDRV_TIMER_CLASS_GLOBAL
:
1364 id
.device
= id
.device
< 0 ? 0 : id
.device
+ 1;
1365 list_for_each(p
, &snd_timer_list
) {
1366 timer
= list_entry(p
, struct snd_timer
, device_list
);
1367 if (timer
->tmr_class
> SNDRV_TIMER_CLASS_GLOBAL
) {
1368 snd_timer_user_copy_id(&id
, timer
);
1371 if (timer
->tmr_device
>= id
.device
) {
1372 snd_timer_user_copy_id(&id
, timer
);
1376 if (p
== &snd_timer_list
)
1377 snd_timer_user_zero_id(&id
);
1379 case SNDRV_TIMER_CLASS_CARD
:
1380 case SNDRV_TIMER_CLASS_PCM
:
1387 if (id
.device
< 0) {
1390 if (id
.subdevice
< 0) {
1398 list_for_each(p
, &snd_timer_list
) {
1399 timer
= list_entry(p
, struct snd_timer
, device_list
);
1400 if (timer
->tmr_class
> id
.dev_class
) {
1401 snd_timer_user_copy_id(&id
, timer
);
1404 if (timer
->tmr_class
< id
.dev_class
)
1406 if (timer
->card
->number
> id
.card
) {
1407 snd_timer_user_copy_id(&id
, timer
);
1410 if (timer
->card
->number
< id
.card
)
1412 if (timer
->tmr_device
> id
.device
) {
1413 snd_timer_user_copy_id(&id
, timer
);
1416 if (timer
->tmr_device
< id
.device
)
1418 if (timer
->tmr_subdevice
> id
.subdevice
) {
1419 snd_timer_user_copy_id(&id
, timer
);
1422 if (timer
->tmr_subdevice
< id
.subdevice
)
1424 snd_timer_user_copy_id(&id
, timer
);
1427 if (p
== &snd_timer_list
)
1428 snd_timer_user_zero_id(&id
);
1431 snd_timer_user_zero_id(&id
);
1434 mutex_unlock(®ister_mutex
);
1435 if (copy_to_user(_tid
, &id
, sizeof(*_tid
)))
1440 static int snd_timer_user_ginfo(struct file
*file
,
1441 struct snd_timer_ginfo __user
*_ginfo
)
1443 struct snd_timer_ginfo
*ginfo
;
1444 struct snd_timer_id tid
;
1445 struct snd_timer
*t
;
1446 struct list_head
*p
;
1449 ginfo
= memdup_user(_ginfo
, sizeof(*ginfo
));
1451 return PTR_ERR(ginfo
);
1454 memset(ginfo
, 0, sizeof(*ginfo
));
1456 mutex_lock(®ister_mutex
);
1457 t
= snd_timer_find(&tid
);
1459 ginfo
->card
= t
->card
? t
->card
->number
: -1;
1460 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1461 ginfo
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1462 strlcpy(ginfo
->id
, t
->id
, sizeof(ginfo
->id
));
1463 strlcpy(ginfo
->name
, t
->name
, sizeof(ginfo
->name
));
1464 ginfo
->resolution
= t
->hw
.resolution
;
1465 if (t
->hw
.resolution_min
> 0) {
1466 ginfo
->resolution_min
= t
->hw
.resolution_min
;
1467 ginfo
->resolution_max
= t
->hw
.resolution_max
;
1469 list_for_each(p
, &t
->open_list_head
) {
1475 mutex_unlock(®ister_mutex
);
1476 if (err
>= 0 && copy_to_user(_ginfo
, ginfo
, sizeof(*ginfo
)))
1482 static int snd_timer_user_gparams(struct file
*file
,
1483 struct snd_timer_gparams __user
*_gparams
)
1485 struct snd_timer_gparams gparams
;
1486 struct snd_timer
*t
;
1489 if (copy_from_user(&gparams
, _gparams
, sizeof(gparams
)))
1491 mutex_lock(®ister_mutex
);
1492 t
= snd_timer_find(&gparams
.tid
);
1497 if (!list_empty(&t
->open_list_head
)) {
1501 if (!t
->hw
.set_period
) {
1505 err
= t
->hw
.set_period(t
, gparams
.period_num
, gparams
.period_den
);
1507 mutex_unlock(®ister_mutex
);
1511 static int snd_timer_user_gstatus(struct file
*file
,
1512 struct snd_timer_gstatus __user
*_gstatus
)
1514 struct snd_timer_gstatus gstatus
;
1515 struct snd_timer_id tid
;
1516 struct snd_timer
*t
;
1519 if (copy_from_user(&gstatus
, _gstatus
, sizeof(gstatus
)))
1522 memset(&gstatus
, 0, sizeof(gstatus
));
1524 mutex_lock(®ister_mutex
);
1525 t
= snd_timer_find(&tid
);
1527 if (t
->hw
.c_resolution
)
1528 gstatus
.resolution
= t
->hw
.c_resolution(t
);
1530 gstatus
.resolution
= t
->hw
.resolution
;
1531 if (t
->hw
.precise_resolution
) {
1532 t
->hw
.precise_resolution(t
, &gstatus
.resolution_num
,
1533 &gstatus
.resolution_den
);
1535 gstatus
.resolution_num
= gstatus
.resolution
;
1536 gstatus
.resolution_den
= 1000000000uL;
1541 mutex_unlock(®ister_mutex
);
1542 if (err
>= 0 && copy_to_user(_gstatus
, &gstatus
, sizeof(gstatus
)))
1547 static int snd_timer_user_tselect(struct file
*file
,
1548 struct snd_timer_select __user
*_tselect
)
1550 struct snd_timer_user
*tu
;
1551 struct snd_timer_select tselect
;
1555 tu
= file
->private_data
;
1557 snd_timer_close(tu
->timeri
);
1560 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
))) {
1564 sprintf(str
, "application %i", current
->pid
);
1565 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1566 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1567 err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
);
1576 tu
->tqueue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_tread
),
1578 if (tu
->tqueue
== NULL
)
1581 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1583 if (tu
->queue
== NULL
)
1588 snd_timer_close(tu
->timeri
);
1591 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1592 tu
->timeri
->callback
= tu
->tread
1593 ? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1594 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1595 tu
->timeri
->callback_data
= (void *)tu
;
1602 static int snd_timer_user_info(struct file
*file
,
1603 struct snd_timer_info __user
*_info
)
1605 struct snd_timer_user
*tu
;
1606 struct snd_timer_info
*info
;
1607 struct snd_timer
*t
;
1610 tu
= file
->private_data
;
1613 t
= tu
->timeri
->timer
;
1617 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1620 info
->card
= t
->card
? t
->card
->number
: -1;
1621 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1622 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1623 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1624 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1625 info
->resolution
= t
->hw
.resolution
;
1626 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1632 static int snd_timer_user_params(struct file
*file
,
1633 struct snd_timer_params __user
*_params
)
1635 struct snd_timer_user
*tu
;
1636 struct snd_timer_params params
;
1637 struct snd_timer
*t
;
1638 struct snd_timer_read
*tr
;
1639 struct snd_timer_tread
*ttr
;
1642 tu
= file
->private_data
;
1645 t
= tu
->timeri
->timer
;
1648 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
1650 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) && params
.ticks
< 1) {
1654 if (params
.queue_size
> 0 &&
1655 (params
.queue_size
< 32 || params
.queue_size
> 1024)) {
1659 if (params
.filter
& ~((1<<SNDRV_TIMER_EVENT_RESOLUTION
)|
1660 (1<<SNDRV_TIMER_EVENT_TICK
)|
1661 (1<<SNDRV_TIMER_EVENT_START
)|
1662 (1<<SNDRV_TIMER_EVENT_STOP
)|
1663 (1<<SNDRV_TIMER_EVENT_CONTINUE
)|
1664 (1<<SNDRV_TIMER_EVENT_PAUSE
)|
1665 (1<<SNDRV_TIMER_EVENT_SUSPEND
)|
1666 (1<<SNDRV_TIMER_EVENT_RESUME
)|
1667 (1<<SNDRV_TIMER_EVENT_MSTART
)|
1668 (1<<SNDRV_TIMER_EVENT_MSTOP
)|
1669 (1<<SNDRV_TIMER_EVENT_MCONTINUE
)|
1670 (1<<SNDRV_TIMER_EVENT_MPAUSE
)|
1671 (1<<SNDRV_TIMER_EVENT_MSUSPEND
)|
1672 (1<<SNDRV_TIMER_EVENT_MRESUME
))) {
1676 snd_timer_stop(tu
->timeri
);
1677 spin_lock_irq(&t
->lock
);
1678 tu
->timeri
->flags
&= ~(SNDRV_TIMER_IFLG_AUTO
|
1679 SNDRV_TIMER_IFLG_EXCLUSIVE
|
1680 SNDRV_TIMER_IFLG_EARLY_EVENT
);
1681 if (params
.flags
& SNDRV_TIMER_PSFLG_AUTO
)
1682 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
1683 if (params
.flags
& SNDRV_TIMER_PSFLG_EXCLUSIVE
)
1684 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EXCLUSIVE
;
1685 if (params
.flags
& SNDRV_TIMER_PSFLG_EARLY_EVENT
)
1686 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EARLY_EVENT
;
1687 spin_unlock_irq(&t
->lock
);
1688 if (params
.queue_size
> 0 &&
1689 (unsigned int)tu
->queue_size
!= params
.queue_size
) {
1691 ttr
= kmalloc(params
.queue_size
* sizeof(*ttr
),
1695 tu
->queue_size
= params
.queue_size
;
1699 tr
= kmalloc(params
.queue_size
* sizeof(*tr
),
1703 tu
->queue_size
= params
.queue_size
;
1708 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1709 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1711 struct snd_timer_tread tread
;
1712 memset(&tread
, 0, sizeof(tread
));
1713 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1714 tread
.tstamp
.tv_sec
= 0;
1715 tread
.tstamp
.tv_nsec
= 0;
1717 snd_timer_user_append_to_tqueue(tu
, &tread
);
1719 struct snd_timer_read
*r
= &tu
->queue
[0];
1726 tu
->filter
= params
.filter
;
1727 tu
->ticks
= params
.ticks
;
1730 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1735 static int snd_timer_user_status(struct file
*file
,
1736 struct snd_timer_status __user
*_status
)
1738 struct snd_timer_user
*tu
;
1739 struct snd_timer_status status
;
1741 tu
= file
->private_data
;
1744 memset(&status
, 0, sizeof(status
));
1745 status
.tstamp
= tu
->tstamp
;
1746 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1747 status
.lost
= tu
->timeri
->lost
;
1748 status
.overrun
= tu
->overrun
;
1749 spin_lock_irq(&tu
->qlock
);
1750 status
.queue
= tu
->qused
;
1751 spin_unlock_irq(&tu
->qlock
);
1752 if (copy_to_user(_status
, &status
, sizeof(status
)))
1757 static int snd_timer_user_start(struct file
*file
)
1760 struct snd_timer_user
*tu
;
1762 tu
= file
->private_data
;
1765 snd_timer_stop(tu
->timeri
);
1766 tu
->timeri
->lost
= 0;
1767 tu
->last_resolution
= 0;
1768 return (err
= snd_timer_start(tu
->timeri
, tu
->ticks
)) < 0 ? err
: 0;
1771 static int snd_timer_user_stop(struct file
*file
)
1774 struct snd_timer_user
*tu
;
1776 tu
= file
->private_data
;
1779 return (err
= snd_timer_stop(tu
->timeri
)) < 0 ? err
: 0;
1782 static int snd_timer_user_continue(struct file
*file
)
1785 struct snd_timer_user
*tu
;
1787 tu
= file
->private_data
;
1790 tu
->timeri
->lost
= 0;
1791 return (err
= snd_timer_continue(tu
->timeri
)) < 0 ? err
: 0;
1794 static int snd_timer_user_pause(struct file
*file
)
1797 struct snd_timer_user
*tu
;
1799 tu
= file
->private_data
;
1802 return (err
= snd_timer_pause(tu
->timeri
)) < 0 ? err
: 0;
1806 SNDRV_TIMER_IOCTL_START_OLD
= _IO('T', 0x20),
1807 SNDRV_TIMER_IOCTL_STOP_OLD
= _IO('T', 0x21),
1808 SNDRV_TIMER_IOCTL_CONTINUE_OLD
= _IO('T', 0x22),
1809 SNDRV_TIMER_IOCTL_PAUSE_OLD
= _IO('T', 0x23),
1812 static long __snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1815 struct snd_timer_user
*tu
;
1816 void __user
*argp
= (void __user
*)arg
;
1817 int __user
*p
= argp
;
1819 tu
= file
->private_data
;
1821 case SNDRV_TIMER_IOCTL_PVERSION
:
1822 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1823 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1824 return snd_timer_user_next_device(argp
);
1825 case SNDRV_TIMER_IOCTL_TREAD
:
1829 if (tu
->timeri
) /* too late */
1831 if (get_user(xarg
, p
))
1833 tu
->tread
= xarg
? 1 : 0;
1836 case SNDRV_TIMER_IOCTL_GINFO
:
1837 return snd_timer_user_ginfo(file
, argp
);
1838 case SNDRV_TIMER_IOCTL_GPARAMS
:
1839 return snd_timer_user_gparams(file
, argp
);
1840 case SNDRV_TIMER_IOCTL_GSTATUS
:
1841 return snd_timer_user_gstatus(file
, argp
);
1842 case SNDRV_TIMER_IOCTL_SELECT
:
1843 return snd_timer_user_tselect(file
, argp
);
1844 case SNDRV_TIMER_IOCTL_INFO
:
1845 return snd_timer_user_info(file
, argp
);
1846 case SNDRV_TIMER_IOCTL_PARAMS
:
1847 return snd_timer_user_params(file
, argp
);
1848 case SNDRV_TIMER_IOCTL_STATUS
:
1849 return snd_timer_user_status(file
, argp
);
1850 case SNDRV_TIMER_IOCTL_START
:
1851 case SNDRV_TIMER_IOCTL_START_OLD
:
1852 return snd_timer_user_start(file
);
1853 case SNDRV_TIMER_IOCTL_STOP
:
1854 case SNDRV_TIMER_IOCTL_STOP_OLD
:
1855 return snd_timer_user_stop(file
);
1856 case SNDRV_TIMER_IOCTL_CONTINUE
:
1857 case SNDRV_TIMER_IOCTL_CONTINUE_OLD
:
1858 return snd_timer_user_continue(file
);
1859 case SNDRV_TIMER_IOCTL_PAUSE
:
1860 case SNDRV_TIMER_IOCTL_PAUSE_OLD
:
1861 return snd_timer_user_pause(file
);
1866 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1869 struct snd_timer_user
*tu
= file
->private_data
;
1872 mutex_lock(&tu
->ioctl_lock
);
1873 ret
= __snd_timer_user_ioctl(file
, cmd
, arg
);
1874 mutex_unlock(&tu
->ioctl_lock
);
1878 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
1880 struct snd_timer_user
*tu
;
1882 tu
= file
->private_data
;
1883 return fasync_helper(fd
, file
, on
, &tu
->fasync
);
1886 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
,
1887 size_t count
, loff_t
*offset
)
1889 struct snd_timer_user
*tu
;
1890 long result
= 0, unit
;
1893 tu
= file
->private_data
;
1894 unit
= tu
->tread
? sizeof(struct snd_timer_tread
) : sizeof(struct snd_timer_read
);
1895 spin_lock_irq(&tu
->qlock
);
1896 while ((long)count
- result
>= unit
) {
1897 while (!tu
->qused
) {
1900 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1905 set_current_state(TASK_INTERRUPTIBLE
);
1906 init_waitqueue_entry(&wait
, current
);
1907 add_wait_queue(&tu
->qchange_sleep
, &wait
);
1909 spin_unlock_irq(&tu
->qlock
);
1911 spin_lock_irq(&tu
->qlock
);
1913 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
1915 if (signal_pending(current
)) {
1921 spin_unlock_irq(&tu
->qlock
);
1926 if (copy_to_user(buffer
, &tu
->tqueue
[tu
->qhead
++],
1927 sizeof(struct snd_timer_tread
))) {
1932 if (copy_to_user(buffer
, &tu
->queue
[tu
->qhead
++],
1933 sizeof(struct snd_timer_read
))) {
1939 tu
->qhead
%= tu
->queue_size
;
1944 spin_lock_irq(&tu
->qlock
);
1947 spin_unlock_irq(&tu
->qlock
);
1949 return result
> 0 ? result
: err
;
1952 static unsigned int snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
1955 struct snd_timer_user
*tu
;
1957 tu
= file
->private_data
;
1959 poll_wait(file
, &tu
->qchange_sleep
, wait
);
1963 mask
|= POLLIN
| POLLRDNORM
;
1968 #ifdef CONFIG_COMPAT
1969 #include "timer_compat.c"
1971 #define snd_timer_user_ioctl_compat NULL
1974 static const struct file_operations snd_timer_f_ops
=
1976 .owner
= THIS_MODULE
,
1977 .read
= snd_timer_user_read
,
1978 .open
= snd_timer_user_open
,
1979 .release
= snd_timer_user_release
,
1980 .llseek
= no_llseek
,
1981 .poll
= snd_timer_user_poll
,
1982 .unlocked_ioctl
= snd_timer_user_ioctl
,
1983 .compat_ioctl
= snd_timer_user_ioctl_compat
,
1984 .fasync
= snd_timer_user_fasync
,
1991 static int __init
alsa_timer_init(void)
1995 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1996 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1,
2000 if ((err
= snd_timer_register_system()) < 0)
2001 snd_printk(KERN_ERR
"unable to register system timer (%i)\n",
2003 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0,
2004 &snd_timer_f_ops
, NULL
, "timer")) < 0)
2005 snd_printk(KERN_ERR
"unable to register timer device (%i)\n",
2007 snd_timer_proc_init();
2011 static void __exit
alsa_timer_exit(void)
2013 struct list_head
*p
, *n
;
2015 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0);
2016 /* unregister the system timer */
2017 list_for_each_safe(p
, n
, &snd_timer_list
) {
2018 struct snd_timer
*timer
= list_entry(p
, struct snd_timer
, device_list
);
2019 snd_timer_free(timer
);
2021 snd_timer_proc_done();
2022 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2023 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
2027 module_init(alsa_timer_init
)
2028 module_exit(alsa_timer_exit
)
2030 EXPORT_SYMBOL(snd_timer_open
);
2031 EXPORT_SYMBOL(snd_timer_close
);
2032 EXPORT_SYMBOL(snd_timer_resolution
);
2033 EXPORT_SYMBOL(snd_timer_start
);
2034 EXPORT_SYMBOL(snd_timer_stop
);
2035 EXPORT_SYMBOL(snd_timer_continue
);
2036 EXPORT_SYMBOL(snd_timer_pause
);
2037 EXPORT_SYMBOL(snd_timer_new
);
2038 EXPORT_SYMBOL(snd_timer_notify
);
2039 EXPORT_SYMBOL(snd_timer_global_new
);
2040 EXPORT_SYMBOL(snd_timer_global_free
);
2041 EXPORT_SYMBOL(snd_timer_global_register
);
2042 EXPORT_SYMBOL(snd_timer_interrupt
);