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