seq_queue.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ALSA sequencer Timing queue handling
  4. * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
  5. *
  6. * MAJOR CHANGES
  7. * Nov. 13, 1999 Takashi Iwai <iwai@ww.uni-erlangen.de>
  8. * - Queues are allocated dynamically via ioctl.
  9. * - When owner client is deleted, all owned queues are deleted, too.
  10. * - Owner of unlocked queue is kept unmodified even if it is
  11. * manipulated by other clients.
  12. * - Owner field in SET_QUEUE_OWNER ioctl must be identical with the
  13. * caller client. i.e. Changing owner to a third client is not
  14. * allowed.
  15. *
  16. * Aug. 30, 2000 Takashi Iwai
  17. * - Queues are managed in static array again, but with better way.
  18. * The API itself is identical.
  19. * - The queue is locked when struct snd_seq_queue pointer is returned via
  20. * queueptr(). This pointer *MUST* be released afterward by
  21. * queuefree(ptr).
  22. * - Addition of experimental sync support.
  23. */
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <sound/core.h>
  27. #include "seq_memory.h"
  28. #include "seq_queue.h"
  29. #include "seq_clientmgr.h"
  30. #include "seq_fifo.h"
  31. #include "seq_timer.h"
  32. #include "seq_info.h"
  33. /* list of allocated queues */
  34. static struct snd_seq_queue *queue_list[SNDRV_SEQ_MAX_QUEUES];
  35. static DEFINE_SPINLOCK(queue_list_lock);
  36. /* number of queues allocated */
  37. static int num_queues;
  38. int snd_seq_queue_get_cur_queues(void)
  39. {
  40. return num_queues;
  41. }
  42. /*----------------------------------------------------------------*/
  43. /* assign queue id and insert to list */
  44. static int queue_list_add(struct snd_seq_queue *q)
  45. {
  46. int i;
  47. guard(spinlock_irqsave)(&queue_list_lock);
  48. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  49. if (! queue_list[i]) {
  50. queue_list[i] = q;
  51. q->queue = i;
  52. num_queues++;
  53. return i;
  54. }
  55. }
  56. return -1;
  57. }
  58. static struct snd_seq_queue *queue_list_remove(int id, int client)
  59. {
  60. struct snd_seq_queue *q;
  61. guard(spinlock_irqsave)(&queue_list_lock);
  62. q = queue_list[id];
  63. if (q) {
  64. guard(spinlock)(&q->owner_lock);
  65. if (q->owner == client) {
  66. /* found */
  67. q->klocked = 1;
  68. queue_list[id] = NULL;
  69. num_queues--;
  70. return q;
  71. }
  72. }
  73. return NULL;
  74. }
  75. /*----------------------------------------------------------------*/
  76. /* create new queue (constructor) */
  77. static struct snd_seq_queue *queue_new(int owner, int locked)
  78. {
  79. struct snd_seq_queue *q;
  80. q = kzalloc(sizeof(*q), GFP_KERNEL);
  81. if (!q)
  82. return NULL;
  83. spin_lock_init(&q->owner_lock);
  84. spin_lock_init(&q->check_lock);
  85. mutex_init(&q->timer_mutex);
  86. snd_use_lock_init(&q->use_lock);
  87. q->queue = -1;
  88. q->tickq = snd_seq_prioq_new();
  89. q->timeq = snd_seq_prioq_new();
  90. q->timer = snd_seq_timer_new();
  91. if (q->tickq == NULL || q->timeq == NULL || q->timer == NULL) {
  92. snd_seq_prioq_delete(&q->tickq);
  93. snd_seq_prioq_delete(&q->timeq);
  94. snd_seq_timer_delete(&q->timer);
  95. kfree(q);
  96. return NULL;
  97. }
  98. q->owner = owner;
  99. q->locked = locked;
  100. q->klocked = 0;
  101. return q;
  102. }
  103. /* delete queue (destructor) */
  104. static void queue_delete(struct snd_seq_queue *q)
  105. {
  106. /* stop and release the timer */
  107. mutex_lock(&q->timer_mutex);
  108. snd_seq_timer_stop(q->timer);
  109. snd_seq_timer_close(q);
  110. mutex_unlock(&q->timer_mutex);
  111. /* wait until access free */
  112. snd_use_lock_sync(&q->use_lock);
  113. /* release resources... */
  114. snd_seq_prioq_delete(&q->tickq);
  115. snd_seq_prioq_delete(&q->timeq);
  116. snd_seq_timer_delete(&q->timer);
  117. kfree(q);
  118. }
  119. /*----------------------------------------------------------------*/
  120. /* delete all existing queues */
  121. void snd_seq_queues_delete(void)
  122. {
  123. int i;
  124. /* clear list */
  125. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  126. if (queue_list[i])
  127. queue_delete(queue_list[i]);
  128. }
  129. }
  130. static void queue_use(struct snd_seq_queue *queue, int client, int use);
  131. /* allocate a new queue -
  132. * return pointer to new queue or ERR_PTR(-errno) for error
  133. * The new queue's use_lock is set to 1. It is the caller's responsibility to
  134. * call snd_use_lock_free(&q->use_lock).
  135. */
  136. struct snd_seq_queue *snd_seq_queue_alloc(int client, int locked, unsigned int info_flags)
  137. {
  138. struct snd_seq_queue *q;
  139. q = queue_new(client, locked);
  140. if (q == NULL)
  141. return ERR_PTR(-ENOMEM);
  142. q->info_flags = info_flags;
  143. queue_use(q, client, 1);
  144. snd_use_lock_use(&q->use_lock);
  145. if (queue_list_add(q) < 0) {
  146. snd_use_lock_free(&q->use_lock);
  147. queue_delete(q);
  148. return ERR_PTR(-ENOMEM);
  149. }
  150. return q;
  151. }
  152. /* delete a queue - queue must be owned by the client */
  153. int snd_seq_queue_delete(int client, int queueid)
  154. {
  155. struct snd_seq_queue *q;
  156. if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES)
  157. return -EINVAL;
  158. q = queue_list_remove(queueid, client);
  159. if (q == NULL)
  160. return -EINVAL;
  161. queue_delete(q);
  162. return 0;
  163. }
  164. /* return pointer to queue structure for specified id */
  165. struct snd_seq_queue *queueptr(int queueid)
  166. {
  167. struct snd_seq_queue *q;
  168. if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES)
  169. return NULL;
  170. guard(spinlock_irqsave)(&queue_list_lock);
  171. q = queue_list[queueid];
  172. if (q)
  173. snd_use_lock_use(&q->use_lock);
  174. return q;
  175. }
  176. /* return the (first) queue matching with the specified name */
  177. struct snd_seq_queue *snd_seq_queue_find_name(char *name)
  178. {
  179. int i;
  180. struct snd_seq_queue *q;
  181. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  182. q = queueptr(i);
  183. if (q) {
  184. if (strncmp(q->name, name, sizeof(q->name)) == 0)
  185. return q;
  186. queuefree(q);
  187. }
  188. }
  189. return NULL;
  190. }
  191. /* -------------------------------------------------------- */
  192. #define MAX_CELL_PROCESSES_IN_QUEUE 1000
  193. void snd_seq_check_queue(struct snd_seq_queue *q, int atomic, int hop)
  194. {
  195. struct snd_seq_event_cell *cell;
  196. snd_seq_tick_time_t cur_tick;
  197. snd_seq_real_time_t cur_time;
  198. int processed = 0;
  199. if (q == NULL)
  200. return;
  201. /* make this function non-reentrant */
  202. scoped_guard(spinlock_irqsave, &q->check_lock) {
  203. if (q->check_blocked) {
  204. q->check_again = 1;
  205. return; /* other thread is already checking queues */
  206. }
  207. q->check_blocked = 1;
  208. }
  209. __again:
  210. /* Process tick queue... */
  211. cur_tick = snd_seq_timer_get_cur_tick(q->timer);
  212. for (;;) {
  213. cell = snd_seq_prioq_cell_out(q->tickq, &cur_tick);
  214. if (!cell)
  215. break;
  216. snd_seq_dispatch_event(cell, atomic, hop);
  217. if (++processed >= MAX_CELL_PROCESSES_IN_QUEUE)
  218. goto out; /* the rest processed at the next batch */
  219. }
  220. /* Process time queue... */
  221. cur_time = snd_seq_timer_get_cur_time(q->timer, false);
  222. for (;;) {
  223. cell = snd_seq_prioq_cell_out(q->timeq, &cur_time);
  224. if (!cell)
  225. break;
  226. snd_seq_dispatch_event(cell, atomic, hop);
  227. if (++processed >= MAX_CELL_PROCESSES_IN_QUEUE)
  228. goto out; /* the rest processed at the next batch */
  229. }
  230. out:
  231. /* free lock */
  232. scoped_guard(spinlock_irqsave, &q->check_lock) {
  233. if (q->check_again) {
  234. q->check_again = 0;
  235. if (processed < MAX_CELL_PROCESSES_IN_QUEUE)
  236. goto __again;
  237. }
  238. q->check_blocked = 0;
  239. }
  240. }
  241. /* enqueue a event to singe queue */
  242. int snd_seq_enqueue_event(struct snd_seq_event_cell *cell, int atomic, int hop)
  243. {
  244. int dest, err;
  245. struct snd_seq_queue *q;
  246. if (snd_BUG_ON(!cell))
  247. return -EINVAL;
  248. dest = cell->event.queue; /* destination queue */
  249. q = queueptr(dest);
  250. if (q == NULL)
  251. return -EINVAL;
  252. /* handle relative time stamps, convert them into absolute */
  253. if ((cell->event.flags & SNDRV_SEQ_TIME_MODE_MASK) == SNDRV_SEQ_TIME_MODE_REL) {
  254. switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
  255. case SNDRV_SEQ_TIME_STAMP_TICK:
  256. cell->event.time.tick += q->timer->tick.cur_tick;
  257. break;
  258. case SNDRV_SEQ_TIME_STAMP_REAL:
  259. snd_seq_inc_real_time(&cell->event.time.time,
  260. &q->timer->cur_time);
  261. break;
  262. }
  263. cell->event.flags &= ~SNDRV_SEQ_TIME_MODE_MASK;
  264. cell->event.flags |= SNDRV_SEQ_TIME_MODE_ABS;
  265. }
  266. /* enqueue event in the real-time or midi queue */
  267. switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
  268. case SNDRV_SEQ_TIME_STAMP_TICK:
  269. err = snd_seq_prioq_cell_in(q->tickq, cell);
  270. break;
  271. case SNDRV_SEQ_TIME_STAMP_REAL:
  272. default:
  273. err = snd_seq_prioq_cell_in(q->timeq, cell);
  274. break;
  275. }
  276. if (err < 0) {
  277. queuefree(q); /* unlock */
  278. return err;
  279. }
  280. /* trigger dispatching */
  281. snd_seq_check_queue(q, atomic, hop);
  282. queuefree(q); /* unlock */
  283. return 0;
  284. }
  285. /*----------------------------------------------------------------*/
  286. static inline int check_access(struct snd_seq_queue *q, int client)
  287. {
  288. return (q->owner == client) || (!q->locked && !q->klocked);
  289. }
  290. /* check if the client has permission to modify queue parameters.
  291. * if it does, lock the queue
  292. */
  293. static int queue_access_lock(struct snd_seq_queue *q, int client)
  294. {
  295. int access_ok;
  296. guard(spinlock_irqsave)(&q->owner_lock);
  297. access_ok = check_access(q, client);
  298. if (access_ok)
  299. q->klocked = 1;
  300. return access_ok;
  301. }
  302. /* unlock the queue */
  303. static inline void queue_access_unlock(struct snd_seq_queue *q)
  304. {
  305. guard(spinlock_irqsave)(&q->owner_lock);
  306. q->klocked = 0;
  307. }
  308. /* exported - only checking permission */
  309. int snd_seq_queue_check_access(int queueid, int client)
  310. {
  311. struct snd_seq_queue *q = queueptr(queueid);
  312. int access_ok;
  313. if (! q)
  314. return 0;
  315. scoped_guard(spinlock_irqsave, &q->owner_lock)
  316. access_ok = check_access(q, client);
  317. queuefree(q);
  318. return access_ok;
  319. }
  320. /*----------------------------------------------------------------*/
  321. /*
  322. * change queue's owner and permission
  323. */
  324. int snd_seq_queue_set_owner(int queueid, int client, int locked)
  325. {
  326. struct snd_seq_queue *q = queueptr(queueid);
  327. if (q == NULL)
  328. return -EINVAL;
  329. if (! queue_access_lock(q, client)) {
  330. queuefree(q);
  331. return -EPERM;
  332. }
  333. scoped_guard(spinlock_irqsave, &q->owner_lock) {
  334. q->locked = locked ? 1 : 0;
  335. q->owner = client;
  336. }
  337. queue_access_unlock(q);
  338. queuefree(q);
  339. return 0;
  340. }
  341. /*----------------------------------------------------------------*/
  342. /* open timer -
  343. * q->use mutex should be down before calling this function to avoid
  344. * confliction with snd_seq_queue_use()
  345. */
  346. int snd_seq_queue_timer_open(int queueid)
  347. {
  348. int result = 0;
  349. struct snd_seq_queue *queue;
  350. struct snd_seq_timer *tmr;
  351. queue = queueptr(queueid);
  352. if (queue == NULL)
  353. return -EINVAL;
  354. tmr = queue->timer;
  355. result = snd_seq_timer_open(queue);
  356. if (result < 0) {
  357. snd_seq_timer_defaults(tmr);
  358. result = snd_seq_timer_open(queue);
  359. }
  360. queuefree(queue);
  361. return result;
  362. }
  363. /* close timer -
  364. * q->use mutex should be down before calling this function
  365. */
  366. int snd_seq_queue_timer_close(int queueid)
  367. {
  368. struct snd_seq_queue *queue;
  369. int result = 0;
  370. queue = queueptr(queueid);
  371. if (queue == NULL)
  372. return -EINVAL;
  373. snd_seq_timer_close(queue);
  374. queuefree(queue);
  375. return result;
  376. }
  377. /* change queue tempo and ppq */
  378. int snd_seq_queue_timer_set_tempo(int queueid, int client,
  379. struct snd_seq_queue_tempo *info)
  380. {
  381. struct snd_seq_queue *q = queueptr(queueid);
  382. int result;
  383. if (q == NULL)
  384. return -EINVAL;
  385. if (! queue_access_lock(q, client)) {
  386. queuefree(q);
  387. return -EPERM;
  388. }
  389. result = snd_seq_timer_set_tempo_ppq(q->timer, info->tempo, info->ppq,
  390. info->tempo_base);
  391. if (result >= 0 && info->skew_base > 0)
  392. result = snd_seq_timer_set_skew(q->timer, info->skew_value,
  393. info->skew_base);
  394. queue_access_unlock(q);
  395. queuefree(q);
  396. return result;
  397. }
  398. /* use or unuse this queue */
  399. static void queue_use(struct snd_seq_queue *queue, int client, int use)
  400. {
  401. if (use) {
  402. if (!test_and_set_bit(client, queue->clients_bitmap))
  403. queue->clients++;
  404. } else {
  405. if (test_and_clear_bit(client, queue->clients_bitmap))
  406. queue->clients--;
  407. }
  408. if (queue->clients) {
  409. if (use && queue->clients == 1)
  410. snd_seq_timer_defaults(queue->timer);
  411. snd_seq_timer_open(queue);
  412. } else {
  413. snd_seq_timer_close(queue);
  414. }
  415. }
  416. /* use or unuse this queue -
  417. * if it is the first client, starts the timer.
  418. * if it is not longer used by any clients, stop the timer.
  419. */
  420. int snd_seq_queue_use(int queueid, int client, int use)
  421. {
  422. struct snd_seq_queue *queue;
  423. queue = queueptr(queueid);
  424. if (queue == NULL)
  425. return -EINVAL;
  426. mutex_lock(&queue->timer_mutex);
  427. queue_use(queue, client, use);
  428. mutex_unlock(&queue->timer_mutex);
  429. queuefree(queue);
  430. return 0;
  431. }
  432. /*
  433. * check if queue is used by the client
  434. * return negative value if the queue is invalid.
  435. * return 0 if not used, 1 if used.
  436. */
  437. int snd_seq_queue_is_used(int queueid, int client)
  438. {
  439. struct snd_seq_queue *q;
  440. int result;
  441. q = queueptr(queueid);
  442. if (q == NULL)
  443. return -EINVAL; /* invalid queue */
  444. result = test_bit(client, q->clients_bitmap) ? 1 : 0;
  445. queuefree(q);
  446. return result;
  447. }
  448. /*----------------------------------------------------------------*/
  449. /* final stage notification -
  450. * remove cells for no longer exist client (for non-owned queue)
  451. * or delete this queue (for owned queue)
  452. */
  453. void snd_seq_queue_client_leave(int client)
  454. {
  455. int i;
  456. struct snd_seq_queue *q;
  457. /* delete own queues from queue list */
  458. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  459. q = queue_list_remove(i, client);
  460. if (q)
  461. queue_delete(q);
  462. }
  463. /* remove cells from existing queues -
  464. * they are not owned by this client
  465. */
  466. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  467. q = queueptr(i);
  468. if (!q)
  469. continue;
  470. if (test_bit(client, q->clients_bitmap)) {
  471. snd_seq_prioq_leave(q->tickq, client, 0);
  472. snd_seq_prioq_leave(q->timeq, client, 0);
  473. snd_seq_queue_use(q->queue, client, 0);
  474. }
  475. queuefree(q);
  476. }
  477. }
  478. /*----------------------------------------------------------------*/
  479. /* remove cells from all queues */
  480. void snd_seq_queue_client_leave_cells(int client)
  481. {
  482. int i;
  483. struct snd_seq_queue *q;
  484. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  485. q = queueptr(i);
  486. if (!q)
  487. continue;
  488. snd_seq_prioq_leave(q->tickq, client, 0);
  489. snd_seq_prioq_leave(q->timeq, client, 0);
  490. queuefree(q);
  491. }
  492. }
  493. /* remove cells based on flush criteria */
  494. void snd_seq_queue_remove_cells(int client, struct snd_seq_remove_events *info)
  495. {
  496. int i;
  497. struct snd_seq_queue *q;
  498. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  499. q = queueptr(i);
  500. if (!q)
  501. continue;
  502. if (test_bit(client, q->clients_bitmap) &&
  503. (! (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) ||
  504. q->queue == info->queue)) {
  505. snd_seq_prioq_remove_events(q->tickq, client, info);
  506. snd_seq_prioq_remove_events(q->timeq, client, info);
  507. }
  508. queuefree(q);
  509. }
  510. }
  511. /*----------------------------------------------------------------*/
  512. /*
  513. * send events to all subscribed ports
  514. */
  515. static void queue_broadcast_event(struct snd_seq_queue *q, struct snd_seq_event *ev,
  516. int atomic, int hop)
  517. {
  518. struct snd_seq_event sev;
  519. sev = *ev;
  520. sev.flags = SNDRV_SEQ_TIME_STAMP_TICK|SNDRV_SEQ_TIME_MODE_ABS;
  521. sev.time.tick = q->timer->tick.cur_tick;
  522. sev.queue = q->queue;
  523. sev.data.queue.queue = q->queue;
  524. /* broadcast events from Timer port */
  525. sev.source.client = SNDRV_SEQ_CLIENT_SYSTEM;
  526. sev.source.port = SNDRV_SEQ_PORT_SYSTEM_TIMER;
  527. sev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  528. snd_seq_kernel_client_dispatch(SNDRV_SEQ_CLIENT_SYSTEM, &sev, atomic, hop);
  529. }
  530. /*
  531. * process a received queue-control event.
  532. * this function is exported for seq_sync.c.
  533. */
  534. static void snd_seq_queue_process_event(struct snd_seq_queue *q,
  535. struct snd_seq_event *ev,
  536. int atomic, int hop)
  537. {
  538. switch (ev->type) {
  539. case SNDRV_SEQ_EVENT_START:
  540. snd_seq_prioq_leave(q->tickq, ev->source.client, 1);
  541. snd_seq_prioq_leave(q->timeq, ev->source.client, 1);
  542. if (! snd_seq_timer_start(q->timer))
  543. queue_broadcast_event(q, ev, atomic, hop);
  544. break;
  545. case SNDRV_SEQ_EVENT_CONTINUE:
  546. if (! snd_seq_timer_continue(q->timer))
  547. queue_broadcast_event(q, ev, atomic, hop);
  548. break;
  549. case SNDRV_SEQ_EVENT_STOP:
  550. snd_seq_timer_stop(q->timer);
  551. queue_broadcast_event(q, ev, atomic, hop);
  552. break;
  553. case SNDRV_SEQ_EVENT_TEMPO:
  554. snd_seq_timer_set_tempo(q->timer, ev->data.queue.param.value);
  555. queue_broadcast_event(q, ev, atomic, hop);
  556. break;
  557. case SNDRV_SEQ_EVENT_SETPOS_TICK:
  558. if (snd_seq_timer_set_position_tick(q->timer, ev->data.queue.param.time.tick) == 0) {
  559. queue_broadcast_event(q, ev, atomic, hop);
  560. }
  561. break;
  562. case SNDRV_SEQ_EVENT_SETPOS_TIME:
  563. if (snd_seq_timer_set_position_time(q->timer, ev->data.queue.param.time.time) == 0) {
  564. queue_broadcast_event(q, ev, atomic, hop);
  565. }
  566. break;
  567. case SNDRV_SEQ_EVENT_QUEUE_SKEW:
  568. if (snd_seq_timer_set_skew(q->timer,
  569. ev->data.queue.param.skew.value,
  570. ev->data.queue.param.skew.base) == 0) {
  571. queue_broadcast_event(q, ev, atomic, hop);
  572. }
  573. break;
  574. }
  575. }
  576. /*
  577. * Queue control via timer control port:
  578. * this function is exported as a callback of timer port.
  579. */
  580. int snd_seq_control_queue(struct snd_seq_event *ev, int atomic, int hop)
  581. {
  582. struct snd_seq_queue *q;
  583. if (snd_BUG_ON(!ev))
  584. return -EINVAL;
  585. q = queueptr(ev->data.queue.queue);
  586. if (q == NULL)
  587. return -EINVAL;
  588. if (! queue_access_lock(q, ev->source.client)) {
  589. queuefree(q);
  590. return -EPERM;
  591. }
  592. snd_seq_queue_process_event(q, ev, atomic, hop);
  593. queue_access_unlock(q);
  594. queuefree(q);
  595. return 0;
  596. }
  597. /*----------------------------------------------------------------*/
  598. #ifdef CONFIG_SND_PROC_FS
  599. /* exported to seq_info.c */
  600. void snd_seq_info_queues_read(struct snd_info_entry *entry,
  601. struct snd_info_buffer *buffer)
  602. {
  603. int i, bpm;
  604. struct snd_seq_queue *q;
  605. struct snd_seq_timer *tmr;
  606. bool locked;
  607. int owner;
  608. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  609. q = queueptr(i);
  610. if (!q)
  611. continue;
  612. tmr = q->timer;
  613. if (tmr->tempo)
  614. bpm = (60000 * tmr->tempo_base) / tmr->tempo;
  615. else
  616. bpm = 0;
  617. scoped_guard(spinlock_irq, &q->owner_lock) {
  618. locked = q->locked;
  619. owner = q->owner;
  620. }
  621. snd_iprintf(buffer, "queue %d: [%s]\n", q->queue, q->name);
  622. snd_iprintf(buffer, "owned by client : %d\n", owner);
  623. snd_iprintf(buffer, "lock status : %s\n", locked ? "Locked" : "Free");
  624. snd_iprintf(buffer, "queued time events : %d\n", snd_seq_prioq_avail(q->timeq));
  625. snd_iprintf(buffer, "queued tick events : %d\n", snd_seq_prioq_avail(q->tickq));
  626. snd_iprintf(buffer, "timer state : %s\n", tmr->running ? "Running" : "Stopped");
  627. snd_iprintf(buffer, "timer PPQ : %d\n", tmr->ppq);
  628. snd_iprintf(buffer, "current tempo : %d\n", tmr->tempo);
  629. snd_iprintf(buffer, "tempo base : %d ns\n", tmr->tempo_base);
  630. snd_iprintf(buffer, "current BPM : %d\n", bpm);
  631. snd_iprintf(buffer, "current time : %d.%09d s\n", tmr->cur_time.tv_sec, tmr->cur_time.tv_nsec);
  632. snd_iprintf(buffer, "current tick : %d\n", tmr->tick.cur_tick);
  633. snd_iprintf(buffer, "\n");
  634. queuefree(q);
  635. }
  636. }
  637. #endif /* CONFIG_SND_PROC_FS */