]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - sound/usb/usbmixer.c
ALSA: Remove superfluous hwdep ops
[linux-2.6.git] / sound / usb / usbmixer.c
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  */
57 static 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 };
70
71 struct 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         u32 rc_code;
82         wait_queue_head_t rc_waitq;
83         struct urb *rc_urb;
84         struct usb_ctrlrequest *rc_setup_packet;
85         u8 rc_buffer[6];
86
87         u8 audigy2nx_leds[3];
88 };
89
90
91 struct usb_audio_term {
92         int id;
93         int type;
94         int channels;
95         unsigned int chconfig;
96         int name;
97 };
98
99 struct usbmix_name_map;
100
101 struct mixer_build {
102         struct snd_usb_audio *chip;
103         struct usb_mixer_interface *mixer;
104         unsigned char *buffer;
105         unsigned int buflen;
106         DECLARE_BITMAP(unitbitmap, 256);
107         struct usb_audio_term oterm;
108         const struct usbmix_name_map *map;
109         const struct usbmix_selector_map *selector_map;
110 };
111
112 struct usb_mixer_elem_info {
113         struct usb_mixer_interface *mixer;
114         struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
115         struct snd_ctl_elem_id *elem_id;
116         unsigned int id;
117         unsigned int control;   /* CS or ICN (high byte) */
118         unsigned int cmask; /* channel mask bitmap: 0 = master */
119         int channels;
120         int val_type;
121         int min, max, res;
122         u8 initialized;
123 };
124
125
126 enum {
127         USB_FEATURE_NONE = 0,
128         USB_FEATURE_MUTE = 1,
129         USB_FEATURE_VOLUME,
130         USB_FEATURE_BASS,
131         USB_FEATURE_MID,
132         USB_FEATURE_TREBLE,
133         USB_FEATURE_GEQ,
134         USB_FEATURE_AGC,
135         USB_FEATURE_DELAY,
136         USB_FEATURE_BASSBOOST,
137         USB_FEATURE_LOUDNESS
138 };
139
140 enum {
141         USB_MIXER_BOOLEAN,
142         USB_MIXER_INV_BOOLEAN,
143         USB_MIXER_S8,
144         USB_MIXER_U8,
145         USB_MIXER_S16,
146         USB_MIXER_U16,
147 };
148
149 enum {
150         USB_PROC_UPDOWN = 1,
151         USB_PROC_UPDOWN_SWITCH = 1,
152         USB_PROC_UPDOWN_MODE_SEL = 2,
153
154         USB_PROC_PROLOGIC = 2,
155         USB_PROC_PROLOGIC_SWITCH = 1,
156         USB_PROC_PROLOGIC_MODE_SEL = 2,
157
158         USB_PROC_3DENH = 3,
159         USB_PROC_3DENH_SWITCH = 1,
160         USB_PROC_3DENH_SPACE = 2,
161
162         USB_PROC_REVERB = 4,
163         USB_PROC_REVERB_SWITCH = 1,
164         USB_PROC_REVERB_LEVEL = 2,
165         USB_PROC_REVERB_TIME = 3,
166         USB_PROC_REVERB_DELAY = 4,
167
168         USB_PROC_CHORUS = 5,
169         USB_PROC_CHORUS_SWITCH = 1,
170         USB_PROC_CHORUS_LEVEL = 2,
171         USB_PROC_CHORUS_RATE = 3,
172         USB_PROC_CHORUS_DEPTH = 4,
173
174         USB_PROC_DCR = 6,
175         USB_PROC_DCR_SWITCH = 1,
176         USB_PROC_DCR_RATIO = 2,
177         USB_PROC_DCR_MAX_AMP = 3,
178         USB_PROC_DCR_THRESHOLD = 4,
179         USB_PROC_DCR_ATTACK = 5,
180         USB_PROC_DCR_RELEASE = 6,
181 };
182
183 #define MAX_CHANNELS    10      /* max logical channels */
184
185
186 /*
187  * manual mapping of mixer names
188  * if the mixer topology is too complicated and the parsed names are
189  * ambiguous, add the entries in usbmixer_maps.c.
190  */
191 #include "usbmixer_maps.c"
192
193 /* get the mapped name if the unit matches */
194 static int check_mapped_name(struct mixer_build *state, int unitid, int control, char *buf, int buflen)
195 {
196         const struct usbmix_name_map *p;
197
198         if (! state->map)
199                 return 0;
200
201         for (p = state->map; p->id; p++) {
202                 if (p->id == unitid && p->name &&
203                     (! control || ! p->control || control == p->control)) {
204                         buflen--;
205                         return strlcpy(buf, p->name, buflen);
206                 }
207         }
208         return 0;
209 }
210
211 /* check whether the control should be ignored */
212 static int check_ignored_ctl(struct mixer_build *state, int unitid, int control)
213 {
214         const struct usbmix_name_map *p;
215
216         if (! state->map)
217                 return 0;
218         for (p = state->map; p->id; p++) {
219                 if (p->id == unitid && ! p->name &&
220                     (! control || ! p->control || control == p->control)) {
221                         // printk("ignored control %d:%d\n", unitid, control);
222                         return 1;
223                 }
224         }
225         return 0;
226 }
227
228 /* get the mapped selector source name */
229 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
230                                       int index, char *buf, int buflen)
231 {
232         const struct usbmix_selector_map *p;
233
234         if (! state->selector_map)
235                 return 0;
236         for (p = state->selector_map; p->id; p++) {
237                 if (p->id == unitid && index < p->count)
238                         return strlcpy(buf, p->names[index], buflen);
239         }
240         return 0;
241 }
242
243 /*
244  * find an audio control unit with the given unit id
245  */
246 static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
247 {
248         unsigned char *p;
249
250         p = NULL;
251         while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
252                                       USB_DT_CS_INTERFACE)) != NULL) {
253                 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
254                         return p;
255         }
256         return NULL;
257 }
258
259
260 /*
261  * copy a string with the given id
262  */
263 static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
264 {
265         int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
266         buf[len] = 0;
267         return len;
268 }
269
270 /*
271  * convert from the byte/word on usb descriptor to the zero-based integer
272  */
273 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
274 {
275         switch (cval->val_type) {
276         case USB_MIXER_BOOLEAN:
277                 return !!val;
278         case USB_MIXER_INV_BOOLEAN:
279                 return !val;
280         case USB_MIXER_U8:
281                 val &= 0xff;
282                 break;
283         case USB_MIXER_S8:
284                 val &= 0xff;
285                 if (val >= 0x80)
286                         val -= 0x100;
287                 break;
288         case USB_MIXER_U16:
289                 val &= 0xffff;
290                 break;
291         case USB_MIXER_S16:
292                 val &= 0xffff;
293                 if (val >= 0x8000)
294                         val -= 0x10000;
295                 break;
296         }
297         return val;
298 }
299
300 /*
301  * convert from the zero-based int to the byte/word for usb descriptor
302  */
303 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
304 {
305         switch (cval->val_type) {
306         case USB_MIXER_BOOLEAN:
307                 return !!val;
308         case USB_MIXER_INV_BOOLEAN:
309                 return !val;
310         case USB_MIXER_S8:
311         case USB_MIXER_U8:
312                 return val & 0xff;
313         case USB_MIXER_S16:
314         case USB_MIXER_U16:
315                 return val & 0xffff;
316         }
317         return 0; /* not reached */
318 }
319
320 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
321 {
322         if (! cval->res)
323                 cval->res = 1;
324         if (val < cval->min)
325                 return 0;
326         else if (val >= cval->max)
327                 return (cval->max - cval->min + cval->res - 1) / cval->res;
328         else
329                 return (val - cval->min) / cval->res;
330 }
331
332 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
333 {
334         if (val < 0)
335                 return cval->min;
336         if (! cval->res)
337                 cval->res = 1;
338         val *= cval->res;
339         val += cval->min;
340         if (val > cval->max)
341                 return cval->max;
342         return val;
343 }
344
345
346 /*
347  * retrieve a mixer value
348  */
349
350 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
351 {
352         unsigned char buf[2];
353         int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
354         int timeout = 10;
355
356         while (timeout-- > 0) {
357                 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
358                                     usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
359                                     request,
360                                     USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
361                                     validx, cval->mixer->ctrlif | (cval->id << 8),
362                                     buf, val_len, 100) >= val_len) {
363                         *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
364                         return 0;
365                 }
366         }
367         snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
368                     request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
369         return -EINVAL;
370 }
371
372 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
373 {
374         return get_ctl_value(cval, GET_CUR, validx, value);
375 }
376
377 /* channel = 0: master, 1 = first channel */
378 static inline int get_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int *value)
379 {
380         return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
381 }
382
383 /*
384  * set a mixer value
385  */
386
387 static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
388 {
389         unsigned char buf[2];
390         int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
391         int timeout = 10;
392
393         value_set = convert_bytes_value(cval, value_set);
394         buf[0] = value_set & 0xff;
395         buf[1] = (value_set >> 8) & 0xff;
396         while (timeout -- > 0)
397                 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
398                                     usb_sndctrlpipe(cval->mixer->chip->dev, 0),
399                                     request,
400                                     USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
401                                     validx, cval->mixer->ctrlif | (cval->id << 8),
402                                     buf, val_len, 100) >= 0)
403                         return 0;
404         snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
405                     request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
406         return -EINVAL;
407 }
408
409 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
410 {
411         return set_ctl_value(cval, SET_CUR, validx, value);
412 }
413
414 static inline int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int value)
415 {
416         return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
417 }
418
419 /*
420  * TLV callback for mixer volume controls
421  */
422 static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
423                          unsigned int size, unsigned int __user *_tlv)
424 {
425         struct usb_mixer_elem_info *cval = kcontrol->private_data;
426         DECLARE_TLV_DB_SCALE(scale, 0, 0, 0);
427
428         if (size < sizeof(scale))
429                 return -ENOMEM;
430         /* USB descriptions contain the dB scale in 1/256 dB unit
431          * while ALSA TLV contains in 1/100 dB unit
432          */
433         scale[2] = (convert_signed_value(cval, cval->min) * 100) / 256;
434         scale[3] = (convert_signed_value(cval, cval->res) * 100) / 256;
435         if (copy_to_user(_tlv, scale, sizeof(scale)))
436                 return -EFAULT;
437         return 0;
438 }
439
440 /*
441  * parser routines begin here...
442  */
443
444 static int parse_audio_unit(struct mixer_build *state, int unitid);
445
446
447 /*
448  * check if the input/output channel routing is enabled on the given bitmap.
449  * used for mixer unit parser
450  */
451 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
452 {
453         int idx = ich * num_outs + och;
454         return bmap[idx >> 3] & (0x80 >> (idx & 7));
455 }
456
457
458 /*
459  * add an alsa control element
460  * search and increment the index until an empty slot is found.
461  *
462  * if failed, give up and free the control instance.
463  */
464
465 static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
466 {
467         struct usb_mixer_elem_info *cval = kctl->private_data;
468         int err;
469
470         while (snd_ctl_find_id(state->chip->card, &kctl->id))
471                 kctl->id.index++;
472         if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
473                 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
474                 return err;
475         }
476         cval->elem_id = &kctl->id;
477         cval->next_id_elem = state->mixer->id_elems[cval->id];
478         state->mixer->id_elems[cval->id] = cval;
479         return 0;
480 }
481
482
483 /*
484  * get a terminal name string
485  */
486
487 static struct iterm_name_combo {
488         int type;
489         char *name;
490 } iterm_names[] = {
491         { 0x0300, "Output" },
492         { 0x0301, "Speaker" },
493         { 0x0302, "Headphone" },
494         { 0x0303, "HMD Audio" },
495         { 0x0304, "Desktop Speaker" },
496         { 0x0305, "Room Speaker" },
497         { 0x0306, "Com Speaker" },
498         { 0x0307, "LFE" },
499         { 0x0600, "External In" },
500         { 0x0601, "Analog In" },
501         { 0x0602, "Digital In" },
502         { 0x0603, "Line" },
503         { 0x0604, "Legacy In" },
504         { 0x0605, "IEC958 In" },
505         { 0x0606, "1394 DA Stream" },
506         { 0x0607, "1394 DV Stream" },
507         { 0x0700, "Embedded" },
508         { 0x0701, "Noise Source" },
509         { 0x0702, "Equalization Noise" },
510         { 0x0703, "CD" },
511         { 0x0704, "DAT" },
512         { 0x0705, "DCC" },
513         { 0x0706, "MiniDisk" },
514         { 0x0707, "Analog Tape" },
515         { 0x0708, "Phonograph" },
516         { 0x0709, "VCR Audio" },
517         { 0x070a, "Video Disk Audio" },
518         { 0x070b, "DVD Audio" },
519         { 0x070c, "TV Tuner Audio" },
520         { 0x070d, "Satellite Rec Audio" },
521         { 0x070e, "Cable Tuner Audio" },
522         { 0x070f, "DSS Audio" },
523         { 0x0710, "Radio Receiver" },
524         { 0x0711, "Radio Transmitter" },
525         { 0x0712, "Multi-Track Recorder" },
526         { 0x0713, "Synthesizer" },
527         { 0 },
528 };
529
530 static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
531                          unsigned char *name, int maxlen, int term_only)
532 {
533         struct iterm_name_combo *names;
534
535         if (iterm->name)
536                 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
537
538         /* virtual type - not a real terminal */
539         if (iterm->type >> 16) {
540                 if (term_only)
541                         return 0;
542                 switch (iterm->type >> 16) {
543                 case SELECTOR_UNIT:
544                         strcpy(name, "Selector"); return 8;
545                 case PROCESSING_UNIT:
546                         strcpy(name, "Process Unit"); return 12;
547                 case EXTENSION_UNIT:
548                         strcpy(name, "Ext Unit"); return 8;
549                 case MIXER_UNIT:
550                         strcpy(name, "Mixer"); return 5;
551                 default:
552                         return sprintf(name, "Unit %d", iterm->id);
553                 }
554         }
555
556         switch (iterm->type & 0xff00) {
557         case 0x0100:
558                 strcpy(name, "PCM"); return 3;
559         case 0x0200:
560                 strcpy(name, "Mic"); return 3;
561         case 0x0400:
562                 strcpy(name, "Headset"); return 7;
563         case 0x0500:
564                 strcpy(name, "Phone"); return 5;
565         }
566
567         for (names = iterm_names; names->type; names++)
568                 if (names->type == iterm->type) {
569                         strcpy(name, names->name);
570                         return strlen(names->name);
571                 }
572         return 0;
573 }
574
575
576 /*
577  * parse the source unit recursively until it reaches to a terminal
578  * or a branched unit.
579  */
580 static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
581 {
582         unsigned char *p1;
583
584         memset(term, 0, sizeof(*term));
585         while ((p1 = find_audio_control_unit(state, id)) != NULL) {
586                 term->id = id;
587                 switch (p1[2]) {
588                 case INPUT_TERMINAL:
589                         term->type = combine_word(p1 + 4);
590                         term->channels = p1[7];
591                         term->chconfig = combine_word(p1 + 8);
592                         term->name = p1[11];
593                         return 0;
594                 case FEATURE_UNIT:
595                         id = p1[4];
596                         break; /* continue to parse */
597                 case MIXER_UNIT:
598                         term->type = p1[2] << 16; /* virtual type */
599                         term->channels = p1[5 + p1[4]];
600                         term->chconfig = combine_word(p1 + 6 + p1[4]);
601                         term->name = p1[p1[0] - 1];
602                         return 0;
603                 case SELECTOR_UNIT:
604                         /* call recursively to retrieve the channel info */
605                         if (check_input_term(state, p1[5], term) < 0)
606                                 return -ENODEV;
607                         term->type = p1[2] << 16; /* virtual type */
608                         term->id = id;
609                         term->name = p1[9 + p1[0] - 1];
610                         return 0;
611                 case PROCESSING_UNIT:
612                 case EXTENSION_UNIT:
613                         if (p1[6] == 1) {
614                                 id = p1[7];
615                                 break; /* continue to parse */
616                         }
617                         term->type = p1[2] << 16; /* virtual type */
618                         term->channels = p1[7 + p1[6]];
619                         term->chconfig = combine_word(p1 + 8 + p1[6]);
620                         term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
621                         return 0;
622                 default:
623                         return -ENODEV;
624                 }
625         }
626         return -ENODEV;
627 }
628
629
630 /*
631  * Feature Unit
632  */
633
634 /* feature unit control information */
635 struct usb_feature_control_info {
636         const char *name;
637         unsigned int type;      /* control type (mute, volume, etc.) */
638 };
639
640 static struct usb_feature_control_info audio_feature_info[] = {
641         { "Mute",               USB_MIXER_INV_BOOLEAN },
642         { "Volume",             USB_MIXER_S16 },
643         { "Tone Control - Bass",        USB_MIXER_S8 },
644         { "Tone Control - Mid",         USB_MIXER_S8 },
645         { "Tone Control - Treble",      USB_MIXER_S8 },
646         { "Graphic Equalizer",          USB_MIXER_S8 }, /* FIXME: not implemeted yet */
647         { "Auto Gain Control",  USB_MIXER_BOOLEAN },
648         { "Delay Control",      USB_MIXER_U16 },
649         { "Bass Boost",         USB_MIXER_BOOLEAN },
650         { "Loudness",           USB_MIXER_BOOLEAN },
651 };
652
653
654 /* private_free callback */
655 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
656 {
657         kfree(kctl->private_data);
658         kctl->private_data = NULL;
659 }
660
661
662 /*
663  * interface to ALSA control for feature/mixer units
664  */
665
666 /*
667  * retrieve the minimum and maximum values for the specified control
668  */
669 static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
670 {
671         /* for failsafe */
672         cval->min = default_min;
673         cval->max = cval->min + 1;
674         cval->res = 1;
675
676         if (cval->val_type == USB_MIXER_BOOLEAN ||
677             cval->val_type == USB_MIXER_INV_BOOLEAN) {
678                 cval->initialized = 1;
679         } else {
680                 int minchn = 0;
681                 if (cval->cmask) {
682                         int i;
683                         for (i = 0; i < MAX_CHANNELS; i++)
684                                 if (cval->cmask & (1 << i)) {
685                                         minchn = i + 1;
686                                         break;
687                                 }
688                 }
689                 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
690                     get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
691                         snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
692                                    cval->id, cval->mixer->ctrlif, cval->control, cval->id);
693                         return -EINVAL;
694                 }
695                 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
696                         cval->res = 1;
697                 } else {
698                         int last_valid_res = cval->res;
699
700                         while (cval->res > 1) {
701                                 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
702                                         break;
703                                 cval->res /= 2;
704                         }
705                         if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
706                                 cval->res = last_valid_res;
707                 }
708                 if (cval->res == 0)
709                         cval->res = 1;
710
711                 /* Additional checks for the proper resolution
712                  *
713                  * Some devices report smaller resolutions than actually
714                  * reacting.  They don't return errors but simply clip
715                  * to the lower aligned value.
716                  */
717                 if (cval->min + cval->res < cval->max) {
718                         int last_valid_res = cval->res;
719                         int saved, test, check;
720                         get_cur_mix_value(cval, minchn, &saved);
721                         for (;;) {
722                                 test = saved;
723                                 if (test < cval->max)
724                                         test += cval->res;
725                                 else
726                                         test -= cval->res;
727                                 if (test < cval->min || test > cval->max ||
728                                     set_cur_mix_value(cval, minchn, test) ||
729                                     get_cur_mix_value(cval, minchn, &check)) {
730                                         cval->res = last_valid_res;
731                                         break;
732                                 }
733                                 if (test == check)
734                                         break;
735                                 cval->res *= 2;
736                         }
737                         set_cur_mix_value(cval, minchn, saved);
738                 }
739
740                 cval->initialized = 1;
741         }
742         return 0;
743 }
744
745
746 /* get a feature/mixer unit info */
747 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
748 {
749         struct usb_mixer_elem_info *cval = kcontrol->private_data;
750
751         if (cval->val_type == USB_MIXER_BOOLEAN ||
752             cval->val_type == USB_MIXER_INV_BOOLEAN)
753                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
754         else
755                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
756         uinfo->count = cval->channels;
757         if (cval->val_type == USB_MIXER_BOOLEAN ||
758             cval->val_type == USB_MIXER_INV_BOOLEAN) {
759                 uinfo->value.integer.min = 0;
760                 uinfo->value.integer.max = 1;
761         } else {
762                 if (! cval->initialized)
763                         get_min_max(cval,  0);
764                 uinfo->value.integer.min = 0;
765                 uinfo->value.integer.max =
766                         (cval->max - cval->min + cval->res - 1) / cval->res;
767         }
768         return 0;
769 }
770
771 /* get the current value from feature/mixer unit */
772 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
773 {
774         struct usb_mixer_elem_info *cval = kcontrol->private_data;
775         int c, cnt, val, err;
776
777         if (cval->cmask) {
778                 cnt = 0;
779                 for (c = 0; c < MAX_CHANNELS; c++) {
780                         if (cval->cmask & (1 << c)) {
781                                 err = get_cur_mix_value(cval, c + 1, &val);
782                                 if (err < 0) {
783                                         if (cval->mixer->ignore_ctl_error) {
784                                                 ucontrol->value.integer.value[0] = cval->min;
785                                                 return 0;
786                                         }
787                                         snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
788                                         return err;
789                                 }
790                                 val = get_relative_value(cval, val);
791                                 ucontrol->value.integer.value[cnt] = val;
792                                 cnt++;
793                         }
794                 }
795         } else {
796                 /* master channel */
797                 err = get_cur_mix_value(cval, 0, &val);
798                 if (err < 0) {
799                         if (cval->mixer->ignore_ctl_error) {
800                                 ucontrol->value.integer.value[0] = cval->min;
801                                 return 0;
802                         }
803                         snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
804                         return err;
805                 }
806                 val = get_relative_value(cval, val);
807                 ucontrol->value.integer.value[0] = val;
808         }
809         return 0;
810 }
811
812 /* put the current value to feature/mixer unit */
813 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
814 {
815         struct usb_mixer_elem_info *cval = kcontrol->private_data;
816         int c, cnt, val, oval, err;
817         int changed = 0;
818
819         if (cval->cmask) {
820                 cnt = 0;
821                 for (c = 0; c < MAX_CHANNELS; c++) {
822                         if (cval->cmask & (1 << c)) {
823                                 err = get_cur_mix_value(cval, c + 1, &oval);
824                                 if (err < 0) {
825                                         if (cval->mixer->ignore_ctl_error)
826                                                 return 0;
827                                         return err;
828                                 }
829                                 val = ucontrol->value.integer.value[cnt];
830                                 val = get_abs_value(cval, val);
831                                 if (oval != val) {
832                                         set_cur_mix_value(cval, c + 1, val);
833                                         changed = 1;
834                                 }
835                                 get_cur_mix_value(cval, c + 1, &val);
836                                 cnt++;
837                         }
838                 }
839         } else {
840                 /* master channel */
841                 err = get_cur_mix_value(cval, 0, &oval);
842                 if (err < 0 && cval->mixer->ignore_ctl_error)
843                         return 0;
844                 if (err < 0)
845                         return err;
846                 val = ucontrol->value.integer.value[0];
847                 val = get_abs_value(cval, val);
848                 if (val != oval) {
849                         set_cur_mix_value(cval, 0, val);
850                         changed = 1;
851                 }
852         }
853         return changed;
854 }
855
856 static struct snd_kcontrol_new usb_feature_unit_ctl = {
857         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
858         .name = "", /* will be filled later manually */
859         .info = mixer_ctl_feature_info,
860         .get = mixer_ctl_feature_get,
861         .put = mixer_ctl_feature_put,
862 };
863
864
865 /*
866  * build a feature control
867  */
868
869 static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
870                               unsigned int ctl_mask, int control,
871                               struct usb_audio_term *iterm, int unitid)
872 {
873         unsigned int len = 0;
874         int mapped_name = 0;
875         int nameid = desc[desc[0] - 1];
876         struct snd_kcontrol *kctl;
877         struct usb_mixer_elem_info *cval;
878
879         control++; /* change from zero-based to 1-based value */
880
881         if (control == USB_FEATURE_GEQ) {
882                 /* FIXME: not supported yet */
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  */
999 static 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
1055 static 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  */
1110 static 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 */
1160 static 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 */
1178 static 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 */
1199 static 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  */
1211 struct procunit_value_info {
1212         int control;
1213         char *suffix;
1214         int val_type;
1215         int min_value;
1216 };
1217
1218 struct procunit_info {
1219         int type;
1220         char *name;
1221         struct procunit_value_info *values;
1222 };
1223
1224 static 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 };
1229 static 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 };
1234 static 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 };
1239 static 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 };
1246 static 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 };
1253 static 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
1263 static 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  */
1276 static 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                 /* FIXME: bitmap might be longer than 8bit */
1311                 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1312                         continue;
1313                 if (check_ignored_ctl(state, unitid, valinfo->control))
1314                         continue;
1315                 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1316                 if (! cval) {
1317                         snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1318                         return -ENOMEM;
1319                 }
1320                 cval->mixer = state->mixer;
1321                 cval->id = unitid;
1322                 cval->control = valinfo->control;
1323                 cval->val_type = valinfo->val_type;
1324                 cval->channels = 1;
1325
1326                 /* get min/max values */
1327                 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1328                         /* FIXME: hard-coded */
1329                         cval->min = 1;
1330                         cval->max = dsc[15];
1331                         cval->res = 1;
1332                         cval->initialized = 1;
1333                 } else
1334                         get_min_max(cval, valinfo->min_value);
1335
1336                 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1337                 if (! kctl) {
1338                         snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1339                         kfree(cval);
1340                         return -ENOMEM;
1341                 }
1342                 kctl->private_free = usb_mixer_elem_free;
1343
1344                 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1345                         ;
1346                 else if (info->name)
1347                         strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1348                 else {
1349                         nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1350                         len = 0;
1351                         if (nameid)
1352                                 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1353                         if (! len)
1354                                 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1355                 }
1356                 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1357                 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1358
1359                 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1360                             cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1361                 if ((err = add_control_to_empty(state, kctl)) < 0)
1362                         return err;
1363         }
1364         return 0;
1365 }
1366
1367
1368 static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1369 {
1370         return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1371 }
1372
1373 static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1374 {
1375         return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1376 }
1377
1378
1379 /*
1380  * Selector Unit
1381  */
1382
1383 /* info callback for selector unit
1384  * use an enumerator type for routing
1385  */
1386 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1387 {
1388         struct usb_mixer_elem_info *cval = kcontrol->private_data;
1389         char **itemlist = (char **)kcontrol->private_value;
1390
1391         if (snd_BUG_ON(!itemlist))
1392                 return -EINVAL;
1393         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1394         uinfo->count = 1;
1395         uinfo->value.enumerated.items = cval->max;
1396         if ((int)uinfo->value.enumerated.item >= cval->max)
1397                 uinfo->value.enumerated.item = cval->max - 1;
1398         strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1399         return 0;
1400 }
1401
1402 /* get callback for selector unit */
1403 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1404 {
1405         struct usb_mixer_elem_info *cval = kcontrol->private_data;
1406         int val, err;
1407
1408         err = get_cur_ctl_value(cval, 0, &val);
1409         if (err < 0) {
1410                 if (cval->mixer->ignore_ctl_error) {
1411                         ucontrol->value.enumerated.item[0] = 0;
1412                         return 0;
1413                 }
1414                 return err;
1415         }
1416         val = get_relative_value(cval, val);
1417         ucontrol->value.enumerated.item[0] = val;
1418         return 0;
1419 }
1420
1421 /* put callback for selector unit */
1422 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1423 {
1424         struct usb_mixer_elem_info *cval = kcontrol->private_data;
1425         int val, oval, err;
1426
1427         err = get_cur_ctl_value(cval, 0, &oval);
1428         if (err < 0) {
1429                 if (cval->mixer->ignore_ctl_error)
1430                         return 0;
1431                 return err;
1432         }
1433         val = ucontrol->value.enumerated.item[0];
1434         val = get_abs_value(cval, val);
1435         if (val != oval) {
1436                 set_cur_ctl_value(cval, 0, val);
1437                 return 1;
1438         }
1439         return 0;
1440 }
1441
1442 /* alsa control interface for selector unit */
1443 static struct snd_kcontrol_new mixer_selectunit_ctl = {
1444         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1445         .name = "", /* will be filled later */
1446         .info = mixer_ctl_selector_info,
1447         .get = mixer_ctl_selector_get,
1448         .put = mixer_ctl_selector_put,
1449 };
1450
1451
1452 /* private free callback.
1453  * free both private_data and private_value
1454  */
1455 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
1456 {
1457         int i, num_ins = 0;
1458
1459         if (kctl->private_data) {
1460                 struct usb_mixer_elem_info *cval = kctl->private_data;
1461                 num_ins = cval->max;
1462                 kfree(cval);
1463                 kctl->private_data = NULL;
1464         }
1465         if (kctl->private_value) {
1466                 char **itemlist = (char **)kctl->private_value;
1467                 for (i = 0; i < num_ins; i++)
1468                         kfree(itemlist[i]);
1469                 kfree(itemlist);
1470                 kctl->private_value = 0;
1471         }
1472 }
1473
1474 /*
1475  * parse a selector unit
1476  */
1477 static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1478 {
1479         unsigned int num_ins = desc[4];
1480         unsigned int i, nameid, len;
1481         int err;
1482         struct usb_mixer_elem_info *cval;
1483         struct snd_kcontrol *kctl;
1484         char **namelist;
1485
1486         if (! num_ins || desc[0] < 5 + num_ins) {
1487                 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1488                 return -EINVAL;
1489         }
1490
1491         for (i = 0; i < num_ins; i++) {
1492                 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1493                         return err;
1494         }
1495
1496         if (num_ins == 1) /* only one ? nonsense! */
1497                 return 0;
1498
1499         if (check_ignored_ctl(state, unitid, 0))
1500                 return 0;
1501
1502         cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1503         if (! cval) {
1504                 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1505                 return -ENOMEM;
1506         }
1507         cval->mixer = state->mixer;
1508         cval->id = unitid;
1509         cval->val_type = USB_MIXER_U8;
1510         cval->channels = 1;
1511         cval->min = 1;
1512         cval->max = num_ins;
1513         cval->res = 1;
1514         cval->initialized = 1;
1515
1516         namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1517         if (! namelist) {
1518                 snd_printk(KERN_ERR "cannot malloc\n");
1519                 kfree(cval);
1520                 return -ENOMEM;
1521         }
1522 #define MAX_ITEM_NAME_LEN       64
1523         for (i = 0; i < num_ins; i++) {
1524                 struct usb_audio_term iterm;
1525                 len = 0;
1526                 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1527                 if (! namelist[i]) {
1528                         snd_printk(KERN_ERR "cannot malloc\n");
1529                         while (i--)
1530                                 kfree(namelist[i]);
1531                         kfree(namelist);
1532                         kfree(cval);
1533                         return -ENOMEM;
1534                 }
1535                 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1536                                                  MAX_ITEM_NAME_LEN);
1537                 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1538                         len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1539                 if (! len)
1540                         sprintf(namelist[i], "Input %d", i);
1541         }
1542
1543         kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1544         if (! kctl) {
1545                 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1546                 kfree(namelist);
1547                 kfree(cval);
1548                 return -ENOMEM;
1549         }
1550         kctl->private_value = (unsigned long)namelist;
1551         kctl->private_free = usb_mixer_selector_elem_free;
1552
1553         nameid = desc[desc[0] - 1];
1554         len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1555         if (len)
1556                 ;
1557         else if (nameid)
1558                 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1559         else {
1560                 len = get_term_name(state, &state->oterm,
1561                                     kctl->id.name, sizeof(kctl->id.name), 0);
1562                 if (! len)
1563                         strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1564
1565                 if ((state->oterm.type & 0xff00) == 0x0100)
1566                         strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1567                 else
1568                         strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1569         }
1570
1571         snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1572                     cval->id, kctl->id.name, num_ins);
1573         if ((err = add_control_to_empty(state, kctl)) < 0)
1574                 return err;
1575
1576         return 0;
1577 }
1578
1579
1580 /*
1581  * parse an audio unit recursively
1582  */
1583
1584 static int parse_audio_unit(struct mixer_build *state, int unitid)
1585 {
1586         unsigned char *p1;
1587
1588         if (test_and_set_bit(unitid, state->unitbitmap))
1589                 return 0; /* the unit already visited */
1590
1591         p1 = find_audio_control_unit(state, unitid);
1592         if (!p1) {
1593                 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1594                 return -EINVAL;
1595         }
1596
1597         switch (p1[2]) {
1598         case INPUT_TERMINAL:
1599                 return 0; /* NOP */
1600         case MIXER_UNIT:
1601                 return parse_audio_mixer_unit(state, unitid, p1);
1602         case SELECTOR_UNIT:
1603                 return parse_audio_selector_unit(state, unitid, p1);
1604         case FEATURE_UNIT:
1605                 return parse_audio_feature_unit(state, unitid, p1);
1606         case PROCESSING_UNIT:
1607                 return parse_audio_processing_unit(state, unitid, p1);
1608         case EXTENSION_UNIT:
1609                 return parse_audio_extension_unit(state, unitid, p1);
1610         default:
1611                 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1612                 return -EINVAL;
1613         }
1614 }
1615
1616 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1617 {
1618         kfree(mixer->id_elems);
1619         if (mixer->urb) {
1620                 kfree(mixer->urb->transfer_buffer);
1621                 usb_free_urb(mixer->urb);
1622         }
1623         usb_free_urb(mixer->rc_urb);
1624         kfree(mixer->rc_setup_packet);
1625         kfree(mixer);
1626 }
1627
1628 static int snd_usb_mixer_dev_free(struct snd_device *device)
1629 {
1630         struct usb_mixer_interface *mixer = device->device_data;
1631         snd_usb_mixer_free(mixer);
1632         return 0;
1633 }
1634
1635 /*
1636  * create mixer controls
1637  *
1638  * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1639  */
1640 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1641 {
1642         unsigned char *desc;
1643         struct mixer_build state;
1644         int err;
1645         const struct usbmix_ctl_map *map;
1646         struct usb_host_interface *hostif;
1647
1648         hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1649         memset(&state, 0, sizeof(state));
1650         state.chip = mixer->chip;
1651         state.mixer = mixer;
1652         state.buffer = hostif->extra;
1653         state.buflen = hostif->extralen;
1654
1655         /* check the mapping table */
1656         for (map = usbmix_ctl_maps; map->id; map++) {
1657                 if (map->id == state.chip->usb_id) {
1658                         state.map = map->map;
1659                         state.selector_map = map->selector_map;
1660                         mixer->ignore_ctl_error = map->ignore_ctl_error;
1661                         break;
1662                 }
1663         }
1664
1665         desc = NULL;
1666         while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1667                 if (desc[0] < 9)
1668                         continue; /* invalid descriptor? */
1669                 set_bit(desc[3], state.unitbitmap);  /* mark terminal ID as visited */
1670                 state.oterm.id = desc[3];
1671                 state.oterm.type = combine_word(&desc[4]);
1672                 state.oterm.name = desc[8];
1673                 err = parse_audio_unit(&state, desc[7]);
1674                 if (err < 0)
1675                         return err;
1676         }
1677         return 0;
1678 }
1679
1680 static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1681                                     int unitid)
1682 {
1683         struct usb_mixer_elem_info *info;
1684
1685         for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1686                 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1687                                info->elem_id);
1688 }
1689
1690 static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1691                                         int unitid)
1692 {
1693         if (!mixer->rc_cfg)
1694                 return;
1695         /* unit ids specific to Extigy/Audigy 2 NX: */
1696         switch (unitid) {
1697         case 0: /* remote control */
1698                 mixer->rc_urb->dev = mixer->chip->dev;
1699                 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1700                 break;
1701         case 4: /* digital in jack */
1702         case 7: /* line in jacks */
1703         case 19: /* speaker out jacks */
1704         case 20: /* headphones out jack */
1705                 break;
1706         /* live24ext: 4 = line-in jack */
1707         case 3: /* hp-out jack (may actuate Mute) */
1708                 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040))
1709                         snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1710                 break;
1711         default:
1712                 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1713                 break;
1714         }
1715 }
1716
1717 static void snd_usb_mixer_status_complete(struct urb *urb)
1718 {
1719         struct usb_mixer_interface *mixer = urb->context;
1720
1721         if (urb->status == 0) {
1722                 u8 *buf = urb->transfer_buffer;
1723                 int i;
1724
1725                 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1726                         snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1727                                    buf[0], buf[1]);
1728                         /* ignore any notifications not from the control interface */
1729                         if ((buf[0] & 0x0f) != 0)
1730                                 continue;
1731                         if (!(buf[0] & 0x40))
1732                                 snd_usb_mixer_notify_id(mixer, buf[1]);
1733                         else
1734                                 snd_usb_mixer_memory_change(mixer, buf[1]);
1735                 }
1736         }
1737         if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1738                 urb->dev = mixer->chip->dev;
1739                 usb_submit_urb(urb, GFP_ATOMIC);
1740         }
1741 }
1742
1743 /* create the handler for the optional status interrupt endpoint */
1744 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1745 {
1746         struct usb_host_interface *hostif;
1747         struct usb_endpoint_descriptor *ep;
1748         void *transfer_buffer;
1749         int buffer_length;
1750         unsigned int epnum;
1751
1752         hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1753         /* we need one interrupt input endpoint */
1754         if (get_iface_desc(hostif)->bNumEndpoints < 1)
1755                 return 0;
1756         ep = get_endpoint(hostif, 0);
1757         if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
1758                 return 0;
1759
1760         epnum = usb_endpoint_num(ep);
1761         buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1762         transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1763         if (!transfer_buffer)
1764                 return -ENOMEM;
1765         mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1766         if (!mixer->urb) {
1767                 kfree(transfer_buffer);
1768                 return -ENOMEM;
1769         }
1770         usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1771                          usb_rcvintpipe(mixer->chip->dev, epnum),
1772                          transfer_buffer, buffer_length,
1773                          snd_usb_mixer_status_complete, mixer, ep->bInterval);
1774         usb_submit_urb(mixer->urb, GFP_KERNEL);
1775         return 0;
1776 }
1777
1778 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
1779 {
1780         struct usb_mixer_interface *mixer = urb->context;
1781         const struct rc_config *rc = mixer->rc_cfg;
1782         u32 code;
1783
1784         if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
1785                 return;
1786
1787         code = mixer->rc_buffer[rc->offset];
1788         if (rc->length == 2)
1789                 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1790
1791         /* the Mute button actually changes the mixer control */
1792         if (code == rc->mute_code)
1793                 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
1794         mixer->rc_code = code;
1795         wmb();
1796         wake_up(&mixer->rc_waitq);
1797 }
1798
1799 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
1800                                      long count, loff_t *offset)
1801 {
1802         struct usb_mixer_interface *mixer = hw->private_data;
1803         int err;
1804         u32 rc_code;
1805
1806         if (count != 1 && count != 4)
1807                 return -EINVAL;
1808         err = wait_event_interruptible(mixer->rc_waitq,
1809                                        (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1810         if (err == 0) {
1811                 if (count == 1)
1812                         err = put_user(rc_code, buf);
1813                 else
1814                         err = put_user(rc_code, (u32 __user *)buf);
1815         }
1816         return err < 0 ? err : count;
1817 }
1818
1819 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
1820                                             poll_table *wait)
1821 {
1822         struct usb_mixer_interface *mixer = hw->private_data;
1823
1824         poll_wait(file, &mixer->rc_waitq, wait);
1825         return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1826 }
1827
1828 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1829 {
1830         struct snd_hwdep *hwdep;
1831         int err, len, i;
1832
1833         for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1834                 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1835                         break;
1836         if (i >= ARRAY_SIZE(rc_configs))
1837                 return 0;
1838         mixer->rc_cfg = &rc_configs[i];
1839
1840         len = mixer->rc_cfg->packet_length;
1841         
1842         init_waitqueue_head(&mixer->rc_waitq);
1843         err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1844         if (err < 0)
1845                 return err;
1846         snprintf(hwdep->name, sizeof(hwdep->name),
1847                  "%s remote control", mixer->chip->card->shortname);
1848         hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1849         hwdep->private_data = mixer;
1850         hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1851         hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1852         hwdep->exclusive = 1;
1853
1854         mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1855         if (!mixer->rc_urb)
1856                 return -ENOMEM;
1857         mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1858         if (!mixer->rc_setup_packet) {
1859                 usb_free_urb(mixer->rc_urb);
1860                 mixer->rc_urb = NULL;
1861                 return -ENOMEM;
1862         }
1863         mixer->rc_setup_packet->bRequestType =
1864                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1865         mixer->rc_setup_packet->bRequest = GET_MEM;
1866         mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1867         mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1868         mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1869         usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1870                              usb_rcvctrlpipe(mixer->chip->dev, 0),
1871                              (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1872                              snd_usb_soundblaster_remote_complete, mixer);
1873         return 0;
1874 }
1875
1876 #define snd_audigy2nx_led_info          snd_ctl_boolean_mono_info
1877
1878 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1879 {
1880         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1881         int index = kcontrol->private_value;
1882
1883         ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1884         return 0;
1885 }
1886
1887 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1888 {
1889         struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1890         int index = kcontrol->private_value;
1891         int value = ucontrol->value.integer.value[0];
1892         int err, changed;
1893
1894         if (value > 1)
1895                 return -EINVAL;
1896         changed = value != mixer->audigy2nx_leds[index];
1897         err = snd_usb_ctl_msg(mixer->chip->dev,
1898                               usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1899                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1900                               value, index + 2, NULL, 0, 100);
1901         if (err < 0)
1902                 return err;
1903         mixer->audigy2nx_leds[index] = value;
1904         return changed;
1905 }
1906
1907 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
1908         {
1909                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1910                 .name = "CMSS LED Switch",
1911                 .info = snd_audigy2nx_led_info,
1912                 .get = snd_audigy2nx_led_get,
1913                 .put = snd_audigy2nx_led_put,
1914                 .private_value = 0,
1915         },
1916         {
1917                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1918                 .name = "Power LED Switch",
1919                 .info = snd_audigy2nx_led_info,
1920                 .get = snd_audigy2nx_led_get,
1921                 .put = snd_audigy2nx_led_put,
1922                 .private_value = 1,
1923         },
1924         {
1925                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1926                 .name = "Dolby Digital LED Switch",
1927                 .info = snd_audigy2nx_led_info,
1928                 .get = snd_audigy2nx_led_get,
1929                 .put = snd_audigy2nx_led_put,
1930                 .private_value = 2,
1931         },
1932 };
1933
1934 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1935 {
1936         int i, err;
1937
1938         for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
1939                 if (i > 1 &&  /* Live24ext has 2 LEDs only */
1940                         mixer->chip->usb_id == USB_ID(0x041e, 0x3040))
1941                         break; 
1942                 err = snd_ctl_add(mixer->chip->card,
1943                                   snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1944                 if (err < 0)
1945                         return err;
1946         }
1947         mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1948         return 0;
1949 }
1950
1951 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
1952                                     struct snd_info_buffer *buffer)
1953 {
1954         static const struct sb_jack {
1955                 int unitid;
1956                 const char *name;
1957         }  jacks_audigy2nx[] = {
1958                 {4,  "dig in "},
1959                 {7,  "line in"},
1960                 {19, "spk out"},
1961                 {20, "hph out"},
1962                 {-1, NULL}
1963         }, jacks_live24ext[] = {
1964                 {4,  "line in"}, /* &1=Line, &2=Mic*/
1965                 {3,  "hph out"}, /* headphones */
1966                 {0,  "RC     "}, /* last command, 6 bytes see rc_config above */
1967                 {-1, NULL}
1968         };
1969         const struct sb_jack *jacks;
1970         struct usb_mixer_interface *mixer = entry->private_data;
1971         int i, err;
1972         u8 buf[3];
1973
1974         snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
1975         if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
1976                 jacks = jacks_audigy2nx;
1977         else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040))
1978                 jacks = jacks_live24ext;
1979         else
1980                 return;
1981
1982         for (i = 0; jacks[i].name; ++i) {
1983                 snd_iprintf(buffer, "%s: ", jacks[i].name);
1984                 err = snd_usb_ctl_msg(mixer->chip->dev,
1985                                       usb_rcvctrlpipe(mixer->chip->dev, 0),
1986                                       GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
1987                                       USB_RECIP_INTERFACE, 0,
1988                                       jacks[i].unitid << 8, buf, 3, 100);
1989                 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
1990                         snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
1991                 else
1992                         snd_iprintf(buffer, "?\n");
1993         }
1994 }
1995
1996 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
1997                          int ignore_error)
1998 {
1999         static struct snd_device_ops dev_ops = {
2000                 .dev_free = snd_usb_mixer_dev_free
2001         };
2002         struct usb_mixer_interface *mixer;
2003         int err;
2004
2005         strcpy(chip->card->mixername, "USB Mixer");
2006
2007         mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
2008         if (!mixer)
2009                 return -ENOMEM;
2010         mixer->chip = chip;
2011         mixer->ctrlif = ctrlif;
2012         mixer->ignore_ctl_error = ignore_error;
2013         mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2014         if (!mixer->id_elems) {
2015                 kfree(mixer);
2016                 return -ENOMEM;
2017         }
2018
2019         if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
2020             (err = snd_usb_mixer_status_create(mixer)) < 0)
2021                 goto _error;
2022
2023         if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2024                 goto _error;
2025
2026         if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
2027             mixer->chip->usb_id == USB_ID(0x041e, 0x3040)) {
2028                 struct snd_info_entry *entry;
2029
2030                 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2031                         goto _error;
2032                 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
2033                         snd_info_set_text_ops(entry, mixer,
2034                                               snd_audigy2nx_proc_read);
2035         }
2036
2037         err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
2038         if (err < 0)
2039                 goto _error;
2040         list_add(&mixer->list, &chip->mixer_list);
2041         return 0;
2042
2043 _error:
2044         snd_usb_mixer_free(mixer);
2045         return err;
2046 }
2047
2048 void snd_usb_mixer_disconnect(struct list_head *p)
2049 {
2050         struct usb_mixer_interface *mixer;
2051         
2052         mixer = list_entry(p, struct usb_mixer_interface, list);
2053         usb_kill_urb(mixer->urb);
2054         usb_kill_urb(mixer->rc_urb);
2055 }