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