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 struct usb_mixer_elem_info {
115 struct usb_mixer_interface *mixer;
116 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
117 struct snd_ctl_elem_id *elem_id;
119 unsigned int control; /* CS or ICN (high byte) */
120 unsigned int cmask; /* channel mask bitmap: 0 = master */
129 USB_FEATURE_NONE = 0,
130 USB_FEATURE_MUTE = 1,
138 USB_FEATURE_BASSBOOST,
144 USB_MIXER_INV_BOOLEAN,
153 USB_PROC_UPDOWN_SWITCH = 1,
154 USB_PROC_UPDOWN_MODE_SEL = 2,
156 USB_PROC_PROLOGIC = 2,
157 USB_PROC_PROLOGIC_SWITCH = 1,
158 USB_PROC_PROLOGIC_MODE_SEL = 2,
161 USB_PROC_3DENH_SWITCH = 1,
162 USB_PROC_3DENH_SPACE = 2,
165 USB_PROC_REVERB_SWITCH = 1,
166 USB_PROC_REVERB_LEVEL = 2,
167 USB_PROC_REVERB_TIME = 3,
168 USB_PROC_REVERB_DELAY = 4,
171 USB_PROC_CHORUS_SWITCH = 1,
172 USB_PROC_CHORUS_LEVEL = 2,
173 USB_PROC_CHORUS_RATE = 3,
174 USB_PROC_CHORUS_DEPTH = 4,
177 USB_PROC_DCR_SWITCH = 1,
178 USB_PROC_DCR_RATIO = 2,
179 USB_PROC_DCR_MAX_AMP = 3,
180 USB_PROC_DCR_THRESHOLD = 4,
181 USB_PROC_DCR_ATTACK = 5,
182 USB_PROC_DCR_RELEASE = 6,
185 #define MAX_CHANNELS 10 /* max logical channels */
189 * manual mapping of mixer names
190 * if the mixer topology is too complicated and the parsed names are
191 * ambiguous, add the entries in usbmixer_maps.c.
193 #include "usbmixer_maps.c"
195 /* get the mapped name if the unit matches */
196 static int check_mapped_name(struct mixer_build *state, int unitid, int control, char *buf, int buflen)
198 const struct usbmix_name_map *p;
203 for (p = state->map; p->id; p++) {
204 if (p->id == unitid && p->name &&
205 (! control || ! p->control || control == p->control)) {
207 return strlcpy(buf, p->name, buflen);
213 /* check whether the control should be ignored */
214 static int check_ignored_ctl(struct mixer_build *state, int unitid, int control)
216 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)) {
223 // printk("ignored control %d:%d\n", unitid, control);
230 /* get the mapped selector source name */
231 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
232 int index, char *buf, int buflen)
234 const struct usbmix_selector_map *p;
236 if (! state->selector_map)
238 for (p = state->selector_map; p->id; p++) {
239 if (p->id == unitid && index < p->count)
240 return strlcpy(buf, p->names[index], buflen);
246 * find an audio control unit with the given unit id
248 static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
253 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
254 USB_DT_CS_INTERFACE)) != NULL) {
255 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
263 * copy a string with the given id
265 static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
267 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
273 * convert from the byte/word on usb descriptor to the zero-based integer
275 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
277 switch (cval->val_type) {
278 case USB_MIXER_BOOLEAN:
280 case USB_MIXER_INV_BOOLEAN:
303 * convert from the zero-based int to the byte/word for usb descriptor
305 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
307 switch (cval->val_type) {
308 case USB_MIXER_BOOLEAN:
310 case USB_MIXER_INV_BOOLEAN:
319 return 0; /* not reached */
322 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
328 else if (val >= cval->max)
329 return (cval->max - cval->min + cval->res - 1) / cval->res;
331 return (val - cval->min) / cval->res;
334 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
349 * retrieve a mixer value
352 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
354 unsigned char buf[2];
355 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
358 while (timeout-- > 0) {
359 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
360 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
362 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
363 validx, cval->mixer->ctrlif | (cval->id << 8),
364 buf, val_len, 100) >= val_len) {
365 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
369 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
370 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
374 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
376 return get_ctl_value(cval, GET_CUR, validx, value);
379 /* channel = 0: master, 1 = first channel */
380 static inline int get_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int *value)
382 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
389 static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
391 unsigned char buf[2];
392 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
395 value_set = convert_bytes_value(cval, value_set);
396 buf[0] = value_set & 0xff;
397 buf[1] = (value_set >> 8) & 0xff;
398 while (timeout -- > 0)
399 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
400 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
402 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
403 validx, cval->mixer->ctrlif | (cval->id << 8),
404 buf, val_len, 100) >= 0)
406 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
407 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
411 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
413 return set_ctl_value(cval, SET_CUR, validx, value);
416 static inline int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int value)
418 return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
422 * TLV callback for mixer volume controls
424 static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
425 unsigned int size, unsigned int __user *_tlv)
427 struct usb_mixer_elem_info *cval = kcontrol->private_data;
428 DECLARE_TLV_DB_SCALE(scale, 0, 0, 0);
430 if (size < sizeof(scale))
432 /* USB descriptions contain the dB scale in 1/256 dB unit
433 * while ALSA TLV contains in 1/100 dB unit
435 scale[2] = (convert_signed_value(cval, cval->min) * 100) / 256;
436 scale[3] = (convert_signed_value(cval, cval->res) * 100) / 256;
437 if (copy_to_user(_tlv, scale, sizeof(scale)))
443 * parser routines begin here...
446 static int parse_audio_unit(struct mixer_build *state, int unitid);
450 * check if the input/output channel routing is enabled on the given bitmap.
451 * used for mixer unit parser
453 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
455 int idx = ich * num_outs + och;
456 return bmap[idx >> 3] & (0x80 >> (idx & 7));
461 * add an alsa control element
462 * search and increment the index until an empty slot is found.
464 * if failed, give up and free the control instance.
467 static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
469 struct usb_mixer_elem_info *cval = kctl->private_data;
472 while (snd_ctl_find_id(state->chip->card, &kctl->id))
474 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
475 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
478 cval->elem_id = &kctl->id;
479 cval->next_id_elem = state->mixer->id_elems[cval->id];
480 state->mixer->id_elems[cval->id] = cval;
486 * get a terminal name string
489 static struct iterm_name_combo {
493 { 0x0300, "Output" },
494 { 0x0301, "Speaker" },
495 { 0x0302, "Headphone" },
496 { 0x0303, "HMD Audio" },
497 { 0x0304, "Desktop Speaker" },
498 { 0x0305, "Room Speaker" },
499 { 0x0306, "Com Speaker" },
501 { 0x0600, "External In" },
502 { 0x0601, "Analog In" },
503 { 0x0602, "Digital In" },
505 { 0x0604, "Legacy In" },
506 { 0x0605, "IEC958 In" },
507 { 0x0606, "1394 DA Stream" },
508 { 0x0607, "1394 DV Stream" },
509 { 0x0700, "Embedded" },
510 { 0x0701, "Noise Source" },
511 { 0x0702, "Equalization Noise" },
515 { 0x0706, "MiniDisk" },
516 { 0x0707, "Analog Tape" },
517 { 0x0708, "Phonograph" },
518 { 0x0709, "VCR Audio" },
519 { 0x070a, "Video Disk Audio" },
520 { 0x070b, "DVD Audio" },
521 { 0x070c, "TV Tuner Audio" },
522 { 0x070d, "Satellite Rec Audio" },
523 { 0x070e, "Cable Tuner Audio" },
524 { 0x070f, "DSS Audio" },
525 { 0x0710, "Radio Receiver" },
526 { 0x0711, "Radio Transmitter" },
527 { 0x0712, "Multi-Track Recorder" },
528 { 0x0713, "Synthesizer" },
532 static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
533 unsigned char *name, int maxlen, int term_only)
535 struct iterm_name_combo *names;
538 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
540 /* virtual type - not a real terminal */
541 if (iterm->type >> 16) {
544 switch (iterm->type >> 16) {
546 strcpy(name, "Selector"); return 8;
547 case PROCESSING_UNIT:
548 strcpy(name, "Process Unit"); return 12;
550 strcpy(name, "Ext Unit"); return 8;
552 strcpy(name, "Mixer"); return 5;
554 return sprintf(name, "Unit %d", iterm->id);
558 switch (iterm->type & 0xff00) {
560 strcpy(name, "PCM"); return 3;
562 strcpy(name, "Mic"); return 3;
564 strcpy(name, "Headset"); return 7;
566 strcpy(name, "Phone"); return 5;
569 for (names = iterm_names; names->type; names++)
570 if (names->type == iterm->type) {
571 strcpy(name, names->name);
572 return strlen(names->name);
579 * parse the source unit recursively until it reaches to a terminal
580 * or a branched unit.
582 static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
586 memset(term, 0, sizeof(*term));
587 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
591 term->type = combine_word(p1 + 4);
592 term->channels = p1[7];
593 term->chconfig = combine_word(p1 + 8);
598 break; /* continue to parse */
600 term->type = p1[2] << 16; /* virtual type */
601 term->channels = p1[5 + p1[4]];
602 term->chconfig = combine_word(p1 + 6 + p1[4]);
603 term->name = p1[p1[0] - 1];
606 /* call recursively to retrieve the channel info */
607 if (check_input_term(state, p1[5], term) < 0)
609 term->type = p1[2] << 16; /* virtual type */
611 term->name = p1[9 + p1[0] - 1];
613 case PROCESSING_UNIT:
617 break; /* continue to parse */
619 term->type = p1[2] << 16; /* virtual type */
620 term->channels = p1[7 + p1[6]];
621 term->chconfig = combine_word(p1 + 8 + p1[6]);
622 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
636 /* feature unit control information */
637 struct usb_feature_control_info {
639 unsigned int type; /* control type (mute, volume, etc.) */
642 static struct usb_feature_control_info audio_feature_info[] = {
643 { "Mute", USB_MIXER_INV_BOOLEAN },
644 { "Volume", USB_MIXER_S16 },
645 { "Tone Control - Bass", USB_MIXER_S8 },
646 { "Tone Control - Mid", USB_MIXER_S8 },
647 { "Tone Control - Treble", USB_MIXER_S8 },
648 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
649 { "Auto Gain Control", USB_MIXER_BOOLEAN },
650 { "Delay Control", USB_MIXER_U16 },
651 { "Bass Boost", USB_MIXER_BOOLEAN },
652 { "Loudness", USB_MIXER_BOOLEAN },
656 /* private_free callback */
657 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
659 kfree(kctl->private_data);
660 kctl->private_data = NULL;
665 * interface to ALSA control for feature/mixer units
669 * retrieve the minimum and maximum values for the specified control
671 static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
674 cval->min = default_min;
675 cval->max = cval->min + 1;
678 if (cval->val_type == USB_MIXER_BOOLEAN ||
679 cval->val_type == USB_MIXER_INV_BOOLEAN) {
680 cval->initialized = 1;
685 for (i = 0; i < MAX_CHANNELS; i++)
686 if (cval->cmask & (1 << i)) {
691 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
692 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
693 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
694 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
697 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
700 int last_valid_res = cval->res;
702 while (cval->res > 1) {
703 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
707 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
708 cval->res = last_valid_res;
713 /* Additional checks for the proper resolution
715 * Some devices report smaller resolutions than actually
716 * reacting. They don't return errors but simply clip
717 * to the lower aligned value.
719 if (cval->min + cval->res < cval->max) {
720 int last_valid_res = cval->res;
721 int saved, test, check;
722 get_cur_mix_value(cval, minchn, &saved);
725 if (test < cval->max)
729 if (test < cval->min || test > cval->max ||
730 set_cur_mix_value(cval, minchn, test) ||
731 get_cur_mix_value(cval, minchn, &check)) {
732 cval->res = last_valid_res;
739 set_cur_mix_value(cval, minchn, saved);
742 cval->initialized = 1;
748 /* get a feature/mixer unit info */
749 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
751 struct usb_mixer_elem_info *cval = kcontrol->private_data;
753 if (cval->val_type == USB_MIXER_BOOLEAN ||
754 cval->val_type == USB_MIXER_INV_BOOLEAN)
755 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
757 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
758 uinfo->count = cval->channels;
759 if (cval->val_type == USB_MIXER_BOOLEAN ||
760 cval->val_type == USB_MIXER_INV_BOOLEAN) {
761 uinfo->value.integer.min = 0;
762 uinfo->value.integer.max = 1;
764 if (! cval->initialized)
765 get_min_max(cval, 0);
766 uinfo->value.integer.min = 0;
767 uinfo->value.integer.max =
768 (cval->max - cval->min + cval->res - 1) / cval->res;
773 /* get the current value from feature/mixer unit */
774 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
776 struct usb_mixer_elem_info *cval = kcontrol->private_data;
777 int c, cnt, val, err;
781 for (c = 0; c < MAX_CHANNELS; c++) {
782 if (cval->cmask & (1 << c)) {
783 err = get_cur_mix_value(cval, c + 1, &val);
785 if (cval->mixer->ignore_ctl_error) {
786 ucontrol->value.integer.value[0] = cval->min;
789 snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
792 val = get_relative_value(cval, val);
793 ucontrol->value.integer.value[cnt] = val;
799 err = get_cur_mix_value(cval, 0, &val);
801 if (cval->mixer->ignore_ctl_error) {
802 ucontrol->value.integer.value[0] = cval->min;
805 snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
808 val = get_relative_value(cval, val);
809 ucontrol->value.integer.value[0] = val;
814 /* put the current value to feature/mixer unit */
815 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
817 struct usb_mixer_elem_info *cval = kcontrol->private_data;
818 int c, cnt, val, oval, err;
823 for (c = 0; c < MAX_CHANNELS; c++) {
824 if (cval->cmask & (1 << c)) {
825 err = get_cur_mix_value(cval, c + 1, &oval);
827 if (cval->mixer->ignore_ctl_error)
831 val = ucontrol->value.integer.value[cnt];
832 val = get_abs_value(cval, val);
834 set_cur_mix_value(cval, c + 1, val);
837 get_cur_mix_value(cval, c + 1, &val);
843 err = get_cur_mix_value(cval, 0, &oval);
844 if (err < 0 && cval->mixer->ignore_ctl_error)
848 val = ucontrol->value.integer.value[0];
849 val = get_abs_value(cval, val);
851 set_cur_mix_value(cval, 0, val);
858 static struct snd_kcontrol_new usb_feature_unit_ctl = {
859 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
860 .name = "", /* will be filled later manually */
861 .info = mixer_ctl_feature_info,
862 .get = mixer_ctl_feature_get,
863 .put = mixer_ctl_feature_put,
868 * build a feature control
871 static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
872 unsigned int ctl_mask, int control,
873 struct usb_audio_term *iterm, int unitid)
875 unsigned int len = 0;
877 int nameid = desc[desc[0] - 1];
878 struct snd_kcontrol *kctl;
879 struct usb_mixer_elem_info *cval;
881 control++; /* change from zero-based to 1-based value */
883 if (control == USB_FEATURE_GEQ) {
884 /* FIXME: not supported yet */
888 if (check_ignored_ctl(state, unitid, control))
891 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
893 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
896 cval->mixer = state->mixer;
898 cval->control = control;
899 cval->cmask = ctl_mask;
900 cval->val_type = audio_feature_info[control-1].type;
902 cval->channels = 1; /* master channel */
905 for (i = 0; i < 16; i++)
906 if (ctl_mask & (1 << i))
911 /* get min/max values */
912 get_min_max(cval, 0);
914 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
916 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
920 kctl->private_free = usb_mixer_elem_free;
922 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
923 mapped_name = len != 0;
925 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
928 case USB_FEATURE_MUTE:
929 case USB_FEATURE_VOLUME:
930 /* determine the control name. the rule is:
931 * - if a name id is given in descriptor, use it.
932 * - if the connected input can be determined, then use the name
934 * - if the connected output can be determined, use it.
935 * - otherwise, anonymous name.
938 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
940 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
942 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
943 "Feature %d", unitid);
945 /* determine the stream direction:
946 * if the connected output is USB stream, then it's likely a
947 * capture stream. otherwise it should be playback (hopefully :)
949 if (! mapped_name && ! (state->oterm.type >> 16)) {
950 if ((state->oterm.type & 0xff00) == 0x0100) {
951 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
953 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
956 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
957 sizeof(kctl->id.name));
958 if (control == USB_FEATURE_VOLUME) {
959 kctl->tlv.c = mixer_vol_tlv;
960 kctl->vd[0].access |=
961 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
962 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
968 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
969 sizeof(kctl->id.name));
973 /* quirk for UDA1321/N101 */
974 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
975 /* is not very clear from datasheets */
976 /* I hope that the min value is -15360 for newer firmware --jk */
977 switch (state->chip->usb_id) {
978 case USB_ID(0x0471, 0x0101):
979 case USB_ID(0x0471, 0x0104):
980 case USB_ID(0x0471, 0x0105):
981 case USB_ID(0x0672, 0x1041):
982 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
983 cval->min == -15616) {
984 snd_printk(KERN_INFO "using volume control quirk for the UDA1321/N101 chip\n");
989 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
990 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
991 add_control_to_empty(state, kctl);
997 * parse a feature unit
999 * most of controlls are defined here.
1001 static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
1004 struct usb_audio_term iterm;
1005 unsigned int master_bits, first_ch_bits;
1008 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1009 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1013 /* parse the source unit */
1014 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1017 /* determine the input source type and name */
1018 if (check_input_term(state, ftr[4], &iterm) < 0)
1021 channels = (ftr[0] - 7) / csize - 1;
1023 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
1025 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1028 /* check all control types */
1029 for (i = 0; i < 10; i++) {
1030 unsigned int ch_bits = 0;
1031 for (j = 0; j < channels; j++) {
1032 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1033 if (mask & (1 << i))
1034 ch_bits |= (1 << j);
1036 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1037 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1038 if (master_bits & (1 << i))
1039 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1051 * build a mixer unit control
1053 * the callbacks are identical with feature unit.
1054 * input channel number (zero based) is given in control field instead.
1057 static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
1058 int in_pin, int in_ch, int unitid,
1059 struct usb_audio_term *iterm)
1061 struct usb_mixer_elem_info *cval;
1062 unsigned int input_pins = desc[4];
1063 unsigned int num_outs = desc[5 + input_pins];
1064 unsigned int i, len;
1065 struct snd_kcontrol *kctl;
1067 if (check_ignored_ctl(state, unitid, 0))
1070 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1074 cval->mixer = state->mixer;
1076 cval->control = in_ch + 1; /* based on 1 */
1077 cval->val_type = USB_MIXER_S16;
1078 for (i = 0; i < num_outs; i++) {
1079 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1080 cval->cmask |= (1 << i);
1085 /* get min/max values */
1086 get_min_max(cval, 0);
1088 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1090 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1094 kctl->private_free = usb_mixer_elem_free;
1096 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1098 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1100 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1101 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1103 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1104 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1105 add_control_to_empty(state, kctl);
1110 * parse a mixer unit
1112 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1114 struct usb_audio_term iterm;
1115 int input_pins, num_ins, num_outs;
1118 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1119 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1122 /* no bmControls field (e.g. Maya44) -> ignore */
1123 if (desc[0] <= 10 + input_pins) {
1124 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1130 for (pin = 0; pin < input_pins; pin++) {
1131 err = parse_audio_unit(state, desc[5 + pin]);
1134 err = check_input_term(state, desc[5 + pin], &iterm);
1137 num_ins += iterm.channels;
1138 for (; ich < num_ins; ++ich) {
1139 int och, ich_has_controls = 0;
1141 for (och = 0; och < num_outs; ++och) {
1142 if (check_matrix_bitmap(desc + 9 + input_pins,
1143 ich, och, num_outs)) {
1144 ich_has_controls = 1;
1148 if (ich_has_controls)
1149 build_mixer_unit_ctl(state, desc, pin, ich,
1158 * Processing Unit / Extension Unit
1161 /* get callback for processing/extension unit */
1162 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1164 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1167 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1168 if (err < 0 && cval->mixer->ignore_ctl_error) {
1169 ucontrol->value.integer.value[0] = cval->min;
1174 val = get_relative_value(cval, val);
1175 ucontrol->value.integer.value[0] = val;
1179 /* put callback for processing/extension unit */
1180 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1182 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1185 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1187 if (cval->mixer->ignore_ctl_error)
1191 val = ucontrol->value.integer.value[0];
1192 val = get_abs_value(cval, val);
1194 set_cur_ctl_value(cval, cval->control << 8, val);
1200 /* alsa control interface for processing/extension unit */
1201 static struct snd_kcontrol_new mixer_procunit_ctl = {
1202 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1203 .name = "", /* will be filled later */
1204 .info = mixer_ctl_feature_info,
1205 .get = mixer_ctl_procunit_get,
1206 .put = mixer_ctl_procunit_put,
1211 * predefined data for processing units
1213 struct procunit_value_info {
1220 struct procunit_info {
1223 struct procunit_value_info *values;
1226 static struct procunit_value_info updown_proc_info[] = {
1227 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1228 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1231 static struct procunit_value_info prologic_proc_info[] = {
1232 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1233 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1236 static struct procunit_value_info threed_enh_proc_info[] = {
1237 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1238 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1241 static struct procunit_value_info reverb_proc_info[] = {
1242 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1243 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1244 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1245 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1248 static struct procunit_value_info chorus_proc_info[] = {
1249 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1250 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1251 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1252 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1255 static struct procunit_value_info dcr_proc_info[] = {
1256 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1257 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1258 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1259 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1260 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1261 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1265 static struct procunit_info procunits[] = {
1266 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1267 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1268 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1269 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1270 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1271 { USB_PROC_DCR, "DCR", dcr_proc_info },
1276 * build a processing/extension unit
1278 static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1280 int num_ins = dsc[6];
1281 struct usb_mixer_elem_info *cval;
1282 struct snd_kcontrol *kctl;
1283 int i, err, nameid, type, len;
1284 struct procunit_info *info;
1285 struct procunit_value_info *valinfo;
1286 static struct procunit_value_info default_value_info[] = {
1287 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1290 static struct procunit_info default_info = {
1291 0, NULL, default_value_info
1294 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1295 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1299 for (i = 0; i < num_ins; i++) {
1300 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1304 type = combine_word(&dsc[4]);
1305 for (info = list; info && info->type; info++)
1306 if (info->type == type)
1308 if (! info || ! info->type)
1309 info = &default_info;
1311 for (valinfo = info->values; valinfo->control; valinfo++) {
1312 /* FIXME: bitmap might be longer than 8bit */
1313 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1315 if (check_ignored_ctl(state, unitid, valinfo->control))
1317 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1319 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1322 cval->mixer = state->mixer;
1324 cval->control = valinfo->control;
1325 cval->val_type = valinfo->val_type;
1328 /* get min/max values */
1329 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1330 /* FIXME: hard-coded */
1332 cval->max = dsc[15];
1334 cval->initialized = 1;
1336 get_min_max(cval, valinfo->min_value);
1338 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1340 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1344 kctl->private_free = usb_mixer_elem_free;
1346 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1348 else if (info->name)
1349 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1351 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1354 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1356 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1358 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1359 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1361 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1362 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1363 if ((err = add_control_to_empty(state, kctl)) < 0)
1370 static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1372 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1375 static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1377 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1385 /* info callback for selector unit
1386 * use an enumerator type for routing
1388 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1390 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1391 char **itemlist = (char **)kcontrol->private_value;
1393 if (snd_BUG_ON(!itemlist))
1395 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1397 uinfo->value.enumerated.items = cval->max;
1398 if ((int)uinfo->value.enumerated.item >= cval->max)
1399 uinfo->value.enumerated.item = cval->max - 1;
1400 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1404 /* get callback for selector unit */
1405 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1407 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1410 err = get_cur_ctl_value(cval, 0, &val);
1412 if (cval->mixer->ignore_ctl_error) {
1413 ucontrol->value.enumerated.item[0] = 0;
1418 val = get_relative_value(cval, val);
1419 ucontrol->value.enumerated.item[0] = val;
1423 /* put callback for selector unit */
1424 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1426 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1429 err = get_cur_ctl_value(cval, 0, &oval);
1431 if (cval->mixer->ignore_ctl_error)
1435 val = ucontrol->value.enumerated.item[0];
1436 val = get_abs_value(cval, val);
1438 set_cur_ctl_value(cval, 0, val);
1444 /* alsa control interface for selector unit */
1445 static struct snd_kcontrol_new mixer_selectunit_ctl = {
1446 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1447 .name = "", /* will be filled later */
1448 .info = mixer_ctl_selector_info,
1449 .get = mixer_ctl_selector_get,
1450 .put = mixer_ctl_selector_put,
1454 /* private free callback.
1455 * free both private_data and private_value
1457 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
1461 if (kctl->private_data) {
1462 struct usb_mixer_elem_info *cval = kctl->private_data;
1463 num_ins = cval->max;
1465 kctl->private_data = NULL;
1467 if (kctl->private_value) {
1468 char **itemlist = (char **)kctl->private_value;
1469 for (i = 0; i < num_ins; i++)
1472 kctl->private_value = 0;
1477 * parse a selector unit
1479 static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1481 unsigned int num_ins = desc[4];
1482 unsigned int i, nameid, len;
1484 struct usb_mixer_elem_info *cval;
1485 struct snd_kcontrol *kctl;
1488 if (! num_ins || desc[0] < 5 + num_ins) {
1489 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1493 for (i = 0; i < num_ins; i++) {
1494 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1498 if (num_ins == 1) /* only one ? nonsense! */
1501 if (check_ignored_ctl(state, unitid, 0))
1504 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1506 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1509 cval->mixer = state->mixer;
1511 cval->val_type = USB_MIXER_U8;
1514 cval->max = num_ins;
1516 cval->initialized = 1;
1518 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1520 snd_printk(KERN_ERR "cannot malloc\n");
1524 #define MAX_ITEM_NAME_LEN 64
1525 for (i = 0; i < num_ins; i++) {
1526 struct usb_audio_term iterm;
1528 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1529 if (! namelist[i]) {
1530 snd_printk(KERN_ERR "cannot malloc\n");
1537 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1539 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1540 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1542 sprintf(namelist[i], "Input %d", i);
1545 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1547 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1552 kctl->private_value = (unsigned long)namelist;
1553 kctl->private_free = usb_mixer_selector_elem_free;
1555 nameid = desc[desc[0] - 1];
1556 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1560 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1562 len = get_term_name(state, &state->oterm,
1563 kctl->id.name, sizeof(kctl->id.name), 0);
1565 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1567 if ((state->oterm.type & 0xff00) == 0x0100)
1568 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1570 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1573 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1574 cval->id, kctl->id.name, num_ins);
1575 if ((err = add_control_to_empty(state, kctl)) < 0)
1583 * parse an audio unit recursively
1586 static int parse_audio_unit(struct mixer_build *state, int unitid)
1590 if (test_and_set_bit(unitid, state->unitbitmap))
1591 return 0; /* the unit already visited */
1593 p1 = find_audio_control_unit(state, unitid);
1595 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1600 case INPUT_TERMINAL:
1603 return parse_audio_mixer_unit(state, unitid, p1);
1605 return parse_audio_selector_unit(state, unitid, p1);
1607 return parse_audio_feature_unit(state, unitid, p1);
1608 case PROCESSING_UNIT:
1609 return parse_audio_processing_unit(state, unitid, p1);
1610 case EXTENSION_UNIT:
1611 return parse_audio_extension_unit(state, unitid, p1);
1613 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1618 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1620 kfree(mixer->id_elems);
1622 kfree(mixer->urb->transfer_buffer);
1623 usb_free_urb(mixer->urb);
1625 usb_free_urb(mixer->rc_urb);
1626 kfree(mixer->rc_setup_packet);
1630 static int snd_usb_mixer_dev_free(struct snd_device *device)
1632 struct usb_mixer_interface *mixer = device->device_data;
1633 snd_usb_mixer_free(mixer);
1638 * create mixer controls
1640 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1642 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1644 unsigned char *desc;
1645 struct mixer_build state;
1647 const struct usbmix_ctl_map *map;
1648 struct usb_host_interface *hostif;
1650 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1651 memset(&state, 0, sizeof(state));
1652 state.chip = mixer->chip;
1653 state.mixer = mixer;
1654 state.buffer = hostif->extra;
1655 state.buflen = hostif->extralen;
1657 /* check the mapping table */
1658 for (map = usbmix_ctl_maps; map->id; map++) {
1659 if (map->id == state.chip->usb_id) {
1660 state.map = map->map;
1661 state.selector_map = map->selector_map;
1662 mixer->ignore_ctl_error = map->ignore_ctl_error;
1668 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1670 continue; /* invalid descriptor? */
1671 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1672 state.oterm.id = desc[3];
1673 state.oterm.type = combine_word(&desc[4]);
1674 state.oterm.name = desc[8];
1675 err = parse_audio_unit(&state, desc[7]);
1682 static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1685 struct usb_mixer_elem_info *info;
1687 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1688 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1692 static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1697 /* unit ids specific to Extigy/Audigy 2 NX: */
1699 case 0: /* remote control */
1700 mixer->rc_urb->dev = mixer->chip->dev;
1701 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1703 case 4: /* digital in jack */
1704 case 7: /* line in jacks */
1705 case 19: /* speaker out jacks */
1706 case 20: /* headphones out jack */
1708 /* live24ext: 4 = line-in jack */
1709 case 3: /* hp-out jack (may actuate Mute) */
1710 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1711 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1712 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1715 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1720 static void snd_usb_mixer_status_complete(struct urb *urb)
1722 struct usb_mixer_interface *mixer = urb->context;
1724 if (urb->status == 0) {
1725 u8 *buf = urb->transfer_buffer;
1728 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1729 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1731 /* ignore any notifications not from the control interface */
1732 if ((buf[0] & 0x0f) != 0)
1734 if (!(buf[0] & 0x40))
1735 snd_usb_mixer_notify_id(mixer, buf[1]);
1737 snd_usb_mixer_memory_change(mixer, buf[1]);
1740 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1741 urb->dev = mixer->chip->dev;
1742 usb_submit_urb(urb, GFP_ATOMIC);
1746 /* create the handler for the optional status interrupt endpoint */
1747 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1749 struct usb_host_interface *hostif;
1750 struct usb_endpoint_descriptor *ep;
1751 void *transfer_buffer;
1755 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1756 /* we need one interrupt input endpoint */
1757 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1759 ep = get_endpoint(hostif, 0);
1760 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
1763 epnum = usb_endpoint_num(ep);
1764 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1765 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1766 if (!transfer_buffer)
1768 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1770 kfree(transfer_buffer);
1773 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1774 usb_rcvintpipe(mixer->chip->dev, epnum),
1775 transfer_buffer, buffer_length,
1776 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1777 usb_submit_urb(mixer->urb, GFP_KERNEL);
1781 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
1783 struct usb_mixer_interface *mixer = urb->context;
1784 const struct rc_config *rc = mixer->rc_cfg;
1787 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
1790 code = mixer->rc_buffer[rc->offset];
1791 if (rc->length == 2)
1792 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1794 /* the Mute button actually changes the mixer control */
1795 if (code == rc->mute_code)
1796 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
1797 mixer->rc_code = code;
1799 wake_up(&mixer->rc_waitq);
1802 static int snd_usb_sbrc_hwdep_open(struct snd_hwdep *hw, struct file *file)
1804 struct usb_mixer_interface *mixer = hw->private_data;
1806 if (test_and_set_bit(0, &mixer->rc_hwdep_open))
1811 static int snd_usb_sbrc_hwdep_release(struct snd_hwdep *hw, struct file *file)
1813 struct usb_mixer_interface *mixer = hw->private_data;
1815 clear_bit(0, &mixer->rc_hwdep_open);
1816 smp_mb__after_clear_bit();
1820 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
1821 long count, loff_t *offset)
1823 struct usb_mixer_interface *mixer = hw->private_data;
1827 if (count != 1 && count != 4)
1829 err = wait_event_interruptible(mixer->rc_waitq,
1830 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1833 err = put_user(rc_code, buf);
1835 err = put_user(rc_code, (u32 __user *)buf);
1837 return err < 0 ? err : count;
1840 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
1843 struct usb_mixer_interface *mixer = hw->private_data;
1845 poll_wait(file, &mixer->rc_waitq, wait);
1846 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1849 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1851 struct snd_hwdep *hwdep;
1854 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1855 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1857 if (i >= ARRAY_SIZE(rc_configs))
1859 mixer->rc_cfg = &rc_configs[i];
1861 len = mixer->rc_cfg->packet_length;
1863 init_waitqueue_head(&mixer->rc_waitq);
1864 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1867 snprintf(hwdep->name, sizeof(hwdep->name),
1868 "%s remote control", mixer->chip->card->shortname);
1869 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1870 hwdep->private_data = mixer;
1871 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1872 hwdep->ops.open = snd_usb_sbrc_hwdep_open;
1873 hwdep->ops.release = snd_usb_sbrc_hwdep_release;
1874 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1876 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1879 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1880 if (!mixer->rc_setup_packet) {
1881 usb_free_urb(mixer->rc_urb);
1882 mixer->rc_urb = NULL;
1885 mixer->rc_setup_packet->bRequestType =
1886 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1887 mixer->rc_setup_packet->bRequest = GET_MEM;
1888 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1889 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1890 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1891 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1892 usb_rcvctrlpipe(mixer->chip->dev, 0),
1893 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1894 snd_usb_soundblaster_remote_complete, mixer);
1898 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
1900 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1902 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1903 int index = kcontrol->private_value;
1905 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1909 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1911 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1912 int index = kcontrol->private_value;
1913 int value = ucontrol->value.integer.value[0];
1918 changed = value != mixer->audigy2nx_leds[index];
1919 err = snd_usb_ctl_msg(mixer->chip->dev,
1920 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1921 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1922 value, index + 2, NULL, 0, 100);
1925 mixer->audigy2nx_leds[index] = value;
1929 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
1931 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1932 .name = "CMSS LED Switch",
1933 .info = snd_audigy2nx_led_info,
1934 .get = snd_audigy2nx_led_get,
1935 .put = snd_audigy2nx_led_put,
1939 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1940 .name = "Power LED Switch",
1941 .info = snd_audigy2nx_led_info,
1942 .get = snd_audigy2nx_led_get,
1943 .put = snd_audigy2nx_led_put,
1947 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1948 .name = "Dolby Digital LED Switch",
1949 .info = snd_audigy2nx_led_info,
1950 .get = snd_audigy2nx_led_get,
1951 .put = snd_audigy2nx_led_put,
1956 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1960 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
1961 if (i > 1 && /* Live24ext has 2 LEDs only */
1962 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1963 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
1965 err = snd_ctl_add(mixer->chip->card,
1966 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1970 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1974 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
1975 struct snd_info_buffer *buffer)
1977 static const struct sb_jack {
1980 } jacks_audigy2nx[] = {
1986 }, jacks_live24ext[] = {
1987 {4, "line in"}, /* &1=Line, &2=Mic*/
1988 {3, "hph out"}, /* headphones */
1989 {0, "RC "}, /* last command, 6 bytes see rc_config above */
1992 const struct sb_jack *jacks;
1993 struct usb_mixer_interface *mixer = entry->private_data;
1997 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
1998 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
1999 jacks = jacks_audigy2nx;
2000 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2001 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
2002 jacks = jacks_live24ext;
2006 for (i = 0; jacks[i].name; ++i) {
2007 snd_iprintf(buffer, "%s: ", jacks[i].name);
2008 err = snd_usb_ctl_msg(mixer->chip->dev,
2009 usb_rcvctrlpipe(mixer->chip->dev, 0),
2010 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2011 USB_RECIP_INTERFACE, 0,
2012 jacks[i].unitid << 8, buf, 3, 100);
2013 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
2014 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2016 snd_iprintf(buffer, "?\n");
2020 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2023 static struct snd_device_ops dev_ops = {
2024 .dev_free = snd_usb_mixer_dev_free
2026 struct usb_mixer_interface *mixer;
2029 strcpy(chip->card->mixername, "USB Mixer");
2031 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
2035 mixer->ctrlif = ctrlif;
2036 mixer->ignore_ctl_error = ignore_error;
2037 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2038 if (!mixer->id_elems) {
2043 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
2044 (err = snd_usb_mixer_status_create(mixer)) < 0)
2047 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2050 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
2051 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2052 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
2053 struct snd_info_entry *entry;
2055 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2057 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
2058 snd_info_set_text_ops(entry, mixer,
2059 snd_audigy2nx_proc_read);
2062 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
2065 list_add(&mixer->list, &chip->mixer_list);
2069 snd_usb_mixer_free(mixer);
2073 void snd_usb_mixer_disconnect(struct list_head *p)
2075 struct usb_mixer_interface *mixer;
2077 mixer = list_entry(p, struct usb_mixer_interface, list);
2078 usb_kill_urb(mixer->urb);
2079 usb_kill_urb(mixer->rc_urb);