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 defined(CONFIG_SND_HRTIMER) || defined(CONFIG_SND_HRTIMER_MODULE)
39 #define DEFAULT_TIMER_LIMIT 4
40 #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
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 snd_printd("invalid slave class %i\n", tid
->dev_sclass
);
248 mutex_lock(®ister_mutex
);
249 timeri
= snd_timer_instance_new(owner
, NULL
);
251 mutex_unlock(®ister_mutex
);
254 timeri
->slave_class
= tid
->dev_sclass
;
255 timeri
->slave_id
= tid
->device
;
256 timeri
->flags
|= SNDRV_TIMER_IFLG_SLAVE
;
257 list_add_tail(&timeri
->open_list
, &snd_timer_slave_list
);
258 snd_timer_check_slave(timeri
);
259 mutex_unlock(®ister_mutex
);
264 /* open a master instance */
265 mutex_lock(®ister_mutex
);
266 timer
= snd_timer_find(tid
);
267 #ifdef CONFIG_MODULES
269 mutex_unlock(®ister_mutex
);
270 snd_timer_request(tid
);
271 mutex_lock(®ister_mutex
);
272 timer
= snd_timer_find(tid
);
276 mutex_unlock(®ister_mutex
);
279 if (!list_empty(&timer
->open_list_head
)) {
280 timeri
= list_entry(timer
->open_list_head
.next
,
281 struct snd_timer_instance
, open_list
);
282 if (timeri
->flags
& SNDRV_TIMER_IFLG_EXCLUSIVE
) {
283 mutex_unlock(®ister_mutex
);
287 timeri
= snd_timer_instance_new(owner
, timer
);
289 mutex_unlock(®ister_mutex
);
292 timeri
->slave_class
= tid
->dev_sclass
;
293 timeri
->slave_id
= slave_id
;
294 if (list_empty(&timer
->open_list_head
) && timer
->hw
.open
)
295 timer
->hw
.open(timer
);
296 list_add_tail(&timeri
->open_list
, &timer
->open_list_head
);
297 snd_timer_check_master(timeri
);
298 mutex_unlock(®ister_mutex
);
303 static int _snd_timer_stop(struct snd_timer_instance
*timeri
,
304 int keep_flag
, int event
);
307 * close a timer instance
309 int snd_timer_close(struct snd_timer_instance
*timeri
)
311 struct snd_timer
*timer
= NULL
;
312 struct snd_timer_instance
*slave
, *tmp
;
314 if (snd_BUG_ON(!timeri
))
317 /* force to stop the timer */
318 snd_timer_stop(timeri
);
320 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
321 /* wait, until the active callback is finished */
322 spin_lock_irq(&slave_active_lock
);
323 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
324 spin_unlock_irq(&slave_active_lock
);
326 spin_lock_irq(&slave_active_lock
);
328 spin_unlock_irq(&slave_active_lock
);
329 mutex_lock(®ister_mutex
);
330 list_del(&timeri
->open_list
);
331 mutex_unlock(®ister_mutex
);
333 timer
= timeri
->timer
;
334 if (snd_BUG_ON(!timer
))
336 /* wait, until the active callback is finished */
337 spin_lock_irq(&timer
->lock
);
338 while (timeri
->flags
& SNDRV_TIMER_IFLG_CALLBACK
) {
339 spin_unlock_irq(&timer
->lock
);
341 spin_lock_irq(&timer
->lock
);
343 spin_unlock_irq(&timer
->lock
);
344 mutex_lock(®ister_mutex
);
345 list_del(&timeri
->open_list
);
346 if (timer
&& list_empty(&timer
->open_list_head
) &&
348 timer
->hw
.close(timer
);
349 /* remove slave links */
350 spin_lock_irq(&slave_active_lock
);
351 spin_lock(&timer
->lock
);
352 list_for_each_entry_safe(slave
, tmp
, &timeri
->slave_list_head
,
354 list_move_tail(&slave
->open_list
, &snd_timer_slave_list
);
355 slave
->master
= NULL
;
357 list_del_init(&slave
->ack_list
);
358 list_del_init(&slave
->active_list
);
360 spin_unlock(&timer
->lock
);
361 spin_unlock_irq(&slave_active_lock
);
362 mutex_unlock(®ister_mutex
);
365 if (timeri
->private_free
)
366 timeri
->private_free(timeri
);
367 kfree(timeri
->owner
);
370 module_put(timer
->module
);
374 unsigned long snd_timer_resolution(struct snd_timer_instance
*timeri
)
376 struct snd_timer
* timer
;
380 if ((timer
= timeri
->timer
) != NULL
) {
381 if (timer
->hw
.c_resolution
)
382 return timer
->hw
.c_resolution(timer
);
383 return timer
->hw
.resolution
;
388 static void snd_timer_notify1(struct snd_timer_instance
*ti
, int event
)
390 struct snd_timer
*timer
;
392 unsigned long resolution
= 0;
393 struct snd_timer_instance
*ts
;
394 struct timespec tstamp
;
396 if (timer_tstamp_monotonic
)
397 do_posix_clock_monotonic_gettime(&tstamp
);
399 getnstimeofday(&tstamp
);
400 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_START
||
401 event
> SNDRV_TIMER_EVENT_PAUSE
))
403 if (event
== SNDRV_TIMER_EVENT_START
||
404 event
== SNDRV_TIMER_EVENT_CONTINUE
)
405 resolution
= snd_timer_resolution(ti
);
407 ti
->ccallback(ti
, event
, &tstamp
, resolution
);
408 if (ti
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
413 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
415 spin_lock_irqsave(&timer
->lock
, flags
);
416 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
418 ts
->ccallback(ti
, event
+ 100, &tstamp
, resolution
);
419 spin_unlock_irqrestore(&timer
->lock
, flags
);
422 static int snd_timer_start1(struct snd_timer
*timer
, struct snd_timer_instance
*timeri
,
423 unsigned long sticks
)
425 list_move_tail(&timeri
->active_list
, &timer
->active_list_head
);
426 if (timer
->running
) {
427 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
429 timer
->flags
|= SNDRV_TIMER_FLG_RESCHED
;
430 timeri
->flags
|= SNDRV_TIMER_IFLG_START
;
431 return 1; /* delayed start */
433 timer
->sticks
= sticks
;
434 timer
->hw
.start(timer
);
437 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
442 static int snd_timer_start_slave(struct snd_timer_instance
*timeri
)
446 spin_lock_irqsave(&slave_active_lock
, flags
);
447 timeri
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
448 if (timeri
->master
&& timeri
->timer
) {
449 spin_lock(&timeri
->timer
->lock
);
450 list_add_tail(&timeri
->active_list
,
451 &timeri
->master
->slave_active_head
);
452 spin_unlock(&timeri
->timer
->lock
);
454 spin_unlock_irqrestore(&slave_active_lock
, flags
);
455 return 1; /* delayed start */
459 * start the timer instance
461 int snd_timer_start(struct snd_timer_instance
*timeri
, unsigned int ticks
)
463 struct snd_timer
*timer
;
464 int result
= -EINVAL
;
467 if (timeri
== NULL
|| ticks
< 1)
469 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
470 result
= snd_timer_start_slave(timeri
);
471 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
474 timer
= timeri
->timer
;
477 spin_lock_irqsave(&timer
->lock
, flags
);
478 timeri
->ticks
= timeri
->cticks
= ticks
;
480 result
= snd_timer_start1(timer
, timeri
, ticks
);
481 spin_unlock_irqrestore(&timer
->lock
, flags
);
482 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_START
);
486 static int _snd_timer_stop(struct snd_timer_instance
* timeri
,
487 int keep_flag
, int event
)
489 struct snd_timer
*timer
;
492 if (snd_BUG_ON(!timeri
))
495 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
) {
497 spin_lock_irqsave(&slave_active_lock
, flags
);
498 timeri
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
499 list_del_init(&timeri
->ack_list
);
500 list_del_init(&timeri
->active_list
);
501 spin_unlock_irqrestore(&slave_active_lock
, flags
);
505 timer
= timeri
->timer
;
508 spin_lock_irqsave(&timer
->lock
, flags
);
509 list_del_init(&timeri
->ack_list
);
510 list_del_init(&timeri
->active_list
);
511 if ((timeri
->flags
& SNDRV_TIMER_IFLG_RUNNING
) &&
512 !(--timer
->running
)) {
513 timer
->hw
.stop(timer
);
514 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
) {
515 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
516 snd_timer_reschedule(timer
, 0);
517 if (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
) {
518 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
519 timer
->hw
.start(timer
);
525 ~(SNDRV_TIMER_IFLG_RUNNING
| SNDRV_TIMER_IFLG_START
);
526 spin_unlock_irqrestore(&timer
->lock
, flags
);
528 if (event
!= SNDRV_TIMER_EVENT_RESOLUTION
)
529 snd_timer_notify1(timeri
, event
);
534 * stop the timer instance.
536 * do not call this from the timer callback!
538 int snd_timer_stop(struct snd_timer_instance
*timeri
)
540 struct snd_timer
*timer
;
544 err
= _snd_timer_stop(timeri
, 0, SNDRV_TIMER_EVENT_STOP
);
547 timer
= timeri
->timer
;
550 spin_lock_irqsave(&timer
->lock
, flags
);
551 timeri
->cticks
= timeri
->ticks
;
553 spin_unlock_irqrestore(&timer
->lock
, flags
);
558 * start again.. the tick is kept.
560 int snd_timer_continue(struct snd_timer_instance
*timeri
)
562 struct snd_timer
*timer
;
563 int result
= -EINVAL
;
568 if (timeri
->flags
& SNDRV_TIMER_IFLG_SLAVE
)
569 return snd_timer_start_slave(timeri
);
570 timer
= timeri
->timer
;
573 spin_lock_irqsave(&timer
->lock
, flags
);
577 result
= snd_timer_start1(timer
, timeri
, timer
->sticks
);
578 spin_unlock_irqrestore(&timer
->lock
, flags
);
579 snd_timer_notify1(timeri
, SNDRV_TIMER_EVENT_CONTINUE
);
584 * pause.. remember the ticks left
586 int snd_timer_pause(struct snd_timer_instance
* timeri
)
588 return _snd_timer_stop(timeri
, 0, SNDRV_TIMER_EVENT_PAUSE
);
592 * reschedule the timer
594 * start pending instances and check the scheduling ticks.
595 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
597 static void snd_timer_reschedule(struct snd_timer
* timer
, unsigned long ticks_left
)
599 struct snd_timer_instance
*ti
;
600 unsigned long ticks
= ~0UL;
602 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
603 if (ti
->flags
& SNDRV_TIMER_IFLG_START
) {
604 ti
->flags
&= ~SNDRV_TIMER_IFLG_START
;
605 ti
->flags
|= SNDRV_TIMER_IFLG_RUNNING
;
608 if (ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
) {
609 if (ticks
> ti
->cticks
)
614 timer
->flags
&= ~SNDRV_TIMER_FLG_RESCHED
;
617 if (ticks
> timer
->hw
.ticks
)
618 ticks
= timer
->hw
.ticks
;
619 if (ticks_left
!= ticks
)
620 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
621 timer
->sticks
= ticks
;
628 static void snd_timer_tasklet(unsigned long arg
)
630 struct snd_timer
*timer
= (struct snd_timer
*) arg
;
631 struct snd_timer_instance
*ti
;
633 unsigned long resolution
, ticks
;
636 spin_lock_irqsave(&timer
->lock
, flags
);
637 /* now process all callbacks */
638 while (!list_empty(&timer
->sack_list_head
)) {
639 p
= timer
->sack_list_head
.next
; /* get first item */
640 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
642 /* remove from ack_list and make empty */
647 resolution
= ti
->resolution
;
649 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
650 spin_unlock(&timer
->lock
);
652 ti
->callback(ti
, resolution
, ticks
);
653 spin_lock(&timer
->lock
);
654 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
656 spin_unlock_irqrestore(&timer
->lock
, flags
);
662 * ticks_left is usually equal to timer->sticks.
665 void snd_timer_interrupt(struct snd_timer
* timer
, unsigned long ticks_left
)
667 struct snd_timer_instance
*ti
, *ts
, *tmp
;
668 unsigned long resolution
, ticks
;
669 struct list_head
*p
, *ack_list_head
;
676 spin_lock_irqsave(&timer
->lock
, flags
);
678 /* remember the current resolution */
679 if (timer
->hw
.c_resolution
)
680 resolution
= timer
->hw
.c_resolution(timer
);
682 resolution
= timer
->hw
.resolution
;
684 /* loop for all active instances
685 * Here we cannot use list_for_each_entry because the active_list of a
686 * processed instance is relinked to done_list_head before the callback
689 list_for_each_entry_safe(ti
, tmp
, &timer
->active_list_head
,
691 if (!(ti
->flags
& SNDRV_TIMER_IFLG_RUNNING
))
693 ti
->pticks
+= ticks_left
;
694 ti
->resolution
= resolution
;
695 if (ti
->cticks
< ticks_left
)
698 ti
->cticks
-= ticks_left
;
699 if (ti
->cticks
) /* not expired */
701 if (ti
->flags
& SNDRV_TIMER_IFLG_AUTO
) {
702 ti
->cticks
= ti
->ticks
;
704 ti
->flags
&= ~SNDRV_TIMER_IFLG_RUNNING
;
705 if (--timer
->running
)
706 list_del_init(&ti
->active_list
);
708 if ((timer
->hw
.flags
& SNDRV_TIMER_HW_TASKLET
) ||
709 (ti
->flags
& SNDRV_TIMER_IFLG_FAST
))
710 ack_list_head
= &timer
->ack_list_head
;
712 ack_list_head
= &timer
->sack_list_head
;
713 if (list_empty(&ti
->ack_list
))
714 list_add_tail(&ti
->ack_list
, ack_list_head
);
715 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
) {
716 ts
->pticks
= ti
->pticks
;
717 ts
->resolution
= resolution
;
718 if (list_empty(&ts
->ack_list
))
719 list_add_tail(&ts
->ack_list
, ack_list_head
);
722 if (timer
->flags
& SNDRV_TIMER_FLG_RESCHED
)
723 snd_timer_reschedule(timer
, timer
->sticks
);
724 if (timer
->running
) {
725 if (timer
->hw
.flags
& SNDRV_TIMER_HW_STOP
) {
726 timer
->hw
.stop(timer
);
727 timer
->flags
|= SNDRV_TIMER_FLG_CHANGE
;
729 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_AUTO
) ||
730 (timer
->flags
& SNDRV_TIMER_FLG_CHANGE
)) {
732 timer
->flags
&= ~SNDRV_TIMER_FLG_CHANGE
;
733 timer
->hw
.start(timer
);
736 timer
->hw
.stop(timer
);
739 /* now process all fast callbacks */
740 while (!list_empty(&timer
->ack_list_head
)) {
741 p
= timer
->ack_list_head
.next
; /* get first item */
742 ti
= list_entry(p
, struct snd_timer_instance
, ack_list
);
744 /* remove from ack_list and make empty */
750 ti
->flags
|= SNDRV_TIMER_IFLG_CALLBACK
;
751 spin_unlock(&timer
->lock
);
753 ti
->callback(ti
, resolution
, ticks
);
754 spin_lock(&timer
->lock
);
755 ti
->flags
&= ~SNDRV_TIMER_IFLG_CALLBACK
;
758 /* do we have any slow callbacks? */
759 use_tasklet
= !list_empty(&timer
->sack_list_head
);
760 spin_unlock_irqrestore(&timer
->lock
, flags
);
763 tasklet_schedule(&timer
->task_queue
);
770 int snd_timer_new(struct snd_card
*card
, char *id
, struct snd_timer_id
*tid
,
771 struct snd_timer
**rtimer
)
773 struct snd_timer
*timer
;
775 static struct snd_device_ops ops
= {
776 .dev_free
= snd_timer_dev_free
,
777 .dev_register
= snd_timer_dev_register
,
778 .dev_disconnect
= snd_timer_dev_disconnect
,
781 if (snd_BUG_ON(!tid
))
785 timer
= kzalloc(sizeof(*timer
), GFP_KERNEL
);
787 snd_printk(KERN_ERR
"timer: cannot allocate\n");
790 timer
->tmr_class
= tid
->dev_class
;
792 timer
->tmr_device
= tid
->device
;
793 timer
->tmr_subdevice
= tid
->subdevice
;
795 strlcpy(timer
->id
, id
, sizeof(timer
->id
));
796 INIT_LIST_HEAD(&timer
->device_list
);
797 INIT_LIST_HEAD(&timer
->open_list_head
);
798 INIT_LIST_HEAD(&timer
->active_list_head
);
799 INIT_LIST_HEAD(&timer
->ack_list_head
);
800 INIT_LIST_HEAD(&timer
->sack_list_head
);
801 spin_lock_init(&timer
->lock
);
802 tasklet_init(&timer
->task_queue
, snd_timer_tasklet
,
803 (unsigned long)timer
);
805 timer
->module
= card
->module
;
806 err
= snd_device_new(card
, SNDRV_DEV_TIMER
, timer
, &ops
);
808 snd_timer_free(timer
);
817 static int snd_timer_free(struct snd_timer
*timer
)
822 mutex_lock(®ister_mutex
);
823 if (! list_empty(&timer
->open_list_head
)) {
824 struct list_head
*p
, *n
;
825 struct snd_timer_instance
*ti
;
826 snd_printk(KERN_WARNING
"timer %p is busy?\n", timer
);
827 list_for_each_safe(p
, n
, &timer
->open_list_head
) {
829 ti
= list_entry(p
, struct snd_timer_instance
, open_list
);
833 list_del(&timer
->device_list
);
834 mutex_unlock(®ister_mutex
);
836 if (timer
->private_free
)
837 timer
->private_free(timer
);
842 static int snd_timer_dev_free(struct snd_device
*device
)
844 struct snd_timer
*timer
= device
->device_data
;
845 return snd_timer_free(timer
);
848 static int snd_timer_dev_register(struct snd_device
*dev
)
850 struct snd_timer
*timer
= dev
->device_data
;
851 struct snd_timer
*timer1
;
853 if (snd_BUG_ON(!timer
|| !timer
->hw
.start
|| !timer
->hw
.stop
))
855 if (!(timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) &&
856 !timer
->hw
.resolution
&& timer
->hw
.c_resolution
== NULL
)
859 mutex_lock(®ister_mutex
);
860 list_for_each_entry(timer1
, &snd_timer_list
, device_list
) {
861 if (timer1
->tmr_class
> timer
->tmr_class
)
863 if (timer1
->tmr_class
< timer
->tmr_class
)
865 if (timer1
->card
&& timer
->card
) {
866 if (timer1
->card
->number
> timer
->card
->number
)
868 if (timer1
->card
->number
< timer
->card
->number
)
871 if (timer1
->tmr_device
> timer
->tmr_device
)
873 if (timer1
->tmr_device
< timer
->tmr_device
)
875 if (timer1
->tmr_subdevice
> timer
->tmr_subdevice
)
877 if (timer1
->tmr_subdevice
< timer
->tmr_subdevice
)
880 mutex_unlock(®ister_mutex
);
883 list_add_tail(&timer
->device_list
, &timer1
->device_list
);
884 mutex_unlock(®ister_mutex
);
888 static int snd_timer_dev_disconnect(struct snd_device
*device
)
890 struct snd_timer
*timer
= device
->device_data
;
891 mutex_lock(®ister_mutex
);
892 list_del_init(&timer
->device_list
);
893 mutex_unlock(®ister_mutex
);
897 void snd_timer_notify(struct snd_timer
*timer
, int event
, struct timespec
*tstamp
)
900 unsigned long resolution
= 0;
901 struct snd_timer_instance
*ti
, *ts
;
903 if (! (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
))
905 if (snd_BUG_ON(event
< SNDRV_TIMER_EVENT_MSTART
||
906 event
> SNDRV_TIMER_EVENT_MRESUME
))
908 spin_lock_irqsave(&timer
->lock
, flags
);
909 if (event
== SNDRV_TIMER_EVENT_MSTART
||
910 event
== SNDRV_TIMER_EVENT_MCONTINUE
||
911 event
== SNDRV_TIMER_EVENT_MRESUME
) {
912 if (timer
->hw
.c_resolution
)
913 resolution
= timer
->hw
.c_resolution(timer
);
915 resolution
= timer
->hw
.resolution
;
917 list_for_each_entry(ti
, &timer
->active_list_head
, active_list
) {
919 ti
->ccallback(ti
, event
, tstamp
, resolution
);
920 list_for_each_entry(ts
, &ti
->slave_active_head
, active_list
)
922 ts
->ccallback(ts
, event
, tstamp
, resolution
);
924 spin_unlock_irqrestore(&timer
->lock
, flags
);
928 * exported functions for global timers
930 int snd_timer_global_new(char *id
, int device
, struct snd_timer
**rtimer
)
932 struct snd_timer_id tid
;
934 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
935 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
939 return snd_timer_new(NULL
, id
, &tid
, rtimer
);
942 int snd_timer_global_free(struct snd_timer
*timer
)
944 return snd_timer_free(timer
);
947 int snd_timer_global_register(struct snd_timer
*timer
)
949 struct snd_device dev
;
951 memset(&dev
, 0, sizeof(dev
));
952 dev
.device_data
= timer
;
953 return snd_timer_dev_register(&dev
);
960 struct snd_timer_system_private
{
961 struct timer_list tlist
;
962 unsigned long last_expires
;
963 unsigned long last_jiffies
;
964 unsigned long correction
;
967 static void snd_timer_s_function(unsigned long data
)
969 struct snd_timer
*timer
= (struct snd_timer
*)data
;
970 struct snd_timer_system_private
*priv
= timer
->private_data
;
971 unsigned long jiff
= jiffies
;
972 if (time_after(jiff
, priv
->last_expires
))
973 priv
->correction
+= (long)jiff
- (long)priv
->last_expires
;
974 snd_timer_interrupt(timer
, (long)jiff
- (long)priv
->last_jiffies
);
977 static int snd_timer_s_start(struct snd_timer
* timer
)
979 struct snd_timer_system_private
*priv
;
982 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
983 njiff
= (priv
->last_jiffies
= jiffies
);
984 if (priv
->correction
> timer
->sticks
- 1) {
985 priv
->correction
-= timer
->sticks
- 1;
988 njiff
+= timer
->sticks
- priv
->correction
;
989 priv
->correction
= 0;
991 priv
->last_expires
= priv
->tlist
.expires
= njiff
;
992 add_timer(&priv
->tlist
);
996 static int snd_timer_s_stop(struct snd_timer
* timer
)
998 struct snd_timer_system_private
*priv
;
1001 priv
= (struct snd_timer_system_private
*) timer
->private_data
;
1002 del_timer(&priv
->tlist
);
1004 if (time_before(jiff
, priv
->last_expires
))
1005 timer
->sticks
= priv
->last_expires
- jiff
;
1008 priv
->correction
= 0;
1012 static struct snd_timer_hardware snd_timer_system
=
1014 .flags
= SNDRV_TIMER_HW_FIRST
| SNDRV_TIMER_HW_TASKLET
,
1015 .resolution
= 1000000000L / HZ
,
1017 .start
= snd_timer_s_start
,
1018 .stop
= snd_timer_s_stop
1021 static void snd_timer_free_system(struct snd_timer
*timer
)
1023 kfree(timer
->private_data
);
1026 static int snd_timer_register_system(void)
1028 struct snd_timer
*timer
;
1029 struct snd_timer_system_private
*priv
;
1032 err
= snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM
, &timer
);
1035 strcpy(timer
->name
, "system timer");
1036 timer
->hw
= snd_timer_system
;
1037 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1039 snd_timer_free(timer
);
1042 init_timer(&priv
->tlist
);
1043 priv
->tlist
.function
= snd_timer_s_function
;
1044 priv
->tlist
.data
= (unsigned long) timer
;
1045 timer
->private_data
= priv
;
1046 timer
->private_free
= snd_timer_free_system
;
1047 return snd_timer_global_register(timer
);
1050 #ifdef CONFIG_PROC_FS
1055 static void snd_timer_proc_read(struct snd_info_entry
*entry
,
1056 struct snd_info_buffer
*buffer
)
1058 struct snd_timer
*timer
;
1059 struct snd_timer_instance
*ti
;
1061 mutex_lock(®ister_mutex
);
1062 list_for_each_entry(timer
, &snd_timer_list
, device_list
) {
1063 switch (timer
->tmr_class
) {
1064 case SNDRV_TIMER_CLASS_GLOBAL
:
1065 snd_iprintf(buffer
, "G%i: ", timer
->tmr_device
);
1067 case SNDRV_TIMER_CLASS_CARD
:
1068 snd_iprintf(buffer
, "C%i-%i: ",
1069 timer
->card
->number
, timer
->tmr_device
);
1071 case SNDRV_TIMER_CLASS_PCM
:
1072 snd_iprintf(buffer
, "P%i-%i-%i: ", timer
->card
->number
,
1073 timer
->tmr_device
, timer
->tmr_subdevice
);
1076 snd_iprintf(buffer
, "?%i-%i-%i-%i: ", timer
->tmr_class
,
1077 timer
->card
? timer
->card
->number
: -1,
1078 timer
->tmr_device
, timer
->tmr_subdevice
);
1080 snd_iprintf(buffer
, "%s :", timer
->name
);
1081 if (timer
->hw
.resolution
)
1082 snd_iprintf(buffer
, " %lu.%03luus (%lu ticks)",
1083 timer
->hw
.resolution
/ 1000,
1084 timer
->hw
.resolution
% 1000,
1086 if (timer
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1087 snd_iprintf(buffer
, " SLAVE");
1088 snd_iprintf(buffer
, "\n");
1089 list_for_each_entry(ti
, &timer
->open_list_head
, open_list
)
1090 snd_iprintf(buffer
, " Client %s : %s\n",
1091 ti
->owner
? ti
->owner
: "unknown",
1092 ti
->flags
& (SNDRV_TIMER_IFLG_START
|
1093 SNDRV_TIMER_IFLG_RUNNING
)
1094 ? "running" : "stopped");
1096 mutex_unlock(®ister_mutex
);
1099 static struct snd_info_entry
*snd_timer_proc_entry
;
1101 static void __init
snd_timer_proc_init(void)
1103 struct snd_info_entry
*entry
;
1105 entry
= snd_info_create_module_entry(THIS_MODULE
, "timers", NULL
);
1106 if (entry
!= NULL
) {
1107 entry
->c
.text
.read
= snd_timer_proc_read
;
1108 if (snd_info_register(entry
) < 0) {
1109 snd_info_free_entry(entry
);
1113 snd_timer_proc_entry
= entry
;
1116 static void __exit
snd_timer_proc_done(void)
1118 snd_info_free_entry(snd_timer_proc_entry
);
1120 #else /* !CONFIG_PROC_FS */
1121 #define snd_timer_proc_init()
1122 #define snd_timer_proc_done()
1126 * USER SPACE interface
1129 static void snd_timer_user_interrupt(struct snd_timer_instance
*timeri
,
1130 unsigned long resolution
,
1131 unsigned long ticks
)
1133 struct snd_timer_user
*tu
= timeri
->callback_data
;
1134 struct snd_timer_read
*r
;
1137 spin_lock(&tu
->qlock
);
1138 if (tu
->qused
> 0) {
1139 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1140 r
= &tu
->queue
[prev
];
1141 if (r
->resolution
== resolution
) {
1146 if (tu
->qused
>= tu
->queue_size
) {
1149 r
= &tu
->queue
[tu
->qtail
++];
1150 tu
->qtail
%= tu
->queue_size
;
1151 r
->resolution
= resolution
;
1156 spin_unlock(&tu
->qlock
);
1157 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1158 wake_up(&tu
->qchange_sleep
);
1161 static void snd_timer_user_append_to_tqueue(struct snd_timer_user
*tu
,
1162 struct snd_timer_tread
*tread
)
1164 if (tu
->qused
>= tu
->queue_size
) {
1167 memcpy(&tu
->tqueue
[tu
->qtail
++], tread
, sizeof(*tread
));
1168 tu
->qtail
%= tu
->queue_size
;
1173 static void snd_timer_user_ccallback(struct snd_timer_instance
*timeri
,
1175 struct timespec
*tstamp
,
1176 unsigned long resolution
)
1178 struct snd_timer_user
*tu
= timeri
->callback_data
;
1179 struct snd_timer_tread r1
;
1180 unsigned long flags
;
1182 if (event
>= SNDRV_TIMER_EVENT_START
&&
1183 event
<= SNDRV_TIMER_EVENT_PAUSE
)
1184 tu
->tstamp
= *tstamp
;
1185 if ((tu
->filter
& (1 << event
)) == 0 || !tu
->tread
)
1188 r1
.tstamp
= *tstamp
;
1189 r1
.val
= resolution
;
1190 spin_lock_irqsave(&tu
->qlock
, flags
);
1191 snd_timer_user_append_to_tqueue(tu
, &r1
);
1192 spin_unlock_irqrestore(&tu
->qlock
, flags
);
1193 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1194 wake_up(&tu
->qchange_sleep
);
1197 static void snd_timer_user_tinterrupt(struct snd_timer_instance
*timeri
,
1198 unsigned long resolution
,
1199 unsigned long ticks
)
1201 struct snd_timer_user
*tu
= timeri
->callback_data
;
1202 struct snd_timer_tread
*r
, r1
;
1203 struct timespec tstamp
;
1204 int prev
, append
= 0;
1206 memset(&tstamp
, 0, sizeof(tstamp
));
1207 spin_lock(&tu
->qlock
);
1208 if ((tu
->filter
& ((1 << SNDRV_TIMER_EVENT_RESOLUTION
) |
1209 (1 << SNDRV_TIMER_EVENT_TICK
))) == 0) {
1210 spin_unlock(&tu
->qlock
);
1213 if (tu
->last_resolution
!= resolution
|| ticks
> 0) {
1214 if (timer_tstamp_monotonic
)
1215 do_posix_clock_monotonic_gettime(&tstamp
);
1217 getnstimeofday(&tstamp
);
1219 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_RESOLUTION
)) &&
1220 tu
->last_resolution
!= resolution
) {
1221 r1
.event
= SNDRV_TIMER_EVENT_RESOLUTION
;
1223 r1
.val
= resolution
;
1224 snd_timer_user_append_to_tqueue(tu
, &r1
);
1225 tu
->last_resolution
= resolution
;
1228 if ((tu
->filter
& (1 << SNDRV_TIMER_EVENT_TICK
)) == 0)
1232 if (tu
->qused
> 0) {
1233 prev
= tu
->qtail
== 0 ? tu
->queue_size
- 1 : tu
->qtail
- 1;
1234 r
= &tu
->tqueue
[prev
];
1235 if (r
->event
== SNDRV_TIMER_EVENT_TICK
) {
1242 r1
.event
= SNDRV_TIMER_EVENT_TICK
;
1245 snd_timer_user_append_to_tqueue(tu
, &r1
);
1248 spin_unlock(&tu
->qlock
);
1251 kill_fasync(&tu
->fasync
, SIGIO
, POLL_IN
);
1252 wake_up(&tu
->qchange_sleep
);
1255 static int snd_timer_user_open(struct inode
*inode
, struct file
*file
)
1257 struct snd_timer_user
*tu
;
1260 err
= nonseekable_open(inode
, file
);
1264 tu
= kzalloc(sizeof(*tu
), GFP_KERNEL
);
1267 spin_lock_init(&tu
->qlock
);
1268 init_waitqueue_head(&tu
->qchange_sleep
);
1269 mutex_init(&tu
->ioctl_lock
);
1271 tu
->queue_size
= 128;
1272 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1274 if (tu
->queue
== NULL
) {
1278 file
->private_data
= tu
;
1282 static int snd_timer_user_release(struct inode
*inode
, struct file
*file
)
1284 struct snd_timer_user
*tu
;
1286 if (file
->private_data
) {
1287 tu
= file
->private_data
;
1288 file
->private_data
= NULL
;
1289 mutex_lock(&tu
->ioctl_lock
);
1291 snd_timer_close(tu
->timeri
);
1292 mutex_unlock(&tu
->ioctl_lock
);
1300 static void snd_timer_user_zero_id(struct snd_timer_id
*id
)
1302 id
->dev_class
= SNDRV_TIMER_CLASS_NONE
;
1303 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1309 static void snd_timer_user_copy_id(struct snd_timer_id
*id
, struct snd_timer
*timer
)
1311 id
->dev_class
= timer
->tmr_class
;
1312 id
->dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
1313 id
->card
= timer
->card
? timer
->card
->number
: -1;
1314 id
->device
= timer
->tmr_device
;
1315 id
->subdevice
= timer
->tmr_subdevice
;
1318 static int snd_timer_user_next_device(struct snd_timer_id __user
*_tid
)
1320 struct snd_timer_id id
;
1321 struct snd_timer
*timer
;
1322 struct list_head
*p
;
1324 if (copy_from_user(&id
, _tid
, sizeof(id
)))
1326 mutex_lock(®ister_mutex
);
1327 if (id
.dev_class
< 0) { /* first item */
1328 if (list_empty(&snd_timer_list
))
1329 snd_timer_user_zero_id(&id
);
1331 timer
= list_entry(snd_timer_list
.next
,
1332 struct snd_timer
, device_list
);
1333 snd_timer_user_copy_id(&id
, timer
);
1336 switch (id
.dev_class
) {
1337 case SNDRV_TIMER_CLASS_GLOBAL
:
1338 id
.device
= id
.device
< 0 ? 0 : id
.device
+ 1;
1339 list_for_each(p
, &snd_timer_list
) {
1340 timer
= list_entry(p
, struct snd_timer
, device_list
);
1341 if (timer
->tmr_class
> SNDRV_TIMER_CLASS_GLOBAL
) {
1342 snd_timer_user_copy_id(&id
, timer
);
1345 if (timer
->tmr_device
>= id
.device
) {
1346 snd_timer_user_copy_id(&id
, timer
);
1350 if (p
== &snd_timer_list
)
1351 snd_timer_user_zero_id(&id
);
1353 case SNDRV_TIMER_CLASS_CARD
:
1354 case SNDRV_TIMER_CLASS_PCM
:
1361 if (id
.device
< 0) {
1364 if (id
.subdevice
< 0) {
1372 list_for_each(p
, &snd_timer_list
) {
1373 timer
= list_entry(p
, struct snd_timer
, device_list
);
1374 if (timer
->tmr_class
> id
.dev_class
) {
1375 snd_timer_user_copy_id(&id
, timer
);
1378 if (timer
->tmr_class
< id
.dev_class
)
1380 if (timer
->card
->number
> id
.card
) {
1381 snd_timer_user_copy_id(&id
, timer
);
1384 if (timer
->card
->number
< id
.card
)
1386 if (timer
->tmr_device
> id
.device
) {
1387 snd_timer_user_copy_id(&id
, timer
);
1390 if (timer
->tmr_device
< id
.device
)
1392 if (timer
->tmr_subdevice
> id
.subdevice
) {
1393 snd_timer_user_copy_id(&id
, timer
);
1396 if (timer
->tmr_subdevice
< id
.subdevice
)
1398 snd_timer_user_copy_id(&id
, timer
);
1401 if (p
== &snd_timer_list
)
1402 snd_timer_user_zero_id(&id
);
1405 snd_timer_user_zero_id(&id
);
1408 mutex_unlock(®ister_mutex
);
1409 if (copy_to_user(_tid
, &id
, sizeof(*_tid
)))
1414 static int snd_timer_user_ginfo(struct file
*file
,
1415 struct snd_timer_ginfo __user
*_ginfo
)
1417 struct snd_timer_ginfo
*ginfo
;
1418 struct snd_timer_id tid
;
1419 struct snd_timer
*t
;
1420 struct list_head
*p
;
1423 ginfo
= memdup_user(_ginfo
, sizeof(*ginfo
));
1425 return PTR_ERR(ginfo
);
1428 memset(ginfo
, 0, sizeof(*ginfo
));
1430 mutex_lock(®ister_mutex
);
1431 t
= snd_timer_find(&tid
);
1433 ginfo
->card
= t
->card
? t
->card
->number
: -1;
1434 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1435 ginfo
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1436 strlcpy(ginfo
->id
, t
->id
, sizeof(ginfo
->id
));
1437 strlcpy(ginfo
->name
, t
->name
, sizeof(ginfo
->name
));
1438 ginfo
->resolution
= t
->hw
.resolution
;
1439 if (t
->hw
.resolution_min
> 0) {
1440 ginfo
->resolution_min
= t
->hw
.resolution_min
;
1441 ginfo
->resolution_max
= t
->hw
.resolution_max
;
1443 list_for_each(p
, &t
->open_list_head
) {
1449 mutex_unlock(®ister_mutex
);
1450 if (err
>= 0 && copy_to_user(_ginfo
, ginfo
, sizeof(*ginfo
)))
1456 static int snd_timer_user_gparams(struct file
*file
,
1457 struct snd_timer_gparams __user
*_gparams
)
1459 struct snd_timer_gparams gparams
;
1460 struct snd_timer
*t
;
1463 if (copy_from_user(&gparams
, _gparams
, sizeof(gparams
)))
1465 mutex_lock(®ister_mutex
);
1466 t
= snd_timer_find(&gparams
.tid
);
1471 if (!list_empty(&t
->open_list_head
)) {
1475 if (!t
->hw
.set_period
) {
1479 err
= t
->hw
.set_period(t
, gparams
.period_num
, gparams
.period_den
);
1481 mutex_unlock(®ister_mutex
);
1485 static int snd_timer_user_gstatus(struct file
*file
,
1486 struct snd_timer_gstatus __user
*_gstatus
)
1488 struct snd_timer_gstatus gstatus
;
1489 struct snd_timer_id tid
;
1490 struct snd_timer
*t
;
1493 if (copy_from_user(&gstatus
, _gstatus
, sizeof(gstatus
)))
1496 memset(&gstatus
, 0, sizeof(gstatus
));
1498 mutex_lock(®ister_mutex
);
1499 t
= snd_timer_find(&tid
);
1501 if (t
->hw
.c_resolution
)
1502 gstatus
.resolution
= t
->hw
.c_resolution(t
);
1504 gstatus
.resolution
= t
->hw
.resolution
;
1505 if (t
->hw
.precise_resolution
) {
1506 t
->hw
.precise_resolution(t
, &gstatus
.resolution_num
,
1507 &gstatus
.resolution_den
);
1509 gstatus
.resolution_num
= gstatus
.resolution
;
1510 gstatus
.resolution_den
= 1000000000uL;
1515 mutex_unlock(®ister_mutex
);
1516 if (err
>= 0 && copy_to_user(_gstatus
, &gstatus
, sizeof(gstatus
)))
1521 static int snd_timer_user_tselect(struct file
*file
,
1522 struct snd_timer_select __user
*_tselect
)
1524 struct snd_timer_user
*tu
;
1525 struct snd_timer_select tselect
;
1529 tu
= file
->private_data
;
1531 snd_timer_close(tu
->timeri
);
1534 if (copy_from_user(&tselect
, _tselect
, sizeof(tselect
))) {
1538 sprintf(str
, "application %i", current
->pid
);
1539 if (tselect
.id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
1540 tselect
.id
.dev_sclass
= SNDRV_TIMER_SCLASS_APPLICATION
;
1541 err
= snd_timer_open(&tu
->timeri
, str
, &tselect
.id
, current
->pid
);
1550 tu
->tqueue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_tread
),
1552 if (tu
->tqueue
== NULL
)
1555 tu
->queue
= kmalloc(tu
->queue_size
* sizeof(struct snd_timer_read
),
1557 if (tu
->queue
== NULL
)
1562 snd_timer_close(tu
->timeri
);
1565 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_FAST
;
1566 tu
->timeri
->callback
= tu
->tread
1567 ? snd_timer_user_tinterrupt
: snd_timer_user_interrupt
;
1568 tu
->timeri
->ccallback
= snd_timer_user_ccallback
;
1569 tu
->timeri
->callback_data
= (void *)tu
;
1576 static int snd_timer_user_info(struct file
*file
,
1577 struct snd_timer_info __user
*_info
)
1579 struct snd_timer_user
*tu
;
1580 struct snd_timer_info
*info
;
1581 struct snd_timer
*t
;
1584 tu
= file
->private_data
;
1587 t
= tu
->timeri
->timer
;
1591 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1594 info
->card
= t
->card
? t
->card
->number
: -1;
1595 if (t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)
1596 info
->flags
|= SNDRV_TIMER_FLG_SLAVE
;
1597 strlcpy(info
->id
, t
->id
, sizeof(info
->id
));
1598 strlcpy(info
->name
, t
->name
, sizeof(info
->name
));
1599 info
->resolution
= t
->hw
.resolution
;
1600 if (copy_to_user(_info
, info
, sizeof(*_info
)))
1606 static int snd_timer_user_params(struct file
*file
,
1607 struct snd_timer_params __user
*_params
)
1609 struct snd_timer_user
*tu
;
1610 struct snd_timer_params params
;
1611 struct snd_timer
*t
;
1612 struct snd_timer_read
*tr
;
1613 struct snd_timer_tread
*ttr
;
1616 tu
= file
->private_data
;
1619 t
= tu
->timeri
->timer
;
1622 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
1624 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
) && params
.ticks
< 1) {
1628 if (params
.queue_size
> 0 &&
1629 (params
.queue_size
< 32 || params
.queue_size
> 1024)) {
1633 if (params
.filter
& ~((1<<SNDRV_TIMER_EVENT_RESOLUTION
)|
1634 (1<<SNDRV_TIMER_EVENT_TICK
)|
1635 (1<<SNDRV_TIMER_EVENT_START
)|
1636 (1<<SNDRV_TIMER_EVENT_STOP
)|
1637 (1<<SNDRV_TIMER_EVENT_CONTINUE
)|
1638 (1<<SNDRV_TIMER_EVENT_PAUSE
)|
1639 (1<<SNDRV_TIMER_EVENT_SUSPEND
)|
1640 (1<<SNDRV_TIMER_EVENT_RESUME
)|
1641 (1<<SNDRV_TIMER_EVENT_MSTART
)|
1642 (1<<SNDRV_TIMER_EVENT_MSTOP
)|
1643 (1<<SNDRV_TIMER_EVENT_MCONTINUE
)|
1644 (1<<SNDRV_TIMER_EVENT_MPAUSE
)|
1645 (1<<SNDRV_TIMER_EVENT_MSUSPEND
)|
1646 (1<<SNDRV_TIMER_EVENT_MRESUME
))) {
1650 snd_timer_stop(tu
->timeri
);
1651 spin_lock_irq(&t
->lock
);
1652 tu
->timeri
->flags
&= ~(SNDRV_TIMER_IFLG_AUTO
|
1653 SNDRV_TIMER_IFLG_EXCLUSIVE
|
1654 SNDRV_TIMER_IFLG_EARLY_EVENT
);
1655 if (params
.flags
& SNDRV_TIMER_PSFLG_AUTO
)
1656 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
1657 if (params
.flags
& SNDRV_TIMER_PSFLG_EXCLUSIVE
)
1658 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EXCLUSIVE
;
1659 if (params
.flags
& SNDRV_TIMER_PSFLG_EARLY_EVENT
)
1660 tu
->timeri
->flags
|= SNDRV_TIMER_IFLG_EARLY_EVENT
;
1661 spin_unlock_irq(&t
->lock
);
1662 if (params
.queue_size
> 0 &&
1663 (unsigned int)tu
->queue_size
!= params
.queue_size
) {
1665 ttr
= kmalloc(params
.queue_size
* sizeof(*ttr
),
1669 tu
->queue_size
= params
.queue_size
;
1673 tr
= kmalloc(params
.queue_size
* sizeof(*tr
),
1677 tu
->queue_size
= params
.queue_size
;
1682 tu
->qhead
= tu
->qtail
= tu
->qused
= 0;
1683 if (tu
->timeri
->flags
& SNDRV_TIMER_IFLG_EARLY_EVENT
) {
1685 struct snd_timer_tread tread
;
1686 tread
.event
= SNDRV_TIMER_EVENT_EARLY
;
1687 tread
.tstamp
.tv_sec
= 0;
1688 tread
.tstamp
.tv_nsec
= 0;
1690 snd_timer_user_append_to_tqueue(tu
, &tread
);
1692 struct snd_timer_read
*r
= &tu
->queue
[0];
1699 tu
->filter
= params
.filter
;
1700 tu
->ticks
= params
.ticks
;
1703 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
1708 static int snd_timer_user_status(struct file
*file
,
1709 struct snd_timer_status __user
*_status
)
1711 struct snd_timer_user
*tu
;
1712 struct snd_timer_status status
;
1714 tu
= file
->private_data
;
1717 memset(&status
, 0, sizeof(status
));
1718 status
.tstamp
= tu
->tstamp
;
1719 status
.resolution
= snd_timer_resolution(tu
->timeri
);
1720 status
.lost
= tu
->timeri
->lost
;
1721 status
.overrun
= tu
->overrun
;
1722 spin_lock_irq(&tu
->qlock
);
1723 status
.queue
= tu
->qused
;
1724 spin_unlock_irq(&tu
->qlock
);
1725 if (copy_to_user(_status
, &status
, sizeof(status
)))
1730 static int snd_timer_user_start(struct file
*file
)
1733 struct snd_timer_user
*tu
;
1735 tu
= file
->private_data
;
1738 snd_timer_stop(tu
->timeri
);
1739 tu
->timeri
->lost
= 0;
1740 tu
->last_resolution
= 0;
1741 return (err
= snd_timer_start(tu
->timeri
, tu
->ticks
)) < 0 ? err
: 0;
1744 static int snd_timer_user_stop(struct file
*file
)
1747 struct snd_timer_user
*tu
;
1749 tu
= file
->private_data
;
1752 return (err
= snd_timer_stop(tu
->timeri
)) < 0 ? err
: 0;
1755 static int snd_timer_user_continue(struct file
*file
)
1758 struct snd_timer_user
*tu
;
1760 tu
= file
->private_data
;
1763 tu
->timeri
->lost
= 0;
1764 return (err
= snd_timer_continue(tu
->timeri
)) < 0 ? err
: 0;
1767 static int snd_timer_user_pause(struct file
*file
)
1770 struct snd_timer_user
*tu
;
1772 tu
= file
->private_data
;
1775 return (err
= snd_timer_pause(tu
->timeri
)) < 0 ? err
: 0;
1779 SNDRV_TIMER_IOCTL_START_OLD
= _IO('T', 0x20),
1780 SNDRV_TIMER_IOCTL_STOP_OLD
= _IO('T', 0x21),
1781 SNDRV_TIMER_IOCTL_CONTINUE_OLD
= _IO('T', 0x22),
1782 SNDRV_TIMER_IOCTL_PAUSE_OLD
= _IO('T', 0x23),
1785 static long __snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1788 struct snd_timer_user
*tu
;
1789 void __user
*argp
= (void __user
*)arg
;
1790 int __user
*p
= argp
;
1792 tu
= file
->private_data
;
1794 case SNDRV_TIMER_IOCTL_PVERSION
:
1795 return put_user(SNDRV_TIMER_VERSION
, p
) ? -EFAULT
: 0;
1796 case SNDRV_TIMER_IOCTL_NEXT_DEVICE
:
1797 return snd_timer_user_next_device(argp
);
1798 case SNDRV_TIMER_IOCTL_TREAD
:
1802 if (tu
->timeri
) /* too late */
1804 if (get_user(xarg
, p
))
1806 tu
->tread
= xarg
? 1 : 0;
1809 case SNDRV_TIMER_IOCTL_GINFO
:
1810 return snd_timer_user_ginfo(file
, argp
);
1811 case SNDRV_TIMER_IOCTL_GPARAMS
:
1812 return snd_timer_user_gparams(file
, argp
);
1813 case SNDRV_TIMER_IOCTL_GSTATUS
:
1814 return snd_timer_user_gstatus(file
, argp
);
1815 case SNDRV_TIMER_IOCTL_SELECT
:
1816 return snd_timer_user_tselect(file
, argp
);
1817 case SNDRV_TIMER_IOCTL_INFO
:
1818 return snd_timer_user_info(file
, argp
);
1819 case SNDRV_TIMER_IOCTL_PARAMS
:
1820 return snd_timer_user_params(file
, argp
);
1821 case SNDRV_TIMER_IOCTL_STATUS
:
1822 return snd_timer_user_status(file
, argp
);
1823 case SNDRV_TIMER_IOCTL_START
:
1824 case SNDRV_TIMER_IOCTL_START_OLD
:
1825 return snd_timer_user_start(file
);
1826 case SNDRV_TIMER_IOCTL_STOP
:
1827 case SNDRV_TIMER_IOCTL_STOP_OLD
:
1828 return snd_timer_user_stop(file
);
1829 case SNDRV_TIMER_IOCTL_CONTINUE
:
1830 case SNDRV_TIMER_IOCTL_CONTINUE_OLD
:
1831 return snd_timer_user_continue(file
);
1832 case SNDRV_TIMER_IOCTL_PAUSE
:
1833 case SNDRV_TIMER_IOCTL_PAUSE_OLD
:
1834 return snd_timer_user_pause(file
);
1839 static long snd_timer_user_ioctl(struct file
*file
, unsigned int cmd
,
1842 struct snd_timer_user
*tu
= file
->private_data
;
1845 mutex_lock(&tu
->ioctl_lock
);
1846 ret
= __snd_timer_user_ioctl(file
, cmd
, arg
);
1847 mutex_unlock(&tu
->ioctl_lock
);
1851 static int snd_timer_user_fasync(int fd
, struct file
* file
, int on
)
1853 struct snd_timer_user
*tu
;
1855 tu
= file
->private_data
;
1856 return fasync_helper(fd
, file
, on
, &tu
->fasync
);
1859 static ssize_t
snd_timer_user_read(struct file
*file
, char __user
*buffer
,
1860 size_t count
, loff_t
*offset
)
1862 struct snd_timer_user
*tu
;
1863 long result
= 0, unit
;
1866 tu
= file
->private_data
;
1867 unit
= tu
->tread
? sizeof(struct snd_timer_tread
) : sizeof(struct snd_timer_read
);
1868 spin_lock_irq(&tu
->qlock
);
1869 while ((long)count
- result
>= unit
) {
1870 while (!tu
->qused
) {
1873 if ((file
->f_flags
& O_NONBLOCK
) != 0 || result
> 0) {
1878 set_current_state(TASK_INTERRUPTIBLE
);
1879 init_waitqueue_entry(&wait
, current
);
1880 add_wait_queue(&tu
->qchange_sleep
, &wait
);
1882 spin_unlock_irq(&tu
->qlock
);
1884 spin_lock_irq(&tu
->qlock
);
1886 remove_wait_queue(&tu
->qchange_sleep
, &wait
);
1888 if (signal_pending(current
)) {
1894 spin_unlock_irq(&tu
->qlock
);
1899 if (copy_to_user(buffer
, &tu
->tqueue
[tu
->qhead
++],
1900 sizeof(struct snd_timer_tread
))) {
1905 if (copy_to_user(buffer
, &tu
->queue
[tu
->qhead
++],
1906 sizeof(struct snd_timer_read
))) {
1912 tu
->qhead
%= tu
->queue_size
;
1917 spin_lock_irq(&tu
->qlock
);
1920 spin_unlock_irq(&tu
->qlock
);
1922 return result
> 0 ? result
: err
;
1925 static unsigned int snd_timer_user_poll(struct file
*file
, poll_table
* wait
)
1928 struct snd_timer_user
*tu
;
1930 tu
= file
->private_data
;
1932 poll_wait(file
, &tu
->qchange_sleep
, wait
);
1936 mask
|= POLLIN
| POLLRDNORM
;
1941 #ifdef CONFIG_COMPAT
1942 #include "timer_compat.c"
1944 #define snd_timer_user_ioctl_compat NULL
1947 static const struct file_operations snd_timer_f_ops
=
1949 .owner
= THIS_MODULE
,
1950 .read
= snd_timer_user_read
,
1951 .open
= snd_timer_user_open
,
1952 .release
= snd_timer_user_release
,
1953 .llseek
= no_llseek
,
1954 .poll
= snd_timer_user_poll
,
1955 .unlocked_ioctl
= snd_timer_user_ioctl
,
1956 .compat_ioctl
= snd_timer_user_ioctl_compat
,
1957 .fasync
= snd_timer_user_fasync
,
1964 static int __init
alsa_timer_init(void)
1968 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1969 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1,
1973 if ((err
= snd_timer_register_system()) < 0)
1974 snd_printk(KERN_ERR
"unable to register system timer (%i)\n",
1976 if ((err
= snd_register_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0,
1977 &snd_timer_f_ops
, NULL
, "timer")) < 0)
1978 snd_printk(KERN_ERR
"unable to register timer device (%i)\n",
1980 snd_timer_proc_init();
1984 static void __exit
alsa_timer_exit(void)
1986 struct list_head
*p
, *n
;
1988 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER
, NULL
, 0);
1989 /* unregister the system timer */
1990 list_for_each_safe(p
, n
, &snd_timer_list
) {
1991 struct snd_timer
*timer
= list_entry(p
, struct snd_timer
, device_list
);
1992 snd_timer_free(timer
);
1994 snd_timer_proc_done();
1995 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1996 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS
, SNDRV_CARDS
- 1);
2000 module_init(alsa_timer_init
)
2001 module_exit(alsa_timer_exit
)
2003 EXPORT_SYMBOL(snd_timer_open
);
2004 EXPORT_SYMBOL(snd_timer_close
);
2005 EXPORT_SYMBOL(snd_timer_resolution
);
2006 EXPORT_SYMBOL(snd_timer_start
);
2007 EXPORT_SYMBOL(snd_timer_stop
);
2008 EXPORT_SYMBOL(snd_timer_continue
);
2009 EXPORT_SYMBOL(snd_timer_pause
);
2010 EXPORT_SYMBOL(snd_timer_new
);
2011 EXPORT_SYMBOL(snd_timer_notify
);
2012 EXPORT_SYMBOL(snd_timer_global_new
);
2013 EXPORT_SYMBOL(snd_timer_global_free
);
2014 EXPORT_SYMBOL(snd_timer_global_register
);
2015 EXPORT_SYMBOL(snd_timer_interrupt
);