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