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
;
69 struct snd_timer_read
*queue
;
70 struct snd_timer_tread
*tqueue
;
72 unsigned long last_resolution
;
74 struct timespec tstamp
; /* trigger tstamp */
75 wait_queue_head_t qchange_sleep
;
76 struct fasync_struct
*fasync
;
77 struct mutex ioctl_lock
;
81 static LIST_HEAD(snd_timer_list
);
83 /* list of slave instances */
84 static LIST_HEAD(snd_timer_slave_list
);
86 /* lock for slave active lists */
87 static DEFINE_SPINLOCK(slave_active_lock
);
89 static DEFINE_MUTEX(register_mutex
);
91 static int snd_timer_free(struct snd_timer
*timer
);
92 static int snd_timer_dev_free(struct snd_device
*device
);
93 static int snd_timer_dev_register(struct snd_device
*device
);
94 static int snd_timer_dev_disconnect(struct snd_device
*device
);
96 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
);
99 * create a timer instance with the given owner string.
100 * when timer is not NULL, increments the module counter
102 static struct snd_timer_instance
*snd_timer_instance_new(char *owner
,
103 struct snd_timer
*timer
)
105 struct snd_timer_instance
*timeri
;
106 timeri
= kzalloc(sizeof(*timeri
), GFP_KERNEL
);
109 timeri
->owner
= kstrdup(owner
, GFP_KERNEL
);
110 if (! timeri
->owner
) {
114 INIT_LIST_HEAD(&timeri
->open_list
);
115 INIT_LIST_HEAD(&timeri
->active_list
);
116 INIT_LIST_HEAD(&timeri
->ack_list
);
117 INIT_LIST_HEAD(&timeri
->slave_list_head
);
118 INIT_LIST_HEAD(&timeri
->slave_active_head
);
120 timeri
->timer
= timer
;
121 if (timer
&& !try_module_get(timer
->module
)) {
122 kfree(timeri
->owner
);
131 * find a timer instance from the given timer id
133 static struct snd_timer
*snd_timer_find(struct snd_timer_id
*tid
)
135 struct snd_timer
*timer
= NULL
;
137 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
138 if (timer
->tmr_class
!= tid
->dev_class
)
140 if ((timer
->tmr_class
== SNDRV_TIMER_CLASS_CARD
||
141 timer
->tmr_class
== SNDRV_TIMER_CLASS_PCM
) &&
142 (timer
->card
== NULL
||
143 timer
->card
->number
!= tid
->card
))
145 if (timer
->tmr_device
!= tid
->device
)
147 if (timer
->tmr_subdevice
!= tid
->subdevice
)
154 #ifdef CONFIG_MODULES
156 static void snd_timer_request(struct snd_timer_id
*tid
)
158 switch (tid
->dev_class
) {
159 case SNDRV_TIMER_CLASS_GLOBAL
:
160 if (tid
->device
< timer_limit
)
161 request_module("snd-timer-%i", tid
->device
);
163 case SNDRV_TIMER_CLASS_CARD
:
164 case SNDRV_TIMER_CLASS_PCM
:
165 if (tid
->card
< snd_ecards_limit
)
166 request_module("snd-card-%i", tid
->card
);
176 * look for a master instance matching with the slave id of the given slave.
177 * when found, relink the open_link of the slave.
179 * call this with register_mutex down.
181 static void snd_timer_check_slave(struct snd_timer_instance
*slave
)
183 struct snd_timer
*timer
;
184 struct snd_timer_instance
*master
;
186 /* FIXME: it's really dumb to look up all entries.. */
187 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
188 list_for_each_entry(master
, &timer
->open_list_head
, open_list
) {
189 if (slave
->slave_class
== master
->slave_class
&&
190 slave
->slave_id
== master
->slave_id
) {
191 list_move_tail(&slave
->open_list
,
192 &master
->slave_list_head
);
193 spin_lock_irq(&slave_active_lock
);
194 slave
->master
= master
;
195 slave
->timer
= master
->timer
;
196 spin_unlock_irq(&slave_active_lock
);
204 * look for slave instances matching with the slave id of the given master.
205 * when found, relink the open_link of slaves.
207 * call this with register_mutex down.
209 static void snd_timer_check_master(struct snd_timer_instance
*master
)
211 struct snd_timer_instance
*slave
, *tmp
;
213 /* check all pending slaves */
214 list_for_each_entry_safe(slave
, tmp
, &snd_timer_slave_list
, open_list
) {
215 if (slave
->slave_class
== master
->slave_class
&&
216 slave
->slave_id
== master
->slave_id
) {
217 list_move_tail(&slave
->open_list
, &master
->slave_list_head
);
218 spin_lock_irq(&slave_active_lock
);
219 spin_lock(&master
->timer
->lock
);
220 slave
->master
= master
;
221 slave
->timer
= master
->timer
;
222 if (slave
->flags
& SNDRV_TIMER_IFLG_RUNNING
)
223 list_add_tail(&slave
->active_list
,
224 &master
->slave_active_head
);
225 spin_unlock(&master
->timer
->lock
);
226 spin_unlock_irq(&slave_active_lock
);
232 * open a timer instance
233 * when opening a master, the slave id must be here given.
235 int snd_timer_open(struct snd_timer_instance
**ti
,
236 char *owner
, struct snd_timer_id
*tid
,
237 unsigned int slave_id
)
239 struct snd_timer
*timer
;
240 struct snd_timer_instance
*timeri
= NULL
;
242 if (tid
->dev_class
== SNDRV_TIMER_CLASS_SLAVE
) {
243 /* open a slave instance */
244 if (tid
->dev_sclass
<= SNDRV_TIMER_SCLASS_NONE
||
245 tid
->dev_sclass
> SNDRV_TIMER_SCLASS_OSS_SEQUENCER
) {
246 pr_debug("ALSA: timer: invalid slave class %i\n",
250 mutex_lock(®ister_mutex
);
251 timeri
= snd_timer_instance_new(owner
, NULL
);
253 mutex_unlock(®ister_mutex
);
256 timeri
->slave_class
= tid
->dev_sclass
;
257 timeri
->slave_id
= tid
->device
;
258 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
259 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
260 snd_timer_check_slave(timeri
);
261 mutex_unlock(®ister_mutex
);
266 /* open a master instance */
267 mutex_lock(®ister_mutex
);
268 timer
= snd_timer_find(tid
);
269 #ifdef CONFIG_MODULES
271 mutex_unlock(®ister_mutex
);
272 snd_timer_request(tid
);
273 mutex_lock(®ister_mutex
);
274 timer
= snd_timer_find(tid
);
278 mutex_unlock(®ister_mutex
);
281 if (!list_empty(&timer
->open_list_head
)) {
282 timeri
= list_entry(timer
->open_list_head
.next
,
283 struct snd_timer_instance
, open_list
);
284 if (timeri
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
285 mutex_unlock(®ister_mutex
);
289 timeri
= snd_timer_instance_new(owner
, timer
);
291 mutex_unlock(®ister_mutex
);
294 /* take a card refcount for safe disconnection */
296 get_device(&timer
->card
->card_dev
);
297 timeri
->slave_class
= tid
->dev_sclass
;
298 timeri
->slave_id
= slave_id
;
300 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
) {
301 int err
= timer
->hw
.open(timer
);
303 kfree(timeri
->owner
);
307 put_device(&timer
->card
->card_dev
);
308 module_put(timer
->module
);
309 mutex_unlock(®ister_mutex
);
314 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
315 snd_timer_check_master(timeri
);
316 mutex_unlock(®ister_mutex
);
322 * close a timer instance
324 int snd_timer_close(struct snd_timer_instance
*timeri
)
326 struct snd_timer
*timer
= NULL
;
327 struct snd_timer_instance
*slave
, *tmp
;
329 if (snd_BUG_ON(!timeri
))
332 /* force to stop the timer */
333 snd_timer_stop(timeri
);
335 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
336 /* wait, until the active callback is finished */
337 spin_lock_irq(&slave_active_lock
);
338 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
339 spin_unlock_irq(&slave_active_lock
);
341 spin_lock_irq(&slave_active_lock
);
343 spin_unlock_irq(&slave_active_lock
);
344 mutex_lock(®ister_mutex
);
345 list_del(&timeri
->open_list
);
346 mutex_unlock(®ister_mutex
);
348 timer
= timeri
->timer
;
349 if (snd_BUG_ON(!timer
))
351 /* wait, until the active callback is finished */
352 spin_lock_irq(&timer
->lock
);
353 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
354 spin_unlock_irq(&timer
->lock
);
356 spin_lock_irq(&timer
->lock
);
358 spin_unlock_irq(&timer
->lock
);
359 mutex_lock(®ister_mutex
);
360 list_del(&timeri
->open_list
);
361 if (list_empty(&timer
->open_list_head
) &&
363 timer
->hw
.close(timer
);
364 /* remove slave links */
365 spin_lock_irq(&slave_active_lock
);
366 spin_lock(&timer
->lock
);
367 list_for_each_entry_safe(slave
, tmp
, &timeri
->slave_list_head
,
369 list_move_tail(&slave
->open_list
, &snd_timer_slave_list
);
370 slave
->master
= NULL
;
372 list_del_init(&slave
->ack_list
);
373 list_del_init(&slave
->active_list
);
375 spin_unlock(&timer
->lock
);
376 spin_unlock_irq(&slave_active_lock
);
377 /* release a card refcount for safe disconnection */
379 put_device(&timer
->card
->card_dev
);
380 mutex_unlock(®ister_mutex
);
383 if (timeri
->private_free
)
384 timeri
->private_free(timeri
);
385 kfree(timeri
->owner
);
388 module_put(timer
->module
);
392 unsigned long snd_timer_resolution(struct snd_timer_instance
*timeri
)
394 struct snd_timer
* timer
;
398 if ((timer
= timeri
->timer
) != NULL
) {
399 if (timer
->hw
.c_resolution
)
400 return timer
->hw
.c_resolution(timer
);
401 return timer
->hw
.resolution
;
406 static void snd_timer_notify1(struct snd_timer_instance
*ti
, int event
)
408 struct snd_timer
*timer
;
409 unsigned long resolution
= 0;
410 struct snd_timer_instance
*ts
;
411 struct timespec tstamp
;
413 if (timer_tstamp_monotonic
)
414 ktime_get_ts(&tstamp
);
416 getnstimeofday(&tstamp
);
417 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_START
||
418 event
> SNDRV_TIMER_EVENT_PAUSE
))
420 if (event
== SNDRV_TIMER_EVENT_START
||
421 event
== SNDRV_TIMER_EVENT_CONTINUE
)
422 resolution
= snd_timer_resolution(ti
);
424 ti
->ccallback(ti
, event
, &tstamp
, resolution
);
425 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
430 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
432 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
434 ts
->ccallback(ts
, event
+ 100, &tstamp
, resolution
);
437 /* start/continue a master timer */
438 static int snd_timer_start1(struct snd_timer_instance
*timeri
,
439 bool start
, unsigned long ticks
)
441 struct snd_timer
*timer
;
445 timer
= timeri
->timer
;
449 spin_lock_irqsave(&timer
->lock
, flags
);
450 if (timer
->card
&& timer
->card
->shutdown
) {
454 if (timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
455 SNDRV_TIMER_IFLG_START
)) {
461 timeri
->ticks
= timeri
->cticks
= ticks
;
462 else if (!timeri
->cticks
)
466 list_move_tail(&timeri
->active_list
, &timer
->active_list_head
);
467 if (timer
->running
) {
468 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
470 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
471 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
472 result
= 1; /* delayed start */
475 timer
->sticks
= ticks
;
476 timer
->hw
.start(timer
);
479 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
482 snd_timer_notify1(timeri
, start
? SNDRV_TIMER_EVENT_START
:
483 SNDRV_TIMER_EVENT_CONTINUE
);
485 spin_unlock_irqrestore(&timer
->lock
, flags
);
489 /* start/continue a slave timer */
490 static int snd_timer_start_slave(struct snd_timer_instance
*timeri
,
495 spin_lock_irqsave(&slave_active_lock
, flags
);
496 if (timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
497 spin_unlock_irqrestore(&slave_active_lock
, flags
);
500 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
501 if (timeri
->master
&& timeri
->timer
) {
502 spin_lock(&timeri
->timer
->lock
);
503 list_add_tail(&timeri
->active_list
,
504 &timeri
->master
->slave_active_head
);
505 snd_timer_notify1(timeri
, start
? SNDRV_TIMER_EVENT_START
:
506 SNDRV_TIMER_EVENT_CONTINUE
);
507 spin_unlock(&timeri
->timer
->lock
);
509 spin_unlock_irqrestore(&slave_active_lock
, flags
);
510 return 1; /* delayed start */
513 /* stop/pause a master timer */
514 static int snd_timer_stop1(struct snd_timer_instance
*timeri
, bool stop
)
516 struct snd_timer
*timer
;
520 timer
= timeri
->timer
;
523 spin_lock_irqsave(&timer
->lock
, flags
);
524 if (!(timeri
->flags
& (SNDRV_TIMER_IFLG_RUNNING
|
525 SNDRV_TIMER_IFLG_START
))) {
529 list_del_init(&timeri
->ack_list
);
530 list_del_init(&timeri
->active_list
);
531 if (timer
->card
&& timer
->card
->shutdown
)
534 timeri
->cticks
= timeri
->ticks
;
537 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
538 !(--timer
->running
)) {
539 timer
->hw
.stop(timer
);
540 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
541 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
542 snd_timer_reschedule(timer
, 0);
543 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
544 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
545 timer
->hw
.start(timer
);
549 timeri
->flags
&= ~(SNDRV_TIMER_IFLG_RUNNING
| SNDRV_TIMER_IFLG_START
);
550 snd_timer_notify1(timeri
, stop
? SNDRV_TIMER_EVENT_STOP
:
551 SNDRV_TIMER_EVENT_PAUSE
);
553 spin_unlock_irqrestore(&timer
->lock
, flags
);
557 /* stop/pause a slave timer */
558 static int snd_timer_stop_slave(struct snd_timer_instance
*timeri
, bool stop
)
562 spin_lock_irqsave(&slave_active_lock
, flags
);
563 if (!(timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
)) {
564 spin_unlock_irqrestore(&slave_active_lock
, flags
);
567 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
569 spin_lock(&timeri
->timer
->lock
);
570 list_del_init(&timeri
->ack_list
);
571 list_del_init(&timeri
->active_list
);
572 snd_timer_notify1(timeri
, stop
? SNDRV_TIMER_EVENT_STOP
:
573 SNDRV_TIMER_EVENT_PAUSE
);
574 spin_unlock(&timeri
->timer
->lock
);
576 spin_unlock_irqrestore(&slave_active_lock
, flags
);
581 * start the timer instance
583 int snd_timer_start(struct snd_timer_instance
*timeri
, unsigned int ticks
)
585 if (timeri
== NULL
|| ticks
< 1)
587 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
588 return snd_timer_start_slave(timeri
, true);
590 return snd_timer_start1(timeri
, true, ticks
);
594 * stop the timer instance.
596 * do not call this from the timer callback!
598 int snd_timer_stop(struct snd_timer_instance
*timeri
)
600 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
601 return snd_timer_stop_slave(timeri
, true);
603 return snd_timer_stop1(timeri
, true);
607 * start again.. the tick is kept.
609 int snd_timer_continue(struct snd_timer_instance
*timeri
)
611 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
612 return snd_timer_start_slave(timeri
, false);
614 return snd_timer_start1(timeri
, false, 0);
618 * pause.. remember the ticks left
620 int snd_timer_pause(struct snd_timer_instance
* timeri
)
622 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
623 return snd_timer_stop_slave(timeri
, false);
625 return snd_timer_stop1(timeri
, false);
629 * reschedule the timer
631 * start pending instances and check the scheduling ticks.
632 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
634 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
)
636 struct snd_timer_instance
*ti
;
637 unsigned long ticks
= ~0UL;
639 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
640 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
641 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
642 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
645 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
646 if (ticks
> ti
->cticks
)
651 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
654 if (ticks
> timer
->hw
.ticks
)
655 ticks
= timer
->hw
.ticks
;
656 if (ticks_left
!= ticks
)
657 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
658 timer
->sticks
= ticks
;
665 static void snd_timer_tasklet(unsigned long arg
)
667 struct snd_timer
*timer
= (struct snd_timer
*) arg
;
668 struct snd_timer_instance
*ti
;
670 unsigned long resolution
, ticks
;
673 if (timer
->card
&& timer
->card
->shutdown
)
676 spin_lock_irqsave(&timer
->lock
, flags
);
677 /* now process all callbacks */
678 while (!list_empty(&timer
->sack_list_head
)) {
679 p
= timer
->sack_list_head
.next
; /* get first item */
680 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
682 /* remove from ack_list and make empty */
687 resolution
= ti
->resolution
;
689 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
690 spin_unlock(&timer
->lock
);
692 ti
->callback(ti
, resolution
, ticks
);
693 spin_lock(&timer
->lock
);
694 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
696 spin_unlock_irqrestore(&timer
->lock
, flags
);
702 * ticks_left is usually equal to timer->sticks.
705 void snd_timer_interrupt(struct snd_timer
* timer
, unsigned long ticks_left
)
707 struct snd_timer_instance
*ti
, *ts
, *tmp
;
708 unsigned long resolution
, ticks
;
709 struct list_head
*p
, *ack_list_head
;
716 if (timer
->card
&& timer
->card
->shutdown
)
719 spin_lock_irqsave(&timer
->lock
, flags
);
721 /* remember the current resolution */
722 if (timer
->hw
.c_resolution
)
723 resolution
= timer
->hw
.c_resolution(timer
);
725 resolution
= timer
->hw
.resolution
;
727 /* loop for all active instances
728 * Here we cannot use list_for_each_entry because the active_list of a
729 * processed instance is relinked to done_list_head before the callback
732 list_for_each_entry_safe(ti
, tmp
, &timer
->active_list_head
,
734 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
736 ti
->pticks
+= ticks_left
;
737 ti
->resolution
= resolution
;
738 if (ti
->cticks
< ticks_left
)
741 ti
->cticks
-= ticks_left
;
742 if (ti
->cticks
) /* not expired */
744 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
745 ti
->cticks
= ti
->ticks
;
747 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
749 list_del_init(&ti
->active_list
);
751 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
752 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
))
753 ack_list_head
= &timer
->ack_list_head
;
755 ack_list_head
= &timer
->sack_list_head
;
756 if (list_empty(&ti
->ack_list
))
757 list_add_tail(&ti
->ack_list
, ack_list_head
);
758 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
) {
759 ts
->pticks
= ti
->pticks
;
760 ts
->resolution
= resolution
;
761 if (list_empty(&ts
->ack_list
))
762 list_add_tail(&ts
->ack_list
, ack_list_head
);
765 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
766 snd_timer_reschedule(timer
, timer
->sticks
);
767 if (timer
->running
) {
768 if (timer
->hw
.flags
& SNDRV_TIMER_HW_STOP
) {
769 timer
->hw
.stop(timer
);
770 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
772 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_AUTO
) ||
773 (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
)) {
775 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
776 timer
->hw
.start(timer
);
779 timer
->hw
.stop(timer
);
782 /* now process all fast callbacks */
783 while (!list_empty(&timer
->ack_list_head
)) {
784 p
= timer
->ack_list_head
.next
; /* get first item */
785 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
787 /* remove from ack_list and make empty */
793 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
794 spin_unlock(&timer
->lock
);
796 ti
->callback(ti
, resolution
, ticks
);
797 spin_lock(&timer
->lock
);
798 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
801 /* do we have any slow callbacks? */
802 use_tasklet
= !list_empty(&timer
->sack_list_head
);
803 spin_unlock_irqrestore(&timer
->lock
, flags
);
806 tasklet_schedule(&timer
->task_queue
);
813 int snd_timer_new(struct snd_card
*card
, char *id
, struct snd_timer_id
*tid
,
814 struct snd_timer
**rtimer
)
816 struct snd_timer
*timer
;
818 static struct snd_device_ops ops
= {
819 .dev_free
= snd_timer_dev_free
,
820 .dev_register
= snd_timer_dev_register
,
821 .dev_disconnect
= snd_timer_dev_disconnect
,
824 if (snd_BUG_ON(!tid
))
828 timer
= kzalloc(sizeof(*timer
), GFP_KERNEL
);
831 timer
->tmr_class
= tid
->dev_class
;
833 timer
->tmr_device
= tid
->device
;
834 timer
->tmr_subdevice
= tid
->subdevice
;
836 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
838 INIT_LIST_HEAD(&timer
->device_list
);
839 INIT_LIST_HEAD(&timer
->open_list_head
);
840 INIT_LIST_HEAD(&timer
->active_list_head
);
841 INIT_LIST_HEAD(&timer
->ack_list_head
);
842 INIT_LIST_HEAD(&timer
->sack_list_head
);
843 spin_lock_init(&timer
->lock
);
844 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
,
845 (unsigned long)timer
);
847 timer
->module
= card
->module
;
848 err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
);
850 snd_timer_free(timer
);
859 static int snd_timer_free(struct snd_timer
*timer
)
864 mutex_lock(®ister_mutex
);
865 if (! list_empty(&timer
->open_list_head
)) {
866 struct list_head
*p
, *n
;
867 struct snd_timer_instance
*ti
;
868 pr_warn("ALSA: timer %p is busy?\n", timer
);
869 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
871 ti
= list_entry(p
, struct snd_timer_instance
, open_list
);
875 list_del(&timer
->device_list
);
876 mutex_unlock(®ister_mutex
);
878 if (timer
->private_free
)
879 timer
->private_free(timer
);
884 static int snd_timer_dev_free(struct snd_device
*device
)
886 struct snd_timer
*timer
= device
->device_data
;
887 return snd_timer_free(timer
);
890 static int snd_timer_dev_register(struct snd_device
*dev
)
892 struct snd_timer
*timer
= dev
->device_data
;
893 struct snd_timer
*timer1
;
895 if (snd_BUG_ON(!timer
|| !timer
->hw
.start
|| !timer
->hw
.stop
))
897 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
898 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
901 mutex_lock(®ister_mutex
);
902 list_for_each_entry(timer1
, &snd_timer_list
, device_list
) {
903 if (timer1
->tmr_class
> timer
->tmr_class
)
905 if (timer1
->tmr_class
< timer
->tmr_class
)
907 if (timer1
->card
&& timer
->card
) {
908 if (timer1
->card
->number
> timer
->card
->number
)
910 if (timer1
->card
->number
< timer
->card
->number
)
913 if (timer1
->tmr_device
> timer
->tmr_device
)
915 if (timer1
->tmr_device
< timer
->tmr_device
)
917 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
919 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
922 mutex_unlock(®ister_mutex
);
925 list_add_tail(&timer
->device_list
, &timer1
->device_list
);
926 mutex_unlock(®ister_mutex
);
930 /* just for reference in snd_timer_dev_disconnect() below */
931 static void snd_timer_user_ccallback(struct snd_timer_instance
*timeri
,
932 int event
, struct timespec
*tstamp
,
933 unsigned long resolution
);
935 static int snd_timer_dev_disconnect(struct snd_device
*device
)
937 struct snd_timer
*timer
= device
->device_data
;
938 struct snd_timer_instance
*ti
;
940 mutex_lock(®ister_mutex
);
941 list_del_init(&timer
->device_list
);
942 /* wake up pending sleepers */
943 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
) {
944 /* FIXME: better to have a ti.disconnect() op */
945 if (ti
->ccallback
== snd_timer_user_ccallback
) {
946 struct snd_timer_user
*tu
= ti
->callback_data
;
948 tu
->disconnected
= true;
949 wake_up(&tu
->qchange_sleep
);
952 mutex_unlock(®ister_mutex
);
956 void snd_timer_notify(struct snd_timer
*timer
, int event
, struct timespec
*tstamp
)
959 unsigned long resolution
= 0;
960 struct snd_timer_instance
*ti
, *ts
;
962 if (timer
->card
&& timer
->card
->shutdown
)
964 if (! (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
))
966 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_MSTART
||
967 event
> SNDRV_TIMER_EVENT_MRESUME
))
969 spin_lock_irqsave(&timer
->lock
, flags
);
970 if (event
== SNDRV_TIMER_EVENT_MSTART
||
971 event
== SNDRV_TIMER_EVENT_MCONTINUE
||
972 event
== SNDRV_TIMER_EVENT_MRESUME
) {
973 if (timer
->hw
.c_resolution
)
974 resolution
= timer
->hw
.c_resolution(timer
);
976 resolution
= timer
->hw
.resolution
;
978 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
980 ti
->ccallback(ti
, event
, tstamp
, resolution
);
981 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
983 ts
->ccallback(ts
, event
, tstamp
, resolution
);
985 spin_unlock_irqrestore(&timer
->lock
, flags
);
989 * exported functions for global timers
991 int snd_timer_global_new(char *id
, int device
, struct snd_timer
**rtimer
)
993 struct snd_timer_id tid
;
995 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
996 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1000 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
1003 int snd_timer_global_free(struct snd_timer
*timer
)
1005 return snd_timer_free(timer
);
1008 int snd_timer_global_register(struct snd_timer
*timer
)
1010 struct snd_device dev
;
1012 memset(&dev
, 0, sizeof(dev
));
1013 dev
.device_data
= timer
;
1014 return snd_timer_dev_register(&dev
);
1021 struct snd_timer_system_private
{
1022 struct timer_list tlist
;
1023 unsigned long last_expires
;
1024 unsigned long last_jiffies
;
1025 unsigned long correction
;
1028 static void snd_timer_s_function(unsigned long data
)
1030 struct snd_timer
*timer
= (struct snd_timer
*)data
;
1031 struct snd_timer_system_private
*priv
= timer
->private_data
;
1032 unsigned long jiff
= jiffies
;
1033 if (time_after(jiff
, priv
->last_expires
))
1034 priv
->correction
+= (long)jiff
- (long)priv
->last_expires
;
1035 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
1038 static int snd_timer_s_start(struct snd_timer
* timer
)
1040 struct snd_timer_system_private
*priv
;
1041 unsigned long njiff
;
1043 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1044 njiff
= (priv
->last_jiffies
= jiffies
);
1045 if (priv
->correction
> timer
->sticks
- 1) {
1046 priv
->correction
-= timer
->sticks
- 1;
1049 njiff
+= timer
->sticks
- priv
->correction
;
1050 priv
->correction
= 0;
1052 priv
->last_expires
= njiff
;
1053 mod_timer(&priv
->tlist
, njiff
);
1057 static int snd_timer_s_stop(struct snd_timer
* timer
)
1059 struct snd_timer_system_private
*priv
;
1062 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1063 del_timer(&priv
->tlist
);
1065 if (time_before(jiff
, priv
->last_expires
))
1066 timer
->sticks
= priv
->last_expires
- jiff
;
1069 priv
->correction
= 0;
1073 static struct snd_timer_hardware snd_timer_system
=
1075 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
1076 .resolution
= 1000000000L / HZ
,
1078 .start
= snd_timer_s_start
,
1079 .stop
= snd_timer_s_stop
1082 static void snd_timer_free_system(struct snd_timer
*timer
)
1084 kfree(timer
->private_data
);
1087 static int snd_timer_register_system(void)
1089 struct snd_timer
*timer
;
1090 struct snd_timer_system_private
*priv
;
1093 err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
);
1096 strcpy(timer
->name
, "system timer");
1097 timer
->hw
= snd_timer_system
;
1098 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1100 snd_timer_free(timer
);
1103 setup_timer(&priv
->tlist
, snd_timer_s_function
, (unsigned long) timer
);
1104 timer
->private_data
= priv
;
1105 timer
->private_free
= snd_timer_free_system
;
1106 return snd_timer_global_register(timer
);
1109 #ifdef CONFIG_SND_PROC_FS
1114 static void snd_timer_proc_read(struct snd_info_entry
*entry
,
1115 struct snd_info_buffer
*buffer
)
1117 struct snd_timer
*timer
;
1118 struct snd_timer_instance
*ti
;
1120 mutex_lock(®ister_mutex
);
1121 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
1122 if (timer
->card
&& timer
->card
->shutdown
)
1124 switch (timer
->tmr_class
) {
1125 case SNDRV_TIMER_CLASS_GLOBAL
:
1126 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1128 case SNDRV_TIMER_CLASS_CARD
:
1129 snd_iprintf(buffer
, "C%i-%i: ",
1130 timer
->card
->number
, timer
->tmr_device
);
1132 case SNDRV_TIMER_CLASS_PCM
:
1133 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
,
1134 timer
->tmr_device
, timer
->tmr_subdevice
);
1137 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
,
1138 timer
->card
? timer
->card
->number
: -1,
1139 timer
->tmr_device
, timer
->tmr_subdevice
);
1141 snd_iprintf(buffer
, "%s :", timer
->name
);
1142 if (timer
->hw
.resolution
)
1143 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)",
1144 timer
->hw
.resolution
/ 1000,
1145 timer
->hw
.resolution
% 1000,
1147 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1148 snd_iprintf(buffer
, " SLAVE");
1149 snd_iprintf(buffer
, "\n");
1150 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
)
1151 snd_iprintf(buffer
, " Client %s : %s\n",
1152 ti
->owner
? ti
->owner
: "unknown",
1153 ti
->flags
& (SNDRV_TIMER_IFLG_START
|
1154 SNDRV_TIMER_IFLG_RUNNING
)
1155 ? "running" : "stopped");
1157 mutex_unlock(®ister_mutex
);
1160 static struct snd_info_entry
*snd_timer_proc_entry
;
1162 static void __init
snd_timer_proc_init(void)
1164 struct snd_info_entry
*entry
;
1166 entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
);
1167 if (entry
!= NULL
) {
1168 entry
->c
.text
.read
= snd_timer_proc_read
;
1169 if (snd_info_register(entry
) < 0) {
1170 snd_info_free_entry(entry
);
1174 snd_timer_proc_entry
= entry
;
1177 static void __exit
snd_timer_proc_done(void)
1179 snd_info_free_entry(snd_timer_proc_entry
);
1181 #else /* !CONFIG_SND_PROC_FS */
1182 #define snd_timer_proc_init()
1183 #define snd_timer_proc_done()
1187 * USER SPACE interface
1190 static void snd_timer_user_interrupt(struct snd_timer_instance
*timeri
,
1191 unsigned long resolution
,
1192 unsigned long ticks
)
1194 struct snd_timer_user
*tu
= timeri
->callback_data
;
1195 struct snd_timer_read
*r
;
1198 spin_lock(&tu
->qlock
);
1199 if (tu
->qused
> 0) {
1200 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1201 r
= &tu
->queue
[prev
];
1202 if (r
->resolution
== resolution
) {
1207 if (tu
->qused
>= tu
->queue_size
) {
1210 r
= &tu
->queue
[tu
->qtail
++];
1211 tu
->qtail
%= tu
->queue_size
;
1212 r
->resolution
= resolution
;
1217 spin_unlock(&tu
->qlock
);
1218 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1219 wake_up(&tu
->qchange_sleep
);
1222 static void snd_timer_user_append_to_tqueue(struct snd_timer_user
*tu
,
1223 struct snd_timer_tread
*tread
)
1225 if (tu
->qused
>= tu
->queue_size
) {
1228 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1229 tu
->qtail
%= tu
->queue_size
;
1234 static void snd_timer_user_ccallback(struct snd_timer_instance
*timeri
,
1236 struct timespec
*tstamp
,
1237 unsigned long resolution
)
1239 struct snd_timer_user
*tu
= timeri
->callback_data
;
1240 struct snd_timer_tread r1
;
1241 unsigned long flags
;
1243 if (event
>= SNDRV_TIMER_EVENT_START
&&
1244 event
<= SNDRV_TIMER_EVENT_PAUSE
)
1245 tu
->tstamp
= *tstamp
;
1246 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1248 memset(&r1
, 0, sizeof(r1
));
1250 r1
.tstamp
= *tstamp
;
1251 r1
.val
= resolution
;
1252 spin_lock_irqsave(&tu
->qlock
, flags
);
1253 snd_timer_user_append_to_tqueue(tu
, &r1
);
1254 spin_unlock_irqrestore(&tu
->qlock
, flags
);
1255 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1256 wake_up(&tu
->qchange_sleep
);
1259 static void snd_timer_user_tinterrupt(struct snd_timer_instance
*timeri
,
1260 unsigned long resolution
,
1261 unsigned long ticks
)
1263 struct snd_timer_user
*tu
= timeri
->callback_data
;
1264 struct snd_timer_tread
*r
, r1
;
1265 struct timespec tstamp
;
1266 int prev
, append
= 0;
1268 memset(&tstamp
, 0, sizeof(tstamp
));
1269 spin_lock(&tu
->qlock
);
1270 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
) |
1271 (1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1272 spin_unlock(&tu
->qlock
);
1275 if (tu
->last_resolution
!= resolution
|| ticks
> 0) {
1276 if (timer_tstamp_monotonic
)
1277 ktime_get_ts(&tstamp
);
1279 getnstimeofday(&tstamp
);
1281 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) &&
1282 tu
->last_resolution
!= resolution
) {
1283 memset(&r1
, 0, sizeof(r1
));
1284 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1286 r1
.val
= resolution
;
1287 snd_timer_user_append_to_tqueue(tu
, &r1
);
1288 tu
->last_resolution
= resolution
;
1291 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1295 if (tu
->qused
> 0) {
1296 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1297 r
= &tu
->tqueue
[prev
];
1298 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1305 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1308 snd_timer_user_append_to_tqueue(tu
, &r1
);
1311 spin_unlock(&tu
->qlock
);
1314 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1315 wake_up(&tu
->qchange_sleep
);
1318 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1320 struct snd_timer_user
*tu
;
1323 err
= nonseekable_open(inode
, file
);
1327 tu
= kzalloc(sizeof(*tu
), GFP_KERNEL
);
1330 spin_lock_init(&tu
->qlock
);
1331 init_waitqueue_head(&tu
->qchange_sleep
);
1332 mutex_init(&tu
->ioctl_lock
);
1334 tu
->queue_size
= 128;
1335 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1337 if (tu
->queue
== NULL
) {
1341 file
->private_data
= tu
;
1345 static int snd_timer_user_release(struct inode
*inode
, struct file
*file
)
1347 struct snd_timer_user
*tu
;
1349 if (file
->private_data
) {
1350 tu
= file
->private_data
;
1351 file
->private_data
= NULL
;
1352 mutex_lock(&tu
->ioctl_lock
);
1354 snd_timer_close(tu
->timeri
);
1355 mutex_unlock(&tu
->ioctl_lock
);
1363 static void snd_timer_user_zero_id(struct snd_timer_id
*id
)
1365 id
->dev_class
= SNDRV_TIMER_CLASS_NONE
;
1366 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1372 static void snd_timer_user_copy_id(struct snd_timer_id
*id
, struct snd_timer
*timer
)
1374 id
->dev_class
= timer
->tmr_class
;
1375 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1376 id
->card
= timer
->card
? timer
->card
->number
: -1;
1377 id
->device
= timer
->tmr_device
;
1378 id
->subdevice
= timer
->tmr_subdevice
;
1381 static int snd_timer_user_next_device(struct snd_timer_id __user
*_tid
)
1383 struct snd_timer_id id
;
1384 struct snd_timer
*timer
;
1385 struct list_head
*p
;
1387 if (copy_from_user(&id
, _tid
, sizeof(id
)))
1389 mutex_lock(®ister_mutex
);
1390 if (id
.dev_class
< 0) { /* first item */
1391 if (list_empty(&snd_timer_list
))
1392 snd_timer_user_zero_id(&id
);
1394 timer
= list_entry(snd_timer_list
.next
,
1395 struct snd_timer
, device_list
);
1396 snd_timer_user_copy_id(&id
, timer
);
1399 switch (id
.dev_class
) {
1400 case SNDRV_TIMER_CLASS_GLOBAL
:
1401 id
.device
= id
.device
< 0 ? 0 : id
.device
+ 1;
1402 list_for_each(p
, &snd_timer_list
) {
1403 timer
= list_entry(p
, struct snd_timer
, device_list
);
1404 if (timer
->tmr_class
> SNDRV_TIMER_CLASS_GLOBAL
) {
1405 snd_timer_user_copy_id(&id
, timer
);
1408 if (timer
->tmr_device
>= id
.device
) {
1409 snd_timer_user_copy_id(&id
, timer
);
1413 if (p
== &snd_timer_list
)
1414 snd_timer_user_zero_id(&id
);
1416 case SNDRV_TIMER_CLASS_CARD
:
1417 case SNDRV_TIMER_CLASS_PCM
:
1424 if (id
.device
< 0) {
1427 if (id
.subdevice
< 0) {
1435 list_for_each(p
, &snd_timer_list
) {
1436 timer
= list_entry(p
, struct snd_timer
, device_list
);
1437 if (timer
->tmr_class
> id
.dev_class
) {
1438 snd_timer_user_copy_id(&id
, timer
);
1441 if (timer
->tmr_class
< id
.dev_class
)
1443 if (timer
->card
->number
> id
.card
) {
1444 snd_timer_user_copy_id(&id
, timer
);
1447 if (timer
->card
->number
< id
.card
)
1449 if (timer
->tmr_device
> id
.device
) {
1450 snd_timer_user_copy_id(&id
, timer
);
1453 if (timer
->tmr_device
< id
.device
)
1455 if (timer
->tmr_subdevice
> id
.subdevice
) {
1456 snd_timer_user_copy_id(&id
, timer
);
1459 if (timer
->tmr_subdevice
< id
.subdevice
)
1461 snd_timer_user_copy_id(&id
, timer
);
1464 if (p
== &snd_timer_list
)
1465 snd_timer_user_zero_id(&id
);
1468 snd_timer_user_zero_id(&id
);
1471 mutex_unlock(®ister_mutex
);
1472 if (copy_to_user(_tid
, &id
, sizeof(*_tid
)))
1477 static int snd_timer_user_ginfo(struct file
*file
,
1478 struct snd_timer_ginfo __user
*_ginfo
)
1480 struct snd_timer_ginfo
*ginfo
;
1481 struct snd_timer_id tid
;
1482 struct snd_timer
*t
;
1483 struct list_head
*p
;
1486 ginfo
= memdup_user(_ginfo
, sizeof(*ginfo
));
1488 return PTR_ERR(ginfo
);
1491 memset(ginfo
, 0, sizeof(*ginfo
));
1493 mutex_lock(®ister_mutex
);
1494 t
= snd_timer_find(&tid
);
1496 ginfo
->card
= t
->card
? t
->card
->number
: -1;
1497 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1498 ginfo
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1499 strlcpy(ginfo
->id
, t
->id
, sizeof(ginfo
->id
));
1500 strlcpy(ginfo
->name
, t
->name
, sizeof(ginfo
->name
));
1501 ginfo
->resolution
= t
->hw
.resolution
;
1502 if (t
->hw
.resolution_min
> 0) {
1503 ginfo
->resolution_min
= t
->hw
.resolution_min
;
1504 ginfo
->resolution_max
= t
->hw
.resolution_max
;
1506 list_for_each(p
, &t
->open_list_head
) {
1512 mutex_unlock(®ister_mutex
);
1513 if (err
>= 0 && copy_to_user(_ginfo
, ginfo
, sizeof(*ginfo
)))
1519 static int snd_timer_user_gparams(struct file
*file
,
1520 struct snd_timer_gparams __user
*_gparams
)
1522 struct snd_timer_gparams gparams
;
1523 struct snd_timer
*t
;
1526 if (copy_from_user(&gparams
, _gparams
, sizeof(gparams
)))
1528 mutex_lock(®ister_mutex
);
1529 t
= snd_timer_find(&gparams
.tid
);
1534 if (!list_empty(&t
->open_list_head
)) {
1538 if (!t
->hw
.set_period
) {
1542 err
= t
->hw
.set_period(t
, gparams
.period_num
, gparams
.period_den
);
1544 mutex_unlock(®ister_mutex
);
1548 static int snd_timer_user_gstatus(struct file
*file
,
1549 struct snd_timer_gstatus __user
*_gstatus
)
1551 struct snd_timer_gstatus gstatus
;
1552 struct snd_timer_id tid
;
1553 struct snd_timer
*t
;
1556 if (copy_from_user(&gstatus
, _gstatus
, sizeof(gstatus
)))
1559 memset(&gstatus
, 0, sizeof(gstatus
));
1561 mutex_lock(®ister_mutex
);
1562 t
= snd_timer_find(&tid
);
1564 if (t
->hw
.c_resolution
)
1565 gstatus
.resolution
= t
->hw
.c_resolution(t
);
1567 gstatus
.resolution
= t
->hw
.resolution
;
1568 if (t
->hw
.precise_resolution
) {
1569 t
->hw
.precise_resolution(t
, &gstatus
.resolution_num
,
1570 &gstatus
.resolution_den
);
1572 gstatus
.resolution_num
= gstatus
.resolution
;
1573 gstatus
.resolution_den
= 1000000000uL;
1578 mutex_unlock(®ister_mutex
);
1579 if (err
>= 0 && copy_to_user(_gstatus
, &gstatus
, sizeof(gstatus
)))
1584 static int snd_timer_user_tselect(struct file
*file
,
1585 struct snd_timer_select __user
*_tselect
)
1587 struct snd_timer_user
*tu
;
1588 struct snd_timer_select tselect
;
1592 tu
= file
->private_data
;
1594 snd_timer_close(tu
->timeri
);
1597 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
))) {
1601 sprintf(str
, "application %i", current
->pid
);
1602 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1603 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1604 err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
);
1608 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1614 tu
->tqueue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_tread
),
1616 if (tu
->tqueue
== NULL
)
1619 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1621 if (tu
->queue
== NULL
)
1626 snd_timer_close(tu
->timeri
);
1629 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1630 tu
->timeri
->callback
= tu
->tread
1631 ? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1632 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1633 tu
->timeri
->callback_data
= (void *)tu
;
1640 static int snd_timer_user_info(struct file
*file
,
1641 struct snd_timer_info __user
*_info
)
1643 struct snd_timer_user
*tu
;
1644 struct snd_timer_info
*info
;
1645 struct snd_timer
*t
;
1648 tu
= file
->private_data
;
1651 t
= tu
->timeri
->timer
;
1655 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1658 info
->card
= t
->card
? t
->card
->number
: -1;
1659 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1660 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1661 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1662 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1663 info
->resolution
= t
->hw
.resolution
;
1664 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1670 static int snd_timer_user_params(struct file
*file
,
1671 struct snd_timer_params __user
*_params
)
1673 struct snd_timer_user
*tu
;
1674 struct snd_timer_params params
;
1675 struct snd_timer
*t
;
1676 struct snd_timer_read
*tr
;
1677 struct snd_timer_tread
*ttr
;
1680 tu
= file
->private_data
;
1683 t
= tu
->timeri
->timer
;
1686 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
1688 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)) {
1691 if (params
.ticks
< 1) {
1696 /* Don't allow resolution less than 1ms */
1697 resolution
= snd_timer_resolution(tu
->timeri
);
1698 resolution
*= params
.ticks
;
1699 if (resolution
< 1000000) {
1704 if (params
.queue_size
> 0 &&
1705 (params
.queue_size
< 32 || params
.queue_size
> 1024)) {
1709 if (params
.filter
& ~((1<<SNDRV_TIMER_EVENT_RESOLUTION
)|
1710 (1<<SNDRV_TIMER_EVENT_TICK
)|
1711 (1<<SNDRV_TIMER_EVENT_START
)|
1712 (1<<SNDRV_TIMER_EVENT_STOP
)|
1713 (1<<SNDRV_TIMER_EVENT_CONTINUE
)|
1714 (1<<SNDRV_TIMER_EVENT_PAUSE
)|
1715 (1<<SNDRV_TIMER_EVENT_SUSPEND
)|
1716 (1<<SNDRV_TIMER_EVENT_RESUME
)|
1717 (1<<SNDRV_TIMER_EVENT_MSTART
)|
1718 (1<<SNDRV_TIMER_EVENT_MSTOP
)|
1719 (1<<SNDRV_TIMER_EVENT_MCONTINUE
)|
1720 (1<<SNDRV_TIMER_EVENT_MPAUSE
)|
1721 (1<<SNDRV_TIMER_EVENT_MSUSPEND
)|
1722 (1<<SNDRV_TIMER_EVENT_MRESUME
))) {
1726 snd_timer_stop(tu
->timeri
);
1727 spin_lock_irq(&t
->lock
);
1728 tu
->timeri
->flags
&= ~(SNDRV_TIMER_IFLG_AUTO
|
1729 SNDRV_TIMER_IFLG_EXCLUSIVE
|
1730 SNDRV_TIMER_IFLG_EARLY_EVENT
);
1731 if (params
.flags
& SNDRV_TIMER_PSFLG_AUTO
)
1732 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
1733 if (params
.flags
& SNDRV_TIMER_PSFLG_EXCLUSIVE
)
1734 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EXCLUSIVE
;
1735 if (params
.flags
& SNDRV_TIMER_PSFLG_EARLY_EVENT
)
1736 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EARLY_EVENT
;
1737 spin_unlock_irq(&t
->lock
);
1738 if (params
.queue_size
> 0 &&
1739 (unsigned int)tu
->queue_size
!= params
.queue_size
) {
1741 ttr
= kmalloc(params
.queue_size
* sizeof(*ttr
),
1745 tu
->queue_size
= params
.queue_size
;
1749 tr
= kmalloc(params
.queue_size
* sizeof(*tr
),
1753 tu
->queue_size
= params
.queue_size
;
1758 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1759 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1761 struct snd_timer_tread tread
;
1762 memset(&tread
, 0, sizeof(tread
));
1763 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1764 tread
.tstamp
.tv_sec
= 0;
1765 tread
.tstamp
.tv_nsec
= 0;
1767 snd_timer_user_append_to_tqueue(tu
, &tread
);
1769 struct snd_timer_read
*r
= &tu
->queue
[0];
1776 tu
->filter
= params
.filter
;
1777 tu
->ticks
= params
.ticks
;
1780 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1785 static int snd_timer_user_status(struct file
*file
,
1786 struct snd_timer_status __user
*_status
)
1788 struct snd_timer_user
*tu
;
1789 struct snd_timer_status status
;
1791 tu
= file
->private_data
;
1794 memset(&status
, 0, sizeof(status
));
1795 status
.tstamp
= tu
->tstamp
;
1796 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1797 status
.lost
= tu
->timeri
->lost
;
1798 status
.overrun
= tu
->overrun
;
1799 spin_lock_irq(&tu
->qlock
);
1800 status
.queue
= tu
->qused
;
1801 spin_unlock_irq(&tu
->qlock
);
1802 if (copy_to_user(_status
, &status
, sizeof(status
)))
1807 static int snd_timer_user_start(struct file
*file
)
1810 struct snd_timer_user
*tu
;
1812 tu
= file
->private_data
;
1815 snd_timer_stop(tu
->timeri
);
1816 tu
->timeri
->lost
= 0;
1817 tu
->last_resolution
= 0;
1818 return (err
= snd_timer_start(tu
->timeri
, tu
->ticks
)) < 0 ? err
: 0;
1821 static int snd_timer_user_stop(struct file
*file
)
1824 struct snd_timer_user
*tu
;
1826 tu
= file
->private_data
;
1829 return (err
= snd_timer_stop(tu
->timeri
)) < 0 ? err
: 0;
1832 static int snd_timer_user_continue(struct file
*file
)
1835 struct snd_timer_user
*tu
;
1837 tu
= file
->private_data
;
1840 tu
->timeri
->lost
= 0;
1841 return (err
= snd_timer_continue(tu
->timeri
)) < 0 ? err
: 0;
1844 static int snd_timer_user_pause(struct file
*file
)
1847 struct snd_timer_user
*tu
;
1849 tu
= file
->private_data
;
1852 return (err
= snd_timer_pause(tu
->timeri
)) < 0 ? err
: 0;
1856 SNDRV_TIMER_IOCTL_START_OLD
= _IO('T', 0x20),
1857 SNDRV_TIMER_IOCTL_STOP_OLD
= _IO('T', 0x21),
1858 SNDRV_TIMER_IOCTL_CONTINUE_OLD
= _IO('T', 0x22),
1859 SNDRV_TIMER_IOCTL_PAUSE_OLD
= _IO('T', 0x23),
1862 static long __snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1865 struct snd_timer_user
*tu
;
1866 void __user
*argp
= (void __user
*)arg
;
1867 int __user
*p
= argp
;
1869 tu
= file
->private_data
;
1871 case SNDRV_TIMER_IOCTL_PVERSION
:
1872 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1873 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1874 return snd_timer_user_next_device(argp
);
1875 case SNDRV_TIMER_IOCTL_TREAD
:
1879 if (tu
->timeri
) /* too late */
1881 if (get_user(xarg
, p
))
1883 tu
->tread
= xarg
? 1 : 0;
1886 case SNDRV_TIMER_IOCTL_GINFO
:
1887 return snd_timer_user_ginfo(file
, argp
);
1888 case SNDRV_TIMER_IOCTL_GPARAMS
:
1889 return snd_timer_user_gparams(file
, argp
);
1890 case SNDRV_TIMER_IOCTL_GSTATUS
:
1891 return snd_timer_user_gstatus(file
, argp
);
1892 case SNDRV_TIMER_IOCTL_SELECT
:
1893 return snd_timer_user_tselect(file
, argp
);
1894 case SNDRV_TIMER_IOCTL_INFO
:
1895 return snd_timer_user_info(file
, argp
);
1896 case SNDRV_TIMER_IOCTL_PARAMS
:
1897 return snd_timer_user_params(file
, argp
);
1898 case SNDRV_TIMER_IOCTL_STATUS
:
1899 return snd_timer_user_status(file
, argp
);
1900 case SNDRV_TIMER_IOCTL_START
:
1901 case SNDRV_TIMER_IOCTL_START_OLD
:
1902 return snd_timer_user_start(file
);
1903 case SNDRV_TIMER_IOCTL_STOP
:
1904 case SNDRV_TIMER_IOCTL_STOP_OLD
:
1905 return snd_timer_user_stop(file
);
1906 case SNDRV_TIMER_IOCTL_CONTINUE
:
1907 case SNDRV_TIMER_IOCTL_CONTINUE_OLD
:
1908 return snd_timer_user_continue(file
);
1909 case SNDRV_TIMER_IOCTL_PAUSE
:
1910 case SNDRV_TIMER_IOCTL_PAUSE_OLD
:
1911 return snd_timer_user_pause(file
);
1916 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1919 struct snd_timer_user
*tu
= file
->private_data
;
1922 mutex_lock(&tu
->ioctl_lock
);
1923 ret
= __snd_timer_user_ioctl(file
, cmd
, arg
);
1924 mutex_unlock(&tu
->ioctl_lock
);
1928 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
1930 struct snd_timer_user
*tu
;
1932 tu
= file
->private_data
;
1933 return fasync_helper(fd
, file
, on
, &tu
->fasync
);
1936 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
,
1937 size_t count
, loff_t
*offset
)
1939 struct snd_timer_user
*tu
;
1940 long result
= 0, unit
;
1944 tu
= file
->private_data
;
1945 unit
= tu
->tread
? sizeof(struct snd_timer_tread
) : sizeof(struct snd_timer_read
);
1946 mutex_lock(&tu
->ioctl_lock
);
1947 spin_lock_irq(&tu
->qlock
);
1948 while ((long)count
- result
>= unit
) {
1949 while (!tu
->qused
) {
1952 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1957 set_current_state(TASK_INTERRUPTIBLE
);
1958 init_waitqueue_entry(&wait
, current
);
1959 add_wait_queue(&tu
->qchange_sleep
, &wait
);
1961 spin_unlock_irq(&tu
->qlock
);
1962 mutex_unlock(&tu
->ioctl_lock
);
1964 mutex_lock(&tu
->ioctl_lock
);
1965 spin_lock_irq(&tu
->qlock
);
1967 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
1969 if (tu
->disconnected
) {
1973 if (signal_pending(current
)) {
1979 qhead
= tu
->qhead
++;
1980 tu
->qhead
%= tu
->queue_size
;
1982 spin_unlock_irq(&tu
->qlock
);
1985 if (copy_to_user(buffer
, &tu
->tqueue
[qhead
],
1986 sizeof(struct snd_timer_tread
)))
1989 if (copy_to_user(buffer
, &tu
->queue
[qhead
],
1990 sizeof(struct snd_timer_read
)))
1994 spin_lock_irq(&tu
->qlock
);
2001 spin_unlock_irq(&tu
->qlock
);
2002 mutex_unlock(&tu
->ioctl_lock
);
2003 return result
> 0 ? result
: err
;
2006 static unsigned int snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
2009 struct snd_timer_user
*tu
;
2011 tu
= file
->private_data
;
2013 poll_wait(file
, &tu
->qchange_sleep
, wait
);
2017 mask
|= POLLIN
| POLLRDNORM
;
2018 if (tu
->disconnected
)
2024 #ifdef CONFIG_COMPAT
2025 #include "timer_compat.c"
2027 #define snd_timer_user_ioctl_compat NULL
2030 static const struct file_operations snd_timer_f_ops
=
2032 .owner
= THIS_MODULE
,
2033 .read
= snd_timer_user_read
,
2034 .open
= snd_timer_user_open
,
2035 .release
= snd_timer_user_release
,
2036 .llseek
= no_llseek
,
2037 .poll
= snd_timer_user_poll
,
2038 .unlocked_ioctl
= snd_timer_user_ioctl
,
2039 .compat_ioctl
= snd_timer_user_ioctl_compat
,
2040 .fasync
= snd_timer_user_fasync
,
2043 /* unregister the system timer */
2044 static void snd_timer_free_all(void)
2046 struct snd_timer
*timer
, *n
;
2048 list_for_each_entry_safe(timer
, n
, &snd_timer_list
, device_list
)
2049 snd_timer_free(timer
);
2052 static struct device timer_dev
;
2058 static int __init
alsa_timer_init(void)
2062 snd_device_initialize(&timer_dev
, NULL
);
2063 dev_set_name(&timer_dev
, "timer");
2065 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2066 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1,
2070 err
= snd_timer_register_system();
2072 pr_err("ALSA: unable to register system timer (%i)\n", err
);
2073 put_device(&timer_dev
);
2077 err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0,
2078 &snd_timer_f_ops
, NULL
, &timer_dev
);
2080 pr_err("ALSA: unable to register timer device (%i)\n", err
);
2081 snd_timer_free_all();
2082 put_device(&timer_dev
);
2086 snd_timer_proc_init();
2090 static void __exit
alsa_timer_exit(void)
2092 snd_unregister_device(&timer_dev
);
2093 snd_timer_free_all();
2094 put_device(&timer_dev
);
2095 snd_timer_proc_done();
2096 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2097 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
2101 module_init(alsa_timer_init
)
2102 module_exit(alsa_timer_exit
)
2104 EXPORT_SYMBOL(snd_timer_open
);
2105 EXPORT_SYMBOL(snd_timer_close
);
2106 EXPORT_SYMBOL(snd_timer_resolution
);
2107 EXPORT_SYMBOL(snd_timer_start
);
2108 EXPORT_SYMBOL(snd_timer_stop
);
2109 EXPORT_SYMBOL(snd_timer_continue
);
2110 EXPORT_SYMBOL(snd_timer_pause
);
2111 EXPORT_SYMBOL(snd_timer_new
);
2112 EXPORT_SYMBOL(snd_timer_notify
);
2113 EXPORT_SYMBOL(snd_timer_global_new
);
2114 EXPORT_SYMBOL(snd_timer_global_free
);
2115 EXPORT_SYMBOL(snd_timer_global_register
);
2116 EXPORT_SYMBOL(snd_timer_interrupt
);