seq_queue.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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 == NULL) {
  103. snd_printd("malloc failed for snd_seq_queue_new()\n");
  104. return NULL;
  105. }
  106. spin_lock_init(&q->owner_lock);
  107. spin_lock_init(&q->check_lock);
  108. mutex_init(&q->timer_mutex);
  109. snd_use_lock_init(&q->use_lock);
  110. q->queue = -1;
  111. q->tickq = snd_seq_prioq_new();
  112. q->timeq = snd_seq_prioq_new();
  113. q->timer = snd_seq_timer_new();
  114. if (q->tickq == NULL || q->timeq == NULL || q->timer == NULL) {
  115. snd_seq_prioq_delete(&q->tickq);
  116. snd_seq_prioq_delete(&q->timeq);
  117. snd_seq_timer_delete(&q->timer);
  118. kfree(q);
  119. return NULL;
  120. }
  121. q->owner = owner;
  122. q->locked = locked;
  123. q->klocked = 0;
  124. return q;
  125. }
  126. /* delete queue (destructor) */
  127. static void queue_delete(struct snd_seq_queue *q)
  128. {
  129. /* stop and release the timer */
  130. snd_seq_timer_stop(q->timer);
  131. snd_seq_timer_close(q);
  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. /* allocate a new queue -
  161. * return queue index value or negative value for error
  162. */
  163. int snd_seq_queue_alloc(int client, int locked, unsigned int info_flags)
  164. {
  165. struct snd_seq_queue *q;
  166. q = queue_new(client, locked);
  167. if (q == NULL)
  168. return -ENOMEM;
  169. q->info_flags = info_flags;
  170. if (queue_list_add(q) < 0) {
  171. queue_delete(q);
  172. return -ENOMEM;
  173. }
  174. snd_seq_queue_use(q->queue, client, 1); /* use this queue */
  175. return q->queue;
  176. }
  177. /* delete a queue - queue must be owned by the client */
  178. int snd_seq_queue_delete(int client, int queueid)
  179. {
  180. struct snd_seq_queue *q;
  181. if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES)
  182. return -EINVAL;
  183. q = queue_list_remove(queueid, client);
  184. if (q == NULL)
  185. return -EINVAL;
  186. queue_delete(q);
  187. return 0;
  188. }
  189. /* return pointer to queue structure for specified id */
  190. struct snd_seq_queue *queueptr(int queueid)
  191. {
  192. struct snd_seq_queue *q;
  193. unsigned long flags;
  194. if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES)
  195. return NULL;
  196. spin_lock_irqsave(&queue_list_lock, flags);
  197. q = queue_list[queueid];
  198. if (q)
  199. snd_use_lock_use(&q->use_lock);
  200. spin_unlock_irqrestore(&queue_list_lock, flags);
  201. return q;
  202. }
  203. /* return the (first) queue matching with the specified name */
  204. struct snd_seq_queue *snd_seq_queue_find_name(char *name)
  205. {
  206. int i;
  207. struct snd_seq_queue *q;
  208. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  209. if ((q = queueptr(i)) != NULL) {
  210. if (strncmp(q->name, name, sizeof(q->name)) == 0)
  211. return q;
  212. queuefree(q);
  213. }
  214. }
  215. return NULL;
  216. }
  217. /* -------------------------------------------------------- */
  218. void snd_seq_check_queue(struct snd_seq_queue *q, int atomic, int hop)
  219. {
  220. unsigned long flags;
  221. struct snd_seq_event_cell *cell;
  222. if (q == NULL)
  223. return;
  224. /* make this function non-reentrant */
  225. spin_lock_irqsave(&q->check_lock, flags);
  226. if (q->check_blocked) {
  227. q->check_again = 1;
  228. spin_unlock_irqrestore(&q->check_lock, flags);
  229. return; /* other thread is already checking queues */
  230. }
  231. q->check_blocked = 1;
  232. spin_unlock_irqrestore(&q->check_lock, flags);
  233. __again:
  234. /* Process tick queue... */
  235. while ((cell = snd_seq_prioq_cell_peek(q->tickq)) != NULL) {
  236. if (snd_seq_compare_tick_time(&q->timer->tick.cur_tick,
  237. &cell->event.time.tick)) {
  238. cell = snd_seq_prioq_cell_out(q->tickq);
  239. if (cell)
  240. snd_seq_dispatch_event(cell, atomic, hop);
  241. } else {
  242. /* event remains in the queue */
  243. break;
  244. }
  245. }
  246. /* Process time queue... */
  247. while ((cell = snd_seq_prioq_cell_peek(q->timeq)) != NULL) {
  248. if (snd_seq_compare_real_time(&q->timer->cur_time,
  249. &cell->event.time.time)) {
  250. cell = snd_seq_prioq_cell_out(q->timeq);
  251. if (cell)
  252. snd_seq_dispatch_event(cell, atomic, hop);
  253. } else {
  254. /* event remains in the queue */
  255. break;
  256. }
  257. }
  258. /* free lock */
  259. spin_lock_irqsave(&q->check_lock, flags);
  260. if (q->check_again) {
  261. q->check_again = 0;
  262. spin_unlock_irqrestore(&q->check_lock, flags);
  263. goto __again;
  264. }
  265. q->check_blocked = 0;
  266. spin_unlock_irqrestore(&q->check_lock, flags);
  267. }
  268. /* enqueue a event to singe queue */
  269. int snd_seq_enqueue_event(struct snd_seq_event_cell *cell, int atomic, int hop)
  270. {
  271. int dest, err;
  272. struct snd_seq_queue *q;
  273. snd_assert(cell != NULL, return -EINVAL);
  274. dest = cell->event.queue; /* destination queue */
  275. q = queueptr(dest);
  276. if (q == NULL)
  277. return -EINVAL;
  278. /* handle relative time stamps, convert them into absolute */
  279. if ((cell->event.flags & SNDRV_SEQ_TIME_MODE_MASK) == SNDRV_SEQ_TIME_MODE_REL) {
  280. switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
  281. case SNDRV_SEQ_TIME_STAMP_TICK:
  282. cell->event.time.tick += q->timer->tick.cur_tick;
  283. break;
  284. case SNDRV_SEQ_TIME_STAMP_REAL:
  285. snd_seq_inc_real_time(&cell->event.time.time,
  286. &q->timer->cur_time);
  287. break;
  288. }
  289. cell->event.flags &= ~SNDRV_SEQ_TIME_MODE_MASK;
  290. cell->event.flags |= SNDRV_SEQ_TIME_MODE_ABS;
  291. }
  292. /* enqueue event in the real-time or midi queue */
  293. switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
  294. case SNDRV_SEQ_TIME_STAMP_TICK:
  295. err = snd_seq_prioq_cell_in(q->tickq, cell);
  296. break;
  297. case SNDRV_SEQ_TIME_STAMP_REAL:
  298. default:
  299. err = snd_seq_prioq_cell_in(q->timeq, cell);
  300. break;
  301. }
  302. if (err < 0) {
  303. queuefree(q); /* unlock */
  304. return err;
  305. }
  306. /* trigger dispatching */
  307. snd_seq_check_queue(q, atomic, hop);
  308. queuefree(q); /* unlock */
  309. return 0;
  310. }
  311. /*----------------------------------------------------------------*/
  312. static inline int check_access(struct snd_seq_queue *q, int client)
  313. {
  314. return (q->owner == client) || (!q->locked && !q->klocked);
  315. }
  316. /* check if the client has permission to modify queue parameters.
  317. * if it does, lock the queue
  318. */
  319. static int queue_access_lock(struct snd_seq_queue *q, int client)
  320. {
  321. unsigned long flags;
  322. int access_ok;
  323. spin_lock_irqsave(&q->owner_lock, flags);
  324. access_ok = check_access(q, client);
  325. if (access_ok)
  326. q->klocked = 1;
  327. spin_unlock_irqrestore(&q->owner_lock, flags);
  328. return access_ok;
  329. }
  330. /* unlock the queue */
  331. static inline void queue_access_unlock(struct snd_seq_queue *q)
  332. {
  333. unsigned long flags;
  334. spin_lock_irqsave(&q->owner_lock, flags);
  335. q->klocked = 0;
  336. spin_unlock_irqrestore(&q->owner_lock, flags);
  337. }
  338. /* exported - only checking permission */
  339. int snd_seq_queue_check_access(int queueid, int client)
  340. {
  341. struct snd_seq_queue *q = queueptr(queueid);
  342. int access_ok;
  343. unsigned long flags;
  344. if (! q)
  345. return 0;
  346. spin_lock_irqsave(&q->owner_lock, flags);
  347. access_ok = check_access(q, client);
  348. spin_unlock_irqrestore(&q->owner_lock, flags);
  349. queuefree(q);
  350. return access_ok;
  351. }
  352. /*----------------------------------------------------------------*/
  353. /*
  354. * change queue's owner and permission
  355. */
  356. int snd_seq_queue_set_owner(int queueid, int client, int locked)
  357. {
  358. struct snd_seq_queue *q = queueptr(queueid);
  359. if (q == NULL)
  360. return -EINVAL;
  361. if (! queue_access_lock(q, client)) {
  362. queuefree(q);
  363. return -EPERM;
  364. }
  365. q->locked = locked ? 1 : 0;
  366. q->owner = client;
  367. queue_access_unlock(q);
  368. queuefree(q);
  369. return 0;
  370. }
  371. /*----------------------------------------------------------------*/
  372. /* open timer -
  373. * q->use mutex should be down before calling this function to avoid
  374. * confliction with snd_seq_queue_use()
  375. */
  376. int snd_seq_queue_timer_open(int queueid)
  377. {
  378. int result = 0;
  379. struct snd_seq_queue *queue;
  380. struct snd_seq_timer *tmr;
  381. queue = queueptr(queueid);
  382. if (queue == NULL)
  383. return -EINVAL;
  384. tmr = queue->timer;
  385. if ((result = snd_seq_timer_open(queue)) < 0) {
  386. snd_seq_timer_defaults(tmr);
  387. result = snd_seq_timer_open(queue);
  388. }
  389. queuefree(queue);
  390. return result;
  391. }
  392. /* close timer -
  393. * q->use mutex should be down before calling this function
  394. */
  395. int snd_seq_queue_timer_close(int queueid)
  396. {
  397. struct snd_seq_queue *queue;
  398. struct snd_seq_timer *tmr;
  399. int result = 0;
  400. queue = queueptr(queueid);
  401. if (queue == NULL)
  402. return -EINVAL;
  403. tmr = queue->timer;
  404. snd_seq_timer_close(queue);
  405. queuefree(queue);
  406. return result;
  407. }
  408. /* change queue tempo and ppq */
  409. int snd_seq_queue_timer_set_tempo(int queueid, int client,
  410. struct snd_seq_queue_tempo *info)
  411. {
  412. struct snd_seq_queue *q = queueptr(queueid);
  413. int result;
  414. if (q == NULL)
  415. return -EINVAL;
  416. if (! queue_access_lock(q, client)) {
  417. queuefree(q);
  418. return -EPERM;
  419. }
  420. result = snd_seq_timer_set_tempo(q->timer, info->tempo);
  421. if (result >= 0)
  422. result = snd_seq_timer_set_ppq(q->timer, info->ppq);
  423. if (result >= 0 && info->skew_base > 0)
  424. result = snd_seq_timer_set_skew(q->timer, info->skew_value,
  425. info->skew_base);
  426. queue_access_unlock(q);
  427. queuefree(q);
  428. return result;
  429. }
  430. /* use or unuse this queue -
  431. * if it is the first client, starts the timer.
  432. * if it is not longer used by any clients, stop the timer.
  433. */
  434. int snd_seq_queue_use(int queueid, int client, int use)
  435. {
  436. struct snd_seq_queue *queue;
  437. queue = queueptr(queueid);
  438. if (queue == NULL)
  439. return -EINVAL;
  440. mutex_lock(&queue->timer_mutex);
  441. if (use) {
  442. if (!test_and_set_bit(client, queue->clients_bitmap))
  443. queue->clients++;
  444. } else {
  445. if (test_and_clear_bit(client, queue->clients_bitmap))
  446. queue->clients--;
  447. }
  448. if (queue->clients) {
  449. if (use && queue->clients == 1)
  450. snd_seq_timer_defaults(queue->timer);
  451. snd_seq_timer_open(queue);
  452. } else {
  453. snd_seq_timer_close(queue);
  454. }
  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. snd_assert(ev != NULL, return -EINVAL);
  630. q = queueptr(ev->data.queue.queue);
  631. if (q == NULL)
  632. return -EINVAL;
  633. if (! queue_access_lock(q, ev->source.client)) {
  634. queuefree(q);
  635. return -EPERM;
  636. }
  637. snd_seq_queue_process_event(q, ev, atomic, hop);
  638. queue_access_unlock(q);
  639. queuefree(q);
  640. return 0;
  641. }
  642. /*----------------------------------------------------------------*/
  643. #ifdef CONFIG_PROC_FS
  644. /* exported to seq_info.c */
  645. void snd_seq_info_queues_read(struct snd_info_entry *entry,
  646. struct snd_info_buffer *buffer)
  647. {
  648. int i, bpm;
  649. struct snd_seq_queue *q;
  650. struct snd_seq_timer *tmr;
  651. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  652. if ((q = queueptr(i)) == NULL)
  653. continue;
  654. tmr = q->timer;
  655. if (tmr->tempo)
  656. bpm = 60000000 / tmr->tempo;
  657. else
  658. bpm = 0;
  659. snd_iprintf(buffer, "queue %d: [%s]\n", q->queue, q->name);
  660. snd_iprintf(buffer, "owned by client : %d\n", q->owner);
  661. snd_iprintf(buffer, "lock status : %s\n", q->locked ? "Locked" : "Free");
  662. snd_iprintf(buffer, "queued time events : %d\n", snd_seq_prioq_avail(q->timeq));
  663. snd_iprintf(buffer, "queued tick events : %d\n", snd_seq_prioq_avail(q->tickq));
  664. snd_iprintf(buffer, "timer state : %s\n", tmr->running ? "Running" : "Stopped");
  665. snd_iprintf(buffer, "timer PPQ : %d\n", tmr->ppq);
  666. snd_iprintf(buffer, "current tempo : %d\n", tmr->tempo);
  667. snd_iprintf(buffer, "current BPM : %d\n", bpm);
  668. snd_iprintf(buffer, "current time : %d.%09d s\n", tmr->cur_time.tv_sec, tmr->cur_time.tv_nsec);
  669. snd_iprintf(buffer, "current tick : %d\n", tmr->tick.cur_tick);
  670. snd_iprintf(buffer, "\n");
  671. queuefree(q);
  672. }
  673. }
  674. #endif /* CONFIG_PROC_FS */