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 struct usb_mixer_interface {
73 struct snd_usb_audio *chip;
75 struct list_head list;
76 unsigned int ignore_ctl_error;
78 struct usb_mixer_elem_info **id_elems; /* array[256], indexed by unit id */
80 /* Sound Blaster remote control stuff */
81 const struct rc_config *rc_cfg;
83 wait_queue_head_t rc_waitq;
85 struct usb_ctrlrequest *rc_setup_packet;
93 struct usb_audio_term {
97 unsigned int chconfig;
101 struct usbmix_name_map;
104 struct snd_usb_audio *chip;
105 struct usb_mixer_interface *mixer;
106 unsigned char *buffer;
108 DECLARE_BITMAP(unitbitmap, 256);
109 struct usb_audio_term oterm;
110 const struct usbmix_name_map *map;
111 const struct usbmix_selector_map *selector_map;
114 #define MAX_CHANNELS 10 /* max logical channels */
116 struct usb_mixer_elem_info {
117 struct usb_mixer_interface *mixer;
118 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
119 struct snd_ctl_elem_id *elem_id;
121 unsigned int control; /* CS or ICN (high byte) */
122 unsigned int cmask; /* channel mask bitmap: 0 = master */
128 int cache_val[MAX_CHANNELS];
134 USB_FEATURE_NONE = 0,
135 USB_FEATURE_MUTE = 1,
143 USB_FEATURE_BASSBOOST,
149 USB_MIXER_INV_BOOLEAN,
158 USB_PROC_UPDOWN_SWITCH = 1,
159 USB_PROC_UPDOWN_MODE_SEL = 2,
161 USB_PROC_PROLOGIC = 2,
162 USB_PROC_PROLOGIC_SWITCH = 1,
163 USB_PROC_PROLOGIC_MODE_SEL = 2,
166 USB_PROC_3DENH_SWITCH = 1,
167 USB_PROC_3DENH_SPACE = 2,
170 USB_PROC_REVERB_SWITCH = 1,
171 USB_PROC_REVERB_LEVEL = 2,
172 USB_PROC_REVERB_TIME = 3,
173 USB_PROC_REVERB_DELAY = 4,
176 USB_PROC_CHORUS_SWITCH = 1,
177 USB_PROC_CHORUS_LEVEL = 2,
178 USB_PROC_CHORUS_RATE = 3,
179 USB_PROC_CHORUS_DEPTH = 4,
182 USB_PROC_DCR_SWITCH = 1,
183 USB_PROC_DCR_RATIO = 2,
184 USB_PROC_DCR_MAX_AMP = 3,
185 USB_PROC_DCR_THRESHOLD = 4,
186 USB_PROC_DCR_ATTACK = 5,
187 USB_PROC_DCR_RELEASE = 6,
190 /*E-mu 0202(0404) eXtension Unit(XU) control*/
192 USB_XU_CLOCK_RATE = 0xe301,
193 USB_XU_CLOCK_SOURCE = 0xe302,
194 USB_XU_DIGITAL_IO_STATUS = 0xe303,
195 USB_XU_DEVICE_OPTIONS = 0xe304,
196 USB_XU_DIRECT_MONITORING = 0xe305,
197 USB_XU_METERING = 0xe306
200 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
201 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
202 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
203 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
207 * manual mapping of mixer names
208 * if the mixer topology is too complicated and the parsed names are
209 * ambiguous, add the entries in usbmixer_maps.c.
211 #include "usbmixer_maps.c"
213 static const struct usbmix_name_map *
214 find_map(struct mixer_build *state, int unitid, int control)
216 const struct usbmix_name_map *p = state->map;
221 for (p = state->map; p->id; p++) {
222 if (p->id == unitid &&
223 (!control || !p->control || control == p->control))
229 /* get the mapped name if the unit matches */
231 check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
237 return strlcpy(buf, p->name, buflen);
240 /* check whether the control should be ignored */
242 check_ignored_ctl(const struct usbmix_name_map *p)
244 if (!p || p->name || p->dB)
250 static inline void check_mapped_dB(const struct usbmix_name_map *p,
251 struct usb_mixer_elem_info *cval)
254 cval->dBmin = p->dB->min;
255 cval->dBmax = p->dB->max;
259 /* get the mapped selector source name */
260 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
261 int index, char *buf, int buflen)
263 const struct usbmix_selector_map *p;
265 if (! state->selector_map)
267 for (p = state->selector_map; p->id; p++) {
268 if (p->id == unitid && index < p->count)
269 return strlcpy(buf, p->names[index], buflen);
275 * find an audio control unit with the given unit id
277 static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
282 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
283 USB_DT_CS_INTERFACE)) != NULL) {
284 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
292 * copy a string with the given id
294 static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
296 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
302 * convert from the byte/word on usb descriptor to the zero-based integer
304 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
306 switch (cval->val_type) {
307 case USB_MIXER_BOOLEAN:
309 case USB_MIXER_INV_BOOLEAN:
332 * convert from the zero-based int to the byte/word for usb descriptor
334 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
336 switch (cval->val_type) {
337 case USB_MIXER_BOOLEAN:
339 case USB_MIXER_INV_BOOLEAN:
348 return 0; /* not reached */
351 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
357 else if (val >= cval->max)
358 return (cval->max - cval->min + cval->res - 1) / cval->res;
360 return (val - cval->min) / cval->res;
363 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
378 * retrieve a mixer value
381 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
383 unsigned char buf[2];
384 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
387 while (timeout-- > 0) {
388 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
389 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
391 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
392 validx, cval->mixer->ctrlif | (cval->id << 8),
393 buf, val_len, 100) >= val_len) {
394 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
398 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
399 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
403 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
405 return get_ctl_value(cval, GET_CUR, validx, value);
408 /* channel = 0: master, 1 = first channel */
409 static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
410 int channel, int *value)
412 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
415 static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
416 int channel, int index, int *value)
420 if (cval->cached & (1 << channel)) {
421 *value = cval->cache_val[index];
424 err = get_cur_mix_raw(cval, channel, value);
426 if (!cval->mixer->ignore_ctl_error)
427 snd_printd(KERN_ERR "cannot get current value for "
428 "control %d ch %d: err = %d\n",
429 cval->control, channel, err);
432 cval->cached |= 1 << channel;
433 cval->cache_val[index] = *value;
442 static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
444 unsigned char buf[2];
445 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
448 value_set = convert_bytes_value(cval, value_set);
449 buf[0] = value_set & 0xff;
450 buf[1] = (value_set >> 8) & 0xff;
451 while (timeout-- > 0)
452 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
453 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
455 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
456 validx, cval->mixer->ctrlif | (cval->id << 8),
457 buf, val_len, 100) >= 0)
459 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
460 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
464 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
466 return set_ctl_value(cval, SET_CUR, validx, value);
469 static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
470 int index, int value)
473 err = set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel,
477 cval->cached |= 1 << channel;
478 cval->cache_val[index] = value;
483 * TLV callback for mixer volume controls
485 static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
486 unsigned int size, unsigned int __user *_tlv)
488 struct usb_mixer_elem_info *cval = kcontrol->private_data;
489 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
491 if (size < sizeof(scale))
493 scale[2] = cval->dBmin;
494 scale[3] = cval->dBmax;
495 if (copy_to_user(_tlv, scale, sizeof(scale)))
501 * parser routines begin here...
504 static int parse_audio_unit(struct mixer_build *state, int unitid);
508 * check if the input/output channel routing is enabled on the given bitmap.
509 * used for mixer unit parser
511 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
513 int idx = ich * num_outs + och;
514 return bmap[idx >> 3] & (0x80 >> (idx & 7));
519 * add an alsa control element
520 * search and increment the index until an empty slot is found.
522 * if failed, give up and free the control instance.
525 static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
527 struct usb_mixer_elem_info *cval = kctl->private_data;
530 while (snd_ctl_find_id(state->chip->card, &kctl->id))
532 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
533 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
536 cval->elem_id = &kctl->id;
537 cval->next_id_elem = state->mixer->id_elems[cval->id];
538 state->mixer->id_elems[cval->id] = cval;
544 * get a terminal name string
547 static struct iterm_name_combo {
551 { 0x0300, "Output" },
552 { 0x0301, "Speaker" },
553 { 0x0302, "Headphone" },
554 { 0x0303, "HMD Audio" },
555 { 0x0304, "Desktop Speaker" },
556 { 0x0305, "Room Speaker" },
557 { 0x0306, "Com Speaker" },
559 { 0x0600, "External In" },
560 { 0x0601, "Analog In" },
561 { 0x0602, "Digital In" },
563 { 0x0604, "Legacy In" },
564 { 0x0605, "IEC958 In" },
565 { 0x0606, "1394 DA Stream" },
566 { 0x0607, "1394 DV Stream" },
567 { 0x0700, "Embedded" },
568 { 0x0701, "Noise Source" },
569 { 0x0702, "Equalization Noise" },
573 { 0x0706, "MiniDisk" },
574 { 0x0707, "Analog Tape" },
575 { 0x0708, "Phonograph" },
576 { 0x0709, "VCR Audio" },
577 { 0x070a, "Video Disk Audio" },
578 { 0x070b, "DVD Audio" },
579 { 0x070c, "TV Tuner Audio" },
580 { 0x070d, "Satellite Rec Audio" },
581 { 0x070e, "Cable Tuner Audio" },
582 { 0x070f, "DSS Audio" },
583 { 0x0710, "Radio Receiver" },
584 { 0x0711, "Radio Transmitter" },
585 { 0x0712, "Multi-Track Recorder" },
586 { 0x0713, "Synthesizer" },
590 static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
591 unsigned char *name, int maxlen, int term_only)
593 struct iterm_name_combo *names;
596 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
598 /* virtual type - not a real terminal */
599 if (iterm->type >> 16) {
602 switch (iterm->type >> 16) {
604 strcpy(name, "Selector"); return 8;
605 case PROCESSING_UNIT:
606 strcpy(name, "Process Unit"); return 12;
608 strcpy(name, "Ext Unit"); return 8;
610 strcpy(name, "Mixer"); return 5;
612 return sprintf(name, "Unit %d", iterm->id);
616 switch (iterm->type & 0xff00) {
618 strcpy(name, "PCM"); return 3;
620 strcpy(name, "Mic"); return 3;
622 strcpy(name, "Headset"); return 7;
624 strcpy(name, "Phone"); return 5;
627 for (names = iterm_names; names->type; names++)
628 if (names->type == iterm->type) {
629 strcpy(name, names->name);
630 return strlen(names->name);
637 * parse the source unit recursively until it reaches to a terminal
638 * or a branched unit.
640 static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
644 memset(term, 0, sizeof(*term));
645 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
649 term->type = combine_word(p1 + 4);
650 term->channels = p1[7];
651 term->chconfig = combine_word(p1 + 8);
656 break; /* continue to parse */
658 term->type = p1[2] << 16; /* virtual type */
659 term->channels = p1[5 + p1[4]];
660 term->chconfig = combine_word(p1 + 6 + p1[4]);
661 term->name = p1[p1[0] - 1];
664 /* call recursively to retrieve the channel info */
665 if (check_input_term(state, p1[5], term) < 0)
667 term->type = p1[2] << 16; /* virtual type */
669 term->name = p1[9 + p1[0] - 1];
671 case PROCESSING_UNIT:
675 break; /* continue to parse */
677 term->type = p1[2] << 16; /* virtual type */
678 term->channels = p1[7 + p1[6]];
679 term->chconfig = combine_word(p1 + 8 + p1[6]);
680 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
694 /* feature unit control information */
695 struct usb_feature_control_info {
697 unsigned int type; /* control type (mute, volume, etc.) */
700 static struct usb_feature_control_info audio_feature_info[] = {
701 { "Mute", USB_MIXER_INV_BOOLEAN },
702 { "Volume", USB_MIXER_S16 },
703 { "Tone Control - Bass", USB_MIXER_S8 },
704 { "Tone Control - Mid", USB_MIXER_S8 },
705 { "Tone Control - Treble", USB_MIXER_S8 },
706 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
707 { "Auto Gain Control", USB_MIXER_BOOLEAN },
708 { "Delay Control", USB_MIXER_U16 },
709 { "Bass Boost", USB_MIXER_BOOLEAN },
710 { "Loudness", USB_MIXER_BOOLEAN },
714 /* private_free callback */
715 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
717 kfree(kctl->private_data);
718 kctl->private_data = NULL;
723 * interface to ALSA control for feature/mixer units
727 * retrieve the minimum and maximum values for the specified control
729 static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
732 cval->min = default_min;
733 cval->max = cval->min + 1;
735 cval->dBmin = cval->dBmax = 0;
737 if (cval->val_type == USB_MIXER_BOOLEAN ||
738 cval->val_type == USB_MIXER_INV_BOOLEAN) {
739 cval->initialized = 1;
744 for (i = 0; i < MAX_CHANNELS; i++)
745 if (cval->cmask & (1 << i)) {
750 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
751 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
752 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
753 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
756 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
759 int last_valid_res = cval->res;
761 while (cval->res > 1) {
762 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
766 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
767 cval->res = last_valid_res;
772 /* Additional checks for the proper resolution
774 * Some devices report smaller resolutions than actually
775 * reacting. They don't return errors but simply clip
776 * to the lower aligned value.
778 if (cval->min + cval->res < cval->max) {
779 int last_valid_res = cval->res;
780 int saved, test, check;
781 get_cur_mix_raw(cval, minchn, &saved);
784 if (test < cval->max)
788 if (test < cval->min || test > cval->max ||
789 set_cur_mix_value(cval, minchn, 0, test) ||
790 get_cur_mix_raw(cval, minchn, &check)) {
791 cval->res = last_valid_res;
798 set_cur_mix_value(cval, minchn, 0, saved);
801 cval->initialized = 1;
804 /* USB descriptions contain the dB scale in 1/256 dB unit
805 * while ALSA TLV contains in 1/100 dB unit
807 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
808 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
809 if (cval->dBmin > cval->dBmax) {
810 /* something is wrong; assume it's either from/to 0dB */
813 else if (cval->dBmin > 0)
815 if (cval->dBmin > cval->dBmax) {
816 /* totally crap, return an error */
825 /* get a feature/mixer unit info */
826 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
828 struct usb_mixer_elem_info *cval = kcontrol->private_data;
830 if (cval->val_type == USB_MIXER_BOOLEAN ||
831 cval->val_type == USB_MIXER_INV_BOOLEAN)
832 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
834 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
835 uinfo->count = cval->channels;
836 if (cval->val_type == USB_MIXER_BOOLEAN ||
837 cval->val_type == USB_MIXER_INV_BOOLEAN) {
838 uinfo->value.integer.min = 0;
839 uinfo->value.integer.max = 1;
841 if (! cval->initialized)
842 get_min_max(cval, 0);
843 uinfo->value.integer.min = 0;
844 uinfo->value.integer.max =
845 (cval->max - cval->min + cval->res - 1) / cval->res;
850 /* get the current value from feature/mixer unit */
851 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
853 struct usb_mixer_elem_info *cval = kcontrol->private_data;
854 int c, cnt, val, err;
856 ucontrol->value.integer.value[0] = cval->min;
859 for (c = 0; c < MAX_CHANNELS; c++) {
860 if (!(cval->cmask & (1 << c)))
862 err = get_cur_mix_value(cval, c + 1, cnt, &val);
864 return cval->mixer->ignore_ctl_error ? 0 : err;
865 val = get_relative_value(cval, val);
866 ucontrol->value.integer.value[cnt] = val;
872 err = get_cur_mix_value(cval, 0, 0, &val);
874 return cval->mixer->ignore_ctl_error ? 0 : err;
875 val = get_relative_value(cval, val);
876 ucontrol->value.integer.value[0] = val;
881 /* put the current value to feature/mixer unit */
882 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
884 struct usb_mixer_elem_info *cval = kcontrol->private_data;
885 int c, cnt, val, oval, err;
890 for (c = 0; c < MAX_CHANNELS; c++) {
891 if (!(cval->cmask & (1 << c)))
893 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
895 return cval->mixer->ignore_ctl_error ? 0 : err;
896 val = ucontrol->value.integer.value[cnt];
897 val = get_abs_value(cval, val);
899 set_cur_mix_value(cval, c + 1, cnt, val);
906 err = get_cur_mix_value(cval, 0, 0, &oval);
908 return cval->mixer->ignore_ctl_error ? 0 : err;
909 val = ucontrol->value.integer.value[0];
910 val = get_abs_value(cval, val);
912 set_cur_mix_value(cval, 0, 0, val);
919 static struct snd_kcontrol_new usb_feature_unit_ctl = {
920 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
921 .name = "", /* will be filled later manually */
922 .info = mixer_ctl_feature_info,
923 .get = mixer_ctl_feature_get,
924 .put = mixer_ctl_feature_put,
929 * build a feature control
932 static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
934 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
937 static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
938 unsigned int ctl_mask, int control,
939 struct usb_audio_term *iterm, int unitid)
941 unsigned int len = 0;
943 int nameid = desc[desc[0] - 1];
944 struct snd_kcontrol *kctl;
945 struct usb_mixer_elem_info *cval;
946 const struct usbmix_name_map *map;
948 control++; /* change from zero-based to 1-based value */
950 if (control == USB_FEATURE_GEQ) {
951 /* FIXME: not supported yet */
955 map = find_map(state, unitid, control);
956 if (check_ignored_ctl(map))
959 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
961 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
964 cval->mixer = state->mixer;
966 cval->control = control;
967 cval->cmask = ctl_mask;
968 cval->val_type = audio_feature_info[control-1].type;
970 cval->channels = 1; /* master channel */
973 for (i = 0; i < 16; i++)
974 if (ctl_mask & (1 << i))
979 /* get min/max values */
980 get_min_max(cval, 0);
982 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
984 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
988 kctl->private_free = usb_mixer_elem_free;
990 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
991 mapped_name = len != 0;
993 len = snd_usb_copy_string_desc(state, nameid,
994 kctl->id.name, sizeof(kctl->id.name));
997 case USB_FEATURE_MUTE:
998 case USB_FEATURE_VOLUME:
999 /* determine the control name. the rule is:
1000 * - if a name id is given in descriptor, use it.
1001 * - if the connected input can be determined, then use the name
1003 * - if the connected output can be determined, use it.
1004 * - otherwise, anonymous name.
1007 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
1009 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
1011 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
1012 "Feature %d", unitid);
1014 /* determine the stream direction:
1015 * if the connected output is USB stream, then it's likely a
1016 * capture stream. otherwise it should be playback (hopefully :)
1018 if (! mapped_name && ! (state->oterm.type >> 16)) {
1019 if ((state->oterm.type & 0xff00) == 0x0100) {
1020 len = append_ctl_name(kctl, " Capture");
1022 len = append_ctl_name(kctl, " Playback");
1025 append_ctl_name(kctl, control == USB_FEATURE_MUTE ?
1026 " Switch" : " Volume");
1027 if (control == USB_FEATURE_VOLUME) {
1028 kctl->tlv.c = mixer_vol_tlv;
1029 kctl->vd[0].access |=
1030 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1031 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1032 check_mapped_dB(map, cval);
1038 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1039 sizeof(kctl->id.name));
1043 /* volume control quirks */
1044 switch (state->chip->usb_id) {
1045 case USB_ID(0x0471, 0x0101):
1046 case USB_ID(0x0471, 0x0104):
1047 case USB_ID(0x0471, 0x0105):
1048 case USB_ID(0x0672, 0x1041):
1049 /* quirk for UDA1321/N101.
1050 * note that detection between firmware 2.1.1.7 (N101)
1051 * and later 2.1.1.21 is not very clear from datasheets.
1052 * I hope that the min value is -15360 for newer firmware --jk
1054 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1055 cval->min == -15616) {
1056 snd_printk(KERN_INFO
1057 "set volume quirk for UDA1321/N101 chip\n");
1062 case USB_ID(0x046d, 0x09a4):
1063 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1064 snd_printk(KERN_INFO
1065 "set volume quirk for QuickCam E3500\n");
1074 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1075 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
1076 add_control_to_empty(state, kctl);
1082 * parse a feature unit
1084 * most of controlls are defined here.
1086 static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
1089 struct usb_audio_term iterm;
1090 unsigned int master_bits, first_ch_bits;
1093 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1094 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1098 /* parse the source unit */
1099 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1102 /* determine the input source type and name */
1103 if (check_input_term(state, ftr[4], &iterm) < 0)
1106 channels = (ftr[0] - 7) / csize - 1;
1108 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
1109 /* master configuration quirks */
1110 switch (state->chip->usb_id) {
1111 case USB_ID(0x08bb, 0x2702):
1112 snd_printk(KERN_INFO
1113 "usbmixer: master volume quirk for PCM2702 chip\n");
1114 /* disable non-functional volume control */
1115 master_bits &= ~(1 << (USB_FEATURE_VOLUME - 1));
1119 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1122 /* check all control types */
1123 for (i = 0; i < 10; i++) {
1124 unsigned int ch_bits = 0;
1125 for (j = 0; j < channels; j++) {
1126 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1127 if (mask & (1 << i))
1128 ch_bits |= (1 << j);
1130 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1131 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1132 if (master_bits & (1 << i))
1133 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1145 * build a mixer unit control
1147 * the callbacks are identical with feature unit.
1148 * input channel number (zero based) is given in control field instead.
1151 static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
1152 int in_pin, int in_ch, int unitid,
1153 struct usb_audio_term *iterm)
1155 struct usb_mixer_elem_info *cval;
1156 unsigned int input_pins = desc[4];
1157 unsigned int num_outs = desc[5 + input_pins];
1158 unsigned int i, len;
1159 struct snd_kcontrol *kctl;
1160 const struct usbmix_name_map *map;
1162 map = find_map(state, unitid, 0);
1163 if (check_ignored_ctl(map))
1166 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1170 cval->mixer = state->mixer;
1172 cval->control = in_ch + 1; /* based on 1 */
1173 cval->val_type = USB_MIXER_S16;
1174 for (i = 0; i < num_outs; i++) {
1175 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1176 cval->cmask |= (1 << i);
1181 /* get min/max values */
1182 get_min_max(cval, 0);
1184 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1186 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1190 kctl->private_free = usb_mixer_elem_free;
1192 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1194 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1196 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1197 append_ctl_name(kctl, " Volume");
1199 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1200 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1201 add_control_to_empty(state, kctl);
1206 * parse a mixer unit
1208 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1210 struct usb_audio_term iterm;
1211 int input_pins, num_ins, num_outs;
1214 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1215 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1218 /* no bmControls field (e.g. Maya44) -> ignore */
1219 if (desc[0] <= 10 + input_pins) {
1220 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1226 for (pin = 0; pin < input_pins; pin++) {
1227 err = parse_audio_unit(state, desc[5 + pin]);
1230 err = check_input_term(state, desc[5 + pin], &iterm);
1233 num_ins += iterm.channels;
1234 for (; ich < num_ins; ++ich) {
1235 int och, ich_has_controls = 0;
1237 for (och = 0; och < num_outs; ++och) {
1238 if (check_matrix_bitmap(desc + 9 + input_pins,
1239 ich, och, num_outs)) {
1240 ich_has_controls = 1;
1244 if (ich_has_controls)
1245 build_mixer_unit_ctl(state, desc, pin, ich,
1254 * Processing Unit / Extension Unit
1257 /* get callback for processing/extension unit */
1258 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1260 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1263 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1264 if (err < 0 && cval->mixer->ignore_ctl_error) {
1265 ucontrol->value.integer.value[0] = cval->min;
1270 val = get_relative_value(cval, val);
1271 ucontrol->value.integer.value[0] = val;
1275 /* put callback for processing/extension unit */
1276 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1278 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1281 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1283 if (cval->mixer->ignore_ctl_error)
1287 val = ucontrol->value.integer.value[0];
1288 val = get_abs_value(cval, val);
1290 set_cur_ctl_value(cval, cval->control << 8, val);
1296 /* alsa control interface for processing/extension unit */
1297 static struct snd_kcontrol_new mixer_procunit_ctl = {
1298 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1299 .name = "", /* will be filled later */
1300 .info = mixer_ctl_feature_info,
1301 .get = mixer_ctl_procunit_get,
1302 .put = mixer_ctl_procunit_put,
1307 * predefined data for processing units
1309 struct procunit_value_info {
1316 struct procunit_info {
1319 struct procunit_value_info *values;
1322 static struct procunit_value_info updown_proc_info[] = {
1323 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1324 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1327 static struct procunit_value_info prologic_proc_info[] = {
1328 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1329 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1332 static struct procunit_value_info threed_enh_proc_info[] = {
1333 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1334 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1337 static struct procunit_value_info reverb_proc_info[] = {
1338 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1339 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1340 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1341 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1344 static struct procunit_value_info chorus_proc_info[] = {
1345 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1346 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1347 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1348 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1351 static struct procunit_value_info dcr_proc_info[] = {
1352 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1353 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1354 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1355 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1356 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1357 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1361 static struct procunit_info procunits[] = {
1362 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1363 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1364 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1365 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1366 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1367 { USB_PROC_DCR, "DCR", dcr_proc_info },
1371 * predefined data for extension units
1373 static struct procunit_value_info clock_rate_xu_info[] = {
1374 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
1377 static struct procunit_value_info clock_source_xu_info[] = {
1378 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
1381 static struct procunit_value_info spdif_format_xu_info[] = {
1382 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
1385 static struct procunit_value_info soft_limit_xu_info[] = {
1386 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
1389 static struct procunit_info extunits[] = {
1390 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
1391 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
1392 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
1393 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
1397 * build a processing/extension unit
1399 static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1401 int num_ins = dsc[6];
1402 struct usb_mixer_elem_info *cval;
1403 struct snd_kcontrol *kctl;
1404 int i, err, nameid, type, len;
1405 struct procunit_info *info;
1406 struct procunit_value_info *valinfo;
1407 const struct usbmix_name_map *map;
1408 static struct procunit_value_info default_value_info[] = {
1409 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1412 static struct procunit_info default_info = {
1413 0, NULL, default_value_info
1416 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1417 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1421 for (i = 0; i < num_ins; i++) {
1422 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1426 type = combine_word(&dsc[4]);
1427 for (info = list; info && info->type; info++)
1428 if (info->type == type)
1430 if (! info || ! info->type)
1431 info = &default_info;
1433 for (valinfo = info->values; valinfo->control; valinfo++) {
1434 /* FIXME: bitmap might be longer than 8bit */
1435 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1437 map = find_map(state, unitid, valinfo->control);
1438 if (check_ignored_ctl(map))
1440 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1442 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1445 cval->mixer = state->mixer;
1447 cval->control = valinfo->control;
1448 cval->val_type = valinfo->val_type;
1451 /* get min/max values */
1452 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1453 /* FIXME: hard-coded */
1455 cval->max = dsc[15];
1457 cval->initialized = 1;
1459 if (type == USB_XU_CLOCK_RATE) {
1460 /* E-Mu USB 0404/0202/TrackerPre
1461 * samplerate control quirk
1466 cval->initialized = 1;
1468 get_min_max(cval, valinfo->min_value);
1471 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1473 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1477 kctl->private_free = usb_mixer_elem_free;
1479 if (check_mapped_name(map, kctl->id.name,
1480 sizeof(kctl->id.name)))
1482 else if (info->name)
1483 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1485 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1488 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1490 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1492 append_ctl_name(kctl, " ");
1493 append_ctl_name(kctl, valinfo->suffix);
1495 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1496 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1497 if ((err = add_control_to_empty(state, kctl)) < 0)
1504 static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1506 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1509 static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1511 return build_audio_procunit(state, unitid, desc, extunits, "Extension Unit");
1519 /* info callback for selector unit
1520 * use an enumerator type for routing
1522 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1524 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1525 char **itemlist = (char **)kcontrol->private_value;
1527 if (snd_BUG_ON(!itemlist))
1529 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1531 uinfo->value.enumerated.items = cval->max;
1532 if ((int)uinfo->value.enumerated.item >= cval->max)
1533 uinfo->value.enumerated.item = cval->max - 1;
1534 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1538 /* get callback for selector unit */
1539 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1541 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1544 err = get_cur_ctl_value(cval, 0, &val);
1546 if (cval->mixer->ignore_ctl_error) {
1547 ucontrol->value.enumerated.item[0] = 0;
1552 val = get_relative_value(cval, val);
1553 ucontrol->value.enumerated.item[0] = val;
1557 /* put callback for selector unit */
1558 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1560 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1563 err = get_cur_ctl_value(cval, 0, &oval);
1565 if (cval->mixer->ignore_ctl_error)
1569 val = ucontrol->value.enumerated.item[0];
1570 val = get_abs_value(cval, val);
1572 set_cur_ctl_value(cval, 0, val);
1578 /* alsa control interface for selector unit */
1579 static struct snd_kcontrol_new mixer_selectunit_ctl = {
1580 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1581 .name = "", /* will be filled later */
1582 .info = mixer_ctl_selector_info,
1583 .get = mixer_ctl_selector_get,
1584 .put = mixer_ctl_selector_put,
1588 /* private free callback.
1589 * free both private_data and private_value
1591 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
1595 if (kctl->private_data) {
1596 struct usb_mixer_elem_info *cval = kctl->private_data;
1597 num_ins = cval->max;
1599 kctl->private_data = NULL;
1601 if (kctl->private_value) {
1602 char **itemlist = (char **)kctl->private_value;
1603 for (i = 0; i < num_ins; i++)
1606 kctl->private_value = 0;
1611 * parse a selector unit
1613 static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1615 unsigned int num_ins = desc[4];
1616 unsigned int i, nameid, len;
1618 struct usb_mixer_elem_info *cval;
1619 struct snd_kcontrol *kctl;
1620 const struct usbmix_name_map *map;
1623 if (! num_ins || desc[0] < 5 + num_ins) {
1624 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1628 for (i = 0; i < num_ins; i++) {
1629 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1633 if (num_ins == 1) /* only one ? nonsense! */
1636 map = find_map(state, unitid, 0);
1637 if (check_ignored_ctl(map))
1640 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1642 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1645 cval->mixer = state->mixer;
1647 cval->val_type = USB_MIXER_U8;
1650 cval->max = num_ins;
1652 cval->initialized = 1;
1654 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1656 snd_printk(KERN_ERR "cannot malloc\n");
1660 #define MAX_ITEM_NAME_LEN 64
1661 for (i = 0; i < num_ins; i++) {
1662 struct usb_audio_term iterm;
1664 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1665 if (! namelist[i]) {
1666 snd_printk(KERN_ERR "cannot malloc\n");
1673 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1675 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1676 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1678 sprintf(namelist[i], "Input %d", i);
1681 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1683 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1688 kctl->private_value = (unsigned long)namelist;
1689 kctl->private_free = usb_mixer_selector_elem_free;
1691 nameid = desc[desc[0] - 1];
1692 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1696 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1698 len = get_term_name(state, &state->oterm,
1699 kctl->id.name, sizeof(kctl->id.name), 0);
1701 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1703 if ((state->oterm.type & 0xff00) == 0x0100)
1704 append_ctl_name(kctl, " Capture Source");
1706 append_ctl_name(kctl, " Playback Source");
1709 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1710 cval->id, kctl->id.name, num_ins);
1711 if ((err = add_control_to_empty(state, kctl)) < 0)
1719 * parse an audio unit recursively
1722 static int parse_audio_unit(struct mixer_build *state, int unitid)
1726 if (test_and_set_bit(unitid, state->unitbitmap))
1727 return 0; /* the unit already visited */
1729 p1 = find_audio_control_unit(state, unitid);
1731 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1736 case INPUT_TERMINAL:
1739 return parse_audio_mixer_unit(state, unitid, p1);
1741 return parse_audio_selector_unit(state, unitid, p1);
1743 return parse_audio_feature_unit(state, unitid, p1);
1744 case PROCESSING_UNIT:
1745 return parse_audio_processing_unit(state, unitid, p1);
1746 case EXTENSION_UNIT:
1747 return parse_audio_extension_unit(state, unitid, p1);
1749 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1754 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1756 kfree(mixer->id_elems);
1758 kfree(mixer->urb->transfer_buffer);
1759 usb_free_urb(mixer->urb);
1761 usb_free_urb(mixer->rc_urb);
1762 kfree(mixer->rc_setup_packet);
1766 static int snd_usb_mixer_dev_free(struct snd_device *device)
1768 struct usb_mixer_interface *mixer = device->device_data;
1769 snd_usb_mixer_free(mixer);
1774 * create mixer controls
1776 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1778 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1780 unsigned char *desc;
1781 struct mixer_build state;
1783 const struct usbmix_ctl_map *map;
1784 struct usb_host_interface *hostif;
1786 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1787 memset(&state, 0, sizeof(state));
1788 state.chip = mixer->chip;
1789 state.mixer = mixer;
1790 state.buffer = hostif->extra;
1791 state.buflen = hostif->extralen;
1793 /* check the mapping table */
1794 for (map = usbmix_ctl_maps; map->id; map++) {
1795 if (map->id == state.chip->usb_id) {
1796 state.map = map->map;
1797 state.selector_map = map->selector_map;
1798 mixer->ignore_ctl_error = map->ignore_ctl_error;
1804 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1806 continue; /* invalid descriptor? */
1807 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1808 state.oterm.id = desc[3];
1809 state.oterm.type = combine_word(&desc[4]);
1810 state.oterm.name = desc[8];
1811 err = parse_audio_unit(&state, desc[7]);
1818 static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1821 struct usb_mixer_elem_info *info;
1823 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1824 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1828 static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1833 /* unit ids specific to Extigy/Audigy 2 NX: */
1835 case 0: /* remote control */
1836 mixer->rc_urb->dev = mixer->chip->dev;
1837 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1839 case 4: /* digital in jack */
1840 case 7: /* line in jacks */
1841 case 19: /* speaker out jacks */
1842 case 20: /* headphones out jack */
1844 /* live24ext: 4 = line-in jack */
1845 case 3: /* hp-out jack (may actuate Mute) */
1846 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1847 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1848 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1851 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1856 static void snd_usb_mixer_status_complete(struct urb *urb)
1858 struct usb_mixer_interface *mixer = urb->context;
1860 if (urb->status == 0) {
1861 u8 *buf = urb->transfer_buffer;
1864 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1865 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1867 /* ignore any notifications not from the control interface */
1868 if ((buf[0] & 0x0f) != 0)
1870 if (!(buf[0] & 0x40))
1871 snd_usb_mixer_notify_id(mixer, buf[1]);
1873 snd_usb_mixer_memory_change(mixer, buf[1]);
1876 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1877 urb->dev = mixer->chip->dev;
1878 usb_submit_urb(urb, GFP_ATOMIC);
1882 /* create the handler for the optional status interrupt endpoint */
1883 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1885 struct usb_host_interface *hostif;
1886 struct usb_endpoint_descriptor *ep;
1887 void *transfer_buffer;
1891 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1892 /* we need one interrupt input endpoint */
1893 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1895 ep = get_endpoint(hostif, 0);
1896 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
1899 epnum = usb_endpoint_num(ep);
1900 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1901 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1902 if (!transfer_buffer)
1904 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1906 kfree(transfer_buffer);
1909 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1910 usb_rcvintpipe(mixer->chip->dev, epnum),
1911 transfer_buffer, buffer_length,
1912 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1913 usb_submit_urb(mixer->urb, GFP_KERNEL);
1917 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
1919 struct usb_mixer_interface *mixer = urb->context;
1920 const struct rc_config *rc = mixer->rc_cfg;
1923 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
1926 code = mixer->rc_buffer[rc->offset];
1927 if (rc->length == 2)
1928 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1930 /* the Mute button actually changes the mixer control */
1931 if (code == rc->mute_code)
1932 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
1933 mixer->rc_code = code;
1935 wake_up(&mixer->rc_waitq);
1938 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
1939 long count, loff_t *offset)
1941 struct usb_mixer_interface *mixer = hw->private_data;
1945 if (count != 1 && count != 4)
1947 err = wait_event_interruptible(mixer->rc_waitq,
1948 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1951 err = put_user(rc_code, buf);
1953 err = put_user(rc_code, (u32 __user *)buf);
1955 return err < 0 ? err : count;
1958 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
1961 struct usb_mixer_interface *mixer = hw->private_data;
1963 poll_wait(file, &mixer->rc_waitq, wait);
1964 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1967 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1969 struct snd_hwdep *hwdep;
1972 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1973 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1975 if (i >= ARRAY_SIZE(rc_configs))
1977 mixer->rc_cfg = &rc_configs[i];
1979 len = mixer->rc_cfg->packet_length;
1981 init_waitqueue_head(&mixer->rc_waitq);
1982 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1985 snprintf(hwdep->name, sizeof(hwdep->name),
1986 "%s remote control", mixer->chip->card->shortname);
1987 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1988 hwdep->private_data = mixer;
1989 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1990 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1991 hwdep->exclusive = 1;
1993 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1996 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1997 if (!mixer->rc_setup_packet) {
1998 usb_free_urb(mixer->rc_urb);
1999 mixer->rc_urb = NULL;
2002 mixer->rc_setup_packet->bRequestType =
2003 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2004 mixer->rc_setup_packet->bRequest = GET_MEM;
2005 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
2006 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
2007 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
2008 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
2009 usb_rcvctrlpipe(mixer->chip->dev, 0),
2010 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
2011 snd_usb_soundblaster_remote_complete, mixer);
2015 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
2017 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2019 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2020 int index = kcontrol->private_value;
2022 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
2026 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2028 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2029 int index = kcontrol->private_value;
2030 int value = ucontrol->value.integer.value[0];
2035 changed = value != mixer->audigy2nx_leds[index];
2036 err = snd_usb_ctl_msg(mixer->chip->dev,
2037 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
2038 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2039 value, index + 2, NULL, 0, 100);
2042 mixer->audigy2nx_leds[index] = value;
2046 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
2048 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2049 .name = "CMSS LED Switch",
2050 .info = snd_audigy2nx_led_info,
2051 .get = snd_audigy2nx_led_get,
2052 .put = snd_audigy2nx_led_put,
2056 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2057 .name = "Power LED Switch",
2058 .info = snd_audigy2nx_led_info,
2059 .get = snd_audigy2nx_led_get,
2060 .put = snd_audigy2nx_led_put,
2064 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2065 .name = "Dolby Digital LED Switch",
2066 .info = snd_audigy2nx_led_info,
2067 .get = snd_audigy2nx_led_get,
2068 .put = snd_audigy2nx_led_put,
2073 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
2077 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
2078 if (i > 1 && /* Live24ext has 2 LEDs only */
2079 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2080 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
2082 err = snd_ctl_add(mixer->chip->card,
2083 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
2087 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
2091 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
2092 struct snd_info_buffer *buffer)
2094 static const struct sb_jack {
2097 } jacks_audigy2nx[] = {
2103 }, jacks_live24ext[] = {
2104 {4, "line in"}, /* &1=Line, &2=Mic*/
2105 {3, "hph out"}, /* headphones */
2106 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2109 const struct sb_jack *jacks;
2110 struct usb_mixer_interface *mixer = entry->private_data;
2114 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
2115 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2116 jacks = jacks_audigy2nx;
2117 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2118 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
2119 jacks = jacks_live24ext;
2123 for (i = 0; jacks[i].name; ++i) {
2124 snd_iprintf(buffer, "%s: ", jacks[i].name);
2125 err = snd_usb_ctl_msg(mixer->chip->dev,
2126 usb_rcvctrlpipe(mixer->chip->dev, 0),
2127 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2128 USB_RECIP_INTERFACE, 0,
2129 jacks[i].unitid << 8, buf, 3, 100);
2130 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
2131 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2133 snd_iprintf(buffer, "?\n");
2137 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
2138 struct snd_ctl_elem_value *ucontrol)
2140 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2142 ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
2146 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
2147 struct snd_ctl_elem_value *ucontrol)
2149 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2150 u8 old_status, new_status;
2153 old_status = mixer->xonar_u1_status;
2154 if (ucontrol->value.integer.value[0])
2155 new_status = old_status | 0x02;
2157 new_status = old_status & ~0x02;
2158 changed = new_status != old_status;
2159 err = snd_usb_ctl_msg(mixer->chip->dev,
2160 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
2161 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2162 50, 0, &new_status, 1, 100);
2165 mixer->xonar_u1_status = new_status;
2169 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
2170 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2171 .name = "Digital Playback Switch",
2172 .info = snd_ctl_boolean_mono_info,
2173 .get = snd_xonar_u1_switch_get,
2174 .put = snd_xonar_u1_switch_put,
2177 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
2181 err = snd_ctl_add(mixer->chip->card,
2182 snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
2185 mixer->xonar_u1_status = 0x05;
2189 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
2190 unsigned char samplerate_id)
2192 struct usb_mixer_interface *mixer;
2193 struct usb_mixer_elem_info *cval;
2194 int unitid = 12; /* SamleRate ExtensionUnit ID */
2196 list_for_each_entry(mixer, &chip->mixer_list, list) {
2197 cval = mixer->id_elems[unitid];
2199 set_cur_ctl_value(cval, cval->control << 8, samplerate_id);
2200 snd_usb_mixer_notify_id(mixer, unitid);
2206 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2209 static struct snd_device_ops dev_ops = {
2210 .dev_free = snd_usb_mixer_dev_free
2212 struct usb_mixer_interface *mixer;
2215 strcpy(chip->card->mixername, "USB Mixer");
2217 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
2221 mixer->ctrlif = ctrlif;
2222 mixer->ignore_ctl_error = ignore_error;
2223 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2224 if (!mixer->id_elems) {
2229 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
2230 (err = snd_usb_mixer_status_create(mixer)) < 0)
2233 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2236 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
2237 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2238 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
2239 struct snd_info_entry *entry;
2241 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2243 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
2244 snd_info_set_text_ops(entry, mixer,
2245 snd_audigy2nx_proc_read);
2248 if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
2249 mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
2250 err = snd_xonar_u1_controls_create(mixer);
2255 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
2258 list_add(&mixer->list, &chip->mixer_list);
2262 snd_usb_mixer_free(mixer);
2266 void snd_usb_mixer_disconnect(struct list_head *p)
2268 struct usb_mixer_interface *mixer;
2270 mixer = list_entry(p, struct usb_mixer_interface, list);
2271 usb_kill_urb(mixer->urb);
2272 usb_kill_urb(mixer->rc_urb);