2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/info.h>
29 #include <sound/initval.h>
31 static int preallocate_dma = 1;
32 MODULE_PARM(preallocate_dma, "i");
33 MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
34 MODULE_PARM_SYNTAX(preallocate_dma, SNDRV_BOOLEAN_TRUE_DESC);
36 static int maximum_substreams = 4;
37 MODULE_PARM(maximum_substreams, "i");
38 MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
39 MODULE_PARM_SYNTAX(maximum_substreams, SNDRV_BOOLEAN_TRUE_DESC);
41 const static size_t snd_minimum_buffer = 16384;
45 * try to allocate as the large pages as possible.
46 * stores the resultant memory size in *res_size.
48 * the minimum size is snd_minimum_buffer. it should be power of 2.
50 static int preallocate_pcm_pages(snd_pcm_substream_t *substream, size_t size)
52 struct snd_dma_buffer *dmab = &substream->dma_buffer;
55 snd_assert(size > 0, return -EINVAL);
57 /* already reserved? */
58 if (snd_dma_get_reserved(&substream->dma_device, dmab) > 0) {
59 if (dmab->bytes >= size)
61 /* no, reset the reserved block */
62 /* if we can find bigger pages below, this block will be
63 * automatically removed in snd_dma_set_reserved().
65 snd_dma_free_reserved(&substream->dma_device);
70 if ((err = snd_dma_alloc_pages(&substream->dma_device, size, dmab)) < 0) {
72 return err; /* fatal error */
74 /* remember this one */
75 snd_dma_set_reserved(&substream->dma_device, dmab);
79 } while (size >= snd_minimum_buffer);
80 dmab->bytes = 0; /* tell error */
85 * release the preallocated buffer if not yet done.
87 static void snd_pcm_lib_preallocate_dma_free(snd_pcm_substream_t *substream)
89 if (substream->dma_buffer.area == NULL)
91 snd_dma_free_reserved(&substream->dma_device);
92 substream->dma_buffer.area = NULL;
96 * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
97 * @substream: the pcm substream instance
99 * Releases the pre-allocated buffer of the given substream.
101 * Returns zero if successful, or a negative error code on failure.
103 int snd_pcm_lib_preallocate_free(snd_pcm_substream_t *substream)
105 snd_pcm_lib_preallocate_dma_free(substream);
106 if (substream->proc_prealloc_entry) {
107 snd_info_unregister(substream->proc_prealloc_entry);
108 substream->proc_prealloc_entry = NULL;
110 substream->dma_device.type = SNDRV_DMA_TYPE_UNKNOWN;
115 * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
116 * @pcm: the pcm instance
118 * Releases all the pre-allocated buffers on the given pcm.
120 * Returns zero if successful, or a negative error code on failure.
122 int snd_pcm_lib_preallocate_free_for_all(snd_pcm_t *pcm)
124 snd_pcm_substream_t *substream;
127 for (stream = 0; stream < 2; stream++)
128 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
129 snd_pcm_lib_preallocate_free(substream);
134 * read callback for prealloc proc file
136 * prints the current allocated size in kB.
138 static void snd_pcm_lib_preallocate_proc_read(snd_info_entry_t *entry,
139 snd_info_buffer_t *buffer)
141 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
142 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
146 * write callback for prealloc proc file
148 * accepts the preallocation size in kB.
150 static void snd_pcm_lib_preallocate_proc_write(snd_info_entry_t *entry,
151 snd_info_buffer_t *buffer)
153 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
154 char line[64], str[64];
156 struct snd_dma_buffer new_dmab;
158 if (substream->runtime) {
159 buffer->error = -EBUSY;
162 if (!snd_info_get_line(buffer, line, sizeof(line))) {
163 snd_info_get_str(str, line, sizeof(str));
164 size = simple_strtoul(str, NULL, 10) * 1024;
165 if ((size != 0 && size < 8192) || size > substream->dma_max) {
166 buffer->error = -EINVAL;
169 if (substream->dma_buffer.bytes == size)
171 memset(&new_dmab, 0, sizeof(new_dmab));
174 if (snd_dma_alloc_pages(&substream->dma_device, size, &new_dmab) < 0) {
175 buffer->error = -ENOMEM;
178 substream->buffer_bytes_max = size;
179 snd_dma_free_reserved(&substream->dma_device);
181 substream->buffer_bytes_max = UINT_MAX;
183 snd_dma_set_reserved(&substream->dma_device, &new_dmab);
184 substream->dma_buffer = new_dmab;
186 buffer->error = -EINVAL;
191 * pre-allocate the buffer and create a proc file for the substream
193 static int snd_pcm_lib_preallocate_pages1(snd_pcm_substream_t *substream,
194 size_t size, size_t max)
196 snd_info_entry_t *entry;
198 memset(&substream->dma_buffer, 0, sizeof(substream->dma_buffer));
199 if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
200 preallocate_pcm_pages(substream, size);
202 if (substream->dma_buffer.bytes > 0)
203 substream->buffer_bytes_max = substream->dma_buffer.bytes;
204 substream->dma_max = max;
205 if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc", substream->proc_root)) != NULL) {
206 entry->c.text.read_size = 64;
207 entry->c.text.read = snd_pcm_lib_preallocate_proc_read;
208 entry->c.text.write_size = 64;
209 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
210 entry->private_data = substream;
211 if (snd_info_register(entry) < 0) {
212 snd_info_free_entry(entry);
216 substream->proc_prealloc_entry = entry;
222 * set up the unique pcm id
224 static inline void setup_pcm_id(snd_pcm_substream_t *subs)
226 if (! subs->dma_device.id)
227 subs->dma_device.id = subs->pcm->device << 16 |
228 subs->stream << 8 | (subs->number + 1);
232 * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
233 * @substream: the pcm substream instance
234 * @type: DMA type (SNDRV_DMA_TYPE_*)
235 * @data: DMA type dependant data
236 * @size: the requested pre-allocation size in bytes
237 * @max: the max. allowed pre-allocation size
239 * Do pre-allocation for the given DMA type.
241 * Returns zero if successful, or a negative error code on failure.
243 int snd_pcm_lib_preallocate_pages(snd_pcm_substream_t *substream,
244 int type, struct device *data,
245 size_t size, size_t max)
247 substream->dma_device.type = type;
248 substream->dma_device.dev = data;
249 setup_pcm_id(substream);
250 return snd_pcm_lib_preallocate_pages1(substream, size, max);
254 * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continous memory type (all substreams)
255 * @substream: the pcm substream instance
256 * @type: DMA type (SNDRV_DMA_TYPE_*)
257 * @data: DMA type dependant data
258 * @size: the requested pre-allocation size in bytes
259 * @max: the max. allowed pre-allocation size
261 * Do pre-allocation to all substreams of the given pcm for the
262 * specified DMA type.
264 * Returns zero if successful, or a negative error code on failure.
266 int snd_pcm_lib_preallocate_pages_for_all(snd_pcm_t *pcm,
267 int type, void *data,
268 size_t size, size_t max)
270 snd_pcm_substream_t *substream;
273 for (stream = 0; stream < 2; stream++)
274 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
275 if ((err = snd_pcm_lib_preallocate_pages(substream, type, data, size, max)) < 0)
281 * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
282 * @substream: the pcm substream instance
283 * @offset: the buffer offset
285 * Returns the page struct at the given buffer offset.
286 * Used as the page callback of PCM ops.
288 struct page *snd_pcm_sgbuf_ops_page(snd_pcm_substream_t *substream, unsigned long offset)
290 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
292 unsigned int idx = offset >> PAGE_SHIFT;
293 if (idx >= (unsigned int)sgbuf->pages)
295 return sgbuf->page_table[idx];
299 * snd_pcm_lib_malloc_pages - allocate the DMA buffer
300 * @substream: the substream to allocate the DMA buffer to
301 * @size: the requested buffer size in bytes
303 * Allocates the DMA buffer on the BUS type given by
304 * snd_pcm_lib_preallocate_xxx_pages().
306 * Returns 1 if the buffer is changed, 0 if not changed, or a negative
309 int snd_pcm_lib_malloc_pages(snd_pcm_substream_t *substream, size_t size)
311 snd_pcm_runtime_t *runtime;
312 struct snd_dma_buffer dmab;
314 snd_assert(substream->dma_device.type != SNDRV_DMA_TYPE_UNKNOWN, return -EINVAL);
315 snd_assert(substream != NULL, return -EINVAL);
316 runtime = substream->runtime;
317 snd_assert(runtime != NULL, return -EINVAL);
319 if (runtime->dma_area != NULL) {
320 /* perphaps, we might free the large DMA memory region
321 to save some space here, but the actual solution
322 costs us less time */
323 if (runtime->dma_bytes >= size)
324 return 0; /* ok, do not change */
325 snd_pcm_lib_free_pages(substream);
327 if (substream->dma_buffer.area != NULL && substream->dma_buffer.bytes >= size) {
328 dmab = substream->dma_buffer; /* use the pre-allocated buffer */
330 memset(&dmab, 0, sizeof(dmab)); /* allocate a new buffer */
331 if (snd_dma_alloc_pages(&substream->dma_device, size, &dmab) < 0)
334 runtime->dma_area = dmab.area;
335 runtime->dma_addr = dmab.addr;
336 runtime->dma_private = dmab.private_data;
337 runtime->dma_bytes = size;
338 return 1; /* area was changed */
342 * snd_pcm_lib_free_pages - release the allocated DMA buffer.
343 * @substream: the substream to release the DMA buffer
345 * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
347 * Returns zero if successful, or a negative error code on failure.
349 int snd_pcm_lib_free_pages(snd_pcm_substream_t *substream)
351 snd_pcm_runtime_t *runtime;
353 snd_assert(substream != NULL, return -EINVAL);
354 runtime = substream->runtime;
355 snd_assert(runtime != NULL, return -EINVAL);
356 if (runtime->dma_area == NULL)
358 if (runtime->dma_area != substream->dma_buffer.area) {
359 /* it's a newly allocated buffer. release it now. */
360 struct snd_dma_buffer dmab;
361 memset(&dmab, 0, sizeof(dmab));
362 dmab.area = runtime->dma_area;
363 dmab.addr = runtime->dma_addr;
364 dmab.bytes = runtime->dma_bytes;
365 dmab.private_data = runtime->dma_private;
366 snd_dma_free_pages(&substream->dma_device, &dmab);
368 runtime->dma_area = NULL;
369 runtime->dma_addr = 0UL;
370 runtime->dma_bytes = 0;
371 runtime->dma_private = NULL;
377 /* format is: snd-pcm=preallocate_dma,maximum_substreams */
379 static int __init alsa_pcm_setup(char *str)
381 (void)(get_option(&str,&preallocate_dma) == 2 &&
382 get_option(&str,&maximum_substreams) == 2);
386 __setup("snd-pcm=", alsa_pcm_setup);
388 #endif /* ifndef MODULE */