Commit | Line | Data |
---|---|---|
1da177e4 LT |
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. | |
c7e0b5bf | 32 | * - The queue is locked when struct snd_seq_queue pointer is returned via |
1da177e4 LT |
33 | * queueptr(). This pointer *MUST* be released afterward by |
34 | * queuefree(ptr). | |
35 | * - Addition of experimental sync support. | |
36 | */ | |
37 | ||
1da177e4 LT |
38 | #include <linux/init.h> |
39 | #include <linux/slab.h> | |
40 | #include <sound/core.h> | |
41 | ||
42 | #include "seq_memory.h" | |
43 | #include "seq_queue.h" | |
44 | #include "seq_clientmgr.h" | |
45 | #include "seq_fifo.h" | |
46 | #include "seq_timer.h" | |
47 | #include "seq_info.h" | |
48 | ||
49 | /* list of allocated queues */ | |
c7e0b5bf | 50 | static struct snd_seq_queue *queue_list[SNDRV_SEQ_MAX_QUEUES]; |
1da177e4 LT |
51 | static DEFINE_SPINLOCK(queue_list_lock); |
52 | /* number of queues allocated */ | |
53 | static int num_queues; | |
54 | ||
55 | int snd_seq_queue_get_cur_queues(void) | |
56 | { | |
57 | return num_queues; | |
58 | } | |
59 | ||
60 | /*----------------------------------------------------------------*/ | |
61 | ||
62 | /* assign queue id and insert to list */ | |
c7e0b5bf | 63 | static int queue_list_add(struct snd_seq_queue *q) |
1da177e4 LT |
64 | { |
65 | int i; | |
66 | unsigned long flags; | |
67 | ||
68 | spin_lock_irqsave(&queue_list_lock, flags); | |
69 | for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { | |
70 | if (! queue_list[i]) { | |
71 | queue_list[i] = q; | |
72 | q->queue = i; | |
73 | num_queues++; | |
74 | spin_unlock_irqrestore(&queue_list_lock, flags); | |
75 | return i; | |
76 | } | |
77 | } | |
78 | spin_unlock_irqrestore(&queue_list_lock, flags); | |
79 | return -1; | |
80 | } | |
81 | ||
c7e0b5bf | 82 | static struct snd_seq_queue *queue_list_remove(int id, int client) |
1da177e4 | 83 | { |
c7e0b5bf | 84 | struct snd_seq_queue *q; |
1da177e4 LT |
85 | unsigned long flags; |
86 | ||
87 | spin_lock_irqsave(&queue_list_lock, flags); | |
88 | q = queue_list[id]; | |
89 | if (q) { | |
90 | spin_lock(&q->owner_lock); | |
91 | if (q->owner == client) { | |
92 | /* found */ | |
93 | q->klocked = 1; | |
94 | spin_unlock(&q->owner_lock); | |
95 | queue_list[id] = NULL; | |
96 | num_queues--; | |
97 | spin_unlock_irqrestore(&queue_list_lock, flags); | |
98 | return q; | |
99 | } | |
100 | spin_unlock(&q->owner_lock); | |
101 | } | |
102 | spin_unlock_irqrestore(&queue_list_lock, flags); | |
103 | return NULL; | |
104 | } | |
105 | ||
106 | /*----------------------------------------------------------------*/ | |
107 | ||
108 | /* create new queue (constructor) */ | |
c7e0b5bf | 109 | static struct snd_seq_queue *queue_new(int owner, int locked) |
1da177e4 | 110 | { |
c7e0b5bf | 111 | struct snd_seq_queue *q; |
1da177e4 | 112 | |
ecca82b4 | 113 | q = kzalloc(sizeof(*q), GFP_KERNEL); |
24db8bba | 114 | if (!q) |
1da177e4 | 115 | return NULL; |
1da177e4 LT |
116 | |
117 | spin_lock_init(&q->owner_lock); | |
118 | spin_lock_init(&q->check_lock); | |
1a60d4c5 | 119 | mutex_init(&q->timer_mutex); |
1da177e4 LT |
120 | snd_use_lock_init(&q->use_lock); |
121 | q->queue = -1; | |
122 | ||
123 | q->tickq = snd_seq_prioq_new(); | |
124 | q->timeq = snd_seq_prioq_new(); | |
125 | q->timer = snd_seq_timer_new(); | |
126 | if (q->tickq == NULL || q->timeq == NULL || q->timer == NULL) { | |
127 | snd_seq_prioq_delete(&q->tickq); | |
128 | snd_seq_prioq_delete(&q->timeq); | |
129 | snd_seq_timer_delete(&q->timer); | |
130 | kfree(q); | |
131 | return NULL; | |
132 | } | |
133 | ||
134 | q->owner = owner; | |
135 | q->locked = locked; | |
136 | q->klocked = 0; | |
137 | ||
138 | return q; | |
139 | } | |
140 | ||
141 | /* delete queue (destructor) */ | |
c7e0b5bf | 142 | static void queue_delete(struct snd_seq_queue *q) |
1da177e4 LT |
143 | { |
144 | /* stop and release the timer */ | |
145 | snd_seq_timer_stop(q->timer); | |
146 | snd_seq_timer_close(q); | |
147 | /* wait until access free */ | |
148 | snd_use_lock_sync(&q->use_lock); | |
149 | /* release resources... */ | |
150 | snd_seq_prioq_delete(&q->tickq); | |
151 | snd_seq_prioq_delete(&q->timeq); | |
152 | snd_seq_timer_delete(&q->timer); | |
153 | ||
154 | kfree(q); | |
155 | } | |
156 | ||
157 | ||
158 | /*----------------------------------------------------------------*/ | |
159 | ||
160 | /* setup queues */ | |
161 | int __init snd_seq_queues_init(void) | |
162 | { | |
163 | /* | |
164 | memset(queue_list, 0, sizeof(queue_list)); | |
165 | num_queues = 0; | |
166 | */ | |
167 | return 0; | |
168 | } | |
169 | ||
170 | /* delete all existing queues */ | |
171 | void __exit snd_seq_queues_delete(void) | |
172 | { | |
173 | int i; | |
174 | ||
175 | /* clear list */ | |
176 | for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { | |
177 | if (queue_list[i]) | |
178 | queue_delete(queue_list[i]); | |
179 | } | |
180 | } | |
181 | ||
182 | /* allocate a new queue - | |
183 | * return queue index value or negative value for error | |
184 | */ | |
185 | int snd_seq_queue_alloc(int client, int locked, unsigned int info_flags) | |
186 | { | |
c7e0b5bf | 187 | struct snd_seq_queue *q; |
1da177e4 LT |
188 | |
189 | q = queue_new(client, locked); | |
190 | if (q == NULL) | |
191 | return -ENOMEM; | |
192 | q->info_flags = info_flags; | |
193 | if (queue_list_add(q) < 0) { | |
194 | queue_delete(q); | |
195 | return -ENOMEM; | |
196 | } | |
197 | snd_seq_queue_use(q->queue, client, 1); /* use this queue */ | |
198 | return q->queue; | |
199 | } | |
200 | ||
201 | /* delete a queue - queue must be owned by the client */ | |
202 | int snd_seq_queue_delete(int client, int queueid) | |
203 | { | |
c7e0b5bf | 204 | struct snd_seq_queue *q; |
1da177e4 LT |
205 | |
206 | if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES) | |
207 | return -EINVAL; | |
208 | q = queue_list_remove(queueid, client); | |
209 | if (q == NULL) | |
210 | return -EINVAL; | |
211 | queue_delete(q); | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
216 | ||
217 | /* return pointer to queue structure for specified id */ | |
c7e0b5bf | 218 | struct snd_seq_queue *queueptr(int queueid) |
1da177e4 | 219 | { |
c7e0b5bf | 220 | struct snd_seq_queue *q; |
1da177e4 LT |
221 | unsigned long flags; |
222 | ||
223 | if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES) | |
224 | return NULL; | |
225 | spin_lock_irqsave(&queue_list_lock, flags); | |
226 | q = queue_list[queueid]; | |
227 | if (q) | |
228 | snd_use_lock_use(&q->use_lock); | |
229 | spin_unlock_irqrestore(&queue_list_lock, flags); | |
230 | return q; | |
231 | } | |
232 | ||
233 | /* return the (first) queue matching with the specified name */ | |
c7e0b5bf | 234 | struct snd_seq_queue *snd_seq_queue_find_name(char *name) |
1da177e4 LT |
235 | { |
236 | int i; | |
c7e0b5bf | 237 | struct snd_seq_queue *q; |
1da177e4 LT |
238 | |
239 | for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { | |
240 | if ((q = queueptr(i)) != NULL) { | |
241 | if (strncmp(q->name, name, sizeof(q->name)) == 0) | |
242 | return q; | |
243 | queuefree(q); | |
244 | } | |
245 | } | |
246 | return NULL; | |
247 | } | |
248 | ||
249 | ||
250 | /* -------------------------------------------------------- */ | |
251 | ||
c7e0b5bf | 252 | void snd_seq_check_queue(struct snd_seq_queue *q, int atomic, int hop) |
1da177e4 LT |
253 | { |
254 | unsigned long flags; | |
c7e0b5bf | 255 | struct snd_seq_event_cell *cell; |
1da177e4 LT |
256 | |
257 | if (q == NULL) | |
258 | return; | |
259 | ||
260 | /* make this function non-reentrant */ | |
261 | spin_lock_irqsave(&q->check_lock, flags); | |
262 | if (q->check_blocked) { | |
263 | q->check_again = 1; | |
264 | spin_unlock_irqrestore(&q->check_lock, flags); | |
265 | return; /* other thread is already checking queues */ | |
266 | } | |
267 | q->check_blocked = 1; | |
268 | spin_unlock_irqrestore(&q->check_lock, flags); | |
269 | ||
270 | __again: | |
271 | /* Process tick queue... */ | |
272 | while ((cell = snd_seq_prioq_cell_peek(q->tickq)) != NULL) { | |
c7e0b5bf TI |
273 | if (snd_seq_compare_tick_time(&q->timer->tick.cur_tick, |
274 | &cell->event.time.tick)) { | |
1da177e4 LT |
275 | cell = snd_seq_prioq_cell_out(q->tickq); |
276 | if (cell) | |
277 | snd_seq_dispatch_event(cell, atomic, hop); | |
278 | } else { | |
279 | /* event remains in the queue */ | |
280 | break; | |
281 | } | |
282 | } | |
283 | ||
284 | ||
285 | /* Process time queue... */ | |
286 | while ((cell = snd_seq_prioq_cell_peek(q->timeq)) != NULL) { | |
c7e0b5bf TI |
287 | if (snd_seq_compare_real_time(&q->timer->cur_time, |
288 | &cell->event.time.time)) { | |
1da177e4 LT |
289 | cell = snd_seq_prioq_cell_out(q->timeq); |
290 | if (cell) | |
291 | snd_seq_dispatch_event(cell, atomic, hop); | |
292 | } else { | |
293 | /* event remains in the queue */ | |
294 | break; | |
295 | } | |
296 | } | |
297 | ||
298 | /* free lock */ | |
299 | spin_lock_irqsave(&q->check_lock, flags); | |
300 | if (q->check_again) { | |
301 | q->check_again = 0; | |
302 | spin_unlock_irqrestore(&q->check_lock, flags); | |
303 | goto __again; | |
304 | } | |
305 | q->check_blocked = 0; | |
306 | spin_unlock_irqrestore(&q->check_lock, flags); | |
307 | } | |
308 | ||
309 | ||
310 | /* enqueue a event to singe queue */ | |
c7e0b5bf | 311 | int snd_seq_enqueue_event(struct snd_seq_event_cell *cell, int atomic, int hop) |
1da177e4 LT |
312 | { |
313 | int dest, err; | |
c7e0b5bf | 314 | struct snd_seq_queue *q; |
1da177e4 | 315 | |
7eaa943c TI |
316 | if (snd_BUG_ON(!cell)) |
317 | return -EINVAL; | |
1da177e4 LT |
318 | dest = cell->event.queue; /* destination queue */ |
319 | q = queueptr(dest); | |
320 | if (q == NULL) | |
321 | return -EINVAL; | |
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; | |
327 | break; | |
328 | ||
329 | case SNDRV_SEQ_TIME_STAMP_REAL: | |
c7e0b5bf TI |
330 | snd_seq_inc_real_time(&cell->event.time.time, |
331 | &q->timer->cur_time); | |
1da177e4 LT |
332 | break; |
333 | } | |
334 | cell->event.flags &= ~SNDRV_SEQ_TIME_MODE_MASK; | |
335 | cell->event.flags |= SNDRV_SEQ_TIME_MODE_ABS; | |
336 | } | |
337 | /* enqueue event in the real-time or midi queue */ | |
338 | switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) { | |
339 | case SNDRV_SEQ_TIME_STAMP_TICK: | |
340 | err = snd_seq_prioq_cell_in(q->tickq, cell); | |
341 | break; | |
342 | ||
343 | case SNDRV_SEQ_TIME_STAMP_REAL: | |
344 | default: | |
345 | err = snd_seq_prioq_cell_in(q->timeq, cell); | |
346 | break; | |
347 | } | |
348 | ||
349 | if (err < 0) { | |
350 | queuefree(q); /* unlock */ | |
351 | return err; | |
352 | } | |
353 | ||
354 | /* trigger dispatching */ | |
355 | snd_seq_check_queue(q, atomic, hop); | |
356 | ||
357 | queuefree(q); /* unlock */ | |
358 | ||
359 | return 0; | |
360 | } | |
361 | ||
362 | ||
363 | /*----------------------------------------------------------------*/ | |
364 | ||
c7e0b5bf | 365 | static inline int check_access(struct snd_seq_queue *q, int client) |
1da177e4 LT |
366 | { |
367 | return (q->owner == client) || (!q->locked && !q->klocked); | |
368 | } | |
369 | ||
370 | /* check if the client has permission to modify queue parameters. | |
371 | * if it does, lock the queue | |
372 | */ | |
c7e0b5bf | 373 | static int queue_access_lock(struct snd_seq_queue *q, int client) |
1da177e4 LT |
374 | { |
375 | unsigned long flags; | |
376 | int access_ok; | |
377 | ||
378 | spin_lock_irqsave(&q->owner_lock, flags); | |
379 | access_ok = check_access(q, client); | |
380 | if (access_ok) | |
381 | q->klocked = 1; | |
382 | spin_unlock_irqrestore(&q->owner_lock, flags); | |
383 | return access_ok; | |
384 | } | |
385 | ||
386 | /* unlock the queue */ | |
c7e0b5bf | 387 | static inline void queue_access_unlock(struct snd_seq_queue *q) |
1da177e4 LT |
388 | { |
389 | unsigned long flags; | |
390 | ||
391 | spin_lock_irqsave(&q->owner_lock, flags); | |
392 | q->klocked = 0; | |
393 | spin_unlock_irqrestore(&q->owner_lock, flags); | |
394 | } | |
395 | ||
396 | /* exported - only checking permission */ | |
397 | int snd_seq_queue_check_access(int queueid, int client) | |
398 | { | |
c7e0b5bf | 399 | struct snd_seq_queue *q = queueptr(queueid); |
1da177e4 LT |
400 | int access_ok; |
401 | unsigned long flags; | |
402 | ||
403 | if (! q) | |
404 | return 0; | |
405 | spin_lock_irqsave(&q->owner_lock, flags); | |
406 | access_ok = check_access(q, client); | |
407 | spin_unlock_irqrestore(&q->owner_lock, flags); | |
408 | queuefree(q); | |
409 | return access_ok; | |
410 | } | |
411 | ||
412 | /*----------------------------------------------------------------*/ | |
413 | ||
414 | /* | |
415 | * change queue's owner and permission | |
416 | */ | |
417 | int snd_seq_queue_set_owner(int queueid, int client, int locked) | |
418 | { | |
c7e0b5bf | 419 | struct snd_seq_queue *q = queueptr(queueid); |
1da177e4 LT |
420 | |
421 | if (q == NULL) | |
422 | return -EINVAL; | |
423 | ||
424 | if (! queue_access_lock(q, client)) { | |
425 | queuefree(q); | |
426 | return -EPERM; | |
427 | } | |
428 | ||
429 | q->locked = locked ? 1 : 0; | |
430 | q->owner = client; | |
431 | queue_access_unlock(q); | |
432 | queuefree(q); | |
433 | ||
434 | return 0; | |
435 | } | |
436 | ||
437 | ||
438 | /*----------------------------------------------------------------*/ | |
439 | ||
440 | /* open timer - | |
441 | * q->use mutex should be down before calling this function to avoid | |
442 | * confliction with snd_seq_queue_use() | |
443 | */ | |
444 | int snd_seq_queue_timer_open(int queueid) | |
445 | { | |
446 | int result = 0; | |
c7e0b5bf TI |
447 | struct snd_seq_queue *queue; |
448 | struct snd_seq_timer *tmr; | |
1da177e4 LT |
449 | |
450 | queue = queueptr(queueid); | |
451 | if (queue == NULL) | |
452 | return -EINVAL; | |
453 | tmr = queue->timer; | |
454 | if ((result = snd_seq_timer_open(queue)) < 0) { | |
455 | snd_seq_timer_defaults(tmr); | |
456 | result = snd_seq_timer_open(queue); | |
457 | } | |
458 | queuefree(queue); | |
459 | return result; | |
460 | } | |
461 | ||
462 | /* close timer - | |
463 | * q->use mutex should be down before calling this function | |
464 | */ | |
465 | int snd_seq_queue_timer_close(int queueid) | |
466 | { | |
c7e0b5bf | 467 | struct snd_seq_queue *queue; |
1da177e4 LT |
468 | int result = 0; |
469 | ||
470 | queue = queueptr(queueid); | |
471 | if (queue == NULL) | |
472 | return -EINVAL; | |
1da177e4 LT |
473 | snd_seq_timer_close(queue); |
474 | queuefree(queue); | |
475 | return result; | |
476 | } | |
477 | ||
478 | /* change queue tempo and ppq */ | |
c7e0b5bf TI |
479 | int snd_seq_queue_timer_set_tempo(int queueid, int client, |
480 | struct snd_seq_queue_tempo *info) | |
1da177e4 | 481 | { |
c7e0b5bf | 482 | struct snd_seq_queue *q = queueptr(queueid); |
1da177e4 LT |
483 | int result; |
484 | ||
485 | if (q == NULL) | |
486 | return -EINVAL; | |
487 | if (! queue_access_lock(q, client)) { | |
488 | queuefree(q); | |
489 | return -EPERM; | |
490 | } | |
491 | ||
492 | result = snd_seq_timer_set_tempo(q->timer, info->tempo); | |
493 | if (result >= 0) | |
494 | result = snd_seq_timer_set_ppq(q->timer, info->ppq); | |
495 | if (result >= 0 && info->skew_base > 0) | |
c7e0b5bf TI |
496 | result = snd_seq_timer_set_skew(q->timer, info->skew_value, |
497 | info->skew_base); | |
1da177e4 LT |
498 | queue_access_unlock(q); |
499 | queuefree(q); | |
500 | return result; | |
501 | } | |
502 | ||
503 | ||
504 | /* use or unuse this queue - | |
505 | * if it is the first client, starts the timer. | |
506 | * if it is not longer used by any clients, stop the timer. | |
507 | */ | |
508 | int snd_seq_queue_use(int queueid, int client, int use) | |
509 | { | |
c7e0b5bf | 510 | struct snd_seq_queue *queue; |
1da177e4 LT |
511 | |
512 | queue = queueptr(queueid); | |
513 | if (queue == NULL) | |
514 | return -EINVAL; | |
1a60d4c5 | 515 | mutex_lock(&queue->timer_mutex); |
1da177e4 LT |
516 | if (use) { |
517 | if (!test_and_set_bit(client, queue->clients_bitmap)) | |
518 | queue->clients++; | |
519 | } else { | |
520 | if (test_and_clear_bit(client, queue->clients_bitmap)) | |
521 | queue->clients--; | |
522 | } | |
523 | if (queue->clients) { | |
524 | if (use && queue->clients == 1) | |
525 | snd_seq_timer_defaults(queue->timer); | |
526 | snd_seq_timer_open(queue); | |
527 | } else { | |
528 | snd_seq_timer_close(queue); | |
529 | } | |
1a60d4c5 | 530 | mutex_unlock(&queue->timer_mutex); |
1da177e4 LT |
531 | queuefree(queue); |
532 | return 0; | |
533 | } | |
534 | ||
535 | /* | |
536 | * check if queue is used by the client | |
537 | * return negative value if the queue is invalid. | |
538 | * return 0 if not used, 1 if used. | |
539 | */ | |
540 | int snd_seq_queue_is_used(int queueid, int client) | |
541 | { | |
c7e0b5bf | 542 | struct snd_seq_queue *q; |
1da177e4 LT |
543 | int result; |
544 | ||
545 | q = queueptr(queueid); | |
546 | if (q == NULL) | |
547 | return -EINVAL; /* invalid queue */ | |
548 | result = test_bit(client, q->clients_bitmap) ? 1 : 0; | |
549 | queuefree(q); | |
550 | return result; | |
551 | } | |
552 | ||
553 | ||
554 | /*----------------------------------------------------------------*/ | |
555 | ||
556 | /* notification that client has left the system - | |
557 | * stop the timer on all queues owned by this client | |
558 | */ | |
559 | void snd_seq_queue_client_termination(int client) | |
560 | { | |
561 | unsigned long flags; | |
562 | int i; | |
c7e0b5bf | 563 | struct snd_seq_queue *q; |
1da177e4 LT |
564 | |
565 | for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { | |
566 | if ((q = queueptr(i)) == NULL) | |
567 | continue; | |
568 | spin_lock_irqsave(&q->owner_lock, flags); | |
569 | if (q->owner == client) | |
570 | q->klocked = 1; | |
571 | spin_unlock_irqrestore(&q->owner_lock, flags); | |
572 | if (q->owner == client) { | |
573 | if (q->timer->running) | |
574 | snd_seq_timer_stop(q->timer); | |
575 | snd_seq_timer_reset(q->timer); | |
576 | } | |
577 | queuefree(q); | |
578 | } | |
579 | } | |
580 | ||
581 | /* final stage notification - | |
582 | * remove cells for no longer exist client (for non-owned queue) | |
583 | * or delete this queue (for owned queue) | |
584 | */ | |
585 | void snd_seq_queue_client_leave(int client) | |
586 | { | |
587 | int i; | |
c7e0b5bf | 588 | struct snd_seq_queue *q; |
1da177e4 LT |
589 | |
590 | /* delete own queues from queue list */ | |
591 | for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { | |
592 | if ((q = queue_list_remove(i, client)) != NULL) | |
593 | queue_delete(q); | |
594 | } | |
595 | ||
596 | /* remove cells from existing queues - | |
597 | * they are not owned by this client | |
598 | */ | |
599 | for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { | |
600 | if ((q = queueptr(i)) == NULL) | |
601 | continue; | |
602 | if (test_bit(client, q->clients_bitmap)) { | |
603 | snd_seq_prioq_leave(q->tickq, client, 0); | |
604 | snd_seq_prioq_leave(q->timeq, client, 0); | |
605 | snd_seq_queue_use(q->queue, client, 0); | |
606 | } | |
607 | queuefree(q); | |
608 | } | |
609 | } | |
610 | ||
611 | ||
612 | ||
613 | /*----------------------------------------------------------------*/ | |
614 | ||
615 | /* remove cells from all queues */ | |
616 | void snd_seq_queue_client_leave_cells(int client) | |
617 | { | |
618 | int i; | |
c7e0b5bf | 619 | struct snd_seq_queue *q; |
1da177e4 LT |
620 | |
621 | for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { | |
622 | if ((q = queueptr(i)) == NULL) | |
623 | continue; | |
624 | snd_seq_prioq_leave(q->tickq, client, 0); | |
625 | snd_seq_prioq_leave(q->timeq, client, 0); | |
626 | queuefree(q); | |
627 | } | |
628 | } | |
629 | ||
630 | /* remove cells based on flush criteria */ | |
c7e0b5bf | 631 | void snd_seq_queue_remove_cells(int client, struct snd_seq_remove_events *info) |
1da177e4 LT |
632 | { |
633 | int i; | |
c7e0b5bf | 634 | struct snd_seq_queue *q; |
1da177e4 LT |
635 | |
636 | for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { | |
637 | if ((q = queueptr(i)) == NULL) | |
638 | continue; | |
639 | if (test_bit(client, q->clients_bitmap) && | |
640 | (! (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) || | |
641 | q->queue == info->queue)) { | |
642 | snd_seq_prioq_remove_events(q->tickq, client, info); | |
643 | snd_seq_prioq_remove_events(q->timeq, client, info); | |
644 | } | |
645 | queuefree(q); | |
646 | } | |
647 | } | |
648 | ||
649 | /*----------------------------------------------------------------*/ | |
650 | ||
651 | /* | |
652 | * send events to all subscribed ports | |
653 | */ | |
c7e0b5bf TI |
654 | static void queue_broadcast_event(struct snd_seq_queue *q, struct snd_seq_event *ev, |
655 | int atomic, int hop) | |
1da177e4 | 656 | { |
c7e0b5bf | 657 | struct snd_seq_event sev; |
1da177e4 LT |
658 | |
659 | sev = *ev; | |
660 | ||
661 | sev.flags = SNDRV_SEQ_TIME_STAMP_TICK|SNDRV_SEQ_TIME_MODE_ABS; | |
662 | sev.time.tick = q->timer->tick.cur_tick; | |
663 | sev.queue = q->queue; | |
664 | sev.data.queue.queue = q->queue; | |
665 | ||
666 | /* broadcast events from Timer port */ | |
667 | sev.source.client = SNDRV_SEQ_CLIENT_SYSTEM; | |
668 | sev.source.port = SNDRV_SEQ_PORT_SYSTEM_TIMER; | |
669 | sev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS; | |
670 | snd_seq_kernel_client_dispatch(SNDRV_SEQ_CLIENT_SYSTEM, &sev, atomic, hop); | |
671 | } | |
672 | ||
673 | /* | |
674 | * process a received queue-control event. | |
675 | * this function is exported for seq_sync.c. | |
676 | */ | |
c7e0b5bf TI |
677 | static void snd_seq_queue_process_event(struct snd_seq_queue *q, |
678 | struct snd_seq_event *ev, | |
123992f7 | 679 | int atomic, int hop) |
1da177e4 LT |
680 | { |
681 | switch (ev->type) { | |
682 | case SNDRV_SEQ_EVENT_START: | |
683 | snd_seq_prioq_leave(q->tickq, ev->source.client, 1); | |
684 | snd_seq_prioq_leave(q->timeq, ev->source.client, 1); | |
685 | if (! snd_seq_timer_start(q->timer)) | |
686 | queue_broadcast_event(q, ev, atomic, hop); | |
687 | break; | |
688 | ||
689 | case SNDRV_SEQ_EVENT_CONTINUE: | |
690 | if (! snd_seq_timer_continue(q->timer)) | |
691 | queue_broadcast_event(q, ev, atomic, hop); | |
692 | break; | |
693 | ||
694 | case SNDRV_SEQ_EVENT_STOP: | |
695 | snd_seq_timer_stop(q->timer); | |
696 | queue_broadcast_event(q, ev, atomic, hop); | |
697 | break; | |
698 | ||
699 | case SNDRV_SEQ_EVENT_TEMPO: | |
700 | snd_seq_timer_set_tempo(q->timer, ev->data.queue.param.value); | |
701 | queue_broadcast_event(q, ev, atomic, hop); | |
702 | break; | |
703 | ||
704 | case SNDRV_SEQ_EVENT_SETPOS_TICK: | |
705 | if (snd_seq_timer_set_position_tick(q->timer, ev->data.queue.param.time.tick) == 0) { | |
706 | queue_broadcast_event(q, ev, atomic, hop); | |
707 | } | |
708 | break; | |
709 | ||
710 | case SNDRV_SEQ_EVENT_SETPOS_TIME: | |
711 | if (snd_seq_timer_set_position_time(q->timer, ev->data.queue.param.time.time) == 0) { | |
712 | queue_broadcast_event(q, ev, atomic, hop); | |
713 | } | |
714 | break; | |
715 | case SNDRV_SEQ_EVENT_QUEUE_SKEW: | |
716 | if (snd_seq_timer_set_skew(q->timer, | |
717 | ev->data.queue.param.skew.value, | |
718 | ev->data.queue.param.skew.base) == 0) { | |
719 | queue_broadcast_event(q, ev, atomic, hop); | |
720 | } | |
721 | break; | |
722 | } | |
723 | } | |
724 | ||
725 | ||
726 | /* | |
727 | * Queue control via timer control port: | |
728 | * this function is exported as a callback of timer port. | |
729 | */ | |
c7e0b5bf | 730 | int snd_seq_control_queue(struct snd_seq_event *ev, int atomic, int hop) |
1da177e4 | 731 | { |
c7e0b5bf | 732 | struct snd_seq_queue *q; |
1da177e4 | 733 | |
7eaa943c TI |
734 | if (snd_BUG_ON(!ev)) |
735 | return -EINVAL; | |
1da177e4 LT |
736 | q = queueptr(ev->data.queue.queue); |
737 | ||
738 | if (q == NULL) | |
739 | return -EINVAL; | |
740 | ||
741 | if (! queue_access_lock(q, ev->source.client)) { | |
742 | queuefree(q); | |
743 | return -EPERM; | |
744 | } | |
745 | ||
746 | snd_seq_queue_process_event(q, ev, atomic, hop); | |
747 | ||
748 | queue_access_unlock(q); | |
749 | queuefree(q); | |
750 | return 0; | |
751 | } | |
752 | ||
753 | ||
754 | /*----------------------------------------------------------------*/ | |
755 | ||
cd6a6503 | 756 | #ifdef CONFIG_SND_PROC_FS |
1da177e4 | 757 | /* exported to seq_info.c */ |
c7e0b5bf TI |
758 | void snd_seq_info_queues_read(struct snd_info_entry *entry, |
759 | struct snd_info_buffer *buffer) | |
1da177e4 LT |
760 | { |
761 | int i, bpm; | |
c7e0b5bf TI |
762 | struct snd_seq_queue *q; |
763 | struct snd_seq_timer *tmr; | |
1da177e4 LT |
764 | |
765 | for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { | |
766 | if ((q = queueptr(i)) == NULL) | |
767 | continue; | |
768 | ||
769 | tmr = q->timer; | |
770 | if (tmr->tempo) | |
771 | bpm = 60000000 / tmr->tempo; | |
772 | else | |
773 | bpm = 0; | |
774 | ||
775 | snd_iprintf(buffer, "queue %d: [%s]\n", q->queue, q->name); | |
776 | snd_iprintf(buffer, "owned by client : %d\n", q->owner); | |
777 | snd_iprintf(buffer, "lock status : %s\n", q->locked ? "Locked" : "Free"); | |
778 | snd_iprintf(buffer, "queued time events : %d\n", snd_seq_prioq_avail(q->timeq)); | |
779 | snd_iprintf(buffer, "queued tick events : %d\n", snd_seq_prioq_avail(q->tickq)); | |
780 | snd_iprintf(buffer, "timer state : %s\n", tmr->running ? "Running" : "Stopped"); | |
781 | snd_iprintf(buffer, "timer PPQ : %d\n", tmr->ppq); | |
782 | snd_iprintf(buffer, "current tempo : %d\n", tmr->tempo); | |
783 | snd_iprintf(buffer, "current BPM : %d\n", bpm); | |
784 | snd_iprintf(buffer, "current time : %d.%09d s\n", tmr->cur_time.tv_sec, tmr->cur_time.tv_nsec); | |
785 | snd_iprintf(buffer, "current tick : %d\n", tmr->tick.cur_tick); | |
786 | snd_iprintf(buffer, "\n"); | |
787 | queuefree(q); | |
788 | } | |
789 | } | |
cd6a6503 | 790 | #endif /* CONFIG_SND_PROC_FS */ |
04f141a8 | 791 |