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