sgbuf.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Scatter-Gather buffer
  3. *
  4. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/config.h>
  22. #include <linux/slab.h>
  23. #include <linux/mm.h>
  24. #include <linux/vmalloc.h>
  25. #include <sound/memalloc.h>
  26. /* table entries are align to 32 */
  27. #define SGBUF_TBL_ALIGN 32
  28. #define sgbuf_align_table(tbl) ((((tbl) + SGBUF_TBL_ALIGN - 1) / SGBUF_TBL_ALIGN) * SGBUF_TBL_ALIGN)
  29. int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab)
  30. {
  31. struct snd_sg_buf *sgbuf = dmab->private_data;
  32. struct snd_dma_buffer tmpb;
  33. int i;
  34. if (! sgbuf)
  35. return -EINVAL;
  36. tmpb.dev.type = SNDRV_DMA_TYPE_DEV;
  37. tmpb.dev.dev = sgbuf->dev;
  38. for (i = 0; i < sgbuf->pages; i++) {
  39. tmpb.area = sgbuf->table[i].buf;
  40. tmpb.addr = sgbuf->table[i].addr;
  41. tmpb.bytes = PAGE_SIZE;
  42. snd_dma_free_pages(&tmpb);
  43. }
  44. if (dmab->area)
  45. vunmap(dmab->area);
  46. dmab->area = NULL;
  47. kfree(sgbuf->table);
  48. kfree(sgbuf->page_table);
  49. kfree(sgbuf);
  50. dmab->private_data = NULL;
  51. return 0;
  52. }
  53. void *snd_malloc_sgbuf_pages(struct device *device,
  54. size_t size, struct snd_dma_buffer *dmab,
  55. size_t *res_size)
  56. {
  57. struct snd_sg_buf *sgbuf;
  58. unsigned int i, pages;
  59. struct snd_dma_buffer tmpb;
  60. dmab->area = NULL;
  61. dmab->addr = 0;
  62. dmab->private_data = sgbuf = kmalloc(sizeof(*sgbuf), GFP_KERNEL);
  63. if (! sgbuf)
  64. return NULL;
  65. memset(sgbuf, 0, sizeof(*sgbuf));
  66. sgbuf->dev = device;
  67. pages = snd_sgbuf_aligned_pages(size);
  68. sgbuf->tblsize = sgbuf_align_table(pages);
  69. sgbuf->table = kmalloc(sizeof(*sgbuf->table) * sgbuf->tblsize, GFP_KERNEL);
  70. if (! sgbuf->table)
  71. goto _failed;
  72. memset(sgbuf->table, 0, sizeof(*sgbuf->table) * sgbuf->tblsize);
  73. sgbuf->page_table = kmalloc(sizeof(*sgbuf->page_table) * sgbuf->tblsize, GFP_KERNEL);
  74. if (! sgbuf->page_table)
  75. goto _failed;
  76. memset(sgbuf->page_table, 0, sizeof(*sgbuf->page_table) * sgbuf->tblsize);
  77. /* allocate each page */
  78. for (i = 0; i < pages; i++) {
  79. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, device, PAGE_SIZE, &tmpb) < 0) {
  80. if (res_size == NULL)
  81. goto _failed;
  82. *res_size = size = sgbuf->pages * PAGE_SIZE;
  83. break;
  84. }
  85. sgbuf->table[i].buf = tmpb.area;
  86. sgbuf->table[i].addr = tmpb.addr;
  87. sgbuf->page_table[i] = virt_to_page(tmpb.area);
  88. sgbuf->pages++;
  89. }
  90. sgbuf->size = size;
  91. dmab->area = vmap(sgbuf->page_table, sgbuf->pages, VM_MAP, PAGE_KERNEL);
  92. if (! dmab->area)
  93. goto _failed;
  94. return dmab->area;
  95. _failed:
  96. snd_free_sgbuf_pages(dmab); /* free the table */
  97. return NULL;
  98. }