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 <linux/moduleparam.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/info.h>
30 #include <sound/initval.h>
32 static int preallocate_dma = 1;
33 module_param(preallocate_dma, int, 0444);
34 MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized.");
35 MODULE_PARM_SYNTAX(preallocate_dma, SNDRV_BOOLEAN_TRUE_DESC);
37 static int maximum_substreams = 4;
38 module_param(maximum_substreams, int, 0444);
39 MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory.");
40 MODULE_PARM_SYNTAX(maximum_substreams, SNDRV_BOOLEAN_TRUE_DESC);
42 const static size_t snd_minimum_buffer = 16384;
46 * try to allocate as the large pages as possible.
47 * stores the resultant memory size in *res_size.
49 * the minimum size is snd_minimum_buffer. it should be power of 2.
51 static int preallocate_pcm_pages(snd_pcm_substream_t *substream, size_t size)
53 struct snd_dma_buffer *dmab = &substream->dma_buffer;
56 snd_assert(size > 0, return -EINVAL);
58 /* already reserved? */
59 if (snd_dma_get_reserved(&substream->dma_device, dmab) > 0) {
60 if (dmab->bytes >= size)
62 /* no, reset the reserved block */
63 /* if we can find bigger pages below, this block will be
64 * automatically removed in snd_dma_set_reserved().
66 snd_dma_free_reserved(&substream->dma_device);
71 if ((err = snd_dma_alloc_pages(&substream->dma_device, size, dmab)) < 0) {
73 return err; /* fatal error */
75 /* remember this one */
76 snd_dma_set_reserved(&substream->dma_device, dmab);
80 } while (size >= snd_minimum_buffer);
81 dmab->bytes = 0; /* tell error */
86 * release the preallocated buffer if not yet done.
88 static void snd_pcm_lib_preallocate_dma_free(snd_pcm_substream_t *substream)
90 if (substream->dma_buffer.area == NULL)
92 snd_dma_free_reserved(&substream->dma_device);
93 substream->dma_buffer.area = NULL;
97 * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
98 * @substream: the pcm substream instance
100 * Releases the pre-allocated buffer of the given substream.
102 * Returns zero if successful, or a negative error code on failure.
104 int snd_pcm_lib_preallocate_free(snd_pcm_substream_t *substream)
106 snd_pcm_lib_preallocate_dma_free(substream);
107 if (substream->proc_prealloc_entry) {
108 snd_info_unregister(substream->proc_prealloc_entry);
109 substream->proc_prealloc_entry = NULL;
111 substream->dma_device.type = SNDRV_DMA_TYPE_UNKNOWN;
116 * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
117 * @pcm: the pcm instance
119 * Releases all the pre-allocated buffers on the given pcm.
121 * Returns zero if successful, or a negative error code on failure.
123 int snd_pcm_lib_preallocate_free_for_all(snd_pcm_t *pcm)
125 snd_pcm_substream_t *substream;
128 for (stream = 0; stream < 2; stream++)
129 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
130 snd_pcm_lib_preallocate_free(substream);
135 * read callback for prealloc proc file
137 * prints the current allocated size in kB.
139 static void snd_pcm_lib_preallocate_proc_read(snd_info_entry_t *entry,
140 snd_info_buffer_t *buffer)
142 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
143 snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024);
147 * write callback for prealloc proc file
149 * accepts the preallocation size in kB.
151 static void snd_pcm_lib_preallocate_proc_write(snd_info_entry_t *entry,
152 snd_info_buffer_t *buffer)
154 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
155 char line[64], str[64];
157 struct snd_dma_buffer new_dmab;
159 if (substream->runtime) {
160 buffer->error = -EBUSY;
163 if (!snd_info_get_line(buffer, line, sizeof(line))) {
164 snd_info_get_str(str, line, sizeof(str));
165 size = simple_strtoul(str, NULL, 10) * 1024;
166 if ((size != 0 && size < 8192) || size > substream->dma_max) {
167 buffer->error = -EINVAL;
170 if (substream->dma_buffer.bytes == size)
172 memset(&new_dmab, 0, sizeof(new_dmab));
175 if (snd_dma_alloc_pages(&substream->dma_device, size, &new_dmab) < 0) {
176 buffer->error = -ENOMEM;
179 substream->buffer_bytes_max = size;
180 snd_dma_free_reserved(&substream->dma_device);
182 substream->buffer_bytes_max = UINT_MAX;
184 snd_dma_set_reserved(&substream->dma_device, &new_dmab);
185 substream->dma_buffer = new_dmab;
187 buffer->error = -EINVAL;
192 * pre-allocate the buffer and create a proc file for the substream
194 static int snd_pcm_lib_preallocate_pages1(snd_pcm_substream_t *substream,
195 size_t size, size_t max)
197 snd_info_entry_t *entry;
199 memset(&substream->dma_buffer, 0, sizeof(substream->dma_buffer));
200 if (size > 0 && preallocate_dma && substream->number < maximum_substreams)
201 preallocate_pcm_pages(substream, size);
203 if (substream->dma_buffer.bytes > 0)
204 substream->buffer_bytes_max = substream->dma_buffer.bytes;
205 substream->dma_max = max;
206 if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc", substream->proc_root)) != NULL) {
207 entry->c.text.read_size = 64;
208 entry->c.text.read = snd_pcm_lib_preallocate_proc_read;
209 entry->c.text.write_size = 64;
210 entry->c.text.write = snd_pcm_lib_preallocate_proc_write;
211 entry->private_data = substream;
212 if (snd_info_register(entry) < 0) {
213 snd_info_free_entry(entry);
217 substream->proc_prealloc_entry = entry;
223 * set up the unique pcm id
225 static inline void setup_pcm_id(snd_pcm_substream_t *subs)
227 if (! subs->dma_device.id) {
228 subs->dma_device.id = subs->pcm->device << 16 |
229 subs->stream << 8 | (subs->number + 1);
230 if (subs->dma_device.type == SNDRV_DMA_TYPE_CONTINUOUS ||
231 subs->dma_device.dev == NULL)
232 subs->dma_device.id |= (subs->pcm->card->number + 1) << 24;
237 * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type
238 * @substream: the pcm substream instance
239 * @type: DMA type (SNDRV_DMA_TYPE_*)
240 * @data: DMA type dependant data
241 * @size: the requested pre-allocation size in bytes
242 * @max: the max. allowed pre-allocation size
244 * Do pre-allocation for the given DMA type.
246 * Returns zero if successful, or a negative error code on failure.
248 int snd_pcm_lib_preallocate_pages(snd_pcm_substream_t *substream,
249 int type, struct device *data,
250 size_t size, size_t max)
252 substream->dma_device.type = type;
253 substream->dma_device.dev = data;
254 setup_pcm_id(substream);
255 return snd_pcm_lib_preallocate_pages1(substream, size, max);
259 * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continous memory type (all substreams)
260 * @substream: the pcm substream instance
261 * @type: DMA type (SNDRV_DMA_TYPE_*)
262 * @data: DMA type dependant data
263 * @size: the requested pre-allocation size in bytes
264 * @max: the max. allowed pre-allocation size
266 * Do pre-allocation to all substreams of the given pcm for the
267 * specified DMA type.
269 * Returns zero if successful, or a negative error code on failure.
271 int snd_pcm_lib_preallocate_pages_for_all(snd_pcm_t *pcm,
272 int type, void *data,
273 size_t size, size_t max)
275 snd_pcm_substream_t *substream;
278 for (stream = 0; stream < 2; stream++)
279 for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
280 if ((err = snd_pcm_lib_preallocate_pages(substream, type, data, size, max)) < 0)
286 * snd_pcm_sgbuf_ops_page - get the page struct at the given offset
287 * @substream: the pcm substream instance
288 * @offset: the buffer offset
290 * Returns the page struct at the given buffer offset.
291 * Used as the page callback of PCM ops.
293 struct page *snd_pcm_sgbuf_ops_page(snd_pcm_substream_t *substream, unsigned long offset)
295 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
297 unsigned int idx = offset >> PAGE_SHIFT;
298 if (idx >= (unsigned int)sgbuf->pages)
300 return sgbuf->page_table[idx];
304 * snd_pcm_lib_malloc_pages - allocate the DMA buffer
305 * @substream: the substream to allocate the DMA buffer to
306 * @size: the requested buffer size in bytes
308 * Allocates the DMA buffer on the BUS type given by
309 * snd_pcm_lib_preallocate_xxx_pages().
311 * Returns 1 if the buffer is changed, 0 if not changed, or a negative
314 int snd_pcm_lib_malloc_pages(snd_pcm_substream_t *substream, size_t size)
316 snd_pcm_runtime_t *runtime;
317 struct snd_dma_buffer dmab;
319 snd_assert(substream->dma_device.type != SNDRV_DMA_TYPE_UNKNOWN, return -EINVAL);
320 snd_assert(substream != NULL, return -EINVAL);
321 runtime = substream->runtime;
322 snd_assert(runtime != NULL, return -EINVAL);
324 if (runtime->dma_area != NULL) {
325 /* perphaps, we might free the large DMA memory region
326 to save some space here, but the actual solution
327 costs us less time */
328 if (runtime->dma_bytes >= size)
329 return 0; /* ok, do not change */
330 snd_pcm_lib_free_pages(substream);
332 if (substream->dma_buffer.area != NULL && substream->dma_buffer.bytes >= size) {
333 dmab = substream->dma_buffer; /* use the pre-allocated buffer */
335 memset(&dmab, 0, sizeof(dmab)); /* allocate a new buffer */
336 if (snd_dma_alloc_pages(&substream->dma_device, size, &dmab) < 0)
339 runtime->dma_area = dmab.area;
340 runtime->dma_addr = dmab.addr;
341 runtime->dma_private = dmab.private_data;
342 runtime->dma_bytes = size;
343 return 1; /* area was changed */
347 * snd_pcm_lib_free_pages - release the allocated DMA buffer.
348 * @substream: the substream to release the DMA buffer
350 * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
352 * Returns zero if successful, or a negative error code on failure.
354 int snd_pcm_lib_free_pages(snd_pcm_substream_t *substream)
356 snd_pcm_runtime_t *runtime;
358 snd_assert(substream != NULL, return -EINVAL);
359 runtime = substream->runtime;
360 snd_assert(runtime != NULL, return -EINVAL);
361 if (runtime->dma_area == NULL)
363 if (runtime->dma_area != substream->dma_buffer.area) {
364 /* it's a newly allocated buffer. release it now. */
365 struct snd_dma_buffer dmab;
366 memset(&dmab, 0, sizeof(dmab));
367 dmab.area = runtime->dma_area;
368 dmab.addr = runtime->dma_addr;
369 dmab.bytes = runtime->dma_bytes;
370 dmab.private_data = runtime->dma_private;
371 snd_dma_free_pages(&substream->dma_device, &dmab);
373 runtime->dma_area = NULL;
374 runtime->dma_addr = 0UL;
375 runtime->dma_bytes = 0;
376 runtime->dma_private = NULL;
382 /* format is: snd-pcm=preallocate_dma,maximum_substreams */
384 static int __init alsa_pcm_setup(char *str)
386 (void)(get_option(&str,&preallocate_dma) == 2 &&
387 get_option(&str,&maximum_substreams) == 2);
391 __setup("snd-pcm=", alsa_pcm_setup);
393 #endif /* ifndef MODULE */