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 */
127 int cache_val[MAX_CHANNELS];
133 USB_FEATURE_NONE = 0,
134 USB_FEATURE_MUTE = 1,
142 USB_FEATURE_BASSBOOST,
148 USB_MIXER_INV_BOOLEAN,
157 USB_PROC_UPDOWN_SWITCH = 1,
158 USB_PROC_UPDOWN_MODE_SEL = 2,
160 USB_PROC_PROLOGIC = 2,
161 USB_PROC_PROLOGIC_SWITCH = 1,
162 USB_PROC_PROLOGIC_MODE_SEL = 2,
165 USB_PROC_3DENH_SWITCH = 1,
166 USB_PROC_3DENH_SPACE = 2,
169 USB_PROC_REVERB_SWITCH = 1,
170 USB_PROC_REVERB_LEVEL = 2,
171 USB_PROC_REVERB_TIME = 3,
172 USB_PROC_REVERB_DELAY = 4,
175 USB_PROC_CHORUS_SWITCH = 1,
176 USB_PROC_CHORUS_LEVEL = 2,
177 USB_PROC_CHORUS_RATE = 3,
178 USB_PROC_CHORUS_DEPTH = 4,
181 USB_PROC_DCR_SWITCH = 1,
182 USB_PROC_DCR_RATIO = 2,
183 USB_PROC_DCR_MAX_AMP = 3,
184 USB_PROC_DCR_THRESHOLD = 4,
185 USB_PROC_DCR_ATTACK = 5,
186 USB_PROC_DCR_RELEASE = 6,
189 /*E-mu 0202(0404) eXtension Unit(XU) control*/
191 USB_XU_CLOCK_RATE = 0xe301,
192 USB_XU_CLOCK_SOURCE = 0xe302,
193 USB_XU_DIGITAL_IO_STATUS = 0xe303,
194 USB_XU_DEVICE_OPTIONS = 0xe304,
195 USB_XU_DIRECT_MONITORING = 0xe305,
196 USB_XU_METERING = 0xe306
199 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
200 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
201 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
202 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
206 * manual mapping of mixer names
207 * if the mixer topology is too complicated and the parsed names are
208 * ambiguous, add the entries in usbmixer_maps.c.
210 #include "usbmixer_maps.c"
212 /* get the mapped name if the unit matches */
213 static int check_mapped_name(struct mixer_build *state, int unitid, int control, char *buf, int buflen)
215 const struct usbmix_name_map *p;
220 for (p = state->map; p->id; p++) {
221 if (p->id == unitid && p->name &&
222 (! control || ! p->control || control == p->control)) {
224 return strlcpy(buf, p->name, buflen);
230 /* check whether the control should be ignored */
231 static int check_ignored_ctl(struct mixer_build *state, int unitid, int control)
233 const struct usbmix_name_map *p;
237 for (p = state->map; p->id; p++) {
238 if (p->id == unitid && ! p->name &&
239 (! control || ! p->control || control == p->control)) {
241 printk(KERN_DEBUG "ignored control %d:%d\n",
250 /* get the mapped selector source name */
251 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
252 int index, char *buf, int buflen)
254 const struct usbmix_selector_map *p;
256 if (! state->selector_map)
258 for (p = state->selector_map; p->id; p++) {
259 if (p->id == unitid && index < p->count)
260 return strlcpy(buf, p->names[index], buflen);
266 * find an audio control unit with the given unit id
268 static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
273 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
274 USB_DT_CS_INTERFACE)) != NULL) {
275 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
283 * copy a string with the given id
285 static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
287 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
293 * convert from the byte/word on usb descriptor to the zero-based integer
295 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
297 switch (cval->val_type) {
298 case USB_MIXER_BOOLEAN:
300 case USB_MIXER_INV_BOOLEAN:
323 * convert from the zero-based int to the byte/word for usb descriptor
325 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
327 switch (cval->val_type) {
328 case USB_MIXER_BOOLEAN:
330 case USB_MIXER_INV_BOOLEAN:
339 return 0; /* not reached */
342 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
348 else if (val >= cval->max)
349 return (cval->max - cval->min + cval->res - 1) / cval->res;
351 return (val - cval->min) / cval->res;
354 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
369 * retrieve a mixer value
372 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
374 unsigned char buf[2];
375 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
378 while (timeout-- > 0) {
379 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
380 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
382 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
383 validx, cval->mixer->ctrlif | (cval->id << 8),
384 buf, val_len, 100) >= val_len) {
385 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
389 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
390 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
394 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
396 return get_ctl_value(cval, GET_CUR, validx, value);
399 /* channel = 0: master, 1 = first channel */
400 static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
401 int channel, int *value)
403 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
406 static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
407 int channel, int index, int *value)
411 if (cval->cached & (1 << channel)) {
412 *value = cval->cache_val[index];
415 err = get_cur_mix_raw(cval, channel, value);
417 if (!cval->mixer->ignore_ctl_error)
418 snd_printd(KERN_ERR "cannot get current value for "
419 "control %d ch %d: err = %d\n",
420 cval->control, channel, err);
423 cval->cached |= 1 << channel;
424 cval->cache_val[index] = *value;
433 static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
435 unsigned char buf[2];
436 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
439 value_set = convert_bytes_value(cval, value_set);
440 buf[0] = value_set & 0xff;
441 buf[1] = (value_set >> 8) & 0xff;
442 while (timeout-- > 0)
443 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
444 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
446 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
447 validx, cval->mixer->ctrlif | (cval->id << 8),
448 buf, val_len, 100) >= 0)
450 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
451 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
455 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
457 return set_ctl_value(cval, SET_CUR, validx, value);
460 static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
461 int index, int value)
464 err = set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel,
468 cval->cached |= 1 << channel;
469 cval->cache_val[index] = value;
474 * TLV callback for mixer volume controls
476 static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
477 unsigned int size, unsigned int __user *_tlv)
479 struct usb_mixer_elem_info *cval = kcontrol->private_data;
480 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
482 if (size < sizeof(scale))
484 /* USB descriptions contain the dB scale in 1/256 dB unit
485 * while ALSA TLV contains in 1/100 dB unit
487 scale[2] = (convert_signed_value(cval, cval->min) * 100) / 256;
488 scale[3] = (convert_signed_value(cval, cval->max) * 100) / 256;
489 if (scale[3] <= scale[2]) {
490 /* something is wrong; assume it's either from/to 0dB */
493 else if (scale[2] > 0)
495 else /* totally crap, return an error */
498 if (copy_to_user(_tlv, scale, sizeof(scale)))
504 * parser routines begin here...
507 static int parse_audio_unit(struct mixer_build *state, int unitid);
511 * check if the input/output channel routing is enabled on the given bitmap.
512 * used for mixer unit parser
514 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
516 int idx = ich * num_outs + och;
517 return bmap[idx >> 3] & (0x80 >> (idx & 7));
522 * add an alsa control element
523 * search and increment the index until an empty slot is found.
525 * if failed, give up and free the control instance.
528 static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
530 struct usb_mixer_elem_info *cval = kctl->private_data;
533 while (snd_ctl_find_id(state->chip->card, &kctl->id))
535 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
536 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
539 cval->elem_id = &kctl->id;
540 cval->next_id_elem = state->mixer->id_elems[cval->id];
541 state->mixer->id_elems[cval->id] = cval;
547 * get a terminal name string
550 static struct iterm_name_combo {
554 { 0x0300, "Output" },
555 { 0x0301, "Speaker" },
556 { 0x0302, "Headphone" },
557 { 0x0303, "HMD Audio" },
558 { 0x0304, "Desktop Speaker" },
559 { 0x0305, "Room Speaker" },
560 { 0x0306, "Com Speaker" },
562 { 0x0600, "External In" },
563 { 0x0601, "Analog In" },
564 { 0x0602, "Digital In" },
566 { 0x0604, "Legacy In" },
567 { 0x0605, "IEC958 In" },
568 { 0x0606, "1394 DA Stream" },
569 { 0x0607, "1394 DV Stream" },
570 { 0x0700, "Embedded" },
571 { 0x0701, "Noise Source" },
572 { 0x0702, "Equalization Noise" },
576 { 0x0706, "MiniDisk" },
577 { 0x0707, "Analog Tape" },
578 { 0x0708, "Phonograph" },
579 { 0x0709, "VCR Audio" },
580 { 0x070a, "Video Disk Audio" },
581 { 0x070b, "DVD Audio" },
582 { 0x070c, "TV Tuner Audio" },
583 { 0x070d, "Satellite Rec Audio" },
584 { 0x070e, "Cable Tuner Audio" },
585 { 0x070f, "DSS Audio" },
586 { 0x0710, "Radio Receiver" },
587 { 0x0711, "Radio Transmitter" },
588 { 0x0712, "Multi-Track Recorder" },
589 { 0x0713, "Synthesizer" },
593 static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
594 unsigned char *name, int maxlen, int term_only)
596 struct iterm_name_combo *names;
599 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
601 /* virtual type - not a real terminal */
602 if (iterm->type >> 16) {
605 switch (iterm->type >> 16) {
607 strcpy(name, "Selector"); return 8;
608 case PROCESSING_UNIT:
609 strcpy(name, "Process Unit"); return 12;
611 strcpy(name, "Ext Unit"); return 8;
613 strcpy(name, "Mixer"); return 5;
615 return sprintf(name, "Unit %d", iterm->id);
619 switch (iterm->type & 0xff00) {
621 strcpy(name, "PCM"); return 3;
623 strcpy(name, "Mic"); return 3;
625 strcpy(name, "Headset"); return 7;
627 strcpy(name, "Phone"); return 5;
630 for (names = iterm_names; names->type; names++)
631 if (names->type == iterm->type) {
632 strcpy(name, names->name);
633 return strlen(names->name);
640 * parse the source unit recursively until it reaches to a terminal
641 * or a branched unit.
643 static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
647 memset(term, 0, sizeof(*term));
648 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
652 term->type = combine_word(p1 + 4);
653 term->channels = p1[7];
654 term->chconfig = combine_word(p1 + 8);
659 break; /* continue to parse */
661 term->type = p1[2] << 16; /* virtual type */
662 term->channels = p1[5 + p1[4]];
663 term->chconfig = combine_word(p1 + 6 + p1[4]);
664 term->name = p1[p1[0] - 1];
667 /* call recursively to retrieve the channel info */
668 if (check_input_term(state, p1[5], term) < 0)
670 term->type = p1[2] << 16; /* virtual type */
672 term->name = p1[9 + p1[0] - 1];
674 case PROCESSING_UNIT:
678 break; /* continue to parse */
680 term->type = p1[2] << 16; /* virtual type */
681 term->channels = p1[7 + p1[6]];
682 term->chconfig = combine_word(p1 + 8 + p1[6]);
683 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
697 /* feature unit control information */
698 struct usb_feature_control_info {
700 unsigned int type; /* control type (mute, volume, etc.) */
703 static struct usb_feature_control_info audio_feature_info[] = {
704 { "Mute", USB_MIXER_INV_BOOLEAN },
705 { "Volume", USB_MIXER_S16 },
706 { "Tone Control - Bass", USB_MIXER_S8 },
707 { "Tone Control - Mid", USB_MIXER_S8 },
708 { "Tone Control - Treble", USB_MIXER_S8 },
709 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
710 { "Auto Gain Control", USB_MIXER_BOOLEAN },
711 { "Delay Control", USB_MIXER_U16 },
712 { "Bass Boost", USB_MIXER_BOOLEAN },
713 { "Loudness", USB_MIXER_BOOLEAN },
717 /* private_free callback */
718 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
720 kfree(kctl->private_data);
721 kctl->private_data = NULL;
726 * interface to ALSA control for feature/mixer units
730 * retrieve the minimum and maximum values for the specified control
732 static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
735 cval->min = default_min;
736 cval->max = cval->min + 1;
739 if (cval->val_type == USB_MIXER_BOOLEAN ||
740 cval->val_type == USB_MIXER_INV_BOOLEAN) {
741 cval->initialized = 1;
746 for (i = 0; i < MAX_CHANNELS; i++)
747 if (cval->cmask & (1 << i)) {
752 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
753 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
754 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
755 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
758 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
761 int last_valid_res = cval->res;
763 while (cval->res > 1) {
764 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
768 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
769 cval->res = last_valid_res;
774 /* Additional checks for the proper resolution
776 * Some devices report smaller resolutions than actually
777 * reacting. They don't return errors but simply clip
778 * to the lower aligned value.
780 if (cval->min + cval->res < cval->max) {
781 int last_valid_res = cval->res;
782 int saved, test, check;
783 get_cur_mix_raw(cval, minchn, &saved);
786 if (test < cval->max)
790 if (test < cval->min || test > cval->max ||
791 set_cur_mix_value(cval, minchn, 0, test) ||
792 get_cur_mix_raw(cval, minchn, &check)) {
793 cval->res = last_valid_res;
800 set_cur_mix_value(cval, minchn, 0, saved);
803 cval->initialized = 1;
809 /* get a feature/mixer unit info */
810 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
812 struct usb_mixer_elem_info *cval = kcontrol->private_data;
814 if (cval->val_type == USB_MIXER_BOOLEAN ||
815 cval->val_type == USB_MIXER_INV_BOOLEAN)
816 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
818 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
819 uinfo->count = cval->channels;
820 if (cval->val_type == USB_MIXER_BOOLEAN ||
821 cval->val_type == USB_MIXER_INV_BOOLEAN) {
822 uinfo->value.integer.min = 0;
823 uinfo->value.integer.max = 1;
825 if (! cval->initialized)
826 get_min_max(cval, 0);
827 uinfo->value.integer.min = 0;
828 uinfo->value.integer.max =
829 (cval->max - cval->min + cval->res - 1) / cval->res;
834 /* get the current value from feature/mixer unit */
835 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
837 struct usb_mixer_elem_info *cval = kcontrol->private_data;
838 int c, cnt, val, err;
840 ucontrol->value.integer.value[0] = cval->min;
843 for (c = 0; c < MAX_CHANNELS; c++) {
844 if (!(cval->cmask & (1 << c)))
846 err = get_cur_mix_value(cval, c + 1, cnt, &val);
848 return cval->mixer->ignore_ctl_error ? 0 : err;
849 val = get_relative_value(cval, val);
850 ucontrol->value.integer.value[cnt] = val;
856 err = get_cur_mix_value(cval, 0, 0, &val);
858 return cval->mixer->ignore_ctl_error ? 0 : err;
859 val = get_relative_value(cval, val);
860 ucontrol->value.integer.value[0] = val;
865 /* put the current value to feature/mixer unit */
866 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
868 struct usb_mixer_elem_info *cval = kcontrol->private_data;
869 int c, cnt, val, oval, err;
874 for (c = 0; c < MAX_CHANNELS; c++) {
875 if (!(cval->cmask & (1 << c)))
877 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
879 return cval->mixer->ignore_ctl_error ? 0 : err;
880 val = ucontrol->value.integer.value[cnt];
881 val = get_abs_value(cval, val);
883 set_cur_mix_value(cval, c + 1, cnt, val);
890 err = get_cur_mix_value(cval, 0, 0, &oval);
892 return cval->mixer->ignore_ctl_error ? 0 : err;
893 val = ucontrol->value.integer.value[0];
894 val = get_abs_value(cval, val);
896 set_cur_mix_value(cval, 0, 0, val);
903 static struct snd_kcontrol_new usb_feature_unit_ctl = {
904 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
905 .name = "", /* will be filled later manually */
906 .info = mixer_ctl_feature_info,
907 .get = mixer_ctl_feature_get,
908 .put = mixer_ctl_feature_put,
913 * build a feature control
916 static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
918 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
921 static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
922 unsigned int ctl_mask, int control,
923 struct usb_audio_term *iterm, int unitid)
925 unsigned int len = 0;
927 int nameid = desc[desc[0] - 1];
928 struct snd_kcontrol *kctl;
929 struct usb_mixer_elem_info *cval;
931 control++; /* change from zero-based to 1-based value */
933 if (control == USB_FEATURE_GEQ) {
934 /* FIXME: not supported yet */
938 if (check_ignored_ctl(state, unitid, control))
941 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
943 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
946 cval->mixer = state->mixer;
948 cval->control = control;
949 cval->cmask = ctl_mask;
950 cval->val_type = audio_feature_info[control-1].type;
952 cval->channels = 1; /* master channel */
955 for (i = 0; i < 16; i++)
956 if (ctl_mask & (1 << i))
961 /* get min/max values */
962 get_min_max(cval, 0);
964 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
966 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
970 kctl->private_free = usb_mixer_elem_free;
972 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
973 mapped_name = len != 0;
975 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
978 case USB_FEATURE_MUTE:
979 case USB_FEATURE_VOLUME:
980 /* determine the control name. the rule is:
981 * - if a name id is given in descriptor, use it.
982 * - if the connected input can be determined, then use the name
984 * - if the connected output can be determined, use it.
985 * - otherwise, anonymous name.
988 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
990 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
992 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
993 "Feature %d", unitid);
995 /* determine the stream direction:
996 * if the connected output is USB stream, then it's likely a
997 * capture stream. otherwise it should be playback (hopefully :)
999 if (! mapped_name && ! (state->oterm.type >> 16)) {
1000 if ((state->oterm.type & 0xff00) == 0x0100) {
1001 len = append_ctl_name(kctl, " Capture");
1003 len = append_ctl_name(kctl, " Playback");
1006 append_ctl_name(kctl, control == USB_FEATURE_MUTE ?
1007 " Switch" : " Volume");
1008 if (control == USB_FEATURE_VOLUME) {
1009 kctl->tlv.c = mixer_vol_tlv;
1010 kctl->vd[0].access |=
1011 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1012 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1018 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1019 sizeof(kctl->id.name));
1023 /* volume control quirks */
1024 switch (state->chip->usb_id) {
1025 case USB_ID(0x0471, 0x0101):
1026 case USB_ID(0x0471, 0x0104):
1027 case USB_ID(0x0471, 0x0105):
1028 case USB_ID(0x0672, 0x1041):
1029 /* quirk for UDA1321/N101.
1030 * note that detection between firmware 2.1.1.7 (N101)
1031 * and later 2.1.1.21 is not very clear from datasheets.
1032 * I hope that the min value is -15360 for newer firmware --jk
1034 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1035 cval->min == -15616) {
1036 snd_printk(KERN_INFO
1037 "set volume quirk for UDA1321/N101 chip\n");
1042 case USB_ID(0x046d, 0x09a4):
1043 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1044 snd_printk(KERN_INFO
1045 "set volume quirk for QuickCam E3500\n");
1054 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1055 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
1056 add_control_to_empty(state, kctl);
1062 * parse a feature unit
1064 * most of controlls are defined here.
1066 static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
1069 struct usb_audio_term iterm;
1070 unsigned int master_bits, first_ch_bits;
1073 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1074 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1078 /* parse the source unit */
1079 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1082 /* determine the input source type and name */
1083 if (check_input_term(state, ftr[4], &iterm) < 0)
1086 channels = (ftr[0] - 7) / csize - 1;
1088 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
1089 /* master configuration quirks */
1090 switch (state->chip->usb_id) {
1091 case USB_ID(0x08bb, 0x2702):
1092 snd_printk(KERN_INFO
1093 "usbmixer: master volume quirk for PCM2702 chip\n");
1094 /* disable non-functional volume control */
1095 master_bits &= ~(1 << (USB_FEATURE_VOLUME - 1));
1099 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1102 /* check all control types */
1103 for (i = 0; i < 10; i++) {
1104 unsigned int ch_bits = 0;
1105 for (j = 0; j < channels; j++) {
1106 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1107 if (mask & (1 << i))
1108 ch_bits |= (1 << j);
1110 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1111 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1112 if (master_bits & (1 << i))
1113 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1125 * build a mixer unit control
1127 * the callbacks are identical with feature unit.
1128 * input channel number (zero based) is given in control field instead.
1131 static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
1132 int in_pin, int in_ch, int unitid,
1133 struct usb_audio_term *iterm)
1135 struct usb_mixer_elem_info *cval;
1136 unsigned int input_pins = desc[4];
1137 unsigned int num_outs = desc[5 + input_pins];
1138 unsigned int i, len;
1139 struct snd_kcontrol *kctl;
1141 if (check_ignored_ctl(state, unitid, 0))
1144 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1148 cval->mixer = state->mixer;
1150 cval->control = in_ch + 1; /* based on 1 */
1151 cval->val_type = USB_MIXER_S16;
1152 for (i = 0; i < num_outs; i++) {
1153 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1154 cval->cmask |= (1 << i);
1159 /* get min/max values */
1160 get_min_max(cval, 0);
1162 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1164 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1168 kctl->private_free = usb_mixer_elem_free;
1170 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1172 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1174 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1175 append_ctl_name(kctl, " Volume");
1177 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1178 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1179 add_control_to_empty(state, kctl);
1184 * parse a mixer unit
1186 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1188 struct usb_audio_term iterm;
1189 int input_pins, num_ins, num_outs;
1192 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1193 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1196 /* no bmControls field (e.g. Maya44) -> ignore */
1197 if (desc[0] <= 10 + input_pins) {
1198 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1204 for (pin = 0; pin < input_pins; pin++) {
1205 err = parse_audio_unit(state, desc[5 + pin]);
1208 err = check_input_term(state, desc[5 + pin], &iterm);
1211 num_ins += iterm.channels;
1212 for (; ich < num_ins; ++ich) {
1213 int och, ich_has_controls = 0;
1215 for (och = 0; och < num_outs; ++och) {
1216 if (check_matrix_bitmap(desc + 9 + input_pins,
1217 ich, och, num_outs)) {
1218 ich_has_controls = 1;
1222 if (ich_has_controls)
1223 build_mixer_unit_ctl(state, desc, pin, ich,
1232 * Processing Unit / Extension Unit
1235 /* get callback for processing/extension unit */
1236 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1238 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1241 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1242 if (err < 0 && cval->mixer->ignore_ctl_error) {
1243 ucontrol->value.integer.value[0] = cval->min;
1248 val = get_relative_value(cval, val);
1249 ucontrol->value.integer.value[0] = val;
1253 /* put callback for processing/extension unit */
1254 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1256 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1259 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1261 if (cval->mixer->ignore_ctl_error)
1265 val = ucontrol->value.integer.value[0];
1266 val = get_abs_value(cval, val);
1268 set_cur_ctl_value(cval, cval->control << 8, val);
1274 /* alsa control interface for processing/extension unit */
1275 static struct snd_kcontrol_new mixer_procunit_ctl = {
1276 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1277 .name = "", /* will be filled later */
1278 .info = mixer_ctl_feature_info,
1279 .get = mixer_ctl_procunit_get,
1280 .put = mixer_ctl_procunit_put,
1285 * predefined data for processing units
1287 struct procunit_value_info {
1294 struct procunit_info {
1297 struct procunit_value_info *values;
1300 static struct procunit_value_info updown_proc_info[] = {
1301 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1302 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1305 static struct procunit_value_info prologic_proc_info[] = {
1306 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1307 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1310 static struct procunit_value_info threed_enh_proc_info[] = {
1311 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1312 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1315 static struct procunit_value_info reverb_proc_info[] = {
1316 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1317 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1318 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1319 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1322 static struct procunit_value_info chorus_proc_info[] = {
1323 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1324 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1325 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1326 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1329 static struct procunit_value_info dcr_proc_info[] = {
1330 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1331 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1332 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1333 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1334 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1335 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1339 static struct procunit_info procunits[] = {
1340 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1341 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1342 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1343 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1344 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1345 { USB_PROC_DCR, "DCR", dcr_proc_info },
1349 * predefined data for extension units
1351 static struct procunit_value_info clock_rate_xu_info[] = {
1352 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
1355 static struct procunit_value_info clock_source_xu_info[] = {
1356 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
1359 static struct procunit_value_info spdif_format_xu_info[] = {
1360 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
1363 static struct procunit_value_info soft_limit_xu_info[] = {
1364 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
1367 static struct procunit_info extunits[] = {
1368 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
1369 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
1370 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
1371 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
1375 * build a processing/extension unit
1377 static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1379 int num_ins = dsc[6];
1380 struct usb_mixer_elem_info *cval;
1381 struct snd_kcontrol *kctl;
1382 int i, err, nameid, type, len;
1383 struct procunit_info *info;
1384 struct procunit_value_info *valinfo;
1385 static struct procunit_value_info default_value_info[] = {
1386 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1389 static struct procunit_info default_info = {
1390 0, NULL, default_value_info
1393 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1394 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1398 for (i = 0; i < num_ins; i++) {
1399 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1403 type = combine_word(&dsc[4]);
1404 for (info = list; info && info->type; info++)
1405 if (info->type == type)
1407 if (! info || ! info->type)
1408 info = &default_info;
1410 for (valinfo = info->values; valinfo->control; valinfo++) {
1411 /* FIXME: bitmap might be longer than 8bit */
1412 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1414 if (check_ignored_ctl(state, unitid, valinfo->control))
1416 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1418 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1421 cval->mixer = state->mixer;
1423 cval->control = valinfo->control;
1424 cval->val_type = valinfo->val_type;
1427 /* get min/max values */
1428 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1429 /* FIXME: hard-coded */
1431 cval->max = dsc[15];
1433 cval->initialized = 1;
1435 if (type == USB_XU_CLOCK_RATE) {
1436 /* E-Mu USB 0404/0202/TrackerPre
1437 * samplerate control quirk
1442 cval->initialized = 1;
1444 get_min_max(cval, valinfo->min_value);
1447 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1449 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1453 kctl->private_free = usb_mixer_elem_free;
1455 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1457 else if (info->name)
1458 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1460 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1463 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1465 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1467 append_ctl_name(kctl, " ");
1468 append_ctl_name(kctl, valinfo->suffix);
1470 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1471 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1472 if ((err = add_control_to_empty(state, kctl)) < 0)
1479 static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1481 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1484 static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1486 return build_audio_procunit(state, unitid, desc, extunits, "Extension Unit");
1494 /* info callback for selector unit
1495 * use an enumerator type for routing
1497 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1499 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1500 char **itemlist = (char **)kcontrol->private_value;
1502 if (snd_BUG_ON(!itemlist))
1504 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1506 uinfo->value.enumerated.items = cval->max;
1507 if ((int)uinfo->value.enumerated.item >= cval->max)
1508 uinfo->value.enumerated.item = cval->max - 1;
1509 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1513 /* get callback for selector unit */
1514 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1516 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1519 err = get_cur_ctl_value(cval, 0, &val);
1521 if (cval->mixer->ignore_ctl_error) {
1522 ucontrol->value.enumerated.item[0] = 0;
1527 val = get_relative_value(cval, val);
1528 ucontrol->value.enumerated.item[0] = val;
1532 /* put callback for selector unit */
1533 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1535 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1538 err = get_cur_ctl_value(cval, 0, &oval);
1540 if (cval->mixer->ignore_ctl_error)
1544 val = ucontrol->value.enumerated.item[0];
1545 val = get_abs_value(cval, val);
1547 set_cur_ctl_value(cval, 0, val);
1553 /* alsa control interface for selector unit */
1554 static struct snd_kcontrol_new mixer_selectunit_ctl = {
1555 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1556 .name = "", /* will be filled later */
1557 .info = mixer_ctl_selector_info,
1558 .get = mixer_ctl_selector_get,
1559 .put = mixer_ctl_selector_put,
1563 /* private free callback.
1564 * free both private_data and private_value
1566 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
1570 if (kctl->private_data) {
1571 struct usb_mixer_elem_info *cval = kctl->private_data;
1572 num_ins = cval->max;
1574 kctl->private_data = NULL;
1576 if (kctl->private_value) {
1577 char **itemlist = (char **)kctl->private_value;
1578 for (i = 0; i < num_ins; i++)
1581 kctl->private_value = 0;
1586 * parse a selector unit
1588 static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1590 unsigned int num_ins = desc[4];
1591 unsigned int i, nameid, len;
1593 struct usb_mixer_elem_info *cval;
1594 struct snd_kcontrol *kctl;
1597 if (! num_ins || desc[0] < 5 + num_ins) {
1598 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1602 for (i = 0; i < num_ins; i++) {
1603 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1607 if (num_ins == 1) /* only one ? nonsense! */
1610 if (check_ignored_ctl(state, unitid, 0))
1613 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1615 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1618 cval->mixer = state->mixer;
1620 cval->val_type = USB_MIXER_U8;
1623 cval->max = num_ins;
1625 cval->initialized = 1;
1627 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1629 snd_printk(KERN_ERR "cannot malloc\n");
1633 #define MAX_ITEM_NAME_LEN 64
1634 for (i = 0; i < num_ins; i++) {
1635 struct usb_audio_term iterm;
1637 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1638 if (! namelist[i]) {
1639 snd_printk(KERN_ERR "cannot malloc\n");
1646 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1648 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1649 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1651 sprintf(namelist[i], "Input %d", i);
1654 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1656 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1661 kctl->private_value = (unsigned long)namelist;
1662 kctl->private_free = usb_mixer_selector_elem_free;
1664 nameid = desc[desc[0] - 1];
1665 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1669 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1671 len = get_term_name(state, &state->oterm,
1672 kctl->id.name, sizeof(kctl->id.name), 0);
1674 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1676 if ((state->oterm.type & 0xff00) == 0x0100)
1677 append_ctl_name(kctl, " Capture Source");
1679 append_ctl_name(kctl, " Playback Source");
1682 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1683 cval->id, kctl->id.name, num_ins);
1684 if ((err = add_control_to_empty(state, kctl)) < 0)
1692 * parse an audio unit recursively
1695 static int parse_audio_unit(struct mixer_build *state, int unitid)
1699 if (test_and_set_bit(unitid, state->unitbitmap))
1700 return 0; /* the unit already visited */
1702 p1 = find_audio_control_unit(state, unitid);
1704 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1709 case INPUT_TERMINAL:
1712 return parse_audio_mixer_unit(state, unitid, p1);
1714 return parse_audio_selector_unit(state, unitid, p1);
1716 return parse_audio_feature_unit(state, unitid, p1);
1717 case PROCESSING_UNIT:
1718 return parse_audio_processing_unit(state, unitid, p1);
1719 case EXTENSION_UNIT:
1720 return parse_audio_extension_unit(state, unitid, p1);
1722 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1727 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1729 kfree(mixer->id_elems);
1731 kfree(mixer->urb->transfer_buffer);
1732 usb_free_urb(mixer->urb);
1734 usb_free_urb(mixer->rc_urb);
1735 kfree(mixer->rc_setup_packet);
1739 static int snd_usb_mixer_dev_free(struct snd_device *device)
1741 struct usb_mixer_interface *mixer = device->device_data;
1742 snd_usb_mixer_free(mixer);
1747 * create mixer controls
1749 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1751 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1753 unsigned char *desc;
1754 struct mixer_build state;
1756 const struct usbmix_ctl_map *map;
1757 struct usb_host_interface *hostif;
1759 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1760 memset(&state, 0, sizeof(state));
1761 state.chip = mixer->chip;
1762 state.mixer = mixer;
1763 state.buffer = hostif->extra;
1764 state.buflen = hostif->extralen;
1766 /* check the mapping table */
1767 for (map = usbmix_ctl_maps; map->id; map++) {
1768 if (map->id == state.chip->usb_id) {
1769 state.map = map->map;
1770 state.selector_map = map->selector_map;
1771 mixer->ignore_ctl_error = map->ignore_ctl_error;
1777 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1779 continue; /* invalid descriptor? */
1780 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1781 state.oterm.id = desc[3];
1782 state.oterm.type = combine_word(&desc[4]);
1783 state.oterm.name = desc[8];
1784 err = parse_audio_unit(&state, desc[7]);
1791 static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1794 struct usb_mixer_elem_info *info;
1796 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1797 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1801 static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1806 /* unit ids specific to Extigy/Audigy 2 NX: */
1808 case 0: /* remote control */
1809 mixer->rc_urb->dev = mixer->chip->dev;
1810 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1812 case 4: /* digital in jack */
1813 case 7: /* line in jacks */
1814 case 19: /* speaker out jacks */
1815 case 20: /* headphones out jack */
1817 /* live24ext: 4 = line-in jack */
1818 case 3: /* hp-out jack (may actuate Mute) */
1819 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1820 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1821 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1824 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1829 static void snd_usb_mixer_status_complete(struct urb *urb)
1831 struct usb_mixer_interface *mixer = urb->context;
1833 if (urb->status == 0) {
1834 u8 *buf = urb->transfer_buffer;
1837 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1838 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1840 /* ignore any notifications not from the control interface */
1841 if ((buf[0] & 0x0f) != 0)
1843 if (!(buf[0] & 0x40))
1844 snd_usb_mixer_notify_id(mixer, buf[1]);
1846 snd_usb_mixer_memory_change(mixer, buf[1]);
1849 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1850 urb->dev = mixer->chip->dev;
1851 usb_submit_urb(urb, GFP_ATOMIC);
1855 /* create the handler for the optional status interrupt endpoint */
1856 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1858 struct usb_host_interface *hostif;
1859 struct usb_endpoint_descriptor *ep;
1860 void *transfer_buffer;
1864 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1865 /* we need one interrupt input endpoint */
1866 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1868 ep = get_endpoint(hostif, 0);
1869 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
1872 epnum = usb_endpoint_num(ep);
1873 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1874 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1875 if (!transfer_buffer)
1877 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1879 kfree(transfer_buffer);
1882 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1883 usb_rcvintpipe(mixer->chip->dev, epnum),
1884 transfer_buffer, buffer_length,
1885 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1886 usb_submit_urb(mixer->urb, GFP_KERNEL);
1890 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
1892 struct usb_mixer_interface *mixer = urb->context;
1893 const struct rc_config *rc = mixer->rc_cfg;
1896 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
1899 code = mixer->rc_buffer[rc->offset];
1900 if (rc->length == 2)
1901 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1903 /* the Mute button actually changes the mixer control */
1904 if (code == rc->mute_code)
1905 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
1906 mixer->rc_code = code;
1908 wake_up(&mixer->rc_waitq);
1911 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
1912 long count, loff_t *offset)
1914 struct usb_mixer_interface *mixer = hw->private_data;
1918 if (count != 1 && count != 4)
1920 err = wait_event_interruptible(mixer->rc_waitq,
1921 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1924 err = put_user(rc_code, buf);
1926 err = put_user(rc_code, (u32 __user *)buf);
1928 return err < 0 ? err : count;
1931 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
1934 struct usb_mixer_interface *mixer = hw->private_data;
1936 poll_wait(file, &mixer->rc_waitq, wait);
1937 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1940 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1942 struct snd_hwdep *hwdep;
1945 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1946 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1948 if (i >= ARRAY_SIZE(rc_configs))
1950 mixer->rc_cfg = &rc_configs[i];
1952 len = mixer->rc_cfg->packet_length;
1954 init_waitqueue_head(&mixer->rc_waitq);
1955 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1958 snprintf(hwdep->name, sizeof(hwdep->name),
1959 "%s remote control", mixer->chip->card->shortname);
1960 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1961 hwdep->private_data = mixer;
1962 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1963 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1964 hwdep->exclusive = 1;
1966 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1969 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1970 if (!mixer->rc_setup_packet) {
1971 usb_free_urb(mixer->rc_urb);
1972 mixer->rc_urb = NULL;
1975 mixer->rc_setup_packet->bRequestType =
1976 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1977 mixer->rc_setup_packet->bRequest = GET_MEM;
1978 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1979 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1980 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1981 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1982 usb_rcvctrlpipe(mixer->chip->dev, 0),
1983 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1984 snd_usb_soundblaster_remote_complete, mixer);
1988 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
1990 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1992 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1993 int index = kcontrol->private_value;
1995 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1999 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2001 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2002 int index = kcontrol->private_value;
2003 int value = ucontrol->value.integer.value[0];
2008 changed = value != mixer->audigy2nx_leds[index];
2009 err = snd_usb_ctl_msg(mixer->chip->dev,
2010 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
2011 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2012 value, index + 2, NULL, 0, 100);
2015 mixer->audigy2nx_leds[index] = value;
2019 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
2021 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2022 .name = "CMSS LED Switch",
2023 .info = snd_audigy2nx_led_info,
2024 .get = snd_audigy2nx_led_get,
2025 .put = snd_audigy2nx_led_put,
2029 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2030 .name = "Power LED Switch",
2031 .info = snd_audigy2nx_led_info,
2032 .get = snd_audigy2nx_led_get,
2033 .put = snd_audigy2nx_led_put,
2037 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2038 .name = "Dolby Digital LED Switch",
2039 .info = snd_audigy2nx_led_info,
2040 .get = snd_audigy2nx_led_get,
2041 .put = snd_audigy2nx_led_put,
2046 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
2050 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
2051 if (i > 1 && /* Live24ext has 2 LEDs only */
2052 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2053 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
2055 err = snd_ctl_add(mixer->chip->card,
2056 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
2060 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
2064 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
2065 struct snd_info_buffer *buffer)
2067 static const struct sb_jack {
2070 } jacks_audigy2nx[] = {
2076 }, jacks_live24ext[] = {
2077 {4, "line in"}, /* &1=Line, &2=Mic*/
2078 {3, "hph out"}, /* headphones */
2079 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2082 const struct sb_jack *jacks;
2083 struct usb_mixer_interface *mixer = entry->private_data;
2087 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
2088 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2089 jacks = jacks_audigy2nx;
2090 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2091 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
2092 jacks = jacks_live24ext;
2096 for (i = 0; jacks[i].name; ++i) {
2097 snd_iprintf(buffer, "%s: ", jacks[i].name);
2098 err = snd_usb_ctl_msg(mixer->chip->dev,
2099 usb_rcvctrlpipe(mixer->chip->dev, 0),
2100 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2101 USB_RECIP_INTERFACE, 0,
2102 jacks[i].unitid << 8, buf, 3, 100);
2103 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
2104 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2106 snd_iprintf(buffer, "?\n");
2110 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
2111 struct snd_ctl_elem_value *ucontrol)
2113 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2115 ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
2119 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
2120 struct snd_ctl_elem_value *ucontrol)
2122 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2123 u8 old_status, new_status;
2126 old_status = mixer->xonar_u1_status;
2127 if (ucontrol->value.integer.value[0])
2128 new_status = old_status | 0x02;
2130 new_status = old_status & ~0x02;
2131 changed = new_status != old_status;
2132 err = snd_usb_ctl_msg(mixer->chip->dev,
2133 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
2134 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2135 50, 0, &new_status, 1, 100);
2138 mixer->xonar_u1_status = new_status;
2142 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
2143 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2144 .name = "Digital Playback Switch",
2145 .info = snd_ctl_boolean_mono_info,
2146 .get = snd_xonar_u1_switch_get,
2147 .put = snd_xonar_u1_switch_put,
2150 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
2154 err = snd_ctl_add(mixer->chip->card,
2155 snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
2158 mixer->xonar_u1_status = 0x05;
2162 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
2163 unsigned char samplerate_id)
2165 struct usb_mixer_interface *mixer;
2166 struct usb_mixer_elem_info *cval;
2167 int unitid = 12; /* SamleRate ExtensionUnit ID */
2169 list_for_each_entry(mixer, &chip->mixer_list, list) {
2170 cval = mixer->id_elems[unitid];
2172 set_cur_ctl_value(cval, cval->control << 8, samplerate_id);
2173 snd_usb_mixer_notify_id(mixer, unitid);
2179 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2182 static struct snd_device_ops dev_ops = {
2183 .dev_free = snd_usb_mixer_dev_free
2185 struct usb_mixer_interface *mixer;
2188 strcpy(chip->card->mixername, "USB Mixer");
2190 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
2194 mixer->ctrlif = ctrlif;
2195 mixer->ignore_ctl_error = ignore_error;
2196 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2197 if (!mixer->id_elems) {
2202 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
2203 (err = snd_usb_mixer_status_create(mixer)) < 0)
2206 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2209 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
2210 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2211 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
2212 struct snd_info_entry *entry;
2214 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2216 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
2217 snd_info_set_text_ops(entry, mixer,
2218 snd_audigy2nx_proc_read);
2221 if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
2222 mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
2223 err = snd_xonar_u1_controls_create(mixer);
2228 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
2231 list_add(&mixer->list, &chip->mixer_list);
2235 snd_usb_mixer_free(mixer);
2239 void snd_usb_mixer_disconnect(struct list_head *p)
2241 struct usb_mixer_interface *mixer;
2243 mixer = list_entry(p, struct usb_mixer_interface, list);
2244 usb_kill_urb(mixer->urb);
2245 usb_kill_urb(mixer->rc_urb);