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