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>
39 #define SNDRV_TIMER_IFLG_PAUSED 0x00010000
41 #if IS_ENABLED(CONFIG_SND_HRTIMER)
42 #define DEFAULT_TIMER_LIMIT 4
44 #define DEFAULT_TIMER_LIMIT 1
47 static int timer_limit
= DEFAULT_TIMER_LIMIT
;
48 static int timer_tstamp_monotonic
= 1;
49 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
50 MODULE_DESCRIPTION("ALSA timer interface");
51 MODULE_LICENSE("GPL");
52 module_param(timer_limit
, int, 0444);
53 MODULE_PARM_DESC(timer_limit
, "Maximum global timers in system.");
54 module_param(timer_tstamp_monotonic
, int, 0444);
55 MODULE_PARM_DESC(timer_tstamp_monotonic
, "Use posix monotonic clock source for timestamps (default).");
57 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR
, SNDRV_MINOR_TIMER
);
58 MODULE_ALIAS("devname:snd/timer");
60 struct snd_timer_user
{
61 struct snd_timer_instance
*timeri
;
62 int tread
; /* enhanced read with timestamps and events */
64 unsigned long overrun
;
70 struct snd_timer_read
*queue
;
71 struct snd_timer_tread
*tqueue
;
73 unsigned long last_resolution
;
75 struct timespec tstamp
; /* trigger tstamp */
76 wait_queue_head_t qchange_sleep
;
77 struct fasync_struct
*fasync
;
78 struct mutex ioctl_lock
;
82 static LIST_HEAD(snd_timer_list
);
84 /* list of slave instances */
85 static LIST_HEAD(snd_timer_slave_list
);
87 /* lock for slave active lists */
88 static DEFINE_SPINLOCK(slave_active_lock
);
90 static DEFINE_MUTEX(register_mutex
);
92 static int snd_timer_free(struct snd_timer
*timer
);
93 static int snd_timer_dev_free(struct snd_device
*device
);
94 static int snd_timer_dev_register(struct snd_device
*device
);
95 static int snd_timer_dev_disconnect(struct snd_device
*device
);
97 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
);
100 * create a timer instance with the given owner string.
101 * when timer is not NULL, increments the module counter
103 static struct snd_timer_instance
*snd_timer_instance_new(char *owner
,
104 struct snd_timer
*timer
)
106 struct snd_timer_instance
*timeri
;
107 timeri
= kzalloc(sizeof(*timeri
), GFP_KERNEL
);
110 timeri
->owner
= kstrdup(owner
, GFP_KERNEL
);
111 if (! timeri
->owner
) {
115 INIT_LIST_HEAD(&timeri
->open_list
);
116 INIT_LIST_HEAD(&timeri
->active_list
);
117 INIT_LIST_HEAD(&timeri
->ack_list
);
118 INIT_LIST_HEAD(&timeri
->slave_list_head
);
119 INIT_LIST_HEAD(&timeri
->slave_active_head
);
121 timeri
->timer
= timer
;
122 if (timer
&& !try_module_get(timer
->module
)) {
123 kfree(timeri
->owner
);
132 * find a timer instance from the given timer id
134 static struct snd_timer
*snd_timer_find(struct snd_timer_id
*tid
)
136 struct snd_timer
*timer
= NULL
;
138 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
139 if (timer
->tmr_class
!= tid
->dev_class
)
141 if ((timer
->tmr_class
== SNDRV_TIMER_CLASS_CARD
||
142 timer
->tmr_class
== SNDRV_TIMER_CLASS_PCM
) &&
143 (timer
->card
== NULL
||
144 timer
->card
->number
!= tid
->card
))
146 if (timer
->tmr_device
!= tid
->device
)
148 if (timer
->tmr_subdevice
!= tid
->subdevice
)
155 #ifdef CONFIG_MODULES
157 static void snd_timer_request(struct snd_timer_id
*tid
)
159 switch (tid
->dev_class
) {
160 case SNDRV_TIMER_CLASS_GLOBAL
:
161 if (tid
->device
< timer_limit
)
162 request_module("snd-timer-%i", tid
->device
);
164 case SNDRV_TIMER_CLASS_CARD
:
165 case SNDRV_TIMER_CLASS_PCM
:
166 if (tid
->card
< snd_ecards_limit
)
167 request_module("snd-card-%i", tid
->card
);
177 * look for a master instance matching with the slave id of the given slave.
178 * when found, relink the open_link of the slave.
180 * call this with register_mutex down.
182 static int snd_timer_check_slave(struct snd_timer_instance
*slave
)
184 struct snd_timer
*timer
;
185 struct snd_timer_instance
*master
;
187 /* FIXME: it's really dumb to look up all entries.. */
188 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
189 list_for_each_entry(master
, &timer
->open_list_head
, open_list
) {
190 if (slave
->slave_class
== master
->slave_class
&&
191 slave
->slave_id
== master
->slave_id
) {
192 if (master
->timer
->num_instances
>=
193 master
->timer
->max_instances
)
195 list_move_tail(&slave
->open_list
,
196 &master
->slave_list_head
);
197 master
->timer
->num_instances
++;
198 spin_lock_irq(&slave_active_lock
);
199 slave
->master
= master
;
200 slave
->timer
= master
->timer
;
201 spin_unlock_irq(&slave_active_lock
);
210 * look for slave instances matching with the slave id of the given master.
211 * when found, relink the open_link of slaves.
213 * call this with register_mutex down.
215 static int snd_timer_check_master(struct snd_timer_instance
*master
)
217 struct snd_timer_instance
*slave
, *tmp
;
219 /* check all pending slaves */
220 list_for_each_entry_safe(slave
, tmp
, &snd_timer_slave_list
, open_list
) {
221 if (slave
->slave_class
== master
->slave_class
&&
222 slave
->slave_id
== master
->slave_id
) {
223 if (master
->timer
->num_instances
>=
224 master
->timer
->max_instances
)
226 list_move_tail(&slave
->open_list
, &master
->slave_list_head
);
227 master
->timer
->num_instances
++;
228 spin_lock_irq(&slave_active_lock
);
229 spin_lock(&master
->timer
->lock
);
230 slave
->master
= master
;
231 slave
->timer
= master
->timer
;
232 if (slave
->flags
& SNDRV_TIMER_IFLG_RUNNING
)
233 list_add_tail(&slave
->active_list
,
234 &master
->slave_active_head
);
235 spin_unlock(&master
->timer
->lock
);
236 spin_unlock_irq(&slave_active_lock
);
242 static int snd_timer_close_locked(struct snd_timer_instance
*timeri
);
245 * open a timer instance
246 * when opening a master, the slave id must be here given.
248 int snd_timer_open(struct snd_timer_instance
**ti
,
249 char *owner
, struct snd_timer_id
*tid
,
250 unsigned int slave_id
)
252 struct snd_timer
*timer
;
253 struct snd_timer_instance
*timeri
= NULL
;
256 mutex_lock(®ister_mutex
);
257 if (tid
->dev_class
== SNDRV_TIMER_CLASS_SLAVE
) {
258 /* open a slave instance */
259 if (tid
->dev_sclass
<= SNDRV_TIMER_SCLASS_NONE
||
260 tid
->dev_sclass
> SNDRV_TIMER_SCLASS_OSS_SEQUENCER
) {
261 pr_debug("ALSA: timer: invalid slave class %i\n",
266 timeri
= snd_timer_instance_new(owner
, NULL
);
271 timeri
->slave_class
= tid
->dev_sclass
;
272 timeri
->slave_id
= tid
->device
;
273 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
274 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
275 err
= snd_timer_check_slave(timeri
);
277 snd_timer_close_locked(timeri
);
283 /* open a master instance */
284 timer
= snd_timer_find(tid
);
285 #ifdef CONFIG_MODULES
287 mutex_unlock(®ister_mutex
);
288 snd_timer_request(tid
);
289 mutex_lock(®ister_mutex
);
290 timer
= snd_timer_find(tid
);
297 if (!list_empty(&timer
->open_list_head
)) {
298 timeri
= list_entry(timer
->open_list_head
.next
,
299 struct snd_timer_instance
, open_list
);
300 if (timeri
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
306 if (timer
->num_instances
>= timer
->max_instances
) {
310 timeri
= snd_timer_instance_new(owner
, timer
);
315 /* take a card refcount for safe disconnection */
317 get_device(&timer
->card
->card_dev
);
318 timeri
->slave_class
= tid
->dev_sclass
;
319 timeri
->slave_id
= slave_id
;
321 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
) {
322 err
= timer
->hw
.open(timer
);
324 kfree(timeri
->owner
);
329 put_device(&timer
->card
->card_dev
);
330 module_put(timer
->module
);
335 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
336 timer
->num_instances
++;
337 err
= snd_timer_check_master(timeri
);
339 snd_timer_close_locked(timeri
);
344 mutex_unlock(®ister_mutex
);
348 EXPORT_SYMBOL(snd_timer_open
);
351 * close a timer instance
352 * call this with register_mutex down.
354 static int snd_timer_close_locked(struct snd_timer_instance
*timeri
)
356 struct snd_timer
*timer
= NULL
;
357 struct snd_timer_instance
*slave
, *tmp
;
359 list_del(&timeri
->open_list
);
361 /* force to stop the timer */
362 snd_timer_stop(timeri
);
364 timer
= timeri
->timer
;
366 timer
->num_instances
--;
367 /* wait, until the active callback is finished */
368 spin_lock_irq(&timer
->lock
);
369 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
370 spin_unlock_irq(&timer
->lock
);
372 spin_lock_irq(&timer
->lock
);
374 spin_unlock_irq(&timer
->lock
);
376 /* remove slave links */
377 spin_lock_irq(&slave_active_lock
);
378 spin_lock(&timer
->lock
);
379 list_for_each_entry_safe(slave
, tmp
, &timeri
->slave_list_head
,
381 list_move_tail(&slave
->open_list
, &snd_timer_slave_list
);
382 timer
->num_instances
--;
383 slave
->master
= NULL
;
385 list_del_init(&slave
->ack_list
);
386 list_del_init(&slave
->active_list
);
388 spin_unlock(&timer
->lock
);
389 spin_unlock_irq(&slave_active_lock
);
391 /* slave doesn't need to release timer resources below */
392 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
396 if (timeri
->private_free
)
397 timeri
->private_free(timeri
);
398 kfree(timeri
->owner
);
402 if (list_empty(&timer
->open_list_head
) && timer
->hw
.close
)
403 timer
->hw
.close(timer
);
404 /* release a card refcount for safe disconnection */
406 put_device(&timer
->card
->card_dev
);
407 module_put(timer
->module
);
414 * close a timer instance
416 int snd_timer_close(struct snd_timer_instance
*timeri
)
420 if (snd_BUG_ON(!timeri
))
423 mutex_lock(®ister_mutex
);
424 err
= snd_timer_close_locked(timeri
);
425 mutex_unlock(®ister_mutex
);
428 EXPORT_SYMBOL(snd_timer_close
);
430 unsigned long snd_timer_resolution(struct snd_timer_instance
*timeri
)
432 struct snd_timer
* timer
;
436 if ((timer
= timeri
->timer
) != NULL
) {
437 if (timer
->hw
.c_resolution
)
438 return timer
->hw
.c_resolution(timer
);
439 return timer
->hw
.resolution
;
443 EXPORT_SYMBOL(snd_timer_resolution
);
445 static void snd_timer_notify1(struct snd_timer_instance
*ti
, int event
)
447 struct snd_timer
*timer
;
448 unsigned long resolution
= 0;
449 struct snd_timer_instance
*ts
;
450 struct timespec tstamp
;
452 if (timer_tstamp_monotonic
)
453 ktime_get_ts(&tstamp
);
455 getnstimeofday(&tstamp
);
456 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_START
||
457 event
> SNDRV_TIMER_EVENT_PAUSE
))
459 if (event
== SNDRV_TIMER_EVENT_START
||
460 event
== SNDRV_TIMER_EVENT_CONTINUE
)
461 resolution
= snd_timer_resolution(ti
);
463 ti
->ccallback(ti
, event
, &tstamp
, resolution
);
464 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
469 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
471 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
473 ts
->ccallback(ts
, event
+ 100, &tstamp
, resolution
);
476 /* start/continue a master timer */
477 static int snd_timer_start1(struct snd_timer_instance
*timeri
,
478 bool start
, unsigned long ticks
)
480 struct snd_timer
*timer
;
484 timer
= timeri
->timer
;
488 spin_lock_irqsave(&timer
->lock
, flags
);
489 if (timer
->card
&& timer
->card
->shutdown
) {
493 if (timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
494 SNDRV_TIMER_IFLG_START
)) {
500 timeri
->ticks
= timeri
->cticks
= ticks
;
501 else if (!timeri
->cticks
)
505 list_move_tail(&timeri
->active_list
, &timer
->active_list_head
);
506 if (timer
->running
) {
507 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
509 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
510 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
511 result
= 1; /* delayed start */
514 timer
->sticks
= ticks
;
515 timer
->hw
.start(timer
);
518 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
521 snd_timer_notify1(timeri
, start
? SNDRV_TIMER_EVENT_START
:
522 SNDRV_TIMER_EVENT_CONTINUE
);
524 spin_unlock_irqrestore(&timer
->lock
, flags
);
528 /* start/continue a slave timer */
529 static int snd_timer_start_slave(struct snd_timer_instance
*timeri
,
534 spin_lock_irqsave(&slave_active_lock
, flags
);
535 if (timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
536 spin_unlock_irqrestore(&slave_active_lock
, flags
);
539 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
540 if (timeri
->master
&& timeri
->timer
) {
541 spin_lock(&timeri
->timer
->lock
);
542 list_add_tail(&timeri
->active_list
,
543 &timeri
->master
->slave_active_head
);
544 snd_timer_notify1(timeri
, start
? SNDRV_TIMER_EVENT_START
:
545 SNDRV_TIMER_EVENT_CONTINUE
);
546 spin_unlock(&timeri
->timer
->lock
);
548 spin_unlock_irqrestore(&slave_active_lock
, flags
);
549 return 1; /* delayed start */
552 /* stop/pause a master timer */
553 static int snd_timer_stop1(struct snd_timer_instance
*timeri
, bool stop
)
555 struct snd_timer
*timer
;
559 timer
= timeri
->timer
;
562 spin_lock_irqsave(&timer
->lock
, flags
);
563 if (!(timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
564 SNDRV_TIMER_IFLG_START
))) {
568 list_del_init(&timeri
->ack_list
);
569 list_del_init(&timeri
->active_list
);
570 if (timer
->card
&& timer
->card
->shutdown
)
573 timeri
->cticks
= timeri
->ticks
;
576 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
577 !(--timer
->running
)) {
578 timer
->hw
.stop(timer
);
579 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
580 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
581 snd_timer_reschedule(timer
, 0);
582 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
583 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
584 timer
->hw
.start(timer
);
588 timeri
->flags
&= ~(SNDRV_TIMER_IFLG_RUNNING
| SNDRV_TIMER_IFLG_START
);
590 timeri
->flags
&= ~SNDRV_TIMER_IFLG_PAUSED
;
592 timeri
->flags
|= SNDRV_TIMER_IFLG_PAUSED
;
593 snd_timer_notify1(timeri
, stop
? SNDRV_TIMER_EVENT_STOP
:
594 SNDRV_TIMER_EVENT_PAUSE
);
596 spin_unlock_irqrestore(&timer
->lock
, flags
);
600 /* stop/pause a slave timer */
601 static int snd_timer_stop_slave(struct snd_timer_instance
*timeri
, bool stop
)
605 spin_lock_irqsave(&slave_active_lock
, flags
);
606 if (!(timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
)) {
607 spin_unlock_irqrestore(&slave_active_lock
, flags
);
610 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
612 spin_lock(&timeri
->timer
->lock
);
613 list_del_init(&timeri
->ack_list
);
614 list_del_init(&timeri
->active_list
);
615 snd_timer_notify1(timeri
, stop
? SNDRV_TIMER_EVENT_STOP
:
616 SNDRV_TIMER_EVENT_PAUSE
);
617 spin_unlock(&timeri
->timer
->lock
);
619 spin_unlock_irqrestore(&slave_active_lock
, flags
);
624 * start the timer instance
626 int snd_timer_start(struct snd_timer_instance
*timeri
, unsigned int ticks
)
628 if (timeri
== NULL
|| ticks
< 1)
630 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
631 return snd_timer_start_slave(timeri
, true);
633 return snd_timer_start1(timeri
, true, ticks
);
635 EXPORT_SYMBOL(snd_timer_start
);
638 * stop the timer instance.
640 * do not call this from the timer callback!
642 int snd_timer_stop(struct snd_timer_instance
*timeri
)
644 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
645 return snd_timer_stop_slave(timeri
, true);
647 return snd_timer_stop1(timeri
, true);
649 EXPORT_SYMBOL(snd_timer_stop
);
652 * start again.. the tick is kept.
654 int snd_timer_continue(struct snd_timer_instance
*timeri
)
656 /* timer can continue only after pause */
657 if (!(timeri
->flags
& SNDRV_TIMER_IFLG_PAUSED
))
660 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
661 return snd_timer_start_slave(timeri
, false);
663 return snd_timer_start1(timeri
, false, 0);
665 EXPORT_SYMBOL(snd_timer_continue
);
668 * pause.. remember the ticks left
670 int snd_timer_pause(struct snd_timer_instance
* timeri
)
672 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
673 return snd_timer_stop_slave(timeri
, false);
675 return snd_timer_stop1(timeri
, false);
677 EXPORT_SYMBOL(snd_timer_pause
);
680 * reschedule the timer
682 * start pending instances and check the scheduling ticks.
683 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
685 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
)
687 struct snd_timer_instance
*ti
;
688 unsigned long ticks
= ~0UL;
690 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
691 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
692 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
693 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
696 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
697 if (ticks
> ti
->cticks
)
702 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
705 if (ticks
> timer
->hw
.ticks
)
706 ticks
= timer
->hw
.ticks
;
707 if (ticks_left
!= ticks
)
708 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
709 timer
->sticks
= ticks
;
716 static void snd_timer_tasklet(unsigned long arg
)
718 struct snd_timer
*timer
= (struct snd_timer
*) arg
;
719 struct snd_timer_instance
*ti
;
721 unsigned long resolution
, ticks
;
724 if (timer
->card
&& timer
->card
->shutdown
)
727 spin_lock_irqsave(&timer
->lock
, flags
);
728 /* now process all callbacks */
729 while (!list_empty(&timer
->sack_list_head
)) {
730 p
= timer
->sack_list_head
.next
; /* get first item */
731 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
733 /* remove from ack_list and make empty */
738 resolution
= ti
->resolution
;
740 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
741 spin_unlock(&timer
->lock
);
743 ti
->callback(ti
, resolution
, ticks
);
744 spin_lock(&timer
->lock
);
745 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
747 spin_unlock_irqrestore(&timer
->lock
, flags
);
753 * ticks_left is usually equal to timer->sticks.
756 void snd_timer_interrupt(struct snd_timer
* timer
, unsigned long ticks_left
)
758 struct snd_timer_instance
*ti
, *ts
, *tmp
;
759 unsigned long resolution
, ticks
;
760 struct list_head
*p
, *ack_list_head
;
767 if (timer
->card
&& timer
->card
->shutdown
)
770 spin_lock_irqsave(&timer
->lock
, flags
);
772 /* remember the current resolution */
773 if (timer
->hw
.c_resolution
)
774 resolution
= timer
->hw
.c_resolution(timer
);
776 resolution
= timer
->hw
.resolution
;
778 /* loop for all active instances
779 * Here we cannot use list_for_each_entry because the active_list of a
780 * processed instance is relinked to done_list_head before the callback
783 list_for_each_entry_safe(ti
, tmp
, &timer
->active_list_head
,
785 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
787 ti
->pticks
+= ticks_left
;
788 ti
->resolution
= resolution
;
789 if (ti
->cticks
< ticks_left
)
792 ti
->cticks
-= ticks_left
;
793 if (ti
->cticks
) /* not expired */
795 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
796 ti
->cticks
= ti
->ticks
;
798 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
800 list_del_init(&ti
->active_list
);
802 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
803 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
))
804 ack_list_head
= &timer
->ack_list_head
;
806 ack_list_head
= &timer
->sack_list_head
;
807 if (list_empty(&ti
->ack_list
))
808 list_add_tail(&ti
->ack_list
, ack_list_head
);
809 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
) {
810 ts
->pticks
= ti
->pticks
;
811 ts
->resolution
= resolution
;
812 if (list_empty(&ts
->ack_list
))
813 list_add_tail(&ts
->ack_list
, ack_list_head
);
816 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
817 snd_timer_reschedule(timer
, timer
->sticks
);
818 if (timer
->running
) {
819 if (timer
->hw
.flags
& SNDRV_TIMER_HW_STOP
) {
820 timer
->hw
.stop(timer
);
821 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
823 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_AUTO
) ||
824 (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
)) {
826 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
827 timer
->hw
.start(timer
);
830 timer
->hw
.stop(timer
);
833 /* now process all fast callbacks */
834 while (!list_empty(&timer
->ack_list_head
)) {
835 p
= timer
->ack_list_head
.next
; /* get first item */
836 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
838 /* remove from ack_list and make empty */
844 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
845 spin_unlock(&timer
->lock
);
847 ti
->callback(ti
, resolution
, ticks
);
848 spin_lock(&timer
->lock
);
849 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
852 /* do we have any slow callbacks? */
853 use_tasklet
= !list_empty(&timer
->sack_list_head
);
854 spin_unlock_irqrestore(&timer
->lock
, flags
);
857 tasklet_schedule(&timer
->task_queue
);
859 EXPORT_SYMBOL(snd_timer_interrupt
);
865 int snd_timer_new(struct snd_card
*card
, char *id
, struct snd_timer_id
*tid
,
866 struct snd_timer
**rtimer
)
868 struct snd_timer
*timer
;
870 static struct snd_device_ops ops
= {
871 .dev_free
= snd_timer_dev_free
,
872 .dev_register
= snd_timer_dev_register
,
873 .dev_disconnect
= snd_timer_dev_disconnect
,
876 if (snd_BUG_ON(!tid
))
880 timer
= kzalloc(sizeof(*timer
), GFP_KERNEL
);
883 timer
->tmr_class
= tid
->dev_class
;
885 timer
->tmr_device
= tid
->device
;
886 timer
->tmr_subdevice
= tid
->subdevice
;
888 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
890 INIT_LIST_HEAD(&timer
->device_list
);
891 INIT_LIST_HEAD(&timer
->open_list_head
);
892 INIT_LIST_HEAD(&timer
->active_list_head
);
893 INIT_LIST_HEAD(&timer
->ack_list_head
);
894 INIT_LIST_HEAD(&timer
->sack_list_head
);
895 spin_lock_init(&timer
->lock
);
896 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
,
897 (unsigned long)timer
);
898 timer
->max_instances
= 1000; /* default limit per timer */
900 timer
->module
= card
->module
;
901 err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
);
903 snd_timer_free(timer
);
911 EXPORT_SYMBOL(snd_timer_new
);
913 static int snd_timer_free(struct snd_timer
*timer
)
918 mutex_lock(®ister_mutex
);
919 if (! list_empty(&timer
->open_list_head
)) {
920 struct list_head
*p
, *n
;
921 struct snd_timer_instance
*ti
;
922 pr_warn("ALSA: timer %p is busy?\n", timer
);
923 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
925 ti
= list_entry(p
, struct snd_timer_instance
, open_list
);
929 list_del(&timer
->device_list
);
930 mutex_unlock(®ister_mutex
);
932 if (timer
->private_free
)
933 timer
->private_free(timer
);
938 static int snd_timer_dev_free(struct snd_device
*device
)
940 struct snd_timer
*timer
= device
->device_data
;
941 return snd_timer_free(timer
);
944 static int snd_timer_dev_register(struct snd_device
*dev
)
946 struct snd_timer
*timer
= dev
->device_data
;
947 struct snd_timer
*timer1
;
949 if (snd_BUG_ON(!timer
|| !timer
->hw
.start
|| !timer
->hw
.stop
))
951 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
952 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
955 mutex_lock(®ister_mutex
);
956 list_for_each_entry(timer1
, &snd_timer_list
, device_list
) {
957 if (timer1
->tmr_class
> timer
->tmr_class
)
959 if (timer1
->tmr_class
< timer
->tmr_class
)
961 if (timer1
->card
&& timer
->card
) {
962 if (timer1
->card
->number
> timer
->card
->number
)
964 if (timer1
->card
->number
< timer
->card
->number
)
967 if (timer1
->tmr_device
> timer
->tmr_device
)
969 if (timer1
->tmr_device
< timer
->tmr_device
)
971 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
973 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
976 mutex_unlock(®ister_mutex
);
979 list_add_tail(&timer
->device_list
, &timer1
->device_list
);
980 mutex_unlock(®ister_mutex
);
984 static int snd_timer_dev_disconnect(struct snd_device
*device
)
986 struct snd_timer
*timer
= device
->device_data
;
987 struct snd_timer_instance
*ti
;
989 mutex_lock(®ister_mutex
);
990 list_del_init(&timer
->device_list
);
991 /* wake up pending sleepers */
992 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
) {
996 mutex_unlock(®ister_mutex
);
1000 void snd_timer_notify(struct snd_timer
*timer
, int event
, struct timespec
*tstamp
)
1002 unsigned long flags
;
1003 unsigned long resolution
= 0;
1004 struct snd_timer_instance
*ti
, *ts
;
1006 if (timer
->card
&& timer
->card
->shutdown
)
1008 if (! (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
))
1010 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_MSTART
||
1011 event
> SNDRV_TIMER_EVENT_MRESUME
))
1013 spin_lock_irqsave(&timer
->lock
, flags
);
1014 if (event
== SNDRV_TIMER_EVENT_MSTART
||
1015 event
== SNDRV_TIMER_EVENT_MCONTINUE
||
1016 event
== SNDRV_TIMER_EVENT_MRESUME
) {
1017 if (timer
->hw
.c_resolution
)
1018 resolution
= timer
->hw
.c_resolution(timer
);
1020 resolution
= timer
->hw
.resolution
;
1022 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
1024 ti
->ccallback(ti
, event
, tstamp
, resolution
);
1025 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
1027 ts
->ccallback(ts
, event
, tstamp
, resolution
);
1029 spin_unlock_irqrestore(&timer
->lock
, flags
);
1031 EXPORT_SYMBOL(snd_timer_notify
);
1034 * exported functions for global timers
1036 int snd_timer_global_new(char *id
, int device
, struct snd_timer
**rtimer
)
1038 struct snd_timer_id tid
;
1040 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
1041 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1043 tid
.device
= device
;
1045 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
1047 EXPORT_SYMBOL(snd_timer_global_new
);
1049 int snd_timer_global_free(struct snd_timer
*timer
)
1051 return snd_timer_free(timer
);
1053 EXPORT_SYMBOL(snd_timer_global_free
);
1055 int snd_timer_global_register(struct snd_timer
*timer
)
1057 struct snd_device dev
;
1059 memset(&dev
, 0, sizeof(dev
));
1060 dev
.device_data
= timer
;
1061 return snd_timer_dev_register(&dev
);
1063 EXPORT_SYMBOL(snd_timer_global_register
);
1069 struct snd_timer_system_private
{
1070 struct timer_list tlist
;
1071 unsigned long last_expires
;
1072 unsigned long last_jiffies
;
1073 unsigned long correction
;
1076 static void snd_timer_s_function(unsigned long data
)
1078 struct snd_timer
*timer
= (struct snd_timer
*)data
;
1079 struct snd_timer_system_private
*priv
= timer
->private_data
;
1080 unsigned long jiff
= jiffies
;
1081 if (time_after(jiff
, priv
->last_expires
))
1082 priv
->correction
+= (long)jiff
- (long)priv
->last_expires
;
1083 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
1086 static int snd_timer_s_start(struct snd_timer
* timer
)
1088 struct snd_timer_system_private
*priv
;
1089 unsigned long njiff
;
1091 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1092 njiff
= (priv
->last_jiffies
= jiffies
);
1093 if (priv
->correction
> timer
->sticks
- 1) {
1094 priv
->correction
-= timer
->sticks
- 1;
1097 njiff
+= timer
->sticks
- priv
->correction
;
1098 priv
->correction
= 0;
1100 priv
->last_expires
= njiff
;
1101 mod_timer(&priv
->tlist
, njiff
);
1105 static int snd_timer_s_stop(struct snd_timer
* timer
)
1107 struct snd_timer_system_private
*priv
;
1110 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1111 del_timer(&priv
->tlist
);
1113 if (time_before(jiff
, priv
->last_expires
))
1114 timer
->sticks
= priv
->last_expires
- jiff
;
1117 priv
->correction
= 0;
1121 static int snd_timer_s_close(struct snd_timer
*timer
)
1123 struct snd_timer_system_private
*priv
;
1125 priv
= (struct snd_timer_system_private
*)timer
->private_data
;
1126 del_timer_sync(&priv
->tlist
);
1130 static struct snd_timer_hardware snd_timer_system
=
1132 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
1133 .resolution
= 1000000000L / HZ
,
1135 .close
= snd_timer_s_close
,
1136 .start
= snd_timer_s_start
,
1137 .stop
= snd_timer_s_stop
1140 static void snd_timer_free_system(struct snd_timer
*timer
)
1142 kfree(timer
->private_data
);
1145 static int snd_timer_register_system(void)
1147 struct snd_timer
*timer
;
1148 struct snd_timer_system_private
*priv
;
1151 err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
);
1154 strcpy(timer
->name
, "system timer");
1155 timer
->hw
= snd_timer_system
;
1156 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1158 snd_timer_free(timer
);
1161 setup_timer(&priv
->tlist
, snd_timer_s_function
, (unsigned long) timer
);
1162 timer
->private_data
= priv
;
1163 timer
->private_free
= snd_timer_free_system
;
1164 return snd_timer_global_register(timer
);
1167 #ifdef CONFIG_SND_PROC_FS
1172 static void snd_timer_proc_read(struct snd_info_entry
*entry
,
1173 struct snd_info_buffer
*buffer
)
1175 struct snd_timer
*timer
;
1176 struct snd_timer_instance
*ti
;
1178 mutex_lock(®ister_mutex
);
1179 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
1180 if (timer
->card
&& timer
->card
->shutdown
)
1182 switch (timer
->tmr_class
) {
1183 case SNDRV_TIMER_CLASS_GLOBAL
:
1184 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1186 case SNDRV_TIMER_CLASS_CARD
:
1187 snd_iprintf(buffer
, "C%i-%i: ",
1188 timer
->card
->number
, timer
->tmr_device
);
1190 case SNDRV_TIMER_CLASS_PCM
:
1191 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
,
1192 timer
->tmr_device
, timer
->tmr_subdevice
);
1195 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
,
1196 timer
->card
? timer
->card
->number
: -1,
1197 timer
->tmr_device
, timer
->tmr_subdevice
);
1199 snd_iprintf(buffer
, "%s :", timer
->name
);
1200 if (timer
->hw
.resolution
)
1201 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)",
1202 timer
->hw
.resolution
/ 1000,
1203 timer
->hw
.resolution
% 1000,
1205 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1206 snd_iprintf(buffer
, " SLAVE");
1207 snd_iprintf(buffer
, "\n");
1208 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
)
1209 snd_iprintf(buffer
, " Client %s : %s\n",
1210 ti
->owner
? ti
->owner
: "unknown",
1211 ti
->flags
& (SNDRV_TIMER_IFLG_START
|
1212 SNDRV_TIMER_IFLG_RUNNING
)
1213 ? "running" : "stopped");
1215 mutex_unlock(®ister_mutex
);
1218 static struct snd_info_entry
*snd_timer_proc_entry
;
1220 static void __init
snd_timer_proc_init(void)
1222 struct snd_info_entry
*entry
;
1224 entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
);
1225 if (entry
!= NULL
) {
1226 entry
->c
.text
.read
= snd_timer_proc_read
;
1227 if (snd_info_register(entry
) < 0) {
1228 snd_info_free_entry(entry
);
1232 snd_timer_proc_entry
= entry
;
1235 static void __exit
snd_timer_proc_done(void)
1237 snd_info_free_entry(snd_timer_proc_entry
);
1239 #else /* !CONFIG_SND_PROC_FS */
1240 #define snd_timer_proc_init()
1241 #define snd_timer_proc_done()
1245 * USER SPACE interface
1248 static void snd_timer_user_interrupt(struct snd_timer_instance
*timeri
,
1249 unsigned long resolution
,
1250 unsigned long ticks
)
1252 struct snd_timer_user
*tu
= timeri
->callback_data
;
1253 struct snd_timer_read
*r
;
1256 spin_lock(&tu
->qlock
);
1257 if (tu
->qused
> 0) {
1258 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1259 r
= &tu
->queue
[prev
];
1260 if (r
->resolution
== resolution
) {
1265 if (tu
->qused
>= tu
->queue_size
) {
1268 r
= &tu
->queue
[tu
->qtail
++];
1269 tu
->qtail
%= tu
->queue_size
;
1270 r
->resolution
= resolution
;
1275 spin_unlock(&tu
->qlock
);
1276 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1277 wake_up(&tu
->qchange_sleep
);
1280 static void snd_timer_user_append_to_tqueue(struct snd_timer_user
*tu
,
1281 struct snd_timer_tread
*tread
)
1283 if (tu
->qused
>= tu
->queue_size
) {
1286 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1287 tu
->qtail
%= tu
->queue_size
;
1292 static void snd_timer_user_ccallback(struct snd_timer_instance
*timeri
,
1294 struct timespec
*tstamp
,
1295 unsigned long resolution
)
1297 struct snd_timer_user
*tu
= timeri
->callback_data
;
1298 struct snd_timer_tread r1
;
1299 unsigned long flags
;
1301 if (event
>= SNDRV_TIMER_EVENT_START
&&
1302 event
<= SNDRV_TIMER_EVENT_PAUSE
)
1303 tu
->tstamp
= *tstamp
;
1304 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1306 memset(&r1
, 0, sizeof(r1
));
1308 r1
.tstamp
= *tstamp
;
1309 r1
.val
= resolution
;
1310 spin_lock_irqsave(&tu
->qlock
, flags
);
1311 snd_timer_user_append_to_tqueue(tu
, &r1
);
1312 spin_unlock_irqrestore(&tu
->qlock
, flags
);
1313 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1314 wake_up(&tu
->qchange_sleep
);
1317 static void snd_timer_user_disconnect(struct snd_timer_instance
*timeri
)
1319 struct snd_timer_user
*tu
= timeri
->callback_data
;
1321 tu
->disconnected
= true;
1322 wake_up(&tu
->qchange_sleep
);
1325 static void snd_timer_user_tinterrupt(struct snd_timer_instance
*timeri
,
1326 unsigned long resolution
,
1327 unsigned long ticks
)
1329 struct snd_timer_user
*tu
= timeri
->callback_data
;
1330 struct snd_timer_tread
*r
, r1
;
1331 struct timespec tstamp
;
1332 int prev
, append
= 0;
1334 memset(&tstamp
, 0, sizeof(tstamp
));
1335 spin_lock(&tu
->qlock
);
1336 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
) |
1337 (1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1338 spin_unlock(&tu
->qlock
);
1341 if (tu
->last_resolution
!= resolution
|| ticks
> 0) {
1342 if (timer_tstamp_monotonic
)
1343 ktime_get_ts(&tstamp
);
1345 getnstimeofday(&tstamp
);
1347 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) &&
1348 tu
->last_resolution
!= resolution
) {
1349 memset(&r1
, 0, sizeof(r1
));
1350 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1352 r1
.val
= resolution
;
1353 snd_timer_user_append_to_tqueue(tu
, &r1
);
1354 tu
->last_resolution
= resolution
;
1357 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1361 if (tu
->qused
> 0) {
1362 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1363 r
= &tu
->tqueue
[prev
];
1364 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1371 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1374 snd_timer_user_append_to_tqueue(tu
, &r1
);
1377 spin_unlock(&tu
->qlock
);
1380 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1381 wake_up(&tu
->qchange_sleep
);
1384 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1386 struct snd_timer_user
*tu
;
1389 err
= nonseekable_open(inode
, file
);
1393 tu
= kzalloc(sizeof(*tu
), GFP_KERNEL
);
1396 spin_lock_init(&tu
->qlock
);
1397 init_waitqueue_head(&tu
->qchange_sleep
);
1398 mutex_init(&tu
->ioctl_lock
);
1400 tu
->queue_size
= 128;
1401 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1403 if (tu
->queue
== NULL
) {
1407 file
->private_data
= tu
;
1411 static int snd_timer_user_release(struct inode
*inode
, struct file
*file
)
1413 struct snd_timer_user
*tu
;
1415 if (file
->private_data
) {
1416 tu
= file
->private_data
;
1417 file
->private_data
= NULL
;
1418 mutex_lock(&tu
->ioctl_lock
);
1420 snd_timer_close(tu
->timeri
);
1421 mutex_unlock(&tu
->ioctl_lock
);
1429 static void snd_timer_user_zero_id(struct snd_timer_id
*id
)
1431 id
->dev_class
= SNDRV_TIMER_CLASS_NONE
;
1432 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1438 static void snd_timer_user_copy_id(struct snd_timer_id
*id
, struct snd_timer
*timer
)
1440 id
->dev_class
= timer
->tmr_class
;
1441 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1442 id
->card
= timer
->card
? timer
->card
->number
: -1;
1443 id
->device
= timer
->tmr_device
;
1444 id
->subdevice
= timer
->tmr_subdevice
;
1447 static int snd_timer_user_next_device(struct snd_timer_id __user
*_tid
)
1449 struct snd_timer_id id
;
1450 struct snd_timer
*timer
;
1451 struct list_head
*p
;
1453 if (copy_from_user(&id
, _tid
, sizeof(id
)))
1455 mutex_lock(®ister_mutex
);
1456 if (id
.dev_class
< 0) { /* first item */
1457 if (list_empty(&snd_timer_list
))
1458 snd_timer_user_zero_id(&id
);
1460 timer
= list_entry(snd_timer_list
.next
,
1461 struct snd_timer
, device_list
);
1462 snd_timer_user_copy_id(&id
, timer
);
1465 switch (id
.dev_class
) {
1466 case SNDRV_TIMER_CLASS_GLOBAL
:
1467 id
.device
= id
.device
< 0 ? 0 : id
.device
+ 1;
1468 list_for_each(p
, &snd_timer_list
) {
1469 timer
= list_entry(p
, struct snd_timer
, device_list
);
1470 if (timer
->tmr_class
> SNDRV_TIMER_CLASS_GLOBAL
) {
1471 snd_timer_user_copy_id(&id
, timer
);
1474 if (timer
->tmr_device
>= id
.device
) {
1475 snd_timer_user_copy_id(&id
, timer
);
1479 if (p
== &snd_timer_list
)
1480 snd_timer_user_zero_id(&id
);
1482 case SNDRV_TIMER_CLASS_CARD
:
1483 case SNDRV_TIMER_CLASS_PCM
:
1490 if (id
.device
< 0) {
1493 if (id
.subdevice
< 0) {
1501 list_for_each(p
, &snd_timer_list
) {
1502 timer
= list_entry(p
, struct snd_timer
, device_list
);
1503 if (timer
->tmr_class
> id
.dev_class
) {
1504 snd_timer_user_copy_id(&id
, timer
);
1507 if (timer
->tmr_class
< id
.dev_class
)
1509 if (timer
->card
->number
> id
.card
) {
1510 snd_timer_user_copy_id(&id
, timer
);
1513 if (timer
->card
->number
< id
.card
)
1515 if (timer
->tmr_device
> id
.device
) {
1516 snd_timer_user_copy_id(&id
, timer
);
1519 if (timer
->tmr_device
< id
.device
)
1521 if (timer
->tmr_subdevice
> id
.subdevice
) {
1522 snd_timer_user_copy_id(&id
, timer
);
1525 if (timer
->tmr_subdevice
< id
.subdevice
)
1527 snd_timer_user_copy_id(&id
, timer
);
1530 if (p
== &snd_timer_list
)
1531 snd_timer_user_zero_id(&id
);
1534 snd_timer_user_zero_id(&id
);
1537 mutex_unlock(®ister_mutex
);
1538 if (copy_to_user(_tid
, &id
, sizeof(*_tid
)))
1543 static int snd_timer_user_ginfo(struct file
*file
,
1544 struct snd_timer_ginfo __user
*_ginfo
)
1546 struct snd_timer_ginfo
*ginfo
;
1547 struct snd_timer_id tid
;
1548 struct snd_timer
*t
;
1549 struct list_head
*p
;
1552 ginfo
= memdup_user(_ginfo
, sizeof(*ginfo
));
1554 return PTR_ERR(ginfo
);
1557 memset(ginfo
, 0, sizeof(*ginfo
));
1559 mutex_lock(®ister_mutex
);
1560 t
= snd_timer_find(&tid
);
1562 ginfo
->card
= t
->card
? t
->card
->number
: -1;
1563 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1564 ginfo
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1565 strlcpy(ginfo
->id
, t
->id
, sizeof(ginfo
->id
));
1566 strlcpy(ginfo
->name
, t
->name
, sizeof(ginfo
->name
));
1567 ginfo
->resolution
= t
->hw
.resolution
;
1568 if (t
->hw
.resolution_min
> 0) {
1569 ginfo
->resolution_min
= t
->hw
.resolution_min
;
1570 ginfo
->resolution_max
= t
->hw
.resolution_max
;
1572 list_for_each(p
, &t
->open_list_head
) {
1578 mutex_unlock(®ister_mutex
);
1579 if (err
>= 0 && copy_to_user(_ginfo
, ginfo
, sizeof(*ginfo
)))
1585 static int timer_set_gparams(struct snd_timer_gparams
*gparams
)
1587 struct snd_timer
*t
;
1590 mutex_lock(®ister_mutex
);
1591 t
= snd_timer_find(&gparams
->tid
);
1596 if (!list_empty(&t
->open_list_head
)) {
1600 if (!t
->hw
.set_period
) {
1604 err
= t
->hw
.set_period(t
, gparams
->period_num
, gparams
->period_den
);
1606 mutex_unlock(®ister_mutex
);
1610 static int snd_timer_user_gparams(struct file
*file
,
1611 struct snd_timer_gparams __user
*_gparams
)
1613 struct snd_timer_gparams gparams
;
1615 if (copy_from_user(&gparams
, _gparams
, sizeof(gparams
)))
1617 return timer_set_gparams(&gparams
);
1620 static int snd_timer_user_gstatus(struct file
*file
,
1621 struct snd_timer_gstatus __user
*_gstatus
)
1623 struct snd_timer_gstatus gstatus
;
1624 struct snd_timer_id tid
;
1625 struct snd_timer
*t
;
1628 if (copy_from_user(&gstatus
, _gstatus
, sizeof(gstatus
)))
1631 memset(&gstatus
, 0, sizeof(gstatus
));
1633 mutex_lock(®ister_mutex
);
1634 t
= snd_timer_find(&tid
);
1636 if (t
->hw
.c_resolution
)
1637 gstatus
.resolution
= t
->hw
.c_resolution(t
);
1639 gstatus
.resolution
= t
->hw
.resolution
;
1640 if (t
->hw
.precise_resolution
) {
1641 t
->hw
.precise_resolution(t
, &gstatus
.resolution_num
,
1642 &gstatus
.resolution_den
);
1644 gstatus
.resolution_num
= gstatus
.resolution
;
1645 gstatus
.resolution_den
= 1000000000uL;
1650 mutex_unlock(®ister_mutex
);
1651 if (err
>= 0 && copy_to_user(_gstatus
, &gstatus
, sizeof(gstatus
)))
1656 static int snd_timer_user_tselect(struct file
*file
,
1657 struct snd_timer_select __user
*_tselect
)
1659 struct snd_timer_user
*tu
;
1660 struct snd_timer_select tselect
;
1664 tu
= file
->private_data
;
1666 snd_timer_close(tu
->timeri
);
1669 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
))) {
1673 sprintf(str
, "application %i", current
->pid
);
1674 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1675 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1676 err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
);
1680 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1686 tu
->tqueue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_tread
),
1688 if (tu
->tqueue
== NULL
)
1691 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1693 if (tu
->queue
== NULL
)
1698 snd_timer_close(tu
->timeri
);
1701 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1702 tu
->timeri
->callback
= tu
->tread
1703 ? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1704 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1705 tu
->timeri
->callback_data
= (void *)tu
;
1706 tu
->timeri
->disconnect
= snd_timer_user_disconnect
;
1713 static int snd_timer_user_info(struct file
*file
,
1714 struct snd_timer_info __user
*_info
)
1716 struct snd_timer_user
*tu
;
1717 struct snd_timer_info
*info
;
1718 struct snd_timer
*t
;
1721 tu
= file
->private_data
;
1724 t
= tu
->timeri
->timer
;
1728 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1731 info
->card
= t
->card
? t
->card
->number
: -1;
1732 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1733 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1734 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1735 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1736 info
->resolution
= t
->hw
.resolution
;
1737 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1743 static int snd_timer_user_params(struct file
*file
,
1744 struct snd_timer_params __user
*_params
)
1746 struct snd_timer_user
*tu
;
1747 struct snd_timer_params params
;
1748 struct snd_timer
*t
;
1749 struct snd_timer_read
*tr
;
1750 struct snd_timer_tread
*ttr
;
1753 tu
= file
->private_data
;
1756 t
= tu
->timeri
->timer
;
1759 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
1761 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)) {
1764 if (params
.ticks
< 1) {
1769 /* Don't allow resolution less than 1ms */
1770 resolution
= snd_timer_resolution(tu
->timeri
);
1771 resolution
*= params
.ticks
;
1772 if (resolution
< 1000000) {
1777 if (params
.queue_size
> 0 &&
1778 (params
.queue_size
< 32 || params
.queue_size
> 1024)) {
1782 if (params
.filter
& ~((1<<SNDRV_TIMER_EVENT_RESOLUTION
)|
1783 (1<<SNDRV_TIMER_EVENT_TICK
)|
1784 (1<<SNDRV_TIMER_EVENT_START
)|
1785 (1<<SNDRV_TIMER_EVENT_STOP
)|
1786 (1<<SNDRV_TIMER_EVENT_CONTINUE
)|
1787 (1<<SNDRV_TIMER_EVENT_PAUSE
)|
1788 (1<<SNDRV_TIMER_EVENT_SUSPEND
)|
1789 (1<<SNDRV_TIMER_EVENT_RESUME
)|
1790 (1<<SNDRV_TIMER_EVENT_MSTART
)|
1791 (1<<SNDRV_TIMER_EVENT_MSTOP
)|
1792 (1<<SNDRV_TIMER_EVENT_MCONTINUE
)|
1793 (1<<SNDRV_TIMER_EVENT_MPAUSE
)|
1794 (1<<SNDRV_TIMER_EVENT_MSUSPEND
)|
1795 (1<<SNDRV_TIMER_EVENT_MRESUME
))) {
1799 snd_timer_stop(tu
->timeri
);
1800 spin_lock_irq(&t
->lock
);
1801 tu
->timeri
->flags
&= ~(SNDRV_TIMER_IFLG_AUTO
|
1802 SNDRV_TIMER_IFLG_EXCLUSIVE
|
1803 SNDRV_TIMER_IFLG_EARLY_EVENT
);
1804 if (params
.flags
& SNDRV_TIMER_PSFLG_AUTO
)
1805 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
1806 if (params
.flags
& SNDRV_TIMER_PSFLG_EXCLUSIVE
)
1807 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EXCLUSIVE
;
1808 if (params
.flags
& SNDRV_TIMER_PSFLG_EARLY_EVENT
)
1809 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EARLY_EVENT
;
1810 spin_unlock_irq(&t
->lock
);
1811 if (params
.queue_size
> 0 &&
1812 (unsigned int)tu
->queue_size
!= params
.queue_size
) {
1814 ttr
= kmalloc(params
.queue_size
* sizeof(*ttr
),
1818 tu
->queue_size
= params
.queue_size
;
1822 tr
= kmalloc(params
.queue_size
* sizeof(*tr
),
1826 tu
->queue_size
= params
.queue_size
;
1831 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1832 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1834 struct snd_timer_tread tread
;
1835 memset(&tread
, 0, sizeof(tread
));
1836 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1837 tread
.tstamp
.tv_sec
= 0;
1838 tread
.tstamp
.tv_nsec
= 0;
1840 snd_timer_user_append_to_tqueue(tu
, &tread
);
1842 struct snd_timer_read
*r
= &tu
->queue
[0];
1849 tu
->filter
= params
.filter
;
1850 tu
->ticks
= params
.ticks
;
1853 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1858 static int snd_timer_user_status(struct file
*file
,
1859 struct snd_timer_status __user
*_status
)
1861 struct snd_timer_user
*tu
;
1862 struct snd_timer_status status
;
1864 tu
= file
->private_data
;
1867 memset(&status
, 0, sizeof(status
));
1868 status
.tstamp
= tu
->tstamp
;
1869 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1870 status
.lost
= tu
->timeri
->lost
;
1871 status
.overrun
= tu
->overrun
;
1872 spin_lock_irq(&tu
->qlock
);
1873 status
.queue
= tu
->qused
;
1874 spin_unlock_irq(&tu
->qlock
);
1875 if (copy_to_user(_status
, &status
, sizeof(status
)))
1880 static int snd_timer_user_start(struct file
*file
)
1883 struct snd_timer_user
*tu
;
1885 tu
= file
->private_data
;
1888 snd_timer_stop(tu
->timeri
);
1889 tu
->timeri
->lost
= 0;
1890 tu
->last_resolution
= 0;
1891 return (err
= snd_timer_start(tu
->timeri
, tu
->ticks
)) < 0 ? err
: 0;
1894 static int snd_timer_user_stop(struct file
*file
)
1897 struct snd_timer_user
*tu
;
1899 tu
= file
->private_data
;
1902 return (err
= snd_timer_stop(tu
->timeri
)) < 0 ? err
: 0;
1905 static int snd_timer_user_continue(struct file
*file
)
1908 struct snd_timer_user
*tu
;
1910 tu
= file
->private_data
;
1913 /* start timer instead of continue if it's not used before */
1914 if (!(tu
->timeri
->flags
& SNDRV_TIMER_IFLG_PAUSED
))
1915 return snd_timer_user_start(file
);
1916 tu
->timeri
->lost
= 0;
1917 return (err
= snd_timer_continue(tu
->timeri
)) < 0 ? err
: 0;
1920 static int snd_timer_user_pause(struct file
*file
)
1923 struct snd_timer_user
*tu
;
1925 tu
= file
->private_data
;
1928 return (err
= snd_timer_pause(tu
->timeri
)) < 0 ? err
: 0;
1932 SNDRV_TIMER_IOCTL_START_OLD
= _IO('T', 0x20),
1933 SNDRV_TIMER_IOCTL_STOP_OLD
= _IO('T', 0x21),
1934 SNDRV_TIMER_IOCTL_CONTINUE_OLD
= _IO('T', 0x22),
1935 SNDRV_TIMER_IOCTL_PAUSE_OLD
= _IO('T', 0x23),
1938 static long __snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1941 struct snd_timer_user
*tu
;
1942 void __user
*argp
= (void __user
*)arg
;
1943 int __user
*p
= argp
;
1945 tu
= file
->private_data
;
1947 case SNDRV_TIMER_IOCTL_PVERSION
:
1948 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1949 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1950 return snd_timer_user_next_device(argp
);
1951 case SNDRV_TIMER_IOCTL_TREAD
:
1955 if (tu
->timeri
) /* too late */
1957 if (get_user(xarg
, p
))
1959 tu
->tread
= xarg
? 1 : 0;
1962 case SNDRV_TIMER_IOCTL_GINFO
:
1963 return snd_timer_user_ginfo(file
, argp
);
1964 case SNDRV_TIMER_IOCTL_GPARAMS
:
1965 return snd_timer_user_gparams(file
, argp
);
1966 case SNDRV_TIMER_IOCTL_GSTATUS
:
1967 return snd_timer_user_gstatus(file
, argp
);
1968 case SNDRV_TIMER_IOCTL_SELECT
:
1969 return snd_timer_user_tselect(file
, argp
);
1970 case SNDRV_TIMER_IOCTL_INFO
:
1971 return snd_timer_user_info(file
, argp
);
1972 case SNDRV_TIMER_IOCTL_PARAMS
:
1973 return snd_timer_user_params(file
, argp
);
1974 case SNDRV_TIMER_IOCTL_STATUS
:
1975 return snd_timer_user_status(file
, argp
);
1976 case SNDRV_TIMER_IOCTL_START
:
1977 case SNDRV_TIMER_IOCTL_START_OLD
:
1978 return snd_timer_user_start(file
);
1979 case SNDRV_TIMER_IOCTL_STOP
:
1980 case SNDRV_TIMER_IOCTL_STOP_OLD
:
1981 return snd_timer_user_stop(file
);
1982 case SNDRV_TIMER_IOCTL_CONTINUE
:
1983 case SNDRV_TIMER_IOCTL_CONTINUE_OLD
:
1984 return snd_timer_user_continue(file
);
1985 case SNDRV_TIMER_IOCTL_PAUSE
:
1986 case SNDRV_TIMER_IOCTL_PAUSE_OLD
:
1987 return snd_timer_user_pause(file
);
1992 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1995 struct snd_timer_user
*tu
= file
->private_data
;
1998 mutex_lock(&tu
->ioctl_lock
);
1999 ret
= __snd_timer_user_ioctl(file
, cmd
, arg
);
2000 mutex_unlock(&tu
->ioctl_lock
);
2004 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
2006 struct snd_timer_user
*tu
;
2008 tu
= file
->private_data
;
2009 return fasync_helper(fd
, file
, on
, &tu
->fasync
);
2012 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
,
2013 size_t count
, loff_t
*offset
)
2015 struct snd_timer_user
*tu
;
2016 long result
= 0, unit
;
2020 tu
= file
->private_data
;
2021 unit
= tu
->tread
? sizeof(struct snd_timer_tread
) : sizeof(struct snd_timer_read
);
2022 mutex_lock(&tu
->ioctl_lock
);
2023 spin_lock_irq(&tu
->qlock
);
2024 while ((long)count
- result
>= unit
) {
2025 while (!tu
->qused
) {
2028 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
2033 set_current_state(TASK_INTERRUPTIBLE
);
2034 init_waitqueue_entry(&wait
, current
);
2035 add_wait_queue(&tu
->qchange_sleep
, &wait
);
2037 spin_unlock_irq(&tu
->qlock
);
2038 mutex_unlock(&tu
->ioctl_lock
);
2040 mutex_lock(&tu
->ioctl_lock
);
2041 spin_lock_irq(&tu
->qlock
);
2043 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
2045 if (tu
->disconnected
) {
2049 if (signal_pending(current
)) {
2055 qhead
= tu
->qhead
++;
2056 tu
->qhead
%= tu
->queue_size
;
2058 spin_unlock_irq(&tu
->qlock
);
2061 if (copy_to_user(buffer
, &tu
->tqueue
[qhead
],
2062 sizeof(struct snd_timer_tread
)))
2065 if (copy_to_user(buffer
, &tu
->queue
[qhead
],
2066 sizeof(struct snd_timer_read
)))
2070 spin_lock_irq(&tu
->qlock
);
2077 spin_unlock_irq(&tu
->qlock
);
2078 mutex_unlock(&tu
->ioctl_lock
);
2079 return result
> 0 ? result
: err
;
2082 static unsigned int snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
2085 struct snd_timer_user
*tu
;
2087 tu
= file
->private_data
;
2089 poll_wait(file
, &tu
->qchange_sleep
, wait
);
2093 mask
|= POLLIN
| POLLRDNORM
;
2094 if (tu
->disconnected
)
2100 #ifdef CONFIG_COMPAT
2101 #include "timer_compat.c"
2103 #define snd_timer_user_ioctl_compat NULL
2106 static const struct file_operations snd_timer_f_ops
=
2108 .owner
= THIS_MODULE
,
2109 .read
= snd_timer_user_read
,
2110 .open
= snd_timer_user_open
,
2111 .release
= snd_timer_user_release
,
2112 .llseek
= no_llseek
,
2113 .poll
= snd_timer_user_poll
,
2114 .unlocked_ioctl
= snd_timer_user_ioctl
,
2115 .compat_ioctl
= snd_timer_user_ioctl_compat
,
2116 .fasync
= snd_timer_user_fasync
,
2119 /* unregister the system timer */
2120 static void snd_timer_free_all(void)
2122 struct snd_timer
*timer
, *n
;
2124 list_for_each_entry_safe(timer
, n
, &snd_timer_list
, device_list
)
2125 snd_timer_free(timer
);
2128 static struct device timer_dev
;
2134 static int __init
alsa_timer_init(void)
2138 snd_device_initialize(&timer_dev
, NULL
);
2139 dev_set_name(&timer_dev
, "timer");
2141 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2142 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1,
2146 err
= snd_timer_register_system();
2148 pr_err("ALSA: unable to register system timer (%i)\n", err
);
2149 put_device(&timer_dev
);
2153 err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0,
2154 &snd_timer_f_ops
, NULL
, &timer_dev
);
2156 pr_err("ALSA: unable to register timer device (%i)\n", err
);
2157 snd_timer_free_all();
2158 put_device(&timer_dev
);
2162 snd_timer_proc_init();
2166 static void __exit
alsa_timer_exit(void)
2168 snd_unregister_device(&timer_dev
);
2169 snd_timer_free_all();
2170 put_device(&timer_dev
);
2171 snd_timer_proc_done();
2172 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2173 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
2177 module_init(alsa_timer_init
)
2178 module_exit(alsa_timer_exit
)