xref: /haiku/src/add-ons/kernel/drivers/audio/usb/Device.cpp (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
1 /*
2  *	Driver for USB Audio Device Class devices.
3  *	Copyright (c) 2009-13 S.Zharski <imker@gmx.li>
4  *	Distributed under the terms of the MIT license.
5  *
6  */
7 
8 
9 #include "Device.h"
10 
11 #include <kernel.h>
12 #include <usb/USB_audio.h>
13 
14 #include "Driver.h"
15 #include "Settings.h"
16 
17 
18 Device::Device(usb_device device)
19 	:
20 	fStatus(B_ERROR),
21 	fOpen(false),
22 	fRemoved(false),
23 	fDevice(device),
24 	fNonBlocking(false),
25 	fAudioControl(this),
26 	fBuffersReadySem(-1)
27 {
28 	const usb_device_descriptor* deviceDescriptor
29 		= gUSBModule->get_device_descriptor(device);
30 
31 	if (deviceDescriptor == NULL) {
32 		TRACE(ERR, "Error of getting USB device descriptor.\n");
33 		return;
34 	}
35 
36 	fVendorID = deviceDescriptor->vendor_id;
37 	fProductID = deviceDescriptor->product_id;
38 	fUSBVersion = deviceDescriptor->usb_version;
39 
40 	fBuffersReadySem = create_sem(0, DRIVER_NAME "_buffers_ready");
41 	if (fBuffersReadySem < B_OK) {
42 		TRACE(ERR, "Error of creating ready "
43 			"buffers semaphore:%#010x\n", fBuffersReadySem);
44 		return;
45 	}
46 
47 	if (_SetupEndpoints() != B_OK)
48 		return;
49 
50 	// must be set in derived class constructor
51 	fStatus = B_OK;
52 }
53 
54 
55 Device::~Device()
56 {
57 	for (Vector<Stream*>::Iterator I = fStreams.Begin();
58 			I != fStreams.End(); I++)
59 		delete *I;
60 
61 	fStreams.MakeEmpty();
62 
63 	if (fBuffersReadySem > B_OK)
64 		delete_sem(fBuffersReadySem);
65 }
66 
67 
68 status_t
69 Device::Open(uint32 flags)
70 {
71 	if (fOpen)
72 		return B_BUSY;
73 	if (fRemoved)
74 		return B_ERROR;
75 
76 	status_t result = StartDevice();
77 	if (result != B_OK)
78 		return result;
79 
80 	// TODO: are we need this???
81 	fNonBlocking = (flags & O_NONBLOCK) == O_NONBLOCK;
82 	fOpen = true;
83 	return result;
84 }
85 
86 
87 status_t
88 Device::Close()
89 {
90 	if (fRemoved) {
91 		fOpen = false;
92 		return B_OK;
93 	}
94 
95 	for (int i = 0; i < fStreams.Count(); i++)
96 		fStreams[i]->Stop();
97 
98 	fOpen = false;
99 
100 	return StopDevice();
101 }
102 
103 
104 status_t
105 Device::Free()
106 {
107 	return B_OK;
108 }
109 
110 
111 status_t
112 Device::Read(uint8* buffer, size_t* numBytes)
113 {
114 	*numBytes = 0;
115 	return B_IO_ERROR;
116 }
117 
118 
119 status_t
120 Device::Write(const uint8* buffer, size_t* numBytes)
121 {
122 	*numBytes = 0;
123 	return B_IO_ERROR;
124 }
125 
126 
127 status_t
128 Device::Control(uint32 op, void* buffer, size_t length)
129 {
130 	switch (op) {
131 		case B_MULTI_GET_DESCRIPTION:
132 		{
133 			multi_description description;
134 			multi_channel_info channels[16];
135 			multi_channel_info* originalChannels;
136 
137 			if (user_memcpy(&description, buffer, sizeof(multi_description))
138 					!= B_OK)
139 				return B_BAD_ADDRESS;
140 
141 			originalChannels = description.channels;
142 			description.channels = channels;
143 			if (description.request_channel_count > 16)
144 				description.request_channel_count = 16;
145 
146 			status_t status = _MultiGetDescription(&description);
147 			if (status != B_OK)
148 				return status;
149 
150 			description.channels = originalChannels;
151 			if (user_memcpy(buffer, &description, sizeof(multi_description))
152 					!= B_OK)
153 				return B_BAD_ADDRESS;
154 			return user_memcpy(originalChannels, channels,
155 				sizeof(multi_channel_info) * description.request_channel_count);
156 		}
157 		case B_MULTI_GET_EVENT_INFO:
158 			TRACE(ERR, "B_MULTI_GET_EVENT_INFO n/i\n");
159 			return B_ERROR;
160 
161 		case B_MULTI_SET_EVENT_INFO:
162 			TRACE(ERR, "B_MULTI_SET_EVENT_INFO n/i\n");
163 			return B_ERROR;
164 
165 		case B_MULTI_GET_EVENT:
166 			TRACE(ERR, "B_MULTI_GET_EVENT n/i\n");
167 			return B_ERROR;
168 
169 		case B_MULTI_GET_ENABLED_CHANNELS:
170 		{
171 			multi_channel_enable enable;
172 			uint32 enable_bits;
173 			uchar* orig_enable_bits;
174 
175 			if (user_memcpy(&enable, buffer, sizeof(enable)) != B_OK
176 					|| !IS_USER_ADDRESS(enable.enable_bits)) {
177 				return B_BAD_ADDRESS;
178 			}
179 
180 			orig_enable_bits = enable.enable_bits;
181 			enable.enable_bits = (uchar*)&enable_bits;
182 			status_t status = _MultiGetEnabledChannels(&enable);
183 			if (status != B_OK)
184 				return status;
185 
186 			enable.enable_bits = orig_enable_bits;
187 			if (user_memcpy(enable.enable_bits, &enable_bits,
188 					sizeof(enable_bits)) != B_OK
189 				|| user_memcpy(buffer, &enable,
190 					sizeof(multi_channel_enable)) != B_OK) {
191 				return B_BAD_ADDRESS;
192 			}
193 
194 			return B_OK;
195 		}
196 		case B_MULTI_SET_ENABLED_CHANNELS:
197 		{
198 			multi_channel_enable enable;
199 			uint32 enable_bits;
200 			uchar* orig_enable_bits;
201 
202 			if (user_memcpy(&enable, buffer, sizeof(enable)) != B_OK
203 				|| !IS_USER_ADDRESS(enable.enable_bits)) {
204 				return B_BAD_ADDRESS;
205 			}
206 
207 			orig_enable_bits = enable.enable_bits;
208 			enable.enable_bits = (uchar*)&enable_bits;
209 			status_t status = _MultiSetEnabledChannels(&enable);
210 			if (status != B_OK)
211 				return status;
212 
213 			enable.enable_bits = orig_enable_bits;
214 			if (user_memcpy(enable.enable_bits, &enable_bits,
215 					sizeof(enable_bits)) < B_OK
216 				|| user_memcpy(buffer, &enable, sizeof(multi_channel_enable))
217 					< B_OK) {
218 				return B_BAD_ADDRESS;
219 			}
220 
221 			return B_OK;
222 		}
223 		case B_MULTI_GET_GLOBAL_FORMAT:
224 		{
225 			multi_format_info info;
226 			if (user_memcpy(&info, buffer, sizeof(multi_format_info)) != B_OK)
227 				return B_BAD_ADDRESS;
228 
229 			status_t status = _MultiGetGlobalFormat(&info);
230 			if (status != B_OK)
231 				return status;
232 			if (user_memcpy(buffer, &info, sizeof(multi_format_info)) != B_OK)
233 				return B_BAD_ADDRESS;
234 			return B_OK;
235 		}
236 		case B_MULTI_SET_GLOBAL_FORMAT:
237 		{
238 			multi_format_info info;
239 			if (user_memcpy(&info, buffer, sizeof(multi_format_info)) != B_OK)
240 				return B_BAD_ADDRESS;
241 
242 			status_t status = _MultiSetGlobalFormat(&info);
243 			if (status != B_OK)
244 				return status;
245 			return user_memcpy(buffer, &info, sizeof(multi_format_info));
246 		}
247 		case B_MULTI_GET_CHANNEL_FORMATS:
248 			TRACE(ERR, "B_MULTI_GET_CHANNEL_FORMATS n/i\n");
249 			return B_ERROR;
250 
251 		case B_MULTI_SET_CHANNEL_FORMATS:
252 			TRACE(ERR, "B_MULTI_SET_CHANNEL_FORMATS n/i\n");
253 			return B_ERROR;
254 
255 		case B_MULTI_GET_MIX:
256 		case B_MULTI_SET_MIX: {
257 			multi_mix_value_info info;
258 			if (user_memcpy(&info, buffer, sizeof(multi_mix_value_info)) != B_OK)
259 				return B_BAD_ADDRESS;
260 
261 			multi_mix_value* originalValues = info.values;
262 			size_t mixValueSize = info.item_count * sizeof(multi_mix_value);
263 			multi_mix_value* values = (multi_mix_value*)alloca(mixValueSize);
264 			if (user_memcpy(values, info.values, mixValueSize) != B_OK)
265 				return B_BAD_ADDRESS;
266 			info.values = values;
267 
268 			status_t status;
269 			if (op == B_MULTI_GET_MIX)
270 				status = _MultiGetMix(&info);
271 			else
272 				status = _MultiSetMix(&info);
273 			if (status != B_OK)
274 				return status;
275 			// the multi_mix_value_info is not modified
276 			return user_memcpy(originalValues, values, mixValueSize);
277 		}
278 
279 		case B_MULTI_LIST_MIX_CHANNELS:
280 			TRACE(ERR, "B_MULTI_LIST_MIX_CHANNELS n/i\n");
281 			return B_ERROR;
282 
283 		case B_MULTI_LIST_MIX_CONTROLS:
284 		{
285 			multi_mix_control_info info;
286 			multi_mix_control* original_controls;
287 			size_t allocSize;
288 			multi_mix_control *controls;
289 
290 			if (user_memcpy(&info, buffer, sizeof(multi_mix_control_info))
291 				!= B_OK) {
292 				return B_BAD_ADDRESS;
293 			}
294 
295 			original_controls = info.controls;
296 			allocSize = sizeof(multi_mix_control) * info.control_count;
297 			controls = (multi_mix_control *)malloc(allocSize);
298 			if (controls == NULL)
299 				return B_NO_MEMORY;
300 
301 			if (!IS_USER_ADDRESS(info.controls)
302 				|| user_memcpy(controls, info.controls, allocSize) < B_OK) {
303 				free(controls);
304 				return B_BAD_ADDRESS;
305 			}
306 			info.controls = controls;
307 
308 			status_t status = _MultiListMixControls(&info);
309 			if (status != B_OK) {
310 				free(controls);
311 				return status;
312 			}
313 
314 			info.controls = original_controls;
315 			status = user_memcpy(info.controls, controls, allocSize);
316 			if (status == B_OK) {
317 				status = user_memcpy(buffer, &info,
318 					sizeof(multi_mix_control_info));
319 			}
320 			if (status != B_OK)
321 				status = B_BAD_ADDRESS;
322 			free(controls);
323 			return status;
324 		}
325 		case B_MULTI_LIST_MIX_CONNECTIONS:
326 			TRACE(ERR, "B_MULTI_LIST_MIX_CONNECTIONS n/i\n");
327 			return B_ERROR;
328 
329 		case B_MULTI_GET_BUFFERS:
330 			// Fill out the struct for the first time; doesn't start anything.
331 		{
332 			multi_buffer_list list;
333 			if (user_memcpy(&list, buffer, sizeof(multi_buffer_list)) != B_OK)
334 				return B_BAD_ADDRESS;
335 			buffer_desc **original_playback_descs = list.playback_buffers;
336 			buffer_desc **original_record_descs = list.record_buffers;
337 
338 			buffer_desc *playback_descs[list.request_playback_buffers];
339 			buffer_desc *record_descs[list.request_record_buffers];
340 
341 			if (!IS_USER_ADDRESS(list.playback_buffers)
342 				|| user_memcpy(playback_descs, list.playback_buffers,
343 					sizeof(buffer_desc*) * list.request_playback_buffers)
344 					< B_OK
345 				|| !IS_USER_ADDRESS(list.record_buffers)
346 				|| user_memcpy(record_descs, list.record_buffers,
347 					sizeof(buffer_desc*) * list.request_record_buffers)
348 					< B_OK) {
349 				return B_BAD_ADDRESS;
350 			}
351 
352 			list.playback_buffers = playback_descs;
353 			list.record_buffers = record_descs;
354 			status_t status = _MultiGetBuffers(&list);
355 			if (status != B_OK)
356 				return status;
357 
358 			list.playback_buffers = original_playback_descs;
359 			list.record_buffers = original_record_descs;
360 
361 			if (user_memcpy(buffer, &list, sizeof(multi_buffer_list)) < B_OK
362 				|| user_memcpy(original_playback_descs, playback_descs,
363 					sizeof(buffer_desc*) * list.request_playback_buffers)
364 					< B_OK
365 				|| user_memcpy(original_record_descs, record_descs,
366 					sizeof(buffer_desc*) * list.request_record_buffers)
367 					< B_OK) {
368 				status = B_BAD_ADDRESS;
369 			}
370 
371 			return status;
372 		}
373 
374 		case B_MULTI_SET_BUFFERS:
375 			// Set what buffers to use, if the driver supports soft buffers.
376 			TRACE(ERR, "B_MULTI_SET_BUFFERS n/i\n");
377 			return B_ERROR; /* we do not support soft buffers */
378 
379 		case B_MULTI_SET_START_TIME:
380 			// When to actually start
381 			TRACE(ERR, "B_MULTI_SET_START_TIME n/i\n");
382 			return B_ERROR;
383 
384 		case B_MULTI_BUFFER_EXCHANGE:
385 			// stop and go are derived from this being called
386 			return _MultiBufferExchange((multi_buffer_info*)buffer);
387 
388 		case B_MULTI_BUFFER_FORCE_STOP:
389 			// force stop of playback, nothing in data
390 			return _MultiBufferForceStop();
391 
392 		default:
393 			TRACE(ERR, "Unhandled IOCTL catched: %#010x\n", op);
394 	}
395 
396 	return B_DEV_INVALID_IOCTL;
397 }
398 
399 
400 void
401 Device::Removed()
402 {
403 	fRemoved = true;
404 
405 	for (int i = 0; i < fStreams.Count(); i++)
406 		fStreams[i]->OnRemove();
407 }
408 
409 
410 status_t
411 Device::SetupDevice(bool deviceReplugged)
412 {
413 	return B_OK;
414 }
415 
416 
417 status_t
418 Device::CompareAndReattach(usb_device device)
419 {
420 	const usb_device_descriptor* deviceDescriptor
421 		= gUSBModule->get_device_descriptor(device);
422 
423 	if (deviceDescriptor == NULL) {
424 		TRACE(ERR, "Error of getting USB device descriptor.\n");
425 		return B_ERROR;
426 	}
427 
428 	if (deviceDescriptor->vendor_id != fVendorID
429 		&& deviceDescriptor->product_id != fProductID)
430 		// this certainly isn't the same device
431 		return B_BAD_VALUE;
432 
433 	// this is the same device that was replugged - clear the removed state,
434 	// re- setup the endpoints and transfers and open the device if it was
435 	// previously opened
436 	fDevice = device;
437 	fRemoved = false;
438 	status_t result = _SetupEndpoints();
439 	if (result != B_OK) {
440 		fRemoved = true;
441 		return result;
442 	}
443 
444 	// we need to setup hardware on device replug
445 	result = SetupDevice(true);
446 	if (result != B_OK)
447 		return result;
448 
449 	if (fOpen) {
450 		fOpen = false;
451 		result = Open(fNonBlocking ? O_NONBLOCK : 0);
452 	}
453 
454 	return result;
455 }
456 
457 
458 status_t
459 Device::_MultiGetDescription(multi_description* multiDescription)
460 {
461 	multi_description Description;
462 	if (user_memcpy(&Description, multiDescription,
463 			sizeof(multi_description)) != B_OK)
464 		return B_BAD_ADDRESS;
465 
466 	Description.interface_version = B_CURRENT_INTERFACE_VERSION;
467 	Description.interface_minimum = B_CURRENT_INTERFACE_VERSION;
468 
469 	strlcpy(Description.friendly_name, "USB Audio",
470 		sizeof(Description.friendly_name));
471 
472 	strlcpy(Description.vendor_info, "S.Zharski",
473 		sizeof(Description.vendor_info));
474 
475 	Description.output_channel_count = 0;
476 	Description.input_channel_count = 0;
477 	Description.output_bus_channel_count = 0;
478 	Description.input_bus_channel_count = 0;
479 	Description.aux_bus_channel_count = 0;
480 
481 	Description.output_rates = 0;
482 	Description.input_rates = 0;
483 
484 	Description.min_cvsr_rate = 0;
485 	Description.max_cvsr_rate = 0;
486 
487 	Description.output_formats = 0;
488 	Description.input_formats = 0;
489 	Description.lock_sources = B_MULTI_LOCK_INTERNAL;
490 	Description.timecode_sources = 0;
491 	Description.interface_flags = 0;
492 	Description.start_latency = 3000;
493 
494 	Description.control_panel[0] = '\0';
495 
496 	Vector<multi_channel_info> Channels;
497 
498 	// channels (USB I/O terminals) are already in fStreams in outputs->inputs order.
499 	for (int i = 0; i < fStreams.Count(); i++) {
500 		Vector<_AudioControl*> USBTerminal;
501 		USBTerminal.PushBack(fAudioControl.Find(fStreams[i]->TerminalLink()));
502 
503 		fAudioControl.GetChannelsDescription(Channels, &Description, USBTerminal,
504 			fStreams[i]->IsInput());
505 		fStreams[i]->GetFormatsAndRates(&Description);
506 	}
507 
508 	fAudioControl.GetBusChannelsDescription(Channels, &Description);
509 
510 	// Description.request_channel_count = channels + bus_channels;
511 
512 	TraceMultiDescription(&Description, Channels);
513 
514 	if (user_memcpy(multiDescription, &Description,
515 			sizeof(multi_description)) != B_OK)
516 		return B_BAD_ADDRESS;
517 
518 	if (user_memcpy(multiDescription->channels,
519 			&Channels[0], sizeof(multi_channel_info) * min_c(Channels.Count(),
520 			Description.request_channel_count)) != B_OK)
521 		return B_BAD_ADDRESS;
522 
523 	return B_OK;
524 }
525 
526 
527 void
528 Device::TraceMultiDescription(multi_description* Description,
529 		Vector<multi_channel_info>& Channels)
530 {
531 	TRACE(API, "interface_version:%d\n", Description->interface_version);
532 	TRACE(API, "interface_minimum:%d\n", Description->interface_minimum);
533 	TRACE(API, "friendly_name:%s\n", Description->friendly_name);
534 	TRACE(API, "vendor_info:%s\n", Description->vendor_info);
535 	TRACE(API, "output_channel_count:%d\n", Description->output_channel_count);
536 	TRACE(API, "input_channel_count:%d\n", Description->input_channel_count);
537 	TRACE(API, "output_bus_channel_count:%d\n",
538 		Description->output_bus_channel_count);
539 	TRACE(API, "input_bus_channel_count:%d\n",
540 		Description->input_bus_channel_count);
541 	TRACE(API, "aux_bus_channel_count:%d\n", Description->aux_bus_channel_count);
542 	TRACE(API, "output_rates:%#08x\n", Description->output_rates);
543 	TRACE(API, "input_rates:%#08x\n", Description->input_rates);
544 	TRACE(API, "min_cvsr_rate:%f\n", Description->min_cvsr_rate);
545 	TRACE(API, "max_cvsr_rate:%f\n", Description->max_cvsr_rate);
546 	TRACE(API, "output_formats:%#08x\n", Description->output_formats);
547 	TRACE(API, "input_formats:%#08x\n", Description->input_formats);
548 	TRACE(API, "lock_sources:%d\n", Description->lock_sources);
549 	TRACE(API, "timecode_sources:%d\n", Description->timecode_sources);
550 	TRACE(API, "interface_flags:%#08x\n", Description->interface_flags);
551 	TRACE(API, "start_latency:%d\n", Description->start_latency);
552 	TRACE(API, "control_panel:%s\n", Description->control_panel);
553 
554 	// multi_channel_info* Channels = Description->channels;
555 	// for (int i = 0; i < Description->request_channel_count; i++) {
556 	for (int i = 0; i < Channels.Count(); i++) {
557 		TRACE(API, " channel_id:%d\n", Channels[i].channel_id);
558 		TRACE(API, "  kind:%#02x\n", Channels[i].kind);
559 		TRACE(API, "  designations:%#08x\n", Channels[i].designations);
560 		TRACE(API, "  connectors:%#08x\n", Channels[i].connectors);
561 	}
562 
563 	TRACE(API, "request_channel_count:%d\n\n",
564 		Description->request_channel_count);
565 }
566 
567 
568 status_t
569 Device::_MultiGetEnabledChannels(multi_channel_enable* Enable)
570 {
571 	status_t status = B_OK;
572 
573 	Enable->lock_source = B_MULTI_LOCK_INTERNAL;
574 
575 	uint32 offset = 0;
576 	for (int i = 0; i < fStreams.Count() && status == B_OK; i++)
577 		status = fStreams[i]->GetEnabledChannels(offset, Enable);
578 
579 	return status;
580 }
581 
582 
583 status_t
584 Device::_MultiSetEnabledChannels(multi_channel_enable* Enable)
585 {
586 	status_t status = B_OK;
587 	uint32 offset = 0;
588 	for (int i = 0; i < fStreams.Count() && status == B_OK; i++)
589 		status = fStreams[i]->SetEnabledChannels(offset, Enable);
590 
591 	return status;
592 }
593 
594 
595 status_t
596 Device::_MultiGetGlobalFormat(multi_format_info* Format)
597 {
598 	status_t status = B_OK;
599 
600 	Format->output_latency = 0;
601 	Format->input_latency = 0;
602 	Format->timecode_kind = 0;
603 
604 	// uint32 offset = 0;
605 	for (int i = 0; i < fStreams.Count() && status == B_OK; i++)
606 		status = fStreams[i]->GetGlobalFormat(Format);
607 
608 	return status;
609 }
610 
611 
612 status_t
613 Device::_MultiSetGlobalFormat(multi_format_info* Format)
614 {
615 	status_t status = B_OK;
616 
617 	TRACE(API, "output_latency:%lld\n", Format->output_latency);
618 	TRACE(API, "input_latency:%lld\n",  Format->input_latency);
619 	TRACE(API, "timecode_kind:%#08x\n", Format->timecode_kind);
620 
621 	// uint32 offset = 0;
622 	for (int i = 0; i < fStreams.Count() && status == B_OK; i++)
623 		status = fStreams[i]->SetGlobalFormat(Format);
624 
625 	return status;
626 }
627 
628 
629 status_t
630 Device::_MultiGetBuffers(multi_buffer_list* List)
631 {
632 	status_t status = B_OK;
633 
634 	TRACE(API, "info_size:%d\n"
635 	"request_playback_buffers:%d\n"
636 	"request_playback_channels:%d\n"
637 	"request_playback_buffer_size:%d\n"
638 	"request_record_buffers:%d\n"
639 	"request_record_channels:%d\n"
640 	"request_record_buffer_size:%d\n",
641 		List->info_size,
642 		List->request_playback_buffers,
643 		List->request_playback_channels,
644 		List->request_playback_buffer_size,
645 		List->request_record_buffers,
646 		List->request_record_channels,
647 		List->request_record_buffer_size);
648 
649 	List->flags = 0;
650 	List->return_playback_channels = 0;
651 	List->return_record_channels = 0;
652 
653 	for (int i = 0; i < fStreams.Count() && status == B_OK; i++)
654 		status = fStreams[i]->GetBuffers(List);
655 
656 	TRACE(API, "flags:%#x\n"
657 	"return_playback_buffers:%d\n"
658 	"return_playback_channels:%d\n"
659 	"return_playback_buffer_size:%d\n"
660 	"return_record_buffers:%d\n"
661 	"return_record_channels:%d\n"
662 	"return_record_buffer_size:%d\n",
663 		List->flags,
664 		List->return_playback_buffers,
665 		List->return_playback_channels,
666 		List->return_playback_buffer_size,
667 		List->return_record_buffers,
668 		List->return_record_channels,
669 		List->return_record_buffer_size);
670 
671 #if 0
672 	TRACE(API, "playback buffers\n");
673 	for (int32_t b = 0; b <  List->return_playback_buffers; b++)
674 		for (int32 c = 0; c < List->return_playback_channels; c++)
675 			TRACE(API, "%d:%d %08x:%d\n", b, c, List->playback_buffers[b][c].base,
676 				List->playback_buffers[b][c].stride);
677 
678 	TRACE(API, "record buffers:\n");
679 	for (int32_t b = 0; b <  List->return_record_buffers; b++)
680 		for (int32 c = 0; c < List->return_record_channels; c++)
681 			TRACE(API, "%d:%d %08x:%d\n", b, c, List->record_buffers[b][c].base,
682 				List->record_buffers[b][c].stride);
683 #endif
684 	return B_OK;
685 }
686 
687 
688 status_t
689 Device::_MultiBufferExchange(multi_buffer_info* multiInfo)
690 {
691 	multi_buffer_info Info;
692 	if (!IS_USER_ADDRESS(multiInfo)
693 		|| user_memcpy(&Info, multiInfo, sizeof(multi_buffer_info)) != B_OK) {
694 		return B_BAD_ADDRESS;
695 	}
696 
697 	for (int i = 0; i < fStreams.Count(); i++) {
698 		if (!fStreams[i]->IsRunning())
699 			fStreams[i]->Start();
700 	}
701 
702 	status_t status = acquire_sem_etc(fBuffersReadySem, 1,
703 		B_RELATIVE_TIMEOUT | B_CAN_INTERRUPT, 50000);
704 	if (status == B_TIMED_OUT) {
705 		TRACE(ERR, "Timeout during buffers exchange.\n");
706 		return status;
707 	}
708 
709 	status = B_ERROR;
710 	for (int i = 0; i < fStreams.Count(); i++) {
711 		if (fStreams[i]->ExchangeBuffer(&Info)) {
712 			status = B_OK;
713 			break;
714 		}
715 	}
716 
717 	if (status != B_OK) {
718 		TRACE(ERR, "Error processing buffers:%08x.\n", status);
719 		return status;
720 	}
721 
722 	if (user_memcpy(multiInfo, &Info, sizeof(multi_buffer_info)) != B_OK)
723 		return B_BAD_ADDRESS;
724 
725 	return status;
726 }
727 
728 
729 status_t
730 Device::_MultiBufferForceStop()
731 {
732 	for (int i = 0; i < fStreams.Count(); i++)
733 		fStreams[i]->Stop();
734 	return B_OK;
735 }
736 
737 
738 status_t
739 Device::_MultiGetMix(multi_mix_value_info* Info)
740 {
741 	return fAudioControl.GetMix(Info);
742 }
743 
744 
745 status_t
746 Device::_MultiSetMix(multi_mix_value_info* Info)
747 {
748 	return fAudioControl.SetMix(Info);
749 }
750 
751 
752 status_t
753 Device::_MultiListMixControls(multi_mix_control_info* Info)
754 {
755 	status_t status = fAudioControl.ListMixControls(Info);
756 	TraceListMixControls(Info);
757 	return status;
758 }
759 
760 
761 void
762 Device::TraceListMixControls(multi_mix_control_info* Info)
763 {
764 	TRACE(MIX, "control_count:%d\n.", Info->control_count);
765 
766 	int32 i = 0;
767 	while (Info->controls[i].id > 0) {
768 		multi_mix_control &c = Info->controls[i];
769 		TRACE(MIX, "id:%#08x\n",		c.id);
770 		TRACE(MIX, "flags:%#08x\n",	c.flags);
771 		TRACE(MIX, "master:%#08x\n", c.master);
772 		TRACE(MIX, "parent:%#08x\n", c.parent);
773 		TRACE(MIX, "string:%d\n",	c.string);
774 		TRACE(MIX, "name:%s\n",		c.name);
775 		i++;
776 	}
777 }
778 
779 
780 status_t
781 Device::_SetupEndpoints()
782 {
783 	const usb_configuration_info* config
784 		= gUSBModule->get_nth_configuration(fDevice, 0);
785 
786 	if (config == NULL) {
787 		TRACE(ERR, "Error of getting USB device configuration.\n");
788 		return B_ERROR;
789 	}
790 
791 	if (config->interface_count <= 0) {
792 		TRACE(ERR, "Error:no interfaces found in USB device configuration\n");
793 		return B_ERROR;
794 	}
795 
796 	for (size_t i = 0; i < config->interface_count; i++) {
797 		usb_interface_info* Interface = config->interface[i].active;
798 		if (Interface == NULL || Interface->descr == NULL)
799 			continue;
800 		if (Interface->descr->interface_class != USB_AUDIO_INTERFACE_AUDIO_CLASS)
801 			continue;
802 
803 		switch (Interface->descr->interface_subclass) {
804 			case USB_AUDIO_INTERFACE_AUDIOCONTROL_SUBCLASS:
805 				fAudioControl.Init(i, Interface);
806 				break;
807 			case USB_AUDIO_INTERFACE_AUDIOSTREAMING_SUBCLASS:
808 				{
809 					Stream* stream = new(std::nothrow) Stream(this, i,
810 						&config->interface[i]);
811 					if (stream->Init() == B_OK) {
812 						// put the stream in the correct order:
813 						// first output that input ones.
814 						if (stream->IsInput())
815 							fStreams.PushBack(stream);
816 						else
817 							fStreams.PushFront(stream);
818 					} else
819 						delete stream;
820 				}
821 				break;
822 			default:
823 				TRACE(ERR, "Ignore interface of unsupported subclass %#x.\n",
824 					Interface->descr->interface_subclass);
825 				break;
826 		}
827 	}
828 
829 	if (fAudioControl.InitCheck() == B_OK && fStreams.Count() > 0) {
830 		TRACE(INF, "Found device %#06x:%#06x\n", fVendorID, fProductID);
831 
832 		status_t status = gUSBModule->set_configuration(fDevice, config);
833 		if (status != B_OK)
834 			return status;
835 
836 		for (int i = 0; i < fStreams.Count(); i++)
837 			fStreams[i]->OnSetConfiguration(fDevice, config);
838 
839 		return B_OK;
840 	}
841 
842 	return B_NO_INIT;
843 }
844 
845 
846 status_t
847 Device::StopDevice()
848 {
849 	status_t result = B_OK;
850 
851 	if (result != B_OK)
852 		TRACE(ERR, "Error of writing %#04x RX Control:%#010x\n", 0, result);
853 
854 	//TRACE_RET(result);
855 	return result;
856 }
857 
858