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