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