ALSA: usbmixer - use MAX_ID_ELEMS where possible
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / usb / usbmixer.c
blob35b4830fb0c4a1b1b3e4072d3214d1bce0c9d0bb
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 <linux/bitops.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/usb.h>
35 #include <sound/core.h>
36 #include <sound/control.h>
37 #include <sound/hwdep.h>
38 #include <sound/info.h>
39 #include <sound/tlv.h>
41 #include "usbaudio.h"
46 /* ignore error from controls - for debugging */
47 /* #define IGNORE_CTL_ERROR */
50 * Sound Blaster remote control configuration
52 * format of remote control data:
53 * Extigy: xx 00
54 * Audigy 2 NX: 06 80 xx 00 00 00
55 * Live! 24-bit: 06 80 xx yy 22 83
57 static const struct rc_config {
58 u32 usb_id;
59 u8 offset;
60 u8 length;
61 u8 packet_length;
62 u8 min_packet_length; /* minimum accepted length of the URB result */
63 u8 mute_mixer_id;
64 u32 mute_code;
65 } rc_configs[] = {
66 { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */
67 { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */
68 { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */
69 { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
72 #define MAX_ID_ELEMS 256
74 struct usb_mixer_interface {
75 struct snd_usb_audio *chip;
76 unsigned int ctrlif;
77 struct list_head list;
78 unsigned int ignore_ctl_error;
79 struct urb *urb;
80 /* array[MAX_ID_ELEMS], indexed by unit id */
81 struct usb_mixer_elem_info **id_elems;
83 /* Sound Blaster remote control stuff */
84 const struct rc_config *rc_cfg;
85 u32 rc_code;
86 wait_queue_head_t rc_waitq;
87 struct urb *rc_urb;
88 struct usb_ctrlrequest *rc_setup_packet;
89 u8 rc_buffer[6];
91 u8 audigy2nx_leds[3];
92 u8 xonar_u1_status;
96 struct usb_audio_term {
97 int id;
98 int type;
99 int channels;
100 unsigned int chconfig;
101 int name;
104 struct usbmix_name_map;
106 struct mixer_build {
107 struct snd_usb_audio *chip;
108 struct usb_mixer_interface *mixer;
109 unsigned char *buffer;
110 unsigned int buflen;
111 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
112 struct usb_audio_term oterm;
113 const struct usbmix_name_map *map;
114 const struct usbmix_selector_map *selector_map;
117 #define MAX_CHANNELS 10 /* max logical channels */
119 struct usb_mixer_elem_info {
120 struct usb_mixer_interface *mixer;
121 struct usb_mixer_elem_info *next_id_elem; /* list of controls with same id */
122 struct snd_ctl_elem_id *elem_id;
123 unsigned int id;
124 unsigned int control; /* CS or ICN (high byte) */
125 unsigned int cmask; /* channel mask bitmap: 0 = master */
126 int channels;
127 int val_type;
128 int min, max, res;
129 int dBmin, dBmax;
130 int cached;
131 int cache_val[MAX_CHANNELS];
132 u8 initialized;
136 enum {
137 USB_FEATURE_NONE = 0,
138 USB_FEATURE_MUTE = 1,
139 USB_FEATURE_VOLUME,
140 USB_FEATURE_BASS,
141 USB_FEATURE_MID,
142 USB_FEATURE_TREBLE,
143 USB_FEATURE_GEQ,
144 USB_FEATURE_AGC,
145 USB_FEATURE_DELAY,
146 USB_FEATURE_BASSBOOST,
147 USB_FEATURE_LOUDNESS
150 enum {
151 USB_MIXER_BOOLEAN,
152 USB_MIXER_INV_BOOLEAN,
153 USB_MIXER_S8,
154 USB_MIXER_U8,
155 USB_MIXER_S16,
156 USB_MIXER_U16,
159 enum {
160 USB_PROC_UPDOWN = 1,
161 USB_PROC_UPDOWN_SWITCH = 1,
162 USB_PROC_UPDOWN_MODE_SEL = 2,
164 USB_PROC_PROLOGIC = 2,
165 USB_PROC_PROLOGIC_SWITCH = 1,
166 USB_PROC_PROLOGIC_MODE_SEL = 2,
168 USB_PROC_3DENH = 3,
169 USB_PROC_3DENH_SWITCH = 1,
170 USB_PROC_3DENH_SPACE = 2,
172 USB_PROC_REVERB = 4,
173 USB_PROC_REVERB_SWITCH = 1,
174 USB_PROC_REVERB_LEVEL = 2,
175 USB_PROC_REVERB_TIME = 3,
176 USB_PROC_REVERB_DELAY = 4,
178 USB_PROC_CHORUS = 5,
179 USB_PROC_CHORUS_SWITCH = 1,
180 USB_PROC_CHORUS_LEVEL = 2,
181 USB_PROC_CHORUS_RATE = 3,
182 USB_PROC_CHORUS_DEPTH = 4,
184 USB_PROC_DCR = 6,
185 USB_PROC_DCR_SWITCH = 1,
186 USB_PROC_DCR_RATIO = 2,
187 USB_PROC_DCR_MAX_AMP = 3,
188 USB_PROC_DCR_THRESHOLD = 4,
189 USB_PROC_DCR_ATTACK = 5,
190 USB_PROC_DCR_RELEASE = 6,
193 /*E-mu 0202(0404) eXtension Unit(XU) control*/
194 enum {
195 USB_XU_CLOCK_RATE = 0xe301,
196 USB_XU_CLOCK_SOURCE = 0xe302,
197 USB_XU_DIGITAL_IO_STATUS = 0xe303,
198 USB_XU_DEVICE_OPTIONS = 0xe304,
199 USB_XU_DIRECT_MONITORING = 0xe305,
200 USB_XU_METERING = 0xe306
202 enum {
203 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
204 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
205 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
206 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
210 * manual mapping of mixer names
211 * if the mixer topology is too complicated and the parsed names are
212 * ambiguous, add the entries in usbmixer_maps.c.
214 #include "usbmixer_maps.c"
216 static const struct usbmix_name_map *
217 find_map(struct mixer_build *state, int unitid, int control)
219 const struct usbmix_name_map *p = state->map;
221 if (!p)
222 return NULL;
224 for (p = state->map; p->id; p++) {
225 if (p->id == unitid &&
226 (!control || !p->control || control == p->control))
227 return p;
229 return NULL;
232 /* get the mapped name if the unit matches */
233 static int
234 check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
236 if (!p || !p->name)
237 return 0;
239 buflen--;
240 return strlcpy(buf, p->name, buflen);
243 /* check whether the control should be ignored */
244 static inline int
245 check_ignored_ctl(const struct usbmix_name_map *p)
247 if (!p || p->name || p->dB)
248 return 0;
249 return 1;
252 /* dB mapping */
253 static inline void check_mapped_dB(const struct usbmix_name_map *p,
254 struct usb_mixer_elem_info *cval)
256 if (p && p->dB) {
257 cval->dBmin = p->dB->min;
258 cval->dBmax = p->dB->max;
262 /* get the mapped selector source name */
263 static int check_mapped_selector_name(struct mixer_build *state, int unitid,
264 int index, char *buf, int buflen)
266 const struct usbmix_selector_map *p;
268 if (! state->selector_map)
269 return 0;
270 for (p = state->selector_map; p->id; p++) {
271 if (p->id == unitid && index < p->count)
272 return strlcpy(buf, p->names[index], buflen);
274 return 0;
278 * find an audio control unit with the given unit id
280 static void *find_audio_control_unit(struct mixer_build *state, unsigned char unit)
282 unsigned char *p;
284 p = NULL;
285 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
286 USB_DT_CS_INTERFACE)) != NULL) {
287 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
288 return p;
290 return NULL;
295 * copy a string with the given id
297 static int snd_usb_copy_string_desc(struct mixer_build *state, int index, char *buf, int maxlen)
299 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
300 buf[len] = 0;
301 return len;
305 * convert from the byte/word on usb descriptor to the zero-based integer
307 static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
309 switch (cval->val_type) {
310 case USB_MIXER_BOOLEAN:
311 return !!val;
312 case USB_MIXER_INV_BOOLEAN:
313 return !val;
314 case USB_MIXER_U8:
315 val &= 0xff;
316 break;
317 case USB_MIXER_S8:
318 val &= 0xff;
319 if (val >= 0x80)
320 val -= 0x100;
321 break;
322 case USB_MIXER_U16:
323 val &= 0xffff;
324 break;
325 case USB_MIXER_S16:
326 val &= 0xffff;
327 if (val >= 0x8000)
328 val -= 0x10000;
329 break;
331 return val;
335 * convert from the zero-based int to the byte/word for usb descriptor
337 static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
339 switch (cval->val_type) {
340 case USB_MIXER_BOOLEAN:
341 return !!val;
342 case USB_MIXER_INV_BOOLEAN:
343 return !val;
344 case USB_MIXER_S8:
345 case USB_MIXER_U8:
346 return val & 0xff;
347 case USB_MIXER_S16:
348 case USB_MIXER_U16:
349 return val & 0xffff;
351 return 0; /* not reached */
354 static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
356 if (! cval->res)
357 cval->res = 1;
358 if (val < cval->min)
359 return 0;
360 else if (val >= cval->max)
361 return (cval->max - cval->min + cval->res - 1) / cval->res;
362 else
363 return (val - cval->min) / cval->res;
366 static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
368 if (val < 0)
369 return cval->min;
370 if (! cval->res)
371 cval->res = 1;
372 val *= cval->res;
373 val += cval->min;
374 if (val > cval->max)
375 return cval->max;
376 return val;
381 * retrieve a mixer value
384 static int get_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int *value_ret)
386 unsigned char buf[2];
387 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
388 int timeout = 10;
390 while (timeout-- > 0) {
391 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
392 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
393 request,
394 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
395 validx, cval->mixer->ctrlif | (cval->id << 8),
396 buf, val_len, 100) >= val_len) {
397 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
398 return 0;
401 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
402 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
403 return -EINVAL;
406 static int get_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int *value)
408 return get_ctl_value(cval, GET_CUR, validx, value);
411 /* channel = 0: master, 1 = first channel */
412 static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
413 int channel, int *value)
415 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
418 static int get_cur_mix_value(struct usb_mixer_elem_info *cval,
419 int channel, int index, int *value)
421 int err;
423 if (cval->cached & (1 << channel)) {
424 *value = cval->cache_val[index];
425 return 0;
427 err = get_cur_mix_raw(cval, channel, value);
428 if (err < 0) {
429 if (!cval->mixer->ignore_ctl_error)
430 snd_printd(KERN_ERR "cannot get current value for "
431 "control %d ch %d: err = %d\n",
432 cval->control, channel, err);
433 return err;
435 cval->cached |= 1 << channel;
436 cval->cache_val[index] = *value;
437 return 0;
442 * set a mixer value
445 static int set_ctl_value(struct usb_mixer_elem_info *cval, int request, int validx, int value_set)
447 unsigned char buf[2];
448 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
449 int timeout = 10;
451 value_set = convert_bytes_value(cval, value_set);
452 buf[0] = value_set & 0xff;
453 buf[1] = (value_set >> 8) & 0xff;
454 while (timeout-- > 0)
455 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
456 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
457 request,
458 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
459 validx, cval->mixer->ctrlif | (cval->id << 8),
460 buf, val_len, 100) >= 0)
461 return 0;
462 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
463 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
464 return -EINVAL;
467 static int set_cur_ctl_value(struct usb_mixer_elem_info *cval, int validx, int value)
469 return set_ctl_value(cval, SET_CUR, validx, value);
472 static int set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
473 int index, int value)
475 int err;
476 err = set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel,
477 value);
478 if (err < 0)
479 return err;
480 cval->cached |= 1 << channel;
481 cval->cache_val[index] = value;
482 return 0;
486 * TLV callback for mixer volume controls
488 static int mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
489 unsigned int size, unsigned int __user *_tlv)
491 struct usb_mixer_elem_info *cval = kcontrol->private_data;
492 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
494 if (size < sizeof(scale))
495 return -ENOMEM;
496 scale[2] = cval->dBmin;
497 scale[3] = cval->dBmax;
498 if (copy_to_user(_tlv, scale, sizeof(scale)))
499 return -EFAULT;
500 return 0;
504 * parser routines begin here...
507 static int parse_audio_unit(struct mixer_build *state, int unitid);
511 * check if the input/output channel routing is enabled on the given bitmap.
512 * used for mixer unit parser
514 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
516 int idx = ich * num_outs + och;
517 return bmap[idx >> 3] & (0x80 >> (idx & 7));
522 * add an alsa control element
523 * search and increment the index until an empty slot is found.
525 * if failed, give up and free the control instance.
528 static int add_control_to_empty(struct mixer_build *state, struct snd_kcontrol *kctl)
530 struct usb_mixer_elem_info *cval = kctl->private_data;
531 int err;
533 while (snd_ctl_find_id(state->chip->card, &kctl->id))
534 kctl->id.index++;
535 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
536 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
537 return err;
539 cval->elem_id = &kctl->id;
540 cval->next_id_elem = state->mixer->id_elems[cval->id];
541 state->mixer->id_elems[cval->id] = cval;
542 return 0;
547 * get a terminal name string
550 static struct iterm_name_combo {
551 int type;
552 char *name;
553 } iterm_names[] = {
554 { 0x0300, "Output" },
555 { 0x0301, "Speaker" },
556 { 0x0302, "Headphone" },
557 { 0x0303, "HMD Audio" },
558 { 0x0304, "Desktop Speaker" },
559 { 0x0305, "Room Speaker" },
560 { 0x0306, "Com Speaker" },
561 { 0x0307, "LFE" },
562 { 0x0600, "External In" },
563 { 0x0601, "Analog In" },
564 { 0x0602, "Digital In" },
565 { 0x0603, "Line" },
566 { 0x0604, "Legacy In" },
567 { 0x0605, "IEC958 In" },
568 { 0x0606, "1394 DA Stream" },
569 { 0x0607, "1394 DV Stream" },
570 { 0x0700, "Embedded" },
571 { 0x0701, "Noise Source" },
572 { 0x0702, "Equalization Noise" },
573 { 0x0703, "CD" },
574 { 0x0704, "DAT" },
575 { 0x0705, "DCC" },
576 { 0x0706, "MiniDisk" },
577 { 0x0707, "Analog Tape" },
578 { 0x0708, "Phonograph" },
579 { 0x0709, "VCR Audio" },
580 { 0x070a, "Video Disk Audio" },
581 { 0x070b, "DVD Audio" },
582 { 0x070c, "TV Tuner Audio" },
583 { 0x070d, "Satellite Rec Audio" },
584 { 0x070e, "Cable Tuner Audio" },
585 { 0x070f, "DSS Audio" },
586 { 0x0710, "Radio Receiver" },
587 { 0x0711, "Radio Transmitter" },
588 { 0x0712, "Multi-Track Recorder" },
589 { 0x0713, "Synthesizer" },
590 { 0 },
593 static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
594 unsigned char *name, int maxlen, int term_only)
596 struct iterm_name_combo *names;
598 if (iterm->name)
599 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
601 /* virtual type - not a real terminal */
602 if (iterm->type >> 16) {
603 if (term_only)
604 return 0;
605 switch (iterm->type >> 16) {
606 case SELECTOR_UNIT:
607 strcpy(name, "Selector"); return 8;
608 case PROCESSING_UNIT:
609 strcpy(name, "Process Unit"); return 12;
610 case EXTENSION_UNIT:
611 strcpy(name, "Ext Unit"); return 8;
612 case MIXER_UNIT:
613 strcpy(name, "Mixer"); return 5;
614 default:
615 return sprintf(name, "Unit %d", iterm->id);
619 switch (iterm->type & 0xff00) {
620 case 0x0100:
621 strcpy(name, "PCM"); return 3;
622 case 0x0200:
623 strcpy(name, "Mic"); return 3;
624 case 0x0400:
625 strcpy(name, "Headset"); return 7;
626 case 0x0500:
627 strcpy(name, "Phone"); return 5;
630 for (names = iterm_names; names->type; names++)
631 if (names->type == iterm->type) {
632 strcpy(name, names->name);
633 return strlen(names->name);
635 return 0;
640 * parse the source unit recursively until it reaches to a terminal
641 * or a branched unit.
643 static int check_input_term(struct mixer_build *state, int id, struct usb_audio_term *term)
645 unsigned char *p1;
647 memset(term, 0, sizeof(*term));
648 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
649 term->id = id;
650 switch (p1[2]) {
651 case INPUT_TERMINAL:
652 term->type = combine_word(p1 + 4);
653 term->channels = p1[7];
654 term->chconfig = combine_word(p1 + 8);
655 term->name = p1[11];
656 return 0;
657 case FEATURE_UNIT:
658 id = p1[4];
659 break; /* continue to parse */
660 case MIXER_UNIT:
661 term->type = p1[2] << 16; /* virtual type */
662 term->channels = p1[5 + p1[4]];
663 term->chconfig = combine_word(p1 + 6 + p1[4]);
664 term->name = p1[p1[0] - 1];
665 return 0;
666 case SELECTOR_UNIT:
667 /* call recursively to retrieve the channel info */
668 if (check_input_term(state, p1[5], term) < 0)
669 return -ENODEV;
670 term->type = p1[2] << 16; /* virtual type */
671 term->id = id;
672 term->name = p1[9 + p1[0] - 1];
673 return 0;
674 case PROCESSING_UNIT:
675 case EXTENSION_UNIT:
676 if (p1[6] == 1) {
677 id = p1[7];
678 break; /* continue to parse */
680 term->type = p1[2] << 16; /* virtual type */
681 term->channels = p1[7 + p1[6]];
682 term->chconfig = combine_word(p1 + 8 + p1[6]);
683 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
684 return 0;
685 default:
686 return -ENODEV;
689 return -ENODEV;
694 * Feature Unit
697 /* feature unit control information */
698 struct usb_feature_control_info {
699 const char *name;
700 unsigned int type; /* control type (mute, volume, etc.) */
703 static struct usb_feature_control_info audio_feature_info[] = {
704 { "Mute", USB_MIXER_INV_BOOLEAN },
705 { "Volume", USB_MIXER_S16 },
706 { "Tone Control - Bass", USB_MIXER_S8 },
707 { "Tone Control - Mid", USB_MIXER_S8 },
708 { "Tone Control - Treble", USB_MIXER_S8 },
709 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
710 { "Auto Gain Control", USB_MIXER_BOOLEAN },
711 { "Delay Control", USB_MIXER_U16 },
712 { "Bass Boost", USB_MIXER_BOOLEAN },
713 { "Loudness", USB_MIXER_BOOLEAN },
717 /* private_free callback */
718 static void usb_mixer_elem_free(struct snd_kcontrol *kctl)
720 kfree(kctl->private_data);
721 kctl->private_data = NULL;
726 * interface to ALSA control for feature/mixer units
730 * retrieve the minimum and maximum values for the specified control
732 static int get_min_max(struct usb_mixer_elem_info *cval, int default_min)
734 /* for failsafe */
735 cval->min = default_min;
736 cval->max = cval->min + 1;
737 cval->res = 1;
738 cval->dBmin = cval->dBmax = 0;
740 if (cval->val_type == USB_MIXER_BOOLEAN ||
741 cval->val_type == USB_MIXER_INV_BOOLEAN) {
742 cval->initialized = 1;
743 } else {
744 int minchn = 0;
745 if (cval->cmask) {
746 int i;
747 for (i = 0; i < MAX_CHANNELS; i++)
748 if (cval->cmask & (1 << i)) {
749 minchn = i + 1;
750 break;
753 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
754 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
755 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
756 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
757 return -EINVAL;
759 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
760 cval->res = 1;
761 } else {
762 int last_valid_res = cval->res;
764 while (cval->res > 1) {
765 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
766 break;
767 cval->res /= 2;
769 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
770 cval->res = last_valid_res;
772 if (cval->res == 0)
773 cval->res = 1;
775 /* Additional checks for the proper resolution
777 * Some devices report smaller resolutions than actually
778 * reacting. They don't return errors but simply clip
779 * to the lower aligned value.
781 if (cval->min + cval->res < cval->max) {
782 int last_valid_res = cval->res;
783 int saved, test, check;
784 get_cur_mix_raw(cval, minchn, &saved);
785 for (;;) {
786 test = saved;
787 if (test < cval->max)
788 test += cval->res;
789 else
790 test -= cval->res;
791 if (test < cval->min || test > cval->max ||
792 set_cur_mix_value(cval, minchn, 0, test) ||
793 get_cur_mix_raw(cval, minchn, &check)) {
794 cval->res = last_valid_res;
795 break;
797 if (test == check)
798 break;
799 cval->res *= 2;
801 set_cur_mix_value(cval, minchn, 0, saved);
804 cval->initialized = 1;
807 /* USB descriptions contain the dB scale in 1/256 dB unit
808 * while ALSA TLV contains in 1/100 dB unit
810 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
811 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
812 if (cval->dBmin > cval->dBmax) {
813 /* something is wrong; assume it's either from/to 0dB */
814 if (cval->dBmin < 0)
815 cval->dBmax = 0;
816 else if (cval->dBmin > 0)
817 cval->dBmin = 0;
818 if (cval->dBmin > cval->dBmax) {
819 /* totally crap, return an error */
820 return -EINVAL;
824 return 0;
828 /* get a feature/mixer unit info */
829 static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
831 struct usb_mixer_elem_info *cval = kcontrol->private_data;
833 if (cval->val_type == USB_MIXER_BOOLEAN ||
834 cval->val_type == USB_MIXER_INV_BOOLEAN)
835 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
836 else
837 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
838 uinfo->count = cval->channels;
839 if (cval->val_type == USB_MIXER_BOOLEAN ||
840 cval->val_type == USB_MIXER_INV_BOOLEAN) {
841 uinfo->value.integer.min = 0;
842 uinfo->value.integer.max = 1;
843 } else {
844 if (! cval->initialized)
845 get_min_max(cval, 0);
846 uinfo->value.integer.min = 0;
847 uinfo->value.integer.max =
848 (cval->max - cval->min + cval->res - 1) / cval->res;
850 return 0;
853 /* get the current value from feature/mixer unit */
854 static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
856 struct usb_mixer_elem_info *cval = kcontrol->private_data;
857 int c, cnt, val, err;
859 ucontrol->value.integer.value[0] = cval->min;
860 if (cval->cmask) {
861 cnt = 0;
862 for (c = 0; c < MAX_CHANNELS; c++) {
863 if (!(cval->cmask & (1 << c)))
864 continue;
865 err = get_cur_mix_value(cval, c + 1, cnt, &val);
866 if (err < 0)
867 return cval->mixer->ignore_ctl_error ? 0 : err;
868 val = get_relative_value(cval, val);
869 ucontrol->value.integer.value[cnt] = val;
870 cnt++;
872 return 0;
873 } else {
874 /* master channel */
875 err = get_cur_mix_value(cval, 0, 0, &val);
876 if (err < 0)
877 return cval->mixer->ignore_ctl_error ? 0 : err;
878 val = get_relative_value(cval, val);
879 ucontrol->value.integer.value[0] = val;
881 return 0;
884 /* put the current value to feature/mixer unit */
885 static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
887 struct usb_mixer_elem_info *cval = kcontrol->private_data;
888 int c, cnt, val, oval, err;
889 int changed = 0;
891 if (cval->cmask) {
892 cnt = 0;
893 for (c = 0; c < MAX_CHANNELS; c++) {
894 if (!(cval->cmask & (1 << c)))
895 continue;
896 err = get_cur_mix_value(cval, c + 1, cnt, &oval);
897 if (err < 0)
898 return cval->mixer->ignore_ctl_error ? 0 : err;
899 val = ucontrol->value.integer.value[cnt];
900 val = get_abs_value(cval, val);
901 if (oval != val) {
902 set_cur_mix_value(cval, c + 1, cnt, val);
903 changed = 1;
905 cnt++;
907 } else {
908 /* master channel */
909 err = get_cur_mix_value(cval, 0, 0, &oval);
910 if (err < 0)
911 return cval->mixer->ignore_ctl_error ? 0 : err;
912 val = ucontrol->value.integer.value[0];
913 val = get_abs_value(cval, val);
914 if (val != oval) {
915 set_cur_mix_value(cval, 0, 0, val);
916 changed = 1;
919 return changed;
922 static struct snd_kcontrol_new usb_feature_unit_ctl = {
923 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
924 .name = "", /* will be filled later manually */
925 .info = mixer_ctl_feature_info,
926 .get = mixer_ctl_feature_get,
927 .put = mixer_ctl_feature_put,
932 * build a feature control
935 static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
937 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
940 static void build_feature_ctl(struct mixer_build *state, unsigned char *desc,
941 unsigned int ctl_mask, int control,
942 struct usb_audio_term *iterm, int unitid)
944 unsigned int len = 0;
945 int mapped_name = 0;
946 int nameid = desc[desc[0] - 1];
947 struct snd_kcontrol *kctl;
948 struct usb_mixer_elem_info *cval;
949 const struct usbmix_name_map *map;
951 control++; /* change from zero-based to 1-based value */
953 if (control == USB_FEATURE_GEQ) {
954 /* FIXME: not supported yet */
955 return;
958 map = find_map(state, unitid, control);
959 if (check_ignored_ctl(map))
960 return;
962 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
963 if (! cval) {
964 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
965 return;
967 cval->mixer = state->mixer;
968 cval->id = unitid;
969 cval->control = control;
970 cval->cmask = ctl_mask;
971 cval->val_type = audio_feature_info[control-1].type;
972 if (ctl_mask == 0)
973 cval->channels = 1; /* master channel */
974 else {
975 int i, c = 0;
976 for (i = 0; i < 16; i++)
977 if (ctl_mask & (1 << i))
978 c++;
979 cval->channels = c;
982 /* get min/max values */
983 get_min_max(cval, 0);
985 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
986 if (! kctl) {
987 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
988 kfree(cval);
989 return;
991 kctl->private_free = usb_mixer_elem_free;
993 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
994 mapped_name = len != 0;
995 if (! len && nameid)
996 len = snd_usb_copy_string_desc(state, nameid,
997 kctl->id.name, sizeof(kctl->id.name));
999 switch (control) {
1000 case USB_FEATURE_MUTE:
1001 case USB_FEATURE_VOLUME:
1002 /* determine the control name. the rule is:
1003 * - if a name id is given in descriptor, use it.
1004 * - if the connected input can be determined, then use the name
1005 * of terminal type.
1006 * - if the connected output can be determined, use it.
1007 * - otherwise, anonymous name.
1009 if (! len) {
1010 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
1011 if (! len)
1012 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
1013 if (! len)
1014 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
1015 "Feature %d", unitid);
1017 /* determine the stream direction:
1018 * if the connected output is USB stream, then it's likely a
1019 * capture stream. otherwise it should be playback (hopefully :)
1021 if (! mapped_name && ! (state->oterm.type >> 16)) {
1022 if ((state->oterm.type & 0xff00) == 0x0100) {
1023 len = append_ctl_name(kctl, " Capture");
1024 } else {
1025 len = append_ctl_name(kctl, " Playback");
1028 append_ctl_name(kctl, control == USB_FEATURE_MUTE ?
1029 " Switch" : " Volume");
1030 if (control == USB_FEATURE_VOLUME) {
1031 kctl->tlv.c = mixer_vol_tlv;
1032 kctl->vd[0].access |=
1033 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1034 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1035 check_mapped_dB(map, cval);
1037 break;
1039 default:
1040 if (! len)
1041 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1042 sizeof(kctl->id.name));
1043 break;
1046 /* volume control quirks */
1047 switch (state->chip->usb_id) {
1048 case USB_ID(0x0471, 0x0101):
1049 case USB_ID(0x0471, 0x0104):
1050 case USB_ID(0x0471, 0x0105):
1051 case USB_ID(0x0672, 0x1041):
1052 /* quirk for UDA1321/N101.
1053 * note that detection between firmware 2.1.1.7 (N101)
1054 * and later 2.1.1.21 is not very clear from datasheets.
1055 * I hope that the min value is -15360 for newer firmware --jk
1057 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1058 cval->min == -15616) {
1059 snd_printk(KERN_INFO
1060 "set volume quirk for UDA1321/N101 chip\n");
1061 cval->max = -256;
1063 break;
1065 case USB_ID(0x046d, 0x09a4):
1066 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
1067 snd_printk(KERN_INFO
1068 "set volume quirk for QuickCam E3500\n");
1069 cval->min = 6080;
1070 cval->max = 8768;
1071 cval->res = 192;
1073 break;
1077 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
1078 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
1079 add_control_to_empty(state, kctl);
1085 * parse a feature unit
1087 * most of controlls are defined here.
1089 static int parse_audio_feature_unit(struct mixer_build *state, int unitid, unsigned char *ftr)
1091 int channels, i, j;
1092 struct usb_audio_term iterm;
1093 unsigned int master_bits, first_ch_bits;
1094 int err, csize;
1096 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
1097 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
1098 return -EINVAL;
1101 /* parse the source unit */
1102 if ((err = parse_audio_unit(state, ftr[4])) < 0)
1103 return err;
1105 /* determine the input source type and name */
1106 if (check_input_term(state, ftr[4], &iterm) < 0)
1107 return -EINVAL;
1109 channels = (ftr[0] - 7) / csize - 1;
1111 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
1112 /* master configuration quirks */
1113 switch (state->chip->usb_id) {
1114 case USB_ID(0x08bb, 0x2702):
1115 snd_printk(KERN_INFO
1116 "usbmixer: master volume quirk for PCM2702 chip\n");
1117 /* disable non-functional volume control */
1118 master_bits &= ~(1 << (USB_FEATURE_VOLUME - 1));
1119 break;
1121 if (channels > 0)
1122 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
1123 else
1124 first_ch_bits = 0;
1125 /* check all control types */
1126 for (i = 0; i < 10; i++) {
1127 unsigned int ch_bits = 0;
1128 for (j = 0; j < channels; j++) {
1129 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
1130 if (mask & (1 << i))
1131 ch_bits |= (1 << j);
1133 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
1134 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
1135 if (master_bits & (1 << i))
1136 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
1139 return 0;
1144 * Mixer Unit
1148 * build a mixer unit control
1150 * the callbacks are identical with feature unit.
1151 * input channel number (zero based) is given in control field instead.
1154 static void build_mixer_unit_ctl(struct mixer_build *state, unsigned char *desc,
1155 int in_pin, int in_ch, int unitid,
1156 struct usb_audio_term *iterm)
1158 struct usb_mixer_elem_info *cval;
1159 unsigned int input_pins = desc[4];
1160 unsigned int num_outs = desc[5 + input_pins];
1161 unsigned int i, len;
1162 struct snd_kcontrol *kctl;
1163 const struct usbmix_name_map *map;
1165 map = find_map(state, unitid, 0);
1166 if (check_ignored_ctl(map))
1167 return;
1169 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1170 if (! cval)
1171 return;
1173 cval->mixer = state->mixer;
1174 cval->id = unitid;
1175 cval->control = in_ch + 1; /* based on 1 */
1176 cval->val_type = USB_MIXER_S16;
1177 for (i = 0; i < num_outs; i++) {
1178 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1179 cval->cmask |= (1 << i);
1180 cval->channels++;
1184 /* get min/max values */
1185 get_min_max(cval, 0);
1187 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1188 if (! kctl) {
1189 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1190 kfree(cval);
1191 return;
1193 kctl->private_free = usb_mixer_elem_free;
1195 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1196 if (! len)
1197 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1198 if (! len)
1199 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1200 append_ctl_name(kctl, " Volume");
1202 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1203 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1204 add_control_to_empty(state, kctl);
1209 * parse a mixer unit
1211 static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1213 struct usb_audio_term iterm;
1214 int input_pins, num_ins, num_outs;
1215 int pin, ich, err;
1217 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1218 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1219 return -EINVAL;
1221 /* no bmControls field (e.g. Maya44) -> ignore */
1222 if (desc[0] <= 10 + input_pins) {
1223 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1224 return 0;
1227 num_ins = 0;
1228 ich = 0;
1229 for (pin = 0; pin < input_pins; pin++) {
1230 err = parse_audio_unit(state, desc[5 + pin]);
1231 if (err < 0)
1232 return err;
1233 err = check_input_term(state, desc[5 + pin], &iterm);
1234 if (err < 0)
1235 return err;
1236 num_ins += iterm.channels;
1237 for (; ich < num_ins; ++ich) {
1238 int och, ich_has_controls = 0;
1240 for (och = 0; och < num_outs; ++och) {
1241 if (check_matrix_bitmap(desc + 9 + input_pins,
1242 ich, och, num_outs)) {
1243 ich_has_controls = 1;
1244 break;
1247 if (ich_has_controls)
1248 build_mixer_unit_ctl(state, desc, pin, ich,
1249 unitid, &iterm);
1252 return 0;
1257 * Processing Unit / Extension Unit
1260 /* get callback for processing/extension unit */
1261 static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1263 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1264 int err, val;
1266 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1267 if (err < 0 && cval->mixer->ignore_ctl_error) {
1268 ucontrol->value.integer.value[0] = cval->min;
1269 return 0;
1271 if (err < 0)
1272 return err;
1273 val = get_relative_value(cval, val);
1274 ucontrol->value.integer.value[0] = val;
1275 return 0;
1278 /* put callback for processing/extension unit */
1279 static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1281 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1282 int val, oval, err;
1284 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1285 if (err < 0) {
1286 if (cval->mixer->ignore_ctl_error)
1287 return 0;
1288 return err;
1290 val = ucontrol->value.integer.value[0];
1291 val = get_abs_value(cval, val);
1292 if (val != oval) {
1293 set_cur_ctl_value(cval, cval->control << 8, val);
1294 return 1;
1296 return 0;
1299 /* alsa control interface for processing/extension unit */
1300 static struct snd_kcontrol_new mixer_procunit_ctl = {
1301 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1302 .name = "", /* will be filled later */
1303 .info = mixer_ctl_feature_info,
1304 .get = mixer_ctl_procunit_get,
1305 .put = mixer_ctl_procunit_put,
1310 * predefined data for processing units
1312 struct procunit_value_info {
1313 int control;
1314 char *suffix;
1315 int val_type;
1316 int min_value;
1319 struct procunit_info {
1320 int type;
1321 char *name;
1322 struct procunit_value_info *values;
1325 static struct procunit_value_info updown_proc_info[] = {
1326 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1327 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1328 { 0 }
1330 static struct procunit_value_info prologic_proc_info[] = {
1331 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1332 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1333 { 0 }
1335 static struct procunit_value_info threed_enh_proc_info[] = {
1336 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1337 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1338 { 0 }
1340 static struct procunit_value_info reverb_proc_info[] = {
1341 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1342 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1343 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1344 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1345 { 0 }
1347 static struct procunit_value_info chorus_proc_info[] = {
1348 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1349 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1350 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1351 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1352 { 0 }
1354 static struct procunit_value_info dcr_proc_info[] = {
1355 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1356 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1357 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1358 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1359 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1360 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1361 { 0 }
1364 static struct procunit_info procunits[] = {
1365 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1366 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1367 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1368 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1369 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1370 { USB_PROC_DCR, "DCR", dcr_proc_info },
1371 { 0 },
1374 * predefined data for extension units
1376 static struct procunit_value_info clock_rate_xu_info[] = {
1377 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
1378 { 0 }
1380 static struct procunit_value_info clock_source_xu_info[] = {
1381 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
1382 { 0 }
1384 static struct procunit_value_info spdif_format_xu_info[] = {
1385 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
1386 { 0 }
1388 static struct procunit_value_info soft_limit_xu_info[] = {
1389 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
1390 { 0 }
1392 static struct procunit_info extunits[] = {
1393 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
1394 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
1395 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
1396 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
1397 { 0 }
1400 * build a processing/extension unit
1402 static int build_audio_procunit(struct mixer_build *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1404 int num_ins = dsc[6];
1405 struct usb_mixer_elem_info *cval;
1406 struct snd_kcontrol *kctl;
1407 int i, err, nameid, type, len;
1408 struct procunit_info *info;
1409 struct procunit_value_info *valinfo;
1410 const struct usbmix_name_map *map;
1411 static struct procunit_value_info default_value_info[] = {
1412 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1413 { 0 }
1415 static struct procunit_info default_info = {
1416 0, NULL, default_value_info
1419 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1420 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1421 return -EINVAL;
1424 for (i = 0; i < num_ins; i++) {
1425 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1426 return err;
1429 type = combine_word(&dsc[4]);
1430 for (info = list; info && info->type; info++)
1431 if (info->type == type)
1432 break;
1433 if (! info || ! info->type)
1434 info = &default_info;
1436 for (valinfo = info->values; valinfo->control; valinfo++) {
1437 /* FIXME: bitmap might be longer than 8bit */
1438 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1439 continue;
1440 map = find_map(state, unitid, valinfo->control);
1441 if (check_ignored_ctl(map))
1442 continue;
1443 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1444 if (! cval) {
1445 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1446 return -ENOMEM;
1448 cval->mixer = state->mixer;
1449 cval->id = unitid;
1450 cval->control = valinfo->control;
1451 cval->val_type = valinfo->val_type;
1452 cval->channels = 1;
1454 /* get min/max values */
1455 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1456 /* FIXME: hard-coded */
1457 cval->min = 1;
1458 cval->max = dsc[15];
1459 cval->res = 1;
1460 cval->initialized = 1;
1461 } else {
1462 if (type == USB_XU_CLOCK_RATE) {
1463 /* E-Mu USB 0404/0202/TrackerPre
1464 * samplerate control quirk
1466 cval->min = 0;
1467 cval->max = 5;
1468 cval->res = 1;
1469 cval->initialized = 1;
1470 } else
1471 get_min_max(cval, valinfo->min_value);
1474 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1475 if (! kctl) {
1476 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1477 kfree(cval);
1478 return -ENOMEM;
1480 kctl->private_free = usb_mixer_elem_free;
1482 if (check_mapped_name(map, kctl->id.name,
1483 sizeof(kctl->id.name)))
1484 /* nothing */ ;
1485 else if (info->name)
1486 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1487 else {
1488 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1489 len = 0;
1490 if (nameid)
1491 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1492 if (! len)
1493 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1495 append_ctl_name(kctl, " ");
1496 append_ctl_name(kctl, valinfo->suffix);
1498 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1499 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1500 if ((err = add_control_to_empty(state, kctl)) < 0)
1501 return err;
1503 return 0;
1507 static int parse_audio_processing_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1509 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1512 static int parse_audio_extension_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1514 return build_audio_procunit(state, unitid, desc, extunits, "Extension Unit");
1519 * Selector Unit
1522 /* info callback for selector unit
1523 * use an enumerator type for routing
1525 static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1527 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1528 char **itemlist = (char **)kcontrol->private_value;
1530 if (snd_BUG_ON(!itemlist))
1531 return -EINVAL;
1532 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1533 uinfo->count = 1;
1534 uinfo->value.enumerated.items = cval->max;
1535 if ((int)uinfo->value.enumerated.item >= cval->max)
1536 uinfo->value.enumerated.item = cval->max - 1;
1537 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1538 return 0;
1541 /* get callback for selector unit */
1542 static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1544 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1545 int val, err;
1547 err = get_cur_ctl_value(cval, 0, &val);
1548 if (err < 0) {
1549 if (cval->mixer->ignore_ctl_error) {
1550 ucontrol->value.enumerated.item[0] = 0;
1551 return 0;
1553 return err;
1555 val = get_relative_value(cval, val);
1556 ucontrol->value.enumerated.item[0] = val;
1557 return 0;
1560 /* put callback for selector unit */
1561 static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1563 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1564 int val, oval, err;
1566 err = get_cur_ctl_value(cval, 0, &oval);
1567 if (err < 0) {
1568 if (cval->mixer->ignore_ctl_error)
1569 return 0;
1570 return err;
1572 val = ucontrol->value.enumerated.item[0];
1573 val = get_abs_value(cval, val);
1574 if (val != oval) {
1575 set_cur_ctl_value(cval, 0, val);
1576 return 1;
1578 return 0;
1581 /* alsa control interface for selector unit */
1582 static struct snd_kcontrol_new mixer_selectunit_ctl = {
1583 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1584 .name = "", /* will be filled later */
1585 .info = mixer_ctl_selector_info,
1586 .get = mixer_ctl_selector_get,
1587 .put = mixer_ctl_selector_put,
1591 /* private free callback.
1592 * free both private_data and private_value
1594 static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
1596 int i, num_ins = 0;
1598 if (kctl->private_data) {
1599 struct usb_mixer_elem_info *cval = kctl->private_data;
1600 num_ins = cval->max;
1601 kfree(cval);
1602 kctl->private_data = NULL;
1604 if (kctl->private_value) {
1605 char **itemlist = (char **)kctl->private_value;
1606 for (i = 0; i < num_ins; i++)
1607 kfree(itemlist[i]);
1608 kfree(itemlist);
1609 kctl->private_value = 0;
1614 * parse a selector unit
1616 static int parse_audio_selector_unit(struct mixer_build *state, int unitid, unsigned char *desc)
1618 unsigned int num_ins = desc[4];
1619 unsigned int i, nameid, len;
1620 int err;
1621 struct usb_mixer_elem_info *cval;
1622 struct snd_kcontrol *kctl;
1623 const struct usbmix_name_map *map;
1624 char **namelist;
1626 if (! num_ins || desc[0] < 5 + num_ins) {
1627 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1628 return -EINVAL;
1631 for (i = 0; i < num_ins; i++) {
1632 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1633 return err;
1636 if (num_ins == 1) /* only one ? nonsense! */
1637 return 0;
1639 map = find_map(state, unitid, 0);
1640 if (check_ignored_ctl(map))
1641 return 0;
1643 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1644 if (! cval) {
1645 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1646 return -ENOMEM;
1648 cval->mixer = state->mixer;
1649 cval->id = unitid;
1650 cval->val_type = USB_MIXER_U8;
1651 cval->channels = 1;
1652 cval->min = 1;
1653 cval->max = num_ins;
1654 cval->res = 1;
1655 cval->initialized = 1;
1657 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1658 if (! namelist) {
1659 snd_printk(KERN_ERR "cannot malloc\n");
1660 kfree(cval);
1661 return -ENOMEM;
1663 #define MAX_ITEM_NAME_LEN 64
1664 for (i = 0; i < num_ins; i++) {
1665 struct usb_audio_term iterm;
1666 len = 0;
1667 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1668 if (! namelist[i]) {
1669 snd_printk(KERN_ERR "cannot malloc\n");
1670 while (i--)
1671 kfree(namelist[i]);
1672 kfree(namelist);
1673 kfree(cval);
1674 return -ENOMEM;
1676 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1677 MAX_ITEM_NAME_LEN);
1678 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1679 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1680 if (! len)
1681 sprintf(namelist[i], "Input %d", i);
1684 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1685 if (! kctl) {
1686 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1687 kfree(namelist);
1688 kfree(cval);
1689 return -ENOMEM;
1691 kctl->private_value = (unsigned long)namelist;
1692 kctl->private_free = usb_mixer_selector_elem_free;
1694 nameid = desc[desc[0] - 1];
1695 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1696 if (len)
1698 else if (nameid)
1699 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1700 else {
1701 len = get_term_name(state, &state->oterm,
1702 kctl->id.name, sizeof(kctl->id.name), 0);
1703 if (! len)
1704 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1706 if ((state->oterm.type & 0xff00) == 0x0100)
1707 append_ctl_name(kctl, " Capture Source");
1708 else
1709 append_ctl_name(kctl, " Playback Source");
1712 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1713 cval->id, kctl->id.name, num_ins);
1714 if ((err = add_control_to_empty(state, kctl)) < 0)
1715 return err;
1717 return 0;
1722 * parse an audio unit recursively
1725 static int parse_audio_unit(struct mixer_build *state, int unitid)
1727 unsigned char *p1;
1729 if (test_and_set_bit(unitid, state->unitbitmap))
1730 return 0; /* the unit already visited */
1732 p1 = find_audio_control_unit(state, unitid);
1733 if (!p1) {
1734 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1735 return -EINVAL;
1738 switch (p1[2]) {
1739 case INPUT_TERMINAL:
1740 return 0; /* NOP */
1741 case MIXER_UNIT:
1742 return parse_audio_mixer_unit(state, unitid, p1);
1743 case SELECTOR_UNIT:
1744 return parse_audio_selector_unit(state, unitid, p1);
1745 case FEATURE_UNIT:
1746 return parse_audio_feature_unit(state, unitid, p1);
1747 case PROCESSING_UNIT:
1748 return parse_audio_processing_unit(state, unitid, p1);
1749 case EXTENSION_UNIT:
1750 return parse_audio_extension_unit(state, unitid, p1);
1751 default:
1752 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1753 return -EINVAL;
1757 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1759 kfree(mixer->id_elems);
1760 if (mixer->urb) {
1761 kfree(mixer->urb->transfer_buffer);
1762 usb_free_urb(mixer->urb);
1764 usb_free_urb(mixer->rc_urb);
1765 kfree(mixer->rc_setup_packet);
1766 kfree(mixer);
1769 static int snd_usb_mixer_dev_free(struct snd_device *device)
1771 struct usb_mixer_interface *mixer = device->device_data;
1772 snd_usb_mixer_free(mixer);
1773 return 0;
1777 * create mixer controls
1779 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1781 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1783 unsigned char *desc;
1784 struct mixer_build state;
1785 int err;
1786 const struct usbmix_ctl_map *map;
1787 struct usb_host_interface *hostif;
1789 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1790 memset(&state, 0, sizeof(state));
1791 state.chip = mixer->chip;
1792 state.mixer = mixer;
1793 state.buffer = hostif->extra;
1794 state.buflen = hostif->extralen;
1796 /* check the mapping table */
1797 for (map = usbmix_ctl_maps; map->id; map++) {
1798 if (map->id == state.chip->usb_id) {
1799 state.map = map->map;
1800 state.selector_map = map->selector_map;
1801 mixer->ignore_ctl_error = map->ignore_ctl_error;
1802 break;
1806 desc = NULL;
1807 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1808 if (desc[0] < 9)
1809 continue; /* invalid descriptor? */
1810 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1811 state.oterm.id = desc[3];
1812 state.oterm.type = combine_word(&desc[4]);
1813 state.oterm.name = desc[8];
1814 err = parse_audio_unit(&state, desc[7]);
1815 if (err < 0)
1816 return err;
1818 return 0;
1821 static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1822 int unitid)
1824 struct usb_mixer_elem_info *info;
1826 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1827 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1828 info->elem_id);
1831 static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
1832 int unitid,
1833 struct usb_mixer_elem_info *cval)
1835 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
1836 "S8", "U8", "S16", "U16"};
1837 snd_iprintf(buffer, " Unit: %i\n", unitid);
1838 if (cval->elem_id)
1839 snd_iprintf(buffer, " Control: name=\"%s\", index=%i\n",
1840 cval->elem_id->name, cval->elem_id->index);
1841 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
1842 "channels=%i, type=\"%s\"\n", cval->id,
1843 cval->control, cval->cmask, cval->channels,
1844 val_types[cval->val_type]);
1845 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
1846 cval->min, cval->max, cval->dBmin, cval->dBmax);
1849 static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
1850 struct snd_info_buffer *buffer)
1852 struct snd_usb_audio *chip = entry->private_data;
1853 struct usb_mixer_interface *mixer;
1854 struct usb_mixer_elem_info *cval;
1855 int unitid;
1857 list_for_each_entry(mixer, &chip->mixer_list, list) {
1858 snd_iprintf(buffer,
1859 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
1860 chip->usb_id, mixer->ctrlif,
1861 mixer->ignore_ctl_error);
1862 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
1863 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
1864 for (cval = mixer->id_elems[unitid]; cval;
1865 cval = cval->next_id_elem)
1866 snd_usb_mixer_dump_cval(buffer, unitid, cval);
1871 static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1872 int unitid)
1874 if (!mixer->rc_cfg)
1875 return;
1876 /* unit ids specific to Extigy/Audigy 2 NX: */
1877 switch (unitid) {
1878 case 0: /* remote control */
1879 mixer->rc_urb->dev = mixer->chip->dev;
1880 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1881 break;
1882 case 4: /* digital in jack */
1883 case 7: /* line in jacks */
1884 case 19: /* speaker out jacks */
1885 case 20: /* headphones out jack */
1886 break;
1887 /* live24ext: 4 = line-in jack */
1888 case 3: /* hp-out jack (may actuate Mute) */
1889 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1890 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1891 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1892 break;
1893 default:
1894 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1895 break;
1899 static void snd_usb_mixer_status_complete(struct urb *urb)
1901 struct usb_mixer_interface *mixer = urb->context;
1903 if (urb->status == 0) {
1904 u8 *buf = urb->transfer_buffer;
1905 int i;
1907 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1908 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1909 buf[0], buf[1]);
1910 /* ignore any notifications not from the control interface */
1911 if ((buf[0] & 0x0f) != 0)
1912 continue;
1913 if (!(buf[0] & 0x40))
1914 snd_usb_mixer_notify_id(mixer, buf[1]);
1915 else
1916 snd_usb_mixer_memory_change(mixer, buf[1]);
1919 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1920 urb->dev = mixer->chip->dev;
1921 usb_submit_urb(urb, GFP_ATOMIC);
1925 /* create the handler for the optional status interrupt endpoint */
1926 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1928 struct usb_host_interface *hostif;
1929 struct usb_endpoint_descriptor *ep;
1930 void *transfer_buffer;
1931 int buffer_length;
1932 unsigned int epnum;
1934 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1935 /* we need one interrupt input endpoint */
1936 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1937 return 0;
1938 ep = get_endpoint(hostif, 0);
1939 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
1940 return 0;
1942 epnum = usb_endpoint_num(ep);
1943 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1944 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1945 if (!transfer_buffer)
1946 return -ENOMEM;
1947 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1948 if (!mixer->urb) {
1949 kfree(transfer_buffer);
1950 return -ENOMEM;
1952 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1953 usb_rcvintpipe(mixer->chip->dev, epnum),
1954 transfer_buffer, buffer_length,
1955 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1956 usb_submit_urb(mixer->urb, GFP_KERNEL);
1957 return 0;
1960 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
1962 struct usb_mixer_interface *mixer = urb->context;
1963 const struct rc_config *rc = mixer->rc_cfg;
1964 u32 code;
1966 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
1967 return;
1969 code = mixer->rc_buffer[rc->offset];
1970 if (rc->length == 2)
1971 code |= mixer->rc_buffer[rc->offset + 1] << 8;
1973 /* the Mute button actually changes the mixer control */
1974 if (code == rc->mute_code)
1975 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
1976 mixer->rc_code = code;
1977 wmb();
1978 wake_up(&mixer->rc_waitq);
1981 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
1982 long count, loff_t *offset)
1984 struct usb_mixer_interface *mixer = hw->private_data;
1985 int err;
1986 u32 rc_code;
1988 if (count != 1 && count != 4)
1989 return -EINVAL;
1990 err = wait_event_interruptible(mixer->rc_waitq,
1991 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1992 if (err == 0) {
1993 if (count == 1)
1994 err = put_user(rc_code, buf);
1995 else
1996 err = put_user(rc_code, (u32 __user *)buf);
1998 return err < 0 ? err : count;
2001 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
2002 poll_table *wait)
2004 struct usb_mixer_interface *mixer = hw->private_data;
2006 poll_wait(file, &mixer->rc_waitq, wait);
2007 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
2010 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
2012 struct snd_hwdep *hwdep;
2013 int err, len, i;
2015 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
2016 if (rc_configs[i].usb_id == mixer->chip->usb_id)
2017 break;
2018 if (i >= ARRAY_SIZE(rc_configs))
2019 return 0;
2020 mixer->rc_cfg = &rc_configs[i];
2022 len = mixer->rc_cfg->packet_length;
2024 init_waitqueue_head(&mixer->rc_waitq);
2025 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
2026 if (err < 0)
2027 return err;
2028 snprintf(hwdep->name, sizeof(hwdep->name),
2029 "%s remote control", mixer->chip->card->shortname);
2030 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
2031 hwdep->private_data = mixer;
2032 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
2033 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
2034 hwdep->exclusive = 1;
2036 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
2037 if (!mixer->rc_urb)
2038 return -ENOMEM;
2039 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
2040 if (!mixer->rc_setup_packet) {
2041 usb_free_urb(mixer->rc_urb);
2042 mixer->rc_urb = NULL;
2043 return -ENOMEM;
2045 mixer->rc_setup_packet->bRequestType =
2046 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2047 mixer->rc_setup_packet->bRequest = GET_MEM;
2048 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
2049 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
2050 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
2051 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
2052 usb_rcvctrlpipe(mixer->chip->dev, 0),
2053 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
2054 snd_usb_soundblaster_remote_complete, mixer);
2055 return 0;
2058 #define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
2060 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2062 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2063 int index = kcontrol->private_value;
2065 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
2066 return 0;
2069 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2071 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2072 int index = kcontrol->private_value;
2073 int value = ucontrol->value.integer.value[0];
2074 int err, changed;
2076 if (value > 1)
2077 return -EINVAL;
2078 changed = value != mixer->audigy2nx_leds[index];
2079 err = snd_usb_ctl_msg(mixer->chip->dev,
2080 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
2081 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2082 value, index + 2, NULL, 0, 100);
2083 if (err < 0)
2084 return err;
2085 mixer->audigy2nx_leds[index] = value;
2086 return changed;
2089 static struct snd_kcontrol_new snd_audigy2nx_controls[] = {
2091 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2092 .name = "CMSS LED Switch",
2093 .info = snd_audigy2nx_led_info,
2094 .get = snd_audigy2nx_led_get,
2095 .put = snd_audigy2nx_led_put,
2096 .private_value = 0,
2099 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2100 .name = "Power LED Switch",
2101 .info = snd_audigy2nx_led_info,
2102 .get = snd_audigy2nx_led_get,
2103 .put = snd_audigy2nx_led_put,
2104 .private_value = 1,
2107 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2108 .name = "Dolby Digital LED Switch",
2109 .info = snd_audigy2nx_led_info,
2110 .get = snd_audigy2nx_led_get,
2111 .put = snd_audigy2nx_led_put,
2112 .private_value = 2,
2116 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
2118 int i, err;
2120 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
2121 if (i > 1 && /* Live24ext has 2 LEDs only */
2122 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2123 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
2124 break;
2125 err = snd_ctl_add(mixer->chip->card,
2126 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
2127 if (err < 0)
2128 return err;
2130 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
2131 return 0;
2134 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
2135 struct snd_info_buffer *buffer)
2137 static const struct sb_jack {
2138 int unitid;
2139 const char *name;
2140 } jacks_audigy2nx[] = {
2141 {4, "dig in "},
2142 {7, "line in"},
2143 {19, "spk out"},
2144 {20, "hph out"},
2145 {-1, NULL}
2146 }, jacks_live24ext[] = {
2147 {4, "line in"}, /* &1=Line, &2=Mic*/
2148 {3, "hph out"}, /* headphones */
2149 {0, "RC "}, /* last command, 6 bytes see rc_config above */
2150 {-1, NULL}
2152 const struct sb_jack *jacks;
2153 struct usb_mixer_interface *mixer = entry->private_data;
2154 int i, err;
2155 u8 buf[3];
2157 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
2158 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
2159 jacks = jacks_audigy2nx;
2160 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2161 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
2162 jacks = jacks_live24ext;
2163 else
2164 return;
2166 for (i = 0; jacks[i].name; ++i) {
2167 snd_iprintf(buffer, "%s: ", jacks[i].name);
2168 err = snd_usb_ctl_msg(mixer->chip->dev,
2169 usb_rcvctrlpipe(mixer->chip->dev, 0),
2170 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
2171 USB_RECIP_INTERFACE, 0,
2172 jacks[i].unitid << 8, buf, 3, 100);
2173 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
2174 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
2175 else
2176 snd_iprintf(buffer, "?\n");
2180 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
2181 struct snd_ctl_elem_value *ucontrol)
2183 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2185 ucontrol->value.integer.value[0] = !!(mixer->xonar_u1_status & 0x02);
2186 return 0;
2189 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
2190 struct snd_ctl_elem_value *ucontrol)
2192 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
2193 u8 old_status, new_status;
2194 int err, changed;
2196 old_status = mixer->xonar_u1_status;
2197 if (ucontrol->value.integer.value[0])
2198 new_status = old_status | 0x02;
2199 else
2200 new_status = old_status & ~0x02;
2201 changed = new_status != old_status;
2202 err = snd_usb_ctl_msg(mixer->chip->dev,
2203 usb_sndctrlpipe(mixer->chip->dev, 0), 0x08,
2204 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2205 50, 0, &new_status, 1, 100);
2206 if (err < 0)
2207 return err;
2208 mixer->xonar_u1_status = new_status;
2209 return changed;
2212 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
2213 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2214 .name = "Digital Playback Switch",
2215 .info = snd_ctl_boolean_mono_info,
2216 .get = snd_xonar_u1_switch_get,
2217 .put = snd_xonar_u1_switch_put,
2220 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
2222 int err;
2224 err = snd_ctl_add(mixer->chip->card,
2225 snd_ctl_new1(&snd_xonar_u1_output_switch, mixer));
2226 if (err < 0)
2227 return err;
2228 mixer->xonar_u1_status = 0x05;
2229 return 0;
2232 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
2233 unsigned char samplerate_id)
2235 struct usb_mixer_interface *mixer;
2236 struct usb_mixer_elem_info *cval;
2237 int unitid = 12; /* SamleRate ExtensionUnit ID */
2239 list_for_each_entry(mixer, &chip->mixer_list, list) {
2240 cval = mixer->id_elems[unitid];
2241 if (cval) {
2242 set_cur_ctl_value(cval, cval->control << 8,
2243 samplerate_id);
2244 snd_usb_mixer_notify_id(mixer, unitid);
2246 break;
2250 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
2251 int ignore_error)
2253 static struct snd_device_ops dev_ops = {
2254 .dev_free = snd_usb_mixer_dev_free
2256 struct usb_mixer_interface *mixer;
2257 struct snd_info_entry *entry;
2258 int err;
2260 strcpy(chip->card->mixername, "USB Mixer");
2262 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
2263 if (!mixer)
2264 return -ENOMEM;
2265 mixer->chip = chip;
2266 mixer->ctrlif = ctrlif;
2267 mixer->ignore_ctl_error = ignore_error;
2268 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
2269 GFP_KERNEL);
2270 if (!mixer->id_elems) {
2271 kfree(mixer);
2272 return -ENOMEM;
2275 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
2276 (err = snd_usb_mixer_status_create(mixer)) < 0)
2277 goto _error;
2279 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
2280 goto _error;
2282 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020) ||
2283 mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2284 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)) {
2285 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
2286 goto _error;
2287 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
2288 snd_info_set_text_ops(entry, mixer,
2289 snd_audigy2nx_proc_read);
2292 if (mixer->chip->usb_id == USB_ID(0x0b05, 0x1739) ||
2293 mixer->chip->usb_id == USB_ID(0x0b05, 0x1743)) {
2294 err = snd_xonar_u1_controls_create(mixer);
2295 if (err < 0)
2296 goto _error;
2299 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
2300 if (err < 0)
2301 goto _error;
2303 if (list_empty(&chip->mixer_list) &&
2304 !snd_card_proc_new(chip->card, "usbmixer", &entry))
2305 snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
2307 list_add(&mixer->list, &chip->mixer_list);
2308 return 0;
2310 _error:
2311 snd_usb_mixer_free(mixer);
2312 return err;
2315 void snd_usb_mixer_disconnect(struct list_head *p)
2317 struct usb_mixer_interface *mixer;
2319 mixer = list_entry(p, struct usb_mixer_interface, list);
2320 usb_kill_urb(mixer->urb);
2321 usb_kill_urb(mixer->rc_urb);