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