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;
82 unsigned long rc_hwdep_open;
84 wait_queue_head_t rc_waitq;
86 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,
191 * manual mapping of mixer names
192 * if the mixer topology is too complicated and the parsed names are
193 * ambiguous, add the entries in usbmixer_maps.c.
195 #include "usbmixer_maps.c"
197 /* get the mapped name if the unit matches */
198 static int check_mapped_name(struct mixer_build *state, int unitid, int control, char *buf, int buflen)
200 const struct usbmix_name_map *p;
205 for (p = state->map; p->id; p++) {
206 if (p->id == unitid && p->name &&
207 (! control || ! p->control || control == p->control)) {
209 return strlcpy(buf, p->name, buflen);
215 /* check whether the control should be ignored */
216 static int check_ignored_ctl(struct mixer_build *state, int unitid, int control)
218 const struct usbmix_name_map *p;
222 for (p = state->map; p->id; p++) {
223 if (p->id == unitid && ! p->name &&
224 (! control || ! p->control || control == p->control)) {
226 printk(KERN_DEBUG "ignored control %d:%d\n",
235 /* get the mapped selector source name */
236 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
237 int index, char *buf, int buflen)
239 const struct usbmix_selector_map *p;
241 if (! state->selector_map)
243 for (p = state->selector_map; p->id; p++) {
244 if (p->id == unitid && index < p->count)
245 return strlcpy(buf, p->names[index], buflen);
251 * find an audio control unit with the given unit id
253 static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
258 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
259 USB_DT_CS_INTERFACE)) != NULL) {
260 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
268 * copy a string with the given id
270 static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
272 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
278 * convert from the byte/word on usb descriptor to the zero-based integer
280 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
282 switch (cval->val_type) {
283 case USB_MIXER_BOOLEAN:
285 case USB_MIXER_INV_BOOLEAN:
308 * convert from the zero-based int to the byte/word for usb descriptor
310 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
312 switch (cval->val_type) {
313 case USB_MIXER_BOOLEAN:
315 case USB_MIXER_INV_BOOLEAN:
324 return 0; /* not reached */
327 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
333 else if (val >= cval->max)
334 return (cval->max - cval->min + cval->res - 1) / cval->res;
336 return (val - cval->min) / cval->res;
339 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
354 * retrieve a mixer value
357 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
359 unsigned char buf[2];
360 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
363 while (timeout-- > 0) {
364 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
365 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
367 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
368 validx, cval->mixer->ctrlif | (cval->id << 8),
369 buf, val_len, 100) >= val_len) {
370 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
374 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
375 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
379 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
381 return get_ctl_value(cval, GET_CUR, validx, value);
384 /* channel = 0: master, 1 = first channel */
385 static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
386 int channel, int *value)
388 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
391 static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
392 int channel, int index, int *value)
396 if (cval->cached & (1 << channel)) {
397 *value = cval->cache_val[index];
400 err = get_cur_mix_raw(cval, channel, value);
402 if (!cval->mixer->ignore_ctl_error)
403 snd_printd(KERN_ERR "cannot get current value for "
404 "control %d ch %d: err = %d\n",
405 cval->control, channel, err);
408 cval->cached |= 1 << channel;
409 cval->cache_val[index] = *value;
418 static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
420 unsigned char buf[2];
421 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
424 value_set = convert_bytes_value(cval, value_set);
425 buf[0] = value_set & 0xff;
426 buf[1] = (value_set >> 8) & 0xff;
427 while (timeout -- > 0)
428 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
429 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
431 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
432 validx, cval->mixer->ctrlif | (cval->id << 8),
433 buf, val_len, 100) >= 0)
435 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
436 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
440 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
442 return set_ctl_value(cval, SET_CUR, validx, value);
445 static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
446 int index, int value)
449 err = set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel,
453 cval->cached |= 1 << channel;
454 cval->cache_val[index] = value;
459 * TLV callback for mixer volume controls
461 static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
462 unsigned int size, unsigned int __user *_tlv)
464 struct usb_mixer_elem_info *cval = kcontrol->private_data;
465 DECLARE_TLV_DB_SCALE(scale, 0, 0, 0);
467 if (size < sizeof(scale))
469 /* USB descriptions contain the dB scale in 1/256 dB unit
470 * while ALSA TLV contains in 1/100 dB unit
472 scale[2] = (convert_signed_value(cval, cval->min) * 100) / 256;
473 scale[3] = (convert_signed_value(cval, cval->res) * 100) / 256;
474 if (copy_to_user(_tlv, scale, sizeof(scale)))
480 * parser routines begin here...
483 static int parse_audio_unit(struct mixer_build *state, int unitid);
487 * check if the input/output channel routing is enabled on the given bitmap.
488 * used for mixer unit parser
490 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
492 int idx = ich * num_outs + och;
493 return bmap[idx >> 3] & (0x80 >> (idx & 7));
498 * add an alsa control element
499 * search and increment the index until an empty slot is found.
501 * if failed, give up and free the control instance.
504 static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
506 struct usb_mixer_elem_info *cval = kctl->private_data;
509 while (snd_ctl_find_id(state->chip->card, &kctl->id))
511 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
512 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
515 cval->elem_id = &kctl->id;
516 cval->next_id_elem = state->mixer->id_elems[cval->id];
517 state->mixer->id_elems[cval->id] = cval;
523 * get a terminal name string
526 static struct iterm_name_combo {
530 { 0x0300, "Output" },
531 { 0x0301, "Speaker" },
532 { 0x0302, "Headphone" },
533 { 0x0303, "HMD Audio" },
534 { 0x0304, "Desktop Speaker" },
535 { 0x0305, "Room Speaker" },
536 { 0x0306, "Com Speaker" },
538 { 0x0600, "External In" },
539 { 0x0601, "Analog In" },
540 { 0x0602, "Digital In" },
542 { 0x0604, "Legacy In" },
543 { 0x0605, "IEC958 In" },
544 { 0x0606, "1394 DA Stream" },
545 { 0x0607, "1394 DV Stream" },
546 { 0x0700, "Embedded" },
547 { 0x0701, "Noise Source" },
548 { 0x0702, "Equalization Noise" },
552 { 0x0706, "MiniDisk" },
553 { 0x0707, "Analog Tape" },
554 { 0x0708, "Phonograph" },
555 { 0x0709, "VCR Audio" },
556 { 0x070a, "Video Disk Audio" },
557 { 0x070b, "DVD Audio" },
558 { 0x070c, "TV Tuner Audio" },
559 { 0x070d, "Satellite Rec Audio" },
560 { 0x070e, "Cable Tuner Audio" },
561 { 0x070f, "DSS Audio" },
562 { 0x0710, "Radio Receiver" },
563 { 0x0711, "Radio Transmitter" },
564 { 0x0712, "Multi-Track Recorder" },
565 { 0x0713, "Synthesizer" },
569 static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
570 unsigned char *name, int maxlen, int term_only)
572 struct iterm_name_combo *names;
575 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
577 /* virtual type - not a real terminal */
578 if (iterm->type >> 16) {
581 switch (iterm->type >> 16) {
583 strcpy(name, "Selector"); return 8;
584 case PROCESSING_UNIT:
585 strcpy(name, "Process Unit"); return 12;
587 strcpy(name, "Ext Unit"); return 8;
589 strcpy(name, "Mixer"); return 5;
591 return sprintf(name, "Unit %d", iterm->id);
595 switch (iterm->type & 0xff00) {
597 strcpy(name, "PCM"); return 3;
599 strcpy(name, "Mic"); return 3;
601 strcpy(name, "Headset"); return 7;
603 strcpy(name, "Phone"); return 5;
606 for (names = iterm_names; names->type; names++)
607 if (names->type == iterm->type) {
608 strcpy(name, names->name);
609 return strlen(names->name);
616 * parse the source unit recursively until it reaches to a terminal
617 * or a branched unit.
619 static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
623 memset(term, 0, sizeof(*term));
624 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
628 term->type = combine_word(p1 + 4);
629 term->channels = p1[7];
630 term->chconfig = combine_word(p1 + 8);
635 break; /* continue to parse */
637 term->type = p1[2] << 16; /* virtual type */
638 term->channels = p1[5 + p1[4]];
639 term->chconfig = combine_word(p1 + 6 + p1[4]);
640 term->name = p1[p1[0] - 1];
643 /* call recursively to retrieve the channel info */
644 if (check_input_term(state, p1[5], term) < 0)
646 term->type = p1[2] << 16; /* virtual type */
648 term->name = p1[9 + p1[0] - 1];
650 case PROCESSING_UNIT:
654 break; /* continue to parse */
656 term->type = p1[2] << 16; /* virtual type */
657 term->channels = p1[7 + p1[6]];
658 term->chconfig = combine_word(p1 + 8 + p1[6]);
659 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
673 /* feature unit control information */
674 struct usb_feature_control_info {
676 unsigned int type; /* control type (mute, volume, etc.) */
679 static struct usb_feature_control_info audio_feature_info[] = {
680 { "Mute", USB_MIXER_INV_BOOLEAN },
681 { "Volume", USB_MIXER_S16 },
682 { "Tone Control - Bass", USB_MIXER_S8 },
683 { "Tone Control - Mid", USB_MIXER_S8 },
684 { "Tone Control - Treble", USB_MIXER_S8 },
685 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
686 { "Auto Gain Control", USB_MIXER_BOOLEAN },
687 { "Delay Control", USB_MIXER_U16 },
688 { "Bass Boost", USB_MIXER_BOOLEAN },
689 { "Loudness", USB_MIXER_BOOLEAN },
693 /* private_free callback */
694 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
696 kfree(kctl->private_data);
697 kctl->private_data = NULL;
702 * interface to ALSA control for feature/mixer units
706 * retrieve the minimum and maximum values for the specified control
708 static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
711 cval->min = default_min;
712 cval->max = cval->min + 1;
715 if (cval->val_type == USB_MIXER_BOOLEAN ||
716 cval->val_type == USB_MIXER_INV_BOOLEAN) {
717 cval->initialized = 1;
722 for (i = 0; i < MAX_CHANNELS; i++)
723 if (cval->cmask & (1 << i)) {
728 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
729 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
730 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
731 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
734 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
737 int last_valid_res = cval->res;
739 while (cval->res > 1) {
740 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
744 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
745 cval->res = last_valid_res;
750 /* Additional checks for the proper resolution
752 * Some devices report smaller resolutions than actually
753 * reacting. They don't return errors but simply clip
754 * to the lower aligned value.
756 if (cval->min + cval->res < cval->max) {
757 int last_valid_res = cval->res;
758 int saved, test, check;
759 get_cur_mix_raw(cval, minchn, &saved);
762 if (test < cval->max)
766 if (test < cval->min || test > cval->max ||
767 set_cur_mix_value(cval, minchn, 0, test) ||
768 get_cur_mix_raw(cval, minchn, &check)) {
769 cval->res = last_valid_res;
776 set_cur_mix_value(cval, minchn, 0, saved);
779 cval->initialized = 1;
785 /* get a feature/mixer unit info */
786 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
788 struct usb_mixer_elem_info *cval = kcontrol->private_data;
790 if (cval->val_type == USB_MIXER_BOOLEAN ||
791 cval->val_type == USB_MIXER_INV_BOOLEAN)
792 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
794 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
795 uinfo->count = cval->channels;
796 if (cval->val_type == USB_MIXER_BOOLEAN ||
797 cval->val_type == USB_MIXER_INV_BOOLEAN) {
798 uinfo->value.integer.min = 0;
799 uinfo->value.integer.max = 1;
801 if (! cval->initialized)
802 get_min_max(cval, 0);
803 uinfo->value.integer.min = 0;
804 uinfo->value.integer.max =
805 (cval->max - cval->min + cval->res - 1) / cval->res;
810 /* get the current value from feature/mixer unit */
811 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
813 struct usb_mixer_elem_info *cval = kcontrol->private_data;
814 int c, cnt, val, err;
816 ucontrol->value.integer.value[0] = cval->min;
819 for (c = 0; c < MAX_CHANNELS; c++) {
820 if (!(cval->cmask & (1 << c)))
822 err = get_cur_mix_value(cval, c + 1, cnt, &val);
824 return cval->mixer->ignore_ctl_error ? 0 : err;
825 val = get_relative_value(cval, val);
826 ucontrol->value.integer.value[cnt] = val;
832 err = get_cur_mix_value(cval, 0, 0, &val);
834 return cval->mixer->ignore_ctl_error ? 0 : err;
835 val = get_relative_value(cval, val);
836 ucontrol->value.integer.value[0] = val;
841 /* put the current value to feature/mixer unit */
842 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
844 struct usb_mixer_elem_info *cval = kcontrol->private_data;
845 int c, cnt, val, oval, err;
850 for (c = 0; c < MAX_CHANNELS; c++) {
851 if (!(cval->cmask & (1 << c)))
853 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
855 return cval->mixer->ignore_ctl_error ? 0 : err;
856 val = ucontrol->value.integer.value[cnt];
857 val = get_abs_value(cval, val);
859 set_cur_mix_value(cval, c + 1, cnt, val);
866 err = get_cur_mix_value(cval, 0, 0, &oval);
868 return cval->mixer->ignore_ctl_error ? 0 : err;
869 val = ucontrol->value.integer.value[0];
870 val = get_abs_value(cval, val);
872 set_cur_mix_value(cval, 0, 0, val);
879 static struct snd_kcontrol_new usb_feature_unit_ctl = {
880 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
881 .name = "", /* will be filled later manually */
882 .info = mixer_ctl_feature_info,
883 .get = mixer_ctl_feature_get,
884 .put = mixer_ctl_feature_put,
889 * build a feature control
892 static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
893 unsigned int ctl_mask, int control,
894 struct usb_audio_term *iterm, int unitid)
896 unsigned int len = 0;
898 int nameid = desc[desc[0] - 1];
899 struct snd_kcontrol *kctl;
900 struct usb_mixer_elem_info *cval;
902 control++; /* change from zero-based to 1-based value */
904 if (control == USB_FEATURE_GEQ) {
905 /* FIXME: not supported yet */
909 if (check_ignored_ctl(state, unitid, control))
912 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
914 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
917 cval->mixer = state->mixer;
919 cval->control = control;
920 cval->cmask = ctl_mask;
921 cval->val_type = audio_feature_info[control-1].type;
923 cval->channels = 1; /* master channel */
926 for (i = 0; i < 16; i++)
927 if (ctl_mask & (1 << i))
932 /* get min/max values */
933 get_min_max(cval, 0);
935 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
937 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
941 kctl->private_free = usb_mixer_elem_free;
943 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
944 mapped_name = len != 0;
946 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
949 case USB_FEATURE_MUTE:
950 case USB_FEATURE_VOLUME:
951 /* determine the control name. the rule is:
952 * - if a name id is given in descriptor, use it.
953 * - if the connected input can be determined, then use the name
955 * - if the connected output can be determined, use it.
956 * - otherwise, anonymous name.
959 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
961 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
963 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
964 "Feature %d", unitid);
966 /* determine the stream direction:
967 * if the connected output is USB stream, then it's likely a
968 * capture stream. otherwise it should be playback (hopefully :)
970 if (! mapped_name && ! (state->oterm.type >> 16)) {
971 if ((state->oterm.type & 0xff00) == 0x0100) {
972 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
974 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
977 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
978 sizeof(kctl->id.name));
979 if (control == USB_FEATURE_VOLUME) {
980 kctl->tlv.c = mixer_vol_tlv;
981 kctl->vd[0].access |=
982 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
983 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
989 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
990 sizeof(kctl->id.name));
994 /* quirk for UDA1321/N101 */
995 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
996 /* is not very clear from datasheets */
997 /* I hope that the min value is -15360 for newer firmware --jk */
998 switch (state->chip->usb_id) {
999 case USB_ID(0x0471, 0x0101):
1000 case USB_ID(0x0471, 0x0104):
1001 case USB_ID(0x0471, 0x0105):
1002 case USB_ID(0x0672, 0x1041):
1003 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1004 cval->min == -15616) {
1005 snd_printk(KERN_INFO "using volume control quirk for the UDA1321/N101 chip\n");
1010 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1011 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
1012 add_control_to_empty(state, kctl);
1018 * parse a feature unit
1020 * most of controlls are defined here.
1022 static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
1025 struct usb_audio_term iterm;
1026 unsigned int master_bits, first_ch_bits;
1029 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1030 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1034 /* parse the source unit */
1035 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1038 /* determine the input source type and name */
1039 if (check_input_term(state, ftr[4], &iterm) < 0)
1042 channels = (ftr[0] - 7) / csize - 1;
1044 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
1046 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1049 /* check all control types */
1050 for (i = 0; i < 10; i++) {
1051 unsigned int ch_bits = 0;
1052 for (j = 0; j < channels; j++) {
1053 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1054 if (mask & (1 << i))
1055 ch_bits |= (1 << j);
1057 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1058 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1059 if (master_bits & (1 << i))
1060 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1072 * build a mixer unit control
1074 * the callbacks are identical with feature unit.
1075 * input channel number (zero based) is given in control field instead.
1078 static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
1079 int in_pin, int in_ch, int unitid,
1080 struct usb_audio_term *iterm)
1082 struct usb_mixer_elem_info *cval;
1083 unsigned int input_pins = desc[4];
1084 unsigned int num_outs = desc[5 + input_pins];
1085 unsigned int i, len;
1086 struct snd_kcontrol *kctl;
1088 if (check_ignored_ctl(state, unitid, 0))
1091 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1095 cval->mixer = state->mixer;
1097 cval->control = in_ch + 1; /* based on 1 */
1098 cval->val_type = USB_MIXER_S16;
1099 for (i = 0; i < num_outs; i++) {
1100 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1101 cval->cmask |= (1 << i);
1106 /* get min/max values */
1107 get_min_max(cval, 0);
1109 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1111 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1115 kctl->private_free = usb_mixer_elem_free;
1117 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1119 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1121 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1122 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1124 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1125 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1126 add_control_to_empty(state, kctl);
1131 * parse a mixer unit
1133 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1135 struct usb_audio_term iterm;
1136 int input_pins, num_ins, num_outs;
1139 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1140 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1143 /* no bmControls field (e.g. Maya44) -> ignore */
1144 if (desc[0] <= 10 + input_pins) {
1145 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1151 for (pin = 0; pin < input_pins; pin++) {
1152 err = parse_audio_unit(state, desc[5 + pin]);
1155 err = check_input_term(state, desc[5 + pin], &iterm);
1158 num_ins += iterm.channels;
1159 for (; ich < num_ins; ++ich) {
1160 int och, ich_has_controls = 0;
1162 for (och = 0; och < num_outs; ++och) {
1163 if (check_matrix_bitmap(desc + 9 + input_pins,
1164 ich, och, num_outs)) {
1165 ich_has_controls = 1;
1169 if (ich_has_controls)
1170 build_mixer_unit_ctl(state, desc, pin, ich,
1179 * Processing Unit / Extension Unit
1182 /* get callback for processing/extension unit */
1183 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1185 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1188 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1189 if (err < 0 && cval->mixer->ignore_ctl_error) {
1190 ucontrol->value.integer.value[0] = cval->min;
1195 val = get_relative_value(cval, val);
1196 ucontrol->value.integer.value[0] = val;
1200 /* put callback for processing/extension unit */
1201 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1203 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1206 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1208 if (cval->mixer->ignore_ctl_error)
1212 val = ucontrol->value.integer.value[0];
1213 val = get_abs_value(cval, val);
1215 set_cur_ctl_value(cval, cval->control << 8, val);
1221 /* alsa control interface for processing/extension unit */
1222 static struct snd_kcontrol_new mixer_procunit_ctl = {
1223 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1224 .name = "", /* will be filled later */
1225 .info = mixer_ctl_feature_info,
1226 .get = mixer_ctl_procunit_get,
1227 .put = mixer_ctl_procunit_put,
1232 * predefined data for processing units
1234 struct procunit_value_info {
1241 struct procunit_info {
1244 struct procunit_value_info *values;
1247 static struct procunit_value_info updown_proc_info[] = {
1248 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1249 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1252 static struct procunit_value_info prologic_proc_info[] = {
1253 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1254 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1257 static struct procunit_value_info threed_enh_proc_info[] = {
1258 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1259 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1262 static struct procunit_value_info reverb_proc_info[] = {
1263 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1264 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1265 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1266 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1269 static struct procunit_value_info chorus_proc_info[] = {
1270 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1271 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1272 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1273 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1276 static struct procunit_value_info dcr_proc_info[] = {
1277 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1278 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1279 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1280 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1281 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1282 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1286 static struct procunit_info procunits[] = {
1287 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1288 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1289 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1290 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1291 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1292 { USB_PROC_DCR, "DCR", dcr_proc_info },
1297 * build a processing/extension unit
1299 static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1301 int num_ins = dsc[6];
1302 struct usb_mixer_elem_info *cval;
1303 struct snd_kcontrol *kctl;
1304 int i, err, nameid, type, len;
1305 struct procunit_info *info;
1306 struct procunit_value_info *valinfo;
1307 static struct procunit_value_info default_value_info[] = {
1308 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1311 static struct procunit_info default_info = {
1312 0, NULL, default_value_info
1315 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1316 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1320 for (i = 0; i < num_ins; i++) {
1321 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1325 type = combine_word(&dsc[4]);
1326 for (info = list; info && info->type; info++)
1327 if (info->type == type)
1329 if (! info || ! info->type)
1330 info = &default_info;
1332 for (valinfo = info->values; valinfo->control; valinfo++) {
1333 /* FIXME: bitmap might be longer than 8bit */
1334 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1336 if (check_ignored_ctl(state, unitid, valinfo->control))
1338 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1340 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1343 cval->mixer = state->mixer;
1345 cval->control = valinfo->control;
1346 cval->val_type = valinfo->val_type;
1349 /* get min/max values */
1350 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1351 /* FIXME: hard-coded */
1353 cval->max = dsc[15];
1355 cval->initialized = 1;
1357 get_min_max(cval, valinfo->min_value);
1359 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1361 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1365 kctl->private_free = usb_mixer_elem_free;
1367 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1369 else if (info->name)
1370 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1372 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1375 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1377 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1379 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1380 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1382 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1383 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1384 if ((err = add_control_to_empty(state, kctl)) < 0)
1391 static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1393 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1396 static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1398 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1406 /* info callback for selector unit
1407 * use an enumerator type for routing
1409 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1411 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1412 char **itemlist = (char **)kcontrol->private_value;
1414 if (snd_BUG_ON(!itemlist))
1416 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1418 uinfo->value.enumerated.items = cval->max;
1419 if ((int)uinfo->value.enumerated.item >= cval->max)
1420 uinfo->value.enumerated.item = cval->max - 1;
1421 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1425 /* get callback for selector unit */
1426 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1428 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1431 err = get_cur_ctl_value(cval, 0, &val);
1433 if (cval->mixer->ignore_ctl_error) {
1434 ucontrol->value.enumerated.item[0] = 0;
1439 val = get_relative_value(cval, val);
1440 ucontrol->value.enumerated.item[0] = val;
1444 /* put callback for selector unit */
1445 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1447 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1450 err = get_cur_ctl_value(cval, 0, &oval);
1452 if (cval->mixer->ignore_ctl_error)
1456 val = ucontrol->value.enumerated.item[0];
1457 val = get_abs_value(cval, val);
1459 set_cur_ctl_value(cval, 0, val);
1465 /* alsa control interface for selector unit */
1466 static struct snd_kcontrol_new mixer_selectunit_ctl = {
1467 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1468 .name = "", /* will be filled later */
1469 .info = mixer_ctl_selector_info,
1470 .get = mixer_ctl_selector_get,
1471 .put = mixer_ctl_selector_put,
1475 /* private free callback.
1476 * free both private_data and private_value
1478 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
1482 if (kctl->private_data) {
1483 struct usb_mixer_elem_info *cval = kctl->private_data;
1484 num_ins = cval->max;
1486 kctl->private_data = NULL;
1488 if (kctl->private_value) {
1489 char **itemlist = (char **)kctl->private_value;
1490 for (i = 0; i < num_ins; i++)
1493 kctl->private_value = 0;
1498 * parse a selector unit
1500 static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1502 unsigned int num_ins = desc[4];
1503 unsigned int i, nameid, len;
1505 struct usb_mixer_elem_info *cval;
1506 struct snd_kcontrol *kctl;
1509 if (! num_ins || desc[0] < 5 + num_ins) {
1510 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1514 for (i = 0; i < num_ins; i++) {
1515 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1519 if (num_ins == 1) /* only one ? nonsense! */
1522 if (check_ignored_ctl(state, unitid, 0))
1525 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1527 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1530 cval->mixer = state->mixer;
1532 cval->val_type = USB_MIXER_U8;
1535 cval->max = num_ins;
1537 cval->initialized = 1;
1539 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1541 snd_printk(KERN_ERR "cannot malloc\n");
1545 #define MAX_ITEM_NAME_LEN 64
1546 for (i = 0; i < num_ins; i++) {
1547 struct usb_audio_term iterm;
1549 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1550 if (! namelist[i]) {
1551 snd_printk(KERN_ERR "cannot malloc\n");
1558 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1560 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1561 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1563 sprintf(namelist[i], "Input %d", i);
1566 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1568 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1573 kctl->private_value = (unsigned long)namelist;
1574 kctl->private_free = usb_mixer_selector_elem_free;
1576 nameid = desc[desc[0] - 1];
1577 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1581 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1583 len = get_term_name(state, &state->oterm,
1584 kctl->id.name, sizeof(kctl->id.name), 0);
1586 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1588 if ((state->oterm.type & 0xff00) == 0x0100)
1589 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1591 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1594 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1595 cval->id, kctl->id.name, num_ins);
1596 if ((err = add_control_to_empty(state, kctl)) < 0)
1604 * parse an audio unit recursively
1607 static int parse_audio_unit(struct mixer_build *state, int unitid)
1611 if (test_and_set_bit(unitid, state->unitbitmap))
1612 return 0; /* the unit already visited */
1614 p1 = find_audio_control_unit(state, unitid);
1616 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1621 case INPUT_TERMINAL:
1624 return parse_audio_mixer_unit(state, unitid, p1);
1626 return parse_audio_selector_unit(state, unitid, p1);
1628 return parse_audio_feature_unit(state, unitid, p1);
1629 case PROCESSING_UNIT:
1630 return parse_audio_processing_unit(state, unitid, p1);
1631 case EXTENSION_UNIT:
1632 return parse_audio_extension_unit(state, unitid, p1);
1634 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1639 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1641 kfree(mixer->id_elems);
1643 kfree(mixer->urb->transfer_buffer);
1644 usb_free_urb(mixer->urb);
1646 usb_free_urb(mixer->rc_urb);
1647 kfree(mixer->rc_setup_packet);
1651 static int snd_usb_mixer_dev_free(struct snd_device *device)
1653 struct usb_mixer_interface *mixer = device->device_data;
1654 snd_usb_mixer_free(mixer);
1659 * create mixer controls
1661 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1663 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1665 unsigned char *desc;
1666 struct mixer_build state;
1668 const struct usbmix_ctl_map *map;
1669 struct usb_host_interface *hostif;
1671 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1672 memset(&state, 0, sizeof(state));
1673 state.chip = mixer->chip;
1674 state.mixer = mixer;
1675 state.buffer = hostif->extra;
1676 state.buflen = hostif->extralen;
1678 /* check the mapping table */
1679 for (map = usbmix_ctl_maps; map->id; map++) {
1680 if (map->id == state.chip->usb_id) {
1681 state.map = map->map;
1682 state.selector_map = map->selector_map;
1683 mixer->ignore_ctl_error = map->ignore_ctl_error;
1689 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1691 continue; /* invalid descriptor? */
1692 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1693 state.oterm.id = desc[3];
1694 state.oterm.type = combine_word(&desc[4]);
1695 state.oterm.name = desc[8];
1696 err = parse_audio_unit(&state, desc[7]);
1703 static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1706 struct usb_mixer_elem_info *info;
1708 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1709 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1713 static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1718 /* unit ids specific to Extigy/Audigy 2 NX: */
1720 case 0: /* remote control */
1721 mixer->rc_urb->dev = mixer->chip->dev;
1722 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1724 case 4: /* digital in jack */
1725 case 7: /* line in jacks */
1726 case 19: /* speaker out jacks */
1727 case 20: /* headphones out jack */
1729 /* live24ext: 4 = line-in jack */
1730 case 3: /* hp-out jack (may actuate Mute) */
1731 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1732 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1733 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1736 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1741 static void snd_usb_mixer_status_complete(struct urb *urb)
1743 struct usb_mixer_interface *mixer = urb->context;
1745 if (urb->status == 0) {
1746 u8 *buf = urb->transfer_buffer;
1749 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1750 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1752 /* ignore any notifications not from the control interface */
1753 if ((buf[0] & 0x0f) != 0)
1755 if (!(buf[0] & 0x40))
1756 snd_usb_mixer_notify_id(mixer, buf[1]);
1758 snd_usb_mixer_memory_change(mixer, buf[1]);
1761 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1762 urb->dev = mixer->chip->dev;
1763 usb_submit_urb(urb, GFP_ATOMIC);
1767 /* create the handler for the optional status interrupt endpoint */
1768 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1770 struct usb_host_interface *hostif;
1771 struct usb_endpoint_descriptor *ep;
1772 void *transfer_buffer;
1776 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1777 /* we need one interrupt input endpoint */
1778 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1780 ep = get_endpoint(hostif, 0);
1781 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
1784 epnum = usb_endpoint_num(ep);
1785 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1786 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1787 if (!transfer_buffer)
1789 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1791 kfree(transfer_buffer);
1794 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1795 usb_rcvintpipe(mixer->chip->dev, epnum),
1796 transfer_buffer, buffer_length,
1797 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1798 usb_submit_urb(mixer->urb, GFP_KERNEL);
1802 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
1804 struct usb_mixer_interface *mixer = urb->context;
1805 const struct rc_config *rc = mixer->rc_cfg;
1808 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
1811 code = mixer->rc_buffer[rc->offset];
1812 if (rc->length == 2)
1813 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1815 /* the Mute button actually changes the mixer control */
1816 if (code == rc->mute_code)
1817 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
1818 mixer->rc_code = code;
1820 wake_up(&mixer->rc_waitq);
1823 static int snd_usb_sbrc_hwdep_open(struct snd_hwdep *hw, struct file *file)
1825 struct usb_mixer_interface *mixer = hw->private_data;
1827 if (test_and_set_bit(0, &mixer->rc_hwdep_open))
1832 static int snd_usb_sbrc_hwdep_release(struct snd_hwdep *hw, struct file *file)
1834 struct usb_mixer_interface *mixer = hw->private_data;
1836 clear_bit(0, &mixer->rc_hwdep_open);
1837 smp_mb__after_clear_bit();
1841 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
1842 long count, loff_t *offset)
1844 struct usb_mixer_interface *mixer = hw->private_data;
1848 if (count != 1 && count != 4)
1850 err = wait_event_interruptible(mixer->rc_waitq,
1851 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1854 err = put_user(rc_code, buf);
1856 err = put_user(rc_code, (u32 __user *)buf);
1858 return err < 0 ? err : count;
1861 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
1864 struct usb_mixer_interface *mixer = hw->private_data;
1866 poll_wait(file, &mixer->rc_waitq, wait);
1867 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1870 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1872 struct snd_hwdep *hwdep;
1875 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1876 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1878 if (i >= ARRAY_SIZE(rc_configs))
1880 mixer->rc_cfg = &rc_configs[i];
1882 len = mixer->rc_cfg->packet_length;
1884 init_waitqueue_head(&mixer->rc_waitq);
1885 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1888 snprintf(hwdep->name, sizeof(hwdep->name),
1889 "%s remote control", mixer->chip->card->shortname);
1890 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1891 hwdep->private_data = mixer;
1892 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1893 hwdep->ops.open = snd_usb_sbrc_hwdep_open;
1894 hwdep->ops.release = snd_usb_sbrc_hwdep_release;
1895 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1897 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1900 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1901 if (!mixer->rc_setup_packet) {
1902 usb_free_urb(mixer->rc_urb);
1903 mixer->rc_urb = NULL;
1906 mixer->rc_setup_packet->bRequestType =
1907 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1908 mixer->rc_setup_packet->bRequest = GET_MEM;
1909 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1910 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1911 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1912 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1913 usb_rcvctrlpipe(mixer->chip->dev, 0),
1914 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1915 snd_usb_soundblaster_remote_complete, mixer);
1919 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
1921 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1923 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1924 int index = kcontrol->private_value;
1926 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1930 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1932 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1933 int index = kcontrol->private_value;
1934 int value = ucontrol->value.integer.value[0];
1939 changed = value != mixer->audigy2nx_leds[index];
1940 err = snd_usb_ctl_msg(mixer->chip->dev,
1941 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1942 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1943 value, index + 2, NULL, 0, 100);
1946 mixer->audigy2nx_leds[index] = value;
1950 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
1952 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1953 .name = "CMSS LED Switch",
1954 .info = snd_audigy2nx_led_info,
1955 .get = snd_audigy2nx_led_get,
1956 .put = snd_audigy2nx_led_put,
1960 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1961 .name = "Power LED Switch",
1962 .info = snd_audigy2nx_led_info,
1963 .get = snd_audigy2nx_led_get,
1964 .put = snd_audigy2nx_led_put,
1968 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1969 .name = "Dolby Digital LED Switch",
1970 .info = snd_audigy2nx_led_info,
1971 .get = snd_audigy2nx_led_get,
1972 .put = snd_audigy2nx_led_put,
1977 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1981 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
1982 if (i > 1 && /* Live24ext has 2 LEDs only */
1983 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1984 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
1986 err = snd_ctl_add(mixer->chip->card,
1987 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1991 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1995 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
1996 struct snd_info_buffer *buffer)
1998 static const struct sb_jack {
2001 } jacks_audigy2nx[] = {
2007 }, jacks_live24ext[] = {
2008 {4, "line in"}, /* &1=Line, &2=Mic*/
2009 {3, "hph out"}, /* headphones */
2010 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2013 const struct sb_jack *jacks;
2014 struct usb_mixer_interface *mixer = entry->private_data;
2018 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
2019 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2020 jacks = jacks_audigy2nx;
2021 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2022 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
2023 jacks = jacks_live24ext;
2027 for (i = 0; jacks[i].name; ++i) {
2028 snd_iprintf(buffer, "%s: ", jacks[i].name);
2029 err = snd_usb_ctl_msg(mixer->chip->dev,
2030 usb_rcvctrlpipe(mixer->chip->dev, 0),
2031 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2032 USB_RECIP_INTERFACE, 0,
2033 jacks[i].unitid << 8, buf, 3, 100);
2034 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
2035 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2037 snd_iprintf(buffer, "?\n");
2041 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2044 static struct snd_device_ops dev_ops = {
2045 .dev_free = snd_usb_mixer_dev_free
2047 struct usb_mixer_interface *mixer;
2050 strcpy(chip->card->mixername, "USB Mixer");
2052 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
2056 mixer->ctrlif = ctrlif;
2057 mixer->ignore_ctl_error = ignore_error;
2058 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2059 if (!mixer->id_elems) {
2064 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
2065 (err = snd_usb_mixer_status_create(mixer)) < 0)
2068 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2071 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
2072 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2073 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
2074 struct snd_info_entry *entry;
2076 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2078 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
2079 snd_info_set_text_ops(entry, mixer,
2080 snd_audigy2nx_proc_read);
2083 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
2086 list_add(&mixer->list, &chip->mixer_list);
2090 snd_usb_mixer_free(mixer);
2094 void snd_usb_mixer_disconnect(struct list_head *p)
2096 struct usb_mixer_interface *mixer;
2098 mixer = list_entry(p, struct usb_mixer_interface, list);
2099 usb_kill_urb(mixer->urb);
2100 usb_kill_urb(mixer->rc_urb);