seq_timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. unsigned long flags;
  80. spin_lock_irqsave(&tmr->lock, flags);
  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. spin_unlock_irqrestore(&tmr->lock, flags);
  95. }
  96. static void seq_timer_reset(struct snd_seq_timer *tmr)
  97. {
  98. /* reset time & songposition */
  99. tmr->cur_time.tv_sec = 0;
  100. tmr->cur_time.tv_nsec = 0;
  101. tmr->tick.cur_tick = 0;
  102. tmr->tick.fraction = 0;
  103. }
  104. void snd_seq_timer_reset(struct snd_seq_timer *tmr)
  105. {
  106. unsigned long flags;
  107. spin_lock_irqsave(&tmr->lock, flags);
  108. seq_timer_reset(tmr);
  109. spin_unlock_irqrestore(&tmr->lock, flags);
  110. }
  111. /* called by timer interrupt routine. the period time since previous invocation is passed */
  112. static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
  113. unsigned long resolution,
  114. unsigned long ticks)
  115. {
  116. unsigned long flags;
  117. struct snd_seq_queue *q = timeri->callback_data;
  118. struct snd_seq_timer *tmr;
  119. if (q == NULL)
  120. return;
  121. tmr = q->timer;
  122. if (tmr == NULL)
  123. return;
  124. spin_lock_irqsave(&tmr->lock, flags);
  125. if (!tmr->running) {
  126. spin_unlock_irqrestore(&tmr->lock, flags);
  127. return;
  128. }
  129. resolution *= ticks;
  130. if (tmr->skew != tmr->skew_base) {
  131. /* FIXME: assuming skew_base = 0x10000 */
  132. resolution = (resolution >> 16) * tmr->skew +
  133. (((resolution & 0xffff) * tmr->skew) >> 16);
  134. }
  135. /* update timer */
  136. snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
  137. /* calculate current tick */
  138. snd_seq_timer_update_tick(&tmr->tick, resolution);
  139. /* register actual time of this timer update */
  140. do_gettimeofday(&tmr->last_update);
  141. spin_unlock_irqrestore(&tmr->lock, flags);
  142. /* check queues and dispatch events */
  143. snd_seq_check_queue(q, 1, 0);
  144. }
  145. /* set current tempo */
  146. int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
  147. {
  148. unsigned long flags;
  149. if (snd_BUG_ON(!tmr))
  150. return -EINVAL;
  151. if (tempo <= 0)
  152. return -EINVAL;
  153. spin_lock_irqsave(&tmr->lock, flags);
  154. if ((unsigned int)tempo != tmr->tempo) {
  155. tmr->tempo = tempo;
  156. snd_seq_timer_set_tick_resolution(tmr);
  157. }
  158. spin_unlock_irqrestore(&tmr->lock, flags);
  159. return 0;
  160. }
  161. /* set current ppq */
  162. int snd_seq_timer_set_ppq(struct snd_seq_timer * tmr, int ppq)
  163. {
  164. unsigned long flags;
  165. if (snd_BUG_ON(!tmr))
  166. return -EINVAL;
  167. if (ppq <= 0)
  168. return -EINVAL;
  169. spin_lock_irqsave(&tmr->lock, flags);
  170. if (tmr->running && (ppq != tmr->ppq)) {
  171. /* refuse to change ppq on running timers */
  172. /* because it will upset the song position (ticks) */
  173. spin_unlock_irqrestore(&tmr->lock, flags);
  174. pr_debug("ALSA: seq: cannot change ppq of a running timer\n");
  175. return -EBUSY;
  176. }
  177. tmr->ppq = ppq;
  178. snd_seq_timer_set_tick_resolution(tmr);
  179. spin_unlock_irqrestore(&tmr->lock, flags);
  180. return 0;
  181. }
  182. /* set current tick position */
  183. int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
  184. snd_seq_tick_time_t position)
  185. {
  186. unsigned long flags;
  187. if (snd_BUG_ON(!tmr))
  188. return -EINVAL;
  189. spin_lock_irqsave(&tmr->lock, flags);
  190. tmr->tick.cur_tick = position;
  191. tmr->tick.fraction = 0;
  192. spin_unlock_irqrestore(&tmr->lock, flags);
  193. return 0;
  194. }
  195. /* set current real-time position */
  196. int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
  197. snd_seq_real_time_t position)
  198. {
  199. unsigned long flags;
  200. if (snd_BUG_ON(!tmr))
  201. return -EINVAL;
  202. snd_seq_sanity_real_time(&position);
  203. spin_lock_irqsave(&tmr->lock, flags);
  204. tmr->cur_time = position;
  205. spin_unlock_irqrestore(&tmr->lock, flags);
  206. return 0;
  207. }
  208. /* set timer skew */
  209. int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
  210. unsigned int base)
  211. {
  212. unsigned long flags;
  213. if (snd_BUG_ON(!tmr))
  214. return -EINVAL;
  215. /* FIXME */
  216. if (base != SKEW_BASE) {
  217. pr_debug("ALSA: seq: invalid skew base 0x%x\n", base);
  218. return -EINVAL;
  219. }
  220. spin_lock_irqsave(&tmr->lock, flags);
  221. tmr->skew = skew;
  222. spin_unlock_irqrestore(&tmr->lock, flags);
  223. return 0;
  224. }
  225. int snd_seq_timer_open(struct snd_seq_queue *q)
  226. {
  227. struct snd_timer_instance *t;
  228. struct snd_seq_timer *tmr;
  229. char str[32];
  230. int err;
  231. tmr = q->timer;
  232. if (snd_BUG_ON(!tmr))
  233. return -EINVAL;
  234. if (tmr->timeri)
  235. return -EBUSY;
  236. sprintf(str, "sequencer queue %i", q->queue);
  237. if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
  238. return -EINVAL;
  239. if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
  240. tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
  241. err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue);
  242. if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
  243. if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
  244. tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
  245. struct snd_timer_id tid;
  246. memset(&tid, 0, sizeof(tid));
  247. tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
  248. tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
  249. tid.card = -1;
  250. tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
  251. err = snd_timer_open(&t, str, &tid, q->queue);
  252. }
  253. }
  254. if (err < 0) {
  255. pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err);
  256. return err;
  257. }
  258. t->callback = snd_seq_timer_interrupt;
  259. t->callback_data = q;
  260. t->flags |= SNDRV_TIMER_IFLG_AUTO;
  261. spin_lock_irq(&tmr->lock);
  262. tmr->timeri = t;
  263. spin_unlock_irq(&tmr->lock);
  264. return 0;
  265. }
  266. int snd_seq_timer_close(struct snd_seq_queue *q)
  267. {
  268. struct snd_seq_timer *tmr;
  269. struct snd_timer_instance *t;
  270. tmr = q->timer;
  271. if (snd_BUG_ON(!tmr))
  272. return -EINVAL;
  273. spin_lock_irq(&tmr->lock);
  274. t = tmr->timeri;
  275. tmr->timeri = NULL;
  276. spin_unlock_irq(&tmr->lock);
  277. if (t)
  278. snd_timer_close(t);
  279. return 0;
  280. }
  281. static int seq_timer_stop(struct snd_seq_timer *tmr)
  282. {
  283. if (! tmr->timeri)
  284. return -EINVAL;
  285. if (!tmr->running)
  286. return 0;
  287. tmr->running = 0;
  288. snd_timer_pause(tmr->timeri);
  289. return 0;
  290. }
  291. int snd_seq_timer_stop(struct snd_seq_timer *tmr)
  292. {
  293. unsigned long flags;
  294. int err;
  295. spin_lock_irqsave(&tmr->lock, flags);
  296. err = seq_timer_stop(tmr);
  297. spin_unlock_irqrestore(&tmr->lock, flags);
  298. return err;
  299. }
  300. static int initialize_timer(struct snd_seq_timer *tmr)
  301. {
  302. struct snd_timer *t;
  303. unsigned long freq;
  304. t = tmr->timeri->timer;
  305. if (!t)
  306. return -EINVAL;
  307. freq = tmr->preferred_resolution;
  308. if (!freq)
  309. freq = DEFAULT_FREQUENCY;
  310. else if (freq < MIN_FREQUENCY)
  311. freq = MIN_FREQUENCY;
  312. else if (freq > MAX_FREQUENCY)
  313. freq = MAX_FREQUENCY;
  314. tmr->ticks = 1;
  315. if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
  316. unsigned long r = t->hw.resolution;
  317. if (! r && t->hw.c_resolution)
  318. r = t->hw.c_resolution(t);
  319. if (r) {
  320. tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
  321. if (! tmr->ticks)
  322. tmr->ticks = 1;
  323. }
  324. }
  325. tmr->initialized = 1;
  326. return 0;
  327. }
  328. static int seq_timer_start(struct snd_seq_timer *tmr)
  329. {
  330. if (! tmr->timeri)
  331. return -EINVAL;
  332. if (tmr->running)
  333. seq_timer_stop(tmr);
  334. seq_timer_reset(tmr);
  335. if (initialize_timer(tmr) < 0)
  336. return -EINVAL;
  337. snd_timer_start(tmr->timeri, tmr->ticks);
  338. tmr->running = 1;
  339. do_gettimeofday(&tmr->last_update);
  340. return 0;
  341. }
  342. int snd_seq_timer_start(struct snd_seq_timer *tmr)
  343. {
  344. unsigned long flags;
  345. int err;
  346. spin_lock_irqsave(&tmr->lock, flags);
  347. err = seq_timer_start(tmr);
  348. spin_unlock_irqrestore(&tmr->lock, flags);
  349. return err;
  350. }
  351. static int seq_timer_continue(struct snd_seq_timer *tmr)
  352. {
  353. if (! tmr->timeri)
  354. return -EINVAL;
  355. if (tmr->running)
  356. return -EBUSY;
  357. if (! tmr->initialized) {
  358. seq_timer_reset(tmr);
  359. if (initialize_timer(tmr) < 0)
  360. return -EINVAL;
  361. }
  362. snd_timer_start(tmr->timeri, tmr->ticks);
  363. tmr->running = 1;
  364. do_gettimeofday(&tmr->last_update);
  365. return 0;
  366. }
  367. int snd_seq_timer_continue(struct snd_seq_timer *tmr)
  368. {
  369. unsigned long flags;
  370. int err;
  371. spin_lock_irqsave(&tmr->lock, flags);
  372. err = seq_timer_continue(tmr);
  373. spin_unlock_irqrestore(&tmr->lock, flags);
  374. return err;
  375. }
  376. /* return current 'real' time. use timeofday() to get better granularity. */
  377. snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr)
  378. {
  379. snd_seq_real_time_t cur_time;
  380. unsigned long flags;
  381. spin_lock_irqsave(&tmr->lock, flags);
  382. cur_time = tmr->cur_time;
  383. if (tmr->running) {
  384. struct timeval tm;
  385. int usec;
  386. do_gettimeofday(&tm);
  387. usec = (int)(tm.tv_usec - tmr->last_update.tv_usec);
  388. if (usec < 0) {
  389. cur_time.tv_nsec += (1000000 + usec) * 1000;
  390. cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec - 1;
  391. } else {
  392. cur_time.tv_nsec += usec * 1000;
  393. cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec;
  394. }
  395. snd_seq_sanity_real_time(&cur_time);
  396. }
  397. spin_unlock_irqrestore(&tmr->lock, flags);
  398. return cur_time;
  399. }
  400. /* TODO: use interpolation on tick queue (will only be useful for very
  401. high PPQ values) */
  402. snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
  403. {
  404. return tmr->tick.cur_tick;
  405. }
  406. #ifdef CONFIG_SND_PROC_FS
  407. /* exported to seq_info.c */
  408. void snd_seq_info_timer_read(struct snd_info_entry *entry,
  409. struct snd_info_buffer *buffer)
  410. {
  411. int idx;
  412. struct snd_seq_queue *q;
  413. struct snd_seq_timer *tmr;
  414. struct snd_timer_instance *ti;
  415. unsigned long resolution;
  416. for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
  417. q = queueptr(idx);
  418. if (q == NULL)
  419. continue;
  420. if ((tmr = q->timer) == NULL ||
  421. (ti = tmr->timeri) == NULL) {
  422. queuefree(q);
  423. continue;
  424. }
  425. snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
  426. resolution = snd_timer_resolution(ti) * tmr->ticks;
  427. snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
  428. snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
  429. queuefree(q);
  430. }
  431. }
  432. #endif /* CONFIG_SND_PROC_FS */