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