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> |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [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 * |
| 115 | find_map(struct mixer_build *state, 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 | const struct usbmix_name_map *p = state->map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 119 | if (!p) |
| 120 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | for (p = state->map; p->id; p++) { |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 123 | if (p->id == unitid && |
| 124 | (!control || !p->control || control == p->control)) |
| 125 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | /* get the mapped name if the unit matches */ |
| 131 | static int |
| 132 | check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen) |
| 133 | { |
| 134 | if (!p || !p->name) |
| 135 | return 0; |
| 136 | |
| 137 | buflen--; |
| 138 | return strlcpy(buf, p->name, buflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 141 | /* ignore the error value if ignore_ctl_error flag is set */ |
| 142 | #define filter_error(cval, err) \ |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 143 | ((cval)->head.mixer->ignore_ctl_error ? 0 : (err)) |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | /* check whether the control should be ignored */ |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 146 | static inline int |
| 147 | check_ignored_ctl(const struct usbmix_name_map *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 149 | if (!p || p->name || p->dB) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | return 0; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | /* dB mapping */ |
| 155 | static inline void check_mapped_dB(const struct usbmix_name_map *p, |
| 156 | struct usb_mixer_elem_info *cval) |
| 157 | { |
| 158 | if (p && p->dB) { |
| 159 | cval->dBmin = p->dB->min; |
| 160 | cval->dBmax = p->dB->max; |
Takashi Iwai | 38b6519 | 2011-08-19 07:55:10 +0200 | [diff] [blame] | 161 | cval->initialized = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Clemens Ladisch | 8e062ec | 2005-04-22 15:49:52 +0200 | [diff] [blame] | 165 | /* get the mapped selector source name */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 166 | static int check_mapped_selector_name(struct mixer_build *state, int unitid, |
Clemens Ladisch | 8e062ec | 2005-04-22 15:49:52 +0200 | [diff] [blame] | 167 | int index, char *buf, int buflen) |
| 168 | { |
| 169 | const struct usbmix_selector_map *p; |
| 170 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 171 | if (!state->selector_map) |
Clemens Ladisch | 8e062ec | 2005-04-22 15:49:52 +0200 | [diff] [blame] | 172 | return 0; |
| 173 | for (p = state->selector_map; p->id; p++) { |
| 174 | if (p->id == unitid && index < p->count) |
| 175 | return strlcpy(buf, p->names[index], buflen); |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* |
| 181 | * find an audio control unit with the given unit id |
| 182 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 183 | static void *find_audio_control_unit(struct mixer_build *state, |
| 184 | unsigned char unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Daniel Mack | 67e1daa | 2010-05-31 13:35:43 +0200 | [diff] [blame] | 186 | /* we just parse the header */ |
| 187 | struct uac_feature_unit_descriptor *hdr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 189 | if (state->mixer->protocol == UAC_VERSION_3) { |
| 190 | int i; |
| 191 | |
| 192 | for (i = 0; i < NUM_BADD_DESCS; i++) { |
| 193 | hdr = (void *)badd_desc_list[i]; |
| 194 | if (hdr->bUnitID == unit) |
| 195 | return hdr; |
| 196 | } |
| 197 | |
| 198 | return NULL; |
| 199 | } |
Daniel Mack | 67e1daa | 2010-05-31 13:35:43 +0200 | [diff] [blame] | 200 | while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr, |
| 201 | USB_DT_CS_INTERFACE)) != NULL) { |
| 202 | if (hdr->bLength >= 4 && |
| 203 | hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL && |
| 204 | hdr->bDescriptorSubtype <= UAC2_SAMPLE_RATE_CONVERTER && |
| 205 | hdr->bUnitID == unit) |
| 206 | return hdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
Daniel Mack | 67e1daa | 2010-05-31 13:35:43 +0200 | [diff] [blame] | 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return NULL; |
| 210 | } |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | /* |
| 213 | * copy a string with the given id |
| 214 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 215 | static int snd_usb_copy_string_desc(struct mixer_build *state, |
| 216 | int index, char *buf, int maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
| 218 | int len = usb_string(state->chip->dev, index, buf, maxlen - 1); |
| 219 | buf[len] = 0; |
| 220 | return len; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * convert from the byte/word on usb descriptor to the zero-based integer |
| 225 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 226 | 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] | 227 | { |
| 228 | switch (cval->val_type) { |
| 229 | case USB_MIXER_BOOLEAN: |
| 230 | return !!val; |
| 231 | case USB_MIXER_INV_BOOLEAN: |
| 232 | return !val; |
| 233 | case USB_MIXER_U8: |
| 234 | val &= 0xff; |
| 235 | break; |
| 236 | case USB_MIXER_S8: |
| 237 | val &= 0xff; |
| 238 | if (val >= 0x80) |
| 239 | val -= 0x100; |
| 240 | break; |
| 241 | case USB_MIXER_U16: |
| 242 | val &= 0xffff; |
| 243 | break; |
| 244 | case USB_MIXER_S16: |
| 245 | val &= 0xffff; |
| 246 | if (val >= 0x8000) |
| 247 | val -= 0x10000; |
| 248 | break; |
| 249 | } |
| 250 | return val; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * convert from the zero-based int to the byte/word for usb descriptor |
| 255 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 256 | 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] | 257 | { |
| 258 | switch (cval->val_type) { |
| 259 | case USB_MIXER_BOOLEAN: |
| 260 | return !!val; |
| 261 | case USB_MIXER_INV_BOOLEAN: |
| 262 | return !val; |
| 263 | case USB_MIXER_S8: |
| 264 | case USB_MIXER_U8: |
| 265 | return val & 0xff; |
| 266 | case USB_MIXER_S16: |
| 267 | case USB_MIXER_U16: |
| 268 | return val & 0xffff; |
| 269 | } |
| 270 | return 0; /* not reached */ |
| 271 | } |
| 272 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 273 | 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] | 274 | { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 275 | if (!cval->res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | cval->res = 1; |
| 277 | if (val < cval->min) |
| 278 | return 0; |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 279 | else if (val >= cval->max) |
| 280 | return (cval->max - cval->min + cval->res - 1) / cval->res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | else |
| 282 | return (val - cval->min) / cval->res; |
| 283 | } |
| 284 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 285 | 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] | 286 | { |
| 287 | if (val < 0) |
| 288 | return cval->min; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 289 | if (!cval->res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | cval->res = 1; |
| 291 | val *= cval->res; |
| 292 | val += cval->min; |
| 293 | if (val > cval->max) |
| 294 | return cval->max; |
| 295 | return val; |
| 296 | } |
| 297 | |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 298 | static int uac2_ctl_value_size(int val_type) |
| 299 | { |
| 300 | switch (val_type) { |
| 301 | case USB_MIXER_S32: |
| 302 | case USB_MIXER_U32: |
| 303 | return 4; |
| 304 | case USB_MIXER_S16: |
| 305 | case USB_MIXER_U16: |
| 306 | return 2; |
| 307 | default: |
| 308 | return 1; |
| 309 | } |
| 310 | return 0; /* unreachable */ |
| 311 | } |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | /* |
| 315 | * retrieve a mixer value |
| 316 | */ |
| 317 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 318 | static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request, |
| 319 | int validx, int *value_ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 321 | struct snd_usb_audio *chip = cval->head.mixer->chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | unsigned char buf[2]; |
| 323 | int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; |
| 324 | int timeout = 10; |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 325 | int idx = 0, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 327 | err = snd_usb_lock_shutdown(chip); |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 328 | if (err < 0) |
| 329 | return -EIO; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | while (timeout-- > 0) { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 332 | idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8); |
Daniel Mack | 3d8d4dc | 2010-06-16 17:57:31 +0200 | [diff] [blame] | 333 | if (snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 335 | validx, idx, buf, val_len) >= val_len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | *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] | 337 | err = 0; |
| 338 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 341 | usb_audio_dbg(chip, |
| 342 | "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", |
| 343 | request, validx, idx, cval->val_type); |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 344 | err = -EINVAL; |
| 345 | |
| 346 | out: |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 347 | snd_usb_unlock_shutdown(chip); |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 348 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 351 | static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request, |
| 352 | int validx, int *value_ret) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 353 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 354 | struct snd_usb_audio *chip = cval->head.mixer->chip; |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 355 | unsigned char buf[4 + 3 * sizeof(__u32)]; /* enough space for one range */ |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 356 | unsigned char *val; |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 357 | int idx = 0, ret, size; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 358 | __u8 bRequest; |
| 359 | |
Daniel Mack | e8bdb6b | 2010-06-11 17:34:22 +0200 | [diff] [blame] | 360 | if (request == UAC_GET_CUR) { |
| 361 | bRequest = UAC2_CS_CUR; |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 362 | size = uac2_ctl_value_size(cval->val_type); |
Daniel Mack | e8bdb6b | 2010-06-11 17:34:22 +0200 | [diff] [blame] | 363 | } else { |
| 364 | bRequest = UAC2_CS_RANGE; |
| 365 | size = sizeof(buf); |
| 366 | } |
| 367 | |
| 368 | memset(buf, 0, sizeof(buf)); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 369 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 370 | ret = snd_usb_lock_shutdown(chip) ? -EIO : 0; |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 371 | if (ret) |
| 372 | goto error; |
| 373 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 374 | idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8); |
| 375 | 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] | 376 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 377 | validx, idx, buf, size); |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 378 | snd_usb_unlock_shutdown(chip); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 379 | |
| 380 | if (ret < 0) { |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 381 | error: |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 382 | usb_audio_err(chip, |
| 383 | "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", |
| 384 | request, validx, idx, cval->val_type); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 385 | return ret; |
| 386 | } |
| 387 | |
Daniel Mack | e8bdb6b | 2010-06-11 17:34:22 +0200 | [diff] [blame] | 388 | /* FIXME: how should we handle multiple triplets here? */ |
| 389 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 390 | switch (request) { |
| 391 | case UAC_GET_CUR: |
| 392 | val = buf; |
| 393 | break; |
| 394 | case UAC_GET_MIN: |
| 395 | val = buf + sizeof(__u16); |
| 396 | break; |
| 397 | case UAC_GET_MAX: |
| 398 | val = buf + sizeof(__u16) * 2; |
| 399 | break; |
| 400 | case UAC_GET_RES: |
| 401 | val = buf + sizeof(__u16) * 3; |
| 402 | break; |
| 403 | default: |
| 404 | return -EINVAL; |
| 405 | } |
| 406 | |
| 407 | *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(val, sizeof(__u16))); |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 412 | static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, |
| 413 | int validx, int *value_ret) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 414 | { |
Eldad Zack | 9f81410 | 2012-11-28 23:55:35 +0100 | [diff] [blame] | 415 | validx += cval->idx_off; |
| 416 | |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 417 | return (cval->head.mixer->protocol == UAC_VERSION_1) ? |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 418 | get_ctl_value_v1(cval, request, validx, value_ret) : |
| 419 | get_ctl_value_v2(cval, request, validx, value_ret); |
| 420 | } |
| 421 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 422 | static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, |
| 423 | int validx, int *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 425 | return get_ctl_value(cval, UAC_GET_CUR, validx, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /* channel = 0: master, 1 = first channel */ |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 429 | static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval, |
| 430 | int channel, int *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 432 | return get_ctl_value(cval, UAC_GET_CUR, |
| 433 | (cval->control << 8) | channel, |
| 434 | value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 437 | 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] | 438 | int channel, int index, int *value) |
| 439 | { |
| 440 | int err; |
| 441 | |
| 442 | if (cval->cached & (1 << channel)) { |
| 443 | *value = cval->cache_val[index]; |
| 444 | return 0; |
| 445 | } |
| 446 | err = get_cur_mix_raw(cval, channel, value); |
| 447 | if (err < 0) { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 448 | if (!cval->head.mixer->ignore_ctl_error) |
| 449 | usb_audio_dbg(cval->head.mixer->chip, |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 450 | "cannot get current value for control %d ch %d: err = %d\n", |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 451 | cval->control, channel, err); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 452 | return err; |
| 453 | } |
| 454 | cval->cached |= 1 << channel; |
| 455 | cval->cache_val[index] = *value; |
| 456 | return 0; |
| 457 | } |
| 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | /* |
| 460 | * set a mixer value |
| 461 | */ |
| 462 | |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 463 | int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval, |
| 464 | int request, int validx, int value_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 466 | struct snd_usb_audio *chip = cval->head.mixer->chip; |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 467 | unsigned char buf[4]; |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 468 | int idx = 0, val_len, err, timeout = 10; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 469 | |
Eldad Zack | 9f81410 | 2012-11-28 23:55:35 +0100 | [diff] [blame] | 470 | validx += cval->idx_off; |
| 471 | |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 472 | if (cval->head.mixer->protocol == UAC_VERSION_1) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 473 | val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; |
| 474 | } else { /* UAC_VERSION_2 */ |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 475 | val_len = uac2_ctl_value_size(cval->val_type); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 476 | |
| 477 | /* FIXME */ |
| 478 | if (request != UAC_SET_CUR) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 479 | usb_audio_dbg(chip, "RANGE setting not yet supported\n"); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 480 | return -EINVAL; |
| 481 | } |
| 482 | |
| 483 | request = UAC2_CS_CUR; |
| 484 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
| 486 | value_set = convert_bytes_value(cval, value_set); |
| 487 | buf[0] = value_set & 0xff; |
| 488 | buf[1] = (value_set >> 8) & 0xff; |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 489 | buf[2] = (value_set >> 16) & 0xff; |
| 490 | buf[3] = (value_set >> 24) & 0xff; |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 491 | |
| 492 | err = snd_usb_lock_shutdown(chip); |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 493 | if (err < 0) |
| 494 | return -EIO; |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 495 | |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 496 | while (timeout-- > 0) { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 497 | idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8); |
Daniel Mack | 3d8d4dc | 2010-06-16 17:57:31 +0200 | [diff] [blame] | 498 | if (snd_usb_ctl_msg(chip->dev, |
| 499 | usb_sndctrlpipe(chip->dev, 0), request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 501 | validx, idx, buf, val_len) >= 0) { |
| 502 | err = 0; |
| 503 | goto out; |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 504 | } |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 505 | } |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 506 | 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] | 507 | request, validx, idx, cval->val_type, buf[0], buf[1]); |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 508 | err = -EINVAL; |
| 509 | |
| 510 | out: |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 511 | snd_usb_unlock_shutdown(chip); |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 512 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 515 | static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, |
| 516 | int validx, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 518 | 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] | 519 | } |
| 520 | |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 521 | 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] | 522 | int index, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 524 | int err; |
Daniel Mack | a6a3325 | 2010-05-31 13:35:37 +0200 | [diff] [blame] | 525 | unsigned int read_only = (channel == 0) ? |
| 526 | cval->master_readonly : |
| 527 | cval->ch_readonly & (1 << (channel - 1)); |
| 528 | |
| 529 | if (read_only) { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 530 | usb_audio_dbg(cval->head.mixer->chip, |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 531 | "%s(): channel %d of control %d is read_only\n", |
Daniel Mack | a6a3325 | 2010-05-31 13:35:37 +0200 | [diff] [blame] | 532 | __func__, channel, cval->control); |
| 533 | return 0; |
| 534 | } |
| 535 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 536 | err = snd_usb_mixer_set_ctl_value(cval, |
| 537 | UAC_SET_CUR, (cval->control << 8) | channel, |
| 538 | value); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 539 | if (err < 0) |
| 540 | return err; |
| 541 | cval->cached |= 1 << channel; |
| 542 | cval->cache_val[index] = value; |
| 543 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Takashi Iwai | 7bc5ba7e | 2006-07-14 15:18:19 +0200 | [diff] [blame] | 546 | /* |
| 547 | * TLV callback for mixer volume controls |
| 548 | */ |
Felix Homann | 285de9c | 2012-04-23 20:24:24 +0200 | [diff] [blame] | 549 | 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] | 550 | unsigned int size, unsigned int __user *_tlv) |
| 551 | { |
| 552 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Takashi Iwai | b8e1c73 | 2009-06-16 14:04:37 +0200 | [diff] [blame] | 553 | DECLARE_TLV_DB_MINMAX(scale, 0, 0); |
Takashi Iwai | 7bc5ba7e | 2006-07-14 15:18:19 +0200 | [diff] [blame] | 554 | |
| 555 | if (size < sizeof(scale)) |
| 556 | return -ENOMEM; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 557 | scale[2] = cval->dBmin; |
| 558 | scale[3] = cval->dBmax; |
Takashi Iwai | 7bc5ba7e | 2006-07-14 15:18:19 +0200 | [diff] [blame] | 559 | if (copy_to_user(_tlv, scale, sizeof(scale))) |
| 560 | return -EFAULT; |
| 561 | return 0; |
| 562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | /* |
| 565 | * parser routines begin here... |
| 566 | */ |
| 567 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 568 | static int parse_audio_unit(struct mixer_build *state, int unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
| 570 | |
| 571 | /* |
| 572 | * check if the input/output channel routing is enabled on the given bitmap. |
| 573 | * used for mixer unit parser |
| 574 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 575 | static int check_matrix_bitmap(unsigned char *bmap, |
| 576 | int ich, int och, int num_outs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | { |
| 578 | int idx = ich * num_outs + och; |
| 579 | return bmap[idx >> 3] & (0x80 >> (idx & 7)); |
| 580 | } |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | /* |
| 583 | * add an alsa control element |
| 584 | * search and increment the index until an empty slot is found. |
| 585 | * |
| 586 | * if failed, give up and free the control instance. |
| 587 | */ |
| 588 | |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 589 | int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list, |
Daniel Mack | ef9d597 | 2011-05-25 09:09:00 +0200 | [diff] [blame] | 590 | struct snd_kcontrol *kctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 592 | struct usb_mixer_interface *mixer = list->mixer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | int err; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 594 | |
Daniel Mack | ef9d597 | 2011-05-25 09:09:00 +0200 | [diff] [blame] | 595 | while (snd_ctl_find_id(mixer->chip->card, &kctl->id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | kctl->id.index++; |
Daniel Mack | ef9d597 | 2011-05-25 09:09:00 +0200 | [diff] [blame] | 597 | if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 598 | usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n", |
| 599 | err); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 600 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 602 | list->kctl = kctl; |
| 603 | list->next_id_elem = mixer->id_elems[list->id]; |
| 604 | mixer->id_elems[list->id] = list; |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 605 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | /* |
| 609 | * get a terminal name string |
| 610 | */ |
| 611 | |
| 612 | static struct iterm_name_combo { |
| 613 | int type; |
| 614 | char *name; |
| 615 | } iterm_names[] = { |
| 616 | { 0x0300, "Output" }, |
| 617 | { 0x0301, "Speaker" }, |
| 618 | { 0x0302, "Headphone" }, |
| 619 | { 0x0303, "HMD Audio" }, |
| 620 | { 0x0304, "Desktop Speaker" }, |
| 621 | { 0x0305, "Room Speaker" }, |
| 622 | { 0x0306, "Com Speaker" }, |
| 623 | { 0x0307, "LFE" }, |
| 624 | { 0x0600, "External In" }, |
| 625 | { 0x0601, "Analog In" }, |
| 626 | { 0x0602, "Digital In" }, |
| 627 | { 0x0603, "Line" }, |
| 628 | { 0x0604, "Legacy In" }, |
| 629 | { 0x0605, "IEC958 In" }, |
| 630 | { 0x0606, "1394 DA Stream" }, |
| 631 | { 0x0607, "1394 DV Stream" }, |
| 632 | { 0x0700, "Embedded" }, |
| 633 | { 0x0701, "Noise Source" }, |
| 634 | { 0x0702, "Equalization Noise" }, |
| 635 | { 0x0703, "CD" }, |
| 636 | { 0x0704, "DAT" }, |
| 637 | { 0x0705, "DCC" }, |
| 638 | { 0x0706, "MiniDisk" }, |
| 639 | { 0x0707, "Analog Tape" }, |
| 640 | { 0x0708, "Phonograph" }, |
| 641 | { 0x0709, "VCR Audio" }, |
| 642 | { 0x070a, "Video Disk Audio" }, |
| 643 | { 0x070b, "DVD Audio" }, |
| 644 | { 0x070c, "TV Tuner Audio" }, |
| 645 | { 0x070d, "Satellite Rec Audio" }, |
| 646 | { 0x070e, "Cable Tuner Audio" }, |
| 647 | { 0x070f, "DSS Audio" }, |
| 648 | { 0x0710, "Radio Receiver" }, |
| 649 | { 0x0711, "Radio Transmitter" }, |
| 650 | { 0x0712, "Multi-Track Recorder" }, |
| 651 | { 0x0713, "Synthesizer" }, |
| 652 | { 0 }, |
| 653 | }; |
| 654 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 655 | static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | unsigned char *name, int maxlen, int term_only) |
| 657 | { |
| 658 | struct iterm_name_combo *names; |
| 659 | |
| 660 | if (iterm->name) |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 661 | return snd_usb_copy_string_desc(state, iterm->name, |
| 662 | name, maxlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | /* virtual type - not a real terminal */ |
| 665 | if (iterm->type >> 16) { |
| 666 | if (term_only) |
| 667 | return 0; |
| 668 | switch (iterm->type >> 16) { |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 669 | case UAC_SELECTOR_UNIT: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 670 | strcpy(name, "Selector"); |
| 671 | return 8; |
Daniel Mack | 69da9bc | 2010-06-16 17:57:28 +0200 | [diff] [blame] | 672 | case UAC1_PROCESSING_UNIT: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 673 | strcpy(name, "Process Unit"); |
| 674 | return 12; |
Daniel Mack | 69da9bc | 2010-06-16 17:57:28 +0200 | [diff] [blame] | 675 | case UAC1_EXTENSION_UNIT: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 676 | strcpy(name, "Ext Unit"); |
| 677 | return 8; |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 678 | case UAC_MIXER_UNIT: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 679 | strcpy(name, "Mixer"); |
| 680 | return 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | default: |
| 682 | return sprintf(name, "Unit %d", iterm->id); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | switch (iterm->type & 0xff00) { |
| 687 | case 0x0100: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 688 | strcpy(name, "PCM"); |
| 689 | return 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | case 0x0200: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 691 | strcpy(name, "Mic"); |
| 692 | return 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | case 0x0400: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 694 | strcpy(name, "Headset"); |
| 695 | return 7; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | case 0x0500: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 697 | strcpy(name, "Phone"); |
| 698 | return 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 701 | for (names = iterm_names; names->type; names++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | if (names->type == iterm->type) { |
| 703 | strcpy(name, names->name); |
| 704 | return strlen(names->name); |
| 705 | } |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 706 | } |
| 707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | return 0; |
| 709 | } |
| 710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | /* |
| 712 | * parse the source unit recursively until it reaches to a terminal |
| 713 | * or a branched unit. |
| 714 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 715 | static int check_input_term(struct mixer_build *state, int id, |
| 716 | struct usb_audio_term *term) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | { |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 718 | int err; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 719 | void *p1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
| 721 | memset(term, 0, sizeof(*term)); |
| 722 | while ((p1 = find_audio_control_unit(state, id)) != NULL) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 723 | unsigned char *hdr = p1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | term->id = id; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 725 | switch (hdr[2]) { |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 726 | case UAC_INPUT_TERMINAL: |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 727 | if (state->mixer->protocol == UAC_VERSION_1) { |
| 728 | struct uac_input_terminal_descriptor *d = p1; |
| 729 | term->type = le16_to_cpu(d->wTerminalType); |
| 730 | term->channels = d->bNrChannels; |
| 731 | term->chconfig = le16_to_cpu(d->wChannelConfig); |
| 732 | term->name = d->iTerminal; |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 733 | } else if (state->mixer->protocol == UAC_VERSION_2) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 734 | struct uac2_input_terminal_descriptor *d = p1; |
Julian Scheel | 9430e54 | 2015-08-19 09:28:09 +0200 | [diff] [blame] | 735 | |
| 736 | /* call recursively to verify that the |
| 737 | * referenced clock entity is valid */ |
| 738 | err = check_input_term(state, d->bCSourceID, term); |
| 739 | if (err < 0) |
| 740 | return err; |
| 741 | |
| 742 | /* save input term properties after recursion, |
| 743 | * to ensure they are not overriden by the |
| 744 | * recursion calls */ |
| 745 | term->id = id; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 746 | term->type = le16_to_cpu(d->wTerminalType); |
| 747 | term->channels = d->bNrChannels; |
| 748 | term->chconfig = le32_to_cpu(d->bmChannelConfig); |
| 749 | term->name = d->iTerminal; |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 750 | } else { /* UAC_VERSION_3 */ |
| 751 | struct uac3_input_terminal_descriptor *d = p1; |
| 752 | |
| 753 | err = check_input_term(state, |
| 754 | d->bCSourceID, term); |
| 755 | if (err < 0) |
| 756 | return err; |
| 757 | |
| 758 | term->id = id; |
| 759 | term->type = d->wTerminalType; |
| 760 | if (d->wClusterDescrID == CLUSTER_ID_MONO) { |
| 761 | term->channels = NUM_CHANNELS_MONO; |
| 762 | term->chconfig = BADD_CH_CONFIG_MONO; |
| 763 | } else { |
| 764 | term->channels = NUM_CHANNELS_STEREO; |
| 765 | term->chconfig = BADD_CH_CONFIG_STEREO; |
| 766 | } |
| 767 | term->name = d->wTerminalDescrStr; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | return 0; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 770 | case UAC_FEATURE_UNIT: { |
| 771 | /* the header is the same for v1 and v2 */ |
| 772 | struct uac_feature_unit_descriptor *d = p1; |
Daniel Mack | 5e68888 | 2010-05-08 11:24:56 +0200 | [diff] [blame] | 773 | id = d->bSourceID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | break; /* continue to parse */ |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 775 | } |
| 776 | case UAC_MIXER_UNIT: { |
| 777 | struct uac_mixer_unit_descriptor *d = p1; |
| 778 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
| 779 | term->channels = uac_mixer_unit_bNrChannels(d); |
| 780 | term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol); |
| 781 | term->name = uac_mixer_unit_iMixer(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | return 0; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 783 | } |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 784 | case UAC_SELECTOR_UNIT: |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 785 | /* UAC3_MIXER_UNIT_V3 */ |
| 786 | case UAC2_CLOCK_SELECTOR: |
| 787 | /* UAC3_CLOCK_SOURCE */ { |
| 788 | if (state->mixer->protocol == UAC_VERSION_3 |
| 789 | && hdr[2] == UAC3_CLOCK_SOURCE) { |
| 790 | struct uac3_clock_source_descriptor *d = p1; |
| 791 | |
| 792 | term->type = d->bDescriptorSubtype << 16; |
| 793 | term->id = id; |
| 794 | term->name = d->wClockSourceStr; |
| 795 | } else if (state->mixer->protocol == UAC_VERSION_3 |
| 796 | && hdr[2] == UAC3_MIXER_UNIT_V3) { |
| 797 | struct uac3_mixer_unit_descriptor *d = p1; |
| 798 | |
| 799 | term->type = d->bDescriptorSubtype << 16; |
| 800 | if (d->wClusterDescrID == CLUSTER_ID_MONO) { |
| 801 | term->channels = NUM_CHANNELS_MONO; |
| 802 | term->chconfig = BADD_CH_CONFIG_MONO; |
| 803 | } else { |
| 804 | term->channels = NUM_CHANNELS_STEREO; |
| 805 | term->chconfig = BADD_CH_CONFIG_STEREO; |
| 806 | } |
| 807 | term->name = d->wMixerDescrStr; |
| 808 | } else { |
| 809 | struct uac_selector_unit_descriptor *d = p1; |
| 810 | /* call recursively to retrieve channel info */ |
| 811 | err = check_input_term(state, |
| 812 | d->baSourceID[0], term); |
| 813 | if (err < 0) |
| 814 | return err; |
| 815 | /* virtual type */ |
| 816 | term->type = d->bDescriptorSubtype << 16; |
| 817 | term->id = id; |
| 818 | term->name = uac_selector_unit_iSelector(d); |
| 819 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | return 0; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 821 | } |
Daniel Mack | 69da9bc | 2010-06-16 17:57:28 +0200 | [diff] [blame] | 822 | case UAC1_PROCESSING_UNIT: |
Eldad Zack | 5dae5fd | 2012-11-28 23:55:36 +0100 | [diff] [blame] | 823 | case UAC1_EXTENSION_UNIT: |
| 824 | /* UAC2_PROCESSING_UNIT_V2 */ |
Torstein Hegge | 61ac513 | 2013-03-19 17:12:14 +0100 | [diff] [blame] | 825 | /* UAC2_EFFECT_UNIT */ |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 826 | /* UAC3_FEATURE_UNIT_V3 */ |
Torstein Hegge | 61ac513 | 2013-03-19 17:12:14 +0100 | [diff] [blame] | 827 | case UAC2_EXTENSION_UNIT_V2: { |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 828 | if (state->mixer->protocol == UAC_VERSION_3) { |
| 829 | struct uac_feature_unit_descriptor *d = p1; |
Eldad Zack | 5dae5fd | 2012-11-28 23:55:36 +0100 | [diff] [blame] | 830 | |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 831 | id = d->bSourceID; |
| 832 | } else { |
| 833 | struct uac_processing_unit_descriptor *d = p1; |
| 834 | |
| 835 | if (state->mixer->protocol == UAC_VERSION_2 && |
| 836 | hdr[2] == UAC2_EFFECT_UNIT) { |
| 837 | /* UAC2/UAC1 unit IDs overlap here in an |
| 838 | * uncompatible way. Ignore this unit |
| 839 | * for now. |
| 840 | */ |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | if (d->bNrInPins) { |
| 845 | id = d->baSourceID[0]; |
| 846 | break; /* continue to parse */ |
| 847 | } |
| 848 | /* virtual type */ |
| 849 | term->type = d->bDescriptorSubtype << 16; |
| 850 | term->channels = |
| 851 | uac_processing_unit_bNrChannels(d); |
| 852 | term->chconfig = |
| 853 | uac_processing_unit_wChannelConfig( |
| 854 | d, state->mixer->protocol); |
| 855 | term->name = uac_processing_unit_iProcessing( |
| 856 | d, state->mixer->protocol); |
Eldad Zack | 5dae5fd | 2012-11-28 23:55:36 +0100 | [diff] [blame] | 857 | return 0; |
| 858 | } |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 859 | break; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 860 | } |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 861 | case UAC2_CLOCK_SOURCE: { |
| 862 | struct uac_clock_source_descriptor *d = p1; |
| 863 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
| 864 | term->id = id; |
| 865 | term->name = d->iClockSource; |
| 866 | return 0; |
| 867 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | default: |
| 869 | return -ENODEV; |
| 870 | } |
| 871 | } |
| 872 | return -ENODEV; |
| 873 | } |
| 874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | /* |
| 876 | * Feature Unit |
| 877 | */ |
| 878 | |
| 879 | /* feature unit control information */ |
| 880 | struct usb_feature_control_info { |
| 881 | const char *name; |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 882 | int type; /* data type for uac1 */ |
| 883 | 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] | 884 | }; |
| 885 | |
| 886 | static struct usb_feature_control_info audio_feature_info[] = { |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 887 | { "Mute", USB_MIXER_INV_BOOLEAN, -1 }, |
| 888 | { "Volume", USB_MIXER_S16, -1 }, |
| 889 | { "Tone Control - Bass", USB_MIXER_S8, -1 }, |
| 890 | { "Tone Control - Mid", USB_MIXER_S8, -1 }, |
| 891 | { "Tone Control - Treble", USB_MIXER_S8, -1 }, |
| 892 | { "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemeted yet */ |
| 893 | { "Auto Gain Control", USB_MIXER_BOOLEAN, -1 }, |
| 894 | { "Delay Control", USB_MIXER_U16, USB_MIXER_U32 }, |
| 895 | { "Bass Boost", USB_MIXER_BOOLEAN, -1 }, |
| 896 | { "Loudness", USB_MIXER_BOOLEAN, -1 }, |
Daniel Mack | 2e0281d | 2010-05-31 13:35:42 +0200 | [diff] [blame] | 897 | /* UAC2 specific */ |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 898 | { "Input Gain Control", USB_MIXER_S16, -1 }, |
| 899 | { "Input Gain Pad Control", USB_MIXER_S16, -1 }, |
| 900 | { "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | }; |
| 902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | /* private_free callback */ |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 904 | void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
Jesper Juhl | 4d57277 | 2005-05-30 17:30:32 +0200 | [diff] [blame] | 906 | kfree(kctl->private_data); |
| 907 | kctl->private_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | /* |
| 911 | * interface to ALSA control for feature/mixer units |
| 912 | */ |
| 913 | |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 914 | /* volume control quirks */ |
| 915 | static void volume_control_quirks(struct usb_mixer_elem_info *cval, |
| 916 | struct snd_kcontrol *kctl) |
| 917 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 918 | struct snd_usb_audio *chip = cval->head.mixer->chip; |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 919 | switch (chip->usb_id) { |
Eldad Zack | d50ed62 | 2012-11-28 23:55:39 +0100 | [diff] [blame] | 920 | case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */ |
Matt Gruskin | e9a25e0 | 2013-02-09 12:56:35 -0500 | [diff] [blame] | 921 | case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */ |
Eldad Zack | d50ed62 | 2012-11-28 23:55:39 +0100 | [diff] [blame] | 922 | if (strcmp(kctl->id.name, "Effect Duration") == 0) { |
| 923 | cval->min = 0x0000; |
| 924 | cval->max = 0xffff; |
| 925 | cval->res = 0x00e6; |
| 926 | break; |
| 927 | } |
| 928 | if (strcmp(kctl->id.name, "Effect Volume") == 0 || |
| 929 | strcmp(kctl->id.name, "Effect Feedback Volume") == 0) { |
| 930 | cval->min = 0x00; |
| 931 | cval->max = 0xff; |
| 932 | break; |
| 933 | } |
| 934 | if (strstr(kctl->id.name, "Effect Return") != NULL) { |
| 935 | cval->min = 0xb706; |
| 936 | cval->max = 0xff7b; |
| 937 | cval->res = 0x0073; |
| 938 | break; |
| 939 | } |
| 940 | if ((strstr(kctl->id.name, "Playback Volume") != NULL) || |
| 941 | (strstr(kctl->id.name, "Effect Send") != NULL)) { |
| 942 | cval->min = 0xb5fb; /* -73 dB = 0xb6ff */ |
| 943 | cval->max = 0xfcfe; |
| 944 | cval->res = 0x0073; |
| 945 | } |
| 946 | break; |
| 947 | |
Felix Homann | d34bf14 | 2012-04-23 20:24:27 +0200 | [diff] [blame] | 948 | case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */ |
| 949 | case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */ |
| 950 | if (strcmp(kctl->id.name, "Effect Duration") == 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 951 | usb_audio_info(chip, |
| 952 | "set quirk for FTU Effect Duration\n"); |
Felix Homann | d34bf14 | 2012-04-23 20:24:27 +0200 | [diff] [blame] | 953 | cval->min = 0x0000; |
| 954 | cval->max = 0x7f00; |
| 955 | cval->res = 0x0100; |
| 956 | break; |
| 957 | } |
| 958 | if (strcmp(kctl->id.name, "Effect Volume") == 0 || |
| 959 | strcmp(kctl->id.name, "Effect Feedback Volume") == 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 960 | usb_audio_info(chip, |
| 961 | "set quirks for FTU Effect Feedback/Volume\n"); |
Felix Homann | d34bf14 | 2012-04-23 20:24:27 +0200 | [diff] [blame] | 962 | cval->min = 0x00; |
| 963 | cval->max = 0x7f; |
| 964 | break; |
| 965 | } |
| 966 | break; |
| 967 | |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 968 | case USB_ID(0x0471, 0x0101): |
| 969 | case USB_ID(0x0471, 0x0104): |
| 970 | case USB_ID(0x0471, 0x0105): |
| 971 | case USB_ID(0x0672, 0x1041): |
| 972 | /* quirk for UDA1321/N101. |
| 973 | * note that detection between firmware 2.1.1.7 (N101) |
| 974 | * and later 2.1.1.21 is not very clear from datasheets. |
| 975 | * I hope that the min value is -15360 for newer firmware --jk |
| 976 | */ |
| 977 | if (!strcmp(kctl->id.name, "PCM Playback Volume") && |
| 978 | cval->min == -15616) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 979 | usb_audio_info(chip, |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 980 | "set volume quirk for UDA1321/N101 chip\n"); |
| 981 | cval->max = -256; |
| 982 | } |
| 983 | break; |
| 984 | |
| 985 | case USB_ID(0x046d, 0x09a4): |
| 986 | if (!strcmp(kctl->id.name, "Mic Capture Volume")) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 987 | usb_audio_info(chip, |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 988 | "set volume quirk for QuickCam E3500\n"); |
| 989 | cval->min = 6080; |
| 990 | cval->max = 8768; |
| 991 | cval->res = 192; |
| 992 | } |
| 993 | break; |
| 994 | |
Takashi Iwai | e805ca8 | 2014-03-05 12:34:39 +0100 | [diff] [blame] | 995 | case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */ |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 996 | case USB_ID(0x046d, 0x0808): |
| 997 | case USB_ID(0x046d, 0x0809): |
Jason Lee Cragg | 6455931 | 2015-01-17 12:28:29 -0500 | [diff] [blame] | 998 | case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */ |
Takashi Iwai | 36691e1 | 2013-06-17 10:25:02 +0200 | [diff] [blame] | 999 | case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */ |
Alexey Fisher | 55c0008 | 2011-11-09 11:39:24 +0100 | [diff] [blame] | 1000 | case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */ |
Takashi Iwai | 11e7064 | 2013-06-05 08:35:26 +0200 | [diff] [blame] | 1001 | case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */ |
Maksim A. Boyko | 140d37d | 2013-08-10 12:20:02 +0400 | [diff] [blame] | 1002 | case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */ |
Wolfram Sang | 1ef9f05 | 2015-05-29 19:50:56 +0900 | [diff] [blame] | 1003 | case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */ |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1004 | case USB_ID(0x046d, 0x0991): |
Con Kolivas | 205d3de | 2016-12-09 15:15:57 +1100 | [diff] [blame] | 1005 | case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */ |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1006 | /* Most audio usb devices lie about volume resolution. |
| 1007 | * Most Logitech webcams have res = 384. |
Con Kolivas | 205d3de | 2016-12-09 15:15:57 +1100 | [diff] [blame] | 1008 | * Probably there is some logitech magic behind this number --fishor |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1009 | */ |
| 1010 | if (!strcmp(kctl->id.name, "Mic Capture Volume")) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1011 | usb_audio_info(chip, |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1012 | "set resolution quirk: cval->res = 384\n"); |
| 1013 | cval->res = 384; |
| 1014 | } |
| 1015 | break; |
Ajay Agarwal | 5a19df0 | 2017-06-22 11:09:33 +0530 | [diff] [blame] | 1016 | |
| 1017 | case USB_ID(0x1130, 0x1620): /* Logitech Speakers S150 */ |
| 1018 | /* This audio device has 2 channels and it explicitly requires the |
| 1019 | * host to send SET_CUR command on the volume control of both the |
| 1020 | * channels. 7936 = 0x1F00 is the default value. |
| 1021 | */ |
| 1022 | if (cval->channels == 2) |
| 1023 | snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, |
| 1024 | (cval->control << 8) | 2, 7936); |
| 1025 | break; |
| 1026 | |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | /* |
| 1031 | * retrieve the minimum and maximum values for the specified control |
| 1032 | */ |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1033 | static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval, |
| 1034 | int default_min, struct snd_kcontrol *kctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | { |
| 1036 | /* for failsafe */ |
| 1037 | cval->min = default_min; |
| 1038 | cval->max = cval->min + 1; |
| 1039 | cval->res = 1; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1040 | cval->dBmin = cval->dBmax = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
| 1042 | if (cval->val_type == USB_MIXER_BOOLEAN || |
| 1043 | cval->val_type == USB_MIXER_INV_BOOLEAN) { |
| 1044 | cval->initialized = 1; |
| 1045 | } else { |
| 1046 | int minchn = 0; |
| 1047 | if (cval->cmask) { |
| 1048 | int i; |
| 1049 | for (i = 0; i < MAX_CHANNELS; i++) |
| 1050 | if (cval->cmask & (1 << i)) { |
| 1051 | minchn = i + 1; |
| 1052 | break; |
| 1053 | } |
| 1054 | } |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1055 | if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 || |
| 1056 | 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] | 1057 | usb_audio_err(cval->head.mixer->chip, |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1058 | "%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] | 1059 | cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip), |
| 1060 | cval->control, cval->head.id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | return -EINVAL; |
| 1062 | } |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1063 | if (get_ctl_value(cval, UAC_GET_RES, |
| 1064 | (cval->control << 8) | minchn, |
| 1065 | &cval->res) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | cval->res = 1; |
| 1067 | } else { |
| 1068 | int last_valid_res = cval->res; |
| 1069 | |
| 1070 | while (cval->res > 1) { |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 1071 | if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1072 | (cval->control << 8) | minchn, |
| 1073 | cval->res / 2) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | break; |
| 1075 | cval->res /= 2; |
| 1076 | } |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1077 | if (get_ctl_value(cval, UAC_GET_RES, |
| 1078 | (cval->control << 8) | minchn, &cval->res) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | cval->res = last_valid_res; |
| 1080 | } |
| 1081 | if (cval->res == 0) |
| 1082 | cval->res = 1; |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 1083 | |
| 1084 | /* Additional checks for the proper resolution |
| 1085 | * |
| 1086 | * Some devices report smaller resolutions than actually |
| 1087 | * reacting. They don't return errors but simply clip |
| 1088 | * to the lower aligned value. |
| 1089 | */ |
| 1090 | if (cval->min + cval->res < cval->max) { |
| 1091 | int last_valid_res = cval->res; |
| 1092 | int saved, test, check; |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1093 | get_cur_mix_raw(cval, minchn, &saved); |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 1094 | for (;;) { |
| 1095 | test = saved; |
| 1096 | if (test < cval->max) |
| 1097 | test += cval->res; |
| 1098 | else |
| 1099 | test -= cval->res; |
| 1100 | if (test < cval->min || test > cval->max || |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1101 | snd_usb_set_cur_mix_value(cval, minchn, 0, test) || |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1102 | get_cur_mix_raw(cval, minchn, &check)) { |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 1103 | cval->res = last_valid_res; |
| 1104 | break; |
| 1105 | } |
| 1106 | if (test == check) |
| 1107 | break; |
| 1108 | cval->res *= 2; |
| 1109 | } |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1110 | snd_usb_set_cur_mix_value(cval, minchn, 0, saved); |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | cval->initialized = 1; |
| 1114 | } |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1115 | |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1116 | if (kctl) |
| 1117 | volume_control_quirks(cval, kctl); |
| 1118 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1119 | /* USB descriptions contain the dB scale in 1/256 dB unit |
| 1120 | * while ALSA TLV contains in 1/100 dB unit |
| 1121 | */ |
Deepa Madiregama | 8b7f7fe | 2013-08-21 17:54:08 +0530 | [diff] [blame] | 1122 | cval->dBmin = |
| 1123 | (convert_signed_value(cval, cval->min) * 100) / (cval->res); |
| 1124 | cval->dBmax = |
| 1125 | (convert_signed_value(cval, cval->max) * 100) / (cval->res); |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1126 | if (cval->dBmin > cval->dBmax) { |
| 1127 | /* something is wrong; assume it's either from/to 0dB */ |
| 1128 | if (cval->dBmin < 0) |
| 1129 | cval->dBmax = 0; |
| 1130 | else if (cval->dBmin > 0) |
| 1131 | cval->dBmin = 0; |
| 1132 | if (cval->dBmin > cval->dBmax) { |
| 1133 | /* totally crap, return an error */ |
| 1134 | return -EINVAL; |
| 1135 | } |
| 1136 | } |
| 1137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | return 0; |
| 1139 | } |
| 1140 | |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1141 | #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] | 1142 | |
| 1143 | /* get a feature/mixer unit info */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1144 | static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, |
| 1145 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1147 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | |
| 1149 | if (cval->val_type == USB_MIXER_BOOLEAN || |
| 1150 | cval->val_type == USB_MIXER_INV_BOOLEAN) |
| 1151 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1152 | else |
| 1153 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1154 | uinfo->count = cval->channels; |
| 1155 | if (cval->val_type == USB_MIXER_BOOLEAN || |
| 1156 | cval->val_type == USB_MIXER_INV_BOOLEAN) { |
| 1157 | uinfo->value.integer.min = 0; |
| 1158 | uinfo->value.integer.max = 1; |
| 1159 | } else { |
Takashi Iwai | 9fcd0ab | 2011-08-19 08:30:53 +0200 | [diff] [blame] | 1160 | if (!cval->initialized) { |
Takashi Iwai | dcaaf9f | 2011-11-08 17:50:27 +0100 | [diff] [blame] | 1161 | get_min_max_with_quirks(cval, 0, kcontrol); |
Takashi Iwai | 9fcd0ab | 2011-08-19 08:30:53 +0200 | [diff] [blame] | 1162 | if (cval->initialized && cval->dBmin >= cval->dBmax) { |
| 1163 | kcontrol->vd[0].access &= |
| 1164 | ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ | |
| 1165 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK); |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 1166 | snd_ctl_notify(cval->head.mixer->chip->card, |
Takashi Iwai | 9fcd0ab | 2011-08-19 08:30:53 +0200 | [diff] [blame] | 1167 | SNDRV_CTL_EVENT_MASK_INFO, |
| 1168 | &kcontrol->id); |
| 1169 | } |
| 1170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | uinfo->value.integer.min = 0; |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 1172 | uinfo->value.integer.max = |
| 1173 | (cval->max - cval->min + cval->res - 1) / cval->res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | } |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | /* get the current value from feature/mixer unit */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1179 | static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, |
| 1180 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1182 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | int c, cnt, val, err; |
| 1184 | |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1185 | ucontrol->value.integer.value[0] = cval->min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | if (cval->cmask) { |
| 1187 | cnt = 0; |
| 1188 | for (c = 0; c < MAX_CHANNELS; c++) { |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1189 | if (!(cval->cmask & (1 << c))) |
| 1190 | continue; |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1191 | err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1192 | if (err < 0) |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 1193 | return filter_error(cval, err); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1194 | val = get_relative_value(cval, val); |
| 1195 | ucontrol->value.integer.value[cnt] = val; |
| 1196 | cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | } |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1198 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | } else { |
| 1200 | /* master channel */ |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1201 | err = snd_usb_get_cur_mix_value(cval, 0, 0, &val); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1202 | if (err < 0) |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 1203 | return filter_error(cval, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | val = get_relative_value(cval, val); |
| 1205 | ucontrol->value.integer.value[0] = val; |
| 1206 | } |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | /* put the current value to feature/mixer unit */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1211 | static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, |
| 1212 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1214 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | int c, cnt, val, oval, err; |
| 1216 | int changed = 0; |
| 1217 | |
| 1218 | if (cval->cmask) { |
| 1219 | cnt = 0; |
| 1220 | for (c = 0; c < MAX_CHANNELS; c++) { |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1221 | if (!(cval->cmask & (1 << c))) |
| 1222 | continue; |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1223 | err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1224 | if (err < 0) |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 1225 | return filter_error(cval, err); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1226 | val = ucontrol->value.integer.value[cnt]; |
| 1227 | val = get_abs_value(cval, val); |
| 1228 | if (oval != val) { |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1229 | snd_usb_set_cur_mix_value(cval, c + 1, cnt, val); |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1230 | changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | } |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 1232 | cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | } |
| 1234 | } else { |
| 1235 | /* master channel */ |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1236 | err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | if (err < 0) |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 1238 | return filter_error(cval, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | val = ucontrol->value.integer.value[0]; |
| 1240 | val = get_abs_value(cval, val); |
| 1241 | if (val != oval) { |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1242 | snd_usb_set_cur_mix_value(cval, 0, 0, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | changed = 1; |
| 1244 | } |
| 1245 | } |
| 1246 | return changed; |
| 1247 | } |
| 1248 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1249 | static struct snd_kcontrol_new usb_feature_unit_ctl = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1251 | .name = "", /* will be filled later manually */ |
| 1252 | .info = mixer_ctl_feature_info, |
| 1253 | .get = mixer_ctl_feature_get, |
| 1254 | .put = mixer_ctl_feature_put, |
| 1255 | }; |
| 1256 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1257 | /* the read-only variant */ |
| 1258 | static struct snd_kcontrol_new usb_feature_unit_ctl_ro = { |
| 1259 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1260 | .name = "", /* will be filled later manually */ |
| 1261 | .info = mixer_ctl_feature_info, |
| 1262 | .get = mixer_ctl_feature_get, |
| 1263 | .put = NULL, |
| 1264 | }; |
| 1265 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1266 | /* |
| 1267 | * This symbol is exported in order to allow the mixer quirks to |
| 1268 | * hook up to the standard feature unit control mechanism |
| 1269 | */ |
Daniel Mack | 9e38658 | 2011-05-25 09:09:01 +0200 | [diff] [blame] | 1270 | 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] | 1271 | |
| 1272 | /* |
| 1273 | * build a feature control |
| 1274 | */ |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 1275 | static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str) |
| 1276 | { |
| 1277 | return strlcat(kctl->id.name, str, sizeof(kctl->id.name)); |
| 1278 | } |
| 1279 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1280 | /* |
| 1281 | * A lot of headsets/headphones have a "Speaker" mixer. Make sure we |
| 1282 | * rename it to "Headphone". We determine if something is a headphone |
| 1283 | * similar to how udev determines form factor. |
| 1284 | */ |
David Henningsson | 9b4ef97 | 2012-11-23 13:48:55 +0100 | [diff] [blame] | 1285 | static void check_no_speaker_on_headset(struct snd_kcontrol *kctl, |
| 1286 | struct snd_card *card) |
| 1287 | { |
| 1288 | const char *names_to_check[] = { |
| 1289 | "Headset", "headset", "Headphone", "headphone", NULL}; |
| 1290 | const char **s; |
Peter Senna Tschudin | e0f17c7 | 2013-09-22 20:44:12 +0200 | [diff] [blame] | 1291 | bool found = false; |
David Henningsson | 9b4ef97 | 2012-11-23 13:48:55 +0100 | [diff] [blame] | 1292 | |
| 1293 | if (strcmp("Speaker", kctl->id.name)) |
| 1294 | return; |
| 1295 | |
| 1296 | for (s = names_to_check; *s; s++) |
| 1297 | if (strstr(card->shortname, *s)) { |
Peter Senna Tschudin | e0f17c7 | 2013-09-22 20:44:12 +0200 | [diff] [blame] | 1298 | found = true; |
David Henningsson | 9b4ef97 | 2012-11-23 13:48:55 +0100 | [diff] [blame] | 1299 | break; |
| 1300 | } |
| 1301 | |
| 1302 | if (!found) |
| 1303 | return; |
| 1304 | |
| 1305 | strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name)); |
| 1306 | } |
| 1307 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1308 | static void build_feature_ctl(struct mixer_build *state, void *raw_desc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | unsigned int ctl_mask, int control, |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1310 | struct usb_audio_term *iterm, int unitid, |
Daniel Mack | a6a3325 | 2010-05-31 13:35:37 +0200 | [diff] [blame] | 1311 | int readonly_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1313 | struct uac_feature_unit_descriptor *desc = raw_desc; |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 1314 | struct usb_feature_control_info *ctl_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | unsigned int len = 0; |
| 1316 | int mapped_name = 0; |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 1317 | int nameid; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1318 | struct snd_kcontrol *kctl; |
| 1319 | struct usb_mixer_elem_info *cval; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1320 | const struct usbmix_name_map *map; |
Alexey Fisher | 80aceff | 2011-03-10 14:53:38 +0100 | [diff] [blame] | 1321 | unsigned int range; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 1323 | if (state->mixer->protocol == UAC_VERSION_3) |
| 1324 | nameid = ((struct uac3_feature_unit_descriptor *) |
| 1325 | raw_desc)->wFeatureDescrStr; |
| 1326 | else |
| 1327 | nameid = uac_feature_unit_iFeature(desc); |
| 1328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | control++; /* change from zero-based to 1-based value */ |
| 1330 | |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 1331 | if (control == UAC_FU_GRAPHIC_EQUALIZER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | /* FIXME: not supported yet */ |
| 1333 | return; |
| 1334 | } |
| 1335 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1336 | map = find_map(state, unitid, control); |
| 1337 | if (check_ignored_ctl(map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | return; |
| 1339 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 1340 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
Daniel Mack | a860d95 | 2014-05-24 10:58:17 +0200 | [diff] [blame] | 1341 | if (!cval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | return; |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 1343 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | cval->control = control; |
| 1345 | cval->cmask = ctl_mask; |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 1346 | ctl_info = &audio_feature_info[control-1]; |
| 1347 | if (state->mixer->protocol == UAC_VERSION_1) |
| 1348 | cval->val_type = ctl_info->type; |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 1349 | else /* UAC_VERSION_2 or UAC_VERSION_3*/ |
Julian Scheel | bc18e31 | 2015-08-14 16:14:45 +0200 | [diff] [blame] | 1350 | cval->val_type = ctl_info->type_uac2 >= 0 ? |
| 1351 | ctl_info->type_uac2 : ctl_info->type; |
| 1352 | |
Daniel Mack | a6a3325 | 2010-05-31 13:35:37 +0200 | [diff] [blame] | 1353 | if (ctl_mask == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | cval->channels = 1; /* master channel */ |
Daniel Mack | a6a3325 | 2010-05-31 13:35:37 +0200 | [diff] [blame] | 1355 | cval->master_readonly = readonly_mask; |
| 1356 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | int i, c = 0; |
| 1358 | for (i = 0; i < 16; i++) |
| 1359 | if (ctl_mask & (1 << i)) |
| 1360 | c++; |
| 1361 | cval->channels = c; |
Daniel Mack | a6a3325 | 2010-05-31 13:35:37 +0200 | [diff] [blame] | 1362 | cval->ch_readonly = readonly_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1365 | /* |
| 1366 | * 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] | 1367 | * 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] | 1368 | * issue write commands to read-only channels. |
| 1369 | */ |
Daniel Mack | a6a3325 | 2010-05-31 13:35:37 +0200 | [diff] [blame] | 1370 | if (cval->channels == readonly_mask) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1371 | kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval); |
| 1372 | else |
| 1373 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); |
| 1374 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1375 | if (!kctl) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1376 | usb_audio_err(state->chip, "cannot malloc kcontrol\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | kfree(cval); |
| 1378 | return; |
| 1379 | } |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1380 | kctl->private_free = snd_usb_mixer_elem_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1382 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | mapped_name = len != 0; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1384 | if (!len && nameid) |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1385 | len = snd_usb_copy_string_desc(state, nameid, |
| 1386 | kctl->id.name, sizeof(kctl->id.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | |
| 1388 | switch (control) { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 1389 | case UAC_FU_MUTE: |
| 1390 | case UAC_FU_VOLUME: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1391 | /* |
| 1392 | * determine the control name. the rule is: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | * - if a name id is given in descriptor, use it. |
| 1394 | * - if the connected input can be determined, then use the name |
| 1395 | * of terminal type. |
| 1396 | * - if the connected output can be determined, use it. |
| 1397 | * - otherwise, anonymous name. |
| 1398 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1399 | if (!len) { |
| 1400 | len = get_term_name(state, iterm, kctl->id.name, |
| 1401 | sizeof(kctl->id.name), 1); |
| 1402 | if (!len) |
| 1403 | len = get_term_name(state, &state->oterm, |
| 1404 | kctl->id.name, |
| 1405 | sizeof(kctl->id.name), 1); |
| 1406 | if (!len) |
Daniel Mack | 49fd46d | 2014-10-19 09:11:25 +0200 | [diff] [blame] | 1407 | snprintf(kctl->id.name, sizeof(kctl->id.name), |
| 1408 | "Feature %d", unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | } |
David Henningsson | 9b4ef97 | 2012-11-23 13:48:55 +0100 | [diff] [blame] | 1410 | |
| 1411 | if (!mapped_name) |
| 1412 | check_no_speaker_on_headset(kctl, state->mixer->chip->card); |
| 1413 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1414 | /* |
| 1415 | * determine the stream direction: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | * if the connected output is USB stream, then it's likely a |
| 1417 | * capture stream. otherwise it should be playback (hopefully :) |
| 1418 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1419 | if (!mapped_name && !(state->oterm.type >> 16)) { |
| 1420 | if ((state->oterm.type & 0xff00) == 0x0100) |
Daniel Mack | 49fd46d | 2014-10-19 09:11:25 +0200 | [diff] [blame] | 1421 | append_ctl_name(kctl, " Capture"); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1422 | else |
Daniel Mack | 49fd46d | 2014-10-19 09:11:25 +0200 | [diff] [blame] | 1423 | append_ctl_name(kctl, " Playback"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | } |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 1425 | append_ctl_name(kctl, control == UAC_FU_MUTE ? |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 1426 | " Switch" : " Volume"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | default: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1429 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | strlcpy(kctl->id.name, audio_feature_info[control-1].name, |
| 1431 | sizeof(kctl->id.name)); |
| 1432 | break; |
| 1433 | } |
| 1434 | |
Takashi Iwai | e182534 | 2012-05-14 17:11:06 +0200 | [diff] [blame] | 1435 | /* get min/max values */ |
| 1436 | get_min_max_with_quirks(cval, 0, kctl); |
| 1437 | |
| 1438 | if (control == UAC_FU_VOLUME) { |
| 1439 | check_mapped_dB(map, cval); |
| 1440 | if (cval->dBmin < cval->dBmax || !cval->initialized) { |
| 1441 | kctl->tlv.c = snd_usb_mixer_vol_tlv; |
| 1442 | kctl->vd[0].access |= |
| 1443 | SNDRV_CTL_ELEM_ACCESS_TLV_READ | |
| 1444 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; |
| 1445 | } |
| 1446 | } |
| 1447 | |
Anssi Hannula | 42e3121 | 2015-12-13 20:49:58 +0200 | [diff] [blame] | 1448 | snd_usb_mixer_fu_apply_quirk(state->mixer, cval, unitid, kctl); |
| 1449 | |
Alexey Fisher | 80aceff | 2011-03-10 14:53:38 +0100 | [diff] [blame] | 1450 | range = (cval->max - cval->min) / cval->res; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1451 | /* |
| 1452 | * 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] | 1453 | * to be sure. 384 is a resolution magic number found on Logitech |
| 1454 | * devices. It will definitively catch all buggy Logitech devices. |
| 1455 | */ |
| 1456 | if (range > 384) { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1457 | usb_audio_warn(state->chip, |
Michał Mirosław | 82c1cf0 | 2014-08-03 15:09:57 +0200 | [diff] [blame] | 1458 | "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.", |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1459 | range); |
Michał Mirosław | 82c1cf0 | 2014-08-03 15:09:57 +0200 | [diff] [blame] | 1460 | usb_audio_warn(state->chip, |
| 1461 | "[%d] FU [%s] ch = %d, val = %d/%d/%d", |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 1462 | cval->head.id, kctl->id.name, cval->channels, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1463 | cval->min, cval->max, cval->res); |
Alexey Fisher | 80aceff | 2011-03-10 14:53:38 +0100 | [diff] [blame] | 1464 | } |
| 1465 | |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1466 | usb_audio_dbg(state->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n", |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 1467 | cval->head.id, kctl->id.name, cval->channels, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1468 | cval->min, cval->max, cval->res); |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 1469 | snd_usb_mixer_add_control(&cval->head, kctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
Daniel Mack | cddaafb | 2016-04-09 11:21:46 +0200 | [diff] [blame] | 1472 | static int parse_clock_source_unit(struct mixer_build *state, int unitid, |
| 1473 | void *_ftr) |
| 1474 | { |
| 1475 | struct uac_clock_source_descriptor *hdr = _ftr; |
| 1476 | struct usb_mixer_elem_info *cval; |
| 1477 | struct snd_kcontrol *kctl; |
| 1478 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
| 1479 | int ret; |
| 1480 | |
| 1481 | if (state->mixer->protocol != UAC_VERSION_2) |
| 1482 | return -EINVAL; |
| 1483 | |
| 1484 | if (hdr->bLength != sizeof(*hdr)) { |
| 1485 | usb_audio_dbg(state->chip, |
| 1486 | "Bogus clock source descriptor length of %d, ignoring.\n", |
| 1487 | hdr->bLength); |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
| 1491 | /* |
| 1492 | * The only property of this unit we are interested in is the |
| 1493 | * clock source validity. If that isn't readable, just bail out. |
| 1494 | */ |
| 1495 | if (!uac2_control_is_readable(hdr->bmControls, |
| 1496 | ilog2(UAC2_CS_CONTROL_CLOCK_VALID))) |
| 1497 | return 0; |
| 1498 | |
| 1499 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
| 1500 | if (!cval) |
| 1501 | return -ENOMEM; |
| 1502 | |
| 1503 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID); |
| 1504 | |
| 1505 | cval->min = 0; |
| 1506 | cval->max = 1; |
| 1507 | cval->channels = 1; |
| 1508 | cval->val_type = USB_MIXER_BOOLEAN; |
| 1509 | cval->control = UAC2_CS_CONTROL_CLOCK_VALID; |
| 1510 | |
| 1511 | if (uac2_control_is_writeable(hdr->bmControls, |
| 1512 | ilog2(UAC2_CS_CONTROL_CLOCK_VALID))) |
| 1513 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); |
| 1514 | else { |
| 1515 | cval->master_readonly = 1; |
| 1516 | kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval); |
| 1517 | } |
| 1518 | |
| 1519 | if (!kctl) { |
| 1520 | kfree(cval); |
| 1521 | return -ENOMEM; |
| 1522 | } |
| 1523 | |
| 1524 | kctl->private_free = snd_usb_mixer_elem_free; |
| 1525 | ret = snd_usb_copy_string_desc(state, hdr->iClockSource, |
| 1526 | name, sizeof(name)); |
| 1527 | if (ret > 0) |
| 1528 | snprintf(kctl->id.name, sizeof(kctl->id.name), |
| 1529 | "%s Validity", name); |
| 1530 | else |
| 1531 | snprintf(kctl->id.name, sizeof(kctl->id.name), |
| 1532 | "Clock Source %d Validity", hdr->bClockID); |
| 1533 | |
| 1534 | return snd_usb_mixer_add_control(&cval->head, kctl); |
| 1535 | } |
| 1536 | |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 1537 | static int find_num_channels(struct mixer_build *state, int dir) |
| 1538 | { |
| 1539 | int num_ch = -EINVAL, num, i, j, wMaxPacketSize; |
| 1540 | int ctrlif = get_iface_desc(state->mixer->hostif)->bInterfaceNumber; |
| 1541 | struct usb_interface *usb_iface = |
| 1542 | usb_ifnum_to_if(state->mixer->chip->dev, ctrlif); |
| 1543 | struct usb_interface_assoc_descriptor *assoc = usb_iface->intf_assoc; |
| 1544 | struct usb_host_interface *alts; |
| 1545 | |
| 1546 | for (i = 0; i < assoc->bInterfaceCount; i++) { |
| 1547 | int intf = assoc->bFirstInterface + i; |
| 1548 | |
| 1549 | if (intf != ctrlif) { |
| 1550 | struct usb_interface *iface = |
| 1551 | usb_ifnum_to_if(state->mixer->chip->dev, intf); |
| 1552 | |
| 1553 | alts = &iface->altsetting[1]; |
| 1554 | if (dir == USB_DIR_OUT && |
| 1555 | get_endpoint(alts, 0)->bEndpointAddress & |
| 1556 | USB_DIR_IN) |
| 1557 | continue; |
| 1558 | if (dir == USB_DIR_IN && |
| 1559 | !(get_endpoint(alts, 0)->bEndpointAddress & |
| 1560 | USB_DIR_IN)) |
| 1561 | continue; |
| 1562 | num = iface->num_altsetting; |
| 1563 | for (j = 1; j < num; j++) { |
| 1564 | num_ch = NUM_CHANNELS_MONO; |
| 1565 | alts = &iface->altsetting[j]; |
| 1566 | wMaxPacketSize = le16_to_cpu( |
| 1567 | get_endpoint(alts, 0)-> |
| 1568 | wMaxPacketSize); |
| 1569 | switch (wMaxPacketSize) { |
| 1570 | case BADD_MAXPSIZE_SYNC_MONO_16: |
| 1571 | case BADD_MAXPSIZE_SYNC_MONO_24: |
| 1572 | case BADD_MAXPSIZE_ASYNC_MONO_16: |
| 1573 | case BADD_MAXPSIZE_ASYNC_MONO_24: |
| 1574 | break; |
| 1575 | case BADD_MAXPSIZE_SYNC_STEREO_16: |
| 1576 | case BADD_MAXPSIZE_SYNC_STEREO_24: |
| 1577 | case BADD_MAXPSIZE_ASYNC_STEREO_16: |
| 1578 | case BADD_MAXPSIZE_ASYNC_STEREO_24: |
| 1579 | num_ch = NUM_CHANNELS_STEREO; |
| 1580 | break; |
| 1581 | } |
| 1582 | if (num_ch == NUM_CHANNELS_MONO) |
| 1583 | continue; |
| 1584 | else |
| 1585 | break; |
| 1586 | } |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | return num_ch; |
| 1591 | } |
| 1592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | /* |
| 1594 | * parse a feature unit |
| 1595 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1596 | * most of controls are defined here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1598 | static int parse_audio_feature_unit(struct mixer_build *state, int unitid, |
| 1599 | void *_ftr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | { |
| 1601 | int channels, i, j; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1602 | struct usb_audio_term iterm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | unsigned int master_bits, first_ch_bits; |
| 1604 | int err, csize; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1605 | struct uac_feature_unit_descriptor *hdr = _ftr; |
| 1606 | __u8 *bmaControls; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1608 | if (state->mixer->protocol == UAC_VERSION_1) { |
| 1609 | csize = hdr->bControlSize; |
Nicolai Krakowiak | 60c961a9 | 2011-08-04 15:56:27 +0200 | [diff] [blame] | 1610 | if (!csize) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1611 | usb_audio_dbg(state->chip, |
| 1612 | "unit %u: invalid bControlSize == 0\n", |
| 1613 | unitid); |
Nicolai Krakowiak | 60c961a9 | 2011-08-04 15:56:27 +0200 | [diff] [blame] | 1614 | return -EINVAL; |
| 1615 | } |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1616 | channels = (hdr->bLength - 7) / csize - 1; |
| 1617 | bmaControls = hdr->bmaControls; |
Clemens Ladisch | d56268f | 2012-11-29 17:04:23 +0100 | [diff] [blame] | 1618 | if (hdr->bLength < 7 + csize) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1619 | usb_audio_err(state->chip, |
| 1620 | "unit %u: invalid UAC_FEATURE_UNIT descriptor\n", |
| 1621 | unitid); |
Clemens Ladisch | d56268f | 2012-11-29 17:04:23 +0100 | [diff] [blame] | 1622 | return -EINVAL; |
| 1623 | } |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 1624 | } else if (state->mixer->protocol == UAC_VERSION_2) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1625 | struct uac2_feature_unit_descriptor *ftr = _ftr; |
| 1626 | csize = 4; |
Daniel Mack | e8d0fee | 2010-05-27 20:15:14 +0200 | [diff] [blame] | 1627 | channels = (hdr->bLength - 6) / 4 - 1; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1628 | bmaControls = ftr->bmaControls; |
Clemens Ladisch | d56268f | 2012-11-29 17:04:23 +0100 | [diff] [blame] | 1629 | if (hdr->bLength < 6 + csize) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1630 | usb_audio_err(state->chip, |
| 1631 | "unit %u: invalid UAC_FEATURE_UNIT descriptor\n", |
| 1632 | unitid); |
Clemens Ladisch | d56268f | 2012-11-29 17:04:23 +0100 | [diff] [blame] | 1633 | return -EINVAL; |
| 1634 | } |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 1635 | } else { |
| 1636 | struct usb_interface *usb_iface = |
| 1637 | usb_ifnum_to_if(state->mixer->chip->dev, |
| 1638 | get_iface_desc(state->mixer->hostif)->bInterfaceNumber); |
| 1639 | struct usb_interface_assoc_descriptor *assoc = |
| 1640 | usb_iface->intf_assoc; |
| 1641 | |
| 1642 | csize = 4; |
| 1643 | switch (unitid) { |
| 1644 | case BADD_FU_ID_BAIOF: |
| 1645 | channels = NUM_CHANNELS_MONO; |
| 1646 | bmaControls = monoControls; |
| 1647 | badd_baif_in_term_desc.wClusterDescrID = |
| 1648 | CLUSTER_ID_MONO; |
| 1649 | break; |
| 1650 | |
| 1651 | case BADD_FU_ID_BAOF: |
| 1652 | switch (assoc->bFunctionSubClass) { |
| 1653 | case PROF_HEADPHONE: |
| 1654 | case PROF_HEADSET_ADAPTER: |
| 1655 | channels = NUM_CHANNELS_STEREO; |
| 1656 | bmaControls = stereoControls; |
| 1657 | badd_baiof_mu_desc.wClusterDescrID = |
| 1658 | CLUSTER_ID_MONO; |
| 1659 | break; |
| 1660 | case PROF_SPEAKERPHONE: |
| 1661 | channels = NUM_CHANNELS_MONO; |
| 1662 | bmaControls = monoControls; |
| 1663 | badd_baof_in_term_desc.wClusterDescrID = |
| 1664 | CLUSTER_ID_MONO; |
| 1665 | break; |
| 1666 | default: |
| 1667 | channels = find_num_channels(state, |
| 1668 | USB_DIR_OUT); |
| 1669 | if (channels < 0) { |
| 1670 | usb_audio_err(state->chip, |
| 1671 | "unit %u: Cant find num of channels\n", |
| 1672 | unitid); |
| 1673 | return channels; |
| 1674 | } |
| 1675 | |
| 1676 | bmaControls = (channels == NUM_CHANNELS_MONO) ? |
| 1677 | monoControls : stereoControls; |
| 1678 | badd_baof_in_term_desc.wClusterDescrID = |
| 1679 | (channels == NUM_CHANNELS_MONO) ? |
| 1680 | CLUSTER_ID_MONO : CLUSTER_ID_STEREO; |
| 1681 | break; |
| 1682 | } |
| 1683 | break; |
| 1684 | |
| 1685 | case BADD_FU_ID_BAIF: |
| 1686 | switch (assoc->bFunctionSubClass) { |
| 1687 | case PROF_HEADSET: |
| 1688 | case PROF_HEADSET_ADAPTER: |
| 1689 | case PROF_SPEAKERPHONE: |
| 1690 | channels = NUM_CHANNELS_MONO; |
| 1691 | bmaControls = monoControls; |
| 1692 | badd_baif_in_term_desc.wClusterDescrID = |
| 1693 | CLUSTER_ID_MONO; |
| 1694 | break; |
| 1695 | default: |
| 1696 | channels = find_num_channels(state, USB_DIR_IN); |
| 1697 | if (channels < 0) { |
| 1698 | usb_audio_err(state->chip, |
| 1699 | "unit %u: Cant find num of channels\n", |
| 1700 | unitid); |
| 1701 | return channels; |
| 1702 | } |
| 1703 | |
| 1704 | bmaControls = (channels == NUM_CHANNELS_MONO) ? |
| 1705 | monoControls : stereoControls; |
| 1706 | badd_baif_in_term_desc.wClusterDescrID = |
| 1707 | (channels == NUM_CHANNELS_MONO) ? |
| 1708 | CLUSTER_ID_MONO : CLUSTER_ID_STEREO; |
| 1709 | break; |
| 1710 | } |
| 1711 | break; |
| 1712 | |
| 1713 | default: |
| 1714 | usb_audio_err(state->chip, "Invalid unit %u\n", unitid); |
| 1715 | return -EINVAL; |
| 1716 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | /* parse the source unit */ |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 1720 | if (state->mixer->protocol != UAC_VERSION_3) { |
| 1721 | err = parse_audio_unit(state, hdr->bSourceID); |
| 1722 | if (err < 0) |
| 1723 | return err; |
| 1724 | } else { |
| 1725 | struct usb_interface *usb_iface = |
| 1726 | usb_ifnum_to_if(state->mixer->chip->dev, |
| 1727 | get_iface_desc(state->mixer->hostif)->bInterfaceNumber); |
| 1728 | struct usb_interface_assoc_descriptor *assoc = |
| 1729 | usb_iface->intf_assoc; |
| 1730 | |
| 1731 | switch (unitid) { |
| 1732 | case BADD_FU_ID_BAOF: |
| 1733 | switch (assoc->bFunctionSubClass) { |
| 1734 | case PROF_HEADSET: |
| 1735 | case PROF_HEADSET_ADAPTER: |
| 1736 | hdr->bSourceID = BADD_MU_ID_BAIOF; |
| 1737 | break; |
| 1738 | default: |
| 1739 | hdr->bSourceID = BADD_IN_TERM_ID_BAOF; |
| 1740 | break; |
| 1741 | } |
| 1742 | } |
| 1743 | err = parse_audio_unit(state, hdr->bSourceID); |
| 1744 | if (err < 0) |
| 1745 | return err; |
| 1746 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | |
| 1748 | /* determine the input source type and name */ |
Daniel Mack | 4d7b86c | 2013-03-19 21:09:24 +0100 | [diff] [blame] | 1749 | err = check_input_term(state, hdr->bSourceID, &iterm); |
| 1750 | if (err < 0) |
| 1751 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1753 | master_bits = snd_usb_combine_bytes(bmaControls, csize); |
Javier Kohen | 0c3cee5 | 2009-11-17 15:36:13 +0100 | [diff] [blame] | 1754 | /* master configuration quirks */ |
| 1755 | switch (state->chip->usb_id) { |
| 1756 | case USB_ID(0x08bb, 0x2702): |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1757 | usb_audio_info(state->chip, |
| 1758 | "usbmixer: master volume quirk for PCM2702 chip\n"); |
Javier Kohen | 0c3cee5 | 2009-11-17 15:36:13 +0100 | [diff] [blame] | 1759 | /* disable non-functional volume control */ |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 1760 | master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME); |
Javier Kohen | 0c3cee5 | 2009-11-17 15:36:13 +0100 | [diff] [blame] | 1761 | break; |
David Henningsson | c105143 | 2012-09-20 10:20:41 +0200 | [diff] [blame] | 1762 | case USB_ID(0x1130, 0xf211): |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1763 | usb_audio_info(state->chip, |
| 1764 | "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n"); |
David Henningsson | c105143 | 2012-09-20 10:20:41 +0200 | [diff] [blame] | 1765 | /* disable non-functional volume control */ |
| 1766 | channels = 0; |
| 1767 | break; |
| 1768 | |
Javier Kohen | 0c3cee5 | 2009-11-17 15:36:13 +0100 | [diff] [blame] | 1769 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | if (channels > 0) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1771 | first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | else |
| 1773 | first_ch_bits = 0; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1774 | |
| 1775 | if (state->mixer->protocol == UAC_VERSION_1) { |
| 1776 | /* check all control types */ |
| 1777 | for (i = 0; i < 10; i++) { |
| 1778 | unsigned int ch_bits = 0; |
| 1779 | for (j = 0; j < channels; j++) { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1780 | unsigned int mask; |
| 1781 | |
| 1782 | mask = snd_usb_combine_bytes(bmaControls + |
| 1783 | csize * (j+1), csize); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1784 | if (mask & (1 << i)) |
| 1785 | ch_bits |= (1 << j); |
| 1786 | } |
| 1787 | /* audio class v1 controls are never read-only */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1788 | |
| 1789 | /* |
| 1790 | * The first channel must be set |
| 1791 | * (for ease of programming). |
| 1792 | */ |
| 1793 | if (ch_bits & 1) |
| 1794 | build_feature_ctl(state, _ftr, ch_bits, i, |
| 1795 | &iterm, unitid, 0); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1796 | if (master_bits & (1 << i)) |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1797 | build_feature_ctl(state, _ftr, 0, i, &iterm, |
| 1798 | unitid, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | } |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 1800 | } else { /* UAC_VERSION_2 or UAC_VERSION_3*/ |
Takashi Iwai | d09c06c | 2011-10-13 08:19:09 +0200 | [diff] [blame] | 1801 | for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1802 | unsigned int ch_bits = 0; |
| 1803 | unsigned int ch_read_only = 0; |
| 1804 | |
| 1805 | for (j = 0; j < channels; j++) { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1806 | unsigned int mask; |
| 1807 | |
| 1808 | mask = snd_usb_combine_bytes(bmaControls + |
| 1809 | csize * (j+1), csize); |
Daniel Mack | dcbe7bc | 2010-05-31 13:35:36 +0200 | [diff] [blame] | 1810 | if (uac2_control_is_readable(mask, i)) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1811 | ch_bits |= (1 << j); |
Daniel Mack | dcbe7bc | 2010-05-31 13:35:36 +0200 | [diff] [blame] | 1812 | if (!uac2_control_is_writeable(mask, i)) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1813 | ch_read_only |= (1 << j); |
| 1814 | } |
| 1815 | } |
| 1816 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1817 | /* |
| 1818 | * NOTE: build_feature_ctl() will mark the control |
| 1819 | * read-only if all channels are marked read-only in |
| 1820 | * the descriptors. Otherwise, the control will be |
| 1821 | * reported as writeable, but the driver will not |
| 1822 | * actually issue a write command for read-only |
| 1823 | * channels. |
| 1824 | */ |
| 1825 | |
| 1826 | /* |
| 1827 | * The first channel must be set |
| 1828 | * (for ease of programming). |
| 1829 | */ |
| 1830 | if (ch_bits & 1) |
| 1831 | build_feature_ctl(state, _ftr, ch_bits, i, |
| 1832 | &iterm, unitid, ch_read_only); |
Daniel Mack | dcbe7bc | 2010-05-31 13:35:36 +0200 | [diff] [blame] | 1833 | if (uac2_control_is_readable(master_bits, i)) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1834 | build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, |
Daniel Mack | dcbe7bc | 2010-05-31 13:35:36 +0200 | [diff] [blame] | 1835 | !uac2_control_is_writeable(master_bits, i)); |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | return 0; |
| 1840 | } |
| 1841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | /* |
| 1843 | * Mixer Unit |
| 1844 | */ |
| 1845 | |
| 1846 | /* |
| 1847 | * build a mixer unit control |
| 1848 | * |
| 1849 | * the callbacks are identical with feature unit. |
| 1850 | * input channel number (zero based) is given in control field instead. |
| 1851 | */ |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1852 | static void build_mixer_unit_ctl(struct mixer_build *state, |
| 1853 | struct uac_mixer_unit_descriptor *desc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | int in_pin, int in_ch, int unitid, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1855 | struct usb_audio_term *iterm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1857 | struct usb_mixer_elem_info *cval; |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1858 | unsigned int num_outs = uac_mixer_unit_bNrChannels(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | unsigned int i, len; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1860 | struct snd_kcontrol *kctl; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1861 | const struct usbmix_name_map *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1863 | map = find_map(state, unitid, 0); |
| 1864 | if (check_ignored_ctl(map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | return; |
| 1866 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 1867 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1868 | if (!cval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | return; |
| 1870 | |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 1871 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | cval->control = in_ch + 1; /* based on 1 */ |
| 1873 | cval->val_type = USB_MIXER_S16; |
| 1874 | for (i = 0; i < num_outs; i++) { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1875 | __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol); |
| 1876 | |
| 1877 | if (check_matrix_bitmap(c, in_ch, i, num_outs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | cval->cmask |= (1 << i); |
| 1879 | cval->channels++; |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | /* get min/max values */ |
| 1884 | get_min_max(cval, 0); |
| 1885 | |
| 1886 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1887 | if (!kctl) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1888 | usb_audio_err(state->chip, "cannot malloc kcontrol\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | kfree(cval); |
| 1890 | return; |
| 1891 | } |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 1892 | kctl->private_free = snd_usb_mixer_elem_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1894 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1895 | if (!len) |
| 1896 | len = get_term_name(state, iterm, kctl->id.name, |
| 1897 | sizeof(kctl->id.name), 0); |
| 1898 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1); |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 1900 | append_ctl_name(kctl, " Volume"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1902 | 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] | 1903 | cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max); |
| 1904 | snd_usb_mixer_add_control(&cval->head, kctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | /* |
| 1908 | * parse a mixer unit |
| 1909 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1910 | static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, |
| 1911 | void *raw_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1913 | struct uac_mixer_unit_descriptor *desc = raw_desc; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1914 | struct usb_audio_term iterm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | int input_pins, num_ins, num_outs; |
| 1916 | int pin, ich, err; |
| 1917 | |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 1918 | if (state->mixer->protocol == UAC_VERSION_3) { |
| 1919 | input_pins = badd_baiof_mu_desc.bNrInPins; |
| 1920 | num_outs = |
| 1921 | (badd_baiof_mu_desc.wClusterDescrID == CLUSTER_ID_MONO) ? |
| 1922 | NUM_CHANNELS_MONO : NUM_CHANNELS_STEREO; |
| 1923 | } else { |
| 1924 | input_pins = desc->bNrInPins; |
| 1925 | num_outs = uac_mixer_unit_bNrChannels(desc); |
| 1926 | if (desc->bLength < 11 || !input_pins || !num_outs) { |
| 1927 | usb_audio_err(state->chip, |
| 1928 | "invalid MIXER UNIT descriptor %d\n", |
| 1929 | unitid); |
| 1930 | return -EINVAL; |
| 1931 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | |
| 1934 | num_ins = 0; |
| 1935 | ich = 0; |
| 1936 | for (pin = 0; pin < input_pins; pin++) { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1937 | err = parse_audio_unit(state, desc->baSourceID[pin]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | if (err < 0) |
Mark Hills | 284a8dd | 2012-04-14 17:19:23 +0100 | [diff] [blame] | 1939 | continue; |
Clemens Ladisch | ea114fc | 2015-06-03 11:36:51 +0200 | [diff] [blame] | 1940 | /* no bmControls field (e.g. Maya44) -> ignore */ |
| 1941 | if (desc->bLength <= 10 + input_pins) |
| 1942 | continue; |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1943 | err = check_input_term(state, desc->baSourceID[pin], &iterm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | if (err < 0) |
| 1945 | return err; |
| 1946 | num_ins += iterm.channels; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1947 | for (; ich < num_ins; ich++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | int och, ich_has_controls = 0; |
| 1949 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1950 | for (och = 0; och < num_outs; och++) { |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 1951 | __u8 *c = NULL; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1952 | |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 1953 | if (state->mixer->protocol == UAC_VERSION_3) |
| 1954 | c = |
| 1955 | &(badd_baiof_mu_desc.bmMixerControls); |
| 1956 | else |
| 1957 | c = uac_mixer_unit_bmControls(desc, |
| 1958 | state->mixer->protocol); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1959 | if (check_matrix_bitmap(c, ich, och, num_outs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | ich_has_controls = 1; |
| 1961 | break; |
| 1962 | } |
| 1963 | } |
| 1964 | if (ich_has_controls) |
| 1965 | build_mixer_unit_ctl(state, desc, pin, ich, |
| 1966 | unitid, &iterm); |
| 1967 | } |
| 1968 | } |
| 1969 | return 0; |
| 1970 | } |
| 1971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | /* |
| 1973 | * Processing Unit / Extension Unit |
| 1974 | */ |
| 1975 | |
| 1976 | /* get callback for processing/extension unit */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1977 | static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, |
| 1978 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1980 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | int err, val; |
| 1982 | |
| 1983 | err = get_cur_ctl_value(cval, cval->control << 8, &val); |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 1984 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | ucontrol->value.integer.value[0] = cval->min; |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 1986 | return filter_error(cval, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | val = get_relative_value(cval, val); |
| 1989 | ucontrol->value.integer.value[0] = val; |
| 1990 | return 0; |
| 1991 | } |
| 1992 | |
| 1993 | /* put callback for processing/extension unit */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 1994 | static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, |
| 1995 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1997 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | int val, oval, err; |
| 1999 | |
| 2000 | err = get_cur_ctl_value(cval, cval->control << 8, &oval); |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 2001 | if (err < 0) |
| 2002 | return filter_error(cval, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | val = ucontrol->value.integer.value[0]; |
| 2004 | val = get_abs_value(cval, val); |
| 2005 | if (val != oval) { |
| 2006 | set_cur_ctl_value(cval, cval->control << 8, val); |
| 2007 | return 1; |
| 2008 | } |
| 2009 | return 0; |
| 2010 | } |
| 2011 | |
| 2012 | /* alsa control interface for processing/extension unit */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2013 | static struct snd_kcontrol_new mixer_procunit_ctl = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2015 | .name = "", /* will be filled later */ |
| 2016 | .info = mixer_ctl_feature_info, |
| 2017 | .get = mixer_ctl_procunit_get, |
| 2018 | .put = mixer_ctl_procunit_put, |
| 2019 | }; |
| 2020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | /* |
| 2022 | * predefined data for processing units |
| 2023 | */ |
| 2024 | struct procunit_value_info { |
| 2025 | int control; |
| 2026 | char *suffix; |
| 2027 | int val_type; |
| 2028 | int min_value; |
| 2029 | }; |
| 2030 | |
| 2031 | struct procunit_info { |
| 2032 | int type; |
| 2033 | char *name; |
| 2034 | struct procunit_value_info *values; |
| 2035 | }; |
| 2036 | |
| 2037 | static struct procunit_value_info updown_proc_info[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2038 | { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
| 2039 | { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | { 0 } |
| 2041 | }; |
| 2042 | static struct procunit_value_info prologic_proc_info[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2043 | { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
| 2044 | { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | { 0 } |
| 2046 | }; |
| 2047 | static struct procunit_value_info threed_enh_proc_info[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2048 | { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
| 2049 | { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | { 0 } |
| 2051 | }; |
| 2052 | static struct procunit_value_info reverb_proc_info[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2053 | { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
| 2054 | { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 }, |
| 2055 | { UAC_REVERB_TIME, "Time", USB_MIXER_U16 }, |
| 2056 | { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | { 0 } |
| 2058 | }; |
| 2059 | static struct procunit_value_info chorus_proc_info[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2060 | { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
| 2061 | { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 }, |
| 2062 | { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 }, |
| 2063 | { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | { 0 } |
| 2065 | }; |
| 2066 | static struct procunit_value_info dcr_proc_info[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2067 | { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN }, |
| 2068 | { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 }, |
| 2069 | { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 }, |
| 2070 | { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 }, |
| 2071 | { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 }, |
| 2072 | { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | { 0 } |
| 2074 | }; |
| 2075 | |
| 2076 | static struct procunit_info procunits[] = { |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2077 | { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info }, |
| 2078 | { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info }, |
| 2079 | { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info }, |
| 2080 | { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info }, |
| 2081 | { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info }, |
| 2082 | { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | { 0 }, |
| 2084 | }; |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame] | 2085 | /* |
| 2086 | * predefined data for extension units |
| 2087 | */ |
| 2088 | static struct procunit_value_info clock_rate_xu_info[] = { |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2089 | { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 }, |
| 2090 | { 0 } |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame] | 2091 | }; |
| 2092 | static struct procunit_value_info clock_source_xu_info[] = { |
| 2093 | { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN }, |
| 2094 | { 0 } |
| 2095 | }; |
| 2096 | static struct procunit_value_info spdif_format_xu_info[] = { |
| 2097 | { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN }, |
| 2098 | { 0 } |
| 2099 | }; |
| 2100 | static struct procunit_value_info soft_limit_xu_info[] = { |
| 2101 | { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN }, |
| 2102 | { 0 } |
| 2103 | }; |
| 2104 | static struct procunit_info extunits[] = { |
| 2105 | { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info }, |
| 2106 | { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info }, |
| 2107 | { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info }, |
| 2108 | { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info }, |
| 2109 | { 0 } |
| 2110 | }; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | /* |
| 2113 | * build a processing/extension unit |
| 2114 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2115 | static int build_audio_procunit(struct mixer_build *state, int unitid, |
| 2116 | void *raw_desc, struct procunit_info *list, |
| 2117 | char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2119 | struct uac_processing_unit_descriptor *desc = raw_desc; |
| 2120 | int num_ins = desc->bNrInPins; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2121 | struct usb_mixer_elem_info *cval; |
| 2122 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | int i, err, nameid, type, len; |
| 2124 | struct procunit_info *info; |
| 2125 | struct procunit_value_info *valinfo; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2126 | const struct usbmix_name_map *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | static struct procunit_value_info default_value_info[] = { |
| 2128 | { 0x01, "Switch", USB_MIXER_BOOLEAN }, |
| 2129 | { 0 } |
| 2130 | }; |
| 2131 | static struct procunit_info default_info = { |
| 2132 | 0, NULL, default_value_info |
| 2133 | }; |
| 2134 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2135 | if (desc->bLength < 13 || desc->bLength < 13 + num_ins || |
| 2136 | desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2137 | 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] | 2138 | return -EINVAL; |
| 2139 | } |
| 2140 | |
| 2141 | for (i = 0; i < num_ins; i++) { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2142 | if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | return err; |
| 2144 | } |
| 2145 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2146 | type = le16_to_cpu(desc->wProcessType); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | for (info = list; info && info->type; info++) |
| 2148 | if (info->type == type) |
| 2149 | break; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2150 | if (!info || !info->type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | info = &default_info; |
| 2152 | |
| 2153 | for (valinfo = info->values; valinfo->control; valinfo++) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2154 | __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol); |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2155 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2156 | if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | continue; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2158 | map = find_map(state, unitid, valinfo->control); |
| 2159 | if (check_ignored_ctl(map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | continue; |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 2161 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
Daniel Mack | a860d95 | 2014-05-24 10:58:17 +0200 | [diff] [blame] | 2162 | if (!cval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | return -ENOMEM; |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2164 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | cval->control = valinfo->control; |
| 2166 | cval->val_type = valinfo->val_type; |
| 2167 | cval->channels = 1; |
| 2168 | |
| 2169 | /* get min/max values */ |
Daniel Mack | 65f25da | 2010-05-31 13:35:41 +0200 | [diff] [blame] | 2170 | if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2171 | __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | /* FIXME: hard-coded */ |
| 2173 | cval->min = 1; |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2174 | cval->max = control_spec[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | cval->res = 1; |
| 2176 | cval->initialized = 1; |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame] | 2177 | } else { |
| 2178 | if (type == USB_XU_CLOCK_RATE) { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2179 | /* |
| 2180 | * E-Mu USB 0404/0202/TrackerPre/0204 |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame] | 2181 | * samplerate control quirk |
| 2182 | */ |
| 2183 | cval->min = 0; |
| 2184 | cval->max = 5; |
| 2185 | cval->res = 1; |
| 2186 | cval->initialized = 1; |
| 2187 | } else |
| 2188 | get_min_max(cval, valinfo->min_value); |
| 2189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | |
| 2191 | kctl = snd_ctl_new1(&mixer_procunit_ctl, cval); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2192 | if (!kctl) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | kfree(cval); |
| 2194 | return -ENOMEM; |
| 2195 | } |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 2196 | kctl->private_free = snd_usb_mixer_elem_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2198 | if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) { |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2199 | /* nothing */ ; |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2200 | } else if (info->name) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name)); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2202 | } else { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2203 | nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | len = 0; |
| 2205 | if (nameid) |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2206 | len = snd_usb_copy_string_desc(state, nameid, |
| 2207 | kctl->id.name, |
| 2208 | sizeof(kctl->id.name)); |
| 2209 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | strlcpy(kctl->id.name, name, sizeof(kctl->id.name)); |
| 2211 | } |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 2212 | append_ctl_name(kctl, " "); |
| 2213 | append_ctl_name(kctl, valinfo->suffix); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2215 | usb_audio_dbg(state->chip, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2216 | "[%d] PU [%s] ch = %d, val = %d/%d\n", |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2217 | cval->head.id, kctl->id.name, cval->channels, |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2218 | cval->min, cval->max); |
| 2219 | |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2220 | err = snd_usb_mixer_add_control(&cval->head, kctl); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2221 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | return err; |
| 2223 | } |
| 2224 | return 0; |
| 2225 | } |
| 2226 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2227 | static int parse_audio_processing_unit(struct mixer_build *state, int unitid, |
| 2228 | void *raw_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2230 | return build_audio_procunit(state, unitid, raw_desc, |
| 2231 | procunits, "Processing Unit"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2234 | static int parse_audio_extension_unit(struct mixer_build *state, int unitid, |
| 2235 | void *raw_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | { |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2237 | /* |
| 2238 | * Note that we parse extension units with processing unit descriptors. |
| 2239 | * That's ok as the layout is the same. |
| 2240 | */ |
| 2241 | return build_audio_procunit(state, unitid, raw_desc, |
| 2242 | extunits, "Extension Unit"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | /* |
| 2246 | * Selector Unit |
| 2247 | */ |
| 2248 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2249 | /* |
| 2250 | * info callback for selector unit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | * use an enumerator type for routing |
| 2252 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2253 | static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, |
| 2254 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2256 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Clemens Ladisch | 2a1803a | 2011-01-10 16:30:13 +0100 | [diff] [blame] | 2257 | const char **itemlist = (const char **)kcontrol->private_value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 2259 | if (snd_BUG_ON(!itemlist)) |
| 2260 | return -EINVAL; |
Clemens Ladisch | 2a1803a | 2011-01-10 16:30:13 +0100 | [diff] [blame] | 2261 | return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | /* get callback for selector unit */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2265 | static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, |
| 2266 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2268 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | int val, err; |
| 2270 | |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2271 | err = get_cur_ctl_value(cval, cval->control << 8, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | if (err < 0) { |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 2273 | ucontrol->value.enumerated.item[0] = 0; |
| 2274 | return filter_error(cval, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | } |
| 2276 | val = get_relative_value(cval, val); |
| 2277 | ucontrol->value.enumerated.item[0] = val; |
| 2278 | return 0; |
| 2279 | } |
| 2280 | |
| 2281 | /* put callback for selector unit */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2282 | static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, |
| 2283 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2285 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | int val, oval, err; |
| 2287 | |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2288 | err = get_cur_ctl_value(cval, cval->control << 8, &oval); |
Takashi Iwai | 5aeee34 | 2014-11-18 11:02:14 +0100 | [diff] [blame] | 2289 | if (err < 0) |
| 2290 | return filter_error(cval, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | val = ucontrol->value.enumerated.item[0]; |
| 2292 | val = get_abs_value(cval, val); |
| 2293 | if (val != oval) { |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2294 | set_cur_ctl_value(cval, cval->control << 8, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | return 1; |
| 2296 | } |
| 2297 | return 0; |
| 2298 | } |
| 2299 | |
| 2300 | /* alsa control interface for selector unit */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2301 | static struct snd_kcontrol_new mixer_selectunit_ctl = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2303 | .name = "", /* will be filled later */ |
| 2304 | .info = mixer_ctl_selector_info, |
| 2305 | .get = mixer_ctl_selector_get, |
| 2306 | .put = mixer_ctl_selector_put, |
| 2307 | }; |
| 2308 | |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2309 | /* |
| 2310 | * private free callback. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | * free both private_data and private_value |
| 2312 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2313 | static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | { |
| 2315 | int i, num_ins = 0; |
| 2316 | |
| 2317 | if (kctl->private_data) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2318 | struct usb_mixer_elem_info *cval = kctl->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | num_ins = cval->max; |
| 2320 | kfree(cval); |
| 2321 | kctl->private_data = NULL; |
| 2322 | } |
| 2323 | if (kctl->private_value) { |
| 2324 | char **itemlist = (char **)kctl->private_value; |
| 2325 | for (i = 0; i < num_ins; i++) |
| 2326 | kfree(itemlist[i]); |
| 2327 | kfree(itemlist); |
| 2328 | kctl->private_value = 0; |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | /* |
| 2333 | * parse a selector unit |
| 2334 | */ |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2335 | static int parse_audio_selector_unit(struct mixer_build *state, int unitid, |
| 2336 | void *raw_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2338 | struct uac_selector_unit_descriptor *desc = raw_desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | unsigned int i, nameid, len; |
| 2340 | int err; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2341 | struct usb_mixer_elem_info *cval; |
| 2342 | struct snd_kcontrol *kctl; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2343 | const struct usbmix_name_map *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | char **namelist; |
| 2345 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2346 | if (!desc->bNrInPins || desc->bLength < 5 + desc->bNrInPins) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2347 | usb_audio_err(state->chip, |
| 2348 | "invalid SELECTOR UNIT descriptor %d\n", unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | return -EINVAL; |
| 2350 | } |
| 2351 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2352 | for (i = 0; i < desc->bNrInPins; i++) { |
| 2353 | if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | return err; |
| 2355 | } |
| 2356 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2357 | if (desc->bNrInPins == 1) /* only one ? nonsense! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | return 0; |
| 2359 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2360 | map = find_map(state, unitid, 0); |
| 2361 | if (check_ignored_ctl(map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | return 0; |
| 2363 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 2364 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
Daniel Mack | a860d95 | 2014-05-24 10:58:17 +0200 | [diff] [blame] | 2365 | if (!cval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | return -ENOMEM; |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2367 | snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | cval->val_type = USB_MIXER_U8; |
| 2369 | cval->channels = 1; |
| 2370 | cval->min = 1; |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2371 | cval->max = desc->bNrInPins; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | cval->res = 1; |
| 2373 | cval->initialized = 1; |
| 2374 | |
Johan Rastén | a358a0e | 2014-11-17 08:39:33 +0100 | [diff] [blame] | 2375 | if (state->mixer->protocol == UAC_VERSION_1) |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2376 | cval->control = 0; |
Johan Rastén | a358a0e | 2014-11-17 08:39:33 +0100 | [diff] [blame] | 2377 | else /* UAC_VERSION_2 */ |
| 2378 | cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ? |
| 2379 | UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR; |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2380 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2381 | namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2382 | if (!namelist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | kfree(cval); |
| 2384 | return -ENOMEM; |
| 2385 | } |
| 2386 | #define MAX_ITEM_NAME_LEN 64 |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2387 | for (i = 0; i < desc->bNrInPins; i++) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2388 | struct usb_audio_term iterm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | len = 0; |
| 2390 | namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2391 | if (!namelist[i]) { |
Mariusz Kozlowski | 7fbe3ca | 2007-01-08 11:25:30 +0100 | [diff] [blame] | 2392 | while (i--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | kfree(namelist[i]); |
| 2394 | kfree(namelist); |
| 2395 | kfree(cval); |
| 2396 | return -ENOMEM; |
| 2397 | } |
Clemens Ladisch | 8e062ec | 2005-04-22 15:49:52 +0200 | [diff] [blame] | 2398 | len = check_mapped_selector_name(state, unitid, i, namelist[i], |
| 2399 | MAX_ITEM_NAME_LEN); |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2400 | if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0); |
| 2402 | if (! len) |
Masanari Iida | af831ee | 2014-04-28 13:08:55 +0900 | [diff] [blame] | 2403 | sprintf(namelist[i], "Input %u", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval); |
| 2407 | if (! kctl) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2408 | usb_audio_err(state->chip, "cannot malloc kcontrol\n"); |
Jesper Juhl | 878b478 | 2006-03-20 11:27:13 +0100 | [diff] [blame] | 2409 | kfree(namelist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | kfree(cval); |
| 2411 | return -ENOMEM; |
| 2412 | } |
| 2413 | kctl->private_value = (unsigned long)namelist; |
| 2414 | kctl->private_free = usb_mixer_selector_elem_free; |
| 2415 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 2416 | nameid = uac_selector_unit_iSelector(desc); |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 2417 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | if (len) |
| 2419 | ; |
| 2420 | else if (nameid) |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2421 | snd_usb_copy_string_desc(state, nameid, kctl->id.name, |
| 2422 | sizeof(kctl->id.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | else { |
| 2424 | len = get_term_name(state, &state->oterm, |
| 2425 | kctl->id.name, sizeof(kctl->id.name), 0); |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2426 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name)); |
| 2428 | |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2429 | if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) |
| 2430 | append_ctl_name(kctl, " Clock Source"); |
| 2431 | else if ((state->oterm.type & 0xff00) == 0x0100) |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 2432 | append_ctl_name(kctl, " Capture Source"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | else |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 2434 | append_ctl_name(kctl, " Playback Source"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2437 | usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n", |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2438 | cval->head.id, kctl->id.name, desc->bNrInPins); |
| 2439 | return snd_usb_mixer_add_control(&cval->head, kctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | } |
| 2441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | /* |
| 2443 | * parse an audio unit recursively |
| 2444 | */ |
| 2445 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2446 | static int parse_audio_unit(struct mixer_build *state, int unitid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | { |
| 2448 | unsigned char *p1; |
| 2449 | |
| 2450 | if (test_and_set_bit(unitid, state->unitbitmap)) |
| 2451 | return 0; /* the unit already visited */ |
| 2452 | |
| 2453 | p1 = find_audio_control_unit(state, unitid); |
| 2454 | if (!p1) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2455 | usb_audio_err(state->chip, "unit %d not found!\n", unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | return -EINVAL; |
| 2457 | } |
| 2458 | |
| 2459 | switch (p1[2]) { |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 2460 | case UAC_INPUT_TERMINAL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | return 0; /* NOP */ |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 2462 | case UAC_MIXER_UNIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | return parse_audio_mixer_unit(state, unitid, p1); |
Daniel Mack | cddaafb | 2016-04-09 11:21:46 +0200 | [diff] [blame] | 2464 | case UAC2_CLOCK_SOURCE: |
| 2465 | return parse_clock_source_unit(state, unitid, p1); |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 2466 | case UAC_SELECTOR_UNIT: |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2467 | /* UAC3_MIXER_UNIT_V3 has the same value */ |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2468 | case UAC2_CLOCK_SELECTOR: |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2469 | /* UAC3_CLOCK_SOURCE has the same value */ |
| 2470 | if (state->mixer->protocol == UAC_VERSION_3 && |
| 2471 | p1[2] == UAC3_CLOCK_SOURCE) |
| 2472 | return 0; /* NOP */ |
| 2473 | else if (state->mixer->protocol == UAC_VERSION_3 |
| 2474 | && p1[2] == UAC3_MIXER_UNIT_V3) |
| 2475 | return parse_audio_mixer_unit(state, unitid, p1); |
| 2476 | else |
| 2477 | return parse_audio_selector_unit(state, unitid, p1); |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 2478 | case UAC_FEATURE_UNIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | return parse_audio_feature_unit(state, unitid, p1); |
Daniel Mack | 69da9bc | 2010-06-16 17:57:28 +0200 | [diff] [blame] | 2480 | case UAC1_PROCESSING_UNIT: |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2481 | /* UAC2_EFFECT_UNIT has the same value */ |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2482 | /* UAC3_FEATURE_UNIT_V3 has the same value */ |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2483 | if (state->mixer->protocol == UAC_VERSION_1) |
| 2484 | return parse_audio_processing_unit(state, unitid, p1); |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2485 | else if (state->mixer->protocol == UAC_VERSION_2) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2486 | return 0; /* FIXME - effect units not implemented yet */ |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2487 | else |
| 2488 | return parse_audio_feature_unit(state, unitid, p1); |
Daniel Mack | 69da9bc | 2010-06-16 17:57:28 +0200 | [diff] [blame] | 2489 | case UAC1_EXTENSION_UNIT: |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2490 | /* UAC2_PROCESSING_UNIT_V2 has the same value */ |
| 2491 | if (state->mixer->protocol == UAC_VERSION_1) |
| 2492 | return parse_audio_extension_unit(state, unitid, p1); |
| 2493 | else /* UAC_VERSION_2 */ |
| 2494 | return parse_audio_processing_unit(state, unitid, p1); |
Torstein Hegge | 61ac513 | 2013-03-19 17:12:14 +0100 | [diff] [blame] | 2495 | case UAC2_EXTENSION_UNIT_V2: |
| 2496 | return parse_audio_extension_unit(state, unitid, p1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | default: |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2498 | usb_audio_err(state->chip, |
| 2499 | "unit %u: unexpected type 0x%02x\n", unitid, p1[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | return -EINVAL; |
| 2501 | } |
| 2502 | } |
| 2503 | |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2504 | static void snd_usb_mixer_free(struct usb_mixer_interface *mixer) |
| 2505 | { |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2506 | kfree(mixer->id_elems); |
| 2507 | if (mixer->urb) { |
| 2508 | kfree(mixer->urb->transfer_buffer); |
| 2509 | usb_free_urb(mixer->urb); |
| 2510 | } |
Mariusz Kozlowski | 68df9de | 2006-11-08 15:37:04 +0100 | [diff] [blame] | 2511 | usb_free_urb(mixer->rc_urb); |
Clemens Ladisch | b259b10 | 2005-04-29 16:29:28 +0200 | [diff] [blame] | 2512 | kfree(mixer->rc_setup_packet); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2513 | kfree(mixer); |
| 2514 | } |
| 2515 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2516 | static int snd_usb_mixer_dev_free(struct snd_device *device) |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2517 | { |
| 2518 | struct usb_mixer_interface *mixer = device->device_data; |
| 2519 | snd_usb_mixer_free(mixer); |
| 2520 | return 0; |
| 2521 | } |
| 2522 | |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2523 | static int make_out_term(struct mixer_build state, int wTerminalType) |
| 2524 | { |
| 2525 | struct uac3_output_terminal_descriptor *desc = NULL; |
| 2526 | |
| 2527 | if (wTerminalType == UAC_TERMINAL_STREAMING) |
| 2528 | desc = &badd_baif_out_term_desc; |
| 2529 | else { |
| 2530 | desc = &badd_baof_out_term_desc; |
| 2531 | desc->wTerminalType = wTerminalType; |
| 2532 | } |
| 2533 | set_bit(desc->bTerminalID, state.unitbitmap); |
| 2534 | state.oterm.id = desc->bTerminalID; |
| 2535 | state.oterm.type = desc->wTerminalType; |
| 2536 | state.oterm.name = desc->wTerminalDescrStr; |
| 2537 | return parse_audio_unit(&state, desc->bSourceID); |
| 2538 | } |
| 2539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | /* |
| 2541 | * create mixer controls |
| 2542 | * |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 2543 | * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | */ |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2545 | static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2547 | struct mixer_build state; |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2548 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | const struct usbmix_ctl_map *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | memset(&state, 0, sizeof(state)); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2552 | state.chip = mixer->chip; |
| 2553 | state.mixer = mixer; |
Daniel Mack | 1faa5d0 | 2011-08-04 15:56:28 +0200 | [diff] [blame] | 2554 | state.buffer = mixer->hostif->extra; |
| 2555 | state.buflen = mixer->hostif->extralen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | |
| 2557 | /* check the mapping table */ |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 2558 | for (map = usbmix_ctl_maps; map->id; map++) { |
| 2559 | if (map->id == state.chip->usb_id) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | state.map = map->map; |
Clemens Ladisch | 8e062ec | 2005-04-22 15:49:52 +0200 | [diff] [blame] | 2561 | state.selector_map = map->selector_map; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2562 | mixer->ignore_ctl_error = map->ignore_ctl_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | break; |
| 2564 | } |
| 2565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2567 | if (mixer->protocol == UAC_VERSION_3) { |
| 2568 | struct usb_interface *usb_iface = |
| 2569 | usb_ifnum_to_if(mixer->chip->dev, |
| 2570 | get_iface_desc(mixer->hostif)->bInterfaceNumber); |
| 2571 | struct usb_interface_assoc_descriptor *assoc = |
| 2572 | usb_iface->intf_assoc; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2573 | |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2574 | switch (assoc->bFunctionSubClass) { |
| 2575 | case PROF_GENERIC_IO: { |
| 2576 | if (assoc->bInterfaceCount == 0x02) { |
| 2577 | if (get_endpoint(mixer->hostif, |
| 2578 | 0)->bEndpointAddress | USB_DIR_IN) |
| 2579 | err = make_out_term(state, |
| 2580 | UAC_TERMINAL_STREAMING); |
| 2581 | else |
| 2582 | err = make_out_term(state, |
| 2583 | UAC_OUTPUT_TERMINAL_UNDEFINED); |
| 2584 | } else { |
| 2585 | err = make_out_term(state, |
| 2586 | UAC_OUTPUT_TERMINAL_UNDEFINED); |
| 2587 | if (err < 0 && err != -EINVAL) |
| 2588 | return err; |
| 2589 | err = make_out_term(state, |
| 2590 | UAC_TERMINAL_STREAMING); |
| 2591 | } |
| 2592 | break; |
| 2593 | } |
| 2594 | |
| 2595 | case PROF_HEADPHONE: |
| 2596 | err = make_out_term(state, |
| 2597 | UAC_OUTPUT_TERMINAL_HEADPHONES); |
| 2598 | break; |
| 2599 | case PROF_SPEAKER: |
| 2600 | err = make_out_term(state, UAC_OUTPUT_TERMINAL_SPEAKER); |
| 2601 | break; |
| 2602 | case PROF_MICROPHONE: |
| 2603 | err = make_out_term(state, UAC_TERMINAL_STREAMING); |
| 2604 | break; |
| 2605 | case PROF_HEADSET: |
| 2606 | case PROF_HEADSET_ADAPTER: |
| 2607 | err = make_out_term(state, UAC_BIDIR_TERMINAL_HEADSET); |
Daniel Mack | 83ea5d1 | 2013-03-19 21:09:25 +0100 | [diff] [blame] | 2608 | if (err < 0 && err != -EINVAL) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2609 | return err; |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2610 | err = make_out_term(state, UAC_TERMINAL_STREAMING); |
| 2611 | break; |
| 2612 | case PROF_SPEAKERPHONE: |
| 2613 | err = make_out_term(state, |
| 2614 | UAC_BIDIR_TERMINAL_SPEAKERPHONE); |
Daniel Mack | 83ea5d1 | 2013-03-19 21:09:25 +0100 | [diff] [blame] | 2615 | if (err < 0 && err != -EINVAL) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2616 | return err; |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2617 | err = make_out_term(state, UAC_TERMINAL_STREAMING); |
| 2618 | break; |
| 2619 | } |
| 2620 | if (err < 0 && err != -EINVAL) |
| 2621 | return err; |
| 2622 | } else { |
| 2623 | void *p; |
Daniel Mack | 0941420 | 2010-05-31 13:35:44 +0200 | [diff] [blame] | 2624 | |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2625 | p = NULL; |
| 2626 | while ((p = snd_usb_find_csint_desc(mixer->hostif->extra, |
| 2627 | mixer->hostif->extralen, p, |
| 2628 | UAC_OUTPUT_TERMINAL)) != NULL) { |
| 2629 | if (mixer->protocol == UAC_VERSION_1) { |
| 2630 | struct uac1_output_terminal_descriptor *desc = |
| 2631 | p; |
| 2632 | |
| 2633 | if (desc->bLength < sizeof(*desc)) |
| 2634 | continue; /* invalid descriptor? */ |
| 2635 | /* mark terminal ID as visited */ |
| 2636 | set_bit(desc->bTerminalID, state.unitbitmap); |
| 2637 | state.oterm.id = desc->bTerminalID; |
| 2638 | state.oterm.type = |
| 2639 | le16_to_cpu(desc->wTerminalType); |
| 2640 | state.oterm.name = desc->iTerminal; |
| 2641 | err = parse_audio_unit(&state, desc->bSourceID); |
| 2642 | if (err < 0 && err != -EINVAL) |
| 2643 | return err; |
| 2644 | } else { /* UAC_VERSION_2 */ |
| 2645 | struct uac2_output_terminal_descriptor *desc = |
| 2646 | p; |
| 2647 | |
| 2648 | if (desc->bLength < sizeof(*desc)) |
| 2649 | continue; /* invalid descriptor? */ |
| 2650 | /* mark terminal ID as visited */ |
| 2651 | set_bit(desc->bTerminalID, state.unitbitmap); |
| 2652 | state.oterm.id = desc->bTerminalID; |
| 2653 | state.oterm.type = |
| 2654 | le16_to_cpu(desc->wTerminalType); |
| 2655 | state.oterm.name = desc->iTerminal; |
| 2656 | err = parse_audio_unit(&state, desc->bSourceID); |
| 2657 | if (err < 0 && err != -EINVAL) |
| 2658 | return err; |
| 2659 | |
| 2660 | /* |
| 2661 | * For UAC2, use the same approach to also add |
| 2662 | * the clock selectors |
| 2663 | */ |
| 2664 | err = parse_audio_unit(&state, |
| 2665 | desc->bCSourceID); |
| 2666 | if (err < 0 && err != -EINVAL) |
| 2667 | return err; |
| 2668 | } |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | } |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | return 0; |
| 2673 | } |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2674 | |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 2675 | 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] | 2676 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2677 | struct usb_mixer_elem_list *list; |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2678 | |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2679 | for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2680 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2681 | &list->kctl->id); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2682 | } |
| 2683 | |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 2684 | static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer, |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2685 | struct usb_mixer_elem_list *list) |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 2686 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2687 | struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list; |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 2688 | static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN", |
| 2689 | "S8", "U8", "S16", "U16"}; |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 2690 | snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, " |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2691 | "channels=%i, type=\"%s\"\n", cval->head.id, |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 2692 | cval->control, cval->cmask, cval->channels, |
| 2693 | val_types[cval->val_type]); |
| 2694 | snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n", |
| 2695 | cval->min, cval->max, cval->dBmin, cval->dBmax); |
| 2696 | } |
| 2697 | |
| 2698 | static void snd_usb_mixer_proc_read(struct snd_info_entry *entry, |
| 2699 | struct snd_info_buffer *buffer) |
| 2700 | { |
| 2701 | struct snd_usb_audio *chip = entry->private_data; |
| 2702 | struct usb_mixer_interface *mixer; |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2703 | struct usb_mixer_elem_list *list; |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 2704 | int unitid; |
| 2705 | |
| 2706 | list_for_each_entry(mixer, &chip->mixer_list, list) { |
| 2707 | snd_iprintf(buffer, |
Jaroslav Kysela | 7affdc1 | 2010-02-16 11:52:27 +0100 | [diff] [blame] | 2708 | "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n", |
Daniel Mack | 3d8d4dc | 2010-06-16 17:57:31 +0200 | [diff] [blame] | 2709 | chip->usb_id, snd_usb_ctrl_intf(chip), |
Jaroslav Kysela | 7affdc1 | 2010-02-16 11:52:27 +0100 | [diff] [blame] | 2710 | mixer->ignore_ctl_error); |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 2711 | snd_iprintf(buffer, "Card: %s\n", chip->card->longname); |
| 2712 | for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2713 | for (list = mixer->id_elems[unitid]; list; |
| 2714 | list = list->next_id_elem) { |
| 2715 | snd_iprintf(buffer, " Unit: %i\n", list->id); |
| 2716 | if (list->kctl) |
| 2717 | snd_iprintf(buffer, |
| 2718 | " Control: name=\"%s\", index=%i\n", |
| 2719 | list->kctl->id.name, |
| 2720 | list->kctl->id.index); |
| 2721 | if (list->dump) |
| 2722 | list->dump(buffer, list); |
| 2723 | } |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 2724 | } |
| 2725 | } |
| 2726 | } |
| 2727 | |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2728 | static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer, |
| 2729 | int attribute, int value, int index) |
| 2730 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2731 | struct usb_mixer_elem_list *list; |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2732 | __u8 unitid = (index >> 8) & 0xff; |
| 2733 | __u8 control = (value >> 8) & 0xff; |
| 2734 | __u8 channel = value & 0xff; |
Daniel Mack | 191227d | 2016-04-08 19:52:02 +0200 | [diff] [blame] | 2735 | unsigned int count = 0; |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2736 | |
| 2737 | if (channel >= MAX_CHANNELS) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2738 | usb_audio_dbg(mixer->chip, |
| 2739 | "%s(): bogus channel number %d\n", |
| 2740 | __func__, channel); |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2741 | return; |
| 2742 | } |
| 2743 | |
Daniel Mack | 191227d | 2016-04-08 19:52:02 +0200 | [diff] [blame] | 2744 | for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) |
| 2745 | count++; |
| 2746 | |
| 2747 | if (count == 0) |
| 2748 | return; |
| 2749 | |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2750 | for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) { |
| 2751 | struct usb_mixer_elem_info *info; |
| 2752 | |
| 2753 | if (!list->kctl) |
| 2754 | continue; |
| 2755 | |
| 2756 | info = (struct usb_mixer_elem_info *)list; |
Daniel Mack | 191227d | 2016-04-08 19:52:02 +0200 | [diff] [blame] | 2757 | if (count > 1 && info->control != control) |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2758 | continue; |
| 2759 | |
| 2760 | switch (attribute) { |
| 2761 | case UAC2_CS_CUR: |
| 2762 | /* invalidate cache, so the value is read from the device */ |
| 2763 | if (channel) |
| 2764 | info->cached &= ~(1 << channel); |
| 2765 | else /* master channel */ |
| 2766 | info->cached = 0; |
| 2767 | |
| 2768 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2769 | &info->head.kctl->id); |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2770 | break; |
| 2771 | |
| 2772 | case UAC2_CS_RANGE: |
| 2773 | /* TODO */ |
| 2774 | break; |
| 2775 | |
| 2776 | case UAC2_CS_MEM: |
| 2777 | /* TODO */ |
| 2778 | break; |
| 2779 | |
| 2780 | default: |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2781 | usb_audio_dbg(mixer->chip, |
| 2782 | "unknown attribute %d in interrupt\n", |
| 2783 | attribute); |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2784 | break; |
| 2785 | } /* switch */ |
| 2786 | } |
| 2787 | } |
| 2788 | |
| 2789 | static void snd_usb_mixer_interrupt(struct urb *urb) |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2790 | { |
| 2791 | struct usb_mixer_interface *mixer = urb->context; |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2792 | int len = urb->actual_length; |
Oliver Neukum | edf7de3 | 2011-03-11 13:19:43 +0100 | [diff] [blame] | 2793 | int ustatus = urb->status; |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2794 | |
Oliver Neukum | edf7de3 | 2011-03-11 13:19:43 +0100 | [diff] [blame] | 2795 | if (ustatus != 0) |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2796 | goto requeue; |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2797 | |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2798 | if (mixer->protocol == UAC_VERSION_1) { |
| 2799 | struct uac1_status_word *status; |
| 2800 | |
| 2801 | for (status = urb->transfer_buffer; |
| 2802 | len >= sizeof(*status); |
| 2803 | len -= sizeof(*status), status++) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 2804 | dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n", |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2805 | status->bStatusType, |
| 2806 | status->bOriginator); |
| 2807 | |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2808 | /* ignore any notifications not from the control interface */ |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2809 | if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) != |
| 2810 | UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF) |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2811 | continue; |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2812 | |
| 2813 | if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED) |
| 2814 | snd_usb_mixer_rc_memory_change(mixer, status->bOriginator); |
Clemens Ladisch | b259b10 | 2005-04-29 16:29:28 +0200 | [diff] [blame] | 2815 | else |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2816 | snd_usb_mixer_notify_id(mixer, status->bOriginator); |
| 2817 | } |
| 2818 | } else { /* UAC_VERSION_2 */ |
| 2819 | struct uac2_interrupt_data_msg *msg; |
| 2820 | |
| 2821 | for (msg = urb->transfer_buffer; |
| 2822 | len >= sizeof(*msg); |
| 2823 | len -= sizeof(*msg), msg++) { |
| 2824 | /* drop vendor specific and endpoint requests */ |
| 2825 | if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) || |
| 2826 | (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP)) |
| 2827 | continue; |
| 2828 | |
| 2829 | snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute, |
| 2830 | le16_to_cpu(msg->wValue), |
| 2831 | le16_to_cpu(msg->wIndex)); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2832 | } |
| 2833 | } |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2834 | |
| 2835 | requeue: |
Daniel Mack | 6bc170e | 2014-05-24 10:58:16 +0200 | [diff] [blame] | 2836 | if (ustatus != -ENOENT && |
| 2837 | ustatus != -ECONNRESET && |
| 2838 | ustatus != -ESHUTDOWN) { |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2839 | urb->dev = mixer->chip->dev; |
| 2840 | usb_submit_urb(urb, GFP_ATOMIC); |
| 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | /* create the handler for the optional status interrupt endpoint */ |
| 2845 | static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer) |
| 2846 | { |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2847 | struct usb_endpoint_descriptor *ep; |
| 2848 | void *transfer_buffer; |
| 2849 | int buffer_length; |
| 2850 | unsigned int epnum; |
| 2851 | |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2852 | /* we need one interrupt input endpoint */ |
Daniel Mack | 1faa5d0 | 2011-08-04 15:56:28 +0200 | [diff] [blame] | 2853 | if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1) |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2854 | return 0; |
Daniel Mack | 1faa5d0 | 2011-08-04 15:56:28 +0200 | [diff] [blame] | 2855 | ep = get_endpoint(mixer->hostif, 0); |
Julia Lawall | 913ae5a | 2009-01-03 17:54:53 +0100 | [diff] [blame] | 2856 | if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep)) |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2857 | return 0; |
| 2858 | |
Julia Lawall | 42a6e66 | 2008-12-29 11:23:02 +0100 | [diff] [blame] | 2859 | epnum = usb_endpoint_num(ep); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2860 | buffer_length = le16_to_cpu(ep->wMaxPacketSize); |
| 2861 | transfer_buffer = kmalloc(buffer_length, GFP_KERNEL); |
| 2862 | if (!transfer_buffer) |
| 2863 | return -ENOMEM; |
| 2864 | mixer->urb = usb_alloc_urb(0, GFP_KERNEL); |
| 2865 | if (!mixer->urb) { |
| 2866 | kfree(transfer_buffer); |
| 2867 | return -ENOMEM; |
| 2868 | } |
| 2869 | usb_fill_int_urb(mixer->urb, mixer->chip->dev, |
| 2870 | usb_rcvintpipe(mixer->chip->dev, epnum), |
| 2871 | transfer_buffer, buffer_length, |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2872 | snd_usb_mixer_interrupt, mixer, ep->bInterval); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2873 | usb_submit_urb(mixer->urb, GFP_KERNEL); |
| 2874 | return 0; |
| 2875 | } |
| 2876 | |
Takashi Iwai | 7a9b806 | 2008-08-13 15:40:53 +0200 | [diff] [blame] | 2877 | int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif, |
| 2878 | int ignore_error) |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2879 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2880 | static struct snd_device_ops dev_ops = { |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2881 | .dev_free = snd_usb_mixer_dev_free |
| 2882 | }; |
| 2883 | struct usb_mixer_interface *mixer; |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 2884 | struct snd_info_entry *entry; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2885 | int err; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2886 | |
| 2887 | strcpy(chip->card->mixername, "USB Mixer"); |
| 2888 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 2889 | mixer = kzalloc(sizeof(*mixer), GFP_KERNEL); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2890 | if (!mixer) |
| 2891 | return -ENOMEM; |
| 2892 | mixer->chip = chip; |
Takashi Iwai | 7a9b806 | 2008-08-13 15:40:53 +0200 | [diff] [blame] | 2893 | mixer->ignore_ctl_error = ignore_error; |
Jaroslav Kysela | 291186e | 2010-02-16 11:55:18 +0100 | [diff] [blame] | 2894 | mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems), |
| 2895 | GFP_KERNEL); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2896 | if (!mixer->id_elems) { |
| 2897 | kfree(mixer); |
| 2898 | return -ENOMEM; |
| 2899 | } |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2900 | |
Daniel Mack | 1faa5d0 | 2011-08-04 15:56:28 +0200 | [diff] [blame] | 2901 | mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0]; |
| 2902 | switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) { |
Clemens Ladisch | a2acad8 | 2010-09-03 10:53:11 +0200 | [diff] [blame] | 2903 | case UAC_VERSION_1: |
| 2904 | default: |
| 2905 | mixer->protocol = UAC_VERSION_1; |
| 2906 | break; |
| 2907 | case UAC_VERSION_2: |
| 2908 | mixer->protocol = UAC_VERSION_2; |
| 2909 | break; |
Ajay Agarwal | 045e374 | 2017-02-07 18:41:34 +0530 | [diff] [blame] | 2910 | case UAC_VERSION_3: |
| 2911 | mixer->protocol = UAC_VERSION_3; |
| 2912 | break; |
Clemens Ladisch | a2acad8 | 2010-09-03 10:53:11 +0200 | [diff] [blame] | 2913 | } |
Daniel Mack | 7b8a043 | 2010-02-22 23:49:12 +0100 | [diff] [blame] | 2914 | |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2915 | if ((err = snd_usb_mixer_controls(mixer)) < 0 || |
Clemens Ladisch | 93446ed | 2005-05-03 08:02:40 +0200 | [diff] [blame] | 2916 | (err = snd_usb_mixer_status_create(mixer)) < 0) |
| 2917 | goto _error; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2918 | |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 2919 | snd_usb_mixer_apply_create_quirk(mixer); |
Clemens Ladisch | 468b8fd | 2009-07-13 11:39:29 +0200 | [diff] [blame] | 2920 | |
Takashi Iwai | 9cbb280 | 2014-02-04 11:15:31 +0100 | [diff] [blame] | 2921 | err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops); |
Clemens Ladisch | 93446ed | 2005-05-03 08:02:40 +0200 | [diff] [blame] | 2922 | if (err < 0) |
| 2923 | goto _error; |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 2924 | |
| 2925 | if (list_empty(&chip->mixer_list) && |
| 2926 | !snd_card_proc_new(chip->card, "usbmixer", &entry)) |
| 2927 | snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read); |
| 2928 | |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2929 | list_add(&mixer->list, &chip->mixer_list); |
| 2930 | return 0; |
Clemens Ladisch | 93446ed | 2005-05-03 08:02:40 +0200 | [diff] [blame] | 2931 | |
| 2932 | _error: |
| 2933 | snd_usb_mixer_free(mixer); |
| 2934 | return err; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2935 | } |
| 2936 | |
Takashi Iwai | a6cece9 | 2014-10-31 11:24:32 +0100 | [diff] [blame] | 2937 | void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer) |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2938 | { |
Mariusz Kozlowski | 68df9de | 2006-11-08 15:37:04 +0100 | [diff] [blame] | 2939 | usb_kill_urb(mixer->urb); |
| 2940 | usb_kill_urb(mixer->rc_urb); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2941 | } |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 2942 | |
| 2943 | #ifdef CONFIG_PM |
| 2944 | /* stop any bus activity of a mixer */ |
| 2945 | static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer) |
| 2946 | { |
| 2947 | usb_kill_urb(mixer->urb); |
| 2948 | usb_kill_urb(mixer->rc_urb); |
| 2949 | } |
| 2950 | |
| 2951 | static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer) |
| 2952 | { |
| 2953 | int err; |
| 2954 | |
| 2955 | if (mixer->urb) { |
| 2956 | err = usb_submit_urb(mixer->urb, GFP_NOIO); |
| 2957 | if (err < 0) |
| 2958 | return err; |
| 2959 | } |
| 2960 | |
| 2961 | return 0; |
| 2962 | } |
| 2963 | |
| 2964 | int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer) |
| 2965 | { |
| 2966 | snd_usb_mixer_inactivate(mixer); |
| 2967 | return 0; |
| 2968 | } |
| 2969 | |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2970 | static int restore_mixer_value(struct usb_mixer_elem_list *list) |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 2971 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 2972 | struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list; |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 2973 | int c, err, idx; |
| 2974 | |
| 2975 | if (cval->cmask) { |
| 2976 | idx = 0; |
| 2977 | for (c = 0; c < MAX_CHANNELS; c++) { |
| 2978 | if (!(cval->cmask & (1 << c))) |
| 2979 | continue; |
Yao-Wen Mao | 6aa6925 | 2015-08-28 16:33:25 +0800 | [diff] [blame] | 2980 | if (cval->cached & (1 << (c + 1))) { |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 2981 | err = snd_usb_set_cur_mix_value(cval, c + 1, idx, |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 2982 | cval->cache_val[idx]); |
| 2983 | if (err < 0) |
| 2984 | return err; |
| 2985 | } |
| 2986 | idx++; |
| 2987 | } |
| 2988 | } else { |
| 2989 | /* master */ |
| 2990 | if (cval->cached) { |
Chris J Arges | eef9045 | 2014-11-12 12:07:01 -0600 | [diff] [blame] | 2991 | 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] | 2992 | if (err < 0) |
| 2993 | return err; |
| 2994 | } |
| 2995 | } |
| 2996 | |
| 2997 | return 0; |
| 2998 | } |
| 2999 | |
| 3000 | int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume) |
| 3001 | { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3002 | struct usb_mixer_elem_list *list; |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 3003 | int id, err; |
| 3004 | |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 3005 | if (reset_resume) { |
| 3006 | /* restore cached mixer values */ |
| 3007 | for (id = 0; id < MAX_ID_ELEMS; id++) { |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3008 | for (list = mixer->id_elems[id]; list; |
| 3009 | list = list->next_id_elem) { |
| 3010 | if (list->resume) { |
| 3011 | err = list->resume(list); |
| 3012 | if (err < 0) |
| 3013 | return err; |
| 3014 | } |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | } |
| 3018 | |
| 3019 | return snd_usb_mixer_activate(mixer); |
| 3020 | } |
| 3021 | #endif |
Takashi Iwai | 3360b84 | 2014-11-18 11:47:04 +0100 | [diff] [blame] | 3022 | |
| 3023 | void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list, |
| 3024 | struct usb_mixer_interface *mixer, |
| 3025 | int unitid) |
| 3026 | { |
| 3027 | list->mixer = mixer; |
| 3028 | list->id = unitid; |
| 3029 | list->dump = snd_usb_mixer_dump_cval; |
| 3030 | #ifdef CONFIG_PM |
| 3031 | list->resume = restore_mixer_value; |
| 3032 | #endif |
| 3033 | } |