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
{
65 { USB_ID(0x041e, 0x3000), 0, 1, 2, 18, 0x0013 }, /* Extigy */
66 { USB_ID(0x041e, 0x3020), 2, 1, 6, 18, 0x0013 }, /* Audigy 2 NX */
67 { USB_ID(0x041e, 0x3040), 2, 2, 6, 2, 0x6e91 }, /* Live! 24-bit */
70 struct usb_mixer_interface
{
71 struct snd_usb_audio
*chip
;
73 struct list_head list
;
74 unsigned int ignore_ctl_error
;
76 struct usb_mixer_elem_info
**id_elems
; /* array[256], indexed by unit id */
78 /* Sound Blaster remote control stuff */
79 const struct rc_config
*rc_cfg
;
80 unsigned long rc_hwdep_open
;
82 wait_queue_head_t rc_waitq
;
84 struct usb_ctrlrequest
*rc_setup_packet
;
91 struct usb_audio_term
{
95 unsigned int chconfig
;
99 struct usbmix_name_map
;
102 struct snd_usb_audio
*chip
;
103 struct usb_mixer_interface
*mixer
;
104 unsigned char *buffer
;
106 DECLARE_BITMAP(unitbitmap
, 256);
107 struct usb_audio_term oterm
;
108 const struct usbmix_name_map
*map
;
109 const struct usbmix_selector_map
*selector_map
;
112 struct usb_mixer_elem_info
{
113 struct usb_mixer_interface
*mixer
;
114 struct usb_mixer_elem_info
*next_id_elem
; /* list of controls with same id */
115 struct snd_ctl_elem_id
*elem_id
;
117 unsigned int control
; /* CS or ICN (high byte) */
118 unsigned int cmask
; /* channel mask bitmap: 0 = master */
127 USB_FEATURE_NONE
= 0,
128 USB_FEATURE_MUTE
= 1,
136 USB_FEATURE_BASSBOOST
,
142 USB_MIXER_INV_BOOLEAN
,
151 USB_PROC_UPDOWN_SWITCH
= 1,
152 USB_PROC_UPDOWN_MODE_SEL
= 2,
154 USB_PROC_PROLOGIC
= 2,
155 USB_PROC_PROLOGIC_SWITCH
= 1,
156 USB_PROC_PROLOGIC_MODE_SEL
= 2,
159 USB_PROC_3DENH_SWITCH
= 1,
160 USB_PROC_3DENH_SPACE
= 2,
163 USB_PROC_REVERB_SWITCH
= 1,
164 USB_PROC_REVERB_LEVEL
= 2,
165 USB_PROC_REVERB_TIME
= 3,
166 USB_PROC_REVERB_DELAY
= 4,
169 USB_PROC_CHORUS_SWITCH
= 1,
170 USB_PROC_CHORUS_LEVEL
= 2,
171 USB_PROC_CHORUS_RATE
= 3,
172 USB_PROC_CHORUS_DEPTH
= 4,
175 USB_PROC_DCR_SWITCH
= 1,
176 USB_PROC_DCR_RATIO
= 2,
177 USB_PROC_DCR_MAX_AMP
= 3,
178 USB_PROC_DCR_THRESHOLD
= 4,
179 USB_PROC_DCR_ATTACK
= 5,
180 USB_PROC_DCR_RELEASE
= 6,
183 #define MAX_CHANNELS 10 /* max logical channels */
187 * manual mapping of mixer names
188 * if the mixer topology is too complicated and the parsed names are
189 * ambiguous, add the entries in usbmixer_maps.c.
191 #include "usbmixer_maps.c"
193 /* get the mapped name if the unit matches */
194 static int check_mapped_name(struct mixer_build
*state
, int unitid
, int control
, char *buf
, int buflen
)
196 const struct usbmix_name_map
*p
;
201 for (p
= state
->map
; p
->id
; p
++) {
202 if (p
->id
== unitid
&& p
->name
&&
203 (! control
|| ! p
->control
|| control
== p
->control
)) {
205 return strlcpy(buf
, p
->name
, buflen
);
211 /* check whether the control should be ignored */
212 static int check_ignored_ctl(struct mixer_build
*state
, int unitid
, int control
)
214 const struct usbmix_name_map
*p
;
218 for (p
= state
->map
; p
->id
; p
++) {
219 if (p
->id
== unitid
&& ! p
->name
&&
220 (! control
|| ! p
->control
|| control
== p
->control
)) {
221 // printk("ignored control %d:%d\n", unitid, control);
228 /* get the mapped selector source name */
229 static int check_mapped_selector_name(struct mixer_build
*state
, int unitid
,
230 int index
, char *buf
, int buflen
)
232 const struct usbmix_selector_map
*p
;
234 if (! state
->selector_map
)
236 for (p
= state
->selector_map
; p
->id
; p
++) {
237 if (p
->id
== unitid
&& index
< p
->count
)
238 return strlcpy(buf
, p
->names
[index
], buflen
);
244 * find an audio control unit with the given unit id
246 static void *find_audio_control_unit(struct mixer_build
*state
, unsigned char unit
)
251 while ((p
= snd_usb_find_desc(state
->buffer
, state
->buflen
, p
,
252 USB_DT_CS_INTERFACE
)) != NULL
) {
253 if (p
[0] >= 4 && p
[2] >= INPUT_TERMINAL
&& p
[2] <= EXTENSION_UNIT
&& p
[3] == unit
)
261 * copy a string with the given id
263 static int snd_usb_copy_string_desc(struct mixer_build
*state
, int index
, char *buf
, int maxlen
)
265 int len
= usb_string(state
->chip
->dev
, index
, buf
, maxlen
- 1);
271 * convert from the byte/word on usb descriptor to the zero-based integer
273 static int convert_signed_value(struct usb_mixer_elem_info
*cval
, int val
)
275 switch (cval
->val_type
) {
276 case USB_MIXER_BOOLEAN
:
278 case USB_MIXER_INV_BOOLEAN
:
301 * convert from the zero-based int to the byte/word for usb descriptor
303 static int convert_bytes_value(struct usb_mixer_elem_info
*cval
, int val
)
305 switch (cval
->val_type
) {
306 case USB_MIXER_BOOLEAN
:
308 case USB_MIXER_INV_BOOLEAN
:
317 return 0; /* not reached */
320 static int get_relative_value(struct usb_mixer_elem_info
*cval
, int val
)
326 else if (val
>= cval
->max
)
327 return (cval
->max
- cval
->min
+ cval
->res
- 1) / cval
->res
;
329 return (val
- cval
->min
) / cval
->res
;
332 static int get_abs_value(struct usb_mixer_elem_info
*cval
, int val
)
347 * retrieve a mixer value
350 static int get_ctl_value(struct usb_mixer_elem_info
*cval
, int request
, int validx
, int *value_ret
)
352 unsigned char buf
[2];
353 int val_len
= cval
->val_type
>= USB_MIXER_S16
? 2 : 1;
356 while (timeout
-- > 0) {
357 if (snd_usb_ctl_msg(cval
->mixer
->chip
->dev
,
358 usb_rcvctrlpipe(cval
->mixer
->chip
->dev
, 0),
360 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
361 validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8),
362 buf
, val_len
, 100) >= val_len
) {
363 *value_ret
= convert_signed_value(cval
, snd_usb_combine_bytes(buf
, val_len
));
367 snd_printdd(KERN_ERR
"cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
368 request
, validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8), cval
->val_type
);
372 static int get_cur_ctl_value(struct usb_mixer_elem_info
*cval
, int validx
, int *value
)
374 return get_ctl_value(cval
, GET_CUR
, validx
, value
);
377 /* channel = 0: master, 1 = first channel */
378 static inline int get_cur_mix_value(struct usb_mixer_elem_info
*cval
, int channel
, int *value
)
380 return get_ctl_value(cval
, GET_CUR
, (cval
->control
<< 8) | channel
, value
);
387 static int set_ctl_value(struct usb_mixer_elem_info
*cval
, int request
, int validx
, int value_set
)
389 unsigned char buf
[2];
390 int val_len
= cval
->val_type
>= USB_MIXER_S16
? 2 : 1;
393 value_set
= convert_bytes_value(cval
, value_set
);
394 buf
[0] = value_set
& 0xff;
395 buf
[1] = (value_set
>> 8) & 0xff;
396 while (timeout
-- > 0)
397 if (snd_usb_ctl_msg(cval
->mixer
->chip
->dev
,
398 usb_sndctrlpipe(cval
->mixer
->chip
->dev
, 0),
400 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
401 validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8),
402 buf
, val_len
, 100) >= 0)
404 snd_printdd(KERN_ERR
"cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
405 request
, validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8), cval
->val_type
, buf
[0], buf
[1]);
409 static int set_cur_ctl_value(struct usb_mixer_elem_info
*cval
, int validx
, int value
)
411 return set_ctl_value(cval
, SET_CUR
, validx
, value
);
414 static inline int set_cur_mix_value(struct usb_mixer_elem_info
*cval
, int channel
, int value
)
416 return set_ctl_value(cval
, SET_CUR
, (cval
->control
<< 8) | channel
, value
);
420 * TLV callback for mixer volume controls
422 static int mixer_vol_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
423 unsigned int size
, unsigned int __user
*_tlv
)
425 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
426 DECLARE_TLV_DB_SCALE(scale
, 0, 0, 0);
428 if (size
< sizeof(scale
))
430 /* USB descriptions contain the dB scale in 1/256 dB unit
431 * while ALSA TLV contains in 1/100 dB unit
433 scale
[2] = (convert_signed_value(cval
, cval
->min
) * 100) / 256;
434 scale
[3] = (convert_signed_value(cval
, cval
->res
) * 100) / 256;
435 if (copy_to_user(_tlv
, scale
, sizeof(scale
)))
441 * parser routines begin here...
444 static int parse_audio_unit(struct mixer_build
*state
, int unitid
);
448 * check if the input/output channel routing is enabled on the given bitmap.
449 * used for mixer unit parser
451 static int check_matrix_bitmap(unsigned char *bmap
, int ich
, int och
, int num_outs
)
453 int idx
= ich
* num_outs
+ och
;
454 return bmap
[idx
>> 3] & (0x80 >> (idx
& 7));
459 * add an alsa control element
460 * search and increment the index until an empty slot is found.
462 * if failed, give up and free the control instance.
465 static int add_control_to_empty(struct mixer_build
*state
, struct snd_kcontrol
*kctl
)
467 struct usb_mixer_elem_info
*cval
= kctl
->private_data
;
470 while (snd_ctl_find_id(state
->chip
->card
, &kctl
->id
))
472 if ((err
= snd_ctl_add(state
->chip
->card
, kctl
)) < 0) {
473 snd_printd(KERN_ERR
"cannot add control (err = %d)\n", err
);
476 cval
->elem_id
= &kctl
->id
;
477 cval
->next_id_elem
= state
->mixer
->id_elems
[cval
->id
];
478 state
->mixer
->id_elems
[cval
->id
] = cval
;
484 * get a terminal name string
487 static struct iterm_name_combo
{
491 { 0x0300, "Output" },
492 { 0x0301, "Speaker" },
493 { 0x0302, "Headphone" },
494 { 0x0303, "HMD Audio" },
495 { 0x0304, "Desktop Speaker" },
496 { 0x0305, "Room Speaker" },
497 { 0x0306, "Com Speaker" },
499 { 0x0600, "External In" },
500 { 0x0601, "Analog In" },
501 { 0x0602, "Digital In" },
503 { 0x0604, "Legacy In" },
504 { 0x0605, "IEC958 In" },
505 { 0x0606, "1394 DA Stream" },
506 { 0x0607, "1394 DV Stream" },
507 { 0x0700, "Embedded" },
508 { 0x0701, "Noise Source" },
509 { 0x0702, "Equalization Noise" },
513 { 0x0706, "MiniDisk" },
514 { 0x0707, "Analog Tape" },
515 { 0x0708, "Phonograph" },
516 { 0x0709, "VCR Audio" },
517 { 0x070a, "Video Disk Audio" },
518 { 0x070b, "DVD Audio" },
519 { 0x070c, "TV Tuner Audio" },
520 { 0x070d, "Satellite Rec Audio" },
521 { 0x070e, "Cable Tuner Audio" },
522 { 0x070f, "DSS Audio" },
523 { 0x0710, "Radio Receiver" },
524 { 0x0711, "Radio Transmitter" },
525 { 0x0712, "Multi-Track Recorder" },
526 { 0x0713, "Synthesizer" },
530 static int get_term_name(struct mixer_build
*state
, struct usb_audio_term
*iterm
,
531 unsigned char *name
, int maxlen
, int term_only
)
533 struct iterm_name_combo
*names
;
536 return snd_usb_copy_string_desc(state
, iterm
->name
, name
, maxlen
);
538 /* virtual type - not a real terminal */
539 if (iterm
->type
>> 16) {
542 switch (iterm
->type
>> 16) {
544 strcpy(name
, "Selector"); return 8;
545 case PROCESSING_UNIT
:
546 strcpy(name
, "Process Unit"); return 12;
548 strcpy(name
, "Ext Unit"); return 8;
550 strcpy(name
, "Mixer"); return 5;
552 return sprintf(name
, "Unit %d", iterm
->id
);
556 switch (iterm
->type
& 0xff00) {
558 strcpy(name
, "PCM"); return 3;
560 strcpy(name
, "Mic"); return 3;
562 strcpy(name
, "Headset"); return 7;
564 strcpy(name
, "Phone"); return 5;
567 for (names
= iterm_names
; names
->type
; names
++)
568 if (names
->type
== iterm
->type
) {
569 strcpy(name
, names
->name
);
570 return strlen(names
->name
);
577 * parse the source unit recursively until it reaches to a terminal
578 * or a branched unit.
580 static int check_input_term(struct mixer_build
*state
, int id
, struct usb_audio_term
*term
)
584 memset(term
, 0, sizeof(*term
));
585 while ((p1
= find_audio_control_unit(state
, id
)) != NULL
) {
589 term
->type
= combine_word(p1
+ 4);
590 term
->channels
= p1
[7];
591 term
->chconfig
= combine_word(p1
+ 8);
596 break; /* continue to parse */
598 term
->type
= p1
[2] << 16; /* virtual type */
599 term
->channels
= p1
[5 + p1
[4]];
600 term
->chconfig
= combine_word(p1
+ 6 + p1
[4]);
601 term
->name
= p1
[p1
[0] - 1];
604 /* call recursively to retrieve the channel info */
605 if (check_input_term(state
, p1
[5], term
) < 0)
607 term
->type
= p1
[2] << 16; /* virtual type */
609 term
->name
= p1
[9 + p1
[0] - 1];
611 case PROCESSING_UNIT
:
615 break; /* continue to parse */
617 term
->type
= p1
[2] << 16; /* virtual type */
618 term
->channels
= p1
[7 + p1
[6]];
619 term
->chconfig
= combine_word(p1
+ 8 + p1
[6]);
620 term
->name
= p1
[12 + p1
[6] + p1
[11 + p1
[6]]];
634 /* feature unit control information */
635 struct usb_feature_control_info
{
637 unsigned int type
; /* control type (mute, volume, etc.) */
640 static struct usb_feature_control_info audio_feature_info
[] = {
641 { "Mute", USB_MIXER_INV_BOOLEAN
},
642 { "Volume", USB_MIXER_S16
},
643 { "Tone Control - Bass", USB_MIXER_S8
},
644 { "Tone Control - Mid", USB_MIXER_S8
},
645 { "Tone Control - Treble", USB_MIXER_S8
},
646 { "Graphic Equalizer", USB_MIXER_S8
}, /* FIXME: not implemeted yet */
647 { "Auto Gain Control", USB_MIXER_BOOLEAN
},
648 { "Delay Control", USB_MIXER_U16
},
649 { "Bass Boost", USB_MIXER_BOOLEAN
},
650 { "Loudness", USB_MIXER_BOOLEAN
},
654 /* private_free callback */
655 static void usb_mixer_elem_free(struct snd_kcontrol
*kctl
)
657 kfree(kctl
->private_data
);
658 kctl
->private_data
= NULL
;
663 * interface to ALSA control for feature/mixer units
667 * retrieve the minimum and maximum values for the specified control
669 static int get_min_max(struct usb_mixer_elem_info
*cval
, int default_min
)
672 cval
->min
= default_min
;
673 cval
->max
= cval
->min
+ 1;
676 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
677 cval
->val_type
== USB_MIXER_INV_BOOLEAN
) {
678 cval
->initialized
= 1;
683 for (i
= 0; i
< MAX_CHANNELS
; i
++)
684 if (cval
->cmask
& (1 << i
)) {
689 if (get_ctl_value(cval
, GET_MAX
, (cval
->control
<< 8) | minchn
, &cval
->max
) < 0 ||
690 get_ctl_value(cval
, GET_MIN
, (cval
->control
<< 8) | minchn
, &cval
->min
) < 0) {
691 snd_printd(KERN_ERR
"%d:%d: cannot get min/max values for control %d (id %d)\n",
692 cval
->id
, cval
->mixer
->ctrlif
, cval
->control
, cval
->id
);
695 if (get_ctl_value(cval
, GET_RES
, (cval
->control
<< 8) | minchn
, &cval
->res
) < 0) {
698 int last_valid_res
= cval
->res
;
700 while (cval
->res
> 1) {
701 if (set_ctl_value(cval
, SET_RES
, (cval
->control
<< 8) | minchn
, cval
->res
/ 2) < 0)
705 if (get_ctl_value(cval
, GET_RES
, (cval
->control
<< 8) | minchn
, &cval
->res
) < 0)
706 cval
->res
= last_valid_res
;
711 /* Additional checks for the proper resolution
713 * Some devices report smaller resolutions than actually
714 * reacting. They don't return errors but simply clip
715 * to the lower aligned value.
717 if (cval
->min
+ cval
->res
< cval
->max
) {
718 int last_valid_res
= cval
->res
;
719 int saved
, test
, check
;
720 get_cur_mix_value(cval
, minchn
, &saved
);
723 if (test
< cval
->max
)
727 if (test
< cval
->min
|| test
> cval
->max
||
728 set_cur_mix_value(cval
, minchn
, test
) ||
729 get_cur_mix_value(cval
, minchn
, &check
)) {
730 cval
->res
= last_valid_res
;
737 set_cur_mix_value(cval
, minchn
, saved
);
740 cval
->initialized
= 1;
746 /* get a feature/mixer unit info */
747 static int mixer_ctl_feature_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
749 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
751 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
752 cval
->val_type
== USB_MIXER_INV_BOOLEAN
)
753 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
755 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
756 uinfo
->count
= cval
->channels
;
757 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
758 cval
->val_type
== USB_MIXER_INV_BOOLEAN
) {
759 uinfo
->value
.integer
.min
= 0;
760 uinfo
->value
.integer
.max
= 1;
762 if (! cval
->initialized
)
763 get_min_max(cval
, 0);
764 uinfo
->value
.integer
.min
= 0;
765 uinfo
->value
.integer
.max
=
766 (cval
->max
- cval
->min
+ cval
->res
- 1) / cval
->res
;
771 /* get the current value from feature/mixer unit */
772 static int mixer_ctl_feature_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
774 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
775 int c
, cnt
, val
, err
;
779 for (c
= 0; c
< MAX_CHANNELS
; c
++) {
780 if (cval
->cmask
& (1 << c
)) {
781 err
= get_cur_mix_value(cval
, c
+ 1, &val
);
783 if (cval
->mixer
->ignore_ctl_error
) {
784 ucontrol
->value
.integer
.value
[0] = cval
->min
;
787 snd_printd(KERN_ERR
"cannot get current value for control %d ch %d: err = %d\n", cval
->control
, c
+ 1, err
);
790 val
= get_relative_value(cval
, val
);
791 ucontrol
->value
.integer
.value
[cnt
] = val
;
797 err
= get_cur_mix_value(cval
, 0, &val
);
799 if (cval
->mixer
->ignore_ctl_error
) {
800 ucontrol
->value
.integer
.value
[0] = cval
->min
;
803 snd_printd(KERN_ERR
"cannot get current value for control %d master ch: err = %d\n", cval
->control
, err
);
806 val
= get_relative_value(cval
, val
);
807 ucontrol
->value
.integer
.value
[0] = val
;
812 /* put the current value to feature/mixer unit */
813 static int mixer_ctl_feature_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
815 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
816 int c
, cnt
, val
, oval
, err
;
821 for (c
= 0; c
< MAX_CHANNELS
; c
++) {
822 if (cval
->cmask
& (1 << c
)) {
823 err
= get_cur_mix_value(cval
, c
+ 1, &oval
);
825 if (cval
->mixer
->ignore_ctl_error
)
829 val
= ucontrol
->value
.integer
.value
[cnt
];
830 val
= get_abs_value(cval
, val
);
832 set_cur_mix_value(cval
, c
+ 1, val
);
835 get_cur_mix_value(cval
, c
+ 1, &val
);
841 err
= get_cur_mix_value(cval
, 0, &oval
);
842 if (err
< 0 && cval
->mixer
->ignore_ctl_error
)
846 val
= ucontrol
->value
.integer
.value
[0];
847 val
= get_abs_value(cval
, val
);
849 set_cur_mix_value(cval
, 0, val
);
856 static struct snd_kcontrol_new usb_feature_unit_ctl
= {
857 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
858 .name
= "", /* will be filled later manually */
859 .info
= mixer_ctl_feature_info
,
860 .get
= mixer_ctl_feature_get
,
861 .put
= mixer_ctl_feature_put
,
866 * build a feature control
869 static void build_feature_ctl(struct mixer_build
*state
, unsigned char *desc
,
870 unsigned int ctl_mask
, int control
,
871 struct usb_audio_term
*iterm
, int unitid
)
873 unsigned int len
= 0;
875 int nameid
= desc
[desc
[0] - 1];
876 struct snd_kcontrol
*kctl
;
877 struct usb_mixer_elem_info
*cval
;
879 control
++; /* change from zero-based to 1-based value */
881 if (control
== USB_FEATURE_GEQ
) {
882 /* FIXME: not supported yet */
886 if (check_ignored_ctl(state
, unitid
, control
))
889 cval
= kzalloc(sizeof(*cval
), GFP_KERNEL
);
891 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
894 cval
->mixer
= state
->mixer
;
896 cval
->control
= control
;
897 cval
->cmask
= ctl_mask
;
898 cval
->val_type
= audio_feature_info
[control
-1].type
;
900 cval
->channels
= 1; /* master channel */
903 for (i
= 0; i
< 16; i
++)
904 if (ctl_mask
& (1 << i
))
909 /* get min/max values */
910 get_min_max(cval
, 0);
912 kctl
= snd_ctl_new1(&usb_feature_unit_ctl
, cval
);
914 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
918 kctl
->private_free
= usb_mixer_elem_free
;
920 len
= check_mapped_name(state
, unitid
, control
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
921 mapped_name
= len
!= 0;
923 len
= snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
926 case USB_FEATURE_MUTE
:
927 case USB_FEATURE_VOLUME
:
928 /* determine the control name. the rule is:
929 * - if a name id is given in descriptor, use it.
930 * - if the connected input can be determined, then use the name
932 * - if the connected output can be determined, use it.
933 * - otherwise, anonymous name.
936 len
= get_term_name(state
, iterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 1);
938 len
= get_term_name(state
, &state
->oterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 1);
940 len
= snprintf(kctl
->id
.name
, sizeof(kctl
->id
.name
),
941 "Feature %d", unitid
);
943 /* determine the stream direction:
944 * if the connected output is USB stream, then it's likely a
945 * capture stream. otherwise it should be playback (hopefully :)
947 if (! mapped_name
&& ! (state
->oterm
.type
>> 16)) {
948 if ((state
->oterm
.type
& 0xff00) == 0x0100) {
949 len
= strlcat(kctl
->id
.name
, " Capture", sizeof(kctl
->id
.name
));
951 len
= strlcat(kctl
->id
.name
+ len
, " Playback", sizeof(kctl
->id
.name
));
954 strlcat(kctl
->id
.name
+ len
, control
== USB_FEATURE_MUTE
? " Switch" : " Volume",
955 sizeof(kctl
->id
.name
));
956 if (control
== USB_FEATURE_VOLUME
) {
957 kctl
->tlv
.c
= mixer_vol_tlv
;
958 kctl
->vd
[0].access
|=
959 SNDRV_CTL_ELEM_ACCESS_TLV_READ
|
960 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
;
966 strlcpy(kctl
->id
.name
, audio_feature_info
[control
-1].name
,
967 sizeof(kctl
->id
.name
));
971 /* quirk for UDA1321/N101 */
972 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
973 /* is not very clear from datasheets */
974 /* I hope that the min value is -15360 for newer firmware --jk */
975 switch (state
->chip
->usb_id
) {
976 case USB_ID(0x0471, 0x0101):
977 case USB_ID(0x0471, 0x0104):
978 case USB_ID(0x0471, 0x0105):
979 case USB_ID(0x0672, 0x1041):
980 if (!strcmp(kctl
->id
.name
, "PCM Playback Volume") &&
981 cval
->min
== -15616) {
982 snd_printk(KERN_INFO
"using volume control quirk for the UDA1321/N101 chip\n");
987 snd_printdd(KERN_INFO
"[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
988 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
, cval
->res
);
989 add_control_to_empty(state
, kctl
);
995 * parse a feature unit
997 * most of controlls are defined here.
999 static int parse_audio_feature_unit(struct mixer_build
*state
, int unitid
, unsigned char *ftr
)
1002 struct usb_audio_term iterm
;
1003 unsigned int master_bits
, first_ch_bits
;
1006 if (ftr
[0] < 7 || ! (csize
= ftr
[5]) || ftr
[0] < 7 + csize
) {
1007 snd_printk(KERN_ERR
"usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid
);
1011 /* parse the source unit */
1012 if ((err
= parse_audio_unit(state
, ftr
[4])) < 0)
1015 /* determine the input source type and name */
1016 if (check_input_term(state
, ftr
[4], &iterm
) < 0)
1019 channels
= (ftr
[0] - 7) / csize
- 1;
1021 master_bits
= snd_usb_combine_bytes(ftr
+ 6, csize
);
1023 first_ch_bits
= snd_usb_combine_bytes(ftr
+ 6 + csize
, csize
);
1026 /* check all control types */
1027 for (i
= 0; i
< 10; i
++) {
1028 unsigned int ch_bits
= 0;
1029 for (j
= 0; j
< channels
; j
++) {
1030 unsigned int mask
= snd_usb_combine_bytes(ftr
+ 6 + csize
* (j
+1), csize
);
1031 if (mask
& (1 << i
))
1032 ch_bits
|= (1 << j
);
1034 if (ch_bits
& 1) /* the first channel must be set (for ease of programming) */
1035 build_feature_ctl(state
, ftr
, ch_bits
, i
, &iterm
, unitid
);
1036 if (master_bits
& (1 << i
))
1037 build_feature_ctl(state
, ftr
, 0, i
, &iterm
, unitid
);
1049 * build a mixer unit control
1051 * the callbacks are identical with feature unit.
1052 * input channel number (zero based) is given in control field instead.
1055 static void build_mixer_unit_ctl(struct mixer_build
*state
, unsigned char *desc
,
1056 int in_pin
, int in_ch
, int unitid
,
1057 struct usb_audio_term
*iterm
)
1059 struct usb_mixer_elem_info
*cval
;
1060 unsigned int input_pins
= desc
[4];
1061 unsigned int num_outs
= desc
[5 + input_pins
];
1062 unsigned int i
, len
;
1063 struct snd_kcontrol
*kctl
;
1065 if (check_ignored_ctl(state
, unitid
, 0))
1068 cval
= kzalloc(sizeof(*cval
), GFP_KERNEL
);
1072 cval
->mixer
= state
->mixer
;
1074 cval
->control
= in_ch
+ 1; /* based on 1 */
1075 cval
->val_type
= USB_MIXER_S16
;
1076 for (i
= 0; i
< num_outs
; i
++) {
1077 if (check_matrix_bitmap(desc
+ 9 + input_pins
, in_ch
, i
, num_outs
)) {
1078 cval
->cmask
|= (1 << i
);
1083 /* get min/max values */
1084 get_min_max(cval
, 0);
1086 kctl
= snd_ctl_new1(&usb_feature_unit_ctl
, cval
);
1088 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1092 kctl
->private_free
= usb_mixer_elem_free
;
1094 len
= check_mapped_name(state
, unitid
, 0, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1096 len
= get_term_name(state
, iterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 0);
1098 len
= sprintf(kctl
->id
.name
, "Mixer Source %d", in_ch
+ 1);
1099 strlcat(kctl
->id
.name
+ len
, " Volume", sizeof(kctl
->id
.name
));
1101 snd_printdd(KERN_INFO
"[%d] MU [%s] ch = %d, val = %d/%d\n",
1102 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
);
1103 add_control_to_empty(state
, kctl
);
1108 * parse a mixer unit
1110 static int parse_audio_mixer_unit(struct mixer_build
*state
, int unitid
, unsigned char *desc
)
1112 struct usb_audio_term iterm
;
1113 int input_pins
, num_ins
, num_outs
;
1116 if (desc
[0] < 11 || ! (input_pins
= desc
[4]) || ! (num_outs
= desc
[5 + input_pins
])) {
1117 snd_printk(KERN_ERR
"invalid MIXER UNIT descriptor %d\n", unitid
);
1120 /* no bmControls field (e.g. Maya44) -> ignore */
1121 if (desc
[0] <= 10 + input_pins
) {
1122 snd_printdd(KERN_INFO
"MU %d has no bmControls field\n", unitid
);
1128 for (pin
= 0; pin
< input_pins
; pin
++) {
1129 err
= parse_audio_unit(state
, desc
[5 + pin
]);
1132 err
= check_input_term(state
, desc
[5 + pin
], &iterm
);
1135 num_ins
+= iterm
.channels
;
1136 for (; ich
< num_ins
; ++ich
) {
1137 int och
, ich_has_controls
= 0;
1139 for (och
= 0; och
< num_outs
; ++och
) {
1140 if (check_matrix_bitmap(desc
+ 9 + input_pins
,
1141 ich
, och
, num_outs
)) {
1142 ich_has_controls
= 1;
1146 if (ich_has_controls
)
1147 build_mixer_unit_ctl(state
, desc
, pin
, ich
,
1156 * Processing Unit / Extension Unit
1159 /* get callback for processing/extension unit */
1160 static int mixer_ctl_procunit_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1162 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1165 err
= get_cur_ctl_value(cval
, cval
->control
<< 8, &val
);
1166 if (err
< 0 && cval
->mixer
->ignore_ctl_error
) {
1167 ucontrol
->value
.integer
.value
[0] = cval
->min
;
1172 val
= get_relative_value(cval
, val
);
1173 ucontrol
->value
.integer
.value
[0] = val
;
1177 /* put callback for processing/extension unit */
1178 static int mixer_ctl_procunit_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1180 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1183 err
= get_cur_ctl_value(cval
, cval
->control
<< 8, &oval
);
1185 if (cval
->mixer
->ignore_ctl_error
)
1189 val
= ucontrol
->value
.integer
.value
[0];
1190 val
= get_abs_value(cval
, val
);
1192 set_cur_ctl_value(cval
, cval
->control
<< 8, val
);
1198 /* alsa control interface for processing/extension unit */
1199 static struct snd_kcontrol_new mixer_procunit_ctl
= {
1200 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1201 .name
= "", /* will be filled later */
1202 .info
= mixer_ctl_feature_info
,
1203 .get
= mixer_ctl_procunit_get
,
1204 .put
= mixer_ctl_procunit_put
,
1209 * predefined data for processing units
1211 struct procunit_value_info
{
1218 struct procunit_info
{
1221 struct procunit_value_info
*values
;
1224 static struct procunit_value_info updown_proc_info
[] = {
1225 { USB_PROC_UPDOWN_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1226 { USB_PROC_UPDOWN_MODE_SEL
, "Mode Select", USB_MIXER_U8
, 1 },
1229 static struct procunit_value_info prologic_proc_info
[] = {
1230 { USB_PROC_PROLOGIC_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1231 { USB_PROC_PROLOGIC_MODE_SEL
, "Mode Select", USB_MIXER_U8
, 1 },
1234 static struct procunit_value_info threed_enh_proc_info
[] = {
1235 { USB_PROC_3DENH_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1236 { USB_PROC_3DENH_SPACE
, "Spaciousness", USB_MIXER_U8
},
1239 static struct procunit_value_info reverb_proc_info
[] = {
1240 { USB_PROC_REVERB_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1241 { USB_PROC_REVERB_LEVEL
, "Level", USB_MIXER_U8
},
1242 { USB_PROC_REVERB_TIME
, "Time", USB_MIXER_U16
},
1243 { USB_PROC_REVERB_DELAY
, "Delay", USB_MIXER_U8
},
1246 static struct procunit_value_info chorus_proc_info
[] = {
1247 { USB_PROC_CHORUS_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1248 { USB_PROC_CHORUS_LEVEL
, "Level", USB_MIXER_U8
},
1249 { USB_PROC_CHORUS_RATE
, "Rate", USB_MIXER_U16
},
1250 { USB_PROC_CHORUS_DEPTH
, "Depth", USB_MIXER_U16
},
1253 static struct procunit_value_info dcr_proc_info
[] = {
1254 { USB_PROC_DCR_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1255 { USB_PROC_DCR_RATIO
, "Ratio", USB_MIXER_U16
},
1256 { USB_PROC_DCR_MAX_AMP
, "Max Amp", USB_MIXER_S16
},
1257 { USB_PROC_DCR_THRESHOLD
, "Threshold", USB_MIXER_S16
},
1258 { USB_PROC_DCR_ATTACK
, "Attack Time", USB_MIXER_U16
},
1259 { USB_PROC_DCR_RELEASE
, "Release Time", USB_MIXER_U16
},
1263 static struct procunit_info procunits
[] = {
1264 { USB_PROC_UPDOWN
, "Up Down", updown_proc_info
},
1265 { USB_PROC_PROLOGIC
, "Dolby Prologic", prologic_proc_info
},
1266 { USB_PROC_3DENH
, "3D Stereo Extender", threed_enh_proc_info
},
1267 { USB_PROC_REVERB
, "Reverb", reverb_proc_info
},
1268 { USB_PROC_CHORUS
, "Chorus", chorus_proc_info
},
1269 { USB_PROC_DCR
, "DCR", dcr_proc_info
},
1274 * build a processing/extension unit
1276 static int build_audio_procunit(struct mixer_build
*state
, int unitid
, unsigned char *dsc
, struct procunit_info
*list
, char *name
)
1278 int num_ins
= dsc
[6];
1279 struct usb_mixer_elem_info
*cval
;
1280 struct snd_kcontrol
*kctl
;
1281 int i
, err
, nameid
, type
, len
;
1282 struct procunit_info
*info
;
1283 struct procunit_value_info
*valinfo
;
1284 static struct procunit_value_info default_value_info
[] = {
1285 { 0x01, "Switch", USB_MIXER_BOOLEAN
},
1288 static struct procunit_info default_info
= {
1289 0, NULL
, default_value_info
1292 if (dsc
[0] < 13 || dsc
[0] < 13 + num_ins
|| dsc
[0] < num_ins
+ dsc
[11 + num_ins
]) {
1293 snd_printk(KERN_ERR
"invalid %s descriptor (id %d)\n", name
, unitid
);
1297 for (i
= 0; i
< num_ins
; i
++) {
1298 if ((err
= parse_audio_unit(state
, dsc
[7 + i
])) < 0)
1302 type
= combine_word(&dsc
[4]);
1303 for (info
= list
; info
&& info
->type
; info
++)
1304 if (info
->type
== type
)
1306 if (! info
|| ! info
->type
)
1307 info
= &default_info
;
1309 for (valinfo
= info
->values
; valinfo
->control
; valinfo
++) {
1310 /* FIXME: bitmap might be longer than 8bit */
1311 if (! (dsc
[12 + num_ins
] & (1 << (valinfo
->control
- 1))))
1313 if (check_ignored_ctl(state
, unitid
, valinfo
->control
))
1315 cval
= kzalloc(sizeof(*cval
), GFP_KERNEL
);
1317 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1320 cval
->mixer
= state
->mixer
;
1322 cval
->control
= valinfo
->control
;
1323 cval
->val_type
= valinfo
->val_type
;
1326 /* get min/max values */
1327 if (type
== USB_PROC_UPDOWN
&& cval
->control
== USB_PROC_UPDOWN_MODE_SEL
) {
1328 /* FIXME: hard-coded */
1330 cval
->max
= dsc
[15];
1332 cval
->initialized
= 1;
1334 get_min_max(cval
, valinfo
->min_value
);
1336 kctl
= snd_ctl_new1(&mixer_procunit_ctl
, cval
);
1338 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1342 kctl
->private_free
= usb_mixer_elem_free
;
1344 if (check_mapped_name(state
, unitid
, cval
->control
, kctl
->id
.name
, sizeof(kctl
->id
.name
)))
1346 else if (info
->name
)
1347 strlcpy(kctl
->id
.name
, info
->name
, sizeof(kctl
->id
.name
));
1349 nameid
= dsc
[12 + num_ins
+ dsc
[11 + num_ins
]];
1352 len
= snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1354 strlcpy(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
));
1356 strlcat(kctl
->id
.name
, " ", sizeof(kctl
->id
.name
));
1357 strlcat(kctl
->id
.name
, valinfo
->suffix
, sizeof(kctl
->id
.name
));
1359 snd_printdd(KERN_INFO
"[%d] PU [%s] ch = %d, val = %d/%d\n",
1360 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
);
1361 if ((err
= add_control_to_empty(state
, kctl
)) < 0)
1368 static int parse_audio_processing_unit(struct mixer_build
*state
, int unitid
, unsigned char *desc
)
1370 return build_audio_procunit(state
, unitid
, desc
, procunits
, "Processing Unit");
1373 static int parse_audio_extension_unit(struct mixer_build
*state
, int unitid
, unsigned char *desc
)
1375 return build_audio_procunit(state
, unitid
, desc
, NULL
, "Extension Unit");
1383 /* info callback for selector unit
1384 * use an enumerator type for routing
1386 static int mixer_ctl_selector_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
1388 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1389 char **itemlist
= (char **)kcontrol
->private_value
;
1391 snd_assert(itemlist
, return -EINVAL
);
1392 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1394 uinfo
->value
.enumerated
.items
= cval
->max
;
1395 if ((int)uinfo
->value
.enumerated
.item
>= cval
->max
)
1396 uinfo
->value
.enumerated
.item
= cval
->max
- 1;
1397 strcpy(uinfo
->value
.enumerated
.name
, itemlist
[uinfo
->value
.enumerated
.item
]);
1401 /* get callback for selector unit */
1402 static int mixer_ctl_selector_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1404 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1407 err
= get_cur_ctl_value(cval
, 0, &val
);
1409 if (cval
->mixer
->ignore_ctl_error
) {
1410 ucontrol
->value
.enumerated
.item
[0] = 0;
1415 val
= get_relative_value(cval
, val
);
1416 ucontrol
->value
.enumerated
.item
[0] = val
;
1420 /* put callback for selector unit */
1421 static int mixer_ctl_selector_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1423 struct usb_mixer_elem_info
*cval
= kcontrol
->private_data
;
1426 err
= get_cur_ctl_value(cval
, 0, &oval
);
1428 if (cval
->mixer
->ignore_ctl_error
)
1432 val
= ucontrol
->value
.enumerated
.item
[0];
1433 val
= get_abs_value(cval
, val
);
1435 set_cur_ctl_value(cval
, 0, val
);
1441 /* alsa control interface for selector unit */
1442 static struct snd_kcontrol_new mixer_selectunit_ctl
= {
1443 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1444 .name
= "", /* will be filled later */
1445 .info
= mixer_ctl_selector_info
,
1446 .get
= mixer_ctl_selector_get
,
1447 .put
= mixer_ctl_selector_put
,
1451 /* private free callback.
1452 * free both private_data and private_value
1454 static void usb_mixer_selector_elem_free(struct snd_kcontrol
*kctl
)
1458 if (kctl
->private_data
) {
1459 struct usb_mixer_elem_info
*cval
= kctl
->private_data
;
1460 num_ins
= cval
->max
;
1462 kctl
->private_data
= NULL
;
1464 if (kctl
->private_value
) {
1465 char **itemlist
= (char **)kctl
->private_value
;
1466 for (i
= 0; i
< num_ins
; i
++)
1469 kctl
->private_value
= 0;
1474 * parse a selector unit
1476 static int parse_audio_selector_unit(struct mixer_build
*state
, int unitid
, unsigned char *desc
)
1478 unsigned int num_ins
= desc
[4];
1479 unsigned int i
, nameid
, len
;
1481 struct usb_mixer_elem_info
*cval
;
1482 struct snd_kcontrol
*kctl
;
1485 if (! num_ins
|| desc
[0] < 5 + num_ins
) {
1486 snd_printk(KERN_ERR
"invalid SELECTOR UNIT descriptor %d\n", unitid
);
1490 for (i
= 0; i
< num_ins
; i
++) {
1491 if ((err
= parse_audio_unit(state
, desc
[5 + i
])) < 0)
1495 if (num_ins
== 1) /* only one ? nonsense! */
1498 if (check_ignored_ctl(state
, unitid
, 0))
1501 cval
= kzalloc(sizeof(*cval
), GFP_KERNEL
);
1503 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1506 cval
->mixer
= state
->mixer
;
1508 cval
->val_type
= USB_MIXER_U8
;
1511 cval
->max
= num_ins
;
1513 cval
->initialized
= 1;
1515 namelist
= kmalloc(sizeof(char *) * num_ins
, GFP_KERNEL
);
1517 snd_printk(KERN_ERR
"cannot malloc\n");
1521 #define MAX_ITEM_NAME_LEN 64
1522 for (i
= 0; i
< num_ins
; i
++) {
1523 struct usb_audio_term iterm
;
1525 namelist
[i
] = kmalloc(MAX_ITEM_NAME_LEN
, GFP_KERNEL
);
1526 if (! namelist
[i
]) {
1527 snd_printk(KERN_ERR
"cannot malloc\n");
1534 len
= check_mapped_selector_name(state
, unitid
, i
, namelist
[i
],
1536 if (! len
&& check_input_term(state
, desc
[5 + i
], &iterm
) >= 0)
1537 len
= get_term_name(state
, &iterm
, namelist
[i
], MAX_ITEM_NAME_LEN
, 0);
1539 sprintf(namelist
[i
], "Input %d", i
);
1542 kctl
= snd_ctl_new1(&mixer_selectunit_ctl
, cval
);
1544 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1549 kctl
->private_value
= (unsigned long)namelist
;
1550 kctl
->private_free
= usb_mixer_selector_elem_free
;
1552 nameid
= desc
[desc
[0] - 1];
1553 len
= check_mapped_name(state
, unitid
, 0, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1557 snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1559 len
= get_term_name(state
, &state
->oterm
,
1560 kctl
->id
.name
, sizeof(kctl
->id
.name
), 0);
1562 strlcpy(kctl
->id
.name
, "USB", sizeof(kctl
->id
.name
));
1564 if ((state
->oterm
.type
& 0xff00) == 0x0100)
1565 strlcat(kctl
->id
.name
, " Capture Source", sizeof(kctl
->id
.name
));
1567 strlcat(kctl
->id
.name
, " Playback Source", sizeof(kctl
->id
.name
));
1570 snd_printdd(KERN_INFO
"[%d] SU [%s] items = %d\n",
1571 cval
->id
, kctl
->id
.name
, num_ins
);
1572 if ((err
= add_control_to_empty(state
, kctl
)) < 0)
1580 * parse an audio unit recursively
1583 static int parse_audio_unit(struct mixer_build
*state
, int unitid
)
1587 if (test_and_set_bit(unitid
, state
->unitbitmap
))
1588 return 0; /* the unit already visited */
1590 p1
= find_audio_control_unit(state
, unitid
);
1592 snd_printk(KERN_ERR
"usbaudio: unit %d not found!\n", unitid
);
1597 case INPUT_TERMINAL
:
1600 return parse_audio_mixer_unit(state
, unitid
, p1
);
1602 return parse_audio_selector_unit(state
, unitid
, p1
);
1604 return parse_audio_feature_unit(state
, unitid
, p1
);
1605 case PROCESSING_UNIT
:
1606 return parse_audio_processing_unit(state
, unitid
, p1
);
1607 case EXTENSION_UNIT
:
1608 return parse_audio_extension_unit(state
, unitid
, p1
);
1610 snd_printk(KERN_ERR
"usbaudio: unit %u: unexpected type 0x%02x\n", unitid
, p1
[2]);
1615 static void snd_usb_mixer_free(struct usb_mixer_interface
*mixer
)
1617 kfree(mixer
->id_elems
);
1619 kfree(mixer
->urb
->transfer_buffer
);
1620 usb_free_urb(mixer
->urb
);
1622 usb_free_urb(mixer
->rc_urb
);
1623 kfree(mixer
->rc_setup_packet
);
1627 static int snd_usb_mixer_dev_free(struct snd_device
*device
)
1629 struct usb_mixer_interface
*mixer
= device
->device_data
;
1630 snd_usb_mixer_free(mixer
);
1635 * create mixer controls
1637 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1639 static int snd_usb_mixer_controls(struct usb_mixer_interface
*mixer
)
1641 unsigned char *desc
;
1642 struct mixer_build state
;
1644 const struct usbmix_ctl_map
*map
;
1645 struct usb_host_interface
*hostif
;
1647 hostif
= &usb_ifnum_to_if(mixer
->chip
->dev
, mixer
->ctrlif
)->altsetting
[0];
1648 memset(&state
, 0, sizeof(state
));
1649 state
.chip
= mixer
->chip
;
1650 state
.mixer
= mixer
;
1651 state
.buffer
= hostif
->extra
;
1652 state
.buflen
= hostif
->extralen
;
1654 /* check the mapping table */
1655 for (map
= usbmix_ctl_maps
; map
->id
; map
++) {
1656 if (map
->id
== state
.chip
->usb_id
) {
1657 state
.map
= map
->map
;
1658 state
.selector_map
= map
->selector_map
;
1659 mixer
->ignore_ctl_error
= map
->ignore_ctl_error
;
1665 while ((desc
= snd_usb_find_csint_desc(hostif
->extra
, hostif
->extralen
, desc
, OUTPUT_TERMINAL
)) != NULL
) {
1667 continue; /* invalid descriptor? */
1668 set_bit(desc
[3], state
.unitbitmap
); /* mark terminal ID as visited */
1669 state
.oterm
.id
= desc
[3];
1670 state
.oterm
.type
= combine_word(&desc
[4]);
1671 state
.oterm
.name
= desc
[8];
1672 err
= parse_audio_unit(&state
, desc
[7]);
1679 static void snd_usb_mixer_notify_id(struct usb_mixer_interface
*mixer
,
1682 struct usb_mixer_elem_info
*info
;
1684 for (info
= mixer
->id_elems
[unitid
]; info
; info
= info
->next_id_elem
)
1685 snd_ctl_notify(mixer
->chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
1689 static void snd_usb_mixer_memory_change(struct usb_mixer_interface
*mixer
,
1694 /* unit ids specific to Extigy/Audigy 2 NX: */
1696 case 0: /* remote control */
1697 mixer
->rc_urb
->dev
= mixer
->chip
->dev
;
1698 usb_submit_urb(mixer
->rc_urb
, GFP_ATOMIC
);
1700 case 4: /* digital in jack */
1701 case 7: /* line in jacks */
1702 case 19: /* speaker out jacks */
1703 case 20: /* headphones out jack */
1705 /* live24ext: 4 = line-in jack */
1706 case 3: /* hp-out jack (may actuate Mute) */
1707 if (mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3040))
1708 snd_usb_mixer_notify_id(mixer
, mixer
->rc_cfg
->mute_mixer_id
);
1711 snd_printd(KERN_DEBUG
"memory change in unknown unit %d\n", unitid
);
1716 static void snd_usb_mixer_status_complete(struct urb
*urb
)
1718 struct usb_mixer_interface
*mixer
= urb
->context
;
1720 if (urb
->status
== 0) {
1721 u8
*buf
= urb
->transfer_buffer
;
1724 for (i
= urb
->actual_length
; i
>= 2; buf
+= 2, i
-= 2) {
1725 snd_printd(KERN_DEBUG
"status interrupt: %02x %02x\n",
1727 /* ignore any notifications not from the control interface */
1728 if ((buf
[0] & 0x0f) != 0)
1730 if (!(buf
[0] & 0x40))
1731 snd_usb_mixer_notify_id(mixer
, buf
[1]);
1733 snd_usb_mixer_memory_change(mixer
, buf
[1]);
1736 if (urb
->status
!= -ENOENT
&& urb
->status
!= -ECONNRESET
) {
1737 urb
->dev
= mixer
->chip
->dev
;
1738 usb_submit_urb(urb
, GFP_ATOMIC
);
1742 /* create the handler for the optional status interrupt endpoint */
1743 static int snd_usb_mixer_status_create(struct usb_mixer_interface
*mixer
)
1745 struct usb_host_interface
*hostif
;
1746 struct usb_endpoint_descriptor
*ep
;
1747 void *transfer_buffer
;
1751 hostif
= &usb_ifnum_to_if(mixer
->chip
->dev
, mixer
->ctrlif
)->altsetting
[0];
1752 /* we need one interrupt input endpoint */
1753 if (get_iface_desc(hostif
)->bNumEndpoints
< 1)
1755 ep
= get_endpoint(hostif
, 0);
1756 if ((ep
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_IN
||
1757 (ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
1760 epnum
= ep
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1761 buffer_length
= le16_to_cpu(ep
->wMaxPacketSize
);
1762 transfer_buffer
= kmalloc(buffer_length
, GFP_KERNEL
);
1763 if (!transfer_buffer
)
1765 mixer
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
1767 kfree(transfer_buffer
);
1770 usb_fill_int_urb(mixer
->urb
, mixer
->chip
->dev
,
1771 usb_rcvintpipe(mixer
->chip
->dev
, epnum
),
1772 transfer_buffer
, buffer_length
,
1773 snd_usb_mixer_status_complete
, mixer
, ep
->bInterval
);
1774 usb_submit_urb(mixer
->urb
, GFP_KERNEL
);
1778 static void snd_usb_soundblaster_remote_complete(struct urb
*urb
)
1780 struct usb_mixer_interface
*mixer
= urb
->context
;
1781 const struct rc_config
*rc
= mixer
->rc_cfg
;
1784 if (urb
->status
< 0 || urb
->actual_length
< rc
->packet_length
)
1787 code
= mixer
->rc_buffer
[rc
->offset
];
1788 if (rc
->length
== 2)
1789 code
|= mixer
->rc_buffer
[rc
->offset
+ 1] << 8;
1791 /* the Mute button actually changes the mixer control */
1792 if (code
== rc
->mute_code
)
1793 snd_usb_mixer_notify_id(mixer
, rc
->mute_mixer_id
);
1794 mixer
->rc_code
= code
;
1796 wake_up(&mixer
->rc_waitq
);
1799 static int snd_usb_sbrc_hwdep_open(struct snd_hwdep
*hw
, struct file
*file
)
1801 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1803 if (test_and_set_bit(0, &mixer
->rc_hwdep_open
))
1808 static int snd_usb_sbrc_hwdep_release(struct snd_hwdep
*hw
, struct file
*file
)
1810 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1812 clear_bit(0, &mixer
->rc_hwdep_open
);
1813 smp_mb__after_clear_bit();
1817 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep
*hw
, char __user
*buf
,
1818 long count
, loff_t
*offset
)
1820 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1824 if (count
!= 1 && count
!= 4)
1826 err
= wait_event_interruptible(mixer
->rc_waitq
,
1827 (rc_code
= xchg(&mixer
->rc_code
, 0)) != 0);
1830 err
= put_user(rc_code
, buf
);
1832 err
= put_user(rc_code
, (u32 __user
*)buf
);
1834 return err
< 0 ? err
: count
;
1837 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep
*hw
, struct file
*file
,
1840 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1842 poll_wait(file
, &mixer
->rc_waitq
, wait
);
1843 return mixer
->rc_code
? POLLIN
| POLLRDNORM
: 0;
1846 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface
*mixer
)
1848 struct snd_hwdep
*hwdep
;
1851 for (i
= 0; i
< ARRAY_SIZE(rc_configs
); ++i
)
1852 if (rc_configs
[i
].usb_id
== mixer
->chip
->usb_id
)
1854 if (i
>= ARRAY_SIZE(rc_configs
))
1856 mixer
->rc_cfg
= &rc_configs
[i
];
1858 len
= mixer
->rc_cfg
->packet_length
;
1860 init_waitqueue_head(&mixer
->rc_waitq
);
1861 err
= snd_hwdep_new(mixer
->chip
->card
, "SB remote control", 0, &hwdep
);
1864 snprintf(hwdep
->name
, sizeof(hwdep
->name
),
1865 "%s remote control", mixer
->chip
->card
->shortname
);
1866 hwdep
->iface
= SNDRV_HWDEP_IFACE_SB_RC
;
1867 hwdep
->private_data
= mixer
;
1868 hwdep
->ops
.read
= snd_usb_sbrc_hwdep_read
;
1869 hwdep
->ops
.open
= snd_usb_sbrc_hwdep_open
;
1870 hwdep
->ops
.release
= snd_usb_sbrc_hwdep_release
;
1871 hwdep
->ops
.poll
= snd_usb_sbrc_hwdep_poll
;
1873 mixer
->rc_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1876 mixer
->rc_setup_packet
= kmalloc(sizeof(*mixer
->rc_setup_packet
), GFP_KERNEL
);
1877 if (!mixer
->rc_setup_packet
) {
1878 usb_free_urb(mixer
->rc_urb
);
1879 mixer
->rc_urb
= NULL
;
1882 mixer
->rc_setup_packet
->bRequestType
=
1883 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
1884 mixer
->rc_setup_packet
->bRequest
= GET_MEM
;
1885 mixer
->rc_setup_packet
->wValue
= cpu_to_le16(0);
1886 mixer
->rc_setup_packet
->wIndex
= cpu_to_le16(0);
1887 mixer
->rc_setup_packet
->wLength
= cpu_to_le16(len
);
1888 usb_fill_control_urb(mixer
->rc_urb
, mixer
->chip
->dev
,
1889 usb_rcvctrlpipe(mixer
->chip
->dev
, 0),
1890 (u8
*)mixer
->rc_setup_packet
, mixer
->rc_buffer
, len
,
1891 snd_usb_soundblaster_remote_complete
, mixer
);
1895 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
1897 static int snd_audigy2nx_led_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1899 struct usb_mixer_interface
*mixer
= snd_kcontrol_chip(kcontrol
);
1900 int index
= kcontrol
->private_value
;
1902 ucontrol
->value
.integer
.value
[0] = mixer
->audigy2nx_leds
[index
];
1906 static int snd_audigy2nx_led_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1908 struct usb_mixer_interface
*mixer
= snd_kcontrol_chip(kcontrol
);
1909 int index
= kcontrol
->private_value
;
1910 int value
= ucontrol
->value
.integer
.value
[0];
1915 changed
= value
!= mixer
->audigy2nx_leds
[index
];
1916 err
= snd_usb_ctl_msg(mixer
->chip
->dev
,
1917 usb_sndctrlpipe(mixer
->chip
->dev
, 0), 0x24,
1918 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_OTHER
,
1919 value
, index
+ 2, NULL
, 0, 100);
1922 mixer
->audigy2nx_leds
[index
] = value
;
1926 static struct snd_kcontrol_new snd_audigy2nx_controls
[] = {
1928 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1929 .name
= "CMSS LED Switch",
1930 .info
= snd_audigy2nx_led_info
,
1931 .get
= snd_audigy2nx_led_get
,
1932 .put
= snd_audigy2nx_led_put
,
1936 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1937 .name
= "Power LED Switch",
1938 .info
= snd_audigy2nx_led_info
,
1939 .get
= snd_audigy2nx_led_get
,
1940 .put
= snd_audigy2nx_led_put
,
1944 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1945 .name
= "Dolby Digital LED Switch",
1946 .info
= snd_audigy2nx_led_info
,
1947 .get
= snd_audigy2nx_led_get
,
1948 .put
= snd_audigy2nx_led_put
,
1953 static int snd_audigy2nx_controls_create(struct usb_mixer_interface
*mixer
)
1957 for (i
= 0; i
< ARRAY_SIZE(snd_audigy2nx_controls
); ++i
) {
1958 if (i
> 1 && /* Live24ext has 2 LEDs only */
1959 mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3040))
1961 err
= snd_ctl_add(mixer
->chip
->card
,
1962 snd_ctl_new1(&snd_audigy2nx_controls
[i
], mixer
));
1966 mixer
->audigy2nx_leds
[1] = 1; /* Power LED is on by default */
1970 static void snd_audigy2nx_proc_read(struct snd_info_entry
*entry
,
1971 struct snd_info_buffer
*buffer
)
1973 static const struct sb_jack
{
1976 } jacks_audigy2nx
[] = {
1982 }, jacks_live24ext
[] = {
1983 {4, "line in"}, /* &1=Line, &2=Mic*/
1984 {3, "hph out"}, /* headphones */
1985 {0, "RC "}, /* last command, 6 bytes see rc_config above */
1988 const struct sb_jack
*jacks
;
1989 struct usb_mixer_interface
*mixer
= entry
->private_data
;
1993 snd_iprintf(buffer
, "%s jacks\n\n", mixer
->chip
->card
->shortname
);
1994 if (mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3020))
1995 jacks
= jacks_audigy2nx
;
1996 else if (mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3040))
1997 jacks
= jacks_live24ext
;
2001 for (i
= 0; jacks
[i
].name
; ++i
) {
2002 snd_iprintf(buffer
, "%s: ", jacks
[i
].name
);
2003 err
= snd_usb_ctl_msg(mixer
->chip
->dev
,
2004 usb_rcvctrlpipe(mixer
->chip
->dev
, 0),
2005 GET_MEM
, USB_DIR_IN
| USB_TYPE_CLASS
|
2006 USB_RECIP_INTERFACE
, 0,
2007 jacks
[i
].unitid
<< 8, buf
, 3, 100);
2008 if (err
== 3 && (buf
[0] == 3 || buf
[0] == 6))
2009 snd_iprintf(buffer
, "%02x %02x\n", buf
[1], buf
[2]);
2011 snd_iprintf(buffer
, "?\n");
2015 int snd_usb_create_mixer(struct snd_usb_audio
*chip
, int ctrlif
)
2017 static struct snd_device_ops dev_ops
= {
2018 .dev_free
= snd_usb_mixer_dev_free
2020 struct usb_mixer_interface
*mixer
;
2023 strcpy(chip
->card
->mixername
, "USB Mixer");
2025 mixer
= kzalloc(sizeof(*mixer
), GFP_KERNEL
);
2029 mixer
->ctrlif
= ctrlif
;
2030 #ifdef IGNORE_CTL_ERROR
2031 mixer
->ignore_ctl_error
= 1;
2033 mixer
->id_elems
= kcalloc(256, sizeof(*mixer
->id_elems
), GFP_KERNEL
);
2034 if (!mixer
->id_elems
) {
2039 if ((err
= snd_usb_mixer_controls(mixer
)) < 0 ||
2040 (err
= snd_usb_mixer_status_create(mixer
)) < 0)
2043 if ((err
= snd_usb_soundblaster_remote_init(mixer
)) < 0)
2046 if (mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3020) ||
2047 mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3040)) {
2048 struct snd_info_entry
*entry
;
2050 if ((err
= snd_audigy2nx_controls_create(mixer
)) < 0)
2052 if (!snd_card_proc_new(chip
->card
, "audigy2nx", &entry
))
2053 snd_info_set_text_ops(entry
, mixer
,
2054 snd_audigy2nx_proc_read
);
2057 err
= snd_device_new(chip
->card
, SNDRV_DEV_LOWLEVEL
, mixer
, &dev_ops
);
2060 list_add(&mixer
->list
, &chip
->mixer_list
);
2064 snd_usb_mixer_free(mixer
);
2068 void snd_usb_mixer_disconnect(struct list_head
*p
)
2070 struct usb_mixer_interface
*mixer
;
2072 mixer
= list_entry(p
, struct usb_mixer_interface
, list
);
2073 usb_kill_urb(mixer
->urb
);
2074 usb_kill_urb(mixer
->rc_urb
);