blob: 539b427d08c6aca54ff1b0f1393bdbd3f55c6fef [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 Iwai7bc5ba7e2006-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;
426 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 Iwai7bc5ba7e2006-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;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200464 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200465
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;
Takashi Iwaib8e1c732009-06-16 14:04:37 +0200472 scale[3] = (convert_signed_value(cval, cval->max) * 100) / 256;
Takashi Iwaieedbdf02009-06-16 14:27:35 +0200473 if (scale[3] <= scale[2]) {
474 /* something is wrong; assume it's either from/to 0dB */
475 if (scale[2] < 0)
476 scale[3] = 0;
477 else if (scale[2] > 0)
478 scale[2] = 0;
479 else /* totally crap, return an error */
480 return -EINVAL;
481 }
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200482 if (copy_to_user(_tlv, scale, sizeof(scale)))
483 return -EFAULT;
484 return 0;
485}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487/*
488 * parser routines begin here...
489 */
490
Takashi Iwai86e07d32005-11-17 15:08:02 +0100491static int parse_audio_unit(struct mixer_build *state, int unitid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493
494/*
495 * check if the input/output channel routing is enabled on the given bitmap.
496 * used for mixer unit parser
497 */
498static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
499{
500 int idx = ich * num_outs + och;
501 return bmap[idx >> 3] & (0x80 >> (idx & 7));
502}
503
504
505/*
506 * add an alsa control element
507 * search and increment the index until an empty slot is found.
508 *
509 * if failed, give up and free the control instance.
510 */
511
Takashi Iwai86e07d32005-11-17 15:08:02 +0100512static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100514 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 int err;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200516
517 while (snd_ctl_find_id(state->chip->card, &kctl->id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 kctl->id.index++;
Clemens Ladisch84957a82005-04-29 16:23:13 +0200519 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200521 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Clemens Ladisch6639b6c2005-04-29 16:26:14 +0200523 cval->elem_id = &kctl->id;
524 cval->next_id_elem = state->mixer->id_elems[cval->id];
525 state->mixer->id_elems[cval->id] = cval;
526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529
530/*
531 * get a terminal name string
532 */
533
534static struct iterm_name_combo {
535 int type;
536 char *name;
537} iterm_names[] = {
538 { 0x0300, "Output" },
539 { 0x0301, "Speaker" },
540 { 0x0302, "Headphone" },
541 { 0x0303, "HMD Audio" },
542 { 0x0304, "Desktop Speaker" },
543 { 0x0305, "Room Speaker" },
544 { 0x0306, "Com Speaker" },
545 { 0x0307, "LFE" },
546 { 0x0600, "External In" },
547 { 0x0601, "Analog In" },
548 { 0x0602, "Digital In" },
549 { 0x0603, "Line" },
550 { 0x0604, "Legacy In" },
551 { 0x0605, "IEC958 In" },
552 { 0x0606, "1394 DA Stream" },
553 { 0x0607, "1394 DV Stream" },
554 { 0x0700, "Embedded" },
555 { 0x0701, "Noise Source" },
556 { 0x0702, "Equalization Noise" },
557 { 0x0703, "CD" },
558 { 0x0704, "DAT" },
559 { 0x0705, "DCC" },
560 { 0x0706, "MiniDisk" },
561 { 0x0707, "Analog Tape" },
562 { 0x0708, "Phonograph" },
563 { 0x0709, "VCR Audio" },
564 { 0x070a, "Video Disk Audio" },
565 { 0x070b, "DVD Audio" },
566 { 0x070c, "TV Tuner Audio" },
567 { 0x070d, "Satellite Rec Audio" },
568 { 0x070e, "Cable Tuner Audio" },
569 { 0x070f, "DSS Audio" },
570 { 0x0710, "Radio Receiver" },
571 { 0x0711, "Radio Transmitter" },
572 { 0x0712, "Multi-Track Recorder" },
573 { 0x0713, "Synthesizer" },
574 { 0 },
575};
576
Takashi Iwai86e07d32005-11-17 15:08:02 +0100577static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 unsigned char *name, int maxlen, int term_only)
579{
580 struct iterm_name_combo *names;
581
582 if (iterm->name)
583 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
584
585 /* virtual type - not a real terminal */
586 if (iterm->type >> 16) {
587 if (term_only)
588 return 0;
589 switch (iterm->type >> 16) {
590 case SELECTOR_UNIT:
591 strcpy(name, "Selector"); return 8;
592 case PROCESSING_UNIT:
593 strcpy(name, "Process Unit"); return 12;
594 case EXTENSION_UNIT:
595 strcpy(name, "Ext Unit"); return 8;
596 case MIXER_UNIT:
597 strcpy(name, "Mixer"); return 5;
598 default:
599 return sprintf(name, "Unit %d", iterm->id);
600 }
601 }
602
603 switch (iterm->type & 0xff00) {
604 case 0x0100:
605 strcpy(name, "PCM"); return 3;
606 case 0x0200:
607 strcpy(name, "Mic"); return 3;
608 case 0x0400:
609 strcpy(name, "Headset"); return 7;
610 case 0x0500:
611 strcpy(name, "Phone"); return 5;
612 }
613
614 for (names = iterm_names; names->type; names++)
615 if (names->type == iterm->type) {
616 strcpy(name, names->name);
617 return strlen(names->name);
618 }
619 return 0;
620}
621
622
623/*
624 * parse the source unit recursively until it reaches to a terminal
625 * or a branched unit.
626 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100627static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 unsigned char *p1;
630
631 memset(term, 0, sizeof(*term));
632 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
633 term->id = id;
634 switch (p1[2]) {
635 case INPUT_TERMINAL:
636 term->type = combine_word(p1 + 4);
637 term->channels = p1[7];
638 term->chconfig = combine_word(p1 + 8);
639 term->name = p1[11];
640 return 0;
641 case FEATURE_UNIT:
642 id = p1[4];
643 break; /* continue to parse */
644 case MIXER_UNIT:
645 term->type = p1[2] << 16; /* virtual type */
646 term->channels = p1[5 + p1[4]];
647 term->chconfig = combine_word(p1 + 6 + p1[4]);
648 term->name = p1[p1[0] - 1];
649 return 0;
650 case SELECTOR_UNIT:
651 /* call recursively to retrieve the channel info */
652 if (check_input_term(state, p1[5], term) < 0)
653 return -ENODEV;
654 term->type = p1[2] << 16; /* virtual type */
655 term->id = id;
656 term->name = p1[9 + p1[0] - 1];
657 return 0;
658 case PROCESSING_UNIT:
659 case EXTENSION_UNIT:
660 if (p1[6] == 1) {
661 id = p1[7];
662 break; /* continue to parse */
663 }
664 term->type = p1[2] << 16; /* virtual type */
665 term->channels = p1[7 + p1[6]];
666 term->chconfig = combine_word(p1 + 8 + p1[6]);
667 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
668 return 0;
669 default:
670 return -ENODEV;
671 }
672 }
673 return -ENODEV;
674}
675
676
677/*
678 * Feature Unit
679 */
680
681/* feature unit control information */
682struct usb_feature_control_info {
683 const char *name;
684 unsigned int type; /* control type (mute, volume, etc.) */
685};
686
687static struct usb_feature_control_info audio_feature_info[] = {
688 { "Mute", USB_MIXER_INV_BOOLEAN },
689 { "Volume", USB_MIXER_S16 },
690 { "Tone Control - Bass", USB_MIXER_S8 },
691 { "Tone Control - Mid", USB_MIXER_S8 },
692 { "Tone Control - Treble", USB_MIXER_S8 },
693 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
694 { "Auto Gain Control", USB_MIXER_BOOLEAN },
695 { "Delay Control", USB_MIXER_U16 },
696 { "Bass Boost", USB_MIXER_BOOLEAN },
697 { "Loudness", USB_MIXER_BOOLEAN },
698};
699
700
701/* private_free callback */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100702static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Jesper Juhl4d572772005-05-30 17:30:32 +0200704 kfree(kctl->private_data);
705 kctl->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708
709/*
710 * interface to ALSA control for feature/mixer units
711 */
712
713/*
714 * retrieve the minimum and maximum values for the specified control
715 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100716static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 /* for failsafe */
719 cval->min = default_min;
720 cval->max = cval->min + 1;
721 cval->res = 1;
722
723 if (cval->val_type == USB_MIXER_BOOLEAN ||
724 cval->val_type == USB_MIXER_INV_BOOLEAN) {
725 cval->initialized = 1;
726 } else {
727 int minchn = 0;
728 if (cval->cmask) {
729 int i;
730 for (i = 0; i < MAX_CHANNELS; i++)
731 if (cval->cmask & (1 << i)) {
732 minchn = i + 1;
733 break;
734 }
735 }
736 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
737 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +0200738 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
739 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return -EINVAL;
741 }
742 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
743 cval->res = 1;
744 } else {
745 int last_valid_res = cval->res;
746
747 while (cval->res > 1) {
748 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
749 break;
750 cval->res /= 2;
751 }
752 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
753 cval->res = last_valid_res;
754 }
755 if (cval->res == 0)
756 cval->res = 1;
Takashi Iwai14790f12006-03-28 17:58:28 +0200757
758 /* Additional checks for the proper resolution
759 *
760 * Some devices report smaller resolutions than actually
761 * reacting. They don't return errors but simply clip
762 * to the lower aligned value.
763 */
764 if (cval->min + cval->res < cval->max) {
765 int last_valid_res = cval->res;
766 int saved, test, check;
Takashi Iwai641b4872009-01-15 17:05:24 +0100767 get_cur_mix_raw(cval, minchn, &saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200768 for (;;) {
769 test = saved;
770 if (test < cval->max)
771 test += cval->res;
772 else
773 test -= cval->res;
774 if (test < cval->min || test > cval->max ||
Takashi Iwai641b4872009-01-15 17:05:24 +0100775 set_cur_mix_value(cval, minchn, 0, test) ||
776 get_cur_mix_raw(cval, minchn, &check)) {
Takashi Iwai14790f12006-03-28 17:58:28 +0200777 cval->res = last_valid_res;
778 break;
779 }
780 if (test == check)
781 break;
782 cval->res *= 2;
783 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100784 set_cur_mix_value(cval, minchn, 0, saved);
Takashi Iwai14790f12006-03-28 17:58:28 +0200785 }
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 cval->initialized = 1;
788 }
789 return 0;
790}
791
792
793/* get a feature/mixer unit info */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100794static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100796 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (cval->val_type == USB_MIXER_BOOLEAN ||
799 cval->val_type == USB_MIXER_INV_BOOLEAN)
800 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
801 else
802 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
803 uinfo->count = cval->channels;
804 if (cval->val_type == USB_MIXER_BOOLEAN ||
805 cval->val_type == USB_MIXER_INV_BOOLEAN) {
806 uinfo->value.integer.min = 0;
807 uinfo->value.integer.max = 1;
808 } else {
809 if (! cval->initialized)
810 get_min_max(cval, 0);
811 uinfo->value.integer.min = 0;
Takashi Iwai14790f12006-03-28 17:58:28 +0200812 uinfo->value.integer.max =
813 (cval->max - cval->min + cval->res - 1) / cval->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815 return 0;
816}
817
818/* get the current value from feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100819static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100821 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 int c, cnt, val, err;
823
Takashi Iwai641b4872009-01-15 17:05:24 +0100824 ucontrol->value.integer.value[0] = cval->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (cval->cmask) {
826 cnt = 0;
827 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100828 if (!(cval->cmask & (1 << c)))
829 continue;
830 err = get_cur_mix_value(cval, c + 1, cnt, &val);
831 if (err < 0)
832 return cval->mixer->ignore_ctl_error ? 0 : err;
833 val = get_relative_value(cval, val);
834 ucontrol->value.integer.value[cnt] = val;
835 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 } else {
839 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100840 err = get_cur_mix_value(cval, 0, 0, &val);
841 if (err < 0)
842 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 val = get_relative_value(cval, val);
844 ucontrol->value.integer.value[0] = val;
845 }
846 return 0;
847}
848
849/* put the current value to feature/mixer unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100850static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100852 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int c, cnt, val, oval, err;
854 int changed = 0;
855
856 if (cval->cmask) {
857 cnt = 0;
858 for (c = 0; c < MAX_CHANNELS; c++) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100859 if (!(cval->cmask & (1 << c)))
860 continue;
861 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
862 if (err < 0)
863 return cval->mixer->ignore_ctl_error ? 0 : err;
864 val = ucontrol->value.integer.value[cnt];
865 val = get_abs_value(cval, val);
866 if (oval != val) {
867 set_cur_mix_value(cval, c + 1, cnt, val);
868 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Takashi Iwai641b4872009-01-15 17:05:24 +0100870 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872 } else {
873 /* master channel */
Takashi Iwai641b4872009-01-15 17:05:24 +0100874 err = get_cur_mix_value(cval, 0, 0, &oval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (err < 0)
Takashi Iwai641b4872009-01-15 17:05:24 +0100876 return cval->mixer->ignore_ctl_error ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 val = ucontrol->value.integer.value[0];
878 val = get_abs_value(cval, val);
879 if (val != oval) {
Takashi Iwai641b4872009-01-15 17:05:24 +0100880 set_cur_mix_value(cval, 0, 0, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 changed = 1;
882 }
883 }
884 return changed;
885}
886
Takashi Iwai86e07d32005-11-17 15:08:02 +0100887static struct snd_kcontrol_new usb_feature_unit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
889 .name = "", /* will be filled later manually */
890 .info = mixer_ctl_feature_info,
891 .get = mixer_ctl_feature_get,
892 .put = mixer_ctl_feature_put,
893};
894
895
896/*
897 * build a feature control
898 */
899
Takashi Iwai86e07d32005-11-17 15:08:02 +0100900static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 unsigned int ctl_mask, int control,
Takashi Iwai86e07d32005-11-17 15:08:02 +0100902 struct usb_audio_term *iterm, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 unsigned int len = 0;
905 int mapped_name = 0;
906 int nameid = desc[desc[0] - 1];
Takashi Iwai86e07d32005-11-17 15:08:02 +0100907 struct snd_kcontrol *kctl;
908 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 control++; /* change from zero-based to 1-based value */
911
912 if (control == USB_FEATURE_GEQ) {
913 /* FIXME: not supported yet */
914 return;
915 }
916
917 if (check_ignored_ctl(state, unitid, control))
918 return;
919
Takashi Iwai561b2202005-09-09 14:22:34 +0200920 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (! cval) {
922 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
923 return;
924 }
Clemens Ladisch84957a82005-04-29 16:23:13 +0200925 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 cval->id = unitid;
927 cval->control = control;
928 cval->cmask = ctl_mask;
929 cval->val_type = audio_feature_info[control-1].type;
930 if (ctl_mask == 0)
931 cval->channels = 1; /* master channel */
932 else {
933 int i, c = 0;
934 for (i = 0; i < 16; i++)
935 if (ctl_mask & (1 << i))
936 c++;
937 cval->channels = c;
938 }
939
940 /* get min/max values */
941 get_min_max(cval, 0);
942
943 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
944 if (! kctl) {
945 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
946 kfree(cval);
947 return;
948 }
949 kctl->private_free = usb_mixer_elem_free;
950
951 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
952 mapped_name = len != 0;
953 if (! len && nameid)
954 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
955
956 switch (control) {
957 case USB_FEATURE_MUTE:
958 case USB_FEATURE_VOLUME:
959 /* determine the control name. the rule is:
960 * - if a name id is given in descriptor, use it.
961 * - if the connected input can be determined, then use the name
962 * of terminal type.
963 * - if the connected output can be determined, use it.
964 * - otherwise, anonymous name.
965 */
966 if (! len) {
967 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
968 if (! len)
969 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
970 if (! len)
971 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
972 "Feature %d", unitid);
973 }
974 /* determine the stream direction:
975 * if the connected output is USB stream, then it's likely a
976 * capture stream. otherwise it should be playback (hopefully :)
977 */
978 if (! mapped_name && ! (state->oterm.type >> 16)) {
979 if ((state->oterm.type & 0xff00) == 0x0100) {
980 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
981 } else {
982 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
983 }
984 }
985 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
986 sizeof(kctl->id.name));
Takashi Iwai7bc5ba7e2006-07-14 15:18:19 +0200987 if (control == USB_FEATURE_VOLUME) {
988 kctl->tlv.c = mixer_vol_tlv;
989 kctl->vd[0].access |=
990 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
991 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 break;
994
995 default:
996 if (! len)
997 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
998 sizeof(kctl->id.name));
999 break;
1000 }
1001
1002 /* quirk for UDA1321/N101 */
1003 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
1004 /* is not very clear from datasheets */
1005 /* I hope that the min value is -15360 for newer firmware --jk */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001006 switch (state->chip->usb_id) {
1007 case USB_ID(0x0471, 0x0101):
1008 case USB_ID(0x0471, 0x0104):
1009 case USB_ID(0x0471, 0x0105):
1010 case USB_ID(0x0672, 0x1041):
1011 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1012 cval->min == -15616) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02001013 snd_printk(KERN_INFO "using volume control quirk for the UDA1321/N101 chip\n");
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001014 cval->max = -256;
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
1018 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1019 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001020 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
1023
1024
1025/*
1026 * parse a feature unit
1027 *
1028 * most of controlls are defined here.
1029 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001030static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 int channels, i, j;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001033 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 unsigned int master_bits, first_ch_bits;
1035 int err, csize;
1036
1037 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1038 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1039 return -EINVAL;
1040 }
1041
1042 /* parse the source unit */
1043 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1044 return err;
1045
1046 /* determine the input source type and name */
1047 if (check_input_term(state, ftr[4], &iterm) < 0)
1048 return -EINVAL;
1049
1050 channels = (ftr[0] - 7) / csize - 1;
1051
1052 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
1053 if (channels > 0)
1054 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1055 else
1056 first_ch_bits = 0;
1057 /* check all control types */
1058 for (i = 0; i < 10; i++) {
1059 unsigned int ch_bits = 0;
1060 for (j = 0; j < channels; j++) {
1061 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1062 if (mask & (1 << i))
1063 ch_bits |= (1 << j);
1064 }
1065 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1066 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1067 if (master_bits & (1 << i))
1068 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1069 }
1070
1071 return 0;
1072}
1073
1074
1075/*
1076 * Mixer Unit
1077 */
1078
1079/*
1080 * build a mixer unit control
1081 *
1082 * the callbacks are identical with feature unit.
1083 * input channel number (zero based) is given in control field instead.
1084 */
1085
Takashi Iwai86e07d32005-11-17 15:08:02 +01001086static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 int in_pin, int in_ch, int unitid,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001088 struct usb_audio_term *iterm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001090 struct usb_mixer_elem_info *cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 unsigned int input_pins = desc[4];
1092 unsigned int num_outs = desc[5 + input_pins];
1093 unsigned int i, len;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001094 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 if (check_ignored_ctl(state, unitid, 0))
1097 return;
1098
Takashi Iwai561b2202005-09-09 14:22:34 +02001099 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (! cval)
1101 return;
1102
Clemens Ladisch84957a82005-04-29 16:23:13 +02001103 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 cval->id = unitid;
1105 cval->control = in_ch + 1; /* based on 1 */
1106 cval->val_type = USB_MIXER_S16;
1107 for (i = 0; i < num_outs; i++) {
1108 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1109 cval->cmask |= (1 << i);
1110 cval->channels++;
1111 }
1112 }
1113
1114 /* get min/max values */
1115 get_min_max(cval, 0);
1116
1117 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1118 if (! kctl) {
1119 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1120 kfree(cval);
1121 return;
1122 }
1123 kctl->private_free = usb_mixer_elem_free;
1124
1125 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1126 if (! len)
1127 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1128 if (! len)
1129 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1130 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1131
1132 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1133 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001134 add_control_to_empty(state, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137
1138/*
1139 * parse a mixer unit
1140 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001141static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001143 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 int input_pins, num_ins, num_outs;
1145 int pin, ich, err;
1146
1147 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1148 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1149 return -EINVAL;
1150 }
1151 /* no bmControls field (e.g. Maya44) -> ignore */
1152 if (desc[0] <= 10 + input_pins) {
1153 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1154 return 0;
1155 }
1156
1157 num_ins = 0;
1158 ich = 0;
1159 for (pin = 0; pin < input_pins; pin++) {
1160 err = parse_audio_unit(state, desc[5 + pin]);
1161 if (err < 0)
1162 return err;
1163 err = check_input_term(state, desc[5 + pin], &iterm);
1164 if (err < 0)
1165 return err;
1166 num_ins += iterm.channels;
1167 for (; ich < num_ins; ++ich) {
1168 int och, ich_has_controls = 0;
1169
1170 for (och = 0; och < num_outs; ++och) {
1171 if (check_matrix_bitmap(desc + 9 + input_pins,
1172 ich, och, num_outs)) {
1173 ich_has_controls = 1;
1174 break;
1175 }
1176 }
1177 if (ich_has_controls)
1178 build_mixer_unit_ctl(state, desc, pin, ich,
1179 unitid, &iterm);
1180 }
1181 }
1182 return 0;
1183}
1184
1185
1186/*
1187 * Processing Unit / Extension Unit
1188 */
1189
1190/* get callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001191static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001193 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int err, val;
1195
1196 err = get_cur_ctl_value(cval, cval->control << 8, &val);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001197 if (err < 0 && cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 ucontrol->value.integer.value[0] = cval->min;
1199 return 0;
1200 }
1201 if (err < 0)
1202 return err;
1203 val = get_relative_value(cval, val);
1204 ucontrol->value.integer.value[0] = val;
1205 return 0;
1206}
1207
1208/* put callback for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001209static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001211 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 int val, oval, err;
1213
1214 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1215 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001216 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return 0;
1218 return err;
1219 }
1220 val = ucontrol->value.integer.value[0];
1221 val = get_abs_value(cval, val);
1222 if (val != oval) {
1223 set_cur_ctl_value(cval, cval->control << 8, val);
1224 return 1;
1225 }
1226 return 0;
1227}
1228
1229/* alsa control interface for processing/extension unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001230static struct snd_kcontrol_new mixer_procunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1232 .name = "", /* will be filled later */
1233 .info = mixer_ctl_feature_info,
1234 .get = mixer_ctl_procunit_get,
1235 .put = mixer_ctl_procunit_put,
1236};
1237
1238
1239/*
1240 * predefined data for processing units
1241 */
1242struct procunit_value_info {
1243 int control;
1244 char *suffix;
1245 int val_type;
1246 int min_value;
1247};
1248
1249struct procunit_info {
1250 int type;
1251 char *name;
1252 struct procunit_value_info *values;
1253};
1254
1255static struct procunit_value_info updown_proc_info[] = {
1256 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1257 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1258 { 0 }
1259};
1260static struct procunit_value_info prologic_proc_info[] = {
1261 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1262 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1263 { 0 }
1264};
1265static struct procunit_value_info threed_enh_proc_info[] = {
1266 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1267 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1268 { 0 }
1269};
1270static struct procunit_value_info reverb_proc_info[] = {
1271 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1272 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1273 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1274 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1275 { 0 }
1276};
1277static struct procunit_value_info chorus_proc_info[] = {
1278 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1279 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1280 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1281 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1282 { 0 }
1283};
1284static struct procunit_value_info dcr_proc_info[] = {
1285 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1286 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1287 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1288 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1289 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1290 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1291 { 0 }
1292};
1293
1294static struct procunit_info procunits[] = {
1295 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1296 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1297 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1298 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1299 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1300 { USB_PROC_DCR, "DCR", dcr_proc_info },
1301 { 0 },
1302};
1303
1304/*
1305 * build a processing/extension unit
1306 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001307static 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 -07001308{
1309 int num_ins = dsc[6];
Takashi Iwai86e07d32005-11-17 15:08:02 +01001310 struct usb_mixer_elem_info *cval;
1311 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int i, err, nameid, type, len;
1313 struct procunit_info *info;
1314 struct procunit_value_info *valinfo;
1315 static struct procunit_value_info default_value_info[] = {
1316 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1317 { 0 }
1318 };
1319 static struct procunit_info default_info = {
1320 0, NULL, default_value_info
1321 };
1322
1323 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1324 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1325 return -EINVAL;
1326 }
1327
1328 for (i = 0; i < num_ins; i++) {
1329 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1330 return err;
1331 }
1332
1333 type = combine_word(&dsc[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 for (info = list; info && info->type; info++)
1335 if (info->type == type)
1336 break;
1337 if (! info || ! info->type)
1338 info = &default_info;
1339
1340 for (valinfo = info->values; valinfo->control; valinfo++) {
1341 /* FIXME: bitmap might be longer than 8bit */
1342 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1343 continue;
1344 if (check_ignored_ctl(state, unitid, valinfo->control))
1345 continue;
Takashi Iwai561b2202005-09-09 14:22:34 +02001346 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (! cval) {
1348 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1349 return -ENOMEM;
1350 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001351 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 cval->id = unitid;
1353 cval->control = valinfo->control;
1354 cval->val_type = valinfo->val_type;
1355 cval->channels = 1;
1356
1357 /* get min/max values */
1358 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1359 /* FIXME: hard-coded */
1360 cval->min = 1;
1361 cval->max = dsc[15];
1362 cval->res = 1;
1363 cval->initialized = 1;
1364 } else
1365 get_min_max(cval, valinfo->min_value);
1366
1367 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1368 if (! kctl) {
1369 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1370 kfree(cval);
1371 return -ENOMEM;
1372 }
1373 kctl->private_free = usb_mixer_elem_free;
1374
1375 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1376 ;
1377 else if (info->name)
1378 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1379 else {
1380 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1381 len = 0;
1382 if (nameid)
1383 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1384 if (! len)
1385 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1386 }
1387 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1388 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1389
1390 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1391 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001392 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return err;
1394 }
1395 return 0;
1396}
1397
1398
Takashi Iwai86e07d32005-11-17 15:08:02 +01001399static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1402}
1403
Takashi Iwai86e07d32005-11-17 15:08:02 +01001404static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1407}
1408
1409
1410/*
1411 * Selector Unit
1412 */
1413
1414/* info callback for selector unit
1415 * use an enumerator type for routing
1416 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001417static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001419 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 char **itemlist = (char **)kcontrol->private_value;
1421
Takashi Iwai5e246b82008-08-08 17:12:47 +02001422 if (snd_BUG_ON(!itemlist))
1423 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1425 uinfo->count = 1;
1426 uinfo->value.enumerated.items = cval->max;
1427 if ((int)uinfo->value.enumerated.item >= cval->max)
1428 uinfo->value.enumerated.item = cval->max - 1;
1429 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1430 return 0;
1431}
1432
1433/* get callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001434static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001436 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int val, err;
1438
1439 err = get_cur_ctl_value(cval, 0, &val);
1440 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001441 if (cval->mixer->ignore_ctl_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 ucontrol->value.enumerated.item[0] = 0;
1443 return 0;
1444 }
1445 return err;
1446 }
1447 val = get_relative_value(cval, val);
1448 ucontrol->value.enumerated.item[0] = val;
1449 return 0;
1450}
1451
1452/* put callback for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001453static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001455 struct usb_mixer_elem_info *cval = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 int val, oval, err;
1457
1458 err = get_cur_ctl_value(cval, 0, &oval);
1459 if (err < 0) {
Clemens Ladisch84957a82005-04-29 16:23:13 +02001460 if (cval->mixer->ignore_ctl_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return 0;
1462 return err;
1463 }
1464 val = ucontrol->value.enumerated.item[0];
1465 val = get_abs_value(cval, val);
1466 if (val != oval) {
1467 set_cur_ctl_value(cval, 0, val);
1468 return 1;
1469 }
1470 return 0;
1471}
1472
1473/* alsa control interface for selector unit */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001474static struct snd_kcontrol_new mixer_selectunit_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1476 .name = "", /* will be filled later */
1477 .info = mixer_ctl_selector_info,
1478 .get = mixer_ctl_selector_get,
1479 .put = mixer_ctl_selector_put,
1480};
1481
1482
1483/* private free callback.
1484 * free both private_data and private_value
1485 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001486static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 int i, num_ins = 0;
1489
1490 if (kctl->private_data) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001491 struct usb_mixer_elem_info *cval = kctl->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 num_ins = cval->max;
1493 kfree(cval);
1494 kctl->private_data = NULL;
1495 }
1496 if (kctl->private_value) {
1497 char **itemlist = (char **)kctl->private_value;
1498 for (i = 0; i < num_ins; i++)
1499 kfree(itemlist[i]);
1500 kfree(itemlist);
1501 kctl->private_value = 0;
1502 }
1503}
1504
1505/*
1506 * parse a selector unit
1507 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001508static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 unsigned int num_ins = desc[4];
1511 unsigned int i, nameid, len;
1512 int err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001513 struct usb_mixer_elem_info *cval;
1514 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 char **namelist;
1516
Russ Cox38977e92007-08-06 15:37:58 +02001517 if (! num_ins || desc[0] < 5 + num_ins) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1519 return -EINVAL;
1520 }
1521
1522 for (i = 0; i < num_ins; i++) {
1523 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1524 return err;
1525 }
1526
1527 if (num_ins == 1) /* only one ? nonsense! */
1528 return 0;
1529
1530 if (check_ignored_ctl(state, unitid, 0))
1531 return 0;
1532
Takashi Iwai561b2202005-09-09 14:22:34 +02001533 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (! cval) {
1535 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1536 return -ENOMEM;
1537 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02001538 cval->mixer = state->mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 cval->id = unitid;
1540 cval->val_type = USB_MIXER_U8;
1541 cval->channels = 1;
1542 cval->min = 1;
1543 cval->max = num_ins;
1544 cval->res = 1;
1545 cval->initialized = 1;
1546
1547 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1548 if (! namelist) {
1549 snd_printk(KERN_ERR "cannot malloc\n");
1550 kfree(cval);
1551 return -ENOMEM;
1552 }
1553#define MAX_ITEM_NAME_LEN 64
1554 for (i = 0; i < num_ins; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001555 struct usb_audio_term iterm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 len = 0;
1557 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1558 if (! namelist[i]) {
1559 snd_printk(KERN_ERR "cannot malloc\n");
Mariusz Kozlowski7fbe3ca2007-01-08 11:25:30 +01001560 while (i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 kfree(namelist[i]);
1562 kfree(namelist);
1563 kfree(cval);
1564 return -ENOMEM;
1565 }
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001566 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1567 MAX_ITEM_NAME_LEN);
1568 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1570 if (! len)
1571 sprintf(namelist[i], "Input %d", i);
1572 }
1573
1574 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1575 if (! kctl) {
1576 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
Jesper Juhl878b4782006-03-20 11:27:13 +01001577 kfree(namelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 kfree(cval);
1579 return -ENOMEM;
1580 }
1581 kctl->private_value = (unsigned long)namelist;
1582 kctl->private_free = usb_mixer_selector_elem_free;
1583
1584 nameid = desc[desc[0] - 1];
1585 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1586 if (len)
1587 ;
1588 else if (nameid)
1589 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1590 else {
1591 len = get_term_name(state, &state->oterm,
1592 kctl->id.name, sizeof(kctl->id.name), 0);
1593 if (! len)
1594 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1595
1596 if ((state->oterm.type & 0xff00) == 0x0100)
1597 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1598 else
1599 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1600 }
1601
1602 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1603 cval->id, kctl->id.name, num_ins);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001604 if ((err = add_control_to_empty(state, kctl)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return err;
1606
1607 return 0;
1608}
1609
1610
1611/*
1612 * parse an audio unit recursively
1613 */
1614
Takashi Iwai86e07d32005-11-17 15:08:02 +01001615static int parse_audio_unit(struct mixer_build *state, int unitid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 unsigned char *p1;
1618
1619 if (test_and_set_bit(unitid, state->unitbitmap))
1620 return 0; /* the unit already visited */
1621
1622 p1 = find_audio_control_unit(state, unitid);
1623 if (!p1) {
1624 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1625 return -EINVAL;
1626 }
1627
1628 switch (p1[2]) {
1629 case INPUT_TERMINAL:
1630 return 0; /* NOP */
1631 case MIXER_UNIT:
1632 return parse_audio_mixer_unit(state, unitid, p1);
1633 case SELECTOR_UNIT:
1634 return parse_audio_selector_unit(state, unitid, p1);
1635 case FEATURE_UNIT:
1636 return parse_audio_feature_unit(state, unitid, p1);
1637 case PROCESSING_UNIT:
1638 return parse_audio_processing_unit(state, unitid, p1);
1639 case EXTENSION_UNIT:
1640 return parse_audio_extension_unit(state, unitid, p1);
1641 default:
1642 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1643 return -EINVAL;
1644 }
1645}
1646
Clemens Ladisch84957a82005-04-29 16:23:13 +02001647static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1648{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001649 kfree(mixer->id_elems);
1650 if (mixer->urb) {
1651 kfree(mixer->urb->transfer_buffer);
1652 usb_free_urb(mixer->urb);
1653 }
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01001654 usb_free_urb(mixer->rc_urb);
Clemens Ladischb259b102005-04-29 16:29:28 +02001655 kfree(mixer->rc_setup_packet);
Clemens Ladisch84957a82005-04-29 16:23:13 +02001656 kfree(mixer);
1657}
1658
Takashi Iwai86e07d32005-11-17 15:08:02 +01001659static int snd_usb_mixer_dev_free(struct snd_device *device)
Clemens Ladisch84957a82005-04-29 16:23:13 +02001660{
1661 struct usb_mixer_interface *mixer = device->device_data;
1662 snd_usb_mixer_free(mixer);
1663 return 0;
1664}
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666/*
1667 * create mixer controls
1668 *
1669 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1670 */
Clemens Ladisch84957a82005-04-29 16:23:13 +02001671static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 unsigned char *desc;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001674 struct mixer_build state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 int err;
1676 const struct usbmix_ctl_map *map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001677 struct usb_host_interface *hostif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Clemens Ladisch84957a82005-04-29 16:23:13 +02001679 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 memset(&state, 0, sizeof(state));
Clemens Ladisch84957a82005-04-29 16:23:13 +02001681 state.chip = mixer->chip;
1682 state.mixer = mixer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 state.buffer = hostif->extra;
1684 state.buflen = hostif->extralen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 /* check the mapping table */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001687 for (map = usbmix_ctl_maps; map->id; map++) {
1688 if (map->id == state.chip->usb_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 state.map = map->map;
Clemens Ladisch8e062ec2005-04-22 15:49:52 +02001690 state.selector_map = map->selector_map;
Clemens Ladisch84957a82005-04-29 16:23:13 +02001691 mixer->ignore_ctl_error = map->ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 break;
1693 }
1694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 desc = NULL;
1697 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1698 if (desc[0] < 9)
1699 continue; /* invalid descriptor? */
1700 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1701 state.oterm.id = desc[3];
1702 state.oterm.type = combine_word(&desc[4]);
1703 state.oterm.name = desc[8];
1704 err = parse_audio_unit(&state, desc[7]);
1705 if (err < 0)
1706 return err;
1707 }
1708 return 0;
1709}
Clemens Ladisch84957a82005-04-29 16:23:13 +02001710
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001711static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1712 int unitid)
1713{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001714 struct usb_mixer_elem_info *info;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001715
1716 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1717 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1718 info->elem_id);
1719}
1720
Clemens Ladischb259b102005-04-29 16:29:28 +02001721static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1722 int unitid)
1723{
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001724 if (!mixer->rc_cfg)
Clemens Ladischaafad562005-05-10 14:47:38 +02001725 return;
1726 /* unit ids specific to Extigy/Audigy 2 NX: */
1727 switch (unitid) {
1728 case 0: /* remote control */
Clemens Ladischb259b102005-04-29 16:29:28 +02001729 mixer->rc_urb->dev = mixer->chip->dev;
1730 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
Clemens Ladischaafad562005-05-10 14:47:38 +02001731 break;
1732 case 4: /* digital in jack */
1733 case 7: /* line in jacks */
1734 case 19: /* speaker out jacks */
1735 case 20: /* headphones out jack */
1736 break;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001737 /* live24ext: 4 = line-in jack */
1738 case 3: /* hp-out jack (may actuate Mute) */
Andrea Borgia31959542009-01-07 22:58:50 +01001739 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1740 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001741 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1742 break;
Clemens Ladischaafad562005-05-10 14:47:38 +02001743 default:
1744 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1745 break;
Clemens Ladischb259b102005-04-29 16:29:28 +02001746 }
1747}
1748
David Howells7d12e782006-10-05 14:55:46 +01001749static void snd_usb_mixer_status_complete(struct urb *urb)
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001750{
1751 struct usb_mixer_interface *mixer = urb->context;
1752
1753 if (urb->status == 0) {
1754 u8 *buf = urb->transfer_buffer;
1755 int i;
1756
1757 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1758 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1759 buf[0], buf[1]);
1760 /* ignore any notifications not from the control interface */
1761 if ((buf[0] & 0x0f) != 0)
1762 continue;
1763 if (!(buf[0] & 0x40))
1764 snd_usb_mixer_notify_id(mixer, buf[1]);
Clemens Ladischb259b102005-04-29 16:29:28 +02001765 else
1766 snd_usb_mixer_memory_change(mixer, buf[1]);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001767 }
1768 }
1769 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1770 urb->dev = mixer->chip->dev;
1771 usb_submit_urb(urb, GFP_ATOMIC);
1772 }
1773}
1774
1775/* create the handler for the optional status interrupt endpoint */
1776static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1777{
1778 struct usb_host_interface *hostif;
1779 struct usb_endpoint_descriptor *ep;
1780 void *transfer_buffer;
1781 int buffer_length;
1782 unsigned int epnum;
1783
1784 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1785 /* we need one interrupt input endpoint */
1786 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1787 return 0;
1788 ep = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001789 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001790 return 0;
1791
Julia Lawall42a6e662008-12-29 11:23:02 +01001792 epnum = usb_endpoint_num(ep);
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02001793 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1794 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1795 if (!transfer_buffer)
1796 return -ENOMEM;
1797 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1798 if (!mixer->urb) {
1799 kfree(transfer_buffer);
1800 return -ENOMEM;
1801 }
1802 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1803 usb_rcvintpipe(mixer->chip->dev, epnum),
1804 transfer_buffer, buffer_length,
1805 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1806 usb_submit_urb(mixer->urb, GFP_KERNEL);
1807 return 0;
1808}
1809
David Howells7d12e782006-10-05 14:55:46 +01001810static void snd_usb_soundblaster_remote_complete(struct urb *urb)
Clemens Ladischb259b102005-04-29 16:29:28 +02001811{
1812 struct usb_mixer_interface *mixer = urb->context;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001813 const struct rc_config *rc = mixer->rc_cfg;
Clemens Ladischb259b102005-04-29 16:29:28 +02001814 u32 code;
1815
Phillip Michael Jordanb9c196e2008-08-05 11:01:00 +02001816 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
Clemens Ladischb259b102005-04-29 16:29:28 +02001817 return;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001818
1819 code = mixer->rc_buffer[rc->offset];
1820 if (rc->length == 2)
1821 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1822
Clemens Ladischb259b102005-04-29 16:29:28 +02001823 /* the Mute button actually changes the mixer control */
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001824 if (code == rc->mute_code)
1825 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
Clemens Ladischb259b102005-04-29 16:29:28 +02001826 mixer->rc_code = code;
1827 wmb();
1828 wake_up(&mixer->rc_waitq);
1829}
1830
Takashi Iwai86e07d32005-11-17 15:08:02 +01001831static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
Clemens Ladischb259b102005-04-29 16:29:28 +02001832 long count, loff_t *offset)
1833{
1834 struct usb_mixer_interface *mixer = hw->private_data;
1835 int err;
1836 u32 rc_code;
1837
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001838 if (count != 1 && count != 4)
Clemens Ladischb259b102005-04-29 16:29:28 +02001839 return -EINVAL;
1840 err = wait_event_interruptible(mixer->rc_waitq,
1841 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1842 if (err == 0) {
Clemens Ladisch434b7f52005-05-02 08:58:31 +02001843 if (count == 1)
1844 err = put_user(rc_code, buf);
1845 else
1846 err = put_user(rc_code, (u32 __user *)buf);
Clemens Ladischb259b102005-04-29 16:29:28 +02001847 }
1848 return err < 0 ? err : count;
1849}
1850
Takashi Iwai86e07d32005-11-17 15:08:02 +01001851static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
Clemens Ladischb259b102005-04-29 16:29:28 +02001852 poll_table *wait)
1853{
1854 struct usb_mixer_interface *mixer = hw->private_data;
1855
1856 poll_wait(file, &mixer->rc_waitq, wait);
1857 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1858}
1859
1860static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1861{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001862 struct snd_hwdep *hwdep;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001863 int err, len, i;
Clemens Ladischb259b102005-04-29 16:29:28 +02001864
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001865 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1866 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1867 break;
1868 if (i >= ARRAY_SIZE(rc_configs))
Clemens Ladischb259b102005-04-29 16:29:28 +02001869 return 0;
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001870 mixer->rc_cfg = &rc_configs[i];
Clemens Ladischb259b102005-04-29 16:29:28 +02001871
Raimonds Cicans4d1a70d2006-05-05 09:49:53 +02001872 len = mixer->rc_cfg->packet_length;
1873
Clemens Ladischb259b102005-04-29 16:29:28 +02001874 init_waitqueue_head(&mixer->rc_waitq);
1875 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1876 if (err < 0)
1877 return err;
1878 snprintf(hwdep->name, sizeof(hwdep->name),
1879 "%s remote control", mixer->chip->card->shortname);
1880 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1881 hwdep->private_data = mixer;
1882 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
Clemens Ladischb259b102005-04-29 16:29:28 +02001883 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
Takashi Iwai28b7e342009-02-05 09:28:08 +01001884 hwdep->exclusive = 1;
Clemens Ladischb259b102005-04-29 16:29:28 +02001885
1886 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1887 if (!mixer->rc_urb)
1888 return -ENOMEM;
1889 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1890 if (!mixer->rc_setup_packet) {
1891 usb_free_urb(mixer->rc_urb);
1892 mixer->rc_urb = NULL;
1893 return -ENOMEM;
1894 }
1895 mixer->rc_setup_packet->bRequestType =
1896 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1897 mixer->rc_setup_packet->bRequest = GET_MEM;
1898 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1899 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1900 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1901 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1902 usb_rcvctrlpipe(mixer->chip->dev, 0),
1903 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1904 snd_usb_soundblaster_remote_complete, mixer);
1905 return 0;
1906}
1907
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001908#define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001909
Takashi Iwai86e07d32005-11-17 15:08:02 +01001910static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001911{
1912 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1913 int index = kcontrol->private_value;
1914
1915 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1916 return 0;
1917}
1918
Takashi Iwai86e07d32005-11-17 15:08:02 +01001919static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001920{
1921 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1922 int index = kcontrol->private_value;
1923 int value = ucontrol->value.integer.value[0];
1924 int err, changed;
1925
1926 if (value > 1)
1927 return -EINVAL;
1928 changed = value != mixer->audigy2nx_leds[index];
1929 err = snd_usb_ctl_msg(mixer->chip->dev,
1930 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1931 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1932 value, index + 2, NULL, 0, 100);
1933 if (err < 0)
1934 return err;
1935 mixer->audigy2nx_leds[index] = value;
1936 return changed;
1937}
1938
Takashi Iwai86e07d32005-11-17 15:08:02 +01001939static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001940 {
1941 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1942 .name = "CMSS LED Switch",
1943 .info = snd_audigy2nx_led_info,
1944 .get = snd_audigy2nx_led_get,
1945 .put = snd_audigy2nx_led_put,
1946 .private_value = 0,
1947 },
1948 {
1949 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1950 .name = "Power LED Switch",
1951 .info = snd_audigy2nx_led_info,
1952 .get = snd_audigy2nx_led_get,
1953 .put = snd_audigy2nx_led_put,
1954 .private_value = 1,
1955 },
1956 {
1957 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1958 .name = "Dolby Digital LED Switch",
1959 .info = snd_audigy2nx_led_info,
1960 .get = snd_audigy2nx_led_get,
1961 .put = snd_audigy2nx_led_put,
1962 .private_value = 2,
1963 },
1964};
1965
1966static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1967{
1968 int i, err;
1969
1970 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
Andrea Borgia31959542009-01-07 22:58:50 +01001971 if (i > 1 && /* Live24ext has 2 LEDs only */
1972 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1973 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001974 break;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02001975 err = snd_ctl_add(mixer->chip->card,
1976 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1977 if (err < 0)
1978 return err;
1979 }
1980 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1981 return 0;
1982}
1983
Takashi Iwai86e07d32005-11-17 15:08:02 +01001984static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
1985 struct snd_info_buffer *buffer)
Clemens Ladischaafad562005-05-10 14:47:38 +02001986{
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001987 static const struct sb_jack {
Clemens Ladischaafad562005-05-10 14:47:38 +02001988 int unitid;
1989 const char *name;
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001990 } jacks_audigy2nx[] = {
Clemens Ladischaafad562005-05-10 14:47:38 +02001991 {4, "dig in "},
1992 {7, "line in"},
1993 {19, "spk out"},
1994 {20, "hph out"},
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01001995 {-1, NULL}
1996 }, jacks_live24ext[] = {
1997 {4, "line in"}, /* &1=Line, &2=Mic*/
1998 {3, "hph out"}, /* headphones */
1999 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2000 {-1, NULL}
Clemens Ladischaafad562005-05-10 14:47:38 +02002001 };
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002002 const struct sb_jack *jacks;
Clemens Ladischaafad562005-05-10 14:47:38 +02002003 struct usb_mixer_interface *mixer = entry->private_data;
2004 int i, err;
2005 u8 buf[3];
2006
2007 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002008 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2009 jacks = jacks_audigy2nx;
Andrea Borgia31959542009-01-07 22:58:50 +01002010 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2011 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002012 jacks = jacks_live24ext;
2013 else
2014 return;
2015
2016 for (i = 0; jacks[i].name; ++i) {
Clemens Ladischaafad562005-05-10 14:47:38 +02002017 snd_iprintf(buffer, "%s: ", jacks[i].name);
2018 err = snd_usb_ctl_msg(mixer->chip->dev,
2019 usb_rcvctrlpipe(mixer->chip->dev, 0),
2020 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2021 USB_RECIP_INTERFACE, 0,
2022 jacks[i].unitid << 8, buf, 3, 100);
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002023 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
Clemens Ladischaafad562005-05-10 14:47:38 +02002024 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2025 else
2026 snd_iprintf(buffer, "?\n");
2027 }
2028}
2029
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002030int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2031 int ignore_error)
Clemens Ladisch84957a82005-04-29 16:23:13 +02002032{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002033 static struct snd_device_ops dev_ops = {
Clemens Ladisch84957a82005-04-29 16:23:13 +02002034 .dev_free = snd_usb_mixer_dev_free
2035 };
2036 struct usb_mixer_interface *mixer;
2037 int err;
2038
2039 strcpy(chip->card->mixername, "USB Mixer");
2040
Takashi Iwai561b2202005-09-09 14:22:34 +02002041 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002042 if (!mixer)
2043 return -ENOMEM;
2044 mixer->chip = chip;
2045 mixer->ctrlif = ctrlif;
Takashi Iwai7a9b8062008-08-13 15:40:53 +02002046 mixer->ignore_ctl_error = ignore_error;
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002047 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2048 if (!mixer->id_elems) {
2049 kfree(mixer);
2050 return -ENOMEM;
2051 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02002052
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002053 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002054 (err = snd_usb_mixer_status_create(mixer)) < 0)
2055 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002056
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002057 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2058 goto _error;
2059
Timofei Bondarenko69b1f1e2007-10-30 15:28:14 +01002060 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
Andrea Borgia31959542009-01-07 22:58:50 +01002061 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2062 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002063 struct snd_info_entry *entry;
Clemens Ladischaafad562005-05-10 14:47:38 +02002064
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002065 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2066 goto _error;
Clemens Ladischaafad562005-05-10 14:47:38 +02002067 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002068 snd_info_set_text_ops(entry, mixer,
Clemens Ladischaafad562005-05-10 14:47:38 +02002069 snd_audigy2nx_proc_read);
Clemens Ladischb259b102005-04-29 16:29:28 +02002070 }
2071
Clemens Ladisch84957a82005-04-29 16:23:13 +02002072 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002073 if (err < 0)
2074 goto _error;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002075 list_add(&mixer->list, &chip->mixer_list);
2076 return 0;
Clemens Ladisch93446ed2005-05-03 08:02:40 +02002077
2078_error:
2079 snd_usb_mixer_free(mixer);
2080 return err;
Clemens Ladisch84957a82005-04-29 16:23:13 +02002081}
2082
2083void snd_usb_mixer_disconnect(struct list_head *p)
2084{
Clemens Ladisch6639b6c2005-04-29 16:26:14 +02002085 struct usb_mixer_interface *mixer;
2086
2087 mixer = list_entry(p, struct usb_mixer_interface, list);
Mariusz Kozlowski68df9de2006-11-08 15:37:04 +01002088 usb_kill_urb(mixer->urb);
2089 usb_kill_urb(mixer->rc_urb);
Clemens Ladisch84957a82005-04-29 16:23:13 +02002090}