seq_queue.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. snd_seq_timer_stop(q->timer);
  129. snd_seq_timer_close(q);
  130. /* wait until access free */
  131. snd_use_lock_sync(&q->use_lock);
  132. /* release resources... */
  133. snd_seq_prioq_delete(&q->tickq);
  134. snd_seq_prioq_delete(&q->timeq);
  135. snd_seq_timer_delete(&q->timer);
  136. kfree(q);
  137. }
  138. /*----------------------------------------------------------------*/
  139. /* setup queues */
  140. int __init snd_seq_queues_init(void)
  141. {
  142. /*
  143. memset(queue_list, 0, sizeof(queue_list));
  144. num_queues = 0;
  145. */
  146. return 0;
  147. }
  148. /* delete all existing queues */
  149. void __exit snd_seq_queues_delete(void)
  150. {
  151. int i;
  152. /* clear list */
  153. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  154. if (queue_list[i])
  155. queue_delete(queue_list[i]);
  156. }
  157. }
  158. /* allocate a new queue -
  159. * return queue index value or negative value for error
  160. */
  161. int snd_seq_queue_alloc(int client, int locked, unsigned int info_flags)
  162. {
  163. struct snd_seq_queue *q;
  164. q = queue_new(client, locked);
  165. if (q == NULL)
  166. return -ENOMEM;
  167. q->info_flags = info_flags;
  168. if (queue_list_add(q) < 0) {
  169. queue_delete(q);
  170. return -ENOMEM;
  171. }
  172. snd_seq_queue_use(q->queue, client, 1); /* use this queue */
  173. return q->queue;
  174. }
  175. /* delete a queue - queue must be owned by the client */
  176. int snd_seq_queue_delete(int client, int queueid)
  177. {
  178. struct snd_seq_queue *q;
  179. if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES)
  180. return -EINVAL;
  181. q = queue_list_remove(queueid, client);
  182. if (q == NULL)
  183. return -EINVAL;
  184. queue_delete(q);
  185. return 0;
  186. }
  187. /* return pointer to queue structure for specified id */
  188. struct snd_seq_queue *queueptr(int queueid)
  189. {
  190. struct snd_seq_queue *q;
  191. unsigned long flags;
  192. if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES)
  193. return NULL;
  194. spin_lock_irqsave(&queue_list_lock, flags);
  195. q = queue_list[queueid];
  196. if (q)
  197. snd_use_lock_use(&q->use_lock);
  198. spin_unlock_irqrestore(&queue_list_lock, flags);
  199. return q;
  200. }
  201. /* return the (first) queue matching with the specified name */
  202. struct snd_seq_queue *snd_seq_queue_find_name(char *name)
  203. {
  204. int i;
  205. struct snd_seq_queue *q;
  206. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  207. if ((q = queueptr(i)) != NULL) {
  208. if (strncmp(q->name, name, sizeof(q->name)) == 0)
  209. return q;
  210. queuefree(q);
  211. }
  212. }
  213. return NULL;
  214. }
  215. /* -------------------------------------------------------- */
  216. void snd_seq_check_queue(struct snd_seq_queue *q, int atomic, int hop)
  217. {
  218. unsigned long flags;
  219. struct snd_seq_event_cell *cell;
  220. if (q == NULL)
  221. return;
  222. /* make this function non-reentrant */
  223. spin_lock_irqsave(&q->check_lock, flags);
  224. if (q->check_blocked) {
  225. q->check_again = 1;
  226. spin_unlock_irqrestore(&q->check_lock, flags);
  227. return; /* other thread is already checking queues */
  228. }
  229. q->check_blocked = 1;
  230. spin_unlock_irqrestore(&q->check_lock, flags);
  231. __again:
  232. /* Process tick queue... */
  233. while ((cell = snd_seq_prioq_cell_peek(q->tickq)) != NULL) {
  234. if (snd_seq_compare_tick_time(&q->timer->tick.cur_tick,
  235. &cell->event.time.tick)) {
  236. cell = snd_seq_prioq_cell_out(q->tickq);
  237. if (cell)
  238. snd_seq_dispatch_event(cell, atomic, hop);
  239. } else {
  240. /* event remains in the queue */
  241. break;
  242. }
  243. }
  244. /* Process time queue... */
  245. while ((cell = snd_seq_prioq_cell_peek(q->timeq)) != NULL) {
  246. if (snd_seq_compare_real_time(&q->timer->cur_time,
  247. &cell->event.time.time)) {
  248. cell = snd_seq_prioq_cell_out(q->timeq);
  249. if (cell)
  250. snd_seq_dispatch_event(cell, atomic, hop);
  251. } else {
  252. /* event remains in the queue */
  253. break;
  254. }
  255. }
  256. /* free lock */
  257. spin_lock_irqsave(&q->check_lock, flags);
  258. if (q->check_again) {
  259. q->check_again = 0;
  260. spin_unlock_irqrestore(&q->check_lock, flags);
  261. goto __again;
  262. }
  263. q->check_blocked = 0;
  264. spin_unlock_irqrestore(&q->check_lock, flags);
  265. }
  266. /* enqueue a event to singe queue */
  267. int snd_seq_enqueue_event(struct snd_seq_event_cell *cell, int atomic, int hop)
  268. {
  269. int dest, err;
  270. struct snd_seq_queue *q;
  271. if (snd_BUG_ON(!cell))
  272. return -EINVAL;
  273. dest = cell->event.queue; /* destination queue */
  274. q = queueptr(dest);
  275. if (q == NULL)
  276. return -EINVAL;
  277. /* handle relative time stamps, convert them into absolute */
  278. if ((cell->event.flags & SNDRV_SEQ_TIME_MODE_MASK) == SNDRV_SEQ_TIME_MODE_REL) {
  279. switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
  280. case SNDRV_SEQ_TIME_STAMP_TICK:
  281. cell->event.time.tick += q->timer->tick.cur_tick;
  282. break;
  283. case SNDRV_SEQ_TIME_STAMP_REAL:
  284. snd_seq_inc_real_time(&cell->event.time.time,
  285. &q->timer->cur_time);
  286. break;
  287. }
  288. cell->event.flags &= ~SNDRV_SEQ_TIME_MODE_MASK;
  289. cell->event.flags |= SNDRV_SEQ_TIME_MODE_ABS;
  290. }
  291. /* enqueue event in the real-time or midi queue */
  292. switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
  293. case SNDRV_SEQ_TIME_STAMP_TICK:
  294. err = snd_seq_prioq_cell_in(q->tickq, cell);
  295. break;
  296. case SNDRV_SEQ_TIME_STAMP_REAL:
  297. default:
  298. err = snd_seq_prioq_cell_in(q->timeq, cell);
  299. break;
  300. }
  301. if (err < 0) {
  302. queuefree(q); /* unlock */
  303. return err;
  304. }
  305. /* trigger dispatching */
  306. snd_seq_check_queue(q, atomic, hop);
  307. queuefree(q); /* unlock */
  308. return 0;
  309. }
  310. /*----------------------------------------------------------------*/
  311. static inline int check_access(struct snd_seq_queue *q, int client)
  312. {
  313. return (q->owner == client) || (!q->locked && !q->klocked);
  314. }
  315. /* check if the client has permission to modify queue parameters.
  316. * if it does, lock the queue
  317. */
  318. static int queue_access_lock(struct snd_seq_queue *q, int client)
  319. {
  320. unsigned long flags;
  321. int access_ok;
  322. spin_lock_irqsave(&q->owner_lock, flags);
  323. access_ok = check_access(q, client);
  324. if (access_ok)
  325. q->klocked = 1;
  326. spin_unlock_irqrestore(&q->owner_lock, flags);
  327. return access_ok;
  328. }
  329. /* unlock the queue */
  330. static inline void queue_access_unlock(struct snd_seq_queue *q)
  331. {
  332. unsigned long flags;
  333. spin_lock_irqsave(&q->owner_lock, flags);
  334. q->klocked = 0;
  335. spin_unlock_irqrestore(&q->owner_lock, flags);
  336. }
  337. /* exported - only checking permission */
  338. int snd_seq_queue_check_access(int queueid, int client)
  339. {
  340. struct snd_seq_queue *q = queueptr(queueid);
  341. int access_ok;
  342. unsigned long flags;
  343. if (! q)
  344. return 0;
  345. spin_lock_irqsave(&q->owner_lock, flags);
  346. access_ok = check_access(q, client);
  347. spin_unlock_irqrestore(&q->owner_lock, flags);
  348. queuefree(q);
  349. return access_ok;
  350. }
  351. /*----------------------------------------------------------------*/
  352. /*
  353. * change queue's owner and permission
  354. */
  355. int snd_seq_queue_set_owner(int queueid, int client, int locked)
  356. {
  357. struct snd_seq_queue *q = queueptr(queueid);
  358. if (q == NULL)
  359. return -EINVAL;
  360. if (! queue_access_lock(q, client)) {
  361. queuefree(q);
  362. return -EPERM;
  363. }
  364. q->locked = locked ? 1 : 0;
  365. q->owner = client;
  366. queue_access_unlock(q);
  367. queuefree(q);
  368. return 0;
  369. }
  370. /*----------------------------------------------------------------*/
  371. /* open timer -
  372. * q->use mutex should be down before calling this function to avoid
  373. * confliction with snd_seq_queue_use()
  374. */
  375. int snd_seq_queue_timer_open(int queueid)
  376. {
  377. int result = 0;
  378. struct snd_seq_queue *queue;
  379. struct snd_seq_timer *tmr;
  380. queue = queueptr(queueid);
  381. if (queue == NULL)
  382. return -EINVAL;
  383. tmr = queue->timer;
  384. if ((result = snd_seq_timer_open(queue)) < 0) {
  385. snd_seq_timer_defaults(tmr);
  386. result = snd_seq_timer_open(queue);
  387. }
  388. queuefree(queue);
  389. return result;
  390. }
  391. /* close timer -
  392. * q->use mutex should be down before calling this function
  393. */
  394. int snd_seq_queue_timer_close(int queueid)
  395. {
  396. struct snd_seq_queue *queue;
  397. int result = 0;
  398. queue = queueptr(queueid);
  399. if (queue == NULL)
  400. return -EINVAL;
  401. snd_seq_timer_close(queue);
  402. queuefree(queue);
  403. return result;
  404. }
  405. /* change queue tempo and ppq */
  406. int snd_seq_queue_timer_set_tempo(int queueid, int client,
  407. struct snd_seq_queue_tempo *info)
  408. {
  409. struct snd_seq_queue *q = queueptr(queueid);
  410. int result;
  411. if (q == NULL)
  412. return -EINVAL;
  413. if (! queue_access_lock(q, client)) {
  414. queuefree(q);
  415. return -EPERM;
  416. }
  417. result = snd_seq_timer_set_tempo(q->timer, info->tempo);
  418. if (result >= 0)
  419. result = snd_seq_timer_set_ppq(q->timer, info->ppq);
  420. if (result >= 0 && info->skew_base > 0)
  421. result = snd_seq_timer_set_skew(q->timer, info->skew_value,
  422. info->skew_base);
  423. queue_access_unlock(q);
  424. queuefree(q);
  425. return result;
  426. }
  427. /* use or unuse this queue -
  428. * if it is the first client, starts the timer.
  429. * if it is not longer used by any clients, stop the timer.
  430. */
  431. int snd_seq_queue_use(int queueid, int client, int use)
  432. {
  433. struct snd_seq_queue *queue;
  434. queue = queueptr(queueid);
  435. if (queue == NULL)
  436. return -EINVAL;
  437. mutex_lock(&queue->timer_mutex);
  438. if (use) {
  439. if (!test_and_set_bit(client, queue->clients_bitmap))
  440. queue->clients++;
  441. } else {
  442. if (test_and_clear_bit(client, queue->clients_bitmap))
  443. queue->clients--;
  444. }
  445. if (queue->clients) {
  446. if (use && queue->clients == 1)
  447. snd_seq_timer_defaults(queue->timer);
  448. snd_seq_timer_open(queue);
  449. } else {
  450. snd_seq_timer_close(queue);
  451. }
  452. mutex_unlock(&queue->timer_mutex);
  453. queuefree(queue);
  454. return 0;
  455. }
  456. /*
  457. * check if queue is used by the client
  458. * return negative value if the queue is invalid.
  459. * return 0 if not used, 1 if used.
  460. */
  461. int snd_seq_queue_is_used(int queueid, int client)
  462. {
  463. struct snd_seq_queue *q;
  464. int result;
  465. q = queueptr(queueid);
  466. if (q == NULL)
  467. return -EINVAL; /* invalid queue */
  468. result = test_bit(client, q->clients_bitmap) ? 1 : 0;
  469. queuefree(q);
  470. return result;
  471. }
  472. /*----------------------------------------------------------------*/
  473. /* notification that client has left the system -
  474. * stop the timer on all queues owned by this client
  475. */
  476. void snd_seq_queue_client_termination(int client)
  477. {
  478. unsigned long flags;
  479. int i;
  480. struct snd_seq_queue *q;
  481. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  482. if ((q = queueptr(i)) == NULL)
  483. continue;
  484. spin_lock_irqsave(&q->owner_lock, flags);
  485. if (q->owner == client)
  486. q->klocked = 1;
  487. spin_unlock_irqrestore(&q->owner_lock, flags);
  488. if (q->owner == client) {
  489. if (q->timer->running)
  490. snd_seq_timer_stop(q->timer);
  491. snd_seq_timer_reset(q->timer);
  492. }
  493. queuefree(q);
  494. }
  495. }
  496. /* final stage notification -
  497. * remove cells for no longer exist client (for non-owned queue)
  498. * or delete this queue (for owned queue)
  499. */
  500. void snd_seq_queue_client_leave(int client)
  501. {
  502. int i;
  503. struct snd_seq_queue *q;
  504. /* delete own queues from queue list */
  505. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  506. if ((q = queue_list_remove(i, client)) != NULL)
  507. queue_delete(q);
  508. }
  509. /* remove cells from existing queues -
  510. * they are not owned by this client
  511. */
  512. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  513. if ((q = queueptr(i)) == NULL)
  514. continue;
  515. if (test_bit(client, q->clients_bitmap)) {
  516. snd_seq_prioq_leave(q->tickq, client, 0);
  517. snd_seq_prioq_leave(q->timeq, client, 0);
  518. snd_seq_queue_use(q->queue, client, 0);
  519. }
  520. queuefree(q);
  521. }
  522. }
  523. /*----------------------------------------------------------------*/
  524. /* remove cells from all queues */
  525. void snd_seq_queue_client_leave_cells(int client)
  526. {
  527. int i;
  528. struct snd_seq_queue *q;
  529. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  530. if ((q = queueptr(i)) == NULL)
  531. continue;
  532. snd_seq_prioq_leave(q->tickq, client, 0);
  533. snd_seq_prioq_leave(q->timeq, client, 0);
  534. queuefree(q);
  535. }
  536. }
  537. /* remove cells based on flush criteria */
  538. void snd_seq_queue_remove_cells(int client, struct snd_seq_remove_events *info)
  539. {
  540. int i;
  541. struct snd_seq_queue *q;
  542. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  543. if ((q = queueptr(i)) == NULL)
  544. continue;
  545. if (test_bit(client, q->clients_bitmap) &&
  546. (! (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) ||
  547. q->queue == info->queue)) {
  548. snd_seq_prioq_remove_events(q->tickq, client, info);
  549. snd_seq_prioq_remove_events(q->timeq, client, info);
  550. }
  551. queuefree(q);
  552. }
  553. }
  554. /*----------------------------------------------------------------*/
  555. /*
  556. * send events to all subscribed ports
  557. */
  558. static void queue_broadcast_event(struct snd_seq_queue *q, struct snd_seq_event *ev,
  559. int atomic, int hop)
  560. {
  561. struct snd_seq_event sev;
  562. sev = *ev;
  563. sev.flags = SNDRV_SEQ_TIME_STAMP_TICK|SNDRV_SEQ_TIME_MODE_ABS;
  564. sev.time.tick = q->timer->tick.cur_tick;
  565. sev.queue = q->queue;
  566. sev.data.queue.queue = q->queue;
  567. /* broadcast events from Timer port */
  568. sev.source.client = SNDRV_SEQ_CLIENT_SYSTEM;
  569. sev.source.port = SNDRV_SEQ_PORT_SYSTEM_TIMER;
  570. sev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  571. snd_seq_kernel_client_dispatch(SNDRV_SEQ_CLIENT_SYSTEM, &sev, atomic, hop);
  572. }
  573. /*
  574. * process a received queue-control event.
  575. * this function is exported for seq_sync.c.
  576. */
  577. static void snd_seq_queue_process_event(struct snd_seq_queue *q,
  578. struct snd_seq_event *ev,
  579. int atomic, int hop)
  580. {
  581. switch (ev->type) {
  582. case SNDRV_SEQ_EVENT_START:
  583. snd_seq_prioq_leave(q->tickq, ev->source.client, 1);
  584. snd_seq_prioq_leave(q->timeq, ev->source.client, 1);
  585. if (! snd_seq_timer_start(q->timer))
  586. queue_broadcast_event(q, ev, atomic, hop);
  587. break;
  588. case SNDRV_SEQ_EVENT_CONTINUE:
  589. if (! snd_seq_timer_continue(q->timer))
  590. queue_broadcast_event(q, ev, atomic, hop);
  591. break;
  592. case SNDRV_SEQ_EVENT_STOP:
  593. snd_seq_timer_stop(q->timer);
  594. queue_broadcast_event(q, ev, atomic, hop);
  595. break;
  596. case SNDRV_SEQ_EVENT_TEMPO:
  597. snd_seq_timer_set_tempo(q->timer, ev->data.queue.param.value);
  598. queue_broadcast_event(q, ev, atomic, hop);
  599. break;
  600. case SNDRV_SEQ_EVENT_SETPOS_TICK:
  601. if (snd_seq_timer_set_position_tick(q->timer, ev->data.queue.param.time.tick) == 0) {
  602. queue_broadcast_event(q, ev, atomic, hop);
  603. }
  604. break;
  605. case SNDRV_SEQ_EVENT_SETPOS_TIME:
  606. if (snd_seq_timer_set_position_time(q->timer, ev->data.queue.param.time.time) == 0) {
  607. queue_broadcast_event(q, ev, atomic, hop);
  608. }
  609. break;
  610. case SNDRV_SEQ_EVENT_QUEUE_SKEW:
  611. if (snd_seq_timer_set_skew(q->timer,
  612. ev->data.queue.param.skew.value,
  613. ev->data.queue.param.skew.base) == 0) {
  614. queue_broadcast_event(q, ev, atomic, hop);
  615. }
  616. break;
  617. }
  618. }
  619. /*
  620. * Queue control via timer control port:
  621. * this function is exported as a callback of timer port.
  622. */
  623. int snd_seq_control_queue(struct snd_seq_event *ev, int atomic, int hop)
  624. {
  625. struct snd_seq_queue *q;
  626. if (snd_BUG_ON(!ev))
  627. return -EINVAL;
  628. q = queueptr(ev->data.queue.queue);
  629. if (q == NULL)
  630. return -EINVAL;
  631. if (! queue_access_lock(q, ev->source.client)) {
  632. queuefree(q);
  633. return -EPERM;
  634. }
  635. snd_seq_queue_process_event(q, ev, atomic, hop);
  636. queue_access_unlock(q);
  637. queuefree(q);
  638. return 0;
  639. }
  640. /*----------------------------------------------------------------*/
  641. #ifdef CONFIG_SND_PROC_FS
  642. /* exported to seq_info.c */
  643. void snd_seq_info_queues_read(struct snd_info_entry *entry,
  644. struct snd_info_buffer *buffer)
  645. {
  646. int i, bpm;
  647. struct snd_seq_queue *q;
  648. struct snd_seq_timer *tmr;
  649. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  650. if ((q = queueptr(i)) == NULL)
  651. continue;
  652. tmr = q->timer;
  653. if (tmr->tempo)
  654. bpm = 60000000 / tmr->tempo;
  655. else
  656. bpm = 0;
  657. snd_iprintf(buffer, "queue %d: [%s]\n", q->queue, q->name);
  658. snd_iprintf(buffer, "owned by client : %d\n", q->owner);
  659. snd_iprintf(buffer, "lock status : %s\n", q->locked ? "Locked" : "Free");
  660. snd_iprintf(buffer, "queued time events : %d\n", snd_seq_prioq_avail(q->timeq));
  661. snd_iprintf(buffer, "queued tick events : %d\n", snd_seq_prioq_avail(q->tickq));
  662. snd_iprintf(buffer, "timer state : %s\n", tmr->running ? "Running" : "Stopped");
  663. snd_iprintf(buffer, "timer PPQ : %d\n", tmr->ppq);
  664. snd_iprintf(buffer, "current tempo : %d\n", tmr->tempo);
  665. snd_iprintf(buffer, "current BPM : %d\n", bpm);
  666. snd_iprintf(buffer, "current time : %d.%09d s\n", tmr->cur_time.tv_sec, tmr->cur_time.tv_nsec);
  667. snd_iprintf(buffer, "current tick : %d\n", tmr->tick.cur_tick);
  668. snd_iprintf(buffer, "\n");
  669. queuefree(q);
  670. }
  671. }
  672. #endif /* CONFIG_SND_PROC_FS */