seq_queue.c 19 KB

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