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 <linux/usb/audio.h>
37 #include <sound/core.h>
38 #include <sound/control.h>
39 #include <sound/hwdep.h>
40 #include <sound/info.h>
41 #include <sound/tlv.h>
50 /* ignore error from controls - for debugging */
51 /* #define IGNORE_CTL_ERROR */
54 * Sound Blaster remote control configuration
56 * format of remote control data:
58 * Audigy 2 NX: 06 80 xx 00 00 00
59 * Live! 24-bit: 06 80 xx yy 22 83
61 static const struct rc_config {
66 u8 min_packet_length; /* minimum accepted length of the URB result */
70 { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */
71 { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */
72 { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */
73 { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
76 #define MAX_ID_ELEMS 256
78 struct usb_mixer_interface {
79 struct snd_usb_audio *chip;
81 struct list_head list;
82 unsigned int ignore_ctl_error;
84 /* array[MAX_ID_ELEMS], indexed by unit id */
85 struct usb_mixer_elem_info **id_elems;
87 /* Sound Blaster remote control stuff */
88 const struct rc_config *rc_cfg;
90 wait_queue_head_t rc_waitq;
92 struct usb_ctrlrequest *rc_setup_packet;
100 struct usb_audio_term {
104 unsigned int chconfig;
108 struct usbmix_name_map;
111 struct snd_usb_audio *chip;
112 struct usb_mixer_interface *mixer;
113 unsigned char *buffer;
115 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
116 struct usb_audio_term oterm;
117 const struct usbmix_name_map *map;
118 const struct usbmix_selector_map *selector_map;
121 #define MAX_CHANNELS 10 /* max logical channels */
123 struct usb_mixer_elem_info {
124 struct usb_mixer_interface *mixer;
125 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
126 struct snd_ctl_elem_id *elem_id;
128 unsigned int control; /* CS or ICN (high byte) */
129 unsigned int cmask; /* channel mask bitmap: 0 = master */
135 int cache_val[MAX_CHANNELS];
141 USB_MIXER_INV_BOOLEAN,
150 USB_PROC_UPDOWN_SWITCH = 1,
151 USB_PROC_UPDOWN_MODE_SEL = 2,
153 USB_PROC_PROLOGIC = 2,
154 USB_PROC_PROLOGIC_SWITCH = 1,
155 USB_PROC_PROLOGIC_MODE_SEL = 2,
158 USB_PROC_3DENH_SWITCH = 1,
159 USB_PROC_3DENH_SPACE = 2,
162 USB_PROC_REVERB_SWITCH = 1,
163 USB_PROC_REVERB_LEVEL = 2,
164 USB_PROC_REVERB_TIME = 3,
165 USB_PROC_REVERB_DELAY = 4,
168 USB_PROC_CHORUS_SWITCH = 1,
169 USB_PROC_CHORUS_LEVEL = 2,
170 USB_PROC_CHORUS_RATE = 3,
171 USB_PROC_CHORUS_DEPTH = 4,
174 USB_PROC_DCR_SWITCH = 1,
175 USB_PROC_DCR_RATIO = 2,
176 USB_PROC_DCR_MAX_AMP = 3,
177 USB_PROC_DCR_THRESHOLD = 4,
178 USB_PROC_DCR_ATTACK = 5,
179 USB_PROC_DCR_RELEASE = 6,
182 /*E-mu 0202(0404) eXtension Unit(XU) control*/
184 USB_XU_CLOCK_RATE = 0xe301,
185 USB_XU_CLOCK_SOURCE = 0xe302,
186 USB_XU_DIGITAL_IO_STATUS = 0xe303,
187 USB_XU_DEVICE_OPTIONS = 0xe304,
188 USB_XU_DIRECT_MONITORING = 0xe305,
189 USB_XU_METERING = 0xe306
192 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
193 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
194 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
195 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
199 * manual mapping of mixer names
200 * if the mixer topology is too complicated and the parsed names are
201 * ambiguous, add the entries in usbmixer_maps.c.
203 #include "usbmixer_maps.c"
205 static const struct usbmix_name_map *
206 find_map(struct mixer_build *state, int unitid, int control)
208 const struct usbmix_name_map *p = state->map;
213 for (p = state->map; p->id; p++) {
214 if (p->id == unitid &&
215 (!control || !p->control || control == p->control))
221 /* get the mapped name if the unit matches */
223 check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
229 return strlcpy(buf, p->name, buflen);
232 /* check whether the control should be ignored */
234 check_ignored_ctl(const struct usbmix_name_map *p)
236 if (!p || p->name || p->dB)
242 static inline void check_mapped_dB(const struct usbmix_name_map *p,
243 struct usb_mixer_elem_info *cval)
246 cval->dBmin = p->dB->min;
247 cval->dBmax = p->dB->max;
251 /* get the mapped selector source name */
252 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
253 int index, char *buf, int buflen)
255 const struct usbmix_selector_map *p;
257 if (! state->selector_map)
259 for (p = state->selector_map; p->id; p++) {
260 if (p->id == unitid && index < p->count)
261 return strlcpy(buf, p->names[index], buflen);
267 * find an audio control unit with the given unit id
269 static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
274 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
275 USB_DT_CS_INTERFACE)) != NULL) {
276 if (p[0] >= 4 && p[2] >= UAC_INPUT_TERMINAL && p[2] <= UAC_EXTENSION_UNIT_V1 && p[3] == unit)
284 * copy a string with the given id
286 static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
288 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
294 * convert from the byte/word on usb descriptor to the zero-based integer
296 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
298 switch (cval->val_type) {
299 case USB_MIXER_BOOLEAN:
301 case USB_MIXER_INV_BOOLEAN:
324 * convert from the zero-based int to the byte/word for usb descriptor
326 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
328 switch (cval->val_type) {
329 case USB_MIXER_BOOLEAN:
331 case USB_MIXER_INV_BOOLEAN:
340 return 0; /* not reached */
343 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
349 else if (val >= cval->max)
350 return (cval->max - cval->min + cval->res - 1) / cval->res;
352 return (val - cval->min) / cval->res;
355 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
370 * retrieve a mixer value
373 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
375 unsigned char buf[2];
376 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
379 while (timeout-- > 0) {
380 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
381 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
383 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
384 validx, cval->mixer->ctrlif | (cval->id << 8),
385 buf, val_len, 100) >= val_len) {
386 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
390 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
391 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
395 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
397 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
400 /* channel = 0: master, 1 = first channel */
401 static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
402 int channel, int *value)
404 return get_ctl_value(cval, UAC_GET_CUR, (cval->control << 8) | channel, value);
407 static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
408 int channel, int index, int *value)
412 if (cval->cached & (1 << channel)) {
413 *value = cval->cache_val[index];
416 err = get_cur_mix_raw(cval, channel, value);
418 if (!cval->mixer->ignore_ctl_error)
419 snd_printd(KERN_ERR "cannot get current value for "
420 "control %d ch %d: err = %d\n",
421 cval->control, channel, err);
424 cval->cached |= 1 << channel;
425 cval->cache_val[index] = *value;
434 static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
436 unsigned char buf[2];
437 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
440 value_set = convert_bytes_value(cval, value_set);
441 buf[0] = value_set & 0xff;
442 buf[1] = (value_set >> 8) & 0xff;
443 while (timeout-- > 0)
444 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
445 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
447 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
448 validx, cval->mixer->ctrlif | (cval->id << 8),
449 buf, val_len, 100) >= 0)
451 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
452 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
456 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
458 return set_ctl_value(cval, UAC_SET_CUR, validx, value);
461 static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
462 int index, int value)
465 err = set_ctl_value(cval, UAC_SET_CUR, (cval->control << 8) | channel,
469 cval->cached |= 1 << channel;
470 cval->cache_val[index] = value;
475 * TLV callback for mixer volume controls
477 static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
478 unsigned int size, unsigned int __user *_tlv)
480 struct usb_mixer_elem_info *cval = kcontrol->private_data;
481 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
483 if (size < sizeof(scale))
485 scale[2] = cval->dBmin;
486 scale[3] = cval->dBmax;
487 if (copy_to_user(_tlv, scale, sizeof(scale)))
493 * parser routines begin here...
496 static int parse_audio_unit(struct mixer_build *state, int unitid);
500 * check if the input/output channel routing is enabled on the given bitmap.
501 * used for mixer unit parser
503 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
505 int idx = ich * num_outs + och;
506 return bmap[idx >> 3] & (0x80 >> (idx & 7));
511 * add an alsa control element
512 * search and increment the index until an empty slot is found.
514 * if failed, give up and free the control instance.
517 static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
519 struct usb_mixer_elem_info *cval = kctl->private_data;
522 while (snd_ctl_find_id(state->chip->card, &kctl->id))
524 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
525 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
528 cval->elem_id = &kctl->id;
529 cval->next_id_elem = state->mixer->id_elems[cval->id];
530 state->mixer->id_elems[cval->id] = cval;
536 * get a terminal name string
539 static struct iterm_name_combo {
543 { 0x0300, "Output" },
544 { 0x0301, "Speaker" },
545 { 0x0302, "Headphone" },
546 { 0x0303, "HMD Audio" },
547 { 0x0304, "Desktop Speaker" },
548 { 0x0305, "Room Speaker" },
549 { 0x0306, "Com Speaker" },
551 { 0x0600, "External In" },
552 { 0x0601, "Analog In" },
553 { 0x0602, "Digital In" },
555 { 0x0604, "Legacy In" },
556 { 0x0605, "IEC958 In" },
557 { 0x0606, "1394 DA Stream" },
558 { 0x0607, "1394 DV Stream" },
559 { 0x0700, "Embedded" },
560 { 0x0701, "Noise Source" },
561 { 0x0702, "Equalization Noise" },
565 { 0x0706, "MiniDisk" },
566 { 0x0707, "Analog Tape" },
567 { 0x0708, "Phonograph" },
568 { 0x0709, "VCR Audio" },
569 { 0x070a, "Video Disk Audio" },
570 { 0x070b, "DVD Audio" },
571 { 0x070c, "TV Tuner Audio" },
572 { 0x070d, "Satellite Rec Audio" },
573 { 0x070e, "Cable Tuner Audio" },
574 { 0x070f, "DSS Audio" },
575 { 0x0710, "Radio Receiver" },
576 { 0x0711, "Radio Transmitter" },
577 { 0x0712, "Multi-Track Recorder" },
578 { 0x0713, "Synthesizer" },
582 static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
583 unsigned char *name, int maxlen, int term_only)
585 struct iterm_name_combo *names;
588 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
590 /* virtual type - not a real terminal */
591 if (iterm->type >> 16) {
594 switch (iterm->type >> 16) {
595 case UAC_SELECTOR_UNIT:
596 strcpy(name, "Selector"); return 8;
597 case UAC_PROCESSING_UNIT_V1:
598 strcpy(name, "Process Unit"); return 12;
599 case UAC_EXTENSION_UNIT_V1:
600 strcpy(name, "Ext Unit"); return 8;
602 strcpy(name, "Mixer"); return 5;
604 return sprintf(name, "Unit %d", iterm->id);
608 switch (iterm->type & 0xff00) {
610 strcpy(name, "PCM"); return 3;
612 strcpy(name, "Mic"); return 3;
614 strcpy(name, "Headset"); return 7;
616 strcpy(name, "Phone"); return 5;
619 for (names = iterm_names; names->type; names++)
620 if (names->type == iterm->type) {
621 strcpy(name, names->name);
622 return strlen(names->name);
629 * parse the source unit recursively until it reaches to a terminal
630 * or a branched unit.
632 static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
636 memset(term, 0, sizeof(*term));
637 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
640 case UAC_INPUT_TERMINAL:
641 term->type = combine_word(p1 + 4);
642 term->channels = p1[7];
643 term->chconfig = combine_word(p1 + 8);
646 case UAC_FEATURE_UNIT:
648 break; /* continue to parse */
650 term->type = p1[2] << 16; /* virtual type */
651 term->channels = p1[5 + p1[4]];
652 term->chconfig = combine_word(p1 + 6 + p1[4]);
653 term->name = p1[p1[0] - 1];
655 case UAC_SELECTOR_UNIT:
656 /* call recursively to retrieve the channel info */
657 if (check_input_term(state, p1[5], term) < 0)
659 term->type = p1[2] << 16; /* virtual type */
661 term->name = p1[9 + p1[0] - 1];
663 case UAC_PROCESSING_UNIT_V1:
664 case UAC_EXTENSION_UNIT_V1:
667 break; /* continue to parse */
669 term->type = p1[2] << 16; /* virtual type */
670 term->channels = p1[7 + p1[6]];
671 term->chconfig = combine_word(p1 + 8 + p1[6]);
672 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
686 /* feature unit control information */
687 struct usb_feature_control_info {
689 unsigned int type; /* control type (mute, volume, etc.) */
692 static struct usb_feature_control_info audio_feature_info[] = {
693 { "Mute", USB_MIXER_INV_BOOLEAN },
694 { "Volume", USB_MIXER_S16 },
695 { "Tone Control - Bass", USB_MIXER_S8 },
696 { "Tone Control - Mid", USB_MIXER_S8 },
697 { "Tone Control - Treble", USB_MIXER_S8 },
698 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
699 { "Auto Gain Control", USB_MIXER_BOOLEAN },
700 { "Delay Control", USB_MIXER_U16 },
701 { "Bass Boost", USB_MIXER_BOOLEAN },
702 { "Loudness", USB_MIXER_BOOLEAN },
706 /* private_free callback */
707 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
709 kfree(kctl->private_data);
710 kctl->private_data = NULL;
715 * interface to ALSA control for feature/mixer units
719 * retrieve the minimum and maximum values for the specified control
721 static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
724 cval->min = default_min;
725 cval->max = cval->min + 1;
727 cval->dBmin = cval->dBmax = 0;
729 if (cval->val_type == USB_MIXER_BOOLEAN ||
730 cval->val_type == USB_MIXER_INV_BOOLEAN) {
731 cval->initialized = 1;
736 for (i = 0; i < MAX_CHANNELS; i++)
737 if (cval->cmask & (1 << i)) {
742 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
743 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
744 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
745 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
748 if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
751 int last_valid_res = cval->res;
753 while (cval->res > 1) {
754 if (set_ctl_value(cval, UAC_SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
758 if (get_ctl_value(cval, UAC_GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
759 cval->res = last_valid_res;
764 /* Additional checks for the proper resolution
766 * Some devices report smaller resolutions than actually
767 * reacting. They don't return errors but simply clip
768 * to the lower aligned value.
770 if (cval->min + cval->res < cval->max) {
771 int last_valid_res = cval->res;
772 int saved, test, check;
773 get_cur_mix_raw(cval, minchn, &saved);
776 if (test < cval->max)
780 if (test < cval->min || test > cval->max ||
781 set_cur_mix_value(cval, minchn, 0, test) ||
782 get_cur_mix_raw(cval, minchn, &check)) {
783 cval->res = last_valid_res;
790 set_cur_mix_value(cval, minchn, 0, saved);
793 cval->initialized = 1;
796 /* USB descriptions contain the dB scale in 1/256 dB unit
797 * while ALSA TLV contains in 1/100 dB unit
799 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
800 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
801 if (cval->dBmin > cval->dBmax) {
802 /* something is wrong; assume it's either from/to 0dB */
805 else if (cval->dBmin > 0)
807 if (cval->dBmin > cval->dBmax) {
808 /* totally crap, return an error */
817 /* get a feature/mixer unit info */
818 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
820 struct usb_mixer_elem_info *cval = kcontrol->private_data;
822 if (cval->val_type == USB_MIXER_BOOLEAN ||
823 cval->val_type == USB_MIXER_INV_BOOLEAN)
824 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
826 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
827 uinfo->count = cval->channels;
828 if (cval->val_type == USB_MIXER_BOOLEAN ||
829 cval->val_type == USB_MIXER_INV_BOOLEAN) {
830 uinfo->value.integer.min = 0;
831 uinfo->value.integer.max = 1;
833 if (! cval->initialized)
834 get_min_max(cval, 0);
835 uinfo->value.integer.min = 0;
836 uinfo->value.integer.max =
837 (cval->max - cval->min + cval->res - 1) / cval->res;
842 /* get the current value from feature/mixer unit */
843 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
845 struct usb_mixer_elem_info *cval = kcontrol->private_data;
846 int c, cnt, val, err;
848 ucontrol->value.integer.value[0] = cval->min;
851 for (c = 0; c < MAX_CHANNELS; c++) {
852 if (!(cval->cmask & (1 << c)))
854 err = get_cur_mix_value(cval, c + 1, cnt, &val);
856 return cval->mixer->ignore_ctl_error ? 0 : err;
857 val = get_relative_value(cval, val);
858 ucontrol->value.integer.value[cnt] = val;
864 err = get_cur_mix_value(cval, 0, 0, &val);
866 return cval->mixer->ignore_ctl_error ? 0 : err;
867 val = get_relative_value(cval, val);
868 ucontrol->value.integer.value[0] = val;
873 /* put the current value to feature/mixer unit */
874 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
876 struct usb_mixer_elem_info *cval = kcontrol->private_data;
877 int c, cnt, val, oval, err;
882 for (c = 0; c < MAX_CHANNELS; c++) {
883 if (!(cval->cmask & (1 << c)))
885 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
887 return cval->mixer->ignore_ctl_error ? 0 : err;
888 val = ucontrol->value.integer.value[cnt];
889 val = get_abs_value(cval, val);
891 set_cur_mix_value(cval, c + 1, cnt, val);
898 err = get_cur_mix_value(cval, 0, 0, &oval);
900 return cval->mixer->ignore_ctl_error ? 0 : err;
901 val = ucontrol->value.integer.value[0];
902 val = get_abs_value(cval, val);
904 set_cur_mix_value(cval, 0, 0, val);
911 static struct snd_kcontrol_new usb_feature_unit_ctl = {
912 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
913 .name = "", /* will be filled later manually */
914 .info = mixer_ctl_feature_info,
915 .get = mixer_ctl_feature_get,
916 .put = mixer_ctl_feature_put,
921 * build a feature control
924 static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
926 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
929 static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
930 unsigned int ctl_mask, int control,
931 struct usb_audio_term *iterm, int unitid)
933 unsigned int len = 0;
935 int nameid = desc[desc[0] - 1];
936 struct snd_kcontrol *kctl;
937 struct usb_mixer_elem_info *cval;
938 const struct usbmix_name_map *map;
940 control++; /* change from zero-based to 1-based value */
942 if (control == UAC_GRAPHIC_EQUALIZER_CONTROL) {
943 /* FIXME: not supported yet */
947 map = find_map(state, unitid, control);
948 if (check_ignored_ctl(map))
951 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
953 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
956 cval->mixer = state->mixer;
958 cval->control = control;
959 cval->cmask = ctl_mask;
960 cval->val_type = audio_feature_info[control-1].type;
962 cval->channels = 1; /* master channel */
965 for (i = 0; i < 16; i++)
966 if (ctl_mask & (1 << i))
971 /* get min/max values */
972 get_min_max(cval, 0);
974 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
976 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
980 kctl->private_free = usb_mixer_elem_free;
982 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
983 mapped_name = len != 0;
985 len = snd_usb_copy_string_desc(state, nameid,
986 kctl->id.name, sizeof(kctl->id.name));
989 case UAC_MUTE_CONTROL:
990 case UAC_VOLUME_CONTROL:
991 /* determine the control name. the rule is:
992 * - if a name id is given in descriptor, use it.
993 * - if the connected input can be determined, then use the name
995 * - if the connected output can be determined, use it.
996 * - otherwise, anonymous name.
999 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
1001 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
1003 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
1004 "Feature %d", unitid);
1006 /* determine the stream direction:
1007 * if the connected output is USB stream, then it's likely a
1008 * capture stream. otherwise it should be playback (hopefully :)
1010 if (! mapped_name && ! (state->oterm.type >> 16)) {
1011 if ((state->oterm.type & 0xff00) == 0x0100) {
1012 len = append_ctl_name(kctl, " Capture");
1014 len = append_ctl_name(kctl, " Playback");
1017 append_ctl_name(kctl, control == UAC_MUTE_CONTROL ?
1018 " Switch" : " Volume");
1019 if (control == UAC_VOLUME_CONTROL) {
1020 kctl->tlv.c = mixer_vol_tlv;
1021 kctl->vd[0].access |=
1022 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1023 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1024 check_mapped_dB(map, cval);
1030 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1031 sizeof(kctl->id.name));
1035 /* volume control quirks */
1036 switch (state->chip->usb_id) {
1037 case USB_ID(0x0471, 0x0101):
1038 case USB_ID(0x0471, 0x0104):
1039 case USB_ID(0x0471, 0x0105):
1040 case USB_ID(0x0672, 0x1041):
1041 /* quirk for UDA1321/N101.
1042 * note that detection between firmware 2.1.1.7 (N101)
1043 * and later 2.1.1.21 is not very clear from datasheets.
1044 * I hope that the min value is -15360 for newer firmware --jk
1046 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1047 cval->min == -15616) {
1048 snd_printk(KERN_INFO
1049 "set volume quirk for UDA1321/N101 chip\n");
1054 case USB_ID(0x046d, 0x09a4):
1055 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1056 snd_printk(KERN_INFO
1057 "set volume quirk for QuickCam E3500\n");
1066 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1067 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
1068 add_control_to_empty(state, kctl);
1074 * parse a feature unit
1076 * most of controlls are defined here.
1078 static int parse_audio_feature_unit(struct mixer_build *state, int unitid, void *_ftr)
1081 struct usb_audio_term iterm;
1082 unsigned int master_bits, first_ch_bits;
1084 struct uac_feature_unit_descriptor *ftr = _ftr;
1086 if (ftr->bLength < 7 || ! (csize = ftr->bControlSize) || ftr->bLength < 7 + csize) {
1087 snd_printk(KERN_ERR "usbaudio: unit %u: invalid UAC_FEATURE_UNIT descriptor\n", unitid);
1091 /* parse the source unit */
1092 if ((err = parse_audio_unit(state, ftr->bSourceID)) < 0)
1095 /* determine the input source type and name */
1096 if (check_input_term(state, ftr->bSourceID, &iterm) < 0)
1099 channels = (ftr->bLength - 7) / csize - 1;
1101 master_bits = snd_usb_combine_bytes(ftr->controls, csize);
1102 /* master configuration quirks */
1103 switch (state->chip->usb_id) {
1104 case USB_ID(0x08bb, 0x2702):
1105 snd_printk(KERN_INFO
1106 "usbmixer: master volume quirk for PCM2702 chip\n");
1107 /* disable non-functional volume control */
1108 master_bits &= ~UAC_FU_VOLUME;
1112 first_ch_bits = snd_usb_combine_bytes(ftr->controls + csize, csize);
1115 /* check all control types */
1116 for (i = 0; i < 10; i++) {
1117 unsigned int ch_bits = 0;
1118 for (j = 0; j < channels; j++) {
1119 unsigned int mask = snd_usb_combine_bytes(ftr->controls + csize * (j+1), csize);
1120 if (mask & (1 << i))
1121 ch_bits |= (1 << j);
1123 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1124 build_feature_ctl(state, _ftr, ch_bits, i, &iterm, unitid);
1125 if (master_bits & (1 << i))
1126 build_feature_ctl(state, _ftr, 0, i, &iterm, unitid);
1138 * build a mixer unit control
1140 * the callbacks are identical with feature unit.
1141 * input channel number (zero based) is given in control field instead.
1144 static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
1145 int in_pin, int in_ch, int unitid,
1146 struct usb_audio_term *iterm)
1148 struct usb_mixer_elem_info *cval;
1149 unsigned int input_pins = desc[4];
1150 unsigned int num_outs = desc[5 + input_pins];
1151 unsigned int i, len;
1152 struct snd_kcontrol *kctl;
1153 const struct usbmix_name_map *map;
1155 map = find_map(state, unitid, 0);
1156 if (check_ignored_ctl(map))
1159 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1163 cval->mixer = state->mixer;
1165 cval->control = in_ch + 1; /* based on 1 */
1166 cval->val_type = USB_MIXER_S16;
1167 for (i = 0; i < num_outs; i++) {
1168 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1169 cval->cmask |= (1 << i);
1174 /* get min/max values */
1175 get_min_max(cval, 0);
1177 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1179 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1183 kctl->private_free = usb_mixer_elem_free;
1185 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1187 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1189 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1190 append_ctl_name(kctl, " Volume");
1192 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1193 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1194 add_control_to_empty(state, kctl);
1199 * parse a mixer unit
1201 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1203 struct usb_audio_term iterm;
1204 int input_pins, num_ins, num_outs;
1207 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1208 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1211 /* no bmControls field (e.g. Maya44) -> ignore */
1212 if (desc[0] <= 10 + input_pins) {
1213 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1219 for (pin = 0; pin < input_pins; pin++) {
1220 err = parse_audio_unit(state, desc[5 + pin]);
1223 err = check_input_term(state, desc[5 + pin], &iterm);
1226 num_ins += iterm.channels;
1227 for (; ich < num_ins; ++ich) {
1228 int och, ich_has_controls = 0;
1230 for (och = 0; och < num_outs; ++och) {
1231 if (check_matrix_bitmap(desc + 9 + input_pins,
1232 ich, och, num_outs)) {
1233 ich_has_controls = 1;
1237 if (ich_has_controls)
1238 build_mixer_unit_ctl(state, desc, pin, ich,
1247 * Processing Unit / Extension Unit
1250 /* get callback for processing/extension unit */
1251 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1253 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1256 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1257 if (err < 0 && cval->mixer->ignore_ctl_error) {
1258 ucontrol->value.integer.value[0] = cval->min;
1263 val = get_relative_value(cval, val);
1264 ucontrol->value.integer.value[0] = val;
1268 /* put callback for processing/extension unit */
1269 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1271 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1274 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1276 if (cval->mixer->ignore_ctl_error)
1280 val = ucontrol->value.integer.value[0];
1281 val = get_abs_value(cval, val);
1283 set_cur_ctl_value(cval, cval->control << 8, val);
1289 /* alsa control interface for processing/extension unit */
1290 static struct snd_kcontrol_new mixer_procunit_ctl = {
1291 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1292 .name = "", /* will be filled later */
1293 .info = mixer_ctl_feature_info,
1294 .get = mixer_ctl_procunit_get,
1295 .put = mixer_ctl_procunit_put,
1300 * predefined data for processing units
1302 struct procunit_value_info {
1309 struct procunit_info {
1312 struct procunit_value_info *values;
1315 static struct procunit_value_info updown_proc_info[] = {
1316 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1317 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1320 static struct procunit_value_info prologic_proc_info[] = {
1321 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1322 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1325 static struct procunit_value_info threed_enh_proc_info[] = {
1326 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1327 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1330 static struct procunit_value_info reverb_proc_info[] = {
1331 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1332 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1333 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1334 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1337 static struct procunit_value_info chorus_proc_info[] = {
1338 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1339 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1340 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1341 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1344 static struct procunit_value_info dcr_proc_info[] = {
1345 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1346 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1347 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1348 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1349 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1350 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1354 static struct procunit_info procunits[] = {
1355 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1356 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1357 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1358 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1359 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1360 { USB_PROC_DCR, "DCR", dcr_proc_info },
1364 * predefined data for extension units
1366 static struct procunit_value_info clock_rate_xu_info[] = {
1367 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
1370 static struct procunit_value_info clock_source_xu_info[] = {
1371 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
1374 static struct procunit_value_info spdif_format_xu_info[] = {
1375 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
1378 static struct procunit_value_info soft_limit_xu_info[] = {
1379 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
1382 static struct procunit_info extunits[] = {
1383 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
1384 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
1385 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
1386 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
1390 * build a processing/extension unit
1392 static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1394 int num_ins = dsc[6];
1395 struct usb_mixer_elem_info *cval;
1396 struct snd_kcontrol *kctl;
1397 int i, err, nameid, type, len;
1398 struct procunit_info *info;
1399 struct procunit_value_info *valinfo;
1400 const struct usbmix_name_map *map;
1401 static struct procunit_value_info default_value_info[] = {
1402 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1405 static struct procunit_info default_info = {
1406 0, NULL, default_value_info
1409 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1410 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1414 for (i = 0; i < num_ins; i++) {
1415 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1419 type = combine_word(&dsc[4]);
1420 for (info = list; info && info->type; info++)
1421 if (info->type == type)
1423 if (! info || ! info->type)
1424 info = &default_info;
1426 for (valinfo = info->values; valinfo->control; valinfo++) {
1427 /* FIXME: bitmap might be longer than 8bit */
1428 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1430 map = find_map(state, unitid, valinfo->control);
1431 if (check_ignored_ctl(map))
1433 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1435 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1438 cval->mixer = state->mixer;
1440 cval->control = valinfo->control;
1441 cval->val_type = valinfo->val_type;
1444 /* get min/max values */
1445 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1446 /* FIXME: hard-coded */
1448 cval->max = dsc[15];
1450 cval->initialized = 1;
1452 if (type == USB_XU_CLOCK_RATE) {
1453 /* E-Mu USB 0404/0202/TrackerPre
1454 * samplerate control quirk
1459 cval->initialized = 1;
1461 get_min_max(cval, valinfo->min_value);
1464 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1466 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1470 kctl->private_free = usb_mixer_elem_free;
1472 if (check_mapped_name(map, kctl->id.name,
1473 sizeof(kctl->id.name)))
1475 else if (info->name)
1476 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1478 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1481 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1483 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1485 append_ctl_name(kctl, " ");
1486 append_ctl_name(kctl, valinfo->suffix);
1488 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1489 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1490 if ((err = add_control_to_empty(state, kctl)) < 0)
1497 static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1499 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1502 static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1504 return build_audio_procunit(state, unitid, desc, extunits, "Extension Unit");
1512 /* info callback for selector unit
1513 * use an enumerator type for routing
1515 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1517 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1518 char **itemlist = (char **)kcontrol->private_value;
1520 if (snd_BUG_ON(!itemlist))
1522 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1524 uinfo->value.enumerated.items = cval->max;
1525 if ((int)uinfo->value.enumerated.item >= cval->max)
1526 uinfo->value.enumerated.item = cval->max - 1;
1527 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1531 /* get callback for selector unit */
1532 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1534 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1537 err = get_cur_ctl_value(cval, 0, &val);
1539 if (cval->mixer->ignore_ctl_error) {
1540 ucontrol->value.enumerated.item[0] = 0;
1545 val = get_relative_value(cval, val);
1546 ucontrol->value.enumerated.item[0] = val;
1550 /* put callback for selector unit */
1551 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1553 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1556 err = get_cur_ctl_value(cval, 0, &oval);
1558 if (cval->mixer->ignore_ctl_error)
1562 val = ucontrol->value.enumerated.item[0];
1563 val = get_abs_value(cval, val);
1565 set_cur_ctl_value(cval, 0, val);
1571 /* alsa control interface for selector unit */
1572 static struct snd_kcontrol_new mixer_selectunit_ctl = {
1573 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1574 .name = "", /* will be filled later */
1575 .info = mixer_ctl_selector_info,
1576 .get = mixer_ctl_selector_get,
1577 .put = mixer_ctl_selector_put,
1581 /* private free callback.
1582 * free both private_data and private_value
1584 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
1588 if (kctl->private_data) {
1589 struct usb_mixer_elem_info *cval = kctl->private_data;
1590 num_ins = cval->max;
1592 kctl->private_data = NULL;
1594 if (kctl->private_value) {
1595 char **itemlist = (char **)kctl->private_value;
1596 for (i = 0; i < num_ins; i++)
1599 kctl->private_value = 0;
1604 * parse a selector unit
1606 static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1608 unsigned int num_ins = desc[4];
1609 unsigned int i, nameid, len;
1611 struct usb_mixer_elem_info *cval;
1612 struct snd_kcontrol *kctl;
1613 const struct usbmix_name_map *map;
1616 if (! num_ins || desc[0] < 5 + num_ins) {
1617 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1621 for (i = 0; i < num_ins; i++) {
1622 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1626 if (num_ins == 1) /* only one ? nonsense! */
1629 map = find_map(state, unitid, 0);
1630 if (check_ignored_ctl(map))
1633 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1635 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1638 cval->mixer = state->mixer;
1640 cval->val_type = USB_MIXER_U8;
1643 cval->max = num_ins;
1645 cval->initialized = 1;
1647 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1649 snd_printk(KERN_ERR "cannot malloc\n");
1653 #define MAX_ITEM_NAME_LEN 64
1654 for (i = 0; i < num_ins; i++) {
1655 struct usb_audio_term iterm;
1657 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1658 if (! namelist[i]) {
1659 snd_printk(KERN_ERR "cannot malloc\n");
1666 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1668 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1669 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1671 sprintf(namelist[i], "Input %d", i);
1674 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1676 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1681 kctl->private_value = (unsigned long)namelist;
1682 kctl->private_free = usb_mixer_selector_elem_free;
1684 nameid = desc[desc[0] - 1];
1685 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1689 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1691 len = get_term_name(state, &state->oterm,
1692 kctl->id.name, sizeof(kctl->id.name), 0);
1694 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1696 if ((state->oterm.type & 0xff00) == 0x0100)
1697 append_ctl_name(kctl, " Capture Source");
1699 append_ctl_name(kctl, " Playback Source");
1702 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1703 cval->id, kctl->id.name, num_ins);
1704 if ((err = add_control_to_empty(state, kctl)) < 0)
1712 * parse an audio unit recursively
1715 static int parse_audio_unit(struct mixer_build *state, int unitid)
1719 if (test_and_set_bit(unitid, state->unitbitmap))
1720 return 0; /* the unit already visited */
1722 p1 = find_audio_control_unit(state, unitid);
1724 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1729 case UAC_INPUT_TERMINAL:
1731 case UAC_MIXER_UNIT:
1732 return parse_audio_mixer_unit(state, unitid, p1);
1733 case UAC_SELECTOR_UNIT:
1734 return parse_audio_selector_unit(state, unitid, p1);
1735 case UAC_FEATURE_UNIT:
1736 return parse_audio_feature_unit(state, unitid, p1);
1737 case UAC_PROCESSING_UNIT_V1:
1738 return parse_audio_processing_unit(state, unitid, p1);
1739 case UAC_EXTENSION_UNIT_V1:
1740 return parse_audio_extension_unit(state, unitid, p1);
1742 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1747 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1749 kfree(mixer->id_elems);
1751 kfree(mixer->urb->transfer_buffer);
1752 usb_free_urb(mixer->urb);
1754 usb_free_urb(mixer->rc_urb);
1755 kfree(mixer->rc_setup_packet);
1759 static int snd_usb_mixer_dev_free(struct snd_device *device)
1761 struct usb_mixer_interface *mixer = device->device_data;
1762 snd_usb_mixer_free(mixer);
1767 * create mixer controls
1769 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
1771 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1773 struct uac_output_terminal_descriptor_v1 *desc;
1774 struct mixer_build state;
1776 const struct usbmix_ctl_map *map;
1777 struct usb_host_interface *hostif;
1779 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1780 memset(&state, 0, sizeof(state));
1781 state.chip = mixer->chip;
1782 state.mixer = mixer;
1783 state.buffer = hostif->extra;
1784 state.buflen = hostif->extralen;
1786 /* check the mapping table */
1787 for (map = usbmix_ctl_maps; map->id; map++) {
1788 if (map->id == state.chip->usb_id) {
1789 state.map = map->map;
1790 state.selector_map = map->selector_map;
1791 mixer->ignore_ctl_error = map->ignore_ctl_error;
1797 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, UAC_OUTPUT_TERMINAL)) != NULL) {
1798 if (desc->bLength < 9)
1799 continue; /* invalid descriptor? */
1800 set_bit(desc->bTerminalID, state.unitbitmap); /* mark terminal ID as visited */
1801 state.oterm.id = desc->bTerminalID;
1802 state.oterm.type = le16_to_cpu(desc->wTerminalType);
1803 state.oterm.name = desc->iTerminal;
1804 err = parse_audio_unit(&state, desc->bSourceID);
1811 static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1814 struct usb_mixer_elem_info *info;
1816 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1817 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1821 static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
1823 struct usb_mixer_elem_info *cval)
1825 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
1826 "S8", "U8", "S16", "U16"};
1827 snd_iprintf(buffer, " Unit: %i\n", unitid);
1829 snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n",
1830 cval->elem_id->name, cval->elem_id->index);
1831 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
1832 "channels=%i, type=\"%s\"\n", cval->id,
1833 cval->control, cval->cmask, cval->channels,
1834 val_types[cval->val_type]);
1835 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
1836 cval->min, cval->max, cval->dBmin, cval->dBmax);
1839 static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
1840 struct snd_info_buffer *buffer)
1842 struct snd_usb_audio *chip = entry->private_data;
1843 struct usb_mixer_interface *mixer;
1844 struct usb_mixer_elem_info *cval;
1847 list_for_each_entry(mixer, &chip->mixer_list, list) {
1849 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
1850 chip->usb_id, mixer->ctrlif,
1851 mixer->ignore_ctl_error);
1852 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
1853 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
1854 for (cval = mixer->id_elems[unitid]; cval;
1855 cval = cval->next_id_elem)
1856 snd_usb_mixer_dump_cval(buffer, unitid, cval);
1861 static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1866 /* unit ids specific to Extigy/Audigy 2 NX: */
1868 case 0: /* remote control */
1869 mixer->rc_urb->dev = mixer->chip->dev;
1870 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1872 case 4: /* digital in jack */
1873 case 7: /* line in jacks */
1874 case 19: /* speaker out jacks */
1875 case 20: /* headphones out jack */
1877 /* live24ext: 4 = line-in jack */
1878 case 3: /* hp-out jack (may actuate Mute) */
1879 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1880 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1881 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1884 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1889 static void snd_usb_mixer_status_complete(struct urb *urb)
1891 struct usb_mixer_interface *mixer = urb->context;
1893 if (urb->status == 0) {
1894 u8 *buf = urb->transfer_buffer;
1897 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1898 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1900 /* ignore any notifications not from the control interface */
1901 if ((buf[0] & 0x0f) != 0)
1903 if (!(buf[0] & 0x40))
1904 snd_usb_mixer_notify_id(mixer, buf[1]);
1906 snd_usb_mixer_memory_change(mixer, buf[1]);
1909 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1910 urb->dev = mixer->chip->dev;
1911 usb_submit_urb(urb, GFP_ATOMIC);
1915 /* create the handler for the optional status interrupt endpoint */
1916 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1918 struct usb_host_interface *hostif;
1919 struct usb_endpoint_descriptor *ep;
1920 void *transfer_buffer;
1924 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1925 /* we need one interrupt input endpoint */
1926 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1928 ep = get_endpoint(hostif, 0);
1929 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
1932 epnum = usb_endpoint_num(ep);
1933 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1934 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1935 if (!transfer_buffer)
1937 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1939 kfree(transfer_buffer);
1942 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1943 usb_rcvintpipe(mixer->chip->dev, epnum),
1944 transfer_buffer, buffer_length,
1945 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1946 usb_submit_urb(mixer->urb, GFP_KERNEL);
1950 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
1952 struct usb_mixer_interface *mixer = urb->context;
1953 const struct rc_config *rc = mixer->rc_cfg;
1956 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
1959 code = mixer->rc_buffer[rc->offset];
1960 if (rc->length == 2)
1961 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1963 /* the Mute button actually changes the mixer control */
1964 if (code == rc->mute_code)
1965 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
1966 mixer->rc_code = code;
1968 wake_up(&mixer->rc_waitq);
1971 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
1972 long count, loff_t *offset)
1974 struct usb_mixer_interface *mixer = hw->private_data;
1978 if (count != 1 && count != 4)
1980 err = wait_event_interruptible(mixer->rc_waitq,
1981 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1984 err = put_user(rc_code, buf);
1986 err = put_user(rc_code, (u32 __user *)buf);
1988 return err < 0 ? err : count;
1991 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
1994 struct usb_mixer_interface *mixer = hw->private_data;
1996 poll_wait(file, &mixer->rc_waitq, wait);
1997 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
2000 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
2002 struct snd_hwdep *hwdep;
2005 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
2006 if (rc_configs[i].usb_id == mixer->chip->usb_id)
2008 if (i >= ARRAY_SIZE(rc_configs))
2010 mixer->rc_cfg = &rc_configs[i];
2012 len = mixer->rc_cfg->packet_length;
2014 init_waitqueue_head(&mixer->rc_waitq);
2015 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
2018 snprintf(hwdep->name, sizeof(hwdep->name),
2019 "%s remote control", mixer->chip->card->shortname);
2020 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
2021 hwdep->private_data = mixer;
2022 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
2023 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
2024 hwdep->exclusive = 1;
2026 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
2029 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
2030 if (!mixer->rc_setup_packet) {
2031 usb_free_urb(mixer->rc_urb);
2032 mixer->rc_urb = NULL;
2035 mixer->rc_setup_packet->bRequestType =
2036 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2037 mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
2038 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
2039 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
2040 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
2041 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
2042 usb_rcvctrlpipe(mixer->chip->dev, 0),
2043 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
2044 snd_usb_soundblaster_remote_complete, mixer);
2048 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
2050 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2052 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2053 int index = kcontrol->private_value;
2055 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
2059 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2061 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2062 int index = kcontrol->private_value;
2063 int value = ucontrol->value.integer.value[0];
2068 changed = value != mixer->audigy2nx_leds[index];
2069 err = snd_usb_ctl_msg(mixer->chip->dev,
2070 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
2071 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2072 value, index + 2, NULL, 0, 100);
2075 mixer->audigy2nx_leds[index] = value;
2079 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
2081 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2082 .name = "CMSS LED Switch",
2083 .info = snd_audigy2nx_led_info,
2084 .get = snd_audigy2nx_led_get,
2085 .put = snd_audigy2nx_led_put,
2089 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2090 .name = "Power LED Switch",
2091 .info = snd_audigy2nx_led_info,
2092 .get = snd_audigy2nx_led_get,
2093 .put = snd_audigy2nx_led_put,
2097 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2098 .name = "Dolby Digital LED Switch",
2099 .info = snd_audigy2nx_led_info,
2100 .get = snd_audigy2nx_led_get,
2101 .put = snd_audigy2nx_led_put,
2106 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
2110 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
2111 if (i > 1 && /* Live24ext has 2 LEDs only */
2112 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2113 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
2115 err = snd_ctl_add(mixer->chip->card,
2116 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
2120 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
2124 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
2125 struct snd_info_buffer *buffer)
2127 static const struct sb_jack {
2130 } jacks_audigy2nx[] = {
2136 }, jacks_live24ext[] = {
2137 {4, "line in"}, /* &1=Line, &2=Mic*/
2138 {3, "hph out"}, /* headphones */
2139 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2142 const struct sb_jack *jacks;
2143 struct usb_mixer_interface *mixer = entry->private_data;
2147 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
2148 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2149 jacks = jacks_audigy2nx;
2150 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2151 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
2152 jacks = jacks_live24ext;
2156 for (i = 0; jacks[i].name; ++i) {
2157 snd_iprintf(buffer, "%s: ", jacks[i].name);
2158 err = snd_usb_ctl_msg(mixer->chip->dev,
2159 usb_rcvctrlpipe(mixer->chip->dev, 0),
2160 UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2161 USB_RECIP_INTERFACE, 0,
2162 jacks[i].unitid << 8, buf, 3, 100);
2163 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
2164 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2166 snd_iprintf(buffer, "?\n");
2170 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
2171 struct snd_ctl_elem_value *ucontrol)
2173 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2175 ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
2179 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
2180 struct snd_ctl_elem_value *ucontrol)
2182 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2183 u8 old_status, new_status;
2186 old_status = mixer->xonar_u1_status;
2187 if (ucontrol->value.integer.value[0])
2188 new_status = old_status | 0x02;
2190 new_status = old_status & ~0x02;
2191 changed = new_status != old_status;
2192 err = snd_usb_ctl_msg(mixer->chip->dev,
2193 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
2194 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2195 50, 0, &new_status, 1, 100);
2198 mixer->xonar_u1_status = new_status;
2202 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
2203 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2204 .name = "Digital Playback Switch",
2205 .info = snd_ctl_boolean_mono_info,
2206 .get = snd_xonar_u1_switch_get,
2207 .put = snd_xonar_u1_switch_put,
2210 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
2214 err = snd_ctl_add(mixer->chip->card,
2215 snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
2218 mixer->xonar_u1_status = 0x05;
2222 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
2223 unsigned char samplerate_id)
2225 struct usb_mixer_interface *mixer;
2226 struct usb_mixer_elem_info *cval;
2227 int unitid = 12; /* SamleRate ExtensionUnit ID */
2229 list_for_each_entry(mixer, &chip->mixer_list, list) {
2230 cval = mixer->id_elems[unitid];
2232 set_cur_ctl_value(cval, cval->control << 8,
2234 snd_usb_mixer_notify_id(mixer, unitid);
2240 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2243 static struct snd_device_ops dev_ops = {
2244 .dev_free = snd_usb_mixer_dev_free
2246 struct usb_mixer_interface *mixer;
2247 struct snd_info_entry *entry;
2248 struct usb_host_interface *host_iface;
2251 strcpy(chip->card->mixername, "USB Mixer");
2253 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
2257 mixer->ctrlif = ctrlif;
2258 mixer->ignore_ctl_error = ignore_error;
2259 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
2261 if (!mixer->id_elems) {
2266 host_iface = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
2267 protocol = host_iface->desc.bInterfaceProtocol;
2270 if (protocol != UAC_VERSION_1) {
2271 snd_printk(KERN_WARNING "mixer interface protocol 0x%02x not yet supported\n",
2276 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
2277 (err = snd_usb_mixer_status_create(mixer)) < 0)
2280 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2283 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
2284 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2285 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
2286 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2288 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
2289 snd_info_set_text_ops(entry, mixer,
2290 snd_audigy2nx_proc_read);
2293 if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
2294 mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
2295 err = snd_xonar_u1_controls_create(mixer);
2300 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
2304 if (list_empty(&chip->mixer_list) &&
2305 !snd_card_proc_new(chip->card, "usbmixer", &entry))
2306 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
2308 list_add(&mixer->list, &chip->mixer_list);
2312 snd_usb_mixer_free(mixer);
2316 void snd_usb_mixer_disconnect(struct list_head *p)
2318 struct usb_mixer_interface *mixer;
2320 mixer = list_entry(p, struct usb_mixer_interface, list);
2321 usb_kill_urb(mixer->urb);
2322 usb_kill_urb(mixer->rc_urb);