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