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