2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/mutex.h>
27 #include <linux/device.h>
28 #include <linux/module.h>
29 #include <linux/string.h>
30 #include <sound/core.h>
31 #include <sound/timer.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/minors.h>
35 #include <sound/initval.h>
36 #include <linux/kmod.h>
38 #if IS_ENABLED(CONFIG_SND_HRTIMER)
39 #define DEFAULT_TIMER_LIMIT 4
40 #elif IS_ENABLED(CONFIG_SND_RTCTIMER)
41 #define DEFAULT_TIMER_LIMIT 2
43 #define DEFAULT_TIMER_LIMIT 1
46 static int timer_limit
= DEFAULT_TIMER_LIMIT
;
47 static int timer_tstamp_monotonic
= 1;
48 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
49 MODULE_DESCRIPTION("ALSA timer interface");
50 MODULE_LICENSE("GPL");
51 module_param(timer_limit
, int, 0444);
52 MODULE_PARM_DESC(timer_limit
, "Maximum global timers in system.");
53 module_param(timer_tstamp_monotonic
, int, 0444);
54 MODULE_PARM_DESC(timer_tstamp_monotonic
, "Use posix monotonic clock source for timestamps (default).");
56 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR
, SNDRV_MINOR_TIMER
);
57 MODULE_ALIAS("devname:snd/timer");
59 struct snd_timer_user
{
60 struct snd_timer_instance
*timeri
;
61 int tread
; /* enhanced read with timestamps and events */
63 unsigned long overrun
;
68 struct snd_timer_read
*queue
;
69 struct snd_timer_tread
*tqueue
;
71 unsigned long last_resolution
;
73 struct timespec tstamp
; /* trigger tstamp */
74 wait_queue_head_t qchange_sleep
;
75 struct fasync_struct
*fasync
;
76 struct mutex ioctl_lock
;
80 static LIST_HEAD(snd_timer_list
);
82 /* list of slave instances */
83 static LIST_HEAD(snd_timer_slave_list
);
85 /* lock for slave active lists */
86 static DEFINE_SPINLOCK(slave_active_lock
);
88 static DEFINE_MUTEX(register_mutex
);
90 static int snd_timer_free(struct snd_timer
*timer
);
91 static int snd_timer_dev_free(struct snd_device
*device
);
92 static int snd_timer_dev_register(struct snd_device
*device
);
93 static int snd_timer_dev_disconnect(struct snd_device
*device
);
95 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
);
98 * create a timer instance with the given owner string.
99 * when timer is not NULL, increments the module counter
101 static struct snd_timer_instance
*snd_timer_instance_new(char *owner
,
102 struct snd_timer
*timer
)
104 struct snd_timer_instance
*timeri
;
105 timeri
= kzalloc(sizeof(*timeri
), GFP_KERNEL
);
108 timeri
->owner
= kstrdup(owner
, GFP_KERNEL
);
109 if (! timeri
->owner
) {
113 INIT_LIST_HEAD(&timeri
->open_list
);
114 INIT_LIST_HEAD(&timeri
->active_list
);
115 INIT_LIST_HEAD(&timeri
->ack_list
);
116 INIT_LIST_HEAD(&timeri
->slave_list_head
);
117 INIT_LIST_HEAD(&timeri
->slave_active_head
);
119 timeri
->timer
= timer
;
120 if (timer
&& !try_module_get(timer
->module
)) {
121 kfree(timeri
->owner
);
130 * find a timer instance from the given timer id
132 static struct snd_timer
*snd_timer_find(struct snd_timer_id
*tid
)
134 struct snd_timer
*timer
= NULL
;
136 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
137 if (timer
->tmr_class
!= tid
->dev_class
)
139 if ((timer
->tmr_class
== SNDRV_TIMER_CLASS_CARD
||
140 timer
->tmr_class
== SNDRV_TIMER_CLASS_PCM
) &&
141 (timer
->card
== NULL
||
142 timer
->card
->number
!= tid
->card
))
144 if (timer
->tmr_device
!= tid
->device
)
146 if (timer
->tmr_subdevice
!= tid
->subdevice
)
153 #ifdef CONFIG_MODULES
155 static void snd_timer_request(struct snd_timer_id
*tid
)
157 switch (tid
->dev_class
) {
158 case SNDRV_TIMER_CLASS_GLOBAL
:
159 if (tid
->device
< timer_limit
)
160 request_module("snd-timer-%i", tid
->device
);
162 case SNDRV_TIMER_CLASS_CARD
:
163 case SNDRV_TIMER_CLASS_PCM
:
164 if (tid
->card
< snd_ecards_limit
)
165 request_module("snd-card-%i", tid
->card
);
175 * look for a master instance matching with the slave id of the given slave.
176 * when found, relink the open_link of the slave.
178 * call this with register_mutex down.
180 static void snd_timer_check_slave(struct snd_timer_instance
*slave
)
182 struct snd_timer
*timer
;
183 struct snd_timer_instance
*master
;
185 /* FIXME: it's really dumb to look up all entries.. */
186 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
187 list_for_each_entry(master
, &timer
->open_list_head
, open_list
) {
188 if (slave
->slave_class
== master
->slave_class
&&
189 slave
->slave_id
== master
->slave_id
) {
190 list_move_tail(&slave
->open_list
,
191 &master
->slave_list_head
);
192 spin_lock_irq(&slave_active_lock
);
193 slave
->master
= master
;
194 slave
->timer
= master
->timer
;
195 spin_unlock_irq(&slave_active_lock
);
203 * look for slave instances matching with the slave id of the given master.
204 * when found, relink the open_link of slaves.
206 * call this with register_mutex down.
208 static void snd_timer_check_master(struct snd_timer_instance
*master
)
210 struct snd_timer_instance
*slave
, *tmp
;
212 /* check all pending slaves */
213 list_for_each_entry_safe(slave
, tmp
, &snd_timer_slave_list
, open_list
) {
214 if (slave
->slave_class
== master
->slave_class
&&
215 slave
->slave_id
== master
->slave_id
) {
216 list_move_tail(&slave
->open_list
, &master
->slave_list_head
);
217 spin_lock_irq(&slave_active_lock
);
218 spin_lock(&master
->timer
->lock
);
219 slave
->master
= master
;
220 slave
->timer
= master
->timer
;
221 if (slave
->flags
& SNDRV_TIMER_IFLG_RUNNING
)
222 list_add_tail(&slave
->active_list
,
223 &master
->slave_active_head
);
224 spin_unlock(&master
->timer
->lock
);
225 spin_unlock_irq(&slave_active_lock
);
231 * open a timer instance
232 * when opening a master, the slave id must be here given.
234 int snd_timer_open(struct snd_timer_instance
**ti
,
235 char *owner
, struct snd_timer_id
*tid
,
236 unsigned int slave_id
)
238 struct snd_timer
*timer
;
239 struct snd_timer_instance
*timeri
= NULL
;
241 if (tid
->dev_class
== SNDRV_TIMER_CLASS_SLAVE
) {
242 /* open a slave instance */
243 if (tid
->dev_sclass
<= SNDRV_TIMER_SCLASS_NONE
||
244 tid
->dev_sclass
> SNDRV_TIMER_SCLASS_OSS_SEQUENCER
) {
245 pr_debug("ALSA: timer: invalid slave class %i\n",
249 mutex_lock(®ister_mutex
);
250 timeri
= snd_timer_instance_new(owner
, NULL
);
252 mutex_unlock(®ister_mutex
);
255 timeri
->slave_class
= tid
->dev_sclass
;
256 timeri
->slave_id
= tid
->device
;
257 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
258 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
259 snd_timer_check_slave(timeri
);
260 mutex_unlock(®ister_mutex
);
265 /* open a master instance */
266 mutex_lock(®ister_mutex
);
267 timer
= snd_timer_find(tid
);
268 #ifdef CONFIG_MODULES
270 mutex_unlock(®ister_mutex
);
271 snd_timer_request(tid
);
272 mutex_lock(®ister_mutex
);
273 timer
= snd_timer_find(tid
);
277 mutex_unlock(®ister_mutex
);
280 if (!list_empty(&timer
->open_list_head
)) {
281 timeri
= list_entry(timer
->open_list_head
.next
,
282 struct snd_timer_instance
, open_list
);
283 if (timeri
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
284 mutex_unlock(®ister_mutex
);
288 timeri
= snd_timer_instance_new(owner
, timer
);
290 mutex_unlock(®ister_mutex
);
293 timeri
->slave_class
= tid
->dev_sclass
;
294 timeri
->slave_id
= slave_id
;
295 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
)
296 timer
->hw
.open(timer
);
297 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
298 snd_timer_check_master(timeri
);
299 mutex_unlock(®ister_mutex
);
304 static int _snd_timer_stop(struct snd_timer_instance
*timeri
,
305 int keep_flag
, int event
);
308 * close a timer instance
310 int snd_timer_close(struct snd_timer_instance
*timeri
)
312 struct snd_timer
*timer
= NULL
;
313 struct snd_timer_instance
*slave
, *tmp
;
315 if (snd_BUG_ON(!timeri
))
318 /* force to stop the timer */
319 snd_timer_stop(timeri
);
321 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
322 /* wait, until the active callback is finished */
323 spin_lock_irq(&slave_active_lock
);
324 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
325 spin_unlock_irq(&slave_active_lock
);
327 spin_lock_irq(&slave_active_lock
);
329 spin_unlock_irq(&slave_active_lock
);
330 mutex_lock(®ister_mutex
);
331 list_del(&timeri
->open_list
);
332 mutex_unlock(®ister_mutex
);
334 timer
= timeri
->timer
;
335 if (snd_BUG_ON(!timer
))
337 /* wait, until the active callback is finished */
338 spin_lock_irq(&timer
->lock
);
339 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
340 spin_unlock_irq(&timer
->lock
);
342 spin_lock_irq(&timer
->lock
);
344 spin_unlock_irq(&timer
->lock
);
345 mutex_lock(®ister_mutex
);
346 list_del(&timeri
->open_list
);
347 if (timer
&& list_empty(&timer
->open_list_head
) &&
349 timer
->hw
.close(timer
);
350 /* remove slave links */
351 spin_lock_irq(&slave_active_lock
);
352 spin_lock(&timer
->lock
);
353 list_for_each_entry_safe(slave
, tmp
, &timeri
->slave_list_head
,
355 list_move_tail(&slave
->open_list
, &snd_timer_slave_list
);
356 slave
->master
= NULL
;
358 list_del_init(&slave
->ack_list
);
359 list_del_init(&slave
->active_list
);
361 spin_unlock(&timer
->lock
);
362 spin_unlock_irq(&slave_active_lock
);
363 mutex_unlock(®ister_mutex
);
366 if (timeri
->private_free
)
367 timeri
->private_free(timeri
);
368 kfree(timeri
->owner
);
371 module_put(timer
->module
);
375 unsigned long snd_timer_resolution(struct snd_timer_instance
*timeri
)
377 struct snd_timer
* timer
;
381 if ((timer
= timeri
->timer
) != NULL
) {
382 if (timer
->hw
.c_resolution
)
383 return timer
->hw
.c_resolution(timer
);
384 return timer
->hw
.resolution
;
389 static void snd_timer_notify1(struct snd_timer_instance
*ti
, int event
)
391 struct snd_timer
*timer
;
393 unsigned long resolution
= 0;
394 struct snd_timer_instance
*ts
;
395 struct timespec tstamp
;
397 if (timer_tstamp_monotonic
)
398 ktime_get_ts(&tstamp
);
400 getnstimeofday(&tstamp
);
401 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_START
||
402 event
> SNDRV_TIMER_EVENT_PAUSE
))
404 if (event
== SNDRV_TIMER_EVENT_START
||
405 event
== SNDRV_TIMER_EVENT_CONTINUE
)
406 resolution
= snd_timer_resolution(ti
);
408 ti
->ccallback(ti
, event
, &tstamp
, resolution
);
409 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
414 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
416 spin_lock_irqsave(&timer
->lock
, flags
);
417 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
419 ts
->ccallback(ti
, event
+ 100, &tstamp
, resolution
);
420 spin_unlock_irqrestore(&timer
->lock
, flags
);
423 static int snd_timer_start1(struct snd_timer
*timer
, struct snd_timer_instance
*timeri
,
424 unsigned long sticks
)
426 list_move_tail(&timeri
->active_list
, &timer
->active_list_head
);
427 if (timer
->running
) {
428 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
430 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
431 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
432 return 1; /* delayed start */
434 timer
->sticks
= sticks
;
435 timer
->hw
.start(timer
);
438 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
443 static int snd_timer_start_slave(struct snd_timer_instance
*timeri
)
447 spin_lock_irqsave(&slave_active_lock
, flags
);
448 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
449 if (timeri
->master
&& timeri
->timer
) {
450 spin_lock(&timeri
->timer
->lock
);
451 list_add_tail(&timeri
->active_list
,
452 &timeri
->master
->slave_active_head
);
453 spin_unlock(&timeri
->timer
->lock
);
455 spin_unlock_irqrestore(&slave_active_lock
, flags
);
456 return 1; /* delayed start */
460 * start the timer instance
462 int snd_timer_start(struct snd_timer_instance
*timeri
, unsigned int ticks
)
464 struct snd_timer
*timer
;
465 int result
= -EINVAL
;
468 if (timeri
== NULL
|| ticks
< 1)
470 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
471 result
= snd_timer_start_slave(timeri
);
472 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
475 timer
= timeri
->timer
;
478 spin_lock_irqsave(&timer
->lock
, flags
);
479 timeri
->ticks
= timeri
->cticks
= ticks
;
481 result
= snd_timer_start1(timer
, timeri
, ticks
);
482 spin_unlock_irqrestore(&timer
->lock
, flags
);
483 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
487 static int _snd_timer_stop(struct snd_timer_instance
* timeri
,
488 int keep_flag
, int event
)
490 struct snd_timer
*timer
;
493 if (snd_BUG_ON(!timeri
))
496 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
498 spin_lock_irqsave(&slave_active_lock
, flags
);
499 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
500 list_del_init(&timeri
->ack_list
);
501 list_del_init(&timeri
->active_list
);
502 spin_unlock_irqrestore(&slave_active_lock
, flags
);
506 timer
= timeri
->timer
;
509 spin_lock_irqsave(&timer
->lock
, flags
);
510 list_del_init(&timeri
->ack_list
);
511 list_del_init(&timeri
->active_list
);
512 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
513 !(--timer
->running
)) {
514 timer
->hw
.stop(timer
);
515 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
516 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
517 snd_timer_reschedule(timer
, 0);
518 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
519 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
520 timer
->hw
.start(timer
);
526 ~(SNDRV_TIMER_IFLG_RUNNING
| SNDRV_TIMER_IFLG_START
);
527 spin_unlock_irqrestore(&timer
->lock
, flags
);
529 if (event
!= SNDRV_TIMER_EVENT_RESOLUTION
)
530 snd_timer_notify1(timeri
, event
);
535 * stop the timer instance.
537 * do not call this from the timer callback!
539 int snd_timer_stop(struct snd_timer_instance
*timeri
)
541 struct snd_timer
*timer
;
545 err
= _snd_timer_stop(timeri
, 0, SNDRV_TIMER_EVENT_STOP
);
548 timer
= timeri
->timer
;
551 spin_lock_irqsave(&timer
->lock
, flags
);
552 timeri
->cticks
= timeri
->ticks
;
554 spin_unlock_irqrestore(&timer
->lock
, flags
);
559 * start again.. the tick is kept.
561 int snd_timer_continue(struct snd_timer_instance
*timeri
)
563 struct snd_timer
*timer
;
564 int result
= -EINVAL
;
569 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
570 return snd_timer_start_slave(timeri
);
571 timer
= timeri
->timer
;
574 spin_lock_irqsave(&timer
->lock
, flags
);
578 result
= snd_timer_start1(timer
, timeri
, timer
->sticks
);
579 spin_unlock_irqrestore(&timer
->lock
, flags
);
580 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_CONTINUE
);
585 * pause.. remember the ticks left
587 int snd_timer_pause(struct snd_timer_instance
* timeri
)
589 return _snd_timer_stop(timeri
, 0, SNDRV_TIMER_EVENT_PAUSE
);
593 * reschedule the timer
595 * start pending instances and check the scheduling ticks.
596 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
598 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
)
600 struct snd_timer_instance
*ti
;
601 unsigned long ticks
= ~0UL;
603 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
604 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
605 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
606 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
609 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
610 if (ticks
> ti
->cticks
)
615 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
618 if (ticks
> timer
->hw
.ticks
)
619 ticks
= timer
->hw
.ticks
;
620 if (ticks_left
!= ticks
)
621 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
622 timer
->sticks
= ticks
;
629 static void snd_timer_tasklet(unsigned long arg
)
631 struct snd_timer
*timer
= (struct snd_timer
*) arg
;
632 struct snd_timer_instance
*ti
;
634 unsigned long resolution
, ticks
;
637 spin_lock_irqsave(&timer
->lock
, flags
);
638 /* now process all callbacks */
639 while (!list_empty(&timer
->sack_list_head
)) {
640 p
= timer
->sack_list_head
.next
; /* get first item */
641 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
643 /* remove from ack_list and make empty */
648 resolution
= ti
->resolution
;
650 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
651 spin_unlock(&timer
->lock
);
653 ti
->callback(ti
, resolution
, ticks
);
654 spin_lock(&timer
->lock
);
655 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
657 spin_unlock_irqrestore(&timer
->lock
, flags
);
663 * ticks_left is usually equal to timer->sticks.
666 void snd_timer_interrupt(struct snd_timer
* timer
, unsigned long ticks_left
)
668 struct snd_timer_instance
*ti
, *ts
, *tmp
;
669 unsigned long resolution
, ticks
;
670 struct list_head
*p
, *ack_list_head
;
677 spin_lock_irqsave(&timer
->lock
, flags
);
679 /* remember the current resolution */
680 if (timer
->hw
.c_resolution
)
681 resolution
= timer
->hw
.c_resolution(timer
);
683 resolution
= timer
->hw
.resolution
;
685 /* loop for all active instances
686 * Here we cannot use list_for_each_entry because the active_list of a
687 * processed instance is relinked to done_list_head before the callback
690 list_for_each_entry_safe(ti
, tmp
, &timer
->active_list_head
,
692 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
694 ti
->pticks
+= ticks_left
;
695 ti
->resolution
= resolution
;
696 if (ti
->cticks
< ticks_left
)
699 ti
->cticks
-= ticks_left
;
700 if (ti
->cticks
) /* not expired */
702 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
703 ti
->cticks
= ti
->ticks
;
705 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
706 if (--timer
->running
)
707 list_del_init(&ti
->active_list
);
709 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
710 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
))
711 ack_list_head
= &timer
->ack_list_head
;
713 ack_list_head
= &timer
->sack_list_head
;
714 if (list_empty(&ti
->ack_list
))
715 list_add_tail(&ti
->ack_list
, ack_list_head
);
716 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
) {
717 ts
->pticks
= ti
->pticks
;
718 ts
->resolution
= resolution
;
719 if (list_empty(&ts
->ack_list
))
720 list_add_tail(&ts
->ack_list
, ack_list_head
);
723 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
724 snd_timer_reschedule(timer
, timer
->sticks
);
725 if (timer
->running
) {
726 if (timer
->hw
.flags
& SNDRV_TIMER_HW_STOP
) {
727 timer
->hw
.stop(timer
);
728 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
730 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_AUTO
) ||
731 (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
)) {
733 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
734 timer
->hw
.start(timer
);
737 timer
->hw
.stop(timer
);
740 /* now process all fast callbacks */
741 while (!list_empty(&timer
->ack_list_head
)) {
742 p
= timer
->ack_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 */
751 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
752 spin_unlock(&timer
->lock
);
754 ti
->callback(ti
, resolution
, ticks
);
755 spin_lock(&timer
->lock
);
756 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
759 /* do we have any slow callbacks? */
760 use_tasklet
= !list_empty(&timer
->sack_list_head
);
761 spin_unlock_irqrestore(&timer
->lock
, flags
);
764 tasklet_schedule(&timer
->task_queue
);
771 int snd_timer_new(struct snd_card
*card
, char *id
, struct snd_timer_id
*tid
,
772 struct snd_timer
**rtimer
)
774 struct snd_timer
*timer
;
776 static struct snd_device_ops ops
= {
777 .dev_free
= snd_timer_dev_free
,
778 .dev_register
= snd_timer_dev_register
,
779 .dev_disconnect
= snd_timer_dev_disconnect
,
782 if (snd_BUG_ON(!tid
))
786 timer
= kzalloc(sizeof(*timer
), GFP_KERNEL
);
789 timer
->tmr_class
= tid
->dev_class
;
791 timer
->tmr_device
= tid
->device
;
792 timer
->tmr_subdevice
= tid
->subdevice
;
794 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
795 INIT_LIST_HEAD(&timer
->device_list
);
796 INIT_LIST_HEAD(&timer
->open_list_head
);
797 INIT_LIST_HEAD(&timer
->active_list_head
);
798 INIT_LIST_HEAD(&timer
->ack_list_head
);
799 INIT_LIST_HEAD(&timer
->sack_list_head
);
800 spin_lock_init(&timer
->lock
);
801 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
,
802 (unsigned long)timer
);
804 timer
->module
= card
->module
;
805 err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
);
807 snd_timer_free(timer
);
816 static int snd_timer_free(struct snd_timer
*timer
)
821 mutex_lock(®ister_mutex
);
822 if (! list_empty(&timer
->open_list_head
)) {
823 struct list_head
*p
, *n
;
824 struct snd_timer_instance
*ti
;
825 pr_warn("ALSA: timer %p is busy?\n", timer
);
826 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
828 ti
= list_entry(p
, struct snd_timer_instance
, open_list
);
832 list_del(&timer
->device_list
);
833 mutex_unlock(®ister_mutex
);
835 if (timer
->private_free
)
836 timer
->private_free(timer
);
841 static int snd_timer_dev_free(struct snd_device
*device
)
843 struct snd_timer
*timer
= device
->device_data
;
844 return snd_timer_free(timer
);
847 static int snd_timer_dev_register(struct snd_device
*dev
)
849 struct snd_timer
*timer
= dev
->device_data
;
850 struct snd_timer
*timer1
;
852 if (snd_BUG_ON(!timer
|| !timer
->hw
.start
|| !timer
->hw
.stop
))
854 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
855 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
858 mutex_lock(®ister_mutex
);
859 list_for_each_entry(timer1
, &snd_timer_list
, device_list
) {
860 if (timer1
->tmr_class
> timer
->tmr_class
)
862 if (timer1
->tmr_class
< timer
->tmr_class
)
864 if (timer1
->card
&& timer
->card
) {
865 if (timer1
->card
->number
> timer
->card
->number
)
867 if (timer1
->card
->number
< timer
->card
->number
)
870 if (timer1
->tmr_device
> timer
->tmr_device
)
872 if (timer1
->tmr_device
< timer
->tmr_device
)
874 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
876 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
879 mutex_unlock(®ister_mutex
);
882 list_add_tail(&timer
->device_list
, &timer1
->device_list
);
883 mutex_unlock(®ister_mutex
);
887 static int snd_timer_dev_disconnect(struct snd_device
*device
)
889 struct snd_timer
*timer
= device
->device_data
;
890 mutex_lock(®ister_mutex
);
891 list_del_init(&timer
->device_list
);
892 mutex_unlock(®ister_mutex
);
896 void snd_timer_notify(struct snd_timer
*timer
, int event
, struct timespec
*tstamp
)
899 unsigned long resolution
= 0;
900 struct snd_timer_instance
*ti
, *ts
;
902 if (! (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
))
904 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_MSTART
||
905 event
> SNDRV_TIMER_EVENT_MRESUME
))
907 spin_lock_irqsave(&timer
->lock
, flags
);
908 if (event
== SNDRV_TIMER_EVENT_MSTART
||
909 event
== SNDRV_TIMER_EVENT_MCONTINUE
||
910 event
== SNDRV_TIMER_EVENT_MRESUME
) {
911 if (timer
->hw
.c_resolution
)
912 resolution
= timer
->hw
.c_resolution(timer
);
914 resolution
= timer
->hw
.resolution
;
916 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
918 ti
->ccallback(ti
, event
, tstamp
, resolution
);
919 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
921 ts
->ccallback(ts
, event
, tstamp
, resolution
);
923 spin_unlock_irqrestore(&timer
->lock
, flags
);
927 * exported functions for global timers
929 int snd_timer_global_new(char *id
, int device
, struct snd_timer
**rtimer
)
931 struct snd_timer_id tid
;
933 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
934 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
938 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
941 int snd_timer_global_free(struct snd_timer
*timer
)
943 return snd_timer_free(timer
);
946 int snd_timer_global_register(struct snd_timer
*timer
)
948 struct snd_device dev
;
950 memset(&dev
, 0, sizeof(dev
));
951 dev
.device_data
= timer
;
952 return snd_timer_dev_register(&dev
);
959 struct snd_timer_system_private
{
960 struct timer_list tlist
;
961 unsigned long last_expires
;
962 unsigned long last_jiffies
;
963 unsigned long correction
;
966 static void snd_timer_s_function(unsigned long data
)
968 struct snd_timer
*timer
= (struct snd_timer
*)data
;
969 struct snd_timer_system_private
*priv
= timer
->private_data
;
970 unsigned long jiff
= jiffies
;
971 if (time_after(jiff
, priv
->last_expires
))
972 priv
->correction
+= (long)jiff
- (long)priv
->last_expires
;
973 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
976 static int snd_timer_s_start(struct snd_timer
* timer
)
978 struct snd_timer_system_private
*priv
;
981 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
982 njiff
= (priv
->last_jiffies
= jiffies
);
983 if (priv
->correction
> timer
->sticks
- 1) {
984 priv
->correction
-= timer
->sticks
- 1;
987 njiff
+= timer
->sticks
- priv
->correction
;
988 priv
->correction
= 0;
990 priv
->last_expires
= priv
->tlist
.expires
= njiff
;
991 add_timer(&priv
->tlist
);
995 static int snd_timer_s_stop(struct snd_timer
* timer
)
997 struct snd_timer_system_private
*priv
;
1000 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1001 del_timer(&priv
->tlist
);
1003 if (time_before(jiff
, priv
->last_expires
))
1004 timer
->sticks
= priv
->last_expires
- jiff
;
1007 priv
->correction
= 0;
1011 static struct snd_timer_hardware snd_timer_system
=
1013 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
1014 .resolution
= 1000000000L / HZ
,
1016 .start
= snd_timer_s_start
,
1017 .stop
= snd_timer_s_stop
1020 static void snd_timer_free_system(struct snd_timer
*timer
)
1022 kfree(timer
->private_data
);
1025 static int snd_timer_register_system(void)
1027 struct snd_timer
*timer
;
1028 struct snd_timer_system_private
*priv
;
1031 err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
);
1034 strcpy(timer
->name
, "system timer");
1035 timer
->hw
= snd_timer_system
;
1036 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1038 snd_timer_free(timer
);
1041 setup_timer(&priv
->tlist
, snd_timer_s_function
, (unsigned long) timer
);
1042 timer
->private_data
= priv
;
1043 timer
->private_free
= snd_timer_free_system
;
1044 return snd_timer_global_register(timer
);
1047 #ifdef CONFIG_SND_PROC_FS
1052 static void snd_timer_proc_read(struct snd_info_entry
*entry
,
1053 struct snd_info_buffer
*buffer
)
1055 struct snd_timer
*timer
;
1056 struct snd_timer_instance
*ti
;
1058 mutex_lock(®ister_mutex
);
1059 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
1060 switch (timer
->tmr_class
) {
1061 case SNDRV_TIMER_CLASS_GLOBAL
:
1062 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1064 case SNDRV_TIMER_CLASS_CARD
:
1065 snd_iprintf(buffer
, "C%i-%i: ",
1066 timer
->card
->number
, timer
->tmr_device
);
1068 case SNDRV_TIMER_CLASS_PCM
:
1069 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
,
1070 timer
->tmr_device
, timer
->tmr_subdevice
);
1073 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
,
1074 timer
->card
? timer
->card
->number
: -1,
1075 timer
->tmr_device
, timer
->tmr_subdevice
);
1077 snd_iprintf(buffer
, "%s :", timer
->name
);
1078 if (timer
->hw
.resolution
)
1079 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)",
1080 timer
->hw
.resolution
/ 1000,
1081 timer
->hw
.resolution
% 1000,
1083 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1084 snd_iprintf(buffer
, " SLAVE");
1085 snd_iprintf(buffer
, "\n");
1086 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
)
1087 snd_iprintf(buffer
, " Client %s : %s\n",
1088 ti
->owner
? ti
->owner
: "unknown",
1089 ti
->flags
& (SNDRV_TIMER_IFLG_START
|
1090 SNDRV_TIMER_IFLG_RUNNING
)
1091 ? "running" : "stopped");
1093 mutex_unlock(®ister_mutex
);
1096 static struct snd_info_entry
*snd_timer_proc_entry
;
1098 static void __init
snd_timer_proc_init(void)
1100 struct snd_info_entry
*entry
;
1102 entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
);
1103 if (entry
!= NULL
) {
1104 entry
->c
.text
.read
= snd_timer_proc_read
;
1105 if (snd_info_register(entry
) < 0) {
1106 snd_info_free_entry(entry
);
1110 snd_timer_proc_entry
= entry
;
1113 static void __exit
snd_timer_proc_done(void)
1115 snd_info_free_entry(snd_timer_proc_entry
);
1117 #else /* !CONFIG_SND_PROC_FS */
1118 #define snd_timer_proc_init()
1119 #define snd_timer_proc_done()
1123 * USER SPACE interface
1126 static void snd_timer_user_interrupt(struct snd_timer_instance
*timeri
,
1127 unsigned long resolution
,
1128 unsigned long ticks
)
1130 struct snd_timer_user
*tu
= timeri
->callback_data
;
1131 struct snd_timer_read
*r
;
1134 spin_lock(&tu
->qlock
);
1135 if (tu
->qused
> 0) {
1136 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1137 r
= &tu
->queue
[prev
];
1138 if (r
->resolution
== resolution
) {
1143 if (tu
->qused
>= tu
->queue_size
) {
1146 r
= &tu
->queue
[tu
->qtail
++];
1147 tu
->qtail
%= tu
->queue_size
;
1148 r
->resolution
= resolution
;
1153 spin_unlock(&tu
->qlock
);
1154 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1155 wake_up(&tu
->qchange_sleep
);
1158 static void snd_timer_user_append_to_tqueue(struct snd_timer_user
*tu
,
1159 struct snd_timer_tread
*tread
)
1161 if (tu
->qused
>= tu
->queue_size
) {
1164 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1165 tu
->qtail
%= tu
->queue_size
;
1170 static void snd_timer_user_ccallback(struct snd_timer_instance
*timeri
,
1172 struct timespec
*tstamp
,
1173 unsigned long resolution
)
1175 struct snd_timer_user
*tu
= timeri
->callback_data
;
1176 struct snd_timer_tread r1
;
1177 unsigned long flags
;
1179 if (event
>= SNDRV_TIMER_EVENT_START
&&
1180 event
<= SNDRV_TIMER_EVENT_PAUSE
)
1181 tu
->tstamp
= *tstamp
;
1182 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1185 r1
.tstamp
= *tstamp
;
1186 r1
.val
= resolution
;
1187 spin_lock_irqsave(&tu
->qlock
, flags
);
1188 snd_timer_user_append_to_tqueue(tu
, &r1
);
1189 spin_unlock_irqrestore(&tu
->qlock
, flags
);
1190 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1191 wake_up(&tu
->qchange_sleep
);
1194 static void snd_timer_user_tinterrupt(struct snd_timer_instance
*timeri
,
1195 unsigned long resolution
,
1196 unsigned long ticks
)
1198 struct snd_timer_user
*tu
= timeri
->callback_data
;
1199 struct snd_timer_tread
*r
, r1
;
1200 struct timespec tstamp
;
1201 int prev
, append
= 0;
1203 memset(&tstamp
, 0, sizeof(tstamp
));
1204 spin_lock(&tu
->qlock
);
1205 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
) |
1206 (1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1207 spin_unlock(&tu
->qlock
);
1210 if (tu
->last_resolution
!= resolution
|| ticks
> 0) {
1211 if (timer_tstamp_monotonic
)
1212 ktime_get_ts(&tstamp
);
1214 getnstimeofday(&tstamp
);
1216 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) &&
1217 tu
->last_resolution
!= resolution
) {
1218 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1220 r1
.val
= resolution
;
1221 snd_timer_user_append_to_tqueue(tu
, &r1
);
1222 tu
->last_resolution
= resolution
;
1225 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1229 if (tu
->qused
> 0) {
1230 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1231 r
= &tu
->tqueue
[prev
];
1232 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1239 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1242 snd_timer_user_append_to_tqueue(tu
, &r1
);
1245 spin_unlock(&tu
->qlock
);
1248 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1249 wake_up(&tu
->qchange_sleep
);
1252 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1254 struct snd_timer_user
*tu
;
1257 err
= nonseekable_open(inode
, file
);
1261 tu
= kzalloc(sizeof(*tu
), GFP_KERNEL
);
1264 spin_lock_init(&tu
->qlock
);
1265 init_waitqueue_head(&tu
->qchange_sleep
);
1266 mutex_init(&tu
->ioctl_lock
);
1268 tu
->queue_size
= 128;
1269 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1271 if (tu
->queue
== NULL
) {
1275 file
->private_data
= tu
;
1279 static int snd_timer_user_release(struct inode
*inode
, struct file
*file
)
1281 struct snd_timer_user
*tu
;
1283 if (file
->private_data
) {
1284 tu
= file
->private_data
;
1285 file
->private_data
= NULL
;
1286 mutex_lock(&tu
->ioctl_lock
);
1288 snd_timer_close(tu
->timeri
);
1289 mutex_unlock(&tu
->ioctl_lock
);
1297 static void snd_timer_user_zero_id(struct snd_timer_id
*id
)
1299 id
->dev_class
= SNDRV_TIMER_CLASS_NONE
;
1300 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1306 static void snd_timer_user_copy_id(struct snd_timer_id
*id
, struct snd_timer
*timer
)
1308 id
->dev_class
= timer
->tmr_class
;
1309 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1310 id
->card
= timer
->card
? timer
->card
->number
: -1;
1311 id
->device
= timer
->tmr_device
;
1312 id
->subdevice
= timer
->tmr_subdevice
;
1315 static int snd_timer_user_next_device(struct snd_timer_id __user
*_tid
)
1317 struct snd_timer_id id
;
1318 struct snd_timer
*timer
;
1319 struct list_head
*p
;
1321 if (copy_from_user(&id
, _tid
, sizeof(id
)))
1323 mutex_lock(®ister_mutex
);
1324 if (id
.dev_class
< 0) { /* first item */
1325 if (list_empty(&snd_timer_list
))
1326 snd_timer_user_zero_id(&id
);
1328 timer
= list_entry(snd_timer_list
.next
,
1329 struct snd_timer
, device_list
);
1330 snd_timer_user_copy_id(&id
, timer
);
1333 switch (id
.dev_class
) {
1334 case SNDRV_TIMER_CLASS_GLOBAL
:
1335 id
.device
= id
.device
< 0 ? 0 : id
.device
+ 1;
1336 list_for_each(p
, &snd_timer_list
) {
1337 timer
= list_entry(p
, struct snd_timer
, device_list
);
1338 if (timer
->tmr_class
> SNDRV_TIMER_CLASS_GLOBAL
) {
1339 snd_timer_user_copy_id(&id
, timer
);
1342 if (timer
->tmr_device
>= id
.device
) {
1343 snd_timer_user_copy_id(&id
, timer
);
1347 if (p
== &snd_timer_list
)
1348 snd_timer_user_zero_id(&id
);
1350 case SNDRV_TIMER_CLASS_CARD
:
1351 case SNDRV_TIMER_CLASS_PCM
:
1358 if (id
.device
< 0) {
1361 if (id
.subdevice
< 0) {
1369 list_for_each(p
, &snd_timer_list
) {
1370 timer
= list_entry(p
, struct snd_timer
, device_list
);
1371 if (timer
->tmr_class
> id
.dev_class
) {
1372 snd_timer_user_copy_id(&id
, timer
);
1375 if (timer
->tmr_class
< id
.dev_class
)
1377 if (timer
->card
->number
> id
.card
) {
1378 snd_timer_user_copy_id(&id
, timer
);
1381 if (timer
->card
->number
< id
.card
)
1383 if (timer
->tmr_device
> id
.device
) {
1384 snd_timer_user_copy_id(&id
, timer
);
1387 if (timer
->tmr_device
< id
.device
)
1389 if (timer
->tmr_subdevice
> id
.subdevice
) {
1390 snd_timer_user_copy_id(&id
, timer
);
1393 if (timer
->tmr_subdevice
< id
.subdevice
)
1395 snd_timer_user_copy_id(&id
, timer
);
1398 if (p
== &snd_timer_list
)
1399 snd_timer_user_zero_id(&id
);
1402 snd_timer_user_zero_id(&id
);
1405 mutex_unlock(®ister_mutex
);
1406 if (copy_to_user(_tid
, &id
, sizeof(*_tid
)))
1411 static int snd_timer_user_ginfo(struct file
*file
,
1412 struct snd_timer_ginfo __user
*_ginfo
)
1414 struct snd_timer_ginfo
*ginfo
;
1415 struct snd_timer_id tid
;
1416 struct snd_timer
*t
;
1417 struct list_head
*p
;
1420 ginfo
= memdup_user(_ginfo
, sizeof(*ginfo
));
1422 return PTR_ERR(ginfo
);
1425 memset(ginfo
, 0, sizeof(*ginfo
));
1427 mutex_lock(®ister_mutex
);
1428 t
= snd_timer_find(&tid
);
1430 ginfo
->card
= t
->card
? t
->card
->number
: -1;
1431 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1432 ginfo
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1433 strlcpy(ginfo
->id
, t
->id
, sizeof(ginfo
->id
));
1434 strlcpy(ginfo
->name
, t
->name
, sizeof(ginfo
->name
));
1435 ginfo
->resolution
= t
->hw
.resolution
;
1436 if (t
->hw
.resolution_min
> 0) {
1437 ginfo
->resolution_min
= t
->hw
.resolution_min
;
1438 ginfo
->resolution_max
= t
->hw
.resolution_max
;
1440 list_for_each(p
, &t
->open_list_head
) {
1446 mutex_unlock(®ister_mutex
);
1447 if (err
>= 0 && copy_to_user(_ginfo
, ginfo
, sizeof(*ginfo
)))
1453 static int snd_timer_user_gparams(struct file
*file
,
1454 struct snd_timer_gparams __user
*_gparams
)
1456 struct snd_timer_gparams gparams
;
1457 struct snd_timer
*t
;
1460 if (copy_from_user(&gparams
, _gparams
, sizeof(gparams
)))
1462 mutex_lock(®ister_mutex
);
1463 t
= snd_timer_find(&gparams
.tid
);
1468 if (!list_empty(&t
->open_list_head
)) {
1472 if (!t
->hw
.set_period
) {
1476 err
= t
->hw
.set_period(t
, gparams
.period_num
, gparams
.period_den
);
1478 mutex_unlock(®ister_mutex
);
1482 static int snd_timer_user_gstatus(struct file
*file
,
1483 struct snd_timer_gstatus __user
*_gstatus
)
1485 struct snd_timer_gstatus gstatus
;
1486 struct snd_timer_id tid
;
1487 struct snd_timer
*t
;
1490 if (copy_from_user(&gstatus
, _gstatus
, sizeof(gstatus
)))
1493 memset(&gstatus
, 0, sizeof(gstatus
));
1495 mutex_lock(®ister_mutex
);
1496 t
= snd_timer_find(&tid
);
1498 if (t
->hw
.c_resolution
)
1499 gstatus
.resolution
= t
->hw
.c_resolution(t
);
1501 gstatus
.resolution
= t
->hw
.resolution
;
1502 if (t
->hw
.precise_resolution
) {
1503 t
->hw
.precise_resolution(t
, &gstatus
.resolution_num
,
1504 &gstatus
.resolution_den
);
1506 gstatus
.resolution_num
= gstatus
.resolution
;
1507 gstatus
.resolution_den
= 1000000000uL;
1512 mutex_unlock(®ister_mutex
);
1513 if (err
>= 0 && copy_to_user(_gstatus
, &gstatus
, sizeof(gstatus
)))
1518 static int snd_timer_user_tselect(struct file
*file
,
1519 struct snd_timer_select __user
*_tselect
)
1521 struct snd_timer_user
*tu
;
1522 struct snd_timer_select tselect
;
1526 tu
= file
->private_data
;
1528 snd_timer_close(tu
->timeri
);
1531 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
))) {
1535 sprintf(str
, "application %i", current
->pid
);
1536 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1537 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1538 err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
);
1547 tu
->tqueue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_tread
),
1549 if (tu
->tqueue
== NULL
)
1552 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1554 if (tu
->queue
== NULL
)
1559 snd_timer_close(tu
->timeri
);
1562 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1563 tu
->timeri
->callback
= tu
->tread
1564 ? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1565 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1566 tu
->timeri
->callback_data
= (void *)tu
;
1573 static int snd_timer_user_info(struct file
*file
,
1574 struct snd_timer_info __user
*_info
)
1576 struct snd_timer_user
*tu
;
1577 struct snd_timer_info
*info
;
1578 struct snd_timer
*t
;
1581 tu
= file
->private_data
;
1584 t
= tu
->timeri
->timer
;
1588 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1591 info
->card
= t
->card
? t
->card
->number
: -1;
1592 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1593 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1594 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1595 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1596 info
->resolution
= t
->hw
.resolution
;
1597 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1603 static int snd_timer_user_params(struct file
*file
,
1604 struct snd_timer_params __user
*_params
)
1606 struct snd_timer_user
*tu
;
1607 struct snd_timer_params params
;
1608 struct snd_timer
*t
;
1609 struct snd_timer_read
*tr
;
1610 struct snd_timer_tread
*ttr
;
1613 tu
= file
->private_data
;
1616 t
= tu
->timeri
->timer
;
1619 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
1621 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) && params
.ticks
< 1) {
1625 if (params
.queue_size
> 0 &&
1626 (params
.queue_size
< 32 || params
.queue_size
> 1024)) {
1630 if (params
.filter
& ~((1<<SNDRV_TIMER_EVENT_RESOLUTION
)|
1631 (1<<SNDRV_TIMER_EVENT_TICK
)|
1632 (1<<SNDRV_TIMER_EVENT_START
)|
1633 (1<<SNDRV_TIMER_EVENT_STOP
)|
1634 (1<<SNDRV_TIMER_EVENT_CONTINUE
)|
1635 (1<<SNDRV_TIMER_EVENT_PAUSE
)|
1636 (1<<SNDRV_TIMER_EVENT_SUSPEND
)|
1637 (1<<SNDRV_TIMER_EVENT_RESUME
)|
1638 (1<<SNDRV_TIMER_EVENT_MSTART
)|
1639 (1<<SNDRV_TIMER_EVENT_MSTOP
)|
1640 (1<<SNDRV_TIMER_EVENT_MCONTINUE
)|
1641 (1<<SNDRV_TIMER_EVENT_MPAUSE
)|
1642 (1<<SNDRV_TIMER_EVENT_MSUSPEND
)|
1643 (1<<SNDRV_TIMER_EVENT_MRESUME
))) {
1647 snd_timer_stop(tu
->timeri
);
1648 spin_lock_irq(&t
->lock
);
1649 tu
->timeri
->flags
&= ~(SNDRV_TIMER_IFLG_AUTO
|
1650 SNDRV_TIMER_IFLG_EXCLUSIVE
|
1651 SNDRV_TIMER_IFLG_EARLY_EVENT
);
1652 if (params
.flags
& SNDRV_TIMER_PSFLG_AUTO
)
1653 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
1654 if (params
.flags
& SNDRV_TIMER_PSFLG_EXCLUSIVE
)
1655 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EXCLUSIVE
;
1656 if (params
.flags
& SNDRV_TIMER_PSFLG_EARLY_EVENT
)
1657 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EARLY_EVENT
;
1658 spin_unlock_irq(&t
->lock
);
1659 if (params
.queue_size
> 0 &&
1660 (unsigned int)tu
->queue_size
!= params
.queue_size
) {
1662 ttr
= kmalloc(params
.queue_size
* sizeof(*ttr
),
1666 tu
->queue_size
= params
.queue_size
;
1670 tr
= kmalloc(params
.queue_size
* sizeof(*tr
),
1674 tu
->queue_size
= params
.queue_size
;
1679 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1680 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1682 struct snd_timer_tread tread
;
1683 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1684 tread
.tstamp
.tv_sec
= 0;
1685 tread
.tstamp
.tv_nsec
= 0;
1687 snd_timer_user_append_to_tqueue(tu
, &tread
);
1689 struct snd_timer_read
*r
= &tu
->queue
[0];
1696 tu
->filter
= params
.filter
;
1697 tu
->ticks
= params
.ticks
;
1700 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1705 static int snd_timer_user_status(struct file
*file
,
1706 struct snd_timer_status __user
*_status
)
1708 struct snd_timer_user
*tu
;
1709 struct snd_timer_status status
;
1711 tu
= file
->private_data
;
1714 memset(&status
, 0, sizeof(status
));
1715 status
.tstamp
= tu
->tstamp
;
1716 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1717 status
.lost
= tu
->timeri
->lost
;
1718 status
.overrun
= tu
->overrun
;
1719 spin_lock_irq(&tu
->qlock
);
1720 status
.queue
= tu
->qused
;
1721 spin_unlock_irq(&tu
->qlock
);
1722 if (copy_to_user(_status
, &status
, sizeof(status
)))
1727 static int snd_timer_user_start(struct file
*file
)
1730 struct snd_timer_user
*tu
;
1732 tu
= file
->private_data
;
1735 snd_timer_stop(tu
->timeri
);
1736 tu
->timeri
->lost
= 0;
1737 tu
->last_resolution
= 0;
1738 return (err
= snd_timer_start(tu
->timeri
, tu
->ticks
)) < 0 ? err
: 0;
1741 static int snd_timer_user_stop(struct file
*file
)
1744 struct snd_timer_user
*tu
;
1746 tu
= file
->private_data
;
1749 return (err
= snd_timer_stop(tu
->timeri
)) < 0 ? err
: 0;
1752 static int snd_timer_user_continue(struct file
*file
)
1755 struct snd_timer_user
*tu
;
1757 tu
= file
->private_data
;
1760 tu
->timeri
->lost
= 0;
1761 return (err
= snd_timer_continue(tu
->timeri
)) < 0 ? err
: 0;
1764 static int snd_timer_user_pause(struct file
*file
)
1767 struct snd_timer_user
*tu
;
1769 tu
= file
->private_data
;
1772 return (err
= snd_timer_pause(tu
->timeri
)) < 0 ? err
: 0;
1776 SNDRV_TIMER_IOCTL_START_OLD
= _IO('T', 0x20),
1777 SNDRV_TIMER_IOCTL_STOP_OLD
= _IO('T', 0x21),
1778 SNDRV_TIMER_IOCTL_CONTINUE_OLD
= _IO('T', 0x22),
1779 SNDRV_TIMER_IOCTL_PAUSE_OLD
= _IO('T', 0x23),
1782 static long __snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1785 struct snd_timer_user
*tu
;
1786 void __user
*argp
= (void __user
*)arg
;
1787 int __user
*p
= argp
;
1789 tu
= file
->private_data
;
1791 case SNDRV_TIMER_IOCTL_PVERSION
:
1792 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1793 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1794 return snd_timer_user_next_device(argp
);
1795 case SNDRV_TIMER_IOCTL_TREAD
:
1799 if (tu
->timeri
) /* too late */
1801 if (get_user(xarg
, p
))
1803 tu
->tread
= xarg
? 1 : 0;
1806 case SNDRV_TIMER_IOCTL_GINFO
:
1807 return snd_timer_user_ginfo(file
, argp
);
1808 case SNDRV_TIMER_IOCTL_GPARAMS
:
1809 return snd_timer_user_gparams(file
, argp
);
1810 case SNDRV_TIMER_IOCTL_GSTATUS
:
1811 return snd_timer_user_gstatus(file
, argp
);
1812 case SNDRV_TIMER_IOCTL_SELECT
:
1813 return snd_timer_user_tselect(file
, argp
);
1814 case SNDRV_TIMER_IOCTL_INFO
:
1815 return snd_timer_user_info(file
, argp
);
1816 case SNDRV_TIMER_IOCTL_PARAMS
:
1817 return snd_timer_user_params(file
, argp
);
1818 case SNDRV_TIMER_IOCTL_STATUS
:
1819 return snd_timer_user_status(file
, argp
);
1820 case SNDRV_TIMER_IOCTL_START
:
1821 case SNDRV_TIMER_IOCTL_START_OLD
:
1822 return snd_timer_user_start(file
);
1823 case SNDRV_TIMER_IOCTL_STOP
:
1824 case SNDRV_TIMER_IOCTL_STOP_OLD
:
1825 return snd_timer_user_stop(file
);
1826 case SNDRV_TIMER_IOCTL_CONTINUE
:
1827 case SNDRV_TIMER_IOCTL_CONTINUE_OLD
:
1828 return snd_timer_user_continue(file
);
1829 case SNDRV_TIMER_IOCTL_PAUSE
:
1830 case SNDRV_TIMER_IOCTL_PAUSE_OLD
:
1831 return snd_timer_user_pause(file
);
1836 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1839 struct snd_timer_user
*tu
= file
->private_data
;
1842 mutex_lock(&tu
->ioctl_lock
);
1843 ret
= __snd_timer_user_ioctl(file
, cmd
, arg
);
1844 mutex_unlock(&tu
->ioctl_lock
);
1848 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
1850 struct snd_timer_user
*tu
;
1852 tu
= file
->private_data
;
1853 return fasync_helper(fd
, file
, on
, &tu
->fasync
);
1856 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
,
1857 size_t count
, loff_t
*offset
)
1859 struct snd_timer_user
*tu
;
1860 long result
= 0, unit
;
1863 tu
= file
->private_data
;
1864 unit
= tu
->tread
? sizeof(struct snd_timer_tread
) : sizeof(struct snd_timer_read
);
1865 spin_lock_irq(&tu
->qlock
);
1866 while ((long)count
- result
>= unit
) {
1867 while (!tu
->qused
) {
1870 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1875 set_current_state(TASK_INTERRUPTIBLE
);
1876 init_waitqueue_entry(&wait
, current
);
1877 add_wait_queue(&tu
->qchange_sleep
, &wait
);
1879 spin_unlock_irq(&tu
->qlock
);
1881 spin_lock_irq(&tu
->qlock
);
1883 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
1885 if (signal_pending(current
)) {
1891 spin_unlock_irq(&tu
->qlock
);
1896 if (copy_to_user(buffer
, &tu
->tqueue
[tu
->qhead
++],
1897 sizeof(struct snd_timer_tread
))) {
1902 if (copy_to_user(buffer
, &tu
->queue
[tu
->qhead
++],
1903 sizeof(struct snd_timer_read
))) {
1909 tu
->qhead
%= tu
->queue_size
;
1914 spin_lock_irq(&tu
->qlock
);
1917 spin_unlock_irq(&tu
->qlock
);
1919 return result
> 0 ? result
: err
;
1922 static unsigned int snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
1925 struct snd_timer_user
*tu
;
1927 tu
= file
->private_data
;
1929 poll_wait(file
, &tu
->qchange_sleep
, wait
);
1933 mask
|= POLLIN
| POLLRDNORM
;
1938 #ifdef CONFIG_COMPAT
1939 #include "timer_compat.c"
1941 #define snd_timer_user_ioctl_compat NULL
1944 static const struct file_operations snd_timer_f_ops
=
1946 .owner
= THIS_MODULE
,
1947 .read
= snd_timer_user_read
,
1948 .open
= snd_timer_user_open
,
1949 .release
= snd_timer_user_release
,
1950 .llseek
= no_llseek
,
1951 .poll
= snd_timer_user_poll
,
1952 .unlocked_ioctl
= snd_timer_user_ioctl
,
1953 .compat_ioctl
= snd_timer_user_ioctl_compat
,
1954 .fasync
= snd_timer_user_fasync
,
1957 /* unregister the system timer */
1958 static void snd_timer_free_all(void)
1960 struct snd_timer
*timer
, *n
;
1962 list_for_each_entry_safe(timer
, n
, &snd_timer_list
, device_list
)
1963 snd_timer_free(timer
);
1966 static struct device timer_dev
;
1972 static int __init
alsa_timer_init(void)
1976 snd_device_initialize(&timer_dev
, NULL
);
1977 dev_set_name(&timer_dev
, "timer");
1979 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1980 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1,
1984 err
= snd_timer_register_system();
1986 pr_err("ALSA: unable to register system timer (%i)\n", err
);
1987 put_device(&timer_dev
);
1991 err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0,
1992 &snd_timer_f_ops
, NULL
, &timer_dev
);
1994 pr_err("ALSA: unable to register timer device (%i)\n", err
);
1995 snd_timer_free_all();
1996 put_device(&timer_dev
);
2000 snd_timer_proc_init();
2004 static void __exit
alsa_timer_exit(void)
2006 snd_unregister_device(&timer_dev
);
2007 snd_timer_free_all();
2008 put_device(&timer_dev
);
2009 snd_timer_proc_done();
2010 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2011 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
2015 module_init(alsa_timer_init
)
2016 module_exit(alsa_timer_exit
)
2018 EXPORT_SYMBOL(snd_timer_open
);
2019 EXPORT_SYMBOL(snd_timer_close
);
2020 EXPORT_SYMBOL(snd_timer_resolution
);
2021 EXPORT_SYMBOL(snd_timer_start
);
2022 EXPORT_SYMBOL(snd_timer_stop
);
2023 EXPORT_SYMBOL(snd_timer_continue
);
2024 EXPORT_SYMBOL(snd_timer_pause
);
2025 EXPORT_SYMBOL(snd_timer_new
);
2026 EXPORT_SYMBOL(snd_timer_notify
);
2027 EXPORT_SYMBOL(snd_timer_global_new
);
2028 EXPORT_SYMBOL(snd_timer_global_free
);
2029 EXPORT_SYMBOL(snd_timer_global_register
);
2030 EXPORT_SYMBOL(snd_timer_interrupt
);