seq_queue.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. * ALSA sequencer Timing queue handling
  3. * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * MAJOR CHANGES
  20. * Nov. 13, 1999 Takashi Iwai <iwai@ww.uni-erlangen.de>
  21. * - Queues are allocated dynamically via ioctl.
  22. * - When owner client is deleted, all owned queues are deleted, too.
  23. * - Owner of unlocked queue is kept unmodified even if it is
  24. * manipulated by other clients.
  25. * - Owner field in SET_QUEUE_OWNER ioctl must be identical with the
  26. * caller client. i.e. Changing owner to a third client is not
  27. * allowed.
  28. *
  29. * Aug. 30, 2000 Takashi Iwai
  30. * - Queues are managed in static array again, but with better way.
  31. * The API itself is identical.
  32. * - The queue is locked when struct snd_seq_queue pointer is returned via
  33. * queueptr(). This pointer *MUST* be released afterward by
  34. * queuefree(ptr).
  35. * - Addition of experimental sync support.
  36. */
  37. #include <linux/init.h>
  38. #include <linux/slab.h>
  39. #include <sound/core.h>
  40. #include "seq_memory.h"
  41. #include "seq_queue.h"
  42. #include "seq_clientmgr.h"
  43. #include "seq_fifo.h"
  44. #include "seq_timer.h"
  45. #include "seq_info.h"
  46. /* list of allocated queues */
  47. static struct snd_seq_queue *queue_list[SNDRV_SEQ_MAX_QUEUES];
  48. static DEFINE_SPINLOCK(queue_list_lock);
  49. /* number of queues allocated */
  50. static int num_queues;
  51. int snd_seq_queue_get_cur_queues(void)
  52. {
  53. return num_queues;
  54. }
  55. /*----------------------------------------------------------------*/
  56. /* assign queue id and insert to list */
  57. static int queue_list_add(struct snd_seq_queue *q)
  58. {
  59. int i;
  60. unsigned long flags;
  61. spin_lock_irqsave(&queue_list_lock, flags);
  62. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  63. if (! queue_list[i]) {
  64. queue_list[i] = q;
  65. q->queue = i;
  66. num_queues++;
  67. spin_unlock_irqrestore(&queue_list_lock, flags);
  68. return i;
  69. }
  70. }
  71. spin_unlock_irqrestore(&queue_list_lock, flags);
  72. return -1;
  73. }
  74. static struct snd_seq_queue *queue_list_remove(int id, int client)
  75. {
  76. struct snd_seq_queue *q;
  77. unsigned long flags;
  78. spin_lock_irqsave(&queue_list_lock, flags);
  79. q = queue_list[id];
  80. if (q) {
  81. spin_lock(&q->owner_lock);
  82. if (q->owner == client) {
  83. /* found */
  84. q->klocked = 1;
  85. spin_unlock(&q->owner_lock);
  86. queue_list[id] = NULL;
  87. num_queues--;
  88. spin_unlock_irqrestore(&queue_list_lock, flags);
  89. return q;
  90. }
  91. spin_unlock(&q->owner_lock);
  92. }
  93. spin_unlock_irqrestore(&queue_list_lock, flags);
  94. return NULL;
  95. }
  96. /*----------------------------------------------------------------*/
  97. /* create new queue (constructor) */
  98. static struct snd_seq_queue *queue_new(int owner, int locked)
  99. {
  100. struct snd_seq_queue *q;
  101. q = kzalloc(sizeof(*q), GFP_KERNEL);
  102. if (q == 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. if (snd_BUG_ON(!cell))
  274. return -EINVAL;
  275. dest = cell->event.queue; /* destination queue */
  276. q = queueptr(dest);
  277. if (q == NULL)
  278. return -EINVAL;
  279. /* handle relative time stamps, convert them into absolute */
  280. if ((cell->event.flags & SNDRV_SEQ_TIME_MODE_MASK) == SNDRV_SEQ_TIME_MODE_REL) {
  281. switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
  282. case SNDRV_SEQ_TIME_STAMP_TICK:
  283. cell->event.time.tick += q->timer->tick.cur_tick;
  284. break;
  285. case SNDRV_SEQ_TIME_STAMP_REAL:
  286. snd_seq_inc_real_time(&cell->event.time.time,
  287. &q->timer->cur_time);
  288. break;
  289. }
  290. cell->event.flags &= ~SNDRV_SEQ_TIME_MODE_MASK;
  291. cell->event.flags |= SNDRV_SEQ_TIME_MODE_ABS;
  292. }
  293. /* enqueue event in the real-time or midi queue */
  294. switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
  295. case SNDRV_SEQ_TIME_STAMP_TICK:
  296. err = snd_seq_prioq_cell_in(q->tickq, cell);
  297. break;
  298. case SNDRV_SEQ_TIME_STAMP_REAL:
  299. default:
  300. err = snd_seq_prioq_cell_in(q->timeq, cell);
  301. break;
  302. }
  303. if (err < 0) {
  304. queuefree(q); /* unlock */
  305. return err;
  306. }
  307. /* trigger dispatching */
  308. snd_seq_check_queue(q, atomic, hop);
  309. queuefree(q); /* unlock */
  310. return 0;
  311. }
  312. /*----------------------------------------------------------------*/
  313. static inline int check_access(struct snd_seq_queue *q, int client)
  314. {
  315. return (q->owner == client) || (!q->locked && !q->klocked);
  316. }
  317. /* check if the client has permission to modify queue parameters.
  318. * if it does, lock the queue
  319. */
  320. static int queue_access_lock(struct snd_seq_queue *q, int client)
  321. {
  322. unsigned long flags;
  323. int access_ok;
  324. spin_lock_irqsave(&q->owner_lock, flags);
  325. access_ok = check_access(q, client);
  326. if (access_ok)
  327. q->klocked = 1;
  328. spin_unlock_irqrestore(&q->owner_lock, flags);
  329. return access_ok;
  330. }
  331. /* unlock the queue */
  332. static inline void queue_access_unlock(struct snd_seq_queue *q)
  333. {
  334. unsigned long flags;
  335. spin_lock_irqsave(&q->owner_lock, flags);
  336. q->klocked = 0;
  337. spin_unlock_irqrestore(&q->owner_lock, flags);
  338. }
  339. /* exported - only checking permission */
  340. int snd_seq_queue_check_access(int queueid, int client)
  341. {
  342. struct snd_seq_queue *q = queueptr(queueid);
  343. int access_ok;
  344. unsigned long flags;
  345. if (! q)
  346. return 0;
  347. spin_lock_irqsave(&q->owner_lock, flags);
  348. access_ok = check_access(q, client);
  349. spin_unlock_irqrestore(&q->owner_lock, flags);
  350. queuefree(q);
  351. return access_ok;
  352. }
  353. /*----------------------------------------------------------------*/
  354. /*
  355. * change queue's owner and permission
  356. */
  357. int snd_seq_queue_set_owner(int queueid, int client, int locked)
  358. {
  359. struct snd_seq_queue *q = queueptr(queueid);
  360. if (q == NULL)
  361. return -EINVAL;
  362. if (! queue_access_lock(q, client)) {
  363. queuefree(q);
  364. return -EPERM;
  365. }
  366. q->locked = locked ? 1 : 0;
  367. q->owner = client;
  368. queue_access_unlock(q);
  369. queuefree(q);
  370. return 0;
  371. }
  372. /*----------------------------------------------------------------*/
  373. /* open timer -
  374. * q->use mutex should be down before calling this function to avoid
  375. * confliction with snd_seq_queue_use()
  376. */
  377. int snd_seq_queue_timer_open(int queueid)
  378. {
  379. int result = 0;
  380. struct snd_seq_queue *queue;
  381. struct snd_seq_timer *tmr;
  382. queue = queueptr(queueid);
  383. if (queue == NULL)
  384. return -EINVAL;
  385. tmr = queue->timer;
  386. if ((result = snd_seq_timer_open(queue)) < 0) {
  387. snd_seq_timer_defaults(tmr);
  388. result = snd_seq_timer_open(queue);
  389. }
  390. queuefree(queue);
  391. return result;
  392. }
  393. /* close timer -
  394. * q->use mutex should be down before calling this function
  395. */
  396. int snd_seq_queue_timer_close(int queueid)
  397. {
  398. struct snd_seq_queue *queue;
  399. int result = 0;
  400. queue = queueptr(queueid);
  401. if (queue == NULL)
  402. return -EINVAL;
  403. snd_seq_timer_close(queue);
  404. queuefree(queue);
  405. return result;
  406. }
  407. /* change queue tempo and ppq */
  408. int snd_seq_queue_timer_set_tempo(int queueid, int client,
  409. struct snd_seq_queue_tempo *info)
  410. {
  411. struct snd_seq_queue *q = queueptr(queueid);
  412. int result;
  413. if (q == NULL)
  414. return -EINVAL;
  415. if (! queue_access_lock(q, client)) {
  416. queuefree(q);
  417. return -EPERM;
  418. }
  419. result = snd_seq_timer_set_tempo(q->timer, info->tempo);
  420. if (result >= 0)
  421. result = snd_seq_timer_set_ppq(q->timer, info->ppq);
  422. if (result >= 0 && info->skew_base > 0)
  423. result = snd_seq_timer_set_skew(q->timer, info->skew_value,
  424. info->skew_base);
  425. queue_access_unlock(q);
  426. queuefree(q);
  427. return result;
  428. }
  429. /* use or unuse this queue -
  430. * if it is the first client, starts the timer.
  431. * if it is not longer used by any clients, stop the timer.
  432. */
  433. int snd_seq_queue_use(int queueid, int client, int use)
  434. {
  435. struct snd_seq_queue *queue;
  436. queue = queueptr(queueid);
  437. if (queue == NULL)
  438. return -EINVAL;
  439. mutex_lock(&queue->timer_mutex);
  440. if (use) {
  441. if (!test_and_set_bit(client, queue->clients_bitmap))
  442. queue->clients++;
  443. } else {
  444. if (test_and_clear_bit(client, queue->clients_bitmap))
  445. queue->clients--;
  446. }
  447. if (queue->clients) {
  448. if (use && queue->clients == 1)
  449. snd_seq_timer_defaults(queue->timer);
  450. snd_seq_timer_open(queue);
  451. } else {
  452. snd_seq_timer_close(queue);
  453. }
  454. mutex_unlock(&queue->timer_mutex);
  455. queuefree(queue);
  456. return 0;
  457. }
  458. /*
  459. * check if queue is used by the client
  460. * return negative value if the queue is invalid.
  461. * return 0 if not used, 1 if used.
  462. */
  463. int snd_seq_queue_is_used(int queueid, int client)
  464. {
  465. struct snd_seq_queue *q;
  466. int result;
  467. q = queueptr(queueid);
  468. if (q == NULL)
  469. return -EINVAL; /* invalid queue */
  470. result = test_bit(client, q->clients_bitmap) ? 1 : 0;
  471. queuefree(q);
  472. return result;
  473. }
  474. /*----------------------------------------------------------------*/
  475. /* notification that client has left the system -
  476. * stop the timer on all queues owned by this client
  477. */
  478. void snd_seq_queue_client_termination(int client)
  479. {
  480. unsigned long flags;
  481. int i;
  482. struct snd_seq_queue *q;
  483. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  484. if ((q = queueptr(i)) == NULL)
  485. continue;
  486. spin_lock_irqsave(&q->owner_lock, flags);
  487. if (q->owner == client)
  488. q->klocked = 1;
  489. spin_unlock_irqrestore(&q->owner_lock, flags);
  490. if (q->owner == client) {
  491. if (q->timer->running)
  492. snd_seq_timer_stop(q->timer);
  493. snd_seq_timer_reset(q->timer);
  494. }
  495. queuefree(q);
  496. }
  497. }
  498. /* final stage notification -
  499. * remove cells for no longer exist client (for non-owned queue)
  500. * or delete this queue (for owned queue)
  501. */
  502. void snd_seq_queue_client_leave(int client)
  503. {
  504. int i;
  505. struct snd_seq_queue *q;
  506. /* delete own queues from queue list */
  507. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  508. if ((q = queue_list_remove(i, client)) != NULL)
  509. queue_delete(q);
  510. }
  511. /* remove cells from existing queues -
  512. * they are not owned by this client
  513. */
  514. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  515. if ((q = queueptr(i)) == NULL)
  516. continue;
  517. if (test_bit(client, q->clients_bitmap)) {
  518. snd_seq_prioq_leave(q->tickq, client, 0);
  519. snd_seq_prioq_leave(q->timeq, client, 0);
  520. snd_seq_queue_use(q->queue, client, 0);
  521. }
  522. queuefree(q);
  523. }
  524. }
  525. /*----------------------------------------------------------------*/
  526. /* remove cells from all queues */
  527. void snd_seq_queue_client_leave_cells(int client)
  528. {
  529. int i;
  530. struct snd_seq_queue *q;
  531. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  532. if ((q = queueptr(i)) == NULL)
  533. continue;
  534. snd_seq_prioq_leave(q->tickq, client, 0);
  535. snd_seq_prioq_leave(q->timeq, client, 0);
  536. queuefree(q);
  537. }
  538. }
  539. /* remove cells based on flush criteria */
  540. void snd_seq_queue_remove_cells(int client, struct snd_seq_remove_events *info)
  541. {
  542. int i;
  543. struct snd_seq_queue *q;
  544. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  545. if ((q = queueptr(i)) == NULL)
  546. continue;
  547. if (test_bit(client, q->clients_bitmap) &&
  548. (! (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) ||
  549. q->queue == info->queue)) {
  550. snd_seq_prioq_remove_events(q->tickq, client, info);
  551. snd_seq_prioq_remove_events(q->timeq, client, info);
  552. }
  553. queuefree(q);
  554. }
  555. }
  556. /*----------------------------------------------------------------*/
  557. /*
  558. * send events to all subscribed ports
  559. */
  560. static void queue_broadcast_event(struct snd_seq_queue *q, struct snd_seq_event *ev,
  561. int atomic, int hop)
  562. {
  563. struct snd_seq_event sev;
  564. sev = *ev;
  565. sev.flags = SNDRV_SEQ_TIME_STAMP_TICK|SNDRV_SEQ_TIME_MODE_ABS;
  566. sev.time.tick = q->timer->tick.cur_tick;
  567. sev.queue = q->queue;
  568. sev.data.queue.queue = q->queue;
  569. /* broadcast events from Timer port */
  570. sev.source.client = SNDRV_SEQ_CLIENT_SYSTEM;
  571. sev.source.port = SNDRV_SEQ_PORT_SYSTEM_TIMER;
  572. sev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  573. snd_seq_kernel_client_dispatch(SNDRV_SEQ_CLIENT_SYSTEM, &sev, atomic, hop);
  574. }
  575. /*
  576. * process a received queue-control event.
  577. * this function is exported for seq_sync.c.
  578. */
  579. static void snd_seq_queue_process_event(struct snd_seq_queue *q,
  580. struct snd_seq_event *ev,
  581. int atomic, int hop)
  582. {
  583. switch (ev->type) {
  584. case SNDRV_SEQ_EVENT_START:
  585. snd_seq_prioq_leave(q->tickq, ev->source.client, 1);
  586. snd_seq_prioq_leave(q->timeq, ev->source.client, 1);
  587. if (! snd_seq_timer_start(q->timer))
  588. queue_broadcast_event(q, ev, atomic, hop);
  589. break;
  590. case SNDRV_SEQ_EVENT_CONTINUE:
  591. if (! snd_seq_timer_continue(q->timer))
  592. queue_broadcast_event(q, ev, atomic, hop);
  593. break;
  594. case SNDRV_SEQ_EVENT_STOP:
  595. snd_seq_timer_stop(q->timer);
  596. queue_broadcast_event(q, ev, atomic, hop);
  597. break;
  598. case SNDRV_SEQ_EVENT_TEMPO:
  599. snd_seq_timer_set_tempo(q->timer, ev->data.queue.param.value);
  600. queue_broadcast_event(q, ev, atomic, hop);
  601. break;
  602. case SNDRV_SEQ_EVENT_SETPOS_TICK:
  603. if (snd_seq_timer_set_position_tick(q->timer, ev->data.queue.param.time.tick) == 0) {
  604. queue_broadcast_event(q, ev, atomic, hop);
  605. }
  606. break;
  607. case SNDRV_SEQ_EVENT_SETPOS_TIME:
  608. if (snd_seq_timer_set_position_time(q->timer, ev->data.queue.param.time.time) == 0) {
  609. queue_broadcast_event(q, ev, atomic, hop);
  610. }
  611. break;
  612. case SNDRV_SEQ_EVENT_QUEUE_SKEW:
  613. if (snd_seq_timer_set_skew(q->timer,
  614. ev->data.queue.param.skew.value,
  615. ev->data.queue.param.skew.base) == 0) {
  616. queue_broadcast_event(q, ev, atomic, hop);
  617. }
  618. break;
  619. }
  620. }
  621. /*
  622. * Queue control via timer control port:
  623. * this function is exported as a callback of timer port.
  624. */
  625. int snd_seq_control_queue(struct snd_seq_event *ev, int atomic, int hop)
  626. {
  627. struct snd_seq_queue *q;
  628. if (snd_BUG_ON(!ev))
  629. 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 */