xref: /haiku/src/add-ons/kernel/drivers/audio/emuxki/emuxki.c (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
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 = ((uint32)card->silentpage_phy_base) << 1;
256 	numblocks = size / EMU_PTESIZE;
257 	if (size % EMU_PTESIZE)
258 		numblocks++;
259 
260 	PRINT(("emuxki_pmem_alloc : numblocks : %ld\n", numblocks));
261 
262 	for (i = 0; i < EMU_MAXPTE; i++) {
263 		PRINT(("emuxki_pmem_alloc : %d\n", i));
264 		if ((B_LENDIAN_TO_HOST_INT32(ptb[i]) & EMU_CHAN_MAP_PTE_MASK) == silentpage) {
265 			/* We look for a free PTE */
266 			//s = splaudio();
267 			for (j = 0; j < numblocks; j++)
268 				if ((B_LENDIAN_TO_HOST_INT32(ptb[i + j]) & EMU_CHAN_MAP_PTE_MASK)
269 				    != silentpage)
270 					break;
271 			if (j == numblocks) {
272 				PRINT(("emuxki_pmem_alloc : j == numblocks %" 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((uint32) (
281 						(( ((uint32)mem->phy_base) +
282 						 j * EMU_PTESIZE) << 1)
283 						| (i + j)));
284 				LIST_INSERT_HEAD(&(card->mem), mem, next);
285 				PRINT(("emuxki_pmem_alloc : j == numblocks returning\n"));
286 
287 				//splx(s);
288 				return mem->log_base;
289 			} else {
290 				PRINT(("emuxki_pmem_alloc : j != numblocks %" B_PRIu32 "\n",
291 					j));
292 				i += j;
293 			}
294 			//splx(s);
295 		}
296 	}
297 	return NULL;
298 }
299 
300 
301 void *
302 emuxki_rmem_alloc(emuxki_dev *card, size_t size)
303 {
304 	emuxki_mem *mem;
305 	//int             s;
306 
307 	mem = emuxki_mem_new(card, EMU_RMEM, size);
308 	if (mem == NULL)
309 		return (NULL);
310 
311 	//s = splaudio();
312 	LIST_INSERT_HEAD(&(card->mem), mem, next);
313 	//splx(s);
314 
315 	return mem->log_base;
316 }
317 
318 
319 void
320 emuxki_mem_free(emuxki_dev *card, void *ptr)
321 {
322 	emuxki_mem 		*mem;
323 	size_t          numblocks;
324 	uint32      	i, *ptb, silentpage;
325 
326 	ptb = card->ptb_log_base;
327 	silentpage = ((uint32)card->silentpage_phy_base) << 1;
328 	LOG(("emuxki_mem_free 1\n"));
329 	LIST_FOREACH(mem, &card->mem, next) {
330 		LOG(("emuxki_mem_free 2\n"));
331 		if (mem->log_base != ptr)
332 			continue;
333 		LOG(("emuxki_mem_free 3\n"));
334 		//s = splaudio();
335 		if (mem->ptbidx != EMU_RMEM) {
336 			LOG(("mem_size : %i\n", mem->size));
337 			numblocks = mem->size / EMU_PTESIZE;
338 			if (mem->size % EMU_PTESIZE)
339 				numblocks++;
340 			for (i = 0; i < numblocks; i++)
341 				ptb[mem->ptbidx + i] =
342 					B_HOST_TO_LENDIAN_INT32(silentpage | (mem->ptbidx + i));
343 		}
344 		LIST_REMOVE(mem, next);
345 		//splx(s);
346 
347 		LOG(("freeing mem\n"));
348 		emuxki_mem_delete(mem);
349 		break;
350 	}
351 }
352 
353 /* Emuxki channel functions */
354 
355 static void
356 emuxki_chanparms_set_defaults(emuxki_channel *chan)
357 {
358 	chan->fxsend.a.level = chan->fxsend.b.level
359 		= chan->fxsend.c.level = chan->fxsend.d.level
360 	/* for audigy */
361 		= chan->fxsend.e.level = chan->fxsend.f.level
362 		= chan->fxsend.g.level = chan->fxsend.h.level
363 		= IS_AUDIGY(&chan->voice->stream->card->config) ? 0xc0 : 0xff;	/* not max */
364 
365 	chan->fxsend.a.dest = 0x0;
366 	chan->fxsend.b.dest = 0x1;
367 	chan->fxsend.c.dest = 0x2;
368 	chan->fxsend.d.dest = 0x3;
369 	/* for audigy */
370 	chan->fxsend.e.dest = 0x4;
371 	chan->fxsend.f.dest = 0x5;
372 	chan->fxsend.g.dest = 0x6;
373 	chan->fxsend.h.dest = 0x7;
374 
375 	chan->pitch.intial = 0x0000;	/* shouldn't it be 0xE000 ? */
376 	chan->pitch.current = 0x0000;	/* should it be 0x0400 */
377 	chan->pitch.target = 0x0000;	/* the unity pitch shift ? */
378 	chan->pitch.envelope_amount = 0x00;	/* none */
379 
380 	chan->initial_attenuation = 0x00;	/* no attenuation */
381 	chan->volume.current = 0x0000;	/* no volume */
382 	chan->volume.target = 0xffff;
383 	chan->volume.envelope.current_state = 0x8000;	/* 0 msec delay */
384 	chan->volume.envelope.hold_time = 0x7f;	/* 0 msec */
385 	chan->volume.envelope.attack_time = 0x7f;	/* 5.5msec */
386 	chan->volume.envelope.sustain_level = 0x7f;	/* full  */
387 	chan->volume.envelope.decay_time = 0x7f;	/* 22msec  */
388 
389 	chan->filter.initial_cutoff_frequency = 0xff;	/* no filter */
390 	chan->filter.current_cutoff_frequency = 0xffff;	/* no filtering */
391 	chan->filter.target_cutoff_frequency = 0xffff;	/* no filtering */
392 	chan->filter.lowpass_resonance_height = 0x0;
393 	chan->filter.interpolation_ROM = 0x1;	/* full band */
394 	chan->filter.envelope_amount = 0x7f;	/* none */
395 	chan->filter.LFO_modulation_depth = 0x00;	/* none */
396 
397 	chan->loop.start = 0x000000;
398 	chan->loop.end = 0x000010;	/* Why ? */
399 
400 	chan->modulation.envelope.current_state = 0x8000;
401 	chan->modulation.envelope.hold_time = 0x00;	/* 127 better ? */
402 	chan->modulation.envelope.attack_time = 0x00;	/* infinite */
403 	chan->modulation.envelope.sustain_level = 0x00;	/* off */
404 	chan->modulation.envelope.decay_time = 0x7f;	/* 22 msec */
405 	chan->modulation.LFO_state = 0x8000;
406 
407 	chan->vibrato_LFO.state = 0x8000;
408 	chan->vibrato_LFO.modulation_depth = 0x00;	/* none */
409 	chan->vibrato_LFO.vibrato_depth = 0x00;
410 	chan->vibrato_LFO.frequency = 0x00;	/* Why set to 24 when
411 						 * initialized ? */
412 
413 	chan->tremolo_depth = 0x00;
414 }
415 
416 
417 static emuxki_channel *
418 emuxki_channel_new(emuxki_voice *voice, uint8 num)
419 {
420 	emuxki_channel *chan;
421 
422 	chan = (emuxki_channel *) malloc(sizeof(emuxki_channel));
423 	if (chan == NULL)
424 		return NULL;
425 	chan->voice = voice;
426 	chan->num = num;
427 	emuxki_chanparms_set_defaults(chan);
428 	chan->voice->stream->card->channel[num] = chan;
429 	return chan;
430 }
431 
432 
433 static void
434 emuxki_channel_delete(emuxki_channel *chan)
435 {
436 	chan->voice->stream->card->channel[chan->num] = NULL;
437 	free(chan);
438 }
439 
440 
441 static void
442 emuxki_channel_set_fxsend(emuxki_channel *chan,
443 			   emuxki_chanparms_fxsend *fxsend)
444 {
445 	/* Could do a memcpy ...*/
446 	chan->fxsend.a.level = fxsend->a.level;
447 	chan->fxsend.b.level = fxsend->b.level;
448 	chan->fxsend.c.level = fxsend->c.level;
449 	chan->fxsend.d.level = fxsend->d.level;
450 	chan->fxsend.a.dest = fxsend->a.dest;
451 	chan->fxsend.b.dest = fxsend->b.dest;
452 	chan->fxsend.c.dest = fxsend->c.dest;
453 	chan->fxsend.d.dest = fxsend->d.dest;
454 
455 	/* for audigy */
456 	chan->fxsend.e.level = fxsend->e.level;
457 	chan->fxsend.f.level = fxsend->f.level;
458 	chan->fxsend.g.level = fxsend->g.level;
459 	chan->fxsend.h.level = fxsend->h.level;
460 	chan->fxsend.e.dest = fxsend->e.dest;
461 	chan->fxsend.f.dest = fxsend->f.dest;
462 	chan->fxsend.g.dest = fxsend->g.dest;
463 	chan->fxsend.h.dest = fxsend->h.dest;
464 }
465 
466 
467 static void
468 emuxki_channel_set_srate(emuxki_channel *chan, uint32 srate)
469 {
470 	chan->pitch.target = (srate << 8) / 375;
471 	chan->pitch.target = (chan->pitch.target >> 1) +
472 		(chan->pitch.target & 1);
473 	chan->pitch.target &= 0xffff;
474 	chan->pitch.current = chan->pitch.target;
475 	chan->pitch.intial =
476 		(emuxki_rate_to_pitch(srate) >> 8) & EMU_CHAN_IP_MASK;
477 }
478 
479 
480 /* voice params must be set before calling this */
481 static void
482 emuxki_channel_set_bufparms(emuxki_channel *chan,
483 			     uint32 start, uint32 end)
484 {
485 	chan->loop.start = start & EMU_CHAN_PSST_LOOPSTARTADDR_MASK;
486 	chan->loop.end = end & EMU_CHAN_DSL_LOOPENDADDR_MASK;
487 }
488 
489 
490 static void
491 emuxki_channel_commit_fx(emuxki_channel *chan)
492 {
493 	emuxki_dev   	*card = chan->voice->stream->card;
494 	uint8			chano = chan->num;
495 
496 	if (IS_AUDIGY(&card->config)) {
497 		emuxki_chan_write(&card->config, chano, 0x4c, 0);
498 		emuxki_chan_write(&card->config, chano, 0x4d, 0);
499 		emuxki_chan_write(&card->config, chano, 0x4e, 0);
500 		emuxki_chan_write(&card->config, chano, 0x4f, 0);
501 
502 		emuxki_chan_write(&card->config, chano, EMU_A_CHAN_FXRT1,
503 			      (chan->fxsend.d.dest << 24) |
504 			      (chan->fxsend.c.dest << 16) |
505 			      (chan->fxsend.b.dest << 8) |
506 			      (chan->fxsend.a.dest));
507 		emuxki_chan_write(&card->config, chano, EMU_A_CHAN_FXRT2,
508 			      (chan->fxsend.h.dest << 24) |
509 			      (chan->fxsend.g.dest << 16) |
510 			      (chan->fxsend.f.dest << 8) |
511 			      (chan->fxsend.e.dest));
512 		emuxki_chan_write(&card->config, chano, EMU_A_CHAN_SENDAMOUNTS,
513 			      (chan->fxsend.e.level << 24) |
514 			      (chan->fxsend.f.level << 16) |
515 			      (chan->fxsend.g.level << 8) |
516 			      (chan->fxsend.h.level));
517 	} else {
518 		emuxki_chan_write(&card->config, chano, EMU_CHAN_FXRT,
519 			      (chan->fxsend.d.dest << 28) |
520 			      (chan->fxsend.c.dest << 24) |
521 			      (chan->fxsend.b.dest << 20) |
522 			      (chan->fxsend.a.dest << 16));
523 	}
524 
525 	emuxki_chan_write(&card->config, chano, 0x10000000 | EMU_CHAN_PTRX,
526 		      (chan->fxsend.a.level << 8) | chan->fxsend.b.level);
527 	emuxki_chan_write(&card->config, chano, EMU_CHAN_DSL,
528 		      (chan->fxsend.d.level << 24) | chan->loop.end);
529 	emuxki_chan_write(&card->config, chano, EMU_CHAN_PSST,
530 		      (chan->fxsend.c.level << 24) | chan->loop.start);
531 }
532 
533 
534 static void
535 emuxki_channel_commit_parms(emuxki_channel *chan)
536 {
537 	emuxki_voice *voice = chan->voice;
538 	emuxki_dev   	*card = chan->voice->stream->card;
539 	uint32			start, mapval;
540 	uint8			chano = chan->num;
541 	//int             s;
542 
543 	start = chan->loop.start +
544 		(voice->stereo ? 28 : 30) * (voice->b16 + 1);
545 	mapval = ((uint32)card->silentpage_phy_base) << 1 | EMU_CHAN_MAP_PTI_MASK;
546 
547 	//s = splaudio();
548 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CPF_STEREO, voice->stereo);
549 
550 	emuxki_channel_commit_fx(chan);
551 
552 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CCCA,
553 		      (chan->filter.lowpass_resonance_height << 28) |
554 		      (chan->filter.interpolation_ROM << 25) |
555 		   (voice->b16 ? 0 : EMU_CHAN_CCCA_8BITSELECT) | start);
556 	emuxki_chan_write(&card->config, chano, EMU_CHAN_Z1, 0);
557 	emuxki_chan_write(&card->config, chano, EMU_CHAN_Z2, 0);
558 	emuxki_chan_write(&card->config, chano, EMU_CHAN_MAPA, mapval);
559 	emuxki_chan_write(&card->config, chano, EMU_CHAN_MAPB, mapval);
560 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CVCF_CURRFILTER,
561 		      chan->filter.current_cutoff_frequency);
562 	emuxki_chan_write(&card->config, chano, EMU_CHAN_VTFT_FILTERTARGET,
563 		      chan->filter.target_cutoff_frequency);
564 	emuxki_chan_write(&card->config, chano, EMU_CHAN_ATKHLDM,
565 		      (chan->modulation.envelope.hold_time << 8) |
566 		      chan->modulation.envelope.attack_time);
567 	emuxki_chan_write(&card->config, chano, EMU_CHAN_DCYSUSM,
568 		      (chan->modulation.envelope.sustain_level << 8) |
569 		      chan->modulation.envelope.decay_time);
570 	emuxki_chan_write(&card->config, chano, EMU_CHAN_LFOVAL1,
571 		      chan->modulation.LFO_state);
572 	emuxki_chan_write(&card->config, chano, EMU_CHAN_LFOVAL2,
573 		      chan->vibrato_LFO.state);
574 	emuxki_chan_write(&card->config, chano, EMU_CHAN_FMMOD,
575 		      (chan->vibrato_LFO.modulation_depth << 8) |
576 		      chan->filter.LFO_modulation_depth);
577 	emuxki_chan_write(&card->config, chano, EMU_CHAN_TREMFRQ,
578 		      (chan->tremolo_depth << 8));
579 	emuxki_chan_write(&card->config, chano, EMU_CHAN_FM2FRQ2,
580 		      (chan->vibrato_LFO.vibrato_depth << 8) |
581 		      chan->vibrato_LFO.frequency);
582 	emuxki_chan_write(&card->config, chano, EMU_CHAN_ENVVAL,
583 		      chan->modulation.envelope.current_state);
584 	emuxki_chan_write(&card->config, chano, EMU_CHAN_ATKHLDV,
585 		      (chan->volume.envelope.hold_time << 8) |
586 		      chan->volume.envelope.attack_time);
587 	emuxki_chan_write(&card->config, chano, EMU_CHAN_ENVVOL,
588 		      chan->volume.envelope.current_state);
589 	emuxki_chan_write(&card->config, chano, EMU_CHAN_PEFE,
590 		      (chan->pitch.envelope_amount << 8) |
591 		      chan->filter.envelope_amount);
592 	//splx(s);
593 }
594 
595 
596 static void
597 emuxki_channel_start(emuxki_channel *chan)
598 {
599 	emuxki_voice *voice = chan->voice;
600 	emuxki_dev   *card = chan->voice->stream->card;
601 	uint8        cache_sample, cache_invalid_size, chano = chan->num;
602 	uint32       sample;
603 	//int             s;
604 
605 	cache_sample = voice->stereo ? 4 : 2;
606 	sample = voice->b16 ? 0x00000000 : 0x80808080;
607 	cache_invalid_size = (voice->stereo ? 28 : 30) * (voice->b16 + 1);
608 
609 	//s = splaudio();
610 	while (cache_sample--)
611 		emuxki_chan_write(&card->config, chano,
612 			      EMU_CHAN_CD0 + cache_sample, sample);
613 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE, 0);
614 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CCR_READADDRESS, 64);
615 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE,
616 		      cache_invalid_size);
617 	emuxki_chan_write(&card->config, chano, EMU_CHAN_IFATN,
618 		      (chan->filter.target_cutoff_frequency << 8) |
619 		      chan->initial_attenuation);
620 	emuxki_chan_write(&card->config, chano, EMU_CHAN_VTFT_VOLUMETARGET,
621 		      chan->volume.target);
622 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CVCF_CURRVOL,
623 		      chan->volume.current);
624 	emuxki_chan_write(&card->config, 0, EMU_MKSUBREG(1, chano,
625 					       EMU_SOLEL + (chano >> 5)),
626 		      0);	/* Clear stop on loop */
627 	emuxki_chan_write(&card->config, 0, EMU_MKSUBREG(1, chano,
628 					       EMU_CLIEL + (chano >> 5)),
629 		      0);	/* Clear loop interrupt */
630 	emuxki_chan_write(&card->config, chano, EMU_CHAN_DCYSUSV,
631 		      (chan->volume.envelope.sustain_level << 8) |
632 		      chan->volume.envelope.decay_time);
633 	emuxki_chan_write(&card->config, chano, EMU_CHAN_PTRX_PITCHTARGET,
634 		      chan->pitch.target);
635 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CPF_PITCH,
636 		      chan->pitch.current);
637 	emuxki_chan_write(&card->config, chano, EMU_CHAN_IP, chan->pitch.intial);
638 
639 	//splx(s);
640 }
641 
642 
643 static void
644 emuxki_channel_stop(emuxki_channel *chan)
645 {
646 	emuxki_dev   	*card = chan->voice->stream->card;
647 	//int             s;
648 	uint8           chano = chan->num;
649 
650 	//s = splaudio();
651 
652 	emuxki_chan_write(&card->config, chano, EMU_CHAN_PTRX_PITCHTARGET, 0);
653 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CPF_PITCH, 0);
654 	emuxki_chan_write(&card->config, chano, EMU_CHAN_IFATN_ATTENUATION, 0xff);
655 	emuxki_chan_write(&card->config, chano, EMU_CHAN_VTFT_VOLUMETARGET, 0);
656 	emuxki_chan_write(&card->config, chano, EMU_CHAN_CVCF_CURRVOL, 0);
657 	emuxki_chan_write(&card->config, chano, EMU_CHAN_IP, 0);
658 
659 	//splx(s);
660 }
661 
662 
663 /*	Emuxki voice functions */
664 /*static void
665 emuxki_dump_voice(emuxki_voice *voice)
666 {
667 	LOG(("voice->use = %#u\n", voice->use));
668 	LOG(("voice->state = %#u\n", voice->state));
669 	LOG(("voice->stereo = %#u\n", voice->stereo));
670 	LOG(("voice->b16 = %#u\n", voice->b16));
671 	LOG(("voice->sample_rate = %#lu\n", voice->sample_rate));
672 	LOG(("voice->buffer = %#08x\n", voice->buffer));
673 	if (voice->buffer) {
674 		LOG(("voice->buffer->ptbidx = %#u\n", voice->buffer->ptbidx));
675 		LOG(("voice->buffer->log_base = %#08x\n", voice->buffer->log_base));
676 		LOG(("voice->buffer->phy_base = %#08x\n", voice->buffer->phy_base));
677 		LOG(("voice->buffer->size = %#08x\n", voice->buffer->size));
678 		LOG(("voice->buffer->area = %#08x\n", voice->buffer->area));
679 	}
680 	LOG(("voice->blksize = %#u\n", voice->blksize));
681 	LOG(("voice->trigblk = %#u\n", voice->trigblk));
682 	LOG(("voice->blkmod = %#u\n", voice->blkmod));
683 	LOG(("voice->timerate = %#u\n", voice->timerate));
684 }*/
685 
686 
687 /* Allocate channels for voice in case of play voice */
688 static status_t
689 emuxki_voice_channel_create(emuxki_voice *voice)
690 {
691 	emuxki_channel **channel = voice->stream->card->channel;
692 	uint8	        i, stereo = voice->stereo;
693 	//int             s;
694 
695 	for (i = 0; i < EMU_NUMCHAN - stereo; i += stereo + 1) {
696 		if ((stereo && (channel[i + 1] != NULL)) ||
697 		    (channel[i] != NULL))	/* Looking for free channels */
698 			continue;
699 		//s = splaudio();
700 		if (stereo) {
701 			voice->dataloc.chan[1] =
702 				emuxki_channel_new(voice, i + 1);
703 			if (voice->dataloc.chan[1] == NULL) {
704 				//splx(s);
705 				return ENOMEM;
706 			}
707 		}
708 		voice->dataloc.chan[0] = emuxki_channel_new(voice, i);
709 		if (voice->dataloc.chan[0] == NULL) {
710 			if (stereo) {
711 				emuxki_channel_delete(voice->dataloc.chan[1]);
712 				voice->dataloc.chan[1] = NULL;
713 			}
714 			//splx(s);
715 			return ENOMEM;
716 		}
717 		//splx(s);
718 		return B_OK;
719 	}
720 	return EAGAIN;
721 }
722 
723 
724 /* When calling this function we assume no one can access the voice */
725 static void
726 emuxki_voice_channel_destroy(emuxki_voice *voice)
727 {
728 	emuxki_channel_delete(voice->dataloc.chan[0]);
729 	voice->dataloc.chan[0] = NULL;
730 	if (voice->stereo)
731 		emuxki_channel_delete(voice->dataloc.chan[1]);
732 	voice->dataloc.chan[1] = NULL;
733 }
734 
735 
736 static status_t
737 emuxki_voice_dataloc_create(emuxki_voice *voice)
738 {
739 	status_t	error;
740 
741 	if (voice->use & EMU_USE_PLAY) {
742 		if ((error = emuxki_voice_channel_create(voice)))
743 			return (error);
744 	} else {
745 
746 	}
747 	return B_OK;
748 }
749 
750 
751 static void
752 emuxki_voice_dataloc_destroy(emuxki_voice *voice)
753 {
754 	if (voice->use & EMU_USE_PLAY) {
755 		if (voice->dataloc.chan[0] != NULL)
756 			emuxki_voice_channel_destroy(voice);
757 	} else {
758 		uint32 buffaddr_reg, buffsize_reg;
759 		switch (voice->dataloc.source) {
760 			case EMU_RECSRC_MIC:
761 				buffaddr_reg = EMU_MICBA;
762 				buffsize_reg = EMU_MICBS;
763 				break;
764 			case EMU_RECSRC_ADC:
765 				buffaddr_reg = EMU_ADCBA;
766 				buffsize_reg = EMU_ADCBS;
767 				break;
768 			case EMU_RECSRC_FX:
769 				buffaddr_reg = EMU_FXBA;
770 				buffsize_reg = EMU_FXBS;
771 				break;
772 			default:
773 				return;
774 		}
775 		emuxki_chan_write(&voice->stream->card->config, 0, buffaddr_reg, 0);
776 		emuxki_chan_write(&voice->stream->card->config, 0, buffsize_reg,
777 			EMU_RECBS_BUFSIZE_NONE);
778 	}
779 }
780 
781 
782 static void
783 emuxki_voice_fxupdate(emuxki_voice *voice)
784 {
785 	emuxki_chanparms_fxsend fxsend;
786 
787 	uint8 maxlevel =
788 		IS_AUDIGY(&voice->stream->card->config) ? 0xc0 : 0xff;	/* not max */
789 
790 	if (voice->use & EMU_USE_PLAY) {
791 		fxsend.a.dest = 0x3f;
792 		fxsend.b.dest = 0x3f;
793 		fxsend.c.dest = 0x3f;
794 		fxsend.d.dest = 0x3f;
795 		/* for audigy */
796 		fxsend.e.dest = 0x3f;
797 		fxsend.f.dest = 0x3f;
798 		fxsend.g.dest = 0x3f;
799 		fxsend.h.dest = 0x3f;
800 
801 		fxsend.a.level = fxsend.b.level = fxsend.c.level = fxsend.d.level =
802 		fxsend.e.level = fxsend.g.level = fxsend.f.level = fxsend.h.level = 0x00;
803 
804 		if (voice->stereo) {
805 			switch(voice->stream->card->play_mode) {
806 				case 2:
807 					if (voice->stream->nstereo == 1) {
808 						fxsend.a.dest = voice->voicenum * 2;
809 						fxsend.a.level = maxlevel;
810 					} else if ((voice->stream->nstereo == 2) ||
811 						((voice->stream->nstereo == 3)&&(voice->voicenum < 2))) {
812 						fxsend.a.dest = voice->voicenum * 2;
813 						fxsend.a.level = maxlevel;
814 						if (voice->voicenum > 1 - 1)
815 							fxsend.a.dest-=2;
816 					} else if (voice->stream->nstereo == 3 && voice->voicenum > 1) {
817 							fxsend.a.dest = 0x0;
818 							fxsend.a.level = maxlevel / 2;
819 							fxsend.b.dest = 0x1;
820 							fxsend.b.level = maxlevel / 2;
821 					} else {
822 						LOG(("emuxki_voice_set_stereo case 2 badly managed\n"));
823 					}
824 					break;
825 				case 4:
826 					if (voice->stream->nstereo == 1) {
827 						fxsend.a.dest = voice->voicenum * 2;
828 						fxsend.a.level = maxlevel;
829 						fxsend.b.dest = voice->voicenum * 2 + 2;
830 						fxsend.b.level = maxlevel;
831 					} else if ((voice->stream->nstereo == 2) ||
832 						((voice->stream->nstereo == 3)&&(voice->voicenum < 2))) {
833 						fxsend.a.dest = voice->voicenum * 2;
834 						fxsend.a.level = maxlevel;
835 					} else if (voice->stream->nstereo == 3 && voice->voicenum > 1) {
836 						fxsend.a.dest = 0x0;
837 						fxsend.a.level = maxlevel / 2;
838 						fxsend.b.dest = 0x1;
839 						fxsend.b.level = maxlevel / 2;
840 					} else {
841 						LOG(("emuxki_voice_set_stereo case 4 badly managed\n"));
842 					}
843 					break;
844 				case 6: // only on audigy
845 					if (voice->stream->nstereo == 1) {
846 						fxsend.a.dest = voice->voicenum * 2;
847 						fxsend.a.level = maxlevel;
848 						fxsend.b.dest = voice->voicenum * 2 + 2;
849 						fxsend.b.level = maxlevel;
850 						fxsend.c.dest = 0x4;
851 						fxsend.c.level = maxlevel / 2;
852 						fxsend.d.dest = 0x5;
853 						fxsend.d.level = maxlevel / 2;
854 					} else if (voice->stream->nstereo == 2) {
855 						fxsend.a.dest = voice->voicenum * 2;
856 						fxsend.a.level = maxlevel;
857 						if (voice->voicenum < 1) {
858 							fxsend.b.dest = 0x4;
859 							fxsend.b.level = maxlevel / 2;
860 							fxsend.c.dest = 0x5;
861 							fxsend.c.level = maxlevel / 2;
862 						}
863 					} else if (voice->stream->nstereo == 3) {
864 						fxsend.a.dest = voice->voicenum * 2;
865 						fxsend.a.level = maxlevel;
866 					} else {
867 						LOG(("emuxki_voice_set_stereo case 6 badly managed\n"));
868 					}
869 					break;
870 			}
871 
872 			emuxki_channel_set_fxsend(voice->dataloc.chan[0],
873 						   &fxsend);
874 
875 			switch(voice->stream->card->play_mode) {
876 				case 2:
877 					if (voice->stream->nstereo == 1) {
878 						fxsend.a.dest = voice->voicenum * 2 + 1;
879 						fxsend.a.level = maxlevel;
880 					} else if ((voice->stream->nstereo == 2) ||
881 						((voice->stream->nstereo == 3)&&(voice->voicenum < 2))) {
882 						fxsend.a.dest = voice->voicenum * 2 + 1;
883 						fxsend.a.level = maxlevel;
884 						if (voice->voicenum > 1 - 1)
885 							fxsend.a.dest-=2;
886 					} else if (voice->stream->nstereo == 3 && voice->voicenum > 1) {
887 							fxsend.a.dest = 0x0;
888 							fxsend.a.level = maxlevel / 2;
889 							fxsend.b.dest = 0x1;
890 							fxsend.b.level = maxlevel / 2;
891 					} else {
892 						LOG(("emuxki_voice_set_stereo case 2 badly managed\n"));
893 					}
894 					break;
895 				case 4:
896 					if (voice->stream->nstereo == 1) {
897 						fxsend.a.dest = voice->voicenum * 2 + 1;
898 						fxsend.a.level = maxlevel;
899 						fxsend.b.dest = voice->voicenum * 2 + 3;
900 						fxsend.b.level = maxlevel;
901 					} else if ((voice->stream->nstereo == 2) ||
902 						((voice->stream->nstereo == 3)&&(voice->voicenum < 2))) {
903 						fxsend.a.dest = voice->voicenum * 2 + 1;
904 						fxsend.a.level = maxlevel;
905 					} else if (voice->stream->nstereo == 3 && voice->voicenum > 1) {
906 						fxsend.a.dest = 0x0;
907 						fxsend.a.level = maxlevel / 2;
908 						fxsend.b.dest = 0x1;
909 						fxsend.b.level = maxlevel / 2;
910 					} else {
911 						LOG(("emuxki_voice_set_stereo case 4 badly managed\n"));
912 					}
913 					break;
914 				case 6: // only on audigy
915 					if (voice->stream->nstereo == 1) {
916 						fxsend.a.dest = voice->voicenum * 2 + 1;
917 						fxsend.a.level = maxlevel;
918 						fxsend.b.dest = voice->voicenum * 2 + 3;
919 						fxsend.b.level = maxlevel;
920 						fxsend.c.dest = 0x4;
921 						fxsend.c.level = maxlevel / 2;
922 						fxsend.d.dest = 0x5;
923 						fxsend.d.level = maxlevel / 2;
924 					} else if (voice->stream->nstereo == 2) {
925 						fxsend.a.dest = voice->voicenum * 2 + 1;
926 						fxsend.a.level = maxlevel;
927 						if (voice->voicenum < 1) {
928 							fxsend.b.dest = 0x4;
929 							fxsend.b.level = maxlevel / 2;
930 							fxsend.c.dest = 0x5;
931 							fxsend.c.level = maxlevel / 2;
932 						}
933 					} else if (voice->stream->nstereo == 3) {
934 						fxsend.a.dest = voice->voicenum * 2 + 1;
935 						fxsend.a.level = maxlevel;
936 					} else {
937 						LOG(("emuxki_voice_set_stereo case 6 badly managed\n"));
938 					}
939 					break;
940 			}
941 
942 			emuxki_channel_set_fxsend(voice->dataloc.chan[1],
943 						   &fxsend);
944 		} else {
945 			switch(voice->stream->card->play_mode) {
946 				case 2:
947 					if (voice->stream->nmono == 1) {
948 						fxsend.a.dest = voice->voicenum;
949 						fxsend.a.level = maxlevel;
950 						fxsend.b.dest = voice->voicenum + 1;
951 						fxsend.b.level = maxlevel;
952 					} else if (voice->stream->nmono == 2) {
953 						fxsend.a.dest = voice->voicenum;
954 						fxsend.a.level = maxlevel;
955 					} else if ((voice->stream->nmono == 4) ||
956 						((voice->stream->nmono == 6)&&(voice->voicenum < 4))) {
957 						fxsend.a.dest = voice->voicenum;
958 						fxsend.a.level = maxlevel;
959 						if (voice->voicenum > 2 - 1)
960 							fxsend.a.dest-=2;
961 					} else if (voice->stream->nmono == 6 && voice->voicenum > 3) {
962 							fxsend.a.dest = 0x0;
963 							fxsend.a.level = maxlevel / 2;
964 							fxsend.b.dest = 0x1;
965 							fxsend.b.level = maxlevel / 2;
966 					} else {
967 						LOG(("emuxki_voice_set_stereo case 2 badly managed\n"));
968 					}
969 					break;
970 				case 4:
971 					if (voice->stream->nmono == 1) {
972 						fxsend.a.dest = voice->voicenum;
973 						fxsend.a.level = maxlevel;
974 						fxsend.b.dest = voice->voicenum + 1;
975 						fxsend.b.level = maxlevel;
976 						fxsend.c.dest = voice->voicenum + 2;
977 						fxsend.c.level = maxlevel;
978 						fxsend.d.dest = voice->voicenum + 3;
979 						fxsend.d.level = maxlevel;
980 					} else if (voice->stream->nmono == 2) {
981 						fxsend.a.dest = voice->voicenum;
982 						fxsend.a.level = maxlevel;
983 						fxsend.b.dest = voice->voicenum + 2;
984 						fxsend.b.level = maxlevel;
985 					} else if ((voice->stream->nmono == 4) ||
986 						((voice->stream->nmono == 6)&&(voice->voicenum < 4))) {
987 						fxsend.a.dest = voice->voicenum;
988 						fxsend.a.level = maxlevel;
989 					} else if (voice->stream->nmono == 6 && voice->voicenum > 3) {
990 							fxsend.a.dest = 0x0;
991 							fxsend.a.level = maxlevel / 2;
992 							fxsend.b.dest = 0x1;
993 							fxsend.b.level = maxlevel / 2;
994 					} else {
995 						LOG(("emuxki_voice_set_stereo case 4 badly managed\n"));
996 					}
997 					break;
998 				case 6: // only on audigy
999 					if (voice->stream->nmono == 1) {
1000 						fxsend.a.dest = voice->voicenum;
1001 						fxsend.a.level = maxlevel;
1002 						fxsend.b.dest = voice->voicenum + 1;
1003 						fxsend.b.level = maxlevel;
1004 						fxsend.c.dest = voice->voicenum + 2;
1005 						fxsend.c.level = maxlevel;
1006 						fxsend.d.dest = voice->voicenum + 3;
1007 						fxsend.d.level = maxlevel;
1008 						fxsend.e.dest = voice->voicenum + 4;
1009 						fxsend.e.level = maxlevel;
1010 						fxsend.f.dest = voice->voicenum + 5;
1011 						fxsend.f.level = maxlevel;
1012 					} else if (voice->stream->nmono == 2) {
1013 						fxsend.a.dest = voice->voicenum;
1014 						fxsend.a.level = maxlevel;
1015 						fxsend.b.dest = voice->voicenum + 2;
1016 						fxsend.b.level = maxlevel;
1017 						fxsend.c.dest = 0x4;
1018 						fxsend.c.level = maxlevel / 2;
1019 						fxsend.d.dest = 0x5;
1020 						fxsend.d.level = maxlevel / 2;
1021 					} else if (voice->stream->nmono == 4) {
1022 						fxsend.a.dest = voice->voicenum;
1023 						fxsend.a.level = maxlevel;
1024 						if (voice->voicenum < 2) {
1025 							fxsend.b.dest = 0x4;
1026 							fxsend.b.level = maxlevel / 2;
1027 							fxsend.c.dest = 0x5;
1028 							fxsend.c.level = maxlevel / 2;
1029 						}
1030 					} else if (voice->stream->nmono == 6) {
1031 						fxsend.a.dest = voice->voicenum;
1032 						fxsend.a.level = maxlevel;
1033 					} else {
1034 						LOG(("emuxki_voice_set_stereo case 6 badly managed\n"));
1035 					}
1036 					break;
1037 			}
1038 
1039 			emuxki_channel_set_fxsend(voice->dataloc.chan[0],
1040 						   &fxsend);
1041 		}
1042 	}
1043 }
1044 
1045 
1046 static status_t
1047 emuxki_voice_set_stereo(emuxki_voice *voice, uint8 stereo)
1048 {
1049 	status_t	error;
1050 
1051 	emuxki_voice_dataloc_destroy(voice);
1052 	voice->stereo = stereo;
1053 	if ((error = emuxki_voice_dataloc_create(voice)))
1054 	  return error;
1055 	emuxki_voice_fxupdate(voice);
1056 	return B_OK;
1057 }
1058 
1059 
1060 static status_t
1061 emuxki_voice_set_srate(emuxki_voice *voice, uint32 srate)
1062 {
1063 	voice->sample_rate = srate;
1064 	if (voice->use & EMU_USE_PLAY) {
1065 		if ((srate < 4000) || (srate > 48000))
1066 			return (EINVAL);
1067 		emuxki_channel_set_srate(voice->dataloc.chan[0], srate);
1068 		if (voice->stereo)
1069 			emuxki_channel_set_srate(voice->dataloc.chan[1],
1070 						  srate);
1071 	}
1072 	return B_OK;
1073 }
1074 
1075 
1076 status_t
1077 emuxki_voice_set_audioparms(emuxki_voice *voice, uint8 stereo,
1078 			     uint8 b16, uint32 srate)
1079 {
1080 	status_t             error;
1081 
1082 	if (voice->stereo == stereo && voice->b16 == b16 &&
1083 	    voice->sample_rate == srate)
1084 		return B_OK;
1085 
1086 	if (voice->stereo != stereo) {
1087 		if ((error = emuxki_voice_set_stereo(voice, stereo)))
1088 			return error;
1089 	}
1090 	voice->b16 = b16;
1091 	if (voice->sample_rate != srate)
1092 		emuxki_voice_set_srate(voice, srate);
1093 	return B_OK;
1094 }
1095 
1096 
1097 status_t
1098 emuxki_voice_set_recparms(emuxki_voice *voice, emuxki_recsrc_t recsrc,
1099 			     	emuxki_recparams *recparams)
1100 {
1101 	if (voice->use & EMU_USE_RECORD) {
1102 		switch(recsrc) {
1103 			case EMU_RECSRC_MIC:
1104 				break;
1105 			case EMU_RECSRC_ADC:
1106 				break;
1107 			case EMU_RECSRC_FX:
1108 				if (!recparams)
1109 					return B_ERROR;
1110 				voice->recparams.efx_voices[0] = recparams->efx_voices[0];
1111 				voice->recparams.efx_voices[1] = recparams->efx_voices[1];
1112 				break;
1113 			default:
1114 				return B_ERROR;
1115 				break;
1116 		}
1117 		voice->dataloc.source = recsrc;
1118 	}
1119 	return B_OK;
1120 }
1121 
1122 
1123 /* voice audio parms (see just before) must be set prior to this */
1124 status_t
1125 emuxki_voice_set_bufparms(emuxki_voice *voice, void *ptr,
1126 			   uint32 bufsize, uint16 blksize)
1127 {
1128 	emuxki_mem *mem;
1129 	emuxki_channel **chan;
1130 	uint32 start, end;
1131 	uint8 sample_size;
1132 	status_t error = EFAULT;
1133 
1134 	LIST_FOREACH(mem, &voice->stream->card->mem, next) {
1135 		if (mem->log_base != ptr)
1136 			continue;
1137 
1138 		voice->buffer = mem;
1139 		sample_size = (voice->b16 + 1) * (voice->stereo + 1);
1140 		voice->trigblk = 0;	/* This shouldn't be needed */
1141 		voice->blkmod = bufsize / blksize;
1142 		if (bufsize % blksize) 	  /* This should not happen */
1143 			voice->blkmod++;
1144 		error = 0;
1145 
1146 		if (voice->use & EMU_USE_PLAY) {
1147 			voice->blksize = blksize / sample_size;
1148 			chan = voice->dataloc.chan;
1149 			start = (mem->ptbidx << 12) / sample_size;
1150 			end = start + bufsize / sample_size;
1151 			emuxki_channel_set_bufparms(chan[0],
1152 						     start, end);
1153 			if (voice->stereo)
1154 				emuxki_channel_set_bufparms(chan[1],
1155 				     start, end);
1156 			voice->timerate = (uint32) 48000 *
1157 			                voice->blksize / voice->sample_rate / 2;
1158 			if (voice->timerate < 5)
1159 				error = EINVAL;
1160 		} else {
1161 			voice->blksize = blksize;
1162 		}
1163 
1164 		break;
1165 	}
1166 
1167 	return error;
1168 }
1169 
1170 
1171 status_t
1172 emuxki_voice_commit_parms(emuxki_voice *voice)
1173 {
1174 	if (voice->use & EMU_USE_PLAY) {
1175 		emuxki_channel_commit_parms(voice->dataloc.chan[0]);
1176 		if (voice->stereo)
1177 			emuxki_channel_commit_parms(voice->dataloc.chan[1]);
1178 	} else {
1179 		uint32 buffaddr_reg, buffsize_reg, idx_reg;
1180 		switch (voice->dataloc.source) {
1181 			case EMU_RECSRC_MIC:
1182 				buffaddr_reg = EMU_MICBA;
1183 				buffsize_reg = EMU_MICBS;
1184 				idx_reg = EMU_MICIDX;
1185 				break;
1186 			case EMU_RECSRC_ADC:
1187 				buffaddr_reg = EMU_ADCBA;
1188 				buffsize_reg = EMU_ADCBS;
1189 				idx_reg = EMU_ADCIDX;
1190 				break;
1191 			case EMU_RECSRC_FX:
1192 				buffaddr_reg = EMU_FXBA;
1193 				buffsize_reg = EMU_FXBS;
1194 				idx_reg = EMU_FXIDX;
1195 				break;
1196 			default:
1197 				return B_ERROR;
1198 		}
1199 		emuxki_chan_write(&voice->stream->card->config, 0, buffaddr_reg, (uint32)voice->buffer->phy_base);
1200 		emuxki_chan_write(&voice->stream->card->config, 0, buffsize_reg, EMU_RECBS_BUFSIZE_NONE);
1201 		emuxki_chan_write(&voice->stream->card->config, 0, buffsize_reg, EMU_RECBS_BUFSIZE_4096);
1202 
1203 		LOG(("emuxki_voice_commit_parms idx_reg : %u\n", idx_reg));
1204 
1205 		idx_reg = EMU_RECIDX(idx_reg);
1206 		while (emuxki_chan_read(&voice->stream->card->config, 0, idx_reg))
1207 			snooze(5);
1208 	}
1209 	return B_OK;
1210 }
1211 
1212 
1213 static uint32
1214 emuxki_voice_curaddr(emuxki_voice *voice)
1215 {
1216 	if (voice->use & EMU_USE_PLAY)
1217 		return (emuxki_chan_read(&voice->stream->card->config,
1218 				     voice->dataloc.chan[0]->num,
1219 				     EMU_CHAN_CCCA_CURRADDR) -
1220 			voice->dataloc.chan[0]->loop.start);
1221 	else {
1222 		uint32 idx_reg = 0;
1223 		switch (voice->dataloc.source) {
1224 			case EMU_RECSRC_MIC:
1225 				idx_reg = IS_AUDIGY(&voice->stream->card->config) ? EMU_A_MICIDX : EMU_MICIDX;
1226 				break;
1227 			case EMU_RECSRC_ADC:
1228 				idx_reg = IS_AUDIGY(&voice->stream->card->config) ? EMU_A_ADCIDX : EMU_ADCIDX;
1229 				break;
1230 			case EMU_RECSRC_FX:
1231 				idx_reg = EMU_FXIDX;
1232 				break;
1233 			default:
1234 				TRACE(("emuxki_voice_curaddr : BUG!!\n"));
1235 		}
1236 		//TRACE(("emuxki_voice_curaddr idx_reg : %u\n", idx_reg));
1237 		//TRACE(("emuxki_voice_curaddr : %lu\n", emuxki_chan_read(&voice->stream->card->config, 0, idx_reg)));
1238 		return emuxki_chan_read(&voice->stream->card->config,
1239 				     0,
1240 				     idx_reg);
1241 	}
1242 }
1243 
1244 
1245 static void
1246 emuxki_resched_timer(emuxki_dev *card)
1247 {
1248 	emuxki_voice 	*voice;
1249 	emuxki_stream 	*stream;
1250 	uint16			timerate = 1024;
1251 	uint8			active = 0;
1252 	//int             s = splaudio();
1253 
1254 	LOG(("emuxki_resched_timer begin\n"));
1255 
1256 	LIST_FOREACH(stream, &card->streams, next) {
1257 		LIST_FOREACH(voice, &stream->voices, next) {
1258 			if ((voice->use & EMU_USE_PLAY) == 0 ||
1259 				(voice->state & EMU_STATE_STARTED) == 0)
1260 				continue;
1261 			active = 1;
1262 			if (voice->timerate < timerate)
1263 				timerate = voice->timerate;
1264 		}
1265 	}
1266 	if (timerate & ~EMU_TIMER_RATE_MASK)
1267 		timerate = 0;
1268 
1269 	if (card->timerate > timerate) {
1270 		LOG(("emuxki_resched_timer written (old %u, new %u)\n", card->timerate, timerate));
1271 		card->timerate = timerate;
1272 		emuxki_reg_write_16(&card->config, EMU_TIMER, timerate);
1273 	}
1274 	if (!active && (card->timerstate & EMU_TIMER_STATE_ENABLED)) {
1275 		emuxki_inte_disable(&card->config, EMU_INTE_INTERTIMERENB);
1276 		card->timerstate &= ~EMU_TIMER_STATE_ENABLED;
1277 		LOG(("emuxki_resched_timer : timer disabled\n"));
1278 	} else
1279 	  if (active && !(card->timerstate & EMU_TIMER_STATE_ENABLED)) {
1280 	  	emuxki_inte_enable(&card->config, EMU_INTE_INTERTIMERENB);
1281 		card->timerstate |= EMU_TIMER_STATE_ENABLED;
1282 		LOG(("emuxki_resched_timer : timer enabled\n"));
1283 	  }
1284 	LOG(("emuxki_resched_timer state : %x\n", card->timerstate));
1285 	//splx(s);
1286 }
1287 
1288 
1289 static uint32
1290 emuxki_voice_adc_rate(emuxki_voice *voice)
1291 {
1292 	switch(voice->sample_rate) {
1293 		case 48000:
1294 			return EMU_ADCCR_SAMPLERATE_48;
1295 			break;
1296 		case 44100:
1297 			return EMU_ADCCR_SAMPLERATE_44;
1298 			break;
1299 		case 32000:
1300 			return EMU_ADCCR_SAMPLERATE_32;
1301 			break;
1302 		case 24000:
1303 			return EMU_ADCCR_SAMPLERATE_24;
1304 			break;
1305 		case 22050:
1306 			return EMU_ADCCR_SAMPLERATE_22;
1307 			break;
1308 		case 16000:
1309 			return EMU_ADCCR_SAMPLERATE_16;
1310 			break;
1311 		case 12000:
1312 			if (IS_AUDIGY(&voice->stream->card->config))
1313 				return EMU_A_ADCCR_SAMPLERATE_12;
1314 			else
1315 				PRINT(("recording sample_rate not supported : %" B_PRIu32 "\n",
1316 					voice->sample_rate));
1317 			break;
1318 		case 11000:
1319 			if (IS_AUDIGY(&voice->stream->card->config))
1320 				return EMU_A_ADCCR_SAMPLERATE_11;
1321 			else
1322 				return EMU_ADCCR_SAMPLERATE_11;
1323 			break;
1324 		case 8000:
1325 			if (IS_AUDIGY(&voice->stream->card->config))
1326 				return EMU_A_ADCCR_SAMPLERATE_8;
1327 			else
1328 				return EMU_ADCCR_SAMPLERATE_8;
1329 			break;
1330 		default:
1331 			PRINT(("recording sample_rate not supported : %" B_PRIu32 "\n",
1332 				voice->sample_rate));
1333 	}
1334 	return 0;
1335 }
1336 
1337 
1338 void
1339 emuxki_voice_start(emuxki_voice *voice)
1340 {
1341 	LOG(("emuxki_voice_start\n"));
1342 
1343 	if (voice->use & EMU_USE_PLAY) {
1344 		voice->trigblk = 1;
1345 		emuxki_channel_start(voice->dataloc.chan[0]);
1346 		if (voice->stereo)
1347 			emuxki_channel_start(voice->dataloc.chan[1]);
1348 	} else {
1349 
1350 		switch (voice->dataloc.source) {
1351 			case EMU_RECSRC_MIC:
1352 				emuxki_inte_enable(&voice->stream->card->config, EMU_INTE_MICBUFENABLE);
1353 				break;
1354 			case EMU_RECSRC_ADC: {
1355 				uint32 adccr_value = 0;
1356 				adccr_value = emuxki_voice_adc_rate(voice);
1357 				LOG(("emuxki_voice_start adccr_value : %u\n", adccr_value));
1358 				if (voice->stereo)
1359 					adccr_value |= ( (IS_AUDIGY(&voice->stream->card->config) ? EMU_A_ADCCR_LCHANENABLE : EMU_ADCCR_LCHANENABLE )
1360 						| ( IS_AUDIGY(&voice->stream->card->config) ? EMU_A_ADCCR_RCHANENABLE : EMU_ADCCR_RCHANENABLE ));
1361 				else
1362 					adccr_value |= IS_AUDIGY(&voice->stream->card->config) ? EMU_A_ADCCR_LCHANENABLE : EMU_ADCCR_LCHANENABLE;
1363 
1364 				LOG(("emuxki_voice_start adccr_value : %u, %u\n", adccr_value, EMU_ADCCR_LCHANENABLE | EMU_ADCCR_RCHANENABLE));
1365 				emuxki_chan_write(&voice->stream->card->config, 0, EMU_ADCCR, adccr_value);
1366 
1367 				emuxki_inte_enable(&voice->stream->card->config, EMU_INTE_ADCBUFENABLE);
1368 				}
1369 				break;
1370 			case EMU_RECSRC_FX:
1371 				if (IS_AUDIGY(&voice->stream->card->config)) {
1372 					emuxki_chan_write(&voice->stream->card->config, 0, EMU_A_FXWC1,
1373 						voice->recparams.efx_voices[0]);
1374 					emuxki_chan_write(&voice->stream->card->config, 0, EMU_A_FXWC2,
1375 						voice->recparams.efx_voices[1]);
1376 				} else {
1377 					emuxki_chan_write(&voice->stream->card->config, 0, EMU_FXWC,
1378 						voice->recparams.efx_voices[0]);
1379 				}
1380 				emuxki_inte_enable(&voice->stream->card->config, EMU_INTE_EFXBUFENABLE);
1381 				break;
1382 			default:
1383 				PRINT(("emuxki_voice_start BUG\n"));
1384 		}
1385 	}
1386 	voice->state |= EMU_STATE_STARTED;
1387 	if (voice->use & EMU_USE_PLAY) {
1388 		emuxki_resched_timer(voice->stream->card);
1389 	}
1390 }
1391 
1392 
1393 void
1394 emuxki_voice_halt(emuxki_voice *voice)
1395 {
1396 	LOG(("emuxki_voice_halt\n"));
1397 
1398 	if (voice->use & EMU_USE_PLAY) {
1399 		emuxki_channel_stop(voice->dataloc.chan[0]);
1400 		if (voice->stereo)
1401 			emuxki_channel_stop(voice->dataloc.chan[1]);
1402 	} else {
1403 
1404 		switch (voice->dataloc.source) {
1405 			case EMU_RECSRC_MIC:
1406 				emuxki_inte_disable(&voice->stream->card->config, EMU_INTE_MICBUFENABLE);
1407 				break;
1408 			case EMU_RECSRC_ADC:
1409 				emuxki_chan_write(&voice->stream->card->config, 0, EMU_ADCCR, 0);
1410 				emuxki_inte_disable(&voice->stream->card->config, EMU_INTE_ADCBUFENABLE);
1411 				break;
1412 			case EMU_RECSRC_FX:
1413 				if (IS_AUDIGY(&voice->stream->card->config)) {
1414 					emuxki_chan_write(&voice->stream->card->config, 0, EMU_A_FXWC1, 0);
1415 					emuxki_chan_write(&voice->stream->card->config, 0, EMU_A_FXWC2, 0);
1416 				} else
1417 					emuxki_chan_write(&voice->stream->card->config, 0, EMU_FXWC, 0);
1418 				emuxki_inte_disable(&voice->stream->card->config, EMU_INTE_EFXBUFENABLE);
1419 				break;
1420 			default:
1421 				PRINT(("emuxki_voice_halt BUG\n"));
1422 		}
1423 	}
1424 	voice->state &= ~EMU_STATE_STARTED;
1425 	if (voice->use & EMU_USE_PLAY) {
1426 		emuxki_resched_timer(voice->stream->card);
1427 	}
1428 }
1429 
1430 
1431 emuxki_voice *
1432 emuxki_voice_new(emuxki_stream *stream, uint8 use, uint8 voicenum)
1433 {
1434 	emuxki_voice *voice;
1435 	//int             s;
1436 
1437 	LOG(("emuxki_voice_new\n"));
1438 
1439 	voice = malloc(sizeof(emuxki_voice));
1440 	if (voice == NULL)
1441 		return (NULL);
1442 	voice->stream = stream;
1443 	voice->use = use;
1444 	voice->state = !EMU_STATE_STARTED;
1445 	voice->stereo = EMU_STEREO_NOTSET;
1446 	voice->b16 = 0;
1447 	voice->sample_rate = 0;
1448 	if (use & EMU_USE_PLAY)
1449 		voice->dataloc.chan[0] = voice->dataloc.chan[1] = NULL;
1450 	else
1451 		voice->dataloc.source = EMU_RECSRC_NOTSET;
1452 	voice->buffer = NULL;
1453 	voice->blksize = 0;
1454 	voice->trigblk = 0;
1455 	voice->blkmod = 0;
1456 	voice->timerate = 0;
1457 	voice->voicenum = voicenum;
1458 	return voice;
1459 }
1460 
1461 
1462 void
1463 emuxki_voice_delete(emuxki_voice *voice)
1464 {
1465 	if (voice->state & EMU_STATE_STARTED)
1466 		emuxki_voice_halt(voice);
1467 	emuxki_voice_dataloc_destroy(voice);
1468 	free(voice);
1469 }
1470 
1471 /*	Emuxki stream functions */
1472 
1473 status_t
1474 emuxki_stream_set_audioparms(emuxki_stream *stream, bool stereo, uint8 channels,
1475      uint8 b16, uint32 sample_rate)
1476 {
1477 	status_t 		error;
1478 	emuxki_voice 	*voice;
1479 	uint8			i, nvoices;
1480 	char 			*buffer;
1481 	uint8 			sample_size, frame_size;
1482 	LOG(("emuxki_stream_set_audioparms\n"));
1483 
1484 	if (stream->stereo == stereo &&
1485 		((stream->nmono + 2*stream->nstereo) == channels) &&
1486 		(stream->b16 == b16) &&
1487 		(stream->sample_rate == sample_rate))
1488 		return B_OK;
1489 
1490 	LIST_FOREACH(voice, &stream->voices, next) {
1491 		if (voice->buffer)
1492 			emuxki_mem_free(stream->card, voice->buffer->log_base);
1493 		emuxki_voice_delete(voice);
1494 	}
1495 	stream->first_voice = NULL;
1496 	LIST_INIT(&(stream->voices));
1497 
1498 	stream->b16 = b16;
1499 	stream->sample_rate = sample_rate;
1500 
1501 	if (stereo && (channels % 2 == 0)) {
1502 		stream->stereo = true;
1503 		stream->nstereo = channels / 2;
1504 		stream->nmono = 0;
1505 		nvoices = stream->nstereo;
1506 	} else {
1507 		stream->stereo = false;
1508 		stream->nstereo = 0;
1509 		stream->nmono = channels;
1510 		nvoices = stream->nmono;
1511 	}
1512 
1513 	sample_size = stream->b16 + 1;
1514 	frame_size = sample_size * (stream->stereo ? 2 : 1);
1515 
1516 	for (i = 0; i < nvoices; i++) {
1517 		voice = emuxki_voice_new(stream, stream->use, i);
1518 		if (voice) {
1519 			if (!stream->first_voice)
1520 				stream->first_voice = voice;
1521 			LIST_INSERT_HEAD((&stream->voices), voice, next);
1522 			if ((error = emuxki_voice_set_audioparms(voice, stream->stereo,
1523 				stream->b16, stream->sample_rate)))
1524 				return error;
1525 
1526 			if (stream->use & EMU_USE_PLAY)
1527 				buffer = emuxki_pmem_alloc(stream->card, stream->bufframes
1528 					* frame_size * stream->bufcount);
1529 			else
1530 				buffer = emuxki_rmem_alloc(stream->card, stream->bufframes
1531 					* frame_size * stream->bufcount);
1532 
1533 			emuxki_voice_set_bufparms(voice, buffer,
1534 				stream->bufframes * frame_size * stream->bufcount,
1535 				stream->bufframes * frame_size);
1536 		}
1537 	}
1538 
1539 	return B_OK;
1540 }
1541 
1542 
1543 status_t
1544 emuxki_stream_set_recparms(emuxki_stream *stream, emuxki_recsrc_t recsrc,
1545 			     	emuxki_recparams *recparams)
1546 {
1547 	emuxki_voice *voice;
1548 	LOG(("emuxki_stream_set_recparms\n"));
1549 
1550 	if (stream->use & EMU_USE_RECORD) {
1551 		switch(recsrc) {
1552 			case EMU_RECSRC_MIC:
1553 				break;
1554 			case EMU_RECSRC_ADC:
1555 				break;
1556 			case EMU_RECSRC_FX:
1557 				if (!recparams)
1558 					return B_ERROR;
1559 				LIST_FOREACH(voice, &stream->voices, next) {
1560 					voice->recparams.efx_voices[0] = recparams->efx_voices[0];
1561 					voice->recparams.efx_voices[1] = recparams->efx_voices[1];
1562 				}
1563 				break;
1564 			default:
1565 				return B_ERROR;
1566 				break;
1567 		}
1568 		LIST_FOREACH(voice, &stream->voices, next)
1569 			voice->dataloc.source = recsrc;
1570 	}
1571 	return B_OK;
1572 }
1573 
1574 
1575 status_t
1576 emuxki_stream_commit_parms(emuxki_stream *stream)
1577 {
1578 	emuxki_voice 	*voice;
1579 	status_t 		error;
1580 	LOG(("emuxki_stream_commit_parms\n"));
1581 
1582 	LIST_FOREACH(voice, &stream->voices, next)
1583 		if ((error = emuxki_voice_commit_parms(voice)))
1584 			return error;
1585 
1586 	return B_OK;
1587 }
1588 
1589 
1590 status_t
1591 emuxki_stream_get_nth_buffer(emuxki_stream *stream, uint8 chan, uint8 buf,
1592 					char** buffer, size_t *stride)
1593 {
1594 	emuxki_voice *voice = NULL;
1595 	uint8 i, sample_size;
1596 	LOG(("emuxki_stream_get_nth_buffer\n"));
1597 
1598 	sample_size = stream->b16 + 1;
1599 	if (buf >= stream->bufcount)
1600 		return B_BAD_INDEX;
1601 
1602 	if (stream->stereo) {
1603 		i = stream->nstereo - 1;
1604 		if (chan/2 > i)
1605 			return B_BAD_INDEX;
1606 		LIST_FOREACH(voice, &stream->voices, next)
1607 			if (i != chan/2)
1608 				i--;
1609 			else
1610 				break;
1611 		if (voice) {
1612 			*buffer = (char*)voice->buffer->log_base
1613 				+ (buf * stream->bufframes * sample_size * 2);
1614 			if (chan % 2 == 1)
1615 				*buffer += sample_size;
1616 			*stride = sample_size * 2;
1617 		} else
1618 			return B_ERROR;
1619 	} else {
1620 		i = stream->nmono - 1;
1621 		if (chan > i)
1622 			return B_BAD_INDEX;
1623 		LIST_FOREACH(voice, &stream->voices, next)
1624 			if (i != chan)
1625 				i--;
1626 			else
1627 				break;
1628 		if (voice) {
1629 			*buffer = (char*)voice->buffer->log_base
1630 				+ (buf * stream->bufframes * sample_size);
1631 			*stride = sample_size;
1632 		} else
1633 			return B_ERROR;
1634 	}
1635 
1636 	return B_OK;
1637 }
1638 
1639 
1640 void
1641 emuxki_stream_start(emuxki_stream *stream, void (*inth) (void *), void *inthparam)
1642 {
1643 	emuxki_voice *voice;
1644 	LOG(("emuxki_stream_start\n"));
1645 
1646 	stream->inth = inth;
1647 	stream->inthparam = inthparam;
1648 
1649 	LIST_FOREACH(voice, &stream->voices, next) {
1650 		emuxki_voice_start(voice);
1651 	}
1652 	stream->state |= EMU_STATE_STARTED;
1653 }
1654 
1655 
1656 void
1657 emuxki_stream_halt(emuxki_stream *stream)
1658 {
1659 	emuxki_voice *voice;
1660 	LOG(("emuxki_stream_halt\n"));
1661 
1662 	LIST_FOREACH(voice, &stream->voices, next) {
1663 		emuxki_voice_halt(voice);
1664 	}
1665 	stream->state &= ~EMU_STATE_STARTED;
1666 }
1667 
1668 
1669 emuxki_stream *
1670 emuxki_stream_new(emuxki_dev *card, uint8 use, uint32 bufframes, uint8 bufcount)
1671 {
1672 	emuxki_stream *stream;
1673 	cpu_status status;
1674 	LOG(("emuxki_stream_new\n"));
1675 
1676 	stream = malloc(sizeof(emuxki_stream));
1677 	if (stream == NULL)
1678 		return (NULL);
1679 	stream->card = card;
1680 	stream->use = use;
1681 	stream->state = !EMU_STATE_STARTED;
1682 	stream->stereo = EMU_STEREO_NOTSET;
1683 	stream->b16 = 0;
1684 	stream->sample_rate = 0;
1685 	stream->nmono = 0;
1686 	stream->nstereo = 0;
1687 	stream->bufframes = bufframes;
1688 	stream->bufcount = bufcount;
1689 	stream->first_voice = NULL;
1690 	stream->inth = NULL;
1691 	stream->inthparam = NULL;
1692 
1693 	stream->frames_count = 0;
1694 	stream->real_time = 0;
1695 	stream->buffer_cycle = 0;
1696 	stream->update_needed = false;
1697 
1698 	/* Init voices list */
1699 	LIST_INIT(&(stream->voices));
1700 
1701 	status = lock();
1702 	LIST_INSERT_HEAD((&card->streams), stream, next);
1703 	unlock(status);
1704 
1705 	return stream;
1706 }
1707 
1708 
1709 void
1710 emuxki_stream_delete(emuxki_stream *stream)
1711 {
1712 	emuxki_voice *voice;
1713 	cpu_status status;
1714 	LOG(("emuxki_stream_delete\n"));
1715 
1716 	emuxki_stream_halt(stream);
1717 
1718 	status = lock();
1719 	LIST_REMOVE(stream, next);
1720 	unlock(status);
1721 
1722 	while (!LIST_EMPTY(&stream->voices)) {
1723 		voice = LIST_FIRST(&stream->voices);
1724 		LIST_REMOVE(voice, next);
1725 		if (voice->buffer)
1726 			emuxki_mem_free(stream->card, voice->buffer->log_base);
1727 		emuxki_voice_delete(voice);
1728 	}
1729 
1730 	free(stream);
1731 }
1732 
1733 
1734 /* Emuxki gprs */
1735 // 87 values from 0.0dB to -xdB (-0.75dB each)
1736 static uint32 db_table[] = {
1737 	2147483647,	1969835071,	1806882308,	1657409659,	1520301995,	1394536435,	1279174712,
1738 	1173356181,	1076291388,	987256190,	905586345,	830672562,	761955951,	698923858,
1739 	641106035,	588071138,	539423503,	494800198,	453868315,	416322483,	381882595,
1740 	350291714,	321314160,	294733747,	270352173,	247987542,	227473005,	208655513,
1741 	191394681,	175561735,	161038555,	147716791,	135497057,	124288190,	114006566,
1742 	104575479,	95924570,	87989300,	80710468,	74033770,	67909395,	62291654,
1743 	57138635,	52411895,	48076170,	44099114,	40451056,	37104780,	34035322,
1744 	31219781,	28637154,	26268172,	24095162,	22101913,	20273552,	18596442,
1745 	17058068,	15646956,	14352576,	13165272,	12076188,	11077196,	10160845,
1746 	9320299,	8549286,	7842054,	7193328,	6598266,	6052431,	5551749,
1747 	5092486,	4671215,	4284793,	3930337,	3605204,	3306967,	3033401,
1748 	2782465,	2552289,	2341153,	2147483,	1969835,	1806882,	1657409,
1749 	1520301,	1394536,	1279174
1750 };
1751 
1752 
1753 void
1754 emuxki_gpr_set(emuxki_dev *card, emuxki_gpr *gpr, int32 type, float *values)
1755 {
1756 	uint8 count = gpr->type & EMU_MIX_STEREO ? 2 : 1;
1757 	uint8 i;
1758 	uint32 index;
1759 
1760 	LOG(("emuxki_set_gpr\n"));
1761 
1762 	switch(type) {
1763 		case EMU_MIX_MUTE:
1764 			gpr->mute = (values[0] == 1.0);
1765 			if (gpr->mute) {
1766 				for (i = 0; i < count; i++)
1767 					emuxki_write_gpr(&card->config, gpr->gpr + i, 0);
1768 				break;
1769 			}
1770 			for (i = 0; i < count; i++) {
1771 				values[i] = gpr->current[i];
1772 			}
1773 		case EMU_MIX_GAIN:
1774 			for (i = 0; i < count; i++) {
1775 				if (values[i]>gpr->max_gain || values[i]<gpr->min_gain)
1776 					return;
1777 				index = values[i] / gpr->granularity;
1778 				if (index > sizeof(db_table)/sizeof(db_table[0]))
1779 					index = sizeof(db_table)/sizeof(db_table[0]);
1780 				LOG(("emuxki_set_gpr gpr: %d \n", gpr->gpr + i));
1781 				LOG(("emuxki_set_gpr values[i]: %g \n", values[i]));
1782 				LOG(("emuxki_set_gpr index: %u \n", index));
1783 				if (!gpr->mute)
1784 					emuxki_write_gpr(&card->config, gpr->gpr + i, db_table[index]);
1785 				gpr->current[i] = index * gpr->granularity;
1786 			}
1787 			break;
1788 	}
1789 }
1790 
1791 
1792 void
1793 emuxki_gpr_get(emuxki_dev *card, emuxki_gpr *gpr, int32 type, float *values)
1794 {
1795 	uint8 count = gpr->type & EMU_MIX_STEREO ? 2 : 1;
1796 	uint16 i;
1797 
1798 	LOG(("emuxki_get_gpr\n"));
1799 
1800 	switch(type) {
1801 		case EMU_MIX_GAIN:
1802 			for (i = 0; i < count; i++) {
1803 				values[i] = gpr->current[i];
1804 			}
1805 			break;
1806 		case EMU_MIX_MUTE:
1807 			values[0] = (gpr->mute ? 1.0 : 0.0);
1808 			break;
1809 	}
1810 }
1811 
1812 
1813 void
1814 emuxki_gpr_dump(emuxki_dev * card, uint16 count)
1815 {
1816 	uint16      pc;
1817 	uint32		value;
1818 
1819 	LOG(("emuxki_dump_gprs\n"));
1820 
1821 	for (pc = 0; pc < count; pc++) {
1822 		value = emuxki_read_gpr(&card->config, pc);
1823 		LOG(("dsp_gpr pc=%x, value=%x\n", pc, value));
1824 	}
1825 }
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 = ((uint32)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, (uint32)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