2 * ALSA sequencer Memory Manager
3 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
4 * Jaroslav Kysela <perex@suse.cz>
5 * 2000 by Takashi Iwai <tiwai@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sound/driver.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <sound/core.h>
29 #include <sound/seq_kernel.h>
30 #include "seq_memory.h"
31 #include "seq_queue.h"
35 /* semaphore in struct file record */
36 #define semaphore_of(fp) ((fp)->f_dentry->d_inode->i_sem)
39 static inline int snd_seq_pool_available(pool_t *pool)
41 return pool->total_elements - atomic_read(&pool->counter);
44 static inline int snd_seq_output_ok(pool_t *pool)
46 return snd_seq_pool_available(pool) >= pool->room;
50 * Variable length event:
51 * The event like sysex uses variable length type.
52 * The external data may be stored in three different formats.
54 * This is the normal case.
55 * ext.data.len = length
56 * ext.data.ptr = buffer pointer
58 * When an event is generated via read(), the external data is
59 * kept in user space until expanded.
60 * ext.data.len = length | SNDRV_SEQ_EXT_USRPTR
61 * ext.data.ptr = userspace pointer
63 * When the variable length event is enqueued (in prioq or fifo),
64 * the external data is decomposed to several cells.
65 * ext.data.len = length | SNDRV_SEQ_EXT_CHAINED
66 * ext.data.ptr = the additiona cell head
67 * -> cell.next -> cell.next -> ..
72 * call dump function to expand external data.
75 static int get_var_len(const snd_seq_event_t *event)
77 if ((event->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
80 return event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
83 int snd_seq_dump_var_event(const snd_seq_event_t *event, snd_seq_dump_func_t func, void *private_data)
86 snd_seq_event_cell_t *cell;
88 if ((len = get_var_len(event)) <= 0)
91 if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
93 char __user *curptr = (char __user *)event->data.ext.ptr;
95 int size = sizeof(buf);
98 if (copy_from_user(buf, curptr, size))
100 err = func(private_data, buf, size);
107 } if (! (event->data.ext.len & SNDRV_SEQ_EXT_CHAINED)) {
108 return func(private_data, event->data.ext.ptr, len);
111 cell = (snd_seq_event_cell_t*)event->data.ext.ptr;
112 for (; len > 0 && cell; cell = cell->next) {
113 int size = sizeof(snd_seq_event_t);
116 err = func(private_data, &cell->event, size);
127 * expand the variable length event to linear buffer space.
130 static int seq_copy_in_kernel(char **bufptr, const void *src, int size)
132 memcpy(*bufptr, src, size);
137 static int seq_copy_in_user(char __user **bufptr, const void *src, int size)
139 if (copy_to_user(*bufptr, src, size))
145 int snd_seq_expand_var_event(const snd_seq_event_t *event, int count, char *buf, int in_kernel, int size_aligned)
150 if ((len = get_var_len(event)) < 0)
153 if (size_aligned > 0)
154 newlen = ((len + size_aligned - 1) / size_aligned) * size_aligned;
158 if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
161 if (copy_from_user(buf, (void __user *)event->data.ext.ptr, len))
165 err = snd_seq_dump_var_event(event,
166 in_kernel ? (snd_seq_dump_func_t)seq_copy_in_kernel :
167 (snd_seq_dump_func_t)seq_copy_in_user,
169 return err < 0 ? err : newlen;
174 * release this cell, free extended data if available
177 static inline void free_cell(pool_t *pool, snd_seq_event_cell_t *cell)
179 cell->next = pool->free;
181 atomic_dec(&pool->counter);
184 void snd_seq_cell_free(snd_seq_event_cell_t * cell)
189 snd_assert(cell != NULL, return);
191 snd_assert(pool != NULL, return);
193 spin_lock_irqsave(&pool->lock, flags);
194 free_cell(pool, cell);
195 if (snd_seq_ev_is_variable(&cell->event)) {
196 if (cell->event.data.ext.len & SNDRV_SEQ_EXT_CHAINED) {
197 snd_seq_event_cell_t *curp, *nextptr;
198 curp = cell->event.data.ext.ptr;
199 for (; curp; curp = nextptr) {
200 nextptr = curp->next;
201 curp->next = pool->free;
202 free_cell(pool, curp);
206 if (waitqueue_active(&pool->output_sleep)) {
207 /* has enough space now? */
208 if (snd_seq_output_ok(pool))
209 wake_up(&pool->output_sleep);
211 spin_unlock_irqrestore(&pool->lock, flags);
216 * allocate an event cell.
218 static int snd_seq_cell_alloc(pool_t *pool, snd_seq_event_cell_t **cellp, int nonblock, struct file *file)
220 snd_seq_event_cell_t *cell;
230 init_waitqueue_entry(&wait, current);
231 spin_lock_irqsave(&pool->lock, flags);
232 if (pool->ptr == NULL) { /* not initialized */
233 snd_printd("seq: pool is not initialized\n");
237 while (pool->free == NULL && ! nonblock && ! pool->closing) {
239 set_current_state(TASK_INTERRUPTIBLE);
240 add_wait_queue(&pool->output_sleep, &wait);
241 spin_unlock_irq(&pool->lock);
243 spin_lock_irq(&pool->lock);
244 remove_wait_queue(&pool->output_sleep, &wait);
246 if (signal_pending(current)) {
251 if (pool->closing) { /* closing.. */
259 pool->free = cell->next;
260 atomic_inc(&pool->counter);
261 used = atomic_read(&pool->counter);
262 if (pool->max_used < used)
263 pool->max_used = used;
264 pool->event_alloc_success++;
265 /* clear cell pointers */
269 pool->event_alloc_failures++;
273 spin_unlock_irqrestore(&pool->lock, flags);
279 * duplicate the event to a cell.
280 * if the event has external data, the data is decomposed to additional
283 int snd_seq_event_dup(pool_t *pool, snd_seq_event_t *event, snd_seq_event_cell_t **cellp, int nonblock, struct file *file)
287 snd_seq_event_cell_t *cell;
293 if (snd_seq_ev_is_variable(event)) {
294 extlen = event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
295 ncells = (extlen + sizeof(snd_seq_event_t) - 1) / sizeof(snd_seq_event_t);
297 if (ncells >= pool->total_elements)
300 err = snd_seq_cell_alloc(pool, &cell, nonblock, file);
305 cell->event = *event;
308 if (snd_seq_ev_is_variable(event)) {
310 int is_chained = event->data.ext.len & SNDRV_SEQ_EXT_CHAINED;
311 int is_usrptr = event->data.ext.len & SNDRV_SEQ_EXT_USRPTR;
312 snd_seq_event_cell_t *src, *tmp, *tail;
315 cell->event.data.ext.len = extlen | SNDRV_SEQ_EXT_CHAINED;
316 cell->event.data.ext.ptr = NULL;
318 src = (snd_seq_event_cell_t*)event->data.ext.ptr;
319 buf = (char *)event->data.ext.ptr;
322 while (ncells-- > 0) {
323 int size = sizeof(snd_seq_event_t);
326 err = snd_seq_cell_alloc(pool, &tmp, nonblock, file);
329 if (cell->event.data.ext.ptr == NULL)
330 cell->event.data.ext.ptr = tmp;
335 if (is_chained && src) {
336 tmp->event = src->event;
338 } else if (is_usrptr) {
339 if (copy_from_user(&tmp->event, (char __user *)buf, size)) {
344 memcpy(&tmp->event, buf, size);
355 snd_seq_cell_free(cell);
361 int snd_seq_pool_poll_wait(pool_t *pool, struct file *file, poll_table *wait)
363 poll_wait(file, &pool->output_sleep, wait);
364 return snd_seq_output_ok(pool);
368 /* allocate room specified number of events */
369 int snd_seq_pool_init(pool_t *pool)
372 snd_seq_event_cell_t *cellptr;
375 snd_assert(pool != NULL, return -EINVAL);
376 if (pool->ptr) /* should be atomic? */
379 pool->ptr = vmalloc(sizeof(snd_seq_event_cell_t) * pool->size);
380 if (pool->ptr == NULL) {
381 snd_printd("seq: malloc for sequencer events failed\n");
385 /* add new cells to the free cell list */
386 spin_lock_irqsave(&pool->lock, flags);
389 for (cell = 0; cell < pool->size; cell++) {
390 cellptr = pool->ptr + cell;
391 cellptr->pool = pool;
392 cellptr->next = pool->free;
393 pool->free = cellptr;
395 pool->room = (pool->size + 1) / 2;
397 /* init statistics */
399 pool->total_elements = pool->size;
400 spin_unlock_irqrestore(&pool->lock, flags);
405 int snd_seq_pool_done(pool_t *pool)
408 snd_seq_event_cell_t *ptr;
409 int max_count = 5 * HZ;
411 snd_assert(pool != NULL, return -EINVAL);
413 /* wait for closing all threads */
414 spin_lock_irqsave(&pool->lock, flags);
416 spin_unlock_irqrestore(&pool->lock, flags);
418 if (waitqueue_active(&pool->output_sleep))
419 wake_up(&pool->output_sleep);
421 while (atomic_read(&pool->counter) > 0) {
422 if (max_count == 0) {
423 snd_printk(KERN_WARNING "snd_seq_pool_done timeout: %d cells remain\n", atomic_read(&pool->counter));
426 set_current_state(TASK_UNINTERRUPTIBLE);
431 /* release all resources */
432 spin_lock_irqsave(&pool->lock, flags);
436 pool->total_elements = 0;
437 spin_unlock_irqrestore(&pool->lock, flags);
441 spin_lock_irqsave(&pool->lock, flags);
443 spin_unlock_irqrestore(&pool->lock, flags);
449 /* init new memory pool */
450 pool_t *snd_seq_pool_new(int poolsize)
454 /* create pool block */
455 pool = kcalloc(1, sizeof(*pool), GFP_KERNEL);
457 snd_printd("seq: malloc failed for pool\n");
460 spin_lock_init(&pool->lock);
463 pool->total_elements = 0;
464 atomic_set(&pool->counter, 0);
466 init_waitqueue_head(&pool->output_sleep);
468 pool->size = poolsize;
470 /* init statistics */
475 /* remove memory pool */
476 int snd_seq_pool_delete(pool_t **ppool)
478 pool_t *pool = *ppool;
483 snd_seq_pool_done(pool);
488 /* initialize sequencer memory */
489 int __init snd_sequencer_memory_init(void)
494 /* release sequencer memory */
495 void __exit snd_sequencer_memory_done(void)
500 /* exported to seq_clientmgr.c */
501 void snd_seq_info_pool(snd_info_buffer_t * buffer, pool_t *pool, char *space)
505 snd_iprintf(buffer, "%sPool size : %d\n", space, pool->total_elements);
506 snd_iprintf(buffer, "%sCells in use : %d\n", space, atomic_read(&pool->counter));
507 snd_iprintf(buffer, "%sPeak cells in use : %d\n", space, pool->max_used);
508 snd_iprintf(buffer, "%sAlloc success : %d\n", space, pool->event_alloc_success);
509 snd_iprintf(buffer, "%sAlloc failures : %d\n", space, pool->event_alloc_failures);