1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * (Tentative) USB Audio Driver for ALSA 4 * 5 * Mixer control part 6 * 7 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de> 8 * 9 * Many codes borrowed from audio.c by 10 * Alan Cox (alan@lxorguk.ukuu.org.uk) 11 * Thomas Sailer (sailer@ife.ee.ethz.ch) 12 */ 13 14 /* 15 * TODOs, for both the mixer and the streaming interfaces: 16 * 17 * - support for UAC2 effect units 18 * - support for graphical equalizers 19 * - RANGE and MEM set commands (UAC2) 20 * - RANGE and MEM interrupt dispatchers (UAC2) 21 * - audio channel clustering (UAC2) 22 * - audio sample rate converter units (UAC2) 23 * - proper handling of clock multipliers (UAC2) 24 * - dispatch clock change notifications (UAC2) 25 * - stop PCM streams which use a clock that became invalid 26 * - stop PCM streams which use a clock selector that has changed 27 * - parse available sample rates again when clock sources changed 28 */ 29 30 #include <linux/bitops.h> 31 #include <linux/init.h> 32 #include <linux/list.h> 33 #include <linux/log2.h> 34 #include <linux/slab.h> 35 #include <linux/string.h> 36 #include <linux/usb.h> 37 #include <linux/usb/audio.h> 38 #include <linux/usb/audio-v2.h> 39 #include <linux/usb/audio-v3.h> 40 41 #include <sound/core.h> 42 #include <sound/control.h> 43 #include <sound/hwdep.h> 44 #include <sound/info.h> 45 #include <sound/tlv.h> 46 47 #include "usbaudio.h" 48 #include "mixer.h" 49 #include "helper.h" 50 #include "mixer_quirks.h" 51 #include "power.h" 52 53 #define MAX_ID_ELEMS 256 54 55 struct usb_audio_term { 56 int id; 57 int type; 58 int channels; 59 unsigned int chconfig; 60 int name; 61 }; 62 63 struct usbmix_name_map; 64 65 struct mixer_build { 66 struct snd_usb_audio *chip; 67 struct usb_mixer_interface *mixer; 68 unsigned char *buffer; 69 unsigned int buflen; 70 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS); 71 DECLARE_BITMAP(termbitmap, MAX_ID_ELEMS); 72 struct usb_audio_term oterm; 73 const struct usbmix_name_map *map; 74 const struct usbmix_selector_map *selector_map; 75 }; 76 77 /*E-mu 0202/0404/0204 eXtension Unit(XU) control*/ 78 enum { 79 USB_XU_CLOCK_RATE = 0xe301, 80 USB_XU_CLOCK_SOURCE = 0xe302, 81 USB_XU_DIGITAL_IO_STATUS = 0xe303, 82 USB_XU_DEVICE_OPTIONS = 0xe304, 83 USB_XU_DIRECT_MONITORING = 0xe305, 84 USB_XU_METERING = 0xe306 85 }; 86 enum { 87 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/ 88 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */ 89 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */ 90 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */ 91 }; 92 93 /* 94 * manual mapping of mixer names 95 * if the mixer topology is too complicated and the parsed names are 96 * ambiguous, add the entries in usbmixer_maps.c. 97 */ 98 #include "mixer_maps.c" 99 100 static const struct usbmix_name_map * 101 find_map(const struct usbmix_name_map *p, int unitid, int control) 102 { 103 if (!p) 104 return NULL; 105 106 for (; p->id; p++) { 107 if (p->id == unitid && 108 (!control || !p->control || control == p->control)) 109 return p; 110 } 111 return NULL; 112 } 113 114 /* get the mapped name if the unit matches */ 115 static int 116 check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen) 117 { 118 int len; 119 120 if (!p || !p->name) 121 return 0; 122 123 buflen--; 124 len = strscpy(buf, p->name, buflen); 125 return len < 0 ? buflen : len; 126 } 127 128 /* ignore the error value if ignore_ctl_error flag is set */ 129 #define filter_error(cval, err) \ 130 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err)) 131 132 /* check whether the control should be ignored */ 133 static inline int 134 check_ignored_ctl(const struct usbmix_name_map *p) 135 { 136 if (!p || p->name || p->dB) 137 return 0; 138 return 1; 139 } 140 141 /* dB mapping */ 142 static inline void check_mapped_dB(const struct usbmix_name_map *p, 143 struct usb_mixer_elem_info *cval) 144 { 145 if (p && p->dB) { 146 cval->dBmin = p->dB->min; 147 cval->dBmax = p->dB->max; 148 cval->min_mute = p->dB->min_mute; 149 cval->initialized = 1; 150 } 151 } 152 153 /* get the mapped selector source name */ 154 static int check_mapped_selector_name(struct mixer_build *state, int unitid, 155 int index, char *buf, int buflen) 156 { 157 const struct usbmix_selector_map *p; 158 int len; 159 160 if (!state->selector_map) 161 return 0; 162 for (p = state->selector_map; p->id; p++) { 163 if (p->id == unitid && index < p->count) { 164 len = strscpy(buf, p->names[index], buflen); 165 return len < 0 ? buflen : len; 166 } 167 } 168 return 0; 169 } 170 171 /* 172 * find an audio control unit with the given unit id 173 */ 174 static void *find_audio_control_unit(struct mixer_build *state, 175 unsigned char unit) 176 { 177 /* we just parse the header */ 178 struct uac_feature_unit_descriptor *hdr = NULL; 179 180 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr, 181 USB_DT_CS_INTERFACE)) != NULL) { 182 if (hdr->bLength >= 4 && 183 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL && 184 hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER && 185 hdr->bUnitID == unit) 186 return hdr; 187 } 188 189 return NULL; 190 } 191 192 /* 193 * copy a string with the given id 194 */ 195 static int snd_usb_copy_string_desc(struct snd_usb_audio *chip, 196 int index, char *buf, int maxlen) 197 { 198 int len = usb_string(chip->dev, index, buf, maxlen - 1); 199 200 if (len < 0) 201 return 0; 202 203 buf[len] = 0; 204 return len; 205 } 206 207 /* 208 * convert from the byte/word on usb descriptor to the zero-based integer 209 */ 210 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val) 211 { 212 switch (cval->val_type) { 213 case USB_MIXER_BOOLEAN: 214 return !!val; 215 case USB_MIXER_INV_BOOLEAN: 216 return !val; 217 case USB_MIXER_U8: 218 val &= 0xff; 219 break; 220 case USB_MIXER_S8: 221 val &= 0xff; 222 if (val >= 0x80) 223 val -= 0x100; 224 break; 225 case USB_MIXER_U16: 226 val &= 0xffff; 227 break; 228 case USB_MIXER_S16: 229 val &= 0xffff; 230 if (val >= 0x8000) 231 val -= 0x10000; 232 break; 233 } 234 return val; 235 } 236 237 /* 238 * convert from the zero-based int to the byte/word for usb descriptor 239 */ 240 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val) 241 { 242 switch (cval->val_type) { 243 case USB_MIXER_BOOLEAN: 244 return !!val; 245 case USB_MIXER_INV_BOOLEAN: 246 return !val; 247 case USB_MIXER_S8: 248 case USB_MIXER_U8: 249 return val & 0xff; 250 case USB_MIXER_S16: 251 case USB_MIXER_U16: 252 return val & 0xffff; 253 } 254 return 0; /* not reached */ 255 } 256 257 static int get_relative_value(struct usb_mixer_elem_info *cval, int val) 258 { 259 if (!cval->res) 260 cval->res = 1; 261 if (val < cval->min) 262 return 0; 263 else if (val >= cval->max) 264 return DIV_ROUND_UP(cval->max - cval->min, cval->res); 265 else 266 return (val - cval->min) / cval->res; 267 } 268 269 static int get_abs_value(struct usb_mixer_elem_info *cval, int val) 270 { 271 if (val < 0) 272 return cval->min; 273 if (!cval->res) 274 cval->res = 1; 275 val *= cval->res; 276 val += cval->min; 277 if (val > cval->max) 278 return cval->max; 279 return val; 280 } 281 282 static int uac2_ctl_value_size(int val_type) 283 { 284 switch (val_type) { 285 case USB_MIXER_S32: 286 case USB_MIXER_U32: 287 return 4; 288 case USB_MIXER_S16: 289 case USB_MIXER_U16: 290 return 2; 291 default: 292 return 1; 293 } 294 return 0; /* unreachable */ 295 } 296 297 298 /* 299 * retrieve a mixer value 300 */ 301 302 static inline int mixer_ctrl_intf(struct usb_mixer_interface *mixer) 303 { 304 return get_iface_desc(mixer->hostif)->bInterfaceNumber; 305 } 306 307 static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request, 308 int validx, int *value_ret) 309 { 310 struct snd_usb_audio *chip = cval->head.mixer->chip; 311 unsigned char buf[2]; 312 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; 313 int timeout = 10; 314 int idx = 0, err; 315 316 CLASS(snd_usb_lock, pm)(chip); 317 if (pm.err < 0) 318 return -EIO; 319 320 while (timeout-- > 0) { 321 idx = mixer_ctrl_intf(cval->head.mixer) | (cval->head.id << 8); 322 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request, 323 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 324 validx, idx, buf, val_len); 325 if (err >= val_len) { 326 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len)); 327 return 0; 328 } else if (err == -ETIMEDOUT) { 329 return err; 330 } 331 } 332 usb_audio_dbg(chip, 333 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", 334 request, validx, idx, cval->val_type); 335 return -EINVAL; 336 } 337 338 static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request, 339 int validx, int *value_ret) 340 { 341 struct snd_usb_audio *chip = cval->head.mixer->chip; 342 /* enough space for one range */ 343 unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)]; 344 unsigned char *val; 345 int idx = 0, ret, val_size, size; 346 __u8 bRequest; 347 348 val_size = uac2_ctl_value_size(cval->val_type); 349 350 if (request == UAC_GET_CUR) { 351 bRequest = UAC2_CS_CUR; 352 size = val_size; 353 } else { 354 bRequest = UAC2_CS_RANGE; 355 size = sizeof(__u16) + 3 * val_size; 356 } 357 358 memset(buf, 0, sizeof(buf)); 359 360 { 361 CLASS(snd_usb_lock, pm)(chip); 362 if (pm.err) 363 return -EIO; 364 365 idx = mixer_ctrl_intf(cval->head.mixer) | (cval->head.id << 8); 366 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest, 367 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 368 validx, idx, buf, size); 369 } 370 371 if (ret < 0) { 372 usb_audio_dbg(chip, 373 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", 374 request, validx, idx, cval->val_type); 375 return ret; 376 } 377 378 /* FIXME: how should we handle multiple triplets here? */ 379 380 switch (request) { 381 case UAC_GET_CUR: 382 val = buf; 383 break; 384 case UAC_GET_MIN: 385 val = buf + sizeof(__u16); 386 break; 387 case UAC_GET_MAX: 388 val = buf + sizeof(__u16) + val_size; 389 break; 390 case UAC_GET_RES: 391 val = buf + sizeof(__u16) + val_size * 2; 392 break; 393 default: 394 return -EINVAL; 395 } 396 397 *value_ret = convert_signed_value(cval, 398 snd_usb_combine_bytes(val, val_size)); 399 400 return 0; 401 } 402 403 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, 404 int validx, int *value_ret) 405 { 406 validx += cval->idx_off; 407 408 return (cval->head.mixer->protocol == UAC_VERSION_1) ? 409 get_ctl_value_v1(cval, request, validx, value_ret) : 410 get_ctl_value_v2(cval, request, validx, value_ret); 411 } 412 413 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, 414 int validx, int *value) 415 { 416 return get_ctl_value(cval, UAC_GET_CUR, validx, value); 417 } 418 419 /* channel = 0: master, 1 = first channel */ 420 static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval, 421 int channel, int *value) 422 { 423 return get_ctl_value(cval, UAC_GET_CUR, 424 (cval->control << 8) | channel, 425 value); 426 } 427 428 int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval, 429 int channel, int index, int *value) 430 { 431 int err; 432 433 if (cval->cached & BIT(channel)) { 434 *value = cval->cache_val[index]; 435 return 0; 436 } 437 438 /* The current value is always provided by the cache after initialization. */ 439 if (cval->get_cur_broken) 440 return -ENXIO; 441 442 err = get_cur_mix_raw(cval, channel, value); 443 if (err < 0) { 444 if (!cval->head.mixer->ignore_ctl_error) 445 usb_audio_dbg(cval->head.mixer->chip, 446 "cannot get current value for control %d ch %d: err = %d\n", 447 cval->control, channel, err); 448 return err; 449 } 450 cval->cached |= BIT(channel); 451 cval->cache_val[index] = *value; 452 return 0; 453 } 454 455 /* 456 * set a mixer value 457 */ 458 459 int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval, 460 int request, int validx, int value_set) 461 { 462 struct snd_usb_audio *chip = cval->head.mixer->chip; 463 unsigned char buf[4]; 464 int idx = 0, val_len, err, timeout = 10; 465 466 validx += cval->idx_off; 467 468 469 if (cval->head.mixer->protocol == UAC_VERSION_1) { 470 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; 471 } else { /* UAC_VERSION_2/3 */ 472 val_len = uac2_ctl_value_size(cval->val_type); 473 474 /* FIXME */ 475 if (request != UAC_SET_CUR) { 476 usb_audio_dbg(chip, "RANGE setting not yet supported\n"); 477 return -EINVAL; 478 } 479 480 request = UAC2_CS_CUR; 481 } 482 483 value_set = convert_bytes_value(cval, value_set); 484 buf[0] = value_set & 0xff; 485 buf[1] = (value_set >> 8) & 0xff; 486 buf[2] = (value_set >> 16) & 0xff; 487 buf[3] = (value_set >> 24) & 0xff; 488 489 CLASS(snd_usb_lock, pm)(chip); 490 if (pm.err < 0) 491 return -EIO; 492 493 while (timeout-- > 0) { 494 idx = mixer_ctrl_intf(cval->head.mixer) | (cval->head.id << 8); 495 err = snd_usb_ctl_msg(chip->dev, 496 usb_sndctrlpipe(chip->dev, 0), request, 497 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, 498 validx, idx, buf, val_len); 499 if (err >= 0) 500 return 0; 501 else if (err == -ETIMEDOUT) 502 return err; 503 } 504 usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n", 505 request, validx, idx, cval->val_type, buf[0], buf[1]); 506 return -EINVAL; 507 } 508 509 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, 510 int validx, int value) 511 { 512 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value); 513 } 514 515 int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, 516 int index, int value) 517 { 518 int err; 519 unsigned int read_only = (channel == 0) ? 520 cval->master_readonly : 521 cval->ch_readonly & BIT(channel - 1); 522 523 if (read_only) { 524 usb_audio_dbg(cval->head.mixer->chip, 525 "%s(): channel %d of control %d is read_only\n", 526 __func__, channel, cval->control); 527 return 0; 528 } 529 530 err = snd_usb_mixer_set_ctl_value(cval, 531 UAC_SET_CUR, (cval->control << 8) | channel, 532 value); 533 if (err < 0) 534 return err; 535 cval->cached |= BIT(channel); 536 cval->cache_val[index] = value; 537 return 0; 538 } 539 540 /* 541 * TLV callback for mixer volume controls 542 */ 543 int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag, 544 unsigned int size, unsigned int __user *_tlv) 545 { 546 struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); 547 DECLARE_TLV_DB_MINMAX(scale, 0, 0); 548 549 if (size < sizeof(scale)) 550 return -ENOMEM; 551 if (cval->min_mute) 552 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE; 553 scale[2] = cval->dBmin; 554 scale[3] = cval->dBmax; 555 if (copy_to_user(_tlv, scale, sizeof(scale))) 556 return -EFAULT; 557 return 0; 558 } 559 560 /* 561 * parser routines begin here... 562 */ 563 564 static int parse_audio_unit(struct mixer_build *state, int unitid); 565 566 567 /* 568 * check if the input/output channel routing is enabled on the given bitmap. 569 * used for mixer unit parser 570 */ 571 static int check_matrix_bitmap(unsigned char *bmap, 572 int ich, int och, int num_outs) 573 { 574 int idx = ich * num_outs + och; 575 return bmap[idx >> 3] & (0x80 >> (idx & 7)); 576 } 577 578 /* 579 * add an alsa control element 580 * search and increment the index until an empty slot is found. 581 * 582 * if failed, give up and free the control instance. 583 */ 584 585 int snd_usb_mixer_add_list(struct usb_mixer_elem_list *list, 586 struct snd_kcontrol *kctl, 587 bool is_std_info) 588 { 589 struct usb_mixer_interface *mixer = list->mixer; 590 int err; 591 592 while (snd_ctl_find_id(mixer->chip->card, &kctl->id)) 593 kctl->id.index++; 594 err = snd_ctl_add(mixer->chip->card, kctl); 595 if (err < 0) { 596 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n", 597 err); 598 return err; 599 } 600 list->kctl = kctl; 601 list->is_std_info = is_std_info; 602 list->next_id_elem = mixer->id_elems[list->id]; 603 mixer->id_elems[list->id] = list; 604 return 0; 605 } 606 607 /* 608 * get a terminal name string 609 */ 610 611 static struct iterm_name_combo { 612 int type; 613 char *name; 614 } iterm_names[] = { 615 { 0x0300, "Output" }, 616 { 0x0301, "Speaker" }, 617 { 0x0302, "Headphone" }, 618 { 0x0303, "HMD Audio" }, 619 { 0x0304, "Desktop Speaker" }, 620 { 0x0305, "Room Speaker" }, 621 { 0x0306, "Com Speaker" }, 622 { 0x0307, "LFE" }, 623 { 0x0600, "External In" }, 624 { 0x0601, "Analog In" }, 625 { 0x0602, "Digital In" }, 626 { 0x0603, "Line" }, 627 { 0x0604, "Legacy In" }, 628 { 0x0605, "IEC958 In" }, 629 { 0x0606, "1394 DA Stream" }, 630 { 0x0607, "1394 DV Stream" }, 631 { 0x0700, "Embedded" }, 632 { 0x0701, "Noise Source" }, 633 { 0x0702, "Equalization Noise" }, 634 { 0x0703, "CD" }, 635 { 0x0704, "DAT" }, 636 { 0x0705, "DCC" }, 637 { 0x0706, "MiniDisk" }, 638 { 0x0707, "Analog Tape" }, 639 { 0x0708, "Phonograph" }, 640 { 0x0709, "VCR Audio" }, 641 { 0x070a, "Video Disk Audio" }, 642 { 0x070b, "DVD Audio" }, 643 { 0x070c, "TV Tuner Audio" }, 644 { 0x070d, "Satellite Rec Audio" }, 645 { 0x070e, "Cable Tuner Audio" }, 646 { 0x070f, "DSS Audio" }, 647 { 0x0710, "Radio Receiver" }, 648 { 0x0711, "Radio Transmitter" }, 649 { 0x0712, "Multi-Track Recorder" }, 650 { 0x0713, "Synthesizer" }, 651 { 0 }, 652 }; 653 654 static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm, 655 unsigned char *name, int maxlen, int term_only) 656 { 657 struct iterm_name_combo *names; 658 int len; 659 660 if (iterm->name) { 661 len = snd_usb_copy_string_desc(chip, iterm->name, 662 name, maxlen); 663 if (len) 664 return len; 665 } 666 667 /* virtual type - not a real terminal */ 668 if (iterm->type >> 16) { 669 if (term_only) 670 return 0; 671 switch (iterm->type >> 16) { 672 case UAC3_SELECTOR_UNIT: 673 return strscpy(name, "Selector", maxlen); 674 case UAC3_PROCESSING_UNIT: 675 return strscpy(name, "Process Unit", maxlen); 676 case UAC3_EXTENSION_UNIT: 677 return strscpy(name, "Ext Unit", maxlen); 678 case UAC3_MIXER_UNIT: 679 return strscpy(name, "Mixer", maxlen); 680 default: 681 return scnprintf(name, maxlen, "Unit %d", iterm->id); 682 } 683 } 684 685 switch (iterm->type & 0xff00) { 686 case 0x0100: 687 return strscpy(name, "PCM", maxlen); 688 case 0x0200: 689 return strscpy(name, "Mic", maxlen); 690 case 0x0400: 691 return strscpy(name, "Headset", maxlen); 692 case 0x0500: 693 return strscpy(name, "Phone", maxlen); 694 } 695 696 for (names = iterm_names; names->type; names++) 697 if (names->type == iterm->type) 698 return strscpy(name, names->name, maxlen); 699 700 return 0; 701 } 702 703 /* 704 * Get logical cluster information for UAC3 devices. 705 */ 706 static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id) 707 { 708 struct uac3_cluster_header_descriptor c_header; 709 int err; 710 711 err = snd_usb_ctl_msg(state->chip->dev, 712 usb_rcvctrlpipe(state->chip->dev, 0), 713 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR, 714 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 715 cluster_id, 716 snd_usb_ctrl_intf(state->mixer->hostif), 717 &c_header, sizeof(c_header)); 718 if (err < 0) 719 goto error; 720 if (err != sizeof(c_header)) { 721 err = -EIO; 722 goto error; 723 } 724 725 return c_header.bNrChannels; 726 727 error: 728 usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err); 729 return err; 730 } 731 732 /* 733 * Get number of channels for a Mixer Unit. 734 */ 735 static int uac_mixer_unit_get_channels(struct mixer_build *state, 736 struct uac_mixer_unit_descriptor *desc) 737 { 738 int mu_channels; 739 740 switch (state->mixer->protocol) { 741 case UAC_VERSION_1: 742 case UAC_VERSION_2: 743 default: 744 if (desc->bLength < sizeof(*desc) + desc->bNrInPins + 1) 745 return 0; /* no bmControls -> skip */ 746 mu_channels = uac_mixer_unit_bNrChannels(desc); 747 break; 748 case UAC_VERSION_3: 749 mu_channels = get_cluster_channels_v3(state, 750 uac3_mixer_unit_wClusterDescrID(desc)); 751 break; 752 } 753 754 return mu_channels; 755 } 756 757 /* 758 * Parse Input Terminal Unit 759 */ 760 static int __check_input_term(struct mixer_build *state, int id, 761 struct usb_audio_term *term); 762 763 static int parse_term_uac1_iterm_unit(struct mixer_build *state, 764 struct usb_audio_term *term, 765 void *p1, int id) 766 { 767 struct uac_input_terminal_descriptor *d = p1; 768 769 term->type = le16_to_cpu(d->wTerminalType); 770 term->channels = d->bNrChannels; 771 term->chconfig = le16_to_cpu(d->wChannelConfig); 772 term->name = d->iTerminal; 773 return 0; 774 } 775 776 static int parse_term_uac2_iterm_unit(struct mixer_build *state, 777 struct usb_audio_term *term, 778 void *p1, int id) 779 { 780 struct uac2_input_terminal_descriptor *d = p1; 781 int err; 782 783 /* call recursively to verify the referenced clock entity */ 784 err = __check_input_term(state, d->bCSourceID, term); 785 if (err < 0) 786 return err; 787 788 /* save input term properties after recursion, 789 * to ensure they are not overriden by the recursion calls 790 */ 791 term->id = id; 792 term->type = le16_to_cpu(d->wTerminalType); 793 term->channels = d->bNrChannels; 794 term->chconfig = le32_to_cpu(d->bmChannelConfig); 795 term->name = d->iTerminal; 796 return 0; 797 } 798 799 static int parse_term_uac3_iterm_unit(struct mixer_build *state, 800 struct usb_audio_term *term, 801 void *p1, int id) 802 { 803 struct uac3_input_terminal_descriptor *d = p1; 804 int err; 805 806 /* call recursively to verify the referenced clock entity */ 807 err = __check_input_term(state, d->bCSourceID, term); 808 if (err < 0) 809 return err; 810 811 /* save input term properties after recursion, 812 * to ensure they are not overriden by the recursion calls 813 */ 814 term->id = id; 815 term->type = le16_to_cpu(d->wTerminalType); 816 817 err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID)); 818 if (err < 0) 819 return err; 820 term->channels = err; 821 822 /* REVISIT: UAC3 IT doesn't have channels cfg */ 823 term->chconfig = 0; 824 825 term->name = le16_to_cpu(d->wTerminalDescrStr); 826 return 0; 827 } 828 829 static int parse_term_mixer_unit(struct mixer_build *state, 830 struct usb_audio_term *term, 831 void *p1, int id) 832 { 833 struct uac_mixer_unit_descriptor *d = p1; 834 int protocol = state->mixer->protocol; 835 int err; 836 837 err = uac_mixer_unit_get_channels(state, d); 838 if (err <= 0) 839 return err; 840 841 term->type = UAC3_MIXER_UNIT << 16; /* virtual type */ 842 term->channels = err; 843 if (protocol != UAC_VERSION_3) { 844 term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol); 845 term->name = uac_mixer_unit_iMixer(d); 846 } 847 return 0; 848 } 849 850 static int parse_term_selector_unit(struct mixer_build *state, 851 struct usb_audio_term *term, 852 void *p1, int id) 853 { 854 struct uac_selector_unit_descriptor *d = p1; 855 int err; 856 857 /* call recursively to retrieve the channel info */ 858 err = __check_input_term(state, d->baSourceID[0], term); 859 if (err < 0) 860 return err; 861 term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */ 862 term->id = id; 863 if (state->mixer->protocol != UAC_VERSION_3) 864 term->name = uac_selector_unit_iSelector(d); 865 return 0; 866 } 867 868 static int parse_term_proc_unit(struct mixer_build *state, 869 struct usb_audio_term *term, 870 void *p1, int id, int vtype) 871 { 872 struct uac_processing_unit_descriptor *d = p1; 873 int protocol = state->mixer->protocol; 874 int err; 875 876 if (d->bNrInPins) { 877 /* call recursively to retrieve the channel info */ 878 err = __check_input_term(state, d->baSourceID[0], term); 879 if (err < 0) 880 return err; 881 } 882 883 term->type = vtype << 16; /* virtual type */ 884 term->id = id; 885 886 if (protocol == UAC_VERSION_3) 887 return 0; 888 889 if (!term->channels) { 890 term->channels = uac_processing_unit_bNrChannels(d); 891 term->chconfig = uac_processing_unit_wChannelConfig(d, protocol); 892 } 893 term->name = uac_processing_unit_iProcessing(d, protocol); 894 return 0; 895 } 896 897 static int parse_term_effect_unit(struct mixer_build *state, 898 struct usb_audio_term *term, 899 void *p1, int id) 900 { 901 struct uac2_effect_unit_descriptor *d = p1; 902 int err; 903 904 err = __check_input_term(state, d->bSourceID, term); 905 if (err < 0) 906 return err; 907 term->type = UAC3_EFFECT_UNIT << 16; /* virtual type */ 908 term->id = id; 909 return 0; 910 } 911 912 static int parse_term_uac2_clock_source(struct mixer_build *state, 913 struct usb_audio_term *term, 914 void *p1, int id) 915 { 916 struct uac_clock_source_descriptor *d = p1; 917 918 term->type = UAC2_CLOCK_SOURCE << 16; /* virtual type */ 919 term->id = id; 920 term->name = d->iClockSource; 921 return 0; 922 } 923 924 static int parse_term_uac3_clock_source(struct mixer_build *state, 925 struct usb_audio_term *term, 926 void *p1, int id) 927 { 928 struct uac3_clock_source_descriptor *d = p1; 929 930 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */ 931 term->id = id; 932 term->name = le16_to_cpu(d->wClockSourceStr); 933 return 0; 934 } 935 936 #define PTYPE(a, b) ((a) << 8 | (b)) 937 938 /* 939 * parse the source unit recursively until it reaches to a terminal 940 * or a branched unit. 941 */ 942 static int __check_input_term(struct mixer_build *state, int id, 943 struct usb_audio_term *term) 944 { 945 int protocol = state->mixer->protocol; 946 void *p1; 947 unsigned char *hdr; 948 949 for (;;) { 950 /* a loop in the terminal chain? */ 951 if (test_and_set_bit(id, state->termbitmap)) 952 return -EINVAL; 953 954 p1 = find_audio_control_unit(state, id); 955 if (!p1) 956 break; 957 if (!snd_usb_validate_audio_desc(p1, protocol)) 958 break; /* bad descriptor */ 959 960 hdr = p1; 961 term->id = id; 962 963 switch (PTYPE(protocol, hdr[2])) { 964 case PTYPE(UAC_VERSION_1, UAC_FEATURE_UNIT): 965 case PTYPE(UAC_VERSION_2, UAC_FEATURE_UNIT): 966 case PTYPE(UAC_VERSION_3, UAC3_FEATURE_UNIT): { 967 /* the header is the same for all versions */ 968 struct uac_feature_unit_descriptor *d = p1; 969 970 id = d->bSourceID; 971 break; /* continue to parse */ 972 } 973 case PTYPE(UAC_VERSION_1, UAC_INPUT_TERMINAL): 974 return parse_term_uac1_iterm_unit(state, term, p1, id); 975 case PTYPE(UAC_VERSION_2, UAC_INPUT_TERMINAL): 976 return parse_term_uac2_iterm_unit(state, term, p1, id); 977 case PTYPE(UAC_VERSION_3, UAC_INPUT_TERMINAL): 978 return parse_term_uac3_iterm_unit(state, term, p1, id); 979 case PTYPE(UAC_VERSION_1, UAC_MIXER_UNIT): 980 case PTYPE(UAC_VERSION_2, UAC_MIXER_UNIT): 981 case PTYPE(UAC_VERSION_3, UAC3_MIXER_UNIT): 982 return parse_term_mixer_unit(state, term, p1, id); 983 case PTYPE(UAC_VERSION_1, UAC_SELECTOR_UNIT): 984 case PTYPE(UAC_VERSION_2, UAC_SELECTOR_UNIT): 985 case PTYPE(UAC_VERSION_2, UAC2_CLOCK_SELECTOR): 986 case PTYPE(UAC_VERSION_3, UAC3_SELECTOR_UNIT): 987 case PTYPE(UAC_VERSION_3, UAC3_CLOCK_SELECTOR): 988 return parse_term_selector_unit(state, term, p1, id); 989 case PTYPE(UAC_VERSION_1, UAC1_PROCESSING_UNIT): 990 case PTYPE(UAC_VERSION_2, UAC2_PROCESSING_UNIT_V2): 991 case PTYPE(UAC_VERSION_3, UAC3_PROCESSING_UNIT): 992 return parse_term_proc_unit(state, term, p1, id, 993 UAC3_PROCESSING_UNIT); 994 case PTYPE(UAC_VERSION_2, UAC2_EFFECT_UNIT): 995 case PTYPE(UAC_VERSION_3, UAC3_EFFECT_UNIT): 996 return parse_term_effect_unit(state, term, p1, id); 997 case PTYPE(UAC_VERSION_1, UAC1_EXTENSION_UNIT): 998 case PTYPE(UAC_VERSION_2, UAC2_EXTENSION_UNIT_V2): 999 case PTYPE(UAC_VERSION_3, UAC3_EXTENSION_UNIT): 1000 return parse_term_proc_unit(state, term, p1, id, 1001 UAC3_EXTENSION_UNIT); 1002 case PTYPE(UAC_VERSION_2, UAC2_CLOCK_SOURCE): 1003 return parse_term_uac2_clock_source(state, term, p1, id); 1004 case PTYPE(UAC_VERSION_3, UAC3_CLOCK_SOURCE): 1005 return parse_term_uac3_clock_source(state, term, p1, id); 1006 default: 1007 return -ENODEV; 1008 } 1009 } 1010 return -ENODEV; 1011 } 1012 1013 1014 static int check_input_term(struct mixer_build *state, int id, 1015 struct usb_audio_term *term) 1016 { 1017 memset(term, 0, sizeof(*term)); 1018 memset(state->termbitmap, 0, sizeof(state->termbitmap)); 1019 return __check_input_term(state, id, term); 1020 } 1021 1022 /* 1023 * Feature Unit 1024 */ 1025 1026 /* feature unit control information */ 1027 struct usb_feature_control_info { 1028 int control; 1029 const char *name; 1030 int type; /* data type for uac1 */ 1031 int type_uac2; /* data type for uac2 if different from uac1, else -1 */ 1032 }; 1033 1034 static const struct usb_feature_control_info audio_feature_info[] = { 1035 { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 }, 1036 { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 }, 1037 { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 }, 1038 { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 }, 1039 { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 }, 1040 { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */ 1041 { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 }, 1042 { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 }, 1043 { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 }, 1044 { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 }, 1045 /* UAC2 specific */ 1046 { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 }, 1047 { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 }, 1048 { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 }, 1049 }; 1050 1051 static void usb_mixer_elem_info_free(struct usb_mixer_elem_info *cval) 1052 { 1053 kfree(cval); 1054 } 1055 1056 /* private_free callback */ 1057 void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl) 1058 { 1059 usb_mixer_elem_info_free(kctl->private_data); 1060 kctl->private_data = NULL; 1061 } 1062 1063 /* 1064 * interface to ALSA control for feature/mixer units 1065 */ 1066 1067 /* volume control quirks */ 1068 static void volume_control_quirks(struct usb_mixer_elem_info *cval, 1069 struct snd_kcontrol *kctl) 1070 { 1071 struct snd_usb_audio *chip = cval->head.mixer->chip; 1072 1073 if (chip->quirk_flags & QUIRK_FLAG_MIC_RES_384) { 1074 if (!strcmp(kctl->id.name, "Mic Capture Volume")) { 1075 usb_audio_info(chip, 1076 "set resolution quirk: cval->res = 384\n"); 1077 cval->res = 384; 1078 } 1079 } else if (chip->quirk_flags & QUIRK_FLAG_MIC_RES_16) { 1080 if (!strcmp(kctl->id.name, "Mic Capture Volume")) { 1081 usb_audio_info(chip, 1082 "set resolution quirk: cval->res = 16\n"); 1083 cval->res = 16; 1084 } 1085 } 1086 1087 switch (chip->usb_id) { 1088 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */ 1089 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */ 1090 if (strcmp(kctl->id.name, "Effect Duration") == 0) { 1091 cval->min = 0x0000; 1092 cval->max = 0xffff; 1093 cval->res = 0x00e6; 1094 break; 1095 } 1096 if (strcmp(kctl->id.name, "Effect Volume") == 0 || 1097 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) { 1098 cval->min = 0x00; 1099 cval->max = 0xff; 1100 break; 1101 } 1102 if (strstr(kctl->id.name, "Effect Return") != NULL) { 1103 cval->min = 0xb706; 1104 cval->max = 0xff7b; 1105 cval->res = 0x0073; 1106 break; 1107 } 1108 if ((strstr(kctl->id.name, "Playback Volume") != NULL) || 1109 (strstr(kctl->id.name, "Effect Send") != NULL)) { 1110 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */ 1111 cval->max = 0xfcfe; 1112 cval->res = 0x0073; 1113 } 1114 break; 1115 1116 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */ 1117 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */ 1118 if (strcmp(kctl->id.name, "Effect Duration") == 0) { 1119 usb_audio_info(chip, 1120 "set quirk for FTU Effect Duration\n"); 1121 cval->min = 0x0000; 1122 cval->max = 0x7f00; 1123 cval->res = 0x0100; 1124 break; 1125 } 1126 if (strcmp(kctl->id.name, "Effect Volume") == 0 || 1127 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) { 1128 usb_audio_info(chip, 1129 "set quirks for FTU Effect Feedback/Volume\n"); 1130 cval->min = 0x00; 1131 cval->max = 0x7f; 1132 break; 1133 } 1134 break; 1135 1136 case USB_ID(0x0d8c, 0x0103): 1137 if (!strcmp(kctl->id.name, "PCM Playback Volume")) { 1138 usb_audio_info(chip, 1139 "set volume quirk for CM102-A+/102S+\n"); 1140 cval->min = -256; 1141 } 1142 break; 1143 1144 case USB_ID(0x045e, 0x070f): /* MS LifeChat LX-3000 Headset */ 1145 if (!strcmp(kctl->id.name, "Speaker Playback Volume")) { 1146 usb_audio_info(chip, 1147 "set volume quirk for MS LifeChat LX-3000\n"); 1148 cval->res = 192; 1149 } 1150 break; 1151 1152 case USB_ID(0x0471, 0x0101): 1153 case USB_ID(0x0471, 0x0104): 1154 case USB_ID(0x0471, 0x0105): 1155 case USB_ID(0x0672, 0x1041): 1156 /* quirk for UDA1321/N101. 1157 * note that detection between firmware 2.1.1.7 (N101) 1158 * and later 2.1.1.21 is not very clear from datasheets. 1159 * I hope that the min value is -15360 for newer firmware --jk 1160 */ 1161 if (!strcmp(kctl->id.name, "PCM Playback Volume") && 1162 cval->min == -15616) { 1163 usb_audio_info(chip, 1164 "set volume quirk for UDA1321/N101 chip\n"); 1165 cval->max = -256; 1166 } 1167 break; 1168 1169 case USB_ID(0x046d, 0x09a4): 1170 if (!strcmp(kctl->id.name, "Mic Capture Volume")) { 1171 usb_audio_info(chip, 1172 "set volume quirk for QuickCam E3500\n"); 1173 cval->min = 6080; 1174 cval->max = 8768; 1175 cval->res = 192; 1176 } 1177 break; 1178 1179 case USB_ID(0x0495, 0x3042): /* ESS Technology Asus USB DAC */ 1180 if ((strstr(kctl->id.name, "Playback Volume") != NULL) || 1181 strstr(kctl->id.name, "Capture Volume") != NULL) { 1182 cval->min >>= 8; 1183 cval->max = 0; 1184 cval->res = 1; 1185 } 1186 break; 1187 1188 case USB_ID(0x0e6f, 0x024a): /* PDP Riffmaster for PS4 */ 1189 case USB_ID(0x0e6f, 0x0249): /* PDP Riffmaster for PS5 */ 1190 if (!strcmp(kctl->id.name, "PCM Playback Volume")) { 1191 usb_audio_info(chip, 1192 "set volume quirk for PDP Riffmaster for PS4/PS5\n"); 1193 cval->min = -2560; /* Mute under it */ 1194 } 1195 break; 1196 1197 case USB_ID(0x3302, 0x12db): /* MOONDROP Quark2 */ 1198 if (!strcmp(kctl->id.name, "PCM Playback Volume")) { 1199 usb_audio_info(chip, 1200 "set volume quirk for MOONDROP Quark2\n"); 1201 cval->min = -14208; /* Mute under it */ 1202 } 1203 break; 1204 case USB_ID(0x12d1, 0x3a07): /* Huawei Technologies Co., Ltd. CM-Q3 */ 1205 if (!strcmp(kctl->id.name, "PCM Playback Volume")) { 1206 usb_audio_info(chip, 1207 "set volume quirk for Huawei Technologies Co., Ltd. CM-Q3\n"); 1208 cval->min = -11264; /* Mute under it */ 1209 } 1210 break; 1211 case USB_ID(0x31b2, 0x0111): /* MOONDROP JU Jiu */ 1212 if (!strcmp(kctl->id.name, "PCM Playback Volume")) { 1213 usb_audio_info(chip, 1214 "set volume quirk for MOONDROP JU Jiu\n"); 1215 cval->min = -10880; /* Mute under it */ 1216 } 1217 break; 1218 } 1219 } 1220 1221 /* forcibly initialize the current mixer value; if GET_CUR fails, set to 1222 * the minimum as default 1223 */ 1224 static void init_cur_mix_raw(struct usb_mixer_elem_info *cval, int ch, int idx) 1225 { 1226 int val, err; 1227 1228 err = snd_usb_get_cur_mix_value(cval, ch, idx, &val); 1229 if (!err) 1230 return; 1231 if (!cval->head.mixer->ignore_ctl_error && !cval->get_cur_broken) 1232 usb_audio_warn(cval->head.mixer->chip, 1233 "%d:%d: failed to get current value for ch %d (%d)\n", 1234 cval->head.id, mixer_ctrl_intf(cval->head.mixer), 1235 ch, err); 1236 snd_usb_set_cur_mix_value(cval, ch, idx, cval->min); 1237 } 1238 1239 /* 1240 * Additional checks for sticky mixers 1241 * 1242 * Some devices' volume control mixers are sticky, which accept SET_CUR but 1243 * do absolutely nothing. 1244 * 1245 * Check the return values of GET_CUR with different SET_CUR values. Consider 1246 * the mixer as sticky if GET_CUR always returns a constant value. 1247 * 1248 * Some devices have effective SET_CUR despite GET_CUR being constant. Do not 1249 * consider the mixer as sticky if a quirk flag indicates that. 1250 * 1251 * Gate the registration of sticky mixers to prevent confusing userspace, so 1252 * that they won't cause ineffective volume control. However, for mixers with 1253 * effective SET_CUR but broken GET_CUR, the registration can continue normally 1254 * but further GET_CUR requests will be gated. 1255 */ 1256 static int check_sticky_volume_control(struct usb_mixer_elem_info *cval, 1257 int channel, int saved) 1258 { 1259 int sticky_test_values[] = { cval->min, cval->max }; 1260 int test, check, i; 1261 1262 for (i = 0; i < ARRAY_SIZE(sticky_test_values); i++) { 1263 test = sticky_test_values[i]; 1264 if (test == saved) 1265 continue; 1266 1267 /* Assume non-sticky on failure. */ 1268 if (snd_usb_set_cur_mix_value(cval, channel, 0, test) || 1269 get_cur_mix_raw(cval, channel, &check) || 1270 check != saved) /* SET_CUR effective, non-sticky. */ 1271 return 0; 1272 } 1273 1274 if (cval->head.mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_GET_CUR_BROKEN) { 1275 usb_audio_info(cval->head.mixer->chip, 1276 "%d:%d: broken mixer GET_CUR (%d/%d/%d => %d)\n", 1277 cval->head.id, mixer_ctrl_intf(cval->head.mixer), 1278 cval->min, cval->max, cval->res, saved); 1279 1280 cval->get_cur_broken = 1; 1281 return -ENXIO; 1282 } 1283 1284 usb_audio_err(cval->head.mixer->chip, 1285 "%d:%d: sticky mixer values (%d/%d/%d => %d), disabling\n", 1286 cval->head.id, mixer_ctrl_intf(cval->head.mixer), 1287 cval->min, cval->max, cval->res, saved); 1288 usb_audio_info(cval->head.mixer->chip, 1289 "check MIXER_GET_CUR_BROKEN if you believe the mixer is non-sticky"); 1290 1291 return -ENODEV; 1292 } 1293 1294 /* 1295 * Additional checks for the proper resolution 1296 * 1297 * Some devices report smaller resolutions than actually reacting. 1298 * They don't return errors but simply clip to the lower aligned value. 1299 */ 1300 static void check_volume_control_res(struct usb_mixer_elem_info *cval, 1301 int channel, int saved) 1302 { 1303 int last_valid_res = cval->res; 1304 int test, check; 1305 1306 for (;;) { 1307 test = saved; 1308 if (test < cval->max) 1309 test += cval->res; 1310 else 1311 test -= cval->res; 1312 1313 if (test < cval->min || test > cval->max || 1314 snd_usb_set_cur_mix_value(cval, channel, 0, test) || 1315 get_cur_mix_raw(cval, channel, &check)) { 1316 cval->res = last_valid_res; 1317 break; 1318 } 1319 if (test == check) 1320 break; 1321 1322 cval->res *= 2; 1323 } 1324 } 1325 1326 /* 1327 * retrieve the minimum and maximum values for the specified control 1328 */ 1329 static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval, 1330 int default_min, struct snd_kcontrol *kctl) 1331 { 1332 int i, idx, ret = 0; 1333 1334 /* for failsafe */ 1335 cval->min = default_min; 1336 cval->max = cval->min + 1; 1337 cval->res = 1; 1338 cval->dBmin = cval->dBmax = 0; 1339 1340 if (cval->val_type == USB_MIXER_BOOLEAN || 1341 cval->val_type == USB_MIXER_INV_BOOLEAN) { 1342 cval->initialized = 1; 1343 } else { 1344 int minchn = 0; 1345 if (cval->cmask) { 1346 for (i = 0; i < MAX_CHANNELS; i++) 1347 if (cval->cmask & BIT(i)) { 1348 minchn = i + 1; 1349 break; 1350 } 1351 } 1352 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 || 1353 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) { 1354 usb_audio_err(cval->head.mixer->chip, 1355 "%d:%d: cannot get min/max values for control %d (id %d)\n", 1356 cval->head.id, mixer_ctrl_intf(cval->head.mixer), 1357 cval->control, cval->head.id); 1358 return -EAGAIN; 1359 } 1360 if (get_ctl_value(cval, UAC_GET_RES, 1361 (cval->control << 8) | minchn, 1362 &cval->res) < 0) { 1363 cval->res = 1; 1364 } else if (cval->head.mixer->protocol == UAC_VERSION_1) { 1365 int last_valid_res = cval->res; 1366 1367 while (cval->res > 1) { 1368 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES, 1369 (cval->control << 8) | minchn, 1370 cval->res / 2) < 0) 1371 break; 1372 cval->res /= 2; 1373 } 1374 if (get_ctl_value(cval, UAC_GET_RES, 1375 (cval->control << 8) | minchn, &cval->res) < 0) 1376 cval->res = last_valid_res; 1377 } 1378 if (cval->res == 0) 1379 cval->res = 1; 1380 1381 if (cval->min < cval->max) { 1382 int saved; 1383 1384 if (get_cur_mix_raw(cval, minchn, &saved) < 0) 1385 goto no_checks; 1386 1387 ret = check_sticky_volume_control(cval, minchn, saved); 1388 if (ret == -ENODEV) 1389 goto sticky; 1390 if (ret) 1391 goto no_checks; 1392 1393 if (cval->min + cval->res < cval->max) 1394 check_volume_control_res(cval, minchn, saved); 1395 1396 snd_usb_set_cur_mix_value(cval, minchn, 0, saved); 1397 } 1398 1399 no_checks: 1400 /* 1401 * Got a non-fatal failure during sanity checks. 1402 * 1403 * Do not propagate mixer values written by sanity checks. 1404 * Instead, rely on init_cur_mix_raw() to initialize the mixer 1405 * properly. 1406 */ 1407 if (ret) 1408 cval->cached = 0; 1409 1410 cval->initialized = 1; 1411 } 1412 1413 if (kctl) 1414 volume_control_quirks(cval, kctl); 1415 1416 /* USB descriptions contain the dB scale in 1/256 dB unit 1417 * while ALSA TLV contains in 1/100 dB unit 1418 */ 1419 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256; 1420 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256; 1421 if (cval->dBmin > cval->dBmax) { 1422 /* something is wrong; assume it's either from/to 0dB */ 1423 if (cval->dBmin < 0) 1424 cval->dBmax = 0; 1425 else if (cval->dBmin > 0) 1426 cval->dBmin = 0; 1427 if (cval->dBmin > cval->dBmax) { 1428 /* totally crap, return an error */ 1429 return -EINVAL; 1430 } 1431 } else { 1432 /* if the max volume is too low, it's likely a bogus range; 1433 * here we use -96dB as the threshold 1434 */ 1435 if (cval->dBmax <= -9600) { 1436 usb_audio_info(cval->head.mixer->chip, 1437 "%d:%d: bogus dB values (%d/%d), disabling dB reporting\n", 1438 cval->head.id, mixer_ctrl_intf(cval->head.mixer), 1439 cval->dBmin, cval->dBmax); 1440 cval->dBmin = cval->dBmax = 0; 1441 } 1442 } 1443 1444 /* initialize all elements */ 1445 if (!cval->cmask) { 1446 init_cur_mix_raw(cval, 0, 0); 1447 } else { 1448 idx = 0; 1449 for (i = 0; i < MAX_CHANNELS; i++) { 1450 if (cval->cmask & BIT(i)) { 1451 init_cur_mix_raw(cval, i + 1, idx); 1452 idx++; 1453 } 1454 } 1455 } 1456 1457 return 0; 1458 1459 sticky: 1460 /* 1461 * It makes no sense to restore the saved value for a sticky mixer, 1462 * since setting any value is a no-op. 1463 * 1464 * However, in some rare cases, SET_CUR is effective despite GET_CUR 1465 * always returns a constant value. These mixers are not sticky, but 1466 * there's no way to distinguish them. Without any additional 1467 * information, the best thing we can do is to set the mixer value to 1468 * the maximum before bailing out, so that a soft mixer can still reach 1469 * the maximum hardware volume if the mixer turns out to be non-sticky. 1470 * Meanwhile, all channels must be synchronized to prevent imbalance 1471 * volume. 1472 */ 1473 if (!cval->cmask) { 1474 snd_usb_set_cur_mix_value(cval, 0, 0, cval->max); 1475 } else { 1476 for (i = 0; i < MAX_CHANNELS; i++) { 1477 idx = 0; 1478 if (cval->cmask & BIT(i)) { 1479 snd_usb_set_cur_mix_value(cval, i + 1, idx, cval->max); 1480 idx++; 1481 } 1482 } 1483 } 1484 return ret; 1485 } 1486 1487 #define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL) 1488 1489 /* get the max value advertised via control API */ 1490 static int get_max_exposed(struct usb_mixer_elem_info *cval) 1491 { 1492 if (!cval->max_exposed) { 1493 if (cval->res) 1494 cval->max_exposed = 1495 DIV_ROUND_UP(cval->max - cval->min, cval->res); 1496 else 1497 cval->max_exposed = cval->max - cval->min; 1498 } 1499 return cval->max_exposed; 1500 } 1501 1502 /* get a feature/mixer unit info */ 1503 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, 1504 struct snd_ctl_elem_info *uinfo) 1505 { 1506 struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); 1507 int ret; 1508 1509 if (cval->val_type == USB_MIXER_BOOLEAN || 1510 cval->val_type == USB_MIXER_INV_BOOLEAN) 1511 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1512 else 1513 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1514 uinfo->count = cval->channels; 1515 if (cval->val_type != USB_MIXER_BOOLEAN && 1516 cval->val_type != USB_MIXER_INV_BOOLEAN) { 1517 if (!cval->initialized) { 1518 ret = get_min_max_with_quirks(cval, 0, kcontrol); 1519 if ((ret >= 0 || ret == -EAGAIN) && 1520 cval->initialized && cval->dBmin >= cval->dBmax) { 1521 kcontrol->vd[0].access &= 1522 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ | 1523 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK); 1524 snd_ctl_notify(cval->head.mixer->chip->card, 1525 SNDRV_CTL_EVENT_MASK_INFO, 1526 &kcontrol->id); 1527 } 1528 } 1529 } 1530 1531 uinfo->value.integer.min = 0; 1532 uinfo->value.integer.max = get_max_exposed(cval); 1533 return 0; 1534 } 1535 1536 /* get the current value from feature/mixer unit */ 1537 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, 1538 struct snd_ctl_elem_value *ucontrol) 1539 { 1540 struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); 1541 int c, cnt, val, err; 1542 1543 ucontrol->value.integer.value[0] = cval->min; 1544 if (cval->cmask) { 1545 cnt = 0; 1546 for (c = 0; c < MAX_CHANNELS; c++) { 1547 if (!(cval->cmask & BIT(c))) 1548 continue; 1549 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val); 1550 if (err < 0) 1551 return filter_error(cval, err); 1552 val = get_relative_value(cval, val); 1553 ucontrol->value.integer.value[cnt] = val; 1554 cnt++; 1555 } 1556 return 0; 1557 } else { 1558 /* master channel */ 1559 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val); 1560 if (err < 0) 1561 return filter_error(cval, err); 1562 val = get_relative_value(cval, val); 1563 ucontrol->value.integer.value[0] = val; 1564 } 1565 return 0; 1566 } 1567 1568 /* put the current value to feature/mixer unit */ 1569 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, 1570 struct snd_ctl_elem_value *ucontrol) 1571 { 1572 struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); 1573 int max_val = get_max_exposed(cval); 1574 int c, cnt, val, oval, err; 1575 int changed = 0; 1576 1577 if (cval->cmask) { 1578 cnt = 0; 1579 for (c = 0; c < MAX_CHANNELS; c++) { 1580 if (!(cval->cmask & BIT(c))) 1581 continue; 1582 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval); 1583 if (err < 0) 1584 return filter_error(cval, err); 1585 val = ucontrol->value.integer.value[cnt]; 1586 if (val < 0 || val > max_val) 1587 return -EINVAL; 1588 val = get_abs_value(cval, val); 1589 if (oval != val) { 1590 err = snd_usb_set_cur_mix_value(cval, c + 1, 1591 cnt, val); 1592 if (err < 0) 1593 return filter_error(cval, err); 1594 changed = 1; 1595 } 1596 cnt++; 1597 } 1598 } else { 1599 /* master channel */ 1600 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval); 1601 if (err < 0) 1602 return filter_error(cval, err); 1603 val = ucontrol->value.integer.value[0]; 1604 if (val < 0 || val > max_val) 1605 return -EINVAL; 1606 val = get_abs_value(cval, val); 1607 if (val != oval) { 1608 err = snd_usb_set_cur_mix_value(cval, 0, 0, val); 1609 if (err < 0) 1610 return filter_error(cval, err); 1611 changed = 1; 1612 } 1613 } 1614 return changed; 1615 } 1616 1617 /* get the boolean value from the master channel of a UAC control */ 1618 static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol, 1619 struct snd_ctl_elem_value *ucontrol) 1620 { 1621 struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); 1622 int val, err; 1623 1624 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val); 1625 if (err < 0) 1626 return filter_error(cval, err); 1627 val = (val != 0); 1628 ucontrol->value.integer.value[0] = val; 1629 return 0; 1630 } 1631 1632 static int get_connector_value(struct usb_mixer_elem_info *cval, 1633 char *name, int *val) 1634 { 1635 struct snd_usb_audio *chip = cval->head.mixer->chip; 1636 int idx = 0, validx, ret; 1637 1638 validx = cval->control << 8 | 0; 1639 1640 CLASS(snd_usb_lock, pm)(chip); 1641 if (pm.err) { 1642 ret = -EIO; 1643 goto error; 1644 } 1645 1646 idx = mixer_ctrl_intf(cval->head.mixer) | (cval->head.id << 8); 1647 if (cval->head.mixer->protocol == UAC_VERSION_2) { 1648 struct uac2_connectors_ctl_blk uac2_conn; 1649 1650 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR, 1651 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 1652 validx, idx, &uac2_conn, sizeof(uac2_conn)); 1653 if (val) 1654 *val = !!uac2_conn.bNrChannels; 1655 } else { /* UAC_VERSION_3 */ 1656 struct uac3_insertion_ctl_blk uac3_conn; 1657 1658 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR, 1659 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 1660 validx, idx, &uac3_conn, sizeof(uac3_conn)); 1661 if (val) 1662 *val = !!uac3_conn.bmConInserted; 1663 } 1664 1665 if (ret < 0) { 1666 if (name && strstr(name, "Speaker")) { 1667 if (val) 1668 *val = 1; 1669 return 0; 1670 } 1671 error: 1672 usb_audio_err(chip, 1673 "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", 1674 UAC_GET_CUR, validx, idx, cval->val_type); 1675 1676 if (val) 1677 *val = 0; 1678 1679 return filter_error(cval, ret); 1680 } 1681 1682 return ret; 1683 } 1684 1685 /* get the connectors status and report it as boolean type */ 1686 static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol, 1687 struct snd_ctl_elem_value *ucontrol) 1688 { 1689 struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); 1690 int ret, val; 1691 1692 ret = get_connector_value(cval, kcontrol->id.name, &val); 1693 1694 if (ret < 0) 1695 return ret; 1696 1697 ucontrol->value.integer.value[0] = val; 1698 return 0; 1699 } 1700 1701 static const struct snd_kcontrol_new usb_feature_unit_ctl = { 1702 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1703 .name = "", /* will be filled later manually */ 1704 .info = mixer_ctl_feature_info, 1705 .get = mixer_ctl_feature_get, 1706 .put = mixer_ctl_feature_put, 1707 }; 1708 1709 /* the read-only variant */ 1710 static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = { 1711 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1712 .name = "", /* will be filled later manually */ 1713 .info = mixer_ctl_feature_info, 1714 .get = mixer_ctl_feature_get, 1715 .put = NULL, 1716 }; 1717 1718 /* 1719 * A control which shows the boolean value from reading a UAC control on 1720 * the master channel. 1721 */ 1722 static const struct snd_kcontrol_new usb_bool_master_control_ctl_ro = { 1723 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 1724 .name = "", /* will be filled later manually */ 1725 .access = SNDRV_CTL_ELEM_ACCESS_READ, 1726 .info = snd_ctl_boolean_mono_info, 1727 .get = mixer_ctl_master_bool_get, 1728 .put = NULL, 1729 }; 1730 1731 static const struct snd_kcontrol_new usb_connector_ctl_ro = { 1732 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 1733 .name = "", /* will be filled later manually */ 1734 .access = SNDRV_CTL_ELEM_ACCESS_READ, 1735 .info = snd_ctl_boolean_mono_info, 1736 .get = mixer_ctl_connector_get, 1737 .put = NULL, 1738 }; 1739 1740 /* 1741 * This symbol is exported in order to allow the mixer quirks to 1742 * hook up to the standard feature unit control mechanism 1743 */ 1744 const struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl; 1745 1746 /* 1747 * build a feature control 1748 */ 1749 static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str) 1750 { 1751 return strlcat(kctl->id.name, str, sizeof(kctl->id.name)); 1752 } 1753 1754 /* 1755 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we 1756 * rename it to "Headphone". We determine if something is a headphone 1757 * similar to how udev determines form factor. 1758 */ 1759 static void check_no_speaker_on_headset(struct snd_kcontrol *kctl, 1760 struct snd_card *card) 1761 { 1762 static const char * const names_to_check[] = { 1763 "Headset", "headset", "Headphone", "headphone", NULL}; 1764 const char * const *s; 1765 bool found = false; 1766 1767 if (strcmp("Speaker", kctl->id.name)) 1768 return; 1769 1770 for (s = names_to_check; *s; s++) 1771 if (strstr(card->shortname, *s)) { 1772 found = true; 1773 break; 1774 } 1775 1776 if (!found) 1777 return; 1778 1779 snd_ctl_rename(card, kctl, "Headphone"); 1780 } 1781 1782 static const struct usb_feature_control_info *get_feature_control_info(int control) 1783 { 1784 int i; 1785 1786 for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) { 1787 if (audio_feature_info[i].control == control) 1788 return &audio_feature_info[i]; 1789 } 1790 return NULL; 1791 } 1792 1793 static bool check_insane_volume_range(struct usb_mixer_interface *mixer, 1794 struct snd_kcontrol *kctl, 1795 struct usb_mixer_elem_info *cval) 1796 { 1797 int range, steps, threshold; 1798 1799 /* 1800 * If a device quirk has overrode our TLV callback, no warning should 1801 * be generated since our checks are only meaningful for dB volume. 1802 */ 1803 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) || 1804 kctl->tlv.c != snd_usb_mixer_vol_tlv) 1805 return false; 1806 1807 /* 1808 * Meaningless volume control capability (<1dB). This should cover 1809 * devices mapping their volume to val = 0/100/1, which are very likely 1810 * to be quirky. 1811 */ 1812 range = cval->max - cval->min; 1813 if (range < 256) { 1814 usb_audio_warn(mixer->chip, 1815 "Warning! Unlikely small volume range (=%u), linear volume or custom curve?", 1816 range); 1817 return true; 1818 } 1819 1820 steps = range / cval->res; 1821 1822 /* 1823 * There are definitely devices with ~20,000 ranges (e.g., HyperX Cloud 1824 * III with val = -18944/0/1), so we use some heuristics here: 1825 * 1826 * min < 0 < max: Attenuator + amplifier? Likely to be sane 1827 * 1828 * min < 0 = max: DSP? Voltage attenuator with FW conversion to dB? 1829 * Likely to be sane 1830 * 1831 * min < max < 0: Measured values? Neutral 1832 * 1833 * min = 0 < max: Oversimplified FW conversion? Linear volume? Likely to 1834 * be quirky (e.g., MV-SILICON) 1835 * 1836 * 0 < min < max: Amplifier with fixed gains? Likely to be quirky 1837 * (e.g., Logitech webcam) 1838 */ 1839 if (cval->min < 0 && 0 <= cval->max) 1840 threshold = 24576; /* 65535 * (3 / 8) */ 1841 else if (cval->min < cval->max && cval->max < 0) 1842 threshold = 1024; 1843 else 1844 threshold = 384; 1845 1846 if (steps > threshold) { 1847 usb_audio_warn(mixer->chip, 1848 "Warning! Unlikely big volume step count (=%u), linear volume or wrong cval->res?", 1849 steps); 1850 return true; 1851 } 1852 1853 return false; 1854 } 1855 1856 static void __build_feature_ctl(struct usb_mixer_interface *mixer, 1857 const struct usbmix_name_map *imap, 1858 u64 ctl_mask, int control, 1859 struct usb_audio_term *iterm, 1860 struct usb_audio_term *oterm, 1861 int unitid, int nameid, int readonly_mask) 1862 { 1863 const struct usb_feature_control_info *ctl_info; 1864 unsigned int len = 0; 1865 int mapped_name = 0; 1866 struct snd_kcontrol *kctl; 1867 struct usb_mixer_elem_info *cval; 1868 const struct usbmix_name_map *map; 1869 int ret; 1870 1871 if (control == UAC_FU_GRAPHIC_EQUALIZER) { 1872 /* FIXME: not supported yet */ 1873 return; 1874 } 1875 1876 map = find_map(imap, unitid, control); 1877 if (check_ignored_ctl(map)) 1878 return; 1879 1880 cval = kzalloc_obj(*cval); 1881 if (!cval) 1882 return; 1883 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid); 1884 cval->control = control; 1885 cval->cmask = ctl_mask; 1886 1887 ctl_info = get_feature_control_info(control); 1888 if (!ctl_info) { 1889 usb_mixer_elem_info_free(cval); 1890 return; 1891 } 1892 if (mixer->protocol == UAC_VERSION_1) 1893 cval->val_type = ctl_info->type; 1894 else /* UAC_VERSION_2 */ 1895 cval->val_type = ctl_info->type_uac2 >= 0 ? 1896 ctl_info->type_uac2 : ctl_info->type; 1897 1898 if (ctl_mask == 0) { 1899 cval->channels = 1; /* master channel */ 1900 cval->master_readonly = readonly_mask; 1901 } else { 1902 int i, c = 0; 1903 for (i = 0; i < MAX_CHANNELS; i++) 1904 if (ctl_mask & BIT(i)) 1905 c++; 1906 cval->channels = c; 1907 cval->ch_readonly = readonly_mask; 1908 } 1909 1910 /* 1911 * If all channels in the mask are marked read-only, make the control 1912 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't 1913 * issue write commands to read-only channels. 1914 */ 1915 if (cval->channels == readonly_mask) 1916 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval); 1917 else 1918 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); 1919 1920 if (!kctl) { 1921 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n"); 1922 usb_mixer_elem_info_free(cval); 1923 return; 1924 } 1925 kctl->private_free = snd_usb_mixer_elem_free; 1926 1927 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); 1928 mapped_name = len != 0; 1929 if (!len && nameid) 1930 len = snd_usb_copy_string_desc(mixer->chip, nameid, 1931 kctl->id.name, sizeof(kctl->id.name)); 1932 1933 switch (control) { 1934 case UAC_FU_MUTE: 1935 case UAC_FU_VOLUME: 1936 /* 1937 * determine the control name. the rule is: 1938 * - if a name id is given in descriptor, use it. 1939 * - if the connected input can be determined, then use the name 1940 * of terminal type. 1941 * - if the connected output can be determined, use it. 1942 * - otherwise, anonymous name. 1943 */ 1944 if (!len) { 1945 if (iterm) 1946 len = get_term_name(mixer->chip, iterm, 1947 kctl->id.name, 1948 sizeof(kctl->id.name), 1); 1949 if (!len && oterm) 1950 len = get_term_name(mixer->chip, oterm, 1951 kctl->id.name, 1952 sizeof(kctl->id.name), 1); 1953 if (!len) 1954 snprintf(kctl->id.name, sizeof(kctl->id.name), 1955 "Feature %d", unitid); 1956 } 1957 1958 if (!mapped_name) 1959 check_no_speaker_on_headset(kctl, mixer->chip->card); 1960 1961 /* 1962 * determine the stream direction: 1963 * if the connected output is USB stream, then it's likely a 1964 * capture stream. otherwise it should be playback (hopefully :) 1965 */ 1966 if (!mapped_name && oterm && !(oterm->type >> 16)) { 1967 if ((oterm->type & 0xff00) == 0x0100) 1968 append_ctl_name(kctl, " Capture"); 1969 else 1970 append_ctl_name(kctl, " Playback"); 1971 } 1972 append_ctl_name(kctl, control == UAC_FU_MUTE ? 1973 " Switch" : " Volume"); 1974 break; 1975 default: 1976 if (!len) 1977 strscpy(kctl->id.name, audio_feature_info[control-1].name, 1978 sizeof(kctl->id.name)); 1979 break; 1980 } 1981 1982 /* get min/max values */ 1983 ret = get_min_max_with_quirks(cval, 0, kctl); 1984 1985 /* skip a bogus volume range */ 1986 if ((ret < 0 && ret != -EAGAIN) || cval->max <= cval->min) { 1987 usb_audio_dbg(mixer->chip, 1988 "[%d] FU [%s] skipped due to invalid volume\n", 1989 cval->head.id, kctl->id.name); 1990 snd_ctl_free_one(kctl); 1991 return; 1992 } 1993 1994 1995 if (control == UAC_FU_VOLUME) { 1996 check_mapped_dB(map, cval); 1997 if (cval->dBmin < cval->dBmax || !cval->initialized) { 1998 kctl->tlv.c = snd_usb_mixer_vol_tlv; 1999 kctl->vd[0].access |= 2000 SNDRV_CTL_ELEM_ACCESS_TLV_READ | 2001 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; 2002 } 2003 } 2004 2005 snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl); 2006 2007 if (check_insane_volume_range(mixer, kctl, cval)) { 2008 usb_audio_warn(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n", 2009 cval->head.id, kctl->id.name, cval->channels, 2010 cval->min, cval->max, cval->res); 2011 } else { 2012 usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n", 2013 cval->head.id, kctl->id.name, cval->channels, 2014 cval->min, cval->max, cval->res); 2015 } 2016 2017 snd_usb_mixer_add_control(&cval->head, kctl); 2018 } 2019 2020 static void build_feature_ctl(struct mixer_build *state, void *raw_desc, 2021 u64 ctl_mask, int control, 2022 struct usb_audio_term *iterm, int unitid, 2023 int readonly_mask) 2024 { 2025 struct uac_feature_unit_descriptor *desc = raw_desc; 2026 int nameid = uac_feature_unit_iFeature(desc); 2027 2028 __build_feature_ctl(state->mixer, state->map, ctl_mask, control, 2029 iterm, &state->oterm, unitid, nameid, readonly_mask); 2030 } 2031 2032 static void build_feature_ctl_badd(struct usb_mixer_interface *mixer, 2033 u64 ctl_mask, int control, int unitid, 2034 const struct usbmix_name_map *badd_map) 2035 { 2036 __build_feature_ctl(mixer, badd_map, ctl_mask, control, 2037 NULL, NULL, unitid, 0, 0); 2038 } 2039 2040 static void get_connector_control_name(struct usb_mixer_interface *mixer, 2041 struct usb_audio_term *term, 2042 bool is_input, char *name, int name_size) 2043 { 2044 int name_len = get_term_name(mixer->chip, term, name, name_size, 0); 2045 2046 if (name_len == 0) 2047 name_len = strscpy(name, "Unknown", name_size); 2048 if (name_len < 0) 2049 return; 2050 2051 /* 2052 * sound/core/ctljack.c has a convention of naming jack controls 2053 * by ending in " Jack". Make it slightly more useful by 2054 * indicating Input or Output after the terminal name. 2055 */ 2056 if (is_input) 2057 strscpy(name + name_len, " - Input Jack", name_size - name_len); 2058 else 2059 strscpy(name + name_len, " - Output Jack", name_size - name_len); 2060 } 2061 2062 /* get connector value to "wake up" the USB audio */ 2063 static int connector_mixer_resume(struct usb_mixer_elem_list *list) 2064 { 2065 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list); 2066 2067 get_connector_value(cval, NULL, NULL); 2068 return 0; 2069 } 2070 2071 /* Build a mixer control for a UAC connector control (jack-detect) */ 2072 static void build_connector_control(struct usb_mixer_interface *mixer, 2073 const struct usbmix_name_map *imap, 2074 struct usb_audio_term *term, bool is_input) 2075 { 2076 struct snd_kcontrol *kctl; 2077 struct usb_mixer_elem_info *cval; 2078 const struct usbmix_name_map *map; 2079 2080 map = find_map(imap, term->id, 0); 2081 if (check_ignored_ctl(map)) 2082 return; 2083 2084 cval = kzalloc_obj(*cval); 2085 if (!cval) 2086 return; 2087 snd_usb_mixer_elem_init_std(&cval->head, mixer, term->id); 2088 2089 /* set up a specific resume callback */ 2090 cval->head.resume = connector_mixer_resume; 2091 2092 /* 2093 * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the 2094 * number of channels connected. 2095 * 2096 * UAC3: The first byte specifies size of bitmap for the inserted controls. The 2097 * following byte(s) specifies which connectors are inserted. 2098 * 2099 * This boolean ctl will simply report if any channels are connected 2100 * or not. 2101 */ 2102 if (mixer->protocol == UAC_VERSION_2) 2103 cval->control = UAC2_TE_CONNECTOR; 2104 else /* UAC_VERSION_3 */ 2105 cval->control = UAC3_TE_INSERTION; 2106 2107 cval->val_type = USB_MIXER_BOOLEAN; 2108 cval->channels = 1; /* report true if any channel is connected */ 2109 cval->min = 0; 2110 cval->max = 1; 2111 kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval); 2112 if (!kctl) { 2113 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n"); 2114 usb_mixer_elem_info_free(cval); 2115 return; 2116 } 2117 2118 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) 2119 strlcat(kctl->id.name, " Jack", sizeof(kctl->id.name)); 2120 else 2121 get_connector_control_name(mixer, term, is_input, kctl->id.name, 2122 sizeof(kctl->id.name)); 2123 kctl->private_free = snd_usb_mixer_elem_free; 2124 snd_usb_mixer_add_control(&cval->head, kctl); 2125 } 2126 2127 static int parse_clock_source_unit(struct mixer_build *state, int unitid, 2128 void *_ftr) 2129 { 2130 struct uac_clock_source_descriptor *hdr = _ftr; 2131 struct usb_mixer_elem_info *cval; 2132 struct snd_kcontrol *kctl; 2133 int ret; 2134 2135 if (state->mixer->protocol != UAC_VERSION_2) 2136 return -EINVAL; 2137 2138 /* 2139 * The only property of this unit we are interested in is the 2140 * clock source validity. If that isn't readable, just bail out. 2141 */ 2142 if (!uac_v2v3_control_is_readable(hdr->bmControls, 2143 UAC2_CS_CONTROL_CLOCK_VALID)) 2144 return 0; 2145 2146 cval = kzalloc_obj(*cval); 2147 if (!cval) 2148 return -ENOMEM; 2149 2150 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID); 2151 2152 cval->min = 0; 2153 cval->max = 1; 2154 cval->channels = 1; 2155 cval->val_type = USB_MIXER_BOOLEAN; 2156 cval->control = UAC2_CS_CONTROL_CLOCK_VALID; 2157 2158 cval->master_readonly = 1; 2159 /* From UAC2 5.2.5.1.2 "Only the get request is supported." */ 2160 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval); 2161 2162 if (!kctl) { 2163 usb_mixer_elem_info_free(cval); 2164 return -ENOMEM; 2165 } 2166 2167 kctl->private_free = snd_usb_mixer_elem_free; 2168 ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource, 2169 kctl->id.name, sizeof(kctl->id.name)); 2170 if (ret > 0) 2171 append_ctl_name(kctl, " Validity"); 2172 else 2173 snprintf(kctl->id.name, sizeof(kctl->id.name), 2174 "Clock Source %d Validity", hdr->bClockID); 2175 2176 return snd_usb_mixer_add_control(&cval->head, kctl); 2177 } 2178 2179 /* 2180 * parse a feature unit 2181 * 2182 * most of controls are defined here. 2183 */ 2184 static int parse_audio_feature_unit(struct mixer_build *state, int unitid, 2185 void *_ftr) 2186 { 2187 int channels, i, j; 2188 struct usb_audio_term iterm; 2189 unsigned int master_bits; 2190 int err, csize; 2191 struct uac_feature_unit_descriptor *hdr = _ftr; 2192 __u8 *bmaControls; 2193 2194 if (state->mixer->protocol == UAC_VERSION_1) { 2195 csize = hdr->bControlSize; 2196 channels = (hdr->bLength - 7) / csize - 1; 2197 bmaControls = hdr->bmaControls; 2198 } else if (state->mixer->protocol == UAC_VERSION_2) { 2199 struct uac2_feature_unit_descriptor *ftr = _ftr; 2200 csize = 4; 2201 channels = (hdr->bLength - 6) / 4 - 1; 2202 bmaControls = ftr->bmaControls; 2203 } else { /* UAC_VERSION_3 */ 2204 struct uac3_feature_unit_descriptor *ftr = _ftr; 2205 2206 csize = 4; 2207 channels = (ftr->bLength - 7) / 4 - 1; 2208 bmaControls = ftr->bmaControls; 2209 } 2210 2211 if (channels > MAX_CHANNELS) { 2212 usb_audio_info(state->chip, 2213 "usbmixer: too many channels (%d) in unit %d\n", 2214 channels, unitid); 2215 return -EINVAL; 2216 } 2217 2218 /* parse the source unit */ 2219 err = parse_audio_unit(state, hdr->bSourceID); 2220 if (err < 0) 2221 return err; 2222 2223 /* determine the input source type and name */ 2224 err = check_input_term(state, hdr->bSourceID, &iterm); 2225 if (err < 0) 2226 return err; 2227 2228 master_bits = snd_usb_combine_bytes(bmaControls, csize); 2229 /* master configuration quirks */ 2230 switch (state->chip->usb_id) { 2231 case USB_ID(0x08bb, 0x2702): 2232 usb_audio_info(state->chip, 2233 "usbmixer: master volume quirk for PCM2702 chip\n"); 2234 /* disable non-functional volume control */ 2235 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME); 2236 break; 2237 case USB_ID(0x1130, 0xf211): 2238 usb_audio_info(state->chip, 2239 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n"); 2240 /* disable non-functional volume control */ 2241 channels = 0; 2242 break; 2243 2244 } 2245 2246 if (state->mixer->protocol == UAC_VERSION_1) { 2247 /* check all control types */ 2248 for (i = 0; i < 10; i++) { 2249 u64 ch_bits = 0; 2250 int control = audio_feature_info[i].control; 2251 2252 for (j = 0; j < channels; j++) { 2253 unsigned int mask; 2254 2255 mask = snd_usb_combine_bytes(bmaControls + 2256 csize * (j+1), csize); 2257 if (mask & BIT(i)) 2258 ch_bits |= BIT(j); 2259 } 2260 /* audio class v1 controls are never read-only */ 2261 2262 /* 2263 * The first channel must be set 2264 * (for ease of programming). 2265 */ 2266 if (ch_bits & 1) 2267 build_feature_ctl(state, _ftr, ch_bits, control, 2268 &iterm, unitid, 0); 2269 if (master_bits & BIT(i)) 2270 build_feature_ctl(state, _ftr, 0, control, 2271 &iterm, unitid, 0); 2272 } 2273 } else { /* UAC_VERSION_2/3 */ 2274 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) { 2275 u64 ch_bits = 0; 2276 unsigned int ch_read_only = 0; 2277 int control = audio_feature_info[i].control; 2278 2279 for (j = 0; j < channels; j++) { 2280 unsigned int mask; 2281 2282 mask = snd_usb_combine_bytes(bmaControls + 2283 csize * (j+1), csize); 2284 if (uac_v2v3_control_is_readable(mask, control)) { 2285 ch_bits |= BIT(j); 2286 if (!uac_v2v3_control_is_writeable(mask, control)) 2287 ch_read_only |= BIT(j); 2288 } 2289 } 2290 2291 /* 2292 * NOTE: build_feature_ctl() will mark the control 2293 * read-only if all channels are marked read-only in 2294 * the descriptors. Otherwise, the control will be 2295 * reported as writeable, but the driver will not 2296 * actually issue a write command for read-only 2297 * channels. 2298 */ 2299 2300 /* 2301 * The first channel must be set 2302 * (for ease of programming). 2303 */ 2304 if (ch_bits & 1) 2305 build_feature_ctl(state, _ftr, ch_bits, control, 2306 &iterm, unitid, ch_read_only); 2307 if (uac_v2v3_control_is_readable(master_bits, control)) 2308 build_feature_ctl(state, _ftr, 0, control, 2309 &iterm, unitid, 2310 !uac_v2v3_control_is_writeable(master_bits, 2311 control)); 2312 } 2313 } 2314 2315 return 0; 2316 } 2317 2318 /* 2319 * Mixer Unit 2320 */ 2321 2322 /* check whether the given in/out overflows bmMixerControls matrix */ 2323 static bool mixer_bitmap_overflow(struct uac_mixer_unit_descriptor *desc, 2324 int protocol, int num_ins, int num_outs) 2325 { 2326 u8 *hdr = (u8 *)desc; 2327 u8 *c = uac_mixer_unit_bmControls(desc, protocol); 2328 size_t rest; /* remaining bytes after bmMixerControls */ 2329 2330 switch (protocol) { 2331 case UAC_VERSION_1: 2332 default: 2333 rest = 1; /* iMixer */ 2334 break; 2335 case UAC_VERSION_2: 2336 rest = 2; /* bmControls + iMixer */ 2337 break; 2338 case UAC_VERSION_3: 2339 rest = 6; /* bmControls + wMixerDescrStr */ 2340 break; 2341 } 2342 2343 /* overflow? */ 2344 return c + (num_ins * num_outs + 7) / 8 + rest > hdr + hdr[0]; 2345 } 2346 2347 /* 2348 * build a mixer unit control 2349 * 2350 * the callbacks are identical with feature unit. 2351 * input channel number (zero based) is given in control field instead. 2352 */ 2353 static void build_mixer_unit_ctl(struct mixer_build *state, 2354 struct uac_mixer_unit_descriptor *desc, 2355 int in_pin, int in_ch, int num_outs, 2356 int unitid, struct usb_audio_term *iterm) 2357 { 2358 struct usb_mixer_elem_info *cval; 2359 unsigned int i, len; 2360 struct snd_kcontrol *kctl; 2361 const struct usbmix_name_map *map; 2362 int ret; 2363 2364 map = find_map(state->map, unitid, 0); 2365 if (check_ignored_ctl(map)) 2366 return; 2367 2368 cval = kzalloc_obj(*cval); 2369 if (!cval) 2370 return; 2371 2372 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); 2373 cval->control = in_ch + 1; /* based on 1 */ 2374 cval->val_type = USB_MIXER_S16; 2375 for (i = 0; i < num_outs; i++) { 2376 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol); 2377 2378 if (check_matrix_bitmap(c, in_ch, i, num_outs)) { 2379 cval->cmask |= BIT(i); 2380 cval->channels++; 2381 } 2382 } 2383 2384 /* get min/max values */ 2385 ret = get_min_max(cval, 0); 2386 if (ret < 0 && ret != -EAGAIN) { 2387 usb_mixer_elem_info_free(cval); 2388 return; 2389 } 2390 2391 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); 2392 if (!kctl) { 2393 usb_audio_err(state->chip, "cannot malloc kcontrol\n"); 2394 usb_mixer_elem_info_free(cval); 2395 return; 2396 } 2397 kctl->private_free = snd_usb_mixer_elem_free; 2398 2399 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); 2400 if (!len) 2401 len = get_term_name(state->chip, iterm, kctl->id.name, 2402 sizeof(kctl->id.name), 0); 2403 if (!len) 2404 snprintf(kctl->id.name, sizeof(kctl->id.name), "Mixer Source %d", in_ch + 1); 2405 2406 append_ctl_name(kctl, " Volume"); 2407 2408 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n", 2409 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max); 2410 snd_usb_mixer_add_control(&cval->head, kctl); 2411 } 2412 2413 static int parse_audio_input_terminal(struct mixer_build *state, int unitid, 2414 void *raw_desc) 2415 { 2416 struct usb_audio_term iterm; 2417 unsigned int control, bmctls, term_id; 2418 2419 if (state->mixer->protocol == UAC_VERSION_2) { 2420 struct uac2_input_terminal_descriptor *d_v2 = raw_desc; 2421 control = UAC2_TE_CONNECTOR; 2422 term_id = d_v2->bTerminalID; 2423 bmctls = le16_to_cpu(d_v2->bmControls); 2424 } else if (state->mixer->protocol == UAC_VERSION_3) { 2425 struct uac3_input_terminal_descriptor *d_v3 = raw_desc; 2426 control = UAC3_TE_INSERTION; 2427 term_id = d_v3->bTerminalID; 2428 bmctls = le32_to_cpu(d_v3->bmControls); 2429 } else { 2430 return 0; /* UAC1. No Insertion control */ 2431 } 2432 2433 check_input_term(state, term_id, &iterm); 2434 2435 /* Check for jack detection. */ 2436 if ((iterm.type & 0xff00) != 0x0100 && 2437 uac_v2v3_control_is_readable(bmctls, control)) 2438 build_connector_control(state->mixer, state->map, &iterm, true); 2439 2440 return 0; 2441 } 2442 2443 /* 2444 * parse a mixer unit 2445 */ 2446 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, 2447 void *raw_desc) 2448 { 2449 struct uac_mixer_unit_descriptor *desc = raw_desc; 2450 struct usb_audio_term iterm; 2451 int input_pins, num_ins, num_outs; 2452 int pin, ich, err; 2453 2454 err = uac_mixer_unit_get_channels(state, desc); 2455 if (err < 0) { 2456 usb_audio_err(state->chip, 2457 "invalid MIXER UNIT descriptor %d\n", 2458 unitid); 2459 return err; 2460 } 2461 2462 num_outs = err; 2463 input_pins = desc->bNrInPins; 2464 2465 num_ins = 0; 2466 ich = 0; 2467 for (pin = 0; pin < input_pins; pin++) { 2468 err = parse_audio_unit(state, desc->baSourceID[pin]); 2469 if (err < 0) 2470 continue; 2471 /* no bmControls field (e.g. Maya44) -> ignore */ 2472 if (!num_outs) 2473 continue; 2474 err = check_input_term(state, desc->baSourceID[pin], &iterm); 2475 if (err < 0) 2476 return err; 2477 num_ins += iterm.channels; 2478 if (mixer_bitmap_overflow(desc, state->mixer->protocol, 2479 num_ins, num_outs)) 2480 break; 2481 for (; ich < num_ins; ich++) { 2482 int och, ich_has_controls = 0; 2483 2484 for (och = 0; och < num_outs; och++) { 2485 __u8 *c = uac_mixer_unit_bmControls(desc, 2486 state->mixer->protocol); 2487 2488 if (check_matrix_bitmap(c, ich, och, num_outs)) { 2489 ich_has_controls = 1; 2490 break; 2491 } 2492 } 2493 if (ich_has_controls) 2494 build_mixer_unit_ctl(state, desc, pin, ich, num_outs, 2495 unitid, &iterm); 2496 } 2497 } 2498 return 0; 2499 } 2500 2501 /* 2502 * Processing Unit / Extension Unit 2503 */ 2504 2505 /* get callback for processing/extension unit */ 2506 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, 2507 struct snd_ctl_elem_value *ucontrol) 2508 { 2509 struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); 2510 int err, val; 2511 2512 err = get_cur_ctl_value(cval, cval->control << 8, &val); 2513 if (err < 0) { 2514 ucontrol->value.integer.value[0] = cval->min; 2515 return filter_error(cval, err); 2516 } 2517 val = get_relative_value(cval, val); 2518 ucontrol->value.integer.value[0] = val; 2519 return 0; 2520 } 2521 2522 /* put callback for processing/extension unit */ 2523 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, 2524 struct snd_ctl_elem_value *ucontrol) 2525 { 2526 struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); 2527 int val, oval, err; 2528 2529 err = get_cur_ctl_value(cval, cval->control << 8, &oval); 2530 if (err < 0) 2531 return filter_error(cval, err); 2532 val = ucontrol->value.integer.value[0]; 2533 if (val < 0 || val > get_max_exposed(cval)) 2534 return -EINVAL; 2535 val = get_abs_value(cval, val); 2536 if (val != oval) { 2537 err = set_cur_ctl_value(cval, cval->control << 8, val); 2538 if (err < 0) 2539 return filter_error(cval, err); 2540 return 1; 2541 } 2542 return 0; 2543 } 2544 2545 /* alsa control interface for processing/extension unit */ 2546 static const struct snd_kcontrol_new mixer_procunit_ctl = { 2547 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2548 .name = "", /* will be filled later */ 2549 .info = mixer_ctl_feature_info, 2550 .get = mixer_ctl_procunit_get, 2551 .put = mixer_ctl_procunit_put, 2552 }; 2553 2554 /* 2555 * predefined data for processing units 2556 */ 2557 struct procunit_value_info { 2558 int control; 2559 const char *suffix; 2560 int val_type; 2561 int min_value; 2562 }; 2563 2564 struct procunit_info { 2565 int type; 2566 char *name; 2567 const struct procunit_value_info *values; 2568 }; 2569 2570 static const struct procunit_value_info undefined_proc_info[] = { 2571 { 0x00, "Control Undefined", 0 }, 2572 { 0 } 2573 }; 2574 2575 static const struct procunit_value_info updown_proc_info[] = { 2576 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN }, 2577 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, 2578 { 0 } 2579 }; 2580 static const struct procunit_value_info prologic_proc_info[] = { 2581 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN }, 2582 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, 2583 { 0 } 2584 }; 2585 static const struct procunit_value_info threed_enh_proc_info[] = { 2586 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN }, 2587 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 }, 2588 { 0 } 2589 }; 2590 static const struct procunit_value_info reverb_proc_info[] = { 2591 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN }, 2592 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 }, 2593 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 }, 2594 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 }, 2595 { 0 } 2596 }; 2597 static const struct procunit_value_info chorus_proc_info[] = { 2598 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN }, 2599 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 }, 2600 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 }, 2601 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 }, 2602 { 0 } 2603 }; 2604 static const struct procunit_value_info dcr_proc_info[] = { 2605 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN }, 2606 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 }, 2607 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 }, 2608 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 }, 2609 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 }, 2610 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 }, 2611 { 0 } 2612 }; 2613 2614 static const struct procunit_info procunits[] = { 2615 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info }, 2616 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info }, 2617 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info }, 2618 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info }, 2619 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info }, 2620 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info }, 2621 { 0 }, 2622 }; 2623 2624 static const struct procunit_value_info uac3_updown_proc_info[] = { 2625 { UAC3_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, 2626 { 0 } 2627 }; 2628 static const struct procunit_value_info uac3_stereo_ext_proc_info[] = { 2629 { UAC3_EXT_WIDTH_CONTROL, "Width Control", USB_MIXER_U8 }, 2630 { 0 } 2631 }; 2632 2633 static const struct procunit_info uac3_procunits[] = { 2634 { UAC3_PROCESS_UP_DOWNMIX, "Up Down", uac3_updown_proc_info }, 2635 { UAC3_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", uac3_stereo_ext_proc_info }, 2636 { UAC3_PROCESS_MULTI_FUNCTION, "Multi-Function", undefined_proc_info }, 2637 { 0 }, 2638 }; 2639 2640 /* 2641 * predefined data for extension units 2642 */ 2643 static const struct procunit_value_info clock_rate_xu_info[] = { 2644 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 }, 2645 { 0 } 2646 }; 2647 static const struct procunit_value_info clock_source_xu_info[] = { 2648 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN }, 2649 { 0 } 2650 }; 2651 static const struct procunit_value_info spdif_format_xu_info[] = { 2652 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN }, 2653 { 0 } 2654 }; 2655 static const struct procunit_value_info soft_limit_xu_info[] = { 2656 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN }, 2657 { 0 } 2658 }; 2659 static const struct procunit_info extunits[] = { 2660 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info }, 2661 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info }, 2662 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info }, 2663 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info }, 2664 { 0 } 2665 }; 2666 2667 /* 2668 * build a processing/extension unit 2669 */ 2670 static int build_audio_procunit(struct mixer_build *state, int unitid, 2671 void *raw_desc, const struct procunit_info *list, 2672 bool extension_unit) 2673 { 2674 struct uac_processing_unit_descriptor *desc = raw_desc; 2675 int num_ins; 2676 struct usb_mixer_elem_info *cval; 2677 struct snd_kcontrol *kctl; 2678 int i, err, nameid, type, len, val; 2679 const struct procunit_info *info; 2680 const struct procunit_value_info *valinfo; 2681 const struct usbmix_name_map *map; 2682 static const struct procunit_value_info default_value_info[] = { 2683 { 0x01, "Switch", USB_MIXER_BOOLEAN }, 2684 { 0 } 2685 }; 2686 static const struct procunit_info default_info = { 2687 0, NULL, default_value_info 2688 }; 2689 const char *name = extension_unit ? 2690 "Extension Unit" : "Processing Unit"; 2691 2692 num_ins = desc->bNrInPins; 2693 for (i = 0; i < num_ins; i++) { 2694 err = parse_audio_unit(state, desc->baSourceID[i]); 2695 if (err < 0) 2696 return err; 2697 } 2698 2699 type = le16_to_cpu(desc->wProcessType); 2700 for (info = list; info && info->type; info++) 2701 if (info->type == type) 2702 break; 2703 if (!info || !info->type) 2704 info = &default_info; 2705 2706 for (valinfo = info->values; valinfo->control; valinfo++) { 2707 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol); 2708 2709 if (state->mixer->protocol == UAC_VERSION_1) { 2710 if (!(controls[valinfo->control / 8] & 2711 BIT((valinfo->control % 8) - 1))) 2712 continue; 2713 } else { /* UAC_VERSION_2/3 */ 2714 if (!uac_v2v3_control_is_readable(controls[valinfo->control / 8], 2715 valinfo->control)) 2716 continue; 2717 } 2718 2719 map = find_map(state->map, unitid, valinfo->control); 2720 if (check_ignored_ctl(map)) 2721 continue; 2722 cval = kzalloc_obj(*cval); 2723 if (!cval) 2724 return -ENOMEM; 2725 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); 2726 cval->control = valinfo->control; 2727 cval->val_type = valinfo->val_type; 2728 cval->channels = 1; 2729 2730 if (state->mixer->protocol > UAC_VERSION_1 && 2731 !uac_v2v3_control_is_writeable(controls[valinfo->control / 8], 2732 valinfo->control)) 2733 cval->master_readonly = 1; 2734 2735 /* get min/max values */ 2736 switch (type) { 2737 case USB_XU_CLOCK_RATE: 2738 /* 2739 * E-Mu USB 0404/0202/TrackerPre/0204 2740 * samplerate control quirk 2741 */ 2742 cval->min = 0; 2743 cval->max = 5; 2744 cval->res = 1; 2745 cval->initialized = 1; 2746 break; 2747 case UAC_PROCESS_UP_DOWNMIX: { 2748 bool mode_sel = false; 2749 2750 switch (state->mixer->protocol) { 2751 case UAC_VERSION_1: 2752 case UAC_VERSION_2: 2753 default: 2754 if (cval->control == UAC_UD_MODE_SELECT) 2755 mode_sel = true; 2756 break; 2757 case UAC_VERSION_3: 2758 if (cval->control == UAC3_UD_MODE_SELECT) 2759 mode_sel = true; 2760 break; 2761 } 2762 2763 if (mode_sel) { 2764 __u8 *control_spec = uac_processing_unit_specific(desc, 2765 state->mixer->protocol); 2766 cval->min = 1; 2767 cval->max = control_spec[0]; 2768 cval->res = 1; 2769 cval->initialized = 1; 2770 break; 2771 } 2772 2773 fallthrough; 2774 } 2775 default: 2776 err = get_min_max(cval, valinfo->min_value); 2777 if (err < 0 && err != -EAGAIN) { 2778 usb_mixer_elem_info_free(cval); 2779 return err; 2780 } 2781 } 2782 2783 err = get_cur_ctl_value(cval, cval->control << 8, &val); 2784 if (err < 0) { 2785 usb_mixer_elem_info_free(cval); 2786 return -EINVAL; 2787 } 2788 2789 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval); 2790 if (!kctl) { 2791 usb_mixer_elem_info_free(cval); 2792 return -ENOMEM; 2793 } 2794 kctl->private_free = snd_usb_mixer_elem_free; 2795 2796 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) { 2797 /* nothing */ ; 2798 } else if (info->name) { 2799 strscpy(kctl->id.name, info->name, sizeof(kctl->id.name)); 2800 } else { 2801 if (extension_unit) 2802 nameid = uac_extension_unit_iExtension(desc, state->mixer->protocol); 2803 else 2804 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol); 2805 len = 0; 2806 if (nameid) 2807 len = snd_usb_copy_string_desc(state->chip, 2808 nameid, 2809 kctl->id.name, 2810 sizeof(kctl->id.name)); 2811 if (!len) 2812 strscpy(kctl->id.name, name, sizeof(kctl->id.name)); 2813 } 2814 append_ctl_name(kctl, " "); 2815 append_ctl_name(kctl, valinfo->suffix); 2816 2817 usb_audio_dbg(state->chip, 2818 "[%d] PU [%s] ch = %d, val = %d/%d\n", 2819 cval->head.id, kctl->id.name, cval->channels, 2820 cval->min, cval->max); 2821 2822 err = snd_usb_mixer_add_control(&cval->head, kctl); 2823 if (err < 0) 2824 return err; 2825 } 2826 return 0; 2827 } 2828 2829 static int parse_audio_processing_unit(struct mixer_build *state, int unitid, 2830 void *raw_desc) 2831 { 2832 switch (state->mixer->protocol) { 2833 case UAC_VERSION_1: 2834 case UAC_VERSION_2: 2835 default: 2836 return build_audio_procunit(state, unitid, raw_desc, 2837 procunits, false); 2838 case UAC_VERSION_3: 2839 return build_audio_procunit(state, unitid, raw_desc, 2840 uac3_procunits, false); 2841 } 2842 } 2843 2844 static int parse_audio_extension_unit(struct mixer_build *state, int unitid, 2845 void *raw_desc) 2846 { 2847 /* 2848 * Note that we parse extension units with processing unit descriptors. 2849 * That's ok as the layout is the same. 2850 */ 2851 return build_audio_procunit(state, unitid, raw_desc, extunits, true); 2852 } 2853 2854 /* 2855 * Selector Unit 2856 */ 2857 2858 /* 2859 * info callback for selector unit 2860 * use an enumerator type for routing 2861 */ 2862 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, 2863 struct snd_ctl_elem_info *uinfo) 2864 { 2865 struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); 2866 const char **itemlist = (const char **)kcontrol->private_value; 2867 2868 if (snd_BUG_ON(!itemlist)) 2869 return -EINVAL; 2870 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist); 2871 } 2872 2873 /* get callback for selector unit */ 2874 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, 2875 struct snd_ctl_elem_value *ucontrol) 2876 { 2877 struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); 2878 int val, err; 2879 2880 err = get_cur_ctl_value(cval, cval->control << 8, &val); 2881 if (err < 0) { 2882 ucontrol->value.enumerated.item[0] = 0; 2883 return filter_error(cval, err); 2884 } 2885 val = get_relative_value(cval, val); 2886 ucontrol->value.enumerated.item[0] = val; 2887 return 0; 2888 } 2889 2890 /* put callback for selector unit */ 2891 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, 2892 struct snd_ctl_elem_value *ucontrol) 2893 { 2894 struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol); 2895 int val, oval, err; 2896 2897 err = get_cur_ctl_value(cval, cval->control << 8, &oval); 2898 if (err < 0) 2899 return filter_error(cval, err); 2900 val = ucontrol->value.enumerated.item[0]; 2901 if (val < 0 || val >= cval->max) /* here cval->max = # elements */ 2902 return -EINVAL; 2903 val = get_abs_value(cval, val); 2904 if (val != oval) { 2905 err = set_cur_ctl_value(cval, cval->control << 8, val); 2906 if (err < 0) 2907 return filter_error(cval, err); 2908 return 1; 2909 } 2910 return 0; 2911 } 2912 2913 /* alsa control interface for selector unit */ 2914 static const struct snd_kcontrol_new mixer_selectunit_ctl = { 2915 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2916 .name = "", /* will be filled later */ 2917 .info = mixer_ctl_selector_info, 2918 .get = mixer_ctl_selector_get, 2919 .put = mixer_ctl_selector_put, 2920 }; 2921 2922 /* 2923 * private free callback. 2924 * free both private_data and private_value 2925 */ 2926 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl) 2927 { 2928 int i, num_ins = 0; 2929 2930 if (kctl->private_data) { 2931 struct usb_mixer_elem_info *cval = kctl->private_data; 2932 num_ins = cval->max; 2933 usb_mixer_elem_info_free(cval); 2934 kctl->private_data = NULL; 2935 } 2936 if (kctl->private_value) { 2937 char **itemlist = (char **)kctl->private_value; 2938 for (i = 0; i < num_ins; i++) 2939 kfree(itemlist[i]); 2940 kfree(itemlist); 2941 kctl->private_value = 0; 2942 } 2943 } 2944 2945 /* 2946 * parse a selector unit 2947 */ 2948 static int parse_audio_selector_unit(struct mixer_build *state, int unitid, 2949 void *raw_desc) 2950 { 2951 struct uac_selector_unit_descriptor *desc = raw_desc; 2952 unsigned int i, nameid, len; 2953 int err; 2954 struct usb_mixer_elem_info *cval; 2955 struct snd_kcontrol *kctl; 2956 const struct usbmix_name_map *map; 2957 char **namelist; 2958 2959 for (i = 0; i < desc->bNrInPins; i++) { 2960 err = parse_audio_unit(state, desc->baSourceID[i]); 2961 if (err < 0) 2962 return err; 2963 } 2964 2965 if (desc->bNrInPins == 1) /* only one ? nonsense! */ 2966 return 0; 2967 2968 map = find_map(state->map, unitid, 0); 2969 if (check_ignored_ctl(map)) 2970 return 0; 2971 2972 cval = kzalloc_obj(*cval); 2973 if (!cval) 2974 return -ENOMEM; 2975 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); 2976 cval->val_type = USB_MIXER_U8; 2977 cval->channels = 1; 2978 cval->min = 1; 2979 cval->max = desc->bNrInPins; 2980 cval->res = 1; 2981 cval->initialized = 1; 2982 2983 switch (state->mixer->protocol) { 2984 case UAC_VERSION_1: 2985 default: 2986 cval->control = 0; 2987 break; 2988 case UAC_VERSION_2: 2989 case UAC_VERSION_3: 2990 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR || 2991 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR) 2992 cval->control = UAC2_CX_CLOCK_SELECTOR; 2993 else /* UAC2/3_SELECTOR_UNIT */ 2994 cval->control = UAC2_SU_SELECTOR; 2995 break; 2996 } 2997 2998 namelist = kcalloc(desc->bNrInPins, sizeof(char *), GFP_KERNEL); 2999 if (!namelist) { 3000 err = -ENOMEM; 3001 goto error_cval; 3002 } 3003 #define MAX_ITEM_NAME_LEN 64 3004 for (i = 0; i < desc->bNrInPins; i++) { 3005 struct usb_audio_term iterm; 3006 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL); 3007 if (!namelist[i]) { 3008 err = -ENOMEM; 3009 goto error_name; 3010 } 3011 len = check_mapped_selector_name(state, unitid, i, namelist[i], 3012 MAX_ITEM_NAME_LEN); 3013 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0) 3014 len = get_term_name(state->chip, &iterm, namelist[i], 3015 MAX_ITEM_NAME_LEN, 0); 3016 if (! len) 3017 scnprintf(namelist[i], MAX_ITEM_NAME_LEN, "Input %u", i); 3018 } 3019 3020 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval); 3021 if (! kctl) { 3022 usb_audio_err(state->chip, "cannot malloc kcontrol\n"); 3023 err = -ENOMEM; 3024 goto error_name; 3025 } 3026 kctl->private_value = (unsigned long)namelist; 3027 kctl->private_free = usb_mixer_selector_elem_free; 3028 3029 /* check the static mapping table at first */ 3030 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); 3031 if (!len) { 3032 /* no mapping ? */ 3033 switch (state->mixer->protocol) { 3034 case UAC_VERSION_1: 3035 case UAC_VERSION_2: 3036 default: 3037 /* if iSelector is given, use it */ 3038 nameid = uac_selector_unit_iSelector(desc); 3039 if (nameid) 3040 len = snd_usb_copy_string_desc(state->chip, 3041 nameid, kctl->id.name, 3042 sizeof(kctl->id.name)); 3043 break; 3044 case UAC_VERSION_3: 3045 /* TODO: Class-Specific strings not yet supported */ 3046 break; 3047 } 3048 3049 /* ... or pick up the terminal name at next */ 3050 if (!len) 3051 len = get_term_name(state->chip, &state->oterm, 3052 kctl->id.name, sizeof(kctl->id.name), 0); 3053 /* ... or use the fixed string "USB" as the last resort */ 3054 if (!len) 3055 strscpy(kctl->id.name, "USB", sizeof(kctl->id.name)); 3056 3057 /* and add the proper suffix */ 3058 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR || 3059 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR) 3060 append_ctl_name(kctl, " Clock Source"); 3061 else if ((state->oterm.type & 0xff00) == 0x0100) 3062 append_ctl_name(kctl, " Capture Source"); 3063 else 3064 append_ctl_name(kctl, " Playback Source"); 3065 } 3066 3067 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n", 3068 cval->head.id, kctl->id.name, desc->bNrInPins); 3069 return snd_usb_mixer_add_control(&cval->head, kctl); 3070 3071 error_name: 3072 for (i = 0; i < desc->bNrInPins; i++) 3073 kfree(namelist[i]); 3074 kfree(namelist); 3075 error_cval: 3076 usb_mixer_elem_info_free(cval); 3077 return err; 3078 } 3079 3080 /* 3081 * parse an audio unit recursively 3082 */ 3083 3084 static int parse_audio_unit(struct mixer_build *state, int unitid) 3085 { 3086 unsigned char *p1; 3087 int protocol = state->mixer->protocol; 3088 3089 if (test_and_set_bit(unitid, state->unitbitmap)) 3090 return 0; /* the unit already visited */ 3091 3092 p1 = find_audio_control_unit(state, unitid); 3093 if (!p1) { 3094 usb_audio_err(state->chip, "unit %d not found!\n", unitid); 3095 return -EINVAL; 3096 } 3097 3098 if (!snd_usb_validate_audio_desc(p1, protocol)) { 3099 usb_audio_dbg(state->chip, "invalid unit %d\n", unitid); 3100 return 0; /* skip invalid unit */ 3101 } 3102 3103 switch (PTYPE(protocol, p1[2])) { 3104 case PTYPE(UAC_VERSION_1, UAC_INPUT_TERMINAL): 3105 case PTYPE(UAC_VERSION_2, UAC_INPUT_TERMINAL): 3106 case PTYPE(UAC_VERSION_3, UAC_INPUT_TERMINAL): 3107 return parse_audio_input_terminal(state, unitid, p1); 3108 case PTYPE(UAC_VERSION_1, UAC_MIXER_UNIT): 3109 case PTYPE(UAC_VERSION_2, UAC_MIXER_UNIT): 3110 case PTYPE(UAC_VERSION_3, UAC3_MIXER_UNIT): 3111 return parse_audio_mixer_unit(state, unitid, p1); 3112 case PTYPE(UAC_VERSION_2, UAC2_CLOCK_SOURCE): 3113 case PTYPE(UAC_VERSION_3, UAC3_CLOCK_SOURCE): 3114 return parse_clock_source_unit(state, unitid, p1); 3115 case PTYPE(UAC_VERSION_1, UAC_SELECTOR_UNIT): 3116 case PTYPE(UAC_VERSION_2, UAC_SELECTOR_UNIT): 3117 case PTYPE(UAC_VERSION_3, UAC3_SELECTOR_UNIT): 3118 case PTYPE(UAC_VERSION_2, UAC2_CLOCK_SELECTOR): 3119 case PTYPE(UAC_VERSION_3, UAC3_CLOCK_SELECTOR): 3120 return parse_audio_selector_unit(state, unitid, p1); 3121 case PTYPE(UAC_VERSION_1, UAC_FEATURE_UNIT): 3122 case PTYPE(UAC_VERSION_2, UAC_FEATURE_UNIT): 3123 case PTYPE(UAC_VERSION_3, UAC3_FEATURE_UNIT): 3124 return parse_audio_feature_unit(state, unitid, p1); 3125 case PTYPE(UAC_VERSION_1, UAC1_PROCESSING_UNIT): 3126 case PTYPE(UAC_VERSION_2, UAC2_PROCESSING_UNIT_V2): 3127 case PTYPE(UAC_VERSION_3, UAC3_PROCESSING_UNIT): 3128 return parse_audio_processing_unit(state, unitid, p1); 3129 case PTYPE(UAC_VERSION_1, UAC1_EXTENSION_UNIT): 3130 case PTYPE(UAC_VERSION_2, UAC2_EXTENSION_UNIT_V2): 3131 case PTYPE(UAC_VERSION_3, UAC3_EXTENSION_UNIT): 3132 return parse_audio_extension_unit(state, unitid, p1); 3133 case PTYPE(UAC_VERSION_2, UAC2_EFFECT_UNIT): 3134 case PTYPE(UAC_VERSION_3, UAC3_EFFECT_UNIT): 3135 return 0; /* FIXME - effect units not implemented yet */ 3136 default: 3137 usb_audio_err(state->chip, 3138 "unit %u: unexpected type 0x%02x\n", 3139 unitid, p1[2]); 3140 return -EINVAL; 3141 } 3142 } 3143 3144 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer) 3145 { 3146 struct usb_mixer_elem_list *list, *next; 3147 int id; 3148 3149 /* kill pending URBs */ 3150 snd_usb_mixer_disconnect(mixer); 3151 3152 /* Unregister controls first, snd_ctl_remove() frees the element */ 3153 if (mixer->id_elems) { 3154 for (id = 0; id < MAX_ID_ELEMS; id++) { 3155 for (list = mixer->id_elems[id]; list; list = next) { 3156 next = list->next_id_elem; 3157 if (list->kctl) 3158 snd_ctl_remove(mixer->chip->card, list->kctl); 3159 } 3160 } 3161 kfree(mixer->id_elems); 3162 } 3163 if (mixer->urb) { 3164 kfree(mixer->urb->transfer_buffer); 3165 usb_free_urb(mixer->urb); 3166 } 3167 usb_free_urb(mixer->rc_urb); 3168 kfree(mixer->rc_setup_packet); 3169 kfree(mixer); 3170 } 3171 3172 static int snd_usb_mixer_dev_free(struct snd_device *device) 3173 { 3174 struct usb_mixer_interface *mixer = device->device_data; 3175 snd_usb_mixer_free(mixer); 3176 return 0; 3177 } 3178 3179 /* UAC3 predefined channels configuration */ 3180 struct uac3_badd_profile { 3181 int subclass; 3182 const char *name; 3183 int c_chmask; /* capture channels mask */ 3184 int p_chmask; /* playback channels mask */ 3185 int st_chmask; /* side tone mixing channel mask */ 3186 }; 3187 3188 static const struct uac3_badd_profile uac3_badd_profiles[] = { 3189 { 3190 /* 3191 * BAIF, BAOF or combination of both 3192 * IN: Mono or Stereo cfg, Mono alt possible 3193 * OUT: Mono or Stereo cfg, Mono alt possible 3194 */ 3195 .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO, 3196 .name = "GENERIC IO", 3197 .c_chmask = -1, /* dynamic channels */ 3198 .p_chmask = -1, /* dynamic channels */ 3199 }, 3200 { 3201 /* BAOF; Stereo only cfg, Mono alt possible */ 3202 .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE, 3203 .name = "HEADPHONE", 3204 .p_chmask = 3, 3205 }, 3206 { 3207 /* BAOF; Mono or Stereo cfg, Mono alt possible */ 3208 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER, 3209 .name = "SPEAKER", 3210 .p_chmask = -1, /* dynamic channels */ 3211 }, 3212 { 3213 /* BAIF; Mono or Stereo cfg, Mono alt possible */ 3214 .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE, 3215 .name = "MICROPHONE", 3216 .c_chmask = -1, /* dynamic channels */ 3217 }, 3218 { 3219 /* 3220 * BAIOF topology 3221 * IN: Mono only 3222 * OUT: Mono or Stereo cfg, Mono alt possible 3223 */ 3224 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET, 3225 .name = "HEADSET", 3226 .c_chmask = 1, 3227 .p_chmask = -1, /* dynamic channels */ 3228 .st_chmask = 1, 3229 }, 3230 { 3231 /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */ 3232 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER, 3233 .name = "HEADSET ADAPTER", 3234 .c_chmask = 1, 3235 .p_chmask = 3, 3236 .st_chmask = 1, 3237 }, 3238 { 3239 /* BAIF + BAOF; IN: Mono only; OUT: Mono only */ 3240 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE, 3241 .name = "SPEAKERPHONE", 3242 .c_chmask = 1, 3243 .p_chmask = 1, 3244 }, 3245 { 0 } /* terminator */ 3246 }; 3247 3248 static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer, 3249 const struct uac3_badd_profile *f, 3250 int c_chmask, int p_chmask) 3251 { 3252 /* 3253 * If both playback/capture channels are dynamic, make sure 3254 * at least one channel is present 3255 */ 3256 if (f->c_chmask < 0 && f->p_chmask < 0) { 3257 if (!c_chmask && !p_chmask) { 3258 usb_audio_warn(mixer->chip, "BAAD %s: no channels?", 3259 f->name); 3260 return false; 3261 } 3262 return true; 3263 } 3264 3265 if ((f->c_chmask < 0 && !c_chmask) || 3266 (f->c_chmask >= 0 && f->c_chmask != c_chmask)) { 3267 usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch", 3268 f->name); 3269 return false; 3270 } 3271 if ((f->p_chmask < 0 && !p_chmask) || 3272 (f->p_chmask >= 0 && f->p_chmask != p_chmask)) { 3273 usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch", 3274 f->name); 3275 return false; 3276 } 3277 return true; 3278 } 3279 3280 /* 3281 * create mixer controls for UAC3 BADD profiles 3282 * 3283 * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything 3284 * 3285 * BADD device may contain Mixer Unit, which doesn't have any controls, skip it 3286 */ 3287 static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer, 3288 int ctrlif) 3289 { 3290 struct usb_device *dev = mixer->chip->dev; 3291 struct usb_interface_assoc_descriptor *assoc; 3292 int badd_profile = mixer->chip->badd_profile; 3293 const struct uac3_badd_profile *f; 3294 const struct usbmix_ctl_map *map; 3295 int p_chmask = 0, c_chmask = 0, st_chmask = 0; 3296 int i; 3297 3298 assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc; 3299 if (!assoc) 3300 return -EINVAL; 3301 3302 /* Detect BADD capture/playback channels from AS EP descriptors */ 3303 for (i = 0; i < assoc->bInterfaceCount; i++) { 3304 int intf = assoc->bFirstInterface + i; 3305 3306 struct usb_interface *iface; 3307 struct usb_host_interface *alts; 3308 struct usb_interface_descriptor *altsd; 3309 unsigned int maxpacksize; 3310 char dir_in; 3311 int chmask, num; 3312 3313 if (intf == ctrlif) 3314 continue; 3315 3316 iface = usb_ifnum_to_if(dev, intf); 3317 if (!iface) 3318 continue; 3319 3320 num = iface->num_altsetting; 3321 3322 if (num < 2) 3323 return -EINVAL; 3324 3325 /* 3326 * The number of Channels in an AudioStreaming interface 3327 * and the audio sample bit resolution (16 bits or 24 3328 * bits) can be derived from the wMaxPacketSize field in 3329 * the Standard AS Audio Data Endpoint descriptor in 3330 * Alternate Setting 1 3331 */ 3332 alts = &iface->altsetting[1]; 3333 altsd = get_iface_desc(alts); 3334 3335 if (altsd->bNumEndpoints < 1) 3336 return -EINVAL; 3337 3338 /* check direction */ 3339 dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN); 3340 maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); 3341 3342 switch (maxpacksize) { 3343 default: 3344 usb_audio_err(mixer->chip, 3345 "incorrect wMaxPacketSize 0x%x for BADD profile\n", 3346 maxpacksize); 3347 return -EINVAL; 3348 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16: 3349 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16: 3350 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24: 3351 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24: 3352 chmask = 1; 3353 break; 3354 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16: 3355 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16: 3356 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24: 3357 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24: 3358 chmask = 3; 3359 break; 3360 } 3361 3362 if (dir_in) 3363 c_chmask = chmask; 3364 else 3365 p_chmask = chmask; 3366 } 3367 3368 usb_audio_dbg(mixer->chip, 3369 "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n", 3370 badd_profile, c_chmask, p_chmask); 3371 3372 /* check the mapping table */ 3373 for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) { 3374 if (map->id == badd_profile) 3375 break; 3376 } 3377 3378 if (!map->id) 3379 return -EINVAL; 3380 3381 for (f = uac3_badd_profiles; f->name; f++) { 3382 if (badd_profile == f->subclass) 3383 break; 3384 } 3385 if (!f->name) 3386 return -EINVAL; 3387 if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask)) 3388 return -EINVAL; 3389 st_chmask = f->st_chmask; 3390 3391 /* Playback */ 3392 if (p_chmask) { 3393 /* Master channel, always writable */ 3394 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE, 3395 UAC3_BADD_FU_ID2, map->map); 3396 /* Mono/Stereo volume channels, always writable */ 3397 build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME, 3398 UAC3_BADD_FU_ID2, map->map); 3399 } 3400 3401 /* Capture */ 3402 if (c_chmask) { 3403 /* Master channel, always writable */ 3404 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE, 3405 UAC3_BADD_FU_ID5, map->map); 3406 /* Mono/Stereo volume channels, always writable */ 3407 build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME, 3408 UAC3_BADD_FU_ID5, map->map); 3409 } 3410 3411 /* Side tone-mixing */ 3412 if (st_chmask) { 3413 /* Master channel, always writable */ 3414 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE, 3415 UAC3_BADD_FU_ID7, map->map); 3416 /* Mono volume channel, always writable */ 3417 build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME, 3418 UAC3_BADD_FU_ID7, map->map); 3419 } 3420 3421 /* Insertion Control */ 3422 if (f->subclass == UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER) { 3423 struct usb_audio_term iterm, oterm; 3424 3425 /* Input Term - Insertion control */ 3426 memset(&iterm, 0, sizeof(iterm)); 3427 iterm.id = UAC3_BADD_IT_ID4; 3428 iterm.type = UAC_BIDIR_TERMINAL_HEADSET; 3429 build_connector_control(mixer, map->map, &iterm, true); 3430 3431 /* Output Term - Insertion control */ 3432 memset(&oterm, 0, sizeof(oterm)); 3433 oterm.id = UAC3_BADD_OT_ID3; 3434 oterm.type = UAC_BIDIR_TERMINAL_HEADSET; 3435 build_connector_control(mixer, map->map, &oterm, false); 3436 } 3437 3438 return 0; 3439 } 3440 3441 /* 3442 * create mixer controls 3443 * 3444 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers 3445 */ 3446 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer) 3447 { 3448 struct mixer_build state; 3449 int err; 3450 const struct usbmix_ctl_map *map; 3451 void *p; 3452 3453 memset(&state, 0, sizeof(state)); 3454 state.chip = mixer->chip; 3455 state.mixer = mixer; 3456 state.buffer = mixer->hostif->extra; 3457 state.buflen = mixer->hostif->extralen; 3458 3459 /* check the mapping table */ 3460 for (map = usbmix_ctl_maps; map->id; map++) { 3461 if (map->id == state.chip->usb_id) { 3462 state.map = map->map; 3463 state.selector_map = map->selector_map; 3464 mixer->connector_map = map->connector_map; 3465 break; 3466 } 3467 } 3468 3469 p = NULL; 3470 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra, 3471 mixer->hostif->extralen, 3472 p, UAC_OUTPUT_TERMINAL)) != NULL) { 3473 if (!snd_usb_validate_audio_desc(p, mixer->protocol)) 3474 continue; /* skip invalid descriptor */ 3475 3476 if (mixer->protocol == UAC_VERSION_1) { 3477 struct uac1_output_terminal_descriptor *desc = p; 3478 3479 /* mark terminal ID as visited */ 3480 set_bit(desc->bTerminalID, state.unitbitmap); 3481 state.oterm.id = desc->bTerminalID; 3482 state.oterm.type = le16_to_cpu(desc->wTerminalType); 3483 state.oterm.name = desc->iTerminal; 3484 err = parse_audio_unit(&state, desc->bSourceID); 3485 if (err < 0 && err != -EINVAL) 3486 return err; 3487 } else if (mixer->protocol == UAC_VERSION_2) { 3488 struct uac2_output_terminal_descriptor *desc = p; 3489 3490 /* mark terminal ID as visited */ 3491 set_bit(desc->bTerminalID, state.unitbitmap); 3492 state.oterm.id = desc->bTerminalID; 3493 state.oterm.type = le16_to_cpu(desc->wTerminalType); 3494 state.oterm.name = desc->iTerminal; 3495 err = parse_audio_unit(&state, desc->bSourceID); 3496 if (err < 0 && err != -EINVAL) 3497 return err; 3498 3499 /* 3500 * For UAC2, use the same approach to also add the 3501 * clock selectors 3502 */ 3503 err = parse_audio_unit(&state, desc->bCSourceID); 3504 if (err < 0 && err != -EINVAL) 3505 return err; 3506 3507 if ((state.oterm.type & 0xff00) != 0x0100 && 3508 uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls), 3509 UAC2_TE_CONNECTOR)) { 3510 build_connector_control(state.mixer, state.map, 3511 &state.oterm, false); 3512 } 3513 } else { /* UAC_VERSION_3 */ 3514 struct uac3_output_terminal_descriptor *desc = p; 3515 3516 /* mark terminal ID as visited */ 3517 set_bit(desc->bTerminalID, state.unitbitmap); 3518 state.oterm.id = desc->bTerminalID; 3519 state.oterm.type = le16_to_cpu(desc->wTerminalType); 3520 state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr); 3521 err = parse_audio_unit(&state, desc->bSourceID); 3522 if (err < 0 && err != -EINVAL) 3523 return err; 3524 3525 /* 3526 * For UAC3, use the same approach to also add the 3527 * clock selectors 3528 */ 3529 err = parse_audio_unit(&state, desc->bCSourceID); 3530 if (err < 0 && err != -EINVAL) 3531 return err; 3532 3533 if ((state.oterm.type & 0xff00) != 0x0100 && 3534 uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls), 3535 UAC3_TE_INSERTION)) { 3536 build_connector_control(state.mixer, state.map, 3537 &state.oterm, false); 3538 } 3539 } 3540 } 3541 3542 return 0; 3543 } 3544 3545 static int delegate_notify(struct usb_mixer_interface *mixer, int unitid, 3546 u8 *control, u8 *channel) 3547 { 3548 const struct usbmix_connector_map *map = mixer->connector_map; 3549 3550 if (!map) 3551 return unitid; 3552 3553 for (; map->id; map++) { 3554 if (map->id == unitid) { 3555 if (control && map->control) 3556 *control = map->control; 3557 if (channel && map->channel) 3558 *channel = map->channel; 3559 return map->delegated_id; 3560 } 3561 } 3562 return unitid; 3563 } 3564 3565 void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid) 3566 { 3567 struct usb_mixer_elem_list *list; 3568 3569 unitid = delegate_notify(mixer, unitid, NULL, NULL); 3570 3571 for_each_mixer_elem(list, mixer, unitid) { 3572 struct usb_mixer_elem_info *info; 3573 3574 if (!list->is_std_info) 3575 continue; 3576 info = mixer_elem_list_to_info(list); 3577 /* invalidate cache, so the value is read from the device */ 3578 if (!info->get_cur_broken) 3579 info->cached = 0; 3580 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, 3581 &list->kctl->id); 3582 } 3583 } 3584 3585 static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer, 3586 struct usb_mixer_elem_list *list) 3587 { 3588 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list); 3589 static const char * const val_types[] = { 3590 [USB_MIXER_BOOLEAN] = "BOOLEAN", 3591 [USB_MIXER_INV_BOOLEAN] = "INV_BOOLEAN", 3592 [USB_MIXER_S8] = "S8", 3593 [USB_MIXER_U8] = "U8", 3594 [USB_MIXER_S16] = "S16", 3595 [USB_MIXER_U16] = "U16", 3596 [USB_MIXER_S32] = "S32", 3597 [USB_MIXER_U32] = "U32", 3598 [USB_MIXER_BESPOKEN] = "BESPOKEN", 3599 }; 3600 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%llx, " 3601 "channels=%i, type=\"%s\"\n", cval->head.id, 3602 cval->control, cval->cmask, cval->channels, 3603 val_types[cval->val_type]); 3604 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n", 3605 cval->min, cval->max, cval->dBmin, cval->dBmax); 3606 } 3607 3608 static void snd_usb_mixer_proc_read(struct snd_info_entry *entry, 3609 struct snd_info_buffer *buffer) 3610 { 3611 struct snd_usb_audio *chip = entry->private_data; 3612 struct usb_mixer_interface *mixer; 3613 struct usb_mixer_elem_list *list; 3614 int unitid; 3615 3616 list_for_each_entry(mixer, &chip->mixer_list, list) { 3617 snd_iprintf(buffer, 3618 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n", 3619 chip->usb_id, mixer_ctrl_intf(mixer), 3620 mixer->ignore_ctl_error); 3621 snd_iprintf(buffer, "Card: %s\n", chip->card->longname); 3622 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) { 3623 for_each_mixer_elem(list, mixer, unitid) { 3624 snd_iprintf(buffer, " Unit: %i\n", list->id); 3625 if (list->kctl) 3626 snd_iprintf(buffer, 3627 " Control: name=\"%s\", index=%i\n", 3628 list->kctl->id.name, 3629 list->kctl->id.index); 3630 if (list->dump) 3631 list->dump(buffer, list); 3632 } 3633 } 3634 } 3635 } 3636 3637 static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer, 3638 int attribute, int value, int index) 3639 { 3640 struct usb_mixer_elem_list *list; 3641 __u8 unitid = (index >> 8) & 0xff; 3642 __u8 control = (value >> 8) & 0xff; 3643 __u8 channel = value & 0xff; 3644 unsigned int count = 0; 3645 3646 if (channel >= MAX_CHANNELS) { 3647 usb_audio_dbg(mixer->chip, 3648 "%s(): bogus channel number %d\n", 3649 __func__, channel); 3650 return; 3651 } 3652 3653 unitid = delegate_notify(mixer, unitid, &control, &channel); 3654 3655 for_each_mixer_elem(list, mixer, unitid) 3656 count++; 3657 3658 if (count == 0) 3659 return; 3660 3661 for_each_mixer_elem(list, mixer, unitid) { 3662 struct usb_mixer_elem_info *info; 3663 3664 if (!list->kctl) 3665 continue; 3666 if (!list->is_std_info) 3667 continue; 3668 3669 info = mixer_elem_list_to_info(list); 3670 if (count > 1 && info->control != control) 3671 continue; 3672 3673 switch (attribute) { 3674 case UAC2_CS_CUR: 3675 /* invalidate cache, so the value is read from the device */ 3676 if (!info->get_cur_broken) { 3677 if (channel) 3678 info->cached &= ~BIT(channel); 3679 else /* master channel */ 3680 info->cached = 0; 3681 } 3682 3683 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, 3684 &info->head.kctl->id); 3685 break; 3686 3687 case UAC2_CS_RANGE: 3688 /* TODO */ 3689 break; 3690 3691 case UAC2_CS_MEM: 3692 /* TODO */ 3693 break; 3694 3695 default: 3696 usb_audio_dbg(mixer->chip, 3697 "unknown attribute %d in interrupt\n", 3698 attribute); 3699 break; 3700 } /* switch */ 3701 } 3702 } 3703 3704 static void snd_usb_mixer_interrupt(struct urb *urb) 3705 { 3706 struct usb_mixer_interface *mixer = urb->context; 3707 int len = urb->actual_length; 3708 int ustatus = urb->status; 3709 3710 if (ustatus != 0) 3711 goto requeue; 3712 3713 if (mixer->protocol == UAC_VERSION_1) { 3714 struct uac1_status_word *status; 3715 3716 for (status = urb->transfer_buffer; 3717 len >= sizeof(*status); 3718 len -= sizeof(*status), status++) { 3719 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n", 3720 status->bStatusType, 3721 status->bOriginator); 3722 3723 /* ignore any notifications not from the control interface */ 3724 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) != 3725 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF) 3726 continue; 3727 3728 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED) 3729 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator); 3730 else 3731 snd_usb_mixer_notify_id(mixer, status->bOriginator); 3732 } 3733 } else { /* UAC_VERSION_2 */ 3734 struct uac2_interrupt_data_msg *msg; 3735 3736 for (msg = urb->transfer_buffer; 3737 len >= sizeof(*msg); 3738 len -= sizeof(*msg), msg++) { 3739 /* drop vendor specific and endpoint requests */ 3740 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) || 3741 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP)) 3742 continue; 3743 3744 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute, 3745 le16_to_cpu(msg->wValue), 3746 le16_to_cpu(msg->wIndex)); 3747 } 3748 } 3749 3750 requeue: 3751 if (ustatus != -ENOENT && 3752 ustatus != -ECONNRESET && 3753 ustatus != -ESHUTDOWN) { 3754 urb->dev = mixer->chip->dev; 3755 usb_submit_urb(urb, GFP_ATOMIC); 3756 } 3757 } 3758 3759 /* create the handler for the optional status interrupt endpoint */ 3760 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer) 3761 { 3762 struct usb_endpoint_descriptor *ep; 3763 void *transfer_buffer; 3764 int buffer_length; 3765 unsigned int epnum; 3766 3767 /* we need one interrupt input endpoint */ 3768 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1) 3769 return 0; 3770 ep = get_endpoint(mixer->hostif, 0); 3771 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep)) 3772 return 0; 3773 3774 epnum = usb_endpoint_num(ep); 3775 buffer_length = le16_to_cpu(ep->wMaxPacketSize); 3776 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL); 3777 if (!transfer_buffer) 3778 return -ENOMEM; 3779 mixer->urb = usb_alloc_urb(0, GFP_KERNEL); 3780 if (!mixer->urb) { 3781 kfree(transfer_buffer); 3782 return -ENOMEM; 3783 } 3784 usb_fill_int_urb(mixer->urb, mixer->chip->dev, 3785 usb_rcvintpipe(mixer->chip->dev, epnum), 3786 transfer_buffer, buffer_length, 3787 snd_usb_mixer_interrupt, mixer, ep->bInterval); 3788 usb_submit_urb(mixer->urb, GFP_KERNEL); 3789 return 0; 3790 } 3791 3792 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif) 3793 { 3794 static const struct snd_device_ops dev_ops = { 3795 .dev_free = snd_usb_mixer_dev_free 3796 }; 3797 struct usb_mixer_interface *mixer; 3798 int err; 3799 3800 strscpy(chip->card->mixername, "USB Mixer"); 3801 3802 mixer = kzalloc_obj(*mixer); 3803 if (!mixer) 3804 return -ENOMEM; 3805 mixer->chip = chip; 3806 mixer->ignore_ctl_error = !!(chip->quirk_flags & QUIRK_FLAG_IGNORE_CTL_ERROR); 3807 mixer->id_elems = kzalloc_objs(*mixer->id_elems, MAX_ID_ELEMS); 3808 if (!mixer->id_elems) { 3809 kfree(mixer); 3810 return -ENOMEM; 3811 } 3812 3813 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0]; 3814 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) { 3815 case UAC_VERSION_1: 3816 default: 3817 mixer->protocol = UAC_VERSION_1; 3818 break; 3819 case UAC_VERSION_2: 3820 mixer->protocol = UAC_VERSION_2; 3821 break; 3822 case UAC_VERSION_3: 3823 mixer->protocol = UAC_VERSION_3; 3824 break; 3825 } 3826 3827 if (mixer->protocol == UAC_VERSION_3 && 3828 chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) { 3829 err = snd_usb_mixer_controls_badd(mixer, ctrlif); 3830 if (err < 0) 3831 goto _error; 3832 } else { 3833 err = snd_usb_mixer_controls(mixer); 3834 if (err < 0) 3835 goto _error; 3836 } 3837 3838 err = snd_usb_mixer_status_create(mixer); 3839 if (err < 0) 3840 goto _error; 3841 3842 err = snd_usb_mixer_apply_create_quirk(mixer); 3843 if (err < 0) 3844 goto _error; 3845 3846 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops); 3847 if (err < 0) 3848 goto _error; 3849 3850 if (list_empty(&chip->mixer_list)) 3851 snd_card_ro_proc_new(chip->card, "usbmixer", chip, 3852 snd_usb_mixer_proc_read); 3853 3854 list_add(&mixer->list, &chip->mixer_list); 3855 return 0; 3856 3857 _error: 3858 snd_usb_mixer_free(mixer); 3859 return err; 3860 } 3861 3862 void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer) 3863 { 3864 if (mixer->disconnected) 3865 return; 3866 if (mixer->urb) 3867 usb_kill_urb(mixer->urb); 3868 if (mixer->rc_urb) 3869 usb_kill_urb(mixer->rc_urb); 3870 if (mixer->private_free) 3871 mixer->private_free(mixer); 3872 mixer->disconnected = true; 3873 } 3874 3875 /* stop any bus activity of a mixer */ 3876 static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer) 3877 { 3878 usb_kill_urb(mixer->urb); 3879 usb_kill_urb(mixer->rc_urb); 3880 } 3881 3882 static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer) 3883 { 3884 int err; 3885 3886 if (mixer->urb) { 3887 err = usb_submit_urb(mixer->urb, GFP_NOIO); 3888 if (err < 0) 3889 return err; 3890 } 3891 3892 return 0; 3893 } 3894 3895 int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer) 3896 { 3897 snd_usb_mixer_inactivate(mixer); 3898 if (mixer->private_suspend) 3899 mixer->private_suspend(mixer); 3900 return 0; 3901 } 3902 3903 static int restore_mixer_value(struct usb_mixer_elem_list *list) 3904 { 3905 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list); 3906 int c, err, idx; 3907 3908 if (cval->val_type == USB_MIXER_BESPOKEN) 3909 return 0; 3910 3911 if (cval->cmask) { 3912 idx = 0; 3913 for (c = 0; c < MAX_CHANNELS; c++) { 3914 if (!(cval->cmask & BIT(c))) 3915 continue; 3916 if (cval->cached & BIT(c + 1)) { 3917 err = snd_usb_set_cur_mix_value(cval, c + 1, idx, 3918 cval->cache_val[idx]); 3919 if (err < 0) 3920 break; 3921 } 3922 idx++; 3923 } 3924 } else { 3925 /* master */ 3926 if (cval->cached) 3927 snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val); 3928 } 3929 3930 return 0; 3931 } 3932 3933 int snd_usb_mixer_resume(struct usb_mixer_interface *mixer) 3934 { 3935 struct usb_mixer_elem_list *list; 3936 int id, err; 3937 3938 /* restore cached mixer values */ 3939 for (id = 0; id < MAX_ID_ELEMS; id++) { 3940 for_each_mixer_elem(list, mixer, id) { 3941 if (list->resume) { 3942 err = list->resume(list); 3943 if (err < 0) 3944 return err; 3945 } 3946 } 3947 } 3948 3949 snd_usb_mixer_resume_quirk(mixer); 3950 3951 return snd_usb_mixer_activate(mixer); 3952 } 3953 3954 void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list, 3955 struct usb_mixer_interface *mixer, 3956 int unitid) 3957 { 3958 list->mixer = mixer; 3959 list->id = unitid; 3960 list->dump = snd_usb_mixer_dump_cval; 3961 list->resume = restore_mixer_value; 3962 } 3963