2 * ALSA sequencer Timing queue handling
3 * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
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.
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.
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
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
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 queue_t pinter is returned via
33 * queueptr(). This pointer *MUST* be released afterward by
35 * - Addition of experimental sync support.
38 #include <sound/driver.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <sound/core.h>
43 #include "seq_memory.h"
44 #include "seq_queue.h"
45 #include "seq_clientmgr.h"
47 #include "seq_timer.h"
50 /* list of allocated queues */
51 static queue_t
*queue_list
[SNDRV_SEQ_MAX_QUEUES
];
52 static DEFINE_SPINLOCK(queue_list_lock
);
53 /* number of queues allocated */
54 static int num_queues
;
56 int snd_seq_queue_get_cur_queues(void)
61 /*----------------------------------------------------------------*/
63 /* assign queue id and insert to list */
64 static int queue_list_add(queue_t
*q
)
69 spin_lock_irqsave(&queue_list_lock
, flags
);
70 for (i
= 0; i
< SNDRV_SEQ_MAX_QUEUES
; i
++) {
71 if (! queue_list
[i
]) {
75 spin_unlock_irqrestore(&queue_list_lock
, flags
);
79 spin_unlock_irqrestore(&queue_list_lock
, flags
);
83 static queue_t
*queue_list_remove(int id
, int client
)
88 spin_lock_irqsave(&queue_list_lock
, flags
);
91 spin_lock(&q
->owner_lock
);
92 if (q
->owner
== client
) {
95 spin_unlock(&q
->owner_lock
);
96 queue_list
[id
] = NULL
;
98 spin_unlock_irqrestore(&queue_list_lock
, flags
);
101 spin_unlock(&q
->owner_lock
);
103 spin_unlock_irqrestore(&queue_list_lock
, flags
);
107 /*----------------------------------------------------------------*/
109 /* create new queue (constructor) */
110 static queue_t
*queue_new(int owner
, int locked
)
114 q
= kcalloc(1, sizeof(*q
), GFP_KERNEL
);
116 snd_printd("malloc failed for snd_seq_queue_new()\n");
120 spin_lock_init(&q
->owner_lock
);
121 spin_lock_init(&q
->check_lock
);
122 init_MUTEX(&q
->timer_mutex
);
123 snd_use_lock_init(&q
->use_lock
);
126 q
->tickq
= snd_seq_prioq_new();
127 q
->timeq
= snd_seq_prioq_new();
128 q
->timer
= snd_seq_timer_new();
129 if (q
->tickq
== NULL
|| q
->timeq
== NULL
|| q
->timer
== NULL
) {
130 snd_seq_prioq_delete(&q
->tickq
);
131 snd_seq_prioq_delete(&q
->timeq
);
132 snd_seq_timer_delete(&q
->timer
);
144 /* delete queue (destructor) */
145 static void queue_delete(queue_t
*q
)
147 /* stop and release the timer */
148 snd_seq_timer_stop(q
->timer
);
149 snd_seq_timer_close(q
);
150 /* wait until access free */
151 snd_use_lock_sync(&q
->use_lock
);
152 /* release resources... */
153 snd_seq_prioq_delete(&q
->tickq
);
154 snd_seq_prioq_delete(&q
->timeq
);
155 snd_seq_timer_delete(&q
->timer
);
161 /*----------------------------------------------------------------*/
164 int __init
snd_seq_queues_init(void)
167 memset(queue_list, 0, sizeof(queue_list));
173 /* delete all existing queues */
174 void __exit
snd_seq_queues_delete(void)
179 for (i
= 0; i
< SNDRV_SEQ_MAX_QUEUES
; i
++) {
181 queue_delete(queue_list
[i
]);
185 /* allocate a new queue -
186 * return queue index value or negative value for error
188 int snd_seq_queue_alloc(int client
, int locked
, unsigned int info_flags
)
192 q
= queue_new(client
, locked
);
195 q
->info_flags
= info_flags
;
196 if (queue_list_add(q
) < 0) {
200 snd_seq_queue_use(q
->queue
, client
, 1); /* use this queue */
204 /* delete a queue - queue must be owned by the client */
205 int snd_seq_queue_delete(int client
, int queueid
)
209 if (queueid
< 0 || queueid
>= SNDRV_SEQ_MAX_QUEUES
)
211 q
= queue_list_remove(queueid
, client
);
220 /* return pointer to queue structure for specified id */
221 queue_t
*queueptr(int queueid
)
226 if (queueid
< 0 || queueid
>= SNDRV_SEQ_MAX_QUEUES
)
228 spin_lock_irqsave(&queue_list_lock
, flags
);
229 q
= queue_list
[queueid
];
231 snd_use_lock_use(&q
->use_lock
);
232 spin_unlock_irqrestore(&queue_list_lock
, flags
);
236 /* return the (first) queue matching with the specified name */
237 queue_t
*snd_seq_queue_find_name(char *name
)
242 for (i
= 0; i
< SNDRV_SEQ_MAX_QUEUES
; i
++) {
243 if ((q
= queueptr(i
)) != NULL
) {
244 if (strncmp(q
->name
, name
, sizeof(q
->name
)) == 0)
253 /* -------------------------------------------------------- */
255 void snd_seq_check_queue(queue_t
*q
, int atomic
, int hop
)
258 snd_seq_event_cell_t
*cell
;
263 /* make this function non-reentrant */
264 spin_lock_irqsave(&q
->check_lock
, flags
);
265 if (q
->check_blocked
) {
267 spin_unlock_irqrestore(&q
->check_lock
, flags
);
268 return; /* other thread is already checking queues */
270 q
->check_blocked
= 1;
271 spin_unlock_irqrestore(&q
->check_lock
, flags
);
274 /* Process tick queue... */
275 while ((cell
= snd_seq_prioq_cell_peek(q
->tickq
)) != NULL
) {
276 if (snd_seq_compare_tick_time(&q
->timer
->tick
.cur_tick
, &cell
->event
.time
.tick
)) {
277 cell
= snd_seq_prioq_cell_out(q
->tickq
);
279 snd_seq_dispatch_event(cell
, atomic
, hop
);
281 /* event remains in the queue */
287 /* Process time queue... */
288 while ((cell
= snd_seq_prioq_cell_peek(q
->timeq
)) != NULL
) {
289 if (snd_seq_compare_real_time(&q
->timer
->cur_time
, &cell
->event
.time
.time
)) {
290 cell
= snd_seq_prioq_cell_out(q
->timeq
);
292 snd_seq_dispatch_event(cell
, atomic
, hop
);
294 /* event remains in the queue */
300 spin_lock_irqsave(&q
->check_lock
, flags
);
301 if (q
->check_again
) {
303 spin_unlock_irqrestore(&q
->check_lock
, flags
);
306 q
->check_blocked
= 0;
307 spin_unlock_irqrestore(&q
->check_lock
, flags
);
311 /* enqueue a event to singe queue */
312 int snd_seq_enqueue_event(snd_seq_event_cell_t
*cell
, int atomic
, int hop
)
317 snd_assert(cell
!= NULL
, return -EINVAL
);
318 dest
= cell
->event
.queue
; /* destination queue */
322 /* handle relative time stamps, convert them into absolute */
323 if ((cell
->event
.flags
& SNDRV_SEQ_TIME_MODE_MASK
) == SNDRV_SEQ_TIME_MODE_REL
) {
324 switch (cell
->event
.flags
& SNDRV_SEQ_TIME_STAMP_MASK
) {
325 case SNDRV_SEQ_TIME_STAMP_TICK
:
326 cell
->event
.time
.tick
+= q
->timer
->tick
.cur_tick
;
329 case SNDRV_SEQ_TIME_STAMP_REAL
:
330 snd_seq_inc_real_time(&cell
->event
.time
.time
, &q
->timer
->cur_time
);
333 cell
->event
.flags
&= ~SNDRV_SEQ_TIME_MODE_MASK
;
334 cell
->event
.flags
|= SNDRV_SEQ_TIME_MODE_ABS
;
336 /* enqueue event in the real-time or midi queue */
337 switch (cell
->event
.flags
& SNDRV_SEQ_TIME_STAMP_MASK
) {
338 case SNDRV_SEQ_TIME_STAMP_TICK
:
339 err
= snd_seq_prioq_cell_in(q
->tickq
, cell
);
342 case SNDRV_SEQ_TIME_STAMP_REAL
:
344 err
= snd_seq_prioq_cell_in(q
->timeq
, cell
);
349 queuefree(q
); /* unlock */
353 /* trigger dispatching */
354 snd_seq_check_queue(q
, atomic
, hop
);
356 queuefree(q
); /* unlock */
362 /*----------------------------------------------------------------*/
364 static inline int check_access(queue_t
*q
, int client
)
366 return (q
->owner
== client
) || (!q
->locked
&& !q
->klocked
);
369 /* check if the client has permission to modify queue parameters.
370 * if it does, lock the queue
372 static int queue_access_lock(queue_t
*q
, int client
)
377 spin_lock_irqsave(&q
->owner_lock
, flags
);
378 access_ok
= check_access(q
, client
);
381 spin_unlock_irqrestore(&q
->owner_lock
, flags
);
385 /* unlock the queue */
386 static inline void queue_access_unlock(queue_t
*q
)
390 spin_lock_irqsave(&q
->owner_lock
, flags
);
392 spin_unlock_irqrestore(&q
->owner_lock
, flags
);
395 /* exported - only checking permission */
396 int snd_seq_queue_check_access(int queueid
, int client
)
398 queue_t
*q
= queueptr(queueid
);
404 spin_lock_irqsave(&q
->owner_lock
, flags
);
405 access_ok
= check_access(q
, client
);
406 spin_unlock_irqrestore(&q
->owner_lock
, flags
);
411 /*----------------------------------------------------------------*/
414 * change queue's owner and permission
416 int snd_seq_queue_set_owner(int queueid
, int client
, int locked
)
418 queue_t
*q
= queueptr(queueid
);
423 if (! queue_access_lock(q
, client
)) {
428 q
->locked
= locked
? 1 : 0;
430 queue_access_unlock(q
);
437 /*----------------------------------------------------------------*/
440 * q->use mutex should be down before calling this function to avoid
441 * confliction with snd_seq_queue_use()
443 int snd_seq_queue_timer_open(int queueid
)
449 queue
= queueptr(queueid
);
453 if ((result
= snd_seq_timer_open(queue
)) < 0) {
454 snd_seq_timer_defaults(tmr
);
455 result
= snd_seq_timer_open(queue
);
462 * q->use mutex should be down before calling this function
464 int snd_seq_queue_timer_close(int queueid
)
470 queue
= queueptr(queueid
);
474 snd_seq_timer_close(queue
);
479 /* change queue tempo and ppq */
480 int snd_seq_queue_timer_set_tempo(int queueid
, int client
, snd_seq_queue_tempo_t
*info
)
482 queue_t
*q
= queueptr(queueid
);
487 if (! queue_access_lock(q
, client
)) {
492 result
= snd_seq_timer_set_tempo(q
->timer
, info
->tempo
);
494 result
= snd_seq_timer_set_ppq(q
->timer
, info
->ppq
);
495 if (result
>= 0 && info
->skew_base
> 0)
496 result
= snd_seq_timer_set_skew(q
->timer
, info
->skew_value
, info
->skew_base
);
497 queue_access_unlock(q
);
503 /* use or unuse this queue -
504 * if it is the first client, starts the timer.
505 * if it is not longer used by any clients, stop the timer.
507 int snd_seq_queue_use(int queueid
, int client
, int use
)
511 queue
= queueptr(queueid
);
514 down(&queue
->timer_mutex
);
516 if (!test_and_set_bit(client
, queue
->clients_bitmap
))
519 if (test_and_clear_bit(client
, queue
->clients_bitmap
))
522 if (queue
->clients
) {
523 if (use
&& queue
->clients
== 1)
524 snd_seq_timer_defaults(queue
->timer
);
525 snd_seq_timer_open(queue
);
527 snd_seq_timer_close(queue
);
529 up(&queue
->timer_mutex
);
535 * check if queue is used by the client
536 * return negative value if the queue is invalid.
537 * return 0 if not used, 1 if used.
539 int snd_seq_queue_is_used(int queueid
, int client
)
544 q
= queueptr(queueid
);
546 return -EINVAL
; /* invalid queue */
547 result
= test_bit(client
, q
->clients_bitmap
) ? 1 : 0;
553 /*----------------------------------------------------------------*/
555 /* notification that client has left the system -
556 * stop the timer on all queues owned by this client
558 void snd_seq_queue_client_termination(int client
)
564 for (i
= 0; i
< SNDRV_SEQ_MAX_QUEUES
; i
++) {
565 if ((q
= queueptr(i
)) == NULL
)
567 spin_lock_irqsave(&q
->owner_lock
, flags
);
568 if (q
->owner
== client
)
570 spin_unlock_irqrestore(&q
->owner_lock
, flags
);
571 if (q
->owner
== client
) {
572 if (q
->timer
->running
)
573 snd_seq_timer_stop(q
->timer
);
574 snd_seq_timer_reset(q
->timer
);
580 /* final stage notification -
581 * remove cells for no longer exist client (for non-owned queue)
582 * or delete this queue (for owned queue)
584 void snd_seq_queue_client_leave(int client
)
589 /* delete own queues from queue list */
590 for (i
= 0; i
< SNDRV_SEQ_MAX_QUEUES
; i
++) {
591 if ((q
= queue_list_remove(i
, client
)) != NULL
)
595 /* remove cells from existing queues -
596 * they are not owned by this client
598 for (i
= 0; i
< SNDRV_SEQ_MAX_QUEUES
; i
++) {
599 if ((q
= queueptr(i
)) == NULL
)
601 if (test_bit(client
, q
->clients_bitmap
)) {
602 snd_seq_prioq_leave(q
->tickq
, client
, 0);
603 snd_seq_prioq_leave(q
->timeq
, client
, 0);
604 snd_seq_queue_use(q
->queue
, client
, 0);
612 /*----------------------------------------------------------------*/
614 /* remove cells from all queues */
615 void snd_seq_queue_client_leave_cells(int client
)
620 for (i
= 0; i
< SNDRV_SEQ_MAX_QUEUES
; i
++) {
621 if ((q
= queueptr(i
)) == NULL
)
623 snd_seq_prioq_leave(q
->tickq
, client
, 0);
624 snd_seq_prioq_leave(q
->timeq
, client
, 0);
629 /* remove cells based on flush criteria */
630 void snd_seq_queue_remove_cells(int client
, snd_seq_remove_events_t
*info
)
635 for (i
= 0; i
< SNDRV_SEQ_MAX_QUEUES
; i
++) {
636 if ((q
= queueptr(i
)) == NULL
)
638 if (test_bit(client
, q
->clients_bitmap
) &&
639 (! (info
->remove_mode
& SNDRV_SEQ_REMOVE_DEST
) ||
640 q
->queue
== info
->queue
)) {
641 snd_seq_prioq_remove_events(q
->tickq
, client
, info
);
642 snd_seq_prioq_remove_events(q
->timeq
, client
, info
);
648 /*----------------------------------------------------------------*/
651 * send events to all subscribed ports
653 static void queue_broadcast_event(queue_t
*q
, snd_seq_event_t
*ev
, int atomic
, int hop
)
659 sev
.flags
= SNDRV_SEQ_TIME_STAMP_TICK
|SNDRV_SEQ_TIME_MODE_ABS
;
660 sev
.time
.tick
= q
->timer
->tick
.cur_tick
;
661 sev
.queue
= q
->queue
;
662 sev
.data
.queue
.queue
= q
->queue
;
664 /* broadcast events from Timer port */
665 sev
.source
.client
= SNDRV_SEQ_CLIENT_SYSTEM
;
666 sev
.source
.port
= SNDRV_SEQ_PORT_SYSTEM_TIMER
;
667 sev
.dest
.client
= SNDRV_SEQ_ADDRESS_SUBSCRIBERS
;
668 snd_seq_kernel_client_dispatch(SNDRV_SEQ_CLIENT_SYSTEM
, &sev
, atomic
, hop
);
672 * process a received queue-control event.
673 * this function is exported for seq_sync.c.
675 void snd_seq_queue_process_event(queue_t
*q
, snd_seq_event_t
*ev
, int atomic
, int hop
)
678 case SNDRV_SEQ_EVENT_START
:
679 snd_seq_prioq_leave(q
->tickq
, ev
->source
.client
, 1);
680 snd_seq_prioq_leave(q
->timeq
, ev
->source
.client
, 1);
681 if (! snd_seq_timer_start(q
->timer
))
682 queue_broadcast_event(q
, ev
, atomic
, hop
);
685 case SNDRV_SEQ_EVENT_CONTINUE
:
686 if (! snd_seq_timer_continue(q
->timer
))
687 queue_broadcast_event(q
, ev
, atomic
, hop
);
690 case SNDRV_SEQ_EVENT_STOP
:
691 snd_seq_timer_stop(q
->timer
);
692 queue_broadcast_event(q
, ev
, atomic
, hop
);
695 case SNDRV_SEQ_EVENT_TEMPO
:
696 snd_seq_timer_set_tempo(q
->timer
, ev
->data
.queue
.param
.value
);
697 queue_broadcast_event(q
, ev
, atomic
, hop
);
700 case SNDRV_SEQ_EVENT_SETPOS_TICK
:
701 if (snd_seq_timer_set_position_tick(q
->timer
, ev
->data
.queue
.param
.time
.tick
) == 0) {
702 queue_broadcast_event(q
, ev
, atomic
, hop
);
706 case SNDRV_SEQ_EVENT_SETPOS_TIME
:
707 if (snd_seq_timer_set_position_time(q
->timer
, ev
->data
.queue
.param
.time
.time
) == 0) {
708 queue_broadcast_event(q
, ev
, atomic
, hop
);
711 case SNDRV_SEQ_EVENT_QUEUE_SKEW
:
712 if (snd_seq_timer_set_skew(q
->timer
,
713 ev
->data
.queue
.param
.skew
.value
,
714 ev
->data
.queue
.param
.skew
.base
) == 0) {
715 queue_broadcast_event(q
, ev
, atomic
, hop
);
723 * Queue control via timer control port:
724 * this function is exported as a callback of timer port.
726 int snd_seq_control_queue(snd_seq_event_t
*ev
, int atomic
, int hop
)
730 snd_assert(ev
!= NULL
, return -EINVAL
);
731 q
= queueptr(ev
->data
.queue
.queue
);
736 if (! queue_access_lock(q
, ev
->source
.client
)) {
741 snd_seq_queue_process_event(q
, ev
, atomic
, hop
);
743 queue_access_unlock(q
);
749 /*----------------------------------------------------------------*/
751 /* exported to seq_info.c */
752 void snd_seq_info_queues_read(snd_info_entry_t
*entry
,
753 snd_info_buffer_t
* buffer
)
759 for (i
= 0; i
< SNDRV_SEQ_MAX_QUEUES
; i
++) {
760 if ((q
= queueptr(i
)) == NULL
)
765 bpm
= 60000000 / tmr
->tempo
;
769 snd_iprintf(buffer
, "queue %d: [%s]\n", q
->queue
, q
->name
);
770 snd_iprintf(buffer
, "owned by client : %d\n", q
->owner
);
771 snd_iprintf(buffer
, "lock status : %s\n", q
->locked
? "Locked" : "Free");
772 snd_iprintf(buffer
, "queued time events : %d\n", snd_seq_prioq_avail(q
->timeq
));
773 snd_iprintf(buffer
, "queued tick events : %d\n", snd_seq_prioq_avail(q
->tickq
));
774 snd_iprintf(buffer
, "timer state : %s\n", tmr
->running
? "Running" : "Stopped");
775 snd_iprintf(buffer
, "timer PPQ : %d\n", tmr
->ppq
);
776 snd_iprintf(buffer
, "current tempo : %d\n", tmr
->tempo
);
777 snd_iprintf(buffer
, "current BPM : %d\n", bpm
);
778 snd_iprintf(buffer
, "current time : %d.%09d s\n", tmr
->cur_time
.tv_sec
, tmr
->cur_time
.tv_nsec
);
779 snd_iprintf(buffer
, "current tick : %d\n", tmr
->tick
.cur_tick
);
780 snd_iprintf(buffer
, "\n");