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_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. if (snd_BUG_ON(!tmr))
  144. 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. if (snd_BUG_ON(!tmr))
  160. return -EINVAL;
  161. if (ppq <= 0)
  162. return -EINVAL;
  163. spin_lock_irqsave(&tmr->lock, flags);
  164. if (tmr->running && (ppq != tmr->ppq)) {
  165. /* refuse to change ppq on running timers */
  166. /* because it will upset the song position (ticks) */
  167. spin_unlock_irqrestore(&tmr->lock, flags);
  168. snd_printd("seq: cannot change ppq of a running timer\n");
  169. return -EBUSY;
  170. }
  171. tmr->ppq = ppq;
  172. snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
  173. spin_unlock_irqrestore(&tmr->lock, flags);
  174. return 0;
  175. }
  176. /* set current tick position */
  177. int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
  178. snd_seq_tick_time_t position)
  179. {
  180. unsigned long flags;
  181. if (snd_BUG_ON(!tmr))
  182. return -EINVAL;
  183. spin_lock_irqsave(&tmr->lock, flags);
  184. tmr->tick.cur_tick = position;
  185. tmr->tick.fraction = 0;
  186. spin_unlock_irqrestore(&tmr->lock, flags);
  187. return 0;
  188. }
  189. /* set current real-time position */
  190. int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
  191. snd_seq_real_time_t position)
  192. {
  193. unsigned long flags;
  194. if (snd_BUG_ON(!tmr))
  195. return -EINVAL;
  196. snd_seq_sanity_real_time(&position);
  197. spin_lock_irqsave(&tmr->lock, flags);
  198. tmr->cur_time = position;
  199. spin_unlock_irqrestore(&tmr->lock, flags);
  200. return 0;
  201. }
  202. /* set timer skew */
  203. int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
  204. unsigned int base)
  205. {
  206. unsigned long flags;
  207. if (snd_BUG_ON(!tmr))
  208. return -EINVAL;
  209. /* FIXME */
  210. if (base != SKEW_BASE) {
  211. snd_printd("invalid skew base 0x%x\n", base);
  212. return -EINVAL;
  213. }
  214. spin_lock_irqsave(&tmr->lock, flags);
  215. tmr->skew = skew;
  216. spin_unlock_irqrestore(&tmr->lock, flags);
  217. return 0;
  218. }
  219. int snd_seq_timer_open(struct snd_seq_queue *q)
  220. {
  221. struct snd_timer_instance *t;
  222. struct snd_seq_timer *tmr;
  223. char str[32];
  224. int err;
  225. tmr = q->timer;
  226. if (snd_BUG_ON(!tmr))
  227. return -EINVAL;
  228. if (tmr->timeri)
  229. return -EBUSY;
  230. sprintf(str, "sequencer queue %i", q->queue);
  231. if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
  232. return -EINVAL;
  233. if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
  234. tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
  235. err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue);
  236. if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
  237. if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
  238. tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
  239. struct snd_timer_id tid;
  240. memset(&tid, 0, sizeof(tid));
  241. tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
  242. tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
  243. tid.card = -1;
  244. tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
  245. err = snd_timer_open(&t, str, &tid, q->queue);
  246. }
  247. if (err < 0) {
  248. snd_printk(KERN_ERR "seq fatal error: cannot create timer (%i)\n", err);
  249. return err;
  250. }
  251. }
  252. t->callback = snd_seq_timer_interrupt;
  253. t->callback_data = q;
  254. t->flags |= SNDRV_TIMER_IFLG_AUTO;
  255. tmr->timeri = t;
  256. return 0;
  257. }
  258. int snd_seq_timer_close(struct snd_seq_queue *q)
  259. {
  260. struct snd_seq_timer *tmr;
  261. tmr = q->timer;
  262. if (snd_BUG_ON(!tmr))
  263. return -EINVAL;
  264. if (tmr->timeri) {
  265. snd_timer_stop(tmr->timeri);
  266. snd_timer_close(tmr->timeri);
  267. tmr->timeri = NULL;
  268. }
  269. return 0;
  270. }
  271. int snd_seq_timer_stop(struct snd_seq_timer * tmr)
  272. {
  273. if (! tmr->timeri)
  274. return -EINVAL;
  275. if (!tmr->running)
  276. return 0;
  277. tmr->running = 0;
  278. snd_timer_pause(tmr->timeri);
  279. return 0;
  280. }
  281. static int initialize_timer(struct snd_seq_timer *tmr)
  282. {
  283. struct snd_timer *t;
  284. unsigned long freq;
  285. t = tmr->timeri->timer;
  286. if (snd_BUG_ON(!t))
  287. return -EINVAL;
  288. freq = tmr->preferred_resolution;
  289. if (!freq)
  290. freq = DEFAULT_FREQUENCY;
  291. else if (freq < MIN_FREQUENCY)
  292. freq = MIN_FREQUENCY;
  293. else if (freq > MAX_FREQUENCY)
  294. freq = MAX_FREQUENCY;
  295. tmr->ticks = 1;
  296. if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
  297. unsigned long r = t->hw.resolution;
  298. if (! r && t->hw.c_resolution)
  299. r = t->hw.c_resolution(t);
  300. if (r) {
  301. tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
  302. if (! tmr->ticks)
  303. tmr->ticks = 1;
  304. }
  305. }
  306. tmr->initialized = 1;
  307. return 0;
  308. }
  309. int snd_seq_timer_start(struct snd_seq_timer * tmr)
  310. {
  311. if (! tmr->timeri)
  312. return -EINVAL;
  313. if (tmr->running)
  314. snd_seq_timer_stop(tmr);
  315. snd_seq_timer_reset(tmr);
  316. if (initialize_timer(tmr) < 0)
  317. return -EINVAL;
  318. snd_timer_start(tmr->timeri, tmr->ticks);
  319. tmr->running = 1;
  320. do_gettimeofday(&tmr->last_update);
  321. return 0;
  322. }
  323. int snd_seq_timer_continue(struct snd_seq_timer * tmr)
  324. {
  325. if (! tmr->timeri)
  326. return -EINVAL;
  327. if (tmr->running)
  328. return -EBUSY;
  329. if (! tmr->initialized) {
  330. snd_seq_timer_reset(tmr);
  331. if (initialize_timer(tmr) < 0)
  332. return -EINVAL;
  333. }
  334. snd_timer_start(tmr->timeri, tmr->ticks);
  335. tmr->running = 1;
  336. do_gettimeofday(&tmr->last_update);
  337. return 0;
  338. }
  339. /* return current 'real' time. use timeofday() to get better granularity. */
  340. snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr)
  341. {
  342. snd_seq_real_time_t cur_time;
  343. cur_time = tmr->cur_time;
  344. if (tmr->running) {
  345. struct timeval tm;
  346. int usec;
  347. do_gettimeofday(&tm);
  348. usec = (int)(tm.tv_usec - tmr->last_update.tv_usec);
  349. if (usec < 0) {
  350. cur_time.tv_nsec += (1000000 + usec) * 1000;
  351. cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec - 1;
  352. } else {
  353. cur_time.tv_nsec += usec * 1000;
  354. cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec;
  355. }
  356. snd_seq_sanity_real_time(&cur_time);
  357. }
  358. return cur_time;
  359. }
  360. /* TODO: use interpolation on tick queue (will only be useful for very
  361. high PPQ values) */
  362. snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
  363. {
  364. return tmr->tick.cur_tick;
  365. }
  366. #ifdef CONFIG_PROC_FS
  367. /* exported to seq_info.c */
  368. void snd_seq_info_timer_read(struct snd_info_entry *entry,
  369. struct snd_info_buffer *buffer)
  370. {
  371. int idx;
  372. struct snd_seq_queue *q;
  373. struct snd_seq_timer *tmr;
  374. struct snd_timer_instance *ti;
  375. unsigned long resolution;
  376. for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
  377. q = queueptr(idx);
  378. if (q == NULL)
  379. continue;
  380. if ((tmr = q->timer) == NULL ||
  381. (ti = tmr->timeri) == NULL) {
  382. queuefree(q);
  383. continue;
  384. }
  385. snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
  386. resolution = snd_timer_resolution(ti) * tmr->ticks;
  387. snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
  388. snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
  389. queuefree(q);
  390. }
  391. }
  392. #endif /* CONFIG_PROC_FS */