xref: /haiku/src/add-ons/kernel/drivers/audio/emuxki/multi.c (revision dd2a1e350b303b855a50fd64e6cb55618be1ae6a)
1 /*
2  * Emuxki BeOS Driver for Creative Labs SBLive!/Audigy series
3  *
4  * Copyright (c) 2002, Jerome Duval (jerome.duval@free.fr)
5  *
6  * Original code : BeOS Driver for Intel ICH AC'97 Link interface
7  * Copyright (c) 2002, Marcus Overhagen <marcus@overhagen.de>
8  *
9  * All rights reserved.
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * - Redistributions of source code must retain the above copyright notice,
14  *   this list of conditions and the following disclaimer.
15  * - Redistributions in binary form must reproduce the above copyright notice,
16  *   this list of conditions and the following disclaimer in the documentation
17  *   and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 #include <OS.h>
33 #include <MediaDefs.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <strings.h>
37 
38 #include <kernel.h>
39 
40 #include "hmulti_audio.h"
41 #include "multi.h"
42 #include "ac97.h"
43 #include "debug.h"
44 #include "emuxki.h"
45 #include "util.h"
46 #include "io.h"
47 
48 static void
49 emuxki_ac97_get_mix(void *card, const void *cookie, int32 type, float *values) {
50 	emuxki_dev *dev = (emuxki_dev*)card;
51 	ac97_source_info *info = (ac97_source_info *)cookie;
52 	uint16 value, mask;
53 	float gain;
54 
55 	switch(type) {
56 		case B_MIX_GAIN:
57 			value = emuxki_codec_read(&dev->config, info->reg);
58 			//PRINT(("B_MIX_GAIN value : %u\n", value));
59 			if (info->type & B_MIX_STEREO) {
60 				mask = ((1 << (info->bits + 1)) - 1) << 8;
61 				gain = ((value & mask) >> 8) * info->granularity;
62 				if (info->polarity == 1)
63 					values[0] = info->max_gain - gain;
64 				else
65 					values[0] = gain - info->min_gain;
66 
67 				mask = ((1 << (info->bits + 1)) - 1);
68 				gain = (value & mask) * info->granularity;
69 				if (info->polarity == 1)
70 					values[1] = info->max_gain - gain;
71 				else
72 					values[1] = gain - info->min_gain;
73 			} else {
74 				mask = ((1 << (info->bits + 1)) - 1);
75 				gain = (value & mask) * info->granularity;
76 				if (info->polarity == 1)
77 					values[0] = info->max_gain - gain;
78 				else
79 					values[0] = gain - info->min_gain;
80 			}
81 			break;
82 		case B_MIX_MUTE:
83 			mask = ((1 << 1) - 1) << 15;
84 			value = emuxki_codec_read(&dev->config, info->reg);
85 			//PRINT(("B_MIX_MUTE value : %u\n", value));
86 			value &= mask;
87 			values[0] = ((value >> 15) == 1) ? 1.0 : 0.0;
88 			break;
89 		case B_MIX_MICBOOST:
90 			mask = ((1 << 1) - 1) << 6;
91 			value = emuxki_codec_read(&dev->config, info->reg);
92 			//PRINT(("B_MIX_MICBOOST value : %u\n", value));
93 			value &= mask;
94 			values[0] = ((value >> 6) == 1) ? 1.0 : 0.0;
95 			break;
96 		case B_MIX_MUX:
97 			mask = ((1 << 3) - 1);
98 			value = emuxki_codec_read(&dev->config, AC97_RECORD_SELECT);
99 			value &= mask;
100 			//PRINT(("B_MIX_MUX value : %u\n", value));
101 			values[0] = (float)value;
102 			break;
103 	}
104 }
105 
106 static void
107 emuxki_ac97_set_mix(void *card, const void *cookie, int32 type, float *values) {
108 	emuxki_dev *dev = (emuxki_dev*)card;
109 	ac97_source_info *info = (ac97_source_info *)cookie;
110 	uint16 value, mask;
111 	float gain;
112 
113 	switch(type) {
114 		case B_MIX_GAIN:
115 			value = emuxki_codec_read(&dev->config, info->reg);
116 			if (info->type & B_MIX_STEREO) {
117 				mask = ((1 << (info->bits + 1)) - 1) << 8;
118 				value &= ~mask;
119 
120 				if (info->polarity == 1)
121 					gain = info->max_gain - values[0];
122 				else
123 					gain =  values[0] - info->min_gain;
124 				value |= ((uint16)(gain	/ info->granularity) << 8) & mask;
125 
126 				mask = ((1 << (info->bits + 1)) - 1);
127 				value &= ~mask;
128 				if (info->polarity == 1)
129 					gain = info->max_gain - values[1];
130 				else
131 					gain =  values[1] - info->min_gain;
132 				value |= ((uint16)(gain / info->granularity)) & mask;
133 			} else {
134 				mask = ((1 << (info->bits + 1)) - 1);
135 				value &= ~mask;
136 				if (info->polarity == 1)
137 					gain = info->max_gain - values[0];
138 				else
139 					gain =  values[0] - info->min_gain;
140 				value |= ((uint16)(gain / info->granularity)) & mask;
141 			}
142 			//PRINT(("B_MIX_GAIN value : %u\n", value));
143 			emuxki_codec_write(&dev->config, info->reg, value);
144 			break;
145 		case B_MIX_MUTE:
146 			mask = ((1 << 1) - 1) << 15;
147 			value = emuxki_codec_read(&dev->config, info->reg);
148 			value &= ~mask;
149 			value |= ((values[0] == 1.0 ? 1 : 0 ) << 15 & mask);
150 			if (info->reg == AC97_SURROUND_VOLUME) {
151 				// there is a independent mute for each channel
152 				mask = ((1 << 1) - 1) << 7;
153 				value &= ~mask;
154 				value |= ((values[0] == 1.0 ? 1 : 0 ) << 7 & mask);
155 			}
156 			//PRINT(("B_MIX_MUTE value : %u\n", value));
157 			emuxki_codec_write(&dev->config, info->reg, value);
158 			break;
159 		case B_MIX_MICBOOST:
160 			mask = ((1 << 1) - 1) << 6;
161 			value = emuxki_codec_read(&dev->config, info->reg);
162 			value &= ~mask;
163 			value |= ((values[0] == 1.0 ? 1 : 0 ) << 6 & mask);
164 			//PRINT(("B_MIX_MICBOOST value : %u\n", value));
165 			emuxki_codec_write(&dev->config, info->reg, value);
166 			break;
167 		case B_MIX_MUX:
168 			mask = ((1 << 3) - 1);
169 			value = ((int32)values[0]) & mask;
170 			value = value | (value << 8);
171 			//PRINT(("B_MIX_MUX value : %u\n", value));
172 			emuxki_codec_write(&dev->config, AC97_RECORD_SELECT, value);
173 			break;
174 	}
175 
176 }
177 
178 static void
179 emuxki_gpr_get_mix(void *card, const void *cookie, int32 type, float *values) {
180 	emuxki_gpr_get((emuxki_dev*)card, (emuxki_gpr *)cookie, type, values);
181 }
182 
183 static void
184 emuxki_gpr_set_mix(void *card, const void *cookie, int32 type, float *values) {
185 	emuxki_gpr_set((emuxki_dev*)card, (emuxki_gpr *)cookie, type, values);
186 }
187 
188 static void
189 emuxki_parameter_get_mix(void *card, const void *cookie, int32 type, float *values) {
190 	int32 value;
191 	emuxki_parameter_get((emuxki_dev*)card, cookie, type, &value);
192 	values[0] = (float)value;
193 }
194 
195 static void
196 emuxki_parameter_set_mix(void *card, const void *cookie, int32 type, float *values) {
197 	int32 value;
198 	value = (int32)values[0];
199 	emuxki_parameter_set((emuxki_dev*)card, cookie, type, &value);
200 }
201 
202 static int32
203 emuxki_create_group_control(multi_dev *multi, uint32 *index, int32 parent,
204 	int32 string, const char* name) {
205 	int32 i = *index;
206 	(*index)++;
207 	multi->controls[i].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + i;
208 	multi->controls[i].mix_control.parent = parent;
209 	multi->controls[i].mix_control.flags = B_MULTI_MIX_GROUP;
210 	multi->controls[i].mix_control.master = EMU_MULTI_CONTROL_MASTERID;
211 	multi->controls[i].mix_control.string = string;
212 	if (name)
213 		strcpy(multi->controls[i].mix_control.name, name);
214 
215 	return multi->controls[i].mix_control.id;
216 }
217 
218 static void
219 emuxki_create_gpr_control(multi_dev *multi, uint32 *index, int32 parent, int32 string,
220 	const emuxki_gpr *gpr) {
221 	int32 i = *index, id;
222 	multi_mixer_control control;
223 
224 	control.mix_control.master = EMU_MULTI_CONTROL_MASTERID;
225 	control.mix_control.parent = parent;
226 	control.cookie = gpr;
227 	control.get = &emuxki_gpr_get_mix;
228 	control.set = &emuxki_gpr_set_mix;
229 	control.mix_control.u.gain.min_gain = gpr->min_gain;
230 	control.mix_control.u.gain.max_gain = gpr->max_gain;
231 	control.mix_control.u.gain.granularity = gpr->granularity;
232 
233 	if (gpr->type & EMU_MIX_GAIN) {
234 		if (gpr->type & EMU_MIX_MUTE) {
235 			control.mix_control.id = EMU_MULTI_CONTROL_FIRSTID + i;
236 			control.mix_control.flags = B_MULTI_MIX_ENABLE;
237 			control.mix_control.string = S_MUTE;
238 			control.type = EMU_MIX_MUTE;
239 			multi->controls[i] = control;
240 			i++;
241 		}
242 
243 		control.mix_control.id = EMU_MULTI_CONTROL_FIRSTID + i;
244 		control.mix_control.flags = B_MULTI_MIX_GAIN;
245 		strcpy(control.mix_control.name, gpr->name);
246 		control.type = EMU_MIX_GAIN;
247 		multi->controls[i] = control;
248 		id = control.mix_control.id;
249 		i++;
250 
251 		if (gpr->type & EMU_MIX_STEREO) {
252 			control.mix_control.id = EMU_MULTI_CONTROL_FIRSTID + i;
253 			control.mix_control.master = id;
254 			multi->controls[i] = control;
255 			i++;
256 		}
257 	}
258 	*index = i;
259 }
260 
261 static status_t
262 emuxki_create_controls_list(multi_dev *multi)
263 {
264 	uint32 	i = 0, index = 0, count, id, parent, parent2, parent3;
265 	emuxki_dev *card = (emuxki_dev*)multi->card;
266 	const ac97_source_info *info;
267 
268 	parent = emuxki_create_group_control(multi, &index, 0, 0, "Playback");
269 
270 	for (i = EMU_GPR_FIRST_MIX; i < card->gpr_count; i++) {
271 		const emuxki_gpr *gpr = &card->gpr[i];
272 		if ((gpr->type & EMU_MIX_PLAYBACK) == 0)
273 			continue;
274 
275 		parent2 = emuxki_create_group_control(multi, &index, parent, 0, gpr->name);
276 
277 		emuxki_create_gpr_control(multi, &index, parent2, 0, gpr);
278 		if (gpr->type & EMU_MIX_GAIN && gpr->type & EMU_MIX_STEREO)
279 			i++;
280 	}
281 
282 	parent = emuxki_create_group_control(multi, &index, 0, 0, "Record");
283 
284 	for (i = EMU_GPR_FIRST_MIX; i < card->gpr_count; i++) {
285 		const emuxki_gpr *gpr = &card->gpr[i];
286 		if ((gpr->type & EMU_MIX_RECORD) == 0)
287 			continue;
288 		parent2 = emuxki_create_group_control(multi, &index, parent, 0, gpr->name);
289 
290 		emuxki_create_gpr_control(multi, &index, parent2, 0, gpr);
291 		if (gpr->type & EMU_MIX_GAIN && gpr->type & EMU_MIX_STEREO)
292 			i++;
293 	}
294 
295 	/* AC97 Record */
296 	info = &source_info[0];
297 	PRINT(("name : %s\n", info->name));
298 
299 	parent2 = emuxki_create_group_control(multi, &index, parent, 0, info->name);
300 
301 	if (info->type & B_MIX_GAIN) {
302 		if (info->type & B_MIX_MUTE) {
303 			multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
304 			multi->controls[index].mix_control.flags = B_MULTI_MIX_ENABLE;
305 			multi->controls[index].mix_control.master = EMU_MULTI_CONTROL_MASTERID;
306 			multi->controls[index].mix_control.parent = parent2;
307 			multi->controls[index].mix_control.string = S_MUTE;
308 			multi->controls[index].cookie = info;
309 			multi->controls[index].type = B_MIX_MUTE;
310 			multi->controls[index].get = &emuxki_ac97_get_mix;
311 			multi->controls[index].set = &emuxki_ac97_set_mix;
312 			index++;
313 		}
314 
315 		multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
316 		multi->controls[index].mix_control.flags = B_MULTI_MIX_GAIN;
317 		multi->controls[index].mix_control.master = EMU_MULTI_CONTROL_MASTERID;
318 		multi->controls[index].mix_control.parent = parent2;
319 		strcpy(multi->controls[index].mix_control.name, info->name);
320 		multi->controls[index].mix_control.u.gain.min_gain = info->min_gain;
321 		multi->controls[index].mix_control.u.gain.max_gain = info->max_gain;
322 		multi->controls[index].mix_control.u.gain.granularity = info->granularity;
323 		multi->controls[index].cookie = info;
324 		multi->controls[index].type = B_MIX_GAIN;
325 		multi->controls[index].get = &emuxki_ac97_get_mix;
326 		multi->controls[index].set = &emuxki_ac97_set_mix;
327 		id = multi->controls[index].mix_control.id;
328 		index++;
329 
330 		if (info->type & B_MIX_STEREO) {
331 			multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
332 			multi->controls[index].mix_control.flags = B_MULTI_MIX_GAIN;
333 			multi->controls[index].mix_control.master = id;
334 			multi->controls[index].mix_control.parent = parent2;
335 			strcpy(multi->controls[index].mix_control.name, info->name);
336 			multi->controls[index].mix_control.u.gain.min_gain = info->min_gain;
337 			multi->controls[index].mix_control.u.gain.max_gain = info->max_gain;
338 			multi->controls[index].mix_control.u.gain.granularity = info->granularity;
339 			multi->controls[index].cookie = info;
340 			multi->controls[index].type = B_MIX_GAIN;
341 			multi->controls[index].get = &emuxki_ac97_get_mix;
342 			multi->controls[index].set = &emuxki_ac97_set_mix;
343 			index++;
344 		}
345 
346 		if (info->type & B_MIX_RECORDMUX) {
347 			multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
348 			multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX;
349 			multi->controls[index].mix_control.parent = parent2;
350 			strcpy(multi->controls[index].mix_control.name, "Record mux");
351 			multi->controls[index].cookie = info;
352 			multi->controls[index].type = B_MIX_MUX;
353 			multi->controls[index].get = &emuxki_ac97_get_mix;
354 			multi->controls[index].set = &emuxki_ac97_set_mix;
355 			parent3 = multi->controls[index].mix_control.id;
356 			index++;
357 
358 			multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
359 			multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX_VALUE;
360 			multi->controls[index].mix_control.parent = parent3;
361 			multi->controls[index].mix_control.string = S_MIC;
362 			index++;
363 			multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
364 			multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX_VALUE;
365 			multi->controls[index].mix_control.parent = parent3;
366 			strcpy(multi->controls[index].mix_control.name, "CD in");
367 			index++;
368 			multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
369 			multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX_VALUE;
370 			multi->controls[index].mix_control.parent = parent3;
371 			strcpy(multi->controls[index].mix_control.name, "Video in");
372 			index++;
373 			multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
374 			multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX_VALUE;
375 			multi->controls[index].mix_control.parent = parent3;
376 			strcpy(multi->controls[index].mix_control.name, "Aux in");
377 			index++;
378 			multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
379 			multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX_VALUE;
380 			multi->controls[index].mix_control.parent = parent3;
381 			strcpy(multi->controls[index].mix_control.name, "Line in");
382 			index++;
383 			multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
384 			multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX_VALUE;
385 			multi->controls[index].mix_control.parent = parent3;
386 			multi->controls[index].mix_control.string = S_STEREO_MIX;
387 			index++;
388 			multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
389 			multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX_VALUE;
390 			multi->controls[index].mix_control.parent = parent3;
391 			multi->controls[index].mix_control.string = S_MONO_MIX;
392 			index++;
393 			multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
394 			multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX_VALUE;
395 			multi->controls[index].mix_control.parent = parent3;
396 			strcpy(multi->controls[index].mix_control.name, "TAD");
397 			index++;
398 		}
399 	}
400 
401 	parent = emuxki_create_group_control(multi, &index, 0, 0, "AC97 mixer");
402 
403 	count = source_info_size;
404 	if (IS_AUDIGY2(&card->config))
405 		count = 1;
406 	if (!IS_LIVE_5_1(&card->config) && !IS_AUDIGY(&card->config))
407 		count--;
408 
409 	for (i = 1; i < count ; i++) {
410 		info = &source_info[i];
411 		PRINT(("name : %s\n", info->name));
412 
413 		parent2 = emuxki_create_group_control(multi, &index, parent, 0, info->name);
414 
415 		if (info->type & B_MIX_GAIN) {
416 			if (info->type & B_MIX_MUTE) {
417 				multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
418 				multi->controls[index].mix_control.flags = B_MULTI_MIX_ENABLE;
419 				multi->controls[index].mix_control.master = EMU_MULTI_CONTROL_MASTERID;
420 				multi->controls[index].mix_control.parent = parent2;
421 				multi->controls[index].mix_control.string = S_MUTE;
422 				multi->controls[index].cookie = info;
423 				multi->controls[index].type = B_MIX_MUTE;
424 				multi->controls[index].get = &emuxki_ac97_get_mix;
425 				multi->controls[index].set = &emuxki_ac97_set_mix;
426 				index++;
427 			}
428 
429 			multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
430 			multi->controls[index].mix_control.flags = B_MULTI_MIX_GAIN;
431 			multi->controls[index].mix_control.master = EMU_MULTI_CONTROL_MASTERID;
432 			multi->controls[index].mix_control.parent = parent2;
433 			strcpy(multi->controls[index].mix_control.name, info->name);
434 			multi->controls[index].mix_control.u.gain.min_gain = info->min_gain;
435 			multi->controls[index].mix_control.u.gain.max_gain = info->max_gain;
436 			multi->controls[index].mix_control.u.gain.granularity = info->granularity;
437 			multi->controls[index].cookie = info;
438 			multi->controls[index].type = B_MIX_GAIN;
439 			multi->controls[index].get = &emuxki_ac97_get_mix;
440 			multi->controls[index].set = &emuxki_ac97_set_mix;
441 			id = multi->controls[index].mix_control.id;
442 			index++;
443 
444 			if (info->type & B_MIX_STEREO) {
445 				multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
446 				multi->controls[index].mix_control.flags = B_MULTI_MIX_GAIN;
447 				multi->controls[index].mix_control.master = id;
448 				multi->controls[index].mix_control.parent = parent2;
449 				strcpy(multi->controls[index].mix_control.name, info->name);
450 				multi->controls[index].mix_control.u.gain.min_gain = info->min_gain;
451 				multi->controls[index].mix_control.u.gain.max_gain = info->max_gain;
452 				multi->controls[index].mix_control.u.gain.granularity = info->granularity;
453 				multi->controls[index].cookie = info;
454 				multi->controls[index].type = B_MIX_GAIN;
455 				multi->controls[index].get = &emuxki_ac97_get_mix;
456 				multi->controls[index].set = &emuxki_ac97_set_mix;
457 				index++;
458 			}
459 		}
460 	}
461 
462 	parent = emuxki_create_group_control(multi, &index, 0, S_SETUP, NULL);
463 
464 	/* AC97 20db Boost Mic */
465 	info = &source_info[6];
466 
467 	if (info->type & B_MIX_GAIN && info->type & B_MIX_MICBOOST) {
468 		multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
469 		multi->controls[index].mix_control.flags = B_MULTI_MIX_ENABLE;
470 		multi->controls[index].mix_control.master = EMU_MULTI_CONTROL_MASTERID;
471 		multi->controls[index].mix_control.parent = parent;
472 		strcpy(multi->controls[index].mix_control.name, "Mic +20dB");
473 		multi->controls[index].cookie = info;
474 		multi->controls[index].type = B_MIX_MICBOOST;
475 		multi->controls[index].get = &emuxki_ac97_get_mix;
476 		multi->controls[index].set = &emuxki_ac97_set_mix;
477 		index++;
478 	}
479 
480 	if (true) {
481 		multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
482 		multi->controls[index].mix_control.flags = B_MULTI_MIX_ENABLE;
483 		multi->controls[index].mix_control.master = EMU_MULTI_CONTROL_MASTERID;
484 		multi->controls[index].mix_control.parent = parent;
485 		strcpy(multi->controls[index].mix_control.name, "Enable digital");
486 		multi->controls[index].cookie = NULL;
487 		multi->controls[index].type = EMU_DIGITAL_MODE;
488 		multi->controls[index].get = &emuxki_parameter_get_mix;
489 		multi->controls[index].set = &emuxki_parameter_set_mix;
490 		index++;
491 	}
492 
493 	if (true) {
494 		multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
495 		multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX;
496 		multi->controls[index].mix_control.parent = parent;
497 		strcpy(multi->controls[index].mix_control.name, "Audio mode");
498 		multi->controls[index].cookie = NULL;
499 		multi->controls[index].type = EMU_AUDIO_MODE;
500 		multi->controls[index].get = &emuxki_parameter_get_mix;
501 		multi->controls[index].set = &emuxki_parameter_set_mix;
502 		parent2 = multi->controls[index].mix_control.id;
503 		index++;
504 
505 		multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
506 		multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX_VALUE;
507 		multi->controls[index].mix_control.parent = parent2;
508 		strcpy(multi->controls[index].mix_control.name, "2.0");
509 		index++;
510 		multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
511 		multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX_VALUE;
512 		multi->controls[index].mix_control.parent = parent2;
513 		strcpy(multi->controls[index].mix_control.name, "4.0");
514 		index++;
515 		multi->controls[index].mix_control.id = EMU_MULTI_CONTROL_FIRSTID + index;
516 		multi->controls[index].mix_control.flags = B_MULTI_MIX_MUX_VALUE;
517 		multi->controls[index].mix_control.parent = parent2;
518 		strcpy(multi->controls[index].mix_control.name, "5.1");
519 		index++;
520 	}
521 
522 	multi->control_count = index;
523 	PRINT(("multi->control_count %" B_PRIu32 "\n", multi->control_count));
524 	return B_OK;
525 }
526 
527 static status_t
528 emuxki_get_mix(emuxki_dev *card, multi_mix_value_info * mmvi)
529 {
530 	int32 i;
531 	uint32 id;
532 	multi_mixer_control *control = NULL;
533 	for (i = 0; i < mmvi->item_count; i++) {
534 		id = mmvi->values[i].id - EMU_MULTI_CONTROL_FIRSTID;
535 		if (id < 0 || id >= card->multi.control_count) {
536 			PRINT(("emuxki_get_mix : "
537 				"invalid control id requested : %" B_PRIi32 "\n", id));
538 			continue;
539 		}
540 		control = &card->multi.controls[id];
541 
542 		if (control->mix_control.flags & B_MULTI_MIX_GAIN) {
543 			if (control->get) {
544 				float values[2];
545 				control->get(card, control->cookie, control->type, values);
546 				if (control->mix_control.master == EMU_MULTI_CONTROL_MASTERID)
547 					mmvi->values[i].u.gain = values[0];
548 				else
549 					mmvi->values[i].u.gain = values[1];
550 			}
551 		}
552 
553 		if (control->mix_control.flags & B_MULTI_MIX_ENABLE && control->get) {
554 			float values[1];
555 			control->get(card, control->cookie, control->type, values);
556 			mmvi->values[i].u.enable = (values[0] == 1.0);
557 		}
558 
559 		if (control->mix_control.flags & B_MULTI_MIX_MUX && control->get) {
560 			float values[1];
561 			control->get(card, control->cookie, control->type, values);
562 			mmvi->values[i].u.mux = (int32)values[0];
563 		}
564 	}
565 	return B_OK;
566 }
567 
568 static status_t
569 emuxki_set_mix(emuxki_dev *card, multi_mix_value_info * mmvi)
570 {
571 	int32 i;
572 	uint32 id;
573 	multi_mixer_control *control = NULL;
574 	for (i = 0; i < mmvi->item_count; i++) {
575 		id = mmvi->values[i].id - EMU_MULTI_CONTROL_FIRSTID;
576 		if (id < 0 || id >= card->multi.control_count) {
577 			PRINT(("emuxki_set_mix : "
578 				"invalid control id requested : %" B_PRIi32 "\n", id));
579 			continue;
580 		}
581 		control = &card->multi.controls[id];
582 
583 		if (control->mix_control.flags & B_MULTI_MIX_GAIN) {
584 			multi_mixer_control *control2 = NULL;
585 			if (i+1<mmvi->item_count) {
586 				id = mmvi->values[i + 1].id - EMU_MULTI_CONTROL_FIRSTID;
587 				if (id < 0 || id >= card->multi.control_count) {
588 					PRINT(("emuxki_set_mix : "
589 						"invalid control id requested : %" B_PRIi32 "\n", id));
590 				} else {
591 					control2 = &card->multi.controls[id];
592 					if (control2->mix_control.master != control->mix_control.id)
593 						control2 = NULL;
594 				}
595 			}
596 
597 			if (control->set) {
598 				float values[2];
599 				values[0] = 0.0;
600 				values[1] = 0.0;
601 
602 				if (control->mix_control.master == EMU_MULTI_CONTROL_MASTERID)
603 					values[0] = mmvi->values[i].u.gain;
604 				else
605 					values[1] = mmvi->values[i].u.gain;
606 
607 				if (control2 && control2->mix_control.master != EMU_MULTI_CONTROL_MASTERID)
608 					values[1] = mmvi->values[i+1].u.gain;
609 
610 				control->set(card, control->cookie, control->type, values);
611 			}
612 
613 			if (control2)
614 				i++;
615 		}
616 
617 		if (control->mix_control.flags & B_MULTI_MIX_ENABLE && control->set) {
618 			float values[1];
619 
620 			values[0] = mmvi->values[i].u.enable ? 1.0 : 0.0;
621 			control->set(card, control->cookie, control->type, values);
622 		}
623 
624 		if (control->mix_control.flags & B_MULTI_MIX_MUX && control->set) {
625 			float values[1];
626 
627 			values[0] = (float)mmvi->values[i].u.mux;
628 			control->set(card, control->cookie, control->type, values);
629 		}
630 	}
631 	return B_OK;
632 }
633 
634 static status_t
635 emuxki_list_mix_controls(emuxki_dev *card, multi_mix_control_info * mmci)
636 {
637 	multi_mix_control	*mmc;
638 	uint32 i;
639 
640 	mmc = mmci->controls;
641 	if (mmci->control_count < EMU_MULTICONTROLSNUM)
642 		return B_ERROR;
643 
644 	if (emuxki_create_controls_list(&card->multi) < B_OK)
645 		return B_ERROR;
646 	for (i = 0; i < card->multi.control_count; i++) {
647 		mmc[i] = card->multi.controls[i].mix_control;
648 	}
649 
650 	mmci->control_count = card->multi.control_count;
651 	return B_OK;
652 }
653 
654 static status_t
655 emuxki_list_mix_connections(emuxki_dev *card, multi_mix_connection_info * data)
656 {
657 	return B_ERROR;
658 }
659 
660 static status_t
661 emuxki_list_mix_channels(emuxki_dev *card, multi_mix_channel_info *data)
662 {
663 	return B_ERROR;
664 }
665 
666 /*multi_channel_info chans[] = {
667 {  0, B_MULTI_OUTPUT_CHANNEL, 	B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS, 0 },
668 {  1, B_MULTI_OUTPUT_CHANNEL, 	B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS, 0 },
669 {  2, B_MULTI_OUTPUT_CHANNEL, 	B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS, 0 },
670 {  3, B_MULTI_OUTPUT_CHANNEL, 	B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS, 0 },
671 {  4, B_MULTI_INPUT_CHANNEL, 	B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS, 0 },
672 {  5, B_MULTI_INPUT_CHANNEL, 	B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS, 0 },
673 {  6, B_MULTI_INPUT_CHANNEL, 	B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS, 0 },
674 {  7, B_MULTI_INPUT_CHANNEL, 	B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS, 0 },
675 {  8, B_MULTI_OUTPUT_BUS, 		B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS, 	B_CHANNEL_MINI_JACK_STEREO },
676 {  9, B_MULTI_OUTPUT_BUS, 		B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS, B_CHANNEL_MINI_JACK_STEREO },
677 {  10, B_MULTI_INPUT_BUS, 		B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS, 	B_CHANNEL_MINI_JACK_STEREO },
678 {  11, B_MULTI_INPUT_BUS, 		B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS, B_CHANNEL_MINI_JACK_STEREO },
679 };*/
680 
681 /*multi_channel_info chans[] = {
682 {  0, B_MULTI_OUTPUT_CHANNEL, 	B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS, 0 },
683 {  1, B_MULTI_OUTPUT_CHANNEL, 	B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS, 0 },
684 {  2, B_MULTI_OUTPUT_CHANNEL, 	B_CHANNEL_LEFT | B_CHANNEL_SURROUND_BUS, 0 },
685 {  3, B_MULTI_OUTPUT_CHANNEL, 	B_CHANNEL_RIGHT | B_CHANNEL_SURROUND_BUS, 0 },
686 {  4, B_MULTI_OUTPUT_CHANNEL, 	B_CHANNEL_REARLEFT | B_CHANNEL_SURROUND_BUS, 0 },
687 {  5, B_MULTI_OUTPUT_CHANNEL, 	B_CHANNEL_REARRIGHT | B_CHANNEL_SURROUND_BUS, 0 },
688 {  6, B_MULTI_INPUT_CHANNEL, 	B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS, 0 },
689 {  7, B_MULTI_INPUT_CHANNEL, 	B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS, 0 },
690 {  8, B_MULTI_INPUT_CHANNEL, 	B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS, 0 },
691 {  9, B_MULTI_INPUT_CHANNEL, 	B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS, 0 },
692 {  10, B_MULTI_OUTPUT_BUS, 		B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS, 	B_CHANNEL_MINI_JACK_STEREO },
693 {  11, B_MULTI_OUTPUT_BUS, 		B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS, B_CHANNEL_MINI_JACK_STEREO },
694 {  12, B_MULTI_INPUT_BUS, 		B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS, 	B_CHANNEL_MINI_JACK_STEREO },
695 {  13, B_MULTI_INPUT_BUS, 		B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS, B_CHANNEL_MINI_JACK_STEREO },
696 };*/
697 
698 
699 static void
700 emuxki_create_channels_list(multi_dev *multi)
701 {
702 	emuxki_stream *stream;
703 	uint32 index, i, designations, nchannels;
704 	int32 mode;
705 	multi_channel_info *chans;
706 	uint32 chan_designations[] = {
707 		B_CHANNEL_LEFT,
708 		B_CHANNEL_RIGHT,
709 		B_CHANNEL_REARLEFT,
710 		B_CHANNEL_REARRIGHT,
711 		B_CHANNEL_CENTER,
712 		B_CHANNEL_SUB
713 	};
714 
715 	chans = multi->chans;
716 	index = 0;
717 
718 	for (mode=EMU_USE_PLAY; mode!=-1;
719 		mode = (mode == EMU_USE_PLAY) ? EMU_USE_RECORD : -1) {
720 		LIST_FOREACH(stream, &((emuxki_dev*)multi->card)->streams, next) {
721 			if ((stream->use & mode) == 0)
722 				continue;
723 
724 			nchannels = stream->nmono + 2 * stream->nstereo;
725 			if (nchannels == 2)
726 				designations = B_CHANNEL_STEREO_BUS;
727 			else
728 				designations = B_CHANNEL_SURROUND_BUS;
729 
730 			for (i = 0; i < nchannels; i++) {
731 				chans[index].channel_id = index;
732 				chans[index].kind = (mode == EMU_USE_PLAY) ? B_MULTI_OUTPUT_CHANNEL : B_MULTI_INPUT_CHANNEL;
733 				chans[index].designations = designations | chan_designations[i];
734 				chans[index].connectors = 0;
735 				index++;
736 			}
737 		}
738 
739 		if (mode==EMU_USE_PLAY) {
740 			multi->output_channel_count = index;
741 		} else {
742 			multi->input_channel_count = index - multi->output_channel_count;
743 		}
744 	}
745 
746 	chans[index].channel_id = index;
747 	chans[index].kind = B_MULTI_OUTPUT_BUS;
748 	chans[index].designations = B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS;
749 	chans[index].connectors = B_CHANNEL_MINI_JACK_STEREO;
750 	index++;
751 
752 	chans[index].channel_id = index;
753 	chans[index].kind = B_MULTI_OUTPUT_BUS;
754 	chans[index].designations = B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS;
755 	chans[index].connectors = B_CHANNEL_MINI_JACK_STEREO;
756 	index++;
757 
758 	multi->output_bus_channel_count = index - multi->output_channel_count
759 		- multi->input_channel_count;
760 
761 	chans[index].channel_id = index;
762 	chans[index].kind = B_MULTI_INPUT_BUS;
763 	chans[index].designations = B_CHANNEL_LEFT | B_CHANNEL_STEREO_BUS;
764 	chans[index].connectors = B_CHANNEL_MINI_JACK_STEREO;
765 	index++;
766 
767 	chans[index].channel_id = index;
768 	chans[index].kind = B_MULTI_INPUT_BUS;
769 	chans[index].designations = B_CHANNEL_RIGHT | B_CHANNEL_STEREO_BUS;
770 	chans[index].connectors = B_CHANNEL_MINI_JACK_STEREO;
771 	index++;
772 
773 	multi->input_bus_channel_count = index - multi->output_channel_count
774 		- multi->input_channel_count - multi->output_bus_channel_count;
775 
776 	multi->aux_bus_channel_count = 0;
777 }
778 
779 
780 static status_t
781 emuxki_get_description(emuxki_dev *card, multi_description *data)
782 {
783 	int32 size;
784 
785 	data->interface_version = B_CURRENT_INTERFACE_VERSION;
786 	data->interface_minimum = B_CURRENT_INTERFACE_VERSION;
787 
788 	if (IS_AUDIGY2_VALUE(&card->config))
789 		strncpy(data->friendly_name, FRIENDLY_NAME_AUDIGY2_VALUE, 32);
790 	else if (IS_AUDIGY2(&card->config))
791 		strncpy(data->friendly_name, FRIENDLY_NAME_AUDIGY2, 32);
792 	else if (IS_AUDIGY(&card->config))
793 		strncpy(data->friendly_name, FRIENDLY_NAME_AUDIGY, 32);
794 	else if (IS_LIVE_5_1(&card->config))
795 		strncpy(data->friendly_name, FRIENDLY_NAME_LIVE_5_1, 32);
796 	else
797 		strncpy(data->friendly_name, FRIENDLY_NAME_LIVE, 32);
798 	strcpy(data->vendor_info, AUTHOR);
799 
800 	/*data->output_channel_count = 6;
801 	data->input_channel_count = 4;
802 	data->output_bus_channel_count = 2;
803 	data->input_bus_channel_count = 2;
804 	data->aux_bus_channel_count = 0;*/
805 
806 	data->output_channel_count = card->multi.output_channel_count;
807 	data->input_channel_count = card->multi.input_channel_count;
808 	data->output_bus_channel_count = card->multi.output_bus_channel_count;
809 	data->input_bus_channel_count = card->multi.input_bus_channel_count;
810 	data->aux_bus_channel_count = card->multi.aux_bus_channel_count;
811 
812 	size = card->multi.output_channel_count + card->multi.input_channel_count
813 			+ card->multi.output_bus_channel_count + card->multi.input_bus_channel_count
814 			+ card->multi.aux_bus_channel_count;
815 
816 	// for each channel, starting with the first output channel,
817 	// then the second, third..., followed by the first input
818 	// channel, second, third, ..., followed by output bus
819 	// channels and input bus channels and finally auxillary channels,
820 
821 	LOG(("request_channel_count = %d\n",data->request_channel_count));
822 	if (data->request_channel_count >= size) {
823 		LOG(("copying data\n"));
824 		memcpy(data->channels, card->multi.chans, size * sizeof(card->multi.chans[0]));
825 	}
826 
827 	switch (current_settings.sample_rate) {
828 		case 192000: data->output_rates = data->input_rates = B_SR_192000; break;
829 		case 96000: data->output_rates = data->input_rates = B_SR_96000; break;
830 		case 48000: data->output_rates = data->input_rates = B_SR_48000; break;
831 		case 44100: data->output_rates = data->input_rates = B_SR_44100; break;
832 	}
833 	data->min_cvsr_rate = 0;
834 	data->max_cvsr_rate = 48000;
835 
836 	switch (current_settings.bitsPerSample) {
837 		case 8: data->output_formats = data->input_formats = B_FMT_8BIT_U; break;
838 		case 16: data->output_formats = data->input_formats = B_FMT_16BIT; break;
839 		case 24: data->output_formats = data->input_formats = B_FMT_24BIT; break;
840 		case 32: data->output_formats = data->input_formats = B_FMT_32BIT; break;
841 	}
842 	data->lock_sources = B_MULTI_LOCK_INTERNAL;
843 	data->timecode_sources = 0;
844 	data->interface_flags = B_MULTI_INTERFACE_PLAYBACK | B_MULTI_INTERFACE_RECORD;
845 	data->start_latency = 3000;
846 
847 	strcpy(data->control_panel,"");
848 
849 	return B_OK;
850 }
851 
852 static status_t
853 emuxki_get_enabled_channels(emuxki_dev *card, multi_channel_enable *data)
854 {
855 	B_SET_CHANNEL(data->enable_bits, 0, true);
856 	B_SET_CHANNEL(data->enable_bits, 1, true);
857 	B_SET_CHANNEL(data->enable_bits, 2, true);
858 	B_SET_CHANNEL(data->enable_bits, 3, true);
859 	data->lock_source = B_MULTI_LOCK_INTERNAL;
860 /*
861 	uint32			lock_source;
862 	int32			lock_data;
863 	uint32			timecode_source;
864 	uint32 *		connectors;
865 */
866 	return B_OK;
867 }
868 
869 static status_t
870 emuxki_set_enabled_channels(emuxki_dev *card, multi_channel_enable *data)
871 {
872 	PRINT(("set_enabled_channels 0 : %s\n", B_TEST_CHANNEL(data->enable_bits, 0) ? "enabled": "disabled"));
873 	PRINT(("set_enabled_channels 1 : %s\n", B_TEST_CHANNEL(data->enable_bits, 1) ? "enabled": "disabled"));
874 	PRINT(("set_enabled_channels 2 : %s\n", B_TEST_CHANNEL(data->enable_bits, 2) ? "enabled": "disabled"));
875 	PRINT(("set_enabled_channels 3 : %s\n", B_TEST_CHANNEL(data->enable_bits, 3) ? "enabled": "disabled"));
876 	return B_OK;
877 }
878 
879 static status_t
880 emuxki_get_global_format(emuxki_dev *card, multi_format_info *data)
881 {
882 	data->output_latency = 0;
883 	data->input_latency = 0;
884 	data->timecode_kind = 0;
885 	switch (current_settings.sample_rate) {
886 		case 192000: data->output.rate = data->input.rate = B_SR_192000; break;
887 		case 96000: data->output.rate = data->input.rate = B_SR_96000; break;
888 		case 48000: data->output.rate = data->input.rate = B_SR_48000; break;
889 		case 44100: data->output.rate = data->input.rate = B_SR_44100; break;
890 	}
891 	switch (current_settings.bitsPerSample) {
892 		case 8: data->input.format = data->output.format = B_FMT_8BIT_U; break;
893 		case 16: data->input.format = data->output.format = B_FMT_16BIT; break;
894 		case 24: data->input.format = data->output.format = B_FMT_24BIT; break;
895 		case 32: data->input.format = data->output.format = B_FMT_32BIT; break;
896 	}
897 	data->input.cvsr = data->output.cvsr = current_settings.sample_rate;
898 	return B_OK;
899 }
900 
901 static status_t
902 emuxki_get_buffers(emuxki_dev *card, multi_buffer_list *data)
903 {
904 	int32 i, j, pchannels, pchannels2, rchannels, rchannels2;
905 
906 	LOG(("flags = %#x\n",data->flags));
907 	LOG(("request_playback_buffers = %#x\n",data->request_playback_buffers));
908 	LOG(("request_playback_channels = %#x\n",data->request_playback_channels));
909 	LOG(("request_playback_buffer_size = %#x\n",data->request_playback_buffer_size));
910 	LOG(("request_record_buffers = %#x\n",data->request_record_buffers));
911 	LOG(("request_record_channels = %#x\n",data->request_record_channels));
912 	LOG(("request_record_buffer_size = %#x\n",data->request_record_buffer_size));
913 
914 	pchannels = card->pstream->nmono + card->pstream->nstereo * 2;
915 	pchannels2 = card->pstream2->nmono + card->pstream2->nstereo * 2;
916 	rchannels = card->rstream->nmono + card->rstream->nstereo * 2;
917 	rchannels2 = card->rstream2->nmono + card->rstream2->nstereo * 2;
918 
919 	if (data->request_playback_buffers < current_settings.buffer_count ||
920 		data->request_playback_channels < (pchannels + pchannels2) ||
921 		data->request_record_buffers < current_settings.buffer_count ||
922 		data->request_record_channels < (rchannels + rchannels2)) {
923 		LOG(("not enough channels/buffers\n"));
924 	}
925 
926 	data->flags = B_MULTI_BUFFER_PLAYBACK | B_MULTI_BUFFER_RECORD;
927 
928 	data->return_playback_buffers = current_settings.buffer_count;	/* playback_buffers[b][] */
929 	data->return_playback_channels = pchannels + pchannels2;		/* playback_buffers[][c] */
930 	data->return_playback_buffer_size = current_settings.buffer_frames;		/* frames */
931 
932 	for (i = 0; i < current_settings.buffer_count; i++) {
933 		struct buffer_desc descs[pchannels];
934 		for (j=0; j<pchannels; j++)
935 			emuxki_stream_get_nth_buffer(card->pstream, j, i,
936 				&descs[j].base,
937 				&descs[j].stride);
938 		if (!IS_USER_ADDRESS(data->playback_buffers[i])
939 			|| user_memcpy(data->playback_buffers[i], descs, sizeof(descs))
940 			< B_OK) {
941 			return B_BAD_ADDRESS;
942 		}
943 	}
944 	for (i = 0; i < current_settings.buffer_count; i++) {
945 		struct buffer_desc descs[pchannels2];
946 		for (j=0; j<pchannels2; j++)
947 			emuxki_stream_get_nth_buffer(card->pstream2, j, i,
948 				&descs[j].base,
949 				&descs[j].stride);
950 		if (!IS_USER_ADDRESS(data->playback_buffers[i])
951 			|| user_memcpy(&data->playback_buffers[i][pchannels], descs, sizeof(descs))
952 			< B_OK) {
953 			return B_BAD_ADDRESS;
954 		}
955 	}
956 
957 	data->return_record_buffers = current_settings.buffer_count;
958 	data->return_record_channels = rchannels + rchannels2;
959 	data->return_record_buffer_size = current_settings.buffer_frames;	/* frames */
960 
961 	for (i = 0; i < current_settings.buffer_count; i++) {
962 		struct buffer_desc descs[rchannels];
963 		for (j=0; j<rchannels; j++)
964 			emuxki_stream_get_nth_buffer(card->rstream, j, i,
965 				&descs[j].base,
966 				&descs[j].stride);
967 		if (!IS_USER_ADDRESS(data->record_buffers[i])
968 			|| user_memcpy(data->record_buffers[i], descs, sizeof(descs))
969 			< B_OK) {
970 			return B_BAD_ADDRESS;
971 		}
972 	}
973 
974 	for (i = 0; i < current_settings.buffer_count; i++) {
975 		struct buffer_desc descs[rchannels2];
976 		for (j=0; j<rchannels2; j++)
977 			emuxki_stream_get_nth_buffer(card->rstream2, j, i,
978 				&descs[j].base,
979 				&descs[j].stride);
980 		if (!IS_USER_ADDRESS(data->record_buffers[i])
981 			|| user_memcpy(&data->record_buffers[i][rchannels], descs, sizeof(descs))
982 			< B_OK) {
983 			return B_BAD_ADDRESS;
984 		}
985 	}
986 
987 	return B_OK;
988 }
989 
990 
991 static void
992 emuxki_play_inth(void* inthparams)
993 {
994 	emuxki_stream *stream = (emuxki_stream *)inthparams;
995 	//int32 count;
996 
997 	acquire_spinlock(&slock);
998 	stream->real_time = system_time();
999 	stream->frames_count += current_settings.buffer_frames;
1000 	stream->buffer_cycle = stream->first_voice->trigblk;
1001 	stream->update_needed = true;
1002 	release_spinlock(&slock);
1003 
1004 	//get_sem_count(stream->card->buffer_ready_sem, &count);
1005 	//if (count <= 0)
1006 		release_sem_etc(stream->card->buffer_ready_sem, 1, B_DO_NOT_RESCHEDULE);
1007 }
1008 
1009 static void
1010 emuxki_record_inth(void* inthparams)
1011 {
1012 	emuxki_stream *stream = (emuxki_stream *)inthparams;
1013 	//int32 count;
1014 
1015 	//TRACE(("emuxki_record_inth\n"));
1016 
1017 	acquire_spinlock(&slock);
1018 	stream->real_time = system_time();
1019 	stream->frames_count += current_settings.buffer_frames;
1020 	stream->buffer_cycle = (stream->first_voice->trigblk
1021 		+ stream->first_voice->blkmod -1) % stream->first_voice->blkmod;
1022 	stream->update_needed = true;
1023 	release_spinlock(&slock);
1024 
1025 	//get_sem_count(stream->card->buffer_ready_sem, &count);
1026 	//if (count <= 0)
1027 		release_sem_etc(stream->card->buffer_ready_sem, 1, B_DO_NOT_RESCHEDULE);
1028 }
1029 
1030 static status_t
1031 emuxki_buffer_exchange(emuxki_dev *card, multi_buffer_info *data)
1032 {
1033 	cpu_status status;
1034 	emuxki_stream *pstream, *rstream;
1035 	multi_buffer_info buffer_info;
1036 
1037 #ifdef __HAIKU__
1038 	if (user_memcpy(&buffer_info, data, sizeof(buffer_info)) < B_OK)
1039 		return B_BAD_ADDRESS;
1040 #else
1041 	memcpy(&buffer_info, data, sizeof(buffer_info));
1042 #endif
1043 
1044 	buffer_info.flags = B_MULTI_BUFFER_PLAYBACK | B_MULTI_BUFFER_RECORD;
1045 
1046 	if (!(card->pstream->state & EMU_STATE_STARTED))
1047 		emuxki_stream_start(card->pstream, emuxki_play_inth, card->pstream);
1048 
1049 	if (!(card->pstream2->state & EMU_STATE_STARTED))
1050 		emuxki_stream_start(card->pstream2, emuxki_play_inth, card->pstream2);
1051 
1052 	if (!(card->rstream->state & EMU_STATE_STARTED))
1053 		emuxki_stream_start(card->rstream, emuxki_record_inth, card->rstream);
1054 
1055 	if (!(card->rstream2->state & EMU_STATE_STARTED))
1056 		emuxki_stream_start(card->rstream2, emuxki_record_inth, card->rstream2);
1057 
1058 
1059 	if (acquire_sem_etc(card->buffer_ready_sem, 1, B_RELATIVE_TIMEOUT | B_CAN_INTERRUPT, 50000)
1060 		== B_TIMED_OUT) {
1061 		LOG(("buffer_exchange timeout ff\n"));
1062 		LOG(("EMU_IPR = %#08x\n",emuxki_reg_read_32(&card->config, EMU_IPR)));
1063 		LOG(("EMU_INTE = %#08x\n",emuxki_reg_read_32(&card->config, EMU_INTE)));
1064 		LOG(("EMU_HCFG = %#08x\n",emuxki_reg_read_32(&card->config, EMU_HCFG)));
1065 	}
1066 
1067 	status = lock();
1068 
1069 	LIST_FOREACH(pstream, &card->streams, next) {
1070 		if ((pstream->use & EMU_USE_PLAY) == 0 ||
1071 			(pstream->state & EMU_STATE_STARTED) == 0)
1072 			continue;
1073 		if (pstream->update_needed)
1074 			break;
1075 	}
1076 
1077 	LIST_FOREACH(rstream, &card->streams, next) {
1078 		if ((rstream->use & EMU_USE_RECORD) == 0 ||
1079 			(rstream->state & EMU_STATE_STARTED) == 0)
1080 			continue;
1081 		if (rstream->update_needed)
1082 			break;
1083 	}
1084 
1085 	if (!pstream)
1086 		pstream = card->pstream;
1087 	if (!rstream)
1088 		rstream = card->rstream;
1089 
1090 	/* do playback */
1091 	buffer_info.playback_buffer_cycle = pstream->buffer_cycle;
1092 	buffer_info.played_real_time = pstream->real_time;
1093 	buffer_info.played_frames_count = pstream->frames_count;
1094 	buffer_info._reserved_0 = pstream->first_channel;
1095 	pstream->update_needed = false;
1096 
1097 	/* do record */
1098 	buffer_info.record_buffer_cycle = rstream->buffer_cycle;
1099 	buffer_info.recorded_frames_count = rstream->frames_count;
1100 	buffer_info.recorded_real_time = rstream->real_time;
1101 	buffer_info._reserved_1 = rstream->first_channel;
1102 	rstream->update_needed = false;
1103 	unlock(status);
1104 
1105 #ifdef __HAIKU__
1106 	if (user_memcpy(data, &buffer_info, sizeof(buffer_info)) < B_OK)
1107 		return B_BAD_ADDRESS;
1108 #else
1109 	memcpy(data, &buffer_info, sizeof(buffer_info));
1110 #endif
1111 
1112 	//TRACE(("buffer_exchange ended\n"));
1113 	return B_OK;
1114 }
1115 
1116 static status_t
1117 emuxki_buffer_force_stop(emuxki_dev *card)
1118 {
1119 	return B_OK;
1120 }
1121 
1122 static status_t
1123 emuxki_multi_control(void *cookie, uint32 op, void *data, size_t length)
1124 {
1125 	emuxki_dev *card = (emuxki_dev *)cookie;
1126 
1127     switch (op) {
1128 		case B_MULTI_GET_DESCRIPTION:
1129 			LOG(("B_MULTI_GET_DESCRIPTION\n"));
1130 			return emuxki_get_description(card, (multi_description *)data);
1131 		case B_MULTI_GET_EVENT_INFO:
1132 			LOG(("B_MULTI_GET_EVENT_INFO\n"));
1133 			return B_ERROR;
1134 		case B_MULTI_SET_EVENT_INFO:
1135 			LOG(("B_MULTI_SET_EVENT_INFO\n"));
1136 			return B_ERROR;
1137 		case B_MULTI_GET_EVENT:
1138 			LOG(("B_MULTI_GET_EVENT\n"));
1139 			return B_ERROR;
1140 		case B_MULTI_GET_ENABLED_CHANNELS:
1141 			LOG(("B_MULTI_GET_ENABLED_CHANNELS\n"));
1142 			return emuxki_get_enabled_channels(card, (multi_channel_enable *)data);
1143 		case B_MULTI_SET_ENABLED_CHANNELS:
1144 			LOG(("B_MULTI_SET_ENABLED_CHANNELS\n"));
1145 			return emuxki_set_enabled_channels(card, (multi_channel_enable *)data);
1146 		case B_MULTI_GET_GLOBAL_FORMAT:
1147 			LOG(("B_MULTI_GET_GLOBAL_FORMAT\n"));
1148 			return emuxki_get_global_format(card, (multi_format_info *)data);
1149 		case B_MULTI_SET_GLOBAL_FORMAT:
1150 			LOG(("B_MULTI_SET_GLOBAL_FORMAT\n"));
1151 			return B_OK; /* XXX BUG! we *MUST* return B_OK, returning B_ERROR will prevent
1152 						  * BeOS to accept the format returned in B_MULTI_GET_GLOBAL_FORMAT
1153 						  */
1154 		case B_MULTI_GET_CHANNEL_FORMATS:
1155 			LOG(("B_MULTI_GET_CHANNEL_FORMATS\n"));
1156 			return B_ERROR;
1157 		case B_MULTI_SET_CHANNEL_FORMATS:	/* only implemented if possible */
1158 			LOG(("B_MULTI_SET_CHANNEL_FORMATS\n"));
1159 			return B_ERROR;
1160 		case B_MULTI_GET_MIX:
1161 			LOG(("B_MULTI_GET_MIX\n"));
1162 			return emuxki_get_mix(card, (multi_mix_value_info *)data);
1163 		case B_MULTI_SET_MIX:
1164 			LOG(("B_MULTI_SET_MIX\n"));
1165 			return emuxki_set_mix(card, (multi_mix_value_info *)data);
1166 		case B_MULTI_LIST_MIX_CHANNELS:
1167 			LOG(("B_MULTI_LIST_MIX_CHANNELS\n"));
1168 			return emuxki_list_mix_channels(card, (multi_mix_channel_info *)data);
1169 		case B_MULTI_LIST_MIX_CONTROLS:
1170 			LOG(("B_MULTI_LIST_MIX_CONTROLS\n"));
1171 			return emuxki_list_mix_controls(card, (multi_mix_control_info *)data);
1172 		case B_MULTI_LIST_MIX_CONNECTIONS:
1173 			LOG(("B_MULTI_LIST_MIX_CONNECTIONS\n"));
1174 			return emuxki_list_mix_connections(card, (multi_mix_connection_info *)data);
1175 		case B_MULTI_GET_BUFFERS:			/* Fill out the struct for the first time; doesn't start anything. */
1176 			LOG(("B_MULTI_GET_BUFFERS\n"));
1177 			return emuxki_get_buffers(card, data);
1178 		case B_MULTI_SET_BUFFERS:			/* Set what buffers to use, if the driver supports soft buffers. */
1179 			LOG(("B_MULTI_SET_BUFFERS\n"));
1180 			return B_ERROR; /* we do not support soft buffers */
1181 		case B_MULTI_SET_START_TIME:			/* When to actually start */
1182 			LOG(("B_MULTI_SET_START_TIME\n"));
1183 			return B_ERROR;
1184 		case B_MULTI_BUFFER_EXCHANGE:		/* stop and go are derived from this being called */
1185 			//TRACE(("B_MULTI_BUFFER_EXCHANGE\n"));
1186 			return emuxki_buffer_exchange(card, (multi_buffer_info *)data);
1187 		case B_MULTI_BUFFER_FORCE_STOP:		/* force stop of playback, nothing in data */
1188 			LOG(("B_MULTI_BUFFER_FORCE_STOP\n"));
1189 			return emuxki_buffer_force_stop(card);
1190 	}
1191 	LOG(("ERROR: unknown multi_control %#x\n",op));
1192 	return B_ERROR;
1193 }
1194 
1195 static status_t emuxki_open(const char *name, uint32 flags, void** cookie);
1196 static status_t emuxki_close(void* cookie);
1197 static status_t emuxki_free(void* cookie);
1198 static status_t emuxki_control(void* cookie, uint32 op, void* arg, size_t len);
1199 static status_t emuxki_read(void* cookie, off_t position, void *buf, size_t* num_bytes);
1200 static status_t emuxki_write(void* cookie, off_t position, const void* buffer, size_t* num_bytes);
1201 
1202 device_hooks multi_hooks = {
1203 	emuxki_open, 			/* -> open entry point */
1204 	emuxki_close, 			/* -> close entry point */
1205 	emuxki_free,			/* -> free cookie */
1206 	emuxki_control, 		/* -> control entry point */
1207 	emuxki_read,			/* -> read entry point */
1208 	emuxki_write,			/* -> write entry point */
1209 	NULL,					/* start select */
1210 	NULL,					/* stop select */
1211 	NULL,					/* scatter-gather read from the device */
1212 	NULL					/* scatter-gather write to the device */
1213 };
1214 
1215 static status_t
1216 emuxki_open(const char *name, uint32 flags, void** cookie)
1217 {
1218 	emuxki_dev *card = NULL;
1219 	emuxki_recparams recparams;
1220 	int ix;
1221 
1222 	LOG(("open()\n"));
1223 
1224 	for (ix=0; ix<num_cards; ix++) {
1225 		if (!strcmp(cards[ix].name, name)) {
1226 			card = &cards[ix];
1227 		}
1228 	}
1229 
1230 	if (card == NULL) {
1231 		LOG(("open() card not found %s\n", name));
1232 		for (ix=0; ix<num_cards; ix++) {
1233 			LOG(("open() card available %s\n", cards[ix].name));
1234 		}
1235 		return B_ERROR;
1236 	}
1237 
1238 	LOG(("open() got card\n"));
1239 
1240 	if (card->pstream !=NULL)
1241 		return B_ERROR;
1242 	if (card->pstream2 !=NULL)
1243 		return B_ERROR;
1244 	if (card->rstream !=NULL)
1245 		return B_ERROR;
1246 	if (card->rstream2 !=NULL)
1247 		return B_ERROR;
1248 
1249 	*cookie = card;
1250 	card->multi.card = card;
1251 
1252 	LOG(("voice_new\n"));
1253 
1254 	card->rstream2 = emuxki_stream_new(card, EMU_USE_RECORD, current_settings.buffer_frames, current_settings.buffer_count);
1255 	card->rstream = emuxki_stream_new(card, EMU_USE_RECORD, current_settings.buffer_frames, current_settings.buffer_count);
1256 	card->pstream2 = emuxki_stream_new(card, EMU_USE_PLAY, current_settings.buffer_frames, current_settings.buffer_count);
1257 	card->pstream = emuxki_stream_new(card, EMU_USE_PLAY, current_settings.buffer_frames, current_settings.buffer_count);
1258 
1259 	card->buffer_ready_sem = create_sem(0,"pbuffer ready");
1260 
1261 	LOG(("voice_setaudio\n"));
1262 
1263 	emuxki_stream_set_audioparms(card->pstream, true, current_settings.channels,
1264 		current_settings.bitsPerSample == 16, current_settings.sample_rate);
1265 	emuxki_stream_set_audioparms(card->pstream2, false, 4,
1266 		current_settings.bitsPerSample == 16, current_settings.sample_rate);
1267 	emuxki_stream_set_audioparms(card->rstream, true, current_settings.channels,
1268 		current_settings.bitsPerSample == 16, current_settings.sample_rate);
1269 	emuxki_stream_set_audioparms(card->rstream2, true, current_settings.channels,
1270 		current_settings.bitsPerSample == 16, current_settings.sample_rate);
1271 	recparams.efx_voices[0] = 3; // channels 1,2
1272 	recparams.efx_voices[1] = 0;
1273 	emuxki_stream_set_recparms(card->rstream, EMU_RECSRC_ADC, NULL);
1274 	emuxki_stream_set_recparms(card->rstream2, EMU_RECSRC_FX, &recparams);
1275 
1276 	card->pstream->first_channel = 0;
1277 	card->pstream2->first_channel = current_settings.channels;
1278 	card->rstream->first_channel = current_settings.channels + 4;
1279 	card->rstream2->first_channel = 2 * current_settings.channels + 4;
1280 
1281 	emuxki_stream_commit_parms(card->pstream);
1282 	emuxki_stream_commit_parms(card->pstream2);
1283 	emuxki_stream_commit_parms(card->rstream);
1284 	emuxki_stream_commit_parms(card->rstream2);
1285 
1286 	emuxki_create_channels_list(&card->multi);
1287 
1288 	return B_OK;
1289 }
1290 
1291 static status_t
1292 emuxki_close(void* cookie)
1293 {
1294 	//emuxki_dev *card = cookie;
1295 	LOG(("close()\n"));
1296 
1297 	return B_OK;
1298 }
1299 
1300 static status_t
1301 emuxki_free(void* cookie)
1302 {
1303 	emuxki_dev *card = cookie;
1304 	emuxki_stream *stream;
1305 	LOG(("free()\n"));
1306 
1307 	if (card->buffer_ready_sem > B_OK)
1308 			delete_sem(card->buffer_ready_sem);
1309 
1310 	LIST_FOREACH(stream, &card->streams, next) {
1311 		emuxki_stream_halt(stream);
1312 	}
1313 
1314 	while (!LIST_EMPTY(&card->streams)) {
1315 		emuxki_stream_delete(LIST_FIRST(&card->streams));
1316 	}
1317 
1318 	card->pstream = NULL;
1319 	card->pstream2 = NULL;
1320 	card->rstream = NULL;
1321 	card->rstream2 = NULL;
1322 
1323 	return B_OK;
1324 }
1325 
1326 static status_t
1327 emuxki_control(void* cookie, uint32 op, void* arg, size_t len)
1328 {
1329 	return emuxki_multi_control(cookie, op, arg, len);
1330 }
1331 
1332 static status_t
1333 emuxki_read(void* cookie, off_t position, void *buf, size_t* num_bytes)
1334 {
1335 	*num_bytes = 0;				/* tell caller nothing was read */
1336 	return B_IO_ERROR;
1337 }
1338 
1339 static status_t
1340 emuxki_write(void* cookie, off_t position, const void* buffer, size_t* num_bytes)
1341 {
1342 	*num_bytes = 0;				/* tell caller nothing was written */
1343 	return B_IO_ERROR;
1344 }
1345 
1346