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 <linux/bitops.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/usb.h>
35 #include <sound/core.h>
36 #include <sound/control.h>
37 #include <sound/hwdep.h>
38 #include <sound/info.h>
39 #include <sound/tlv.h>
46 /* ignore error from controls - for debugging */
47 /* #define IGNORE_CTL_ERROR */
50 * Sound Blaster remote control configuration
52 * format of remote control data:
54 * Audigy 2 NX: 06 80 xx 00 00 00
55 * Live! 24-bit: 06 80 xx yy 22 83
57 static const struct rc_config {
62 u8 min_packet_length; /* minimum accepted length of the URB result */
66 { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */
67 { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */
68 { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */
69 { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
72 #define MAX_ID_ELEMS 256
74 struct usb_mixer_interface {
75 struct snd_usb_audio *chip;
77 struct list_head list;
78 unsigned int ignore_ctl_error;
80 /* array[MAX_ID_ELEMS], indexed by unit id */
81 struct usb_mixer_elem_info **id_elems;
83 /* Sound Blaster remote control stuff */
84 const struct rc_config *rc_cfg;
86 wait_queue_head_t rc_waitq;
88 struct usb_ctrlrequest *rc_setup_packet;
96 struct usb_audio_term {
100 unsigned int chconfig;
104 struct usbmix_name_map;
107 struct snd_usb_audio *chip;
108 struct usb_mixer_interface *mixer;
109 unsigned char *buffer;
111 DECLARE_BITMAP(unitbitmap, 256);
112 struct usb_audio_term oterm;
113 const struct usbmix_name_map *map;
114 const struct usbmix_selector_map *selector_map;
117 #define MAX_CHANNELS 10 /* max logical channels */
119 struct usb_mixer_elem_info {
120 struct usb_mixer_interface *mixer;
121 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
122 struct snd_ctl_elem_id *elem_id;
124 unsigned int control; /* CS or ICN (high byte) */
125 unsigned int cmask; /* channel mask bitmap: 0 = master */
131 int cache_val[MAX_CHANNELS];
137 USB_FEATURE_NONE = 0,
138 USB_FEATURE_MUTE = 1,
146 USB_FEATURE_BASSBOOST,
152 USB_MIXER_INV_BOOLEAN,
161 USB_PROC_UPDOWN_SWITCH = 1,
162 USB_PROC_UPDOWN_MODE_SEL = 2,
164 USB_PROC_PROLOGIC = 2,
165 USB_PROC_PROLOGIC_SWITCH = 1,
166 USB_PROC_PROLOGIC_MODE_SEL = 2,
169 USB_PROC_3DENH_SWITCH = 1,
170 USB_PROC_3DENH_SPACE = 2,
173 USB_PROC_REVERB_SWITCH = 1,
174 USB_PROC_REVERB_LEVEL = 2,
175 USB_PROC_REVERB_TIME = 3,
176 USB_PROC_REVERB_DELAY = 4,
179 USB_PROC_CHORUS_SWITCH = 1,
180 USB_PROC_CHORUS_LEVEL = 2,
181 USB_PROC_CHORUS_RATE = 3,
182 USB_PROC_CHORUS_DEPTH = 4,
185 USB_PROC_DCR_SWITCH = 1,
186 USB_PROC_DCR_RATIO = 2,
187 USB_PROC_DCR_MAX_AMP = 3,
188 USB_PROC_DCR_THRESHOLD = 4,
189 USB_PROC_DCR_ATTACK = 5,
190 USB_PROC_DCR_RELEASE = 6,
193 /*E-mu 0202(0404) eXtension Unit(XU) control*/
195 USB_XU_CLOCK_RATE = 0xe301,
196 USB_XU_CLOCK_SOURCE = 0xe302,
197 USB_XU_DIGITAL_IO_STATUS = 0xe303,
198 USB_XU_DEVICE_OPTIONS = 0xe304,
199 USB_XU_DIRECT_MONITORING = 0xe305,
200 USB_XU_METERING = 0xe306
203 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
204 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
205 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
206 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
210 * manual mapping of mixer names
211 * if the mixer topology is too complicated and the parsed names are
212 * ambiguous, add the entries in usbmixer_maps.c.
214 #include "usbmixer_maps.c"
216 static const struct usbmix_name_map *
217 find_map(struct mixer_build *state, int unitid, int control)
219 const struct usbmix_name_map *p = state->map;
224 for (p = state->map; p->id; p++) {
225 if (p->id == unitid &&
226 (!control || !p->control || control == p->control))
232 /* get the mapped name if the unit matches */
234 check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
240 return strlcpy(buf, p->name, buflen);
243 /* check whether the control should be ignored */
245 check_ignored_ctl(const struct usbmix_name_map *p)
247 if (!p || p->name || p->dB)
253 static inline void check_mapped_dB(const struct usbmix_name_map *p,
254 struct usb_mixer_elem_info *cval)
257 cval->dBmin = p->dB->min;
258 cval->dBmax = p->dB->max;
262 /* get the mapped selector source name */
263 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
264 int index, char *buf, int buflen)
266 const struct usbmix_selector_map *p;
268 if (! state->selector_map)
270 for (p = state->selector_map; p->id; p++) {
271 if (p->id == unitid && index < p->count)
272 return strlcpy(buf, p->names[index], buflen);
278 * find an audio control unit with the given unit id
280 static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
285 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
286 USB_DT_CS_INTERFACE)) != NULL) {
287 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
295 * copy a string with the given id
297 static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
299 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
305 * convert from the byte/word on usb descriptor to the zero-based integer
307 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
309 switch (cval->val_type) {
310 case USB_MIXER_BOOLEAN:
312 case USB_MIXER_INV_BOOLEAN:
335 * convert from the zero-based int to the byte/word for usb descriptor
337 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
339 switch (cval->val_type) {
340 case USB_MIXER_BOOLEAN:
342 case USB_MIXER_INV_BOOLEAN:
351 return 0; /* not reached */
354 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
360 else if (val >= cval->max)
361 return (cval->max - cval->min + cval->res - 1) / cval->res;
363 return (val - cval->min) / cval->res;
366 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
381 * retrieve a mixer value
384 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
386 unsigned char buf[2];
387 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
390 while (timeout-- > 0) {
391 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
392 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
394 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
395 validx, cval->mixer->ctrlif | (cval->id << 8),
396 buf, val_len, 100) >= val_len) {
397 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
401 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
402 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
406 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
408 return get_ctl_value(cval, GET_CUR, validx, value);
411 /* channel = 0: master, 1 = first channel */
412 static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
413 int channel, int *value)
415 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
418 static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
419 int channel, int index, int *value)
423 if (cval->cached & (1 << channel)) {
424 *value = cval->cache_val[index];
427 err = get_cur_mix_raw(cval, channel, value);
429 if (!cval->mixer->ignore_ctl_error)
430 snd_printd(KERN_ERR "cannot get current value for "
431 "control %d ch %d: err = %d\n",
432 cval->control, channel, err);
435 cval->cached |= 1 << channel;
436 cval->cache_val[index] = *value;
445 static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
447 unsigned char buf[2];
448 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
451 value_set = convert_bytes_value(cval, value_set);
452 buf[0] = value_set & 0xff;
453 buf[1] = (value_set >> 8) & 0xff;
454 while (timeout-- > 0)
455 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
456 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
458 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
459 validx, cval->mixer->ctrlif | (cval->id << 8),
460 buf, val_len, 100) >= 0)
462 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
463 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
467 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
469 return set_ctl_value(cval, SET_CUR, validx, value);
472 static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
473 int index, int value)
476 err = set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel,
480 cval->cached |= 1 << channel;
481 cval->cache_val[index] = value;
486 * TLV callback for mixer volume controls
488 static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
489 unsigned int size, unsigned int __user *_tlv)
491 struct usb_mixer_elem_info *cval = kcontrol->private_data;
492 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
494 if (size < sizeof(scale))
496 scale[2] = cval->dBmin;
497 scale[3] = cval->dBmax;
498 if (copy_to_user(_tlv, scale, sizeof(scale)))
504 * parser routines begin here...
507 static int parse_audio_unit(struct mixer_build *state, int unitid);
511 * check if the input/output channel routing is enabled on the given bitmap.
512 * used for mixer unit parser
514 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
516 int idx = ich * num_outs + och;
517 return bmap[idx >> 3] & (0x80 >> (idx & 7));
522 * add an alsa control element
523 * search and increment the index until an empty slot is found.
525 * if failed, give up and free the control instance.
528 static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
530 struct usb_mixer_elem_info *cval = kctl->private_data;
533 while (snd_ctl_find_id(state->chip->card, &kctl->id))
535 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
536 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
539 cval->elem_id = &kctl->id;
540 cval->next_id_elem = state->mixer->id_elems[cval->id];
541 state->mixer->id_elems[cval->id] = cval;
547 * get a terminal name string
550 static struct iterm_name_combo {
554 { 0x0300, "Output" },
555 { 0x0301, "Speaker" },
556 { 0x0302, "Headphone" },
557 { 0x0303, "HMD Audio" },
558 { 0x0304, "Desktop Speaker" },
559 { 0x0305, "Room Speaker" },
560 { 0x0306, "Com Speaker" },
562 { 0x0600, "External In" },
563 { 0x0601, "Analog In" },
564 { 0x0602, "Digital In" },
566 { 0x0604, "Legacy In" },
567 { 0x0605, "IEC958 In" },
568 { 0x0606, "1394 DA Stream" },
569 { 0x0607, "1394 DV Stream" },
570 { 0x0700, "Embedded" },
571 { 0x0701, "Noise Source" },
572 { 0x0702, "Equalization Noise" },
576 { 0x0706, "MiniDisk" },
577 { 0x0707, "Analog Tape" },
578 { 0x0708, "Phonograph" },
579 { 0x0709, "VCR Audio" },
580 { 0x070a, "Video Disk Audio" },
581 { 0x070b, "DVD Audio" },
582 { 0x070c, "TV Tuner Audio" },
583 { 0x070d, "Satellite Rec Audio" },
584 { 0x070e, "Cable Tuner Audio" },
585 { 0x070f, "DSS Audio" },
586 { 0x0710, "Radio Receiver" },
587 { 0x0711, "Radio Transmitter" },
588 { 0x0712, "Multi-Track Recorder" },
589 { 0x0713, "Synthesizer" },
593 static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
594 unsigned char *name, int maxlen, int term_only)
596 struct iterm_name_combo *names;
599 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
601 /* virtual type - not a real terminal */
602 if (iterm->type >> 16) {
605 switch (iterm->type >> 16) {
607 strcpy(name, "Selector"); return 8;
608 case PROCESSING_UNIT:
609 strcpy(name, "Process Unit"); return 12;
611 strcpy(name, "Ext Unit"); return 8;
613 strcpy(name, "Mixer"); return 5;
615 return sprintf(name, "Unit %d", iterm->id);
619 switch (iterm->type & 0xff00) {
621 strcpy(name, "PCM"); return 3;
623 strcpy(name, "Mic"); return 3;
625 strcpy(name, "Headset"); return 7;
627 strcpy(name, "Phone"); return 5;
630 for (names = iterm_names; names->type; names++)
631 if (names->type == iterm->type) {
632 strcpy(name, names->name);
633 return strlen(names->name);
640 * parse the source unit recursively until it reaches to a terminal
641 * or a branched unit.
643 static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
647 memset(term, 0, sizeof(*term));
648 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
652 term->type = combine_word(p1 + 4);
653 term->channels = p1[7];
654 term->chconfig = combine_word(p1 + 8);
659 break; /* continue to parse */
661 term->type = p1[2] << 16; /* virtual type */
662 term->channels = p1[5 + p1[4]];
663 term->chconfig = combine_word(p1 + 6 + p1[4]);
664 term->name = p1[p1[0] - 1];
667 /* call recursively to retrieve the channel info */
668 if (check_input_term(state, p1[5], term) < 0)
670 term->type = p1[2] << 16; /* virtual type */
672 term->name = p1[9 + p1[0] - 1];
674 case PROCESSING_UNIT:
678 break; /* continue to parse */
680 term->type = p1[2] << 16; /* virtual type */
681 term->channels = p1[7 + p1[6]];
682 term->chconfig = combine_word(p1 + 8 + p1[6]);
683 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
697 /* feature unit control information */
698 struct usb_feature_control_info {
700 unsigned int type; /* control type (mute, volume, etc.) */
703 static struct usb_feature_control_info audio_feature_info[] = {
704 { "Mute", USB_MIXER_INV_BOOLEAN },
705 { "Volume", USB_MIXER_S16 },
706 { "Tone Control - Bass", USB_MIXER_S8 },
707 { "Tone Control - Mid", USB_MIXER_S8 },
708 { "Tone Control - Treble", USB_MIXER_S8 },
709 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
710 { "Auto Gain Control", USB_MIXER_BOOLEAN },
711 { "Delay Control", USB_MIXER_U16 },
712 { "Bass Boost", USB_MIXER_BOOLEAN },
713 { "Loudness", USB_MIXER_BOOLEAN },
717 /* private_free callback */
718 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
720 kfree(kctl->private_data);
721 kctl->private_data = NULL;
726 * interface to ALSA control for feature/mixer units
730 * retrieve the minimum and maximum values for the specified control
732 static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
735 cval->min = default_min;
736 cval->max = cval->min + 1;
738 cval->dBmin = cval->dBmax = 0;
740 if (cval->val_type == USB_MIXER_BOOLEAN ||
741 cval->val_type == USB_MIXER_INV_BOOLEAN) {
742 cval->initialized = 1;
747 for (i = 0; i < MAX_CHANNELS; i++)
748 if (cval->cmask & (1 << i)) {
753 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
754 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
755 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
756 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
759 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
762 int last_valid_res = cval->res;
764 while (cval->res > 1) {
765 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
769 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
770 cval->res = last_valid_res;
775 /* Additional checks for the proper resolution
777 * Some devices report smaller resolutions than actually
778 * reacting. They don't return errors but simply clip
779 * to the lower aligned value.
781 if (cval->min + cval->res < cval->max) {
782 int last_valid_res = cval->res;
783 int saved, test, check;
784 get_cur_mix_raw(cval, minchn, &saved);
787 if (test < cval->max)
791 if (test < cval->min || test > cval->max ||
792 set_cur_mix_value(cval, minchn, 0, test) ||
793 get_cur_mix_raw(cval, minchn, &check)) {
794 cval->res = last_valid_res;
801 set_cur_mix_value(cval, minchn, 0, saved);
804 cval->initialized = 1;
807 /* USB descriptions contain the dB scale in 1/256 dB unit
808 * while ALSA TLV contains in 1/100 dB unit
810 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
811 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
812 if (cval->dBmin > cval->dBmax) {
813 /* something is wrong; assume it's either from/to 0dB */
816 else if (cval->dBmin > 0)
818 if (cval->dBmin > cval->dBmax) {
819 /* totally crap, return an error */
828 /* get a feature/mixer unit info */
829 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
831 struct usb_mixer_elem_info *cval = kcontrol->private_data;
833 if (cval->val_type == USB_MIXER_BOOLEAN ||
834 cval->val_type == USB_MIXER_INV_BOOLEAN)
835 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
837 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
838 uinfo->count = cval->channels;
839 if (cval->val_type == USB_MIXER_BOOLEAN ||
840 cval->val_type == USB_MIXER_INV_BOOLEAN) {
841 uinfo->value.integer.min = 0;
842 uinfo->value.integer.max = 1;
844 if (! cval->initialized)
845 get_min_max(cval, 0);
846 uinfo->value.integer.min = 0;
847 uinfo->value.integer.max =
848 (cval->max - cval->min + cval->res - 1) / cval->res;
853 /* get the current value from feature/mixer unit */
854 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
856 struct usb_mixer_elem_info *cval = kcontrol->private_data;
857 int c, cnt, val, err;
859 ucontrol->value.integer.value[0] = cval->min;
862 for (c = 0; c < MAX_CHANNELS; c++) {
863 if (!(cval->cmask & (1 << c)))
865 err = get_cur_mix_value(cval, c + 1, cnt, &val);
867 return cval->mixer->ignore_ctl_error ? 0 : err;
868 val = get_relative_value(cval, val);
869 ucontrol->value.integer.value[cnt] = val;
875 err = get_cur_mix_value(cval, 0, 0, &val);
877 return cval->mixer->ignore_ctl_error ? 0 : err;
878 val = get_relative_value(cval, val);
879 ucontrol->value.integer.value[0] = val;
884 /* put the current value to feature/mixer unit */
885 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
887 struct usb_mixer_elem_info *cval = kcontrol->private_data;
888 int c, cnt, val, oval, err;
893 for (c = 0; c < MAX_CHANNELS; c++) {
894 if (!(cval->cmask & (1 << c)))
896 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
898 return cval->mixer->ignore_ctl_error ? 0 : err;
899 val = ucontrol->value.integer.value[cnt];
900 val = get_abs_value(cval, val);
902 set_cur_mix_value(cval, c + 1, cnt, val);
909 err = get_cur_mix_value(cval, 0, 0, &oval);
911 return cval->mixer->ignore_ctl_error ? 0 : err;
912 val = ucontrol->value.integer.value[0];
913 val = get_abs_value(cval, val);
915 set_cur_mix_value(cval, 0, 0, val);
922 static struct snd_kcontrol_new usb_feature_unit_ctl = {
923 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
924 .name = "", /* will be filled later manually */
925 .info = mixer_ctl_feature_info,
926 .get = mixer_ctl_feature_get,
927 .put = mixer_ctl_feature_put,
932 * build a feature control
935 static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
937 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
940 static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
941 unsigned int ctl_mask, int control,
942 struct usb_audio_term *iterm, int unitid)
944 unsigned int len = 0;
946 int nameid = desc[desc[0] - 1];
947 struct snd_kcontrol *kctl;
948 struct usb_mixer_elem_info *cval;
949 const struct usbmix_name_map *map;
951 control++; /* change from zero-based to 1-based value */
953 if (control == USB_FEATURE_GEQ) {
954 /* FIXME: not supported yet */
958 map = find_map(state, unitid, control);
959 if (check_ignored_ctl(map))
962 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
964 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
967 cval->mixer = state->mixer;
969 cval->control = control;
970 cval->cmask = ctl_mask;
971 cval->val_type = audio_feature_info[control-1].type;
973 cval->channels = 1; /* master channel */
976 for (i = 0; i < 16; i++)
977 if (ctl_mask & (1 << i))
982 /* get min/max values */
983 get_min_max(cval, 0);
985 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
987 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
991 kctl->private_free = usb_mixer_elem_free;
993 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
994 mapped_name = len != 0;
996 len = snd_usb_copy_string_desc(state, nameid,
997 kctl->id.name, sizeof(kctl->id.name));
1000 case USB_FEATURE_MUTE:
1001 case USB_FEATURE_VOLUME:
1002 /* determine the control name. the rule is:
1003 * - if a name id is given in descriptor, use it.
1004 * - if the connected input can be determined, then use the name
1006 * - if the connected output can be determined, use it.
1007 * - otherwise, anonymous name.
1010 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
1012 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
1014 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
1015 "Feature %d", unitid);
1017 /* determine the stream direction:
1018 * if the connected output is USB stream, then it's likely a
1019 * capture stream. otherwise it should be playback (hopefully :)
1021 if (! mapped_name && ! (state->oterm.type >> 16)) {
1022 if ((state->oterm.type & 0xff00) == 0x0100) {
1023 len = append_ctl_name(kctl, " Capture");
1025 len = append_ctl_name(kctl, " Playback");
1028 append_ctl_name(kctl, control == USB_FEATURE_MUTE ?
1029 " Switch" : " Volume");
1030 if (control == USB_FEATURE_VOLUME) {
1031 kctl->tlv.c = mixer_vol_tlv;
1032 kctl->vd[0].access |=
1033 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1034 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1035 check_mapped_dB(map, cval);
1041 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1042 sizeof(kctl->id.name));
1046 /* volume control quirks */
1047 switch (state->chip->usb_id) {
1048 case USB_ID(0x0471, 0x0101):
1049 case USB_ID(0x0471, 0x0104):
1050 case USB_ID(0x0471, 0x0105):
1051 case USB_ID(0x0672, 0x1041):
1052 /* quirk for UDA1321/N101.
1053 * note that detection between firmware 2.1.1.7 (N101)
1054 * and later 2.1.1.21 is not very clear from datasheets.
1055 * I hope that the min value is -15360 for newer firmware --jk
1057 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1058 cval->min == -15616) {
1059 snd_printk(KERN_INFO
1060 "set volume quirk for UDA1321/N101 chip\n");
1065 case USB_ID(0x046d, 0x09a4):
1066 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1067 snd_printk(KERN_INFO
1068 "set volume quirk for QuickCam E3500\n");
1077 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1078 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
1079 add_control_to_empty(state, kctl);
1085 * parse a feature unit
1087 * most of controlls are defined here.
1089 static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
1092 struct usb_audio_term iterm;
1093 unsigned int master_bits, first_ch_bits;
1096 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1097 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1101 /* parse the source unit */
1102 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1105 /* determine the input source type and name */
1106 if (check_input_term(state, ftr[4], &iterm) < 0)
1109 channels = (ftr[0] - 7) / csize - 1;
1111 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
1112 /* master configuration quirks */
1113 switch (state->chip->usb_id) {
1114 case USB_ID(0x08bb, 0x2702):
1115 snd_printk(KERN_INFO
1116 "usbmixer: master volume quirk for PCM2702 chip\n");
1117 /* disable non-functional volume control */
1118 master_bits &= ~(1 << (USB_FEATURE_VOLUME - 1));
1122 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1125 /* check all control types */
1126 for (i = 0; i < 10; i++) {
1127 unsigned int ch_bits = 0;
1128 for (j = 0; j < channels; j++) {
1129 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1130 if (mask & (1 << i))
1131 ch_bits |= (1 << j);
1133 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1134 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1135 if (master_bits & (1 << i))
1136 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1148 * build a mixer unit control
1150 * the callbacks are identical with feature unit.
1151 * input channel number (zero based) is given in control field instead.
1154 static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
1155 int in_pin, int in_ch, int unitid,
1156 struct usb_audio_term *iterm)
1158 struct usb_mixer_elem_info *cval;
1159 unsigned int input_pins = desc[4];
1160 unsigned int num_outs = desc[5 + input_pins];
1161 unsigned int i, len;
1162 struct snd_kcontrol *kctl;
1163 const struct usbmix_name_map *map;
1165 map = find_map(state, unitid, 0);
1166 if (check_ignored_ctl(map))
1169 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1173 cval->mixer = state->mixer;
1175 cval->control = in_ch + 1; /* based on 1 */
1176 cval->val_type = USB_MIXER_S16;
1177 for (i = 0; i < num_outs; i++) {
1178 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1179 cval->cmask |= (1 << i);
1184 /* get min/max values */
1185 get_min_max(cval, 0);
1187 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1189 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1193 kctl->private_free = usb_mixer_elem_free;
1195 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1197 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1199 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1200 append_ctl_name(kctl, " Volume");
1202 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1203 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1204 add_control_to_empty(state, kctl);
1209 * parse a mixer unit
1211 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1213 struct usb_audio_term iterm;
1214 int input_pins, num_ins, num_outs;
1217 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1218 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1221 /* no bmControls field (e.g. Maya44) -> ignore */
1222 if (desc[0] <= 10 + input_pins) {
1223 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1229 for (pin = 0; pin < input_pins; pin++) {
1230 err = parse_audio_unit(state, desc[5 + pin]);
1233 err = check_input_term(state, desc[5 + pin], &iterm);
1236 num_ins += iterm.channels;
1237 for (; ich < num_ins; ++ich) {
1238 int och, ich_has_controls = 0;
1240 for (och = 0; och < num_outs; ++och) {
1241 if (check_matrix_bitmap(desc + 9 + input_pins,
1242 ich, och, num_outs)) {
1243 ich_has_controls = 1;
1247 if (ich_has_controls)
1248 build_mixer_unit_ctl(state, desc, pin, ich,
1257 * Processing Unit / Extension Unit
1260 /* get callback for processing/extension unit */
1261 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1263 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1266 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1267 if (err < 0 && cval->mixer->ignore_ctl_error) {
1268 ucontrol->value.integer.value[0] = cval->min;
1273 val = get_relative_value(cval, val);
1274 ucontrol->value.integer.value[0] = val;
1278 /* put callback for processing/extension unit */
1279 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1281 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1284 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1286 if (cval->mixer->ignore_ctl_error)
1290 val = ucontrol->value.integer.value[0];
1291 val = get_abs_value(cval, val);
1293 set_cur_ctl_value(cval, cval->control << 8, val);
1299 /* alsa control interface for processing/extension unit */
1300 static struct snd_kcontrol_new mixer_procunit_ctl = {
1301 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1302 .name = "", /* will be filled later */
1303 .info = mixer_ctl_feature_info,
1304 .get = mixer_ctl_procunit_get,
1305 .put = mixer_ctl_procunit_put,
1310 * predefined data for processing units
1312 struct procunit_value_info {
1319 struct procunit_info {
1322 struct procunit_value_info *values;
1325 static struct procunit_value_info updown_proc_info[] = {
1326 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1327 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1330 static struct procunit_value_info prologic_proc_info[] = {
1331 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1332 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1335 static struct procunit_value_info threed_enh_proc_info[] = {
1336 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1337 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1340 static struct procunit_value_info reverb_proc_info[] = {
1341 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1342 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1343 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1344 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1347 static struct procunit_value_info chorus_proc_info[] = {
1348 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1349 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1350 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1351 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1354 static struct procunit_value_info dcr_proc_info[] = {
1355 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1356 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1357 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1358 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1359 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1360 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1364 static struct procunit_info procunits[] = {
1365 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1366 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1367 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1368 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1369 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1370 { USB_PROC_DCR, "DCR", dcr_proc_info },
1374 * predefined data for extension units
1376 static struct procunit_value_info clock_rate_xu_info[] = {
1377 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
1380 static struct procunit_value_info clock_source_xu_info[] = {
1381 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
1384 static struct procunit_value_info spdif_format_xu_info[] = {
1385 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
1388 static struct procunit_value_info soft_limit_xu_info[] = {
1389 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
1392 static struct procunit_info extunits[] = {
1393 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
1394 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
1395 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
1396 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
1400 * build a processing/extension unit
1402 static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1404 int num_ins = dsc[6];
1405 struct usb_mixer_elem_info *cval;
1406 struct snd_kcontrol *kctl;
1407 int i, err, nameid, type, len;
1408 struct procunit_info *info;
1409 struct procunit_value_info *valinfo;
1410 const struct usbmix_name_map *map;
1411 static struct procunit_value_info default_value_info[] = {
1412 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1415 static struct procunit_info default_info = {
1416 0, NULL, default_value_info
1419 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1420 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1424 for (i = 0; i < num_ins; i++) {
1425 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1429 type = combine_word(&dsc[4]);
1430 for (info = list; info && info->type; info++)
1431 if (info->type == type)
1433 if (! info || ! info->type)
1434 info = &default_info;
1436 for (valinfo = info->values; valinfo->control; valinfo++) {
1437 /* FIXME: bitmap might be longer than 8bit */
1438 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1440 map = find_map(state, unitid, valinfo->control);
1441 if (check_ignored_ctl(map))
1443 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1445 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1448 cval->mixer = state->mixer;
1450 cval->control = valinfo->control;
1451 cval->val_type = valinfo->val_type;
1454 /* get min/max values */
1455 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1456 /* FIXME: hard-coded */
1458 cval->max = dsc[15];
1460 cval->initialized = 1;
1462 if (type == USB_XU_CLOCK_RATE) {
1463 /* E-Mu USB 0404/0202/TrackerPre
1464 * samplerate control quirk
1469 cval->initialized = 1;
1471 get_min_max(cval, valinfo->min_value);
1474 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1476 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1480 kctl->private_free = usb_mixer_elem_free;
1482 if (check_mapped_name(map, kctl->id.name,
1483 sizeof(kctl->id.name)))
1485 else if (info->name)
1486 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1488 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1491 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1493 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1495 append_ctl_name(kctl, " ");
1496 append_ctl_name(kctl, valinfo->suffix);
1498 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1499 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1500 if ((err = add_control_to_empty(state, kctl)) < 0)
1507 static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1509 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1512 static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1514 return build_audio_procunit(state, unitid, desc, extunits, "Extension Unit");
1522 /* info callback for selector unit
1523 * use an enumerator type for routing
1525 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1527 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1528 char **itemlist = (char **)kcontrol->private_value;
1530 if (snd_BUG_ON(!itemlist))
1532 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1534 uinfo->value.enumerated.items = cval->max;
1535 if ((int)uinfo->value.enumerated.item >= cval->max)
1536 uinfo->value.enumerated.item = cval->max - 1;
1537 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1541 /* get callback for selector unit */
1542 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1544 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1547 err = get_cur_ctl_value(cval, 0, &val);
1549 if (cval->mixer->ignore_ctl_error) {
1550 ucontrol->value.enumerated.item[0] = 0;
1555 val = get_relative_value(cval, val);
1556 ucontrol->value.enumerated.item[0] = val;
1560 /* put callback for selector unit */
1561 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1563 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1566 err = get_cur_ctl_value(cval, 0, &oval);
1568 if (cval->mixer->ignore_ctl_error)
1572 val = ucontrol->value.enumerated.item[0];
1573 val = get_abs_value(cval, val);
1575 set_cur_ctl_value(cval, 0, val);
1581 /* alsa control interface for selector unit */
1582 static struct snd_kcontrol_new mixer_selectunit_ctl = {
1583 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1584 .name = "", /* will be filled later */
1585 .info = mixer_ctl_selector_info,
1586 .get = mixer_ctl_selector_get,
1587 .put = mixer_ctl_selector_put,
1591 /* private free callback.
1592 * free both private_data and private_value
1594 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
1598 if (kctl->private_data) {
1599 struct usb_mixer_elem_info *cval = kctl->private_data;
1600 num_ins = cval->max;
1602 kctl->private_data = NULL;
1604 if (kctl->private_value) {
1605 char **itemlist = (char **)kctl->private_value;
1606 for (i = 0; i < num_ins; i++)
1609 kctl->private_value = 0;
1614 * parse a selector unit
1616 static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1618 unsigned int num_ins = desc[4];
1619 unsigned int i, nameid, len;
1621 struct usb_mixer_elem_info *cval;
1622 struct snd_kcontrol *kctl;
1623 const struct usbmix_name_map *map;
1626 if (! num_ins || desc[0] < 5 + num_ins) {
1627 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1631 for (i = 0; i < num_ins; i++) {
1632 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1636 if (num_ins == 1) /* only one ? nonsense! */
1639 map = find_map(state, unitid, 0);
1640 if (check_ignored_ctl(map))
1643 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1645 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1648 cval->mixer = state->mixer;
1650 cval->val_type = USB_MIXER_U8;
1653 cval->max = num_ins;
1655 cval->initialized = 1;
1657 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1659 snd_printk(KERN_ERR "cannot malloc\n");
1663 #define MAX_ITEM_NAME_LEN 64
1664 for (i = 0; i < num_ins; i++) {
1665 struct usb_audio_term iterm;
1667 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1668 if (! namelist[i]) {
1669 snd_printk(KERN_ERR "cannot malloc\n");
1676 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1678 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1679 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1681 sprintf(namelist[i], "Input %d", i);
1684 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1686 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1691 kctl->private_value = (unsigned long)namelist;
1692 kctl->private_free = usb_mixer_selector_elem_free;
1694 nameid = desc[desc[0] - 1];
1695 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1699 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1701 len = get_term_name(state, &state->oterm,
1702 kctl->id.name, sizeof(kctl->id.name), 0);
1704 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1706 if ((state->oterm.type & 0xff00) == 0x0100)
1707 append_ctl_name(kctl, " Capture Source");
1709 append_ctl_name(kctl, " Playback Source");
1712 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1713 cval->id, kctl->id.name, num_ins);
1714 if ((err = add_control_to_empty(state, kctl)) < 0)
1722 * parse an audio unit recursively
1725 static int parse_audio_unit(struct mixer_build *state, int unitid)
1729 if (test_and_set_bit(unitid, state->unitbitmap))
1730 return 0; /* the unit already visited */
1732 p1 = find_audio_control_unit(state, unitid);
1734 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1739 case INPUT_TERMINAL:
1742 return parse_audio_mixer_unit(state, unitid, p1);
1744 return parse_audio_selector_unit(state, unitid, p1);
1746 return parse_audio_feature_unit(state, unitid, p1);
1747 case PROCESSING_UNIT:
1748 return parse_audio_processing_unit(state, unitid, p1);
1749 case EXTENSION_UNIT:
1750 return parse_audio_extension_unit(state, unitid, p1);
1752 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1757 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1759 kfree(mixer->id_elems);
1761 kfree(mixer->urb->transfer_buffer);
1762 usb_free_urb(mixer->urb);
1764 usb_free_urb(mixer->rc_urb);
1765 kfree(mixer->rc_setup_packet);
1769 static int snd_usb_mixer_dev_free(struct snd_device *device)
1771 struct usb_mixer_interface *mixer = device->device_data;
1772 snd_usb_mixer_free(mixer);
1777 * create mixer controls
1779 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1781 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1783 unsigned char *desc;
1784 struct mixer_build state;
1786 const struct usbmix_ctl_map *map;
1787 struct usb_host_interface *hostif;
1789 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1790 memset(&state, 0, sizeof(state));
1791 state.chip = mixer->chip;
1792 state.mixer = mixer;
1793 state.buffer = hostif->extra;
1794 state.buflen = hostif->extralen;
1796 /* check the mapping table */
1797 for (map = usbmix_ctl_maps; map->id; map++) {
1798 if (map->id == state.chip->usb_id) {
1799 state.map = map->map;
1800 state.selector_map = map->selector_map;
1801 mixer->ignore_ctl_error = map->ignore_ctl_error;
1807 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1809 continue; /* invalid descriptor? */
1810 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1811 state.oterm.id = desc[3];
1812 state.oterm.type = combine_word(&desc[4]);
1813 state.oterm.name = desc[8];
1814 err = parse_audio_unit(&state, desc[7]);
1821 static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1824 struct usb_mixer_elem_info *info;
1826 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1827 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1831 static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
1833 struct usb_mixer_elem_info *cval)
1835 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
1836 "S8", "U8", "S16", "U16"};
1837 snd_iprintf(buffer, " Unit: %i\n", unitid);
1839 snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n",
1840 cval->elem_id->name, cval->elem_id->index);
1841 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
1842 "channels=%i, type=\"%s\"\n", cval->id,
1843 cval->control, cval->cmask, cval->channels,
1844 val_types[cval->val_type]);
1845 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
1846 cval->min, cval->max, cval->dBmin, cval->dBmax);
1849 static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
1850 struct snd_info_buffer *buffer)
1852 struct snd_usb_audio *chip = entry->private_data;
1853 struct usb_mixer_interface *mixer;
1854 struct usb_mixer_elem_info *cval;
1857 list_for_each_entry(mixer, &chip->mixer_list, list) {
1859 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
1860 chip->usb_id, mixer->ctrlif,
1861 mixer->ignore_ctl_error);
1862 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
1863 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
1864 for (cval = mixer->id_elems[unitid]; cval;
1865 cval = cval->next_id_elem)
1866 snd_usb_mixer_dump_cval(buffer, unitid, cval);
1871 static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1876 /* unit ids specific to Extigy/Audigy 2 NX: */
1878 case 0: /* remote control */
1879 mixer->rc_urb->dev = mixer->chip->dev;
1880 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1882 case 4: /* digital in jack */
1883 case 7: /* line in jacks */
1884 case 19: /* speaker out jacks */
1885 case 20: /* headphones out jack */
1887 /* live24ext: 4 = line-in jack */
1888 case 3: /* hp-out jack (may actuate Mute) */
1889 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1890 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1891 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1894 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1899 static void snd_usb_mixer_status_complete(struct urb *urb)
1901 struct usb_mixer_interface *mixer = urb->context;
1903 if (urb->status == 0) {
1904 u8 *buf = urb->transfer_buffer;
1907 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1908 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1910 /* ignore any notifications not from the control interface */
1911 if ((buf[0] & 0x0f) != 0)
1913 if (!(buf[0] & 0x40))
1914 snd_usb_mixer_notify_id(mixer, buf[1]);
1916 snd_usb_mixer_memory_change(mixer, buf[1]);
1919 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1920 urb->dev = mixer->chip->dev;
1921 usb_submit_urb(urb, GFP_ATOMIC);
1925 /* create the handler for the optional status interrupt endpoint */
1926 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1928 struct usb_host_interface *hostif;
1929 struct usb_endpoint_descriptor *ep;
1930 void *transfer_buffer;
1934 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1935 /* we need one interrupt input endpoint */
1936 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1938 ep = get_endpoint(hostif, 0);
1939 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
1942 epnum = usb_endpoint_num(ep);
1943 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1944 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1945 if (!transfer_buffer)
1947 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1949 kfree(transfer_buffer);
1952 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1953 usb_rcvintpipe(mixer->chip->dev, epnum),
1954 transfer_buffer, buffer_length,
1955 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1956 usb_submit_urb(mixer->urb, GFP_KERNEL);
1960 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
1962 struct usb_mixer_interface *mixer = urb->context;
1963 const struct rc_config *rc = mixer->rc_cfg;
1966 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
1969 code = mixer->rc_buffer[rc->offset];
1970 if (rc->length == 2)
1971 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1973 /* the Mute button actually changes the mixer control */
1974 if (code == rc->mute_code)
1975 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
1976 mixer->rc_code = code;
1978 wake_up(&mixer->rc_waitq);
1981 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
1982 long count, loff_t *offset)
1984 struct usb_mixer_interface *mixer = hw->private_data;
1988 if (count != 1 && count != 4)
1990 err = wait_event_interruptible(mixer->rc_waitq,
1991 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1994 err = put_user(rc_code, buf);
1996 err = put_user(rc_code, (u32 __user *)buf);
1998 return err < 0 ? err : count;
2001 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
2004 struct usb_mixer_interface *mixer = hw->private_data;
2006 poll_wait(file, &mixer->rc_waitq, wait);
2007 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
2010 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
2012 struct snd_hwdep *hwdep;
2015 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
2016 if (rc_configs[i].usb_id == mixer->chip->usb_id)
2018 if (i >= ARRAY_SIZE(rc_configs))
2020 mixer->rc_cfg = &rc_configs[i];
2022 len = mixer->rc_cfg->packet_length;
2024 init_waitqueue_head(&mixer->rc_waitq);
2025 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
2028 snprintf(hwdep->name, sizeof(hwdep->name),
2029 "%s remote control", mixer->chip->card->shortname);
2030 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
2031 hwdep->private_data = mixer;
2032 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
2033 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
2034 hwdep->exclusive = 1;
2036 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
2039 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
2040 if (!mixer->rc_setup_packet) {
2041 usb_free_urb(mixer->rc_urb);
2042 mixer->rc_urb = NULL;
2045 mixer->rc_setup_packet->bRequestType =
2046 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2047 mixer->rc_setup_packet->bRequest = GET_MEM;
2048 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
2049 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
2050 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
2051 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
2052 usb_rcvctrlpipe(mixer->chip->dev, 0),
2053 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
2054 snd_usb_soundblaster_remote_complete, mixer);
2058 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
2060 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2062 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2063 int index = kcontrol->private_value;
2065 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
2069 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2071 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2072 int index = kcontrol->private_value;
2073 int value = ucontrol->value.integer.value[0];
2078 changed = value != mixer->audigy2nx_leds[index];
2079 err = snd_usb_ctl_msg(mixer->chip->dev,
2080 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
2081 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2082 value, index + 2, NULL, 0, 100);
2085 mixer->audigy2nx_leds[index] = value;
2089 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
2091 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2092 .name = "CMSS LED Switch",
2093 .info = snd_audigy2nx_led_info,
2094 .get = snd_audigy2nx_led_get,
2095 .put = snd_audigy2nx_led_put,
2099 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2100 .name = "Power LED Switch",
2101 .info = snd_audigy2nx_led_info,
2102 .get = snd_audigy2nx_led_get,
2103 .put = snd_audigy2nx_led_put,
2107 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2108 .name = "Dolby Digital LED Switch",
2109 .info = snd_audigy2nx_led_info,
2110 .get = snd_audigy2nx_led_get,
2111 .put = snd_audigy2nx_led_put,
2116 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
2120 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
2121 if (i > 1 && /* Live24ext has 2 LEDs only */
2122 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2123 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
2125 err = snd_ctl_add(mixer->chip->card,
2126 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
2130 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
2134 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
2135 struct snd_info_buffer *buffer)
2137 static const struct sb_jack {
2140 } jacks_audigy2nx[] = {
2146 }, jacks_live24ext[] = {
2147 {4, "line in"}, /* &1=Line, &2=Mic*/
2148 {3, "hph out"}, /* headphones */
2149 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2152 const struct sb_jack *jacks;
2153 struct usb_mixer_interface *mixer = entry->private_data;
2157 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
2158 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2159 jacks = jacks_audigy2nx;
2160 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2161 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
2162 jacks = jacks_live24ext;
2166 for (i = 0; jacks[i].name; ++i) {
2167 snd_iprintf(buffer, "%s: ", jacks[i].name);
2168 err = snd_usb_ctl_msg(mixer->chip->dev,
2169 usb_rcvctrlpipe(mixer->chip->dev, 0),
2170 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2171 USB_RECIP_INTERFACE, 0,
2172 jacks[i].unitid << 8, buf, 3, 100);
2173 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
2174 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2176 snd_iprintf(buffer, "?\n");
2180 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
2181 struct snd_ctl_elem_value *ucontrol)
2183 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2185 ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
2189 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
2190 struct snd_ctl_elem_value *ucontrol)
2192 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2193 u8 old_status, new_status;
2196 old_status = mixer->xonar_u1_status;
2197 if (ucontrol->value.integer.value[0])
2198 new_status = old_status | 0x02;
2200 new_status = old_status & ~0x02;
2201 changed = new_status != old_status;
2202 err = snd_usb_ctl_msg(mixer->chip->dev,
2203 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
2204 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2205 50, 0, &new_status, 1, 100);
2208 mixer->xonar_u1_status = new_status;
2212 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
2213 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2214 .name = "Digital Playback Switch",
2215 .info = snd_ctl_boolean_mono_info,
2216 .get = snd_xonar_u1_switch_get,
2217 .put = snd_xonar_u1_switch_put,
2220 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
2224 err = snd_ctl_add(mixer->chip->card,
2225 snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
2228 mixer->xonar_u1_status = 0x05;
2232 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
2233 unsigned char samplerate_id)
2235 struct usb_mixer_interface *mixer;
2236 struct usb_mixer_elem_info *cval;
2237 int unitid = 12; /* SamleRate ExtensionUnit ID */
2239 list_for_each_entry(mixer, &chip->mixer_list, list) {
2240 cval = mixer->id_elems[unitid];
2242 set_cur_ctl_value(cval, cval->control << 8,
2244 snd_usb_mixer_notify_id(mixer, unitid);
2250 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2253 static struct snd_device_ops dev_ops = {
2254 .dev_free = snd_usb_mixer_dev_free
2256 struct usb_mixer_interface *mixer;
2257 struct snd_info_entry *entry;
2260 strcpy(chip->card->mixername, "USB Mixer");
2262 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
2266 mixer->ctrlif = ctrlif;
2267 mixer->ignore_ctl_error = ignore_error;
2268 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2269 if (!mixer->id_elems) {
2274 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
2275 (err = snd_usb_mixer_status_create(mixer)) < 0)
2278 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2281 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
2282 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2283 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
2284 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2286 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
2287 snd_info_set_text_ops(entry, mixer,
2288 snd_audigy2nx_proc_read);
2291 if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
2292 mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
2293 err = snd_xonar_u1_controls_create(mixer);
2298 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
2302 if (list_empty(&chip->mixer_list) &&
2303 !snd_card_proc_new(chip->card, "usbmixer", &entry))
2304 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
2306 list_add(&mixer->list, &chip->mixer_list);
2310 snd_usb_mixer_free(mixer);
2314 void snd_usb_mixer_disconnect(struct list_head *p)
2316 struct usb_mixer_interface *mixer;
2318 mixer = list_entry(p, struct usb_mixer_interface, list);
2319 usb_kill_urb(mixer->urb);
2320 usb_kill_urb(mixer->rc_urb);