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>
38 #include <sound/hwdep.h>
39 #include <sound/info.h>
40 #include <sound/tlv.h>
47 /* ignore error from controls - for debugging */
48 /* #define IGNORE_CTL_ERROR */
51 * Sound Blaster remote control configuration
53 * format of remote control data:
55 * Audigy 2 NX: 06 80 xx 00 00 00
56 * Live! 24-bit: 06 80 xx yy 22 83
58 static const struct rc_config
{
66 { USB_ID(0x041e, 0x3000), 0, 1, 2, 18, 0x0013 }, /* Extigy */
67 { USB_ID(0x041e, 0x3020), 2, 1, 6, 18, 0x0013 }, /* Audigy 2 NX */
68 { USB_ID(0x041e, 0x3040), 2, 2, 6, 2, 0x6e91 }, /* Live! 24-bit */
71 struct usb_mixer_interface
{
72 struct snd_usb_audio
*chip
;
74 struct list_head list
;
75 unsigned int ignore_ctl_error
;
77 struct usb_mixer_elem_info
**id_elems
; /* array[256], indexed by unit id */
79 /* Sound Blaster remote control stuff */
80 const struct rc_config
*rc_cfg
;
81 unsigned long rc_hwdep_open
;
83 wait_queue_head_t rc_waitq
;
85 struct usb_ctrlrequest
*rc_setup_packet
;
92 struct usb_audio_term
{
96 unsigned int chconfig
;
100 struct usbmix_name_map
;
103 struct snd_usb_audio
*chip
;
104 struct usb_mixer_interface
*mixer
;
105 unsigned char *buffer
;
107 DECLARE_BITMAP(unitbitmap
, 256);
108 struct usb_audio_term oterm
;
109 const struct usbmix_name_map
*map
;
110 const struct usbmix_selector_map
*selector_map
;
113 struct usb_mixer_elem_info
{
114 struct usb_mixer_interface
*mixer
;
115 struct usb_mixer_elem_info
*next_id_elem
; /* list of controls with same id */
116 struct snd_ctl_elem_id
*elem_id
;
118 unsigned int control
; /* CS or ICN (high byte) */
119 unsigned int cmask
; /* channel mask bitmap: 0 = master */
128 USB_FEATURE_NONE
= 0,
129 USB_FEATURE_MUTE
= 1,
137 USB_FEATURE_BASSBOOST
,
143 USB_MIXER_INV_BOOLEAN
,
152 USB_PROC_UPDOWN_SWITCH
= 1,
153 USB_PROC_UPDOWN_MODE_SEL
= 2,
155 USB_PROC_PROLOGIC
= 2,
156 USB_PROC_PROLOGIC_SWITCH
= 1,
157 USB_PROC_PROLOGIC_MODE_SEL
= 2,
160 USB_PROC_3DENH_SWITCH
= 1,
161 USB_PROC_3DENH_SPACE
= 2,
164 USB_PROC_REVERB_SWITCH
= 1,
165 USB_PROC_REVERB_LEVEL
= 2,
166 USB_PROC_REVERB_TIME
= 3,
167 USB_PROC_REVERB_DELAY
= 4,
170 USB_PROC_CHORUS_SWITCH
= 1,
171 USB_PROC_CHORUS_LEVEL
= 2,
172 USB_PROC_CHORUS_RATE
= 3,
173 USB_PROC_CHORUS_DEPTH
= 4,
176 USB_PROC_DCR_SWITCH
= 1,
177 USB_PROC_DCR_RATIO
= 2,
178 USB_PROC_DCR_MAX_AMP
= 3,
179 USB_PROC_DCR_THRESHOLD
= 4,
180 USB_PROC_DCR_ATTACK
= 5,
181 USB_PROC_DCR_RELEASE
= 6,
184 #define MAX_CHANNELS 10 /* max logical channels */
188 * manual mapping of mixer names
189 * if the mixer topology is too complicated and the parsed names are
190 * ambiguous, add the entries in usbmixer_maps.c.
192 #include "usbmixer_maps.c"
194 /* get the mapped name if the unit matches */
195 static int check_mapped_name(struct mixer_build
*state
, int unitid
, int control
, char *buf
, int buflen
)
197 const struct usbmix_name_map
*p
;
202 for (p
= state
->map
; p
->id
; p
++) {
203 if (p
->id
== unitid
&& p
->name
&&
204 (! control
|| ! p
->control
|| control
== p
->control
)) {
206 return strlcpy(buf
, p
->name
, buflen
);
212 /* check whether the control should be ignored */
213 static int check_ignored_ctl(struct mixer_build
*state
, int unitid
, int control
)
215 const struct usbmix_name_map
*p
;
219 for (p
= state
->map
; p
->id
; p
++) {
220 if (p
->id
== unitid
&& ! p
->name
&&
221 (! control
|| ! p
->control
|| control
== p
->control
)) {
222 // printk("ignored control %d:%d\n", unitid, control);
229 /* get the mapped selector source name */
230 static int check_mapped_selector_name(struct mixer_build
*state
, int unitid
,
231 int index
, char *buf
, int buflen
)
233 const struct usbmix_selector_map
*p
;
235 if (! state
->selector_map
)
237 for (p
= state
->selector_map
; p
->id
; p
++) {
238 if (p
->id
== unitid
&& index
< p
->count
)
239 return strlcpy(buf
, p
->names
[index
], buflen
);
245 * find an audio control unit with the given unit id
247 static void *find_audio_control_unit(struct mixer_build
*state
, unsigned char unit
)
252 while ((p
= snd_usb_find_desc(state
->buffer
, state
->buflen
, p
,
253 USB_DT_CS_INTERFACE
)) != NULL
) {
254 if (p
[0] >= 4 && p
[2] >= INPUT_TERMINAL
&& p
[2] <= EXTENSION_UNIT
&& p
[3] == unit
)
262 * copy a string with the given id
264 static int snd_usb_copy_string_desc(struct mixer_build
*state
, int index
, char *buf
, int maxlen
)
266 int len
= usb_string(state
->chip
->dev
, index
, buf
, maxlen
- 1);
272 * convert from the byte/word on usb descriptor to the zero-based integer
274 static int convert_signed_value(struct usb_mixer_elem_info
*cval
, int val
)
276 switch (cval
->val_type
) {
277 case USB_MIXER_BOOLEAN
:
279 case USB_MIXER_INV_BOOLEAN
:
302 * convert from the zero-based int to the byte/word for usb descriptor
304 static int convert_bytes_value(struct usb_mixer_elem_info
*cval
, int val
)
306 switch (cval
->val_type
) {
307 case USB_MIXER_BOOLEAN
:
309 case USB_MIXER_INV_BOOLEAN
:
318 return 0; /* not reached */
321 static int get_relative_value(struct usb_mixer_elem_info
*cval
, int val
)
327 else if (val
>= cval
->max
)
328 return (cval
->max
- cval
->min
+ cval
->res
- 1) / cval
->res
;
330 return (val
- cval
->min
) / cval
->res
;
333 static int get_abs_value(struct usb_mixer_elem_info
*cval
, int val
)
348 * retrieve a mixer value
351 static int get_ctl_value(struct usb_mixer_elem_info
*cval
, int request
, int validx
, int *value_ret
)
353 unsigned char buf
[2];
354 int val_len
= cval
->val_type
>= USB_MIXER_S16
? 2 : 1;
357 while (timeout
-- > 0) {
358 if (snd_usb_ctl_msg(cval
->mixer
->chip
->dev
,
359 usb_rcvctrlpipe(cval
->mixer
->chip
->dev
, 0),
361 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
362 validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8),
363 buf
, val_len
, 100) >= val_len
) {
364 *value_ret
= convert_signed_value(cval
, snd_usb_combine_bytes(buf
, val_len
));
368 snd_printdd(KERN_ERR
"cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
369 request
, validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8), cval
->val_type
);
373 static int get_cur_ctl_value(struct usb_mixer_elem_info
*cval
, int validx
, int *value
)
375 return get_ctl_value(cval
, GET_CUR
, validx
, value
);
378 /* channel = 0: master, 1 = first channel */
379 static inline int get_cur_mix_value(struct usb_mixer_elem_info
*cval
, int channel
, int *value
)
381 return get_ctl_value(cval
, GET_CUR
, (cval
->control
<< 8) | channel
, value
);
388 static int set_ctl_value(struct usb_mixer_elem_info
*cval
, int request
, int validx
, int value_set
)
390 unsigned char buf
[2];
391 int val_len
= cval
->val_type
>= USB_MIXER_S16
? 2 : 1;
394 value_set
= convert_bytes_value(cval
, value_set
);
395 buf
[0] = value_set
& 0xff;
396 buf
[1] = (value_set
>> 8) & 0xff;
397 while (timeout
-- > 0)
398 if (snd_usb_ctl_msg(cval
->mixer
->chip
->dev
,
399 usb_sndctrlpipe(cval
->mixer
->chip
->dev
, 0),
401 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
402 validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8),
403 buf
, val_len
, 100) >= 0)
405 snd_printdd(KERN_ERR
"cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
406 request
, validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8), cval
->val_type
, buf
[0], buf
[1]);
410 static int set_cur_ctl_value(struct usb_mixer_elem_info
*cval
, int validx
, int value
)
412 return set_ctl_value(cval
, SET_CUR
, validx
, value
);
415 static inline int set_cur_mix_value(struct usb_mixer_elem_info
*cval
, int channel
, int value
)
417 return set_ctl_value(cval
, SET_CUR
, (cval
->control
<< 8) | channel
, value
);
421 * TLV callback for mixer volume controls
423 static int mixer_vol_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
424 unsigned int size
, unsigned int __user
*_tlv
)
426 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
427 DECLARE_TLV_DB_SCALE(scale
, 0, 0, 0);
429 if (size
< sizeof(scale
))
431 /* USB descriptions contain the dB scale in 1/256 dB unit
432 * while ALSA TLV contains in 1/100 dB unit
434 scale
[2] = (convert_signed_value(cval
, cval
->min
) * 100) / 256;
435 scale
[3] = (convert_signed_value(cval
, cval
->res
) * 100) / 256;
436 if (copy_to_user(_tlv
, scale
, sizeof(scale
)))
442 * parser routines begin here...
445 static int parse_audio_unit(struct mixer_build
*state
, int unitid
);
449 * check if the input/output channel routing is enabled on the given bitmap.
450 * used for mixer unit parser
452 static int check_matrix_bitmap(unsigned char *bmap
, int ich
, int och
, int num_outs
)
454 int idx
= ich
* num_outs
+ och
;
455 return bmap
[idx
>> 3] & (0x80 >> (idx
& 7));
460 * add an alsa control element
461 * search and increment the index until an empty slot is found.
463 * if failed, give up and free the control instance.
466 static int add_control_to_empty(struct mixer_build
*state
, struct snd_kcontrol
*kctl
)
468 struct usb_mixer_elem_info
*cval
= kctl
->private_data
;
471 while (snd_ctl_find_id(state
->chip
->card
, &kctl
->id
))
473 if ((err
= snd_ctl_add(state
->chip
->card
, kctl
)) < 0) {
474 snd_printd(KERN_ERR
"cannot add control (err = %d)\n", err
);
477 cval
->elem_id
= &kctl
->id
;
478 cval
->next_id_elem
= state
->mixer
->id_elems
[cval
->id
];
479 state
->mixer
->id_elems
[cval
->id
] = cval
;
485 * get a terminal name string
488 static struct iterm_name_combo
{
492 { 0x0300, "Output" },
493 { 0x0301, "Speaker" },
494 { 0x0302, "Headphone" },
495 { 0x0303, "HMD Audio" },
496 { 0x0304, "Desktop Speaker" },
497 { 0x0305, "Room Speaker" },
498 { 0x0306, "Com Speaker" },
500 { 0x0600, "External In" },
501 { 0x0601, "Analog In" },
502 { 0x0602, "Digital In" },
504 { 0x0604, "Legacy In" },
505 { 0x0605, "IEC958 In" },
506 { 0x0606, "1394 DA Stream" },
507 { 0x0607, "1394 DV Stream" },
508 { 0x0700, "Embedded" },
509 { 0x0701, "Noise Source" },
510 { 0x0702, "Equalization Noise" },
514 { 0x0706, "MiniDisk" },
515 { 0x0707, "Analog Tape" },
516 { 0x0708, "Phonograph" },
517 { 0x0709, "VCR Audio" },
518 { 0x070a, "Video Disk Audio" },
519 { 0x070b, "DVD Audio" },
520 { 0x070c, "TV Tuner Audio" },
521 { 0x070d, "Satellite Rec Audio" },
522 { 0x070e, "Cable Tuner Audio" },
523 { 0x070f, "DSS Audio" },
524 { 0x0710, "Radio Receiver" },
525 { 0x0711, "Radio Transmitter" },
526 { 0x0712, "Multi-Track Recorder" },
527 { 0x0713, "Synthesizer" },
531 static int get_term_name(struct mixer_build
*state
, struct usb_audio_term
*iterm
,
532 unsigned char *name
, int maxlen
, int term_only
)
534 struct iterm_name_combo
*names
;
537 return snd_usb_copy_string_desc(state
, iterm
->name
, name
, maxlen
);
539 /* virtual type - not a real terminal */
540 if (iterm
->type
>> 16) {
543 switch (iterm
->type
>> 16) {
545 strcpy(name
, "Selector"); return 8;
546 case PROCESSING_UNIT
:
547 strcpy(name
, "Process Unit"); return 12;
549 strcpy(name
, "Ext Unit"); return 8;
551 strcpy(name
, "Mixer"); return 5;
553 return sprintf(name
, "Unit %d", iterm
->id
);
557 switch (iterm
->type
& 0xff00) {
559 strcpy(name
, "PCM"); return 3;
561 strcpy(name
, "Mic"); return 3;
563 strcpy(name
, "Headset"); return 7;
565 strcpy(name
, "Phone"); return 5;
568 for (names
= iterm_names
; names
->type
; names
++)
569 if (names
->type
== iterm
->type
) {
570 strcpy(name
, names
->name
);
571 return strlen(names
->name
);
578 * parse the source unit recursively until it reaches to a terminal
579 * or a branched unit.
581 static int check_input_term(struct mixer_build
*state
, int id
, struct usb_audio_term
*term
)
585 memset(term
, 0, sizeof(*term
));
586 while ((p1
= find_audio_control_unit(state
, id
)) != NULL
) {
590 term
->type
= combine_word(p1
+ 4);
591 term
->channels
= p1
[7];
592 term
->chconfig
= combine_word(p1
+ 8);
597 break; /* continue to parse */
599 term
->type
= p1
[2] << 16; /* virtual type */
600 term
->channels
= p1
[5 + p1
[4]];
601 term
->chconfig
= combine_word(p1
+ 6 + p1
[4]);
602 term
->name
= p1
[p1
[0] - 1];
605 /* call recursively to retrieve the channel info */
606 if (check_input_term(state
, p1
[5], term
) < 0)
608 term
->type
= p1
[2] << 16; /* virtual type */
610 term
->name
= p1
[9 + p1
[0] - 1];
612 case PROCESSING_UNIT
:
616 break; /* continue to parse */
618 term
->type
= p1
[2] << 16; /* virtual type */
619 term
->channels
= p1
[7 + p1
[6]];
620 term
->chconfig
= combine_word(p1
+ 8 + p1
[6]);
621 term
->name
= p1
[12 + p1
[6] + p1
[11 + p1
[6]]];
635 /* feature unit control information */
636 struct usb_feature_control_info
{
638 unsigned int type
; /* control type (mute, volume, etc.) */
641 static struct usb_feature_control_info audio_feature_info
[] = {
642 { "Mute", USB_MIXER_INV_BOOLEAN
},
643 { "Volume", USB_MIXER_S16
},
644 { "Tone Control - Bass", USB_MIXER_S8
},
645 { "Tone Control - Mid", USB_MIXER_S8
},
646 { "Tone Control - Treble", USB_MIXER_S8
},
647 { "Graphic Equalizer", USB_MIXER_S8
}, /* FIXME: not implemeted yet */
648 { "Auto Gain Control", USB_MIXER_BOOLEAN
},
649 { "Delay Control", USB_MIXER_U16
},
650 { "Bass Boost", USB_MIXER_BOOLEAN
},
651 { "Loudness", USB_MIXER_BOOLEAN
},
655 /* private_free callback */
656 static void usb_mixer_elem_free(struct snd_kcontrol
*kctl
)
658 kfree(kctl
->private_data
);
659 kctl
->private_data
= NULL
;
664 * interface to ALSA control for feature/mixer units
668 * retrieve the minimum and maximum values for the specified control
670 static int get_min_max(struct usb_mixer_elem_info
*cval
, int default_min
)
673 cval
->min
= default_min
;
674 cval
->max
= cval
->min
+ 1;
677 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
678 cval
->val_type
== USB_MIXER_INV_BOOLEAN
) {
679 cval
->initialized
= 1;
684 for (i
= 0; i
< MAX_CHANNELS
; i
++)
685 if (cval
->cmask
& (1 << i
)) {
690 if (get_ctl_value(cval
, GET_MAX
, (cval
->control
<< 8) | minchn
, &cval
->max
) < 0 ||
691 get_ctl_value(cval
, GET_MIN
, (cval
->control
<< 8) | minchn
, &cval
->min
) < 0) {
692 snd_printd(KERN_ERR
"%d:%d: cannot get min/max values for control %d (id %d)\n",
693 cval
->id
, cval
->mixer
->ctrlif
, cval
->control
, cval
->id
);
696 if (get_ctl_value(cval
, GET_RES
, (cval
->control
<< 8) | minchn
, &cval
->res
) < 0) {
699 int last_valid_res
= cval
->res
;
701 while (cval
->res
> 1) {
702 if (set_ctl_value(cval
, SET_RES
, (cval
->control
<< 8) | minchn
, cval
->res
/ 2) < 0)
706 if (get_ctl_value(cval
, GET_RES
, (cval
->control
<< 8) | minchn
, &cval
->res
) < 0)
707 cval
->res
= last_valid_res
;
712 /* Additional checks for the proper resolution
714 * Some devices report smaller resolutions than actually
715 * reacting. They don't return errors but simply clip
716 * to the lower aligned value.
718 if (cval
->min
+ cval
->res
< cval
->max
) {
719 int last_valid_res
= cval
->res
;
720 int saved
, test
, check
;
721 get_cur_mix_value(cval
, minchn
, &saved
);
724 if (test
< cval
->max
)
728 if (test
< cval
->min
|| test
> cval
->max
||
729 set_cur_mix_value(cval
, minchn
, test
) ||
730 get_cur_mix_value(cval
, minchn
, &check
)) {
731 cval
->res
= last_valid_res
;
738 set_cur_mix_value(cval
, minchn
, saved
);
741 cval
->initialized
= 1;
747 /* get a feature/mixer unit info */
748 static int mixer_ctl_feature_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
750 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
752 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
753 cval
->val_type
== USB_MIXER_INV_BOOLEAN
)
754 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
756 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
757 uinfo
->count
= cval
->channels
;
758 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
759 cval
->val_type
== USB_MIXER_INV_BOOLEAN
) {
760 uinfo
->value
.integer
.min
= 0;
761 uinfo
->value
.integer
.max
= 1;
763 if (! cval
->initialized
)
764 get_min_max(cval
, 0);
765 uinfo
->value
.integer
.min
= 0;
766 uinfo
->value
.integer
.max
=
767 (cval
->max
- cval
->min
+ cval
->res
- 1) / cval
->res
;
772 /* get the current value from feature/mixer unit */
773 static int mixer_ctl_feature_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
775 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
776 int c
, cnt
, val
, err
;
780 for (c
= 0; c
< MAX_CHANNELS
; c
++) {
781 if (cval
->cmask
& (1 << c
)) {
782 err
= get_cur_mix_value(cval
, c
+ 1, &val
);
784 if (cval
->mixer
->ignore_ctl_error
) {
785 ucontrol
->value
.integer
.value
[0] = cval
->min
;
788 snd_printd(KERN_ERR
"cannot get current value for control %d ch %d: err = %d\n", cval
->control
, c
+ 1, err
);
791 val
= get_relative_value(cval
, val
);
792 ucontrol
->value
.integer
.value
[cnt
] = val
;
798 err
= get_cur_mix_value(cval
, 0, &val
);
800 if (cval
->mixer
->ignore_ctl_error
) {
801 ucontrol
->value
.integer
.value
[0] = cval
->min
;
804 snd_printd(KERN_ERR
"cannot get current value for control %d master ch: err = %d\n", cval
->control
, err
);
807 val
= get_relative_value(cval
, val
);
808 ucontrol
->value
.integer
.value
[0] = val
;
813 /* put the current value to feature/mixer unit */
814 static int mixer_ctl_feature_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
816 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
817 int c
, cnt
, val
, oval
, err
;
822 for (c
= 0; c
< MAX_CHANNELS
; c
++) {
823 if (cval
->cmask
& (1 << c
)) {
824 err
= get_cur_mix_value(cval
, c
+ 1, &oval
);
826 if (cval
->mixer
->ignore_ctl_error
)
830 val
= ucontrol
->value
.integer
.value
[cnt
];
831 val
= get_abs_value(cval
, val
);
833 set_cur_mix_value(cval
, c
+ 1, val
);
836 get_cur_mix_value(cval
, c
+ 1, &val
);
842 err
= get_cur_mix_value(cval
, 0, &oval
);
843 if (err
< 0 && cval
->mixer
->ignore_ctl_error
)
847 val
= ucontrol
->value
.integer
.value
[0];
848 val
= get_abs_value(cval
, val
);
850 set_cur_mix_value(cval
, 0, val
);
857 static struct snd_kcontrol_new usb_feature_unit_ctl
= {
858 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
859 .name
= "", /* will be filled later manually */
860 .info
= mixer_ctl_feature_info
,
861 .get
= mixer_ctl_feature_get
,
862 .put
= mixer_ctl_feature_put
,
867 * build a feature control
870 static void build_feature_ctl(struct mixer_build
*state
, unsigned char *desc
,
871 unsigned int ctl_mask
, int control
,
872 struct usb_audio_term
*iterm
, int unitid
)
874 unsigned int len
= 0;
876 int nameid
= desc
[desc
[0] - 1];
877 struct snd_kcontrol
*kctl
;
878 struct usb_mixer_elem_info
*cval
;
880 control
++; /* change from zero-based to 1-based value */
882 if (control
== USB_FEATURE_GEQ
) {
883 /* FIXME: not supported yet */
887 if (check_ignored_ctl(state
, unitid
, control
))
890 cval
= kzalloc(sizeof(*cval
), GFP_KERNEL
);
892 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
895 cval
->mixer
= state
->mixer
;
897 cval
->control
= control
;
898 cval
->cmask
= ctl_mask
;
899 cval
->val_type
= audio_feature_info
[control
-1].type
;
901 cval
->channels
= 1; /* master channel */
904 for (i
= 0; i
< 16; i
++)
905 if (ctl_mask
& (1 << i
))
910 /* get min/max values */
911 get_min_max(cval
, 0);
913 kctl
= snd_ctl_new1(&usb_feature_unit_ctl
, cval
);
915 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
919 kctl
->private_free
= usb_mixer_elem_free
;
921 len
= check_mapped_name(state
, unitid
, control
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
922 mapped_name
= len
!= 0;
924 len
= snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
927 case USB_FEATURE_MUTE
:
928 case USB_FEATURE_VOLUME
:
929 /* determine the control name. the rule is:
930 * - if a name id is given in descriptor, use it.
931 * - if the connected input can be determined, then use the name
933 * - if the connected output can be determined, use it.
934 * - otherwise, anonymous name.
937 len
= get_term_name(state
, iterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 1);
939 len
= get_term_name(state
, &state
->oterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 1);
941 len
= snprintf(kctl
->id
.name
, sizeof(kctl
->id
.name
),
942 "Feature %d", unitid
);
944 /* determine the stream direction:
945 * if the connected output is USB stream, then it's likely a
946 * capture stream. otherwise it should be playback (hopefully :)
948 if (! mapped_name
&& ! (state
->oterm
.type
>> 16)) {
949 if ((state
->oterm
.type
& 0xff00) == 0x0100) {
950 len
= strlcat(kctl
->id
.name
, " Capture", sizeof(kctl
->id
.name
));
952 len
= strlcat(kctl
->id
.name
+ len
, " Playback", sizeof(kctl
->id
.name
));
955 strlcat(kctl
->id
.name
+ len
, control
== USB_FEATURE_MUTE
? " Switch" : " Volume",
956 sizeof(kctl
->id
.name
));
957 if (control
== USB_FEATURE_VOLUME
) {
958 kctl
->tlv
.c
= mixer_vol_tlv
;
959 kctl
->vd
[0].access
|=
960 SNDRV_CTL_ELEM_ACCESS_TLV_READ
|
961 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
;
967 strlcpy(kctl
->id
.name
, audio_feature_info
[control
-1].name
,
968 sizeof(kctl
->id
.name
));
972 /* quirk for UDA1321/N101 */
973 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
974 /* is not very clear from datasheets */
975 /* I hope that the min value is -15360 for newer firmware --jk */
976 switch (state
->chip
->usb_id
) {
977 case USB_ID(0x0471, 0x0101):
978 case USB_ID(0x0471, 0x0104):
979 case USB_ID(0x0471, 0x0105):
980 case USB_ID(0x0672, 0x1041):
981 if (!strcmp(kctl
->id
.name
, "PCM Playback Volume") &&
982 cval
->min
== -15616) {
983 snd_printk(KERN_INFO
"using volume control quirk for the UDA1321/N101 chip\n");
988 snd_printdd(KERN_INFO
"[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
989 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
, cval
->res
);
990 add_control_to_empty(state
, kctl
);
996 * parse a feature unit
998 * most of controlls are defined here.
1000 static int parse_audio_feature_unit(struct mixer_build
*state
, int unitid
, unsigned char *ftr
)
1003 struct usb_audio_term iterm
;
1004 unsigned int master_bits
, first_ch_bits
;
1007 if (ftr
[0] < 7 || ! (csize
= ftr
[5]) || ftr
[0] < 7 + csize
) {
1008 snd_printk(KERN_ERR
"usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid
);
1012 /* parse the source unit */
1013 if ((err
= parse_audio_unit(state
, ftr
[4])) < 0)
1016 /* determine the input source type and name */
1017 if (check_input_term(state
, ftr
[4], &iterm
) < 0)
1020 channels
= (ftr
[0] - 7) / csize
- 1;
1022 master_bits
= snd_usb_combine_bytes(ftr
+ 6, csize
);
1024 first_ch_bits
= snd_usb_combine_bytes(ftr
+ 6 + csize
, csize
);
1027 /* check all control types */
1028 for (i
= 0; i
< 10; i
++) {
1029 unsigned int ch_bits
= 0;
1030 for (j
= 0; j
< channels
; j
++) {
1031 unsigned int mask
= snd_usb_combine_bytes(ftr
+ 6 + csize
* (j
+1), csize
);
1032 if (mask
& (1 << i
))
1033 ch_bits
|= (1 << j
);
1035 if (ch_bits
& 1) /* the first channel must be set (for ease of programming) */
1036 build_feature_ctl(state
, ftr
, ch_bits
, i
, &iterm
, unitid
);
1037 if (master_bits
& (1 << i
))
1038 build_feature_ctl(state
, ftr
, 0, i
, &iterm
, unitid
);
1050 * build a mixer unit control
1052 * the callbacks are identical with feature unit.
1053 * input channel number (zero based) is given in control field instead.
1056 static void build_mixer_unit_ctl(struct mixer_build
*state
, unsigned char *desc
,
1057 int in_pin
, int in_ch
, int unitid
,
1058 struct usb_audio_term
*iterm
)
1060 struct usb_mixer_elem_info
*cval
;
1061 unsigned int input_pins
= desc
[4];
1062 unsigned int num_outs
= desc
[5 + input_pins
];
1063 unsigned int i
, len
;
1064 struct snd_kcontrol
*kctl
;
1066 if (check_ignored_ctl(state
, unitid
, 0))
1069 cval
= kzalloc(sizeof(*cval
), GFP_KERNEL
);
1073 cval
->mixer
= state
->mixer
;
1075 cval
->control
= in_ch
+ 1; /* based on 1 */
1076 cval
->val_type
= USB_MIXER_S16
;
1077 for (i
= 0; i
< num_outs
; i
++) {
1078 if (check_matrix_bitmap(desc
+ 9 + input_pins
, in_ch
, i
, num_outs
)) {
1079 cval
->cmask
|= (1 << i
);
1084 /* get min/max values */
1085 get_min_max(cval
, 0);
1087 kctl
= snd_ctl_new1(&usb_feature_unit_ctl
, cval
);
1089 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1093 kctl
->private_free
= usb_mixer_elem_free
;
1095 len
= check_mapped_name(state
, unitid
, 0, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1097 len
= get_term_name(state
, iterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 0);
1099 len
= sprintf(kctl
->id
.name
, "Mixer Source %d", in_ch
+ 1);
1100 strlcat(kctl
->id
.name
+ len
, " Volume", sizeof(kctl
->id
.name
));
1102 snd_printdd(KERN_INFO
"[%d] MU [%s] ch = %d, val = %d/%d\n",
1103 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
);
1104 add_control_to_empty(state
, kctl
);
1109 * parse a mixer unit
1111 static int parse_audio_mixer_unit(struct mixer_build
*state
, int unitid
, unsigned char *desc
)
1113 struct usb_audio_term iterm
;
1114 int input_pins
, num_ins
, num_outs
;
1117 if (desc
[0] < 11 || ! (input_pins
= desc
[4]) || ! (num_outs
= desc
[5 + input_pins
])) {
1118 snd_printk(KERN_ERR
"invalid MIXER UNIT descriptor %d\n", unitid
);
1121 /* no bmControls field (e.g. Maya44) -> ignore */
1122 if (desc
[0] <= 10 + input_pins
) {
1123 snd_printdd(KERN_INFO
"MU %d has no bmControls field\n", unitid
);
1129 for (pin
= 0; pin
< input_pins
; pin
++) {
1130 err
= parse_audio_unit(state
, desc
[5 + pin
]);
1133 err
= check_input_term(state
, desc
[5 + pin
], &iterm
);
1136 num_ins
+= iterm
.channels
;
1137 for (; ich
< num_ins
; ++ich
) {
1138 int och
, ich_has_controls
= 0;
1140 for (och
= 0; och
< num_outs
; ++och
) {
1141 if (check_matrix_bitmap(desc
+ 9 + input_pins
,
1142 ich
, och
, num_outs
)) {
1143 ich_has_controls
= 1;
1147 if (ich_has_controls
)
1148 build_mixer_unit_ctl(state
, desc
, pin
, ich
,
1157 * Processing Unit / Extension Unit
1160 /* get callback for processing/extension unit */
1161 static int mixer_ctl_procunit_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1163 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1166 err
= get_cur_ctl_value(cval
, cval
->control
<< 8, &val
);
1167 if (err
< 0 && cval
->mixer
->ignore_ctl_error
) {
1168 ucontrol
->value
.integer
.value
[0] = cval
->min
;
1173 val
= get_relative_value(cval
, val
);
1174 ucontrol
->value
.integer
.value
[0] = val
;
1178 /* put callback for processing/extension unit */
1179 static int mixer_ctl_procunit_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1181 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1184 err
= get_cur_ctl_value(cval
, cval
->control
<< 8, &oval
);
1186 if (cval
->mixer
->ignore_ctl_error
)
1190 val
= ucontrol
->value
.integer
.value
[0];
1191 val
= get_abs_value(cval
, val
);
1193 set_cur_ctl_value(cval
, cval
->control
<< 8, val
);
1199 /* alsa control interface for processing/extension unit */
1200 static struct snd_kcontrol_new mixer_procunit_ctl
= {
1201 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1202 .name
= "", /* will be filled later */
1203 .info
= mixer_ctl_feature_info
,
1204 .get
= mixer_ctl_procunit_get
,
1205 .put
= mixer_ctl_procunit_put
,
1210 * predefined data for processing units
1212 struct procunit_value_info
{
1219 struct procunit_info
{
1222 struct procunit_value_info
*values
;
1225 static struct procunit_value_info updown_proc_info
[] = {
1226 { USB_PROC_UPDOWN_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1227 { USB_PROC_UPDOWN_MODE_SEL
, "Mode Select", USB_MIXER_U8
, 1 },
1230 static struct procunit_value_info prologic_proc_info
[] = {
1231 { USB_PROC_PROLOGIC_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1232 { USB_PROC_PROLOGIC_MODE_SEL
, "Mode Select", USB_MIXER_U8
, 1 },
1235 static struct procunit_value_info threed_enh_proc_info
[] = {
1236 { USB_PROC_3DENH_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1237 { USB_PROC_3DENH_SPACE
, "Spaciousness", USB_MIXER_U8
},
1240 static struct procunit_value_info reverb_proc_info
[] = {
1241 { USB_PROC_REVERB_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1242 { USB_PROC_REVERB_LEVEL
, "Level", USB_MIXER_U8
},
1243 { USB_PROC_REVERB_TIME
, "Time", USB_MIXER_U16
},
1244 { USB_PROC_REVERB_DELAY
, "Delay", USB_MIXER_U8
},
1247 static struct procunit_value_info chorus_proc_info
[] = {
1248 { USB_PROC_CHORUS_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1249 { USB_PROC_CHORUS_LEVEL
, "Level", USB_MIXER_U8
},
1250 { USB_PROC_CHORUS_RATE
, "Rate", USB_MIXER_U16
},
1251 { USB_PROC_CHORUS_DEPTH
, "Depth", USB_MIXER_U16
},
1254 static struct procunit_value_info dcr_proc_info
[] = {
1255 { USB_PROC_DCR_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1256 { USB_PROC_DCR_RATIO
, "Ratio", USB_MIXER_U16
},
1257 { USB_PROC_DCR_MAX_AMP
, "Max Amp", USB_MIXER_S16
},
1258 { USB_PROC_DCR_THRESHOLD
, "Threshold", USB_MIXER_S16
},
1259 { USB_PROC_DCR_ATTACK
, "Attack Time", USB_MIXER_U16
},
1260 { USB_PROC_DCR_RELEASE
, "Release Time", USB_MIXER_U16
},
1264 static struct procunit_info procunits
[] = {
1265 { USB_PROC_UPDOWN
, "Up Down", updown_proc_info
},
1266 { USB_PROC_PROLOGIC
, "Dolby Prologic", prologic_proc_info
},
1267 { USB_PROC_3DENH
, "3D Stereo Extender", threed_enh_proc_info
},
1268 { USB_PROC_REVERB
, "Reverb", reverb_proc_info
},
1269 { USB_PROC_CHORUS
, "Chorus", chorus_proc_info
},
1270 { USB_PROC_DCR
, "DCR", dcr_proc_info
},
1275 * build a processing/extension unit
1277 static int build_audio_procunit(struct mixer_build
*state
, int unitid
, unsigned char *dsc
, struct procunit_info
*list
, char *name
)
1279 int num_ins
= dsc
[6];
1280 struct usb_mixer_elem_info
*cval
;
1281 struct snd_kcontrol
*kctl
;
1282 int i
, err
, nameid
, type
, len
;
1283 struct procunit_info
*info
;
1284 struct procunit_value_info
*valinfo
;
1285 static struct procunit_value_info default_value_info
[] = {
1286 { 0x01, "Switch", USB_MIXER_BOOLEAN
},
1289 static struct procunit_info default_info
= {
1290 0, NULL
, default_value_info
1293 if (dsc
[0] < 13 || dsc
[0] < 13 + num_ins
|| dsc
[0] < num_ins
+ dsc
[11 + num_ins
]) {
1294 snd_printk(KERN_ERR
"invalid %s descriptor (id %d)\n", name
, unitid
);
1298 for (i
= 0; i
< num_ins
; i
++) {
1299 if ((err
= parse_audio_unit(state
, dsc
[7 + i
])) < 0)
1303 type
= combine_word(&dsc
[4]);
1304 for (info
= list
; info
&& info
->type
; info
++)
1305 if (info
->type
== type
)
1307 if (! info
|| ! info
->type
)
1308 info
= &default_info
;
1310 for (valinfo
= info
->values
; valinfo
->control
; valinfo
++) {
1311 /* FIXME: bitmap might be longer than 8bit */
1312 if (! (dsc
[12 + num_ins
] & (1 << (valinfo
->control
- 1))))
1314 if (check_ignored_ctl(state
, unitid
, valinfo
->control
))
1316 cval
= kzalloc(sizeof(*cval
), GFP_KERNEL
);
1318 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1321 cval
->mixer
= state
->mixer
;
1323 cval
->control
= valinfo
->control
;
1324 cval
->val_type
= valinfo
->val_type
;
1327 /* get min/max values */
1328 if (type
== USB_PROC_UPDOWN
&& cval
->control
== USB_PROC_UPDOWN_MODE_SEL
) {
1329 /* FIXME: hard-coded */
1331 cval
->max
= dsc
[15];
1333 cval
->initialized
= 1;
1335 get_min_max(cval
, valinfo
->min_value
);
1337 kctl
= snd_ctl_new1(&mixer_procunit_ctl
, cval
);
1339 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1343 kctl
->private_free
= usb_mixer_elem_free
;
1345 if (check_mapped_name(state
, unitid
, cval
->control
, kctl
->id
.name
, sizeof(kctl
->id
.name
)))
1347 else if (info
->name
)
1348 strlcpy(kctl
->id
.name
, info
->name
, sizeof(kctl
->id
.name
));
1350 nameid
= dsc
[12 + num_ins
+ dsc
[11 + num_ins
]];
1353 len
= snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1355 strlcpy(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
));
1357 strlcat(kctl
->id
.name
, " ", sizeof(kctl
->id
.name
));
1358 strlcat(kctl
->id
.name
, valinfo
->suffix
, sizeof(kctl
->id
.name
));
1360 snd_printdd(KERN_INFO
"[%d] PU [%s] ch = %d, val = %d/%d\n",
1361 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
);
1362 if ((err
= add_control_to_empty(state
, kctl
)) < 0)
1369 static int parse_audio_processing_unit(struct mixer_build
*state
, int unitid
, unsigned char *desc
)
1371 return build_audio_procunit(state
, unitid
, desc
, procunits
, "Processing Unit");
1374 static int parse_audio_extension_unit(struct mixer_build
*state
, int unitid
, unsigned char *desc
)
1376 return build_audio_procunit(state
, unitid
, desc
, NULL
, "Extension Unit");
1384 /* info callback for selector unit
1385 * use an enumerator type for routing
1387 static int mixer_ctl_selector_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
1389 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1390 char **itemlist
= (char **)kcontrol
->private_value
;
1392 snd_assert(itemlist
, return -EINVAL
);
1393 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1395 uinfo
->value
.enumerated
.items
= cval
->max
;
1396 if ((int)uinfo
->value
.enumerated
.item
>= cval
->max
)
1397 uinfo
->value
.enumerated
.item
= cval
->max
- 1;
1398 strcpy(uinfo
->value
.enumerated
.name
, itemlist
[uinfo
->value
.enumerated
.item
]);
1402 /* get callback for selector unit */
1403 static int mixer_ctl_selector_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1405 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1408 err
= get_cur_ctl_value(cval
, 0, &val
);
1410 if (cval
->mixer
->ignore_ctl_error
) {
1411 ucontrol
->value
.enumerated
.item
[0] = 0;
1416 val
= get_relative_value(cval
, val
);
1417 ucontrol
->value
.enumerated
.item
[0] = val
;
1421 /* put callback for selector unit */
1422 static int mixer_ctl_selector_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1424 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1427 err
= get_cur_ctl_value(cval
, 0, &oval
);
1429 if (cval
->mixer
->ignore_ctl_error
)
1433 val
= ucontrol
->value
.enumerated
.item
[0];
1434 val
= get_abs_value(cval
, val
);
1436 set_cur_ctl_value(cval
, 0, val
);
1442 /* alsa control interface for selector unit */
1443 static struct snd_kcontrol_new mixer_selectunit_ctl
= {
1444 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1445 .name
= "", /* will be filled later */
1446 .info
= mixer_ctl_selector_info
,
1447 .get
= mixer_ctl_selector_get
,
1448 .put
= mixer_ctl_selector_put
,
1452 /* private free callback.
1453 * free both private_data and private_value
1455 static void usb_mixer_selector_elem_free(struct snd_kcontrol
*kctl
)
1459 if (kctl
->private_data
) {
1460 struct usb_mixer_elem_info
*cval
= kctl
->private_data
;
1461 num_ins
= cval
->max
;
1463 kctl
->private_data
= NULL
;
1465 if (kctl
->private_value
) {
1466 char **itemlist
= (char **)kctl
->private_value
;
1467 for (i
= 0; i
< num_ins
; i
++)
1470 kctl
->private_value
= 0;
1475 * parse a selector unit
1477 static int parse_audio_selector_unit(struct mixer_build
*state
, int unitid
, unsigned char *desc
)
1479 unsigned int num_ins
= desc
[4];
1480 unsigned int i
, nameid
, len
;
1482 struct usb_mixer_elem_info
*cval
;
1483 struct snd_kcontrol
*kctl
;
1486 if (! num_ins
|| desc
[0] < 5 + num_ins
) {
1487 snd_printk(KERN_ERR
"invalid SELECTOR UNIT descriptor %d\n", unitid
);
1491 for (i
= 0; i
< num_ins
; i
++) {
1492 if ((err
= parse_audio_unit(state
, desc
[5 + i
])) < 0)
1496 if (num_ins
== 1) /* only one ? nonsense! */
1499 if (check_ignored_ctl(state
, unitid
, 0))
1502 cval
= kzalloc(sizeof(*cval
), GFP_KERNEL
);
1504 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1507 cval
->mixer
= state
->mixer
;
1509 cval
->val_type
= USB_MIXER_U8
;
1512 cval
->max
= num_ins
;
1514 cval
->initialized
= 1;
1516 namelist
= kmalloc(sizeof(char *) * num_ins
, GFP_KERNEL
);
1518 snd_printk(KERN_ERR
"cannot malloc\n");
1522 #define MAX_ITEM_NAME_LEN 64
1523 for (i
= 0; i
< num_ins
; i
++) {
1524 struct usb_audio_term iterm
;
1526 namelist
[i
] = kmalloc(MAX_ITEM_NAME_LEN
, GFP_KERNEL
);
1527 if (! namelist
[i
]) {
1528 snd_printk(KERN_ERR
"cannot malloc\n");
1535 len
= check_mapped_selector_name(state
, unitid
, i
, namelist
[i
],
1537 if (! len
&& check_input_term(state
, desc
[5 + i
], &iterm
) >= 0)
1538 len
= get_term_name(state
, &iterm
, namelist
[i
], MAX_ITEM_NAME_LEN
, 0);
1540 sprintf(namelist
[i
], "Input %d", i
);
1543 kctl
= snd_ctl_new1(&mixer_selectunit_ctl
, cval
);
1545 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1550 kctl
->private_value
= (unsigned long)namelist
;
1551 kctl
->private_free
= usb_mixer_selector_elem_free
;
1553 nameid
= desc
[desc
[0] - 1];
1554 len
= check_mapped_name(state
, unitid
, 0, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1558 snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1560 len
= get_term_name(state
, &state
->oterm
,
1561 kctl
->id
.name
, sizeof(kctl
->id
.name
), 0);
1563 strlcpy(kctl
->id
.name
, "USB", sizeof(kctl
->id
.name
));
1565 if ((state
->oterm
.type
& 0xff00) == 0x0100)
1566 strlcat(kctl
->id
.name
, " Capture Source", sizeof(kctl
->id
.name
));
1568 strlcat(kctl
->id
.name
, " Playback Source", sizeof(kctl
->id
.name
));
1571 snd_printdd(KERN_INFO
"[%d] SU [%s] items = %d\n",
1572 cval
->id
, kctl
->id
.name
, num_ins
);
1573 if ((err
= add_control_to_empty(state
, kctl
)) < 0)
1581 * parse an audio unit recursively
1584 static int parse_audio_unit(struct mixer_build
*state
, int unitid
)
1588 if (test_and_set_bit(unitid
, state
->unitbitmap
))
1589 return 0; /* the unit already visited */
1591 p1
= find_audio_control_unit(state
, unitid
);
1593 snd_printk(KERN_ERR
"usbaudio: unit %d not found!\n", unitid
);
1598 case INPUT_TERMINAL
:
1601 return parse_audio_mixer_unit(state
, unitid
, p1
);
1603 return parse_audio_selector_unit(state
, unitid
, p1
);
1605 return parse_audio_feature_unit(state
, unitid
, p1
);
1606 case PROCESSING_UNIT
:
1607 return parse_audio_processing_unit(state
, unitid
, p1
);
1608 case EXTENSION_UNIT
:
1609 return parse_audio_extension_unit(state
, unitid
, p1
);
1611 snd_printk(KERN_ERR
"usbaudio: unit %u: unexpected type 0x%02x\n", unitid
, p1
[2]);
1616 static void snd_usb_mixer_free(struct usb_mixer_interface
*mixer
)
1618 kfree(mixer
->id_elems
);
1620 kfree(mixer
->urb
->transfer_buffer
);
1621 usb_free_urb(mixer
->urb
);
1623 usb_free_urb(mixer
->rc_urb
);
1624 kfree(mixer
->rc_setup_packet
);
1628 static int snd_usb_mixer_dev_free(struct snd_device
*device
)
1630 struct usb_mixer_interface
*mixer
= device
->device_data
;
1631 snd_usb_mixer_free(mixer
);
1636 * create mixer controls
1638 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1640 static int snd_usb_mixer_controls(struct usb_mixer_interface
*mixer
)
1642 unsigned char *desc
;
1643 struct mixer_build state
;
1645 const struct usbmix_ctl_map
*map
;
1646 struct usb_host_interface
*hostif
;
1648 hostif
= &usb_ifnum_to_if(mixer
->chip
->dev
, mixer
->ctrlif
)->altsetting
[0];
1649 memset(&state
, 0, sizeof(state
));
1650 state
.chip
= mixer
->chip
;
1651 state
.mixer
= mixer
;
1652 state
.buffer
= hostif
->extra
;
1653 state
.buflen
= hostif
->extralen
;
1655 /* check the mapping table */
1656 for (map
= usbmix_ctl_maps
; map
->id
; map
++) {
1657 if (map
->id
== state
.chip
->usb_id
) {
1658 state
.map
= map
->map
;
1659 state
.selector_map
= map
->selector_map
;
1660 mixer
->ignore_ctl_error
= map
->ignore_ctl_error
;
1666 while ((desc
= snd_usb_find_csint_desc(hostif
->extra
, hostif
->extralen
, desc
, OUTPUT_TERMINAL
)) != NULL
) {
1668 continue; /* invalid descriptor? */
1669 set_bit(desc
[3], state
.unitbitmap
); /* mark terminal ID as visited */
1670 state
.oterm
.id
= desc
[3];
1671 state
.oterm
.type
= combine_word(&desc
[4]);
1672 state
.oterm
.name
= desc
[8];
1673 err
= parse_audio_unit(&state
, desc
[7]);
1680 static void snd_usb_mixer_notify_id(struct usb_mixer_interface
*mixer
,
1683 struct usb_mixer_elem_info
*info
;
1685 for (info
= mixer
->id_elems
[unitid
]; info
; info
= info
->next_id_elem
)
1686 snd_ctl_notify(mixer
->chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
1690 static void snd_usb_mixer_memory_change(struct usb_mixer_interface
*mixer
,
1695 /* unit ids specific to Extigy/Audigy 2 NX: */
1697 case 0: /* remote control */
1698 mixer
->rc_urb
->dev
= mixer
->chip
->dev
;
1699 usb_submit_urb(mixer
->rc_urb
, GFP_ATOMIC
);
1701 case 4: /* digital in jack */
1702 case 7: /* line in jacks */
1703 case 19: /* speaker out jacks */
1704 case 20: /* headphones out jack */
1707 snd_printd(KERN_DEBUG
"memory change in unknown unit %d\n", unitid
);
1712 static void snd_usb_mixer_status_complete(struct urb
*urb
)
1714 struct usb_mixer_interface
*mixer
= urb
->context
;
1716 if (urb
->status
== 0) {
1717 u8
*buf
= urb
->transfer_buffer
;
1720 for (i
= urb
->actual_length
; i
>= 2; buf
+= 2, i
-= 2) {
1721 snd_printd(KERN_DEBUG
"status interrupt: %02x %02x\n",
1723 /* ignore any notifications not from the control interface */
1724 if ((buf
[0] & 0x0f) != 0)
1726 if (!(buf
[0] & 0x40))
1727 snd_usb_mixer_notify_id(mixer
, buf
[1]);
1729 snd_usb_mixer_memory_change(mixer
, buf
[1]);
1732 if (urb
->status
!= -ENOENT
&& urb
->status
!= -ECONNRESET
) {
1733 urb
->dev
= mixer
->chip
->dev
;
1734 usb_submit_urb(urb
, GFP_ATOMIC
);
1738 /* create the handler for the optional status interrupt endpoint */
1739 static int snd_usb_mixer_status_create(struct usb_mixer_interface
*mixer
)
1741 struct usb_host_interface
*hostif
;
1742 struct usb_endpoint_descriptor
*ep
;
1743 void *transfer_buffer
;
1747 hostif
= &usb_ifnum_to_if(mixer
->chip
->dev
, mixer
->ctrlif
)->altsetting
[0];
1748 /* we need one interrupt input endpoint */
1749 if (get_iface_desc(hostif
)->bNumEndpoints
< 1)
1751 ep
= get_endpoint(hostif
, 0);
1752 if ((ep
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_IN
||
1753 (ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
1756 epnum
= ep
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1757 buffer_length
= le16_to_cpu(ep
->wMaxPacketSize
);
1758 transfer_buffer
= kmalloc(buffer_length
, GFP_KERNEL
);
1759 if (!transfer_buffer
)
1761 mixer
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
1763 kfree(transfer_buffer
);
1766 usb_fill_int_urb(mixer
->urb
, mixer
->chip
->dev
,
1767 usb_rcvintpipe(mixer
->chip
->dev
, epnum
),
1768 transfer_buffer
, buffer_length
,
1769 snd_usb_mixer_status_complete
, mixer
, ep
->bInterval
);
1770 usb_submit_urb(mixer
->urb
, GFP_KERNEL
);
1774 static void snd_usb_soundblaster_remote_complete(struct urb
*urb
)
1776 struct usb_mixer_interface
*mixer
= urb
->context
;
1777 const struct rc_config
*rc
= mixer
->rc_cfg
;
1780 if (urb
->status
< 0 || urb
->actual_length
< rc
->packet_length
)
1783 code
= mixer
->rc_buffer
[rc
->offset
];
1784 if (rc
->length
== 2)
1785 code
|= mixer
->rc_buffer
[rc
->offset
+ 1] << 8;
1787 /* the Mute button actually changes the mixer control */
1788 if (code
== rc
->mute_code
)
1789 snd_usb_mixer_notify_id(mixer
, rc
->mute_mixer_id
);
1790 mixer
->rc_code
= code
;
1792 wake_up(&mixer
->rc_waitq
);
1795 static int snd_usb_sbrc_hwdep_open(struct snd_hwdep
*hw
, struct file
*file
)
1797 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1799 if (test_and_set_bit(0, &mixer
->rc_hwdep_open
))
1804 static int snd_usb_sbrc_hwdep_release(struct snd_hwdep
*hw
, struct file
*file
)
1806 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1808 clear_bit(0, &mixer
->rc_hwdep_open
);
1809 smp_mb__after_clear_bit();
1813 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep
*hw
, char __user
*buf
,
1814 long count
, loff_t
*offset
)
1816 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1820 if (count
!= 1 && count
!= 4)
1822 err
= wait_event_interruptible(mixer
->rc_waitq
,
1823 (rc_code
= xchg(&mixer
->rc_code
, 0)) != 0);
1826 err
= put_user(rc_code
, buf
);
1828 err
= put_user(rc_code
, (u32 __user
*)buf
);
1830 return err
< 0 ? err
: count
;
1833 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep
*hw
, struct file
*file
,
1836 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1838 poll_wait(file
, &mixer
->rc_waitq
, wait
);
1839 return mixer
->rc_code
? POLLIN
| POLLRDNORM
: 0;
1842 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface
*mixer
)
1844 struct snd_hwdep
*hwdep
;
1847 for (i
= 0; i
< ARRAY_SIZE(rc_configs
); ++i
)
1848 if (rc_configs
[i
].usb_id
== mixer
->chip
->usb_id
)
1850 if (i
>= ARRAY_SIZE(rc_configs
))
1852 mixer
->rc_cfg
= &rc_configs
[i
];
1854 len
= mixer
->rc_cfg
->packet_length
;
1856 init_waitqueue_head(&mixer
->rc_waitq
);
1857 err
= snd_hwdep_new(mixer
->chip
->card
, "SB remote control", 0, &hwdep
);
1860 snprintf(hwdep
->name
, sizeof(hwdep
->name
),
1861 "%s remote control", mixer
->chip
->card
->shortname
);
1862 hwdep
->iface
= SNDRV_HWDEP_IFACE_SB_RC
;
1863 hwdep
->private_data
= mixer
;
1864 hwdep
->ops
.read
= snd_usb_sbrc_hwdep_read
;
1865 hwdep
->ops
.open
= snd_usb_sbrc_hwdep_open
;
1866 hwdep
->ops
.release
= snd_usb_sbrc_hwdep_release
;
1867 hwdep
->ops
.poll
= snd_usb_sbrc_hwdep_poll
;
1869 mixer
->rc_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1872 mixer
->rc_setup_packet
= kmalloc(sizeof(*mixer
->rc_setup_packet
), GFP_KERNEL
);
1873 if (!mixer
->rc_setup_packet
) {
1874 usb_free_urb(mixer
->rc_urb
);
1875 mixer
->rc_urb
= NULL
;
1878 mixer
->rc_setup_packet
->bRequestType
=
1879 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
1880 mixer
->rc_setup_packet
->bRequest
= GET_MEM
;
1881 mixer
->rc_setup_packet
->wValue
= cpu_to_le16(0);
1882 mixer
->rc_setup_packet
->wIndex
= cpu_to_le16(0);
1883 mixer
->rc_setup_packet
->wLength
= cpu_to_le16(len
);
1884 usb_fill_control_urb(mixer
->rc_urb
, mixer
->chip
->dev
,
1885 usb_rcvctrlpipe(mixer
->chip
->dev
, 0),
1886 (u8
*)mixer
->rc_setup_packet
, mixer
->rc_buffer
, len
,
1887 snd_usb_soundblaster_remote_complete
, mixer
);
1891 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
1893 static int snd_audigy2nx_led_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1895 struct usb_mixer_interface
*mixer
= snd_kcontrol_chip(kcontrol
);
1896 int index
= kcontrol
->private_value
;
1898 ucontrol
->value
.integer
.value
[0] = mixer
->audigy2nx_leds
[index
];
1902 static int snd_audigy2nx_led_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1904 struct usb_mixer_interface
*mixer
= snd_kcontrol_chip(kcontrol
);
1905 int index
= kcontrol
->private_value
;
1906 int value
= ucontrol
->value
.integer
.value
[0];
1911 changed
= value
!= mixer
->audigy2nx_leds
[index
];
1912 err
= snd_usb_ctl_msg(mixer
->chip
->dev
,
1913 usb_sndctrlpipe(mixer
->chip
->dev
, 0), 0x24,
1914 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_OTHER
,
1915 value
, index
+ 2, NULL
, 0, 100);
1918 mixer
->audigy2nx_leds
[index
] = value
;
1922 static struct snd_kcontrol_new snd_audigy2nx_controls
[] = {
1924 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1925 .name
= "CMSS LED Switch",
1926 .info
= snd_audigy2nx_led_info
,
1927 .get
= snd_audigy2nx_led_get
,
1928 .put
= snd_audigy2nx_led_put
,
1932 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1933 .name
= "Power LED Switch",
1934 .info
= snd_audigy2nx_led_info
,
1935 .get
= snd_audigy2nx_led_get
,
1936 .put
= snd_audigy2nx_led_put
,
1940 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1941 .name
= "Dolby Digital LED Switch",
1942 .info
= snd_audigy2nx_led_info
,
1943 .get
= snd_audigy2nx_led_get
,
1944 .put
= snd_audigy2nx_led_put
,
1949 static int snd_audigy2nx_controls_create(struct usb_mixer_interface
*mixer
)
1953 for (i
= 0; i
< ARRAY_SIZE(snd_audigy2nx_controls
); ++i
) {
1954 err
= snd_ctl_add(mixer
->chip
->card
,
1955 snd_ctl_new1(&snd_audigy2nx_controls
[i
], mixer
));
1959 mixer
->audigy2nx_leds
[1] = 1; /* Power LED is on by default */
1963 static void snd_audigy2nx_proc_read(struct snd_info_entry
*entry
,
1964 struct snd_info_buffer
*buffer
)
1966 static const struct {
1975 struct usb_mixer_interface
*mixer
= entry
->private_data
;
1979 snd_iprintf(buffer
, "%s jacks\n\n", mixer
->chip
->card
->shortname
);
1980 for (i
= 0; i
< ARRAY_SIZE(jacks
); ++i
) {
1981 snd_iprintf(buffer
, "%s: ", jacks
[i
].name
);
1982 err
= snd_usb_ctl_msg(mixer
->chip
->dev
,
1983 usb_rcvctrlpipe(mixer
->chip
->dev
, 0),
1984 GET_MEM
, USB_DIR_IN
| USB_TYPE_CLASS
|
1985 USB_RECIP_INTERFACE
, 0,
1986 jacks
[i
].unitid
<< 8, buf
, 3, 100);
1987 if (err
== 3 && buf
[0] == 3)
1988 snd_iprintf(buffer
, "%02x %02x\n", buf
[1], buf
[2]);
1990 snd_iprintf(buffer
, "?\n");
1994 int snd_usb_create_mixer(struct snd_usb_audio
*chip
, int ctrlif
)
1996 static struct snd_device_ops dev_ops
= {
1997 .dev_free
= snd_usb_mixer_dev_free
1999 struct usb_mixer_interface
*mixer
;
2002 strcpy(chip
->card
->mixername
, "USB Mixer");
2004 mixer
= kzalloc(sizeof(*mixer
), GFP_KERNEL
);
2008 mixer
->ctrlif
= ctrlif
;
2009 #ifdef IGNORE_CTL_ERROR
2010 mixer
->ignore_ctl_error
= 1;
2012 mixer
->id_elems
= kcalloc(256, sizeof(*mixer
->id_elems
), GFP_KERNEL
);
2013 if (!mixer
->id_elems
) {
2018 if ((err
= snd_usb_mixer_controls(mixer
)) < 0 ||
2019 (err
= snd_usb_mixer_status_create(mixer
)) < 0)
2022 if ((err
= snd_usb_soundblaster_remote_init(mixer
)) < 0)
2025 if (mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3020)) {
2026 struct snd_info_entry
*entry
;
2028 if ((err
= snd_audigy2nx_controls_create(mixer
)) < 0)
2030 if (!snd_card_proc_new(chip
->card
, "audigy2nx", &entry
))
2031 snd_info_set_text_ops(entry
, mixer
,
2032 snd_audigy2nx_proc_read
);
2035 err
= snd_device_new(chip
->card
, SNDRV_DEV_LOWLEVEL
, mixer
, &dev_ops
);
2038 list_add(&mixer
->list
, &chip
->mixer_list
);
2042 snd_usb_mixer_free(mixer
);
2046 void snd_usb_mixer_disconnect(struct list_head
*p
)
2048 struct usb_mixer_interface
*mixer
;
2050 mixer
= list_entry(p
, struct usb_mixer_interface
, list
);
2051 usb_kill_urb(mixer
->urb
);
2052 usb_kill_urb(mixer
->rc_urb
);