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