2 * (Tentative) USB Audio Driver for ALSA
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
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.
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.
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
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 #include <sound/info.h>
46 /* ignore error from controls - for debugging */
47 /* #define IGNORE_CTL_ERROR */
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;
54 struct usb_mixer_interface {
55 snd_usb_audio_t *chip;
57 struct list_head list;
58 unsigned int ignore_ctl_error;
60 usb_mixer_elem_info_t **id_elems; /* array[256], indexed by unit id */
62 /* Sound Blaster remote control stuff */
68 unsigned long rc_hwdep_open;
70 wait_queue_head_t rc_waitq;
72 struct usb_ctrlrequest *rc_setup_packet;
79 struct usb_audio_term {
83 unsigned int chconfig;
87 struct usbmix_name_map;
89 struct usb_mixer_build {
90 snd_usb_audio_t *chip;
91 struct usb_mixer_interface *mixer;
92 unsigned char *buffer;
94 DECLARE_BITMAP(unitbitmap, 256);
95 usb_audio_term_t oterm;
96 const struct usbmix_name_map *map;
97 const struct usbmix_selector_map *selector_map;
100 struct usb_mixer_elem_info {
101 struct usb_mixer_interface *mixer;
102 usb_mixer_elem_info_t *next_id_elem; /* list of controls with same id */
103 snd_ctl_elem_id_t *elem_id;
105 unsigned int control; /* CS or ICN (high byte) */
106 unsigned int cmask; /* channel mask bitmap: 0 = master */
115 USB_FEATURE_NONE = 0,
116 USB_FEATURE_MUTE = 1,
124 USB_FEATURE_BASSBOOST,
130 USB_MIXER_INV_BOOLEAN,
139 USB_PROC_UPDOWN_SWITCH = 1,
140 USB_PROC_UPDOWN_MODE_SEL = 2,
142 USB_PROC_PROLOGIC = 2,
143 USB_PROC_PROLOGIC_SWITCH = 1,
144 USB_PROC_PROLOGIC_MODE_SEL = 2,
147 USB_PROC_3DENH_SWITCH = 1,
148 USB_PROC_3DENH_SPACE = 2,
151 USB_PROC_REVERB_SWITCH = 1,
152 USB_PROC_REVERB_LEVEL = 2,
153 USB_PROC_REVERB_TIME = 3,
154 USB_PROC_REVERB_DELAY = 4,
157 USB_PROC_CHORUS_SWITCH = 1,
158 USB_PROC_CHORUS_LEVEL = 2,
159 USB_PROC_CHORUS_RATE = 3,
160 USB_PROC_CHORUS_DEPTH = 4,
163 USB_PROC_DCR_SWITCH = 1,
164 USB_PROC_DCR_RATIO = 2,
165 USB_PROC_DCR_MAX_AMP = 3,
166 USB_PROC_DCR_THRESHOLD = 4,
167 USB_PROC_DCR_ATTACK = 5,
168 USB_PROC_DCR_RELEASE = 6,
171 #define MAX_CHANNELS 10 /* max logical channels */
175 * manual mapping of mixer names
176 * if the mixer topology is too complicated and the parsed names are
177 * ambiguous, add the entries in usbmixer_maps.c.
179 #include "usbmixer_maps.c"
181 /* get the mapped name if the unit matches */
182 static int check_mapped_name(mixer_build_t *state, int unitid, int control, char *buf, int buflen)
184 const struct usbmix_name_map *p;
189 for (p = state->map; p->id; p++) {
190 if (p->id == unitid && p->name &&
191 (! control || ! p->control || control == p->control)) {
193 return strlcpy(buf, p->name, buflen);
199 /* check whether the control should be ignored */
200 static int check_ignored_ctl(mixer_build_t *state, int unitid, int control)
202 const struct usbmix_name_map *p;
206 for (p = state->map; p->id; p++) {
207 if (p->id == unitid && ! p->name &&
208 (! control || ! p->control || control == p->control)) {
209 // printk("ignored control %d:%d\n", unitid, control);
216 /* get the mapped selector source name */
217 static int check_mapped_selector_name(mixer_build_t *state, int unitid,
218 int index, char *buf, int buflen)
220 const struct usbmix_selector_map *p;
222 if (! state->selector_map)
224 for (p = state->selector_map; p->id; p++) {
225 if (p->id == unitid && index < p->count)
226 return strlcpy(buf, p->names[index], buflen);
232 * find an audio control unit with the given unit id
234 static void *find_audio_control_unit(mixer_build_t *state, unsigned char unit)
239 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
240 USB_DT_CS_INTERFACE)) != NULL) {
241 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
249 * copy a string with the given id
251 static int snd_usb_copy_string_desc(mixer_build_t *state, int index, char *buf, int maxlen)
253 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
259 * convert from the byte/word on usb descriptor to the zero-based integer
261 static int convert_signed_value(usb_mixer_elem_info_t *cval, int val)
263 switch (cval->val_type) {
264 case USB_MIXER_BOOLEAN:
266 case USB_MIXER_INV_BOOLEAN:
289 * convert from the zero-based int to the byte/word for usb descriptor
291 static int convert_bytes_value(usb_mixer_elem_info_t *cval, int val)
293 switch (cval->val_type) {
294 case USB_MIXER_BOOLEAN:
296 case USB_MIXER_INV_BOOLEAN:
305 return 0; /* not reached */
308 static int get_relative_value(usb_mixer_elem_info_t *cval, int val)
314 else if (val > cval->max)
315 return (cval->max - cval->min) / cval->res;
317 return (val - cval->min) / cval->res;
320 static int get_abs_value(usb_mixer_elem_info_t *cval, int val)
335 * retrieve a mixer value
338 static int get_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int *value_ret)
340 unsigned char buf[2];
341 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
344 while (timeout-- > 0) {
345 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
346 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
348 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
349 validx, cval->mixer->ctrlif | (cval->id << 8),
350 buf, val_len, 100) >= 0) {
351 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
355 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
356 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
360 static int get_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int *value)
362 return get_ctl_value(cval, GET_CUR, validx, value);
365 /* channel = 0: master, 1 = first channel */
366 inline static int get_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int *value)
368 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
375 static int set_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int value_set)
377 unsigned char buf[2];
378 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
381 value_set = convert_bytes_value(cval, value_set);
382 buf[0] = value_set & 0xff;
383 buf[1] = (value_set >> 8) & 0xff;
384 while (timeout -- > 0)
385 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
386 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
388 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
389 validx, cval->mixer->ctrlif | (cval->id << 8),
390 buf, val_len, 100) >= 0)
392 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
393 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
397 static int set_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int value)
399 return set_ctl_value(cval, SET_CUR, validx, value);
402 inline static int set_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int value)
404 return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
409 * parser routines begin here...
412 static int parse_audio_unit(mixer_build_t *state, int unitid);
416 * check if the input/output channel routing is enabled on the given bitmap.
417 * used for mixer unit parser
419 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
421 int idx = ich * num_outs + och;
422 return bmap[idx >> 3] & (0x80 >> (idx & 7));
427 * add an alsa control element
428 * search and increment the index until an empty slot is found.
430 * if failed, give up and free the control instance.
433 static int add_control_to_empty(mixer_build_t *state, snd_kcontrol_t *kctl)
435 usb_mixer_elem_info_t *cval = kctl->private_data;
438 while (snd_ctl_find_id(state->chip->card, &kctl->id))
440 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
441 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
442 snd_ctl_free_one(kctl);
445 cval->elem_id = &kctl->id;
446 cval->next_id_elem = state->mixer->id_elems[cval->id];
447 state->mixer->id_elems[cval->id] = cval;
453 * get a terminal name string
456 static struct iterm_name_combo {
460 { 0x0300, "Output" },
461 { 0x0301, "Speaker" },
462 { 0x0302, "Headphone" },
463 { 0x0303, "HMD Audio" },
464 { 0x0304, "Desktop Speaker" },
465 { 0x0305, "Room Speaker" },
466 { 0x0306, "Com Speaker" },
468 { 0x0600, "External In" },
469 { 0x0601, "Analog In" },
470 { 0x0602, "Digital In" },
472 { 0x0604, "Legacy In" },
473 { 0x0605, "IEC958 In" },
474 { 0x0606, "1394 DA Stream" },
475 { 0x0607, "1394 DV Stream" },
476 { 0x0700, "Embedded" },
477 { 0x0701, "Noise Source" },
478 { 0x0702, "Equalization Noise" },
482 { 0x0706, "MiniDisk" },
483 { 0x0707, "Analog Tape" },
484 { 0x0708, "Phonograph" },
485 { 0x0709, "VCR Audio" },
486 { 0x070a, "Video Disk Audio" },
487 { 0x070b, "DVD Audio" },
488 { 0x070c, "TV Tuner Audio" },
489 { 0x070d, "Satellite Rec Audio" },
490 { 0x070e, "Cable Tuner Audio" },
491 { 0x070f, "DSS Audio" },
492 { 0x0710, "Radio Receiver" },
493 { 0x0711, "Radio Transmitter" },
494 { 0x0712, "Multi-Track Recorder" },
495 { 0x0713, "Synthesizer" },
499 static int get_term_name(mixer_build_t *state, usb_audio_term_t *iterm,
500 unsigned char *name, int maxlen, int term_only)
502 struct iterm_name_combo *names;
505 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
507 /* virtual type - not a real terminal */
508 if (iterm->type >> 16) {
511 switch (iterm->type >> 16) {
513 strcpy(name, "Selector"); return 8;
514 case PROCESSING_UNIT:
515 strcpy(name, "Process Unit"); return 12;
517 strcpy(name, "Ext Unit"); return 8;
519 strcpy(name, "Mixer"); return 5;
521 return sprintf(name, "Unit %d", iterm->id);
525 switch (iterm->type & 0xff00) {
527 strcpy(name, "PCM"); return 3;
529 strcpy(name, "Mic"); return 3;
531 strcpy(name, "Headset"); return 7;
533 strcpy(name, "Phone"); return 5;
536 for (names = iterm_names; names->type; names++)
537 if (names->type == iterm->type) {
538 strcpy(name, names->name);
539 return strlen(names->name);
546 * parse the source unit recursively until it reaches to a terminal
547 * or a branched unit.
549 static int check_input_term(mixer_build_t *state, int id, usb_audio_term_t *term)
553 memset(term, 0, sizeof(*term));
554 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
558 term->type = combine_word(p1 + 4);
559 term->channels = p1[7];
560 term->chconfig = combine_word(p1 + 8);
565 break; /* continue to parse */
567 term->type = p1[2] << 16; /* virtual type */
568 term->channels = p1[5 + p1[4]];
569 term->chconfig = combine_word(p1 + 6 + p1[4]);
570 term->name = p1[p1[0] - 1];
573 /* call recursively to retrieve the channel info */
574 if (check_input_term(state, p1[5], term) < 0)
576 term->type = p1[2] << 16; /* virtual type */
578 term->name = p1[9 + p1[0] - 1];
580 case PROCESSING_UNIT:
584 break; /* continue to parse */
586 term->type = p1[2] << 16; /* virtual type */
587 term->channels = p1[7 + p1[6]];
588 term->chconfig = combine_word(p1 + 8 + p1[6]);
589 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
603 /* feature unit control information */
604 struct usb_feature_control_info {
606 unsigned int type; /* control type (mute, volume, etc.) */
609 static struct usb_feature_control_info audio_feature_info[] = {
610 { "Mute", USB_MIXER_INV_BOOLEAN },
611 { "Volume", USB_MIXER_S16 },
612 { "Tone Control - Bass", USB_MIXER_S8 },
613 { "Tone Control - Mid", USB_MIXER_S8 },
614 { "Tone Control - Treble", USB_MIXER_S8 },
615 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
616 { "Auto Gain Control", USB_MIXER_BOOLEAN },
617 { "Delay Control", USB_MIXER_U16 },
618 { "Bass Boost", USB_MIXER_BOOLEAN },
619 { "Loudness", USB_MIXER_BOOLEAN },
623 /* private_free callback */
624 static void usb_mixer_elem_free(snd_kcontrol_t *kctl)
626 if (kctl->private_data) {
627 kfree(kctl->private_data);
628 kctl->private_data = NULL;
634 * interface to ALSA control for feature/mixer units
638 * retrieve the minimum and maximum values for the specified control
640 static int get_min_max(usb_mixer_elem_info_t *cval, int default_min)
643 cval->min = default_min;
644 cval->max = cval->min + 1;
647 if (cval->val_type == USB_MIXER_BOOLEAN ||
648 cval->val_type == USB_MIXER_INV_BOOLEAN) {
649 cval->initialized = 1;
654 for (i = 0; i < MAX_CHANNELS; i++)
655 if (cval->cmask & (1 << i)) {
660 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
661 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
662 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
663 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
666 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
669 int last_valid_res = cval->res;
671 while (cval->res > 1) {
672 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
676 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
677 cval->res = last_valid_res;
681 cval->initialized = 1;
687 /* get a feature/mixer unit info */
688 static int mixer_ctl_feature_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
690 usb_mixer_elem_info_t *cval = kcontrol->private_data;
692 if (cval->val_type == USB_MIXER_BOOLEAN ||
693 cval->val_type == USB_MIXER_INV_BOOLEAN)
694 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
696 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
697 uinfo->count = cval->channels;
698 if (cval->val_type == USB_MIXER_BOOLEAN ||
699 cval->val_type == USB_MIXER_INV_BOOLEAN) {
700 uinfo->value.integer.min = 0;
701 uinfo->value.integer.max = 1;
703 if (! cval->initialized)
704 get_min_max(cval, 0);
705 uinfo->value.integer.min = 0;
706 uinfo->value.integer.max = (cval->max - cval->min) / cval->res;
711 /* get the current value from feature/mixer unit */
712 static int mixer_ctl_feature_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
714 usb_mixer_elem_info_t *cval = kcontrol->private_data;
715 int c, cnt, val, err;
719 for (c = 0; c < MAX_CHANNELS; c++) {
720 if (cval->cmask & (1 << c)) {
721 err = get_cur_mix_value(cval, c + 1, &val);
723 if (cval->mixer->ignore_ctl_error) {
724 ucontrol->value.integer.value[0] = cval->min;
727 snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
730 val = get_relative_value(cval, val);
731 ucontrol->value.integer.value[cnt] = val;
737 err = get_cur_mix_value(cval, 0, &val);
739 if (cval->mixer->ignore_ctl_error) {
740 ucontrol->value.integer.value[0] = cval->min;
743 snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
746 val = get_relative_value(cval, val);
747 ucontrol->value.integer.value[0] = val;
752 /* put the current value to feature/mixer unit */
753 static int mixer_ctl_feature_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
755 usb_mixer_elem_info_t *cval = kcontrol->private_data;
756 int c, cnt, val, oval, err;
761 for (c = 0; c < MAX_CHANNELS; c++) {
762 if (cval->cmask & (1 << c)) {
763 err = get_cur_mix_value(cval, c + 1, &oval);
765 if (cval->mixer->ignore_ctl_error)
769 val = ucontrol->value.integer.value[cnt];
770 val = get_abs_value(cval, val);
772 set_cur_mix_value(cval, c + 1, val);
775 get_cur_mix_value(cval, c + 1, &val);
781 err = get_cur_mix_value(cval, 0, &oval);
782 if (err < 0 && cval->mixer->ignore_ctl_error)
786 val = ucontrol->value.integer.value[0];
787 val = get_abs_value(cval, val);
789 set_cur_mix_value(cval, 0, val);
796 static snd_kcontrol_new_t usb_feature_unit_ctl = {
797 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
798 .name = "", /* will be filled later manually */
799 .info = mixer_ctl_feature_info,
800 .get = mixer_ctl_feature_get,
801 .put = mixer_ctl_feature_put,
806 * build a feature control
809 static void build_feature_ctl(mixer_build_t *state, unsigned char *desc,
810 unsigned int ctl_mask, int control,
811 usb_audio_term_t *iterm, int unitid)
813 unsigned int len = 0;
815 int nameid = desc[desc[0] - 1];
816 snd_kcontrol_t *kctl;
817 usb_mixer_elem_info_t *cval;
819 control++; /* change from zero-based to 1-based value */
821 if (control == USB_FEATURE_GEQ) {
822 /* FIXME: not supported yet */
826 if (check_ignored_ctl(state, unitid, control))
829 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
831 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
834 cval->mixer = state->mixer;
836 cval->control = control;
837 cval->cmask = ctl_mask;
838 cval->val_type = audio_feature_info[control-1].type;
840 cval->channels = 1; /* master channel */
843 for (i = 0; i < 16; i++)
844 if (ctl_mask & (1 << i))
849 /* get min/max values */
850 get_min_max(cval, 0);
852 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
854 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
858 kctl->private_free = usb_mixer_elem_free;
860 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
861 mapped_name = len != 0;
863 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
866 case USB_FEATURE_MUTE:
867 case USB_FEATURE_VOLUME:
868 /* determine the control name. the rule is:
869 * - if a name id is given in descriptor, use it.
870 * - if the connected input can be determined, then use the name
872 * - if the connected output can be determined, use it.
873 * - otherwise, anonymous name.
876 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
878 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
880 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
881 "Feature %d", unitid);
883 /* determine the stream direction:
884 * if the connected output is USB stream, then it's likely a
885 * capture stream. otherwise it should be playback (hopefully :)
887 if (! mapped_name && ! (state->oterm.type >> 16)) {
888 if ((state->oterm.type & 0xff00) == 0x0100) {
889 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
891 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
894 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
895 sizeof(kctl->id.name));
900 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
901 sizeof(kctl->id.name));
905 /* quirk for UDA1321/N101 */
906 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
907 /* is not very clear from datasheets */
908 /* I hope that the min value is -15360 for newer firmware --jk */
909 switch (state->chip->usb_id) {
910 case USB_ID(0x0471, 0x0101):
911 case USB_ID(0x0471, 0x0104):
912 case USB_ID(0x0471, 0x0105):
913 case USB_ID(0x0672, 0x1041):
914 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
915 cval->min == -15616) {
916 snd_printk("using volume control quirk for the UDA1321/N101 chip\n");
921 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
922 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
923 add_control_to_empty(state, kctl);
929 * parse a feature unit
931 * most of controlls are defined here.
933 static int parse_audio_feature_unit(mixer_build_t *state, int unitid, unsigned char *ftr)
936 usb_audio_term_t iterm;
937 unsigned int master_bits, first_ch_bits;
940 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
941 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
945 /* parse the source unit */
946 if ((err = parse_audio_unit(state, ftr[4])) < 0)
949 /* determine the input source type and name */
950 if (check_input_term(state, ftr[4], &iterm) < 0)
953 channels = (ftr[0] - 7) / csize - 1;
955 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
957 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
960 /* check all control types */
961 for (i = 0; i < 10; i++) {
962 unsigned int ch_bits = 0;
963 for (j = 0; j < channels; j++) {
964 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
968 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
969 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
970 if (master_bits & (1 << i))
971 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
983 * build a mixer unit control
985 * the callbacks are identical with feature unit.
986 * input channel number (zero based) is given in control field instead.
989 static void build_mixer_unit_ctl(mixer_build_t *state, unsigned char *desc,
990 int in_pin, int in_ch, int unitid,
991 usb_audio_term_t *iterm)
993 usb_mixer_elem_info_t *cval;
994 unsigned int input_pins = desc[4];
995 unsigned int num_outs = desc[5 + input_pins];
997 snd_kcontrol_t *kctl;
999 if (check_ignored_ctl(state, unitid, 0))
1002 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
1006 cval->mixer = state->mixer;
1008 cval->control = in_ch + 1; /* based on 1 */
1009 cval->val_type = USB_MIXER_S16;
1010 for (i = 0; i < num_outs; i++) {
1011 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1012 cval->cmask |= (1 << i);
1017 /* get min/max values */
1018 get_min_max(cval, 0);
1020 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1022 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1026 kctl->private_free = usb_mixer_elem_free;
1028 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1030 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1032 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1033 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1035 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1036 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1037 add_control_to_empty(state, kctl);
1042 * parse a mixer unit
1044 static int parse_audio_mixer_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1046 usb_audio_term_t iterm;
1047 int input_pins, num_ins, num_outs;
1050 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1051 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1054 /* no bmControls field (e.g. Maya44) -> ignore */
1055 if (desc[0] <= 10 + input_pins) {
1056 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1062 for (pin = 0; pin < input_pins; pin++) {
1063 err = parse_audio_unit(state, desc[5 + pin]);
1066 err = check_input_term(state, desc[5 + pin], &iterm);
1069 num_ins += iterm.channels;
1070 for (; ich < num_ins; ++ich) {
1071 int och, ich_has_controls = 0;
1073 for (och = 0; och < num_outs; ++och) {
1074 if (check_matrix_bitmap(desc + 9 + input_pins,
1075 ich, och, num_outs)) {
1076 ich_has_controls = 1;
1080 if (ich_has_controls)
1081 build_mixer_unit_ctl(state, desc, pin, ich,
1090 * Processing Unit / Extension Unit
1093 /* get callback for processing/extension unit */
1094 static int mixer_ctl_procunit_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1096 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1099 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1100 if (err < 0 && cval->mixer->ignore_ctl_error) {
1101 ucontrol->value.integer.value[0] = cval->min;
1106 val = get_relative_value(cval, val);
1107 ucontrol->value.integer.value[0] = val;
1111 /* put callback for processing/extension unit */
1112 static int mixer_ctl_procunit_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1114 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1117 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1119 if (cval->mixer->ignore_ctl_error)
1123 val = ucontrol->value.integer.value[0];
1124 val = get_abs_value(cval, val);
1126 set_cur_ctl_value(cval, cval->control << 8, val);
1132 /* alsa control interface for processing/extension unit */
1133 static snd_kcontrol_new_t mixer_procunit_ctl = {
1134 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1135 .name = "", /* will be filled later */
1136 .info = mixer_ctl_feature_info,
1137 .get = mixer_ctl_procunit_get,
1138 .put = mixer_ctl_procunit_put,
1143 * predefined data for processing units
1145 struct procunit_value_info {
1152 struct procunit_info {
1155 struct procunit_value_info *values;
1158 static struct procunit_value_info updown_proc_info[] = {
1159 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1160 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1163 static struct procunit_value_info prologic_proc_info[] = {
1164 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1165 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1168 static struct procunit_value_info threed_enh_proc_info[] = {
1169 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1170 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1173 static struct procunit_value_info reverb_proc_info[] = {
1174 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1175 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1176 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1177 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1180 static struct procunit_value_info chorus_proc_info[] = {
1181 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1182 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1183 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1184 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1187 static struct procunit_value_info dcr_proc_info[] = {
1188 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1189 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1190 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1191 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1192 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1193 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1197 static struct procunit_info procunits[] = {
1198 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1199 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1200 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1201 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1202 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1203 { USB_PROC_DCR, "DCR", dcr_proc_info },
1208 * build a processing/extension unit
1210 static int build_audio_procunit(mixer_build_t *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1212 int num_ins = dsc[6];
1213 usb_mixer_elem_info_t *cval;
1214 snd_kcontrol_t *kctl;
1215 int i, err, nameid, type, len;
1216 struct procunit_info *info;
1217 struct procunit_value_info *valinfo;
1218 static struct procunit_value_info default_value_info[] = {
1219 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1222 static struct procunit_info default_info = {
1223 0, NULL, default_value_info
1226 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1227 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1231 for (i = 0; i < num_ins; i++) {
1232 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1236 type = combine_word(&dsc[4]);
1237 for (info = list; info && info->type; info++)
1238 if (info->type == type)
1240 if (! info || ! info->type)
1241 info = &default_info;
1243 for (valinfo = info->values; valinfo->control; valinfo++) {
1244 /* FIXME: bitmap might be longer than 8bit */
1245 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1247 if (check_ignored_ctl(state, unitid, valinfo->control))
1249 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
1251 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1254 cval->mixer = state->mixer;
1256 cval->control = valinfo->control;
1257 cval->val_type = valinfo->val_type;
1260 /* get min/max values */
1261 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1262 /* FIXME: hard-coded */
1264 cval->max = dsc[15];
1266 cval->initialized = 1;
1268 get_min_max(cval, valinfo->min_value);
1270 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1272 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1276 kctl->private_free = usb_mixer_elem_free;
1278 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1280 else if (info->name)
1281 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1283 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1286 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1288 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1290 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1291 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1293 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1294 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1295 if ((err = add_control_to_empty(state, kctl)) < 0)
1302 static int parse_audio_processing_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1304 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1307 static int parse_audio_extension_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1309 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1317 /* info callback for selector unit
1318 * use an enumerator type for routing
1320 static int mixer_ctl_selector_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1322 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1323 char **itemlist = (char **)kcontrol->private_value;
1325 snd_assert(itemlist, return -EINVAL);
1326 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1328 uinfo->value.enumerated.items = cval->max;
1329 if ((int)uinfo->value.enumerated.item >= cval->max)
1330 uinfo->value.enumerated.item = cval->max - 1;
1331 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1335 /* get callback for selector unit */
1336 static int mixer_ctl_selector_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1338 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1341 err = get_cur_ctl_value(cval, 0, &val);
1343 if (cval->mixer->ignore_ctl_error) {
1344 ucontrol->value.enumerated.item[0] = 0;
1349 val = get_relative_value(cval, val);
1350 ucontrol->value.enumerated.item[0] = val;
1354 /* put callback for selector unit */
1355 static int mixer_ctl_selector_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1357 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1360 err = get_cur_ctl_value(cval, 0, &oval);
1362 if (cval->mixer->ignore_ctl_error)
1366 val = ucontrol->value.enumerated.item[0];
1367 val = get_abs_value(cval, val);
1369 set_cur_ctl_value(cval, 0, val);
1375 /* alsa control interface for selector unit */
1376 static snd_kcontrol_new_t mixer_selectunit_ctl = {
1377 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1378 .name = "", /* will be filled later */
1379 .info = mixer_ctl_selector_info,
1380 .get = mixer_ctl_selector_get,
1381 .put = mixer_ctl_selector_put,
1385 /* private free callback.
1386 * free both private_data and private_value
1388 static void usb_mixer_selector_elem_free(snd_kcontrol_t *kctl)
1392 if (kctl->private_data) {
1393 usb_mixer_elem_info_t *cval = kctl->private_data;
1394 num_ins = cval->max;
1396 kctl->private_data = NULL;
1398 if (kctl->private_value) {
1399 char **itemlist = (char **)kctl->private_value;
1400 for (i = 0; i < num_ins; i++)
1403 kctl->private_value = 0;
1408 * parse a selector unit
1410 static int parse_audio_selector_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1412 unsigned int num_ins = desc[4];
1413 unsigned int i, nameid, len;
1415 usb_mixer_elem_info_t *cval;
1416 snd_kcontrol_t *kctl;
1419 if (! num_ins || desc[0] < 6 + num_ins) {
1420 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1424 for (i = 0; i < num_ins; i++) {
1425 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1429 if (num_ins == 1) /* only one ? nonsense! */
1432 if (check_ignored_ctl(state, unitid, 0))
1435 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
1437 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1440 cval->mixer = state->mixer;
1442 cval->val_type = USB_MIXER_U8;
1445 cval->max = num_ins;
1447 cval->initialized = 1;
1449 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1451 snd_printk(KERN_ERR "cannot malloc\n");
1455 #define MAX_ITEM_NAME_LEN 64
1456 for (i = 0; i < num_ins; i++) {
1457 usb_audio_term_t iterm;
1459 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1460 if (! namelist[i]) {
1461 snd_printk(KERN_ERR "cannot malloc\n");
1468 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1470 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1471 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1473 sprintf(namelist[i], "Input %d", i);
1476 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1478 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1482 kctl->private_value = (unsigned long)namelist;
1483 kctl->private_free = usb_mixer_selector_elem_free;
1485 nameid = desc[desc[0] - 1];
1486 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1490 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1492 len = get_term_name(state, &state->oterm,
1493 kctl->id.name, sizeof(kctl->id.name), 0);
1495 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1497 if ((state->oterm.type & 0xff00) == 0x0100)
1498 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1500 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1503 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1504 cval->id, kctl->id.name, num_ins);
1505 if ((err = add_control_to_empty(state, kctl)) < 0)
1513 * parse an audio unit recursively
1516 static int parse_audio_unit(mixer_build_t *state, int unitid)
1520 if (test_and_set_bit(unitid, state->unitbitmap))
1521 return 0; /* the unit already visited */
1523 p1 = find_audio_control_unit(state, unitid);
1525 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1530 case INPUT_TERMINAL:
1533 return parse_audio_mixer_unit(state, unitid, p1);
1535 return parse_audio_selector_unit(state, unitid, p1);
1537 return parse_audio_feature_unit(state, unitid, p1);
1538 case PROCESSING_UNIT:
1539 return parse_audio_processing_unit(state, unitid, p1);
1540 case EXTENSION_UNIT:
1541 return parse_audio_extension_unit(state, unitid, p1);
1543 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1548 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1550 kfree(mixer->id_elems);
1552 kfree(mixer->urb->transfer_buffer);
1553 usb_free_urb(mixer->urb);
1556 usb_free_urb(mixer->rc_urb);
1557 kfree(mixer->rc_setup_packet);
1561 static int snd_usb_mixer_dev_free(snd_device_t *device)
1563 struct usb_mixer_interface *mixer = device->device_data;
1564 snd_usb_mixer_free(mixer);
1569 * create mixer controls
1571 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1573 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1575 unsigned char *desc;
1576 mixer_build_t state;
1578 const struct usbmix_ctl_map *map;
1579 struct usb_host_interface *hostif;
1581 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1582 memset(&state, 0, sizeof(state));
1583 state.chip = mixer->chip;
1584 state.mixer = mixer;
1585 state.buffer = hostif->extra;
1586 state.buflen = hostif->extralen;
1588 /* check the mapping table */
1589 for (map = usbmix_ctl_maps; map->id; map++) {
1590 if (map->id == state.chip->usb_id) {
1591 state.map = map->map;
1592 state.selector_map = map->selector_map;
1593 mixer->ignore_ctl_error = map->ignore_ctl_error;
1599 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1601 continue; /* invalid descriptor? */
1602 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1603 state.oterm.id = desc[3];
1604 state.oterm.type = combine_word(&desc[4]);
1605 state.oterm.name = desc[8];
1606 err = parse_audio_unit(&state, desc[7]);
1613 static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1616 usb_mixer_elem_info_t *info;
1618 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1619 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1623 static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1626 if (mixer->rc_type == RC_NONE)
1628 /* unit ids specific to Extigy/Audigy 2 NX: */
1630 case 0: /* remote control */
1631 mixer->rc_urb->dev = mixer->chip->dev;
1632 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1634 case 4: /* digital in jack */
1635 case 7: /* line in jacks */
1636 case 19: /* speaker out jacks */
1637 case 20: /* headphones out jack */
1640 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1645 static void snd_usb_mixer_status_complete(struct urb *urb, struct pt_regs *regs)
1647 struct usb_mixer_interface *mixer = urb->context;
1649 if (urb->status == 0) {
1650 u8 *buf = urb->transfer_buffer;
1653 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1654 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1656 /* ignore any notifications not from the control interface */
1657 if ((buf[0] & 0x0f) != 0)
1659 if (!(buf[0] & 0x40))
1660 snd_usb_mixer_notify_id(mixer, buf[1]);
1662 snd_usb_mixer_memory_change(mixer, buf[1]);
1665 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1666 urb->dev = mixer->chip->dev;
1667 usb_submit_urb(urb, GFP_ATOMIC);
1671 /* create the handler for the optional status interrupt endpoint */
1672 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1674 struct usb_host_interface *hostif;
1675 struct usb_endpoint_descriptor *ep;
1676 void *transfer_buffer;
1680 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1681 /* we need one interrupt input endpoint */
1682 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1684 ep = get_endpoint(hostif, 0);
1685 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1686 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1689 epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1690 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1691 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1692 if (!transfer_buffer)
1694 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1696 kfree(transfer_buffer);
1699 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1700 usb_rcvintpipe(mixer->chip->dev, epnum),
1701 transfer_buffer, buffer_length,
1702 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1703 usb_submit_urb(mixer->urb, GFP_KERNEL);
1707 static void snd_usb_soundblaster_remote_complete(struct urb *urb,
1708 struct pt_regs *regs)
1710 struct usb_mixer_interface *mixer = urb->context;
1712 * format of remote control data:
1714 * Audigy 2 NX: 06 80 xx 00 00 00
1716 int offset = mixer->rc_type == RC_EXTIGY ? 0 : 2;
1719 if (urb->status < 0 || urb->actual_length <= offset)
1721 code = mixer->rc_buffer[offset];
1722 /* the Mute button actually changes the mixer control */
1724 snd_usb_mixer_notify_id(mixer, 18);
1725 mixer->rc_code = code;
1727 wake_up(&mixer->rc_waitq);
1730 static int snd_usb_sbrc_hwdep_open(snd_hwdep_t *hw, struct file *file)
1732 struct usb_mixer_interface *mixer = hw->private_data;
1734 if (test_and_set_bit(0, &mixer->rc_hwdep_open))
1739 static int snd_usb_sbrc_hwdep_release(snd_hwdep_t *hw, struct file *file)
1741 struct usb_mixer_interface *mixer = hw->private_data;
1743 clear_bit(0, &mixer->rc_hwdep_open);
1744 smp_mb__after_clear_bit();
1748 static long snd_usb_sbrc_hwdep_read(snd_hwdep_t *hw, char __user *buf,
1749 long count, loff_t *offset)
1751 struct usb_mixer_interface *mixer = hw->private_data;
1755 if (count != 1 && count != 4)
1757 err = wait_event_interruptible(mixer->rc_waitq,
1758 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1761 err = put_user(rc_code, buf);
1763 err = put_user(rc_code, (u32 __user *)buf);
1765 return err < 0 ? err : count;
1768 static unsigned int snd_usb_sbrc_hwdep_poll(snd_hwdep_t *hw, struct file *file,
1771 struct usb_mixer_interface *mixer = hw->private_data;
1773 poll_wait(file, &mixer->rc_waitq, wait);
1774 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1777 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1782 switch (mixer->chip->usb_id) {
1783 case USB_ID(0x041e, 0x3000):
1784 mixer->rc_type = RC_EXTIGY;
1787 case USB_ID(0x041e, 0x3020):
1788 mixer->rc_type = RC_AUDIGY2NX;
1795 init_waitqueue_head(&mixer->rc_waitq);
1796 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1799 snprintf(hwdep->name, sizeof(hwdep->name),
1800 "%s remote control", mixer->chip->card->shortname);
1801 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1802 hwdep->private_data = mixer;
1803 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1804 hwdep->ops.open = snd_usb_sbrc_hwdep_open;
1805 hwdep->ops.release = snd_usb_sbrc_hwdep_release;
1806 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1808 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1811 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1812 if (!mixer->rc_setup_packet) {
1813 usb_free_urb(mixer->rc_urb);
1814 mixer->rc_urb = NULL;
1817 mixer->rc_setup_packet->bRequestType =
1818 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1819 mixer->rc_setup_packet->bRequest = GET_MEM;
1820 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1821 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1822 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1823 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1824 usb_rcvctrlpipe(mixer->chip->dev, 0),
1825 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1826 snd_usb_soundblaster_remote_complete, mixer);
1830 static int snd_audigy2nx_led_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1832 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1834 uinfo->value.integer.min = 0;
1835 uinfo->value.integer.max = 1;
1839 static int snd_audigy2nx_led_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1841 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1842 int index = kcontrol->private_value;
1844 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1848 static int snd_audigy2nx_led_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1850 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1851 int index = kcontrol->private_value;
1852 int value = ucontrol->value.integer.value[0];
1857 changed = value != mixer->audigy2nx_leds[index];
1858 err = snd_usb_ctl_msg(mixer->chip->dev,
1859 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1860 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1861 value, index + 2, NULL, 0, 100);
1864 mixer->audigy2nx_leds[index] = value;
1868 static snd_kcontrol_new_t snd_audigy2nx_controls[] = {
1870 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1871 .name = "CMSS LED Switch",
1872 .info = snd_audigy2nx_led_info,
1873 .get = snd_audigy2nx_led_get,
1874 .put = snd_audigy2nx_led_put,
1878 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1879 .name = "Power LED Switch",
1880 .info = snd_audigy2nx_led_info,
1881 .get = snd_audigy2nx_led_get,
1882 .put = snd_audigy2nx_led_put,
1886 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1887 .name = "Dolby Digital LED Switch",
1888 .info = snd_audigy2nx_led_info,
1889 .get = snd_audigy2nx_led_get,
1890 .put = snd_audigy2nx_led_put,
1895 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1899 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
1900 err = snd_ctl_add(mixer->chip->card,
1901 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1905 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1909 static void snd_audigy2nx_proc_read(snd_info_entry_t *entry,
1910 snd_info_buffer_t *buffer)
1912 static const struct {
1921 struct usb_mixer_interface *mixer = entry->private_data;
1925 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
1926 for (i = 0; i < ARRAY_SIZE(jacks); ++i) {
1927 snd_iprintf(buffer, "%s: ", jacks[i].name);
1928 err = snd_usb_ctl_msg(mixer->chip->dev,
1929 usb_rcvctrlpipe(mixer->chip->dev, 0),
1930 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
1931 USB_RECIP_INTERFACE, 0,
1932 jacks[i].unitid << 8, buf, 3, 100);
1933 if (err == 3 && buf[0] == 3)
1934 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
1936 snd_iprintf(buffer, "?\n");
1940 int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif)
1942 static snd_device_ops_t dev_ops = {
1943 .dev_free = snd_usb_mixer_dev_free
1945 struct usb_mixer_interface *mixer;
1948 strcpy(chip->card->mixername, "USB Mixer");
1950 mixer = kcalloc(1, sizeof(*mixer), GFP_KERNEL);
1954 mixer->ctrlif = ctrlif;
1955 #ifdef IGNORE_CTL_ERROR
1956 mixer->ignore_ctl_error = 1;
1958 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
1959 if (!mixer->id_elems) {
1964 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
1965 (err = snd_usb_mixer_status_create(mixer)) < 0)
1968 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
1971 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020)) {
1972 snd_info_entry_t *entry;
1974 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
1976 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
1977 snd_info_set_text_ops(entry, mixer, 1024,
1978 snd_audigy2nx_proc_read);
1981 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
1984 list_add(&mixer->list, &chip->mixer_list);
1988 snd_usb_mixer_free(mixer);
1992 void snd_usb_mixer_disconnect(struct list_head *p)
1994 struct usb_mixer_interface *mixer;
1996 mixer = list_entry(p, struct usb_mixer_interface, list);
1998 usb_kill_urb(mixer->urb);
2000 usb_kill_urb(mixer->rc_urb);