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