seq_queue.c 19 KB

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