ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / pcm_memory.c
1 /*
2  *  Digital Audio (PCM) abstract layer
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include <sound/driver.h>
23 #include <asm/io.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>
30
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);
35
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);
40
41 const static size_t snd_minimum_buffer = 16384;
42
43
44 /*
45  * try to allocate as the large pages as possible.
46  * stores the resultant memory size in *res_size.
47  *
48  * the minimum size is snd_minimum_buffer.  it should be power of 2.
49  */
50 static int preallocate_pcm_pages(snd_pcm_substream_t *substream, size_t size)
51 {
52         struct snd_dma_buffer *dmab = &substream->dma_buffer;
53         int err;
54
55         snd_assert(size > 0, return -EINVAL);
56
57         /* already reserved? */
58         if (snd_dma_get_reserved(&substream->dma_device, dmab) > 0) {
59                 if (dmab->bytes >= size)
60                         return 0; /* yes */
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().
64                  */
65                 snd_dma_free_reserved(&substream->dma_device);
66                 dmab->bytes = 0;
67         }
68
69         do {
70                 if ((err = snd_dma_alloc_pages(&substream->dma_device, size, dmab)) < 0) {
71                         if (err != -ENOMEM)
72                                 return err; /* fatal error */
73                 } else {
74                         /* remember this one */
75                         snd_dma_set_reserved(&substream->dma_device, dmab);
76                         return 0;
77                 }
78                 size >>= 1;
79         } while (size >= snd_minimum_buffer);
80         dmab->bytes = 0; /* tell error */
81         return 0;
82 }
83
84 /*
85  * release the preallocated buffer if not yet done.
86  */
87 static void snd_pcm_lib_preallocate_dma_free(snd_pcm_substream_t *substream)
88 {
89         if (substream->dma_buffer.area == NULL)
90                 return;
91         snd_dma_free_reserved(&substream->dma_device);
92         substream->dma_buffer.area = NULL;
93 }
94
95 /**
96  * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
97  * @substream: the pcm substream instance
98  *
99  * Releases the pre-allocated buffer of the given substream.
100  *
101  * Returns zero if successful, or a negative error code on failure.
102  */
103 int snd_pcm_lib_preallocate_free(snd_pcm_substream_t *substream)
104 {
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;
109         }
110         substream->dma_device.type = SNDRV_DMA_TYPE_UNKNOWN;
111         return 0;
112 }
113
114 /**
115  * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
116  * @pcm: the pcm instance
117  *
118  * Releases all the pre-allocated buffers on the given pcm.
119  *
120  * Returns zero if successful, or a negative error code on failure.
121  */
122 int snd_pcm_lib_preallocate_free_for_all(snd_pcm_t *pcm)
123 {
124         snd_pcm_substream_t *substream;
125         int stream;
126
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);
130         return 0;
131 }
132
133 /*
134  * read callback for prealloc proc file
135  *
136  * prints the current allocated size in kB.
137  */
138 static void snd_pcm_lib_preallocate_proc_read(snd_info_entry_t *entry,
139                                               snd_info_buffer_t *buffer)
140 {
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);
143 }
144
145 /*
146  * write callback for prealloc proc file
147  *
148  * accepts the preallocation size in kB.
149  */
150 static void snd_pcm_lib_preallocate_proc_write(snd_info_entry_t *entry,
151                                                snd_info_buffer_t *buffer)
152 {
153         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
154         char line[64], str[64];
155         size_t size;
156         struct snd_dma_buffer new_dmab;
157
158         if (substream->runtime) {
159                 buffer->error = -EBUSY;
160                 return;
161         }
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;
167                         return;
168                 }
169                 if (substream->dma_buffer.bytes == size)
170                         return;
171                 memset(&new_dmab, 0, sizeof(new_dmab));
172                 if (size > 0) {
173
174                         if (snd_dma_alloc_pages(&substream->dma_device, size, &new_dmab) < 0) {
175                                 buffer->error = -ENOMEM;
176                                 return;
177                         }
178                         substream->buffer_bytes_max = size;
179                         snd_dma_free_reserved(&substream->dma_device);
180                 } else {
181                         substream->buffer_bytes_max = UINT_MAX;
182                 }
183                 snd_dma_set_reserved(&substream->dma_device, &new_dmab);
184                 substream->dma_buffer = new_dmab;
185         } else {
186                 buffer->error = -EINVAL;
187         }
188 }
189
190 /*
191  * pre-allocate the buffer and create a proc file for the substream
192  */
193 static int snd_pcm_lib_preallocate_pages1(snd_pcm_substream_t *substream,
194                                           size_t size, size_t max)
195 {
196         snd_info_entry_t *entry;
197
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);
201
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);
213                         entry = NULL;
214                 }
215         }
216         substream->proc_prealloc_entry = entry;
217         return 0;
218 }
219
220
221 /*
222  * set up the unique pcm id
223  */
224 static inline void setup_pcm_id(snd_pcm_substream_t *subs)
225 {
226         if (! subs->dma_device.id)
227                 subs->dma_device.id = subs->pcm->device << 16 |
228                         subs->stream << 8 | (subs->number + 1);
229 }
230
231 /**
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
238  *
239  * Do pre-allocation for the given DMA type.
240  *
241  * Returns zero if successful, or a negative error code on failure.
242  */
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)
246 {
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);
251 }
252
253 /**
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
260  *
261  * Do pre-allocation to all substreams of the given pcm for the
262  * specified DMA type.
263  *
264  * Returns zero if successful, or a negative error code on failure.
265  */
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)
269 {
270         snd_pcm_substream_t *substream;
271         int stream, err;
272
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)
276                                 return err;
277         return 0;
278 }
279
280 /**
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
284  *
285  * Returns the page struct at the given buffer offset.
286  * Used as the page callback of PCM ops.
287  */
288 struct page *snd_pcm_sgbuf_ops_page(snd_pcm_substream_t *substream, unsigned long offset)
289 {
290         struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
291
292         unsigned int idx = offset >> PAGE_SHIFT;
293         if (idx >= (unsigned int)sgbuf->pages)
294                 return NULL;
295         return sgbuf->page_table[idx];
296 }
297
298 /**
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
302  *
303  * Allocates the DMA buffer on the BUS type given by
304  * snd_pcm_lib_preallocate_xxx_pages().
305  *
306  * Returns 1 if the buffer is changed, 0 if not changed, or a negative
307  * code on failure.
308  */
309 int snd_pcm_lib_malloc_pages(snd_pcm_substream_t *substream, size_t size)
310 {
311         snd_pcm_runtime_t *runtime;
312         struct snd_dma_buffer dmab;
313
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);    
318
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);
326         }
327         if (substream->dma_buffer.area != NULL && substream->dma_buffer.bytes >= size) {
328                 dmab = substream->dma_buffer; /* use the pre-allocated buffer */
329         } else {
330                 memset(&dmab, 0, sizeof(dmab)); /* allocate a new buffer */
331                 if (snd_dma_alloc_pages(&substream->dma_device, size, &dmab) < 0)
332                         return -ENOMEM;
333         }
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 */
339 }
340
341 /**
342  * snd_pcm_lib_free_pages - release the allocated DMA buffer.
343  * @substream: the substream to release the DMA buffer
344  *
345  * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
346  *
347  * Returns zero if successful, or a negative error code on failure.
348  */
349 int snd_pcm_lib_free_pages(snd_pcm_substream_t *substream)
350 {
351         snd_pcm_runtime_t *runtime;
352
353         snd_assert(substream != NULL, return -EINVAL);
354         runtime = substream->runtime;
355         snd_assert(runtime != NULL, return -EINVAL);
356         if (runtime->dma_area == NULL)
357                 return 0;
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);
367         }
368         runtime->dma_area = NULL;
369         runtime->dma_addr = 0UL;
370         runtime->dma_bytes = 0;
371         runtime->dma_private = NULL;
372         return 0;
373 }
374
375 #ifndef MODULE
376
377 /* format is: snd-pcm=preallocate_dma,maximum_substreams */
378
379 static int __init alsa_pcm_setup(char *str)
380 {
381         (void)(get_option(&str,&preallocate_dma) == 2 &&
382                get_option(&str,&maximum_substreams) == 2);
383         return 1;
384 }
385
386 __setup("snd-pcm=", alsa_pcm_setup);
387
388 #endif /* ifndef MODULE */