blob: a7b46ec72f3231191cb6f81d7c816f6b011bc696 [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
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010049static DEFINE_MUTEX(list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static LIST_HEAD(mem_list_head);
51
52/* buffer preservation list */
53struct snd_mem_list {
54 struct snd_dma_buffer buffer;
55 unsigned int id;
56 struct list_head list;
57};
58
59/* id for pre-allocated buffers */
60#define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
64 * Generic memory allocators
65 *
66 */
67
68static long snd_allocated_pages; /* holding the number of allocated pages */
69
70static inline void inc_snd_pages(int order)
71{
72 snd_allocated_pages += 1 << order;
73}
74
75static inline void dec_snd_pages(int order)
76{
77 snd_allocated_pages -= 1 << order;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/**
81 * snd_malloc_pages - allocate pages with the given size
82 * @size: the size to allocate in bytes
83 * @gfp_flags: the allocation conditions, GFP_XXX
84 *
85 * Allocates the physically contiguous pages with the given size.
86 *
87 * Returns the pointer of the buffer, or NULL if no enoguh memory.
88 */
Al Viro1ef64e62005-10-21 03:22:18 -040089void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 int pg;
92 void *res;
93
Takashi Iwai7eaa9432008-08-08 17:09:09 +020094 if (WARN_ON(!size))
95 return NULL;
96 if (WARN_ON(!gfp_flags))
97 return NULL;
Hugh Dickinsf3d48f02005-11-21 21:32:22 -080098 gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 pg = get_order(size);
Takashi Iwai2ba8c152006-01-31 14:44:28 +0100100 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 inc_snd_pages(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return res;
103}
104
105/**
106 * snd_free_pages - release the pages
107 * @ptr: the buffer pointer to release
108 * @size: the allocated buffer size
109 *
110 * Releases the buffer allocated via snd_malloc_pages().
111 */
112void snd_free_pages(void *ptr, size_t size)
113{
114 int pg;
115
116 if (ptr == NULL)
117 return;
118 pg = get_order(size);
119 dec_snd_pages(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 free_pages((unsigned long) ptr, pg);
121}
122
123/*
124 *
125 * Bus-specific memory allocators
126 *
127 */
128
Takashi Iwai8f115512007-07-26 18:59:36 +0200129#ifdef CONFIG_HAS_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/* allocate the coherent DMA pages */
131static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
132{
133 int pg;
134 void *res;
Al Viro1ef64e62005-10-21 03:22:18 -0400135 gfp_t gfp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200137 if (WARN_ON(!dma))
138 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 pg = get_order(size);
140 gfp_flags = GFP_KERNEL
Hugh Dickinsf3d48f02005-11-21 21:32:22 -0800141 | __GFP_COMP /* compound page lets parts be mapped */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 | __GFP_NORETRY /* don't trigger OOM-killer */
143 | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
144 res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
Takashi Iwai2ba8c152006-01-31 14:44:28 +0100145 if (res != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 inc_snd_pages(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 return res;
149}
150
151/* free the coherent DMA pages */
152static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
153 dma_addr_t dma)
154{
155 int pg;
156
157 if (ptr == NULL)
158 return;
159 pg = get_order(size);
160 dec_snd_pages(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
162}
Takashi Iwai8f115512007-07-26 18:59:36 +0200163#endif /* CONFIG_HAS_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165#ifdef CONFIG_SBUS
166
167static void *snd_malloc_sbus_pages(struct device *dev, size_t size,
168 dma_addr_t *dma_addr)
169{
170 struct sbus_dev *sdev = (struct sbus_dev *)dev;
171 int pg;
172 void *res;
173
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200174 if (WARN_ON(!dma_addr))
175 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 pg = get_order(size);
177 res = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr);
178 if (res != NULL)
179 inc_snd_pages(pg);
180 return res;
181}
182
183static void snd_free_sbus_pages(struct device *dev, size_t size,
184 void *ptr, dma_addr_t dma_addr)
185{
186 struct sbus_dev *sdev = (struct sbus_dev *)dev;
187 int pg;
188
189 if (ptr == NULL)
190 return;
191 pg = get_order(size);
192 dec_snd_pages(pg);
193 sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr);
194}
195
196#endif /* CONFIG_SBUS */
197
198/*
199 *
200 * ALSA generic memory management
201 *
202 */
203
204
205/**
206 * snd_dma_alloc_pages - allocate the buffer area according to the given type
207 * @type: the DMA buffer type
208 * @device: the device pointer
209 * @size: the buffer size to allocate
210 * @dmab: buffer allocation record to store the allocated data
211 *
212 * Calls the memory-allocator function for the corresponding
213 * buffer type.
214 *
215 * Returns zero if the buffer with the given size is allocated successfuly,
216 * other a negative value at error.
217 */
218int snd_dma_alloc_pages(int type, struct device *device, size_t size,
219 struct snd_dma_buffer *dmab)
220{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200221 if (WARN_ON(!size))
222 return -ENXIO;
223 if (WARN_ON(!dmab))
224 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 dmab->dev.type = type;
227 dmab->dev.dev = device;
228 dmab->bytes = 0;
229 switch (type) {
230 case SNDRV_DMA_TYPE_CONTINUOUS:
231 dmab->area = snd_malloc_pages(size, (unsigned long)device);
232 dmab->addr = 0;
233 break;
234#ifdef CONFIG_SBUS
235 case SNDRV_DMA_TYPE_SBUS:
236 dmab->area = snd_malloc_sbus_pages(device, size, &dmab->addr);
237 break;
238#endif
Takashi Iwai8f115512007-07-26 18:59:36 +0200239#ifdef CONFIG_HAS_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 case SNDRV_DMA_TYPE_DEV:
241 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
242 break;
243 case SNDRV_DMA_TYPE_DEV_SG:
244 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
245 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200246#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 default:
248 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
249 dmab->area = NULL;
250 dmab->addr = 0;
251 return -ENXIO;
252 }
253 if (! dmab->area)
254 return -ENOMEM;
255 dmab->bytes = size;
256 return 0;
257}
258
259/**
260 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
261 * @type: the DMA buffer type
262 * @device: the device pointer
263 * @size: the buffer size to allocate
264 * @dmab: buffer allocation record to store the allocated data
265 *
266 * Calls the memory-allocator function for the corresponding
267 * buffer type. When no space is left, this function reduces the size and
268 * tries to allocate again. The size actually allocated is stored in
269 * res_size argument.
270 *
271 * Returns zero if the buffer with the given size is allocated successfuly,
272 * other a negative value at error.
273 */
274int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
275 struct snd_dma_buffer *dmab)
276{
277 int err;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
Takashi Iwai4e184f82008-07-30 15:13:33 +0200280 size_t aligned_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (err != -ENOMEM)
282 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (size <= PAGE_SIZE)
284 return -ENOMEM;
Takashi Iwai4e184f82008-07-30 15:13:33 +0200285 aligned_size = PAGE_SIZE << get_order(size);
286 if (size != aligned_size)
287 size = aligned_size;
288 else
289 size >>= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291 if (! dmab->area)
292 return -ENOMEM;
293 return 0;
294}
295
296
297/**
298 * snd_dma_free_pages - release the allocated buffer
299 * @dmab: the buffer allocation record to release
300 *
301 * Releases the allocated buffer via snd_dma_alloc_pages().
302 */
303void snd_dma_free_pages(struct snd_dma_buffer *dmab)
304{
305 switch (dmab->dev.type) {
306 case SNDRV_DMA_TYPE_CONTINUOUS:
307 snd_free_pages(dmab->area, dmab->bytes);
308 break;
309#ifdef CONFIG_SBUS
310 case SNDRV_DMA_TYPE_SBUS:
311 snd_free_sbus_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
312 break;
313#endif
Takashi Iwai8f115512007-07-26 18:59:36 +0200314#ifdef CONFIG_HAS_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 case SNDRV_DMA_TYPE_DEV:
316 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
317 break;
318 case SNDRV_DMA_TYPE_DEV_SG:
319 snd_free_sgbuf_pages(dmab);
320 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 default:
323 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
324 }
325}
326
327
328/**
329 * snd_dma_get_reserved - get the reserved buffer for the given device
330 * @dmab: the buffer allocation record to store
331 * @id: the buffer id
332 *
333 * Looks for the reserved-buffer list and re-uses if the same buffer
334 * is found in the list. When the buffer is found, it's removed from the free list.
335 *
336 * Returns the size of buffer if the buffer is found, or zero if not found.
337 */
338size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
339{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct snd_mem_list *mem;
341
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200342 if (WARN_ON(!dmab))
343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100345 mutex_lock(&list_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200346 list_for_each_entry(mem, &mem_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (mem->id == id &&
Takashi Iwaib6a96912005-05-30 18:27:03 +0200348 (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
349 ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
350 struct device *dev = dmab->dev.dev;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200351 list_del(&mem->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 *dmab = mem->buffer;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200353 if (dmab->dev.dev == NULL)
354 dmab->dev.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 kfree(mem);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100356 mutex_unlock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return dmab->bytes;
358 }
359 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100360 mutex_unlock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
362}
363
364/**
365 * snd_dma_reserve_buf - reserve the buffer
366 * @dmab: the buffer to reserve
367 * @id: the buffer id
368 *
369 * Reserves the given buffer as a reserved buffer.
370 *
371 * Returns zero if successful, or a negative code at error.
372 */
373int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
374{
375 struct snd_mem_list *mem;
376
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200377 if (WARN_ON(!dmab))
378 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
380 if (! mem)
381 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100382 mutex_lock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 mem->buffer = *dmab;
384 mem->id = id;
385 list_add_tail(&mem->list, &mem_list_head);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100386 mutex_unlock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 0;
388}
389
390/*
391 * purge all reserved buffers
392 */
393static void free_all_reserved_pages(void)
394{
395 struct list_head *p;
396 struct snd_mem_list *mem;
397
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100398 mutex_lock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 while (! list_empty(&mem_list_head)) {
400 p = mem_list_head.next;
401 mem = list_entry(p, struct snd_mem_list, list);
402 list_del(p);
403 snd_dma_free_pages(&mem->buffer);
404 kfree(mem);
405 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100406 mutex_unlock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410#ifdef CONFIG_PROC_FS
411/*
412 * proc file interface
413 */
Takashi Iwaib6a96912005-05-30 18:27:03 +0200414#define SND_MEM_PROC_FILE "driver/snd-page-alloc"
Clemens Ladischa53fc182005-08-11 15:59:17 +0200415static struct proc_dir_entry *snd_mem_proc;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200416
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200417static int snd_mem_proc_read(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct snd_mem_list *mem;
421 int devno;
422 static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
423
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100424 mutex_lock(&list_mutex);
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200425 seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
426 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 devno = 0;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200428 list_for_each_entry(mem, &mem_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 devno++;
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200430 seq_printf(seq, "buffer %d : ID %08x : type %s\n",
431 devno, mem->id, types[mem->buffer.dev.type]);
432 seq_printf(seq, " addr = 0x%lx, size = %d bytes\n",
433 (unsigned long)mem->buffer.addr,
434 (int)mem->buffer.bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100436 mutex_unlock(&list_mutex);
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200437 return 0;
438}
439
440static int snd_mem_proc_open(struct inode *inode, struct file *file)
441{
442 return single_open(file, snd_mem_proc_read, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
Takashi Iwaib6a96912005-05-30 18:27:03 +0200444
445/* FIXME: for pci only - other bus? */
446#ifdef CONFIG_PCI
447#define gettoken(bufp) strsep(bufp, " \t\n")
448
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200449static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
450 size_t count, loff_t * ppos)
Takashi Iwaib6a96912005-05-30 18:27:03 +0200451{
452 char buf[128];
453 char *token, *p;
454
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200455 if (count > sizeof(buf) - 1)
456 return -EINVAL;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200457 if (copy_from_user(buf, buffer, count))
458 return -EFAULT;
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200459 buf[count] = '\0';
Takashi Iwaib6a96912005-05-30 18:27:03 +0200460
461 p = buf;
462 token = gettoken(&p);
463 if (! token || *token == '#')
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200464 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200465 if (strcmp(token, "add") == 0) {
466 char *endp;
467 int vendor, device, size, buffers;
468 long mask;
469 int i, alloced;
470 struct pci_dev *pci;
471
472 if ((token = gettoken(&p)) == NULL ||
473 (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
474 (token = gettoken(&p)) == NULL ||
475 (device = simple_strtol(token, NULL, 0)) <= 0 ||
476 (token = gettoken(&p)) == NULL ||
477 (mask = simple_strtol(token, NULL, 0)) < 0 ||
478 (token = gettoken(&p)) == NULL ||
479 (size = memparse(token, &endp)) < 64*1024 ||
480 size > 16*1024*1024 /* too big */ ||
481 (token = gettoken(&p)) == NULL ||
482 (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
483 buffers > 4) {
484 printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200485 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200486 }
487 vendor &= 0xffff;
488 device &= 0xffff;
489
490 alloced = 0;
491 pci = NULL;
Jiri Slaby0dd119f2005-09-07 14:28:33 +0200492 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
Takashi Iwaib6a96912005-05-30 18:27:03 +0200493 if (mask > 0 && mask < 0xffffffff) {
494 if (pci_set_dma_mask(pci, mask) < 0 ||
495 pci_set_consistent_dma_mask(pci, mask) < 0) {
496 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 +0100497 pci_dev_put(pci);
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200498 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200499 }
500 }
501 for (i = 0; i < buffers; i++) {
502 struct snd_dma_buffer dmab;
503 memset(&dmab, 0, sizeof(dmab));
504 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
505 size, &dmab) < 0) {
506 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
Jiri Slaby0dd119f2005-09-07 14:28:33 +0200507 pci_dev_put(pci);
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200508 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200509 }
510 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
511 }
512 alloced++;
513 }
514 if (! alloced) {
515 for (i = 0; i < buffers; i++) {
516 struct snd_dma_buffer dmab;
517 memset(&dmab, 0, sizeof(dmab));
518 /* FIXME: We can allocate only in ZONE_DMA
519 * without a device pointer!
520 */
521 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
522 size, &dmab) < 0) {
523 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
524 break;
525 }
526 snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
527 }
528 }
529 } else if (strcmp(token, "erase") == 0)
530 /* FIXME: need for releasing each buffer chunk? */
531 free_all_reserved_pages();
532 else
533 printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200534 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200535}
536#endif /* CONFIG_PCI */
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200537
538static const struct file_operations snd_mem_proc_fops = {
539 .owner = THIS_MODULE,
540 .open = snd_mem_proc_open,
541 .read = seq_read,
542#ifdef CONFIG_PCI
543 .write = snd_mem_proc_write,
544#endif
545 .llseek = seq_lseek,
546 .release = single_release,
547};
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#endif /* CONFIG_PROC_FS */
550
551/*
552 * module entry
553 */
554
555static int __init snd_mem_init(void)
556{
557#ifdef CONFIG_PROC_FS
Denis V. Lunev7bf4e6d2008-04-29 01:02:13 -0700558 snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
559 &snd_mem_proc_fops);
Takashi Iwaib6a96912005-05-30 18:27:03 +0200560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return 0;
562}
563
564static void __exit snd_mem_exit(void)
565{
Takashi Iwaie0be4d32005-08-23 11:11:03 +0200566 remove_proc_entry(SND_MEM_PROC_FILE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 free_all_reserved_pages();
568 if (snd_allocated_pages > 0)
569 printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
570}
571
572
573module_init(snd_mem_init)
574module_exit(snd_mem_exit)
575
576
577/*
578 * exports
579 */
580EXPORT_SYMBOL(snd_dma_alloc_pages);
581EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
582EXPORT_SYMBOL(snd_dma_free_pages);
583
584EXPORT_SYMBOL(snd_dma_get_reserved_buf);
585EXPORT_SYMBOL(snd_dma_reserve_buf);
586
587EXPORT_SYMBOL(snd_malloc_pages);
588EXPORT_SYMBOL(snd_free_pages);