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 <sound/driver.h>
30 #include <linux/bitops.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/usb.h>
36 #include <sound/core.h>
37 #include <sound/control.h>
45 /* ignore error from controls - for debugging */
46 /* #define IGNORE_CTL_ERROR */
48 typedef struct usb_mixer_build mixer_build_t
;
49 typedef struct usb_audio_term usb_audio_term_t
;
50 typedef struct usb_mixer_elem_info usb_mixer_elem_info_t
;
53 struct usb_audio_term
{
57 unsigned int chconfig
;
61 struct usbmix_name_map
;
63 struct usb_mixer_build
{
64 snd_usb_audio_t
*chip
;
65 unsigned char *buffer
;
68 unsigned short vendor
;
69 unsigned short product
;
70 DECLARE_BITMAP(unitbitmap
, 32*32);
71 usb_audio_term_t oterm
;
72 const struct usbmix_name_map
*map
;
75 struct usb_mixer_elem_info
{
76 snd_usb_audio_t
*chip
;
79 unsigned int control
; /* CS or ICN (high byte) */
80 unsigned int cmask
; /* channel mask bitmap: 0 = master */
84 unsigned int initialized
: 1;
98 USB_FEATURE_BASSBOOST
,
104 USB_MIXER_INV_BOOLEAN
,
113 USB_PROC_UPDOWN_SWITCH
= 1,
114 USB_PROC_UPDOWN_MODE_SEL
= 2,
116 USB_PROC_PROLOGIC
= 2,
117 USB_PROC_PROLOGIC_SWITCH
= 1,
118 USB_PROC_PROLOGIC_MODE_SEL
= 2,
121 USB_PROC_3DENH_SWITCH
= 1,
122 USB_PROC_3DENH_SPACE
= 2,
125 USB_PROC_REVERB_SWITCH
= 1,
126 USB_PROC_REVERB_LEVEL
= 2,
127 USB_PROC_REVERB_TIME
= 3,
128 USB_PROC_REVERB_DELAY
= 4,
131 USB_PROC_CHORUS_SWITCH
= 1,
132 USB_PROC_CHORUS_LEVEL
= 2,
133 USB_PROC_CHORUS_RATE
= 3,
134 USB_PROC_CHORUS_DEPTH
= 4,
137 USB_PROC_DCR_SWITCH
= 1,
138 USB_PROC_DCR_RATIO
= 2,
139 USB_PROC_DCR_MAX_AMP
= 3,
140 USB_PROC_DCR_THRESHOLD
= 4,
141 USB_PROC_DCR_ATTACK
= 5,
142 USB_PROC_DCR_RELEASE
= 6,
145 #define MAX_CHANNELS 10 /* max logical channels */
149 * manual mapping of mixer names
150 * if the mixer topology is too complicated and the parsed names are
151 * ambiguous, add the entries in usbmixer_maps.c.
153 #include "usbmixer_maps.c"
155 /* get the mapped name if the unit matches */
156 static int check_mapped_name(mixer_build_t
*state
, int unitid
, int control
, char *buf
, int buflen
)
158 const struct usbmix_name_map
*p
;
163 for (p
= state
->map
; p
->id
; p
++) {
164 if (p
->id
== unitid
&& p
->name
&&
165 (! control
|| ! p
->control
|| control
== p
->control
)) {
167 return strlcpy(buf
, p
->name
, buflen
);
173 /* check whether the control should be ignored */
174 static int check_ignored_ctl(mixer_build_t
*state
, int unitid
, int control
)
176 const struct usbmix_name_map
*p
;
180 for (p
= state
->map
; p
->id
; p
++) {
181 if (p
->id
== unitid
&& ! p
->name
&&
182 (! control
|| ! p
->control
|| control
== p
->control
)) {
183 // printk("ignored control %d:%d\n", unitid, control);
191 * find an audio control unit with the given unit id
193 static void *find_audio_control_unit(mixer_build_t
*state
, unsigned char unit
)
198 while ((p
= snd_usb_find_desc(state
->buffer
, state
->buflen
, p
,
199 USB_DT_CS_INTERFACE
)) != NULL
) {
200 if (p
[0] >= 4 && p
[2] >= INPUT_TERMINAL
&& p
[2] <= EXTENSION_UNIT
&& p
[3] == unit
)
208 * copy a string with the given id
210 static int snd_usb_copy_string_desc(mixer_build_t
*state
, int index
, char *buf
, int maxlen
)
212 int len
= usb_string(state
->chip
->dev
, index
, buf
, maxlen
- 1);
218 * convert from the byte/word on usb descriptor to the zero-based integer
220 static int convert_signed_value(usb_mixer_elem_info_t
*cval
, int val
)
222 switch (cval
->val_type
) {
223 case USB_MIXER_BOOLEAN
:
225 case USB_MIXER_INV_BOOLEAN
:
248 * convert from the zero-based int to the byte/word for usb descriptor
250 static int convert_bytes_value(usb_mixer_elem_info_t
*cval
, int val
)
252 switch (cval
->val_type
) {
253 case USB_MIXER_BOOLEAN
:
255 case USB_MIXER_INV_BOOLEAN
:
264 return 0; /* not reached */
267 static int get_relative_value(usb_mixer_elem_info_t
*cval
, int val
)
273 else if (val
> cval
->max
)
274 return (cval
->max
- cval
->min
) / cval
->res
;
276 return (val
- cval
->min
) / cval
->res
;
279 static int get_abs_value(usb_mixer_elem_info_t
*cval
, int val
)
294 * retrieve a mixer value
297 static int get_ctl_value(usb_mixer_elem_info_t
*cval
, int request
, int validx
, int *value_ret
)
299 unsigned char buf
[2];
300 int val_len
= cval
->val_type
>= USB_MIXER_S16
? 2 : 1;
303 while (timeout
-- > 0) {
304 if (snd_usb_ctl_msg(cval
->chip
->dev
, usb_rcvctrlpipe(cval
->chip
->dev
, 0),
306 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
307 validx
, cval
->ctrlif
| (cval
->id
<< 8),
308 buf
, val_len
, 100) >= 0) {
309 *value_ret
= convert_signed_value(cval
, snd_usb_combine_bytes(buf
, val_len
));
313 snd_printdd(KERN_ERR
"cannot get ctl value: req = 0x%x, wValue = 0x%x, wIndex = 0x%x, type = %d\n", request
, validx
, cval
->ctrlif
| (cval
->id
<< 8), cval
->val_type
);
317 static int get_cur_ctl_value(usb_mixer_elem_info_t
*cval
, int validx
, int *value
)
319 return get_ctl_value(cval
, GET_CUR
, validx
, value
);
322 /* channel = 0: master, 1 = first channel */
323 inline static int get_cur_mix_value(usb_mixer_elem_info_t
*cval
, int channel
, int *value
)
325 return get_ctl_value(cval
, GET_CUR
, (cval
->control
<< 8) | channel
, value
);
332 static int set_ctl_value(usb_mixer_elem_info_t
*cval
, int request
, int validx
, int value_set
)
334 unsigned char buf
[2];
335 int val_len
= cval
->val_type
>= USB_MIXER_S16
? 2 : 1;
338 value_set
= convert_bytes_value(cval
, value_set
);
339 buf
[0] = value_set
& 0xff;
340 buf
[1] = (value_set
>> 8) & 0xff;
341 while (timeout
-- > 0)
342 if (snd_usb_ctl_msg(cval
->chip
->dev
, usb_sndctrlpipe(cval
->chip
->dev
, 0),
344 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
345 validx
, cval
->ctrlif
| (cval
->id
<< 8),
346 buf
, val_len
, 100) >= 0)
348 snd_printdd(KERN_ERR
"cannot set ctl value: req = 0x%x, wValue = 0x%x, wIndex = 0x%x, type = %d, data = 0x%x/0x%x\n", request
, validx
, cval
->ctrlif
| (cval
->id
<< 8), cval
->val_type
, buf
[0], buf
[1]);
352 static int set_cur_ctl_value(usb_mixer_elem_info_t
*cval
, int validx
, int value
)
354 return set_ctl_value(cval
, SET_CUR
, validx
, value
);
357 inline static int set_cur_mix_value(usb_mixer_elem_info_t
*cval
, int channel
, int value
)
359 return set_ctl_value(cval
, SET_CUR
, (cval
->control
<< 8) | channel
, value
);
364 * parser routines begin here...
367 static int parse_audio_unit(mixer_build_t
*state
, int unitid
);
371 * check if the input/output channel routing is enabled on the given bitmap.
372 * used for mixer unit parser
374 static int check_matrix_bitmap(unsigned char *bmap
, int ich
, int och
, int num_outs
)
376 int idx
= ich
* num_outs
+ och
;
377 return bmap
[idx
>> 3] & (0x80 >> (idx
& 7));
382 * add an alsa control element
383 * search and increment the index until an empty slot is found.
385 * if failed, give up and free the control instance.
388 static int add_control_to_empty(snd_card_t
*card
, snd_kcontrol_t
*kctl
)
391 while (snd_ctl_find_id(card
, &kctl
->id
))
393 if ((err
= snd_ctl_add(card
, kctl
)) < 0) {
394 snd_printd(KERN_ERR
"cannot add control (err = %d)\n", err
);
395 snd_ctl_free_one(kctl
);
402 * get a terminal name string
405 static struct iterm_name_combo
{
409 { 0x0300, "Output" },
410 { 0x0301, "Speaker" },
411 { 0x0302, "Headphone" },
412 { 0x0303, "HMD Audio" },
413 { 0x0304, "Desktop Speaker" },
414 { 0x0305, "Room Speaker" },
415 { 0x0306, "Com Speaker" },
417 { 0x0600, "External In" },
418 { 0x0601, "Analog In" },
419 { 0x0602, "Digital In" },
421 { 0x0604, "Legacy In" },
422 { 0x0605, "IEC958 In" },
423 { 0x0606, "1394 DA Stream" },
424 { 0x0607, "1394 DV Stream" },
425 { 0x0700, "Embedded" },
426 { 0x0701, "Noise Source" },
427 { 0x0702, "Equalization Noise" },
431 { 0x0706, "MiniDisk" },
432 { 0x0707, "Analog Tape" },
433 { 0x0708, "Phonograph" },
434 { 0x0709, "VCR Audio" },
435 { 0x070a, "Video Disk Audio" },
436 { 0x070b, "DVD Audio" },
437 { 0x070c, "TV Tuner Audio" },
438 { 0x070d, "Satellite Rec Audio" },
439 { 0x070e, "Cable Tuner Audio" },
440 { 0x070f, "DSS Audio" },
441 { 0x0710, "Radio Receiver" },
442 { 0x0711, "Radio Transmitter" },
443 { 0x0712, "Multi-Track Recorder" },
444 { 0x0713, "Synthesizer" },
448 static int get_term_name(mixer_build_t
*state
, usb_audio_term_t
*iterm
,
449 unsigned char *name
, int maxlen
, int term_only
)
451 struct iterm_name_combo
*names
;
454 return snd_usb_copy_string_desc(state
, iterm
->name
, name
, maxlen
);
456 /* virtual type - not a real terminal */
457 if (iterm
->type
>> 16) {
460 switch (iterm
->type
>> 16) {
462 strcpy(name
, "Selector"); return 8;
463 case PROCESSING_UNIT
:
464 strcpy(name
, "Process Unit"); return 12;
466 strcpy(name
, "Ext Unit"); return 8;
468 strcpy(name
, "Mixer"); return 5;
470 return sprintf(name
, "Unit %d", iterm
->id
);
474 switch (iterm
->type
& 0xff00) {
476 strcpy(name
, "PCM"); return 3;
478 strcpy(name
, "Mic"); return 3;
480 strcpy(name
, "Headset"); return 7;
482 strcpy(name
, "Phone"); return 5;
485 for (names
= iterm_names
; names
->type
; names
++)
486 if (names
->type
== iterm
->type
) {
487 strcpy(name
, names
->name
);
488 return strlen(names
->name
);
495 * parse the source unit recursively until it reaches to a terminal
496 * or a branched unit.
498 static int check_input_term(mixer_build_t
*state
, int id
, usb_audio_term_t
*term
)
502 memset(term
, 0, sizeof(*term
));
503 while ((p1
= find_audio_control_unit(state
, id
)) != NULL
) {
507 term
->type
= combine_word(p1
+ 4);
508 term
->channels
= p1
[7];
509 term
->chconfig
= combine_word(p1
+ 8);
514 break; /* continue to parse */
516 term
->type
= p1
[2] << 16; /* virtual type */
517 term
->channels
= p1
[5 + p1
[4]];
518 term
->chconfig
= combine_word(p1
+ 6 + p1
[4]);
519 term
->name
= p1
[p1
[0] - 1];
522 /* call recursively to retrieve the channel info */
523 if (check_input_term(state
, p1
[5], term
) < 0)
525 term
->type
= p1
[2] << 16; /* virtual type */
527 term
->name
= p1
[9 + p1
[0] - 1];
529 case PROCESSING_UNIT
:
533 break; /* continue to parse */
535 term
->type
= p1
[2] << 16; /* virtual type */
536 term
->channels
= p1
[7 + p1
[6]];
537 term
->chconfig
= combine_word(p1
+ 8 + p1
[6]);
538 term
->name
= p1
[12 + p1
[6] + p1
[11 + p1
[6]]];
552 /* feature unit control information */
553 struct usb_feature_control_info
{
555 unsigned int type
; /* control type (mute, volume, etc.) */
558 static struct usb_feature_control_info audio_feature_info
[] = {
559 { "Mute", USB_MIXER_INV_BOOLEAN
},
560 { "Volume", USB_MIXER_S16
},
561 { "Tone Control - Bass", USB_MIXER_S8
},
562 { "Tone Control - Mid", USB_MIXER_S8
},
563 { "Tone Control - Treble", USB_MIXER_S8
},
564 { "Graphic Equalizer", USB_MIXER_S8
}, /* FIXME: not implemeted yet */
565 { "Auto Gain Control", USB_MIXER_BOOLEAN
},
566 { "Delay Control", USB_MIXER_U16
},
567 { "Bass Boost", USB_MIXER_BOOLEAN
},
568 { "Loudness", USB_MIXER_BOOLEAN
},
572 /* private_free callback */
573 static void usb_mixer_elem_free(snd_kcontrol_t
*kctl
)
575 if (kctl
->private_data
) {
576 kfree(kctl
->private_data
);
577 kctl
->private_data
= NULL
;
583 * interface to ALSA control for feature/mixer units
587 * retrieve the minimum and maximum values for the specified control
589 static int get_min_max(usb_mixer_elem_info_t
*cval
, int default_min
)
592 cval
->min
= default_min
;
593 cval
->max
= cval
->min
+ 1;
596 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
597 cval
->val_type
== USB_MIXER_INV_BOOLEAN
) {
598 cval
->initialized
= 1;
603 for (i
= 0; i
< MAX_CHANNELS
; i
++)
604 if (cval
->cmask
& (1 << i
)) {
609 if (get_ctl_value(cval
, GET_MAX
, (cval
->control
<< 8) | minchn
, &cval
->max
) < 0 ||
610 get_ctl_value(cval
, GET_MIN
, (cval
->control
<< 8) | minchn
, &cval
->min
) < 0) {
611 snd_printd(KERN_ERR
"%d:%d: cannot get min/max values for control %d (id %d)\n", cval
->id
, cval
->ctrlif
, cval
->control
, cval
->id
);
614 if (get_ctl_value(cval
, GET_RES
, (cval
->control
<< 8) | minchn
, &cval
->res
) < 0) {
617 int last_valid_res
= cval
->res
;
619 while (cval
->res
> 1) {
620 if (set_ctl_value(cval
, SET_RES
, (cval
->control
<< 8) | minchn
, cval
->res
/ 2) < 0)
624 if (get_ctl_value(cval
, GET_RES
, (cval
->control
<< 8) | minchn
, &cval
->res
) < 0)
625 cval
->res
= last_valid_res
;
629 cval
->initialized
= 1;
635 /* get a feature/mixer unit info */
636 static int mixer_ctl_feature_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
638 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
640 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
641 cval
->val_type
== USB_MIXER_INV_BOOLEAN
)
642 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
644 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
645 uinfo
->count
= cval
->channels
;
646 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
647 cval
->val_type
== USB_MIXER_INV_BOOLEAN
) {
648 uinfo
->value
.integer
.min
= 0;
649 uinfo
->value
.integer
.max
= 1;
651 if (! cval
->initialized
)
652 get_min_max(cval
, 0);
653 uinfo
->value
.integer
.min
= 0;
654 uinfo
->value
.integer
.max
= (cval
->max
- cval
->min
) / cval
->res
;
659 /* get the current value from feature/mixer unit */
660 static int mixer_ctl_feature_get(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
662 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
663 int c
, cnt
, val
, err
;
667 for (c
= 0; c
< MAX_CHANNELS
; c
++) {
668 if (cval
->cmask
& (1 << c
)) {
669 err
= get_cur_mix_value(cval
, c
+ 1, &val
);
671 if (cval
->chip
->ignore_ctl_error
) {
672 ucontrol
->value
.integer
.value
[0] = cval
->min
;
675 snd_printd(KERN_ERR
"cannot get current value for control %d ch %d: err = %d\n", cval
->control
, c
+ 1, err
);
678 val
= get_relative_value(cval
, val
);
679 ucontrol
->value
.integer
.value
[cnt
] = val
;
685 err
= get_cur_mix_value(cval
, 0, &val
);
687 if (cval
->chip
->ignore_ctl_error
) {
688 ucontrol
->value
.integer
.value
[0] = cval
->min
;
691 snd_printd(KERN_ERR
"cannot get current value for control %d master ch: err = %d\n", cval
->control
, err
);
694 val
= get_relative_value(cval
, val
);
695 ucontrol
->value
.integer
.value
[0] = val
;
700 /* put the current value to feature/mixer unit */
701 static int mixer_ctl_feature_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
703 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
704 int c
, cnt
, val
, oval
, err
;
709 for (c
= 0; c
< MAX_CHANNELS
; c
++) {
710 if (cval
->cmask
& (1 << c
)) {
711 err
= get_cur_mix_value(cval
, c
+ 1, &oval
);
713 if (cval
->chip
->ignore_ctl_error
)
717 val
= ucontrol
->value
.integer
.value
[cnt
];
718 val
= get_abs_value(cval
, val
);
720 set_cur_mix_value(cval
, c
+ 1, val
);
723 get_cur_mix_value(cval
, c
+ 1, &val
);
729 err
= get_cur_mix_value(cval
, 0, &oval
);
730 if (err
< 0 && cval
->chip
->ignore_ctl_error
)
734 val
= ucontrol
->value
.integer
.value
[0];
735 val
= get_abs_value(cval
, val
);
737 set_cur_mix_value(cval
, 0, val
);
744 static snd_kcontrol_new_t usb_feature_unit_ctl
= {
745 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
746 .name
= "", /* will be filled later manually */
747 .info
= mixer_ctl_feature_info
,
748 .get
= mixer_ctl_feature_get
,
749 .put
= mixer_ctl_feature_put
,
754 * build a feature control
757 static void build_feature_ctl(mixer_build_t
*state
, unsigned char *desc
,
758 unsigned int ctl_mask
, int control
,
759 usb_audio_term_t
*iterm
, int unitid
)
761 unsigned int len
= 0;
763 int nameid
= desc
[desc
[0] - 1];
764 snd_kcontrol_t
*kctl
;
765 usb_mixer_elem_info_t
*cval
;
767 control
++; /* change from zero-based to 1-based value */
769 if (control
== USB_FEATURE_GEQ
) {
770 /* FIXME: not supported yet */
774 if (check_ignored_ctl(state
, unitid
, control
))
777 cval
= kcalloc(1, sizeof(*cval
), GFP_KERNEL
);
779 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
782 cval
->chip
= state
->chip
;
783 cval
->ctrlif
= state
->ctrlif
;
785 cval
->control
= control
;
786 cval
->cmask
= ctl_mask
;
787 cval
->val_type
= audio_feature_info
[control
-1].type
;
789 cval
->channels
= 1; /* master channel */
792 for (i
= 0; i
< 16; i
++)
793 if (ctl_mask
& (1 << i
))
798 /* get min/max values */
799 get_min_max(cval
, 0);
801 kctl
= snd_ctl_new1(&usb_feature_unit_ctl
, cval
);
803 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
807 kctl
->private_free
= usb_mixer_elem_free
;
809 len
= check_mapped_name(state
, unitid
, control
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
810 mapped_name
= len
!= 0;
812 len
= snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
815 case USB_FEATURE_MUTE
:
816 case USB_FEATURE_VOLUME
:
817 /* determine the control name. the rule is:
818 * - if a name id is given in descriptor, use it.
819 * - if the connected input can be determined, then use the name
821 * - if the connected output can be determined, use it.
822 * - otherwise, anonymous name.
825 len
= get_term_name(state
, iterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 1);
827 len
= get_term_name(state
, &state
->oterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 1);
829 len
= snprintf(kctl
->id
.name
, sizeof(kctl
->id
.name
),
830 "Feature %d", unitid
);
832 /* determine the stream direction:
833 * if the connected output is USB stream, then it's likely a
834 * capture stream. otherwise it should be playback (hopefully :)
836 if (! mapped_name
&& ! (state
->oterm
.type
>> 16)) {
837 if ((state
->oterm
.type
& 0xff00) == 0x0100) {
838 len
= strlcat(kctl
->id
.name
, " Capture", sizeof(kctl
->id
.name
));
840 len
= strlcat(kctl
->id
.name
+ len
, " Playback", sizeof(kctl
->id
.name
));
843 strlcat(kctl
->id
.name
+ len
, control
== USB_FEATURE_MUTE
? " Switch" : " Volume",
844 sizeof(kctl
->id
.name
));
849 strlcpy(kctl
->id
.name
, audio_feature_info
[control
-1].name
,
850 sizeof(kctl
->id
.name
));
854 /* quirk for UDA1321/N101 */
855 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
856 /* is not very clear from datasheets */
857 /* I hope that the min value is -15360 for newer firmware --jk */
858 if (((state
->vendor
== 0x471 && (state
->product
== 0x104 || state
->product
== 0x105 || state
->product
== 0x101)) ||
859 (state
->vendor
== 0x672 && state
->product
== 0x1041)) && !strcmp(kctl
->id
.name
, "PCM Playback Volume") &&
860 cval
->min
== -15616) {
861 snd_printk("USB Audio: using volume control quirk for the UDA1321/N101 chip\n");
865 snd_printdd(KERN_INFO
"[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
866 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
, cval
->res
);
867 add_control_to_empty(state
->chip
->card
, kctl
);
873 * parse a feature unit
875 * most of controlls are defined here.
877 static int parse_audio_feature_unit(mixer_build_t
*state
, int unitid
, unsigned char *ftr
)
880 usb_audio_term_t iterm
;
881 unsigned int master_bits
, first_ch_bits
;
884 if (ftr
[0] < 7 || ! (csize
= ftr
[5]) || ftr
[0] < 7 + csize
) {
885 snd_printk(KERN_ERR
"usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid
);
889 /* parse the source unit */
890 if ((err
= parse_audio_unit(state
, ftr
[4])) < 0)
893 /* determine the input source type and name */
894 if (check_input_term(state
, ftr
[4], &iterm
) < 0)
897 channels
= (ftr
[0] - 7) / csize
- 1;
899 master_bits
= snd_usb_combine_bytes(ftr
+ 6, csize
);
901 first_ch_bits
= snd_usb_combine_bytes(ftr
+ 6 + csize
, csize
);
904 /* check all control types */
905 for (i
= 0; i
< 10; i
++) {
906 unsigned int ch_bits
= 0;
907 for (j
= 0; j
< channels
; j
++) {
908 unsigned int mask
= snd_usb_combine_bytes(ftr
+ 6 + csize
* (j
+1), csize
);
912 if (ch_bits
& 1) /* the first channel must be set (for ease of programming) */
913 build_feature_ctl(state
, ftr
, ch_bits
, i
, &iterm
, unitid
);
914 if (master_bits
& (1 << i
))
915 build_feature_ctl(state
, ftr
, 0, i
, &iterm
, unitid
);
927 * build a mixer unit control
929 * the callbacks are identical with feature unit.
930 * input channel number (zero based) is given in control field instead.
933 static void build_mixer_unit_ctl(mixer_build_t
*state
, unsigned char *desc
,
934 int in_pin
, int in_ch
, int unitid
,
935 usb_audio_term_t
*iterm
)
937 usb_mixer_elem_info_t
*cval
;
938 unsigned int input_pins
= desc
[4];
939 unsigned int num_outs
= desc
[5 + input_pins
];
941 snd_kcontrol_t
*kctl
;
943 if (check_ignored_ctl(state
, unitid
, 0))
946 cval
= kcalloc(1, sizeof(*cval
), GFP_KERNEL
);
950 cval
->chip
= state
->chip
;
951 cval
->ctrlif
= state
->ctrlif
;
953 cval
->control
= in_ch
+ 1; /* based on 1 */
954 cval
->val_type
= USB_MIXER_S16
;
955 for (i
= 0; i
< num_outs
; i
++) {
956 if (check_matrix_bitmap(desc
+ 9 + input_pins
, in_ch
, i
, num_outs
)) {
957 cval
->cmask
|= (1 << i
);
962 /* get min/max values */
963 get_min_max(cval
, 0);
965 kctl
= snd_ctl_new1(&usb_feature_unit_ctl
, cval
);
967 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
971 kctl
->private_free
= usb_mixer_elem_free
;
973 len
= check_mapped_name(state
, unitid
, 0, kctl
->id
.name
, sizeof(kctl
->id
.name
));
975 len
= get_term_name(state
, iterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 0);
977 len
= sprintf(kctl
->id
.name
, "Mixer Source %d", in_ch
+ 1);
978 strlcat(kctl
->id
.name
+ len
, " Volume", sizeof(kctl
->id
.name
));
980 snd_printdd(KERN_INFO
"[%d] MU [%s] ch = %d, val = %d/%d\n",
981 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
);
982 add_control_to_empty(state
->chip
->card
, kctl
);
989 static int parse_audio_mixer_unit(mixer_build_t
*state
, int unitid
, unsigned char *desc
)
991 usb_audio_term_t iterm
;
992 int input_pins
, num_ins
, num_outs
;
995 if (desc
[0] < 11 || ! (input_pins
= desc
[4]) || ! (num_outs
= desc
[5 + input_pins
])) {
996 snd_printk(KERN_ERR
"invalid MIXER UNIT descriptor %d\n", unitid
);
999 /* no bmControls field (e.g. Maya44) -> ignore */
1000 if (desc
[0] <= 10 + input_pins
) {
1001 snd_printdd(KERN_INFO
"MU %d has no bmControls field\n", unitid
);
1007 for (pin
= 0; pin
< input_pins
; pin
++) {
1008 err
= parse_audio_unit(state
, desc
[5 + pin
]);
1011 err
= check_input_term(state
, desc
[5 + pin
], &iterm
);
1014 num_ins
+= iterm
.channels
;
1015 for (; ich
< num_ins
; ++ich
) {
1016 int och
, ich_has_controls
= 0;
1018 for (och
= 0; och
< num_outs
; ++och
) {
1019 if (check_matrix_bitmap(desc
+ 9 + input_pins
,
1020 ich
, och
, num_outs
)) {
1021 ich_has_controls
= 1;
1025 if (ich_has_controls
)
1026 build_mixer_unit_ctl(state
, desc
, pin
, ich
,
1035 * Processing Unit / Extension Unit
1038 /* get callback for processing/extension unit */
1039 static int mixer_ctl_procunit_get(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1041 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1044 err
= get_cur_ctl_value(cval
, cval
->control
<< 8, &val
);
1045 if (err
< 0 && cval
->chip
->ignore_ctl_error
) {
1046 ucontrol
->value
.integer
.value
[0] = cval
->min
;
1051 val
= get_relative_value(cval
, val
);
1052 ucontrol
->value
.integer
.value
[0] = val
;
1056 /* put callback for processing/extension unit */
1057 static int mixer_ctl_procunit_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1059 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1062 err
= get_cur_ctl_value(cval
, cval
->control
<< 8, &oval
);
1064 if (cval
->chip
->ignore_ctl_error
)
1068 val
= ucontrol
->value
.integer
.value
[0];
1069 val
= get_abs_value(cval
, val
);
1071 set_cur_ctl_value(cval
, cval
->control
<< 8, val
);
1077 /* alsa control interface for processing/extension unit */
1078 static snd_kcontrol_new_t mixer_procunit_ctl
= {
1079 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1080 .name
= "", /* will be filled later */
1081 .info
= mixer_ctl_feature_info
,
1082 .get
= mixer_ctl_procunit_get
,
1083 .put
= mixer_ctl_procunit_put
,
1088 * predefined data for processing units
1090 struct procunit_value_info
{
1097 struct procunit_info
{
1100 struct procunit_value_info
*values
;
1103 static struct procunit_value_info updown_proc_info
[] = {
1104 { USB_PROC_UPDOWN_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1105 { USB_PROC_UPDOWN_MODE_SEL
, "Mode Select", USB_MIXER_U8
, 1 },
1108 static struct procunit_value_info prologic_proc_info
[] = {
1109 { USB_PROC_PROLOGIC_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1110 { USB_PROC_PROLOGIC_MODE_SEL
, "Mode Select", USB_MIXER_U8
, 1 },
1113 static struct procunit_value_info threed_enh_proc_info
[] = {
1114 { USB_PROC_3DENH_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1115 { USB_PROC_3DENH_SPACE
, "Spaciousness", USB_MIXER_U8
},
1118 static struct procunit_value_info reverb_proc_info
[] = {
1119 { USB_PROC_REVERB_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1120 { USB_PROC_REVERB_LEVEL
, "Level", USB_MIXER_U8
},
1121 { USB_PROC_REVERB_TIME
, "Time", USB_MIXER_U16
},
1122 { USB_PROC_REVERB_DELAY
, "Delay", USB_MIXER_U8
},
1125 static struct procunit_value_info chorus_proc_info
[] = {
1126 { USB_PROC_CHORUS_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1127 { USB_PROC_CHORUS_LEVEL
, "Level", USB_MIXER_U8
},
1128 { USB_PROC_CHORUS_RATE
, "Rate", USB_MIXER_U16
},
1129 { USB_PROC_CHORUS_DEPTH
, "Depth", USB_MIXER_U16
},
1132 static struct procunit_value_info dcr_proc_info
[] = {
1133 { USB_PROC_DCR_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1134 { USB_PROC_DCR_RATIO
, "Ratio", USB_MIXER_U16
},
1135 { USB_PROC_DCR_MAX_AMP
, "Max Amp", USB_MIXER_S16
},
1136 { USB_PROC_DCR_THRESHOLD
, "Threshold", USB_MIXER_S16
},
1137 { USB_PROC_DCR_ATTACK
, "Attack Time", USB_MIXER_U16
},
1138 { USB_PROC_DCR_RELEASE
, "Release Time", USB_MIXER_U16
},
1142 static struct procunit_info procunits
[] = {
1143 { USB_PROC_UPDOWN
, "Up Down", updown_proc_info
},
1144 { USB_PROC_PROLOGIC
, "Dolby Prologic", prologic_proc_info
},
1145 { USB_PROC_3DENH
, "3D Stereo Extender", threed_enh_proc_info
},
1146 { USB_PROC_REVERB
, "Reverb", reverb_proc_info
},
1147 { USB_PROC_CHORUS
, "Chorus", chorus_proc_info
},
1148 { USB_PROC_DCR
, "DCR", dcr_proc_info
},
1153 * build a processing/extension unit
1155 static int build_audio_procunit(mixer_build_t
*state
, int unitid
, unsigned char *dsc
, struct procunit_info
*list
, char *name
)
1157 int num_ins
= dsc
[6];
1158 usb_mixer_elem_info_t
*cval
;
1159 snd_kcontrol_t
*kctl
;
1160 int i
, err
, nameid
, type
, len
;
1161 struct procunit_info
*info
;
1162 struct procunit_value_info
*valinfo
;
1163 static struct procunit_value_info default_value_info
[] = {
1164 { 0x01, "Switch", USB_MIXER_BOOLEAN
},
1167 static struct procunit_info default_info
= {
1168 0, NULL
, default_value_info
1171 if (dsc
[0] < 13 || dsc
[0] < 13 + num_ins
|| dsc
[0] < num_ins
+ dsc
[11 + num_ins
]) {
1172 snd_printk(KERN_ERR
"invalid %s descriptor (id %d)\n", name
, unitid
);
1176 for (i
= 0; i
< num_ins
; i
++) {
1177 if ((err
= parse_audio_unit(state
, dsc
[7 + i
])) < 0)
1181 type
= combine_word(&dsc
[4]);
1183 return 0; /* undefined? */
1185 for (info
= list
; info
&& info
->type
; info
++)
1186 if (info
->type
== type
)
1188 if (! info
|| ! info
->type
)
1189 info
= &default_info
;
1191 for (valinfo
= info
->values
; valinfo
->control
; valinfo
++) {
1192 /* FIXME: bitmap might be longer than 8bit */
1193 if (! (dsc
[12 + num_ins
] & (1 << (valinfo
->control
- 1))))
1195 if (check_ignored_ctl(state
, unitid
, valinfo
->control
))
1197 cval
= kcalloc(1, sizeof(*cval
), GFP_KERNEL
);
1199 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1202 cval
->chip
= state
->chip
;
1203 cval
->ctrlif
= state
->ctrlif
;
1205 cval
->control
= valinfo
->control
;
1206 cval
->val_type
= valinfo
->val_type
;
1209 /* get min/max values */
1210 if (type
== USB_PROC_UPDOWN
&& cval
->control
== USB_PROC_UPDOWN_MODE_SEL
) {
1211 /* FIXME: hard-coded */
1213 cval
->max
= dsc
[15];
1215 cval
->initialized
= 1;
1217 get_min_max(cval
, valinfo
->min_value
);
1219 kctl
= snd_ctl_new1(&mixer_procunit_ctl
, cval
);
1221 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1225 kctl
->private_free
= usb_mixer_elem_free
;
1227 if (check_mapped_name(state
, unitid
, cval
->control
, kctl
->id
.name
, sizeof(kctl
->id
.name
)))
1229 else if (info
->name
)
1230 strlcpy(kctl
->id
.name
, info
->name
, sizeof(kctl
->id
.name
));
1232 nameid
= dsc
[12 + num_ins
+ dsc
[11 + num_ins
]];
1235 len
= snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1237 strlcpy(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
));
1239 strlcat(kctl
->id
.name
, " ", sizeof(kctl
->id
.name
));
1240 strlcat(kctl
->id
.name
, valinfo
->suffix
, sizeof(kctl
->id
.name
));
1242 snd_printdd(KERN_INFO
"[%d] PU [%s] ch = %d, val = %d/%d\n",
1243 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
);
1244 if ((err
= add_control_to_empty(state
->chip
->card
, kctl
)) < 0)
1251 static int parse_audio_processing_unit(mixer_build_t
*state
, int unitid
, unsigned char *desc
)
1253 return build_audio_procunit(state
, unitid
, desc
, procunits
, "Processing Unit");
1256 static int parse_audio_extension_unit(mixer_build_t
*state
, int unitid
, unsigned char *desc
)
1258 return build_audio_procunit(state
, unitid
, desc
, NULL
, "Extension Unit");
1266 /* info callback for selector unit
1267 * use an enumerator type for routing
1269 static int mixer_ctl_selector_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
1271 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1272 char **itemlist
= (char **)kcontrol
->private_value
;
1274 snd_assert(itemlist
, return -EINVAL
);
1275 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1277 uinfo
->value
.enumerated
.items
= cval
->max
;
1278 if ((int)uinfo
->value
.enumerated
.item
>= cval
->max
)
1279 uinfo
->value
.enumerated
.item
= cval
->max
- 1;
1280 strcpy(uinfo
->value
.enumerated
.name
, itemlist
[uinfo
->value
.enumerated
.item
]);
1284 /* get callback for selector unit */
1285 static int mixer_ctl_selector_get(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1287 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1290 err
= get_cur_ctl_value(cval
, 0, &val
);
1292 if (cval
->chip
->ignore_ctl_error
) {
1293 ucontrol
->value
.enumerated
.item
[0] = 0;
1298 val
= get_relative_value(cval
, val
);
1299 ucontrol
->value
.enumerated
.item
[0] = val
;
1303 /* put callback for selector unit */
1304 static int mixer_ctl_selector_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1306 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1309 err
= get_cur_ctl_value(cval
, 0, &oval
);
1311 if (cval
->chip
->ignore_ctl_error
)
1315 val
= ucontrol
->value
.enumerated
.item
[0];
1316 val
= get_abs_value(cval
, val
);
1318 set_cur_ctl_value(cval
, 0, val
);
1324 /* alsa control interface for selector unit */
1325 static snd_kcontrol_new_t mixer_selectunit_ctl
= {
1326 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1327 .name
= "", /* will be filled later */
1328 .info
= mixer_ctl_selector_info
,
1329 .get
= mixer_ctl_selector_get
,
1330 .put
= mixer_ctl_selector_put
,
1334 /* private free callback.
1335 * free both private_data and private_value
1337 static void usb_mixer_selector_elem_free(snd_kcontrol_t
*kctl
)
1341 if (kctl
->private_data
) {
1342 usb_mixer_elem_info_t
*cval
= kctl
->private_data
;
1343 num_ins
= cval
->max
;
1345 kctl
->private_data
= NULL
;
1347 if (kctl
->private_value
) {
1348 char **itemlist
= (char **)kctl
->private_value
;
1349 for (i
= 0; i
< num_ins
; i
++)
1352 kctl
->private_value
= 0;
1357 * parse a selector unit
1359 static int parse_audio_selector_unit(mixer_build_t
*state
, int unitid
, unsigned char *desc
)
1361 unsigned int num_ins
= desc
[4];
1362 unsigned int i
, nameid
, len
;
1364 usb_mixer_elem_info_t
*cval
;
1365 snd_kcontrol_t
*kctl
;
1368 if (! num_ins
|| desc
[0] < 6 + num_ins
) {
1369 snd_printk(KERN_ERR
"invalid SELECTOR UNIT descriptor %d\n", unitid
);
1373 for (i
= 0; i
< num_ins
; i
++) {
1374 if ((err
= parse_audio_unit(state
, desc
[5 + i
])) < 0)
1378 if (num_ins
== 1) /* only one ? nonsense! */
1381 if (check_ignored_ctl(state
, unitid
, 0))
1384 cval
= kcalloc(1, sizeof(*cval
), GFP_KERNEL
);
1386 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1389 cval
->chip
= state
->chip
;
1390 cval
->ctrlif
= state
->ctrlif
;
1392 cval
->val_type
= USB_MIXER_U8
;
1395 cval
->max
= num_ins
;
1397 cval
->initialized
= 1;
1399 namelist
= kmalloc(sizeof(char *) * num_ins
, GFP_KERNEL
);
1401 snd_printk(KERN_ERR
"cannot malloc\n");
1405 #define MAX_ITEM_NAME_LEN 64
1406 for (i
= 0; i
< num_ins
; i
++) {
1407 usb_audio_term_t iterm
;
1409 namelist
[i
] = kmalloc(MAX_ITEM_NAME_LEN
, GFP_KERNEL
);
1410 if (! namelist
[i
]) {
1411 snd_printk(KERN_ERR
"cannot malloc\n");
1418 if (check_input_term(state
, desc
[5 + i
], &iterm
) >= 0)
1419 len
= get_term_name(state
, &iterm
, namelist
[i
], MAX_ITEM_NAME_LEN
, 0);
1421 sprintf(namelist
[i
], "Input %d", i
);
1424 kctl
= snd_ctl_new1(&mixer_selectunit_ctl
, cval
);
1426 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1430 kctl
->private_value
= (unsigned long)namelist
;
1431 kctl
->private_free
= usb_mixer_selector_elem_free
;
1433 nameid
= desc
[desc
[0] - 1];
1434 len
= check_mapped_name(state
, unitid
, 0, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1438 snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1440 len
= get_term_name(state
, &state
->oterm
,
1441 kctl
->id
.name
, sizeof(kctl
->id
.name
), 0);
1443 strlcpy(kctl
->id
.name
, "USB", sizeof(kctl
->id
.name
));
1445 if ((state
->oterm
.type
& 0xff00) == 0x0100)
1446 strlcat(kctl
->id
.name
, " Capture Source", sizeof(kctl
->id
.name
));
1448 strlcat(kctl
->id
.name
, " Playback Source", sizeof(kctl
->id
.name
));
1451 snd_printdd(KERN_INFO
"[%d] SU [%s] items = %d\n",
1452 cval
->id
, kctl
->id
.name
, num_ins
);
1453 if ((err
= add_control_to_empty(state
->chip
->card
, kctl
)) < 0)
1461 * parse an audio unit recursively
1464 static int parse_audio_unit(mixer_build_t
*state
, int unitid
)
1468 if (test_and_set_bit(unitid
, state
->unitbitmap
))
1469 return 0; /* the unit already visited */
1471 p1
= find_audio_control_unit(state
, unitid
);
1473 snd_printk(KERN_ERR
"usbaudio: unit %d not found!\n", unitid
);
1478 case INPUT_TERMINAL
:
1481 return parse_audio_mixer_unit(state
, unitid
, p1
);
1483 return parse_audio_selector_unit(state
, unitid
, p1
);
1485 return parse_audio_feature_unit(state
, unitid
, p1
);
1486 case PROCESSING_UNIT
:
1487 return parse_audio_processing_unit(state
, unitid
, p1
);
1488 case EXTENSION_UNIT
:
1489 return parse_audio_extension_unit(state
, unitid
, p1
);
1491 snd_printk(KERN_ERR
"usbaudio: unit %u: unexpected type 0x%02x\n", unitid
, p1
[2]);
1497 * create mixer controls
1499 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1501 int snd_usb_create_mixer(snd_usb_audio_t
*chip
, int ctrlif
)
1503 unsigned char *desc
;
1504 mixer_build_t state
;
1506 const struct usbmix_ctl_map
*map
;
1507 struct usb_device_descriptor
*dev
= &chip
->dev
->descriptor
;
1508 struct usb_host_interface
*hostif
= &usb_ifnum_to_if(chip
->dev
, ctrlif
)->altsetting
[0];
1510 strcpy(chip
->card
->mixername
, "USB Mixer");
1512 memset(&state
, 0, sizeof(state
));
1514 state
.buffer
= hostif
->extra
;
1515 state
.buflen
= hostif
->extralen
;
1516 state
.ctrlif
= ctrlif
;
1517 state
.vendor
= le16_to_cpu(dev
->idVendor
);
1518 state
.product
= le16_to_cpu(dev
->idProduct
);
1520 /* check the mapping table */
1521 for (map
= usbmix_ctl_maps
; map
->vendor
; map
++) {
1522 if (map
->vendor
== state
.vendor
&& map
->product
== state
.product
) {
1523 state
.map
= map
->map
;
1524 chip
->ignore_ctl_error
= map
->ignore_ctl_error
;
1528 #ifdef IGNORE_CTL_ERROR
1529 chip
->ignore_ctl_error
= 1;
1533 while ((desc
= snd_usb_find_csint_desc(hostif
->extra
, hostif
->extralen
, desc
, OUTPUT_TERMINAL
)) != NULL
) {
1535 continue; /* invalid descriptor? */
1536 set_bit(desc
[3], state
.unitbitmap
); /* mark terminal ID as visited */
1537 state
.oterm
.id
= desc
[3];
1538 state
.oterm
.type
= combine_word(&desc
[4]);
1539 state
.oterm
.name
= desc
[8];
1540 err
= parse_audio_unit(&state
, desc
[7]);