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 55 #include <sound/core.h> 56 #include <sound/control.h> 57 #include <sound/hwdep.h> 58 #include <sound/info.h> 59 #include <sound/tlv.h> 60 61 #include "usbaudio.h" 62 #include "mixer.h" 63 #include "helper.h" 64 #include "mixer_quirks.h" 65 #include "power.h" 66 67 #define MAX_ID_ELEMS 256 68 69 struct usb_audio_term { 70 int id; 71 int type; 72 int channels; 73 unsigned int chconfig; 74 int name; 75 }; 76 77 struct usbmix_name_map; 78 79 struct mixer_build { 80 struct snd_usb_audio *chip; 81 struct usb_mixer_interface *mixer; 82 unsigned char *buffer; 83 unsigned int buflen; 84 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS); 85 struct usb_audio_term oterm; 86 const struct usbmix_name_map *map; 87 const struct usbmix_selector_map *selector_map; 88 }; 89 90 /*E-mu 0202/0404/0204 eXtension Unit(XU) control*/ 91 enum { 92 USB_XU_CLOCK_RATE = 0xe301, 93 USB_XU_CLOCK_SOURCE = 0xe302, 94 USB_XU_DIGITAL_IO_STATUS = 0xe303, 95 USB_XU_DEVICE_OPTIONS = 0xe304, 96 USB_XU_DIRECT_MONITORING = 0xe305, 97 USB_XU_METERING = 0xe306 98 }; 99 enum { 100 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/ 101 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */ 102 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */ 103 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */ 104 }; 105 106 /* 107 * manual mapping of mixer names 108 * if the mixer topology is too complicated and the parsed names are 109 * ambiguous, add the entries in usbmixer_maps.c. 110 */ 111 #include "mixer_maps.c" 112 113 static const struct usbmix_name_map * 114 find_map(struct mixer_build *state, int unitid, int control) 115 { 116 const struct usbmix_name_map *p = state->map; 117 118 if (!p) 119 return NULL; 120 121 for (p = state->map; p->id; p++) { 122 if (p->id == unitid && 123 (!control || !p->control || control == p->control)) 124 return p; 125 } 126 return NULL; 127 } 128 129 /* get the mapped name if the unit matches */ 130 static int 131 check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen) 132 { 133 if (!p || !p->name) 134 return 0; 135 136 buflen--; 137 return strlcpy(buf, p->name, buflen); 138 } 139 140 /* ignore the error value if ignore_ctl_error flag is set */ 141 #define filter_error(cval, err) \ 142 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err)) 143 144 /* check whether the control should be ignored */ 145 static inline int 146 check_ignored_ctl(const struct usbmix_name_map *p) 147 { 148 if (!p || p->name || p->dB) 149 return 0; 150 return 1; 151 } 152 153 /* dB mapping */ 154 static inline void check_mapped_dB(const struct usbmix_name_map *p, 155 struct usb_mixer_elem_info *cval) 156 { 157 if (p && p->dB) { 158 cval->dBmin = p->dB->min; 159 cval->dBmax = p->dB->max; 160 cval->initialized = 1; 161 } 162 } 163 164 /* get the mapped selector source name */ 165 static int check_mapped_selector_name(struct mixer_build *state, int unitid, 166 int index, char *buf, int buflen) 167 { 168 const struct usbmix_selector_map *p; 169 170 if (!state->selector_map) 171 return 0; 172 for (p = state->selector_map; p->id; p++) { 173 if (p->id == unitid && index < p->count) 174 return strlcpy(buf, p->names[index], buflen); 175 } 176 return 0; 177 } 178 179 /* 180 * find an audio control unit with the given unit id 181 */ 182 static void *find_audio_control_unit(struct mixer_build *state, 183 unsigned char unit) 184 { 185 /* we just parse the header */ 186 struct uac_feature_unit_descriptor *hdr = NULL; 187 188 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr, 189 USB_DT_CS_INTERFACE)) != NULL) { 190 if (hdr->bLength >= 4 && 191 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL && 192 hdr->bDescriptorSubtype <= UAC2_SAMPLE_RATE_CONVERTER && 193 hdr->bUnitID == unit) 194 return hdr; 195 } 196 197 return NULL; 198 } 199 200 /* 201 * copy a string with the given id 202 */ 203 static int snd_usb_copy_string_desc(struct mixer_build *state, 204 int index, char *buf, int maxlen) 205 { 206 int len = usb_string(state->chip->dev, index, buf, maxlen - 1); 207 buf[len] = 0; 208 return len; 209 } 210 211 /* 212 * convert from the byte/word on usb descriptor to the zero-based integer 213 */ 214 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val) 215 { 216 switch (cval->val_type) { 217 case USB_MIXER_BOOLEAN: 218 return !!val; 219 case USB_MIXER_INV_BOOLEAN: 220 return !val; 221 case USB_MIXER_U8: 222 val &= 0xff; 223 break; 224 case USB_MIXER_S8: 225 val &= 0xff; 226 if (val >= 0x80) 227 val -= 0x100; 228 break; 229 case USB_MIXER_U16: 230 val &= 0xffff; 231 break; 232 case USB_MIXER_S16: 233 val &= 0xffff; 234 if (val >= 0x8000) 235 val -= 0x10000; 236 break; 237 } 238 return val; 239 } 240 241 /* 242 * convert from the zero-based int to the byte/word for usb descriptor 243 */ 244 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val) 245 { 246 switch (cval->val_type) { 247 case USB_MIXER_BOOLEAN: 248 return !!val; 249 case USB_MIXER_INV_BOOLEAN: 250 return !val; 251 case USB_MIXER_S8: 252 case USB_MIXER_U8: 253 return val & 0xff; 254 case USB_MIXER_S16: 255 case USB_MIXER_U16: 256 return val & 0xffff; 257 } 258 return 0; /* not reached */ 259 } 260 261 static int get_relative_value(struct usb_mixer_elem_info *cval, int val) 262 { 263 if (!cval->res) 264 cval->res = 1; 265 if (val < cval->min) 266 return 0; 267 else if (val >= cval->max) 268 return (cval->max - cval->min + cval->res - 1) / cval->res; 269 else 270 return (val - cval->min) / cval->res; 271 } 272 273 static int get_abs_value(struct usb_mixer_elem_info *cval, int val) 274 { 275 if (val < 0) 276 return cval->min; 277 if (!cval->res) 278 cval->res = 1; 279 val *= cval->res; 280 val += cval->min; 281 if (val > cval->max) 282 return cval->max; 283 return val; 284 } 285 286 static int uac2_ctl_value_size(int val_type) 287 { 288 switch (val_type) { 289 case USB_MIXER_S32: 290 case USB_MIXER_U32: 291 return 4; 292 case USB_MIXER_S16: 293 case USB_MIXER_U16: 294 return 2; 295 default: 296 return 1; 297 } 298 return 0; /* unreachable */ 299 } 300 301 302 /* 303 * retrieve a mixer value 304 */ 305 306 static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request, 307 int validx, int *value_ret) 308 { 309 struct snd_usb_audio *chip = cval->head.mixer->chip; 310 unsigned char buf[2]; 311 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; 312 int timeout = 10; 313 int idx = 0, err; 314 315 err = snd_usb_lock_shutdown(chip); 316 if (err < 0) 317 return -EIO; 318 319 while (timeout-- > 0) { 320 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8); 321 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request, 322 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 323 validx, idx, buf, val_len); 324 if (err >= val_len) { 325 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len)); 326 err = 0; 327 goto out; 328 } else if (err == -ETIMEDOUT) { 329 goto out; 330 } 331 } 332 usb_audio_dbg(chip, 333 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", 334 request, validx, idx, cval->val_type); 335 err = -EINVAL; 336 337 out: 338 snd_usb_unlock_shutdown(chip); 339 return err; 340 } 341 342 static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request, 343 int validx, int *value_ret) 344 { 345 struct snd_usb_audio *chip = cval->head.mixer->chip; 346 unsigned char buf[4 + 3 * sizeof(__u32)]; /* enough space for one range */ 347 unsigned char *val; 348 int idx = 0, ret, size; 349 __u8 bRequest; 350 351 if (request == UAC_GET_CUR) { 352 bRequest = UAC2_CS_CUR; 353 size = uac2_ctl_value_size(cval->val_type); 354 } else { 355 bRequest = UAC2_CS_RANGE; 356 size = sizeof(buf); 357 } 358 359 memset(buf, 0, sizeof(buf)); 360 361 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0; 362 if (ret) 363 goto error; 364 365 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8); 366 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest, 367 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 368 validx, idx, buf, size); 369 snd_usb_unlock_shutdown(chip); 370 371 if (ret < 0) { 372 error: 373 usb_audio_err(chip, 374 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", 375 request, validx, idx, cval->val_type); 376 return ret; 377 } 378 379 /* FIXME: how should we handle multiple triplets here? */ 380 381 switch (request) { 382 case UAC_GET_CUR: 383 val = buf; 384 break; 385 case UAC_GET_MIN: 386 val = buf + sizeof(__u16); 387 break; 388 case UAC_GET_MAX: 389 val = buf + sizeof(__u16) * 2; 390 break; 391 case UAC_GET_RES: 392 val = buf + sizeof(__u16) * 3; 393 break; 394 default: 395 return -EINVAL; 396 } 397 398 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(val, sizeof(__u16))); 399 400 return 0; 401 } 402 403 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, 404 int validx, int *value_ret) 405 { 406 validx += cval->idx_off; 407 408 return (cval->head.mixer->protocol == UAC_VERSION_1) ? 409 get_ctl_value_v1(cval, request, validx, value_ret) : 410 get_ctl_value_v2(cval, request, validx, value_ret); 411 } 412 413 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, 414 int validx, int *value) 415 { 416 return get_ctl_value(cval, UAC_GET_CUR, validx, value); 417 } 418 419 /* channel = 0: master, 1 = first channel */ 420 static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval, 421 int channel, int *value) 422 { 423 return get_ctl_value(cval, UAC_GET_CUR, 424 (cval->control << 8) | channel, 425 value); 426 } 427 428 int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval, 429 int channel, int index, int *value) 430 { 431 int err; 432 433 if (cval->cached & (1 << channel)) { 434 *value = cval->cache_val[index]; 435 return 0; 436 } 437 err = get_cur_mix_raw(cval, channel, value); 438 if (err < 0) { 439 if (!cval->head.mixer->ignore_ctl_error) 440 usb_audio_dbg(cval->head.mixer->chip, 441 "cannot get current value for control %d ch %d: err = %d\n", 442 cval->control, channel, err); 443 return err; 444 } 445 cval->cached |= 1 << channel; 446 cval->cache_val[index] = *value; 447 return 0; 448 } 449 450 /* 451 * set a mixer value 452 */ 453 454 int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval, 455 int request, int validx, int value_set) 456 { 457 struct snd_usb_audio *chip = cval->head.mixer->chip; 458 unsigned char buf[4]; 459 int idx = 0, val_len, err, timeout = 10; 460 461 validx += cval->idx_off; 462 463 if (cval->head.mixer->protocol == UAC_VERSION_1) { 464 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; 465 } else { /* UAC_VERSION_2 */ 466 val_len = uac2_ctl_value_size(cval->val_type); 467 468 /* FIXME */ 469 if (request != UAC_SET_CUR) { 470 usb_audio_dbg(chip, "RANGE setting not yet supported\n"); 471 return -EINVAL; 472 } 473 474 request = UAC2_CS_CUR; 475 } 476 477 value_set = convert_bytes_value(cval, value_set); 478 buf[0] = value_set & 0xff; 479 buf[1] = (value_set >> 8) & 0xff; 480 buf[2] = (value_set >> 16) & 0xff; 481 buf[3] = (value_set >> 24) & 0xff; 482 483 err = snd_usb_lock_shutdown(chip); 484 if (err < 0) 485 return -EIO; 486 487 while (timeout-- > 0) { 488 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8); 489 err = snd_usb_ctl_msg(chip->dev, 490 usb_sndctrlpipe(chip->dev, 0), request, 491 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, 492 validx, idx, buf, val_len); 493 if (err >= 0) { 494 err = 0; 495 goto out; 496 } else if (err == -ETIMEDOUT) { 497 goto out; 498 } 499 } 500 usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n", 501 request, validx, idx, cval->val_type, buf[0], buf[1]); 502 err = -EINVAL; 503 504 out: 505 snd_usb_unlock_shutdown(chip); 506 return err; 507 } 508 509 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, 510 int validx, int value) 511 { 512 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value); 513 } 514 515 int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, 516 int index, int value) 517 { 518 int err; 519 unsigned int read_only = (channel == 0) ? 520 cval->master_readonly : 521 cval->ch_readonly & (1 << (channel - 1)); 522 523 if (read_only) { 524 usb_audio_dbg(cval->head.mixer->chip, 525 "%s(): channel %d of control %d is read_only\n", 526 __func__, channel, cval->control); 527 return 0; 528 } 529 530 err = snd_usb_mixer_set_ctl_value(cval, 531 UAC_SET_CUR, (cval->control << 8) | channel, 532 value); 533 if (err < 0) 534 return err; 535 cval->cached |= 1 << channel; 536 cval->cache_val[index] = value; 537 return 0; 538 } 539 540 /* 541 * TLV callback for mixer volume controls 542 */ 543 int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag, 544 unsigned int size, unsigned int __user *_tlv) 545 { 546 struct usb_mixer_elem_info *cval = kcontrol->private_data; 547 DECLARE_TLV_DB_MINMAX(scale, 0, 0); 548 549 if (size < sizeof(scale)) 550 return -ENOMEM; 551 scale[2] = cval->dBmin; 552 scale[3] = cval->dBmax; 553 if (copy_to_user(_tlv, scale, sizeof(scale))) 554 return -EFAULT; 555 return 0; 556 } 557 558 /* 559 * parser routines begin here... 560 */ 561 562 static int parse_audio_unit(struct mixer_build *state, int unitid); 563 564 565 /* 566 * check if the input/output channel routing is enabled on the given bitmap. 567 * used for mixer unit parser 568 */ 569 static int check_matrix_bitmap(unsigned char *bmap, 570 int ich, int och, int num_outs) 571 { 572 int idx = ich * num_outs + och; 573 return bmap[idx >> 3] & (0x80 >> (idx & 7)); 574 } 575 576 /* 577 * add an alsa control element 578 * search and increment the index until an empty slot is found. 579 * 580 * if failed, give up and free the control instance. 581 */ 582 583 int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list, 584 struct snd_kcontrol *kctl) 585 { 586 struct usb_mixer_interface *mixer = list->mixer; 587 int err; 588 589 while (snd_ctl_find_id(mixer->chip->card, &kctl->id)) 590 kctl->id.index++; 591 if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) { 592 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n", 593 err); 594 return err; 595 } 596 list->kctl = kctl; 597 list->next_id_elem = mixer->id_elems[list->id]; 598 mixer->id_elems[list->id] = list; 599 return 0; 600 } 601 602 /* 603 * get a terminal name string 604 */ 605 606 static struct iterm_name_combo { 607 int type; 608 char *name; 609 } iterm_names[] = { 610 { 0x0300, "Output" }, 611 { 0x0301, "Speaker" }, 612 { 0x0302, "Headphone" }, 613 { 0x0303, "HMD Audio" }, 614 { 0x0304, "Desktop Speaker" }, 615 { 0x0305, "Room Speaker" }, 616 { 0x0306, "Com Speaker" }, 617 { 0x0307, "LFE" }, 618 { 0x0600, "External In" }, 619 { 0x0601, "Analog In" }, 620 { 0x0602, "Digital In" }, 621 { 0x0603, "Line" }, 622 { 0x0604, "Legacy In" }, 623 { 0x0605, "IEC958 In" }, 624 { 0x0606, "1394 DA Stream" }, 625 { 0x0607, "1394 DV Stream" }, 626 { 0x0700, "Embedded" }, 627 { 0x0701, "Noise Source" }, 628 { 0x0702, "Equalization Noise" }, 629 { 0x0703, "CD" }, 630 { 0x0704, "DAT" }, 631 { 0x0705, "DCC" }, 632 { 0x0706, "MiniDisk" }, 633 { 0x0707, "Analog Tape" }, 634 { 0x0708, "Phonograph" }, 635 { 0x0709, "VCR Audio" }, 636 { 0x070a, "Video Disk Audio" }, 637 { 0x070b, "DVD Audio" }, 638 { 0x070c, "TV Tuner Audio" }, 639 { 0x070d, "Satellite Rec Audio" }, 640 { 0x070e, "Cable Tuner Audio" }, 641 { 0x070f, "DSS Audio" }, 642 { 0x0710, "Radio Receiver" }, 643 { 0x0711, "Radio Transmitter" }, 644 { 0x0712, "Multi-Track Recorder" }, 645 { 0x0713, "Synthesizer" }, 646 { 0 }, 647 }; 648 649 static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm, 650 unsigned char *name, int maxlen, int term_only) 651 { 652 struct iterm_name_combo *names; 653 654 if (iterm->name) 655 return snd_usb_copy_string_desc(state, iterm->name, 656 name, maxlen); 657 658 /* virtual type - not a real terminal */ 659 if (iterm->type >> 16) { 660 if (term_only) 661 return 0; 662 switch (iterm->type >> 16) { 663 case UAC_SELECTOR_UNIT: 664 strcpy(name, "Selector"); 665 return 8; 666 case UAC1_PROCESSING_UNIT: 667 strcpy(name, "Process Unit"); 668 return 12; 669 case UAC1_EXTENSION_UNIT: 670 strcpy(name, "Ext Unit"); 671 return 8; 672 case UAC_MIXER_UNIT: 673 strcpy(name, "Mixer"); 674 return 5; 675 default: 676 return sprintf(name, "Unit %d", iterm->id); 677 } 678 } 679 680 switch (iterm->type & 0xff00) { 681 case 0x0100: 682 strcpy(name, "PCM"); 683 return 3; 684 case 0x0200: 685 strcpy(name, "Mic"); 686 return 3; 687 case 0x0400: 688 strcpy(name, "Headset"); 689 return 7; 690 case 0x0500: 691 strcpy(name, "Phone"); 692 return 5; 693 } 694 695 for (names = iterm_names; names->type; names++) { 696 if (names->type == iterm->type) { 697 strcpy(name, names->name); 698 return strlen(names->name); 699 } 700 } 701 702 return 0; 703 } 704 705 /* 706 * parse the source unit recursively until it reaches to a terminal 707 * or a branched unit. 708 */ 709 static int check_input_term(struct mixer_build *state, int id, 710 struct usb_audio_term *term) 711 { 712 int err; 713 void *p1; 714 715 memset(term, 0, sizeof(*term)); 716 while ((p1 = find_audio_control_unit(state, id)) != NULL) { 717 unsigned char *hdr = p1; 718 term->id = id; 719 switch (hdr[2]) { 720 case UAC_INPUT_TERMINAL: 721 if (state->mixer->protocol == UAC_VERSION_1) { 722 struct uac_input_terminal_descriptor *d = p1; 723 term->type = le16_to_cpu(d->wTerminalType); 724 term->channels = d->bNrChannels; 725 term->chconfig = le16_to_cpu(d->wChannelConfig); 726 term->name = d->iTerminal; 727 } else { /* UAC_VERSION_2 */ 728 struct uac2_input_terminal_descriptor *d = p1; 729 730 /* call recursively to verify that the 731 * referenced clock entity is valid */ 732 err = check_input_term(state, d->bCSourceID, term); 733 if (err < 0) 734 return err; 735 736 /* save input term properties after recursion, 737 * to ensure they are not overriden by the 738 * recursion calls */ 739 term->id = id; 740 term->type = le16_to_cpu(d->wTerminalType); 741 term->channels = d->bNrChannels; 742 term->chconfig = le32_to_cpu(d->bmChannelConfig); 743 term->name = d->iTerminal; 744 } 745 return 0; 746 case UAC_FEATURE_UNIT: { 747 /* the header is the same for v1 and v2 */ 748 struct uac_feature_unit_descriptor *d = p1; 749 id = d->bSourceID; 750 break; /* continue to parse */ 751 } 752 case UAC_MIXER_UNIT: { 753 struct uac_mixer_unit_descriptor *d = p1; 754 term->type = d->bDescriptorSubtype << 16; /* virtual type */ 755 term->channels = uac_mixer_unit_bNrChannels(d); 756 term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol); 757 term->name = uac_mixer_unit_iMixer(d); 758 return 0; 759 } 760 case UAC_SELECTOR_UNIT: 761 case UAC2_CLOCK_SELECTOR: { 762 struct uac_selector_unit_descriptor *d = p1; 763 /* call recursively to retrieve the channel info */ 764 err = check_input_term(state, d->baSourceID[0], term); 765 if (err < 0) 766 return err; 767 term->type = d->bDescriptorSubtype << 16; /* virtual type */ 768 term->id = id; 769 term->name = uac_selector_unit_iSelector(d); 770 return 0; 771 } 772 case UAC1_PROCESSING_UNIT: 773 case UAC1_EXTENSION_UNIT: 774 /* UAC2_PROCESSING_UNIT_V2 */ 775 /* UAC2_EFFECT_UNIT */ 776 case UAC2_EXTENSION_UNIT_V2: { 777 struct uac_processing_unit_descriptor *d = p1; 778 779 if (state->mixer->protocol == UAC_VERSION_2 && 780 hdr[2] == UAC2_EFFECT_UNIT) { 781 /* UAC2/UAC1 unit IDs overlap here in an 782 * uncompatible way. Ignore this unit for now. 783 */ 784 return 0; 785 } 786 787 if (d->bNrInPins) { 788 id = d->baSourceID[0]; 789 break; /* continue to parse */ 790 } 791 term->type = d->bDescriptorSubtype << 16; /* virtual type */ 792 term->channels = uac_processing_unit_bNrChannels(d); 793 term->chconfig = uac_processing_unit_wChannelConfig(d, state->mixer->protocol); 794 term->name = uac_processing_unit_iProcessing(d, state->mixer->protocol); 795 return 0; 796 } 797 case UAC2_CLOCK_SOURCE: { 798 struct uac_clock_source_descriptor *d = p1; 799 term->type = d->bDescriptorSubtype << 16; /* virtual type */ 800 term->id = id; 801 term->name = d->iClockSource; 802 return 0; 803 } 804 default: 805 return -ENODEV; 806 } 807 } 808 return -ENODEV; 809 } 810 811 /* 812 * Feature Unit 813 */ 814 815 /* feature unit control information */ 816 struct usb_feature_control_info { 817 const char *name; 818 int type; /* data type for uac1 */ 819 int type_uac2; /* data type for uac2 if different from uac1, else -1 */ 820 }; 821 822 static struct usb_feature_control_info audio_feature_info[] = { 823 { "Mute", USB_MIXER_INV_BOOLEAN, -1 }, 824 { "Volume", USB_MIXER_S16, -1 }, 825 { "Tone Control - Bass", USB_MIXER_S8, -1 }, 826 { "Tone Control - Mid", USB_MIXER_S8, -1 }, 827 { "Tone Control - Treble", USB_MIXER_S8, -1 }, 828 { "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemeted yet */ 829 { "Auto Gain Control", USB_MIXER_BOOLEAN, -1 }, 830 { "Delay Control", USB_MIXER_U16, USB_MIXER_U32 }, 831 { "Bass Boost", USB_MIXER_BOOLEAN, -1 }, 832 { "Loudness", USB_MIXER_BOOLEAN, -1 }, 833 /* UAC2 specific */ 834 { "Input Gain Control", USB_MIXER_S16, -1 }, 835 { "Input Gain Pad Control", USB_MIXER_S16, -1 }, 836 { "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 }, 837 }; 838 839 /* private_free callback */ 840 void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl) 841 { 842 kfree(kctl->private_data); 843 kctl->private_data = NULL; 844 } 845 846 /* 847 * interface to ALSA control for feature/mixer units 848 */ 849 850 /* volume control quirks */ 851 static void volume_control_quirks(struct usb_mixer_elem_info *cval, 852 struct snd_kcontrol *kctl) 853 { 854 struct snd_usb_audio *chip = cval->head.mixer->chip; 855 switch (chip->usb_id) { 856 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */ 857 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */ 858 if (strcmp(kctl->id.name, "Effect Duration") == 0) { 859 cval->min = 0x0000; 860 cval->max = 0xffff; 861 cval->res = 0x00e6; 862 break; 863 } 864 if (strcmp(kctl->id.name, "Effect Volume") == 0 || 865 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) { 866 cval->min = 0x00; 867 cval->max = 0xff; 868 break; 869 } 870 if (strstr(kctl->id.name, "Effect Return") != NULL) { 871 cval->min = 0xb706; 872 cval->max = 0xff7b; 873 cval->res = 0x0073; 874 break; 875 } 876 if ((strstr(kctl->id.name, "Playback Volume") != NULL) || 877 (strstr(kctl->id.name, "Effect Send") != NULL)) { 878 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */ 879 cval->max = 0xfcfe; 880 cval->res = 0x0073; 881 } 882 break; 883 884 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */ 885 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */ 886 if (strcmp(kctl->id.name, "Effect Duration") == 0) { 887 usb_audio_info(chip, 888 "set quirk for FTU Effect Duration\n"); 889 cval->min = 0x0000; 890 cval->max = 0x7f00; 891 cval->res = 0x0100; 892 break; 893 } 894 if (strcmp(kctl->id.name, "Effect Volume") == 0 || 895 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) { 896 usb_audio_info(chip, 897 "set quirks for FTU Effect Feedback/Volume\n"); 898 cval->min = 0x00; 899 cval->max = 0x7f; 900 break; 901 } 902 break; 903 904 case USB_ID(0x0471, 0x0101): 905 case USB_ID(0x0471, 0x0104): 906 case USB_ID(0x0471, 0x0105): 907 case USB_ID(0x0672, 0x1041): 908 /* quirk for UDA1321/N101. 909 * note that detection between firmware 2.1.1.7 (N101) 910 * and later 2.1.1.21 is not very clear from datasheets. 911 * I hope that the min value is -15360 for newer firmware --jk 912 */ 913 if (!strcmp(kctl->id.name, "PCM Playback Volume") && 914 cval->min == -15616) { 915 usb_audio_info(chip, 916 "set volume quirk for UDA1321/N101 chip\n"); 917 cval->max = -256; 918 } 919 break; 920 921 case USB_ID(0x046d, 0x09a4): 922 if (!strcmp(kctl->id.name, "Mic Capture Volume")) { 923 usb_audio_info(chip, 924 "set volume quirk for QuickCam E3500\n"); 925 cval->min = 6080; 926 cval->max = 8768; 927 cval->res = 192; 928 } 929 break; 930 931 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */ 932 case USB_ID(0x046d, 0x0808): 933 case USB_ID(0x046d, 0x0809): 934 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */ 935 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */ 936 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */ 937 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */ 938 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */ 939 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */ 940 case USB_ID(0x046d, 0x0991): 941 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */ 942 /* Most audio usb devices lie about volume resolution. 943 * Most Logitech webcams have res = 384. 944 * Probably there is some logitech magic behind this number --fishor 945 */ 946 if (!strcmp(kctl->id.name, "Mic Capture Volume")) { 947 usb_audio_info(chip, 948 "set resolution quirk: cval->res = 384\n"); 949 cval->res = 384; 950 } 951 break; 952 } 953 } 954 955 /* 956 * retrieve the minimum and maximum values for the specified control 957 */ 958 static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval, 959 int default_min, struct snd_kcontrol *kctl) 960 { 961 /* for failsafe */ 962 cval->min = default_min; 963 cval->max = cval->min + 1; 964 cval->res = 1; 965 cval->dBmin = cval->dBmax = 0; 966 967 if (cval->val_type == USB_MIXER_BOOLEAN || 968 cval->val_type == USB_MIXER_INV_BOOLEAN) { 969 cval->initialized = 1; 970 } else { 971 int minchn = 0; 972 if (cval->cmask) { 973 int i; 974 for (i = 0; i < MAX_CHANNELS; i++) 975 if (cval->cmask & (1 << i)) { 976 minchn = i + 1; 977 break; 978 } 979 } 980 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 || 981 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) { 982 usb_audio_err(cval->head.mixer->chip, 983 "%d:%d: cannot get min/max values for control %d (id %d)\n", 984 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip), 985 cval->control, cval->head.id); 986 return -EINVAL; 987 } 988 if (get_ctl_value(cval, UAC_GET_RES, 989 (cval->control << 8) | minchn, 990 &cval->res) < 0) { 991 cval->res = 1; 992 } else { 993 int last_valid_res = cval->res; 994 995 while (cval->res > 1) { 996 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES, 997 (cval->control << 8) | minchn, 998 cval->res / 2) < 0) 999 break; 1000 cval->res /= 2; 1001 } 1002 if (get_ctl_value(cval, UAC_GET_RES, 1003 (cval->control << 8) | minchn, &cval->res) < 0) 1004 cval->res = last_valid_res; 1005 } 1006 if (cval->res == 0) 1007 cval->res = 1; 1008 1009 /* Additional checks for the proper resolution 1010 * 1011 * Some devices report smaller resolutions than actually 1012 * reacting. They don't return errors but simply clip 1013 * to the lower aligned value. 1014 */ 1015 if (cval->min + cval->res < cval->max) { 1016 int last_valid_res = cval->res; 1017 int saved, test, check; 1018 get_cur_mix_raw(cval, minchn, &saved); 1019 for (;;) { 1020 test = saved; 1021 if (test < cval->max) 1022 test += cval->res; 1023 else 1024 test -= cval->res; 1025 if (test < cval->min || test > cval->max || 1026 snd_usb_set_cur_mix_value(cval, minchn, 0, test) || 1027 get_cur_mix_raw(cval, minchn, &check)) { 1028 cval->res = last_valid_res; 1029 break; 1030 } 1031 if (test == check) 1032 break; 1033 cval->res *= 2; 1034 } 1035 snd_usb_set_cur_mix_value(cval, minchn, 0, saved); 1036 } 1037 1038 cval->initialized = 1; 1039 } 1040 1041 if (kctl) 1042 volume_control_quirks(cval, kctl); 1043 1044 /* USB descriptions contain the dB scale in 1/256 dB unit 1045 * while ALSA TLV contains in 1/100 dB unit 1046 */ 1047 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256; 1048 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256; 1049 if (cval->dBmin > cval->dBmax) { 1050 /* something is wrong; assume it's either from/to 0dB */ 1051 if (cval->dBmin < 0) 1052 cval->dBmax = 0; 1053 else if (cval->dBmin > 0) 1054 cval->dBmin = 0; 1055 if (cval->dBmin > cval->dBmax) { 1056 /* totally crap, return an error */ 1057 return -EINVAL; 1058 } 1059 } 1060 1061 return 0; 1062 } 1063 1064 #define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL) 1065 1066 /* get a feature/mixer unit info */ 1067 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, 1068 struct snd_ctl_elem_info *uinfo) 1069 { 1070 struct usb_mixer_elem_info *cval = kcontrol->private_data; 1071 1072 if (cval->val_type == USB_MIXER_BOOLEAN || 1073 cval->val_type == USB_MIXER_INV_BOOLEAN) 1074 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1075 else 1076 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1077 uinfo->count = cval->channels; 1078 if (cval->val_type == USB_MIXER_BOOLEAN || 1079 cval->val_type == USB_MIXER_INV_BOOLEAN) { 1080 uinfo->value.integer.min = 0; 1081 uinfo->value.integer.max = 1; 1082 } else { 1083 if (!cval->initialized) { 1084 get_min_max_with_quirks(cval, 0, kcontrol); 1085 if (cval->initialized && cval->dBmin >= cval->dBmax) { 1086 kcontrol->vd[0].access &= 1087 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ | 1088 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK); 1089 snd_ctl_notify(cval->head.mixer->chip->card, 1090 SNDRV_CTL_EVENT_MASK_INFO, 1091 &kcontrol->id); 1092 } 1093 } 1094 uinfo->value.integer.min = 0; 1095 uinfo->value.integer.max = 1096 (cval->max - cval->min + cval->res - 1) / cval->res; 1097 } 1098 return 0; 1099 } 1100 1101 /* get the current value from feature/mixer unit */ 1102 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, 1103 struct snd_ctl_elem_value *ucontrol) 1104 { 1105 struct usb_mixer_elem_info *cval = kcontrol->private_data; 1106 int c, cnt, val, err; 1107 1108 ucontrol->value.integer.value[0] = cval->min; 1109 if (cval->cmask) { 1110 cnt = 0; 1111 for (c = 0; c < MAX_CHANNELS; c++) { 1112 if (!(cval->cmask & (1 << c))) 1113 continue; 1114 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val); 1115 if (err < 0) 1116 return filter_error(cval, err); 1117 val = get_relative_value(cval, val); 1118 ucontrol->value.integer.value[cnt] = val; 1119 cnt++; 1120 } 1121 return 0; 1122 } else { 1123 /* master channel */ 1124 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val); 1125 if (err < 0) 1126 return filter_error(cval, err); 1127 val = get_relative_value(cval, val); 1128 ucontrol->value.integer.value[0] = val; 1129 } 1130 return 0; 1131 } 1132 1133 /* put the current value to feature/mixer unit */ 1134 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, 1135 struct snd_ctl_elem_value *ucontrol) 1136 { 1137 struct usb_mixer_elem_info *cval = kcontrol->private_data; 1138 int c, cnt, val, oval, err; 1139 int changed = 0; 1140 1141 if (cval->cmask) { 1142 cnt = 0; 1143 for (c = 0; c < MAX_CHANNELS; c++) { 1144 if (!(cval->cmask & (1 << c))) 1145 continue; 1146 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval); 1147 if (err < 0) 1148 return filter_error(cval, err); 1149 val = ucontrol->value.integer.value[cnt]; 1150 val = get_abs_value(cval, val); 1151 if (oval != val) { 1152 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val); 1153 changed = 1; 1154 } 1155 cnt++; 1156 } 1157 } else { 1158 /* master channel */ 1159 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval); 1160 if (err < 0) 1161 return filter_error(cval, err); 1162 val = ucontrol->value.integer.value[0]; 1163 val = get_abs_value(cval, val); 1164 if (val != oval) { 1165 snd_usb_set_cur_mix_value(cval, 0, 0, val); 1166 changed = 1; 1167 } 1168 } 1169 return changed; 1170 } 1171 1172 static struct snd_kcontrol_new usb_feature_unit_ctl = { 1173 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1174 .name = "", /* will be filled later manually */ 1175 .info = mixer_ctl_feature_info, 1176 .get = mixer_ctl_feature_get, 1177 .put = mixer_ctl_feature_put, 1178 }; 1179 1180 /* the read-only variant */ 1181 static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = { 1182 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1183 .name = "", /* will be filled later manually */ 1184 .info = mixer_ctl_feature_info, 1185 .get = mixer_ctl_feature_get, 1186 .put = NULL, 1187 }; 1188 1189 /* 1190 * This symbol is exported in order to allow the mixer quirks to 1191 * hook up to the standard feature unit control mechanism 1192 */ 1193 struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl; 1194 1195 /* 1196 * build a feature control 1197 */ 1198 static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str) 1199 { 1200 return strlcat(kctl->id.name, str, sizeof(kctl->id.name)); 1201 } 1202 1203 /* 1204 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we 1205 * rename it to "Headphone". We determine if something is a headphone 1206 * similar to how udev determines form factor. 1207 */ 1208 static void check_no_speaker_on_headset(struct snd_kcontrol *kctl, 1209 struct snd_card *card) 1210 { 1211 const char *names_to_check[] = { 1212 "Headset", "headset", "Headphone", "headphone", NULL}; 1213 const char **s; 1214 bool found = false; 1215 1216 if (strcmp("Speaker", kctl->id.name)) 1217 return; 1218 1219 for (s = names_to_check; *s; s++) 1220 if (strstr(card->shortname, *s)) { 1221 found = true; 1222 break; 1223 } 1224 1225 if (!found) 1226 return; 1227 1228 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name)); 1229 } 1230 1231 static void build_feature_ctl(struct mixer_build *state, void *raw_desc, 1232 unsigned int ctl_mask, int control, 1233 struct usb_audio_term *iterm, int unitid, 1234 int readonly_mask) 1235 { 1236 struct uac_feature_unit_descriptor *desc = raw_desc; 1237 struct usb_feature_control_info *ctl_info; 1238 unsigned int len = 0; 1239 int mapped_name = 0; 1240 int nameid = uac_feature_unit_iFeature(desc); 1241 struct snd_kcontrol *kctl; 1242 struct usb_mixer_elem_info *cval; 1243 const struct usbmix_name_map *map; 1244 unsigned int range; 1245 1246 control++; /* change from zero-based to 1-based value */ 1247 1248 if (control == UAC_FU_GRAPHIC_EQUALIZER) { 1249 /* FIXME: not supported yet */ 1250 return; 1251 } 1252 1253 map = find_map(state, unitid, control); 1254 if (check_ignored_ctl(map)) 1255 return; 1256 1257 cval = kzalloc(sizeof(*cval), GFP_KERNEL); 1258 if (!cval) 1259 return; 1260 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); 1261 cval->control = control; 1262 cval->cmask = ctl_mask; 1263 ctl_info = &audio_feature_info[control-1]; 1264 if (state->mixer->protocol == UAC_VERSION_1) 1265 cval->val_type = ctl_info->type; 1266 else /* UAC_VERSION_2 */ 1267 cval->val_type = ctl_info->type_uac2 >= 0 ? 1268 ctl_info->type_uac2 : ctl_info->type; 1269 1270 if (ctl_mask == 0) { 1271 cval->channels = 1; /* master channel */ 1272 cval->master_readonly = readonly_mask; 1273 } else { 1274 int i, c = 0; 1275 for (i = 0; i < 16; i++) 1276 if (ctl_mask & (1 << i)) 1277 c++; 1278 cval->channels = c; 1279 cval->ch_readonly = readonly_mask; 1280 } 1281 1282 /* 1283 * If all channels in the mask are marked read-only, make the control 1284 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't 1285 * issue write commands to read-only channels. 1286 */ 1287 if (cval->channels == readonly_mask) 1288 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval); 1289 else 1290 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); 1291 1292 if (!kctl) { 1293 usb_audio_err(state->chip, "cannot malloc kcontrol\n"); 1294 kfree(cval); 1295 return; 1296 } 1297 kctl->private_free = snd_usb_mixer_elem_free; 1298 1299 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); 1300 mapped_name = len != 0; 1301 if (!len && nameid) 1302 len = snd_usb_copy_string_desc(state, nameid, 1303 kctl->id.name, sizeof(kctl->id.name)); 1304 1305 switch (control) { 1306 case UAC_FU_MUTE: 1307 case UAC_FU_VOLUME: 1308 /* 1309 * determine the control name. the rule is: 1310 * - if a name id is given in descriptor, use it. 1311 * - if the connected input can be determined, then use the name 1312 * of terminal type. 1313 * - if the connected output can be determined, use it. 1314 * - otherwise, anonymous name. 1315 */ 1316 if (!len) { 1317 len = get_term_name(state, iterm, kctl->id.name, 1318 sizeof(kctl->id.name), 1); 1319 if (!len) 1320 len = get_term_name(state, &state->oterm, 1321 kctl->id.name, 1322 sizeof(kctl->id.name), 1); 1323 if (!len) 1324 snprintf(kctl->id.name, sizeof(kctl->id.name), 1325 "Feature %d", unitid); 1326 } 1327 1328 if (!mapped_name) 1329 check_no_speaker_on_headset(kctl, state->mixer->chip->card); 1330 1331 /* 1332 * determine the stream direction: 1333 * if the connected output is USB stream, then it's likely a 1334 * capture stream. otherwise it should be playback (hopefully :) 1335 */ 1336 if (!mapped_name && !(state->oterm.type >> 16)) { 1337 if ((state->oterm.type & 0xff00) == 0x0100) 1338 append_ctl_name(kctl, " Capture"); 1339 else 1340 append_ctl_name(kctl, " Playback"); 1341 } 1342 append_ctl_name(kctl, control == UAC_FU_MUTE ? 1343 " Switch" : " Volume"); 1344 break; 1345 default: 1346 if (!len) 1347 strlcpy(kctl->id.name, audio_feature_info[control-1].name, 1348 sizeof(kctl->id.name)); 1349 break; 1350 } 1351 1352 /* get min/max values */ 1353 get_min_max_with_quirks(cval, 0, kctl); 1354 1355 if (control == UAC_FU_VOLUME) { 1356 check_mapped_dB(map, cval); 1357 if (cval->dBmin < cval->dBmax || !cval->initialized) { 1358 kctl->tlv.c = snd_usb_mixer_vol_tlv; 1359 kctl->vd[0].access |= 1360 SNDRV_CTL_ELEM_ACCESS_TLV_READ | 1361 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; 1362 } 1363 } 1364 1365 snd_usb_mixer_fu_apply_quirk(state->mixer, cval, unitid, kctl); 1366 1367 range = (cval->max - cval->min) / cval->res; 1368 /* 1369 * Are there devices with volume range more than 255? I use a bit more 1370 * to be sure. 384 is a resolution magic number found on Logitech 1371 * devices. It will definitively catch all buggy Logitech devices. 1372 */ 1373 if (range > 384) { 1374 usb_audio_warn(state->chip, 1375 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.", 1376 range); 1377 usb_audio_warn(state->chip, 1378 "[%d] FU [%s] ch = %d, val = %d/%d/%d", 1379 cval->head.id, kctl->id.name, cval->channels, 1380 cval->min, cval->max, cval->res); 1381 } 1382 1383 usb_audio_dbg(state->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n", 1384 cval->head.id, kctl->id.name, cval->channels, 1385 cval->min, cval->max, cval->res); 1386 snd_usb_mixer_add_control(&cval->head, kctl); 1387 } 1388 1389 static int parse_clock_source_unit(struct mixer_build *state, int unitid, 1390 void *_ftr) 1391 { 1392 struct uac_clock_source_descriptor *hdr = _ftr; 1393 struct usb_mixer_elem_info *cval; 1394 struct snd_kcontrol *kctl; 1395 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 1396 int ret; 1397 1398 if (state->mixer->protocol != UAC_VERSION_2) 1399 return -EINVAL; 1400 1401 if (hdr->bLength != sizeof(*hdr)) { 1402 usb_audio_dbg(state->chip, 1403 "Bogus clock source descriptor length of %d, ignoring.\n", 1404 hdr->bLength); 1405 return 0; 1406 } 1407 1408 /* 1409 * The only property of this unit we are interested in is the 1410 * clock source validity. If that isn't readable, just bail out. 1411 */ 1412 if (!uac2_control_is_readable(hdr->bmControls, 1413 ilog2(UAC2_CS_CONTROL_CLOCK_VALID))) 1414 return 0; 1415 1416 cval = kzalloc(sizeof(*cval), GFP_KERNEL); 1417 if (!cval) 1418 return -ENOMEM; 1419 1420 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID); 1421 1422 cval->min = 0; 1423 cval->max = 1; 1424 cval->channels = 1; 1425 cval->val_type = USB_MIXER_BOOLEAN; 1426 cval->control = UAC2_CS_CONTROL_CLOCK_VALID; 1427 1428 if (uac2_control_is_writeable(hdr->bmControls, 1429 ilog2(UAC2_CS_CONTROL_CLOCK_VALID))) 1430 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); 1431 else { 1432 cval->master_readonly = 1; 1433 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval); 1434 } 1435 1436 if (!kctl) { 1437 kfree(cval); 1438 return -ENOMEM; 1439 } 1440 1441 kctl->private_free = snd_usb_mixer_elem_free; 1442 ret = snd_usb_copy_string_desc(state, hdr->iClockSource, 1443 name, sizeof(name)); 1444 if (ret > 0) 1445 snprintf(kctl->id.name, sizeof(kctl->id.name), 1446 "%s Validity", name); 1447 else 1448 snprintf(kctl->id.name, sizeof(kctl->id.name), 1449 "Clock Source %d Validity", hdr->bClockID); 1450 1451 return snd_usb_mixer_add_control(&cval->head, kctl); 1452 } 1453 1454 /* 1455 * parse a feature unit 1456 * 1457 * most of controls are defined here. 1458 */ 1459 static int parse_audio_feature_unit(struct mixer_build *state, int unitid, 1460 void *_ftr) 1461 { 1462 int channels, i, j; 1463 struct usb_audio_term iterm; 1464 unsigned int master_bits, first_ch_bits; 1465 int err, csize; 1466 struct uac_feature_unit_descriptor *hdr = _ftr; 1467 __u8 *bmaControls; 1468 1469 if (state->mixer->protocol == UAC_VERSION_1) { 1470 csize = hdr->bControlSize; 1471 if (!csize) { 1472 usb_audio_dbg(state->chip, 1473 "unit %u: invalid bControlSize == 0\n", 1474 unitid); 1475 return -EINVAL; 1476 } 1477 channels = (hdr->bLength - 7) / csize - 1; 1478 bmaControls = hdr->bmaControls; 1479 if (hdr->bLength < 7 + csize) { 1480 usb_audio_err(state->chip, 1481 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n", 1482 unitid); 1483 return -EINVAL; 1484 } 1485 } else { 1486 struct uac2_feature_unit_descriptor *ftr = _ftr; 1487 csize = 4; 1488 channels = (hdr->bLength - 6) / 4 - 1; 1489 bmaControls = ftr->bmaControls; 1490 if (hdr->bLength < 6 + csize) { 1491 usb_audio_err(state->chip, 1492 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n", 1493 unitid); 1494 return -EINVAL; 1495 } 1496 } 1497 1498 /* parse the source unit */ 1499 if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0) 1500 return err; 1501 1502 /* determine the input source type and name */ 1503 err = check_input_term(state, hdr->bSourceID, &iterm); 1504 if (err < 0) 1505 return err; 1506 1507 master_bits = snd_usb_combine_bytes(bmaControls, csize); 1508 /* master configuration quirks */ 1509 switch (state->chip->usb_id) { 1510 case USB_ID(0x08bb, 0x2702): 1511 usb_audio_info(state->chip, 1512 "usbmixer: master volume quirk for PCM2702 chip\n"); 1513 /* disable non-functional volume control */ 1514 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME); 1515 break; 1516 case USB_ID(0x1130, 0xf211): 1517 usb_audio_info(state->chip, 1518 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n"); 1519 /* disable non-functional volume control */ 1520 channels = 0; 1521 break; 1522 1523 } 1524 if (channels > 0) 1525 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize); 1526 else 1527 first_ch_bits = 0; 1528 1529 if (state->mixer->protocol == UAC_VERSION_1) { 1530 /* check all control types */ 1531 for (i = 0; i < 10; i++) { 1532 unsigned int ch_bits = 0; 1533 for (j = 0; j < channels; j++) { 1534 unsigned int mask; 1535 1536 mask = snd_usb_combine_bytes(bmaControls + 1537 csize * (j+1), csize); 1538 if (mask & (1 << i)) 1539 ch_bits |= (1 << j); 1540 } 1541 /* audio class v1 controls are never read-only */ 1542 1543 /* 1544 * The first channel must be set 1545 * (for ease of programming). 1546 */ 1547 if (ch_bits & 1) 1548 build_feature_ctl(state, _ftr, ch_bits, i, 1549 &iterm, unitid, 0); 1550 if (master_bits & (1 << i)) 1551 build_feature_ctl(state, _ftr, 0, i, &iterm, 1552 unitid, 0); 1553 } 1554 } else { /* UAC_VERSION_2 */ 1555 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) { 1556 unsigned int ch_bits = 0; 1557 unsigned int ch_read_only = 0; 1558 1559 for (j = 0; j < channels; j++) { 1560 unsigned int mask; 1561 1562 mask = snd_usb_combine_bytes(bmaControls + 1563 csize * (j+1), csize); 1564 if (uac2_control_is_readable(mask, i)) { 1565 ch_bits |= (1 << j); 1566 if (!uac2_control_is_writeable(mask, i)) 1567 ch_read_only |= (1 << j); 1568 } 1569 } 1570 1571 /* 1572 * NOTE: build_feature_ctl() will mark the control 1573 * read-only if all channels are marked read-only in 1574 * the descriptors. Otherwise, the control will be 1575 * reported as writeable, but the driver will not 1576 * actually issue a write command for read-only 1577 * channels. 1578 */ 1579 1580 /* 1581 * The first channel must be set 1582 * (for ease of programming). 1583 */ 1584 if (ch_bits & 1) 1585 build_feature_ctl(state, _ftr, ch_bits, i, 1586 &iterm, unitid, ch_read_only); 1587 if (uac2_control_is_readable(master_bits, i)) 1588 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, 1589 !uac2_control_is_writeable(master_bits, i)); 1590 } 1591 } 1592 1593 return 0; 1594 } 1595 1596 /* 1597 * Mixer Unit 1598 */ 1599 1600 /* 1601 * build a mixer unit control 1602 * 1603 * the callbacks are identical with feature unit. 1604 * input channel number (zero based) is given in control field instead. 1605 */ 1606 static void build_mixer_unit_ctl(struct mixer_build *state, 1607 struct uac_mixer_unit_descriptor *desc, 1608 int in_pin, int in_ch, int unitid, 1609 struct usb_audio_term *iterm) 1610 { 1611 struct usb_mixer_elem_info *cval; 1612 unsigned int num_outs = uac_mixer_unit_bNrChannels(desc); 1613 unsigned int i, len; 1614 struct snd_kcontrol *kctl; 1615 const struct usbmix_name_map *map; 1616 1617 map = find_map(state, unitid, 0); 1618 if (check_ignored_ctl(map)) 1619 return; 1620 1621 cval = kzalloc(sizeof(*cval), GFP_KERNEL); 1622 if (!cval) 1623 return; 1624 1625 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); 1626 cval->control = in_ch + 1; /* based on 1 */ 1627 cval->val_type = USB_MIXER_S16; 1628 for (i = 0; i < num_outs; i++) { 1629 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol); 1630 1631 if (check_matrix_bitmap(c, in_ch, i, num_outs)) { 1632 cval->cmask |= (1 << i); 1633 cval->channels++; 1634 } 1635 } 1636 1637 /* get min/max values */ 1638 get_min_max(cval, 0); 1639 1640 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); 1641 if (!kctl) { 1642 usb_audio_err(state->chip, "cannot malloc kcontrol\n"); 1643 kfree(cval); 1644 return; 1645 } 1646 kctl->private_free = snd_usb_mixer_elem_free; 1647 1648 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); 1649 if (!len) 1650 len = get_term_name(state, iterm, kctl->id.name, 1651 sizeof(kctl->id.name), 0); 1652 if (!len) 1653 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1); 1654 append_ctl_name(kctl, " Volume"); 1655 1656 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n", 1657 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max); 1658 snd_usb_mixer_add_control(&cval->head, kctl); 1659 } 1660 1661 /* 1662 * parse a mixer unit 1663 */ 1664 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, 1665 void *raw_desc) 1666 { 1667 struct uac_mixer_unit_descriptor *desc = raw_desc; 1668 struct usb_audio_term iterm; 1669 int input_pins, num_ins, num_outs; 1670 int pin, ich, err; 1671 1672 if (desc->bLength < 11 || !(input_pins = desc->bNrInPins) || 1673 !(num_outs = uac_mixer_unit_bNrChannels(desc))) { 1674 usb_audio_err(state->chip, 1675 "invalid MIXER UNIT descriptor %d\n", 1676 unitid); 1677 return -EINVAL; 1678 } 1679 1680 num_ins = 0; 1681 ich = 0; 1682 for (pin = 0; pin < input_pins; pin++) { 1683 err = parse_audio_unit(state, desc->baSourceID[pin]); 1684 if (err < 0) 1685 continue; 1686 /* no bmControls field (e.g. Maya44) -> ignore */ 1687 if (desc->bLength <= 10 + input_pins) 1688 continue; 1689 err = check_input_term(state, desc->baSourceID[pin], &iterm); 1690 if (err < 0) 1691 return err; 1692 num_ins += iterm.channels; 1693 for (; ich < num_ins; ich++) { 1694 int och, ich_has_controls = 0; 1695 1696 for (och = 0; och < num_outs; och++) { 1697 __u8 *c = uac_mixer_unit_bmControls(desc, 1698 state->mixer->protocol); 1699 1700 if (check_matrix_bitmap(c, ich, och, num_outs)) { 1701 ich_has_controls = 1; 1702 break; 1703 } 1704 } 1705 if (ich_has_controls) 1706 build_mixer_unit_ctl(state, desc, pin, ich, 1707 unitid, &iterm); 1708 } 1709 } 1710 return 0; 1711 } 1712 1713 /* 1714 * Processing Unit / Extension Unit 1715 */ 1716 1717 /* get callback for processing/extension unit */ 1718 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, 1719 struct snd_ctl_elem_value *ucontrol) 1720 { 1721 struct usb_mixer_elem_info *cval = kcontrol->private_data; 1722 int err, val; 1723 1724 err = get_cur_ctl_value(cval, cval->control << 8, &val); 1725 if (err < 0) { 1726 ucontrol->value.integer.value[0] = cval->min; 1727 return filter_error(cval, err); 1728 } 1729 val = get_relative_value(cval, val); 1730 ucontrol->value.integer.value[0] = val; 1731 return 0; 1732 } 1733 1734 /* put callback for processing/extension unit */ 1735 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, 1736 struct snd_ctl_elem_value *ucontrol) 1737 { 1738 struct usb_mixer_elem_info *cval = kcontrol->private_data; 1739 int val, oval, err; 1740 1741 err = get_cur_ctl_value(cval, cval->control << 8, &oval); 1742 if (err < 0) 1743 return filter_error(cval, err); 1744 val = ucontrol->value.integer.value[0]; 1745 val = get_abs_value(cval, val); 1746 if (val != oval) { 1747 set_cur_ctl_value(cval, cval->control << 8, val); 1748 return 1; 1749 } 1750 return 0; 1751 } 1752 1753 /* alsa control interface for processing/extension unit */ 1754 static const struct snd_kcontrol_new mixer_procunit_ctl = { 1755 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1756 .name = "", /* will be filled later */ 1757 .info = mixer_ctl_feature_info, 1758 .get = mixer_ctl_procunit_get, 1759 .put = mixer_ctl_procunit_put, 1760 }; 1761 1762 /* 1763 * predefined data for processing units 1764 */ 1765 struct procunit_value_info { 1766 int control; 1767 char *suffix; 1768 int val_type; 1769 int min_value; 1770 }; 1771 1772 struct procunit_info { 1773 int type; 1774 char *name; 1775 struct procunit_value_info *values; 1776 }; 1777 1778 static struct procunit_value_info updown_proc_info[] = { 1779 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN }, 1780 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, 1781 { 0 } 1782 }; 1783 static struct procunit_value_info prologic_proc_info[] = { 1784 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN }, 1785 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, 1786 { 0 } 1787 }; 1788 static struct procunit_value_info threed_enh_proc_info[] = { 1789 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN }, 1790 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 }, 1791 { 0 } 1792 }; 1793 static struct procunit_value_info reverb_proc_info[] = { 1794 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN }, 1795 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 }, 1796 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 }, 1797 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 }, 1798 { 0 } 1799 }; 1800 static struct procunit_value_info chorus_proc_info[] = { 1801 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN }, 1802 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 }, 1803 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 }, 1804 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 }, 1805 { 0 } 1806 }; 1807 static struct procunit_value_info dcr_proc_info[] = { 1808 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN }, 1809 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 }, 1810 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 }, 1811 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 }, 1812 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 }, 1813 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 }, 1814 { 0 } 1815 }; 1816 1817 static struct procunit_info procunits[] = { 1818 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info }, 1819 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info }, 1820 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info }, 1821 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info }, 1822 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info }, 1823 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info }, 1824 { 0 }, 1825 }; 1826 /* 1827 * predefined data for extension units 1828 */ 1829 static struct procunit_value_info clock_rate_xu_info[] = { 1830 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 }, 1831 { 0 } 1832 }; 1833 static struct procunit_value_info clock_source_xu_info[] = { 1834 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN }, 1835 { 0 } 1836 }; 1837 static struct procunit_value_info spdif_format_xu_info[] = { 1838 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN }, 1839 { 0 } 1840 }; 1841 static struct procunit_value_info soft_limit_xu_info[] = { 1842 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN }, 1843 { 0 } 1844 }; 1845 static struct procunit_info extunits[] = { 1846 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info }, 1847 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info }, 1848 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info }, 1849 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info }, 1850 { 0 } 1851 }; 1852 1853 /* 1854 * build a processing/extension unit 1855 */ 1856 static int build_audio_procunit(struct mixer_build *state, int unitid, 1857 void *raw_desc, struct procunit_info *list, 1858 char *name) 1859 { 1860 struct uac_processing_unit_descriptor *desc = raw_desc; 1861 int num_ins = desc->bNrInPins; 1862 struct usb_mixer_elem_info *cval; 1863 struct snd_kcontrol *kctl; 1864 int i, err, nameid, type, len; 1865 struct procunit_info *info; 1866 struct procunit_value_info *valinfo; 1867 const struct usbmix_name_map *map; 1868 static struct procunit_value_info default_value_info[] = { 1869 { 0x01, "Switch", USB_MIXER_BOOLEAN }, 1870 { 0 } 1871 }; 1872 static struct procunit_info default_info = { 1873 0, NULL, default_value_info 1874 }; 1875 1876 if (desc->bLength < 13 || desc->bLength < 13 + num_ins || 1877 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) { 1878 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid); 1879 return -EINVAL; 1880 } 1881 1882 for (i = 0; i < num_ins; i++) { 1883 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0) 1884 return err; 1885 } 1886 1887 type = le16_to_cpu(desc->wProcessType); 1888 for (info = list; info && info->type; info++) 1889 if (info->type == type) 1890 break; 1891 if (!info || !info->type) 1892 info = &default_info; 1893 1894 for (valinfo = info->values; valinfo->control; valinfo++) { 1895 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol); 1896 1897 if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1)))) 1898 continue; 1899 map = find_map(state, unitid, valinfo->control); 1900 if (check_ignored_ctl(map)) 1901 continue; 1902 cval = kzalloc(sizeof(*cval), GFP_KERNEL); 1903 if (!cval) 1904 return -ENOMEM; 1905 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); 1906 cval->control = valinfo->control; 1907 cval->val_type = valinfo->val_type; 1908 cval->channels = 1; 1909 1910 /* get min/max values */ 1911 if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) { 1912 __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol); 1913 /* FIXME: hard-coded */ 1914 cval->min = 1; 1915 cval->max = control_spec[0]; 1916 cval->res = 1; 1917 cval->initialized = 1; 1918 } else { 1919 if (type == USB_XU_CLOCK_RATE) { 1920 /* 1921 * E-Mu USB 0404/0202/TrackerPre/0204 1922 * samplerate control quirk 1923 */ 1924 cval->min = 0; 1925 cval->max = 5; 1926 cval->res = 1; 1927 cval->initialized = 1; 1928 } else 1929 get_min_max(cval, valinfo->min_value); 1930 } 1931 1932 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval); 1933 if (!kctl) { 1934 kfree(cval); 1935 return -ENOMEM; 1936 } 1937 kctl->private_free = snd_usb_mixer_elem_free; 1938 1939 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) { 1940 /* nothing */ ; 1941 } else if (info->name) { 1942 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name)); 1943 } else { 1944 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol); 1945 len = 0; 1946 if (nameid) 1947 len = snd_usb_copy_string_desc(state, nameid, 1948 kctl->id.name, 1949 sizeof(kctl->id.name)); 1950 if (!len) 1951 strlcpy(kctl->id.name, name, sizeof(kctl->id.name)); 1952 } 1953 append_ctl_name(kctl, " "); 1954 append_ctl_name(kctl, valinfo->suffix); 1955 1956 usb_audio_dbg(state->chip, 1957 "[%d] PU [%s] ch = %d, val = %d/%d\n", 1958 cval->head.id, kctl->id.name, cval->channels, 1959 cval->min, cval->max); 1960 1961 err = snd_usb_mixer_add_control(&cval->head, kctl); 1962 if (err < 0) 1963 return err; 1964 } 1965 return 0; 1966 } 1967 1968 static int parse_audio_processing_unit(struct mixer_build *state, int unitid, 1969 void *raw_desc) 1970 { 1971 return build_audio_procunit(state, unitid, raw_desc, 1972 procunits, "Processing Unit"); 1973 } 1974 1975 static int parse_audio_extension_unit(struct mixer_build *state, int unitid, 1976 void *raw_desc) 1977 { 1978 /* 1979 * Note that we parse extension units with processing unit descriptors. 1980 * That's ok as the layout is the same. 1981 */ 1982 return build_audio_procunit(state, unitid, raw_desc, 1983 extunits, "Extension Unit"); 1984 } 1985 1986 /* 1987 * Selector Unit 1988 */ 1989 1990 /* 1991 * info callback for selector unit 1992 * use an enumerator type for routing 1993 */ 1994 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, 1995 struct snd_ctl_elem_info *uinfo) 1996 { 1997 struct usb_mixer_elem_info *cval = kcontrol->private_data; 1998 const char **itemlist = (const char **)kcontrol->private_value; 1999 2000 if (snd_BUG_ON(!itemlist)) 2001 return -EINVAL; 2002 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist); 2003 } 2004 2005 /* get callback for selector unit */ 2006 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, 2007 struct snd_ctl_elem_value *ucontrol) 2008 { 2009 struct usb_mixer_elem_info *cval = kcontrol->private_data; 2010 int val, err; 2011 2012 err = get_cur_ctl_value(cval, cval->control << 8, &val); 2013 if (err < 0) { 2014 ucontrol->value.enumerated.item[0] = 0; 2015 return filter_error(cval, err); 2016 } 2017 val = get_relative_value(cval, val); 2018 ucontrol->value.enumerated.item[0] = val; 2019 return 0; 2020 } 2021 2022 /* put callback for selector unit */ 2023 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, 2024 struct snd_ctl_elem_value *ucontrol) 2025 { 2026 struct usb_mixer_elem_info *cval = kcontrol->private_data; 2027 int val, oval, err; 2028 2029 err = get_cur_ctl_value(cval, cval->control << 8, &oval); 2030 if (err < 0) 2031 return filter_error(cval, err); 2032 val = ucontrol->value.enumerated.item[0]; 2033 val = get_abs_value(cval, val); 2034 if (val != oval) { 2035 set_cur_ctl_value(cval, cval->control << 8, val); 2036 return 1; 2037 } 2038 return 0; 2039 } 2040 2041 /* alsa control interface for selector unit */ 2042 static const struct snd_kcontrol_new mixer_selectunit_ctl = { 2043 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2044 .name = "", /* will be filled later */ 2045 .info = mixer_ctl_selector_info, 2046 .get = mixer_ctl_selector_get, 2047 .put = mixer_ctl_selector_put, 2048 }; 2049 2050 /* 2051 * private free callback. 2052 * free both private_data and private_value 2053 */ 2054 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl) 2055 { 2056 int i, num_ins = 0; 2057 2058 if (kctl->private_data) { 2059 struct usb_mixer_elem_info *cval = kctl->private_data; 2060 num_ins = cval->max; 2061 kfree(cval); 2062 kctl->private_data = NULL; 2063 } 2064 if (kctl->private_value) { 2065 char **itemlist = (char **)kctl->private_value; 2066 for (i = 0; i < num_ins; i++) 2067 kfree(itemlist[i]); 2068 kfree(itemlist); 2069 kctl->private_value = 0; 2070 } 2071 } 2072 2073 /* 2074 * parse a selector unit 2075 */ 2076 static int parse_audio_selector_unit(struct mixer_build *state, int unitid, 2077 void *raw_desc) 2078 { 2079 struct uac_selector_unit_descriptor *desc = raw_desc; 2080 unsigned int i, nameid, len; 2081 int err; 2082 struct usb_mixer_elem_info *cval; 2083 struct snd_kcontrol *kctl; 2084 const struct usbmix_name_map *map; 2085 char **namelist; 2086 2087 if (!desc->bNrInPins || desc->bLength < 5 + desc->bNrInPins) { 2088 usb_audio_err(state->chip, 2089 "invalid SELECTOR UNIT descriptor %d\n", unitid); 2090 return -EINVAL; 2091 } 2092 2093 for (i = 0; i < desc->bNrInPins; i++) { 2094 if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0) 2095 return err; 2096 } 2097 2098 if (desc->bNrInPins == 1) /* only one ? nonsense! */ 2099 return 0; 2100 2101 map = find_map(state, unitid, 0); 2102 if (check_ignored_ctl(map)) 2103 return 0; 2104 2105 cval = kzalloc(sizeof(*cval), GFP_KERNEL); 2106 if (!cval) 2107 return -ENOMEM; 2108 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); 2109 cval->val_type = USB_MIXER_U8; 2110 cval->channels = 1; 2111 cval->min = 1; 2112 cval->max = desc->bNrInPins; 2113 cval->res = 1; 2114 cval->initialized = 1; 2115 2116 if (state->mixer->protocol == UAC_VERSION_1) 2117 cval->control = 0; 2118 else /* UAC_VERSION_2 */ 2119 cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ? 2120 UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR; 2121 2122 namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL); 2123 if (!namelist) { 2124 kfree(cval); 2125 return -ENOMEM; 2126 } 2127 #define MAX_ITEM_NAME_LEN 64 2128 for (i = 0; i < desc->bNrInPins; i++) { 2129 struct usb_audio_term iterm; 2130 len = 0; 2131 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL); 2132 if (!namelist[i]) { 2133 while (i--) 2134 kfree(namelist[i]); 2135 kfree(namelist); 2136 kfree(cval); 2137 return -ENOMEM; 2138 } 2139 len = check_mapped_selector_name(state, unitid, i, namelist[i], 2140 MAX_ITEM_NAME_LEN); 2141 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0) 2142 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0); 2143 if (! len) 2144 sprintf(namelist[i], "Input %u", i); 2145 } 2146 2147 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval); 2148 if (! kctl) { 2149 usb_audio_err(state->chip, "cannot malloc kcontrol\n"); 2150 kfree(namelist); 2151 kfree(cval); 2152 return -ENOMEM; 2153 } 2154 kctl->private_value = (unsigned long)namelist; 2155 kctl->private_free = usb_mixer_selector_elem_free; 2156 2157 nameid = uac_selector_unit_iSelector(desc); 2158 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); 2159 if (len) 2160 ; 2161 else if (nameid) 2162 snd_usb_copy_string_desc(state, nameid, kctl->id.name, 2163 sizeof(kctl->id.name)); 2164 else { 2165 len = get_term_name(state, &state->oterm, 2166 kctl->id.name, sizeof(kctl->id.name), 0); 2167 if (!len) 2168 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name)); 2169 2170 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) 2171 append_ctl_name(kctl, " Clock Source"); 2172 else if ((state->oterm.type & 0xff00) == 0x0100) 2173 append_ctl_name(kctl, " Capture Source"); 2174 else 2175 append_ctl_name(kctl, " Playback Source"); 2176 } 2177 2178 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n", 2179 cval->head.id, kctl->id.name, desc->bNrInPins); 2180 return snd_usb_mixer_add_control(&cval->head, kctl); 2181 } 2182 2183 /* 2184 * parse an audio unit recursively 2185 */ 2186 2187 static int parse_audio_unit(struct mixer_build *state, int unitid) 2188 { 2189 unsigned char *p1; 2190 2191 if (test_and_set_bit(unitid, state->unitbitmap)) 2192 return 0; /* the unit already visited */ 2193 2194 p1 = find_audio_control_unit(state, unitid); 2195 if (!p1) { 2196 usb_audio_err(state->chip, "unit %d not found!\n", unitid); 2197 return -EINVAL; 2198 } 2199 2200 switch (p1[2]) { 2201 case UAC_INPUT_TERMINAL: 2202 return 0; /* NOP */ 2203 case UAC_MIXER_UNIT: 2204 return parse_audio_mixer_unit(state, unitid, p1); 2205 case UAC2_CLOCK_SOURCE: 2206 return parse_clock_source_unit(state, unitid, p1); 2207 case UAC_SELECTOR_UNIT: 2208 case UAC2_CLOCK_SELECTOR: 2209 return parse_audio_selector_unit(state, unitid, p1); 2210 case UAC_FEATURE_UNIT: 2211 return parse_audio_feature_unit(state, unitid, p1); 2212 case UAC1_PROCESSING_UNIT: 2213 /* UAC2_EFFECT_UNIT has the same value */ 2214 if (state->mixer->protocol == UAC_VERSION_1) 2215 return parse_audio_processing_unit(state, unitid, p1); 2216 else 2217 return 0; /* FIXME - effect units not implemented yet */ 2218 case UAC1_EXTENSION_UNIT: 2219 /* UAC2_PROCESSING_UNIT_V2 has the same value */ 2220 if (state->mixer->protocol == UAC_VERSION_1) 2221 return parse_audio_extension_unit(state, unitid, p1); 2222 else /* UAC_VERSION_2 */ 2223 return parse_audio_processing_unit(state, unitid, p1); 2224 case UAC2_EXTENSION_UNIT_V2: 2225 return parse_audio_extension_unit(state, unitid, p1); 2226 default: 2227 usb_audio_err(state->chip, 2228 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]); 2229 return -EINVAL; 2230 } 2231 } 2232 2233 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer) 2234 { 2235 kfree(mixer->id_elems); 2236 if (mixer->urb) { 2237 kfree(mixer->urb->transfer_buffer); 2238 usb_free_urb(mixer->urb); 2239 } 2240 usb_free_urb(mixer->rc_urb); 2241 kfree(mixer->rc_setup_packet); 2242 kfree(mixer); 2243 } 2244 2245 static int snd_usb_mixer_dev_free(struct snd_device *device) 2246 { 2247 struct usb_mixer_interface *mixer = device->device_data; 2248 snd_usb_mixer_free(mixer); 2249 return 0; 2250 } 2251 2252 /* 2253 * create mixer controls 2254 * 2255 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers 2256 */ 2257 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer) 2258 { 2259 struct mixer_build state; 2260 int err; 2261 const struct usbmix_ctl_map *map; 2262 void *p; 2263 2264 memset(&state, 0, sizeof(state)); 2265 state.chip = mixer->chip; 2266 state.mixer = mixer; 2267 state.buffer = mixer->hostif->extra; 2268 state.buflen = mixer->hostif->extralen; 2269 2270 /* check the mapping table */ 2271 for (map = usbmix_ctl_maps; map->id; map++) { 2272 if (map->id == state.chip->usb_id) { 2273 state.map = map->map; 2274 state.selector_map = map->selector_map; 2275 mixer->ignore_ctl_error = map->ignore_ctl_error; 2276 break; 2277 } 2278 } 2279 2280 p = NULL; 2281 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra, 2282 mixer->hostif->extralen, 2283 p, UAC_OUTPUT_TERMINAL)) != NULL) { 2284 if (mixer->protocol == UAC_VERSION_1) { 2285 struct uac1_output_terminal_descriptor *desc = p; 2286 2287 if (desc->bLength < sizeof(*desc)) 2288 continue; /* invalid descriptor? */ 2289 /* mark terminal ID as visited */ 2290 set_bit(desc->bTerminalID, state.unitbitmap); 2291 state.oterm.id = desc->bTerminalID; 2292 state.oterm.type = le16_to_cpu(desc->wTerminalType); 2293 state.oterm.name = desc->iTerminal; 2294 err = parse_audio_unit(&state, desc->bSourceID); 2295 if (err < 0 && err != -EINVAL) 2296 return err; 2297 } else { /* UAC_VERSION_2 */ 2298 struct uac2_output_terminal_descriptor *desc = p; 2299 2300 if (desc->bLength < sizeof(*desc)) 2301 continue; /* invalid descriptor? */ 2302 /* mark terminal ID as visited */ 2303 set_bit(desc->bTerminalID, state.unitbitmap); 2304 state.oterm.id = desc->bTerminalID; 2305 state.oterm.type = le16_to_cpu(desc->wTerminalType); 2306 state.oterm.name = desc->iTerminal; 2307 err = parse_audio_unit(&state, desc->bSourceID); 2308 if (err < 0 && err != -EINVAL) 2309 return err; 2310 2311 /* 2312 * For UAC2, use the same approach to also add the 2313 * clock selectors 2314 */ 2315 err = parse_audio_unit(&state, desc->bCSourceID); 2316 if (err < 0 && err != -EINVAL) 2317 return err; 2318 } 2319 } 2320 2321 return 0; 2322 } 2323 2324 void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid) 2325 { 2326 struct usb_mixer_elem_list *list; 2327 2328 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) 2329 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, 2330 &list->kctl->id); 2331 } 2332 2333 static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer, 2334 struct usb_mixer_elem_list *list) 2335 { 2336 struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list; 2337 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN", 2338 "S8", "U8", "S16", "U16"}; 2339 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, " 2340 "channels=%i, type=\"%s\"\n", cval->head.id, 2341 cval->control, cval->cmask, cval->channels, 2342 val_types[cval->val_type]); 2343 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n", 2344 cval->min, cval->max, cval->dBmin, cval->dBmax); 2345 } 2346 2347 static void snd_usb_mixer_proc_read(struct snd_info_entry *entry, 2348 struct snd_info_buffer *buffer) 2349 { 2350 struct snd_usb_audio *chip = entry->private_data; 2351 struct usb_mixer_interface *mixer; 2352 struct usb_mixer_elem_list *list; 2353 int unitid; 2354 2355 list_for_each_entry(mixer, &chip->mixer_list, list) { 2356 snd_iprintf(buffer, 2357 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n", 2358 chip->usb_id, snd_usb_ctrl_intf(chip), 2359 mixer->ignore_ctl_error); 2360 snd_iprintf(buffer, "Card: %s\n", chip->card->longname); 2361 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) { 2362 for (list = mixer->id_elems[unitid]; list; 2363 list = list->next_id_elem) { 2364 snd_iprintf(buffer, " Unit: %i\n", list->id); 2365 if (list->kctl) 2366 snd_iprintf(buffer, 2367 " Control: name=\"%s\", index=%i\n", 2368 list->kctl->id.name, 2369 list->kctl->id.index); 2370 if (list->dump) 2371 list->dump(buffer, list); 2372 } 2373 } 2374 } 2375 } 2376 2377 static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer, 2378 int attribute, int value, int index) 2379 { 2380 struct usb_mixer_elem_list *list; 2381 __u8 unitid = (index >> 8) & 0xff; 2382 __u8 control = (value >> 8) & 0xff; 2383 __u8 channel = value & 0xff; 2384 unsigned int count = 0; 2385 2386 if (channel >= MAX_CHANNELS) { 2387 usb_audio_dbg(mixer->chip, 2388 "%s(): bogus channel number %d\n", 2389 __func__, channel); 2390 return; 2391 } 2392 2393 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) 2394 count++; 2395 2396 if (count == 0) 2397 return; 2398 2399 for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) { 2400 struct usb_mixer_elem_info *info; 2401 2402 if (!list->kctl) 2403 continue; 2404 2405 info = (struct usb_mixer_elem_info *)list; 2406 if (count > 1 && info->control != control) 2407 continue; 2408 2409 switch (attribute) { 2410 case UAC2_CS_CUR: 2411 /* invalidate cache, so the value is read from the device */ 2412 if (channel) 2413 info->cached &= ~(1 << channel); 2414 else /* master channel */ 2415 info->cached = 0; 2416 2417 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, 2418 &info->head.kctl->id); 2419 break; 2420 2421 case UAC2_CS_RANGE: 2422 /* TODO */ 2423 break; 2424 2425 case UAC2_CS_MEM: 2426 /* TODO */ 2427 break; 2428 2429 default: 2430 usb_audio_dbg(mixer->chip, 2431 "unknown attribute %d in interrupt\n", 2432 attribute); 2433 break; 2434 } /* switch */ 2435 } 2436 } 2437 2438 static void snd_usb_mixer_interrupt(struct urb *urb) 2439 { 2440 struct usb_mixer_interface *mixer = urb->context; 2441 int len = urb->actual_length; 2442 int ustatus = urb->status; 2443 2444 if (ustatus != 0) 2445 goto requeue; 2446 2447 if (mixer->protocol == UAC_VERSION_1) { 2448 struct uac1_status_word *status; 2449 2450 for (status = urb->transfer_buffer; 2451 len >= sizeof(*status); 2452 len -= sizeof(*status), status++) { 2453 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n", 2454 status->bStatusType, 2455 status->bOriginator); 2456 2457 /* ignore any notifications not from the control interface */ 2458 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) != 2459 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF) 2460 continue; 2461 2462 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED) 2463 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator); 2464 else 2465 snd_usb_mixer_notify_id(mixer, status->bOriginator); 2466 } 2467 } else { /* UAC_VERSION_2 */ 2468 struct uac2_interrupt_data_msg *msg; 2469 2470 for (msg = urb->transfer_buffer; 2471 len >= sizeof(*msg); 2472 len -= sizeof(*msg), msg++) { 2473 /* drop vendor specific and endpoint requests */ 2474 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) || 2475 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP)) 2476 continue; 2477 2478 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute, 2479 le16_to_cpu(msg->wValue), 2480 le16_to_cpu(msg->wIndex)); 2481 } 2482 } 2483 2484 requeue: 2485 if (ustatus != -ENOENT && 2486 ustatus != -ECONNRESET && 2487 ustatus != -ESHUTDOWN) { 2488 urb->dev = mixer->chip->dev; 2489 usb_submit_urb(urb, GFP_ATOMIC); 2490 } 2491 } 2492 2493 /* create the handler for the optional status interrupt endpoint */ 2494 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer) 2495 { 2496 struct usb_endpoint_descriptor *ep; 2497 void *transfer_buffer; 2498 int buffer_length; 2499 unsigned int epnum; 2500 2501 /* we need one interrupt input endpoint */ 2502 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1) 2503 return 0; 2504 ep = get_endpoint(mixer->hostif, 0); 2505 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep)) 2506 return 0; 2507 2508 epnum = usb_endpoint_num(ep); 2509 buffer_length = le16_to_cpu(ep->wMaxPacketSize); 2510 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL); 2511 if (!transfer_buffer) 2512 return -ENOMEM; 2513 mixer->urb = usb_alloc_urb(0, GFP_KERNEL); 2514 if (!mixer->urb) { 2515 kfree(transfer_buffer); 2516 return -ENOMEM; 2517 } 2518 usb_fill_int_urb(mixer->urb, mixer->chip->dev, 2519 usb_rcvintpipe(mixer->chip->dev, epnum), 2520 transfer_buffer, buffer_length, 2521 snd_usb_mixer_interrupt, mixer, ep->bInterval); 2522 usb_submit_urb(mixer->urb, GFP_KERNEL); 2523 return 0; 2524 } 2525 2526 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif, 2527 int ignore_error) 2528 { 2529 static struct snd_device_ops dev_ops = { 2530 .dev_free = snd_usb_mixer_dev_free 2531 }; 2532 struct usb_mixer_interface *mixer; 2533 struct snd_info_entry *entry; 2534 int err; 2535 2536 strcpy(chip->card->mixername, "USB Mixer"); 2537 2538 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL); 2539 if (!mixer) 2540 return -ENOMEM; 2541 mixer->chip = chip; 2542 mixer->ignore_ctl_error = ignore_error; 2543 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems), 2544 GFP_KERNEL); 2545 if (!mixer->id_elems) { 2546 kfree(mixer); 2547 return -ENOMEM; 2548 } 2549 2550 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0]; 2551 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) { 2552 case UAC_VERSION_1: 2553 default: 2554 mixer->protocol = UAC_VERSION_1; 2555 break; 2556 case UAC_VERSION_2: 2557 mixer->protocol = UAC_VERSION_2; 2558 break; 2559 } 2560 2561 if ((err = snd_usb_mixer_controls(mixer)) < 0 || 2562 (err = snd_usb_mixer_status_create(mixer)) < 0) 2563 goto _error; 2564 2565 snd_usb_mixer_apply_create_quirk(mixer); 2566 2567 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops); 2568 if (err < 0) 2569 goto _error; 2570 2571 if (list_empty(&chip->mixer_list) && 2572 !snd_card_proc_new(chip->card, "usbmixer", &entry)) 2573 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read); 2574 2575 list_add(&mixer->list, &chip->mixer_list); 2576 return 0; 2577 2578 _error: 2579 snd_usb_mixer_free(mixer); 2580 return err; 2581 } 2582 2583 void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer) 2584 { 2585 usb_kill_urb(mixer->urb); 2586 usb_kill_urb(mixer->rc_urb); 2587 } 2588 2589 #ifdef CONFIG_PM 2590 /* stop any bus activity of a mixer */ 2591 static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer) 2592 { 2593 usb_kill_urb(mixer->urb); 2594 usb_kill_urb(mixer->rc_urb); 2595 } 2596 2597 static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer) 2598 { 2599 int err; 2600 2601 if (mixer->urb) { 2602 err = usb_submit_urb(mixer->urb, GFP_NOIO); 2603 if (err < 0) 2604 return err; 2605 } 2606 2607 return 0; 2608 } 2609 2610 int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer) 2611 { 2612 snd_usb_mixer_inactivate(mixer); 2613 return 0; 2614 } 2615 2616 static int restore_mixer_value(struct usb_mixer_elem_list *list) 2617 { 2618 struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list; 2619 int c, err, idx; 2620 2621 if (cval->cmask) { 2622 idx = 0; 2623 for (c = 0; c < MAX_CHANNELS; c++) { 2624 if (!(cval->cmask & (1 << c))) 2625 continue; 2626 if (cval->cached & (1 << (c + 1))) { 2627 err = snd_usb_set_cur_mix_value(cval, c + 1, idx, 2628 cval->cache_val[idx]); 2629 if (err < 0) 2630 return err; 2631 } 2632 idx++; 2633 } 2634 } else { 2635 /* master */ 2636 if (cval->cached) { 2637 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val); 2638 if (err < 0) 2639 return err; 2640 } 2641 } 2642 2643 return 0; 2644 } 2645 2646 int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume) 2647 { 2648 struct usb_mixer_elem_list *list; 2649 int id, err; 2650 2651 if (reset_resume) { 2652 /* restore cached mixer values */ 2653 for (id = 0; id < MAX_ID_ELEMS; id++) { 2654 for (list = mixer->id_elems[id]; list; 2655 list = list->next_id_elem) { 2656 if (list->resume) { 2657 err = list->resume(list); 2658 if (err < 0) 2659 return err; 2660 } 2661 } 2662 } 2663 } 2664 2665 return snd_usb_mixer_activate(mixer); 2666 } 2667 #endif 2668 2669 void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list, 2670 struct usb_mixer_interface *mixer, 2671 int unitid) 2672 { 2673 list->mixer = mixer; 2674 list->id = unitid; 2675 list->dump = snd_usb_mixer_dump_cval; 2676 #ifdef CONFIG_PM 2677 list->resume = restore_mixer_value; 2678 #endif 2679 } 2680