ia64: add pci_get_legacy_ide_irq()
[linux/fpc-iii.git] / sound / usb / usbmixer.c
blob0bfea72373957e2d49982b61455aa694c76143b7
1 /*
2 * (Tentative) USB Audio Driver for ALSA
4 * Mixer control part
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sound/driver.h>
30 #include <linux/bitops.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/usb.h>
36 #include <sound/core.h>
37 #include <sound/control.h>
38 #include <sound/hwdep.h>
39 #include <sound/info.h>
41 #include "usbaudio.h"
46 /* ignore error from controls - for debugging */
47 /* #define IGNORE_CTL_ERROR */
49 struct usb_mixer_interface {
50 struct snd_usb_audio *chip;
51 unsigned int ctrlif;
52 struct list_head list;
53 unsigned int ignore_ctl_error;
54 struct urb *urb;
55 struct usb_mixer_elem_info **id_elems; /* array[256], indexed by unit id */
57 /* Sound Blaster remote control stuff */
58 enum {
59 RC_NONE,
60 RC_EXTIGY,
61 RC_AUDIGY2NX,
62 } rc_type;
63 unsigned long rc_hwdep_open;
64 u32 rc_code;
65 wait_queue_head_t rc_waitq;
66 struct urb *rc_urb;
67 struct usb_ctrlrequest *rc_setup_packet;
68 u8 rc_buffer[6];
70 u8 audigy2nx_leds[3];
74 struct usb_audio_term {
75 int id;
76 int type;
77 int channels;
78 unsigned int chconfig;
79 int name;
82 struct usbmix_name_map;
84 struct mixer_build {
85 struct snd_usb_audio *chip;
86 struct usb_mixer_interface *mixer;
87 unsigned char *buffer;
88 unsigned int buflen;
89 DECLARE_BITMAP(unitbitmap, 256);
90 struct usb_audio_term oterm;
91 const struct usbmix_name_map *map;
92 const struct usbmix_selector_map *selector_map;
95 struct usb_mixer_elem_info {
96 struct usb_mixer_interface *mixer;
97 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
98 struct snd_ctl_elem_id *elem_id;
99 unsigned int id;
100 unsigned int control; /* CS or ICN (high byte) */
101 unsigned int cmask; /* channel mask bitmap: 0 = master */
102 int channels;
103 int val_type;
104 int min, max, res;
105 u8 initialized;
109 enum {
110 USB_FEATURE_NONE = 0,
111 USB_FEATURE_MUTE = 1,
112 USB_FEATURE_VOLUME,
113 USB_FEATURE_BASS,
114 USB_FEATURE_MID,
115 USB_FEATURE_TREBLE,
116 USB_FEATURE_GEQ,
117 USB_FEATURE_AGC,
118 USB_FEATURE_DELAY,
119 USB_FEATURE_BASSBOOST,
120 USB_FEATURE_LOUDNESS
123 enum {
124 USB_MIXER_BOOLEAN,
125 USB_MIXER_INV_BOOLEAN,
126 USB_MIXER_S8,
127 USB_MIXER_U8,
128 USB_MIXER_S16,
129 USB_MIXER_U16,
132 enum {
133 USB_PROC_UPDOWN = 1,
134 USB_PROC_UPDOWN_SWITCH = 1,
135 USB_PROC_UPDOWN_MODE_SEL = 2,
137 USB_PROC_PROLOGIC = 2,
138 USB_PROC_PROLOGIC_SWITCH = 1,
139 USB_PROC_PROLOGIC_MODE_SEL = 2,
141 USB_PROC_3DENH = 3,
142 USB_PROC_3DENH_SWITCH = 1,
143 USB_PROC_3DENH_SPACE = 2,
145 USB_PROC_REVERB = 4,
146 USB_PROC_REVERB_SWITCH = 1,
147 USB_PROC_REVERB_LEVEL = 2,
148 USB_PROC_REVERB_TIME = 3,
149 USB_PROC_REVERB_DELAY = 4,
151 USB_PROC_CHORUS = 5,
152 USB_PROC_CHORUS_SWITCH = 1,
153 USB_PROC_CHORUS_LEVEL = 2,
154 USB_PROC_CHORUS_RATE = 3,
155 USB_PROC_CHORUS_DEPTH = 4,
157 USB_PROC_DCR = 6,
158 USB_PROC_DCR_SWITCH = 1,
159 USB_PROC_DCR_RATIO = 2,
160 USB_PROC_DCR_MAX_AMP = 3,
161 USB_PROC_DCR_THRESHOLD = 4,
162 USB_PROC_DCR_ATTACK = 5,
163 USB_PROC_DCR_RELEASE = 6,
166 #define MAX_CHANNELS 10 /* max logical channels */
170 * manual mapping of mixer names
171 * if the mixer topology is too complicated and the parsed names are
172 * ambiguous, add the entries in usbmixer_maps.c.
174 #include "usbmixer_maps.c"
176 /* get the mapped name if the unit matches */
177 static int check_mapped_name(struct mixer_build *state, int unitid, int control, char *buf, int buflen)
179 const struct usbmix_name_map *p;
181 if (! state->map)
182 return 0;
184 for (p = state->map; p->id; p++) {
185 if (p->id == unitid && p->name &&
186 (! control || ! p->control || control == p->control)) {
187 buflen--;
188 return strlcpy(buf, p->name, buflen);
191 return 0;
194 /* check whether the control should be ignored */
195 static int check_ignored_ctl(struct mixer_build *state, int unitid, int control)
197 const struct usbmix_name_map *p;
199 if (! state->map)
200 return 0;
201 for (p = state->map; p->id; p++) {
202 if (p->id == unitid && ! p->name &&
203 (! control || ! p->control || control == p->control)) {
204 // printk("ignored control %d:%d\n", unitid, control);
205 return 1;
208 return 0;
211 /* get the mapped selector source name */
212 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
213 int index, char *buf, int buflen)
215 const struct usbmix_selector_map *p;
217 if (! state->selector_map)
218 return 0;
219 for (p = state->selector_map; p->id; p++) {
220 if (p->id == unitid && index < p->count)
221 return strlcpy(buf, p->names[index], buflen);
223 return 0;
227 * find an audio control unit with the given unit id
229 static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
231 unsigned char *p;
233 p = NULL;
234 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
235 USB_DT_CS_INTERFACE)) != NULL) {
236 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
237 return p;
239 return NULL;
244 * copy a string with the given id
246 static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
248 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
249 buf[len] = 0;
250 return len;
254 * convert from the byte/word on usb descriptor to the zero-based integer
256 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
258 switch (cval->val_type) {
259 case USB_MIXER_BOOLEAN:
260 return !!val;
261 case USB_MIXER_INV_BOOLEAN:
262 return !val;
263 case USB_MIXER_U8:
264 val &= 0xff;
265 break;
266 case USB_MIXER_S8:
267 val &= 0xff;
268 if (val >= 0x80)
269 val -= 0x100;
270 break;
271 case USB_MIXER_U16:
272 val &= 0xffff;
273 break;
274 case USB_MIXER_S16:
275 val &= 0xffff;
276 if (val >= 0x8000)
277 val -= 0x10000;
278 break;
280 return val;
284 * convert from the zero-based int to the byte/word for usb descriptor
286 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
288 switch (cval->val_type) {
289 case USB_MIXER_BOOLEAN:
290 return !!val;
291 case USB_MIXER_INV_BOOLEAN:
292 return !val;
293 case USB_MIXER_S8:
294 case USB_MIXER_U8:
295 return val & 0xff;
296 case USB_MIXER_S16:
297 case USB_MIXER_U16:
298 return val & 0xffff;
300 return 0; /* not reached */
303 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
305 if (! cval->res)
306 cval->res = 1;
307 if (val < cval->min)
308 return 0;
309 else if (val > cval->max)
310 return (cval->max - cval->min) / cval->res;
311 else
312 return (val - cval->min) / cval->res;
315 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
317 if (val < 0)
318 return cval->min;
319 if (! cval->res)
320 cval->res = 1;
321 val *= cval->res;
322 val += cval->min;
323 if (val > cval->max)
324 return cval->max;
325 return val;
330 * retrieve a mixer value
333 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
335 unsigned char buf[2];
336 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
337 int timeout = 10;
339 while (timeout-- > 0) {
340 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
341 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
342 request,
343 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
344 validx, cval->mixer->ctrlif | (cval->id << 8),
345 buf, val_len, 100) >= 0) {
346 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
347 return 0;
350 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
351 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
352 return -EINVAL;
355 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
357 return get_ctl_value(cval, GET_CUR, validx, value);
360 /* channel = 0: master, 1 = first channel */
361 static inline int get_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int *value)
363 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
367 * set a mixer value
370 static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
372 unsigned char buf[2];
373 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
374 int timeout = 10;
376 value_set = convert_bytes_value(cval, value_set);
377 buf[0] = value_set & 0xff;
378 buf[1] = (value_set >> 8) & 0xff;
379 while (timeout -- > 0)
380 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
381 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
382 request,
383 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
384 validx, cval->mixer->ctrlif | (cval->id << 8),
385 buf, val_len, 100) >= 0)
386 return 0;
387 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
388 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
389 return -EINVAL;
392 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
394 return set_ctl_value(cval, SET_CUR, validx, value);
397 static inline int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, int value)
399 return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
404 * parser routines begin here...
407 static int parse_audio_unit(struct mixer_build *state, int unitid);
411 * check if the input/output channel routing is enabled on the given bitmap.
412 * used for mixer unit parser
414 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
416 int idx = ich * num_outs + och;
417 return bmap[idx >> 3] & (0x80 >> (idx & 7));
422 * add an alsa control element
423 * search and increment the index until an empty slot is found.
425 * if failed, give up and free the control instance.
428 static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
430 struct usb_mixer_elem_info *cval = kctl->private_data;
431 int err;
433 while (snd_ctl_find_id(state->chip->card, &kctl->id))
434 kctl->id.index++;
435 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
436 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
437 return err;
439 cval->elem_id = &kctl->id;
440 cval->next_id_elem = state->mixer->id_elems[cval->id];
441 state->mixer->id_elems[cval->id] = cval;
442 return 0;
447 * get a terminal name string
450 static struct iterm_name_combo {
451 int type;
452 char *name;
453 } iterm_names[] = {
454 { 0x0300, "Output" },
455 { 0x0301, "Speaker" },
456 { 0x0302, "Headphone" },
457 { 0x0303, "HMD Audio" },
458 { 0x0304, "Desktop Speaker" },
459 { 0x0305, "Room Speaker" },
460 { 0x0306, "Com Speaker" },
461 { 0x0307, "LFE" },
462 { 0x0600, "External In" },
463 { 0x0601, "Analog In" },
464 { 0x0602, "Digital In" },
465 { 0x0603, "Line" },
466 { 0x0604, "Legacy In" },
467 { 0x0605, "IEC958 In" },
468 { 0x0606, "1394 DA Stream" },
469 { 0x0607, "1394 DV Stream" },
470 { 0x0700, "Embedded" },
471 { 0x0701, "Noise Source" },
472 { 0x0702, "Equalization Noise" },
473 { 0x0703, "CD" },
474 { 0x0704, "DAT" },
475 { 0x0705, "DCC" },
476 { 0x0706, "MiniDisk" },
477 { 0x0707, "Analog Tape" },
478 { 0x0708, "Phonograph" },
479 { 0x0709, "VCR Audio" },
480 { 0x070a, "Video Disk Audio" },
481 { 0x070b, "DVD Audio" },
482 { 0x070c, "TV Tuner Audio" },
483 { 0x070d, "Satellite Rec Audio" },
484 { 0x070e, "Cable Tuner Audio" },
485 { 0x070f, "DSS Audio" },
486 { 0x0710, "Radio Receiver" },
487 { 0x0711, "Radio Transmitter" },
488 { 0x0712, "Multi-Track Recorder" },
489 { 0x0713, "Synthesizer" },
490 { 0 },
493 static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
494 unsigned char *name, int maxlen, int term_only)
496 struct iterm_name_combo *names;
498 if (iterm->name)
499 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
501 /* virtual type - not a real terminal */
502 if (iterm->type >> 16) {
503 if (term_only)
504 return 0;
505 switch (iterm->type >> 16) {
506 case SELECTOR_UNIT:
507 strcpy(name, "Selector"); return 8;
508 case PROCESSING_UNIT:
509 strcpy(name, "Process Unit"); return 12;
510 case EXTENSION_UNIT:
511 strcpy(name, "Ext Unit"); return 8;
512 case MIXER_UNIT:
513 strcpy(name, "Mixer"); return 5;
514 default:
515 return sprintf(name, "Unit %d", iterm->id);
519 switch (iterm->type & 0xff00) {
520 case 0x0100:
521 strcpy(name, "PCM"); return 3;
522 case 0x0200:
523 strcpy(name, "Mic"); return 3;
524 case 0x0400:
525 strcpy(name, "Headset"); return 7;
526 case 0x0500:
527 strcpy(name, "Phone"); return 5;
530 for (names = iterm_names; names->type; names++)
531 if (names->type == iterm->type) {
532 strcpy(name, names->name);
533 return strlen(names->name);
535 return 0;
540 * parse the source unit recursively until it reaches to a terminal
541 * or a branched unit.
543 static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
545 unsigned char *p1;
547 memset(term, 0, sizeof(*term));
548 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
549 term->id = id;
550 switch (p1[2]) {
551 case INPUT_TERMINAL:
552 term->type = combine_word(p1 + 4);
553 term->channels = p1[7];
554 term->chconfig = combine_word(p1 + 8);
555 term->name = p1[11];
556 return 0;
557 case FEATURE_UNIT:
558 id = p1[4];
559 break; /* continue to parse */
560 case MIXER_UNIT:
561 term->type = p1[2] << 16; /* virtual type */
562 term->channels = p1[5 + p1[4]];
563 term->chconfig = combine_word(p1 + 6 + p1[4]);
564 term->name = p1[p1[0] - 1];
565 return 0;
566 case SELECTOR_UNIT:
567 /* call recursively to retrieve the channel info */
568 if (check_input_term(state, p1[5], term) < 0)
569 return -ENODEV;
570 term->type = p1[2] << 16; /* virtual type */
571 term->id = id;
572 term->name = p1[9 + p1[0] - 1];
573 return 0;
574 case PROCESSING_UNIT:
575 case EXTENSION_UNIT:
576 if (p1[6] == 1) {
577 id = p1[7];
578 break; /* continue to parse */
580 term->type = p1[2] << 16; /* virtual type */
581 term->channels = p1[7 + p1[6]];
582 term->chconfig = combine_word(p1 + 8 + p1[6]);
583 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
584 return 0;
585 default:
586 return -ENODEV;
589 return -ENODEV;
594 * Feature Unit
597 /* feature unit control information */
598 struct usb_feature_control_info {
599 const char *name;
600 unsigned int type; /* control type (mute, volume, etc.) */
603 static struct usb_feature_control_info audio_feature_info[] = {
604 { "Mute", USB_MIXER_INV_BOOLEAN },
605 { "Volume", USB_MIXER_S16 },
606 { "Tone Control - Bass", USB_MIXER_S8 },
607 { "Tone Control - Mid", USB_MIXER_S8 },
608 { "Tone Control - Treble", USB_MIXER_S8 },
609 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
610 { "Auto Gain Control", USB_MIXER_BOOLEAN },
611 { "Delay Control", USB_MIXER_U16 },
612 { "Bass Boost", USB_MIXER_BOOLEAN },
613 { "Loudness", USB_MIXER_BOOLEAN },
617 /* private_free callback */
618 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
620 kfree(kctl->private_data);
621 kctl->private_data = NULL;
626 * interface to ALSA control for feature/mixer units
630 * retrieve the minimum and maximum values for the specified control
632 static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
634 /* for failsafe */
635 cval->min = default_min;
636 cval->max = cval->min + 1;
637 cval->res = 1;
639 if (cval->val_type == USB_MIXER_BOOLEAN ||
640 cval->val_type == USB_MIXER_INV_BOOLEAN) {
641 cval->initialized = 1;
642 } else {
643 int minchn = 0;
644 if (cval->cmask) {
645 int i;
646 for (i = 0; i < MAX_CHANNELS; i++)
647 if (cval->cmask & (1 << i)) {
648 minchn = i + 1;
649 break;
652 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
653 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
654 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
655 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
656 return -EINVAL;
658 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
659 cval->res = 1;
660 } else {
661 int last_valid_res = cval->res;
663 while (cval->res > 1) {
664 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
665 break;
666 cval->res /= 2;
668 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
669 cval->res = last_valid_res;
671 if (cval->res == 0)
672 cval->res = 1;
673 cval->initialized = 1;
675 return 0;
679 /* get a feature/mixer unit info */
680 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
682 struct usb_mixer_elem_info *cval = kcontrol->private_data;
684 if (cval->val_type == USB_MIXER_BOOLEAN ||
685 cval->val_type == USB_MIXER_INV_BOOLEAN)
686 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
687 else
688 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
689 uinfo->count = cval->channels;
690 if (cval->val_type == USB_MIXER_BOOLEAN ||
691 cval->val_type == USB_MIXER_INV_BOOLEAN) {
692 uinfo->value.integer.min = 0;
693 uinfo->value.integer.max = 1;
694 } else {
695 if (! cval->initialized)
696 get_min_max(cval, 0);
697 uinfo->value.integer.min = 0;
698 uinfo->value.integer.max = (cval->max - cval->min) / cval->res;
700 return 0;
703 /* get the current value from feature/mixer unit */
704 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
706 struct usb_mixer_elem_info *cval = kcontrol->private_data;
707 int c, cnt, val, err;
709 if (cval->cmask) {
710 cnt = 0;
711 for (c = 0; c < MAX_CHANNELS; c++) {
712 if (cval->cmask & (1 << c)) {
713 err = get_cur_mix_value(cval, c + 1, &val);
714 if (err < 0) {
715 if (cval->mixer->ignore_ctl_error) {
716 ucontrol->value.integer.value[0] = cval->min;
717 return 0;
719 snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
720 return err;
722 val = get_relative_value(cval, val);
723 ucontrol->value.integer.value[cnt] = val;
724 cnt++;
727 } else {
728 /* master channel */
729 err = get_cur_mix_value(cval, 0, &val);
730 if (err < 0) {
731 if (cval->mixer->ignore_ctl_error) {
732 ucontrol->value.integer.value[0] = cval->min;
733 return 0;
735 snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
736 return err;
738 val = get_relative_value(cval, val);
739 ucontrol->value.integer.value[0] = val;
741 return 0;
744 /* put the current value to feature/mixer unit */
745 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
747 struct usb_mixer_elem_info *cval = kcontrol->private_data;
748 int c, cnt, val, oval, err;
749 int changed = 0;
751 if (cval->cmask) {
752 cnt = 0;
753 for (c = 0; c < MAX_CHANNELS; c++) {
754 if (cval->cmask & (1 << c)) {
755 err = get_cur_mix_value(cval, c + 1, &oval);
756 if (err < 0) {
757 if (cval->mixer->ignore_ctl_error)
758 return 0;
759 return err;
761 val = ucontrol->value.integer.value[cnt];
762 val = get_abs_value(cval, val);
763 if (oval != val) {
764 set_cur_mix_value(cval, c + 1, val);
765 changed = 1;
767 get_cur_mix_value(cval, c + 1, &val);
768 cnt++;
771 } else {
772 /* master channel */
773 err = get_cur_mix_value(cval, 0, &oval);
774 if (err < 0 && cval->mixer->ignore_ctl_error)
775 return 0;
776 if (err < 0)
777 return err;
778 val = ucontrol->value.integer.value[0];
779 val = get_abs_value(cval, val);
780 if (val != oval) {
781 set_cur_mix_value(cval, 0, val);
782 changed = 1;
785 return changed;
788 static struct snd_kcontrol_new usb_feature_unit_ctl = {
789 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
790 .name = "", /* will be filled later manually */
791 .info = mixer_ctl_feature_info,
792 .get = mixer_ctl_feature_get,
793 .put = mixer_ctl_feature_put,
798 * build a feature control
801 static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
802 unsigned int ctl_mask, int control,
803 struct usb_audio_term *iterm, int unitid)
805 unsigned int len = 0;
806 int mapped_name = 0;
807 int nameid = desc[desc[0] - 1];
808 struct snd_kcontrol *kctl;
809 struct usb_mixer_elem_info *cval;
811 control++; /* change from zero-based to 1-based value */
813 if (control == USB_FEATURE_GEQ) {
814 /* FIXME: not supported yet */
815 return;
818 if (check_ignored_ctl(state, unitid, control))
819 return;
821 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
822 if (! cval) {
823 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
824 return;
826 cval->mixer = state->mixer;
827 cval->id = unitid;
828 cval->control = control;
829 cval->cmask = ctl_mask;
830 cval->val_type = audio_feature_info[control-1].type;
831 if (ctl_mask == 0)
832 cval->channels = 1; /* master channel */
833 else {
834 int i, c = 0;
835 for (i = 0; i < 16; i++)
836 if (ctl_mask & (1 << i))
837 c++;
838 cval->channels = c;
841 /* get min/max values */
842 get_min_max(cval, 0);
844 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
845 if (! kctl) {
846 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
847 kfree(cval);
848 return;
850 kctl->private_free = usb_mixer_elem_free;
852 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
853 mapped_name = len != 0;
854 if (! len && nameid)
855 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
857 switch (control) {
858 case USB_FEATURE_MUTE:
859 case USB_FEATURE_VOLUME:
860 /* determine the control name. the rule is:
861 * - if a name id is given in descriptor, use it.
862 * - if the connected input can be determined, then use the name
863 * of terminal type.
864 * - if the connected output can be determined, use it.
865 * - otherwise, anonymous name.
867 if (! len) {
868 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
869 if (! len)
870 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
871 if (! len)
872 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
873 "Feature %d", unitid);
875 /* determine the stream direction:
876 * if the connected output is USB stream, then it's likely a
877 * capture stream. otherwise it should be playback (hopefully :)
879 if (! mapped_name && ! (state->oterm.type >> 16)) {
880 if ((state->oterm.type & 0xff00) == 0x0100) {
881 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
882 } else {
883 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
886 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
887 sizeof(kctl->id.name));
888 break;
890 default:
891 if (! len)
892 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
893 sizeof(kctl->id.name));
894 break;
897 /* quirk for UDA1321/N101 */
898 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
899 /* is not very clear from datasheets */
900 /* I hope that the min value is -15360 for newer firmware --jk */
901 switch (state->chip->usb_id) {
902 case USB_ID(0x0471, 0x0101):
903 case USB_ID(0x0471, 0x0104):
904 case USB_ID(0x0471, 0x0105):
905 case USB_ID(0x0672, 0x1041):
906 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
907 cval->min == -15616) {
908 snd_printk(KERN_INFO "using volume control quirk for the UDA1321/N101 chip\n");
909 cval->max = -256;
913 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
914 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
915 add_control_to_empty(state, kctl);
921 * parse a feature unit
923 * most of controlls are defined here.
925 static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
927 int channels, i, j;
928 struct usb_audio_term iterm;
929 unsigned int master_bits, first_ch_bits;
930 int err, csize;
932 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
933 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
934 return -EINVAL;
937 /* parse the source unit */
938 if ((err = parse_audio_unit(state, ftr[4])) < 0)
939 return err;
941 /* determine the input source type and name */
942 if (check_input_term(state, ftr[4], &iterm) < 0)
943 return -EINVAL;
945 channels = (ftr[0] - 7) / csize - 1;
947 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
948 if (channels > 0)
949 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
950 else
951 first_ch_bits = 0;
952 /* check all control types */
953 for (i = 0; i < 10; i++) {
954 unsigned int ch_bits = 0;
955 for (j = 0; j < channels; j++) {
956 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
957 if (mask & (1 << i))
958 ch_bits |= (1 << j);
960 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
961 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
962 if (master_bits & (1 << i))
963 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
966 return 0;
971 * Mixer Unit
975 * build a mixer unit control
977 * the callbacks are identical with feature unit.
978 * input channel number (zero based) is given in control field instead.
981 static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
982 int in_pin, int in_ch, int unitid,
983 struct usb_audio_term *iterm)
985 struct usb_mixer_elem_info *cval;
986 unsigned int input_pins = desc[4];
987 unsigned int num_outs = desc[5 + input_pins];
988 unsigned int i, len;
989 struct snd_kcontrol *kctl;
991 if (check_ignored_ctl(state, unitid, 0))
992 return;
994 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
995 if (! cval)
996 return;
998 cval->mixer = state->mixer;
999 cval->id = unitid;
1000 cval->control = in_ch + 1; /* based on 1 */
1001 cval->val_type = USB_MIXER_S16;
1002 for (i = 0; i < num_outs; i++) {
1003 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1004 cval->cmask |= (1 << i);
1005 cval->channels++;
1009 /* get min/max values */
1010 get_min_max(cval, 0);
1012 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1013 if (! kctl) {
1014 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1015 kfree(cval);
1016 return;
1018 kctl->private_free = usb_mixer_elem_free;
1020 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1021 if (! len)
1022 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1023 if (! len)
1024 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1025 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1027 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1028 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1029 add_control_to_empty(state, kctl);
1034 * parse a mixer unit
1036 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1038 struct usb_audio_term iterm;
1039 int input_pins, num_ins, num_outs;
1040 int pin, ich, err;
1042 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1043 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1044 return -EINVAL;
1046 /* no bmControls field (e.g. Maya44) -> ignore */
1047 if (desc[0] <= 10 + input_pins) {
1048 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1049 return 0;
1052 num_ins = 0;
1053 ich = 0;
1054 for (pin = 0; pin < input_pins; pin++) {
1055 err = parse_audio_unit(state, desc[5 + pin]);
1056 if (err < 0)
1057 return err;
1058 err = check_input_term(state, desc[5 + pin], &iterm);
1059 if (err < 0)
1060 return err;
1061 num_ins += iterm.channels;
1062 for (; ich < num_ins; ++ich) {
1063 int och, ich_has_controls = 0;
1065 for (och = 0; och < num_outs; ++och) {
1066 if (check_matrix_bitmap(desc + 9 + input_pins,
1067 ich, och, num_outs)) {
1068 ich_has_controls = 1;
1069 break;
1072 if (ich_has_controls)
1073 build_mixer_unit_ctl(state, desc, pin, ich,
1074 unitid, &iterm);
1077 return 0;
1082 * Processing Unit / Extension Unit
1085 /* get callback for processing/extension unit */
1086 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1088 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1089 int err, val;
1091 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1092 if (err < 0 && cval->mixer->ignore_ctl_error) {
1093 ucontrol->value.integer.value[0] = cval->min;
1094 return 0;
1096 if (err < 0)
1097 return err;
1098 val = get_relative_value(cval, val);
1099 ucontrol->value.integer.value[0] = val;
1100 return 0;
1103 /* put callback for processing/extension unit */
1104 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1106 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1107 int val, oval, err;
1109 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1110 if (err < 0) {
1111 if (cval->mixer->ignore_ctl_error)
1112 return 0;
1113 return err;
1115 val = ucontrol->value.integer.value[0];
1116 val = get_abs_value(cval, val);
1117 if (val != oval) {
1118 set_cur_ctl_value(cval, cval->control << 8, val);
1119 return 1;
1121 return 0;
1124 /* alsa control interface for processing/extension unit */
1125 static struct snd_kcontrol_new mixer_procunit_ctl = {
1126 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1127 .name = "", /* will be filled later */
1128 .info = mixer_ctl_feature_info,
1129 .get = mixer_ctl_procunit_get,
1130 .put = mixer_ctl_procunit_put,
1135 * predefined data for processing units
1137 struct procunit_value_info {
1138 int control;
1139 char *suffix;
1140 int val_type;
1141 int min_value;
1144 struct procunit_info {
1145 int type;
1146 char *name;
1147 struct procunit_value_info *values;
1150 static struct procunit_value_info updown_proc_info[] = {
1151 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1152 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1153 { 0 }
1155 static struct procunit_value_info prologic_proc_info[] = {
1156 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1157 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1158 { 0 }
1160 static struct procunit_value_info threed_enh_proc_info[] = {
1161 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1162 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1163 { 0 }
1165 static struct procunit_value_info reverb_proc_info[] = {
1166 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1167 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1168 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1169 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1170 { 0 }
1172 static struct procunit_value_info chorus_proc_info[] = {
1173 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1174 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1175 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1176 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1177 { 0 }
1179 static struct procunit_value_info dcr_proc_info[] = {
1180 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1181 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1182 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1183 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1184 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1185 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1186 { 0 }
1189 static struct procunit_info procunits[] = {
1190 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1191 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1192 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1193 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1194 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1195 { USB_PROC_DCR, "DCR", dcr_proc_info },
1196 { 0 },
1200 * build a processing/extension unit
1202 static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1204 int num_ins = dsc[6];
1205 struct usb_mixer_elem_info *cval;
1206 struct snd_kcontrol *kctl;
1207 int i, err, nameid, type, len;
1208 struct procunit_info *info;
1209 struct procunit_value_info *valinfo;
1210 static struct procunit_value_info default_value_info[] = {
1211 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1212 { 0 }
1214 static struct procunit_info default_info = {
1215 0, NULL, default_value_info
1218 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1219 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1220 return -EINVAL;
1223 for (i = 0; i < num_ins; i++) {
1224 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1225 return err;
1228 type = combine_word(&dsc[4]);
1229 for (info = list; info && info->type; info++)
1230 if (info->type == type)
1231 break;
1232 if (! info || ! info->type)
1233 info = &default_info;
1235 for (valinfo = info->values; valinfo->control; valinfo++) {
1236 /* FIXME: bitmap might be longer than 8bit */
1237 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1238 continue;
1239 if (check_ignored_ctl(state, unitid, valinfo->control))
1240 continue;
1241 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1242 if (! cval) {
1243 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1244 return -ENOMEM;
1246 cval->mixer = state->mixer;
1247 cval->id = unitid;
1248 cval->control = valinfo->control;
1249 cval->val_type = valinfo->val_type;
1250 cval->channels = 1;
1252 /* get min/max values */
1253 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1254 /* FIXME: hard-coded */
1255 cval->min = 1;
1256 cval->max = dsc[15];
1257 cval->res = 1;
1258 cval->initialized = 1;
1259 } else
1260 get_min_max(cval, valinfo->min_value);
1262 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1263 if (! kctl) {
1264 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1265 kfree(cval);
1266 return -ENOMEM;
1268 kctl->private_free = usb_mixer_elem_free;
1270 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1272 else if (info->name)
1273 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1274 else {
1275 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1276 len = 0;
1277 if (nameid)
1278 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1279 if (! len)
1280 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1282 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1283 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1285 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1286 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1287 if ((err = add_control_to_empty(state, kctl)) < 0)
1288 return err;
1290 return 0;
1294 static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1296 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1299 static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1301 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1306 * Selector Unit
1309 /* info callback for selector unit
1310 * use an enumerator type for routing
1312 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1314 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1315 char **itemlist = (char **)kcontrol->private_value;
1317 snd_assert(itemlist, return -EINVAL);
1318 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1319 uinfo->count = 1;
1320 uinfo->value.enumerated.items = cval->max;
1321 if ((int)uinfo->value.enumerated.item >= cval->max)
1322 uinfo->value.enumerated.item = cval->max - 1;
1323 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1324 return 0;
1327 /* get callback for selector unit */
1328 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1330 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1331 int val, err;
1333 err = get_cur_ctl_value(cval, 0, &val);
1334 if (err < 0) {
1335 if (cval->mixer->ignore_ctl_error) {
1336 ucontrol->value.enumerated.item[0] = 0;
1337 return 0;
1339 return err;
1341 val = get_relative_value(cval, val);
1342 ucontrol->value.enumerated.item[0] = val;
1343 return 0;
1346 /* put callback for selector unit */
1347 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1349 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1350 int val, oval, err;
1352 err = get_cur_ctl_value(cval, 0, &oval);
1353 if (err < 0) {
1354 if (cval->mixer->ignore_ctl_error)
1355 return 0;
1356 return err;
1358 val = ucontrol->value.enumerated.item[0];
1359 val = get_abs_value(cval, val);
1360 if (val != oval) {
1361 set_cur_ctl_value(cval, 0, val);
1362 return 1;
1364 return 0;
1367 /* alsa control interface for selector unit */
1368 static struct snd_kcontrol_new mixer_selectunit_ctl = {
1369 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1370 .name = "", /* will be filled later */
1371 .info = mixer_ctl_selector_info,
1372 .get = mixer_ctl_selector_get,
1373 .put = mixer_ctl_selector_put,
1377 /* private free callback.
1378 * free both private_data and private_value
1380 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
1382 int i, num_ins = 0;
1384 if (kctl->private_data) {
1385 struct usb_mixer_elem_info *cval = kctl->private_data;
1386 num_ins = cval->max;
1387 kfree(cval);
1388 kctl->private_data = NULL;
1390 if (kctl->private_value) {
1391 char **itemlist = (char **)kctl->private_value;
1392 for (i = 0; i < num_ins; i++)
1393 kfree(itemlist[i]);
1394 kfree(itemlist);
1395 kctl->private_value = 0;
1400 * parse a selector unit
1402 static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1404 unsigned int num_ins = desc[4];
1405 unsigned int i, nameid, len;
1406 int err;
1407 struct usb_mixer_elem_info *cval;
1408 struct snd_kcontrol *kctl;
1409 char **namelist;
1411 if (! num_ins || desc[0] < 6 + num_ins) {
1412 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1413 return -EINVAL;
1416 for (i = 0; i < num_ins; i++) {
1417 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1418 return err;
1421 if (num_ins == 1) /* only one ? nonsense! */
1422 return 0;
1424 if (check_ignored_ctl(state, unitid, 0))
1425 return 0;
1427 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1428 if (! cval) {
1429 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1430 return -ENOMEM;
1432 cval->mixer = state->mixer;
1433 cval->id = unitid;
1434 cval->val_type = USB_MIXER_U8;
1435 cval->channels = 1;
1436 cval->min = 1;
1437 cval->max = num_ins;
1438 cval->res = 1;
1439 cval->initialized = 1;
1441 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1442 if (! namelist) {
1443 snd_printk(KERN_ERR "cannot malloc\n");
1444 kfree(cval);
1445 return -ENOMEM;
1447 #define MAX_ITEM_NAME_LEN 64
1448 for (i = 0; i < num_ins; i++) {
1449 struct usb_audio_term iterm;
1450 len = 0;
1451 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1452 if (! namelist[i]) {
1453 snd_printk(KERN_ERR "cannot malloc\n");
1454 while (--i > 0)
1455 kfree(namelist[i]);
1456 kfree(namelist);
1457 kfree(cval);
1458 return -ENOMEM;
1460 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1461 MAX_ITEM_NAME_LEN);
1462 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1463 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1464 if (! len)
1465 sprintf(namelist[i], "Input %d", i);
1468 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1469 if (! kctl) {
1470 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1471 kfree(cval);
1472 return -ENOMEM;
1474 kctl->private_value = (unsigned long)namelist;
1475 kctl->private_free = usb_mixer_selector_elem_free;
1477 nameid = desc[desc[0] - 1];
1478 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1479 if (len)
1481 else if (nameid)
1482 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1483 else {
1484 len = get_term_name(state, &state->oterm,
1485 kctl->id.name, sizeof(kctl->id.name), 0);
1486 if (! len)
1487 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1489 if ((state->oterm.type & 0xff00) == 0x0100)
1490 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1491 else
1492 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1495 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1496 cval->id, kctl->id.name, num_ins);
1497 if ((err = add_control_to_empty(state, kctl)) < 0)
1498 return err;
1500 return 0;
1505 * parse an audio unit recursively
1508 static int parse_audio_unit(struct mixer_build *state, int unitid)
1510 unsigned char *p1;
1512 if (test_and_set_bit(unitid, state->unitbitmap))
1513 return 0; /* the unit already visited */
1515 p1 = find_audio_control_unit(state, unitid);
1516 if (!p1) {
1517 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1518 return -EINVAL;
1521 switch (p1[2]) {
1522 case INPUT_TERMINAL:
1523 return 0; /* NOP */
1524 case MIXER_UNIT:
1525 return parse_audio_mixer_unit(state, unitid, p1);
1526 case SELECTOR_UNIT:
1527 return parse_audio_selector_unit(state, unitid, p1);
1528 case FEATURE_UNIT:
1529 return parse_audio_feature_unit(state, unitid, p1);
1530 case PROCESSING_UNIT:
1531 return parse_audio_processing_unit(state, unitid, p1);
1532 case EXTENSION_UNIT:
1533 return parse_audio_extension_unit(state, unitid, p1);
1534 default:
1535 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1536 return -EINVAL;
1540 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1542 kfree(mixer->id_elems);
1543 if (mixer->urb) {
1544 kfree(mixer->urb->transfer_buffer);
1545 usb_free_urb(mixer->urb);
1547 if (mixer->rc_urb)
1548 usb_free_urb(mixer->rc_urb);
1549 kfree(mixer->rc_setup_packet);
1550 kfree(mixer);
1553 static int snd_usb_mixer_dev_free(struct snd_device *device)
1555 struct usb_mixer_interface *mixer = device->device_data;
1556 snd_usb_mixer_free(mixer);
1557 return 0;
1561 * create mixer controls
1563 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1565 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1567 unsigned char *desc;
1568 struct mixer_build state;
1569 int err;
1570 const struct usbmix_ctl_map *map;
1571 struct usb_host_interface *hostif;
1573 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1574 memset(&state, 0, sizeof(state));
1575 state.chip = mixer->chip;
1576 state.mixer = mixer;
1577 state.buffer = hostif->extra;
1578 state.buflen = hostif->extralen;
1580 /* check the mapping table */
1581 for (map = usbmix_ctl_maps; map->id; map++) {
1582 if (map->id == state.chip->usb_id) {
1583 state.map = map->map;
1584 state.selector_map = map->selector_map;
1585 mixer->ignore_ctl_error = map->ignore_ctl_error;
1586 break;
1590 desc = NULL;
1591 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1592 if (desc[0] < 9)
1593 continue; /* invalid descriptor? */
1594 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1595 state.oterm.id = desc[3];
1596 state.oterm.type = combine_word(&desc[4]);
1597 state.oterm.name = desc[8];
1598 err = parse_audio_unit(&state, desc[7]);
1599 if (err < 0)
1600 return err;
1602 return 0;
1605 static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1606 int unitid)
1608 struct usb_mixer_elem_info *info;
1610 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1611 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1612 info->elem_id);
1615 static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1616 int unitid)
1618 if (mixer->rc_type == RC_NONE)
1619 return;
1620 /* unit ids specific to Extigy/Audigy 2 NX: */
1621 switch (unitid) {
1622 case 0: /* remote control */
1623 mixer->rc_urb->dev = mixer->chip->dev;
1624 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1625 break;
1626 case 4: /* digital in jack */
1627 case 7: /* line in jacks */
1628 case 19: /* speaker out jacks */
1629 case 20: /* headphones out jack */
1630 break;
1631 default:
1632 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1633 break;
1637 static void snd_usb_mixer_status_complete(struct urb *urb, struct pt_regs *regs)
1639 struct usb_mixer_interface *mixer = urb->context;
1641 if (urb->status == 0) {
1642 u8 *buf = urb->transfer_buffer;
1643 int i;
1645 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1646 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1647 buf[0], buf[1]);
1648 /* ignore any notifications not from the control interface */
1649 if ((buf[0] & 0x0f) != 0)
1650 continue;
1651 if (!(buf[0] & 0x40))
1652 snd_usb_mixer_notify_id(mixer, buf[1]);
1653 else
1654 snd_usb_mixer_memory_change(mixer, buf[1]);
1657 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1658 urb->dev = mixer->chip->dev;
1659 usb_submit_urb(urb, GFP_ATOMIC);
1663 /* create the handler for the optional status interrupt endpoint */
1664 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1666 struct usb_host_interface *hostif;
1667 struct usb_endpoint_descriptor *ep;
1668 void *transfer_buffer;
1669 int buffer_length;
1670 unsigned int epnum;
1672 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1673 /* we need one interrupt input endpoint */
1674 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1675 return 0;
1676 ep = get_endpoint(hostif, 0);
1677 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1678 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1679 return 0;
1681 epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1682 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1683 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1684 if (!transfer_buffer)
1685 return -ENOMEM;
1686 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1687 if (!mixer->urb) {
1688 kfree(transfer_buffer);
1689 return -ENOMEM;
1691 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1692 usb_rcvintpipe(mixer->chip->dev, epnum),
1693 transfer_buffer, buffer_length,
1694 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1695 usb_submit_urb(mixer->urb, GFP_KERNEL);
1696 return 0;
1699 static void snd_usb_soundblaster_remote_complete(struct urb *urb,
1700 struct pt_regs *regs)
1702 struct usb_mixer_interface *mixer = urb->context;
1704 * format of remote control data:
1705 * Extigy: xx 00
1706 * Audigy 2 NX: 06 80 xx 00 00 00
1708 int offset = mixer->rc_type == RC_EXTIGY ? 0 : 2;
1709 u32 code;
1711 if (urb->status < 0 || urb->actual_length <= offset)
1712 return;
1713 code = mixer->rc_buffer[offset];
1714 /* the Mute button actually changes the mixer control */
1715 if (code == 13)
1716 snd_usb_mixer_notify_id(mixer, 18);
1717 mixer->rc_code = code;
1718 wmb();
1719 wake_up(&mixer->rc_waitq);
1722 static int snd_usb_sbrc_hwdep_open(struct snd_hwdep *hw, struct file *file)
1724 struct usb_mixer_interface *mixer = hw->private_data;
1726 if (test_and_set_bit(0, &mixer->rc_hwdep_open))
1727 return -EBUSY;
1728 return 0;
1731 static int snd_usb_sbrc_hwdep_release(struct snd_hwdep *hw, struct file *file)
1733 struct usb_mixer_interface *mixer = hw->private_data;
1735 clear_bit(0, &mixer->rc_hwdep_open);
1736 smp_mb__after_clear_bit();
1737 return 0;
1740 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
1741 long count, loff_t *offset)
1743 struct usb_mixer_interface *mixer = hw->private_data;
1744 int err;
1745 u32 rc_code;
1747 if (count != 1 && count != 4)
1748 return -EINVAL;
1749 err = wait_event_interruptible(mixer->rc_waitq,
1750 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1751 if (err == 0) {
1752 if (count == 1)
1753 err = put_user(rc_code, buf);
1754 else
1755 err = put_user(rc_code, (u32 __user *)buf);
1757 return err < 0 ? err : count;
1760 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
1761 poll_table *wait)
1763 struct usb_mixer_interface *mixer = hw->private_data;
1765 poll_wait(file, &mixer->rc_waitq, wait);
1766 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1769 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1771 struct snd_hwdep *hwdep;
1772 int err, len;
1774 switch (mixer->chip->usb_id) {
1775 case USB_ID(0x041e, 0x3000):
1776 mixer->rc_type = RC_EXTIGY;
1777 len = 2;
1778 break;
1779 case USB_ID(0x041e, 0x3020):
1780 mixer->rc_type = RC_AUDIGY2NX;
1781 len = 6;
1782 break;
1783 default:
1784 return 0;
1787 init_waitqueue_head(&mixer->rc_waitq);
1788 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1789 if (err < 0)
1790 return err;
1791 snprintf(hwdep->name, sizeof(hwdep->name),
1792 "%s remote control", mixer->chip->card->shortname);
1793 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1794 hwdep->private_data = mixer;
1795 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1796 hwdep->ops.open = snd_usb_sbrc_hwdep_open;
1797 hwdep->ops.release = snd_usb_sbrc_hwdep_release;
1798 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1800 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1801 if (!mixer->rc_urb)
1802 return -ENOMEM;
1803 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1804 if (!mixer->rc_setup_packet) {
1805 usb_free_urb(mixer->rc_urb);
1806 mixer->rc_urb = NULL;
1807 return -ENOMEM;
1809 mixer->rc_setup_packet->bRequestType =
1810 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1811 mixer->rc_setup_packet->bRequest = GET_MEM;
1812 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1813 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1814 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1815 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1816 usb_rcvctrlpipe(mixer->chip->dev, 0),
1817 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1818 snd_usb_soundblaster_remote_complete, mixer);
1819 return 0;
1822 static int snd_audigy2nx_led_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1824 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1825 uinfo->count = 1;
1826 uinfo->value.integer.min = 0;
1827 uinfo->value.integer.max = 1;
1828 return 0;
1831 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1833 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1834 int index = kcontrol->private_value;
1836 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1837 return 0;
1840 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1842 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1843 int index = kcontrol->private_value;
1844 int value = ucontrol->value.integer.value[0];
1845 int err, changed;
1847 if (value > 1)
1848 return -EINVAL;
1849 changed = value != mixer->audigy2nx_leds[index];
1850 err = snd_usb_ctl_msg(mixer->chip->dev,
1851 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1852 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1853 value, index + 2, NULL, 0, 100);
1854 if (err < 0)
1855 return err;
1856 mixer->audigy2nx_leds[index] = value;
1857 return changed;
1860 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
1862 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1863 .name = "CMSS LED Switch",
1864 .info = snd_audigy2nx_led_info,
1865 .get = snd_audigy2nx_led_get,
1866 .put = snd_audigy2nx_led_put,
1867 .private_value = 0,
1870 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1871 .name = "Power LED Switch",
1872 .info = snd_audigy2nx_led_info,
1873 .get = snd_audigy2nx_led_get,
1874 .put = snd_audigy2nx_led_put,
1875 .private_value = 1,
1878 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1879 .name = "Dolby Digital LED Switch",
1880 .info = snd_audigy2nx_led_info,
1881 .get = snd_audigy2nx_led_get,
1882 .put = snd_audigy2nx_led_put,
1883 .private_value = 2,
1887 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1889 int i, err;
1891 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
1892 err = snd_ctl_add(mixer->chip->card,
1893 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1894 if (err < 0)
1895 return err;
1897 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1898 return 0;
1901 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
1902 struct snd_info_buffer *buffer)
1904 static const struct {
1905 int unitid;
1906 const char *name;
1907 } jacks[] = {
1908 {4, "dig in "},
1909 {7, "line in"},
1910 {19, "spk out"},
1911 {20, "hph out"},
1913 struct usb_mixer_interface *mixer = entry->private_data;
1914 int i, err;
1915 u8 buf[3];
1917 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
1918 for (i = 0; i < ARRAY_SIZE(jacks); ++i) {
1919 snd_iprintf(buffer, "%s: ", jacks[i].name);
1920 err = snd_usb_ctl_msg(mixer->chip->dev,
1921 usb_rcvctrlpipe(mixer->chip->dev, 0),
1922 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
1923 USB_RECIP_INTERFACE, 0,
1924 jacks[i].unitid << 8, buf, 3, 100);
1925 if (err == 3 && buf[0] == 3)
1926 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
1927 else
1928 snd_iprintf(buffer, "?\n");
1932 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif)
1934 static struct snd_device_ops dev_ops = {
1935 .dev_free = snd_usb_mixer_dev_free
1937 struct usb_mixer_interface *mixer;
1938 int err;
1940 strcpy(chip->card->mixername, "USB Mixer");
1942 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
1943 if (!mixer)
1944 return -ENOMEM;
1945 mixer->chip = chip;
1946 mixer->ctrlif = ctrlif;
1947 #ifdef IGNORE_CTL_ERROR
1948 mixer->ignore_ctl_error = 1;
1949 #endif
1950 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
1951 if (!mixer->id_elems) {
1952 kfree(mixer);
1953 return -ENOMEM;
1956 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
1957 (err = snd_usb_mixer_status_create(mixer)) < 0)
1958 goto _error;
1960 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
1961 goto _error;
1963 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020)) {
1964 struct snd_info_entry *entry;
1966 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
1967 goto _error;
1968 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
1969 snd_info_set_text_ops(entry, mixer, 1024,
1970 snd_audigy2nx_proc_read);
1973 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
1974 if (err < 0)
1975 goto _error;
1976 list_add(&mixer->list, &chip->mixer_list);
1977 return 0;
1979 _error:
1980 snd_usb_mixer_free(mixer);
1981 return err;
1984 void snd_usb_mixer_disconnect(struct list_head *p)
1986 struct usb_mixer_interface *mixer;
1988 mixer = list_entry(p, struct usb_mixer_interface, list);
1989 if (mixer->urb)
1990 usb_kill_urb(mixer->urb);
1991 if (mixer->rc_urb)
1992 usb_kill_urb(mixer->rc_urb);