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@suse.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. extern int seq_default_timer_class;
  29. extern int seq_default_timer_sclass;
  30. extern int seq_default_timer_card;
  31. extern int seq_default_timer_device;
  32. extern int seq_default_timer_subdevice;
  33. extern int seq_default_timer_resolution;
  34. /* allowed sequencer timer frequencies, in Hz */
  35. #define MIN_FREQUENCY 10
  36. #define MAX_FREQUENCY 6250
  37. #define DEFAULT_FREQUENCY 1000
  38. #define SKEW_BASE 0x10000 /* 16bit shift */
  39. static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer_tick *tick,
  40. int tempo, int ppq)
  41. {
  42. if (tempo < 1000000)
  43. tick->resolution = (tempo * 1000) / ppq;
  44. else {
  45. /* might overflow.. */
  46. unsigned int s;
  47. s = tempo % ppq;
  48. s = (s * 1000) / ppq;
  49. tick->resolution = (tempo / ppq) * 1000;
  50. tick->resolution += s;
  51. }
  52. if (tick->resolution <= 0)
  53. tick->resolution = 1;
  54. snd_seq_timer_update_tick(tick, 0);
  55. }
  56. /* create new timer (constructor) */
  57. struct snd_seq_timer *snd_seq_timer_new(void)
  58. {
  59. struct snd_seq_timer *tmr;
  60. tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
  61. if (tmr == NULL) {
  62. snd_printd("malloc failed for snd_seq_timer_new() \n");
  63. return NULL;
  64. }
  65. spin_lock_init(&tmr->lock);
  66. /* reset setup to defaults */
  67. snd_seq_timer_defaults(tmr);
  68. /* reset time */
  69. snd_seq_timer_reset(tmr);
  70. return tmr;
  71. }
  72. /* delete timer (destructor) */
  73. void snd_seq_timer_delete(struct snd_seq_timer **tmr)
  74. {
  75. struct snd_seq_timer *t = *tmr;
  76. *tmr = NULL;
  77. if (t == NULL) {
  78. snd_printd("oops: snd_seq_timer_delete() called with NULL timer\n");
  79. return;
  80. }
  81. t->running = 0;
  82. /* reset time */
  83. snd_seq_timer_stop(t);
  84. snd_seq_timer_reset(t);
  85. kfree(t);
  86. }
  87. void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
  88. {
  89. /* setup defaults */
  90. tmr->ppq = 96; /* 96 PPQ */
  91. tmr->tempo = 500000; /* 120 BPM */
  92. snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
  93. tmr->running = 0;
  94. tmr->type = SNDRV_SEQ_TIMER_ALSA;
  95. tmr->alsa_id.dev_class = seq_default_timer_class;
  96. tmr->alsa_id.dev_sclass = seq_default_timer_sclass;
  97. tmr->alsa_id.card = seq_default_timer_card;
  98. tmr->alsa_id.device = seq_default_timer_device;
  99. tmr->alsa_id.subdevice = seq_default_timer_subdevice;
  100. tmr->preferred_resolution = seq_default_timer_resolution;
  101. tmr->skew = tmr->skew_base = SKEW_BASE;
  102. }
  103. void snd_seq_timer_reset(struct snd_seq_timer * tmr)
  104. {
  105. unsigned long flags;
  106. spin_lock_irqsave(&tmr->lock, flags);
  107. /* reset time & songposition */
  108. tmr->cur_time.tv_sec = 0;
  109. tmr->cur_time.tv_nsec = 0;
  110. tmr->tick.cur_tick = 0;
  111. tmr->tick.fraction = 0;
  112. spin_unlock_irqrestore(&tmr->lock, flags);
  113. }
  114. /* called by timer interrupt routine. the period time since previous invocation is passed */
  115. static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
  116. unsigned long resolution,
  117. unsigned long ticks)
  118. {
  119. unsigned long flags;
  120. struct snd_seq_queue *q = timeri->callback_data;
  121. struct snd_seq_timer *tmr;
  122. if (q == NULL)
  123. return;
  124. tmr = q->timer;
  125. if (tmr == NULL)
  126. return;
  127. if (!tmr->running)
  128. return;
  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. spin_lock_irqsave(&tmr->lock, flags);
  136. /* update timer */
  137. snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
  138. /* calculate current tick */
  139. snd_seq_timer_update_tick(&tmr->tick, resolution);
  140. /* register actual time of this timer update */
  141. do_gettimeofday(&tmr->last_update);
  142. spin_unlock_irqrestore(&tmr->lock, flags);
  143. /* check queues and dispatch events */
  144. snd_seq_check_queue(q, 1, 0);
  145. }
  146. /* set current tempo */
  147. int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
  148. {
  149. unsigned long flags;
  150. snd_assert(tmr, 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->tick, tmr->tempo, tmr->ppq);
  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. snd_assert(tmr, return -EINVAL);
  166. if (ppq <= 0)
  167. return -EINVAL;
  168. spin_lock_irqsave(&tmr->lock, flags);
  169. if (tmr->running && (ppq != tmr->ppq)) {
  170. /* refuse to change ppq on running timers */
  171. /* because it will upset the song position (ticks) */
  172. spin_unlock_irqrestore(&tmr->lock, flags);
  173. snd_printd("seq: cannot change ppq of a running timer\n");
  174. return -EBUSY;
  175. }
  176. tmr->ppq = ppq;
  177. snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq);
  178. spin_unlock_irqrestore(&tmr->lock, flags);
  179. return 0;
  180. }
  181. /* set current tick position */
  182. int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
  183. snd_seq_tick_time_t position)
  184. {
  185. unsigned long flags;
  186. snd_assert(tmr, return -EINVAL);
  187. spin_lock_irqsave(&tmr->lock, flags);
  188. tmr->tick.cur_tick = position;
  189. tmr->tick.fraction = 0;
  190. spin_unlock_irqrestore(&tmr->lock, flags);
  191. return 0;
  192. }
  193. /* set current real-time position */
  194. int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
  195. snd_seq_real_time_t position)
  196. {
  197. unsigned long flags;
  198. snd_assert(tmr, return -EINVAL);
  199. snd_seq_sanity_real_time(&position);
  200. spin_lock_irqsave(&tmr->lock, flags);
  201. tmr->cur_time = position;
  202. spin_unlock_irqrestore(&tmr->lock, flags);
  203. return 0;
  204. }
  205. /* set timer skew */
  206. int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
  207. unsigned int base)
  208. {
  209. unsigned long flags;
  210. snd_assert(tmr, return -EINVAL);
  211. /* FIXME */
  212. if (base != SKEW_BASE) {
  213. snd_printd("invalid skew base 0x%x\n", base);
  214. return -EINVAL;
  215. }
  216. spin_lock_irqsave(&tmr->lock, flags);
  217. tmr->skew = skew;
  218. spin_unlock_irqrestore(&tmr->lock, flags);
  219. return 0;
  220. }
  221. int snd_seq_timer_open(struct snd_seq_queue *q)
  222. {
  223. struct snd_timer_instance *t;
  224. struct snd_seq_timer *tmr;
  225. char str[32];
  226. int err;
  227. tmr = q->timer;
  228. snd_assert(tmr != NULL, return -EINVAL);
  229. if (tmr->timeri)
  230. return -EBUSY;
  231. sprintf(str, "sequencer queue %i", q->queue);
  232. if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
  233. return -EINVAL;
  234. if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
  235. tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
  236. err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue);
  237. if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
  238. if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
  239. tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
  240. struct snd_timer_id tid;
  241. memset(&tid, 0, sizeof(tid));
  242. tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
  243. tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
  244. tid.card = -1;
  245. tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
  246. err = snd_timer_open(&t, str, &tid, q->queue);
  247. }
  248. if (err < 0) {
  249. snd_printk(KERN_ERR "seq fatal error: cannot create timer (%i)\n", err);
  250. return err;
  251. }
  252. }
  253. t->callback = snd_seq_timer_interrupt;
  254. t->callback_data = q;
  255. t->flags |= SNDRV_TIMER_IFLG_AUTO;
  256. tmr->timeri = t;
  257. return 0;
  258. }
  259. int snd_seq_timer_close(struct snd_seq_queue *q)
  260. {
  261. struct snd_seq_timer *tmr;
  262. tmr = q->timer;
  263. snd_assert(tmr != NULL, 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. snd_assert(t, 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 */