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