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
,
243 struct device
**card_devp_to_put
);
246 * open a timer instance
247 * when opening a master, the slave id must be here given.
249 int snd_timer_open(struct snd_timer_instance
**ti
,
250 char *owner
, struct snd_timer_id
*tid
,
251 unsigned int slave_id
)
253 struct snd_timer
*timer
;
254 struct snd_timer_instance
*timeri
= NULL
;
255 struct device
*card_dev_to_put
= NULL
;
258 mutex_lock(®ister_mutex
);
259 if (tid
->dev_class
== SNDRV_TIMER_CLASS_SLAVE
) {
260 /* open a slave instance */
261 if (tid
->dev_sclass
<= SNDRV_TIMER_SCLASS_NONE
||
262 tid
->dev_sclass
> SNDRV_TIMER_SCLASS_OSS_SEQUENCER
) {
263 pr_debug("ALSA: timer: invalid slave class %i\n",
268 timeri
= snd_timer_instance_new(owner
, NULL
);
273 timeri
->slave_class
= tid
->dev_sclass
;
274 timeri
->slave_id
= tid
->device
;
275 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
276 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
277 err
= snd_timer_check_slave(timeri
);
279 snd_timer_close_locked(timeri
, &card_dev_to_put
);
285 /* open a master instance */
286 timer
= snd_timer_find(tid
);
287 #ifdef CONFIG_MODULES
289 mutex_unlock(®ister_mutex
);
290 snd_timer_request(tid
);
291 mutex_lock(®ister_mutex
);
292 timer
= snd_timer_find(tid
);
299 if (!list_empty(&timer
->open_list_head
)) {
300 timeri
= list_entry(timer
->open_list_head
.next
,
301 struct snd_timer_instance
, open_list
);
302 if (timeri
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
308 if (timer
->num_instances
>= timer
->max_instances
) {
312 timeri
= snd_timer_instance_new(owner
, timer
);
317 /* take a card refcount for safe disconnection */
319 get_device(&timer
->card
->card_dev
);
320 timeri
->slave_class
= tid
->dev_sclass
;
321 timeri
->slave_id
= slave_id
;
323 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
) {
324 err
= timer
->hw
.open(timer
);
326 kfree(timeri
->owner
);
331 card_dev_to_put
= &timer
->card
->card_dev
;
332 module_put(timer
->module
);
337 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
338 timer
->num_instances
++;
339 err
= snd_timer_check_master(timeri
);
341 snd_timer_close_locked(timeri
, &card_dev_to_put
);
346 mutex_unlock(®ister_mutex
);
347 /* put_device() is called after unlock for avoiding deadlock */
349 put_device(card_dev_to_put
);
353 EXPORT_SYMBOL(snd_timer_open
);
356 * close a timer instance
357 * call this with register_mutex down.
359 static int snd_timer_close_locked(struct snd_timer_instance
*timeri
,
360 struct device
**card_devp_to_put
)
362 struct snd_timer
*timer
= NULL
;
363 struct snd_timer_instance
*slave
, *tmp
;
365 list_del(&timeri
->open_list
);
367 /* force to stop the timer */
368 snd_timer_stop(timeri
);
370 timer
= timeri
->timer
;
372 timer
->num_instances
--;
373 /* wait, until the active callback is finished */
374 spin_lock_irq(&timer
->lock
);
375 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
376 spin_unlock_irq(&timer
->lock
);
378 spin_lock_irq(&timer
->lock
);
380 spin_unlock_irq(&timer
->lock
);
382 /* remove slave links */
383 spin_lock_irq(&slave_active_lock
);
384 spin_lock(&timer
->lock
);
385 list_for_each_entry_safe(slave
, tmp
, &timeri
->slave_list_head
,
387 list_move_tail(&slave
->open_list
, &snd_timer_slave_list
);
388 timer
->num_instances
--;
389 slave
->master
= NULL
;
391 list_del_init(&slave
->ack_list
);
392 list_del_init(&slave
->active_list
);
394 spin_unlock(&timer
->lock
);
395 spin_unlock_irq(&slave_active_lock
);
397 /* slave doesn't need to release timer resources below */
398 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
402 if (timeri
->private_free
)
403 timeri
->private_free(timeri
);
404 kfree(timeri
->owner
);
408 if (list_empty(&timer
->open_list_head
) && timer
->hw
.close
)
409 timer
->hw
.close(timer
);
410 /* release a card refcount for safe disconnection */
412 *card_devp_to_put
= &timer
->card
->card_dev
;
413 module_put(timer
->module
);
420 * close a timer instance
422 int snd_timer_close(struct snd_timer_instance
*timeri
)
424 struct device
*card_dev_to_put
= NULL
;
427 if (snd_BUG_ON(!timeri
))
430 mutex_lock(®ister_mutex
);
431 err
= snd_timer_close_locked(timeri
, &card_dev_to_put
);
432 mutex_unlock(®ister_mutex
);
433 /* put_device() is called after unlock for avoiding deadlock */
435 put_device(card_dev_to_put
);
438 EXPORT_SYMBOL(snd_timer_close
);
440 unsigned long snd_timer_resolution(struct snd_timer_instance
*timeri
)
442 struct snd_timer
* timer
;
446 if ((timer
= timeri
->timer
) != NULL
) {
447 if (timer
->hw
.c_resolution
)
448 return timer
->hw
.c_resolution(timer
);
449 return timer
->hw
.resolution
;
453 EXPORT_SYMBOL(snd_timer_resolution
);
455 static void snd_timer_notify1(struct snd_timer_instance
*ti
, int event
)
457 struct snd_timer
*timer
;
458 unsigned long resolution
= 0;
459 struct snd_timer_instance
*ts
;
460 struct timespec tstamp
;
462 if (timer_tstamp_monotonic
)
463 ktime_get_ts(&tstamp
);
465 getnstimeofday(&tstamp
);
466 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_START
||
467 event
> SNDRV_TIMER_EVENT_PAUSE
))
469 if (event
== SNDRV_TIMER_EVENT_START
||
470 event
== SNDRV_TIMER_EVENT_CONTINUE
)
471 resolution
= snd_timer_resolution(ti
);
473 ti
->ccallback(ti
, event
, &tstamp
, resolution
);
474 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
479 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
481 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
483 ts
->ccallback(ts
, event
+ 100, &tstamp
, resolution
);
486 /* start/continue a master timer */
487 static int snd_timer_start1(struct snd_timer_instance
*timeri
,
488 bool start
, unsigned long ticks
)
490 struct snd_timer
*timer
;
494 timer
= timeri
->timer
;
498 spin_lock_irqsave(&timer
->lock
, flags
);
499 if (timer
->card
&& timer
->card
->shutdown
) {
503 if (timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
504 SNDRV_TIMER_IFLG_START
)) {
510 timeri
->ticks
= timeri
->cticks
= ticks
;
511 else if (!timeri
->cticks
)
515 list_move_tail(&timeri
->active_list
, &timer
->active_list_head
);
516 if (timer
->running
) {
517 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
519 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
520 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
521 result
= 1; /* delayed start */
524 timer
->sticks
= ticks
;
525 timer
->hw
.start(timer
);
528 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
531 snd_timer_notify1(timeri
, start
? SNDRV_TIMER_EVENT_START
:
532 SNDRV_TIMER_EVENT_CONTINUE
);
534 spin_unlock_irqrestore(&timer
->lock
, flags
);
538 /* start/continue a slave timer */
539 static int snd_timer_start_slave(struct snd_timer_instance
*timeri
,
544 spin_lock_irqsave(&slave_active_lock
, flags
);
545 if (timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
546 spin_unlock_irqrestore(&slave_active_lock
, flags
);
549 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
550 if (timeri
->master
&& timeri
->timer
) {
551 spin_lock(&timeri
->timer
->lock
);
552 list_add_tail(&timeri
->active_list
,
553 &timeri
->master
->slave_active_head
);
554 snd_timer_notify1(timeri
, start
? SNDRV_TIMER_EVENT_START
:
555 SNDRV_TIMER_EVENT_CONTINUE
);
556 spin_unlock(&timeri
->timer
->lock
);
558 spin_unlock_irqrestore(&slave_active_lock
, flags
);
559 return 1; /* delayed start */
562 /* stop/pause a master timer */
563 static int snd_timer_stop1(struct snd_timer_instance
*timeri
, bool stop
)
565 struct snd_timer
*timer
;
569 timer
= timeri
->timer
;
572 spin_lock_irqsave(&timer
->lock
, flags
);
573 if (!(timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
574 SNDRV_TIMER_IFLG_START
))) {
578 list_del_init(&timeri
->ack_list
);
579 list_del_init(&timeri
->active_list
);
580 if (timer
->card
&& timer
->card
->shutdown
)
583 timeri
->cticks
= timeri
->ticks
;
586 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
587 !(--timer
->running
)) {
588 timer
->hw
.stop(timer
);
589 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
590 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
591 snd_timer_reschedule(timer
, 0);
592 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
593 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
594 timer
->hw
.start(timer
);
598 timeri
->flags
&= ~(SNDRV_TIMER_IFLG_RUNNING
| SNDRV_TIMER_IFLG_START
);
600 timeri
->flags
&= ~SNDRV_TIMER_IFLG_PAUSED
;
602 timeri
->flags
|= SNDRV_TIMER_IFLG_PAUSED
;
603 snd_timer_notify1(timeri
, stop
? SNDRV_TIMER_EVENT_STOP
:
604 SNDRV_TIMER_EVENT_PAUSE
);
606 spin_unlock_irqrestore(&timer
->lock
, flags
);
610 /* stop/pause a slave timer */
611 static int snd_timer_stop_slave(struct snd_timer_instance
*timeri
, bool stop
)
615 spin_lock_irqsave(&slave_active_lock
, flags
);
616 if (!(timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
)) {
617 spin_unlock_irqrestore(&slave_active_lock
, flags
);
620 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
622 spin_lock(&timeri
->timer
->lock
);
623 list_del_init(&timeri
->ack_list
);
624 list_del_init(&timeri
->active_list
);
625 snd_timer_notify1(timeri
, stop
? SNDRV_TIMER_EVENT_STOP
:
626 SNDRV_TIMER_EVENT_PAUSE
);
627 spin_unlock(&timeri
->timer
->lock
);
629 spin_unlock_irqrestore(&slave_active_lock
, flags
);
634 * start the timer instance
636 int snd_timer_start(struct snd_timer_instance
*timeri
, unsigned int ticks
)
638 if (timeri
== NULL
|| ticks
< 1)
640 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
641 return snd_timer_start_slave(timeri
, true);
643 return snd_timer_start1(timeri
, true, ticks
);
645 EXPORT_SYMBOL(snd_timer_start
);
648 * stop the timer instance.
650 * do not call this from the timer callback!
652 int snd_timer_stop(struct snd_timer_instance
*timeri
)
654 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
655 return snd_timer_stop_slave(timeri
, true);
657 return snd_timer_stop1(timeri
, true);
659 EXPORT_SYMBOL(snd_timer_stop
);
662 * start again.. the tick is kept.
664 int snd_timer_continue(struct snd_timer_instance
*timeri
)
666 /* timer can continue only after pause */
667 if (!(timeri
->flags
& SNDRV_TIMER_IFLG_PAUSED
))
670 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
671 return snd_timer_start_slave(timeri
, false);
673 return snd_timer_start1(timeri
, false, 0);
675 EXPORT_SYMBOL(snd_timer_continue
);
678 * pause.. remember the ticks left
680 int snd_timer_pause(struct snd_timer_instance
* timeri
)
682 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
683 return snd_timer_stop_slave(timeri
, false);
685 return snd_timer_stop1(timeri
, false);
687 EXPORT_SYMBOL(snd_timer_pause
);
690 * reschedule the timer
692 * start pending instances and check the scheduling ticks.
693 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
695 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
)
697 struct snd_timer_instance
*ti
;
698 unsigned long ticks
= ~0UL;
700 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
701 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
702 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
703 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
706 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
707 if (ticks
> ti
->cticks
)
712 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
715 if (ticks
> timer
->hw
.ticks
)
716 ticks
= timer
->hw
.ticks
;
717 if (ticks_left
!= ticks
)
718 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
719 timer
->sticks
= ticks
;
726 static void snd_timer_tasklet(unsigned long arg
)
728 struct snd_timer
*timer
= (struct snd_timer
*) arg
;
729 struct snd_timer_instance
*ti
;
731 unsigned long resolution
, ticks
;
734 if (timer
->card
&& timer
->card
->shutdown
)
737 spin_lock_irqsave(&timer
->lock
, flags
);
738 /* now process all callbacks */
739 while (!list_empty(&timer
->sack_list_head
)) {
740 p
= timer
->sack_list_head
.next
; /* get first item */
741 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
743 /* remove from ack_list and make empty */
748 resolution
= ti
->resolution
;
750 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
751 spin_unlock(&timer
->lock
);
753 ti
->callback(ti
, resolution
, ticks
);
754 spin_lock(&timer
->lock
);
755 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
757 spin_unlock_irqrestore(&timer
->lock
, flags
);
763 * ticks_left is usually equal to timer->sticks.
766 void snd_timer_interrupt(struct snd_timer
* timer
, unsigned long ticks_left
)
768 struct snd_timer_instance
*ti
, *ts
, *tmp
;
769 unsigned long resolution
, ticks
;
770 struct list_head
*p
, *ack_list_head
;
777 if (timer
->card
&& timer
->card
->shutdown
)
780 spin_lock_irqsave(&timer
->lock
, flags
);
782 /* remember the current resolution */
783 if (timer
->hw
.c_resolution
)
784 resolution
= timer
->hw
.c_resolution(timer
);
786 resolution
= timer
->hw
.resolution
;
788 /* loop for all active instances
789 * Here we cannot use list_for_each_entry because the active_list of a
790 * processed instance is relinked to done_list_head before the callback
793 list_for_each_entry_safe(ti
, tmp
, &timer
->active_list_head
,
795 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
797 ti
->pticks
+= ticks_left
;
798 ti
->resolution
= resolution
;
799 if (ti
->cticks
< ticks_left
)
802 ti
->cticks
-= ticks_left
;
803 if (ti
->cticks
) /* not expired */
805 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
806 ti
->cticks
= ti
->ticks
;
808 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
810 list_del_init(&ti
->active_list
);
812 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
813 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
))
814 ack_list_head
= &timer
->ack_list_head
;
816 ack_list_head
= &timer
->sack_list_head
;
817 if (list_empty(&ti
->ack_list
))
818 list_add_tail(&ti
->ack_list
, ack_list_head
);
819 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
) {
820 ts
->pticks
= ti
->pticks
;
821 ts
->resolution
= resolution
;
822 if (list_empty(&ts
->ack_list
))
823 list_add_tail(&ts
->ack_list
, ack_list_head
);
826 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
827 snd_timer_reschedule(timer
, timer
->sticks
);
828 if (timer
->running
) {
829 if (timer
->hw
.flags
& SNDRV_TIMER_HW_STOP
) {
830 timer
->hw
.stop(timer
);
831 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
833 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_AUTO
) ||
834 (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
)) {
836 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
837 timer
->hw
.start(timer
);
840 timer
->hw
.stop(timer
);
843 /* now process all fast callbacks */
844 while (!list_empty(&timer
->ack_list_head
)) {
845 p
= timer
->ack_list_head
.next
; /* get first item */
846 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
848 /* remove from ack_list and make empty */
854 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
855 spin_unlock(&timer
->lock
);
857 ti
->callback(ti
, resolution
, ticks
);
858 spin_lock(&timer
->lock
);
859 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
862 /* do we have any slow callbacks? */
863 use_tasklet
= !list_empty(&timer
->sack_list_head
);
864 spin_unlock_irqrestore(&timer
->lock
, flags
);
867 tasklet_schedule(&timer
->task_queue
);
869 EXPORT_SYMBOL(snd_timer_interrupt
);
875 int snd_timer_new(struct snd_card
*card
, char *id
, struct snd_timer_id
*tid
,
876 struct snd_timer
**rtimer
)
878 struct snd_timer
*timer
;
880 static struct snd_device_ops ops
= {
881 .dev_free
= snd_timer_dev_free
,
882 .dev_register
= snd_timer_dev_register
,
883 .dev_disconnect
= snd_timer_dev_disconnect
,
886 if (snd_BUG_ON(!tid
))
890 timer
= kzalloc(sizeof(*timer
), GFP_KERNEL
);
893 timer
->tmr_class
= tid
->dev_class
;
895 timer
->tmr_device
= tid
->device
;
896 timer
->tmr_subdevice
= tid
->subdevice
;
898 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
900 INIT_LIST_HEAD(&timer
->device_list
);
901 INIT_LIST_HEAD(&timer
->open_list_head
);
902 INIT_LIST_HEAD(&timer
->active_list_head
);
903 INIT_LIST_HEAD(&timer
->ack_list_head
);
904 INIT_LIST_HEAD(&timer
->sack_list_head
);
905 spin_lock_init(&timer
->lock
);
906 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
,
907 (unsigned long)timer
);
908 timer
->max_instances
= 1000; /* default limit per timer */
910 timer
->module
= card
->module
;
911 err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
);
913 snd_timer_free(timer
);
921 EXPORT_SYMBOL(snd_timer_new
);
923 static int snd_timer_free(struct snd_timer
*timer
)
928 mutex_lock(®ister_mutex
);
929 if (! list_empty(&timer
->open_list_head
)) {
930 struct list_head
*p
, *n
;
931 struct snd_timer_instance
*ti
;
932 pr_warn("ALSA: timer %p is busy?\n", timer
);
933 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
935 ti
= list_entry(p
, struct snd_timer_instance
, open_list
);
939 list_del(&timer
->device_list
);
940 mutex_unlock(®ister_mutex
);
942 if (timer
->private_free
)
943 timer
->private_free(timer
);
948 static int snd_timer_dev_free(struct snd_device
*device
)
950 struct snd_timer
*timer
= device
->device_data
;
951 return snd_timer_free(timer
);
954 static int snd_timer_dev_register(struct snd_device
*dev
)
956 struct snd_timer
*timer
= dev
->device_data
;
957 struct snd_timer
*timer1
;
959 if (snd_BUG_ON(!timer
|| !timer
->hw
.start
|| !timer
->hw
.stop
))
961 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
962 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
965 mutex_lock(®ister_mutex
);
966 list_for_each_entry(timer1
, &snd_timer_list
, device_list
) {
967 if (timer1
->tmr_class
> timer
->tmr_class
)
969 if (timer1
->tmr_class
< timer
->tmr_class
)
971 if (timer1
->card
&& timer
->card
) {
972 if (timer1
->card
->number
> timer
->card
->number
)
974 if (timer1
->card
->number
< timer
->card
->number
)
977 if (timer1
->tmr_device
> timer
->tmr_device
)
979 if (timer1
->tmr_device
< timer
->tmr_device
)
981 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
983 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
986 mutex_unlock(®ister_mutex
);
989 list_add_tail(&timer
->device_list
, &timer1
->device_list
);
990 mutex_unlock(®ister_mutex
);
994 static int snd_timer_dev_disconnect(struct snd_device
*device
)
996 struct snd_timer
*timer
= device
->device_data
;
997 struct snd_timer_instance
*ti
;
999 mutex_lock(®ister_mutex
);
1000 list_del_init(&timer
->device_list
);
1001 /* wake up pending sleepers */
1002 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
) {
1006 mutex_unlock(®ister_mutex
);
1010 void snd_timer_notify(struct snd_timer
*timer
, int event
, struct timespec
*tstamp
)
1012 unsigned long flags
;
1013 unsigned long resolution
= 0;
1014 struct snd_timer_instance
*ti
, *ts
;
1016 if (timer
->card
&& timer
->card
->shutdown
)
1018 if (! (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
))
1020 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_MSTART
||
1021 event
> SNDRV_TIMER_EVENT_MRESUME
))
1023 spin_lock_irqsave(&timer
->lock
, flags
);
1024 if (event
== SNDRV_TIMER_EVENT_MSTART
||
1025 event
== SNDRV_TIMER_EVENT_MCONTINUE
||
1026 event
== SNDRV_TIMER_EVENT_MRESUME
) {
1027 if (timer
->hw
.c_resolution
)
1028 resolution
= timer
->hw
.c_resolution(timer
);
1030 resolution
= timer
->hw
.resolution
;
1032 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
1034 ti
->ccallback(ti
, event
, tstamp
, resolution
);
1035 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
1037 ts
->ccallback(ts
, event
, tstamp
, resolution
);
1039 spin_unlock_irqrestore(&timer
->lock
, flags
);
1041 EXPORT_SYMBOL(snd_timer_notify
);
1044 * exported functions for global timers
1046 int snd_timer_global_new(char *id
, int device
, struct snd_timer
**rtimer
)
1048 struct snd_timer_id tid
;
1050 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
1051 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1053 tid
.device
= device
;
1055 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
1057 EXPORT_SYMBOL(snd_timer_global_new
);
1059 int snd_timer_global_free(struct snd_timer
*timer
)
1061 return snd_timer_free(timer
);
1063 EXPORT_SYMBOL(snd_timer_global_free
);
1065 int snd_timer_global_register(struct snd_timer
*timer
)
1067 struct snd_device dev
;
1069 memset(&dev
, 0, sizeof(dev
));
1070 dev
.device_data
= timer
;
1071 return snd_timer_dev_register(&dev
);
1073 EXPORT_SYMBOL(snd_timer_global_register
);
1079 struct snd_timer_system_private
{
1080 struct timer_list tlist
;
1081 unsigned long last_expires
;
1082 unsigned long last_jiffies
;
1083 unsigned long correction
;
1086 static void snd_timer_s_function(unsigned long data
)
1088 struct snd_timer
*timer
= (struct snd_timer
*)data
;
1089 struct snd_timer_system_private
*priv
= timer
->private_data
;
1090 unsigned long jiff
= jiffies
;
1091 if (time_after(jiff
, priv
->last_expires
))
1092 priv
->correction
+= (long)jiff
- (long)priv
->last_expires
;
1093 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
1096 static int snd_timer_s_start(struct snd_timer
* timer
)
1098 struct snd_timer_system_private
*priv
;
1099 unsigned long njiff
;
1101 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1102 njiff
= (priv
->last_jiffies
= jiffies
);
1103 if (priv
->correction
> timer
->sticks
- 1) {
1104 priv
->correction
-= timer
->sticks
- 1;
1107 njiff
+= timer
->sticks
- priv
->correction
;
1108 priv
->correction
= 0;
1110 priv
->last_expires
= njiff
;
1111 mod_timer(&priv
->tlist
, njiff
);
1115 static int snd_timer_s_stop(struct snd_timer
* timer
)
1117 struct snd_timer_system_private
*priv
;
1120 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1121 del_timer(&priv
->tlist
);
1123 if (time_before(jiff
, priv
->last_expires
))
1124 timer
->sticks
= priv
->last_expires
- jiff
;
1127 priv
->correction
= 0;
1131 static int snd_timer_s_close(struct snd_timer
*timer
)
1133 struct snd_timer_system_private
*priv
;
1135 priv
= (struct snd_timer_system_private
*)timer
->private_data
;
1136 del_timer_sync(&priv
->tlist
);
1140 static struct snd_timer_hardware snd_timer_system
=
1142 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
1143 .resolution
= 1000000000L / HZ
,
1145 .close
= snd_timer_s_close
,
1146 .start
= snd_timer_s_start
,
1147 .stop
= snd_timer_s_stop
1150 static void snd_timer_free_system(struct snd_timer
*timer
)
1152 kfree(timer
->private_data
);
1155 static int snd_timer_register_system(void)
1157 struct snd_timer
*timer
;
1158 struct snd_timer_system_private
*priv
;
1161 err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
);
1164 strcpy(timer
->name
, "system timer");
1165 timer
->hw
= snd_timer_system
;
1166 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1168 snd_timer_free(timer
);
1171 setup_timer(&priv
->tlist
, snd_timer_s_function
, (unsigned long) timer
);
1172 timer
->private_data
= priv
;
1173 timer
->private_free
= snd_timer_free_system
;
1174 return snd_timer_global_register(timer
);
1177 #ifdef CONFIG_SND_PROC_FS
1182 static void snd_timer_proc_read(struct snd_info_entry
*entry
,
1183 struct snd_info_buffer
*buffer
)
1185 struct snd_timer
*timer
;
1186 struct snd_timer_instance
*ti
;
1188 mutex_lock(®ister_mutex
);
1189 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
1190 if (timer
->card
&& timer
->card
->shutdown
)
1192 switch (timer
->tmr_class
) {
1193 case SNDRV_TIMER_CLASS_GLOBAL
:
1194 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1196 case SNDRV_TIMER_CLASS_CARD
:
1197 snd_iprintf(buffer
, "C%i-%i: ",
1198 timer
->card
->number
, timer
->tmr_device
);
1200 case SNDRV_TIMER_CLASS_PCM
:
1201 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
,
1202 timer
->tmr_device
, timer
->tmr_subdevice
);
1205 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
,
1206 timer
->card
? timer
->card
->number
: -1,
1207 timer
->tmr_device
, timer
->tmr_subdevice
);
1209 snd_iprintf(buffer
, "%s :", timer
->name
);
1210 if (timer
->hw
.resolution
)
1211 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)",
1212 timer
->hw
.resolution
/ 1000,
1213 timer
->hw
.resolution
% 1000,
1215 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1216 snd_iprintf(buffer
, " SLAVE");
1217 snd_iprintf(buffer
, "\n");
1218 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
)
1219 snd_iprintf(buffer
, " Client %s : %s\n",
1220 ti
->owner
? ti
->owner
: "unknown",
1221 ti
->flags
& (SNDRV_TIMER_IFLG_START
|
1222 SNDRV_TIMER_IFLG_RUNNING
)
1223 ? "running" : "stopped");
1225 mutex_unlock(®ister_mutex
);
1228 static struct snd_info_entry
*snd_timer_proc_entry
;
1230 static void __init
snd_timer_proc_init(void)
1232 struct snd_info_entry
*entry
;
1234 entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
);
1235 if (entry
!= NULL
) {
1236 entry
->c
.text
.read
= snd_timer_proc_read
;
1237 if (snd_info_register(entry
) < 0) {
1238 snd_info_free_entry(entry
);
1242 snd_timer_proc_entry
= entry
;
1245 static void __exit
snd_timer_proc_done(void)
1247 snd_info_free_entry(snd_timer_proc_entry
);
1249 #else /* !CONFIG_SND_PROC_FS */
1250 #define snd_timer_proc_init()
1251 #define snd_timer_proc_done()
1255 * USER SPACE interface
1258 static void snd_timer_user_interrupt(struct snd_timer_instance
*timeri
,
1259 unsigned long resolution
,
1260 unsigned long ticks
)
1262 struct snd_timer_user
*tu
= timeri
->callback_data
;
1263 struct snd_timer_read
*r
;
1266 spin_lock(&tu
->qlock
);
1267 if (tu
->qused
> 0) {
1268 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1269 r
= &tu
->queue
[prev
];
1270 if (r
->resolution
== resolution
) {
1275 if (tu
->qused
>= tu
->queue_size
) {
1278 r
= &tu
->queue
[tu
->qtail
++];
1279 tu
->qtail
%= tu
->queue_size
;
1280 r
->resolution
= resolution
;
1285 spin_unlock(&tu
->qlock
);
1286 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1287 wake_up(&tu
->qchange_sleep
);
1290 static void snd_timer_user_append_to_tqueue(struct snd_timer_user
*tu
,
1291 struct snd_timer_tread
*tread
)
1293 if (tu
->qused
>= tu
->queue_size
) {
1296 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1297 tu
->qtail
%= tu
->queue_size
;
1302 static void snd_timer_user_ccallback(struct snd_timer_instance
*timeri
,
1304 struct timespec
*tstamp
,
1305 unsigned long resolution
)
1307 struct snd_timer_user
*tu
= timeri
->callback_data
;
1308 struct snd_timer_tread r1
;
1309 unsigned long flags
;
1311 if (event
>= SNDRV_TIMER_EVENT_START
&&
1312 event
<= SNDRV_TIMER_EVENT_PAUSE
)
1313 tu
->tstamp
= *tstamp
;
1314 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1316 memset(&r1
, 0, sizeof(r1
));
1318 r1
.tstamp
= *tstamp
;
1319 r1
.val
= resolution
;
1320 spin_lock_irqsave(&tu
->qlock
, flags
);
1321 snd_timer_user_append_to_tqueue(tu
, &r1
);
1322 spin_unlock_irqrestore(&tu
->qlock
, flags
);
1323 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1324 wake_up(&tu
->qchange_sleep
);
1327 static void snd_timer_user_disconnect(struct snd_timer_instance
*timeri
)
1329 struct snd_timer_user
*tu
= timeri
->callback_data
;
1331 tu
->disconnected
= true;
1332 wake_up(&tu
->qchange_sleep
);
1335 static void snd_timer_user_tinterrupt(struct snd_timer_instance
*timeri
,
1336 unsigned long resolution
,
1337 unsigned long ticks
)
1339 struct snd_timer_user
*tu
= timeri
->callback_data
;
1340 struct snd_timer_tread
*r
, r1
;
1341 struct timespec tstamp
;
1342 int prev
, append
= 0;
1344 memset(&tstamp
, 0, sizeof(tstamp
));
1345 spin_lock(&tu
->qlock
);
1346 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
) |
1347 (1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1348 spin_unlock(&tu
->qlock
);
1351 if (tu
->last_resolution
!= resolution
|| ticks
> 0) {
1352 if (timer_tstamp_monotonic
)
1353 ktime_get_ts(&tstamp
);
1355 getnstimeofday(&tstamp
);
1357 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) &&
1358 tu
->last_resolution
!= resolution
) {
1359 memset(&r1
, 0, sizeof(r1
));
1360 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1362 r1
.val
= resolution
;
1363 snd_timer_user_append_to_tqueue(tu
, &r1
);
1364 tu
->last_resolution
= resolution
;
1367 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1371 if (tu
->qused
> 0) {
1372 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1373 r
= &tu
->tqueue
[prev
];
1374 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1381 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1384 snd_timer_user_append_to_tqueue(tu
, &r1
);
1387 spin_unlock(&tu
->qlock
);
1390 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1391 wake_up(&tu
->qchange_sleep
);
1394 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1396 struct snd_timer_user
*tu
;
1399 err
= nonseekable_open(inode
, file
);
1403 tu
= kzalloc(sizeof(*tu
), GFP_KERNEL
);
1406 spin_lock_init(&tu
->qlock
);
1407 init_waitqueue_head(&tu
->qchange_sleep
);
1408 mutex_init(&tu
->ioctl_lock
);
1410 tu
->queue_size
= 128;
1411 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1413 if (tu
->queue
== NULL
) {
1417 file
->private_data
= tu
;
1421 static int snd_timer_user_release(struct inode
*inode
, struct file
*file
)
1423 struct snd_timer_user
*tu
;
1425 if (file
->private_data
) {
1426 tu
= file
->private_data
;
1427 file
->private_data
= NULL
;
1428 mutex_lock(&tu
->ioctl_lock
);
1430 snd_timer_close(tu
->timeri
);
1431 mutex_unlock(&tu
->ioctl_lock
);
1439 static void snd_timer_user_zero_id(struct snd_timer_id
*id
)
1441 id
->dev_class
= SNDRV_TIMER_CLASS_NONE
;
1442 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1448 static void snd_timer_user_copy_id(struct snd_timer_id
*id
, struct snd_timer
*timer
)
1450 id
->dev_class
= timer
->tmr_class
;
1451 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1452 id
->card
= timer
->card
? timer
->card
->number
: -1;
1453 id
->device
= timer
->tmr_device
;
1454 id
->subdevice
= timer
->tmr_subdevice
;
1457 static int snd_timer_user_next_device(struct snd_timer_id __user
*_tid
)
1459 struct snd_timer_id id
;
1460 struct snd_timer
*timer
;
1461 struct list_head
*p
;
1463 if (copy_from_user(&id
, _tid
, sizeof(id
)))
1465 mutex_lock(®ister_mutex
);
1466 if (id
.dev_class
< 0) { /* first item */
1467 if (list_empty(&snd_timer_list
))
1468 snd_timer_user_zero_id(&id
);
1470 timer
= list_entry(snd_timer_list
.next
,
1471 struct snd_timer
, device_list
);
1472 snd_timer_user_copy_id(&id
, timer
);
1475 switch (id
.dev_class
) {
1476 case SNDRV_TIMER_CLASS_GLOBAL
:
1477 id
.device
= id
.device
< 0 ? 0 : id
.device
+ 1;
1478 list_for_each(p
, &snd_timer_list
) {
1479 timer
= list_entry(p
, struct snd_timer
, device_list
);
1480 if (timer
->tmr_class
> SNDRV_TIMER_CLASS_GLOBAL
) {
1481 snd_timer_user_copy_id(&id
, timer
);
1484 if (timer
->tmr_device
>= id
.device
) {
1485 snd_timer_user_copy_id(&id
, timer
);
1489 if (p
== &snd_timer_list
)
1490 snd_timer_user_zero_id(&id
);
1492 case SNDRV_TIMER_CLASS_CARD
:
1493 case SNDRV_TIMER_CLASS_PCM
:
1500 if (id
.device
< 0) {
1503 if (id
.subdevice
< 0) {
1511 list_for_each(p
, &snd_timer_list
) {
1512 timer
= list_entry(p
, struct snd_timer
, device_list
);
1513 if (timer
->tmr_class
> id
.dev_class
) {
1514 snd_timer_user_copy_id(&id
, timer
);
1517 if (timer
->tmr_class
< id
.dev_class
)
1519 if (timer
->card
->number
> id
.card
) {
1520 snd_timer_user_copy_id(&id
, timer
);
1523 if (timer
->card
->number
< id
.card
)
1525 if (timer
->tmr_device
> id
.device
) {
1526 snd_timer_user_copy_id(&id
, timer
);
1529 if (timer
->tmr_device
< id
.device
)
1531 if (timer
->tmr_subdevice
> id
.subdevice
) {
1532 snd_timer_user_copy_id(&id
, timer
);
1535 if (timer
->tmr_subdevice
< id
.subdevice
)
1537 snd_timer_user_copy_id(&id
, timer
);
1540 if (p
== &snd_timer_list
)
1541 snd_timer_user_zero_id(&id
);
1544 snd_timer_user_zero_id(&id
);
1547 mutex_unlock(®ister_mutex
);
1548 if (copy_to_user(_tid
, &id
, sizeof(*_tid
)))
1553 static int snd_timer_user_ginfo(struct file
*file
,
1554 struct snd_timer_ginfo __user
*_ginfo
)
1556 struct snd_timer_ginfo
*ginfo
;
1557 struct snd_timer_id tid
;
1558 struct snd_timer
*t
;
1559 struct list_head
*p
;
1562 ginfo
= memdup_user(_ginfo
, sizeof(*ginfo
));
1564 return PTR_ERR(ginfo
);
1567 memset(ginfo
, 0, sizeof(*ginfo
));
1569 mutex_lock(®ister_mutex
);
1570 t
= snd_timer_find(&tid
);
1572 ginfo
->card
= t
->card
? t
->card
->number
: -1;
1573 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1574 ginfo
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1575 strlcpy(ginfo
->id
, t
->id
, sizeof(ginfo
->id
));
1576 strlcpy(ginfo
->name
, t
->name
, sizeof(ginfo
->name
));
1577 ginfo
->resolution
= t
->hw
.resolution
;
1578 if (t
->hw
.resolution_min
> 0) {
1579 ginfo
->resolution_min
= t
->hw
.resolution_min
;
1580 ginfo
->resolution_max
= t
->hw
.resolution_max
;
1582 list_for_each(p
, &t
->open_list_head
) {
1588 mutex_unlock(®ister_mutex
);
1589 if (err
>= 0 && copy_to_user(_ginfo
, ginfo
, sizeof(*ginfo
)))
1595 static int timer_set_gparams(struct snd_timer_gparams
*gparams
)
1597 struct snd_timer
*t
;
1600 mutex_lock(®ister_mutex
);
1601 t
= snd_timer_find(&gparams
->tid
);
1606 if (!list_empty(&t
->open_list_head
)) {
1610 if (!t
->hw
.set_period
) {
1614 err
= t
->hw
.set_period(t
, gparams
->period_num
, gparams
->period_den
);
1616 mutex_unlock(®ister_mutex
);
1620 static int snd_timer_user_gparams(struct file
*file
,
1621 struct snd_timer_gparams __user
*_gparams
)
1623 struct snd_timer_gparams gparams
;
1625 if (copy_from_user(&gparams
, _gparams
, sizeof(gparams
)))
1627 return timer_set_gparams(&gparams
);
1630 static int snd_timer_user_gstatus(struct file
*file
,
1631 struct snd_timer_gstatus __user
*_gstatus
)
1633 struct snd_timer_gstatus gstatus
;
1634 struct snd_timer_id tid
;
1635 struct snd_timer
*t
;
1638 if (copy_from_user(&gstatus
, _gstatus
, sizeof(gstatus
)))
1641 memset(&gstatus
, 0, sizeof(gstatus
));
1643 mutex_lock(®ister_mutex
);
1644 t
= snd_timer_find(&tid
);
1646 if (t
->hw
.c_resolution
)
1647 gstatus
.resolution
= t
->hw
.c_resolution(t
);
1649 gstatus
.resolution
= t
->hw
.resolution
;
1650 if (t
->hw
.precise_resolution
) {
1651 t
->hw
.precise_resolution(t
, &gstatus
.resolution_num
,
1652 &gstatus
.resolution_den
);
1654 gstatus
.resolution_num
= gstatus
.resolution
;
1655 gstatus
.resolution_den
= 1000000000uL;
1660 mutex_unlock(®ister_mutex
);
1661 if (err
>= 0 && copy_to_user(_gstatus
, &gstatus
, sizeof(gstatus
)))
1666 static int snd_timer_user_tselect(struct file
*file
,
1667 struct snd_timer_select __user
*_tselect
)
1669 struct snd_timer_user
*tu
;
1670 struct snd_timer_select tselect
;
1674 tu
= file
->private_data
;
1676 snd_timer_close(tu
->timeri
);
1679 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
))) {
1683 sprintf(str
, "application %i", current
->pid
);
1684 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1685 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1686 err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
);
1690 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1696 tu
->tqueue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_tread
),
1698 if (tu
->tqueue
== NULL
)
1701 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1703 if (tu
->queue
== NULL
)
1708 snd_timer_close(tu
->timeri
);
1711 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1712 tu
->timeri
->callback
= tu
->tread
1713 ? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1714 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1715 tu
->timeri
->callback_data
= (void *)tu
;
1716 tu
->timeri
->disconnect
= snd_timer_user_disconnect
;
1723 static int snd_timer_user_info(struct file
*file
,
1724 struct snd_timer_info __user
*_info
)
1726 struct snd_timer_user
*tu
;
1727 struct snd_timer_info
*info
;
1728 struct snd_timer
*t
;
1731 tu
= file
->private_data
;
1734 t
= tu
->timeri
->timer
;
1738 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1741 info
->card
= t
->card
? t
->card
->number
: -1;
1742 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1743 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1744 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1745 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1746 info
->resolution
= t
->hw
.resolution
;
1747 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1753 static int snd_timer_user_params(struct file
*file
,
1754 struct snd_timer_params __user
*_params
)
1756 struct snd_timer_user
*tu
;
1757 struct snd_timer_params params
;
1758 struct snd_timer
*t
;
1759 struct snd_timer_read
*tr
;
1760 struct snd_timer_tread
*ttr
;
1763 tu
= file
->private_data
;
1766 t
= tu
->timeri
->timer
;
1769 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
1771 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)) {
1774 if (params
.ticks
< 1) {
1779 /* Don't allow resolution less than 1ms */
1780 resolution
= snd_timer_resolution(tu
->timeri
);
1781 resolution
*= params
.ticks
;
1782 if (resolution
< 1000000) {
1787 if (params
.queue_size
> 0 &&
1788 (params
.queue_size
< 32 || params
.queue_size
> 1024)) {
1792 if (params
.filter
& ~((1<<SNDRV_TIMER_EVENT_RESOLUTION
)|
1793 (1<<SNDRV_TIMER_EVENT_TICK
)|
1794 (1<<SNDRV_TIMER_EVENT_START
)|
1795 (1<<SNDRV_TIMER_EVENT_STOP
)|
1796 (1<<SNDRV_TIMER_EVENT_CONTINUE
)|
1797 (1<<SNDRV_TIMER_EVENT_PAUSE
)|
1798 (1<<SNDRV_TIMER_EVENT_SUSPEND
)|
1799 (1<<SNDRV_TIMER_EVENT_RESUME
)|
1800 (1<<SNDRV_TIMER_EVENT_MSTART
)|
1801 (1<<SNDRV_TIMER_EVENT_MSTOP
)|
1802 (1<<SNDRV_TIMER_EVENT_MCONTINUE
)|
1803 (1<<SNDRV_TIMER_EVENT_MPAUSE
)|
1804 (1<<SNDRV_TIMER_EVENT_MSUSPEND
)|
1805 (1<<SNDRV_TIMER_EVENT_MRESUME
))) {
1809 snd_timer_stop(tu
->timeri
);
1810 spin_lock_irq(&t
->lock
);
1811 tu
->timeri
->flags
&= ~(SNDRV_TIMER_IFLG_AUTO
|
1812 SNDRV_TIMER_IFLG_EXCLUSIVE
|
1813 SNDRV_TIMER_IFLG_EARLY_EVENT
);
1814 if (params
.flags
& SNDRV_TIMER_PSFLG_AUTO
)
1815 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
1816 if (params
.flags
& SNDRV_TIMER_PSFLG_EXCLUSIVE
)
1817 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EXCLUSIVE
;
1818 if (params
.flags
& SNDRV_TIMER_PSFLG_EARLY_EVENT
)
1819 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EARLY_EVENT
;
1820 spin_unlock_irq(&t
->lock
);
1821 if (params
.queue_size
> 0 &&
1822 (unsigned int)tu
->queue_size
!= params
.queue_size
) {
1824 ttr
= kmalloc(params
.queue_size
* sizeof(*ttr
),
1828 tu
->queue_size
= params
.queue_size
;
1832 tr
= kmalloc(params
.queue_size
* sizeof(*tr
),
1836 tu
->queue_size
= params
.queue_size
;
1841 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1842 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1844 struct snd_timer_tread tread
;
1845 memset(&tread
, 0, sizeof(tread
));
1846 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1847 tread
.tstamp
.tv_sec
= 0;
1848 tread
.tstamp
.tv_nsec
= 0;
1850 snd_timer_user_append_to_tqueue(tu
, &tread
);
1852 struct snd_timer_read
*r
= &tu
->queue
[0];
1859 tu
->filter
= params
.filter
;
1860 tu
->ticks
= params
.ticks
;
1863 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1868 static int snd_timer_user_status(struct file
*file
,
1869 struct snd_timer_status __user
*_status
)
1871 struct snd_timer_user
*tu
;
1872 struct snd_timer_status status
;
1874 tu
= file
->private_data
;
1877 memset(&status
, 0, sizeof(status
));
1878 status
.tstamp
= tu
->tstamp
;
1879 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1880 status
.lost
= tu
->timeri
->lost
;
1881 status
.overrun
= tu
->overrun
;
1882 spin_lock_irq(&tu
->qlock
);
1883 status
.queue
= tu
->qused
;
1884 spin_unlock_irq(&tu
->qlock
);
1885 if (copy_to_user(_status
, &status
, sizeof(status
)))
1890 static int snd_timer_user_start(struct file
*file
)
1893 struct snd_timer_user
*tu
;
1895 tu
= file
->private_data
;
1898 snd_timer_stop(tu
->timeri
);
1899 tu
->timeri
->lost
= 0;
1900 tu
->last_resolution
= 0;
1901 return (err
= snd_timer_start(tu
->timeri
, tu
->ticks
)) < 0 ? err
: 0;
1904 static int snd_timer_user_stop(struct file
*file
)
1907 struct snd_timer_user
*tu
;
1909 tu
= file
->private_data
;
1912 return (err
= snd_timer_stop(tu
->timeri
)) < 0 ? err
: 0;
1915 static int snd_timer_user_continue(struct file
*file
)
1918 struct snd_timer_user
*tu
;
1920 tu
= file
->private_data
;
1923 /* start timer instead of continue if it's not used before */
1924 if (!(tu
->timeri
->flags
& SNDRV_TIMER_IFLG_PAUSED
))
1925 return snd_timer_user_start(file
);
1926 tu
->timeri
->lost
= 0;
1927 return (err
= snd_timer_continue(tu
->timeri
)) < 0 ? err
: 0;
1930 static int snd_timer_user_pause(struct file
*file
)
1933 struct snd_timer_user
*tu
;
1935 tu
= file
->private_data
;
1938 return (err
= snd_timer_pause(tu
->timeri
)) < 0 ? err
: 0;
1942 SNDRV_TIMER_IOCTL_START_OLD
= _IO('T', 0x20),
1943 SNDRV_TIMER_IOCTL_STOP_OLD
= _IO('T', 0x21),
1944 SNDRV_TIMER_IOCTL_CONTINUE_OLD
= _IO('T', 0x22),
1945 SNDRV_TIMER_IOCTL_PAUSE_OLD
= _IO('T', 0x23),
1948 static long __snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1951 struct snd_timer_user
*tu
;
1952 void __user
*argp
= (void __user
*)arg
;
1953 int __user
*p
= argp
;
1955 tu
= file
->private_data
;
1957 case SNDRV_TIMER_IOCTL_PVERSION
:
1958 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1959 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1960 return snd_timer_user_next_device(argp
);
1961 case SNDRV_TIMER_IOCTL_TREAD
:
1965 if (tu
->timeri
) /* too late */
1967 if (get_user(xarg
, p
))
1969 tu
->tread
= xarg
? 1 : 0;
1972 case SNDRV_TIMER_IOCTL_GINFO
:
1973 return snd_timer_user_ginfo(file
, argp
);
1974 case SNDRV_TIMER_IOCTL_GPARAMS
:
1975 return snd_timer_user_gparams(file
, argp
);
1976 case SNDRV_TIMER_IOCTL_GSTATUS
:
1977 return snd_timer_user_gstatus(file
, argp
);
1978 case SNDRV_TIMER_IOCTL_SELECT
:
1979 return snd_timer_user_tselect(file
, argp
);
1980 case SNDRV_TIMER_IOCTL_INFO
:
1981 return snd_timer_user_info(file
, argp
);
1982 case SNDRV_TIMER_IOCTL_PARAMS
:
1983 return snd_timer_user_params(file
, argp
);
1984 case SNDRV_TIMER_IOCTL_STATUS
:
1985 return snd_timer_user_status(file
, argp
);
1986 case SNDRV_TIMER_IOCTL_START
:
1987 case SNDRV_TIMER_IOCTL_START_OLD
:
1988 return snd_timer_user_start(file
);
1989 case SNDRV_TIMER_IOCTL_STOP
:
1990 case SNDRV_TIMER_IOCTL_STOP_OLD
:
1991 return snd_timer_user_stop(file
);
1992 case SNDRV_TIMER_IOCTL_CONTINUE
:
1993 case SNDRV_TIMER_IOCTL_CONTINUE_OLD
:
1994 return snd_timer_user_continue(file
);
1995 case SNDRV_TIMER_IOCTL_PAUSE
:
1996 case SNDRV_TIMER_IOCTL_PAUSE_OLD
:
1997 return snd_timer_user_pause(file
);
2002 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
2005 struct snd_timer_user
*tu
= file
->private_data
;
2008 mutex_lock(&tu
->ioctl_lock
);
2009 ret
= __snd_timer_user_ioctl(file
, cmd
, arg
);
2010 mutex_unlock(&tu
->ioctl_lock
);
2014 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
2016 struct snd_timer_user
*tu
;
2018 tu
= file
->private_data
;
2019 return fasync_helper(fd
, file
, on
, &tu
->fasync
);
2022 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
,
2023 size_t count
, loff_t
*offset
)
2025 struct snd_timer_user
*tu
;
2026 long result
= 0, unit
;
2030 tu
= file
->private_data
;
2031 unit
= tu
->tread
? sizeof(struct snd_timer_tread
) : sizeof(struct snd_timer_read
);
2032 mutex_lock(&tu
->ioctl_lock
);
2033 spin_lock_irq(&tu
->qlock
);
2034 while ((long)count
- result
>= unit
) {
2035 while (!tu
->qused
) {
2038 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
2043 set_current_state(TASK_INTERRUPTIBLE
);
2044 init_waitqueue_entry(&wait
, current
);
2045 add_wait_queue(&tu
->qchange_sleep
, &wait
);
2047 spin_unlock_irq(&tu
->qlock
);
2048 mutex_unlock(&tu
->ioctl_lock
);
2050 mutex_lock(&tu
->ioctl_lock
);
2051 spin_lock_irq(&tu
->qlock
);
2053 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
2055 if (tu
->disconnected
) {
2059 if (signal_pending(current
)) {
2065 qhead
= tu
->qhead
++;
2066 tu
->qhead
%= tu
->queue_size
;
2068 spin_unlock_irq(&tu
->qlock
);
2071 if (copy_to_user(buffer
, &tu
->tqueue
[qhead
],
2072 sizeof(struct snd_timer_tread
)))
2075 if (copy_to_user(buffer
, &tu
->queue
[qhead
],
2076 sizeof(struct snd_timer_read
)))
2080 spin_lock_irq(&tu
->qlock
);
2087 spin_unlock_irq(&tu
->qlock
);
2088 mutex_unlock(&tu
->ioctl_lock
);
2089 return result
> 0 ? result
: err
;
2092 static unsigned int snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
2095 struct snd_timer_user
*tu
;
2097 tu
= file
->private_data
;
2099 poll_wait(file
, &tu
->qchange_sleep
, wait
);
2103 mask
|= POLLIN
| POLLRDNORM
;
2104 if (tu
->disconnected
)
2110 #ifdef CONFIG_COMPAT
2111 #include "timer_compat.c"
2113 #define snd_timer_user_ioctl_compat NULL
2116 static const struct file_operations snd_timer_f_ops
=
2118 .owner
= THIS_MODULE
,
2119 .read
= snd_timer_user_read
,
2120 .open
= snd_timer_user_open
,
2121 .release
= snd_timer_user_release
,
2122 .llseek
= no_llseek
,
2123 .poll
= snd_timer_user_poll
,
2124 .unlocked_ioctl
= snd_timer_user_ioctl
,
2125 .compat_ioctl
= snd_timer_user_ioctl_compat
,
2126 .fasync
= snd_timer_user_fasync
,
2129 /* unregister the system timer */
2130 static void snd_timer_free_all(void)
2132 struct snd_timer
*timer
, *n
;
2134 list_for_each_entry_safe(timer
, n
, &snd_timer_list
, device_list
)
2135 snd_timer_free(timer
);
2138 static struct device timer_dev
;
2144 static int __init
alsa_timer_init(void)
2148 snd_device_initialize(&timer_dev
, NULL
);
2149 dev_set_name(&timer_dev
, "timer");
2151 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2152 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1,
2156 err
= snd_timer_register_system();
2158 pr_err("ALSA: unable to register system timer (%i)\n", err
);
2159 put_device(&timer_dev
);
2163 err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0,
2164 &snd_timer_f_ops
, NULL
, &timer_dev
);
2166 pr_err("ALSA: unable to register timer device (%i)\n", err
);
2167 snd_timer_free_all();
2168 put_device(&timer_dev
);
2172 snd_timer_proc_init();
2176 static void __exit
alsa_timer_exit(void)
2178 snd_unregister_device(&timer_dev
);
2179 snd_timer_free_all();
2180 put_device(&timer_dev
);
2181 snd_timer_proc_done();
2182 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2183 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
2187 module_init(alsa_timer_init
)
2188 module_exit(alsa_timer_exit
)