blob: 9863be6fd43e1a34aece5ffe258ffc627eb05bc2 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * ALSA sequencer Timer
4 * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02005 * Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <sound/core.h>
9#include <linux/slab.h>
10#include "seq_timer.h"
11#include "seq_queue.h"
12#include "seq_info.h"
13
Clemens Ladisch87ef7772005-10-19 14:38:30 +020014/* allowed sequencer timer frequencies, in Hz */
15#define MIN_FREQUENCY 10
16#define MAX_FREQUENCY 6250
17#define DEFAULT_FREQUENCY 1000
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define SKEW_BASE 0x10000 /* 16bit shift */
20
Clemens Ladischa32f6672010-01-18 15:40:56 +010021static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
Clemens Ladischa32f6672010-01-18 15:40:56 +010023 if (tmr->tempo < 1000000)
24 tmr->tick.resolution = (tmr->tempo * 1000) / tmr->ppq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 else {
26 /* might overflow.. */
27 unsigned int s;
Clemens Ladischa32f6672010-01-18 15:40:56 +010028 s = tmr->tempo % tmr->ppq;
29 s = (s * 1000) / tmr->ppq;
30 tmr->tick.resolution = (tmr->tempo / tmr->ppq) * 1000;
31 tmr->tick.resolution += s;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 }
Clemens Ladischa32f6672010-01-18 15:40:56 +010033 if (tmr->tick.resolution <= 0)
34 tmr->tick.resolution = 1;
35 snd_seq_timer_update_tick(&tmr->tick, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38/* create new timer (constructor) */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010039struct snd_seq_timer *snd_seq_timer_new(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010041 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Takashi Iwaiecca82b2005-09-09 14:20:49 +020043 tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
Takashi Iwai24db8bb2015-03-10 15:41:18 +010044 if (!tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 spin_lock_init(&tmr->lock);
47
48 /* reset setup to defaults */
49 snd_seq_timer_defaults(tmr);
50
51 /* reset time */
52 snd_seq_timer_reset(tmr);
53
54 return tmr;
55}
56
57/* delete timer (destructor) */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010058void snd_seq_timer_delete(struct snd_seq_timer **tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010060 struct snd_seq_timer *t = *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *tmr = NULL;
62
63 if (t == NULL) {
Takashi Iwai04cc79a2014-02-04 18:24:34 +010064 pr_debug("ALSA: seq: snd_seq_timer_delete() called with NULL timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return;
66 }
67 t->running = 0;
68
69 /* reset time */
70 snd_seq_timer_stop(t);
71 snd_seq_timer_reset(t);
72
73 kfree(t);
74}
75
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010076void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Takashi Iwai2cdc7b62016-01-30 23:30:25 +010078 unsigned long flags;
79
80 spin_lock_irqsave(&tmr->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /* setup defaults */
82 tmr->ppq = 96; /* 96 PPQ */
83 tmr->tempo = 500000; /* 120 BPM */
Clemens Ladischa32f6672010-01-18 15:40:56 +010084 snd_seq_timer_set_tick_resolution(tmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 tmr->running = 0;
86
87 tmr->type = SNDRV_SEQ_TIMER_ALSA;
88 tmr->alsa_id.dev_class = seq_default_timer_class;
89 tmr->alsa_id.dev_sclass = seq_default_timer_sclass;
90 tmr->alsa_id.card = seq_default_timer_card;
91 tmr->alsa_id.device = seq_default_timer_device;
92 tmr->alsa_id.subdevice = seq_default_timer_subdevice;
93 tmr->preferred_resolution = seq_default_timer_resolution;
94
95 tmr->skew = tmr->skew_base = SKEW_BASE;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +010096 spin_unlock_irqrestore(&tmr->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Takashi Iwai2cdc7b62016-01-30 23:30:25 +010099static void seq_timer_reset(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* reset time & songposition */
102 tmr->cur_time.tv_sec = 0;
103 tmr->cur_time.tv_nsec = 0;
104
105 tmr->tick.cur_tick = 0;
106 tmr->tick.fraction = 0;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100107}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100109void snd_seq_timer_reset(struct snd_seq_timer *tmr)
110{
111 unsigned long flags;
112
113 spin_lock_irqsave(&tmr->lock, flags);
114 seq_timer_reset(tmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 spin_unlock_irqrestore(&tmr->lock, flags);
116}
117
118
119/* called by timer interrupt routine. the period time since previous invocation is passed */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100120static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned long resolution,
122 unsigned long ticks)
123{
124 unsigned long flags;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100125 struct snd_seq_queue *q = timeri->callback_data;
126 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (q == NULL)
129 return;
130 tmr = q->timer;
131 if (tmr == NULL)
132 return;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100133 spin_lock_irqsave(&tmr->lock, flags);
134 if (!tmr->running) {
135 spin_unlock_irqrestore(&tmr->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 resolution *= ticks;
140 if (tmr->skew != tmr->skew_base) {
141 /* FIXME: assuming skew_base = 0x10000 */
142 resolution = (resolution >> 16) * tmr->skew +
143 (((resolution & 0xffff) * tmr->skew) >> 16);
144 }
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /* update timer */
147 snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
148
149 /* calculate current tick */
150 snd_seq_timer_update_tick(&tmr->tick, resolution);
151
152 /* register actual time of this timer update */
Arnd Bergmann3915bf22016-06-17 17:10:32 +0200153 ktime_get_ts64(&tmr->last_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 spin_unlock_irqrestore(&tmr->lock, flags);
156
157 /* check queues and dispatch events */
158 snd_seq_check_queue(q, 1, 0);
159}
160
161/* set current tempo */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100162int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 unsigned long flags;
165
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200166 if (snd_BUG_ON(!tmr))
167 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (tempo <= 0)
169 return -EINVAL;
170 spin_lock_irqsave(&tmr->lock, flags);
171 if ((unsigned int)tempo != tmr->tempo) {
172 tmr->tempo = tempo;
Clemens Ladischa32f6672010-01-18 15:40:56 +0100173 snd_seq_timer_set_tick_resolution(tmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175 spin_unlock_irqrestore(&tmr->lock, flags);
176 return 0;
177}
178
Takashi Iwai671ec852018-01-15 16:48:36 +0100179/* set current tempo and ppq in a shot */
180int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Takashi Iwai671ec852018-01-15 16:48:36 +0100182 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 unsigned long flags;
184
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200185 if (snd_BUG_ON(!tmr))
186 return -EINVAL;
Takashi Iwai671ec852018-01-15 16:48:36 +0100187 if (tempo <= 0 || ppq <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EINVAL;
189 spin_lock_irqsave(&tmr->lock, flags);
190 if (tmr->running && (ppq != tmr->ppq)) {
191 /* refuse to change ppq on running timers */
192 /* because it will upset the song position (ticks) */
193 spin_unlock_irqrestore(&tmr->lock, flags);
Takashi Iwai04cc79a2014-02-04 18:24:34 +0100194 pr_debug("ALSA: seq: cannot change ppq of a running timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -EBUSY;
196 }
Takashi Iwai671ec852018-01-15 16:48:36 +0100197 changed = (tempo != tmr->tempo) || (ppq != tmr->ppq);
198 tmr->tempo = tempo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 tmr->ppq = ppq;
Takashi Iwai671ec852018-01-15 16:48:36 +0100200 if (changed)
201 snd_seq_timer_set_tick_resolution(tmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 spin_unlock_irqrestore(&tmr->lock, flags);
203 return 0;
204}
205
206/* set current tick position */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100207int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
208 snd_seq_tick_time_t position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 unsigned long flags;
211
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200212 if (snd_BUG_ON(!tmr))
213 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 spin_lock_irqsave(&tmr->lock, flags);
216 tmr->tick.cur_tick = position;
217 tmr->tick.fraction = 0;
218 spin_unlock_irqrestore(&tmr->lock, flags);
219 return 0;
220}
221
222/* set current real-time position */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100223int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
224 snd_seq_real_time_t position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 unsigned long flags;
227
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200228 if (snd_BUG_ON(!tmr))
229 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 snd_seq_sanity_real_time(&position);
232 spin_lock_irqsave(&tmr->lock, flags);
233 tmr->cur_time = position;
234 spin_unlock_irqrestore(&tmr->lock, flags);
235 return 0;
236}
237
238/* set timer skew */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100239int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
240 unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 unsigned long flags;
243
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200244 if (snd_BUG_ON(!tmr))
245 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /* FIXME */
248 if (base != SKEW_BASE) {
Takashi Iwai04cc79a2014-02-04 18:24:34 +0100249 pr_debug("ALSA: seq: invalid skew base 0x%x\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return -EINVAL;
251 }
252 spin_lock_irqsave(&tmr->lock, flags);
253 tmr->skew = skew;
254 spin_unlock_irqrestore(&tmr->lock, flags);
255 return 0;
256}
257
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100258int snd_seq_timer_open(struct snd_seq_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100260 struct snd_timer_instance *t;
261 struct snd_seq_timer *tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 char str[32];
263 int err;
264
265 tmr = q->timer;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200266 if (snd_BUG_ON(!tmr))
267 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (tmr->timeri)
269 return -EBUSY;
270 sprintf(str, "sequencer queue %i", q->queue);
271 if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
272 return -EINVAL;
273 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
274 tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
Takashi Iwai6a343672019-11-07 20:20:08 +0100275 t = snd_timer_instance_new(str);
276 if (!t)
277 return -ENOMEM;
278 t->callback = snd_seq_timer_interrupt;
279 t->callback_data = q;
280 t->flags |= SNDRV_TIMER_IFLG_AUTO;
281 err = snd_timer_open(t, &tmr->alsa_id, q->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
283 if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
284 tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100285 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 memset(&tid, 0, sizeof(tid));
287 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
288 tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
289 tid.card = -1;
290 tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
Takashi Iwai6a343672019-11-07 20:20:08 +0100291 err = snd_timer_open(t, &tid, q->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
Takashi Iwai66efdc72013-03-08 18:11:17 +0100293 }
294 if (err < 0) {
Takashi Iwai04cc79a2014-02-04 18:24:34 +0100295 pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err);
Takashi Iwai6a343672019-11-07 20:20:08 +0100296 snd_timer_instance_free(t);
Takashi Iwai66efdc72013-03-08 18:11:17 +0100297 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100299 spin_lock_irq(&tmr->lock);
Takashi Iwai83e197a2021-06-10 17:20:59 +0200300 if (tmr->timeri)
301 err = -EBUSY;
302 else
303 tmr->timeri = t;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100304 spin_unlock_irq(&tmr->lock);
Takashi Iwai83e197a2021-06-10 17:20:59 +0200305 if (err < 0) {
306 snd_timer_close(t);
307 snd_timer_instance_free(t);
308 return err;
309 }
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);
Takashi Iwai6a343672019-11-07 20:20:08 +0100325 if (t) {
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100326 snd_timer_close(t);
Takashi Iwai6a343672019-11-07 20:20:08 +0100327 snd_timer_instance_free(t);
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330}
331
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100332static int seq_timer_stop(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
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
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100343int 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
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100354static int initialize_timer(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100356 struct snd_timer *t;
Clemens Ladisch87ef7772005-10-19 14:38:30 +0200357 unsigned long freq;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 t = tmr->timeri->timer;
Takashi Iwai43a35422017-11-30 10:08:28 +0100360 if (!t)
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200361 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Clemens Ladisch87ef7772005-10-19 14:38:30 +0200363 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 tmr->ticks = 1;
Clemens Ladisch87ef7772005-10-19 14:38:30 +0200372 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
Takashi Iwai21244e32018-05-17 10:43:16 +0200373 unsigned long r = snd_timer_resolution(tmr->timeri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 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 Iwaidc749772020-02-14 12:13:15 +0100439snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr,
440 bool adjust_ktime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 snd_seq_real_time_t cur_time;
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100443 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100445 spin_lock_irqsave(&tmr->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 cur_time = tmr->cur_time;
Takashi Iwaidc749772020-02-14 12:13:15 +0100447 if (adjust_ktime && tmr->running) {
Arnd Bergmann3915bf22016-06-17 17:10:32 +0200448 struct timespec64 tm;
449
450 ktime_get_ts64(&tm);
451 tm = timespec64_sub(tm, tmr->last_update);
Takashi Iwai9b508982016-10-25 15:56:35 +0200452 cur_time.tv_nsec += tm.tv_nsec;
453 cur_time.tv_sec += tm.tv_sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 snd_seq_sanity_real_time(&cur_time);
455 }
Takashi Iwai2cdc7b62016-01-30 23:30:25 +0100456 spin_unlock_irqrestore(&tmr->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return cur_time;
458}
459
460/* TODO: use interpolation on tick queue (will only be useful for very
461 high PPQ values) */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100462snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Takashi Iwaidc749772020-02-14 12:13:15 +0100464 snd_seq_tick_time_t cur_tick;
465 unsigned long flags;
466
467 spin_lock_irqsave(&tmr->lock, flags);
468 cur_tick = tmr->tick.cur_tick;
469 spin_unlock_irqrestore(&tmr->lock, flags);
470 return cur_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473
Jie Yangcd6a6502015-05-27 19:45:45 +0800474#ifdef CONFIG_SND_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475/* exported to seq_info.c */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100476void snd_seq_info_timer_read(struct snd_info_entry *entry,
477 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 int idx;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100480 struct snd_seq_queue *q;
481 struct snd_seq_timer *tmr;
482 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 unsigned long resolution;
484
485 for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
486 q = queueptr(idx);
487 if (q == NULL)
488 continue;
Takashi Iwai60adcfd2020-01-15 21:37:33 +0100489 mutex_lock(&q->timer_mutex);
490 tmr = q->timer;
491 if (!tmr)
492 goto unlock;
493 ti = tmr->timeri;
494 if (!ti)
495 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
497 resolution = snd_timer_resolution(ti) * tmr->ticks;
498 snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
499 snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
Takashi Iwai60adcfd2020-01-15 21:37:33 +0100500unlock:
501 mutex_unlock(&q->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 queuefree(q);
503 }
504}
Jie Yangcd6a6502015-05-27 19:45:45 +0800505#endif /* CONFIG_SND_PROC_FS */
Takashi Iwai04f141a2005-12-01 10:43:51 +0100506