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