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