blob: 0e1feb597586fbcab9278b4ed53367a294071c7d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA sequencer Timer
3 * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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
Clemens Ladisch87ef7772005-10-19 14:38:30 +020029/* allowed sequencer timer frequencies, in Hz */
30#define MIN_FREQUENCY 10
31#define MAX_FREQUENCY 6250
32#define DEFAULT_FREQUENCY 1000
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define SKEW_BASE 0x10000 /* 16bit shift */
35
Clemens Ladischa32f6672010-01-18 15:40:56 +010036static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Clemens Ladischa32f6672010-01-18 15:40:56 +010038 if (tmr->tempo < 1000000)
39 tmr->tick.resolution = (tmr->tempo * 1000) / tmr->ppq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 else {
41 /* might overflow.. */
42 unsigned int s;
Clemens Ladischa32f6672010-01-18 15:40:56 +010043 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
Clemens Ladischa32f6672010-01-18 15:40:56 +010048 if (tmr->tick.resolution <= 0)
49 tmr->tick.resolution = 1;
50 snd_seq_timer_update_tick(&tmr->tick, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
53/* create new timer (constructor) */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010054struct snd_seq_timer *snd_seq_timer_new(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010056 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Takashi Iwaiecca82b2005-09-09 14:20:49 +020058 tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
Takashi Iwai24db8bb2015-03-10 15:41:18 +010059 if (!tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 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) */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010073void snd_seq_timer_delete(struct snd_seq_timer **tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010075 struct snd_seq_timer *t = *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *tmr = NULL;
77
78 if (t == NULL) {
Takashi Iwai04cc79a2014-02-04 18:24:34 +010079 pr_debug("ALSA: seq: snd_seq_timer_delete() called with NULL timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 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
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010091void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Takashi Iwai2cdc7b62016-01-30 23:30:25 +010093 unsigned long flags;
94
95 spin_lock_irqsave(&tmr->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 /* setup defaults */
97 tmr->ppq = 96; /* 96 PPQ */
98 tmr->tempo = 500000; /* 120 BPM */
Clemens Ladischa32f6672010-01-18 15:40:56 +010099 snd_seq_timer_set_tick_resolution(tmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 tmr->running = 0;
101
102 tmr->type = SNDRV_SEQ_TIMER_ALSA;
103 tmr->alsa_id.dev_class = seq_default_timer_class;
104 tmr->alsa_id.dev_sclass = seq_default_timer_sclass;
105 tmr->alsa_id.card = seq_default_timer_card;
106 tmr->alsa_id.device = seq_default_timer_device;
107 tmr->alsa_id.subdevice = seq_default_timer_subdevice;
108 tmr->preferred_resolution = seq_default_timer_resolution;
109
110 tmr->skew = tmr->skew_base = SKEW_BASE;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100111 spin_unlock_irqrestore(&tmr->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100114static void seq_timer_reset(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* 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;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100122}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100124void snd_seq_timer_reset(struct snd_seq_timer *tmr)
125{
126 unsigned long flags;
127
128 spin_lock_irqsave(&tmr->lock, flags);
129 seq_timer_reset(tmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 spin_unlock_irqrestore(&tmr->lock, flags);
131}
132
133
134/* called by timer interrupt routine. the period time since previous invocation is passed */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100135static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned long resolution,
137 unsigned long ticks)
138{
139 unsigned long flags;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100140 struct snd_seq_queue *q = timeri->callback_data;
141 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 if (q == NULL)
144 return;
145 tmr = q->timer;
146 if (tmr == NULL)
147 return;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100148 spin_lock_irqsave(&tmr->lock, flags);
149 if (!tmr->running) {
150 spin_unlock_irqrestore(&tmr->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 resolution *= ticks;
155 if (tmr->skew != tmr->skew_base) {
156 /* FIXME: assuming skew_base = 0x10000 */
157 resolution = (resolution >> 16) * tmr->skew +
158 (((resolution & 0xffff) * tmr->skew) >> 16);
159 }
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* update timer */
162 snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
163
164 /* calculate current tick */
165 snd_seq_timer_update_tick(&tmr->tick, resolution);
166
167 /* register actual time of this timer update */
Arnd Bergmann3915bf22016-06-17 17:10:32 +0200168 ktime_get_ts64(&tmr->last_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 spin_unlock_irqrestore(&tmr->lock, flags);
171
172 /* check queues and dispatch events */
173 snd_seq_check_queue(q, 1, 0);
174}
175
176/* set current tempo */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100177int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 unsigned long flags;
180
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200181 if (snd_BUG_ON(!tmr))
182 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (tempo <= 0)
184 return -EINVAL;
185 spin_lock_irqsave(&tmr->lock, flags);
186 if ((unsigned int)tempo != tmr->tempo) {
187 tmr->tempo = tempo;
Clemens Ladischa32f6672010-01-18 15:40:56 +0100188 snd_seq_timer_set_tick_resolution(tmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190 spin_unlock_irqrestore(&tmr->lock, flags);
191 return 0;
192}
193
194/* set current ppq */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100195int snd_seq_timer_set_ppq(struct snd_seq_timer * tmr, int ppq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 unsigned long flags;
198
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200199 if (snd_BUG_ON(!tmr))
200 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (ppq <= 0)
202 return -EINVAL;
203 spin_lock_irqsave(&tmr->lock, flags);
204 if (tmr->running && (ppq != tmr->ppq)) {
205 /* refuse to change ppq on running timers */
206 /* because it will upset the song position (ticks) */
207 spin_unlock_irqrestore(&tmr->lock, flags);
Takashi Iwai04cc79a2014-02-04 18:24:34 +0100208 pr_debug("ALSA: seq: cannot change ppq of a running timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return -EBUSY;
210 }
211
212 tmr->ppq = ppq;
Clemens Ladischa32f6672010-01-18 15:40:56 +0100213 snd_seq_timer_set_tick_resolution(tmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 spin_unlock_irqrestore(&tmr->lock, flags);
215 return 0;
216}
217
218/* set current tick position */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100219int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
220 snd_seq_tick_time_t position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 unsigned long flags;
223
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200224 if (snd_BUG_ON(!tmr))
225 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
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;
232}
233
234/* set current real-time position */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100235int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
236 snd_seq_real_time_t position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 unsigned long flags;
239
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200240 if (snd_BUG_ON(!tmr))
241 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 snd_seq_sanity_real_time(&position);
244 spin_lock_irqsave(&tmr->lock, flags);
245 tmr->cur_time = position;
246 spin_unlock_irqrestore(&tmr->lock, flags);
247 return 0;
248}
249
250/* set timer skew */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100251int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
252 unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 unsigned long flags;
255
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200256 if (snd_BUG_ON(!tmr))
257 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* FIXME */
260 if (base != SKEW_BASE) {
Takashi Iwai04cc79a2014-02-04 18:24:34 +0100261 pr_debug("ALSA: seq: invalid skew base 0x%x\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -EINVAL;
263 }
264 spin_lock_irqsave(&tmr->lock, flags);
265 tmr->skew = skew;
266 spin_unlock_irqrestore(&tmr->lock, flags);
267 return 0;
268}
269
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100270int snd_seq_timer_open(struct snd_seq_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100272 struct snd_timer_instance *t;
273 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 char str[32];
275 int err;
276
277 tmr = q->timer;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200278 if (snd_BUG_ON(!tmr))
279 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (tmr->timeri)
281 return -EBUSY;
282 sprintf(str, "sequencer queue %i", q->queue);
283 if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
284 return -EINVAL;
285 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
286 tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
287 err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue);
288 if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
289 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
290 tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100291 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 memset(&tid, 0, sizeof(tid));
293 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
294 tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
295 tid.card = -1;
296 tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
297 err = snd_timer_open(&t, str, &tid, q->queue);
298 }
Takashi Iwai66efdc72013-03-08 18:11:17 +0100299 }
300 if (err < 0) {
Takashi Iwai04cc79a2014-02-04 18:24:34 +0100301 pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err);
Takashi Iwai66efdc72013-03-08 18:11:17 +0100302 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304 t->callback = snd_seq_timer_interrupt;
305 t->callback_data = q;
306 t->flags |= SNDRV_TIMER_IFLG_AUTO;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100307 spin_lock_irq(&tmr->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 tmr->timeri = t;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100309 spin_unlock_irq(&tmr->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 0;
311}
312
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100313int snd_seq_timer_close(struct snd_seq_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100315 struct snd_seq_timer *tmr;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100316 struct snd_timer_instance *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 tmr = q->timer;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200319 if (snd_BUG_ON(!tmr))
320 return -EINVAL;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100321 spin_lock_irq(&tmr->lock);
322 t = tmr->timeri;
323 tmr->timeri = NULL;
324 spin_unlock_irq(&tmr->lock);
325 if (t)
326 snd_timer_close(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return 0;
328}
329
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100330static int seq_timer_stop(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 if (! tmr->timeri)
333 return -EINVAL;
334 if (!tmr->running)
335 return 0;
336 tmr->running = 0;
337 snd_timer_pause(tmr->timeri);
338 return 0;
339}
340
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100341int snd_seq_timer_stop(struct snd_seq_timer *tmr)
342{
343 unsigned long flags;
344 int err;
345
346 spin_lock_irqsave(&tmr->lock, flags);
347 err = seq_timer_stop(tmr);
348 spin_unlock_irqrestore(&tmr->lock, flags);
349 return err;
350}
351
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100352static int initialize_timer(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100354 struct snd_timer *t;
Clemens Ladisch87ef7772005-10-19 14:38:30 +0200355 unsigned long freq;
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 t = tmr->timeri->timer;
Takashi Iwai20ca63e2017-11-30 10:08:28 +0100358 if (!t)
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Clemens Ladisch87ef7772005-10-19 14:38:30 +0200361 freq = tmr->preferred_resolution;
362 if (!freq)
363 freq = DEFAULT_FREQUENCY;
364 else if (freq < MIN_FREQUENCY)
365 freq = MIN_FREQUENCY;
366 else if (freq > MAX_FREQUENCY)
367 freq = MAX_FREQUENCY;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 tmr->ticks = 1;
Clemens Ladisch87ef7772005-10-19 14:38:30 +0200370 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 unsigned long r = t->hw.resolution;
372 if (! r && t->hw.c_resolution)
373 r = t->hw.c_resolution(t);
374 if (r) {
Clemens Ladisch87ef7772005-10-19 14:38:30 +0200375 tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (! tmr->ticks)
377 tmr->ticks = 1;
378 }
379 }
380 tmr->initialized = 1;
381 return 0;
382}
383
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100384static int seq_timer_start(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 if (! tmr->timeri)
387 return -EINVAL;
388 if (tmr->running)
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100389 seq_timer_stop(tmr);
390 seq_timer_reset(tmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (initialize_timer(tmr) < 0)
392 return -EINVAL;
393 snd_timer_start(tmr->timeri, tmr->ticks);
394 tmr->running = 1;
Arnd Bergmann3915bf22016-06-17 17:10:32 +0200395 ktime_get_ts64(&tmr->last_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 0;
397}
398
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100399int snd_seq_timer_start(struct snd_seq_timer *tmr)
400{
401 unsigned long flags;
402 int err;
403
404 spin_lock_irqsave(&tmr->lock, flags);
405 err = seq_timer_start(tmr);
406 spin_unlock_irqrestore(&tmr->lock, flags);
407 return err;
408}
409
410static int seq_timer_continue(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 if (! tmr->timeri)
413 return -EINVAL;
414 if (tmr->running)
415 return -EBUSY;
416 if (! tmr->initialized) {
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100417 seq_timer_reset(tmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (initialize_timer(tmr) < 0)
419 return -EINVAL;
420 }
421 snd_timer_start(tmr->timeri, tmr->ticks);
422 tmr->running = 1;
Arnd Bergmann3915bf22016-06-17 17:10:32 +0200423 ktime_get_ts64(&tmr->last_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 0;
425}
426
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100427int snd_seq_timer_continue(struct snd_seq_timer *tmr)
428{
429 unsigned long flags;
430 int err;
431
432 spin_lock_irqsave(&tmr->lock, flags);
433 err = seq_timer_continue(tmr);
434 spin_unlock_irqrestore(&tmr->lock, flags);
435 return err;
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/* return current 'real' time. use timeofday() to get better granularity. */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100439snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 snd_seq_real_time_t cur_time;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100442 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100444 spin_lock_irqsave(&tmr->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 cur_time = tmr->cur_time;
446 if (tmr->running) {
Arnd Bergmann3915bf22016-06-17 17:10:32 +0200447 struct timespec64 tm;
448
449 ktime_get_ts64(&tm);
450 tm = timespec64_sub(tm, tmr->last_update);
Takashi Iwai9b508982016-10-25 15:56:35 +0200451 cur_time.tv_nsec += tm.tv_nsec;
452 cur_time.tv_sec += tm.tv_sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 snd_seq_sanity_real_time(&cur_time);
454 }
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100455 spin_unlock_irqrestore(&tmr->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return cur_time;
457}
458
459/* TODO: use interpolation on tick queue (will only be useful for very
460 high PPQ values) */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100461snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 return tmr->tick.cur_tick;
464}
465
466
Jie Yangcd6a6502015-05-27 19:45:45 +0800467#ifdef CONFIG_SND_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/* exported to seq_info.c */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100469void snd_seq_info_timer_read(struct snd_info_entry *entry,
470 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 int idx;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100473 struct snd_seq_queue *q;
474 struct snd_seq_timer *tmr;
475 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 unsigned long resolution;
477
478 for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
479 q = queueptr(idx);
480 if (q == NULL)
481 continue;
Takashi Iwai18832462020-01-15 21:37:33 +0100482 mutex_lock(&q->timer_mutex);
483 tmr = q->timer;
484 if (!tmr)
485 goto unlock;
486 ti = tmr->timeri;
487 if (!ti)
488 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
490 resolution = snd_timer_resolution(ti) * tmr->ticks;
491 snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
492 snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
Takashi Iwai18832462020-01-15 21:37:33 +0100493unlock:
494 mutex_unlock(&q->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 queuefree(q);
496 }
497}
Jie Yangcd6a6502015-05-27 19:45:45 +0800498#endif /* CONFIG_SND_PROC_FS */
Takashi Iwai04f141a2005-12-01 10:43:51 +0100499