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 * Many Focusrite devices supports a limited set of sampling rates per 309 * altsetting. Maximum rate is exposed in the last 4 bytes of Format Type 310 * descriptor which has a non-standard bLength = 10. 311 */ 312 static bool focusrite_valid_sample_rate(struct snd_usb_audio *chip, 313 struct audioformat *fp, 314 unsigned int rate) 315 { 316 struct usb_host_interface *alts; 317 unsigned char *fmt; 318 unsigned int max_rate; 319 320 alts = snd_usb_get_host_interface(chip, fp->iface, fp->altsetting); 321 if (!alts) 322 return true; 323 324 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, 325 NULL, UAC_FORMAT_TYPE); 326 if (!fmt) 327 return true; 328 329 if (fmt[0] == 10) { /* bLength */ 330 max_rate = combine_quad(&fmt[6]); 331 332 switch (max_rate) { 333 case 192000: 334 if (rate == 176400 || rate == 192000) 335 return true; 336 fallthrough; 337 case 96000: 338 if (rate == 88200 || rate == 96000) 339 return true; 340 fallthrough; 341 case 48000: 342 return (rate == 44100 || rate == 48000); 343 default: 344 usb_audio_info(chip, 345 "%u:%d : unexpected max rate: %u\n", 346 fp->iface, fp->altsetting, max_rate); 347 348 return true; 349 } 350 } 351 352 return true; 353 } 354 355 /* 356 * Helper function to walk the array of sample rate triplets reported by 357 * the device. The problem is that we need to parse whole array first to 358 * get to know how many sample rates we have to expect. 359 * Then fp->rate_table can be allocated and filled. 360 */ 361 static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip, 362 struct audioformat *fp, int nr_triplets, 363 const unsigned char *data) 364 { 365 int i, nr_rates = 0; 366 367 for (i = 0; i < nr_triplets; i++) { 368 int min = combine_quad(&data[2 + 12 * i]); 369 int max = combine_quad(&data[6 + 12 * i]); 370 int res = combine_quad(&data[10 + 12 * i]); 371 unsigned int rate; 372 373 if ((max < 0) || (min < 0) || (res < 0) || (max < min)) 374 continue; 375 376 /* 377 * for ranges with res == 1, we announce a continuous sample 378 * rate range, and this function should return 0 for no further 379 * parsing. 380 */ 381 if (res == 1) { 382 fp->rate_min = min; 383 fp->rate_max = max; 384 fp->rates = SNDRV_PCM_RATE_CONTINUOUS; 385 return 0; 386 } 387 388 for (rate = min; rate <= max; rate += res) { 389 390 /* Filter out invalid rates on Presonus Studio 1810c */ 391 if (chip->usb_id == USB_ID(0x194f, 0x010c) && 392 !s1810c_valid_sample_rate(fp, rate)) 393 goto skip_rate; 394 /* Filter out invalid rates on Presonus Studio 1824c */ 395 if (chip->usb_id == USB_ID(0x194f, 0x010d) && 396 !s1810c_valid_sample_rate(fp, rate)) 397 goto skip_rate; 398 399 /* Filter out invalid rates on Focusrite devices */ 400 if (USB_ID_VENDOR(chip->usb_id) == 0x1235 && 401 !focusrite_valid_sample_rate(chip, fp, rate)) 402 goto skip_rate; 403 404 if (fp->rate_table) 405 fp->rate_table[nr_rates] = rate; 406 nr_rates++; 407 if (nr_rates >= MAX_NR_RATES) { 408 usb_audio_err(chip, "invalid uac2 rates\n"); 409 break; 410 } 411 412 skip_rate: 413 /* avoid endless loop */ 414 if (res == 0) 415 break; 416 } 417 } 418 419 return nr_rates; 420 } 421 422 /* Line6 Helix series and the Rode Rodecaster Pro don't support the 423 * UAC2_CS_RANGE usb function call. Return a static table of known 424 * clock rates. 425 */ 426 static int line6_parse_audio_format_rates_quirk(struct snd_usb_audio *chip, 427 struct audioformat *fp) 428 { 429 switch (chip->usb_id) { 430 case USB_ID(0x0e41, 0x4241): /* Line6 Helix */ 431 case USB_ID(0x0e41, 0x4242): /* Line6 Helix Rack */ 432 case USB_ID(0x0e41, 0x4244): /* Line6 Helix LT */ 433 case USB_ID(0x0e41, 0x4246): /* Line6 HX-Stomp */ 434 case USB_ID(0x0e41, 0x4253): /* Line6 HX-Stomp XL */ 435 case USB_ID(0x0e41, 0x4247): /* Line6 Pod Go */ 436 case USB_ID(0x0e41, 0x4248): /* Line6 Helix >= fw 2.82 */ 437 case USB_ID(0x0e41, 0x4249): /* Line6 Helix Rack >= fw 2.82 */ 438 case USB_ID(0x0e41, 0x424a): /* Line6 Helix LT >= fw 2.82 */ 439 case USB_ID(0x0e41, 0x424b): /* Line6 Pod Go */ 440 case USB_ID(0x19f7, 0x0011): /* Rode Rodecaster Pro */ 441 return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000); 442 } 443 444 return -ENODEV; 445 } 446 447 /* check whether the given altsetting is supported for the already set rate */ 448 static bool check_valid_altsetting_v2v3(struct snd_usb_audio *chip, int iface, 449 int altsetting) 450 { 451 struct usb_device *dev = chip->dev; 452 __le64 raw_data = 0; 453 u64 data; 454 int err; 455 456 /* we assume 64bit is enough for any altsettings */ 457 if (snd_BUG_ON(altsetting >= 64 - 8)) 458 return false; 459 460 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR, 461 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 462 UAC2_AS_VAL_ALT_SETTINGS << 8, 463 iface, &raw_data, sizeof(raw_data)); 464 if (err < 0) 465 return false; 466 467 data = le64_to_cpu(raw_data); 468 /* first byte contains the bitmap size */ 469 if ((data & 0xff) * 8 < altsetting) 470 return false; 471 if (data & (1ULL << (altsetting + 8))) 472 return true; 473 474 return false; 475 } 476 477 /* 478 * Validate each sample rate with the altsetting 479 * Rebuild the rate table if only partial values are valid 480 */ 481 static int validate_sample_rate_table_v2v3(struct snd_usb_audio *chip, 482 struct audioformat *fp, 483 int clock) 484 { 485 struct usb_device *dev = chip->dev; 486 struct usb_host_interface *alts; 487 unsigned int *table; 488 unsigned int nr_rates; 489 int i, err; 490 u32 bmControls; 491 492 /* performing the rate verification may lead to unexpected USB bus 493 * behavior afterwards by some unknown reason. Do this only for the 494 * known devices. 495 */ 496 if (!(chip->quirk_flags & QUIRK_FLAG_VALIDATE_RATES)) 497 return 0; /* don't perform the validation as default */ 498 499 alts = snd_usb_get_host_interface(chip, fp->iface, fp->altsetting); 500 if (!alts) 501 return 0; 502 503 if (fp->protocol == UAC_VERSION_3) { 504 struct uac3_as_header_descriptor *as = snd_usb_find_csint_desc( 505 alts->extra, alts->extralen, NULL, UAC_AS_GENERAL); 506 bmControls = le32_to_cpu(as->bmControls); 507 } else { 508 struct uac2_as_header_descriptor *as = snd_usb_find_csint_desc( 509 alts->extra, alts->extralen, NULL, UAC_AS_GENERAL); 510 bmControls = as->bmControls; 511 } 512 513 if (!uac_v2v3_control_is_readable(bmControls, 514 UAC2_AS_VAL_ALT_SETTINGS)) 515 return 0; 516 517 table = kcalloc(fp->nr_rates, sizeof(*table), GFP_KERNEL); 518 if (!table) 519 return -ENOMEM; 520 521 /* clear the interface altsetting at first */ 522 usb_set_interface(dev, fp->iface, 0); 523 524 nr_rates = 0; 525 for (i = 0; i < fp->nr_rates; i++) { 526 err = snd_usb_set_sample_rate_v2v3(chip, fp, clock, 527 fp->rate_table[i]); 528 if (err < 0) 529 continue; 530 531 if (check_valid_altsetting_v2v3(chip, fp->iface, fp->altsetting)) 532 table[nr_rates++] = fp->rate_table[i]; 533 } 534 535 if (!nr_rates) { 536 usb_audio_dbg(chip, 537 "No valid sample rate available for %d:%d, assuming a firmware bug\n", 538 fp->iface, fp->altsetting); 539 nr_rates = fp->nr_rates; /* continue as is */ 540 } 541 542 if (fp->nr_rates == nr_rates) { 543 kfree(table); 544 return 0; 545 } 546 547 kfree(fp->rate_table); 548 fp->rate_table = table; 549 fp->nr_rates = nr_rates; 550 return 0; 551 } 552 553 /* 554 * parse the format descriptor and stores the possible sample rates 555 * on the audioformat table (audio class v2 and v3). 556 */ 557 static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip, 558 struct audioformat *fp) 559 { 560 struct usb_device *dev = chip->dev; 561 unsigned char tmp[2], *data; 562 int nr_triplets, data_size, ret = 0, ret_l6; 563 int clock = snd_usb_clock_find_source(chip, fp, false); 564 struct usb_host_interface *ctrl_intf; 565 566 ctrl_intf = snd_usb_find_ctrl_interface(chip, fp->iface); 567 if (clock < 0) { 568 dev_err(&dev->dev, 569 "%s(): unable to find clock source (clock %d)\n", 570 __func__, clock); 571 goto err; 572 } 573 574 /* get the number of sample rates first by only fetching 2 bytes */ 575 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE, 576 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 577 UAC2_CS_CONTROL_SAM_FREQ << 8, 578 snd_usb_ctrl_intf(ctrl_intf) | (clock << 8), 579 tmp, sizeof(tmp)); 580 581 if (ret < 0) { 582 /* line6 helix devices don't support UAC2_CS_CONTROL_SAM_FREQ call */ 583 ret_l6 = line6_parse_audio_format_rates_quirk(chip, fp); 584 if (ret_l6 == -ENODEV) { 585 /* no line6 device found continue showing the error */ 586 dev_err(&dev->dev, 587 "%s(): unable to retrieve number of sample rates (clock %d)\n", 588 __func__, clock); 589 goto err; 590 } 591 if (ret_l6 == 0) { 592 dev_info(&dev->dev, 593 "%s(): unable to retrieve number of sample rates: set it to a predefined value (clock %d).\n", 594 __func__, clock); 595 return 0; 596 } 597 ret = ret_l6; 598 goto err; 599 } 600 601 nr_triplets = (tmp[1] << 8) | tmp[0]; 602 data_size = 2 + 12 * nr_triplets; 603 data = kzalloc(data_size, GFP_KERNEL); 604 if (!data) { 605 ret = -ENOMEM; 606 goto err; 607 } 608 609 /* now get the full information */ 610 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE, 611 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 612 UAC2_CS_CONTROL_SAM_FREQ << 8, 613 snd_usb_ctrl_intf(ctrl_intf) | (clock << 8), 614 data, data_size); 615 616 if (ret < 0) { 617 dev_err(&dev->dev, 618 "%s(): unable to retrieve sample rate range (clock %d)\n", 619 __func__, clock); 620 ret = -EINVAL; 621 goto err_free; 622 } 623 624 /* Call the triplet parser, and make sure fp->rate_table is NULL. 625 * We just use the return value to know how many sample rates we 626 * will have to deal with. */ 627 kfree(fp->rate_table); 628 fp->rate_table = NULL; 629 fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data); 630 631 if (fp->nr_rates == 0) { 632 /* SNDRV_PCM_RATE_CONTINUOUS */ 633 ret = 0; 634 goto err_free; 635 } 636 637 fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL); 638 if (!fp->rate_table) { 639 ret = -ENOMEM; 640 goto err_free; 641 } 642 643 /* Call the triplet parser again, but this time, fp->rate_table is 644 * allocated, so the rates will be stored */ 645 parse_uac2_sample_rate_range(chip, fp, nr_triplets, data); 646 647 ret = validate_sample_rate_table_v2v3(chip, fp, clock); 648 if (ret < 0) 649 goto err_free; 650 651 set_rate_table_min_max(fp); 652 653 err_free: 654 kfree(data); 655 err: 656 return ret; 657 } 658 659 /* 660 * parse the format type I and III descriptors 661 */ 662 static int parse_audio_format_i(struct snd_usb_audio *chip, 663 struct audioformat *fp, u64 format, 664 void *_fmt) 665 { 666 snd_pcm_format_t pcm_format; 667 unsigned int fmt_type; 668 int ret; 669 670 switch (fp->protocol) { 671 default: 672 case UAC_VERSION_1: 673 case UAC_VERSION_2: { 674 struct uac_format_type_i_continuous_descriptor *fmt = _fmt; 675 676 fmt_type = fmt->bFormatType; 677 break; 678 } 679 case UAC_VERSION_3: { 680 /* fp->fmt_type is already set in this case */ 681 fmt_type = fp->fmt_type; 682 break; 683 } 684 } 685 686 if (fmt_type == UAC_FORMAT_TYPE_III) { 687 /* FIXME: the format type is really IECxxx 688 * but we give normal PCM format to get the existing 689 * apps working... 690 */ 691 switch (chip->usb_id) { 692 693 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ 694 if (chip->setup == 0x00 && 695 fp->altsetting == 6) 696 pcm_format = SNDRV_PCM_FORMAT_S16_BE; 697 else 698 pcm_format = SNDRV_PCM_FORMAT_S16_LE; 699 break; 700 default: 701 pcm_format = SNDRV_PCM_FORMAT_S16_LE; 702 } 703 fp->formats = pcm_format_to_bits(pcm_format); 704 } else { 705 fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt); 706 if (!fp->formats) 707 return -EINVAL; 708 } 709 710 /* gather possible sample rates */ 711 /* audio class v1 reports possible sample rates as part of the 712 * proprietary class specific descriptor. 713 * audio class v2 uses class specific EP0 range requests for that. 714 */ 715 switch (fp->protocol) { 716 default: 717 case UAC_VERSION_1: { 718 struct uac_format_type_i_continuous_descriptor *fmt = _fmt; 719 720 fp->channels = fmt->bNrChannels; 721 ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7); 722 break; 723 } 724 case UAC_VERSION_2: 725 case UAC_VERSION_3: { 726 /* fp->channels is already set in this case */ 727 ret = parse_audio_format_rates_v2v3(chip, fp); 728 break; 729 } 730 } 731 732 if (fp->channels < 1) { 733 usb_audio_err(chip, 734 "%u:%d : invalid channels %d\n", 735 fp->iface, fp->altsetting, fp->channels); 736 return -EINVAL; 737 } 738 739 return ret; 740 } 741 742 /* 743 * parse the format type II descriptor 744 */ 745 static int parse_audio_format_ii(struct snd_usb_audio *chip, 746 struct audioformat *fp, 747 u64 format, void *_fmt) 748 { 749 int brate, framesize, ret; 750 751 switch (format) { 752 case UAC_FORMAT_TYPE_II_AC3: 753 /* FIXME: there is no AC3 format defined yet */ 754 // fp->formats = SNDRV_PCM_FMTBIT_AC3; 755 fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */ 756 break; 757 case UAC_FORMAT_TYPE_II_MPEG: 758 fp->formats = SNDRV_PCM_FMTBIT_MPEG; 759 break; 760 default: 761 usb_audio_info(chip, 762 "%u:%d : unknown format tag %#llx is detected. processed as MPEG.\n", 763 fp->iface, fp->altsetting, format); 764 fp->formats = SNDRV_PCM_FMTBIT_MPEG; 765 break; 766 } 767 768 fp->channels = 1; 769 770 switch (fp->protocol) { 771 default: 772 case UAC_VERSION_1: { 773 struct uac_format_type_ii_discrete_descriptor *fmt = _fmt; 774 brate = le16_to_cpu(fmt->wMaxBitRate); 775 framesize = le16_to_cpu(fmt->wSamplesPerFrame); 776 usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); 777 fp->frame_size = framesize; 778 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */ 779 break; 780 } 781 case UAC_VERSION_2: { 782 struct uac_format_type_ii_ext_descriptor *fmt = _fmt; 783 brate = le16_to_cpu(fmt->wMaxBitRate); 784 framesize = le16_to_cpu(fmt->wSamplesPerFrame); 785 usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); 786 fp->frame_size = framesize; 787 ret = parse_audio_format_rates_v2v3(chip, fp); 788 break; 789 } 790 } 791 792 return ret; 793 } 794 795 int snd_usb_parse_audio_format(struct snd_usb_audio *chip, 796 struct audioformat *fp, u64 format, 797 struct uac_format_type_i_continuous_descriptor *fmt, 798 int stream) 799 { 800 int err; 801 802 switch (fmt->bFormatType) { 803 case UAC_FORMAT_TYPE_I: 804 case UAC_FORMAT_TYPE_III: 805 err = parse_audio_format_i(chip, fp, format, fmt); 806 break; 807 case UAC_FORMAT_TYPE_II: 808 err = parse_audio_format_ii(chip, fp, format, fmt); 809 break; 810 default: 811 usb_audio_info(chip, 812 "%u:%d : format type %d is not supported yet\n", 813 fp->iface, fp->altsetting, 814 fmt->bFormatType); 815 return -ENOTSUPP; 816 } 817 fp->fmt_type = fmt->bFormatType; 818 if (err < 0) 819 return err; 820 #if 1 821 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */ 822 /* extigy apparently supports sample rates other than 48k 823 * but not in ordinary way. so we enable only 48k atm. 824 */ 825 if (chip->usb_id == USB_ID(0x041e, 0x3000) || 826 chip->usb_id == USB_ID(0x041e, 0x3020) || 827 chip->usb_id == USB_ID(0x041e, 0x3061)) { 828 if (fmt->bFormatType == UAC_FORMAT_TYPE_I && 829 fp->rates != SNDRV_PCM_RATE_48000 && 830 fp->rates != SNDRV_PCM_RATE_96000) 831 return -ENOTSUPP; 832 } 833 #endif 834 return 0; 835 } 836 837 int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip, 838 struct audioformat *fp, 839 struct uac3_as_header_descriptor *as, 840 int stream) 841 { 842 u64 format = le64_to_cpu(as->bmFormats); 843 int err; 844 845 /* 846 * Type I format bits are D0..D6 847 * This test works because type IV is not supported 848 */ 849 if (format & 0x7f) 850 fp->fmt_type = UAC_FORMAT_TYPE_I; 851 else 852 fp->fmt_type = UAC_FORMAT_TYPE_III; 853 854 err = parse_audio_format_i(chip, fp, format, as); 855 if (err < 0) 856 return err; 857 858 return 0; 859 } 860