seq_timer.c 11 KB

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