1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 */ 4 5 6 #include <linux/init.h> 7 #include <linux/slab.h> 8 #include <linux/usb.h> 9 #include <linux/usb/audio.h> 10 #include <linux/usb/audio-v2.h> 11 #include <linux/usb/audio-v3.h> 12 13 #include <sound/core.h> 14 #include <sound/pcm.h> 15 #include <sound/control.h> 16 #include <sound/tlv.h> 17 18 #include "usbaudio.h" 19 #include "card.h" 20 #include "proc.h" 21 #include "quirks.h" 22 #include "endpoint.h" 23 #include "pcm.h" 24 #include "helper.h" 25 #include "format.h" 26 #include "clock.h" 27 #include "stream.h" 28 #include "power.h" 29 #include "media.h" 30 31 static void audioformat_free(struct audioformat *fp) 32 { 33 list_del(&fp->list); /* unlink for avoiding double-free */ 34 kfree(fp->rate_table); 35 kfree(fp->chmap); 36 kfree(fp); 37 } 38 39 /* 40 * free a substream 41 */ 42 static void free_substream(struct snd_usb_substream *subs) 43 { 44 struct audioformat *fp, *n; 45 46 if (!subs->num_formats) 47 return; /* not initialized */ 48 list_for_each_entry_safe(fp, n, &subs->fmt_list, list) 49 audioformat_free(fp); 50 kfree(subs->str_pd); 51 snd_media_stream_delete(subs); 52 } 53 54 55 /* 56 * free a usb stream instance 57 */ 58 static void snd_usb_audio_stream_free(struct snd_usb_stream *stream) 59 { 60 free_substream(&stream->substream[0]); 61 free_substream(&stream->substream[1]); 62 list_del(&stream->list); 63 kfree(stream); 64 } 65 66 static void snd_usb_audio_pcm_free(struct snd_pcm *pcm) 67 { 68 struct snd_usb_stream *stream = pcm->private_data; 69 if (stream) { 70 stream->pcm = NULL; 71 snd_usb_audio_stream_free(stream); 72 } 73 } 74 75 /* 76 * initialize the substream instance. 77 */ 78 79 static void snd_usb_init_substream(struct snd_usb_stream *as, 80 int stream, 81 struct audioformat *fp, 82 struct snd_usb_power_domain *pd) 83 { 84 struct snd_usb_substream *subs = &as->substream[stream]; 85 86 INIT_LIST_HEAD(&subs->fmt_list); 87 spin_lock_init(&subs->lock); 88 89 subs->stream = as; 90 subs->direction = stream; 91 subs->dev = as->chip->dev; 92 subs->txfr_quirk = !!(as->chip->quirk_flags & QUIRK_FLAG_ALIGN_TRANSFER); 93 subs->tx_length_quirk = !!(as->chip->quirk_flags & QUIRK_FLAG_TX_LENGTH); 94 subs->speed = snd_usb_get_speed(subs->dev); 95 subs->pkt_offset_adj = 0; 96 subs->stream_offset_adj = 0; 97 98 snd_usb_set_pcm_ops(as->pcm, stream); 99 100 list_add_tail(&fp->list, &subs->fmt_list); 101 subs->formats |= fp->formats; 102 subs->num_formats++; 103 subs->fmt_type = fp->fmt_type; 104 subs->ep_num = fp->endpoint; 105 if (fp->channels > subs->channels_max) 106 subs->channels_max = fp->channels; 107 108 if (pd) { 109 subs->str_pd = pd; 110 /* Initialize Power Domain to idle status D1 */ 111 snd_usb_power_domain_set(subs->stream->chip, pd, 112 UAC3_PD_STATE_D1); 113 } 114 115 snd_usb_preallocate_buffer(subs); 116 } 117 118 /* kctl callbacks for usb-audio channel maps */ 119 static int usb_chmap_ctl_info(struct snd_kcontrol *kcontrol, 120 struct snd_ctl_elem_info *uinfo) 121 { 122 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); 123 struct snd_usb_substream *subs = info->private_data; 124 125 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 126 uinfo->count = subs->channels_max; 127 uinfo->value.integer.min = 0; 128 uinfo->value.integer.max = SNDRV_CHMAP_LAST; 129 return 0; 130 } 131 132 /* check whether a duplicated entry exists in the audiofmt list */ 133 static bool have_dup_chmap(struct snd_usb_substream *subs, 134 struct audioformat *fp) 135 { 136 struct audioformat *prev = fp; 137 138 list_for_each_entry_continue_reverse(prev, &subs->fmt_list, list) { 139 if (prev->chmap && 140 !memcmp(prev->chmap, fp->chmap, sizeof(*fp->chmap))) 141 return true; 142 } 143 return false; 144 } 145 146 static int usb_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag, 147 unsigned int size, unsigned int __user *tlv) 148 { 149 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); 150 struct snd_usb_substream *subs = info->private_data; 151 struct audioformat *fp; 152 unsigned int __user *dst; 153 int count = 0; 154 155 if (size < 8) 156 return -ENOMEM; 157 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv)) 158 return -EFAULT; 159 size -= 8; 160 dst = tlv + 2; 161 list_for_each_entry(fp, &subs->fmt_list, list) { 162 int i, ch_bytes; 163 164 if (!fp->chmap) 165 continue; 166 if (have_dup_chmap(subs, fp)) 167 continue; 168 /* copy the entry */ 169 ch_bytes = fp->chmap->channels * 4; 170 if (size < 8 + ch_bytes) 171 return -ENOMEM; 172 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) || 173 put_user(ch_bytes, dst + 1)) 174 return -EFAULT; 175 dst += 2; 176 for (i = 0; i < fp->chmap->channels; i++, dst++) { 177 if (put_user(fp->chmap->map[i], dst)) 178 return -EFAULT; 179 } 180 181 count += 8 + ch_bytes; 182 size -= 8 + ch_bytes; 183 } 184 if (put_user(count, tlv + 1)) 185 return -EFAULT; 186 return 0; 187 } 188 189 static int usb_chmap_ctl_get(struct snd_kcontrol *kcontrol, 190 struct snd_ctl_elem_value *ucontrol) 191 { 192 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); 193 struct snd_usb_substream *subs = info->private_data; 194 struct snd_pcm_chmap_elem *chmap = NULL; 195 int i = 0; 196 197 if (subs->cur_audiofmt) 198 chmap = subs->cur_audiofmt->chmap; 199 if (chmap) { 200 for (i = 0; i < chmap->channels; i++) 201 ucontrol->value.integer.value[i] = chmap->map[i]; 202 } 203 for (; i < subs->channels_max; i++) 204 ucontrol->value.integer.value[i] = 0; 205 return 0; 206 } 207 208 /* create a chmap kctl assigned to the given USB substream */ 209 static int add_chmap(struct snd_pcm *pcm, int stream, 210 struct snd_usb_substream *subs) 211 { 212 struct audioformat *fp; 213 struct snd_pcm_chmap *chmap; 214 struct snd_kcontrol *kctl; 215 int err; 216 217 list_for_each_entry(fp, &subs->fmt_list, list) 218 if (fp->chmap) 219 goto ok; 220 /* no chmap is found */ 221 return 0; 222 223 ok: 224 err = snd_pcm_add_chmap_ctls(pcm, stream, NULL, 0, 0, &chmap); 225 if (err < 0) 226 return err; 227 228 /* override handlers */ 229 chmap->private_data = subs; 230 kctl = chmap->kctl; 231 kctl->info = usb_chmap_ctl_info; 232 kctl->get = usb_chmap_ctl_get; 233 kctl->tlv.c = usb_chmap_ctl_tlv; 234 235 return 0; 236 } 237 238 /* convert from USB ChannelConfig bits to ALSA chmap element */ 239 static struct snd_pcm_chmap_elem *convert_chmap(int channels, unsigned int bits, 240 int protocol) 241 { 242 static const unsigned int uac1_maps[] = { 243 SNDRV_CHMAP_FL, /* left front */ 244 SNDRV_CHMAP_FR, /* right front */ 245 SNDRV_CHMAP_FC, /* center front */ 246 SNDRV_CHMAP_LFE, /* LFE */ 247 SNDRV_CHMAP_RL, /* left surround */ 248 SNDRV_CHMAP_RR, /* right surround */ 249 SNDRV_CHMAP_FLC, /* left of center */ 250 SNDRV_CHMAP_FRC, /* right of center */ 251 SNDRV_CHMAP_RC, /* surround */ 252 SNDRV_CHMAP_SL, /* side left */ 253 SNDRV_CHMAP_SR, /* side right */ 254 SNDRV_CHMAP_TC, /* top */ 255 0 /* terminator */ 256 }; 257 static const unsigned int uac2_maps[] = { 258 SNDRV_CHMAP_FL, /* front left */ 259 SNDRV_CHMAP_FR, /* front right */ 260 SNDRV_CHMAP_FC, /* front center */ 261 SNDRV_CHMAP_LFE, /* LFE */ 262 SNDRV_CHMAP_RL, /* back left */ 263 SNDRV_CHMAP_RR, /* back right */ 264 SNDRV_CHMAP_FLC, /* front left of center */ 265 SNDRV_CHMAP_FRC, /* front right of center */ 266 SNDRV_CHMAP_RC, /* back center */ 267 SNDRV_CHMAP_SL, /* side left */ 268 SNDRV_CHMAP_SR, /* side right */ 269 SNDRV_CHMAP_TC, /* top center */ 270 SNDRV_CHMAP_TFL, /* top front left */ 271 SNDRV_CHMAP_TFC, /* top front center */ 272 SNDRV_CHMAP_TFR, /* top front right */ 273 SNDRV_CHMAP_TRL, /* top back left */ 274 SNDRV_CHMAP_TRC, /* top back center */ 275 SNDRV_CHMAP_TRR, /* top back right */ 276 SNDRV_CHMAP_TFLC, /* top front left of center */ 277 SNDRV_CHMAP_TFRC, /* top front right of center */ 278 SNDRV_CHMAP_LLFE, /* left LFE */ 279 SNDRV_CHMAP_RLFE, /* right LFE */ 280 SNDRV_CHMAP_TSL, /* top side left */ 281 SNDRV_CHMAP_TSR, /* top side right */ 282 SNDRV_CHMAP_BC, /* bottom center */ 283 SNDRV_CHMAP_RLC, /* back left of center */ 284 SNDRV_CHMAP_RRC, /* back right of center */ 285 0 /* terminator */ 286 }; 287 struct snd_pcm_chmap_elem *chmap; 288 const unsigned int *maps; 289 int c; 290 291 if (channels > ARRAY_SIZE(chmap->map)) 292 return NULL; 293 294 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL); 295 if (!chmap) 296 return NULL; 297 298 maps = protocol == UAC_VERSION_2 ? uac2_maps : uac1_maps; 299 chmap->channels = channels; 300 c = 0; 301 302 if (bits) { 303 for (; bits && *maps; maps++, bits >>= 1) { 304 if (bits & 1) 305 chmap->map[c++] = *maps; 306 if (c == chmap->channels) 307 break; 308 } 309 } else { 310 /* If we're missing wChannelConfig, then guess something 311 to make sure the channel map is not skipped entirely */ 312 if (channels == 1) 313 chmap->map[c++] = SNDRV_CHMAP_MONO; 314 else 315 for (; c < channels && *maps; maps++) 316 chmap->map[c++] = *maps; 317 } 318 319 for (; c < channels; c++) 320 chmap->map[c] = SNDRV_CHMAP_UNKNOWN; 321 322 return chmap; 323 } 324 325 /* UAC3 device stores channels information in Cluster Descriptors */ 326 static struct 327 snd_pcm_chmap_elem *convert_chmap_v3(struct uac3_cluster_header_descriptor 328 *cluster) 329 { 330 unsigned int channels = cluster->bNrChannels; 331 struct snd_pcm_chmap_elem *chmap; 332 void *p = cluster; 333 int len, c; 334 335 if (channels > ARRAY_SIZE(chmap->map)) 336 return NULL; 337 338 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL); 339 if (!chmap) 340 return NULL; 341 342 len = le16_to_cpu(cluster->wLength); 343 c = 0; 344 p += sizeof(*cluster); 345 len -= sizeof(*cluster); 346 347 while (len > 0 && (c < channels)) { 348 struct uac3_cluster_segment_descriptor *cs_desc = p; 349 u16 cs_len; 350 u8 cs_type; 351 352 if (len < sizeof(*cs_desc)) 353 break; 354 cs_len = le16_to_cpu(cs_desc->wLength); 355 if (len < cs_len) 356 break; 357 cs_type = cs_desc->bSegmentType; 358 359 if (cs_type == UAC3_CHANNEL_INFORMATION) { 360 struct uac3_cluster_information_segment_descriptor *is = p; 361 unsigned char map; 362 363 if (cs_len < sizeof(*is)) 364 break; 365 366 /* 367 * TODO: this conversion is not complete, update it 368 * after adding UAC3 values to asound.h 369 */ 370 switch (is->bChRelationship) { 371 case UAC3_CH_MONO: 372 map = SNDRV_CHMAP_MONO; 373 break; 374 case UAC3_CH_LEFT: 375 case UAC3_CH_FRONT_LEFT: 376 case UAC3_CH_HEADPHONE_LEFT: 377 map = SNDRV_CHMAP_FL; 378 break; 379 case UAC3_CH_RIGHT: 380 case UAC3_CH_FRONT_RIGHT: 381 case UAC3_CH_HEADPHONE_RIGHT: 382 map = SNDRV_CHMAP_FR; 383 break; 384 case UAC3_CH_FRONT_CENTER: 385 map = SNDRV_CHMAP_FC; 386 break; 387 case UAC3_CH_FRONT_LEFT_OF_CENTER: 388 map = SNDRV_CHMAP_FLC; 389 break; 390 case UAC3_CH_FRONT_RIGHT_OF_CENTER: 391 map = SNDRV_CHMAP_FRC; 392 break; 393 case UAC3_CH_SIDE_LEFT: 394 map = SNDRV_CHMAP_SL; 395 break; 396 case UAC3_CH_SIDE_RIGHT: 397 map = SNDRV_CHMAP_SR; 398 break; 399 case UAC3_CH_BACK_LEFT: 400 map = SNDRV_CHMAP_RL; 401 break; 402 case UAC3_CH_BACK_RIGHT: 403 map = SNDRV_CHMAP_RR; 404 break; 405 case UAC3_CH_BACK_CENTER: 406 map = SNDRV_CHMAP_RC; 407 break; 408 case UAC3_CH_BACK_LEFT_OF_CENTER: 409 map = SNDRV_CHMAP_RLC; 410 break; 411 case UAC3_CH_BACK_RIGHT_OF_CENTER: 412 map = SNDRV_CHMAP_RRC; 413 break; 414 case UAC3_CH_TOP_CENTER: 415 map = SNDRV_CHMAP_TC; 416 break; 417 case UAC3_CH_TOP_FRONT_LEFT: 418 map = SNDRV_CHMAP_TFL; 419 break; 420 case UAC3_CH_TOP_FRONT_RIGHT: 421 map = SNDRV_CHMAP_TFR; 422 break; 423 case UAC3_CH_TOP_FRONT_CENTER: 424 map = SNDRV_CHMAP_TFC; 425 break; 426 case UAC3_CH_TOP_FRONT_LOC: 427 map = SNDRV_CHMAP_TFLC; 428 break; 429 case UAC3_CH_TOP_FRONT_ROC: 430 map = SNDRV_CHMAP_TFRC; 431 break; 432 case UAC3_CH_TOP_SIDE_LEFT: 433 map = SNDRV_CHMAP_TSL; 434 break; 435 case UAC3_CH_TOP_SIDE_RIGHT: 436 map = SNDRV_CHMAP_TSR; 437 break; 438 case UAC3_CH_TOP_BACK_LEFT: 439 map = SNDRV_CHMAP_TRL; 440 break; 441 case UAC3_CH_TOP_BACK_RIGHT: 442 map = SNDRV_CHMAP_TRR; 443 break; 444 case UAC3_CH_TOP_BACK_CENTER: 445 map = SNDRV_CHMAP_TRC; 446 break; 447 case UAC3_CH_BOTTOM_CENTER: 448 map = SNDRV_CHMAP_BC; 449 break; 450 case UAC3_CH_LOW_FREQUENCY_EFFECTS: 451 map = SNDRV_CHMAP_LFE; 452 break; 453 case UAC3_CH_LFE_LEFT: 454 map = SNDRV_CHMAP_LLFE; 455 break; 456 case UAC3_CH_LFE_RIGHT: 457 map = SNDRV_CHMAP_RLFE; 458 break; 459 case UAC3_CH_RELATIONSHIP_UNDEFINED: 460 default: 461 map = SNDRV_CHMAP_UNKNOWN; 462 break; 463 } 464 chmap->map[c++] = map; 465 } 466 p += cs_len; 467 len -= cs_len; 468 } 469 470 if (channels < c) 471 pr_err("%s: channel number mismatch\n", __func__); 472 473 chmap->channels = channels; 474 475 for (; c < channels; c++) 476 chmap->map[c] = SNDRV_CHMAP_UNKNOWN; 477 478 return chmap; 479 } 480 481 /* 482 * add this endpoint to the chip instance. 483 * if a stream with the same endpoint already exists, append to it. 484 * if not, create a new pcm stream. note, fp is added to the substream 485 * fmt_list and will be freed on the chip instance release. do not free 486 * fp or do remove it from the substream fmt_list to avoid double-free. 487 */ 488 static int __snd_usb_add_audio_stream(struct snd_usb_audio *chip, 489 int stream, 490 struct audioformat *fp, 491 struct snd_usb_power_domain *pd) 492 493 { 494 struct snd_usb_stream *as; 495 struct snd_usb_substream *subs; 496 struct snd_pcm *pcm; 497 int err; 498 499 list_for_each_entry(as, &chip->pcm_list, list) { 500 if (as->fmt_type != fp->fmt_type) 501 continue; 502 subs = &as->substream[stream]; 503 if (subs->ep_num == fp->endpoint) { 504 list_add_tail(&fp->list, &subs->fmt_list); 505 subs->num_formats++; 506 subs->formats |= fp->formats; 507 return 0; 508 } 509 } 510 511 if (chip->card->registered) 512 chip->need_delayed_register = true; 513 514 /* look for an empty stream */ 515 list_for_each_entry(as, &chip->pcm_list, list) { 516 if (as->fmt_type != fp->fmt_type) 517 continue; 518 subs = &as->substream[stream]; 519 if (subs->ep_num) 520 continue; 521 err = snd_pcm_new_stream(as->pcm, stream, 1); 522 if (err < 0) 523 return err; 524 snd_usb_init_substream(as, stream, fp, pd); 525 return add_chmap(as->pcm, stream, subs); 526 } 527 528 /* create a new pcm */ 529 as = kzalloc(sizeof(*as), GFP_KERNEL); 530 if (!as) 531 return -ENOMEM; 532 as->pcm_index = chip->pcm_devs; 533 as->chip = chip; 534 as->fmt_type = fp->fmt_type; 535 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs, 536 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0, 537 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1, 538 &pcm); 539 if (err < 0) { 540 kfree(as); 541 return err; 542 } 543 as->pcm = pcm; 544 pcm->private_data = as; 545 pcm->private_free = snd_usb_audio_pcm_free; 546 pcm->info_flags = 0; 547 if (chip->pcm_devs > 0) 548 scnprintf(pcm->name, sizeof(pcm->name), "USB Audio #%d", 549 chip->pcm_devs); 550 else 551 strscpy(pcm->name, "USB Audio"); 552 553 snd_usb_init_substream(as, stream, fp, pd); 554 555 /* 556 * Keep using head insertion for M-Audio Audiophile USB (tm) which has a 557 * fix to swap capture stream order in conf/cards/USB-audio.conf 558 */ 559 if (chip->usb_id == USB_ID(0x0763, 0x2003)) 560 list_add(&as->list, &chip->pcm_list); 561 else 562 list_add_tail(&as->list, &chip->pcm_list); 563 564 chip->pcm_devs++; 565 566 snd_usb_proc_pcm_format_add(as); 567 568 return add_chmap(pcm, stream, &as->substream[stream]); 569 } 570 571 int snd_usb_add_audio_stream(struct snd_usb_audio *chip, 572 int stream, 573 struct audioformat *fp) 574 { 575 return __snd_usb_add_audio_stream(chip, stream, fp, NULL); 576 } 577 578 static int snd_usb_add_audio_stream_v3(struct snd_usb_audio *chip, 579 int stream, 580 struct audioformat *fp, 581 struct snd_usb_power_domain *pd) 582 { 583 return __snd_usb_add_audio_stream(chip, stream, fp, pd); 584 } 585 586 static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip, 587 struct usb_host_interface *alts, 588 int protocol, int iface_no) 589 { 590 /* parsed with a v1 header here. that's ok as we only look at the 591 * header first which is the same for both versions */ 592 struct uac_iso_endpoint_descriptor *csep; 593 struct usb_interface_descriptor *altsd = get_iface_desc(alts); 594 int attributes = 0; 595 596 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT); 597 598 /* Creamware Noah has this descriptor after the 2nd endpoint */ 599 if (!csep && altsd->bNumEndpoints >= 2) 600 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT); 601 602 /* 603 * If we can't locate the USB_DT_CS_ENDPOINT descriptor in the extra 604 * bytes after the first endpoint, go search the entire interface. 605 * Some devices have it directly *before* the standard endpoint. 606 */ 607 if (!csep) 608 csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT); 609 610 if (!csep || csep->bLength < 7 || 611 csep->bDescriptorSubtype != UAC_EP_GENERAL) 612 goto error; 613 614 if (protocol == UAC_VERSION_1) { 615 attributes = csep->bmAttributes; 616 } else if (protocol == UAC_VERSION_2) { 617 struct uac2_iso_endpoint_descriptor *csep2 = 618 (struct uac2_iso_endpoint_descriptor *) csep; 619 620 if (csep2->bLength < sizeof(*csep2)) 621 goto error; 622 attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX; 623 624 /* emulate the endpoint attributes of a v1 device */ 625 if (csep2->bmControls & UAC2_CONTROL_PITCH) 626 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL; 627 } else { /* UAC_VERSION_3 */ 628 struct uac3_iso_endpoint_descriptor *csep3 = 629 (struct uac3_iso_endpoint_descriptor *) csep; 630 631 if (csep3->bLength < sizeof(*csep3)) 632 goto error; 633 /* emulate the endpoint attributes of a v1 device */ 634 if (le32_to_cpu(csep3->bmControls) & UAC2_CONTROL_PITCH) 635 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL; 636 } 637 638 return attributes; 639 640 error: 641 usb_audio_warn(chip, 642 "%u:%d : no or invalid class specific endpoint descriptor\n", 643 iface_no, altsd->bAlternateSetting); 644 return 0; 645 } 646 647 /* find an input terminal descriptor (either UAC1 or UAC2) with the given 648 * terminal id 649 */ 650 static void * 651 snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface, 652 int terminal_id, int protocol) 653 { 654 struct uac2_input_terminal_descriptor *term = NULL; 655 656 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra, 657 ctrl_iface->extralen, 658 term, UAC_INPUT_TERMINAL))) { 659 if (!snd_usb_validate_audio_desc(term, protocol)) 660 continue; 661 if (term->bTerminalID == terminal_id) 662 return term; 663 } 664 665 return NULL; 666 } 667 668 static void * 669 snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface, 670 int terminal_id, int protocol) 671 { 672 /* OK to use with both UAC2 and UAC3 */ 673 struct uac2_output_terminal_descriptor *term = NULL; 674 675 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra, 676 ctrl_iface->extralen, 677 term, UAC_OUTPUT_TERMINAL))) { 678 if (!snd_usb_validate_audio_desc(term, protocol)) 679 continue; 680 if (term->bTerminalID == terminal_id) 681 return term; 682 } 683 684 return NULL; 685 } 686 687 static struct audioformat * 688 audio_format_alloc_init(struct snd_usb_audio *chip, 689 struct usb_host_interface *alts, 690 int protocol, int iface_no, int altset_idx, 691 int altno, int num_channels, int clock) 692 { 693 struct usb_host_endpoint *ep = &alts->endpoint[0]; 694 struct audioformat *fp; 695 696 fp = kzalloc(sizeof(*fp), GFP_KERNEL); 697 if (!fp) 698 return NULL; 699 700 fp->iface = iface_no; 701 fp->altsetting = altno; 702 fp->altset_idx = altset_idx; 703 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; 704 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; 705 fp->datainterval = snd_usb_parse_datainterval(chip, alts); 706 fp->protocol = protocol; 707 fp->maxpacksize = usb_endpoint_max_periodic_payload(chip->dev, ep); 708 fp->channels = num_channels; 709 fp->clock = clock; 710 INIT_LIST_HEAD(&fp->list); 711 712 return fp; 713 } 714 715 static struct audioformat * 716 snd_usb_get_audioformat_uac12(struct snd_usb_audio *chip, 717 struct usb_host_interface *alts, 718 int protocol, int iface_no, int altset_idx, 719 int altno, int stream, int bm_quirk) 720 { 721 struct usb_device *dev = chip->dev; 722 struct uac_format_type_i_continuous_descriptor *fmt; 723 unsigned int num_channels = 0, chconfig = 0; 724 struct usb_host_interface *ctrl_intf; 725 struct audioformat *fp; 726 int clock = 0; 727 u64 format; 728 729 ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no); 730 731 /* get audio formats */ 732 if (protocol == UAC_VERSION_1) { 733 struct uac1_as_header_descriptor *as = 734 snd_usb_find_csint_desc(alts->extra, alts->extralen, 735 NULL, UAC_AS_GENERAL); 736 struct uac_input_terminal_descriptor *iterm; 737 738 if (!as) { 739 dev_err(&dev->dev, 740 "%u:%d : UAC_AS_GENERAL descriptor not found\n", 741 iface_no, altno); 742 return NULL; 743 } 744 745 if (as->bLength < sizeof(*as)) { 746 dev_err(&dev->dev, 747 "%u:%d : invalid UAC_AS_GENERAL desc\n", 748 iface_no, altno); 749 return NULL; 750 } 751 752 format = le16_to_cpu(as->wFormatTag); /* remember the format value */ 753 754 iterm = snd_usb_find_input_terminal_descriptor(ctrl_intf, 755 as->bTerminalLink, 756 protocol); 757 if (iterm) { 758 num_channels = iterm->bNrChannels; 759 chconfig = le16_to_cpu(iterm->wChannelConfig); 760 } 761 } else { /* UAC_VERSION_2 */ 762 struct uac2_input_terminal_descriptor *input_term; 763 struct uac2_output_terminal_descriptor *output_term; 764 struct uac2_as_header_descriptor *as = 765 snd_usb_find_csint_desc(alts->extra, alts->extralen, 766 NULL, UAC_AS_GENERAL); 767 768 if (!as) { 769 dev_err(&dev->dev, 770 "%u:%d : UAC_AS_GENERAL descriptor not found\n", 771 iface_no, altno); 772 return NULL; 773 } 774 775 if (as->bLength < sizeof(*as)) { 776 dev_err(&dev->dev, 777 "%u:%d : invalid UAC_AS_GENERAL desc\n", 778 iface_no, altno); 779 return NULL; 780 } 781 782 num_channels = as->bNrChannels; 783 format = le32_to_cpu(as->bmFormats); 784 chconfig = le32_to_cpu(as->bmChannelConfig); 785 786 /* 787 * lookup the terminal associated to this interface 788 * to extract the clock 789 */ 790 input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf, 791 as->bTerminalLink, 792 protocol); 793 if (input_term) { 794 clock = input_term->bCSourceID; 795 if (!chconfig && (num_channels == input_term->bNrChannels)) 796 chconfig = le32_to_cpu(input_term->bmChannelConfig); 797 goto found_clock; 798 } 799 800 output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf, 801 as->bTerminalLink, 802 protocol); 803 if (output_term) { 804 clock = output_term->bCSourceID; 805 goto found_clock; 806 } 807 808 dev_err(&dev->dev, 809 "%u:%d : bogus bTerminalLink %d\n", 810 iface_no, altno, as->bTerminalLink); 811 return NULL; 812 } 813 814 found_clock: 815 /* get format type */ 816 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, 817 NULL, UAC_FORMAT_TYPE); 818 if (!fmt) { 819 dev_err(&dev->dev, 820 "%u:%d : no UAC_FORMAT_TYPE desc\n", 821 iface_no, altno); 822 return NULL; 823 } 824 if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8)) 825 || ((protocol == UAC_VERSION_2) && 826 (fmt->bLength < 6))) { 827 dev_err(&dev->dev, 828 "%u:%d : invalid UAC_FORMAT_TYPE desc\n", 829 iface_no, altno); 830 return NULL; 831 } 832 833 /* 834 * Blue Microphones workaround: The last altsetting is 835 * identical with the previous one, except for a larger 836 * packet size, but is actually a mislabeled two-channel 837 * setting; ignore it. 838 * 839 * Part 2: analyze quirk flag and format 840 */ 841 if (bm_quirk && fmt->bNrChannels == 1 && fmt->bSubframeSize == 2) 842 return NULL; 843 844 fp = audio_format_alloc_init(chip, alts, protocol, iface_no, 845 altset_idx, altno, num_channels, clock); 846 if (!fp) 847 return ERR_PTR(-ENOMEM); 848 849 fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, 850 iface_no); 851 852 /* some quirks for attributes here */ 853 snd_usb_audioformat_attributes_quirk(chip, fp, stream); 854 855 /* ok, let's parse further... */ 856 if (snd_usb_parse_audio_format(chip, fp, format, 857 fmt, stream) < 0) { 858 audioformat_free(fp); 859 return NULL; 860 } 861 862 /* Create chmap */ 863 if (fp->channels != num_channels) 864 chconfig = 0; 865 866 fp->chmap = convert_chmap(fp->channels, chconfig, protocol); 867 868 return fp; 869 } 870 871 static struct audioformat * 872 snd_usb_get_audioformat_uac3(struct snd_usb_audio *chip, 873 struct usb_host_interface *alts, 874 struct snd_usb_power_domain **pd_out, 875 int iface_no, int altset_idx, 876 int altno, int stream) 877 { 878 struct usb_device *dev = chip->dev; 879 struct uac3_input_terminal_descriptor *input_term; 880 struct uac3_output_terminal_descriptor *output_term; 881 struct uac3_cluster_header_descriptor *cluster; 882 struct uac3_as_header_descriptor *as = NULL; 883 struct uac3_hc_descriptor_header hc_header; 884 struct usb_host_interface *ctrl_intf; 885 struct snd_pcm_chmap_elem *chmap; 886 struct snd_usb_power_domain *pd; 887 unsigned char badd_profile; 888 u64 badd_formats = 0; 889 unsigned int num_channels; 890 struct audioformat *fp; 891 u16 cluster_id, wLength, cluster_wLength; 892 int clock = 0; 893 int err; 894 895 badd_profile = chip->badd_profile; 896 ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no); 897 898 if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) { 899 unsigned int maxpacksize = 900 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); 901 902 switch (maxpacksize) { 903 default: 904 dev_err(&dev->dev, 905 "%u:%d : incorrect wMaxPacketSize for BADD profile\n", 906 iface_no, altno); 907 return NULL; 908 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16: 909 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16: 910 badd_formats = SNDRV_PCM_FMTBIT_S16_LE; 911 num_channels = 1; 912 break; 913 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24: 914 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24: 915 badd_formats = SNDRV_PCM_FMTBIT_S24_3LE; 916 num_channels = 1; 917 break; 918 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16: 919 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16: 920 badd_formats = SNDRV_PCM_FMTBIT_S16_LE; 921 num_channels = 2; 922 break; 923 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24: 924 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24: 925 badd_formats = SNDRV_PCM_FMTBIT_S24_3LE; 926 num_channels = 2; 927 break; 928 } 929 930 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL); 931 if (!chmap) 932 return ERR_PTR(-ENOMEM); 933 934 if (num_channels == 1) { 935 chmap->map[0] = SNDRV_CHMAP_MONO; 936 } else { 937 chmap->map[0] = SNDRV_CHMAP_FL; 938 chmap->map[1] = SNDRV_CHMAP_FR; 939 } 940 941 chmap->channels = num_channels; 942 clock = UAC3_BADD_CS_ID9; 943 goto found_clock; 944 } 945 946 as = snd_usb_find_csint_desc(alts->extra, alts->extralen, 947 NULL, UAC_AS_GENERAL); 948 if (!as) { 949 dev_err(&dev->dev, 950 "%u:%d : UAC_AS_GENERAL descriptor not found\n", 951 iface_no, altno); 952 return NULL; 953 } 954 955 if (as->bLength < sizeof(*as)) { 956 dev_err(&dev->dev, 957 "%u:%d : invalid UAC_AS_GENERAL desc\n", 958 iface_no, altno); 959 return NULL; 960 } 961 962 cluster_id = le16_to_cpu(as->wClusterDescrID); 963 if (!cluster_id) { 964 dev_err(&dev->dev, 965 "%u:%d : no cluster descriptor\n", 966 iface_no, altno); 967 return NULL; 968 } 969 970 /* 971 * Get number of channels and channel map through 972 * High Capability Cluster Descriptor 973 * 974 * First step: get High Capability header and 975 * read size of Cluster Descriptor 976 */ 977 err = snd_usb_ctl_msg(chip->dev, 978 usb_rcvctrlpipe(chip->dev, 0), 979 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR, 980 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 981 cluster_id, 982 snd_usb_ctrl_intf(ctrl_intf), 983 &hc_header, sizeof(hc_header)); 984 if (err < 0) 985 return ERR_PTR(err); 986 else if (err != sizeof(hc_header)) { 987 dev_err(&dev->dev, 988 "%u:%d : can't get High Capability descriptor\n", 989 iface_no, altno); 990 return ERR_PTR(-EIO); 991 } 992 993 /* 994 * Second step: allocate needed amount of memory 995 * and request Cluster Descriptor 996 */ 997 wLength = le16_to_cpu(hc_header.wLength); 998 if (wLength < sizeof(cluster)) 999 return NULL; 1000 cluster = kzalloc(wLength, GFP_KERNEL); 1001 if (!cluster) 1002 return ERR_PTR(-ENOMEM); 1003 err = snd_usb_ctl_msg(chip->dev, 1004 usb_rcvctrlpipe(chip->dev, 0), 1005 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR, 1006 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 1007 cluster_id, 1008 snd_usb_ctrl_intf(ctrl_intf), 1009 cluster, wLength); 1010 if (err < 0) { 1011 kfree(cluster); 1012 return ERR_PTR(err); 1013 } else if (err != wLength) { 1014 dev_err(&dev->dev, 1015 "%u:%d : can't get Cluster Descriptor\n", 1016 iface_no, altno); 1017 kfree(cluster); 1018 return ERR_PTR(-EIO); 1019 } 1020 1021 cluster_wLength = le16_to_cpu(cluster->wLength); 1022 if (cluster_wLength < sizeof(*cluster) || 1023 cluster_wLength > wLength) { 1024 dev_err(&dev->dev, 1025 "%u:%d : invalid Cluster Descriptor size\n", 1026 iface_no, altno); 1027 kfree(cluster); 1028 return ERR_PTR(-EIO); 1029 } 1030 1031 num_channels = cluster->bNrChannels; 1032 chmap = convert_chmap_v3(cluster); 1033 kfree(cluster); 1034 1035 /* 1036 * lookup the terminal associated to this interface 1037 * to extract the clock 1038 */ 1039 input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf, 1040 as->bTerminalLink, 1041 UAC_VERSION_3); 1042 if (input_term) { 1043 clock = input_term->bCSourceID; 1044 goto found_clock; 1045 } 1046 1047 output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf, 1048 as->bTerminalLink, 1049 UAC_VERSION_3); 1050 if (output_term) { 1051 clock = output_term->bCSourceID; 1052 goto found_clock; 1053 } 1054 1055 dev_err(&dev->dev, "%u:%d : bogus bTerminalLink %d\n", 1056 iface_no, altno, as->bTerminalLink); 1057 kfree(chmap); 1058 return NULL; 1059 1060 found_clock: 1061 fp = audio_format_alloc_init(chip, alts, UAC_VERSION_3, iface_no, 1062 altset_idx, altno, num_channels, clock); 1063 if (!fp) { 1064 kfree(chmap); 1065 return ERR_PTR(-ENOMEM); 1066 } 1067 1068 fp->chmap = chmap; 1069 1070 if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) { 1071 fp->attributes = 0; /* No attributes */ 1072 1073 fp->fmt_type = UAC_FORMAT_TYPE_I; 1074 fp->formats = badd_formats; 1075 1076 fp->nr_rates = 0; /* SNDRV_PCM_RATE_CONTINUOUS */ 1077 fp->rate_min = UAC3_BADD_SAMPLING_RATE; 1078 fp->rate_max = UAC3_BADD_SAMPLING_RATE; 1079 fp->rates = SNDRV_PCM_RATE_CONTINUOUS; 1080 1081 pd = kzalloc(sizeof(*pd), GFP_KERNEL); 1082 if (!pd) { 1083 audioformat_free(fp); 1084 return NULL; 1085 } 1086 pd->pd_id = (stream == SNDRV_PCM_STREAM_PLAYBACK) ? 1087 UAC3_BADD_PD_ID10 : UAC3_BADD_PD_ID11; 1088 pd->pd_d1d0_rec = UAC3_BADD_PD_RECOVER_D1D0; 1089 pd->pd_d2d0_rec = UAC3_BADD_PD_RECOVER_D2D0; 1090 pd->ctrl_iface = ctrl_intf; 1091 1092 } else { 1093 fp->attributes = parse_uac_endpoint_attributes(chip, alts, 1094 UAC_VERSION_3, 1095 iface_no); 1096 1097 pd = snd_usb_find_power_domain(ctrl_intf, 1098 as->bTerminalLink); 1099 1100 /* ok, let's parse further... */ 1101 if (snd_usb_parse_audio_format_v3(chip, fp, as, stream) < 0) { 1102 kfree(pd); 1103 audioformat_free(fp); 1104 return NULL; 1105 } 1106 } 1107 1108 if (pd) 1109 *pd_out = pd; 1110 1111 return fp; 1112 } 1113 1114 static int __snd_usb_parse_audio_interface(struct snd_usb_audio *chip, 1115 int iface_no, 1116 bool *has_non_pcm, bool non_pcm) 1117 { 1118 struct usb_device *dev; 1119 struct usb_interface *iface; 1120 struct usb_host_interface *alts; 1121 struct usb_interface_descriptor *altsd; 1122 int i, altno, err, stream; 1123 struct audioformat *fp = NULL; 1124 struct snd_usb_power_domain *pd = NULL; 1125 bool set_iface_first; 1126 int num, protocol; 1127 1128 dev = chip->dev; 1129 1130 /* parse the interface's altsettings */ 1131 iface = usb_ifnum_to_if(dev, iface_no); 1132 1133 num = iface->num_altsetting; 1134 1135 /* 1136 * Dallas DS4201 workaround: It presents 5 altsettings, but the last 1137 * one misses syncpipe, and does not produce any sound. 1138 */ 1139 if (chip->usb_id == USB_ID(0x04fa, 0x4201) && num >= 4) 1140 num = 4; 1141 1142 for (i = 0; i < num; i++) { 1143 alts = &iface->altsetting[i]; 1144 altsd = get_iface_desc(alts); 1145 protocol = altsd->bInterfaceProtocol; 1146 /* skip invalid one */ 1147 if (((altsd->bInterfaceClass != USB_CLASS_AUDIO || 1148 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING && 1149 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC)) && 1150 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || 1151 altsd->bNumEndpoints < 1 || 1152 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0) 1153 continue; 1154 /* must be isochronous */ 1155 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 1156 USB_ENDPOINT_XFER_ISOC) 1157 continue; 1158 /* check direction */ 1159 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ? 1160 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; 1161 altno = altsd->bAlternateSetting; 1162 1163 if (snd_usb_apply_interface_quirk(chip, iface_no, altno)) 1164 continue; 1165 1166 /* 1167 * Roland audio streaming interfaces are marked with protocols 1168 * 0/1/2, but are UAC 1 compatible. 1169 */ 1170 if (USB_ID_VENDOR(chip->usb_id) == 0x0582 && 1171 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC && 1172 protocol <= 2) 1173 protocol = UAC_VERSION_1; 1174 1175 switch (protocol) { 1176 default: 1177 dev_dbg(&dev->dev, "%u:%d: unknown interface protocol %#02x, assuming v1\n", 1178 iface_no, altno, protocol); 1179 protocol = UAC_VERSION_1; 1180 fallthrough; 1181 case UAC_VERSION_1: 1182 case UAC_VERSION_2: { 1183 int bm_quirk = 0; 1184 1185 /* 1186 * Blue Microphones workaround: The last altsetting is 1187 * identical with the previous one, except for a larger 1188 * packet size, but is actually a mislabeled two-channel 1189 * setting; ignore it. 1190 * 1191 * Part 1: prepare quirk flag 1192 */ 1193 if (altno == 2 && num == 3 && 1194 fp && fp->altsetting == 1 && fp->channels == 1 && 1195 fp->formats == SNDRV_PCM_FMTBIT_S16_LE && 1196 protocol == UAC_VERSION_1 && 1197 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 1198 fp->maxpacksize * 2) 1199 bm_quirk = 1; 1200 1201 fp = snd_usb_get_audioformat_uac12(chip, alts, protocol, 1202 iface_no, i, altno, 1203 stream, bm_quirk); 1204 break; 1205 } 1206 case UAC_VERSION_3: 1207 fp = snd_usb_get_audioformat_uac3(chip, alts, &pd, 1208 iface_no, i, altno, stream); 1209 break; 1210 } 1211 1212 if (!fp) 1213 continue; 1214 else if (IS_ERR(fp)) 1215 return PTR_ERR(fp); 1216 1217 if (fp->fmt_type != UAC_FORMAT_TYPE_I) 1218 *has_non_pcm = true; 1219 if ((fp->fmt_type == UAC_FORMAT_TYPE_I) == non_pcm) { 1220 audioformat_free(fp); 1221 kfree(pd); 1222 fp = NULL; 1223 pd = NULL; 1224 continue; 1225 } 1226 1227 snd_usb_audioformat_set_sync_ep(chip, fp); 1228 1229 dev_dbg(&dev->dev, "%u:%d: add audio endpoint %#x\n", iface_no, altno, fp->endpoint); 1230 if (protocol == UAC_VERSION_3) 1231 err = snd_usb_add_audio_stream_v3(chip, stream, fp, pd); 1232 else 1233 err = snd_usb_add_audio_stream(chip, stream, fp); 1234 1235 if (err < 0) { 1236 audioformat_free(fp); 1237 kfree(pd); 1238 return err; 1239 } 1240 1241 /* add endpoints */ 1242 err = snd_usb_add_endpoint(chip, fp->endpoint, 1243 SND_USB_ENDPOINT_TYPE_DATA); 1244 if (err < 0) 1245 return err; 1246 1247 if (fp->sync_ep) { 1248 err = snd_usb_add_endpoint(chip, fp->sync_ep, 1249 fp->implicit_fb ? 1250 SND_USB_ENDPOINT_TYPE_DATA : 1251 SND_USB_ENDPOINT_TYPE_SYNC); 1252 if (err < 0) 1253 return err; 1254 } 1255 1256 set_iface_first = false; 1257 if (protocol == UAC_VERSION_1 || 1258 (chip->quirk_flags & QUIRK_FLAG_SET_IFACE_FIRST)) 1259 set_iface_first = true; 1260 1261 /* try to set the interface... */ 1262 usb_set_interface(chip->dev, iface_no, 0); 1263 if (set_iface_first) 1264 usb_set_interface(chip->dev, iface_no, altno); 1265 snd_usb_init_pitch(chip, fp); 1266 snd_usb_init_sample_rate(chip, fp, fp->rate_max); 1267 if (!set_iface_first) 1268 usb_set_interface(chip->dev, iface_no, altno); 1269 } 1270 return 0; 1271 } 1272 1273 int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no) 1274 { 1275 int err; 1276 bool has_non_pcm = false; 1277 1278 /* parse PCM formats */ 1279 err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, false); 1280 if (err < 0) 1281 return err; 1282 1283 if (has_non_pcm) { 1284 /* parse non-PCM formats */ 1285 err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, true); 1286 if (err < 0) 1287 return err; 1288 } 1289 1290 return 0; 1291 } 1292 1293