Linux-2.6.12-rc2
[linux-2.6/next.git] / sound / isa / gus / gus_mem.c
blobbfc2b91001d5d8db9f18d3317bf65a9560adaeb2
1 /*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * GUS's memory allocation routines / bottom layer
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.
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.
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
22 #include <sound/driver.h>
23 #include <linux/slab.h>
24 #include <sound/core.h>
25 #include <sound/gus.h>
26 #include <sound/info.h>
28 #ifdef CONFIG_SND_DEBUG
29 static void snd_gf1_mem_info_read(snd_info_entry_t *entry,
30 snd_info_buffer_t * buffer);
31 #endif
33 void snd_gf1_mem_lock(snd_gf1_mem_t * alloc, int xup)
35 if (!xup) {
36 down(&alloc->memory_mutex);
37 } else {
38 up(&alloc->memory_mutex);
42 snd_gf1_mem_block_t *snd_gf1_mem_xalloc(snd_gf1_mem_t * alloc,
43 snd_gf1_mem_block_t * block)
45 snd_gf1_mem_block_t *pblock, *nblock;
47 nblock = (snd_gf1_mem_block_t *) kmalloc(sizeof(snd_gf1_mem_block_t), GFP_KERNEL);
48 if (nblock == NULL)
49 return NULL;
50 *nblock = *block;
51 pblock = alloc->first;
52 while (pblock) {
53 if (pblock->ptr > nblock->ptr) {
54 nblock->prev = pblock->prev;
55 nblock->next = pblock;
56 pblock->prev = nblock;
57 if (pblock == alloc->first)
58 alloc->first = nblock;
59 else
60 nblock->prev->next = nblock;
61 up(&alloc->memory_mutex);
62 return NULL;
64 pblock = pblock->next;
66 nblock->next = NULL;
67 if (alloc->last == NULL) {
68 nblock->prev = NULL;
69 alloc->first = alloc->last = nblock;
70 } else {
71 nblock->prev = alloc->last;
72 alloc->last->next = nblock;
73 alloc->last = nblock;
75 return nblock;
78 int snd_gf1_mem_xfree(snd_gf1_mem_t * alloc, snd_gf1_mem_block_t * block)
80 if (block->share) { /* ok.. shared block */
81 block->share--;
82 up(&alloc->memory_mutex);
83 return 0;
85 if (alloc->first == block) {
86 alloc->first = block->next;
87 if (block->next)
88 block->next->prev = NULL;
89 } else {
90 block->prev->next = block->next;
91 if (block->next)
92 block->next->prev = block->prev;
94 if (alloc->last == block) {
95 alloc->last = block->prev;
96 if (block->prev)
97 block->prev->next = NULL;
98 } else {
99 block->next->prev = block->prev;
100 if (block->prev)
101 block->prev->next = block->next;
103 kfree(block->name);
104 kfree(block);
105 return 0;
108 snd_gf1_mem_block_t *snd_gf1_mem_look(snd_gf1_mem_t * alloc,
109 unsigned int address)
111 snd_gf1_mem_block_t *block;
113 for (block = alloc->first; block; block = block->next) {
114 if (block->ptr == address) {
115 return block;
118 return NULL;
121 snd_gf1_mem_block_t *snd_gf1_mem_share(snd_gf1_mem_t * alloc,
122 unsigned int *share_id)
124 snd_gf1_mem_block_t *block;
126 if (!share_id[0] && !share_id[1] &&
127 !share_id[2] && !share_id[3])
128 return NULL;
129 for (block = alloc->first; block; block = block->next)
130 if (!memcmp(share_id, block->share_id, sizeof(share_id)))
131 return block;
132 return NULL;
135 static int snd_gf1_mem_find(snd_gf1_mem_t * alloc,
136 snd_gf1_mem_block_t * block,
137 unsigned int size, int w_16, int align)
139 snd_gf1_bank_info_t *info = w_16 ? alloc->banks_16 : alloc->banks_8;
140 unsigned int idx, boundary;
141 int size1;
142 snd_gf1_mem_block_t *pblock;
143 unsigned int ptr1, ptr2;
145 align--;
146 if (w_16 && align < 1)
147 align = 1;
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;
167 ptr1 = (pblock->ptr + pblock->size + align) & ~align;
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;
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;
185 return -ENOMEM;
188 snd_gf1_mem_block_t *snd_gf1_mem_alloc(snd_gf1_mem_t * alloc, int owner,
189 char *name, int size, int w_16, int align,
190 unsigned int *share_id)
192 snd_gf1_mem_block_t block, *nblock;
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 */
200 snd_printk("snd_gf1_mem_alloc - share: sizes differ\n");
201 goto __std;
203 nblock->share++;
204 snd_gf1_mem_lock(alloc, 1);
205 return NULL;
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;
213 if (share_id != NULL)
214 memcpy(&block.share_id, share_id, sizeof(block.share_id));
215 block.owner = owner;
216 block.name = snd_kmalloc_strdup(name, GFP_KERNEL);
217 nblock = snd_gf1_mem_xalloc(alloc, &block);
218 snd_gf1_mem_lock(alloc, 1);
219 return nblock;
222 int snd_gf1_mem_free(snd_gf1_mem_t * alloc, unsigned int address)
224 int result;
225 snd_gf1_mem_block_t *block;
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;
233 snd_gf1_mem_lock(alloc, 1);
234 return -EINVAL;
237 int snd_gf1_mem_init(snd_gus_card_t * gus)
239 snd_gf1_mem_t *alloc;
240 snd_gf1_mem_block_t block;
241 #ifdef CONFIG_SND_DEBUG
242 snd_info_entry_t *entry;
243 #endif
245 alloc = &gus->gf1.mem_alloc;
246 init_MUTEX(&alloc->memory_mutex);
247 alloc->first = alloc->last = NULL;
248 if (!gus->gf1.memory)
249 return 0;
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;
256 block.name = snd_kmalloc_strdup("InterWave LFOs", GFP_KERNEL);
257 if (snd_gf1_mem_xalloc(alloc, &block) == NULL)
258 return -ENOMEM;
260 block.ptr = gus->gf1.default_voice_address;
261 block.size = 4;
262 block.name = snd_kmalloc_strdup("Voice default (NULL's)", GFP_KERNEL);
263 if (snd_gf1_mem_xalloc(alloc, &block) == NULL)
264 return -ENOMEM;
265 #ifdef CONFIG_SND_DEBUG
266 if (! snd_card_proc_new(gus->card, "gusmem", &entry)) {
267 snd_info_set_text_ops(entry, gus, 1024, snd_gf1_mem_info_read);
268 entry->c.text.read_size = 256 * 1024;
270 #endif
271 return 0;
274 int snd_gf1_mem_done(snd_gus_card_t * gus)
276 snd_gf1_mem_t *alloc;
277 snd_gf1_mem_block_t *block, *nblock;
279 alloc = &gus->gf1.mem_alloc;
280 block = alloc->first;
281 while (block) {
282 nblock = block->next;
283 snd_gf1_mem_xfree(alloc, block);
284 block = nblock;
286 return 0;
289 #ifdef CONFIG_SND_DEBUG
290 static void snd_gf1_mem_info_read(snd_info_entry_t *entry,
291 snd_info_buffer_t * buffer)
293 snd_gus_card_t *gus;
294 snd_gf1_mem_t *alloc;
295 snd_gf1_mem_block_t *block;
296 unsigned int total, used;
297 int i;
299 gus = entry->private_data;
300 alloc = &gus->gf1.mem_alloc;
301 down(&alloc->memory_mutex);
302 snd_iprintf(buffer, "8-bit banks : \n ");
303 for (i = 0; i < 4; i++)
304 snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_8[i].address, alloc->banks_8[i].size >> 10, i + 1 < 4 ? "," : "");
305 snd_iprintf(buffer, "\n"
306 "16-bit banks : \n ");
307 for (i = total = 0; i < 4; i++) {
308 snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_16[i].address, alloc->banks_16[i].size >> 10, i + 1 < 4 ? "," : "");
309 total += alloc->banks_16[i].size;
311 snd_iprintf(buffer, "\n");
312 used = 0;
313 for (block = alloc->first, i = 0; block; block = block->next, i++) {
314 used += block->size;
315 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);
316 if (block->share ||
317 block->share_id[0] || block->share_id[1] ||
318 block->share_id[2] || block->share_id[3])
319 snd_iprintf(buffer, " Share : %i [id0 0x%x] [id1 0x%x] [id2 0x%x] [id3 0x%x]\n",
320 block->share,
321 block->share_id[0], block->share_id[1],
322 block->share_id[2], block->share_id[3]);
323 snd_iprintf(buffer, " Flags :%s\n",
324 block->flags & SNDRV_GF1_MEM_BLOCK_16BIT ? " 16-bit" : "");
325 snd_iprintf(buffer, " Owner : ");
326 switch (block->owner) {
327 case SNDRV_GF1_MEM_OWNER_DRIVER:
328 snd_iprintf(buffer, "driver - %s\n", block->name);
329 break;
330 case SNDRV_GF1_MEM_OWNER_WAVE_SIMPLE:
331 snd_iprintf(buffer, "SIMPLE wave\n");
332 break;
333 case SNDRV_GF1_MEM_OWNER_WAVE_GF1:
334 snd_iprintf(buffer, "GF1 wave\n");
335 break;
336 case SNDRV_GF1_MEM_OWNER_WAVE_IWFFFF:
337 snd_iprintf(buffer, "IWFFFF wave\n");
338 break;
339 default:
340 snd_iprintf(buffer, "unknown\n");
343 snd_iprintf(buffer, " Total: memory = %i, used = %i, free = %i\n",
344 total, used, total - used);
345 up(&alloc->memory_mutex);
346 #if 0
347 ultra_iprintf(buffer, " Verify: free = %i, max 8-bit block = %i, max 16-bit block = %i\n",
348 ultra_memory_free_size(card, &card->gf1.mem_alloc),
349 ultra_memory_free_block(card, &card->gf1.mem_alloc, 0),
350 ultra_memory_free_block(card, &card->gf1.mem_alloc, 1));
351 #endif
353 #endif