1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (Tentative) USB Audio Driver for ALSA
7 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
9 * Many codes borrowed from audio.c by
10 * Alan Cox (alan@lxorguk.ukuu.org.uk)
11 * Thomas Sailer (sailer@ife.ee.ethz.ch)
15 * TODOs, for both the mixer and the streaming interfaces:
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
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>
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>
50 #include "mixer_quirks.h"
53 #define MAX_ID_ELEMS 256
55 struct usb_audio_term {
59 unsigned int chconfig;
63 struct usbmix_name_map;
66 struct snd_usb_audio *chip;
67 struct usb_mixer_interface *mixer;
68 unsigned char *buffer;
70 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
71 struct usb_audio_term oterm;
72 const struct usbmix_name_map *map;
73 const struct usbmix_selector_map *selector_map;
76 /*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
78 USB_XU_CLOCK_RATE = 0xe301,
79 USB_XU_CLOCK_SOURCE = 0xe302,
80 USB_XU_DIGITAL_IO_STATUS = 0xe303,
81 USB_XU_DEVICE_OPTIONS = 0xe304,
82 USB_XU_DIRECT_MONITORING = 0xe305,
83 USB_XU_METERING = 0xe306
86 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
87 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
88 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
89 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
93 * manual mapping of mixer names
94 * if the mixer topology is too complicated and the parsed names are
95 * ambiguous, add the entries in usbmixer_maps.c.
97 #include "mixer_maps.c"
99 static const struct usbmix_name_map *
100 find_map(const struct usbmix_name_map *p, int unitid, int control)
106 if (p->id == unitid &&
107 (!control || !p->control || control == p->control))
113 /* get the mapped name if the unit matches */
115 check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
121 return strlcpy(buf, p->name, buflen);
124 /* ignore the error value if ignore_ctl_error flag is set */
125 #define filter_error(cval, err) \
126 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
128 /* check whether the control should be ignored */
130 check_ignored_ctl(const struct usbmix_name_map *p)
132 if (!p || p->name || p->dB)
138 static inline void check_mapped_dB(const struct usbmix_name_map *p,
139 struct usb_mixer_elem_info *cval)
142 cval->dBmin = p->dB->min;
143 cval->dBmax = p->dB->max;
144 cval->initialized = 1;
148 /* get the mapped selector source name */
149 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
150 int index, char *buf, int buflen)
152 const struct usbmix_selector_map *p;
154 if (!state->selector_map)
156 for (p = state->selector_map; p->id; p++) {
157 if (p->id == unitid && index < p->count)
158 return strlcpy(buf, p->names[index], buflen);
164 * find an audio control unit with the given unit id
166 static void *find_audio_control_unit(struct mixer_build *state,
169 /* we just parse the header */
170 struct uac_feature_unit_descriptor *hdr = NULL;
172 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
173 USB_DT_CS_INTERFACE)) != NULL) {
174 if (hdr->bLength >= 4 &&
175 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
176 hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER &&
177 hdr->bUnitID == unit)
185 * copy a string with the given id
187 static int snd_usb_copy_string_desc(struct snd_usb_audio *chip,
188 int index, char *buf, int maxlen)
190 int len = usb_string(chip->dev, index, buf, maxlen - 1);
200 * convert from the byte/word on usb descriptor to the zero-based integer
202 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
204 switch (cval->val_type) {
205 case USB_MIXER_BOOLEAN:
207 case USB_MIXER_INV_BOOLEAN:
230 * convert from the zero-based int to the byte/word for usb descriptor
232 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
234 switch (cval->val_type) {
235 case USB_MIXER_BOOLEAN:
237 case USB_MIXER_INV_BOOLEAN:
246 return 0; /* not reached */
249 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
255 else if (val >= cval->max)
256 return (cval->max - cval->min + cval->res - 1) / cval->res;
258 return (val - cval->min) / cval->res;
261 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
274 static int uac2_ctl_value_size(int val_type)
286 return 0; /* unreachable */
291 * retrieve a mixer value
294 static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
295 int validx, int *value_ret)
297 struct snd_usb_audio *chip = cval->head.mixer->chip;
298 unsigned char buf[2];
299 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
303 err = snd_usb_lock_shutdown(chip);
307 while (timeout-- > 0) {
308 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
309 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
310 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
311 validx, idx, buf, val_len);
312 if (err >= val_len) {
313 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
316 } else if (err == -ETIMEDOUT) {
321 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
322 request, validx, idx, cval->val_type);
326 snd_usb_unlock_shutdown(chip);
330 static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
331 int validx, int *value_ret)
333 struct snd_usb_audio *chip = cval->head.mixer->chip;
334 /* enough space for one range */
335 unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
337 int idx = 0, ret, val_size, size;
340 val_size = uac2_ctl_value_size(cval->val_type);
342 if (request == UAC_GET_CUR) {
343 bRequest = UAC2_CS_CUR;
346 bRequest = UAC2_CS_RANGE;
347 size = sizeof(__u16) + 3 * val_size;
350 memset(buf, 0, sizeof(buf));
352 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
356 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
357 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
358 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
359 validx, idx, buf, size);
360 snd_usb_unlock_shutdown(chip);
365 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
366 request, validx, idx, cval->val_type);
370 /* FIXME: how should we handle multiple triplets here? */
377 val = buf + sizeof(__u16);
380 val = buf + sizeof(__u16) + val_size;
383 val = buf + sizeof(__u16) + val_size * 2;
389 *value_ret = convert_signed_value(cval,
390 snd_usb_combine_bytes(val, val_size));
395 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
396 int validx, int *value_ret)
398 validx += cval->idx_off;
400 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
401 get_ctl_value_v1(cval, request, validx, value_ret) :
402 get_ctl_value_v2(cval, request, validx, value_ret);
405 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
406 int validx, int *value)
408 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
411 /* channel = 0: master, 1 = first channel */
412 static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
413 int channel, int *value)
415 return get_ctl_value(cval, UAC_GET_CUR,
416 (cval->control << 8) | channel,
420 int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
421 int channel, int index, int *value)
425 if (cval->cached & (1 << channel)) {
426 *value = cval->cache_val[index];
429 err = get_cur_mix_raw(cval, channel, value);
431 if (!cval->head.mixer->ignore_ctl_error)
432 usb_audio_dbg(cval->head.mixer->chip,
433 "cannot get current value for control %d ch %d: err = %d\n",
434 cval->control, channel, err);
437 cval->cached |= 1 << channel;
438 cval->cache_val[index] = *value;
446 int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
447 int request, int validx, int value_set)
449 struct snd_usb_audio *chip = cval->head.mixer->chip;
450 unsigned char buf[4];
451 int idx = 0, val_len, err, timeout = 10;
453 validx += cval->idx_off;
456 if (cval->head.mixer->protocol == UAC_VERSION_1) {
457 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
458 } else { /* UAC_VERSION_2/3 */
459 val_len = uac2_ctl_value_size(cval->val_type);
462 if (request != UAC_SET_CUR) {
463 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
467 request = UAC2_CS_CUR;
470 value_set = convert_bytes_value(cval, value_set);
471 buf[0] = value_set & 0xff;
472 buf[1] = (value_set >> 8) & 0xff;
473 buf[2] = (value_set >> 16) & 0xff;
474 buf[3] = (value_set >> 24) & 0xff;
476 err = snd_usb_lock_shutdown(chip);
480 while (timeout-- > 0) {
481 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
482 err = snd_usb_ctl_msg(chip->dev,
483 usb_sndctrlpipe(chip->dev, 0), request,
484 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
485 validx, idx, buf, val_len);
489 } else if (err == -ETIMEDOUT) {
493 usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
494 request, validx, idx, cval->val_type, buf[0], buf[1]);
498 snd_usb_unlock_shutdown(chip);
502 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
503 int validx, int value)
505 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
508 int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
509 int index, int value)
512 unsigned int read_only = (channel == 0) ?
513 cval->master_readonly :
514 cval->ch_readonly & (1 << (channel - 1));
517 usb_audio_dbg(cval->head.mixer->chip,
518 "%s(): channel %d of control %d is read_only\n",
519 __func__, channel, cval->control);
523 err = snd_usb_mixer_set_ctl_value(cval,
524 UAC_SET_CUR, (cval->control << 8) | channel,
528 cval->cached |= 1 << channel;
529 cval->cache_val[index] = value;
534 * TLV callback for mixer volume controls
536 int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
537 unsigned int size, unsigned int __user *_tlv)
539 struct usb_mixer_elem_info *cval = kcontrol->private_data;
540 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
542 if (size < sizeof(scale))
545 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
546 scale[2] = cval->dBmin;
547 scale[3] = cval->dBmax;
548 if (copy_to_user(_tlv, scale, sizeof(scale)))
554 * parser routines begin here...
557 static int parse_audio_unit(struct mixer_build *state, int unitid);
561 * check if the input/output channel routing is enabled on the given bitmap.
562 * used for mixer unit parser
564 static int check_matrix_bitmap(unsigned char *bmap,
565 int ich, int och, int num_outs)
567 int idx = ich * num_outs + och;
568 return bmap[idx >> 3] & (0x80 >> (idx & 7));
572 * add an alsa control element
573 * search and increment the index until an empty slot is found.
575 * if failed, give up and free the control instance.
578 int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
579 struct snd_kcontrol *kctl)
581 struct usb_mixer_interface *mixer = list->mixer;
584 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
586 err = snd_ctl_add(mixer->chip->card, kctl);
588 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
593 list->next_id_elem = mixer->id_elems[list->id];
594 mixer->id_elems[list->id] = list;
599 * get a terminal name string
602 static struct iterm_name_combo {
606 { 0x0300, "Output" },
607 { 0x0301, "Speaker" },
608 { 0x0302, "Headphone" },
609 { 0x0303, "HMD Audio" },
610 { 0x0304, "Desktop Speaker" },
611 { 0x0305, "Room Speaker" },
612 { 0x0306, "Com Speaker" },
614 { 0x0600, "External In" },
615 { 0x0601, "Analog In" },
616 { 0x0602, "Digital In" },
618 { 0x0604, "Legacy In" },
619 { 0x0605, "IEC958 In" },
620 { 0x0606, "1394 DA Stream" },
621 { 0x0607, "1394 DV Stream" },
622 { 0x0700, "Embedded" },
623 { 0x0701, "Noise Source" },
624 { 0x0702, "Equalization Noise" },
628 { 0x0706, "MiniDisk" },
629 { 0x0707, "Analog Tape" },
630 { 0x0708, "Phonograph" },
631 { 0x0709, "VCR Audio" },
632 { 0x070a, "Video Disk Audio" },
633 { 0x070b, "DVD Audio" },
634 { 0x070c, "TV Tuner Audio" },
635 { 0x070d, "Satellite Rec Audio" },
636 { 0x070e, "Cable Tuner Audio" },
637 { 0x070f, "DSS Audio" },
638 { 0x0710, "Radio Receiver" },
639 { 0x0711, "Radio Transmitter" },
640 { 0x0712, "Multi-Track Recorder" },
641 { 0x0713, "Synthesizer" },
645 static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm,
646 unsigned char *name, int maxlen, int term_only)
648 struct iterm_name_combo *names;
652 len = snd_usb_copy_string_desc(chip, iterm->name,
658 /* virtual type - not a real terminal */
659 if (iterm->type >> 16) {
662 switch (iterm->type >> 16) {
663 case UAC3_SELECTOR_UNIT:
664 strcpy(name, "Selector");
666 case UAC3_PROCESSING_UNIT:
667 strcpy(name, "Process Unit");
669 case UAC3_EXTENSION_UNIT:
670 strcpy(name, "Ext Unit");
672 case UAC3_MIXER_UNIT:
673 strcpy(name, "Mixer");
676 return sprintf(name, "Unit %d", iterm->id);
680 switch (iterm->type & 0xff00) {
688 strcpy(name, "Headset");
691 strcpy(name, "Phone");
695 for (names = iterm_names; names->type; names++) {
696 if (names->type == iterm->type) {
697 strcpy(name, names->name);
698 return strlen(names->name);
706 * Get logical cluster information for UAC3 devices.
708 static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id)
710 struct uac3_cluster_header_descriptor c_header;
713 err = snd_usb_ctl_msg(state->chip->dev,
714 usb_rcvctrlpipe(state->chip->dev, 0),
715 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
716 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
718 snd_usb_ctrl_intf(state->chip),
719 &c_header, sizeof(c_header));
722 if (err != sizeof(c_header)) {
727 return c_header.bNrChannels;
730 usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err);
735 * Get number of channels for a Mixer Unit.
737 static int uac_mixer_unit_get_channels(struct mixer_build *state,
738 struct uac_mixer_unit_descriptor *desc)
743 if (desc->bLength < sizeof(*desc))
745 if (!desc->bNrInPins)
748 switch (state->mixer->protocol) {
752 if (desc->bLength < sizeof(*desc) + desc->bNrInPins + 1)
753 return 0; /* no bmControls -> skip */
754 mu_channels = uac_mixer_unit_bNrChannels(desc);
757 mu_channels = get_cluster_channels_v3(state,
758 uac3_mixer_unit_wClusterDescrID(desc));
765 c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
766 if (c - (void *)desc + (mu_channels - 1) / 8 >= desc->bLength)
767 return 0; /* no bmControls -> skip */
773 * parse the source unit recursively until it reaches to a terminal
774 * or a branched unit.
776 static int check_input_term(struct mixer_build *state, int id,
777 struct usb_audio_term *term)
779 int protocol = state->mixer->protocol;
783 memset(term, 0, sizeof(*term));
784 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
785 unsigned char *hdr = p1;
788 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
790 case UAC_INPUT_TERMINAL:
791 if (protocol == UAC_VERSION_1) {
792 struct uac_input_terminal_descriptor *d = p1;
794 term->type = le16_to_cpu(d->wTerminalType);
795 term->channels = d->bNrChannels;
796 term->chconfig = le16_to_cpu(d->wChannelConfig);
797 term->name = d->iTerminal;
798 } else { /* UAC_VERSION_2 */
799 struct uac2_input_terminal_descriptor *d = p1;
801 /* call recursively to verify that the
802 * referenced clock entity is valid */
803 err = check_input_term(state, d->bCSourceID, term);
807 /* save input term properties after recursion,
808 * to ensure they are not overriden by the
811 term->type = le16_to_cpu(d->wTerminalType);
812 term->channels = d->bNrChannels;
813 term->chconfig = le32_to_cpu(d->bmChannelConfig);
814 term->name = d->iTerminal;
817 case UAC_FEATURE_UNIT: {
818 /* the header is the same for v1 and v2 */
819 struct uac_feature_unit_descriptor *d = p1;
822 break; /* continue to parse */
824 case UAC_MIXER_UNIT: {
825 struct uac_mixer_unit_descriptor *d = p1;
827 term->type = UAC3_MIXER_UNIT << 16; /* virtual type */
828 term->channels = uac_mixer_unit_bNrChannels(d);
829 term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
830 term->name = uac_mixer_unit_iMixer(d);
833 case UAC_SELECTOR_UNIT:
834 case UAC2_CLOCK_SELECTOR: {
835 struct uac_selector_unit_descriptor *d = p1;
836 /* call recursively to retrieve the channel info */
837 err = check_input_term(state, d->baSourceID[0], term);
840 term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */
842 term->name = uac_selector_unit_iSelector(d);
845 case UAC1_PROCESSING_UNIT:
846 /* UAC2_EFFECT_UNIT */
847 if (protocol == UAC_VERSION_1)
848 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
849 else /* UAC_VERSION_2 */
850 term->type = UAC3_EFFECT_UNIT << 16; /* virtual type */
852 case UAC1_EXTENSION_UNIT:
853 /* UAC2_PROCESSING_UNIT_V2 */
854 if (protocol == UAC_VERSION_1 && !term->type)
855 term->type = UAC3_EXTENSION_UNIT << 16; /* virtual type */
856 else if (protocol == UAC_VERSION_2 && !term->type)
857 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
859 case UAC2_EXTENSION_UNIT_V2: {
860 struct uac_processing_unit_descriptor *d = p1;
862 if (protocol == UAC_VERSION_2 &&
863 hdr[2] == UAC2_EFFECT_UNIT) {
864 /* UAC2/UAC1 unit IDs overlap here in an
865 * uncompatible way. Ignore this unit for now.
871 id = d->baSourceID[0];
872 break; /* continue to parse */
875 term->type = UAC3_EXTENSION_UNIT << 16; /* virtual type */
877 term->channels = uac_processing_unit_bNrChannels(d);
878 term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
879 term->name = uac_processing_unit_iProcessing(d, protocol);
882 case UAC2_CLOCK_SOURCE: {
883 struct uac_clock_source_descriptor *d = p1;
885 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */
887 term->name = d->iClockSource;
893 } else { /* UAC_VERSION_3 */
895 case UAC_INPUT_TERMINAL: {
896 struct uac3_input_terminal_descriptor *d = p1;
898 /* call recursively to verify that the
899 * referenced clock entity is valid */
900 err = check_input_term(state, d->bCSourceID, term);
904 /* save input term properties after recursion,
905 * to ensure they are not overriden by the
908 term->type = le16_to_cpu(d->wTerminalType);
910 err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID));
913 term->channels = err;
915 /* REVISIT: UAC3 IT doesn't have channels cfg */
918 term->name = le16_to_cpu(d->wTerminalDescrStr);
921 case UAC3_FEATURE_UNIT: {
922 struct uac3_feature_unit_descriptor *d = p1;
925 break; /* continue to parse */
927 case UAC3_CLOCK_SOURCE: {
928 struct uac3_clock_source_descriptor *d = p1;
930 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */
932 term->name = le16_to_cpu(d->wClockSourceStr);
935 case UAC3_MIXER_UNIT: {
936 struct uac_mixer_unit_descriptor *d = p1;
938 err = uac_mixer_unit_get_channels(state, d);
942 term->channels = err;
943 term->type = UAC3_MIXER_UNIT << 16; /* virtual type */
947 case UAC3_SELECTOR_UNIT:
948 case UAC3_CLOCK_SELECTOR: {
949 struct uac_selector_unit_descriptor *d = p1;
950 /* call recursively to retrieve the channel info */
951 err = check_input_term(state, d->baSourceID[0], term);
954 term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */
956 term->name = 0; /* TODO: UAC3 Class-specific strings */
960 case UAC3_PROCESSING_UNIT: {
961 struct uac_processing_unit_descriptor *d = p1;
966 /* call recursively to retrieve the channel info */
967 err = check_input_term(state, d->baSourceID[0], term);
971 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
973 term->name = 0; /* TODO: UAC3 Class-specific strings */
989 /* feature unit control information */
990 struct usb_feature_control_info {
993 int type; /* data type for uac1 */
994 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
997 static struct usb_feature_control_info audio_feature_info[] = {
998 { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
999 { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
1000 { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
1001 { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
1002 { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
1003 { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
1004 { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
1005 { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
1006 { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
1007 { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
1009 { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
1010 { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
1011 { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
1014 /* private_free callback */
1015 void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
1017 kfree(kctl->private_data);
1018 kctl->private_data = NULL;
1022 * interface to ALSA control for feature/mixer units
1025 /* volume control quirks */
1026 static void volume_control_quirks(struct usb_mixer_elem_info *cval,
1027 struct snd_kcontrol *kctl)
1029 struct snd_usb_audio *chip = cval->head.mixer->chip;
1030 switch (chip->usb_id) {
1031 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
1032 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
1033 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
1039 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1040 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1045 if (strstr(kctl->id.name, "Effect Return") != NULL) {
1051 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
1052 (strstr(kctl->id.name, "Effect Send") != NULL)) {
1053 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
1059 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1060 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1061 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
1062 usb_audio_info(chip,
1063 "set quirk for FTU Effect Duration\n");
1069 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1070 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1071 usb_audio_info(chip,
1072 "set quirks for FTU Effect Feedback/Volume\n");
1079 case USB_ID(0x0d8c, 0x0103):
1080 if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
1081 usb_audio_info(chip,
1082 "set volume quirk for CM102-A+/102S+\n");
1087 case USB_ID(0x0471, 0x0101):
1088 case USB_ID(0x0471, 0x0104):
1089 case USB_ID(0x0471, 0x0105):
1090 case USB_ID(0x0672, 0x1041):
1091 /* quirk for UDA1321/N101.
1092 * note that detection between firmware 2.1.1.7 (N101)
1093 * and later 2.1.1.21 is not very clear from datasheets.
1094 * I hope that the min value is -15360 for newer firmware --jk
1096 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1097 cval->min == -15616) {
1098 usb_audio_info(chip,
1099 "set volume quirk for UDA1321/N101 chip\n");
1104 case USB_ID(0x046d, 0x09a4):
1105 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1106 usb_audio_info(chip,
1107 "set volume quirk for QuickCam E3500\n");
1114 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
1115 case USB_ID(0x046d, 0x0808):
1116 case USB_ID(0x046d, 0x0809):
1117 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
1118 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
1119 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
1120 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
1121 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
1122 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
1123 case USB_ID(0x046d, 0x0991):
1124 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
1125 /* Most audio usb devices lie about volume resolution.
1126 * Most Logitech webcams have res = 384.
1127 * Probably there is some logitech magic behind this number --fishor
1129 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1130 usb_audio_info(chip,
1131 "set resolution quirk: cval->res = 384\n");
1139 * retrieve the minimum and maximum values for the specified control
1141 static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1142 int default_min, struct snd_kcontrol *kctl)
1145 cval->min = default_min;
1146 cval->max = cval->min + 1;
1148 cval->dBmin = cval->dBmax = 0;
1150 if (cval->val_type == USB_MIXER_BOOLEAN ||
1151 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1152 cval->initialized = 1;
1157 for (i = 0; i < MAX_CHANNELS; i++)
1158 if (cval->cmask & (1 << i)) {
1163 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1164 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
1165 usb_audio_err(cval->head.mixer->chip,
1166 "%d:%d: cannot get min/max values for control %d (id %d)\n",
1167 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1168 cval->control, cval->head.id);
1171 if (get_ctl_value(cval, UAC_GET_RES,
1172 (cval->control << 8) | minchn,
1176 int last_valid_res = cval->res;
1178 while (cval->res > 1) {
1179 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
1180 (cval->control << 8) | minchn,
1185 if (get_ctl_value(cval, UAC_GET_RES,
1186 (cval->control << 8) | minchn, &cval->res) < 0)
1187 cval->res = last_valid_res;
1192 /* Additional checks for the proper resolution
1194 * Some devices report smaller resolutions than actually
1195 * reacting. They don't return errors but simply clip
1196 * to the lower aligned value.
1198 if (cval->min + cval->res < cval->max) {
1199 int last_valid_res = cval->res;
1200 int saved, test, check;
1201 get_cur_mix_raw(cval, minchn, &saved);
1204 if (test < cval->max)
1208 if (test < cval->min || test > cval->max ||
1209 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
1210 get_cur_mix_raw(cval, minchn, &check)) {
1211 cval->res = last_valid_res;
1218 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
1221 cval->initialized = 1;
1225 volume_control_quirks(cval, kctl);
1227 /* USB descriptions contain the dB scale in 1/256 dB unit
1228 * while ALSA TLV contains in 1/100 dB unit
1230 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
1231 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
1232 if (cval->dBmin > cval->dBmax) {
1233 /* something is wrong; assume it's either from/to 0dB */
1234 if (cval->dBmin < 0)
1236 else if (cval->dBmin > 0)
1238 if (cval->dBmin > cval->dBmax) {
1239 /* totally crap, return an error */
1247 #define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
1249 /* get a feature/mixer unit info */
1250 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1251 struct snd_ctl_elem_info *uinfo)
1253 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1255 if (cval->val_type == USB_MIXER_BOOLEAN ||
1256 cval->val_type == USB_MIXER_INV_BOOLEAN)
1257 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1259 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1260 uinfo->count = cval->channels;
1261 if (cval->val_type == USB_MIXER_BOOLEAN ||
1262 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1263 uinfo->value.integer.min = 0;
1264 uinfo->value.integer.max = 1;
1266 if (!cval->initialized) {
1267 get_min_max_with_quirks(cval, 0, kcontrol);
1268 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1269 kcontrol->vd[0].access &=
1270 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1271 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
1272 snd_ctl_notify(cval->head.mixer->chip->card,
1273 SNDRV_CTL_EVENT_MASK_INFO,
1277 uinfo->value.integer.min = 0;
1278 uinfo->value.integer.max =
1279 (cval->max - cval->min + cval->res - 1) / cval->res;
1284 /* get the current value from feature/mixer unit */
1285 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1286 struct snd_ctl_elem_value *ucontrol)
1288 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1289 int c, cnt, val, err;
1291 ucontrol->value.integer.value[0] = cval->min;
1294 for (c = 0; c < MAX_CHANNELS; c++) {
1295 if (!(cval->cmask & (1 << c)))
1297 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
1299 return filter_error(cval, err);
1300 val = get_relative_value(cval, val);
1301 ucontrol->value.integer.value[cnt] = val;
1306 /* master channel */
1307 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1309 return filter_error(cval, err);
1310 val = get_relative_value(cval, val);
1311 ucontrol->value.integer.value[0] = val;
1316 /* put the current value to feature/mixer unit */
1317 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1318 struct snd_ctl_elem_value *ucontrol)
1320 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1321 int c, cnt, val, oval, err;
1326 for (c = 0; c < MAX_CHANNELS; c++) {
1327 if (!(cval->cmask & (1 << c)))
1329 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
1331 return filter_error(cval, err);
1332 val = ucontrol->value.integer.value[cnt];
1333 val = get_abs_value(cval, val);
1335 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
1341 /* master channel */
1342 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
1344 return filter_error(cval, err);
1345 val = ucontrol->value.integer.value[0];
1346 val = get_abs_value(cval, val);
1348 snd_usb_set_cur_mix_value(cval, 0, 0, val);
1355 /* get the boolean value from the master channel of a UAC control */
1356 static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
1357 struct snd_ctl_elem_value *ucontrol)
1359 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1362 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1364 return filter_error(cval, err);
1366 ucontrol->value.integer.value[0] = val;
1370 /* get the connectors status and report it as boolean type */
1371 static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol,
1372 struct snd_ctl_elem_value *ucontrol)
1374 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1375 struct snd_usb_audio *chip = cval->head.mixer->chip;
1376 int idx = 0, validx, ret, val;
1378 validx = cval->control << 8 | 0;
1380 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
1384 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
1385 if (cval->head.mixer->protocol == UAC_VERSION_2) {
1386 struct uac2_connectors_ctl_blk uac2_conn;
1388 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1389 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1390 validx, idx, &uac2_conn, sizeof(uac2_conn));
1391 val = !!uac2_conn.bNrChannels;
1392 } else { /* UAC_VERSION_3 */
1393 struct uac3_insertion_ctl_blk uac3_conn;
1395 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1396 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1397 validx, idx, &uac3_conn, sizeof(uac3_conn));
1398 val = !!uac3_conn.bmConInserted;
1401 snd_usb_unlock_shutdown(chip);
1406 "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
1407 UAC_GET_CUR, validx, idx, cval->val_type);
1411 ucontrol->value.integer.value[0] = val;
1415 static struct snd_kcontrol_new usb_feature_unit_ctl = {
1416 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1417 .name = "", /* will be filled later manually */
1418 .info = mixer_ctl_feature_info,
1419 .get = mixer_ctl_feature_get,
1420 .put = mixer_ctl_feature_put,
1423 /* the read-only variant */
1424 static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
1425 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1426 .name = "", /* will be filled later manually */
1427 .info = mixer_ctl_feature_info,
1428 .get = mixer_ctl_feature_get,
1433 * A control which shows the boolean value from reading a UAC control on
1434 * the master channel.
1436 static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
1437 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1438 .name = "", /* will be filled later manually */
1439 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1440 .info = snd_ctl_boolean_mono_info,
1441 .get = mixer_ctl_master_bool_get,
1445 static const struct snd_kcontrol_new usb_connector_ctl_ro = {
1446 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1447 .name = "", /* will be filled later manually */
1448 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1449 .info = snd_ctl_boolean_mono_info,
1450 .get = mixer_ctl_connector_get,
1455 * This symbol is exported in order to allow the mixer quirks to
1456 * hook up to the standard feature unit control mechanism
1458 struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
1461 * build a feature control
1463 static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1465 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1469 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1470 * rename it to "Headphone". We determine if something is a headphone
1471 * similar to how udev determines form factor.
1473 static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1474 struct snd_card *card)
1476 const char *names_to_check[] = {
1477 "Headset", "headset", "Headphone", "headphone", NULL};
1481 if (strcmp("Speaker", kctl->id.name))
1484 for (s = names_to_check; *s; s++)
1485 if (strstr(card->shortname, *s)) {
1493 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1496 static struct usb_feature_control_info *get_feature_control_info(int control)
1500 for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
1501 if (audio_feature_info[i].control == control)
1502 return &audio_feature_info[i];
1507 static void __build_feature_ctl(struct usb_mixer_interface *mixer,
1508 const struct usbmix_name_map *imap,
1509 unsigned int ctl_mask, int control,
1510 struct usb_audio_term *iterm,
1511 struct usb_audio_term *oterm,
1512 int unitid, int nameid, int readonly_mask)
1514 struct usb_feature_control_info *ctl_info;
1515 unsigned int len = 0;
1516 int mapped_name = 0;
1517 struct snd_kcontrol *kctl;
1518 struct usb_mixer_elem_info *cval;
1519 const struct usbmix_name_map *map;
1522 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
1523 /* FIXME: not supported yet */
1527 map = find_map(imap, unitid, control);
1528 if (check_ignored_ctl(map))
1531 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1534 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
1535 cval->control = control;
1536 cval->cmask = ctl_mask;
1538 ctl_info = get_feature_control_info(control);
1543 if (mixer->protocol == UAC_VERSION_1)
1544 cval->val_type = ctl_info->type;
1545 else /* UAC_VERSION_2 */
1546 cval->val_type = ctl_info->type_uac2 >= 0 ?
1547 ctl_info->type_uac2 : ctl_info->type;
1549 if (ctl_mask == 0) {
1550 cval->channels = 1; /* master channel */
1551 cval->master_readonly = readonly_mask;
1554 for (i = 0; i < 16; i++)
1555 if (ctl_mask & (1 << i))
1558 cval->ch_readonly = readonly_mask;
1562 * If all channels in the mask are marked read-only, make the control
1563 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
1564 * issue write commands to read-only channels.
1566 if (cval->channels == readonly_mask)
1567 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1569 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1572 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
1576 kctl->private_free = snd_usb_mixer_elem_free;
1578 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1579 mapped_name = len != 0;
1581 len = snd_usb_copy_string_desc(mixer->chip, nameid,
1582 kctl->id.name, sizeof(kctl->id.name));
1588 * determine the control name. the rule is:
1589 * - if a name id is given in descriptor, use it.
1590 * - if the connected input can be determined, then use the name
1592 * - if the connected output can be determined, use it.
1593 * - otherwise, anonymous name.
1597 len = get_term_name(mixer->chip, iterm,
1599 sizeof(kctl->id.name), 1);
1601 len = get_term_name(mixer->chip, oterm,
1603 sizeof(kctl->id.name), 1);
1605 snprintf(kctl->id.name, sizeof(kctl->id.name),
1606 "Feature %d", unitid);
1610 check_no_speaker_on_headset(kctl, mixer->chip->card);
1613 * determine the stream direction:
1614 * if the connected output is USB stream, then it's likely a
1615 * capture stream. otherwise it should be playback (hopefully :)
1617 if (!mapped_name && oterm && !(oterm->type >> 16)) {
1618 if ((oterm->type & 0xff00) == 0x0100)
1619 append_ctl_name(kctl, " Capture");
1621 append_ctl_name(kctl, " Playback");
1623 append_ctl_name(kctl, control == UAC_FU_MUTE ?
1624 " Switch" : " Volume");
1628 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1629 sizeof(kctl->id.name));
1633 /* get min/max values */
1634 get_min_max_with_quirks(cval, 0, kctl);
1636 if (control == UAC_FU_VOLUME) {
1637 check_mapped_dB(map, cval);
1638 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1639 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1640 kctl->vd[0].access |=
1641 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1642 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1646 snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
1648 range = (cval->max - cval->min) / cval->res;
1650 * Are there devices with volume range more than 255? I use a bit more
1651 * to be sure. 384 is a resolution magic number found on Logitech
1652 * devices. It will definitively catch all buggy Logitech devices.
1655 usb_audio_warn(mixer->chip,
1656 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
1658 usb_audio_warn(mixer->chip,
1659 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
1660 cval->head.id, kctl->id.name, cval->channels,
1661 cval->min, cval->max, cval->res);
1664 usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1665 cval->head.id, kctl->id.name, cval->channels,
1666 cval->min, cval->max, cval->res);
1667 snd_usb_mixer_add_control(&cval->head, kctl);
1670 static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1671 unsigned int ctl_mask, int control,
1672 struct usb_audio_term *iterm, int unitid,
1675 struct uac_feature_unit_descriptor *desc = raw_desc;
1676 int nameid = uac_feature_unit_iFeature(desc);
1678 __build_feature_ctl(state->mixer, state->map, ctl_mask, control,
1679 iterm, &state->oterm, unitid, nameid, readonly_mask);
1682 static void build_feature_ctl_badd(struct usb_mixer_interface *mixer,
1683 unsigned int ctl_mask, int control, int unitid,
1684 const struct usbmix_name_map *badd_map)
1686 __build_feature_ctl(mixer, badd_map, ctl_mask, control,
1687 NULL, NULL, unitid, 0, 0);
1690 static void get_connector_control_name(struct usb_mixer_interface *mixer,
1691 struct usb_audio_term *term,
1692 bool is_input, char *name, int name_size)
1694 int name_len = get_term_name(mixer->chip, term, name, name_size, 0);
1697 strlcpy(name, "Unknown", name_size);
1700 * sound/core/ctljack.c has a convention of naming jack controls
1701 * by ending in " Jack". Make it slightly more useful by
1702 * indicating Input or Output after the terminal name.
1705 strlcat(name, " - Input Jack", name_size);
1707 strlcat(name, " - Output Jack", name_size);
1710 /* Build a mixer control for a UAC connector control (jack-detect) */
1711 static void build_connector_control(struct usb_mixer_interface *mixer,
1712 struct usb_audio_term *term, bool is_input)
1714 struct snd_kcontrol *kctl;
1715 struct usb_mixer_elem_info *cval;
1717 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1720 snd_usb_mixer_elem_init_std(&cval->head, mixer, term->id);
1722 * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the
1723 * number of channels connected.
1725 * UAC3: The first byte specifies size of bitmap for the inserted controls. The
1726 * following byte(s) specifies which connectors are inserted.
1728 * This boolean ctl will simply report if any channels are connected
1731 if (mixer->protocol == UAC_VERSION_2)
1732 cval->control = UAC2_TE_CONNECTOR;
1733 else /* UAC_VERSION_3 */
1734 cval->control = UAC3_TE_INSERTION;
1736 cval->val_type = USB_MIXER_BOOLEAN;
1737 cval->channels = 1; /* report true if any channel is connected */
1740 kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval);
1742 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
1746 get_connector_control_name(mixer, term, is_input, kctl->id.name,
1747 sizeof(kctl->id.name));
1748 kctl->private_free = snd_usb_mixer_elem_free;
1749 snd_usb_mixer_add_control(&cval->head, kctl);
1752 static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1755 struct uac_clock_source_descriptor *hdr = _ftr;
1756 struct usb_mixer_elem_info *cval;
1757 struct snd_kcontrol *kctl;
1758 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1761 if (state->mixer->protocol != UAC_VERSION_2)
1764 if (hdr->bLength != sizeof(*hdr)) {
1765 usb_audio_dbg(state->chip,
1766 "Bogus clock source descriptor length of %d, ignoring.\n",
1772 * The only property of this unit we are interested in is the
1773 * clock source validity. If that isn't readable, just bail out.
1775 if (!uac_v2v3_control_is_readable(hdr->bmControls,
1776 UAC2_CS_CONTROL_CLOCK_VALID))
1779 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1783 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1788 cval->val_type = USB_MIXER_BOOLEAN;
1789 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1791 cval->master_readonly = 1;
1792 /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
1793 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
1800 kctl->private_free = snd_usb_mixer_elem_free;
1801 ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource,
1802 name, sizeof(name));
1804 snprintf(kctl->id.name, sizeof(kctl->id.name),
1805 "%s Validity", name);
1807 snprintf(kctl->id.name, sizeof(kctl->id.name),
1808 "Clock Source %d Validity", hdr->bClockID);
1810 return snd_usb_mixer_add_control(&cval->head, kctl);
1814 * parse a feature unit
1816 * most of controls are defined here.
1818 static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1822 struct usb_audio_term iterm;
1823 unsigned int master_bits;
1825 struct uac_feature_unit_descriptor *hdr = _ftr;
1828 if (state->mixer->protocol == UAC_VERSION_1) {
1829 if (hdr->bLength < 7) {
1830 usb_audio_err(state->chip,
1831 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1835 csize = hdr->bControlSize;
1837 usb_audio_dbg(state->chip,
1838 "unit %u: invalid bControlSize == 0\n",
1842 channels = (hdr->bLength - 7) / csize - 1;
1843 bmaControls = hdr->bmaControls;
1844 if (hdr->bLength < 7 + csize) {
1845 usb_audio_err(state->chip,
1846 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1850 } else if (state->mixer->protocol == UAC_VERSION_2) {
1851 struct uac2_feature_unit_descriptor *ftr = _ftr;
1852 if (hdr->bLength < 6) {
1853 usb_audio_err(state->chip,
1854 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1859 channels = (hdr->bLength - 6) / 4 - 1;
1860 bmaControls = ftr->bmaControls;
1861 if (hdr->bLength < 6 + csize) {
1862 usb_audio_err(state->chip,
1863 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1867 } else { /* UAC_VERSION_3 */
1868 struct uac3_feature_unit_descriptor *ftr = _ftr;
1870 if (hdr->bLength < 7) {
1871 usb_audio_err(state->chip,
1872 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1877 channels = (ftr->bLength - 7) / 4 - 1;
1878 bmaControls = ftr->bmaControls;
1879 if (hdr->bLength < 7 + csize) {
1880 usb_audio_err(state->chip,
1881 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1887 /* parse the source unit */
1888 err = parse_audio_unit(state, hdr->bSourceID);
1892 /* determine the input source type and name */
1893 err = check_input_term(state, hdr->bSourceID, &iterm);
1897 master_bits = snd_usb_combine_bytes(bmaControls, csize);
1898 /* master configuration quirks */
1899 switch (state->chip->usb_id) {
1900 case USB_ID(0x08bb, 0x2702):
1901 usb_audio_info(state->chip,
1902 "usbmixer: master volume quirk for PCM2702 chip\n");
1903 /* disable non-functional volume control */
1904 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
1906 case USB_ID(0x1130, 0xf211):
1907 usb_audio_info(state->chip,
1908 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
1909 /* disable non-functional volume control */
1915 if (state->mixer->protocol == UAC_VERSION_1) {
1916 /* check all control types */
1917 for (i = 0; i < 10; i++) {
1918 unsigned int ch_bits = 0;
1919 int control = audio_feature_info[i].control;
1921 for (j = 0; j < channels; j++) {
1924 mask = snd_usb_combine_bytes(bmaControls +
1925 csize * (j+1), csize);
1926 if (mask & (1 << i))
1927 ch_bits |= (1 << j);
1929 /* audio class v1 controls are never read-only */
1932 * The first channel must be set
1933 * (for ease of programming).
1936 build_feature_ctl(state, _ftr, ch_bits, control,
1938 if (master_bits & (1 << i))
1939 build_feature_ctl(state, _ftr, 0, control,
1942 } else { /* UAC_VERSION_2/3 */
1943 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
1944 unsigned int ch_bits = 0;
1945 unsigned int ch_read_only = 0;
1946 int control = audio_feature_info[i].control;
1948 for (j = 0; j < channels; j++) {
1951 mask = snd_usb_combine_bytes(bmaControls +
1952 csize * (j+1), csize);
1953 if (uac_v2v3_control_is_readable(mask, control)) {
1954 ch_bits |= (1 << j);
1955 if (!uac_v2v3_control_is_writeable(mask, control))
1956 ch_read_only |= (1 << j);
1961 * NOTE: build_feature_ctl() will mark the control
1962 * read-only if all channels are marked read-only in
1963 * the descriptors. Otherwise, the control will be
1964 * reported as writeable, but the driver will not
1965 * actually issue a write command for read-only
1970 * The first channel must be set
1971 * (for ease of programming).
1974 build_feature_ctl(state, _ftr, ch_bits, control,
1975 &iterm, unitid, ch_read_only);
1976 if (uac_v2v3_control_is_readable(master_bits, control))
1977 build_feature_ctl(state, _ftr, 0, control,
1979 !uac_v2v3_control_is_writeable(master_bits,
1992 * build a mixer unit control
1994 * the callbacks are identical with feature unit.
1995 * input channel number (zero based) is given in control field instead.
1997 static void build_mixer_unit_ctl(struct mixer_build *state,
1998 struct uac_mixer_unit_descriptor *desc,
1999 int in_pin, int in_ch, int num_outs,
2000 int unitid, struct usb_audio_term *iterm)
2002 struct usb_mixer_elem_info *cval;
2003 unsigned int i, len;
2004 struct snd_kcontrol *kctl;
2005 const struct usbmix_name_map *map;
2007 map = find_map(state->map, unitid, 0);
2008 if (check_ignored_ctl(map))
2011 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2015 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
2016 cval->control = in_ch + 1; /* based on 1 */
2017 cval->val_type = USB_MIXER_S16;
2018 for (i = 0; i < num_outs; i++) {
2019 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
2021 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
2022 cval->cmask |= (1 << i);
2027 /* get min/max values */
2028 get_min_max(cval, 0);
2030 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
2032 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
2036 kctl->private_free = snd_usb_mixer_elem_free;
2038 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
2040 len = get_term_name(state->chip, iterm, kctl->id.name,
2041 sizeof(kctl->id.name), 0);
2043 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
2044 append_ctl_name(kctl, " Volume");
2046 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
2047 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
2048 snd_usb_mixer_add_control(&cval->head, kctl);
2051 static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
2054 struct usb_audio_term iterm;
2055 unsigned int control, bmctls, term_id;
2057 if (state->mixer->protocol == UAC_VERSION_2) {
2058 struct uac2_input_terminal_descriptor *d_v2 = raw_desc;
2059 if (d_v2->bLength < sizeof(*d_v2))
2061 control = UAC2_TE_CONNECTOR;
2062 term_id = d_v2->bTerminalID;
2063 bmctls = le16_to_cpu(d_v2->bmControls);
2064 } else if (state->mixer->protocol == UAC_VERSION_3) {
2065 struct uac3_input_terminal_descriptor *d_v3 = raw_desc;
2066 if (d_v3->bLength < sizeof(*d_v3))
2068 control = UAC3_TE_INSERTION;
2069 term_id = d_v3->bTerminalID;
2070 bmctls = le32_to_cpu(d_v3->bmControls);
2072 return 0; /* UAC1. No Insertion control */
2075 check_input_term(state, term_id, &iterm);
2077 /* Check for jack detection. */
2078 if (uac_v2v3_control_is_readable(bmctls, control))
2079 build_connector_control(state->mixer, &iterm, true);
2085 * parse a mixer unit
2087 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
2090 struct uac_mixer_unit_descriptor *desc = raw_desc;
2091 struct usb_audio_term iterm;
2092 int input_pins, num_ins, num_outs;
2095 err = uac_mixer_unit_get_channels(state, desc);
2097 usb_audio_err(state->chip,
2098 "invalid MIXER UNIT descriptor %d\n",
2104 input_pins = desc->bNrInPins;
2108 for (pin = 0; pin < input_pins; pin++) {
2109 err = parse_audio_unit(state, desc->baSourceID[pin]);
2112 /* no bmControls field (e.g. Maya44) -> ignore */
2115 err = check_input_term(state, desc->baSourceID[pin], &iterm);
2118 num_ins += iterm.channels;
2119 for (; ich < num_ins; ich++) {
2120 int och, ich_has_controls = 0;
2122 for (och = 0; och < num_outs; och++) {
2123 __u8 *c = uac_mixer_unit_bmControls(desc,
2124 state->mixer->protocol);
2126 if (check_matrix_bitmap(c, ich, och, num_outs)) {
2127 ich_has_controls = 1;
2131 if (ich_has_controls)
2132 build_mixer_unit_ctl(state, desc, pin, ich, num_outs,
2140 * Processing Unit / Extension Unit
2143 /* get callback for processing/extension unit */
2144 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
2145 struct snd_ctl_elem_value *ucontrol)
2147 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2150 err = get_cur_ctl_value(cval, cval->control << 8, &val);
2152 ucontrol->value.integer.value[0] = cval->min;
2153 return filter_error(cval, err);
2155 val = get_relative_value(cval, val);
2156 ucontrol->value.integer.value[0] = val;
2160 /* put callback for processing/extension unit */
2161 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
2162 struct snd_ctl_elem_value *ucontrol)
2164 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2167 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
2169 return filter_error(cval, err);
2170 val = ucontrol->value.integer.value[0];
2171 val = get_abs_value(cval, val);
2173 set_cur_ctl_value(cval, cval->control << 8, val);
2179 /* alsa control interface for processing/extension unit */
2180 static const struct snd_kcontrol_new mixer_procunit_ctl = {
2181 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2182 .name = "", /* will be filled later */
2183 .info = mixer_ctl_feature_info,
2184 .get = mixer_ctl_procunit_get,
2185 .put = mixer_ctl_procunit_put,
2189 * predefined data for processing units
2191 struct procunit_value_info {
2198 struct procunit_info {
2201 struct procunit_value_info *values;
2204 static struct procunit_value_info undefined_proc_info[] = {
2205 { 0x00, "Control Undefined", 0 },
2209 static struct procunit_value_info updown_proc_info[] = {
2210 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2211 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
2214 static struct procunit_value_info prologic_proc_info[] = {
2215 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2216 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
2219 static struct procunit_value_info threed_enh_proc_info[] = {
2220 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2221 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
2224 static struct procunit_value_info reverb_proc_info[] = {
2225 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2226 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2227 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2228 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
2231 static struct procunit_value_info chorus_proc_info[] = {
2232 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2233 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2234 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2235 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
2238 static struct procunit_value_info dcr_proc_info[] = {
2239 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2240 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2241 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2242 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2243 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2244 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
2248 static struct procunit_info procunits[] = {
2249 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2250 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2251 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2252 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2253 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2254 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
2258 static struct procunit_value_info uac3_updown_proc_info[] = {
2259 { UAC3_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
2262 static struct procunit_value_info uac3_stereo_ext_proc_info[] = {
2263 { UAC3_EXT_WIDTH_CONTROL, "Width Control", USB_MIXER_U8 },
2267 static struct procunit_info uac3_procunits[] = {
2268 { UAC3_PROCESS_UP_DOWNMIX, "Up Down", uac3_updown_proc_info },
2269 { UAC3_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", uac3_stereo_ext_proc_info },
2270 { UAC3_PROCESS_MULTI_FUNCTION, "Multi-Function", undefined_proc_info },
2275 * predefined data for extension units
2277 static struct procunit_value_info clock_rate_xu_info[] = {
2278 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2281 static struct procunit_value_info clock_source_xu_info[] = {
2282 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2285 static struct procunit_value_info spdif_format_xu_info[] = {
2286 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2289 static struct procunit_value_info soft_limit_xu_info[] = {
2290 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2293 static struct procunit_info extunits[] = {
2294 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2295 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2296 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2297 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2302 * build a processing/extension unit
2304 static int build_audio_procunit(struct mixer_build *state, int unitid,
2305 void *raw_desc, struct procunit_info *list,
2306 bool extension_unit)
2308 struct uac_processing_unit_descriptor *desc = raw_desc;
2310 struct usb_mixer_elem_info *cval;
2311 struct snd_kcontrol *kctl;
2312 int i, err, nameid, type, len;
2313 struct procunit_info *info;
2314 struct procunit_value_info *valinfo;
2315 const struct usbmix_name_map *map;
2316 static struct procunit_value_info default_value_info[] = {
2317 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2320 static struct procunit_info default_info = {
2321 0, NULL, default_value_info
2323 const char *name = extension_unit ?
2324 "Extension Unit" : "Processing Unit";
2326 if (desc->bLength < 13) {
2327 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
2331 num_ins = desc->bNrInPins;
2332 if (desc->bLength < 13 + num_ins ||
2333 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
2334 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
2338 for (i = 0; i < num_ins; i++) {
2339 err = parse_audio_unit(state, desc->baSourceID[i]);
2344 type = le16_to_cpu(desc->wProcessType);
2345 for (info = list; info && info->type; info++)
2346 if (info->type == type)
2348 if (!info || !info->type)
2349 info = &default_info;
2351 for (valinfo = info->values; valinfo->control; valinfo++) {
2352 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
2354 if (state->mixer->protocol == UAC_VERSION_1) {
2355 if (!(controls[valinfo->control / 8] &
2356 (1 << ((valinfo->control % 8) - 1))))
2358 } else { /* UAC_VERSION_2/3 */
2359 if (!uac_v2v3_control_is_readable(controls[valinfo->control / 8],
2364 map = find_map(state->map, unitid, valinfo->control);
2365 if (check_ignored_ctl(map))
2367 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2370 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
2371 cval->control = valinfo->control;
2372 cval->val_type = valinfo->val_type;
2375 if (state->mixer->protocol > UAC_VERSION_1 &&
2376 !uac_v2v3_control_is_writeable(controls[valinfo->control / 8],
2378 cval->master_readonly = 1;
2380 /* get min/max values */
2382 case UAC_PROCESS_UP_DOWNMIX: {
2383 bool mode_sel = false;
2385 switch (state->mixer->protocol) {
2389 if (cval->control == UAC_UD_MODE_SELECT)
2393 if (cval->control == UAC3_UD_MODE_SELECT)
2399 __u8 *control_spec = uac_processing_unit_specific(desc,
2400 state->mixer->protocol);
2402 cval->max = control_spec[0];
2404 cval->initialized = 1;
2408 get_min_max(cval, valinfo->min_value);
2411 case USB_XU_CLOCK_RATE:
2413 * E-Mu USB 0404/0202/TrackerPre/0204
2414 * samplerate control quirk
2419 cval->initialized = 1;
2422 get_min_max(cval, valinfo->min_value);
2426 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
2431 kctl->private_free = snd_usb_mixer_elem_free;
2433 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
2435 } else if (info->name) {
2436 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
2439 nameid = uac_extension_unit_iExtension(desc, state->mixer->protocol);
2441 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
2444 len = snd_usb_copy_string_desc(state->chip,
2447 sizeof(kctl->id.name));
2449 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2451 append_ctl_name(kctl, " ");
2452 append_ctl_name(kctl, valinfo->suffix);
2454 usb_audio_dbg(state->chip,
2455 "[%d] PU [%s] ch = %d, val = %d/%d\n",
2456 cval->head.id, kctl->id.name, cval->channels,
2457 cval->min, cval->max);
2459 err = snd_usb_mixer_add_control(&cval->head, kctl);
2466 static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2469 switch (state->mixer->protocol) {
2473 return build_audio_procunit(state, unitid, raw_desc,
2476 return build_audio_procunit(state, unitid, raw_desc,
2477 uac3_procunits, false);
2481 static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2485 * Note that we parse extension units with processing unit descriptors.
2486 * That's ok as the layout is the same.
2488 return build_audio_procunit(state, unitid, raw_desc, extunits, true);
2496 * info callback for selector unit
2497 * use an enumerator type for routing
2499 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2500 struct snd_ctl_elem_info *uinfo)
2502 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2503 const char **itemlist = (const char **)kcontrol->private_value;
2505 if (snd_BUG_ON(!itemlist))
2507 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
2510 /* get callback for selector unit */
2511 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2512 struct snd_ctl_elem_value *ucontrol)
2514 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2517 err = get_cur_ctl_value(cval, cval->control << 8, &val);
2519 ucontrol->value.enumerated.item[0] = 0;
2520 return filter_error(cval, err);
2522 val = get_relative_value(cval, val);
2523 ucontrol->value.enumerated.item[0] = val;
2527 /* put callback for selector unit */
2528 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2529 struct snd_ctl_elem_value *ucontrol)
2531 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2534 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
2536 return filter_error(cval, err);
2537 val = ucontrol->value.enumerated.item[0];
2538 val = get_abs_value(cval, val);
2540 set_cur_ctl_value(cval, cval->control << 8, val);
2546 /* alsa control interface for selector unit */
2547 static const struct snd_kcontrol_new mixer_selectunit_ctl = {
2548 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2549 .name = "", /* will be filled later */
2550 .info = mixer_ctl_selector_info,
2551 .get = mixer_ctl_selector_get,
2552 .put = mixer_ctl_selector_put,
2556 * private free callback.
2557 * free both private_data and private_value
2559 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
2563 if (kctl->private_data) {
2564 struct usb_mixer_elem_info *cval = kctl->private_data;
2565 num_ins = cval->max;
2567 kctl->private_data = NULL;
2569 if (kctl->private_value) {
2570 char **itemlist = (char **)kctl->private_value;
2571 for (i = 0; i < num_ins; i++)
2574 kctl->private_value = 0;
2579 * parse a selector unit
2581 static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2584 struct uac_selector_unit_descriptor *desc = raw_desc;
2585 unsigned int i, nameid, len;
2587 struct usb_mixer_elem_info *cval;
2588 struct snd_kcontrol *kctl;
2589 const struct usbmix_name_map *map;
2592 if (desc->bLength < 5 || !desc->bNrInPins ||
2593 desc->bLength < 5 + desc->bNrInPins) {
2594 usb_audio_err(state->chip,
2595 "invalid SELECTOR UNIT descriptor %d\n", unitid);
2599 for (i = 0; i < desc->bNrInPins; i++) {
2600 err = parse_audio_unit(state, desc->baSourceID[i]);
2605 if (desc->bNrInPins == 1) /* only one ? nonsense! */
2608 map = find_map(state->map, unitid, 0);
2609 if (check_ignored_ctl(map))
2612 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2615 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
2616 cval->val_type = USB_MIXER_U8;
2619 cval->max = desc->bNrInPins;
2621 cval->initialized = 1;
2623 switch (state->mixer->protocol) {
2630 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2631 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
2632 cval->control = UAC2_CX_CLOCK_SELECTOR;
2633 else /* UAC2/3_SELECTOR_UNIT */
2634 cval->control = UAC2_SU_SELECTOR;
2638 namelist = kmalloc_array(desc->bNrInPins, sizeof(char *), GFP_KERNEL);
2643 #define MAX_ITEM_NAME_LEN 64
2644 for (i = 0; i < desc->bNrInPins; i++) {
2645 struct usb_audio_term iterm;
2647 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
2655 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2657 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
2658 len = get_term_name(state->chip, &iterm, namelist[i],
2659 MAX_ITEM_NAME_LEN, 0);
2661 sprintf(namelist[i], "Input %u", i);
2664 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2666 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
2667 for (i = 0; i < desc->bNrInPins; i++)
2673 kctl->private_value = (unsigned long)namelist;
2674 kctl->private_free = usb_mixer_selector_elem_free;
2676 /* check the static mapping table at first */
2677 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
2680 switch (state->mixer->protocol) {
2684 /* if iSelector is given, use it */
2685 nameid = uac_selector_unit_iSelector(desc);
2687 len = snd_usb_copy_string_desc(state->chip,
2688 nameid, kctl->id.name,
2689 sizeof(kctl->id.name));
2692 /* TODO: Class-Specific strings not yet supported */
2696 /* ... or pick up the terminal name at next */
2698 len = get_term_name(state->chip, &state->oterm,
2699 kctl->id.name, sizeof(kctl->id.name), 0);
2700 /* ... or use the fixed string "USB" as the last resort */
2702 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2704 /* and add the proper suffix */
2705 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2706 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
2707 append_ctl_name(kctl, " Clock Source");
2708 else if ((state->oterm.type & 0xff00) == 0x0100)
2709 append_ctl_name(kctl, " Capture Source");
2711 append_ctl_name(kctl, " Playback Source");
2714 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
2715 cval->head.id, kctl->id.name, desc->bNrInPins);
2716 return snd_usb_mixer_add_control(&cval->head, kctl);
2720 * parse an audio unit recursively
2723 static int parse_audio_unit(struct mixer_build *state, int unitid)
2726 int protocol = state->mixer->protocol;
2728 if (test_and_set_bit(unitid, state->unitbitmap))
2729 return 0; /* the unit already visited */
2731 p1 = find_audio_control_unit(state, unitid);
2733 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
2737 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
2739 case UAC_INPUT_TERMINAL:
2740 return parse_audio_input_terminal(state, unitid, p1);
2741 case UAC_MIXER_UNIT:
2742 return parse_audio_mixer_unit(state, unitid, p1);
2743 case UAC2_CLOCK_SOURCE:
2744 return parse_clock_source_unit(state, unitid, p1);
2745 case UAC_SELECTOR_UNIT:
2746 case UAC2_CLOCK_SELECTOR:
2747 return parse_audio_selector_unit(state, unitid, p1);
2748 case UAC_FEATURE_UNIT:
2749 return parse_audio_feature_unit(state, unitid, p1);
2750 case UAC1_PROCESSING_UNIT:
2751 /* UAC2_EFFECT_UNIT has the same value */
2752 if (protocol == UAC_VERSION_1)
2753 return parse_audio_processing_unit(state, unitid, p1);
2755 return 0; /* FIXME - effect units not implemented yet */
2756 case UAC1_EXTENSION_UNIT:
2757 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2758 if (protocol == UAC_VERSION_1)
2759 return parse_audio_extension_unit(state, unitid, p1);
2760 else /* UAC_VERSION_2 */
2761 return parse_audio_processing_unit(state, unitid, p1);
2762 case UAC2_EXTENSION_UNIT_V2:
2763 return parse_audio_extension_unit(state, unitid, p1);
2765 usb_audio_err(state->chip,
2766 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2769 } else { /* UAC_VERSION_3 */
2771 case UAC_INPUT_TERMINAL:
2772 return parse_audio_input_terminal(state, unitid, p1);
2773 case UAC3_MIXER_UNIT:
2774 return parse_audio_mixer_unit(state, unitid, p1);
2775 case UAC3_CLOCK_SOURCE:
2776 return parse_clock_source_unit(state, unitid, p1);
2777 case UAC3_SELECTOR_UNIT:
2778 case UAC3_CLOCK_SELECTOR:
2779 return parse_audio_selector_unit(state, unitid, p1);
2780 case UAC3_FEATURE_UNIT:
2781 return parse_audio_feature_unit(state, unitid, p1);
2782 case UAC3_EFFECT_UNIT:
2783 return 0; /* FIXME - effect units not implemented yet */
2784 case UAC3_PROCESSING_UNIT:
2785 return parse_audio_processing_unit(state, unitid, p1);
2786 case UAC3_EXTENSION_UNIT:
2787 return parse_audio_extension_unit(state, unitid, p1);
2789 usb_audio_err(state->chip,
2790 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2796 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2798 /* kill pending URBs */
2799 snd_usb_mixer_disconnect(mixer);
2801 kfree(mixer->id_elems);
2803 kfree(mixer->urb->transfer_buffer);
2804 usb_free_urb(mixer->urb);
2806 usb_free_urb(mixer->rc_urb);
2807 kfree(mixer->rc_setup_packet);
2811 static int snd_usb_mixer_dev_free(struct snd_device *device)
2813 struct usb_mixer_interface *mixer = device->device_data;
2814 snd_usb_mixer_free(mixer);
2818 /* UAC3 predefined channels configuration */
2819 struct uac3_badd_profile {
2822 int c_chmask; /* capture channels mask */
2823 int p_chmask; /* playback channels mask */
2824 int st_chmask; /* side tone mixing channel mask */
2827 static struct uac3_badd_profile uac3_badd_profiles[] = {
2830 * BAIF, BAOF or combination of both
2831 * IN: Mono or Stereo cfg, Mono alt possible
2832 * OUT: Mono or Stereo cfg, Mono alt possible
2834 .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO,
2835 .name = "GENERIC IO",
2836 .c_chmask = -1, /* dynamic channels */
2837 .p_chmask = -1, /* dynamic channels */
2840 /* BAOF; Stereo only cfg, Mono alt possible */
2841 .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE,
2842 .name = "HEADPHONE",
2846 /* BAOF; Mono or Stereo cfg, Mono alt possible */
2847 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER,
2849 .p_chmask = -1, /* dynamic channels */
2852 /* BAIF; Mono or Stereo cfg, Mono alt possible */
2853 .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE,
2854 .name = "MICROPHONE",
2855 .c_chmask = -1, /* dynamic channels */
2861 * OUT: Mono or Stereo cfg, Mono alt possible
2863 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET,
2866 .p_chmask = -1, /* dynamic channels */
2870 /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */
2871 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER,
2872 .name = "HEADSET ADAPTER",
2878 /* BAIF + BAOF; IN: Mono only; OUT: Mono only */
2879 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE,
2880 .name = "SPEAKERPHONE",
2884 { 0 } /* terminator */
2887 static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer,
2888 struct uac3_badd_profile *f,
2889 int c_chmask, int p_chmask)
2892 * If both playback/capture channels are dynamic, make sure
2893 * at least one channel is present
2895 if (f->c_chmask < 0 && f->p_chmask < 0) {
2896 if (!c_chmask && !p_chmask) {
2897 usb_audio_warn(mixer->chip, "BAAD %s: no channels?",
2904 if ((f->c_chmask < 0 && !c_chmask) ||
2905 (f->c_chmask >= 0 && f->c_chmask != c_chmask)) {
2906 usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch",
2910 if ((f->p_chmask < 0 && !p_chmask) ||
2911 (f->p_chmask >= 0 && f->p_chmask != p_chmask)) {
2912 usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch",
2920 * create mixer controls for UAC3 BADD profiles
2922 * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything
2924 * BADD device may contain Mixer Unit, which doesn't have any controls, skip it
2926 static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer,
2929 struct usb_device *dev = mixer->chip->dev;
2930 struct usb_interface_assoc_descriptor *assoc;
2931 int badd_profile = mixer->chip->badd_profile;
2932 struct uac3_badd_profile *f;
2933 const struct usbmix_ctl_map *map;
2934 int p_chmask = 0, c_chmask = 0, st_chmask = 0;
2937 assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
2939 /* Detect BADD capture/playback channels from AS EP descriptors */
2940 for (i = 0; i < assoc->bInterfaceCount; i++) {
2941 int intf = assoc->bFirstInterface + i;
2943 struct usb_interface *iface;
2944 struct usb_host_interface *alts;
2945 struct usb_interface_descriptor *altsd;
2946 unsigned int maxpacksize;
2953 iface = usb_ifnum_to_if(dev, intf);
2954 num = iface->num_altsetting;
2960 * The number of Channels in an AudioStreaming interface
2961 * and the audio sample bit resolution (16 bits or 24
2962 * bits) can be derived from the wMaxPacketSize field in
2963 * the Standard AS Audio Data Endpoint descriptor in
2964 * Alternate Setting 1
2966 alts = &iface->altsetting[1];
2967 altsd = get_iface_desc(alts);
2969 if (altsd->bNumEndpoints < 1)
2972 /* check direction */
2973 dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
2974 maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2976 switch (maxpacksize) {
2978 usb_audio_err(mixer->chip,
2979 "incorrect wMaxPacketSize 0x%x for BADD profile\n",
2982 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
2983 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
2984 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
2985 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
2988 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
2989 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
2990 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
2991 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
3002 usb_audio_dbg(mixer->chip,
3003 "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n",
3004 badd_profile, c_chmask, p_chmask);
3006 /* check the mapping table */
3007 for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) {
3008 if (map->id == badd_profile)
3015 for (f = uac3_badd_profiles; f->name; f++) {
3016 if (badd_profile == f->subclass)
3021 if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask))
3023 st_chmask = f->st_chmask;
3027 /* Master channel, always writable */
3028 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3029 UAC3_BADD_FU_ID2, map->map);
3030 /* Mono/Stereo volume channels, always writable */
3031 build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME,
3032 UAC3_BADD_FU_ID2, map->map);
3037 /* Master channel, always writable */
3038 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3039 UAC3_BADD_FU_ID5, map->map);
3040 /* Mono/Stereo volume channels, always writable */
3041 build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME,
3042 UAC3_BADD_FU_ID5, map->map);
3045 /* Side tone-mixing */
3047 /* Master channel, always writable */
3048 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3049 UAC3_BADD_FU_ID7, map->map);
3050 /* Mono volume channel, always writable */
3051 build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME,
3052 UAC3_BADD_FU_ID7, map->map);
3055 /* Insertion Control */
3056 if (f->subclass == UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER) {
3057 struct usb_audio_term iterm, oterm;
3059 /* Input Term - Insertion control */
3060 memset(&iterm, 0, sizeof(iterm));
3061 iterm.id = UAC3_BADD_IT_ID4;
3062 iterm.type = UAC_BIDIR_TERMINAL_HEADSET;
3063 build_connector_control(mixer, &iterm, true);
3065 /* Output Term - Insertion control */
3066 memset(&oterm, 0, sizeof(oterm));
3067 oterm.id = UAC3_BADD_OT_ID3;
3068 oterm.type = UAC_BIDIR_TERMINAL_HEADSET;
3069 build_connector_control(mixer, &oterm, false);
3076 * create mixer controls
3078 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
3080 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
3082 struct mixer_build state;
3084 const struct usbmix_ctl_map *map;
3087 memset(&state, 0, sizeof(state));
3088 state.chip = mixer->chip;
3089 state.mixer = mixer;
3090 state.buffer = mixer->hostif->extra;
3091 state.buflen = mixer->hostif->extralen;
3093 /* check the mapping table */
3094 for (map = usbmix_ctl_maps; map->id; map++) {
3095 if (map->id == state.chip->usb_id) {
3096 state.map = map->map;
3097 state.selector_map = map->selector_map;
3098 mixer->ignore_ctl_error = map->ignore_ctl_error;
3104 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
3105 mixer->hostif->extralen,
3106 p, UAC_OUTPUT_TERMINAL)) != NULL) {
3107 if (mixer->protocol == UAC_VERSION_1) {
3108 struct uac1_output_terminal_descriptor *desc = p;
3110 if (desc->bLength < sizeof(*desc))
3111 continue; /* invalid descriptor? */
3112 /* mark terminal ID as visited */
3113 set_bit(desc->bTerminalID, state.unitbitmap);
3114 state.oterm.id = desc->bTerminalID;
3115 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3116 state.oterm.name = desc->iTerminal;
3117 err = parse_audio_unit(&state, desc->bSourceID);
3118 if (err < 0 && err != -EINVAL)
3120 } else if (mixer->protocol == UAC_VERSION_2) {
3121 struct uac2_output_terminal_descriptor *desc = p;
3123 if (desc->bLength < sizeof(*desc))
3124 continue; /* invalid descriptor? */
3125 /* mark terminal ID as visited */
3126 set_bit(desc->bTerminalID, state.unitbitmap);
3127 state.oterm.id = desc->bTerminalID;
3128 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3129 state.oterm.name = desc->iTerminal;
3130 err = parse_audio_unit(&state, desc->bSourceID);
3131 if (err < 0 && err != -EINVAL)
3135 * For UAC2, use the same approach to also add the
3138 err = parse_audio_unit(&state, desc->bCSourceID);
3139 if (err < 0 && err != -EINVAL)
3142 if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
3143 UAC2_TE_CONNECTOR)) {
3144 build_connector_control(state.mixer, &state.oterm,
3147 } else { /* UAC_VERSION_3 */
3148 struct uac3_output_terminal_descriptor *desc = p;
3150 if (desc->bLength < sizeof(*desc))
3151 continue; /* invalid descriptor? */
3152 /* mark terminal ID as visited */
3153 set_bit(desc->bTerminalID, state.unitbitmap);
3154 state.oterm.id = desc->bTerminalID;
3155 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3156 state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
3157 err = parse_audio_unit(&state, desc->bSourceID);
3158 if (err < 0 && err != -EINVAL)
3162 * For UAC3, use the same approach to also add the
3165 err = parse_audio_unit(&state, desc->bCSourceID);
3166 if (err < 0 && err != -EINVAL)
3169 if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls),
3170 UAC3_TE_INSERTION)) {
3171 build_connector_control(state.mixer, &state.oterm,
3180 void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
3182 struct usb_mixer_elem_list *list;
3184 for_each_mixer_elem(list, mixer, unitid) {
3185 struct usb_mixer_elem_info *info =
3186 mixer_elem_list_to_info(list);
3187 /* invalidate cache, so the value is read from the device */
3189 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3194 static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
3195 struct usb_mixer_elem_list *list)
3197 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
3198 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
3199 "S8", "U8", "S16", "U16"};
3200 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
3201 "channels=%i, type=\"%s\"\n", cval->head.id,
3202 cval->control, cval->cmask, cval->channels,
3203 val_types[cval->val_type]);
3204 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
3205 cval->min, cval->max, cval->dBmin, cval->dBmax);
3208 static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
3209 struct snd_info_buffer *buffer)
3211 struct snd_usb_audio *chip = entry->private_data;
3212 struct usb_mixer_interface *mixer;
3213 struct usb_mixer_elem_list *list;
3216 list_for_each_entry(mixer, &chip->mixer_list, list) {
3218 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
3219 chip->usb_id, snd_usb_ctrl_intf(chip),
3220 mixer->ignore_ctl_error);
3221 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
3222 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
3223 for_each_mixer_elem(list, mixer, unitid) {
3224 snd_iprintf(buffer, " Unit: %i\n", list->id);
3227 " Control: name=\"%s\", index=%i\n",
3228 list->kctl->id.name,
3229 list->kctl->id.index);
3231 list->dump(buffer, list);
3237 static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
3238 int attribute, int value, int index)
3240 struct usb_mixer_elem_list *list;
3241 __u8 unitid = (index >> 8) & 0xff;
3242 __u8 control = (value >> 8) & 0xff;
3243 __u8 channel = value & 0xff;
3244 unsigned int count = 0;
3246 if (channel >= MAX_CHANNELS) {
3247 usb_audio_dbg(mixer->chip,
3248 "%s(): bogus channel number %d\n",
3253 for_each_mixer_elem(list, mixer, unitid)
3259 for_each_mixer_elem(list, mixer, unitid) {
3260 struct usb_mixer_elem_info *info;
3265 info = mixer_elem_list_to_info(list);
3266 if (count > 1 && info->control != control)
3269 switch (attribute) {
3271 /* invalidate cache, so the value is read from the device */
3273 info->cached &= ~(1 << channel);
3274 else /* master channel */
3277 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3278 &info->head.kctl->id);
3290 usb_audio_dbg(mixer->chip,
3291 "unknown attribute %d in interrupt\n",
3298 static void snd_usb_mixer_interrupt(struct urb *urb)
3300 struct usb_mixer_interface *mixer = urb->context;
3301 int len = urb->actual_length;
3302 int ustatus = urb->status;
3307 if (mixer->protocol == UAC_VERSION_1) {
3308 struct uac1_status_word *status;
3310 for (status = urb->transfer_buffer;
3311 len >= sizeof(*status);
3312 len -= sizeof(*status), status++) {
3313 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
3314 status->bStatusType,
3315 status->bOriginator);
3317 /* ignore any notifications not from the control interface */
3318 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
3319 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
3322 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
3323 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
3325 snd_usb_mixer_notify_id(mixer, status->bOriginator);
3327 } else { /* UAC_VERSION_2 */
3328 struct uac2_interrupt_data_msg *msg;
3330 for (msg = urb->transfer_buffer;
3331 len >= sizeof(*msg);
3332 len -= sizeof(*msg), msg++) {
3333 /* drop vendor specific and endpoint requests */
3334 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
3335 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
3338 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
3339 le16_to_cpu(msg->wValue),
3340 le16_to_cpu(msg->wIndex));
3345 if (ustatus != -ENOENT &&
3346 ustatus != -ECONNRESET &&
3347 ustatus != -ESHUTDOWN) {
3348 urb->dev = mixer->chip->dev;
3349 usb_submit_urb(urb, GFP_ATOMIC);
3353 /* create the handler for the optional status interrupt endpoint */
3354 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
3356 struct usb_endpoint_descriptor *ep;
3357 void *transfer_buffer;
3361 /* we need one interrupt input endpoint */
3362 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
3364 ep = get_endpoint(mixer->hostif, 0);
3365 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
3368 epnum = usb_endpoint_num(ep);
3369 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
3370 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
3371 if (!transfer_buffer)
3373 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3375 kfree(transfer_buffer);
3378 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
3379 usb_rcvintpipe(mixer->chip->dev, epnum),
3380 transfer_buffer, buffer_length,
3381 snd_usb_mixer_interrupt, mixer, ep->bInterval);
3382 usb_submit_urb(mixer->urb, GFP_KERNEL);
3386 static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol,
3387 struct snd_ctl_elem_value *ucontrol)
3389 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3391 ucontrol->value.integer.value[0] = mixer->chip->keep_iface;
3395 static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol,
3396 struct snd_ctl_elem_value *ucontrol)
3398 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3399 bool keep_iface = !!ucontrol->value.integer.value[0];
3401 if (mixer->chip->keep_iface == keep_iface)
3403 mixer->chip->keep_iface = keep_iface;
3407 static const struct snd_kcontrol_new keep_iface_ctl = {
3408 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3409 .name = "Keep Interface",
3410 .info = snd_ctl_boolean_mono_info,
3411 .get = keep_iface_ctl_get,
3412 .put = keep_iface_ctl_put,
3415 static int create_keep_iface_ctl(struct usb_mixer_interface *mixer)
3417 struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer);
3419 /* need only one control per card */
3420 if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) {
3421 snd_ctl_free_one(kctl);
3425 return snd_ctl_add(mixer->chip->card, kctl);
3428 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
3431 static struct snd_device_ops dev_ops = {
3432 .dev_free = snd_usb_mixer_dev_free
3434 struct usb_mixer_interface *mixer;
3437 strcpy(chip->card->mixername, "USB Mixer");
3439 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
3443 mixer->ignore_ctl_error = ignore_error;
3444 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
3446 if (!mixer->id_elems) {
3451 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
3452 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
3455 mixer->protocol = UAC_VERSION_1;
3458 mixer->protocol = UAC_VERSION_2;
3461 mixer->protocol = UAC_VERSION_3;
3465 if (mixer->protocol == UAC_VERSION_3 &&
3466 chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
3467 err = snd_usb_mixer_controls_badd(mixer, ctrlif);
3471 err = snd_usb_mixer_controls(mixer);
3476 err = snd_usb_mixer_status_create(mixer);
3480 err = create_keep_iface_ctl(mixer);
3484 err = snd_usb_mixer_apply_create_quirk(mixer);
3488 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
3492 if (list_empty(&chip->mixer_list))
3493 snd_card_ro_proc_new(chip->card, "usbmixer", chip,
3494 snd_usb_mixer_proc_read);
3496 list_add(&mixer->list, &chip->mixer_list);
3500 snd_usb_mixer_free(mixer);
3504 void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
3506 if (mixer->disconnected)
3509 usb_kill_urb(mixer->urb);
3511 usb_kill_urb(mixer->rc_urb);
3512 mixer->disconnected = true;
3516 /* stop any bus activity of a mixer */
3517 static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3519 usb_kill_urb(mixer->urb);
3520 usb_kill_urb(mixer->rc_urb);
3523 static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3528 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3536 int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3538 snd_usb_mixer_inactivate(mixer);
3542 static int restore_mixer_value(struct usb_mixer_elem_list *list)
3544 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
3549 for (c = 0; c < MAX_CHANNELS; c++) {
3550 if (!(cval->cmask & (1 << c)))
3552 if (cval->cached & (1 << (c + 1))) {
3553 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
3554 cval->cache_val[idx]);
3563 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
3572 int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3574 struct usb_mixer_elem_list *list;
3578 /* restore cached mixer values */
3579 for (id = 0; id < MAX_ID_ELEMS; id++) {
3580 for_each_mixer_elem(list, mixer, id) {
3582 err = list->resume(list);
3590 snd_usb_mixer_resume_quirk(mixer);
3592 return snd_usb_mixer_activate(mixer);
3596 void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3597 struct usb_mixer_interface *mixer,
3600 list->mixer = mixer;
3602 list->dump = snd_usb_mixer_dump_cval;
3604 list->resume = restore_mixer_value;