ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / memalloc.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *                   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
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/proc_fs.h>
27 #include <linux/init.h>
28 #include <linux/pci.h>
29 #include <linux/slab.h>
30 #include <linux/mm.h>
31 #include <linux/dma-mapping.h>
32 #include <asm/semaphore.h>
33 #include <sound/memalloc.h>
34 #ifdef CONFIG_SBUS
35 #include <asm/sbus.h>
36 #endif
37
38
39 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@suse.cz>");
40 MODULE_DESCRIPTION("Memory allocator for ALSA system.");
41 MODULE_LICENSE("GPL");
42
43
44 #ifndef SNDRV_CARDS
45 #define SNDRV_CARDS     8
46 #endif
47 static int enable[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
48 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
49 MODULE_PARM_DESC(enable, "Enable cards to allocate buffers.");
50
51 /*
52  */
53
54 void *snd_malloc_sgbuf_pages(const struct snd_dma_device *dev,
55                              size_t size, struct snd_dma_buffer *dmab,
56                              size_t *res_size);
57 int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
58
59 /*
60  */
61
62 static DECLARE_MUTEX(list_mutex);
63 static LIST_HEAD(mem_list_head);
64
65 /* buffer preservation list */
66 struct snd_mem_list {
67         struct snd_dma_device dev;
68         struct snd_dma_buffer buffer;
69         int used;
70         struct list_head list;
71 };
72
73 /* id for pre-allocated buffers */
74 #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
75
76 #ifdef CONFIG_SND_DEBUG
77 #define __ASTRING__(x) #x
78 #define snd_assert(expr, args...) do {\
79         if (!(expr)) {\
80                 printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
81                 args;\
82         }\
83 } while (0)
84 #else
85 #define snd_assert(expr, args...) /**/
86 #endif
87
88 /*
89  *  Hacks
90  */
91
92 static void *snd_dma_alloc_coherent1(struct device *dev, size_t size,
93                                      dma_addr_t *dma_handle, int flags)
94 {
95         if (dev)
96                 return dma_alloc_coherent(dev, size, dma_handle, flags);
97         else /* FIXME: dma_alloc_coherent does't always accept dev=NULL */
98                 return pci_alloc_consistent(NULL, size, dma_handle);
99 }
100
101 static void snd_dma_free_coherent1(struct device *dev, size_t size, void *dma_addr,
102                                    dma_addr_t dma_handle)
103 {
104         if (dev)
105                 return dma_free_coherent(dev, size, dma_addr, dma_handle);
106         else
107                 return pci_free_consistent(NULL, size, dma_addr, dma_handle);
108 }
109
110 #undef dma_alloc_coherent
111 #define dma_alloc_coherent snd_dma_alloc_coherent1
112 #undef dma_free_coherent
113 #define dma_free_coherent snd_dma_free_coherent1
114
115
116 #if defined(__i386__) || defined(__ppc__) || defined(__x86_64__)
117
118 /*
119  * A hack to allocate large buffers via dma_alloc_coherent()
120  *
121  * since dma_alloc_coherent always tries GFP_DMA when the requested
122  * pci memory region is below 32bit, it happens quite often that even
123  * 2 order of pages cannot be allocated.
124  *
125  * so in the following, we allocate at first without dma_mask, so that
126  * allocation will be done without GFP_DMA.  if the area doesn't match
127  * with the requested region, then realloate with the original dma_mask
128  * again.
129  *
130  * Really, we want to move this type of thing into dma_alloc_coherent()
131  * so dma_mask doesn't have to be messed with.
132  */
133
134 static void *snd_dma_hack_alloc_coherent(struct device *dev, size_t size,
135                                          dma_addr_t *dma_handle, int flags)
136 {
137         void *ret;
138         u64 dma_mask;
139
140         if (dev == NULL || !dev->dma_mask)
141                 return dma_alloc_coherent(dev, size, dma_handle, flags);
142         dma_mask = *dev->dma_mask;
143         *dev->dma_mask = 0xffffffff;    /* do without masking */
144         ret = dma_alloc_coherent(dev, size, dma_handle, flags);
145         *dev->dma_mask = dma_mask;      /* restore */
146         if (ret) {
147                 /* obtained address is out of range? */
148                 if (((unsigned long)*dma_handle + size - 1) & ~dma_mask) {
149                         /* reallocate with the proper mask */
150                         dma_free_coherent(dev, size, ret, *dma_handle);
151                         ret = dma_alloc_coherent(dev, size, dma_handle, flags);
152                 }
153         } else {
154                 /* wish to success now with the proper mask... */
155                 if (dma_mask != 0xffffffffUL)
156                         ret = dma_alloc_coherent(dev, size, dma_handle, flags);
157         }
158         return ret;
159 }
160
161 /* redefine dma_alloc_coherent for some architectures */
162 #undef dma_alloc_coherent
163 #define dma_alloc_coherent snd_dma_hack_alloc_coherent
164
165 #endif /* arch */
166
167 /*
168  *
169  *  Generic memory allocators
170  *
171  */
172
173 static long snd_allocated_pages; /* holding the number of allocated pages */
174
175 static void mark_pages(void *res, int order)
176 {
177         struct page *page = virt_to_page(res);
178         struct page *last_page = page + (1 << order);
179         while (page < last_page)
180                 SetPageReserved(page++);
181         snd_allocated_pages += 1 << order;
182 }
183
184 static void unmark_pages(void *res, int order)
185 {
186         struct page *page = virt_to_page(res);
187         struct page *last_page = page + (1 << order);
188         while (page < last_page)
189                 ClearPageReserved(page++);
190         snd_allocated_pages -= 1 << order;
191 }
192
193 /**
194  * snd_malloc_pages - allocate pages with the given size
195  * @size: the size to allocate in bytes
196  * @gfp_flags: the allocation conditions, GFP_XXX
197  *
198  * Allocates the physically contiguous pages with the given size.
199  *
200  * Returns the pointer of the buffer, or NULL if no enoguh memory.
201  */
202 void *snd_malloc_pages(size_t size, unsigned int gfp_flags)
203 {
204         int pg;
205         void *res;
206
207         snd_assert(size > 0, return NULL);
208         snd_assert(gfp_flags != 0, return NULL);
209         for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
210         if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL) {
211                 mark_pages(res, pg);
212         }
213         return res;
214 }
215
216 /**
217  * snd_malloc_pages_fallback - allocate pages with the given size with fallback
218  * @size: the requested size to allocate in bytes
219  * @gfp_flags: the allocation conditions, GFP_XXX
220  * @res_size: the pointer to store the size of buffer actually allocated
221  *
222  * Allocates the physically contiguous pages with the given request
223  * size.  When no space is left, this function reduces the size and
224  * tries to allocate again.  The size actually allocated is stored in
225  * res_size argument.
226  *
227  * Returns the pointer of the buffer, or NULL if no enoguh memory.
228  */            
229 void *snd_malloc_pages_fallback(size_t size, unsigned int gfp_flags, size_t *res_size)
230 {
231         void *res;
232
233         snd_assert(size > 0, return NULL);
234         snd_assert(res_size != NULL, return NULL);
235         do {
236                 if ((res = snd_malloc_pages(size, gfp_flags)) != NULL) {
237                         *res_size = size;
238                         return res;
239                 }
240                 size >>= 1;
241         } while (size >= PAGE_SIZE);
242         return NULL;
243 }
244
245 /**
246  * snd_free_pages - release the pages
247  * @ptr: the buffer pointer to release
248  * @size: the allocated buffer size
249  *
250  * Releases the buffer allocated via snd_malloc_pages().
251  */
252 void snd_free_pages(void *ptr, size_t size)
253 {
254         int pg;
255
256         if (ptr == NULL)
257                 return;
258         for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
259         unmark_pages(ptr, pg);
260         free_pages((unsigned long) ptr, pg);
261 }
262
263 /*
264  *
265  *  Bus-specific memory allocators
266  *
267  */
268
269 static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
270 {
271         int pg;
272         void *res;
273         unsigned int gfp_flags;
274
275         snd_assert(size > 0, return NULL);
276         snd_assert(dma != NULL, return NULL);
277         pg = get_order(size);
278         gfp_flags = GFP_KERNEL;
279         if (pg > 0)
280                 gfp_flags |= __GFP_NOWARN;
281         res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
282         if (res != NULL)
283                 mark_pages(res, pg);
284
285         return res;
286 }
287
288 static void *snd_malloc_dev_pages_fallback(struct device *dev, size_t size,
289                                            dma_addr_t *dma, size_t *res_size)
290 {
291         void *res;
292
293         snd_assert(res_size != NULL, return NULL);
294         do {
295                 if ((res = snd_malloc_dev_pages(dev, size, dma)) != NULL) {
296                         *res_size = size;
297                         return res;
298                 }
299                 size >>= 1;
300         } while (size >= PAGE_SIZE);
301         return NULL;
302 }
303
304 static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
305                                dma_addr_t dma)
306 {
307         int pg;
308
309         if (ptr == NULL)
310                 return;
311         pg = get_order(size);
312         unmark_pages(ptr, pg);
313         dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
314 }
315
316 #ifdef CONFIG_SBUS
317
318 static void *snd_malloc_sbus_pages(struct device *dev, size_t size,
319                                    dma_addr_t *dma_addr)
320 {
321         struct sbus_dev *sdev = (struct sbus_dev *)dev;
322         int pg;
323         void *res;
324
325         snd_assert(size > 0, return NULL);
326         snd_assert(dma_addr != NULL, return NULL);
327         for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
328         res = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr);
329         if (res != NULL) {
330                 mark_pages(res, pg);
331         }
332         return res;
333 }
334
335 static void *snd_malloc_sbus_pages_fallback(struct device *dev, size_t size,
336                                             dma_addr_t *dma_addr, size_t *res_size)
337 {
338         void *res;
339
340         snd_assert(res_size != NULL, return NULL);
341         do {
342                 if ((res = snd_malloc_sbus_pages(dev, size, dma_addr)) != NULL) {
343                         *res_size = size;
344                         return res;
345                 }
346                 size >>= 1;
347         } while (size >= PAGE_SIZE);
348         return NULL;
349 }
350
351 static void snd_free_sbus_pages(struct device *dev, size_t size,
352                                 void *ptr, dma_addr_t dma_addr)
353 {
354         struct sbus_dev *sdev = (struct sbus_dev *)dev;
355         int pg;
356
357         if (ptr == NULL)
358                 return;
359         for (pg = 0; PAGE_SIZE * (1 << pg) < size; pg++);
360         unmark_pages(ptr, pg);
361         sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr);
362 }
363
364 #endif /* CONFIG_SBUS */
365
366 /*
367  *
368  *  ALSA generic memory management
369  *
370  */
371
372
373 /*
374  * compare the two devices
375  * returns non-zero if matched.
376  */
377 static int compare_device(const struct snd_dma_device *a, const struct snd_dma_device *b, int allow_unused)
378 {
379         if (a->type != b->type)
380                 return 0;
381         if (a->id != b->id) {
382                 if (! allow_unused || (a->id != SNDRV_DMA_DEVICE_UNUSED && b->id != SNDRV_DMA_DEVICE_UNUSED))
383                         return 0;
384         }
385         return a->dev == b->dev;
386 }
387
388 /**
389  * snd_dma_alloc_pages - allocate the buffer area according to the given type
390  * @dev: the buffer device info
391  * @size: the buffer size to allocate
392  * @dmab: buffer allocation record to store the allocated data
393  *
394  * Calls the memory-allocator function for the corresponding
395  * buffer type.
396  * 
397  * Returns zero if the buffer with the given size is allocated successfuly,
398  * other a negative value at error.
399  */
400 int snd_dma_alloc_pages(const struct snd_dma_device *dev, size_t size,
401                         struct snd_dma_buffer *dmab)
402 {
403         snd_assert(dev != NULL, return -ENXIO);
404         snd_assert(size > 0, return -ENXIO);
405         snd_assert(dmab != NULL, return -ENXIO);
406
407         dmab->bytes = 0;
408         switch (dev->type) {
409         case SNDRV_DMA_TYPE_CONTINUOUS:
410                 dmab->area = snd_malloc_pages(size, (unsigned long)dev->dev);
411                 dmab->addr = 0;
412                 break;
413 #ifdef CONFIG_SBUS
414         case SNDRV_DMA_TYPE_SBUS:
415                 dmab->area = snd_malloc_sbus_pages(dev->dev, size, &dmab->addr);
416                 break;
417 #endif
418         case SNDRV_DMA_TYPE_DEV:
419                 dmab->area = snd_malloc_dev_pages(dev->dev, size, &dmab->addr);
420                 break;
421         case SNDRV_DMA_TYPE_DEV_SG:
422                 snd_malloc_sgbuf_pages(dev, size, dmab, NULL);
423                 break;
424         default:
425                 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dev->type);
426                 dmab->area = NULL;
427                 dmab->addr = 0;
428                 return -ENXIO;
429         }
430         if (! dmab->area)
431                 return -ENOMEM;
432         dmab->bytes = size;
433         return 0;
434 }
435
436 /**
437  * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
438  * @dev: the buffer device info
439  * @size: the buffer size to allocate
440  * @dmab: buffer allocation record to store the allocated data
441  *
442  * Calls the memory-allocator function for the corresponding
443  * buffer type.  When no space is left, this function reduces the size and
444  * tries to allocate again.  The size actually allocated is stored in
445  * res_size argument.
446  * 
447  * Returns zero if the buffer with the given size is allocated successfuly,
448  * other a negative value at error.
449  */
450 int snd_dma_alloc_pages_fallback(const struct snd_dma_device *dev, size_t size,
451                                  struct snd_dma_buffer *dmab)
452 {
453         snd_assert(dev != NULL, return -ENXIO);
454         snd_assert(size > 0, return -ENXIO);
455         snd_assert(dmab != NULL, return -ENXIO);
456
457         dmab->bytes = 0;
458         switch (dev->type) {
459         case SNDRV_DMA_TYPE_CONTINUOUS:
460                 dmab->area = snd_malloc_pages_fallback(size, (unsigned long)dev->dev, &dmab->bytes);
461                 dmab->addr = 0;
462                 break;
463 #ifdef CONFIG_SBUS
464         case SNDRV_DMA_TYPE_SBUS:
465                 dmab->area = snd_malloc_sbus_pages_fallback(dev->dev, size, &dmab->addr, &dmab->bytes);
466                 break;
467 #endif
468         case SNDRV_DMA_TYPE_DEV:
469                 dmab->area = snd_malloc_dev_pages_fallback(dev->dev, size, &dmab->addr, &dmab->bytes);
470                 break;
471         case SNDRV_DMA_TYPE_DEV_SG:
472                 snd_malloc_sgbuf_pages(dev, size, dmab, &dmab->bytes);
473                 break;
474         default:
475                 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dev->type);
476                 dmab->area = NULL;
477                 dmab->addr = 0;
478                 return -ENXIO;
479         }
480         if (! dmab->area)
481                 return -ENOMEM;
482         return 0;
483 }
484
485
486 /**
487  * snd_dma_free_pages - release the allocated buffer
488  * @dev: the buffer device info
489  * @dmbab: the buffer allocation record to release
490  *
491  * Releases the allocated buffer via snd_dma_alloc_pages().
492  */
493 void snd_dma_free_pages(const struct snd_dma_device *dev, struct snd_dma_buffer *dmab)
494 {
495         switch (dev->type) {
496         case SNDRV_DMA_TYPE_CONTINUOUS:
497                 snd_free_pages(dmab->area, dmab->bytes);
498                 break;
499 #ifdef CONFIG_SBUS
500         case SNDRV_DMA_TYPE_SBUS:
501                 snd_free_sbus_pages(dev->dev, dmab->bytes, dmab->area, dmab->addr);
502                 break;
503 #endif
504         case SNDRV_DMA_TYPE_DEV:
505                 snd_free_dev_pages(dev->dev, dmab->bytes, dmab->area, dmab->addr);
506                 break;
507         case SNDRV_DMA_TYPE_DEV_SG:
508                 snd_free_sgbuf_pages(dmab);
509                 break;
510         default:
511                 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dev->type);
512         }
513 }
514
515
516 /*
517  * search for the device
518  */
519 static struct snd_mem_list *mem_list_find(const struct snd_dma_device *dev, int search_empty)
520 {
521         struct list_head *p;
522         struct snd_mem_list *mem;
523
524         list_for_each(p, &mem_list_head) {
525                 mem = list_entry(p, struct snd_mem_list, list);
526                 if (mem->used && search_empty)
527                         continue;
528                 if (compare_device(&mem->dev, dev, search_empty))
529                         return mem;
530         }
531         return NULL;
532 }
533
534 /**
535  * snd_dma_get_reserved - get the reserved buffer for the given device
536  * @dev: the buffer device info
537  * @dmab: the buffer allocation record to store
538  *
539  * Looks for the reserved-buffer list and re-uses if the same buffer
540  * is found in the list.  When the buffer is found, it's marked as used.
541  * For unmarking the buffer, call snd_dma_free_reserved().
542  *
543  * Returns the size of buffer if the buffer is found, or zero if not found.
544  */
545 size_t snd_dma_get_reserved(const struct snd_dma_device *dev, struct snd_dma_buffer *dmab)
546 {
547         struct snd_mem_list *mem;
548
549         snd_assert(dev && dmab, return 0);
550
551         down(&list_mutex);
552         mem = mem_list_find(dev, 1);
553         if (mem) {
554                 mem->used = 1;
555                 mem->dev = *dev;
556                 *dmab = mem->buffer;
557                 up(&list_mutex);
558                 return dmab->bytes;
559         }
560         up(&list_mutex);
561         return 0;
562 }
563
564 /**
565  * snd_dma_free_reserved - unmark the reserved buffer
566  * @dev: the buffer device info
567  *
568  * Looks for the matching reserved buffer and erases the mark on it
569  * if found.
570  *
571  * Returns zero.
572  */
573 int snd_dma_free_reserved(const struct snd_dma_device *dev)
574 {
575         struct snd_mem_list *mem;
576
577         snd_assert(dev, return -EINVAL);
578         down(&list_mutex);
579         mem = mem_list_find(dev, 0);
580         if (mem)
581                 mem->used = 0;
582         up(&list_mutex);
583         return 0;
584 }
585
586 /**
587  * snd_dma_set_reserved - reserve the buffer
588  * @dev: the buffer device info
589  * @dmab: the buffer to reserve
590  *
591  * Reserves the given buffer as a reserved buffer.
592  * When an old reserved buffer already exists, the old one is released
593  * and replaced with the new one.
594  *
595  * When NULL buffer pointer or zero buffer size is given, the existing
596  * buffer is released and the entry is removed.
597  * 
598  * Returns zero if successful, or a negative code at error.
599  */
600 int snd_dma_set_reserved(const struct snd_dma_device *dev, struct snd_dma_buffer *dmab)
601 {
602         struct snd_mem_list *mem;
603
604         snd_assert(dev, return -EINVAL);
605         down(&list_mutex);
606         mem = mem_list_find(dev, 0);
607         if (mem) {
608                 if (mem->used)
609                         printk(KERN_WARNING "snd-page-alloc: releasing the used block (type=%d, id=0x%x\n", mem->dev.type, mem->dev.id);
610                 snd_dma_free_pages(dev, &mem->buffer);
611                 if (! dmab || ! dmab->bytes) {
612                         /* remove the entry */
613                         list_del(&mem->list);
614                         kfree(mem);
615                         up(&list_mutex);
616                         return 0;
617                 }
618         } else {
619                 if (! dmab || ! dmab->bytes) {
620                         up(&list_mutex);
621                         return 0;
622                 }
623                 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
624                 if (! mem) {
625                         up(&list_mutex);
626                         return -ENOMEM;
627                 }
628                 mem->dev = *dev;
629                 list_add_tail(&mem->list, &mem_list_head);
630         }
631         /* store the entry */
632         mem->used = 1;
633         mem->buffer = *dmab;
634         up(&list_mutex);
635         return 0;
636 }
637
638 /*
639  * purge all reserved buffers
640  */
641 static void free_all_reserved_pages(void)
642 {
643         struct list_head *p;
644         struct snd_mem_list *mem;
645
646         down(&list_mutex);
647         while (! list_empty(&mem_list_head)) {
648                 p = mem_list_head.next;
649                 mem = list_entry(p, struct snd_mem_list, list);
650                 list_del(p);
651                 snd_dma_free_pages(&mem->dev, &mem->buffer);
652                 kfree(mem);
653         }
654         up(&list_mutex);
655 }
656
657
658
659 /*
660  * allocation of buffers for pre-defined devices
661  */
662
663 #ifdef CONFIG_PCI
664 /* FIXME: for pci only - other bus? */
665 struct prealloc_dev {
666         unsigned short vendor;
667         unsigned short device;
668         unsigned long dma_mask;
669         unsigned int size;
670         unsigned int buffers;
671 };
672
673 #define HAMMERFALL_BUFFER_SIZE    (16*1024*4*(26+1))
674
675 static struct prealloc_dev prealloc_devices[] __initdata = {
676         {
677                 /* hammerfall */
678                 .vendor = 0x10ee,
679                 .device = 0x3fc4,
680                 .dma_mask = 0xffffffff,
681                 .size = HAMMERFALL_BUFFER_SIZE,
682                 .buffers = 2
683         },
684         {
685                 /* HDSP */
686                 .vendor = 0x10ee,
687                 .device = 0x3fc5,
688                 .dma_mask = 0xffffffff,
689                 .size = HAMMERFALL_BUFFER_SIZE,
690                 .buffers = 2
691         },
692         { }, /* terminator */
693 };
694
695 /*
696  * compose a snd_dma_device struct for the PCI device
697  */
698 static inline void snd_dma_device_pci(struct snd_dma_device *dev, struct pci_dev *pci, unsigned int id)
699 {
700         memset(dev, 0, sizeof(*dev));
701         dev->type = SNDRV_DMA_TYPE_DEV;
702         dev->dev = snd_dma_pci_data(pci);
703         dev->id = id;
704 }
705
706 static void __init preallocate_cards(void)
707 {
708         struct pci_dev *pci = NULL;
709         int card;
710
711         card = 0;
712
713         while ((pci = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pci)) != NULL) {
714                 struct prealloc_dev *dev;
715                 unsigned int i;
716                 if (card >= SNDRV_CARDS)
717                         break;
718                 for (dev = prealloc_devices; dev->vendor; dev++) {
719                         if (dev->vendor == pci->vendor && dev->device == pci->device)
720                                 break;
721                 }
722                 if (! dev->vendor)
723                         continue;
724                 if (! enable[card++]) {
725                         printk(KERN_DEBUG "snd-page-alloc: skipping card %d, device %04x:%04x\n", card, pci->vendor, pci->device);
726                         continue;
727                 }
728                         
729                 if (pci_set_dma_mask(pci, dev->dma_mask) < 0 ||
730                     pci_set_consistent_dma_mask(pci, dev->dma_mask) < 0) {
731                         printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", dev->dma_mask, dev->vendor, dev->device);
732                         continue;
733                 }
734                 for (i = 0; i < dev->buffers; i++) {
735                         struct snd_mem_list *mem;
736                         mem = kmalloc(sizeof(*mem), GFP_KERNEL);
737                         if (! mem) {
738                                 printk(KERN_WARNING "snd-page-alloc: can't malloc memlist\n");
739                                 break;
740                         }
741                         memset(mem, 0, sizeof(*mem));
742                         snd_dma_device_pci(&mem->dev, pci, SNDRV_DMA_DEVICE_UNUSED);
743                         if (snd_dma_alloc_pages(&mem->dev, dev->size, &mem->buffer) < 0) {
744                                 printk(KERN_WARNING "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", dev->size);
745                                 kfree(mem);
746                         } else {
747                                 down(&list_mutex);
748                                 list_add_tail(&mem->list, &mem_list_head);
749                                 up(&list_mutex);
750                         }
751                 }
752         }
753 }
754 #else
755 #define preallocate_cards()     /* NOP */
756 #endif
757
758
759 #ifdef CONFIG_PROC_FS
760 /*
761  * proc file interface
762  */
763 static int snd_mem_proc_read(char *page, char **start, off_t off,
764                              int count, int *eof, void *data)
765 {
766         int len = 0;
767         long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
768         struct list_head *p;
769         struct snd_mem_list *mem;
770         int devno;
771
772         down(&list_mutex);
773         len += sprintf(page + len, "pages  : %li bytes (%li pages per %likB)\n",
774                        pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
775         devno = 0;
776         list_for_each(p, &mem_list_head) {
777                 mem = list_entry(p, struct snd_mem_list, list);
778                 devno++;
779                 len += sprintf(page + len, "buffer %d : ", devno);
780                 if (mem->dev.id == SNDRV_DMA_DEVICE_UNUSED)
781                         len += sprintf(page + len, "UNUSED");
782                 else
783                         len += sprintf(page + len, "ID %08x", mem->dev.id);
784                 len += sprintf(page + len, " : type ");
785                 switch (mem->dev.type) {
786                 case SNDRV_DMA_TYPE_CONTINUOUS:
787                         len += sprintf(page + len, "CONT [%p]", mem->dev.dev);
788                         break;
789 #ifdef CONFIG_SBUS
790                 case SNDRV_DMA_TYPE_SBUS:
791                         {
792                                 struct sbus_dev *sdev = (struct sbus_dev *)(mem->dev.dev);
793                                 len += sprintf(page + len, "SBUS [%x]", sdev->slot);
794                         }
795                         break;
796 #endif
797                 case SNDRV_DMA_TYPE_DEV:
798                 case SNDRV_DMA_TYPE_DEV_SG:
799                         if (mem->dev.dev) {
800                                 len += sprintf(page + len, "%s [%s]",
801                                                mem->dev.type == SNDRV_DMA_TYPE_DEV_SG ? "DEV-SG" : "DEV",
802                                                mem->dev.dev->bus_id);
803                         } else
804                                 len += sprintf(page + len, "ISA");
805                         break;
806                 default:
807                         len += sprintf(page + len, "UNKNOWN");
808                         break;
809                 }
810                 len += sprintf(page + len, "\n  addr = 0x%lx, size = %d bytes, used = %s\n",
811                                (unsigned long)mem->buffer.addr, (int)mem->buffer.bytes,
812                                mem->used ? "yes" : "no");
813         }
814         up(&list_mutex);
815         return len;
816 }
817 #endif /* CONFIG_PROC_FS */
818
819 /*
820  * module entry
821  */
822
823 static int __init snd_mem_init(void)
824 {
825 #ifdef CONFIG_PROC_FS
826         create_proc_read_entry("driver/snd-page-alloc", 0, 0, snd_mem_proc_read, NULL);
827 #endif
828         preallocate_cards();
829         return 0;
830 }
831
832 static void __exit snd_mem_exit(void)
833 {
834         remove_proc_entry("driver/snd-page-alloc", NULL);
835         free_all_reserved_pages();
836         if (snd_allocated_pages > 0)
837                 printk(KERN_ERR "snd-malloc: Memory leak?  pages not freed = %li\n", snd_allocated_pages);
838 }
839
840
841 module_init(snd_mem_init)
842 module_exit(snd_mem_exit)
843
844
845 #ifndef MODULE
846
847 /* format is: snd-page-alloc=enable */
848
849 static int __init snd_mem_setup(char *str)
850 {
851         static unsigned __initdata nr_dev = 0;
852
853         if (nr_dev >= SNDRV_CARDS)
854                 return 0;
855         (void)(get_option(&str,&enable[nr_dev]) == 2);
856         nr_dev++;
857         return 1;
858 }
859
860 __setup("snd-page-alloc=", snd_mem_setup);
861
862 #endif
863
864 /*
865  * exports
866  */
867 EXPORT_SYMBOL(snd_dma_alloc_pages);
868 EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
869 EXPORT_SYMBOL(snd_dma_free_pages);
870
871 EXPORT_SYMBOL(snd_dma_get_reserved);
872 EXPORT_SYMBOL(snd_dma_free_reserved);
873 EXPORT_SYMBOL(snd_dma_set_reserved);
874
875 EXPORT_SYMBOL(snd_malloc_pages);
876 EXPORT_SYMBOL(snd_malloc_pages_fallback);
877 EXPORT_SYMBOL(snd_free_pages);