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) { |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 678 | case UAC_SELECTOR_UNIT: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 679 | strcpy(name, "Selector"); |
| 680 | return 8; |
Daniel Mack | 69da9bc | 2010-06-16 17:57:28 +0200 | [diff] [blame] | 681 | case UAC1_PROCESSING_UNIT: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 682 | strcpy(name, "Process Unit"); |
| 683 | return 12; |
Daniel Mack | 69da9bc | 2010-06-16 17:57:28 +0200 | [diff] [blame] | 684 | case UAC1_EXTENSION_UNIT: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 685 | strcpy(name, "Ext Unit"); |
| 686 | return 8; |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 687 | case UAC_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; |
| 756 | |
| 757 | if (desc->bLength < 11) |
| 758 | return -EINVAL; |
| 759 | if (!desc->bNrInPins) |
| 760 | return -EINVAL; |
| 761 | |
| 762 | switch (state->mixer->protocol) { |
| 763 | case UAC_VERSION_1: |
| 764 | case UAC_VERSION_2: |
| 765 | default: |
| 766 | mu_channels = uac_mixer_unit_bNrChannels(desc); |
| 767 | break; |
| 768 | case UAC_VERSION_3: |
| 769 | mu_channels = get_cluster_channels_v3(state, |
| 770 | uac3_mixer_unit_wClusterDescrID(desc)); |
| 771 | break; |
| 772 | } |
| 773 | |
| 774 | if (!mu_channels) |
| 775 | return -EINVAL; |
| 776 | |
| 777 | return mu_channels; |
| 778 | } |
| 779 | |
| 780 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | * parse the source unit recursively until it reaches to a terminal |
| 782 | * or a branched unit. |
| 783 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 784 | static int check_input_term(struct mixer_build *state, int id, |
| 785 | struct usb_audio_term *term) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | { |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 787 | int protocol = state->mixer->protocol; |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 788 | int err; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 789 | void *p1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
| 791 | memset(term, 0, sizeof(*term)); |
| 792 | while ((p1 = find_audio_control_unit(state, id)) != NULL) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 793 | unsigned char *hdr = p1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | term->id = id; |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 795 | |
| 796 | if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) { |
| 797 | switch (hdr[2]) { |
| 798 | case UAC_INPUT_TERMINAL: |
| 799 | if (protocol == UAC_VERSION_1) { |
| 800 | struct uac_input_terminal_descriptor *d = p1; |
| 801 | |
| 802 | term->type = le16_to_cpu(d->wTerminalType); |
| 803 | term->channels = d->bNrChannels; |
| 804 | term->chconfig = le16_to_cpu(d->wChannelConfig); |
| 805 | term->name = d->iTerminal; |
| 806 | } else { /* UAC_VERSION_2 */ |
| 807 | struct uac2_input_terminal_descriptor *d = p1; |
| 808 | |
| 809 | /* call recursively to verify that the |
| 810 | * referenced clock entity is valid */ |
| 811 | err = check_input_term(state, d->bCSourceID, term); |
| 812 | if (err < 0) |
| 813 | return err; |
| 814 | |
| 815 | /* save input term properties after recursion, |
| 816 | * to ensure they are not overriden by the |
| 817 | * recursion calls */ |
| 818 | term->id = id; |
| 819 | term->type = le16_to_cpu(d->wTerminalType); |
| 820 | term->channels = d->bNrChannels; |
| 821 | term->chconfig = le32_to_cpu(d->bmChannelConfig); |
| 822 | term->name = d->iTerminal; |
| 823 | } |
| 824 | return 0; |
| 825 | case UAC_FEATURE_UNIT: { |
| 826 | /* the header is the same for v1 and v2 */ |
| 827 | struct uac_feature_unit_descriptor *d = p1; |
| 828 | |
| 829 | id = d->bSourceID; |
| 830 | break; /* continue to parse */ |
| 831 | } |
| 832 | case UAC_MIXER_UNIT: { |
| 833 | struct uac_mixer_unit_descriptor *d = p1; |
| 834 | |
| 835 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
| 836 | term->channels = uac_mixer_unit_bNrChannels(d); |
| 837 | term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol); |
| 838 | term->name = uac_mixer_unit_iMixer(d); |
| 839 | return 0; |
| 840 | } |
| 841 | case UAC_SELECTOR_UNIT: |
| 842 | case UAC2_CLOCK_SELECTOR: { |
| 843 | struct uac_selector_unit_descriptor *d = p1; |
| 844 | /* call recursively to retrieve the channel info */ |
| 845 | err = check_input_term(state, d->baSourceID[0], term); |
| 846 | if (err < 0) |
| 847 | return err; |
| 848 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
| 849 | term->id = id; |
| 850 | term->name = uac_selector_unit_iSelector(d); |
| 851 | return 0; |
| 852 | } |
| 853 | case UAC1_PROCESSING_UNIT: |
| 854 | case UAC1_EXTENSION_UNIT: |
| 855 | /* UAC2_PROCESSING_UNIT_V2 */ |
| 856 | /* UAC2_EFFECT_UNIT */ |
| 857 | case UAC2_EXTENSION_UNIT_V2: { |
| 858 | struct uac_processing_unit_descriptor *d = p1; |
| 859 | |
| 860 | if (protocol == UAC_VERSION_2 && |
| 861 | hdr[2] == UAC2_EFFECT_UNIT) { |
| 862 | /* UAC2/UAC1 unit IDs overlap here in an |
| 863 | * uncompatible way. Ignore this unit for now. |
| 864 | */ |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | if (d->bNrInPins) { |
| 869 | id = d->baSourceID[0]; |
| 870 | break; /* continue to parse */ |
| 871 | } |
| 872 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
| 873 | term->channels = uac_processing_unit_bNrChannels(d); |
| 874 | term->chconfig = uac_processing_unit_wChannelConfig(d, protocol); |
| 875 | term->name = uac_processing_unit_iProcessing(d, protocol); |
| 876 | return 0; |
| 877 | } |
| 878 | case UAC2_CLOCK_SOURCE: { |
| 879 | struct uac_clock_source_descriptor *d = p1; |
| 880 | |
| 881 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
| 882 | term->id = id; |
| 883 | term->name = d->iClockSource; |
| 884 | return 0; |
| 885 | } |
| 886 | default: |
| 887 | return -ENODEV; |
| 888 | } |
| 889 | } else { /* UAC_VERSION_3 */ |
| 890 | switch (hdr[2]) { |
| 891 | case UAC_INPUT_TERMINAL: { |
| 892 | struct uac3_input_terminal_descriptor *d = p1; |
Julian Scheel | 9430e54 | 2015-08-19 09:28:09 +0200 | [diff] [blame] | 893 | |
| 894 | /* call recursively to verify that the |
| 895 | * referenced clock entity is valid */ |
| 896 | err = check_input_term(state, d->bCSourceID, term); |
| 897 | if (err < 0) |
| 898 | return err; |
| 899 | |
| 900 | /* save input term properties after recursion, |
| 901 | * to ensure they are not overriden by the |
| 902 | * recursion calls */ |
| 903 | term->id = id; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 904 | term->type = le16_to_cpu(d->wTerminalType); |
Eldad Zack | 5dae5fd | 2012-11-28 23:55:36 +0100 | [diff] [blame] | 905 | |
Jorge Sanjuan | 7106694 | 2018-05-14 12:03:42 +0100 | [diff] [blame] | 906 | err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID)); |
| 907 | if (err < 0) |
| 908 | return err; |
| 909 | term->channels = err; |
| 910 | |
| 911 | /* REVISIT: UAC3 IT doesn't have channels cfg */ |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 912 | term->chconfig = 0; |
| 913 | |
| 914 | term->name = le16_to_cpu(d->wTerminalDescrStr); |
Eldad Zack | 5dae5fd | 2012-11-28 23:55:36 +0100 | [diff] [blame] | 915 | return 0; |
| 916 | } |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 917 | case UAC3_FEATURE_UNIT: { |
| 918 | struct uac3_feature_unit_descriptor *d = p1; |
Eldad Zack | 5dae5fd | 2012-11-28 23:55:36 +0100 | [diff] [blame] | 919 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 920 | id = d->bSourceID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | break; /* continue to parse */ |
| 922 | } |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 923 | case UAC3_CLOCK_SOURCE: { |
| 924 | struct uac3_clock_source_descriptor *d = p1; |
| 925 | |
| 926 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
| 927 | term->id = id; |
| 928 | term->name = le16_to_cpu(d->wClockSourceStr); |
| 929 | return 0; |
| 930 | } |
Jorge Sanjuan | 6cfd839 | 2018-05-11 16:25:34 +0100 | [diff] [blame] | 931 | case UAC3_MIXER_UNIT: { |
| 932 | struct uac_mixer_unit_descriptor *d = p1; |
| 933 | |
| 934 | err = uac_mixer_unit_get_channels(state, d); |
| 935 | if (err < 0) |
| 936 | return err; |
| 937 | |
| 938 | term->channels = err; |
| 939 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
| 940 | |
| 941 | return 0; |
| 942 | } |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 943 | default: |
| 944 | return -ENODEV; |
| 945 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | return -ENODEV; |
| 949 | } |
| 950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | /* |
| 952 | * Feature Unit |
| 953 | */ |
| 954 | |
| 955 | /* feature unit control information */ |
| 956 | struct usb_feature_control_info { |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 957 | int control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | const char *name; |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 959 | int type; /* data type for uac1 */ |
| 960 | 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] | 961 | }; |
| 962 | |
| 963 | static struct usb_feature_control_info audio_feature_info[] = { |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 964 | { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 }, |
| 965 | { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 }, |
| 966 | { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 }, |
| 967 | { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 }, |
| 968 | { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 }, |
| 969 | { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */ |
| 970 | { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 }, |
| 971 | { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 }, |
| 972 | { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 }, |
| 973 | { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 }, |
Daniel Mack | 2e0281d | 2010-05-31 13:35:42 +0200 | [diff] [blame] | 974 | /* UAC2 specific */ |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 975 | { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 }, |
| 976 | { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 }, |
| 977 | { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | }; |
| 979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | /* private_free callback */ |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 981 | void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | { |
Jesper Juhl | 4d57277 | 2005-05-30 17:30:32 +0200 | [diff] [blame] | 983 | kfree(kctl->private_data); |
| 984 | kctl->private_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | /* |
| 988 | * interface to ALSA control for feature/mixer units |
| 989 | */ |
| 990 | |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 991 | /* volume control quirks */ |
| 992 | static void volume_control_quirks(struct usb_mixer_elem_info *cval, |
| 993 | struct snd_kcontrol *kctl) |
| 994 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 995 | struct snd_usb_audio *chip = cval->head.mixer->chip; |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 996 | switch (chip->usb_id) { |
Eldad Zack | d50ed62 | 2012-11-28 23:55:39 +0100 | [diff] [blame] | 997 | case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */ |
Matt Gruskin | e9a25e0 | 2013-02-09 12:56:35 -0500 | [diff] [blame] | 998 | case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */ |
Eldad Zack | d50ed62 | 2012-11-28 23:55:39 +0100 | [diff] [blame] | 999 | if (strcmp(kctl->id.name, "Effect Duration") == 0) { |
| 1000 | cval->min = 0x0000; |
| 1001 | cval->max = 0xffff; |
| 1002 | cval->res = 0x00e6; |
| 1003 | break; |
| 1004 | } |
| 1005 | if (strcmp(kctl->id.name, "Effect Volume") == 0 || |
| 1006 | strcmp(kctl->id.name, "Effect Feedback Volume") == 0) { |
| 1007 | cval->min = 0x00; |
| 1008 | cval->max = 0xff; |
| 1009 | break; |
| 1010 | } |
| 1011 | if (strstr(kctl->id.name, "Effect Return") != NULL) { |
| 1012 | cval->min = 0xb706; |
| 1013 | cval->max = 0xff7b; |
| 1014 | cval->res = 0x0073; |
| 1015 | break; |
| 1016 | } |
| 1017 | if ((strstr(kctl->id.name, "Playback Volume") != NULL) || |
| 1018 | (strstr(kctl->id.name, "Effect Send") != NULL)) { |
| 1019 | cval->min = 0xb5fb; /* -73 dB = 0xb6ff */ |
| 1020 | cval->max = 0xfcfe; |
| 1021 | cval->res = 0x0073; |
| 1022 | } |
| 1023 | break; |
| 1024 | |
Felix Homann | d34bf14 | 2012-04-23 20:24:27 +0200 | [diff] [blame] | 1025 | case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */ |
| 1026 | case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */ |
| 1027 | if (strcmp(kctl->id.name, "Effect Duration") == 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1028 | usb_audio_info(chip, |
| 1029 | "set quirk for FTU Effect Duration\n"); |
Felix Homann | d34bf14 | 2012-04-23 20:24:27 +0200 | [diff] [blame] | 1030 | cval->min = 0x0000; |
| 1031 | cval->max = 0x7f00; |
| 1032 | cval->res = 0x0100; |
| 1033 | break; |
| 1034 | } |
| 1035 | if (strcmp(kctl->id.name, "Effect Volume") == 0 || |
| 1036 | strcmp(kctl->id.name, "Effect Feedback Volume") == 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1037 | usb_audio_info(chip, |
| 1038 | "set quirks for FTU Effect Feedback/Volume\n"); |
Felix Homann | d34bf14 | 2012-04-23 20:24:27 +0200 | [diff] [blame] | 1039 | cval->min = 0x00; |
| 1040 | cval->max = 0x7f; |
| 1041 | break; |
| 1042 | } |
| 1043 | break; |
| 1044 | |
Federico Cuello | 2149331 | 2018-05-09 00:13:38 +0200 | [diff] [blame] | 1045 | case USB_ID(0x0d8c, 0x0103): |
| 1046 | if (!strcmp(kctl->id.name, "PCM Playback Volume")) { |
| 1047 | usb_audio_info(chip, |
| 1048 | "set volume quirk for CM102-A+/102S+\n"); |
| 1049 | cval->min = -256; |
| 1050 | } |
| 1051 | break; |
| 1052 | |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1053 | case USB_ID(0x0471, 0x0101): |
| 1054 | case USB_ID(0x0471, 0x0104): |
| 1055 | case USB_ID(0x0471, 0x0105): |
| 1056 | case USB_ID(0x0672, 0x1041): |
| 1057 | /* quirk for UDA1321/N101. |
| 1058 | * note that detection between firmware 2.1.1.7 (N101) |
| 1059 | * and later 2.1.1.21 is not very clear from datasheets. |
| 1060 | * I hope that the min value is -15360 for newer firmware --jk |
| 1061 | */ |
| 1062 | if (!strcmp(kctl->id.name, "PCM Playback Volume") && |
| 1063 | cval->min == -15616) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1064 | usb_audio_info(chip, |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1065 | "set volume quirk for UDA1321/N101 chip\n"); |
| 1066 | cval->max = -256; |
| 1067 | } |
| 1068 | break; |
| 1069 | |
| 1070 | case USB_ID(0x046d, 0x09a4): |
| 1071 | if (!strcmp(kctl->id.name, "Mic Capture Volume")) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1072 | usb_audio_info(chip, |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1073 | "set volume quirk for QuickCam E3500\n"); |
| 1074 | cval->min = 6080; |
| 1075 | cval->max = 8768; |
| 1076 | cval->res = 192; |
| 1077 | } |
| 1078 | break; |
| 1079 | |
Takashi Iwai | e805ca8 | 2014-03-05 12:34:39 +0100 | [diff] [blame] | 1080 | case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */ |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1081 | case USB_ID(0x046d, 0x0808): |
| 1082 | case USB_ID(0x046d, 0x0809): |
Jason Lee Cragg | 6455931 | 2015-01-17 12:28:29 -0500 | [diff] [blame] | 1083 | case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */ |
Takashi Iwai | 36691e1 | 2013-06-17 10:25:02 +0200 | [diff] [blame] | 1084 | case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */ |
Alexey Fisher | 55c0008 | 2011-11-09 11:39:24 +0100 | [diff] [blame] | 1085 | case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */ |
Takashi Iwai | 11e7064 | 2013-06-05 08:35:26 +0200 | [diff] [blame] | 1086 | case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */ |
Maksim A. Boyko | 140d37d | 2013-08-10 12:20:02 +0400 | [diff] [blame] | 1087 | case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */ |
Wolfram Sang | 1ef9f05 | 2015-05-29 19:50:56 +0900 | [diff] [blame] | 1088 | case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */ |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1089 | case USB_ID(0x046d, 0x0991): |
Con Kolivas | 82ffb6f | 2016-12-09 15:15:57 +1100 | [diff] [blame] | 1090 | case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */ |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1091 | /* Most audio usb devices lie about volume resolution. |
| 1092 | * Most Logitech webcams have res = 384. |
Con Kolivas | 82ffb6f | 2016-12-09 15:15:57 +1100 | [diff] [blame] | 1093 | * Probably there is some logitech magic behind this number --fishor |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1094 | */ |
| 1095 | if (!strcmp(kctl->id.name, "Mic Capture Volume")) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1096 | usb_audio_info(chip, |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1097 | "set resolution quirk: cval->res = 384\n"); |
| 1098 | cval->res = 384; |
| 1099 | } |
| 1100 | break; |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | /* |
| 1105 | * retrieve the minimum and maximum values for the specified control |
| 1106 | */ |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1107 | static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval, |
| 1108 | int default_min, struct snd_kcontrol *kctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | { |
| 1110 | /* for failsafe */ |
| 1111 | cval->min = default_min; |
| 1112 | cval->max = cval->min + 1; |
| 1113 | cval->res = 1; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1114 | cval->dBmin = cval->dBmax = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | if (cval->val_type == USB_MIXER_BOOLEAN || |
| 1117 | cval->val_type == USB_MIXER_INV_BOOLEAN) { |
| 1118 | cval->initialized = 1; |
| 1119 | } else { |
| 1120 | int minchn = 0; |
| 1121 | if (cval->cmask) { |
| 1122 | int i; |
| 1123 | for (i = 0; i < MAX_CHANNELS; i++) |
| 1124 | if (cval->cmask & (1 << i)) { |
| 1125 | minchn = i + 1; |
| 1126 | break; |
| 1127 | } |
| 1128 | } |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1129 | if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 || |
| 1130 | 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] | 1131 | usb_audio_err(cval->head.mixer->chip, |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1132 | "%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] | 1133 | cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip), |
| 1134 | cval->control, cval->head.id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | return -EINVAL; |
| 1136 | } |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1137 | if (get_ctl_value(cval, UAC_GET_RES, |
| 1138 | (cval->control << 8) | minchn, |
| 1139 | &cval->res) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | cval->res = 1; |
| 1141 | } else { |
| 1142 | int last_valid_res = cval->res; |
| 1143 | |
| 1144 | while (cval->res > 1) { |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 1145 | if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1146 | (cval->control << 8) | minchn, |
| 1147 | cval->res / 2) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | break; |
| 1149 | cval->res /= 2; |
| 1150 | } |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1151 | if (get_ctl_value(cval, UAC_GET_RES, |
| 1152 | (cval->control << 8) | minchn, &cval->res) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | cval->res = last_valid_res; |
| 1154 | } |
| 1155 | if (cval->res == 0) |
| 1156 | cval->res = 1; |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 1157 | |
| 1158 | /* Additional checks for the proper resolution |
| 1159 | * |
| 1160 | * Some devices report smaller resolutions than actually |
| 1161 | * reacting. They don't return errors but simply clip |
| 1162 | * to the lower aligned value. |
| 1163 | */ |
| 1164 | if (cval->min + cval->res < cval->max) { |
| 1165 | int last_valid_res = cval->res; |
| 1166 | int saved, test, check; |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1167 | get_cur_mix_raw(cval, minchn, &saved); |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 1168 | for (;;) { |
| 1169 | test = saved; |
| 1170 | if (test < cval->max) |
| 1171 | test += cval->res; |
| 1172 | else |
| 1173 | test -= cval->res; |
| 1174 | if (test < cval->min || test > cval->max || |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1175 | snd_usb_set_cur_mix_value(cval, minchn, 0, test) || |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1176 | get_cur_mix_raw(cval, minchn, &check)) { |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 1177 | cval->res = last_valid_res; |
| 1178 | break; |
| 1179 | } |
| 1180 | if (test == check) |
| 1181 | break; |
| 1182 | cval->res *= 2; |
| 1183 | } |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1184 | snd_usb_set_cur_mix_value(cval, minchn, 0, saved); |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 1185 | } |
| 1186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | cval->initialized = 1; |
| 1188 | } |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1189 | |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1190 | if (kctl) |
| 1191 | volume_control_quirks(cval, kctl); |
| 1192 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1193 | /* USB descriptions contain the dB scale in 1/256 dB unit |
| 1194 | * while ALSA TLV contains in 1/100 dB unit |
| 1195 | */ |
| 1196 | cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256; |
| 1197 | cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256; |
| 1198 | if (cval->dBmin > cval->dBmax) { |
| 1199 | /* something is wrong; assume it's either from/to 0dB */ |
| 1200 | if (cval->dBmin < 0) |
| 1201 | cval->dBmax = 0; |
| 1202 | else if (cval->dBmin > 0) |
| 1203 | cval->dBmin = 0; |
| 1204 | if (cval->dBmin > cval->dBmax) { |
| 1205 | /* totally crap, return an error */ |
| 1206 | return -EINVAL; |
| 1207 | } |
| 1208 | } |
| 1209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | return 0; |
| 1211 | } |
| 1212 | |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1213 | #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] | 1214 | |
| 1215 | /* get a feature/mixer unit info */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1216 | static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, |
| 1217 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1219 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | |
| 1221 | if (cval->val_type == USB_MIXER_BOOLEAN || |
| 1222 | cval->val_type == USB_MIXER_INV_BOOLEAN) |
| 1223 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1224 | else |
| 1225 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1226 | uinfo->count = cval->channels; |
| 1227 | if (cval->val_type == USB_MIXER_BOOLEAN || |
| 1228 | cval->val_type == USB_MIXER_INV_BOOLEAN) { |
| 1229 | uinfo->value.integer.min = 0; |
| 1230 | uinfo->value.integer.max = 1; |
| 1231 | } else { |
Takashi Iwai | 9fcd0ab | 2011-08-19 08:30:53 +0200 | [diff] [blame] | 1232 | if (!cval->initialized) { |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1233 | get_min_max_with_quirks(cval, 0, kcontrol); |
Takashi Iwai | 9fcd0ab | 2011-08-19 08:30:53 +0200 | [diff] [blame] | 1234 | if (cval->initialized && cval->dBmin >= cval->dBmax) { |
| 1235 | kcontrol->vd[0].access &= |
| 1236 | ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ | |
| 1237 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK); |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 1238 | snd_ctl_notify(cval->head.mixer->chip->card, |
Takashi Iwai | 9fcd0ab | 2011-08-19 08:30:53 +0200 | [diff] [blame] | 1239 | SNDRV_CTL_EVENT_MASK_INFO, |
| 1240 | &kcontrol->id); |
| 1241 | } |
| 1242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | uinfo->value.integer.min = 0; |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 1244 | uinfo->value.integer.max = |
| 1245 | (cval->max - cval->min + cval->res - 1) / cval->res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | } |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | /* get the current value from feature/mixer unit */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1251 | static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, |
| 1252 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1254 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | int c, cnt, val, err; |
| 1256 | |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1257 | ucontrol->value.integer.value[0] = cval->min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | if (cval->cmask) { |
| 1259 | cnt = 0; |
| 1260 | for (c = 0; c < MAX_CHANNELS; c++) { |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1261 | if (!(cval->cmask & (1 << c))) |
| 1262 | continue; |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1263 | err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1264 | if (err < 0) |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 1265 | return filter_error(cval, err); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1266 | val = get_relative_value(cval, val); |
| 1267 | ucontrol->value.integer.value[cnt] = val; |
| 1268 | cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | } |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1270 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | } else { |
| 1272 | /* master channel */ |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1273 | err = snd_usb_get_cur_mix_value(cval, 0, 0, &val); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1274 | if (err < 0) |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 1275 | return filter_error(cval, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | val = get_relative_value(cval, val); |
| 1277 | ucontrol->value.integer.value[0] = val; |
| 1278 | } |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
| 1282 | /* put the current value to feature/mixer unit */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1283 | static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, |
| 1284 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1286 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | int c, cnt, val, oval, err; |
| 1288 | int changed = 0; |
| 1289 | |
| 1290 | if (cval->cmask) { |
| 1291 | cnt = 0; |
| 1292 | for (c = 0; c < MAX_CHANNELS; c++) { |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1293 | if (!(cval->cmask & (1 << c))) |
| 1294 | continue; |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1295 | err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1296 | if (err < 0) |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 1297 | return filter_error(cval, err); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1298 | val = ucontrol->value.integer.value[cnt]; |
| 1299 | val = get_abs_value(cval, val); |
| 1300 | if (oval != val) { |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1301 | snd_usb_set_cur_mix_value(cval, c + 1, cnt, val); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1302 | changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | } |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1304 | cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | } |
| 1306 | } else { |
| 1307 | /* master channel */ |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1308 | err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | if (err < 0) |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 1310 | return filter_error(cval, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | val = ucontrol->value.integer.value[0]; |
| 1312 | val = get_abs_value(cval, val); |
| 1313 | if (val != oval) { |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1314 | snd_usb_set_cur_mix_value(cval, 0, 0, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | changed = 1; |
| 1316 | } |
| 1317 | } |
| 1318 | return changed; |
| 1319 | } |
| 1320 | |
Andrew Chant | 568fa7e | 2018-03-23 19:25:24 -0700 | [diff] [blame] | 1321 | /* get the boolean value from the master channel of a UAC control */ |
| 1322 | static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol, |
| 1323 | struct snd_ctl_elem_value *ucontrol) |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 1324 | { |
| 1325 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
| 1326 | int val, err; |
| 1327 | |
| 1328 | err = snd_usb_get_cur_mix_value(cval, 0, 0, &val); |
| 1329 | if (err < 0) |
| 1330 | return filter_error(cval, err); |
| 1331 | val = (val != 0); |
| 1332 | ucontrol->value.integer.value[0] = val; |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
Jorge Sanjuan | 1d38f5d | 2018-05-11 16:25:36 +0100 | [diff] [blame] | 1336 | /* get the connectors status and report it as boolean type */ |
| 1337 | static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol, |
| 1338 | struct snd_ctl_elem_value *ucontrol) |
| 1339 | { |
| 1340 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
| 1341 | struct snd_usb_audio *chip = cval->head.mixer->chip; |
| 1342 | int idx = 0, validx, ret, val; |
| 1343 | |
| 1344 | validx = cval->control << 8 | 0; |
| 1345 | |
| 1346 | ret = snd_usb_lock_shutdown(chip) ? -EIO : 0; |
| 1347 | if (ret) |
| 1348 | goto error; |
| 1349 | |
| 1350 | idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8); |
| 1351 | if (cval->head.mixer->protocol == UAC_VERSION_2) { |
| 1352 | struct uac2_connectors_ctl_blk uac2_conn; |
| 1353 | |
| 1354 | ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR, |
| 1355 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, |
| 1356 | validx, idx, &uac2_conn, sizeof(uac2_conn)); |
| 1357 | val = !!uac2_conn.bNrChannels; |
| 1358 | } else { /* UAC_VERSION_3 */ |
| 1359 | struct uac3_insertion_ctl_blk uac3_conn; |
| 1360 | |
| 1361 | ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR, |
| 1362 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, |
| 1363 | validx, idx, &uac3_conn, sizeof(uac3_conn)); |
| 1364 | val = !!uac3_conn.bmConInserted; |
| 1365 | } |
| 1366 | |
| 1367 | snd_usb_unlock_shutdown(chip); |
| 1368 | |
| 1369 | if (ret < 0) { |
| 1370 | error: |
| 1371 | usb_audio_err(chip, |
| 1372 | "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", |
| 1373 | UAC_GET_CUR, validx, idx, cval->val_type); |
| 1374 | return ret; |
| 1375 | } |
| 1376 | |
| 1377 | ucontrol->value.integer.value[0] = val; |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1381 | static struct snd_kcontrol_new usb_feature_unit_ctl = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1383 | .name = "", /* will be filled later manually */ |
| 1384 | .info = mixer_ctl_feature_info, |
| 1385 | .get = mixer_ctl_feature_get, |
| 1386 | .put = mixer_ctl_feature_put, |
| 1387 | }; |
| 1388 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1389 | /* the read-only variant */ |
Bhumika Goyal | 8fdaebb | 2017-04-12 18:38:06 +0530 | [diff] [blame] | 1390 | static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1391 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1392 | .name = "", /* will be filled later manually */ |
| 1393 | .info = mixer_ctl_feature_info, |
| 1394 | .get = mixer_ctl_feature_get, |
| 1395 | .put = NULL, |
| 1396 | }; |
| 1397 | |
Andrew Chant | 568fa7e | 2018-03-23 19:25:24 -0700 | [diff] [blame] | 1398 | /* |
| 1399 | * A control which shows the boolean value from reading a UAC control on |
| 1400 | * the master channel. |
| 1401 | */ |
| 1402 | static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = { |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 1403 | .iface = SNDRV_CTL_ELEM_IFACE_CARD, |
| 1404 | .name = "", /* will be filled later manually */ |
| 1405 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1406 | .info = snd_ctl_boolean_mono_info, |
Andrew Chant | 568fa7e | 2018-03-23 19:25:24 -0700 | [diff] [blame] | 1407 | .get = mixer_ctl_master_bool_get, |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 1408 | .put = NULL, |
| 1409 | }; |
| 1410 | |
Jorge Sanjuan | 1d38f5d | 2018-05-11 16:25:36 +0100 | [diff] [blame] | 1411 | static const struct snd_kcontrol_new usb_connector_ctl_ro = { |
| 1412 | .iface = SNDRV_CTL_ELEM_IFACE_CARD, |
| 1413 | .name = "", /* will be filled later manually */ |
| 1414 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1415 | .info = snd_ctl_boolean_mono_info, |
| 1416 | .get = mixer_ctl_connector_get, |
| 1417 | .put = NULL, |
| 1418 | }; |
| 1419 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1420 | /* |
| 1421 | * This symbol is exported in order to allow the mixer quirks to |
| 1422 | * hook up to the standard feature unit control mechanism |
| 1423 | */ |
Daniel Mack | 9e38658 | 2011-05-25 09:09:01 +0200 | [diff] [blame] | 1424 | 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] | 1425 | |
| 1426 | /* |
| 1427 | * build a feature control |
| 1428 | */ |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 1429 | static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str) |
| 1430 | { |
| 1431 | return strlcat(kctl->id.name, str, sizeof(kctl->id.name)); |
| 1432 | } |
| 1433 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1434 | /* |
| 1435 | * A lot of headsets/headphones have a "Speaker" mixer. Make sure we |
| 1436 | * rename it to "Headphone". We determine if something is a headphone |
| 1437 | * similar to how udev determines form factor. |
| 1438 | */ |
David Henningsson | 9b4ef97 | 2012-11-23 13:48:55 +0100 | [diff] [blame] | 1439 | static void check_no_speaker_on_headset(struct snd_kcontrol *kctl, |
| 1440 | struct snd_card *card) |
| 1441 | { |
| 1442 | const char *names_to_check[] = { |
| 1443 | "Headset", "headset", "Headphone", "headphone", NULL}; |
| 1444 | const char **s; |
Peter Senna Tschudin | e0f17c7 | 2013-09-22 20:44:12 +0200 | [diff] [blame] | 1445 | bool found = false; |
David Henningsson | 9b4ef97 | 2012-11-23 13:48:55 +0100 | [diff] [blame] | 1446 | |
| 1447 | if (strcmp("Speaker", kctl->id.name)) |
| 1448 | return; |
| 1449 | |
| 1450 | for (s = names_to_check; *s; s++) |
| 1451 | if (strstr(card->shortname, *s)) { |
Peter Senna Tschudin | e0f17c7 | 2013-09-22 20:44:12 +0200 | [diff] [blame] | 1452 | found = true; |
David Henningsson | 9b4ef97 | 2012-11-23 13:48:55 +0100 | [diff] [blame] | 1453 | break; |
| 1454 | } |
| 1455 | |
| 1456 | if (!found) |
| 1457 | return; |
| 1458 | |
| 1459 | strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name)); |
| 1460 | } |
| 1461 | |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1462 | static struct usb_feature_control_info *get_feature_control_info(int control) |
| 1463 | { |
| 1464 | int i; |
| 1465 | |
| 1466 | for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) { |
| 1467 | if (audio_feature_info[i].control == control) |
| 1468 | return &audio_feature_info[i]; |
| 1469 | } |
| 1470 | return NULL; |
| 1471 | } |
| 1472 | |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1473 | static void __build_feature_ctl(struct usb_mixer_interface *mixer, |
| 1474 | const struct usbmix_name_map *imap, |
| 1475 | unsigned int ctl_mask, int control, |
| 1476 | struct usb_audio_term *iterm, |
| 1477 | struct usb_audio_term *oterm, |
| 1478 | int unitid, int nameid, int readonly_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | { |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 1480 | struct usb_feature_control_info *ctl_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | unsigned int len = 0; |
| 1482 | int mapped_name = 0; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1483 | struct snd_kcontrol *kctl; |
| 1484 | struct usb_mixer_elem_info *cval; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1485 | const struct usbmix_name_map *map; |
Alexey Fisher | 80aceff | 2011-03-10 14:53:38 +0100 | [diff] [blame] | 1486 | unsigned int range; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 1488 | if (control == UAC_FU_GRAPHIC_EQUALIZER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | /* FIXME: not supported yet */ |
| 1490 | return; |
| 1491 | } |
| 1492 | |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1493 | map = find_map(imap, unitid, control); |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1494 | if (check_ignored_ctl(map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | return; |
| 1496 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 1497 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
Daniel Mack | a860d95 | 2014-05-24 10:58:17 +0200 | [diff] [blame] | 1498 | if (!cval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | return; |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1500 | snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | cval->control = control; |
| 1502 | cval->cmask = ctl_mask; |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1503 | |
| 1504 | ctl_info = get_feature_control_info(control); |
Colin Ian King | 6237602 | 2018-03-27 15:30:01 +0100 | [diff] [blame] | 1505 | if (!ctl_info) { |
| 1506 | kfree(cval); |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1507 | return; |
Colin Ian King | 6237602 | 2018-03-27 15:30:01 +0100 | [diff] [blame] | 1508 | } |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1509 | if (mixer->protocol == UAC_VERSION_1) |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 1510 | cval->val_type = ctl_info->type; |
| 1511 | else /* UAC_VERSION_2 */ |
| 1512 | cval->val_type = ctl_info->type_uac2 >= 0 ? |
| 1513 | ctl_info->type_uac2 : ctl_info->type; |
| 1514 | |
Daniel Mack | a6a3325 | 2010-05-31 13:35:37 +0200 | [diff] [blame] | 1515 | if (ctl_mask == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | cval->channels = 1; /* master channel */ |
Daniel Mack | a6a3325 | 2010-05-31 13:35:37 +0200 | [diff] [blame] | 1517 | cval->master_readonly = readonly_mask; |
| 1518 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | int i, c = 0; |
| 1520 | for (i = 0; i < 16; i++) |
| 1521 | if (ctl_mask & (1 << i)) |
| 1522 | c++; |
| 1523 | cval->channels = c; |
Daniel Mack | a6a3325 | 2010-05-31 13:35:37 +0200 | [diff] [blame] | 1524 | cval->ch_readonly = readonly_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1527 | /* |
| 1528 | * 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] | 1529 | * 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] | 1530 | * issue write commands to read-only channels. |
| 1531 | */ |
Daniel Mack | a6a3325 | 2010-05-31 13:35:37 +0200 | [diff] [blame] | 1532 | if (cval->channels == readonly_mask) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1533 | kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval); |
| 1534 | else |
| 1535 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); |
| 1536 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1537 | if (!kctl) { |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1538 | usb_audio_err(mixer->chip, "cannot malloc kcontrol\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | kfree(cval); |
| 1540 | return; |
| 1541 | } |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1542 | kctl->private_free = snd_usb_mixer_elem_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1544 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | mapped_name = len != 0; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1546 | if (!len && nameid) |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1547 | len = snd_usb_copy_string_desc(mixer->chip, nameid, |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1548 | kctl->id.name, sizeof(kctl->id.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | |
| 1550 | switch (control) { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 1551 | case UAC_FU_MUTE: |
| 1552 | case UAC_FU_VOLUME: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1553 | /* |
| 1554 | * determine the control name. the rule is: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | * - if a name id is given in descriptor, use it. |
| 1556 | * - if the connected input can be determined, then use the name |
| 1557 | * of terminal type. |
| 1558 | * - if the connected output can be determined, use it. |
| 1559 | * - otherwise, anonymous name. |
| 1560 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1561 | if (!len) { |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1562 | if (iterm) |
| 1563 | len = get_term_name(mixer->chip, iterm, |
| 1564 | kctl->id.name, |
| 1565 | sizeof(kctl->id.name), 1); |
| 1566 | if (!len && oterm) |
| 1567 | len = get_term_name(mixer->chip, oterm, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1568 | kctl->id.name, |
| 1569 | sizeof(kctl->id.name), 1); |
| 1570 | if (!len) |
Daniel Mack | 49fd46d | 2014-10-19 09:11:25 +0200 | [diff] [blame] | 1571 | snprintf(kctl->id.name, sizeof(kctl->id.name), |
| 1572 | "Feature %d", unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | } |
David Henningsson | 9b4ef97 | 2012-11-23 13:48:55 +0100 | [diff] [blame] | 1574 | |
| 1575 | if (!mapped_name) |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1576 | check_no_speaker_on_headset(kctl, mixer->chip->card); |
David Henningsson | 9b4ef97 | 2012-11-23 13:48:55 +0100 | [diff] [blame] | 1577 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1578 | /* |
| 1579 | * determine the stream direction: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | * if the connected output is USB stream, then it's likely a |
| 1581 | * capture stream. otherwise it should be playback (hopefully :) |
| 1582 | */ |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1583 | if (!mapped_name && oterm && !(oterm->type >> 16)) { |
| 1584 | if ((oterm->type & 0xff00) == 0x0100) |
Daniel Mack | 49fd46d | 2014-10-19 09:11:25 +0200 | [diff] [blame] | 1585 | append_ctl_name(kctl, " Capture"); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1586 | else |
Daniel Mack | 49fd46d | 2014-10-19 09:11:25 +0200 | [diff] [blame] | 1587 | append_ctl_name(kctl, " Playback"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | } |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 1589 | append_ctl_name(kctl, control == UAC_FU_MUTE ? |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 1590 | " Switch" : " Volume"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | default: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1593 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | strlcpy(kctl->id.name, audio_feature_info[control-1].name, |
| 1595 | sizeof(kctl->id.name)); |
| 1596 | break; |
| 1597 | } |
| 1598 | |
Takashi Iwai | e182534 | 2012-05-14 17:11:06 +0200 | [diff] [blame] | 1599 | /* get min/max values */ |
| 1600 | get_min_max_with_quirks(cval, 0, kctl); |
| 1601 | |
| 1602 | if (control == UAC_FU_VOLUME) { |
| 1603 | check_mapped_dB(map, cval); |
| 1604 | if (cval->dBmin < cval->dBmax || !cval->initialized) { |
| 1605 | kctl->tlv.c = snd_usb_mixer_vol_tlv; |
| 1606 | kctl->vd[0].access |= |
| 1607 | SNDRV_CTL_ELEM_ACCESS_TLV_READ | |
| 1608 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; |
| 1609 | } |
| 1610 | } |
| 1611 | |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1612 | snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl); |
Anssi Hannula | 42e3121 | 2015-12-13 20:49:58 +0200 | [diff] [blame] | 1613 | |
Alexey Fisher | 80aceff | 2011-03-10 14:53:38 +0100 | [diff] [blame] | 1614 | range = (cval->max - cval->min) / cval->res; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1615 | /* |
| 1616 | * 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] | 1617 | * to be sure. 384 is a resolution magic number found on Logitech |
| 1618 | * devices. It will definitively catch all buggy Logitech devices. |
| 1619 | */ |
| 1620 | if (range > 384) { |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1621 | usb_audio_warn(mixer->chip, |
Michał Mirosław | 82c1cf0 | 2014-08-03 15:09:57 +0200 | [diff] [blame] | 1622 | "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.", |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1623 | range); |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1624 | usb_audio_warn(mixer->chip, |
Michał Mirosław | 82c1cf0 | 2014-08-03 15:09:57 +0200 | [diff] [blame] | 1625 | "[%d] FU [%s] ch = %d, val = %d/%d/%d", |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 1626 | cval->head.id, kctl->id.name, cval->channels, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1627 | cval->min, cval->max, cval->res); |
Alexey Fisher | 80aceff | 2011-03-10 14:53:38 +0100 | [diff] [blame] | 1628 | } |
| 1629 | |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1630 | 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] | 1631 | cval->head.id, kctl->id.name, cval->channels, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1632 | cval->min, cval->max, cval->res); |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 1633 | snd_usb_mixer_add_control(&cval->head, kctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1636 | static void build_feature_ctl(struct mixer_build *state, void *raw_desc, |
| 1637 | unsigned int ctl_mask, int control, |
| 1638 | struct usb_audio_term *iterm, int unitid, |
| 1639 | int readonly_mask) |
| 1640 | { |
| 1641 | struct uac_feature_unit_descriptor *desc = raw_desc; |
| 1642 | int nameid = uac_feature_unit_iFeature(desc); |
| 1643 | |
| 1644 | __build_feature_ctl(state->mixer, state->map, ctl_mask, control, |
| 1645 | iterm, &state->oterm, unitid, nameid, readonly_mask); |
| 1646 | } |
| 1647 | |
| 1648 | static void build_feature_ctl_badd(struct usb_mixer_interface *mixer, |
| 1649 | unsigned int ctl_mask, int control, int unitid, |
| 1650 | const struct usbmix_name_map *badd_map) |
| 1651 | { |
| 1652 | __build_feature_ctl(mixer, badd_map, ctl_mask, control, |
| 1653 | NULL, NULL, unitid, 0, 0); |
| 1654 | } |
| 1655 | |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 1656 | static void get_connector_control_name(struct mixer_build *state, |
| 1657 | struct usb_audio_term *term, |
| 1658 | bool is_input, char *name, int name_size) |
| 1659 | { |
Ruslan Bilovol | eccfc1b | 2018-05-04 04:24:02 +0300 | [diff] [blame] | 1660 | int name_len = get_term_name(state->chip, term, name, name_size, 0); |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 1661 | |
| 1662 | if (name_len == 0) |
| 1663 | strlcpy(name, "Unknown", name_size); |
| 1664 | |
| 1665 | /* |
| 1666 | * sound/core/ctljack.c has a convention of naming jack controls |
| 1667 | * by ending in " Jack". Make it slightly more useful by |
| 1668 | * indicating Input or Output after the terminal name. |
| 1669 | */ |
| 1670 | if (is_input) |
| 1671 | strlcat(name, " - Input Jack", name_size); |
| 1672 | else |
| 1673 | strlcat(name, " - Output Jack", name_size); |
| 1674 | } |
| 1675 | |
| 1676 | /* Build a mixer control for a UAC connector control (jack-detect) */ |
| 1677 | static void build_connector_control(struct mixer_build *state, |
| 1678 | struct usb_audio_term *term, bool is_input) |
| 1679 | { |
| 1680 | struct snd_kcontrol *kctl; |
| 1681 | struct usb_mixer_elem_info *cval; |
| 1682 | |
| 1683 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
| 1684 | if (!cval) |
| 1685 | return; |
| 1686 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, term->id); |
Andrew Chant | 568fa7e | 2018-03-23 19:25:24 -0700 | [diff] [blame] | 1687 | /* |
Jorge Sanjuan | 1d38f5d | 2018-05-11 16:25:36 +0100 | [diff] [blame] | 1688 | * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the |
| 1689 | * number of channels connected. |
| 1690 | * |
| 1691 | * UAC3: The first byte specifies size of bitmap for the inserted controls. The |
| 1692 | * following byte(s) specifies which connectors are inserted. |
| 1693 | * |
| 1694 | * This boolean ctl will simply report if any channels are connected |
| 1695 | * or not. |
Andrew Chant | 568fa7e | 2018-03-23 19:25:24 -0700 | [diff] [blame] | 1696 | */ |
Jorge Sanjuan | 1d38f5d | 2018-05-11 16:25:36 +0100 | [diff] [blame] | 1697 | if (state->mixer->protocol == UAC_VERSION_2) |
| 1698 | cval->control = UAC2_TE_CONNECTOR; |
| 1699 | else /* UAC_VERSION_3 */ |
| 1700 | cval->control = UAC3_TE_INSERTION; |
| 1701 | |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 1702 | cval->val_type = USB_MIXER_BOOLEAN; |
| 1703 | cval->channels = 1; /* report true if any channel is connected */ |
| 1704 | cval->min = 0; |
| 1705 | cval->max = 1; |
Jorge Sanjuan | 1d38f5d | 2018-05-11 16:25:36 +0100 | [diff] [blame] | 1706 | kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval); |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 1707 | if (!kctl) { |
| 1708 | usb_audio_err(state->chip, "cannot malloc kcontrol\n"); |
| 1709 | kfree(cval); |
| 1710 | return; |
| 1711 | } |
| 1712 | get_connector_control_name(state, term, is_input, kctl->id.name, |
| 1713 | sizeof(kctl->id.name)); |
| 1714 | kctl->private_free = snd_usb_mixer_elem_free; |
| 1715 | snd_usb_mixer_add_control(&cval->head, kctl); |
| 1716 | } |
| 1717 | |
Daniel Mack | cddaafb | 2016-04-09 11:21:46 +0200 | [diff] [blame] | 1718 | static int parse_clock_source_unit(struct mixer_build *state, int unitid, |
| 1719 | void *_ftr) |
| 1720 | { |
| 1721 | struct uac_clock_source_descriptor *hdr = _ftr; |
| 1722 | struct usb_mixer_elem_info *cval; |
| 1723 | struct snd_kcontrol *kctl; |
| 1724 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
| 1725 | int ret; |
| 1726 | |
| 1727 | if (state->mixer->protocol != UAC_VERSION_2) |
| 1728 | return -EINVAL; |
| 1729 | |
| 1730 | if (hdr->bLength != sizeof(*hdr)) { |
| 1731 | usb_audio_dbg(state->chip, |
| 1732 | "Bogus clock source descriptor length of %d, ignoring.\n", |
| 1733 | hdr->bLength); |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | /* |
| 1738 | * The only property of this unit we are interested in is the |
| 1739 | * clock source validity. If that isn't readable, just bail out. |
| 1740 | */ |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 1741 | if (!uac_v2v3_control_is_readable(hdr->bmControls, |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1742 | UAC2_CS_CONTROL_CLOCK_VALID)) |
Daniel Mack | cddaafb | 2016-04-09 11:21:46 +0200 | [diff] [blame] | 1743 | return 0; |
| 1744 | |
| 1745 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
| 1746 | if (!cval) |
| 1747 | return -ENOMEM; |
| 1748 | |
| 1749 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID); |
| 1750 | |
| 1751 | cval->min = 0; |
| 1752 | cval->max = 1; |
| 1753 | cval->channels = 1; |
| 1754 | cval->val_type = USB_MIXER_BOOLEAN; |
| 1755 | cval->control = UAC2_CS_CONTROL_CLOCK_VALID; |
| 1756 | |
Andrew Chant | 568fa7e | 2018-03-23 19:25:24 -0700 | [diff] [blame] | 1757 | cval->master_readonly = 1; |
| 1758 | /* From UAC2 5.2.5.1.2 "Only the get request is supported." */ |
| 1759 | kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval); |
Daniel Mack | cddaafb | 2016-04-09 11:21:46 +0200 | [diff] [blame] | 1760 | |
| 1761 | if (!kctl) { |
| 1762 | kfree(cval); |
| 1763 | return -ENOMEM; |
| 1764 | } |
| 1765 | |
| 1766 | kctl->private_free = snd_usb_mixer_elem_free; |
Ruslan Bilovol | eccfc1b | 2018-05-04 04:24:02 +0300 | [diff] [blame] | 1767 | ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource, |
Daniel Mack | cddaafb | 2016-04-09 11:21:46 +0200 | [diff] [blame] | 1768 | name, sizeof(name)); |
| 1769 | if (ret > 0) |
| 1770 | snprintf(kctl->id.name, sizeof(kctl->id.name), |
| 1771 | "%s Validity", name); |
| 1772 | else |
| 1773 | snprintf(kctl->id.name, sizeof(kctl->id.name), |
| 1774 | "Clock Source %d Validity", hdr->bClockID); |
| 1775 | |
| 1776 | return snd_usb_mixer_add_control(&cval->head, kctl); |
| 1777 | } |
| 1778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | /* |
| 1780 | * parse a feature unit |
| 1781 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1782 | * most of controls are defined here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1784 | static int parse_audio_feature_unit(struct mixer_build *state, int unitid, |
| 1785 | void *_ftr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | { |
| 1787 | int channels, i, j; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1788 | struct usb_audio_term iterm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | unsigned int master_bits, first_ch_bits; |
| 1790 | int err, csize; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1791 | struct uac_feature_unit_descriptor *hdr = _ftr; |
| 1792 | __u8 *bmaControls; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1794 | if (state->mixer->protocol == UAC_VERSION_1) { |
Takashi Iwai | d937cd6 | 2017-11-21 16:55:51 +0100 | [diff] [blame] | 1795 | if (hdr->bLength < 7) { |
| 1796 | usb_audio_err(state->chip, |
| 1797 | "unit %u: invalid UAC_FEATURE_UNIT descriptor\n", |
| 1798 | unitid); |
| 1799 | return -EINVAL; |
| 1800 | } |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1801 | csize = hdr->bControlSize; |
Takashi Iwai | 3c02a6d | 2017-11-27 10:59:40 +0100 | [diff] [blame] | 1802 | if (!csize) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1803 | usb_audio_dbg(state->chip, |
Takashi Iwai | 3c02a6d | 2017-11-27 10:59:40 +0100 | [diff] [blame] | 1804 | "unit %u: invalid bControlSize == 0\n", |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1805 | unitid); |
Nicolai Krakowiak | 60c961a9 | 2011-08-04 15:56:27 +0200 | [diff] [blame] | 1806 | return -EINVAL; |
| 1807 | } |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1808 | channels = (hdr->bLength - 7) / csize - 1; |
| 1809 | bmaControls = hdr->bmaControls; |
Clemens Ladisch | d56268f | 2012-11-29 17:04:23 +0100 | [diff] [blame] | 1810 | if (hdr->bLength < 7 + csize) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1811 | usb_audio_err(state->chip, |
| 1812 | "unit %u: invalid UAC_FEATURE_UNIT descriptor\n", |
| 1813 | unitid); |
Clemens Ladisch | d56268f | 2012-11-29 17:04:23 +0100 | [diff] [blame] | 1814 | return -EINVAL; |
| 1815 | } |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 1816 | } else if (state->mixer->protocol == UAC_VERSION_2) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1817 | struct uac2_feature_unit_descriptor *ftr = _ftr; |
Takashi Iwai | d937cd6 | 2017-11-21 16:55:51 +0100 | [diff] [blame] | 1818 | if (hdr->bLength < 6) { |
| 1819 | usb_audio_err(state->chip, |
| 1820 | "unit %u: invalid UAC_FEATURE_UNIT descriptor\n", |
| 1821 | unitid); |
| 1822 | return -EINVAL; |
| 1823 | } |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1824 | csize = 4; |
Daniel Mack | e8d0fee | 2010-05-27 20:15:14 +0200 | [diff] [blame] | 1825 | channels = (hdr->bLength - 6) / 4 - 1; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1826 | bmaControls = ftr->bmaControls; |
Clemens Ladisch | d56268f | 2012-11-29 17:04:23 +0100 | [diff] [blame] | 1827 | if (hdr->bLength < 6 + csize) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1828 | usb_audio_err(state->chip, |
| 1829 | "unit %u: invalid UAC_FEATURE_UNIT descriptor\n", |
| 1830 | unitid); |
Clemens Ladisch | d56268f | 2012-11-29 17:04:23 +0100 | [diff] [blame] | 1831 | return -EINVAL; |
| 1832 | } |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 1833 | } else { /* UAC_VERSION_3 */ |
| 1834 | struct uac3_feature_unit_descriptor *ftr = _ftr; |
| 1835 | |
| 1836 | if (hdr->bLength < 7) { |
| 1837 | usb_audio_err(state->chip, |
| 1838 | "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n", |
| 1839 | unitid); |
| 1840 | return -EINVAL; |
| 1841 | } |
| 1842 | csize = 4; |
| 1843 | channels = (ftr->bLength - 7) / 4 - 1; |
| 1844 | bmaControls = ftr->bmaControls; |
| 1845 | if (hdr->bLength < 7 + csize) { |
| 1846 | usb_audio_err(state->chip, |
| 1847 | "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n", |
| 1848 | unitid); |
| 1849 | return -EINVAL; |
| 1850 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | /* parse the source unit */ |
Takashi Iwai | f25ecf8 | 2018-05-27 15:18:22 +0200 | [diff] [blame] | 1854 | err = parse_audio_unit(state, hdr->bSourceID); |
| 1855 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | return err; |
| 1857 | |
| 1858 | /* determine the input source type and name */ |
Daniel Mack | 4d7b86c | 2013-03-19 21:09:24 +0100 | [diff] [blame] | 1859 | err = check_input_term(state, hdr->bSourceID, &iterm); |
| 1860 | if (err < 0) |
| 1861 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1863 | master_bits = snd_usb_combine_bytes(bmaControls, csize); |
Javier Kohen | 0c3cee5 | 2009-11-17 15:36:13 +0100 | [diff] [blame] | 1864 | /* master configuration quirks */ |
| 1865 | switch (state->chip->usb_id) { |
| 1866 | case USB_ID(0x08bb, 0x2702): |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1867 | usb_audio_info(state->chip, |
| 1868 | "usbmixer: master volume quirk for PCM2702 chip\n"); |
Javier Kohen | 0c3cee5 | 2009-11-17 15:36:13 +0100 | [diff] [blame] | 1869 | /* disable non-functional volume control */ |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 1870 | master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME); |
Javier Kohen | 0c3cee5 | 2009-11-17 15:36:13 +0100 | [diff] [blame] | 1871 | break; |
David Henningsson | c105143 | 2012-09-20 10:20:41 +0200 | [diff] [blame] | 1872 | case USB_ID(0x1130, 0xf211): |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1873 | usb_audio_info(state->chip, |
| 1874 | "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n"); |
David Henningsson | c105143 | 2012-09-20 10:20:41 +0200 | [diff] [blame] | 1875 | /* disable non-functional volume control */ |
| 1876 | channels = 0; |
| 1877 | break; |
| 1878 | |
Javier Kohen | 0c3cee5 | 2009-11-17 15:36:13 +0100 | [diff] [blame] | 1879 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | if (channels > 0) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1881 | first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | else |
| 1883 | first_ch_bits = 0; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1884 | |
| 1885 | if (state->mixer->protocol == UAC_VERSION_1) { |
| 1886 | /* check all control types */ |
| 1887 | for (i = 0; i < 10; i++) { |
| 1888 | unsigned int ch_bits = 0; |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1889 | int control = audio_feature_info[i].control; |
| 1890 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1891 | for (j = 0; j < channels; j++) { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1892 | unsigned int mask; |
| 1893 | |
| 1894 | mask = snd_usb_combine_bytes(bmaControls + |
| 1895 | csize * (j+1), csize); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1896 | if (mask & (1 << i)) |
| 1897 | ch_bits |= (1 << j); |
| 1898 | } |
| 1899 | /* audio class v1 controls are never read-only */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1900 | |
| 1901 | /* |
| 1902 | * The first channel must be set |
| 1903 | * (for ease of programming). |
| 1904 | */ |
| 1905 | if (ch_bits & 1) |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1906 | build_feature_ctl(state, _ftr, ch_bits, control, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1907 | &iterm, unitid, 0); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1908 | if (master_bits & (1 << i)) |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1909 | build_feature_ctl(state, _ftr, 0, control, |
| 1910 | &iterm, unitid, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | } |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 1912 | } else { /* UAC_VERSION_2/3 */ |
Takashi Iwai | d09c06c | 2011-10-13 08:19:09 +0200 | [diff] [blame] | 1913 | for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1914 | unsigned int ch_bits = 0; |
| 1915 | unsigned int ch_read_only = 0; |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1916 | int control = audio_feature_info[i].control; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1917 | |
| 1918 | for (j = 0; j < channels; j++) { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1919 | unsigned int mask; |
| 1920 | |
| 1921 | mask = snd_usb_combine_bytes(bmaControls + |
| 1922 | csize * (j+1), csize); |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1923 | if (uac_v2v3_control_is_readable(mask, control)) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1924 | ch_bits |= (1 << j); |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1925 | if (!uac_v2v3_control_is_writeable(mask, control)) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1926 | ch_read_only |= (1 << j); |
| 1927 | } |
| 1928 | } |
| 1929 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1930 | /* |
| 1931 | * NOTE: build_feature_ctl() will mark the control |
| 1932 | * read-only if all channels are marked read-only in |
| 1933 | * the descriptors. Otherwise, the control will be |
| 1934 | * reported as writeable, but the driver will not |
| 1935 | * actually issue a write command for read-only |
| 1936 | * channels. |
| 1937 | */ |
| 1938 | |
| 1939 | /* |
| 1940 | * The first channel must be set |
| 1941 | * (for ease of programming). |
| 1942 | */ |
| 1943 | if (ch_bits & 1) |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1944 | build_feature_ctl(state, _ftr, ch_bits, control, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1945 | &iterm, unitid, ch_read_only); |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1946 | if (uac_v2v3_control_is_readable(master_bits, control)) |
Takashi Iwai | 2de841e | 2018-04-23 08:59:36 +0200 | [diff] [blame] | 1947 | build_feature_ctl(state, _ftr, 0, control, |
| 1948 | &iterm, unitid, |
Andrew Chant | 21e9b3e | 2018-03-22 14:39:55 -0700 | [diff] [blame] | 1949 | !uac_v2v3_control_is_writeable(master_bits, |
| 1950 | control)); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | /* |
| 1958 | * Mixer Unit |
| 1959 | */ |
| 1960 | |
| 1961 | /* |
| 1962 | * build a mixer unit control |
| 1963 | * |
| 1964 | * the callbacks are identical with feature unit. |
| 1965 | * input channel number (zero based) is given in control field instead. |
| 1966 | */ |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1967 | static void build_mixer_unit_ctl(struct mixer_build *state, |
| 1968 | struct uac_mixer_unit_descriptor *desc, |
Jorge Sanjuan | 6cfd839 | 2018-05-11 16:25:34 +0100 | [diff] [blame] | 1969 | int in_pin, int in_ch, int num_outs, |
| 1970 | int unitid, struct usb_audio_term *iterm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1972 | struct usb_mixer_elem_info *cval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | unsigned int i, len; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1974 | struct snd_kcontrol *kctl; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1975 | const struct usbmix_name_map *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 1977 | map = find_map(state->map, unitid, 0); |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1978 | if (check_ignored_ctl(map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | return; |
| 1980 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 1981 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1982 | if (!cval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | return; |
| 1984 | |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 1985 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | cval->control = in_ch + 1; /* based on 1 */ |
| 1987 | cval->val_type = USB_MIXER_S16; |
| 1988 | for (i = 0; i < num_outs; i++) { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1989 | __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol); |
| 1990 | |
| 1991 | if (check_matrix_bitmap(c, in_ch, i, num_outs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | cval->cmask |= (1 << i); |
| 1993 | cval->channels++; |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | /* get min/max values */ |
| 1998 | get_min_max(cval, 0); |
| 1999 | |
| 2000 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2001 | if (!kctl) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2002 | usb_audio_err(state->chip, "cannot malloc kcontrol\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | kfree(cval); |
| 2004 | return; |
| 2005 | } |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 2006 | kctl->private_free = snd_usb_mixer_elem_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2008 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2009 | if (!len) |
Ruslan Bilovol | eccfc1b | 2018-05-04 04:24:02 +0300 | [diff] [blame] | 2010 | len = get_term_name(state->chip, iterm, kctl->id.name, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2011 | sizeof(kctl->id.name), 0); |
| 2012 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1); |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 2014 | append_ctl_name(kctl, " Volume"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2016 | 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] | 2017 | cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max); |
| 2018 | snd_usb_mixer_add_control(&cval->head, kctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 2021 | static int parse_audio_input_terminal(struct mixer_build *state, int unitid, |
| 2022 | void *raw_desc) |
| 2023 | { |
| 2024 | struct usb_audio_term iterm; |
Jorge Sanjuan | 1d38f5d | 2018-05-11 16:25:36 +0100 | [diff] [blame] | 2025 | unsigned int control, bmctls, term_id; |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 2026 | |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 2027 | if (state->mixer->protocol == UAC_VERSION_2) { |
Jorge Sanjuan | 1d38f5d | 2018-05-11 16:25:36 +0100 | [diff] [blame] | 2028 | struct uac2_input_terminal_descriptor *d_v2 = raw_desc; |
| 2029 | control = UAC2_TE_CONNECTOR; |
| 2030 | term_id = d_v2->bTerminalID; |
| 2031 | bmctls = le16_to_cpu(d_v2->bmControls); |
| 2032 | } else if (state->mixer->protocol == UAC_VERSION_3) { |
| 2033 | struct uac3_input_terminal_descriptor *d_v3 = raw_desc; |
| 2034 | control = UAC3_TE_INSERTION; |
| 2035 | term_id = d_v3->bTerminalID; |
| 2036 | bmctls = le32_to_cpu(d_v3->bmControls); |
| 2037 | } else { |
| 2038 | return 0; /* UAC1. No Insertion control */ |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 2039 | } |
Jorge Sanjuan | 1d38f5d | 2018-05-11 16:25:36 +0100 | [diff] [blame] | 2040 | |
| 2041 | check_input_term(state, term_id, &iterm); |
| 2042 | |
| 2043 | /* Check for jack detection. */ |
| 2044 | if (uac_v2v3_control_is_readable(bmctls, control)) |
| 2045 | build_connector_control(state, &iterm, true); |
| 2046 | |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 2047 | return 0; |
| 2048 | } |
| 2049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | /* |
| 2051 | * parse a mixer unit |
| 2052 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2053 | static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, |
| 2054 | void *raw_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2056 | struct uac_mixer_unit_descriptor *desc = raw_desc; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2057 | struct usb_audio_term iterm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | int input_pins, num_ins, num_outs; |
| 2059 | int pin, ich, err; |
| 2060 | |
Jorge Sanjuan | 6cfd839 | 2018-05-11 16:25:34 +0100 | [diff] [blame] | 2061 | err = uac_mixer_unit_get_channels(state, desc); |
| 2062 | if (err < 0) { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2063 | usb_audio_err(state->chip, |
| 2064 | "invalid MIXER UNIT descriptor %d\n", |
| 2065 | unitid); |
Jorge Sanjuan | 6cfd839 | 2018-05-11 16:25:34 +0100 | [diff] [blame] | 2066 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | |
Jorge Sanjuan | 6cfd839 | 2018-05-11 16:25:34 +0100 | [diff] [blame] | 2069 | num_outs = err; |
| 2070 | input_pins = desc->bNrInPins; |
| 2071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | num_ins = 0; |
| 2073 | ich = 0; |
| 2074 | for (pin = 0; pin < input_pins; pin++) { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2075 | err = parse_audio_unit(state, desc->baSourceID[pin]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | if (err < 0) |
Mark Hills | 284a8dd | 2012-04-14 17:19:23 +0100 | [diff] [blame] | 2077 | continue; |
Clemens Ladisch | ea114fc | 2015-06-03 11:36:51 +0200 | [diff] [blame] | 2078 | /* no bmControls field (e.g. Maya44) -> ignore */ |
| 2079 | if (desc->bLength <= 10 + input_pins) |
| 2080 | continue; |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2081 | err = check_input_term(state, desc->baSourceID[pin], &iterm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | if (err < 0) |
| 2083 | return err; |
| 2084 | num_ins += iterm.channels; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2085 | for (; ich < num_ins; ich++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | int och, ich_has_controls = 0; |
| 2087 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2088 | for (och = 0; och < num_outs; och++) { |
| 2089 | __u8 *c = uac_mixer_unit_bmControls(desc, |
| 2090 | state->mixer->protocol); |
| 2091 | |
| 2092 | if (check_matrix_bitmap(c, ich, och, num_outs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | ich_has_controls = 1; |
| 2094 | break; |
| 2095 | } |
| 2096 | } |
| 2097 | if (ich_has_controls) |
Jorge Sanjuan | 6cfd839 | 2018-05-11 16:25:34 +0100 | [diff] [blame] | 2098 | build_mixer_unit_ctl(state, desc, pin, ich, num_outs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | unitid, &iterm); |
| 2100 | } |
| 2101 | } |
| 2102 | return 0; |
| 2103 | } |
| 2104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | /* |
| 2106 | * Processing Unit / Extension Unit |
| 2107 | */ |
| 2108 | |
| 2109 | /* get callback for processing/extension unit */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2110 | static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, |
| 2111 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2113 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | int err, val; |
| 2115 | |
| 2116 | err = get_cur_ctl_value(cval, cval->control << 8, &val); |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 2117 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | ucontrol->value.integer.value[0] = cval->min; |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 2119 | return filter_error(cval, 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 | val = get_relative_value(cval, val); |
| 2122 | ucontrol->value.integer.value[0] = val; |
| 2123 | return 0; |
| 2124 | } |
| 2125 | |
| 2126 | /* put callback for processing/extension unit */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2127 | static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, |
| 2128 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2130 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | int val, oval, err; |
| 2132 | |
| 2133 | err = get_cur_ctl_value(cval, cval->control << 8, &oval); |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 2134 | if (err < 0) |
| 2135 | return filter_error(cval, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | val = ucontrol->value.integer.value[0]; |
| 2137 | val = get_abs_value(cval, val); |
| 2138 | if (val != oval) { |
| 2139 | set_cur_ctl_value(cval, cval->control << 8, val); |
| 2140 | return 1; |
| 2141 | } |
| 2142 | return 0; |
| 2143 | } |
| 2144 | |
| 2145 | /* alsa control interface for processing/extension unit */ |
Bhumika Goyal | 8fdaebb | 2017-04-12 18:38:06 +0530 | [diff] [blame] | 2146 | static const struct snd_kcontrol_new mixer_procunit_ctl = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2148 | .name = "", /* will be filled later */ |
| 2149 | .info = mixer_ctl_feature_info, |
| 2150 | .get = mixer_ctl_procunit_get, |
| 2151 | .put = mixer_ctl_procunit_put, |
| 2152 | }; |
| 2153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | /* |
| 2155 | * predefined data for processing units |
| 2156 | */ |
| 2157 | struct procunit_value_info { |
| 2158 | int control; |
| 2159 | char *suffix; |
| 2160 | int val_type; |
| 2161 | int min_value; |
| 2162 | }; |
| 2163 | |
| 2164 | struct procunit_info { |
| 2165 | int type; |
| 2166 | char *name; |
| 2167 | struct procunit_value_info *values; |
| 2168 | }; |
| 2169 | |
| 2170 | static struct procunit_value_info updown_proc_info[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2171 | { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
| 2172 | { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | { 0 } |
| 2174 | }; |
| 2175 | static struct procunit_value_info prologic_proc_info[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2176 | { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
| 2177 | { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | { 0 } |
| 2179 | }; |
| 2180 | static struct procunit_value_info threed_enh_proc_info[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2181 | { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
| 2182 | { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | { 0 } |
| 2184 | }; |
| 2185 | static struct procunit_value_info reverb_proc_info[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2186 | { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
| 2187 | { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 }, |
| 2188 | { UAC_REVERB_TIME, "Time", USB_MIXER_U16 }, |
| 2189 | { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | { 0 } |
| 2191 | }; |
| 2192 | static struct procunit_value_info chorus_proc_info[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2193 | { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
| 2194 | { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 }, |
| 2195 | { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 }, |
| 2196 | { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | { 0 } |
| 2198 | }; |
| 2199 | static struct procunit_value_info dcr_proc_info[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2200 | { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
| 2201 | { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 }, |
| 2202 | { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 }, |
| 2203 | { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 }, |
| 2204 | { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 }, |
| 2205 | { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | { 0 } |
| 2207 | }; |
| 2208 | |
| 2209 | static struct procunit_info procunits[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2210 | { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info }, |
| 2211 | { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info }, |
| 2212 | { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info }, |
| 2213 | { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info }, |
| 2214 | { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info }, |
| 2215 | { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | { 0 }, |
| 2217 | }; |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame] | 2218 | /* |
| 2219 | * predefined data for extension units |
| 2220 | */ |
| 2221 | static struct procunit_value_info clock_rate_xu_info[] = { |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2222 | { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 }, |
| 2223 | { 0 } |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame] | 2224 | }; |
| 2225 | static struct procunit_value_info clock_source_xu_info[] = { |
| 2226 | { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN }, |
| 2227 | { 0 } |
| 2228 | }; |
| 2229 | static struct procunit_value_info spdif_format_xu_info[] = { |
| 2230 | { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN }, |
| 2231 | { 0 } |
| 2232 | }; |
| 2233 | static struct procunit_value_info soft_limit_xu_info[] = { |
| 2234 | { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN }, |
| 2235 | { 0 } |
| 2236 | }; |
| 2237 | static struct procunit_info extunits[] = { |
| 2238 | { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info }, |
| 2239 | { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info }, |
| 2240 | { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info }, |
| 2241 | { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info }, |
| 2242 | { 0 } |
| 2243 | }; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | /* |
| 2246 | * build a processing/extension unit |
| 2247 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2248 | static int build_audio_procunit(struct mixer_build *state, int unitid, |
| 2249 | void *raw_desc, struct procunit_info *list, |
| 2250 | char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2252 | struct uac_processing_unit_descriptor *desc = raw_desc; |
| 2253 | int num_ins = desc->bNrInPins; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2254 | struct usb_mixer_elem_info *cval; |
| 2255 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | int i, err, nameid, type, len; |
| 2257 | struct procunit_info *info; |
| 2258 | struct procunit_value_info *valinfo; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2259 | const struct usbmix_name_map *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | static struct procunit_value_info default_value_info[] = { |
| 2261 | { 0x01, "Switch", USB_MIXER_BOOLEAN }, |
| 2262 | { 0 } |
| 2263 | }; |
| 2264 | static struct procunit_info default_info = { |
| 2265 | 0, NULL, default_value_info |
| 2266 | }; |
| 2267 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2268 | if (desc->bLength < 13 || desc->bLength < 13 + num_ins || |
| 2269 | desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2270 | 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] | 2271 | return -EINVAL; |
| 2272 | } |
| 2273 | |
| 2274 | for (i = 0; i < num_ins; i++) { |
Takashi Iwai | f25ecf8 | 2018-05-27 15:18:22 +0200 | [diff] [blame] | 2275 | err = parse_audio_unit(state, desc->baSourceID[i]); |
| 2276 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | return err; |
| 2278 | } |
| 2279 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2280 | type = le16_to_cpu(desc->wProcessType); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | for (info = list; info && info->type; info++) |
| 2282 | if (info->type == type) |
| 2283 | break; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2284 | if (!info || !info->type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | info = &default_info; |
| 2286 | |
| 2287 | for (valinfo = info->values; valinfo->control; valinfo++) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2288 | __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol); |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2289 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2290 | if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | continue; |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 2292 | map = find_map(state->map, unitid, valinfo->control); |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2293 | if (check_ignored_ctl(map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | continue; |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 2295 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
Daniel Mack | a860d95 | 2014-05-24 10:58:17 +0200 | [diff] [blame] | 2296 | if (!cval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | return -ENOMEM; |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2298 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | cval->control = valinfo->control; |
| 2300 | cval->val_type = valinfo->val_type; |
| 2301 | cval->channels = 1; |
| 2302 | |
| 2303 | /* get min/max values */ |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2304 | if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2305 | __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | /* FIXME: hard-coded */ |
| 2307 | cval->min = 1; |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2308 | cval->max = control_spec[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | cval->res = 1; |
| 2310 | cval->initialized = 1; |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame] | 2311 | } else { |
| 2312 | if (type == USB_XU_CLOCK_RATE) { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2313 | /* |
| 2314 | * E-Mu USB 0404/0202/TrackerPre/0204 |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame] | 2315 | * samplerate control quirk |
| 2316 | */ |
| 2317 | cval->min = 0; |
| 2318 | cval->max = 5; |
| 2319 | cval->res = 1; |
| 2320 | cval->initialized = 1; |
| 2321 | } else |
| 2322 | get_min_max(cval, valinfo->min_value); |
| 2323 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | |
| 2325 | kctl = snd_ctl_new1(&mixer_procunit_ctl, cval); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2326 | if (!kctl) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | kfree(cval); |
| 2328 | return -ENOMEM; |
| 2329 | } |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 2330 | kctl->private_free = snd_usb_mixer_elem_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2332 | if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) { |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2333 | /* nothing */ ; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2334 | } else if (info->name) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name)); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2336 | } else { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2337 | nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | len = 0; |
| 2339 | if (nameid) |
Ruslan Bilovol | eccfc1b | 2018-05-04 04:24:02 +0300 | [diff] [blame] | 2340 | len = snd_usb_copy_string_desc(state->chip, |
| 2341 | nameid, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2342 | kctl->id.name, |
| 2343 | sizeof(kctl->id.name)); |
| 2344 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | strlcpy(kctl->id.name, name, sizeof(kctl->id.name)); |
| 2346 | } |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 2347 | append_ctl_name(kctl, " "); |
| 2348 | append_ctl_name(kctl, valinfo->suffix); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2350 | usb_audio_dbg(state->chip, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2351 | "[%d] PU [%s] ch = %d, val = %d/%d\n", |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2352 | cval->head.id, kctl->id.name, cval->channels, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2353 | cval->min, cval->max); |
| 2354 | |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2355 | err = snd_usb_mixer_add_control(&cval->head, kctl); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2356 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | return err; |
| 2358 | } |
| 2359 | return 0; |
| 2360 | } |
| 2361 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2362 | static int parse_audio_processing_unit(struct mixer_build *state, int unitid, |
| 2363 | void *raw_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2365 | return build_audio_procunit(state, unitid, raw_desc, |
| 2366 | procunits, "Processing Unit"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | } |
| 2368 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2369 | static int parse_audio_extension_unit(struct mixer_build *state, int unitid, |
| 2370 | void *raw_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2372 | /* |
| 2373 | * Note that we parse extension units with processing unit descriptors. |
| 2374 | * That's ok as the layout is the same. |
| 2375 | */ |
| 2376 | return build_audio_procunit(state, unitid, raw_desc, |
| 2377 | extunits, "Extension Unit"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | } |
| 2379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | /* |
| 2381 | * Selector Unit |
| 2382 | */ |
| 2383 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2384 | /* |
| 2385 | * info callback for selector unit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | * use an enumerator type for routing |
| 2387 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2388 | static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, |
| 2389 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2391 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Clemens Ladisch | 2a1803a | 2011-01-10 16:30:13 +0100 | [diff] [blame] | 2392 | const char **itemlist = (const char **)kcontrol->private_value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 2394 | if (snd_BUG_ON(!itemlist)) |
| 2395 | return -EINVAL; |
Clemens Ladisch | 2a1803a | 2011-01-10 16:30:13 +0100 | [diff] [blame] | 2396 | return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | /* get callback for selector unit */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2400 | static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, |
| 2401 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2403 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | int val, err; |
| 2405 | |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2406 | err = get_cur_ctl_value(cval, cval->control << 8, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | if (err < 0) { |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 2408 | ucontrol->value.enumerated.item[0] = 0; |
| 2409 | return filter_error(cval, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | } |
| 2411 | val = get_relative_value(cval, val); |
| 2412 | ucontrol->value.enumerated.item[0] = val; |
| 2413 | return 0; |
| 2414 | } |
| 2415 | |
| 2416 | /* put callback for selector unit */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2417 | static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, |
| 2418 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2420 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | int val, oval, err; |
| 2422 | |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2423 | err = get_cur_ctl_value(cval, cval->control << 8, &oval); |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 2424 | if (err < 0) |
| 2425 | return filter_error(cval, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | val = ucontrol->value.enumerated.item[0]; |
| 2427 | val = get_abs_value(cval, val); |
| 2428 | if (val != oval) { |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2429 | set_cur_ctl_value(cval, cval->control << 8, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | return 1; |
| 2431 | } |
| 2432 | return 0; |
| 2433 | } |
| 2434 | |
| 2435 | /* alsa control interface for selector unit */ |
Bhumika Goyal | 8fdaebb | 2017-04-12 18:38:06 +0530 | [diff] [blame] | 2436 | static const struct snd_kcontrol_new mixer_selectunit_ctl = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2438 | .name = "", /* will be filled later */ |
| 2439 | .info = mixer_ctl_selector_info, |
| 2440 | .get = mixer_ctl_selector_get, |
| 2441 | .put = mixer_ctl_selector_put, |
| 2442 | }; |
| 2443 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2444 | /* |
| 2445 | * private free callback. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | * free both private_data and private_value |
| 2447 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2448 | static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | { |
| 2450 | int i, num_ins = 0; |
| 2451 | |
| 2452 | if (kctl->private_data) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2453 | struct usb_mixer_elem_info *cval = kctl->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | num_ins = cval->max; |
| 2455 | kfree(cval); |
| 2456 | kctl->private_data = NULL; |
| 2457 | } |
| 2458 | if (kctl->private_value) { |
| 2459 | char **itemlist = (char **)kctl->private_value; |
| 2460 | for (i = 0; i < num_ins; i++) |
| 2461 | kfree(itemlist[i]); |
| 2462 | kfree(itemlist); |
| 2463 | kctl->private_value = 0; |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | /* |
| 2468 | * parse a selector unit |
| 2469 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2470 | static int parse_audio_selector_unit(struct mixer_build *state, int unitid, |
| 2471 | void *raw_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2473 | struct uac_selector_unit_descriptor *desc = raw_desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | unsigned int i, nameid, len; |
| 2475 | int err; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2476 | struct usb_mixer_elem_info *cval; |
| 2477 | struct snd_kcontrol *kctl; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2478 | const struct usbmix_name_map *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | char **namelist; |
| 2480 | |
Takashi Iwai | f658f17 | 2017-11-21 17:00:32 +0100 | [diff] [blame] | 2481 | if (desc->bLength < 5 || !desc->bNrInPins || |
| 2482 | desc->bLength < 5 + desc->bNrInPins) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2483 | usb_audio_err(state->chip, |
| 2484 | "invalid SELECTOR UNIT descriptor %d\n", unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | return -EINVAL; |
| 2486 | } |
| 2487 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2488 | for (i = 0; i < desc->bNrInPins; i++) { |
Takashi Iwai | f25ecf8 | 2018-05-27 15:18:22 +0200 | [diff] [blame] | 2489 | err = parse_audio_unit(state, desc->baSourceID[i]); |
| 2490 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | return err; |
| 2492 | } |
| 2493 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2494 | if (desc->bNrInPins == 1) /* only one ? nonsense! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | return 0; |
| 2496 | |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 2497 | map = find_map(state->map, unitid, 0); |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2498 | if (check_ignored_ctl(map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | return 0; |
| 2500 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 2501 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
Daniel Mack | a860d95 | 2014-05-24 10:58:17 +0200 | [diff] [blame] | 2502 | if (!cval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | return -ENOMEM; |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2504 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | cval->val_type = USB_MIXER_U8; |
| 2506 | cval->channels = 1; |
| 2507 | cval->min = 1; |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2508 | cval->max = desc->bNrInPins; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | cval->res = 1; |
| 2510 | cval->initialized = 1; |
| 2511 | |
Johan Rastén | a358a0e | 2014-11-17 08:39:33 +0100 | [diff] [blame] | 2512 | if (state->mixer->protocol == UAC_VERSION_1) |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2513 | cval->control = 0; |
Johan Rastén | a358a0e | 2014-11-17 08:39:33 +0100 | [diff] [blame] | 2514 | else /* UAC_VERSION_2 */ |
| 2515 | cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ? |
| 2516 | UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR; |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2517 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame^] | 2518 | namelist = kmalloc_array(desc->bNrInPins, sizeof(char *), GFP_KERNEL); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2519 | if (!namelist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | kfree(cval); |
| 2521 | return -ENOMEM; |
| 2522 | } |
| 2523 | #define MAX_ITEM_NAME_LEN 64 |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2524 | for (i = 0; i < desc->bNrInPins; i++) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2525 | struct usb_audio_term iterm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | len = 0; |
| 2527 | namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2528 | if (!namelist[i]) { |
Mariusz Kozlowski | 7fbe3ca | 2007-01-08 11:25:30 +0100 | [diff] [blame] | 2529 | while (i--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | kfree(namelist[i]); |
| 2531 | kfree(namelist); |
| 2532 | kfree(cval); |
| 2533 | return -ENOMEM; |
| 2534 | } |
Clemens Ladisch | 8e062ec | 2005-04-22 15:49:52 +0200 | [diff] [blame] | 2535 | len = check_mapped_selector_name(state, unitid, i, namelist[i], |
| 2536 | MAX_ITEM_NAME_LEN); |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2537 | if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0) |
Ruslan Bilovol | eccfc1b | 2018-05-04 04:24:02 +0300 | [diff] [blame] | 2538 | len = get_term_name(state->chip, &iterm, namelist[i], |
| 2539 | MAX_ITEM_NAME_LEN, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | if (! len) |
Masanari Iida | af831ee | 2014-04-28 13:08:55 +0900 | [diff] [blame] | 2541 | sprintf(namelist[i], "Input %u", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval); |
| 2545 | if (! kctl) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2546 | usb_audio_err(state->chip, "cannot malloc kcontrol\n"); |
Jesper Juhl | 878b478 | 2006-03-20 11:27:13 +0100 | [diff] [blame] | 2547 | kfree(namelist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | kfree(cval); |
| 2549 | return -ENOMEM; |
| 2550 | } |
| 2551 | kctl->private_value = (unsigned long)namelist; |
| 2552 | kctl->private_free = usb_mixer_selector_elem_free; |
| 2553 | |
Takashi Iwai | 5a15f28 | 2017-12-18 23:36:57 +0100 | [diff] [blame] | 2554 | /* check the static mapping table at first */ |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2555 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
Jaejoong Kim | 89b89d1 | 2017-12-04 15:31:49 +0900 | [diff] [blame] | 2556 | if (!len) { |
Takashi Iwai | 5a15f28 | 2017-12-18 23:36:57 +0100 | [diff] [blame] | 2557 | /* no mapping ? */ |
| 2558 | /* if iSelector is given, use it */ |
| 2559 | nameid = uac_selector_unit_iSelector(desc); |
| 2560 | if (nameid) |
Ruslan Bilovol | eccfc1b | 2018-05-04 04:24:02 +0300 | [diff] [blame] | 2561 | len = snd_usb_copy_string_desc(state->chip, nameid, |
Takashi Iwai | 5a15f28 | 2017-12-18 23:36:57 +0100 | [diff] [blame] | 2562 | kctl->id.name, |
| 2563 | sizeof(kctl->id.name)); |
| 2564 | /* ... or pick up the terminal name at next */ |
| 2565 | if (!len) |
Ruslan Bilovol | eccfc1b | 2018-05-04 04:24:02 +0300 | [diff] [blame] | 2566 | len = get_term_name(state->chip, &state->oterm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | kctl->id.name, sizeof(kctl->id.name), 0); |
Takashi Iwai | 5a15f28 | 2017-12-18 23:36:57 +0100 | [diff] [blame] | 2568 | /* ... or use the fixed string "USB" as the last resort */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2569 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name)); |
| 2571 | |
Takashi Iwai | 5a15f28 | 2017-12-18 23:36:57 +0100 | [diff] [blame] | 2572 | /* and add the proper suffix */ |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2573 | if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) |
| 2574 | append_ctl_name(kctl, " Clock Source"); |
| 2575 | else if ((state->oterm.type & 0xff00) == 0x0100) |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 2576 | append_ctl_name(kctl, " Capture Source"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | else |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 2578 | append_ctl_name(kctl, " Playback Source"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | } |
| 2580 | |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2581 | usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n", |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2582 | cval->head.id, kctl->id.name, desc->bNrInPins); |
| 2583 | return snd_usb_mixer_add_control(&cval->head, kctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | } |
| 2585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | /* |
| 2587 | * parse an audio unit recursively |
| 2588 | */ |
| 2589 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2590 | static int parse_audio_unit(struct mixer_build *state, int unitid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | { |
| 2592 | unsigned char *p1; |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 2593 | int protocol = state->mixer->protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | |
| 2595 | if (test_and_set_bit(unitid, state->unitbitmap)) |
| 2596 | return 0; /* the unit already visited */ |
| 2597 | |
| 2598 | p1 = find_audio_control_unit(state, unitid); |
| 2599 | if (!p1) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2600 | usb_audio_err(state->chip, "unit %d not found!\n", unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | return -EINVAL; |
| 2602 | } |
| 2603 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 2604 | if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) { |
| 2605 | switch (p1[2]) { |
| 2606 | case UAC_INPUT_TERMINAL: |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 2607 | return parse_audio_input_terminal(state, unitid, p1); |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 2608 | case UAC_MIXER_UNIT: |
| 2609 | return parse_audio_mixer_unit(state, unitid, p1); |
| 2610 | case UAC2_CLOCK_SOURCE: |
| 2611 | return parse_clock_source_unit(state, unitid, p1); |
| 2612 | case UAC_SELECTOR_UNIT: |
| 2613 | case UAC2_CLOCK_SELECTOR: |
| 2614 | return parse_audio_selector_unit(state, unitid, p1); |
| 2615 | case UAC_FEATURE_UNIT: |
| 2616 | return parse_audio_feature_unit(state, unitid, p1); |
| 2617 | case UAC1_PROCESSING_UNIT: |
| 2618 | /* UAC2_EFFECT_UNIT has the same value */ |
| 2619 | if (protocol == UAC_VERSION_1) |
| 2620 | return parse_audio_processing_unit(state, unitid, p1); |
| 2621 | else |
| 2622 | return 0; /* FIXME - effect units not implemented yet */ |
| 2623 | case UAC1_EXTENSION_UNIT: |
| 2624 | /* UAC2_PROCESSING_UNIT_V2 has the same value */ |
| 2625 | if (protocol == UAC_VERSION_1) |
| 2626 | return parse_audio_extension_unit(state, unitid, p1); |
| 2627 | else /* UAC_VERSION_2 */ |
| 2628 | return parse_audio_processing_unit(state, unitid, p1); |
| 2629 | case UAC2_EXTENSION_UNIT_V2: |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2630 | return parse_audio_extension_unit(state, unitid, p1); |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 2631 | default: |
| 2632 | usb_audio_err(state->chip, |
| 2633 | "unit %u: unexpected type 0x%02x\n", unitid, p1[2]); |
| 2634 | return -EINVAL; |
| 2635 | } |
| 2636 | } else { /* UAC_VERSION_3 */ |
| 2637 | switch (p1[2]) { |
| 2638 | case UAC_INPUT_TERMINAL: |
Jorge Sanjuan | 1d38f5d | 2018-05-11 16:25:36 +0100 | [diff] [blame] | 2639 | return parse_audio_input_terminal(state, unitid, p1); |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 2640 | case UAC3_MIXER_UNIT: |
| 2641 | return parse_audio_mixer_unit(state, unitid, p1); |
| 2642 | case UAC3_CLOCK_SOURCE: |
| 2643 | return parse_clock_source_unit(state, unitid, p1); |
| 2644 | case UAC3_CLOCK_SELECTOR: |
| 2645 | return parse_audio_selector_unit(state, unitid, p1); |
| 2646 | case UAC3_FEATURE_UNIT: |
| 2647 | return parse_audio_feature_unit(state, unitid, p1); |
| 2648 | case UAC3_EFFECT_UNIT: |
| 2649 | return 0; /* FIXME - effect units not implemented yet */ |
| 2650 | case UAC3_PROCESSING_UNIT: |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2651 | return parse_audio_processing_unit(state, unitid, p1); |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 2652 | case UAC3_EXTENSION_UNIT: |
| 2653 | return parse_audio_extension_unit(state, unitid, p1); |
| 2654 | default: |
| 2655 | usb_audio_err(state->chip, |
| 2656 | "unit %u: unexpected type 0x%02x\n", unitid, p1[2]); |
| 2657 | return -EINVAL; |
| 2658 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2662 | static void snd_usb_mixer_free(struct usb_mixer_interface *mixer) |
| 2663 | { |
Takashi Iwai | 124751d | 2017-10-10 14:10:32 +0200 | [diff] [blame] | 2664 | /* kill pending URBs */ |
| 2665 | snd_usb_mixer_disconnect(mixer); |
| 2666 | |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2667 | kfree(mixer->id_elems); |
| 2668 | if (mixer->urb) { |
| 2669 | kfree(mixer->urb->transfer_buffer); |
| 2670 | usb_free_urb(mixer->urb); |
| 2671 | } |
Mariusz Kozlowski | 68df9de | 2006-11-08 15:37:04 +0100 | [diff] [blame] | 2672 | usb_free_urb(mixer->rc_urb); |
Clemens Ladisch | b259b10 | 2005-04-29 16:29:28 +0200 | [diff] [blame] | 2673 | kfree(mixer->rc_setup_packet); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2674 | kfree(mixer); |
| 2675 | } |
| 2676 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2677 | static int snd_usb_mixer_dev_free(struct snd_device *device) |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2678 | { |
| 2679 | struct usb_mixer_interface *mixer = device->device_data; |
| 2680 | snd_usb_mixer_free(mixer); |
| 2681 | return 0; |
| 2682 | } |
| 2683 | |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 2684 | /* UAC3 predefined channels configuration */ |
| 2685 | struct uac3_badd_profile { |
| 2686 | int subclass; |
| 2687 | const char *name; |
| 2688 | int c_chmask; /* capture channels mask */ |
| 2689 | int p_chmask; /* playback channels mask */ |
| 2690 | int st_chmask; /* side tone mixing channel mask */ |
| 2691 | }; |
| 2692 | |
| 2693 | static struct uac3_badd_profile uac3_badd_profiles[] = { |
| 2694 | { |
| 2695 | /* |
| 2696 | * BAIF, BAOF or combination of both |
| 2697 | * IN: Mono or Stereo cfg, Mono alt possible |
| 2698 | * OUT: Mono or Stereo cfg, Mono alt possible |
| 2699 | */ |
| 2700 | .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO, |
| 2701 | .name = "GENERIC IO", |
| 2702 | .c_chmask = -1, /* dynamic channels */ |
| 2703 | .p_chmask = -1, /* dynamic channels */ |
| 2704 | }, |
| 2705 | { |
| 2706 | /* BAOF; Stereo only cfg, Mono alt possible */ |
| 2707 | .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE, |
| 2708 | .name = "HEADPHONE", |
| 2709 | .p_chmask = 3, |
| 2710 | }, |
| 2711 | { |
| 2712 | /* BAOF; Mono or Stereo cfg, Mono alt possible */ |
| 2713 | .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER, |
| 2714 | .name = "SPEAKER", |
| 2715 | .p_chmask = -1, /* dynamic channels */ |
| 2716 | }, |
| 2717 | { |
| 2718 | /* BAIF; Mono or Stereo cfg, Mono alt possible */ |
| 2719 | .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE, |
| 2720 | .name = "MICROPHONE", |
| 2721 | .c_chmask = -1, /* dynamic channels */ |
| 2722 | }, |
| 2723 | { |
| 2724 | /* |
| 2725 | * BAIOF topology |
| 2726 | * IN: Mono only |
| 2727 | * OUT: Mono or Stereo cfg, Mono alt possible |
| 2728 | */ |
| 2729 | .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET, |
| 2730 | .name = "HEADSET", |
| 2731 | .c_chmask = 1, |
| 2732 | .p_chmask = -1, /* dynamic channels */ |
| 2733 | .st_chmask = 1, |
| 2734 | }, |
| 2735 | { |
| 2736 | /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */ |
| 2737 | .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER, |
| 2738 | .name = "HEADSET ADAPTER", |
| 2739 | .c_chmask = 1, |
| 2740 | .p_chmask = 3, |
| 2741 | .st_chmask = 1, |
| 2742 | }, |
| 2743 | { |
| 2744 | /* BAIF + BAOF; IN: Mono only; OUT: Mono only */ |
| 2745 | .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE, |
| 2746 | .name = "SPEAKERPHONE", |
| 2747 | .c_chmask = 1, |
| 2748 | .p_chmask = 1, |
| 2749 | }, |
| 2750 | { 0 } /* terminator */ |
| 2751 | }; |
| 2752 | |
| 2753 | static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer, |
| 2754 | struct uac3_badd_profile *f, |
| 2755 | int c_chmask, int p_chmask) |
| 2756 | { |
| 2757 | /* |
| 2758 | * If both playback/capture channels are dynamic, make sure |
| 2759 | * at least one channel is present |
| 2760 | */ |
| 2761 | if (f->c_chmask < 0 && f->p_chmask < 0) { |
| 2762 | if (!c_chmask && !p_chmask) { |
| 2763 | usb_audio_warn(mixer->chip, "BAAD %s: no channels?", |
| 2764 | f->name); |
| 2765 | return false; |
| 2766 | } |
| 2767 | return true; |
| 2768 | } |
| 2769 | |
| 2770 | if ((f->c_chmask < 0 && !c_chmask) || |
| 2771 | (f->c_chmask >= 0 && f->c_chmask != c_chmask)) { |
| 2772 | usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch", |
| 2773 | f->name); |
| 2774 | return false; |
| 2775 | } |
| 2776 | if ((f->p_chmask < 0 && !p_chmask) || |
| 2777 | (f->p_chmask >= 0 && f->p_chmask != p_chmask)) { |
| 2778 | usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch", |
| 2779 | f->name); |
| 2780 | return false; |
| 2781 | } |
| 2782 | return true; |
| 2783 | } |
| 2784 | |
| 2785 | /* |
| 2786 | * create mixer controls for UAC3 BADD profiles |
| 2787 | * |
| 2788 | * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything |
| 2789 | * |
| 2790 | * BADD device may contain Mixer Unit, which doesn't have any controls, skip it |
| 2791 | */ |
| 2792 | static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer, |
| 2793 | int ctrlif) |
| 2794 | { |
| 2795 | struct usb_device *dev = mixer->chip->dev; |
| 2796 | struct usb_interface_assoc_descriptor *assoc; |
| 2797 | int badd_profile = mixer->chip->badd_profile; |
| 2798 | struct uac3_badd_profile *f; |
| 2799 | const struct usbmix_ctl_map *map; |
| 2800 | int p_chmask = 0, c_chmask = 0, st_chmask = 0; |
| 2801 | int i; |
| 2802 | |
| 2803 | assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc; |
| 2804 | |
| 2805 | /* Detect BADD capture/playback channels from AS EP descriptors */ |
| 2806 | for (i = 0; i < assoc->bInterfaceCount; i++) { |
| 2807 | int intf = assoc->bFirstInterface + i; |
| 2808 | |
| 2809 | struct usb_interface *iface; |
| 2810 | struct usb_host_interface *alts; |
| 2811 | struct usb_interface_descriptor *altsd; |
| 2812 | unsigned int maxpacksize; |
| 2813 | char dir_in; |
| 2814 | int chmask, num; |
| 2815 | |
| 2816 | if (intf == ctrlif) |
| 2817 | continue; |
| 2818 | |
| 2819 | iface = usb_ifnum_to_if(dev, intf); |
| 2820 | num = iface->num_altsetting; |
| 2821 | |
| 2822 | if (num < 2) |
| 2823 | return -EINVAL; |
| 2824 | |
| 2825 | /* |
| 2826 | * The number of Channels in an AudioStreaming interface |
| 2827 | * and the audio sample bit resolution (16 bits or 24 |
| 2828 | * bits) can be derived from the wMaxPacketSize field in |
| 2829 | * the Standard AS Audio Data Endpoint descriptor in |
| 2830 | * Alternate Setting 1 |
| 2831 | */ |
| 2832 | alts = &iface->altsetting[1]; |
| 2833 | altsd = get_iface_desc(alts); |
| 2834 | |
| 2835 | if (altsd->bNumEndpoints < 1) |
| 2836 | return -EINVAL; |
| 2837 | |
| 2838 | /* check direction */ |
| 2839 | dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN); |
| 2840 | maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); |
| 2841 | |
| 2842 | switch (maxpacksize) { |
| 2843 | default: |
| 2844 | usb_audio_err(mixer->chip, |
| 2845 | "incorrect wMaxPacketSize 0x%x for BADD profile\n", |
| 2846 | maxpacksize); |
| 2847 | return -EINVAL; |
| 2848 | case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16: |
| 2849 | case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16: |
| 2850 | case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24: |
| 2851 | case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24: |
| 2852 | chmask = 1; |
| 2853 | break; |
| 2854 | case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16: |
| 2855 | case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16: |
| 2856 | case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24: |
| 2857 | case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24: |
| 2858 | chmask = 3; |
| 2859 | break; |
| 2860 | } |
| 2861 | |
| 2862 | if (dir_in) |
| 2863 | c_chmask = chmask; |
| 2864 | else |
| 2865 | p_chmask = chmask; |
| 2866 | } |
| 2867 | |
| 2868 | usb_audio_dbg(mixer->chip, |
| 2869 | "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n", |
| 2870 | badd_profile, c_chmask, p_chmask); |
| 2871 | |
| 2872 | /* check the mapping table */ |
| 2873 | for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) { |
| 2874 | if (map->id == badd_profile) |
| 2875 | break; |
| 2876 | } |
| 2877 | |
| 2878 | if (!map->id) |
| 2879 | return -EINVAL; |
| 2880 | |
| 2881 | for (f = uac3_badd_profiles; f->name; f++) { |
| 2882 | if (badd_profile == f->subclass) |
| 2883 | break; |
| 2884 | } |
| 2885 | if (!f->name) |
| 2886 | return -EINVAL; |
| 2887 | if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask)) |
| 2888 | return -EINVAL; |
| 2889 | st_chmask = f->st_chmask; |
| 2890 | |
| 2891 | /* Playback */ |
| 2892 | if (p_chmask) { |
| 2893 | /* Master channel, always writable */ |
| 2894 | build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE, |
| 2895 | UAC3_BADD_FU_ID2, map->map); |
| 2896 | /* Mono/Stereo volume channels, always writable */ |
| 2897 | build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME, |
| 2898 | UAC3_BADD_FU_ID2, map->map); |
| 2899 | } |
| 2900 | |
| 2901 | /* Capture */ |
| 2902 | if (c_chmask) { |
| 2903 | /* Master channel, always writable */ |
| 2904 | build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE, |
| 2905 | UAC3_BADD_FU_ID5, map->map); |
| 2906 | /* Mono/Stereo volume channels, always writable */ |
| 2907 | build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME, |
| 2908 | UAC3_BADD_FU_ID5, map->map); |
| 2909 | } |
| 2910 | |
| 2911 | /* Side tone-mixing */ |
| 2912 | if (st_chmask) { |
| 2913 | /* Master channel, always writable */ |
| 2914 | build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE, |
| 2915 | UAC3_BADD_FU_ID7, map->map); |
| 2916 | /* Mono volume channel, always writable */ |
| 2917 | build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME, |
| 2918 | UAC3_BADD_FU_ID7, map->map); |
| 2919 | } |
| 2920 | |
| 2921 | return 0; |
| 2922 | } |
| 2923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | /* |
| 2925 | * create mixer controls |
| 2926 | * |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 2927 | * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | */ |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2929 | static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2931 | struct mixer_build state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | int err; |
| 2933 | const struct usbmix_ctl_map *map; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2934 | void *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | memset(&state, 0, sizeof(state)); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2937 | state.chip = mixer->chip; |
| 2938 | state.mixer = mixer; |
Daniel Mack | 1faa5d0 | 2011-08-04 15:56:28 +0200 | [diff] [blame] | 2939 | state.buffer = mixer->hostif->extra; |
| 2940 | state.buflen = mixer->hostif->extralen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2941 | |
| 2942 | /* check the mapping table */ |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2943 | for (map = usbmix_ctl_maps; map->id; map++) { |
| 2944 | if (map->id == state.chip->usb_id) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | state.map = map->map; |
Clemens Ladisch | 8e062ec | 2005-04-22 15:49:52 +0200 | [diff] [blame] | 2946 | state.selector_map = map->selector_map; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2947 | mixer->ignore_ctl_error = map->ignore_ctl_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | break; |
| 2949 | } |
| 2950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2952 | p = NULL; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2953 | while ((p = snd_usb_find_csint_desc(mixer->hostif->extra, |
| 2954 | mixer->hostif->extralen, |
Daniel Mack | 1faa5d0 | 2011-08-04 15:56:28 +0200 | [diff] [blame] | 2955 | p, UAC_OUTPUT_TERMINAL)) != NULL) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2956 | if (mixer->protocol == UAC_VERSION_1) { |
Daniel Mack | 69da9bc | 2010-06-16 17:57:28 +0200 | [diff] [blame] | 2957 | struct uac1_output_terminal_descriptor *desc = p; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2958 | |
| 2959 | if (desc->bLength < sizeof(*desc)) |
| 2960 | continue; /* invalid descriptor? */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2961 | /* mark terminal ID as visited */ |
| 2962 | set_bit(desc->bTerminalID, state.unitbitmap); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2963 | state.oterm.id = desc->bTerminalID; |
| 2964 | state.oterm.type = le16_to_cpu(desc->wTerminalType); |
| 2965 | state.oterm.name = desc->iTerminal; |
| 2966 | err = parse_audio_unit(&state, desc->bSourceID); |
Daniel Mack | 83ea5d1 | 2013-03-19 21:09:25 +0100 | [diff] [blame] | 2967 | if (err < 0 && err != -EINVAL) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2968 | return err; |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 2969 | } else if (mixer->protocol == UAC_VERSION_2) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2970 | struct uac2_output_terminal_descriptor *desc = p; |
| 2971 | |
| 2972 | if (desc->bLength < sizeof(*desc)) |
| 2973 | continue; /* invalid descriptor? */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2974 | /* mark terminal ID as visited */ |
| 2975 | set_bit(desc->bTerminalID, state.unitbitmap); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2976 | state.oterm.id = desc->bTerminalID; |
| 2977 | state.oterm.type = le16_to_cpu(desc->wTerminalType); |
| 2978 | state.oterm.name = desc->iTerminal; |
| 2979 | err = parse_audio_unit(&state, desc->bSourceID); |
Daniel Mack | 83ea5d1 | 2013-03-19 21:09:25 +0100 | [diff] [blame] | 2980 | if (err < 0 && err != -EINVAL) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2981 | return err; |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2982 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2983 | /* |
| 2984 | * For UAC2, use the same approach to also add the |
| 2985 | * clock selectors |
| 2986 | */ |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2987 | err = parse_audio_unit(&state, desc->bCSourceID); |
Daniel Mack | 83ea5d1 | 2013-03-19 21:09:25 +0100 | [diff] [blame] | 2988 | if (err < 0 && err != -EINVAL) |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2989 | return err; |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 2990 | |
Takashi Iwai | 2b54f785 | 2018-04-23 15:19:25 +0200 | [diff] [blame] | 2991 | if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls), |
Andrew Chant | 5a222e8 | 2018-03-23 19:25:23 -0700 | [diff] [blame] | 2992 | UAC2_TE_CONNECTOR)) { |
| 2993 | build_connector_control(&state, &state.oterm, |
| 2994 | false); |
| 2995 | } |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 2996 | } else { /* UAC_VERSION_3 */ |
| 2997 | struct uac3_output_terminal_descriptor *desc = p; |
| 2998 | |
| 2999 | if (desc->bLength < sizeof(*desc)) |
| 3000 | continue; /* invalid descriptor? */ |
| 3001 | /* mark terminal ID as visited */ |
| 3002 | set_bit(desc->bTerminalID, state.unitbitmap); |
| 3003 | state.oterm.id = desc->bTerminalID; |
| 3004 | state.oterm.type = le16_to_cpu(desc->wTerminalType); |
| 3005 | state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr); |
| 3006 | err = parse_audio_unit(&state, desc->bSourceID); |
| 3007 | if (err < 0 && err != -EINVAL) |
| 3008 | return err; |
| 3009 | |
| 3010 | /* |
| 3011 | * For UAC3, use the same approach to also add the |
| 3012 | * clock selectors |
| 3013 | */ |
| 3014 | err = parse_audio_unit(&state, desc->bCSourceID); |
| 3015 | if (err < 0 && err != -EINVAL) |
| 3016 | return err; |
Jorge Sanjuan | 1d38f5d | 2018-05-11 16:25:36 +0100 | [diff] [blame] | 3017 | |
| 3018 | if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls), |
| 3019 | UAC3_TE_INSERTION)) { |
| 3020 | build_connector_control(&state, &state.oterm, |
| 3021 | false); |
| 3022 | } |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 3023 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | } |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 3025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3026 | return 0; |
| 3027 | } |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3028 | |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 3029 | 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] | 3030 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3031 | struct usb_mixer_elem_list *list; |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3032 | |
Takashi Iwai | 8c55807 | 2018-05-03 12:33:32 +0200 | [diff] [blame] | 3033 | for_each_mixer_elem(list, mixer, unitid) { |
Julian Scheel | b2500b5 | 2017-11-16 17:35:17 +0100 | [diff] [blame] | 3034 | struct usb_mixer_elem_info *info = |
Takashi Iwai | 8c55807 | 2018-05-03 12:33:32 +0200 | [diff] [blame] | 3035 | mixer_elem_list_to_info(list); |
Julian Scheel | b2500b5 | 2017-11-16 17:35:17 +0100 | [diff] [blame] | 3036 | /* invalidate cache, so the value is read from the device */ |
| 3037 | info->cached = 0; |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3038 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3039 | &list->kctl->id); |
Julian Scheel | b2500b5 | 2017-11-16 17:35:17 +0100 | [diff] [blame] | 3040 | } |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3041 | } |
| 3042 | |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 3043 | static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer, |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3044 | struct usb_mixer_elem_list *list) |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 3045 | { |
Takashi Iwai | 8c55807 | 2018-05-03 12:33:32 +0200 | [diff] [blame] | 3046 | struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list); |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 3047 | static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN", |
| 3048 | "S8", "U8", "S16", "U16"}; |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 3049 | snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, " |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3050 | "channels=%i, type=\"%s\"\n", cval->head.id, |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 3051 | cval->control, cval->cmask, cval->channels, |
| 3052 | val_types[cval->val_type]); |
| 3053 | snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n", |
| 3054 | cval->min, cval->max, cval->dBmin, cval->dBmax); |
| 3055 | } |
| 3056 | |
| 3057 | static void snd_usb_mixer_proc_read(struct snd_info_entry *entry, |
| 3058 | struct snd_info_buffer *buffer) |
| 3059 | { |
| 3060 | struct snd_usb_audio *chip = entry->private_data; |
| 3061 | struct usb_mixer_interface *mixer; |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3062 | struct usb_mixer_elem_list *list; |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 3063 | int unitid; |
| 3064 | |
| 3065 | list_for_each_entry(mixer, &chip->mixer_list, list) { |
| 3066 | snd_iprintf(buffer, |
Jaroslav Kysela | 7affdc1 | 2010-02-16 11:52:27 +0100 | [diff] [blame] | 3067 | "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n", |
Daniel Mack | 3d8d4dc | 2010-06-16 17:57:31 +0200 | [diff] [blame] | 3068 | chip->usb_id, snd_usb_ctrl_intf(chip), |
Jaroslav Kysela | 7affdc1 | 2010-02-16 11:52:27 +0100 | [diff] [blame] | 3069 | mixer->ignore_ctl_error); |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 3070 | snd_iprintf(buffer, "Card: %s\n", chip->card->longname); |
| 3071 | for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) { |
Takashi Iwai | 8c55807 | 2018-05-03 12:33:32 +0200 | [diff] [blame] | 3072 | for_each_mixer_elem(list, mixer, unitid) { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3073 | snd_iprintf(buffer, " Unit: %i\n", list->id); |
| 3074 | if (list->kctl) |
| 3075 | snd_iprintf(buffer, |
| 3076 | " Control: name=\"%s\", index=%i\n", |
| 3077 | list->kctl->id.name, |
| 3078 | list->kctl->id.index); |
| 3079 | if (list->dump) |
| 3080 | list->dump(buffer, list); |
| 3081 | } |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 3082 | } |
| 3083 | } |
| 3084 | } |
| 3085 | |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3086 | static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer, |
| 3087 | int attribute, int value, int index) |
| 3088 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3089 | struct usb_mixer_elem_list *list; |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3090 | __u8 unitid = (index >> 8) & 0xff; |
| 3091 | __u8 control = (value >> 8) & 0xff; |
| 3092 | __u8 channel = value & 0xff; |
Daniel Mack | 191227d | 2016-04-08 19:52:02 +0200 | [diff] [blame] | 3093 | unsigned int count = 0; |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3094 | |
| 3095 | if (channel >= MAX_CHANNELS) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 3096 | usb_audio_dbg(mixer->chip, |
| 3097 | "%s(): bogus channel number %d\n", |
| 3098 | __func__, channel); |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3099 | return; |
| 3100 | } |
| 3101 | |
Takashi Iwai | 8c55807 | 2018-05-03 12:33:32 +0200 | [diff] [blame] | 3102 | for_each_mixer_elem(list, mixer, unitid) |
Daniel Mack | 191227d | 2016-04-08 19:52:02 +0200 | [diff] [blame] | 3103 | count++; |
| 3104 | |
| 3105 | if (count == 0) |
| 3106 | return; |
| 3107 | |
Takashi Iwai | 8c55807 | 2018-05-03 12:33:32 +0200 | [diff] [blame] | 3108 | for_each_mixer_elem(list, mixer, unitid) { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3109 | struct usb_mixer_elem_info *info; |
| 3110 | |
| 3111 | if (!list->kctl) |
| 3112 | continue; |
| 3113 | |
Takashi Iwai | 8c55807 | 2018-05-03 12:33:32 +0200 | [diff] [blame] | 3114 | info = mixer_elem_list_to_info(list); |
Daniel Mack | 191227d | 2016-04-08 19:52:02 +0200 | [diff] [blame] | 3115 | if (count > 1 && info->control != control) |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3116 | continue; |
| 3117 | |
| 3118 | switch (attribute) { |
| 3119 | case UAC2_CS_CUR: |
| 3120 | /* invalidate cache, so the value is read from the device */ |
| 3121 | if (channel) |
| 3122 | info->cached &= ~(1 << channel); |
| 3123 | else /* master channel */ |
| 3124 | info->cached = 0; |
| 3125 | |
| 3126 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3127 | &info->head.kctl->id); |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3128 | break; |
| 3129 | |
| 3130 | case UAC2_CS_RANGE: |
| 3131 | /* TODO */ |
| 3132 | break; |
| 3133 | |
| 3134 | case UAC2_CS_MEM: |
| 3135 | /* TODO */ |
| 3136 | break; |
| 3137 | |
| 3138 | default: |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 3139 | usb_audio_dbg(mixer->chip, |
| 3140 | "unknown attribute %d in interrupt\n", |
| 3141 | attribute); |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3142 | break; |
| 3143 | } /* switch */ |
| 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | static void snd_usb_mixer_interrupt(struct urb *urb) |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3148 | { |
| 3149 | struct usb_mixer_interface *mixer = urb->context; |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3150 | int len = urb->actual_length; |
Oliver Neukum | edf7de3 | 2011-03-11 13:19:43 +0100 | [diff] [blame] | 3151 | int ustatus = urb->status; |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3152 | |
Oliver Neukum | edf7de3 | 2011-03-11 13:19:43 +0100 | [diff] [blame] | 3153 | if (ustatus != 0) |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3154 | goto requeue; |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3155 | |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3156 | if (mixer->protocol == UAC_VERSION_1) { |
| 3157 | struct uac1_status_word *status; |
| 3158 | |
| 3159 | for (status = urb->transfer_buffer; |
| 3160 | len >= sizeof(*status); |
| 3161 | len -= sizeof(*status), status++) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 3162 | dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n", |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3163 | status->bStatusType, |
| 3164 | status->bOriginator); |
| 3165 | |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3166 | /* ignore any notifications not from the control interface */ |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3167 | if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) != |
| 3168 | UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF) |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3169 | continue; |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3170 | |
| 3171 | if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED) |
| 3172 | snd_usb_mixer_rc_memory_change(mixer, status->bOriginator); |
Clemens Ladisch | b259b10 | 2005-04-29 16:29:28 +0200 | [diff] [blame] | 3173 | else |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3174 | snd_usb_mixer_notify_id(mixer, status->bOriginator); |
| 3175 | } |
| 3176 | } else { /* UAC_VERSION_2 */ |
| 3177 | struct uac2_interrupt_data_msg *msg; |
| 3178 | |
| 3179 | for (msg = urb->transfer_buffer; |
| 3180 | len >= sizeof(*msg); |
| 3181 | len -= sizeof(*msg), msg++) { |
| 3182 | /* drop vendor specific and endpoint requests */ |
| 3183 | if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) || |
| 3184 | (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP)) |
| 3185 | continue; |
| 3186 | |
| 3187 | snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute, |
| 3188 | le16_to_cpu(msg->wValue), |
| 3189 | le16_to_cpu(msg->wIndex)); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3190 | } |
| 3191 | } |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3192 | |
| 3193 | requeue: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 3194 | if (ustatus != -ENOENT && |
| 3195 | ustatus != -ECONNRESET && |
| 3196 | ustatus != -ESHUTDOWN) { |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3197 | urb->dev = mixer->chip->dev; |
| 3198 | usb_submit_urb(urb, GFP_ATOMIC); |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | /* create the handler for the optional status interrupt endpoint */ |
| 3203 | static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer) |
| 3204 | { |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3205 | struct usb_endpoint_descriptor *ep; |
| 3206 | void *transfer_buffer; |
| 3207 | int buffer_length; |
| 3208 | unsigned int epnum; |
| 3209 | |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3210 | /* we need one interrupt input endpoint */ |
Daniel Mack | 1faa5d0 | 2011-08-04 15:56:28 +0200 | [diff] [blame] | 3211 | if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1) |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3212 | return 0; |
Daniel Mack | 1faa5d0 | 2011-08-04 15:56:28 +0200 | [diff] [blame] | 3213 | ep = get_endpoint(mixer->hostif, 0); |
Julia Lawall | 913ae5a | 2009-01-03 17:54:53 +0100 | [diff] [blame] | 3214 | if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep)) |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3215 | return 0; |
| 3216 | |
Julia Lawall | 42a6e66 | 2008-12-29 11:23:02 +0100 | [diff] [blame] | 3217 | epnum = usb_endpoint_num(ep); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3218 | buffer_length = le16_to_cpu(ep->wMaxPacketSize); |
| 3219 | transfer_buffer = kmalloc(buffer_length, GFP_KERNEL); |
| 3220 | if (!transfer_buffer) |
| 3221 | return -ENOMEM; |
| 3222 | mixer->urb = usb_alloc_urb(0, GFP_KERNEL); |
| 3223 | if (!mixer->urb) { |
| 3224 | kfree(transfer_buffer); |
| 3225 | return -ENOMEM; |
| 3226 | } |
| 3227 | usb_fill_int_urb(mixer->urb, mixer->chip->dev, |
| 3228 | usb_rcvintpipe(mixer->chip->dev, epnum), |
| 3229 | transfer_buffer, buffer_length, |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 3230 | snd_usb_mixer_interrupt, mixer, ep->bInterval); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3231 | usb_submit_urb(mixer->urb, GFP_KERNEL); |
| 3232 | return 0; |
| 3233 | } |
| 3234 | |
Takashi Iwai | 4120fbe | 2018-05-02 11:52:55 +0200 | [diff] [blame] | 3235 | static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol, |
| 3236 | struct snd_ctl_elem_value *ucontrol) |
| 3237 | { |
| 3238 | struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol); |
| 3239 | |
| 3240 | ucontrol->value.integer.value[0] = mixer->chip->keep_iface; |
| 3241 | return 0; |
| 3242 | } |
| 3243 | |
| 3244 | static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol, |
| 3245 | struct snd_ctl_elem_value *ucontrol) |
| 3246 | { |
| 3247 | struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol); |
| 3248 | bool keep_iface = !!ucontrol->value.integer.value[0]; |
| 3249 | |
| 3250 | if (mixer->chip->keep_iface == keep_iface) |
| 3251 | return 0; |
| 3252 | mixer->chip->keep_iface = keep_iface; |
| 3253 | return 1; |
| 3254 | } |
| 3255 | |
| 3256 | static const struct snd_kcontrol_new keep_iface_ctl = { |
| 3257 | .iface = SNDRV_CTL_ELEM_IFACE_CARD, |
| 3258 | .name = "Keep Interface", |
| 3259 | .info = snd_ctl_boolean_mono_info, |
| 3260 | .get = keep_iface_ctl_get, |
| 3261 | .put = keep_iface_ctl_put, |
| 3262 | }; |
| 3263 | |
| 3264 | static int create_keep_iface_ctl(struct usb_mixer_interface *mixer) |
| 3265 | { |
| 3266 | struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer); |
| 3267 | |
| 3268 | /* need only one control per card */ |
| 3269 | if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) { |
| 3270 | snd_ctl_free_one(kctl); |
| 3271 | return 0; |
| 3272 | } |
| 3273 | |
| 3274 | return snd_ctl_add(mixer->chip->card, kctl); |
| 3275 | } |
| 3276 | |
Takashi Iwai | 7a9b806 | 2008-08-13 15:40:53 +0200 | [diff] [blame] | 3277 | int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif, |
| 3278 | int ignore_error) |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3279 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 3280 | static struct snd_device_ops dev_ops = { |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3281 | .dev_free = snd_usb_mixer_dev_free |
| 3282 | }; |
| 3283 | struct usb_mixer_interface *mixer; |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 3284 | struct snd_info_entry *entry; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 3285 | int err; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3286 | |
| 3287 | strcpy(chip->card->mixername, "USB Mixer"); |
| 3288 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 3289 | mixer = kzalloc(sizeof(*mixer), GFP_KERNEL); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3290 | if (!mixer) |
| 3291 | return -ENOMEM; |
| 3292 | mixer->chip = chip; |
Takashi Iwai | 7a9b806 | 2008-08-13 15:40:53 +0200 | [diff] [blame] | 3293 | mixer->ignore_ctl_error = ignore_error; |
Jaroslav Kysela | 291186e | 2010-02-16 11:55:18 +0100 | [diff] [blame] | 3294 | mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems), |
| 3295 | GFP_KERNEL); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 3296 | if (!mixer->id_elems) { |
| 3297 | kfree(mixer); |
| 3298 | return -ENOMEM; |
| 3299 | } |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3300 | |
Daniel Mack | 1faa5d0 | 2011-08-04 15:56:28 +0200 | [diff] [blame] | 3301 | mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0]; |
| 3302 | switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) { |
Clemens Ladisch | a2acad8 | 2010-09-03 10:53:11 +0200 | [diff] [blame] | 3303 | case UAC_VERSION_1: |
| 3304 | default: |
| 3305 | mixer->protocol = UAC_VERSION_1; |
| 3306 | break; |
| 3307 | case UAC_VERSION_2: |
| 3308 | mixer->protocol = UAC_VERSION_2; |
| 3309 | break; |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 3310 | case UAC_VERSION_3: |
| 3311 | mixer->protocol = UAC_VERSION_3; |
| 3312 | break; |
Clemens Ladisch | a2acad8 | 2010-09-03 10:53:11 +0200 | [diff] [blame] | 3313 | } |
Daniel Mack | 7b8a043 | 2010-02-22 23:49:12 +0100 | [diff] [blame] | 3314 | |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 3315 | if (mixer->protocol == UAC_VERSION_3 && |
| 3316 | chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) { |
Takashi Iwai | f25ecf8 | 2018-05-27 15:18:22 +0200 | [diff] [blame] | 3317 | err = snd_usb_mixer_controls_badd(mixer, ctrlif); |
| 3318 | if (err < 0) |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 3319 | goto _error; |
Takashi Iwai | f25ecf8 | 2018-05-27 15:18:22 +0200 | [diff] [blame] | 3320 | } else { |
| 3321 | err = snd_usb_mixer_controls(mixer); |
| 3322 | if (err < 0) |
| 3323 | goto _error; |
| 3324 | err = snd_usb_mixer_status_create(mixer); |
| 3325 | if (err < 0) |
| 3326 | goto _error; |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 3327 | } |
Takashi Iwai | 4120fbe | 2018-05-02 11:52:55 +0200 | [diff] [blame] | 3328 | err = create_keep_iface_ctl(mixer); |
| 3329 | if (err < 0) |
| 3330 | goto _error; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3331 | |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 3332 | snd_usb_mixer_apply_create_quirk(mixer); |
Clemens Ladisch | 468b8fd | 2009-07-13 11:39:29 +0200 | [diff] [blame] | 3333 | |
Takashi Iwai | 9cbb280 | 2014-02-04 11:15:31 +0100 | [diff] [blame] | 3334 | err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops); |
Clemens Ladisch | 93446ed | 2005-05-03 08:02:40 +0200 | [diff] [blame] | 3335 | if (err < 0) |
| 3336 | goto _error; |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 3337 | |
| 3338 | if (list_empty(&chip->mixer_list) && |
| 3339 | !snd_card_proc_new(chip->card, "usbmixer", &entry)) |
| 3340 | snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read); |
| 3341 | |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3342 | list_add(&mixer->list, &chip->mixer_list); |
| 3343 | return 0; |
Clemens Ladisch | 93446ed | 2005-05-03 08:02:40 +0200 | [diff] [blame] | 3344 | |
| 3345 | _error: |
| 3346 | snd_usb_mixer_free(mixer); |
| 3347 | return err; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3348 | } |
| 3349 | |
Takashi Iwai | a6cece9 | 2014-10-31 11:24:32 +0100 | [diff] [blame] | 3350 | void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer) |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3351 | { |
Takashi Iwai | 124751d | 2017-10-10 14:10:32 +0200 | [diff] [blame] | 3352 | if (mixer->disconnected) |
| 3353 | return; |
| 3354 | if (mixer->urb) |
| 3355 | usb_kill_urb(mixer->urb); |
| 3356 | if (mixer->rc_urb) |
| 3357 | usb_kill_urb(mixer->rc_urb); |
| 3358 | mixer->disconnected = true; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 3359 | } |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 3360 | |
| 3361 | #ifdef CONFIG_PM |
| 3362 | /* stop any bus activity of a mixer */ |
| 3363 | static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer) |
| 3364 | { |
| 3365 | usb_kill_urb(mixer->urb); |
| 3366 | usb_kill_urb(mixer->rc_urb); |
| 3367 | } |
| 3368 | |
| 3369 | static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer) |
| 3370 | { |
| 3371 | int err; |
| 3372 | |
| 3373 | if (mixer->urb) { |
| 3374 | err = usb_submit_urb(mixer->urb, GFP_NOIO); |
| 3375 | if (err < 0) |
| 3376 | return err; |
| 3377 | } |
| 3378 | |
| 3379 | return 0; |
| 3380 | } |
| 3381 | |
| 3382 | int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer) |
| 3383 | { |
| 3384 | snd_usb_mixer_inactivate(mixer); |
| 3385 | return 0; |
| 3386 | } |
| 3387 | |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3388 | static int restore_mixer_value(struct usb_mixer_elem_list *list) |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 3389 | { |
Takashi Iwai | 8c55807 | 2018-05-03 12:33:32 +0200 | [diff] [blame] | 3390 | struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list); |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 3391 | int c, err, idx; |
| 3392 | |
| 3393 | if (cval->cmask) { |
| 3394 | idx = 0; |
| 3395 | for (c = 0; c < MAX_CHANNELS; c++) { |
| 3396 | if (!(cval->cmask & (1 << c))) |
| 3397 | continue; |
Yao-Wen Mao | 6aa6925 | 2015-08-28 16:33:25 +0800 | [diff] [blame] | 3398 | if (cval->cached & (1 << (c + 1))) { |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 3399 | err = snd_usb_set_cur_mix_value(cval, c + 1, idx, |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 3400 | cval->cache_val[idx]); |
| 3401 | if (err < 0) |
| 3402 | return err; |
| 3403 | } |
| 3404 | idx++; |
| 3405 | } |
| 3406 | } else { |
| 3407 | /* master */ |
| 3408 | if (cval->cached) { |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 3409 | 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] | 3410 | if (err < 0) |
| 3411 | return err; |
| 3412 | } |
| 3413 | } |
| 3414 | |
| 3415 | return 0; |
| 3416 | } |
| 3417 | |
| 3418 | int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume) |
| 3419 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3420 | struct usb_mixer_elem_list *list; |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 3421 | int id, err; |
| 3422 | |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 3423 | if (reset_resume) { |
| 3424 | /* restore cached mixer values */ |
| 3425 | for (id = 0; id < MAX_ID_ELEMS; id++) { |
Takashi Iwai | 8c55807 | 2018-05-03 12:33:32 +0200 | [diff] [blame] | 3426 | for_each_mixer_elem(list, mixer, id) { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3427 | if (list->resume) { |
| 3428 | err = list->resume(list); |
| 3429 | if (err < 0) |
| 3430 | return err; |
| 3431 | } |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 3432 | } |
| 3433 | } |
| 3434 | } |
| 3435 | |
Takashi Iwai | 964af63 | 2018-04-27 14:23:37 +0200 | [diff] [blame] | 3436 | snd_usb_mixer_resume_quirk(mixer); |
| 3437 | |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 3438 | return snd_usb_mixer_activate(mixer); |
| 3439 | } |
| 3440 | #endif |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3441 | |
| 3442 | void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list, |
| 3443 | struct usb_mixer_interface *mixer, |
| 3444 | int unitid) |
| 3445 | { |
| 3446 | list->mixer = mixer; |
| 3447 | list->id = unitid; |
| 3448 | list->dump = snd_usb_mixer_dump_cval; |
| 3449 | #ifdef CONFIG_PM |
| 3450 | list->resume = restore_mixer_value; |
| 3451 | #endif |
| 3452 | } |