[PATCH] dvb: saa7134-dvb must select tda1004x
[linux-ginger.git] / sound / core / seq / seq_memory.c
blob03acb2d519ba6bbf975c18ae19d8e13f0e66328b
1 /*
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"
32 #include "seq_info.h"
33 #include "seq_lock.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.
53 * 1) kernel space
54 * This is the normal case.
55 * ext.data.len = length
56 * ext.data.ptr = buffer pointer
57 * 2) user space
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
62 * 3) chained cells
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 -> ..
71 * exported:
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)
78 return -EINVAL;
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)
85 int len, err;
86 snd_seq_event_cell_t *cell;
88 if ((len = get_var_len(event)) <= 0)
89 return len;
91 if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
92 char buf[32];
93 char __user *curptr = (char __user *)event->data.ext.ptr;
94 while (len > 0) {
95 int size = sizeof(buf);
96 if (len < size)
97 size = len;
98 if (copy_from_user(buf, curptr, size))
99 return -EFAULT;
100 err = func(private_data, buf, size);
101 if (err < 0)
102 return err;
103 curptr += size;
104 len -= size;
106 return 0;
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);
114 if (len < size)
115 size = len;
116 err = func(private_data, &cell->event, size);
117 if (err < 0)
118 return err;
119 len -= size;
121 return 0;
126 * exported:
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);
133 *bufptr += size;
134 return 0;
137 static int seq_copy_in_user(char __user **bufptr, const void *src, int size)
139 if (copy_to_user(*bufptr, src, size))
140 return -EFAULT;
141 *bufptr += size;
142 return 0;
145 int snd_seq_expand_var_event(const snd_seq_event_t *event, int count, char *buf, int in_kernel, int size_aligned)
147 int len, newlen;
148 int err;
150 if ((len = get_var_len(event)) < 0)
151 return len;
152 newlen = len;
153 if (size_aligned > 0)
154 newlen = ((len + size_aligned - 1) / size_aligned) * size_aligned;
155 if (count < newlen)
156 return -EAGAIN;
158 if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
159 if (! in_kernel)
160 return -EINVAL;
161 if (copy_from_user(buf, (void __user *)event->data.ext.ptr, len))
162 return -EFAULT;
163 return newlen;
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,
168 &buf);
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;
180 pool->free = cell;
181 atomic_dec(&pool->counter);
184 void snd_seq_cell_free(snd_seq_event_cell_t * cell)
186 unsigned long flags;
187 pool_t *pool;
189 snd_assert(cell != NULL, return);
190 pool = cell->pool;
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;
221 unsigned long flags;
222 int err = -EAGAIN;
223 wait_queue_t wait;
225 if (pool == NULL)
226 return -EINVAL;
228 *cellp = NULL;
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");
234 err = -EINVAL;
235 goto __error;
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);
242 schedule();
243 spin_lock_irq(&pool->lock);
244 remove_wait_queue(&pool->output_sleep, &wait);
245 /* interrupted? */
246 if (signal_pending(current)) {
247 err = -ERESTARTSYS;
248 goto __error;
251 if (pool->closing) { /* closing.. */
252 err = -ENOMEM;
253 goto __error;
256 cell = pool->free;
257 if (cell) {
258 int used;
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 */
266 cell->next = NULL;
267 err = 0;
268 } else
269 pool->event_alloc_failures++;
270 *cellp = cell;
272 __error:
273 spin_unlock_irqrestore(&pool->lock, flags);
274 return err;
279 * duplicate the event to a cell.
280 * if the event has external data, the data is decomposed to additional
281 * cells.
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)
285 int ncells, err;
286 unsigned int extlen;
287 snd_seq_event_cell_t *cell;
289 *cellp = NULL;
291 ncells = 0;
292 extlen = 0;
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)
298 return -ENOMEM;
300 err = snd_seq_cell_alloc(pool, &cell, nonblock, file);
301 if (err < 0)
302 return err;
304 /* copy the event */
305 cell->event = *event;
307 /* decompose */
308 if (snd_seq_ev_is_variable(event)) {
309 int len = extlen;
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;
313 char *buf;
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;
320 tail = NULL;
322 while (ncells-- > 0) {
323 int size = sizeof(snd_seq_event_t);
324 if (len < size)
325 size = len;
326 err = snd_seq_cell_alloc(pool, &tmp, nonblock, file);
327 if (err < 0)
328 goto __error;
329 if (cell->event.data.ext.ptr == NULL)
330 cell->event.data.ext.ptr = tmp;
331 if (tail)
332 tail->next = tmp;
333 tail = tmp;
334 /* copy chunk */
335 if (is_chained && src) {
336 tmp->event = src->event;
337 src = src->next;
338 } else if (is_usrptr) {
339 if (copy_from_user(&tmp->event, (char __user *)buf, size)) {
340 err = -EFAULT;
341 goto __error;
343 } else {
344 memcpy(&tmp->event, buf, size);
346 buf += size;
347 len -= size;
351 *cellp = cell;
352 return 0;
354 __error:
355 snd_seq_cell_free(cell);
356 return err;
360 /* poll wait */
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)
371 int cell;
372 snd_seq_event_cell_t *cellptr;
373 unsigned long flags;
375 snd_assert(pool != NULL, return -EINVAL);
376 if (pool->ptr) /* should be atomic? */
377 return 0;
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");
382 return -ENOMEM;
385 /* add new cells to the free cell list */
386 spin_lock_irqsave(&pool->lock, flags);
387 pool->free = NULL;
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 */
398 pool->max_used = 0;
399 pool->total_elements = pool->size;
400 spin_unlock_irqrestore(&pool->lock, flags);
401 return 0;
404 /* remove events */
405 int snd_seq_pool_done(pool_t *pool)
407 unsigned long flags;
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);
415 pool->closing = 1;
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));
424 break;
426 set_current_state(TASK_UNINTERRUPTIBLE);
427 schedule_timeout(1);
428 max_count--;
431 /* release all resources */
432 spin_lock_irqsave(&pool->lock, flags);
433 ptr = pool->ptr;
434 pool->ptr = NULL;
435 pool->free = NULL;
436 pool->total_elements = 0;
437 spin_unlock_irqrestore(&pool->lock, flags);
439 vfree(ptr);
441 spin_lock_irqsave(&pool->lock, flags);
442 pool->closing = 0;
443 spin_unlock_irqrestore(&pool->lock, flags);
445 return 0;
449 /* init new memory pool */
450 pool_t *snd_seq_pool_new(int poolsize)
452 pool_t *pool;
454 /* create pool block */
455 pool = kcalloc(1, sizeof(*pool), GFP_KERNEL);
456 if (pool == NULL) {
457 snd_printd("seq: malloc failed for pool\n");
458 return NULL;
460 spin_lock_init(&pool->lock);
461 pool->ptr = NULL;
462 pool->free = NULL;
463 pool->total_elements = 0;
464 atomic_set(&pool->counter, 0);
465 pool->closing = 0;
466 init_waitqueue_head(&pool->output_sleep);
468 pool->size = poolsize;
470 /* init statistics */
471 pool->max_used = 0;
472 return pool;
475 /* remove memory pool */
476 int snd_seq_pool_delete(pool_t **ppool)
478 pool_t *pool = *ppool;
480 *ppool = NULL;
481 if (pool == NULL)
482 return 0;
483 snd_seq_pool_done(pool);
484 kfree(pool);
485 return 0;
488 /* initialize sequencer memory */
489 int __init snd_sequencer_memory_init(void)
491 return 0;
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)
503 if (pool == NULL)
504 return;
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);