memalloc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. #include <linux/config.h>
  24. #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>
  30. #include <asm/uaccess.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. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@suse.cz>");
  39. MODULE_DESCRIPTION("Memory allocator for ALSA system.");
  40. MODULE_LICENSE("GPL");
  41. #ifndef SNDRV_CARDS
  42. #define SNDRV_CARDS 8
  43. #endif
  44. /*
  45. */
  46. void *snd_malloc_sgbuf_pages(struct device *device,
  47. size_t size, struct snd_dma_buffer *dmab,
  48. size_t *res_size);
  49. int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
  50. /*
  51. */
  52. static DECLARE_MUTEX(list_mutex);
  53. static LIST_HEAD(mem_list_head);
  54. /* buffer preservation list */
  55. struct snd_mem_list {
  56. struct snd_dma_buffer buffer;
  57. unsigned int id;
  58. struct list_head list;
  59. };
  60. /* id for pre-allocated buffers */
  61. #define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
  62. #ifdef CONFIG_SND_DEBUG
  63. #define __ASTRING__(x) #x
  64. #define snd_assert(expr, args...) do {\
  65. if (!(expr)) {\
  66. printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
  67. args;\
  68. }\
  69. } while (0)
  70. #else
  71. #define snd_assert(expr, args...) /**/
  72. #endif
  73. /*
  74. * Hacks
  75. */
  76. #if defined(__i386__) || defined(__ppc__) || defined(__x86_64__)
  77. /*
  78. * A hack to allocate large buffers via dma_alloc_coherent()
  79. *
  80. * since dma_alloc_coherent always tries GFP_DMA when the requested
  81. * pci memory region is below 32bit, it happens quite often that even
  82. * 2 order of pages cannot be allocated.
  83. *
  84. * so in the following, we allocate at first without dma_mask, so that
  85. * allocation will be done without GFP_DMA. if the area doesn't match
  86. * with the requested region, then realloate with the original dma_mask
  87. * again.
  88. *
  89. * Really, we want to move this type of thing into dma_alloc_coherent()
  90. * so dma_mask doesn't have to be messed with.
  91. */
  92. static void *snd_dma_hack_alloc_coherent(struct device *dev, size_t size,
  93. dma_addr_t *dma_handle,
  94. gfp_t flags)
  95. {
  96. void *ret;
  97. u64 dma_mask, coherent_dma_mask;
  98. if (dev == NULL || !dev->dma_mask)
  99. return dma_alloc_coherent(dev, size, dma_handle, flags);
  100. dma_mask = *dev->dma_mask;
  101. coherent_dma_mask = dev->coherent_dma_mask;
  102. *dev->dma_mask = 0xffffffff; /* do without masking */
  103. dev->coherent_dma_mask = 0xffffffff; /* do without masking */
  104. ret = dma_alloc_coherent(dev, size, dma_handle, flags);
  105. *dev->dma_mask = dma_mask; /* restore */
  106. dev->coherent_dma_mask = coherent_dma_mask; /* restore */
  107. if (ret) {
  108. /* obtained address is out of range? */
  109. if (((unsigned long)*dma_handle + size - 1) & ~dma_mask) {
  110. /* reallocate with the proper mask */
  111. dma_free_coherent(dev, size, ret, *dma_handle);
  112. ret = dma_alloc_coherent(dev, size, dma_handle, flags);
  113. }
  114. } else {
  115. /* wish to success now with the proper mask... */
  116. if (dma_mask != 0xffffffffUL) {
  117. /* allocation with GFP_ATOMIC to avoid the long stall */
  118. flags &= ~GFP_KERNEL;
  119. flags |= GFP_ATOMIC;
  120. ret = dma_alloc_coherent(dev, size, dma_handle, flags);
  121. }
  122. }
  123. return ret;
  124. }
  125. /* redefine dma_alloc_coherent for some architectures */
  126. #undef dma_alloc_coherent
  127. #define dma_alloc_coherent snd_dma_hack_alloc_coherent
  128. #endif /* arch */
  129. #if ! defined(__arm__)
  130. #define NEED_RESERVE_PAGES
  131. #endif
  132. /*
  133. *
  134. * Generic memory allocators
  135. *
  136. */
  137. static long snd_allocated_pages; /* holding the number of allocated pages */
  138. static inline void inc_snd_pages(int order)
  139. {
  140. snd_allocated_pages += 1 << order;
  141. }
  142. static inline void dec_snd_pages(int order)
  143. {
  144. snd_allocated_pages -= 1 << order;
  145. }
  146. static void mark_pages(struct page *page, int order)
  147. {
  148. struct page *last_page = page + (1 << order);
  149. while (page < last_page)
  150. SetPageReserved(page++);
  151. }
  152. static void unmark_pages(struct page *page, int order)
  153. {
  154. struct page *last_page = page + (1 << order);
  155. while (page < last_page)
  156. ClearPageReserved(page++);
  157. }
  158. /**
  159. * snd_malloc_pages - allocate pages with the given size
  160. * @size: the size to allocate in bytes
  161. * @gfp_flags: the allocation conditions, GFP_XXX
  162. *
  163. * Allocates the physically contiguous pages with the given size.
  164. *
  165. * Returns the pointer of the buffer, or NULL if no enoguh memory.
  166. */
  167. void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
  168. {
  169. int pg;
  170. void *res;
  171. snd_assert(size > 0, return NULL);
  172. snd_assert(gfp_flags != 0, return NULL);
  173. gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
  174. pg = get_order(size);
  175. if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL) {
  176. mark_pages(virt_to_page(res), pg);
  177. inc_snd_pages(pg);
  178. }
  179. return res;
  180. }
  181. /**
  182. * snd_free_pages - release the pages
  183. * @ptr: the buffer pointer to release
  184. * @size: the allocated buffer size
  185. *
  186. * Releases the buffer allocated via snd_malloc_pages().
  187. */
  188. void snd_free_pages(void *ptr, size_t size)
  189. {
  190. int pg;
  191. if (ptr == NULL)
  192. return;
  193. pg = get_order(size);
  194. dec_snd_pages(pg);
  195. unmark_pages(virt_to_page(ptr), pg);
  196. free_pages((unsigned long) ptr, pg);
  197. }
  198. /*
  199. *
  200. * Bus-specific memory allocators
  201. *
  202. */
  203. /* allocate the coherent DMA pages */
  204. static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
  205. {
  206. int pg;
  207. void *res;
  208. gfp_t gfp_flags;
  209. snd_assert(size > 0, return NULL);
  210. snd_assert(dma != NULL, return NULL);
  211. pg = get_order(size);
  212. gfp_flags = GFP_KERNEL
  213. | __GFP_COMP /* compound page lets parts be mapped */
  214. | __GFP_NORETRY /* don't trigger OOM-killer */
  215. | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
  216. res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
  217. if (res != NULL) {
  218. #ifdef NEED_RESERVE_PAGES
  219. mark_pages(virt_to_page(res), pg); /* should be dma_to_page() */
  220. #endif
  221. inc_snd_pages(pg);
  222. }
  223. return res;
  224. }
  225. /* free the coherent DMA pages */
  226. static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
  227. dma_addr_t dma)
  228. {
  229. int pg;
  230. if (ptr == NULL)
  231. return;
  232. pg = get_order(size);
  233. dec_snd_pages(pg);
  234. #ifdef NEED_RESERVE_PAGES
  235. unmark_pages(virt_to_page(ptr), pg); /* should be dma_to_page() */
  236. #endif
  237. dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
  238. }
  239. #ifdef CONFIG_SBUS
  240. static void *snd_malloc_sbus_pages(struct device *dev, size_t size,
  241. dma_addr_t *dma_addr)
  242. {
  243. struct sbus_dev *sdev = (struct sbus_dev *)dev;
  244. int pg;
  245. void *res;
  246. snd_assert(size > 0, return NULL);
  247. snd_assert(dma_addr != NULL, return NULL);
  248. pg = get_order(size);
  249. res = sbus_alloc_consistent(sdev, PAGE_SIZE * (1 << pg), dma_addr);
  250. if (res != NULL)
  251. inc_snd_pages(pg);
  252. return res;
  253. }
  254. static void snd_free_sbus_pages(struct device *dev, size_t size,
  255. void *ptr, dma_addr_t dma_addr)
  256. {
  257. struct sbus_dev *sdev = (struct sbus_dev *)dev;
  258. int pg;
  259. if (ptr == NULL)
  260. return;
  261. pg = get_order(size);
  262. dec_snd_pages(pg);
  263. sbus_free_consistent(sdev, PAGE_SIZE * (1 << pg), ptr, dma_addr);
  264. }
  265. #endif /* CONFIG_SBUS */
  266. /*
  267. *
  268. * ALSA generic memory management
  269. *
  270. */
  271. /**
  272. * snd_dma_alloc_pages - allocate the buffer area according to the given type
  273. * @type: the DMA buffer type
  274. * @device: the device pointer
  275. * @size: the buffer size to allocate
  276. * @dmab: buffer allocation record to store the allocated data
  277. *
  278. * Calls the memory-allocator function for the corresponding
  279. * buffer type.
  280. *
  281. * Returns zero if the buffer with the given size is allocated successfuly,
  282. * other a negative value at error.
  283. */
  284. int snd_dma_alloc_pages(int type, struct device *device, size_t size,
  285. struct snd_dma_buffer *dmab)
  286. {
  287. snd_assert(size > 0, return -ENXIO);
  288. snd_assert(dmab != NULL, return -ENXIO);
  289. dmab->dev.type = type;
  290. dmab->dev.dev = device;
  291. dmab->bytes = 0;
  292. switch (type) {
  293. case SNDRV_DMA_TYPE_CONTINUOUS:
  294. dmab->area = snd_malloc_pages(size, (unsigned long)device);
  295. dmab->addr = 0;
  296. break;
  297. #ifdef CONFIG_SBUS
  298. case SNDRV_DMA_TYPE_SBUS:
  299. dmab->area = snd_malloc_sbus_pages(device, size, &dmab->addr);
  300. break;
  301. #endif
  302. case SNDRV_DMA_TYPE_DEV:
  303. dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
  304. break;
  305. case SNDRV_DMA_TYPE_DEV_SG:
  306. snd_malloc_sgbuf_pages(device, size, dmab, NULL);
  307. break;
  308. default:
  309. printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
  310. dmab->area = NULL;
  311. dmab->addr = 0;
  312. return -ENXIO;
  313. }
  314. if (! dmab->area)
  315. return -ENOMEM;
  316. dmab->bytes = size;
  317. return 0;
  318. }
  319. /**
  320. * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
  321. * @type: the DMA buffer type
  322. * @device: the device pointer
  323. * @size: the buffer size to allocate
  324. * @dmab: buffer allocation record to store the allocated data
  325. *
  326. * Calls the memory-allocator function for the corresponding
  327. * buffer type. When no space is left, this function reduces the size and
  328. * tries to allocate again. The size actually allocated is stored in
  329. * res_size argument.
  330. *
  331. * Returns zero if the buffer with the given size is allocated successfuly,
  332. * other a negative value at error.
  333. */
  334. int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
  335. struct snd_dma_buffer *dmab)
  336. {
  337. int err;
  338. snd_assert(size > 0, return -ENXIO);
  339. snd_assert(dmab != NULL, return -ENXIO);
  340. while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
  341. if (err != -ENOMEM)
  342. return err;
  343. size >>= 1;
  344. if (size <= PAGE_SIZE)
  345. return -ENOMEM;
  346. }
  347. if (! dmab->area)
  348. return -ENOMEM;
  349. return 0;
  350. }
  351. /**
  352. * snd_dma_free_pages - release the allocated buffer
  353. * @dmab: the buffer allocation record to release
  354. *
  355. * Releases the allocated buffer via snd_dma_alloc_pages().
  356. */
  357. void snd_dma_free_pages(struct snd_dma_buffer *dmab)
  358. {
  359. switch (dmab->dev.type) {
  360. case SNDRV_DMA_TYPE_CONTINUOUS:
  361. snd_free_pages(dmab->area, dmab->bytes);
  362. break;
  363. #ifdef CONFIG_SBUS
  364. case SNDRV_DMA_TYPE_SBUS:
  365. snd_free_sbus_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
  366. break;
  367. #endif
  368. case SNDRV_DMA_TYPE_DEV:
  369. snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
  370. break;
  371. case SNDRV_DMA_TYPE_DEV_SG:
  372. snd_free_sgbuf_pages(dmab);
  373. break;
  374. default:
  375. printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
  376. }
  377. }
  378. /**
  379. * snd_dma_get_reserved - get the reserved buffer for the given device
  380. * @dmab: the buffer allocation record to store
  381. * @id: the buffer id
  382. *
  383. * Looks for the reserved-buffer list and re-uses if the same buffer
  384. * is found in the list. When the buffer is found, it's removed from the free list.
  385. *
  386. * Returns the size of buffer if the buffer is found, or zero if not found.
  387. */
  388. size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
  389. {
  390. struct list_head *p;
  391. struct snd_mem_list *mem;
  392. snd_assert(dmab, return 0);
  393. down(&list_mutex);
  394. list_for_each(p, &mem_list_head) {
  395. mem = list_entry(p, struct snd_mem_list, list);
  396. if (mem->id == id &&
  397. (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
  398. ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
  399. struct device *dev = dmab->dev.dev;
  400. list_del(p);
  401. *dmab = mem->buffer;
  402. if (dmab->dev.dev == NULL)
  403. dmab->dev.dev = dev;
  404. kfree(mem);
  405. up(&list_mutex);
  406. return dmab->bytes;
  407. }
  408. }
  409. up(&list_mutex);
  410. return 0;
  411. }
  412. /**
  413. * snd_dma_reserve_buf - reserve the buffer
  414. * @dmab: the buffer to reserve
  415. * @id: the buffer id
  416. *
  417. * Reserves the given buffer as a reserved buffer.
  418. *
  419. * Returns zero if successful, or a negative code at error.
  420. */
  421. int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
  422. {
  423. struct snd_mem_list *mem;
  424. snd_assert(dmab, return -EINVAL);
  425. mem = kmalloc(sizeof(*mem), GFP_KERNEL);
  426. if (! mem)
  427. return -ENOMEM;
  428. down(&list_mutex);
  429. mem->buffer = *dmab;
  430. mem->id = id;
  431. list_add_tail(&mem->list, &mem_list_head);
  432. up(&list_mutex);
  433. return 0;
  434. }
  435. /*
  436. * purge all reserved buffers
  437. */
  438. static void free_all_reserved_pages(void)
  439. {
  440. struct list_head *p;
  441. struct snd_mem_list *mem;
  442. down(&list_mutex);
  443. while (! list_empty(&mem_list_head)) {
  444. p = mem_list_head.next;
  445. mem = list_entry(p, struct snd_mem_list, list);
  446. list_del(p);
  447. snd_dma_free_pages(&mem->buffer);
  448. kfree(mem);
  449. }
  450. up(&list_mutex);
  451. }
  452. #ifdef CONFIG_PROC_FS
  453. /*
  454. * proc file interface
  455. */
  456. #define SND_MEM_PROC_FILE "driver/snd-page-alloc"
  457. static struct proc_dir_entry *snd_mem_proc;
  458. static int snd_mem_proc_read(char *page, char **start, off_t off,
  459. int count, int *eof, void *data)
  460. {
  461. int len = 0;
  462. long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
  463. struct list_head *p;
  464. struct snd_mem_list *mem;
  465. int devno;
  466. static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
  467. down(&list_mutex);
  468. len += snprintf(page + len, count - len,
  469. "pages : %li bytes (%li pages per %likB)\n",
  470. pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
  471. devno = 0;
  472. list_for_each(p, &mem_list_head) {
  473. mem = list_entry(p, struct snd_mem_list, list);
  474. devno++;
  475. len += snprintf(page + len, count - len,
  476. "buffer %d : ID %08x : type %s\n",
  477. devno, mem->id, types[mem->buffer.dev.type]);
  478. len += snprintf(page + len, count - len,
  479. " addr = 0x%lx, size = %d bytes\n",
  480. (unsigned long)mem->buffer.addr, (int)mem->buffer.bytes);
  481. }
  482. up(&list_mutex);
  483. return len;
  484. }
  485. /* FIXME: for pci only - other bus? */
  486. #ifdef CONFIG_PCI
  487. #define gettoken(bufp) strsep(bufp, " \t\n")
  488. static int snd_mem_proc_write(struct file *file, const char __user *buffer,
  489. unsigned long count, void *data)
  490. {
  491. char buf[128];
  492. char *token, *p;
  493. if (count > ARRAY_SIZE(buf) - 1)
  494. count = ARRAY_SIZE(buf) - 1;
  495. if (copy_from_user(buf, buffer, count))
  496. return -EFAULT;
  497. buf[ARRAY_SIZE(buf) - 1] = '\0';
  498. p = buf;
  499. token = gettoken(&p);
  500. if (! token || *token == '#')
  501. return (int)count;
  502. if (strcmp(token, "add") == 0) {
  503. char *endp;
  504. int vendor, device, size, buffers;
  505. long mask;
  506. int i, alloced;
  507. struct pci_dev *pci;
  508. if ((token = gettoken(&p)) == NULL ||
  509. (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
  510. (token = gettoken(&p)) == NULL ||
  511. (device = simple_strtol(token, NULL, 0)) <= 0 ||
  512. (token = gettoken(&p)) == NULL ||
  513. (mask = simple_strtol(token, NULL, 0)) < 0 ||
  514. (token = gettoken(&p)) == NULL ||
  515. (size = memparse(token, &endp)) < 64*1024 ||
  516. size > 16*1024*1024 /* too big */ ||
  517. (token = gettoken(&p)) == NULL ||
  518. (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
  519. buffers > 4) {
  520. printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
  521. return (int)count;
  522. }
  523. vendor &= 0xffff;
  524. device &= 0xffff;
  525. alloced = 0;
  526. pci = NULL;
  527. while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
  528. if (mask > 0 && mask < 0xffffffff) {
  529. if (pci_set_dma_mask(pci, mask) < 0 ||
  530. pci_set_consistent_dma_mask(pci, mask) < 0) {
  531. printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
  532. return (int)count;
  533. }
  534. }
  535. for (i = 0; i < buffers; i++) {
  536. struct snd_dma_buffer dmab;
  537. memset(&dmab, 0, sizeof(dmab));
  538. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  539. size, &dmab) < 0) {
  540. printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
  541. pci_dev_put(pci);
  542. return (int)count;
  543. }
  544. snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
  545. }
  546. alloced++;
  547. }
  548. if (! alloced) {
  549. for (i = 0; i < buffers; i++) {
  550. struct snd_dma_buffer dmab;
  551. memset(&dmab, 0, sizeof(dmab));
  552. /* FIXME: We can allocate only in ZONE_DMA
  553. * without a device pointer!
  554. */
  555. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
  556. size, &dmab) < 0) {
  557. printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
  558. break;
  559. }
  560. snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
  561. }
  562. }
  563. } else if (strcmp(token, "erase") == 0)
  564. /* FIXME: need for releasing each buffer chunk? */
  565. free_all_reserved_pages();
  566. else
  567. printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
  568. return (int)count;
  569. }
  570. #endif /* CONFIG_PCI */
  571. #endif /* CONFIG_PROC_FS */
  572. /*
  573. * module entry
  574. */
  575. static int __init snd_mem_init(void)
  576. {
  577. #ifdef CONFIG_PROC_FS
  578. snd_mem_proc = create_proc_entry(SND_MEM_PROC_FILE, 0644, NULL);
  579. if (snd_mem_proc) {
  580. snd_mem_proc->read_proc = snd_mem_proc_read;
  581. #ifdef CONFIG_PCI
  582. snd_mem_proc->write_proc = snd_mem_proc_write;
  583. #endif
  584. }
  585. #endif
  586. return 0;
  587. }
  588. static void __exit snd_mem_exit(void)
  589. {
  590. remove_proc_entry(SND_MEM_PROC_FILE, NULL);
  591. free_all_reserved_pages();
  592. if (snd_allocated_pages > 0)
  593. printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
  594. }
  595. module_init(snd_mem_init)
  596. module_exit(snd_mem_exit)
  597. /*
  598. * exports
  599. */
  600. EXPORT_SYMBOL(snd_dma_alloc_pages);
  601. EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
  602. EXPORT_SYMBOL(snd_dma_free_pages);
  603. EXPORT_SYMBOL(snd_dma_get_reserved_buf);
  604. EXPORT_SYMBOL(snd_dma_reserve_buf);
  605. EXPORT_SYMBOL(snd_malloc_pages);
  606. EXPORT_SYMBOL(snd_free_pages);