2 * (Tentative) USB Audio Driver for ALSA
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * TODOs, for both the mixer and the streaming interfaces:
32 * - support for UAC2 effect units
33 * - support for graphical equalizers
34 * - RANGE and MEM set commands (UAC2)
35 * - RANGE and MEM interrupt dispatchers (UAC2)
36 * - audio channel clustering (UAC2)
37 * - audio sample rate converter units (UAC2)
38 * - proper handling of clock multipliers (UAC2)
39 * - dispatch clock change notifications (UAC2)
40 * - stop PCM streams which use a clock that became invalid
41 * - stop PCM streams which use a clock selector that has changed
42 * - parse available sample rates again when clock sources changed
45 #include <linux/bitops.h>
46 #include <linux/init.h>
47 #include <linux/list.h>
48 #include <linux/log2.h>
49 #include <linux/slab.h>
50 #include <linux/string.h>
51 #include <linux/usb.h>
52 #include <linux/usb/audio.h>
53 #include <linux/usb/audio-v2.h>
54 #include <linux/usb/audio-v3.h>
56 #include <sound/core.h>
57 #include <sound/control.h>
58 #include <sound/hwdep.h>
59 #include <sound/info.h>
60 #include <sound/tlv.h>
65 #include "mixer_quirks.h"
68 #define MAX_ID_ELEMS 256
70 struct usb_audio_term {
74 unsigned int chconfig;
78 struct usbmix_name_map;
81 struct snd_usb_audio *chip;
82 struct usb_mixer_interface *mixer;
83 unsigned char *buffer;
85 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
86 struct usb_audio_term oterm;
87 const struct usbmix_name_map *map;
88 const struct usbmix_selector_map *selector_map;
91 /*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
93 USB_XU_CLOCK_RATE = 0xe301,
94 USB_XU_CLOCK_SOURCE = 0xe302,
95 USB_XU_DIGITAL_IO_STATUS = 0xe303,
96 USB_XU_DEVICE_OPTIONS = 0xe304,
97 USB_XU_DIRECT_MONITORING = 0xe305,
98 USB_XU_METERING = 0xe306
101 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
102 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
103 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
104 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
108 * manual mapping of mixer names
109 * if the mixer topology is too complicated and the parsed names are
110 * ambiguous, add the entries in usbmixer_maps.c.
112 #include "mixer_maps.c"
114 static const struct usbmix_name_map *
115 find_map(const struct usbmix_name_map *p, int unitid, int control)
121 if (p->id == unitid &&
122 (!control || !p->control || control == p->control))
128 /* get the mapped name if the unit matches */
130 check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
136 return strlcpy(buf, p->name, buflen);
139 /* ignore the error value if ignore_ctl_error flag is set */
140 #define filter_error(cval, err) \
141 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
143 /* check whether the control should be ignored */
145 check_ignored_ctl(const struct usbmix_name_map *p)
147 if (!p || p->name || p->dB)
153 static inline void check_mapped_dB(const struct usbmix_name_map *p,
154 struct usb_mixer_elem_info *cval)
157 cval->dBmin = p->dB->min;
158 cval->dBmax = p->dB->max;
159 cval->initialized = 1;
163 /* get the mapped selector source name */
164 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
165 int index, char *buf, int buflen)
167 const struct usbmix_selector_map *p;
169 if (!state->selector_map)
171 for (p = state->selector_map; p->id; p++) {
172 if (p->id == unitid && index < p->count)
173 return strlcpy(buf, p->names[index], buflen);
179 * find an audio control unit with the given unit id
181 static void *find_audio_control_unit(struct mixer_build *state,
184 /* we just parse the header */
185 struct uac_feature_unit_descriptor *hdr = NULL;
187 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
188 USB_DT_CS_INTERFACE)) != NULL) {
189 if (hdr->bLength >= 4 &&
190 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
191 hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER &&
192 hdr->bUnitID == unit)
200 * copy a string with the given id
202 static int snd_usb_copy_string_desc(struct snd_usb_audio *chip,
203 int index, char *buf, int maxlen)
205 int len = usb_string(chip->dev, index, buf, maxlen - 1);
215 * convert from the byte/word on usb descriptor to the zero-based integer
217 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
219 switch (cval->val_type) {
220 case USB_MIXER_BOOLEAN:
222 case USB_MIXER_INV_BOOLEAN:
245 * convert from the zero-based int to the byte/word for usb descriptor
247 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
249 switch (cval->val_type) {
250 case USB_MIXER_BOOLEAN:
252 case USB_MIXER_INV_BOOLEAN:
261 return 0; /* not reached */
264 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
270 else if (val >= cval->max)
271 return (cval->max - cval->min + cval->res - 1) / cval->res;
273 return (val - cval->min) / cval->res;
276 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
289 static int uac2_ctl_value_size(int val_type)
301 return 0; /* unreachable */
306 * retrieve a mixer value
309 static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
310 int validx, int *value_ret)
312 struct snd_usb_audio *chip = cval->head.mixer->chip;
313 unsigned char buf[2];
314 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
318 err = snd_usb_lock_shutdown(chip);
322 while (timeout-- > 0) {
323 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
324 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
325 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
326 validx, idx, buf, val_len);
327 if (err >= val_len) {
328 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
331 } else if (err == -ETIMEDOUT) {
336 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
337 request, validx, idx, cval->val_type);
341 snd_usb_unlock_shutdown(chip);
345 static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
346 int validx, int *value_ret)
348 struct snd_usb_audio *chip = cval->head.mixer->chip;
349 /* enough space for one range */
350 unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
352 int idx = 0, ret, val_size, size;
355 val_size = uac2_ctl_value_size(cval->val_type);
357 if (request == UAC_GET_CUR) {
358 bRequest = UAC2_CS_CUR;
361 bRequest = UAC2_CS_RANGE;
362 size = sizeof(__u16) + 3 * val_size;
365 memset(buf, 0, sizeof(buf));
367 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
371 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
372 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
373 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
374 validx, idx, buf, size);
375 snd_usb_unlock_shutdown(chip);
380 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
381 request, validx, idx, cval->val_type);
385 /* FIXME: how should we handle multiple triplets here? */
392 val = buf + sizeof(__u16);
395 val = buf + sizeof(__u16) + val_size;
398 val = buf + sizeof(__u16) + val_size * 2;
404 *value_ret = convert_signed_value(cval,
405 snd_usb_combine_bytes(val, val_size));
410 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
411 int validx, int *value_ret)
413 validx += cval->idx_off;
415 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
416 get_ctl_value_v1(cval, request, validx, value_ret) :
417 get_ctl_value_v2(cval, request, validx, value_ret);
420 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
421 int validx, int *value)
423 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
426 /* channel = 0: master, 1 = first channel */
427 static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
428 int channel, int *value)
430 return get_ctl_value(cval, UAC_GET_CUR,
431 (cval->control << 8) | channel,
435 int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
436 int channel, int index, int *value)
440 if (cval->cached & (1 << channel)) {
441 *value = cval->cache_val[index];
444 err = get_cur_mix_raw(cval, channel, value);
446 if (!cval->head.mixer->ignore_ctl_error)
447 usb_audio_dbg(cval->head.mixer->chip,
448 "cannot get current value for control %d ch %d: err = %d\n",
449 cval->control, channel, err);
452 cval->cached |= 1 << channel;
453 cval->cache_val[index] = *value;
461 int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
462 int request, int validx, int value_set)
464 struct snd_usb_audio *chip = cval->head.mixer->chip;
465 unsigned char buf[4];
466 int idx = 0, val_len, err, timeout = 10;
468 validx += cval->idx_off;
471 if (cval->head.mixer->protocol == UAC_VERSION_1) {
472 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
473 } else { /* UAC_VERSION_2/3 */
474 val_len = uac2_ctl_value_size(cval->val_type);
477 if (request != UAC_SET_CUR) {
478 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
482 request = UAC2_CS_CUR;
485 value_set = convert_bytes_value(cval, value_set);
486 buf[0] = value_set & 0xff;
487 buf[1] = (value_set >> 8) & 0xff;
488 buf[2] = (value_set >> 16) & 0xff;
489 buf[3] = (value_set >> 24) & 0xff;
491 err = snd_usb_lock_shutdown(chip);
495 while (timeout-- > 0) {
496 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
497 err = snd_usb_ctl_msg(chip->dev,
498 usb_sndctrlpipe(chip->dev, 0), request,
499 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
500 validx, idx, buf, val_len);
504 } else if (err == -ETIMEDOUT) {
508 usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
509 request, validx, idx, cval->val_type, buf[0], buf[1]);
513 snd_usb_unlock_shutdown(chip);
517 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
518 int validx, int value)
520 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
523 int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
524 int index, int value)
527 unsigned int read_only = (channel == 0) ?
528 cval->master_readonly :
529 cval->ch_readonly & (1 << (channel - 1));
532 usb_audio_dbg(cval->head.mixer->chip,
533 "%s(): channel %d of control %d is read_only\n",
534 __func__, channel, cval->control);
538 err = snd_usb_mixer_set_ctl_value(cval,
539 UAC_SET_CUR, (cval->control << 8) | channel,
543 cval->cached |= 1 << channel;
544 cval->cache_val[index] = value;
549 * TLV callback for mixer volume controls
551 int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
552 unsigned int size, unsigned int __user *_tlv)
554 struct usb_mixer_elem_info *cval = kcontrol->private_data;
555 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
557 if (size < sizeof(scale))
560 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
561 scale[2] = cval->dBmin;
562 scale[3] = cval->dBmax;
563 if (copy_to_user(_tlv, scale, sizeof(scale)))
569 * parser routines begin here...
572 static int parse_audio_unit(struct mixer_build *state, int unitid);
576 * check if the input/output channel routing is enabled on the given bitmap.
577 * used for mixer unit parser
579 static int check_matrix_bitmap(unsigned char *bmap,
580 int ich, int och, int num_outs)
582 int idx = ich * num_outs + och;
583 return bmap[idx >> 3] & (0x80 >> (idx & 7));
587 * add an alsa control element
588 * search and increment the index until an empty slot is found.
590 * if failed, give up and free the control instance.
593 int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
594 struct snd_kcontrol *kctl)
596 struct usb_mixer_interface *mixer = list->mixer;
599 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
601 err = snd_ctl_add(mixer->chip->card, kctl);
603 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
608 list->next_id_elem = mixer->id_elems[list->id];
609 mixer->id_elems[list->id] = list;
614 * get a terminal name string
617 static struct iterm_name_combo {
621 { 0x0300, "Output" },
622 { 0x0301, "Speaker" },
623 { 0x0302, "Headphone" },
624 { 0x0303, "HMD Audio" },
625 { 0x0304, "Desktop Speaker" },
626 { 0x0305, "Room Speaker" },
627 { 0x0306, "Com Speaker" },
629 { 0x0600, "External In" },
630 { 0x0601, "Analog In" },
631 { 0x0602, "Digital In" },
633 { 0x0604, "Legacy In" },
634 { 0x0605, "IEC958 In" },
635 { 0x0606, "1394 DA Stream" },
636 { 0x0607, "1394 DV Stream" },
637 { 0x0700, "Embedded" },
638 { 0x0701, "Noise Source" },
639 { 0x0702, "Equalization Noise" },
643 { 0x0706, "MiniDisk" },
644 { 0x0707, "Analog Tape" },
645 { 0x0708, "Phonograph" },
646 { 0x0709, "VCR Audio" },
647 { 0x070a, "Video Disk Audio" },
648 { 0x070b, "DVD Audio" },
649 { 0x070c, "TV Tuner Audio" },
650 { 0x070d, "Satellite Rec Audio" },
651 { 0x070e, "Cable Tuner Audio" },
652 { 0x070f, "DSS Audio" },
653 { 0x0710, "Radio Receiver" },
654 { 0x0711, "Radio Transmitter" },
655 { 0x0712, "Multi-Track Recorder" },
656 { 0x0713, "Synthesizer" },
660 static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm,
661 unsigned char *name, int maxlen, int term_only)
663 struct iterm_name_combo *names;
667 len = snd_usb_copy_string_desc(chip, iterm->name,
673 /* virtual type - not a real terminal */
674 if (iterm->type >> 16) {
677 switch (iterm->type >> 16) {
678 case UAC3_SELECTOR_UNIT:
679 strcpy(name, "Selector");
681 case UAC3_PROCESSING_UNIT:
682 strcpy(name, "Process Unit");
684 case UAC3_EXTENSION_UNIT:
685 strcpy(name, "Ext Unit");
687 case UAC3_MIXER_UNIT:
688 strcpy(name, "Mixer");
691 return sprintf(name, "Unit %d", iterm->id);
695 switch (iterm->type & 0xff00) {
703 strcpy(name, "Headset");
706 strcpy(name, "Phone");
710 for (names = iterm_names; names->type; names++) {
711 if (names->type == iterm->type) {
712 strcpy(name, names->name);
713 return strlen(names->name);
721 * Get logical cluster information for UAC3 devices.
723 static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id)
725 struct uac3_cluster_header_descriptor c_header;
728 err = snd_usb_ctl_msg(state->chip->dev,
729 usb_rcvctrlpipe(state->chip->dev, 0),
730 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
731 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
733 snd_usb_ctrl_intf(state->chip),
734 &c_header, sizeof(c_header));
737 if (err != sizeof(c_header)) {
742 return c_header.bNrChannels;
745 usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err);
750 * Get number of channels for a Mixer Unit.
752 static int uac_mixer_unit_get_channels(struct mixer_build *state,
753 struct uac_mixer_unit_descriptor *desc)
758 if (desc->bLength < sizeof(*desc))
760 if (!desc->bNrInPins)
763 switch (state->mixer->protocol) {
767 if (desc->bLength < sizeof(*desc) + desc->bNrInPins + 1)
768 return 0; /* no bmControls -> skip */
769 mu_channels = uac_mixer_unit_bNrChannels(desc);
772 mu_channels = get_cluster_channels_v3(state,
773 uac3_mixer_unit_wClusterDescrID(desc));
780 c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
781 if (c - (void *)desc + (mu_channels - 1) / 8 >= desc->bLength)
782 return 0; /* no bmControls -> skip */
788 * parse the source unit recursively until it reaches to a terminal
789 * or a branched unit.
791 static int check_input_term(struct mixer_build *state, int id,
792 struct usb_audio_term *term)
794 int protocol = state->mixer->protocol;
798 memset(term, 0, sizeof(*term));
799 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
800 unsigned char *hdr = p1;
803 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
805 case UAC_INPUT_TERMINAL:
806 if (protocol == UAC_VERSION_1) {
807 struct uac_input_terminal_descriptor *d = p1;
809 term->type = le16_to_cpu(d->wTerminalType);
810 term->channels = d->bNrChannels;
811 term->chconfig = le16_to_cpu(d->wChannelConfig);
812 term->name = d->iTerminal;
813 } else { /* UAC_VERSION_2 */
814 struct uac2_input_terminal_descriptor *d = p1;
816 /* call recursively to verify that the
817 * referenced clock entity is valid */
818 err = check_input_term(state, d->bCSourceID, term);
822 /* save input term properties after recursion,
823 * to ensure they are not overriden by the
826 term->type = le16_to_cpu(d->wTerminalType);
827 term->channels = d->bNrChannels;
828 term->chconfig = le32_to_cpu(d->bmChannelConfig);
829 term->name = d->iTerminal;
832 case UAC_FEATURE_UNIT: {
833 /* the header is the same for v1 and v2 */
834 struct uac_feature_unit_descriptor *d = p1;
837 break; /* continue to parse */
839 case UAC_MIXER_UNIT: {
840 struct uac_mixer_unit_descriptor *d = p1;
842 term->type = UAC3_MIXER_UNIT << 16; /* virtual type */
843 term->channels = uac_mixer_unit_bNrChannels(d);
844 term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
845 term->name = uac_mixer_unit_iMixer(d);
848 case UAC_SELECTOR_UNIT:
849 case UAC2_CLOCK_SELECTOR: {
850 struct uac_selector_unit_descriptor *d = p1;
851 /* call recursively to retrieve the channel info */
852 err = check_input_term(state, d->baSourceID[0], term);
855 term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */
857 term->name = uac_selector_unit_iSelector(d);
860 case UAC1_PROCESSING_UNIT:
861 /* UAC2_EFFECT_UNIT */
862 if (protocol == UAC_VERSION_1)
863 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
864 else /* UAC_VERSION_2 */
865 term->type = UAC3_EFFECT_UNIT << 16; /* virtual type */
867 case UAC1_EXTENSION_UNIT:
868 /* UAC2_PROCESSING_UNIT_V2 */
869 if (protocol == UAC_VERSION_1 && !term->type)
870 term->type = UAC3_EXTENSION_UNIT << 16; /* virtual type */
871 else if (protocol == UAC_VERSION_2 && !term->type)
872 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
874 case UAC2_EXTENSION_UNIT_V2: {
875 struct uac_processing_unit_descriptor *d = p1;
877 if (protocol == UAC_VERSION_2 &&
878 hdr[2] == UAC2_EFFECT_UNIT) {
879 /* UAC2/UAC1 unit IDs overlap here in an
880 * uncompatible way. Ignore this unit for now.
886 id = d->baSourceID[0];
887 break; /* continue to parse */
890 term->type = UAC3_EXTENSION_UNIT << 16; /* virtual type */
892 term->channels = uac_processing_unit_bNrChannels(d);
893 term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
894 term->name = uac_processing_unit_iProcessing(d, protocol);
897 case UAC2_CLOCK_SOURCE: {
898 struct uac_clock_source_descriptor *d = p1;
900 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */
902 term->name = d->iClockSource;
908 } else { /* UAC_VERSION_3 */
910 case UAC_INPUT_TERMINAL: {
911 struct uac3_input_terminal_descriptor *d = p1;
913 /* call recursively to verify that the
914 * referenced clock entity is valid */
915 err = check_input_term(state, d->bCSourceID, term);
919 /* save input term properties after recursion,
920 * to ensure they are not overriden by the
923 term->type = le16_to_cpu(d->wTerminalType);
925 err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID));
928 term->channels = err;
930 /* REVISIT: UAC3 IT doesn't have channels cfg */
933 term->name = le16_to_cpu(d->wTerminalDescrStr);
936 case UAC3_FEATURE_UNIT: {
937 struct uac3_feature_unit_descriptor *d = p1;
940 break; /* continue to parse */
942 case UAC3_CLOCK_SOURCE: {
943 struct uac3_clock_source_descriptor *d = p1;
945 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */
947 term->name = le16_to_cpu(d->wClockSourceStr);
950 case UAC3_MIXER_UNIT: {
951 struct uac_mixer_unit_descriptor *d = p1;
953 err = uac_mixer_unit_get_channels(state, d);
957 term->channels = err;
958 term->type = UAC3_MIXER_UNIT << 16; /* virtual type */
962 case UAC3_SELECTOR_UNIT:
963 case UAC3_CLOCK_SELECTOR: {
964 struct uac_selector_unit_descriptor *d = p1;
965 /* call recursively to retrieve the channel info */
966 err = check_input_term(state, d->baSourceID[0], term);
969 term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */
971 term->name = 0; /* TODO: UAC3 Class-specific strings */
975 case UAC3_PROCESSING_UNIT: {
976 struct uac_processing_unit_descriptor *d = p1;
981 /* call recursively to retrieve the channel info */
982 err = check_input_term(state, d->baSourceID[0], term);
986 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
988 term->name = 0; /* TODO: UAC3 Class-specific strings */
1004 /* feature unit control information */
1005 struct usb_feature_control_info {
1008 int type; /* data type for uac1 */
1009 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
1012 static struct usb_feature_control_info audio_feature_info[] = {
1013 { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
1014 { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
1015 { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
1016 { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
1017 { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
1018 { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
1019 { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
1020 { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
1021 { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
1022 { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
1024 { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
1025 { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
1026 { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
1029 /* private_free callback */
1030 void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
1032 kfree(kctl->private_data);
1033 kctl->private_data = NULL;
1037 * interface to ALSA control for feature/mixer units
1040 /* volume control quirks */
1041 static void volume_control_quirks(struct usb_mixer_elem_info *cval,
1042 struct snd_kcontrol *kctl)
1044 struct snd_usb_audio *chip = cval->head.mixer->chip;
1045 switch (chip->usb_id) {
1046 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
1047 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
1048 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
1054 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1055 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1060 if (strstr(kctl->id.name, "Effect Return") != NULL) {
1066 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
1067 (strstr(kctl->id.name, "Effect Send") != NULL)) {
1068 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
1074 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1075 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1076 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
1077 usb_audio_info(chip,
1078 "set quirk for FTU Effect Duration\n");
1084 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1085 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1086 usb_audio_info(chip,
1087 "set quirks for FTU Effect Feedback/Volume\n");
1094 case USB_ID(0x0d8c, 0x0103):
1095 if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
1096 usb_audio_info(chip,
1097 "set volume quirk for CM102-A+/102S+\n");
1102 case USB_ID(0x0471, 0x0101):
1103 case USB_ID(0x0471, 0x0104):
1104 case USB_ID(0x0471, 0x0105):
1105 case USB_ID(0x0672, 0x1041):
1106 /* quirk for UDA1321/N101.
1107 * note that detection between firmware 2.1.1.7 (N101)
1108 * and later 2.1.1.21 is not very clear from datasheets.
1109 * I hope that the min value is -15360 for newer firmware --jk
1111 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1112 cval->min == -15616) {
1113 usb_audio_info(chip,
1114 "set volume quirk for UDA1321/N101 chip\n");
1119 case USB_ID(0x046d, 0x09a4):
1120 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1121 usb_audio_info(chip,
1122 "set volume quirk for QuickCam E3500\n");
1129 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
1130 case USB_ID(0x046d, 0x0808):
1131 case USB_ID(0x046d, 0x0809):
1132 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
1133 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
1134 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
1135 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
1136 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
1137 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
1138 case USB_ID(0x046d, 0x0991):
1139 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
1140 /* Most audio usb devices lie about volume resolution.
1141 * Most Logitech webcams have res = 384.
1142 * Probably there is some logitech magic behind this number --fishor
1144 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1145 usb_audio_info(chip,
1146 "set resolution quirk: cval->res = 384\n");
1154 * retrieve the minimum and maximum values for the specified control
1156 static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1157 int default_min, struct snd_kcontrol *kctl)
1160 cval->min = default_min;
1161 cval->max = cval->min + 1;
1163 cval->dBmin = cval->dBmax = 0;
1165 if (cval->val_type == USB_MIXER_BOOLEAN ||
1166 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1167 cval->initialized = 1;
1172 for (i = 0; i < MAX_CHANNELS; i++)
1173 if (cval->cmask & (1 << i)) {
1178 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1179 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
1180 usb_audio_err(cval->head.mixer->chip,
1181 "%d:%d: cannot get min/max values for control %d (id %d)\n",
1182 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1183 cval->control, cval->head.id);
1186 if (get_ctl_value(cval, UAC_GET_RES,
1187 (cval->control << 8) | minchn,
1191 int last_valid_res = cval->res;
1193 while (cval->res > 1) {
1194 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
1195 (cval->control << 8) | minchn,
1200 if (get_ctl_value(cval, UAC_GET_RES,
1201 (cval->control << 8) | minchn, &cval->res) < 0)
1202 cval->res = last_valid_res;
1207 /* Additional checks for the proper resolution
1209 * Some devices report smaller resolutions than actually
1210 * reacting. They don't return errors but simply clip
1211 * to the lower aligned value.
1213 if (cval->min + cval->res < cval->max) {
1214 int last_valid_res = cval->res;
1215 int saved, test, check;
1216 get_cur_mix_raw(cval, minchn, &saved);
1219 if (test < cval->max)
1223 if (test < cval->min || test > cval->max ||
1224 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
1225 get_cur_mix_raw(cval, minchn, &check)) {
1226 cval->res = last_valid_res;
1233 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
1236 cval->initialized = 1;
1240 volume_control_quirks(cval, kctl);
1242 /* USB descriptions contain the dB scale in 1/256 dB unit
1243 * while ALSA TLV contains in 1/100 dB unit
1245 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
1246 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
1247 if (cval->dBmin > cval->dBmax) {
1248 /* something is wrong; assume it's either from/to 0dB */
1249 if (cval->dBmin < 0)
1251 else if (cval->dBmin > 0)
1253 if (cval->dBmin > cval->dBmax) {
1254 /* totally crap, return an error */
1262 #define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
1264 /* get a feature/mixer unit info */
1265 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1266 struct snd_ctl_elem_info *uinfo)
1268 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1270 if (cval->val_type == USB_MIXER_BOOLEAN ||
1271 cval->val_type == USB_MIXER_INV_BOOLEAN)
1272 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1274 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1275 uinfo->count = cval->channels;
1276 if (cval->val_type == USB_MIXER_BOOLEAN ||
1277 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1278 uinfo->value.integer.min = 0;
1279 uinfo->value.integer.max = 1;
1281 if (!cval->initialized) {
1282 get_min_max_with_quirks(cval, 0, kcontrol);
1283 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1284 kcontrol->vd[0].access &=
1285 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1286 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
1287 snd_ctl_notify(cval->head.mixer->chip->card,
1288 SNDRV_CTL_EVENT_MASK_INFO,
1292 uinfo->value.integer.min = 0;
1293 uinfo->value.integer.max =
1294 (cval->max - cval->min + cval->res - 1) / cval->res;
1299 /* get the current value from feature/mixer unit */
1300 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1301 struct snd_ctl_elem_value *ucontrol)
1303 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1304 int c, cnt, val, err;
1306 ucontrol->value.integer.value[0] = cval->min;
1309 for (c = 0; c < MAX_CHANNELS; c++) {
1310 if (!(cval->cmask & (1 << c)))
1312 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
1314 return filter_error(cval, err);
1315 val = get_relative_value(cval, val);
1316 ucontrol->value.integer.value[cnt] = val;
1321 /* master channel */
1322 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1324 return filter_error(cval, err);
1325 val = get_relative_value(cval, val);
1326 ucontrol->value.integer.value[0] = val;
1331 /* put the current value to feature/mixer unit */
1332 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1333 struct snd_ctl_elem_value *ucontrol)
1335 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1336 int c, cnt, val, oval, err;
1341 for (c = 0; c < MAX_CHANNELS; c++) {
1342 if (!(cval->cmask & (1 << c)))
1344 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
1346 return filter_error(cval, err);
1347 val = ucontrol->value.integer.value[cnt];
1348 val = get_abs_value(cval, val);
1350 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
1356 /* master channel */
1357 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
1359 return filter_error(cval, err);
1360 val = ucontrol->value.integer.value[0];
1361 val = get_abs_value(cval, val);
1363 snd_usb_set_cur_mix_value(cval, 0, 0, val);
1370 /* get the boolean value from the master channel of a UAC control */
1371 static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
1372 struct snd_ctl_elem_value *ucontrol)
1374 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1377 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1379 return filter_error(cval, err);
1381 ucontrol->value.integer.value[0] = val;
1385 /* get the connectors status and report it as boolean type */
1386 static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol,
1387 struct snd_ctl_elem_value *ucontrol)
1389 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1390 struct snd_usb_audio *chip = cval->head.mixer->chip;
1391 int idx = 0, validx, ret, val;
1393 validx = cval->control << 8 | 0;
1395 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
1399 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
1400 if (cval->head.mixer->protocol == UAC_VERSION_2) {
1401 struct uac2_connectors_ctl_blk uac2_conn;
1403 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1404 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1405 validx, idx, &uac2_conn, sizeof(uac2_conn));
1406 val = !!uac2_conn.bNrChannels;
1407 } else { /* UAC_VERSION_3 */
1408 struct uac3_insertion_ctl_blk uac3_conn;
1410 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1411 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1412 validx, idx, &uac3_conn, sizeof(uac3_conn));
1413 val = !!uac3_conn.bmConInserted;
1416 snd_usb_unlock_shutdown(chip);
1421 "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
1422 UAC_GET_CUR, validx, idx, cval->val_type);
1426 ucontrol->value.integer.value[0] = val;
1430 static struct snd_kcontrol_new usb_feature_unit_ctl = {
1431 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1432 .name = "", /* will be filled later manually */
1433 .info = mixer_ctl_feature_info,
1434 .get = mixer_ctl_feature_get,
1435 .put = mixer_ctl_feature_put,
1438 /* the read-only variant */
1439 static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
1440 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1441 .name = "", /* will be filled later manually */
1442 .info = mixer_ctl_feature_info,
1443 .get = mixer_ctl_feature_get,
1448 * A control which shows the boolean value from reading a UAC control on
1449 * the master channel.
1451 static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
1452 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1453 .name = "", /* will be filled later manually */
1454 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1455 .info = snd_ctl_boolean_mono_info,
1456 .get = mixer_ctl_master_bool_get,
1460 static const struct snd_kcontrol_new usb_connector_ctl_ro = {
1461 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1462 .name = "", /* will be filled later manually */
1463 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1464 .info = snd_ctl_boolean_mono_info,
1465 .get = mixer_ctl_connector_get,
1470 * This symbol is exported in order to allow the mixer quirks to
1471 * hook up to the standard feature unit control mechanism
1473 struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
1476 * build a feature control
1478 static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1480 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1484 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1485 * rename it to "Headphone". We determine if something is a headphone
1486 * similar to how udev determines form factor.
1488 static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1489 struct snd_card *card)
1491 const char *names_to_check[] = {
1492 "Headset", "headset", "Headphone", "headphone", NULL};
1496 if (strcmp("Speaker", kctl->id.name))
1499 for (s = names_to_check; *s; s++)
1500 if (strstr(card->shortname, *s)) {
1508 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1511 static struct usb_feature_control_info *get_feature_control_info(int control)
1515 for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
1516 if (audio_feature_info[i].control == control)
1517 return &audio_feature_info[i];
1522 static void __build_feature_ctl(struct usb_mixer_interface *mixer,
1523 const struct usbmix_name_map *imap,
1524 unsigned int ctl_mask, int control,
1525 struct usb_audio_term *iterm,
1526 struct usb_audio_term *oterm,
1527 int unitid, int nameid, int readonly_mask)
1529 struct usb_feature_control_info *ctl_info;
1530 unsigned int len = 0;
1531 int mapped_name = 0;
1532 struct snd_kcontrol *kctl;
1533 struct usb_mixer_elem_info *cval;
1534 const struct usbmix_name_map *map;
1537 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
1538 /* FIXME: not supported yet */
1542 map = find_map(imap, unitid, control);
1543 if (check_ignored_ctl(map))
1546 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1549 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
1550 cval->control = control;
1551 cval->cmask = ctl_mask;
1553 ctl_info = get_feature_control_info(control);
1558 if (mixer->protocol == UAC_VERSION_1)
1559 cval->val_type = ctl_info->type;
1560 else /* UAC_VERSION_2 */
1561 cval->val_type = ctl_info->type_uac2 >= 0 ?
1562 ctl_info->type_uac2 : ctl_info->type;
1564 if (ctl_mask == 0) {
1565 cval->channels = 1; /* master channel */
1566 cval->master_readonly = readonly_mask;
1569 for (i = 0; i < 16; i++)
1570 if (ctl_mask & (1 << i))
1573 cval->ch_readonly = readonly_mask;
1577 * If all channels in the mask are marked read-only, make the control
1578 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
1579 * issue write commands to read-only channels.
1581 if (cval->channels == readonly_mask)
1582 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1584 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1587 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
1591 kctl->private_free = snd_usb_mixer_elem_free;
1593 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1594 mapped_name = len != 0;
1596 len = snd_usb_copy_string_desc(mixer->chip, nameid,
1597 kctl->id.name, sizeof(kctl->id.name));
1603 * determine the control name. the rule is:
1604 * - if a name id is given in descriptor, use it.
1605 * - if the connected input can be determined, then use the name
1607 * - if the connected output can be determined, use it.
1608 * - otherwise, anonymous name.
1612 len = get_term_name(mixer->chip, iterm,
1614 sizeof(kctl->id.name), 1);
1616 len = get_term_name(mixer->chip, oterm,
1618 sizeof(kctl->id.name), 1);
1620 snprintf(kctl->id.name, sizeof(kctl->id.name),
1621 "Feature %d", unitid);
1625 check_no_speaker_on_headset(kctl, mixer->chip->card);
1628 * determine the stream direction:
1629 * if the connected output is USB stream, then it's likely a
1630 * capture stream. otherwise it should be playback (hopefully :)
1632 if (!mapped_name && oterm && !(oterm->type >> 16)) {
1633 if ((oterm->type & 0xff00) == 0x0100)
1634 append_ctl_name(kctl, " Capture");
1636 append_ctl_name(kctl, " Playback");
1638 append_ctl_name(kctl, control == UAC_FU_MUTE ?
1639 " Switch" : " Volume");
1643 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1644 sizeof(kctl->id.name));
1648 /* get min/max values */
1649 get_min_max_with_quirks(cval, 0, kctl);
1651 if (control == UAC_FU_VOLUME) {
1652 check_mapped_dB(map, cval);
1653 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1654 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1655 kctl->vd[0].access |=
1656 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1657 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1661 snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
1663 range = (cval->max - cval->min) / cval->res;
1665 * Are there devices with volume range more than 255? I use a bit more
1666 * to be sure. 384 is a resolution magic number found on Logitech
1667 * devices. It will definitively catch all buggy Logitech devices.
1670 usb_audio_warn(mixer->chip,
1671 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
1673 usb_audio_warn(mixer->chip,
1674 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
1675 cval->head.id, kctl->id.name, cval->channels,
1676 cval->min, cval->max, cval->res);
1679 usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1680 cval->head.id, kctl->id.name, cval->channels,
1681 cval->min, cval->max, cval->res);
1682 snd_usb_mixer_add_control(&cval->head, kctl);
1685 static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1686 unsigned int ctl_mask, int control,
1687 struct usb_audio_term *iterm, int unitid,
1690 struct uac_feature_unit_descriptor *desc = raw_desc;
1691 int nameid = uac_feature_unit_iFeature(desc);
1693 __build_feature_ctl(state->mixer, state->map, ctl_mask, control,
1694 iterm, &state->oterm, unitid, nameid, readonly_mask);
1697 static void build_feature_ctl_badd(struct usb_mixer_interface *mixer,
1698 unsigned int ctl_mask, int control, int unitid,
1699 const struct usbmix_name_map *badd_map)
1701 __build_feature_ctl(mixer, badd_map, ctl_mask, control,
1702 NULL, NULL, unitid, 0, 0);
1705 static void get_connector_control_name(struct usb_mixer_interface *mixer,
1706 struct usb_audio_term *term,
1707 bool is_input, char *name, int name_size)
1709 int name_len = get_term_name(mixer->chip, term, name, name_size, 0);
1712 strlcpy(name, "Unknown", name_size);
1715 * sound/core/ctljack.c has a convention of naming jack controls
1716 * by ending in " Jack". Make it slightly more useful by
1717 * indicating Input or Output after the terminal name.
1720 strlcat(name, " - Input Jack", name_size);
1722 strlcat(name, " - Output Jack", name_size);
1725 /* Build a mixer control for a UAC connector control (jack-detect) */
1726 static void build_connector_control(struct usb_mixer_interface *mixer,
1727 struct usb_audio_term *term, bool is_input)
1729 struct snd_kcontrol *kctl;
1730 struct usb_mixer_elem_info *cval;
1732 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1735 snd_usb_mixer_elem_init_std(&cval->head, mixer, term->id);
1737 * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the
1738 * number of channels connected.
1740 * UAC3: The first byte specifies size of bitmap for the inserted controls. The
1741 * following byte(s) specifies which connectors are inserted.
1743 * This boolean ctl will simply report if any channels are connected
1746 if (mixer->protocol == UAC_VERSION_2)
1747 cval->control = UAC2_TE_CONNECTOR;
1748 else /* UAC_VERSION_3 */
1749 cval->control = UAC3_TE_INSERTION;
1751 cval->val_type = USB_MIXER_BOOLEAN;
1752 cval->channels = 1; /* report true if any channel is connected */
1755 kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval);
1757 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
1761 get_connector_control_name(mixer, term, is_input, kctl->id.name,
1762 sizeof(kctl->id.name));
1763 kctl->private_free = snd_usb_mixer_elem_free;
1764 snd_usb_mixer_add_control(&cval->head, kctl);
1767 static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1770 struct uac_clock_source_descriptor *hdr = _ftr;
1771 struct usb_mixer_elem_info *cval;
1772 struct snd_kcontrol *kctl;
1773 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1776 if (state->mixer->protocol != UAC_VERSION_2)
1779 if (hdr->bLength != sizeof(*hdr)) {
1780 usb_audio_dbg(state->chip,
1781 "Bogus clock source descriptor length of %d, ignoring.\n",
1787 * The only property of this unit we are interested in is the
1788 * clock source validity. If that isn't readable, just bail out.
1790 if (!uac_v2v3_control_is_readable(hdr->bmControls,
1791 UAC2_CS_CONTROL_CLOCK_VALID))
1794 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1798 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1803 cval->val_type = USB_MIXER_BOOLEAN;
1804 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1806 cval->master_readonly = 1;
1807 /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
1808 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
1815 kctl->private_free = snd_usb_mixer_elem_free;
1816 ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource,
1817 name, sizeof(name));
1819 snprintf(kctl->id.name, sizeof(kctl->id.name),
1820 "%s Validity", name);
1822 snprintf(kctl->id.name, sizeof(kctl->id.name),
1823 "Clock Source %d Validity", hdr->bClockID);
1825 return snd_usb_mixer_add_control(&cval->head, kctl);
1829 * parse a feature unit
1831 * most of controls are defined here.
1833 static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1837 struct usb_audio_term iterm;
1838 unsigned int master_bits, first_ch_bits;
1840 struct uac_feature_unit_descriptor *hdr = _ftr;
1843 if (state->mixer->protocol == UAC_VERSION_1) {
1844 if (hdr->bLength < 7) {
1845 usb_audio_err(state->chip,
1846 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1850 csize = hdr->bControlSize;
1852 usb_audio_dbg(state->chip,
1853 "unit %u: invalid bControlSize == 0\n",
1857 channels = (hdr->bLength - 7) / csize - 1;
1858 bmaControls = hdr->bmaControls;
1859 if (hdr->bLength < 7 + csize) {
1860 usb_audio_err(state->chip,
1861 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1865 } else if (state->mixer->protocol == UAC_VERSION_2) {
1866 struct uac2_feature_unit_descriptor *ftr = _ftr;
1867 if (hdr->bLength < 6) {
1868 usb_audio_err(state->chip,
1869 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1874 channels = (hdr->bLength - 6) / 4 - 1;
1875 bmaControls = ftr->bmaControls;
1876 if (hdr->bLength < 6 + csize) {
1877 usb_audio_err(state->chip,
1878 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1882 } else { /* UAC_VERSION_3 */
1883 struct uac3_feature_unit_descriptor *ftr = _ftr;
1885 if (hdr->bLength < 7) {
1886 usb_audio_err(state->chip,
1887 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1892 channels = (ftr->bLength - 7) / 4 - 1;
1893 bmaControls = ftr->bmaControls;
1894 if (hdr->bLength < 7 + csize) {
1895 usb_audio_err(state->chip,
1896 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1902 /* parse the source unit */
1903 err = parse_audio_unit(state, hdr->bSourceID);
1907 /* determine the input source type and name */
1908 err = check_input_term(state, hdr->bSourceID, &iterm);
1912 master_bits = snd_usb_combine_bytes(bmaControls, csize);
1913 /* master configuration quirks */
1914 switch (state->chip->usb_id) {
1915 case USB_ID(0x08bb, 0x2702):
1916 usb_audio_info(state->chip,
1917 "usbmixer: master volume quirk for PCM2702 chip\n");
1918 /* disable non-functional volume control */
1919 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
1921 case USB_ID(0x1130, 0xf211):
1922 usb_audio_info(state->chip,
1923 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
1924 /* disable non-functional volume control */
1930 first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
1934 if (state->mixer->protocol == UAC_VERSION_1) {
1935 /* check all control types */
1936 for (i = 0; i < 10; i++) {
1937 unsigned int ch_bits = 0;
1938 int control = audio_feature_info[i].control;
1940 for (j = 0; j < channels; j++) {
1943 mask = snd_usb_combine_bytes(bmaControls +
1944 csize * (j+1), csize);
1945 if (mask & (1 << i))
1946 ch_bits |= (1 << j);
1948 /* audio class v1 controls are never read-only */
1951 * The first channel must be set
1952 * (for ease of programming).
1955 build_feature_ctl(state, _ftr, ch_bits, control,
1957 if (master_bits & (1 << i))
1958 build_feature_ctl(state, _ftr, 0, control,
1961 } else { /* UAC_VERSION_2/3 */
1962 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
1963 unsigned int ch_bits = 0;
1964 unsigned int ch_read_only = 0;
1965 int control = audio_feature_info[i].control;
1967 for (j = 0; j < channels; j++) {
1970 mask = snd_usb_combine_bytes(bmaControls +
1971 csize * (j+1), csize);
1972 if (uac_v2v3_control_is_readable(mask, control)) {
1973 ch_bits |= (1 << j);
1974 if (!uac_v2v3_control_is_writeable(mask, control))
1975 ch_read_only |= (1 << j);
1980 * NOTE: build_feature_ctl() will mark the control
1981 * read-only if all channels are marked read-only in
1982 * the descriptors. Otherwise, the control will be
1983 * reported as writeable, but the driver will not
1984 * actually issue a write command for read-only
1989 * The first channel must be set
1990 * (for ease of programming).
1993 build_feature_ctl(state, _ftr, ch_bits, control,
1994 &iterm, unitid, ch_read_only);
1995 if (uac_v2v3_control_is_readable(master_bits, control))
1996 build_feature_ctl(state, _ftr, 0, control,
1998 !uac_v2v3_control_is_writeable(master_bits,
2011 * build a mixer unit control
2013 * the callbacks are identical with feature unit.
2014 * input channel number (zero based) is given in control field instead.
2016 static void build_mixer_unit_ctl(struct mixer_build *state,
2017 struct uac_mixer_unit_descriptor *desc,
2018 int in_pin, int in_ch, int num_outs,
2019 int unitid, struct usb_audio_term *iterm)
2021 struct usb_mixer_elem_info *cval;
2022 unsigned int i, len;
2023 struct snd_kcontrol *kctl;
2024 const struct usbmix_name_map *map;
2026 map = find_map(state->map, unitid, 0);
2027 if (check_ignored_ctl(map))
2030 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2034 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
2035 cval->control = in_ch + 1; /* based on 1 */
2036 cval->val_type = USB_MIXER_S16;
2037 for (i = 0; i < num_outs; i++) {
2038 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
2040 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
2041 cval->cmask |= (1 << i);
2046 /* get min/max values */
2047 get_min_max(cval, 0);
2049 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
2051 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
2055 kctl->private_free = snd_usb_mixer_elem_free;
2057 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
2059 len = get_term_name(state->chip, iterm, kctl->id.name,
2060 sizeof(kctl->id.name), 0);
2062 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
2063 append_ctl_name(kctl, " Volume");
2065 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
2066 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
2067 snd_usb_mixer_add_control(&cval->head, kctl);
2070 static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
2073 struct usb_audio_term iterm;
2074 unsigned int control, bmctls, term_id;
2076 if (state->mixer->protocol == UAC_VERSION_2) {
2077 struct uac2_input_terminal_descriptor *d_v2 = raw_desc;
2078 control = UAC2_TE_CONNECTOR;
2079 term_id = d_v2->bTerminalID;
2080 bmctls = le16_to_cpu(d_v2->bmControls);
2081 } else if (state->mixer->protocol == UAC_VERSION_3) {
2082 struct uac3_input_terminal_descriptor *d_v3 = raw_desc;
2083 control = UAC3_TE_INSERTION;
2084 term_id = d_v3->bTerminalID;
2085 bmctls = le32_to_cpu(d_v3->bmControls);
2087 return 0; /* UAC1. No Insertion control */
2090 check_input_term(state, term_id, &iterm);
2092 /* Check for jack detection. */
2093 if (uac_v2v3_control_is_readable(bmctls, control))
2094 build_connector_control(state->mixer, &iterm, true);
2100 * parse a mixer unit
2102 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
2105 struct uac_mixer_unit_descriptor *desc = raw_desc;
2106 struct usb_audio_term iterm;
2107 int input_pins, num_ins, num_outs;
2110 err = uac_mixer_unit_get_channels(state, desc);
2112 usb_audio_err(state->chip,
2113 "invalid MIXER UNIT descriptor %d\n",
2119 input_pins = desc->bNrInPins;
2123 for (pin = 0; pin < input_pins; pin++) {
2124 err = parse_audio_unit(state, desc->baSourceID[pin]);
2127 /* no bmControls field (e.g. Maya44) -> ignore */
2130 err = check_input_term(state, desc->baSourceID[pin], &iterm);
2133 num_ins += iterm.channels;
2134 for (; ich < num_ins; ich++) {
2135 int och, ich_has_controls = 0;
2137 for (och = 0; och < num_outs; och++) {
2138 __u8 *c = uac_mixer_unit_bmControls(desc,
2139 state->mixer->protocol);
2141 if (check_matrix_bitmap(c, ich, och, num_outs)) {
2142 ich_has_controls = 1;
2146 if (ich_has_controls)
2147 build_mixer_unit_ctl(state, desc, pin, ich, num_outs,
2155 * Processing Unit / Extension Unit
2158 /* get callback for processing/extension unit */
2159 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
2160 struct snd_ctl_elem_value *ucontrol)
2162 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2165 err = get_cur_ctl_value(cval, cval->control << 8, &val);
2167 ucontrol->value.integer.value[0] = cval->min;
2168 return filter_error(cval, err);
2170 val = get_relative_value(cval, val);
2171 ucontrol->value.integer.value[0] = val;
2175 /* put callback for processing/extension unit */
2176 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
2177 struct snd_ctl_elem_value *ucontrol)
2179 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2182 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
2184 return filter_error(cval, err);
2185 val = ucontrol->value.integer.value[0];
2186 val = get_abs_value(cval, val);
2188 set_cur_ctl_value(cval, cval->control << 8, val);
2194 /* alsa control interface for processing/extension unit */
2195 static const struct snd_kcontrol_new mixer_procunit_ctl = {
2196 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2197 .name = "", /* will be filled later */
2198 .info = mixer_ctl_feature_info,
2199 .get = mixer_ctl_procunit_get,
2200 .put = mixer_ctl_procunit_put,
2204 * predefined data for processing units
2206 struct procunit_value_info {
2213 struct procunit_info {
2216 struct procunit_value_info *values;
2219 static struct procunit_value_info undefined_proc_info[] = {
2220 { 0x00, "Control Undefined", 0 },
2224 static struct procunit_value_info updown_proc_info[] = {
2225 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2226 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
2229 static struct procunit_value_info prologic_proc_info[] = {
2230 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2231 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
2234 static struct procunit_value_info threed_enh_proc_info[] = {
2235 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2236 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
2239 static struct procunit_value_info reverb_proc_info[] = {
2240 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2241 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2242 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2243 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
2246 static struct procunit_value_info chorus_proc_info[] = {
2247 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2248 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2249 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2250 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
2253 static struct procunit_value_info dcr_proc_info[] = {
2254 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2255 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2256 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2257 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2258 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2259 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
2263 static struct procunit_info procunits[] = {
2264 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2265 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2266 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2267 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2268 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2269 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
2273 static struct procunit_value_info uac3_updown_proc_info[] = {
2274 { UAC3_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
2277 static struct procunit_value_info uac3_stereo_ext_proc_info[] = {
2278 { UAC3_EXT_WIDTH_CONTROL, "Width Control", USB_MIXER_U8 },
2282 static struct procunit_info uac3_procunits[] = {
2283 { UAC3_PROCESS_UP_DOWNMIX, "Up Down", uac3_updown_proc_info },
2284 { UAC3_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", uac3_stereo_ext_proc_info },
2285 { UAC3_PROCESS_MULTI_FUNCTION, "Multi-Function", undefined_proc_info },
2290 * predefined data for extension units
2292 static struct procunit_value_info clock_rate_xu_info[] = {
2293 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2296 static struct procunit_value_info clock_source_xu_info[] = {
2297 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2300 static struct procunit_value_info spdif_format_xu_info[] = {
2301 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2304 static struct procunit_value_info soft_limit_xu_info[] = {
2305 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2308 static struct procunit_info extunits[] = {
2309 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2310 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2311 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2312 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2317 * build a processing/extension unit
2319 static int build_audio_procunit(struct mixer_build *state, int unitid,
2320 void *raw_desc, struct procunit_info *list,
2323 struct uac_processing_unit_descriptor *desc = raw_desc;
2325 struct usb_mixer_elem_info *cval;
2326 struct snd_kcontrol *kctl;
2327 int i, err, nameid, type, len;
2328 struct procunit_info *info;
2329 struct procunit_value_info *valinfo;
2330 const struct usbmix_name_map *map;
2331 static struct procunit_value_info default_value_info[] = {
2332 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2335 static struct procunit_info default_info = {
2336 0, NULL, default_value_info
2339 if (desc->bLength < 13) {
2340 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
2344 num_ins = desc->bNrInPins;
2345 if (desc->bLength < 13 + num_ins ||
2346 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
2347 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
2351 for (i = 0; i < num_ins; i++) {
2352 err = parse_audio_unit(state, desc->baSourceID[i]);
2357 type = le16_to_cpu(desc->wProcessType);
2358 for (info = list; info && info->type; info++)
2359 if (info->type == type)
2361 if (!info || !info->type)
2362 info = &default_info;
2364 for (valinfo = info->values; valinfo->control; valinfo++) {
2365 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
2367 if (state->mixer->protocol == UAC_VERSION_1) {
2368 if (!(controls[valinfo->control / 8] &
2369 (1 << ((valinfo->control % 8) - 1))))
2371 } else { /* UAC_VERSION_2/3 */
2372 if (!uac_v2v3_control_is_readable(controls[valinfo->control / 8],
2377 map = find_map(state->map, unitid, valinfo->control);
2378 if (check_ignored_ctl(map))
2380 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2383 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
2384 cval->control = valinfo->control;
2385 cval->val_type = valinfo->val_type;
2388 if (state->mixer->protocol > UAC_VERSION_1 &&
2389 !uac_v2v3_control_is_writeable(controls[valinfo->control / 8],
2391 cval->master_readonly = 1;
2393 /* get min/max values */
2395 case UAC_PROCESS_UP_DOWNMIX: {
2396 bool mode_sel = false;
2398 switch (state->mixer->protocol) {
2402 if (cval->control == UAC_UD_MODE_SELECT)
2406 if (cval->control == UAC3_UD_MODE_SELECT)
2412 __u8 *control_spec = uac_processing_unit_specific(desc,
2413 state->mixer->protocol);
2415 cval->max = control_spec[0];
2417 cval->initialized = 1;
2421 get_min_max(cval, valinfo->min_value);
2424 case USB_XU_CLOCK_RATE:
2426 * E-Mu USB 0404/0202/TrackerPre/0204
2427 * samplerate control quirk
2432 cval->initialized = 1;
2435 get_min_max(cval, valinfo->min_value);
2439 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
2444 kctl->private_free = snd_usb_mixer_elem_free;
2446 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
2448 } else if (info->name) {
2449 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
2451 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
2454 len = snd_usb_copy_string_desc(state->chip,
2457 sizeof(kctl->id.name));
2459 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2461 append_ctl_name(kctl, " ");
2462 append_ctl_name(kctl, valinfo->suffix);
2464 usb_audio_dbg(state->chip,
2465 "[%d] PU [%s] ch = %d, val = %d/%d\n",
2466 cval->head.id, kctl->id.name, cval->channels,
2467 cval->min, cval->max);
2469 err = snd_usb_mixer_add_control(&cval->head, kctl);
2476 static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2479 switch (state->mixer->protocol) {
2483 return build_audio_procunit(state, unitid, raw_desc,
2484 procunits, "Processing Unit");
2486 return build_audio_procunit(state, unitid, raw_desc,
2487 uac3_procunits, "Processing Unit");
2491 static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2495 * Note that we parse extension units with processing unit descriptors.
2496 * That's ok as the layout is the same.
2498 return build_audio_procunit(state, unitid, raw_desc,
2499 extunits, "Extension Unit");
2507 * info callback for selector unit
2508 * use an enumerator type for routing
2510 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2511 struct snd_ctl_elem_info *uinfo)
2513 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2514 const char **itemlist = (const char **)kcontrol->private_value;
2516 if (snd_BUG_ON(!itemlist))
2518 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
2521 /* get callback for selector unit */
2522 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
2525 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2528 err = get_cur_ctl_value(cval, cval->control << 8, &val);
2530 ucontrol->value.enumerated.item[0] = 0;
2531 return filter_error(cval, err);
2533 val = get_relative_value(cval, val);
2534 ucontrol->value.enumerated.item[0] = val;
2538 /* put callback for selector unit */
2539 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_value *ucontrol)
2542 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2545 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
2547 return filter_error(cval, err);
2548 val = ucontrol->value.enumerated.item[0];
2549 val = get_abs_value(cval, val);
2551 set_cur_ctl_value(cval, cval->control << 8, val);
2557 /* alsa control interface for selector unit */
2558 static const struct snd_kcontrol_new mixer_selectunit_ctl = {
2559 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2560 .name = "", /* will be filled later */
2561 .info = mixer_ctl_selector_info,
2562 .get = mixer_ctl_selector_get,
2563 .put = mixer_ctl_selector_put,
2567 * private free callback.
2568 * free both private_data and private_value
2570 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
2574 if (kctl->private_data) {
2575 struct usb_mixer_elem_info *cval = kctl->private_data;
2576 num_ins = cval->max;
2578 kctl->private_data = NULL;
2580 if (kctl->private_value) {
2581 char **itemlist = (char **)kctl->private_value;
2582 for (i = 0; i < num_ins; i++)
2585 kctl->private_value = 0;
2590 * parse a selector unit
2592 static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2595 struct uac_selector_unit_descriptor *desc = raw_desc;
2596 unsigned int i, nameid, len;
2598 struct usb_mixer_elem_info *cval;
2599 struct snd_kcontrol *kctl;
2600 const struct usbmix_name_map *map;
2603 if (desc->bLength < 5 || !desc->bNrInPins ||
2604 desc->bLength < 5 + desc->bNrInPins) {
2605 usb_audio_err(state->chip,
2606 "invalid SELECTOR UNIT descriptor %d\n", unitid);
2610 for (i = 0; i < desc->bNrInPins; i++) {
2611 err = parse_audio_unit(state, desc->baSourceID[i]);
2616 if (desc->bNrInPins == 1) /* only one ? nonsense! */
2619 map = find_map(state->map, unitid, 0);
2620 if (check_ignored_ctl(map))
2623 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2626 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
2627 cval->val_type = USB_MIXER_U8;
2630 cval->max = desc->bNrInPins;
2632 cval->initialized = 1;
2634 switch (state->mixer->protocol) {
2641 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2642 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
2643 cval->control = UAC2_CX_CLOCK_SELECTOR;
2644 else /* UAC2/3_SELECTOR_UNIT */
2645 cval->control = UAC2_SU_SELECTOR;
2649 namelist = kmalloc_array(desc->bNrInPins, sizeof(char *), GFP_KERNEL);
2654 #define MAX_ITEM_NAME_LEN 64
2655 for (i = 0; i < desc->bNrInPins; i++) {
2656 struct usb_audio_term iterm;
2658 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
2666 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2668 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
2669 len = get_term_name(state->chip, &iterm, namelist[i],
2670 MAX_ITEM_NAME_LEN, 0);
2672 sprintf(namelist[i], "Input %u", i);
2675 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2677 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
2682 kctl->private_value = (unsigned long)namelist;
2683 kctl->private_free = usb_mixer_selector_elem_free;
2685 /* check the static mapping table at first */
2686 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
2689 switch (state->mixer->protocol) {
2693 /* if iSelector is given, use it */
2694 nameid = uac_selector_unit_iSelector(desc);
2696 len = snd_usb_copy_string_desc(state->chip,
2697 nameid, kctl->id.name,
2698 sizeof(kctl->id.name));
2701 /* TODO: Class-Specific strings not yet supported */
2705 /* ... or pick up the terminal name at next */
2707 len = get_term_name(state->chip, &state->oterm,
2708 kctl->id.name, sizeof(kctl->id.name), 0);
2709 /* ... or use the fixed string "USB" as the last resort */
2711 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2713 /* and add the proper suffix */
2714 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2715 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
2716 append_ctl_name(kctl, " Clock Source");
2717 else if ((state->oterm.type & 0xff00) == 0x0100)
2718 append_ctl_name(kctl, " Capture Source");
2720 append_ctl_name(kctl, " Playback Source");
2723 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
2724 cval->head.id, kctl->id.name, desc->bNrInPins);
2725 return snd_usb_mixer_add_control(&cval->head, kctl);
2729 * parse an audio unit recursively
2732 static int parse_audio_unit(struct mixer_build *state, int unitid)
2735 int protocol = state->mixer->protocol;
2737 if (test_and_set_bit(unitid, state->unitbitmap))
2738 return 0; /* the unit already visited */
2740 p1 = find_audio_control_unit(state, unitid);
2742 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
2746 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
2748 case UAC_INPUT_TERMINAL:
2749 return parse_audio_input_terminal(state, unitid, p1);
2750 case UAC_MIXER_UNIT:
2751 return parse_audio_mixer_unit(state, unitid, p1);
2752 case UAC2_CLOCK_SOURCE:
2753 return parse_clock_source_unit(state, unitid, p1);
2754 case UAC_SELECTOR_UNIT:
2755 case UAC2_CLOCK_SELECTOR:
2756 return parse_audio_selector_unit(state, unitid, p1);
2757 case UAC_FEATURE_UNIT:
2758 return parse_audio_feature_unit(state, unitid, p1);
2759 case UAC1_PROCESSING_UNIT:
2760 /* UAC2_EFFECT_UNIT has the same value */
2761 if (protocol == UAC_VERSION_1)
2762 return parse_audio_processing_unit(state, unitid, p1);
2764 return 0; /* FIXME - effect units not implemented yet */
2765 case UAC1_EXTENSION_UNIT:
2766 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2767 if (protocol == UAC_VERSION_1)
2768 return parse_audio_extension_unit(state, unitid, p1);
2769 else /* UAC_VERSION_2 */
2770 return parse_audio_processing_unit(state, unitid, p1);
2771 case UAC2_EXTENSION_UNIT_V2:
2772 return parse_audio_extension_unit(state, unitid, p1);
2774 usb_audio_err(state->chip,
2775 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2778 } else { /* UAC_VERSION_3 */
2780 case UAC_INPUT_TERMINAL:
2781 return parse_audio_input_terminal(state, unitid, p1);
2782 case UAC3_MIXER_UNIT:
2783 return parse_audio_mixer_unit(state, unitid, p1);
2784 case UAC3_CLOCK_SOURCE:
2785 return parse_clock_source_unit(state, unitid, p1);
2786 case UAC3_SELECTOR_UNIT:
2787 case UAC3_CLOCK_SELECTOR:
2788 return parse_audio_selector_unit(state, unitid, p1);
2789 case UAC3_FEATURE_UNIT:
2790 return parse_audio_feature_unit(state, unitid, p1);
2791 case UAC3_EFFECT_UNIT:
2792 return 0; /* FIXME - effect units not implemented yet */
2793 case UAC3_PROCESSING_UNIT:
2794 return parse_audio_processing_unit(state, unitid, p1);
2795 case UAC3_EXTENSION_UNIT:
2796 return parse_audio_extension_unit(state, unitid, p1);
2798 usb_audio_err(state->chip,
2799 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2805 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2807 /* kill pending URBs */
2808 snd_usb_mixer_disconnect(mixer);
2810 kfree(mixer->id_elems);
2812 kfree(mixer->urb->transfer_buffer);
2813 usb_free_urb(mixer->urb);
2815 usb_free_urb(mixer->rc_urb);
2816 kfree(mixer->rc_setup_packet);
2820 static int snd_usb_mixer_dev_free(struct snd_device *device)
2822 struct usb_mixer_interface *mixer = device->device_data;
2823 snd_usb_mixer_free(mixer);
2827 /* UAC3 predefined channels configuration */
2828 struct uac3_badd_profile {
2831 int c_chmask; /* capture channels mask */
2832 int p_chmask; /* playback channels mask */
2833 int st_chmask; /* side tone mixing channel mask */
2836 static struct uac3_badd_profile uac3_badd_profiles[] = {
2839 * BAIF, BAOF or combination of both
2840 * IN: Mono or Stereo cfg, Mono alt possible
2841 * OUT: Mono or Stereo cfg, Mono alt possible
2843 .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO,
2844 .name = "GENERIC IO",
2845 .c_chmask = -1, /* dynamic channels */
2846 .p_chmask = -1, /* dynamic channels */
2849 /* BAOF; Stereo only cfg, Mono alt possible */
2850 .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE,
2851 .name = "HEADPHONE",
2855 /* BAOF; Mono or Stereo cfg, Mono alt possible */
2856 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER,
2858 .p_chmask = -1, /* dynamic channels */
2861 /* BAIF; Mono or Stereo cfg, Mono alt possible */
2862 .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE,
2863 .name = "MICROPHONE",
2864 .c_chmask = -1, /* dynamic channels */
2870 * OUT: Mono or Stereo cfg, Mono alt possible
2872 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET,
2875 .p_chmask = -1, /* dynamic channels */
2879 /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */
2880 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER,
2881 .name = "HEADSET ADAPTER",
2887 /* BAIF + BAOF; IN: Mono only; OUT: Mono only */
2888 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE,
2889 .name = "SPEAKERPHONE",
2893 { 0 } /* terminator */
2896 static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer,
2897 struct uac3_badd_profile *f,
2898 int c_chmask, int p_chmask)
2901 * If both playback/capture channels are dynamic, make sure
2902 * at least one channel is present
2904 if (f->c_chmask < 0 && f->p_chmask < 0) {
2905 if (!c_chmask && !p_chmask) {
2906 usb_audio_warn(mixer->chip, "BAAD %s: no channels?",
2913 if ((f->c_chmask < 0 && !c_chmask) ||
2914 (f->c_chmask >= 0 && f->c_chmask != c_chmask)) {
2915 usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch",
2919 if ((f->p_chmask < 0 && !p_chmask) ||
2920 (f->p_chmask >= 0 && f->p_chmask != p_chmask)) {
2921 usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch",
2929 * create mixer controls for UAC3 BADD profiles
2931 * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything
2933 * BADD device may contain Mixer Unit, which doesn't have any controls, skip it
2935 static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer,
2938 struct usb_device *dev = mixer->chip->dev;
2939 struct usb_interface_assoc_descriptor *assoc;
2940 int badd_profile = mixer->chip->badd_profile;
2941 struct uac3_badd_profile *f;
2942 const struct usbmix_ctl_map *map;
2943 int p_chmask = 0, c_chmask = 0, st_chmask = 0;
2946 assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
2948 /* Detect BADD capture/playback channels from AS EP descriptors */
2949 for (i = 0; i < assoc->bInterfaceCount; i++) {
2950 int intf = assoc->bFirstInterface + i;
2952 struct usb_interface *iface;
2953 struct usb_host_interface *alts;
2954 struct usb_interface_descriptor *altsd;
2955 unsigned int maxpacksize;
2962 iface = usb_ifnum_to_if(dev, intf);
2963 num = iface->num_altsetting;
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
2975 alts = &iface->altsetting[1];
2976 altsd = get_iface_desc(alts);
2978 if (altsd->bNumEndpoints < 1)
2981 /* check direction */
2982 dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
2983 maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2985 switch (maxpacksize) {
2987 usb_audio_err(mixer->chip,
2988 "incorrect wMaxPacketSize 0x%x for BADD profile\n",
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:
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:
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);
3015 /* check the mapping table */
3016 for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) {
3017 if (map->id == badd_profile)
3024 for (f = uac3_badd_profiles; f->name; f++) {
3025 if (badd_profile == f->subclass)
3030 if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask))
3032 st_chmask = f->st_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);
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);
3054 /* Side tone-mixing */
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);
3064 /* Insertion Control */
3065 if (f->subclass == UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER) {
3066 struct usb_audio_term iterm, oterm;
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, &iterm, true);
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, &oterm, false);
3085 * create mixer controls
3087 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
3089 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
3091 struct mixer_build state;
3093 const struct usbmix_ctl_map *map;
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;
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->ignore_ctl_error = map->ignore_ctl_error;
3113 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
3114 mixer->hostif->extralen,
3115 p, UAC_OUTPUT_TERMINAL)) != NULL) {
3116 if (mixer->protocol == UAC_VERSION_1) {
3117 struct uac1_output_terminal_descriptor *desc = p;
3119 if (desc->bLength < sizeof(*desc))
3120 continue; /* invalid descriptor? */
3121 /* mark terminal ID as visited */
3122 set_bit(desc->bTerminalID, state.unitbitmap);
3123 state.oterm.id = desc->bTerminalID;
3124 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3125 state.oterm.name = desc->iTerminal;
3126 err = parse_audio_unit(&state, desc->bSourceID);
3127 if (err < 0 && err != -EINVAL)
3129 } else if (mixer->protocol == UAC_VERSION_2) {
3130 struct uac2_output_terminal_descriptor *desc = p;
3132 if (desc->bLength < sizeof(*desc))
3133 continue; /* invalid descriptor? */
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)
3144 * For UAC2, use the same approach to also add the
3147 err = parse_audio_unit(&state, desc->bCSourceID);
3148 if (err < 0 && err != -EINVAL)
3151 if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
3152 UAC2_TE_CONNECTOR)) {
3153 build_connector_control(state.mixer, &state.oterm,
3156 } else { /* UAC_VERSION_3 */
3157 struct uac3_output_terminal_descriptor *desc = p;
3159 if (desc->bLength < sizeof(*desc))
3160 continue; /* invalid descriptor? */
3161 /* mark terminal ID as visited */
3162 set_bit(desc->bTerminalID, state.unitbitmap);
3163 state.oterm.id = desc->bTerminalID;
3164 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3165 state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
3166 err = parse_audio_unit(&state, desc->bSourceID);
3167 if (err < 0 && err != -EINVAL)
3171 * For UAC3, use the same approach to also add the
3174 err = parse_audio_unit(&state, desc->bCSourceID);
3175 if (err < 0 && err != -EINVAL)
3178 if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls),
3179 UAC3_TE_INSERTION)) {
3180 build_connector_control(state.mixer, &state.oterm,
3189 void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
3191 struct usb_mixer_elem_list *list;
3193 for_each_mixer_elem(list, mixer, unitid) {
3194 struct usb_mixer_elem_info *info =
3195 mixer_elem_list_to_info(list);
3196 /* invalidate cache, so the value is read from the device */
3198 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3203 static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
3204 struct usb_mixer_elem_list *list)
3206 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
3207 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
3208 "S8", "U8", "S16", "U16"};
3209 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
3210 "channels=%i, type=\"%s\"\n", cval->head.id,
3211 cval->control, cval->cmask, cval->channels,
3212 val_types[cval->val_type]);
3213 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
3214 cval->min, cval->max, cval->dBmin, cval->dBmax);
3217 static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
3218 struct snd_info_buffer *buffer)
3220 struct snd_usb_audio *chip = entry->private_data;
3221 struct usb_mixer_interface *mixer;
3222 struct usb_mixer_elem_list *list;
3225 list_for_each_entry(mixer, &chip->mixer_list, list) {
3227 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
3228 chip->usb_id, snd_usb_ctrl_intf(chip),
3229 mixer->ignore_ctl_error);
3230 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
3231 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
3232 for_each_mixer_elem(list, mixer, unitid) {
3233 snd_iprintf(buffer, " Unit: %i\n", list->id);
3236 " Control: name=\"%s\", index=%i\n",
3237 list->kctl->id.name,
3238 list->kctl->id.index);
3240 list->dump(buffer, list);
3246 static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
3247 int attribute, int value, int index)
3249 struct usb_mixer_elem_list *list;
3250 __u8 unitid = (index >> 8) & 0xff;
3251 __u8 control = (value >> 8) & 0xff;
3252 __u8 channel = value & 0xff;
3253 unsigned int count = 0;
3255 if (channel >= MAX_CHANNELS) {
3256 usb_audio_dbg(mixer->chip,
3257 "%s(): bogus channel number %d\n",
3262 for_each_mixer_elem(list, mixer, unitid)
3268 for_each_mixer_elem(list, mixer, unitid) {
3269 struct usb_mixer_elem_info *info;
3274 info = mixer_elem_list_to_info(list);
3275 if (count > 1 && info->control != control)
3278 switch (attribute) {
3280 /* invalidate cache, so the value is read from the device */
3282 info->cached &= ~(1 << channel);
3283 else /* master channel */
3286 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3287 &info->head.kctl->id);
3299 usb_audio_dbg(mixer->chip,
3300 "unknown attribute %d in interrupt\n",
3307 static void snd_usb_mixer_interrupt(struct urb *urb)
3309 struct usb_mixer_interface *mixer = urb->context;
3310 int len = urb->actual_length;
3311 int ustatus = urb->status;
3316 if (mixer->protocol == UAC_VERSION_1) {
3317 struct uac1_status_word *status;
3319 for (status = urb->transfer_buffer;
3320 len >= sizeof(*status);
3321 len -= sizeof(*status), status++) {
3322 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
3323 status->bStatusType,
3324 status->bOriginator);
3326 /* ignore any notifications not from the control interface */
3327 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
3328 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
3331 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
3332 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
3334 snd_usb_mixer_notify_id(mixer, status->bOriginator);
3336 } else { /* UAC_VERSION_2 */
3337 struct uac2_interrupt_data_msg *msg;
3339 for (msg = urb->transfer_buffer;
3340 len >= sizeof(*msg);
3341 len -= sizeof(*msg), msg++) {
3342 /* drop vendor specific and endpoint requests */
3343 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
3344 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
3347 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
3348 le16_to_cpu(msg->wValue),
3349 le16_to_cpu(msg->wIndex));
3354 if (ustatus != -ENOENT &&
3355 ustatus != -ECONNRESET &&
3356 ustatus != -ESHUTDOWN) {
3357 urb->dev = mixer->chip->dev;
3358 usb_submit_urb(urb, GFP_ATOMIC);
3362 /* create the handler for the optional status interrupt endpoint */
3363 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
3365 struct usb_endpoint_descriptor *ep;
3366 void *transfer_buffer;
3370 /* we need one interrupt input endpoint */
3371 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
3373 ep = get_endpoint(mixer->hostif, 0);
3374 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
3377 epnum = usb_endpoint_num(ep);
3378 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
3379 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
3380 if (!transfer_buffer)
3382 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3384 kfree(transfer_buffer);
3387 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
3388 usb_rcvintpipe(mixer->chip->dev, epnum),
3389 transfer_buffer, buffer_length,
3390 snd_usb_mixer_interrupt, mixer, ep->bInterval);
3391 usb_submit_urb(mixer->urb, GFP_KERNEL);
3395 static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol,
3396 struct snd_ctl_elem_value *ucontrol)
3398 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3400 ucontrol->value.integer.value[0] = mixer->chip->keep_iface;
3404 static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol,
3405 struct snd_ctl_elem_value *ucontrol)
3407 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3408 bool keep_iface = !!ucontrol->value.integer.value[0];
3410 if (mixer->chip->keep_iface == keep_iface)
3412 mixer->chip->keep_iface = keep_iface;
3416 static const struct snd_kcontrol_new keep_iface_ctl = {
3417 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3418 .name = "Keep Interface",
3419 .info = snd_ctl_boolean_mono_info,
3420 .get = keep_iface_ctl_get,
3421 .put = keep_iface_ctl_put,
3424 static int create_keep_iface_ctl(struct usb_mixer_interface *mixer)
3426 struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer);
3428 /* need only one control per card */
3429 if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) {
3430 snd_ctl_free_one(kctl);
3434 return snd_ctl_add(mixer->chip->card, kctl);
3437 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
3440 static struct snd_device_ops dev_ops = {
3441 .dev_free = snd_usb_mixer_dev_free
3443 struct usb_mixer_interface *mixer;
3444 struct snd_info_entry *entry;
3447 strcpy(chip->card->mixername, "USB Mixer");
3449 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
3453 mixer->ignore_ctl_error = ignore_error;
3454 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
3456 if (!mixer->id_elems) {
3461 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
3462 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
3465 mixer->protocol = UAC_VERSION_1;
3468 mixer->protocol = UAC_VERSION_2;
3471 mixer->protocol = UAC_VERSION_3;
3475 if (mixer->protocol == UAC_VERSION_3 &&
3476 chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
3477 err = snd_usb_mixer_controls_badd(mixer, ctrlif);
3481 err = snd_usb_mixer_controls(mixer);
3486 err = snd_usb_mixer_status_create(mixer);
3490 err = create_keep_iface_ctl(mixer);
3494 snd_usb_mixer_apply_create_quirk(mixer);
3496 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
3500 if (list_empty(&chip->mixer_list) &&
3501 !snd_card_proc_new(chip->card, "usbmixer", &entry))
3502 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
3504 list_add(&mixer->list, &chip->mixer_list);
3508 snd_usb_mixer_free(mixer);
3512 void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
3514 if (mixer->disconnected)
3517 usb_kill_urb(mixer->urb);
3519 usb_kill_urb(mixer->rc_urb);
3520 mixer->disconnected = true;
3524 /* stop any bus activity of a mixer */
3525 static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3527 usb_kill_urb(mixer->urb);
3528 usb_kill_urb(mixer->rc_urb);
3531 static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3536 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3544 int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3546 snd_usb_mixer_inactivate(mixer);
3550 static int restore_mixer_value(struct usb_mixer_elem_list *list)
3552 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
3557 for (c = 0; c < MAX_CHANNELS; c++) {
3558 if (!(cval->cmask & (1 << c)))
3560 if (cval->cached & (1 << (c + 1))) {
3561 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
3562 cval->cache_val[idx]);
3571 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
3580 int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3582 struct usb_mixer_elem_list *list;
3586 /* restore cached mixer values */
3587 for (id = 0; id < MAX_ID_ELEMS; id++) {
3588 for_each_mixer_elem(list, mixer, id) {
3590 err = list->resume(list);
3598 snd_usb_mixer_resume_quirk(mixer);
3600 return snd_usb_mixer_activate(mixer);
3604 void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3605 struct usb_mixer_interface *mixer,
3608 list->mixer = mixer;
3610 list->dump = snd_usb_mixer_dump_cval;
3612 list->resume = restore_mixer_value;