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