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