[PATCH] pcmcia: id_table for ray_cs
[linux-2.6/mini2440.git] / sound / usb / usbmixer.c
blobe73c1c9d3e735a50c9b1edc56c31830c600f28b5
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 typedef struct usb_mixer_build mixer_build_t;
50 typedef struct usb_audio_term usb_audio_term_t;
51 typedef struct usb_mixer_elem_info usb_mixer_elem_info_t;
54 struct usb_mixer_interface {
55 snd_usb_audio_t *chip;
56 unsigned int ctrlif;
57 struct list_head list;
58 unsigned int ignore_ctl_error;
59 struct urb *urb;
60 usb_mixer_elem_info_t **id_elems; /* array[256], indexed by unit id */
62 /* Sound Blaster remote control stuff */
63 enum {
64 RC_NONE,
65 RC_EXTIGY,
66 RC_AUDIGY2NX,
67 } rc_type;
68 unsigned long rc_hwdep_open;
69 u32 rc_code;
70 wait_queue_head_t rc_waitq;
71 struct urb *rc_urb;
72 struct usb_ctrlrequest *rc_setup_packet;
73 u8 rc_buffer[6];
75 u8 audigy2nx_leds[3];
79 struct usb_audio_term {
80 int id;
81 int type;
82 int channels;
83 unsigned int chconfig;
84 int name;
87 struct usbmix_name_map;
89 struct usb_mixer_build {
90 snd_usb_audio_t *chip;
91 struct usb_mixer_interface *mixer;
92 unsigned char *buffer;
93 unsigned int buflen;
94 DECLARE_BITMAP(unitbitmap, 256);
95 usb_audio_term_t oterm;
96 const struct usbmix_name_map *map;
97 const struct usbmix_selector_map *selector_map;
100 struct usb_mixer_elem_info {
101 struct usb_mixer_interface *mixer;
102 usb_mixer_elem_info_t *next_id_elem; /* list of controls with same id */
103 snd_ctl_elem_id_t *elem_id;
104 unsigned int id;
105 unsigned int control; /* CS or ICN (high byte) */
106 unsigned int cmask; /* channel mask bitmap: 0 = master */
107 int channels;
108 int val_type;
109 int min, max, res;
110 u8 initialized;
114 enum {
115 USB_FEATURE_NONE = 0,
116 USB_FEATURE_MUTE = 1,
117 USB_FEATURE_VOLUME,
118 USB_FEATURE_BASS,
119 USB_FEATURE_MID,
120 USB_FEATURE_TREBLE,
121 USB_FEATURE_GEQ,
122 USB_FEATURE_AGC,
123 USB_FEATURE_DELAY,
124 USB_FEATURE_BASSBOOST,
125 USB_FEATURE_LOUDNESS
128 enum {
129 USB_MIXER_BOOLEAN,
130 USB_MIXER_INV_BOOLEAN,
131 USB_MIXER_S8,
132 USB_MIXER_U8,
133 USB_MIXER_S16,
134 USB_MIXER_U16,
137 enum {
138 USB_PROC_UPDOWN = 1,
139 USB_PROC_UPDOWN_SWITCH = 1,
140 USB_PROC_UPDOWN_MODE_SEL = 2,
142 USB_PROC_PROLOGIC = 2,
143 USB_PROC_PROLOGIC_SWITCH = 1,
144 USB_PROC_PROLOGIC_MODE_SEL = 2,
146 USB_PROC_3DENH = 3,
147 USB_PROC_3DENH_SWITCH = 1,
148 USB_PROC_3DENH_SPACE = 2,
150 USB_PROC_REVERB = 4,
151 USB_PROC_REVERB_SWITCH = 1,
152 USB_PROC_REVERB_LEVEL = 2,
153 USB_PROC_REVERB_TIME = 3,
154 USB_PROC_REVERB_DELAY = 4,
156 USB_PROC_CHORUS = 5,
157 USB_PROC_CHORUS_SWITCH = 1,
158 USB_PROC_CHORUS_LEVEL = 2,
159 USB_PROC_CHORUS_RATE = 3,
160 USB_PROC_CHORUS_DEPTH = 4,
162 USB_PROC_DCR = 6,
163 USB_PROC_DCR_SWITCH = 1,
164 USB_PROC_DCR_RATIO = 2,
165 USB_PROC_DCR_MAX_AMP = 3,
166 USB_PROC_DCR_THRESHOLD = 4,
167 USB_PROC_DCR_ATTACK = 5,
168 USB_PROC_DCR_RELEASE = 6,
171 #define MAX_CHANNELS 10 /* max logical channels */
175 * manual mapping of mixer names
176 * if the mixer topology is too complicated and the parsed names are
177 * ambiguous, add the entries in usbmixer_maps.c.
179 #include "usbmixer_maps.c"
181 /* get the mapped name if the unit matches */
182 static int check_mapped_name(mixer_build_t *state, int unitid, int control, char *buf, int buflen)
184 const struct usbmix_name_map *p;
186 if (! state->map)
187 return 0;
189 for (p = state->map; p->id; p++) {
190 if (p->id == unitid && p->name &&
191 (! control || ! p->control || control == p->control)) {
192 buflen--;
193 return strlcpy(buf, p->name, buflen);
196 return 0;
199 /* check whether the control should be ignored */
200 static int check_ignored_ctl(mixer_build_t *state, int unitid, int control)
202 const struct usbmix_name_map *p;
204 if (! state->map)
205 return 0;
206 for (p = state->map; p->id; p++) {
207 if (p->id == unitid && ! p->name &&
208 (! control || ! p->control || control == p->control)) {
209 // printk("ignored control %d:%d\n", unitid, control);
210 return 1;
213 return 0;
216 /* get the mapped selector source name */
217 static int check_mapped_selector_name(mixer_build_t *state, int unitid,
218 int index, char *buf, int buflen)
220 const struct usbmix_selector_map *p;
222 if (! state->selector_map)
223 return 0;
224 for (p = state->selector_map; p->id; p++) {
225 if (p->id == unitid && index < p->count)
226 return strlcpy(buf, p->names[index], buflen);
228 return 0;
232 * find an audio control unit with the given unit id
234 static void *find_audio_control_unit(mixer_build_t *state, unsigned char unit)
236 unsigned char *p;
238 p = NULL;
239 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
240 USB_DT_CS_INTERFACE)) != NULL) {
241 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
242 return p;
244 return NULL;
249 * copy a string with the given id
251 static int snd_usb_copy_string_desc(mixer_build_t *state, int index, char *buf, int maxlen)
253 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
254 buf[len] = 0;
255 return len;
259 * convert from the byte/word on usb descriptor to the zero-based integer
261 static int convert_signed_value(usb_mixer_elem_info_t *cval, int val)
263 switch (cval->val_type) {
264 case USB_MIXER_BOOLEAN:
265 return !!val;
266 case USB_MIXER_INV_BOOLEAN:
267 return !val;
268 case USB_MIXER_U8:
269 val &= 0xff;
270 break;
271 case USB_MIXER_S8:
272 val &= 0xff;
273 if (val >= 0x80)
274 val -= 0x100;
275 break;
276 case USB_MIXER_U16:
277 val &= 0xffff;
278 break;
279 case USB_MIXER_S16:
280 val &= 0xffff;
281 if (val >= 0x8000)
282 val -= 0x10000;
283 break;
285 return val;
289 * convert from the zero-based int to the byte/word for usb descriptor
291 static int convert_bytes_value(usb_mixer_elem_info_t *cval, int val)
293 switch (cval->val_type) {
294 case USB_MIXER_BOOLEAN:
295 return !!val;
296 case USB_MIXER_INV_BOOLEAN:
297 return !val;
298 case USB_MIXER_S8:
299 case USB_MIXER_U8:
300 return val & 0xff;
301 case USB_MIXER_S16:
302 case USB_MIXER_U16:
303 return val & 0xffff;
305 return 0; /* not reached */
308 static int get_relative_value(usb_mixer_elem_info_t *cval, int val)
310 if (! cval->res)
311 cval->res = 1;
312 if (val < cval->min)
313 return 0;
314 else if (val > cval->max)
315 return (cval->max - cval->min) / cval->res;
316 else
317 return (val - cval->min) / cval->res;
320 static int get_abs_value(usb_mixer_elem_info_t *cval, int val)
322 if (val < 0)
323 return cval->min;
324 if (! cval->res)
325 cval->res = 1;
326 val *= cval->res;
327 val += cval->min;
328 if (val > cval->max)
329 return cval->max;
330 return val;
335 * retrieve a mixer value
338 static int get_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int *value_ret)
340 unsigned char buf[2];
341 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
342 int timeout = 10;
344 while (timeout-- > 0) {
345 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
346 usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
347 request,
348 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
349 validx, cval->mixer->ctrlif | (cval->id << 8),
350 buf, val_len, 100) >= 0) {
351 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
352 return 0;
355 snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
356 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
357 return -EINVAL;
360 static int get_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int *value)
362 return get_ctl_value(cval, GET_CUR, validx, value);
365 /* channel = 0: master, 1 = first channel */
366 inline static int get_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int *value)
368 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
372 * set a mixer value
375 static int set_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int value_set)
377 unsigned char buf[2];
378 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
379 int timeout = 10;
381 value_set = convert_bytes_value(cval, value_set);
382 buf[0] = value_set & 0xff;
383 buf[1] = (value_set >> 8) & 0xff;
384 while (timeout -- > 0)
385 if (snd_usb_ctl_msg(cval->mixer->chip->dev,
386 usb_sndctrlpipe(cval->mixer->chip->dev, 0),
387 request,
388 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
389 validx, cval->mixer->ctrlif | (cval->id << 8),
390 buf, val_len, 100) >= 0)
391 return 0;
392 snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
393 request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
394 return -EINVAL;
397 static int set_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int value)
399 return set_ctl_value(cval, SET_CUR, validx, value);
402 inline static int set_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int value)
404 return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
409 * parser routines begin here...
412 static int parse_audio_unit(mixer_build_t *state, int unitid);
416 * check if the input/output channel routing is enabled on the given bitmap.
417 * used for mixer unit parser
419 static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
421 int idx = ich * num_outs + och;
422 return bmap[idx >> 3] & (0x80 >> (idx & 7));
427 * add an alsa control element
428 * search and increment the index until an empty slot is found.
430 * if failed, give up and free the control instance.
433 static int add_control_to_empty(mixer_build_t *state, snd_kcontrol_t *kctl)
435 usb_mixer_elem_info_t *cval = kctl->private_data;
436 int err;
438 while (snd_ctl_find_id(state->chip->card, &kctl->id))
439 kctl->id.index++;
440 if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
441 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
442 snd_ctl_free_one(kctl);
443 return err;
445 cval->elem_id = &kctl->id;
446 cval->next_id_elem = state->mixer->id_elems[cval->id];
447 state->mixer->id_elems[cval->id] = cval;
448 return 0;
453 * get a terminal name string
456 static struct iterm_name_combo {
457 int type;
458 char *name;
459 } iterm_names[] = {
460 { 0x0300, "Output" },
461 { 0x0301, "Speaker" },
462 { 0x0302, "Headphone" },
463 { 0x0303, "HMD Audio" },
464 { 0x0304, "Desktop Speaker" },
465 { 0x0305, "Room Speaker" },
466 { 0x0306, "Com Speaker" },
467 { 0x0307, "LFE" },
468 { 0x0600, "External In" },
469 { 0x0601, "Analog In" },
470 { 0x0602, "Digital In" },
471 { 0x0603, "Line" },
472 { 0x0604, "Legacy In" },
473 { 0x0605, "IEC958 In" },
474 { 0x0606, "1394 DA Stream" },
475 { 0x0607, "1394 DV Stream" },
476 { 0x0700, "Embedded" },
477 { 0x0701, "Noise Source" },
478 { 0x0702, "Equalization Noise" },
479 { 0x0703, "CD" },
480 { 0x0704, "DAT" },
481 { 0x0705, "DCC" },
482 { 0x0706, "MiniDisk" },
483 { 0x0707, "Analog Tape" },
484 { 0x0708, "Phonograph" },
485 { 0x0709, "VCR Audio" },
486 { 0x070a, "Video Disk Audio" },
487 { 0x070b, "DVD Audio" },
488 { 0x070c, "TV Tuner Audio" },
489 { 0x070d, "Satellite Rec Audio" },
490 { 0x070e, "Cable Tuner Audio" },
491 { 0x070f, "DSS Audio" },
492 { 0x0710, "Radio Receiver" },
493 { 0x0711, "Radio Transmitter" },
494 { 0x0712, "Multi-Track Recorder" },
495 { 0x0713, "Synthesizer" },
496 { 0 },
499 static int get_term_name(mixer_build_t *state, usb_audio_term_t *iterm,
500 unsigned char *name, int maxlen, int term_only)
502 struct iterm_name_combo *names;
504 if (iterm->name)
505 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
507 /* virtual type - not a real terminal */
508 if (iterm->type >> 16) {
509 if (term_only)
510 return 0;
511 switch (iterm->type >> 16) {
512 case SELECTOR_UNIT:
513 strcpy(name, "Selector"); return 8;
514 case PROCESSING_UNIT:
515 strcpy(name, "Process Unit"); return 12;
516 case EXTENSION_UNIT:
517 strcpy(name, "Ext Unit"); return 8;
518 case MIXER_UNIT:
519 strcpy(name, "Mixer"); return 5;
520 default:
521 return sprintf(name, "Unit %d", iterm->id);
525 switch (iterm->type & 0xff00) {
526 case 0x0100:
527 strcpy(name, "PCM"); return 3;
528 case 0x0200:
529 strcpy(name, "Mic"); return 3;
530 case 0x0400:
531 strcpy(name, "Headset"); return 7;
532 case 0x0500:
533 strcpy(name, "Phone"); return 5;
536 for (names = iterm_names; names->type; names++)
537 if (names->type == iterm->type) {
538 strcpy(name, names->name);
539 return strlen(names->name);
541 return 0;
546 * parse the source unit recursively until it reaches to a terminal
547 * or a branched unit.
549 static int check_input_term(mixer_build_t *state, int id, usb_audio_term_t *term)
551 unsigned char *p1;
553 memset(term, 0, sizeof(*term));
554 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
555 term->id = id;
556 switch (p1[2]) {
557 case INPUT_TERMINAL:
558 term->type = combine_word(p1 + 4);
559 term->channels = p1[7];
560 term->chconfig = combine_word(p1 + 8);
561 term->name = p1[11];
562 return 0;
563 case FEATURE_UNIT:
564 id = p1[4];
565 break; /* continue to parse */
566 case MIXER_UNIT:
567 term->type = p1[2] << 16; /* virtual type */
568 term->channels = p1[5 + p1[4]];
569 term->chconfig = combine_word(p1 + 6 + p1[4]);
570 term->name = p1[p1[0] - 1];
571 return 0;
572 case SELECTOR_UNIT:
573 /* call recursively to retrieve the channel info */
574 if (check_input_term(state, p1[5], term) < 0)
575 return -ENODEV;
576 term->type = p1[2] << 16; /* virtual type */
577 term->id = id;
578 term->name = p1[9 + p1[0] - 1];
579 return 0;
580 case PROCESSING_UNIT:
581 case EXTENSION_UNIT:
582 if (p1[6] == 1) {
583 id = p1[7];
584 break; /* continue to parse */
586 term->type = p1[2] << 16; /* virtual type */
587 term->channels = p1[7 + p1[6]];
588 term->chconfig = combine_word(p1 + 8 + p1[6]);
589 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
590 return 0;
591 default:
592 return -ENODEV;
595 return -ENODEV;
600 * Feature Unit
603 /* feature unit control information */
604 struct usb_feature_control_info {
605 const char *name;
606 unsigned int type; /* control type (mute, volume, etc.) */
609 static struct usb_feature_control_info audio_feature_info[] = {
610 { "Mute", USB_MIXER_INV_BOOLEAN },
611 { "Volume", USB_MIXER_S16 },
612 { "Tone Control - Bass", USB_MIXER_S8 },
613 { "Tone Control - Mid", USB_MIXER_S8 },
614 { "Tone Control - Treble", USB_MIXER_S8 },
615 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
616 { "Auto Gain Control", USB_MIXER_BOOLEAN },
617 { "Delay Control", USB_MIXER_U16 },
618 { "Bass Boost", USB_MIXER_BOOLEAN },
619 { "Loudness", USB_MIXER_BOOLEAN },
623 /* private_free callback */
624 static void usb_mixer_elem_free(snd_kcontrol_t *kctl)
626 kfree(kctl->private_data);
627 kctl->private_data = NULL;
632 * interface to ALSA control for feature/mixer units
636 * retrieve the minimum and maximum values for the specified control
638 static int get_min_max(usb_mixer_elem_info_t *cval, int default_min)
640 /* for failsafe */
641 cval->min = default_min;
642 cval->max = cval->min + 1;
643 cval->res = 1;
645 if (cval->val_type == USB_MIXER_BOOLEAN ||
646 cval->val_type == USB_MIXER_INV_BOOLEAN) {
647 cval->initialized = 1;
648 } else {
649 int minchn = 0;
650 if (cval->cmask) {
651 int i;
652 for (i = 0; i < MAX_CHANNELS; i++)
653 if (cval->cmask & (1 << i)) {
654 minchn = i + 1;
655 break;
658 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
659 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
660 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n",
661 cval->id, cval->mixer->ctrlif, cval->control, cval->id);
662 return -EINVAL;
664 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
665 cval->res = 1;
666 } else {
667 int last_valid_res = cval->res;
669 while (cval->res > 1) {
670 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
671 break;
672 cval->res /= 2;
674 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
675 cval->res = last_valid_res;
677 if (cval->res == 0)
678 cval->res = 1;
679 cval->initialized = 1;
681 return 0;
685 /* get a feature/mixer unit info */
686 static int mixer_ctl_feature_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
688 usb_mixer_elem_info_t *cval = kcontrol->private_data;
690 if (cval->val_type == USB_MIXER_BOOLEAN ||
691 cval->val_type == USB_MIXER_INV_BOOLEAN)
692 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
693 else
694 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
695 uinfo->count = cval->channels;
696 if (cval->val_type == USB_MIXER_BOOLEAN ||
697 cval->val_type == USB_MIXER_INV_BOOLEAN) {
698 uinfo->value.integer.min = 0;
699 uinfo->value.integer.max = 1;
700 } else {
701 if (! cval->initialized)
702 get_min_max(cval, 0);
703 uinfo->value.integer.min = 0;
704 uinfo->value.integer.max = (cval->max - cval->min) / cval->res;
706 return 0;
709 /* get the current value from feature/mixer unit */
710 static int mixer_ctl_feature_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
712 usb_mixer_elem_info_t *cval = kcontrol->private_data;
713 int c, cnt, val, err;
715 if (cval->cmask) {
716 cnt = 0;
717 for (c = 0; c < MAX_CHANNELS; c++) {
718 if (cval->cmask & (1 << c)) {
719 err = get_cur_mix_value(cval, c + 1, &val);
720 if (err < 0) {
721 if (cval->mixer->ignore_ctl_error) {
722 ucontrol->value.integer.value[0] = cval->min;
723 return 0;
725 snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
726 return err;
728 val = get_relative_value(cval, val);
729 ucontrol->value.integer.value[cnt] = val;
730 cnt++;
733 } else {
734 /* master channel */
735 err = get_cur_mix_value(cval, 0, &val);
736 if (err < 0) {
737 if (cval->mixer->ignore_ctl_error) {
738 ucontrol->value.integer.value[0] = cval->min;
739 return 0;
741 snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
742 return err;
744 val = get_relative_value(cval, val);
745 ucontrol->value.integer.value[0] = val;
747 return 0;
750 /* put the current value to feature/mixer unit */
751 static int mixer_ctl_feature_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
753 usb_mixer_elem_info_t *cval = kcontrol->private_data;
754 int c, cnt, val, oval, err;
755 int changed = 0;
757 if (cval->cmask) {
758 cnt = 0;
759 for (c = 0; c < MAX_CHANNELS; c++) {
760 if (cval->cmask & (1 << c)) {
761 err = get_cur_mix_value(cval, c + 1, &oval);
762 if (err < 0) {
763 if (cval->mixer->ignore_ctl_error)
764 return 0;
765 return err;
767 val = ucontrol->value.integer.value[cnt];
768 val = get_abs_value(cval, val);
769 if (oval != val) {
770 set_cur_mix_value(cval, c + 1, val);
771 changed = 1;
773 get_cur_mix_value(cval, c + 1, &val);
774 cnt++;
777 } else {
778 /* master channel */
779 err = get_cur_mix_value(cval, 0, &oval);
780 if (err < 0 && cval->mixer->ignore_ctl_error)
781 return 0;
782 if (err < 0)
783 return err;
784 val = ucontrol->value.integer.value[0];
785 val = get_abs_value(cval, val);
786 if (val != oval) {
787 set_cur_mix_value(cval, 0, val);
788 changed = 1;
791 return changed;
794 static snd_kcontrol_new_t usb_feature_unit_ctl = {
795 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
796 .name = "", /* will be filled later manually */
797 .info = mixer_ctl_feature_info,
798 .get = mixer_ctl_feature_get,
799 .put = mixer_ctl_feature_put,
804 * build a feature control
807 static void build_feature_ctl(mixer_build_t *state, unsigned char *desc,
808 unsigned int ctl_mask, int control,
809 usb_audio_term_t *iterm, int unitid)
811 unsigned int len = 0;
812 int mapped_name = 0;
813 int nameid = desc[desc[0] - 1];
814 snd_kcontrol_t *kctl;
815 usb_mixer_elem_info_t *cval;
817 control++; /* change from zero-based to 1-based value */
819 if (control == USB_FEATURE_GEQ) {
820 /* FIXME: not supported yet */
821 return;
824 if (check_ignored_ctl(state, unitid, control))
825 return;
827 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
828 if (! cval) {
829 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
830 return;
832 cval->mixer = state->mixer;
833 cval->id = unitid;
834 cval->control = control;
835 cval->cmask = ctl_mask;
836 cval->val_type = audio_feature_info[control-1].type;
837 if (ctl_mask == 0)
838 cval->channels = 1; /* master channel */
839 else {
840 int i, c = 0;
841 for (i = 0; i < 16; i++)
842 if (ctl_mask & (1 << i))
843 c++;
844 cval->channels = c;
847 /* get min/max values */
848 get_min_max(cval, 0);
850 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
851 if (! kctl) {
852 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
853 kfree(cval);
854 return;
856 kctl->private_free = usb_mixer_elem_free;
858 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
859 mapped_name = len != 0;
860 if (! len && nameid)
861 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
863 switch (control) {
864 case USB_FEATURE_MUTE:
865 case USB_FEATURE_VOLUME:
866 /* determine the control name. the rule is:
867 * - if a name id is given in descriptor, use it.
868 * - if the connected input can be determined, then use the name
869 * of terminal type.
870 * - if the connected output can be determined, use it.
871 * - otherwise, anonymous name.
873 if (! len) {
874 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
875 if (! len)
876 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
877 if (! len)
878 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
879 "Feature %d", unitid);
881 /* determine the stream direction:
882 * if the connected output is USB stream, then it's likely a
883 * capture stream. otherwise it should be playback (hopefully :)
885 if (! mapped_name && ! (state->oterm.type >> 16)) {
886 if ((state->oterm.type & 0xff00) == 0x0100) {
887 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
888 } else {
889 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
892 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
893 sizeof(kctl->id.name));
894 break;
896 default:
897 if (! len)
898 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
899 sizeof(kctl->id.name));
900 break;
903 /* quirk for UDA1321/N101 */
904 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
905 /* is not very clear from datasheets */
906 /* I hope that the min value is -15360 for newer firmware --jk */
907 switch (state->chip->usb_id) {
908 case USB_ID(0x0471, 0x0101):
909 case USB_ID(0x0471, 0x0104):
910 case USB_ID(0x0471, 0x0105):
911 case USB_ID(0x0672, 0x1041):
912 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
913 cval->min == -15616) {
914 snd_printk("using volume control quirk for the UDA1321/N101 chip\n");
915 cval->max = -256;
919 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
920 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
921 add_control_to_empty(state, kctl);
927 * parse a feature unit
929 * most of controlls are defined here.
931 static int parse_audio_feature_unit(mixer_build_t *state, int unitid, unsigned char *ftr)
933 int channels, i, j;
934 usb_audio_term_t iterm;
935 unsigned int master_bits, first_ch_bits;
936 int err, csize;
938 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
939 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
940 return -EINVAL;
943 /* parse the source unit */
944 if ((err = parse_audio_unit(state, ftr[4])) < 0)
945 return err;
947 /* determine the input source type and name */
948 if (check_input_term(state, ftr[4], &iterm) < 0)
949 return -EINVAL;
951 channels = (ftr[0] - 7) / csize - 1;
953 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
954 if (channels > 0)
955 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
956 else
957 first_ch_bits = 0;
958 /* check all control types */
959 for (i = 0; i < 10; i++) {
960 unsigned int ch_bits = 0;
961 for (j = 0; j < channels; j++) {
962 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
963 if (mask & (1 << i))
964 ch_bits |= (1 << j);
966 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
967 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
968 if (master_bits & (1 << i))
969 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
972 return 0;
977 * Mixer Unit
981 * build a mixer unit control
983 * the callbacks are identical with feature unit.
984 * input channel number (zero based) is given in control field instead.
987 static void build_mixer_unit_ctl(mixer_build_t *state, unsigned char *desc,
988 int in_pin, int in_ch, int unitid,
989 usb_audio_term_t *iterm)
991 usb_mixer_elem_info_t *cval;
992 unsigned int input_pins = desc[4];
993 unsigned int num_outs = desc[5 + input_pins];
994 unsigned int i, len;
995 snd_kcontrol_t *kctl;
997 if (check_ignored_ctl(state, unitid, 0))
998 return;
1000 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
1001 if (! cval)
1002 return;
1004 cval->mixer = state->mixer;
1005 cval->id = unitid;
1006 cval->control = in_ch + 1; /* based on 1 */
1007 cval->val_type = USB_MIXER_S16;
1008 for (i = 0; i < num_outs; i++) {
1009 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
1010 cval->cmask |= (1 << i);
1011 cval->channels++;
1015 /* get min/max values */
1016 get_min_max(cval, 0);
1018 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1019 if (! kctl) {
1020 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1021 kfree(cval);
1022 return;
1024 kctl->private_free = usb_mixer_elem_free;
1026 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1027 if (! len)
1028 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
1029 if (! len)
1030 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
1031 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
1033 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
1034 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1035 add_control_to_empty(state, kctl);
1040 * parse a mixer unit
1042 static int parse_audio_mixer_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1044 usb_audio_term_t iterm;
1045 int input_pins, num_ins, num_outs;
1046 int pin, ich, err;
1048 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1049 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1050 return -EINVAL;
1052 /* no bmControls field (e.g. Maya44) -> ignore */
1053 if (desc[0] <= 10 + input_pins) {
1054 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1055 return 0;
1058 num_ins = 0;
1059 ich = 0;
1060 for (pin = 0; pin < input_pins; pin++) {
1061 err = parse_audio_unit(state, desc[5 + pin]);
1062 if (err < 0)
1063 return err;
1064 err = check_input_term(state, desc[5 + pin], &iterm);
1065 if (err < 0)
1066 return err;
1067 num_ins += iterm.channels;
1068 for (; ich < num_ins; ++ich) {
1069 int och, ich_has_controls = 0;
1071 for (och = 0; och < num_outs; ++och) {
1072 if (check_matrix_bitmap(desc + 9 + input_pins,
1073 ich, och, num_outs)) {
1074 ich_has_controls = 1;
1075 break;
1078 if (ich_has_controls)
1079 build_mixer_unit_ctl(state, desc, pin, ich,
1080 unitid, &iterm);
1083 return 0;
1088 * Processing Unit / Extension Unit
1091 /* get callback for processing/extension unit */
1092 static int mixer_ctl_procunit_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1094 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1095 int err, val;
1097 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1098 if (err < 0 && cval->mixer->ignore_ctl_error) {
1099 ucontrol->value.integer.value[0] = cval->min;
1100 return 0;
1102 if (err < 0)
1103 return err;
1104 val = get_relative_value(cval, val);
1105 ucontrol->value.integer.value[0] = val;
1106 return 0;
1109 /* put callback for processing/extension unit */
1110 static int mixer_ctl_procunit_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1112 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1113 int val, oval, err;
1115 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1116 if (err < 0) {
1117 if (cval->mixer->ignore_ctl_error)
1118 return 0;
1119 return err;
1121 val = ucontrol->value.integer.value[0];
1122 val = get_abs_value(cval, val);
1123 if (val != oval) {
1124 set_cur_ctl_value(cval, cval->control << 8, val);
1125 return 1;
1127 return 0;
1130 /* alsa control interface for processing/extension unit */
1131 static snd_kcontrol_new_t mixer_procunit_ctl = {
1132 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1133 .name = "", /* will be filled later */
1134 .info = mixer_ctl_feature_info,
1135 .get = mixer_ctl_procunit_get,
1136 .put = mixer_ctl_procunit_put,
1141 * predefined data for processing units
1143 struct procunit_value_info {
1144 int control;
1145 char *suffix;
1146 int val_type;
1147 int min_value;
1150 struct procunit_info {
1151 int type;
1152 char *name;
1153 struct procunit_value_info *values;
1156 static struct procunit_value_info updown_proc_info[] = {
1157 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1158 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1159 { 0 }
1161 static struct procunit_value_info prologic_proc_info[] = {
1162 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1163 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1164 { 0 }
1166 static struct procunit_value_info threed_enh_proc_info[] = {
1167 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1168 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1169 { 0 }
1171 static struct procunit_value_info reverb_proc_info[] = {
1172 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1173 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1174 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1175 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1176 { 0 }
1178 static struct procunit_value_info chorus_proc_info[] = {
1179 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1180 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1181 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1182 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1183 { 0 }
1185 static struct procunit_value_info dcr_proc_info[] = {
1186 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1187 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1188 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1189 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1190 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1191 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1192 { 0 }
1195 static struct procunit_info procunits[] = {
1196 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1197 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1198 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1199 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1200 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1201 { USB_PROC_DCR, "DCR", dcr_proc_info },
1202 { 0 },
1206 * build a processing/extension unit
1208 static int build_audio_procunit(mixer_build_t *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1210 int num_ins = dsc[6];
1211 usb_mixer_elem_info_t *cval;
1212 snd_kcontrol_t *kctl;
1213 int i, err, nameid, type, len;
1214 struct procunit_info *info;
1215 struct procunit_value_info *valinfo;
1216 static struct procunit_value_info default_value_info[] = {
1217 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1218 { 0 }
1220 static struct procunit_info default_info = {
1221 0, NULL, default_value_info
1224 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1225 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1226 return -EINVAL;
1229 for (i = 0; i < num_ins; i++) {
1230 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1231 return err;
1234 type = combine_word(&dsc[4]);
1235 for (info = list; info && info->type; info++)
1236 if (info->type == type)
1237 break;
1238 if (! info || ! info->type)
1239 info = &default_info;
1241 for (valinfo = info->values; valinfo->control; valinfo++) {
1242 /* FIXME: bitmap might be longer than 8bit */
1243 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1244 continue;
1245 if (check_ignored_ctl(state, unitid, valinfo->control))
1246 continue;
1247 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
1248 if (! cval) {
1249 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1250 return -ENOMEM;
1252 cval->mixer = state->mixer;
1253 cval->id = unitid;
1254 cval->control = valinfo->control;
1255 cval->val_type = valinfo->val_type;
1256 cval->channels = 1;
1258 /* get min/max values */
1259 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1260 /* FIXME: hard-coded */
1261 cval->min = 1;
1262 cval->max = dsc[15];
1263 cval->res = 1;
1264 cval->initialized = 1;
1265 } else
1266 get_min_max(cval, valinfo->min_value);
1268 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1269 if (! kctl) {
1270 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1271 kfree(cval);
1272 return -ENOMEM;
1274 kctl->private_free = usb_mixer_elem_free;
1276 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1278 else if (info->name)
1279 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1280 else {
1281 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1282 len = 0;
1283 if (nameid)
1284 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1285 if (! len)
1286 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1288 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1289 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1291 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1292 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1293 if ((err = add_control_to_empty(state, kctl)) < 0)
1294 return err;
1296 return 0;
1300 static int parse_audio_processing_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1302 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1305 static int parse_audio_extension_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1307 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1312 * Selector Unit
1315 /* info callback for selector unit
1316 * use an enumerator type for routing
1318 static int mixer_ctl_selector_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1320 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1321 char **itemlist = (char **)kcontrol->private_value;
1323 snd_assert(itemlist, return -EINVAL);
1324 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1325 uinfo->count = 1;
1326 uinfo->value.enumerated.items = cval->max;
1327 if ((int)uinfo->value.enumerated.item >= cval->max)
1328 uinfo->value.enumerated.item = cval->max - 1;
1329 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1330 return 0;
1333 /* get callback for selector unit */
1334 static int mixer_ctl_selector_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1336 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1337 int val, err;
1339 err = get_cur_ctl_value(cval, 0, &val);
1340 if (err < 0) {
1341 if (cval->mixer->ignore_ctl_error) {
1342 ucontrol->value.enumerated.item[0] = 0;
1343 return 0;
1345 return err;
1347 val = get_relative_value(cval, val);
1348 ucontrol->value.enumerated.item[0] = val;
1349 return 0;
1352 /* put callback for selector unit */
1353 static int mixer_ctl_selector_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1355 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1356 int val, oval, err;
1358 err = get_cur_ctl_value(cval, 0, &oval);
1359 if (err < 0) {
1360 if (cval->mixer->ignore_ctl_error)
1361 return 0;
1362 return err;
1364 val = ucontrol->value.enumerated.item[0];
1365 val = get_abs_value(cval, val);
1366 if (val != oval) {
1367 set_cur_ctl_value(cval, 0, val);
1368 return 1;
1370 return 0;
1373 /* alsa control interface for selector unit */
1374 static snd_kcontrol_new_t mixer_selectunit_ctl = {
1375 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1376 .name = "", /* will be filled later */
1377 .info = mixer_ctl_selector_info,
1378 .get = mixer_ctl_selector_get,
1379 .put = mixer_ctl_selector_put,
1383 /* private free callback.
1384 * free both private_data and private_value
1386 static void usb_mixer_selector_elem_free(snd_kcontrol_t *kctl)
1388 int i, num_ins = 0;
1390 if (kctl->private_data) {
1391 usb_mixer_elem_info_t *cval = kctl->private_data;
1392 num_ins = cval->max;
1393 kfree(cval);
1394 kctl->private_data = NULL;
1396 if (kctl->private_value) {
1397 char **itemlist = (char **)kctl->private_value;
1398 for (i = 0; i < num_ins; i++)
1399 kfree(itemlist[i]);
1400 kfree(itemlist);
1401 kctl->private_value = 0;
1406 * parse a selector unit
1408 static int parse_audio_selector_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1410 unsigned int num_ins = desc[4];
1411 unsigned int i, nameid, len;
1412 int err;
1413 usb_mixer_elem_info_t *cval;
1414 snd_kcontrol_t *kctl;
1415 char **namelist;
1417 if (! num_ins || desc[0] < 6 + num_ins) {
1418 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1419 return -EINVAL;
1422 for (i = 0; i < num_ins; i++) {
1423 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1424 return err;
1427 if (num_ins == 1) /* only one ? nonsense! */
1428 return 0;
1430 if (check_ignored_ctl(state, unitid, 0))
1431 return 0;
1433 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
1434 if (! cval) {
1435 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1436 return -ENOMEM;
1438 cval->mixer = state->mixer;
1439 cval->id = unitid;
1440 cval->val_type = USB_MIXER_U8;
1441 cval->channels = 1;
1442 cval->min = 1;
1443 cval->max = num_ins;
1444 cval->res = 1;
1445 cval->initialized = 1;
1447 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1448 if (! namelist) {
1449 snd_printk(KERN_ERR "cannot malloc\n");
1450 kfree(cval);
1451 return -ENOMEM;
1453 #define MAX_ITEM_NAME_LEN 64
1454 for (i = 0; i < num_ins; i++) {
1455 usb_audio_term_t iterm;
1456 len = 0;
1457 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1458 if (! namelist[i]) {
1459 snd_printk(KERN_ERR "cannot malloc\n");
1460 while (--i > 0)
1461 kfree(namelist[i]);
1462 kfree(namelist);
1463 kfree(cval);
1464 return -ENOMEM;
1466 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1467 MAX_ITEM_NAME_LEN);
1468 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1469 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1470 if (! len)
1471 sprintf(namelist[i], "Input %d", i);
1474 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1475 if (! kctl) {
1476 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1477 kfree(cval);
1478 return -ENOMEM;
1480 kctl->private_value = (unsigned long)namelist;
1481 kctl->private_free = usb_mixer_selector_elem_free;
1483 nameid = desc[desc[0] - 1];
1484 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1485 if (len)
1487 else if (nameid)
1488 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1489 else {
1490 len = get_term_name(state, &state->oterm,
1491 kctl->id.name, sizeof(kctl->id.name), 0);
1492 if (! len)
1493 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1495 if ((state->oterm.type & 0xff00) == 0x0100)
1496 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1497 else
1498 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1501 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1502 cval->id, kctl->id.name, num_ins);
1503 if ((err = add_control_to_empty(state, kctl)) < 0)
1504 return err;
1506 return 0;
1511 * parse an audio unit recursively
1514 static int parse_audio_unit(mixer_build_t *state, int unitid)
1516 unsigned char *p1;
1518 if (test_and_set_bit(unitid, state->unitbitmap))
1519 return 0; /* the unit already visited */
1521 p1 = find_audio_control_unit(state, unitid);
1522 if (!p1) {
1523 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1524 return -EINVAL;
1527 switch (p1[2]) {
1528 case INPUT_TERMINAL:
1529 return 0; /* NOP */
1530 case MIXER_UNIT:
1531 return parse_audio_mixer_unit(state, unitid, p1);
1532 case SELECTOR_UNIT:
1533 return parse_audio_selector_unit(state, unitid, p1);
1534 case FEATURE_UNIT:
1535 return parse_audio_feature_unit(state, unitid, p1);
1536 case PROCESSING_UNIT:
1537 return parse_audio_processing_unit(state, unitid, p1);
1538 case EXTENSION_UNIT:
1539 return parse_audio_extension_unit(state, unitid, p1);
1540 default:
1541 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1542 return -EINVAL;
1546 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
1548 kfree(mixer->id_elems);
1549 if (mixer->urb) {
1550 kfree(mixer->urb->transfer_buffer);
1551 usb_free_urb(mixer->urb);
1553 if (mixer->rc_urb)
1554 usb_free_urb(mixer->rc_urb);
1555 kfree(mixer->rc_setup_packet);
1556 kfree(mixer);
1559 static int snd_usb_mixer_dev_free(snd_device_t *device)
1561 struct usb_mixer_interface *mixer = device->device_data;
1562 snd_usb_mixer_free(mixer);
1563 return 0;
1567 * create mixer controls
1569 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1571 static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1573 unsigned char *desc;
1574 mixer_build_t state;
1575 int err;
1576 const struct usbmix_ctl_map *map;
1577 struct usb_host_interface *hostif;
1579 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1580 memset(&state, 0, sizeof(state));
1581 state.chip = mixer->chip;
1582 state.mixer = mixer;
1583 state.buffer = hostif->extra;
1584 state.buflen = hostif->extralen;
1586 /* check the mapping table */
1587 for (map = usbmix_ctl_maps; map->id; map++) {
1588 if (map->id == state.chip->usb_id) {
1589 state.map = map->map;
1590 state.selector_map = map->selector_map;
1591 mixer->ignore_ctl_error = map->ignore_ctl_error;
1592 break;
1596 desc = NULL;
1597 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1598 if (desc[0] < 9)
1599 continue; /* invalid descriptor? */
1600 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1601 state.oterm.id = desc[3];
1602 state.oterm.type = combine_word(&desc[4]);
1603 state.oterm.name = desc[8];
1604 err = parse_audio_unit(&state, desc[7]);
1605 if (err < 0)
1606 return err;
1608 return 0;
1611 static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
1612 int unitid)
1614 usb_mixer_elem_info_t *info;
1616 for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
1617 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1618 info->elem_id);
1621 static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
1622 int unitid)
1624 if (mixer->rc_type == RC_NONE)
1625 return;
1626 /* unit ids specific to Extigy/Audigy 2 NX: */
1627 switch (unitid) {
1628 case 0: /* remote control */
1629 mixer->rc_urb->dev = mixer->chip->dev;
1630 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1631 break;
1632 case 4: /* digital in jack */
1633 case 7: /* line in jacks */
1634 case 19: /* speaker out jacks */
1635 case 20: /* headphones out jack */
1636 break;
1637 default:
1638 snd_printd(KERN_DEBUG "memory change in unknown unit %d\n", unitid);
1639 break;
1643 static void snd_usb_mixer_status_complete(struct urb *urb, struct pt_regs *regs)
1645 struct usb_mixer_interface *mixer = urb->context;
1647 if (urb->status == 0) {
1648 u8 *buf = urb->transfer_buffer;
1649 int i;
1651 for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
1652 snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
1653 buf[0], buf[1]);
1654 /* ignore any notifications not from the control interface */
1655 if ((buf[0] & 0x0f) != 0)
1656 continue;
1657 if (!(buf[0] & 0x40))
1658 snd_usb_mixer_notify_id(mixer, buf[1]);
1659 else
1660 snd_usb_mixer_memory_change(mixer, buf[1]);
1663 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
1664 urb->dev = mixer->chip->dev;
1665 usb_submit_urb(urb, GFP_ATOMIC);
1669 /* create the handler for the optional status interrupt endpoint */
1670 static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
1672 struct usb_host_interface *hostif;
1673 struct usb_endpoint_descriptor *ep;
1674 void *transfer_buffer;
1675 int buffer_length;
1676 unsigned int epnum;
1678 hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
1679 /* we need one interrupt input endpoint */
1680 if (get_iface_desc(hostif)->bNumEndpoints < 1)
1681 return 0;
1682 ep = get_endpoint(hostif, 0);
1683 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1684 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1685 return 0;
1687 epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1688 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
1689 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
1690 if (!transfer_buffer)
1691 return -ENOMEM;
1692 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1693 if (!mixer->urb) {
1694 kfree(transfer_buffer);
1695 return -ENOMEM;
1697 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
1698 usb_rcvintpipe(mixer->chip->dev, epnum),
1699 transfer_buffer, buffer_length,
1700 snd_usb_mixer_status_complete, mixer, ep->bInterval);
1701 usb_submit_urb(mixer->urb, GFP_KERNEL);
1702 return 0;
1705 static void snd_usb_soundblaster_remote_complete(struct urb *urb,
1706 struct pt_regs *regs)
1708 struct usb_mixer_interface *mixer = urb->context;
1710 * format of remote control data:
1711 * Extigy: xx 00
1712 * Audigy 2 NX: 06 80 xx 00 00 00
1714 int offset = mixer->rc_type == RC_EXTIGY ? 0 : 2;
1715 u32 code;
1717 if (urb->status < 0 || urb->actual_length <= offset)
1718 return;
1719 code = mixer->rc_buffer[offset];
1720 /* the Mute button actually changes the mixer control */
1721 if (code == 13)
1722 snd_usb_mixer_notify_id(mixer, 18);
1723 mixer->rc_code = code;
1724 wmb();
1725 wake_up(&mixer->rc_waitq);
1728 static int snd_usb_sbrc_hwdep_open(snd_hwdep_t *hw, struct file *file)
1730 struct usb_mixer_interface *mixer = hw->private_data;
1732 if (test_and_set_bit(0, &mixer->rc_hwdep_open))
1733 return -EBUSY;
1734 return 0;
1737 static int snd_usb_sbrc_hwdep_release(snd_hwdep_t *hw, struct file *file)
1739 struct usb_mixer_interface *mixer = hw->private_data;
1741 clear_bit(0, &mixer->rc_hwdep_open);
1742 smp_mb__after_clear_bit();
1743 return 0;
1746 static long snd_usb_sbrc_hwdep_read(snd_hwdep_t *hw, char __user *buf,
1747 long count, loff_t *offset)
1749 struct usb_mixer_interface *mixer = hw->private_data;
1750 int err;
1751 u32 rc_code;
1753 if (count != 1 && count != 4)
1754 return -EINVAL;
1755 err = wait_event_interruptible(mixer->rc_waitq,
1756 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
1757 if (err == 0) {
1758 if (count == 1)
1759 err = put_user(rc_code, buf);
1760 else
1761 err = put_user(rc_code, (u32 __user *)buf);
1763 return err < 0 ? err : count;
1766 static unsigned int snd_usb_sbrc_hwdep_poll(snd_hwdep_t *hw, struct file *file,
1767 poll_table *wait)
1769 struct usb_mixer_interface *mixer = hw->private_data;
1771 poll_wait(file, &mixer->rc_waitq, wait);
1772 return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
1775 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
1777 snd_hwdep_t *hwdep;
1778 int err, len;
1780 switch (mixer->chip->usb_id) {
1781 case USB_ID(0x041e, 0x3000):
1782 mixer->rc_type = RC_EXTIGY;
1783 len = 2;
1784 break;
1785 case USB_ID(0x041e, 0x3020):
1786 mixer->rc_type = RC_AUDIGY2NX;
1787 len = 6;
1788 break;
1789 default:
1790 return 0;
1793 init_waitqueue_head(&mixer->rc_waitq);
1794 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
1795 if (err < 0)
1796 return err;
1797 snprintf(hwdep->name, sizeof(hwdep->name),
1798 "%s remote control", mixer->chip->card->shortname);
1799 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
1800 hwdep->private_data = mixer;
1801 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
1802 hwdep->ops.open = snd_usb_sbrc_hwdep_open;
1803 hwdep->ops.release = snd_usb_sbrc_hwdep_release;
1804 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
1806 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
1807 if (!mixer->rc_urb)
1808 return -ENOMEM;
1809 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
1810 if (!mixer->rc_setup_packet) {
1811 usb_free_urb(mixer->rc_urb);
1812 mixer->rc_urb = NULL;
1813 return -ENOMEM;
1815 mixer->rc_setup_packet->bRequestType =
1816 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1817 mixer->rc_setup_packet->bRequest = GET_MEM;
1818 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
1819 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
1820 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
1821 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
1822 usb_rcvctrlpipe(mixer->chip->dev, 0),
1823 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
1824 snd_usb_soundblaster_remote_complete, mixer);
1825 return 0;
1828 static int snd_audigy2nx_led_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1830 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1831 uinfo->count = 1;
1832 uinfo->value.integer.min = 0;
1833 uinfo->value.integer.max = 1;
1834 return 0;
1837 static int snd_audigy2nx_led_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1839 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1840 int index = kcontrol->private_value;
1842 ucontrol->value.integer.value[0] = mixer->audigy2nx_leds[index];
1843 return 0;
1846 static int snd_audigy2nx_led_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1848 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
1849 int index = kcontrol->private_value;
1850 int value = ucontrol->value.integer.value[0];
1851 int err, changed;
1853 if (value > 1)
1854 return -EINVAL;
1855 changed = value != mixer->audigy2nx_leds[index];
1856 err = snd_usb_ctl_msg(mixer->chip->dev,
1857 usb_sndctrlpipe(mixer->chip->dev, 0), 0x24,
1858 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1859 value, index + 2, NULL, 0, 100);
1860 if (err < 0)
1861 return err;
1862 mixer->audigy2nx_leds[index] = value;
1863 return changed;
1866 static snd_kcontrol_new_t snd_audigy2nx_controls[] = {
1868 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1869 .name = "CMSS LED Switch",
1870 .info = snd_audigy2nx_led_info,
1871 .get = snd_audigy2nx_led_get,
1872 .put = snd_audigy2nx_led_put,
1873 .private_value = 0,
1876 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1877 .name = "Power LED Switch",
1878 .info = snd_audigy2nx_led_info,
1879 .get = snd_audigy2nx_led_get,
1880 .put = snd_audigy2nx_led_put,
1881 .private_value = 1,
1884 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1885 .name = "Dolby Digital LED Switch",
1886 .info = snd_audigy2nx_led_info,
1887 .get = snd_audigy2nx_led_get,
1888 .put = snd_audigy2nx_led_put,
1889 .private_value = 2,
1893 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
1895 int i, err;
1897 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_controls); ++i) {
1898 err = snd_ctl_add(mixer->chip->card,
1899 snd_ctl_new1(&snd_audigy2nx_controls[i], mixer));
1900 if (err < 0)
1901 return err;
1903 mixer->audigy2nx_leds[1] = 1; /* Power LED is on by default */
1904 return 0;
1907 static void snd_audigy2nx_proc_read(snd_info_entry_t *entry,
1908 snd_info_buffer_t *buffer)
1910 static const struct {
1911 int unitid;
1912 const char *name;
1913 } jacks[] = {
1914 {4, "dig in "},
1915 {7, "line in"},
1916 {19, "spk out"},
1917 {20, "hph out"},
1919 struct usb_mixer_interface *mixer = entry->private_data;
1920 int i, err;
1921 u8 buf[3];
1923 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
1924 for (i = 0; i < ARRAY_SIZE(jacks); ++i) {
1925 snd_iprintf(buffer, "%s: ", jacks[i].name);
1926 err = snd_usb_ctl_msg(mixer->chip->dev,
1927 usb_rcvctrlpipe(mixer->chip->dev, 0),
1928 GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
1929 USB_RECIP_INTERFACE, 0,
1930 jacks[i].unitid << 8, buf, 3, 100);
1931 if (err == 3 && buf[0] == 3)
1932 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
1933 else
1934 snd_iprintf(buffer, "?\n");
1938 int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif)
1940 static snd_device_ops_t dev_ops = {
1941 .dev_free = snd_usb_mixer_dev_free
1943 struct usb_mixer_interface *mixer;
1944 int err;
1946 strcpy(chip->card->mixername, "USB Mixer");
1948 mixer = kcalloc(1, sizeof(*mixer), GFP_KERNEL);
1949 if (!mixer)
1950 return -ENOMEM;
1951 mixer->chip = chip;
1952 mixer->ctrlif = ctrlif;
1953 #ifdef IGNORE_CTL_ERROR
1954 mixer->ignore_ctl_error = 1;
1955 #endif
1956 mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
1957 if (!mixer->id_elems) {
1958 kfree(mixer);
1959 return -ENOMEM;
1962 if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
1963 (err = snd_usb_mixer_status_create(mixer)) < 0)
1964 goto _error;
1966 if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
1967 goto _error;
1969 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020)) {
1970 snd_info_entry_t *entry;
1972 if ((err = snd_audigy2nx_controls_create(mixer)) < 0)
1973 goto _error;
1974 if (!snd_card_proc_new(chip->card, "audigy2nx", &entry))
1975 snd_info_set_text_ops(entry, mixer, 1024,
1976 snd_audigy2nx_proc_read);
1979 err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
1980 if (err < 0)
1981 goto _error;
1982 list_add(&mixer->list, &chip->mixer_list);
1983 return 0;
1985 _error:
1986 snd_usb_mixer_free(mixer);
1987 return err;
1990 void snd_usb_mixer_disconnect(struct list_head *p)
1992 struct usb_mixer_interface *mixer;
1994 mixer = list_entry(p, struct usb_mixer_interface, list);
1995 if (mixer->urb)
1996 usb_kill_urb(mixer->urb);
1997 if (mixer->rc_urb)
1998 usb_kill_urb(mixer->rc_urb);