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