1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 */ 4 5 #include <linux/init.h> 6 #include <linux/slab.h> 7 #include <linux/usb.h> 8 #include <linux/usb/audio.h> 9 #include <linux/usb/audio-v2.h> 10 #include <linux/usb/audio-v3.h> 11 12 #include <sound/core.h> 13 #include <sound/pcm.h> 14 15 #include "usbaudio.h" 16 #include "card.h" 17 #include "quirks.h" 18 #include "helper.h" 19 #include "clock.h" 20 #include "format.h" 21 22 /* 23 * parse the audio format type I descriptor 24 * and returns the corresponding pcm format 25 * 26 * @dev: usb device 27 * @fp: audioformat record 28 * @format: the format tag (wFormatTag) 29 * @fmt: the format type descriptor (v1/v2) or AudioStreaming descriptor (v3) 30 */ 31 static u64 parse_audio_format_i_type(struct snd_usb_audio *chip, 32 struct audioformat *fp, 33 u64 format, void *_fmt) 34 { 35 int sample_width, sample_bytes; 36 u64 pcm_formats = 0; 37 38 switch (fp->protocol) { 39 case UAC_VERSION_1: 40 default: { 41 struct uac_format_type_i_discrete_descriptor *fmt = _fmt; 42 if (format >= 64) { 43 usb_audio_info(chip, 44 "%u:%d: invalid format type 0x%llx is detected, processed as PCM\n", 45 fp->iface, fp->altsetting, format); 46 format = UAC_FORMAT_TYPE_I_PCM; 47 } 48 sample_width = fmt->bBitResolution; 49 sample_bytes = fmt->bSubframeSize; 50 format = 1ULL << format; 51 break; 52 } 53 54 case UAC_VERSION_2: { 55 struct uac_format_type_i_ext_descriptor *fmt = _fmt; 56 sample_width = fmt->bBitResolution; 57 sample_bytes = fmt->bSubslotSize; 58 59 if (format & UAC2_FORMAT_TYPE_I_RAW_DATA) { 60 pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL; 61 /* flag potentially raw DSD capable altsettings */ 62 fp->dsd_raw = true; 63 } 64 65 format <<= 1; 66 break; 67 } 68 case UAC_VERSION_3: { 69 struct uac3_as_header_descriptor *as = _fmt; 70 71 sample_width = as->bBitResolution; 72 sample_bytes = as->bSubslotSize; 73 74 if (format & UAC3_FORMAT_TYPE_I_RAW_DATA) 75 pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL; 76 77 format <<= 1; 78 break; 79 } 80 } 81 82 fp->fmt_bits = sample_width; 83 84 if ((pcm_formats == 0) && 85 (format == 0 || format == BIT(UAC_FORMAT_TYPE_I_UNDEFINED))) { 86 /* some devices don't define this correctly... */ 87 usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n", 88 fp->iface, fp->altsetting); 89 format = BIT(UAC_FORMAT_TYPE_I_PCM); 90 } 91 if (format & BIT(UAC_FORMAT_TYPE_I_PCM)) { 92 if (((chip->usb_id == USB_ID(0x0582, 0x0016)) || 93 /* Edirol SD-90 */ 94 (chip->usb_id == USB_ID(0x0582, 0x000c))) && 95 /* Roland SC-D70 */ 96 sample_width == 24 && sample_bytes == 2) 97 sample_bytes = 3; 98 else if (sample_width > sample_bytes * 8) { 99 usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n", 100 fp->iface, fp->altsetting, 101 sample_width, sample_bytes); 102 } 103 /* check the format byte size */ 104 switch (sample_bytes) { 105 case 1: 106 pcm_formats |= SNDRV_PCM_FMTBIT_S8; 107 break; 108 case 2: 109 if (snd_usb_is_big_endian_format(chip, fp)) 110 pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */ 111 else 112 pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE; 113 break; 114 case 3: 115 if (snd_usb_is_big_endian_format(chip, fp)) 116 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */ 117 else 118 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE; 119 break; 120 case 4: 121 pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE; 122 break; 123 default: 124 usb_audio_info(chip, 125 "%u:%d : unsupported sample bitwidth %d in %d bytes\n", 126 fp->iface, fp->altsetting, 127 sample_width, sample_bytes); 128 break; 129 } 130 } 131 if (format & BIT(UAC_FORMAT_TYPE_I_PCM8)) { 132 /* Dallas DS4201 workaround: it advertises U8 format, but really 133 supports S8. */ 134 if (chip->usb_id == USB_ID(0x04fa, 0x4201)) 135 pcm_formats |= SNDRV_PCM_FMTBIT_S8; 136 else 137 pcm_formats |= SNDRV_PCM_FMTBIT_U8; 138 } 139 if (format & BIT(UAC_FORMAT_TYPE_I_IEEE_FLOAT)) 140 pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; 141 if (format & BIT(UAC_FORMAT_TYPE_I_ALAW)) 142 pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW; 143 if (format & BIT(UAC_FORMAT_TYPE_I_MULAW)) 144 pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW; 145 if (format & ~0x3f) { 146 usb_audio_info(chip, 147 "%u:%d : unsupported format bits %#llx\n", 148 fp->iface, fp->altsetting, format); 149 } 150 151 pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes); 152 153 return pcm_formats; 154 } 155 156 static int set_fixed_rate(struct audioformat *fp, int rate, int rate_bits) 157 { 158 kfree(fp->rate_table); 159 fp->rate_table = kmalloc(sizeof(int), GFP_KERNEL); 160 if (!fp->rate_table) 161 return -ENOMEM; 162 fp->nr_rates = 1; 163 fp->rate_min = rate; 164 fp->rate_max = rate; 165 fp->rates = rate_bits; 166 fp->rate_table[0] = rate; 167 return 0; 168 } 169 170 /* set up rate_min, rate_max and rates from the rate table */ 171 static void set_rate_table_min_max(struct audioformat *fp) 172 { 173 unsigned int rate; 174 int i; 175 176 fp->rate_min = INT_MAX; 177 fp->rate_max = 0; 178 fp->rates = 0; 179 for (i = 0; i < fp->nr_rates; i++) { 180 rate = fp->rate_table[i]; 181 fp->rate_min = min(fp->rate_min, rate); 182 fp->rate_max = max(fp->rate_max, rate); 183 fp->rates |= snd_pcm_rate_to_rate_bit(rate); 184 } 185 } 186 187 /* 188 * parse the format descriptor and stores the possible sample rates 189 * on the audioformat table (audio class v1). 190 * 191 * @dev: usb device 192 * @fp: audioformat record 193 * @fmt: the format descriptor 194 * @offset: the start offset of descriptor pointing the rate type 195 * (7 for type I and II, 8 for type II) 196 */ 197 static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp, 198 unsigned char *fmt, int offset) 199 { 200 int nr_rates = fmt[offset]; 201 202 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) { 203 usb_audio_err(chip, 204 "%u:%d : invalid UAC_FORMAT_TYPE desc\n", 205 fp->iface, fp->altsetting); 206 return -EINVAL; 207 } 208 209 if (nr_rates) { 210 /* 211 * build the rate table and bitmap flags 212 */ 213 int r, idx; 214 215 fp->rate_table = kmalloc_array(nr_rates, sizeof(int), 216 GFP_KERNEL); 217 if (fp->rate_table == NULL) 218 return -ENOMEM; 219 220 fp->nr_rates = 0; 221 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) { 222 unsigned int rate = combine_triple(&fmt[idx]); 223 if (!rate) 224 continue; 225 /* C-Media CM6501 mislabels its 96 kHz altsetting */ 226 /* Terratec Aureon 7.1 USB C-Media 6206, too */ 227 /* Ozone Z90 USB C-Media, too */ 228 if (rate == 48000 && nr_rates == 1 && 229 (chip->usb_id == USB_ID(0x0d8c, 0x0201) || 230 chip->usb_id == USB_ID(0x0d8c, 0x0102) || 231 chip->usb_id == USB_ID(0x0d8c, 0x0078) || 232 chip->usb_id == USB_ID(0x0ccd, 0x00b1)) && 233 fp->altsetting == 5 && fp->maxpacksize == 392) 234 rate = 96000; 235 /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */ 236 if (rate == 16000 && 237 (chip->usb_id == USB_ID(0x041e, 0x4064) || 238 chip->usb_id == USB_ID(0x041e, 0x4068))) 239 rate = 8000; 240 241 fp->rate_table[fp->nr_rates++] = rate; 242 } 243 if (!fp->nr_rates) { 244 usb_audio_info(chip, 245 "%u:%d: All rates were zero\n", 246 fp->iface, fp->altsetting); 247 return -EINVAL; 248 } 249 set_rate_table_min_max(fp); 250 } else { 251 /* continuous rates */ 252 fp->rates = SNDRV_PCM_RATE_CONTINUOUS; 253 fp->rate_min = combine_triple(&fmt[offset + 1]); 254 fp->rate_max = combine_triple(&fmt[offset + 4]); 255 } 256 257 /* Jabra Evolve 65 headset */ 258 if (chip->usb_id == USB_ID(0x0b0e, 0x030b)) { 259 /* only 48kHz for playback while keeping 16kHz for capture */ 260 if (fp->nr_rates != 1) 261 return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000); 262 } 263 264 return 0; 265 } 266 267 268 /* 269 * Presonus Studio 1810c supports a limited set of sampling 270 * rates per altsetting but reports the full set each time. 271 * If we don't filter out the unsupported rates and attempt 272 * to configure the card, it will hang refusing to do any 273 * further audio I/O until a hard reset is performed. 274 * 275 * The list of supported rates per altsetting (set of available 276 * I/O channels) is described in the owner's manual, section 2.2. 277 */ 278 static bool s1810c_valid_sample_rate(struct audioformat *fp, 279 unsigned int rate) 280 { 281 switch (fp->altsetting) { 282 case 1: 283 /* All ADAT ports available */ 284 return rate <= 48000; 285 case 2: 286 /* Half of ADAT ports available */ 287 return (rate == 88200 || rate == 96000); 288 case 3: 289 /* Analog I/O only (no S/PDIF nor ADAT) */ 290 return rate >= 176400; 291 default: 292 return false; 293 } 294 return false; 295 } 296 297 /* 298 * Many Focusrite devices supports a limited set of sampling rates per 299 * altsetting. Maximum rate is exposed in the last 4 bytes of Format Type 300 * descriptor which has a non-standard bLength = 10. 301 */ 302 static bool focusrite_valid_sample_rate(struct snd_usb_audio *chip, 303 struct audioformat *fp, 304 unsigned int rate) 305 { 306 struct usb_interface *iface; 307 struct usb_host_interface *alts; 308 unsigned char *fmt; 309 unsigned int max_rate; 310 311 iface = usb_ifnum_to_if(chip->dev, fp->iface); 312 if (!iface) 313 return true; 314 315 alts = &iface->altsetting[fp->altset_idx]; 316 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, 317 NULL, UAC_FORMAT_TYPE); 318 if (!fmt) 319 return true; 320 321 if (fmt[0] == 10) { /* bLength */ 322 max_rate = combine_quad(&fmt[6]); 323 324 /* Validate max rate */ 325 if (max_rate != 48000 && 326 max_rate != 96000 && 327 max_rate != 192000 && 328 max_rate != 384000) { 329 330 usb_audio_info(chip, 331 "%u:%d : unexpected max rate: %u\n", 332 fp->iface, fp->altsetting, max_rate); 333 334 return true; 335 } 336 337 return rate <= max_rate; 338 } 339 340 return true; 341 } 342 343 /* 344 * Helper function to walk the array of sample rate triplets reported by 345 * the device. The problem is that we need to parse whole array first to 346 * get to know how many sample rates we have to expect. 347 * Then fp->rate_table can be allocated and filled. 348 */ 349 static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip, 350 struct audioformat *fp, int nr_triplets, 351 const unsigned char *data) 352 { 353 int i, nr_rates = 0; 354 355 for (i = 0; i < nr_triplets; i++) { 356 int min = combine_quad(&data[2 + 12 * i]); 357 int max = combine_quad(&data[6 + 12 * i]); 358 int res = combine_quad(&data[10 + 12 * i]); 359 unsigned int rate; 360 361 if ((max < 0) || (min < 0) || (res < 0) || (max < min)) 362 continue; 363 364 /* 365 * for ranges with res == 1, we announce a continuous sample 366 * rate range, and this function should return 0 for no further 367 * parsing. 368 */ 369 if (res == 1) { 370 fp->rate_min = min; 371 fp->rate_max = max; 372 fp->rates = SNDRV_PCM_RATE_CONTINUOUS; 373 return 0; 374 } 375 376 for (rate = min; rate <= max; rate += res) { 377 378 /* Filter out invalid rates on Presonus Studio 1810c */ 379 if (chip->usb_id == USB_ID(0x194f, 0x010c) && 380 !s1810c_valid_sample_rate(fp, rate)) 381 goto skip_rate; 382 383 /* Filter out invalid rates on Focusrite devices */ 384 if (USB_ID_VENDOR(chip->usb_id) == 0x1235 && 385 !focusrite_valid_sample_rate(chip, fp, rate)) 386 goto skip_rate; 387 388 if (fp->rate_table) 389 fp->rate_table[nr_rates] = rate; 390 nr_rates++; 391 if (nr_rates >= MAX_NR_RATES) { 392 usb_audio_err(chip, "invalid uac2 rates\n"); 393 break; 394 } 395 396 skip_rate: 397 /* avoid endless loop */ 398 if (res == 0) 399 break; 400 } 401 } 402 403 return nr_rates; 404 } 405 406 /* Line6 Helix series and the Rode Rodecaster Pro don't support the 407 * UAC2_CS_RANGE usb function call. Return a static table of known 408 * clock rates. 409 */ 410 static int line6_parse_audio_format_rates_quirk(struct snd_usb_audio *chip, 411 struct audioformat *fp) 412 { 413 switch (chip->usb_id) { 414 case USB_ID(0x0e41, 0x4241): /* Line6 Helix */ 415 case USB_ID(0x0e41, 0x4242): /* Line6 Helix Rack */ 416 case USB_ID(0x0e41, 0x4244): /* Line6 Helix LT */ 417 case USB_ID(0x0e41, 0x4246): /* Line6 HX-Stomp */ 418 case USB_ID(0x0e41, 0x4253): /* Line6 HX-Stomp XL */ 419 case USB_ID(0x0e41, 0x4247): /* Line6 Pod Go */ 420 case USB_ID(0x0e41, 0x4248): /* Line6 Helix >= fw 2.82 */ 421 case USB_ID(0x0e41, 0x4249): /* Line6 Helix Rack >= fw 2.82 */ 422 case USB_ID(0x0e41, 0x424a): /* Line6 Helix LT >= fw 2.82 */ 423 case USB_ID(0x0e41, 0x424b): /* Line6 Pod Go */ 424 case USB_ID(0x19f7, 0x0011): /* Rode Rodecaster Pro */ 425 return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000); 426 } 427 428 return -ENODEV; 429 } 430 431 /* check whether the given altsetting is supported for the already set rate */ 432 static bool check_valid_altsetting_v2v3(struct snd_usb_audio *chip, int iface, 433 int altsetting) 434 { 435 struct usb_device *dev = chip->dev; 436 __le64 raw_data = 0; 437 u64 data; 438 int err; 439 440 /* we assume 64bit is enough for any altsettings */ 441 if (snd_BUG_ON(altsetting >= 64 - 8)) 442 return false; 443 444 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR, 445 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 446 UAC2_AS_VAL_ALT_SETTINGS << 8, 447 iface, &raw_data, sizeof(raw_data)); 448 if (err < 0) 449 return false; 450 451 data = le64_to_cpu(raw_data); 452 /* first byte contains the bitmap size */ 453 if ((data & 0xff) * 8 < altsetting) 454 return false; 455 if (data & (1ULL << (altsetting + 8))) 456 return true; 457 458 return false; 459 } 460 461 /* 462 * Validate each sample rate with the altsetting 463 * Rebuild the rate table if only partial values are valid 464 */ 465 static int validate_sample_rate_table_v2v3(struct snd_usb_audio *chip, 466 struct audioformat *fp, 467 int clock) 468 { 469 struct usb_device *dev = chip->dev; 470 struct usb_host_interface *alts; 471 unsigned int *table; 472 unsigned int nr_rates; 473 int i, err; 474 u32 bmControls; 475 476 /* performing the rate verification may lead to unexpected USB bus 477 * behavior afterwards by some unknown reason. Do this only for the 478 * known devices. 479 */ 480 if (!(chip->quirk_flags & QUIRK_FLAG_VALIDATE_RATES)) 481 return 0; /* don't perform the validation as default */ 482 483 alts = snd_usb_get_host_interface(chip, fp->iface, fp->altsetting); 484 if (!alts) 485 return 0; 486 487 if (fp->protocol == UAC_VERSION_3) { 488 struct uac3_as_header_descriptor *as = snd_usb_find_csint_desc( 489 alts->extra, alts->extralen, NULL, UAC_AS_GENERAL); 490 bmControls = le32_to_cpu(as->bmControls); 491 } else { 492 struct uac2_as_header_descriptor *as = snd_usb_find_csint_desc( 493 alts->extra, alts->extralen, NULL, UAC_AS_GENERAL); 494 bmControls = as->bmControls; 495 } 496 497 if (!uac_v2v3_control_is_readable(bmControls, 498 UAC2_AS_VAL_ALT_SETTINGS)) 499 return 0; 500 501 table = kcalloc(fp->nr_rates, sizeof(*table), GFP_KERNEL); 502 if (!table) 503 return -ENOMEM; 504 505 /* clear the interface altsetting at first */ 506 usb_set_interface(dev, fp->iface, 0); 507 508 nr_rates = 0; 509 for (i = 0; i < fp->nr_rates; i++) { 510 err = snd_usb_set_sample_rate_v2v3(chip, fp, clock, 511 fp->rate_table[i]); 512 if (err < 0) 513 continue; 514 515 if (check_valid_altsetting_v2v3(chip, fp->iface, fp->altsetting)) 516 table[nr_rates++] = fp->rate_table[i]; 517 } 518 519 if (!nr_rates) { 520 usb_audio_dbg(chip, 521 "No valid sample rate available for %d:%d, assuming a firmware bug\n", 522 fp->iface, fp->altsetting); 523 nr_rates = fp->nr_rates; /* continue as is */ 524 } 525 526 if (fp->nr_rates == nr_rates) { 527 kfree(table); 528 return 0; 529 } 530 531 kfree(fp->rate_table); 532 fp->rate_table = table; 533 fp->nr_rates = nr_rates; 534 return 0; 535 } 536 537 /* 538 * parse the format descriptor and stores the possible sample rates 539 * on the audioformat table (audio class v2 and v3). 540 */ 541 static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip, 542 struct audioformat *fp) 543 { 544 struct usb_device *dev = chip->dev; 545 unsigned char tmp[2], *data; 546 int nr_triplets, data_size, ret = 0, ret_l6; 547 int clock = snd_usb_clock_find_source(chip, fp, false); 548 struct usb_host_interface *ctrl_intf; 549 550 ctrl_intf = snd_usb_find_ctrl_interface(chip, fp->iface); 551 if (clock < 0) { 552 dev_err(&dev->dev, 553 "%s(): unable to find clock source (clock %d)\n", 554 __func__, clock); 555 goto err; 556 } 557 558 /* get the number of sample rates first by only fetching 2 bytes */ 559 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE, 560 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 561 UAC2_CS_CONTROL_SAM_FREQ << 8, 562 snd_usb_ctrl_intf(ctrl_intf) | (clock << 8), 563 tmp, sizeof(tmp)); 564 565 if (ret < 0) { 566 /* line6 helix devices don't support UAC2_CS_CONTROL_SAM_FREQ call */ 567 ret_l6 = line6_parse_audio_format_rates_quirk(chip, fp); 568 if (ret_l6 == -ENODEV) { 569 /* no line6 device found continue showing the error */ 570 dev_err(&dev->dev, 571 "%s(): unable to retrieve number of sample rates (clock %d)\n", 572 __func__, clock); 573 goto err; 574 } 575 if (ret_l6 == 0) { 576 dev_info(&dev->dev, 577 "%s(): unable to retrieve number of sample rates: set it to a predefined value (clock %d).\n", 578 __func__, clock); 579 return 0; 580 } 581 ret = ret_l6; 582 goto err; 583 } 584 585 nr_triplets = (tmp[1] << 8) | tmp[0]; 586 data_size = 2 + 12 * nr_triplets; 587 data = kzalloc(data_size, GFP_KERNEL); 588 if (!data) { 589 ret = -ENOMEM; 590 goto err; 591 } 592 593 /* now get the full information */ 594 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE, 595 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 596 UAC2_CS_CONTROL_SAM_FREQ << 8, 597 snd_usb_ctrl_intf(ctrl_intf) | (clock << 8), 598 data, data_size); 599 600 if (ret < 0) { 601 dev_err(&dev->dev, 602 "%s(): unable to retrieve sample rate range (clock %d)\n", 603 __func__, clock); 604 ret = -EINVAL; 605 goto err_free; 606 } 607 608 /* Call the triplet parser, and make sure fp->rate_table is NULL. 609 * We just use the return value to know how many sample rates we 610 * will have to deal with. */ 611 kfree(fp->rate_table); 612 fp->rate_table = NULL; 613 fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data); 614 615 if (fp->nr_rates == 0) { 616 /* SNDRV_PCM_RATE_CONTINUOUS */ 617 ret = 0; 618 goto err_free; 619 } 620 621 fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL); 622 if (!fp->rate_table) { 623 ret = -ENOMEM; 624 goto err_free; 625 } 626 627 /* Call the triplet parser again, but this time, fp->rate_table is 628 * allocated, so the rates will be stored */ 629 parse_uac2_sample_rate_range(chip, fp, nr_triplets, data); 630 631 ret = validate_sample_rate_table_v2v3(chip, fp, clock); 632 if (ret < 0) 633 goto err_free; 634 635 set_rate_table_min_max(fp); 636 637 err_free: 638 kfree(data); 639 err: 640 return ret; 641 } 642 643 /* 644 * parse the format type I and III descriptors 645 */ 646 static int parse_audio_format_i(struct snd_usb_audio *chip, 647 struct audioformat *fp, u64 format, 648 void *_fmt) 649 { 650 snd_pcm_format_t pcm_format; 651 unsigned int fmt_type; 652 int ret; 653 654 switch (fp->protocol) { 655 default: 656 case UAC_VERSION_1: 657 case UAC_VERSION_2: { 658 struct uac_format_type_i_continuous_descriptor *fmt = _fmt; 659 660 fmt_type = fmt->bFormatType; 661 break; 662 } 663 case UAC_VERSION_3: { 664 /* fp->fmt_type is already set in this case */ 665 fmt_type = fp->fmt_type; 666 break; 667 } 668 } 669 670 if (fmt_type == UAC_FORMAT_TYPE_III) { 671 /* FIXME: the format type is really IECxxx 672 * but we give normal PCM format to get the existing 673 * apps working... 674 */ 675 switch (chip->usb_id) { 676 677 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ 678 if (chip->setup == 0x00 && 679 fp->altsetting == 6) 680 pcm_format = SNDRV_PCM_FORMAT_S16_BE; 681 else 682 pcm_format = SNDRV_PCM_FORMAT_S16_LE; 683 break; 684 default: 685 pcm_format = SNDRV_PCM_FORMAT_S16_LE; 686 } 687 fp->formats = pcm_format_to_bits(pcm_format); 688 } else { 689 fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt); 690 if (!fp->formats) 691 return -EINVAL; 692 } 693 694 /* gather possible sample rates */ 695 /* audio class v1 reports possible sample rates as part of the 696 * proprietary class specific descriptor. 697 * audio class v2 uses class specific EP0 range requests for that. 698 */ 699 switch (fp->protocol) { 700 default: 701 case UAC_VERSION_1: { 702 struct uac_format_type_i_continuous_descriptor *fmt = _fmt; 703 704 fp->channels = fmt->bNrChannels; 705 ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7); 706 break; 707 } 708 case UAC_VERSION_2: 709 case UAC_VERSION_3: { 710 /* fp->channels is already set in this case */ 711 ret = parse_audio_format_rates_v2v3(chip, fp); 712 break; 713 } 714 } 715 716 if (fp->channels < 1) { 717 usb_audio_err(chip, 718 "%u:%d : invalid channels %d\n", 719 fp->iface, fp->altsetting, fp->channels); 720 return -EINVAL; 721 } 722 723 return ret; 724 } 725 726 /* 727 * parse the format type II descriptor 728 */ 729 static int parse_audio_format_ii(struct snd_usb_audio *chip, 730 struct audioformat *fp, 731 u64 format, void *_fmt) 732 { 733 int brate, framesize, ret; 734 735 switch (format) { 736 case UAC_FORMAT_TYPE_II_AC3: 737 /* FIXME: there is no AC3 format defined yet */ 738 // fp->formats = SNDRV_PCM_FMTBIT_AC3; 739 fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */ 740 break; 741 case UAC_FORMAT_TYPE_II_MPEG: 742 fp->formats = SNDRV_PCM_FMTBIT_MPEG; 743 break; 744 default: 745 usb_audio_info(chip, 746 "%u:%d : unknown format tag %#llx is detected. processed as MPEG.\n", 747 fp->iface, fp->altsetting, format); 748 fp->formats = SNDRV_PCM_FMTBIT_MPEG; 749 break; 750 } 751 752 fp->channels = 1; 753 754 switch (fp->protocol) { 755 default: 756 case UAC_VERSION_1: { 757 struct uac_format_type_ii_discrete_descriptor *fmt = _fmt; 758 brate = le16_to_cpu(fmt->wMaxBitRate); 759 framesize = le16_to_cpu(fmt->wSamplesPerFrame); 760 usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); 761 fp->frame_size = framesize; 762 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */ 763 break; 764 } 765 case UAC_VERSION_2: { 766 struct uac_format_type_ii_ext_descriptor *fmt = _fmt; 767 brate = le16_to_cpu(fmt->wMaxBitRate); 768 framesize = le16_to_cpu(fmt->wSamplesPerFrame); 769 usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); 770 fp->frame_size = framesize; 771 ret = parse_audio_format_rates_v2v3(chip, fp); 772 break; 773 } 774 } 775 776 return ret; 777 } 778 779 int snd_usb_parse_audio_format(struct snd_usb_audio *chip, 780 struct audioformat *fp, u64 format, 781 struct uac_format_type_i_continuous_descriptor *fmt, 782 int stream) 783 { 784 int err; 785 786 switch (fmt->bFormatType) { 787 case UAC_FORMAT_TYPE_I: 788 case UAC_FORMAT_TYPE_III: 789 err = parse_audio_format_i(chip, fp, format, fmt); 790 break; 791 case UAC_FORMAT_TYPE_II: 792 err = parse_audio_format_ii(chip, fp, format, fmt); 793 break; 794 default: 795 usb_audio_info(chip, 796 "%u:%d : format type %d is not supported yet\n", 797 fp->iface, fp->altsetting, 798 fmt->bFormatType); 799 return -ENOTSUPP; 800 } 801 fp->fmt_type = fmt->bFormatType; 802 if (err < 0) 803 return err; 804 #if 1 805 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */ 806 /* extigy apparently supports sample rates other than 48k 807 * but not in ordinary way. so we enable only 48k atm. 808 */ 809 if (chip->usb_id == USB_ID(0x041e, 0x3000) || 810 chip->usb_id == USB_ID(0x041e, 0x3020) || 811 chip->usb_id == USB_ID(0x041e, 0x3061)) { 812 if (fmt->bFormatType == UAC_FORMAT_TYPE_I && 813 fp->rates != SNDRV_PCM_RATE_48000 && 814 fp->rates != SNDRV_PCM_RATE_96000) 815 return -ENOTSUPP; 816 } 817 #endif 818 return 0; 819 } 820 821 int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip, 822 struct audioformat *fp, 823 struct uac3_as_header_descriptor *as, 824 int stream) 825 { 826 u64 format = le64_to_cpu(as->bmFormats); 827 int err; 828 829 /* 830 * Type I format bits are D0..D6 831 * This test works because type IV is not supported 832 */ 833 if (format & 0x7f) 834 fp->fmt_type = UAC_FORMAT_TYPE_I; 835 else 836 fp->fmt_type = UAC_FORMAT_TYPE_III; 837 838 err = parse_audio_format_i(chip, fp, format, as); 839 if (err < 0) 840 return err; 841 842 return 0; 843 } 844