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