[ALSA] usb-audio - reduce size of unitbitmap array
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / usb / usbmixer.c
CommitLineData
1da177e4
LT
1/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Mixer control part
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
11 *
12 *
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.
17 *
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.
22 *
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
26 *
27 */
28
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
39#include "usbaudio.h"
40
41
42/*
43 */
44
45/* ignore error from controls - for debugging */
46/* #define IGNORE_CTL_ERROR */
47
48typedef struct usb_mixer_build mixer_build_t;
49typedef struct usb_audio_term usb_audio_term_t;
50typedef struct usb_mixer_elem_info usb_mixer_elem_info_t;
51
52
53struct usb_audio_term {
54 int id;
55 int type;
56 int channels;
57 unsigned int chconfig;
58 int name;
59};
60
61struct usbmix_name_map;
62
63struct usb_mixer_build {
64 snd_usb_audio_t *chip;
65 unsigned char *buffer;
66 unsigned int buflen;
67 unsigned int ctrlif;
68 unsigned short vendor;
69 unsigned short product;
707e6073 70 DECLARE_BITMAP(unitbitmap, 256);
1da177e4
LT
71 usb_audio_term_t oterm;
72 const struct usbmix_name_map *map;
8e062ec7 73 const struct usbmix_selector_map *selector_map;
1da177e4
LT
74};
75
76struct usb_mixer_elem_info {
77 snd_usb_audio_t *chip;
78 unsigned int ctrlif;
79 unsigned int id;
80 unsigned int control; /* CS or ICN (high byte) */
81 unsigned int cmask; /* channel mask bitmap: 0 = master */
82 int channels;
83 int val_type;
84 int min, max, res;
85 unsigned int initialized: 1;
86};
87
88
89enum {
90 USB_FEATURE_NONE = 0,
91 USB_FEATURE_MUTE = 1,
92 USB_FEATURE_VOLUME,
93 USB_FEATURE_BASS,
94 USB_FEATURE_MID,
95 USB_FEATURE_TREBLE,
96 USB_FEATURE_GEQ,
97 USB_FEATURE_AGC,
98 USB_FEATURE_DELAY,
99 USB_FEATURE_BASSBOOST,
100 USB_FEATURE_LOUDNESS
101};
102
103enum {
104 USB_MIXER_BOOLEAN,
105 USB_MIXER_INV_BOOLEAN,
106 USB_MIXER_S8,
107 USB_MIXER_U8,
108 USB_MIXER_S16,
109 USB_MIXER_U16,
110};
111
112enum {
113 USB_PROC_UPDOWN = 1,
114 USB_PROC_UPDOWN_SWITCH = 1,
115 USB_PROC_UPDOWN_MODE_SEL = 2,
116
117 USB_PROC_PROLOGIC = 2,
118 USB_PROC_PROLOGIC_SWITCH = 1,
119 USB_PROC_PROLOGIC_MODE_SEL = 2,
120
121 USB_PROC_3DENH = 3,
122 USB_PROC_3DENH_SWITCH = 1,
123 USB_PROC_3DENH_SPACE = 2,
124
125 USB_PROC_REVERB = 4,
126 USB_PROC_REVERB_SWITCH = 1,
127 USB_PROC_REVERB_LEVEL = 2,
128 USB_PROC_REVERB_TIME = 3,
129 USB_PROC_REVERB_DELAY = 4,
130
131 USB_PROC_CHORUS = 5,
132 USB_PROC_CHORUS_SWITCH = 1,
133 USB_PROC_CHORUS_LEVEL = 2,
134 USB_PROC_CHORUS_RATE = 3,
135 USB_PROC_CHORUS_DEPTH = 4,
136
137 USB_PROC_DCR = 6,
138 USB_PROC_DCR_SWITCH = 1,
139 USB_PROC_DCR_RATIO = 2,
140 USB_PROC_DCR_MAX_AMP = 3,
141 USB_PROC_DCR_THRESHOLD = 4,
142 USB_PROC_DCR_ATTACK = 5,
143 USB_PROC_DCR_RELEASE = 6,
144};
145
146#define MAX_CHANNELS 10 /* max logical channels */
147
148
149/*
150 * manual mapping of mixer names
151 * if the mixer topology is too complicated and the parsed names are
152 * ambiguous, add the entries in usbmixer_maps.c.
153 */
154#include "usbmixer_maps.c"
155
156/* get the mapped name if the unit matches */
157static int check_mapped_name(mixer_build_t *state, int unitid, int control, char *buf, int buflen)
158{
159 const struct usbmix_name_map *p;
160
161 if (! state->map)
162 return 0;
163
164 for (p = state->map; p->id; p++) {
165 if (p->id == unitid && p->name &&
166 (! control || ! p->control || control == p->control)) {
167 buflen--;
168 return strlcpy(buf, p->name, buflen);
169 }
170 }
171 return 0;
172}
173
174/* check whether the control should be ignored */
175static int check_ignored_ctl(mixer_build_t *state, int unitid, int control)
176{
177 const struct usbmix_name_map *p;
178
179 if (! state->map)
180 return 0;
181 for (p = state->map; p->id; p++) {
182 if (p->id == unitid && ! p->name &&
183 (! control || ! p->control || control == p->control)) {
184 // printk("ignored control %d:%d\n", unitid, control);
185 return 1;
186 }
187 }
188 return 0;
189}
190
8e062ec7
CL
191/* get the mapped selector source name */
192static int check_mapped_selector_name(mixer_build_t *state, int unitid,
193 int index, char *buf, int buflen)
194{
195 const struct usbmix_selector_map *p;
196
197 if (! state->selector_map)
198 return 0;
199 for (p = state->selector_map; p->id; p++) {
200 if (p->id == unitid && index < p->count)
201 return strlcpy(buf, p->names[index], buflen);
202 }
203 return 0;
204}
205
1da177e4
LT
206/*
207 * find an audio control unit with the given unit id
208 */
209static void *find_audio_control_unit(mixer_build_t *state, unsigned char unit)
210{
211 unsigned char *p;
212
213 p = NULL;
214 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
215 USB_DT_CS_INTERFACE)) != NULL) {
216 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
217 return p;
218 }
219 return NULL;
220}
221
222
223/*
224 * copy a string with the given id
225 */
226static int snd_usb_copy_string_desc(mixer_build_t *state, int index, char *buf, int maxlen)
227{
228 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
229 buf[len] = 0;
230 return len;
231}
232
233/*
234 * convert from the byte/word on usb descriptor to the zero-based integer
235 */
236static int convert_signed_value(usb_mixer_elem_info_t *cval, int val)
237{
238 switch (cval->val_type) {
239 case USB_MIXER_BOOLEAN:
240 return !!val;
241 case USB_MIXER_INV_BOOLEAN:
242 return !val;
243 case USB_MIXER_U8:
244 val &= 0xff;
245 break;
246 case USB_MIXER_S8:
247 val &= 0xff;
248 if (val >= 0x80)
249 val -= 0x100;
250 break;
251 case USB_MIXER_U16:
252 val &= 0xffff;
253 break;
254 case USB_MIXER_S16:
255 val &= 0xffff;
256 if (val >= 0x8000)
257 val -= 0x10000;
258 break;
259 }
260 return val;
261}
262
263/*
264 * convert from the zero-based int to the byte/word for usb descriptor
265 */
266static int convert_bytes_value(usb_mixer_elem_info_t *cval, int val)
267{
268 switch (cval->val_type) {
269 case USB_MIXER_BOOLEAN:
270 return !!val;
271 case USB_MIXER_INV_BOOLEAN:
272 return !val;
273 case USB_MIXER_S8:
274 case USB_MIXER_U8:
275 return val & 0xff;
276 case USB_MIXER_S16:
277 case USB_MIXER_U16:
278 return val & 0xffff;
279 }
280 return 0; /* not reached */
281}
282
283static int get_relative_value(usb_mixer_elem_info_t *cval, int val)
284{
285 if (! cval->res)
286 cval->res = 1;
287 if (val < cval->min)
288 return 0;
289 else if (val > cval->max)
290 return (cval->max - cval->min) / cval->res;
291 else
292 return (val - cval->min) / cval->res;
293}
294
295static int get_abs_value(usb_mixer_elem_info_t *cval, int val)
296{
297 if (val < 0)
298 return cval->min;
299 if (! cval->res)
300 cval->res = 1;
301 val *= cval->res;
302 val += cval->min;
303 if (val > cval->max)
304 return cval->max;
305 return val;
306}
307
308
309/*
310 * retrieve a mixer value
311 */
312
313static int get_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int *value_ret)
314{
315 unsigned char buf[2];
316 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
317 int timeout = 10;
318
319 while (timeout-- > 0) {
320 if (snd_usb_ctl_msg(cval->chip->dev, usb_rcvctrlpipe(cval->chip->dev, 0),
321 request,
322 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
323 validx, cval->ctrlif | (cval->id << 8),
324 buf, val_len, 100) >= 0) {
325 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
326 return 0;
327 }
328 }
329 snd_printdd(KERN_ERR "cannot get ctl value: req = 0x%x, wValue = 0x%x, wIndex = 0x%x, type = %d\n", request, validx, cval->ctrlif | (cval->id << 8), cval->val_type);
330 return -EINVAL;
331}
332
333static int get_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int *value)
334{
335 return get_ctl_value(cval, GET_CUR, validx, value);
336}
337
338/* channel = 0: master, 1 = first channel */
339inline static int get_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int *value)
340{
341 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
342}
343
344/*
345 * set a mixer value
346 */
347
348static int set_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int value_set)
349{
350 unsigned char buf[2];
351 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
352 int timeout = 10;
353
354 value_set = convert_bytes_value(cval, value_set);
355 buf[0] = value_set & 0xff;
356 buf[1] = (value_set >> 8) & 0xff;
357 while (timeout -- > 0)
358 if (snd_usb_ctl_msg(cval->chip->dev, usb_sndctrlpipe(cval->chip->dev, 0),
359 request,
360 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
361 validx, cval->ctrlif | (cval->id << 8),
362 buf, val_len, 100) >= 0)
363 return 0;
364 snd_printdd(KERN_ERR "cannot set ctl value: req = 0x%x, wValue = 0x%x, wIndex = 0x%x, type = %d, data = 0x%x/0x%x\n", request, validx, cval->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
365 return -EINVAL;
366}
367
368static int set_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int value)
369{
370 return set_ctl_value(cval, SET_CUR, validx, value);
371}
372
373inline static int set_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int value)
374{
375 return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
376}
377
378
379/*
380 * parser routines begin here...
381 */
382
383static int parse_audio_unit(mixer_build_t *state, int unitid);
384
385
386/*
387 * check if the input/output channel routing is enabled on the given bitmap.
388 * used for mixer unit parser
389 */
390static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
391{
392 int idx = ich * num_outs + och;
393 return bmap[idx >> 3] & (0x80 >> (idx & 7));
394}
395
396
397/*
398 * add an alsa control element
399 * search and increment the index until an empty slot is found.
400 *
401 * if failed, give up and free the control instance.
402 */
403
404static int add_control_to_empty(snd_card_t *card, snd_kcontrol_t *kctl)
405{
406 int err;
407 while (snd_ctl_find_id(card, &kctl->id))
408 kctl->id.index++;
409 if ((err = snd_ctl_add(card, kctl)) < 0) {
410 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
411 snd_ctl_free_one(kctl);
412 }
413 return err;
414}
415
416
417/*
418 * get a terminal name string
419 */
420
421static struct iterm_name_combo {
422 int type;
423 char *name;
424} iterm_names[] = {
425 { 0x0300, "Output" },
426 { 0x0301, "Speaker" },
427 { 0x0302, "Headphone" },
428 { 0x0303, "HMD Audio" },
429 { 0x0304, "Desktop Speaker" },
430 { 0x0305, "Room Speaker" },
431 { 0x0306, "Com Speaker" },
432 { 0x0307, "LFE" },
433 { 0x0600, "External In" },
434 { 0x0601, "Analog In" },
435 { 0x0602, "Digital In" },
436 { 0x0603, "Line" },
437 { 0x0604, "Legacy In" },
438 { 0x0605, "IEC958 In" },
439 { 0x0606, "1394 DA Stream" },
440 { 0x0607, "1394 DV Stream" },
441 { 0x0700, "Embedded" },
442 { 0x0701, "Noise Source" },
443 { 0x0702, "Equalization Noise" },
444 { 0x0703, "CD" },
445 { 0x0704, "DAT" },
446 { 0x0705, "DCC" },
447 { 0x0706, "MiniDisk" },
448 { 0x0707, "Analog Tape" },
449 { 0x0708, "Phonograph" },
450 { 0x0709, "VCR Audio" },
451 { 0x070a, "Video Disk Audio" },
452 { 0x070b, "DVD Audio" },
453 { 0x070c, "TV Tuner Audio" },
454 { 0x070d, "Satellite Rec Audio" },
455 { 0x070e, "Cable Tuner Audio" },
456 { 0x070f, "DSS Audio" },
457 { 0x0710, "Radio Receiver" },
458 { 0x0711, "Radio Transmitter" },
459 { 0x0712, "Multi-Track Recorder" },
460 { 0x0713, "Synthesizer" },
461 { 0 },
462};
463
464static int get_term_name(mixer_build_t *state, usb_audio_term_t *iterm,
465 unsigned char *name, int maxlen, int term_only)
466{
467 struct iterm_name_combo *names;
468
469 if (iterm->name)
470 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
471
472 /* virtual type - not a real terminal */
473 if (iterm->type >> 16) {
474 if (term_only)
475 return 0;
476 switch (iterm->type >> 16) {
477 case SELECTOR_UNIT:
478 strcpy(name, "Selector"); return 8;
479 case PROCESSING_UNIT:
480 strcpy(name, "Process Unit"); return 12;
481 case EXTENSION_UNIT:
482 strcpy(name, "Ext Unit"); return 8;
483 case MIXER_UNIT:
484 strcpy(name, "Mixer"); return 5;
485 default:
486 return sprintf(name, "Unit %d", iterm->id);
487 }
488 }
489
490 switch (iterm->type & 0xff00) {
491 case 0x0100:
492 strcpy(name, "PCM"); return 3;
493 case 0x0200:
494 strcpy(name, "Mic"); return 3;
495 case 0x0400:
496 strcpy(name, "Headset"); return 7;
497 case 0x0500:
498 strcpy(name, "Phone"); return 5;
499 }
500
501 for (names = iterm_names; names->type; names++)
502 if (names->type == iterm->type) {
503 strcpy(name, names->name);
504 return strlen(names->name);
505 }
506 return 0;
507}
508
509
510/*
511 * parse the source unit recursively until it reaches to a terminal
512 * or a branched unit.
513 */
514static int check_input_term(mixer_build_t *state, int id, usb_audio_term_t *term)
515{
516 unsigned char *p1;
517
518 memset(term, 0, sizeof(*term));
519 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
520 term->id = id;
521 switch (p1[2]) {
522 case INPUT_TERMINAL:
523 term->type = combine_word(p1 + 4);
524 term->channels = p1[7];
525 term->chconfig = combine_word(p1 + 8);
526 term->name = p1[11];
527 return 0;
528 case FEATURE_UNIT:
529 id = p1[4];
530 break; /* continue to parse */
531 case MIXER_UNIT:
532 term->type = p1[2] << 16; /* virtual type */
533 term->channels = p1[5 + p1[4]];
534 term->chconfig = combine_word(p1 + 6 + p1[4]);
535 term->name = p1[p1[0] - 1];
536 return 0;
537 case SELECTOR_UNIT:
538 /* call recursively to retrieve the channel info */
539 if (check_input_term(state, p1[5], term) < 0)
540 return -ENODEV;
541 term->type = p1[2] << 16; /* virtual type */
542 term->id = id;
543 term->name = p1[9 + p1[0] - 1];
544 return 0;
545 case PROCESSING_UNIT:
546 case EXTENSION_UNIT:
547 if (p1[6] == 1) {
548 id = p1[7];
549 break; /* continue to parse */
550 }
551 term->type = p1[2] << 16; /* virtual type */
552 term->channels = p1[7 + p1[6]];
553 term->chconfig = combine_word(p1 + 8 + p1[6]);
554 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
555 return 0;
556 default:
557 return -ENODEV;
558 }
559 }
560 return -ENODEV;
561}
562
563
564/*
565 * Feature Unit
566 */
567
568/* feature unit control information */
569struct usb_feature_control_info {
570 const char *name;
571 unsigned int type; /* control type (mute, volume, etc.) */
572};
573
574static struct usb_feature_control_info audio_feature_info[] = {
575 { "Mute", USB_MIXER_INV_BOOLEAN },
576 { "Volume", USB_MIXER_S16 },
577 { "Tone Control - Bass", USB_MIXER_S8 },
578 { "Tone Control - Mid", USB_MIXER_S8 },
579 { "Tone Control - Treble", USB_MIXER_S8 },
580 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
581 { "Auto Gain Control", USB_MIXER_BOOLEAN },
582 { "Delay Control", USB_MIXER_U16 },
583 { "Bass Boost", USB_MIXER_BOOLEAN },
584 { "Loudness", USB_MIXER_BOOLEAN },
585};
586
587
588/* private_free callback */
589static void usb_mixer_elem_free(snd_kcontrol_t *kctl)
590{
591 if (kctl->private_data) {
592 kfree(kctl->private_data);
593 kctl->private_data = NULL;
594 }
595}
596
597
598/*
599 * interface to ALSA control for feature/mixer units
600 */
601
602/*
603 * retrieve the minimum and maximum values for the specified control
604 */
605static int get_min_max(usb_mixer_elem_info_t *cval, int default_min)
606{
607 /* for failsafe */
608 cval->min = default_min;
609 cval->max = cval->min + 1;
610 cval->res = 1;
611
612 if (cval->val_type == USB_MIXER_BOOLEAN ||
613 cval->val_type == USB_MIXER_INV_BOOLEAN) {
614 cval->initialized = 1;
615 } else {
616 int minchn = 0;
617 if (cval->cmask) {
618 int i;
619 for (i = 0; i < MAX_CHANNELS; i++)
620 if (cval->cmask & (1 << i)) {
621 minchn = i + 1;
622 break;
623 }
624 }
625 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
626 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
627 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n", cval->id, cval->ctrlif, cval->control, cval->id);
628 return -EINVAL;
629 }
630 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
631 cval->res = 1;
632 } else {
633 int last_valid_res = cval->res;
634
635 while (cval->res > 1) {
636 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
637 break;
638 cval->res /= 2;
639 }
640 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
641 cval->res = last_valid_res;
642 }
643 if (cval->res == 0)
644 cval->res = 1;
645 cval->initialized = 1;
646 }
647 return 0;
648}
649
650
651/* get a feature/mixer unit info */
652static int mixer_ctl_feature_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
653{
654 usb_mixer_elem_info_t *cval = kcontrol->private_data;
655
656 if (cval->val_type == USB_MIXER_BOOLEAN ||
657 cval->val_type == USB_MIXER_INV_BOOLEAN)
658 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
659 else
660 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
661 uinfo->count = cval->channels;
662 if (cval->val_type == USB_MIXER_BOOLEAN ||
663 cval->val_type == USB_MIXER_INV_BOOLEAN) {
664 uinfo->value.integer.min = 0;
665 uinfo->value.integer.max = 1;
666 } else {
667 if (! cval->initialized)
668 get_min_max(cval, 0);
669 uinfo->value.integer.min = 0;
670 uinfo->value.integer.max = (cval->max - cval->min) / cval->res;
671 }
672 return 0;
673}
674
675/* get the current value from feature/mixer unit */
676static int mixer_ctl_feature_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
677{
678 usb_mixer_elem_info_t *cval = kcontrol->private_data;
679 int c, cnt, val, err;
680
681 if (cval->cmask) {
682 cnt = 0;
683 for (c = 0; c < MAX_CHANNELS; c++) {
684 if (cval->cmask & (1 << c)) {
685 err = get_cur_mix_value(cval, c + 1, &val);
686 if (err < 0) {
687 if (cval->chip->ignore_ctl_error) {
688 ucontrol->value.integer.value[0] = cval->min;
689 return 0;
690 }
691 snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
692 return err;
693 }
694 val = get_relative_value(cval, val);
695 ucontrol->value.integer.value[cnt] = val;
696 cnt++;
697 }
698 }
699 } else {
700 /* master channel */
701 err = get_cur_mix_value(cval, 0, &val);
702 if (err < 0) {
703 if (cval->chip->ignore_ctl_error) {
704 ucontrol->value.integer.value[0] = cval->min;
705 return 0;
706 }
707 snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
708 return err;
709 }
710 val = get_relative_value(cval, val);
711 ucontrol->value.integer.value[0] = val;
712 }
713 return 0;
714}
715
716/* put the current value to feature/mixer unit */
717static int mixer_ctl_feature_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
718{
719 usb_mixer_elem_info_t *cval = kcontrol->private_data;
720 int c, cnt, val, oval, err;
721 int changed = 0;
722
723 if (cval->cmask) {
724 cnt = 0;
725 for (c = 0; c < MAX_CHANNELS; c++) {
726 if (cval->cmask & (1 << c)) {
727 err = get_cur_mix_value(cval, c + 1, &oval);
728 if (err < 0) {
729 if (cval->chip->ignore_ctl_error)
730 return 0;
731 return err;
732 }
733 val = ucontrol->value.integer.value[cnt];
734 val = get_abs_value(cval, val);
735 if (oval != val) {
736 set_cur_mix_value(cval, c + 1, val);
737 changed = 1;
738 }
739 get_cur_mix_value(cval, c + 1, &val);
740 cnt++;
741 }
742 }
743 } else {
744 /* master channel */
745 err = get_cur_mix_value(cval, 0, &oval);
746 if (err < 0 && cval->chip->ignore_ctl_error)
747 return 0;
748 if (err < 0)
749 return err;
750 val = ucontrol->value.integer.value[0];
751 val = get_abs_value(cval, val);
752 if (val != oval) {
753 set_cur_mix_value(cval, 0, val);
754 changed = 1;
755 }
756 }
757 return changed;
758}
759
760static snd_kcontrol_new_t usb_feature_unit_ctl = {
761 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
762 .name = "", /* will be filled later manually */
763 .info = mixer_ctl_feature_info,
764 .get = mixer_ctl_feature_get,
765 .put = mixer_ctl_feature_put,
766};
767
768
769/*
770 * build a feature control
771 */
772
773static void build_feature_ctl(mixer_build_t *state, unsigned char *desc,
774 unsigned int ctl_mask, int control,
775 usb_audio_term_t *iterm, int unitid)
776{
777 unsigned int len = 0;
778 int mapped_name = 0;
779 int nameid = desc[desc[0] - 1];
780 snd_kcontrol_t *kctl;
781 usb_mixer_elem_info_t *cval;
782
783 control++; /* change from zero-based to 1-based value */
784
785 if (control == USB_FEATURE_GEQ) {
786 /* FIXME: not supported yet */
787 return;
788 }
789
790 if (check_ignored_ctl(state, unitid, control))
791 return;
792
793 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
794 if (! cval) {
795 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
796 return;
797 }
798 cval->chip = state->chip;
799 cval->ctrlif = state->ctrlif;
800 cval->id = unitid;
801 cval->control = control;
802 cval->cmask = ctl_mask;
803 cval->val_type = audio_feature_info[control-1].type;
804 if (ctl_mask == 0)
805 cval->channels = 1; /* master channel */
806 else {
807 int i, c = 0;
808 for (i = 0; i < 16; i++)
809 if (ctl_mask & (1 << i))
810 c++;
811 cval->channels = c;
812 }
813
814 /* get min/max values */
815 get_min_max(cval, 0);
816
817 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
818 if (! kctl) {
819 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
820 kfree(cval);
821 return;
822 }
823 kctl->private_free = usb_mixer_elem_free;
824
825 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
826 mapped_name = len != 0;
827 if (! len && nameid)
828 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
829
830 switch (control) {
831 case USB_FEATURE_MUTE:
832 case USB_FEATURE_VOLUME:
833 /* determine the control name. the rule is:
834 * - if a name id is given in descriptor, use it.
835 * - if the connected input can be determined, then use the name
836 * of terminal type.
837 * - if the connected output can be determined, use it.
838 * - otherwise, anonymous name.
839 */
840 if (! len) {
841 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
842 if (! len)
843 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
844 if (! len)
845 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
846 "Feature %d", unitid);
847 }
848 /* determine the stream direction:
849 * if the connected output is USB stream, then it's likely a
850 * capture stream. otherwise it should be playback (hopefully :)
851 */
852 if (! mapped_name && ! (state->oterm.type >> 16)) {
853 if ((state->oterm.type & 0xff00) == 0x0100) {
854 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
855 } else {
856 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
857 }
858 }
859 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
860 sizeof(kctl->id.name));
861 break;
862
863 default:
864 if (! len)
865 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
866 sizeof(kctl->id.name));
867 break;
868 }
869
870 /* quirk for UDA1321/N101 */
871 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
872 /* is not very clear from datasheets */
873 /* I hope that the min value is -15360 for newer firmware --jk */
874 if (((state->vendor == 0x471 && (state->product == 0x104 || state->product == 0x105 || state->product == 0x101)) ||
875 (state->vendor == 0x672 && state->product == 0x1041)) && !strcmp(kctl->id.name, "PCM Playback Volume") &&
876 cval->min == -15616) {
877 snd_printk("USB Audio: using volume control quirk for the UDA1321/N101 chip\n");
878 cval->max = -256;
879 }
880
881 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
882 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
883 add_control_to_empty(state->chip->card, kctl);
884}
885
886
887
888/*
889 * parse a feature unit
890 *
891 * most of controlls are defined here.
892 */
893static int parse_audio_feature_unit(mixer_build_t *state, int unitid, unsigned char *ftr)
894{
895 int channels, i, j;
896 usb_audio_term_t iterm;
897 unsigned int master_bits, first_ch_bits;
898 int err, csize;
899
900 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
901 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
902 return -EINVAL;
903 }
904
905 /* parse the source unit */
906 if ((err = parse_audio_unit(state, ftr[4])) < 0)
907 return err;
908
909 /* determine the input source type and name */
910 if (check_input_term(state, ftr[4], &iterm) < 0)
911 return -EINVAL;
912
913 channels = (ftr[0] - 7) / csize - 1;
914
915 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
916 if (channels > 0)
917 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
918 else
919 first_ch_bits = 0;
920 /* check all control types */
921 for (i = 0; i < 10; i++) {
922 unsigned int ch_bits = 0;
923 for (j = 0; j < channels; j++) {
924 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
925 if (mask & (1 << i))
926 ch_bits |= (1 << j);
927 }
928 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
929 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
930 if (master_bits & (1 << i))
931 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
932 }
933
934 return 0;
935}
936
937
938/*
939 * Mixer Unit
940 */
941
942/*
943 * build a mixer unit control
944 *
945 * the callbacks are identical with feature unit.
946 * input channel number (zero based) is given in control field instead.
947 */
948
949static void build_mixer_unit_ctl(mixer_build_t *state, unsigned char *desc,
950 int in_pin, int in_ch, int unitid,
951 usb_audio_term_t *iterm)
952{
953 usb_mixer_elem_info_t *cval;
954 unsigned int input_pins = desc[4];
955 unsigned int num_outs = desc[5 + input_pins];
956 unsigned int i, len;
957 snd_kcontrol_t *kctl;
958
959 if (check_ignored_ctl(state, unitid, 0))
960 return;
961
962 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
963 if (! cval)
964 return;
965
966 cval->chip = state->chip;
967 cval->ctrlif = state->ctrlif;
968 cval->id = unitid;
969 cval->control = in_ch + 1; /* based on 1 */
970 cval->val_type = USB_MIXER_S16;
971 for (i = 0; i < num_outs; i++) {
972 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
973 cval->cmask |= (1 << i);
974 cval->channels++;
975 }
976 }
977
978 /* get min/max values */
979 get_min_max(cval, 0);
980
981 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
982 if (! kctl) {
983 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
984 kfree(cval);
985 return;
986 }
987 kctl->private_free = usb_mixer_elem_free;
988
989 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
990 if (! len)
991 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
992 if (! len)
993 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
994 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
995
996 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
997 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
998 add_control_to_empty(state->chip->card, kctl);
999}
1000
1001
1002/*
1003 * parse a mixer unit
1004 */
1005static int parse_audio_mixer_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1006{
1007 usb_audio_term_t iterm;
1008 int input_pins, num_ins, num_outs;
1009 int pin, ich, err;
1010
1011 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
1012 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
1013 return -EINVAL;
1014 }
1015 /* no bmControls field (e.g. Maya44) -> ignore */
1016 if (desc[0] <= 10 + input_pins) {
1017 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1018 return 0;
1019 }
1020
1021 num_ins = 0;
1022 ich = 0;
1023 for (pin = 0; pin < input_pins; pin++) {
1024 err = parse_audio_unit(state, desc[5 + pin]);
1025 if (err < 0)
1026 return err;
1027 err = check_input_term(state, desc[5 + pin], &iterm);
1028 if (err < 0)
1029 return err;
1030 num_ins += iterm.channels;
1031 for (; ich < num_ins; ++ich) {
1032 int och, ich_has_controls = 0;
1033
1034 for (och = 0; och < num_outs; ++och) {
1035 if (check_matrix_bitmap(desc + 9 + input_pins,
1036 ich, och, num_outs)) {
1037 ich_has_controls = 1;
1038 break;
1039 }
1040 }
1041 if (ich_has_controls)
1042 build_mixer_unit_ctl(state, desc, pin, ich,
1043 unitid, &iterm);
1044 }
1045 }
1046 return 0;
1047}
1048
1049
1050/*
1051 * Processing Unit / Extension Unit
1052 */
1053
1054/* get callback for processing/extension unit */
1055static int mixer_ctl_procunit_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1056{
1057 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1058 int err, val;
1059
1060 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1061 if (err < 0 && cval->chip->ignore_ctl_error) {
1062 ucontrol->value.integer.value[0] = cval->min;
1063 return 0;
1064 }
1065 if (err < 0)
1066 return err;
1067 val = get_relative_value(cval, val);
1068 ucontrol->value.integer.value[0] = val;
1069 return 0;
1070}
1071
1072/* put callback for processing/extension unit */
1073static int mixer_ctl_procunit_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1074{
1075 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1076 int val, oval, err;
1077
1078 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1079 if (err < 0) {
1080 if (cval->chip->ignore_ctl_error)
1081 return 0;
1082 return err;
1083 }
1084 val = ucontrol->value.integer.value[0];
1085 val = get_abs_value(cval, val);
1086 if (val != oval) {
1087 set_cur_ctl_value(cval, cval->control << 8, val);
1088 return 1;
1089 }
1090 return 0;
1091}
1092
1093/* alsa control interface for processing/extension unit */
1094static snd_kcontrol_new_t mixer_procunit_ctl = {
1095 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1096 .name = "", /* will be filled later */
1097 .info = mixer_ctl_feature_info,
1098 .get = mixer_ctl_procunit_get,
1099 .put = mixer_ctl_procunit_put,
1100};
1101
1102
1103/*
1104 * predefined data for processing units
1105 */
1106struct procunit_value_info {
1107 int control;
1108 char *suffix;
1109 int val_type;
1110 int min_value;
1111};
1112
1113struct procunit_info {
1114 int type;
1115 char *name;
1116 struct procunit_value_info *values;
1117};
1118
1119static struct procunit_value_info updown_proc_info[] = {
1120 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1121 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1122 { 0 }
1123};
1124static struct procunit_value_info prologic_proc_info[] = {
1125 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1126 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1127 { 0 }
1128};
1129static struct procunit_value_info threed_enh_proc_info[] = {
1130 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1131 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1132 { 0 }
1133};
1134static struct procunit_value_info reverb_proc_info[] = {
1135 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1136 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1137 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1138 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1139 { 0 }
1140};
1141static struct procunit_value_info chorus_proc_info[] = {
1142 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1143 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1144 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1145 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1146 { 0 }
1147};
1148static struct procunit_value_info dcr_proc_info[] = {
1149 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1150 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1151 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1152 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1153 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1154 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1155 { 0 }
1156};
1157
1158static struct procunit_info procunits[] = {
1159 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1160 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1161 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1162 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1163 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1164 { USB_PROC_DCR, "DCR", dcr_proc_info },
1165 { 0 },
1166};
1167
1168/*
1169 * build a processing/extension unit
1170 */
1171static int build_audio_procunit(mixer_build_t *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1172{
1173 int num_ins = dsc[6];
1174 usb_mixer_elem_info_t *cval;
1175 snd_kcontrol_t *kctl;
1176 int i, err, nameid, type, len;
1177 struct procunit_info *info;
1178 struct procunit_value_info *valinfo;
1179 static struct procunit_value_info default_value_info[] = {
1180 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1181 { 0 }
1182 };
1183 static struct procunit_info default_info = {
1184 0, NULL, default_value_info
1185 };
1186
1187 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1188 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1189 return -EINVAL;
1190 }
1191
1192 for (i = 0; i < num_ins; i++) {
1193 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1194 return err;
1195 }
1196
1197 type = combine_word(&dsc[4]);
1198 if (! type)
1199 return 0; /* undefined? */
1200
1201 for (info = list; info && info->type; info++)
1202 if (info->type == type)
1203 break;
1204 if (! info || ! info->type)
1205 info = &default_info;
1206
1207 for (valinfo = info->values; valinfo->control; valinfo++) {
1208 /* FIXME: bitmap might be longer than 8bit */
1209 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1210 continue;
1211 if (check_ignored_ctl(state, unitid, valinfo->control))
1212 continue;
1213 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
1214 if (! cval) {
1215 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1216 return -ENOMEM;
1217 }
1218 cval->chip = state->chip;
1219 cval->ctrlif = state->ctrlif;
1220 cval->id = unitid;
1221 cval->control = valinfo->control;
1222 cval->val_type = valinfo->val_type;
1223 cval->channels = 1;
1224
1225 /* get min/max values */
1226 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1227 /* FIXME: hard-coded */
1228 cval->min = 1;
1229 cval->max = dsc[15];
1230 cval->res = 1;
1231 cval->initialized = 1;
1232 } else
1233 get_min_max(cval, valinfo->min_value);
1234
1235 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1236 if (! kctl) {
1237 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1238 kfree(cval);
1239 return -ENOMEM;
1240 }
1241 kctl->private_free = usb_mixer_elem_free;
1242
1243 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1244 ;
1245 else if (info->name)
1246 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1247 else {
1248 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1249 len = 0;
1250 if (nameid)
1251 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1252 if (! len)
1253 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1254 }
1255 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1256 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1257
1258 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1259 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1260 if ((err = add_control_to_empty(state->chip->card, kctl)) < 0)
1261 return err;
1262 }
1263 return 0;
1264}
1265
1266
1267static int parse_audio_processing_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1268{
1269 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1270}
1271
1272static int parse_audio_extension_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1273{
1274 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1275}
1276
1277
1278/*
1279 * Selector Unit
1280 */
1281
1282/* info callback for selector unit
1283 * use an enumerator type for routing
1284 */
1285static int mixer_ctl_selector_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1286{
1287 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1288 char **itemlist = (char **)kcontrol->private_value;
1289
1290 snd_assert(itemlist, return -EINVAL);
1291 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1292 uinfo->count = 1;
1293 uinfo->value.enumerated.items = cval->max;
1294 if ((int)uinfo->value.enumerated.item >= cval->max)
1295 uinfo->value.enumerated.item = cval->max - 1;
1296 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1297 return 0;
1298}
1299
1300/* get callback for selector unit */
1301static int mixer_ctl_selector_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1302{
1303 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1304 int val, err;
1305
1306 err = get_cur_ctl_value(cval, 0, &val);
1307 if (err < 0) {
1308 if (cval->chip->ignore_ctl_error) {
1309 ucontrol->value.enumerated.item[0] = 0;
1310 return 0;
1311 }
1312 return err;
1313 }
1314 val = get_relative_value(cval, val);
1315 ucontrol->value.enumerated.item[0] = val;
1316 return 0;
1317}
1318
1319/* put callback for selector unit */
1320static int mixer_ctl_selector_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1321{
1322 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1323 int val, oval, err;
1324
1325 err = get_cur_ctl_value(cval, 0, &oval);
1326 if (err < 0) {
1327 if (cval->chip->ignore_ctl_error)
1328 return 0;
1329 return err;
1330 }
1331 val = ucontrol->value.enumerated.item[0];
1332 val = get_abs_value(cval, val);
1333 if (val != oval) {
1334 set_cur_ctl_value(cval, 0, val);
1335 return 1;
1336 }
1337 return 0;
1338}
1339
1340/* alsa control interface for selector unit */
1341static snd_kcontrol_new_t mixer_selectunit_ctl = {
1342 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1343 .name = "", /* will be filled later */
1344 .info = mixer_ctl_selector_info,
1345 .get = mixer_ctl_selector_get,
1346 .put = mixer_ctl_selector_put,
1347};
1348
1349
1350/* private free callback.
1351 * free both private_data and private_value
1352 */
1353static void usb_mixer_selector_elem_free(snd_kcontrol_t *kctl)
1354{
1355 int i, num_ins = 0;
1356
1357 if (kctl->private_data) {
1358 usb_mixer_elem_info_t *cval = kctl->private_data;
1359 num_ins = cval->max;
1360 kfree(cval);
1361 kctl->private_data = NULL;
1362 }
1363 if (kctl->private_value) {
1364 char **itemlist = (char **)kctl->private_value;
1365 for (i = 0; i < num_ins; i++)
1366 kfree(itemlist[i]);
1367 kfree(itemlist);
1368 kctl->private_value = 0;
1369 }
1370}
1371
1372/*
1373 * parse a selector unit
1374 */
1375static int parse_audio_selector_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1376{
1377 unsigned int num_ins = desc[4];
1378 unsigned int i, nameid, len;
1379 int err;
1380 usb_mixer_elem_info_t *cval;
1381 snd_kcontrol_t *kctl;
1382 char **namelist;
1383
1384 if (! num_ins || desc[0] < 6 + num_ins) {
1385 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1386 return -EINVAL;
1387 }
1388
1389 for (i = 0; i < num_ins; i++) {
1390 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1391 return err;
1392 }
1393
1394 if (num_ins == 1) /* only one ? nonsense! */
1395 return 0;
1396
1397 if (check_ignored_ctl(state, unitid, 0))
1398 return 0;
1399
1400 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
1401 if (! cval) {
1402 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1403 return -ENOMEM;
1404 }
1405 cval->chip = state->chip;
1406 cval->ctrlif = state->ctrlif;
1407 cval->id = unitid;
1408 cval->val_type = USB_MIXER_U8;
1409 cval->channels = 1;
1410 cval->min = 1;
1411 cval->max = num_ins;
1412 cval->res = 1;
1413 cval->initialized = 1;
1414
1415 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1416 if (! namelist) {
1417 snd_printk(KERN_ERR "cannot malloc\n");
1418 kfree(cval);
1419 return -ENOMEM;
1420 }
1421#define MAX_ITEM_NAME_LEN 64
1422 for (i = 0; i < num_ins; i++) {
1423 usb_audio_term_t iterm;
1424 len = 0;
1425 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1426 if (! namelist[i]) {
1427 snd_printk(KERN_ERR "cannot malloc\n");
1428 while (--i > 0)
1429 kfree(namelist[i]);
1430 kfree(namelist);
1431 kfree(cval);
1432 return -ENOMEM;
1433 }
8e062ec7
CL
1434 len = check_mapped_selector_name(state, unitid, i, namelist[i],
1435 MAX_ITEM_NAME_LEN);
1436 if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
1da177e4
LT
1437 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1438 if (! len)
1439 sprintf(namelist[i], "Input %d", i);
1440 }
1441
1442 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1443 if (! kctl) {
1444 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1445 kfree(cval);
1446 return -ENOMEM;
1447 }
1448 kctl->private_value = (unsigned long)namelist;
1449 kctl->private_free = usb_mixer_selector_elem_free;
1450
1451 nameid = desc[desc[0] - 1];
1452 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1453 if (len)
1454 ;
1455 else if (nameid)
1456 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1457 else {
1458 len = get_term_name(state, &state->oterm,
1459 kctl->id.name, sizeof(kctl->id.name), 0);
1460 if (! len)
1461 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1462
1463 if ((state->oterm.type & 0xff00) == 0x0100)
1464 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1465 else
1466 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1467 }
1468
1469 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1470 cval->id, kctl->id.name, num_ins);
1471 if ((err = add_control_to_empty(state->chip->card, kctl)) < 0)
1472 return err;
1473
1474 return 0;
1475}
1476
1477
1478/*
1479 * parse an audio unit recursively
1480 */
1481
1482static int parse_audio_unit(mixer_build_t *state, int unitid)
1483{
1484 unsigned char *p1;
1485
1486 if (test_and_set_bit(unitid, state->unitbitmap))
1487 return 0; /* the unit already visited */
1488
1489 p1 = find_audio_control_unit(state, unitid);
1490 if (!p1) {
1491 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1492 return -EINVAL;
1493 }
1494
1495 switch (p1[2]) {
1496 case INPUT_TERMINAL:
1497 return 0; /* NOP */
1498 case MIXER_UNIT:
1499 return parse_audio_mixer_unit(state, unitid, p1);
1500 case SELECTOR_UNIT:
1501 return parse_audio_selector_unit(state, unitid, p1);
1502 case FEATURE_UNIT:
1503 return parse_audio_feature_unit(state, unitid, p1);
1504 case PROCESSING_UNIT:
1505 return parse_audio_processing_unit(state, unitid, p1);
1506 case EXTENSION_UNIT:
1507 return parse_audio_extension_unit(state, unitid, p1);
1508 default:
1509 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1510 return -EINVAL;
1511 }
1512}
1513
1514/*
1515 * create mixer controls
1516 *
1517 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1518 */
1519int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif)
1520{
1521 unsigned char *desc;
1522 mixer_build_t state;
1523 int err;
1524 const struct usbmix_ctl_map *map;
1525 struct usb_device_descriptor *dev = &chip->dev->descriptor;
1526 struct usb_host_interface *hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
1527
1528 strcpy(chip->card->mixername, "USB Mixer");
1529
1530 memset(&state, 0, sizeof(state));
1531 state.chip = chip;
1532 state.buffer = hostif->extra;
1533 state.buflen = hostif->extralen;
1534 state.ctrlif = ctrlif;
1535 state.vendor = le16_to_cpu(dev->idVendor);
1536 state.product = le16_to_cpu(dev->idProduct);
1537
1538 /* check the mapping table */
1539 for (map = usbmix_ctl_maps; map->vendor; map++) {
1540 if (map->vendor == state.vendor && map->product == state.product) {
1541 state.map = map->map;
8e062ec7 1542 state.selector_map = map->selector_map;
1da177e4
LT
1543 chip->ignore_ctl_error = map->ignore_ctl_error;
1544 break;
1545 }
1546 }
1547#ifdef IGNORE_CTL_ERROR
1548 chip->ignore_ctl_error = 1;
1549#endif
1550
1551 desc = NULL;
1552 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1553 if (desc[0] < 9)
1554 continue; /* invalid descriptor? */
1555 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1556 state.oterm.id = desc[3];
1557 state.oterm.type = combine_word(&desc[4]);
1558 state.oterm.name = desc[8];
1559 err = parse_audio_unit(&state, desc[7]);
1560 if (err < 0)
1561 return err;
1562 }
1563 return 0;
1564}