Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ALSA sequencer Timer |
| 3 | * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl> |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 4 | * Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Clemens Ladisch | 87ef777 | 2005-10-19 14:38:30 +0200 | [diff] [blame] | 29 | /* allowed sequencer timer frequencies, in Hz */ |
| 30 | #define MIN_FREQUENCY 10 |
| 31 | #define MAX_FREQUENCY 6250 |
| 32 | #define DEFAULT_FREQUENCY 1000 |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #define SKEW_BASE 0x10000 /* 16bit shift */ |
| 35 | |
Clemens Ladisch | a32f667 | 2010-01-18 15:40:56 +0100 | [diff] [blame] | 36 | static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer *tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Clemens Ladisch | a32f667 | 2010-01-18 15:40:56 +0100 | [diff] [blame] | 38 | if (tmr->tempo < 1000000) |
| 39 | tmr->tick.resolution = (tmr->tempo * 1000) / tmr->ppq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | else { |
| 41 | /* might overflow.. */ |
| 42 | unsigned int s; |
Clemens Ladisch | a32f667 | 2010-01-18 15:40:56 +0100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
Clemens Ladisch | a32f667 | 2010-01-18 15:40:56 +0100 | [diff] [blame] | 48 | if (tmr->tick.resolution <= 0) |
| 49 | tmr->tick.resolution = 1; |
| 50 | snd_seq_timer_update_tick(&tmr->tick, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* create new timer (constructor) */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 54 | struct snd_seq_timer *snd_seq_timer_new(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 56 | struct snd_seq_timer *tmr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Takashi Iwai | ecca82b | 2005-09-09 14:20:49 +0200 | [diff] [blame] | 58 | tmr = kzalloc(sizeof(*tmr), GFP_KERNEL); |
Takashi Iwai | 24db8bb | 2015-03-10 15:41:18 +0100 | [diff] [blame] | 59 | if (!tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 73 | void snd_seq_timer_delete(struct snd_seq_timer **tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 75 | struct snd_seq_timer *t = *tmr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | *tmr = NULL; |
| 77 | |
| 78 | if (t == NULL) { |
Takashi Iwai | 04cc79a | 2014-02-04 18:24:34 +0100 | [diff] [blame] | 79 | pr_debug("ALSA: seq: snd_seq_timer_delete() called with NULL timer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 91 | void snd_seq_timer_defaults(struct snd_seq_timer * tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 93 | unsigned long flags; |
| 94 | |
| 95 | spin_lock_irqsave(&tmr->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | /* setup defaults */ |
| 97 | tmr->ppq = 96; /* 96 PPQ */ |
| 98 | tmr->tempo = 500000; /* 120 BPM */ |
Clemens Ladisch | a32f667 | 2010-01-18 15:40:56 +0100 | [diff] [blame] | 99 | snd_seq_timer_set_tick_resolution(tmr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | 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 Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 111 | spin_unlock_irqrestore(&tmr->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 114 | static void seq_timer_reset(struct snd_seq_timer *tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 122 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 124 | void 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* called by timer interrupt routine. the period time since previous invocation is passed */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 135 | static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | unsigned long resolution, |
| 137 | unsigned long ticks) |
| 138 | { |
| 139 | unsigned long flags; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 140 | struct snd_seq_queue *q = timeri->callback_data; |
| 141 | struct snd_seq_timer *tmr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | if (q == NULL) |
| 144 | return; |
| 145 | tmr = q->timer; |
| 146 | if (tmr == NULL) |
| 147 | return; |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 148 | spin_lock_irqsave(&tmr->lock, flags); |
| 149 | if (!tmr->running) { |
| 150 | spin_unlock_irqrestore(&tmr->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return; |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* 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 Bergmann | 3915bf2 | 2016-06-17 17:10:32 +0200 | [diff] [blame] | 168 | ktime_get_ts64(&tmr->last_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 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 Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 177 | int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
| 179 | unsigned long flags; |
| 180 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 181 | if (snd_BUG_ON(!tmr)) |
| 182 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | 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 Ladisch | a32f667 | 2010-01-18 15:40:56 +0100 | [diff] [blame] | 188 | snd_seq_timer_set_tick_resolution(tmr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 191 | return 0; |
| 192 | } |
| 193 | |
Takashi Iwai | 671ec85 | 2018-01-15 16:48:36 +0100 | [diff] [blame] | 194 | /* set current tempo and ppq in a shot */ |
| 195 | int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
Takashi Iwai | 671ec85 | 2018-01-15 16:48:36 +0100 | [diff] [blame] | 197 | int changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | unsigned long flags; |
| 199 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 200 | if (snd_BUG_ON(!tmr)) |
| 201 | return -EINVAL; |
Takashi Iwai | 671ec85 | 2018-01-15 16:48:36 +0100 | [diff] [blame] | 202 | if (tempo <= 0 || ppq <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Takashi Iwai | 04cc79a | 2014-02-04 18:24:34 +0100 | [diff] [blame] | 209 | pr_debug("ALSA: seq: cannot change ppq of a running timer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | return -EBUSY; |
| 211 | } |
Takashi Iwai | 671ec85 | 2018-01-15 16:48:36 +0100 | [diff] [blame] | 212 | changed = (tempo != tmr->tempo) || (ppq != tmr->ppq); |
| 213 | tmr->tempo = tempo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | tmr->ppq = ppq; |
Takashi Iwai | 671ec85 | 2018-01-15 16:48:36 +0100 | [diff] [blame] | 215 | if (changed) |
| 216 | snd_seq_timer_set_tick_resolution(tmr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /* set current tick position */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 222 | int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr, |
| 223 | snd_seq_tick_time_t position) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
| 225 | unsigned long flags; |
| 226 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 227 | if (snd_BUG_ON(!tmr)) |
| 228 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | spin_lock_irqsave(&tmr->lock, flags); |
| 231 | tmr->tick.cur_tick = position; |
| 232 | tmr->tick.fraction = 0; |
| 233 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /* set current real-time position */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 238 | int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr, |
| 239 | snd_seq_real_time_t position) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
| 241 | unsigned long flags; |
| 242 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 243 | if (snd_BUG_ON(!tmr)) |
| 244 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | snd_seq_sanity_real_time(&position); |
| 247 | spin_lock_irqsave(&tmr->lock, flags); |
| 248 | tmr->cur_time = position; |
| 249 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | /* set timer skew */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 254 | int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew, |
| 255 | unsigned int base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
| 257 | unsigned long flags; |
| 258 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 259 | if (snd_BUG_ON(!tmr)) |
| 260 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | /* FIXME */ |
| 263 | if (base != SKEW_BASE) { |
Takashi Iwai | 04cc79a | 2014-02-04 18:24:34 +0100 | [diff] [blame] | 264 | pr_debug("ALSA: seq: invalid skew base 0x%x\n", base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | return -EINVAL; |
| 266 | } |
| 267 | spin_lock_irqsave(&tmr->lock, flags); |
| 268 | tmr->skew = skew; |
| 269 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 270 | return 0; |
| 271 | } |
| 272 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 273 | int snd_seq_timer_open(struct snd_seq_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 275 | struct snd_timer_instance *t; |
| 276 | struct snd_seq_timer *tmr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | char str[32]; |
| 278 | int err; |
| 279 | |
| 280 | tmr = q->timer; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 281 | if (snd_BUG_ON(!tmr)) |
| 282 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | if (tmr->timeri) |
| 284 | return -EBUSY; |
| 285 | sprintf(str, "sequencer queue %i", q->queue); |
| 286 | if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */ |
| 287 | return -EINVAL; |
| 288 | if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) |
| 289 | tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER; |
| 290 | err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue); |
| 291 | if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) { |
| 292 | if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL || |
| 293 | tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 294 | struct snd_timer_id tid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | memset(&tid, 0, sizeof(tid)); |
| 296 | tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL; |
| 297 | tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER; |
| 298 | tid.card = -1; |
| 299 | tid.device = SNDRV_TIMER_GLOBAL_SYSTEM; |
| 300 | err = snd_timer_open(&t, str, &tid, q->queue); |
| 301 | } |
Takashi Iwai | 66efdc7 | 2013-03-08 18:11:17 +0100 | [diff] [blame] | 302 | } |
| 303 | if (err < 0) { |
Takashi Iwai | 04cc79a | 2014-02-04 18:24:34 +0100 | [diff] [blame] | 304 | pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err); |
Takashi Iwai | 66efdc7 | 2013-03-08 18:11:17 +0100 | [diff] [blame] | 305 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | t->callback = snd_seq_timer_interrupt; |
| 308 | t->callback_data = q; |
| 309 | t->flags |= SNDRV_TIMER_IFLG_AUTO; |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 310 | spin_lock_irq(&tmr->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | tmr->timeri = t; |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 312 | spin_unlock_irq(&tmr->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 316 | int snd_seq_timer_close(struct snd_seq_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 318 | struct snd_seq_timer *tmr; |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 319 | struct snd_timer_instance *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
| 321 | tmr = q->timer; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 322 | if (snd_BUG_ON(!tmr)) |
| 323 | return -EINVAL; |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 324 | spin_lock_irq(&tmr->lock); |
| 325 | t = tmr->timeri; |
| 326 | tmr->timeri = NULL; |
| 327 | spin_unlock_irq(&tmr->lock); |
| 328 | if (t) |
| 329 | snd_timer_close(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 333 | static int seq_timer_stop(struct snd_seq_timer *tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
| 335 | if (! tmr->timeri) |
| 336 | return -EINVAL; |
| 337 | if (!tmr->running) |
| 338 | return 0; |
| 339 | tmr->running = 0; |
| 340 | snd_timer_pause(tmr->timeri); |
| 341 | return 0; |
| 342 | } |
| 343 | |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 344 | int snd_seq_timer_stop(struct snd_seq_timer *tmr) |
| 345 | { |
| 346 | unsigned long flags; |
| 347 | int err; |
| 348 | |
| 349 | spin_lock_irqsave(&tmr->lock, flags); |
| 350 | err = seq_timer_stop(tmr); |
| 351 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 352 | return err; |
| 353 | } |
| 354 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 355 | static int initialize_timer(struct snd_seq_timer *tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 357 | struct snd_timer *t; |
Clemens Ladisch | 87ef777 | 2005-10-19 14:38:30 +0200 | [diff] [blame] | 358 | unsigned long freq; |
| 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | t = tmr->timeri->timer; |
Takashi Iwai | 43a3542 | 2017-11-30 10:08:28 +0100 | [diff] [blame] | 361 | if (!t) |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 362 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Clemens Ladisch | 87ef777 | 2005-10-19 14:38:30 +0200 | [diff] [blame] | 364 | freq = tmr->preferred_resolution; |
| 365 | if (!freq) |
| 366 | freq = DEFAULT_FREQUENCY; |
| 367 | else if (freq < MIN_FREQUENCY) |
| 368 | freq = MIN_FREQUENCY; |
| 369 | else if (freq > MAX_FREQUENCY) |
| 370 | freq = MAX_FREQUENCY; |
| 371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | tmr->ticks = 1; |
Clemens Ladisch | 87ef777 | 2005-10-19 14:38:30 +0200 | [diff] [blame] | 373 | if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) { |
Takashi Iwai | 21244e3 | 2018-05-17 10:43:16 +0200 | [diff] [blame] | 374 | unsigned long r = snd_timer_resolution(tmr->timeri); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | if (r) { |
Clemens Ladisch | 87ef777 | 2005-10-19 14:38:30 +0200 | [diff] [blame] | 376 | tmr->ticks = (unsigned int)(1000000000uL / (r * freq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | if (! tmr->ticks) |
| 378 | tmr->ticks = 1; |
| 379 | } |
| 380 | } |
| 381 | tmr->initialized = 1; |
| 382 | return 0; |
| 383 | } |
| 384 | |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 385 | static int seq_timer_start(struct snd_seq_timer *tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | if (! tmr->timeri) |
| 388 | return -EINVAL; |
| 389 | if (tmr->running) |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 390 | seq_timer_stop(tmr); |
| 391 | seq_timer_reset(tmr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | if (initialize_timer(tmr) < 0) |
| 393 | return -EINVAL; |
| 394 | snd_timer_start(tmr->timeri, tmr->ticks); |
| 395 | tmr->running = 1; |
Arnd Bergmann | 3915bf2 | 2016-06-17 17:10:32 +0200 | [diff] [blame] | 396 | ktime_get_ts64(&tmr->last_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 400 | int snd_seq_timer_start(struct snd_seq_timer *tmr) |
| 401 | { |
| 402 | unsigned long flags; |
| 403 | int err; |
| 404 | |
| 405 | spin_lock_irqsave(&tmr->lock, flags); |
| 406 | err = seq_timer_start(tmr); |
| 407 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 408 | return err; |
| 409 | } |
| 410 | |
| 411 | static int seq_timer_continue(struct snd_seq_timer *tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
| 413 | if (! tmr->timeri) |
| 414 | return -EINVAL; |
| 415 | if (tmr->running) |
| 416 | return -EBUSY; |
| 417 | if (! tmr->initialized) { |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 418 | seq_timer_reset(tmr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | if (initialize_timer(tmr) < 0) |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | snd_timer_start(tmr->timeri, tmr->ticks); |
| 423 | tmr->running = 1; |
Arnd Bergmann | 3915bf2 | 2016-06-17 17:10:32 +0200 | [diff] [blame] | 424 | ktime_get_ts64(&tmr->last_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 428 | int snd_seq_timer_continue(struct snd_seq_timer *tmr) |
| 429 | { |
| 430 | unsigned long flags; |
| 431 | int err; |
| 432 | |
| 433 | spin_lock_irqsave(&tmr->lock, flags); |
| 434 | err = seq_timer_continue(tmr); |
| 435 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 436 | return err; |
| 437 | } |
| 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | /* return current 'real' time. use timeofday() to get better granularity. */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 440 | snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
| 442 | snd_seq_real_time_t cur_time; |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 443 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 445 | spin_lock_irqsave(&tmr->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | cur_time = tmr->cur_time; |
| 447 | if (tmr->running) { |
Arnd Bergmann | 3915bf2 | 2016-06-17 17:10:32 +0200 | [diff] [blame] | 448 | struct timespec64 tm; |
| 449 | |
| 450 | ktime_get_ts64(&tm); |
| 451 | tm = timespec64_sub(tm, tmr->last_update); |
Takashi Iwai | 9b50898 | 2016-10-25 15:56:35 +0200 | [diff] [blame] | 452 | cur_time.tv_nsec += tm.tv_nsec; |
| 453 | cur_time.tv_sec += tm.tv_sec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | snd_seq_sanity_real_time(&cur_time); |
| 455 | } |
Takashi Iwai | 2cdc7b6 | 2016-01-30 23:30:25 +0100 | [diff] [blame] | 456 | spin_unlock_irqrestore(&tmr->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | return cur_time; |
| 458 | } |
| 459 | |
| 460 | /* TODO: use interpolation on tick queue (will only be useful for very |
| 461 | high PPQ values) */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 462 | snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
| 464 | return tmr->tick.cur_tick; |
| 465 | } |
| 466 | |
| 467 | |
Jie Yang | cd6a650 | 2015-05-27 19:45:45 +0800 | [diff] [blame] | 468 | #ifdef CONFIG_SND_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | /* exported to seq_info.c */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 470 | void snd_seq_info_timer_read(struct snd_info_entry *entry, |
| 471 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
| 473 | int idx; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 474 | struct snd_seq_queue *q; |
| 475 | struct snd_seq_timer *tmr; |
| 476 | struct snd_timer_instance *ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | unsigned long resolution; |
| 478 | |
| 479 | for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) { |
| 480 | q = queueptr(idx); |
| 481 | if (q == NULL) |
| 482 | continue; |
| 483 | if ((tmr = q->timer) == NULL || |
| 484 | (ti = tmr->timeri) == NULL) { |
| 485 | queuefree(q); |
| 486 | continue; |
| 487 | } |
| 488 | snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name); |
| 489 | resolution = snd_timer_resolution(ti) * tmr->ticks; |
| 490 | snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000); |
| 491 | snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base); |
| 492 | queuefree(q); |
| 493 | } |
| 494 | } |
Jie Yang | cd6a650 | 2015-05-27 19:45:45 +0800 | [diff] [blame] | 495 | #endif /* CONFIG_SND_PROC_FS */ |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 496 | |