seq_timer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ALSA sequencer Timer
  4. * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
  5. * Jaroslav Kysela <perex@perex.cz>
  6. */
  7. #include <sound/core.h>
  8. #include <linux/slab.h>
  9. #include "seq_timer.h"
  10. #include "seq_queue.h"
  11. #include "seq_info.h"
  12. /* allowed sequencer timer frequencies, in Hz */
  13. #define MIN_FREQUENCY 10
  14. #define MAX_FREQUENCY 6250
  15. #define DEFAULT_FREQUENCY 1000
  16. #define SKEW_BASE 0x10000 /* 16bit shift */
  17. static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer *tmr)
  18. {
  19. unsigned int threshold =
  20. tmr->tempo_base == 1000 ? 1000000 : 10000;
  21. if (tmr->tempo < threshold)
  22. tmr->tick.resolution = (tmr->tempo * tmr->tempo_base) / tmr->ppq;
  23. else {
  24. /* might overflow.. */
  25. unsigned int s;
  26. s = tmr->tempo % tmr->ppq;
  27. s = (s * tmr->tempo_base) / tmr->ppq;
  28. tmr->tick.resolution = (tmr->tempo / tmr->ppq) * tmr->tempo_base;
  29. tmr->tick.resolution += s;
  30. }
  31. if (tmr->tick.resolution <= 0)
  32. tmr->tick.resolution = 1;
  33. snd_seq_timer_update_tick(&tmr->tick, 0);
  34. }
  35. /* create new timer (constructor) */
  36. struct snd_seq_timer *snd_seq_timer_new(void)
  37. {
  38. struct snd_seq_timer *tmr;
  39. tmr = kzalloc_obj(*tmr);
  40. if (!tmr)
  41. return NULL;
  42. spin_lock_init(&tmr->lock);
  43. /* reset setup to defaults */
  44. snd_seq_timer_defaults(tmr);
  45. /* reset time */
  46. snd_seq_timer_reset(tmr);
  47. return tmr;
  48. }
  49. /* delete timer (destructor) */
  50. void snd_seq_timer_delete(struct snd_seq_timer **tmr)
  51. {
  52. struct snd_seq_timer *t = *tmr;
  53. *tmr = NULL;
  54. if (t == NULL) {
  55. pr_debug("ALSA: seq: snd_seq_timer_delete() called with NULL timer\n");
  56. return;
  57. }
  58. t->running = 0;
  59. /* reset time */
  60. snd_seq_timer_stop(t);
  61. snd_seq_timer_reset(t);
  62. kfree(t);
  63. }
  64. void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
  65. {
  66. guard(spinlock_irqsave)(&tmr->lock);
  67. /* setup defaults */
  68. tmr->ppq = 96; /* 96 PPQ */
  69. tmr->tempo = 500000; /* 120 BPM */
  70. tmr->tempo_base = 1000; /* 1us */
  71. snd_seq_timer_set_tick_resolution(tmr);
  72. tmr->running = 0;
  73. tmr->type = SNDRV_SEQ_TIMER_ALSA;
  74. tmr->alsa_id.dev_class = seq_default_timer_class;
  75. tmr->alsa_id.dev_sclass = seq_default_timer_sclass;
  76. tmr->alsa_id.card = seq_default_timer_card;
  77. tmr->alsa_id.device = seq_default_timer_device;
  78. tmr->alsa_id.subdevice = seq_default_timer_subdevice;
  79. tmr->preferred_resolution = seq_default_timer_resolution;
  80. tmr->skew = tmr->skew_base = SKEW_BASE;
  81. }
  82. static void seq_timer_reset(struct snd_seq_timer *tmr)
  83. {
  84. /* reset time & songposition */
  85. tmr->cur_time.tv_sec = 0;
  86. tmr->cur_time.tv_nsec = 0;
  87. tmr->tick.cur_tick = 0;
  88. tmr->tick.fraction = 0;
  89. }
  90. void snd_seq_timer_reset(struct snd_seq_timer *tmr)
  91. {
  92. guard(spinlock_irqsave)(&tmr->lock);
  93. seq_timer_reset(tmr);
  94. }
  95. /* called by timer interrupt routine. the period time since previous invocation is passed */
  96. static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
  97. unsigned long resolution,
  98. unsigned long ticks)
  99. {
  100. struct snd_seq_queue *q = timeri->callback_data;
  101. struct snd_seq_timer *tmr;
  102. if (q == NULL)
  103. return;
  104. tmr = q->timer;
  105. if (tmr == NULL)
  106. return;
  107. scoped_guard(spinlock_irqsave, &tmr->lock) {
  108. if (!tmr->running)
  109. return;
  110. resolution *= ticks;
  111. if (tmr->skew != tmr->skew_base) {
  112. /* FIXME: assuming skew_base = 0x10000 */
  113. resolution = (resolution >> 16) * tmr->skew +
  114. (((resolution & 0xffff) * tmr->skew) >> 16);
  115. }
  116. /* update timer */
  117. snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
  118. /* calculate current tick */
  119. snd_seq_timer_update_tick(&tmr->tick, resolution);
  120. /* register actual time of this timer update */
  121. ktime_get_ts64(&tmr->last_update);
  122. }
  123. /* check queues and dispatch events */
  124. snd_seq_check_queue(q, 1, 0);
  125. }
  126. /* set current tempo */
  127. int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
  128. {
  129. if (snd_BUG_ON(!tmr))
  130. return -EINVAL;
  131. if (tempo <= 0)
  132. return -EINVAL;
  133. guard(spinlock_irqsave)(&tmr->lock);
  134. if ((unsigned int)tempo != tmr->tempo) {
  135. tmr->tempo = tempo;
  136. snd_seq_timer_set_tick_resolution(tmr);
  137. }
  138. return 0;
  139. }
  140. /* set current tempo, ppq and base in a shot */
  141. int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq,
  142. unsigned int tempo_base)
  143. {
  144. int changed;
  145. if (snd_BUG_ON(!tmr))
  146. return -EINVAL;
  147. if (tempo <= 0 || ppq <= 0)
  148. return -EINVAL;
  149. /* allow only 10ns or 1us tempo base for now */
  150. if (tempo_base && tempo_base != 10 && tempo_base != 1000)
  151. return -EINVAL;
  152. guard(spinlock_irqsave)(&tmr->lock);
  153. if (tmr->running && (ppq != tmr->ppq)) {
  154. /* refuse to change ppq on running timers */
  155. /* because it will upset the song position (ticks) */
  156. pr_debug("ALSA: seq: cannot change ppq of a running timer\n");
  157. return -EBUSY;
  158. }
  159. changed = (tempo != tmr->tempo) || (ppq != tmr->ppq);
  160. tmr->tempo = tempo;
  161. tmr->ppq = ppq;
  162. tmr->tempo_base = tempo_base ? tempo_base : 1000;
  163. if (changed)
  164. snd_seq_timer_set_tick_resolution(tmr);
  165. return 0;
  166. }
  167. /* set current tick position */
  168. int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
  169. snd_seq_tick_time_t position)
  170. {
  171. if (snd_BUG_ON(!tmr))
  172. return -EINVAL;
  173. guard(spinlock_irqsave)(&tmr->lock);
  174. tmr->tick.cur_tick = position;
  175. tmr->tick.fraction = 0;
  176. return 0;
  177. }
  178. /* set current real-time position */
  179. int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
  180. snd_seq_real_time_t position)
  181. {
  182. if (snd_BUG_ON(!tmr))
  183. return -EINVAL;
  184. snd_seq_sanity_real_time(&position);
  185. guard(spinlock_irqsave)(&tmr->lock);
  186. tmr->cur_time = position;
  187. return 0;
  188. }
  189. /* set timer skew */
  190. int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
  191. unsigned int base)
  192. {
  193. if (snd_BUG_ON(!tmr))
  194. return -EINVAL;
  195. /* FIXME */
  196. if (base != SKEW_BASE) {
  197. pr_debug("ALSA: seq: invalid skew base 0x%x\n", base);
  198. return -EINVAL;
  199. }
  200. guard(spinlock_irqsave)(&tmr->lock);
  201. tmr->skew = skew;
  202. return 0;
  203. }
  204. int snd_seq_timer_open(struct snd_seq_queue *q)
  205. {
  206. struct snd_timer_instance *t;
  207. struct snd_seq_timer *tmr;
  208. char str[32];
  209. int err;
  210. tmr = q->timer;
  211. if (snd_BUG_ON(!tmr))
  212. return -EINVAL;
  213. if (tmr->timeri)
  214. return -EBUSY;
  215. sprintf(str, "sequencer queue %i", q->queue);
  216. if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
  217. return -EINVAL;
  218. if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
  219. tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
  220. t = snd_timer_instance_new(str);
  221. if (!t)
  222. return -ENOMEM;
  223. t->callback = snd_seq_timer_interrupt;
  224. t->callback_data = q;
  225. t->flags |= SNDRV_TIMER_IFLG_AUTO;
  226. err = snd_timer_open(t, &tmr->alsa_id, q->queue);
  227. if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
  228. if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
  229. tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
  230. struct snd_timer_id tid;
  231. memset(&tid, 0, sizeof(tid));
  232. tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
  233. tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
  234. tid.card = -1;
  235. tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
  236. err = snd_timer_open(t, &tid, q->queue);
  237. }
  238. }
  239. if (err < 0) {
  240. pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err);
  241. snd_timer_instance_free(t);
  242. return err;
  243. }
  244. scoped_guard(spinlock_irq, &tmr->lock) {
  245. if (tmr->timeri)
  246. err = -EBUSY;
  247. else
  248. tmr->timeri = t;
  249. }
  250. if (err < 0) {
  251. snd_timer_close(t);
  252. snd_timer_instance_free(t);
  253. return err;
  254. }
  255. return 0;
  256. }
  257. int snd_seq_timer_close(struct snd_seq_queue *q)
  258. {
  259. struct snd_seq_timer *tmr;
  260. struct snd_timer_instance *t;
  261. tmr = q->timer;
  262. if (snd_BUG_ON(!tmr))
  263. return -EINVAL;
  264. scoped_guard(spinlock_irq, &tmr->lock) {
  265. t = tmr->timeri;
  266. tmr->timeri = NULL;
  267. }
  268. if (t) {
  269. snd_timer_close(t);
  270. snd_timer_instance_free(t);
  271. }
  272. return 0;
  273. }
  274. static int seq_timer_stop(struct snd_seq_timer *tmr)
  275. {
  276. if (! tmr->timeri)
  277. return -EINVAL;
  278. if (!tmr->running)
  279. return 0;
  280. tmr->running = 0;
  281. snd_timer_pause(tmr->timeri);
  282. return 0;
  283. }
  284. int snd_seq_timer_stop(struct snd_seq_timer *tmr)
  285. {
  286. guard(spinlock_irqsave)(&tmr->lock);
  287. return seq_timer_stop(tmr);
  288. }
  289. static int initialize_timer(struct snd_seq_timer *tmr)
  290. {
  291. struct snd_timer *t;
  292. unsigned long freq;
  293. t = tmr->timeri->timer;
  294. if (!t)
  295. return -EINVAL;
  296. freq = tmr->preferred_resolution;
  297. if (!freq)
  298. freq = DEFAULT_FREQUENCY;
  299. else if (freq < MIN_FREQUENCY)
  300. freq = MIN_FREQUENCY;
  301. else if (freq > MAX_FREQUENCY)
  302. freq = MAX_FREQUENCY;
  303. tmr->ticks = 1;
  304. if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
  305. unsigned long r = snd_timer_resolution(tmr->timeri);
  306. if (r) {
  307. tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
  308. if (! tmr->ticks)
  309. tmr->ticks = 1;
  310. }
  311. }
  312. tmr->initialized = 1;
  313. return 0;
  314. }
  315. static int seq_timer_start(struct snd_seq_timer *tmr)
  316. {
  317. if (! tmr->timeri)
  318. return -EINVAL;
  319. if (tmr->running)
  320. seq_timer_stop(tmr);
  321. seq_timer_reset(tmr);
  322. if (initialize_timer(tmr) < 0)
  323. return -EINVAL;
  324. snd_timer_start(tmr->timeri, tmr->ticks);
  325. tmr->running = 1;
  326. ktime_get_ts64(&tmr->last_update);
  327. return 0;
  328. }
  329. int snd_seq_timer_start(struct snd_seq_timer *tmr)
  330. {
  331. guard(spinlock_irqsave)(&tmr->lock);
  332. return seq_timer_start(tmr);
  333. }
  334. static int seq_timer_continue(struct snd_seq_timer *tmr)
  335. {
  336. if (! tmr->timeri)
  337. return -EINVAL;
  338. if (tmr->running)
  339. return -EBUSY;
  340. if (! tmr->initialized) {
  341. seq_timer_reset(tmr);
  342. if (initialize_timer(tmr) < 0)
  343. return -EINVAL;
  344. }
  345. snd_timer_start(tmr->timeri, tmr->ticks);
  346. tmr->running = 1;
  347. ktime_get_ts64(&tmr->last_update);
  348. return 0;
  349. }
  350. int snd_seq_timer_continue(struct snd_seq_timer *tmr)
  351. {
  352. guard(spinlock_irqsave)(&tmr->lock);
  353. return seq_timer_continue(tmr);
  354. }
  355. /* return current 'real' time. use timeofday() to get better granularity. */
  356. snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr,
  357. bool adjust_ktime)
  358. {
  359. snd_seq_real_time_t cur_time;
  360. guard(spinlock_irqsave)(&tmr->lock);
  361. cur_time = tmr->cur_time;
  362. if (adjust_ktime && tmr->running) {
  363. struct timespec64 tm;
  364. ktime_get_ts64(&tm);
  365. tm = timespec64_sub(tm, tmr->last_update);
  366. cur_time.tv_nsec += tm.tv_nsec;
  367. cur_time.tv_sec += tm.tv_sec;
  368. snd_seq_sanity_real_time(&cur_time);
  369. }
  370. return cur_time;
  371. }
  372. /* TODO: use interpolation on tick queue (will only be useful for very
  373. high PPQ values) */
  374. snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
  375. {
  376. guard(spinlock_irqsave)(&tmr->lock);
  377. return tmr->tick.cur_tick;
  378. }
  379. #ifdef CONFIG_SND_PROC_FS
  380. /* exported to seq_info.c */
  381. void snd_seq_info_timer_read(struct snd_info_entry *entry,
  382. struct snd_info_buffer *buffer)
  383. {
  384. int idx;
  385. struct snd_seq_timer *tmr;
  386. struct snd_timer_instance *ti;
  387. unsigned long resolution;
  388. for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
  389. struct snd_seq_queue *q __free(snd_seq_queue) = queueptr(idx);
  390. if (q == NULL)
  391. continue;
  392. scoped_guard(mutex, &q->timer_mutex) {
  393. tmr = q->timer;
  394. if (!tmr)
  395. break;
  396. ti = tmr->timeri;
  397. if (!ti)
  398. break;
  399. snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
  400. resolution = snd_timer_resolution(ti) * tmr->ticks;
  401. snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
  402. snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
  403. }
  404. }
  405. }
  406. #endif /* CONFIG_SND_PROC_FS */