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
;
83 wait_queue_head_t rc_waitq
;
85 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 */
128 int cache_val
[MAX_CHANNELS
];
134 USB_FEATURE_NONE
= 0,
135 USB_FEATURE_MUTE
= 1,
143 USB_FEATURE_BASSBOOST
,
149 USB_MIXER_INV_BOOLEAN
,
158 USB_PROC_UPDOWN_SWITCH
= 1,
159 USB_PROC_UPDOWN_MODE_SEL
= 2,
161 USB_PROC_PROLOGIC
= 2,
162 USB_PROC_PROLOGIC_SWITCH
= 1,
163 USB_PROC_PROLOGIC_MODE_SEL
= 2,
166 USB_PROC_3DENH_SWITCH
= 1,
167 USB_PROC_3DENH_SPACE
= 2,
170 USB_PROC_REVERB_SWITCH
= 1,
171 USB_PROC_REVERB_LEVEL
= 2,
172 USB_PROC_REVERB_TIME
= 3,
173 USB_PROC_REVERB_DELAY
= 4,
176 USB_PROC_CHORUS_SWITCH
= 1,
177 USB_PROC_CHORUS_LEVEL
= 2,
178 USB_PROC_CHORUS_RATE
= 3,
179 USB_PROC_CHORUS_DEPTH
= 4,
182 USB_PROC_DCR_SWITCH
= 1,
183 USB_PROC_DCR_RATIO
= 2,
184 USB_PROC_DCR_MAX_AMP
= 3,
185 USB_PROC_DCR_THRESHOLD
= 4,
186 USB_PROC_DCR_ATTACK
= 5,
187 USB_PROC_DCR_RELEASE
= 6,
192 * manual mapping of mixer names
193 * if the mixer topology is too complicated and the parsed names are
194 * ambiguous, add the entries in usbmixer_maps.c.
196 #include "usbmixer_maps.c"
198 static const struct usbmix_name_map
*
199 find_map(struct mixer_build
*state
, int unitid
, int control
)
201 const struct usbmix_name_map
*p
= state
->map
;
206 for (p
= state
->map
; p
->id
; p
++) {
207 if (p
->id
== unitid
&&
208 (!control
|| !p
->control
|| control
== p
->control
))
214 /* get the mapped name if the unit matches */
216 check_mapped_name(const struct usbmix_name_map
*p
, char *buf
, int buflen
)
222 return strlcpy(buf
, p
->name
, buflen
);
225 /* check whether the control should be ignored */
227 check_ignored_ctl(const struct usbmix_name_map
*p
)
229 if (!p
|| p
->name
|| p
->dB
)
235 static inline void check_mapped_dB(const struct usbmix_name_map
*p
,
236 struct usb_mixer_elem_info
*cval
)
239 cval
->dBmin
= p
->dB
->min
;
240 cval
->dBmax
= p
->dB
->max
;
244 /* get the mapped selector source name */
245 static int check_mapped_selector_name(struct mixer_build
*state
, int unitid
,
246 int index
, char *buf
, int buflen
)
248 const struct usbmix_selector_map
*p
;
250 if (! state
->selector_map
)
252 for (p
= state
->selector_map
; p
->id
; p
++) {
253 if (p
->id
== unitid
&& index
< p
->count
)
254 return strlcpy(buf
, p
->names
[index
], buflen
);
260 * find an audio control unit with the given unit id
262 static void *find_audio_control_unit(struct mixer_build
*state
, unsigned char unit
)
267 while ((p
= snd_usb_find_desc(state
->buffer
, state
->buflen
, p
,
268 USB_DT_CS_INTERFACE
)) != NULL
) {
269 if (p
[0] >= 4 && p
[2] >= INPUT_TERMINAL
&& p
[2] <= EXTENSION_UNIT
&& p
[3] == unit
)
277 * copy a string with the given id
279 static int snd_usb_copy_string_desc(struct mixer_build
*state
, int index
, char *buf
, int maxlen
)
281 int len
= usb_string(state
->chip
->dev
, index
, buf
, maxlen
- 1);
287 * convert from the byte/word on usb descriptor to the zero-based integer
289 static int convert_signed_value(struct usb_mixer_elem_info
*cval
, int val
)
291 switch (cval
->val_type
) {
292 case USB_MIXER_BOOLEAN
:
294 case USB_MIXER_INV_BOOLEAN
:
317 * convert from the zero-based int to the byte/word for usb descriptor
319 static int convert_bytes_value(struct usb_mixer_elem_info
*cval
, int val
)
321 switch (cval
->val_type
) {
322 case USB_MIXER_BOOLEAN
:
324 case USB_MIXER_INV_BOOLEAN
:
333 return 0; /* not reached */
336 static int get_relative_value(struct usb_mixer_elem_info
*cval
, int val
)
342 else if (val
>= cval
->max
)
343 return (cval
->max
- cval
->min
+ cval
->res
- 1) / cval
->res
;
345 return (val
- cval
->min
) / cval
->res
;
348 static int get_abs_value(struct usb_mixer_elem_info
*cval
, int val
)
363 * retrieve a mixer value
366 static int get_ctl_value(struct usb_mixer_elem_info
*cval
, int request
, int validx
, int *value_ret
)
368 unsigned char buf
[2];
369 int val_len
= cval
->val_type
>= USB_MIXER_S16
? 2 : 1;
372 while (timeout
-- > 0) {
373 if (snd_usb_ctl_msg(cval
->mixer
->chip
->dev
,
374 usb_rcvctrlpipe(cval
->mixer
->chip
->dev
, 0),
376 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
377 validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8),
378 buf
, val_len
, 100) >= val_len
) {
379 *value_ret
= convert_signed_value(cval
, snd_usb_combine_bytes(buf
, val_len
));
383 snd_printdd(KERN_ERR
"cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
384 request
, validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8), cval
->val_type
);
388 static int get_cur_ctl_value(struct usb_mixer_elem_info
*cval
, int validx
, int *value
)
390 return get_ctl_value(cval
, GET_CUR
, validx
, value
);
393 /* channel = 0: master, 1 = first channel */
394 static inline int get_cur_mix_raw(struct usb_mixer_elem_info
*cval
,
395 int channel
, int *value
)
397 return get_ctl_value(cval
, GET_CUR
, (cval
->control
<< 8) | channel
, value
);
400 static int get_cur_mix_value(struct usb_mixer_elem_info
*cval
,
401 int channel
, int index
, int *value
)
405 if (cval
->cached
& (1 << channel
)) {
406 *value
= cval
->cache_val
[index
];
409 err
= get_cur_mix_raw(cval
, channel
, value
);
411 if (!cval
->mixer
->ignore_ctl_error
)
412 snd_printd(KERN_ERR
"cannot get current value for "
413 "control %d ch %d: err = %d\n",
414 cval
->control
, channel
, err
);
417 cval
->cached
|= 1 << channel
;
418 cval
->cache_val
[index
] = *value
;
427 static int set_ctl_value(struct usb_mixer_elem_info
*cval
, int request
, int validx
, int value_set
)
429 unsigned char buf
[2];
430 int val_len
= cval
->val_type
>= USB_MIXER_S16
? 2 : 1;
433 value_set
= convert_bytes_value(cval
, value_set
);
434 buf
[0] = value_set
& 0xff;
435 buf
[1] = (value_set
>> 8) & 0xff;
436 while (timeout
-- > 0)
437 if (snd_usb_ctl_msg(cval
->mixer
->chip
->dev
,
438 usb_sndctrlpipe(cval
->mixer
->chip
->dev
, 0),
440 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
441 validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8),
442 buf
, val_len
, 100) >= 0)
444 snd_printdd(KERN_ERR
"cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
445 request
, validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8), cval
->val_type
, buf
[0], buf
[1]);
449 static int set_cur_ctl_value(struct usb_mixer_elem_info
*cval
, int validx
, int value
)
451 return set_ctl_value(cval
, SET_CUR
, validx
, value
);
454 static int set_cur_mix_value(struct usb_mixer_elem_info
*cval
, int channel
,
455 int index
, int value
)
458 err
= set_ctl_value(cval
, SET_CUR
, (cval
->control
<< 8) | channel
,
462 cval
->cached
|= 1 << channel
;
463 cval
->cache_val
[index
] = value
;
468 * TLV callback for mixer volume controls
470 static int mixer_vol_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
471 unsigned int size
, unsigned int __user
*_tlv
)
473 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
474 DECLARE_TLV_DB_MINMAX(scale
, 0, 0);
476 if (size
< sizeof(scale
))
478 scale
[2] = cval
->dBmin
;
479 scale
[3] = cval
->dBmax
;
480 if (copy_to_user(_tlv
, scale
, sizeof(scale
)))
486 * parser routines begin here...
489 static int parse_audio_unit(struct mixer_build
*state
, int unitid
);
493 * check if the input/output channel routing is enabled on the given bitmap.
494 * used for mixer unit parser
496 static int check_matrix_bitmap(unsigned char *bmap
, int ich
, int och
, int num_outs
)
498 int idx
= ich
* num_outs
+ och
;
499 return bmap
[idx
>> 3] & (0x80 >> (idx
& 7));
504 * add an alsa control element
505 * search and increment the index until an empty slot is found.
507 * if failed, give up and free the control instance.
510 static int add_control_to_empty(struct mixer_build
*state
, struct snd_kcontrol
*kctl
)
512 struct usb_mixer_elem_info
*cval
= kctl
->private_data
;
515 while (snd_ctl_find_id(state
->chip
->card
, &kctl
->id
))
517 if ((err
= snd_ctl_add(state
->chip
->card
, kctl
)) < 0) {
518 snd_printd(KERN_ERR
"cannot add control (err = %d)\n", err
);
521 cval
->elem_id
= &kctl
->id
;
522 cval
->next_id_elem
= state
->mixer
->id_elems
[cval
->id
];
523 state
->mixer
->id_elems
[cval
->id
] = cval
;
529 * get a terminal name string
532 static struct iterm_name_combo
{
536 { 0x0300, "Output" },
537 { 0x0301, "Speaker" },
538 { 0x0302, "Headphone" },
539 { 0x0303, "HMD Audio" },
540 { 0x0304, "Desktop Speaker" },
541 { 0x0305, "Room Speaker" },
542 { 0x0306, "Com Speaker" },
544 { 0x0600, "External In" },
545 { 0x0601, "Analog In" },
546 { 0x0602, "Digital In" },
548 { 0x0604, "Legacy In" },
549 { 0x0605, "IEC958 In" },
550 { 0x0606, "1394 DA Stream" },
551 { 0x0607, "1394 DV Stream" },
552 { 0x0700, "Embedded" },
553 { 0x0701, "Noise Source" },
554 { 0x0702, "Equalization Noise" },
558 { 0x0706, "MiniDisk" },
559 { 0x0707, "Analog Tape" },
560 { 0x0708, "Phonograph" },
561 { 0x0709, "VCR Audio" },
562 { 0x070a, "Video Disk Audio" },
563 { 0x070b, "DVD Audio" },
564 { 0x070c, "TV Tuner Audio" },
565 { 0x070d, "Satellite Rec Audio" },
566 { 0x070e, "Cable Tuner Audio" },
567 { 0x070f, "DSS Audio" },
568 { 0x0710, "Radio Receiver" },
569 { 0x0711, "Radio Transmitter" },
570 { 0x0712, "Multi-Track Recorder" },
571 { 0x0713, "Synthesizer" },
575 static int get_term_name(struct mixer_build
*state
, struct usb_audio_term
*iterm
,
576 unsigned char *name
, int maxlen
, int term_only
)
578 struct iterm_name_combo
*names
;
581 return snd_usb_copy_string_desc(state
, iterm
->name
, name
, maxlen
);
583 /* virtual type - not a real terminal */
584 if (iterm
->type
>> 16) {
587 switch (iterm
->type
>> 16) {
589 strcpy(name
, "Selector"); return 8;
590 case PROCESSING_UNIT
:
591 strcpy(name
, "Process Unit"); return 12;
593 strcpy(name
, "Ext Unit"); return 8;
595 strcpy(name
, "Mixer"); return 5;
597 return sprintf(name
, "Unit %d", iterm
->id
);
601 switch (iterm
->type
& 0xff00) {
603 strcpy(name
, "PCM"); return 3;
605 strcpy(name
, "Mic"); return 3;
607 strcpy(name
, "Headset"); return 7;
609 strcpy(name
, "Phone"); return 5;
612 for (names
= iterm_names
; names
->type
; names
++)
613 if (names
->type
== iterm
->type
) {
614 strcpy(name
, names
->name
);
615 return strlen(names
->name
);
622 * parse the source unit recursively until it reaches to a terminal
623 * or a branched unit.
625 static int check_input_term(struct mixer_build
*state
, int id
, struct usb_audio_term
*term
)
629 memset(term
, 0, sizeof(*term
));
630 while ((p1
= find_audio_control_unit(state
, id
)) != NULL
) {
634 term
->type
= combine_word(p1
+ 4);
635 term
->channels
= p1
[7];
636 term
->chconfig
= combine_word(p1
+ 8);
641 break; /* continue to parse */
643 term
->type
= p1
[2] << 16; /* virtual type */
644 term
->channels
= p1
[5 + p1
[4]];
645 term
->chconfig
= combine_word(p1
+ 6 + p1
[4]);
646 term
->name
= p1
[p1
[0] - 1];
649 /* call recursively to retrieve the channel info */
650 if (check_input_term(state
, p1
[5], term
) < 0)
652 term
->type
= p1
[2] << 16; /* virtual type */
654 term
->name
= p1
[9 + p1
[0] - 1];
656 case PROCESSING_UNIT
:
660 break; /* continue to parse */
662 term
->type
= p1
[2] << 16; /* virtual type */
663 term
->channels
= p1
[7 + p1
[6]];
664 term
->chconfig
= combine_word(p1
+ 8 + p1
[6]);
665 term
->name
= p1
[12 + p1
[6] + p1
[11 + p1
[6]]];
679 /* feature unit control information */
680 struct usb_feature_control_info
{
682 unsigned int type
; /* control type (mute, volume, etc.) */
685 static struct usb_feature_control_info audio_feature_info
[] = {
686 { "Mute", USB_MIXER_INV_BOOLEAN
},
687 { "Volume", USB_MIXER_S16
},
688 { "Tone Control - Bass", USB_MIXER_S8
},
689 { "Tone Control - Mid", USB_MIXER_S8
},
690 { "Tone Control - Treble", USB_MIXER_S8
},
691 { "Graphic Equalizer", USB_MIXER_S8
}, /* FIXME: not implemeted yet */
692 { "Auto Gain Control", USB_MIXER_BOOLEAN
},
693 { "Delay Control", USB_MIXER_U16
},
694 { "Bass Boost", USB_MIXER_BOOLEAN
},
695 { "Loudness", USB_MIXER_BOOLEAN
},
699 /* private_free callback */
700 static void usb_mixer_elem_free(struct snd_kcontrol
*kctl
)
702 kfree(kctl
->private_data
);
703 kctl
->private_data
= NULL
;
708 * interface to ALSA control for feature/mixer units
712 * retrieve the minimum and maximum values for the specified control
714 static int get_min_max(struct usb_mixer_elem_info
*cval
, int default_min
)
717 cval
->min
= default_min
;
718 cval
->max
= cval
->min
+ 1;
720 cval
->dBmin
= cval
->dBmax
= 0;
722 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
723 cval
->val_type
== USB_MIXER_INV_BOOLEAN
) {
724 cval
->initialized
= 1;
729 for (i
= 0; i
< MAX_CHANNELS
; i
++)
730 if (cval
->cmask
& (1 << i
)) {
735 if (get_ctl_value(cval
, GET_MAX
, (cval
->control
<< 8) | minchn
, &cval
->max
) < 0 ||
736 get_ctl_value(cval
, GET_MIN
, (cval
->control
<< 8) | minchn
, &cval
->min
) < 0) {
737 snd_printd(KERN_ERR
"%d:%d: cannot get min/max values for control %d (id %d)\n",
738 cval
->id
, cval
->mixer
->ctrlif
, cval
->control
, cval
->id
);
741 if (get_ctl_value(cval
, GET_RES
, (cval
->control
<< 8) | minchn
, &cval
->res
) < 0) {
744 int last_valid_res
= cval
->res
;
746 while (cval
->res
> 1) {
747 if (set_ctl_value(cval
, SET_RES
, (cval
->control
<< 8) | minchn
, cval
->res
/ 2) < 0)
751 if (get_ctl_value(cval
, GET_RES
, (cval
->control
<< 8) | minchn
, &cval
->res
) < 0)
752 cval
->res
= last_valid_res
;
757 /* Additional checks for the proper resolution
759 * Some devices report smaller resolutions than actually
760 * reacting. They don't return errors but simply clip
761 * to the lower aligned value.
763 if (cval
->min
+ cval
->res
< cval
->max
) {
764 int last_valid_res
= cval
->res
;
765 int saved
, test
, check
;
766 get_cur_mix_raw(cval
, minchn
, &saved
);
769 if (test
< cval
->max
)
773 if (test
< cval
->min
|| test
> cval
->max
||
774 set_cur_mix_value(cval
, minchn
, 0, test
) ||
775 get_cur_mix_raw(cval
, minchn
, &check
)) {
776 cval
->res
= last_valid_res
;
783 set_cur_mix_value(cval
, minchn
, 0, saved
);
786 cval
->initialized
= 1;
789 /* USB descriptions contain the dB scale in 1/256 dB unit
790 * while ALSA TLV contains in 1/100 dB unit
792 cval
->dBmin
= (convert_signed_value(cval
, cval
->min
) * 100) / 256;
793 cval
->dBmax
= (convert_signed_value(cval
, cval
->max
) * 100) / 256;
794 if (cval
->dBmin
> cval
->dBmax
) {
795 /* something is wrong; assume it's either from/to 0dB */
798 else if (cval
->dBmin
> 0)
800 if (cval
->dBmin
> cval
->dBmax
) {
801 /* totally crap, return an error */
810 /* get a feature/mixer unit info */
811 static int mixer_ctl_feature_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
813 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
815 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
816 cval
->val_type
== USB_MIXER_INV_BOOLEAN
)
817 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
819 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
820 uinfo
->count
= cval
->channels
;
821 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
822 cval
->val_type
== USB_MIXER_INV_BOOLEAN
) {
823 uinfo
->value
.integer
.min
= 0;
824 uinfo
->value
.integer
.max
= 1;
826 if (! cval
->initialized
)
827 get_min_max(cval
, 0);
828 uinfo
->value
.integer
.min
= 0;
829 uinfo
->value
.integer
.max
=
830 (cval
->max
- cval
->min
+ cval
->res
- 1) / cval
->res
;
835 /* get the current value from feature/mixer unit */
836 static int mixer_ctl_feature_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
838 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
839 int c
, cnt
, val
, err
;
841 ucontrol
->value
.integer
.value
[0] = cval
->min
;
844 for (c
= 0; c
< MAX_CHANNELS
; c
++) {
845 if (!(cval
->cmask
& (1 << c
)))
847 err
= get_cur_mix_value(cval
, c
+ 1, cnt
, &val
);
849 return cval
->mixer
->ignore_ctl_error
? 0 : err
;
850 val
= get_relative_value(cval
, val
);
851 ucontrol
->value
.integer
.value
[cnt
] = val
;
857 err
= get_cur_mix_value(cval
, 0, 0, &val
);
859 return cval
->mixer
->ignore_ctl_error
? 0 : err
;
860 val
= get_relative_value(cval
, val
);
861 ucontrol
->value
.integer
.value
[0] = val
;
866 /* put the current value to feature/mixer unit */
867 static int mixer_ctl_feature_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
869 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
870 int c
, cnt
, val
, oval
, err
;
875 for (c
= 0; c
< MAX_CHANNELS
; c
++) {
876 if (!(cval
->cmask
& (1 << c
)))
878 err
= get_cur_mix_value(cval
, c
+ 1, cnt
, &oval
);
880 return cval
->mixer
->ignore_ctl_error
? 0 : err
;
881 val
= ucontrol
->value
.integer
.value
[cnt
];
882 val
= get_abs_value(cval
, val
);
884 set_cur_mix_value(cval
, c
+ 1, cnt
, val
);
891 err
= get_cur_mix_value(cval
, 0, 0, &oval
);
893 return cval
->mixer
->ignore_ctl_error
? 0 : err
;
894 val
= ucontrol
->value
.integer
.value
[0];
895 val
= get_abs_value(cval
, val
);
897 set_cur_mix_value(cval
, 0, 0, val
);
904 static struct snd_kcontrol_new usb_feature_unit_ctl
= {
905 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
906 .name
= "", /* will be filled later manually */
907 .info
= mixer_ctl_feature_info
,
908 .get
= mixer_ctl_feature_get
,
909 .put
= mixer_ctl_feature_put
,
914 * build a feature control
917 static size_t append_ctl_name(struct snd_kcontrol
*kctl
, const char *str
)
919 return strlcat(kctl
->id
.name
, str
, sizeof(kctl
->id
.name
));
922 static void build_feature_ctl(struct mixer_build
*state
, unsigned char *desc
,
923 unsigned int ctl_mask
, int control
,
924 struct usb_audio_term
*iterm
, int unitid
)
926 unsigned int len
= 0;
928 int nameid
= desc
[desc
[0] - 1];
929 struct snd_kcontrol
*kctl
;
930 struct usb_mixer_elem_info
*cval
;
931 const struct usbmix_name_map
*map
;
933 control
++; /* change from zero-based to 1-based value */
935 if (control
== USB_FEATURE_GEQ
) {
936 /* FIXME: not supported yet */
940 map
= find_map(state
, unitid
, control
);
941 if (check_ignored_ctl(map
))
944 cval
= kzalloc(sizeof(*cval
), GFP_KERNEL
);
946 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
949 cval
->mixer
= state
->mixer
;
951 cval
->control
= control
;
952 cval
->cmask
= ctl_mask
;
953 cval
->val_type
= audio_feature_info
[control
-1].type
;
955 cval
->channels
= 1; /* master channel */
958 for (i
= 0; i
< 16; i
++)
959 if (ctl_mask
& (1 << i
))
964 /* get min/max values */
965 get_min_max(cval
, 0);
967 kctl
= snd_ctl_new1(&usb_feature_unit_ctl
, cval
);
969 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
973 kctl
->private_free
= usb_mixer_elem_free
;
975 len
= check_mapped_name(map
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
976 mapped_name
= len
!= 0;
978 len
= snd_usb_copy_string_desc(state
, nameid
,
979 kctl
->id
.name
, sizeof(kctl
->id
.name
));
982 case USB_FEATURE_MUTE
:
983 case USB_FEATURE_VOLUME
:
984 /* determine the control name. the rule is:
985 * - if a name id is given in descriptor, use it.
986 * - if the connected input can be determined, then use the name
988 * - if the connected output can be determined, use it.
989 * - otherwise, anonymous name.
992 len
= get_term_name(state
, iterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 1);
994 len
= get_term_name(state
, &state
->oterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 1);
996 len
= snprintf(kctl
->id
.name
, sizeof(kctl
->id
.name
),
997 "Feature %d", unitid
);
999 /* determine the stream direction:
1000 * if the connected output is USB stream, then it's likely a
1001 * capture stream. otherwise it should be playback (hopefully :)
1003 if (! mapped_name
&& ! (state
->oterm
.type
>> 16)) {
1004 if ((state
->oterm
.type
& 0xff00) == 0x0100) {
1005 len
= append_ctl_name(kctl
, " Capture");
1007 len
= append_ctl_name(kctl
, " Playback");
1010 append_ctl_name(kctl
, control
== USB_FEATURE_MUTE
?
1011 " Switch" : " Volume");
1012 if (control
== USB_FEATURE_VOLUME
) {
1013 kctl
->tlv
.c
= mixer_vol_tlv
;
1014 kctl
->vd
[0].access
|=
1015 SNDRV_CTL_ELEM_ACCESS_TLV_READ
|
1016 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
;
1017 check_mapped_dB(map
, cval
);
1023 strlcpy(kctl
->id
.name
, audio_feature_info
[control
-1].name
,
1024 sizeof(kctl
->id
.name
));
1028 /* volume control quirks */
1029 switch (state
->chip
->usb_id
) {
1030 case USB_ID(0x0471, 0x0101):
1031 case USB_ID(0x0471, 0x0104):
1032 case USB_ID(0x0471, 0x0105):
1033 case USB_ID(0x0672, 0x1041):
1034 /* quirk for UDA1321/N101.
1035 * note that detection between firmware 2.1.1.7 (N101)
1036 * and later 2.1.1.21 is not very clear from datasheets.
1037 * I hope that the min value is -15360 for newer firmware --jk
1039 if (!strcmp(kctl
->id
.name
, "PCM Playback Volume") &&
1040 cval
->min
== -15616) {
1041 snd_printk(KERN_INFO
1042 "set volume quirk for UDA1321/N101 chip\n");
1047 case USB_ID(0x046d, 0x09a4):
1048 if (!strcmp(kctl
->id
.name
, "Mic Capture Volume")) {
1049 snd_printk(KERN_INFO
1050 "set volume quirk for QuickCam E3500\n");
1059 snd_printdd(KERN_INFO
"[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1060 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
, cval
->res
);
1061 add_control_to_empty(state
, kctl
);
1067 * parse a feature unit
1069 * most of controlls are defined here.
1071 static int parse_audio_feature_unit(struct mixer_build
*state
, int unitid
, unsigned char *ftr
)
1074 struct usb_audio_term iterm
;
1075 unsigned int master_bits
, first_ch_bits
;
1078 if (ftr
[0] < 7 || ! (csize
= ftr
[5]) || ftr
[0] < 7 + csize
) {
1079 snd_printk(KERN_ERR
"usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid
);
1083 /* parse the source unit */
1084 if ((err
= parse_audio_unit(state
, ftr
[4])) < 0)
1087 /* determine the input source type and name */
1088 if (check_input_term(state
, ftr
[4], &iterm
) < 0)
1091 channels
= (ftr
[0] - 7) / csize
- 1;
1093 master_bits
= snd_usb_combine_bytes(ftr
+ 6, csize
);
1094 /* master configuration quirks */
1095 switch (state
->chip
->usb_id
) {
1096 case USB_ID(0x08bb, 0x2702):
1097 snd_printk(KERN_INFO
1098 "usbmixer: master volume quirk for PCM2702 chip\n");
1099 /* disable non-functional volume control */
1100 master_bits
&= ~(1 << (USB_FEATURE_VOLUME
- 1));
1104 first_ch_bits
= snd_usb_combine_bytes(ftr
+ 6 + csize
, csize
);
1107 /* check all control types */
1108 for (i
= 0; i
< 10; i
++) {
1109 unsigned int ch_bits
= 0;
1110 for (j
= 0; j
< channels
; j
++) {
1111 unsigned int mask
= snd_usb_combine_bytes(ftr
+ 6 + csize
* (j
+1), csize
);
1112 if (mask
& (1 << i
))
1113 ch_bits
|= (1 << j
);
1115 if (ch_bits
& 1) /* the first channel must be set (for ease of programming) */
1116 build_feature_ctl(state
, ftr
, ch_bits
, i
, &iterm
, unitid
);
1117 if (master_bits
& (1 << i
))
1118 build_feature_ctl(state
, ftr
, 0, i
, &iterm
, unitid
);
1130 * build a mixer unit control
1132 * the callbacks are identical with feature unit.
1133 * input channel number (zero based) is given in control field instead.
1136 static void build_mixer_unit_ctl(struct mixer_build
*state
, unsigned char *desc
,
1137 int in_pin
, int in_ch
, int unitid
,
1138 struct usb_audio_term
*iterm
)
1140 struct usb_mixer_elem_info
*cval
;
1141 unsigned int input_pins
= desc
[4];
1142 unsigned int num_outs
= desc
[5 + input_pins
];
1143 unsigned int i
, len
;
1144 struct snd_kcontrol
*kctl
;
1145 const struct usbmix_name_map
*map
;
1147 map
= find_map(state
, unitid
, 0);
1148 if (check_ignored_ctl(map
))
1151 cval
= kzalloc(sizeof(*cval
), GFP_KERNEL
);
1155 cval
->mixer
= state
->mixer
;
1157 cval
->control
= in_ch
+ 1; /* based on 1 */
1158 cval
->val_type
= USB_MIXER_S16
;
1159 for (i
= 0; i
< num_outs
; i
++) {
1160 if (check_matrix_bitmap(desc
+ 9 + input_pins
, in_ch
, i
, num_outs
)) {
1161 cval
->cmask
|= (1 << i
);
1166 /* get min/max values */
1167 get_min_max(cval
, 0);
1169 kctl
= snd_ctl_new1(&usb_feature_unit_ctl
, cval
);
1171 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1175 kctl
->private_free
= usb_mixer_elem_free
;
1177 len
= check_mapped_name(map
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1179 len
= get_term_name(state
, iterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 0);
1181 len
= sprintf(kctl
->id
.name
, "Mixer Source %d", in_ch
+ 1);
1182 append_ctl_name(kctl
, " Volume");
1184 snd_printdd(KERN_INFO
"[%d] MU [%s] ch = %d, val = %d/%d\n",
1185 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
);
1186 add_control_to_empty(state
, kctl
);
1191 * parse a mixer unit
1193 static int parse_audio_mixer_unit(struct mixer_build
*state
, int unitid
, unsigned char *desc
)
1195 struct usb_audio_term iterm
;
1196 int input_pins
, num_ins
, num_outs
;
1199 if (desc
[0] < 11 || ! (input_pins
= desc
[4]) || ! (num_outs
= desc
[5 + input_pins
])) {
1200 snd_printk(KERN_ERR
"invalid MIXER UNIT descriptor %d\n", unitid
);
1203 /* no bmControls field (e.g. Maya44) -> ignore */
1204 if (desc
[0] <= 10 + input_pins
) {
1205 snd_printdd(KERN_INFO
"MU %d has no bmControls field\n", unitid
);
1211 for (pin
= 0; pin
< input_pins
; pin
++) {
1212 err
= parse_audio_unit(state
, desc
[5 + pin
]);
1215 err
= check_input_term(state
, desc
[5 + pin
], &iterm
);
1218 num_ins
+= iterm
.channels
;
1219 for (; ich
< num_ins
; ++ich
) {
1220 int och
, ich_has_controls
= 0;
1222 for (och
= 0; och
< num_outs
; ++och
) {
1223 if (check_matrix_bitmap(desc
+ 9 + input_pins
,
1224 ich
, och
, num_outs
)) {
1225 ich_has_controls
= 1;
1229 if (ich_has_controls
)
1230 build_mixer_unit_ctl(state
, desc
, pin
, ich
,
1239 * Processing Unit / Extension Unit
1242 /* get callback for processing/extension unit */
1243 static int mixer_ctl_procunit_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1245 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1248 err
= get_cur_ctl_value(cval
, cval
->control
<< 8, &val
);
1249 if (err
< 0 && cval
->mixer
->ignore_ctl_error
) {
1250 ucontrol
->value
.integer
.value
[0] = cval
->min
;
1255 val
= get_relative_value(cval
, val
);
1256 ucontrol
->value
.integer
.value
[0] = val
;
1260 /* put callback for processing/extension unit */
1261 static int mixer_ctl_procunit_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1263 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1266 err
= get_cur_ctl_value(cval
, cval
->control
<< 8, &oval
);
1268 if (cval
->mixer
->ignore_ctl_error
)
1272 val
= ucontrol
->value
.integer
.value
[0];
1273 val
= get_abs_value(cval
, val
);
1275 set_cur_ctl_value(cval
, cval
->control
<< 8, val
);
1281 /* alsa control interface for processing/extension unit */
1282 static struct snd_kcontrol_new mixer_procunit_ctl
= {
1283 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1284 .name
= "", /* will be filled later */
1285 .info
= mixer_ctl_feature_info
,
1286 .get
= mixer_ctl_procunit_get
,
1287 .put
= mixer_ctl_procunit_put
,
1292 * predefined data for processing units
1294 struct procunit_value_info
{
1301 struct procunit_info
{
1304 struct procunit_value_info
*values
;
1307 static struct procunit_value_info updown_proc_info
[] = {
1308 { USB_PROC_UPDOWN_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1309 { USB_PROC_UPDOWN_MODE_SEL
, "Mode Select", USB_MIXER_U8
, 1 },
1312 static struct procunit_value_info prologic_proc_info
[] = {
1313 { USB_PROC_PROLOGIC_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1314 { USB_PROC_PROLOGIC_MODE_SEL
, "Mode Select", USB_MIXER_U8
, 1 },
1317 static struct procunit_value_info threed_enh_proc_info
[] = {
1318 { USB_PROC_3DENH_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1319 { USB_PROC_3DENH_SPACE
, "Spaciousness", USB_MIXER_U8
},
1322 static struct procunit_value_info reverb_proc_info
[] = {
1323 { USB_PROC_REVERB_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1324 { USB_PROC_REVERB_LEVEL
, "Level", USB_MIXER_U8
},
1325 { USB_PROC_REVERB_TIME
, "Time", USB_MIXER_U16
},
1326 { USB_PROC_REVERB_DELAY
, "Delay", USB_MIXER_U8
},
1329 static struct procunit_value_info chorus_proc_info
[] = {
1330 { USB_PROC_CHORUS_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1331 { USB_PROC_CHORUS_LEVEL
, "Level", USB_MIXER_U8
},
1332 { USB_PROC_CHORUS_RATE
, "Rate", USB_MIXER_U16
},
1333 { USB_PROC_CHORUS_DEPTH
, "Depth", USB_MIXER_U16
},
1336 static struct procunit_value_info dcr_proc_info
[] = {
1337 { USB_PROC_DCR_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1338 { USB_PROC_DCR_RATIO
, "Ratio", USB_MIXER_U16
},
1339 { USB_PROC_DCR_MAX_AMP
, "Max Amp", USB_MIXER_S16
},
1340 { USB_PROC_DCR_THRESHOLD
, "Threshold", USB_MIXER_S16
},
1341 { USB_PROC_DCR_ATTACK
, "Attack Time", USB_MIXER_U16
},
1342 { USB_PROC_DCR_RELEASE
, "Release Time", USB_MIXER_U16
},
1346 static struct procunit_info procunits
[] = {
1347 { USB_PROC_UPDOWN
, "Up Down", updown_proc_info
},
1348 { USB_PROC_PROLOGIC
, "Dolby Prologic", prologic_proc_info
},
1349 { USB_PROC_3DENH
, "3D Stereo Extender", threed_enh_proc_info
},
1350 { USB_PROC_REVERB
, "Reverb", reverb_proc_info
},
1351 { USB_PROC_CHORUS
, "Chorus", chorus_proc_info
},
1352 { USB_PROC_DCR
, "DCR", dcr_proc_info
},
1357 * build a processing/extension unit
1359 static int build_audio_procunit(struct mixer_build
*state
, int unitid
, unsigned char *dsc
, struct procunit_info
*list
, char *name
)
1361 int num_ins
= dsc
[6];
1362 struct usb_mixer_elem_info
*cval
;
1363 struct snd_kcontrol
*kctl
;
1364 int i
, err
, nameid
, type
, len
;
1365 struct procunit_info
*info
;
1366 struct procunit_value_info
*valinfo
;
1367 const struct usbmix_name_map
*map
;
1368 static struct procunit_value_info default_value_info
[] = {
1369 { 0x01, "Switch", USB_MIXER_BOOLEAN
},
1372 static struct procunit_info default_info
= {
1373 0, NULL
, default_value_info
1376 if (dsc
[0] < 13 || dsc
[0] < 13 + num_ins
|| dsc
[0] < num_ins
+ dsc
[11 + num_ins
]) {
1377 snd_printk(KERN_ERR
"invalid %s descriptor (id %d)\n", name
, unitid
);
1381 for (i
= 0; i
< num_ins
; i
++) {
1382 if ((err
= parse_audio_unit(state
, dsc
[7 + i
])) < 0)
1386 type
= combine_word(&dsc
[4]);
1387 for (info
= list
; info
&& info
->type
; info
++)
1388 if (info
->type
== type
)
1390 if (! info
|| ! info
->type
)
1391 info
= &default_info
;
1393 for (valinfo
= info
->values
; valinfo
->control
; valinfo
++) {
1394 /* FIXME: bitmap might be longer than 8bit */
1395 if (! (dsc
[12 + num_ins
] & (1 << (valinfo
->control
- 1))))
1397 map
= find_map(state
, unitid
, valinfo
->control
);
1398 if (check_ignored_ctl(map
))
1400 cval
= kzalloc(sizeof(*cval
), GFP_KERNEL
);
1402 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1405 cval
->mixer
= state
->mixer
;
1407 cval
->control
= valinfo
->control
;
1408 cval
->val_type
= valinfo
->val_type
;
1411 /* get min/max values */
1412 if (type
== USB_PROC_UPDOWN
&& cval
->control
== USB_PROC_UPDOWN_MODE_SEL
) {
1413 /* FIXME: hard-coded */
1415 cval
->max
= dsc
[15];
1417 cval
->initialized
= 1;
1419 get_min_max(cval
, valinfo
->min_value
);
1421 kctl
= snd_ctl_new1(&mixer_procunit_ctl
, cval
);
1423 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1427 kctl
->private_free
= usb_mixer_elem_free
;
1429 if (check_mapped_name(map
, kctl
->id
.name
,
1430 sizeof(kctl
->id
.name
)))
1432 else if (info
->name
)
1433 strlcpy(kctl
->id
.name
, info
->name
, sizeof(kctl
->id
.name
));
1435 nameid
= dsc
[12 + num_ins
+ dsc
[11 + num_ins
]];
1438 len
= snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1440 strlcpy(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
));
1442 append_ctl_name(kctl
, " ");
1443 append_ctl_name(kctl
, valinfo
->suffix
);
1445 snd_printdd(KERN_INFO
"[%d] PU [%s] ch = %d, val = %d/%d\n",
1446 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
);
1447 if ((err
= add_control_to_empty(state
, kctl
)) < 0)
1454 static int parse_audio_processing_unit(struct mixer_build
*state
, int unitid
, unsigned char *desc
)
1456 return build_audio_procunit(state
, unitid
, desc
, procunits
, "Processing Unit");
1459 static int parse_audio_extension_unit(struct mixer_build
*state
, int unitid
, unsigned char *desc
)
1461 return build_audio_procunit(state
, unitid
, desc
, NULL
, "Extension Unit");
1469 /* info callback for selector unit
1470 * use an enumerator type for routing
1472 static int mixer_ctl_selector_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
1474 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1475 char **itemlist
= (char **)kcontrol
->private_value
;
1477 if (snd_BUG_ON(!itemlist
))
1479 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1481 uinfo
->value
.enumerated
.items
= cval
->max
;
1482 if ((int)uinfo
->value
.enumerated
.item
>= cval
->max
)
1483 uinfo
->value
.enumerated
.item
= cval
->max
- 1;
1484 strcpy(uinfo
->value
.enumerated
.name
, itemlist
[uinfo
->value
.enumerated
.item
]);
1488 /* get callback for selector unit */
1489 static int mixer_ctl_selector_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1491 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1494 err
= get_cur_ctl_value(cval
, 0, &val
);
1496 if (cval
->mixer
->ignore_ctl_error
) {
1497 ucontrol
->value
.enumerated
.item
[0] = 0;
1502 val
= get_relative_value(cval
, val
);
1503 ucontrol
->value
.enumerated
.item
[0] = val
;
1507 /* put callback for selector unit */
1508 static int mixer_ctl_selector_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1510 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1513 err
= get_cur_ctl_value(cval
, 0, &oval
);
1515 if (cval
->mixer
->ignore_ctl_error
)
1519 val
= ucontrol
->value
.enumerated
.item
[0];
1520 val
= get_abs_value(cval
, val
);
1522 set_cur_ctl_value(cval
, 0, val
);
1528 /* alsa control interface for selector unit */
1529 static struct snd_kcontrol_new mixer_selectunit_ctl
= {
1530 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1531 .name
= "", /* will be filled later */
1532 .info
= mixer_ctl_selector_info
,
1533 .get
= mixer_ctl_selector_get
,
1534 .put
= mixer_ctl_selector_put
,
1538 /* private free callback.
1539 * free both private_data and private_value
1541 static void usb_mixer_selector_elem_free(struct snd_kcontrol
*kctl
)
1545 if (kctl
->private_data
) {
1546 struct usb_mixer_elem_info
*cval
= kctl
->private_data
;
1547 num_ins
= cval
->max
;
1549 kctl
->private_data
= NULL
;
1551 if (kctl
->private_value
) {
1552 char **itemlist
= (char **)kctl
->private_value
;
1553 for (i
= 0; i
< num_ins
; i
++)
1556 kctl
->private_value
= 0;
1561 * parse a selector unit
1563 static int parse_audio_selector_unit(struct mixer_build
*state
, int unitid
, unsigned char *desc
)
1565 unsigned int num_ins
= desc
[4];
1566 unsigned int i
, nameid
, len
;
1568 struct usb_mixer_elem_info
*cval
;
1569 struct snd_kcontrol
*kctl
;
1570 const struct usbmix_name_map
*map
;
1573 if (! num_ins
|| desc
[0] < 5 + num_ins
) {
1574 snd_printk(KERN_ERR
"invalid SELECTOR UNIT descriptor %d\n", unitid
);
1578 for (i
= 0; i
< num_ins
; i
++) {
1579 if ((err
= parse_audio_unit(state
, desc
[5 + i
])) < 0)
1583 if (num_ins
== 1) /* only one ? nonsense! */
1586 map
= find_map(state
, unitid
, 0);
1587 if (check_ignored_ctl(map
))
1590 cval
= kzalloc(sizeof(*cval
), GFP_KERNEL
);
1592 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1595 cval
->mixer
= state
->mixer
;
1597 cval
->val_type
= USB_MIXER_U8
;
1600 cval
->max
= num_ins
;
1602 cval
->initialized
= 1;
1604 namelist
= kmalloc(sizeof(char *) * num_ins
, GFP_KERNEL
);
1606 snd_printk(KERN_ERR
"cannot malloc\n");
1610 #define MAX_ITEM_NAME_LEN 64
1611 for (i
= 0; i
< num_ins
; i
++) {
1612 struct usb_audio_term iterm
;
1614 namelist
[i
] = kmalloc(MAX_ITEM_NAME_LEN
, GFP_KERNEL
);
1615 if (! namelist
[i
]) {
1616 snd_printk(KERN_ERR
"cannot malloc\n");
1623 len
= check_mapped_selector_name(state
, unitid
, i
, namelist
[i
],
1625 if (! len
&& check_input_term(state
, desc
[5 + i
], &iterm
) >= 0)
1626 len
= get_term_name(state
, &iterm
, namelist
[i
], MAX_ITEM_NAME_LEN
, 0);
1628 sprintf(namelist
[i
], "Input %d", i
);
1631 kctl
= snd_ctl_new1(&mixer_selectunit_ctl
, cval
);
1633 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1638 kctl
->private_value
= (unsigned long)namelist
;
1639 kctl
->private_free
= usb_mixer_selector_elem_free
;
1641 nameid
= desc
[desc
[0] - 1];
1642 len
= check_mapped_name(map
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1646 snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1648 len
= get_term_name(state
, &state
->oterm
,
1649 kctl
->id
.name
, sizeof(kctl
->id
.name
), 0);
1651 strlcpy(kctl
->id
.name
, "USB", sizeof(kctl
->id
.name
));
1653 if ((state
->oterm
.type
& 0xff00) == 0x0100)
1654 append_ctl_name(kctl
, " Capture Source");
1656 append_ctl_name(kctl
, " Playback Source");
1659 snd_printdd(KERN_INFO
"[%d] SU [%s] items = %d\n",
1660 cval
->id
, kctl
->id
.name
, num_ins
);
1661 if ((err
= add_control_to_empty(state
, kctl
)) < 0)
1669 * parse an audio unit recursively
1672 static int parse_audio_unit(struct mixer_build
*state
, int unitid
)
1676 if (test_and_set_bit(unitid
, state
->unitbitmap
))
1677 return 0; /* the unit already visited */
1679 p1
= find_audio_control_unit(state
, unitid
);
1681 snd_printk(KERN_ERR
"usbaudio: unit %d not found!\n", unitid
);
1686 case INPUT_TERMINAL
:
1689 return parse_audio_mixer_unit(state
, unitid
, p1
);
1691 return parse_audio_selector_unit(state
, unitid
, p1
);
1693 return parse_audio_feature_unit(state
, unitid
, p1
);
1694 case PROCESSING_UNIT
:
1695 return parse_audio_processing_unit(state
, unitid
, p1
);
1696 case EXTENSION_UNIT
:
1697 return parse_audio_extension_unit(state
, unitid
, p1
);
1699 snd_printk(KERN_ERR
"usbaudio: unit %u: unexpected type 0x%02x\n", unitid
, p1
[2]);
1704 static void snd_usb_mixer_free(struct usb_mixer_interface
*mixer
)
1706 kfree(mixer
->id_elems
);
1708 kfree(mixer
->urb
->transfer_buffer
);
1709 usb_free_urb(mixer
->urb
);
1711 usb_free_urb(mixer
->rc_urb
);
1712 kfree(mixer
->rc_setup_packet
);
1716 static int snd_usb_mixer_dev_free(struct snd_device
*device
)
1718 struct usb_mixer_interface
*mixer
= device
->device_data
;
1719 snd_usb_mixer_free(mixer
);
1724 * create mixer controls
1726 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1728 static int snd_usb_mixer_controls(struct usb_mixer_interface
*mixer
)
1730 unsigned char *desc
;
1731 struct mixer_build state
;
1733 const struct usbmix_ctl_map
*map
;
1734 struct usb_host_interface
*hostif
;
1736 hostif
= &usb_ifnum_to_if(mixer
->chip
->dev
, mixer
->ctrlif
)->altsetting
[0];
1737 memset(&state
, 0, sizeof(state
));
1738 state
.chip
= mixer
->chip
;
1739 state
.mixer
= mixer
;
1740 state
.buffer
= hostif
->extra
;
1741 state
.buflen
= hostif
->extralen
;
1743 /* check the mapping table */
1744 for (map
= usbmix_ctl_maps
; map
->id
; map
++) {
1745 if (map
->id
== state
.chip
->usb_id
) {
1746 state
.map
= map
->map
;
1747 state
.selector_map
= map
->selector_map
;
1748 mixer
->ignore_ctl_error
= map
->ignore_ctl_error
;
1754 while ((desc
= snd_usb_find_csint_desc(hostif
->extra
, hostif
->extralen
, desc
, OUTPUT_TERMINAL
)) != NULL
) {
1756 continue; /* invalid descriptor? */
1757 set_bit(desc
[3], state
.unitbitmap
); /* mark terminal ID as visited */
1758 state
.oterm
.id
= desc
[3];
1759 state
.oterm
.type
= combine_word(&desc
[4]);
1760 state
.oterm
.name
= desc
[8];
1761 err
= parse_audio_unit(&state
, desc
[7]);
1768 static void snd_usb_mixer_notify_id(struct usb_mixer_interface
*mixer
,
1771 struct usb_mixer_elem_info
*info
;
1773 for (info
= mixer
->id_elems
[unitid
]; info
; info
= info
->next_id_elem
)
1774 snd_ctl_notify(mixer
->chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
1778 static void snd_usb_mixer_memory_change(struct usb_mixer_interface
*mixer
,
1783 /* unit ids specific to Extigy/Audigy 2 NX: */
1785 case 0: /* remote control */
1786 mixer
->rc_urb
->dev
= mixer
->chip
->dev
;
1787 usb_submit_urb(mixer
->rc_urb
, GFP_ATOMIC
);
1789 case 4: /* digital in jack */
1790 case 7: /* line in jacks */
1791 case 19: /* speaker out jacks */
1792 case 20: /* headphones out jack */
1794 /* live24ext: 4 = line-in jack */
1795 case 3: /* hp-out jack (may actuate Mute) */
1796 if (mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3040) ||
1797 mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3048))
1798 snd_usb_mixer_notify_id(mixer
, mixer
->rc_cfg
->mute_mixer_id
);
1801 snd_printd(KERN_DEBUG
"memory change in unknown unit %d\n", unitid
);
1806 static void snd_usb_mixer_status_complete(struct urb
*urb
)
1808 struct usb_mixer_interface
*mixer
= urb
->context
;
1810 if (urb
->status
== 0) {
1811 u8
*buf
= urb
->transfer_buffer
;
1814 for (i
= urb
->actual_length
; i
>= 2; buf
+= 2, i
-= 2) {
1815 snd_printd(KERN_DEBUG
"status interrupt: %02x %02x\n",
1817 /* ignore any notifications not from the control interface */
1818 if ((buf
[0] & 0x0f) != 0)
1820 if (!(buf
[0] & 0x40))
1821 snd_usb_mixer_notify_id(mixer
, buf
[1]);
1823 snd_usb_mixer_memory_change(mixer
, buf
[1]);
1826 if (urb
->status
!= -ENOENT
&& urb
->status
!= -ECONNRESET
) {
1827 urb
->dev
= mixer
->chip
->dev
;
1828 usb_submit_urb(urb
, GFP_ATOMIC
);
1832 /* create the handler for the optional status interrupt endpoint */
1833 static int snd_usb_mixer_status_create(struct usb_mixer_interface
*mixer
)
1835 struct usb_host_interface
*hostif
;
1836 struct usb_endpoint_descriptor
*ep
;
1837 void *transfer_buffer
;
1841 hostif
= &usb_ifnum_to_if(mixer
->chip
->dev
, mixer
->ctrlif
)->altsetting
[0];
1842 /* we need one interrupt input endpoint */
1843 if (get_iface_desc(hostif
)->bNumEndpoints
< 1)
1845 ep
= get_endpoint(hostif
, 0);
1846 if (!usb_endpoint_dir_in(ep
) || !usb_endpoint_xfer_int(ep
))
1849 epnum
= usb_endpoint_num(ep
);
1850 buffer_length
= le16_to_cpu(ep
->wMaxPacketSize
);
1851 transfer_buffer
= kmalloc(buffer_length
, GFP_KERNEL
);
1852 if (!transfer_buffer
)
1854 mixer
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
1856 kfree(transfer_buffer
);
1859 usb_fill_int_urb(mixer
->urb
, mixer
->chip
->dev
,
1860 usb_rcvintpipe(mixer
->chip
->dev
, epnum
),
1861 transfer_buffer
, buffer_length
,
1862 snd_usb_mixer_status_complete
, mixer
, ep
->bInterval
);
1863 usb_submit_urb(mixer
->urb
, GFP_KERNEL
);
1867 static void snd_usb_soundblaster_remote_complete(struct urb
*urb
)
1869 struct usb_mixer_interface
*mixer
= urb
->context
;
1870 const struct rc_config
*rc
= mixer
->rc_cfg
;
1873 if (urb
->status
< 0 || urb
->actual_length
< rc
->min_packet_length
)
1876 code
= mixer
->rc_buffer
[rc
->offset
];
1877 if (rc
->length
== 2)
1878 code
|= mixer
->rc_buffer
[rc
->offset
+ 1] << 8;
1880 /* the Mute button actually changes the mixer control */
1881 if (code
== rc
->mute_code
)
1882 snd_usb_mixer_notify_id(mixer
, rc
->mute_mixer_id
);
1883 mixer
->rc_code
= code
;
1885 wake_up(&mixer
->rc_waitq
);
1888 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep
*hw
, char __user
*buf
,
1889 long count
, loff_t
*offset
)
1891 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1895 if (count
!= 1 && count
!= 4)
1897 err
= wait_event_interruptible(mixer
->rc_waitq
,
1898 (rc_code
= xchg(&mixer
->rc_code
, 0)) != 0);
1901 err
= put_user(rc_code
, buf
);
1903 err
= put_user(rc_code
, (u32 __user
*)buf
);
1905 return err
< 0 ? err
: count
;
1908 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep
*hw
, struct file
*file
,
1911 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1913 poll_wait(file
, &mixer
->rc_waitq
, wait
);
1914 return mixer
->rc_code
? POLLIN
| POLLRDNORM
: 0;
1917 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface
*mixer
)
1919 struct snd_hwdep
*hwdep
;
1922 for (i
= 0; i
< ARRAY_SIZE(rc_configs
); ++i
)
1923 if (rc_configs
[i
].usb_id
== mixer
->chip
->usb_id
)
1925 if (i
>= ARRAY_SIZE(rc_configs
))
1927 mixer
->rc_cfg
= &rc_configs
[i
];
1929 len
= mixer
->rc_cfg
->packet_length
;
1931 init_waitqueue_head(&mixer
->rc_waitq
);
1932 err
= snd_hwdep_new(mixer
->chip
->card
, "SB remote control", 0, &hwdep
);
1935 snprintf(hwdep
->name
, sizeof(hwdep
->name
),
1936 "%s remote control", mixer
->chip
->card
->shortname
);
1937 hwdep
->iface
= SNDRV_HWDEP_IFACE_SB_RC
;
1938 hwdep
->private_data
= mixer
;
1939 hwdep
->ops
.read
= snd_usb_sbrc_hwdep_read
;
1940 hwdep
->ops
.poll
= snd_usb_sbrc_hwdep_poll
;
1941 hwdep
->exclusive
= 1;
1943 mixer
->rc_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1946 mixer
->rc_setup_packet
= kmalloc(sizeof(*mixer
->rc_setup_packet
), GFP_KERNEL
);
1947 if (!mixer
->rc_setup_packet
) {
1948 usb_free_urb(mixer
->rc_urb
);
1949 mixer
->rc_urb
= NULL
;
1952 mixer
->rc_setup_packet
->bRequestType
=
1953 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
1954 mixer
->rc_setup_packet
->bRequest
= GET_MEM
;
1955 mixer
->rc_setup_packet
->wValue
= cpu_to_le16(0);
1956 mixer
->rc_setup_packet
->wIndex
= cpu_to_le16(0);
1957 mixer
->rc_setup_packet
->wLength
= cpu_to_le16(len
);
1958 usb_fill_control_urb(mixer
->rc_urb
, mixer
->chip
->dev
,
1959 usb_rcvctrlpipe(mixer
->chip
->dev
, 0),
1960 (u8
*)mixer
->rc_setup_packet
, mixer
->rc_buffer
, len
,
1961 snd_usb_soundblaster_remote_complete
, mixer
);
1965 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
1967 static int snd_audigy2nx_led_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1969 struct usb_mixer_interface
*mixer
= snd_kcontrol_chip(kcontrol
);
1970 int index
= kcontrol
->private_value
;
1972 ucontrol
->value
.integer
.value
[0] = mixer
->audigy2nx_leds
[index
];
1976 static int snd_audigy2nx_led_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1978 struct usb_mixer_interface
*mixer
= snd_kcontrol_chip(kcontrol
);
1979 int index
= kcontrol
->private_value
;
1980 int value
= ucontrol
->value
.integer
.value
[0];
1985 changed
= value
!= mixer
->audigy2nx_leds
[index
];
1986 err
= snd_usb_ctl_msg(mixer
->chip
->dev
,
1987 usb_sndctrlpipe(mixer
->chip
->dev
, 0), 0x24,
1988 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_OTHER
,
1989 value
, index
+ 2, NULL
, 0, 100);
1992 mixer
->audigy2nx_leds
[index
] = value
;
1996 static struct snd_kcontrol_new snd_audigy2nx_controls
[] = {
1998 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1999 .name
= "CMSS LED Switch",
2000 .info
= snd_audigy2nx_led_info
,
2001 .get
= snd_audigy2nx_led_get
,
2002 .put
= snd_audigy2nx_led_put
,
2006 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2007 .name
= "Power LED Switch",
2008 .info
= snd_audigy2nx_led_info
,
2009 .get
= snd_audigy2nx_led_get
,
2010 .put
= snd_audigy2nx_led_put
,
2014 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2015 .name
= "Dolby Digital LED Switch",
2016 .info
= snd_audigy2nx_led_info
,
2017 .get
= snd_audigy2nx_led_get
,
2018 .put
= snd_audigy2nx_led_put
,
2023 static int snd_audigy2nx_controls_create(struct usb_mixer_interface
*mixer
)
2027 for (i
= 0; i
< ARRAY_SIZE(snd_audigy2nx_controls
); ++i
) {
2028 if (i
> 1 && /* Live24ext has 2 LEDs only */
2029 (mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3040) ||
2030 mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3048)))
2032 err
= snd_ctl_add(mixer
->chip
->card
,
2033 snd_ctl_new1(&snd_audigy2nx_controls
[i
], mixer
));
2037 mixer
->audigy2nx_leds
[1] = 1; /* Power LED is on by default */
2041 static void snd_audigy2nx_proc_read(struct snd_info_entry
*entry
,
2042 struct snd_info_buffer
*buffer
)
2044 static const struct sb_jack
{
2047 } jacks_audigy2nx
[] = {
2053 }, jacks_live24ext
[] = {
2054 {4, "line in"}, /* &1=Line, &2=Mic*/
2055 {3, "hph out"}, /* headphones */
2056 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2059 const struct sb_jack
*jacks
;
2060 struct usb_mixer_interface
*mixer
= entry
->private_data
;
2064 snd_iprintf(buffer
, "%s jacks\n\n", mixer
->chip
->card
->shortname
);
2065 if (mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3020))
2066 jacks
= jacks_audigy2nx
;
2067 else if (mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3040) ||
2068 mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3048))
2069 jacks
= jacks_live24ext
;
2073 for (i
= 0; jacks
[i
].name
; ++i
) {
2074 snd_iprintf(buffer
, "%s: ", jacks
[i
].name
);
2075 err
= snd_usb_ctl_msg(mixer
->chip
->dev
,
2076 usb_rcvctrlpipe(mixer
->chip
->dev
, 0),
2077 GET_MEM
, USB_DIR_IN
| USB_TYPE_CLASS
|
2078 USB_RECIP_INTERFACE
, 0,
2079 jacks
[i
].unitid
<< 8, buf
, 3, 100);
2080 if (err
== 3 && (buf
[0] == 3 || buf
[0] == 6))
2081 snd_iprintf(buffer
, "%02x %02x\n", buf
[1], buf
[2]);
2083 snd_iprintf(buffer
, "?\n");
2087 static int snd_xonar_u1_switch_get(struct snd_kcontrol
*kcontrol
,
2088 struct snd_ctl_elem_value
*ucontrol
)
2090 struct usb_mixer_interface
*mixer
= snd_kcontrol_chip(kcontrol
);
2092 ucontrol
->value
.integer
.value
[0] = !!(mixer
->xonar_u1_status
& 0x02);
2096 static int snd_xonar_u1_switch_put(struct snd_kcontrol
*kcontrol
,
2097 struct snd_ctl_elem_value
*ucontrol
)
2099 struct usb_mixer_interface
*mixer
= snd_kcontrol_chip(kcontrol
);
2100 u8 old_status
, new_status
;
2103 old_status
= mixer
->xonar_u1_status
;
2104 if (ucontrol
->value
.integer
.value
[0])
2105 new_status
= old_status
| 0x02;
2107 new_status
= old_status
& ~0x02;
2108 changed
= new_status
!= old_status
;
2109 err
= snd_usb_ctl_msg(mixer
->chip
->dev
,
2110 usb_sndctrlpipe(mixer
->chip
->dev
, 0), 0x08,
2111 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_OTHER
,
2112 50, 0, &new_status
, 1, 100);
2115 mixer
->xonar_u1_status
= new_status
;
2119 static struct snd_kcontrol_new snd_xonar_u1_output_switch
= {
2120 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2121 .name
= "Digital Playback Switch",
2122 .info
= snd_ctl_boolean_mono_info
,
2123 .get
= snd_xonar_u1_switch_get
,
2124 .put
= snd_xonar_u1_switch_put
,
2127 static int snd_xonar_u1_controls_create(struct usb_mixer_interface
*mixer
)
2131 err
= snd_ctl_add(mixer
->chip
->card
,
2132 snd_ctl_new1(&snd_xonar_u1_output_switch
, mixer
));
2135 mixer
->xonar_u1_status
= 0x05;
2139 int snd_usb_create_mixer(struct snd_usb_audio
*chip
, int ctrlif
,
2142 static struct snd_device_ops dev_ops
= {
2143 .dev_free
= snd_usb_mixer_dev_free
2145 struct usb_mixer_interface
*mixer
;
2148 strcpy(chip
->card
->mixername
, "USB Mixer");
2150 mixer
= kzalloc(sizeof(*mixer
), GFP_KERNEL
);
2154 mixer
->ctrlif
= ctrlif
;
2155 mixer
->ignore_ctl_error
= ignore_error
;
2156 mixer
->id_elems
= kcalloc(256, sizeof(*mixer
->id_elems
), GFP_KERNEL
);
2157 if (!mixer
->id_elems
) {
2162 if ((err
= snd_usb_mixer_controls(mixer
)) < 0 ||
2163 (err
= snd_usb_mixer_status_create(mixer
)) < 0)
2166 if ((err
= snd_usb_soundblaster_remote_init(mixer
)) < 0)
2169 if (mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3020) ||
2170 mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3040) ||
2171 mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3048)) {
2172 struct snd_info_entry
*entry
;
2174 if ((err
= snd_audigy2nx_controls_create(mixer
)) < 0)
2176 if (!snd_card_proc_new(chip
->card
, "audigy2nx", &entry
))
2177 snd_info_set_text_ops(entry
, mixer
,
2178 snd_audigy2nx_proc_read
);
2181 if (mixer
->chip
->usb_id
== USB_ID(0x0b05, 0x1739) ||
2182 mixer
->chip
->usb_id
== USB_ID(0x0b05, 0x1743)) {
2183 err
= snd_xonar_u1_controls_create(mixer
);
2188 err
= snd_device_new(chip
->card
, SNDRV_DEV_LOWLEVEL
, mixer
, &dev_ops
);
2191 list_add(&mixer
->list
, &chip
->mixer_list
);
2195 snd_usb_mixer_free(mixer
);
2199 void snd_usb_mixer_disconnect(struct list_head
*p
)
2201 struct usb_mixer_interface
*mixer
;
2203 mixer
= list_entry(p
, struct usb_mixer_interface
, list
);
2204 usb_kill_urb(mixer
->urb
);
2205 usb_kill_urb(mixer
->rc_urb
);