Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * ALSA sequencer Timer | |
4 | * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl> | |
c1017a4c | 5 | * Jaroslav Kysela <perex@perex.cz> |
1da177e4 LT |
6 | */ |
7 | ||
1da177e4 LT |
8 | #include <sound/core.h> |
9 | #include <linux/slab.h> | |
10 | #include "seq_timer.h" | |
11 | #include "seq_queue.h" | |
12 | #include "seq_info.h" | |
13 | ||
87ef7779 CL |
14 | /* allowed sequencer timer frequencies, in Hz */ |
15 | #define MIN_FREQUENCY 10 | |
16 | #define MAX_FREQUENCY 6250 | |
17 | #define DEFAULT_FREQUENCY 1000 | |
18 | ||
1da177e4 LT |
19 | #define SKEW_BASE 0x10000 /* 16bit shift */ |
20 | ||
a32f6674 | 21 | static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer *tmr) |
1da177e4 | 22 | { |
a32f6674 CL |
23 | if (tmr->tempo < 1000000) |
24 | tmr->tick.resolution = (tmr->tempo * 1000) / tmr->ppq; | |
1da177e4 LT |
25 | else { |
26 | /* might overflow.. */ | |
27 | unsigned int s; | |
a32f6674 CL |
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; | |
1da177e4 | 32 | } |
a32f6674 CL |
33 | if (tmr->tick.resolution <= 0) |
34 | tmr->tick.resolution = 1; | |
35 | snd_seq_timer_update_tick(&tmr->tick, 0); | |
1da177e4 LT |
36 | } |
37 | ||
38 | /* create new timer (constructor) */ | |
c7e0b5bf | 39 | struct snd_seq_timer *snd_seq_timer_new(void) |
1da177e4 | 40 | { |
c7e0b5bf | 41 | struct snd_seq_timer *tmr; |
1da177e4 | 42 | |
ecca82b4 | 43 | tmr = kzalloc(sizeof(*tmr), GFP_KERNEL); |
24db8bba | 44 | if (!tmr) |
1da177e4 | 45 | return NULL; |
1da177e4 LT |
46 | spin_lock_init(&tmr->lock); |
47 | ||
48 | /* reset setup to defaults */ | |
49 | snd_seq_timer_defaults(tmr); | |
50 | ||
51 | /* reset time */ | |
52 | snd_seq_timer_reset(tmr); | |
53 | ||
54 | return tmr; | |
55 | } | |
56 | ||
57 | /* delete timer (destructor) */ | |
c7e0b5bf | 58 | void snd_seq_timer_delete(struct snd_seq_timer **tmr) |
1da177e4 | 59 | { |
c7e0b5bf | 60 | struct snd_seq_timer *t = *tmr; |
1da177e4 LT |
61 | *tmr = NULL; |
62 | ||
63 | if (t == NULL) { | |
04cc79a0 | 64 | pr_debug("ALSA: seq: snd_seq_timer_delete() called with NULL timer\n"); |
1da177e4 LT |
65 | return; |
66 | } | |
67 | t->running = 0; | |
68 | ||
69 | /* reset time */ | |
70 | snd_seq_timer_stop(t); | |
71 | snd_seq_timer_reset(t); | |
72 | ||
73 | kfree(t); | |
74 | } | |
75 | ||
c7e0b5bf | 76 | void snd_seq_timer_defaults(struct snd_seq_timer * tmr) |
1da177e4 | 77 | { |
2cdc7b63 TI |
78 | unsigned long flags; |
79 | ||
80 | spin_lock_irqsave(&tmr->lock, flags); | |
1da177e4 LT |
81 | /* setup defaults */ |
82 | tmr->ppq = 96; /* 96 PPQ */ | |
83 | tmr->tempo = 500000; /* 120 BPM */ | |
a32f6674 | 84 | snd_seq_timer_set_tick_resolution(tmr); |
1da177e4 LT |
85 | tmr->running = 0; |
86 | ||
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; | |
94 | ||
95 | tmr->skew = tmr->skew_base = SKEW_BASE; | |
2cdc7b63 | 96 | spin_unlock_irqrestore(&tmr->lock, flags); |
1da177e4 LT |
97 | } |
98 | ||
2cdc7b63 | 99 | static void seq_timer_reset(struct snd_seq_timer *tmr) |
1da177e4 | 100 | { |
1da177e4 LT |
101 | /* reset time & songposition */ |
102 | tmr->cur_time.tv_sec = 0; | |
103 | tmr->cur_time.tv_nsec = 0; | |
104 | ||
105 | tmr->tick.cur_tick = 0; | |
106 | tmr->tick.fraction = 0; | |
2cdc7b63 TI |
107 | } |
108 | ||
109 | void snd_seq_timer_reset(struct snd_seq_timer *tmr) | |
110 | { | |
111 | unsigned long flags; | |
1da177e4 | 112 | |
2cdc7b63 TI |
113 | spin_lock_irqsave(&tmr->lock, flags); |
114 | seq_timer_reset(tmr); | |
1da177e4 LT |
115 | spin_unlock_irqrestore(&tmr->lock, flags); |
116 | } | |
117 | ||
118 | ||
119 | /* called by timer interrupt routine. the period time since previous invocation is passed */ | |
c7e0b5bf | 120 | static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri, |
1da177e4 LT |
121 | unsigned long resolution, |
122 | unsigned long ticks) | |
123 | { | |
124 | unsigned long flags; | |
c7e0b5bf TI |
125 | struct snd_seq_queue *q = timeri->callback_data; |
126 | struct snd_seq_timer *tmr; | |
1da177e4 LT |
127 | |
128 | if (q == NULL) | |
129 | return; | |
130 | tmr = q->timer; | |
131 | if (tmr == NULL) | |
132 | return; | |
2cdc7b63 TI |
133 | spin_lock_irqsave(&tmr->lock, flags); |
134 | if (!tmr->running) { | |
135 | spin_unlock_irqrestore(&tmr->lock, flags); | |
1da177e4 | 136 | return; |
2cdc7b63 | 137 | } |
1da177e4 LT |
138 | |
139 | resolution *= ticks; | |
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); | |
144 | } | |
145 | ||
1da177e4 LT |
146 | /* update timer */ |
147 | snd_seq_inc_time_nsec(&tmr->cur_time, resolution); | |
148 | ||
149 | /* calculate current tick */ | |
150 | snd_seq_timer_update_tick(&tmr->tick, resolution); | |
151 | ||
152 | /* register actual time of this timer update */ | |
3915bf29 | 153 | ktime_get_ts64(&tmr->last_update); |
1da177e4 LT |
154 | |
155 | spin_unlock_irqrestore(&tmr->lock, flags); | |
156 | ||
157 | /* check queues and dispatch events */ | |
158 | snd_seq_check_queue(q, 1, 0); | |
159 | } | |
160 | ||
161 | /* set current tempo */ | |
c7e0b5bf | 162 | int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo) |
1da177e4 LT |
163 | { |
164 | unsigned long flags; | |
165 | ||
7eaa943c TI |
166 | if (snd_BUG_ON(!tmr)) |
167 | return -EINVAL; | |
1da177e4 LT |
168 | if (tempo <= 0) |
169 | return -EINVAL; | |
170 | spin_lock_irqsave(&tmr->lock, flags); | |
171 | if ((unsigned int)tempo != tmr->tempo) { | |
172 | tmr->tempo = tempo; | |
a32f6674 | 173 | snd_seq_timer_set_tick_resolution(tmr); |
1da177e4 LT |
174 | } |
175 | spin_unlock_irqrestore(&tmr->lock, flags); | |
176 | return 0; | |
177 | } | |
178 | ||
671ec859 TI |
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) | |
1da177e4 | 181 | { |
671ec859 | 182 | int changed; |
1da177e4 LT |
183 | unsigned long flags; |
184 | ||
7eaa943c TI |
185 | if (snd_BUG_ON(!tmr)) |
186 | return -EINVAL; | |
671ec859 | 187 | if (tempo <= 0 || ppq <= 0) |
1da177e4 LT |
188 | return -EINVAL; |
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); | |
04cc79a0 | 194 | pr_debug("ALSA: seq: cannot change ppq of a running timer\n"); |
1da177e4 LT |
195 | return -EBUSY; |
196 | } | |
671ec859 TI |
197 | changed = (tempo != tmr->tempo) || (ppq != tmr->ppq); |
198 | tmr->tempo = tempo; | |
1da177e4 | 199 | tmr->ppq = ppq; |
671ec859 TI |
200 | if (changed) |
201 | snd_seq_timer_set_tick_resolution(tmr); | |
1da177e4 LT |
202 | spin_unlock_irqrestore(&tmr->lock, flags); |
203 | return 0; | |
204 | } | |
205 | ||
206 | /* set current tick position */ | |
c7e0b5bf TI |
207 | int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr, |
208 | snd_seq_tick_time_t position) | |
1da177e4 LT |
209 | { |
210 | unsigned long flags; | |
211 | ||
7eaa943c TI |
212 | if (snd_BUG_ON(!tmr)) |
213 | return -EINVAL; | |
1da177e4 LT |
214 | |
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); | |
219 | return 0; | |
220 | } | |
221 | ||
222 | /* set current real-time position */ | |
c7e0b5bf TI |
223 | int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr, |
224 | snd_seq_real_time_t position) | |
1da177e4 LT |
225 | { |
226 | unsigned long flags; | |
227 | ||
7eaa943c TI |
228 | if (snd_BUG_ON(!tmr)) |
229 | return -EINVAL; | |
1da177e4 LT |
230 | |
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); | |
235 | return 0; | |
236 | } | |
237 | ||
238 | /* set timer skew */ | |
c7e0b5bf TI |
239 | int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew, |
240 | unsigned int base) | |
1da177e4 LT |
241 | { |
242 | unsigned long flags; | |
243 | ||
7eaa943c TI |
244 | if (snd_BUG_ON(!tmr)) |
245 | return -EINVAL; | |
1da177e4 LT |
246 | |
247 | /* FIXME */ | |
248 | if (base != SKEW_BASE) { | |
04cc79a0 | 249 | pr_debug("ALSA: seq: invalid skew base 0x%x\n", base); |
1da177e4 LT |
250 | return -EINVAL; |
251 | } | |
252 | spin_lock_irqsave(&tmr->lock, flags); | |
253 | tmr->skew = skew; | |
254 | spin_unlock_irqrestore(&tmr->lock, flags); | |
255 | return 0; | |
256 | } | |
257 | ||
c7e0b5bf | 258 | int snd_seq_timer_open(struct snd_seq_queue *q) |
1da177e4 | 259 | { |
c7e0b5bf TI |
260 | struct snd_timer_instance *t; |
261 | struct snd_seq_timer *tmr; | |
1da177e4 LT |
262 | char str[32]; |
263 | int err; | |
264 | ||
265 | tmr = q->timer; | |
7eaa943c TI |
266 | if (snd_BUG_ON(!tmr)) |
267 | return -EINVAL; | |
1da177e4 LT |
268 | if (tmr->timeri) |
269 | return -EBUSY; | |
270 | sprintf(str, "sequencer queue %i", q->queue); | |
271 | if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */ | |
272 | return -EINVAL; | |
273 | if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) | |
274 | tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER; | |
275 | err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue); | |
276 | if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) { | |
277 | if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL || | |
278 | tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) { | |
c7e0b5bf | 279 | struct snd_timer_id tid; |
1da177e4 LT |
280 | memset(&tid, 0, sizeof(tid)); |
281 | tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL; | |
282 | tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER; | |
283 | tid.card = -1; | |
284 | tid.device = SNDRV_TIMER_GLOBAL_SYSTEM; | |
285 | err = snd_timer_open(&t, str, &tid, q->queue); | |
286 | } | |
66efdc71 TI |
287 | } |
288 | if (err < 0) { | |
04cc79a0 | 289 | pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err); |
66efdc71 | 290 | return err; |
1da177e4 LT |
291 | } |
292 | t->callback = snd_seq_timer_interrupt; | |
293 | t->callback_data = q; | |
294 | t->flags |= SNDRV_TIMER_IFLG_AUTO; | |
2cdc7b63 | 295 | spin_lock_irq(&tmr->lock); |
1da177e4 | 296 | tmr->timeri = t; |
2cdc7b63 | 297 | spin_unlock_irq(&tmr->lock); |
1da177e4 LT |
298 | return 0; |
299 | } | |
300 | ||
c7e0b5bf | 301 | int snd_seq_timer_close(struct snd_seq_queue *q) |
1da177e4 | 302 | { |
c7e0b5bf | 303 | struct snd_seq_timer *tmr; |
2cdc7b63 | 304 | struct snd_timer_instance *t; |
1da177e4 LT |
305 | |
306 | tmr = q->timer; | |
7eaa943c TI |
307 | if (snd_BUG_ON(!tmr)) |
308 | return -EINVAL; | |
2cdc7b63 TI |
309 | spin_lock_irq(&tmr->lock); |
310 | t = tmr->timeri; | |
311 | tmr->timeri = NULL; | |
312 | spin_unlock_irq(&tmr->lock); | |
313 | if (t) | |
314 | snd_timer_close(t); | |
1da177e4 LT |
315 | return 0; |
316 | } | |
317 | ||
2cdc7b63 | 318 | static int seq_timer_stop(struct snd_seq_timer *tmr) |
1da177e4 LT |
319 | { |
320 | if (! tmr->timeri) | |
321 | return -EINVAL; | |
322 | if (!tmr->running) | |
323 | return 0; | |
324 | tmr->running = 0; | |
325 | snd_timer_pause(tmr->timeri); | |
326 | return 0; | |
327 | } | |
328 | ||
2cdc7b63 TI |
329 | int snd_seq_timer_stop(struct snd_seq_timer *tmr) |
330 | { | |
331 | unsigned long flags; | |
332 | int err; | |
333 | ||
334 | spin_lock_irqsave(&tmr->lock, flags); | |
335 | err = seq_timer_stop(tmr); | |
336 | spin_unlock_irqrestore(&tmr->lock, flags); | |
337 | return err; | |
338 | } | |
339 | ||
c7e0b5bf | 340 | static int initialize_timer(struct snd_seq_timer *tmr) |
1da177e4 | 341 | { |
c7e0b5bf | 342 | struct snd_timer *t; |
87ef7779 CL |
343 | unsigned long freq; |
344 | ||
1da177e4 | 345 | t = tmr->timeri->timer; |
43a35428 | 346 | if (!t) |
7eaa943c | 347 | return -EINVAL; |
1da177e4 | 348 | |
87ef7779 CL |
349 | freq = tmr->preferred_resolution; |
350 | if (!freq) | |
351 | freq = DEFAULT_FREQUENCY; | |
352 | else if (freq < MIN_FREQUENCY) | |
353 | freq = MIN_FREQUENCY; | |
354 | else if (freq > MAX_FREQUENCY) | |
355 | freq = MAX_FREQUENCY; | |
356 | ||
1da177e4 | 357 | tmr->ticks = 1; |
87ef7779 | 358 | if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) { |
21244e3d | 359 | unsigned long r = snd_timer_resolution(tmr->timeri); |
1da177e4 | 360 | if (r) { |
87ef7779 | 361 | tmr->ticks = (unsigned int)(1000000000uL / (r * freq)); |
1da177e4 LT |
362 | if (! tmr->ticks) |
363 | tmr->ticks = 1; | |
364 | } | |
365 | } | |
366 | tmr->initialized = 1; | |
367 | return 0; | |
368 | } | |
369 | ||
2cdc7b63 | 370 | static int seq_timer_start(struct snd_seq_timer *tmr) |
1da177e4 LT |
371 | { |
372 | if (! tmr->timeri) | |
373 | return -EINVAL; | |
374 | if (tmr->running) | |
2cdc7b63 TI |
375 | seq_timer_stop(tmr); |
376 | seq_timer_reset(tmr); | |
1da177e4 LT |
377 | if (initialize_timer(tmr) < 0) |
378 | return -EINVAL; | |
379 | snd_timer_start(tmr->timeri, tmr->ticks); | |
380 | tmr->running = 1; | |
3915bf29 | 381 | ktime_get_ts64(&tmr->last_update); |
1da177e4 LT |
382 | return 0; |
383 | } | |
384 | ||
2cdc7b63 TI |
385 | int snd_seq_timer_start(struct snd_seq_timer *tmr) |
386 | { | |
387 | unsigned long flags; | |
388 | int err; | |
389 | ||
390 | spin_lock_irqsave(&tmr->lock, flags); | |
391 | err = seq_timer_start(tmr); | |
392 | spin_unlock_irqrestore(&tmr->lock, flags); | |
393 | return err; | |
394 | } | |
395 | ||
396 | static int seq_timer_continue(struct snd_seq_timer *tmr) | |
1da177e4 LT |
397 | { |
398 | if (! tmr->timeri) | |
399 | return -EINVAL; | |
400 | if (tmr->running) | |
401 | return -EBUSY; | |
402 | if (! tmr->initialized) { | |
2cdc7b63 | 403 | seq_timer_reset(tmr); |
1da177e4 LT |
404 | if (initialize_timer(tmr) < 0) |
405 | return -EINVAL; | |
406 | } | |
407 | snd_timer_start(tmr->timeri, tmr->ticks); | |
408 | tmr->running = 1; | |
3915bf29 | 409 | ktime_get_ts64(&tmr->last_update); |
1da177e4 LT |
410 | return 0; |
411 | } | |
412 | ||
2cdc7b63 TI |
413 | int snd_seq_timer_continue(struct snd_seq_timer *tmr) |
414 | { | |
415 | unsigned long flags; | |
416 | int err; | |
417 | ||
418 | spin_lock_irqsave(&tmr->lock, flags); | |
419 | err = seq_timer_continue(tmr); | |
420 | spin_unlock_irqrestore(&tmr->lock, flags); | |
421 | return err; | |
422 | } | |
423 | ||
1da177e4 | 424 | /* return current 'real' time. use timeofday() to get better granularity. */ |
c7e0b5bf | 425 | snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr) |
1da177e4 LT |
426 | { |
427 | snd_seq_real_time_t cur_time; | |
2cdc7b63 | 428 | unsigned long flags; |
1da177e4 | 429 | |
2cdc7b63 | 430 | spin_lock_irqsave(&tmr->lock, flags); |
1da177e4 LT |
431 | cur_time = tmr->cur_time; |
432 | if (tmr->running) { | |
3915bf29 AB |
433 | struct timespec64 tm; |
434 | ||
435 | ktime_get_ts64(&tm); | |
436 | tm = timespec64_sub(tm, tmr->last_update); | |
9b50898a TI |
437 | cur_time.tv_nsec += tm.tv_nsec; |
438 | cur_time.tv_sec += tm.tv_sec; | |
1da177e4 LT |
439 | snd_seq_sanity_real_time(&cur_time); |
440 | } | |
2cdc7b63 | 441 | spin_unlock_irqrestore(&tmr->lock, flags); |
1da177e4 LT |
442 | return cur_time; |
443 | } | |
444 | ||
445 | /* TODO: use interpolation on tick queue (will only be useful for very | |
446 | high PPQ values) */ | |
c7e0b5bf | 447 | snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr) |
1da177e4 LT |
448 | { |
449 | return tmr->tick.cur_tick; | |
450 | } | |
451 | ||
452 | ||
cd6a6503 | 453 | #ifdef CONFIG_SND_PROC_FS |
1da177e4 | 454 | /* exported to seq_info.c */ |
c7e0b5bf TI |
455 | void snd_seq_info_timer_read(struct snd_info_entry *entry, |
456 | struct snd_info_buffer *buffer) | |
1da177e4 LT |
457 | { |
458 | int idx; | |
c7e0b5bf TI |
459 | struct snd_seq_queue *q; |
460 | struct snd_seq_timer *tmr; | |
461 | struct snd_timer_instance *ti; | |
1da177e4 LT |
462 | unsigned long resolution; |
463 | ||
464 | for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) { | |
465 | q = queueptr(idx); | |
466 | if (q == NULL) | |
467 | continue; | |
468 | if ((tmr = q->timer) == NULL || | |
469 | (ti = tmr->timeri) == NULL) { | |
470 | queuefree(q); | |
471 | continue; | |
472 | } | |
473 | snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name); | |
474 | resolution = snd_timer_resolution(ti) * tmr->ticks; | |
475 | snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000); | |
476 | snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base); | |
477 | queuefree(q); | |
478 | } | |
479 | } | |
cd6a6503 | 480 | #endif /* CONFIG_SND_PROC_FS */ |
04f141a8 | 481 |