xref: /openbmc/linux/sound/core/seq/seq_timer.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
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 #define SKEW_BASE	0x10000	/* 16bit shift */
38 
39 void snd_seq_timer_set_tick_resolution(seq_timer_tick_t *tick, int tempo, int ppq, int nticks)
40 {
41 	if (tempo < 1000000)
42 		tick->resolution = (tempo * 1000) / ppq;
43 	else {
44 		/* might overflow.. */
45 		unsigned int s;
46 		s = tempo % ppq;
47 		s = (s * 1000) / ppq;
48 		tick->resolution = (tempo / ppq) * 1000;
49 		tick->resolution += s;
50 	}
51 	if (tick->resolution <= 0)
52 		tick->resolution = 1;
53 	tick->resolution *= nticks;
54 	snd_seq_timer_update_tick(tick, 0);
55 }
56 
57 /* create new timer (constructor) */
58 seq_timer_t *snd_seq_timer_new(void)
59 {
60 	seq_timer_t *tmr;
61 
62 	tmr = kcalloc(1, sizeof(*tmr), GFP_KERNEL);
63 	if (tmr == NULL) {
64 		snd_printd("malloc failed for snd_seq_timer_new() \n");
65 		return NULL;
66 	}
67 	spin_lock_init(&tmr->lock);
68 
69 	/* reset setup to defaults */
70 	snd_seq_timer_defaults(tmr);
71 
72 	/* reset time */
73 	snd_seq_timer_reset(tmr);
74 
75 	return tmr;
76 }
77 
78 /* delete timer (destructor) */
79 void snd_seq_timer_delete(seq_timer_t **tmr)
80 {
81 	seq_timer_t *t = *tmr;
82 	*tmr = NULL;
83 
84 	if (t == NULL) {
85 		snd_printd("oops: snd_seq_timer_delete() called with NULL timer\n");
86 		return;
87 	}
88 	t->running = 0;
89 
90 	/* reset time */
91 	snd_seq_timer_stop(t);
92 	snd_seq_timer_reset(t);
93 
94 	kfree(t);
95 }
96 
97 void snd_seq_timer_defaults(seq_timer_t * tmr)
98 {
99 	/* setup defaults */
100 	tmr->ppq = 96;		/* 96 PPQ */
101 	tmr->tempo = 500000;	/* 120 BPM */
102 	snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq, 1);
103 	tmr->running = 0;
104 
105 	tmr->type = SNDRV_SEQ_TIMER_ALSA;
106 	tmr->alsa_id.dev_class = seq_default_timer_class;
107 	tmr->alsa_id.dev_sclass = seq_default_timer_sclass;
108 	tmr->alsa_id.card = seq_default_timer_card;
109 	tmr->alsa_id.device = seq_default_timer_device;
110 	tmr->alsa_id.subdevice = seq_default_timer_subdevice;
111 	tmr->preferred_resolution = seq_default_timer_resolution;
112 
113 	tmr->skew = tmr->skew_base = SKEW_BASE;
114 }
115 
116 void snd_seq_timer_reset(seq_timer_t * tmr)
117 {
118 	unsigned long flags;
119 
120 	spin_lock_irqsave(&tmr->lock, flags);
121 
122 	/* reset time & songposition */
123 	tmr->cur_time.tv_sec = 0;
124 	tmr->cur_time.tv_nsec = 0;
125 
126 	tmr->tick.cur_tick = 0;
127 	tmr->tick.fraction = 0;
128 
129 	spin_unlock_irqrestore(&tmr->lock, flags);
130 }
131 
132 
133 /* called by timer interrupt routine. the period time since previous invocation is passed */
134 static void snd_seq_timer_interrupt(snd_timer_instance_t *timeri,
135 				    unsigned long resolution,
136 				    unsigned long ticks)
137 {
138 	unsigned long flags;
139 	queue_t *q = (queue_t *)timeri->callback_data;
140 	seq_timer_t *tmr;
141 
142 	if (q == NULL)
143 		return;
144 	tmr = q->timer;
145 	if (tmr == NULL)
146 		return;
147 	if (!tmr->running)
148 		return;
149 
150 	resolution *= ticks;
151 	if (tmr->skew != tmr->skew_base) {
152 		/* FIXME: assuming skew_base = 0x10000 */
153 		resolution = (resolution >> 16) * tmr->skew +
154 			(((resolution & 0xffff) * tmr->skew) >> 16);
155 	}
156 
157 	spin_lock_irqsave(&tmr->lock, flags);
158 
159 	/* update timer */
160 	snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
161 
162 	/* calculate current tick */
163 	snd_seq_timer_update_tick(&tmr->tick, resolution);
164 
165 	/* register actual time of this timer update */
166 	do_gettimeofday(&tmr->last_update);
167 
168 	spin_unlock_irqrestore(&tmr->lock, flags);
169 
170 	/* check queues and dispatch events */
171 	snd_seq_check_queue(q, 1, 0);
172 }
173 
174 /* set current tempo */
175 int snd_seq_timer_set_tempo(seq_timer_t * tmr, int tempo)
176 {
177 	unsigned long flags;
178 
179 	snd_assert(tmr, return -EINVAL);
180 	if (tempo <= 0)
181 		return -EINVAL;
182 	spin_lock_irqsave(&tmr->lock, flags);
183 	if ((unsigned int)tempo != tmr->tempo) {
184 		tmr->tempo = tempo;
185 		snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq, 1);
186 	}
187 	spin_unlock_irqrestore(&tmr->lock, flags);
188 	return 0;
189 }
190 
191 /* set current ppq */
192 int snd_seq_timer_set_ppq(seq_timer_t * tmr, int ppq)
193 {
194 	unsigned long flags;
195 
196 	snd_assert(tmr, return -EINVAL);
197 	if (ppq <= 0)
198 		return -EINVAL;
199 	spin_lock_irqsave(&tmr->lock, flags);
200 	if (tmr->running && (ppq != tmr->ppq)) {
201 		/* refuse to change ppq on running timers */
202 		/* because it will upset the song position (ticks) */
203 		spin_unlock_irqrestore(&tmr->lock, flags);
204 		snd_printd("seq: cannot change ppq of a running timer\n");
205 		return -EBUSY;
206 	}
207 
208 	tmr->ppq = ppq;
209 	snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq, 1);
210 	spin_unlock_irqrestore(&tmr->lock, flags);
211 	return 0;
212 }
213 
214 /* set current tick position */
215 int snd_seq_timer_set_position_tick(seq_timer_t *tmr, snd_seq_tick_time_t position)
216 {
217 	unsigned long flags;
218 
219 	snd_assert(tmr, return -EINVAL);
220 
221 	spin_lock_irqsave(&tmr->lock, flags);
222 	tmr->tick.cur_tick = position;
223 	tmr->tick.fraction = 0;
224 	spin_unlock_irqrestore(&tmr->lock, flags);
225 	return 0;
226 }
227 
228 /* set current real-time position */
229 int snd_seq_timer_set_position_time(seq_timer_t *tmr, snd_seq_real_time_t position)
230 {
231 	unsigned long flags;
232 
233 	snd_assert(tmr, return -EINVAL);
234 
235 	snd_seq_sanity_real_time(&position);
236 	spin_lock_irqsave(&tmr->lock, flags);
237 	tmr->cur_time = position;
238 	spin_unlock_irqrestore(&tmr->lock, flags);
239 	return 0;
240 }
241 
242 /* set timer skew */
243 int snd_seq_timer_set_skew(seq_timer_t *tmr, unsigned int skew, unsigned int base)
244 {
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 
260 int snd_seq_timer_open(queue_t *q)
261 {
262 	snd_timer_instance_t *t;
263 	seq_timer_t *tmr;
264 	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) {
280 			snd_timer_id_t tid;
281 			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 
300 int snd_seq_timer_close(queue_t *q)
301 {
302 	seq_timer_t *tmr;
303 
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 
314 int snd_seq_timer_stop(seq_timer_t * tmr)
315 {
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 
325 static int initialize_timer(seq_timer_t *tmr)
326 {
327 	snd_timer_t *t;
328 	t = tmr->timeri->timer;
329 	snd_assert(t, return -EINVAL);
330 
331 	tmr->ticks = 1;
332 	if (tmr->preferred_resolution &&
333 	    ! (t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
334 		unsigned long r = t->hw.resolution;
335 		if (! r && t->hw.c_resolution)
336 			r = t->hw.c_resolution(t);
337 		if (r) {
338 			tmr->ticks = (unsigned int)(1000000000uL / (r * tmr->preferred_resolution));
339 			if (! tmr->ticks)
340 				tmr->ticks = 1;
341 		}
342 	}
343 	tmr->initialized = 1;
344 	return 0;
345 }
346 
347 int snd_seq_timer_start(seq_timer_t * tmr)
348 {
349 	if (! tmr->timeri)
350 		return -EINVAL;
351 	if (tmr->running)
352 		snd_seq_timer_stop(tmr);
353 	snd_seq_timer_reset(tmr);
354 	if (initialize_timer(tmr) < 0)
355 		return -EINVAL;
356 	snd_timer_start(tmr->timeri, tmr->ticks);
357 	tmr->running = 1;
358 	do_gettimeofday(&tmr->last_update);
359 	return 0;
360 }
361 
362 int snd_seq_timer_continue(seq_timer_t * tmr)
363 {
364 	if (! tmr->timeri)
365 		return -EINVAL;
366 	if (tmr->running)
367 		return -EBUSY;
368 	if (! tmr->initialized) {
369 		snd_seq_timer_reset(tmr);
370 		if (initialize_timer(tmr) < 0)
371 			return -EINVAL;
372 	}
373 	snd_timer_start(tmr->timeri, tmr->ticks);
374 	tmr->running = 1;
375 	do_gettimeofday(&tmr->last_update);
376 	return 0;
377 }
378 
379 /* return current 'real' time. use timeofday() to get better granularity. */
380 snd_seq_real_time_t snd_seq_timer_get_cur_time(seq_timer_t *tmr)
381 {
382 	snd_seq_real_time_t cur_time;
383 
384 	cur_time = tmr->cur_time;
385 	if (tmr->running) {
386 		struct timeval tm;
387 		int usec;
388 		do_gettimeofday(&tm);
389 		usec = (int)(tm.tv_usec - tmr->last_update.tv_usec);
390 		if (usec < 0) {
391 			cur_time.tv_nsec += (1000000 + usec) * 1000;
392 			cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec - 1;
393 		} else {
394 			cur_time.tv_nsec += usec * 1000;
395 			cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec;
396 		}
397 		snd_seq_sanity_real_time(&cur_time);
398 	}
399 
400 	return cur_time;
401 }
402 
403 /* TODO: use interpolation on tick queue (will only be useful for very
404  high PPQ values) */
405 snd_seq_tick_time_t snd_seq_timer_get_cur_tick(seq_timer_t *tmr)
406 {
407 	return tmr->tick.cur_tick;
408 }
409 
410 
411 /* exported to seq_info.c */
412 void snd_seq_info_timer_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
413 {
414 	int idx;
415 	queue_t *q;
416 	seq_timer_t *tmr;
417 	snd_timer_instance_t *ti;
418 	unsigned long resolution;
419 
420 	for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
421 		q = queueptr(idx);
422 		if (q == NULL)
423 			continue;
424 		if ((tmr = q->timer) == NULL ||
425 		    (ti = tmr->timeri) == NULL) {
426 			queuefree(q);
427 			continue;
428 		}
429 		snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
430 		resolution = snd_timer_resolution(ti) * tmr->ticks;
431 		snd_iprintf(buffer, "  Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
432 		snd_iprintf(buffer, "  Skew : %u / %u\n", tmr->skew, tmr->skew_base);
433 		queuefree(q);
434  	}
435 }
436