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