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