xref: /haiku/src/add-ons/kernel/drivers/audio/usb/AudioStreamingInterface.cpp (revision dd2a1e350b303b855a50fd64e6cb55618be1ae6a)
1 /*
2  *	Driver for USB Audio Device Class devices.
3  *	Copyright (c) 2009-13 S.Zharski <imker@gmx.li>
4  *	Distributed under the tems of the MIT license.
5  *
6  */
7 
8 
9 #include "AudioStreamingInterface.h"
10 
11 #include <usb/USB_audio.h>
12 
13 #include "Driver.h"
14 #include "Settings.h"
15 
16 
17 static struct RatePair {
18 	uint32 rate;
19 	uint32 rateId;
20 } ratesMap[] = {
21 	{ 8000, B_SR_8000 },
22 	{ 11025, B_SR_11025 },
23 	{ 12000, B_SR_12000 },
24 	{ 16000, B_SR_16000 },
25 	{ 22050, B_SR_22050 },
26 	{ 24000, B_SR_24000 },
27 	{ 32000, B_SR_32000 },
28 	{ 44100, B_SR_44100 },
29 	{ 48000, B_SR_48000 },
30 	{ 64000, B_SR_64000 },
31 	{ 88200, B_SR_88200 },
32 	{ 96000, B_SR_96000 },
33 	{ 176400, B_SR_176400 },
34 	{ 192000, B_SR_192000 },
35 	{ 384000, B_SR_384000 },
36 	{ 1536000, B_SR_1536000 }
37 };
38 
39 
40 //
41 //	Audio Stream information entities
42 //
43 //
44 ASInterfaceDescriptor::ASInterfaceDescriptor(
45 		usb_audio_streaming_interface_descriptor* Descriptor)
46 	:
47 	fTerminalLink(0),
48 	fDelay(0),
49 	fFormatTag(0)
50 {
51 // TODO: what aboput rev 2???????
52 	fTerminalLink = Descriptor->terminal_link;
53 	fDelay = Descriptor->r1.delay;
54 	fFormatTag = Descriptor->r1.format_tag;
55 
56 	TRACE(UAC, "fTerminalLink:%d\n", fTerminalLink);
57 	TRACE(UAC, "fDelay:%d\n", fDelay);
58 	TRACE(UAC, "fFormatTag:%#06x\n", fFormatTag);
59 
60 	// fStatus = B_OK;
61 }
62 
63 
64 ASInterfaceDescriptor::~ASInterfaceDescriptor()
65 {
66 }
67 
68 
69 ASEndpointDescriptor::ASEndpointDescriptor(usb_endpoint_descriptor* Endpoint,
70 		usb_audio_streaming_endpoint_descriptor* Descriptor)
71 	:
72 	fCSAttributes(0),
73 	fLockDelayUnits(0),
74 	fLockDelay(0),
75 	fMaxPacketSize(0),
76 	fEndpointAddress(0),
77 	fEndpointAttributes(0)
78 {
79 	fCSAttributes = Descriptor->attributes;
80 	fLockDelayUnits = Descriptor->lock_delay_units;
81 	fLockDelay = Descriptor->lock_delay;
82 
83 //	usb_endpoint_descriptor* endpoint = Interface->endpoint[0]->descr;
84 	fEndpointAttributes = Endpoint->attributes;
85 	fEndpointAddress = Endpoint->endpoint_address;
86 	fMaxPacketSize = Endpoint->max_packet_size;
87 
88 	TRACE(UAC, "fCSAttributes:%d\n", fCSAttributes);
89 	TRACE(UAC, "fLockDelayUnits:%d\n", fLockDelayUnits);
90 	TRACE(UAC, "fLockDelay:%d\n", fLockDelay);
91 	TRACE(UAC, "fMaxPacketSize:%d\n", fMaxPacketSize);
92 	TRACE(UAC, "fEndpointAddress:%#02x\n", fEndpointAddress);
93 	TRACE(UAC, "fEndpointAttributes:%d\n", fEndpointAttributes);
94 }
95 
96 
97 ASEndpointDescriptor::~ASEndpointDescriptor()
98 {
99 }
100 
101 
102 _ASFormatDescriptor::_ASFormatDescriptor(
103 		usb_audio_format_descriptor* Descriptor)
104 	:
105 	fFormatType(USB_AUDIO_FORMAT_TYPE_UNDEFINED)
106 {
107 	fFormatType = Descriptor->format_type;
108 }
109 
110 
111 _ASFormatDescriptor::~_ASFormatDescriptor()
112 {
113 }
114 
115 
116 uint32
117 _ASFormatDescriptor::GetSamFreq(const usb_audio_sampling_freq& freq)
118 {
119 	return freq.bytes[0] | freq.bytes[1] << 8 | freq.bytes[2] << 16;
120 }
121 
122 
123 usb_audio_sampling_freq
124 _ASFormatDescriptor::GetSamFreq(uint32 samplingRate)
125 {
126 	usb_audio_sampling_freq freq;
127 	for (size_t i = 0; i < 3; i++)
128 		freq.bytes[i] = 0xFF & samplingRate >> 8 * i;
129 	return freq;
130 }
131 
132 
133 TypeIFormatDescriptor::TypeIFormatDescriptor(
134 		usb_audio_format_descriptor* Descriptor)
135 	:
136 	_ASFormatDescriptor(Descriptor),
137 	fNumChannels(0),
138 	fSubframeSize(0),
139 	fBitResolution(0),
140 	fSampleFrequencyType(0)
141 {
142 	/*fStatus =*/ Init(Descriptor);
143 }
144 
145 
146 TypeIFormatDescriptor::~TypeIFormatDescriptor()
147 {
148 }
149 
150 
151 status_t
152 TypeIFormatDescriptor::Init(usb_audio_format_descriptor* Descriptor)
153 {
154 	fNumChannels = Descriptor->typeI.nr_channels;
155 	fSubframeSize = Descriptor->typeI.subframe_size;
156 	fBitResolution = Descriptor->typeI.bit_resolution;
157 	fSampleFrequencyType = Descriptor->typeI.sam_freq_type;
158 
159 	if (fSampleFrequencyType == 0) {
160 		fSampleFrequencies.PushBack(
161 			GetSamFreq(Descriptor->typeI.sam_freqs[0]));
162 		fSampleFrequencies.PushBack(
163 			GetSamFreq(Descriptor->typeI.sam_freqs[1]));
164 	} else
165 		for (size_t i = 0; i < fSampleFrequencyType; i++)
166 			fSampleFrequencies.PushBack(
167 				GetSamFreq(Descriptor->typeI.sam_freqs[i]));
168 
169 	TRACE(UAC, "fNumChannels:%d\n", fNumChannels);
170 	TRACE(UAC, "fSubframeSize:%d\n", fSubframeSize);
171 	TRACE(UAC, "fBitResolution:%d\n", fBitResolution);
172 	TRACE(UAC, "fSampleFrequencyType:%d\n", fSampleFrequencyType);
173 
174 	for (int32 i = 0; i < fSampleFrequencies.Count(); i++)
175 		TRACE(UAC, "Frequency #%d: %d\n", i, fSampleFrequencies[i]);
176 
177 	return B_OK;
178 }
179 
180 
181 TypeIIFormatDescriptor::TypeIIFormatDescriptor(
182 		usb_audio_format_descriptor* Descriptor)
183 	:
184 	_ASFormatDescriptor(Descriptor),
185 	fMaxBitRate(0),
186 	fSamplesPerFrame(0),
187 	fSampleFrequencyType(0),
188 	fSampleFrequencies(0)
189 {
190 }
191 
192 
193 TypeIIFormatDescriptor::~TypeIIFormatDescriptor()
194 {
195 }
196 
197 
198 TypeIIIFormatDescriptor::TypeIIIFormatDescriptor(
199 		usb_audio_format_descriptor* Descriptor)
200 	:
201 	TypeIFormatDescriptor(Descriptor)
202 {
203 }
204 
205 
206 TypeIIIFormatDescriptor::~TypeIIIFormatDescriptor()
207 {
208 }
209 
210 
211 AudioStreamAlternate::AudioStreamAlternate(size_t alternate,
212 		ASInterfaceDescriptor* interface, ASEndpointDescriptor* endpoint,
213 		_ASFormatDescriptor* format)
214 	:
215 	fAlternate(alternate),
216 	fInterface(interface),
217 	fEndpoint(endpoint),
218 	fFormat(format),
219 	fSamplingRate(0)
220 {
221 }
222 
223 
224 AudioStreamAlternate::~AudioStreamAlternate()
225 {
226 	delete fInterface;
227 	delete fEndpoint;
228 	delete fFormat;
229 }
230 
231 
232 status_t
233 AudioStreamAlternate::SetSamplingRate(uint32 newRate)
234 {
235 	TypeIFormatDescriptor* format
236 		= static_cast<TypeIFormatDescriptor*>(Format());
237 
238 	if (format == NULL) {
239 		TRACE(ERR, "Format not set for active alternate\n");
240 		return B_NO_INIT;
241 	}
242 
243 	Vector<uint32>& frequencies = format->fSampleFrequencies;
244 	bool continuous = format->fSampleFrequencyType == 0;
245 
246 	if (newRate == 0) { // by default select max available
247 		fSamplingRate = 0;
248 		if (continuous)
249 			fSamplingRate = max_c(frequencies[0], frequencies[1]);
250 		else
251 			for (int i = 0; i < frequencies.Count(); i++)
252 				fSamplingRate = max_c(fSamplingRate, frequencies[i]);
253 	} else {
254 		if (continuous) {
255 			uint32 min = min_c(frequencies[0], frequencies[1]);
256 			uint32 max = max_c(frequencies[0], frequencies[1]);
257 			if (newRate < min || newRate > max) {
258 				TRACE(ERR, "Rate %d outside of %d - %d ignored.\n",
259 					newRate, min, max);
260 				return B_BAD_INDEX;
261 			}
262 			fSamplingRate = newRate;
263 		} else {
264 			for (int i = 0; i < frequencies.Count(); i++) {
265 				if (newRate == frequencies[i]) {
266 					fSamplingRate = newRate;
267 					return B_OK;
268 				}
269 			}
270 			TRACE(ERR, "Rate %d not found - ignore it.\n", newRate);
271 			return B_BAD_INDEX;
272 		}
273 	}
274 
275 	return B_OK;
276 }
277 
278 
279 uint32
280 AudioStreamAlternate::GetSamplingRateId(uint32 rate)
281 {
282 	if (rate == 0)
283 		rate = fSamplingRate;
284 
285 	for (size_t i = 0; i < B_COUNT_OF(ratesMap); i++)
286 		if (ratesMap[i].rate == rate)
287 			return ratesMap[i].rateId;
288 
289 	TRACE(ERR, "Ignore unsupported sample rate %d.\n", rate);
290 	return 0;
291 }
292 
293 
294 uint32
295 AudioStreamAlternate::GetSamplingRateIds()
296 {
297 	TypeIFormatDescriptor* format
298 		= static_cast<TypeIFormatDescriptor*>(Format());
299 
300 	if (format == NULL) {
301 		TRACE(ERR, "Format not set for active alternate\n");
302 		return 0;
303 	}
304 
305 	uint32 rates = 0;
306 	Vector<uint32>& frequencies = format->fSampleFrequencies;
307 	if (format->fSampleFrequencyType == 0) { // continuous frequencies
308 		uint32 min = min_c(frequencies[0], frequencies[1]);
309 		uint32 max = max_c(frequencies[0], frequencies[1]);
310 
311 		for (int i = 0; i < frequencies.Count(); i++) {
312 			if (frequencies[i] < min || frequencies[i] > max)
313 				continue;
314 			rates |= GetSamplingRateId(frequencies[i]);
315 		}
316 	} else
317 		for (int i = 0; i < frequencies.Count(); i++)
318 			rates |= GetSamplingRateId(frequencies[i]);
319 
320 	return rates;
321 }
322 
323 
324 uint32
325 AudioStreamAlternate::GetFormatId()
326 {
327 	TypeIFormatDescriptor* format
328 		= static_cast<TypeIFormatDescriptor*>(Format());
329 
330 	if (format == NULL || Interface() == NULL) {
331 		TRACE(ERR, "Ignore alternate due format "
332 			"%#08x or interface %#08x null.\n", format, Interface());
333 		return 0;
334 	}
335 
336 	uint32 formats = 0;
337 	switch (Interface()->fFormatTag) {
338 		case USB_AUDIO_FORMAT_PCM8:
339 			formats = B_FMT_8BIT_U;
340 			break;
341 		case USB_AUDIO_FORMAT_IEEE_FLOAT:
342 			formats = B_FMT_FLOAT;
343 			break;
344 		case USB_AUDIO_FORMAT_PCM:
345 			switch(format->fBitResolution) {
346 				case 8: formats = B_FMT_8BIT_S; break;
347 				case 16: formats = B_FMT_16BIT; break;
348 				case 18: formats = B_FMT_18BIT; break;
349 				case 20: formats = B_FMT_20BIT; break;
350 				case 24: formats = B_FMT_24BIT; break;
351 				case 32: formats = B_FMT_32BIT; break;
352 				default:
353 					TRACE(ERR, "Ignore unsupported "
354 						"bit resolution %d for alternate.\n",
355 						format->fBitResolution);
356 					break;
357 			}
358 			break;
359 		default:
360 			TRACE(ERR, "Ignore unsupported "
361 				"format bit resolution %d for alternate.\n",
362 				Interface()->fFormatTag);
363 			break;
364 	}
365 
366 	return formats;
367 }
368 
369 
370 uint32
371 AudioStreamAlternate::SamplingRateFromId(uint32 id)
372 {
373 	for (size_t i = 0; i < B_COUNT_OF(ratesMap); i++)
374 		if (ratesMap[i].rateId == id)
375 			return ratesMap[i].rate;
376 
377 	TRACE(ERR, "Unknown sample rate id: %d.\n", id);
378 	return 0;
379 }
380 
381 
382 status_t
383 AudioStreamAlternate::SetSamplingRateById(uint32 newId)
384 {
385 	return SetSamplingRate(SamplingRateFromId(newId));
386 }
387 
388 
389 status_t
390 AudioStreamAlternate::SetFormatId(uint32 /*newFormatId*/)
391 {
392 	return B_OK; // TODO
393 }
394 
395 
396 AudioStreamingInterface::AudioStreamingInterface(
397 		AudioControlInterface*	controlInterface,
398 		size_t interface, usb_interface_list* List)
399 	:
400 	fInterface(interface),
401 	fControlInterface(controlInterface),
402 	fIsInput(false),
403 	fActiveAlternate(0)
404 {
405 	TRACE(ERR, "if[%d]:alt_count:%d\n", interface, List->alt_count);
406 
407 	for (size_t alt = 0; alt < List->alt_count; alt++) {
408 		ASInterfaceDescriptor*	ASInterface	= NULL;
409 		ASEndpointDescriptor*	ASEndpoint	= NULL;
410 		_ASFormatDescriptor*	ASFormat	= NULL;
411 
412 		usb_interface_info* Interface = &List->alt[alt];
413 
414 		TRACE(ERR, "if[%d]:alt[%d]:descrs_count:%d\n",
415 			interface, alt, Interface->generic_count);
416 		for (size_t i = 0; i < Interface->generic_count; i++) {
417 			usb_audiocontrol_header_descriptor* Header
418 				= (usb_audiocontrol_header_descriptor*)Interface->generic[i];
419 
420 			if (Header->descriptor_type == USB_AUDIO_CS_INTERFACE) {
421 				switch(Header->descriptor_subtype) {
422 					case USB_AUDIO_AS_GENERAL:
423 						if (ASInterface == 0)
424 							ASInterface = new(std::nothrow)
425 								ASInterfaceDescriptor(
426 								(usb_audio_streaming_interface_descriptor*)Header);
427 						else
428 							TRACE(ERR, "Duplicate AStream interface ignored.\n");
429 						break;
430 					case USB_AUDIO_AS_FORMAT_TYPE:
431 						if (ASFormat == 0)
432 							ASFormat = new(std::nothrow) TypeIFormatDescriptor(
433 								(usb_audio_format_descriptor*) Header);
434 						else
435 							TRACE(ERR, "Duplicate AStream format ignored.\n");
436 						break;
437 					default:
438 						TRACE(ERR, "Ignore AStream descr subtype %#04x\n",
439 							Header->descriptor_subtype);
440 						break;
441 				}
442 				continue;
443 			}
444 
445 			if (Header->descriptor_type == USB_AUDIO_CS_ENDPOINT) {
446 				if (ASEndpoint == 0) {
447 					usb_endpoint_descriptor* Endpoint
448 						= Interface->endpoint[0].descr;
449 					ASEndpoint = new(std::nothrow) ASEndpointDescriptor(Endpoint,
450 						(usb_audio_streaming_endpoint_descriptor*)Header);
451 				} else
452 					TRACE(ERR, "Duplicate AStream endpoint ignored.\n");
453 				continue;
454 			}
455 
456 			TRACE(ERR, "Ignore Audio Stream of "
457 				"unknown descriptor type %#04x.\n",	Header->descriptor_type);
458 		}
459 
460 		fAlternates.Add(new(std::nothrow) AudioStreamAlternate(alt, ASInterface,
461 			ASEndpoint, ASFormat));
462 	}
463 }
464 
465 
466 AudioStreamingInterface::~AudioStreamingInterface()
467 {
468 	// we own stream header objects too, so free them
469 	for (Vector<AudioStreamAlternate*>::Iterator I = fAlternates.Begin();
470 			I != fAlternates.End(); I++)
471 		delete *I;
472 
473 	fAlternates.MakeEmpty();
474 }
475 
476 
477 uint8
478 AudioStreamingInterface::TerminalLink()
479 {
480 	if (fAlternates[fActiveAlternate]->Interface() != NULL)
481 		return fAlternates[fActiveAlternate]->Interface()->fTerminalLink;
482 	return 0;
483 }
484 
485 
486 AudioChannelCluster*
487 AudioStreamingInterface::ChannelCluster()
488 {
489 	_AudioControl* control = fControlInterface->Find(TerminalLink());
490 	if (control == 0) {
491 		TRACE(ERR, "Control was not found for terminal id:%d.\n",
492 			TerminalLink());
493 		return NULL;
494 	}
495 
496 	return control->OutCluster();
497 }
498 
499 
500 void
501 AudioStreamingInterface::GetFormatsAndRates(multi_description* Description)
502 {
503 	Description->interface_flags
504 		|= fIsInput ? B_MULTI_INTERFACE_RECORD : B_MULTI_INTERFACE_PLAYBACK;
505 
506 	uint32 rates = fAlternates[fActiveAlternate]->GetSamplingRateIds();
507 	uint32 formats = fAlternates[fActiveAlternate]->GetFormatId();
508 
509 	if (fIsInput) {
510 		Description->input_rates = rates;
511 		Description->input_formats = formats;
512 	} else {
513 		Description->output_rates = rates;
514 		Description->output_formats = formats;
515 	}
516 }
517 
518