x86: add pte_pgprot to 32-bit
[linux-2.6/linux-2.6-openrd.git] / sound / core / seq / seq_timer.c
blob8716352afc812a8eb9dee2469d2e6c3fd1f30433
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>
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.
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.
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
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"
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;
37 /* allowed sequencer timer frequencies, in Hz */
38 #define MIN_FREQUENCY 10
39 #define MAX_FREQUENCY 6250
40 #define DEFAULT_FREQUENCY 1000
42 #define SKEW_BASE 0x10000 /* 16bit shift */
44 static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer_tick *tick,
45 int tempo, int ppq)
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;
57 if (tick->resolution <= 0)
58 tick->resolution = 1;
59 snd_seq_timer_update_tick(tick, 0);
62 /* create new timer (constructor) */
63 struct snd_seq_timer *snd_seq_timer_new(void)
65 struct snd_seq_timer *tmr;
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;
72 spin_lock_init(&tmr->lock);
74 /* reset setup to defaults */
75 snd_seq_timer_defaults(tmr);
77 /* reset time */
78 snd_seq_timer_reset(tmr);
80 return tmr;
83 /* delete timer (destructor) */
84 void snd_seq_timer_delete(struct snd_seq_timer **tmr)
86 struct snd_seq_timer *t = *tmr;
87 *tmr = NULL;
89 if (t == NULL) {
90 snd_printd("oops: snd_seq_timer_delete() called with NULL timer\n");
91 return;
93 t->running = 0;
95 /* reset time */
96 snd_seq_timer_stop(t);
97 snd_seq_timer_reset(t);
99 kfree(t);
102 void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
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;
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;
118 tmr->skew = tmr->skew_base = SKEW_BASE;
121 void snd_seq_timer_reset(struct snd_seq_timer * tmr)
123 unsigned long flags;
125 spin_lock_irqsave(&tmr->lock, flags);
127 /* reset time & songposition */
128 tmr->cur_time.tv_sec = 0;
129 tmr->cur_time.tv_nsec = 0;
131 tmr->tick.cur_tick = 0;
132 tmr->tick.fraction = 0;
134 spin_unlock_irqrestore(&tmr->lock, flags);
138 /* called by timer interrupt routine. the period time since previous invocation is passed */
139 static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
140 unsigned long resolution,
141 unsigned long ticks)
143 unsigned long flags;
144 struct snd_seq_queue *q = timeri->callback_data;
145 struct snd_seq_timer *tmr;
147 if (q == NULL)
148 return;
149 tmr = q->timer;
150 if (tmr == NULL)
151 return;
152 if (!tmr->running)
153 return;
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);
162 spin_lock_irqsave(&tmr->lock, flags);
164 /* update timer */
165 snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
167 /* calculate current tick */
168 snd_seq_timer_update_tick(&tmr->tick, resolution);
170 /* register actual time of this timer update */
171 do_gettimeofday(&tmr->last_update);
173 spin_unlock_irqrestore(&tmr->lock, flags);
175 /* check queues and dispatch events */
176 snd_seq_check_queue(q, 1, 0);
179 /* set current tempo */
180 int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
182 unsigned long flags;
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);
192 spin_unlock_irqrestore(&tmr->lock, flags);
193 return 0;
196 /* set current ppq */
197 int snd_seq_timer_set_ppq(struct snd_seq_timer * tmr, int ppq)
199 unsigned long flags;
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;
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;
219 /* set current tick position */
220 int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
221 snd_seq_tick_time_t position)
223 unsigned long flags;
225 snd_assert(tmr, return -EINVAL);
227 spin_lock_irqsave(&tmr->lock, flags);
228 tmr->tick.cur_tick = position;
229 tmr->tick.fraction = 0;
230 spin_unlock_irqrestore(&tmr->lock, flags);
231 return 0;
234 /* set current real-time position */
235 int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
236 snd_seq_real_time_t position)
238 unsigned long flags;
240 snd_assert(tmr, return -EINVAL);
242 snd_seq_sanity_real_time(&position);
243 spin_lock_irqsave(&tmr->lock, flags);
244 tmr->cur_time = position;
245 spin_unlock_irqrestore(&tmr->lock, flags);
246 return 0;
249 /* set timer skew */
250 int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
251 unsigned int base)
253 unsigned long flags;
255 snd_assert(tmr, return -EINVAL);
257 /* FIXME */
258 if (base != SKEW_BASE) {
259 snd_printd("invalid skew base 0x%x\n", base);
260 return -EINVAL;
262 spin_lock_irqsave(&tmr->lock, flags);
263 tmr->skew = skew;
264 spin_unlock_irqrestore(&tmr->lock, flags);
265 return 0;
268 int snd_seq_timer_open(struct snd_seq_queue *q)
270 struct snd_timer_instance *t;
271 struct snd_seq_timer *tmr;
272 char str[32];
273 int err;
275 tmr = q->timer;
276 snd_assert(tmr != NULL, return -EINVAL);
277 if (tmr->timeri)
278 return -EBUSY;
279 sprintf(str, "sequencer queue %i", q->queue);
280 if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
281 return -EINVAL;
282 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
283 tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
284 err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue);
285 if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
286 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
287 tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
288 struct snd_timer_id tid;
289 memset(&tid, 0, sizeof(tid));
290 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
291 tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
292 tid.card = -1;
293 tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
294 err = snd_timer_open(&t, str, &tid, q->queue);
296 if (err < 0) {
297 snd_printk(KERN_ERR "seq fatal error: cannot create timer (%i)\n", err);
298 return err;
301 t->callback = snd_seq_timer_interrupt;
302 t->callback_data = q;
303 t->flags |= SNDRV_TIMER_IFLG_AUTO;
304 tmr->timeri = t;
305 return 0;
308 int snd_seq_timer_close(struct snd_seq_queue *q)
310 struct snd_seq_timer *tmr;
312 tmr = q->timer;
313 snd_assert(tmr != NULL, return -EINVAL);
314 if (tmr->timeri) {
315 snd_timer_stop(tmr->timeri);
316 snd_timer_close(tmr->timeri);
317 tmr->timeri = NULL;
319 return 0;
322 int snd_seq_timer_stop(struct snd_seq_timer * tmr)
324 if (! tmr->timeri)
325 return -EINVAL;
326 if (!tmr->running)
327 return 0;
328 tmr->running = 0;
329 snd_timer_pause(tmr->timeri);
330 return 0;
333 static int initialize_timer(struct snd_seq_timer *tmr)
335 struct snd_timer *t;
336 unsigned long freq;
338 t = tmr->timeri->timer;
339 snd_assert(t, return -EINVAL);
341 freq = tmr->preferred_resolution;
342 if (!freq)
343 freq = DEFAULT_FREQUENCY;
344 else if (freq < MIN_FREQUENCY)
345 freq = MIN_FREQUENCY;
346 else if (freq > MAX_FREQUENCY)
347 freq = MAX_FREQUENCY;
349 tmr->ticks = 1;
350 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
351 unsigned long r = t->hw.resolution;
352 if (! r && t->hw.c_resolution)
353 r = t->hw.c_resolution(t);
354 if (r) {
355 tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
356 if (! tmr->ticks)
357 tmr->ticks = 1;
360 tmr->initialized = 1;
361 return 0;
364 int snd_seq_timer_start(struct snd_seq_timer * tmr)
366 if (! tmr->timeri)
367 return -EINVAL;
368 if (tmr->running)
369 snd_seq_timer_stop(tmr);
370 snd_seq_timer_reset(tmr);
371 if (initialize_timer(tmr) < 0)
372 return -EINVAL;
373 snd_timer_start(tmr->timeri, tmr->ticks);
374 tmr->running = 1;
375 do_gettimeofday(&tmr->last_update);
376 return 0;
379 int snd_seq_timer_continue(struct snd_seq_timer * tmr)
381 if (! tmr->timeri)
382 return -EINVAL;
383 if (tmr->running)
384 return -EBUSY;
385 if (! tmr->initialized) {
386 snd_seq_timer_reset(tmr);
387 if (initialize_timer(tmr) < 0)
388 return -EINVAL;
390 snd_timer_start(tmr->timeri, tmr->ticks);
391 tmr->running = 1;
392 do_gettimeofday(&tmr->last_update);
393 return 0;
396 /* return current 'real' time. use timeofday() to get better granularity. */
397 snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr)
399 snd_seq_real_time_t cur_time;
401 cur_time = tmr->cur_time;
402 if (tmr->running) {
403 struct timeval tm;
404 int usec;
405 do_gettimeofday(&tm);
406 usec = (int)(tm.tv_usec - tmr->last_update.tv_usec);
407 if (usec < 0) {
408 cur_time.tv_nsec += (1000000 + usec) * 1000;
409 cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec - 1;
410 } else {
411 cur_time.tv_nsec += usec * 1000;
412 cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec;
414 snd_seq_sanity_real_time(&cur_time);
417 return cur_time;
420 /* TODO: use interpolation on tick queue (will only be useful for very
421 high PPQ values) */
422 snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
424 return tmr->tick.cur_tick;
428 #ifdef CONFIG_PROC_FS
429 /* exported to seq_info.c */
430 void snd_seq_info_timer_read(struct snd_info_entry *entry,
431 struct snd_info_buffer *buffer)
433 int idx;
434 struct snd_seq_queue *q;
435 struct snd_seq_timer *tmr;
436 struct snd_timer_instance *ti;
437 unsigned long resolution;
439 for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
440 q = queueptr(idx);
441 if (q == NULL)
442 continue;
443 if ((tmr = q->timer) == NULL ||
444 (ti = tmr->timeri) == NULL) {
445 queuefree(q);
446 continue;
448 snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
449 resolution = snd_timer_resolution(ti) * tmr->ticks;
450 snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
451 snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
452 queuefree(q);
455 #endif /* CONFIG_PROC_FS */