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