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/driver.h>
24 #include <sound/core.h>
25 #include <linux/slab.h>
26 #include "seq_timer.h"
27 #include "seq_queue.h"
30 /* allowed sequencer timer frequencies, in Hz */
31 #define MIN_FREQUENCY 10
32 #define MAX_FREQUENCY 6250
33 #define DEFAULT_FREQUENCY 1000
35 #define SKEW_BASE 0x10000 /* 16bit shift */
37 static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer_tick *tick,
41 tick->resolution = (tempo * 1000) / ppq;
43 /* might overflow.. */
47 tick->resolution = (tempo / ppq) * 1000;
48 tick->resolution += s;
50 if (tick->resolution <= 0)
52 snd_seq_timer_update_tick(tick, 0);
55 /* create new timer (constructor) */
56 struct snd_seq_timer *snd_seq_timer_new(void)
58 struct snd_seq_timer *tmr;
60 tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
62 snd_printd("malloc failed for snd_seq_timer_new() \n");
65 spin_lock_init(&tmr->lock);
67 /* reset setup to defaults */
68 snd_seq_timer_defaults(tmr);
71 snd_seq_timer_reset(tmr);
76 /* delete timer (destructor) */
77 void snd_seq_timer_delete(struct snd_seq_timer **tmr)
79 struct snd_seq_timer *t = *tmr;
83 snd_printd("oops: snd_seq_timer_delete() called with NULL timer\n");
89 snd_seq_timer_stop(t);
90 snd_seq_timer_reset(t);
95 void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
98 tmr->ppq = 96; /* 96 PPQ */
99 tmr->tempo = 500000; /* 120 BPM */
100 snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
103 tmr->type = SNDRV_SEQ_TIMER_ALSA;
104 tmr->alsa_id.dev_class = seq_default_timer_class;
105 tmr->alsa_id.dev_sclass = seq_default_timer_sclass;
106 tmr->alsa_id.card = seq_default_timer_card;
107 tmr->alsa_id.device = seq_default_timer_device;
108 tmr->alsa_id.subdevice = seq_default_timer_subdevice;
109 tmr->preferred_resolution = seq_default_timer_resolution;
111 tmr->skew = tmr->skew_base = SKEW_BASE;
114 void snd_seq_timer_reset(struct snd_seq_timer * tmr)
118 spin_lock_irqsave(&tmr->lock, flags);
120 /* reset time & songposition */
121 tmr->cur_time.tv_sec = 0;
122 tmr->cur_time.tv_nsec = 0;
124 tmr->tick.cur_tick = 0;
125 tmr->tick.fraction = 0;
127 spin_unlock_irqrestore(&tmr->lock, flags);
131 /* called by timer interrupt routine. the period time since previous invocation is passed */
132 static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
133 unsigned long resolution,
137 struct snd_seq_queue *q = timeri->callback_data;
138 struct snd_seq_timer *tmr;
149 if (tmr->skew != tmr->skew_base) {
150 /* FIXME: assuming skew_base = 0x10000 */
151 resolution = (resolution >> 16) * tmr->skew +
152 (((resolution & 0xffff) * tmr->skew) >> 16);
155 spin_lock_irqsave(&tmr->lock, flags);
158 snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
160 /* calculate current tick */
161 snd_seq_timer_update_tick(&tmr->tick, resolution);
163 /* register actual time of this timer update */
164 do_gettimeofday(&tmr->last_update);
166 spin_unlock_irqrestore(&tmr->lock, flags);
168 /* check queues and dispatch events */
169 snd_seq_check_queue(q, 1, 0);
172 /* set current tempo */
173 int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
177 snd_assert(tmr, return -EINVAL);
180 spin_lock_irqsave(&tmr->lock, flags);
181 if ((unsigned int)tempo != tmr->tempo) {
183 snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
185 spin_unlock_irqrestore(&tmr->lock, flags);
189 /* set current ppq */
190 int snd_seq_timer_set_ppq(struct snd_seq_timer * tmr, int ppq)
194 snd_assert(tmr, return -EINVAL);
197 spin_lock_irqsave(&tmr->lock, flags);
198 if (tmr->running && (ppq != tmr->ppq)) {
199 /* refuse to change ppq on running timers */
200 /* because it will upset the song position (ticks) */
201 spin_unlock_irqrestore(&tmr->lock, flags);
202 snd_printd("seq: cannot change ppq of a running timer\n");
207 snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
208 spin_unlock_irqrestore(&tmr->lock, flags);
212 /* set current tick position */
213 int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
214 snd_seq_tick_time_t position)
218 snd_assert(tmr, return -EINVAL);
220 spin_lock_irqsave(&tmr->lock, flags);
221 tmr->tick.cur_tick = position;
222 tmr->tick.fraction = 0;
223 spin_unlock_irqrestore(&tmr->lock, flags);
227 /* set current real-time position */
228 int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
229 snd_seq_real_time_t position)
233 snd_assert(tmr, return -EINVAL);
235 snd_seq_sanity_real_time(&position);
236 spin_lock_irqsave(&tmr->lock, flags);
237 tmr->cur_time = position;
238 spin_unlock_irqrestore(&tmr->lock, flags);
243 int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
248 snd_assert(tmr, return -EINVAL);
251 if (base != SKEW_BASE) {
252 snd_printd("invalid skew base 0x%x\n", base);
255 spin_lock_irqsave(&tmr->lock, flags);
257 spin_unlock_irqrestore(&tmr->lock, flags);
261 int snd_seq_timer_open(struct snd_seq_queue *q)
263 struct snd_timer_instance *t;
264 struct snd_seq_timer *tmr;
269 snd_assert(tmr != NULL, return -EINVAL);
272 sprintf(str, "sequencer queue %i", q->queue);
273 if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
275 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
276 tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
277 err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue);
278 if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
279 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
280 tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
281 struct snd_timer_id tid;
282 memset(&tid, 0, sizeof(tid));
283 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
284 tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
286 tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
287 err = snd_timer_open(&t, str, &tid, q->queue);
290 snd_printk(KERN_ERR "seq fatal error: cannot create timer (%i)\n", err);
294 t->callback = snd_seq_timer_interrupt;
295 t->callback_data = q;
296 t->flags |= SNDRV_TIMER_IFLG_AUTO;
301 int snd_seq_timer_close(struct snd_seq_queue *q)
303 struct snd_seq_timer *tmr;
306 snd_assert(tmr != NULL, return -EINVAL);
308 snd_timer_stop(tmr->timeri);
309 snd_timer_close(tmr->timeri);
315 int snd_seq_timer_stop(struct snd_seq_timer * tmr)
322 snd_timer_pause(tmr->timeri);
326 static int initialize_timer(struct snd_seq_timer *tmr)
331 t = tmr->timeri->timer;
332 snd_assert(t, return -EINVAL);
334 freq = tmr->preferred_resolution;
336 freq = DEFAULT_FREQUENCY;
337 else if (freq < MIN_FREQUENCY)
338 freq = MIN_FREQUENCY;
339 else if (freq > MAX_FREQUENCY)
340 freq = MAX_FREQUENCY;
343 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
344 unsigned long r = t->hw.resolution;
345 if (! r && t->hw.c_resolution)
346 r = t->hw.c_resolution(t);
348 tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
353 tmr->initialized = 1;
357 int snd_seq_timer_start(struct snd_seq_timer * tmr)
362 snd_seq_timer_stop(tmr);
363 snd_seq_timer_reset(tmr);
364 if (initialize_timer(tmr) < 0)
366 snd_timer_start(tmr->timeri, tmr->ticks);
368 do_gettimeofday(&tmr->last_update);
372 int snd_seq_timer_continue(struct snd_seq_timer * tmr)
378 if (! tmr->initialized) {
379 snd_seq_timer_reset(tmr);
380 if (initialize_timer(tmr) < 0)
383 snd_timer_start(tmr->timeri, tmr->ticks);
385 do_gettimeofday(&tmr->last_update);
389 /* return current 'real' time. use timeofday() to get better granularity. */
390 snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr)
392 snd_seq_real_time_t cur_time;
394 cur_time = tmr->cur_time;
398 do_gettimeofday(&tm);
399 usec = (int)(tm.tv_usec - tmr->last_update.tv_usec);
401 cur_time.tv_nsec += (1000000 + usec) * 1000;
402 cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec - 1;
404 cur_time.tv_nsec += usec * 1000;
405 cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec;
407 snd_seq_sanity_real_time(&cur_time);
413 /* TODO: use interpolation on tick queue (will only be useful for very
415 snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
417 return tmr->tick.cur_tick;
421 #ifdef CONFIG_PROC_FS
422 /* exported to seq_info.c */
423 void snd_seq_info_timer_read(struct snd_info_entry *entry,
424 struct snd_info_buffer *buffer)
427 struct snd_seq_queue *q;
428 struct snd_seq_timer *tmr;
429 struct snd_timer_instance *ti;
430 unsigned long resolution;
432 for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
436 if ((tmr = q->timer) == NULL ||
437 (ti = tmr->timeri) == NULL) {
441 snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
442 resolution = snd_timer_resolution(ti) * tmr->ticks;
443 snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
444 snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
448 #endif /* CONFIG_PROC_FS */