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