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