ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / sgbuf.c
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
22 #include <linux/config.h>
23 #include <linux/slab.h>
24 #include <linux/mm.h>
25 #include <linux/vmalloc.h>
26 #include <sound/memalloc.h>
27
28
29 /* table entries are align to 32 */
30 #define SGBUF_TBL_ALIGN         32
31 #define sgbuf_align_table(tbl)  ((((tbl) + SGBUF_TBL_ALIGN - 1) / SGBUF_TBL_ALIGN) * SGBUF_TBL_ALIGN)
32
33 int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab)
34 {
35         struct snd_sg_buf *sgbuf = dmab->private_data;
36         struct snd_dma_buffer tmpb;
37         int i;
38
39         if (! sgbuf)
40                 return -EINVAL;
41
42         for (i = 0; i < sgbuf->pages; i++) {
43                 tmpb.area = sgbuf->table[i].buf;
44                 tmpb.addr = sgbuf->table[i].addr;
45                 tmpb.bytes = PAGE_SIZE;
46                 snd_dma_free_pages(&sgbuf->dev, &tmpb);
47         }
48         if (dmab->area)
49                 vunmap(dmab->area);
50         dmab->area = NULL;
51
52         if (sgbuf->table)
53                 kfree(sgbuf->table);
54         if (sgbuf->page_table)
55                 kfree(sgbuf->page_table);
56         kfree(sgbuf);
57         dmab->private_data = NULL;
58         
59         return 0;
60 }
61
62 void *snd_malloc_sgbuf_pages(const struct snd_dma_device *dev,
63                              size_t size, struct snd_dma_buffer *dmab,
64                              size_t *res_size)
65 {
66         struct snd_sg_buf *sgbuf;
67         unsigned int i, pages;
68         struct snd_dma_buffer tmpb;
69
70         dmab->area = NULL;
71         dmab->addr = 0;
72         dmab->private_data = sgbuf = kmalloc(sizeof(*sgbuf), GFP_KERNEL);
73         if (! sgbuf)
74                 return NULL;
75         memset(sgbuf, 0, sizeof(*sgbuf));
76         sgbuf->dev = *dev;
77         sgbuf->dev.type = SNDRV_DMA_TYPE_DEV;
78         pages = snd_sgbuf_aligned_pages(size);
79         sgbuf->tblsize = sgbuf_align_table(pages);
80         sgbuf->table = kmalloc(sizeof(*sgbuf->table) * sgbuf->tblsize, GFP_KERNEL);
81         if (! sgbuf->table)
82                 goto _failed;
83         memset(sgbuf->table, 0, sizeof(*sgbuf->table) * sgbuf->tblsize);
84         sgbuf->page_table = kmalloc(sizeof(*sgbuf->page_table) * sgbuf->tblsize, GFP_KERNEL);
85         if (! sgbuf->page_table)
86                 goto _failed;
87         memset(sgbuf->page_table, 0, sizeof(*sgbuf->page_table) * sgbuf->tblsize);
88
89         /* allocate each page */
90         for (i = 0; i < pages; i++) {
91                 if (snd_dma_alloc_pages(&sgbuf->dev, PAGE_SIZE, &tmpb) < 0) {
92                         if (res_size == NULL)
93                                 goto _failed;
94                         *res_size = size = sgbuf->pages * PAGE_SIZE;
95                         break;
96                 }
97                 sgbuf->table[i].buf = tmpb.area;
98                 sgbuf->table[i].addr = tmpb.addr;
99                 sgbuf->page_table[i] = virt_to_page(tmpb.area);
100                 sgbuf->pages++;
101         }
102
103         sgbuf->size = size;
104         dmab->area = vmap(sgbuf->page_table, sgbuf->pages, VM_MAP, PAGE_KERNEL);
105         if (! dmab->area)
106                 goto _failed;
107         return dmab->area;
108
109  _failed:
110         snd_free_sgbuf_pages(dmab); /* free the table */
111         return NULL;
112 }