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