~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/core/seq/seq_memory.c

Version: ~ [ linux-5.1-rc1 ] ~ [ linux-5.0.3 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.30 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.107 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.164 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.176 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.136 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  ALSA sequencer Memory Manager
  3  *  Copyright (c) 1998 by Frank van de Pol <fvdpol@home.nl>
  4  *                        Jaroslav Kysela <perex@suse.cz>
  5  *                2000 by Takashi Iwai <tiwai@suse.de>
  6  *
  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.
 11  *
 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.
 16  *
 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
 20  *
 21  */
 22 
 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>
 28 
 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"
 34 
 35 /* semaphore in struct file record */
 36 #define semaphore_of(fp)        ((fp)->f_dentry->d_inode->i_sem)
 37 
 38 
 39 inline static int snd_seq_pool_available(pool_t *pool)
 40 {
 41         return pool->total_elements - atomic_read(&pool->counter);
 42 }
 43 
 44 inline static int snd_seq_output_ok(pool_t *pool)
 45 {
 46         return snd_seq_pool_available(pool) >= pool->room;
 47 }
 48 
 49 /*
 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 -> ..
 68  */
 69 
 70 /*
 71  * exported:
 72  * call dump function to expand external data.
 73  */
 74 
 75 static int get_var_len(const snd_seq_event_t *event)
 76 {
 77         if ((event->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
 78                 return -EINVAL;
 79 
 80         return event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
 81 }
 82 
 83 int snd_seq_dump_var_event(const snd_seq_event_t *event, snd_seq_dump_func_t func, void *private_data)
 84 {
 85         int len, err;
 86         snd_seq_event_cell_t *cell;
 87 
 88         if ((len = get_var_len(event)) <= 0)
 89                 return len;
 90 
 91         if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
 92                 char buf[32];
 93                 char *curptr = 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) < 0)
 99                                 return -EFAULT;
100                         err = func(private_data, buf, size);
101                         if (err < 0)
102                                 return err;
103                         curptr += size;
104                         len -= size;
105                 }
106                 return 0;
107         } if (! (event->data.ext.len & SNDRV_SEQ_EXT_CHAINED)) {
108                 return func(private_data, event->data.ext.ptr, len);
109         }
110 
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;
120         }
121         return 0;
122 }
123 
124 
125 /*
126  * exported:
127  * expand the variable length event to linear buffer space.
128  */
129 
130 static int seq_copy_in_kernel(char **bufptr, const void *src, int size)
131 {
132         memcpy(*bufptr, src, size);
133         *bufptr += size;
134         return 0;
135 }
136 
137 static int seq_copy_in_user(char **bufptr, const void *src, int size)
138 {
139         if (copy_to_user(*bufptr, src, size))
140                 return -EFAULT;
141         *bufptr += size;
142         return 0;
143 }
144 
145 int snd_seq_expand_var_event(const snd_seq_event_t *event, int count, char *buf, int in_kernel, int size_aligned)
146 {
147         int len, newlen;
148         int err;
149 
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;
157 
158         if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
159                 if (! in_kernel)
160                         return -EINVAL;
161                 if (copy_from_user(buf, event->data.ext.ptr, len) < 0)
162                         return -EFAULT;
163                 return newlen;
164         }
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;
170 }
171 
172 
173 /*
174  * release this cell, free extended data if available
175  */
176 
177 static inline void free_cell(pool_t *pool, snd_seq_event_cell_t *cell)
178 {
179         cell->next = pool->free;
180         pool->free = cell;
181         atomic_dec(&pool->counter);
182 }
183 
184 void snd_seq_cell_free(snd_seq_event_cell_t * cell)
185 {
186         unsigned long flags;
187         pool_t *pool;
188 
189         snd_assert(cell != NULL, return);
190         pool = cell->pool;
191         snd_assert(pool != NULL, return);
192 
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);
203                         }
204                 }
205         }
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);
210         }
211         spin_unlock_irqrestore(&pool->lock, flags);
212 }
213 
214 
215 /*
216  * allocate an event cell.
217  */
218 int snd_seq_cell_alloc(pool_t *pool, snd_seq_event_cell_t **cellp, int nonblock, struct file *file)
219 {
220         snd_seq_event_cell_t *cell;
221         unsigned long flags;
222         int err = -EAGAIN;
223 
224         if (pool == NULL)
225                 return -EINVAL;
226 
227         *cellp = NULL;
228 
229         spin_lock_irqsave(&pool->lock, flags);
230         if (pool->ptr == NULL) {        /* not initialized */
231                 snd_printd("seq: pool is not initialized\n");
232                 err = -EINVAL;
233                 goto __error;
234         }
235         while (pool->free == NULL && ! nonblock && ! pool->closing) {
236 
237                 spin_unlock(&pool->lock);
238                 interruptible_sleep_on(&pool->output_sleep);
239                 spin_lock(&pool->lock);
240                 /* interrupted? */
241                 if (signal_pending(current)) {
242                         err = -ERESTARTSYS;
243                         goto __error;
244                 }
245         }
246         if (pool->closing) { /* closing.. */
247                 err = -ENOMEM;
248                 goto __error;
249         }
250 
251         cell = pool->free;
252         if (cell) {
253                 int used;
254                 pool->free = cell->next;
255                 atomic_inc(&pool->counter);
256                 used = atomic_read(&pool->counter);
257                 if (pool->max_used < used)
258                         pool->max_used = used;
259                 pool->event_alloc_success++;
260                 /* clear cell pointers */
261                 cell->next = NULL;
262                 err = 0;
263         } else
264                 pool->event_alloc_failures++;
265         *cellp = cell;
266 
267 __error:
268         spin_unlock_irqrestore(&pool->lock, flags);
269         return err;
270 }
271 
272 
273 /*
274  * duplicate the event to a cell.
275  * if the event has external data, the data is decomposed to additional
276  * cells.
277  */
278 int snd_seq_event_dup(pool_t *pool, snd_seq_event_t *event, snd_seq_event_cell_t **cellp, int nonblock, struct file *file)
279 {
280         int ncells, err;
281         unsigned int extlen;
282         snd_seq_event_cell_t *cell;
283 
284         *cellp = NULL;
285 
286         ncells = 0;
287         extlen = 0;
288         if (snd_seq_ev_is_variable(event)) {
289                 extlen = event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
290                 ncells = (extlen + sizeof(snd_seq_event_t) - 1) / sizeof(snd_seq_event_t);
291         }
292         if (ncells >= pool->total_elements)
293                 return -ENOMEM;
294 
295         err = snd_seq_cell_alloc(pool, &cell, nonblock, file);
296         if (err < 0)
297                 return err;
298 
299         /* copy the event */
300         cell->event = *event;
301 
302         /* decompose */
303         if (snd_seq_ev_is_variable(event)) {
304                 int len = extlen;
305                 int is_chained = event->data.ext.len & SNDRV_SEQ_EXT_CHAINED;
306                 int is_usrptr = event->data.ext.len & SNDRV_SEQ_EXT_USRPTR;
307                 snd_seq_event_cell_t *src, *tmp, *tail;
308                 char *buf;
309 
310                 cell->event.data.ext.len = extlen | SNDRV_SEQ_EXT_CHAINED;
311                 cell->event.data.ext.ptr = NULL;
312 
313                 src = (snd_seq_event_cell_t*)event->data.ext.ptr;
314                 buf = (char *)event->data.ext.ptr;
315                 tail = NULL;
316 
317                 while (ncells-- > 0) {
318                         int size = sizeof(snd_seq_event_t);
319                         if (len < size)
320                                 size = len;
321                         err = snd_seq_cell_alloc(pool, &tmp, nonblock, file);
322                         if (err < 0)
323                                 goto __error;
324                         if (cell->event.data.ext.ptr == NULL)
325                                 cell->event.data.ext.ptr = tmp;
326                         if (tail)
327                                 tail->next = tmp;
328                         tail = tmp;
329                         /* copy chunk */
330                         if (is_chained && src) {
331                                 tmp->event = src->event;
332                                 src = src->next;
333                         } else if (is_usrptr) {
334                                 if (copy_from_user(&tmp->event, buf, size)) {
335                                         err = -EFAULT;
336                                         goto __error;
337                                 }
338                         } else {
339                                 memcpy(&tmp->event, buf, size);
340                         }
341                         buf += size;
342                         len -= size;
343                 }
344         }
345 
346         *cellp = cell;
347         return 0;
348 
349 __error:
350         snd_seq_cell_free(cell);
351         return err;
352 }
353   
354 
355 /* poll wait */
356 int snd_seq_pool_poll_wait(pool_t *pool, struct file *file, poll_table *wait)
357 {
358         poll_wait(file, &pool->output_sleep, wait);
359         return snd_seq_output_ok(pool);
360 }
361 
362 
363 /* allocate room specified number of events */
364 int snd_seq_pool_init(pool_t *pool)
365 {
366         int cell;
367         snd_seq_event_cell_t *cellptr;
368         unsigned long flags;
369 
370         snd_assert(pool != NULL, return -EINVAL);
371         if (pool->ptr)                  /* should be atomic? */
372                 return 0;
373 
374         pool->ptr = vmalloc(sizeof(snd_seq_event_cell_t) * pool->size);
375         if (pool->ptr == NULL) {
376                 snd_printd("seq: malloc for sequencer events failed\n");
377                 return -ENOMEM;
378         }
379 
380         /* add new cells to the free cell list */
381         spin_lock_irqsave(&pool->lock, flags);
382         pool->free = NULL;
383 
384         for (cell = 0; cell < pool->size; cell++) {
385                 cellptr = pool->ptr + cell;
386                 cellptr->pool = pool;
387                 cellptr->next = pool->free;
388                 pool->free = cellptr;
389         }
390         pool->room = (pool->size + 1) / 2;
391 
392         /* init statistics */
393         pool->max_used = 0;
394         pool->total_elements = pool->size;
395         spin_unlock_irqrestore(&pool->lock, flags);
396         return 0;
397 }
398 
399 /* remove events */
400 int snd_seq_pool_done(pool_t *pool)
401 {
402         unsigned long flags;
403         snd_seq_event_cell_t *ptr;
404         int max_count = 5 * HZ;
405 
406         snd_assert(pool != NULL, return -EINVAL);
407 
408         /* wait for closing all threads */
409         spin_lock_irqsave(&pool->lock, flags);
410         pool->closing = 1;
411         spin_unlock_irqrestore(&pool->lock, flags);
412 
413         if (waitqueue_active(&pool->output_sleep))
414                 wake_up(&pool->output_sleep);
415 
416         while (atomic_read(&pool->counter) > 0) {
417                 if (max_count == 0) {
418                         snd_printk(KERN_WARNING "snd_seq_pool_done timeout: %d cells remain\n", atomic_read(&pool->counter));
419                         break;
420                 }
421                 set_current_state(TASK_UNINTERRUPTIBLE);
422                 schedule_timeout(1);
423                 max_count--;
424         }
425         
426         /* release all resources */
427         spin_lock_irqsave(&pool->lock, flags);
428         ptr = pool->ptr;
429         pool->ptr = NULL;
430         pool->free = NULL;
431         pool->total_elements = 0;
432         spin_unlock_irqrestore(&pool->lock, flags);
433 
434         if (ptr)
435                 vfree(ptr);
436 
437         spin_lock_irqsave(&pool->lock, flags);
438         pool->closing = 0;
439         spin_unlock_irqrestore(&pool->lock, flags);
440 
441         return 0;
442 }
443 
444 
445 /* init new memory pool */
446 pool_t *snd_seq_pool_new(int poolsize)
447 {
448         pool_t *pool;
449 
450         /* create pool block */
451         pool = snd_kcalloc(sizeof(pool_t), GFP_KERNEL);
452         if (pool == NULL) {
453                 snd_printd("seq: malloc failed for pool\n");
454                 return NULL;
455         }
456         spin_lock_init(&pool->lock);
457         pool->ptr = NULL;
458         pool->free = NULL;
459         pool->total_elements = 0;
460         atomic_set(&pool->counter, 0);
461         pool->closing = 0;
462         init_waitqueue_head(&pool->output_sleep);
463         
464         pool->size = poolsize;
465 
466         /* init statistics */
467         pool->max_used = 0;
468         return pool;
469 }
470 
471 /* remove memory pool */
472 int snd_seq_pool_delete(pool_t **ppool)
473 {
474         pool_t *pool = *ppool;
475 
476         *ppool = NULL;
477         if (pool == NULL)
478                 return 0;
479         snd_seq_pool_done(pool);
480         kfree(pool);
481         return 0;
482 }
483 
484 /* initialize sequencer memory */
485 int __init snd_sequencer_memory_init(void)
486 {
487         return 0;
488 }
489 
490 /* release sequencer memory */
491 void __exit snd_sequencer_memory_done(void)
492 {
493 }
494 
495 
496 /* exported to seq_clientmgr.c */
497 void snd_seq_info_pool(snd_info_buffer_t * buffer, pool_t *pool, char *space)
498 {
499         if (pool == NULL)
500                 return;
501         snd_iprintf(buffer, "%sPool size          : %d\n", space, pool->total_elements);
502         snd_iprintf(buffer, "%sCells in use       : %d\n", space, atomic_read(&pool->counter));
503         snd_iprintf(buffer, "%sPeak cells in use  : %d\n", space, pool->max_used);
504         snd_iprintf(buffer, "%sAlloc success      : %d\n", space, pool->event_alloc_success);
505         snd_iprintf(buffer, "%sAlloc failures     : %d\n", space, pool->event_alloc_failures);
506 }
507 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp