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 <linux/sched/signal.h>
31 #include <sound/core.h>
32 #include <sound/timer.h>
33 #include <sound/control.h>
34 #include <sound/info.h>
35 #include <sound/minors.h>
36 #include <sound/initval.h>
37 #include <linux/kmod.h>
40 #define SNDRV_TIMER_IFLG_PAUSED 0x00010000
42 #if IS_ENABLED(CONFIG_SND_HRTIMER)
43 #define DEFAULT_TIMER_LIMIT 4
45 #define DEFAULT_TIMER_LIMIT 1
48 static int timer_limit
= DEFAULT_TIMER_LIMIT
;
49 static int timer_tstamp_monotonic
= 1;
50 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
51 MODULE_DESCRIPTION("ALSA timer interface");
52 MODULE_LICENSE("GPL");
53 module_param(timer_limit
, int, 0444);
54 MODULE_PARM_DESC(timer_limit
, "Maximum global timers in system.");
55 module_param(timer_tstamp_monotonic
, int, 0444);
56 MODULE_PARM_DESC(timer_tstamp_monotonic
, "Use posix monotonic clock source for timestamps (default).");
58 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR
, SNDRV_MINOR_TIMER
);
59 MODULE_ALIAS("devname:snd/timer");
61 struct snd_timer_user
{
62 struct snd_timer_instance
*timeri
;
63 int tread
; /* enhanced read with timestamps and events */
65 unsigned long overrun
;
71 struct snd_timer_read
*queue
;
72 struct snd_timer_tread
*tqueue
;
74 unsigned long last_resolution
;
76 struct timespec tstamp
; /* trigger tstamp */
77 wait_queue_head_t qchange_sleep
;
78 struct fasync_struct
*fasync
;
79 struct mutex ioctl_lock
;
83 static LIST_HEAD(snd_timer_list
);
85 /* list of slave instances */
86 static LIST_HEAD(snd_timer_slave_list
);
88 /* lock for slave active lists */
89 static DEFINE_SPINLOCK(slave_active_lock
);
91 static DEFINE_MUTEX(register_mutex
);
93 static int snd_timer_free(struct snd_timer
*timer
);
94 static int snd_timer_dev_free(struct snd_device
*device
);
95 static int snd_timer_dev_register(struct snd_device
*device
);
96 static int snd_timer_dev_disconnect(struct snd_device
*device
);
98 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
);
101 * create a timer instance with the given owner string.
102 * when timer is not NULL, increments the module counter
104 static struct snd_timer_instance
*snd_timer_instance_new(char *owner
,
105 struct snd_timer
*timer
)
107 struct snd_timer_instance
*timeri
;
108 timeri
= kzalloc(sizeof(*timeri
), GFP_KERNEL
);
111 timeri
->owner
= kstrdup(owner
, GFP_KERNEL
);
112 if (! timeri
->owner
) {
116 INIT_LIST_HEAD(&timeri
->open_list
);
117 INIT_LIST_HEAD(&timeri
->active_list
);
118 INIT_LIST_HEAD(&timeri
->ack_list
);
119 INIT_LIST_HEAD(&timeri
->slave_list_head
);
120 INIT_LIST_HEAD(&timeri
->slave_active_head
);
122 timeri
->timer
= timer
;
123 if (timer
&& !try_module_get(timer
->module
)) {
124 kfree(timeri
->owner
);
133 * find a timer instance from the given timer id
135 static struct snd_timer
*snd_timer_find(struct snd_timer_id
*tid
)
137 struct snd_timer
*timer
= NULL
;
139 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
140 if (timer
->tmr_class
!= tid
->dev_class
)
142 if ((timer
->tmr_class
== SNDRV_TIMER_CLASS_CARD
||
143 timer
->tmr_class
== SNDRV_TIMER_CLASS_PCM
) &&
144 (timer
->card
== NULL
||
145 timer
->card
->number
!= tid
->card
))
147 if (timer
->tmr_device
!= tid
->device
)
149 if (timer
->tmr_subdevice
!= tid
->subdevice
)
156 #ifdef CONFIG_MODULES
158 static void snd_timer_request(struct snd_timer_id
*tid
)
160 switch (tid
->dev_class
) {
161 case SNDRV_TIMER_CLASS_GLOBAL
:
162 if (tid
->device
< timer_limit
)
163 request_module("snd-timer-%i", tid
->device
);
165 case SNDRV_TIMER_CLASS_CARD
:
166 case SNDRV_TIMER_CLASS_PCM
:
167 if (tid
->card
< snd_ecards_limit
)
168 request_module("snd-card-%i", tid
->card
);
178 * look for a master instance matching with the slave id of the given slave.
179 * when found, relink the open_link of the slave.
181 * call this with register_mutex down.
183 static int snd_timer_check_slave(struct snd_timer_instance
*slave
)
185 struct snd_timer
*timer
;
186 struct snd_timer_instance
*master
;
188 /* FIXME: it's really dumb to look up all entries.. */
189 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
190 list_for_each_entry(master
, &timer
->open_list_head
, open_list
) {
191 if (slave
->slave_class
== master
->slave_class
&&
192 slave
->slave_id
== master
->slave_id
) {
193 if (master
->timer
->num_instances
>=
194 master
->timer
->max_instances
)
196 list_move_tail(&slave
->open_list
,
197 &master
->slave_list_head
);
198 master
->timer
->num_instances
++;
199 spin_lock_irq(&slave_active_lock
);
200 slave
->master
= master
;
201 slave
->timer
= master
->timer
;
202 spin_unlock_irq(&slave_active_lock
);
211 * look for slave instances matching with the slave id of the given master.
212 * when found, relink the open_link of slaves.
214 * call this with register_mutex down.
216 static int snd_timer_check_master(struct snd_timer_instance
*master
)
218 struct snd_timer_instance
*slave
, *tmp
;
220 /* check all pending slaves */
221 list_for_each_entry_safe(slave
, tmp
, &snd_timer_slave_list
, open_list
) {
222 if (slave
->slave_class
== master
->slave_class
&&
223 slave
->slave_id
== master
->slave_id
) {
224 if (master
->timer
->num_instances
>=
225 master
->timer
->max_instances
)
227 list_move_tail(&slave
->open_list
, &master
->slave_list_head
);
228 master
->timer
->num_instances
++;
229 spin_lock_irq(&slave_active_lock
);
230 spin_lock(&master
->timer
->lock
);
231 slave
->master
= master
;
232 slave
->timer
= master
->timer
;
233 if (slave
->flags
& SNDRV_TIMER_IFLG_RUNNING
)
234 list_add_tail(&slave
->active_list
,
235 &master
->slave_active_head
);
236 spin_unlock(&master
->timer
->lock
);
237 spin_unlock_irq(&slave_active_lock
);
243 static int snd_timer_close_locked(struct snd_timer_instance
*timeri
,
244 struct device
**card_devp_to_put
);
247 * open a timer instance
248 * when opening a master, the slave id must be here given.
250 int snd_timer_open(struct snd_timer_instance
**ti
,
251 char *owner
, struct snd_timer_id
*tid
,
252 unsigned int slave_id
)
254 struct snd_timer
*timer
;
255 struct snd_timer_instance
*timeri
= NULL
;
256 struct device
*card_dev_to_put
= NULL
;
259 mutex_lock(®ister_mutex
);
260 if (tid
->dev_class
== SNDRV_TIMER_CLASS_SLAVE
) {
261 /* open a slave instance */
262 if (tid
->dev_sclass
<= SNDRV_TIMER_SCLASS_NONE
||
263 tid
->dev_sclass
> SNDRV_TIMER_SCLASS_OSS_SEQUENCER
) {
264 pr_debug("ALSA: timer: invalid slave class %i\n",
269 timeri
= snd_timer_instance_new(owner
, NULL
);
274 timeri
->slave_class
= tid
->dev_sclass
;
275 timeri
->slave_id
= tid
->device
;
276 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
277 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
278 err
= snd_timer_check_slave(timeri
);
280 snd_timer_close_locked(timeri
, &card_dev_to_put
);
286 /* open a master instance */
287 timer
= snd_timer_find(tid
);
288 #ifdef CONFIG_MODULES
290 mutex_unlock(®ister_mutex
);
291 snd_timer_request(tid
);
292 mutex_lock(®ister_mutex
);
293 timer
= snd_timer_find(tid
);
300 if (!list_empty(&timer
->open_list_head
)) {
301 timeri
= list_entry(timer
->open_list_head
.next
,
302 struct snd_timer_instance
, open_list
);
303 if (timeri
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
309 if (timer
->num_instances
>= timer
->max_instances
) {
313 timeri
= snd_timer_instance_new(owner
, timer
);
318 /* take a card refcount for safe disconnection */
320 get_device(&timer
->card
->card_dev
);
321 timeri
->slave_class
= tid
->dev_sclass
;
322 timeri
->slave_id
= slave_id
;
324 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
) {
325 err
= timer
->hw
.open(timer
);
327 kfree(timeri
->owner
);
332 card_dev_to_put
= &timer
->card
->card_dev
;
333 module_put(timer
->module
);
338 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
339 timer
->num_instances
++;
340 err
= snd_timer_check_master(timeri
);
342 snd_timer_close_locked(timeri
, &card_dev_to_put
);
347 mutex_unlock(®ister_mutex
);
348 /* put_device() is called after unlock for avoiding deadlock */
350 put_device(card_dev_to_put
);
354 EXPORT_SYMBOL(snd_timer_open
);
357 * close a timer instance
358 * call this with register_mutex down.
360 static int snd_timer_close_locked(struct snd_timer_instance
*timeri
,
361 struct device
**card_devp_to_put
)
363 struct snd_timer
*timer
= NULL
;
364 struct snd_timer_instance
*slave
, *tmp
;
366 list_del(&timeri
->open_list
);
368 /* force to stop the timer */
369 snd_timer_stop(timeri
);
371 timer
= timeri
->timer
;
373 timer
->num_instances
--;
374 /* wait, until the active callback is finished */
375 spin_lock_irq(&timer
->lock
);
376 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
377 spin_unlock_irq(&timer
->lock
);
379 spin_lock_irq(&timer
->lock
);
381 spin_unlock_irq(&timer
->lock
);
383 /* remove slave links */
384 spin_lock_irq(&slave_active_lock
);
385 spin_lock(&timer
->lock
);
386 list_for_each_entry_safe(slave
, tmp
, &timeri
->slave_list_head
,
388 list_move_tail(&slave
->open_list
, &snd_timer_slave_list
);
389 timer
->num_instances
--;
390 slave
->master
= NULL
;
392 list_del_init(&slave
->ack_list
);
393 list_del_init(&slave
->active_list
);
395 spin_unlock(&timer
->lock
);
396 spin_unlock_irq(&slave_active_lock
);
398 /* slave doesn't need to release timer resources below */
399 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
403 if (timeri
->private_free
)
404 timeri
->private_free(timeri
);
405 kfree(timeri
->owner
);
409 if (list_empty(&timer
->open_list_head
) && timer
->hw
.close
)
410 timer
->hw
.close(timer
);
411 /* release a card refcount for safe disconnection */
413 *card_devp_to_put
= &timer
->card
->card_dev
;
414 module_put(timer
->module
);
421 * close a timer instance
423 int snd_timer_close(struct snd_timer_instance
*timeri
)
425 struct device
*card_dev_to_put
= NULL
;
428 if (snd_BUG_ON(!timeri
))
431 mutex_lock(®ister_mutex
);
432 err
= snd_timer_close_locked(timeri
, &card_dev_to_put
);
433 mutex_unlock(®ister_mutex
);
434 /* put_device() is called after unlock for avoiding deadlock */
436 put_device(card_dev_to_put
);
439 EXPORT_SYMBOL(snd_timer_close
);
441 unsigned long snd_timer_resolution(struct snd_timer_instance
*timeri
)
443 struct snd_timer
* timer
;
447 timer
= timeri
->timer
;
449 if (timer
->hw
.c_resolution
)
450 return timer
->hw
.c_resolution(timer
);
451 return timer
->hw
.resolution
;
455 EXPORT_SYMBOL(snd_timer_resolution
);
457 static void snd_timer_notify1(struct snd_timer_instance
*ti
, int event
)
459 struct snd_timer
*timer
;
460 unsigned long resolution
= 0;
461 struct snd_timer_instance
*ts
;
462 struct timespec tstamp
;
464 if (timer_tstamp_monotonic
)
465 ktime_get_ts(&tstamp
);
467 getnstimeofday(&tstamp
);
468 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_START
||
469 event
> SNDRV_TIMER_EVENT_PAUSE
))
471 if (event
== SNDRV_TIMER_EVENT_START
||
472 event
== SNDRV_TIMER_EVENT_CONTINUE
)
473 resolution
= snd_timer_resolution(ti
);
475 ti
->ccallback(ti
, event
, &tstamp
, resolution
);
476 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
481 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
483 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
485 ts
->ccallback(ts
, event
+ 100, &tstamp
, resolution
);
488 /* start/continue a master timer */
489 static int snd_timer_start1(struct snd_timer_instance
*timeri
,
490 bool start
, unsigned long ticks
)
492 struct snd_timer
*timer
;
496 timer
= timeri
->timer
;
500 spin_lock_irqsave(&timer
->lock
, flags
);
501 if (timer
->card
&& timer
->card
->shutdown
) {
505 if (timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
506 SNDRV_TIMER_IFLG_START
)) {
512 timeri
->ticks
= timeri
->cticks
= ticks
;
513 else if (!timeri
->cticks
)
517 list_move_tail(&timeri
->active_list
, &timer
->active_list_head
);
518 if (timer
->running
) {
519 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
521 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
522 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
523 result
= 1; /* delayed start */
526 timer
->sticks
= ticks
;
527 timer
->hw
.start(timer
);
530 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
533 snd_timer_notify1(timeri
, start
? SNDRV_TIMER_EVENT_START
:
534 SNDRV_TIMER_EVENT_CONTINUE
);
536 spin_unlock_irqrestore(&timer
->lock
, flags
);
540 /* start/continue a slave timer */
541 static int snd_timer_start_slave(struct snd_timer_instance
*timeri
,
546 spin_lock_irqsave(&slave_active_lock
, flags
);
547 if (timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
548 spin_unlock_irqrestore(&slave_active_lock
, flags
);
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 spin_unlock_irqrestore(&slave_active_lock
, flags
);
561 return 1; /* delayed start */
564 /* stop/pause a master timer */
565 static int snd_timer_stop1(struct snd_timer_instance
*timeri
, bool stop
)
567 struct snd_timer
*timer
;
571 timer
= timeri
->timer
;
574 spin_lock_irqsave(&timer
->lock
, flags
);
575 if (!(timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
576 SNDRV_TIMER_IFLG_START
))) {
580 list_del_init(&timeri
->ack_list
);
581 list_del_init(&timeri
->active_list
);
582 if (timer
->card
&& timer
->card
->shutdown
)
585 timeri
->cticks
= timeri
->ticks
;
588 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
589 !(--timer
->running
)) {
590 timer
->hw
.stop(timer
);
591 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
592 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
593 snd_timer_reschedule(timer
, 0);
594 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
595 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
596 timer
->hw
.start(timer
);
600 timeri
->flags
&= ~(SNDRV_TIMER_IFLG_RUNNING
| SNDRV_TIMER_IFLG_START
);
602 timeri
->flags
&= ~SNDRV_TIMER_IFLG_PAUSED
;
604 timeri
->flags
|= SNDRV_TIMER_IFLG_PAUSED
;
605 snd_timer_notify1(timeri
, stop
? SNDRV_TIMER_EVENT_STOP
:
606 SNDRV_TIMER_EVENT_PAUSE
);
608 spin_unlock_irqrestore(&timer
->lock
, flags
);
612 /* stop/pause a slave timer */
613 static int snd_timer_stop_slave(struct snd_timer_instance
*timeri
, bool stop
)
617 spin_lock_irqsave(&slave_active_lock
, flags
);
618 if (!(timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
)) {
619 spin_unlock_irqrestore(&slave_active_lock
, flags
);
622 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
624 spin_lock(&timeri
->timer
->lock
);
625 list_del_init(&timeri
->ack_list
);
626 list_del_init(&timeri
->active_list
);
627 snd_timer_notify1(timeri
, stop
? SNDRV_TIMER_EVENT_STOP
:
628 SNDRV_TIMER_EVENT_PAUSE
);
629 spin_unlock(&timeri
->timer
->lock
);
631 spin_unlock_irqrestore(&slave_active_lock
, flags
);
636 * start the timer instance
638 int snd_timer_start(struct snd_timer_instance
*timeri
, unsigned int ticks
)
640 if (timeri
== NULL
|| ticks
< 1)
642 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
643 return snd_timer_start_slave(timeri
, true);
645 return snd_timer_start1(timeri
, true, ticks
);
647 EXPORT_SYMBOL(snd_timer_start
);
650 * stop the timer instance.
652 * do not call this from the timer callback!
654 int snd_timer_stop(struct snd_timer_instance
*timeri
)
656 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
657 return snd_timer_stop_slave(timeri
, true);
659 return snd_timer_stop1(timeri
, true);
661 EXPORT_SYMBOL(snd_timer_stop
);
664 * start again.. the tick is kept.
666 int snd_timer_continue(struct snd_timer_instance
*timeri
)
668 /* timer can continue only after pause */
669 if (!(timeri
->flags
& SNDRV_TIMER_IFLG_PAUSED
))
672 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
673 return snd_timer_start_slave(timeri
, false);
675 return snd_timer_start1(timeri
, false, 0);
677 EXPORT_SYMBOL(snd_timer_continue
);
680 * pause.. remember the ticks left
682 int snd_timer_pause(struct snd_timer_instance
* timeri
)
684 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
685 return snd_timer_stop_slave(timeri
, false);
687 return snd_timer_stop1(timeri
, false);
689 EXPORT_SYMBOL(snd_timer_pause
);
692 * reschedule the timer
694 * start pending instances and check the scheduling ticks.
695 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
697 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
)
699 struct snd_timer_instance
*ti
;
700 unsigned long ticks
= ~0UL;
702 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
703 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
704 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
705 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
708 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
709 if (ticks
> ti
->cticks
)
714 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
717 if (ticks
> timer
->hw
.ticks
)
718 ticks
= timer
->hw
.ticks
;
719 if (ticks_left
!= ticks
)
720 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
721 timer
->sticks
= ticks
;
728 static void snd_timer_tasklet(unsigned long arg
)
730 struct snd_timer
*timer
= (struct snd_timer
*) arg
;
731 struct snd_timer_instance
*ti
;
733 unsigned long resolution
, ticks
;
736 if (timer
->card
&& timer
->card
->shutdown
)
739 spin_lock_irqsave(&timer
->lock
, flags
);
740 /* now process all callbacks */
741 while (!list_empty(&timer
->sack_list_head
)) {
742 p
= timer
->sack_list_head
.next
; /* get first item */
743 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
745 /* remove from ack_list and make empty */
750 resolution
= ti
->resolution
;
752 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
753 spin_unlock(&timer
->lock
);
755 ti
->callback(ti
, resolution
, ticks
);
756 spin_lock(&timer
->lock
);
757 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
759 spin_unlock_irqrestore(&timer
->lock
, flags
);
765 * ticks_left is usually equal to timer->sticks.
768 void snd_timer_interrupt(struct snd_timer
* timer
, unsigned long ticks_left
)
770 struct snd_timer_instance
*ti
, *ts
, *tmp
;
771 unsigned long resolution
, ticks
;
772 struct list_head
*p
, *ack_list_head
;
779 if (timer
->card
&& timer
->card
->shutdown
)
782 spin_lock_irqsave(&timer
->lock
, flags
);
784 /* remember the current resolution */
785 if (timer
->hw
.c_resolution
)
786 resolution
= timer
->hw
.c_resolution(timer
);
788 resolution
= timer
->hw
.resolution
;
790 /* loop for all active instances
791 * Here we cannot use list_for_each_entry because the active_list of a
792 * processed instance is relinked to done_list_head before the callback
795 list_for_each_entry_safe(ti
, tmp
, &timer
->active_list_head
,
797 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
799 ti
->pticks
+= ticks_left
;
800 ti
->resolution
= resolution
;
801 if (ti
->cticks
< ticks_left
)
804 ti
->cticks
-= ticks_left
;
805 if (ti
->cticks
) /* not expired */
807 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
808 ti
->cticks
= ti
->ticks
;
810 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
812 list_del_init(&ti
->active_list
);
814 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
815 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
))
816 ack_list_head
= &timer
->ack_list_head
;
818 ack_list_head
= &timer
->sack_list_head
;
819 if (list_empty(&ti
->ack_list
))
820 list_add_tail(&ti
->ack_list
, ack_list_head
);
821 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
) {
822 ts
->pticks
= ti
->pticks
;
823 ts
->resolution
= resolution
;
824 if (list_empty(&ts
->ack_list
))
825 list_add_tail(&ts
->ack_list
, ack_list_head
);
828 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
829 snd_timer_reschedule(timer
, timer
->sticks
);
830 if (timer
->running
) {
831 if (timer
->hw
.flags
& SNDRV_TIMER_HW_STOP
) {
832 timer
->hw
.stop(timer
);
833 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
835 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_AUTO
) ||
836 (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
)) {
838 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
839 timer
->hw
.start(timer
);
842 timer
->hw
.stop(timer
);
845 /* now process all fast callbacks */
846 while (!list_empty(&timer
->ack_list_head
)) {
847 p
= timer
->ack_list_head
.next
; /* get first item */
848 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
850 /* remove from ack_list and make empty */
856 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
857 spin_unlock(&timer
->lock
);
859 ti
->callback(ti
, resolution
, ticks
);
860 spin_lock(&timer
->lock
);
861 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
864 /* do we have any slow callbacks? */
865 use_tasklet
= !list_empty(&timer
->sack_list_head
);
866 spin_unlock_irqrestore(&timer
->lock
, flags
);
869 tasklet_schedule(&timer
->task_queue
);
871 EXPORT_SYMBOL(snd_timer_interrupt
);
877 int snd_timer_new(struct snd_card
*card
, char *id
, struct snd_timer_id
*tid
,
878 struct snd_timer
**rtimer
)
880 struct snd_timer
*timer
;
882 static struct snd_device_ops ops
= {
883 .dev_free
= snd_timer_dev_free
,
884 .dev_register
= snd_timer_dev_register
,
885 .dev_disconnect
= snd_timer_dev_disconnect
,
888 if (snd_BUG_ON(!tid
))
892 timer
= kzalloc(sizeof(*timer
), GFP_KERNEL
);
895 timer
->tmr_class
= tid
->dev_class
;
897 timer
->tmr_device
= tid
->device
;
898 timer
->tmr_subdevice
= tid
->subdevice
;
900 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
902 INIT_LIST_HEAD(&timer
->device_list
);
903 INIT_LIST_HEAD(&timer
->open_list_head
);
904 INIT_LIST_HEAD(&timer
->active_list_head
);
905 INIT_LIST_HEAD(&timer
->ack_list_head
);
906 INIT_LIST_HEAD(&timer
->sack_list_head
);
907 spin_lock_init(&timer
->lock
);
908 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
,
909 (unsigned long)timer
);
910 timer
->max_instances
= 1000; /* default limit per timer */
912 timer
->module
= card
->module
;
913 err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
);
915 snd_timer_free(timer
);
923 EXPORT_SYMBOL(snd_timer_new
);
925 static int snd_timer_free(struct snd_timer
*timer
)
930 mutex_lock(®ister_mutex
);
931 if (! list_empty(&timer
->open_list_head
)) {
932 struct list_head
*p
, *n
;
933 struct snd_timer_instance
*ti
;
934 pr_warn("ALSA: timer %p is busy?\n", timer
);
935 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
937 ti
= list_entry(p
, struct snd_timer_instance
, open_list
);
941 list_del(&timer
->device_list
);
942 mutex_unlock(®ister_mutex
);
944 if (timer
->private_free
)
945 timer
->private_free(timer
);
950 static int snd_timer_dev_free(struct snd_device
*device
)
952 struct snd_timer
*timer
= device
->device_data
;
953 return snd_timer_free(timer
);
956 static int snd_timer_dev_register(struct snd_device
*dev
)
958 struct snd_timer
*timer
= dev
->device_data
;
959 struct snd_timer
*timer1
;
961 if (snd_BUG_ON(!timer
|| !timer
->hw
.start
|| !timer
->hw
.stop
))
963 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
964 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
967 mutex_lock(®ister_mutex
);
968 list_for_each_entry(timer1
, &snd_timer_list
, device_list
) {
969 if (timer1
->tmr_class
> timer
->tmr_class
)
971 if (timer1
->tmr_class
< timer
->tmr_class
)
973 if (timer1
->card
&& timer
->card
) {
974 if (timer1
->card
->number
> timer
->card
->number
)
976 if (timer1
->card
->number
< timer
->card
->number
)
979 if (timer1
->tmr_device
> timer
->tmr_device
)
981 if (timer1
->tmr_device
< timer
->tmr_device
)
983 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
985 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
988 mutex_unlock(®ister_mutex
);
991 list_add_tail(&timer
->device_list
, &timer1
->device_list
);
992 mutex_unlock(®ister_mutex
);
996 static int snd_timer_dev_disconnect(struct snd_device
*device
)
998 struct snd_timer
*timer
= device
->device_data
;
999 struct snd_timer_instance
*ti
;
1001 mutex_lock(®ister_mutex
);
1002 list_del_init(&timer
->device_list
);
1003 /* wake up pending sleepers */
1004 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
) {
1008 mutex_unlock(®ister_mutex
);
1012 void snd_timer_notify(struct snd_timer
*timer
, int event
, struct timespec
*tstamp
)
1014 unsigned long flags
;
1015 unsigned long resolution
= 0;
1016 struct snd_timer_instance
*ti
, *ts
;
1018 if (timer
->card
&& timer
->card
->shutdown
)
1020 if (! (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
))
1022 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_MSTART
||
1023 event
> SNDRV_TIMER_EVENT_MRESUME
))
1025 spin_lock_irqsave(&timer
->lock
, flags
);
1026 if (event
== SNDRV_TIMER_EVENT_MSTART
||
1027 event
== SNDRV_TIMER_EVENT_MCONTINUE
||
1028 event
== SNDRV_TIMER_EVENT_MRESUME
) {
1029 if (timer
->hw
.c_resolution
)
1030 resolution
= timer
->hw
.c_resolution(timer
);
1032 resolution
= timer
->hw
.resolution
;
1034 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
1036 ti
->ccallback(ti
, event
, tstamp
, resolution
);
1037 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
1039 ts
->ccallback(ts
, event
, tstamp
, resolution
);
1041 spin_unlock_irqrestore(&timer
->lock
, flags
);
1043 EXPORT_SYMBOL(snd_timer_notify
);
1046 * exported functions for global timers
1048 int snd_timer_global_new(char *id
, int device
, struct snd_timer
**rtimer
)
1050 struct snd_timer_id tid
;
1052 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
1053 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1055 tid
.device
= device
;
1057 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
1059 EXPORT_SYMBOL(snd_timer_global_new
);
1061 int snd_timer_global_free(struct snd_timer
*timer
)
1063 return snd_timer_free(timer
);
1065 EXPORT_SYMBOL(snd_timer_global_free
);
1067 int snd_timer_global_register(struct snd_timer
*timer
)
1069 struct snd_device dev
;
1071 memset(&dev
, 0, sizeof(dev
));
1072 dev
.device_data
= timer
;
1073 return snd_timer_dev_register(&dev
);
1075 EXPORT_SYMBOL(snd_timer_global_register
);
1081 struct snd_timer_system_private
{
1082 struct timer_list tlist
;
1083 struct snd_timer
*snd_timer
;
1084 unsigned long last_expires
;
1085 unsigned long last_jiffies
;
1086 unsigned long correction
;
1089 static void snd_timer_s_function(struct timer_list
*t
)
1091 struct snd_timer_system_private
*priv
= from_timer(priv
, t
,
1093 struct snd_timer
*timer
= priv
->snd_timer
;
1094 unsigned long jiff
= jiffies
;
1095 if (time_after(jiff
, priv
->last_expires
))
1096 priv
->correction
+= (long)jiff
- (long)priv
->last_expires
;
1097 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
1100 static int snd_timer_s_start(struct snd_timer
* timer
)
1102 struct snd_timer_system_private
*priv
;
1103 unsigned long njiff
;
1105 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1106 njiff
= (priv
->last_jiffies
= jiffies
);
1107 if (priv
->correction
> timer
->sticks
- 1) {
1108 priv
->correction
-= timer
->sticks
- 1;
1111 njiff
+= timer
->sticks
- priv
->correction
;
1112 priv
->correction
= 0;
1114 priv
->last_expires
= njiff
;
1115 mod_timer(&priv
->tlist
, njiff
);
1119 static int snd_timer_s_stop(struct snd_timer
* timer
)
1121 struct snd_timer_system_private
*priv
;
1124 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1125 del_timer(&priv
->tlist
);
1127 if (time_before(jiff
, priv
->last_expires
))
1128 timer
->sticks
= priv
->last_expires
- jiff
;
1131 priv
->correction
= 0;
1135 static int snd_timer_s_close(struct snd_timer
*timer
)
1137 struct snd_timer_system_private
*priv
;
1139 priv
= (struct snd_timer_system_private
*)timer
->private_data
;
1140 del_timer_sync(&priv
->tlist
);
1144 static struct snd_timer_hardware snd_timer_system
=
1146 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
1147 .resolution
= 1000000000L / HZ
,
1149 .close
= snd_timer_s_close
,
1150 .start
= snd_timer_s_start
,
1151 .stop
= snd_timer_s_stop
1154 static void snd_timer_free_system(struct snd_timer
*timer
)
1156 kfree(timer
->private_data
);
1159 static int snd_timer_register_system(void)
1161 struct snd_timer
*timer
;
1162 struct snd_timer_system_private
*priv
;
1165 err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
);
1168 strcpy(timer
->name
, "system timer");
1169 timer
->hw
= snd_timer_system
;
1170 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1172 snd_timer_free(timer
);
1175 priv
->snd_timer
= timer
;
1176 timer_setup(&priv
->tlist
, snd_timer_s_function
, 0);
1177 timer
->private_data
= priv
;
1178 timer
->private_free
= snd_timer_free_system
;
1179 return snd_timer_global_register(timer
);
1182 #ifdef CONFIG_SND_PROC_FS
1187 static void snd_timer_proc_read(struct snd_info_entry
*entry
,
1188 struct snd_info_buffer
*buffer
)
1190 struct snd_timer
*timer
;
1191 struct snd_timer_instance
*ti
;
1193 mutex_lock(®ister_mutex
);
1194 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
1195 if (timer
->card
&& timer
->card
->shutdown
)
1197 switch (timer
->tmr_class
) {
1198 case SNDRV_TIMER_CLASS_GLOBAL
:
1199 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1201 case SNDRV_TIMER_CLASS_CARD
:
1202 snd_iprintf(buffer
, "C%i-%i: ",
1203 timer
->card
->number
, timer
->tmr_device
);
1205 case SNDRV_TIMER_CLASS_PCM
:
1206 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
,
1207 timer
->tmr_device
, timer
->tmr_subdevice
);
1210 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
,
1211 timer
->card
? timer
->card
->number
: -1,
1212 timer
->tmr_device
, timer
->tmr_subdevice
);
1214 snd_iprintf(buffer
, "%s :", timer
->name
);
1215 if (timer
->hw
.resolution
)
1216 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)",
1217 timer
->hw
.resolution
/ 1000,
1218 timer
->hw
.resolution
% 1000,
1220 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1221 snd_iprintf(buffer
, " SLAVE");
1222 snd_iprintf(buffer
, "\n");
1223 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
)
1224 snd_iprintf(buffer
, " Client %s : %s\n",
1225 ti
->owner
? ti
->owner
: "unknown",
1226 ti
->flags
& (SNDRV_TIMER_IFLG_START
|
1227 SNDRV_TIMER_IFLG_RUNNING
)
1228 ? "running" : "stopped");
1230 mutex_unlock(®ister_mutex
);
1233 static struct snd_info_entry
*snd_timer_proc_entry
;
1235 static void __init
snd_timer_proc_init(void)
1237 struct snd_info_entry
*entry
;
1239 entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
);
1240 if (entry
!= NULL
) {
1241 entry
->c
.text
.read
= snd_timer_proc_read
;
1242 if (snd_info_register(entry
) < 0) {
1243 snd_info_free_entry(entry
);
1247 snd_timer_proc_entry
= entry
;
1250 static void __exit
snd_timer_proc_done(void)
1252 snd_info_free_entry(snd_timer_proc_entry
);
1254 #else /* !CONFIG_SND_PROC_FS */
1255 #define snd_timer_proc_init()
1256 #define snd_timer_proc_done()
1260 * USER SPACE interface
1263 static void snd_timer_user_interrupt(struct snd_timer_instance
*timeri
,
1264 unsigned long resolution
,
1265 unsigned long ticks
)
1267 struct snd_timer_user
*tu
= timeri
->callback_data
;
1268 struct snd_timer_read
*r
;
1271 spin_lock(&tu
->qlock
);
1272 if (tu
->qused
> 0) {
1273 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1274 r
= &tu
->queue
[prev
];
1275 if (r
->resolution
== resolution
) {
1280 if (tu
->qused
>= tu
->queue_size
) {
1283 r
= &tu
->queue
[tu
->qtail
++];
1284 tu
->qtail
%= tu
->queue_size
;
1285 r
->resolution
= resolution
;
1290 spin_unlock(&tu
->qlock
);
1291 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1292 wake_up(&tu
->qchange_sleep
);
1295 static void snd_timer_user_append_to_tqueue(struct snd_timer_user
*tu
,
1296 struct snd_timer_tread
*tread
)
1298 if (tu
->qused
>= tu
->queue_size
) {
1301 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1302 tu
->qtail
%= tu
->queue_size
;
1307 static void snd_timer_user_ccallback(struct snd_timer_instance
*timeri
,
1309 struct timespec
*tstamp
,
1310 unsigned long resolution
)
1312 struct snd_timer_user
*tu
= timeri
->callback_data
;
1313 struct snd_timer_tread r1
;
1314 unsigned long flags
;
1316 if (event
>= SNDRV_TIMER_EVENT_START
&&
1317 event
<= SNDRV_TIMER_EVENT_PAUSE
)
1318 tu
->tstamp
= *tstamp
;
1319 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1321 memset(&r1
, 0, sizeof(r1
));
1323 r1
.tstamp
= *tstamp
;
1324 r1
.val
= resolution
;
1325 spin_lock_irqsave(&tu
->qlock
, flags
);
1326 snd_timer_user_append_to_tqueue(tu
, &r1
);
1327 spin_unlock_irqrestore(&tu
->qlock
, flags
);
1328 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1329 wake_up(&tu
->qchange_sleep
);
1332 static void snd_timer_user_disconnect(struct snd_timer_instance
*timeri
)
1334 struct snd_timer_user
*tu
= timeri
->callback_data
;
1336 tu
->disconnected
= true;
1337 wake_up(&tu
->qchange_sleep
);
1340 static void snd_timer_user_tinterrupt(struct snd_timer_instance
*timeri
,
1341 unsigned long resolution
,
1342 unsigned long ticks
)
1344 struct snd_timer_user
*tu
= timeri
->callback_data
;
1345 struct snd_timer_tread
*r
, r1
;
1346 struct timespec tstamp
;
1347 int prev
, append
= 0;
1349 memset(&r1
, 0, sizeof(r1
));
1350 memset(&tstamp
, 0, sizeof(tstamp
));
1351 spin_lock(&tu
->qlock
);
1352 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
) |
1353 (1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1354 spin_unlock(&tu
->qlock
);
1357 if (tu
->last_resolution
!= resolution
|| ticks
> 0) {
1358 if (timer_tstamp_monotonic
)
1359 ktime_get_ts(&tstamp
);
1361 getnstimeofday(&tstamp
);
1363 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) &&
1364 tu
->last_resolution
!= resolution
) {
1365 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1367 r1
.val
= resolution
;
1368 snd_timer_user_append_to_tqueue(tu
, &r1
);
1369 tu
->last_resolution
= resolution
;
1372 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1376 if (tu
->qused
> 0) {
1377 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1378 r
= &tu
->tqueue
[prev
];
1379 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1386 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1389 snd_timer_user_append_to_tqueue(tu
, &r1
);
1392 spin_unlock(&tu
->qlock
);
1395 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1396 wake_up(&tu
->qchange_sleep
);
1399 static int realloc_user_queue(struct snd_timer_user
*tu
, int size
)
1401 struct snd_timer_read
*queue
= NULL
;
1402 struct snd_timer_tread
*tqueue
= NULL
;
1405 tqueue
= kcalloc(size
, sizeof(*tqueue
), GFP_KERNEL
);
1409 queue
= kcalloc(size
, sizeof(*queue
), GFP_KERNEL
);
1414 spin_lock_irq(&tu
->qlock
);
1417 tu
->queue_size
= size
;
1419 tu
->tqueue
= tqueue
;
1420 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1421 spin_unlock_irq(&tu
->qlock
);
1426 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1428 struct snd_timer_user
*tu
;
1431 err
= nonseekable_open(inode
, file
);
1435 tu
= kzalloc(sizeof(*tu
), GFP_KERNEL
);
1438 spin_lock_init(&tu
->qlock
);
1439 init_waitqueue_head(&tu
->qchange_sleep
);
1440 mutex_init(&tu
->ioctl_lock
);
1442 if (realloc_user_queue(tu
, 128) < 0) {
1446 file
->private_data
= tu
;
1450 static int snd_timer_user_release(struct inode
*inode
, struct file
*file
)
1452 struct snd_timer_user
*tu
;
1454 if (file
->private_data
) {
1455 tu
= file
->private_data
;
1456 file
->private_data
= NULL
;
1457 mutex_lock(&tu
->ioctl_lock
);
1459 snd_timer_close(tu
->timeri
);
1460 mutex_unlock(&tu
->ioctl_lock
);
1468 static void snd_timer_user_zero_id(struct snd_timer_id
*id
)
1470 id
->dev_class
= SNDRV_TIMER_CLASS_NONE
;
1471 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1477 static void snd_timer_user_copy_id(struct snd_timer_id
*id
, struct snd_timer
*timer
)
1479 id
->dev_class
= timer
->tmr_class
;
1480 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1481 id
->card
= timer
->card
? timer
->card
->number
: -1;
1482 id
->device
= timer
->tmr_device
;
1483 id
->subdevice
= timer
->tmr_subdevice
;
1486 static int snd_timer_user_next_device(struct snd_timer_id __user
*_tid
)
1488 struct snd_timer_id id
;
1489 struct snd_timer
*timer
;
1490 struct list_head
*p
;
1492 if (copy_from_user(&id
, _tid
, sizeof(id
)))
1494 mutex_lock(®ister_mutex
);
1495 if (id
.dev_class
< 0) { /* first item */
1496 if (list_empty(&snd_timer_list
))
1497 snd_timer_user_zero_id(&id
);
1499 timer
= list_entry(snd_timer_list
.next
,
1500 struct snd_timer
, device_list
);
1501 snd_timer_user_copy_id(&id
, timer
);
1504 switch (id
.dev_class
) {
1505 case SNDRV_TIMER_CLASS_GLOBAL
:
1506 id
.device
= id
.device
< 0 ? 0 : id
.device
+ 1;
1507 list_for_each(p
, &snd_timer_list
) {
1508 timer
= list_entry(p
, struct snd_timer
, device_list
);
1509 if (timer
->tmr_class
> SNDRV_TIMER_CLASS_GLOBAL
) {
1510 snd_timer_user_copy_id(&id
, timer
);
1513 if (timer
->tmr_device
>= id
.device
) {
1514 snd_timer_user_copy_id(&id
, timer
);
1518 if (p
== &snd_timer_list
)
1519 snd_timer_user_zero_id(&id
);
1521 case SNDRV_TIMER_CLASS_CARD
:
1522 case SNDRV_TIMER_CLASS_PCM
:
1526 if (id
.device
< 0) {
1529 if (id
.subdevice
< 0)
1531 else if (id
.subdevice
< INT_MAX
)
1535 list_for_each(p
, &snd_timer_list
) {
1536 timer
= list_entry(p
, struct snd_timer
, device_list
);
1537 if (timer
->tmr_class
> id
.dev_class
) {
1538 snd_timer_user_copy_id(&id
, timer
);
1541 if (timer
->tmr_class
< id
.dev_class
)
1543 if (timer
->card
->number
> id
.card
) {
1544 snd_timer_user_copy_id(&id
, timer
);
1547 if (timer
->card
->number
< id
.card
)
1549 if (timer
->tmr_device
> id
.device
) {
1550 snd_timer_user_copy_id(&id
, timer
);
1553 if (timer
->tmr_device
< id
.device
)
1555 if (timer
->tmr_subdevice
> id
.subdevice
) {
1556 snd_timer_user_copy_id(&id
, timer
);
1559 if (timer
->tmr_subdevice
< id
.subdevice
)
1561 snd_timer_user_copy_id(&id
, timer
);
1564 if (p
== &snd_timer_list
)
1565 snd_timer_user_zero_id(&id
);
1568 snd_timer_user_zero_id(&id
);
1571 mutex_unlock(®ister_mutex
);
1572 if (copy_to_user(_tid
, &id
, sizeof(*_tid
)))
1577 static int snd_timer_user_ginfo(struct file
*file
,
1578 struct snd_timer_ginfo __user
*_ginfo
)
1580 struct snd_timer_ginfo
*ginfo
;
1581 struct snd_timer_id tid
;
1582 struct snd_timer
*t
;
1583 struct list_head
*p
;
1586 ginfo
= memdup_user(_ginfo
, sizeof(*ginfo
));
1588 return PTR_ERR(ginfo
);
1591 memset(ginfo
, 0, sizeof(*ginfo
));
1593 mutex_lock(®ister_mutex
);
1594 t
= snd_timer_find(&tid
);
1596 ginfo
->card
= t
->card
? t
->card
->number
: -1;
1597 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1598 ginfo
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1599 strlcpy(ginfo
->id
, t
->id
, sizeof(ginfo
->id
));
1600 strlcpy(ginfo
->name
, t
->name
, sizeof(ginfo
->name
));
1601 ginfo
->resolution
= t
->hw
.resolution
;
1602 if (t
->hw
.resolution_min
> 0) {
1603 ginfo
->resolution_min
= t
->hw
.resolution_min
;
1604 ginfo
->resolution_max
= t
->hw
.resolution_max
;
1606 list_for_each(p
, &t
->open_list_head
) {
1612 mutex_unlock(®ister_mutex
);
1613 if (err
>= 0 && copy_to_user(_ginfo
, ginfo
, sizeof(*ginfo
)))
1619 static int timer_set_gparams(struct snd_timer_gparams
*gparams
)
1621 struct snd_timer
*t
;
1624 mutex_lock(®ister_mutex
);
1625 t
= snd_timer_find(&gparams
->tid
);
1630 if (!list_empty(&t
->open_list_head
)) {
1634 if (!t
->hw
.set_period
) {
1638 err
= t
->hw
.set_period(t
, gparams
->period_num
, gparams
->period_den
);
1640 mutex_unlock(®ister_mutex
);
1644 static int snd_timer_user_gparams(struct file
*file
,
1645 struct snd_timer_gparams __user
*_gparams
)
1647 struct snd_timer_gparams gparams
;
1649 if (copy_from_user(&gparams
, _gparams
, sizeof(gparams
)))
1651 return timer_set_gparams(&gparams
);
1654 static int snd_timer_user_gstatus(struct file
*file
,
1655 struct snd_timer_gstatus __user
*_gstatus
)
1657 struct snd_timer_gstatus gstatus
;
1658 struct snd_timer_id tid
;
1659 struct snd_timer
*t
;
1662 if (copy_from_user(&gstatus
, _gstatus
, sizeof(gstatus
)))
1665 memset(&gstatus
, 0, sizeof(gstatus
));
1667 mutex_lock(®ister_mutex
);
1668 t
= snd_timer_find(&tid
);
1670 if (t
->hw
.c_resolution
)
1671 gstatus
.resolution
= t
->hw
.c_resolution(t
);
1673 gstatus
.resolution
= t
->hw
.resolution
;
1674 if (t
->hw
.precise_resolution
) {
1675 t
->hw
.precise_resolution(t
, &gstatus
.resolution_num
,
1676 &gstatus
.resolution_den
);
1678 gstatus
.resolution_num
= gstatus
.resolution
;
1679 gstatus
.resolution_den
= 1000000000uL;
1684 mutex_unlock(®ister_mutex
);
1685 if (err
>= 0 && copy_to_user(_gstatus
, &gstatus
, sizeof(gstatus
)))
1690 static int snd_timer_user_tselect(struct file
*file
,
1691 struct snd_timer_select __user
*_tselect
)
1693 struct snd_timer_user
*tu
;
1694 struct snd_timer_select tselect
;
1698 tu
= file
->private_data
;
1700 snd_timer_close(tu
->timeri
);
1703 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
))) {
1707 sprintf(str
, "application %i", current
->pid
);
1708 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1709 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1710 err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
);
1714 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1715 tu
->timeri
->callback
= tu
->tread
1716 ? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1717 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1718 tu
->timeri
->callback_data
= (void *)tu
;
1719 tu
->timeri
->disconnect
= snd_timer_user_disconnect
;
1725 static int snd_timer_user_info(struct file
*file
,
1726 struct snd_timer_info __user
*_info
)
1728 struct snd_timer_user
*tu
;
1729 struct snd_timer_info
*info
;
1730 struct snd_timer
*t
;
1733 tu
= file
->private_data
;
1736 t
= tu
->timeri
->timer
;
1740 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1743 info
->card
= t
->card
? t
->card
->number
: -1;
1744 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1745 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1746 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1747 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1748 info
->resolution
= t
->hw
.resolution
;
1749 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1755 static int snd_timer_user_params(struct file
*file
,
1756 struct snd_timer_params __user
*_params
)
1758 struct snd_timer_user
*tu
;
1759 struct snd_timer_params params
;
1760 struct snd_timer
*t
;
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
) {
1823 err
= realloc_user_queue(tu
, params
.queue_size
);
1827 spin_lock_irq(&tu
->qlock
);
1828 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1829 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1831 struct snd_timer_tread tread
;
1832 memset(&tread
, 0, sizeof(tread
));
1833 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1834 tread
.tstamp
.tv_sec
= 0;
1835 tread
.tstamp
.tv_nsec
= 0;
1837 snd_timer_user_append_to_tqueue(tu
, &tread
);
1839 struct snd_timer_read
*r
= &tu
->queue
[0];
1846 tu
->filter
= params
.filter
;
1847 tu
->ticks
= params
.ticks
;
1848 spin_unlock_irq(&tu
->qlock
);
1851 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1856 static int snd_timer_user_status(struct file
*file
,
1857 struct snd_timer_status __user
*_status
)
1859 struct snd_timer_user
*tu
;
1860 struct snd_timer_status status
;
1862 tu
= file
->private_data
;
1865 memset(&status
, 0, sizeof(status
));
1866 status
.tstamp
= tu
->tstamp
;
1867 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1868 status
.lost
= tu
->timeri
->lost
;
1869 status
.overrun
= tu
->overrun
;
1870 spin_lock_irq(&tu
->qlock
);
1871 status
.queue
= tu
->qused
;
1872 spin_unlock_irq(&tu
->qlock
);
1873 if (copy_to_user(_status
, &status
, sizeof(status
)))
1878 static int snd_timer_user_start(struct file
*file
)
1881 struct snd_timer_user
*tu
;
1883 tu
= file
->private_data
;
1886 snd_timer_stop(tu
->timeri
);
1887 tu
->timeri
->lost
= 0;
1888 tu
->last_resolution
= 0;
1889 return (err
= snd_timer_start(tu
->timeri
, tu
->ticks
)) < 0 ? err
: 0;
1892 static int snd_timer_user_stop(struct file
*file
)
1895 struct snd_timer_user
*tu
;
1897 tu
= file
->private_data
;
1900 return (err
= snd_timer_stop(tu
->timeri
)) < 0 ? err
: 0;
1903 static int snd_timer_user_continue(struct file
*file
)
1906 struct snd_timer_user
*tu
;
1908 tu
= file
->private_data
;
1911 /* start timer instead of continue if it's not used before */
1912 if (!(tu
->timeri
->flags
& SNDRV_TIMER_IFLG_PAUSED
))
1913 return snd_timer_user_start(file
);
1914 tu
->timeri
->lost
= 0;
1915 return (err
= snd_timer_continue(tu
->timeri
)) < 0 ? err
: 0;
1918 static int snd_timer_user_pause(struct file
*file
)
1921 struct snd_timer_user
*tu
;
1923 tu
= file
->private_data
;
1926 return (err
= snd_timer_pause(tu
->timeri
)) < 0 ? err
: 0;
1930 SNDRV_TIMER_IOCTL_START_OLD
= _IO('T', 0x20),
1931 SNDRV_TIMER_IOCTL_STOP_OLD
= _IO('T', 0x21),
1932 SNDRV_TIMER_IOCTL_CONTINUE_OLD
= _IO('T', 0x22),
1933 SNDRV_TIMER_IOCTL_PAUSE_OLD
= _IO('T', 0x23),
1936 static long __snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1939 struct snd_timer_user
*tu
;
1940 void __user
*argp
= (void __user
*)arg
;
1941 int __user
*p
= argp
;
1943 tu
= file
->private_data
;
1945 case SNDRV_TIMER_IOCTL_PVERSION
:
1946 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1947 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1948 return snd_timer_user_next_device(argp
);
1949 case SNDRV_TIMER_IOCTL_TREAD
:
1951 int xarg
, old_tread
;
1953 if (tu
->timeri
) /* too late */
1955 if (get_user(xarg
, p
))
1957 old_tread
= tu
->tread
;
1958 tu
->tread
= xarg
? 1 : 0;
1959 if (tu
->tread
!= old_tread
&&
1960 realloc_user_queue(tu
, tu
->queue_size
) < 0) {
1961 tu
->tread
= old_tread
;
1966 case SNDRV_TIMER_IOCTL_GINFO
:
1967 return snd_timer_user_ginfo(file
, argp
);
1968 case SNDRV_TIMER_IOCTL_GPARAMS
:
1969 return snd_timer_user_gparams(file
, argp
);
1970 case SNDRV_TIMER_IOCTL_GSTATUS
:
1971 return snd_timer_user_gstatus(file
, argp
);
1972 case SNDRV_TIMER_IOCTL_SELECT
:
1973 return snd_timer_user_tselect(file
, argp
);
1974 case SNDRV_TIMER_IOCTL_INFO
:
1975 return snd_timer_user_info(file
, argp
);
1976 case SNDRV_TIMER_IOCTL_PARAMS
:
1977 return snd_timer_user_params(file
, argp
);
1978 case SNDRV_TIMER_IOCTL_STATUS
:
1979 return snd_timer_user_status(file
, argp
);
1980 case SNDRV_TIMER_IOCTL_START
:
1981 case SNDRV_TIMER_IOCTL_START_OLD
:
1982 return snd_timer_user_start(file
);
1983 case SNDRV_TIMER_IOCTL_STOP
:
1984 case SNDRV_TIMER_IOCTL_STOP_OLD
:
1985 return snd_timer_user_stop(file
);
1986 case SNDRV_TIMER_IOCTL_CONTINUE
:
1987 case SNDRV_TIMER_IOCTL_CONTINUE_OLD
:
1988 return snd_timer_user_continue(file
);
1989 case SNDRV_TIMER_IOCTL_PAUSE
:
1990 case SNDRV_TIMER_IOCTL_PAUSE_OLD
:
1991 return snd_timer_user_pause(file
);
1996 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1999 struct snd_timer_user
*tu
= file
->private_data
;
2002 mutex_lock(&tu
->ioctl_lock
);
2003 ret
= __snd_timer_user_ioctl(file
, cmd
, arg
);
2004 mutex_unlock(&tu
->ioctl_lock
);
2008 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
2010 struct snd_timer_user
*tu
;
2012 tu
= file
->private_data
;
2013 return fasync_helper(fd
, file
, on
, &tu
->fasync
);
2016 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
,
2017 size_t count
, loff_t
*offset
)
2019 struct snd_timer_user
*tu
;
2020 long result
= 0, unit
;
2024 tu
= file
->private_data
;
2025 unit
= tu
->tread
? sizeof(struct snd_timer_tread
) : sizeof(struct snd_timer_read
);
2026 mutex_lock(&tu
->ioctl_lock
);
2027 spin_lock_irq(&tu
->qlock
);
2028 while ((long)count
- result
>= unit
) {
2029 while (!tu
->qused
) {
2030 wait_queue_entry_t wait
;
2032 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
2037 set_current_state(TASK_INTERRUPTIBLE
);
2038 init_waitqueue_entry(&wait
, current
);
2039 add_wait_queue(&tu
->qchange_sleep
, &wait
);
2041 spin_unlock_irq(&tu
->qlock
);
2042 mutex_unlock(&tu
->ioctl_lock
);
2044 mutex_lock(&tu
->ioctl_lock
);
2045 spin_lock_irq(&tu
->qlock
);
2047 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
2049 if (tu
->disconnected
) {
2053 if (signal_pending(current
)) {
2059 qhead
= tu
->qhead
++;
2060 tu
->qhead
%= tu
->queue_size
;
2062 spin_unlock_irq(&tu
->qlock
);
2065 if (copy_to_user(buffer
, &tu
->tqueue
[qhead
],
2066 sizeof(struct snd_timer_tread
)))
2069 if (copy_to_user(buffer
, &tu
->queue
[qhead
],
2070 sizeof(struct snd_timer_read
)))
2074 spin_lock_irq(&tu
->qlock
);
2081 spin_unlock_irq(&tu
->qlock
);
2082 mutex_unlock(&tu
->ioctl_lock
);
2083 return result
> 0 ? result
: err
;
2086 static unsigned int snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
2089 struct snd_timer_user
*tu
;
2091 tu
= file
->private_data
;
2093 poll_wait(file
, &tu
->qchange_sleep
, wait
);
2096 spin_lock_irq(&tu
->qlock
);
2098 mask
|= POLLIN
| POLLRDNORM
;
2099 if (tu
->disconnected
)
2101 spin_unlock_irq(&tu
->qlock
);
2106 #ifdef CONFIG_COMPAT
2107 #include "timer_compat.c"
2109 #define snd_timer_user_ioctl_compat NULL
2112 static const struct file_operations snd_timer_f_ops
=
2114 .owner
= THIS_MODULE
,
2115 .read
= snd_timer_user_read
,
2116 .open
= snd_timer_user_open
,
2117 .release
= snd_timer_user_release
,
2118 .llseek
= no_llseek
,
2119 .poll
= snd_timer_user_poll
,
2120 .unlocked_ioctl
= snd_timer_user_ioctl
,
2121 .compat_ioctl
= snd_timer_user_ioctl_compat
,
2122 .fasync
= snd_timer_user_fasync
,
2125 /* unregister the system timer */
2126 static void snd_timer_free_all(void)
2128 struct snd_timer
*timer
, *n
;
2130 list_for_each_entry_safe(timer
, n
, &snd_timer_list
, device_list
)
2131 snd_timer_free(timer
);
2134 static struct device timer_dev
;
2140 static int __init
alsa_timer_init(void)
2144 snd_device_initialize(&timer_dev
, NULL
);
2145 dev_set_name(&timer_dev
, "timer");
2147 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2148 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1,
2152 err
= snd_timer_register_system();
2154 pr_err("ALSA: unable to register system timer (%i)\n", err
);
2158 err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0,
2159 &snd_timer_f_ops
, NULL
, &timer_dev
);
2161 pr_err("ALSA: unable to register timer device (%i)\n", err
);
2162 snd_timer_free_all();
2166 snd_timer_proc_init();
2170 put_device(&timer_dev
);
2174 static void __exit
alsa_timer_exit(void)
2176 snd_unregister_device(&timer_dev
);
2177 snd_timer_free_all();
2178 put_device(&timer_dev
);
2179 snd_timer_proc_done();
2180 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2181 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
2185 module_init(alsa_timer_init
)
2186 module_exit(alsa_timer_exit
)