seq_timer.c 11 KB

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