]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/isa/gus/gus_mem.c
[ALSA] Changed Jaroslav Kysela's e-mail from perex@suse.cz to perex@perex.cz
[net-next-2.6.git] / sound / isa / gus / gus_mem.c
CommitLineData
1da177e4 1/*
c1017a4c 2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
3 * GUS's memory allocation routines / bottom layer
4 *
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 <sound/driver.h>
23#include <linux/slab.h>
543537bd 24#include <linux/string.h>
1da177e4
LT
25#include <sound/core.h>
26#include <sound/gus.h>
27#include <sound/info.h>
28
29#ifdef CONFIG_SND_DEBUG
5e2da206
TI
30static void snd_gf1_mem_info_read(struct snd_info_entry *entry,
31 struct snd_info_buffer *buffer);
1da177e4
LT
32#endif
33
5e2da206 34void snd_gf1_mem_lock(struct snd_gf1_mem * alloc, int xup)
1da177e4
LT
35{
36 if (!xup) {
8b7547f9 37 mutex_lock(&alloc->memory_mutex);
1da177e4 38 } else {
8b7547f9 39 mutex_unlock(&alloc->memory_mutex);
1da177e4
LT
40 }
41}
42
5e2da206
TI
43static struct snd_gf1_mem_block *snd_gf1_mem_xalloc(struct snd_gf1_mem * alloc,
44 struct snd_gf1_mem_block * block)
1da177e4 45{
5e2da206 46 struct snd_gf1_mem_block *pblock, *nblock;
1da177e4 47
5e2da206 48 nblock = kmalloc(sizeof(struct snd_gf1_mem_block), GFP_KERNEL);
1da177e4
LT
49 if (nblock == NULL)
50 return NULL;
51 *nblock = *block;
52 pblock = alloc->first;
53 while (pblock) {
54 if (pblock->ptr > nblock->ptr) {
55 nblock->prev = pblock->prev;
56 nblock->next = pblock;
57 pblock->prev = nblock;
58 if (pblock == alloc->first)
59 alloc->first = nblock;
60 else
61 nblock->prev->next = nblock;
8b7547f9 62 mutex_unlock(&alloc->memory_mutex);
1da177e4
LT
63 return NULL;
64 }
65 pblock = pblock->next;
66 }
67 nblock->next = NULL;
68 if (alloc->last == NULL) {
69 nblock->prev = NULL;
70 alloc->first = alloc->last = nblock;
71 } else {
72 nblock->prev = alloc->last;
73 alloc->last->next = nblock;
74 alloc->last = nblock;
75 }
76 return nblock;
77}
78
5e2da206 79int snd_gf1_mem_xfree(struct snd_gf1_mem * alloc, struct snd_gf1_mem_block * block)
1da177e4
LT
80{
81 if (block->share) { /* ok.. shared block */
82 block->share--;
8b7547f9 83 mutex_unlock(&alloc->memory_mutex);
1da177e4
LT
84 return 0;
85 }
86 if (alloc->first == block) {
87 alloc->first = block->next;
88 if (block->next)
89 block->next->prev = NULL;
90 } else {
91 block->prev->next = block->next;
92 if (block->next)
93 block->next->prev = block->prev;
94 }
95 if (alloc->last == block) {
96 alloc->last = block->prev;
97 if (block->prev)
98 block->prev->next = NULL;
99 } else {
100 block->next->prev = block->prev;
101 if (block->prev)
102 block->prev->next = block->next;
103 }
104 kfree(block->name);
105 kfree(block);
106 return 0;
107}
108
5e2da206 109static struct snd_gf1_mem_block *snd_gf1_mem_look(struct snd_gf1_mem * alloc,
209ac85d 110 unsigned int address)
1da177e4 111{
5e2da206 112 struct snd_gf1_mem_block *block;
1da177e4
LT
113
114 for (block = alloc->first; block; block = block->next) {
115 if (block->ptr == address) {
116 return block;
117 }
118 }
119 return NULL;
120}
121
5e2da206 122static struct snd_gf1_mem_block *snd_gf1_mem_share(struct snd_gf1_mem * alloc,
209ac85d 123 unsigned int *share_id)
1da177e4 124{
5e2da206 125 struct snd_gf1_mem_block *block;
1da177e4
LT
126
127 if (!share_id[0] && !share_id[1] &&
128 !share_id[2] && !share_id[3])
129 return NULL;
130 for (block = alloc->first; block; block = block->next)
131 if (!memcmp(share_id, block->share_id, sizeof(share_id)))
132 return block;
133 return NULL;
134}
135
5e2da206
TI
136static int snd_gf1_mem_find(struct snd_gf1_mem * alloc,
137 struct snd_gf1_mem_block * block,
1da177e4
LT
138 unsigned int size, int w_16, int align)
139{
5e2da206 140 struct snd_gf1_bank_info *info = w_16 ? alloc->banks_16 : alloc->banks_8;
1da177e4
LT
141 unsigned int idx, boundary;
142 int size1;
5e2da206 143 struct snd_gf1_mem_block *pblock;
1da177e4
LT
144 unsigned int ptr1, ptr2;
145
7ab39926
CL
146 if (w_16 && align < 2)
147 align = 2;
1da177e4
LT
148 block->flags = w_16 ? SNDRV_GF1_MEM_BLOCK_16BIT : 0;
149 block->owner = SNDRV_GF1_MEM_OWNER_DRIVER;
150 block->share = 0;
151 block->share_id[0] = block->share_id[1] =
152 block->share_id[2] = block->share_id[3] = 0;
153 block->name = NULL;
154 block->prev = block->next = NULL;
155 for (pblock = alloc->first, idx = 0; pblock; pblock = pblock->next) {
156 while (pblock->ptr >= (boundary = info[idx].address + info[idx].size))
157 idx++;
158 while (pblock->ptr + pblock->size >= (boundary = info[idx].address + info[idx].size))
159 idx++;
160 ptr2 = boundary;
161 if (pblock->next) {
162 if (pblock->ptr + pblock->size == pblock->next->ptr)
163 continue;
164 if (pblock->next->ptr < boundary)
165 ptr2 = pblock->next->ptr;
166 }
7ab39926 167 ptr1 = ALIGN(pblock->ptr + pblock->size, align);
1da177e4
LT
168 if (ptr1 >= ptr2)
169 continue;
170 size1 = ptr2 - ptr1;
171 if ((int)size <= size1) {
172 block->ptr = ptr1;
173 block->size = size;
174 return 0;
175 }
176 }
177 while (++idx < 4) {
178 if (size <= info[idx].size) {
179 /* I assume that bank address is already aligned.. */
180 block->ptr = info[idx].address;
181 block->size = size;
182 return 0;
183 }
184 }
185 return -ENOMEM;
186}
187
5e2da206 188struct snd_gf1_mem_block *snd_gf1_mem_alloc(struct snd_gf1_mem * alloc, int owner,
1da177e4
LT
189 char *name, int size, int w_16, int align,
190 unsigned int *share_id)
191{
5e2da206 192 struct snd_gf1_mem_block block, *nblock;
1da177e4
LT
193
194 snd_gf1_mem_lock(alloc, 0);
195 if (share_id != NULL) {
196 nblock = snd_gf1_mem_share(alloc, share_id);
197 if (nblock != NULL) {
198 if (size != (int)nblock->size) {
199 /* TODO: remove in the future */
99b359ba 200 snd_printk(KERN_ERR "snd_gf1_mem_alloc - share: sizes differ\n");
1da177e4
LT
201 goto __std;
202 }
203 nblock->share++;
204 snd_gf1_mem_lock(alloc, 1);
205 return NULL;
206 }
207 }
208 __std:
209 if (snd_gf1_mem_find(alloc, &block, size, w_16, align) < 0) {
210 snd_gf1_mem_lock(alloc, 1);
211 return NULL;
212 }
213 if (share_id != NULL)
214 memcpy(&block.share_id, share_id, sizeof(block.share_id));
215 block.owner = owner;
543537bd 216 block.name = kstrdup(name, GFP_KERNEL);
1da177e4
LT
217 nblock = snd_gf1_mem_xalloc(alloc, &block);
218 snd_gf1_mem_lock(alloc, 1);
219 return nblock;
220}
221
5e2da206 222int snd_gf1_mem_free(struct snd_gf1_mem * alloc, unsigned int address)
1da177e4
LT
223{
224 int result;
5e2da206 225 struct snd_gf1_mem_block *block;
1da177e4
LT
226
227 snd_gf1_mem_lock(alloc, 0);
228 if ((block = snd_gf1_mem_look(alloc, address)) != NULL) {
229 result = snd_gf1_mem_xfree(alloc, block);
230 snd_gf1_mem_lock(alloc, 1);
231 return result;
232 }
233 snd_gf1_mem_lock(alloc, 1);
234 return -EINVAL;
235}
236
5e2da206 237int snd_gf1_mem_init(struct snd_gus_card * gus)
1da177e4 238{
5e2da206
TI
239 struct snd_gf1_mem *alloc;
240 struct snd_gf1_mem_block block;
1da177e4 241#ifdef CONFIG_SND_DEBUG
5e2da206 242 struct snd_info_entry *entry;
1da177e4
LT
243#endif
244
245 alloc = &gus->gf1.mem_alloc;
8b7547f9 246 mutex_init(&alloc->memory_mutex);
1da177e4
LT
247 alloc->first = alloc->last = NULL;
248 if (!gus->gf1.memory)
249 return 0;
250
251 memset(&block, 0, sizeof(block));
252 block.owner = SNDRV_GF1_MEM_OWNER_DRIVER;
253 if (gus->gf1.enh_mode) {
254 block.ptr = 0;
255 block.size = 1024;
543537bd 256 block.name = kstrdup("InterWave LFOs", GFP_KERNEL);
1da177e4
LT
257 if (snd_gf1_mem_xalloc(alloc, &block) == NULL)
258 return -ENOMEM;
259 }
260 block.ptr = gus->gf1.default_voice_address;
261 block.size = 4;
543537bd 262 block.name = kstrdup("Voice default (NULL's)", GFP_KERNEL);
1da177e4
LT
263 if (snd_gf1_mem_xalloc(alloc, &block) == NULL)
264 return -ENOMEM;
265#ifdef CONFIG_SND_DEBUG
bf850204
TI
266 if (! snd_card_proc_new(gus->card, "gusmem", &entry))
267 snd_info_set_text_ops(entry, gus, snd_gf1_mem_info_read);
1da177e4
LT
268#endif
269 return 0;
270}
271
5e2da206 272int snd_gf1_mem_done(struct snd_gus_card * gus)
1da177e4 273{
5e2da206
TI
274 struct snd_gf1_mem *alloc;
275 struct snd_gf1_mem_block *block, *nblock;
1da177e4
LT
276
277 alloc = &gus->gf1.mem_alloc;
278 block = alloc->first;
279 while (block) {
280 nblock = block->next;
281 snd_gf1_mem_xfree(alloc, block);
282 block = nblock;
283 }
284 return 0;
285}
286
287#ifdef CONFIG_SND_DEBUG
5e2da206
TI
288static void snd_gf1_mem_info_read(struct snd_info_entry *entry,
289 struct snd_info_buffer *buffer)
1da177e4 290{
5e2da206
TI
291 struct snd_gus_card *gus;
292 struct snd_gf1_mem *alloc;
293 struct snd_gf1_mem_block *block;
1da177e4
LT
294 unsigned int total, used;
295 int i;
296
297 gus = entry->private_data;
298 alloc = &gus->gf1.mem_alloc;
8b7547f9 299 mutex_lock(&alloc->memory_mutex);
1da177e4
LT
300 snd_iprintf(buffer, "8-bit banks : \n ");
301 for (i = 0; i < 4; i++)
302 snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_8[i].address, alloc->banks_8[i].size >> 10, i + 1 < 4 ? "," : "");
303 snd_iprintf(buffer, "\n"
304 "16-bit banks : \n ");
305 for (i = total = 0; i < 4; i++) {
306 snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_16[i].address, alloc->banks_16[i].size >> 10, i + 1 < 4 ? "," : "");
307 total += alloc->banks_16[i].size;
308 }
309 snd_iprintf(buffer, "\n");
310 used = 0;
311 for (block = alloc->first, i = 0; block; block = block->next, i++) {
312 used += block->size;
313 snd_iprintf(buffer, "Block %i at 0x%lx onboard 0x%x size %i (0x%x):\n", i, (long) block, block->ptr, block->size, block->size);
314 if (block->share ||
315 block->share_id[0] || block->share_id[1] ||
316 block->share_id[2] || block->share_id[3])
317 snd_iprintf(buffer, " Share : %i [id0 0x%x] [id1 0x%x] [id2 0x%x] [id3 0x%x]\n",
318 block->share,
319 block->share_id[0], block->share_id[1],
320 block->share_id[2], block->share_id[3]);
321 snd_iprintf(buffer, " Flags :%s\n",
322 block->flags & SNDRV_GF1_MEM_BLOCK_16BIT ? " 16-bit" : "");
323 snd_iprintf(buffer, " Owner : ");
324 switch (block->owner) {
325 case SNDRV_GF1_MEM_OWNER_DRIVER:
326 snd_iprintf(buffer, "driver - %s\n", block->name);
327 break;
328 case SNDRV_GF1_MEM_OWNER_WAVE_SIMPLE:
329 snd_iprintf(buffer, "SIMPLE wave\n");
330 break;
331 case SNDRV_GF1_MEM_OWNER_WAVE_GF1:
332 snd_iprintf(buffer, "GF1 wave\n");
333 break;
334 case SNDRV_GF1_MEM_OWNER_WAVE_IWFFFF:
335 snd_iprintf(buffer, "IWFFFF wave\n");
336 break;
337 default:
338 snd_iprintf(buffer, "unknown\n");
339 }
340 }
341 snd_iprintf(buffer, " Total: memory = %i, used = %i, free = %i\n",
342 total, used, total - used);
8b7547f9 343 mutex_unlock(&alloc->memory_mutex);
1da177e4
LT
344#if 0
345 ultra_iprintf(buffer, " Verify: free = %i, max 8-bit block = %i, max 16-bit block = %i\n",
346 ultra_memory_free_size(card, &card->gf1.mem_alloc),
347 ultra_memory_free_block(card, &card->gf1.mem_alloc, 0),
348 ultra_memory_free_block(card, &card->gf1.mem_alloc, 1));
349#endif
350}
351#endif