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