ALSA: replace CONFIG_PROC_FS with CONFIG_SND_PROC_FS
[deliverable/linux.git] / sound / core / seq / seq_timer.c
CommitLineData
1da177e4
LT
1/*
2 * ALSA sequencer Timer
3 * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
c1017a4c 4 * Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
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
1da177e4
LT
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
87ef7779
CL
29/* allowed sequencer timer frequencies, in Hz */
30#define MIN_FREQUENCY 10
31#define MAX_FREQUENCY 6250
32#define DEFAULT_FREQUENCY 1000
33
1da177e4
LT
34#define SKEW_BASE 0x10000 /* 16bit shift */
35
a32f6674 36static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer *tmr)
1da177e4 37{
a32f6674
CL
38 if (tmr->tempo < 1000000)
39 tmr->tick.resolution = (tmr->tempo * 1000) / tmr->ppq;
1da177e4
LT
40 else {
41 /* might overflow.. */
42 unsigned int s;
a32f6674
CL
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;
1da177e4 47 }
a32f6674
CL
48 if (tmr->tick.resolution <= 0)
49 tmr->tick.resolution = 1;
50 snd_seq_timer_update_tick(&tmr->tick, 0);
1da177e4
LT
51}
52
53/* create new timer (constructor) */
c7e0b5bf 54struct snd_seq_timer *snd_seq_timer_new(void)
1da177e4 55{
c7e0b5bf 56 struct snd_seq_timer *tmr;
1da177e4 57
ecca82b4 58 tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
24db8bba 59 if (!tmr)
1da177e4 60 return NULL;
1da177e4
LT
61 spin_lock_init(&tmr->lock);
62
63 /* reset setup to defaults */
64 snd_seq_timer_defaults(tmr);
65
66 /* reset time */
67 snd_seq_timer_reset(tmr);
68
69 return tmr;
70}
71
72/* delete timer (destructor) */
c7e0b5bf 73void snd_seq_timer_delete(struct snd_seq_timer **tmr)
1da177e4 74{
c7e0b5bf 75 struct snd_seq_timer *t = *tmr;
1da177e4
LT
76 *tmr = NULL;
77
78 if (t == NULL) {
04cc79a0 79 pr_debug("ALSA: seq: snd_seq_timer_delete() called with NULL timer\n");
1da177e4
LT
80 return;
81 }
82 t->running = 0;
83
84 /* reset time */
85 snd_seq_timer_stop(t);
86 snd_seq_timer_reset(t);
87
88 kfree(t);
89}
90
c7e0b5bf 91void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
1da177e4
LT
92{
93 /* setup defaults */
94 tmr->ppq = 96; /* 96 PPQ */
95 tmr->tempo = 500000; /* 120 BPM */
a32f6674 96 snd_seq_timer_set_tick_resolution(tmr);
1da177e4
LT
97 tmr->running = 0;
98
99 tmr->type = SNDRV_SEQ_TIMER_ALSA;
100 tmr->alsa_id.dev_class = seq_default_timer_class;
101 tmr->alsa_id.dev_sclass = seq_default_timer_sclass;
102 tmr->alsa_id.card = seq_default_timer_card;
103 tmr->alsa_id.device = seq_default_timer_device;
104 tmr->alsa_id.subdevice = seq_default_timer_subdevice;
105 tmr->preferred_resolution = seq_default_timer_resolution;
106
107 tmr->skew = tmr->skew_base = SKEW_BASE;
108}
109
c7e0b5bf 110void snd_seq_timer_reset(struct snd_seq_timer * tmr)
1da177e4
LT
111{
112 unsigned long flags;
113
114 spin_lock_irqsave(&tmr->lock, flags);
115
116 /* reset time & songposition */
117 tmr->cur_time.tv_sec = 0;
118 tmr->cur_time.tv_nsec = 0;
119
120 tmr->tick.cur_tick = 0;
121 tmr->tick.fraction = 0;
122
123 spin_unlock_irqrestore(&tmr->lock, flags);
124}
125
126
127/* called by timer interrupt routine. the period time since previous invocation is passed */
c7e0b5bf 128static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
1da177e4
LT
129 unsigned long resolution,
130 unsigned long ticks)
131{
132 unsigned long flags;
c7e0b5bf
TI
133 struct snd_seq_queue *q = timeri->callback_data;
134 struct snd_seq_timer *tmr;
1da177e4
LT
135
136 if (q == NULL)
137 return;
138 tmr = q->timer;
139 if (tmr == NULL)
140 return;
141 if (!tmr->running)
142 return;
143
144 resolution *= ticks;
145 if (tmr->skew != tmr->skew_base) {
146 /* FIXME: assuming skew_base = 0x10000 */
147 resolution = (resolution >> 16) * tmr->skew +
148 (((resolution & 0xffff) * tmr->skew) >> 16);
149 }
150
151 spin_lock_irqsave(&tmr->lock, flags);
152
153 /* update timer */
154 snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
155
156 /* calculate current tick */
157 snd_seq_timer_update_tick(&tmr->tick, resolution);
158
159 /* register actual time of this timer update */
160 do_gettimeofday(&tmr->last_update);
161
162 spin_unlock_irqrestore(&tmr->lock, flags);
163
164 /* check queues and dispatch events */
165 snd_seq_check_queue(q, 1, 0);
166}
167
168/* set current tempo */
c7e0b5bf 169int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
1da177e4
LT
170{
171 unsigned long flags;
172
7eaa943c
TI
173 if (snd_BUG_ON(!tmr))
174 return -EINVAL;
1da177e4
LT
175 if (tempo <= 0)
176 return -EINVAL;
177 spin_lock_irqsave(&tmr->lock, flags);
178 if ((unsigned int)tempo != tmr->tempo) {
179 tmr->tempo = tempo;
a32f6674 180 snd_seq_timer_set_tick_resolution(tmr);
1da177e4
LT
181 }
182 spin_unlock_irqrestore(&tmr->lock, flags);
183 return 0;
184}
185
186/* set current ppq */
c7e0b5bf 187int snd_seq_timer_set_ppq(struct snd_seq_timer * tmr, int ppq)
1da177e4
LT
188{
189 unsigned long flags;
190
7eaa943c
TI
191 if (snd_BUG_ON(!tmr))
192 return -EINVAL;
1da177e4
LT
193 if (ppq <= 0)
194 return -EINVAL;
195 spin_lock_irqsave(&tmr->lock, flags);
196 if (tmr->running && (ppq != tmr->ppq)) {
197 /* refuse to change ppq on running timers */
198 /* because it will upset the song position (ticks) */
199 spin_unlock_irqrestore(&tmr->lock, flags);
04cc79a0 200 pr_debug("ALSA: seq: cannot change ppq of a running timer\n");
1da177e4
LT
201 return -EBUSY;
202 }
203
204 tmr->ppq = ppq;
a32f6674 205 snd_seq_timer_set_tick_resolution(tmr);
1da177e4
LT
206 spin_unlock_irqrestore(&tmr->lock, flags);
207 return 0;
208}
209
210/* set current tick position */
c7e0b5bf
TI
211int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
212 snd_seq_tick_time_t position)
1da177e4
LT
213{
214 unsigned long flags;
215
7eaa943c
TI
216 if (snd_BUG_ON(!tmr))
217 return -EINVAL;
1da177e4
LT
218
219 spin_lock_irqsave(&tmr->lock, flags);
220 tmr->tick.cur_tick = position;
221 tmr->tick.fraction = 0;
222 spin_unlock_irqrestore(&tmr->lock, flags);
223 return 0;
224}
225
226/* set current real-time position */
c7e0b5bf
TI
227int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
228 snd_seq_real_time_t position)
1da177e4
LT
229{
230 unsigned long flags;
231
7eaa943c
TI
232 if (snd_BUG_ON(!tmr))
233 return -EINVAL;
1da177e4
LT
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 */
c7e0b5bf
TI
243int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
244 unsigned int base)
1da177e4
LT
245{
246 unsigned long flags;
247
7eaa943c
TI
248 if (snd_BUG_ON(!tmr))
249 return -EINVAL;
1da177e4
LT
250
251 /* FIXME */
252 if (base != SKEW_BASE) {
04cc79a0 253 pr_debug("ALSA: seq: invalid skew base 0x%x\n", base);
1da177e4
LT
254 return -EINVAL;
255 }
256 spin_lock_irqsave(&tmr->lock, flags);
257 tmr->skew = skew;
258 spin_unlock_irqrestore(&tmr->lock, flags);
259 return 0;
260}
261
c7e0b5bf 262int snd_seq_timer_open(struct snd_seq_queue *q)
1da177e4 263{
c7e0b5bf
TI
264 struct snd_timer_instance *t;
265 struct snd_seq_timer *tmr;
1da177e4
LT
266 char str[32];
267 int err;
268
269 tmr = q->timer;
7eaa943c
TI
270 if (snd_BUG_ON(!tmr))
271 return -EINVAL;
1da177e4
LT
272 if (tmr->timeri)
273 return -EBUSY;
274 sprintf(str, "sequencer queue %i", q->queue);
275 if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
276 return -EINVAL;
277 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
278 tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
279 err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue);
280 if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
281 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
282 tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
c7e0b5bf 283 struct snd_timer_id tid;
1da177e4
LT
284 memset(&tid, 0, sizeof(tid));
285 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
286 tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
287 tid.card = -1;
288 tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
289 err = snd_timer_open(&t, str, &tid, q->queue);
290 }
66efdc71
TI
291 }
292 if (err < 0) {
04cc79a0 293 pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err);
66efdc71 294 return err;
1da177e4
LT
295 }
296 t->callback = snd_seq_timer_interrupt;
297 t->callback_data = q;
298 t->flags |= SNDRV_TIMER_IFLG_AUTO;
299 tmr->timeri = t;
300 return 0;
301}
302
c7e0b5bf 303int snd_seq_timer_close(struct snd_seq_queue *q)
1da177e4 304{
c7e0b5bf 305 struct snd_seq_timer *tmr;
1da177e4
LT
306
307 tmr = q->timer;
7eaa943c
TI
308 if (snd_BUG_ON(!tmr))
309 return -EINVAL;
1da177e4
LT
310 if (tmr->timeri) {
311 snd_timer_stop(tmr->timeri);
312 snd_timer_close(tmr->timeri);
313 tmr->timeri = NULL;
314 }
315 return 0;
316}
317
c7e0b5bf 318int snd_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
c7e0b5bf 329static int initialize_timer(struct snd_seq_timer *tmr)
1da177e4 330{
c7e0b5bf 331 struct snd_timer *t;
87ef7779
CL
332 unsigned long freq;
333
1da177e4 334 t = tmr->timeri->timer;
7eaa943c
TI
335 if (snd_BUG_ON(!t))
336 return -EINVAL;
1da177e4 337
87ef7779
CL
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
1da177e4 346 tmr->ticks = 1;
87ef7779 347 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1da177e4
LT
348 unsigned long r = t->hw.resolution;
349 if (! r && t->hw.c_resolution)
350 r = t->hw.c_resolution(t);
351 if (r) {
87ef7779 352 tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
1da177e4
LT
353 if (! tmr->ticks)
354 tmr->ticks = 1;
355 }
356 }
357 tmr->initialized = 1;
358 return 0;
359}
360
c7e0b5bf 361int snd_seq_timer_start(struct snd_seq_timer * tmr)
1da177e4
LT
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
c7e0b5bf 376int snd_seq_timer_continue(struct snd_seq_timer * tmr)
1da177e4
LT
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. */
c7e0b5bf 394snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr)
1da177e4
LT
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) */
c7e0b5bf 419snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
1da177e4
LT
420{
421 return tmr->tick.cur_tick;
422}
423
424
cd6a6503 425#ifdef CONFIG_SND_PROC_FS
1da177e4 426/* exported to seq_info.c */
c7e0b5bf
TI
427void snd_seq_info_timer_read(struct snd_info_entry *entry,
428 struct snd_info_buffer *buffer)
1da177e4
LT
429{
430 int idx;
c7e0b5bf
TI
431 struct snd_seq_queue *q;
432 struct snd_seq_timer *tmr;
433 struct snd_timer_instance *ti;
1da177e4
LT
434 unsigned long resolution;
435
436 for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
437 q = queueptr(idx);
438 if (q == NULL)
439 continue;
440 if ((tmr = q->timer) == NULL ||
441 (ti = tmr->timeri) == NULL) {
442 queuefree(q);
443 continue;
444 }
445 snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
446 resolution = snd_timer_resolution(ti) * tmr->ticks;
447 snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
448 snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
449 queuefree(q);
450 }
451}
cd6a6503 452#endif /* CONFIG_SND_PROC_FS */
04f141a8 453
This page took 1.056355 seconds and 5 git commands to generate.