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)) {
225 // printk("ignored control %d:%d\n", unitid, control);
232 /* get the mapped selector source name */
233 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
234 int index, char *buf, int buflen)
236 const struct usbmix_selector_map *p;
238 if (! state->selector_map)
240 for (p = state->selector_map; p->id; p++) {
241 if (p->id == unitid && index < p->count)
242 return strlcpy(buf, p->names[index], buflen);
248 * find an audio control unit with the given unit id
250 static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
255 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
256 USB_DT_CS_INTERFACE)) != NULL) {
257 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
265 * copy a string with the given id
267 static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
269 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
275 * convert from the byte/word on usb descriptor to the zero-based integer
277 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
279 switch (cval->val_type) {
280 case USB_MIXER_BOOLEAN:
282 case USB_MIXER_INV_BOOLEAN:
305 * convert from the zero-based int to the byte/word for usb descriptor
307 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
309 switch (cval->val_type) {
310 case USB_MIXER_BOOLEAN:
312 case USB_MIXER_INV_BOOLEAN:
321 return 0; /* not reached */
324 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
330 else if (val >= cval->max)
331 return (cval->max - cval->min + cval->res - 1) / cval->res;
333 return (val - cval->min) / cval->res;
336 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
351 * retrieve a mixer value
354 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
356 unsigned char buf[2];
357 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
360 while (timeout-- > 0) {
361 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
362 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
364 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
365 validx, cval->mixer->ctrlif | (cval->id << 8),
366 buf, val_len, 100) >= val_len) {
367 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
371 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
372 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
376 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
378 return get_ctl_value(cval, GET_CUR, validx, value);
381 /* channel = 0: master, 1 = first channel */
382 static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
383 int channel, int *value)
385 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
388 static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
389 int channel, int index, int *value)
393 if (cval->cached & (1 << channel)) {
394 *value = cval->cache_val[index];
397 err = get_cur_mix_raw(cval, channel, value);
399 if (!cval->mixer->ignore_ctl_error)
400 snd_printd(KERN_ERR "cannot get current value for "
401 "control %d ch %d: err = %d\n",
402 cval->control, channel, err);
405 cval->cached |= 1 << channel;
406 cval->cache_val[index] = *value;
415 static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
417 unsigned char buf[2];
418 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
421 value_set = convert_bytes_value(cval, value_set);
422 buf[0] = value_set & 0xff;
423 buf[1] = (value_set >> 8) & 0xff;
424 while (timeout -- > 0)
425 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
426 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
428 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
429 validx, cval->mixer->ctrlif | (cval->id << 8),
430 buf, val_len, 100) >= 0)
432 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
433 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
437 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
439 return set_ctl_value(cval, SET_CUR, validx, value);
442 static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
443 int index, int value)
446 err = set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel,
450 cval->cached |= 1 << channel;
451 cval->cache_val[index] = value;
456 * TLV callback for mixer volume controls
458 static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
459 unsigned int size, unsigned int __user *_tlv)
461 struct usb_mixer_elem_info *cval = kcontrol->private_data;
462 DECLARE_TLV_DB_SCALE(scale, 0, 0, 0);
464 if (size < sizeof(scale))
466 /* USB descriptions contain the dB scale in 1/256 dB unit
467 * while ALSA TLV contains in 1/100 dB unit
469 scale[2] = (convert_signed_value(cval, cval->min) * 100) / 256;
470 scale[3] = (convert_signed_value(cval, cval->res) * 100) / 256;
471 if (copy_to_user(_tlv, scale, sizeof(scale)))
477 * parser routines begin here...
480 static int parse_audio_unit(struct mixer_build *state, int unitid);
484 * check if the input/output channel routing is enabled on the given bitmap.
485 * used for mixer unit parser
487 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
489 int idx = ich * num_outs + och;
490 return bmap[idx >> 3] & (0x80 >> (idx & 7));
495 * add an alsa control element
496 * search and increment the index until an empty slot is found.
498 * if failed, give up and free the control instance.
501 static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
503 struct usb_mixer_elem_info *cval = kctl->private_data;
506 while (snd_ctl_find_id(state->chip->card, &kctl->id))
508 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
509 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
512 cval->elem_id = &kctl->id;
513 cval->next_id_elem = state->mixer->id_elems[cval->id];
514 state->mixer->id_elems[cval->id] = cval;
520 * get a terminal name string
523 static struct iterm_name_combo {
527 { 0x0300, "Output" },
528 { 0x0301, "Speaker" },
529 { 0x0302, "Headphone" },
530 { 0x0303, "HMD Audio" },
531 { 0x0304, "Desktop Speaker" },
532 { 0x0305, "Room Speaker" },
533 { 0x0306, "Com Speaker" },
535 { 0x0600, "External In" },
536 { 0x0601, "Analog In" },
537 { 0x0602, "Digital In" },
539 { 0x0604, "Legacy In" },
540 { 0x0605, "IEC958 In" },
541 { 0x0606, "1394 DA Stream" },
542 { 0x0607, "1394 DV Stream" },
543 { 0x0700, "Embedded" },
544 { 0x0701, "Noise Source" },
545 { 0x0702, "Equalization Noise" },
549 { 0x0706, "MiniDisk" },
550 { 0x0707, "Analog Tape" },
551 { 0x0708, "Phonograph" },
552 { 0x0709, "VCR Audio" },
553 { 0x070a, "Video Disk Audio" },
554 { 0x070b, "DVD Audio" },
555 { 0x070c, "TV Tuner Audio" },
556 { 0x070d, "Satellite Rec Audio" },
557 { 0x070e, "Cable Tuner Audio" },
558 { 0x070f, "DSS Audio" },
559 { 0x0710, "Radio Receiver" },
560 { 0x0711, "Radio Transmitter" },
561 { 0x0712, "Multi-Track Recorder" },
562 { 0x0713, "Synthesizer" },
566 static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
567 unsigned char *name, int maxlen, int term_only)
569 struct iterm_name_combo *names;
572 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
574 /* virtual type - not a real terminal */
575 if (iterm->type >> 16) {
578 switch (iterm->type >> 16) {
580 strcpy(name, "Selector"); return 8;
581 case PROCESSING_UNIT:
582 strcpy(name, "Process Unit"); return 12;
584 strcpy(name, "Ext Unit"); return 8;
586 strcpy(name, "Mixer"); return 5;
588 return sprintf(name, "Unit %d", iterm->id);
592 switch (iterm->type & 0xff00) {
594 strcpy(name, "PCM"); return 3;
596 strcpy(name, "Mic"); return 3;
598 strcpy(name, "Headset"); return 7;
600 strcpy(name, "Phone"); return 5;
603 for (names = iterm_names; names->type; names++)
604 if (names->type == iterm->type) {
605 strcpy(name, names->name);
606 return strlen(names->name);
613 * parse the source unit recursively until it reaches to a terminal
614 * or a branched unit.
616 static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
620 memset(term, 0, sizeof(*term));
621 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
625 term->type = combine_word(p1 + 4);
626 term->channels = p1[7];
627 term->chconfig = combine_word(p1 + 8);
632 break; /* continue to parse */
634 term->type = p1[2] << 16; /* virtual type */
635 term->channels = p1[5 + p1[4]];
636 term->chconfig = combine_word(p1 + 6 + p1[4]);
637 term->name = p1[p1[0] - 1];
640 /* call recursively to retrieve the channel info */
641 if (check_input_term(state, p1[5], term) < 0)
643 term->type = p1[2] << 16; /* virtual type */
645 term->name = p1[9 + p1[0] - 1];
647 case PROCESSING_UNIT:
651 break; /* continue to parse */
653 term->type = p1[2] << 16; /* virtual type */
654 term->channels = p1[7 + p1[6]];
655 term->chconfig = combine_word(p1 + 8 + p1[6]);
656 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
670 /* feature unit control information */
671 struct usb_feature_control_info {
673 unsigned int type; /* control type (mute, volume, etc.) */
676 static struct usb_feature_control_info audio_feature_info[] = {
677 { "Mute", USB_MIXER_INV_BOOLEAN },
678 { "Volume", USB_MIXER_S16 },
679 { "Tone Control - Bass", USB_MIXER_S8 },
680 { "Tone Control - Mid", USB_MIXER_S8 },
681 { "Tone Control - Treble", USB_MIXER_S8 },
682 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
683 { "Auto Gain Control", USB_MIXER_BOOLEAN },
684 { "Delay Control", USB_MIXER_U16 },
685 { "Bass Boost", USB_MIXER_BOOLEAN },
686 { "Loudness", USB_MIXER_BOOLEAN },
690 /* private_free callback */
691 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
693 kfree(kctl->private_data);
694 kctl->private_data = NULL;
699 * interface to ALSA control for feature/mixer units
703 * retrieve the minimum and maximum values for the specified control
705 static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
708 cval->min = default_min;
709 cval->max = cval->min + 1;
712 if (cval->val_type == USB_MIXER_BOOLEAN ||
713 cval->val_type == USB_MIXER_INV_BOOLEAN) {
714 cval->initialized = 1;
719 for (i = 0; i < MAX_CHANNELS; i++)
720 if (cval->cmask & (1 << i)) {
725 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
726 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
727 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
728 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
731 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
734 int last_valid_res = cval->res;
736 while (cval->res > 1) {
737 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
741 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
742 cval->res = last_valid_res;
747 /* Additional checks for the proper resolution
749 * Some devices report smaller resolutions than actually
750 * reacting. They don't return errors but simply clip
751 * to the lower aligned value.
753 if (cval->min + cval->res < cval->max) {
754 int last_valid_res = cval->res;
755 int saved, test, check;
756 get_cur_mix_raw(cval, minchn, &saved);
759 if (test < cval->max)
763 if (test < cval->min || test > cval->max ||
764 set_cur_mix_value(cval, minchn, 0, test) ||
765 get_cur_mix_raw(cval, minchn, &check)) {
766 cval->res = last_valid_res;
773 set_cur_mix_value(cval, minchn, 0, saved);
776 cval->initialized = 1;
782 /* get a feature/mixer unit info */
783 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
785 struct usb_mixer_elem_info *cval = kcontrol->private_data;
787 if (cval->val_type == USB_MIXER_BOOLEAN ||
788 cval->val_type == USB_MIXER_INV_BOOLEAN)
789 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
791 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
792 uinfo->count = cval->channels;
793 if (cval->val_type == USB_MIXER_BOOLEAN ||
794 cval->val_type == USB_MIXER_INV_BOOLEAN) {
795 uinfo->value.integer.min = 0;
796 uinfo->value.integer.max = 1;
798 if (! cval->initialized)
799 get_min_max(cval, 0);
800 uinfo->value.integer.min = 0;
801 uinfo->value.integer.max =
802 (cval->max - cval->min + cval->res - 1) / cval->res;
807 /* get the current value from feature/mixer unit */
808 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
810 struct usb_mixer_elem_info *cval = kcontrol->private_data;
811 int c, cnt, val, err;
813 ucontrol->value.integer.value[0] = cval->min;
816 for (c = 0; c < MAX_CHANNELS; c++) {
817 if (!(cval->cmask & (1 << c)))
819 err = get_cur_mix_value(cval, c + 1, cnt, &val);
821 return cval->mixer->ignore_ctl_error ? 0 : err;
822 val = get_relative_value(cval, val);
823 ucontrol->value.integer.value[cnt] = val;
829 err = get_cur_mix_value(cval, 0, 0, &val);
831 return cval->mixer->ignore_ctl_error ? 0 : err;
832 val = get_relative_value(cval, val);
833 ucontrol->value.integer.value[0] = val;
838 /* put the current value to feature/mixer unit */
839 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
841 struct usb_mixer_elem_info *cval = kcontrol->private_data;
842 int c, cnt, val, oval, err;
847 for (c = 0; c < MAX_CHANNELS; c++) {
848 if (!(cval->cmask & (1 << c)))
850 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
852 return cval->mixer->ignore_ctl_error ? 0 : err;
853 val = ucontrol->value.integer.value[cnt];
854 val = get_abs_value(cval, val);
856 set_cur_mix_value(cval, c + 1, cnt, val);
863 err = get_cur_mix_value(cval, 0, 0, &oval);
865 return cval->mixer->ignore_ctl_error ? 0 : err;
866 val = ucontrol->value.integer.value[0];
867 val = get_abs_value(cval, val);
869 set_cur_mix_value(cval, 0, 0, val);
876 static struct snd_kcontrol_new usb_feature_unit_ctl = {
877 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
878 .name = "", /* will be filled later manually */
879 .info = mixer_ctl_feature_info,
880 .get = mixer_ctl_feature_get,
881 .put = mixer_ctl_feature_put,
886 * build a feature control
889 static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
890 unsigned int ctl_mask, int control,
891 struct usb_audio_term *iterm, int unitid)
893 unsigned int len = 0;
895 int nameid = desc[desc[0] - 1];
896 struct snd_kcontrol *kctl;
897 struct usb_mixer_elem_info *cval;
899 control++; /* change from zero-based to 1-based value */
901 if (control == USB_FEATURE_GEQ) {
902 /* FIXME: not supported yet */
906 if (check_ignored_ctl(state, unitid, control))
909 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
911 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
914 cval->mixer = state->mixer;
916 cval->control = control;
917 cval->cmask = ctl_mask;
918 cval->val_type = audio_feature_info[control-1].type;
920 cval->channels = 1; /* master channel */
923 for (i = 0; i < 16; i++)
924 if (ctl_mask & (1 << i))
929 /* get min/max values */
930 get_min_max(cval, 0);
932 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
934 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
938 kctl->private_free = usb_mixer_elem_free;
940 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
941 mapped_name = len != 0;
943 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
946 case USB_FEATURE_MUTE:
947 case USB_FEATURE_VOLUME:
948 /* determine the control name. the rule is:
949 * - if a name id is given in descriptor, use it.
950 * - if the connected input can be determined, then use the name
952 * - if the connected output can be determined, use it.
953 * - otherwise, anonymous name.
956 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
958 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
960 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
961 "Feature %d", unitid);
963 /* determine the stream direction:
964 * if the connected output is USB stream, then it's likely a
965 * capture stream. otherwise it should be playback (hopefully :)
967 if (! mapped_name && ! (state->oterm.type >> 16)) {
968 if ((state->oterm.type & 0xff00) == 0x0100) {
969 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
971 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
974 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
975 sizeof(kctl->id.name));
976 if (control == USB_FEATURE_VOLUME) {
977 kctl->tlv.c = mixer_vol_tlv;
978 kctl->vd[0].access |=
979 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
980 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
986 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
987 sizeof(kctl->id.name));
991 /* quirk for UDA1321/N101 */
992 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
993 /* is not very clear from datasheets */
994 /* I hope that the min value is -15360 for newer firmware --jk */
995 switch (state->chip->usb_id) {
996 case USB_ID(0x0471, 0x0101):
997 case USB_ID(0x0471, 0x0104):
998 case USB_ID(0x0471, 0x0105):
999 case USB_ID(0x0672, 0x1041):
1000 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1001 cval->min == -15616) {
1002 snd_printk(KERN_INFO "using volume control quirk for the UDA1321/N101 chip\n");
1007 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1008 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
1009 add_control_to_empty(state, kctl);
1015 * parse a feature unit
1017 * most of controlls are defined here.
1019 static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
1022 struct usb_audio_term iterm;
1023 unsigned int master_bits, first_ch_bits;
1026 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1027 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1031 /* parse the source unit */
1032 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1035 /* determine the input source type and name */
1036 if (check_input_term(state, ftr[4], &iterm) < 0)
1039 channels = (ftr[0] - 7) / csize - 1;
1041 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
1043 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1046 /* check all control types */
1047 for (i = 0; i < 10; i++) {
1048 unsigned int ch_bits = 0;
1049 for (j = 0; j < channels; j++) {
1050 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1051 if (mask & (1 << i))
1052 ch_bits |= (1 << j);
1054 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1055 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1056 if (master_bits & (1 << i))
1057 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1069 * build a mixer unit control
1071 * the callbacks are identical with feature unit.
1072 * input channel number (zero based) is given in control field instead.
1075 static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
1076 int in_pin, int in_ch, int unitid,
1077 struct usb_audio_term *iterm)
1079 struct usb_mixer_elem_info *cval;
1080 unsigned int input_pins = desc[4];
1081 unsigned int num_outs = desc[5 + input_pins];
1082 unsigned int i, len;
1083 struct snd_kcontrol *kctl;
1085 if (check_ignored_ctl(state, unitid, 0))
1088 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1092 cval->mixer = state->mixer;
1094 cval->control = in_ch + 1; /* based on 1 */
1095 cval->val_type = USB_MIXER_S16;
1096 for (i = 0; i < num_outs; i++) {
1097 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1098 cval->cmask |= (1 << i);
1103 /* get min/max values */
1104 get_min_max(cval, 0);
1106 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1108 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1112 kctl->private_free = usb_mixer_elem_free;
1114 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1116 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1118 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1119 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1121 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1122 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1123 add_control_to_empty(state, kctl);
1128 * parse a mixer unit
1130 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1132 struct usb_audio_term iterm;
1133 int input_pins, num_ins, num_outs;
1136 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1137 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1140 /* no bmControls field (e.g. Maya44) -> ignore */
1141 if (desc[0] <= 10 + input_pins) {
1142 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1148 for (pin = 0; pin < input_pins; pin++) {
1149 err = parse_audio_unit(state, desc[5 + pin]);
1152 err = check_input_term(state, desc[5 + pin], &iterm);
1155 num_ins += iterm.channels;
1156 for (; ich < num_ins; ++ich) {
1157 int och, ich_has_controls = 0;
1159 for (och = 0; och < num_outs; ++och) {
1160 if (check_matrix_bitmap(desc + 9 + input_pins,
1161 ich, och, num_outs)) {
1162 ich_has_controls = 1;
1166 if (ich_has_controls)
1167 build_mixer_unit_ctl(state, desc, pin, ich,
1176 * Processing Unit / Extension Unit
1179 /* get callback for processing/extension unit */
1180 static int mixer_ctl_procunit_get(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, &val);
1186 if (err < 0 && cval->mixer->ignore_ctl_error) {
1187 ucontrol->value.integer.value[0] = cval->min;
1192 val = get_relative_value(cval, val);
1193 ucontrol->value.integer.value[0] = val;
1197 /* put callback for processing/extension unit */
1198 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1200 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1203 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1205 if (cval->mixer->ignore_ctl_error)
1209 val = ucontrol->value.integer.value[0];
1210 val = get_abs_value(cval, val);
1212 set_cur_ctl_value(cval, cval->control << 8, val);
1218 /* alsa control interface for processing/extension unit */
1219 static struct snd_kcontrol_new mixer_procunit_ctl = {
1220 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1221 .name = "", /* will be filled later */
1222 .info = mixer_ctl_feature_info,
1223 .get = mixer_ctl_procunit_get,
1224 .put = mixer_ctl_procunit_put,
1229 * predefined data for processing units
1231 struct procunit_value_info {
1238 struct procunit_info {
1241 struct procunit_value_info *values;
1244 static struct procunit_value_info updown_proc_info[] = {
1245 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1246 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1249 static struct procunit_value_info prologic_proc_info[] = {
1250 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1251 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1254 static struct procunit_value_info threed_enh_proc_info[] = {
1255 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1256 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1259 static struct procunit_value_info reverb_proc_info[] = {
1260 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1261 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1262 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1263 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1266 static struct procunit_value_info chorus_proc_info[] = {
1267 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1268 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1269 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1270 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1273 static struct procunit_value_info dcr_proc_info[] = {
1274 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1275 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1276 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1277 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1278 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1279 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1283 static struct procunit_info procunits[] = {
1284 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1285 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1286 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1287 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1288 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1289 { USB_PROC_DCR, "DCR", dcr_proc_info },
1294 * build a processing/extension unit
1296 static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1298 int num_ins = dsc[6];
1299 struct usb_mixer_elem_info *cval;
1300 struct snd_kcontrol *kctl;
1301 int i, err, nameid, type, len;
1302 struct procunit_info *info;
1303 struct procunit_value_info *valinfo;
1304 static struct procunit_value_info default_value_info[] = {
1305 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1308 static struct procunit_info default_info = {
1309 0, NULL, default_value_info
1312 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1313 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1317 for (i = 0; i < num_ins; i++) {
1318 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1322 type = combine_word(&dsc[4]);
1323 for (info = list; info && info->type; info++)
1324 if (info->type == type)
1326 if (! info || ! info->type)
1327 info = &default_info;
1329 for (valinfo = info->values; valinfo->control; valinfo++) {
1330 /* FIXME: bitmap might be longer than 8bit */
1331 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1333 if (check_ignored_ctl(state, unitid, valinfo->control))
1335 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1337 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1340 cval->mixer = state->mixer;
1342 cval->control = valinfo->control;
1343 cval->val_type = valinfo->val_type;
1346 /* get min/max values */
1347 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1348 /* FIXME: hard-coded */
1350 cval->max = dsc[15];
1352 cval->initialized = 1;
1354 get_min_max(cval, valinfo->min_value);
1356 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1358 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1362 kctl->private_free = usb_mixer_elem_free;
1364 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1366 else if (info->name)
1367 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1369 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1372 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1374 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1376 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1377 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1379 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1380 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1381 if ((err = add_control_to_empty(state, kctl)) < 0)
1388 static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1390 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1393 static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1395 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1403 /* info callback for selector unit
1404 * use an enumerator type for routing
1406 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1408 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1409 char **itemlist = (char **)kcontrol->private_value;
1411 if (snd_BUG_ON(!itemlist))
1413 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1415 uinfo->value.enumerated.items = cval->max;
1416 if ((int)uinfo->value.enumerated.item >= cval->max)
1417 uinfo->value.enumerated.item = cval->max - 1;
1418 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1422 /* get callback for selector unit */
1423 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1425 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1428 err = get_cur_ctl_value(cval, 0, &val);
1430 if (cval->mixer->ignore_ctl_error) {
1431 ucontrol->value.enumerated.item[0] = 0;
1436 val = get_relative_value(cval, val);
1437 ucontrol->value.enumerated.item[0] = val;
1441 /* put callback for selector unit */
1442 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1444 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1447 err = get_cur_ctl_value(cval, 0, &oval);
1449 if (cval->mixer->ignore_ctl_error)
1453 val = ucontrol->value.enumerated.item[0];
1454 val = get_abs_value(cval, val);
1456 set_cur_ctl_value(cval, 0, val);
1462 /* alsa control interface for selector unit */
1463 static struct snd_kcontrol_new mixer_selectunit_ctl = {
1464 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1465 .name = "", /* will be filled later */
1466 .info = mixer_ctl_selector_info,
1467 .get = mixer_ctl_selector_get,
1468 .put = mixer_ctl_selector_put,
1472 /* private free callback.
1473 * free both private_data and private_value
1475 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
1479 if (kctl->private_data) {
1480 struct usb_mixer_elem_info *cval = kctl->private_data;
1481 num_ins = cval->max;
1483 kctl->private_data = NULL;
1485 if (kctl->private_value) {
1486 char **itemlist = (char **)kctl->private_value;
1487 for (i = 0; i < num_ins; i++)
1490 kctl->private_value = 0;
1495 * parse a selector unit
1497 static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1499 unsigned int num_ins = desc[4];
1500 unsigned int i, nameid, len;
1502 struct usb_mixer_elem_info *cval;
1503 struct snd_kcontrol *kctl;
1506 if (! num_ins || desc[0] < 5 + num_ins) {
1507 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1511 for (i = 0; i < num_ins; i++) {
1512 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1516 if (num_ins == 1) /* only one ? nonsense! */
1519 if (check_ignored_ctl(state, unitid, 0))
1522 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1524 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1527 cval->mixer = state->mixer;
1529 cval->val_type = USB_MIXER_U8;
1532 cval->max = num_ins;
1534 cval->initialized = 1;
1536 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1538 snd_printk(KERN_ERR "cannot malloc\n");
1542 #define MAX_ITEM_NAME_LEN 64
1543 for (i = 0; i < num_ins; i++) {
1544 struct usb_audio_term iterm;
1546 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1547 if (! namelist[i]) {
1548 snd_printk(KERN_ERR "cannot malloc\n");
1555 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1557 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1558 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1560 sprintf(namelist[i], "Input %d", i);
1563 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1565 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1570 kctl->private_value = (unsigned long)namelist;
1571 kctl->private_free = usb_mixer_selector_elem_free;
1573 nameid = desc[desc[0] - 1];
1574 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1578 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1580 len = get_term_name(state, &state->oterm,
1581 kctl->id.name, sizeof(kctl->id.name), 0);
1583 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1585 if ((state->oterm.type & 0xff00) == 0x0100)
1586 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1588 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1591 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1592 cval->id, kctl->id.name, num_ins);
1593 if ((err = add_control_to_empty(state, kctl)) < 0)
1601 * parse an audio unit recursively
1604 static int parse_audio_unit(struct mixer_build *state, int unitid)
1608 if (test_and_set_bit(unitid, state->unitbitmap))
1609 return 0; /* the unit already visited */
1611 p1 = find_audio_control_unit(state, unitid);
1613 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1618 case INPUT_TERMINAL:
1621 return parse_audio_mixer_unit(state, unitid, p1);
1623 return parse_audio_selector_unit(state, unitid, p1);
1625 return parse_audio_feature_unit(state, unitid, p1);
1626 case PROCESSING_UNIT:
1627 return parse_audio_processing_unit(state, unitid, p1);
1628 case EXTENSION_UNIT:
1629 return parse_audio_extension_unit(state, unitid, p1);
1631 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1636 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1638 kfree(mixer->id_elems);
1640 kfree(mixer->urb->transfer_buffer);
1641 usb_free_urb(mixer->urb);
1643 usb_free_urb(mixer->rc_urb);
1644 kfree(mixer->rc_setup_packet);
1648 static int snd_usb_mixer_dev_free(struct snd_device *device)
1650 struct usb_mixer_interface *mixer = device->device_data;
1651 snd_usb_mixer_free(mixer);
1656 * create mixer controls
1658 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1660 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1662 unsigned char *desc;
1663 struct mixer_build state;
1665 const struct usbmix_ctl_map *map;
1666 struct usb_host_interface *hostif;
1668 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1669 memset(&state, 0, sizeof(state));
1670 state.chip = mixer->chip;
1671 state.mixer = mixer;
1672 state.buffer = hostif->extra;
1673 state.buflen = hostif->extralen;
1675 /* check the mapping table */
1676 for (map = usbmix_ctl_maps; map->id; map++) {
1677 if (map->id == state.chip->usb_id) {
1678 state.map = map->map;
1679 state.selector_map = map->selector_map;
1680 mixer->ignore_ctl_error = map->ignore_ctl_error;
1686 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1688 continue; /* invalid descriptor? */
1689 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1690 state.oterm.id = desc[3];
1691 state.oterm.type = combine_word(&desc[4]);
1692 state.oterm.name = desc[8];
1693 err = parse_audio_unit(&state, desc[7]);
1700 static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1703 struct usb_mixer_elem_info *info;
1705 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1706 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1710 static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1715 /* unit ids specific to Extigy/Audigy 2 NX: */
1717 case 0: /* remote control */
1718 mixer->rc_urb->dev = mixer->chip->dev;
1719 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1721 case 4: /* digital in jack */
1722 case 7: /* line in jacks */
1723 case 19: /* speaker out jacks */
1724 case 20: /* headphones out jack */
1726 /* live24ext: 4 = line-in jack */
1727 case 3: /* hp-out jack (may actuate Mute) */
1728 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1729 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1730 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1733 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1738 static void snd_usb_mixer_status_complete(struct urb *urb)
1740 struct usb_mixer_interface *mixer = urb->context;
1742 if (urb->status == 0) {
1743 u8 *buf = urb->transfer_buffer;
1746 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1747 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1749 /* ignore any notifications not from the control interface */
1750 if ((buf[0] & 0x0f) != 0)
1752 if (!(buf[0] & 0x40))
1753 snd_usb_mixer_notify_id(mixer, buf[1]);
1755 snd_usb_mixer_memory_change(mixer, buf[1]);
1758 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1759 urb->dev = mixer->chip->dev;
1760 usb_submit_urb(urb, GFP_ATOMIC);
1764 /* create the handler for the optional status interrupt endpoint */
1765 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1767 struct usb_host_interface *hostif;
1768 struct usb_endpoint_descriptor *ep;
1769 void *transfer_buffer;
1773 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1774 /* we need one interrupt input endpoint */
1775 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1777 ep = get_endpoint(hostif, 0);
1778 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
1781 epnum = usb_endpoint_num(ep);
1782 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1783 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1784 if (!transfer_buffer)
1786 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1788 kfree(transfer_buffer);
1791 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1792 usb_rcvintpipe(mixer->chip->dev, epnum),
1793 transfer_buffer, buffer_length,
1794 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1795 usb_submit_urb(mixer->urb, GFP_KERNEL);
1799 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
1801 struct usb_mixer_interface *mixer = urb->context;
1802 const struct rc_config *rc = mixer->rc_cfg;
1805 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
1808 code = mixer->rc_buffer[rc->offset];
1809 if (rc->length == 2)
1810 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1812 /* the Mute button actually changes the mixer control */
1813 if (code == rc->mute_code)
1814 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
1815 mixer->rc_code = code;
1817 wake_up(&mixer->rc_waitq);
1820 static int snd_usb_sbrc_hwdep_open(struct snd_hwdep *hw, struct file *file)
1822 struct usb_mixer_interface *mixer = hw->private_data;
1824 if (test_and_set_bit(0, &mixer->rc_hwdep_open))
1829 static int snd_usb_sbrc_hwdep_release(struct snd_hwdep *hw, struct file *file)
1831 struct usb_mixer_interface *mixer = hw->private_data;
1833 clear_bit(0, &mixer->rc_hwdep_open);
1834 smp_mb__after_clear_bit();
1838 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
1839 long count, loff_t *offset)
1841 struct usb_mixer_interface *mixer = hw->private_data;
1845 if (count != 1 && count != 4)
1847 err = wait_event_interruptible(mixer->rc_waitq,
1848 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1851 err = put_user(rc_code, buf);
1853 err = put_user(rc_code, (u32 __user *)buf);
1855 return err < 0 ? err : count;
1858 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
1861 struct usb_mixer_interface *mixer = hw->private_data;
1863 poll_wait(file, &mixer->rc_waitq, wait);
1864 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1867 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1869 struct snd_hwdep *hwdep;
1872 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
1873 if (rc_configs[i].usb_id == mixer->chip->usb_id)
1875 if (i >= ARRAY_SIZE(rc_configs))
1877 mixer->rc_cfg = &rc_configs[i];
1879 len = mixer->rc_cfg->packet_length;
1881 init_waitqueue_head(&mixer->rc_waitq);
1882 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1885 snprintf(hwdep->name, sizeof(hwdep->name),
1886 "%s remote control", mixer->chip->card->shortname);
1887 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1888 hwdep->private_data = mixer;
1889 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1890 hwdep->ops.open = snd_usb_sbrc_hwdep_open;
1891 hwdep->ops.release = snd_usb_sbrc_hwdep_release;
1892 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1894 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1897 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1898 if (!mixer->rc_setup_packet) {
1899 usb_free_urb(mixer->rc_urb);
1900 mixer->rc_urb = NULL;
1903 mixer->rc_setup_packet->bRequestType =
1904 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1905 mixer->rc_setup_packet->bRequest = GET_MEM;
1906 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1907 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1908 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1909 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1910 usb_rcvctrlpipe(mixer->chip->dev, 0),
1911 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1912 snd_usb_soundblaster_remote_complete, mixer);
1916 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
1918 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1920 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1921 int index = kcontrol->private_value;
1923 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1927 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1929 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1930 int index = kcontrol->private_value;
1931 int value = ucontrol->value.integer.value[0];
1936 changed = value != mixer->audigy2nx_leds[index];
1937 err = snd_usb_ctl_msg(mixer->chip->dev,
1938 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1939 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1940 value, index + 2, NULL, 0, 100);
1943 mixer->audigy2nx_leds[index] = value;
1947 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
1949 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1950 .name = "CMSS LED Switch",
1951 .info = snd_audigy2nx_led_info,
1952 .get = snd_audigy2nx_led_get,
1953 .put = snd_audigy2nx_led_put,
1957 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1958 .name = "Power LED Switch",
1959 .info = snd_audigy2nx_led_info,
1960 .get = snd_audigy2nx_led_get,
1961 .put = snd_audigy2nx_led_put,
1965 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1966 .name = "Dolby Digital LED Switch",
1967 .info = snd_audigy2nx_led_info,
1968 .get = snd_audigy2nx_led_get,
1969 .put = snd_audigy2nx_led_put,
1974 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1978 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
1979 if (i > 1 && /* Live24ext has 2 LEDs only */
1980 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1981 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
1983 err = snd_ctl_add(mixer->chip->card,
1984 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1988 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1992 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
1993 struct snd_info_buffer *buffer)
1995 static const struct sb_jack {
1998 } jacks_audigy2nx[] = {
2004 }, jacks_live24ext[] = {
2005 {4, "line in"}, /* &1=Line, &2=Mic*/
2006 {3, "hph out"}, /* headphones */
2007 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2010 const struct sb_jack *jacks;
2011 struct usb_mixer_interface *mixer = entry->private_data;
2015 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
2016 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2017 jacks = jacks_audigy2nx;
2018 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2019 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
2020 jacks = jacks_live24ext;
2024 for (i = 0; jacks[i].name; ++i) {
2025 snd_iprintf(buffer, "%s: ", jacks[i].name);
2026 err = snd_usb_ctl_msg(mixer->chip->dev,
2027 usb_rcvctrlpipe(mixer->chip->dev, 0),
2028 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2029 USB_RECIP_INTERFACE, 0,
2030 jacks[i].unitid << 8, buf, 3, 100);
2031 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
2032 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2034 snd_iprintf(buffer, "?\n");
2038 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2041 static struct snd_device_ops dev_ops = {
2042 .dev_free = snd_usb_mixer_dev_free
2044 struct usb_mixer_interface *mixer;
2047 strcpy(chip->card->mixername, "USB Mixer");
2049 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
2053 mixer->ctrlif = ctrlif;
2054 mixer->ignore_ctl_error = ignore_error;
2055 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
2056 if (!mixer->id_elems) {
2061 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
2062 (err = snd_usb_mixer_status_create(mixer)) < 0)
2065 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2068 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
2069 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2070 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
2071 struct snd_info_entry *entry;
2073 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2075 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
2076 snd_info_set_text_ops(entry, mixer,
2077 snd_audigy2nx_proc_read);
2080 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
2083 list_add(&mixer->list, &chip->mixer_list);
2087 snd_usb_mixer_free(mixer);
2091 void snd_usb_mixer_disconnect(struct list_head *p)
2093 struct usb_mixer_interface *mixer;
2095 mixer = list_entry(p, struct usb_mixer_interface, list);
2096 usb_kill_urb(mixer->urb);
2097 usb_kill_urb(mixer->rc_urb);