xref: /haiku/src/add-ons/kernel/drivers/audio/emuxki/multi.c (revision 079c69cbfd7cd3c97baae91332251c8388a8bb02)
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(&card->config))
781 		strncpy(data->friendly_name, FRIENDLY_NAME_AUDIGY2, 32);
782 	else if(IS_AUDIGY(&card->config))
783 		strncpy(data->friendly_name, FRIENDLY_NAME_AUDIGY, 32);
784 	else if(IS_LIVE_5_1(&card->config))
785 		strncpy(data->friendly_name, FRIENDLY_NAME_LIVE_5_1, 32);
786 	else
787 		strncpy(data->friendly_name, FRIENDLY_NAME_LIVE, 32);
788 	strcpy(data->vendor_info, AUTHOR);
789 
790 	/*data->output_channel_count = 6;
791 	data->input_channel_count = 4;
792 	data->output_bus_channel_count = 2;
793 	data->input_bus_channel_count = 2;
794 	data->aux_bus_channel_count = 0;*/
795 
796 	data->output_channel_count = card->multi.output_channel_count;
797 	data->input_channel_count = card->multi.input_channel_count;
798 	data->output_bus_channel_count = card->multi.output_bus_channel_count;
799 	data->input_bus_channel_count = card->multi.input_bus_channel_count;
800 	data->aux_bus_channel_count = card->multi.aux_bus_channel_count;
801 
802 	size = card->multi.output_channel_count + card->multi.input_channel_count
803 			+ card->multi.output_bus_channel_count + card->multi.input_bus_channel_count
804 			+ card->multi.aux_bus_channel_count;
805 
806 	// for each channel, starting with the first output channel,
807 	// then the second, third..., followed by the first input
808 	// channel, second, third, ..., followed by output bus
809 	// channels and input bus channels and finally auxillary channels,
810 
811 	LOG(("request_channel_count = %d\n",data->request_channel_count));
812 	if (data->request_channel_count >= size) {
813 		LOG(("copying data\n"));
814 		memcpy(data->channels, card->multi.chans, size * sizeof(card->multi.chans[0]));
815 	}
816 
817 	switch (current_settings.sample_rate) {
818 		case 192000: data->output_rates = data->input_rates = B_SR_192000; break;
819 		case 96000: data->output_rates = data->input_rates = B_SR_96000; break;
820 		case 48000: data->output_rates = data->input_rates = B_SR_48000; break;
821 		case 44100: data->output_rates = data->input_rates = B_SR_44100; break;
822 	}
823 	data->min_cvsr_rate = 0;
824 	data->max_cvsr_rate = 48000;
825 
826 	switch (current_settings.bitsPerSample) {
827 		case 8: data->output_formats = data->input_formats = B_FMT_8BIT_U; break;
828 		case 16: data->output_formats = data->input_formats = B_FMT_16BIT; break;
829 		case 24: data->output_formats = data->input_formats = B_FMT_24BIT; break;
830 		case 32: data->output_formats = data->input_formats = B_FMT_32BIT; break;
831 	}
832 	data->lock_sources = B_MULTI_LOCK_INTERNAL;
833 	data->timecode_sources = 0;
834 	data->interface_flags = B_MULTI_INTERFACE_PLAYBACK | B_MULTI_INTERFACE_RECORD;
835 	data->start_latency = 3000;
836 
837 	strcpy(data->control_panel,"");
838 
839 	return B_OK;
840 }
841 
842 static status_t
843 emuxki_get_enabled_channels(emuxki_dev *card, multi_channel_enable *data)
844 {
845 	B_SET_CHANNEL(data->enable_bits, 0, true);
846 	B_SET_CHANNEL(data->enable_bits, 1, true);
847 	B_SET_CHANNEL(data->enable_bits, 2, true);
848 	B_SET_CHANNEL(data->enable_bits, 3, true);
849 	data->lock_source = B_MULTI_LOCK_INTERNAL;
850 /*
851 	uint32			lock_source;
852 	int32			lock_data;
853 	uint32			timecode_source;
854 	uint32 *		connectors;
855 */
856 	return B_OK;
857 }
858 
859 static status_t
860 emuxki_set_enabled_channels(emuxki_dev *card, multi_channel_enable *data)
861 {
862 	PRINT(("set_enabled_channels 0 : %s\n", B_TEST_CHANNEL(data->enable_bits, 0) ? "enabled": "disabled"));
863 	PRINT(("set_enabled_channels 1 : %s\n", B_TEST_CHANNEL(data->enable_bits, 1) ? "enabled": "disabled"));
864 	PRINT(("set_enabled_channels 2 : %s\n", B_TEST_CHANNEL(data->enable_bits, 2) ? "enabled": "disabled"));
865 	PRINT(("set_enabled_channels 3 : %s\n", B_TEST_CHANNEL(data->enable_bits, 3) ? "enabled": "disabled"));
866 	return B_OK;
867 }
868 
869 static status_t
870 emuxki_get_global_format(emuxki_dev *card, multi_format_info *data)
871 {
872 	data->output_latency = 0;
873 	data->input_latency = 0;
874 	data->timecode_kind = 0;
875 	switch (current_settings.sample_rate) {
876 		case 192000: data->output.rate = data->input.rate = B_SR_192000; break;
877 		case 96000: data->output.rate = data->input.rate = B_SR_96000; break;
878 		case 48000: data->output.rate = data->input.rate = B_SR_48000; break;
879 		case 44100: data->output.rate = data->input.rate = B_SR_44100; break;
880 	}
881 	switch (current_settings.bitsPerSample) {
882 		case 8: data->input.format = data->output.format = B_FMT_8BIT_U; break;
883 		case 16: data->input.format = data->output.format = B_FMT_16BIT; break;
884 		case 24: data->input.format = data->output.format = B_FMT_24BIT; break;
885 		case 32: data->input.format = data->output.format = B_FMT_32BIT; break;
886 	}
887 	data->input.cvsr = data->output.cvsr = current_settings.sample_rate;
888 	return B_OK;
889 }
890 
891 static status_t
892 emuxki_get_buffers(emuxki_dev *card, multi_buffer_list *data)
893 {
894 	int32 i, j, pchannels, pchannels2, rchannels, rchannels2;
895 
896 	LOG(("flags = %#x\n",data->flags));
897 	LOG(("request_playback_buffers = %#x\n",data->request_playback_buffers));
898 	LOG(("request_playback_channels = %#x\n",data->request_playback_channels));
899 	LOG(("request_playback_buffer_size = %#x\n",data->request_playback_buffer_size));
900 	LOG(("request_record_buffers = %#x\n",data->request_record_buffers));
901 	LOG(("request_record_channels = %#x\n",data->request_record_channels));
902 	LOG(("request_record_buffer_size = %#x\n",data->request_record_buffer_size));
903 
904 	pchannels = card->pstream->nmono + card->pstream->nstereo * 2;
905 	pchannels2 = card->pstream2->nmono + card->pstream2->nstereo * 2;
906 	rchannels = card->rstream->nmono + card->rstream->nstereo * 2;
907 	rchannels2 = card->rstream2->nmono + card->rstream2->nstereo * 2;
908 
909 	if (data->request_playback_buffers < current_settings.buffer_count ||
910 		data->request_playback_channels < (pchannels + pchannels2) ||
911 		data->request_record_buffers < current_settings.buffer_count ||
912 		data->request_record_channels < (rchannels + rchannels2)) {
913 		LOG(("not enough channels/buffers\n"));
914 	}
915 
916 	data->flags = B_MULTI_BUFFER_PLAYBACK | B_MULTI_BUFFER_RECORD;
917 
918 	data->return_playback_buffers = current_settings.buffer_count;	/* playback_buffers[b][] */
919 	data->return_playback_channels = pchannels + pchannels2;		/* playback_buffers[][c] */
920 	data->return_playback_buffer_size = current_settings.buffer_frames;		/* frames */
921 
922 	for(i=0; i<current_settings.buffer_count; i++)
923 		for(j=0; j<pchannels; j++)
924 			emuxki_stream_get_nth_buffer(card->pstream, j, i,
925 				&data->playback_buffers[i][j].base,
926 				&data->playback_buffers[i][j].stride);
927 
928 	for(i=0; i<current_settings.buffer_count; i++)
929 		for(j=0; j<pchannels2; j++)
930 			emuxki_stream_get_nth_buffer(card->pstream2, j, i,
931 				&data->playback_buffers[i][pchannels + j].base,
932 				&data->playback_buffers[i][pchannels + j].stride);
933 
934 	data->return_record_buffers = current_settings.buffer_count;
935 	data->return_record_channels = rchannels + rchannels2;
936 	data->return_record_buffer_size = current_settings.buffer_frames;	/* frames */
937 
938 	for(i=0; i<current_settings.buffer_count; i++)
939 		for(j=0; j<rchannels; j++)
940 			emuxki_stream_get_nth_buffer(card->rstream, j, i,
941 				&data->record_buffers[i][j].base,
942 				&data->record_buffers[i][j].stride);
943 
944 	for(i=0; i<current_settings.buffer_count; i++)
945 		for(j=0; j<rchannels2; j++)
946 			emuxki_stream_get_nth_buffer(card->rstream2, j, i,
947 				&data->record_buffers[i][rchannels + j].base,
948 				&data->record_buffers[i][rchannels + j].stride);
949 
950 	return B_OK;
951 }
952 
953 
954 static void
955 emuxki_play_inth(void* inthparams)
956 {
957 	emuxki_stream *stream = (emuxki_stream *)inthparams;
958 	//int32 count;
959 
960 	acquire_spinlock(&slock);
961 	stream->real_time = system_time();
962 	stream->frames_count += current_settings.buffer_frames;
963 	stream->buffer_cycle = stream->first_voice->trigblk;
964 	stream->update_needed = true;
965 	release_spinlock(&slock);
966 
967 	//get_sem_count(stream->card->buffer_ready_sem, &count);
968 	//if (count <= 0)
969 		release_sem_etc(stream->card->buffer_ready_sem, 1, B_DO_NOT_RESCHEDULE);
970 }
971 
972 static void
973 emuxki_record_inth(void* inthparams)
974 {
975 	emuxki_stream *stream = (emuxki_stream *)inthparams;
976 	//int32 count;
977 
978 	//TRACE(("emuxki_record_inth\n"));
979 
980 	acquire_spinlock(&slock);
981 	stream->real_time = system_time();
982 	stream->frames_count += current_settings.buffer_frames;
983 	stream->buffer_cycle = (stream->first_voice->trigblk
984 		+ stream->first_voice->blkmod -1) % stream->first_voice->blkmod;
985 	stream->update_needed = true;
986 	release_spinlock(&slock);
987 
988 	//get_sem_count(stream->card->buffer_ready_sem, &count);
989 	//if (count <= 0)
990 		release_sem_etc(stream->card->buffer_ready_sem, 1, B_DO_NOT_RESCHEDULE);
991 }
992 
993 static status_t
994 emuxki_buffer_exchange(emuxki_dev *card, multi_buffer_info *data)
995 {
996 	cpu_status status;
997 	emuxki_stream *pstream, *rstream;
998 
999 	data->flags = B_MULTI_BUFFER_PLAYBACK | B_MULTI_BUFFER_RECORD;
1000 
1001 	if (!(card->pstream->state & EMU_STATE_STARTED))
1002 		emuxki_stream_start(card->pstream, emuxki_play_inth, card->pstream);
1003 
1004 	if (!(card->pstream2->state & EMU_STATE_STARTED))
1005 		emuxki_stream_start(card->pstream2, emuxki_play_inth, card->pstream2);
1006 
1007 	if (!(card->rstream->state & EMU_STATE_STARTED))
1008 		emuxki_stream_start(card->rstream, emuxki_record_inth, card->rstream);
1009 
1010 	if (!(card->rstream2->state & EMU_STATE_STARTED))
1011 		emuxki_stream_start(card->rstream2, emuxki_record_inth, card->rstream2);
1012 
1013 
1014 	if(acquire_sem_etc(card->buffer_ready_sem, 1, B_RELATIVE_TIMEOUT | B_CAN_INTERRUPT, 50000)
1015 		== B_TIMED_OUT) {
1016 		LOG(("buffer_exchange timeout ff\n"));
1017 		LOG(("EMU_IPR = %#08x\n",emuxki_reg_read_32(&card->config, EMU_IPR)));
1018 		LOG(("EMU_INTE = %#08x\n",emuxki_reg_read_32(&card->config, EMU_INTE)));
1019 		LOG(("EMU_HCFG = %#08x\n",emuxki_reg_read_32(&card->config, EMU_HCFG)));
1020 	}
1021 
1022 	status = lock();
1023 
1024 	LIST_FOREACH(pstream, &card->streams, next) {
1025 		if ((pstream->use & EMU_USE_PLAY) == 0 ||
1026 			(pstream->state & EMU_STATE_STARTED) == 0)
1027 			continue;
1028 		if(pstream->update_needed)
1029 			break;
1030 	}
1031 
1032 	LIST_FOREACH(rstream, &card->streams, next) {
1033 		if ((rstream->use & EMU_USE_RECORD) == 0 ||
1034 			(rstream->state & EMU_STATE_STARTED) == 0)
1035 			continue;
1036 		if(rstream->update_needed)
1037 			break;
1038 	}
1039 
1040 	if(!pstream)
1041 		pstream = card->pstream;
1042 	if(!rstream)
1043 		rstream = card->rstream;
1044 
1045 	/* do playback */
1046 	data->playback_buffer_cycle = pstream->buffer_cycle;
1047 	data->played_real_time = pstream->real_time;
1048 	data->played_frames_count = pstream->frames_count;
1049 	data->_reserved_0 = pstream->first_channel;
1050 	pstream->update_needed = false;
1051 
1052 	/* do record */
1053 	data->record_buffer_cycle = rstream->buffer_cycle;
1054 	data->recorded_frames_count = rstream->frames_count;
1055 	data->recorded_real_time = rstream->real_time;
1056 	data->_reserved_1 = rstream->first_channel;
1057 	rstream->update_needed = false;
1058 	unlock(status);
1059 
1060 	//TRACE(("buffer_exchange ended\n"));
1061 	return B_OK;
1062 }
1063 
1064 static status_t
1065 emuxki_buffer_force_stop(emuxki_dev *card)
1066 {
1067 	return B_OK;
1068 }
1069 
1070 static status_t
1071 emuxki_multi_control(void *cookie, uint32 op, void *data, size_t length)
1072 {
1073 	emuxki_dev *card = (emuxki_dev *)cookie;
1074 
1075     switch (op) {
1076 		case B_MULTI_GET_DESCRIPTION:
1077 			LOG(("B_MULTI_GET_DESCRIPTION\n"));
1078 			return emuxki_get_description(card, (multi_description *)data);
1079 		case B_MULTI_GET_EVENT_INFO:
1080 			LOG(("B_MULTI_GET_EVENT_INFO\n"));
1081 			return B_ERROR;
1082 		case B_MULTI_SET_EVENT_INFO:
1083 			LOG(("B_MULTI_SET_EVENT_INFO\n"));
1084 			return B_ERROR;
1085 		case B_MULTI_GET_EVENT:
1086 			LOG(("B_MULTI_GET_EVENT\n"));
1087 			return B_ERROR;
1088 		case B_MULTI_GET_ENABLED_CHANNELS:
1089 			LOG(("B_MULTI_GET_ENABLED_CHANNELS\n"));
1090 			return emuxki_get_enabled_channels(card, (multi_channel_enable *)data);
1091 		case B_MULTI_SET_ENABLED_CHANNELS:
1092 			LOG(("B_MULTI_SET_ENABLED_CHANNELS\n"));
1093 			return emuxki_set_enabled_channels(card, (multi_channel_enable *)data);
1094 		case B_MULTI_GET_GLOBAL_FORMAT:
1095 			LOG(("B_MULTI_GET_GLOBAL_FORMAT\n"));
1096 			return emuxki_get_global_format(card, (multi_format_info *)data);
1097 		case B_MULTI_SET_GLOBAL_FORMAT:
1098 			LOG(("B_MULTI_SET_GLOBAL_FORMAT\n"));
1099 			return B_OK; /* XXX BUG! we *MUST* return B_OK, returning B_ERROR will prevent
1100 						  * BeOS to accept the format returned in B_MULTI_GET_GLOBAL_FORMAT
1101 						  */
1102 		case B_MULTI_GET_CHANNEL_FORMATS:
1103 			LOG(("B_MULTI_GET_CHANNEL_FORMATS\n"));
1104 			return B_ERROR;
1105 		case B_MULTI_SET_CHANNEL_FORMATS:	/* only implemented if possible */
1106 			LOG(("B_MULTI_SET_CHANNEL_FORMATS\n"));
1107 			return B_ERROR;
1108 		case B_MULTI_GET_MIX:
1109 			LOG(("B_MULTI_GET_MIX\n"));
1110 			return emuxki_get_mix(card, (multi_mix_value_info *)data);
1111 		case B_MULTI_SET_MIX:
1112 			LOG(("B_MULTI_SET_MIX\n"));
1113 			return emuxki_set_mix(card, (multi_mix_value_info *)data);
1114 		case B_MULTI_LIST_MIX_CHANNELS:
1115 			LOG(("B_MULTI_LIST_MIX_CHANNELS\n"));
1116 			return emuxki_list_mix_channels(card, (multi_mix_channel_info *)data);
1117 		case B_MULTI_LIST_MIX_CONTROLS:
1118 			LOG(("B_MULTI_LIST_MIX_CONTROLS\n"));
1119 			return emuxki_list_mix_controls(card, (multi_mix_control_info *)data);
1120 		case B_MULTI_LIST_MIX_CONNECTIONS:
1121 			LOG(("B_MULTI_LIST_MIX_CONNECTIONS\n"));
1122 			return emuxki_list_mix_connections(card, (multi_mix_connection_info *)data);
1123 		case B_MULTI_GET_BUFFERS:			/* Fill out the struct for the first time; doesn't start anything. */
1124 			LOG(("B_MULTI_GET_BUFFERS\n"));
1125 			return emuxki_get_buffers(card, data);
1126 		case B_MULTI_SET_BUFFERS:			/* Set what buffers to use, if the driver supports soft buffers. */
1127 			LOG(("B_MULTI_SET_BUFFERS\n"));
1128 			return B_ERROR; /* we do not support soft buffers */
1129 		case B_MULTI_SET_START_TIME:			/* When to actually start */
1130 			LOG(("B_MULTI_SET_START_TIME\n"));
1131 			return B_ERROR;
1132 		case B_MULTI_BUFFER_EXCHANGE:		/* stop and go are derived from this being called */
1133 			//TRACE(("B_MULTI_BUFFER_EXCHANGE\n"));
1134 			return emuxki_buffer_exchange(card, (multi_buffer_info *)data);
1135 		case B_MULTI_BUFFER_FORCE_STOP:		/* force stop of playback, nothing in data */
1136 			LOG(("B_MULTI_BUFFER_FORCE_STOP\n"));
1137 			return emuxki_buffer_force_stop(card);
1138 	}
1139 	LOG(("ERROR: unknown multi_control %#x\n",op));
1140 	return B_ERROR;
1141 }
1142 
1143 static status_t emuxki_open(const char *name, uint32 flags, void** cookie);
1144 static status_t emuxki_close(void* cookie);
1145 static status_t emuxki_free(void* cookie);
1146 static status_t emuxki_control(void* cookie, uint32 op, void* arg, size_t len);
1147 static status_t emuxki_read(void* cookie, off_t position, void *buf, size_t* num_bytes);
1148 static status_t emuxki_write(void* cookie, off_t position, const void* buffer, size_t* num_bytes);
1149 
1150 device_hooks multi_hooks = {
1151 	emuxki_open, 			/* -> open entry point */
1152 	emuxki_close, 			/* -> close entry point */
1153 	emuxki_free,			/* -> free cookie */
1154 	emuxki_control, 		/* -> control entry point */
1155 	emuxki_read,			/* -> read entry point */
1156 	emuxki_write,			/* -> write entry point */
1157 	NULL,					/* start select */
1158 	NULL,					/* stop select */
1159 	NULL,					/* scatter-gather read from the device */
1160 	NULL					/* scatter-gather write to the device */
1161 };
1162 
1163 static status_t
1164 emuxki_open(const char *name, uint32 flags, void** cookie)
1165 {
1166 	emuxki_dev *card = NULL;
1167 	emuxki_recparams recparams;
1168 	int ix;
1169 
1170 	LOG(("open()\n"));
1171 
1172 	for (ix=0; ix<num_cards; ix++) {
1173 		if (!strcmp(cards[ix].name, name)) {
1174 			card = &cards[ix];
1175 		}
1176 	}
1177 
1178 	if(card == NULL) {
1179 		LOG(("open() card not found %s\n", name));
1180 		for (ix=0; ix<num_cards; ix++) {
1181 			LOG(("open() card available %s\n", cards[ix].name));
1182 		}
1183 		return B_ERROR;
1184 	}
1185 
1186 	LOG(("open() got card\n"));
1187 
1188 	if(card->pstream !=NULL)
1189 		return B_ERROR;
1190 	if(card->pstream2 !=NULL)
1191 		return B_ERROR;
1192 	if(card->rstream !=NULL)
1193 		return B_ERROR;
1194 	if(card->rstream2 !=NULL)
1195 		return B_ERROR;
1196 
1197 	*cookie = card;
1198 	card->multi.card = card;
1199 
1200 	LOG(("voice_new\n"));
1201 
1202 	card->rstream2 = emuxki_stream_new(card, EMU_USE_RECORD, current_settings.buffer_frames, current_settings.buffer_count);
1203 	card->rstream = emuxki_stream_new(card, EMU_USE_RECORD, current_settings.buffer_frames, current_settings.buffer_count);
1204 	card->pstream2 = emuxki_stream_new(card, EMU_USE_PLAY, current_settings.buffer_frames, current_settings.buffer_count);
1205 	card->pstream = emuxki_stream_new(card, EMU_USE_PLAY, current_settings.buffer_frames, current_settings.buffer_count);
1206 
1207 	card->buffer_ready_sem = create_sem(0,"pbuffer ready");
1208 
1209 	LOG(("voice_setaudio\n"));
1210 
1211 	emuxki_stream_set_audioparms(card->pstream, true, current_settings.channels,
1212 		current_settings.bitsPerSample == 16, current_settings.sample_rate);
1213 	emuxki_stream_set_audioparms(card->pstream2, false, 4,
1214 		current_settings.bitsPerSample == 16, current_settings.sample_rate);
1215 	emuxki_stream_set_audioparms(card->rstream, true, current_settings.channels,
1216 		current_settings.bitsPerSample == 16, current_settings.sample_rate);
1217 	emuxki_stream_set_audioparms(card->rstream2, true, current_settings.channels,
1218 		current_settings.bitsPerSample == 16, current_settings.sample_rate);
1219 	recparams.efx_voices[0] = 3; // channels 1,2
1220 	recparams.efx_voices[1] = 0;
1221 	emuxki_stream_set_recparms(card->rstream, EMU_RECSRC_ADC, NULL);
1222 	emuxki_stream_set_recparms(card->rstream2, EMU_RECSRC_FX, &recparams);
1223 
1224 	card->pstream->first_channel = 0;
1225 	card->pstream2->first_channel = current_settings.channels;
1226 	card->rstream->first_channel = current_settings.channels + 4;
1227 	card->rstream2->first_channel = 2 * current_settings.channels + 4;
1228 
1229 	emuxki_stream_commit_parms(card->pstream);
1230 	emuxki_stream_commit_parms(card->pstream2);
1231 	emuxki_stream_commit_parms(card->rstream);
1232 	emuxki_stream_commit_parms(card->rstream2);
1233 
1234 	emuxki_create_channels_list(&card->multi);
1235 
1236 	return B_OK;
1237 }
1238 
1239 static status_t
1240 emuxki_close(void* cookie)
1241 {
1242 	//emuxki_dev *card = cookie;
1243 	LOG(("close()\n"));
1244 
1245 	return B_OK;
1246 }
1247 
1248 static status_t
1249 emuxki_free(void* cookie)
1250 {
1251 	emuxki_dev *card = cookie;
1252 	emuxki_stream *stream;
1253 	LOG(("free()\n"));
1254 
1255 	if (card->buffer_ready_sem > B_OK)
1256 			delete_sem(card->buffer_ready_sem);
1257 
1258 	LIST_FOREACH(stream, &card->streams, next) {
1259 		emuxki_stream_halt(stream);
1260 	}
1261 
1262 	while(!LIST_EMPTY(&card->streams)) {
1263 		emuxki_stream_delete(LIST_FIRST(&card->streams));
1264 	}
1265 
1266 	card->pstream = NULL;
1267 	card->pstream2 = NULL;
1268 	card->rstream = NULL;
1269 	card->rstream2 = NULL;
1270 
1271 	return B_OK;
1272 }
1273 
1274 static status_t
1275 emuxki_control(void* cookie, uint32 op, void* arg, size_t len)
1276 {
1277 	return emuxki_multi_control(cookie, op, arg, len);
1278 }
1279 
1280 static status_t
1281 emuxki_read(void* cookie, off_t position, void *buf, size_t* num_bytes)
1282 {
1283 	*num_bytes = 0;				/* tell caller nothing was read */
1284 	return B_IO_ERROR;
1285 }
1286 
1287 static status_t
1288 emuxki_write(void* cookie, off_t position, const void* buffer, size_t* num_bytes)
1289 {
1290 	*num_bytes = 0;				/* tell caller nothing was written */
1291 	return B_IO_ERROR;
1292 }
1293 
1294