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>
43 #include <linux/lirc.h>
45 /* only those symbols from lirc.h we actually need: */
46 #include <linux/ioctl.h>
47 #define LIRC_MODE2REC(x) ((x) << 16)
48 #define LIRC_MODE_CODE 0x00000008
49 #define LIRC_CAN_REC_CODE LIRC_MODE2REC(LIRC_MODE_CODE)
50 #define LIRC_GET_FEATURES _IOR('i', 0x00000000, __u32)
51 #define LIRC_GET_REC_MODE _IOR('i', 0x00000002, __u32)
52 #define LIRC_SET_REC_MODE _IOW('i', 0x00000012, __u32)
58 /* ignore error from controls - for debugging */
59 /* #define IGNORE_CTL_ERROR */
61 typedef struct usb_mixer_build mixer_build_t
;
62 typedef struct usb_audio_term usb_audio_term_t
;
63 typedef struct usb_mixer_elem_info usb_mixer_elem_info_t
;
66 struct usb_mixer_interface
{
67 snd_usb_audio_t
*chip
;
69 struct list_head list
;
70 unsigned int ignore_ctl_error
;
72 usb_mixer_elem_info_t
**id_elems
; /* array[256], indexed by unit id */
74 /* Sound Blaster remote control stuff */
80 unsigned long rc_hwdep_open
;
82 wait_queue_head_t rc_waitq
;
84 struct usb_ctrlrequest
*rc_setup_packet
;
89 struct usb_audio_term
{
93 unsigned int chconfig
;
97 struct usbmix_name_map
;
99 struct usb_mixer_build
{
100 snd_usb_audio_t
*chip
;
101 struct usb_mixer_interface
*mixer
;
102 unsigned char *buffer
;
104 DECLARE_BITMAP(unitbitmap
, 256);
105 usb_audio_term_t oterm
;
106 const struct usbmix_name_map
*map
;
107 const struct usbmix_selector_map
*selector_map
;
110 struct usb_mixer_elem_info
{
111 struct usb_mixer_interface
*mixer
;
112 usb_mixer_elem_info_t
*next_id_elem
; /* list of controls with same id */
113 snd_ctl_elem_id_t
*elem_id
;
115 unsigned int control
; /* CS or ICN (high byte) */
116 unsigned int cmask
; /* channel mask bitmap: 0 = master */
125 USB_FEATURE_NONE
= 0,
126 USB_FEATURE_MUTE
= 1,
134 USB_FEATURE_BASSBOOST
,
140 USB_MIXER_INV_BOOLEAN
,
149 USB_PROC_UPDOWN_SWITCH
= 1,
150 USB_PROC_UPDOWN_MODE_SEL
= 2,
152 USB_PROC_PROLOGIC
= 2,
153 USB_PROC_PROLOGIC_SWITCH
= 1,
154 USB_PROC_PROLOGIC_MODE_SEL
= 2,
157 USB_PROC_3DENH_SWITCH
= 1,
158 USB_PROC_3DENH_SPACE
= 2,
161 USB_PROC_REVERB_SWITCH
= 1,
162 USB_PROC_REVERB_LEVEL
= 2,
163 USB_PROC_REVERB_TIME
= 3,
164 USB_PROC_REVERB_DELAY
= 4,
167 USB_PROC_CHORUS_SWITCH
= 1,
168 USB_PROC_CHORUS_LEVEL
= 2,
169 USB_PROC_CHORUS_RATE
= 3,
170 USB_PROC_CHORUS_DEPTH
= 4,
173 USB_PROC_DCR_SWITCH
= 1,
174 USB_PROC_DCR_RATIO
= 2,
175 USB_PROC_DCR_MAX_AMP
= 3,
176 USB_PROC_DCR_THRESHOLD
= 4,
177 USB_PROC_DCR_ATTACK
= 5,
178 USB_PROC_DCR_RELEASE
= 6,
181 #define MAX_CHANNELS 10 /* max logical channels */
185 * manual mapping of mixer names
186 * if the mixer topology is too complicated and the parsed names are
187 * ambiguous, add the entries in usbmixer_maps.c.
189 #include "usbmixer_maps.c"
191 /* get the mapped name if the unit matches */
192 static int check_mapped_name(mixer_build_t
*state
, int unitid
, int control
, char *buf
, int buflen
)
194 const struct usbmix_name_map
*p
;
199 for (p
= state
->map
; p
->id
; p
++) {
200 if (p
->id
== unitid
&& p
->name
&&
201 (! control
|| ! p
->control
|| control
== p
->control
)) {
203 return strlcpy(buf
, p
->name
, buflen
);
209 /* check whether the control should be ignored */
210 static int check_ignored_ctl(mixer_build_t
*state
, int unitid
, int control
)
212 const struct usbmix_name_map
*p
;
216 for (p
= state
->map
; p
->id
; p
++) {
217 if (p
->id
== unitid
&& ! p
->name
&&
218 (! control
|| ! p
->control
|| control
== p
->control
)) {
219 // printk("ignored control %d:%d\n", unitid, control);
226 /* get the mapped selector source name */
227 static int check_mapped_selector_name(mixer_build_t
*state
, int unitid
,
228 int index
, char *buf
, int buflen
)
230 const struct usbmix_selector_map
*p
;
232 if (! state
->selector_map
)
234 for (p
= state
->selector_map
; p
->id
; p
++) {
235 if (p
->id
== unitid
&& index
< p
->count
)
236 return strlcpy(buf
, p
->names
[index
], buflen
);
242 * find an audio control unit with the given unit id
244 static void *find_audio_control_unit(mixer_build_t
*state
, unsigned char unit
)
249 while ((p
= snd_usb_find_desc(state
->buffer
, state
->buflen
, p
,
250 USB_DT_CS_INTERFACE
)) != NULL
) {
251 if (p
[0] >= 4 && p
[2] >= INPUT_TERMINAL
&& p
[2] <= EXTENSION_UNIT
&& p
[3] == unit
)
259 * copy a string with the given id
261 static int snd_usb_copy_string_desc(mixer_build_t
*state
, int index
, char *buf
, int maxlen
)
263 int len
= usb_string(state
->chip
->dev
, index
, buf
, maxlen
- 1);
269 * convert from the byte/word on usb descriptor to the zero-based integer
271 static int convert_signed_value(usb_mixer_elem_info_t
*cval
, int val
)
273 switch (cval
->val_type
) {
274 case USB_MIXER_BOOLEAN
:
276 case USB_MIXER_INV_BOOLEAN
:
299 * convert from the zero-based int to the byte/word for usb descriptor
301 static int convert_bytes_value(usb_mixer_elem_info_t
*cval
, int val
)
303 switch (cval
->val_type
) {
304 case USB_MIXER_BOOLEAN
:
306 case USB_MIXER_INV_BOOLEAN
:
315 return 0; /* not reached */
318 static int get_relative_value(usb_mixer_elem_info_t
*cval
, int val
)
324 else if (val
> cval
->max
)
325 return (cval
->max
- cval
->min
) / cval
->res
;
327 return (val
- cval
->min
) / cval
->res
;
330 static int get_abs_value(usb_mixer_elem_info_t
*cval
, int val
)
345 * retrieve a mixer value
348 static int get_ctl_value(usb_mixer_elem_info_t
*cval
, int request
, int validx
, int *value_ret
)
350 unsigned char buf
[2];
351 int val_len
= cval
->val_type
>= USB_MIXER_S16
? 2 : 1;
354 while (timeout
-- > 0) {
355 if (snd_usb_ctl_msg(cval
->mixer
->chip
->dev
,
356 usb_rcvctrlpipe(cval
->mixer
->chip
->dev
, 0),
358 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
359 validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8),
360 buf
, val_len
, 100) >= 0) {
361 *value_ret
= convert_signed_value(cval
, snd_usb_combine_bytes(buf
, val_len
));
365 snd_printdd(KERN_ERR
"cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
366 request
, validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8), cval
->val_type
);
370 static int get_cur_ctl_value(usb_mixer_elem_info_t
*cval
, int validx
, int *value
)
372 return get_ctl_value(cval
, GET_CUR
, validx
, value
);
375 /* channel = 0: master, 1 = first channel */
376 inline static int get_cur_mix_value(usb_mixer_elem_info_t
*cval
, int channel
, int *value
)
378 return get_ctl_value(cval
, GET_CUR
, (cval
->control
<< 8) | channel
, value
);
385 static int set_ctl_value(usb_mixer_elem_info_t
*cval
, int request
, int validx
, int value_set
)
387 unsigned char buf
[2];
388 int val_len
= cval
->val_type
>= USB_MIXER_S16
? 2 : 1;
391 value_set
= convert_bytes_value(cval
, value_set
);
392 buf
[0] = value_set
& 0xff;
393 buf
[1] = (value_set
>> 8) & 0xff;
394 while (timeout
-- > 0)
395 if (snd_usb_ctl_msg(cval
->mixer
->chip
->dev
,
396 usb_sndctrlpipe(cval
->mixer
->chip
->dev
, 0),
398 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
399 validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8),
400 buf
, val_len
, 100) >= 0)
402 snd_printdd(KERN_ERR
"cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
403 request
, validx
, cval
->mixer
->ctrlif
| (cval
->id
<< 8), cval
->val_type
, buf
[0], buf
[1]);
407 static int set_cur_ctl_value(usb_mixer_elem_info_t
*cval
, int validx
, int value
)
409 return set_ctl_value(cval
, SET_CUR
, validx
, value
);
412 inline static int set_cur_mix_value(usb_mixer_elem_info_t
*cval
, int channel
, int value
)
414 return set_ctl_value(cval
, SET_CUR
, (cval
->control
<< 8) | channel
, value
);
419 * parser routines begin here...
422 static int parse_audio_unit(mixer_build_t
*state
, int unitid
);
426 * check if the input/output channel routing is enabled on the given bitmap.
427 * used for mixer unit parser
429 static int check_matrix_bitmap(unsigned char *bmap
, int ich
, int och
, int num_outs
)
431 int idx
= ich
* num_outs
+ och
;
432 return bmap
[idx
>> 3] & (0x80 >> (idx
& 7));
437 * add an alsa control element
438 * search and increment the index until an empty slot is found.
440 * if failed, give up and free the control instance.
443 static int add_control_to_empty(mixer_build_t
*state
, snd_kcontrol_t
*kctl
)
445 usb_mixer_elem_info_t
*cval
= kctl
->private_data
;
448 while (snd_ctl_find_id(state
->chip
->card
, &kctl
->id
))
450 if ((err
= snd_ctl_add(state
->chip
->card
, kctl
)) < 0) {
451 snd_printd(KERN_ERR
"cannot add control (err = %d)\n", err
);
452 snd_ctl_free_one(kctl
);
455 cval
->elem_id
= &kctl
->id
;
456 cval
->next_id_elem
= state
->mixer
->id_elems
[cval
->id
];
457 state
->mixer
->id_elems
[cval
->id
] = cval
;
463 * get a terminal name string
466 static struct iterm_name_combo
{
470 { 0x0300, "Output" },
471 { 0x0301, "Speaker" },
472 { 0x0302, "Headphone" },
473 { 0x0303, "HMD Audio" },
474 { 0x0304, "Desktop Speaker" },
475 { 0x0305, "Room Speaker" },
476 { 0x0306, "Com Speaker" },
478 { 0x0600, "External In" },
479 { 0x0601, "Analog In" },
480 { 0x0602, "Digital In" },
482 { 0x0604, "Legacy In" },
483 { 0x0605, "IEC958 In" },
484 { 0x0606, "1394 DA Stream" },
485 { 0x0607, "1394 DV Stream" },
486 { 0x0700, "Embedded" },
487 { 0x0701, "Noise Source" },
488 { 0x0702, "Equalization Noise" },
492 { 0x0706, "MiniDisk" },
493 { 0x0707, "Analog Tape" },
494 { 0x0708, "Phonograph" },
495 { 0x0709, "VCR Audio" },
496 { 0x070a, "Video Disk Audio" },
497 { 0x070b, "DVD Audio" },
498 { 0x070c, "TV Tuner Audio" },
499 { 0x070d, "Satellite Rec Audio" },
500 { 0x070e, "Cable Tuner Audio" },
501 { 0x070f, "DSS Audio" },
502 { 0x0710, "Radio Receiver" },
503 { 0x0711, "Radio Transmitter" },
504 { 0x0712, "Multi-Track Recorder" },
505 { 0x0713, "Synthesizer" },
509 static int get_term_name(mixer_build_t
*state
, usb_audio_term_t
*iterm
,
510 unsigned char *name
, int maxlen
, int term_only
)
512 struct iterm_name_combo
*names
;
515 return snd_usb_copy_string_desc(state
, iterm
->name
, name
, maxlen
);
517 /* virtual type - not a real terminal */
518 if (iterm
->type
>> 16) {
521 switch (iterm
->type
>> 16) {
523 strcpy(name
, "Selector"); return 8;
524 case PROCESSING_UNIT
:
525 strcpy(name
, "Process Unit"); return 12;
527 strcpy(name
, "Ext Unit"); return 8;
529 strcpy(name
, "Mixer"); return 5;
531 return sprintf(name
, "Unit %d", iterm
->id
);
535 switch (iterm
->type
& 0xff00) {
537 strcpy(name
, "PCM"); return 3;
539 strcpy(name
, "Mic"); return 3;
541 strcpy(name
, "Headset"); return 7;
543 strcpy(name
, "Phone"); return 5;
546 for (names
= iterm_names
; names
->type
; names
++)
547 if (names
->type
== iterm
->type
) {
548 strcpy(name
, names
->name
);
549 return strlen(names
->name
);
556 * parse the source unit recursively until it reaches to a terminal
557 * or a branched unit.
559 static int check_input_term(mixer_build_t
*state
, int id
, usb_audio_term_t
*term
)
563 memset(term
, 0, sizeof(*term
));
564 while ((p1
= find_audio_control_unit(state
, id
)) != NULL
) {
568 term
->type
= combine_word(p1
+ 4);
569 term
->channels
= p1
[7];
570 term
->chconfig
= combine_word(p1
+ 8);
575 break; /* continue to parse */
577 term
->type
= p1
[2] << 16; /* virtual type */
578 term
->channels
= p1
[5 + p1
[4]];
579 term
->chconfig
= combine_word(p1
+ 6 + p1
[4]);
580 term
->name
= p1
[p1
[0] - 1];
583 /* call recursively to retrieve the channel info */
584 if (check_input_term(state
, p1
[5], term
) < 0)
586 term
->type
= p1
[2] << 16; /* virtual type */
588 term
->name
= p1
[9 + p1
[0] - 1];
590 case PROCESSING_UNIT
:
594 break; /* continue to parse */
596 term
->type
= p1
[2] << 16; /* virtual type */
597 term
->channels
= p1
[7 + p1
[6]];
598 term
->chconfig
= combine_word(p1
+ 8 + p1
[6]);
599 term
->name
= p1
[12 + p1
[6] + p1
[11 + p1
[6]]];
613 /* feature unit control information */
614 struct usb_feature_control_info
{
616 unsigned int type
; /* control type (mute, volume, etc.) */
619 static struct usb_feature_control_info audio_feature_info
[] = {
620 { "Mute", USB_MIXER_INV_BOOLEAN
},
621 { "Volume", USB_MIXER_S16
},
622 { "Tone Control - Bass", USB_MIXER_S8
},
623 { "Tone Control - Mid", USB_MIXER_S8
},
624 { "Tone Control - Treble", USB_MIXER_S8
},
625 { "Graphic Equalizer", USB_MIXER_S8
}, /* FIXME: not implemeted yet */
626 { "Auto Gain Control", USB_MIXER_BOOLEAN
},
627 { "Delay Control", USB_MIXER_U16
},
628 { "Bass Boost", USB_MIXER_BOOLEAN
},
629 { "Loudness", USB_MIXER_BOOLEAN
},
633 /* private_free callback */
634 static void usb_mixer_elem_free(snd_kcontrol_t
*kctl
)
636 if (kctl
->private_data
) {
637 kfree(kctl
->private_data
);
638 kctl
->private_data
= NULL
;
644 * interface to ALSA control for feature/mixer units
648 * retrieve the minimum and maximum values for the specified control
650 static int get_min_max(usb_mixer_elem_info_t
*cval
, int default_min
)
653 cval
->min
= default_min
;
654 cval
->max
= cval
->min
+ 1;
657 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
658 cval
->val_type
== USB_MIXER_INV_BOOLEAN
) {
659 cval
->initialized
= 1;
664 for (i
= 0; i
< MAX_CHANNELS
; i
++)
665 if (cval
->cmask
& (1 << i
)) {
670 if (get_ctl_value(cval
, GET_MAX
, (cval
->control
<< 8) | minchn
, &cval
->max
) < 0 ||
671 get_ctl_value(cval
, GET_MIN
, (cval
->control
<< 8) | minchn
, &cval
->min
) < 0) {
672 snd_printd(KERN_ERR
"%d:%d: cannot get min/max values for control %d (id %d)\n",
673 cval
->id
, cval
->mixer
->ctrlif
, cval
->control
, cval
->id
);
676 if (get_ctl_value(cval
, GET_RES
, (cval
->control
<< 8) | minchn
, &cval
->res
) < 0) {
679 int last_valid_res
= cval
->res
;
681 while (cval
->res
> 1) {
682 if (set_ctl_value(cval
, SET_RES
, (cval
->control
<< 8) | minchn
, cval
->res
/ 2) < 0)
686 if (get_ctl_value(cval
, GET_RES
, (cval
->control
<< 8) | minchn
, &cval
->res
) < 0)
687 cval
->res
= last_valid_res
;
691 cval
->initialized
= 1;
697 /* get a feature/mixer unit info */
698 static int mixer_ctl_feature_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
700 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
702 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
703 cval
->val_type
== USB_MIXER_INV_BOOLEAN
)
704 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
706 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
707 uinfo
->count
= cval
->channels
;
708 if (cval
->val_type
== USB_MIXER_BOOLEAN
||
709 cval
->val_type
== USB_MIXER_INV_BOOLEAN
) {
710 uinfo
->value
.integer
.min
= 0;
711 uinfo
->value
.integer
.max
= 1;
713 if (! cval
->initialized
)
714 get_min_max(cval
, 0);
715 uinfo
->value
.integer
.min
= 0;
716 uinfo
->value
.integer
.max
= (cval
->max
- cval
->min
) / cval
->res
;
721 /* get the current value from feature/mixer unit */
722 static int mixer_ctl_feature_get(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
724 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
725 int c
, cnt
, val
, err
;
729 for (c
= 0; c
< MAX_CHANNELS
; c
++) {
730 if (cval
->cmask
& (1 << c
)) {
731 err
= get_cur_mix_value(cval
, c
+ 1, &val
);
733 if (cval
->mixer
->ignore_ctl_error
) {
734 ucontrol
->value
.integer
.value
[0] = cval
->min
;
737 snd_printd(KERN_ERR
"cannot get current value for control %d ch %d: err = %d\n", cval
->control
, c
+ 1, err
);
740 val
= get_relative_value(cval
, val
);
741 ucontrol
->value
.integer
.value
[cnt
] = val
;
747 err
= get_cur_mix_value(cval
, 0, &val
);
749 if (cval
->mixer
->ignore_ctl_error
) {
750 ucontrol
->value
.integer
.value
[0] = cval
->min
;
753 snd_printd(KERN_ERR
"cannot get current value for control %d master ch: err = %d\n", cval
->control
, err
);
756 val
= get_relative_value(cval
, val
);
757 ucontrol
->value
.integer
.value
[0] = val
;
762 /* put the current value to feature/mixer unit */
763 static int mixer_ctl_feature_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
765 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
766 int c
, cnt
, val
, oval
, err
;
771 for (c
= 0; c
< MAX_CHANNELS
; c
++) {
772 if (cval
->cmask
& (1 << c
)) {
773 err
= get_cur_mix_value(cval
, c
+ 1, &oval
);
775 if (cval
->mixer
->ignore_ctl_error
)
779 val
= ucontrol
->value
.integer
.value
[cnt
];
780 val
= get_abs_value(cval
, val
);
782 set_cur_mix_value(cval
, c
+ 1, val
);
785 get_cur_mix_value(cval
, c
+ 1, &val
);
791 err
= get_cur_mix_value(cval
, 0, &oval
);
792 if (err
< 0 && cval
->mixer
->ignore_ctl_error
)
796 val
= ucontrol
->value
.integer
.value
[0];
797 val
= get_abs_value(cval
, val
);
799 set_cur_mix_value(cval
, 0, val
);
806 static snd_kcontrol_new_t usb_feature_unit_ctl
= {
807 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
808 .name
= "", /* will be filled later manually */
809 .info
= mixer_ctl_feature_info
,
810 .get
= mixer_ctl_feature_get
,
811 .put
= mixer_ctl_feature_put
,
816 * build a feature control
819 static void build_feature_ctl(mixer_build_t
*state
, unsigned char *desc
,
820 unsigned int ctl_mask
, int control
,
821 usb_audio_term_t
*iterm
, int unitid
)
823 unsigned int len
= 0;
825 int nameid
= desc
[desc
[0] - 1];
826 snd_kcontrol_t
*kctl
;
827 usb_mixer_elem_info_t
*cval
;
829 control
++; /* change from zero-based to 1-based value */
831 if (control
== USB_FEATURE_GEQ
) {
832 /* FIXME: not supported yet */
836 if (check_ignored_ctl(state
, unitid
, control
))
839 cval
= kcalloc(1, sizeof(*cval
), GFP_KERNEL
);
841 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
844 cval
->mixer
= state
->mixer
;
846 cval
->control
= control
;
847 cval
->cmask
= ctl_mask
;
848 cval
->val_type
= audio_feature_info
[control
-1].type
;
850 cval
->channels
= 1; /* master channel */
853 for (i
= 0; i
< 16; i
++)
854 if (ctl_mask
& (1 << i
))
859 /* get min/max values */
860 get_min_max(cval
, 0);
862 kctl
= snd_ctl_new1(&usb_feature_unit_ctl
, cval
);
864 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
868 kctl
->private_free
= usb_mixer_elem_free
;
870 len
= check_mapped_name(state
, unitid
, control
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
871 mapped_name
= len
!= 0;
873 len
= snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
876 case USB_FEATURE_MUTE
:
877 case USB_FEATURE_VOLUME
:
878 /* determine the control name. the rule is:
879 * - if a name id is given in descriptor, use it.
880 * - if the connected input can be determined, then use the name
882 * - if the connected output can be determined, use it.
883 * - otherwise, anonymous name.
886 len
= get_term_name(state
, iterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 1);
888 len
= get_term_name(state
, &state
->oterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 1);
890 len
= snprintf(kctl
->id
.name
, sizeof(kctl
->id
.name
),
891 "Feature %d", unitid
);
893 /* determine the stream direction:
894 * if the connected output is USB stream, then it's likely a
895 * capture stream. otherwise it should be playback (hopefully :)
897 if (! mapped_name
&& ! (state
->oterm
.type
>> 16)) {
898 if ((state
->oterm
.type
& 0xff00) == 0x0100) {
899 len
= strlcat(kctl
->id
.name
, " Capture", sizeof(kctl
->id
.name
));
901 len
= strlcat(kctl
->id
.name
+ len
, " Playback", sizeof(kctl
->id
.name
));
904 strlcat(kctl
->id
.name
+ len
, control
== USB_FEATURE_MUTE
? " Switch" : " Volume",
905 sizeof(kctl
->id
.name
));
910 strlcpy(kctl
->id
.name
, audio_feature_info
[control
-1].name
,
911 sizeof(kctl
->id
.name
));
915 /* quirk for UDA1321/N101 */
916 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
917 /* is not very clear from datasheets */
918 /* I hope that the min value is -15360 for newer firmware --jk */
919 switch (state
->chip
->usb_id
) {
920 case USB_ID(0x0471, 0x0101):
921 case USB_ID(0x0471, 0x0104):
922 case USB_ID(0x0471, 0x0105):
923 case USB_ID(0x0672, 0x1041):
924 if (!strcmp(kctl
->id
.name
, "PCM Playback Volume") &&
925 cval
->min
== -15616) {
926 snd_printk("using volume control quirk for the UDA1321/N101 chip\n");
931 snd_printdd(KERN_INFO
"[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
932 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
, cval
->res
);
933 add_control_to_empty(state
, kctl
);
939 * parse a feature unit
941 * most of controlls are defined here.
943 static int parse_audio_feature_unit(mixer_build_t
*state
, int unitid
, unsigned char *ftr
)
946 usb_audio_term_t iterm
;
947 unsigned int master_bits
, first_ch_bits
;
950 if (ftr
[0] < 7 || ! (csize
= ftr
[5]) || ftr
[0] < 7 + csize
) {
951 snd_printk(KERN_ERR
"usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid
);
955 /* parse the source unit */
956 if ((err
= parse_audio_unit(state
, ftr
[4])) < 0)
959 /* determine the input source type and name */
960 if (check_input_term(state
, ftr
[4], &iterm
) < 0)
963 channels
= (ftr
[0] - 7) / csize
- 1;
965 master_bits
= snd_usb_combine_bytes(ftr
+ 6, csize
);
967 first_ch_bits
= snd_usb_combine_bytes(ftr
+ 6 + csize
, csize
);
970 /* check all control types */
971 for (i
= 0; i
< 10; i
++) {
972 unsigned int ch_bits
= 0;
973 for (j
= 0; j
< channels
; j
++) {
974 unsigned int mask
= snd_usb_combine_bytes(ftr
+ 6 + csize
* (j
+1), csize
);
978 if (ch_bits
& 1) /* the first channel must be set (for ease of programming) */
979 build_feature_ctl(state
, ftr
, ch_bits
, i
, &iterm
, unitid
);
980 if (master_bits
& (1 << i
))
981 build_feature_ctl(state
, ftr
, 0, i
, &iterm
, unitid
);
993 * build a mixer unit control
995 * the callbacks are identical with feature unit.
996 * input channel number (zero based) is given in control field instead.
999 static void build_mixer_unit_ctl(mixer_build_t
*state
, unsigned char *desc
,
1000 int in_pin
, int in_ch
, int unitid
,
1001 usb_audio_term_t
*iterm
)
1003 usb_mixer_elem_info_t
*cval
;
1004 unsigned int input_pins
= desc
[4];
1005 unsigned int num_outs
= desc
[5 + input_pins
];
1006 unsigned int i
, len
;
1007 snd_kcontrol_t
*kctl
;
1009 if (check_ignored_ctl(state
, unitid
, 0))
1012 cval
= kcalloc(1, sizeof(*cval
), GFP_KERNEL
);
1016 cval
->mixer
= state
->mixer
;
1018 cval
->control
= in_ch
+ 1; /* based on 1 */
1019 cval
->val_type
= USB_MIXER_S16
;
1020 for (i
= 0; i
< num_outs
; i
++) {
1021 if (check_matrix_bitmap(desc
+ 9 + input_pins
, in_ch
, i
, num_outs
)) {
1022 cval
->cmask
|= (1 << i
);
1027 /* get min/max values */
1028 get_min_max(cval
, 0);
1030 kctl
= snd_ctl_new1(&usb_feature_unit_ctl
, cval
);
1032 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1036 kctl
->private_free
= usb_mixer_elem_free
;
1038 len
= check_mapped_name(state
, unitid
, 0, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1040 len
= get_term_name(state
, iterm
, kctl
->id
.name
, sizeof(kctl
->id
.name
), 0);
1042 len
= sprintf(kctl
->id
.name
, "Mixer Source %d", in_ch
+ 1);
1043 strlcat(kctl
->id
.name
+ len
, " Volume", sizeof(kctl
->id
.name
));
1045 snd_printdd(KERN_INFO
"[%d] MU [%s] ch = %d, val = %d/%d\n",
1046 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
);
1047 add_control_to_empty(state
, kctl
);
1052 * parse a mixer unit
1054 static int parse_audio_mixer_unit(mixer_build_t
*state
, int unitid
, unsigned char *desc
)
1056 usb_audio_term_t iterm
;
1057 int input_pins
, num_ins
, num_outs
;
1060 if (desc
[0] < 11 || ! (input_pins
= desc
[4]) || ! (num_outs
= desc
[5 + input_pins
])) {
1061 snd_printk(KERN_ERR
"invalid MIXER UNIT descriptor %d\n", unitid
);
1064 /* no bmControls field (e.g. Maya44) -> ignore */
1065 if (desc
[0] <= 10 + input_pins
) {
1066 snd_printdd(KERN_INFO
"MU %d has no bmControls field\n", unitid
);
1072 for (pin
= 0; pin
< input_pins
; pin
++) {
1073 err
= parse_audio_unit(state
, desc
[5 + pin
]);
1076 err
= check_input_term(state
, desc
[5 + pin
], &iterm
);
1079 num_ins
+= iterm
.channels
;
1080 for (; ich
< num_ins
; ++ich
) {
1081 int och
, ich_has_controls
= 0;
1083 for (och
= 0; och
< num_outs
; ++och
) {
1084 if (check_matrix_bitmap(desc
+ 9 + input_pins
,
1085 ich
, och
, num_outs
)) {
1086 ich_has_controls
= 1;
1090 if (ich_has_controls
)
1091 build_mixer_unit_ctl(state
, desc
, pin
, ich
,
1100 * Processing Unit / Extension Unit
1103 /* get callback for processing/extension unit */
1104 static int mixer_ctl_procunit_get(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1106 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1109 err
= get_cur_ctl_value(cval
, cval
->control
<< 8, &val
);
1110 if (err
< 0 && cval
->mixer
->ignore_ctl_error
) {
1111 ucontrol
->value
.integer
.value
[0] = cval
->min
;
1116 val
= get_relative_value(cval
, val
);
1117 ucontrol
->value
.integer
.value
[0] = val
;
1121 /* put callback for processing/extension unit */
1122 static int mixer_ctl_procunit_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1124 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1127 err
= get_cur_ctl_value(cval
, cval
->control
<< 8, &oval
);
1129 if (cval
->mixer
->ignore_ctl_error
)
1133 val
= ucontrol
->value
.integer
.value
[0];
1134 val
= get_abs_value(cval
, val
);
1136 set_cur_ctl_value(cval
, cval
->control
<< 8, val
);
1142 /* alsa control interface for processing/extension unit */
1143 static snd_kcontrol_new_t mixer_procunit_ctl
= {
1144 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1145 .name
= "", /* will be filled later */
1146 .info
= mixer_ctl_feature_info
,
1147 .get
= mixer_ctl_procunit_get
,
1148 .put
= mixer_ctl_procunit_put
,
1153 * predefined data for processing units
1155 struct procunit_value_info
{
1162 struct procunit_info
{
1165 struct procunit_value_info
*values
;
1168 static struct procunit_value_info updown_proc_info
[] = {
1169 { USB_PROC_UPDOWN_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1170 { USB_PROC_UPDOWN_MODE_SEL
, "Mode Select", USB_MIXER_U8
, 1 },
1173 static struct procunit_value_info prologic_proc_info
[] = {
1174 { USB_PROC_PROLOGIC_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1175 { USB_PROC_PROLOGIC_MODE_SEL
, "Mode Select", USB_MIXER_U8
, 1 },
1178 static struct procunit_value_info threed_enh_proc_info
[] = {
1179 { USB_PROC_3DENH_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1180 { USB_PROC_3DENH_SPACE
, "Spaciousness", USB_MIXER_U8
},
1183 static struct procunit_value_info reverb_proc_info
[] = {
1184 { USB_PROC_REVERB_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1185 { USB_PROC_REVERB_LEVEL
, "Level", USB_MIXER_U8
},
1186 { USB_PROC_REVERB_TIME
, "Time", USB_MIXER_U16
},
1187 { USB_PROC_REVERB_DELAY
, "Delay", USB_MIXER_U8
},
1190 static struct procunit_value_info chorus_proc_info
[] = {
1191 { USB_PROC_CHORUS_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1192 { USB_PROC_CHORUS_LEVEL
, "Level", USB_MIXER_U8
},
1193 { USB_PROC_CHORUS_RATE
, "Rate", USB_MIXER_U16
},
1194 { USB_PROC_CHORUS_DEPTH
, "Depth", USB_MIXER_U16
},
1197 static struct procunit_value_info dcr_proc_info
[] = {
1198 { USB_PROC_DCR_SWITCH
, "Switch", USB_MIXER_BOOLEAN
},
1199 { USB_PROC_DCR_RATIO
, "Ratio", USB_MIXER_U16
},
1200 { USB_PROC_DCR_MAX_AMP
, "Max Amp", USB_MIXER_S16
},
1201 { USB_PROC_DCR_THRESHOLD
, "Threshold", USB_MIXER_S16
},
1202 { USB_PROC_DCR_ATTACK
, "Attack Time", USB_MIXER_U16
},
1203 { USB_PROC_DCR_RELEASE
, "Release Time", USB_MIXER_U16
},
1207 static struct procunit_info procunits
[] = {
1208 { USB_PROC_UPDOWN
, "Up Down", updown_proc_info
},
1209 { USB_PROC_PROLOGIC
, "Dolby Prologic", prologic_proc_info
},
1210 { USB_PROC_3DENH
, "3D Stereo Extender", threed_enh_proc_info
},
1211 { USB_PROC_REVERB
, "Reverb", reverb_proc_info
},
1212 { USB_PROC_CHORUS
, "Chorus", chorus_proc_info
},
1213 { USB_PROC_DCR
, "DCR", dcr_proc_info
},
1218 * build a processing/extension unit
1220 static int build_audio_procunit(mixer_build_t
*state
, int unitid
, unsigned char *dsc
, struct procunit_info
*list
, char *name
)
1222 int num_ins
= dsc
[6];
1223 usb_mixer_elem_info_t
*cval
;
1224 snd_kcontrol_t
*kctl
;
1225 int i
, err
, nameid
, type
, len
;
1226 struct procunit_info
*info
;
1227 struct procunit_value_info
*valinfo
;
1228 static struct procunit_value_info default_value_info
[] = {
1229 { 0x01, "Switch", USB_MIXER_BOOLEAN
},
1232 static struct procunit_info default_info
= {
1233 0, NULL
, default_value_info
1236 if (dsc
[0] < 13 || dsc
[0] < 13 + num_ins
|| dsc
[0] < num_ins
+ dsc
[11 + num_ins
]) {
1237 snd_printk(KERN_ERR
"invalid %s descriptor (id %d)\n", name
, unitid
);
1241 for (i
= 0; i
< num_ins
; i
++) {
1242 if ((err
= parse_audio_unit(state
, dsc
[7 + i
])) < 0)
1246 type
= combine_word(&dsc
[4]);
1247 for (info
= list
; info
&& info
->type
; info
++)
1248 if (info
->type
== type
)
1250 if (! info
|| ! info
->type
)
1251 info
= &default_info
;
1253 for (valinfo
= info
->values
; valinfo
->control
; valinfo
++) {
1254 /* FIXME: bitmap might be longer than 8bit */
1255 if (! (dsc
[12 + num_ins
] & (1 << (valinfo
->control
- 1))))
1257 if (check_ignored_ctl(state
, unitid
, valinfo
->control
))
1259 cval
= kcalloc(1, sizeof(*cval
), GFP_KERNEL
);
1261 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1264 cval
->mixer
= state
->mixer
;
1266 cval
->control
= valinfo
->control
;
1267 cval
->val_type
= valinfo
->val_type
;
1270 /* get min/max values */
1271 if (type
== USB_PROC_UPDOWN
&& cval
->control
== USB_PROC_UPDOWN_MODE_SEL
) {
1272 /* FIXME: hard-coded */
1274 cval
->max
= dsc
[15];
1276 cval
->initialized
= 1;
1278 get_min_max(cval
, valinfo
->min_value
);
1280 kctl
= snd_ctl_new1(&mixer_procunit_ctl
, cval
);
1282 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1286 kctl
->private_free
= usb_mixer_elem_free
;
1288 if (check_mapped_name(state
, unitid
, cval
->control
, kctl
->id
.name
, sizeof(kctl
->id
.name
)))
1290 else if (info
->name
)
1291 strlcpy(kctl
->id
.name
, info
->name
, sizeof(kctl
->id
.name
));
1293 nameid
= dsc
[12 + num_ins
+ dsc
[11 + num_ins
]];
1296 len
= snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1298 strlcpy(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
));
1300 strlcat(kctl
->id
.name
, " ", sizeof(kctl
->id
.name
));
1301 strlcat(kctl
->id
.name
, valinfo
->suffix
, sizeof(kctl
->id
.name
));
1303 snd_printdd(KERN_INFO
"[%d] PU [%s] ch = %d, val = %d/%d\n",
1304 cval
->id
, kctl
->id
.name
, cval
->channels
, cval
->min
, cval
->max
);
1305 if ((err
= add_control_to_empty(state
, kctl
)) < 0)
1312 static int parse_audio_processing_unit(mixer_build_t
*state
, int unitid
, unsigned char *desc
)
1314 return build_audio_procunit(state
, unitid
, desc
, procunits
, "Processing Unit");
1317 static int parse_audio_extension_unit(mixer_build_t
*state
, int unitid
, unsigned char *desc
)
1319 return build_audio_procunit(state
, unitid
, desc
, NULL
, "Extension Unit");
1327 /* info callback for selector unit
1328 * use an enumerator type for routing
1330 static int mixer_ctl_selector_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
1332 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1333 char **itemlist
= (char **)kcontrol
->private_value
;
1335 snd_assert(itemlist
, return -EINVAL
);
1336 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1338 uinfo
->value
.enumerated
.items
= cval
->max
;
1339 if ((int)uinfo
->value
.enumerated
.item
>= cval
->max
)
1340 uinfo
->value
.enumerated
.item
= cval
->max
- 1;
1341 strcpy(uinfo
->value
.enumerated
.name
, itemlist
[uinfo
->value
.enumerated
.item
]);
1345 /* get callback for selector unit */
1346 static int mixer_ctl_selector_get(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1348 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1351 err
= get_cur_ctl_value(cval
, 0, &val
);
1353 if (cval
->mixer
->ignore_ctl_error
) {
1354 ucontrol
->value
.enumerated
.item
[0] = 0;
1359 val
= get_relative_value(cval
, val
);
1360 ucontrol
->value
.enumerated
.item
[0] = val
;
1364 /* put callback for selector unit */
1365 static int mixer_ctl_selector_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
1367 usb_mixer_elem_info_t
*cval
= kcontrol
->private_data
;
1370 err
= get_cur_ctl_value(cval
, 0, &oval
);
1372 if (cval
->mixer
->ignore_ctl_error
)
1376 val
= ucontrol
->value
.enumerated
.item
[0];
1377 val
= get_abs_value(cval
, val
);
1379 set_cur_ctl_value(cval
, 0, val
);
1385 /* alsa control interface for selector unit */
1386 static snd_kcontrol_new_t mixer_selectunit_ctl
= {
1387 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1388 .name
= "", /* will be filled later */
1389 .info
= mixer_ctl_selector_info
,
1390 .get
= mixer_ctl_selector_get
,
1391 .put
= mixer_ctl_selector_put
,
1395 /* private free callback.
1396 * free both private_data and private_value
1398 static void usb_mixer_selector_elem_free(snd_kcontrol_t
*kctl
)
1402 if (kctl
->private_data
) {
1403 usb_mixer_elem_info_t
*cval
= kctl
->private_data
;
1404 num_ins
= cval
->max
;
1406 kctl
->private_data
= NULL
;
1408 if (kctl
->private_value
) {
1409 char **itemlist
= (char **)kctl
->private_value
;
1410 for (i
= 0; i
< num_ins
; i
++)
1413 kctl
->private_value
= 0;
1418 * parse a selector unit
1420 static int parse_audio_selector_unit(mixer_build_t
*state
, int unitid
, unsigned char *desc
)
1422 unsigned int num_ins
= desc
[4];
1423 unsigned int i
, nameid
, len
;
1425 usb_mixer_elem_info_t
*cval
;
1426 snd_kcontrol_t
*kctl
;
1429 if (! num_ins
|| desc
[0] < 6 + num_ins
) {
1430 snd_printk(KERN_ERR
"invalid SELECTOR UNIT descriptor %d\n", unitid
);
1434 for (i
= 0; i
< num_ins
; i
++) {
1435 if ((err
= parse_audio_unit(state
, desc
[5 + i
])) < 0)
1439 if (num_ins
== 1) /* only one ? nonsense! */
1442 if (check_ignored_ctl(state
, unitid
, 0))
1445 cval
= kcalloc(1, sizeof(*cval
), GFP_KERNEL
);
1447 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1450 cval
->mixer
= state
->mixer
;
1452 cval
->val_type
= USB_MIXER_U8
;
1455 cval
->max
= num_ins
;
1457 cval
->initialized
= 1;
1459 namelist
= kmalloc(sizeof(char *) * num_ins
, GFP_KERNEL
);
1461 snd_printk(KERN_ERR
"cannot malloc\n");
1465 #define MAX_ITEM_NAME_LEN 64
1466 for (i
= 0; i
< num_ins
; i
++) {
1467 usb_audio_term_t iterm
;
1469 namelist
[i
] = kmalloc(MAX_ITEM_NAME_LEN
, GFP_KERNEL
);
1470 if (! namelist
[i
]) {
1471 snd_printk(KERN_ERR
"cannot malloc\n");
1478 len
= check_mapped_selector_name(state
, unitid
, i
, namelist
[i
],
1480 if (! len
&& check_input_term(state
, desc
[5 + i
], &iterm
) >= 0)
1481 len
= get_term_name(state
, &iterm
, namelist
[i
], MAX_ITEM_NAME_LEN
, 0);
1483 sprintf(namelist
[i
], "Input %d", i
);
1486 kctl
= snd_ctl_new1(&mixer_selectunit_ctl
, cval
);
1488 snd_printk(KERN_ERR
"cannot malloc kcontrol\n");
1492 kctl
->private_value
= (unsigned long)namelist
;
1493 kctl
->private_free
= usb_mixer_selector_elem_free
;
1495 nameid
= desc
[desc
[0] - 1];
1496 len
= check_mapped_name(state
, unitid
, 0, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1500 snd_usb_copy_string_desc(state
, nameid
, kctl
->id
.name
, sizeof(kctl
->id
.name
));
1502 len
= get_term_name(state
, &state
->oterm
,
1503 kctl
->id
.name
, sizeof(kctl
->id
.name
), 0);
1505 strlcpy(kctl
->id
.name
, "USB", sizeof(kctl
->id
.name
));
1507 if ((state
->oterm
.type
& 0xff00) == 0x0100)
1508 strlcat(kctl
->id
.name
, " Capture Source", sizeof(kctl
->id
.name
));
1510 strlcat(kctl
->id
.name
, " Playback Source", sizeof(kctl
->id
.name
));
1513 snd_printdd(KERN_INFO
"[%d] SU [%s] items = %d\n",
1514 cval
->id
, kctl
->id
.name
, num_ins
);
1515 if ((err
= add_control_to_empty(state
, kctl
)) < 0)
1523 * parse an audio unit recursively
1526 static int parse_audio_unit(mixer_build_t
*state
, int unitid
)
1530 if (test_and_set_bit(unitid
, state
->unitbitmap
))
1531 return 0; /* the unit already visited */
1533 p1
= find_audio_control_unit(state
, unitid
);
1535 snd_printk(KERN_ERR
"usbaudio: unit %d not found!\n", unitid
);
1540 case INPUT_TERMINAL
:
1543 return parse_audio_mixer_unit(state
, unitid
, p1
);
1545 return parse_audio_selector_unit(state
, unitid
, p1
);
1547 return parse_audio_feature_unit(state
, unitid
, p1
);
1548 case PROCESSING_UNIT
:
1549 return parse_audio_processing_unit(state
, unitid
, p1
);
1550 case EXTENSION_UNIT
:
1551 return parse_audio_extension_unit(state
, unitid
, p1
);
1553 snd_printk(KERN_ERR
"usbaudio: unit %u: unexpected type 0x%02x\n", unitid
, p1
[2]);
1558 static void snd_usb_mixer_free(struct usb_mixer_interface
*mixer
)
1560 kfree(mixer
->id_elems
);
1562 kfree(mixer
->urb
->transfer_buffer
);
1563 usb_free_urb(mixer
->urb
);
1566 usb_free_urb(mixer
->rc_urb
);
1567 kfree(mixer
->rc_setup_packet
);
1571 static int snd_usb_mixer_dev_free(snd_device_t
*device
)
1573 struct usb_mixer_interface
*mixer
= device
->device_data
;
1574 snd_usb_mixer_free(mixer
);
1579 * create mixer controls
1581 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1583 static int snd_usb_mixer_controls(struct usb_mixer_interface
*mixer
)
1585 unsigned char *desc
;
1586 mixer_build_t state
;
1588 const struct usbmix_ctl_map
*map
;
1589 struct usb_host_interface
*hostif
;
1591 hostif
= &usb_ifnum_to_if(mixer
->chip
->dev
, mixer
->ctrlif
)->altsetting
[0];
1592 memset(&state
, 0, sizeof(state
));
1593 state
.chip
= mixer
->chip
;
1594 state
.mixer
= mixer
;
1595 state
.buffer
= hostif
->extra
;
1596 state
.buflen
= hostif
->extralen
;
1598 /* check the mapping table */
1599 for (map
= usbmix_ctl_maps
; map
->id
; map
++) {
1600 if (map
->id
== state
.chip
->usb_id
) {
1601 state
.map
= map
->map
;
1602 state
.selector_map
= map
->selector_map
;
1603 mixer
->ignore_ctl_error
= map
->ignore_ctl_error
;
1609 while ((desc
= snd_usb_find_csint_desc(hostif
->extra
, hostif
->extralen
, desc
, OUTPUT_TERMINAL
)) != NULL
) {
1611 continue; /* invalid descriptor? */
1612 set_bit(desc
[3], state
.unitbitmap
); /* mark terminal ID as visited */
1613 state
.oterm
.id
= desc
[3];
1614 state
.oterm
.type
= combine_word(&desc
[4]);
1615 state
.oterm
.name
= desc
[8];
1616 err
= parse_audio_unit(&state
, desc
[7]);
1623 static void snd_usb_mixer_notify_id(struct usb_mixer_interface
*mixer
,
1626 usb_mixer_elem_info_t
*info
;
1628 for (info
= mixer
->id_elems
[unitid
]; info
; info
= info
->next_id_elem
)
1629 snd_ctl_notify(mixer
->chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
1633 static void snd_usb_mixer_memory_change(struct usb_mixer_interface
*mixer
,
1636 /* SB remote control */
1637 if (mixer
->rc_type
!= RC_NONE
&& unitid
== 0) {
1638 /* read control code from device memory */
1639 mixer
->rc_urb
->dev
= mixer
->chip
->dev
;
1640 usb_submit_urb(mixer
->rc_urb
, GFP_ATOMIC
);
1644 static void snd_usb_mixer_status_complete(struct urb
*urb
, struct pt_regs
*regs
)
1646 struct usb_mixer_interface
*mixer
= urb
->context
;
1648 if (urb
->status
== 0) {
1649 u8
*buf
= urb
->transfer_buffer
;
1652 for (i
= urb
->actual_length
; i
>= 2; buf
+= 2, i
-= 2) {
1653 snd_printd(KERN_DEBUG
"status interrupt: %02x %02x\n",
1655 /* ignore any notifications not from the control interface */
1656 if ((buf
[0] & 0x0f) != 0)
1658 if (!(buf
[0] & 0x40))
1659 snd_usb_mixer_notify_id(mixer
, buf
[1]);
1661 snd_usb_mixer_memory_change(mixer
, buf
[1]);
1664 if (urb
->status
!= -ENOENT
&& urb
->status
!= -ECONNRESET
) {
1665 urb
->dev
= mixer
->chip
->dev
;
1666 usb_submit_urb(urb
, GFP_ATOMIC
);
1670 /* create the handler for the optional status interrupt endpoint */
1671 static int snd_usb_mixer_status_create(struct usb_mixer_interface
*mixer
)
1673 struct usb_host_interface
*hostif
;
1674 struct usb_endpoint_descriptor
*ep
;
1675 void *transfer_buffer
;
1679 hostif
= &usb_ifnum_to_if(mixer
->chip
->dev
, mixer
->ctrlif
)->altsetting
[0];
1680 /* we need one interrupt input endpoint */
1681 if (get_iface_desc(hostif
)->bNumEndpoints
< 1)
1683 ep
= get_endpoint(hostif
, 0);
1684 if ((ep
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_IN
||
1685 (ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
1688 epnum
= ep
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1689 buffer_length
= le16_to_cpu(ep
->wMaxPacketSize
);
1690 transfer_buffer
= kmalloc(buffer_length
, GFP_KERNEL
);
1691 if (!transfer_buffer
)
1693 mixer
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
1695 kfree(transfer_buffer
);
1698 usb_fill_int_urb(mixer
->urb
, mixer
->chip
->dev
,
1699 usb_rcvintpipe(mixer
->chip
->dev
, epnum
),
1700 transfer_buffer
, buffer_length
,
1701 snd_usb_mixer_status_complete
, mixer
, ep
->bInterval
);
1702 usb_submit_urb(mixer
->urb
, GFP_KERNEL
);
1706 static void snd_usb_soundblaster_remote_complete(struct urb
*urb
,
1707 struct pt_regs
*regs
)
1709 struct usb_mixer_interface
*mixer
= urb
->context
;
1711 * format of remote control data:
1713 * Audigy 2 NX: 06 80 xx 00 00 00
1715 int offset
= mixer
->rc_type
== RC_EXTIGY
? 0 : 2;
1718 if (urb
->status
< 0 || urb
->actual_length
<= offset
)
1720 code
= mixer
->rc_buffer
[offset
];
1721 /* the Mute button actually changes the mixer control */
1723 snd_usb_mixer_notify_id(mixer
, 18);
1724 mixer
->rc_code
= code
;
1726 wake_up(&mixer
->rc_waitq
);
1729 static int snd_usb_sbrc_hwdep_open(snd_hwdep_t
*hw
, struct file
*file
)
1731 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1733 if (test_and_set_bit(0, &mixer
->rc_hwdep_open
))
1738 static int snd_usb_sbrc_hwdep_release(snd_hwdep_t
*hw
, struct file
*file
)
1740 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1742 clear_bit(0, &mixer
->rc_hwdep_open
);
1743 smp_mb__after_clear_bit();
1747 static long snd_usb_sbrc_hwdep_read(snd_hwdep_t
*hw
, char __user
*buf
,
1748 long count
, loff_t
*offset
)
1750 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1754 if (count
!= 1 && count
!= 4)
1756 err
= wait_event_interruptible(mixer
->rc_waitq
,
1757 (rc_code
= xchg(&mixer
->rc_code
, 0)) != 0);
1760 err
= put_user(rc_code
, buf
);
1762 err
= put_user(rc_code
, (u32 __user
*)buf
);
1764 return err
< 0 ? err
: count
;
1767 static unsigned int snd_usb_sbrc_hwdep_poll(snd_hwdep_t
*hw
, struct file
*file
,
1770 struct usb_mixer_interface
*mixer
= hw
->private_data
;
1772 poll_wait(file
, &mixer
->rc_waitq
, wait
);
1773 return mixer
->rc_code
? POLLIN
| POLLRDNORM
: 0;
1776 static int snd_usb_sbrc_hwdep_ioctl(snd_hwdep_t
*hw
, struct file
*file
,
1777 unsigned int cmd
, unsigned long arg
)
1779 u32 __user
*argp
= (u32 __user
*)arg
;
1783 case LIRC_GET_FEATURES
:
1784 return put_user(LIRC_CAN_REC_CODE
, argp
);
1785 case LIRC_GET_REC_MODE
:
1786 return put_user(LIRC_MODE_CODE
, argp
);
1787 case LIRC_SET_REC_MODE
:
1788 if (get_user(mode
, argp
))
1790 return mode
== LIRC_MODE_CODE
? 0 : -ENOSYS
;
1795 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface
*mixer
)
1800 switch (mixer
->chip
->usb_id
) {
1801 case USB_ID(0x041e, 0x3000):
1802 mixer
->rc_type
= RC_EXTIGY
;
1805 case USB_ID(0x041e, 0x3020):
1806 mixer
->rc_type
= RC_AUDIGY2NX
;
1813 init_waitqueue_head(&mixer
->rc_waitq
);
1814 err
= snd_hwdep_new(mixer
->chip
->card
, "SB remote control", 0, &hwdep
);
1817 snprintf(hwdep
->name
, sizeof(hwdep
->name
),
1818 "%s remote control", mixer
->chip
->card
->shortname
);
1819 hwdep
->iface
= SNDRV_HWDEP_IFACE_SB_RC
;
1820 hwdep
->private_data
= mixer
;
1821 hwdep
->ops
.read
= snd_usb_sbrc_hwdep_read
;
1822 hwdep
->ops
.open
= snd_usb_sbrc_hwdep_open
;
1823 hwdep
->ops
.release
= snd_usb_sbrc_hwdep_release
;
1824 hwdep
->ops
.poll
= snd_usb_sbrc_hwdep_poll
;
1825 hwdep
->ops
.ioctl
= snd_usb_sbrc_hwdep_ioctl
;
1827 mixer
->rc_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1830 mixer
->rc_setup_packet
= kmalloc(sizeof(*mixer
->rc_setup_packet
), GFP_KERNEL
);
1831 if (!mixer
->rc_setup_packet
) {
1832 usb_free_urb(mixer
->rc_urb
);
1833 mixer
->rc_urb
= NULL
;
1836 mixer
->rc_setup_packet
->bRequestType
=
1837 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
1838 mixer
->rc_setup_packet
->bRequest
= GET_MEM
;
1839 mixer
->rc_setup_packet
->wValue
= cpu_to_le16(0);
1840 mixer
->rc_setup_packet
->wIndex
= cpu_to_le16(0);
1841 mixer
->rc_setup_packet
->wLength
= cpu_to_le16(len
);
1842 usb_fill_control_urb(mixer
->rc_urb
, mixer
->chip
->dev
,
1843 usb_rcvctrlpipe(mixer
->chip
->dev
, 0),
1844 (u8
*)mixer
->rc_setup_packet
, mixer
->rc_buffer
, len
,
1845 snd_usb_soundblaster_remote_complete
, mixer
);
1849 int snd_usb_create_mixer(snd_usb_audio_t
*chip
, int ctrlif
)
1851 static snd_device_ops_t dev_ops
= {
1852 .dev_free
= snd_usb_mixer_dev_free
1854 struct usb_mixer_interface
*mixer
;
1857 strcpy(chip
->card
->mixername
, "USB Mixer");
1859 mixer
= kcalloc(1, sizeof(*mixer
), GFP_KERNEL
);
1863 mixer
->ctrlif
= ctrlif
;
1864 #ifdef IGNORE_CTL_ERROR
1865 mixer
->ignore_ctl_error
= 1;
1867 mixer
->id_elems
= kcalloc(256, sizeof(*mixer
->id_elems
), GFP_KERNEL
);
1868 if (!mixer
->id_elems
) {
1873 if ((err
= snd_usb_mixer_controls(mixer
)) < 0 ||
1874 (err
= snd_usb_mixer_status_create(mixer
)) < 0) {
1875 snd_usb_mixer_free(mixer
);
1879 if ((err
= snd_usb_soundblaster_remote_init(mixer
)) < 0) {
1880 snd_usb_mixer_free(mixer
);
1884 err
= snd_device_new(chip
->card
, SNDRV_DEV_LOWLEVEL
, mixer
, &dev_ops
);
1886 snd_usb_mixer_free(mixer
);
1889 list_add(&mixer
->list
, &chip
->mixer_list
);
1893 void snd_usb_mixer_disconnect(struct list_head
*p
)
1895 struct usb_mixer_interface
*mixer
;
1897 mixer
= list_entry(p
, struct usb_mixer_interface
, list
);
1899 usb_kill_urb(mixer
->urb
);
1901 usb_kill_urb(mixer
->rc_urb
);