blob: ec9cdf986928cfd1ce1847b18e111fdb71552fba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070029#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 Ladischb259b102005-04-29 16:29:28 +020037#include <sound/hwdep.h>
Clemens Ladischaafad562005-05-10 14:47:38 +020038#include <sound/info.h>
Takashi Iwai7bc5ba72006-07-14 15:18:19 +020039#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include "usbaudio.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 */
45
46/* ignore error from controls - for debugging */
47/* #define IGNORE_CTL_ERROR */
48
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020049/*
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 */
57static const struct rc_config {
58 u32 usb_id;
59 u8 offset;
60 u8 length;
61 u8 packet_length;
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +020062 u8 min_packet_length; /* minimum accepted length of the URB result */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020063 u8 mute_mixer_id;
64 u32 mute_code;
65} rc_configs[] = {
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +020066 { 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 Borgia31959542009-01-07 22:58:50 +010069 { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020070};
71
Clemens Ladisch84957a82005-04-29 16:23:13 +020072struct usb_mixer_interface {
Takashi Iwai86e07d32005-11-17 15:08:02 +010073 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +020074 unsigned int ctrlif;
75 struct list_head list;
76 unsigned int ignore_ctl_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +020077 struct urb *urb;
Takashi Iwai86e07d32005-11-17 15:08:02 +010078 struct usb_mixer_elem_info **id_elems; /* array[256], indexed by unit id */
Clemens Ladischb259b102005-04-29 16:29:28 +020079
80 /* Sound Blaster remote control stuff */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +020081 const struct rc_config *rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +020082 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 Ladisch93446ed2005-05-03 08:02:40 +020087
88 u8 audigy2nx_leds[3];
Clemens Ladisch84957a82005-04-29 16:23:13 +020089};
90
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct usb_audio_term {
93 int id;
94 int type;
95 int channels;
96 unsigned int chconfig;
97 int name;
98};
99
100struct usbmix_name_map;
101
Takashi Iwai86e07d32005-11-17 15:08:02 +0100102struct mixer_build {
103 struct snd_usb_audio *chip;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200104 struct usb_mixer_interface *mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned char *buffer;
106 unsigned int buflen;
Clemens Ladisch707e6072005-04-29 09:56:17 +0200107 DECLARE_BITMAP(unitbitmap, 256);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100108 struct usb_audio_term oterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 const struct usbmix_name_map *map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200110 const struct usbmix_selector_map *selector_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
Takashi Iwai641b4872009-01-15 17:05:24 +0100113#define MAX_CHANNELS 10 /* max logical channels */
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115struct usb_mixer_elem_info {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200116 struct usb_mixer_interface *mixer;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100117 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
118 struct snd_ctl_elem_id *elem_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned int id;
120 unsigned int control; /* CS or ICN (high byte) */
121 unsigned int cmask; /* channel mask bitmap: 0 = master */
122 int channels;
123 int val_type;
124 int min, max, res;
Takashi Iwai641b4872009-01-15 17:05:24 +0100125 int cached;
126 int cache_val[MAX_CHANNELS];
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200127 u8 initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130
131enum {
132 USB_FEATURE_NONE = 0,
133 USB_FEATURE_MUTE = 1,
134 USB_FEATURE_VOLUME,
135 USB_FEATURE_BASS,
136 USB_FEATURE_MID,
137 USB_FEATURE_TREBLE,
138 USB_FEATURE_GEQ,
139 USB_FEATURE_AGC,
140 USB_FEATURE_DELAY,
141 USB_FEATURE_BASSBOOST,
142 USB_FEATURE_LOUDNESS
143};
144
145enum {
146 USB_MIXER_BOOLEAN,
147 USB_MIXER_INV_BOOLEAN,
148 USB_MIXER_S8,
149 USB_MIXER_U8,
150 USB_MIXER_S16,
151 USB_MIXER_U16,
152};
153
154enum {
155 USB_PROC_UPDOWN = 1,
156 USB_PROC_UPDOWN_SWITCH = 1,
157 USB_PROC_UPDOWN_MODE_SEL = 2,
158
159 USB_PROC_PROLOGIC = 2,
160 USB_PROC_PROLOGIC_SWITCH = 1,
161 USB_PROC_PROLOGIC_MODE_SEL = 2,
162
163 USB_PROC_3DENH = 3,
164 USB_PROC_3DENH_SWITCH = 1,
165 USB_PROC_3DENH_SPACE = 2,
166
167 USB_PROC_REVERB = 4,
168 USB_PROC_REVERB_SWITCH = 1,
169 USB_PROC_REVERB_LEVEL = 2,
170 USB_PROC_REVERB_TIME = 3,
171 USB_PROC_REVERB_DELAY = 4,
172
173 USB_PROC_CHORUS = 5,
174 USB_PROC_CHORUS_SWITCH = 1,
175 USB_PROC_CHORUS_LEVEL = 2,
176 USB_PROC_CHORUS_RATE = 3,
177 USB_PROC_CHORUS_DEPTH = 4,
178
179 USB_PROC_DCR = 6,
180 USB_PROC_DCR_SWITCH = 1,
181 USB_PROC_DCR_RATIO = 2,
182 USB_PROC_DCR_MAX_AMP = 3,
183 USB_PROC_DCR_THRESHOLD = 4,
184 USB_PROC_DCR_ATTACK = 5,
185 USB_PROC_DCR_RELEASE = 6,
186};
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189/*
190 * manual mapping of mixer names
191 * if the mixer topology is too complicated and the parsed names are
192 * ambiguous, add the entries in usbmixer_maps.c.
193 */
194#include "usbmixer_maps.c"
195
196/* get the mapped name if the unit matches */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100197static int check_mapped_name(struct mixer_build *state, int unitid, int control, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 const struct usbmix_name_map *p;
200
201 if (! state->map)
202 return 0;
203
204 for (p = state->map; p->id; p++) {
205 if (p->id == unitid && p->name &&
206 (! control || ! p->control || control == p->control)) {
207 buflen--;
208 return strlcpy(buf, p->name, buflen);
209 }
210 }
211 return 0;
212}
213
214/* check whether the control should be ignored */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100215static int check_ignored_ctl(struct mixer_build *state, int unitid, int control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 const struct usbmix_name_map *p;
218
219 if (! state->map)
220 return 0;
221 for (p = state->map; p->id; p++) {
222 if (p->id == unitid && ! p->name &&
223 (! control || ! p->control || control == p->control)) {
Takashi Iwai54530bd2009-02-05 15:55:18 +0100224 /*
225 printk(KERN_DEBUG "ignored control %d:%d\n",
226 unitid, control);
227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return 1;
229 }
230 }
231 return 0;
232}
233
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200234/* get the mapped selector source name */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100235static int check_mapped_selector_name(struct mixer_build *state, int unitid,
Clemens Ladisch8e062ec2005-04-22 15:49:52 +0200236 int index, char *buf, int buflen)
237{
238 const struct usbmix_selector_map *p;
239
240 if (! state->selector_map)
241 return 0;
242 for (p = state->selector_map; p->id; p++) {
243 if (p->id == unitid && index < p->count)
244 return strlcpy(buf, p->names[index], buflen);
245 }
246 return 0;
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/*
250 * find an audio control unit with the given unit id
251 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100252static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 unsigned char *p;
255
256 p = NULL;
257 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
258 USB_DT_CS_INTERFACE)) != NULL) {
259 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
260 return p;
261 }
262 return NULL;
263}
264
265
266/*
267 * copy a string with the given id
268 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100269static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
272 buf[len] = 0;
273 return len;
274}
275
276/*
277 * convert from the byte/word on usb descriptor to the zero-based integer
278 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100279static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 switch (cval->val_type) {
282 case USB_MIXER_BOOLEAN:
283 return !!val;
284 case USB_MIXER_INV_BOOLEAN:
285 return !val;
286 case USB_MIXER_U8:
287 val &= 0xff;
288 break;
289 case USB_MIXER_S8:
290 val &= 0xff;
291 if (val >= 0x80)
292 val -= 0x100;
293 break;
294 case USB_MIXER_U16:
295 val &= 0xffff;
296 break;
297 case USB_MIXER_S16:
298 val &= 0xffff;
299 if (val >= 0x8000)
300 val -= 0x10000;
301 break;
302 }
303 return val;
304}
305
306/*
307 * convert from the zero-based int to the byte/word for usb descriptor
308 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100309static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 switch (cval->val_type) {
312 case USB_MIXER_BOOLEAN:
313 return !!val;
314 case USB_MIXER_INV_BOOLEAN:
315 return !val;
316 case USB_MIXER_S8:
317 case USB_MIXER_U8:
318 return val & 0xff;
319 case USB_MIXER_S16:
320 case USB_MIXER_U16:
321 return val & 0xffff;
322 }
323 return 0; /* not reached */
324}
325
Takashi Iwai86e07d32005-11-17 15:08:02 +0100326static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 if (! cval->res)
329 cval->res = 1;
330 if (val < cval->min)
331 return 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200332 else if (val >= cval->max)
333 return (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 else
335 return (val - cval->min) / cval->res;
336}
337
Takashi Iwai86e07d32005-11-17 15:08:02 +0100338static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 if (val < 0)
341 return cval->min;
342 if (! cval->res)
343 cval->res = 1;
344 val *= cval->res;
345 val += cval->min;
346 if (val > cval->max)
347 return cval->max;
348 return val;
349}
350
351
352/*
353 * retrieve a mixer value
354 */
355
Takashi Iwai86e07d32005-11-17 15:08:02 +0100356static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 unsigned char buf[2];
359 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
360 int timeout = 10;
361
362 while (timeout-- > 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200363 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
364 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 request,
366 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200367 validx, cval->mixer->ctrlif | (cval->id << 8),
Thomas Reitmayra04395e2007-05-15 11:47:48 +0200368 buf, val_len, 100) >= val_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
370 return 0;
371 }
372 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200373 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
374 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -EINVAL;
376}
377
Takashi Iwai86e07d32005-11-17 15:08:02 +0100378static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 return get_ctl_value(cval, GET_CUR, validx, value);
381}
382
383/* channel = 0: master, 1 = first channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100384static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
385 int channel, int *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
388}
389
Takashi Iwai641b4872009-01-15 17:05:24 +0100390static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
391 int channel, int index, int *value)
392{
393 int err;
394
395 if (cval->cached & (1 << channel)) {
396 *value = cval->cache_val[index];
397 return 0;
398 }
399 err = get_cur_mix_raw(cval, channel, value);
400 if (err < 0) {
401 if (!cval->mixer->ignore_ctl_error)
402 snd_printd(KERN_ERR "cannot get current value for "
403 "control %d ch %d: err = %d\n",
404 cval->control, channel, err);
405 return err;
406 }
407 cval->cached |= 1 << channel;
408 cval->cache_val[index] = *value;
409 return 0;
410}
411
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/*
414 * set a mixer value
415 */
416
Takashi Iwai86e07d32005-11-17 15:08:02 +0100417static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 unsigned char buf[2];
420 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
421 int timeout = 10;
422
423 value_set = convert_bytes_value(cval, value_set);
424 buf[0] = value_set & 0xff;
425 buf[1] = (value_set >> 8) & 0xff;
Viral Mehtacf3f9132009-04-03 13:08:14 +0530426 while (timeout-- > 0)
Clemens Ladisch84957a82005-04-29 16:23:13 +0200427 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
428 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 request,
430 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
Clemens Ladisch84957a82005-04-29 16:23:13 +0200431 validx, cval->mixer->ctrlif | (cval->id << 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 buf, val_len, 100) >= 0)
433 return 0;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200434 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
435 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -EINVAL;
437}
438
Takashi Iwai86e07d32005-11-17 15:08:02 +0100439static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 return set_ctl_value(cval, SET_CUR, validx, value);
442}
443
Takashi Iwai641b4872009-01-15 17:05:24 +0100444static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
445 int index, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Takashi Iwai641b4872009-01-15 17:05:24 +0100447 int err;
448 err = set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel,
449 value);
450 if (err < 0)
451 return err;
452 cval->cached |= 1 << channel;
453 cval->cache_val[index] = value;
454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200457/*
458 * TLV callback for mixer volume controls
459 */
460static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
461 unsigned int size, unsigned int __user *_tlv)
462{
463 struct usb_mixer_elem_info *cval = kcontrol->private_data;
464 DECLARE_TLV_DB_SCALE(scale, 0, 0, 0);
465
466 if (size < sizeof(scale))
467 return -ENOMEM;
468 /* USB descriptions contain the dB scale in 1/256 dB unit
469 * while ALSA TLV contains in 1/100 dB unit
470 */
471 scale[2] = (convert_signed_value(cval, cval->min) * 100) / 256;
472 scale[3] = (convert_signed_value(cval, cval->res) * 100) / 256;
473 if (copy_to_user(_tlv, scale, sizeof(scale)))
474 return -EFAULT;
475 return 0;
476}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478/*
479 * parser routines begin here...
480 */
481
Takashi Iwai86e07d32005-11-17 15:08:02 +0100482static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484
485/*
486 * check if the input/output channel routing is enabled on the given bitmap.
487 * used for mixer unit parser
488 */
489static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
490{
491 int idx = ich * num_outs + och;
492 return bmap[idx >> 3] & (0x80 >> (idx & 7));
493}
494
495
496/*
497 * add an alsa control element
498 * search and increment the index until an empty slot is found.
499 *
500 * if failed, give up and free the control instance.
501 */
502
Takashi Iwai86e07d32005-11-17 15:08:02 +0100503static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100505 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200507
508 while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 kctl->id.index++;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200510 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200512 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200514 cval->elem_id = &kctl->id;
515 cval->next_id_elem = state->mixer->id_elems[cval->id];
516 state->mixer->id_elems[cval->id] = cval;
517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520
521/*
522 * get a terminal name string
523 */
524
525static struct iterm_name_combo {
526 int type;
527 char *name;
528} iterm_names[] = {
529 { 0x0300, "Output" },
530 { 0x0301, "Speaker" },
531 { 0x0302, "Headphone" },
532 { 0x0303, "HMD Audio" },
533 { 0x0304, "Desktop Speaker" },
534 { 0x0305, "Room Speaker" },
535 { 0x0306, "Com Speaker" },
536 { 0x0307, "LFE" },
537 { 0x0600, "External In" },
538 { 0x0601, "Analog In" },
539 { 0x0602, "Digital In" },
540 { 0x0603, "Line" },
541 { 0x0604, "Legacy In" },
542 { 0x0605, "IEC958 In" },
543 { 0x0606, "1394 DA Stream" },
544 { 0x0607, "1394 DV Stream" },
545 { 0x0700, "Embedded" },
546 { 0x0701, "Noise Source" },
547 { 0x0702, "Equalization Noise" },
548 { 0x0703, "CD" },
549 { 0x0704, "DAT" },
550 { 0x0705, "DCC" },
551 { 0x0706, "MiniDisk" },
552 { 0x0707, "Analog Tape" },
553 { 0x0708, "Phonograph" },
554 { 0x0709, "VCR Audio" },
555 { 0x070a, "Video Disk Audio" },
556 { 0x070b, "DVD Audio" },
557 { 0x070c, "TV Tuner Audio" },
558 { 0x070d, "Satellite Rec Audio" },
559 { 0x070e, "Cable Tuner Audio" },
560 { 0x070f, "DSS Audio" },
561 { 0x0710, "Radio Receiver" },
562 { 0x0711, "Radio Transmitter" },
563 { 0x0712, "Multi-Track Recorder" },
564 { 0x0713, "Synthesizer" },
565 { 0 },
566};
567
Takashi Iwai86e07d32005-11-17 15:08:02 +0100568static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 unsigned char *name, int maxlen, int term_only)
570{
571 struct iterm_name_combo *names;
572
573 if (iterm->name)
574 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
575
576 /* virtual type - not a real terminal */
577 if (iterm->type >> 16) {
578 if (term_only)
579 return 0;
580 switch (iterm->type >> 16) {
581 case SELECTOR_UNIT:
582 strcpy(name, "Selector"); return 8;
583 case PROCESSING_UNIT:
584 strcpy(name, "Process Unit"); return 12;
585 case EXTENSION_UNIT:
586 strcpy(name, "Ext Unit"); return 8;
587 case MIXER_UNIT:
588 strcpy(name, "Mixer"); return 5;
589 default:
590 return sprintf(name, "Unit %d", iterm->id);
591 }
592 }
593
594 switch (iterm->type & 0xff00) {
595 case 0x0100:
596 strcpy(name, "PCM"); return 3;
597 case 0x0200:
598 strcpy(name, "Mic"); return 3;
599 case 0x0400:
600 strcpy(name, "Headset"); return 7;
601 case 0x0500:
602 strcpy(name, "Phone"); return 5;
603 }
604
605 for (names = iterm_names; names->type; names++)
606 if (names->type == iterm->type) {
607 strcpy(name, names->name);
608 return strlen(names->name);
609 }
610 return 0;
611}
612
613
614/*
615 * parse the source unit recursively until it reaches to a terminal
616 * or a branched unit.
617 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100618static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 unsigned char *p1;
621
622 memset(term, 0, sizeof(*term));
623 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
624 term->id = id;
625 switch (p1[2]) {
626 case INPUT_TERMINAL:
627 term->type = combine_word(p1 + 4);
628 term->channels = p1[7];
629 term->chconfig = combine_word(p1 + 8);
630 term->name = p1[11];
631 return 0;
632 case FEATURE_UNIT:
633 id = p1[4];
634 break; /* continue to parse */
635 case MIXER_UNIT:
636 term->type = p1[2] << 16; /* virtual type */
637 term->channels = p1[5 + p1[4]];
638 term->chconfig = combine_word(p1 + 6 + p1[4]);
639 term->name = p1[p1[0] - 1];
640 return 0;
641 case SELECTOR_UNIT:
642 /* call recursively to retrieve the channel info */
643 if (check_input_term(state, p1[5], term) < 0)
644 return -ENODEV;
645 term->type = p1[2] << 16; /* virtual type */
646 term->id = id;
647 term->name = p1[9 + p1[0] - 1];
648 return 0;
649 case PROCESSING_UNIT:
650 case EXTENSION_UNIT:
651 if (p1[6] == 1) {
652 id = p1[7];
653 break; /* continue to parse */
654 }
655 term->type = p1[2] << 16; /* virtual type */
656 term->channels = p1[7 + p1[6]];
657 term->chconfig = combine_word(p1 + 8 + p1[6]);
658 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
659 return 0;
660 default:
661 return -ENODEV;
662 }
663 }
664 return -ENODEV;
665}
666
667
668/*
669 * Feature Unit
670 */
671
672/* feature unit control information */
673struct usb_feature_control_info {
674 const char *name;
675 unsigned int type; /* control type (mute, volume, etc.) */
676};
677
678static struct usb_feature_control_info audio_feature_info[] = {
679 { "Mute", USB_MIXER_INV_BOOLEAN },
680 { "Volume", USB_MIXER_S16 },
681 { "Tone Control - Bass", USB_MIXER_S8 },
682 { "Tone Control - Mid", USB_MIXER_S8 },
683 { "Tone Control - Treble", USB_MIXER_S8 },
684 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
685 { "Auto Gain Control", USB_MIXER_BOOLEAN },
686 { "Delay Control", USB_MIXER_U16 },
687 { "Bass Boost", USB_MIXER_BOOLEAN },
688 { "Loudness", USB_MIXER_BOOLEAN },
689};
690
691
692/* private_free callback */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100693static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Jesper Juhl4d572772005-05-30 17:30:32 +0200695 kfree(kctl->private_data);
696 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699
700/*
701 * interface to ALSA control for feature/mixer units
702 */
703
704/*
705 * retrieve the minimum and maximum values for the specified control
706 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100707static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 /* for failsafe */
710 cval->min = default_min;
711 cval->max = cval->min + 1;
712 cval->res = 1;
713
714 if (cval->val_type == USB_MIXER_BOOLEAN ||
715 cval->val_type == USB_MIXER_INV_BOOLEAN) {
716 cval->initialized = 1;
717 } else {
718 int minchn = 0;
719 if (cval->cmask) {
720 int i;
721 for (i = 0; i < MAX_CHANNELS; i++)
722 if (cval->cmask & (1 << i)) {
723 minchn = i + 1;
724 break;
725 }
726 }
727 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
728 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200729 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
730 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return -EINVAL;
732 }
733 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
734 cval->res = 1;
735 } else {
736 int last_valid_res = cval->res;
737
738 while (cval->res > 1) {
739 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
740 break;
741 cval->res /= 2;
742 }
743 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
744 cval->res = last_valid_res;
745 }
746 if (cval->res == 0)
747 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +0200748
749 /* Additional checks for the proper resolution
750 *
751 * Some devices report smaller resolutions than actually
752 * reacting. They don't return errors but simply clip
753 * to the lower aligned value.
754 */
755 if (cval->min + cval->res < cval->max) {
756 int last_valid_res = cval->res;
757 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +0100758 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200759 for (;;) {
760 test = saved;
761 if (test < cval->max)
762 test += cval->res;
763 else
764 test -= cval->res;
765 if (test < cval->min || test > cval->max ||
Takashi Iwai641b4872009-01-15 17:05:24 +0100766 set_cur_mix_value(cval, minchn, 0, test) ||
767 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +0200768 cval->res = last_valid_res;
769 break;
770 }
771 if (test == check)
772 break;
773 cval->res *= 2;
774 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100775 set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200776 }
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 cval->initialized = 1;
779 }
780 return 0;
781}
782
783
784/* get a feature/mixer unit info */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100785static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100787 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (cval->val_type == USB_MIXER_BOOLEAN ||
790 cval->val_type == USB_MIXER_INV_BOOLEAN)
791 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
792 else
793 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
794 uinfo->count = cval->channels;
795 if (cval->val_type == USB_MIXER_BOOLEAN ||
796 cval->val_type == USB_MIXER_INV_BOOLEAN) {
797 uinfo->value.integer.min = 0;
798 uinfo->value.integer.max = 1;
799 } else {
800 if (! cval->initialized)
801 get_min_max(cval, 0);
802 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200803 uinfo->value.integer.max =
804 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806 return 0;
807}
808
809/* get the current value from feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100810static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100812 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 int c, cnt, val, err;
814
Takashi Iwai641b4872009-01-15 17:05:24 +0100815 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (cval->cmask) {
817 cnt = 0;
818 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100819 if (!(cval->cmask & (1 << c)))
820 continue;
821 err = get_cur_mix_value(cval, c + 1, cnt, &val);
822 if (err < 0)
823 return cval->mixer->ignore_ctl_error ? 0 : err;
824 val = get_relative_value(cval, val);
825 ucontrol->value.integer.value[cnt] = val;
826 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100828 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 } else {
830 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100831 err = get_cur_mix_value(cval, 0, 0, &val);
832 if (err < 0)
833 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 val = get_relative_value(cval, val);
835 ucontrol->value.integer.value[0] = val;
836 }
837 return 0;
838}
839
840/* put the current value to feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100841static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100843 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 int c, cnt, val, oval, err;
845 int changed = 0;
846
847 if (cval->cmask) {
848 cnt = 0;
849 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100850 if (!(cval->cmask & (1 << c)))
851 continue;
852 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
853 if (err < 0)
854 return cval->mixer->ignore_ctl_error ? 0 : err;
855 val = ucontrol->value.integer.value[cnt];
856 val = get_abs_value(cval, val);
857 if (oval != val) {
858 set_cur_mix_value(cval, c + 1, cnt, val);
859 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100861 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 } else {
864 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100865 err = get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (err < 0)
Takashi Iwai641b4872009-01-15 17:05:24 +0100867 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 val = ucontrol->value.integer.value[0];
869 val = get_abs_value(cval, val);
870 if (val != oval) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100871 set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 changed = 1;
873 }
874 }
875 return changed;
876}
877
Takashi Iwai86e07d32005-11-17 15:08:02 +0100878static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
880 .name = "", /* will be filled later manually */
881 .info = mixer_ctl_feature_info,
882 .get = mixer_ctl_feature_get,
883 .put = mixer_ctl_feature_put,
884};
885
886
887/*
888 * build a feature control
889 */
890
Takashi Iwai86e07d32005-11-17 15:08:02 +0100891static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 unsigned int ctl_mask, int control,
Takashi Iwai86e07d32005-11-17 15:08:02 +0100893 struct usb_audio_term *iterm, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 unsigned int len = 0;
896 int mapped_name = 0;
897 int nameid = desc[desc[0] - 1];
Takashi Iwai86e07d32005-11-17 15:08:02 +0100898 struct snd_kcontrol *kctl;
899 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 control++; /* change from zero-based to 1-based value */
902
903 if (control == USB_FEATURE_GEQ) {
904 /* FIXME: not supported yet */
905 return;
906 }
907
908 if (check_ignored_ctl(state, unitid, control))
909 return;
910
Takashi Iwai561b2202005-09-09 14:22:34 +0200911 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (! cval) {
913 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
914 return;
915 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200916 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 cval->id = unitid;
918 cval->control = control;
919 cval->cmask = ctl_mask;
920 cval->val_type = audio_feature_info[control-1].type;
921 if (ctl_mask == 0)
922 cval->channels = 1; /* master channel */
923 else {
924 int i, c = 0;
925 for (i = 0; i < 16; i++)
926 if (ctl_mask & (1 << i))
927 c++;
928 cval->channels = c;
929 }
930
931 /* get min/max values */
932 get_min_max(cval, 0);
933
934 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
935 if (! kctl) {
936 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
937 kfree(cval);
938 return;
939 }
940 kctl->private_free = usb_mixer_elem_free;
941
942 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
943 mapped_name = len != 0;
944 if (! len && nameid)
945 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
946
947 switch (control) {
948 case USB_FEATURE_MUTE:
949 case USB_FEATURE_VOLUME:
950 /* determine the control name. the rule is:
951 * - if a name id is given in descriptor, use it.
952 * - if the connected input can be determined, then use the name
953 * of terminal type.
954 * - if the connected output can be determined, use it.
955 * - otherwise, anonymous name.
956 */
957 if (! len) {
958 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
959 if (! len)
960 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
961 if (! len)
962 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
963 "Feature %d", unitid);
964 }
965 /* determine the stream direction:
966 * if the connected output is USB stream, then it's likely a
967 * capture stream. otherwise it should be playback (hopefully :)
968 */
969 if (! mapped_name && ! (state->oterm.type >> 16)) {
970 if ((state->oterm.type & 0xff00) == 0x0100) {
971 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
972 } else {
973 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
974 }
975 }
976 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
977 sizeof(kctl->id.name));
Takashi Iwai7bc5ba72006-07-14 15:18:19 +0200978 if (control == USB_FEATURE_VOLUME) {
979 kctl->tlv.c = mixer_vol_tlv;
980 kctl->vd[0].access |=
981 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
982 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 break;
985
986 default:
987 if (! len)
988 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
989 sizeof(kctl->id.name));
990 break;
991 }
992
Alexey Fisher2cf313e2009-07-22 14:57:54 +0200993 /* volume control quirks */
Clemens Ladisch27d10f52005-05-02 08:51:26 +0200994 switch (state->chip->usb_id) {
995 case USB_ID(0x0471, 0x0101):
996 case USB_ID(0x0471, 0x0104):
997 case USB_ID(0x0471, 0x0105):
998 case USB_ID(0x0672, 0x1041):
Alexey Fisher2cf313e2009-07-22 14:57:54 +0200999 /* quirk for UDA1321/N101.
1000 * note that detection between firmware 2.1.1.7 (N101)
1001 * and later 2.1.1.21 is not very clear from datasheets.
1002 * I hope that the min value is -15360 for newer firmware --jk
1003 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001004 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1005 cval->min == -15616) {
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001006 snd_printk(KERN_INFO
1007 "set volume quirk for UDA1321/N101 chip\n");
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001008 cval->max = -256;
1009 }
Alexey Fisher2cf313e2009-07-22 14:57:54 +02001010 break;
1011
1012 case USB_ID(0x046d, 0x09a4):
1013 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1014 snd_printk(KERN_INFO
1015 "set volume quirk for QuickCam E3500\n");
1016 cval->min = 6080;
1017 cval->max = 8768;
1018 cval->res = 192;
1019 }
1020 break;
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023
1024 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1025 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001026 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029
1030
1031/*
1032 * parse a feature unit
1033 *
1034 * most of controlls are defined here.
1035 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001036static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001039 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 unsigned int master_bits, first_ch_bits;
1041 int err, csize;
1042
1043 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1044 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1045 return -EINVAL;
1046 }
1047
1048 /* parse the source unit */
1049 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1050 return err;
1051
1052 /* determine the input source type and name */
1053 if (check_input_term(state, ftr[4], &iterm) < 0)
1054 return -EINVAL;
1055
1056 channels = (ftr[0] - 7) / csize - 1;
1057
1058 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
1059 if (channels > 0)
1060 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1061 else
1062 first_ch_bits = 0;
1063 /* check all control types */
1064 for (i = 0; i < 10; i++) {
1065 unsigned int ch_bits = 0;
1066 for (j = 0; j < channels; j++) {
1067 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1068 if (mask & (1 << i))
1069 ch_bits |= (1 << j);
1070 }
1071 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1072 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1073 if (master_bits & (1 << i))
1074 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1075 }
1076
1077 return 0;
1078}
1079
1080
1081/*
1082 * Mixer Unit
1083 */
1084
1085/*
1086 * build a mixer unit control
1087 *
1088 * the callbacks are identical with feature unit.
1089 * input channel number (zero based) is given in control field instead.
1090 */
1091
Takashi Iwai86e07d32005-11-17 15:08:02 +01001092static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001094 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001096 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 unsigned int input_pins = desc[4];
1098 unsigned int num_outs = desc[5 + input_pins];
1099 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001100 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 if (check_ignored_ctl(state, unitid, 0))
1103 return;
1104
Takashi Iwai561b2202005-09-09 14:22:34 +02001105 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (! cval)
1107 return;
1108
Clemens Ladisch84957a82005-04-29 16:23:13 +02001109 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 cval->id = unitid;
1111 cval->control = in_ch + 1; /* based on 1 */
1112 cval->val_type = USB_MIXER_S16;
1113 for (i = 0; i < num_outs; i++) {
1114 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1115 cval->cmask |= (1 << i);
1116 cval->channels++;
1117 }
1118 }
1119
1120 /* get min/max values */
1121 get_min_max(cval, 0);
1122
1123 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1124 if (! kctl) {
1125 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1126 kfree(cval);
1127 return;
1128 }
1129 kctl->private_free = usb_mixer_elem_free;
1130
1131 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1132 if (! len)
1133 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1134 if (! len)
1135 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1136 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1137
1138 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1139 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001140 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143
1144/*
1145 * parse a mixer unit
1146 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001147static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001149 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 int input_pins, num_ins, num_outs;
1151 int pin, ich, err;
1152
1153 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1154 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1155 return -EINVAL;
1156 }
1157 /* no bmControls field (e.g. Maya44) -> ignore */
1158 if (desc[0] <= 10 + input_pins) {
1159 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1160 return 0;
1161 }
1162
1163 num_ins = 0;
1164 ich = 0;
1165 for (pin = 0; pin < input_pins; pin++) {
1166 err = parse_audio_unit(state, desc[5 + pin]);
1167 if (err < 0)
1168 return err;
1169 err = check_input_term(state, desc[5 + pin], &iterm);
1170 if (err < 0)
1171 return err;
1172 num_ins += iterm.channels;
1173 for (; ich < num_ins; ++ich) {
1174 int och, ich_has_controls = 0;
1175
1176 for (och = 0; och < num_outs; ++och) {
1177 if (check_matrix_bitmap(desc + 9 + input_pins,
1178 ich, och, num_outs)) {
1179 ich_has_controls = 1;
1180 break;
1181 }
1182 }
1183 if (ich_has_controls)
1184 build_mixer_unit_ctl(state, desc, pin, ich,
1185 unitid, &iterm);
1186 }
1187 }
1188 return 0;
1189}
1190
1191
1192/*
1193 * Processing Unit / Extension Unit
1194 */
1195
1196/* get callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001197static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001199 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int err, val;
1201
1202 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001203 if (err < 0 && cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 ucontrol->value.integer.value[0] = cval->min;
1205 return 0;
1206 }
1207 if (err < 0)
1208 return err;
1209 val = get_relative_value(cval, val);
1210 ucontrol->value.integer.value[0] = val;
1211 return 0;
1212}
1213
1214/* put callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001215static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001217 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 int val, oval, err;
1219
1220 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1221 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001222 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return 0;
1224 return err;
1225 }
1226 val = ucontrol->value.integer.value[0];
1227 val = get_abs_value(cval, val);
1228 if (val != oval) {
1229 set_cur_ctl_value(cval, cval->control << 8, val);
1230 return 1;
1231 }
1232 return 0;
1233}
1234
1235/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001236static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1238 .name = "", /* will be filled later */
1239 .info = mixer_ctl_feature_info,
1240 .get = mixer_ctl_procunit_get,
1241 .put = mixer_ctl_procunit_put,
1242};
1243
1244
1245/*
1246 * predefined data for processing units
1247 */
1248struct procunit_value_info {
1249 int control;
1250 char *suffix;
1251 int val_type;
1252 int min_value;
1253};
1254
1255struct procunit_info {
1256 int type;
1257 char *name;
1258 struct procunit_value_info *values;
1259};
1260
1261static struct procunit_value_info updown_proc_info[] = {
1262 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1263 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1264 { 0 }
1265};
1266static struct procunit_value_info prologic_proc_info[] = {
1267 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1268 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1269 { 0 }
1270};
1271static struct procunit_value_info threed_enh_proc_info[] = {
1272 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1273 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1274 { 0 }
1275};
1276static struct procunit_value_info reverb_proc_info[] = {
1277 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1278 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1279 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1280 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1281 { 0 }
1282};
1283static struct procunit_value_info chorus_proc_info[] = {
1284 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1285 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1286 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1287 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1288 { 0 }
1289};
1290static struct procunit_value_info dcr_proc_info[] = {
1291 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1292 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1293 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1294 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1295 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1296 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1297 { 0 }
1298};
1299
1300static struct procunit_info procunits[] = {
1301 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1302 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1303 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1304 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1305 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1306 { USB_PROC_DCR, "DCR", dcr_proc_info },
1307 { 0 },
1308};
1309
1310/*
1311 * build a processing/extension unit
1312 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001313static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 int num_ins = dsc[6];
Takashi Iwai86e07d32005-11-17 15:08:02 +01001316 struct usb_mixer_elem_info *cval;
1317 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 int i, err, nameid, type, len;
1319 struct procunit_info *info;
1320 struct procunit_value_info *valinfo;
1321 static struct procunit_value_info default_value_info[] = {
1322 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1323 { 0 }
1324 };
1325 static struct procunit_info default_info = {
1326 0, NULL, default_value_info
1327 };
1328
1329 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1330 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1331 return -EINVAL;
1332 }
1333
1334 for (i = 0; i < num_ins; i++) {
1335 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1336 return err;
1337 }
1338
1339 type = combine_word(&dsc[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 for (info = list; info && info->type; info++)
1341 if (info->type == type)
1342 break;
1343 if (! info || ! info->type)
1344 info = &default_info;
1345
1346 for (valinfo = info->values; valinfo->control; valinfo++) {
1347 /* FIXME: bitmap might be longer than 8bit */
1348 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1349 continue;
1350 if (check_ignored_ctl(state, unitid, valinfo->control))
1351 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02001352 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (! cval) {
1354 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1355 return -ENOMEM;
1356 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001357 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 cval->id = unitid;
1359 cval->control = valinfo->control;
1360 cval->val_type = valinfo->val_type;
1361 cval->channels = 1;
1362
1363 /* get min/max values */
1364 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1365 /* FIXME: hard-coded */
1366 cval->min = 1;
1367 cval->max = dsc[15];
1368 cval->res = 1;
1369 cval->initialized = 1;
1370 } else
1371 get_min_max(cval, valinfo->min_value);
1372
1373 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1374 if (! kctl) {
1375 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1376 kfree(cval);
1377 return -ENOMEM;
1378 }
1379 kctl->private_free = usb_mixer_elem_free;
1380
1381 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1382 ;
1383 else if (info->name)
1384 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1385 else {
1386 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1387 len = 0;
1388 if (nameid)
1389 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1390 if (! len)
1391 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1392 }
1393 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1394 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1395
1396 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1397 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001398 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return err;
1400 }
1401 return 0;
1402}
1403
1404
Takashi Iwai86e07d32005-11-17 15:08:02 +01001405static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1408}
1409
Takashi Iwai86e07d32005-11-17 15:08:02 +01001410static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1413}
1414
1415
1416/*
1417 * Selector Unit
1418 */
1419
1420/* info callback for selector unit
1421 * use an enumerator type for routing
1422 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001423static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001425 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 char **itemlist = (char **)kcontrol->private_value;
1427
Takashi Iwai5e246b82008-08-08 17:12:47 +02001428 if (snd_BUG_ON(!itemlist))
1429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1431 uinfo->count = 1;
1432 uinfo->value.enumerated.items = cval->max;
1433 if ((int)uinfo->value.enumerated.item >= cval->max)
1434 uinfo->value.enumerated.item = cval->max - 1;
1435 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1436 return 0;
1437}
1438
1439/* get callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001440static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001442 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 int val, err;
1444
1445 err = get_cur_ctl_value(cval, 0, &val);
1446 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001447 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 ucontrol->value.enumerated.item[0] = 0;
1449 return 0;
1450 }
1451 return err;
1452 }
1453 val = get_relative_value(cval, val);
1454 ucontrol->value.enumerated.item[0] = val;
1455 return 0;
1456}
1457
1458/* put callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001459static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001461 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 int val, oval, err;
1463
1464 err = get_cur_ctl_value(cval, 0, &oval);
1465 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001466 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return 0;
1468 return err;
1469 }
1470 val = ucontrol->value.enumerated.item[0];
1471 val = get_abs_value(cval, val);
1472 if (val != oval) {
1473 set_cur_ctl_value(cval, 0, val);
1474 return 1;
1475 }
1476 return 0;
1477}
1478
1479/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001480static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1482 .name = "", /* will be filled later */
1483 .info = mixer_ctl_selector_info,
1484 .get = mixer_ctl_selector_get,
1485 .put = mixer_ctl_selector_put,
1486};
1487
1488
1489/* private free callback.
1490 * free both private_data and private_value
1491 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001492static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 int i, num_ins = 0;
1495
1496 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001497 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 num_ins = cval->max;
1499 kfree(cval);
1500 kctl->private_data = NULL;
1501 }
1502 if (kctl->private_value) {
1503 char **itemlist = (char **)kctl->private_value;
1504 for (i = 0; i < num_ins; i++)
1505 kfree(itemlist[i]);
1506 kfree(itemlist);
1507 kctl->private_value = 0;
1508 }
1509}
1510
1511/*
1512 * parse a selector unit
1513 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001514static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 unsigned int num_ins = desc[4];
1517 unsigned int i, nameid, len;
1518 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001519 struct usb_mixer_elem_info *cval;
1520 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 char **namelist;
1522
Russ Cox38977e92007-08-06 15:37:58 +02001523 if (! num_ins || desc[0] < 5 + num_ins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1525 return -EINVAL;
1526 }
1527
1528 for (i = 0; i < num_ins; i++) {
1529 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1530 return err;
1531 }
1532
1533 if (num_ins == 1) /* only one ? nonsense! */
1534 return 0;
1535
1536 if (check_ignored_ctl(state, unitid, 0))
1537 return 0;
1538
Takashi Iwai561b2202005-09-09 14:22:34 +02001539 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (! cval) {
1541 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1542 return -ENOMEM;
1543 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001544 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 cval->id = unitid;
1546 cval->val_type = USB_MIXER_U8;
1547 cval->channels = 1;
1548 cval->min = 1;
1549 cval->max = num_ins;
1550 cval->res = 1;
1551 cval->initialized = 1;
1552
1553 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1554 if (! namelist) {
1555 snd_printk(KERN_ERR "cannot malloc\n");
1556 kfree(cval);
1557 return -ENOMEM;
1558 }
1559#define MAX_ITEM_NAME_LEN 64
1560 for (i = 0; i < num_ins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001561 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 len = 0;
1563 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1564 if (! namelist[i]) {
1565 snd_printk(KERN_ERR "cannot malloc\n");
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01001566 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 kfree(namelist[i]);
1568 kfree(namelist);
1569 kfree(cval);
1570 return -ENOMEM;
1571 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001572 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1573 MAX_ITEM_NAME_LEN);
1574 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1576 if (! len)
1577 sprintf(namelist[i], "Input %d", i);
1578 }
1579
1580 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1581 if (! kctl) {
1582 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01001583 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 kfree(cval);
1585 return -ENOMEM;
1586 }
1587 kctl->private_value = (unsigned long)namelist;
1588 kctl->private_free = usb_mixer_selector_elem_free;
1589
1590 nameid = desc[desc[0] - 1];
1591 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1592 if (len)
1593 ;
1594 else if (nameid)
1595 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1596 else {
1597 len = get_term_name(state, &state->oterm,
1598 kctl->id.name, sizeof(kctl->id.name), 0);
1599 if (! len)
1600 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1601
1602 if ((state->oterm.type & 0xff00) == 0x0100)
1603 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1604 else
1605 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1606 }
1607
1608 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1609 cval->id, kctl->id.name, num_ins);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001610 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return err;
1612
1613 return 0;
1614}
1615
1616
1617/*
1618 * parse an audio unit recursively
1619 */
1620
Takashi Iwai86e07d32005-11-17 15:08:02 +01001621static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 unsigned char *p1;
1624
1625 if (test_and_set_bit(unitid, state->unitbitmap))
1626 return 0; /* the unit already visited */
1627
1628 p1 = find_audio_control_unit(state, unitid);
1629 if (!p1) {
1630 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1631 return -EINVAL;
1632 }
1633
1634 switch (p1[2]) {
1635 case INPUT_TERMINAL:
1636 return 0; /* NOP */
1637 case MIXER_UNIT:
1638 return parse_audio_mixer_unit(state, unitid, p1);
1639 case SELECTOR_UNIT:
1640 return parse_audio_selector_unit(state, unitid, p1);
1641 case FEATURE_UNIT:
1642 return parse_audio_feature_unit(state, unitid, p1);
1643 case PROCESSING_UNIT:
1644 return parse_audio_processing_unit(state, unitid, p1);
1645 case EXTENSION_UNIT:
1646 return parse_audio_extension_unit(state, unitid, p1);
1647 default:
1648 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1649 return -EINVAL;
1650 }
1651}
1652
Clemens Ladisch84957a82005-04-29 16:23:13 +02001653static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1654{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001655 kfree(mixer->id_elems);
1656 if (mixer->urb) {
1657 kfree(mixer->urb->transfer_buffer);
1658 usb_free_urb(mixer->urb);
1659 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01001660 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02001661 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001662 kfree(mixer);
1663}
1664
Takashi Iwai86e07d32005-11-17 15:08:02 +01001665static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001666{
1667 struct usb_mixer_interface *mixer = device->device_data;
1668 snd_usb_mixer_free(mixer);
1669 return 0;
1670}
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672/*
1673 * create mixer controls
1674 *
1675 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1676 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02001677static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 unsigned char *desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001680 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int err;
1682 const struct usbmix_ctl_map *map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001683 struct usb_host_interface *hostif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Clemens Ladisch84957a82005-04-29 16:23:13 +02001685 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02001687 state.chip = mixer->chip;
1688 state.mixer = mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 state.buffer = hostif->extra;
1690 state.buflen = hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001693 for (map = usbmix_ctl_maps; map->id; map++) {
1694 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001696 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001697 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 break;
1699 }
1700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 desc = NULL;
1703 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1704 if (desc[0] < 9)
1705 continue; /* invalid descriptor? */
1706 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1707 state.oterm.id = desc[3];
1708 state.oterm.type = combine_word(&desc[4]);
1709 state.oterm.name = desc[8];
1710 err = parse_audio_unit(&state, desc[7]);
1711 if (err < 0)
1712 return err;
1713 }
1714 return 0;
1715}
Clemens Ladisch84957a82005-04-29 16:23:13 +02001716
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001717static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1718 int unitid)
1719{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001720 struct usb_mixer_elem_info *info;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001721
1722 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1723 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1724 info->elem_id);
1725}
1726
Clemens Ladischb259b102005-04-29 16:29:28 +02001727static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1728 int unitid)
1729{
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001730 if (!mixer->rc_cfg)
Clemens Ladischaafad562005-05-10 14:47:38 +02001731 return;
1732 /* unit ids specific to Extigy/Audigy 2 NX: */
1733 switch (unitid) {
1734 case 0: /* remote control */
Clemens Ladischb259b102005-04-29 16:29:28 +02001735 mixer->rc_urb->dev = mixer->chip->dev;
1736 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
Clemens Ladischaafad562005-05-10 14:47:38 +02001737 break;
1738 case 4: /* digital in jack */
1739 case 7: /* line in jacks */
1740 case 19: /* speaker out jacks */
1741 case 20: /* headphones out jack */
1742 break;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001743 /* live24ext: 4 = line-in jack */
1744 case 3: /* hp-out jack (may actuate Mute) */
Andrea Borgia31959542009-01-07 22:58:50 +01001745 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1746 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001747 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1748 break;
Clemens Ladischaafad562005-05-10 14:47:38 +02001749 default:
1750 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1751 break;
Clemens Ladischb259b102005-04-29 16:29:28 +02001752 }
1753}
1754
David Howells7d12e782006-10-05 14:55:46 +01001755static void snd_usb_mixer_status_complete(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001756{
1757 struct usb_mixer_interface *mixer = urb->context;
1758
1759 if (urb->status == 0) {
1760 u8 *buf = urb->transfer_buffer;
1761 int i;
1762
1763 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1764 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1765 buf[0], buf[1]);
1766 /* ignore any notifications not from the control interface */
1767 if ((buf[0] & 0x0f) != 0)
1768 continue;
1769 if (!(buf[0] & 0x40))
1770 snd_usb_mixer_notify_id(mixer, buf[1]);
Clemens Ladischb259b102005-04-29 16:29:28 +02001771 else
1772 snd_usb_mixer_memory_change(mixer, buf[1]);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001773 }
1774 }
1775 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1776 urb->dev = mixer->chip->dev;
1777 usb_submit_urb(urb, GFP_ATOMIC);
1778 }
1779}
1780
1781/* create the handler for the optional status interrupt endpoint */
1782static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1783{
1784 struct usb_host_interface *hostif;
1785 struct usb_endpoint_descriptor *ep;
1786 void *transfer_buffer;
1787 int buffer_length;
1788 unsigned int epnum;
1789
1790 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1791 /* we need one interrupt input endpoint */
1792 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1793 return 0;
1794 ep = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001795 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001796 return 0;
1797
Julia Lawall42a6e662008-12-29 11:23:02 +01001798 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001799 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1800 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1801 if (!transfer_buffer)
1802 return -ENOMEM;
1803 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1804 if (!mixer->urb) {
1805 kfree(transfer_buffer);
1806 return -ENOMEM;
1807 }
1808 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1809 usb_rcvintpipe(mixer->chip->dev, epnum),
1810 transfer_buffer, buffer_length,
1811 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1812 usb_submit_urb(mixer->urb, GFP_KERNEL);
1813 return 0;
1814}
1815
David Howells7d12e782006-10-05 14:55:46 +01001816static void snd_usb_soundblaster_remote_complete(struct urb *urb)
Clemens Ladischb259b102005-04-29 16:29:28 +02001817{
1818 struct usb_mixer_interface *mixer = urb->context;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001819 const struct rc_config *rc = mixer->rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +02001820 u32 code;
1821
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +02001822 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
Clemens Ladischb259b102005-04-29 16:29:28 +02001823 return;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001824
1825 code = mixer->rc_buffer[rc->offset];
1826 if (rc->length == 2)
1827 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1828
Clemens Ladischb259b102005-04-29 16:29:28 +02001829 /* the Mute button actually changes the mixer control */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001830 if (code == rc->mute_code)
1831 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
Clemens Ladischb259b102005-04-29 16:29:28 +02001832 mixer->rc_code = code;
1833 wmb();
1834 wake_up(&mixer->rc_waitq);
1835}
1836
Takashi Iwai86e07d32005-11-17 15:08:02 +01001837static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
Clemens Ladischb259b102005-04-29 16:29:28 +02001838 long count, loff_t *offset)
1839{
1840 struct usb_mixer_interface *mixer = hw->private_data;
1841 int err;
1842 u32 rc_code;
1843
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001844 if (count != 1 && count != 4)
Clemens Ladischb259b102005-04-29 16:29:28 +02001845 return -EINVAL;
1846 err = wait_event_interruptible(mixer->rc_waitq,
1847 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1848 if (err == 0) {
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001849 if (count == 1)
1850 err = put_user(rc_code, buf);
1851 else
1852 err = put_user(rc_code, (u32 __user *)buf);
Clemens Ladischb259b102005-04-29 16:29:28 +02001853 }
1854 return err < 0 ? err : count;
1855}
1856
Takashi Iwai86e07d32005-11-17 15:08:02 +01001857static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
Clemens Ladischb259b102005-04-29 16:29:28 +02001858 poll_table *wait)
1859{
1860 struct usb_mixer_interface *mixer = hw->private_data;
1861
1862 poll_wait(file, &mixer->rc_waitq, wait);
1863 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1864}
1865
1866static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1867{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001868 struct snd_hwdep *hwdep;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001869 int err, len, i;
Clemens Ladischb259b102005-04-29 16:29:28 +02001870
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001871 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1872 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1873 break;
1874 if (i >= ARRAY_SIZE(rc_configs))
Clemens Ladischb259b102005-04-29 16:29:28 +02001875 return 0;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001876 mixer->rc_cfg = &rc_configs[i];
Clemens Ladischb259b102005-04-29 16:29:28 +02001877
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001878 len = mixer->rc_cfg->packet_length;
1879
Clemens Ladischb259b102005-04-29 16:29:28 +02001880 init_waitqueue_head(&mixer->rc_waitq);
1881 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1882 if (err < 0)
1883 return err;
1884 snprintf(hwdep->name, sizeof(hwdep->name),
1885 "%s remote control", mixer->chip->card->shortname);
1886 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1887 hwdep->private_data = mixer;
1888 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
Clemens Ladischb259b102005-04-29 16:29:28 +02001889 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
Takashi Iwai28b7e342009-02-05 09:28:08 +01001890 hwdep->exclusive = 1;
Clemens Ladischb259b102005-04-29 16:29:28 +02001891
1892 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1893 if (!mixer->rc_urb)
1894 return -ENOMEM;
1895 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1896 if (!mixer->rc_setup_packet) {
1897 usb_free_urb(mixer->rc_urb);
1898 mixer->rc_urb = NULL;
1899 return -ENOMEM;
1900 }
1901 mixer->rc_setup_packet->bRequestType =
1902 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1903 mixer->rc_setup_packet->bRequest = GET_MEM;
1904 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1905 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1906 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1907 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1908 usb_rcvctrlpipe(mixer->chip->dev, 0),
1909 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1910 snd_usb_soundblaster_remote_complete, mixer);
1911 return 0;
1912}
1913
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001914#define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001915
Takashi Iwai86e07d32005-11-17 15:08:02 +01001916static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001917{
1918 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1919 int index = kcontrol->private_value;
1920
1921 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1922 return 0;
1923}
1924
Takashi Iwai86e07d32005-11-17 15:08:02 +01001925static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001926{
1927 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1928 int index = kcontrol->private_value;
1929 int value = ucontrol->value.integer.value[0];
1930 int err, changed;
1931
1932 if (value > 1)
1933 return -EINVAL;
1934 changed = value != mixer->audigy2nx_leds[index];
1935 err = snd_usb_ctl_msg(mixer->chip->dev,
1936 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1937 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1938 value, index + 2, NULL, 0, 100);
1939 if (err < 0)
1940 return err;
1941 mixer->audigy2nx_leds[index] = value;
1942 return changed;
1943}
1944
Takashi Iwai86e07d32005-11-17 15:08:02 +01001945static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001946 {
1947 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1948 .name = "CMSS LED Switch",
1949 .info = snd_audigy2nx_led_info,
1950 .get = snd_audigy2nx_led_get,
1951 .put = snd_audigy2nx_led_put,
1952 .private_value = 0,
1953 },
1954 {
1955 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1956 .name = "Power LED Switch",
1957 .info = snd_audigy2nx_led_info,
1958 .get = snd_audigy2nx_led_get,
1959 .put = snd_audigy2nx_led_put,
1960 .private_value = 1,
1961 },
1962 {
1963 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1964 .name = "Dolby Digital LED Switch",
1965 .info = snd_audigy2nx_led_info,
1966 .get = snd_audigy2nx_led_get,
1967 .put = snd_audigy2nx_led_put,
1968 .private_value = 2,
1969 },
1970};
1971
1972static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1973{
1974 int i, err;
1975
1976 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
Andrea Borgia31959542009-01-07 22:58:50 +01001977 if (i > 1 && /* Live24ext has 2 LEDs only */
1978 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1979 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001980 break;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001981 err = snd_ctl_add(mixer->chip->card,
1982 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1983 if (err < 0)
1984 return err;
1985 }
1986 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1987 return 0;
1988}
1989
Takashi Iwai86e07d32005-11-17 15:08:02 +01001990static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
1991 struct snd_info_buffer *buffer)
Clemens Ladischaafad562005-05-10 14:47:38 +02001992{
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001993 static const struct sb_jack {
Clemens Ladischaafad562005-05-10 14:47:38 +02001994 int unitid;
1995 const char *name;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001996 } jacks_audigy2nx[] = {
Clemens Ladischaafad562005-05-10 14:47:38 +02001997 {4, "dig in "},
1998 {7, "line in"},
1999 {19, "spk out"},
2000 {20, "hph out"},
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002001 {-1, NULL}
2002 }, jacks_live24ext[] = {
2003 {4, "line in"}, /* &1=Line, &2=Mic*/
2004 {3, "hph out"}, /* headphones */
2005 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2006 {-1, NULL}
Clemens Ladischaafad562005-05-10 14:47:38 +02002007 };
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002008 const struct sb_jack *jacks;
Clemens Ladischaafad562005-05-10 14:47:38 +02002009 struct usb_mixer_interface *mixer = entry->private_data;
2010 int i, err;
2011 u8 buf[3];
2012
2013 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002014 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2015 jacks = jacks_audigy2nx;
Andrea Borgia31959542009-01-07 22:58:50 +01002016 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2017 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002018 jacks = jacks_live24ext;
2019 else
2020 return;
2021
2022 for (i = 0; jacks[i].name; ++i) {
Clemens Ladischaafad562005-05-10 14:47:38 +02002023 snd_iprintf(buffer, "%s: ", jacks[i].name);
2024 err = snd_usb_ctl_msg(mixer->chip->dev,
2025 usb_rcvctrlpipe(mixer->chip->dev, 0),
2026 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2027 USB_RECIP_INTERFACE, 0,
2028 jacks[i].unitid << 8, buf, 3, 100);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002029 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
Clemens Ladischaafad562005-05-10 14:47:38 +02002030 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2031 else
2032 snd_iprintf(buffer, "?\n");
2033 }
2034}
2035
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002036int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2037 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002038{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002039 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002040 .dev_free = snd_usb_mixer_dev_free
2041 };
2042 struct usb_mixer_interface *mixer;
2043 int err;
2044
2045 strcpy(chip->card->mixername, "USB Mixer");
2046
Takashi Iwai561b2202005-09-09 14:22:34 +02002047 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002048 if (!mixer)
2049 return -ENOMEM;
2050 mixer->chip = chip;
2051 mixer->ctrlif = ctrlif;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002052 mixer->ignore_ctl_error = ignore_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002053 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2054 if (!mixer->id_elems) {
2055 kfree(mixer);
2056 return -ENOMEM;
2057 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002058
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002059 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002060 (err = snd_usb_mixer_status_create(mixer)) < 0)
2061 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002062
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002063 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2064 goto _error;
2065
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002066 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
Andrea Borgia31959542009-01-07 22:58:50 +01002067 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2068 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002069 struct snd_info_entry *entry;
Clemens Ladischaafad562005-05-10 14:47:38 +02002070
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002071 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2072 goto _error;
Clemens Ladischaafad562005-05-10 14:47:38 +02002073 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002074 snd_info_set_text_ops(entry, mixer,
Clemens Ladischaafad562005-05-10 14:47:38 +02002075 snd_audigy2nx_proc_read);
Clemens Ladischb259b102005-04-29 16:29:28 +02002076 }
2077
Clemens Ladisch84957a82005-04-29 16:23:13 +02002078 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002079 if (err < 0)
2080 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002081 list_add(&mixer->list, &chip->mixer_list);
2082 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002083
2084_error:
2085 snd_usb_mixer_free(mixer);
2086 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002087}
2088
2089void snd_usb_mixer_disconnect(struct list_head *p)
2090{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002091 struct usb_mixer_interface *mixer;
2092
2093 mixer = list_entry(p, struct usb_mixer_interface, list);
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002094 usb_kill_urb(mixer->urb);
2095 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002096}