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