root/sound/usb/mixer.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. find_map
  2. check_mapped_name
  3. check_ignored_ctl
  4. check_mapped_dB
  5. check_mapped_selector_name
  6. find_audio_control_unit
  7. snd_usb_copy_string_desc
  8. convert_signed_value
  9. convert_bytes_value
  10. get_relative_value
  11. get_abs_value
  12. uac2_ctl_value_size
  13. get_ctl_value_v1
  14. get_ctl_value_v2
  15. get_ctl_value
  16. get_cur_ctl_value
  17. get_cur_mix_raw
  18. snd_usb_get_cur_mix_value
  19. snd_usb_mixer_set_ctl_value
  20. set_cur_ctl_value
  21. snd_usb_set_cur_mix_value
  22. snd_usb_mixer_vol_tlv
  23. check_matrix_bitmap
  24. snd_usb_mixer_add_control
  25. get_term_name
  26. get_cluster_channels_v3
  27. uac_mixer_unit_get_channels
  28. parse_term_uac1_iterm_unit
  29. parse_term_uac2_iterm_unit
  30. parse_term_uac3_iterm_unit
  31. parse_term_mixer_unit
  32. parse_term_selector_unit
  33. parse_term_proc_unit
  34. parse_term_effect_unit
  35. parse_term_uac2_clock_source
  36. parse_term_uac3_clock_source
  37. __check_input_term
  38. check_input_term
  39. usb_mixer_elem_info_free
  40. snd_usb_mixer_elem_free
  41. volume_control_quirks
  42. get_min_max_with_quirks
  43. mixer_ctl_feature_info
  44. mixer_ctl_feature_get
  45. mixer_ctl_feature_put
  46. mixer_ctl_master_bool_get
  47. mixer_ctl_connector_get
  48. append_ctl_name
  49. check_no_speaker_on_headset
  50. get_feature_control_info
  51. __build_feature_ctl
  52. build_feature_ctl
  53. build_feature_ctl_badd
  54. get_connector_control_name
  55. build_connector_control
  56. parse_clock_source_unit
  57. parse_audio_feature_unit
  58. mixer_bitmap_overflow
  59. build_mixer_unit_ctl
  60. parse_audio_input_terminal
  61. parse_audio_mixer_unit
  62. mixer_ctl_procunit_get
  63. mixer_ctl_procunit_put
  64. build_audio_procunit
  65. parse_audio_processing_unit
  66. parse_audio_extension_unit
  67. mixer_ctl_selector_info
  68. mixer_ctl_selector_get
  69. mixer_ctl_selector_put
  70. usb_mixer_selector_elem_free
  71. parse_audio_selector_unit
  72. parse_audio_unit
  73. snd_usb_mixer_free
  74. snd_usb_mixer_dev_free
  75. uac3_badd_func_has_valid_channels
  76. snd_usb_mixer_controls_badd
  77. snd_usb_mixer_controls
  78. delegate_notify
  79. snd_usb_mixer_notify_id
  80. snd_usb_mixer_dump_cval
  81. snd_usb_mixer_proc_read
  82. snd_usb_mixer_interrupt_v2
  83. snd_usb_mixer_interrupt
  84. snd_usb_mixer_status_create
  85. keep_iface_ctl_get
  86. keep_iface_ctl_put
  87. create_keep_iface_ctl
  88. snd_usb_create_mixer
  89. snd_usb_mixer_disconnect
  90. snd_usb_mixer_inactivate
  91. snd_usb_mixer_activate
  92. snd_usb_mixer_suspend
  93. restore_mixer_value
  94. snd_usb_mixer_resume
  95. snd_usb_mixer_elem_init_std

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

/* [<][>][^][v][top][bottom][index][help] */