blob: d8fcd62e400f0ed512b330132e3c603ad4cc7b49 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA sequencer Timer
3 * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 *
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.
11 *
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.
16 *
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
20 *
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <sound/core.h>
24#include <linux/slab.h>
25#include "seq_timer.h"
26#include "seq_queue.h"
27#include "seq_info.h"
28
Clemens Ladisch87ef7772005-10-19 14:38:30 +020029/* allowed sequencer timer frequencies, in Hz */
30#define MIN_FREQUENCY 10
31#define MAX_FREQUENCY 6250
32#define DEFAULT_FREQUENCY 1000
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define SKEW_BASE 0x10000 /* 16bit shift */
35
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010036static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer_tick *tick,
Clemens Ladischbf3b6442005-09-28 08:18:17 +020037 int tempo, int ppq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 if (tempo < 1000000)
40 tick->resolution = (tempo * 1000) / ppq;
41 else {
42 /* might overflow.. */
43 unsigned int s;
44 s = tempo % ppq;
45 s = (s * 1000) / ppq;
46 tick->resolution = (tempo / ppq) * 1000;
47 tick->resolution += s;
48 }
49 if (tick->resolution <= 0)
50 tick->resolution = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 snd_seq_timer_update_tick(tick, 0);
52}
53
54/* create new timer (constructor) */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010055struct snd_seq_timer *snd_seq_timer_new(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010057 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Takashi Iwaiecca82b2005-09-09 14:20:49 +020059 tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (tmr == NULL) {
61 snd_printd("malloc failed for snd_seq_timer_new() \n");
62 return NULL;
63 }
64 spin_lock_init(&tmr->lock);
65
66 /* reset setup to defaults */
67 snd_seq_timer_defaults(tmr);
68
69 /* reset time */
70 snd_seq_timer_reset(tmr);
71
72 return tmr;
73}
74
75/* delete timer (destructor) */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010076void snd_seq_timer_delete(struct snd_seq_timer **tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010078 struct snd_seq_timer *t = *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *tmr = NULL;
80
81 if (t == NULL) {
82 snd_printd("oops: snd_seq_timer_delete() called with NULL timer\n");
83 return;
84 }
85 t->running = 0;
86
87 /* reset time */
88 snd_seq_timer_stop(t);
89 snd_seq_timer_reset(t);
90
91 kfree(t);
92}
93
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010094void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 /* setup defaults */
97 tmr->ppq = 96; /* 96 PPQ */
98 tmr->tempo = 500000; /* 120 BPM */
Clemens Ladischbf3b6442005-09-28 08:18:17 +020099 snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 tmr->running = 0;
101
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;
109
110 tmr->skew = tmr->skew_base = SKEW_BASE;
111}
112
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100113void snd_seq_timer_reset(struct snd_seq_timer * tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 unsigned long flags;
116
117 spin_lock_irqsave(&tmr->lock, flags);
118
119 /* reset time & songposition */
120 tmr->cur_time.tv_sec = 0;
121 tmr->cur_time.tv_nsec = 0;
122
123 tmr->tick.cur_tick = 0;
124 tmr->tick.fraction = 0;
125
126 spin_unlock_irqrestore(&tmr->lock, flags);
127}
128
129
130/* called by timer interrupt routine. the period time since previous invocation is passed */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100131static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned long resolution,
133 unsigned long ticks)
134{
135 unsigned long flags;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100136 struct snd_seq_queue *q = timeri->callback_data;
137 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 if (q == NULL)
140 return;
141 tmr = q->timer;
142 if (tmr == NULL)
143 return;
144 if (!tmr->running)
145 return;
146
147 resolution *= ticks;
148 if (tmr->skew != tmr->skew_base) {
149 /* FIXME: assuming skew_base = 0x10000 */
150 resolution = (resolution >> 16) * tmr->skew +
151 (((resolution & 0xffff) * tmr->skew) >> 16);
152 }
153
154 spin_lock_irqsave(&tmr->lock, flags);
155
156 /* update timer */
157 snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
158
159 /* calculate current tick */
160 snd_seq_timer_update_tick(&tmr->tick, resolution);
161
162 /* register actual time of this timer update */
163 do_gettimeofday(&tmr->last_update);
164
165 spin_unlock_irqrestore(&tmr->lock, flags);
166
167 /* check queues and dispatch events */
168 snd_seq_check_queue(q, 1, 0);
169}
170
171/* set current tempo */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100172int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 unsigned long flags;
175
176 snd_assert(tmr, return -EINVAL);
177 if (tempo <= 0)
178 return -EINVAL;
179 spin_lock_irqsave(&tmr->lock, flags);
180 if ((unsigned int)tempo != tmr->tempo) {
181 tmr->tempo = tempo;
Clemens Ladischbf3b6442005-09-28 08:18:17 +0200182 snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184 spin_unlock_irqrestore(&tmr->lock, flags);
185 return 0;
186}
187
188/* set current ppq */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100189int snd_seq_timer_set_ppq(struct snd_seq_timer * tmr, int ppq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 unsigned long flags;
192
193 snd_assert(tmr, return -EINVAL);
194 if (ppq <= 0)
195 return -EINVAL;
196 spin_lock_irqsave(&tmr->lock, flags);
197 if (tmr->running && (ppq != tmr->ppq)) {
198 /* refuse to change ppq on running timers */
199 /* because it will upset the song position (ticks) */
200 spin_unlock_irqrestore(&tmr->lock, flags);
201 snd_printd("seq: cannot change ppq of a running timer\n");
202 return -EBUSY;
203 }
204
205 tmr->ppq = ppq;
Clemens Ladischbf3b6442005-09-28 08:18:17 +0200206 snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 spin_unlock_irqrestore(&tmr->lock, flags);
208 return 0;
209}
210
211/* set current tick position */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100212int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
213 snd_seq_tick_time_t position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 unsigned long flags;
216
217 snd_assert(tmr, return -EINVAL);
218
219 spin_lock_irqsave(&tmr->lock, flags);
220 tmr->tick.cur_tick = position;
221 tmr->tick.fraction = 0;
222 spin_unlock_irqrestore(&tmr->lock, flags);
223 return 0;
224}
225
226/* set current real-time position */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100227int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
228 snd_seq_real_time_t position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 unsigned long flags;
231
232 snd_assert(tmr, return -EINVAL);
233
234 snd_seq_sanity_real_time(&position);
235 spin_lock_irqsave(&tmr->lock, flags);
236 tmr->cur_time = position;
237 spin_unlock_irqrestore(&tmr->lock, flags);
238 return 0;
239}
240
241/* set timer skew */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100242int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
243 unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 unsigned long flags;
246
247 snd_assert(tmr, return -EINVAL);
248
249 /* FIXME */
250 if (base != SKEW_BASE) {
251 snd_printd("invalid skew base 0x%x\n", base);
252 return -EINVAL;
253 }
254 spin_lock_irqsave(&tmr->lock, flags);
255 tmr->skew = skew;
256 spin_unlock_irqrestore(&tmr->lock, flags);
257 return 0;
258}
259
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100260int snd_seq_timer_open(struct snd_seq_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100262 struct snd_timer_instance *t;
263 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 char str[32];
265 int err;
266
267 tmr = q->timer;
268 snd_assert(tmr != NULL, return -EINVAL);
269 if (tmr->timeri)
270 return -EBUSY;
271 sprintf(str, "sequencer queue %i", q->queue);
272 if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
273 return -EINVAL;
274 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
275 tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
276 err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue);
277 if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
278 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
279 tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100280 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 memset(&tid, 0, sizeof(tid));
282 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
283 tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
284 tid.card = -1;
285 tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
286 err = snd_timer_open(&t, str, &tid, q->queue);
287 }
288 if (err < 0) {
289 snd_printk(KERN_ERR "seq fatal error: cannot create timer (%i)\n", err);
290 return err;
291 }
292 }
293 t->callback = snd_seq_timer_interrupt;
294 t->callback_data = q;
295 t->flags |= SNDRV_TIMER_IFLG_AUTO;
296 tmr->timeri = t;
297 return 0;
298}
299
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100300int snd_seq_timer_close(struct snd_seq_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100302 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 tmr = q->timer;
305 snd_assert(tmr != NULL, return -EINVAL);
306 if (tmr->timeri) {
307 snd_timer_stop(tmr->timeri);
308 snd_timer_close(tmr->timeri);
309 tmr->timeri = NULL;
310 }
311 return 0;
312}
313
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100314int snd_seq_timer_stop(struct snd_seq_timer * tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 if (! tmr->timeri)
317 return -EINVAL;
318 if (!tmr->running)
319 return 0;
320 tmr->running = 0;
321 snd_timer_pause(tmr->timeri);
322 return 0;
323}
324
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100325static int initialize_timer(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100327 struct snd_timer *t;
Clemens Ladisch87ef7772005-10-19 14:38:30 +0200328 unsigned long freq;
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 t = tmr->timeri->timer;
331 snd_assert(t, return -EINVAL);
332
Clemens Ladisch87ef7772005-10-19 14:38:30 +0200333 freq = tmr->preferred_resolution;
334 if (!freq)
335 freq = DEFAULT_FREQUENCY;
336 else if (freq < MIN_FREQUENCY)
337 freq = MIN_FREQUENCY;
338 else if (freq > MAX_FREQUENCY)
339 freq = MAX_FREQUENCY;
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 tmr->ticks = 1;
Clemens Ladisch87ef7772005-10-19 14:38:30 +0200342 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 unsigned long r = t->hw.resolution;
344 if (! r && t->hw.c_resolution)
345 r = t->hw.c_resolution(t);
346 if (r) {
Clemens Ladisch87ef7772005-10-19 14:38:30 +0200347 tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (! tmr->ticks)
349 tmr->ticks = 1;
350 }
351 }
352 tmr->initialized = 1;
353 return 0;
354}
355
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100356int snd_seq_timer_start(struct snd_seq_timer * tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 if (! tmr->timeri)
359 return -EINVAL;
360 if (tmr->running)
361 snd_seq_timer_stop(tmr);
362 snd_seq_timer_reset(tmr);
363 if (initialize_timer(tmr) < 0)
364 return -EINVAL;
365 snd_timer_start(tmr->timeri, tmr->ticks);
366 tmr->running = 1;
367 do_gettimeofday(&tmr->last_update);
368 return 0;
369}
370
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100371int snd_seq_timer_continue(struct snd_seq_timer * tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 if (! tmr->timeri)
374 return -EINVAL;
375 if (tmr->running)
376 return -EBUSY;
377 if (! tmr->initialized) {
378 snd_seq_timer_reset(tmr);
379 if (initialize_timer(tmr) < 0)
380 return -EINVAL;
381 }
382 snd_timer_start(tmr->timeri, tmr->ticks);
383 tmr->running = 1;
384 do_gettimeofday(&tmr->last_update);
385 return 0;
386}
387
388/* return current 'real' time. use timeofday() to get better granularity. */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100389snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 snd_seq_real_time_t cur_time;
392
393 cur_time = tmr->cur_time;
394 if (tmr->running) {
395 struct timeval tm;
396 int usec;
397 do_gettimeofday(&tm);
398 usec = (int)(tm.tv_usec - tmr->last_update.tv_usec);
399 if (usec < 0) {
400 cur_time.tv_nsec += (1000000 + usec) * 1000;
401 cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec - 1;
402 } else {
403 cur_time.tv_nsec += usec * 1000;
404 cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec;
405 }
406 snd_seq_sanity_real_time(&cur_time);
407 }
408
409 return cur_time;
410}
411
412/* TODO: use interpolation on tick queue (will only be useful for very
413 high PPQ values) */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100414snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 return tmr->tick.cur_tick;
417}
418
419
Takashi Iwai04f141a2005-12-01 10:43:51 +0100420#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/* exported to seq_info.c */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100422void snd_seq_info_timer_read(struct snd_info_entry *entry,
423 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 int idx;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100426 struct snd_seq_queue *q;
427 struct snd_seq_timer *tmr;
428 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 unsigned long resolution;
430
431 for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
432 q = queueptr(idx);
433 if (q == NULL)
434 continue;
435 if ((tmr = q->timer) == NULL ||
436 (ti = tmr->timeri) == NULL) {
437 queuefree(q);
438 continue;
439 }
440 snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
441 resolution = snd_timer_resolution(ti) * tmr->ticks;
442 snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
443 snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
444 queuefree(q);
445 }
446}
Takashi Iwai04f141a2005-12-01 10:43:51 +0100447#endif /* CONFIG_PROC_FS */
448