1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Timers abstract layer
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <linux/delay.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/time.h>
11 #include <linux/mutex.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/sched/signal.h>
16 #include <sound/core.h>
17 #include <sound/timer.h>
18 #include <sound/control.h>
19 #include <sound/info.h>
20 #include <sound/minors.h>
21 #include <sound/initval.h>
22 #include <linux/kmod.h>
25 #define SNDRV_TIMER_IFLG_PAUSED 0x00010000
26 #define SNDRV_TIMER_IFLG_DEAD 0x00020000
28 #if IS_ENABLED(CONFIG_SND_HRTIMER)
29 #define DEFAULT_TIMER_LIMIT 4
31 #define DEFAULT_TIMER_LIMIT 1
34 static int timer_limit
= DEFAULT_TIMER_LIMIT
;
35 static int timer_tstamp_monotonic
= 1;
36 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
37 MODULE_DESCRIPTION("ALSA timer interface");
38 MODULE_LICENSE("GPL");
39 module_param(timer_limit
, int, 0444);
40 MODULE_PARM_DESC(timer_limit
, "Maximum global timers in system.");
41 module_param(timer_tstamp_monotonic
, int, 0444);
42 MODULE_PARM_DESC(timer_tstamp_monotonic
, "Use posix monotonic clock source for timestamps (default).");
44 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR
, SNDRV_MINOR_TIMER
);
45 MODULE_ALIAS("devname:snd/timer");
47 struct snd_timer_user
{
48 struct snd_timer_instance
*timeri
;
49 int tread
; /* enhanced read with timestamps and events */
51 unsigned long overrun
;
57 struct snd_timer_read
*queue
;
58 struct snd_timer_tread
*tqueue
;
60 unsigned long last_resolution
;
62 struct timespec tstamp
; /* trigger tstamp */
63 wait_queue_head_t qchange_sleep
;
64 struct fasync_struct
*fasync
;
65 struct mutex ioctl_lock
;
69 static LIST_HEAD(snd_timer_list
);
71 /* list of slave instances */
72 static LIST_HEAD(snd_timer_slave_list
);
74 /* lock for slave active lists */
75 static DEFINE_SPINLOCK(slave_active_lock
);
77 static DEFINE_MUTEX(register_mutex
);
79 static int snd_timer_free(struct snd_timer
*timer
);
80 static int snd_timer_dev_free(struct snd_device
*device
);
81 static int snd_timer_dev_register(struct snd_device
*device
);
82 static int snd_timer_dev_disconnect(struct snd_device
*device
);
84 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
);
87 * create a timer instance with the given owner string.
88 * when timer is not NULL, increments the module counter
90 static struct snd_timer_instance
*snd_timer_instance_new(char *owner
,
91 struct snd_timer
*timer
)
93 struct snd_timer_instance
*timeri
;
94 timeri
= kzalloc(sizeof(*timeri
), GFP_KERNEL
);
97 timeri
->owner
= kstrdup(owner
, GFP_KERNEL
);
98 if (! timeri
->owner
) {
102 INIT_LIST_HEAD(&timeri
->open_list
);
103 INIT_LIST_HEAD(&timeri
->active_list
);
104 INIT_LIST_HEAD(&timeri
->ack_list
);
105 INIT_LIST_HEAD(&timeri
->slave_list_head
);
106 INIT_LIST_HEAD(&timeri
->slave_active_head
);
108 timeri
->timer
= timer
;
109 if (timer
&& !try_module_get(timer
->module
)) {
110 kfree(timeri
->owner
);
119 * find a timer instance from the given timer id
121 static struct snd_timer
*snd_timer_find(struct snd_timer_id
*tid
)
123 struct snd_timer
*timer
= NULL
;
125 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
126 if (timer
->tmr_class
!= tid
->dev_class
)
128 if ((timer
->tmr_class
== SNDRV_TIMER_CLASS_CARD
||
129 timer
->tmr_class
== SNDRV_TIMER_CLASS_PCM
) &&
130 (timer
->card
== NULL
||
131 timer
->card
->number
!= tid
->card
))
133 if (timer
->tmr_device
!= tid
->device
)
135 if (timer
->tmr_subdevice
!= tid
->subdevice
)
142 #ifdef CONFIG_MODULES
144 static void snd_timer_request(struct snd_timer_id
*tid
)
146 switch (tid
->dev_class
) {
147 case SNDRV_TIMER_CLASS_GLOBAL
:
148 if (tid
->device
< timer_limit
)
149 request_module("snd-timer-%i", tid
->device
);
151 case SNDRV_TIMER_CLASS_CARD
:
152 case SNDRV_TIMER_CLASS_PCM
:
153 if (tid
->card
< snd_ecards_limit
)
154 request_module("snd-card-%i", tid
->card
);
164 * look for a master instance matching with the slave id of the given slave.
165 * when found, relink the open_link of the slave.
167 * call this with register_mutex down.
169 static int snd_timer_check_slave(struct snd_timer_instance
*slave
)
171 struct snd_timer
*timer
;
172 struct snd_timer_instance
*master
;
174 /* FIXME: it's really dumb to look up all entries.. */
175 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
176 list_for_each_entry(master
, &timer
->open_list_head
, open_list
) {
177 if (slave
->slave_class
== master
->slave_class
&&
178 slave
->slave_id
== master
->slave_id
) {
179 if (master
->timer
->num_instances
>=
180 master
->timer
->max_instances
)
182 list_move_tail(&slave
->open_list
,
183 &master
->slave_list_head
);
184 master
->timer
->num_instances
++;
185 spin_lock_irq(&slave_active_lock
);
186 slave
->master
= master
;
187 slave
->timer
= master
->timer
;
188 spin_unlock_irq(&slave_active_lock
);
197 * look for slave instances matching with the slave id of the given master.
198 * when found, relink the open_link of slaves.
200 * call this with register_mutex down.
202 static int snd_timer_check_master(struct snd_timer_instance
*master
)
204 struct snd_timer_instance
*slave
, *tmp
;
206 /* check all pending slaves */
207 list_for_each_entry_safe(slave
, tmp
, &snd_timer_slave_list
, open_list
) {
208 if (slave
->slave_class
== master
->slave_class
&&
209 slave
->slave_id
== master
->slave_id
) {
210 if (master
->timer
->num_instances
>=
211 master
->timer
->max_instances
)
213 list_move_tail(&slave
->open_list
, &master
->slave_list_head
);
214 master
->timer
->num_instances
++;
215 spin_lock_irq(&slave_active_lock
);
216 spin_lock(&master
->timer
->lock
);
217 slave
->master
= master
;
218 slave
->timer
= master
->timer
;
219 if (slave
->flags
& SNDRV_TIMER_IFLG_RUNNING
)
220 list_add_tail(&slave
->active_list
,
221 &master
->slave_active_head
);
222 spin_unlock(&master
->timer
->lock
);
223 spin_unlock_irq(&slave_active_lock
);
229 static int snd_timer_close_locked(struct snd_timer_instance
*timeri
);
232 * open a timer instance
233 * when opening a master, the slave id must be here given.
235 int snd_timer_open(struct snd_timer_instance
**ti
,
236 char *owner
, struct snd_timer_id
*tid
,
237 unsigned int slave_id
)
239 struct snd_timer
*timer
;
240 struct snd_timer_instance
*timeri
= NULL
;
243 mutex_lock(®ister_mutex
);
244 if (tid
->dev_class
== SNDRV_TIMER_CLASS_SLAVE
) {
245 /* open a slave instance */
246 if (tid
->dev_sclass
<= SNDRV_TIMER_SCLASS_NONE
||
247 tid
->dev_sclass
> SNDRV_TIMER_SCLASS_OSS_SEQUENCER
) {
248 pr_debug("ALSA: timer: invalid slave class %i\n",
253 timeri
= snd_timer_instance_new(owner
, NULL
);
258 timeri
->slave_class
= tid
->dev_sclass
;
259 timeri
->slave_id
= tid
->device
;
260 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
261 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
262 err
= snd_timer_check_slave(timeri
);
264 snd_timer_close_locked(timeri
);
270 /* open a master instance */
271 timer
= snd_timer_find(tid
);
272 #ifdef CONFIG_MODULES
274 mutex_unlock(®ister_mutex
);
275 snd_timer_request(tid
);
276 mutex_lock(®ister_mutex
);
277 timer
= snd_timer_find(tid
);
284 if (!list_empty(&timer
->open_list_head
)) {
285 timeri
= list_entry(timer
->open_list_head
.next
,
286 struct snd_timer_instance
, open_list
);
287 if (timeri
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
293 if (timer
->num_instances
>= timer
->max_instances
) {
297 timeri
= snd_timer_instance_new(owner
, timer
);
302 /* take a card refcount for safe disconnection */
304 get_device(&timer
->card
->card_dev
);
305 timeri
->slave_class
= tid
->dev_sclass
;
306 timeri
->slave_id
= slave_id
;
308 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
) {
309 err
= timer
->hw
.open(timer
);
311 kfree(timeri
->owner
);
316 put_device(&timer
->card
->card_dev
);
317 module_put(timer
->module
);
322 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
323 timer
->num_instances
++;
324 err
= snd_timer_check_master(timeri
);
326 snd_timer_close_locked(timeri
);
331 mutex_unlock(®ister_mutex
);
335 EXPORT_SYMBOL(snd_timer_open
);
338 * close a timer instance
339 * call this with register_mutex down.
341 static int snd_timer_close_locked(struct snd_timer_instance
*timeri
)
343 struct snd_timer
*timer
= timeri
->timer
;
344 struct snd_timer_instance
*slave
, *tmp
;
347 spin_lock_irq(&timer
->lock
);
348 timeri
->flags
|= SNDRV_TIMER_IFLG_DEAD
;
349 spin_unlock_irq(&timer
->lock
);
352 list_del(&timeri
->open_list
);
354 /* force to stop the timer */
355 snd_timer_stop(timeri
);
358 timer
->num_instances
--;
359 /* wait, until the active callback is finished */
360 spin_lock_irq(&timer
->lock
);
361 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
362 spin_unlock_irq(&timer
->lock
);
364 spin_lock_irq(&timer
->lock
);
366 spin_unlock_irq(&timer
->lock
);
368 /* remove slave links */
369 spin_lock_irq(&slave_active_lock
);
370 spin_lock(&timer
->lock
);
371 list_for_each_entry_safe(slave
, tmp
, &timeri
->slave_list_head
,
373 list_move_tail(&slave
->open_list
, &snd_timer_slave_list
);
374 timer
->num_instances
--;
375 slave
->master
= NULL
;
377 list_del_init(&slave
->ack_list
);
378 list_del_init(&slave
->active_list
);
380 spin_unlock(&timer
->lock
);
381 spin_unlock_irq(&slave_active_lock
);
383 /* slave doesn't need to release timer resources below */
384 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
388 if (timeri
->private_free
)
389 timeri
->private_free(timeri
);
390 kfree(timeri
->owner
);
394 if (list_empty(&timer
->open_list_head
) && timer
->hw
.close
)
395 timer
->hw
.close(timer
);
396 /* release a card refcount for safe disconnection */
398 put_device(&timer
->card
->card_dev
);
399 module_put(timer
->module
);
406 * close a timer instance
408 int snd_timer_close(struct snd_timer_instance
*timeri
)
412 if (snd_BUG_ON(!timeri
))
415 mutex_lock(®ister_mutex
);
416 err
= snd_timer_close_locked(timeri
);
417 mutex_unlock(®ister_mutex
);
420 EXPORT_SYMBOL(snd_timer_close
);
422 static unsigned long snd_timer_hw_resolution(struct snd_timer
*timer
)
424 if (timer
->hw
.c_resolution
)
425 return timer
->hw
.c_resolution(timer
);
427 return timer
->hw
.resolution
;
430 unsigned long snd_timer_resolution(struct snd_timer_instance
*timeri
)
432 struct snd_timer
* timer
;
433 unsigned long ret
= 0;
438 timer
= timeri
->timer
;
440 spin_lock_irqsave(&timer
->lock
, flags
);
441 ret
= snd_timer_hw_resolution(timer
);
442 spin_unlock_irqrestore(&timer
->lock
, flags
);
446 EXPORT_SYMBOL(snd_timer_resolution
);
448 static void snd_timer_notify1(struct snd_timer_instance
*ti
, int event
)
450 struct snd_timer
*timer
= ti
->timer
;
451 unsigned long resolution
= 0;
452 struct snd_timer_instance
*ts
;
453 struct timespec tstamp
;
455 if (timer_tstamp_monotonic
)
456 ktime_get_ts(&tstamp
);
458 getnstimeofday(&tstamp
);
459 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_START
||
460 event
> SNDRV_TIMER_EVENT_PAUSE
))
463 (event
== SNDRV_TIMER_EVENT_START
||
464 event
== SNDRV_TIMER_EVENT_CONTINUE
))
465 resolution
= snd_timer_hw_resolution(timer
);
467 ti
->ccallback(ti
, event
, &tstamp
, resolution
);
468 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
472 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
474 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
476 ts
->ccallback(ts
, event
+ 100, &tstamp
, resolution
);
479 /* start/continue a master timer */
480 static int snd_timer_start1(struct snd_timer_instance
*timeri
,
481 bool start
, unsigned long ticks
)
483 struct snd_timer
*timer
;
487 timer
= timeri
->timer
;
491 spin_lock_irqsave(&timer
->lock
, flags
);
492 if (timeri
->flags
& SNDRV_TIMER_IFLG_DEAD
) {
496 if (timer
->card
&& timer
->card
->shutdown
) {
500 if (timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
501 SNDRV_TIMER_IFLG_START
)) {
507 timeri
->ticks
= timeri
->cticks
= ticks
;
508 else if (!timeri
->cticks
)
512 list_move_tail(&timeri
->active_list
, &timer
->active_list_head
);
513 if (timer
->running
) {
514 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
516 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
517 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
518 result
= 1; /* delayed start */
521 timer
->sticks
= ticks
;
522 timer
->hw
.start(timer
);
525 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
528 snd_timer_notify1(timeri
, start
? SNDRV_TIMER_EVENT_START
:
529 SNDRV_TIMER_EVENT_CONTINUE
);
531 spin_unlock_irqrestore(&timer
->lock
, flags
);
535 /* start/continue a slave timer */
536 static int snd_timer_start_slave(struct snd_timer_instance
*timeri
,
542 spin_lock_irqsave(&slave_active_lock
, flags
);
543 if (timeri
->flags
& SNDRV_TIMER_IFLG_DEAD
) {
547 if (timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
551 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
552 if (timeri
->master
&& timeri
->timer
) {
553 spin_lock(&timeri
->timer
->lock
);
554 list_add_tail(&timeri
->active_list
,
555 &timeri
->master
->slave_active_head
);
556 snd_timer_notify1(timeri
, start
? SNDRV_TIMER_EVENT_START
:
557 SNDRV_TIMER_EVENT_CONTINUE
);
558 spin_unlock(&timeri
->timer
->lock
);
560 err
= 1; /* delayed start */
562 spin_unlock_irqrestore(&slave_active_lock
, flags
);
566 /* stop/pause a master timer */
567 static int snd_timer_stop1(struct snd_timer_instance
*timeri
, bool stop
)
569 struct snd_timer
*timer
;
573 timer
= timeri
->timer
;
576 spin_lock_irqsave(&timer
->lock
, flags
);
577 if (!(timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
578 SNDRV_TIMER_IFLG_START
))) {
582 list_del_init(&timeri
->ack_list
);
583 list_del_init(&timeri
->active_list
);
584 if (timer
->card
&& timer
->card
->shutdown
)
587 timeri
->cticks
= timeri
->ticks
;
590 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
591 !(--timer
->running
)) {
592 timer
->hw
.stop(timer
);
593 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
594 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
595 snd_timer_reschedule(timer
, 0);
596 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
597 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
598 timer
->hw
.start(timer
);
602 timeri
->flags
&= ~(SNDRV_TIMER_IFLG_RUNNING
| SNDRV_TIMER_IFLG_START
);
604 timeri
->flags
&= ~SNDRV_TIMER_IFLG_PAUSED
;
606 timeri
->flags
|= SNDRV_TIMER_IFLG_PAUSED
;
607 snd_timer_notify1(timeri
, stop
? SNDRV_TIMER_EVENT_STOP
:
608 SNDRV_TIMER_EVENT_PAUSE
);
610 spin_unlock_irqrestore(&timer
->lock
, flags
);
614 /* stop/pause a slave timer */
615 static int snd_timer_stop_slave(struct snd_timer_instance
*timeri
, bool stop
)
619 spin_lock_irqsave(&slave_active_lock
, flags
);
620 if (!(timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
)) {
621 spin_unlock_irqrestore(&slave_active_lock
, flags
);
624 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
626 spin_lock(&timeri
->timer
->lock
);
627 list_del_init(&timeri
->ack_list
);
628 list_del_init(&timeri
->active_list
);
629 snd_timer_notify1(timeri
, stop
? SNDRV_TIMER_EVENT_STOP
:
630 SNDRV_TIMER_EVENT_PAUSE
);
631 spin_unlock(&timeri
->timer
->lock
);
633 spin_unlock_irqrestore(&slave_active_lock
, flags
);
638 * start the timer instance
640 int snd_timer_start(struct snd_timer_instance
*timeri
, unsigned int ticks
)
642 if (timeri
== NULL
|| ticks
< 1)
644 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
645 return snd_timer_start_slave(timeri
, true);
647 return snd_timer_start1(timeri
, true, ticks
);
649 EXPORT_SYMBOL(snd_timer_start
);
652 * stop the timer instance.
654 * do not call this from the timer callback!
656 int snd_timer_stop(struct snd_timer_instance
*timeri
)
658 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
659 return snd_timer_stop_slave(timeri
, true);
661 return snd_timer_stop1(timeri
, true);
663 EXPORT_SYMBOL(snd_timer_stop
);
666 * start again.. the tick is kept.
668 int snd_timer_continue(struct snd_timer_instance
*timeri
)
670 /* timer can continue only after pause */
671 if (!(timeri
->flags
& SNDRV_TIMER_IFLG_PAUSED
))
674 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
675 return snd_timer_start_slave(timeri
, false);
677 return snd_timer_start1(timeri
, false, 0);
679 EXPORT_SYMBOL(snd_timer_continue
);
682 * pause.. remember the ticks left
684 int snd_timer_pause(struct snd_timer_instance
* timeri
)
686 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
687 return snd_timer_stop_slave(timeri
, false);
689 return snd_timer_stop1(timeri
, false);
691 EXPORT_SYMBOL(snd_timer_pause
);
694 * reschedule the timer
696 * start pending instances and check the scheduling ticks.
697 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
699 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
)
701 struct snd_timer_instance
*ti
;
702 unsigned long ticks
= ~0UL;
704 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
705 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
706 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
707 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
710 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
711 if (ticks
> ti
->cticks
)
716 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
719 if (ticks
> timer
->hw
.ticks
)
720 ticks
= timer
->hw
.ticks
;
721 if (ticks_left
!= ticks
)
722 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
723 timer
->sticks
= ticks
;
726 /* call callbacks in timer ack list */
727 static void snd_timer_process_callbacks(struct snd_timer
*timer
,
728 struct list_head
*head
)
730 struct snd_timer_instance
*ti
;
731 unsigned long resolution
, ticks
;
733 while (!list_empty(head
)) {
734 ti
= list_first_entry(head
, struct snd_timer_instance
,
737 /* remove from ack_list and make empty */
738 list_del_init(&ti
->ack_list
);
740 if (!(ti
->flags
& SNDRV_TIMER_IFLG_DEAD
)) {
743 resolution
= ti
->resolution
;
744 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
745 spin_unlock(&timer
->lock
);
747 ti
->callback(ti
, resolution
, ticks
);
748 spin_lock(&timer
->lock
);
749 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
754 /* clear pending instances from ack list */
755 static void snd_timer_clear_callbacks(struct snd_timer
*timer
,
756 struct list_head
*head
)
760 spin_lock_irqsave(&timer
->lock
, flags
);
761 while (!list_empty(head
))
762 list_del_init(head
->next
);
763 spin_unlock_irqrestore(&timer
->lock
, flags
);
770 static void snd_timer_tasklet(unsigned long arg
)
772 struct snd_timer
*timer
= (struct snd_timer
*) arg
;
775 if (timer
->card
&& timer
->card
->shutdown
) {
776 snd_timer_clear_callbacks(timer
, &timer
->sack_list_head
);
780 spin_lock_irqsave(&timer
->lock
, flags
);
781 snd_timer_process_callbacks(timer
, &timer
->sack_list_head
);
782 spin_unlock_irqrestore(&timer
->lock
, flags
);
788 * ticks_left is usually equal to timer->sticks.
791 void snd_timer_interrupt(struct snd_timer
* timer
, unsigned long ticks_left
)
793 struct snd_timer_instance
*ti
, *ts
, *tmp
;
794 unsigned long resolution
;
795 struct list_head
*ack_list_head
;
802 if (timer
->card
&& timer
->card
->shutdown
) {
803 snd_timer_clear_callbacks(timer
, &timer
->ack_list_head
);
807 spin_lock_irqsave(&timer
->lock
, flags
);
809 /* remember the current resolution */
810 resolution
= snd_timer_hw_resolution(timer
);
812 /* loop for all active instances
813 * Here we cannot use list_for_each_entry because the active_list of a
814 * processed instance is relinked to done_list_head before the callback
817 list_for_each_entry_safe(ti
, tmp
, &timer
->active_list_head
,
819 if (ti
->flags
& SNDRV_TIMER_IFLG_DEAD
)
821 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
823 ti
->pticks
+= ticks_left
;
824 ti
->resolution
= resolution
;
825 if (ti
->cticks
< ticks_left
)
828 ti
->cticks
-= ticks_left
;
829 if (ti
->cticks
) /* not expired */
831 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
832 ti
->cticks
= ti
->ticks
;
834 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
836 list_del_init(&ti
->active_list
);
838 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
839 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
))
840 ack_list_head
= &timer
->ack_list_head
;
842 ack_list_head
= &timer
->sack_list_head
;
843 if (list_empty(&ti
->ack_list
))
844 list_add_tail(&ti
->ack_list
, ack_list_head
);
845 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
) {
846 ts
->pticks
= ti
->pticks
;
847 ts
->resolution
= resolution
;
848 if (list_empty(&ts
->ack_list
))
849 list_add_tail(&ts
->ack_list
, ack_list_head
);
852 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
853 snd_timer_reschedule(timer
, timer
->sticks
);
854 if (timer
->running
) {
855 if (timer
->hw
.flags
& SNDRV_TIMER_HW_STOP
) {
856 timer
->hw
.stop(timer
);
857 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
859 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_AUTO
) ||
860 (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
)) {
862 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
863 timer
->hw
.start(timer
);
866 timer
->hw
.stop(timer
);
869 /* now process all fast callbacks */
870 snd_timer_process_callbacks(timer
, &timer
->ack_list_head
);
872 /* do we have any slow callbacks? */
873 use_tasklet
= !list_empty(&timer
->sack_list_head
);
874 spin_unlock_irqrestore(&timer
->lock
, flags
);
877 tasklet_schedule(&timer
->task_queue
);
879 EXPORT_SYMBOL(snd_timer_interrupt
);
885 int snd_timer_new(struct snd_card
*card
, char *id
, struct snd_timer_id
*tid
,
886 struct snd_timer
**rtimer
)
888 struct snd_timer
*timer
;
890 static struct snd_device_ops ops
= {
891 .dev_free
= snd_timer_dev_free
,
892 .dev_register
= snd_timer_dev_register
,
893 .dev_disconnect
= snd_timer_dev_disconnect
,
896 if (snd_BUG_ON(!tid
))
898 if (tid
->dev_class
== SNDRV_TIMER_CLASS_CARD
||
899 tid
->dev_class
== SNDRV_TIMER_CLASS_PCM
) {
905 timer
= kzalloc(sizeof(*timer
), GFP_KERNEL
);
908 timer
->tmr_class
= tid
->dev_class
;
910 timer
->tmr_device
= tid
->device
;
911 timer
->tmr_subdevice
= tid
->subdevice
;
913 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
915 INIT_LIST_HEAD(&timer
->device_list
);
916 INIT_LIST_HEAD(&timer
->open_list_head
);
917 INIT_LIST_HEAD(&timer
->active_list_head
);
918 INIT_LIST_HEAD(&timer
->ack_list_head
);
919 INIT_LIST_HEAD(&timer
->sack_list_head
);
920 spin_lock_init(&timer
->lock
);
921 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
,
922 (unsigned long)timer
);
923 timer
->max_instances
= 1000; /* default limit per timer */
925 timer
->module
= card
->module
;
926 err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
);
928 snd_timer_free(timer
);
936 EXPORT_SYMBOL(snd_timer_new
);
938 static int snd_timer_free(struct snd_timer
*timer
)
943 mutex_lock(®ister_mutex
);
944 if (! list_empty(&timer
->open_list_head
)) {
945 struct list_head
*p
, *n
;
946 struct snd_timer_instance
*ti
;
947 pr_warn("ALSA: timer %p is busy?\n", timer
);
948 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
950 ti
= list_entry(p
, struct snd_timer_instance
, open_list
);
954 list_del(&timer
->device_list
);
955 mutex_unlock(®ister_mutex
);
957 if (timer
->private_free
)
958 timer
->private_free(timer
);
963 static int snd_timer_dev_free(struct snd_device
*device
)
965 struct snd_timer
*timer
= device
->device_data
;
966 return snd_timer_free(timer
);
969 static int snd_timer_dev_register(struct snd_device
*dev
)
971 struct snd_timer
*timer
= dev
->device_data
;
972 struct snd_timer
*timer1
;
974 if (snd_BUG_ON(!timer
|| !timer
->hw
.start
|| !timer
->hw
.stop
))
976 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
977 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
980 mutex_lock(®ister_mutex
);
981 list_for_each_entry(timer1
, &snd_timer_list
, device_list
) {
982 if (timer1
->tmr_class
> timer
->tmr_class
)
984 if (timer1
->tmr_class
< timer
->tmr_class
)
986 if (timer1
->card
&& timer
->card
) {
987 if (timer1
->card
->number
> timer
->card
->number
)
989 if (timer1
->card
->number
< timer
->card
->number
)
992 if (timer1
->tmr_device
> timer
->tmr_device
)
994 if (timer1
->tmr_device
< timer
->tmr_device
)
996 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
998 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
1001 mutex_unlock(®ister_mutex
);
1004 list_add_tail(&timer
->device_list
, &timer1
->device_list
);
1005 mutex_unlock(®ister_mutex
);
1009 static int snd_timer_dev_disconnect(struct snd_device
*device
)
1011 struct snd_timer
*timer
= device
->device_data
;
1012 struct snd_timer_instance
*ti
;
1014 mutex_lock(®ister_mutex
);
1015 list_del_init(&timer
->device_list
);
1016 /* wake up pending sleepers */
1017 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
) {
1021 mutex_unlock(®ister_mutex
);
1025 void snd_timer_notify(struct snd_timer
*timer
, int event
, struct timespec
*tstamp
)
1027 unsigned long flags
;
1028 unsigned long resolution
= 0;
1029 struct snd_timer_instance
*ti
, *ts
;
1031 if (timer
->card
&& timer
->card
->shutdown
)
1033 if (! (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
))
1035 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_MSTART
||
1036 event
> SNDRV_TIMER_EVENT_MRESUME
))
1038 spin_lock_irqsave(&timer
->lock
, flags
);
1039 if (event
== SNDRV_TIMER_EVENT_MSTART
||
1040 event
== SNDRV_TIMER_EVENT_MCONTINUE
||
1041 event
== SNDRV_TIMER_EVENT_MRESUME
)
1042 resolution
= snd_timer_hw_resolution(timer
);
1043 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
1045 ti
->ccallback(ti
, event
, tstamp
, resolution
);
1046 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
1048 ts
->ccallback(ts
, event
, tstamp
, resolution
);
1050 spin_unlock_irqrestore(&timer
->lock
, flags
);
1052 EXPORT_SYMBOL(snd_timer_notify
);
1055 * exported functions for global timers
1057 int snd_timer_global_new(char *id
, int device
, struct snd_timer
**rtimer
)
1059 struct snd_timer_id tid
;
1061 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
1062 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1064 tid
.device
= device
;
1066 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
1068 EXPORT_SYMBOL(snd_timer_global_new
);
1070 int snd_timer_global_free(struct snd_timer
*timer
)
1072 return snd_timer_free(timer
);
1074 EXPORT_SYMBOL(snd_timer_global_free
);
1076 int snd_timer_global_register(struct snd_timer
*timer
)
1078 struct snd_device dev
;
1080 memset(&dev
, 0, sizeof(dev
));
1081 dev
.device_data
= timer
;
1082 return snd_timer_dev_register(&dev
);
1084 EXPORT_SYMBOL(snd_timer_global_register
);
1090 struct snd_timer_system_private
{
1091 struct timer_list tlist
;
1092 struct snd_timer
*snd_timer
;
1093 unsigned long last_expires
;
1094 unsigned long last_jiffies
;
1095 unsigned long correction
;
1098 static void snd_timer_s_function(struct timer_list
*t
)
1100 struct snd_timer_system_private
*priv
= from_timer(priv
, t
,
1102 struct snd_timer
*timer
= priv
->snd_timer
;
1103 unsigned long jiff
= jiffies
;
1104 if (time_after(jiff
, priv
->last_expires
))
1105 priv
->correction
+= (long)jiff
- (long)priv
->last_expires
;
1106 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
1109 static int snd_timer_s_start(struct snd_timer
* timer
)
1111 struct snd_timer_system_private
*priv
;
1112 unsigned long njiff
;
1114 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1115 njiff
= (priv
->last_jiffies
= jiffies
);
1116 if (priv
->correction
> timer
->sticks
- 1) {
1117 priv
->correction
-= timer
->sticks
- 1;
1120 njiff
+= timer
->sticks
- priv
->correction
;
1121 priv
->correction
= 0;
1123 priv
->last_expires
= njiff
;
1124 mod_timer(&priv
->tlist
, njiff
);
1128 static int snd_timer_s_stop(struct snd_timer
* timer
)
1130 struct snd_timer_system_private
*priv
;
1133 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1134 del_timer(&priv
->tlist
);
1136 if (time_before(jiff
, priv
->last_expires
))
1137 timer
->sticks
= priv
->last_expires
- jiff
;
1140 priv
->correction
= 0;
1144 static int snd_timer_s_close(struct snd_timer
*timer
)
1146 struct snd_timer_system_private
*priv
;
1148 priv
= (struct snd_timer_system_private
*)timer
->private_data
;
1149 del_timer_sync(&priv
->tlist
);
1153 static struct snd_timer_hardware snd_timer_system
=
1155 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
1156 .resolution
= 1000000000L / HZ
,
1158 .close
= snd_timer_s_close
,
1159 .start
= snd_timer_s_start
,
1160 .stop
= snd_timer_s_stop
1163 static void snd_timer_free_system(struct snd_timer
*timer
)
1165 kfree(timer
->private_data
);
1168 static int snd_timer_register_system(void)
1170 struct snd_timer
*timer
;
1171 struct snd_timer_system_private
*priv
;
1174 err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
);
1177 strcpy(timer
->name
, "system timer");
1178 timer
->hw
= snd_timer_system
;
1179 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1181 snd_timer_free(timer
);
1184 priv
->snd_timer
= timer
;
1185 timer_setup(&priv
->tlist
, snd_timer_s_function
, 0);
1186 timer
->private_data
= priv
;
1187 timer
->private_free
= snd_timer_free_system
;
1188 return snd_timer_global_register(timer
);
1191 #ifdef CONFIG_SND_PROC_FS
1196 static void snd_timer_proc_read(struct snd_info_entry
*entry
,
1197 struct snd_info_buffer
*buffer
)
1199 struct snd_timer
*timer
;
1200 struct snd_timer_instance
*ti
;
1202 mutex_lock(®ister_mutex
);
1203 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
1204 if (timer
->card
&& timer
->card
->shutdown
)
1206 switch (timer
->tmr_class
) {
1207 case SNDRV_TIMER_CLASS_GLOBAL
:
1208 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1210 case SNDRV_TIMER_CLASS_CARD
:
1211 snd_iprintf(buffer
, "C%i-%i: ",
1212 timer
->card
->number
, timer
->tmr_device
);
1214 case SNDRV_TIMER_CLASS_PCM
:
1215 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
,
1216 timer
->tmr_device
, timer
->tmr_subdevice
);
1219 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
,
1220 timer
->card
? timer
->card
->number
: -1,
1221 timer
->tmr_device
, timer
->tmr_subdevice
);
1223 snd_iprintf(buffer
, "%s :", timer
->name
);
1224 if (timer
->hw
.resolution
)
1225 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)",
1226 timer
->hw
.resolution
/ 1000,
1227 timer
->hw
.resolution
% 1000,
1229 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1230 snd_iprintf(buffer
, " SLAVE");
1231 snd_iprintf(buffer
, "\n");
1232 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
)
1233 snd_iprintf(buffer
, " Client %s : %s\n",
1234 ti
->owner
? ti
->owner
: "unknown",
1235 ti
->flags
& (SNDRV_TIMER_IFLG_START
|
1236 SNDRV_TIMER_IFLG_RUNNING
)
1237 ? "running" : "stopped");
1239 mutex_unlock(®ister_mutex
);
1242 static struct snd_info_entry
*snd_timer_proc_entry
;
1244 static void __init
snd_timer_proc_init(void)
1246 struct snd_info_entry
*entry
;
1248 entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
);
1249 if (entry
!= NULL
) {
1250 entry
->c
.text
.read
= snd_timer_proc_read
;
1251 if (snd_info_register(entry
) < 0) {
1252 snd_info_free_entry(entry
);
1256 snd_timer_proc_entry
= entry
;
1259 static void __exit
snd_timer_proc_done(void)
1261 snd_info_free_entry(snd_timer_proc_entry
);
1263 #else /* !CONFIG_SND_PROC_FS */
1264 #define snd_timer_proc_init()
1265 #define snd_timer_proc_done()
1269 * USER SPACE interface
1272 static void snd_timer_user_interrupt(struct snd_timer_instance
*timeri
,
1273 unsigned long resolution
,
1274 unsigned long ticks
)
1276 struct snd_timer_user
*tu
= timeri
->callback_data
;
1277 struct snd_timer_read
*r
;
1280 spin_lock(&tu
->qlock
);
1281 if (tu
->qused
> 0) {
1282 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1283 r
= &tu
->queue
[prev
];
1284 if (r
->resolution
== resolution
) {
1289 if (tu
->qused
>= tu
->queue_size
) {
1292 r
= &tu
->queue
[tu
->qtail
++];
1293 tu
->qtail
%= tu
->queue_size
;
1294 r
->resolution
= resolution
;
1299 spin_unlock(&tu
->qlock
);
1300 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1301 wake_up(&tu
->qchange_sleep
);
1304 static void snd_timer_user_append_to_tqueue(struct snd_timer_user
*tu
,
1305 struct snd_timer_tread
*tread
)
1307 if (tu
->qused
>= tu
->queue_size
) {
1310 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1311 tu
->qtail
%= tu
->queue_size
;
1316 static void snd_timer_user_ccallback(struct snd_timer_instance
*timeri
,
1318 struct timespec
*tstamp
,
1319 unsigned long resolution
)
1321 struct snd_timer_user
*tu
= timeri
->callback_data
;
1322 struct snd_timer_tread r1
;
1323 unsigned long flags
;
1325 if (event
>= SNDRV_TIMER_EVENT_START
&&
1326 event
<= SNDRV_TIMER_EVENT_PAUSE
)
1327 tu
->tstamp
= *tstamp
;
1328 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1330 memset(&r1
, 0, sizeof(r1
));
1332 r1
.tstamp
= *tstamp
;
1333 r1
.val
= resolution
;
1334 spin_lock_irqsave(&tu
->qlock
, flags
);
1335 snd_timer_user_append_to_tqueue(tu
, &r1
);
1336 spin_unlock_irqrestore(&tu
->qlock
, flags
);
1337 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1338 wake_up(&tu
->qchange_sleep
);
1341 static void snd_timer_user_disconnect(struct snd_timer_instance
*timeri
)
1343 struct snd_timer_user
*tu
= timeri
->callback_data
;
1345 tu
->disconnected
= true;
1346 wake_up(&tu
->qchange_sleep
);
1349 static void snd_timer_user_tinterrupt(struct snd_timer_instance
*timeri
,
1350 unsigned long resolution
,
1351 unsigned long ticks
)
1353 struct snd_timer_user
*tu
= timeri
->callback_data
;
1354 struct snd_timer_tread
*r
, r1
;
1355 struct timespec tstamp
;
1356 int prev
, append
= 0;
1358 memset(&r1
, 0, sizeof(r1
));
1359 memset(&tstamp
, 0, sizeof(tstamp
));
1360 spin_lock(&tu
->qlock
);
1361 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
) |
1362 (1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1363 spin_unlock(&tu
->qlock
);
1366 if (tu
->last_resolution
!= resolution
|| ticks
> 0) {
1367 if (timer_tstamp_monotonic
)
1368 ktime_get_ts(&tstamp
);
1370 getnstimeofday(&tstamp
);
1372 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) &&
1373 tu
->last_resolution
!= resolution
) {
1374 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1376 r1
.val
= resolution
;
1377 snd_timer_user_append_to_tqueue(tu
, &r1
);
1378 tu
->last_resolution
= resolution
;
1381 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1385 if (tu
->qused
> 0) {
1386 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1387 r
= &tu
->tqueue
[prev
];
1388 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1395 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1398 snd_timer_user_append_to_tqueue(tu
, &r1
);
1401 spin_unlock(&tu
->qlock
);
1404 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1405 wake_up(&tu
->qchange_sleep
);
1408 static int realloc_user_queue(struct snd_timer_user
*tu
, int size
)
1410 struct snd_timer_read
*queue
= NULL
;
1411 struct snd_timer_tread
*tqueue
= NULL
;
1414 tqueue
= kcalloc(size
, sizeof(*tqueue
), GFP_KERNEL
);
1418 queue
= kcalloc(size
, sizeof(*queue
), GFP_KERNEL
);
1423 spin_lock_irq(&tu
->qlock
);
1426 tu
->queue_size
= size
;
1428 tu
->tqueue
= tqueue
;
1429 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1430 spin_unlock_irq(&tu
->qlock
);
1435 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1437 struct snd_timer_user
*tu
;
1440 err
= stream_open(inode
, file
);
1444 tu
= kzalloc(sizeof(*tu
), GFP_KERNEL
);
1447 spin_lock_init(&tu
->qlock
);
1448 init_waitqueue_head(&tu
->qchange_sleep
);
1449 mutex_init(&tu
->ioctl_lock
);
1451 if (realloc_user_queue(tu
, 128) < 0) {
1455 file
->private_data
= tu
;
1459 static int snd_timer_user_release(struct inode
*inode
, struct file
*file
)
1461 struct snd_timer_user
*tu
;
1463 if (file
->private_data
) {
1464 tu
= file
->private_data
;
1465 file
->private_data
= NULL
;
1466 mutex_lock(&tu
->ioctl_lock
);
1468 snd_timer_close(tu
->timeri
);
1469 mutex_unlock(&tu
->ioctl_lock
);
1477 static void snd_timer_user_zero_id(struct snd_timer_id
*id
)
1479 id
->dev_class
= SNDRV_TIMER_CLASS_NONE
;
1480 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1486 static void snd_timer_user_copy_id(struct snd_timer_id
*id
, struct snd_timer
*timer
)
1488 id
->dev_class
= timer
->tmr_class
;
1489 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1490 id
->card
= timer
->card
? timer
->card
->number
: -1;
1491 id
->device
= timer
->tmr_device
;
1492 id
->subdevice
= timer
->tmr_subdevice
;
1495 static int snd_timer_user_next_device(struct snd_timer_id __user
*_tid
)
1497 struct snd_timer_id id
;
1498 struct snd_timer
*timer
;
1499 struct list_head
*p
;
1501 if (copy_from_user(&id
, _tid
, sizeof(id
)))
1503 mutex_lock(®ister_mutex
);
1504 if (id
.dev_class
< 0) { /* first item */
1505 if (list_empty(&snd_timer_list
))
1506 snd_timer_user_zero_id(&id
);
1508 timer
= list_entry(snd_timer_list
.next
,
1509 struct snd_timer
, device_list
);
1510 snd_timer_user_copy_id(&id
, timer
);
1513 switch (id
.dev_class
) {
1514 case SNDRV_TIMER_CLASS_GLOBAL
:
1515 id
.device
= id
.device
< 0 ? 0 : id
.device
+ 1;
1516 list_for_each(p
, &snd_timer_list
) {
1517 timer
= list_entry(p
, struct snd_timer
, device_list
);
1518 if (timer
->tmr_class
> SNDRV_TIMER_CLASS_GLOBAL
) {
1519 snd_timer_user_copy_id(&id
, timer
);
1522 if (timer
->tmr_device
>= id
.device
) {
1523 snd_timer_user_copy_id(&id
, timer
);
1527 if (p
== &snd_timer_list
)
1528 snd_timer_user_zero_id(&id
);
1530 case SNDRV_TIMER_CLASS_CARD
:
1531 case SNDRV_TIMER_CLASS_PCM
:
1535 if (id
.device
< 0) {
1538 if (id
.subdevice
< 0)
1540 else if (id
.subdevice
< INT_MAX
)
1544 list_for_each(p
, &snd_timer_list
) {
1545 timer
= list_entry(p
, struct snd_timer
, device_list
);
1546 if (timer
->tmr_class
> id
.dev_class
) {
1547 snd_timer_user_copy_id(&id
, timer
);
1550 if (timer
->tmr_class
< id
.dev_class
)
1552 if (timer
->card
->number
> id
.card
) {
1553 snd_timer_user_copy_id(&id
, timer
);
1556 if (timer
->card
->number
< id
.card
)
1558 if (timer
->tmr_device
> id
.device
) {
1559 snd_timer_user_copy_id(&id
, timer
);
1562 if (timer
->tmr_device
< id
.device
)
1564 if (timer
->tmr_subdevice
> id
.subdevice
) {
1565 snd_timer_user_copy_id(&id
, timer
);
1568 if (timer
->tmr_subdevice
< id
.subdevice
)
1570 snd_timer_user_copy_id(&id
, timer
);
1573 if (p
== &snd_timer_list
)
1574 snd_timer_user_zero_id(&id
);
1577 snd_timer_user_zero_id(&id
);
1580 mutex_unlock(®ister_mutex
);
1581 if (copy_to_user(_tid
, &id
, sizeof(*_tid
)))
1586 static int snd_timer_user_ginfo(struct file
*file
,
1587 struct snd_timer_ginfo __user
*_ginfo
)
1589 struct snd_timer_ginfo
*ginfo
;
1590 struct snd_timer_id tid
;
1591 struct snd_timer
*t
;
1592 struct list_head
*p
;
1595 ginfo
= memdup_user(_ginfo
, sizeof(*ginfo
));
1597 return PTR_ERR(ginfo
);
1600 memset(ginfo
, 0, sizeof(*ginfo
));
1602 mutex_lock(®ister_mutex
);
1603 t
= snd_timer_find(&tid
);
1605 ginfo
->card
= t
->card
? t
->card
->number
: -1;
1606 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1607 ginfo
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1608 strlcpy(ginfo
->id
, t
->id
, sizeof(ginfo
->id
));
1609 strlcpy(ginfo
->name
, t
->name
, sizeof(ginfo
->name
));
1610 ginfo
->resolution
= t
->hw
.resolution
;
1611 if (t
->hw
.resolution_min
> 0) {
1612 ginfo
->resolution_min
= t
->hw
.resolution_min
;
1613 ginfo
->resolution_max
= t
->hw
.resolution_max
;
1615 list_for_each(p
, &t
->open_list_head
) {
1621 mutex_unlock(®ister_mutex
);
1622 if (err
>= 0 && copy_to_user(_ginfo
, ginfo
, sizeof(*ginfo
)))
1628 static int timer_set_gparams(struct snd_timer_gparams
*gparams
)
1630 struct snd_timer
*t
;
1633 mutex_lock(®ister_mutex
);
1634 t
= snd_timer_find(&gparams
->tid
);
1639 if (!list_empty(&t
->open_list_head
)) {
1643 if (!t
->hw
.set_period
) {
1647 err
= t
->hw
.set_period(t
, gparams
->period_num
, gparams
->period_den
);
1649 mutex_unlock(®ister_mutex
);
1653 static int snd_timer_user_gparams(struct file
*file
,
1654 struct snd_timer_gparams __user
*_gparams
)
1656 struct snd_timer_gparams gparams
;
1658 if (copy_from_user(&gparams
, _gparams
, sizeof(gparams
)))
1660 return timer_set_gparams(&gparams
);
1663 static int snd_timer_user_gstatus(struct file
*file
,
1664 struct snd_timer_gstatus __user
*_gstatus
)
1666 struct snd_timer_gstatus gstatus
;
1667 struct snd_timer_id tid
;
1668 struct snd_timer
*t
;
1671 if (copy_from_user(&gstatus
, _gstatus
, sizeof(gstatus
)))
1674 memset(&gstatus
, 0, sizeof(gstatus
));
1676 mutex_lock(®ister_mutex
);
1677 t
= snd_timer_find(&tid
);
1679 spin_lock_irq(&t
->lock
);
1680 gstatus
.resolution
= snd_timer_hw_resolution(t
);
1681 if (t
->hw
.precise_resolution
) {
1682 t
->hw
.precise_resolution(t
, &gstatus
.resolution_num
,
1683 &gstatus
.resolution_den
);
1685 gstatus
.resolution_num
= gstatus
.resolution
;
1686 gstatus
.resolution_den
= 1000000000uL;
1688 spin_unlock_irq(&t
->lock
);
1692 mutex_unlock(®ister_mutex
);
1693 if (err
>= 0 && copy_to_user(_gstatus
, &gstatus
, sizeof(gstatus
)))
1698 static int snd_timer_user_tselect(struct file
*file
,
1699 struct snd_timer_select __user
*_tselect
)
1701 struct snd_timer_user
*tu
;
1702 struct snd_timer_select tselect
;
1706 tu
= file
->private_data
;
1708 snd_timer_close(tu
->timeri
);
1711 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
))) {
1715 sprintf(str
, "application %i", current
->pid
);
1716 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1717 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1718 err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
);
1722 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1723 tu
->timeri
->callback
= tu
->tread
1724 ? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1725 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1726 tu
->timeri
->callback_data
= (void *)tu
;
1727 tu
->timeri
->disconnect
= snd_timer_user_disconnect
;
1733 static int snd_timer_user_info(struct file
*file
,
1734 struct snd_timer_info __user
*_info
)
1736 struct snd_timer_user
*tu
;
1737 struct snd_timer_info
*info
;
1738 struct snd_timer
*t
;
1741 tu
= file
->private_data
;
1744 t
= tu
->timeri
->timer
;
1748 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1751 info
->card
= t
->card
? t
->card
->number
: -1;
1752 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1753 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1754 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1755 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1756 info
->resolution
= t
->hw
.resolution
;
1757 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1763 static int snd_timer_user_params(struct file
*file
,
1764 struct snd_timer_params __user
*_params
)
1766 struct snd_timer_user
*tu
;
1767 struct snd_timer_params params
;
1768 struct snd_timer
*t
;
1771 tu
= file
->private_data
;
1774 t
= tu
->timeri
->timer
;
1777 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
1779 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)) {
1782 if (params
.ticks
< 1) {
1787 /* Don't allow resolution less than 1ms */
1788 resolution
= snd_timer_resolution(tu
->timeri
);
1789 resolution
*= params
.ticks
;
1790 if (resolution
< 1000000) {
1795 if (params
.queue_size
> 0 &&
1796 (params
.queue_size
< 32 || params
.queue_size
> 1024)) {
1800 if (params
.filter
& ~((1<<SNDRV_TIMER_EVENT_RESOLUTION
)|
1801 (1<<SNDRV_TIMER_EVENT_TICK
)|
1802 (1<<SNDRV_TIMER_EVENT_START
)|
1803 (1<<SNDRV_TIMER_EVENT_STOP
)|
1804 (1<<SNDRV_TIMER_EVENT_CONTINUE
)|
1805 (1<<SNDRV_TIMER_EVENT_PAUSE
)|
1806 (1<<SNDRV_TIMER_EVENT_SUSPEND
)|
1807 (1<<SNDRV_TIMER_EVENT_RESUME
)|
1808 (1<<SNDRV_TIMER_EVENT_MSTART
)|
1809 (1<<SNDRV_TIMER_EVENT_MSTOP
)|
1810 (1<<SNDRV_TIMER_EVENT_MCONTINUE
)|
1811 (1<<SNDRV_TIMER_EVENT_MPAUSE
)|
1812 (1<<SNDRV_TIMER_EVENT_MSUSPEND
)|
1813 (1<<SNDRV_TIMER_EVENT_MRESUME
))) {
1817 snd_timer_stop(tu
->timeri
);
1818 spin_lock_irq(&t
->lock
);
1819 tu
->timeri
->flags
&= ~(SNDRV_TIMER_IFLG_AUTO
|
1820 SNDRV_TIMER_IFLG_EXCLUSIVE
|
1821 SNDRV_TIMER_IFLG_EARLY_EVENT
);
1822 if (params
.flags
& SNDRV_TIMER_PSFLG_AUTO
)
1823 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
1824 if (params
.flags
& SNDRV_TIMER_PSFLG_EXCLUSIVE
)
1825 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EXCLUSIVE
;
1826 if (params
.flags
& SNDRV_TIMER_PSFLG_EARLY_EVENT
)
1827 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EARLY_EVENT
;
1828 spin_unlock_irq(&t
->lock
);
1829 if (params
.queue_size
> 0 &&
1830 (unsigned int)tu
->queue_size
!= params
.queue_size
) {
1831 err
= realloc_user_queue(tu
, params
.queue_size
);
1835 spin_lock_irq(&tu
->qlock
);
1836 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1837 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1839 struct snd_timer_tread tread
;
1840 memset(&tread
, 0, sizeof(tread
));
1841 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1842 tread
.tstamp
.tv_sec
= 0;
1843 tread
.tstamp
.tv_nsec
= 0;
1845 snd_timer_user_append_to_tqueue(tu
, &tread
);
1847 struct snd_timer_read
*r
= &tu
->queue
[0];
1854 tu
->filter
= params
.filter
;
1855 tu
->ticks
= params
.ticks
;
1856 spin_unlock_irq(&tu
->qlock
);
1859 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1864 static int snd_timer_user_status(struct file
*file
,
1865 struct snd_timer_status __user
*_status
)
1867 struct snd_timer_user
*tu
;
1868 struct snd_timer_status status
;
1870 tu
= file
->private_data
;
1873 memset(&status
, 0, sizeof(status
));
1874 status
.tstamp
= tu
->tstamp
;
1875 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1876 status
.lost
= tu
->timeri
->lost
;
1877 status
.overrun
= tu
->overrun
;
1878 spin_lock_irq(&tu
->qlock
);
1879 status
.queue
= tu
->qused
;
1880 spin_unlock_irq(&tu
->qlock
);
1881 if (copy_to_user(_status
, &status
, sizeof(status
)))
1886 static int snd_timer_user_start(struct file
*file
)
1889 struct snd_timer_user
*tu
;
1891 tu
= file
->private_data
;
1894 snd_timer_stop(tu
->timeri
);
1895 tu
->timeri
->lost
= 0;
1896 tu
->last_resolution
= 0;
1897 err
= snd_timer_start(tu
->timeri
, tu
->ticks
);
1903 static int snd_timer_user_stop(struct file
*file
)
1906 struct snd_timer_user
*tu
;
1908 tu
= file
->private_data
;
1911 err
= snd_timer_stop(tu
->timeri
);
1917 static int snd_timer_user_continue(struct file
*file
)
1920 struct snd_timer_user
*tu
;
1922 tu
= file
->private_data
;
1925 /* start timer instead of continue if it's not used before */
1926 if (!(tu
->timeri
->flags
& SNDRV_TIMER_IFLG_PAUSED
))
1927 return snd_timer_user_start(file
);
1928 tu
->timeri
->lost
= 0;
1929 err
= snd_timer_continue(tu
->timeri
);
1935 static int snd_timer_user_pause(struct file
*file
)
1938 struct snd_timer_user
*tu
;
1940 tu
= file
->private_data
;
1943 err
= snd_timer_pause(tu
->timeri
);
1950 SNDRV_TIMER_IOCTL_START_OLD
= _IO('T', 0x20),
1951 SNDRV_TIMER_IOCTL_STOP_OLD
= _IO('T', 0x21),
1952 SNDRV_TIMER_IOCTL_CONTINUE_OLD
= _IO('T', 0x22),
1953 SNDRV_TIMER_IOCTL_PAUSE_OLD
= _IO('T', 0x23),
1956 static long __snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1959 struct snd_timer_user
*tu
;
1960 void __user
*argp
= (void __user
*)arg
;
1961 int __user
*p
= argp
;
1963 tu
= file
->private_data
;
1965 case SNDRV_TIMER_IOCTL_PVERSION
:
1966 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1967 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1968 return snd_timer_user_next_device(argp
);
1969 case SNDRV_TIMER_IOCTL_TREAD
:
1971 int xarg
, old_tread
;
1973 if (tu
->timeri
) /* too late */
1975 if (get_user(xarg
, p
))
1977 old_tread
= tu
->tread
;
1978 tu
->tread
= xarg
? 1 : 0;
1979 if (tu
->tread
!= old_tread
&&
1980 realloc_user_queue(tu
, tu
->queue_size
) < 0) {
1981 tu
->tread
= old_tread
;
1986 case SNDRV_TIMER_IOCTL_GINFO
:
1987 return snd_timer_user_ginfo(file
, argp
);
1988 case SNDRV_TIMER_IOCTL_GPARAMS
:
1989 return snd_timer_user_gparams(file
, argp
);
1990 case SNDRV_TIMER_IOCTL_GSTATUS
:
1991 return snd_timer_user_gstatus(file
, argp
);
1992 case SNDRV_TIMER_IOCTL_SELECT
:
1993 return snd_timer_user_tselect(file
, argp
);
1994 case SNDRV_TIMER_IOCTL_INFO
:
1995 return snd_timer_user_info(file
, argp
);
1996 case SNDRV_TIMER_IOCTL_PARAMS
:
1997 return snd_timer_user_params(file
, argp
);
1998 case SNDRV_TIMER_IOCTL_STATUS
:
1999 return snd_timer_user_status(file
, argp
);
2000 case SNDRV_TIMER_IOCTL_START
:
2001 case SNDRV_TIMER_IOCTL_START_OLD
:
2002 return snd_timer_user_start(file
);
2003 case SNDRV_TIMER_IOCTL_STOP
:
2004 case SNDRV_TIMER_IOCTL_STOP_OLD
:
2005 return snd_timer_user_stop(file
);
2006 case SNDRV_TIMER_IOCTL_CONTINUE
:
2007 case SNDRV_TIMER_IOCTL_CONTINUE_OLD
:
2008 return snd_timer_user_continue(file
);
2009 case SNDRV_TIMER_IOCTL_PAUSE
:
2010 case SNDRV_TIMER_IOCTL_PAUSE_OLD
:
2011 return snd_timer_user_pause(file
);
2016 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
2019 struct snd_timer_user
*tu
= file
->private_data
;
2022 mutex_lock(&tu
->ioctl_lock
);
2023 ret
= __snd_timer_user_ioctl(file
, cmd
, arg
);
2024 mutex_unlock(&tu
->ioctl_lock
);
2028 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
2030 struct snd_timer_user
*tu
;
2032 tu
= file
->private_data
;
2033 return fasync_helper(fd
, file
, on
, &tu
->fasync
);
2036 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
,
2037 size_t count
, loff_t
*offset
)
2039 struct snd_timer_user
*tu
;
2040 long result
= 0, unit
;
2044 tu
= file
->private_data
;
2045 unit
= tu
->tread
? sizeof(struct snd_timer_tread
) : sizeof(struct snd_timer_read
);
2046 mutex_lock(&tu
->ioctl_lock
);
2047 spin_lock_irq(&tu
->qlock
);
2048 while ((long)count
- result
>= unit
) {
2049 while (!tu
->qused
) {
2050 wait_queue_entry_t wait
;
2052 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
2057 set_current_state(TASK_INTERRUPTIBLE
);
2058 init_waitqueue_entry(&wait
, current
);
2059 add_wait_queue(&tu
->qchange_sleep
, &wait
);
2061 spin_unlock_irq(&tu
->qlock
);
2062 mutex_unlock(&tu
->ioctl_lock
);
2064 mutex_lock(&tu
->ioctl_lock
);
2065 spin_lock_irq(&tu
->qlock
);
2067 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
2069 if (tu
->disconnected
) {
2073 if (signal_pending(current
)) {
2079 qhead
= tu
->qhead
++;
2080 tu
->qhead
%= tu
->queue_size
;
2082 spin_unlock_irq(&tu
->qlock
);
2085 if (copy_to_user(buffer
, &tu
->tqueue
[qhead
],
2086 sizeof(struct snd_timer_tread
)))
2089 if (copy_to_user(buffer
, &tu
->queue
[qhead
],
2090 sizeof(struct snd_timer_read
)))
2094 spin_lock_irq(&tu
->qlock
);
2101 spin_unlock_irq(&tu
->qlock
);
2102 mutex_unlock(&tu
->ioctl_lock
);
2103 return result
> 0 ? result
: err
;
2106 static __poll_t
snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
2109 struct snd_timer_user
*tu
;
2111 tu
= file
->private_data
;
2113 poll_wait(file
, &tu
->qchange_sleep
, wait
);
2116 spin_lock_irq(&tu
->qlock
);
2118 mask
|= EPOLLIN
| EPOLLRDNORM
;
2119 if (tu
->disconnected
)
2121 spin_unlock_irq(&tu
->qlock
);
2126 #ifdef CONFIG_COMPAT
2127 #include "timer_compat.c"
2129 #define snd_timer_user_ioctl_compat NULL
2132 static const struct file_operations snd_timer_f_ops
=
2134 .owner
= THIS_MODULE
,
2135 .read
= snd_timer_user_read
,
2136 .open
= snd_timer_user_open
,
2137 .release
= snd_timer_user_release
,
2138 .llseek
= no_llseek
,
2139 .poll
= snd_timer_user_poll
,
2140 .unlocked_ioctl
= snd_timer_user_ioctl
,
2141 .compat_ioctl
= snd_timer_user_ioctl_compat
,
2142 .fasync
= snd_timer_user_fasync
,
2145 /* unregister the system timer */
2146 static void snd_timer_free_all(void)
2148 struct snd_timer
*timer
, *n
;
2150 list_for_each_entry_safe(timer
, n
, &snd_timer_list
, device_list
)
2151 snd_timer_free(timer
);
2154 static struct device timer_dev
;
2160 static int __init
alsa_timer_init(void)
2164 snd_device_initialize(&timer_dev
, NULL
);
2165 dev_set_name(&timer_dev
, "timer");
2167 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2168 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1,
2172 err
= snd_timer_register_system();
2174 pr_err("ALSA: unable to register system timer (%i)\n", err
);
2178 err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0,
2179 &snd_timer_f_ops
, NULL
, &timer_dev
);
2181 pr_err("ALSA: unable to register timer device (%i)\n", err
);
2182 snd_timer_free_all();
2186 snd_timer_proc_init();
2190 put_device(&timer_dev
);
2194 static void __exit
alsa_timer_exit(void)
2196 snd_unregister_device(&timer_dev
);
2197 snd_timer_free_all();
2198 put_device(&timer_dev
);
2199 snd_timer_proc_done();
2200 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2201 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
2205 module_init(alsa_timer_init
)
2206 module_exit(alsa_timer_exit
)