xref: /haiku/src/add-ons/kernel/drivers/audio/emuxki/emuxki.c (revision a3e794ae459fec76826407f8ba8c94cd3535f128)
1 /*
2  * Emuxki BeOS Driver for Creative Labs SBLive!/Audigy series
3  *
4  * Copyright (c) 2002, Jerome Duval (jerome.duval@free.fr)
5  *
6  * Authors:
7  *		Alexander Coers		Alexander.Coers@gmx.de
8  *		Fredrik Modéen 		fredrik@modeen.se
9  *
10 */
11 /* This code is derived from the NetBSD driver for Creative Labs SBLive! series
12  *
13  * Copyright (c) 2001 The NetBSD Foundation, Inc.
14  * All rights reserved.
15  *
16  * This code is derived from software contributed to The NetBSD Foundation
17  * by Yannick Montulet.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  * 3. All advertising materials mentioning features or use of this software
28  *    must display the following acknowledgement:
29  *	This product includes software developed by the NetBSD
30  *	Foundation, Inc. and its contributors.
31  * 4. Neither the name of The NetBSD Foundation nor the names of its
32  *    contributors may be used to endorse or promote products derived
33  *    from this software without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
36  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
37  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45  * POSSIBILITY OF SUCH DAMAGE.
46  */
47 
48 #include <ByteOrder.h>
49 #include <KernelExport.h>
50 #include <PCI.h>
51 #include <driver_settings.h>
52 #include <fcntl.h>
53 #include <math.h>
54 #include <midi_driver.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include "emuxki.h"
60 #include "debug.h"
61 #include "config.h"
62 #include "util.h"
63 #include "io.h"
64 #include "multi.h"
65 #include "ac97.h"
66 
67 status_t init_hardware(void);
68 status_t init_driver(void);
69 void uninit_driver(void);
70 const char ** publish_devices(void);
71 device_hooks * find_device(const char *);
72 
73 pci_module_info	*pci;
74 generic_mpu401_module * mpu401;
75 //static char gameport_name[] = "generic/gameport/v2";
76 //generic_gameport_module * gameport;
77 
78 int32 num_cards;
79 emuxki_dev cards[NUM_CARDS];
80 int32 num_names;
81 char * names[NUM_CARDS*20+1];
82 
83 emuxki_settings current_settings = {
84 		2,      // channels
85 		16,     // bits per sample
86 		48000,  // sample rate
87 		512,    // buffer frames
88 		2       // buffer count
89 };
90 
91 status_t emuxki_init(emuxki_dev *card);
92 void emuxki_shutdown(emuxki_dev *card);
93 
94 extern device_hooks multi_hooks;
95 extern device_hooks midi_hooks;
96 //extern device_hooks joy_hooks;
97 
98 /* Hardware Dump */
99 
100 static void
101 dump_hardware_regs(device_config *config)
102 {
103 	LOG(("EMU_IPR = %#08x\n",emuxki_reg_read_32(config, EMU_IPR)));
104 	LOG(("EMU_INTE = %#08x\n",emuxki_reg_read_32(config, EMU_INTE)));
105 	LOG(("EMU_HCFG = %#08x\n",emuxki_reg_read_32(config, EMU_HCFG)));
106 	snooze(1000);
107 	/*emuxki_reg_write_8(config, EMU_AC97ADDRESS, EMU_AC97ADDRESS_READY);
108 	LOG(("EMU_AC97ADDRESS_READY = %#08x\n", emuxki_reg_read_16(config, EMU_AC97DATA)));*/
109 
110 	/*emuxki_reg_write_8(config, EMU_AC97ADDRESS, EMU_AC97ADDRESS_ADDRESS);
111 	LOG(("EMU_AC97ADDRESS_ADDRESS = %#08x\n", emuxki_reg_read_16(config, EMU_AC97DATA)));*/
112 
113 	/*LOG(("EMU_CHAN_CPF_STEREO = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_CPF_STEREO)));
114 	LOG(("EMU_CHAN_FXRT = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_FXRT)));
115 	LOG(("EMU_CHAN_PTRX = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_PTRX)));
116 	LOG(("EMU_CHAN_DSL = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_DSL)));
117 	LOG(("EMU_CHAN_PSST = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_PSST)));
118 	LOG(("EMU_CHAN_CCCA = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_CCCA)));
119 	LOG(("EMU_CHAN_Z1 = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_Z1)));
120 	LOG(("EMU_CHAN_Z2 = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_Z2)));
121 	LOG(("EMU_CHAN_MAPA = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_MAPA)));
122 	LOG(("EMU_CHAN_MAPB = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_MAPB)));
123 	LOG(("EMU_CHAN_CVCF_CURRENTFILTER = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_CVCF_CURRENTFILTER)));
124 	LOG(("EMU_CHAN_VTFT_FILTERTARGET = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_VTFT_FILTERTARGET)));
125 	LOG(("EMU_CHAN_ATKHLDM = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_ATKHLDM)));
126 	LOG(("EMU_CHAN_DCYSUSM = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_DCYSUSM)));
127 	LOG(("EMU_CHAN_LFOVAL1 = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_LFOVAL1)));
128 	LOG(("EMU_CHAN_LFOVAL2 = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_LFOVAL2)));
129 	LOG(("EMU_CHAN_FMMOD = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_FMMOD)));
130 	LOG(("EMU_CHAN_TREMFRQ = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_TREMFRQ)));
131 	LOG(("EMU_CHAN_FM2FRQ2 = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_FM2FRQ2)));
132 	LOG(("EMU_CHAN_ENVVAL = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_ENVVAL)));
133 	LOG(("EMU_CHAN_ATKHLDV = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_ATKHLDV)));
134 	LOG(("EMU_CHAN_ENVVOL = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_ENVVOL)));
135 	LOG(("EMU_CHAN_PEFE = %#08x\n",emuxki_chan_read(config, 0, EMU_CHAN_PEFE)));*/
136 
137 }
138 
139 
140 /*static void
141 trace_hardware_regs(device_config *config)
142 {
143 	TRACE(("EMU_IPR = %#08x\n",emuxki_reg_read_32(config, EMU_IPR)));
144 	TRACE(("EMU_INTE = %#08x\n",emuxki_reg_read_32(config, EMU_INTE)));
145 	TRACE(("EMU_HCFG = %#08x\n",emuxki_reg_read_32(config, EMU_HCFG)));
146 }*/
147 
148 /* Misc stuff relative to Emuxki */
149 
150 int             emu10k1_recbuf_sizes[] = {
151 	0, 384, 448, 512, 640, 768, 896, 1024, 1280, 1536, 1792,
152 	2048, 2560, 3072, 3584, 4096, 5120, 6144, 7168, 8192, 10240, 12288,
153 	14366, 16384, 20480, 24576, 28672, 32768, 40960, 49152, 57344, 65536
154 };
155 
156 
157 static uint32
158 emuxki_rate_to_pitch(uint32 rate)
159 {
160 	static uint32 logMagTable[128] = {
161 		0x00000, 0x02dfc, 0x05b9e, 0x088e6, 0x0b5d6, 0x0e26f, 0x10eb3,
162 		0x13aa2, 0x1663f, 0x1918a, 0x1bc84, 0x1e72e, 0x2118b, 0x23b9a,
163 		0x2655d, 0x28ed5, 0x2b803, 0x2e0e8, 0x30985, 0x331db, 0x359eb,
164 		0x381b6, 0x3a93d, 0x3d081, 0x3f782, 0x41e42, 0x444c1, 0x46b01,
165 		0x49101, 0x4b6c4, 0x4dc49, 0x50191, 0x5269e, 0x54b6f, 0x57006,
166 		0x59463, 0x5b888, 0x5dc74, 0x60029, 0x623a7, 0x646ee, 0x66a00,
167 		0x68cdd, 0x6af86, 0x6d1fa, 0x6f43c, 0x7164b, 0x73829, 0x759d4,
168 		0x77b4f, 0x79c9a, 0x7bdb5, 0x7dea1, 0x7ff5e, 0x81fed, 0x8404e,
169 		0x86082, 0x88089, 0x8a064, 0x8c014, 0x8df98, 0x8fef1, 0x91e20,
170 		0x93d26, 0x95c01, 0x97ab4, 0x9993e, 0x9b79f, 0x9d5d9, 0x9f3ec,
171 		0xa11d8, 0xa2f9d, 0xa4d3c, 0xa6ab5, 0xa8808, 0xaa537, 0xac241,
172 		0xadf26, 0xafbe7, 0xb1885, 0xb3500, 0xb5157, 0xb6d8c, 0xb899f,
173 		0xba58f, 0xbc15e, 0xbdd0c, 0xbf899, 0xc1404, 0xc2f50, 0xc4a7b,
174 		0xc6587, 0xc8073, 0xc9b3f, 0xcb5ed, 0xcd07c, 0xceaec, 0xd053f,
175 		0xd1f73, 0xd398a, 0xd5384, 0xd6d60, 0xd8720, 0xda0c3, 0xdba4a,
176 		0xdd3b4, 0xded03, 0xe0636, 0xe1f4e, 0xe384a, 0xe512c, 0xe69f3,
177 		0xe829f, 0xe9b31, 0xeb3a9, 0xecc08, 0xee44c, 0xefc78, 0xf148a,
178 		0xf2c83, 0xf4463, 0xf5c2a, 0xf73da, 0xf8b71, 0xfa2f0, 0xfba57,
179 		0xfd1a7, 0xfe8df
180 	};
181 	static uint8 logSlopeTable[128] = {
182 		0x5c, 0x5c, 0x5b, 0x5a, 0x5a, 0x59, 0x58, 0x58,
183 		0x57, 0x56, 0x56, 0x55, 0x55, 0x54, 0x53, 0x53,
184 		0x52, 0x52, 0x51, 0x51, 0x50, 0x50, 0x4f, 0x4f,
185 		0x4e, 0x4d, 0x4d, 0x4d, 0x4c, 0x4c, 0x4b, 0x4b,
186 		0x4a, 0x4a, 0x49, 0x49, 0x48, 0x48, 0x47, 0x47,
187 		0x47, 0x46, 0x46, 0x45, 0x45, 0x45, 0x44, 0x44,
188 		0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x41, 0x41,
189 		0x41, 0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e,
190 		0x3e, 0x3e, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c,
191 		0x3b, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39,
192 		0x39, 0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37,
193 		0x37, 0x37, 0x37, 0x36, 0x36, 0x36, 0x36, 0x35,
194 		0x35, 0x35, 0x35, 0x34, 0x34, 0x34, 0x34, 0x34,
195 		0x33, 0x33, 0x33, 0x33, 0x32, 0x32, 0x32, 0x32,
196 		0x32, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30, 0x30,
197 		0x30, 0x30, 0x30, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f
198 	};
199 	int8          i;
200 
201 	if (rate == 0)
202 		return 0;	/* Bail out if no leading "1" */
203 	rate *= 11185;		/* Scale 48000 to 0x20002380 */
204 	for (i = 31; i > 0; i--) {
205 		if (rate & 0x80000000) {	/* Detect leading "1" */
206 			return (((uint32) (i - 15) << 20) +
207 				logMagTable[0x7f & (rate >> 24)] +
208 				(0x7f & (rate >> 17)) *
209 				logSlopeTable[0x7f & (rate >> 24)]);
210 		}
211 		rate <<= 1;
212 	}
213 	return 0;		/* Should never reach this point */
214 }
215 
216 /* Emuxki Memory management */
217 
218 static emuxki_mem *
219 emuxki_mem_new(emuxki_dev *card, int ptbidx, size_t size)
220 {
221 	emuxki_mem *mem;
222 
223 	if ((mem = malloc(sizeof(*mem))) == NULL)
224 		return (NULL);
225 
226 	mem->ptbidx = ptbidx;
227 	mem->area = alloc_mem(&mem->phy_base, &mem->log_base, size, "emuxki buffer");
228 	mem->size = size;
229 	if (mem->area < B_OK) {
230 		free(mem);
231 		return NULL;
232 	}
233 	return mem;
234 }
235 
236 
237 static void
238 emuxki_mem_delete(emuxki_mem *mem)
239 {
240 	if (mem->area > B_OK)
241 		delete_area(mem->area);
242 	free(mem);
243 }
244 
245 
246 void *
247 emuxki_pmem_alloc(emuxki_dev *card, size_t size)
248 {
249 	int             i;//, s;
250 	size_t          numblocks;
251 	emuxki_mem *mem;
252 	uint32      j, *ptb, silentpage;
253 
254 	ptb = card->ptb_log_base;
255 	silentpage = ((uint32)card->silentpage_phy_base) << 1;
256 	numblocks = size / EMU_PTESIZE;
257 	if (size % EMU_PTESIZE)
258 		numblocks++;
259 
260 	PRINT(("emuxki_pmem_alloc : numblocks : %ld\n", numblocks));
261 
262 	for (i = 0; i < EMU_MAXPTE; i++) {
263 		PRINT(("emuxki_pmem_alloc : %d\n", i));
264 		if ((B_LENDIAN_TO_HOST_INT32(ptb[i]) & EMU_CHAN_MAP_PTE_MASK) == silentpage) {
265 			/* We look for a free PTE */
266 			//s = splaudio();
267 			for (j = 0; j < numblocks; j++)
268 				if ((B_LENDIAN_TO_HOST_INT32(ptb[i + j]) & EMU_CHAN_MAP_PTE_MASK)
269 				    != silentpage)
270 					break;
271 			if (j == numblocks) {
272 				PRINT(("emuxki_pmem_alloc : j == numblocks %ld\n", j));
273 				if ((mem = emuxki_mem_new(card, i, size)) == NULL) {
274 					//splx(s);
275 					return (NULL);
276 				}
277 				PRINT(("emuxki_pmem_alloc : j == numblocks emuxki_mem_new ok\n"));
278 				for (j = 0; j < numblocks; j++)
279 					ptb[i + j] = B_HOST_TO_LENDIAN_INT32((uint32) (
280 						(( ((uint32)mem->phy_base) +
281 						 j * EMU_PTESIZE) << 1)
282 						| (i + j)));
283 				LIST_INSERT_HEAD(&(card->mem), mem, next);
284 				PRINT(("emuxki_pmem_alloc : j == numblocks returning\n"));
285 
286 				//splx(s);
287 				return mem->log_base;
288 			} else {
289 				PRINT(("emuxki_pmem_alloc : j != numblocks %ld\n", j));
290 				i += j;
291 			}
292 			//splx(s);
293 		}
294 	}
295 	return NULL;
296 }
297 
298 
299 void *
300 emuxki_rmem_alloc(emuxki_dev *card, size_t size)
301 {
302 	emuxki_mem *mem;
303 	//int             s;
304 
305 	mem = emuxki_mem_new(card, EMU_RMEM, size);
306 	if (mem == NULL)
307 		return (NULL);
308 
309 	//s = splaudio();
310 	LIST_INSERT_HEAD(&(card->mem), mem, next);
311 	//splx(s);
312 
313 	return mem->log_base;
314 }
315 
316 
317 void
318 emuxki_mem_free(emuxki_dev *card, void *ptr)
319 {
320 	emuxki_mem 		*mem;
321 	size_t          numblocks;
322 	uint32      	i, *ptb, silentpage;
323 
324 	ptb = card->ptb_log_base;
325 	silentpage = ((uint32)card->silentpage_phy_base) << 1;
326 	LOG(("emuxki_mem_free 1\n"));
327 	LIST_FOREACH(mem, &card->mem, next) {
328 		LOG(("emuxki_mem_free 2\n"));
329 		if (mem->log_base != ptr)
330 			continue;
331 		LOG(("emuxki_mem_free 3\n"));
332 		//s = splaudio();
333 		if (mem->ptbidx != EMU_RMEM) {
334 			LOG(("mem_size : %i\n", mem->size));
335 			numblocks = mem->size / EMU_PTESIZE;
336 			if (mem->size % EMU_PTESIZE)
337 				numblocks++;
338 			for (i = 0; i < numblocks; i++)
339 				ptb[mem->ptbidx + i] =
340 					B_HOST_TO_LENDIAN_INT32(silentpage | (mem->ptbidx + i));
341 		}
342 		LIST_REMOVE(mem, next);
343 		//splx(s);
344 
345 		LOG(("freeing mem\n"));
346 		emuxki_mem_delete(mem);
347 		break;
348 	}
349 }
350 
351 /* Emuxki channel functions */
352 
353 static void
354 emuxki_chanparms_set_defaults(emuxki_channel *chan)
355 {
356 	chan->fxsend.a.level = chan->fxsend.b.level
357 		= chan->fxsend.c.level = chan->fxsend.d.level
358 	/* for audigy */
359 		= chan->fxsend.e.level = chan->fxsend.f.level
360 		= chan->fxsend.g.level = chan->fxsend.h.level
361 		= IS_AUDIGY(&chan->voice->stream->card->config) ? 0xc0 : 0xff;	/* not max */
362 
363 	chan->fxsend.a.dest = 0x0;
364 	chan->fxsend.b.dest = 0x1;
365 	chan->fxsend.c.dest = 0x2;
366 	chan->fxsend.d.dest = 0x3;
367 	/* for audigy */
368 	chan->fxsend.e.dest = 0x4;
369 	chan->fxsend.f.dest = 0x5;
370 	chan->fxsend.g.dest = 0x6;
371 	chan->fxsend.h.dest = 0x7;
372 
373 	chan->pitch.intial = 0x0000;	/* shouldn't it be 0xE000 ? */
374 	chan->pitch.current = 0x0000;	/* should it be 0x0400 */
375 	chan->pitch.target = 0x0000;	/* the unity pitch shift ? */
376 	chan->pitch.envelope_amount = 0x00;	/* none */
377 
378 	chan->initial_attenuation = 0x00;	/* no attenuation */
379 	chan->volume.current = 0x0000;	/* no volume */
380 	chan->volume.target = 0xffff;
381 	chan->volume.envelope.current_state = 0x8000;	/* 0 msec delay */
382 	chan->volume.envelope.hold_time = 0x7f;	/* 0 msec */
383 	chan->volume.envelope.attack_time = 0x7f;	/* 5.5msec */
384 	chan->volume.envelope.sustain_level = 0x7f;	/* full  */
385 	chan->volume.envelope.decay_time = 0x7f;	/* 22msec  */
386 
387 	chan->filter.initial_cutoff_frequency = 0xff;	/* no filter */
388 	chan->filter.current_cutoff_frequency = 0xffff;	/* no filtering */
389 	chan->filter.target_cutoff_frequency = 0xffff;	/* no filtering */
390 	chan->filter.lowpass_resonance_height = 0x0;
391 	chan->filter.interpolation_ROM = 0x1;	/* full band */
392 	chan->filter.envelope_amount = 0x7f;	/* none */
393 	chan->filter.LFO_modulation_depth = 0x00;	/* none */
394 
395 	chan->loop.start = 0x000000;
396 	chan->loop.end = 0x000010;	/* Why ? */
397 
398 	chan->modulation.envelope.current_state = 0x8000;
399 	chan->modulation.envelope.hold_time = 0x00;	/* 127 better ? */
400 	chan->modulation.envelope.attack_time = 0x00;	/* infinite */
401 	chan->modulation.envelope.sustain_level = 0x00;	/* off */
402 	chan->modulation.envelope.decay_time = 0x7f;	/* 22 msec */
403 	chan->modulation.LFO_state = 0x8000;
404 
405 	chan->vibrato_LFO.state = 0x8000;
406 	chan->vibrato_LFO.modulation_depth = 0x00;	/* none */
407 	chan->vibrato_LFO.vibrato_depth = 0x00;
408 	chan->vibrato_LFO.frequency = 0x00;	/* Why set to 24 when
409 						 * initialized ? */
410 
411 	chan->tremolo_depth = 0x00;
412 }
413 
414 
415 static emuxki_channel *
416 emuxki_channel_new(emuxki_voice *voice, uint8 num)
417 {
418 	emuxki_channel *chan;
419 
420 	chan = (emuxki_channel *) malloc(sizeof(emuxki_channel));
421 	if (chan == NULL)
422 		return NULL;
423 	chan->voice = voice;
424 	chan->num = num;
425 	emuxki_chanparms_set_defaults(chan);
426 	chan->voice->stream->card->channel[num] = chan;
427 	return chan;
428 }
429 
430 
431 static void
432 emuxki_channel_delete(emuxki_channel *chan)
433 {
434 	chan->voice->stream->card->channel[chan->num] = NULL;
435 	free(chan);
436 }
437 
438 
439 static void
440 emuxki_channel_set_fxsend(emuxki_channel *chan,
441 			   emuxki_chanparms_fxsend *fxsend)
442 {
443 	/* Could do a memcpy ...*/
444 	chan->fxsend.a.level = fxsend->a.level;
445 	chan->fxsend.b.level = fxsend->b.level;
446 	chan->fxsend.c.level = fxsend->c.level;
447 	chan->fxsend.d.level = fxsend->d.level;
448 	chan->fxsend.a.dest = fxsend->a.dest;
449 	chan->fxsend.b.dest = fxsend->b.dest;
450 	chan->fxsend.c.dest = fxsend->c.dest;
451 	chan->fxsend.d.dest = fxsend->d.dest;
452 
453 	/* for audigy */
454 	chan->fxsend.e.level = fxsend->e.level;
455 	chan->fxsend.f.level = fxsend->f.level;
456 	chan->fxsend.g.level = fxsend->g.level;
457 	chan->fxsend.h.level = fxsend->h.level;
458 	chan->fxsend.e.dest = fxsend->e.dest;
459 	chan->fxsend.f.dest = fxsend->f.dest;
460 	chan->fxsend.g.dest = fxsend->g.dest;
461 	chan->fxsend.h.dest = fxsend->h.dest;
462 }
463 
464 
465 static void
466 emuxki_channel_set_srate(emuxki_channel *chan, uint32 srate)
467 {
468 	chan->pitch.target = (srate << 8) / 375;
469 	chan->pitch.target = (chan->pitch.target >> 1) +
470 		(chan->pitch.target & 1);
471 	chan->pitch.target &= 0xffff;
472 	chan->pitch.current = chan->pitch.target;
473 	chan->pitch.intial =
474 		(emuxki_rate_to_pitch(srate) >> 8) & EMU_CHAN_IP_MASK;
475 }
476 
477 
478 /* voice params must be set before calling this */
479 static void
480 emuxki_channel_set_bufparms(emuxki_channel *chan,
481 			     uint32 start, uint32 end)
482 {
483 	chan->loop.start = start & EMU_CHAN_PSST_LOOPSTARTADDR_MASK;
484 	chan->loop.end = end & EMU_CHAN_DSL_LOOPENDADDR_MASK;
485 }
486 
487 
488 static void
489 emuxki_channel_commit_fx(emuxki_channel *chan)
490 {
491 	emuxki_dev   	*card = chan->voice->stream->card;
492 	uint8			chano = chan->num;
493 
494 	if (IS_AUDIGY(&card->config)) {
495 		emuxki_chan_write(&card->config, chano, 0x4c, 0);
496 		emuxki_chan_write(&card->config, chano, 0x4d, 0);
497 		emuxki_chan_write(&card->config, chano, 0x4e, 0);
498 		emuxki_chan_write(&card->config, chano, 0x4f, 0);
499 
500 		emuxki_chan_write(&card->config, chano, EMU_A_CHAN_FXRT1,
501 			      (chan->fxsend.d.dest << 24) |
502 			      (chan->fxsend.c.dest << 16) |
503 			      (chan->fxsend.b.dest << 8) |
504 			      (chan->fxsend.a.dest));
505 		emuxki_chan_write(&card->config, chano, EMU_A_CHAN_FXRT2,
506 			      (chan->fxsend.h.dest << 24) |
507 			      (chan->fxsend.g.dest << 16) |
508 			      (chan->fxsend.f.dest << 8) |
509 			      (chan->fxsend.e.dest));
510 		emuxki_chan_write(&card->config, chano, EMU_A_CHAN_SENDAMOUNTS,
511 			      (chan->fxsend.e.level << 24) |
512 			      (chan->fxsend.f.level << 16) |
513 			      (chan->fxsend.g.level << 8) |
514 			      (chan->fxsend.h.level));
515 	} else {
516 		emuxki_chan_write(&card->config, chano, EMU_CHAN_FXRT,
517 			      (chan->fxsend.d.dest << 28) |
518 			      (chan->fxsend.c.dest << 24) |
519 			      (chan->fxsend.b.dest << 20) |
520 			      (chan->fxsend.a.dest << 16));
521 	}
522 
523 	emuxki_chan_write(&card->config, chano, 0x10000000 | EMU_CHAN_PTRX,
524 		      (chan->fxsend.a.level << 8) | chan->fxsend.b.level);
525 	emuxki_chan_write(&card->config, chano, EMU_CHAN_DSL,
526 		      (chan->fxsend.d.level << 24) | chan->loop.end);
527 	emuxki_chan_write(&card->config, chano, EMU_CHAN_PSST,
528 		      (chan->fxsend.c.level << 24) | chan->loop.start);
529 }
530 
531 
532 static void
533 emuxki_channel_commit_parms(emuxki_channel *chan)
534 {
535 	emuxki_voice *voice = chan->voice;
536 	emuxki_dev   	*card = chan->voice->stream->card;
537 	uint32			start, mapval;
538 	uint8			chano = chan->num;
539 	//int             s;
540 
541 	start = chan->loop.start +
542 		(voice->stereo ? 28 : 30) * (voice->b16 + 1);
543 	mapval = ((uint32)card->silentpage_phy_base) << 1 | EMU_CHAN_MAP_PTI_MASK;
544 
545 	//s = splaudio();
546 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CPF_STEREO, voice->stereo);
547 
548 	emuxki_channel_commit_fx(chan);
549 
550 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CCCA,
551 		      (chan->filter.lowpass_resonance_height << 28) |
552 		      (chan->filter.interpolation_ROM << 25) |
553 		   (voice->b16 ? 0 : EMU_CHAN_CCCA_8BITSELECT) | start);
554 	emuxki_chan_write(&card->config, chano, EMU_CHAN_Z1, 0);
555 	emuxki_chan_write(&card->config, chano, EMU_CHAN_Z2, 0);
556 	emuxki_chan_write(&card->config, chano, EMU_CHAN_MAPA, mapval);
557 	emuxki_chan_write(&card->config, chano, EMU_CHAN_MAPB, mapval);
558 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CVCF_CURRFILTER,
559 		      chan->filter.current_cutoff_frequency);
560 	emuxki_chan_write(&card->config, chano, EMU_CHAN_VTFT_FILTERTARGET,
561 		      chan->filter.target_cutoff_frequency);
562 	emuxki_chan_write(&card->config, chano, EMU_CHAN_ATKHLDM,
563 		      (chan->modulation.envelope.hold_time << 8) |
564 		      chan->modulation.envelope.attack_time);
565 	emuxki_chan_write(&card->config, chano, EMU_CHAN_DCYSUSM,
566 		      (chan->modulation.envelope.sustain_level << 8) |
567 		      chan->modulation.envelope.decay_time);
568 	emuxki_chan_write(&card->config, chano, EMU_CHAN_LFOVAL1,
569 		      chan->modulation.LFO_state);
570 	emuxki_chan_write(&card->config, chano, EMU_CHAN_LFOVAL2,
571 		      chan->vibrato_LFO.state);
572 	emuxki_chan_write(&card->config, chano, EMU_CHAN_FMMOD,
573 		      (chan->vibrato_LFO.modulation_depth << 8) |
574 		      chan->filter.LFO_modulation_depth);
575 	emuxki_chan_write(&card->config, chano, EMU_CHAN_TREMFRQ,
576 		      (chan->tremolo_depth << 8));
577 	emuxki_chan_write(&card->config, chano, EMU_CHAN_FM2FRQ2,
578 		      (chan->vibrato_LFO.vibrato_depth << 8) |
579 		      chan->vibrato_LFO.frequency);
580 	emuxki_chan_write(&card->config, chano, EMU_CHAN_ENVVAL,
581 		      chan->modulation.envelope.current_state);
582 	emuxki_chan_write(&card->config, chano, EMU_CHAN_ATKHLDV,
583 		      (chan->volume.envelope.hold_time << 8) |
584 		      chan->volume.envelope.attack_time);
585 	emuxki_chan_write(&card->config, chano, EMU_CHAN_ENVVOL,
586 		      chan->volume.envelope.current_state);
587 	emuxki_chan_write(&card->config, chano, EMU_CHAN_PEFE,
588 		      (chan->pitch.envelope_amount << 8) |
589 		      chan->filter.envelope_amount);
590 	//splx(s);
591 }
592 
593 
594 static void
595 emuxki_channel_start(emuxki_channel *chan)
596 {
597 	emuxki_voice *voice = chan->voice;
598 	emuxki_dev   *card = chan->voice->stream->card;
599 	uint8        cache_sample, cache_invalid_size, chano = chan->num;
600 	uint32       sample;
601 	//int             s;
602 
603 	cache_sample = voice->stereo ? 4 : 2;
604 	sample = voice->b16 ? 0x00000000 : 0x80808080;
605 	cache_invalid_size = (voice->stereo ? 28 : 30) * (voice->b16 + 1);
606 
607 	//s = splaudio();
608 	while (cache_sample--)
609 		emuxki_chan_write(&card->config, chano,
610 			      EMU_CHAN_CD0 + cache_sample, sample);
611 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE, 0);
612 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CCR_READADDRESS, 64);
613 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE,
614 		      cache_invalid_size);
615 	emuxki_chan_write(&card->config, chano, EMU_CHAN_IFATN,
616 		      (chan->filter.target_cutoff_frequency << 8) |
617 		      chan->initial_attenuation);
618 	emuxki_chan_write(&card->config, chano, EMU_CHAN_VTFT_VOLUMETARGET,
619 		      chan->volume.target);
620 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CVCF_CURRVOL,
621 		      chan->volume.current);
622 	emuxki_chan_write(&card->config, 0, EMU_MKSUBREG(1, chano,
623 					       EMU_SOLEL + (chano >> 5)),
624 		      0);	/* Clear stop on loop */
625 	emuxki_chan_write(&card->config, 0, EMU_MKSUBREG(1, chano,
626 					       EMU_CLIEL + (chano >> 5)),
627 		      0);	/* Clear loop interrupt */
628 	emuxki_chan_write(&card->config, chano, EMU_CHAN_DCYSUSV,
629 		      (chan->volume.envelope.sustain_level << 8) |
630 		      chan->volume.envelope.decay_time);
631 	emuxki_chan_write(&card->config, chano, EMU_CHAN_PTRX_PITCHTARGET,
632 		      chan->pitch.target);
633 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CPF_PITCH,
634 		      chan->pitch.current);
635 	emuxki_chan_write(&card->config, chano, EMU_CHAN_IP, chan->pitch.intial);
636 
637 	//splx(s);
638 }
639 
640 
641 static void
642 emuxki_channel_stop(emuxki_channel *chan)
643 {
644 	emuxki_dev   	*card = chan->voice->stream->card;
645 	//int             s;
646 	uint8           chano = chan->num;
647 
648 	//s = splaudio();
649 
650 	emuxki_chan_write(&card->config, chano, EMU_CHAN_PTRX_PITCHTARGET, 0);
651 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CPF_PITCH, 0);
652 	emuxki_chan_write(&card->config, chano, EMU_CHAN_IFATN_ATTENUATION, 0xff);
653 	emuxki_chan_write(&card->config, chano, EMU_CHAN_VTFT_VOLUMETARGET, 0);
654 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CVCF_CURRVOL, 0);
655 	emuxki_chan_write(&card->config, chano, EMU_CHAN_IP, 0);
656 
657 	//splx(s);
658 }
659 
660 
661 /*	Emuxki voice functions */
662 /*static void
663 emuxki_dump_voice(emuxki_voice *voice)
664 {
665 	LOG(("voice->use = %#u\n", voice->use));
666 	LOG(("voice->state = %#u\n", voice->state));
667 	LOG(("voice->stereo = %#u\n", voice->stereo));
668 	LOG(("voice->b16 = %#u\n", voice->b16));
669 	LOG(("voice->sample_rate = %#lu\n", voice->sample_rate));
670 	LOG(("voice->buffer = %#08x\n", voice->buffer));
671 	if (voice->buffer) {
672 		LOG(("voice->buffer->ptbidx = %#u\n", voice->buffer->ptbidx));
673 		LOG(("voice->buffer->log_base = %#08x\n", voice->buffer->log_base));
674 		LOG(("voice->buffer->phy_base = %#08x\n", voice->buffer->phy_base));
675 		LOG(("voice->buffer->size = %#08x\n", voice->buffer->size));
676 		LOG(("voice->buffer->area = %#08x\n", voice->buffer->area));
677 	}
678 	LOG(("voice->blksize = %#u\n", voice->blksize));
679 	LOG(("voice->trigblk = %#u\n", voice->trigblk));
680 	LOG(("voice->blkmod = %#u\n", voice->blkmod));
681 	LOG(("voice->timerate = %#u\n", voice->timerate));
682 }*/
683 
684 
685 /* Allocate channels for voice in case of play voice */
686 static status_t
687 emuxki_voice_channel_create(emuxki_voice *voice)
688 {
689 	emuxki_channel **channel = voice->stream->card->channel;
690 	uint8	        i, stereo = voice->stereo;
691 	//int             s;
692 
693 	for (i = 0; i < EMU_NUMCHAN - stereo; i += stereo + 1) {
694 		if ((stereo && (channel[i + 1] != NULL)) ||
695 		    (channel[i] != NULL))	/* Looking for free channels */
696 			continue;
697 		//s = splaudio();
698 		if (stereo) {
699 			voice->dataloc.chan[1] =
700 				emuxki_channel_new(voice, i + 1);
701 			if (voice->dataloc.chan[1] == NULL) {
702 				//splx(s);
703 				return ENOMEM;
704 			}
705 		}
706 		voice->dataloc.chan[0] = emuxki_channel_new(voice, i);
707 		if (voice->dataloc.chan[0] == NULL) {
708 			if (stereo) {
709 				emuxki_channel_delete(voice->dataloc.chan[1]);
710 				voice->dataloc.chan[1] = NULL;
711 			}
712 			//splx(s);
713 			return ENOMEM;
714 		}
715 		//splx(s);
716 		return B_OK;
717 	}
718 	return EAGAIN;
719 }
720 
721 
722 /* When calling this function we assume no one can access the voice */
723 static void
724 emuxki_voice_channel_destroy(emuxki_voice *voice)
725 {
726 	emuxki_channel_delete(voice->dataloc.chan[0]);
727 	voice->dataloc.chan[0] = NULL;
728 	if (voice->stereo)
729 		emuxki_channel_delete(voice->dataloc.chan[1]);
730 	voice->dataloc.chan[1] = NULL;
731 }
732 
733 
734 static status_t
735 emuxki_voice_dataloc_create(emuxki_voice *voice)
736 {
737 	status_t	error;
738 
739 	if (voice->use & EMU_USE_PLAY) {
740 		if ((error = emuxki_voice_channel_create(voice)))
741 			return (error);
742 	} else {
743 
744 	}
745 	return B_OK;
746 }
747 
748 
749 static void
750 emuxki_voice_dataloc_destroy(emuxki_voice *voice)
751 {
752 	if (voice->use & EMU_USE_PLAY) {
753 		if (voice->dataloc.chan[0] != NULL)
754 			emuxki_voice_channel_destroy(voice);
755 	} else {
756 		uint32 buffaddr_reg, buffsize_reg;
757 		switch (voice->dataloc.source) {
758 			case EMU_RECSRC_MIC:
759 				buffaddr_reg = EMU_MICBA;
760 				buffsize_reg = EMU_MICBS;
761 				break;
762 			case EMU_RECSRC_ADC:
763 				buffaddr_reg = EMU_ADCBA;
764 				buffsize_reg = EMU_ADCBS;
765 				break;
766 			case EMU_RECSRC_FX:
767 				buffaddr_reg = EMU_FXBA;
768 				buffsize_reg = EMU_FXBS;
769 				break;
770 			default:
771 				return;
772 		}
773 		emuxki_chan_write(&voice->stream->card->config, 0, buffaddr_reg, 0);
774 		emuxki_chan_write(&voice->stream->card->config, 0, buffsize_reg,
775 			EMU_RECBS_BUFSIZE_NONE);
776 	}
777 }
778 
779 
780 static void
781 emuxki_voice_fxupdate(emuxki_voice *voice)
782 {
783 	emuxki_chanparms_fxsend fxsend;
784 
785 	uint8 maxlevel =
786 		IS_AUDIGY(&voice->stream->card->config) ? 0xc0 : 0xff;	/* not max */
787 
788 	if (voice->use & EMU_USE_PLAY) {
789 		fxsend.a.dest = 0x3f;
790 		fxsend.b.dest = 0x3f;
791 		fxsend.c.dest = 0x3f;
792 		fxsend.d.dest = 0x3f;
793 		/* for audigy */
794 		fxsend.e.dest = 0x3f;
795 		fxsend.f.dest = 0x3f;
796 		fxsend.g.dest = 0x3f;
797 		fxsend.h.dest = 0x3f;
798 
799 		fxsend.a.level = fxsend.b.level = fxsend.c.level = fxsend.d.level =
800 		fxsend.e.level = fxsend.g.level = fxsend.f.level = fxsend.h.level = 0x00;
801 
802 		if (voice->stereo) {
803 			switch(voice->stream->card->play_mode) {
804 				case 2:
805 					if (voice->stream->nstereo == 1) {
806 						fxsend.a.dest = voice->voicenum * 2;
807 						fxsend.a.level = maxlevel;
808 					} else if ((voice->stream->nstereo == 2) ||
809 						((voice->stream->nstereo == 3)&&(voice->voicenum < 2))) {
810 						fxsend.a.dest = voice->voicenum * 2;
811 						fxsend.a.level = maxlevel;
812 						if (voice->voicenum > 1 - 1)
813 							fxsend.a.dest-=2;
814 					} else if (voice->stream->nstereo == 3 && voice->voicenum > 1) {
815 							fxsend.a.dest = 0x0;
816 							fxsend.a.level = maxlevel / 2;
817 							fxsend.b.dest = 0x1;
818 							fxsend.b.level = maxlevel / 2;
819 					} else {
820 						LOG(("emuxki_voice_set_stereo case 2 badly managed\n"));
821 					}
822 					break;
823 				case 4:
824 					if (voice->stream->nstereo == 1) {
825 						fxsend.a.dest = voice->voicenum * 2;
826 						fxsend.a.level = maxlevel;
827 						fxsend.b.dest = voice->voicenum * 2 + 2;
828 						fxsend.b.level = maxlevel;
829 					} else if ((voice->stream->nstereo == 2) ||
830 						((voice->stream->nstereo == 3)&&(voice->voicenum < 2))) {
831 						fxsend.a.dest = voice->voicenum * 2;
832 						fxsend.a.level = maxlevel;
833 					} else if (voice->stream->nstereo == 3 && voice->voicenum > 1) {
834 						fxsend.a.dest = 0x0;
835 						fxsend.a.level = maxlevel / 2;
836 						fxsend.b.dest = 0x1;
837 						fxsend.b.level = maxlevel / 2;
838 					} else {
839 						LOG(("emuxki_voice_set_stereo case 4 badly managed\n"));
840 					}
841 					break;
842 				case 6: // only on audigy
843 					if (voice->stream->nstereo == 1) {
844 						fxsend.a.dest = voice->voicenum * 2;
845 						fxsend.a.level = maxlevel;
846 						fxsend.b.dest = voice->voicenum * 2 + 2;
847 						fxsend.b.level = maxlevel;
848 						fxsend.c.dest = 0x4;
849 						fxsend.c.level = maxlevel / 2;
850 						fxsend.d.dest = 0x5;
851 						fxsend.d.level = maxlevel / 2;
852 					} else if (voice->stream->nstereo == 2) {
853 						fxsend.a.dest = voice->voicenum * 2;
854 						fxsend.a.level = maxlevel;
855 						if (voice->voicenum < 1) {
856 							fxsend.b.dest = 0x4;
857 							fxsend.b.level = maxlevel / 2;
858 							fxsend.c.dest = 0x5;
859 							fxsend.c.level = maxlevel / 2;
860 						}
861 					} else if (voice->stream->nstereo == 3) {
862 						fxsend.a.dest = voice->voicenum * 2;
863 						fxsend.a.level = maxlevel;
864 					} else {
865 						LOG(("emuxki_voice_set_stereo case 6 badly managed\n"));
866 					}
867 					break;
868 			}
869 
870 			emuxki_channel_set_fxsend(voice->dataloc.chan[0],
871 						   &fxsend);
872 
873 			switch(voice->stream->card->play_mode) {
874 				case 2:
875 					if (voice->stream->nstereo == 1) {
876 						fxsend.a.dest = voice->voicenum * 2 + 1;
877 						fxsend.a.level = maxlevel;
878 					} else if ((voice->stream->nstereo == 2) ||
879 						((voice->stream->nstereo == 3)&&(voice->voicenum < 2))) {
880 						fxsend.a.dest = voice->voicenum * 2 + 1;
881 						fxsend.a.level = maxlevel;
882 						if (voice->voicenum > 1 - 1)
883 							fxsend.a.dest-=2;
884 					} else if (voice->stream->nstereo == 3 && voice->voicenum > 1) {
885 							fxsend.a.dest = 0x0;
886 							fxsend.a.level = maxlevel / 2;
887 							fxsend.b.dest = 0x1;
888 							fxsend.b.level = maxlevel / 2;
889 					} else {
890 						LOG(("emuxki_voice_set_stereo case 2 badly managed\n"));
891 					}
892 					break;
893 				case 4:
894 					if (voice->stream->nstereo == 1) {
895 						fxsend.a.dest = voice->voicenum * 2 + 1;
896 						fxsend.a.level = maxlevel;
897 						fxsend.b.dest = voice->voicenum * 2 + 3;
898 						fxsend.b.level = maxlevel;
899 					} else if ((voice->stream->nstereo == 2) ||
900 						((voice->stream->nstereo == 3)&&(voice->voicenum < 2))) {
901 						fxsend.a.dest = voice->voicenum * 2 + 1;
902 						fxsend.a.level = maxlevel;
903 					} else if (voice->stream->nstereo == 3 && voice->voicenum > 1) {
904 						fxsend.a.dest = 0x0;
905 						fxsend.a.level = maxlevel / 2;
906 						fxsend.b.dest = 0x1;
907 						fxsend.b.level = maxlevel / 2;
908 					} else {
909 						LOG(("emuxki_voice_set_stereo case 4 badly managed\n"));
910 					}
911 					break;
912 				case 6: // only on audigy
913 					if (voice->stream->nstereo == 1) {
914 						fxsend.a.dest = voice->voicenum * 2 + 1;
915 						fxsend.a.level = maxlevel;
916 						fxsend.b.dest = voice->voicenum * 2 + 3;
917 						fxsend.b.level = maxlevel;
918 						fxsend.c.dest = 0x4;
919 						fxsend.c.level = maxlevel / 2;
920 						fxsend.d.dest = 0x5;
921 						fxsend.d.level = maxlevel / 2;
922 					} else if (voice->stream->nstereo == 2) {
923 						fxsend.a.dest = voice->voicenum * 2 + 1;
924 						fxsend.a.level = maxlevel;
925 						if (voice->voicenum < 1) {
926 							fxsend.b.dest = 0x4;
927 							fxsend.b.level = maxlevel / 2;
928 							fxsend.c.dest = 0x5;
929 							fxsend.c.level = maxlevel / 2;
930 						}
931 					} else if (voice->stream->nstereo == 3) {
932 						fxsend.a.dest = voice->voicenum * 2 + 1;
933 						fxsend.a.level = maxlevel;
934 					} else {
935 						LOG(("emuxki_voice_set_stereo case 6 badly managed\n"));
936 					}
937 					break;
938 			}
939 
940 			emuxki_channel_set_fxsend(voice->dataloc.chan[1],
941 						   &fxsend);
942 		} else {
943 			switch(voice->stream->card->play_mode) {
944 				case 2:
945 					if (voice->stream->nmono == 1) {
946 						fxsend.a.dest = voice->voicenum;
947 						fxsend.a.level = maxlevel;
948 						fxsend.b.dest = voice->voicenum + 1;
949 						fxsend.b.level = maxlevel;
950 					} else if (voice->stream->nmono == 2) {
951 						fxsend.a.dest = voice->voicenum;
952 						fxsend.a.level = maxlevel;
953 					} else if ((voice->stream->nmono == 4) ||
954 						((voice->stream->nmono == 6)&&(voice->voicenum < 4))) {
955 						fxsend.a.dest = voice->voicenum;
956 						fxsend.a.level = maxlevel;
957 						if (voice->voicenum > 2 - 1)
958 							fxsend.a.dest-=2;
959 					} else if (voice->stream->nmono == 6 && voice->voicenum > 3) {
960 							fxsend.a.dest = 0x0;
961 							fxsend.a.level = maxlevel / 2;
962 							fxsend.b.dest = 0x1;
963 							fxsend.b.level = maxlevel / 2;
964 					} else {
965 						LOG(("emuxki_voice_set_stereo case 2 badly managed\n"));
966 					}
967 					break;
968 				case 4:
969 					if (voice->stream->nmono == 1) {
970 						fxsend.a.dest = voice->voicenum;
971 						fxsend.a.level = maxlevel;
972 						fxsend.b.dest = voice->voicenum + 1;
973 						fxsend.b.level = maxlevel;
974 						fxsend.c.dest = voice->voicenum + 2;
975 						fxsend.c.level = maxlevel;
976 						fxsend.d.dest = voice->voicenum + 3;
977 						fxsend.d.level = maxlevel;
978 					} else if (voice->stream->nmono == 2) {
979 						fxsend.a.dest = voice->voicenum;
980 						fxsend.a.level = maxlevel;
981 						fxsend.b.dest = voice->voicenum + 2;
982 						fxsend.b.level = maxlevel;
983 					} else if ((voice->stream->nmono == 4) ||
984 						((voice->stream->nmono == 6)&&(voice->voicenum < 4))) {
985 						fxsend.a.dest = voice->voicenum;
986 						fxsend.a.level = maxlevel;
987 					} else if (voice->stream->nmono == 6 && voice->voicenum > 3) {
988 							fxsend.a.dest = 0x0;
989 							fxsend.a.level = maxlevel / 2;
990 							fxsend.b.dest = 0x1;
991 							fxsend.b.level = maxlevel / 2;
992 					} else {
993 						LOG(("emuxki_voice_set_stereo case 4 badly managed\n"));
994 					}
995 					break;
996 				case 6: // only on audigy
997 					if (voice->stream->nmono == 1) {
998 						fxsend.a.dest = voice->voicenum;
999 						fxsend.a.level = maxlevel;
1000 						fxsend.b.dest = voice->voicenum + 1;
1001 						fxsend.b.level = maxlevel;
1002 						fxsend.c.dest = voice->voicenum + 2;
1003 						fxsend.c.level = maxlevel;
1004 						fxsend.d.dest = voice->voicenum + 3;
1005 						fxsend.d.level = maxlevel;
1006 						fxsend.e.dest = voice->voicenum + 4;
1007 						fxsend.e.level = maxlevel;
1008 						fxsend.f.dest = voice->voicenum + 5;
1009 						fxsend.f.level = maxlevel;
1010 					} else if (voice->stream->nmono == 2) {
1011 						fxsend.a.dest = voice->voicenum;
1012 						fxsend.a.level = maxlevel;
1013 						fxsend.b.dest = voice->voicenum + 2;
1014 						fxsend.b.level = maxlevel;
1015 						fxsend.c.dest = 0x4;
1016 						fxsend.c.level = maxlevel / 2;
1017 						fxsend.d.dest = 0x5;
1018 						fxsend.d.level = maxlevel / 2;
1019 					} else if (voice->stream->nmono == 4) {
1020 						fxsend.a.dest = voice->voicenum;
1021 						fxsend.a.level = maxlevel;
1022 						if (voice->voicenum < 2) {
1023 							fxsend.b.dest = 0x4;
1024 							fxsend.b.level = maxlevel / 2;
1025 							fxsend.c.dest = 0x5;
1026 							fxsend.c.level = maxlevel / 2;
1027 						}
1028 					} else if (voice->stream->nmono == 6) {
1029 						fxsend.a.dest = voice->voicenum;
1030 						fxsend.a.level = maxlevel;
1031 					} else {
1032 						LOG(("emuxki_voice_set_stereo case 6 badly managed\n"));
1033 					}
1034 					break;
1035 			}
1036 
1037 			emuxki_channel_set_fxsend(voice->dataloc.chan[0],
1038 						   &fxsend);
1039 		}
1040 	}
1041 }
1042 
1043 
1044 static status_t
1045 emuxki_voice_set_stereo(emuxki_voice *voice, uint8 stereo)
1046 {
1047 	status_t	error;
1048 
1049 	emuxki_voice_dataloc_destroy(voice);
1050 	voice->stereo = stereo;
1051 	if ((error = emuxki_voice_dataloc_create(voice)))
1052 	  return error;
1053 	emuxki_voice_fxupdate(voice);
1054 	return B_OK;
1055 }
1056 
1057 
1058 static status_t
1059 emuxki_voice_set_srate(emuxki_voice *voice, uint32 srate)
1060 {
1061 	voice->sample_rate = srate;
1062 	if (voice->use & EMU_USE_PLAY) {
1063 		if ((srate < 4000) || (srate > 48000))
1064 			return (EINVAL);
1065 		emuxki_channel_set_srate(voice->dataloc.chan[0], srate);
1066 		if (voice->stereo)
1067 			emuxki_channel_set_srate(voice->dataloc.chan[1],
1068 						  srate);
1069 	}
1070 	return B_OK;
1071 }
1072 
1073 
1074 status_t
1075 emuxki_voice_set_audioparms(emuxki_voice *voice, uint8 stereo,
1076 			     uint8 b16, uint32 srate)
1077 {
1078 	status_t             error;
1079 
1080 	if (voice->stereo == stereo && voice->b16 == b16 &&
1081 	    voice->sample_rate == srate)
1082 		return B_OK;
1083 
1084 	if (voice->stereo != stereo) {
1085 		if ((error = emuxki_voice_set_stereo(voice, stereo)))
1086 			return error;
1087 	}
1088 	voice->b16 = b16;
1089 	if (voice->sample_rate != srate)
1090 		emuxki_voice_set_srate(voice, srate);
1091 	return B_OK;
1092 }
1093 
1094 
1095 status_t
1096 emuxki_voice_set_recparms(emuxki_voice *voice, emuxki_recsrc_t recsrc,
1097 			     	emuxki_recparams *recparams)
1098 {
1099 	if (voice->use & EMU_USE_RECORD) {
1100 		switch(recsrc) {
1101 			case EMU_RECSRC_MIC:
1102 				break;
1103 			case EMU_RECSRC_ADC:
1104 				break;
1105 			case EMU_RECSRC_FX:
1106 				if (!recparams)
1107 					return B_ERROR;
1108 				voice->recparams.efx_voices[0] = recparams->efx_voices[0];
1109 				voice->recparams.efx_voices[1] = recparams->efx_voices[1];
1110 				break;
1111 			default:
1112 				return B_ERROR;
1113 				break;
1114 		}
1115 		voice->dataloc.source = recsrc;
1116 	}
1117 	return B_OK;
1118 }
1119 
1120 
1121 /* voice audio parms (see just before) must be set prior to this */
1122 status_t
1123 emuxki_voice_set_bufparms(emuxki_voice *voice, void *ptr,
1124 			   uint32 bufsize, uint16 blksize)
1125 {
1126 	emuxki_mem *mem;
1127 	emuxki_channel **chan;
1128 	uint32 start, end;
1129 	uint8 sample_size;
1130 	status_t error = EFAULT;
1131 
1132 	LIST_FOREACH(mem, &voice->stream->card->mem, next) {
1133 		if (mem->log_base != ptr)
1134 			continue;
1135 
1136 		voice->buffer = mem;
1137 		sample_size = (voice->b16 + 1) * (voice->stereo + 1);
1138 		voice->trigblk = 0;	/* This shouldn't be needed */
1139 		voice->blkmod = bufsize / blksize;
1140 		if (bufsize % blksize) 	  /* This should not happen */
1141 			voice->blkmod++;
1142 		error = 0;
1143 
1144 		if (voice->use & EMU_USE_PLAY) {
1145 			voice->blksize = blksize / sample_size;
1146 			chan = voice->dataloc.chan;
1147 			start = (mem->ptbidx << 12) / sample_size;
1148 			end = start + bufsize / sample_size;
1149 			emuxki_channel_set_bufparms(chan[0],
1150 						     start, end);
1151 			if (voice->stereo)
1152 				emuxki_channel_set_bufparms(chan[1],
1153 				     start, end);
1154 			voice->timerate = (uint32) 48000 *
1155 			                voice->blksize / voice->sample_rate / 2;
1156 			if (voice->timerate < 5)
1157 				error = EINVAL;
1158 		} else {
1159 			voice->blksize = blksize;
1160 		}
1161 
1162 		break;
1163 	}
1164 
1165 	return error;
1166 }
1167 
1168 
1169 status_t
1170 emuxki_voice_commit_parms(emuxki_voice *voice)
1171 {
1172 	if (voice->use & EMU_USE_PLAY) {
1173 		emuxki_channel_commit_parms(voice->dataloc.chan[0]);
1174 		if (voice->stereo)
1175 			emuxki_channel_commit_parms(voice->dataloc.chan[1]);
1176 	} else {
1177 		uint32 buffaddr_reg, buffsize_reg, idx_reg;
1178 		switch (voice->dataloc.source) {
1179 			case EMU_RECSRC_MIC:
1180 				buffaddr_reg = EMU_MICBA;
1181 				buffsize_reg = EMU_MICBS;
1182 				idx_reg = EMU_MICIDX;
1183 				break;
1184 			case EMU_RECSRC_ADC:
1185 				buffaddr_reg = EMU_ADCBA;
1186 				buffsize_reg = EMU_ADCBS;
1187 				idx_reg = EMU_ADCIDX;
1188 				break;
1189 			case EMU_RECSRC_FX:
1190 				buffaddr_reg = EMU_FXBA;
1191 				buffsize_reg = EMU_FXBS;
1192 				idx_reg = EMU_FXIDX;
1193 				break;
1194 			default:
1195 				return B_ERROR;
1196 		}
1197 		emuxki_chan_write(&voice->stream->card->config, 0, buffaddr_reg, (uint32)voice->buffer->phy_base);
1198 		emuxki_chan_write(&voice->stream->card->config, 0, buffsize_reg, EMU_RECBS_BUFSIZE_NONE);
1199 		emuxki_chan_write(&voice->stream->card->config, 0, buffsize_reg, EMU_RECBS_BUFSIZE_4096);
1200 
1201 		LOG(("emuxki_voice_commit_parms idx_reg : %u\n", idx_reg));
1202 
1203 		idx_reg = EMU_RECIDX(idx_reg);
1204 		while (emuxki_chan_read(&voice->stream->card->config, 0, idx_reg))
1205 			snooze(5);
1206 	}
1207 	return B_OK;
1208 }
1209 
1210 
1211 static uint32
1212 emuxki_voice_curaddr(emuxki_voice *voice)
1213 {
1214 	if (voice->use & EMU_USE_PLAY)
1215 		return (emuxki_chan_read(&voice->stream->card->config,
1216 				     voice->dataloc.chan[0]->num,
1217 				     EMU_CHAN_CCCA_CURRADDR) -
1218 			voice->dataloc.chan[0]->loop.start);
1219 	else {
1220 		uint32 idx_reg = 0;
1221 		switch (voice->dataloc.source) {
1222 			case EMU_RECSRC_MIC:
1223 				idx_reg = IS_AUDIGY(&voice->stream->card->config) ? EMU_A_MICIDX : EMU_MICIDX;
1224 				break;
1225 			case EMU_RECSRC_ADC:
1226 				idx_reg = IS_AUDIGY(&voice->stream->card->config) ? EMU_A_ADCIDX : EMU_ADCIDX;
1227 				break;
1228 			case EMU_RECSRC_FX:
1229 				idx_reg = EMU_FXIDX;
1230 				break;
1231 			default:
1232 				TRACE(("emuxki_voice_curaddr : BUG!!\n"));
1233 		}
1234 		//TRACE(("emuxki_voice_curaddr idx_reg : %u\n", idx_reg));
1235 		//TRACE(("emuxki_voice_curaddr : %lu\n", emuxki_chan_read(&voice->stream->card->config, 0, idx_reg)));
1236 		return emuxki_chan_read(&voice->stream->card->config,
1237 				     0,
1238 				     idx_reg);
1239 	}
1240 }
1241 
1242 
1243 static void
1244 emuxki_resched_timer(emuxki_dev *card)
1245 {
1246 	emuxki_voice 	*voice;
1247 	emuxki_stream 	*stream;
1248 	uint16			timerate = 1024;
1249 	uint8			active = 0;
1250 	//int             s = splaudio();
1251 
1252 	LOG(("emuxki_resched_timer begin\n"));
1253 
1254 	LIST_FOREACH(stream, &card->streams, next) {
1255 		LIST_FOREACH(voice, &stream->voices, next) {
1256 			if ((voice->use & EMU_USE_PLAY) == 0 ||
1257 				(voice->state & EMU_STATE_STARTED) == 0)
1258 				continue;
1259 			active = 1;
1260 			if (voice->timerate < timerate)
1261 				timerate = voice->timerate;
1262 		}
1263 	}
1264 	if (timerate & ~EMU_TIMER_RATE_MASK)
1265 		timerate = 0;
1266 
1267 	if (card->timerate > timerate) {
1268 		LOG(("emuxki_resched_timer written (old %u, new %u)\n", card->timerate, timerate));
1269 		card->timerate = timerate;
1270 		emuxki_reg_write_16(&card->config, EMU_TIMER, timerate);
1271 	}
1272 	if (!active && (card->timerstate & EMU_TIMER_STATE_ENABLED)) {
1273 		emuxki_inte_disable(&card->config, EMU_INTE_INTERTIMERENB);
1274 		card->timerstate &= ~EMU_TIMER_STATE_ENABLED;
1275 		LOG(("emuxki_resched_timer : timer disabled\n"));
1276 	} else
1277 	  if (active && !(card->timerstate & EMU_TIMER_STATE_ENABLED)) {
1278 	  	emuxki_inte_enable(&card->config, EMU_INTE_INTERTIMERENB);
1279 		card->timerstate |= EMU_TIMER_STATE_ENABLED;
1280 		LOG(("emuxki_resched_timer : timer enabled\n"));
1281 	  }
1282 	LOG(("emuxki_resched_timer state : %x\n", card->timerstate));
1283 	//splx(s);
1284 }
1285 
1286 
1287 static uint32
1288 emuxki_voice_adc_rate(emuxki_voice *voice)
1289 {
1290 	switch(voice->sample_rate) {
1291 		case 48000:
1292 			return EMU_ADCCR_SAMPLERATE_48;
1293 			break;
1294 		case 44100:
1295 			return EMU_ADCCR_SAMPLERATE_44;
1296 			break;
1297 		case 32000:
1298 			return EMU_ADCCR_SAMPLERATE_32;
1299 			break;
1300 		case 24000:
1301 			return EMU_ADCCR_SAMPLERATE_24;
1302 			break;
1303 		case 22050:
1304 			return EMU_ADCCR_SAMPLERATE_22;
1305 			break;
1306 		case 16000:
1307 			return EMU_ADCCR_SAMPLERATE_16;
1308 			break;
1309 		case 12000:
1310 			if (IS_AUDIGY(&voice->stream->card->config))
1311 				return EMU_A_ADCCR_SAMPLERATE_12;
1312 			else
1313 				PRINT(("recording sample_rate not supported : %lu\n", voice->sample_rate));
1314 			break;
1315 		case 11000:
1316 			if (IS_AUDIGY(&voice->stream->card->config))
1317 				return EMU_A_ADCCR_SAMPLERATE_11;
1318 			else
1319 				return EMU_ADCCR_SAMPLERATE_11;
1320 			break;
1321 		case 8000:
1322 			if (IS_AUDIGY(&voice->stream->card->config))
1323 				return EMU_A_ADCCR_SAMPLERATE_8;
1324 			else
1325 				return EMU_ADCCR_SAMPLERATE_8;
1326 			break;
1327 		default:
1328 			PRINT(("recording sample_rate not supported : %lu\n", voice->sample_rate));
1329 	}
1330 	return 0;
1331 }
1332 
1333 
1334 void
1335 emuxki_voice_start(emuxki_voice *voice)
1336 {
1337 	LOG(("emuxki_voice_start\n"));
1338 
1339 	if (voice->use & EMU_USE_PLAY) {
1340 		voice->trigblk = 1;
1341 		emuxki_channel_start(voice->dataloc.chan[0]);
1342 		if (voice->stereo)
1343 			emuxki_channel_start(voice->dataloc.chan[1]);
1344 	} else {
1345 
1346 		switch (voice->dataloc.source) {
1347 			case EMU_RECSRC_MIC:
1348 				emuxki_inte_enable(&voice->stream->card->config, EMU_INTE_MICBUFENABLE);
1349 				break;
1350 			case EMU_RECSRC_ADC: {
1351 				uint32 adccr_value = 0;
1352 				adccr_value = emuxki_voice_adc_rate(voice);
1353 				LOG(("emuxki_voice_start adccr_value : %u\n", adccr_value));
1354 				if (voice->stereo)
1355 					adccr_value |= ( (IS_AUDIGY(&voice->stream->card->config) ? EMU_A_ADCCR_LCHANENABLE : EMU_ADCCR_LCHANENABLE )
1356 						| ( IS_AUDIGY(&voice->stream->card->config) ? EMU_A_ADCCR_RCHANENABLE : EMU_ADCCR_RCHANENABLE ));
1357 				else
1358 					adccr_value |= IS_AUDIGY(&voice->stream->card->config) ? EMU_A_ADCCR_LCHANENABLE : EMU_ADCCR_LCHANENABLE;
1359 
1360 				LOG(("emuxki_voice_start adccr_value : %u, %u\n", adccr_value, EMU_ADCCR_LCHANENABLE | EMU_ADCCR_RCHANENABLE));
1361 				emuxki_chan_write(&voice->stream->card->config, 0, EMU_ADCCR, adccr_value);
1362 
1363 				emuxki_inte_enable(&voice->stream->card->config, EMU_INTE_ADCBUFENABLE);
1364 				}
1365 				break;
1366 			case EMU_RECSRC_FX:
1367 				if (IS_AUDIGY(&voice->stream->card->config)) {
1368 					emuxki_chan_write(&voice->stream->card->config, 0, EMU_A_FXWC1,
1369 						voice->recparams.efx_voices[0]);
1370 					emuxki_chan_write(&voice->stream->card->config, 0, EMU_A_FXWC2,
1371 						voice->recparams.efx_voices[1]);
1372 				} else {
1373 					emuxki_chan_write(&voice->stream->card->config, 0, EMU_FXWC,
1374 						voice->recparams.efx_voices[0]);
1375 				}
1376 				emuxki_inte_enable(&voice->stream->card->config, EMU_INTE_EFXBUFENABLE);
1377 				break;
1378 			default:
1379 				PRINT(("emuxki_voice_start BUG\n"));
1380 		}
1381 	}
1382 	voice->state |= EMU_STATE_STARTED;
1383 	if (voice->use & EMU_USE_PLAY) {
1384 		emuxki_resched_timer(voice->stream->card);
1385 	}
1386 }
1387 
1388 
1389 void
1390 emuxki_voice_halt(emuxki_voice *voice)
1391 {
1392 	LOG(("emuxki_voice_halt\n"));
1393 
1394 	if (voice->use & EMU_USE_PLAY) {
1395 		emuxki_channel_stop(voice->dataloc.chan[0]);
1396 		if (voice->stereo)
1397 			emuxki_channel_stop(voice->dataloc.chan[1]);
1398 	} else {
1399 
1400 		switch (voice->dataloc.source) {
1401 			case EMU_RECSRC_MIC:
1402 				emuxki_inte_disable(&voice->stream->card->config, EMU_INTE_MICBUFENABLE);
1403 				break;
1404 			case EMU_RECSRC_ADC:
1405 				emuxki_chan_write(&voice->stream->card->config, 0, EMU_ADCCR, 0);
1406 				emuxki_inte_disable(&voice->stream->card->config, EMU_INTE_ADCBUFENABLE);
1407 				break;
1408 			case EMU_RECSRC_FX:
1409 				if (IS_AUDIGY(&voice->stream->card->config)) {
1410 					emuxki_chan_write(&voice->stream->card->config, 0, EMU_A_FXWC1, 0);
1411 					emuxki_chan_write(&voice->stream->card->config, 0, EMU_A_FXWC2, 0);
1412 				} else
1413 					emuxki_chan_write(&voice->stream->card->config, 0, EMU_FXWC, 0);
1414 				emuxki_inte_disable(&voice->stream->card->config, EMU_INTE_EFXBUFENABLE);
1415 				break;
1416 			default:
1417 				PRINT(("emuxki_voice_halt BUG\n"));
1418 		}
1419 	}
1420 	voice->state &= ~EMU_STATE_STARTED;
1421 	if (voice->use & EMU_USE_PLAY) {
1422 		emuxki_resched_timer(voice->stream->card);
1423 	}
1424 }
1425 
1426 
1427 emuxki_voice *
1428 emuxki_voice_new(emuxki_stream *stream, uint8 use, uint8 voicenum)
1429 {
1430 	emuxki_voice *voice;
1431 	//int             s;
1432 
1433 	LOG(("emuxki_voice_new\n"));
1434 
1435 	voice = malloc(sizeof(emuxki_voice));
1436 	if (voice == NULL)
1437 		return (NULL);
1438 	voice->stream = stream;
1439 	voice->use = use;
1440 	voice->state = !EMU_STATE_STARTED;
1441 	voice->stereo = EMU_STEREO_NOTSET;
1442 	voice->b16 = 0;
1443 	voice->sample_rate = 0;
1444 	if (use & EMU_USE_PLAY)
1445 		voice->dataloc.chan[0] = voice->dataloc.chan[1] = NULL;
1446 	else
1447 		voice->dataloc.source = EMU_RECSRC_NOTSET;
1448 	voice->buffer = NULL;
1449 	voice->blksize = 0;
1450 	voice->trigblk = 0;
1451 	voice->blkmod = 0;
1452 	voice->timerate = 0;
1453 	voice->voicenum = voicenum;
1454 	return voice;
1455 }
1456 
1457 
1458 void
1459 emuxki_voice_delete(emuxki_voice *voice)
1460 {
1461 	if (voice->state & EMU_STATE_STARTED)
1462 		emuxki_voice_halt(voice);
1463 	emuxki_voice_dataloc_destroy(voice);
1464 	free(voice);
1465 }
1466 
1467 /*	Emuxki stream functions */
1468 
1469 status_t
1470 emuxki_stream_set_audioparms(emuxki_stream *stream, bool stereo, uint8 channels,
1471      uint8 b16, uint32 sample_rate)
1472 {
1473 	status_t 		error;
1474 	emuxki_voice 	*voice;
1475 	uint8			i, nvoices;
1476 	char 			*buffer;
1477 	uint8 			sample_size, frame_size;
1478 	LOG(("emuxki_stream_set_audioparms\n"));
1479 
1480 	if (stream->stereo == stereo &&
1481 		((stream->nmono + 2*stream->nstereo) == channels) &&
1482 		(stream->b16 == b16) &&
1483 		(stream->sample_rate == sample_rate))
1484 		return B_OK;
1485 
1486 	LIST_FOREACH(voice, &stream->voices, next) {
1487 		if (voice->buffer)
1488 			emuxki_mem_free(stream->card, voice->buffer->log_base);
1489 		emuxki_voice_delete(voice);
1490 	}
1491 	stream->first_voice = NULL;
1492 	LIST_INIT(&(stream->voices));
1493 
1494 	stream->b16 = b16;
1495 	stream->sample_rate = sample_rate;
1496 
1497 	if (stereo && (channels % 2 == 0)) {
1498 		stream->stereo = true;
1499 		stream->nstereo = channels / 2;
1500 		stream->nmono = 0;
1501 		nvoices = stream->nstereo;
1502 	} else {
1503 		stream->stereo = false;
1504 		stream->nstereo = 0;
1505 		stream->nmono = channels;
1506 		nvoices = stream->nmono;
1507 	}
1508 
1509 	sample_size = stream->b16 + 1;
1510 	frame_size = sample_size * (stream->stereo ? 2 : 1);
1511 
1512 	for (i = 0; i < nvoices; i++) {
1513 		voice = emuxki_voice_new(stream, stream->use, i);
1514 		if (voice) {
1515 			if (!stream->first_voice)
1516 				stream->first_voice = voice;
1517 			LIST_INSERT_HEAD((&stream->voices), voice, next);
1518 			if ((error = emuxki_voice_set_audioparms(voice, stream->stereo,
1519 				stream->b16, stream->sample_rate)))
1520 				return error;
1521 
1522 			if (stream->use & EMU_USE_PLAY)
1523 				buffer = emuxki_pmem_alloc(stream->card, stream->bufframes
1524 					* frame_size * stream->bufcount);
1525 			else
1526 				buffer = emuxki_rmem_alloc(stream->card, stream->bufframes
1527 					* frame_size * stream->bufcount);
1528 
1529 			emuxki_voice_set_bufparms(voice, buffer,
1530 				stream->bufframes * frame_size * stream->bufcount,
1531 				stream->bufframes * frame_size);
1532 		}
1533 	}
1534 
1535 	return B_OK;
1536 }
1537 
1538 
1539 status_t
1540 emuxki_stream_set_recparms(emuxki_stream *stream, emuxki_recsrc_t recsrc,
1541 			     	emuxki_recparams *recparams)
1542 {
1543 	emuxki_voice *voice;
1544 	LOG(("emuxki_stream_set_recparms\n"));
1545 
1546 	if (stream->use & EMU_USE_RECORD) {
1547 		switch(recsrc) {
1548 			case EMU_RECSRC_MIC:
1549 				break;
1550 			case EMU_RECSRC_ADC:
1551 				break;
1552 			case EMU_RECSRC_FX:
1553 				if (!recparams)
1554 					return B_ERROR;
1555 				LIST_FOREACH(voice, &stream->voices, next) {
1556 					voice->recparams.efx_voices[0] = recparams->efx_voices[0];
1557 					voice->recparams.efx_voices[1] = recparams->efx_voices[1];
1558 				}
1559 				break;
1560 			default:
1561 				return B_ERROR;
1562 				break;
1563 		}
1564 		LIST_FOREACH(voice, &stream->voices, next)
1565 			voice->dataloc.source = recsrc;
1566 	}
1567 	return B_OK;
1568 }
1569 
1570 
1571 status_t
1572 emuxki_stream_commit_parms(emuxki_stream *stream)
1573 {
1574 	emuxki_voice 	*voice;
1575 	status_t 		error;
1576 	LOG(("emuxki_stream_commit_parms\n"));
1577 
1578 	LIST_FOREACH(voice, &stream->voices, next)
1579 		if ((error = emuxki_voice_commit_parms(voice)))
1580 			return error;
1581 
1582 	return B_OK;
1583 }
1584 
1585 
1586 status_t
1587 emuxki_stream_get_nth_buffer(emuxki_stream *stream, uint8 chan, uint8 buf,
1588 					char** buffer, size_t *stride)
1589 {
1590 	emuxki_voice *voice = NULL;
1591 	uint8 i, sample_size;
1592 	LOG(("emuxki_stream_get_nth_buffer\n"));
1593 
1594 	sample_size = stream->b16 + 1;
1595 	if (buf >= stream->bufcount)
1596 		return B_BAD_INDEX;
1597 
1598 	if (stream->stereo) {
1599 		i = stream->nstereo - 1;
1600 		if (chan/2 > i)
1601 			return B_BAD_INDEX;
1602 		LIST_FOREACH(voice, &stream->voices, next)
1603 			if (i != chan/2)
1604 				i--;
1605 			else
1606 				break;
1607 		if (voice) {
1608 			*buffer = (char*)voice->buffer->log_base
1609 				+ (buf * stream->bufframes * sample_size * 2);
1610 			if (chan % 2 == 1)
1611 				*buffer += sample_size;
1612 			*stride = sample_size * 2;
1613 		} else
1614 			return B_ERROR;
1615 	} else {
1616 		i = stream->nmono - 1;
1617 		if (chan > i)
1618 			return B_BAD_INDEX;
1619 		LIST_FOREACH(voice, &stream->voices, next)
1620 			if (i != chan)
1621 				i--;
1622 			else
1623 				break;
1624 		if (voice) {
1625 			*buffer = (char*)voice->buffer->log_base
1626 				+ (buf * stream->bufframes * sample_size);
1627 			*stride = sample_size;
1628 		} else
1629 			return B_ERROR;
1630 	}
1631 
1632 	return B_OK;
1633 }
1634 
1635 
1636 void
1637 emuxki_stream_start(emuxki_stream *stream, void (*inth) (void *), void *inthparam)
1638 {
1639 	emuxki_voice *voice;
1640 	LOG(("emuxki_stream_start\n"));
1641 
1642 	stream->inth = inth;
1643 	stream->inthparam = inthparam;
1644 
1645 	LIST_FOREACH(voice, &stream->voices, next) {
1646 		emuxki_voice_start(voice);
1647 	}
1648 	stream->state |= EMU_STATE_STARTED;
1649 }
1650 
1651 
1652 void
1653 emuxki_stream_halt(emuxki_stream *stream)
1654 {
1655 	emuxki_voice *voice;
1656 	LOG(("emuxki_stream_halt\n"));
1657 
1658 	LIST_FOREACH(voice, &stream->voices, next) {
1659 		emuxki_voice_halt(voice);
1660 	}
1661 	stream->state &= ~EMU_STATE_STARTED;
1662 }
1663 
1664 
1665 emuxki_stream *
1666 emuxki_stream_new(emuxki_dev *card, uint8 use, uint32 bufframes, uint8 bufcount)
1667 {
1668 	emuxki_stream *stream;
1669 	cpu_status status;
1670 	LOG(("emuxki_stream_new\n"));
1671 
1672 	stream = malloc(sizeof(emuxki_stream));
1673 	if (stream == NULL)
1674 		return (NULL);
1675 	stream->card = card;
1676 	stream->use = use;
1677 	stream->state = !EMU_STATE_STARTED;
1678 	stream->stereo = EMU_STEREO_NOTSET;
1679 	stream->b16 = 0;
1680 	stream->sample_rate = 0;
1681 	stream->nmono = 0;
1682 	stream->nstereo = 0;
1683 	stream->bufframes = bufframes;
1684 	stream->bufcount = bufcount;
1685 	stream->first_voice = NULL;
1686 	stream->inth = NULL;
1687 	stream->inthparam = NULL;
1688 
1689 	stream->frames_count = 0;
1690 	stream->real_time = 0;
1691 	stream->buffer_cycle = 0;
1692 	stream->update_needed = false;
1693 
1694 	/* Init voices list */
1695 	LIST_INIT(&(stream->voices));
1696 
1697 	status = lock();
1698 	LIST_INSERT_HEAD((&card->streams), stream, next);
1699 	unlock(status);
1700 
1701 	return stream;
1702 }
1703 
1704 
1705 void
1706 emuxki_stream_delete(emuxki_stream *stream)
1707 {
1708 	emuxki_voice *voice;
1709 	cpu_status status;
1710 	LOG(("emuxki_stream_delete\n"));
1711 
1712 	emuxki_stream_halt(stream);
1713 
1714 	status = lock();
1715 	LIST_REMOVE(stream, next);
1716 	unlock(status);
1717 
1718 	while (!LIST_EMPTY(&stream->voices)) {
1719 		voice = LIST_FIRST(&stream->voices);
1720 		LIST_REMOVE(voice, next);
1721 		if (voice->buffer)
1722 			emuxki_mem_free(stream->card, voice->buffer->log_base);
1723 		emuxki_voice_delete(voice);
1724 	}
1725 
1726 	free(stream);
1727 }
1728 
1729 
1730 /* Emuxki gprs */
1731 // 87 values from 0.0dB to -xdB (-0.75dB each)
1732 static uint32 db_table[] = {
1733 	2147483647,	1969835071,	1806882308,	1657409659,	1520301995,	1394536435,	1279174712,
1734 	1173356181,	1076291388,	987256190,	905586345,	830672562,	761955951,	698923858,
1735 	641106035,	588071138,	539423503,	494800198,	453868315,	416322483,	381882595,
1736 	350291714,	321314160,	294733747,	270352173,	247987542,	227473005,	208655513,
1737 	191394681,	175561735,	161038555,	147716791,	135497057,	124288190,	114006566,
1738 	104575479,	95924570,	87989300,	80710468,	74033770,	67909395,	62291654,
1739 	57138635,	52411895,	48076170,	44099114,	40451056,	37104780,	34035322,
1740 	31219781,	28637154,	26268172,	24095162,	22101913,	20273552,	18596442,
1741 	17058068,	15646956,	14352576,	13165272,	12076188,	11077196,	10160845,
1742 	9320299,	8549286,	7842054,	7193328,	6598266,	6052431,	5551749,
1743 	5092486,	4671215,	4284793,	3930337,	3605204,	3306967,	3033401,
1744 	2782465,	2552289,	2341153,	2147483,	1969835,	1806882,	1657409,
1745 	1520301,	1394536,	1279174
1746 };
1747 
1748 
1749 void
1750 emuxki_gpr_set(emuxki_dev *card, emuxki_gpr *gpr, int32 type, float *values)
1751 {
1752 	uint8 count = gpr->type & EMU_MIX_STEREO ? 2 : 1;
1753 	uint8 i;
1754 	uint32 index;
1755 
1756 	LOG(("emuxki_set_gpr\n"));
1757 
1758 	switch(type) {
1759 		case EMU_MIX_MUTE:
1760 			gpr->mute = (values[0] == 1.0);
1761 			if (gpr->mute) {
1762 				for (i = 0; i < count; i++)
1763 					emuxki_write_gpr(&card->config, gpr->gpr + i, 0);
1764 				break;
1765 			}
1766 			for (i = 0; i < count; i++) {
1767 				values[i] = gpr->current[i];
1768 			}
1769 		case EMU_MIX_GAIN:
1770 			for (i = 0; i < count; i++) {
1771 				if (values[i]>gpr->max_gain || values[i]<gpr->min_gain)
1772 					return;
1773 				index = (int32)(values[i] / gpr->granularity);
1774 				if (index > sizeof(db_table)/sizeof(db_table[0]))
1775 					index = sizeof(db_table)/sizeof(db_table[0]);
1776 				else if (index < 0)
1777 					index = 0;
1778 				LOG(("emuxki_set_gpr gpr: %d \n", gpr->gpr + i));
1779 				LOG(("emuxki_set_gpr values[i]: %g \n", values[i]));
1780 				LOG(("emuxki_set_gpr index: %u \n", index));
1781 				if (!gpr->mute)
1782 					emuxki_write_gpr(&card->config, gpr->gpr + i, db_table[index]);
1783 				gpr->current[i] = index * gpr->granularity;
1784 			}
1785 			break;
1786 	}
1787 }
1788 
1789 
1790 void
1791 emuxki_gpr_get(emuxki_dev *card, emuxki_gpr *gpr, int32 type, float *values)
1792 {
1793 	uint8 count = gpr->type & EMU_MIX_STEREO ? 2 : 1;
1794 	uint16 i;
1795 
1796 	LOG(("emuxki_get_gpr\n"));
1797 
1798 	switch(type) {
1799 		case EMU_MIX_GAIN:
1800 			for (i = 0; i < count; i++) {
1801 				values[i] = gpr->current[i];
1802 			}
1803 			break;
1804 		case EMU_MIX_MUTE:
1805 			values[0] = (gpr->mute ? 1.0 : 0.0);
1806 			break;
1807 	}
1808 }
1809 
1810 
1811 void
1812 emuxki_gpr_dump(emuxki_dev * card, uint16 count)
1813 {
1814 	uint16      pc;
1815 	uint32		value;
1816 
1817 	LOG(("emuxki_dump_gprs\n"));
1818 
1819 	for (pc = 0; pc < count; pc++) {
1820 		value = emuxki_read_gpr(&card->config, pc);
1821 		LOG(("dsp_gpr pc=%x, value=%x\n", pc, value));
1822 	}
1823 }
1824 
1825 
1826 static emuxki_gpr *
1827 emuxki_gpr_new(emuxki_dev *card, const char *name, emuxki_gpr_type type, uint16 *gpr_num,
1828 			float default_value, float default_mute, float min_gain, float max_gain, float granularity)
1829 {
1830 	emuxki_gpr *gpr;
1831 	float	values[2];
1832 
1833 	LOG(("emuxki_gpr_new\n"));
1834 
1835 	gpr = &card->gpr[*gpr_num];
1836 	strncpy(gpr->name, name, 32);
1837 	gpr->type = type;
1838 	gpr->gpr = *gpr_num;
1839 	gpr->default_value = default_value;
1840 	gpr->min_gain = min_gain;
1841 	gpr->max_gain = max_gain;
1842 	gpr->granularity = granularity;
1843 	gpr->mute = false;
1844 	(*gpr_num)++;
1845 	if (gpr->type & EMU_MIX_STEREO)
1846 		(*gpr_num)++;
1847 
1848 	if (default_mute == 1.0) {
1849 		values[0] = default_mute;
1850 		emuxki_gpr_set(card, gpr, EMU_MIX_MUTE, values);
1851 	}
1852 
1853 	values[0] = gpr->default_value;
1854 	if (gpr->type & EMU_MIX_STEREO)
1855 		values[1] = gpr->default_value;
1856 	emuxki_gpr_set(card, gpr, EMU_MIX_GAIN, values);
1857 
1858 
1859 	return gpr;
1860 }
1861 
1862 /* Emuxki parameter */
1863 
1864 void
1865 emuxki_parameter_set(emuxki_dev *card, const void* cookie, int32 type, int32 *value)
1866 {
1867 	emuxki_stream *stream;
1868 	emuxki_voice *voice;
1869 	LOG(("emuxki_parameter_set\n"));
1870 
1871 	switch(type) {
1872 		case EMU_DIGITAL_MODE:
1873 			card->digital_enabled = *value == 1;
1874 			if (IS_AUDIGY(&card->config))
1875 				if (IS_AUDIGY2(&card->config)) {
1876 					// this disables analog, not enough
1877 					emuxki_reg_write_32(&card->config, EMU_A_IOCFG,
1878 						(card->digital_enabled ? 0 : EMU_A_IOCFG_GPOUT0) |
1879 							(emuxki_reg_read_32(&card->config, EMU_A_IOCFG)
1880 							& ~(EMU_A_IOCFG_GPOUT0 | EMU_A_IOCFG_GPOUT1) ) );
1881 				} else {
1882 					// this disables analog, and enables digital
1883 					emuxki_reg_write_32(&card->config, EMU_A_IOCFG,
1884 						(card->digital_enabled ? EMU_A_IOCFG_GPOUT0 | EMU_A_IOCFG_GPOUT1 : 0) |
1885 							(emuxki_reg_read_32(&card->config, EMU_A_IOCFG)
1886 							& ~(EMU_A_IOCFG_GPOUT0 | EMU_A_IOCFG_GPOUT1) ) );
1887 				}
1888 			else {
1889 				// this enables digital, not enough
1890 				emuxki_reg_write_32(&card->config, EMU_HCFG,
1891 					(card->digital_enabled ? EMU_HCFG_GPOUTPUT0 : 0) |
1892 					(emuxki_reg_read_32(&card->config, EMU_HCFG) & ~EMU_HCFG_GPOUTPUT0));
1893 			}
1894 
1895 			break;
1896 		case EMU_AUDIO_MODE:
1897 			if (*value!=0 && *value!=1 && *value!=2) {
1898 				PRINT(("emuxki_parameter_set error value unexpected\n"));
1899 				return;
1900 			}
1901 			card->play_mode = (*value + 1) * 2;
1902 			LIST_FOREACH(stream, &card->streams, next) {
1903 				if ((stream->use & EMU_USE_PLAY) == 0 ||
1904 					(stream->state & EMU_STATE_STARTED) == 0)
1905 						continue;
1906 				LIST_FOREACH(voice, &stream->voices, next) {
1907 					emuxki_voice_fxupdate(voice);
1908 					emuxki_channel_commit_fx(voice->dataloc.chan[0]);
1909 					if (voice->stereo)
1910 						emuxki_channel_commit_fx(voice->dataloc.chan[1]);
1911 				}
1912 			}
1913 			break;
1914 	}
1915 }
1916 
1917 
1918 void
1919 emuxki_parameter_get(emuxki_dev *card, const void* cookie, int32 type, int32 *value)
1920 {
1921 	LOG(("emuxki_parameter_get\n"));
1922 
1923 	switch(type) {
1924 		case EMU_DIGITAL_MODE:
1925 			*value = card->digital_enabled ? 1 : 0;
1926 			break;
1927 		case EMU_AUDIO_MODE:
1928 			*value = card->play_mode / 2 - 1;
1929 			break;
1930 	}
1931 }
1932 
1933 
1934 /* Emuxki interrupt */
1935 static int32
1936 emuxki_int(void *arg)
1937 {
1938 	emuxki_dev	 *card = arg;
1939 	uint32       ipr, curblk;
1940 	bool gotone = false;
1941 	emuxki_voice *voice;
1942 	emuxki_stream *stream;
1943 
1944 	while ((ipr = emuxki_reg_read_32(&card->config, EMU_IPR))) {
1945 		gotone = true;
1946 		if (ipr & EMU_IPR_INTERVALTIMER) {
1947 			//TRACE(("EMU_IPR_INTERVALTIMER\n"));
1948 			LIST_FOREACH(stream, &card->streams, next) {
1949 				if ((stream->use & EMU_USE_PLAY) == 0 ||
1950 					(stream->state & EMU_STATE_STARTED) == 0 ||
1951 					(stream->inth == NULL))
1952 						continue;
1953 
1954 				voice = stream->first_voice;
1955 				//TRACE(("voice %p\n", voice));
1956 				curblk = emuxki_voice_curaddr(voice) /
1957 				       voice->blksize;
1958 				//TRACE(("EMU_IPR_INTERVALTIMER at trigblk %lu\n", curblk));
1959 				//TRACE(("EMU_IPR_INTERVALTIMER at voice->trigblk %lu\n", voice->trigblk));
1960 				if (curblk == voice->trigblk) {
1961 					//TRACE(("EMU_IPR_INTERVALTIMER at trigblk %lu\n", curblk));
1962 					//dump_voice(voice);
1963 					//trace_hardware_regs(&card->config);
1964 					//TRACE(("voice pointer %p\n", voice));
1965 
1966 					if (stream->inth)
1967 						stream->inth(stream->inthparam);
1968 
1969 					voice->trigblk++;
1970 					voice->trigblk %= voice->blkmod;
1971 				}
1972 			}
1973 		}
1974 #if MIDI
1975 		if (ipr & (EMU_IPR_MIDIRECVBUFE)) {
1976 			midi_interrupt(card);          /* Gameport */
1977 		}
1978 
1979 		if (ipr & (EMU_IPR_MIDITRANSBUFE)) {
1980 			if (!midi_interrupt(card)) {
1981 				emuxki_inte_disable(&card->config, EMU_INTE_MIDITXENABLE);
1982 				TRACE(("EMU_INTE_MIDITXENABLE disabled\n"));
1983 			}
1984 		}
1985 #endif
1986 		if (ipr & (EMU_IPR_ADCBUFHALFFULL | EMU_IPR_ADCBUFFULL
1987 			| EMU_IPR_MICBUFHALFFULL | EMU_IPR_MICBUFFULL
1988 			| EMU_IPR_EFXBUFHALFFULL | EMU_IPR_EFXBUFFULL)) {
1989 			//TRACE(("EMU_IPR_ADCBUF\n"));
1990 			LIST_FOREACH(stream, &card->streams, next) {
1991 				if ((stream->use & EMU_USE_RECORD) == 0 ||
1992 					(stream->state & EMU_STATE_STARTED) == 0 ||
1993 					(stream->inth == NULL) ||
1994 					(stream->first_voice == NULL))
1995 						continue;
1996 				voice = stream->first_voice;
1997 				curblk = emuxki_voice_curaddr(voice) /
1998 				       voice->blksize;
1999 				//TRACE(("EMU_IPR_ADCBUF at trigblk %lu\n", curblk));
2000 				//TRACE(("EMU_IPR_ADCBUF at voice->trigblk %lu\n", voice->trigblk));
2001 				if (curblk == voice->trigblk) {
2002 					//TRACE(("EMU_IPR_ADCBUF at trigblk %lu\n", curblk));
2003 					//dump_voice(voice);
2004 					//trace_hardware_regs(&card->config);
2005 
2006 					if (stream->inth)
2007 						stream->inth(stream->inthparam);
2008 
2009 					voice->trigblk++;
2010 					voice->trigblk %= voice->blkmod;
2011 				}
2012 			}
2013 		}
2014 
2015 		/*if (ipr & (EMU_IPR_CHANNELLOOP)) {
2016 			TRACE(("EMU_IPR_CHANNELLOOP pending channel : %u\n", ipr & EMU_IPR_CHNOMASK));
2017 			LIST_FOREACH(stream, &card->streams, next)
2018 			LIST_FOREACH(voice, &stream->voices, next) {
2019 				if ((voice->use & EMU_USE_PLAY) == 0 ||
2020 					(voice->state & EMU_STATE_STARTED) == 0)
2021 						continue;
2022 				TRACE(("EMU_IPR_CHANNELLOOP at trigblk %lu\n", emuxki_voice_curaddr(voice)));
2023 				TRACE(("EMU_IPR_CHANNELLOOP read %x\n", emuxki_chan_read(&voice->card->config, 0, EMU_CLIPL)));
2024 				emuxki_chan_write(&voice->card->config, 0, EMU_CLIPL, emuxki_chan_read(&voice->card->config, 0, EMU_CLIPL));
2025 			}
2026 		}*/
2027 
2028 		if (ipr & ~(EMU_IPR_RATETRCHANGE | EMU_IPR_INTERVALTIMER
2029 				| EMU_IPR_MIDITRANSBUFE | EMU_IPR_MIDIRECVBUFE
2030 				| EMU_IPR_ADCBUFHALFFULL | EMU_IPR_ADCBUFFULL
2031 				| EMU_IPR_MICBUFHALFFULL | EMU_IPR_MICBUFFULL
2032 				| EMU_IPR_EFXBUFHALFFULL | EMU_IPR_EFXBUFFULL))
2033 			TRACE(("Got interrupt 0x%08x !!!\n",
2034 			       ipr & ~(EMU_IPR_RATETRCHANGE |
2035 				       EMU_IPR_INTERVALTIMER)));
2036 
2037 		emuxki_reg_write_32(&card->config, EMU_IPR, ipr);
2038 	}
2039 
2040 	if (IS_AUDIGY2(&card->config)) {
2041 		while ((ipr = emuxki_reg_read_32(&card->config, EMU_A2_IPR2))) {
2042 			emuxki_reg_write_32(&card->config, EMU_A2_IPR2, ipr);
2043 			break;	// avoid loop
2044 		}
2045 
2046 		if (!IS_AUDIGY2_VALUE(&card->config)) {
2047 			while ((ipr = emuxki_reg_read_32(&card->config, EMU_A2_IPR3))) {
2048 				emuxki_reg_write_32(&card->config, EMU_A2_IPR3, ipr);
2049 				break; // avoid loop
2050 			}
2051 		}
2052 	}
2053 
2054 	if (gotone)
2055 		return B_INVOKE_SCHEDULER;
2056 
2057 	TRACE(("Got unhandled interrupt\n"));
2058 	return B_UNHANDLED_INTERRUPT;
2059 }
2060 
2061 
2062 
2063 /*	Emu10k1 driver functions */
2064 
2065 
2066 /* detect presence of our hardware */
2067 status_t
2068 init_hardware(void)
2069 {
2070 	int ix = 0;
2071 	pci_info info;
2072 //	uint32 buffer;
2073 	status_t err = ENODEV;
2074 
2075 	LOG_CREATE();
2076 
2077 	PRINT(("init_hardware()\n"));
2078 
2079 	if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci))
2080 		return ENOSYS;
2081 
2082 	while ((*pci->get_nth_pci_info)(ix, &info) == B_OK) {
2083 		if (info.vendor_id == CREATIVELABS_VENDOR_ID &&
2084 			(info.device_id == CREATIVELABS_SBLIVE_DEVICE_ID
2085 #if AUDIGY
2086 			|| info.device_id == CREATIVELABS_AUDIGY_DEVICE_ID
2087 			|| info.device_id == CREATIVELABS_AUDIGY2_VALUE_DEVICE_ID
2088 #endif
2089 			)) {
2090 			err = B_OK;
2091 
2092 /*
2093 			Joystick suport
2094 			if (!(info.u.h0.subsystem_id == 0x20 ||
2095 					info.u.h0.subsystem_id == 0xc400 ||
2096  					(info.u.h0.subsystem_id == 0x21 && info.revision < 6))) {
2097 	    		buffer = (*pci->read_io_32)(info.u.h0.base_registers[0] + HCFG);
2098 	    		buffer |= HCFG_JOYENABLE;
2099 	    		(*pci->write_io_32)(info.u.h0.base_registers[0] + HCFG, buffer);
2100 	   		}*/
2101 		}
2102 		ix++;
2103 	}
2104 
2105 	put_module(B_PCI_MODULE_NAME);
2106 
2107 	return err;
2108 }
2109 
2110 
2111 static void
2112 make_device_names(
2113 	emuxki_dev * card)
2114 {
2115 #if MIDI
2116 	sprintf(card->midi.name, "midi/emuxki/%ld", card-cards+1);
2117 	names[num_names++] = card->midi.name;
2118 #endif
2119 
2120 //	sprintf(card->joy.name1, "joystick/"DRIVER_NAME "/%x", card-cards+1);
2121 //	names[num_names++] = card->joy.name1;
2122 
2123 	sprintf(card->name, "audio/hmulti/emuxki/%ld", card-cards+1);
2124 	names[num_names++] = card->name;
2125 
2126 	names[num_names] = NULL;
2127 }
2128 
2129 
2130 static status_t
2131 emuxki_setup(emuxki_dev * card)
2132 {
2133 	status_t err = B_OK;
2134 	unsigned char cmd;
2135 	//int32 base;
2136 
2137 	PRINT(("setup_emuxki(%p)\n", card));
2138 
2139 	make_device_names(card);
2140 	card->config.nabmbar = card->info.u.h0.base_registers[0];
2141 	card->config.irq = card->info.u.h0.interrupt_line;
2142 	card->config.type = 0;
2143 	if (card->info.device_id == CREATIVELABS_AUDIGY_DEVICE_ID) {
2144 		card->config.type |= TYPE_AUDIGY;
2145 		if (card->info.revision == 4)
2146 			card->config.type |= TYPE_AUDIGY2;
2147 	} else if (card->info.device_id == CREATIVELABS_AUDIGY2_VALUE_DEVICE_ID)
2148 		card->config.type |= TYPE_AUDIGY | TYPE_AUDIGY2 | TYPE_AUDIGY2_VALUE;
2149 
2150 	PRINT(("%s deviceid = %#04x chiprev = %x model = %x enhanced at %lx\n", card->name, card->info.device_id,
2151 		card->info.revision, card->info.u.h0.subsystem_id, card->config.nabmbar));
2152 
2153 	cmd = (*pci->read_pci_config)(card->info.bus, card->info.device, card->info.function, PCI_command, 2);
2154 	PRINT(("PCI command before: %x\n", cmd));
2155 	(*pci->write_pci_config)(card->info.bus, card->info.device, card->info.function, PCI_command, 2, cmd | PCI_command_io);
2156 	cmd = (*pci->read_pci_config)(card->info.bus, card->info.device, card->info.function, PCI_command, 2);
2157 	PRINT(("PCI command after: %x\n", cmd));
2158 
2159 	dump_hardware_regs(&card->config);
2160 
2161 	emuxki_reg_write_32(&card->config, EMU_HCFG, EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK|
2162 		EMU_HCFG_MUTEBUTTONENABLE);
2163 
2164 	dump_hardware_regs(&card->config);
2165 
2166 #if MIDI
2167 	//SBLIVE : EMU_MUDATA, workaround 0, AUDIGY, AUDIGY2: 0, workaround 0x11020004
2168 	if ((err = (*mpu401->create_device)((card->config.nabmbar + !IS_AUDIGY(&card->config) ? EMU_MUDATA : 0),
2169 		&card->midi.driver, !IS_AUDIGY(&card->config) ? 0 : 0x11020004, midi_interrupt_op, &card->midi)) < B_OK)
2170 		return (err);
2171 
2172 	card->midi.card = card;
2173 #endif
2174 
2175 	// begin Joystick part
2176 /*	base = card->info.u.h0.base_registers[0];
2177 	(*pci->write_pci_config) (card->info.bus,card->info.device,
2178 			card->info.function, 0x10, 2, base);
2179 
2180 	if ((*gameport->create_device)(base, &card->joy.driver) < B_OK) {
2181 		dprintf("Audigy joystick - Error creating device\n");
2182 		(*gameport->delete_device)(card->joy.driver);
2183 	}*/
2184 	// end Joystick part
2185 
2186 	/* reset the codec */
2187 	PRINT(("codec reset\n"));
2188 	emuxki_codec_write(&card->config, 0x00, 0x0000);
2189 	snooze(50000); // 50 ms
2190 
2191 	ac97_init(&card->config);
2192 	ac97_amp_enable(&card->config, true);
2193 
2194 	PRINT(("codec vendor id      = %#08lx\n",ac97_get_vendor_id(&card->config)));
2195 	PRINT(("codec description     = %s\n",ac97_get_vendor_id_description(&card->config)));
2196 	PRINT(("codec 3d enhancement = %s\n",ac97_get_3d_stereo_enhancement(&card->config)));
2197 
2198 	if (IS_AUDIGY2(&card->config)) {
2199 		emuxki_reg_write_32(&card->config, EMU_A_IOCFG,
2200 			EMU_A_IOCFG_GPOUT0 | emuxki_reg_read_32(&card->config, EMU_A_IOCFG));
2201 	}
2202 
2203 	dump_hardware_regs(&card->config);
2204 
2205 	/*PRINT(("codec master output = %#04x\n",emuxki_codec_read(&card->config, 0x02)));
2206 	PRINT(("codec aux output    = %#04x\n",emuxki_codec_read(&card->config, 0x04)));
2207 	PRINT(("codec mono output   = %#04x\n",emuxki_codec_read(&card->config, 0x06)));
2208 	PRINT(("codec pcm output    = %#04x\n",emuxki_codec_read(&card->config, 0x18)));
2209 	PRINT(("codec line in	    = %#04x\n",emuxki_codec_read(&card->config, 0x10)));
2210 	PRINT(("codec record line in= %#04x\n",emuxki_codec_read(&card->config, 0x1a)));
2211 	PRINT(("codec record gain   = %#04x\n",emuxki_codec_read(&card->config, 0x1c)));*/
2212 
2213 	/*PRINT(("adc index	    = %#08x\n",emuxki_chan_read(&card->config, EMU_ADCIDX, 0)));
2214 	PRINT(("micro index 	= %#08x\n",emuxki_chan_read(&card->config, EMU_MICIDX, 0)));
2215 	PRINT(("fx index   		= %#08x\n",emuxki_chan_read(&card->config, EMU_FXIDX, 0)));
2216 	PRINT(("adc addr	    = %#08x\n",emuxki_chan_read(&card->config, EMU_ADCBA, 0)));
2217 	PRINT(("micro addr 		= %#08x\n",emuxki_chan_read(&card->config, EMU_MICBA, 0)));
2218 	PRINT(("fx addr   		= %#08x\n",emuxki_chan_read(&card->config, EMU_FXBA, 0)));
2219 	PRINT(("adc size	    = %#08x\n",emuxki_chan_read(&card->config, EMU_ADCBS, 0)));
2220 	PRINT(("micro size 		= %#08x\n",emuxki_chan_read(&card->config, EMU_MICBS, 0)));
2221 	PRINT(("fx size   		= %#08x\n",emuxki_chan_read(&card->config, EMU_FXBS, 0)));
2222 
2223 	PRINT(("EMU_ADCCR   		= %#08x\n",emuxki_chan_read(&card->config, EMU_ADCCR, 0)));
2224 	PRINT(("EMU_FXWC   		= %#08x\n",emuxki_chan_read(&card->config, EMU_FXWC, 0)));
2225 	PRINT(("EMU_FXWC   		= %#08x\n",emuxki_reg_read_32(&card->config, EMU_FXWC)));*/
2226 
2227 	PRINT(("writing codec registers\n"));
2228 	// TODO : to move with AC97
2229 	/* enable master output */
2230 	emuxki_codec_write(&card->config, AC97_MASTER_VOLUME, 0x0000);
2231 	/* enable aux output */
2232 	emuxki_codec_write(&card->config, AC97_AUX_OUT_VOLUME, 0x0000);
2233 	/* enable mono output */
2234 	//emuxki_codec_write(&card->config, AC97_MONO_VOLUME, 0x0004);
2235 	/* enable pcm output */
2236 	emuxki_codec_write(&card->config, AC97_PCM_OUT_VOLUME, 0x0808);
2237 	/* enable line in */
2238 	//emuxki_codec_write(&card->config, AC97_LINE_IN_VOLUME, 0x8808);
2239 	/* set record line in */
2240 	emuxki_codec_write(&card->config, AC97_RECORD_SELECT, 0x0404);
2241 	/* set record gain */
2242 	//emuxki_codec_write(&card->config, AC97_RECORD_GAIN, 0x0000);
2243 
2244 	PRINT(("codec master output = %#04x\n",emuxki_codec_read(&card->config, AC97_MASTER_VOLUME)));
2245 	PRINT(("codec aux output    = %#04x\n",emuxki_codec_read(&card->config, AC97_AUX_OUT_VOLUME)));
2246 	PRINT(("codec mono output   = %#04x\n",emuxki_codec_read(&card->config, AC97_MONO_VOLUME)));
2247 	PRINT(("codec pcm output    = %#04x\n",emuxki_codec_read(&card->config, AC97_PCM_OUT_VOLUME)));
2248 	PRINT(("codec line in	    = %#04x\n",emuxki_codec_read(&card->config, AC97_LINE_IN_VOLUME)));
2249 	PRINT(("codec record line in= %#04x\n",emuxki_codec_read(&card->config, AC97_RECORD_SELECT)));
2250 	PRINT(("codec record gain   = %#04x\n",emuxki_codec_read(&card->config, AC97_RECORD_GAIN)));
2251 
2252 	if (emuxki_codec_read(&card->config, AC97_EXTENDED_AUDIO_ID) & 0x0080) {
2253 		card->config.type |= TYPE_LIVE_5_1;
2254 		emuxki_chan_write(&card->config, 0, EMU_AC97SLOT, EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE);
2255 		emuxki_codec_write(&card->config, AC97_SURROUND_VOLUME, 0x0000);
2256 	}
2257 
2258 	if ((err = emuxki_init(card)))
2259 		return (err);
2260 
2261 	if (IS_AUDIGY(&card->config) || IS_LIVE_5_1(&card->config)) {
2262 		card->play_mode = 6;	// mode 5.1
2263 	} else {
2264 		card->play_mode = 4;	// mode 4.0
2265 	}
2266 
2267 	emuxki_reg_write_32(&card->config, EMU_INTE, EMU_INTE_SAMPLERATER | EMU_INTE_PCIERRENABLE);
2268 	if (IS_AUDIGY2(&card->config)) {
2269 		emuxki_reg_write_32(&card->config, EMU_A2_INTE2, 0);
2270 		if (!IS_AUDIGY2_VALUE(&card->config)) {
2271 			emuxki_reg_write_32(&card->config, EMU_A2_INTE3, 0);
2272 		}
2273 	}
2274 
2275 	PRINT(("installing interrupt : %lx\n", card->config.irq));
2276 	err = install_io_interrupt_handler(card->config.irq, emuxki_int, card, 0);
2277 	if (err != B_OK) {
2278 		PRINT(("failed to install interrupt\n"));
2279 		emuxki_shutdown(card);
2280 		return err;
2281 	}
2282 
2283 	emuxki_inte_enable(&card->config, EMU_INTE_VOLINCRENABLE | EMU_INTE_VOLDECRENABLE
2284 		| EMU_INTE_MUTEENABLE | EMU_INTE_FXDSPENABLE);
2285 	if (IS_AUDIGY2(&card->config)) {
2286 		emuxki_reg_write_32(&card->config, EMU_HCFG, EMU_HCFG_AUDIOENABLE |
2287 			EMU_HCFG_AC3ENABLE_CDSPDIF | EMU_HCFG_AC3ENABLE_GPSPDIF|
2288 			EMU_HCFG_JOYENABLE | EMU_HCFG_AUTOMUTE);
2289 	} else if (IS_AUDIGY(&card->config)) {
2290 		emuxki_reg_write_32(&card->config, EMU_HCFG, EMU_HCFG_AUDIOENABLE |
2291 			EMU_HCFG_JOYENABLE | EMU_HCFG_AUTOMUTE);
2292 	} else {
2293 		emuxki_reg_write_32(&card->config, EMU_HCFG, EMU_HCFG_AUDIOENABLE |
2294 			EMU_HCFG_LOCKTANKCACHE_MASK | EMU_HCFG_JOYENABLE | EMU_HCFG_AUTOMUTE);
2295 	}
2296 
2297 	PRINT(("setup_emuxki done\n"));
2298 
2299 	return err;
2300 }
2301 
2302 
2303 void
2304 emuxki_dump_fx(emuxki_dev * card)
2305 {
2306 	uint16      pc = 0;
2307 	uint8		op;
2308 	uint16		r,a,x,y,zero;
2309 
2310 	LOG(("emuxki_dump_fx\n"));
2311 
2312 	zero = IS_AUDIGY(&card->config) ? EMU_A_DSP_CST(0) : EMU_DSP_CST(0);
2313 
2314 	while (pc < 512) {
2315 		emuxki_dsp_getop(&card->config, &pc, &op, &r, &a, &x, &y);
2316 		if (op!=EMU_DSP_OP_ACC3 || r!=zero || a!=zero || x!=zero || y!=zero) {
2317 			LOG(("dsp_op pc=%u, op=%x, r=%x, a=%x, x=%x, y=%x\n",
2318 				pc, op, r, a, x, y));
2319 		}
2320 	}
2321 }
2322 
2323 
2324 static void
2325 emuxki_initfx(emuxki_dev * card)
2326 {
2327 	uint16       pc, gpr;
2328 	emuxki_gpr *a_front_gpr, *a_rear_gpr, *a_center_sub_gpr = NULL;
2329 	emuxki_gpr *p_ac97_in_gpr, *p_cd_in_gpr, *r_ac97_in_gpr, *r_cd_in_gpr, *r_fx_out_gpr;
2330 	emuxki_gpr *d_front_gpr, *d_rear_gpr, *d_center_sub_gpr;
2331 
2332 	/* Set all GPRs to 0 */
2333 	for (pc = 0; pc < 256; pc++) {
2334 		emuxki_write_gpr(&card->config, pc, 0);
2335 		card->gpr[pc].gpr = -1;
2336 	}
2337 
2338 	for (pc = 0; pc < 160; pc++) {
2339 		emuxki_chan_write(&card->config, 0, EMU_TANKMEMDATAREGBASE + pc, 0);
2340 		emuxki_chan_write(&card->config, 0, EMU_TANKMEMADDRREGBASE + pc, 0);
2341 	}
2342 	pc = 0;
2343 	gpr = EMU_GPR_FIRST_MIX;	// we reserve 16 gprs for processing
2344 #define EMU_DSP_TMPGPR_FRONT_LEFT 0
2345 #define EMU_DSP_TMPGPR_FRONT_RIGHT 1
2346 #define EMU_DSP_TMPGPR_REAR_LEFT 2
2347 #define EMU_DSP_TMPGPR_REAR_RIGHT 3
2348 #define EMU_DSP_TMPGPR_CENTER 4
2349 #define EMU_DSP_TMPGPR_SUB 5
2350 #define EMU_DSP_TMPGPR_DSP_IN_L	6
2351 #define EMU_DSP_TMPGPR_DSP_IN_R	7
2352 
2353 	a_front_gpr = emuxki_gpr_new(card, "Analog Front",
2354 			EMU_MIX_GAIN|EMU_MIX_STEREO|EMU_MIX_MUTE|EMU_MIX_PLAYBACK, &gpr, 0.0, 0.0, -46.5, 0.0, -0.75);
2355 	a_rear_gpr = emuxki_gpr_new(card, "Analog Rear",
2356 			EMU_MIX_GAIN|EMU_MIX_STEREO|EMU_MIX_MUTE|EMU_MIX_PLAYBACK, &gpr, 0.0, 0.0, -46.5, 0.0, -0.75);
2357 	if (IS_AUDIGY(&card->config) || IS_LIVE_5_1(&card->config))
2358 		a_center_sub_gpr = emuxki_gpr_new(card, "Analog Center/Sub",
2359 			EMU_MIX_GAIN|EMU_MIX_STEREO|EMU_MIX_MUTE|EMU_MIX_PLAYBACK, &gpr, 0.0, 0.0, -46.5, 0.0, -0.75);
2360 
2361 	d_front_gpr = emuxki_gpr_new(card, "Digital Front",
2362 			EMU_MIX_GAIN|EMU_MIX_STEREO|EMU_MIX_MUTE|EMU_MIX_PLAYBACK, &gpr, 0.0, 0.0, -46.5, 0.0, -0.75);
2363 	d_rear_gpr = emuxki_gpr_new(card, "Digital Rear",
2364 			EMU_MIX_GAIN|EMU_MIX_STEREO|EMU_MIX_MUTE|EMU_MIX_PLAYBACK, &gpr, 0.0, 0.0, -46.5, 0.0, -0.75);
2365 	d_center_sub_gpr = emuxki_gpr_new(card, "Digital Center/Sub",
2366 			EMU_MIX_GAIN|EMU_MIX_STEREO|EMU_MIX_MUTE|EMU_MIX_PLAYBACK, &gpr, 0.0, 0.0, -46.5, 0.0, -0.75);
2367 
2368 	/* playback in gprs */
2369 	p_ac97_in_gpr = emuxki_gpr_new(card, "AC97 Record In",
2370 			EMU_MIX_GAIN|EMU_MIX_STEREO|EMU_MIX_MUTE|EMU_MIX_PLAYBACK, &gpr, 0.0, 1.0, -46.5, 0.0, -0.75);
2371 	p_cd_in_gpr = emuxki_gpr_new(card, "CD Spdif In",
2372 			EMU_MIX_GAIN|EMU_MIX_STEREO|EMU_MIX_MUTE|EMU_MIX_PLAYBACK, &gpr, 0.0, 1.0, -46.5, 0.0, -0.75);
2373 
2374 	/* record in gprs */
2375 	r_ac97_in_gpr = emuxki_gpr_new(card, "AC97 Record In",
2376 			EMU_MIX_GAIN|EMU_MIX_STEREO|EMU_MIX_MUTE|EMU_MIX_RECORD, &gpr, 0.0, 0.0, -46.5, 0.0, -0.75);
2377 	r_cd_in_gpr = emuxki_gpr_new(card, "CD Spdif In",
2378 			EMU_MIX_GAIN|EMU_MIX_STEREO|EMU_MIX_MUTE|EMU_MIX_RECORD, &gpr, 0.0, 0.0, -46.5, 0.0, -0.75);
2379 	r_fx_out_gpr = emuxki_gpr_new(card, "FX 0/1",
2380 			EMU_MIX_GAIN|EMU_MIX_STEREO|EMU_MIX_MUTE|EMU_MIX_RECORD, &gpr, 0.0, 0.0, -46.5, 0.0, -0.75);
2381 
2382 	card->gpr_count = gpr;
2383 
2384 	if (IS_AUDIGY(&card->config)) {
2385 		/* DSP_IN_GPR(l/r) = 0 + AC97In(l/r) * P_AC97_IN_GPR(l/r) */
2386 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2387 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2388 				  EMU_A_DSP_CST(0),
2389 				  EMU_A_DSP_INL(EMU_DSP_IN_AC97), EMU_A_DSP_GPR(p_ac97_in_gpr->gpr));
2390 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2391 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2392 				  EMU_A_DSP_CST(0),
2393 				  EMU_A_DSP_INR(EMU_DSP_IN_AC97), EMU_A_DSP_GPR(p_ac97_in_gpr->gpr + 1));
2394 
2395 		/* DSP_IN_GPR(l/r) = DSP_IN_GPR(l/r) + CDIn(l/r) * P_CD_IN_GPR(l/r) */
2396 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2397 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2398 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2399 				  EMU_A_DSP_INL(EMU_DSP_IN_CDSPDIF), EMU_A_DSP_GPR(p_cd_in_gpr->gpr));
2400 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2401 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2402 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2403 				  EMU_A_DSP_INR(EMU_DSP_IN_CDSPDIF), EMU_A_DSP_GPR(p_cd_in_gpr->gpr + 1));
2404 
2405 		/* Front GPR(l/r) = DSP_IN_GPR(l/r) + FX(0/1) * 4 */
2406 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2407 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_FRONT_LEFT),
2408 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2409 				  EMU_DSP_FX(0), EMU_A_DSP_CST(4));
2410 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2411 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_FRONT_RIGHT),
2412 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2413 				  EMU_DSP_FX(1), EMU_A_DSP_CST(4));
2414 
2415 		/* Rear GPR(l/r) = DSP_IN_GPR(l/r) + FX(2/3) * 4 */
2416 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2417 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_REAR_LEFT),
2418 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2419 				  EMU_DSP_FX(2), EMU_A_DSP_CST(4));
2420 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2421 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_REAR_RIGHT),
2422 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2423 				  EMU_DSP_FX(3), EMU_A_DSP_CST(4));
2424 
2425 		/* Center/Sub GPR = 0 + FX(4/5) * 4 */
2426 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2427 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_CENTER),
2428 				  EMU_A_DSP_CST(0),
2429 				  EMU_DSP_FX(4), EMU_A_DSP_CST(4));
2430 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2431 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_SUB),
2432 				  EMU_A_DSP_CST(0),
2433 				  EMU_DSP_FX(5), EMU_A_DSP_CST(4));
2434 
2435 		/* Analog Front Output l/r = 0 + Front GPR(l/r) * A_FRONT_GPR(l/r) */
2436 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2437 				  EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_FRONT),
2438 				  EMU_A_DSP_CST(0),
2439 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_FRONT_LEFT), EMU_A_DSP_GPR(a_front_gpr->gpr));
2440 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2441 				  EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_FRONT),
2442 				  EMU_A_DSP_CST(0),
2443 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_FRONT_RIGHT), EMU_A_DSP_GPR(a_front_gpr->gpr+1));
2444 
2445 		/* Analog Rear Output l/r = 0 + Rear GPR(l/r) * A_REAR_GPR(l/r) */
2446 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2447 				  EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_REAR),
2448 				  EMU_A_DSP_CST(0),
2449 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_REAR_LEFT), EMU_A_DSP_GPR(a_rear_gpr->gpr));
2450 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2451 				  EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_REAR),
2452 				  EMU_A_DSP_CST(0),
2453 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_REAR_RIGHT), EMU_A_DSP_GPR(a_rear_gpr->gpr+1));
2454 
2455 		/* Analog Center/Sub = 0 + Center/Sub GPR(l/r) * A_CENTER_GPR(l/r) */
2456 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2457 				  EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_CENTER),
2458 				  EMU_A_DSP_CST(0),
2459 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_CENTER), EMU_A_DSP_GPR(a_center_sub_gpr->gpr));
2460 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2461 				  EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_CENTER),
2462 				  EMU_A_DSP_CST(0),
2463 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_SUB), EMU_A_DSP_GPR(a_center_sub_gpr->gpr+1));
2464 
2465 		/* Digital Front Output l/r = 0 + Front GPR(l/r) * D_FRONT_GPR(l/r) */
2466 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2467 				  EMU_A_DSP_OUTL(EMU_A_DSP_OUT_D_FRONT),
2468 				  EMU_A_DSP_CST(0),
2469 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_FRONT_LEFT), EMU_A_DSP_GPR(d_front_gpr->gpr));
2470 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2471 				  EMU_A_DSP_OUTR(EMU_A_DSP_OUT_D_FRONT),
2472 				  EMU_A_DSP_CST(0),
2473 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_FRONT_RIGHT), EMU_A_DSP_GPR(d_front_gpr->gpr+1));
2474 
2475 		/* Digital Rear Output l/r = 0 + Rear GPR(l/r) * D_REAR_GPR(l/r) */
2476 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2477 				  EMU_A_DSP_OUTL(EMU_A_DSP_OUT_D_REAR),
2478 				  EMU_A_DSP_CST(0),
2479 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_REAR_LEFT), EMU_A_DSP_GPR(d_rear_gpr->gpr));
2480 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2481 				  EMU_A_DSP_OUTR(EMU_A_DSP_OUT_D_REAR),
2482 				  EMU_A_DSP_CST(0),
2483 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_REAR_RIGHT), EMU_A_DSP_GPR(d_rear_gpr->gpr+1));
2484 
2485 		/* Digital Center/Sub = 0 + Center/Sub GPR(l/r) * D_CENTER_GPR(l/r) */
2486 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2487 				  EMU_A_DSP_OUTL(EMU_A_DSP_OUT_D_CENTER),
2488 				  EMU_A_DSP_CST(0),
2489 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_CENTER), EMU_A_DSP_GPR(d_center_sub_gpr->gpr));
2490 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2491 				  EMU_A_DSP_OUTR(EMU_A_DSP_OUT_D_CENTER),
2492 				  EMU_A_DSP_CST(0),
2493 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_SUB), EMU_A_DSP_GPR(d_center_sub_gpr->gpr+1));
2494 
2495 		/* DSP_IN_GPR(l/r) = 0 + AC97In(l/r) * R_AC97_IN_GPR(l/r) */
2496 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2497 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2498 				  EMU_A_DSP_CST(0),
2499 				  EMU_A_DSP_INL(EMU_DSP_IN_AC97), EMU_A_DSP_GPR(r_ac97_in_gpr->gpr));
2500 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2501 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2502 				  EMU_A_DSP_CST(0),
2503 				  EMU_A_DSP_INR(EMU_DSP_IN_AC97), EMU_A_DSP_GPR(r_ac97_in_gpr->gpr + 1));
2504 
2505 		/* DSP_IN_GPR (l/r) = DSP_IN_GPR(l/r) + FX(0/1) * R_FX_OUT_GPR(l/r) */
2506 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2507 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2508 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2509 				  EMU_DSP_FX(0), EMU_A_DSP_GPR(r_fx_out_gpr->gpr));
2510 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2511 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2512 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2513 				  EMU_DSP_FX(1), EMU_A_DSP_GPR(r_fx_out_gpr->gpr + 1));
2514 
2515 		/* DSP_IN_GPR(l/r) = 0 + DSP_IN_GPR(l/r) * 4 */
2516 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2517 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2518 				  EMU_A_DSP_CST(0),
2519 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L), EMU_A_DSP_CST(4));
2520 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2521 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2522 				  EMU_A_DSP_CST(0),
2523 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R), EMU_A_DSP_CST(4));
2524 
2525 		/* ADC recording buffer (l/r) = DSP_IN_GPR(l/r) + CDIn(l/r) * R_CD_IN_GPR(l/r) */
2526 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2527 				  EMU_A_DSP_OUTL(EMU_A_DSP_OUT_ADC),
2528 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2529 				  EMU_A_DSP_INL(EMU_DSP_IN_CDSPDIF), EMU_A_DSP_GPR(r_cd_in_gpr->gpr));
2530 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2531 				  EMU_A_DSP_OUTR(EMU_A_DSP_OUT_ADC),
2532 				  EMU_A_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2533 				  EMU_A_DSP_INR(EMU_DSP_IN_CDSPDIF), EMU_A_DSP_GPR(r_cd_in_gpr->gpr + 1));
2534 
2535 
2536 
2537 		/* zero out the rest of the microcode */
2538 		while (pc < 512)
2539 			emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_ACC3,
2540 				  EMU_A_DSP_CST(0), EMU_A_DSP_CST(0),
2541 				  EMU_A_DSP_CST(0), EMU_A_DSP_CST(0));
2542 
2543 		emuxki_chan_write(&card->config, 0, EMU_A_DBG, 0);	/* Is it really necessary ? */
2544 
2545 	} else {
2546 		/* DSP_IN_GPR(l/r) = 0 + AC97In(l/r) * P_AC97_IN_GPR(l/r) */
2547 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2548 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2549 				  EMU_DSP_CST(0),
2550 				  EMU_DSP_INL(EMU_DSP_IN_AC97), EMU_DSP_GPR(p_ac97_in_gpr->gpr));
2551 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2552 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2553 				  EMU_DSP_CST(0),
2554 				  EMU_DSP_INR(EMU_DSP_IN_AC97), EMU_DSP_GPR(p_ac97_in_gpr->gpr + 1));
2555 
2556 		/* DSP_IN_GPR(l/r) = DSP_IN_GPR(l/r) + CDIn(l/r) * P_CD_IN_GPR(l/r) */
2557 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2558 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2559 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2560 				  EMU_DSP_INL(EMU_DSP_IN_CDSPDIF), EMU_DSP_GPR(p_cd_in_gpr->gpr));
2561 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2562 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2563 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2564 				  EMU_DSP_INR(EMU_DSP_IN_CDSPDIF), EMU_DSP_GPR(p_cd_in_gpr->gpr + 1));
2565 
2566 		/* Front GPR(l/r) = DSP_IN_GPR(l/r) + FX(0/1) * 4 */
2567 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2568 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_FRONT_LEFT),
2569 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2570 				  EMU_DSP_FX(0), EMU_DSP_CST(4));
2571 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2572 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_FRONT_RIGHT),
2573 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2574 				  EMU_DSP_FX(1), EMU_DSP_CST(4));
2575 
2576 		/* Rear GPR(l/r) = DSP_IN_GPR(l/r) + FX(2/3) * 4 */
2577 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2578 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_REAR_LEFT),
2579 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2580 				  EMU_DSP_FX(2), EMU_DSP_CST(4));
2581 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2582 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_REAR_RIGHT),
2583 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2584 				  EMU_DSP_FX(3), EMU_DSP_CST(4));
2585 
2586 		/* Center/Sub GPR = 0 + FX(4/5) * 4 */
2587 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2588 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_CENTER),
2589 				  EMU_DSP_CST(0),
2590 				  EMU_DSP_FX(4), EMU_DSP_CST(4));
2591 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2592 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_SUB),
2593 				  EMU_DSP_CST(0),
2594 				  EMU_DSP_FX(5), EMU_DSP_CST(4));
2595 
2596 		/* Analog Front Output l/r = 0 + Front GPR(l/r) * A_FRONT_GPR(l/r) */
2597 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2598 				  EMU_DSP_OUTL(EMU_DSP_OUT_A_FRONT),
2599 				  EMU_DSP_CST(0),
2600 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_FRONT_LEFT), EMU_DSP_GPR(a_front_gpr->gpr));
2601 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2602 				  EMU_DSP_OUTR(EMU_DSP_OUT_A_FRONT),
2603 				  EMU_DSP_CST(0),
2604 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_FRONT_RIGHT), EMU_DSP_GPR(a_front_gpr->gpr+1));
2605 
2606 		/* Analog Rear Output l/r = 0 + Rear GPR(l/r) * A_REAR_GPR(l/r) */
2607 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2608 				  EMU_DSP_OUTL(EMU_DSP_OUT_AD_REAR),
2609 				  EMU_DSP_CST(0),
2610 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_REAR_LEFT), EMU_DSP_GPR(a_rear_gpr->gpr));
2611 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2612 				  EMU_DSP_OUTR(EMU_DSP_OUT_AD_REAR),
2613 				  EMU_DSP_CST(0),
2614 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_REAR_RIGHT), EMU_DSP_GPR(a_rear_gpr->gpr+1));
2615 
2616 		/* Analog Center/Sub = 0 + Center/Sub GPR(l/r) * A_CENTER_GPR(l/r) */
2617 		if (IS_LIVE_5_1(&card->config)) {
2618 			emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2619 					  EMU_DSP_OUT_A_CENTER,
2620 					  EMU_DSP_CST(0),
2621 					  EMU_DSP_GPR(EMU_DSP_TMPGPR_CENTER), EMU_DSP_GPR(a_center_sub_gpr->gpr));
2622 			emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2623 					  EMU_DSP_OUT_A_SUB,
2624 					  EMU_DSP_CST(0),
2625 					  EMU_DSP_GPR(EMU_DSP_TMPGPR_SUB), EMU_DSP_GPR(a_center_sub_gpr->gpr+1));
2626 		}
2627 
2628 		/* Digital Front Output l/r = 0 + Front GPR(l/r) * D_FRONT_GPR(l/r) */
2629 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2630 				  EMU_DSP_OUTL(EMU_DSP_OUT_D_FRONT),
2631 				  EMU_DSP_CST(0),
2632 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_FRONT_LEFT), EMU_DSP_GPR(d_front_gpr->gpr));
2633 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2634 				  EMU_DSP_OUTR(EMU_DSP_OUT_D_FRONT),
2635 				  EMU_DSP_CST(0),
2636 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_FRONT_RIGHT), EMU_DSP_GPR(d_front_gpr->gpr+1));
2637 
2638 		/* Digital Rear Output l/r = 0 + Rear GPR(l/r) * D_REAR_GPR(l/r) */
2639 		/*emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2640 				  EMU_DSP_OUTL(EMU_DSP_OUT_D_REAR),
2641 				  EMU_DSP_CST(0),
2642 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_REAR_LEFT), EMU_DSP_GPR(d_rear_gpr->gpr));
2643 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2644 				  EMU_DSP_OUTR(EMU_DSP_OUT_D_REAR),
2645 				  EMU_DSP_CST(0),
2646 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_REAR_RIGHT), EMU_DSP_GPR(d_rear_gpr->gpr+1));*/
2647 
2648 		/* Digital Center/Sub = 0 + Center/Sub GPR(l/r) * D_CENTER_GPR(l/r) */
2649 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2650 				  EMU_DSP_OUTL(EMU_DSP_OUT_D_CENTER),
2651 				  EMU_DSP_CST(0),
2652 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_CENTER), EMU_DSP_GPR(d_center_sub_gpr->gpr));
2653 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2654 				  EMU_DSP_OUTR(EMU_DSP_OUT_D_CENTER),
2655 				  EMU_DSP_CST(0),
2656 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_SUB), EMU_DSP_GPR(d_center_sub_gpr->gpr+1));
2657 
2658 		/* DSP_IN_GPR(l/r) = 0 + AC97In(l/r) * R_AC97_IN_GPR(l/r) */
2659 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2660 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2661 				  EMU_DSP_CST(0),
2662 				  EMU_DSP_INL(EMU_DSP_IN_AC97), EMU_DSP_GPR(r_ac97_in_gpr->gpr));
2663 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2664 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2665 				  EMU_DSP_CST(0),
2666 				  EMU_DSP_INR(EMU_DSP_IN_AC97), EMU_DSP_GPR(r_ac97_in_gpr->gpr + 1));
2667 
2668 		/* DSP_IN_GPR (l/r) = DSP_IN_GPR(l/r) + FX(0/1) * R_FX_OUT_GPR(l/r) */
2669 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2670 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2671 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2672 				  EMU_DSP_FX(0), EMU_DSP_GPR(r_fx_out_gpr->gpr));
2673 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2674 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2675 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2676 				  EMU_DSP_FX(1), EMU_DSP_GPR(r_fx_out_gpr->gpr + 1));
2677 
2678 		/* DSP_IN_GPR(l/r) = 0 + DSP_IN_GPR(l/r) * 4 */
2679 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2680 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2681 				  EMU_DSP_CST(0),
2682 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L), EMU_DSP_CST(4));
2683 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACINTS,
2684 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2685 				  EMU_DSP_CST(0),
2686 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R), EMU_DSP_CST(4));
2687 
2688 		/* ADC recording buffer (l/r) = DSP_IN_GPR(l/r) + CDIn(l/r) * R_CD_IN_GPR(l/r) */
2689 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2690 				  EMU_DSP_OUTL(EMU_DSP_OUT_ADC),
2691 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_L),
2692 				  EMU_DSP_INL(EMU_DSP_IN_CDSPDIF), EMU_DSP_GPR(r_cd_in_gpr->gpr));
2693 		emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_MACS,
2694 				  EMU_DSP_OUTR(EMU_DSP_OUT_ADC),
2695 				  EMU_DSP_GPR(EMU_DSP_TMPGPR_DSP_IN_R),
2696 				  EMU_DSP_INR(EMU_DSP_IN_CDSPDIF), EMU_DSP_GPR(r_cd_in_gpr->gpr + 1));
2697 
2698 		/* zero out the rest of the microcode */
2699 		while (pc < 512)
2700 			emuxki_dsp_addop(&card->config, &pc, EMU_DSP_OP_ACC3,
2701 				  EMU_DSP_CST(0), EMU_DSP_CST(0),
2702 				  EMU_DSP_CST(0), EMU_DSP_CST(0));
2703 
2704 		emuxki_chan_write(&card->config, 0, EMU_DBG, 0);	/* Is it really necessary ? */
2705 	}
2706 
2707 	emuxki_dump_fx(card);
2708 }
2709 
2710 
2711 status_t
2712 emuxki_init(emuxki_dev * card)
2713 {
2714 	uint16       i;
2715 	uint32       spcs, *ptb;
2716 	uint32	 	 silentpage;
2717 
2718 	/* disable any channel interrupt */
2719 	emuxki_chan_write(&card->config, 0, EMU_CLIEL, 0);
2720 	emuxki_chan_write(&card->config, 0, EMU_CLIEH, 0);
2721 	emuxki_chan_write(&card->config, 0, EMU_SOLEL, 0);
2722 	emuxki_chan_write(&card->config, 0, EMU_SOLEH, 0);
2723 
2724 	/* Set recording buffers sizes to zero */
2725 	emuxki_chan_write(&card->config, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
2726 	emuxki_chan_write(&card->config, 0, EMU_MICBA, 0);
2727 	emuxki_chan_write(&card->config, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
2728 	emuxki_chan_write(&card->config, 0, EMU_FXBA, 0);
2729 	emuxki_chan_write(&card->config, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
2730 	emuxki_chan_write(&card->config, 0, EMU_ADCBA, 0);
2731 
2732 	if (IS_AUDIGY(&card->config)) {
2733 		emuxki_chan_write(&card->config, 0, EMU_SPBYPASS, EMU_SPBYPASS_24_BITS);
2734 		emuxki_chan_write(&card->config, 0, EMU_AC97SLOT, EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE);
2735 	}
2736 
2737 	/* Initialize all channels to stopped and no effects */
2738 	for (i = 0; i < EMU_NUMCHAN; i++) {
2739 		emuxki_chan_write(&card->config, i, EMU_CHAN_DCYSUSV, 0);
2740 		emuxki_chan_write(&card->config, i, EMU_CHAN_IP, 0);
2741 		emuxki_chan_write(&card->config, i, EMU_CHAN_VTFT, 0xffff);
2742 		emuxki_chan_write(&card->config, i, EMU_CHAN_CVCF, 0xffff);
2743 		emuxki_chan_write(&card->config, i, EMU_CHAN_PTRX, 0);
2744 		emuxki_chan_write(&card->config, i, EMU_CHAN_CPF, 0);
2745 		emuxki_chan_write(&card->config, i, EMU_CHAN_CCR, 0);
2746 		emuxki_chan_write(&card->config, i, EMU_CHAN_PSST, 0);
2747 		emuxki_chan_write(&card->config, i, EMU_CHAN_DSL, 0x10);	/* Why 16 ? */
2748 		emuxki_chan_write(&card->config, i, EMU_CHAN_CCCA, 0);
2749 		emuxki_chan_write(&card->config, i, EMU_CHAN_Z1, 0);
2750 		emuxki_chan_write(&card->config, i, EMU_CHAN_Z2, 0);
2751 		emuxki_chan_write(&card->config, i, EMU_CHAN_FXRT, 0x32100000);
2752 		emuxki_chan_write(&card->config, i, EMU_CHAN_ATKHLDM, 0);
2753 		emuxki_chan_write(&card->config, i, EMU_CHAN_DCYSUSM, 0);
2754 		emuxki_chan_write(&card->config, i, EMU_CHAN_IFATN, 0xffff);
2755 		emuxki_chan_write(&card->config, i, EMU_CHAN_PEFE, 0);
2756 		emuxki_chan_write(&card->config, i, EMU_CHAN_FMMOD, 0);
2757 		emuxki_chan_write(&card->config, i, EMU_CHAN_TREMFRQ, 24);
2758 		emuxki_chan_write(&card->config, i, EMU_CHAN_FM2FRQ2, 24);
2759 		emuxki_chan_write(&card->config, i, EMU_CHAN_TEMPENV, 0);
2760 		/*** these are last so OFF prevents writing ***/
2761 		emuxki_chan_write(&card->config, i, EMU_CHAN_LFOVAL2, 0);
2762 		emuxki_chan_write(&card->config, i, EMU_CHAN_LFOVAL1, 0);
2763 		emuxki_chan_write(&card->config, i, EMU_CHAN_ATKHLDV, 0);
2764 		emuxki_chan_write(&card->config, i, EMU_CHAN_ENVVOL, 0);
2765 		emuxki_chan_write(&card->config, i, EMU_CHAN_ENVVAL, 0);
2766 	}
2767 
2768 	/* set digital outputs format */
2769 	spcs = (EMU_SPCS_CLKACCY_1000PPM | EMU_SPCS_SAMPLERATE_48 |
2770 	      EMU_SPCS_CHANNELNUM_LEFT | EMU_SPCS_SOURCENUM_UNSPEC |
2771 		EMU_SPCS_GENERATIONSTATUS | 0x00001200 /* Cat code. */ |
2772 		0x00000000 /* IEC-958 Mode */ | EMU_SPCS_EMPHASIS_NONE |
2773 		EMU_SPCS_COPYRIGHT);
2774 	emuxki_chan_write(&card->config, 0, EMU_SPCS0, spcs);
2775 	emuxki_chan_write(&card->config, 0, EMU_SPCS1, spcs);
2776 	emuxki_chan_write(&card->config, 0, EMU_SPCS2, spcs);
2777 
2778 	if (IS_AUDIGY2(&card->config)) {
2779 		emuxki_chan_write(&card->config, 0, EMU_A2_SPDIF_SAMPLERATE, EMU_A2_SPDIF_UNKNOWN);
2780 
2781 		emuxki_p16v_write(&card->config, 0, EMU_A2_SRCSEL,
2782 			EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI);
2783 
2784 		if (IS_AUDIGY2_VALUE(&card->config)) {
2785 			emuxki_p16v_write(&card->config, 0, EMU_A2_P17V_I2S, EMU_A2_P17V_I2S_ENABLE);
2786 			emuxki_p16v_write(&card->config, 0, EMU_A2_P17V_SPDIF, EMU_A2_P17V_SPDIF_ENABLE);
2787 
2788 			emuxki_reg_write_32(&card->config, EMU_A_IOCFG,
2789 				emuxki_reg_read_32(&card->config, EMU_A_IOCFG) & ~0x8);
2790 		} else {
2791 			emuxki_p16v_write(&card->config, 0, EMU_A2_SRCMULTI, EMU_A2_SRCMULTI_ENABLE_INPUT);
2792 		}
2793 	}
2794 
2795 	/* Let's play with sound processor */
2796 	emuxki_initfx(card);
2797 
2798 	/* allocate memory for our Page Table */
2799 	card->ptb_area = alloc_mem(&card->ptb_phy_base, &card->ptb_log_base,
2800 		EMU_MAXPTE * sizeof(uint32), "emuxki ptb");
2801 
2802 	/* This is necessary unless you like Metallic noise... */
2803 	card->silentpage_area = alloc_mem(&card->silentpage_phy_base, &card->silentpage_log_base,
2804 		EMU_PTESIZE, "emuxki sp");
2805 
2806 	if (card->ptb_area < B_OK || card->silentpage_area < B_OK) {
2807 		PRINT(("couldn't allocate memory\n"));
2808 		if (card->ptb_area > B_OK)
2809 			delete_area(card->ptb_area);
2810 		if (card->silentpage_area > B_OK)
2811 			delete_area(card->silentpage_area);
2812 		return B_ERROR;
2813 	}
2814 
2815 	/* Zero out the silent page */
2816 	/* This might not be always true, it might be 128 for 8bit channels */
2817 	memset(card->ptb_log_base, 0, EMU_PTESIZE);
2818 
2819 	/*
2820 	 * Set all the PTB Entries to the silent page We shift the physical
2821 	 * address by one and OR it with the page number. I don't know what
2822 	 * the ORed index is for, might be a very useful unused feature...
2823 	 */
2824 	silentpage = ((uint32)card->silentpage_phy_base) << 1;
2825 	ptb = card->ptb_log_base;
2826 	for (i = 0; i < EMU_MAXPTE; i++)
2827 		ptb[i] = B_HOST_TO_LENDIAN_INT32(silentpage | i);
2828 
2829 	/* Write PTB address and set TCB to none */
2830 	emuxki_chan_write(&card->config, 0, EMU_PTB, (uint32)card->ptb_phy_base);
2831 	emuxki_chan_write(&card->config, 0, EMU_TCBS, 0);	/* This means 16K TCB */
2832 	emuxki_chan_write(&card->config, 0, EMU_TCB, 0);	/* No TCB use for now */
2833 
2834 	/*
2835 	 * Set channels MAPs to the silent page.
2836 	 * I don't know what MAPs are for.
2837 	 */
2838 	silentpage |= EMU_CHAN_MAP_PTI_MASK;
2839 	for (i = 0; i < EMU_NUMCHAN; i++) {
2840 		emuxki_chan_write(&card->config, i, EMU_CHAN_MAPA, silentpage);
2841 		emuxki_chan_write(&card->config, i, EMU_CHAN_MAPB, silentpage);
2842 		card->channel[i] = NULL;
2843 	}
2844 
2845 	/* Init streams list */
2846 	LIST_INIT(&(card->streams));
2847 
2848 	/* Init mems list */
2849 	LIST_INIT(&(card->mem));
2850 
2851 	/* Timer is stopped */
2852 	card->timerstate &= ~EMU_TIMER_STATE_ENABLED;
2853 	card->timerate = 0xffff;
2854 
2855 	return B_OK;
2856 }
2857 
2858 
2859 status_t
2860 init_driver(void)
2861 {
2862 	void *settings_handle;
2863 	pci_info info;
2864 	status_t err;
2865 	int ix = 0;
2866 	num_cards = 0;
2867 
2868 	PRINT(("init_driver()\n"));
2869 
2870 	// get driver settings
2871 	settings_handle = load_driver_settings("emuxki.settings");
2872 	if (settings_handle != NULL) {
2873 		const char *item;
2874 		char       *end;
2875 		uint32      value;
2876 
2877 		item = get_driver_parameter (settings_handle, "channels", NULL, NULL);
2878 		if (item) {
2879 			value = strtoul (item, &end, 0);
2880 			if (*end == '\0') current_settings.channels = value;
2881 		}
2882 
2883 		item = get_driver_parameter (settings_handle, "bitsPerSample", NULL, NULL);
2884 		if (item) {
2885 			value = strtoul (item, &end, 0);
2886 			if (*end == '\0') current_settings.bitsPerSample = value;
2887 		}
2888 
2889 		item = get_driver_parameter (settings_handle, "sample_rate", NULL, NULL);
2890 		if (item) {
2891 			value = strtoul (item, &end, 0);
2892 			if (*end == '\0') current_settings.sample_rate = value;
2893 		}
2894 
2895 		item = get_driver_parameter (settings_handle, "buffer_frames", NULL, NULL);
2896 		if (item) {
2897 			value = strtoul (item, &end, 0);
2898 			if (*end == '\0') current_settings.buffer_frames = value;
2899 		}
2900 
2901 		item = get_driver_parameter (settings_handle, "buffer_count", NULL, NULL);
2902 		if (item) {
2903 			value = strtoul (item, &end, 0);
2904 			if (*end == '\0') current_settings.buffer_count = value;
2905 		}
2906 
2907 		unload_driver_settings (settings_handle);
2908 	}
2909 
2910 	if (get_module(B_PCI_MODULE_NAME, (module_info **) &pci))
2911 		return ENOSYS;
2912 
2913 //	if (get_module (gameport_name, (module_info **)&gameport)) {
2914 //		put_module (B_PCI_MODULE_NAME);
2915 //		return ENOSYS;
2916 //	}
2917 
2918 	if (get_module(B_MPU_401_MODULE_NAME, (module_info **) &mpu401)) {
2919 		//put_module(gameport_name);
2920 		put_module(B_PCI_MODULE_NAME);
2921 		return ENOSYS;
2922 	}
2923 
2924 	while ((*pci->get_nth_pci_info)(ix++, &info) == B_OK) {
2925 		if (info.vendor_id == CREATIVELABS_VENDOR_ID &&
2926 			(info.device_id == CREATIVELABS_SBLIVE_DEVICE_ID
2927 #if AUDIGY
2928 			|| info.device_id == CREATIVELABS_AUDIGY_DEVICE_ID
2929 			|| info.device_id == CREATIVELABS_AUDIGY2_VALUE_DEVICE_ID
2930 #endif
2931 			)) {
2932 			if (num_cards == NUM_CARDS) {
2933 				PRINT(("Too many emuxki cards installed!\n"));
2934 				break;
2935 			}
2936 			memset(&cards[num_cards], 0, sizeof(emuxki_dev));
2937 			cards[num_cards].info = info;
2938 #ifdef __HAIKU__
2939 			if ((err = (*pci->reserve_device)(info.bus, info.device, info.function,
2940 				DRIVER_NAME, &cards[num_cards])) < B_OK) {
2941 				dprintf("%s: failed to reserve_device(%d, %d, %d,): %s\n",
2942 					DRIVER_NAME, info.bus, info.device, info.function,
2943 					strerror(err));
2944 				continue;
2945 			}
2946 #endif
2947 			if (emuxki_setup(&cards[num_cards])) {
2948 				PRINT(("Setup of emuxki %ld failed\n", num_cards+1));
2949 #ifdef __HAIKU__
2950 				(*pci->unreserve_device)(info.bus, info.device, info.function,
2951 					DRIVER_NAME, &cards[num_cards]);
2952 #endif
2953 			} else {
2954 				num_cards++;
2955 			}
2956 		}
2957 	}
2958 	if (!num_cards) {
2959 		put_module(B_MPU_401_MODULE_NAME);
2960 //		put_module(gameport_name);
2961 		put_module(B_PCI_MODULE_NAME);
2962 		PRINT(("emuxki: no suitable cards found\n"));
2963 		return ENODEV;
2964 	}
2965 
2966 	return B_OK;
2967 }
2968 
2969 
2970 void
2971 emuxki_shutdown(emuxki_dev *card)
2972 {
2973 	uint32       i;
2974 
2975 	PRINT(("shutdown(%p)\n", card));
2976 
2977 	emuxki_reg_write_32(&card->config, EMU_HCFG, EMU_HCFG_LOCKSOUNDCACHE |
2978 		  EMU_HCFG_LOCKTANKCACHE_MASK | EMU_HCFG_MUTEBUTTONENABLE);
2979 	emuxki_reg_write_32(&card->config, EMU_INTE, 0);
2980 
2981 	dump_hardware_regs(&card->config);
2982 
2983 	/* Disable any Channels interrupts */
2984 	emuxki_chan_write(&card->config, 0, EMU_CLIEL, 0);
2985 	emuxki_chan_write(&card->config, 0, EMU_CLIEH, 0);
2986 	emuxki_chan_write(&card->config, 0, EMU_SOLEL, 0);
2987 	emuxki_chan_write(&card->config, 0, EMU_SOLEH, 0);
2988 
2989 	/* Stop all channels */
2990 	/* This shouldn't be necessary, i'll remove once everything works */
2991 	for (i = 0; i < EMU_NUMCHAN; i++)
2992 		emuxki_chan_write(&card->config, i, EMU_CHAN_DCYSUSV, 0);
2993 	for (i = 0; i < EMU_NUMCHAN; i++) {
2994 		emuxki_chan_write(&card->config, i, EMU_CHAN_VTFT, 0);
2995 		emuxki_chan_write(&card->config, i, EMU_CHAN_CVCF, 0);
2996 		emuxki_chan_write(&card->config, i, EMU_CHAN_PTRX, 0);
2997 		emuxki_chan_write(&card->config, i, EMU_CHAN_CPF, 0);
2998 	}
2999 
3000 	remove_io_interrupt_handler(card->config.irq, emuxki_int, card);
3001 
3002 	/*
3003 	 * deallocate Emu10k1 caches and recording buffers Again it will be
3004 	 * removed because it will be done in voice shutdown.
3005 	 */
3006 	emuxki_chan_write(&card->config, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
3007 	emuxki_chan_write(&card->config, 0, EMU_MICBA, 0);
3008 	emuxki_chan_write(&card->config, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
3009 	emuxki_chan_write(&card->config, 0, EMU_FXBA, 0);
3010 	if (IS_AUDIGY(&card->config)) {
3011 		emuxki_chan_write(&card->config, 0, EMU_A_FXWC1, 0);
3012 		emuxki_chan_write(&card->config, 0, EMU_A_FXWC2, 0);
3013 	} else {
3014 		emuxki_chan_write(&card->config, 0, EMU_FXWC, 0);
3015 	}
3016 	emuxki_chan_write(&card->config, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
3017 	emuxki_chan_write(&card->config, 0, EMU_ADCBA, 0);
3018 
3019 	/*
3020 	 * I don't know yet how i will handle tank cache buffer,
3021 	 * I don't even clearly  know what it is for
3022 	 */
3023 	emuxki_chan_write(&card->config, 0, EMU_TCB, 0);	/* 16K again */
3024 	emuxki_chan_write(&card->config, 0, EMU_TCBS, 0);
3025 
3026 	emuxki_chan_write(&card->config, 0, EMU_DBG, 0x8000);	/* necessary ? */
3027 
3028 	PRINT(("freeing ptb_area\n"));
3029 	if (card->ptb_area > B_OK)
3030 		delete_area(card->ptb_area);
3031 	PRINT(("freeing silentpage_area\n"));
3032 	if (card->silentpage_area > B_OK)
3033 		delete_area(card->silentpage_area);
3034 
3035 //	(*gameport->delete_device)(card->joy.driver);
3036 }
3037 
3038 
3039 void
3040 uninit_driver(void)
3041 {
3042 	int ix, cnt = num_cards;
3043 
3044 	PRINT(("uninit_driver()\n"));
3045 
3046 	for (ix=0; ix<cnt; ix++) {
3047 		emuxki_shutdown(&cards[ix]);
3048 #ifdef __HAIKU__
3049 		(*pci->unreserve_device)(cards[ix].info.bus,
3050 			cards[ix].info.device, cards[ix].info.function,
3051 			DRIVER_NAME, &cards[ix]);
3052 #endif
3053 	}
3054 	memset(&cards, 0, sizeof(cards));
3055 	put_module(B_MPU_401_MODULE_NAME);
3056 //	put_module(gameport_name);
3057 	put_module(B_PCI_MODULE_NAME);
3058 	num_cards = 0;
3059 }
3060 
3061 
3062 const char **
3063 publish_devices(void)
3064 {
3065 	int ix = 0;
3066 	PRINT(("publish_devices()\n"));
3067 
3068 	for (ix=0; names[ix]; ix++) {
3069 		PRINT(("publish %s\n", names[ix]));
3070 	}
3071 	return (const char **)names;
3072 }
3073 
3074 
3075 device_hooks *
3076 find_device(const char * name)
3077 {
3078 	int ix;
3079 
3080 	PRINT(("emuxki: find_device(%s)\n", name));
3081 
3082 	for (ix=0; ix<num_cards; ix++) {
3083 #if MIDI
3084 		if (!strcmp(cards[ix].midi.name, name)) {
3085 			return &midi_hooks;
3086 		}
3087 #endif
3088 //		if (!strcmp(cards[ix].joy.name1, name)) {
3089 //			return &joy_hooks;
3090 //		}
3091 
3092 		if (!strcmp(cards[ix].name, name)) {
3093 			return &multi_hooks;
3094 		}
3095 	}
3096 	PRINT(("emuxki: find_device(%s) failed\n", name));
3097 	return NULL;
3098 }
3099 
3100 int32	api_version = B_CUR_DRIVER_API_VERSION;
3101