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