e28365ca9996fc27734d33965a6a40f7721c7c31
[linux-flexiantxendom0-3.2.10.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 <sound/driver.h>
30 #include <linux/bitops.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/usb.h>
36 #include <sound/core.h>
37 #include <sound/control.h>
38
39 #include "usbaudio.h"
40
41
42 /*
43  */
44
45 /* ignore error from controls - for debugging */
46 /* #define IGNORE_CTL_ERROR */
47
48 typedef struct usb_mixer_build mixer_build_t;
49 typedef struct usb_audio_term usb_audio_term_t;
50 typedef struct usb_mixer_elem_info usb_mixer_elem_info_t;
51
52
53 struct usb_audio_term {
54         int id;
55         int type;
56         int channels;
57         unsigned int chconfig;
58         int name;
59 };
60
61 struct usbmix_name_map;
62
63 struct usb_mixer_build {
64         snd_usb_audio_t *chip;
65         unsigned char *buffer;
66         unsigned int buflen;
67         unsigned int ctrlif;
68         unsigned short vendor;
69         unsigned short product;
70         DECLARE_BITMAP(unitbitmap, 32*32);
71         usb_audio_term_t oterm;
72         const struct usbmix_name_map *map;
73 };
74
75 struct usb_mixer_elem_info {
76         snd_usb_audio_t *chip;
77         unsigned int ctrlif;
78         unsigned int id;
79         unsigned int control;   /* CS or ICN (high byte) */
80         unsigned int cmask; /* channel mask bitmap: 0 = master */ 
81         int channels;
82         int val_type;
83         int min, max, res;
84         unsigned int initialized: 1;
85 };
86
87
88 enum {
89         USB_FEATURE_NONE = 0,
90         USB_FEATURE_MUTE = 1,
91         USB_FEATURE_VOLUME,
92         USB_FEATURE_BASS,
93         USB_FEATURE_MID,
94         USB_FEATURE_TREBLE,
95         USB_FEATURE_GEQ,
96         USB_FEATURE_AGC,
97         USB_FEATURE_DELAY,
98         USB_FEATURE_BASSBOOST,
99         FSB_FEATURE_LOUDNESS
100 };
101
102 enum {
103         USB_MIXER_BOOLEAN,
104         USB_MIXER_INV_BOOLEAN,
105         USB_MIXER_S8,
106         USB_MIXER_U8,
107         USB_MIXER_S16,
108         USB_MIXER_U16,
109 };
110
111 enum {
112         USB_PROC_UPDOWN = 1,
113         USB_PROC_UPDOWN_SWITCH = 1,
114         USB_PROC_UPDOWN_MODE_SEL = 2,
115
116         USB_PROC_PROLOGIC = 2,
117         USB_PROC_PROLOGIC_SWITCH = 1,
118         USB_PROC_PROLOGIC_MODE_SEL = 2,
119
120         USB_PROC_3DENH = 3,
121         USB_PROC_3DENH_SWITCH = 1,
122         USB_PROC_3DENH_SPACE = 2,
123
124         USB_PROC_REVERB = 4,
125         USB_PROC_REVERB_SWITCH = 1,
126         USB_PROC_REVERB_LEVEL = 2,
127         USB_PROC_REVERB_TIME = 3,
128         USB_PROC_REVERB_DELAY = 4,
129
130         USB_PROC_CHORUS = 5,
131         USB_PROC_CHORUS_SWITCH = 1,
132         USB_PROC_CHORUS_LEVEL = 2,
133         USB_PROC_CHORUS_RATE = 3,
134         USB_PROC_CHORUS_DEPTH = 4,
135
136         USB_PROC_DCR = 6,
137         USB_PROC_DCR_SWITCH = 1,
138         USB_PROC_DCR_RATIO = 2,
139         USB_PROC_DCR_MAX_AMP = 3,
140         USB_PROC_DCR_THRESHOLD = 4,
141         USB_PROC_DCR_ATTACK = 5,
142         USB_PROC_DCR_RELEASE = 6,
143 };
144
145 #define MAX_CHANNELS    10      /* max logical channels */
146
147
148 /*
149  * manual mapping of mixer names
150  * if the mixer topology is too complicated and the parsed names are
151  * ambiguous, add the entries in usbmixer_maps.c.
152  */
153 #include "usbmixer_maps.c"
154
155 /* get the mapped name if the unit matches */
156 static int check_mapped_name(mixer_build_t *state, int unitid, int control, char *buf, int buflen)
157 {
158         const struct usbmix_name_map *p;
159
160         if (! state->map)
161                 return 0;
162
163         for (p = state->map; p->id; p++) {
164                 if (p->id == unitid && p->name &&
165                     (! control || ! p->control || control == p->control)) {
166                         buflen--;
167                         return strlcpy(buf, p->name, buflen);
168                 }
169         }
170         return 0;
171 }
172
173 /* check whether the control should be ignored */
174 static int check_ignored_ctl(mixer_build_t *state, int unitid, int control)
175 {
176         const struct usbmix_name_map *p;
177
178         if (! state->map)
179                 return 0;
180         for (p = state->map; p->id; p++) {
181                 if (p->id == unitid && ! p->name &&
182                     (! control || ! p->control || control == p->control)) {
183                         // printk("ignored control %d:%d\n", unitid, control);
184                         return 1;
185                 }
186         }
187         return 0;
188 }
189
190 /*
191  * find an audio control unit with the given unit id
192  */
193 static void *find_audio_control_unit(mixer_build_t *state, unsigned char unit)
194 {
195         unsigned char *p;
196
197         p = NULL;
198         while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
199                                       USB_DT_CS_INTERFACE)) != NULL) {
200                 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
201                         return p;
202         }
203         return NULL;
204 }
205
206
207 /*
208  * copy a string with the given id
209  */
210 static int snd_usb_copy_string_desc(mixer_build_t *state, int index, char *buf, int maxlen)
211 {
212         int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
213         buf[len] = 0;
214         return len;
215 }
216
217 /*
218  * convert from the byte/word on usb descriptor to the zero-based integer
219  */
220 static int convert_signed_value(usb_mixer_elem_info_t *cval, int val)
221 {
222         switch (cval->val_type) {
223         case USB_MIXER_BOOLEAN:
224                 return !!val;
225         case USB_MIXER_INV_BOOLEAN:
226                 return !val;
227         case USB_MIXER_U8:
228                 val &= 0xff;
229                 break;
230         case USB_MIXER_S8:
231                 val &= 0xff;
232                 if (val >= 0x80)
233                         val -= 0x100;
234                 break;
235         case USB_MIXER_U16:
236                 val &= 0xffff;
237                 break;
238         case USB_MIXER_S16:
239                 val &= 0xffff;
240                 if (val >= 0x8000)
241                         val -= 0x10000;
242                 break;
243         }
244         return val;
245 }
246
247 /*
248  * convert from the zero-based int to the byte/word for usb descriptor
249  */
250 static int convert_bytes_value(usb_mixer_elem_info_t *cval, int val)
251 {
252         switch (cval->val_type) {
253         case USB_MIXER_BOOLEAN:
254                 return !!val;
255         case USB_MIXER_INV_BOOLEAN:
256                 return !val;
257         case USB_MIXER_S8:
258         case USB_MIXER_U8:
259                 return val & 0xff;
260         case USB_MIXER_S16:
261         case USB_MIXER_U16:
262                 return val & 0xffff;
263         }
264         return 0; /* not reached */
265 }
266
267 static int get_relative_value(usb_mixer_elem_info_t *cval, int val)
268 {
269         if (! cval->res)
270                 cval->res = 1;
271         if (val < cval->min)
272                 return 0;
273         else if (val > cval->max)
274                 return (cval->max - cval->min) / cval->res;
275         else
276                 return (val - cval->min) / cval->res;
277 }
278
279 static int get_abs_value(usb_mixer_elem_info_t *cval, int val)
280 {
281         if (val < 0)
282                 return cval->min;
283         if (! cval->res)
284                 cval->res = 1;
285         val *= cval->res;
286         val += cval->min;
287         if (val > cval->max)
288                 return cval->max;
289         return val;
290 }
291
292
293 /*
294  * retrieve a mixer value
295  */
296
297 static int get_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int *value_ret)
298 {
299         unsigned char buf[2];
300         int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
301         int timeout = 10;
302  
303         while (timeout-- > 0) {
304                 if (usb_control_msg(cval->chip->dev, usb_rcvctrlpipe(cval->chip->dev, 0),
305                                     request,
306                                     USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
307                                     validx, cval->ctrlif | (cval->id << 8),
308                                     buf, val_len, HZ / 10) >= 0) {
309                         *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
310                         return 0;
311                 }
312         }
313         snd_printdd(KERN_ERR "cannot get ctl value: req = 0x%x, wValue = 0x%x, wIndex = 0x%x, type = %d\n", request, validx, cval->ctrlif | (cval->id << 8), cval->val_type);
314         return -EINVAL;
315 }
316
317 static int get_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int *value)
318 {
319         return get_ctl_value(cval, GET_CUR, validx, value);
320 }
321
322 /* channel = 0: master, 1 = first channel */
323 inline static int get_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int *value)
324 {
325         return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
326 }
327
328 /*
329  * set a mixer value
330  */
331
332 static int set_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int value_set)
333 {
334         unsigned char buf[2];
335         int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
336         int timeout = 10;
337  
338         value_set = convert_bytes_value(cval, value_set);
339         buf[0] = value_set & 0xff;
340         buf[1] = (value_set >> 8) & 0xff;
341         while (timeout -- > 0)
342                 if (usb_control_msg(cval->chip->dev, usb_sndctrlpipe(cval->chip->dev, 0),
343                                     request,
344                                     USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
345                                     validx, cval->ctrlif | (cval->id << 8),
346                                     buf, val_len, HZ / 10) >= 0)
347                         return 0;
348         snd_printdd(KERN_ERR "cannot set ctl value: req = 0x%x, wValue = 0x%x, wIndex = 0x%x, type = %d, data = 0x%x/0x%x\n", request, validx, cval->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
349         return -EINVAL;
350 }
351
352 static int set_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int value)
353 {
354         return set_ctl_value(cval, SET_CUR, validx, value);
355 }
356
357 inline static int set_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int value)
358 {
359         return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
360 }
361
362
363 /*
364  * parser routines begin here... 
365  */
366
367 static int parse_audio_unit(mixer_build_t *state, int unitid);
368
369
370 /*
371  * check if the input/output channel routing is enabled on the given bitmap.
372  * used for mixer unit parser
373  */
374 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
375 {
376         int idx = ich * num_outs + och;
377         return bmap[-(idx >> 3)] & (0x80 >> (idx & 7));
378 }
379
380
381 /*
382  * add an alsa control element
383  * search and increment the index until an empty slot is found.
384  *
385  * if failed, give up and free the control instance.
386  */
387
388 static int add_control_to_empty(snd_card_t *card, snd_kcontrol_t *kctl)
389 {
390         int err;
391         while (snd_ctl_find_id(card, &kctl->id))
392                 kctl->id.index++;
393         if ((err = snd_ctl_add(card, kctl)) < 0) {
394                 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
395                 snd_ctl_free_one(kctl);
396         }
397         return err;
398 }
399
400
401 /*
402  * get a terminal name string
403  */
404
405 static struct iterm_name_combo {
406         int type;
407         char *name;
408 } iterm_names[] = {
409         { 0x0300, "Output" },
410         { 0x0301, "Speaker" },
411         { 0x0302, "Headphone" },
412         { 0x0303, "HMD Audio" },
413         { 0x0304, "Desktop Speaker" },
414         { 0x0305, "Room Speaker" },
415         { 0x0306, "Com Speaker" },
416         { 0x0307, "LFE" },
417         { 0x0600, "External In" },
418         { 0x0601, "Analog In" },
419         { 0x0602, "Digital In" },
420         { 0x0603, "Line" },
421         { 0x0604, "Legacy In" },
422         { 0x0605, "IEC958 In" },
423         { 0x0606, "1394 DA Stream" },
424         { 0x0607, "1394 DV Stream" },
425         { 0x0700, "Embedded" },
426         { 0x0701, "Noise Source" },
427         { 0x0702, "Equalization Noise" },
428         { 0x0703, "CD" },
429         { 0x0704, "DAT" },
430         { 0x0705, "DCC" },
431         { 0x0706, "MiniDisk" },
432         { 0x0707, "Analog Tape" },
433         { 0x0708, "Phonograph" },
434         { 0x0709, "VCR Audio" },
435         { 0x070a, "Video Disk Audio" },
436         { 0x070b, "DVD Audio" },
437         { 0x070c, "TV Tuner Audio" },
438         { 0x070d, "Satellite Rec Audio" },
439         { 0x070e, "Cable Tuner Audio" },
440         { 0x070f, "DSS Audio" },
441         { 0x0710, "Radio Receiver" },
442         { 0x0711, "Radio Transmitter" },
443         { 0x0712, "Multi-Track Recorder" },
444         { 0x0713, "Synthesizer" },
445         { 0 },
446 };      
447
448 static int get_term_name(mixer_build_t *state, usb_audio_term_t *iterm,
449                          unsigned char *name, int maxlen, int term_only)
450 {
451         struct iterm_name_combo *names;
452
453         if (iterm->name)
454                 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
455
456         /* virtual type - not a real terminal */
457         if (iterm->type >> 16) {
458                 if (term_only)
459                         return 0;
460                 switch (iterm->type >> 16) {
461                 case SELECTOR_UNIT:
462                         strcpy(name, "Selector"); return 8;
463                 case PROCESSING_UNIT:
464                         strcpy(name, "Process Unit"); return 12;
465                 case EXTENSION_UNIT:
466                         strcpy(name, "Ext Unit"); return 8;
467                 case MIXER_UNIT:
468                         strcpy(name, "Mixer"); return 5;
469                 default:
470                         return sprintf(name, "Unit %d", iterm->id);
471                 }
472         }
473
474         switch (iterm->type & 0xff00) {
475         case 0x0100:
476                 strcpy(name, "PCM"); return 3;
477         case 0x0200:
478                 strcpy(name, "Mic"); return 3;
479         case 0x0400:
480                 strcpy(name, "Headset"); return 7;
481         case 0x0500:
482                 strcpy(name, "Phone"); return 5;
483         }
484
485         for (names = iterm_names; names->type; names++)
486                 if (names->type == iterm->type) {
487                         strcpy(name, names->name);
488                         return strlen(names->name);
489                 }
490         return 0;
491 }
492
493
494 /*
495  * parse the source unit recursively until it reaches to a terminal
496  * or a branched unit.
497  */
498 static int check_input_term(mixer_build_t *state, int id, usb_audio_term_t *term)
499 {
500         unsigned char *p1;
501
502         memset(term, 0, sizeof(*term));
503         while ((p1 = find_audio_control_unit(state, id)) != NULL) {
504                 term->id = id;
505                 switch (p1[2]) {
506                 case INPUT_TERMINAL:
507                         term->type = combine_word(p1 + 4);
508                         term->channels = p1[7];
509                         term->chconfig = combine_word(p1 + 8);
510                         term->name = p1[11];
511                         return 0;
512                 case FEATURE_UNIT:
513                         id = p1[4];
514                         break; /* continue to parse */
515                 case MIXER_UNIT:
516                         term->type = p1[2] << 16; /* virtual type */
517                         term->channels = p1[5 + p1[4]];
518                         term->chconfig = combine_word(p1 + 6 + p1[4]);
519                         term->name = p1[p1[0] - 1];
520                         return 0;
521                 case SELECTOR_UNIT:
522                         /* call recursively to retrieve the channel info */
523                         if (check_input_term(state, p1[5], term) < 0)
524                                 return -ENODEV;
525                         term->type = p1[2] << 16; /* virtual type */
526                         term->id = id;
527                         term->name = p1[9 + p1[0] - 1];
528                         return 0;
529                 case PROCESSING_UNIT:
530                 case EXTENSION_UNIT:
531                         if (p1[6] == 1) {
532                                 id = p1[7];
533                                 break; /* continue to parse */
534                         }
535                         term->type = p1[2] << 16; /* virtual type */
536                         term->channels = p1[7 + p1[6]];
537                         term->chconfig = combine_word(p1 + 8 + p1[6]);
538                         term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
539                         return 0;
540                 default:
541                         return -ENODEV;
542                 }
543         }
544         return -ENODEV;
545 }
546
547
548 /*
549  * Feature Unit
550  */
551
552 /* feature unit control information */
553 struct usb_feature_control_info {
554         const char *name;
555         unsigned int type;      /* control type (mute, volume, etc.) */
556 };
557
558 static struct usb_feature_control_info audio_feature_info[] = {
559         { "Mute",               USB_MIXER_INV_BOOLEAN },
560         { "Volume",             USB_MIXER_S16 },
561         { "Tone Control - Bass",        USB_MIXER_S8 },
562         { "Tone Control - Mid",         USB_MIXER_S8 },
563         { "Tone Control - Treble",      USB_MIXER_S8 },
564         { "Graphic Equalizer",          USB_MIXER_S8 }, /* FIXME: not implemeted yet */
565         { "Auto Gain Control",  USB_MIXER_BOOLEAN },
566         { "Delay Control",      USB_MIXER_U16 },
567         { "Bass Boost",         USB_MIXER_BOOLEAN },
568         { "Loudness",           USB_MIXER_BOOLEAN },
569 };
570
571
572 /* private_free callback */
573 static void usb_mixer_elem_free(snd_kcontrol_t *kctl)
574 {
575         if (kctl->private_data) {
576                 snd_magic_kfree((void *)kctl->private_data);
577                 kctl->private_data = 0;
578         }
579 }
580
581
582 /*
583  * interface to ALSA control for feature/mixer units
584  */
585
586 /*
587  * retrieve the minimum and maximum values for the specified control
588  */
589 static int get_min_max(usb_mixer_elem_info_t *cval, int default_min)
590 {
591         /* for failsafe */
592         cval->min = default_min;
593         cval->max = cval->min + 1;
594         cval->res = 1;
595
596         if (cval->val_type == USB_MIXER_BOOLEAN ||
597             cval->val_type == USB_MIXER_INV_BOOLEAN) {
598                 cval->initialized = 1;
599         } else {
600                 int minchn = 0;
601                 if (cval->cmask) {
602                         int i;
603                         for (i = 0; i < MAX_CHANNELS; i++)
604                                 if (cval->cmask & (1 << i)) {
605                                         minchn = i + 1;
606                                         break;
607                                 }
608                 }
609                 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
610                     get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
611                         snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n", cval->id, cval->ctrlif, cval->control, cval->id);
612                         return -EINVAL;
613                 }
614                 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
615                         cval->res = 1;
616                 } else {
617                         int last_valid_res = cval->res;
618                 
619                         while (cval->res > 1) {
620                                 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
621                                         break;
622                                 cval->res /= 2;
623                         }
624                         if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
625                                 cval->res = last_valid_res;
626                 }
627                 if (cval->res == 0)
628                         cval->res = 1;
629                 cval->initialized = 1;
630         }
631         return 0;
632 }
633
634
635 /* get a feature/mixer unit info */
636 static int mixer_ctl_feature_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
637 {       
638         usb_mixer_elem_info_t *cval = snd_magic_cast(usb_mixer_elem_info_t, kcontrol->private_data, return -EINVAL);
639
640         if (cval->val_type == USB_MIXER_BOOLEAN ||
641             cval->val_type == USB_MIXER_INV_BOOLEAN)
642                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
643         else
644                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
645         uinfo->count = cval->channels;
646         if (cval->val_type == USB_MIXER_BOOLEAN ||
647             cval->val_type == USB_MIXER_INV_BOOLEAN) {
648                 uinfo->value.integer.min = 0;
649                 uinfo->value.integer.max = 1;
650         } else {
651                 if (! cval->initialized)
652                         get_min_max(cval,  0);
653                 uinfo->value.integer.min = 0;
654                 uinfo->value.integer.max = (cval->max - cval->min) / cval->res;
655         }
656         return 0;
657 }
658
659 /* get the current value from feature/mixer unit */
660 static int mixer_ctl_feature_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
661 {
662         usb_mixer_elem_info_t *cval = snd_magic_cast(usb_mixer_elem_info_t, kcontrol->private_data, return -EINVAL);
663         int c, cnt, val, err;
664
665         if (cval->cmask) {
666                 cnt = 0;
667                 for (c = 0; c < MAX_CHANNELS; c++) {
668                         if (cval->cmask & (1 << c)) {
669                                 err = get_cur_mix_value(cval, c + 1, &val);
670 #ifdef IGNORE_CTL_ERROR
671                                 if (err < 0) {
672                                         ucontrol->value.integer.value[0] = cval->min;
673                                         return 0;
674                                 }
675 #endif
676                                 if (err < 0) {
677                                         snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
678                                         return err;
679                                 }
680                                 val = get_relative_value(cval, val);
681                                 ucontrol->value.integer.value[cnt] = val;
682                                 cnt++;
683                         }
684                 }
685         } else {
686                 /* master channel */
687                 err = get_cur_mix_value(cval, 0, &val);
688 #ifdef IGNORE_CTL_ERROR
689                 if (err < 0) {
690                         ucontrol->value.integer.value[0] = cval->min;
691                         return 0;
692                 }
693 #endif
694                 if (err < 0) {
695                         snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
696                         return err;
697                 }
698                 val = get_relative_value(cval, val);
699                 ucontrol->value.integer.value[0] = val;
700         }
701         return 0;
702 }
703
704 /* put the current value to feature/mixer unit */
705 static int mixer_ctl_feature_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
706 {
707         usb_mixer_elem_info_t *cval = snd_magic_cast(usb_mixer_elem_info_t, kcontrol->private_data, return -EINVAL);
708         int c, cnt, val, oval, err;
709         int changed = 0;
710
711         if (cval->cmask) {
712                 cnt = 0;
713                 for (c = 0; c < MAX_CHANNELS; c++) {
714                         if (cval->cmask & (1 << c)) {
715                                 err = get_cur_mix_value(cval, c + 1, &oval);
716 #ifdef IGNORE_CTL_ERROR
717                                 if (err < 0)
718                                         return 0;
719 #endif
720                                 if (err < 0)
721                                         return err;
722                                 val = ucontrol->value.integer.value[cnt];
723                                 val = get_abs_value(cval, val);
724                                 if (oval != val) {
725                                         set_cur_mix_value(cval, c + 1, val);
726                                         changed = 1;
727                                 }
728                                 get_cur_mix_value(cval, c + 1, &val);
729                                 cnt++;
730                         }
731                 }
732         } else {
733                 /* master channel */
734                 err = get_cur_mix_value(cval, 0, &oval);
735 #ifdef IGNORE_CTL_ERROR
736                 if (err < 0)
737                         return 0;
738 #endif
739                 if (err < 0)
740                         return err;
741                 val = ucontrol->value.integer.value[0];
742                 val = get_abs_value(cval, val);
743                 if (val != oval) {
744                         set_cur_mix_value(cval, 0, val);
745                         changed = 1;
746                 }
747         }
748         return changed;
749 }
750
751 static snd_kcontrol_new_t usb_feature_unit_ctl = {
752         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
753         .name = "", /* will be filled later manually */
754         .info = mixer_ctl_feature_info,
755         .get = mixer_ctl_feature_get,
756         .put = mixer_ctl_feature_put,
757 };
758
759
760 /*
761  * build a feature control
762  */
763
764 static void build_feature_ctl(mixer_build_t *state, unsigned char *desc,
765                               unsigned int ctl_mask, int control,
766                               usb_audio_term_t *iterm, int unitid)
767 {
768         unsigned int len = 0;
769         int mapped_name = 0;
770         int nameid = desc[desc[0] - 1];
771         snd_kcontrol_t *kctl;
772         usb_mixer_elem_info_t *cval;
773
774         control++; /* change from zero-based to 1-based value */
775
776         if (control == USB_FEATURE_GEQ) {
777                 /* FIXME: not supported yet */
778                 return;
779         }
780
781         if (check_ignored_ctl(state, unitid, control))
782                 return;
783
784         cval = snd_magic_kcalloc(usb_mixer_elem_info_t, 0, GFP_KERNEL);
785         if (! cval) {
786                 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
787                 return;
788         }
789         cval->chip = state->chip;
790         cval->ctrlif = state->ctrlif;
791         cval->id = unitid;
792         cval->control = control;
793         cval->cmask = ctl_mask;
794         cval->val_type = audio_feature_info[control-1].type;
795         if (ctl_mask == 0)
796                 cval->channels = 1;     /* master channel */
797         else {
798                 int i, c = 0;
799                 for (i = 0; i < 16; i++)
800                         if (ctl_mask & (1 << i))
801                                 c++;
802                 cval->channels = c;
803         }
804
805         /* get min/max values */
806         get_min_max(cval, 0);
807
808         kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
809         if (! kctl) {
810                 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
811                 snd_magic_kfree(cval);
812                 return;
813         }
814         kctl->private_free = usb_mixer_elem_free;
815
816         len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
817         mapped_name = len != 0;
818         if (! len && nameid)
819                 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
820
821         switch (control) {
822         case USB_FEATURE_MUTE:
823         case USB_FEATURE_VOLUME:
824                 /* determine the control name.  the rule is:
825                  * - if a name id is given in descriptor, use it.
826                  * - if the connected input can be determined, then use the name
827                  *   of terminal type.
828                  * - if the connected output can be determined, use it.
829                  * - otherwise, anonymous name.
830                  */
831                 if (! len) {
832                         len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
833                         if (! len)
834                                 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
835                         if (! len)
836                                 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
837                                                "Feature %d", unitid);
838                 }
839                 /* determine the stream direction:
840                  * if the connected output is USB stream, then it's likely a
841                  * capture stream.  otherwise it should be playback (hopefully :)
842                  */
843                 if (! mapped_name && ! (state->oterm.type >> 16)) {
844                         if ((state->oterm.type & 0xff00) == 0x0100) {
845                                 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
846                         } else {
847                                 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
848                         }
849                 }
850                 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
851                         sizeof(kctl->id.name));
852                 break;
853
854         default:
855                 if (! len)
856                         strlcpy(kctl->id.name, audio_feature_info[control-1].name,
857                                 sizeof(kctl->id.name));
858                 break;
859         }
860
861         /* quirk for UDA1321/N101 */
862         /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
863         /* is not very clear from datasheets */
864         /* I hope that the min value is -15360 for newer firmware --jk */
865         if (((state->vendor == 0x471 && (state->product == 0x104 || state->product == 0x105 || state->product == 0x101)) ||
866              (state->vendor == 0x672 && state->product == 0x1041)) && !strcmp(kctl->id.name, "PCM Playback Volume") &&
867              cval->min == -15616) {
868                 snd_printk("USB Audio: using volume control quirk for the UDA1321/N101 chip\n");
869                 cval->max = -256;
870         }
871
872         snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
873                     cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
874         add_control_to_empty(state->chip->card, kctl);
875 }
876
877
878
879 /*
880  * parse a feature unit
881  *
882  * most of controlls are defined here.
883  */
884 static int parse_audio_feature_unit(mixer_build_t *state, int unitid, unsigned char *ftr)
885 {
886         int channels, i, j;
887         usb_audio_term_t iterm;
888         unsigned int master_bits, first_ch_bits;
889         int err, csize;
890
891         if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
892                 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
893                 return -EINVAL;
894         }
895
896         /* parse the source unit */
897         if ((err = parse_audio_unit(state, ftr[4])) < 0)
898                 return err;
899
900         /* determine the input source type and name */
901         if (check_input_term(state, ftr[4], &iterm) < 0)
902                 return -EINVAL;
903
904         channels = (ftr[0] - 7) / csize - 1;
905
906         master_bits = snd_usb_combine_bytes(ftr + 6, csize);
907         if (channels > 0)
908                 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
909         else
910                 first_ch_bits = 0;
911         /* check all control types */
912         for (i = 0; i < 10; i++) {
913                 unsigned int ch_bits = 0;
914                 for (j = 0; j < channels; j++) {
915                         unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
916                         if (mask & (1 << i))
917                                 ch_bits |= (1 << j);
918                 }
919                 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
920                         build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
921                 if (master_bits & (1 << i))
922                         build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
923         }
924         
925         return 0;
926 }
927
928
929 /*
930  * Mixer Unit
931  */
932
933 /*
934  * build a mixer unit control
935  *
936  * the callbacks are identical with feature unit.
937  * input channel number (zero based) is given in control field instead.
938  */
939
940 static void build_mixer_unit_ctl(mixer_build_t *state, unsigned char *desc,
941                                  int in_ch, int unitid)
942 {
943         usb_mixer_elem_info_t *cval;
944         unsigned int num_ins = desc[4];
945         unsigned int num_outs = desc[5 + num_ins];
946         unsigned int i, len;
947         snd_kcontrol_t *kctl;
948         usb_audio_term_t iterm;
949
950         if (check_ignored_ctl(state, unitid, 0))
951                 return;
952
953         cval = snd_magic_kcalloc(usb_mixer_elem_info_t, 0, GFP_KERNEL);
954         if (! cval)
955                 return;
956
957         if (check_input_term(state, desc[5 + in_ch], &iterm) < 0)
958                 return;
959
960         cval->chip = state->chip;
961         cval->ctrlif = state->ctrlif;
962         cval->id = unitid;
963         cval->control = in_ch + 1; /* based on 1 */
964         cval->val_type = USB_MIXER_S16;
965         for (i = 0; i < num_outs; i++) {
966                 if (check_matrix_bitmap(desc + 9 + num_ins, in_ch, i, num_outs)) {
967                         cval->cmask |= (1 << i);
968                         cval->channels++;
969                 }
970         }
971
972         /* get min/max values */
973         get_min_max(cval, 0);
974
975         kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
976         if (! kctl) {
977                 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
978                 snd_magic_kfree(cval);
979                 return;
980         }
981         kctl->private_free = usb_mixer_elem_free;
982
983         len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
984         if (! len)
985                 len = get_term_name(state, &iterm, kctl->id.name, sizeof(kctl->id.name), 0);
986         if (! len)
987                 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch);
988         strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
989
990         snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
991                     cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
992         add_control_to_empty(state->chip->card, kctl);
993 }
994
995
996 /*
997  * parse a mixer unit
998  */
999 static int parse_audio_mixer_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1000 {
1001         int num_ins, num_outs;
1002         int i, err;
1003         if (desc[0] < 12 || ! (num_ins = desc[4]) || ! (num_outs = desc[5 + num_ins]))
1004                 return -EINVAL;
1005
1006         for (i = 0; i < num_ins; i++) {
1007                 err = parse_audio_unit(state, desc[5 + i]);
1008                 if (err < 0)
1009                         return err;
1010                 if (check_matrix_bitmap(desc + 9 + num_ins, i, 0, num_outs))
1011                         build_mixer_unit_ctl(state, desc, i, unitid);
1012         }
1013         return 0;
1014 }
1015
1016
1017 /*
1018  * Processing Unit / Extension Unit
1019  */
1020
1021 /* get callback for processing/extension unit */
1022 static int mixer_ctl_procunit_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1023 {
1024         usb_mixer_elem_info_t *cval = snd_magic_cast(usb_mixer_elem_info_t, kcontrol->private_data, return -EINVAL);
1025         int err, val;
1026
1027         err = get_cur_ctl_value(cval, cval->control << 8, &val);
1028 #ifdef IGNORE_CTL_ERROR
1029         if (err < 0) {
1030                 ucontrol->value.integer.value[0] = cval->min;
1031                 return 0;
1032         }
1033 #endif
1034         if (err < 0)
1035                 return err;
1036         val = get_relative_value(cval, val);
1037         ucontrol->value.integer.value[0] = val;
1038         return 0;
1039 }
1040
1041 /* put callback for processing/extension unit */
1042 static int mixer_ctl_procunit_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1043 {
1044         usb_mixer_elem_info_t *cval = snd_magic_cast(usb_mixer_elem_info_t, kcontrol->private_data, return -EINVAL);
1045         int val, oval, err;
1046
1047         err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1048 #ifdef IGNORE_CTL_ERROR
1049         if (err < 0)
1050                 return 0;
1051 #endif
1052         if (err < 0)
1053                 return err;
1054         val = ucontrol->value.integer.value[0];
1055         val = get_abs_value(cval, val);
1056         if (val != oval) {
1057                 set_cur_ctl_value(cval, cval->control << 8, val);
1058                 return 1;
1059         }
1060         return 0;
1061 }
1062
1063 /* alsa control interface for processing/extension unit */
1064 static snd_kcontrol_new_t mixer_procunit_ctl = {
1065         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1066         .name = "", /* will be filled later */
1067         .info = mixer_ctl_feature_info,
1068         .get = mixer_ctl_procunit_get,
1069         .put = mixer_ctl_procunit_put,
1070 };
1071
1072
1073 /*
1074  * predefined data for processing units
1075  */
1076 struct procunit_value_info {
1077         int control;
1078         char *suffix;
1079         int val_type;
1080         int min_value;
1081 };
1082
1083 struct procunit_info {
1084         int type;
1085         char *name;
1086         struct procunit_value_info *values;
1087 };
1088
1089 static struct procunit_value_info updown_proc_info[] = {
1090         { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1091         { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1092         { 0 }
1093 };
1094 static struct procunit_value_info prologic_proc_info[] = {
1095         { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1096         { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1097         { 0 }
1098 };
1099 static struct procunit_value_info threed_enh_proc_info[] = {
1100         { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1101         { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1102         { 0 }
1103 };
1104 static struct procunit_value_info reverb_proc_info[] = {
1105         { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1106         { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1107         { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1108         { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1109         { 0 }
1110 };
1111 static struct procunit_value_info chorus_proc_info[] = {
1112         { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1113         { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1114         { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1115         { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1116         { 0 }
1117 };
1118 static struct procunit_value_info dcr_proc_info[] = {
1119         { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1120         { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1121         { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1122         { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1123         { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1124         { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1125         { 0 }
1126 };
1127
1128 static struct procunit_info procunits[] = {
1129         { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1130         { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1131         { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1132         { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1133         { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1134         { USB_PROC_DCR, "DCR", dcr_proc_info },
1135         { 0 },
1136 };
1137
1138 /*
1139  * build a processing/extension unit
1140  */
1141 static int build_audio_procunit(mixer_build_t *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1142 {
1143         int num_ins = dsc[6];
1144         usb_mixer_elem_info_t *cval;
1145         snd_kcontrol_t *kctl;
1146         int i, err, nameid, type, len;
1147         struct procunit_info *info;
1148         struct procunit_value_info *valinfo;
1149         static struct procunit_value_info default_value_info[] = {
1150                 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1151                 { 0 }
1152         };
1153         static struct procunit_info default_info = {
1154                 0, NULL, default_value_info
1155         };
1156
1157         if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1158                 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1159                 return -EINVAL;
1160         }
1161
1162         for (i = 0; i < num_ins; i++) {
1163                 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1164                         return err;
1165         }
1166
1167         type = combine_word(&dsc[4]);
1168         if (! type)
1169                 return 0; /* undefined? */
1170
1171         for (info = list; info && info->type; info++)
1172                 if (info->type == type)
1173                         break;
1174         if (! info || ! info->type)
1175                 info = &default_info;
1176
1177         for (valinfo = info->values; valinfo->control; valinfo++) {
1178                 /* FIXME: bitmap might be longer than 8bit */
1179                 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1180                         continue;
1181                 if (check_ignored_ctl(state, unitid, valinfo->control))
1182                         continue;
1183                 cval = snd_magic_kcalloc(usb_mixer_elem_info_t, 0, GFP_KERNEL);
1184                 if (! cval) {
1185                         snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1186                         return -ENOMEM;
1187                 }
1188                 cval->chip = state->chip;
1189                 cval->ctrlif = state->ctrlif;
1190                 cval->id = unitid;
1191                 cval->control = valinfo->control;
1192                 cval->val_type = valinfo->val_type;
1193                 cval->channels = 1;
1194
1195                 /* get min/max values */
1196                 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1197                         /* FIXME: hard-coded */
1198                         cval->min = 1;
1199                         cval->max = dsc[15];
1200                         cval->res = 1;
1201                         cval->initialized = 1;
1202                 } else
1203                         get_min_max(cval, valinfo->min_value);
1204
1205                 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1206                 if (! kctl) {
1207                         snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1208                         snd_magic_kfree(cval);
1209                         return -ENOMEM;
1210                 }
1211                 kctl->private_free = usb_mixer_elem_free;
1212
1213                 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1214                         ;
1215                 else if (info->name)
1216                         strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1217                 else {
1218                         nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1219                         len = 0;
1220                         if (nameid)
1221                                 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1222                         if (! len)
1223                                 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1224                 }
1225                 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1226                 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1227
1228                 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1229                             cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1230                 if ((err = add_control_to_empty(state->chip->card, kctl)) < 0)
1231                         return err;
1232         }
1233         return 0;
1234 }
1235
1236
1237 static int parse_audio_processing_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1238 {
1239         return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1240 }
1241
1242 static int parse_audio_extension_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1243 {
1244         return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1245 }
1246
1247
1248 /*
1249  * Selector Unit
1250  */
1251
1252 /* info callback for selector unit
1253  * use an enumerator type for routing
1254  */
1255 static int mixer_ctl_selector_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1256 {       
1257         usb_mixer_elem_info_t *cval = snd_magic_cast(usb_mixer_elem_info_t, kcontrol->private_data, return -EINVAL);
1258         char **itemlist = (char **)kcontrol->private_value;
1259
1260         snd_assert(itemlist, return -EINVAL);
1261         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1262         uinfo->count = 1;
1263         uinfo->value.enumerated.items = cval->max;
1264         if ((int)uinfo->value.enumerated.item >= cval->max)
1265                 uinfo->value.enumerated.item = cval->max - 1;
1266         strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1267         return 0;
1268 }
1269
1270 /* get callback for selector unit */
1271 static int mixer_ctl_selector_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1272 {
1273         usb_mixer_elem_info_t *cval = snd_magic_cast(usb_mixer_elem_info_t, kcontrol->private_data, return -EINVAL);
1274         int val, err;
1275
1276         err = get_cur_ctl_value(cval, 0, &val);
1277 #ifdef IGNORE_CTL_ERROR
1278         if (err < 0) {
1279                 ucontrol->value.enumerated.item[0] = 0;
1280                 return 0;
1281         }
1282 #endif
1283         if (err < 0)
1284                 return err;
1285         val = get_relative_value(cval, val);
1286         ucontrol->value.enumerated.item[0] = val;
1287         return 0;
1288 }
1289
1290 /* put callback for selector unit */
1291 static int mixer_ctl_selector_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1292 {
1293         usb_mixer_elem_info_t *cval = snd_magic_cast(usb_mixer_elem_info_t, kcontrol->private_data, return -EINVAL);
1294         int val, oval, err;
1295
1296         err = get_cur_ctl_value(cval, 0, &oval);
1297 #ifdef IGNORE_CTL_ERROR
1298         if (err < 0)
1299                 return 0;
1300 #endif
1301         if (err < 0)
1302                 return err;
1303         val = ucontrol->value.enumerated.item[0];
1304         val = get_abs_value(cval, val);
1305         if (val != oval) {
1306                 set_cur_ctl_value(cval, 0, val);
1307                 return 1;
1308         }
1309         return 0;
1310 }
1311
1312 /* alsa control interface for selector unit */
1313 static snd_kcontrol_new_t mixer_selectunit_ctl = {
1314         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1315         .name = "", /* will be filled later */
1316         .info = mixer_ctl_selector_info,
1317         .get = mixer_ctl_selector_get,
1318         .put = mixer_ctl_selector_put,
1319 };
1320
1321
1322 /* private free callback.
1323  * free both private_data and private_value
1324  */
1325 static void usb_mixer_selector_elem_free(snd_kcontrol_t *kctl)
1326 {
1327         int i, num_ins = 0;
1328
1329         if (kctl->private_data) {
1330                 usb_mixer_elem_info_t *cval = snd_magic_cast(usb_mixer_elem_info_t, kctl->private_data,);
1331                 num_ins = cval->max;
1332                 snd_magic_kfree(cval);
1333                 kctl->private_data = 0;
1334         }
1335         if (kctl->private_value) {
1336                 char **itemlist = (char **)kctl->private_value;
1337                 for (i = 0; i < num_ins; i++)
1338                         kfree(itemlist[i]);
1339                 kfree(itemlist);
1340                 kctl->private_value = 0;
1341         }
1342 }
1343
1344 /*
1345  * parse a selector unit
1346  */
1347 static int parse_audio_selector_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1348 {
1349         unsigned int num_ins = desc[4];
1350         unsigned int i, nameid, len;
1351         int err;
1352         usb_mixer_elem_info_t *cval;
1353         snd_kcontrol_t *kctl;
1354         char **namelist;
1355
1356         if (! num_ins || desc[0] < 6 + num_ins) {
1357                 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1358                 return -EINVAL;
1359         }
1360
1361         if (check_ignored_ctl(state, unitid, 0))
1362                 return 0;
1363
1364         for (i = 0; i < num_ins; i++) {
1365                 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1366                         return err;
1367         }
1368
1369         cval = snd_magic_kcalloc(usb_mixer_elem_info_t, 0, GFP_KERNEL);
1370         if (! cval) {
1371                 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1372                 return -ENOMEM;
1373         }
1374         cval->chip = state->chip;
1375         cval->ctrlif = state->ctrlif;
1376         cval->id = unitid;
1377         cval->val_type = USB_MIXER_U8;
1378         cval->channels = 1;
1379         cval->min = 1;
1380         cval->max = num_ins;
1381         cval->res = 1;
1382         cval->initialized = 1;
1383
1384         namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1385         if (! namelist) {
1386                 snd_printk(KERN_ERR "cannot malloc\n");
1387                 snd_magic_kfree(cval);
1388                 return -ENOMEM;
1389         }
1390 #define MAX_ITEM_NAME_LEN       64
1391         for (i = 0; i < num_ins; i++) {
1392                 usb_audio_term_t iterm;
1393                 len = 0;
1394                 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1395                 if (! namelist[i]) {
1396                         snd_printk(KERN_ERR "cannot malloc\n");
1397                         while (--i > 0)
1398                                 kfree(namelist[i]);
1399                         kfree(namelist);
1400                         snd_magic_kfree(cval);
1401                         return -ENOMEM;
1402                 }
1403                 if (check_input_term(state, desc[5 + i], &iterm) >= 0)
1404                         len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1405                 if (! len)
1406                         sprintf(namelist[i], "Input %d", i);
1407         }
1408
1409         kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1410         if (! kctl) {
1411                 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1412                 snd_magic_kfree(cval);
1413                 return -ENOMEM;
1414         }
1415         kctl->private_value = (unsigned long)namelist;
1416         kctl->private_free = usb_mixer_selector_elem_free;
1417
1418         nameid = desc[desc[0] - 1];
1419         len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1420         if (len)
1421                 ;
1422         else if (nameid)
1423                 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1424         else {
1425                 len = get_term_name(state, &state->oterm,
1426                                     kctl->id.name, sizeof(kctl->id.name), 0);
1427                 if (! len)
1428                         strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1429
1430                 if ((state->oterm.type & 0xff00) == 0x0100)
1431                         strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1432                 else
1433                         strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1434         }
1435
1436         snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1437                     cval->id, kctl->id.name, num_ins);
1438         if ((err = add_control_to_empty(state->chip->card, kctl)) < 0)
1439                 return err;
1440
1441         return 0;
1442 }
1443
1444
1445 /*
1446  * parse an audio unit recursively
1447  */
1448
1449 static int parse_audio_unit(mixer_build_t *state, int unitid)
1450 {
1451         unsigned char *p1;
1452
1453         if (test_and_set_bit(unitid, state->unitbitmap))
1454                 return 0; /* the unit already visited */
1455
1456         p1 = find_audio_control_unit(state, unitid);
1457         if (!p1) {
1458                 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1459                 return -EINVAL;
1460         }
1461
1462         switch (p1[2]) {
1463         case INPUT_TERMINAL:
1464                 return 0; /* NOP */
1465         case MIXER_UNIT:
1466                 return parse_audio_mixer_unit(state, unitid, p1);
1467         case SELECTOR_UNIT:
1468                 return parse_audio_selector_unit(state, unitid, p1);
1469         case FEATURE_UNIT:
1470                 return parse_audio_feature_unit(state, unitid, p1);
1471         case PROCESSING_UNIT:
1472                 return parse_audio_processing_unit(state, unitid, p1);
1473         case EXTENSION_UNIT:
1474                 return parse_audio_extension_unit(state, unitid, p1);
1475         default:
1476                 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1477                 return -EINVAL;
1478         }
1479 }
1480
1481 /*
1482  * create mixer controls
1483  *
1484  * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1485  */
1486 int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif)
1487 {
1488         unsigned char *desc;
1489         mixer_build_t state;
1490         int err;
1491         const struct usbmix_ctl_map *map;
1492         struct usb_device_descriptor *dev = &chip->dev->descriptor;
1493         struct usb_host_interface *hostif = &chip->dev->actconfig->interface[ctrlif].altsetting[0];
1494
1495         strcpy(chip->card->mixername, "USB Mixer");
1496
1497         memset(&state, 0, sizeof(state));
1498         state.chip = chip;
1499         state.buffer = hostif->extra;
1500         state.buflen = hostif->extralen;
1501         state.ctrlif = ctrlif;
1502         state.vendor = dev->idVendor;
1503         state.product = dev->idProduct;
1504
1505         /* check the mapping table */
1506         for (map = usbmix_ctl_maps; map->vendor; map++) {
1507                 if (map->vendor == dev->idVendor && map->product == dev->idProduct) {
1508                         state.map = map->map;
1509                         break;
1510                 }
1511         }
1512
1513         desc = NULL;
1514         while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1515                 if (desc[0] < 9)
1516                         continue; /* invalid descriptor? */
1517                 set_bit(desc[3], state.unitbitmap);  /* mark terminal ID as visited */
1518                 state.oterm.id = desc[3];
1519                 state.oterm.type = combine_word(&desc[4]);
1520                 state.oterm.name = desc[8];
1521                 err = parse_audio_unit(&state, desc[7]);
1522                 if (err < 0)
1523                         return err;
1524         }
1525         return 0;
1526 }