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