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