vserver 1.9.3
[linux-2.6.git] / sound / core / pcm_memory.c
index ab70ba7..16a8124 100644 (file)
 static int preallocate_dma = 1;
 module_param(preallocate_dma, int, 0444);
 MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
-MODULE_PARM_SYNTAX(preallocate_dma, SNDRV_BOOLEAN_TRUE_DESC);
 
 static int maximum_substreams = 4;
 module_param(maximum_substreams, int, 0444);
 MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
-MODULE_PARM_SYNTAX(maximum_substreams, SNDRV_BOOLEAN_TRUE_DESC);
 
 const static size_t snd_minimum_buffer = 16384;
 
@@ -56,26 +54,21 @@ static int preallocate_pcm_pages(snd_pcm_substream_t *substream, size_t size)
        snd_assert(size > 0, return -EINVAL);
 
        /* already reserved? */
-       if (snd_dma_get_reserved(&substream->dma_device, dmab) > 0) {
+       if (snd_dma_get_reserved_buf(dmab, substream->dma_buf_id) > 0) {
                if (dmab->bytes >= size)
                        return 0; /* yes */
-               /* no, reset the reserved block */
-               /* if we can find bigger pages below, this block will be
-                * automatically removed in snd_dma_set_reserved().
-                */
-               snd_dma_free_reserved(&substream->dma_device);
+               /* no, free the reserved block */
+               snd_dma_free_pages(dmab);
                dmab->bytes = 0;
        }
 
        do {
-               if ((err = snd_dma_alloc_pages(&substream->dma_device, size, dmab)) < 0) {
+               if ((err = snd_dma_alloc_pages(dmab->dev.type, dmab->dev.dev,
+                                              size, dmab)) < 0) {
                        if (err != -ENOMEM)
                                return err; /* fatal error */
-               } else {
-                       /* remember this one */
-                       snd_dma_set_reserved(&substream->dma_device, dmab);
+               } else
                        return 0;
-               }
                size >>= 1;
        } while (size >= snd_minimum_buffer);
        dmab->bytes = 0; /* tell error */
@@ -89,7 +82,10 @@ static void snd_pcm_lib_preallocate_dma_free(snd_pcm_substream_t *substream)
 {
        if (substream->dma_buffer.area == NULL)
                return;
-       snd_dma_free_reserved(&substream->dma_device);
+       if (substream->dma_buf_id)
+               snd_dma_reserve_buf(&substream->dma_buffer, substream->dma_buf_id);
+       else
+               snd_dma_free_pages(&substream->dma_buffer);
        substream->dma_buffer.area = NULL;
 }
 
@@ -108,7 +104,6 @@ int snd_pcm_lib_preallocate_free(snd_pcm_substream_t *substream)
                snd_info_unregister(substream->proc_prealloc_entry);
                substream->proc_prealloc_entry = NULL;
        }
-       substream->dma_device.type = SNDRV_DMA_TYPE_UNKNOWN;
        return 0;
 }
 
@@ -170,18 +165,20 @@ static void snd_pcm_lib_preallocate_proc_write(snd_info_entry_t *entry,
                if (substream->dma_buffer.bytes == size)
                        return;
                memset(&new_dmab, 0, sizeof(new_dmab));
+               new_dmab.dev = substream->dma_buffer.dev;
                if (size > 0) {
-
-                       if (snd_dma_alloc_pages(&substream->dma_device, size, &new_dmab) < 0) {
+                       if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
+                                               substream->dma_buffer.dev.dev,
+                                               size, &new_dmab) < 0) {
                                buffer->error = -ENOMEM;
                                return;
                        }
                        substream->buffer_bytes_max = size;
-                       snd_dma_free_reserved(&substream->dma_device);
                } else {
                        substream->buffer_bytes_max = UINT_MAX;
                }
-               snd_dma_set_reserved(&substream->dma_device, &new_dmab);
+               if (substream->dma_buffer.area)
+                       snd_dma_free_pages(&substream->dma_buffer);
                substream->dma_buffer = new_dmab;
        } else {
                buffer->error = -EINVAL;
@@ -196,7 +193,6 @@ static int snd_pcm_lib_preallocate_pages1(snd_pcm_substream_t *substream,
 {
        snd_info_entry_t *entry;
 
-       memset(&substream->dma_buffer, 0, sizeof(substream->dma_buffer));
        if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
                preallocate_pcm_pages(substream, size);
 
@@ -219,20 +215,6 @@ static int snd_pcm_lib_preallocate_pages1(snd_pcm_substream_t *substream,
 }
 
 
-/*
- * set up the unique pcm id
- */
-static inline void setup_pcm_id(snd_pcm_substream_t *subs)
-{
-       if (! subs->dma_device.id) {
-               subs->dma_device.id = subs->pcm->device << 16 |
-                       subs->stream << 8 | (subs->number + 1);
-               if (subs->dma_device.type == SNDRV_DMA_TYPE_CONTINUOUS ||
-                   subs->dma_device.dev == NULL)
-                       subs->dma_device.id |= (subs->pcm->card->number + 1) << 24;
-       }
-}
-
 /**
  * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
  * @substream: the pcm substream instance
@@ -241,7 +223,12 @@ static inline void setup_pcm_id(snd_pcm_substream_t *subs)
  * @size: the requested pre-allocation size in bytes
  * @max: the max. allowed pre-allocation size
  *
- * Do pre-allocation for the given DMA type.
+ * Do pre-allocation for the given DMA buffer type.
+ *
+ * When substream->dma_buf_id is set, the function tries to look for
+ * the reserved buffer, and the buffer is not freed but reserved at
+ * destruction time.  The dma_buf_id must be unique for all systems
+ * (in the same DMA buffer type) e.g. using snd_dma_pci_buf_id().
  *
  * Returns zero if successful, or a negative error code on failure.
  */
@@ -249,9 +236,8 @@ int snd_pcm_lib_preallocate_pages(snd_pcm_substream_t *substream,
                                  int type, struct device *data,
                                  size_t size, size_t max)
 {
-       substream->dma_device.type = type;
-       substream->dma_device.dev = data;
-       setup_pcm_id(substream);
+       substream->dma_buffer.dev.type = type;
+       substream->dma_buffer.dev.dev = data;
        return snd_pcm_lib_preallocate_pages1(substream, size, max);
 }
 
@@ -314,31 +300,38 @@ struct page *snd_pcm_sgbuf_ops_page(snd_pcm_substream_t *substream, unsigned lon
 int snd_pcm_lib_malloc_pages(snd_pcm_substream_t *substream, size_t size)
 {
        snd_pcm_runtime_t *runtime;
-       struct snd_dma_buffer dmab;
+       struct snd_dma_buffer *dmab = NULL;
 
-       snd_assert(substream->dma_device.type != SNDRV_DMA_TYPE_UNKNOWN, return -EINVAL);
+       snd_assert(substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_UNKNOWN, return -EINVAL);
        snd_assert(substream != NULL, return -EINVAL);
        runtime = substream->runtime;
-       snd_assert(runtime != NULL, return -EINVAL);    
+       snd_assert(runtime != NULL, return -EINVAL);
 
-       if (runtime->dma_area != NULL) {
+       if (runtime->dma_buffer_p) {
                /* perphaps, we might free the large DMA memory region
                   to save some space here, but the actual solution
                   costs us less time */
-               if (runtime->dma_bytes >= size)
+               if (runtime->dma_buffer_p->bytes >= size) {
+                       runtime->dma_bytes = size;
                        return 0;       /* ok, do not change */
+               }
                snd_pcm_lib_free_pages(substream);
        }
        if (substream->dma_buffer.area != NULL && substream->dma_buffer.bytes >= size) {
-               dmab = substream->dma_buffer; /* use the pre-allocated buffer */
+               dmab = &substream->dma_buffer; /* use the pre-allocated buffer */
        } else {
-               memset(&dmab, 0, sizeof(dmab)); /* allocate a new buffer */
-               if (snd_dma_alloc_pages(&substream->dma_device, size, &dmab) < 0)
+               dmab = kcalloc(1, sizeof(*dmab), GFP_KERNEL);
+               if (! dmab)
+                       return -ENOMEM;
+               dmab->dev = substream->dma_buffer.dev;
+               if (snd_dma_alloc_pages(substream->dma_buffer.dev.type,
+                                       substream->dma_buffer.dev.dev,
+                                       size, dmab) < 0) {
+                       kfree(dmab);
                        return -ENOMEM;
+               }
        }
-       runtime->dma_area = dmab.area;
-       runtime->dma_addr = dmab.addr;
-       runtime->dma_private = dmab.private_data;
+       snd_pcm_set_runtime_buffer(substream, dmab);
        runtime->dma_bytes = size;
        return 1;                       /* area was changed */
 }
@@ -360,34 +353,11 @@ int snd_pcm_lib_free_pages(snd_pcm_substream_t *substream)
        snd_assert(runtime != NULL, return -EINVAL);
        if (runtime->dma_area == NULL)
                return 0;
-       if (runtime->dma_area != substream->dma_buffer.area) {
+       if (runtime->dma_buffer_p != &substream->dma_buffer) {
                /* it's a newly allocated buffer.  release it now. */
-               struct snd_dma_buffer dmab;
-               memset(&dmab, 0, sizeof(dmab));
-               dmab.area = runtime->dma_area;
-               dmab.addr = runtime->dma_addr;
-               dmab.bytes = runtime->dma_bytes;
-               dmab.private_data = runtime->dma_private;
-               snd_dma_free_pages(&substream->dma_device, &dmab);
+               snd_dma_free_pages(runtime->dma_buffer_p);
+               kfree(runtime->dma_buffer_p);
        }
-       runtime->dma_area = NULL;
-       runtime->dma_addr = 0UL;
-       runtime->dma_bytes = 0;
-       runtime->dma_private = NULL;
+       snd_pcm_set_runtime_buffer(substream, NULL);
        return 0;
 }
-
-#ifndef MODULE
-
-/* format is: snd-pcm=preallocate_dma,maximum_substreams */
-
-static int __init alsa_pcm_setup(char *str)
-{
-       (void)(get_option(&str,&preallocate_dma) == 2 &&
-              get_option(&str,&maximum_substreams) == 2);
-       return 1;
-}
-
-__setup("snd-pcm=", alsa_pcm_setup);
-
-#endif /* ifndef MODULE */