patch-2_6_7-vs1_9_1_12
[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 <linux/moduleparam.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/info.h>
30 #include <sound/initval.h>
31
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);
36
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);
41
42 const static size_t snd_minimum_buffer = 16384;
43
44
45 /*
46  * try to allocate as the large pages as possible.
47  * stores the resultant memory size in *res_size.
48  *
49  * the minimum size is snd_minimum_buffer.  it should be power of 2.
50  */
51 static int preallocate_pcm_pages(snd_pcm_substream_t *substream, size_t size)
52 {
53         struct snd_dma_buffer *dmab = &substream->dma_buffer;
54         int err;
55
56         snd_assert(size > 0, return -EINVAL);
57
58         /* already reserved? */
59         if (snd_dma_get_reserved(&substream->dma_device, dmab) > 0) {
60                 if (dmab->bytes >= size)
61                         return 0; /* yes */
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().
65                  */
66                 snd_dma_free_reserved(&substream->dma_device);
67                 dmab->bytes = 0;
68         }
69
70         do {
71                 if ((err = snd_dma_alloc_pages(&substream->dma_device, size, dmab)) < 0) {
72                         if (err != -ENOMEM)
73                                 return err; /* fatal error */
74                 } else {
75                         /* remember this one */
76                         snd_dma_set_reserved(&substream->dma_device, dmab);
77                         return 0;
78                 }
79                 size >>= 1;
80         } while (size >= snd_minimum_buffer);
81         dmab->bytes = 0; /* tell error */
82         return 0;
83 }
84
85 /*
86  * release the preallocated buffer if not yet done.
87  */
88 static void snd_pcm_lib_preallocate_dma_free(snd_pcm_substream_t *substream)
89 {
90         if (substream->dma_buffer.area == NULL)
91                 return;
92         snd_dma_free_reserved(&substream->dma_device);
93         substream->dma_buffer.area = NULL;
94 }
95
96 /**
97  * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream.
98  * @substream: the pcm substream instance
99  *
100  * Releases the pre-allocated buffer of the given substream.
101  *
102  * Returns zero if successful, or a negative error code on failure.
103  */
104 int snd_pcm_lib_preallocate_free(snd_pcm_substream_t *substream)
105 {
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;
110         }
111         substream->dma_device.type = SNDRV_DMA_TYPE_UNKNOWN;
112         return 0;
113 }
114
115 /**
116  * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm
117  * @pcm: the pcm instance
118  *
119  * Releases all the pre-allocated buffers on the given pcm.
120  *
121  * Returns zero if successful, or a negative error code on failure.
122  */
123 int snd_pcm_lib_preallocate_free_for_all(snd_pcm_t *pcm)
124 {
125         snd_pcm_substream_t *substream;
126         int stream;
127
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);
131         return 0;
132 }
133
134 /*
135  * read callback for prealloc proc file
136  *
137  * prints the current allocated size in kB.
138  */
139 static void snd_pcm_lib_preallocate_proc_read(snd_info_entry_t *entry,
140                                               snd_info_buffer_t *buffer)
141 {
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);
144 }
145
146 /*
147  * write callback for prealloc proc file
148  *
149  * accepts the preallocation size in kB.
150  */
151 static void snd_pcm_lib_preallocate_proc_write(snd_info_entry_t *entry,
152                                                snd_info_buffer_t *buffer)
153 {
154         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)entry->private_data;
155         char line[64], str[64];
156         size_t size;
157         struct snd_dma_buffer new_dmab;
158
159         if (substream->runtime) {
160                 buffer->error = -EBUSY;
161                 return;
162         }
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;
168                         return;
169                 }
170                 if (substream->dma_buffer.bytes == size)
171                         return;
172                 memset(&new_dmab, 0, sizeof(new_dmab));
173                 if (size > 0) {
174
175                         if (snd_dma_alloc_pages(&substream->dma_device, size, &new_dmab) < 0) {
176                                 buffer->error = -ENOMEM;
177                                 return;
178                         }
179                         substream->buffer_bytes_max = size;
180                         snd_dma_free_reserved(&substream->dma_device);
181                 } else {
182                         substream->buffer_bytes_max = UINT_MAX;
183                 }
184                 snd_dma_set_reserved(&substream->dma_device, &new_dmab);
185                 substream->dma_buffer = new_dmab;
186         } else {
187                 buffer->error = -EINVAL;
188         }
189 }
190
191 /*
192  * pre-allocate the buffer and create a proc file for the substream
193  */
194 static int snd_pcm_lib_preallocate_pages1(snd_pcm_substream_t *substream,
195                                           size_t size, size_t max)
196 {
197         snd_info_entry_t *entry;
198
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);
202
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);
214                         entry = NULL;
215                 }
216         }
217         substream->proc_prealloc_entry = entry;
218         return 0;
219 }
220
221
222 /*
223  * set up the unique pcm id
224  */
225 static inline void setup_pcm_id(snd_pcm_substream_t *subs)
226 {
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;
233         }
234 }
235
236 /**
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
243  *
244  * Do pre-allocation for the given DMA type.
245  *
246  * Returns zero if successful, or a negative error code on failure.
247  */
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)
251 {
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);
256 }
257
258 /**
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
265  *
266  * Do pre-allocation to all substreams of the given pcm for the
267  * specified DMA type.
268  *
269  * Returns zero if successful, or a negative error code on failure.
270  */
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)
274 {
275         snd_pcm_substream_t *substream;
276         int stream, err;
277
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)
281                                 return err;
282         return 0;
283 }
284
285 /**
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
289  *
290  * Returns the page struct at the given buffer offset.
291  * Used as the page callback of PCM ops.
292  */
293 struct page *snd_pcm_sgbuf_ops_page(snd_pcm_substream_t *substream, unsigned long offset)
294 {
295         struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
296
297         unsigned int idx = offset >> PAGE_SHIFT;
298         if (idx >= (unsigned int)sgbuf->pages)
299                 return NULL;
300         return sgbuf->page_table[idx];
301 }
302
303 /**
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
307  *
308  * Allocates the DMA buffer on the BUS type given by
309  * snd_pcm_lib_preallocate_xxx_pages().
310  *
311  * Returns 1 if the buffer is changed, 0 if not changed, or a negative
312  * code on failure.
313  */
314 int snd_pcm_lib_malloc_pages(snd_pcm_substream_t *substream, size_t size)
315 {
316         snd_pcm_runtime_t *runtime;
317         struct snd_dma_buffer dmab;
318
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);    
323
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);
331         }
332         if (substream->dma_buffer.area != NULL && substream->dma_buffer.bytes >= size) {
333                 dmab = substream->dma_buffer; /* use the pre-allocated buffer */
334         } else {
335                 memset(&dmab, 0, sizeof(dmab)); /* allocate a new buffer */
336                 if (snd_dma_alloc_pages(&substream->dma_device, size, &dmab) < 0)
337                         return -ENOMEM;
338         }
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 */
344 }
345
346 /**
347  * snd_pcm_lib_free_pages - release the allocated DMA buffer.
348  * @substream: the substream to release the DMA buffer
349  *
350  * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages().
351  *
352  * Returns zero if successful, or a negative error code on failure.
353  */
354 int snd_pcm_lib_free_pages(snd_pcm_substream_t *substream)
355 {
356         snd_pcm_runtime_t *runtime;
357
358         snd_assert(substream != NULL, return -EINVAL);
359         runtime = substream->runtime;
360         snd_assert(runtime != NULL, return -EINVAL);
361         if (runtime->dma_area == NULL)
362                 return 0;
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);
372         }
373         runtime->dma_area = NULL;
374         runtime->dma_addr = 0UL;
375         runtime->dma_bytes = 0;
376         runtime->dma_private = NULL;
377         return 0;
378 }
379
380 #ifndef MODULE
381
382 /* format is: snd-pcm=preallocate_dma,maximum_substreams */
383
384 static int __init alsa_pcm_setup(char *str)
385 {
386         (void)(get_option(&str,&preallocate_dma) == 2 &&
387                get_option(&str,&maximum_substreams) == 2);
388         return 1;
389 }
390
391 __setup("snd-pcm=", alsa_pcm_setup);
392
393 #endif /* ifndef MODULE */