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 #define MAX_SLAVE_INSTANCES 1000
92 static int num_slaves
;
94 static DEFINE_MUTEX(register_mutex
);
96 static int snd_timer_free(struct snd_timer
*timer
);
97 static int snd_timer_dev_free(struct snd_device
*device
);
98 static int snd_timer_dev_register(struct snd_device
*device
);
99 static int snd_timer_dev_disconnect(struct snd_device
*device
);
101 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
);
104 * create a timer instance with the given owner string.
105 * when timer is not NULL, increments the module counter
107 static struct snd_timer_instance
*snd_timer_instance_new(char *owner
,
108 struct snd_timer
*timer
)
110 struct snd_timer_instance
*timeri
;
111 timeri
= kzalloc(sizeof(*timeri
), GFP_KERNEL
);
114 timeri
->owner
= kstrdup(owner
, GFP_KERNEL
);
115 if (! timeri
->owner
) {
119 INIT_LIST_HEAD(&timeri
->open_list
);
120 INIT_LIST_HEAD(&timeri
->active_list
);
121 INIT_LIST_HEAD(&timeri
->ack_list
);
122 INIT_LIST_HEAD(&timeri
->slave_list_head
);
123 INIT_LIST_HEAD(&timeri
->slave_active_head
);
125 timeri
->timer
= timer
;
126 if (timer
&& !try_module_get(timer
->module
)) {
127 kfree(timeri
->owner
);
136 * find a timer instance from the given timer id
138 static struct snd_timer
*snd_timer_find(struct snd_timer_id
*tid
)
140 struct snd_timer
*timer
= NULL
;
142 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
143 if (timer
->tmr_class
!= tid
->dev_class
)
145 if ((timer
->tmr_class
== SNDRV_TIMER_CLASS_CARD
||
146 timer
->tmr_class
== SNDRV_TIMER_CLASS_PCM
) &&
147 (timer
->card
== NULL
||
148 timer
->card
->number
!= tid
->card
))
150 if (timer
->tmr_device
!= tid
->device
)
152 if (timer
->tmr_subdevice
!= tid
->subdevice
)
159 #ifdef CONFIG_MODULES
161 static void snd_timer_request(struct snd_timer_id
*tid
)
163 switch (tid
->dev_class
) {
164 case SNDRV_TIMER_CLASS_GLOBAL
:
165 if (tid
->device
< timer_limit
)
166 request_module("snd-timer-%i", tid
->device
);
168 case SNDRV_TIMER_CLASS_CARD
:
169 case SNDRV_TIMER_CLASS_PCM
:
170 if (tid
->card
< snd_ecards_limit
)
171 request_module("snd-card-%i", tid
->card
);
181 * look for a master instance matching with the slave id of the given slave.
182 * when found, relink the open_link of the slave.
184 * call this with register_mutex down.
186 static int snd_timer_check_slave(struct snd_timer_instance
*slave
)
188 struct snd_timer
*timer
;
189 struct snd_timer_instance
*master
;
191 /* FIXME: it's really dumb to look up all entries.. */
192 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
193 list_for_each_entry(master
, &timer
->open_list_head
, open_list
) {
194 if (slave
->slave_class
== master
->slave_class
&&
195 slave
->slave_id
== master
->slave_id
) {
196 if (master
->timer
->num_instances
>=
197 master
->timer
->max_instances
)
199 list_move_tail(&slave
->open_list
,
200 &master
->slave_list_head
);
201 master
->timer
->num_instances
++;
202 spin_lock_irq(&slave_active_lock
);
203 slave
->master
= master
;
204 slave
->timer
= master
->timer
;
205 spin_unlock_irq(&slave_active_lock
);
214 * look for slave instances matching with the slave id of the given master.
215 * when found, relink the open_link of slaves.
217 * call this with register_mutex down.
219 static int snd_timer_check_master(struct snd_timer_instance
*master
)
221 struct snd_timer_instance
*slave
, *tmp
;
223 /* check all pending slaves */
224 list_for_each_entry_safe(slave
, tmp
, &snd_timer_slave_list
, open_list
) {
225 if (slave
->slave_class
== master
->slave_class
&&
226 slave
->slave_id
== master
->slave_id
) {
227 if (master
->timer
->num_instances
>=
228 master
->timer
->max_instances
)
230 list_move_tail(&slave
->open_list
, &master
->slave_list_head
);
231 master
->timer
->num_instances
++;
232 spin_lock_irq(&slave_active_lock
);
233 spin_lock(&master
->timer
->lock
);
234 slave
->master
= master
;
235 slave
->timer
= master
->timer
;
236 if (slave
->flags
& SNDRV_TIMER_IFLG_RUNNING
)
237 list_add_tail(&slave
->active_list
,
238 &master
->slave_active_head
);
239 spin_unlock(&master
->timer
->lock
);
240 spin_unlock_irq(&slave_active_lock
);
246 static int snd_timer_close_locked(struct snd_timer_instance
*timeri
,
247 struct device
**card_devp_to_put
);
250 * open a timer instance
251 * when opening a master, the slave id must be here given.
253 int snd_timer_open(struct snd_timer_instance
**ti
,
254 char *owner
, struct snd_timer_id
*tid
,
255 unsigned int slave_id
)
257 struct snd_timer
*timer
;
258 struct snd_timer_instance
*timeri
= NULL
;
259 struct device
*card_dev_to_put
= NULL
;
262 mutex_lock(®ister_mutex
);
263 if (tid
->dev_class
== SNDRV_TIMER_CLASS_SLAVE
) {
264 /* open a slave instance */
265 if (tid
->dev_sclass
<= SNDRV_TIMER_SCLASS_NONE
||
266 tid
->dev_sclass
> SNDRV_TIMER_SCLASS_OSS_SEQUENCER
) {
267 pr_debug("ALSA: timer: invalid slave class %i\n",
272 if (num_slaves
>= MAX_SLAVE_INSTANCES
) {
276 timeri
= snd_timer_instance_new(owner
, NULL
);
281 timeri
->slave_class
= tid
->dev_sclass
;
282 timeri
->slave_id
= tid
->device
;
283 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
284 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
286 err
= snd_timer_check_slave(timeri
);
288 snd_timer_close_locked(timeri
, &card_dev_to_put
);
294 /* open a master instance */
295 timer
= snd_timer_find(tid
);
296 #ifdef CONFIG_MODULES
298 mutex_unlock(®ister_mutex
);
299 snd_timer_request(tid
);
300 mutex_lock(®ister_mutex
);
301 timer
= snd_timer_find(tid
);
308 if (!list_empty(&timer
->open_list_head
)) {
309 struct snd_timer_instance
*t
=
310 list_entry(timer
->open_list_head
.next
,
311 struct snd_timer_instance
, open_list
);
312 if (t
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
317 if (timer
->num_instances
>= timer
->max_instances
) {
321 timeri
= snd_timer_instance_new(owner
, timer
);
326 /* take a card refcount for safe disconnection */
328 get_device(&timer
->card
->card_dev
);
329 timeri
->slave_class
= tid
->dev_sclass
;
330 timeri
->slave_id
= slave_id
;
332 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
) {
333 err
= timer
->hw
.open(timer
);
335 kfree(timeri
->owner
);
340 card_dev_to_put
= &timer
->card
->card_dev
;
341 module_put(timer
->module
);
346 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
347 timer
->num_instances
++;
348 err
= snd_timer_check_master(timeri
);
350 snd_timer_close_locked(timeri
, &card_dev_to_put
);
355 mutex_unlock(®ister_mutex
);
356 /* put_device() is called after unlock for avoiding deadlock */
358 put_device(card_dev_to_put
);
362 EXPORT_SYMBOL(snd_timer_open
);
365 * close a timer instance
366 * call this with register_mutex down.
368 static int snd_timer_close_locked(struct snd_timer_instance
*timeri
,
369 struct device
**card_devp_to_put
)
371 struct snd_timer
*timer
= NULL
;
372 struct snd_timer_instance
*slave
, *tmp
;
374 list_del(&timeri
->open_list
);
375 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
378 /* force to stop the timer */
379 snd_timer_stop(timeri
);
381 timer
= timeri
->timer
;
383 timer
->num_instances
--;
384 /* wait, until the active callback is finished */
385 spin_lock_irq(&timer
->lock
);
386 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
387 spin_unlock_irq(&timer
->lock
);
389 spin_lock_irq(&timer
->lock
);
391 spin_unlock_irq(&timer
->lock
);
393 /* remove slave links */
394 spin_lock_irq(&slave_active_lock
);
395 spin_lock(&timer
->lock
);
396 list_for_each_entry_safe(slave
, tmp
, &timeri
->slave_list_head
,
398 list_move_tail(&slave
->open_list
, &snd_timer_slave_list
);
399 timer
->num_instances
--;
400 slave
->master
= NULL
;
402 list_del_init(&slave
->ack_list
);
403 list_del_init(&slave
->active_list
);
405 spin_unlock(&timer
->lock
);
406 spin_unlock_irq(&slave_active_lock
);
408 /* slave doesn't need to release timer resources below */
409 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
413 if (timeri
->private_free
)
414 timeri
->private_free(timeri
);
415 kfree(timeri
->owner
);
419 if (list_empty(&timer
->open_list_head
) && timer
->hw
.close
)
420 timer
->hw
.close(timer
);
421 /* release a card refcount for safe disconnection */
423 *card_devp_to_put
= &timer
->card
->card_dev
;
424 module_put(timer
->module
);
431 * close a timer instance
433 int snd_timer_close(struct snd_timer_instance
*timeri
)
435 struct device
*card_dev_to_put
= NULL
;
438 if (snd_BUG_ON(!timeri
))
441 mutex_lock(®ister_mutex
);
442 err
= snd_timer_close_locked(timeri
, &card_dev_to_put
);
443 mutex_unlock(®ister_mutex
);
444 /* put_device() is called after unlock for avoiding deadlock */
446 put_device(card_dev_to_put
);
449 EXPORT_SYMBOL(snd_timer_close
);
451 unsigned long snd_timer_resolution(struct snd_timer_instance
*timeri
)
453 struct snd_timer
* timer
;
457 timer
= timeri
->timer
;
459 if (timer
->hw
.c_resolution
)
460 return timer
->hw
.c_resolution(timer
);
461 return timer
->hw
.resolution
;
465 EXPORT_SYMBOL(snd_timer_resolution
);
467 static void snd_timer_notify1(struct snd_timer_instance
*ti
, int event
)
469 struct snd_timer
*timer
;
470 unsigned long resolution
= 0;
471 struct snd_timer_instance
*ts
;
472 struct timespec tstamp
;
474 if (timer_tstamp_monotonic
)
475 ktime_get_ts(&tstamp
);
477 getnstimeofday(&tstamp
);
478 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_START
||
479 event
> SNDRV_TIMER_EVENT_PAUSE
))
481 if (event
== SNDRV_TIMER_EVENT_START
||
482 event
== SNDRV_TIMER_EVENT_CONTINUE
)
483 resolution
= snd_timer_resolution(ti
);
485 ti
->ccallback(ti
, event
, &tstamp
, resolution
);
486 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
491 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
493 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
495 ts
->ccallback(ts
, event
+ 100, &tstamp
, resolution
);
498 /* start/continue a master timer */
499 static int snd_timer_start1(struct snd_timer_instance
*timeri
,
500 bool start
, unsigned long ticks
)
502 struct snd_timer
*timer
;
506 timer
= timeri
->timer
;
510 spin_lock_irqsave(&timer
->lock
, flags
);
511 if (timer
->card
&& timer
->card
->shutdown
) {
515 if (timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
516 SNDRV_TIMER_IFLG_START
)) {
522 timeri
->ticks
= timeri
->cticks
= ticks
;
523 else if (!timeri
->cticks
)
527 list_move_tail(&timeri
->active_list
, &timer
->active_list_head
);
528 if (timer
->running
) {
529 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
531 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
532 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
533 result
= 1; /* delayed start */
536 timer
->sticks
= ticks
;
537 timer
->hw
.start(timer
);
540 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
543 snd_timer_notify1(timeri
, start
? SNDRV_TIMER_EVENT_START
:
544 SNDRV_TIMER_EVENT_CONTINUE
);
546 spin_unlock_irqrestore(&timer
->lock
, flags
);
550 /* start/continue a slave timer */
551 static int snd_timer_start_slave(struct snd_timer_instance
*timeri
,
556 spin_lock_irqsave(&slave_active_lock
, flags
);
557 if (timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
558 spin_unlock_irqrestore(&slave_active_lock
, flags
);
561 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
562 if (timeri
->master
&& timeri
->timer
) {
563 spin_lock(&timeri
->timer
->lock
);
564 list_add_tail(&timeri
->active_list
,
565 &timeri
->master
->slave_active_head
);
566 snd_timer_notify1(timeri
, start
? SNDRV_TIMER_EVENT_START
:
567 SNDRV_TIMER_EVENT_CONTINUE
);
568 spin_unlock(&timeri
->timer
->lock
);
570 spin_unlock_irqrestore(&slave_active_lock
, flags
);
571 return 1; /* delayed start */
574 /* stop/pause a master timer */
575 static int snd_timer_stop1(struct snd_timer_instance
*timeri
, bool stop
)
577 struct snd_timer
*timer
;
581 timer
= timeri
->timer
;
584 spin_lock_irqsave(&timer
->lock
, flags
);
585 if (!(timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
586 SNDRV_TIMER_IFLG_START
))) {
590 list_del_init(&timeri
->ack_list
);
591 list_del_init(&timeri
->active_list
);
592 if (timer
->card
&& timer
->card
->shutdown
)
595 timeri
->cticks
= timeri
->ticks
;
598 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
599 !(--timer
->running
)) {
600 timer
->hw
.stop(timer
);
601 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
602 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
603 snd_timer_reschedule(timer
, 0);
604 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
605 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
606 timer
->hw
.start(timer
);
610 timeri
->flags
&= ~(SNDRV_TIMER_IFLG_RUNNING
| SNDRV_TIMER_IFLG_START
);
612 timeri
->flags
&= ~SNDRV_TIMER_IFLG_PAUSED
;
614 timeri
->flags
|= SNDRV_TIMER_IFLG_PAUSED
;
615 snd_timer_notify1(timeri
, stop
? SNDRV_TIMER_EVENT_STOP
:
616 SNDRV_TIMER_EVENT_PAUSE
);
618 spin_unlock_irqrestore(&timer
->lock
, flags
);
622 /* stop/pause a slave timer */
623 static int snd_timer_stop_slave(struct snd_timer_instance
*timeri
, bool stop
)
627 spin_lock_irqsave(&slave_active_lock
, flags
);
628 if (!(timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
)) {
629 spin_unlock_irqrestore(&slave_active_lock
, flags
);
632 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
634 spin_lock(&timeri
->timer
->lock
);
635 list_del_init(&timeri
->ack_list
);
636 list_del_init(&timeri
->active_list
);
637 snd_timer_notify1(timeri
, stop
? SNDRV_TIMER_EVENT_STOP
:
638 SNDRV_TIMER_EVENT_PAUSE
);
639 spin_unlock(&timeri
->timer
->lock
);
641 spin_unlock_irqrestore(&slave_active_lock
, flags
);
646 * start the timer instance
648 int snd_timer_start(struct snd_timer_instance
*timeri
, unsigned int ticks
)
650 if (timeri
== NULL
|| ticks
< 1)
652 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
653 return snd_timer_start_slave(timeri
, true);
655 return snd_timer_start1(timeri
, true, ticks
);
657 EXPORT_SYMBOL(snd_timer_start
);
660 * stop the timer instance.
662 * do not call this from the timer callback!
664 int snd_timer_stop(struct snd_timer_instance
*timeri
)
666 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
667 return snd_timer_stop_slave(timeri
, true);
669 return snd_timer_stop1(timeri
, true);
671 EXPORT_SYMBOL(snd_timer_stop
);
674 * start again.. the tick is kept.
676 int snd_timer_continue(struct snd_timer_instance
*timeri
)
678 /* timer can continue only after pause */
679 if (!(timeri
->flags
& SNDRV_TIMER_IFLG_PAUSED
))
682 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
683 return snd_timer_start_slave(timeri
, false);
685 return snd_timer_start1(timeri
, false, 0);
687 EXPORT_SYMBOL(snd_timer_continue
);
690 * pause.. remember the ticks left
692 int snd_timer_pause(struct snd_timer_instance
* timeri
)
694 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
695 return snd_timer_stop_slave(timeri
, false);
697 return snd_timer_stop1(timeri
, false);
699 EXPORT_SYMBOL(snd_timer_pause
);
702 * reschedule the timer
704 * start pending instances and check the scheduling ticks.
705 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
707 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
)
709 struct snd_timer_instance
*ti
;
710 unsigned long ticks
= ~0UL;
712 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
713 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
714 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
715 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
718 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
719 if (ticks
> ti
->cticks
)
724 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
727 if (ticks
> timer
->hw
.ticks
)
728 ticks
= timer
->hw
.ticks
;
729 if (ticks_left
!= ticks
)
730 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
731 timer
->sticks
= ticks
;
738 static void snd_timer_tasklet(unsigned long arg
)
740 struct snd_timer
*timer
= (struct snd_timer
*) arg
;
741 struct snd_timer_instance
*ti
;
743 unsigned long resolution
, ticks
;
746 if (timer
->card
&& timer
->card
->shutdown
)
749 spin_lock_irqsave(&timer
->lock
, flags
);
750 /* now process all callbacks */
751 while (!list_empty(&timer
->sack_list_head
)) {
752 p
= timer
->sack_list_head
.next
; /* get first item */
753 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
755 /* remove from ack_list and make empty */
760 resolution
= ti
->resolution
;
762 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
763 spin_unlock(&timer
->lock
);
765 ti
->callback(ti
, resolution
, ticks
);
766 spin_lock(&timer
->lock
);
767 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
769 spin_unlock_irqrestore(&timer
->lock
, flags
);
775 * ticks_left is usually equal to timer->sticks.
778 void snd_timer_interrupt(struct snd_timer
* timer
, unsigned long ticks_left
)
780 struct snd_timer_instance
*ti
, *ts
, *tmp
;
781 unsigned long resolution
, ticks
;
782 struct list_head
*p
, *ack_list_head
;
789 if (timer
->card
&& timer
->card
->shutdown
)
792 spin_lock_irqsave(&timer
->lock
, flags
);
794 /* remember the current resolution */
795 if (timer
->hw
.c_resolution
)
796 resolution
= timer
->hw
.c_resolution(timer
);
798 resolution
= timer
->hw
.resolution
;
800 /* loop for all active instances
801 * Here we cannot use list_for_each_entry because the active_list of a
802 * processed instance is relinked to done_list_head before the callback
805 list_for_each_entry_safe(ti
, tmp
, &timer
->active_list_head
,
807 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
809 ti
->pticks
+= ticks_left
;
810 ti
->resolution
= resolution
;
811 if (ti
->cticks
< ticks_left
)
814 ti
->cticks
-= ticks_left
;
815 if (ti
->cticks
) /* not expired */
817 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
818 ti
->cticks
= ti
->ticks
;
820 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
822 list_del_init(&ti
->active_list
);
824 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
825 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
))
826 ack_list_head
= &timer
->ack_list_head
;
828 ack_list_head
= &timer
->sack_list_head
;
829 if (list_empty(&ti
->ack_list
))
830 list_add_tail(&ti
->ack_list
, ack_list_head
);
831 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
) {
832 ts
->pticks
= ti
->pticks
;
833 ts
->resolution
= resolution
;
834 if (list_empty(&ts
->ack_list
))
835 list_add_tail(&ts
->ack_list
, ack_list_head
);
838 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
839 snd_timer_reschedule(timer
, timer
->sticks
);
840 if (timer
->running
) {
841 if (timer
->hw
.flags
& SNDRV_TIMER_HW_STOP
) {
842 timer
->hw
.stop(timer
);
843 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
845 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_AUTO
) ||
846 (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
)) {
848 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
849 timer
->hw
.start(timer
);
852 timer
->hw
.stop(timer
);
855 /* now process all fast callbacks */
856 while (!list_empty(&timer
->ack_list_head
)) {
857 p
= timer
->ack_list_head
.next
; /* get first item */
858 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
860 /* remove from ack_list and make empty */
866 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
867 spin_unlock(&timer
->lock
);
869 ti
->callback(ti
, resolution
, ticks
);
870 spin_lock(&timer
->lock
);
871 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
874 /* do we have any slow callbacks? */
875 use_tasklet
= !list_empty(&timer
->sack_list_head
);
876 spin_unlock_irqrestore(&timer
->lock
, flags
);
879 tasklet_schedule(&timer
->task_queue
);
881 EXPORT_SYMBOL(snd_timer_interrupt
);
887 int snd_timer_new(struct snd_card
*card
, char *id
, struct snd_timer_id
*tid
,
888 struct snd_timer
**rtimer
)
890 struct snd_timer
*timer
;
892 static struct snd_device_ops ops
= {
893 .dev_free
= snd_timer_dev_free
,
894 .dev_register
= snd_timer_dev_register
,
895 .dev_disconnect
= snd_timer_dev_disconnect
,
898 if (snd_BUG_ON(!tid
))
902 timer
= kzalloc(sizeof(*timer
), GFP_KERNEL
);
905 timer
->tmr_class
= tid
->dev_class
;
907 timer
->tmr_device
= tid
->device
;
908 timer
->tmr_subdevice
= tid
->subdevice
;
910 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
912 INIT_LIST_HEAD(&timer
->device_list
);
913 INIT_LIST_HEAD(&timer
->open_list_head
);
914 INIT_LIST_HEAD(&timer
->active_list_head
);
915 INIT_LIST_HEAD(&timer
->ack_list_head
);
916 INIT_LIST_HEAD(&timer
->sack_list_head
);
917 spin_lock_init(&timer
->lock
);
918 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
,
919 (unsigned long)timer
);
920 timer
->max_instances
= 1000; /* default limit per timer */
922 timer
->module
= card
->module
;
923 err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
);
925 snd_timer_free(timer
);
933 EXPORT_SYMBOL(snd_timer_new
);
935 static int snd_timer_free(struct snd_timer
*timer
)
940 mutex_lock(®ister_mutex
);
941 if (! list_empty(&timer
->open_list_head
)) {
942 struct list_head
*p
, *n
;
943 struct snd_timer_instance
*ti
;
944 pr_warn("ALSA: timer %p is busy?\n", timer
);
945 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
947 ti
= list_entry(p
, struct snd_timer_instance
, open_list
);
951 list_del(&timer
->device_list
);
952 mutex_unlock(®ister_mutex
);
954 if (timer
->private_free
)
955 timer
->private_free(timer
);
960 static int snd_timer_dev_free(struct snd_device
*device
)
962 struct snd_timer
*timer
= device
->device_data
;
963 return snd_timer_free(timer
);
966 static int snd_timer_dev_register(struct snd_device
*dev
)
968 struct snd_timer
*timer
= dev
->device_data
;
969 struct snd_timer
*timer1
;
971 if (snd_BUG_ON(!timer
|| !timer
->hw
.start
|| !timer
->hw
.stop
))
973 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
974 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
977 mutex_lock(®ister_mutex
);
978 list_for_each_entry(timer1
, &snd_timer_list
, device_list
) {
979 if (timer1
->tmr_class
> timer
->tmr_class
)
981 if (timer1
->tmr_class
< timer
->tmr_class
)
983 if (timer1
->card
&& timer
->card
) {
984 if (timer1
->card
->number
> timer
->card
->number
)
986 if (timer1
->card
->number
< timer
->card
->number
)
989 if (timer1
->tmr_device
> timer
->tmr_device
)
991 if (timer1
->tmr_device
< timer
->tmr_device
)
993 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
995 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
998 mutex_unlock(®ister_mutex
);
1001 list_add_tail(&timer
->device_list
, &timer1
->device_list
);
1002 mutex_unlock(®ister_mutex
);
1006 static int snd_timer_dev_disconnect(struct snd_device
*device
)
1008 struct snd_timer
*timer
= device
->device_data
;
1009 struct snd_timer_instance
*ti
;
1011 mutex_lock(®ister_mutex
);
1012 list_del_init(&timer
->device_list
);
1013 /* wake up pending sleepers */
1014 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
) {
1018 mutex_unlock(®ister_mutex
);
1022 void snd_timer_notify(struct snd_timer
*timer
, int event
, struct timespec
*tstamp
)
1024 unsigned long flags
;
1025 unsigned long resolution
= 0;
1026 struct snd_timer_instance
*ti
, *ts
;
1028 if (timer
->card
&& timer
->card
->shutdown
)
1030 if (! (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
))
1032 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_MSTART
||
1033 event
> SNDRV_TIMER_EVENT_MRESUME
))
1035 spin_lock_irqsave(&timer
->lock
, flags
);
1036 if (event
== SNDRV_TIMER_EVENT_MSTART
||
1037 event
== SNDRV_TIMER_EVENT_MCONTINUE
||
1038 event
== SNDRV_TIMER_EVENT_MRESUME
) {
1039 if (timer
->hw
.c_resolution
)
1040 resolution
= timer
->hw
.c_resolution(timer
);
1042 resolution
= timer
->hw
.resolution
;
1044 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
1046 ti
->ccallback(ti
, event
, tstamp
, resolution
);
1047 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
1049 ts
->ccallback(ts
, event
, tstamp
, resolution
);
1051 spin_unlock_irqrestore(&timer
->lock
, flags
);
1053 EXPORT_SYMBOL(snd_timer_notify
);
1056 * exported functions for global timers
1058 int snd_timer_global_new(char *id
, int device
, struct snd_timer
**rtimer
)
1060 struct snd_timer_id tid
;
1062 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
1063 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1065 tid
.device
= device
;
1067 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
1069 EXPORT_SYMBOL(snd_timer_global_new
);
1071 int snd_timer_global_free(struct snd_timer
*timer
)
1073 return snd_timer_free(timer
);
1075 EXPORT_SYMBOL(snd_timer_global_free
);
1077 int snd_timer_global_register(struct snd_timer
*timer
)
1079 struct snd_device dev
;
1081 memset(&dev
, 0, sizeof(dev
));
1082 dev
.device_data
= timer
;
1083 return snd_timer_dev_register(&dev
);
1085 EXPORT_SYMBOL(snd_timer_global_register
);
1091 struct snd_timer_system_private
{
1092 struct timer_list tlist
;
1093 struct snd_timer
*snd_timer
;
1094 unsigned long last_expires
;
1095 unsigned long last_jiffies
;
1096 unsigned long correction
;
1099 static void snd_timer_s_function(struct timer_list
*t
)
1101 struct snd_timer_system_private
*priv
= from_timer(priv
, t
,
1103 struct snd_timer
*timer
= priv
->snd_timer
;
1104 unsigned long jiff
= jiffies
;
1105 if (time_after(jiff
, priv
->last_expires
))
1106 priv
->correction
+= (long)jiff
- (long)priv
->last_expires
;
1107 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
1110 static int snd_timer_s_start(struct snd_timer
* timer
)
1112 struct snd_timer_system_private
*priv
;
1113 unsigned long njiff
;
1115 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1116 njiff
= (priv
->last_jiffies
= jiffies
);
1117 if (priv
->correction
> timer
->sticks
- 1) {
1118 priv
->correction
-= timer
->sticks
- 1;
1121 njiff
+= timer
->sticks
- priv
->correction
;
1122 priv
->correction
= 0;
1124 priv
->last_expires
= njiff
;
1125 mod_timer(&priv
->tlist
, njiff
);
1129 static int snd_timer_s_stop(struct snd_timer
* timer
)
1131 struct snd_timer_system_private
*priv
;
1134 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1135 del_timer(&priv
->tlist
);
1137 if (time_before(jiff
, priv
->last_expires
))
1138 timer
->sticks
= priv
->last_expires
- jiff
;
1141 priv
->correction
= 0;
1145 static int snd_timer_s_close(struct snd_timer
*timer
)
1147 struct snd_timer_system_private
*priv
;
1149 priv
= (struct snd_timer_system_private
*)timer
->private_data
;
1150 del_timer_sync(&priv
->tlist
);
1154 static struct snd_timer_hardware snd_timer_system
=
1156 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
1157 .resolution
= 1000000000L / HZ
,
1159 .close
= snd_timer_s_close
,
1160 .start
= snd_timer_s_start
,
1161 .stop
= snd_timer_s_stop
1164 static void snd_timer_free_system(struct snd_timer
*timer
)
1166 kfree(timer
->private_data
);
1169 static int snd_timer_register_system(void)
1171 struct snd_timer
*timer
;
1172 struct snd_timer_system_private
*priv
;
1175 err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
);
1178 strcpy(timer
->name
, "system timer");
1179 timer
->hw
= snd_timer_system
;
1180 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1182 snd_timer_free(timer
);
1185 priv
->snd_timer
= timer
;
1186 timer_setup(&priv
->tlist
, snd_timer_s_function
, 0);
1187 timer
->private_data
= priv
;
1188 timer
->private_free
= snd_timer_free_system
;
1189 return snd_timer_global_register(timer
);
1192 #ifdef CONFIG_SND_PROC_FS
1197 static void snd_timer_proc_read(struct snd_info_entry
*entry
,
1198 struct snd_info_buffer
*buffer
)
1200 struct snd_timer
*timer
;
1201 struct snd_timer_instance
*ti
;
1203 mutex_lock(®ister_mutex
);
1204 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
1205 if (timer
->card
&& timer
->card
->shutdown
)
1207 switch (timer
->tmr_class
) {
1208 case SNDRV_TIMER_CLASS_GLOBAL
:
1209 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1211 case SNDRV_TIMER_CLASS_CARD
:
1212 snd_iprintf(buffer
, "C%i-%i: ",
1213 timer
->card
->number
, timer
->tmr_device
);
1215 case SNDRV_TIMER_CLASS_PCM
:
1216 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
,
1217 timer
->tmr_device
, timer
->tmr_subdevice
);
1220 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
,
1221 timer
->card
? timer
->card
->number
: -1,
1222 timer
->tmr_device
, timer
->tmr_subdevice
);
1224 snd_iprintf(buffer
, "%s :", timer
->name
);
1225 if (timer
->hw
.resolution
)
1226 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)",
1227 timer
->hw
.resolution
/ 1000,
1228 timer
->hw
.resolution
% 1000,
1230 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1231 snd_iprintf(buffer
, " SLAVE");
1232 snd_iprintf(buffer
, "\n");
1233 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
)
1234 snd_iprintf(buffer
, " Client %s : %s\n",
1235 ti
->owner
? ti
->owner
: "unknown",
1236 ti
->flags
& (SNDRV_TIMER_IFLG_START
|
1237 SNDRV_TIMER_IFLG_RUNNING
)
1238 ? "running" : "stopped");
1240 mutex_unlock(®ister_mutex
);
1243 static struct snd_info_entry
*snd_timer_proc_entry
;
1245 static void __init
snd_timer_proc_init(void)
1247 struct snd_info_entry
*entry
;
1249 entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
);
1250 if (entry
!= NULL
) {
1251 entry
->c
.text
.read
= snd_timer_proc_read
;
1252 if (snd_info_register(entry
) < 0) {
1253 snd_info_free_entry(entry
);
1257 snd_timer_proc_entry
= entry
;
1260 static void __exit
snd_timer_proc_done(void)
1262 snd_info_free_entry(snd_timer_proc_entry
);
1264 #else /* !CONFIG_SND_PROC_FS */
1265 #define snd_timer_proc_init()
1266 #define snd_timer_proc_done()
1270 * USER SPACE interface
1273 static void snd_timer_user_interrupt(struct snd_timer_instance
*timeri
,
1274 unsigned long resolution
,
1275 unsigned long ticks
)
1277 struct snd_timer_user
*tu
= timeri
->callback_data
;
1278 struct snd_timer_read
*r
;
1281 spin_lock(&tu
->qlock
);
1282 if (tu
->qused
> 0) {
1283 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1284 r
= &tu
->queue
[prev
];
1285 if (r
->resolution
== resolution
) {
1290 if (tu
->qused
>= tu
->queue_size
) {
1293 r
= &tu
->queue
[tu
->qtail
++];
1294 tu
->qtail
%= tu
->queue_size
;
1295 r
->resolution
= resolution
;
1300 spin_unlock(&tu
->qlock
);
1301 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1302 wake_up(&tu
->qchange_sleep
);
1305 static void snd_timer_user_append_to_tqueue(struct snd_timer_user
*tu
,
1306 struct snd_timer_tread
*tread
)
1308 if (tu
->qused
>= tu
->queue_size
) {
1311 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1312 tu
->qtail
%= tu
->queue_size
;
1317 static void snd_timer_user_ccallback(struct snd_timer_instance
*timeri
,
1319 struct timespec
*tstamp
,
1320 unsigned long resolution
)
1322 struct snd_timer_user
*tu
= timeri
->callback_data
;
1323 struct snd_timer_tread r1
;
1324 unsigned long flags
;
1326 if (event
>= SNDRV_TIMER_EVENT_START
&&
1327 event
<= SNDRV_TIMER_EVENT_PAUSE
)
1328 tu
->tstamp
= *tstamp
;
1329 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1331 memset(&r1
, 0, sizeof(r1
));
1333 r1
.tstamp
= *tstamp
;
1334 r1
.val
= resolution
;
1335 spin_lock_irqsave(&tu
->qlock
, flags
);
1336 snd_timer_user_append_to_tqueue(tu
, &r1
);
1337 spin_unlock_irqrestore(&tu
->qlock
, flags
);
1338 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1339 wake_up(&tu
->qchange_sleep
);
1342 static void snd_timer_user_disconnect(struct snd_timer_instance
*timeri
)
1344 struct snd_timer_user
*tu
= timeri
->callback_data
;
1346 tu
->disconnected
= true;
1347 wake_up(&tu
->qchange_sleep
);
1350 static void snd_timer_user_tinterrupt(struct snd_timer_instance
*timeri
,
1351 unsigned long resolution
,
1352 unsigned long ticks
)
1354 struct snd_timer_user
*tu
= timeri
->callback_data
;
1355 struct snd_timer_tread
*r
, r1
;
1356 struct timespec tstamp
;
1357 int prev
, append
= 0;
1359 memset(&r1
, 0, sizeof(r1
));
1360 memset(&tstamp
, 0, sizeof(tstamp
));
1361 spin_lock(&tu
->qlock
);
1362 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
) |
1363 (1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1364 spin_unlock(&tu
->qlock
);
1367 if (tu
->last_resolution
!= resolution
|| ticks
> 0) {
1368 if (timer_tstamp_monotonic
)
1369 ktime_get_ts(&tstamp
);
1371 getnstimeofday(&tstamp
);
1373 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) &&
1374 tu
->last_resolution
!= resolution
) {
1375 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1377 r1
.val
= resolution
;
1378 snd_timer_user_append_to_tqueue(tu
, &r1
);
1379 tu
->last_resolution
= resolution
;
1382 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1386 if (tu
->qused
> 0) {
1387 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1388 r
= &tu
->tqueue
[prev
];
1389 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1396 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1399 snd_timer_user_append_to_tqueue(tu
, &r1
);
1402 spin_unlock(&tu
->qlock
);
1405 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1406 wake_up(&tu
->qchange_sleep
);
1409 static int realloc_user_queue(struct snd_timer_user
*tu
, int size
)
1411 struct snd_timer_read
*queue
= NULL
;
1412 struct snd_timer_tread
*tqueue
= NULL
;
1415 tqueue
= kcalloc(size
, sizeof(*tqueue
), GFP_KERNEL
);
1419 queue
= kcalloc(size
, sizeof(*queue
), GFP_KERNEL
);
1424 spin_lock_irq(&tu
->qlock
);
1427 tu
->queue_size
= size
;
1429 tu
->tqueue
= tqueue
;
1430 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1431 spin_unlock_irq(&tu
->qlock
);
1436 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1438 struct snd_timer_user
*tu
;
1441 err
= nonseekable_open(inode
, file
);
1445 tu
= kzalloc(sizeof(*tu
), GFP_KERNEL
);
1448 spin_lock_init(&tu
->qlock
);
1449 init_waitqueue_head(&tu
->qchange_sleep
);
1450 mutex_init(&tu
->ioctl_lock
);
1452 if (realloc_user_queue(tu
, 128) < 0) {
1456 file
->private_data
= tu
;
1460 static int snd_timer_user_release(struct inode
*inode
, struct file
*file
)
1462 struct snd_timer_user
*tu
;
1464 if (file
->private_data
) {
1465 tu
= file
->private_data
;
1466 file
->private_data
= NULL
;
1467 mutex_lock(&tu
->ioctl_lock
);
1469 snd_timer_close(tu
->timeri
);
1470 mutex_unlock(&tu
->ioctl_lock
);
1478 static void snd_timer_user_zero_id(struct snd_timer_id
*id
)
1480 id
->dev_class
= SNDRV_TIMER_CLASS_NONE
;
1481 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1487 static void snd_timer_user_copy_id(struct snd_timer_id
*id
, struct snd_timer
*timer
)
1489 id
->dev_class
= timer
->tmr_class
;
1490 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1491 id
->card
= timer
->card
? timer
->card
->number
: -1;
1492 id
->device
= timer
->tmr_device
;
1493 id
->subdevice
= timer
->tmr_subdevice
;
1496 static int snd_timer_user_next_device(struct snd_timer_id __user
*_tid
)
1498 struct snd_timer_id id
;
1499 struct snd_timer
*timer
;
1500 struct list_head
*p
;
1502 if (copy_from_user(&id
, _tid
, sizeof(id
)))
1504 mutex_lock(®ister_mutex
);
1505 if (id
.dev_class
< 0) { /* first item */
1506 if (list_empty(&snd_timer_list
))
1507 snd_timer_user_zero_id(&id
);
1509 timer
= list_entry(snd_timer_list
.next
,
1510 struct snd_timer
, device_list
);
1511 snd_timer_user_copy_id(&id
, timer
);
1514 switch (id
.dev_class
) {
1515 case SNDRV_TIMER_CLASS_GLOBAL
:
1516 id
.device
= id
.device
< 0 ? 0 : id
.device
+ 1;
1517 list_for_each(p
, &snd_timer_list
) {
1518 timer
= list_entry(p
, struct snd_timer
, device_list
);
1519 if (timer
->tmr_class
> SNDRV_TIMER_CLASS_GLOBAL
) {
1520 snd_timer_user_copy_id(&id
, timer
);
1523 if (timer
->tmr_device
>= id
.device
) {
1524 snd_timer_user_copy_id(&id
, timer
);
1528 if (p
== &snd_timer_list
)
1529 snd_timer_user_zero_id(&id
);
1531 case SNDRV_TIMER_CLASS_CARD
:
1532 case SNDRV_TIMER_CLASS_PCM
:
1536 if (id
.device
< 0) {
1539 if (id
.subdevice
< 0)
1541 else if (id
.subdevice
< INT_MAX
)
1545 list_for_each(p
, &snd_timer_list
) {
1546 timer
= list_entry(p
, struct snd_timer
, device_list
);
1547 if (timer
->tmr_class
> id
.dev_class
) {
1548 snd_timer_user_copy_id(&id
, timer
);
1551 if (timer
->tmr_class
< id
.dev_class
)
1553 if (timer
->card
->number
> id
.card
) {
1554 snd_timer_user_copy_id(&id
, timer
);
1557 if (timer
->card
->number
< id
.card
)
1559 if (timer
->tmr_device
> id
.device
) {
1560 snd_timer_user_copy_id(&id
, timer
);
1563 if (timer
->tmr_device
< id
.device
)
1565 if (timer
->tmr_subdevice
> id
.subdevice
) {
1566 snd_timer_user_copy_id(&id
, timer
);
1569 if (timer
->tmr_subdevice
< id
.subdevice
)
1571 snd_timer_user_copy_id(&id
, timer
);
1574 if (p
== &snd_timer_list
)
1575 snd_timer_user_zero_id(&id
);
1578 snd_timer_user_zero_id(&id
);
1581 mutex_unlock(®ister_mutex
);
1582 if (copy_to_user(_tid
, &id
, sizeof(*_tid
)))
1587 static int snd_timer_user_ginfo(struct file
*file
,
1588 struct snd_timer_ginfo __user
*_ginfo
)
1590 struct snd_timer_ginfo
*ginfo
;
1591 struct snd_timer_id tid
;
1592 struct snd_timer
*t
;
1593 struct list_head
*p
;
1596 ginfo
= memdup_user(_ginfo
, sizeof(*ginfo
));
1598 return PTR_ERR(ginfo
);
1601 memset(ginfo
, 0, sizeof(*ginfo
));
1603 mutex_lock(®ister_mutex
);
1604 t
= snd_timer_find(&tid
);
1606 ginfo
->card
= t
->card
? t
->card
->number
: -1;
1607 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1608 ginfo
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1609 strlcpy(ginfo
->id
, t
->id
, sizeof(ginfo
->id
));
1610 strlcpy(ginfo
->name
, t
->name
, sizeof(ginfo
->name
));
1611 ginfo
->resolution
= t
->hw
.resolution
;
1612 if (t
->hw
.resolution_min
> 0) {
1613 ginfo
->resolution_min
= t
->hw
.resolution_min
;
1614 ginfo
->resolution_max
= t
->hw
.resolution_max
;
1616 list_for_each(p
, &t
->open_list_head
) {
1622 mutex_unlock(®ister_mutex
);
1623 if (err
>= 0 && copy_to_user(_ginfo
, ginfo
, sizeof(*ginfo
)))
1629 static int timer_set_gparams(struct snd_timer_gparams
*gparams
)
1631 struct snd_timer
*t
;
1634 mutex_lock(®ister_mutex
);
1635 t
= snd_timer_find(&gparams
->tid
);
1640 if (!list_empty(&t
->open_list_head
)) {
1644 if (!t
->hw
.set_period
) {
1648 err
= t
->hw
.set_period(t
, gparams
->period_num
, gparams
->period_den
);
1650 mutex_unlock(®ister_mutex
);
1654 static int snd_timer_user_gparams(struct file
*file
,
1655 struct snd_timer_gparams __user
*_gparams
)
1657 struct snd_timer_gparams gparams
;
1659 if (copy_from_user(&gparams
, _gparams
, sizeof(gparams
)))
1661 return timer_set_gparams(&gparams
);
1664 static int snd_timer_user_gstatus(struct file
*file
,
1665 struct snd_timer_gstatus __user
*_gstatus
)
1667 struct snd_timer_gstatus gstatus
;
1668 struct snd_timer_id tid
;
1669 struct snd_timer
*t
;
1672 if (copy_from_user(&gstatus
, _gstatus
, sizeof(gstatus
)))
1675 memset(&gstatus
, 0, sizeof(gstatus
));
1677 mutex_lock(®ister_mutex
);
1678 t
= snd_timer_find(&tid
);
1680 if (t
->hw
.c_resolution
)
1681 gstatus
.resolution
= t
->hw
.c_resolution(t
);
1683 gstatus
.resolution
= t
->hw
.resolution
;
1684 if (t
->hw
.precise_resolution
) {
1685 t
->hw
.precise_resolution(t
, &gstatus
.resolution_num
,
1686 &gstatus
.resolution_den
);
1688 gstatus
.resolution_num
= gstatus
.resolution
;
1689 gstatus
.resolution_den
= 1000000000uL;
1694 mutex_unlock(®ister_mutex
);
1695 if (err
>= 0 && copy_to_user(_gstatus
, &gstatus
, sizeof(gstatus
)))
1700 static int snd_timer_user_tselect(struct file
*file
,
1701 struct snd_timer_select __user
*_tselect
)
1703 struct snd_timer_user
*tu
;
1704 struct snd_timer_select tselect
;
1708 tu
= file
->private_data
;
1710 snd_timer_close(tu
->timeri
);
1713 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
))) {
1717 sprintf(str
, "application %i", current
->pid
);
1718 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1719 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1720 err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
);
1724 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1725 tu
->timeri
->callback
= tu
->tread
1726 ? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1727 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1728 tu
->timeri
->callback_data
= (void *)tu
;
1729 tu
->timeri
->disconnect
= snd_timer_user_disconnect
;
1735 static int snd_timer_user_info(struct file
*file
,
1736 struct snd_timer_info __user
*_info
)
1738 struct snd_timer_user
*tu
;
1739 struct snd_timer_info
*info
;
1740 struct snd_timer
*t
;
1743 tu
= file
->private_data
;
1746 t
= tu
->timeri
->timer
;
1750 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1753 info
->card
= t
->card
? t
->card
->number
: -1;
1754 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1755 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1756 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1757 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1758 info
->resolution
= t
->hw
.resolution
;
1759 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1765 static int snd_timer_user_params(struct file
*file
,
1766 struct snd_timer_params __user
*_params
)
1768 struct snd_timer_user
*tu
;
1769 struct snd_timer_params params
;
1770 struct snd_timer
*t
;
1773 tu
= file
->private_data
;
1776 t
= tu
->timeri
->timer
;
1779 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
1781 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)) {
1784 if (params
.ticks
< 1) {
1789 /* Don't allow resolution less than 1ms */
1790 resolution
= snd_timer_resolution(tu
->timeri
);
1791 resolution
*= params
.ticks
;
1792 if (resolution
< 1000000) {
1797 if (params
.queue_size
> 0 &&
1798 (params
.queue_size
< 32 || params
.queue_size
> 1024)) {
1802 if (params
.filter
& ~((1<<SNDRV_TIMER_EVENT_RESOLUTION
)|
1803 (1<<SNDRV_TIMER_EVENT_TICK
)|
1804 (1<<SNDRV_TIMER_EVENT_START
)|
1805 (1<<SNDRV_TIMER_EVENT_STOP
)|
1806 (1<<SNDRV_TIMER_EVENT_CONTINUE
)|
1807 (1<<SNDRV_TIMER_EVENT_PAUSE
)|
1808 (1<<SNDRV_TIMER_EVENT_SUSPEND
)|
1809 (1<<SNDRV_TIMER_EVENT_RESUME
)|
1810 (1<<SNDRV_TIMER_EVENT_MSTART
)|
1811 (1<<SNDRV_TIMER_EVENT_MSTOP
)|
1812 (1<<SNDRV_TIMER_EVENT_MCONTINUE
)|
1813 (1<<SNDRV_TIMER_EVENT_MPAUSE
)|
1814 (1<<SNDRV_TIMER_EVENT_MSUSPEND
)|
1815 (1<<SNDRV_TIMER_EVENT_MRESUME
))) {
1819 snd_timer_stop(tu
->timeri
);
1820 spin_lock_irq(&t
->lock
);
1821 tu
->timeri
->flags
&= ~(SNDRV_TIMER_IFLG_AUTO
|
1822 SNDRV_TIMER_IFLG_EXCLUSIVE
|
1823 SNDRV_TIMER_IFLG_EARLY_EVENT
);
1824 if (params
.flags
& SNDRV_TIMER_PSFLG_AUTO
)
1825 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
1826 if (params
.flags
& SNDRV_TIMER_PSFLG_EXCLUSIVE
)
1827 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EXCLUSIVE
;
1828 if (params
.flags
& SNDRV_TIMER_PSFLG_EARLY_EVENT
)
1829 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EARLY_EVENT
;
1830 spin_unlock_irq(&t
->lock
);
1831 if (params
.queue_size
> 0 &&
1832 (unsigned int)tu
->queue_size
!= params
.queue_size
) {
1833 err
= realloc_user_queue(tu
, params
.queue_size
);
1837 spin_lock_irq(&tu
->qlock
);
1838 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1839 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1841 struct snd_timer_tread tread
;
1842 memset(&tread
, 0, sizeof(tread
));
1843 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1844 tread
.tstamp
.tv_sec
= 0;
1845 tread
.tstamp
.tv_nsec
= 0;
1847 snd_timer_user_append_to_tqueue(tu
, &tread
);
1849 struct snd_timer_read
*r
= &tu
->queue
[0];
1856 tu
->filter
= params
.filter
;
1857 tu
->ticks
= params
.ticks
;
1858 spin_unlock_irq(&tu
->qlock
);
1861 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1866 static int snd_timer_user_status(struct file
*file
,
1867 struct snd_timer_status __user
*_status
)
1869 struct snd_timer_user
*tu
;
1870 struct snd_timer_status status
;
1872 tu
= file
->private_data
;
1875 memset(&status
, 0, sizeof(status
));
1876 status
.tstamp
= tu
->tstamp
;
1877 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1878 status
.lost
= tu
->timeri
->lost
;
1879 status
.overrun
= tu
->overrun
;
1880 spin_lock_irq(&tu
->qlock
);
1881 status
.queue
= tu
->qused
;
1882 spin_unlock_irq(&tu
->qlock
);
1883 if (copy_to_user(_status
, &status
, sizeof(status
)))
1888 static int snd_timer_user_start(struct file
*file
)
1891 struct snd_timer_user
*tu
;
1893 tu
= file
->private_data
;
1896 snd_timer_stop(tu
->timeri
);
1897 tu
->timeri
->lost
= 0;
1898 tu
->last_resolution
= 0;
1899 return (err
= snd_timer_start(tu
->timeri
, tu
->ticks
)) < 0 ? err
: 0;
1902 static int snd_timer_user_stop(struct file
*file
)
1905 struct snd_timer_user
*tu
;
1907 tu
= file
->private_data
;
1910 return (err
= snd_timer_stop(tu
->timeri
)) < 0 ? err
: 0;
1913 static int snd_timer_user_continue(struct file
*file
)
1916 struct snd_timer_user
*tu
;
1918 tu
= file
->private_data
;
1921 /* start timer instead of continue if it's not used before */
1922 if (!(tu
->timeri
->flags
& SNDRV_TIMER_IFLG_PAUSED
))
1923 return snd_timer_user_start(file
);
1924 tu
->timeri
->lost
= 0;
1925 return (err
= snd_timer_continue(tu
->timeri
)) < 0 ? err
: 0;
1928 static int snd_timer_user_pause(struct file
*file
)
1931 struct snd_timer_user
*tu
;
1933 tu
= file
->private_data
;
1936 return (err
= snd_timer_pause(tu
->timeri
)) < 0 ? err
: 0;
1940 SNDRV_TIMER_IOCTL_START_OLD
= _IO('T', 0x20),
1941 SNDRV_TIMER_IOCTL_STOP_OLD
= _IO('T', 0x21),
1942 SNDRV_TIMER_IOCTL_CONTINUE_OLD
= _IO('T', 0x22),
1943 SNDRV_TIMER_IOCTL_PAUSE_OLD
= _IO('T', 0x23),
1946 static long __snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1949 struct snd_timer_user
*tu
;
1950 void __user
*argp
= (void __user
*)arg
;
1951 int __user
*p
= argp
;
1953 tu
= file
->private_data
;
1955 case SNDRV_TIMER_IOCTL_PVERSION
:
1956 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1957 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1958 return snd_timer_user_next_device(argp
);
1959 case SNDRV_TIMER_IOCTL_TREAD
:
1961 int xarg
, old_tread
;
1963 if (tu
->timeri
) /* too late */
1965 if (get_user(xarg
, p
))
1967 old_tread
= tu
->tread
;
1968 tu
->tread
= xarg
? 1 : 0;
1969 if (tu
->tread
!= old_tread
&&
1970 realloc_user_queue(tu
, tu
->queue_size
) < 0) {
1971 tu
->tread
= old_tread
;
1976 case SNDRV_TIMER_IOCTL_GINFO
:
1977 return snd_timer_user_ginfo(file
, argp
);
1978 case SNDRV_TIMER_IOCTL_GPARAMS
:
1979 return snd_timer_user_gparams(file
, argp
);
1980 case SNDRV_TIMER_IOCTL_GSTATUS
:
1981 return snd_timer_user_gstatus(file
, argp
);
1982 case SNDRV_TIMER_IOCTL_SELECT
:
1983 return snd_timer_user_tselect(file
, argp
);
1984 case SNDRV_TIMER_IOCTL_INFO
:
1985 return snd_timer_user_info(file
, argp
);
1986 case SNDRV_TIMER_IOCTL_PARAMS
:
1987 return snd_timer_user_params(file
, argp
);
1988 case SNDRV_TIMER_IOCTL_STATUS
:
1989 return snd_timer_user_status(file
, argp
);
1990 case SNDRV_TIMER_IOCTL_START
:
1991 case SNDRV_TIMER_IOCTL_START_OLD
:
1992 return snd_timer_user_start(file
);
1993 case SNDRV_TIMER_IOCTL_STOP
:
1994 case SNDRV_TIMER_IOCTL_STOP_OLD
:
1995 return snd_timer_user_stop(file
);
1996 case SNDRV_TIMER_IOCTL_CONTINUE
:
1997 case SNDRV_TIMER_IOCTL_CONTINUE_OLD
:
1998 return snd_timer_user_continue(file
);
1999 case SNDRV_TIMER_IOCTL_PAUSE
:
2000 case SNDRV_TIMER_IOCTL_PAUSE_OLD
:
2001 return snd_timer_user_pause(file
);
2006 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
2009 struct snd_timer_user
*tu
= file
->private_data
;
2012 mutex_lock(&tu
->ioctl_lock
);
2013 ret
= __snd_timer_user_ioctl(file
, cmd
, arg
);
2014 mutex_unlock(&tu
->ioctl_lock
);
2018 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
2020 struct snd_timer_user
*tu
;
2022 tu
= file
->private_data
;
2023 return fasync_helper(fd
, file
, on
, &tu
->fasync
);
2026 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
,
2027 size_t count
, loff_t
*offset
)
2029 struct snd_timer_user
*tu
;
2030 long result
= 0, unit
;
2034 tu
= file
->private_data
;
2035 unit
= tu
->tread
? sizeof(struct snd_timer_tread
) : sizeof(struct snd_timer_read
);
2036 mutex_lock(&tu
->ioctl_lock
);
2037 spin_lock_irq(&tu
->qlock
);
2038 while ((long)count
- result
>= unit
) {
2039 while (!tu
->qused
) {
2040 wait_queue_entry_t wait
;
2042 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
2047 set_current_state(TASK_INTERRUPTIBLE
);
2048 init_waitqueue_entry(&wait
, current
);
2049 add_wait_queue(&tu
->qchange_sleep
, &wait
);
2051 spin_unlock_irq(&tu
->qlock
);
2052 mutex_unlock(&tu
->ioctl_lock
);
2054 mutex_lock(&tu
->ioctl_lock
);
2055 spin_lock_irq(&tu
->qlock
);
2057 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
2059 if (tu
->disconnected
) {
2063 if (signal_pending(current
)) {
2069 qhead
= tu
->qhead
++;
2070 tu
->qhead
%= tu
->queue_size
;
2072 spin_unlock_irq(&tu
->qlock
);
2075 if (copy_to_user(buffer
, &tu
->tqueue
[qhead
],
2076 sizeof(struct snd_timer_tread
)))
2079 if (copy_to_user(buffer
, &tu
->queue
[qhead
],
2080 sizeof(struct snd_timer_read
)))
2084 spin_lock_irq(&tu
->qlock
);
2091 spin_unlock_irq(&tu
->qlock
);
2092 mutex_unlock(&tu
->ioctl_lock
);
2093 return result
> 0 ? result
: err
;
2096 static unsigned int snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
2099 struct snd_timer_user
*tu
;
2101 tu
= file
->private_data
;
2103 poll_wait(file
, &tu
->qchange_sleep
, wait
);
2106 spin_lock_irq(&tu
->qlock
);
2108 mask
|= POLLIN
| POLLRDNORM
;
2109 if (tu
->disconnected
)
2111 spin_unlock_irq(&tu
->qlock
);
2116 #ifdef CONFIG_COMPAT
2117 #include "timer_compat.c"
2119 #define snd_timer_user_ioctl_compat NULL
2122 static const struct file_operations snd_timer_f_ops
=
2124 .owner
= THIS_MODULE
,
2125 .read
= snd_timer_user_read
,
2126 .open
= snd_timer_user_open
,
2127 .release
= snd_timer_user_release
,
2128 .llseek
= no_llseek
,
2129 .poll
= snd_timer_user_poll
,
2130 .unlocked_ioctl
= snd_timer_user_ioctl
,
2131 .compat_ioctl
= snd_timer_user_ioctl_compat
,
2132 .fasync
= snd_timer_user_fasync
,
2135 /* unregister the system timer */
2136 static void snd_timer_free_all(void)
2138 struct snd_timer
*timer
, *n
;
2140 list_for_each_entry_safe(timer
, n
, &snd_timer_list
, device_list
)
2141 snd_timer_free(timer
);
2144 static struct device timer_dev
;
2150 static int __init
alsa_timer_init(void)
2154 snd_device_initialize(&timer_dev
, NULL
);
2155 dev_set_name(&timer_dev
, "timer");
2157 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2158 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1,
2162 err
= snd_timer_register_system();
2164 pr_err("ALSA: unable to register system timer (%i)\n", err
);
2168 err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0,
2169 &snd_timer_f_ops
, NULL
, &timer_dev
);
2171 pr_err("ALSA: unable to register timer device (%i)\n", err
);
2172 snd_timer_free_all();
2176 snd_timer_proc_init();
2180 put_device(&timer_dev
);
2184 static void __exit
alsa_timer_exit(void)
2186 snd_unregister_device(&timer_dev
);
2187 snd_timer_free_all();
2188 put_device(&timer_dev
);
2189 snd_timer_proc_done();
2190 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2191 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
2195 module_init(alsa_timer_init
)
2196 module_exit(alsa_timer_exit
)