blob: 4a649976cc8afab8a7caa42102518a5fa7e699f3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02002 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Takashi Iwai <tiwai@suse.de>
4 *
5 * Generic memory allocators
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/proc_fs.h>
26#include <linux/init.h>
27#include <linux/pci.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
Takashi Iwaiccec6e22007-09-17 21:55:10 +020030#include <linux/seq_file.h>
Takashi Iwaib6a96912005-05-30 18:27:03 +020031#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/dma-mapping.h>
33#include <linux/moduleparam.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010034#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/memalloc.h>
36#ifdef CONFIG_SBUS
37#include <asm/sbus.h>
38#endif
39
40
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020041MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042MODULE_DESCRIPTION("Memory allocator for ALSA system.");
43MODULE_LICENSE("GPL");
44
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 */
48
49void *snd_malloc_sgbuf_pages(struct device *device,
50 size_t size, struct snd_dma_buffer *dmab,
51 size_t *res_size);
52int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
53
54/*
55 */
56
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010057static DEFINE_MUTEX(list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static LIST_HEAD(mem_list_head);
59
60/* buffer preservation list */
61struct snd_mem_list {
62 struct snd_dma_buffer buffer;
63 unsigned int id;
64 struct list_head list;
65};
66
67/* id for pre-allocated buffers */
68#define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 *
72 * Generic memory allocators
73 *
74 */
75
76static long snd_allocated_pages; /* holding the number of allocated pages */
77
78static inline void inc_snd_pages(int order)
79{
80 snd_allocated_pages += 1 << order;
81}
82
83static inline void dec_snd_pages(int order)
84{
85 snd_allocated_pages -= 1 << order;
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/**
89 * snd_malloc_pages - allocate pages with the given size
90 * @size: the size to allocate in bytes
91 * @gfp_flags: the allocation conditions, GFP_XXX
92 *
93 * Allocates the physically contiguous pages with the given size.
94 *
95 * Returns the pointer of the buffer, or NULL if no enoguh memory.
96 */
Al Viro1ef64e62005-10-21 03:22:18 -040097void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 int pg;
100 void *res;
101
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200102 if (WARN_ON(!size))
103 return NULL;
104 if (WARN_ON(!gfp_flags))
105 return NULL;
Hugh Dickinsf3d48f02005-11-21 21:32:22 -0800106 gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 pg = get_order(size);
Takashi Iwai2ba8c152006-01-31 14:44:28 +0100108 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 inc_snd_pages(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return res;
111}
112
113/**
114 * snd_free_pages - release the pages
115 * @ptr: the buffer pointer to release
116 * @size: the allocated buffer size
117 *
118 * Releases the buffer allocated via snd_malloc_pages().
119 */
120void snd_free_pages(void *ptr, size_t size)
121{
122 int pg;
123
124 if (ptr == NULL)
125 return;
126 pg = get_order(size);
127 dec_snd_pages(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 free_pages((unsigned long) ptr, pg);
129}
130
131/*
132 *
133 * Bus-specific memory allocators
134 *
135 */
136
Takashi Iwai8f115512007-07-26 18:59:36 +0200137#ifdef CONFIG_HAS_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* allocate the coherent DMA pages */
139static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
140{
141 int pg;
142 void *res;
Al Viro1ef64e62005-10-21 03:22:18 -0400143 gfp_t gfp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200145 if (WARN_ON(!dma))
146 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 pg = get_order(size);
148 gfp_flags = GFP_KERNEL
Hugh Dickinsf3d48f02005-11-21 21:32:22 -0800149 | __GFP_COMP /* compound page lets parts be mapped */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 | __GFP_NORETRY /* don't trigger OOM-killer */
151 | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
152 res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
Takashi Iwai2ba8c152006-01-31 14:44:28 +0100153 if (res != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 inc_snd_pages(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return res;
157}
158
159/* free the coherent DMA pages */
160static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
161 dma_addr_t dma)
162{
163 int pg;
164
165 if (ptr == NULL)
166 return;
167 pg = get_order(size);
168 dec_snd_pages(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
170}
Takashi Iwai8f115512007-07-26 18:59:36 +0200171#endif /* CONFIG_HAS_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173#ifdef CONFIG_SBUS
174
175static void *snd_malloc_sbus_pages(struct device *dev, size_t size,
176 dma_addr_t *dma_addr)
177{
178 struct sbus_dev *sdev = (struct sbus_dev *)dev;
179 int pg;
180 void *res;
181
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200182 if (WARN_ON(!dma_addr))
183 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 pg = get_order(size);
185 res = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr);
186 if (res != NULL)
187 inc_snd_pages(pg);
188 return res;
189}
190
191static void snd_free_sbus_pages(struct device *dev, size_t size,
192 void *ptr, dma_addr_t dma_addr)
193{
194 struct sbus_dev *sdev = (struct sbus_dev *)dev;
195 int pg;
196
197 if (ptr == NULL)
198 return;
199 pg = get_order(size);
200 dec_snd_pages(pg);
201 sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr);
202}
203
204#endif /* CONFIG_SBUS */
205
206/*
207 *
208 * ALSA generic memory management
209 *
210 */
211
212
213/**
214 * snd_dma_alloc_pages - allocate the buffer area according to the given type
215 * @type: the DMA buffer type
216 * @device: the device pointer
217 * @size: the buffer size to allocate
218 * @dmab: buffer allocation record to store the allocated data
219 *
220 * Calls the memory-allocator function for the corresponding
221 * buffer type.
222 *
223 * Returns zero if the buffer with the given size is allocated successfuly,
224 * other a negative value at error.
225 */
226int snd_dma_alloc_pages(int type, struct device *device, size_t size,
227 struct snd_dma_buffer *dmab)
228{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200229 if (WARN_ON(!size))
230 return -ENXIO;
231 if (WARN_ON(!dmab))
232 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 dmab->dev.type = type;
235 dmab->dev.dev = device;
236 dmab->bytes = 0;
237 switch (type) {
238 case SNDRV_DMA_TYPE_CONTINUOUS:
239 dmab->area = snd_malloc_pages(size, (unsigned long)device);
240 dmab->addr = 0;
241 break;
242#ifdef CONFIG_SBUS
243 case SNDRV_DMA_TYPE_SBUS:
244 dmab->area = snd_malloc_sbus_pages(device, size, &dmab->addr);
245 break;
246#endif
Takashi Iwai8f115512007-07-26 18:59:36 +0200247#ifdef CONFIG_HAS_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 case SNDRV_DMA_TYPE_DEV:
249 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
250 break;
251 case SNDRV_DMA_TYPE_DEV_SG:
252 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
253 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200254#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 default:
256 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
257 dmab->area = NULL;
258 dmab->addr = 0;
259 return -ENXIO;
260 }
261 if (! dmab->area)
262 return -ENOMEM;
263 dmab->bytes = size;
264 return 0;
265}
266
267/**
268 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
269 * @type: the DMA buffer type
270 * @device: the device pointer
271 * @size: the buffer size to allocate
272 * @dmab: buffer allocation record to store the allocated data
273 *
274 * Calls the memory-allocator function for the corresponding
275 * buffer type. When no space is left, this function reduces the size and
276 * tries to allocate again. The size actually allocated is stored in
277 * res_size argument.
278 *
279 * Returns zero if the buffer with the given size is allocated successfuly,
280 * other a negative value at error.
281 */
282int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
283 struct snd_dma_buffer *dmab)
284{
285 int err;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
288 if (err != -ENOMEM)
289 return err;
290 size >>= 1;
291 if (size <= PAGE_SIZE)
292 return -ENOMEM;
293 }
294 if (! dmab->area)
295 return -ENOMEM;
296 return 0;
297}
298
299
300/**
301 * snd_dma_free_pages - release the allocated buffer
302 * @dmab: the buffer allocation record to release
303 *
304 * Releases the allocated buffer via snd_dma_alloc_pages().
305 */
306void snd_dma_free_pages(struct snd_dma_buffer *dmab)
307{
308 switch (dmab->dev.type) {
309 case SNDRV_DMA_TYPE_CONTINUOUS:
310 snd_free_pages(dmab->area, dmab->bytes);
311 break;
312#ifdef CONFIG_SBUS
313 case SNDRV_DMA_TYPE_SBUS:
314 snd_free_sbus_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
315 break;
316#endif
Takashi Iwai8f115512007-07-26 18:59:36 +0200317#ifdef CONFIG_HAS_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 case SNDRV_DMA_TYPE_DEV:
319 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
320 break;
321 case SNDRV_DMA_TYPE_DEV_SG:
322 snd_free_sgbuf_pages(dmab);
323 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200324#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 default:
326 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
327 }
328}
329
330
331/**
332 * snd_dma_get_reserved - get the reserved buffer for the given device
333 * @dmab: the buffer allocation record to store
334 * @id: the buffer id
335 *
336 * Looks for the reserved-buffer list and re-uses if the same buffer
337 * is found in the list. When the buffer is found, it's removed from the free list.
338 *
339 * Returns the size of buffer if the buffer is found, or zero if not found.
340 */
341size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
342{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct snd_mem_list *mem;
344
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200345 if (WARN_ON(!dmab))
346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100348 mutex_lock(&list_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200349 list_for_each_entry(mem, &mem_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (mem->id == id &&
Takashi Iwaib6a96912005-05-30 18:27:03 +0200351 (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
352 ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
353 struct device *dev = dmab->dev.dev;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200354 list_del(&mem->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 *dmab = mem->buffer;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200356 if (dmab->dev.dev == NULL)
357 dmab->dev.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 kfree(mem);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100359 mutex_unlock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return dmab->bytes;
361 }
362 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100363 mutex_unlock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365}
366
367/**
368 * snd_dma_reserve_buf - reserve the buffer
369 * @dmab: the buffer to reserve
370 * @id: the buffer id
371 *
372 * Reserves the given buffer as a reserved buffer.
373 *
374 * Returns zero if successful, or a negative code at error.
375 */
376int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
377{
378 struct snd_mem_list *mem;
379
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200380 if (WARN_ON(!dmab))
381 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
383 if (! mem)
384 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100385 mutex_lock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 mem->buffer = *dmab;
387 mem->id = id;
388 list_add_tail(&mem->list, &mem_list_head);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100389 mutex_unlock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391}
392
393/*
394 * purge all reserved buffers
395 */
396static void free_all_reserved_pages(void)
397{
398 struct list_head *p;
399 struct snd_mem_list *mem;
400
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100401 mutex_lock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 while (! list_empty(&mem_list_head)) {
403 p = mem_list_head.next;
404 mem = list_entry(p, struct snd_mem_list, list);
405 list_del(p);
406 snd_dma_free_pages(&mem->buffer);
407 kfree(mem);
408 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100409 mutex_unlock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413#ifdef CONFIG_PROC_FS
414/*
415 * proc file interface
416 */
Takashi Iwaib6a96912005-05-30 18:27:03 +0200417#define SND_MEM_PROC_FILE "driver/snd-page-alloc"
Clemens Ladischa53fc182005-08-11 15:59:17 +0200418static struct proc_dir_entry *snd_mem_proc;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200419
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200420static int snd_mem_proc_read(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct snd_mem_list *mem;
424 int devno;
425 static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
426
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100427 mutex_lock(&list_mutex);
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200428 seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
429 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 devno = 0;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200431 list_for_each_entry(mem, &mem_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 devno++;
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200433 seq_printf(seq, "buffer %d : ID %08x : type %s\n",
434 devno, mem->id, types[mem->buffer.dev.type]);
435 seq_printf(seq, " addr = 0x%lx, size = %d bytes\n",
436 (unsigned long)mem->buffer.addr,
437 (int)mem->buffer.bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100439 mutex_unlock(&list_mutex);
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200440 return 0;
441}
442
443static int snd_mem_proc_open(struct inode *inode, struct file *file)
444{
445 return single_open(file, snd_mem_proc_read, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
Takashi Iwaib6a96912005-05-30 18:27:03 +0200447
448/* FIXME: for pci only - other bus? */
449#ifdef CONFIG_PCI
450#define gettoken(bufp) strsep(bufp, " \t\n")
451
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200452static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
453 size_t count, loff_t * ppos)
Takashi Iwaib6a96912005-05-30 18:27:03 +0200454{
455 char buf[128];
456 char *token, *p;
457
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200458 if (count > sizeof(buf) - 1)
459 return -EINVAL;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200460 if (copy_from_user(buf, buffer, count))
461 return -EFAULT;
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200462 buf[count] = '\0';
Takashi Iwaib6a96912005-05-30 18:27:03 +0200463
464 p = buf;
465 token = gettoken(&p);
466 if (! token || *token == '#')
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200467 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200468 if (strcmp(token, "add") == 0) {
469 char *endp;
470 int vendor, device, size, buffers;
471 long mask;
472 int i, alloced;
473 struct pci_dev *pci;
474
475 if ((token = gettoken(&p)) == NULL ||
476 (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
477 (token = gettoken(&p)) == NULL ||
478 (device = simple_strtol(token, NULL, 0)) <= 0 ||
479 (token = gettoken(&p)) == NULL ||
480 (mask = simple_strtol(token, NULL, 0)) < 0 ||
481 (token = gettoken(&p)) == NULL ||
482 (size = memparse(token, &endp)) < 64*1024 ||
483 size > 16*1024*1024 /* too big */ ||
484 (token = gettoken(&p)) == NULL ||
485 (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
486 buffers > 4) {
487 printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200488 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200489 }
490 vendor &= 0xffff;
491 device &= 0xffff;
492
493 alloced = 0;
494 pci = NULL;
Jiri Slaby0dd119f2005-09-07 14:28:33 +0200495 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
Takashi Iwaib6a96912005-05-30 18:27:03 +0200496 if (mask > 0 && mask < 0xffffffff) {
497 if (pci_set_dma_mask(pci, mask) < 0 ||
498 pci_set_consistent_dma_mask(pci, mask) < 0) {
499 printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
Julia Lawalldf1deb62007-11-28 11:58:56 +0100500 pci_dev_put(pci);
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200501 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200502 }
503 }
504 for (i = 0; i < buffers; i++) {
505 struct snd_dma_buffer dmab;
506 memset(&dmab, 0, sizeof(dmab));
507 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
508 size, &dmab) < 0) {
509 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
Jiri Slaby0dd119f2005-09-07 14:28:33 +0200510 pci_dev_put(pci);
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200511 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200512 }
513 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
514 }
515 alloced++;
516 }
517 if (! alloced) {
518 for (i = 0; i < buffers; i++) {
519 struct snd_dma_buffer dmab;
520 memset(&dmab, 0, sizeof(dmab));
521 /* FIXME: We can allocate only in ZONE_DMA
522 * without a device pointer!
523 */
524 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
525 size, &dmab) < 0) {
526 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
527 break;
528 }
529 snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
530 }
531 }
532 } else if (strcmp(token, "erase") == 0)
533 /* FIXME: need for releasing each buffer chunk? */
534 free_all_reserved_pages();
535 else
536 printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200537 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200538}
539#endif /* CONFIG_PCI */
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200540
541static const struct file_operations snd_mem_proc_fops = {
542 .owner = THIS_MODULE,
543 .open = snd_mem_proc_open,
544 .read = seq_read,
545#ifdef CONFIG_PCI
546 .write = snd_mem_proc_write,
547#endif
548 .llseek = seq_lseek,
549 .release = single_release,
550};
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552#endif /* CONFIG_PROC_FS */
553
554/*
555 * module entry
556 */
557
558static int __init snd_mem_init(void)
559{
560#ifdef CONFIG_PROC_FS
Denis V. Lunev7bf4e6d2008-04-29 01:02:13 -0700561 snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
562 &snd_mem_proc_fops);
Takashi Iwaib6a96912005-05-30 18:27:03 +0200563#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return 0;
565}
566
567static void __exit snd_mem_exit(void)
568{
Takashi Iwaie0be4d32005-08-23 11:11:03 +0200569 remove_proc_entry(SND_MEM_PROC_FILE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 free_all_reserved_pages();
571 if (snd_allocated_pages > 0)
572 printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
573}
574
575
576module_init(snd_mem_init)
577module_exit(snd_mem_exit)
578
579
580/*
581 * exports
582 */
583EXPORT_SYMBOL(snd_dma_alloc_pages);
584EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
585EXPORT_SYMBOL(snd_dma_free_pages);
586
587EXPORT_SYMBOL(snd_dma_get_reserved_buf);
588EXPORT_SYMBOL(snd_dma_reserve_buf);
589
590EXPORT_SYMBOL(snd_malloc_pages);
591EXPORT_SYMBOL(snd_free_pages);