Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / sound / core / seq / seq_timer.c
blob23167578231f3c002a79dd034b0e3d8c0b83eeea
1 /*
2 * ALSA sequencer Timer
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"
27 #include "seq_info.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;
40 else {
41 /* might overflow.. */
42 unsigned int s;
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);
59 if (!tmr)
60 return NULL;
61 spin_lock_init(&tmr->lock);
63 /* reset setup to defaults */
64 snd_seq_timer_defaults(tmr);
66 /* reset time */
67 snd_seq_timer_reset(tmr);
69 return tmr;
72 /* delete timer (destructor) */
73 void snd_seq_timer_delete(struct snd_seq_timer **tmr)
75 struct snd_seq_timer *t = *tmr;
76 *tmr = NULL;
78 if (t == NULL) {
79 pr_debug("ALSA: seq: snd_seq_timer_delete() called with NULL timer\n");
80 return;
82 t->running = 0;
84 /* reset time */
85 snd_seq_timer_stop(t);
86 snd_seq_timer_reset(t);
88 kfree(t);
91 void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
93 unsigned long flags;
95 spin_lock_irqsave(&tmr->lock, flags);
96 /* setup defaults */
97 tmr->ppq = 96; /* 96 PPQ */
98 tmr->tempo = 500000; /* 120 BPM */
99 snd_seq_timer_set_tick_resolution(tmr);
100 tmr->running = 0;
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)
126 unsigned long flags;
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,
137 unsigned long ticks)
139 unsigned long flags;
140 struct snd_seq_queue *q = timeri->callback_data;
141 struct snd_seq_timer *tmr;
143 if (q == NULL)
144 return;
145 tmr = q->timer;
146 if (tmr == NULL)
147 return;
148 spin_lock_irqsave(&tmr->lock, flags);
149 if (!tmr->running) {
150 spin_unlock_irqrestore(&tmr->lock, flags);
151 return;
154 resolution *= ticks;
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);
161 /* update timer */
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)
179 unsigned long flags;
181 if (snd_BUG_ON(!tmr))
182 return -EINVAL;
183 if (tempo <= 0)
184 return -EINVAL;
185 spin_lock_irqsave(&tmr->lock, flags);
186 if ((unsigned int)tempo != tmr->tempo) {
187 tmr->tempo = tempo;
188 snd_seq_timer_set_tick_resolution(tmr);
190 spin_unlock_irqrestore(&tmr->lock, flags);
191 return 0;
194 /* set current tempo and ppq in a shot */
195 int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq)
197 int changed;
198 unsigned long flags;
200 if (snd_BUG_ON(!tmr))
201 return -EINVAL;
202 if (tempo <= 0 || ppq <= 0)
203 return -EINVAL;
204 spin_lock_irqsave(&tmr->lock, flags);
205 if (tmr->running && (ppq != tmr->ppq)) {
206 /* refuse to change ppq on running timers */
207 /* because it will upset the song position (ticks) */
208 spin_unlock_irqrestore(&tmr->lock, flags);
209 pr_debug("ALSA: seq: cannot change ppq of a running timer\n");
210 return -EBUSY;
212 changed = (tempo != tmr->tempo) || (ppq != tmr->ppq);
213 tmr->tempo = tempo;
214 tmr->ppq = ppq;
215 if (changed)
216 snd_seq_timer_set_tick_resolution(tmr);
217 spin_unlock_irqrestore(&tmr->lock, flags);
218 return 0;
221 /* set current tick position */
222 int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
223 snd_seq_tick_time_t position)
225 unsigned long flags;
227 if (snd_BUG_ON(!tmr))
228 return -EINVAL;
230 spin_lock_irqsave(&tmr->lock, flags);
231 tmr->tick.cur_tick = position;
232 tmr->tick.fraction = 0;
233 spin_unlock_irqrestore(&tmr->lock, flags);
234 return 0;
237 /* set current real-time position */
238 int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
239 snd_seq_real_time_t position)
241 unsigned long flags;
243 if (snd_BUG_ON(!tmr))
244 return -EINVAL;
246 snd_seq_sanity_real_time(&position);
247 spin_lock_irqsave(&tmr->lock, flags);
248 tmr->cur_time = position;
249 spin_unlock_irqrestore(&tmr->lock, flags);
250 return 0;
253 /* set timer skew */
254 int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
255 unsigned int base)
257 unsigned long flags;
259 if (snd_BUG_ON(!tmr))
260 return -EINVAL;
262 /* FIXME */
263 if (base != SKEW_BASE) {
264 pr_debug("ALSA: seq: invalid skew base 0x%x\n", base);
265 return -EINVAL;
267 spin_lock_irqsave(&tmr->lock, flags);
268 tmr->skew = skew;
269 spin_unlock_irqrestore(&tmr->lock, flags);
270 return 0;
273 int snd_seq_timer_open(struct snd_seq_queue *q)
275 struct snd_timer_instance *t;
276 struct snd_seq_timer *tmr;
277 char str[32];
278 int err;
280 tmr = q->timer;
281 if (snd_BUG_ON(!tmr))
282 return -EINVAL;
283 if (tmr->timeri)
284 return -EBUSY;
285 sprintf(str, "sequencer queue %i", q->queue);
286 if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
287 return -EINVAL;
288 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
289 tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
290 err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue);
291 if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
292 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
293 tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
294 struct snd_timer_id tid;
295 memset(&tid, 0, sizeof(tid));
296 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
297 tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
298 tid.card = -1;
299 tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
300 err = snd_timer_open(&t, str, &tid, q->queue);
303 if (err < 0) {
304 pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err);
305 return err;
307 t->callback = snd_seq_timer_interrupt;
308 t->callback_data = q;
309 t->flags |= SNDRV_TIMER_IFLG_AUTO;
310 spin_lock_irq(&tmr->lock);
311 tmr->timeri = t;
312 spin_unlock_irq(&tmr->lock);
313 return 0;
316 int snd_seq_timer_close(struct snd_seq_queue *q)
318 struct snd_seq_timer *tmr;
319 struct snd_timer_instance *t;
321 tmr = q->timer;
322 if (snd_BUG_ON(!tmr))
323 return -EINVAL;
324 spin_lock_irq(&tmr->lock);
325 t = tmr->timeri;
326 tmr->timeri = NULL;
327 spin_unlock_irq(&tmr->lock);
328 if (t)
329 snd_timer_close(t);
330 return 0;
333 static int seq_timer_stop(struct snd_seq_timer *tmr)
335 if (! tmr->timeri)
336 return -EINVAL;
337 if (!tmr->running)
338 return 0;
339 tmr->running = 0;
340 snd_timer_pause(tmr->timeri);
341 return 0;
344 int snd_seq_timer_stop(struct snd_seq_timer *tmr)
346 unsigned long flags;
347 int err;
349 spin_lock_irqsave(&tmr->lock, flags);
350 err = seq_timer_stop(tmr);
351 spin_unlock_irqrestore(&tmr->lock, flags);
352 return err;
355 static int initialize_timer(struct snd_seq_timer *tmr)
357 struct snd_timer *t;
358 unsigned long freq;
360 t = tmr->timeri->timer;
361 if (!t)
362 return -EINVAL;
364 freq = tmr->preferred_resolution;
365 if (!freq)
366 freq = DEFAULT_FREQUENCY;
367 else if (freq < MIN_FREQUENCY)
368 freq = MIN_FREQUENCY;
369 else if (freq > MAX_FREQUENCY)
370 freq = MAX_FREQUENCY;
372 tmr->ticks = 1;
373 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
374 unsigned long r = t->hw.resolution;
375 if (! r && t->hw.c_resolution)
376 r = t->hw.c_resolution(t);
377 if (r) {
378 tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
379 if (! tmr->ticks)
380 tmr->ticks = 1;
383 tmr->initialized = 1;
384 return 0;
387 static int seq_timer_start(struct snd_seq_timer *tmr)
389 if (! tmr->timeri)
390 return -EINVAL;
391 if (tmr->running)
392 seq_timer_stop(tmr);
393 seq_timer_reset(tmr);
394 if (initialize_timer(tmr) < 0)
395 return -EINVAL;
396 snd_timer_start(tmr->timeri, tmr->ticks);
397 tmr->running = 1;
398 ktime_get_ts64(&tmr->last_update);
399 return 0;
402 int snd_seq_timer_start(struct snd_seq_timer *tmr)
404 unsigned long flags;
405 int err;
407 spin_lock_irqsave(&tmr->lock, flags);
408 err = seq_timer_start(tmr);
409 spin_unlock_irqrestore(&tmr->lock, flags);
410 return err;
413 static int seq_timer_continue(struct snd_seq_timer *tmr)
415 if (! tmr->timeri)
416 return -EINVAL;
417 if (tmr->running)
418 return -EBUSY;
419 if (! tmr->initialized) {
420 seq_timer_reset(tmr);
421 if (initialize_timer(tmr) < 0)
422 return -EINVAL;
424 snd_timer_start(tmr->timeri, tmr->ticks);
425 tmr->running = 1;
426 ktime_get_ts64(&tmr->last_update);
427 return 0;
430 int snd_seq_timer_continue(struct snd_seq_timer *tmr)
432 unsigned long flags;
433 int err;
435 spin_lock_irqsave(&tmr->lock, flags);
436 err = seq_timer_continue(tmr);
437 spin_unlock_irqrestore(&tmr->lock, flags);
438 return err;
441 /* return current 'real' time. use timeofday() to get better granularity. */
442 snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr)
444 snd_seq_real_time_t cur_time;
445 unsigned long flags;
447 spin_lock_irqsave(&tmr->lock, flags);
448 cur_time = tmr->cur_time;
449 if (tmr->running) {
450 struct timespec64 tm;
452 ktime_get_ts64(&tm);
453 tm = timespec64_sub(tm, tmr->last_update);
454 cur_time.tv_nsec += tm.tv_nsec;
455 cur_time.tv_sec += tm.tv_sec;
456 snd_seq_sanity_real_time(&cur_time);
458 spin_unlock_irqrestore(&tmr->lock, flags);
459 return cur_time;
462 /* TODO: use interpolation on tick queue (will only be useful for very
463 high PPQ values) */
464 snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
466 return tmr->tick.cur_tick;
470 #ifdef CONFIG_SND_PROC_FS
471 /* exported to seq_info.c */
472 void snd_seq_info_timer_read(struct snd_info_entry *entry,
473 struct snd_info_buffer *buffer)
475 int idx;
476 struct snd_seq_queue *q;
477 struct snd_seq_timer *tmr;
478 struct snd_timer_instance *ti;
479 unsigned long resolution;
481 for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
482 q = queueptr(idx);
483 if (q == NULL)
484 continue;
485 if ((tmr = q->timer) == NULL ||
486 (ti = tmr->timeri) == NULL) {
487 queuefree(q);
488 continue;
490 snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
491 resolution = snd_timer_resolution(ti) * tmr->ticks;
492 snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
493 snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
494 queuefree(q);
497 #endif /* CONFIG_SND_PROC_FS */