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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | if (tmr == NULL) { |
Takashi Iwai | 04cc79a | 2014-02-04 18:24:34 +0100 | [diff] [blame] | 60 | pr_debug("ALSA: seq: malloc failed for snd_seq_timer_new() \n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return NULL; |
| 62 | } |
| 63 | spin_lock_init(&tmr->lock); |
| 64 | |
| 65 | /* reset setup to defaults */ |
| 66 | snd_seq_timer_defaults(tmr); |
| 67 | |
| 68 | /* reset time */ |
| 69 | snd_seq_timer_reset(tmr); |
| 70 | |
| 71 | return tmr; |
| 72 | } |
| 73 | |
| 74 | /* delete timer (destructor) */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 75 | void snd_seq_timer_delete(struct snd_seq_timer **tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 77 | struct snd_seq_timer *t = *tmr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | *tmr = NULL; |
| 79 | |
| 80 | if (t == NULL) { |
Takashi Iwai | 04cc79a | 2014-02-04 18:24:34 +0100 | [diff] [blame] | 81 | 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] | 82 | return; |
| 83 | } |
| 84 | t->running = 0; |
| 85 | |
| 86 | /* reset time */ |
| 87 | snd_seq_timer_stop(t); |
| 88 | snd_seq_timer_reset(t); |
| 89 | |
| 90 | kfree(t); |
| 91 | } |
| 92 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 93 | void snd_seq_timer_defaults(struct snd_seq_timer * tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
| 95 | /* setup defaults */ |
| 96 | tmr->ppq = 96; /* 96 PPQ */ |
| 97 | tmr->tempo = 500000; /* 120 BPM */ |
Clemens Ladisch | a32f667 | 2010-01-18 15:40:56 +0100 | [diff] [blame] | 98 | snd_seq_timer_set_tick_resolution(tmr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | tmr->running = 0; |
| 100 | |
| 101 | tmr->type = SNDRV_SEQ_TIMER_ALSA; |
| 102 | tmr->alsa_id.dev_class = seq_default_timer_class; |
| 103 | tmr->alsa_id.dev_sclass = seq_default_timer_sclass; |
| 104 | tmr->alsa_id.card = seq_default_timer_card; |
| 105 | tmr->alsa_id.device = seq_default_timer_device; |
| 106 | tmr->alsa_id.subdevice = seq_default_timer_subdevice; |
| 107 | tmr->preferred_resolution = seq_default_timer_resolution; |
| 108 | |
| 109 | tmr->skew = tmr->skew_base = SKEW_BASE; |
| 110 | } |
| 111 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 112 | void snd_seq_timer_reset(struct snd_seq_timer * tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
| 114 | unsigned long flags; |
| 115 | |
| 116 | spin_lock_irqsave(&tmr->lock, flags); |
| 117 | |
| 118 | /* reset time & songposition */ |
| 119 | tmr->cur_time.tv_sec = 0; |
| 120 | tmr->cur_time.tv_nsec = 0; |
| 121 | |
| 122 | tmr->tick.cur_tick = 0; |
| 123 | tmr->tick.fraction = 0; |
| 124 | |
| 125 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /* 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] | 130 | static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | unsigned long resolution, |
| 132 | unsigned long ticks) |
| 133 | { |
| 134 | unsigned long flags; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 135 | struct snd_seq_queue *q = timeri->callback_data; |
| 136 | struct snd_seq_timer *tmr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | if (q == NULL) |
| 139 | return; |
| 140 | tmr = q->timer; |
| 141 | if (tmr == NULL) |
| 142 | return; |
| 143 | if (!tmr->running) |
| 144 | return; |
| 145 | |
| 146 | resolution *= ticks; |
| 147 | if (tmr->skew != tmr->skew_base) { |
| 148 | /* FIXME: assuming skew_base = 0x10000 */ |
| 149 | resolution = (resolution >> 16) * tmr->skew + |
| 150 | (((resolution & 0xffff) * tmr->skew) >> 16); |
| 151 | } |
| 152 | |
| 153 | spin_lock_irqsave(&tmr->lock, flags); |
| 154 | |
| 155 | /* update timer */ |
| 156 | snd_seq_inc_time_nsec(&tmr->cur_time, resolution); |
| 157 | |
| 158 | /* calculate current tick */ |
| 159 | snd_seq_timer_update_tick(&tmr->tick, resolution); |
| 160 | |
| 161 | /* register actual time of this timer update */ |
| 162 | do_gettimeofday(&tmr->last_update); |
| 163 | |
| 164 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 165 | |
| 166 | /* check queues and dispatch events */ |
| 167 | snd_seq_check_queue(q, 1, 0); |
| 168 | } |
| 169 | |
| 170 | /* set current tempo */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 171 | 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] | 172 | { |
| 173 | unsigned long flags; |
| 174 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 175 | if (snd_BUG_ON(!tmr)) |
| 176 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | if (tempo <= 0) |
| 178 | return -EINVAL; |
| 179 | spin_lock_irqsave(&tmr->lock, flags); |
| 180 | if ((unsigned int)tempo != tmr->tempo) { |
| 181 | tmr->tempo = tempo; |
Clemens Ladisch | a32f667 | 2010-01-18 15:40:56 +0100 | [diff] [blame] | 182 | snd_seq_timer_set_tick_resolution(tmr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /* set current ppq */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 189 | int snd_seq_timer_set_ppq(struct snd_seq_timer * tmr, int ppq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
| 191 | unsigned long flags; |
| 192 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 193 | if (snd_BUG_ON(!tmr)) |
| 194 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Takashi Iwai | 04cc79a | 2014-02-04 18:24:34 +0100 | [diff] [blame] | 202 | pr_debug("ALSA: seq: cannot change ppq of a running timer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | return -EBUSY; |
| 204 | } |
| 205 | |
| 206 | tmr->ppq = ppq; |
Clemens Ladisch | a32f667 | 2010-01-18 15:40:56 +0100 | [diff] [blame] | 207 | snd_seq_timer_set_tick_resolution(tmr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* set current tick position */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 213 | int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr, |
| 214 | snd_seq_tick_time_t position) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
| 216 | unsigned long flags; |
| 217 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 218 | if (snd_BUG_ON(!tmr)) |
| 219 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | spin_lock_irqsave(&tmr->lock, flags); |
| 222 | tmr->tick.cur_tick = position; |
| 223 | tmr->tick.fraction = 0; |
| 224 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | /* set current real-time position */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 229 | int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr, |
| 230 | snd_seq_real_time_t position) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
| 232 | unsigned long flags; |
| 233 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 234 | if (snd_BUG_ON(!tmr)) |
| 235 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | snd_seq_sanity_real_time(&position); |
| 238 | spin_lock_irqsave(&tmr->lock, flags); |
| 239 | tmr->cur_time = position; |
| 240 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /* set timer skew */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 245 | int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew, |
| 246 | unsigned int base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
| 248 | unsigned long flags; |
| 249 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 250 | if (snd_BUG_ON(!tmr)) |
| 251 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | /* FIXME */ |
| 254 | if (base != SKEW_BASE) { |
Takashi Iwai | 04cc79a | 2014-02-04 18:24:34 +0100 | [diff] [blame] | 255 | pr_debug("ALSA: seq: invalid skew base 0x%x\n", base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | return -EINVAL; |
| 257 | } |
| 258 | spin_lock_irqsave(&tmr->lock, flags); |
| 259 | tmr->skew = skew; |
| 260 | spin_unlock_irqrestore(&tmr->lock, flags); |
| 261 | return 0; |
| 262 | } |
| 263 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 264 | int snd_seq_timer_open(struct snd_seq_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 266 | struct snd_timer_instance *t; |
| 267 | struct snd_seq_timer *tmr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | char str[32]; |
| 269 | int err; |
| 270 | |
| 271 | tmr = q->timer; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 272 | if (snd_BUG_ON(!tmr)) |
| 273 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | if (tmr->timeri) |
| 275 | return -EBUSY; |
| 276 | sprintf(str, "sequencer queue %i", q->queue); |
| 277 | if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */ |
| 278 | return -EINVAL; |
| 279 | if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) |
| 280 | tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER; |
| 281 | err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue); |
| 282 | 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 Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 285 | struct snd_timer_id tid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | 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; |
| 291 | err = snd_timer_open(&t, str, &tid, q->queue); |
| 292 | } |
Takashi Iwai | 66efdc7 | 2013-03-08 18:11:17 +0100 | [diff] [blame] | 293 | } |
| 294 | if (err < 0) { |
Takashi Iwai | 04cc79a | 2014-02-04 18:24:34 +0100 | [diff] [blame] | 295 | pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err); |
Takashi Iwai | 66efdc7 | 2013-03-08 18:11:17 +0100 | [diff] [blame] | 296 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | t->callback = snd_seq_timer_interrupt; |
| 299 | t->callback_data = q; |
| 300 | t->flags |= SNDRV_TIMER_IFLG_AUTO; |
| 301 | tmr->timeri = t; |
| 302 | return 0; |
| 303 | } |
| 304 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 305 | int snd_seq_timer_close(struct snd_seq_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 307 | struct snd_seq_timer *tmr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | tmr = q->timer; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 310 | if (snd_BUG_ON(!tmr)) |
| 311 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | if (tmr->timeri) { |
| 313 | snd_timer_stop(tmr->timeri); |
| 314 | snd_timer_close(tmr->timeri); |
| 315 | tmr->timeri = NULL; |
| 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 320 | int snd_seq_timer_stop(struct snd_seq_timer * tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
| 322 | if (! tmr->timeri) |
| 323 | return -EINVAL; |
| 324 | if (!tmr->running) |
| 325 | return 0; |
| 326 | tmr->running = 0; |
| 327 | snd_timer_pause(tmr->timeri); |
| 328 | return 0; |
| 329 | } |
| 330 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 331 | static int initialize_timer(struct snd_seq_timer *tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 333 | struct snd_timer *t; |
Clemens Ladisch | 87ef777 | 2005-10-19 14:38:30 +0200 | [diff] [blame] | 334 | unsigned long freq; |
| 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | t = tmr->timeri->timer; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 337 | if (snd_BUG_ON(!t)) |
| 338 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
Clemens Ladisch | 87ef777 | 2005-10-19 14:38:30 +0200 | [diff] [blame] | 340 | freq = tmr->preferred_resolution; |
| 341 | if (!freq) |
| 342 | freq = DEFAULT_FREQUENCY; |
| 343 | else if (freq < MIN_FREQUENCY) |
| 344 | freq = MIN_FREQUENCY; |
| 345 | else if (freq > MAX_FREQUENCY) |
| 346 | freq = MAX_FREQUENCY; |
| 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | tmr->ticks = 1; |
Clemens Ladisch | 87ef777 | 2005-10-19 14:38:30 +0200 | [diff] [blame] | 349 | if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | unsigned long r = t->hw.resolution; |
| 351 | if (! r && t->hw.c_resolution) |
| 352 | r = t->hw.c_resolution(t); |
| 353 | if (r) { |
Clemens Ladisch | 87ef777 | 2005-10-19 14:38:30 +0200 | [diff] [blame] | 354 | tmr->ticks = (unsigned int)(1000000000uL / (r * freq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | if (! tmr->ticks) |
| 356 | tmr->ticks = 1; |
| 357 | } |
| 358 | } |
| 359 | tmr->initialized = 1; |
| 360 | return 0; |
| 361 | } |
| 362 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 363 | int snd_seq_timer_start(struct snd_seq_timer * tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
| 365 | if (! tmr->timeri) |
| 366 | return -EINVAL; |
| 367 | if (tmr->running) |
| 368 | snd_seq_timer_stop(tmr); |
| 369 | snd_seq_timer_reset(tmr); |
| 370 | if (initialize_timer(tmr) < 0) |
| 371 | return -EINVAL; |
| 372 | snd_timer_start(tmr->timeri, tmr->ticks); |
| 373 | tmr->running = 1; |
| 374 | do_gettimeofday(&tmr->last_update); |
| 375 | return 0; |
| 376 | } |
| 377 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 378 | int snd_seq_timer_continue(struct snd_seq_timer * tmr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
| 380 | if (! tmr->timeri) |
| 381 | return -EINVAL; |
| 382 | if (tmr->running) |
| 383 | return -EBUSY; |
| 384 | if (! tmr->initialized) { |
| 385 | snd_seq_timer_reset(tmr); |
| 386 | if (initialize_timer(tmr) < 0) |
| 387 | return -EINVAL; |
| 388 | } |
| 389 | snd_timer_start(tmr->timeri, tmr->ticks); |
| 390 | tmr->running = 1; |
| 391 | do_gettimeofday(&tmr->last_update); |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | /* return current 'real' time. use timeofday() to get better granularity. */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 396 | 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] | 397 | { |
| 398 | snd_seq_real_time_t cur_time; |
| 399 | |
| 400 | cur_time = tmr->cur_time; |
| 401 | if (tmr->running) { |
| 402 | struct timeval tm; |
| 403 | int usec; |
| 404 | do_gettimeofday(&tm); |
| 405 | usec = (int)(tm.tv_usec - tmr->last_update.tv_usec); |
| 406 | if (usec < 0) { |
| 407 | cur_time.tv_nsec += (1000000 + usec) * 1000; |
| 408 | cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec - 1; |
| 409 | } else { |
| 410 | cur_time.tv_nsec += usec * 1000; |
| 411 | cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec; |
| 412 | } |
| 413 | snd_seq_sanity_real_time(&cur_time); |
| 414 | } |
| 415 | |
| 416 | return cur_time; |
| 417 | } |
| 418 | |
| 419 | /* TODO: use interpolation on tick queue (will only be useful for very |
| 420 | high PPQ values) */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 421 | 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] | 422 | { |
| 423 | return tmr->tick.cur_tick; |
| 424 | } |
| 425 | |
| 426 | |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 427 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | /* exported to seq_info.c */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 429 | void snd_seq_info_timer_read(struct snd_info_entry *entry, |
| 430 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
| 432 | int idx; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 433 | struct snd_seq_queue *q; |
| 434 | struct snd_seq_timer *tmr; |
| 435 | struct snd_timer_instance *ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | unsigned long resolution; |
| 437 | |
| 438 | for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) { |
| 439 | q = queueptr(idx); |
| 440 | if (q == NULL) |
| 441 | continue; |
| 442 | if ((tmr = q->timer) == NULL || |
| 443 | (ti = tmr->timeri) == NULL) { |
| 444 | queuefree(q); |
| 445 | continue; |
| 446 | } |
| 447 | snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name); |
| 448 | resolution = snd_timer_resolution(ti) * tmr->ticks; |
| 449 | snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000); |
| 450 | snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base); |
| 451 | queuefree(q); |
| 452 | } |
| 453 | } |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 454 | #endif /* CONFIG_PROC_FS */ |
| 455 | |