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 TRACE(ERR, "Rate %d not found - ignore it.\n", newRate); 270 return B_BAD_INDEX; 271 } 272 } 273 274 return B_OK; 275 } 276 277 278 uint32 279 AudioStreamAlternate::GetSamplingRateId(uint32 rate) 280 { 281 if (rate == 0) 282 rate = fSamplingRate; 283 284 for (size_t i = 0; i < B_COUNT_OF(ratesMap); i++) 285 if (ratesMap[i].rate == rate) 286 return ratesMap[i].rateId; 287 288 TRACE(ERR, "Ignore unsupported sample rate %d.\n", rate); 289 return 0; 290 } 291 292 293 uint32 294 AudioStreamAlternate::GetSamplingRateIds() 295 { 296 TypeIFormatDescriptor* format 297 = static_cast<TypeIFormatDescriptor*>(Format()); 298 299 if (format == NULL) { 300 TRACE(ERR, "Format not set for active alternate\n"); 301 return 0; 302 } 303 304 uint32 rates = 0; 305 Vector<uint32>& frequencies = format->fSampleFrequencies; 306 if (format->fSampleFrequencyType == 0) { // continuous frequencies 307 uint32 min = min_c(frequencies[0], frequencies[1]); 308 uint32 max = max_c(frequencies[0], frequencies[1]); 309 310 for (int i = 0; i < frequencies.Count(); i++) { 311 if (frequencies[i] < min || frequencies[i] > max) 312 continue; 313 rates |= GetSamplingRateId(frequencies[i]); 314 } 315 } else 316 for (int i = 0; i < frequencies.Count(); i++) 317 rates |= GetSamplingRateId(frequencies[i]); 318 319 return rates; 320 } 321 322 323 uint32 324 AudioStreamAlternate::GetFormatId() 325 { 326 TypeIFormatDescriptor* format 327 = static_cast<TypeIFormatDescriptor*>(Format()); 328 329 if (format == NULL || Interface() == NULL) { 330 TRACE(ERR, "Ignore alternate due format " 331 "%#08x or interface %#08x null.\n", format, Interface()); 332 return 0; 333 } 334 335 uint32 formats = 0; 336 switch (Interface()->fFormatTag) { 337 case USB_AUDIO_FORMAT_PCM8: 338 formats = B_FMT_8BIT_U; 339 break; 340 case USB_AUDIO_FORMAT_IEEE_FLOAT: 341 formats = B_FMT_FLOAT; 342 break; 343 case USB_AUDIO_FORMAT_PCM: 344 switch(format->fBitResolution) { 345 case 8: formats = B_FMT_8BIT_S; break; 346 case 16: formats = B_FMT_16BIT; break; 347 case 18: formats = B_FMT_18BIT; break; 348 case 20: formats = B_FMT_20BIT; break; 349 case 24: formats = B_FMT_24BIT; break; 350 case 32: formats = B_FMT_32BIT; break; 351 default: 352 TRACE(ERR, "Ignore unsupported " 353 "bit resolution %d for alternate.\n", 354 format->fBitResolution); 355 break; 356 } 357 break; 358 default: 359 TRACE(ERR, "Ignore unsupported " 360 "format bit resolution %d for alternate.\n", 361 Interface()->fFormatTag); 362 break; 363 } 364 365 return formats; 366 } 367 368 369 uint32 370 AudioStreamAlternate::SamplingRateFromId(uint32 id) 371 { 372 for (size_t i = 0; i < B_COUNT_OF(ratesMap); i++) 373 if (ratesMap[i].rateId == id) 374 return ratesMap[i].rate; 375 376 TRACE(ERR, "Unknown sample rate id: %d.\n", id); 377 return 0; 378 } 379 380 381 status_t 382 AudioStreamAlternate::SetSamplingRateById(uint32 newId) 383 { 384 return SetSamplingRate(SamplingRateFromId(newId)); 385 } 386 387 388 status_t 389 AudioStreamAlternate::SetFormatId(uint32 /*newFormatId*/) 390 { 391 return B_OK; // TODO 392 } 393 394 395 AudioStreamingInterface::AudioStreamingInterface( 396 AudioControlInterface* controlInterface, 397 size_t interface, usb_interface_list* List) 398 : 399 fInterface(interface), 400 fControlInterface(controlInterface), 401 fIsInput(false), 402 fActiveAlternate(0) 403 { 404 TRACE(ERR, "if[%d]:alt_count:%d\n", interface, List->alt_count); 405 406 for (size_t alt = 0; alt < List->alt_count; alt++) { 407 ASInterfaceDescriptor* ASInterface = NULL; 408 ASEndpointDescriptor* ASEndpoint = NULL; 409 _ASFormatDescriptor* ASFormat = NULL; 410 411 usb_interface_info* Interface = &List->alt[alt]; 412 413 TRACE(ERR, "if[%d]:alt[%d]:descrs_count:%d\n", 414 interface, alt, Interface->generic_count); 415 for (size_t i = 0; i < Interface->generic_count; i++) { 416 usb_audiocontrol_header_descriptor* Header 417 = (usb_audiocontrol_header_descriptor*)Interface->generic[i]; 418 419 if (Header->descriptor_type == USB_AUDIO_CS_INTERFACE) { 420 switch(Header->descriptor_subtype) { 421 case USB_AUDIO_AS_GENERAL: 422 if (ASInterface == 0) 423 ASInterface = new(std::nothrow) 424 ASInterfaceDescriptor( 425 (usb_audio_streaming_interface_descriptor*)Header); 426 else 427 TRACE(ERR, "Duplicate AStream interface ignored.\n"); 428 break; 429 case USB_AUDIO_AS_FORMAT_TYPE: 430 if (ASFormat == 0) 431 ASFormat = new(std::nothrow) TypeIFormatDescriptor( 432 (usb_audio_format_descriptor*) Header); 433 else 434 TRACE(ERR, "Duplicate AStream format ignored.\n"); 435 break; 436 default: 437 TRACE(ERR, "Ignore AStream descr subtype %#04x\n", 438 Header->descriptor_subtype); 439 break; 440 } 441 continue; 442 } 443 444 if (Header->descriptor_type == USB_AUDIO_CS_ENDPOINT) { 445 if (ASEndpoint == 0) { 446 usb_endpoint_descriptor* Endpoint 447 = Interface->endpoint[0].descr; 448 ASEndpoint = new(std::nothrow) ASEndpointDescriptor(Endpoint, 449 (usb_audio_streaming_endpoint_descriptor*)Header); 450 } else 451 TRACE(ERR, "Duplicate AStream endpoint ignored.\n"); 452 continue; 453 } 454 455 TRACE(ERR, "Ignore Audio Stream of " 456 "unknown descriptor type %#04x.\n", Header->descriptor_type); 457 } 458 459 fAlternates.Add(new(std::nothrow) AudioStreamAlternate(alt, ASInterface, 460 ASEndpoint, ASFormat)); 461 } 462 } 463 464 465 AudioStreamingInterface::~AudioStreamingInterface() 466 { 467 // we own stream header objects too, so free them 468 for (Vector<AudioStreamAlternate*>::Iterator I = fAlternates.Begin(); 469 I != fAlternates.End(); I++) 470 delete *I; 471 472 fAlternates.MakeEmpty(); 473 } 474 475 476 uint8 477 AudioStreamingInterface::TerminalLink() 478 { 479 if (fAlternates[fActiveAlternate]->Interface() != NULL) 480 return fAlternates[fActiveAlternate]->Interface()->fTerminalLink; 481 return 0; 482 } 483 484 485 AudioChannelCluster* 486 AudioStreamingInterface::ChannelCluster() 487 { 488 _AudioControl* control = fControlInterface->Find(TerminalLink()); 489 if (control == 0) { 490 TRACE(ERR, "Control was not found for terminal id:%d.\n", 491 TerminalLink()); 492 return NULL; 493 } 494 495 return control->OutCluster(); 496 } 497 498 499 void 500 AudioStreamingInterface::GetFormatsAndRates(multi_description* Description) 501 { 502 Description->interface_flags 503 |= fIsInput ? B_MULTI_INTERFACE_RECORD : B_MULTI_INTERFACE_PLAYBACK; 504 505 uint32 rates = fAlternates[fActiveAlternate]->GetSamplingRateIds(); 506 uint32 formats = fAlternates[fActiveAlternate]->GetFormatId(); 507 508 if (fIsInput) { 509 Description->input_rates = rates; 510 Description->input_formats = formats; 511 } else { 512 Description->output_rates = rates; 513 Description->output_formats = formats; 514 } 515 } 516 517