xref: /linux/sound/core/seq/seq_timer.c (revision 9ce7677cfd7cd871adb457c80bea3b581b839641)
1 /*
2  *   ALSA sequencer Timer
3  *   Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
4  *                              Jaroslav Kysela <perex@suse.cz>
5  *
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 
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"
28 #include "seq_info.h"
29 
30 extern int seq_default_timer_class;
31 extern int seq_default_timer_sclass;
32 extern int seq_default_timer_card;
33 extern int seq_default_timer_device;
34 extern int seq_default_timer_subdevice;
35 extern int seq_default_timer_resolution;
36 
37 /* allowed sequencer timer frequencies, in Hz */
38 #define MIN_FREQUENCY		10
39 #define MAX_FREQUENCY		6250
40 #define DEFAULT_FREQUENCY	1000
41 
42 #define SKEW_BASE	0x10000	/* 16bit shift */
43 
44 static void snd_seq_timer_set_tick_resolution(seq_timer_tick_t *tick,
45 					      int tempo, int ppq)
46 {
47 	if (tempo < 1000000)
48 		tick->resolution = (tempo * 1000) / ppq;
49 	else {
50 		/* might overflow.. */
51 		unsigned int s;
52 		s = tempo % ppq;
53 		s = (s * 1000) / ppq;
54 		tick->resolution = (tempo / ppq) * 1000;
55 		tick->resolution += s;
56 	}
57 	if (tick->resolution <= 0)
58 		tick->resolution = 1;
59 	snd_seq_timer_update_tick(tick, 0);
60 }
61 
62 /* create new timer (constructor) */
63 seq_timer_t *snd_seq_timer_new(void)
64 {
65 	seq_timer_t *tmr;
66 
67 	tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
68 	if (tmr == NULL) {
69 		snd_printd("malloc failed for snd_seq_timer_new() \n");
70 		return NULL;
71 	}
72 	spin_lock_init(&tmr->lock);
73 
74 	/* reset setup to defaults */
75 	snd_seq_timer_defaults(tmr);
76 
77 	/* reset time */
78 	snd_seq_timer_reset(tmr);
79 
80 	return tmr;
81 }
82 
83 /* delete timer (destructor) */
84 void snd_seq_timer_delete(seq_timer_t **tmr)
85 {
86 	seq_timer_t *t = *tmr;
87 	*tmr = NULL;
88 
89 	if (t == NULL) {
90 		snd_printd("oops: snd_seq_timer_delete() called with NULL timer\n");
91 		return;
92 	}
93 	t->running = 0;
94 
95 	/* reset time */
96 	snd_seq_timer_stop(t);
97 	snd_seq_timer_reset(t);
98 
99 	kfree(t);
100 }
101 
102 void snd_seq_timer_defaults(seq_timer_t * tmr)
103 {
104 	/* setup defaults */
105 	tmr->ppq = 96;		/* 96 PPQ */
106 	tmr->tempo = 500000;	/* 120 BPM */
107 	snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
108 	tmr->running = 0;
109 
110 	tmr->type = SNDRV_SEQ_TIMER_ALSA;
111 	tmr->alsa_id.dev_class = seq_default_timer_class;
112 	tmr->alsa_id.dev_sclass = seq_default_timer_sclass;
113 	tmr->alsa_id.card = seq_default_timer_card;
114 	tmr->alsa_id.device = seq_default_timer_device;
115 	tmr->alsa_id.subdevice = seq_default_timer_subdevice;
116 	tmr->preferred_resolution = seq_default_timer_resolution;
117 
118 	tmr->skew = tmr->skew_base = SKEW_BASE;
119 }
120 
121 void snd_seq_timer_reset(seq_timer_t * tmr)
122 {
123 	unsigned long flags;
124 
125 	spin_lock_irqsave(&tmr->lock, flags);
126 
127 	/* reset time & songposition */
128 	tmr->cur_time.tv_sec = 0;
129 	tmr->cur_time.tv_nsec = 0;
130 
131 	tmr->tick.cur_tick = 0;
132 	tmr->tick.fraction = 0;
133 
134 	spin_unlock_irqrestore(&tmr->lock, flags);
135 }
136 
137 
138 /* called by timer interrupt routine. the period time since previous invocation is passed */
139 static void snd_seq_timer_interrupt(snd_timer_instance_t *timeri,
140 				    unsigned long resolution,
141 				    unsigned long ticks)
142 {
143 	unsigned long flags;
144 	queue_t *q = (queue_t *)timeri->callback_data;
145 	seq_timer_t *tmr;
146 
147 	if (q == NULL)
148 		return;
149 	tmr = q->timer;
150 	if (tmr == NULL)
151 		return;
152 	if (!tmr->running)
153 		return;
154 
155 	resolution *= ticks;
156 	if (tmr->skew != tmr->skew_base) {
157 		/* FIXME: assuming skew_base = 0x10000 */
158 		resolution = (resolution >> 16) * tmr->skew +
159 			(((resolution & 0xffff) * tmr->skew) >> 16);
160 	}
161 
162 	spin_lock_irqsave(&tmr->lock, flags);
163 
164 	/* update timer */
165 	snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
166 
167 	/* calculate current tick */
168 	snd_seq_timer_update_tick(&tmr->tick, resolution);
169 
170 	/* register actual time of this timer update */
171 	do_gettimeofday(&tmr->last_update);
172 
173 	spin_unlock_irqrestore(&tmr->lock, flags);
174 
175 	/* check queues and dispatch events */
176 	snd_seq_check_queue(q, 1, 0);
177 }
178 
179 /* set current tempo */
180 int snd_seq_timer_set_tempo(seq_timer_t * tmr, int tempo)
181 {
182 	unsigned long flags;
183 
184 	snd_assert(tmr, return -EINVAL);
185 	if (tempo <= 0)
186 		return -EINVAL;
187 	spin_lock_irqsave(&tmr->lock, flags);
188 	if ((unsigned int)tempo != tmr->tempo) {
189 		tmr->tempo = tempo;
190 		snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
191 	}
192 	spin_unlock_irqrestore(&tmr->lock, flags);
193 	return 0;
194 }
195 
196 /* set current ppq */
197 int snd_seq_timer_set_ppq(seq_timer_t * tmr, int ppq)
198 {
199 	unsigned long flags;
200 
201 	snd_assert(tmr, return -EINVAL);
202 	if (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 		snd_printd("seq: cannot change ppq of a running timer\n");
210 		return -EBUSY;
211 	}
212 
213 	tmr->ppq = ppq;
214 	snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
215 	spin_unlock_irqrestore(&tmr->lock, flags);
216 	return 0;
217 }
218 
219 /* set current tick position */
220 int snd_seq_timer_set_position_tick(seq_timer_t *tmr, snd_seq_tick_time_t position)
221 {
222 	unsigned long flags;
223 
224 	snd_assert(tmr, return -EINVAL);
225 
226 	spin_lock_irqsave(&tmr->lock, flags);
227 	tmr->tick.cur_tick = position;
228 	tmr->tick.fraction = 0;
229 	spin_unlock_irqrestore(&tmr->lock, flags);
230 	return 0;
231 }
232 
233 /* set current real-time position */
234 int snd_seq_timer_set_position_time(seq_timer_t *tmr, snd_seq_real_time_t position)
235 {
236 	unsigned long flags;
237 
238 	snd_assert(tmr, return -EINVAL);
239 
240 	snd_seq_sanity_real_time(&position);
241 	spin_lock_irqsave(&tmr->lock, flags);
242 	tmr->cur_time = position;
243 	spin_unlock_irqrestore(&tmr->lock, flags);
244 	return 0;
245 }
246 
247 /* set timer skew */
248 int snd_seq_timer_set_skew(seq_timer_t *tmr, unsigned int skew, unsigned int base)
249 {
250 	unsigned long flags;
251 
252 	snd_assert(tmr, return -EINVAL);
253 
254 	/* FIXME */
255 	if (base != SKEW_BASE) {
256 		snd_printd("invalid skew base 0x%x\n", base);
257 		return -EINVAL;
258 	}
259 	spin_lock_irqsave(&tmr->lock, flags);
260 	tmr->skew = skew;
261 	spin_unlock_irqrestore(&tmr->lock, flags);
262 	return 0;
263 }
264 
265 int snd_seq_timer_open(queue_t *q)
266 {
267 	snd_timer_instance_t *t;
268 	seq_timer_t *tmr;
269 	char str[32];
270 	int err;
271 
272 	tmr = q->timer;
273 	snd_assert(tmr != NULL, return -EINVAL);
274 	if (tmr->timeri)
275 		return -EBUSY;
276 	sprintf(str, "sequencer queue %i", q->queue);
277 	if (tmr->type != SNDRV_SEQ_TIMER_ALSA)	/* standard ALSA timer */
278 		return -EINVAL;
279 	if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
280 		tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
281 	err = snd_timer_open(&t, str, &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 			snd_timer_id_t tid;
286 			memset(&tid, 0, sizeof(tid));
287 			tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
288 			tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
289 			tid.card = -1;
290 			tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
291 			err = snd_timer_open(&t, str, &tid, q->queue);
292 		}
293 		if (err < 0) {
294 			snd_printk(KERN_ERR "seq fatal error: cannot create timer (%i)\n", err);
295 			return err;
296 		}
297 	}
298 	t->callback = snd_seq_timer_interrupt;
299 	t->callback_data = q;
300 	t->flags |= SNDRV_TIMER_IFLG_AUTO;
301 	tmr->timeri = t;
302 	return 0;
303 }
304 
305 int snd_seq_timer_close(queue_t *q)
306 {
307 	seq_timer_t *tmr;
308 
309 	tmr = q->timer;
310 	snd_assert(tmr != NULL, return -EINVAL);
311 	if (tmr->timeri) {
312 		snd_timer_stop(tmr->timeri);
313 		snd_timer_close(tmr->timeri);
314 		tmr->timeri = NULL;
315 	}
316 	return 0;
317 }
318 
319 int snd_seq_timer_stop(seq_timer_t * tmr)
320 {
321 	if (! tmr->timeri)
322 		return -EINVAL;
323 	if (!tmr->running)
324 		return 0;
325 	tmr->running = 0;
326 	snd_timer_pause(tmr->timeri);
327 	return 0;
328 }
329 
330 static int initialize_timer(seq_timer_t *tmr)
331 {
332 	snd_timer_t *t;
333 	unsigned long freq;
334 
335 	t = tmr->timeri->timer;
336 	snd_assert(t, return -EINVAL);
337 
338 	freq = tmr->preferred_resolution;
339 	if (!freq)
340 		freq = DEFAULT_FREQUENCY;
341 	else if (freq < MIN_FREQUENCY)
342 		freq = MIN_FREQUENCY;
343 	else if (freq > MAX_FREQUENCY)
344 		freq = MAX_FREQUENCY;
345 
346 	tmr->ticks = 1;
347 	if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
348 		unsigned long r = t->hw.resolution;
349 		if (! r && t->hw.c_resolution)
350 			r = t->hw.c_resolution(t);
351 		if (r) {
352 			tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
353 			if (! tmr->ticks)
354 				tmr->ticks = 1;
355 		}
356 	}
357 	tmr->initialized = 1;
358 	return 0;
359 }
360 
361 int snd_seq_timer_start(seq_timer_t * tmr)
362 {
363 	if (! tmr->timeri)
364 		return -EINVAL;
365 	if (tmr->running)
366 		snd_seq_timer_stop(tmr);
367 	snd_seq_timer_reset(tmr);
368 	if (initialize_timer(tmr) < 0)
369 		return -EINVAL;
370 	snd_timer_start(tmr->timeri, tmr->ticks);
371 	tmr->running = 1;
372 	do_gettimeofday(&tmr->last_update);
373 	return 0;
374 }
375 
376 int snd_seq_timer_continue(seq_timer_t * tmr)
377 {
378 	if (! tmr->timeri)
379 		return -EINVAL;
380 	if (tmr->running)
381 		return -EBUSY;
382 	if (! tmr->initialized) {
383 		snd_seq_timer_reset(tmr);
384 		if (initialize_timer(tmr) < 0)
385 			return -EINVAL;
386 	}
387 	snd_timer_start(tmr->timeri, tmr->ticks);
388 	tmr->running = 1;
389 	do_gettimeofday(&tmr->last_update);
390 	return 0;
391 }
392 
393 /* return current 'real' time. use timeofday() to get better granularity. */
394 snd_seq_real_time_t snd_seq_timer_get_cur_time(seq_timer_t *tmr)
395 {
396 	snd_seq_real_time_t cur_time;
397 
398 	cur_time = tmr->cur_time;
399 	if (tmr->running) {
400 		struct timeval tm;
401 		int usec;
402 		do_gettimeofday(&tm);
403 		usec = (int)(tm.tv_usec - tmr->last_update.tv_usec);
404 		if (usec < 0) {
405 			cur_time.tv_nsec += (1000000 + usec) * 1000;
406 			cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec - 1;
407 		} else {
408 			cur_time.tv_nsec += usec * 1000;
409 			cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec;
410 		}
411 		snd_seq_sanity_real_time(&cur_time);
412 	}
413 
414 	return cur_time;
415 }
416 
417 /* TODO: use interpolation on tick queue (will only be useful for very
418  high PPQ values) */
419 snd_seq_tick_time_t snd_seq_timer_get_cur_tick(seq_timer_t *tmr)
420 {
421 	return tmr->tick.cur_tick;
422 }
423 
424 
425 /* exported to seq_info.c */
426 void snd_seq_info_timer_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
427 {
428 	int idx;
429 	queue_t *q;
430 	seq_timer_t *tmr;
431 	snd_timer_instance_t *ti;
432 	unsigned long resolution;
433 
434 	for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
435 		q = queueptr(idx);
436 		if (q == NULL)
437 			continue;
438 		if ((tmr = q->timer) == NULL ||
439 		    (ti = tmr->timeri) == NULL) {
440 			queuefree(q);
441 			continue;
442 		}
443 		snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
444 		resolution = snd_timer_resolution(ti) * tmr->ticks;
445 		snd_iprintf(buffer, "  Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
446 		snd_iprintf(buffer, "  Skew : %u / %u\n", tmr->skew, tmr->skew_base);
447 		queuefree(q);
448  	}
449 }
450