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