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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/bitops.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/list.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/usb.h> |
Daniel Mack | 28e1b77 | 2010-02-22 23:49:09 +0100 | [diff] [blame] | 35 | #include <linux/usb/audio.h> |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 36 | #include <linux/usb/audio-v2.h> |
Daniel Mack | 28e1b77 | 2010-02-22 23:49:09 +0100 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <sound/core.h> |
| 39 | #include <sound/control.h> |
Clemens Ladisch | b259b10 | 2005-04-29 16:29:28 +0200 | [diff] [blame] | 40 | #include <sound/hwdep.h> |
Clemens Ladisch | aafad56 | 2005-05-10 14:47:38 +0200 | [diff] [blame] | 41 | #include <sound/info.h> |
Takashi Iwai | 7bc5ba7e | 2006-07-14 15:18:19 +0200 | [diff] [blame] | 42 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include "usbaudio.h" |
Daniel Mack | f0b5e63 | 2010-03-11 21:13:23 +0100 | [diff] [blame] | 45 | #include "mixer.h" |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 46 | #include "helper.h" |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 47 | #include "mixer_quirks.h" |
Raimonds Cicans | 4d1a70d | 2006-05-05 09:49:53 +0200 | [diff] [blame] | 48 | |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 49 | #define MAX_ID_ELEMS 256 |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | struct usb_audio_term { |
| 52 | int id; |
| 53 | int type; |
| 54 | int channels; |
| 55 | unsigned int chconfig; |
| 56 | int name; |
| 57 | }; |
| 58 | |
| 59 | struct usbmix_name_map; |
| 60 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 61 | struct mixer_build { |
| 62 | struct snd_usb_audio *chip; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 63 | struct usb_mixer_interface *mixer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | unsigned char *buffer; |
| 65 | unsigned int buflen; |
Jaroslav Kysela | 291186e | 2010-02-16 11:55:18 +0100 | [diff] [blame] | 66 | DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS); |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 67 | struct usb_audio_term oterm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | const struct usbmix_name_map *map; |
Clemens Ladisch | 8e062ec | 2005-04-22 15:49:52 +0200 | [diff] [blame] | 69 | const struct usbmix_selector_map *selector_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | enum { |
| 73 | USB_MIXER_BOOLEAN, |
| 74 | USB_MIXER_INV_BOOLEAN, |
| 75 | USB_MIXER_S8, |
| 76 | USB_MIXER_U8, |
| 77 | USB_MIXER_S16, |
| 78 | USB_MIXER_U16, |
| 79 | }; |
| 80 | |
| 81 | enum { |
| 82 | USB_PROC_UPDOWN = 1, |
| 83 | USB_PROC_UPDOWN_SWITCH = 1, |
| 84 | USB_PROC_UPDOWN_MODE_SEL = 2, |
| 85 | |
| 86 | USB_PROC_PROLOGIC = 2, |
| 87 | USB_PROC_PROLOGIC_SWITCH = 1, |
| 88 | USB_PROC_PROLOGIC_MODE_SEL = 2, |
| 89 | |
| 90 | USB_PROC_3DENH = 3, |
| 91 | USB_PROC_3DENH_SWITCH = 1, |
| 92 | USB_PROC_3DENH_SPACE = 2, |
| 93 | |
| 94 | USB_PROC_REVERB = 4, |
| 95 | USB_PROC_REVERB_SWITCH = 1, |
| 96 | USB_PROC_REVERB_LEVEL = 2, |
| 97 | USB_PROC_REVERB_TIME = 3, |
| 98 | USB_PROC_REVERB_DELAY = 4, |
| 99 | |
| 100 | USB_PROC_CHORUS = 5, |
| 101 | USB_PROC_CHORUS_SWITCH = 1, |
| 102 | USB_PROC_CHORUS_LEVEL = 2, |
| 103 | USB_PROC_CHORUS_RATE = 3, |
| 104 | USB_PROC_CHORUS_DEPTH = 4, |
| 105 | |
| 106 | USB_PROC_DCR = 6, |
| 107 | USB_PROC_DCR_SWITCH = 1, |
| 108 | USB_PROC_DCR_RATIO = 2, |
| 109 | USB_PROC_DCR_MAX_AMP = 3, |
| 110 | USB_PROC_DCR_THRESHOLD = 4, |
| 111 | USB_PROC_DCR_ATTACK = 5, |
| 112 | USB_PROC_DCR_RELEASE = 6, |
| 113 | }; |
| 114 | |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame] | 115 | /*E-mu 0202(0404) eXtension Unit(XU) control*/ |
| 116 | enum { |
| 117 | USB_XU_CLOCK_RATE = 0xe301, |
| 118 | USB_XU_CLOCK_SOURCE = 0xe302, |
| 119 | USB_XU_DIGITAL_IO_STATUS = 0xe303, |
| 120 | USB_XU_DEVICE_OPTIONS = 0xe304, |
| 121 | USB_XU_DIRECT_MONITORING = 0xe305, |
| 122 | USB_XU_METERING = 0xe306 |
| 123 | }; |
| 124 | enum { |
| 125 | USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/ |
| 126 | USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */ |
| 127 | USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */ |
| 128 | USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */ |
| 129 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * manual mapping of mixer names |
| 133 | * if the mixer topology is too complicated and the parsed names are |
| 134 | * ambiguous, add the entries in usbmixer_maps.c. |
| 135 | */ |
Daniel Mack | f0b5e63 | 2010-03-11 21:13:23 +0100 | [diff] [blame] | 136 | #include "mixer_maps.c" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 138 | static const struct usbmix_name_map * |
| 139 | find_map(struct mixer_build *state, int unitid, int control) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 141 | const struct usbmix_name_map *p = state->map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 143 | if (!p) |
| 144 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | for (p = state->map; p->id; p++) { |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 147 | if (p->id == unitid && |
| 148 | (!control || !p->control || control == p->control)) |
| 149 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | /* get the mapped name if the unit matches */ |
| 155 | static int |
| 156 | check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen) |
| 157 | { |
| 158 | if (!p || !p->name) |
| 159 | return 0; |
| 160 | |
| 161 | buflen--; |
| 162 | return strlcpy(buf, p->name, buflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* check whether the control should be ignored */ |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 166 | static inline int |
| 167 | check_ignored_ctl(const struct usbmix_name_map *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 169 | if (!p || p->name || p->dB) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return 0; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | /* dB mapping */ |
| 175 | static inline void check_mapped_dB(const struct usbmix_name_map *p, |
| 176 | struct usb_mixer_elem_info *cval) |
| 177 | { |
| 178 | if (p && p->dB) { |
| 179 | cval->dBmin = p->dB->min; |
| 180 | cval->dBmax = p->dB->max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Clemens Ladisch | 8e062ec | 2005-04-22 15:49:52 +0200 | [diff] [blame] | 184 | /* get the mapped selector source name */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 185 | static int check_mapped_selector_name(struct mixer_build *state, int unitid, |
Clemens Ladisch | 8e062ec | 2005-04-22 15:49:52 +0200 | [diff] [blame] | 186 | int index, char *buf, int buflen) |
| 187 | { |
| 188 | const struct usbmix_selector_map *p; |
| 189 | |
| 190 | if (! state->selector_map) |
| 191 | return 0; |
| 192 | for (p = state->selector_map; p->id; p++) { |
| 193 | if (p->id == unitid && index < p->count) |
| 194 | return strlcpy(buf, p->names[index], buflen); |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | /* |
| 200 | * find an audio control unit with the given unit id |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 201 | * this doesn't return any clock related units, so they need to be handled elsewhere |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 203 | static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
| 205 | unsigned char *p; |
| 206 | |
| 207 | p = NULL; |
| 208 | while ((p = snd_usb_find_desc(state->buffer, state->buflen, p, |
| 209 | USB_DT_CS_INTERFACE)) != NULL) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 210 | if (p[0] >= 4 && p[2] >= UAC_INPUT_TERMINAL && p[2] <= UAC2_EXTENSION_UNIT_V2 && p[3] == unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return p; |
| 212 | } |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /* |
| 218 | * copy a string with the given id |
| 219 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 220 | static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
| 222 | int len = usb_string(state->chip->dev, index, buf, maxlen - 1); |
| 223 | buf[len] = 0; |
| 224 | return len; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * convert from the byte/word on usb descriptor to the zero-based integer |
| 229 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 230 | 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] | 231 | { |
| 232 | switch (cval->val_type) { |
| 233 | case USB_MIXER_BOOLEAN: |
| 234 | return !!val; |
| 235 | case USB_MIXER_INV_BOOLEAN: |
| 236 | return !val; |
| 237 | case USB_MIXER_U8: |
| 238 | val &= 0xff; |
| 239 | break; |
| 240 | case USB_MIXER_S8: |
| 241 | val &= 0xff; |
| 242 | if (val >= 0x80) |
| 243 | val -= 0x100; |
| 244 | break; |
| 245 | case USB_MIXER_U16: |
| 246 | val &= 0xffff; |
| 247 | break; |
| 248 | case USB_MIXER_S16: |
| 249 | val &= 0xffff; |
| 250 | if (val >= 0x8000) |
| 251 | val -= 0x10000; |
| 252 | break; |
| 253 | } |
| 254 | return val; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * convert from the zero-based int to the byte/word for usb descriptor |
| 259 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 260 | 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] | 261 | { |
| 262 | switch (cval->val_type) { |
| 263 | case USB_MIXER_BOOLEAN: |
| 264 | return !!val; |
| 265 | case USB_MIXER_INV_BOOLEAN: |
| 266 | return !val; |
| 267 | case USB_MIXER_S8: |
| 268 | case USB_MIXER_U8: |
| 269 | return val & 0xff; |
| 270 | case USB_MIXER_S16: |
| 271 | case USB_MIXER_U16: |
| 272 | return val & 0xffff; |
| 273 | } |
| 274 | return 0; /* not reached */ |
| 275 | } |
| 276 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 277 | 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] | 278 | { |
| 279 | if (! cval->res) |
| 280 | cval->res = 1; |
| 281 | if (val < cval->min) |
| 282 | return 0; |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 283 | else if (val >= cval->max) |
| 284 | return (cval->max - cval->min + cval->res - 1) / cval->res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | else |
| 286 | return (val - cval->min) / cval->res; |
| 287 | } |
| 288 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 289 | 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] | 290 | { |
| 291 | if (val < 0) |
| 292 | return cval->min; |
| 293 | if (! cval->res) |
| 294 | cval->res = 1; |
| 295 | val *= cval->res; |
| 296 | val += cval->min; |
| 297 | if (val > cval->max) |
| 298 | return cval->max; |
| 299 | return val; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | /* |
| 304 | * retrieve a mixer value |
| 305 | */ |
| 306 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 307 | static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
| 309 | unsigned char buf[2]; |
| 310 | int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; |
| 311 | int timeout = 10; |
| 312 | |
| 313 | while (timeout-- > 0) { |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 314 | if (snd_usb_ctl_msg(cval->mixer->chip->dev, |
| 315 | usb_rcvctrlpipe(cval->mixer->chip->dev, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | request, |
| 317 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 318 | validx, cval->mixer->ctrlif | (cval->id << 8), |
Thomas Reitmayr | a04395e | 2007-05-15 11:47:48 +0200 | [diff] [blame] | 319 | buf, val_len, 100) >= val_len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len)); |
| 321 | return 0; |
| 322 | } |
| 323 | } |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 324 | snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", |
| 325 | request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | return -EINVAL; |
| 327 | } |
| 328 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 329 | static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret) |
| 330 | { |
| 331 | unsigned char buf[14]; /* enough space for one range of 4 bytes */ |
| 332 | unsigned char *val; |
| 333 | int ret; |
| 334 | __u8 bRequest; |
| 335 | |
| 336 | bRequest = (request == UAC_GET_CUR) ? |
| 337 | UAC2_CS_CUR : UAC2_CS_RANGE; |
| 338 | |
| 339 | ret = snd_usb_ctl_msg(cval->mixer->chip->dev, |
| 340 | usb_rcvctrlpipe(cval->mixer->chip->dev, 0), |
| 341 | bRequest, |
| 342 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, |
| 343 | validx, cval->mixer->ctrlif | (cval->id << 8), |
| 344 | buf, sizeof(buf), 1000); |
| 345 | |
| 346 | if (ret < 0) { |
| 347 | snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n", |
| 348 | request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type); |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | switch (request) { |
| 353 | case UAC_GET_CUR: |
| 354 | val = buf; |
| 355 | break; |
| 356 | case UAC_GET_MIN: |
| 357 | val = buf + sizeof(__u16); |
| 358 | break; |
| 359 | case UAC_GET_MAX: |
| 360 | val = buf + sizeof(__u16) * 2; |
| 361 | break; |
| 362 | case UAC_GET_RES: |
| 363 | val = buf + sizeof(__u16) * 3; |
| 364 | break; |
| 365 | default: |
| 366 | return -EINVAL; |
| 367 | } |
| 368 | |
| 369 | *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(val, sizeof(__u16))); |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret) |
| 375 | { |
| 376 | return (cval->mixer->protocol == UAC_VERSION_1) ? |
| 377 | get_ctl_value_v1(cval, request, validx, value_ret) : |
| 378 | get_ctl_value_v2(cval, request, validx, value_ret); |
| 379 | } |
| 380 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 381 | static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 383 | return get_ctl_value(cval, UAC_GET_CUR, validx, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* channel = 0: master, 1 = first channel */ |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 387 | static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval, |
| 388 | int channel, int *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 390 | return get_ctl_value(cval, UAC_GET_CUR, (cval->control << 8) | channel, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 393 | static int get_cur_mix_value(struct usb_mixer_elem_info *cval, |
| 394 | int channel, int index, int *value) |
| 395 | { |
| 396 | int err; |
| 397 | |
| 398 | if (cval->cached & (1 << channel)) { |
| 399 | *value = cval->cache_val[index]; |
| 400 | return 0; |
| 401 | } |
| 402 | err = get_cur_mix_raw(cval, channel, value); |
| 403 | if (err < 0) { |
| 404 | if (!cval->mixer->ignore_ctl_error) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 405 | snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 406 | cval->control, channel, err); |
| 407 | return err; |
| 408 | } |
| 409 | cval->cached |= 1 << channel; |
| 410 | cval->cache_val[index] = *value; |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | /* |
| 416 | * set a mixer value |
| 417 | */ |
| 418 | |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 419 | int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval, |
| 420 | int request, int validx, int value_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | { |
| 422 | unsigned char buf[2]; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 423 | int val_len, timeout = 10; |
| 424 | |
| 425 | if (cval->mixer->protocol == UAC_VERSION_1) { |
| 426 | val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1; |
| 427 | } else { /* UAC_VERSION_2 */ |
| 428 | /* audio class v2 controls are always 2 bytes in size */ |
| 429 | val_len = sizeof(__u16); |
| 430 | |
| 431 | /* FIXME */ |
| 432 | if (request != UAC_SET_CUR) { |
| 433 | snd_printdd(KERN_WARNING "RANGE setting not yet supported\n"); |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
| 437 | request = UAC2_CS_CUR; |
| 438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | value_set = convert_bytes_value(cval, value_set); |
| 441 | buf[0] = value_set & 0xff; |
| 442 | buf[1] = (value_set >> 8) & 0xff; |
Viral Mehta | cf3f913 | 2009-04-03 13:08:14 +0530 | [diff] [blame] | 443 | while (timeout-- > 0) |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 444 | if (snd_usb_ctl_msg(cval->mixer->chip->dev, |
| 445 | usb_sndctrlpipe(cval->mixer->chip->dev, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | request, |
| 447 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 448 | validx, cval->mixer->ctrlif | (cval->id << 8), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | buf, val_len, 100) >= 0) |
| 450 | return 0; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 451 | snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n", |
| 452 | request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | return -EINVAL; |
| 454 | } |
| 455 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 456 | static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 458 | 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] | 459 | } |
| 460 | |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 461 | static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, |
| 462 | int index, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 464 | int err; |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 465 | err = snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, (cval->control << 8) | channel, |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 466 | value); |
| 467 | if (err < 0) |
| 468 | return err; |
| 469 | cval->cached |= 1 << channel; |
| 470 | cval->cache_val[index] = value; |
| 471 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Takashi Iwai | 7bc5ba7e | 2006-07-14 15:18:19 +0200 | [diff] [blame] | 474 | /* |
| 475 | * TLV callback for mixer volume controls |
| 476 | */ |
| 477 | static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
| 478 | unsigned int size, unsigned int __user *_tlv) |
| 479 | { |
| 480 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Takashi Iwai | b8e1c73 | 2009-06-16 14:04:37 +0200 | [diff] [blame] | 481 | DECLARE_TLV_DB_MINMAX(scale, 0, 0); |
Takashi Iwai | 7bc5ba7e | 2006-07-14 15:18:19 +0200 | [diff] [blame] | 482 | |
| 483 | if (size < sizeof(scale)) |
| 484 | return -ENOMEM; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 485 | scale[2] = cval->dBmin; |
| 486 | scale[3] = cval->dBmax; |
Takashi Iwai | 7bc5ba7e | 2006-07-14 15:18:19 +0200 | [diff] [blame] | 487 | if (copy_to_user(_tlv, scale, sizeof(scale))) |
| 488 | return -EFAULT; |
| 489 | return 0; |
| 490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * parser routines begin here... |
| 494 | */ |
| 495 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 496 | static int parse_audio_unit(struct mixer_build *state, int unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
| 498 | |
| 499 | /* |
| 500 | * check if the input/output channel routing is enabled on the given bitmap. |
| 501 | * used for mixer unit parser |
| 502 | */ |
| 503 | static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs) |
| 504 | { |
| 505 | int idx = ich * num_outs + och; |
| 506 | return bmap[idx >> 3] & (0x80 >> (idx & 7)); |
| 507 | } |
| 508 | |
| 509 | |
| 510 | /* |
| 511 | * add an alsa control element |
| 512 | * search and increment the index until an empty slot is found. |
| 513 | * |
| 514 | * if failed, give up and free the control instance. |
| 515 | */ |
| 516 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 517 | static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 519 | struct usb_mixer_elem_info *cval = kctl->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | int err; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 521 | |
| 522 | while (snd_ctl_find_id(state->chip->card, &kctl->id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | kctl->id.index++; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 524 | if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | snd_printd(KERN_ERR "cannot add control (err = %d)\n", err); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 526 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 528 | cval->elem_id = &kctl->id; |
| 529 | cval->next_id_elem = state->mixer->id_elems[cval->id]; |
| 530 | state->mixer->id_elems[cval->id] = cval; |
| 531 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | |
| 535 | /* |
| 536 | * get a terminal name string |
| 537 | */ |
| 538 | |
| 539 | static struct iterm_name_combo { |
| 540 | int type; |
| 541 | char *name; |
| 542 | } iterm_names[] = { |
| 543 | { 0x0300, "Output" }, |
| 544 | { 0x0301, "Speaker" }, |
| 545 | { 0x0302, "Headphone" }, |
| 546 | { 0x0303, "HMD Audio" }, |
| 547 | { 0x0304, "Desktop Speaker" }, |
| 548 | { 0x0305, "Room Speaker" }, |
| 549 | { 0x0306, "Com Speaker" }, |
| 550 | { 0x0307, "LFE" }, |
| 551 | { 0x0600, "External In" }, |
| 552 | { 0x0601, "Analog In" }, |
| 553 | { 0x0602, "Digital In" }, |
| 554 | { 0x0603, "Line" }, |
| 555 | { 0x0604, "Legacy In" }, |
| 556 | { 0x0605, "IEC958 In" }, |
| 557 | { 0x0606, "1394 DA Stream" }, |
| 558 | { 0x0607, "1394 DV Stream" }, |
| 559 | { 0x0700, "Embedded" }, |
| 560 | { 0x0701, "Noise Source" }, |
| 561 | { 0x0702, "Equalization Noise" }, |
| 562 | { 0x0703, "CD" }, |
| 563 | { 0x0704, "DAT" }, |
| 564 | { 0x0705, "DCC" }, |
| 565 | { 0x0706, "MiniDisk" }, |
| 566 | { 0x0707, "Analog Tape" }, |
| 567 | { 0x0708, "Phonograph" }, |
| 568 | { 0x0709, "VCR Audio" }, |
| 569 | { 0x070a, "Video Disk Audio" }, |
| 570 | { 0x070b, "DVD Audio" }, |
| 571 | { 0x070c, "TV Tuner Audio" }, |
| 572 | { 0x070d, "Satellite Rec Audio" }, |
| 573 | { 0x070e, "Cable Tuner Audio" }, |
| 574 | { 0x070f, "DSS Audio" }, |
| 575 | { 0x0710, "Radio Receiver" }, |
| 576 | { 0x0711, "Radio Transmitter" }, |
| 577 | { 0x0712, "Multi-Track Recorder" }, |
| 578 | { 0x0713, "Synthesizer" }, |
| 579 | { 0 }, |
| 580 | }; |
| 581 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 582 | 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] | 583 | unsigned char *name, int maxlen, int term_only) |
| 584 | { |
| 585 | struct iterm_name_combo *names; |
| 586 | |
| 587 | if (iterm->name) |
| 588 | return snd_usb_copy_string_desc(state, iterm->name, name, maxlen); |
| 589 | |
| 590 | /* virtual type - not a real terminal */ |
| 591 | if (iterm->type >> 16) { |
| 592 | if (term_only) |
| 593 | return 0; |
| 594 | switch (iterm->type >> 16) { |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 595 | case UAC_SELECTOR_UNIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | strcpy(name, "Selector"); return 8; |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 597 | case UAC_PROCESSING_UNIT_V1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | strcpy(name, "Process Unit"); return 12; |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 599 | case UAC_EXTENSION_UNIT_V1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | strcpy(name, "Ext Unit"); return 8; |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 601 | case UAC_MIXER_UNIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | strcpy(name, "Mixer"); return 5; |
| 603 | default: |
| 604 | return sprintf(name, "Unit %d", iterm->id); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | switch (iterm->type & 0xff00) { |
| 609 | case 0x0100: |
| 610 | strcpy(name, "PCM"); return 3; |
| 611 | case 0x0200: |
| 612 | strcpy(name, "Mic"); return 3; |
| 613 | case 0x0400: |
| 614 | strcpy(name, "Headset"); return 7; |
| 615 | case 0x0500: |
| 616 | strcpy(name, "Phone"); return 5; |
| 617 | } |
| 618 | |
| 619 | for (names = iterm_names; names->type; names++) |
| 620 | if (names->type == iterm->type) { |
| 621 | strcpy(name, names->name); |
| 622 | return strlen(names->name); |
| 623 | } |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | |
| 628 | /* |
| 629 | * parse the source unit recursively until it reaches to a terminal |
| 630 | * or a branched unit. |
| 631 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 632 | static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 634 | void *p1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
| 636 | memset(term, 0, sizeof(*term)); |
| 637 | while ((p1 = find_audio_control_unit(state, id)) != NULL) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 638 | unsigned char *hdr = p1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | term->id = id; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 640 | switch (hdr[2]) { |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 641 | case UAC_INPUT_TERMINAL: |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 642 | if (state->mixer->protocol == UAC_VERSION_1) { |
| 643 | struct uac_input_terminal_descriptor *d = p1; |
| 644 | term->type = le16_to_cpu(d->wTerminalType); |
| 645 | term->channels = d->bNrChannels; |
| 646 | term->chconfig = le16_to_cpu(d->wChannelConfig); |
| 647 | term->name = d->iTerminal; |
| 648 | } else { /* UAC_VERSION_2 */ |
| 649 | struct uac2_input_terminal_descriptor *d = p1; |
| 650 | term->type = le16_to_cpu(d->wTerminalType); |
| 651 | term->channels = d->bNrChannels; |
| 652 | term->chconfig = le32_to_cpu(d->bmChannelConfig); |
| 653 | term->name = d->iTerminal; |
| 654 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | return 0; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 656 | case UAC_FEATURE_UNIT: { |
| 657 | /* the header is the same for v1 and v2 */ |
| 658 | struct uac_feature_unit_descriptor *d = p1; |
Daniel Mack | 5e68888 | 2010-05-08 11:24:56 +0200 | [diff] [blame] | 659 | id = d->bSourceID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | break; /* continue to parse */ |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 661 | } |
| 662 | case UAC_MIXER_UNIT: { |
| 663 | struct uac_mixer_unit_descriptor *d = p1; |
| 664 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
| 665 | term->channels = uac_mixer_unit_bNrChannels(d); |
| 666 | term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol); |
| 667 | term->name = uac_mixer_unit_iMixer(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | return 0; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 669 | } |
| 670 | case UAC_SELECTOR_UNIT: { |
| 671 | struct uac_selector_unit_descriptor *d = p1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | /* call recursively to retrieve the channel info */ |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 673 | if (check_input_term(state, d->baSourceID[0], term) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | return -ENODEV; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 675 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | term->id = id; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 677 | term->name = uac_selector_unit_iSelector(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | return 0; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 679 | } |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 680 | case UAC_PROCESSING_UNIT_V1: |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 681 | case UAC_EXTENSION_UNIT_V1: { |
| 682 | struct uac_processing_unit_descriptor *d = p1; |
| 683 | if (d->bNrInPins) { |
| 684 | id = d->baSourceID[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | break; /* continue to parse */ |
| 686 | } |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 687 | term->type = d->bDescriptorSubtype << 16; /* virtual type */ |
| 688 | term->channels = uac_processing_unit_bNrChannels(d); |
| 689 | term->chconfig = uac_processing_unit_wChannelConfig(d, state->mixer->protocol); |
| 690 | term->name = uac_processing_unit_iProcessing(d, state->mixer->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | return 0; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 692 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | default: |
| 694 | return -ENODEV; |
| 695 | } |
| 696 | } |
| 697 | return -ENODEV; |
| 698 | } |
| 699 | |
| 700 | |
| 701 | /* |
| 702 | * Feature Unit |
| 703 | */ |
| 704 | |
| 705 | /* feature unit control information */ |
| 706 | struct usb_feature_control_info { |
| 707 | const char *name; |
| 708 | unsigned int type; /* control type (mute, volume, etc.) */ |
| 709 | }; |
| 710 | |
| 711 | static struct usb_feature_control_info audio_feature_info[] = { |
| 712 | { "Mute", USB_MIXER_INV_BOOLEAN }, |
| 713 | { "Volume", USB_MIXER_S16 }, |
| 714 | { "Tone Control - Bass", USB_MIXER_S8 }, |
| 715 | { "Tone Control - Mid", USB_MIXER_S8 }, |
| 716 | { "Tone Control - Treble", USB_MIXER_S8 }, |
| 717 | { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */ |
| 718 | { "Auto Gain Control", USB_MIXER_BOOLEAN }, |
| 719 | { "Delay Control", USB_MIXER_U16 }, |
| 720 | { "Bass Boost", USB_MIXER_BOOLEAN }, |
| 721 | { "Loudness", USB_MIXER_BOOLEAN }, |
| 722 | }; |
| 723 | |
| 724 | |
| 725 | /* private_free callback */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 726 | static void usb_mixer_elem_free(struct snd_kcontrol *kctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | { |
Jesper Juhl | 4d57277 | 2005-05-30 17:30:32 +0200 | [diff] [blame] | 728 | kfree(kctl->private_data); |
| 729 | kctl->private_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | |
| 733 | /* |
| 734 | * interface to ALSA control for feature/mixer units |
| 735 | */ |
| 736 | |
| 737 | /* |
| 738 | * retrieve the minimum and maximum values for the specified control |
| 739 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 740 | static int get_min_max(struct usb_mixer_elem_info *cval, int default_min) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | { |
| 742 | /* for failsafe */ |
| 743 | cval->min = default_min; |
| 744 | cval->max = cval->min + 1; |
| 745 | cval->res = 1; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 746 | cval->dBmin = cval->dBmax = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
| 748 | if (cval->val_type == USB_MIXER_BOOLEAN || |
| 749 | cval->val_type == USB_MIXER_INV_BOOLEAN) { |
| 750 | cval->initialized = 1; |
| 751 | } else { |
| 752 | int minchn = 0; |
| 753 | if (cval->cmask) { |
| 754 | int i; |
| 755 | for (i = 0; i < MAX_CHANNELS; i++) |
| 756 | if (cval->cmask & (1 << i)) { |
| 757 | minchn = i + 1; |
| 758 | break; |
| 759 | } |
| 760 | } |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 761 | if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 || |
| 762 | get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) { |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 763 | snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n", |
| 764 | cval->id, cval->mixer->ctrlif, cval->control, cval->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | return -EINVAL; |
| 766 | } |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 767 | if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | cval->res = 1; |
| 769 | } else { |
| 770 | int last_valid_res = cval->res; |
| 771 | |
| 772 | while (cval->res > 1) { |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 773 | if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES, |
| 774 | (cval->control << 8) | minchn, cval->res / 2) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | break; |
| 776 | cval->res /= 2; |
| 777 | } |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 778 | if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | cval->res = last_valid_res; |
| 780 | } |
| 781 | if (cval->res == 0) |
| 782 | cval->res = 1; |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 783 | |
| 784 | /* Additional checks for the proper resolution |
| 785 | * |
| 786 | * Some devices report smaller resolutions than actually |
| 787 | * reacting. They don't return errors but simply clip |
| 788 | * to the lower aligned value. |
| 789 | */ |
| 790 | if (cval->min + cval->res < cval->max) { |
| 791 | int last_valid_res = cval->res; |
| 792 | int saved, test, check; |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 793 | get_cur_mix_raw(cval, minchn, &saved); |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 794 | for (;;) { |
| 795 | test = saved; |
| 796 | if (test < cval->max) |
| 797 | test += cval->res; |
| 798 | else |
| 799 | test -= cval->res; |
| 800 | if (test < cval->min || test > cval->max || |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 801 | set_cur_mix_value(cval, minchn, 0, test) || |
| 802 | get_cur_mix_raw(cval, minchn, &check)) { |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 803 | cval->res = last_valid_res; |
| 804 | break; |
| 805 | } |
| 806 | if (test == check) |
| 807 | break; |
| 808 | cval->res *= 2; |
| 809 | } |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 810 | set_cur_mix_value(cval, minchn, 0, saved); |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 811 | } |
| 812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | cval->initialized = 1; |
| 814 | } |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 815 | |
| 816 | /* USB descriptions contain the dB scale in 1/256 dB unit |
| 817 | * while ALSA TLV contains in 1/100 dB unit |
| 818 | */ |
| 819 | cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256; |
| 820 | cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256; |
| 821 | if (cval->dBmin > cval->dBmax) { |
| 822 | /* something is wrong; assume it's either from/to 0dB */ |
| 823 | if (cval->dBmin < 0) |
| 824 | cval->dBmax = 0; |
| 825 | else if (cval->dBmin > 0) |
| 826 | cval->dBmin = 0; |
| 827 | if (cval->dBmin > cval->dBmax) { |
| 828 | /* totally crap, return an error */ |
| 829 | return -EINVAL; |
| 830 | } |
| 831 | } |
| 832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | |
| 837 | /* get a feature/mixer unit info */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 838 | static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 840 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | |
| 842 | if (cval->val_type == USB_MIXER_BOOLEAN || |
| 843 | cval->val_type == USB_MIXER_INV_BOOLEAN) |
| 844 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 845 | else |
| 846 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 847 | uinfo->count = cval->channels; |
| 848 | if (cval->val_type == USB_MIXER_BOOLEAN || |
| 849 | cval->val_type == USB_MIXER_INV_BOOLEAN) { |
| 850 | uinfo->value.integer.min = 0; |
| 851 | uinfo->value.integer.max = 1; |
| 852 | } else { |
| 853 | if (! cval->initialized) |
| 854 | get_min_max(cval, 0); |
| 855 | uinfo->value.integer.min = 0; |
Takashi Iwai | 14790f1 | 2006-03-28 17:58:28 +0200 | [diff] [blame] | 856 | uinfo->value.integer.max = |
| 857 | (cval->max - cval->min + cval->res - 1) / cval->res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | /* get the current value from feature/mixer unit */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 863 | static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 865 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | int c, cnt, val, err; |
| 867 | |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 868 | ucontrol->value.integer.value[0] = cval->min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | if (cval->cmask) { |
| 870 | cnt = 0; |
| 871 | for (c = 0; c < MAX_CHANNELS; c++) { |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 872 | if (!(cval->cmask & (1 << c))) |
| 873 | continue; |
| 874 | err = get_cur_mix_value(cval, c + 1, cnt, &val); |
| 875 | if (err < 0) |
| 876 | return cval->mixer->ignore_ctl_error ? 0 : err; |
| 877 | val = get_relative_value(cval, val); |
| 878 | ucontrol->value.integer.value[cnt] = val; |
| 879 | cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | } |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 881 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | } else { |
| 883 | /* master channel */ |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 884 | err = get_cur_mix_value(cval, 0, 0, &val); |
| 885 | if (err < 0) |
| 886 | return cval->mixer->ignore_ctl_error ? 0 : err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | val = get_relative_value(cval, val); |
| 888 | ucontrol->value.integer.value[0] = val; |
| 889 | } |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | /* put the current value to feature/mixer unit */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 894 | static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 896 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | int c, cnt, val, oval, err; |
| 898 | int changed = 0; |
| 899 | |
| 900 | if (cval->cmask) { |
| 901 | cnt = 0; |
| 902 | for (c = 0; c < MAX_CHANNELS; c++) { |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 903 | if (!(cval->cmask & (1 << c))) |
| 904 | continue; |
| 905 | err = get_cur_mix_value(cval, c + 1, cnt, &oval); |
| 906 | if (err < 0) |
| 907 | return cval->mixer->ignore_ctl_error ? 0 : err; |
| 908 | val = ucontrol->value.integer.value[cnt]; |
| 909 | val = get_abs_value(cval, val); |
| 910 | if (oval != val) { |
| 911 | set_cur_mix_value(cval, c + 1, cnt, val); |
| 912 | changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 914 | cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
| 916 | } else { |
| 917 | /* master channel */ |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 918 | err = get_cur_mix_value(cval, 0, 0, &oval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | if (err < 0) |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 920 | return cval->mixer->ignore_ctl_error ? 0 : err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | val = ucontrol->value.integer.value[0]; |
| 922 | val = get_abs_value(cval, val); |
| 923 | if (val != oval) { |
Takashi Iwai | 641b487 | 2009-01-15 17:05:24 +0100 | [diff] [blame] | 924 | set_cur_mix_value(cval, 0, 0, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | changed = 1; |
| 926 | } |
| 927 | } |
| 928 | return changed; |
| 929 | } |
| 930 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 931 | static struct snd_kcontrol_new usb_feature_unit_ctl = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 933 | .name = "", /* will be filled later manually */ |
| 934 | .info = mixer_ctl_feature_info, |
| 935 | .get = mixer_ctl_feature_get, |
| 936 | .put = mixer_ctl_feature_put, |
| 937 | }; |
| 938 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 939 | /* the read-only variant */ |
| 940 | static struct snd_kcontrol_new usb_feature_unit_ctl_ro = { |
| 941 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 942 | .name = "", /* will be filled later manually */ |
| 943 | .info = mixer_ctl_feature_info, |
| 944 | .get = mixer_ctl_feature_get, |
| 945 | .put = NULL, |
| 946 | }; |
| 947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
| 949 | /* |
| 950 | * build a feature control |
| 951 | */ |
| 952 | |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 953 | static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str) |
| 954 | { |
| 955 | return strlcat(kctl->id.name, str, sizeof(kctl->id.name)); |
| 956 | } |
| 957 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 958 | static void build_feature_ctl(struct mixer_build *state, void *raw_desc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | unsigned int ctl_mask, int control, |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 960 | struct usb_audio_term *iterm, int unitid, |
| 961 | int read_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 963 | struct uac_feature_unit_descriptor *desc = raw_desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | unsigned int len = 0; |
| 965 | int mapped_name = 0; |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 966 | int nameid = uac_feature_unit_iFeature(desc); |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 967 | struct snd_kcontrol *kctl; |
| 968 | struct usb_mixer_elem_info *cval; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 969 | const struct usbmix_name_map *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
| 971 | control++; /* change from zero-based to 1-based value */ |
| 972 | |
Daniel Mack | 45d7605 | 2010-03-11 21:13:21 +0100 | [diff] [blame] | 973 | if (control == UAC_GRAPHIC_EQUALIZER_CONTROL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | /* FIXME: not supported yet */ |
| 975 | return; |
| 976 | } |
| 977 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 978 | map = find_map(state, unitid, control); |
| 979 | if (check_ignored_ctl(map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | return; |
| 981 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 982 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | if (! cval) { |
| 984 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); |
| 985 | return; |
| 986 | } |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 987 | cval->mixer = state->mixer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | cval->id = unitid; |
| 989 | cval->control = control; |
| 990 | cval->cmask = ctl_mask; |
| 991 | cval->val_type = audio_feature_info[control-1].type; |
| 992 | if (ctl_mask == 0) |
| 993 | cval->channels = 1; /* master channel */ |
| 994 | else { |
| 995 | int i, c = 0; |
| 996 | for (i = 0; i < 16; i++) |
| 997 | if (ctl_mask & (1 << i)) |
| 998 | c++; |
| 999 | cval->channels = c; |
| 1000 | } |
| 1001 | |
| 1002 | /* get min/max values */ |
| 1003 | get_min_max(cval, 0); |
| 1004 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1005 | if (read_only) |
| 1006 | kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval); |
| 1007 | else |
| 1008 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); |
| 1009 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | if (! kctl) { |
| 1011 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); |
| 1012 | kfree(cval); |
| 1013 | return; |
| 1014 | } |
| 1015 | kctl->private_free = usb_mixer_elem_free; |
| 1016 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1017 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | mapped_name = len != 0; |
| 1019 | if (! len && nameid) |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1020 | len = snd_usb_copy_string_desc(state, nameid, |
| 1021 | kctl->id.name, sizeof(kctl->id.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | |
| 1023 | switch (control) { |
Daniel Mack | 45d7605 | 2010-03-11 21:13:21 +0100 | [diff] [blame] | 1024 | case UAC_MUTE_CONTROL: |
| 1025 | case UAC_VOLUME_CONTROL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | /* determine the control name. the rule is: |
| 1027 | * - if a name id is given in descriptor, use it. |
| 1028 | * - if the connected input can be determined, then use the name |
| 1029 | * of terminal type. |
| 1030 | * - if the connected output can be determined, use it. |
| 1031 | * - otherwise, anonymous name. |
| 1032 | */ |
| 1033 | if (! len) { |
| 1034 | len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1); |
| 1035 | if (! len) |
| 1036 | len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1); |
| 1037 | if (! len) |
| 1038 | len = snprintf(kctl->id.name, sizeof(kctl->id.name), |
| 1039 | "Feature %d", unitid); |
| 1040 | } |
| 1041 | /* determine the stream direction: |
| 1042 | * if the connected output is USB stream, then it's likely a |
| 1043 | * capture stream. otherwise it should be playback (hopefully :) |
| 1044 | */ |
| 1045 | if (! mapped_name && ! (state->oterm.type >> 16)) { |
| 1046 | if ((state->oterm.type & 0xff00) == 0x0100) { |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 1047 | len = append_ctl_name(kctl, " Capture"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | } else { |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 1049 | len = append_ctl_name(kctl, " Playback"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | } |
| 1051 | } |
Daniel Mack | 45d7605 | 2010-03-11 21:13:21 +0100 | [diff] [blame] | 1052 | append_ctl_name(kctl, control == UAC_MUTE_CONTROL ? |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 1053 | " Switch" : " Volume"); |
Daniel Mack | 45d7605 | 2010-03-11 21:13:21 +0100 | [diff] [blame] | 1054 | if (control == UAC_VOLUME_CONTROL) { |
Takashi Iwai | 7bc5ba7e | 2006-07-14 15:18:19 +0200 | [diff] [blame] | 1055 | kctl->tlv.c = mixer_vol_tlv; |
| 1056 | kctl->vd[0].access |= |
| 1057 | SNDRV_CTL_ELEM_ACCESS_TLV_READ | |
| 1058 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1059 | check_mapped_dB(map, cval); |
Takashi Iwai | 7bc5ba7e | 2006-07-14 15:18:19 +0200 | [diff] [blame] | 1060 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | break; |
| 1062 | |
| 1063 | default: |
| 1064 | if (! len) |
| 1065 | strlcpy(kctl->id.name, audio_feature_info[control-1].name, |
| 1066 | sizeof(kctl->id.name)); |
| 1067 | break; |
| 1068 | } |
| 1069 | |
Alexey Fisher | 2cf313e | 2009-07-22 14:57:54 +0200 | [diff] [blame] | 1070 | /* volume control quirks */ |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 1071 | switch (state->chip->usb_id) { |
| 1072 | case USB_ID(0x0471, 0x0101): |
| 1073 | case USB_ID(0x0471, 0x0104): |
| 1074 | case USB_ID(0x0471, 0x0105): |
| 1075 | case USB_ID(0x0672, 0x1041): |
Alexey Fisher | 2cf313e | 2009-07-22 14:57:54 +0200 | [diff] [blame] | 1076 | /* quirk for UDA1321/N101. |
| 1077 | * note that detection between firmware 2.1.1.7 (N101) |
| 1078 | * and later 2.1.1.21 is not very clear from datasheets. |
| 1079 | * I hope that the min value is -15360 for newer firmware --jk |
| 1080 | */ |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 1081 | if (!strcmp(kctl->id.name, "PCM Playback Volume") && |
| 1082 | cval->min == -15616) { |
Alexey Fisher | 2cf313e | 2009-07-22 14:57:54 +0200 | [diff] [blame] | 1083 | snd_printk(KERN_INFO |
| 1084 | "set volume quirk for UDA1321/N101 chip\n"); |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 1085 | cval->max = -256; |
| 1086 | } |
Alexey Fisher | 2cf313e | 2009-07-22 14:57:54 +0200 | [diff] [blame] | 1087 | break; |
| 1088 | |
| 1089 | case USB_ID(0x046d, 0x09a4): |
| 1090 | if (!strcmp(kctl->id.name, "Mic Capture Volume")) { |
| 1091 | snd_printk(KERN_INFO |
| 1092 | "set volume quirk for QuickCam E3500\n"); |
| 1093 | cval->min = 6080; |
| 1094 | cval->max = 8768; |
| 1095 | cval->res = 192; |
| 1096 | } |
| 1097 | break; |
| 1098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n", |
| 1102 | cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1103 | add_control_to_empty(state, kctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | /* |
| 1109 | * parse a feature unit |
| 1110 | * |
| 1111 | * most of controlls are defined here. |
| 1112 | */ |
Daniel Mack | 28e1b77 | 2010-02-22 23:49:09 +0100 | [diff] [blame] | 1113 | static int parse_audio_feature_unit(struct mixer_build *state, int unitid, void *_ftr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | { |
| 1115 | int channels, i, j; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1116 | struct usb_audio_term iterm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | unsigned int master_bits, first_ch_bits; |
| 1118 | int err, csize; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1119 | struct uac_feature_unit_descriptor *hdr = _ftr; |
| 1120 | __u8 *bmaControls; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1122 | if (state->mixer->protocol == UAC_VERSION_1) { |
| 1123 | csize = hdr->bControlSize; |
| 1124 | channels = (hdr->bLength - 7) / csize - 1; |
| 1125 | bmaControls = hdr->bmaControls; |
| 1126 | } else { |
| 1127 | struct uac2_feature_unit_descriptor *ftr = _ftr; |
| 1128 | csize = 4; |
Daniel Mack | e8d0fee | 2010-05-27 20:15:14 +0200 | [diff] [blame] | 1129 | channels = (hdr->bLength - 6) / 4 - 1; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1130 | bmaControls = ftr->bmaControls; |
| 1131 | } |
| 1132 | |
| 1133 | if (hdr->bLength < 7 || !csize || hdr->bLength < 7 + csize) { |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1134 | snd_printk(KERN_ERR "usbaudio: unit %u: invalid UAC_FEATURE_UNIT descriptor\n", unitid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | return -EINVAL; |
| 1136 | } |
| 1137 | |
| 1138 | /* parse the source unit */ |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1139 | if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | return err; |
| 1141 | |
| 1142 | /* determine the input source type and name */ |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1143 | if (check_input_term(state, hdr->bSourceID, &iterm) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | return -EINVAL; |
| 1145 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1146 | master_bits = snd_usb_combine_bytes(bmaControls, csize); |
Javier Kohen | 0c3cee5 | 2009-11-17 15:36:13 +0100 | [diff] [blame] | 1147 | /* master configuration quirks */ |
| 1148 | switch (state->chip->usb_id) { |
| 1149 | case USB_ID(0x08bb, 0x2702): |
| 1150 | snd_printk(KERN_INFO |
| 1151 | "usbmixer: master volume quirk for PCM2702 chip\n"); |
| 1152 | /* disable non-functional volume control */ |
Daniel Mack | 45d7605 | 2010-03-11 21:13:21 +0100 | [diff] [blame] | 1153 | master_bits &= ~UAC_FU_VOLUME; |
Javier Kohen | 0c3cee5 | 2009-11-17 15:36:13 +0100 | [diff] [blame] | 1154 | break; |
| 1155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | if (channels > 0) |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1157 | first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | else |
| 1159 | first_ch_bits = 0; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1160 | |
| 1161 | if (state->mixer->protocol == UAC_VERSION_1) { |
| 1162 | /* check all control types */ |
| 1163 | for (i = 0; i < 10; i++) { |
| 1164 | unsigned int ch_bits = 0; |
| 1165 | for (j = 0; j < channels; j++) { |
| 1166 | unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize); |
| 1167 | if (mask & (1 << i)) |
| 1168 | ch_bits |= (1 << j); |
| 1169 | } |
| 1170 | /* audio class v1 controls are never read-only */ |
| 1171 | if (ch_bits & 1) /* the first channel must be set (for ease of programming) */ |
| 1172 | build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, 0); |
| 1173 | if (master_bits & (1 << i)) |
| 1174 | build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | } |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1176 | } else { /* UAC_VERSION_2 */ |
| 1177 | for (i = 0; i < 30/2; i++) { |
| 1178 | /* From the USB Audio spec v2.0: |
| 1179 | bmaControls() is a (ch+1)-element array of 4-byte bitmaps, |
| 1180 | each containing a set of bit pairs. If a Control is present, |
| 1181 | it must be Host readable. If a certain Control is not |
| 1182 | present then the bit pair must be set to 0b00. |
| 1183 | If a Control is present but read-only, the bit pair must be |
| 1184 | set to 0b01. If a Control is also Host programmable, the bit |
| 1185 | pair must be set to 0b11. The value 0b10 is not allowed. */ |
| 1186 | unsigned int ch_bits = 0; |
| 1187 | unsigned int ch_read_only = 0; |
| 1188 | |
| 1189 | for (j = 0; j < channels; j++) { |
| 1190 | unsigned int mask = snd_usb_combine_bytes(bmaControls + csize * (j+1), csize); |
| 1191 | if (mask & (1 << (i * 2))) { |
| 1192 | ch_bits |= (1 << j); |
| 1193 | if (~mask & (1 << ((i * 2) + 1))) |
| 1194 | ch_read_only |= (1 << j); |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | /* FIXME: the whole unit is read-only if any of the channels is marked read-only */ |
| 1199 | if (ch_bits & 1) /* the first channel must be set (for ease of programming) */ |
| 1200 | build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid, !!ch_read_only); |
| 1201 | if (master_bits & (1 << i * 2)) |
| 1202 | build_feature_ctl(state, _ftr, 0, i, &iterm, unitid, |
| 1203 | ~master_bits & (1 << ((i * 2) + 1))); |
| 1204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | |
| 1211 | /* |
| 1212 | * Mixer Unit |
| 1213 | */ |
| 1214 | |
| 1215 | /* |
| 1216 | * build a mixer unit control |
| 1217 | * |
| 1218 | * the callbacks are identical with feature unit. |
| 1219 | * input channel number (zero based) is given in control field instead. |
| 1220 | */ |
| 1221 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1222 | static void build_mixer_unit_ctl(struct mixer_build *state, |
| 1223 | struct uac_mixer_unit_descriptor *desc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | int in_pin, int in_ch, int unitid, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1225 | struct usb_audio_term *iterm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1227 | struct usb_mixer_elem_info *cval; |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1228 | unsigned int num_outs = uac_mixer_unit_bNrChannels(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | unsigned int i, len; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1230 | struct snd_kcontrol *kctl; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1231 | const struct usbmix_name_map *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1233 | map = find_map(state, unitid, 0); |
| 1234 | if (check_ignored_ctl(map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | return; |
| 1236 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 1237 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | if (! cval) |
| 1239 | return; |
| 1240 | |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1241 | cval->mixer = state->mixer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | cval->id = unitid; |
| 1243 | cval->control = in_ch + 1; /* based on 1 */ |
| 1244 | cval->val_type = USB_MIXER_S16; |
| 1245 | for (i = 0; i < num_outs; i++) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1246 | if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol), in_ch, i, num_outs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | cval->cmask |= (1 << i); |
| 1248 | cval->channels++; |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | /* get min/max values */ |
| 1253 | get_min_max(cval, 0); |
| 1254 | |
| 1255 | kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval); |
| 1256 | if (! kctl) { |
| 1257 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); |
| 1258 | kfree(cval); |
| 1259 | return; |
| 1260 | } |
| 1261 | kctl->private_free = usb_mixer_elem_free; |
| 1262 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1263 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | if (! len) |
| 1265 | len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0); |
| 1266 | if (! len) |
| 1267 | len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1); |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 1268 | append_ctl_name(kctl, " Volume"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | |
| 1270 | snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n", |
| 1271 | cval->id, kctl->id.name, cval->channels, cval->min, cval->max); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1272 | add_control_to_empty(state, kctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | |
| 1276 | /* |
| 1277 | * parse a mixer unit |
| 1278 | */ |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1279 | static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, void *raw_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1281 | struct uac_mixer_unit_descriptor *desc = raw_desc; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1282 | struct usb_audio_term iterm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | int input_pins, num_ins, num_outs; |
| 1284 | int pin, ich, err; |
| 1285 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1286 | if (desc->bLength < 11 || ! (input_pins = desc->bNrInPins) || ! (num_outs = uac_mixer_unit_bNrChannels(desc))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid); |
| 1288 | return -EINVAL; |
| 1289 | } |
| 1290 | /* no bmControls field (e.g. Maya44) -> ignore */ |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1291 | if (desc->bLength <= 10 + input_pins) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid); |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | num_ins = 0; |
| 1297 | ich = 0; |
| 1298 | for (pin = 0; pin < input_pins; pin++) { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1299 | err = parse_audio_unit(state, desc->baSourceID[pin]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | if (err < 0) |
| 1301 | return err; |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1302 | err = check_input_term(state, desc->baSourceID[pin], &iterm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | if (err < 0) |
| 1304 | return err; |
| 1305 | num_ins += iterm.channels; |
| 1306 | for (; ich < num_ins; ++ich) { |
| 1307 | int och, ich_has_controls = 0; |
| 1308 | |
| 1309 | for (och = 0; och < num_outs; ++och) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1310 | if (check_matrix_bitmap(uac_mixer_unit_bmControls(desc, state->mixer->protocol), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | ich, och, num_outs)) { |
| 1312 | ich_has_controls = 1; |
| 1313 | break; |
| 1314 | } |
| 1315 | } |
| 1316 | if (ich_has_controls) |
| 1317 | build_mixer_unit_ctl(state, desc, pin, ich, |
| 1318 | unitid, &iterm); |
| 1319 | } |
| 1320 | } |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
| 1324 | |
| 1325 | /* |
| 1326 | * Processing Unit / Extension Unit |
| 1327 | */ |
| 1328 | |
| 1329 | /* get callback for processing/extension unit */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1330 | static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1332 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | int err, val; |
| 1334 | |
| 1335 | err = get_cur_ctl_value(cval, cval->control << 8, &val); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1336 | if (err < 0 && cval->mixer->ignore_ctl_error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | ucontrol->value.integer.value[0] = cval->min; |
| 1338 | return 0; |
| 1339 | } |
| 1340 | if (err < 0) |
| 1341 | return err; |
| 1342 | val = get_relative_value(cval, val); |
| 1343 | ucontrol->value.integer.value[0] = val; |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | /* put callback for processing/extension unit */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1348 | static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1350 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | int val, oval, err; |
| 1352 | |
| 1353 | err = get_cur_ctl_value(cval, cval->control << 8, &oval); |
| 1354 | if (err < 0) { |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1355 | if (cval->mixer->ignore_ctl_error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | return 0; |
| 1357 | return err; |
| 1358 | } |
| 1359 | val = ucontrol->value.integer.value[0]; |
| 1360 | val = get_abs_value(cval, val); |
| 1361 | if (val != oval) { |
| 1362 | set_cur_ctl_value(cval, cval->control << 8, val); |
| 1363 | return 1; |
| 1364 | } |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | /* alsa control interface for processing/extension unit */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1369 | static struct snd_kcontrol_new mixer_procunit_ctl = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1371 | .name = "", /* will be filled later */ |
| 1372 | .info = mixer_ctl_feature_info, |
| 1373 | .get = mixer_ctl_procunit_get, |
| 1374 | .put = mixer_ctl_procunit_put, |
| 1375 | }; |
| 1376 | |
| 1377 | |
| 1378 | /* |
| 1379 | * predefined data for processing units |
| 1380 | */ |
| 1381 | struct procunit_value_info { |
| 1382 | int control; |
| 1383 | char *suffix; |
| 1384 | int val_type; |
| 1385 | int min_value; |
| 1386 | }; |
| 1387 | |
| 1388 | struct procunit_info { |
| 1389 | int type; |
| 1390 | char *name; |
| 1391 | struct procunit_value_info *values; |
| 1392 | }; |
| 1393 | |
| 1394 | static struct procunit_value_info updown_proc_info[] = { |
| 1395 | { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN }, |
| 1396 | { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 }, |
| 1397 | { 0 } |
| 1398 | }; |
| 1399 | static struct procunit_value_info prologic_proc_info[] = { |
| 1400 | { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN }, |
| 1401 | { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 }, |
| 1402 | { 0 } |
| 1403 | }; |
| 1404 | static struct procunit_value_info threed_enh_proc_info[] = { |
| 1405 | { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN }, |
| 1406 | { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 }, |
| 1407 | { 0 } |
| 1408 | }; |
| 1409 | static struct procunit_value_info reverb_proc_info[] = { |
| 1410 | { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN }, |
| 1411 | { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 }, |
| 1412 | { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 }, |
| 1413 | { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 }, |
| 1414 | { 0 } |
| 1415 | }; |
| 1416 | static struct procunit_value_info chorus_proc_info[] = { |
| 1417 | { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN }, |
| 1418 | { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 }, |
| 1419 | { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 }, |
| 1420 | { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 }, |
| 1421 | { 0 } |
| 1422 | }; |
| 1423 | static struct procunit_value_info dcr_proc_info[] = { |
| 1424 | { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN }, |
| 1425 | { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 }, |
| 1426 | { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 }, |
| 1427 | { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 }, |
| 1428 | { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 }, |
| 1429 | { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 }, |
| 1430 | { 0 } |
| 1431 | }; |
| 1432 | |
| 1433 | static struct procunit_info procunits[] = { |
| 1434 | { USB_PROC_UPDOWN, "Up Down", updown_proc_info }, |
| 1435 | { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info }, |
| 1436 | { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info }, |
| 1437 | { USB_PROC_REVERB, "Reverb", reverb_proc_info }, |
| 1438 | { USB_PROC_CHORUS, "Chorus", chorus_proc_info }, |
| 1439 | { USB_PROC_DCR, "DCR", dcr_proc_info }, |
| 1440 | { 0 }, |
| 1441 | }; |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame] | 1442 | /* |
| 1443 | * predefined data for extension units |
| 1444 | */ |
| 1445 | static struct procunit_value_info clock_rate_xu_info[] = { |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 1446 | { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 }, |
| 1447 | { 0 } |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame] | 1448 | }; |
| 1449 | static struct procunit_value_info clock_source_xu_info[] = { |
| 1450 | { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN }, |
| 1451 | { 0 } |
| 1452 | }; |
| 1453 | static struct procunit_value_info spdif_format_xu_info[] = { |
| 1454 | { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN }, |
| 1455 | { 0 } |
| 1456 | }; |
| 1457 | static struct procunit_value_info soft_limit_xu_info[] = { |
| 1458 | { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN }, |
| 1459 | { 0 } |
| 1460 | }; |
| 1461 | static struct procunit_info extunits[] = { |
| 1462 | { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info }, |
| 1463 | { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info }, |
| 1464 | { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info }, |
| 1465 | { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info }, |
| 1466 | { 0 } |
| 1467 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | /* |
| 1469 | * build a processing/extension unit |
| 1470 | */ |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1471 | static int build_audio_procunit(struct mixer_build *state, int unitid, void *raw_desc, struct procunit_info *list, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1473 | struct uac_processing_unit_descriptor *desc = raw_desc; |
| 1474 | int num_ins = desc->bNrInPins; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1475 | struct usb_mixer_elem_info *cval; |
| 1476 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | int i, err, nameid, type, len; |
| 1478 | struct procunit_info *info; |
| 1479 | struct procunit_value_info *valinfo; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1480 | const struct usbmix_name_map *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | static struct procunit_value_info default_value_info[] = { |
| 1482 | { 0x01, "Switch", USB_MIXER_BOOLEAN }, |
| 1483 | { 0 } |
| 1484 | }; |
| 1485 | static struct procunit_info default_info = { |
| 1486 | 0, NULL, default_value_info |
| 1487 | }; |
| 1488 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1489 | if (desc->bLength < 13 || desc->bLength < 13 + num_ins || |
| 1490 | desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid); |
| 1492 | return -EINVAL; |
| 1493 | } |
| 1494 | |
| 1495 | for (i = 0; i < num_ins; i++) { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1496 | if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | return err; |
| 1498 | } |
| 1499 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1500 | type = le16_to_cpu(desc->wProcessType); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | for (info = list; info && info->type; info++) |
| 1502 | if (info->type == type) |
| 1503 | break; |
| 1504 | if (! info || ! info->type) |
| 1505 | info = &default_info; |
| 1506 | |
| 1507 | for (valinfo = info->values; valinfo->control; valinfo++) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1508 | __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol); |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1509 | |
| 1510 | if (! (controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | continue; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1512 | map = find_map(state, unitid, valinfo->control); |
| 1513 | if (check_ignored_ctl(map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | continue; |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 1515 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | if (! cval) { |
| 1517 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); |
| 1518 | return -ENOMEM; |
| 1519 | } |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1520 | cval->mixer = state->mixer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | cval->id = unitid; |
| 1522 | cval->control = valinfo->control; |
| 1523 | cval->val_type = valinfo->val_type; |
| 1524 | cval->channels = 1; |
| 1525 | |
| 1526 | /* get min/max values */ |
| 1527 | if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1528 | __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | /* FIXME: hard-coded */ |
| 1530 | cval->min = 1; |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1531 | cval->max = control_spec[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | cval->res = 1; |
| 1533 | cval->initialized = 1; |
Sergiy Kovalchuk | 7d2b451 | 2009-12-27 09:13:41 -0800 | [diff] [blame] | 1534 | } else { |
| 1535 | if (type == USB_XU_CLOCK_RATE) { |
| 1536 | /* E-Mu USB 0404/0202/TrackerPre |
| 1537 | * samplerate control quirk |
| 1538 | */ |
| 1539 | cval->min = 0; |
| 1540 | cval->max = 5; |
| 1541 | cval->res = 1; |
| 1542 | cval->initialized = 1; |
| 1543 | } else |
| 1544 | get_min_max(cval, valinfo->min_value); |
| 1545 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | |
| 1547 | kctl = snd_ctl_new1(&mixer_procunit_ctl, cval); |
| 1548 | if (! kctl) { |
| 1549 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); |
| 1550 | kfree(cval); |
| 1551 | return -ENOMEM; |
| 1552 | } |
| 1553 | kctl->private_free = usb_mixer_elem_free; |
| 1554 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1555 | if (check_mapped_name(map, kctl->id.name, |
| 1556 | sizeof(kctl->id.name))) |
| 1557 | /* nothing */ ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | else if (info->name) |
| 1559 | strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name)); |
| 1560 | else { |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1561 | nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | len = 0; |
| 1563 | if (nameid) |
| 1564 | len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name)); |
| 1565 | if (! len) |
| 1566 | strlcpy(kctl->id.name, name, sizeof(kctl->id.name)); |
| 1567 | } |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 1568 | append_ctl_name(kctl, " "); |
| 1569 | append_ctl_name(kctl, valinfo->suffix); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | |
| 1571 | snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n", |
| 1572 | cval->id, kctl->id.name, cval->channels, cval->min, cval->max); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1573 | if ((err = add_control_to_empty(state, kctl)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | return err; |
| 1575 | } |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
| 1579 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1580 | static int parse_audio_processing_unit(struct mixer_build *state, int unitid, void *raw_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1582 | return build_audio_procunit(state, unitid, raw_desc, procunits, "Processing Unit"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1585 | static int parse_audio_extension_unit(struct mixer_build *state, int unitid, void *raw_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1587 | /* Note that we parse extension units with processing unit descriptors. |
| 1588 | * That's ok as the layout is the same */ |
| 1589 | return build_audio_procunit(state, unitid, raw_desc, extunits, "Extension Unit"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | |
| 1593 | /* |
| 1594 | * Selector Unit |
| 1595 | */ |
| 1596 | |
| 1597 | /* info callback for selector unit |
| 1598 | * use an enumerator type for routing |
| 1599 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1600 | static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1602 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | char **itemlist = (char **)kcontrol->private_value; |
| 1604 | |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 1605 | if (snd_BUG_ON(!itemlist)) |
| 1606 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1608 | uinfo->count = 1; |
| 1609 | uinfo->value.enumerated.items = cval->max; |
| 1610 | if ((int)uinfo->value.enumerated.item >= cval->max) |
| 1611 | uinfo->value.enumerated.item = cval->max - 1; |
| 1612 | strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]); |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | /* get callback for selector unit */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1617 | static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1619 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | int val, err; |
| 1621 | |
| 1622 | err = get_cur_ctl_value(cval, 0, &val); |
| 1623 | if (err < 0) { |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1624 | if (cval->mixer->ignore_ctl_error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | ucontrol->value.enumerated.item[0] = 0; |
| 1626 | return 0; |
| 1627 | } |
| 1628 | return err; |
| 1629 | } |
| 1630 | val = get_relative_value(cval, val); |
| 1631 | ucontrol->value.enumerated.item[0] = val; |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
| 1635 | /* put callback for selector unit */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1636 | static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1638 | struct usb_mixer_elem_info *cval = kcontrol->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | int val, oval, err; |
| 1640 | |
| 1641 | err = get_cur_ctl_value(cval, 0, &oval); |
| 1642 | if (err < 0) { |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1643 | if (cval->mixer->ignore_ctl_error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | return 0; |
| 1645 | return err; |
| 1646 | } |
| 1647 | val = ucontrol->value.enumerated.item[0]; |
| 1648 | val = get_abs_value(cval, val); |
| 1649 | if (val != oval) { |
| 1650 | set_cur_ctl_value(cval, 0, val); |
| 1651 | return 1; |
| 1652 | } |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | /* alsa control interface for selector unit */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1657 | static struct snd_kcontrol_new mixer_selectunit_ctl = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1659 | .name = "", /* will be filled later */ |
| 1660 | .info = mixer_ctl_selector_info, |
| 1661 | .get = mixer_ctl_selector_get, |
| 1662 | .put = mixer_ctl_selector_put, |
| 1663 | }; |
| 1664 | |
| 1665 | |
| 1666 | /* private free callback. |
| 1667 | * free both private_data and private_value |
| 1668 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1669 | static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | { |
| 1671 | int i, num_ins = 0; |
| 1672 | |
| 1673 | if (kctl->private_data) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1674 | struct usb_mixer_elem_info *cval = kctl->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | num_ins = cval->max; |
| 1676 | kfree(cval); |
| 1677 | kctl->private_data = NULL; |
| 1678 | } |
| 1679 | if (kctl->private_value) { |
| 1680 | char **itemlist = (char **)kctl->private_value; |
| 1681 | for (i = 0; i < num_ins; i++) |
| 1682 | kfree(itemlist[i]); |
| 1683 | kfree(itemlist); |
| 1684 | kctl->private_value = 0; |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | * parse a selector unit |
| 1690 | */ |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1691 | static int parse_audio_selector_unit(struct mixer_build *state, int unitid, void *raw_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | { |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1693 | struct uac_selector_unit_descriptor *desc = raw_desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | unsigned int i, nameid, len; |
| 1695 | int err; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1696 | struct usb_mixer_elem_info *cval; |
| 1697 | struct snd_kcontrol *kctl; |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1698 | const struct usbmix_name_map *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | char **namelist; |
| 1700 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1701 | if (!desc->bNrInPins || desc->bLength < 5 + desc->bNrInPins) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid); |
| 1703 | return -EINVAL; |
| 1704 | } |
| 1705 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1706 | for (i = 0; i < desc->bNrInPins; i++) { |
| 1707 | if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | return err; |
| 1709 | } |
| 1710 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1711 | if (desc->bNrInPins == 1) /* only one ? nonsense! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | return 0; |
| 1713 | |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1714 | map = find_map(state, unitid, 0); |
| 1715 | if (check_ignored_ctl(map)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | return 0; |
| 1717 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 1718 | cval = kzalloc(sizeof(*cval), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | if (! cval) { |
| 1720 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); |
| 1721 | return -ENOMEM; |
| 1722 | } |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1723 | cval->mixer = state->mixer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | cval->id = unitid; |
| 1725 | cval->val_type = USB_MIXER_U8; |
| 1726 | cval->channels = 1; |
| 1727 | cval->min = 1; |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1728 | cval->max = desc->bNrInPins; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | cval->res = 1; |
| 1730 | cval->initialized = 1; |
| 1731 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1732 | namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | if (! namelist) { |
| 1734 | snd_printk(KERN_ERR "cannot malloc\n"); |
| 1735 | kfree(cval); |
| 1736 | return -ENOMEM; |
| 1737 | } |
| 1738 | #define MAX_ITEM_NAME_LEN 64 |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1739 | for (i = 0; i < desc->bNrInPins; i++) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1740 | struct usb_audio_term iterm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | len = 0; |
| 1742 | namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL); |
| 1743 | if (! namelist[i]) { |
| 1744 | snd_printk(KERN_ERR "cannot malloc\n"); |
Mariusz Kozlowski | 7fbe3ca | 2007-01-08 11:25:30 +0100 | [diff] [blame] | 1745 | while (i--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | kfree(namelist[i]); |
| 1747 | kfree(namelist); |
| 1748 | kfree(cval); |
| 1749 | return -ENOMEM; |
| 1750 | } |
Clemens Ladisch | 8e062ec | 2005-04-22 15:49:52 +0200 | [diff] [blame] | 1751 | len = check_mapped_selector_name(state, unitid, i, namelist[i], |
| 1752 | MAX_ITEM_NAME_LEN); |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1753 | if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0); |
| 1755 | if (! len) |
| 1756 | sprintf(namelist[i], "Input %d", i); |
| 1757 | } |
| 1758 | |
| 1759 | kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval); |
| 1760 | if (! kctl) { |
| 1761 | snd_printk(KERN_ERR "cannot malloc kcontrol\n"); |
Jesper Juhl | 878b478 | 2006-03-20 11:27:13 +0100 | [diff] [blame] | 1762 | kfree(namelist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | kfree(cval); |
| 1764 | return -ENOMEM; |
| 1765 | } |
| 1766 | kctl->private_value = (unsigned long)namelist; |
| 1767 | kctl->private_free = usb_mixer_selector_elem_free; |
| 1768 | |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1769 | nameid = uac_selector_unit_iSelector(desc); |
Jaroslav Kysela | c3a3e04 | 2010-02-11 17:50:44 +0100 | [diff] [blame] | 1770 | len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | if (len) |
| 1772 | ; |
| 1773 | else if (nameid) |
| 1774 | snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name)); |
| 1775 | else { |
| 1776 | len = get_term_name(state, &state->oterm, |
| 1777 | kctl->id.name, sizeof(kctl->id.name), 0); |
| 1778 | if (! len) |
| 1779 | strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name)); |
| 1780 | |
| 1781 | if ((state->oterm.type & 0xff00) == 0x0100) |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 1782 | append_ctl_name(kctl, " Capture Source"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | else |
Takashi Iwai | 08d1e63 | 2009-10-02 14:06:08 +0200 | [diff] [blame] | 1784 | append_ctl_name(kctl, " Playback Source"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n", |
Daniel Mack | 99fc864 | 2010-03-11 21:13:24 +0100 | [diff] [blame] | 1788 | cval->id, kctl->id.name, desc->bNrInPins); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1789 | if ((err = add_control_to_empty(state, kctl)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | return err; |
| 1791 | |
| 1792 | return 0; |
| 1793 | } |
| 1794 | |
| 1795 | |
| 1796 | /* |
| 1797 | * parse an audio unit recursively |
| 1798 | */ |
| 1799 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1800 | static int parse_audio_unit(struct mixer_build *state, int unitid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | { |
| 1802 | unsigned char *p1; |
| 1803 | |
| 1804 | if (test_and_set_bit(unitid, state->unitbitmap)) |
| 1805 | return 0; /* the unit already visited */ |
| 1806 | |
| 1807 | p1 = find_audio_control_unit(state, unitid); |
| 1808 | if (!p1) { |
| 1809 | snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid); |
| 1810 | return -EINVAL; |
| 1811 | } |
| 1812 | |
| 1813 | switch (p1[2]) { |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1814 | case UAC_INPUT_TERMINAL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | return 0; /* NOP */ |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1816 | case UAC_MIXER_UNIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | return parse_audio_mixer_unit(state, unitid, p1); |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1818 | case UAC_SELECTOR_UNIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | return parse_audio_selector_unit(state, unitid, p1); |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1820 | case UAC_FEATURE_UNIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | return parse_audio_feature_unit(state, unitid, p1); |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1822 | case UAC_PROCESSING_UNIT_V1: |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1823 | /* UAC2_EFFECT_UNIT has the same value */ |
| 1824 | if (state->mixer->protocol == UAC_VERSION_1) |
| 1825 | return parse_audio_processing_unit(state, unitid, p1); |
| 1826 | else |
| 1827 | return 0; /* FIXME - effect units not implemented yet */ |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1828 | case UAC_EXTENSION_UNIT_V1: |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1829 | /* UAC2_PROCESSING_UNIT_V2 has the same value */ |
| 1830 | if (state->mixer->protocol == UAC_VERSION_1) |
| 1831 | return parse_audio_extension_unit(state, unitid, p1); |
| 1832 | else /* UAC_VERSION_2 */ |
| 1833 | return parse_audio_processing_unit(state, unitid, p1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | default: |
| 1835 | snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]); |
| 1836 | return -EINVAL; |
| 1837 | } |
| 1838 | } |
| 1839 | |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1840 | static void snd_usb_mixer_free(struct usb_mixer_interface *mixer) |
| 1841 | { |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 1842 | kfree(mixer->id_elems); |
| 1843 | if (mixer->urb) { |
| 1844 | kfree(mixer->urb->transfer_buffer); |
| 1845 | usb_free_urb(mixer->urb); |
| 1846 | } |
Mariusz Kozlowski | 68df9de | 2006-11-08 15:37:04 +0100 | [diff] [blame] | 1847 | usb_free_urb(mixer->rc_urb); |
Clemens Ladisch | b259b10 | 2005-04-29 16:29:28 +0200 | [diff] [blame] | 1848 | kfree(mixer->rc_setup_packet); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1849 | kfree(mixer); |
| 1850 | } |
| 1851 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1852 | static int snd_usb_mixer_dev_free(struct snd_device *device) |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1853 | { |
| 1854 | struct usb_mixer_interface *mixer = device->device_data; |
| 1855 | snd_usb_mixer_free(mixer); |
| 1856 | return 0; |
| 1857 | } |
| 1858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | /* |
| 1860 | * create mixer controls |
| 1861 | * |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1862 | * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | */ |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1864 | static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1866 | struct mixer_build state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | int err; |
| 1868 | const struct usbmix_ctl_map *map; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1869 | struct usb_host_interface *hostif; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1870 | void *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1872 | hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | memset(&state, 0, sizeof(state)); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1874 | state.chip = mixer->chip; |
| 1875 | state.mixer = mixer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | state.buffer = hostif->extra; |
| 1877 | state.buflen = hostif->extralen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | |
| 1879 | /* check the mapping table */ |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 1880 | for (map = usbmix_ctl_maps; map->id; map++) { |
| 1881 | if (map->id == state.chip->usb_id) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | state.map = map->map; |
Clemens Ladisch | 8e062ec | 2005-04-22 15:49:52 +0200 | [diff] [blame] | 1883 | state.selector_map = map->selector_map; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1884 | mixer->ignore_ctl_error = map->ignore_ctl_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | break; |
| 1886 | } |
| 1887 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1889 | p = NULL; |
| 1890 | while ((p = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, p, UAC_OUTPUT_TERMINAL)) != NULL) { |
| 1891 | if (mixer->protocol == UAC_VERSION_1) { |
| 1892 | struct uac_output_terminal_descriptor_v1 *desc = p; |
| 1893 | |
| 1894 | if (desc->bLength < sizeof(*desc)) |
| 1895 | continue; /* invalid descriptor? */ |
| 1896 | set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */ |
| 1897 | state.oterm.id = desc->bTerminalID; |
| 1898 | state.oterm.type = le16_to_cpu(desc->wTerminalType); |
| 1899 | state.oterm.name = desc->iTerminal; |
| 1900 | err = parse_audio_unit(&state, desc->bSourceID); |
| 1901 | if (err < 0) |
| 1902 | return err; |
| 1903 | } else { /* UAC_VERSION_2 */ |
| 1904 | struct uac2_output_terminal_descriptor *desc = p; |
| 1905 | |
| 1906 | if (desc->bLength < sizeof(*desc)) |
| 1907 | continue; /* invalid descriptor? */ |
| 1908 | set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */ |
| 1909 | state.oterm.id = desc->bTerminalID; |
| 1910 | state.oterm.type = le16_to_cpu(desc->wTerminalType); |
| 1911 | state.oterm.name = desc->iTerminal; |
| 1912 | err = parse_audio_unit(&state, desc->bSourceID); |
| 1913 | if (err < 0) |
| 1914 | return err; |
| 1915 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | } |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 1917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | return 0; |
| 1919 | } |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 1920 | |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 1921 | 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] | 1922 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1923 | struct usb_mixer_elem_info *info; |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 1924 | |
| 1925 | for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem) |
| 1926 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 1927 | info->elem_id); |
| 1928 | } |
| 1929 | |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 1930 | static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer, |
| 1931 | int unitid, |
| 1932 | struct usb_mixer_elem_info *cval) |
| 1933 | { |
| 1934 | static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN", |
| 1935 | "S8", "U8", "S16", "U16"}; |
| 1936 | snd_iprintf(buffer, " Unit: %i\n", unitid); |
| 1937 | if (cval->elem_id) |
| 1938 | snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n", |
| 1939 | cval->elem_id->name, cval->elem_id->index); |
| 1940 | snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, " |
| 1941 | "channels=%i, type=\"%s\"\n", cval->id, |
| 1942 | cval->control, cval->cmask, cval->channels, |
| 1943 | val_types[cval->val_type]); |
| 1944 | snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n", |
| 1945 | cval->min, cval->max, cval->dBmin, cval->dBmax); |
| 1946 | } |
| 1947 | |
| 1948 | static void snd_usb_mixer_proc_read(struct snd_info_entry *entry, |
| 1949 | struct snd_info_buffer *buffer) |
| 1950 | { |
| 1951 | struct snd_usb_audio *chip = entry->private_data; |
| 1952 | struct usb_mixer_interface *mixer; |
| 1953 | struct usb_mixer_elem_info *cval; |
| 1954 | int unitid; |
| 1955 | |
| 1956 | list_for_each_entry(mixer, &chip->mixer_list, list) { |
| 1957 | snd_iprintf(buffer, |
Jaroslav Kysela | 7affdc1 | 2010-02-16 11:52:27 +0100 | [diff] [blame] | 1958 | "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n", |
| 1959 | chip->usb_id, mixer->ctrlif, |
| 1960 | mixer->ignore_ctl_error); |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 1961 | snd_iprintf(buffer, "Card: %s\n", chip->card->longname); |
| 1962 | for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) { |
| 1963 | for (cval = mixer->id_elems[unitid]; cval; |
| 1964 | cval = cval->next_id_elem) |
| 1965 | snd_usb_mixer_dump_cval(buffer, unitid, cval); |
| 1966 | } |
| 1967 | } |
| 1968 | } |
| 1969 | |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 1970 | static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer, |
| 1971 | int attribute, int value, int index) |
| 1972 | { |
| 1973 | struct usb_mixer_elem_info *info; |
| 1974 | __u8 unitid = (index >> 8) & 0xff; |
| 1975 | __u8 control = (value >> 8) & 0xff; |
| 1976 | __u8 channel = value & 0xff; |
| 1977 | |
| 1978 | if (channel >= MAX_CHANNELS) { |
| 1979 | snd_printk(KERN_DEBUG "%s(): bogus channel number %d\n", |
| 1980 | __func__, channel); |
| 1981 | return; |
| 1982 | } |
| 1983 | |
| 1984 | for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem) { |
| 1985 | if (info->control != control) |
| 1986 | continue; |
| 1987 | |
| 1988 | switch (attribute) { |
| 1989 | case UAC2_CS_CUR: |
| 1990 | /* invalidate cache, so the value is read from the device */ |
| 1991 | if (channel) |
| 1992 | info->cached &= ~(1 << channel); |
| 1993 | else /* master channel */ |
| 1994 | info->cached = 0; |
| 1995 | |
| 1996 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 1997 | info->elem_id); |
| 1998 | break; |
| 1999 | |
| 2000 | case UAC2_CS_RANGE: |
| 2001 | /* TODO */ |
| 2002 | break; |
| 2003 | |
| 2004 | case UAC2_CS_MEM: |
| 2005 | /* TODO */ |
| 2006 | break; |
| 2007 | |
| 2008 | default: |
| 2009 | snd_printk(KERN_DEBUG "unknown attribute %d in interrupt\n", |
| 2010 | attribute); |
| 2011 | break; |
| 2012 | } /* switch */ |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | static void snd_usb_mixer_interrupt(struct urb *urb) |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2017 | { |
| 2018 | struct usb_mixer_interface *mixer = urb->context; |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2019 | int len = urb->actual_length; |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2020 | |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2021 | if (urb->status != 0) |
| 2022 | goto requeue; |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2023 | |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2024 | if (mixer->protocol == UAC_VERSION_1) { |
| 2025 | struct uac1_status_word *status; |
| 2026 | |
| 2027 | for (status = urb->transfer_buffer; |
| 2028 | len >= sizeof(*status); |
| 2029 | len -= sizeof(*status), status++) { |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2030 | snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n", |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2031 | status->bStatusType, |
| 2032 | status->bOriginator); |
| 2033 | |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2034 | /* ignore any notifications not from the control interface */ |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2035 | if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) != |
| 2036 | UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF) |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2037 | continue; |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2038 | |
| 2039 | if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED) |
| 2040 | snd_usb_mixer_rc_memory_change(mixer, status->bOriginator); |
Clemens Ladisch | b259b10 | 2005-04-29 16:29:28 +0200 | [diff] [blame] | 2041 | else |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2042 | snd_usb_mixer_notify_id(mixer, status->bOriginator); |
| 2043 | } |
| 2044 | } else { /* UAC_VERSION_2 */ |
| 2045 | struct uac2_interrupt_data_msg *msg; |
| 2046 | |
| 2047 | for (msg = urb->transfer_buffer; |
| 2048 | len >= sizeof(*msg); |
| 2049 | len -= sizeof(*msg), msg++) { |
| 2050 | /* drop vendor specific and endpoint requests */ |
| 2051 | if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) || |
| 2052 | (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP)) |
| 2053 | continue; |
| 2054 | |
| 2055 | snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute, |
| 2056 | le16_to_cpu(msg->wValue), |
| 2057 | le16_to_cpu(msg->wIndex)); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2058 | } |
| 2059 | } |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2060 | |
| 2061 | requeue: |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2062 | if (urb->status != -ENOENT && urb->status != -ECONNRESET) { |
| 2063 | urb->dev = mixer->chip->dev; |
| 2064 | usb_submit_urb(urb, GFP_ATOMIC); |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | /* create the handler for the optional status interrupt endpoint */ |
| 2069 | static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer) |
| 2070 | { |
| 2071 | struct usb_host_interface *hostif; |
| 2072 | struct usb_endpoint_descriptor *ep; |
| 2073 | void *transfer_buffer; |
| 2074 | int buffer_length; |
| 2075 | unsigned int epnum; |
| 2076 | |
| 2077 | hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0]; |
| 2078 | /* we need one interrupt input endpoint */ |
| 2079 | if (get_iface_desc(hostif)->bNumEndpoints < 1) |
| 2080 | return 0; |
| 2081 | ep = get_endpoint(hostif, 0); |
Julia Lawall | 913ae5a | 2009-01-03 17:54:53 +0100 | [diff] [blame] | 2082 | if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep)) |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2083 | return 0; |
| 2084 | |
Julia Lawall | 42a6e66 | 2008-12-29 11:23:02 +0100 | [diff] [blame] | 2085 | epnum = usb_endpoint_num(ep); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2086 | buffer_length = le16_to_cpu(ep->wMaxPacketSize); |
| 2087 | transfer_buffer = kmalloc(buffer_length, GFP_KERNEL); |
| 2088 | if (!transfer_buffer) |
| 2089 | return -ENOMEM; |
| 2090 | mixer->urb = usb_alloc_urb(0, GFP_KERNEL); |
| 2091 | if (!mixer->urb) { |
| 2092 | kfree(transfer_buffer); |
| 2093 | return -ENOMEM; |
| 2094 | } |
| 2095 | usb_fill_int_urb(mixer->urb, mixer->chip->dev, |
| 2096 | usb_rcvintpipe(mixer->chip->dev, epnum), |
| 2097 | transfer_buffer, buffer_length, |
Daniel Mack | e213e9c | 2010-05-11 18:13:50 +0200 | [diff] [blame] | 2098 | snd_usb_mixer_interrupt, mixer, ep->bInterval); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2099 | usb_submit_urb(mixer->urb, GFP_KERNEL); |
| 2100 | return 0; |
| 2101 | } |
| 2102 | |
Takashi Iwai | 7a9b806 | 2008-08-13 15:40:53 +0200 | [diff] [blame] | 2103 | int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif, |
| 2104 | int ignore_error) |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2105 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2106 | static struct snd_device_ops dev_ops = { |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2107 | .dev_free = snd_usb_mixer_dev_free |
| 2108 | }; |
| 2109 | struct usb_mixer_interface *mixer; |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 2110 | struct snd_info_entry *entry; |
Daniel Mack | 7b8a043 | 2010-02-22 23:49:12 +0100 | [diff] [blame] | 2111 | struct usb_host_interface *host_iface; |
Daniel Mack | 23caaf1 | 2010-03-11 21:13:25 +0100 | [diff] [blame] | 2112 | int err; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2113 | |
| 2114 | strcpy(chip->card->mixername, "USB Mixer"); |
| 2115 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 2116 | mixer = kzalloc(sizeof(*mixer), GFP_KERNEL); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2117 | if (!mixer) |
| 2118 | return -ENOMEM; |
| 2119 | mixer->chip = chip; |
| 2120 | mixer->ctrlif = ctrlif; |
Takashi Iwai | 7a9b806 | 2008-08-13 15:40:53 +0200 | [diff] [blame] | 2121 | mixer->ignore_ctl_error = ignore_error; |
Jaroslav Kysela | 291186e | 2010-02-16 11:55:18 +0100 | [diff] [blame] | 2122 | mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems), |
| 2123 | GFP_KERNEL); |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2124 | if (!mixer->id_elems) { |
| 2125 | kfree(mixer); |
| 2126 | return -ENOMEM; |
| 2127 | } |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2128 | |
Daniel Mack | 7b8a043 | 2010-02-22 23:49:12 +0100 | [diff] [blame] | 2129 | host_iface = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0]; |
Jaroslav Kysela | ca4c2ad | 2010-04-16 10:32:54 +0200 | [diff] [blame] | 2130 | mixer->protocol = get_iface_desc(host_iface)->bInterfaceProtocol; |
Daniel Mack | 7b8a043 | 2010-02-22 23:49:12 +0100 | [diff] [blame] | 2131 | |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2132 | if ((err = snd_usb_mixer_controls(mixer)) < 0 || |
Clemens Ladisch | 93446ed | 2005-05-03 08:02:40 +0200 | [diff] [blame] | 2133 | (err = snd_usb_mixer_status_create(mixer)) < 0) |
| 2134 | goto _error; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2135 | |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 2136 | snd_usb_mixer_apply_create_quirk(mixer); |
Clemens Ladisch | 468b8fd | 2009-07-13 11:39:29 +0200 | [diff] [blame] | 2137 | |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2138 | err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops); |
Clemens Ladisch | 93446ed | 2005-05-03 08:02:40 +0200 | [diff] [blame] | 2139 | if (err < 0) |
| 2140 | goto _error; |
Jaroslav Kysela | ebfdeea | 2010-02-16 11:17:09 +0100 | [diff] [blame] | 2141 | |
| 2142 | if (list_empty(&chip->mixer_list) && |
| 2143 | !snd_card_proc_new(chip->card, "usbmixer", &entry)) |
| 2144 | snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read); |
| 2145 | |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2146 | list_add(&mixer->list, &chip->mixer_list); |
| 2147 | return 0; |
Clemens Ladisch | 93446ed | 2005-05-03 08:02:40 +0200 | [diff] [blame] | 2148 | |
| 2149 | _error: |
| 2150 | snd_usb_mixer_free(mixer); |
| 2151 | return err; |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | void snd_usb_mixer_disconnect(struct list_head *p) |
| 2155 | { |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2156 | struct usb_mixer_interface *mixer; |
Daniel Mack | 7b1eda2 | 2010-03-11 21:13:22 +0100 | [diff] [blame] | 2157 | |
Clemens Ladisch | 6639b6c | 2005-04-29 16:26:14 +0200 | [diff] [blame] | 2158 | mixer = list_entry(p, struct usb_mixer_interface, list); |
Mariusz Kozlowski | 68df9de | 2006-11-08 15:37:04 +0100 | [diff] [blame] | 2159 | usb_kill_urb(mixer->urb); |
| 2160 | usb_kill_urb(mixer->rc_urb); |
Clemens Ladisch | 84957a8 | 2005-04-29 16:23:13 +0200 | [diff] [blame] | 2161 | } |