seq_timer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * ALSA sequencer Timer
  3. * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
  4. * Jaroslav Kysela <perex@suse.cz>
  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. #include <sound/driver.h>
  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. extern int seq_default_timer_class;
  29. extern int seq_default_timer_sclass;
  30. extern int seq_default_timer_card;
  31. extern int seq_default_timer_device;
  32. extern int seq_default_timer_subdevice;
  33. extern int seq_default_timer_resolution;
  34. /* allowed sequencer timer frequencies, in Hz */
  35. #define MIN_FREQUENCY 10
  36. #define MAX_FREQUENCY 6250
  37. #define DEFAULT_FREQUENCY 1000
  38. #define SKEW_BASE 0x10000 /* 16bit shift */
  39. static void snd_seq_timer_set_tick_resolution(seq_timer_tick_t *tick,
  40. int tempo, int ppq)
  41. {
  42. if (tempo < 1000000)
  43. tick->resolution = (tempo * 1000) / ppq;
  44. else {
  45. /* might overflow.. */
  46. unsigned int s;
  47. s = tempo % ppq;
  48. s = (s * 1000) / ppq;
  49. tick->resolution = (tempo / ppq) * 1000;
  50. tick->resolution += s;
  51. }
  52. if (tick->resolution <= 0)
  53. tick->resolution = 1;
  54. snd_seq_timer_update_tick(tick, 0);
  55. }
  56. /* create new timer (constructor) */
  57. seq_timer_t *snd_seq_timer_new(void)
  58. {
  59. seq_timer_t *tmr;
  60. tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
  61. if (tmr == NULL) {
  62. snd_printd("malloc failed for snd_seq_timer_new() \n");
  63. return NULL;
  64. }
  65. spin_lock_init(&tmr->lock);
  66. /* reset setup to defaults */
  67. snd_seq_timer_defaults(tmr);
  68. /* reset time */
  69. snd_seq_timer_reset(tmr);
  70. return tmr;
  71. }
  72. /* delete timer (destructor) */
  73. void snd_seq_timer_delete(seq_timer_t **tmr)
  74. {
  75. seq_timer_t *t = *tmr;
  76. *tmr = NULL;
  77. if (t == NULL) {
  78. snd_printd("oops: snd_seq_timer_delete() called with NULL timer\n");
  79. return;
  80. }
  81. t->running = 0;
  82. /* reset time */
  83. snd_seq_timer_stop(t);
  84. snd_seq_timer_reset(t);
  85. kfree(t);
  86. }
  87. void snd_seq_timer_defaults(seq_timer_t * tmr)
  88. {
  89. /* setup defaults */
  90. tmr->ppq = 96; /* 96 PPQ */
  91. tmr->tempo = 500000; /* 120 BPM */
  92. snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
  93. tmr->running = 0;
  94. tmr->type = SNDRV_SEQ_TIMER_ALSA;
  95. tmr->alsa_id.dev_class = seq_default_timer_class;
  96. tmr->alsa_id.dev_sclass = seq_default_timer_sclass;
  97. tmr->alsa_id.card = seq_default_timer_card;
  98. tmr->alsa_id.device = seq_default_timer_device;
  99. tmr->alsa_id.subdevice = seq_default_timer_subdevice;
  100. tmr->preferred_resolution = seq_default_timer_resolution;
  101. tmr->skew = tmr->skew_base = SKEW_BASE;
  102. }
  103. void snd_seq_timer_reset(seq_timer_t * tmr)
  104. {
  105. unsigned long flags;
  106. spin_lock_irqsave(&tmr->lock, flags);
  107. /* reset time & songposition */
  108. tmr->cur_time.tv_sec = 0;
  109. tmr->cur_time.tv_nsec = 0;
  110. tmr->tick.cur_tick = 0;
  111. tmr->tick.fraction = 0;
  112. spin_unlock_irqrestore(&tmr->lock, flags);
  113. }
  114. /* called by timer interrupt routine. the period time since previous invocation is passed */
  115. static void snd_seq_timer_interrupt(snd_timer_instance_t *timeri,
  116. unsigned long resolution,
  117. unsigned long ticks)
  118. {
  119. unsigned long flags;
  120. queue_t *q = (queue_t *)timeri->callback_data;
  121. seq_timer_t *tmr;
  122. if (q == NULL)
  123. return;
  124. tmr = q->timer;
  125. if (tmr == NULL)
  126. return;
  127. if (!tmr->running)
  128. return;
  129. resolution *= ticks;
  130. if (tmr->skew != tmr->skew_base) {
  131. /* FIXME: assuming skew_base = 0x10000 */
  132. resolution = (resolution >> 16) * tmr->skew +
  133. (((resolution & 0xffff) * tmr->skew) >> 16);
  134. }
  135. spin_lock_irqsave(&tmr->lock, flags);
  136. /* update timer */
  137. snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
  138. /* calculate current tick */
  139. snd_seq_timer_update_tick(&tmr->tick, resolution);
  140. /* register actual time of this timer update */
  141. do_gettimeofday(&tmr->last_update);
  142. spin_unlock_irqrestore(&tmr->lock, flags);
  143. /* check queues and dispatch events */
  144. snd_seq_check_queue(q, 1, 0);
  145. }
  146. /* set current tempo */
  147. int snd_seq_timer_set_tempo(seq_timer_t * tmr, int tempo)
  148. {
  149. unsigned long flags;
  150. snd_assert(tmr, return -EINVAL);
  151. if (tempo <= 0)
  152. return -EINVAL;
  153. spin_lock_irqsave(&tmr->lock, flags);
  154. if ((unsigned int)tempo != tmr->tempo) {
  155. tmr->tempo = tempo;
  156. snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
  157. }
  158. spin_unlock_irqrestore(&tmr->lock, flags);
  159. return 0;
  160. }
  161. /* set current ppq */
  162. int snd_seq_timer_set_ppq(seq_timer_t * tmr, int ppq)
  163. {
  164. unsigned long flags;
  165. snd_assert(tmr, return -EINVAL);
  166. if (ppq <= 0)
  167. return -EINVAL;
  168. spin_lock_irqsave(&tmr->lock, flags);
  169. if (tmr->running && (ppq != tmr->ppq)) {
  170. /* refuse to change ppq on running timers */
  171. /* because it will upset the song position (ticks) */
  172. spin_unlock_irqrestore(&tmr->lock, flags);
  173. snd_printd("seq: cannot change ppq of a running timer\n");
  174. return -EBUSY;
  175. }
  176. tmr->ppq = ppq;
  177. snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
  178. spin_unlock_irqrestore(&tmr->lock, flags);
  179. return 0;
  180. }
  181. /* set current tick position */
  182. int snd_seq_timer_set_position_tick(seq_timer_t *tmr, snd_seq_tick_time_t position)
  183. {
  184. unsigned long flags;
  185. snd_assert(tmr, return -EINVAL);
  186. spin_lock_irqsave(&tmr->lock, flags);
  187. tmr->tick.cur_tick = position;
  188. tmr->tick.fraction = 0;
  189. spin_unlock_irqrestore(&tmr->lock, flags);
  190. return 0;
  191. }
  192. /* set current real-time position */
  193. int snd_seq_timer_set_position_time(seq_timer_t *tmr, snd_seq_real_time_t position)
  194. {
  195. unsigned long flags;
  196. snd_assert(tmr, return -EINVAL);
  197. snd_seq_sanity_real_time(&position);
  198. spin_lock_irqsave(&tmr->lock, flags);
  199. tmr->cur_time = position;
  200. spin_unlock_irqrestore(&tmr->lock, flags);
  201. return 0;
  202. }
  203. /* set timer skew */
  204. int snd_seq_timer_set_skew(seq_timer_t *tmr, unsigned int skew, unsigned int base)
  205. {
  206. unsigned long flags;
  207. snd_assert(tmr, return -EINVAL);
  208. /* FIXME */
  209. if (base != SKEW_BASE) {
  210. snd_printd("invalid skew base 0x%x\n", base);
  211. return -EINVAL;
  212. }
  213. spin_lock_irqsave(&tmr->lock, flags);
  214. tmr->skew = skew;
  215. spin_unlock_irqrestore(&tmr->lock, flags);
  216. return 0;
  217. }
  218. int snd_seq_timer_open(queue_t *q)
  219. {
  220. snd_timer_instance_t *t;
  221. seq_timer_t *tmr;
  222. char str[32];
  223. int err;
  224. tmr = q->timer;
  225. snd_assert(tmr != NULL, return -EINVAL);
  226. if (tmr->timeri)
  227. return -EBUSY;
  228. sprintf(str, "sequencer queue %i", q->queue);
  229. if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
  230. return -EINVAL;
  231. if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
  232. tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
  233. err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue);
  234. if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
  235. if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
  236. tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
  237. snd_timer_id_t tid;
  238. memset(&tid, 0, sizeof(tid));
  239. tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
  240. tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
  241. tid.card = -1;
  242. tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
  243. err = snd_timer_open(&t, str, &tid, q->queue);
  244. }
  245. if (err < 0) {
  246. snd_printk(KERN_ERR "seq fatal error: cannot create timer (%i)\n", err);
  247. return err;
  248. }
  249. }
  250. t->callback = snd_seq_timer_interrupt;
  251. t->callback_data = q;
  252. t->flags |= SNDRV_TIMER_IFLG_AUTO;
  253. tmr->timeri = t;
  254. return 0;
  255. }
  256. int snd_seq_timer_close(queue_t *q)
  257. {
  258. seq_timer_t *tmr;
  259. tmr = q->timer;
  260. snd_assert(tmr != NULL, return -EINVAL);
  261. if (tmr->timeri) {
  262. snd_timer_stop(tmr->timeri);
  263. snd_timer_close(tmr->timeri);
  264. tmr->timeri = NULL;
  265. }
  266. return 0;
  267. }
  268. int snd_seq_timer_stop(seq_timer_t * tmr)
  269. {
  270. if (! tmr->timeri)
  271. return -EINVAL;
  272. if (!tmr->running)
  273. return 0;
  274. tmr->running = 0;
  275. snd_timer_pause(tmr->timeri);
  276. return 0;
  277. }
  278. static int initialize_timer(seq_timer_t *tmr)
  279. {
  280. snd_timer_t *t;
  281. unsigned long freq;
  282. t = tmr->timeri->timer;
  283. snd_assert(t, return -EINVAL);
  284. freq = tmr->preferred_resolution;
  285. if (!freq)
  286. freq = DEFAULT_FREQUENCY;
  287. else if (freq < MIN_FREQUENCY)
  288. freq = MIN_FREQUENCY;
  289. else if (freq > MAX_FREQUENCY)
  290. freq = MAX_FREQUENCY;
  291. tmr->ticks = 1;
  292. if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
  293. unsigned long r = t->hw.resolution;
  294. if (! r && t->hw.c_resolution)
  295. r = t->hw.c_resolution(t);
  296. if (r) {
  297. tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
  298. if (! tmr->ticks)
  299. tmr->ticks = 1;
  300. }
  301. }
  302. tmr->initialized = 1;
  303. return 0;
  304. }
  305. int snd_seq_timer_start(seq_timer_t * tmr)
  306. {
  307. if (! tmr->timeri)
  308. return -EINVAL;
  309. if (tmr->running)
  310. snd_seq_timer_stop(tmr);
  311. snd_seq_timer_reset(tmr);
  312. if (initialize_timer(tmr) < 0)
  313. return -EINVAL;
  314. snd_timer_start(tmr->timeri, tmr->ticks);
  315. tmr->running = 1;
  316. do_gettimeofday(&tmr->last_update);
  317. return 0;
  318. }
  319. int snd_seq_timer_continue(seq_timer_t * tmr)
  320. {
  321. if (! tmr->timeri)
  322. return -EINVAL;
  323. if (tmr->running)
  324. return -EBUSY;
  325. if (! tmr->initialized) {
  326. snd_seq_timer_reset(tmr);
  327. if (initialize_timer(tmr) < 0)
  328. return -EINVAL;
  329. }
  330. snd_timer_start(tmr->timeri, tmr->ticks);
  331. tmr->running = 1;
  332. do_gettimeofday(&tmr->last_update);
  333. return 0;
  334. }
  335. /* return current 'real' time. use timeofday() to get better granularity. */
  336. snd_seq_real_time_t snd_seq_timer_get_cur_time(seq_timer_t *tmr)
  337. {
  338. snd_seq_real_time_t cur_time;
  339. cur_time = tmr->cur_time;
  340. if (tmr->running) {
  341. struct timeval tm;
  342. int usec;
  343. do_gettimeofday(&tm);
  344. usec = (int)(tm.tv_usec - tmr->last_update.tv_usec);
  345. if (usec < 0) {
  346. cur_time.tv_nsec += (1000000 + usec) * 1000;
  347. cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec - 1;
  348. } else {
  349. cur_time.tv_nsec += usec * 1000;
  350. cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec;
  351. }
  352. snd_seq_sanity_real_time(&cur_time);
  353. }
  354. return cur_time;
  355. }
  356. /* TODO: use interpolation on tick queue (will only be useful for very
  357. high PPQ values) */
  358. snd_seq_tick_time_t snd_seq_timer_get_cur_tick(seq_timer_t *tmr)
  359. {
  360. return tmr->tick.cur_tick;
  361. }
  362. /* exported to seq_info.c */
  363. void snd_seq_info_timer_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
  364. {
  365. int idx;
  366. queue_t *q;
  367. seq_timer_t *tmr;
  368. snd_timer_instance_t *ti;
  369. unsigned long resolution;
  370. for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
  371. q = queueptr(idx);
  372. if (q == NULL)
  373. continue;
  374. if ((tmr = q->timer) == NULL ||
  375. (ti = tmr->timeri) == NULL) {
  376. queuefree(q);
  377. continue;
  378. }
  379. snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
  380. resolution = snd_timer_resolution(ti) * tmr->ticks;
  381. snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
  382. snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
  383. queuefree(q);
  384. }
  385. }