2 * (Tentative) USB Audio Driver for ALSA
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sound/driver.h>
30 #include <linux/bitops.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/usb.h>
36 #include <sound/core.h>
37 #include <sound/control.h>
38 #include <sound/hwdep.h>
45 /* ignore error from controls - for debugging */
46 /* #define IGNORE_CTL_ERROR */
48 typedef struct usb_mixer_build mixer_build_t
;
49 typedef struct usb_audio_term usb_audio_term_t
;
50 typedef struct usb_mixer_elem_info usb_mixer_elem_info_t
;
53 struct usb_mixer_interface
{
54 snd_usb_audio_t
*chip
;
56 struct list_head list
;
57 unsigned int ignore_ctl_error
;
59 usb_mixer_elem_info_t
**id_elems
; /* array[256], indexed by unit id */
61 /* Sound Blaster remote control stuff */
67 unsigned long rc_hwdep_open
;
69 wait_queue_head_t rc_waitq
;
71 struct usb_ctrlrequest
*rc_setup_packet
;
78 struct usb_audio_term
{
82 unsigned int chconfig
;
86 struct usbmix_name_map
;
88 struct usb_mixer_build
{
89 snd_usb_audio_t
*chip
;
90 struct usb_mixer_interface
*mixer
;
91 unsigned char *buffer
;
93 DECLARE_BITMAP(unitbitmap
, 256);
94 usb_audio_term_t oterm
;
95 const struct usbmix_name_map
*map
;
96 const struct usbmix_selector_map
*selector_map
;
99 struct usb_mixer_elem_info
{
100 struct usb_mixer_interface
*mixer
;
101 usb_mixer_elem_info_t
*next_id_elem
; /* list of controls with same id */
102 snd_ctl_elem_id_t
*elem_id
;
104 unsigned int control
; /* CS or ICN (high byte) */
105 unsigned int cmask
; /* channel mask bitmap: 0 = master */
114 USB_FEATURE_NONE
= 0,
115 USB_FEATURE_MUTE
= 1,
123 USB_FEATURE_BASSBOOST
,
129 USB_MIXER_INV_BOOLEAN
,
138 USB_PROC_UPDOWN_SWITCH
= 1,
139 USB_PROC_UPDOWN_MODE_SEL
= 2,
141 USB_PROC_PROLOGIC
= 2,
142 USB_PROC_PROLOGIC_SWITCH
= 1,
143 USB_PROC_PROLOGIC_MODE_SEL
= 2,
146 USB_PROC_3DENH_SWITCH
= 1,
147 USB_PROC_3DENH_SPACE
= 2,
150 USB_PROC_REVERB_SWITCH
= 1,
151 USB_PROC_REVERB_LEVEL
= 2,
152 USB_PROC_REVERB_TIME
= 3,
153 USB_PROC_REVERB_DELAY
= 4,
156 USB_PROC_CHORUS_SWITCH
= 1,
157 USB_PROC_CHORUS_LEVEL
= 2,
158 USB_PROC_CHORUS_RATE
= 3,
159 USB_PROC_CHORUS_DEPTH
= 4,
162 USB_PROC_DCR_SWITCH
= 1,
163 USB_PROC_DCR_RATIO
= 2,
164 USB_PROC_DCR_MAX_AMP
= 3,
165 USB_PROC_DCR_THRESHOLD
= 4,
166 USB_PROC_DCR_ATTACK
= 5,
167 USB_PROC_DCR_RELEASE
= 6,
170 #define MAX_CHANNELS 10 /* max logical channels */
174 * manual mapping of mixer names
175 * if the mixer topology is too complicated and the parsed names are
176 * ambiguous, add the entries in usbmixer_maps.c.
178 #include "usbmixer_maps.c"
180 /* get the mapped name if the unit matches */
181 static int check_mapped_name(mixer_build_t
*state
, int unitid
, int control
, char *buf
, int buflen
)
183 const struct usbmix_name_map
*p
;
188 for (p
= state
->map
; p
->id
; p
++) {
189 if (p
->id
== unitid
&& p
->name
&&
190 (! control
|| ! p
->control
|| control
== p
->control
)) {
192 return strlcpy(buf
, p
->name
, buflen
);
198 /* check whether the control should be ignored */
199 static int check_ignored_ctl(mixer_build_t
*state
, int unitid
, int control
)
201 const struct usbmix_name_map
*p
;
205 for (p
= state
->map
; p
->id
; p
++) {
206 if (p
->id
== unitid
&& ! p
->name
&&
207 (! control
|| ! p
->control
|| control
== p
->control
)) {
208 // printk("ignored control %d:%d\n", unitid, control);
215 /* get the mapped selector source name */
216 static int check_mapped_selector_name(mixer_build_t
*state
, int unitid
,
217 int index
, char *buf
, int buflen
)
219 const struct usbmix_selector_map
*p
;
221 if (! state
->selector_map
)
223 for (p
= state
->selector_map
; p
->id
; p
++) {
224 if (p
->id
== unitid
&& index
< p
->count
)
225 return strlcpy(buf
, p
->names
[index
], buflen
);
231 * find an audio control unit with the given unit id
233 static void *find_audio_control_unit(mixer_build_t
*state
, unsigned char unit
)
238 while ((p
= snd_usb_find_desc(state
->buffer
, state
->buflen
, p
,
239 USB_DT_CS_INTERFACE
)) != NULL
) {
240 if (p
[0] >= 4 && p
[2] >= INPUT_TERMINAL
&& p
[2] <= EXTENSION_UNIT
&& p
[3] == unit
)
248 * copy a string with the given id
250 static int snd_usb_copy_string_desc(mixer_build_t
*state
, int index
, char *buf
, int maxlen
)
252 int len
= usb_string(state
->chip
->dev
, index
, buf
, maxlen
- 1);
258 * convert from the byte/word on usb descriptor to the zero-based integer
260 static int convert_signed_value(usb_mixer_elem_info_t
*cval
, int val
)
262 switch (cval
->val_type
) {
263 case USB_MIXER_BOOLEAN
:
265 case USB_MIXER_INV_BOOLEAN
:
288 * convert from the zero-based int to the byte/word for usb descriptor
290 static int convert_bytes_value(usb_mixer_elem_info_t
*cval
, int val
)
292 switch (cval
->val_type
) {
293 case USB_MIXER_BOOLEAN
:
295 case USB_MIXER_INV_BOOLEAN
:
304 return 0; /* not reached */
307 static int get_relative_value(usb_mixer_elem_info_t
*cval
, int val
)
313 else if (val
> cval
->max
)
314 return (cval
->max
- cval
->min
) / cval
->res
;
316 return (val
- cval
->min
) / cval
->res
;
319 static int get_abs_value(usb_mixer_elem_info_t
*cval
, int val
)
334 * retrieve a mixer value
337 static int get_ctl_value(usb_mixer_elem_info_t
*cval
, int request
, int validx
, int *value_ret
)
339 unsigned char buf
[2];
340 int val_len
= cval
->val_type
>= USB_MIXER_S16
? 2 : 1;
343 while (timeout
-- > 0) {
344 if (snd_usb_ctl_msg(cval
->mixer
->chip
->dev
,
345 usb_rcvctrlpipe(cval
->mixer
->chip
->dev
, 0),
347 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
348 validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8),
349 buf
, val_len
, 100) >= 0) {
350 *value_ret
= convert_signed_value(cval
, snd_usb_combine_bytes(buf
, val_len
));
354 snd_printdd(KERN_ERR
"cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
355 request
, validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8), cval
->val_type
);
359 static int get_cur_ctl_value(usb_mixer_elem_info_t
*cval
, int validx
, int *value
)
361 return get_ctl_value(cval
, GET_CUR
, validx
, value
);
364 /* channel = 0: master, 1 = first channel */
365 inline static int get_cur_mix_value(usb_mixer_elem_info_t
*cval
, int channel
, int *value
)
367 return get_ctl_value(cval
, GET_CUR
, (cval
->control
<< 8) | channel
, value
);
374 static int set_ctl_value(usb_mixer_elem_info_t
*cval
, int request
, int validx
, int value_set
)
376 unsigned char buf
[2];
377 int val_len
= cval
->val_type
>= USB_MIXER_S16
? 2 : 1;
380 value_set
= convert_bytes_value(cval
, value_set
);
381 buf
[0] = value_set
& 0xff;
382 buf
[1] = (value_set
>> 8) & 0xff;
383 while (timeout
-- > 0)
384 if (snd_usb_ctl_msg(cval
->mixer
->chip
->dev
,
385 usb_sndctrlpipe(cval
->mixer
->chip
->dev
, 0),
387 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
388 validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8),
389 buf
, val_len
, 100) >= 0)
391 snd_printdd(KERN_ERR
"cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
392 request
, validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8), cval
->val_type
, buf
[0], buf
[1]);
396 static int set_cur_ctl_value(usb_mixer_elem_info_t
*cval
, int validx
, int value
)
398 return set_ctl_value(cval
, SET_CUR
, validx
, value
);
401 inline static int set_cur_mix_value(usb_mixer_elem_info_t
*cval
, int channel
, int value
)
403 return set_ctl_value(cval
, SET_CUR
, (cval
->control
<< 8) | channel
, value
);
408 * parser routines begin here...
411 static int parse_audio_unit(mixer_build_t
*state
, int unitid
);
415 * check if the input/output channel routing is enabled on the given bitmap.
416 * used for mixer unit parser
418 static int check_matrix_bitmap(unsigned char *bmap
, int ich
, int och
, int num_outs
)
420 int idx
= ich
* num_outs
+ och
;
421 return bmap
[idx
>> 3] & (0x80 >> (idx
& 7));
426 * add an alsa control element
427 * search and increment the index until an empty slot is found.
429 * if failed, give up and free the control instance.
432 static int add_control_to_empty(mixer_build_t
*state
, snd_kcontrol_t
*kctl
)
434 usb_mixer_elem_info_t
*cval
= kctl
->private_data
;
437 while (snd_ctl_find_id(state
->chip
->card
, &kctl
->id
))
439 if ((err
= snd_ctl_add(state
->chip
->card
, kctl
)) < 0) {
440 snd_printd(KERN_ERR
"cannot add control (err = %d)\n", err
);
441 snd_ctl_free_one(kctl
);
444 cval
->elem_id
= &kctl
->id
;
445 cval
->next_id_elem
= state
->mixer
->id_elems
[cval
->id
];
446 state
->mixer
->id_elems
[cval
->id
] = cval
;
452 * get a terminal name string
455 static struct iterm_name_combo
{
459 { 0x0300, "Output" },
460 { 0x0301, "Speaker" },
461 { 0x0302, "Headphone" },
462 { 0x0303, "HMD Audio" },
463 { 0x0304, "Desktop Speaker" },
464 { 0x0305, "Room Speaker" },
465 { 0x0306, "Com Speaker" },
467 { 0x0600, "External In" },
468 { 0x0601, "Analog In" },
469 { 0x0602, "Digital In" },
471 { 0x0604, "Legacy In" },
472 { 0x0605, "IEC958 In" },
473 { 0x0606, "1394 DA Stream" },
474 { 0x0607, "1394 DV Stream" },
475 { 0x0700, "Embedded" },
476 { 0x0701, "Noise Source" },
477 { 0x0702, "Equalization Noise" },
481 { 0x0706, "MiniDisk" },
482 { 0x0707, "Analog Tape" },
483 { 0x0708, "Phonograph" },
484 { 0x0709, "VCR Audio" },
485 { 0x070a, "Video Disk Audio" },
486 { 0x070b, "DVD Audio" },
487 { 0x070c, "TV Tuner Audio" },
488 { 0x070d, "Satellite Rec Audio" },
489 { 0x070e, "Cable Tuner Audio" },
490 { 0x070f, "DSS Audio" },
491 { 0x0710, "Radio Receiver" },
492 { 0x0711, "Radio Transmitter" },
493 { 0x0712, "Multi-Track Recorder" },
494 { 0x0713, "Synthesizer" },
498 static int get_term_name(mixer_build_t
*state
, usb_audio_term_t
*iterm
,
499 unsigned char *name
, int maxlen
, int term_only
)
501 struct iterm_name_combo
*names
;
504 return snd_usb_copy_string_desc(state
, iterm
->name
, name
, maxlen
);
506 /* virtual type - not a real terminal */
507 if (iterm
->type
>> 16) {
510 switch (iterm
->type
>> 16) {
512 strcpy(name
, "Selector"); return 8;
513 case PROCESSING_UNIT
:
514 strcpy(name
, "Process Unit"); return 12;
516 strcpy(name
, "Ext Unit"); return 8;
518 strcpy(name
, "Mixer"); return 5;
520 return sprintf(name
, "Unit %d", iterm
->id
);
524 switch (iterm
->type
& 0xff00) {
526 strcpy(name
, "PCM"); return 3;
528 strcpy(name
, "Mic"); return 3;
530 strcpy(name
, "Headset"); return 7;
532 strcpy(name
, "Phone"); return 5;
535 for (names
= iterm_names
; names
->type
; names
++)
536 if (names
->type
== iterm
->type
) {
537 strcpy(name
, names
->name
);
538 return strlen(names
->name
);
545 * parse the source unit recursively until it reaches to a terminal
546 * or a branched unit.
548 static int check_input_term(mixer_build_t
*state
, int id
, usb_audio_term_t
*term
)
552 memset(term
, 0, sizeof(*term
));
553 while ((p1
= find_audio_control_unit(state
, id
)) != NULL
) {
557 term
->type
= combine_word(p1
+ 4);
558 term
->channels
= p1
[7];
559 term
->chconfig
= combine_word(p1
+ 8);
564 break; /* continue to parse */
566 term
->type
= p1
[2] << 16; /* virtual type */
567 term
->channels
= p1
[5 + p1
[4]];
568 term
->chconfig
= combine_word(p1
+ 6 + p1
[4]);
569 term
->name
= p1
[p1
[0] - 1];
572 /* call recursively to retrieve the channel info */
573 if (check_input_term(state
, p1
[5], term
) < 0)
575 term
->type
= p1
[2] << 16; /* virtual type */
577 term
->name
= p1
[9 + p1
[0] - 1];
579 case PROCESSING_UNIT
:
583 break; /* continue to parse */
585 term
->type
= p1
[2] << 16; /* virtual type */
586 term
->channels
= p1
[7 + p1
[6]];
587 term
->chconfig
= combine_word(p1
+ 8 + p1
[6]);
588 term
->name
= p1
[12 + p1
[6] + p1
[11 + p1
[6]]];
602 /* feature unit control information */
603 struct usb_feature_control_info
{
605 unsigned int type
; /* control type (mute, volume, etc.) */
608 static struct usb_feature_control_info audio_feature_info
[] = {
609 { "Mute", USB_MIXER_INV_BOOLEAN
},
610 { "Volume", USB_MIXER_S16
},
611 { "Tone Control - Bass", USB_MIXER_S8
},
612 { "Tone Control - Mid", USB_MIXER_S8
},
613 { "Tone Control - Treble", USB_MIXER_S8
},
614 { "Graphic Equalizer", USB_MIXER_S8
}, /* FIXME: not implemeted yet */
615 { "Auto Gain Control", USB_MIXER_BOOLEAN
},
616 { "Delay Control", USB_MIXER_U16
},
617 { "Bass Boost", USB_MIXER_BOOLEAN
},
618 { "Loudness", USB_MIXER_BOOLEAN
},
622 /* private_free callback */
623 static void usb_mixer_elem_free(snd_kcontrol_t
*kctl
)
625 if (kctl
->private_data
) {
626 kfree(kctl
->private_data
);
627 kctl
->private_data
= NULL
;
633 * interface to ALSA control for feature/mixer units
637 * retrieve the minimum and maximum values for the specified control
639 static int get_min_max(usb_mixer_elem_info_t
*cval
, int default_min
)
642 cval
->min
= default_min
;
643 cval
->max
= cval
->min
+ 1;
646 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
647 cval
->val_type
== USB_MIXER_INV_BOOLEAN
) {
648 cval
->initialized
= 1;
653 for (i
= 0; i
< MAX_CHANNELS
; i
++)
654 if (cval
->cmask
& (1 << i
)) {
659 if (get_ctl_value(cval
, GET_MAX
, (cval
->control
<< 8) | minchn
, &cval
->max
) < 0 ||
660 get_ctl_value(cval
, GET_MIN
, (cval
->control
<< 8) | minchn
, &cval
->min
) < 0) {
661 snd_printd(KERN_ERR
"%d:%d: cannot get min/max values for control %d (id %d)\n",
662 cval
->id
, cval
->mixer
->ctrlif
, cval
->control
, cval
->id
);
665 if (get_ctl_value(cval
, GET_RES
, (cval
->control
<< 8) | minchn
, &cval
->res
) < 0) {
668 int last_valid_res
= cval
->res
;
670 while (cval
->res
> 1) {
671 if (set_ctl_value(cval
, SET_RES
, (cval
->control
<< 8) | minchn
, cval
->res
/ 2) < 0)
675 if (get_ctl_value(cval
, GET_RES
, (cval
->control
<< 8) | minchn
, &cval
->res
) < 0)
676 cval
->res
= last_valid_res
;
680 cval
->initialized
= 1;
686 /* get a feature/mixer unit info */
687 static int mixer_ctl_feature_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
689 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
691 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
692 cval
->val_type
== USB_MIXER_INV_BOOLEAN
)
693 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
695 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
696 uinfo
->count
= cval
->channels
;
697 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
698 cval
->val_type
== USB_MIXER_INV_BOOLEAN
) {
699 uinfo
->value
.integer
.min
= 0;
700 uinfo
->value
.integer
.max
= 1;
702 if (! cval
->initialized
)
703 get_min_max(cval
, 0);
704 uinfo
->value
.integer
.min
= 0;
705 uinfo
->value
.integer
.max
= (cval
->max
- cval
->min
) / cval
->res
;
710 /* get the current value from feature/mixer unit */
711 static int mixer_ctl_feature_get(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
713 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
714 int c
, cnt
, val
, err
;
718 for (c
= 0; c
< MAX_CHANNELS
; c
++) {
719 if (cval
->cmask
& (1 << c
)) {
720 err
= get_cur_mix_value(cval
, c
+ 1, &val
);
722 if (cval
->mixer
->ignore_ctl_error
) {
723 ucontrol
->value
.integer
.value
[0] = cval
->min
;
726 snd_printd(KERN_ERR
"cannot get current value for control %d ch %d: err = %d\n", cval
->control
, c
+ 1, err
);
729 val
= get_relative_value(cval
, val
);
730 ucontrol
->value
.integer
.value
[cnt
] = val
;
736 err
= get_cur_mix_value(cval
, 0, &val
);
738 if (cval
->mixer
->ignore_ctl_error
) {
739 ucontrol
->value
.integer
.value
[0] = cval
->min
;
742 snd_printd(KERN_ERR
"cannot get current value for control %d master ch: err = %d\n", cval
->control
, err
);
745 val
= get_relative_value(cval
, val
);
746 ucontrol
->value
.integer
.value
[0] = val
;
751 /* put the current value to feature/mixer unit */
752 static int mixer_ctl_feature_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
754 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
755 int c
, cnt
, val
, oval
, err
;
760 for (c
= 0; c
< MAX_CHANNELS
; c
++) {
761 if (cval
->cmask
& (1 << c
)) {
762 err
= get_cur_mix_value(cval
, c
+ 1, &oval
);
764 if (cval
->mixer
->ignore_ctl_error
)
768 val
= ucontrol
->value
.integer
.value
[cnt
];
769 val
= get_abs_value(cval
, val
);
771 set_cur_mix_value(cval
, c
+ 1, val
);
774 get_cur_mix_value(cval
, c
+ 1, &val
);
780 err
= get_cur_mix_value(cval
, 0, &oval
);
781 if (err
< 0 && cval
->mixer
->ignore_ctl_error
)
785 val
= ucontrol
->value
.integer
.value
[0];
786 val
= get_abs_value(cval
, val
);
788 set_cur_mix_value(cval
, 0, val
);
795 static snd_kcontrol_new_t usb_feature_unit_ctl
= {
796 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
797 .name
= "", /* will be filled later manually */
798 .info
= mixer_ctl_feature_info
,
799 .get
= mixer_ctl_feature_get
,
800 .put
= mixer_ctl_feature_put
,
805 * build a feature control
808 static void build_feature_ctl(mixer_build_t
*state
, unsigned char *desc
,
809 unsigned int ctl_mask
, int control
,
810 usb_audio_term_t
*iterm
, int unitid
)
812 unsigned int len
= 0;
814 int nameid
= desc
[desc
[0] - 1];
815 snd_kcontrol_t
*kctl
;
816 usb_mixer_elem_info_t
*cval
;
818 control
++; /* change from zero-based to 1-based value */
820 if (control
== USB_FEATURE_GEQ
) {
821 /* FIXME: not supported yet */
825 if (check_ignored_ctl(state
, unitid
, control
))
828 cval
= kcalloc(1, sizeof(*cval
), GFP_KERNEL
);
830 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
833 cval
->mixer
= state
->mixer
;
835 cval
->control
= control
;
836 cval
->cmask
= ctl_mask
;
837 cval
->val_type
= audio_feature_info
[control
-1].type
;
839 cval
->channels
= 1; /* master channel */
842 for (i
= 0; i
< 16; i
++)
843 if (ctl_mask
& (1 << i
))
848 /* get min/max values */
849 get_min_max(cval
, 0);
851 kctl
= snd_ctl_new1(&usb_feature_unit_ctl
, cval
);
853 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
857 kctl
->private_free
= usb_mixer_elem_free
;
859 len
= check_mapped_name(state
, unitid
, control
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
860 mapped_name
= len
!= 0;
862 len
= snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
865 case USB_FEATURE_MUTE
:
866 case USB_FEATURE_VOLUME
:
867 /* determine the control name. the rule is:
868 * - if a name id is given in descriptor, use it.
869 * - if the connected input can be determined, then use the name
871 * - if the connected output can be determined, use it.
872 * - otherwise, anonymous name.
875 len
= get_term_name(state
, iterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 1);
877 len
= get_term_name(state
, &state
->oterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 1);
879 len
= snprintf(kctl
->id
.name
, sizeof(kctl
->id
.name
),
880 "Feature %d", unitid
);
882 /* determine the stream direction:
883 * if the connected output is USB stream, then it's likely a
884 * capture stream. otherwise it should be playback (hopefully :)
886 if (! mapped_name
&& ! (state
->oterm
.type
>> 16)) {
887 if ((state
->oterm
.type
& 0xff00) == 0x0100) {
888 len
= strlcat(kctl
->id
.name
, " Capture", sizeof(kctl
->id
.name
));
890 len
= strlcat(kctl
->id
.name
+ len
, " Playback", sizeof(kctl
->id
.name
));
893 strlcat(kctl
->id
.name
+ len
, control
== USB_FEATURE_MUTE
? " Switch" : " Volume",
894 sizeof(kctl
->id
.name
));
899 strlcpy(kctl
->id
.name
, audio_feature_info
[control
-1].name
,
900 sizeof(kctl
->id
.name
));
904 /* quirk for UDA1321/N101 */
905 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
906 /* is not very clear from datasheets */
907 /* I hope that the min value is -15360 for newer firmware --jk */
908 switch (state
->chip
->usb_id
) {
909 case USB_ID(0x0471, 0x0101):
910 case USB_ID(0x0471, 0x0104):
911 case USB_ID(0x0471, 0x0105):
912 case USB_ID(0x0672, 0x1041):
913 if (!strcmp(kctl
->id
.name
, "PCM Playback Volume") &&
914 cval
->min
== -15616) {
915 snd_printk("using volume control quirk for the UDA1321/N101 chip\n");
920 snd_printdd(KERN_INFO
"[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
921 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
, cval
->res
);
922 add_control_to_empty(state
, kctl
);
928 * parse a feature unit
930 * most of controlls are defined here.
932 static int parse_audio_feature_unit(mixer_build_t
*state
, int unitid
, unsigned char *ftr
)
935 usb_audio_term_t iterm
;
936 unsigned int master_bits
, first_ch_bits
;
939 if (ftr
[0] < 7 || ! (csize
= ftr
[5]) || ftr
[0] < 7 + csize
) {
940 snd_printk(KERN_ERR
"usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid
);
944 /* parse the source unit */
945 if ((err
= parse_audio_unit(state
, ftr
[4])) < 0)
948 /* determine the input source type and name */
949 if (check_input_term(state
, ftr
[4], &iterm
) < 0)
952 channels
= (ftr
[0] - 7) / csize
- 1;
954 master_bits
= snd_usb_combine_bytes(ftr
+ 6, csize
);
956 first_ch_bits
= snd_usb_combine_bytes(ftr
+ 6 + csize
, csize
);
959 /* check all control types */
960 for (i
= 0; i
< 10; i
++) {
961 unsigned int ch_bits
= 0;
962 for (j
= 0; j
< channels
; j
++) {
963 unsigned int mask
= snd_usb_combine_bytes(ftr
+ 6 + csize
* (j
+1), csize
);
967 if (ch_bits
& 1) /* the first channel must be set (for ease of programming) */
968 build_feature_ctl(state
, ftr
, ch_bits
, i
, &iterm
, unitid
);
969 if (master_bits
& (1 << i
))
970 build_feature_ctl(state
, ftr
, 0, i
, &iterm
, unitid
);
982 * build a mixer unit control
984 * the callbacks are identical with feature unit.
985 * input channel number (zero based) is given in control field instead.
988 static void build_mixer_unit_ctl(mixer_build_t
*state
, unsigned char *desc
,
989 int in_pin
, int in_ch
, int unitid
,
990 usb_audio_term_t
*iterm
)
992 usb_mixer_elem_info_t
*cval
;
993 unsigned int input_pins
= desc
[4];
994 unsigned int num_outs
= desc
[5 + input_pins
];
996 snd_kcontrol_t
*kctl
;
998 if (check_ignored_ctl(state
, unitid
, 0))
1001 cval
= kcalloc(1, sizeof(*cval
), GFP_KERNEL
);
1005 cval
->mixer
= state
->mixer
;
1007 cval
->control
= in_ch
+ 1; /* based on 1 */
1008 cval
->val_type
= USB_MIXER_S16
;
1009 for (i
= 0; i
< num_outs
; i
++) {
1010 if (check_matrix_bitmap(desc
+ 9 + input_pins
, in_ch
, i
, num_outs
)) {
1011 cval
->cmask
|= (1 << i
);
1016 /* get min/max values */
1017 get_min_max(cval
, 0);
1019 kctl
= snd_ctl_new1(&usb_feature_unit_ctl
, cval
);
1021 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1025 kctl
->private_free
= usb_mixer_elem_free
;
1027 len
= check_mapped_name(state
, unitid
, 0, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1029 len
= get_term_name(state
, iterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 0);
1031 len
= sprintf(kctl
->id
.name
, "Mixer Source %d", in_ch
+ 1);
1032 strlcat(kctl
->id
.name
+ len
, " Volume", sizeof(kctl
->id
.name
));
1034 snd_printdd(KERN_INFO
"[%d] MU [%s] ch = %d, val = %d/%d\n",
1035 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
);
1036 add_control_to_empty(state
, kctl
);
1041 * parse a mixer unit
1043 static int parse_audio_mixer_unit(mixer_build_t
*state
, int unitid
, unsigned char *desc
)
1045 usb_audio_term_t iterm
;
1046 int input_pins
, num_ins
, num_outs
;
1049 if (desc
[0] < 11 || ! (input_pins
= desc
[4]) || ! (num_outs
= desc
[5 + input_pins
])) {
1050 snd_printk(KERN_ERR
"invalid MIXER UNIT descriptor %d\n", unitid
);
1053 /* no bmControls field (e.g. Maya44) -> ignore */
1054 if (desc
[0] <= 10 + input_pins
) {
1055 snd_printdd(KERN_INFO
"MU %d has no bmControls field\n", unitid
);
1061 for (pin
= 0; pin
< input_pins
; pin
++) {
1062 err
= parse_audio_unit(state
, desc
[5 + pin
]);
1065 err
= check_input_term(state
, desc
[5 + pin
], &iterm
);
1068 num_ins
+= iterm
.channels
;
1069 for (; ich
< num_ins
; ++ich
) {
1070 int och
, ich_has_controls
= 0;
1072 for (och
= 0; och
< num_outs
; ++och
) {
1073 if (check_matrix_bitmap(desc
+ 9 + input_pins
,
1074 ich
, och
, num_outs
)) {
1075 ich_has_controls
= 1;
1079 if (ich_has_controls
)
1080 build_mixer_unit_ctl(state
, desc
, pin
, ich
,
1089 * Processing Unit / Extension Unit
1092 /* get callback for processing/extension unit */
1093 static int mixer_ctl_procunit_get(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1095 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1098 err
= get_cur_ctl_value(cval
, cval
->control
<< 8, &val
);
1099 if (err
< 0 && cval
->mixer
->ignore_ctl_error
) {
1100 ucontrol
->value
.integer
.value
[0] = cval
->min
;
1105 val
= get_relative_value(cval
, val
);
1106 ucontrol
->value
.integer
.value
[0] = val
;
1110 /* put callback for processing/extension unit */
1111 static int mixer_ctl_procunit_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1113 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1116 err
= get_cur_ctl_value(cval
, cval
->control
<< 8, &oval
);
1118 if (cval
->mixer
->ignore_ctl_error
)
1122 val
= ucontrol
->value
.integer
.value
[0];
1123 val
= get_abs_value(cval
, val
);
1125 set_cur_ctl_value(cval
, cval
->control
<< 8, val
);
1131 /* alsa control interface for processing/extension unit */
1132 static snd_kcontrol_new_t mixer_procunit_ctl
= {
1133 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1134 .name
= "", /* will be filled later */
1135 .info
= mixer_ctl_feature_info
,
1136 .get
= mixer_ctl_procunit_get
,
1137 .put
= mixer_ctl_procunit_put
,
1142 * predefined data for processing units
1144 struct procunit_value_info
{
1151 struct procunit_info
{
1154 struct procunit_value_info
*values
;
1157 static struct procunit_value_info updown_proc_info
[] = {
1158 { USB_PROC_UPDOWN_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1159 { USB_PROC_UPDOWN_MODE_SEL
, "Mode Select", USB_MIXER_U8
, 1 },
1162 static struct procunit_value_info prologic_proc_info
[] = {
1163 { USB_PROC_PROLOGIC_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1164 { USB_PROC_PROLOGIC_MODE_SEL
, "Mode Select", USB_MIXER_U8
, 1 },
1167 static struct procunit_value_info threed_enh_proc_info
[] = {
1168 { USB_PROC_3DENH_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1169 { USB_PROC_3DENH_SPACE
, "Spaciousness", USB_MIXER_U8
},
1172 static struct procunit_value_info reverb_proc_info
[] = {
1173 { USB_PROC_REVERB_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1174 { USB_PROC_REVERB_LEVEL
, "Level", USB_MIXER_U8
},
1175 { USB_PROC_REVERB_TIME
, "Time", USB_MIXER_U16
},
1176 { USB_PROC_REVERB_DELAY
, "Delay", USB_MIXER_U8
},
1179 static struct procunit_value_info chorus_proc_info
[] = {
1180 { USB_PROC_CHORUS_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1181 { USB_PROC_CHORUS_LEVEL
, "Level", USB_MIXER_U8
},
1182 { USB_PROC_CHORUS_RATE
, "Rate", USB_MIXER_U16
},
1183 { USB_PROC_CHORUS_DEPTH
, "Depth", USB_MIXER_U16
},
1186 static struct procunit_value_info dcr_proc_info
[] = {
1187 { USB_PROC_DCR_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1188 { USB_PROC_DCR_RATIO
, "Ratio", USB_MIXER_U16
},
1189 { USB_PROC_DCR_MAX_AMP
, "Max Amp", USB_MIXER_S16
},
1190 { USB_PROC_DCR_THRESHOLD
, "Threshold", USB_MIXER_S16
},
1191 { USB_PROC_DCR_ATTACK
, "Attack Time", USB_MIXER_U16
},
1192 { USB_PROC_DCR_RELEASE
, "Release Time", USB_MIXER_U16
},
1196 static struct procunit_info procunits
[] = {
1197 { USB_PROC_UPDOWN
, "Up Down", updown_proc_info
},
1198 { USB_PROC_PROLOGIC
, "Dolby Prologic", prologic_proc_info
},
1199 { USB_PROC_3DENH
, "3D Stereo Extender", threed_enh_proc_info
},
1200 { USB_PROC_REVERB
, "Reverb", reverb_proc_info
},
1201 { USB_PROC_CHORUS
, "Chorus", chorus_proc_info
},
1202 { USB_PROC_DCR
, "DCR", dcr_proc_info
},
1207 * build a processing/extension unit
1209 static int build_audio_procunit(mixer_build_t
*state
, int unitid
, unsigned char *dsc
, struct procunit_info
*list
, char *name
)
1211 int num_ins
= dsc
[6];
1212 usb_mixer_elem_info_t
*cval
;
1213 snd_kcontrol_t
*kctl
;
1214 int i
, err
, nameid
, type
, len
;
1215 struct procunit_info
*info
;
1216 struct procunit_value_info
*valinfo
;
1217 static struct procunit_value_info default_value_info
[] = {
1218 { 0x01, "Switch", USB_MIXER_BOOLEAN
},
1221 static struct procunit_info default_info
= {
1222 0, NULL
, default_value_info
1225 if (dsc
[0] < 13 || dsc
[0] < 13 + num_ins
|| dsc
[0] < num_ins
+ dsc
[11 + num_ins
]) {
1226 snd_printk(KERN_ERR
"invalid %s descriptor (id %d)\n", name
, unitid
);
1230 for (i
= 0; i
< num_ins
; i
++) {
1231 if ((err
= parse_audio_unit(state
, dsc
[7 + i
])) < 0)
1235 type
= combine_word(&dsc
[4]);
1236 for (info
= list
; info
&& info
->type
; info
++)
1237 if (info
->type
== type
)
1239 if (! info
|| ! info
->type
)
1240 info
= &default_info
;
1242 for (valinfo
= info
->values
; valinfo
->control
; valinfo
++) {
1243 /* FIXME: bitmap might be longer than 8bit */
1244 if (! (dsc
[12 + num_ins
] & (1 << (valinfo
->control
- 1))))
1246 if (check_ignored_ctl(state
, unitid
, valinfo
->control
))
1248 cval
= kcalloc(1, sizeof(*cval
), GFP_KERNEL
);
1250 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1253 cval
->mixer
= state
->mixer
;
1255 cval
->control
= valinfo
->control
;
1256 cval
->val_type
= valinfo
->val_type
;
1259 /* get min/max values */
1260 if (type
== USB_PROC_UPDOWN
&& cval
->control
== USB_PROC_UPDOWN_MODE_SEL
) {
1261 /* FIXME: hard-coded */
1263 cval
->max
= dsc
[15];
1265 cval
->initialized
= 1;
1267 get_min_max(cval
, valinfo
->min_value
);
1269 kctl
= snd_ctl_new1(&mixer_procunit_ctl
, cval
);
1271 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1275 kctl
->private_free
= usb_mixer_elem_free
;
1277 if (check_mapped_name(state
, unitid
, cval
->control
, kctl
->id
.name
, sizeof(kctl
->id
.name
)))
1279 else if (info
->name
)
1280 strlcpy(kctl
->id
.name
, info
->name
, sizeof(kctl
->id
.name
));
1282 nameid
= dsc
[12 + num_ins
+ dsc
[11 + num_ins
]];
1285 len
= snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1287 strlcpy(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
));
1289 strlcat(kctl
->id
.name
, " ", sizeof(kctl
->id
.name
));
1290 strlcat(kctl
->id
.name
, valinfo
->suffix
, sizeof(kctl
->id
.name
));
1292 snd_printdd(KERN_INFO
"[%d] PU [%s] ch = %d, val = %d/%d\n",
1293 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
);
1294 if ((err
= add_control_to_empty(state
, kctl
)) < 0)
1301 static int parse_audio_processing_unit(mixer_build_t
*state
, int unitid
, unsigned char *desc
)
1303 return build_audio_procunit(state
, unitid
, desc
, procunits
, "Processing Unit");
1306 static int parse_audio_extension_unit(mixer_build_t
*state
, int unitid
, unsigned char *desc
)
1308 return build_audio_procunit(state
, unitid
, desc
, NULL
, "Extension Unit");
1316 /* info callback for selector unit
1317 * use an enumerator type for routing
1319 static int mixer_ctl_selector_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
1321 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1322 char **itemlist
= (char **)kcontrol
->private_value
;
1324 snd_assert(itemlist
, return -EINVAL
);
1325 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1327 uinfo
->value
.enumerated
.items
= cval
->max
;
1328 if ((int)uinfo
->value
.enumerated
.item
>= cval
->max
)
1329 uinfo
->value
.enumerated
.item
= cval
->max
- 1;
1330 strcpy(uinfo
->value
.enumerated
.name
, itemlist
[uinfo
->value
.enumerated
.item
]);
1334 /* get callback for selector unit */
1335 static int mixer_ctl_selector_get(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1337 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1340 err
= get_cur_ctl_value(cval
, 0, &val
);
1342 if (cval
->mixer
->ignore_ctl_error
) {
1343 ucontrol
->value
.enumerated
.item
[0] = 0;
1348 val
= get_relative_value(cval
, val
);
1349 ucontrol
->value
.enumerated
.item
[0] = val
;
1353 /* put callback for selector unit */
1354 static int mixer_ctl_selector_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1356 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1359 err
= get_cur_ctl_value(cval
, 0, &oval
);
1361 if (cval
->mixer
->ignore_ctl_error
)
1365 val
= ucontrol
->value
.enumerated
.item
[0];
1366 val
= get_abs_value(cval
, val
);
1368 set_cur_ctl_value(cval
, 0, val
);
1374 /* alsa control interface for selector unit */
1375 static snd_kcontrol_new_t mixer_selectunit_ctl
= {
1376 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1377 .name
= "", /* will be filled later */
1378 .info
= mixer_ctl_selector_info
,
1379 .get
= mixer_ctl_selector_get
,
1380 .put
= mixer_ctl_selector_put
,
1384 /* private free callback.
1385 * free both private_data and private_value
1387 static void usb_mixer_selector_elem_free(snd_kcontrol_t
*kctl
)
1391 if (kctl
->private_data
) {
1392 usb_mixer_elem_info_t
*cval
= kctl
->private_data
;
1393 num_ins
= cval
->max
;
1395 kctl
->private_data
= NULL
;
1397 if (kctl
->private_value
) {
1398 char **itemlist
= (char **)kctl
->private_value
;
1399 for (i
= 0; i
< num_ins
; i
++)
1402 kctl
->private_value
= 0;
1407 * parse a selector unit
1409 static int parse_audio_selector_unit(mixer_build_t
*state
, int unitid
, unsigned char *desc
)
1411 unsigned int num_ins
= desc
[4];
1412 unsigned int i
, nameid
, len
;
1414 usb_mixer_elem_info_t
*cval
;
1415 snd_kcontrol_t
*kctl
;
1418 if (! num_ins
|| desc
[0] < 6 + num_ins
) {
1419 snd_printk(KERN_ERR
"invalid SELECTOR UNIT descriptor %d\n", unitid
);
1423 for (i
= 0; i
< num_ins
; i
++) {
1424 if ((err
= parse_audio_unit(state
, desc
[5 + i
])) < 0)
1428 if (num_ins
== 1) /* only one ? nonsense! */
1431 if (check_ignored_ctl(state
, unitid
, 0))
1434 cval
= kcalloc(1, sizeof(*cval
), GFP_KERNEL
);
1436 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1439 cval
->mixer
= state
->mixer
;
1441 cval
->val_type
= USB_MIXER_U8
;
1444 cval
->max
= num_ins
;
1446 cval
->initialized
= 1;
1448 namelist
= kmalloc(sizeof(char *) * num_ins
, GFP_KERNEL
);
1450 snd_printk(KERN_ERR
"cannot malloc\n");
1454 #define MAX_ITEM_NAME_LEN 64
1455 for (i
= 0; i
< num_ins
; i
++) {
1456 usb_audio_term_t iterm
;
1458 namelist
[i
] = kmalloc(MAX_ITEM_NAME_LEN
, GFP_KERNEL
);
1459 if (! namelist
[i
]) {
1460 snd_printk(KERN_ERR
"cannot malloc\n");
1467 len
= check_mapped_selector_name(state
, unitid
, i
, namelist
[i
],
1469 if (! len
&& check_input_term(state
, desc
[5 + i
], &iterm
) >= 0)
1470 len
= get_term_name(state
, &iterm
, namelist
[i
], MAX_ITEM_NAME_LEN
, 0);
1472 sprintf(namelist
[i
], "Input %d", i
);
1475 kctl
= snd_ctl_new1(&mixer_selectunit_ctl
, cval
);
1477 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1481 kctl
->private_value
= (unsigned long)namelist
;
1482 kctl
->private_free
= usb_mixer_selector_elem_free
;
1484 nameid
= desc
[desc
[0] - 1];
1485 len
= check_mapped_name(state
, unitid
, 0, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1489 snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1491 len
= get_term_name(state
, &state
->oterm
,
1492 kctl
->id
.name
, sizeof(kctl
->id
.name
), 0);
1494 strlcpy(kctl
->id
.name
, "USB", sizeof(kctl
->id
.name
));
1496 if ((state
->oterm
.type
& 0xff00) == 0x0100)
1497 strlcat(kctl
->id
.name
, " Capture Source", sizeof(kctl
->id
.name
));
1499 strlcat(kctl
->id
.name
, " Playback Source", sizeof(kctl
->id
.name
));
1502 snd_printdd(KERN_INFO
"[%d] SU [%s] items = %d\n",
1503 cval
->id
, kctl
->id
.name
, num_ins
);
1504 if ((err
= add_control_to_empty(state
, kctl
)) < 0)
1512 * parse an audio unit recursively
1515 static int parse_audio_unit(mixer_build_t
*state
, int unitid
)
1519 if (test_and_set_bit(unitid
, state
->unitbitmap
))
1520 return 0; /* the unit already visited */
1522 p1
= find_audio_control_unit(state
, unitid
);
1524 snd_printk(KERN_ERR
"usbaudio: unit %d not found!\n", unitid
);
1529 case INPUT_TERMINAL
:
1532 return parse_audio_mixer_unit(state
, unitid
, p1
);
1534 return parse_audio_selector_unit(state
, unitid
, p1
);
1536 return parse_audio_feature_unit(state
, unitid
, p1
);
1537 case PROCESSING_UNIT
:
1538 return parse_audio_processing_unit(state
, unitid
, p1
);
1539 case EXTENSION_UNIT
:
1540 return parse_audio_extension_unit(state
, unitid
, p1
);
1542 snd_printk(KERN_ERR
"usbaudio: unit %u: unexpected type 0x%02x\n", unitid
, p1
[2]);
1547 static void snd_usb_mixer_free(struct usb_mixer_interface
*mixer
)
1549 kfree(mixer
->id_elems
);
1551 kfree(mixer
->urb
->transfer_buffer
);
1552 usb_free_urb(mixer
->urb
);
1555 usb_free_urb(mixer
->rc_urb
);
1556 kfree(mixer
->rc_setup_packet
);
1560 static int snd_usb_mixer_dev_free(snd_device_t
*device
)
1562 struct usb_mixer_interface
*mixer
= device
->device_data
;
1563 snd_usb_mixer_free(mixer
);
1568 * create mixer controls
1570 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1572 static int snd_usb_mixer_controls(struct usb_mixer_interface
*mixer
)
1574 unsigned char *desc
;
1575 mixer_build_t state
;
1577 const struct usbmix_ctl_map
*map
;
1578 struct usb_host_interface
*hostif
;
1580 hostif
= &usb_ifnum_to_if(mixer
->chip
->dev
, mixer
->ctrlif
)->altsetting
[0];
1581 memset(&state
, 0, sizeof(state
));
1582 state
.chip
= mixer
->chip
;
1583 state
.mixer
= mixer
;
1584 state
.buffer
= hostif
->extra
;
1585 state
.buflen
= hostif
->extralen
;
1587 /* check the mapping table */
1588 for (map
= usbmix_ctl_maps
; map
->id
; map
++) {
1589 if (map
->id
== state
.chip
->usb_id
) {
1590 state
.map
= map
->map
;
1591 state
.selector_map
= map
->selector_map
;
1592 mixer
->ignore_ctl_error
= map
->ignore_ctl_error
;
1598 while ((desc
= snd_usb_find_csint_desc(hostif
->extra
, hostif
->extralen
, desc
, OUTPUT_TERMINAL
)) != NULL
) {
1600 continue; /* invalid descriptor? */
1601 set_bit(desc
[3], state
.unitbitmap
); /* mark terminal ID as visited */
1602 state
.oterm
.id
= desc
[3];
1603 state
.oterm
.type
= combine_word(&desc
[4]);
1604 state
.oterm
.name
= desc
[8];
1605 err
= parse_audio_unit(&state
, desc
[7]);
1612 static void snd_usb_mixer_notify_id(struct usb_mixer_interface
*mixer
,
1615 usb_mixer_elem_info_t
*info
;
1617 for (info
= mixer
->id_elems
[unitid
]; info
; info
= info
->next_id_elem
)
1618 snd_ctl_notify(mixer
->chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
1622 static void snd_usb_mixer_memory_change(struct usb_mixer_interface
*mixer
,
1625 /* SB remote control */
1626 if (mixer
->rc_type
!= RC_NONE
&& unitid
== 0) {
1627 /* read control code from device memory */
1628 mixer
->rc_urb
->dev
= mixer
->chip
->dev
;
1629 usb_submit_urb(mixer
->rc_urb
, GFP_ATOMIC
);
1633 static void snd_usb_mixer_status_complete(struct urb
*urb
, struct pt_regs
*regs
)
1635 struct usb_mixer_interface
*mixer
= urb
->context
;
1637 if (urb
->status
== 0) {
1638 u8
*buf
= urb
->transfer_buffer
;
1641 for (i
= urb
->actual_length
; i
>= 2; buf
+= 2, i
-= 2) {
1642 snd_printd(KERN_DEBUG
"status interrupt: %02x %02x\n",
1644 /* ignore any notifications not from the control interface */
1645 if ((buf
[0] & 0x0f) != 0)
1647 if (!(buf
[0] & 0x40))
1648 snd_usb_mixer_notify_id(mixer
, buf
[1]);
1650 snd_usb_mixer_memory_change(mixer
, buf
[1]);
1653 if (urb
->status
!= -ENOENT
&& urb
->status
!= -ECONNRESET
) {
1654 urb
->dev
= mixer
->chip
->dev
;
1655 usb_submit_urb(urb
, GFP_ATOMIC
);
1659 /* create the handler for the optional status interrupt endpoint */
1660 static int snd_usb_mixer_status_create(struct usb_mixer_interface
*mixer
)
1662 struct usb_host_interface
*hostif
;
1663 struct usb_endpoint_descriptor
*ep
;
1664 void *transfer_buffer
;
1668 hostif
= &usb_ifnum_to_if(mixer
->chip
->dev
, mixer
->ctrlif
)->altsetting
[0];
1669 /* we need one interrupt input endpoint */
1670 if (get_iface_desc(hostif
)->bNumEndpoints
< 1)
1672 ep
= get_endpoint(hostif
, 0);
1673 if ((ep
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_IN
||
1674 (ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
1677 epnum
= ep
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1678 buffer_length
= le16_to_cpu(ep
->wMaxPacketSize
);
1679 transfer_buffer
= kmalloc(buffer_length
, GFP_KERNEL
);
1680 if (!transfer_buffer
)
1682 mixer
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
1684 kfree(transfer_buffer
);
1687 usb_fill_int_urb(mixer
->urb
, mixer
->chip
->dev
,
1688 usb_rcvintpipe(mixer
->chip
->dev
, epnum
),
1689 transfer_buffer
, buffer_length
,
1690 snd_usb_mixer_status_complete
, mixer
, ep
->bInterval
);
1691 usb_submit_urb(mixer
->urb
, GFP_KERNEL
);
1695 static void snd_usb_soundblaster_remote_complete(struct urb
*urb
,
1696 struct pt_regs
*regs
)
1698 struct usb_mixer_interface
*mixer
= urb
->context
;
1700 * format of remote control data:
1702 * Audigy 2 NX: 06 80 xx 00 00 00
1704 int offset
= mixer
->rc_type
== RC_EXTIGY
? 0 : 2;
1707 if (urb
->status
< 0 || urb
->actual_length
<= offset
)
1709 code
= mixer
->rc_buffer
[offset
];
1710 /* the Mute button actually changes the mixer control */
1712 snd_usb_mixer_notify_id(mixer
, 18);
1713 mixer
->rc_code
= code
;
1715 wake_up(&mixer
->rc_waitq
);
1718 static int snd_usb_sbrc_hwdep_open(snd_hwdep_t
*hw
, struct file
*file
)
1720 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1722 if (test_and_set_bit(0, &mixer
->rc_hwdep_open
))
1727 static int snd_usb_sbrc_hwdep_release(snd_hwdep_t
*hw
, struct file
*file
)
1729 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1731 clear_bit(0, &mixer
->rc_hwdep_open
);
1732 smp_mb__after_clear_bit();
1736 static long snd_usb_sbrc_hwdep_read(snd_hwdep_t
*hw
, char __user
*buf
,
1737 long count
, loff_t
*offset
)
1739 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1743 if (count
!= 1 && count
!= 4)
1745 err
= wait_event_interruptible(mixer
->rc_waitq
,
1746 (rc_code
= xchg(&mixer
->rc_code
, 0)) != 0);
1749 err
= put_user(rc_code
, buf
);
1751 err
= put_user(rc_code
, (u32 __user
*)buf
);
1753 return err
< 0 ? err
: count
;
1756 static unsigned int snd_usb_sbrc_hwdep_poll(snd_hwdep_t
*hw
, struct file
*file
,
1759 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1761 poll_wait(file
, &mixer
->rc_waitq
, wait
);
1762 return mixer
->rc_code
? POLLIN
| POLLRDNORM
: 0;
1765 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface
*mixer
)
1770 switch (mixer
->chip
->usb_id
) {
1771 case USB_ID(0x041e, 0x3000):
1772 mixer
->rc_type
= RC_EXTIGY
;
1775 case USB_ID(0x041e, 0x3020):
1776 mixer
->rc_type
= RC_AUDIGY2NX
;
1783 init_waitqueue_head(&mixer
->rc_waitq
);
1784 err
= snd_hwdep_new(mixer
->chip
->card
, "SB remote control", 0, &hwdep
);
1787 snprintf(hwdep
->name
, sizeof(hwdep
->name
),
1788 "%s remote control", mixer
->chip
->card
->shortname
);
1789 hwdep
->iface
= SNDRV_HWDEP_IFACE_SB_RC
;
1790 hwdep
->private_data
= mixer
;
1791 hwdep
->ops
.read
= snd_usb_sbrc_hwdep_read
;
1792 hwdep
->ops
.open
= snd_usb_sbrc_hwdep_open
;
1793 hwdep
->ops
.release
= snd_usb_sbrc_hwdep_release
;
1794 hwdep
->ops
.poll
= snd_usb_sbrc_hwdep_poll
;
1796 mixer
->rc_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1799 mixer
->rc_setup_packet
= kmalloc(sizeof(*mixer
->rc_setup_packet
), GFP_KERNEL
);
1800 if (!mixer
->rc_setup_packet
) {
1801 usb_free_urb(mixer
->rc_urb
);
1802 mixer
->rc_urb
= NULL
;
1805 mixer
->rc_setup_packet
->bRequestType
=
1806 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
1807 mixer
->rc_setup_packet
->bRequest
= GET_MEM
;
1808 mixer
->rc_setup_packet
->wValue
= cpu_to_le16(0);
1809 mixer
->rc_setup_packet
->wIndex
= cpu_to_le16(0);
1810 mixer
->rc_setup_packet
->wLength
= cpu_to_le16(len
);
1811 usb_fill_control_urb(mixer
->rc_urb
, mixer
->chip
->dev
,
1812 usb_rcvctrlpipe(mixer
->chip
->dev
, 0),
1813 (u8
*)mixer
->rc_setup_packet
, mixer
->rc_buffer
, len
,
1814 snd_usb_soundblaster_remote_complete
, mixer
);
1818 static int snd_audigy2nx_led_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
1820 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1822 uinfo
->value
.integer
.min
= 0;
1823 uinfo
->value
.integer
.max
= 1;
1827 static int snd_audigy2nx_led_get(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1829 struct usb_mixer_interface
*mixer
= snd_kcontrol_chip(kcontrol
);
1830 int index
= kcontrol
->private_value
;
1832 ucontrol
->value
.integer
.value
[0] = mixer
->audigy2nx_leds
[index
];
1836 static int snd_audigy2nx_led_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1838 struct usb_mixer_interface
*mixer
= snd_kcontrol_chip(kcontrol
);
1839 int index
= kcontrol
->private_value
;
1840 int value
= ucontrol
->value
.integer
.value
[0];
1845 changed
= value
!= mixer
->audigy2nx_leds
[index
];
1846 err
= snd_usb_ctl_msg(mixer
->chip
->dev
,
1847 usb_sndctrlpipe(mixer
->chip
->dev
, 0), 0x24,
1848 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_OTHER
,
1849 value
, index
+ 2, NULL
, 0, 100);
1852 mixer
->audigy2nx_leds
[index
] = value
;
1856 static snd_kcontrol_new_t snd_audigy2nx_controls
[] = {
1858 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1859 .name
= "CMSS LED Switch",
1860 .info
= snd_audigy2nx_led_info
,
1861 .get
= snd_audigy2nx_led_get
,
1862 .put
= snd_audigy2nx_led_put
,
1866 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1867 .name
= "Power LED Switch",
1868 .info
= snd_audigy2nx_led_info
,
1869 .get
= snd_audigy2nx_led_get
,
1870 .put
= snd_audigy2nx_led_put
,
1874 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1875 .name
= "Dolby Digital LED Switch",
1876 .info
= snd_audigy2nx_led_info
,
1877 .get
= snd_audigy2nx_led_get
,
1878 .put
= snd_audigy2nx_led_put
,
1883 static int snd_audigy2nx_controls_create(struct usb_mixer_interface
*mixer
)
1887 for (i
= 0; i
< ARRAY_SIZE(snd_audigy2nx_controls
); ++i
) {
1888 err
= snd_ctl_add(mixer
->chip
->card
,
1889 snd_ctl_new1(&snd_audigy2nx_controls
[i
], mixer
));
1893 mixer
->audigy2nx_leds
[1] = 1; /* Power LED is on by default */
1897 int snd_usb_create_mixer(snd_usb_audio_t
*chip
, int ctrlif
)
1899 static snd_device_ops_t dev_ops
= {
1900 .dev_free
= snd_usb_mixer_dev_free
1902 struct usb_mixer_interface
*mixer
;
1905 strcpy(chip
->card
->mixername
, "USB Mixer");
1907 mixer
= kcalloc(1, sizeof(*mixer
), GFP_KERNEL
);
1911 mixer
->ctrlif
= ctrlif
;
1912 #ifdef IGNORE_CTL_ERROR
1913 mixer
->ignore_ctl_error
= 1;
1915 mixer
->id_elems
= kcalloc(256, sizeof(*mixer
->id_elems
), GFP_KERNEL
);
1916 if (!mixer
->id_elems
) {
1921 if ((err
= snd_usb_mixer_controls(mixer
)) < 0 ||
1922 (err
= snd_usb_mixer_status_create(mixer
)) < 0)
1925 if ((err
= snd_usb_soundblaster_remote_init(mixer
)) < 0)
1928 if (mixer
->chip
->usb_id
== USB_ID(0x041e, 0x3020)) {
1929 if ((err
= snd_audigy2nx_controls_create(mixer
)) < 0)
1933 err
= snd_device_new(chip
->card
, SNDRV_DEV_LOWLEVEL
, mixer
, &dev_ops
);
1936 list_add(&mixer
->list
, &chip
->mixer_list
);
1940 snd_usb_mixer_free(mixer
);
1944 void snd_usb_mixer_disconnect(struct list_head
*p
)
1946 struct usb_mixer_interface
*mixer
;
1948 mixer
= list_entry(p
, struct usb_mixer_interface
, list
);
1950 usb_kill_urb(mixer
->urb
);
1952 usb_kill_urb(mixer
->rc_urb
);