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