seq_timer.c 12 KB

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