seq_timer.c 11 KB

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