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 <sound/driver.h>
  38. #include <linux/init.h>
  39. #include <linux/slab.h>
  40. #include <sound/core.h>
  41. #include "seq_memory.h"
  42. #include "seq_queue.h"
  43. #include "seq_clientmgr.h"
  44. #include "seq_fifo.h"
  45. #include "seq_timer.h"
  46. #include "seq_info.h"
  47. /* list of allocated queues */
  48. static struct snd_seq_queue *queue_list[SNDRV_SEQ_MAX_QUEUES];
  49. static DEFINE_SPINLOCK(queue_list_lock);
  50. /* number of queues allocated */
  51. static int num_queues;
  52. int snd_seq_queue_get_cur_queues(void)
  53. {
  54. return num_queues;
  55. }
  56. /*----------------------------------------------------------------*/
  57. /* assign queue id and insert to list */
  58. static int queue_list_add(struct snd_seq_queue *q)
  59. {
  60. int i;
  61. unsigned long flags;
  62. spin_lock_irqsave(&queue_list_lock, flags);
  63. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  64. if (! queue_list[i]) {
  65. queue_list[i] = q;
  66. q->queue = i;
  67. num_queues++;
  68. spin_unlock_irqrestore(&queue_list_lock, flags);
  69. return i;
  70. }
  71. }
  72. spin_unlock_irqrestore(&queue_list_lock, flags);
  73. return -1;
  74. }
  75. static struct snd_seq_queue *queue_list_remove(int id, int client)
  76. {
  77. struct snd_seq_queue *q;
  78. unsigned long flags;
  79. spin_lock_irqsave(&queue_list_lock, flags);
  80. q = queue_list[id];
  81. if (q) {
  82. spin_lock(&q->owner_lock);
  83. if (q->owner == client) {
  84. /* found */
  85. q->klocked = 1;
  86. spin_unlock(&q->owner_lock);
  87. queue_list[id] = NULL;
  88. num_queues--;
  89. spin_unlock_irqrestore(&queue_list_lock, flags);
  90. return q;
  91. }
  92. spin_unlock(&q->owner_lock);
  93. }
  94. spin_unlock_irqrestore(&queue_list_lock, flags);
  95. return NULL;
  96. }
  97. /*----------------------------------------------------------------*/
  98. /* create new queue (constructor) */
  99. static struct snd_seq_queue *queue_new(int owner, int locked)
  100. {
  101. struct snd_seq_queue *q;
  102. q = kzalloc(sizeof(*q), GFP_KERNEL);
  103. if (q == NULL) {
  104. snd_printd("malloc failed for snd_seq_queue_new()\n");
  105. return NULL;
  106. }
  107. spin_lock_init(&q->owner_lock);
  108. spin_lock_init(&q->check_lock);
  109. mutex_init(&q->timer_mutex);
  110. snd_use_lock_init(&q->use_lock);
  111. q->queue = -1;
  112. q->tickq = snd_seq_prioq_new();
  113. q->timeq = snd_seq_prioq_new();
  114. q->timer = snd_seq_timer_new();
  115. if (q->tickq == NULL || q->timeq == NULL || q->timer == NULL) {
  116. snd_seq_prioq_delete(&q->tickq);
  117. snd_seq_prioq_delete(&q->timeq);
  118. snd_seq_timer_delete(&q->timer);
  119. kfree(q);
  120. return NULL;
  121. }
  122. q->owner = owner;
  123. q->locked = locked;
  124. q->klocked = 0;
  125. return q;
  126. }
  127. /* delete queue (destructor) */
  128. static void queue_delete(struct snd_seq_queue *q)
  129. {
  130. /* stop and release the timer */
  131. snd_seq_timer_stop(q->timer);
  132. snd_seq_timer_close(q);
  133. /* wait until access free */
  134. snd_use_lock_sync(&q->use_lock);
  135. /* release resources... */
  136. snd_seq_prioq_delete(&q->tickq);
  137. snd_seq_prioq_delete(&q->timeq);
  138. snd_seq_timer_delete(&q->timer);
  139. kfree(q);
  140. }
  141. /*----------------------------------------------------------------*/
  142. /* setup queues */
  143. int __init snd_seq_queues_init(void)
  144. {
  145. /*
  146. memset(queue_list, 0, sizeof(queue_list));
  147. num_queues = 0;
  148. */
  149. return 0;
  150. }
  151. /* delete all existing queues */
  152. void __exit snd_seq_queues_delete(void)
  153. {
  154. int i;
  155. /* clear list */
  156. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  157. if (queue_list[i])
  158. queue_delete(queue_list[i]);
  159. }
  160. }
  161. /* allocate a new queue -
  162. * return queue index value or negative value for error
  163. */
  164. int snd_seq_queue_alloc(int client, int locked, unsigned int info_flags)
  165. {
  166. struct snd_seq_queue *q;
  167. q = queue_new(client, locked);
  168. if (q == NULL)
  169. return -ENOMEM;
  170. q->info_flags = info_flags;
  171. if (queue_list_add(q) < 0) {
  172. queue_delete(q);
  173. return -ENOMEM;
  174. }
  175. snd_seq_queue_use(q->queue, client, 1); /* use this queue */
  176. return q->queue;
  177. }
  178. /* delete a queue - queue must be owned by the client */
  179. int snd_seq_queue_delete(int client, int queueid)
  180. {
  181. struct snd_seq_queue *q;
  182. if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES)
  183. return -EINVAL;
  184. q = queue_list_remove(queueid, client);
  185. if (q == NULL)
  186. return -EINVAL;
  187. queue_delete(q);
  188. return 0;
  189. }
  190. /* return pointer to queue structure for specified id */
  191. struct snd_seq_queue *queueptr(int queueid)
  192. {
  193. struct snd_seq_queue *q;
  194. unsigned long flags;
  195. if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES)
  196. return NULL;
  197. spin_lock_irqsave(&queue_list_lock, flags);
  198. q = queue_list[queueid];
  199. if (q)
  200. snd_use_lock_use(&q->use_lock);
  201. spin_unlock_irqrestore(&queue_list_lock, flags);
  202. return q;
  203. }
  204. /* return the (first) queue matching with the specified name */
  205. struct snd_seq_queue *snd_seq_queue_find_name(char *name)
  206. {
  207. int i;
  208. struct snd_seq_queue *q;
  209. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  210. if ((q = queueptr(i)) != NULL) {
  211. if (strncmp(q->name, name, sizeof(q->name)) == 0)
  212. return q;
  213. queuefree(q);
  214. }
  215. }
  216. return NULL;
  217. }
  218. /* -------------------------------------------------------- */
  219. void snd_seq_check_queue(struct snd_seq_queue *q, int atomic, int hop)
  220. {
  221. unsigned long flags;
  222. struct snd_seq_event_cell *cell;
  223. if (q == NULL)
  224. return;
  225. /* make this function non-reentrant */
  226. spin_lock_irqsave(&q->check_lock, flags);
  227. if (q->check_blocked) {
  228. q->check_again = 1;
  229. spin_unlock_irqrestore(&q->check_lock, flags);
  230. return; /* other thread is already checking queues */
  231. }
  232. q->check_blocked = 1;
  233. spin_unlock_irqrestore(&q->check_lock, flags);
  234. __again:
  235. /* Process tick queue... */
  236. while ((cell = snd_seq_prioq_cell_peek(q->tickq)) != NULL) {
  237. if (snd_seq_compare_tick_time(&q->timer->tick.cur_tick,
  238. &cell->event.time.tick)) {
  239. cell = snd_seq_prioq_cell_out(q->tickq);
  240. if (cell)
  241. snd_seq_dispatch_event(cell, atomic, hop);
  242. } else {
  243. /* event remains in the queue */
  244. break;
  245. }
  246. }
  247. /* Process time queue... */
  248. while ((cell = snd_seq_prioq_cell_peek(q->timeq)) != NULL) {
  249. if (snd_seq_compare_real_time(&q->timer->cur_time,
  250. &cell->event.time.time)) {
  251. cell = snd_seq_prioq_cell_out(q->timeq);
  252. if (cell)
  253. snd_seq_dispatch_event(cell, atomic, hop);
  254. } else {
  255. /* event remains in the queue */
  256. break;
  257. }
  258. }
  259. /* free lock */
  260. spin_lock_irqsave(&q->check_lock, flags);
  261. if (q->check_again) {
  262. q->check_again = 0;
  263. spin_unlock_irqrestore(&q->check_lock, flags);
  264. goto __again;
  265. }
  266. q->check_blocked = 0;
  267. spin_unlock_irqrestore(&q->check_lock, flags);
  268. }
  269. /* enqueue a event to singe queue */
  270. int snd_seq_enqueue_event(struct snd_seq_event_cell *cell, int atomic, int hop)
  271. {
  272. int dest, err;
  273. struct snd_seq_queue *q;
  274. snd_assert(cell != NULL, 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. struct snd_seq_timer *tmr;
  400. int result = 0;
  401. queue = queueptr(queueid);
  402. if (queue == NULL)
  403. return -EINVAL;
  404. tmr = queue->timer;
  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. snd_assert(ev != NULL, return -EINVAL);
  631. q = queueptr(ev->data.queue.queue);
  632. if (q == NULL)
  633. return -EINVAL;
  634. if (! queue_access_lock(q, ev->source.client)) {
  635. queuefree(q);
  636. return -EPERM;
  637. }
  638. snd_seq_queue_process_event(q, ev, atomic, hop);
  639. queue_access_unlock(q);
  640. queuefree(q);
  641. return 0;
  642. }
  643. /*----------------------------------------------------------------*/
  644. #ifdef CONFIG_PROC_FS
  645. /* exported to seq_info.c */
  646. void snd_seq_info_queues_read(struct snd_info_entry *entry,
  647. struct snd_info_buffer *buffer)
  648. {
  649. int i, bpm;
  650. struct snd_seq_queue *q;
  651. struct snd_seq_timer *tmr;
  652. for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
  653. if ((q = queueptr(i)) == NULL)
  654. continue;
  655. tmr = q->timer;
  656. if (tmr->tempo)
  657. bpm = 60000000 / tmr->tempo;
  658. else
  659. bpm = 0;
  660. snd_iprintf(buffer, "queue %d: [%s]\n", q->queue, q->name);
  661. snd_iprintf(buffer, "owned by client : %d\n", q->owner);
  662. snd_iprintf(buffer, "lock status : %s\n", q->locked ? "Locked" : "Free");
  663. snd_iprintf(buffer, "queued time events : %d\n", snd_seq_prioq_avail(q->timeq));
  664. snd_iprintf(buffer, "queued tick events : %d\n", snd_seq_prioq_avail(q->tickq));
  665. snd_iprintf(buffer, "timer state : %s\n", tmr->running ? "Running" : "Stopped");
  666. snd_iprintf(buffer, "timer PPQ : %d\n", tmr->ppq);
  667. snd_iprintf(buffer, "current tempo : %d\n", tmr->tempo);
  668. snd_iprintf(buffer, "current BPM : %d\n", bpm);
  669. snd_iprintf(buffer, "current time : %d.%09d s\n", tmr->cur_time.tv_sec, tmr->cur_time.tv_nsec);
  670. snd_iprintf(buffer, "current tick : %d\n", tmr->tick.cur_tick);
  671. snd_iprintf(buffer, "\n");
  672. queuefree(q);
  673. }
  674. }
  675. #endif /* CONFIG_PROC_FS */