1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
5 * Jaroslav Kysela <perex@perex.cz>
8 #include <sound/core.h>
9 #include <linux/slab.h>
10 #include "seq_timer.h"
11 #include "seq_queue.h"
14 /* allowed sequencer timer frequencies, in Hz */
15 #define MIN_FREQUENCY 10
16 #define MAX_FREQUENCY 6250
17 #define DEFAULT_FREQUENCY 1000
19 #define SKEW_BASE 0x10000 /* 16bit shift */
21 static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer *tmr)
23 if (tmr->tempo < 1000000)
24 tmr->tick.resolution = (tmr->tempo * 1000) / tmr->ppq;
26 /* might overflow.. */
28 s = tmr->tempo % tmr->ppq;
29 s = (s * 1000) / tmr->ppq;
30 tmr->tick.resolution = (tmr->tempo / tmr->ppq) * 1000;
31 tmr->tick.resolution += s;
33 if (tmr->tick.resolution <= 0)
34 tmr->tick.resolution = 1;
35 snd_seq_timer_update_tick(&tmr->tick, 0);
38 /* create new timer (constructor) */
39 struct snd_seq_timer *snd_seq_timer_new(void)
41 struct snd_seq_timer *tmr;
43 tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
46 spin_lock_init(&tmr->lock);
48 /* reset setup to defaults */
49 snd_seq_timer_defaults(tmr);
52 snd_seq_timer_reset(tmr);
57 /* delete timer (destructor) */
58 void snd_seq_timer_delete(struct snd_seq_timer **tmr)
60 struct snd_seq_timer *t = *tmr;
64 pr_debug("ALSA: seq: snd_seq_timer_delete() called with NULL timer\n");
70 snd_seq_timer_stop(t);
71 snd_seq_timer_reset(t);
76 void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
80 spin_lock_irqsave(&tmr->lock, flags);
82 tmr->ppq = 96; /* 96 PPQ */
83 tmr->tempo = 500000; /* 120 BPM */
84 snd_seq_timer_set_tick_resolution(tmr);
87 tmr->type = SNDRV_SEQ_TIMER_ALSA;
88 tmr->alsa_id.dev_class = seq_default_timer_class;
89 tmr->alsa_id.dev_sclass = seq_default_timer_sclass;
90 tmr->alsa_id.card = seq_default_timer_card;
91 tmr->alsa_id.device = seq_default_timer_device;
92 tmr->alsa_id.subdevice = seq_default_timer_subdevice;
93 tmr->preferred_resolution = seq_default_timer_resolution;
95 tmr->skew = tmr->skew_base = SKEW_BASE;
96 spin_unlock_irqrestore(&tmr->lock, flags);
99 static void seq_timer_reset(struct snd_seq_timer *tmr)
101 /* reset time & songposition */
102 tmr->cur_time.tv_sec = 0;
103 tmr->cur_time.tv_nsec = 0;
105 tmr->tick.cur_tick = 0;
106 tmr->tick.fraction = 0;
109 void snd_seq_timer_reset(struct snd_seq_timer *tmr)
113 spin_lock_irqsave(&tmr->lock, flags);
114 seq_timer_reset(tmr);
115 spin_unlock_irqrestore(&tmr->lock, flags);
119 /* called by timer interrupt routine. the period time since previous invocation is passed */
120 static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
121 unsigned long resolution,
125 struct snd_seq_queue *q = timeri->callback_data;
126 struct snd_seq_timer *tmr;
133 spin_lock_irqsave(&tmr->lock, flags);
135 spin_unlock_irqrestore(&tmr->lock, flags);
140 if (tmr->skew != tmr->skew_base) {
141 /* FIXME: assuming skew_base = 0x10000 */
142 resolution = (resolution >> 16) * tmr->skew +
143 (((resolution & 0xffff) * tmr->skew) >> 16);
147 snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
149 /* calculate current tick */
150 snd_seq_timer_update_tick(&tmr->tick, resolution);
152 /* register actual time of this timer update */
153 ktime_get_ts64(&tmr->last_update);
155 spin_unlock_irqrestore(&tmr->lock, flags);
157 /* check queues and dispatch events */
158 snd_seq_check_queue(q, 1, 0);
161 /* set current tempo */
162 int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
166 if (snd_BUG_ON(!tmr))
170 spin_lock_irqsave(&tmr->lock, flags);
171 if ((unsigned int)tempo != tmr->tempo) {
173 snd_seq_timer_set_tick_resolution(tmr);
175 spin_unlock_irqrestore(&tmr->lock, flags);
179 /* set current tempo and ppq in a shot */
180 int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq)
185 if (snd_BUG_ON(!tmr))
187 if (tempo <= 0 || ppq <= 0)
189 spin_lock_irqsave(&tmr->lock, flags);
190 if (tmr->running && (ppq != tmr->ppq)) {
191 /* refuse to change ppq on running timers */
192 /* because it will upset the song position (ticks) */
193 spin_unlock_irqrestore(&tmr->lock, flags);
194 pr_debug("ALSA: seq: cannot change ppq of a running timer\n");
197 changed = (tempo != tmr->tempo) || (ppq != tmr->ppq);
201 snd_seq_timer_set_tick_resolution(tmr);
202 spin_unlock_irqrestore(&tmr->lock, flags);
206 /* set current tick position */
207 int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
208 snd_seq_tick_time_t position)
212 if (snd_BUG_ON(!tmr))
215 spin_lock_irqsave(&tmr->lock, flags);
216 tmr->tick.cur_tick = position;
217 tmr->tick.fraction = 0;
218 spin_unlock_irqrestore(&tmr->lock, flags);
222 /* set current real-time position */
223 int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
224 snd_seq_real_time_t position)
228 if (snd_BUG_ON(!tmr))
231 snd_seq_sanity_real_time(&position);
232 spin_lock_irqsave(&tmr->lock, flags);
233 tmr->cur_time = position;
234 spin_unlock_irqrestore(&tmr->lock, flags);
239 int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
244 if (snd_BUG_ON(!tmr))
248 if (base != SKEW_BASE) {
249 pr_debug("ALSA: seq: invalid skew base 0x%x\n", base);
252 spin_lock_irqsave(&tmr->lock, flags);
254 spin_unlock_irqrestore(&tmr->lock, flags);
258 int snd_seq_timer_open(struct snd_seq_queue *q)
260 struct snd_timer_instance *t;
261 struct snd_seq_timer *tmr;
266 if (snd_BUG_ON(!tmr))
270 sprintf(str, "sequencer queue %i", q->queue);
271 if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
273 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
274 tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
275 t = snd_timer_instance_new(str);
278 t->callback = snd_seq_timer_interrupt;
279 t->callback_data = q;
280 t->flags |= SNDRV_TIMER_IFLG_AUTO;
281 err = snd_timer_open(t, &tmr->alsa_id, q->queue);
282 if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
283 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
284 tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
285 struct snd_timer_id tid;
286 memset(&tid, 0, sizeof(tid));
287 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
288 tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
290 tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
291 err = snd_timer_open(t, &tid, q->queue);
295 pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err);
296 snd_timer_instance_free(t);
299 spin_lock_irq(&tmr->lock);
301 spin_unlock_irq(&tmr->lock);
305 int snd_seq_timer_close(struct snd_seq_queue *q)
307 struct snd_seq_timer *tmr;
308 struct snd_timer_instance *t;
311 if (snd_BUG_ON(!tmr))
313 spin_lock_irq(&tmr->lock);
316 spin_unlock_irq(&tmr->lock);
319 snd_timer_instance_free(t);
324 static int seq_timer_stop(struct snd_seq_timer *tmr)
331 snd_timer_pause(tmr->timeri);
335 int snd_seq_timer_stop(struct snd_seq_timer *tmr)
340 spin_lock_irqsave(&tmr->lock, flags);
341 err = seq_timer_stop(tmr);
342 spin_unlock_irqrestore(&tmr->lock, flags);
346 static int initialize_timer(struct snd_seq_timer *tmr)
351 t = tmr->timeri->timer;
355 freq = tmr->preferred_resolution;
357 freq = DEFAULT_FREQUENCY;
358 else if (freq < MIN_FREQUENCY)
359 freq = MIN_FREQUENCY;
360 else if (freq > MAX_FREQUENCY)
361 freq = MAX_FREQUENCY;
364 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
365 unsigned long r = snd_timer_resolution(tmr->timeri);
367 tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
372 tmr->initialized = 1;
376 static int seq_timer_start(struct snd_seq_timer *tmr)
382 seq_timer_reset(tmr);
383 if (initialize_timer(tmr) < 0)
385 snd_timer_start(tmr->timeri, tmr->ticks);
387 ktime_get_ts64(&tmr->last_update);
391 int snd_seq_timer_start(struct snd_seq_timer *tmr)
396 spin_lock_irqsave(&tmr->lock, flags);
397 err = seq_timer_start(tmr);
398 spin_unlock_irqrestore(&tmr->lock, flags);
402 static int seq_timer_continue(struct snd_seq_timer *tmr)
408 if (! tmr->initialized) {
409 seq_timer_reset(tmr);
410 if (initialize_timer(tmr) < 0)
413 snd_timer_start(tmr->timeri, tmr->ticks);
415 ktime_get_ts64(&tmr->last_update);
419 int snd_seq_timer_continue(struct snd_seq_timer *tmr)
424 spin_lock_irqsave(&tmr->lock, flags);
425 err = seq_timer_continue(tmr);
426 spin_unlock_irqrestore(&tmr->lock, flags);
430 /* return current 'real' time. use timeofday() to get better granularity. */
431 snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr,
434 snd_seq_real_time_t cur_time;
437 spin_lock_irqsave(&tmr->lock, flags);
438 cur_time = tmr->cur_time;
439 if (adjust_ktime && tmr->running) {
440 struct timespec64 tm;
443 tm = timespec64_sub(tm, tmr->last_update);
444 cur_time.tv_nsec += tm.tv_nsec;
445 cur_time.tv_sec += tm.tv_sec;
446 snd_seq_sanity_real_time(&cur_time);
448 spin_unlock_irqrestore(&tmr->lock, flags);
452 /* TODO: use interpolation on tick queue (will only be useful for very
454 snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
456 snd_seq_tick_time_t cur_tick;
459 spin_lock_irqsave(&tmr->lock, flags);
460 cur_tick = tmr->tick.cur_tick;
461 spin_unlock_irqrestore(&tmr->lock, flags);
466 #ifdef CONFIG_SND_PROC_FS
467 /* exported to seq_info.c */
468 void snd_seq_info_timer_read(struct snd_info_entry *entry,
469 struct snd_info_buffer *buffer)
472 struct snd_seq_queue *q;
473 struct snd_seq_timer *tmr;
474 struct snd_timer_instance *ti;
475 unsigned long resolution;
477 for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
481 mutex_lock(&q->timer_mutex);
488 snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
489 resolution = snd_timer_resolution(ti) * tmr->ticks;
490 snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
491 snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
493 mutex_unlock(&q->timer_mutex);
497 #endif /* CONFIG_SND_PROC_FS */