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