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. 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_ppq(q->timer, info->tempo, info->ppq);
  416. if (result >= 0 && info->skew_base > 0)
  417. result = snd_seq_timer_set_skew(q->timer, info->skew_value,
  418. info->skew_base);
  419. queue_access_unlock(q);
  420. queuefree(q);
  421. return result;
  422. }
  423. /* use or unuse this queue */
  424. static void queue_use(struct snd_seq_queue *queue, int client, int use)
  425. {
  426. if (use) {
  427. if (!test_and_set_bit(client, queue->clients_bitmap))
  428. queue->clients++;
  429. } else {
  430. if (test_and_clear_bit(client, queue->clients_bitmap))
  431. queue->clients--;
  432. }
  433. if (queue->clients) {
  434. if (use && queue->clients == 1)
  435. snd_seq_timer_defaults(queue->timer);
  436. snd_seq_timer_open(queue);
  437. } else {
  438. snd_seq_timer_close(queue);
  439. }
  440. }
  441. /* use or unuse this queue -
  442. * if it is the first client, starts the timer.
  443. * if it is not longer used by any clients, stop the timer.
  444. */
  445. int snd_seq_queue_use(int queueid, int client, int use)
  446. {
  447. struct snd_seq_queue *queue;
  448. queue = queueptr(queueid);
  449. if (queue == NULL)
  450. return -EINVAL;
  451. mutex_lock(&queue->timer_mutex);
  452. queue_use(queue, client, use);
  453. mutex_unlock(&queue->timer_mutex);
  454. queuefree(queue);
  455. return 0;
  456. }
  457. /*
  458. * check if queue is used by the client
  459. * return negative value if the queue is invalid.
  460. * return 0 if not used, 1 if used.
  461. */
  462. int snd_seq_queue_is_used(int queueid, int client)
  463. {
  464. struct snd_seq_queue *q;
  465. int result;
  466. q = queueptr(queueid);
  467. if (q == NULL)
  468. return -EINVAL; /* invalid queue */
  469. result = test_bit(client, q->clients_bitmap) ? 1 : 0;
  470. queuefree(q);
  471. return result;
  472. }
  473. /*----------------------------------------------------------------*/
  474. /* notification that client has left the system -
  475. * stop the timer on all queues owned by this client
  476. */
  477. void snd_seq_queue_client_termination(int client)
  478. {
  479. unsigned long flags;
  480. int i;
  481. struct snd_seq_queue *q;
  482. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  483. if ((q = queueptr(i)) == NULL)
  484. continue;
  485. spin_lock_irqsave(&q->owner_lock, flags);
  486. if (q->owner == client)
  487. q->klocked = 1;
  488. spin_unlock_irqrestore(&q->owner_lock, flags);
  489. if (q->owner == client) {
  490. if (q->timer->running)
  491. snd_seq_timer_stop(q->timer);
  492. snd_seq_timer_reset(q->timer);
  493. }
  494. queuefree(q);
  495. }
  496. }
  497. /* final stage notification -
  498. * remove cells for no longer exist client (for non-owned queue)
  499. * or delete this queue (for owned queue)
  500. */
  501. void snd_seq_queue_client_leave(int client)
  502. {
  503. int i;
  504. struct snd_seq_queue *q;
  505. /* delete own queues from queue list */
  506. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  507. if ((q = queue_list_remove(i, client)) != NULL)
  508. queue_delete(q);
  509. }
  510. /* remove cells from existing queues -
  511. * they are not owned by this client
  512. */
  513. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  514. if ((q = queueptr(i)) == NULL)
  515. continue;
  516. if (test_bit(client, q->clients_bitmap)) {
  517. snd_seq_prioq_leave(q->tickq, client, 0);
  518. snd_seq_prioq_leave(q->timeq, client, 0);
  519. snd_seq_queue_use(q->queue, client, 0);
  520. }
  521. queuefree(q);
  522. }
  523. }
  524. /*----------------------------------------------------------------*/
  525. /* remove cells from all queues */
  526. void snd_seq_queue_client_leave_cells(int client)
  527. {
  528. int i;
  529. struct snd_seq_queue *q;
  530. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  531. if ((q = queueptr(i)) == NULL)
  532. continue;
  533. snd_seq_prioq_leave(q->tickq, client, 0);
  534. snd_seq_prioq_leave(q->timeq, client, 0);
  535. queuefree(q);
  536. }
  537. }
  538. /* remove cells based on flush criteria */
  539. void snd_seq_queue_remove_cells(int client, struct snd_seq_remove_events *info)
  540. {
  541. int i;
  542. struct snd_seq_queue *q;
  543. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  544. if ((q = queueptr(i)) == NULL)
  545. continue;
  546. if (test_bit(client, q->clients_bitmap) &&
  547. (! (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) ||
  548. q->queue == info->queue)) {
  549. snd_seq_prioq_remove_events(q->tickq, client, info);
  550. snd_seq_prioq_remove_events(q->timeq, client, info);
  551. }
  552. queuefree(q);
  553. }
  554. }
  555. /*----------------------------------------------------------------*/
  556. /*
  557. * send events to all subscribed ports
  558. */
  559. static void queue_broadcast_event(struct snd_seq_queue *q, struct snd_seq_event *ev,
  560. int atomic, int hop)
  561. {
  562. struct snd_seq_event sev;
  563. sev = *ev;
  564. sev.flags = SNDRV_SEQ_TIME_STAMP_TICK|SNDRV_SEQ_TIME_MODE_ABS;
  565. sev.time.tick = q->timer->tick.cur_tick;
  566. sev.queue = q->queue;
  567. sev.data.queue.queue = q->queue;
  568. /* broadcast events from Timer port */
  569. sev.source.client = SNDRV_SEQ_CLIENT_SYSTEM;
  570. sev.source.port = SNDRV_SEQ_PORT_SYSTEM_TIMER;
  571. sev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  572. snd_seq_kernel_client_dispatch(SNDRV_SEQ_CLIENT_SYSTEM, &sev, atomic, hop);
  573. }
  574. /*
  575. * process a received queue-control event.
  576. * this function is exported for seq_sync.c.
  577. */
  578. static void snd_seq_queue_process_event(struct snd_seq_queue *q,
  579. struct snd_seq_event *ev,
  580. int atomic, int hop)
  581. {
  582. switch (ev->type) {
  583. case SNDRV_SEQ_EVENT_START:
  584. snd_seq_prioq_leave(q->tickq, ev->source.client, 1);
  585. snd_seq_prioq_leave(q->timeq, ev->source.client, 1);
  586. if (! snd_seq_timer_start(q->timer))
  587. queue_broadcast_event(q, ev, atomic, hop);
  588. break;
  589. case SNDRV_SEQ_EVENT_CONTINUE:
  590. if (! snd_seq_timer_continue(q->timer))
  591. queue_broadcast_event(q, ev, atomic, hop);
  592. break;
  593. case SNDRV_SEQ_EVENT_STOP:
  594. snd_seq_timer_stop(q->timer);
  595. queue_broadcast_event(q, ev, atomic, hop);
  596. break;
  597. case SNDRV_SEQ_EVENT_TEMPO:
  598. snd_seq_timer_set_tempo(q->timer, ev->data.queue.param.value);
  599. queue_broadcast_event(q, ev, atomic, hop);
  600. break;
  601. case SNDRV_SEQ_EVENT_SETPOS_TICK:
  602. if (snd_seq_timer_set_position_tick(q->timer, ev->data.queue.param.time.tick) == 0) {
  603. queue_broadcast_event(q, ev, atomic, hop);
  604. }
  605. break;
  606. case SNDRV_SEQ_EVENT_SETPOS_TIME:
  607. if (snd_seq_timer_set_position_time(q->timer, ev->data.queue.param.time.time) == 0) {
  608. queue_broadcast_event(q, ev, atomic, hop);
  609. }
  610. break;
  611. case SNDRV_SEQ_EVENT_QUEUE_SKEW:
  612. if (snd_seq_timer_set_skew(q->timer,
  613. ev->data.queue.param.skew.value,
  614. ev->data.queue.param.skew.base) == 0) {
  615. queue_broadcast_event(q, ev, atomic, hop);
  616. }
  617. break;
  618. }
  619. }
  620. /*
  621. * Queue control via timer control port:
  622. * this function is exported as a callback of timer port.
  623. */
  624. int snd_seq_control_queue(struct snd_seq_event *ev, int atomic, int hop)
  625. {
  626. struct snd_seq_queue *q;
  627. if (snd_BUG_ON(!ev))
  628. return -EINVAL;
  629. q = queueptr(ev->data.queue.queue);
  630. if (q == NULL)
  631. return -EINVAL;
  632. if (! queue_access_lock(q, ev->source.client)) {
  633. queuefree(q);
  634. return -EPERM;
  635. }
  636. snd_seq_queue_process_event(q, ev, atomic, hop);
  637. queue_access_unlock(q);
  638. queuefree(q);
  639. return 0;
  640. }
  641. /*----------------------------------------------------------------*/
  642. #ifdef CONFIG_SND_PROC_FS
  643. /* exported to seq_info.c */
  644. void snd_seq_info_queues_read(struct snd_info_entry *entry,
  645. struct snd_info_buffer *buffer)
  646. {
  647. int i, bpm;
  648. struct snd_seq_queue *q;
  649. struct snd_seq_timer *tmr;
  650. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  651. if ((q = queueptr(i)) == NULL)
  652. continue;
  653. tmr = q->timer;
  654. if (tmr->tempo)
  655. bpm = 60000000 / tmr->tempo;
  656. else
  657. bpm = 0;
  658. snd_iprintf(buffer, "queue %d: [%s]\n", q->queue, q->name);
  659. snd_iprintf(buffer, "owned by client : %d\n", q->owner);
  660. snd_iprintf(buffer, "lock status : %s\n", q->locked ? "Locked" : "Free");
  661. snd_iprintf(buffer, "queued time events : %d\n", snd_seq_prioq_avail(q->timeq));
  662. snd_iprintf(buffer, "queued tick events : %d\n", snd_seq_prioq_avail(q->tickq));
  663. snd_iprintf(buffer, "timer state : %s\n", tmr->running ? "Running" : "Stopped");
  664. snd_iprintf(buffer, "timer PPQ : %d\n", tmr->ppq);
  665. snd_iprintf(buffer, "current tempo : %d\n", tmr->tempo);
  666. snd_iprintf(buffer, "current BPM : %d\n", bpm);
  667. snd_iprintf(buffer, "current time : %d.%09d s\n", tmr->cur_time.tv_sec, tmr->cur_time.tv_nsec);
  668. snd_iprintf(buffer, "current tick : %d\n", tmr->tick.cur_tick);
  669. snd_iprintf(buffer, "\n");
  670. queuefree(q);
  671. }
  672. }
  673. #endif /* CONFIG_SND_PROC_FS */