3 * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
4 * Jaroslav Kysela <perex@perex.cz>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sound/core.h>
24 #include <linux/slab.h>
25 #include "seq_timer.h"
26 #include "seq_queue.h"
29 /* allowed sequencer timer frequencies, in Hz */
30 #define MIN_FREQUENCY 10
31 #define MAX_FREQUENCY 6250
32 #define DEFAULT_FREQUENCY 1000
34 #define SKEW_BASE 0x10000 /* 16bit shift */
36 static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer
*tmr
)
38 if (tmr
->tempo
< 1000000)
39 tmr
->tick
.resolution
= (tmr
->tempo
* 1000) / tmr
->ppq
;
41 /* might overflow.. */
43 s
= tmr
->tempo
% tmr
->ppq
;
44 s
= (s
* 1000) / tmr
->ppq
;
45 tmr
->tick
.resolution
= (tmr
->tempo
/ tmr
->ppq
) * 1000;
46 tmr
->tick
.resolution
+= s
;
48 if (tmr
->tick
.resolution
<= 0)
49 tmr
->tick
.resolution
= 1;
50 snd_seq_timer_update_tick(&tmr
->tick
, 0);
53 /* create new timer (constructor) */
54 struct snd_seq_timer
*snd_seq_timer_new(void)
56 struct snd_seq_timer
*tmr
;
58 tmr
= kzalloc(sizeof(*tmr
), GFP_KERNEL
);
61 spin_lock_init(&tmr
->lock
);
63 /* reset setup to defaults */
64 snd_seq_timer_defaults(tmr
);
67 snd_seq_timer_reset(tmr
);
72 /* delete timer (destructor) */
73 void snd_seq_timer_delete(struct snd_seq_timer
**tmr
)
75 struct snd_seq_timer
*t
= *tmr
;
79 pr_debug("ALSA: seq: snd_seq_timer_delete() called with NULL timer\n");
85 snd_seq_timer_stop(t
);
86 snd_seq_timer_reset(t
);
91 void snd_seq_timer_defaults(struct snd_seq_timer
* tmr
)
95 spin_lock_irqsave(&tmr
->lock
, flags
);
97 tmr
->ppq
= 96; /* 96 PPQ */
98 tmr
->tempo
= 500000; /* 120 BPM */
99 snd_seq_timer_set_tick_resolution(tmr
);
102 tmr
->type
= SNDRV_SEQ_TIMER_ALSA
;
103 tmr
->alsa_id
.dev_class
= seq_default_timer_class
;
104 tmr
->alsa_id
.dev_sclass
= seq_default_timer_sclass
;
105 tmr
->alsa_id
.card
= seq_default_timer_card
;
106 tmr
->alsa_id
.device
= seq_default_timer_device
;
107 tmr
->alsa_id
.subdevice
= seq_default_timer_subdevice
;
108 tmr
->preferred_resolution
= seq_default_timer_resolution
;
110 tmr
->skew
= tmr
->skew_base
= SKEW_BASE
;
111 spin_unlock_irqrestore(&tmr
->lock
, flags
);
114 static void seq_timer_reset(struct snd_seq_timer
*tmr
)
116 /* reset time & songposition */
117 tmr
->cur_time
.tv_sec
= 0;
118 tmr
->cur_time
.tv_nsec
= 0;
120 tmr
->tick
.cur_tick
= 0;
121 tmr
->tick
.fraction
= 0;
124 void snd_seq_timer_reset(struct snd_seq_timer
*tmr
)
128 spin_lock_irqsave(&tmr
->lock
, flags
);
129 seq_timer_reset(tmr
);
130 spin_unlock_irqrestore(&tmr
->lock
, flags
);
134 /* called by timer interrupt routine. the period time since previous invocation is passed */
135 static void snd_seq_timer_interrupt(struct snd_timer_instance
*timeri
,
136 unsigned long resolution
,
140 struct snd_seq_queue
*q
= timeri
->callback_data
;
141 struct snd_seq_timer
*tmr
;
148 spin_lock_irqsave(&tmr
->lock
, flags
);
150 spin_unlock_irqrestore(&tmr
->lock
, flags
);
155 if (tmr
->skew
!= tmr
->skew_base
) {
156 /* FIXME: assuming skew_base = 0x10000 */
157 resolution
= (resolution
>> 16) * tmr
->skew
+
158 (((resolution
& 0xffff) * tmr
->skew
) >> 16);
162 snd_seq_inc_time_nsec(&tmr
->cur_time
, resolution
);
164 /* calculate current tick */
165 snd_seq_timer_update_tick(&tmr
->tick
, resolution
);
167 /* register actual time of this timer update */
168 ktime_get_ts64(&tmr
->last_update
);
170 spin_unlock_irqrestore(&tmr
->lock
, flags
);
172 /* check queues and dispatch events */
173 snd_seq_check_queue(q
, 1, 0);
176 /* set current tempo */
177 int snd_seq_timer_set_tempo(struct snd_seq_timer
* tmr
, int tempo
)
181 if (snd_BUG_ON(!tmr
))
185 spin_lock_irqsave(&tmr
->lock
, flags
);
186 if ((unsigned int)tempo
!= tmr
->tempo
) {
188 snd_seq_timer_set_tick_resolution(tmr
);
190 spin_unlock_irqrestore(&tmr
->lock
, flags
);
194 /* set current ppq */
195 int snd_seq_timer_set_ppq(struct snd_seq_timer
* tmr
, int ppq
)
199 if (snd_BUG_ON(!tmr
))
203 spin_lock_irqsave(&tmr
->lock
, flags
);
204 if (tmr
->running
&& (ppq
!= tmr
->ppq
)) {
205 /* refuse to change ppq on running timers */
206 /* because it will upset the song position (ticks) */
207 spin_unlock_irqrestore(&tmr
->lock
, flags
);
208 pr_debug("ALSA: seq: cannot change ppq of a running timer\n");
213 snd_seq_timer_set_tick_resolution(tmr
);
214 spin_unlock_irqrestore(&tmr
->lock
, flags
);
218 /* set current tick position */
219 int snd_seq_timer_set_position_tick(struct snd_seq_timer
*tmr
,
220 snd_seq_tick_time_t position
)
224 if (snd_BUG_ON(!tmr
))
227 spin_lock_irqsave(&tmr
->lock
, flags
);
228 tmr
->tick
.cur_tick
= position
;
229 tmr
->tick
.fraction
= 0;
230 spin_unlock_irqrestore(&tmr
->lock
, flags
);
234 /* set current real-time position */
235 int snd_seq_timer_set_position_time(struct snd_seq_timer
*tmr
,
236 snd_seq_real_time_t position
)
240 if (snd_BUG_ON(!tmr
))
243 snd_seq_sanity_real_time(&position
);
244 spin_lock_irqsave(&tmr
->lock
, flags
);
245 tmr
->cur_time
= position
;
246 spin_unlock_irqrestore(&tmr
->lock
, flags
);
251 int snd_seq_timer_set_skew(struct snd_seq_timer
*tmr
, unsigned int skew
,
256 if (snd_BUG_ON(!tmr
))
260 if (base
!= SKEW_BASE
) {
261 pr_debug("ALSA: seq: invalid skew base 0x%x\n", base
);
264 spin_lock_irqsave(&tmr
->lock
, flags
);
266 spin_unlock_irqrestore(&tmr
->lock
, flags
);
270 int snd_seq_timer_open(struct snd_seq_queue
*q
)
272 struct snd_timer_instance
*t
;
273 struct snd_seq_timer
*tmr
;
278 if (snd_BUG_ON(!tmr
))
282 sprintf(str
, "sequencer queue %i", q
->queue
);
283 if (tmr
->type
!= SNDRV_SEQ_TIMER_ALSA
) /* standard ALSA timer */
285 if (tmr
->alsa_id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
286 tmr
->alsa_id
.dev_sclass
= SNDRV_TIMER_SCLASS_SEQUENCER
;
287 err
= snd_timer_open(&t
, str
, &tmr
->alsa_id
, q
->queue
);
288 if (err
< 0 && tmr
->alsa_id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
) {
289 if (tmr
->alsa_id
.dev_class
!= SNDRV_TIMER_CLASS_GLOBAL
||
290 tmr
->alsa_id
.device
!= SNDRV_TIMER_GLOBAL_SYSTEM
) {
291 struct snd_timer_id tid
;
292 memset(&tid
, 0, sizeof(tid
));
293 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
294 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_SEQUENCER
;
296 tid
.device
= SNDRV_TIMER_GLOBAL_SYSTEM
;
297 err
= snd_timer_open(&t
, str
, &tid
, q
->queue
);
301 pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err
);
304 t
->callback
= snd_seq_timer_interrupt
;
305 t
->callback_data
= q
;
306 t
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
307 spin_lock_irq(&tmr
->lock
);
309 spin_unlock_irq(&tmr
->lock
);
313 int snd_seq_timer_close(struct snd_seq_queue
*q
)
315 struct snd_seq_timer
*tmr
;
316 struct snd_timer_instance
*t
;
319 if (snd_BUG_ON(!tmr
))
321 spin_lock_irq(&tmr
->lock
);
324 spin_unlock_irq(&tmr
->lock
);
330 static int seq_timer_stop(struct snd_seq_timer
*tmr
)
337 snd_timer_pause(tmr
->timeri
);
341 int snd_seq_timer_stop(struct snd_seq_timer
*tmr
)
346 spin_lock_irqsave(&tmr
->lock
, flags
);
347 err
= seq_timer_stop(tmr
);
348 spin_unlock_irqrestore(&tmr
->lock
, flags
);
352 static int initialize_timer(struct snd_seq_timer
*tmr
)
357 t
= tmr
->timeri
->timer
;
361 freq
= tmr
->preferred_resolution
;
363 freq
= DEFAULT_FREQUENCY
;
364 else if (freq
< MIN_FREQUENCY
)
365 freq
= MIN_FREQUENCY
;
366 else if (freq
> MAX_FREQUENCY
)
367 freq
= MAX_FREQUENCY
;
370 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)) {
371 unsigned long r
= t
->hw
.resolution
;
372 if (! r
&& t
->hw
.c_resolution
)
373 r
= t
->hw
.c_resolution(t
);
375 tmr
->ticks
= (unsigned int)(1000000000uL / (r
* freq
));
380 tmr
->initialized
= 1;
384 static int seq_timer_start(struct snd_seq_timer
*tmr
)
390 seq_timer_reset(tmr
);
391 if (initialize_timer(tmr
) < 0)
393 snd_timer_start(tmr
->timeri
, tmr
->ticks
);
395 ktime_get_ts64(&tmr
->last_update
);
399 int snd_seq_timer_start(struct snd_seq_timer
*tmr
)
404 spin_lock_irqsave(&tmr
->lock
, flags
);
405 err
= seq_timer_start(tmr
);
406 spin_unlock_irqrestore(&tmr
->lock
, flags
);
410 static int seq_timer_continue(struct snd_seq_timer
*tmr
)
416 if (! tmr
->initialized
) {
417 seq_timer_reset(tmr
);
418 if (initialize_timer(tmr
) < 0)
421 snd_timer_start(tmr
->timeri
, tmr
->ticks
);
423 ktime_get_ts64(&tmr
->last_update
);
427 int snd_seq_timer_continue(struct snd_seq_timer
*tmr
)
432 spin_lock_irqsave(&tmr
->lock
, flags
);
433 err
= seq_timer_continue(tmr
);
434 spin_unlock_irqrestore(&tmr
->lock
, flags
);
438 /* return current 'real' time. use timeofday() to get better granularity. */
439 snd_seq_real_time_t
snd_seq_timer_get_cur_time(struct snd_seq_timer
*tmr
)
441 snd_seq_real_time_t cur_time
;
444 spin_lock_irqsave(&tmr
->lock
, flags
);
445 cur_time
= tmr
->cur_time
;
447 struct timespec64 tm
;
450 tm
= timespec64_sub(tm
, tmr
->last_update
);
451 cur_time
.tv_nsec
+= tm
.tv_nsec
;
452 cur_time
.tv_sec
+= tm
.tv_sec
;
453 snd_seq_sanity_real_time(&cur_time
);
455 spin_unlock_irqrestore(&tmr
->lock
, flags
);
459 /* TODO: use interpolation on tick queue (will only be useful for very
461 snd_seq_tick_time_t
snd_seq_timer_get_cur_tick(struct snd_seq_timer
*tmr
)
463 return tmr
->tick
.cur_tick
;
467 #ifdef CONFIG_SND_PROC_FS
468 /* exported to seq_info.c */
469 void snd_seq_info_timer_read(struct snd_info_entry
*entry
,
470 struct snd_info_buffer
*buffer
)
473 struct snd_seq_queue
*q
;
474 struct snd_seq_timer
*tmr
;
475 struct snd_timer_instance
*ti
;
476 unsigned long resolution
;
478 for (idx
= 0; idx
< SNDRV_SEQ_MAX_QUEUES
; idx
++) {
482 if ((tmr
= q
->timer
) == NULL
||
483 (ti
= tmr
->timeri
) == NULL
) {
487 snd_iprintf(buffer
, "Timer for queue %i : %s\n", q
->queue
, ti
->timer
->name
);
488 resolution
= snd_timer_resolution(ti
) * tmr
->ticks
;
489 snd_iprintf(buffer
, " Period time : %lu.%09lu\n", resolution
/ 1000000000, resolution
% 1000000000);
490 snd_iprintf(buffer
, " Skew : %u / %u\n", tmr
->skew
, tmr
->skew_base
);
494 #endif /* CONFIG_SND_PROC_FS */