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