ALSA: hda/ca0132 - remove redundant assignment to variable 'changed'
[linux-2.6-block.git] / sound / usb / mixer.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
157a57b6
DM
29/*
30 * TODOs, for both the mixer and the streaming interfaces:
31 *
32 * - support for UAC2 effect units
33 * - support for graphical equalizers
34 * - RANGE and MEM set commands (UAC2)
35 * - RANGE and MEM interrupt dispatchers (UAC2)
36 * - audio channel clustering (UAC2)
37 * - audio sample rate converter units (UAC2)
38 * - proper handling of clock multipliers (UAC2)
39 * - dispatch clock change notifications (UAC2)
40 * - stop PCM streams which use a clock that became invalid
41 * - stop PCM streams which use a clock selector that has changed
42 * - parse available sample rates again when clock sources changed
43 */
44
1da177e4
LT
45#include <linux/bitops.h>
46#include <linux/init.h>
47#include <linux/list.h>
cddaafb9 48#include <linux/log2.h>
1da177e4
LT
49#include <linux/slab.h>
50#include <linux/string.h>
51#include <linux/usb.h>
28e1b773 52#include <linux/usb/audio.h>
23caaf19 53#include <linux/usb/audio-v2.h>
9a2fe9b8 54#include <linux/usb/audio-v3.h>
28e1b773 55
1da177e4
LT
56#include <sound/core.h>
57#include <sound/control.h>
b259b10c 58#include <sound/hwdep.h>
aafad562 59#include <sound/info.h>
7bc5ba7e 60#include <sound/tlv.h>
1da177e4
LT
61
62#include "usbaudio.h"
f0b5e634 63#include "mixer.h"
e5779998 64#include "helper.h"
7b1eda22 65#include "mixer_quirks.h"
88a8516a 66#include "power.h"
4d1a70da 67
ebfdeea3
JK
68#define MAX_ID_ELEMS 256
69
1da177e4
LT
70struct usb_audio_term {
71 int id;
72 int type;
73 int channels;
74 unsigned int chconfig;
75 int name;
76};
77
78struct usbmix_name_map;
79
86e07d34
TI
80struct mixer_build {
81 struct snd_usb_audio *chip;
84957a8a 82 struct usb_mixer_interface *mixer;
1da177e4
LT
83 unsigned char *buffer;
84 unsigned int buflen;
291186e0 85 DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
86e07d34 86 struct usb_audio_term oterm;
1da177e4 87 const struct usbmix_name_map *map;
8e062ec7 88 const struct usbmix_selector_map *selector_map;
1da177e4
LT
89};
90
1cdfa9f3 91/*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
7d2b451e
SK
92enum {
93 USB_XU_CLOCK_RATE = 0xe301,
94 USB_XU_CLOCK_SOURCE = 0xe302,
95 USB_XU_DIGITAL_IO_STATUS = 0xe303,
96 USB_XU_DEVICE_OPTIONS = 0xe304,
97 USB_XU_DIRECT_MONITORING = 0xe305,
98 USB_XU_METERING = 0xe306
99};
100enum {
101 USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
102 USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
103 USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
104 USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
105};
1da177e4
LT
106
107/*
108 * manual mapping of mixer names
109 * if the mixer topology is too complicated and the parsed names are
110 * ambiguous, add the entries in usbmixer_maps.c.
111 */
f0b5e634 112#include "mixer_maps.c"
1da177e4 113
c3a3e040 114static const struct usbmix_name_map *
17156f23 115find_map(const struct usbmix_name_map *p, int unitid, int control)
1da177e4 116{
c3a3e040
JK
117 if (!p)
118 return NULL;
1da177e4 119
17156f23 120 for (; p->id; p++) {
c3a3e040
JK
121 if (p->id == unitid &&
122 (!control || !p->control || control == p->control))
123 return p;
1da177e4 124 }
c3a3e040 125 return NULL;
1da177e4
LT
126}
127
c3a3e040
JK
128/* get the mapped name if the unit matches */
129static int
130check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
1da177e4 131{
c3a3e040
JK
132 if (!p || !p->name)
133 return 0;
1da177e4 134
c3a3e040
JK
135 buflen--;
136 return strlcpy(buf, p->name, buflen);
137}
138
5aeee342
TI
139/* ignore the error value if ignore_ctl_error flag is set */
140#define filter_error(cval, err) \
3360b84b 141 ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
5aeee342 142
c3a3e040
JK
143/* check whether the control should be ignored */
144static inline int
145check_ignored_ctl(const struct usbmix_name_map *p)
146{
147 if (!p || p->name || p->dB)
1da177e4 148 return 0;
c3a3e040
JK
149 return 1;
150}
151
152/* dB mapping */
153static inline void check_mapped_dB(const struct usbmix_name_map *p,
154 struct usb_mixer_elem_info *cval)
155{
156 if (p && p->dB) {
157 cval->dBmin = p->dB->min;
158 cval->dBmax = p->dB->max;
38b65190 159 cval->initialized = 1;
1da177e4 160 }
1da177e4
LT
161}
162
8e062ec7 163/* get the mapped selector source name */
86e07d34 164static int check_mapped_selector_name(struct mixer_build *state, int unitid,
8e062ec7
CL
165 int index, char *buf, int buflen)
166{
167 const struct usbmix_selector_map *p;
168
6bc170e4 169 if (!state->selector_map)
8e062ec7
CL
170 return 0;
171 for (p = state->selector_map; p->id; p++) {
172 if (p->id == unitid && index < p->count)
173 return strlcpy(buf, p->names[index], buflen);
174 }
175 return 0;
176}
177
1da177e4
LT
178/*
179 * find an audio control unit with the given unit id
180 */
6bc170e4
DM
181static void *find_audio_control_unit(struct mixer_build *state,
182 unsigned char unit)
1da177e4 183{
67e1daa0
DM
184 /* we just parse the header */
185 struct uac_feature_unit_descriptor *hdr = NULL;
186
187 while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
188 USB_DT_CS_INTERFACE)) != NULL) {
189 if (hdr->bLength >= 4 &&
190 hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
9a2fe9b8 191 hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER &&
67e1daa0
DM
192 hdr->bUnitID == unit)
193 return hdr;
1da177e4 194 }
67e1daa0 195
1da177e4
LT
196 return NULL;
197}
198
1da177e4
LT
199/*
200 * copy a string with the given id
201 */
eccfc1b8 202static int snd_usb_copy_string_desc(struct snd_usb_audio *chip,
6bc170e4 203 int index, char *buf, int maxlen)
1da177e4 204{
eccfc1b8 205 int len = usb_string(chip->dev, index, buf, maxlen - 1);
251552a2
JK
206
207 if (len < 0)
208 return 0;
209
1da177e4
LT
210 buf[len] = 0;
211 return len;
212}
213
214/*
215 * convert from the byte/word on usb descriptor to the zero-based integer
216 */
86e07d34 217static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
1da177e4
LT
218{
219 switch (cval->val_type) {
220 case USB_MIXER_BOOLEAN:
221 return !!val;
222 case USB_MIXER_INV_BOOLEAN:
223 return !val;
224 case USB_MIXER_U8:
225 val &= 0xff;
226 break;
227 case USB_MIXER_S8:
228 val &= 0xff;
229 if (val >= 0x80)
230 val -= 0x100;
231 break;
232 case USB_MIXER_U16:
233 val &= 0xffff;
234 break;
235 case USB_MIXER_S16:
236 val &= 0xffff;
237 if (val >= 0x8000)
238 val -= 0x10000;
239 break;
240 }
241 return val;
242}
243
244/*
245 * convert from the zero-based int to the byte/word for usb descriptor
246 */
86e07d34 247static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
1da177e4
LT
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_S8:
255 case USB_MIXER_U8:
256 return val & 0xff;
257 case USB_MIXER_S16:
258 case USB_MIXER_U16:
259 return val & 0xffff;
260 }
261 return 0; /* not reached */
262}
263
86e07d34 264static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
1da177e4 265{
6bc170e4 266 if (!cval->res)
1da177e4
LT
267 cval->res = 1;
268 if (val < cval->min)
269 return 0;
14790f1c
TI
270 else if (val >= cval->max)
271 return (cval->max - cval->min + cval->res - 1) / cval->res;
1da177e4
LT
272 else
273 return (val - cval->min) / cval->res;
274}
275
86e07d34 276static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
1da177e4
LT
277{
278 if (val < 0)
279 return cval->min;
6bc170e4 280 if (!cval->res)
1da177e4
LT
281 cval->res = 1;
282 val *= cval->res;
283 val += cval->min;
284 if (val > cval->max)
285 return cval->max;
286 return val;
287}
288
bc18e31c
JS
289static int uac2_ctl_value_size(int val_type)
290{
291 switch (val_type) {
292 case USB_MIXER_S32:
293 case USB_MIXER_U32:
294 return 4;
295 case USB_MIXER_S16:
296 case USB_MIXER_U16:
297 return 2;
298 default:
299 return 1;
300 }
301 return 0; /* unreachable */
302}
303
1da177e4
LT
304
305/*
306 * retrieve a mixer value
307 */
308
6bc170e4
DM
309static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
310 int validx, int *value_ret)
1da177e4 311{
3360b84b 312 struct snd_usb_audio *chip = cval->head.mixer->chip;
1da177e4
LT
313 unsigned char buf[2];
314 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
315 int timeout = 10;
978520b7 316 int idx = 0, err;
1da177e4 317
47ab1545 318 err = snd_usb_lock_shutdown(chip);
88a8516a
ON
319 if (err < 0)
320 return -EIO;
6bc170e4 321
1da177e4 322 while (timeout-- > 0) {
3360b84b 323 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
5a9a8eca
SB
324 err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
325 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
326 validx, idx, buf, val_len);
327 if (err >= val_len) {
1da177e4 328 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
978520b7
TI
329 err = 0;
330 goto out;
5a9a8eca
SB
331 } else if (err == -ETIMEDOUT) {
332 goto out;
1da177e4
LT
333 }
334 }
0ba41d91
TI
335 usb_audio_dbg(chip,
336 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
337 request, validx, idx, cval->val_type);
978520b7
TI
338 err = -EINVAL;
339
340 out:
47ab1545 341 snd_usb_unlock_shutdown(chip);
978520b7 342 return err;
1da177e4
LT
343}
344
6bc170e4
DM
345static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
346 int validx, int *value_ret)
23caaf19 347{
3360b84b 348 struct snd_usb_audio *chip = cval->head.mixer->chip;
447cae58
KM
349 /* enough space for one range */
350 unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
23caaf19 351 unsigned char *val;
447cae58 352 int idx = 0, ret, val_size, size;
23caaf19
DM
353 __u8 bRequest;
354
447cae58
KM
355 val_size = uac2_ctl_value_size(cval->val_type);
356
e8bdb6bb
DM
357 if (request == UAC_GET_CUR) {
358 bRequest = UAC2_CS_CUR;
447cae58 359 size = val_size;
e8bdb6bb
DM
360 } else {
361 bRequest = UAC2_CS_RANGE;
447cae58 362 size = sizeof(__u16) + 3 * val_size;
e8bdb6bb
DM
363 }
364
365 memset(buf, 0, sizeof(buf));
23caaf19 366
47ab1545 367 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
88a8516a
ON
368 if (ret)
369 goto error;
370
47ab1545
TI
371 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
372 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
23caaf19 373 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
978520b7 374 validx, idx, buf, size);
47ab1545 375 snd_usb_unlock_shutdown(chip);
23caaf19
DM
376
377 if (ret < 0) {
88a8516a 378error:
0ba41d91
TI
379 usb_audio_err(chip,
380 "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
381 request, validx, idx, cval->val_type);
23caaf19
DM
382 return ret;
383 }
384
e8bdb6bb
DM
385 /* FIXME: how should we handle multiple triplets here? */
386
23caaf19
DM
387 switch (request) {
388 case UAC_GET_CUR:
389 val = buf;
390 break;
391 case UAC_GET_MIN:
392 val = buf + sizeof(__u16);
393 break;
394 case UAC_GET_MAX:
447cae58 395 val = buf + sizeof(__u16) + val_size;
23caaf19
DM
396 break;
397 case UAC_GET_RES:
447cae58 398 val = buf + sizeof(__u16) + val_size * 2;
23caaf19
DM
399 break;
400 default:
401 return -EINVAL;
402 }
403
447cae58
KM
404 *value_ret = convert_signed_value(cval,
405 snd_usb_combine_bytes(val, val_size));
23caaf19
DM
406
407 return 0;
408}
409
6bc170e4
DM
410static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
411 int validx, int *value_ret)
23caaf19 412{
9f814105
EZ
413 validx += cval->idx_off;
414
3360b84b 415 return (cval->head.mixer->protocol == UAC_VERSION_1) ?
23caaf19
DM
416 get_ctl_value_v1(cval, request, validx, value_ret) :
417 get_ctl_value_v2(cval, request, validx, value_ret);
418}
419
6bc170e4
DM
420static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
421 int validx, int *value)
1da177e4 422{
de48c7bc 423 return get_ctl_value(cval, UAC_GET_CUR, validx, value);
1da177e4
LT
424}
425
426/* channel = 0: master, 1 = first channel */
641b4879
TI
427static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
428 int channel, int *value)
1da177e4 429{
6bc170e4
DM
430 return get_ctl_value(cval, UAC_GET_CUR,
431 (cval->control << 8) | channel,
432 value);
1da177e4
LT
433}
434
eef90451 435int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
641b4879
TI
436 int channel, int index, int *value)
437{
438 int err;
439
440 if (cval->cached & (1 << channel)) {
441 *value = cval->cache_val[index];
442 return 0;
443 }
444 err = get_cur_mix_raw(cval, channel, value);
445 if (err < 0) {
3360b84b
TI
446 if (!cval->head.mixer->ignore_ctl_error)
447 usb_audio_dbg(cval->head.mixer->chip,
0ba41d91 448 "cannot get current value for control %d ch %d: err = %d\n",
6bc170e4 449 cval->control, channel, err);
641b4879
TI
450 return err;
451 }
452 cval->cached |= 1 << channel;
453 cval->cache_val[index] = *value;
454 return 0;
455}
456
1da177e4
LT
457/*
458 * set a mixer value
459 */
460
7b1eda22
DM
461int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
462 int request, int validx, int value_set)
1da177e4 463{
3360b84b 464 struct snd_usb_audio *chip = cval->head.mixer->chip;
bc18e31c 465 unsigned char buf[4];
978520b7 466 int idx = 0, val_len, err, timeout = 10;
23caaf19 467
9f814105
EZ
468 validx += cval->idx_off;
469
9a2fe9b8 470
3360b84b 471 if (cval->head.mixer->protocol == UAC_VERSION_1) {
23caaf19 472 val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
9a2fe9b8 473 } else { /* UAC_VERSION_2/3 */
bc18e31c 474 val_len = uac2_ctl_value_size(cval->val_type);
23caaf19
DM
475
476 /* FIXME */
477 if (request != UAC_SET_CUR) {
0ba41d91 478 usb_audio_dbg(chip, "RANGE setting not yet supported\n");
23caaf19
DM
479 return -EINVAL;
480 }
481
482 request = UAC2_CS_CUR;
483 }
1da177e4
LT
484
485 value_set = convert_bytes_value(cval, value_set);
486 buf[0] = value_set & 0xff;
487 buf[1] = (value_set >> 8) & 0xff;
bc18e31c
JS
488 buf[2] = (value_set >> 16) & 0xff;
489 buf[3] = (value_set >> 24) & 0xff;
47ab1545
TI
490
491 err = snd_usb_lock_shutdown(chip);
88a8516a
ON
492 if (err < 0)
493 return -EIO;
47ab1545 494
978520b7 495 while (timeout-- > 0) {
3360b84b 496 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
5a9a8eca
SB
497 err = snd_usb_ctl_msg(chip->dev,
498 usb_sndctrlpipe(chip->dev, 0), request,
499 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
500 validx, idx, buf, val_len);
501 if (err >= 0) {
978520b7
TI
502 err = 0;
503 goto out;
5a9a8eca
SB
504 } else if (err == -ETIMEDOUT) {
505 goto out;
88a8516a 506 }
978520b7 507 }
0ba41d91 508 usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
6bc170e4 509 request, validx, idx, cval->val_type, buf[0], buf[1]);
978520b7
TI
510 err = -EINVAL;
511
512 out:
47ab1545 513 snd_usb_unlock_shutdown(chip);
978520b7 514 return err;
1da177e4
LT
515}
516
6bc170e4
DM
517static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
518 int validx, int value)
1da177e4 519{
7b1eda22 520 return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
1da177e4
LT
521}
522
eef90451 523int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
641b4879 524 int index, int value)
1da177e4 525{
641b4879 526 int err;
a6a33259
DM
527 unsigned int read_only = (channel == 0) ?
528 cval->master_readonly :
529 cval->ch_readonly & (1 << (channel - 1));
530
531 if (read_only) {
3360b84b 532 usb_audio_dbg(cval->head.mixer->chip,
0ba41d91 533 "%s(): channel %d of control %d is read_only\n",
a6a33259
DM
534 __func__, channel, cval->control);
535 return 0;
536 }
537
6bc170e4
DM
538 err = snd_usb_mixer_set_ctl_value(cval,
539 UAC_SET_CUR, (cval->control << 8) | channel,
540 value);
641b4879
TI
541 if (err < 0)
542 return err;
543 cval->cached |= 1 << channel;
544 cval->cache_val[index] = value;
545 return 0;
1da177e4
LT
546}
547
7bc5ba7e
TI
548/*
549 * TLV callback for mixer volume controls
550 */
285de9c0 551int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
7bc5ba7e
TI
552 unsigned int size, unsigned int __user *_tlv)
553{
554 struct usb_mixer_elem_info *cval = kcontrol->private_data;
b8e1c73f 555 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
7bc5ba7e
TI
556
557 if (size < sizeof(scale))
558 return -ENOMEM;
0f174b35
TI
559 if (cval->min_mute)
560 scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
c3a3e040
JK
561 scale[2] = cval->dBmin;
562 scale[3] = cval->dBmax;
7bc5ba7e
TI
563 if (copy_to_user(_tlv, scale, sizeof(scale)))
564 return -EFAULT;
565 return 0;
566}
1da177e4
LT
567
568/*
569 * parser routines begin here...
570 */
571
86e07d34 572static int parse_audio_unit(struct mixer_build *state, int unitid);
1da177e4
LT
573
574
575/*
576 * check if the input/output channel routing is enabled on the given bitmap.
577 * used for mixer unit parser
578 */
6bc170e4
DM
579static int check_matrix_bitmap(unsigned char *bmap,
580 int ich, int och, int num_outs)
1da177e4
LT
581{
582 int idx = ich * num_outs + och;
583 return bmap[idx >> 3] & (0x80 >> (idx & 7));
584}
585
1da177e4
LT
586/*
587 * add an alsa control element
588 * search and increment the index until an empty slot is found.
589 *
590 * if failed, give up and free the control instance.
591 */
592
3360b84b 593int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
ef9d5970 594 struct snd_kcontrol *kctl)
1da177e4 595{
3360b84b 596 struct usb_mixer_interface *mixer = list->mixer;
1da177e4 597 int err;
84957a8a 598
ef9d5970 599 while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
1da177e4 600 kctl->id.index++;
f25ecf8f
TI
601 err = snd_ctl_add(mixer->chip->card, kctl);
602 if (err < 0) {
6bc170e4
DM
603 usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
604 err);
6639b6c2 605 return err;
1da177e4 606 }
3360b84b
TI
607 list->kctl = kctl;
608 list->next_id_elem = mixer->id_elems[list->id];
609 mixer->id_elems[list->id] = list;
6639b6c2 610 return 0;
1da177e4
LT
611}
612
1da177e4
LT
613/*
614 * get a terminal name string
615 */
616
617static struct iterm_name_combo {
618 int type;
619 char *name;
620} iterm_names[] = {
621 { 0x0300, "Output" },
622 { 0x0301, "Speaker" },
623 { 0x0302, "Headphone" },
624 { 0x0303, "HMD Audio" },
625 { 0x0304, "Desktop Speaker" },
626 { 0x0305, "Room Speaker" },
627 { 0x0306, "Com Speaker" },
628 { 0x0307, "LFE" },
629 { 0x0600, "External In" },
630 { 0x0601, "Analog In" },
631 { 0x0602, "Digital In" },
632 { 0x0603, "Line" },
633 { 0x0604, "Legacy In" },
634 { 0x0605, "IEC958 In" },
635 { 0x0606, "1394 DA Stream" },
636 { 0x0607, "1394 DV Stream" },
637 { 0x0700, "Embedded" },
638 { 0x0701, "Noise Source" },
639 { 0x0702, "Equalization Noise" },
640 { 0x0703, "CD" },
641 { 0x0704, "DAT" },
642 { 0x0705, "DCC" },
643 { 0x0706, "MiniDisk" },
644 { 0x0707, "Analog Tape" },
645 { 0x0708, "Phonograph" },
646 { 0x0709, "VCR Audio" },
647 { 0x070a, "Video Disk Audio" },
648 { 0x070b, "DVD Audio" },
649 { 0x070c, "TV Tuner Audio" },
650 { 0x070d, "Satellite Rec Audio" },
651 { 0x070e, "Cable Tuner Audio" },
652 { 0x070f, "DSS Audio" },
653 { 0x0710, "Radio Receiver" },
654 { 0x0711, "Radio Transmitter" },
655 { 0x0712, "Multi-Track Recorder" },
656 { 0x0713, "Synthesizer" },
657 { 0 },
658};
659
eccfc1b8 660static int get_term_name(struct snd_usb_audio *chip, struct usb_audio_term *iterm,
1da177e4
LT
661 unsigned char *name, int maxlen, int term_only)
662{
663 struct iterm_name_combo *names;
56a23ee5 664 int len;
1da177e4 665
56a23ee5 666 if (iterm->name) {
eccfc1b8 667 len = snd_usb_copy_string_desc(chip, iterm->name,
6bc170e4 668 name, maxlen);
56a23ee5
TI
669 if (len)
670 return len;
671 }
1da177e4
LT
672
673 /* virtual type - not a real terminal */
674 if (iterm->type >> 16) {
675 if (term_only)
676 return 0;
677 switch (iterm->type >> 16) {
8b3a087f 678 case UAC3_SELECTOR_UNIT:
6bc170e4
DM
679 strcpy(name, "Selector");
680 return 8;
8b3a087f 681 case UAC3_PROCESSING_UNIT:
6bc170e4
DM
682 strcpy(name, "Process Unit");
683 return 12;
8b3a087f 684 case UAC3_EXTENSION_UNIT:
6bc170e4
DM
685 strcpy(name, "Ext Unit");
686 return 8;
8b3a087f 687 case UAC3_MIXER_UNIT:
6bc170e4
DM
688 strcpy(name, "Mixer");
689 return 5;
1da177e4
LT
690 default:
691 return sprintf(name, "Unit %d", iterm->id);
692 }
693 }
694
695 switch (iterm->type & 0xff00) {
696 case 0x0100:
6bc170e4
DM
697 strcpy(name, "PCM");
698 return 3;
1da177e4 699 case 0x0200:
6bc170e4
DM
700 strcpy(name, "Mic");
701 return 3;
1da177e4 702 case 0x0400:
6bc170e4
DM
703 strcpy(name, "Headset");
704 return 7;
1da177e4 705 case 0x0500:
6bc170e4
DM
706 strcpy(name, "Phone");
707 return 5;
1da177e4
LT
708 }
709
6bc170e4 710 for (names = iterm_names; names->type; names++) {
1da177e4
LT
711 if (names->type == iterm->type) {
712 strcpy(name, names->name);
713 return strlen(names->name);
714 }
6bc170e4
DM
715 }
716
1da177e4
LT
717 return 0;
718}
719
6cfd839a
JS
720/*
721 * Get logical cluster information for UAC3 devices.
722 */
723static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id)
724{
725 struct uac3_cluster_header_descriptor c_header;
726 int err;
727
728 err = snd_usb_ctl_msg(state->chip->dev,
729 usb_rcvctrlpipe(state->chip->dev, 0),
730 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
731 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
732 cluster_id,
733 snd_usb_ctrl_intf(state->chip),
734 &c_header, sizeof(c_header));
735 if (err < 0)
736 goto error;
737 if (err != sizeof(c_header)) {
738 err = -EIO;
739 goto error;
740 }
741
742 return c_header.bNrChannels;
743
744error:
745 usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err);
746 return err;
747}
748
749/*
750 * Get number of channels for a Mixer Unit.
751 */
752static int uac_mixer_unit_get_channels(struct mixer_build *state,
753 struct uac_mixer_unit_descriptor *desc)
754{
755 int mu_channels;
0bfe5e43 756 void *c;
6cfd839a 757
0bfe5e43 758 if (desc->bLength < sizeof(*desc))
6cfd839a
JS
759 return -EINVAL;
760 if (!desc->bNrInPins)
761 return -EINVAL;
762
763 switch (state->mixer->protocol) {
764 case UAC_VERSION_1:
765 case UAC_VERSION_2:
766 default:
0bfe5e43
TI
767 if (desc->bLength < sizeof(*desc) + desc->bNrInPins + 1)
768 return 0; /* no bmControls -> skip */
6cfd839a
JS
769 mu_channels = uac_mixer_unit_bNrChannels(desc);
770 break;
771 case UAC_VERSION_3:
772 mu_channels = get_cluster_channels_v3(state,
773 uac3_mixer_unit_wClusterDescrID(desc));
774 break;
775 }
776
777 if (!mu_channels)
0bfe5e43
TI
778 return 0;
779
780 c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
781 if (c - (void *)desc + (mu_channels - 1) / 8 >= desc->bLength)
782 return 0; /* no bmControls -> skip */
6cfd839a
JS
783
784 return mu_channels;
785}
786
1da177e4
LT
787/*
788 * parse the source unit recursively until it reaches to a terminal
789 * or a branched unit.
790 */
6bc170e4
DM
791static int check_input_term(struct mixer_build *state, int id,
792 struct usb_audio_term *term)
1da177e4 793{
9a2fe9b8 794 int protocol = state->mixer->protocol;
09414207 795 int err;
23caaf19 796 void *p1;
1da177e4
LT
797
798 memset(term, 0, sizeof(*term));
799 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
23caaf19 800 unsigned char *hdr = p1;
1da177e4 801 term->id = id;
9a2fe9b8
RB
802
803 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
804 switch (hdr[2]) {
805 case UAC_INPUT_TERMINAL:
806 if (protocol == UAC_VERSION_1) {
807 struct uac_input_terminal_descriptor *d = p1;
808
809 term->type = le16_to_cpu(d->wTerminalType);
810 term->channels = d->bNrChannels;
811 term->chconfig = le16_to_cpu(d->wChannelConfig);
812 term->name = d->iTerminal;
813 } else { /* UAC_VERSION_2 */
814 struct uac2_input_terminal_descriptor *d = p1;
815
816 /* call recursively to verify that the
817 * referenced clock entity is valid */
818 err = check_input_term(state, d->bCSourceID, term);
819 if (err < 0)
820 return err;
821
822 /* save input term properties after recursion,
823 * to ensure they are not overriden by the
824 * recursion calls */
825 term->id = id;
826 term->type = le16_to_cpu(d->wTerminalType);
827 term->channels = d->bNrChannels;
828 term->chconfig = le32_to_cpu(d->bmChannelConfig);
829 term->name = d->iTerminal;
830 }
831 return 0;
832 case UAC_FEATURE_UNIT: {
833 /* the header is the same for v1 and v2 */
834 struct uac_feature_unit_descriptor *d = p1;
835
836 id = d->bSourceID;
837 break; /* continue to parse */
838 }
839 case UAC_MIXER_UNIT: {
840 struct uac_mixer_unit_descriptor *d = p1;
841
8b3a087f 842 term->type = UAC3_MIXER_UNIT << 16; /* virtual type */
9a2fe9b8
RB
843 term->channels = uac_mixer_unit_bNrChannels(d);
844 term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
845 term->name = uac_mixer_unit_iMixer(d);
846 return 0;
847 }
848 case UAC_SELECTOR_UNIT:
849 case UAC2_CLOCK_SELECTOR: {
850 struct uac_selector_unit_descriptor *d = p1;
851 /* call recursively to retrieve the channel info */
852 err = check_input_term(state, d->baSourceID[0], term);
853 if (err < 0)
854 return err;
8b3a087f 855 term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */
9a2fe9b8
RB
856 term->id = id;
857 term->name = uac_selector_unit_iSelector(d);
858 return 0;
859 }
860 case UAC1_PROCESSING_UNIT:
8b3a087f
JS
861 /* UAC2_EFFECT_UNIT */
862 if (protocol == UAC_VERSION_1)
863 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
864 else /* UAC_VERSION_2 */
865 term->type = UAC3_EFFECT_UNIT << 16; /* virtual type */
91c6e15e 866 /* fall through */
9a2fe9b8
RB
867 case UAC1_EXTENSION_UNIT:
868 /* UAC2_PROCESSING_UNIT_V2 */
8b3a087f
JS
869 if (protocol == UAC_VERSION_1 && !term->type)
870 term->type = UAC3_EXTENSION_UNIT << 16; /* virtual type */
871 else if (protocol == UAC_VERSION_2 && !term->type)
872 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
91c6e15e 873 /* fall through */
9a2fe9b8
RB
874 case UAC2_EXTENSION_UNIT_V2: {
875 struct uac_processing_unit_descriptor *d = p1;
876
877 if (protocol == UAC_VERSION_2 &&
878 hdr[2] == UAC2_EFFECT_UNIT) {
879 /* UAC2/UAC1 unit IDs overlap here in an
880 * uncompatible way. Ignore this unit for now.
881 */
882 return 0;
883 }
884
885 if (d->bNrInPins) {
886 id = d->baSourceID[0];
887 break; /* continue to parse */
888 }
8b3a087f
JS
889 if (!term->type)
890 term->type = UAC3_EXTENSION_UNIT << 16; /* virtual type */
891
9a2fe9b8
RB
892 term->channels = uac_processing_unit_bNrChannels(d);
893 term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
894 term->name = uac_processing_unit_iProcessing(d, protocol);
895 return 0;
896 }
897 case UAC2_CLOCK_SOURCE: {
898 struct uac_clock_source_descriptor *d = p1;
899
8b3a087f 900 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */
9a2fe9b8
RB
901 term->id = id;
902 term->name = d->iClockSource;
903 return 0;
904 }
905 default:
906 return -ENODEV;
907 }
908 } else { /* UAC_VERSION_3 */
909 switch (hdr[2]) {
910 case UAC_INPUT_TERMINAL: {
911 struct uac3_input_terminal_descriptor *d = p1;
09414207 912
9430e547
JS
913 /* call recursively to verify that the
914 * referenced clock entity is valid */
09414207
DM
915 err = check_input_term(state, d->bCSourceID, term);
916 if (err < 0)
917 return err;
9430e547
JS
918
919 /* save input term properties after recursion,
920 * to ensure they are not overriden by the
921 * recursion calls */
922 term->id = id;
923 term->type = le16_to_cpu(d->wTerminalType);
9a2fe9b8 924
71066945
JS
925 err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID));
926 if (err < 0)
927 return err;
928 term->channels = err;
929
930 /* REVISIT: UAC3 IT doesn't have channels cfg */
9a2fe9b8
RB
931 term->chconfig = 0;
932
933 term->name = le16_to_cpu(d->wTerminalDescrStr);
5dae5fd2
EZ
934 return 0;
935 }
9a2fe9b8
RB
936 case UAC3_FEATURE_UNIT: {
937 struct uac3_feature_unit_descriptor *d = p1;
5dae5fd2 938
9a2fe9b8 939 id = d->bSourceID;
1da177e4
LT
940 break; /* continue to parse */
941 }
9a2fe9b8
RB
942 case UAC3_CLOCK_SOURCE: {
943 struct uac3_clock_source_descriptor *d = p1;
944
8b3a087f 945 term->type = UAC3_CLOCK_SOURCE << 16; /* virtual type */
9a2fe9b8
RB
946 term->id = id;
947 term->name = le16_to_cpu(d->wClockSourceStr);
948 return 0;
949 }
6cfd839a
JS
950 case UAC3_MIXER_UNIT: {
951 struct uac_mixer_unit_descriptor *d = p1;
952
953 err = uac_mixer_unit_get_channels(state, d);
0bfe5e43 954 if (err <= 0)
6cfd839a
JS
955 return err;
956
957 term->channels = err;
8b3a087f 958 term->type = UAC3_MIXER_UNIT << 16; /* virtual type */
6cfd839a
JS
959
960 return 0;
961 }
c77e1ef1
JS
962 case UAC3_SELECTOR_UNIT:
963 case UAC3_CLOCK_SELECTOR: {
964 struct uac_selector_unit_descriptor *d = p1;
965 /* call recursively to retrieve the channel info */
966 err = check_input_term(state, d->baSourceID[0], term);
967 if (err < 0)
968 return err;
8b3a087f 969 term->type = UAC3_SELECTOR_UNIT << 16; /* virtual type */
c77e1ef1
JS
970 term->id = id;
971 term->name = 0; /* TODO: UAC3 Class-specific strings */
972
973 return 0;
974 }
0f292f02
JS
975 case UAC3_PROCESSING_UNIT: {
976 struct uac_processing_unit_descriptor *d = p1;
977
978 if (!d->bNrInPins)
979 return -EINVAL;
980
981 /* call recursively to retrieve the channel info */
982 err = check_input_term(state, d->baSourceID[0], term);
983 if (err < 0)
984 return err;
985
8b3a087f 986 term->type = UAC3_PROCESSING_UNIT << 16; /* virtual type */
0f292f02
JS
987 term->id = id;
988 term->name = 0; /* TODO: UAC3 Class-specific strings */
989
990 return 0;
991 }
9a2fe9b8
RB
992 default:
993 return -ENODEV;
994 }
1da177e4
LT
995 }
996 }
997 return -ENODEV;
998}
999
1da177e4
LT
1000/*
1001 * Feature Unit
1002 */
1003
1004/* feature unit control information */
1005struct usb_feature_control_info {
21e9b3e9 1006 int control;
1da177e4 1007 const char *name;
bc18e31c
JS
1008 int type; /* data type for uac1 */
1009 int type_uac2; /* data type for uac2 if different from uac1, else -1 */
1da177e4
LT
1010};
1011
1012static struct usb_feature_control_info audio_feature_info[] = {
21e9b3e9
AC
1013 { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
1014 { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
1015 { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
1016 { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
1017 { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
1018 { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
1019 { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
1020 { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
1021 { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
1022 { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
2e0281d1 1023 /* UAC2 specific */
21e9b3e9
AC
1024 { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
1025 { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
1026 { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
1da177e4
LT
1027};
1028
1da177e4 1029/* private_free callback */
eef90451 1030void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
1da177e4 1031{
4d572776
JJ
1032 kfree(kctl->private_data);
1033 kctl->private_data = NULL;
1da177e4
LT
1034}
1035
1da177e4
LT
1036/*
1037 * interface to ALSA control for feature/mixer units
1038 */
1039
dcaaf9f2
TI
1040/* volume control quirks */
1041static void volume_control_quirks(struct usb_mixer_elem_info *cval,
1042 struct snd_kcontrol *kctl)
1043{
3360b84b 1044 struct snd_usb_audio *chip = cval->head.mixer->chip;
0ba41d91 1045 switch (chip->usb_id) {
d50ed624 1046 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
e9a25e04 1047 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
d50ed624
EZ
1048 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
1049 cval->min = 0x0000;
1050 cval->max = 0xffff;
1051 cval->res = 0x00e6;
1052 break;
1053 }
1054 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1055 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
1056 cval->min = 0x00;
1057 cval->max = 0xff;
1058 break;
1059 }
1060 if (strstr(kctl->id.name, "Effect Return") != NULL) {
1061 cval->min = 0xb706;
1062 cval->max = 0xff7b;
1063 cval->res = 0x0073;
1064 break;
1065 }
1066 if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
1067 (strstr(kctl->id.name, "Effect Send") != NULL)) {
1068 cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
1069 cval->max = 0xfcfe;
1070 cval->res = 0x0073;
1071 }
1072 break;
1073
d34bf148
FH
1074 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1075 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1076 if (strcmp(kctl->id.name, "Effect Duration") == 0) {
0ba41d91
TI
1077 usb_audio_info(chip,
1078 "set quirk for FTU Effect Duration\n");
d34bf148
FH
1079 cval->min = 0x0000;
1080 cval->max = 0x7f00;
1081 cval->res = 0x0100;
1082 break;
1083 }
1084 if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
1085 strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
0ba41d91
TI
1086 usb_audio_info(chip,
1087 "set quirks for FTU Effect Feedback/Volume\n");
d34bf148
FH
1088 cval->min = 0x00;
1089 cval->max = 0x7f;
1090 break;
1091 }
1092 break;
1093
21493316
FC
1094 case USB_ID(0x0d8c, 0x0103):
1095 if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
1096 usb_audio_info(chip,
1097 "set volume quirk for CM102-A+/102S+\n");
1098 cval->min = -256;
1099 }
1100 break;
1101
dcaaf9f2
TI
1102 case USB_ID(0x0471, 0x0101):
1103 case USB_ID(0x0471, 0x0104):
1104 case USB_ID(0x0471, 0x0105):
1105 case USB_ID(0x0672, 0x1041):
1106 /* quirk for UDA1321/N101.
1107 * note that detection between firmware 2.1.1.7 (N101)
1108 * and later 2.1.1.21 is not very clear from datasheets.
1109 * I hope that the min value is -15360 for newer firmware --jk
1110 */
1111 if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
1112 cval->min == -15616) {
0ba41d91 1113 usb_audio_info(chip,
dcaaf9f2
TI
1114 "set volume quirk for UDA1321/N101 chip\n");
1115 cval->max = -256;
1116 }
1117 break;
1118
1119 case USB_ID(0x046d, 0x09a4):
1120 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
0ba41d91 1121 usb_audio_info(chip,
dcaaf9f2
TI
1122 "set volume quirk for QuickCam E3500\n");
1123 cval->min = 6080;
1124 cval->max = 8768;
1125 cval->res = 192;
1126 }
1127 break;
1128
e805ca8b 1129 case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
dcaaf9f2
TI
1130 case USB_ID(0x046d, 0x0808):
1131 case USB_ID(0x046d, 0x0809):
64559311 1132 case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
36691e1b 1133 case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
55c0008b 1134 case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
11e7064f 1135 case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
140d37de 1136 case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
1ef9f058 1137 case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
dcaaf9f2 1138 case USB_ID(0x046d, 0x0991):
82ffb6fc 1139 case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
dcaaf9f2
TI
1140 /* Most audio usb devices lie about volume resolution.
1141 * Most Logitech webcams have res = 384.
82ffb6fc 1142 * Probably there is some logitech magic behind this number --fishor
dcaaf9f2
TI
1143 */
1144 if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
0ba41d91 1145 usb_audio_info(chip,
dcaaf9f2
TI
1146 "set resolution quirk: cval->res = 384\n");
1147 cval->res = 384;
1148 }
1149 break;
dcaaf9f2
TI
1150 }
1151}
1152
1da177e4
LT
1153/*
1154 * retrieve the minimum and maximum values for the specified control
1155 */
dcaaf9f2
TI
1156static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
1157 int default_min, struct snd_kcontrol *kctl)
1da177e4
LT
1158{
1159 /* for failsafe */
1160 cval->min = default_min;
1161 cval->max = cval->min + 1;
1162 cval->res = 1;
c3a3e040 1163 cval->dBmin = cval->dBmax = 0;
1da177e4
LT
1164
1165 if (cval->val_type == USB_MIXER_BOOLEAN ||
1166 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1167 cval->initialized = 1;
1168 } else {
1169 int minchn = 0;
1170 if (cval->cmask) {
1171 int i;
1172 for (i = 0; i < MAX_CHANNELS; i++)
1173 if (cval->cmask & (1 << i)) {
1174 minchn = i + 1;
1175 break;
1176 }
1177 }
de48c7bc
DM
1178 if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
1179 get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
3360b84b 1180 usb_audio_err(cval->head.mixer->chip,
0ba41d91 1181 "%d:%d: cannot get min/max values for control %d (id %d)\n",
3360b84b
TI
1182 cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
1183 cval->control, cval->head.id);
1da177e4
LT
1184 return -EINVAL;
1185 }
6bc170e4
DM
1186 if (get_ctl_value(cval, UAC_GET_RES,
1187 (cval->control << 8) | minchn,
1188 &cval->res) < 0) {
1da177e4
LT
1189 cval->res = 1;
1190 } else {
1191 int last_valid_res = cval->res;
1192
1193 while (cval->res > 1) {
7b1eda22 1194 if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
6bc170e4
DM
1195 (cval->control << 8) | minchn,
1196 cval->res / 2) < 0)
1da177e4
LT
1197 break;
1198 cval->res /= 2;
1199 }
6bc170e4
DM
1200 if (get_ctl_value(cval, UAC_GET_RES,
1201 (cval->control << 8) | minchn, &cval->res) < 0)
1da177e4
LT
1202 cval->res = last_valid_res;
1203 }
1204 if (cval->res == 0)
1205 cval->res = 1;
14790f1c
TI
1206
1207 /* Additional checks for the proper resolution
1208 *
1209 * Some devices report smaller resolutions than actually
1210 * reacting. They don't return errors but simply clip
1211 * to the lower aligned value.
1212 */
1213 if (cval->min + cval->res < cval->max) {
1214 int last_valid_res = cval->res;
1215 int saved, test, check;
641b4879 1216 get_cur_mix_raw(cval, minchn, &saved);
14790f1c
TI
1217 for (;;) {
1218 test = saved;
1219 if (test < cval->max)
1220 test += cval->res;
1221 else
1222 test -= cval->res;
1223 if (test < cval->min || test > cval->max ||
eef90451 1224 snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
641b4879 1225 get_cur_mix_raw(cval, minchn, &check)) {
14790f1c
TI
1226 cval->res = last_valid_res;
1227 break;
1228 }
1229 if (test == check)
1230 break;
1231 cval->res *= 2;
1232 }
eef90451 1233 snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
14790f1c
TI
1234 }
1235
1da177e4
LT
1236 cval->initialized = 1;
1237 }
c3a3e040 1238
dcaaf9f2
TI
1239 if (kctl)
1240 volume_control_quirks(cval, kctl);
1241
c3a3e040
JK
1242 /* USB descriptions contain the dB scale in 1/256 dB unit
1243 * while ALSA TLV contains in 1/100 dB unit
1244 */
1245 cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
1246 cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
1247 if (cval->dBmin > cval->dBmax) {
1248 /* something is wrong; assume it's either from/to 0dB */
1249 if (cval->dBmin < 0)
1250 cval->dBmax = 0;
1251 else if (cval->dBmin > 0)
1252 cval->dBmin = 0;
1253 if (cval->dBmin > cval->dBmax) {
1254 /* totally crap, return an error */
1255 return -EINVAL;
1256 }
1257 }
1258
1da177e4
LT
1259 return 0;
1260}
1261
dcaaf9f2 1262#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
1da177e4
LT
1263
1264/* get a feature/mixer unit info */
6bc170e4
DM
1265static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
1266 struct snd_ctl_elem_info *uinfo)
1da177e4 1267{
86e07d34 1268 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
1269
1270 if (cval->val_type == USB_MIXER_BOOLEAN ||
1271 cval->val_type == USB_MIXER_INV_BOOLEAN)
1272 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1273 else
1274 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1275 uinfo->count = cval->channels;
1276 if (cval->val_type == USB_MIXER_BOOLEAN ||
1277 cval->val_type == USB_MIXER_INV_BOOLEAN) {
1278 uinfo->value.integer.min = 0;
1279 uinfo->value.integer.max = 1;
1280 } else {
9fcd0ab1 1281 if (!cval->initialized) {
dcaaf9f2 1282 get_min_max_with_quirks(cval, 0, kcontrol);
9fcd0ab1
TI
1283 if (cval->initialized && cval->dBmin >= cval->dBmax) {
1284 kcontrol->vd[0].access &=
1285 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1286 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
3360b84b 1287 snd_ctl_notify(cval->head.mixer->chip->card,
9fcd0ab1
TI
1288 SNDRV_CTL_EVENT_MASK_INFO,
1289 &kcontrol->id);
1290 }
1291 }
1da177e4 1292 uinfo->value.integer.min = 0;
14790f1c
TI
1293 uinfo->value.integer.max =
1294 (cval->max - cval->min + cval->res - 1) / cval->res;
1da177e4
LT
1295 }
1296 return 0;
1297}
1298
1299/* get the current value from feature/mixer unit */
6bc170e4
DM
1300static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
1301 struct snd_ctl_elem_value *ucontrol)
1da177e4 1302{
86e07d34 1303 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
1304 int c, cnt, val, err;
1305
641b4879 1306 ucontrol->value.integer.value[0] = cval->min;
1da177e4
LT
1307 if (cval->cmask) {
1308 cnt = 0;
1309 for (c = 0; c < MAX_CHANNELS; c++) {
641b4879
TI
1310 if (!(cval->cmask & (1 << c)))
1311 continue;
eef90451 1312 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
641b4879 1313 if (err < 0)
5aeee342 1314 return filter_error(cval, err);
641b4879
TI
1315 val = get_relative_value(cval, val);
1316 ucontrol->value.integer.value[cnt] = val;
1317 cnt++;
1da177e4 1318 }
641b4879 1319 return 0;
1da177e4
LT
1320 } else {
1321 /* master channel */
eef90451 1322 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
641b4879 1323 if (err < 0)
5aeee342 1324 return filter_error(cval, err);
1da177e4
LT
1325 val = get_relative_value(cval, val);
1326 ucontrol->value.integer.value[0] = val;
1327 }
1328 return 0;
1329}
1330
1331/* put the current value to feature/mixer unit */
6bc170e4
DM
1332static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
1333 struct snd_ctl_elem_value *ucontrol)
1da177e4 1334{
86e07d34 1335 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
1336 int c, cnt, val, oval, err;
1337 int changed = 0;
1338
1339 if (cval->cmask) {
1340 cnt = 0;
1341 for (c = 0; c < MAX_CHANNELS; c++) {
641b4879
TI
1342 if (!(cval->cmask & (1 << c)))
1343 continue;
eef90451 1344 err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
641b4879 1345 if (err < 0)
5aeee342 1346 return filter_error(cval, err);
641b4879
TI
1347 val = ucontrol->value.integer.value[cnt];
1348 val = get_abs_value(cval, val);
1349 if (oval != val) {
eef90451 1350 snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
641b4879 1351 changed = 1;
1da177e4 1352 }
641b4879 1353 cnt++;
1da177e4
LT
1354 }
1355 } else {
1356 /* master channel */
eef90451 1357 err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
1da177e4 1358 if (err < 0)
5aeee342 1359 return filter_error(cval, err);
1da177e4
LT
1360 val = ucontrol->value.integer.value[0];
1361 val = get_abs_value(cval, val);
1362 if (val != oval) {
eef90451 1363 snd_usb_set_cur_mix_value(cval, 0, 0, val);
1da177e4
LT
1364 changed = 1;
1365 }
1366 }
1367 return changed;
1368}
1369
568fa7e0
AC
1370/* get the boolean value from the master channel of a UAC control */
1371static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
1372 struct snd_ctl_elem_value *ucontrol)
5a222e84
AC
1373{
1374 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1375 int val, err;
1376
1377 err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
1378 if (err < 0)
1379 return filter_error(cval, err);
1380 val = (val != 0);
1381 ucontrol->value.integer.value[0] = val;
1382 return 0;
1383}
1384
1d38f5d8
JS
1385/* get the connectors status and report it as boolean type */
1386static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol,
1387 struct snd_ctl_elem_value *ucontrol)
1388{
1389 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1390 struct snd_usb_audio *chip = cval->head.mixer->chip;
1391 int idx = 0, validx, ret, val;
1392
1393 validx = cval->control << 8 | 0;
1394
1395 ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
1396 if (ret)
1397 goto error;
1398
1399 idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
1400 if (cval->head.mixer->protocol == UAC_VERSION_2) {
1401 struct uac2_connectors_ctl_blk uac2_conn;
1402
1403 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1404 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1405 validx, idx, &uac2_conn, sizeof(uac2_conn));
1406 val = !!uac2_conn.bNrChannels;
1407 } else { /* UAC_VERSION_3 */
1408 struct uac3_insertion_ctl_blk uac3_conn;
1409
1410 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
1411 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1412 validx, idx, &uac3_conn, sizeof(uac3_conn));
1413 val = !!uac3_conn.bmConInserted;
1414 }
1415
1416 snd_usb_unlock_shutdown(chip);
1417
1418 if (ret < 0) {
1419error:
1420 usb_audio_err(chip,
1421 "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
1422 UAC_GET_CUR, validx, idx, cval->val_type);
1423 return ret;
1424 }
1425
1426 ucontrol->value.integer.value[0] = val;
1427 return 0;
1428}
1429
86e07d34 1430static struct snd_kcontrol_new usb_feature_unit_ctl = {
1da177e4
LT
1431 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1432 .name = "", /* will be filled later manually */
1433 .info = mixer_ctl_feature_info,
1434 .get = mixer_ctl_feature_get,
1435 .put = mixer_ctl_feature_put,
1436};
1437
23caaf19 1438/* the read-only variant */
8fdaebbb 1439static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
23caaf19
DM
1440 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1441 .name = "", /* will be filled later manually */
1442 .info = mixer_ctl_feature_info,
1443 .get = mixer_ctl_feature_get,
1444 .put = NULL,
1445};
1446
568fa7e0
AC
1447/*
1448 * A control which shows the boolean value from reading a UAC control on
1449 * the master channel.
1450 */
1451static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
5a222e84
AC
1452 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1453 .name = "", /* will be filled later manually */
1454 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1455 .info = snd_ctl_boolean_mono_info,
568fa7e0 1456 .get = mixer_ctl_master_bool_get,
5a222e84
AC
1457 .put = NULL,
1458};
1459
1d38f5d8
JS
1460static const struct snd_kcontrol_new usb_connector_ctl_ro = {
1461 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1462 .name = "", /* will be filled later manually */
1463 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1464 .info = snd_ctl_boolean_mono_info,
1465 .get = mixer_ctl_connector_get,
1466 .put = NULL,
1467};
1468
6bc170e4
DM
1469/*
1470 * This symbol is exported in order to allow the mixer quirks to
1471 * hook up to the standard feature unit control mechanism
1472 */
9e38658f 1473struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
1da177e4
LT
1474
1475/*
1476 * build a feature control
1477 */
08d1e635
TI
1478static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
1479{
1480 return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
1481}
1482
6bc170e4
DM
1483/*
1484 * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
1485 * rename it to "Headphone". We determine if something is a headphone
1486 * similar to how udev determines form factor.
1487 */
9b4ef977
DH
1488static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
1489 struct snd_card *card)
1490{
1491 const char *names_to_check[] = {
1492 "Headset", "headset", "Headphone", "headphone", NULL};
1493 const char **s;
e0f17c75 1494 bool found = false;
9b4ef977
DH
1495
1496 if (strcmp("Speaker", kctl->id.name))
1497 return;
1498
1499 for (s = names_to_check; *s; s++)
1500 if (strstr(card->shortname, *s)) {
e0f17c75 1501 found = true;
9b4ef977
DH
1502 break;
1503 }
1504
1505 if (!found)
1506 return;
1507
1508 strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
1509}
1510
21e9b3e9
AC
1511static struct usb_feature_control_info *get_feature_control_info(int control)
1512{
1513 int i;
1514
1515 for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
1516 if (audio_feature_info[i].control == control)
1517 return &audio_feature_info[i];
1518 }
1519 return NULL;
1520}
1521
17156f23
RB
1522static void __build_feature_ctl(struct usb_mixer_interface *mixer,
1523 const struct usbmix_name_map *imap,
1524 unsigned int ctl_mask, int control,
1525 struct usb_audio_term *iterm,
1526 struct usb_audio_term *oterm,
1527 int unitid, int nameid, int readonly_mask)
1da177e4 1528{
bc18e31c 1529 struct usb_feature_control_info *ctl_info;
1da177e4
LT
1530 unsigned int len = 0;
1531 int mapped_name = 0;
86e07d34
TI
1532 struct snd_kcontrol *kctl;
1533 struct usb_mixer_elem_info *cval;
c3a3e040 1534 const struct usbmix_name_map *map;
80acefff 1535 unsigned int range;
1da177e4 1536
65f25da4 1537 if (control == UAC_FU_GRAPHIC_EQUALIZER) {
1da177e4
LT
1538 /* FIXME: not supported yet */
1539 return;
1540 }
1541
17156f23 1542 map = find_map(imap, unitid, control);
c3a3e040 1543 if (check_ignored_ctl(map))
1da177e4
LT
1544 return;
1545
561b220a 1546 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
a860d95f 1547 if (!cval)
1da177e4 1548 return;
17156f23 1549 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
1da177e4
LT
1550 cval->control = control;
1551 cval->cmask = ctl_mask;
21e9b3e9
AC
1552
1553 ctl_info = get_feature_control_info(control);
62376025
CIK
1554 if (!ctl_info) {
1555 kfree(cval);
21e9b3e9 1556 return;
62376025 1557 }
17156f23 1558 if (mixer->protocol == UAC_VERSION_1)
bc18e31c
JS
1559 cval->val_type = ctl_info->type;
1560 else /* UAC_VERSION_2 */
1561 cval->val_type = ctl_info->type_uac2 >= 0 ?
1562 ctl_info->type_uac2 : ctl_info->type;
1563
a6a33259 1564 if (ctl_mask == 0) {
1da177e4 1565 cval->channels = 1; /* master channel */
a6a33259
DM
1566 cval->master_readonly = readonly_mask;
1567 } else {
1da177e4
LT
1568 int i, c = 0;
1569 for (i = 0; i < 16; i++)
1570 if (ctl_mask & (1 << i))
1571 c++;
1572 cval->channels = c;
a6a33259 1573 cval->ch_readonly = readonly_mask;
1da177e4
LT
1574 }
1575
6bc170e4
DM
1576 /*
1577 * If all channels in the mask are marked read-only, make the control
eef90451 1578 * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
6bc170e4
DM
1579 * issue write commands to read-only channels.
1580 */
a6a33259 1581 if (cval->channels == readonly_mask)
23caaf19
DM
1582 kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
1583 else
1584 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
1585
6bc170e4 1586 if (!kctl) {
17156f23 1587 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
1da177e4
LT
1588 kfree(cval);
1589 return;
1590 }
eef90451 1591 kctl->private_free = snd_usb_mixer_elem_free;
1da177e4 1592
c3a3e040 1593 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
1da177e4 1594 mapped_name = len != 0;
6bc170e4 1595 if (!len && nameid)
17156f23 1596 len = snd_usb_copy_string_desc(mixer->chip, nameid,
c3a3e040 1597 kctl->id.name, sizeof(kctl->id.name));
1da177e4
LT
1598
1599 switch (control) {
65f25da4
DM
1600 case UAC_FU_MUTE:
1601 case UAC_FU_VOLUME:
6bc170e4
DM
1602 /*
1603 * determine the control name. the rule is:
1da177e4
LT
1604 * - if a name id is given in descriptor, use it.
1605 * - if the connected input can be determined, then use the name
1606 * of terminal type.
1607 * - if the connected output can be determined, use it.
1608 * - otherwise, anonymous name.
1609 */
6bc170e4 1610 if (!len) {
17156f23
RB
1611 if (iterm)
1612 len = get_term_name(mixer->chip, iterm,
1613 kctl->id.name,
1614 sizeof(kctl->id.name), 1);
1615 if (!len && oterm)
1616 len = get_term_name(mixer->chip, oterm,
6bc170e4
DM
1617 kctl->id.name,
1618 sizeof(kctl->id.name), 1);
1619 if (!len)
49fd46d2
DM
1620 snprintf(kctl->id.name, sizeof(kctl->id.name),
1621 "Feature %d", unitid);
1da177e4 1622 }
9b4ef977
DH
1623
1624 if (!mapped_name)
17156f23 1625 check_no_speaker_on_headset(kctl, mixer->chip->card);
9b4ef977 1626
6bc170e4
DM
1627 /*
1628 * determine the stream direction:
1da177e4
LT
1629 * if the connected output is USB stream, then it's likely a
1630 * capture stream. otherwise it should be playback (hopefully :)
1631 */
17156f23
RB
1632 if (!mapped_name && oterm && !(oterm->type >> 16)) {
1633 if ((oterm->type & 0xff00) == 0x0100)
49fd46d2 1634 append_ctl_name(kctl, " Capture");
6bc170e4 1635 else
49fd46d2 1636 append_ctl_name(kctl, " Playback");
1da177e4 1637 }
65f25da4 1638 append_ctl_name(kctl, control == UAC_FU_MUTE ?
08d1e635 1639 " Switch" : " Volume");
1da177e4 1640 break;
1da177e4 1641 default:
6bc170e4 1642 if (!len)
1da177e4
LT
1643 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
1644 sizeof(kctl->id.name));
1645 break;
1646 }
1647
e182534d
TI
1648 /* get min/max values */
1649 get_min_max_with_quirks(cval, 0, kctl);
1650
1651 if (control == UAC_FU_VOLUME) {
1652 check_mapped_dB(map, cval);
1653 if (cval->dBmin < cval->dBmax || !cval->initialized) {
1654 kctl->tlv.c = snd_usb_mixer_vol_tlv;
1655 kctl->vd[0].access |=
1656 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1657 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1658 }
1659 }
1660
17156f23 1661 snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
42e3121d 1662
80acefff 1663 range = (cval->max - cval->min) / cval->res;
6bc170e4
DM
1664 /*
1665 * Are there devices with volume range more than 255? I use a bit more
80acefff
AF
1666 * to be sure. 384 is a resolution magic number found on Logitech
1667 * devices. It will definitively catch all buggy Logitech devices.
1668 */
1669 if (range > 384) {
17156f23 1670 usb_audio_warn(mixer->chip,
82c1cf0a 1671 "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
6bc170e4 1672 range);
17156f23 1673 usb_audio_warn(mixer->chip,
82c1cf0a 1674 "[%d] FU [%s] ch = %d, val = %d/%d/%d",
3360b84b 1675 cval->head.id, kctl->id.name, cval->channels,
6bc170e4 1676 cval->min, cval->max, cval->res);
80acefff
AF
1677 }
1678
17156f23 1679 usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
3360b84b 1680 cval->head.id, kctl->id.name, cval->channels,
6bc170e4 1681 cval->min, cval->max, cval->res);
3360b84b 1682 snd_usb_mixer_add_control(&cval->head, kctl);
1da177e4
LT
1683}
1684
17156f23
RB
1685static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
1686 unsigned int ctl_mask, int control,
1687 struct usb_audio_term *iterm, int unitid,
1688 int readonly_mask)
1689{
1690 struct uac_feature_unit_descriptor *desc = raw_desc;
1691 int nameid = uac_feature_unit_iFeature(desc);
1692
1693 __build_feature_ctl(state->mixer, state->map, ctl_mask, control,
1694 iterm, &state->oterm, unitid, nameid, readonly_mask);
1695}
1696
1697static void build_feature_ctl_badd(struct usb_mixer_interface *mixer,
1698 unsigned int ctl_mask, int control, int unitid,
1699 const struct usbmix_name_map *badd_map)
1700{
1701 __build_feature_ctl(mixer, badd_map, ctl_mask, control,
1702 NULL, NULL, unitid, 0, 0);
1703}
1704
167e1fb1 1705static void get_connector_control_name(struct usb_mixer_interface *mixer,
5a222e84
AC
1706 struct usb_audio_term *term,
1707 bool is_input, char *name, int name_size)
1708{
167e1fb1 1709 int name_len = get_term_name(mixer->chip, term, name, name_size, 0);
5a222e84
AC
1710
1711 if (name_len == 0)
1712 strlcpy(name, "Unknown", name_size);
1713
1714 /*
1715 * sound/core/ctljack.c has a convention of naming jack controls
1716 * by ending in " Jack". Make it slightly more useful by
1717 * indicating Input or Output after the terminal name.
1718 */
1719 if (is_input)
1720 strlcat(name, " - Input Jack", name_size);
1721 else
1722 strlcat(name, " - Output Jack", name_size);
1723}
1724
1725/* Build a mixer control for a UAC connector control (jack-detect) */
167e1fb1 1726static void build_connector_control(struct usb_mixer_interface *mixer,
5a222e84
AC
1727 struct usb_audio_term *term, bool is_input)
1728{
1729 struct snd_kcontrol *kctl;
1730 struct usb_mixer_elem_info *cval;
1731
1732 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1733 if (!cval)
1734 return;
167e1fb1 1735 snd_usb_mixer_elem_init_std(&cval->head, mixer, term->id);
568fa7e0 1736 /*
1d38f5d8
JS
1737 * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the
1738 * number of channels connected.
1739 *
1740 * UAC3: The first byte specifies size of bitmap for the inserted controls. The
1741 * following byte(s) specifies which connectors are inserted.
1742 *
1743 * This boolean ctl will simply report if any channels are connected
1744 * or not.
568fa7e0 1745 */
167e1fb1 1746 if (mixer->protocol == UAC_VERSION_2)
1d38f5d8
JS
1747 cval->control = UAC2_TE_CONNECTOR;
1748 else /* UAC_VERSION_3 */
1749 cval->control = UAC3_TE_INSERTION;
1750
5a222e84
AC
1751 cval->val_type = USB_MIXER_BOOLEAN;
1752 cval->channels = 1; /* report true if any channel is connected */
1753 cval->min = 0;
1754 cval->max = 1;
1d38f5d8 1755 kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval);
5a222e84 1756 if (!kctl) {
167e1fb1 1757 usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
5a222e84
AC
1758 kfree(cval);
1759 return;
1760 }
167e1fb1 1761 get_connector_control_name(mixer, term, is_input, kctl->id.name,
5a222e84
AC
1762 sizeof(kctl->id.name));
1763 kctl->private_free = snd_usb_mixer_elem_free;
1764 snd_usb_mixer_add_control(&cval->head, kctl);
1765}
1766
cddaafb9
DM
1767static int parse_clock_source_unit(struct mixer_build *state, int unitid,
1768 void *_ftr)
1769{
1770 struct uac_clock_source_descriptor *hdr = _ftr;
1771 struct usb_mixer_elem_info *cval;
1772 struct snd_kcontrol *kctl;
1773 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1774 int ret;
1775
1776 if (state->mixer->protocol != UAC_VERSION_2)
1777 return -EINVAL;
1778
1779 if (hdr->bLength != sizeof(*hdr)) {
1780 usb_audio_dbg(state->chip,
1781 "Bogus clock source descriptor length of %d, ignoring.\n",
1782 hdr->bLength);
1783 return 0;
1784 }
1785
1786 /*
1787 * The only property of this unit we are interested in is the
1788 * clock source validity. If that isn't readable, just bail out.
1789 */
9a2fe9b8 1790 if (!uac_v2v3_control_is_readable(hdr->bmControls,
21e9b3e9 1791 UAC2_CS_CONTROL_CLOCK_VALID))
cddaafb9
DM
1792 return 0;
1793
1794 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
1795 if (!cval)
1796 return -ENOMEM;
1797
1798 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
1799
1800 cval->min = 0;
1801 cval->max = 1;
1802 cval->channels = 1;
1803 cval->val_type = USB_MIXER_BOOLEAN;
1804 cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
1805
568fa7e0
AC
1806 cval->master_readonly = 1;
1807 /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
1808 kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
cddaafb9
DM
1809
1810 if (!kctl) {
1811 kfree(cval);
1812 return -ENOMEM;
1813 }
1814
1815 kctl->private_free = snd_usb_mixer_elem_free;
eccfc1b8 1816 ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource,
cddaafb9
DM
1817 name, sizeof(name));
1818 if (ret > 0)
1819 snprintf(kctl->id.name, sizeof(kctl->id.name),
1820 "%s Validity", name);
1821 else
1822 snprintf(kctl->id.name, sizeof(kctl->id.name),
1823 "Clock Source %d Validity", hdr->bClockID);
1824
1825 return snd_usb_mixer_add_control(&cval->head, kctl);
1826}
1827
1da177e4
LT
1828/*
1829 * parse a feature unit
1830 *
25985edc 1831 * most of controls are defined here.
1da177e4 1832 */
6bc170e4
DM
1833static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
1834 void *_ftr)
1da177e4
LT
1835{
1836 int channels, i, j;
86e07d34 1837 struct usb_audio_term iterm;
36c346e1 1838 unsigned int master_bits;
1da177e4 1839 int err, csize;
23caaf19
DM
1840 struct uac_feature_unit_descriptor *hdr = _ftr;
1841 __u8 *bmaControls;
1842
1843 if (state->mixer->protocol == UAC_VERSION_1) {
d937cd67
TI
1844 if (hdr->bLength < 7) {
1845 usb_audio_err(state->chip,
1846 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1847 unitid);
1848 return -EINVAL;
1849 }
23caaf19 1850 csize = hdr->bControlSize;
3c02a6d9 1851 if (!csize) {
0ba41d91 1852 usb_audio_dbg(state->chip,
3c02a6d9 1853 "unit %u: invalid bControlSize == 0\n",
0ba41d91 1854 unitid);
60c961a9
NK
1855 return -EINVAL;
1856 }
23caaf19
DM
1857 channels = (hdr->bLength - 7) / csize - 1;
1858 bmaControls = hdr->bmaControls;
d56268fb 1859 if (hdr->bLength < 7 + csize) {
0ba41d91
TI
1860 usb_audio_err(state->chip,
1861 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1862 unitid);
d56268fb
CL
1863 return -EINVAL;
1864 }
9a2fe9b8 1865 } else if (state->mixer->protocol == UAC_VERSION_2) {
23caaf19 1866 struct uac2_feature_unit_descriptor *ftr = _ftr;
d937cd67
TI
1867 if (hdr->bLength < 6) {
1868 usb_audio_err(state->chip,
1869 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1870 unitid);
1871 return -EINVAL;
1872 }
23caaf19 1873 csize = 4;
e8d0fee7 1874 channels = (hdr->bLength - 6) / 4 - 1;
23caaf19 1875 bmaControls = ftr->bmaControls;
d56268fb 1876 if (hdr->bLength < 6 + csize) {
0ba41d91
TI
1877 usb_audio_err(state->chip,
1878 "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
1879 unitid);
d56268fb
CL
1880 return -EINVAL;
1881 }
9a2fe9b8
RB
1882 } else { /* UAC_VERSION_3 */
1883 struct uac3_feature_unit_descriptor *ftr = _ftr;
1884
1885 if (hdr->bLength < 7) {
1886 usb_audio_err(state->chip,
1887 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1888 unitid);
1889 return -EINVAL;
1890 }
1891 csize = 4;
1892 channels = (ftr->bLength - 7) / 4 - 1;
1893 bmaControls = ftr->bmaControls;
1894 if (hdr->bLength < 7 + csize) {
1895 usb_audio_err(state->chip,
1896 "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
1897 unitid);
1898 return -EINVAL;
1899 }
1da177e4
LT
1900 }
1901
1902 /* parse the source unit */
f25ecf8f
TI
1903 err = parse_audio_unit(state, hdr->bSourceID);
1904 if (err < 0)
1da177e4
LT
1905 return err;
1906
1907 /* determine the input source type and name */
4d7b86c9
DM
1908 err = check_input_term(state, hdr->bSourceID, &iterm);
1909 if (err < 0)
1910 return err;
1da177e4 1911
23caaf19 1912 master_bits = snd_usb_combine_bytes(bmaControls, csize);
0c3cee57
JK
1913 /* master configuration quirks */
1914 switch (state->chip->usb_id) {
1915 case USB_ID(0x08bb, 0x2702):
0ba41d91
TI
1916 usb_audio_info(state->chip,
1917 "usbmixer: master volume quirk for PCM2702 chip\n");
0c3cee57 1918 /* disable non-functional volume control */
65f25da4 1919 master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
0c3cee57 1920 break;
c1051439 1921 case USB_ID(0x1130, 0xf211):
0ba41d91
TI
1922 usb_audio_info(state->chip,
1923 "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
c1051439
DH
1924 /* disable non-functional volume control */
1925 channels = 0;
1926 break;
1927
0c3cee57 1928 }
23caaf19
DM
1929
1930 if (state->mixer->protocol == UAC_VERSION_1) {
1931 /* check all control types */
1932 for (i = 0; i < 10; i++) {
1933 unsigned int ch_bits = 0;
21e9b3e9
AC
1934 int control = audio_feature_info[i].control;
1935
23caaf19 1936 for (j = 0; j < channels; j++) {
6bc170e4
DM
1937 unsigned int mask;
1938
1939 mask = snd_usb_combine_bytes(bmaControls +
1940 csize * (j+1), csize);
23caaf19
DM
1941 if (mask & (1 << i))
1942 ch_bits |= (1 << j);
1943 }
1944 /* audio class v1 controls are never read-only */
6bc170e4
DM
1945
1946 /*
1947 * The first channel must be set
1948 * (for ease of programming).
1949 */
1950 if (ch_bits & 1)
21e9b3e9 1951 build_feature_ctl(state, _ftr, ch_bits, control,
6bc170e4 1952 &iterm, unitid, 0);
23caaf19 1953 if (master_bits & (1 << i))
21e9b3e9
AC
1954 build_feature_ctl(state, _ftr, 0, control,
1955 &iterm, unitid, 0);
23caaf19 1956 }
9a2fe9b8 1957 } else { /* UAC_VERSION_2/3 */
d09c06c6 1958 for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
23caaf19
DM
1959 unsigned int ch_bits = 0;
1960 unsigned int ch_read_only = 0;
21e9b3e9 1961 int control = audio_feature_info[i].control;
23caaf19
DM
1962
1963 for (j = 0; j < channels; j++) {
6bc170e4
DM
1964 unsigned int mask;
1965
1966 mask = snd_usb_combine_bytes(bmaControls +
1967 csize * (j+1), csize);
21e9b3e9 1968 if (uac_v2v3_control_is_readable(mask, control)) {
23caaf19 1969 ch_bits |= (1 << j);
21e9b3e9 1970 if (!uac_v2v3_control_is_writeable(mask, control))
23caaf19
DM
1971 ch_read_only |= (1 << j);
1972 }
1973 }
1974
6bc170e4
DM
1975 /*
1976 * NOTE: build_feature_ctl() will mark the control
1977 * read-only if all channels are marked read-only in
1978 * the descriptors. Otherwise, the control will be
1979 * reported as writeable, but the driver will not
1980 * actually issue a write command for read-only
1981 * channels.
1982 */
1983
1984 /*
1985 * The first channel must be set
1986 * (for ease of programming).
1987 */
1988 if (ch_bits & 1)
21e9b3e9 1989 build_feature_ctl(state, _ftr, ch_bits, control,
6bc170e4 1990 &iterm, unitid, ch_read_only);
21e9b3e9 1991 if (uac_v2v3_control_is_readable(master_bits, control))
2de841ef
TI
1992 build_feature_ctl(state, _ftr, 0, control,
1993 &iterm, unitid,
21e9b3e9
AC
1994 !uac_v2v3_control_is_writeable(master_bits,
1995 control));
1da177e4 1996 }
1da177e4
LT
1997 }
1998
1999 return 0;
2000}
2001
1da177e4
LT
2002/*
2003 * Mixer Unit
2004 */
2005
2006/*
2007 * build a mixer unit control
2008 *
2009 * the callbacks are identical with feature unit.
2010 * input channel number (zero based) is given in control field instead.
2011 */
99fc8645
DM
2012static void build_mixer_unit_ctl(struct mixer_build *state,
2013 struct uac_mixer_unit_descriptor *desc,
6cfd839a
JS
2014 int in_pin, int in_ch, int num_outs,
2015 int unitid, struct usb_audio_term *iterm)
1da177e4 2016{
86e07d34 2017 struct usb_mixer_elem_info *cval;
1da177e4 2018 unsigned int i, len;
86e07d34 2019 struct snd_kcontrol *kctl;
c3a3e040 2020 const struct usbmix_name_map *map;
1da177e4 2021
17156f23 2022 map = find_map(state->map, unitid, 0);
c3a3e040 2023 if (check_ignored_ctl(map))
1da177e4
LT
2024 return;
2025
561b220a 2026 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
6bc170e4 2027 if (!cval)
1da177e4
LT
2028 return;
2029
3360b84b 2030 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
1da177e4
LT
2031 cval->control = in_ch + 1; /* based on 1 */
2032 cval->val_type = USB_MIXER_S16;
2033 for (i = 0; i < num_outs; i++) {
6bc170e4
DM
2034 __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
2035
2036 if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
1da177e4
LT
2037 cval->cmask |= (1 << i);
2038 cval->channels++;
2039 }
2040 }
2041
2042 /* get min/max values */
2043 get_min_max(cval, 0);
2044
2045 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
6bc170e4 2046 if (!kctl) {
0ba41d91 2047 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
1da177e4
LT
2048 kfree(cval);
2049 return;
2050 }
eef90451 2051 kctl->private_free = snd_usb_mixer_elem_free;
1da177e4 2052
c3a3e040 2053 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
6bc170e4 2054 if (!len)
eccfc1b8 2055 len = get_term_name(state->chip, iterm, kctl->id.name,
6bc170e4
DM
2056 sizeof(kctl->id.name), 0);
2057 if (!len)
1da177e4 2058 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
08d1e635 2059 append_ctl_name(kctl, " Volume");
1da177e4 2060
0ba41d91 2061 usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
3360b84b
TI
2062 cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
2063 snd_usb_mixer_add_control(&cval->head, kctl);
1da177e4
LT
2064}
2065
5a222e84
AC
2066static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
2067 void *raw_desc)
2068{
2069 struct usb_audio_term iterm;
1d38f5d8 2070 unsigned int control, bmctls, term_id;
5a222e84 2071
5a222e84 2072 if (state->mixer->protocol == UAC_VERSION_2) {
1d38f5d8 2073 struct uac2_input_terminal_descriptor *d_v2 = raw_desc;
3e96d728
TI
2074 if (d_v2->bLength < sizeof(*d_v2))
2075 return -EINVAL;
1d38f5d8
JS
2076 control = UAC2_TE_CONNECTOR;
2077 term_id = d_v2->bTerminalID;
2078 bmctls = le16_to_cpu(d_v2->bmControls);
2079 } else if (state->mixer->protocol == UAC_VERSION_3) {
2080 struct uac3_input_terminal_descriptor *d_v3 = raw_desc;
3e96d728
TI
2081 if (d_v3->bLength < sizeof(*d_v3))
2082 return -EINVAL;
1d38f5d8
JS
2083 control = UAC3_TE_INSERTION;
2084 term_id = d_v3->bTerminalID;
2085 bmctls = le32_to_cpu(d_v3->bmControls);
2086 } else {
2087 return 0; /* UAC1. No Insertion control */
5a222e84 2088 }
1d38f5d8
JS
2089
2090 check_input_term(state, term_id, &iterm);
2091
2092 /* Check for jack detection. */
2093 if (uac_v2v3_control_is_readable(bmctls, control))
167e1fb1 2094 build_connector_control(state->mixer, &iterm, true);
1d38f5d8 2095
5a222e84
AC
2096 return 0;
2097}
2098
1da177e4
LT
2099/*
2100 * parse a mixer unit
2101 */
6bc170e4
DM
2102static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
2103 void *raw_desc)
1da177e4 2104{
99fc8645 2105 struct uac_mixer_unit_descriptor *desc = raw_desc;
86e07d34 2106 struct usb_audio_term iterm;
1da177e4
LT
2107 int input_pins, num_ins, num_outs;
2108 int pin, ich, err;
2109
6cfd839a
JS
2110 err = uac_mixer_unit_get_channels(state, desc);
2111 if (err < 0) {
6bc170e4
DM
2112 usb_audio_err(state->chip,
2113 "invalid MIXER UNIT descriptor %d\n",
2114 unitid);
6cfd839a 2115 return err;
1da177e4 2116 }
1da177e4 2117
6cfd839a
JS
2118 num_outs = err;
2119 input_pins = desc->bNrInPins;
2120
1da177e4
LT
2121 num_ins = 0;
2122 ich = 0;
2123 for (pin = 0; pin < input_pins; pin++) {
99fc8645 2124 err = parse_audio_unit(state, desc->baSourceID[pin]);
1da177e4 2125 if (err < 0)
284a8dd6 2126 continue;
ea114fc2 2127 /* no bmControls field (e.g. Maya44) -> ignore */
0bfe5e43 2128 if (!num_outs)
ea114fc2 2129 continue;
99fc8645 2130 err = check_input_term(state, desc->baSourceID[pin], &iterm);
1da177e4
LT
2131 if (err < 0)
2132 return err;
2133 num_ins += iterm.channels;
6bc170e4 2134 for (; ich < num_ins; ich++) {
1da177e4
LT
2135 int och, ich_has_controls = 0;
2136
6bc170e4
DM
2137 for (och = 0; och < num_outs; och++) {
2138 __u8 *c = uac_mixer_unit_bmControls(desc,
2139 state->mixer->protocol);
2140
2141 if (check_matrix_bitmap(c, ich, och, num_outs)) {
1da177e4
LT
2142 ich_has_controls = 1;
2143 break;
2144 }
2145 }
2146 if (ich_has_controls)
6cfd839a 2147 build_mixer_unit_ctl(state, desc, pin, ich, num_outs,
1da177e4
LT
2148 unitid, &iterm);
2149 }
2150 }
2151 return 0;
2152}
2153
1da177e4
LT
2154/*
2155 * Processing Unit / Extension Unit
2156 */
2157
2158/* get callback for processing/extension unit */
6bc170e4
DM
2159static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
2160 struct snd_ctl_elem_value *ucontrol)
1da177e4 2161{
86e07d34 2162 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
2163 int err, val;
2164
2165 err = get_cur_ctl_value(cval, cval->control << 8, &val);
5aeee342 2166 if (err < 0) {
1da177e4 2167 ucontrol->value.integer.value[0] = cval->min;
5aeee342 2168 return filter_error(cval, err);
1da177e4 2169 }
1da177e4
LT
2170 val = get_relative_value(cval, val);
2171 ucontrol->value.integer.value[0] = val;
2172 return 0;
2173}
2174
2175/* put callback for processing/extension unit */
6bc170e4
DM
2176static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
2177 struct snd_ctl_elem_value *ucontrol)
1da177e4 2178{
86e07d34 2179 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
2180 int val, oval, err;
2181
2182 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
5aeee342
TI
2183 if (err < 0)
2184 return filter_error(cval, err);
1da177e4
LT
2185 val = ucontrol->value.integer.value[0];
2186 val = get_abs_value(cval, val);
2187 if (val != oval) {
2188 set_cur_ctl_value(cval, cval->control << 8, val);
2189 return 1;
2190 }
2191 return 0;
2192}
2193
2194/* alsa control interface for processing/extension unit */
8fdaebbb 2195static const struct snd_kcontrol_new mixer_procunit_ctl = {
1da177e4
LT
2196 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2197 .name = "", /* will be filled later */
2198 .info = mixer_ctl_feature_info,
2199 .get = mixer_ctl_procunit_get,
2200 .put = mixer_ctl_procunit_put,
2201};
2202
1da177e4
LT
2203/*
2204 * predefined data for processing units
2205 */
2206struct procunit_value_info {
2207 int control;
2208 char *suffix;
2209 int val_type;
2210 int min_value;
2211};
2212
2213struct procunit_info {
2214 int type;
2215 char *name;
2216 struct procunit_value_info *values;
2217};
2218
0f292f02
JS
2219static struct procunit_value_info undefined_proc_info[] = {
2220 { 0x00, "Control Undefined", 0 },
2221 { 0 }
2222};
2223
1da177e4 2224static struct procunit_value_info updown_proc_info[] = {
65f25da4
DM
2225 { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2226 { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
1da177e4
LT
2227 { 0 }
2228};
2229static struct procunit_value_info prologic_proc_info[] = {
65f25da4
DM
2230 { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2231 { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
1da177e4
LT
2232 { 0 }
2233};
2234static struct procunit_value_info threed_enh_proc_info[] = {
65f25da4
DM
2235 { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2236 { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
1da177e4
LT
2237 { 0 }
2238};
2239static struct procunit_value_info reverb_proc_info[] = {
65f25da4
DM
2240 { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2241 { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
2242 { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
2243 { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
1da177e4
LT
2244 { 0 }
2245};
2246static struct procunit_value_info chorus_proc_info[] = {
65f25da4
DM
2247 { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2248 { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
2249 { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
2250 { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1da177e4
LT
2251 { 0 }
2252};
2253static struct procunit_value_info dcr_proc_info[] = {
65f25da4
DM
2254 { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
2255 { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
2256 { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
2257 { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
2258 { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
2259 { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
1da177e4
LT
2260 { 0 }
2261};
2262
2263static struct procunit_info procunits[] = {
65f25da4
DM
2264 { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
2265 { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
2266 { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
2267 { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
2268 { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
2269 { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
1da177e4
LT
2270 { 0 },
2271};
0f292f02
JS
2272
2273static struct procunit_value_info uac3_updown_proc_info[] = {
2274 { UAC3_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
2275 { 0 }
2276};
2277static struct procunit_value_info uac3_stereo_ext_proc_info[] = {
2278 { UAC3_EXT_WIDTH_CONTROL, "Width Control", USB_MIXER_U8 },
2279 { 0 }
2280};
2281
2282static struct procunit_info uac3_procunits[] = {
2283 { UAC3_PROCESS_UP_DOWNMIX, "Up Down", uac3_updown_proc_info },
2284 { UAC3_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", uac3_stereo_ext_proc_info },
2285 { UAC3_PROCESS_MULTI_FUNCTION, "Multi-Function", undefined_proc_info },
2286 { 0 },
2287};
2288
7d2b451e
SK
2289/*
2290 * predefined data for extension units
2291 */
2292static struct procunit_value_info clock_rate_xu_info[] = {
e213e9cf
DM
2293 { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
2294 { 0 }
7d2b451e
SK
2295};
2296static struct procunit_value_info clock_source_xu_info[] = {
2297 { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
2298 { 0 }
2299};
2300static struct procunit_value_info spdif_format_xu_info[] = {
2301 { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
2302 { 0 }
2303};
2304static struct procunit_value_info soft_limit_xu_info[] = {
2305 { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
2306 { 0 }
2307};
2308static struct procunit_info extunits[] = {
2309 { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
2310 { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
2311 { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
2312 { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
2313 { 0 }
2314};
6bc170e4 2315
1da177e4
LT
2316/*
2317 * build a processing/extension unit
2318 */
6bc170e4
DM
2319static int build_audio_procunit(struct mixer_build *state, int unitid,
2320 void *raw_desc, struct procunit_info *list,
2321 char *name)
1da177e4 2322{
99fc8645 2323 struct uac_processing_unit_descriptor *desc = raw_desc;
f4351a19 2324 int num_ins;
86e07d34
TI
2325 struct usb_mixer_elem_info *cval;
2326 struct snd_kcontrol *kctl;
1da177e4
LT
2327 int i, err, nameid, type, len;
2328 struct procunit_info *info;
2329 struct procunit_value_info *valinfo;
c3a3e040 2330 const struct usbmix_name_map *map;
1da177e4
LT
2331 static struct procunit_value_info default_value_info[] = {
2332 { 0x01, "Switch", USB_MIXER_BOOLEAN },
2333 { 0 }
2334 };
2335 static struct procunit_info default_info = {
2336 0, NULL, default_value_info
2337 };
2338
f4351a19
TI
2339 if (desc->bLength < 13) {
2340 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
2341 return -EINVAL;
2342 }
2343
2344 num_ins = desc->bNrInPins;
2345 if (desc->bLength < 13 + num_ins ||
23caaf19 2346 desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
0ba41d91 2347 usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
1da177e4
LT
2348 return -EINVAL;
2349 }
2350
2351 for (i = 0; i < num_ins; i++) {
f25ecf8f
TI
2352 err = parse_audio_unit(state, desc->baSourceID[i]);
2353 if (err < 0)
1da177e4
LT
2354 return err;
2355 }
2356
99fc8645 2357 type = le16_to_cpu(desc->wProcessType);
1da177e4
LT
2358 for (info = list; info && info->type; info++)
2359 if (info->type == type)
2360 break;
6bc170e4 2361 if (!info || !info->type)
1da177e4
LT
2362 info = &default_info;
2363
2364 for (valinfo = info->values; valinfo->control; valinfo++) {
23caaf19 2365 __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
99fc8645 2366
4e887af3
JS
2367 if (state->mixer->protocol == UAC_VERSION_1) {
2368 if (!(controls[valinfo->control / 8] &
2369 (1 << ((valinfo->control % 8) - 1))))
2370 continue;
2371 } else { /* UAC_VERSION_2/3 */
2372 if (!uac_v2v3_control_is_readable(controls[valinfo->control / 8],
2373 valinfo->control))
2374 continue;
2375 }
2376
17156f23 2377 map = find_map(state->map, unitid, valinfo->control);
c3a3e040 2378 if (check_ignored_ctl(map))
1da177e4 2379 continue;
561b220a 2380 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
a860d95f 2381 if (!cval)
1da177e4 2382 return -ENOMEM;
3360b84b 2383 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
1da177e4
LT
2384 cval->control = valinfo->control;
2385 cval->val_type = valinfo->val_type;
2386 cval->channels = 1;
2387
4e887af3
JS
2388 if (state->mixer->protocol > UAC_VERSION_1 &&
2389 !uac_v2v3_control_is_writeable(controls[valinfo->control / 8],
2390 valinfo->control))
2391 cval->master_readonly = 1;
2392
1da177e4 2393 /* get min/max values */
55b8cb46
JS
2394 switch (type) {
2395 case UAC_PROCESS_UP_DOWNMIX: {
2396 bool mode_sel = false;
2397
2398 switch (state->mixer->protocol) {
2399 case UAC_VERSION_1:
2400 case UAC_VERSION_2:
2401 default:
2402 if (cval->control == UAC_UD_MODE_SELECT)
2403 mode_sel = true;
2404 break;
2405 case UAC_VERSION_3:
2406 if (cval->control == UAC3_UD_MODE_SELECT)
2407 mode_sel = true;
2408 break;
2409 }
2410
2411 if (mode_sel) {
2412 __u8 *control_spec = uac_processing_unit_specific(desc,
2413 state->mixer->protocol);
2414 cval->min = 1;
2415 cval->max = control_spec[0];
7d2b451e
SK
2416 cval->res = 1;
2417 cval->initialized = 1;
55b8cb46
JS
2418 break;
2419 }
2420
2421 get_min_max(cval, valinfo->min_value);
2422 break;
2423 }
2424 case USB_XU_CLOCK_RATE:
2425 /*
2426 * E-Mu USB 0404/0202/TrackerPre/0204
2427 * samplerate control quirk
2428 */
2429 cval->min = 0;
2430 cval->max = 5;
2431 cval->res = 1;
2432 cval->initialized = 1;
2433 break;
2434 default:
2435 get_min_max(cval, valinfo->min_value);
2436 break;
7d2b451e 2437 }
1da177e4
LT
2438
2439 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
6bc170e4 2440 if (!kctl) {
1da177e4
LT
2441 kfree(cval);
2442 return -ENOMEM;
2443 }
eef90451 2444 kctl->private_free = snd_usb_mixer_elem_free;
1da177e4 2445
6bc170e4 2446 if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
c3a3e040 2447 /* nothing */ ;
6bc170e4 2448 } else if (info->name) {
1da177e4 2449 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
6bc170e4 2450 } else {
23caaf19 2451 nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
1da177e4
LT
2452 len = 0;
2453 if (nameid)
eccfc1b8
RB
2454 len = snd_usb_copy_string_desc(state->chip,
2455 nameid,
6bc170e4
DM
2456 kctl->id.name,
2457 sizeof(kctl->id.name));
2458 if (!len)
1da177e4
LT
2459 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
2460 }
08d1e635
TI
2461 append_ctl_name(kctl, " ");
2462 append_ctl_name(kctl, valinfo->suffix);
1da177e4 2463
0ba41d91 2464 usb_audio_dbg(state->chip,
6bc170e4 2465 "[%d] PU [%s] ch = %d, val = %d/%d\n",
3360b84b 2466 cval->head.id, kctl->id.name, cval->channels,
6bc170e4
DM
2467 cval->min, cval->max);
2468
3360b84b 2469 err = snd_usb_mixer_add_control(&cval->head, kctl);
6bc170e4 2470 if (err < 0)
1da177e4
LT
2471 return err;
2472 }
2473 return 0;
2474}
2475
6bc170e4
DM
2476static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
2477 void *raw_desc)
1da177e4 2478{
0f292f02
JS
2479 switch (state->mixer->protocol) {
2480 case UAC_VERSION_1:
2481 case UAC_VERSION_2:
2482 default:
2483 return build_audio_procunit(state, unitid, raw_desc,
2484 procunits, "Processing Unit");
2485 case UAC_VERSION_3:
2486 return build_audio_procunit(state, unitid, raw_desc,
2487 uac3_procunits, "Processing Unit");
2488 }
1da177e4
LT
2489}
2490
6bc170e4
DM
2491static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
2492 void *raw_desc)
1da177e4 2493{
6bc170e4
DM
2494 /*
2495 * Note that we parse extension units with processing unit descriptors.
2496 * That's ok as the layout is the same.
2497 */
2498 return build_audio_procunit(state, unitid, raw_desc,
2499 extunits, "Extension Unit");
1da177e4
LT
2500}
2501
1da177e4
LT
2502/*
2503 * Selector Unit
2504 */
2505
6bc170e4
DM
2506/*
2507 * info callback for selector unit
1da177e4
LT
2508 * use an enumerator type for routing
2509 */
6bc170e4
DM
2510static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
2511 struct snd_ctl_elem_info *uinfo)
1da177e4 2512{
86e07d34 2513 struct usb_mixer_elem_info *cval = kcontrol->private_data;
2a1803a7 2514 const char **itemlist = (const char **)kcontrol->private_value;
1da177e4 2515
5e246b85
TI
2516 if (snd_BUG_ON(!itemlist))
2517 return -EINVAL;
2a1803a7 2518 return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
1da177e4
LT
2519}
2520
2521/* get callback for selector unit */
6bc170e4
DM
2522static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
1da177e4 2524{
86e07d34 2525 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
2526 int val, err;
2527
09414207 2528 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1da177e4 2529 if (err < 0) {
5aeee342
TI
2530 ucontrol->value.enumerated.item[0] = 0;
2531 return filter_error(cval, err);
1da177e4
LT
2532 }
2533 val = get_relative_value(cval, val);
2534 ucontrol->value.enumerated.item[0] = val;
2535 return 0;
2536}
2537
2538/* put callback for selector unit */
6bc170e4
DM
2539static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_value *ucontrol)
1da177e4 2541{
86e07d34 2542 struct usb_mixer_elem_info *cval = kcontrol->private_data;
1da177e4
LT
2543 int val, oval, err;
2544
09414207 2545 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
5aeee342
TI
2546 if (err < 0)
2547 return filter_error(cval, err);
1da177e4
LT
2548 val = ucontrol->value.enumerated.item[0];
2549 val = get_abs_value(cval, val);
2550 if (val != oval) {
09414207 2551 set_cur_ctl_value(cval, cval->control << 8, val);
1da177e4
LT
2552 return 1;
2553 }
2554 return 0;
2555}
2556
2557/* alsa control interface for selector unit */
8fdaebbb 2558static const struct snd_kcontrol_new mixer_selectunit_ctl = {
1da177e4
LT
2559 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2560 .name = "", /* will be filled later */
2561 .info = mixer_ctl_selector_info,
2562 .get = mixer_ctl_selector_get,
2563 .put = mixer_ctl_selector_put,
2564};
2565
6bc170e4
DM
2566/*
2567 * private free callback.
1da177e4
LT
2568 * free both private_data and private_value
2569 */
86e07d34 2570static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
1da177e4
LT
2571{
2572 int i, num_ins = 0;
2573
2574 if (kctl->private_data) {
86e07d34 2575 struct usb_mixer_elem_info *cval = kctl->private_data;
1da177e4
LT
2576 num_ins = cval->max;
2577 kfree(cval);
2578 kctl->private_data = NULL;
2579 }
2580 if (kctl->private_value) {
2581 char **itemlist = (char **)kctl->private_value;
2582 for (i = 0; i < num_ins; i++)
2583 kfree(itemlist[i]);
2584 kfree(itemlist);
2585 kctl->private_value = 0;
2586 }
2587}
2588
2589/*
2590 * parse a selector unit
2591 */
6bc170e4
DM
2592static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
2593 void *raw_desc)
1da177e4 2594{
99fc8645 2595 struct uac_selector_unit_descriptor *desc = raw_desc;
1da177e4
LT
2596 unsigned int i, nameid, len;
2597 int err;
86e07d34
TI
2598 struct usb_mixer_elem_info *cval;
2599 struct snd_kcontrol *kctl;
c3a3e040 2600 const struct usbmix_name_map *map;
1da177e4
LT
2601 char **namelist;
2602
f658f17b
TI
2603 if (desc->bLength < 5 || !desc->bNrInPins ||
2604 desc->bLength < 5 + desc->bNrInPins) {
0ba41d91
TI
2605 usb_audio_err(state->chip,
2606 "invalid SELECTOR UNIT descriptor %d\n", unitid);
1da177e4
LT
2607 return -EINVAL;
2608 }
2609
99fc8645 2610 for (i = 0; i < desc->bNrInPins; i++) {
f25ecf8f
TI
2611 err = parse_audio_unit(state, desc->baSourceID[i]);
2612 if (err < 0)
1da177e4
LT
2613 return err;
2614 }
2615
99fc8645 2616 if (desc->bNrInPins == 1) /* only one ? nonsense! */
1da177e4
LT
2617 return 0;
2618
17156f23 2619 map = find_map(state->map, unitid, 0);
c3a3e040 2620 if (check_ignored_ctl(map))
1da177e4
LT
2621 return 0;
2622
561b220a 2623 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
a860d95f 2624 if (!cval)
1da177e4 2625 return -ENOMEM;
3360b84b 2626 snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
1da177e4
LT
2627 cval->val_type = USB_MIXER_U8;
2628 cval->channels = 1;
2629 cval->min = 1;
99fc8645 2630 cval->max = desc->bNrInPins;
1da177e4
LT
2631 cval->res = 1;
2632 cval->initialized = 1;
2633
c77e1ef1
JS
2634 switch (state->mixer->protocol) {
2635 case UAC_VERSION_1:
2636 default:
09414207 2637 cval->control = 0;
c77e1ef1
JS
2638 break;
2639 case UAC_VERSION_2:
2640 case UAC_VERSION_3:
2641 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2642 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
2643 cval->control = UAC2_CX_CLOCK_SELECTOR;
2644 else /* UAC2/3_SELECTOR_UNIT */
2645 cval->control = UAC2_SU_SELECTOR;
2646 break;
2647 }
09414207 2648
6da2ec56 2649 namelist = kmalloc_array(desc->bNrInPins, sizeof(char *), GFP_KERNEL);
6bc170e4 2650 if (!namelist) {
1da177e4
LT
2651 kfree(cval);
2652 return -ENOMEM;
2653 }
2654#define MAX_ITEM_NAME_LEN 64
99fc8645 2655 for (i = 0; i < desc->bNrInPins; i++) {
86e07d34 2656 struct usb_audio_term iterm;
1da177e4
LT
2657 len = 0;
2658 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
6bc170e4 2659 if (!namelist[i]) {
7fbe3ca5 2660 while (i--)
1da177e4
LT
2661 kfree(namelist[i]);
2662 kfree(namelist);
2663 kfree(cval);
2664 return -ENOMEM;
2665 }
8e062ec7
CL
2666 len = check_mapped_selector_name(state, unitid, i, namelist[i],
2667 MAX_ITEM_NAME_LEN);
99fc8645 2668 if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
eccfc1b8
RB
2669 len = get_term_name(state->chip, &iterm, namelist[i],
2670 MAX_ITEM_NAME_LEN, 0);
1da177e4 2671 if (! len)
af831eef 2672 sprintf(namelist[i], "Input %u", i);
1da177e4
LT
2673 }
2674
2675 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
2676 if (! kctl) {
0ba41d91 2677 usb_audio_err(state->chip, "cannot malloc kcontrol\n");
cb517359
WW
2678 for (i = 0; i < desc->bNrInPins; i++)
2679 kfree(namelist[i]);
878b4789 2680 kfree(namelist);
1da177e4
LT
2681 kfree(cval);
2682 return -ENOMEM;
2683 }
2684 kctl->private_value = (unsigned long)namelist;
2685 kctl->private_free = usb_mixer_selector_elem_free;
2686
5a15f289 2687 /* check the static mapping table at first */
c3a3e040 2688 len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
89b89d12 2689 if (!len) {
5a15f289 2690 /* no mapping ? */
c77e1ef1
JS
2691 switch (state->mixer->protocol) {
2692 case UAC_VERSION_1:
2693 case UAC_VERSION_2:
2694 default:
5a15f289 2695 /* if iSelector is given, use it */
c77e1ef1
JS
2696 nameid = uac_selector_unit_iSelector(desc);
2697 if (nameid)
2698 len = snd_usb_copy_string_desc(state->chip,
2699 nameid, kctl->id.name,
2700 sizeof(kctl->id.name));
2701 break;
2702 case UAC_VERSION_3:
2703 /* TODO: Class-Specific strings not yet supported */
2704 break;
2705 }
2706
5a15f289
TI
2707 /* ... or pick up the terminal name at next */
2708 if (!len)
eccfc1b8 2709 len = get_term_name(state->chip, &state->oterm,
1da177e4 2710 kctl->id.name, sizeof(kctl->id.name), 0);
5a15f289 2711 /* ... or use the fixed string "USB" as the last resort */
6bc170e4 2712 if (!len)
1da177e4
LT
2713 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
2714
5a15f289 2715 /* and add the proper suffix */
c77e1ef1
JS
2716 if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR ||
2717 desc->bDescriptorSubtype == UAC3_CLOCK_SELECTOR)
09414207
DM
2718 append_ctl_name(kctl, " Clock Source");
2719 else if ((state->oterm.type & 0xff00) == 0x0100)
08d1e635 2720 append_ctl_name(kctl, " Capture Source");
1da177e4 2721 else
08d1e635 2722 append_ctl_name(kctl, " Playback Source");
1da177e4
LT
2723 }
2724
0ba41d91 2725 usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
3360b84b
TI
2726 cval->head.id, kctl->id.name, desc->bNrInPins);
2727 return snd_usb_mixer_add_control(&cval->head, kctl);
1da177e4
LT
2728}
2729
1da177e4
LT
2730/*
2731 * parse an audio unit recursively
2732 */
2733
86e07d34 2734static int parse_audio_unit(struct mixer_build *state, int unitid)
1da177e4
LT
2735{
2736 unsigned char *p1;
9a2fe9b8 2737 int protocol = state->mixer->protocol;
1da177e4
LT
2738
2739 if (test_and_set_bit(unitid, state->unitbitmap))
2740 return 0; /* the unit already visited */
2741
2742 p1 = find_audio_control_unit(state, unitid);
2743 if (!p1) {
0ba41d91 2744 usb_audio_err(state->chip, "unit %d not found!\n", unitid);
1da177e4
LT
2745 return -EINVAL;
2746 }
2747
9a2fe9b8
RB
2748 if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
2749 switch (p1[2]) {
2750 case UAC_INPUT_TERMINAL:
5a222e84 2751 return parse_audio_input_terminal(state, unitid, p1);
9a2fe9b8
RB
2752 case UAC_MIXER_UNIT:
2753 return parse_audio_mixer_unit(state, unitid, p1);
2754 case UAC2_CLOCK_SOURCE:
2755 return parse_clock_source_unit(state, unitid, p1);
2756 case UAC_SELECTOR_UNIT:
2757 case UAC2_CLOCK_SELECTOR:
2758 return parse_audio_selector_unit(state, unitid, p1);
2759 case UAC_FEATURE_UNIT:
2760 return parse_audio_feature_unit(state, unitid, p1);
2761 case UAC1_PROCESSING_UNIT:
2762 /* UAC2_EFFECT_UNIT has the same value */
2763 if (protocol == UAC_VERSION_1)
2764 return parse_audio_processing_unit(state, unitid, p1);
2765 else
2766 return 0; /* FIXME - effect units not implemented yet */
2767 case UAC1_EXTENSION_UNIT:
2768 /* UAC2_PROCESSING_UNIT_V2 has the same value */
2769 if (protocol == UAC_VERSION_1)
2770 return parse_audio_extension_unit(state, unitid, p1);
2771 else /* UAC_VERSION_2 */
2772 return parse_audio_processing_unit(state, unitid, p1);
2773 case UAC2_EXTENSION_UNIT_V2:
23caaf19 2774 return parse_audio_extension_unit(state, unitid, p1);
9a2fe9b8
RB
2775 default:
2776 usb_audio_err(state->chip,
2777 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2778 return -EINVAL;
2779 }
2780 } else { /* UAC_VERSION_3 */
2781 switch (p1[2]) {
2782 case UAC_INPUT_TERMINAL:
1d38f5d8 2783 return parse_audio_input_terminal(state, unitid, p1);
9a2fe9b8
RB
2784 case UAC3_MIXER_UNIT:
2785 return parse_audio_mixer_unit(state, unitid, p1);
2786 case UAC3_CLOCK_SOURCE:
2787 return parse_clock_source_unit(state, unitid, p1);
c77e1ef1 2788 case UAC3_SELECTOR_UNIT:
9a2fe9b8
RB
2789 case UAC3_CLOCK_SELECTOR:
2790 return parse_audio_selector_unit(state, unitid, p1);
2791 case UAC3_FEATURE_UNIT:
2792 return parse_audio_feature_unit(state, unitid, p1);
2793 case UAC3_EFFECT_UNIT:
2794 return 0; /* FIXME - effect units not implemented yet */
2795 case UAC3_PROCESSING_UNIT:
23caaf19 2796 return parse_audio_processing_unit(state, unitid, p1);
9a2fe9b8
RB
2797 case UAC3_EXTENSION_UNIT:
2798 return parse_audio_extension_unit(state, unitid, p1);
2799 default:
2800 usb_audio_err(state->chip,
2801 "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
2802 return -EINVAL;
2803 }
1da177e4
LT
2804 }
2805}
2806
84957a8a
CL
2807static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
2808{
124751d5
TI
2809 /* kill pending URBs */
2810 snd_usb_mixer_disconnect(mixer);
2811
6639b6c2
CL
2812 kfree(mixer->id_elems);
2813 if (mixer->urb) {
2814 kfree(mixer->urb->transfer_buffer);
2815 usb_free_urb(mixer->urb);
2816 }
68df9de1 2817 usb_free_urb(mixer->rc_urb);
b259b10c 2818 kfree(mixer->rc_setup_packet);
84957a8a
CL
2819 kfree(mixer);
2820}
2821
86e07d34 2822static int snd_usb_mixer_dev_free(struct snd_device *device)
84957a8a
CL
2823{
2824 struct usb_mixer_interface *mixer = device->device_data;
2825 snd_usb_mixer_free(mixer);
2826 return 0;
2827}
2828
17156f23
RB
2829/* UAC3 predefined channels configuration */
2830struct uac3_badd_profile {
2831 int subclass;
2832 const char *name;
2833 int c_chmask; /* capture channels mask */
2834 int p_chmask; /* playback channels mask */
2835 int st_chmask; /* side tone mixing channel mask */
2836};
2837
2838static struct uac3_badd_profile uac3_badd_profiles[] = {
2839 {
2840 /*
2841 * BAIF, BAOF or combination of both
2842 * IN: Mono or Stereo cfg, Mono alt possible
2843 * OUT: Mono or Stereo cfg, Mono alt possible
2844 */
2845 .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO,
2846 .name = "GENERIC IO",
2847 .c_chmask = -1, /* dynamic channels */
2848 .p_chmask = -1, /* dynamic channels */
2849 },
2850 {
2851 /* BAOF; Stereo only cfg, Mono alt possible */
2852 .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE,
2853 .name = "HEADPHONE",
2854 .p_chmask = 3,
2855 },
2856 {
2857 /* BAOF; Mono or Stereo cfg, Mono alt possible */
2858 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER,
2859 .name = "SPEAKER",
2860 .p_chmask = -1, /* dynamic channels */
2861 },
2862 {
2863 /* BAIF; Mono or Stereo cfg, Mono alt possible */
2864 .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE,
2865 .name = "MICROPHONE",
2866 .c_chmask = -1, /* dynamic channels */
2867 },
2868 {
2869 /*
2870 * BAIOF topology
2871 * IN: Mono only
2872 * OUT: Mono or Stereo cfg, Mono alt possible
2873 */
2874 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET,
2875 .name = "HEADSET",
2876 .c_chmask = 1,
2877 .p_chmask = -1, /* dynamic channels */
2878 .st_chmask = 1,
2879 },
2880 {
2881 /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */
2882 .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER,
2883 .name = "HEADSET ADAPTER",
2884 .c_chmask = 1,
2885 .p_chmask = 3,
2886 .st_chmask = 1,
2887 },
2888 {
2889 /* BAIF + BAOF; IN: Mono only; OUT: Mono only */
2890 .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE,
2891 .name = "SPEAKERPHONE",
2892 .c_chmask = 1,
2893 .p_chmask = 1,
2894 },
2895 { 0 } /* terminator */
2896};
2897
2898static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer,
2899 struct uac3_badd_profile *f,
2900 int c_chmask, int p_chmask)
2901{
2902 /*
2903 * If both playback/capture channels are dynamic, make sure
2904 * at least one channel is present
2905 */
2906 if (f->c_chmask < 0 && f->p_chmask < 0) {
2907 if (!c_chmask && !p_chmask) {
2908 usb_audio_warn(mixer->chip, "BAAD %s: no channels?",
2909 f->name);
2910 return false;
2911 }
2912 return true;
2913 }
2914
2915 if ((f->c_chmask < 0 && !c_chmask) ||
2916 (f->c_chmask >= 0 && f->c_chmask != c_chmask)) {
2917 usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch",
2918 f->name);
2919 return false;
2920 }
2921 if ((f->p_chmask < 0 && !p_chmask) ||
2922 (f->p_chmask >= 0 && f->p_chmask != p_chmask)) {
2923 usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch",
2924 f->name);
2925 return false;
2926 }
2927 return true;
2928}
2929
2930/*
2931 * create mixer controls for UAC3 BADD profiles
2932 *
2933 * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything
2934 *
2935 * BADD device may contain Mixer Unit, which doesn't have any controls, skip it
2936 */
2937static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer,
2938 int ctrlif)
2939{
2940 struct usb_device *dev = mixer->chip->dev;
2941 struct usb_interface_assoc_descriptor *assoc;
2942 int badd_profile = mixer->chip->badd_profile;
2943 struct uac3_badd_profile *f;
2944 const struct usbmix_ctl_map *map;
2945 int p_chmask = 0, c_chmask = 0, st_chmask = 0;
2946 int i;
2947
2948 assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
2949
2950 /* Detect BADD capture/playback channels from AS EP descriptors */
2951 for (i = 0; i < assoc->bInterfaceCount; i++) {
2952 int intf = assoc->bFirstInterface + i;
2953
2954 struct usb_interface *iface;
2955 struct usb_host_interface *alts;
2956 struct usb_interface_descriptor *altsd;
2957 unsigned int maxpacksize;
2958 char dir_in;
2959 int chmask, num;
2960
2961 if (intf == ctrlif)
2962 continue;
2963
2964 iface = usb_ifnum_to_if(dev, intf);
2965 num = iface->num_altsetting;
2966
2967 if (num < 2)
2968 return -EINVAL;
2969
2970 /*
2971 * The number of Channels in an AudioStreaming interface
2972 * and the audio sample bit resolution (16 bits or 24
2973 * bits) can be derived from the wMaxPacketSize field in
2974 * the Standard AS Audio Data Endpoint descriptor in
2975 * Alternate Setting 1
2976 */
2977 alts = &iface->altsetting[1];
2978 altsd = get_iface_desc(alts);
2979
2980 if (altsd->bNumEndpoints < 1)
2981 return -EINVAL;
2982
2983 /* check direction */
2984 dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
2985 maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2986
2987 switch (maxpacksize) {
2988 default:
2989 usb_audio_err(mixer->chip,
2990 "incorrect wMaxPacketSize 0x%x for BADD profile\n",
2991 maxpacksize);
2992 return -EINVAL;
2993 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
2994 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
2995 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
2996 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
2997 chmask = 1;
2998 break;
2999 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
3000 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
3001 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
3002 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
3003 chmask = 3;
3004 break;
3005 }
3006
3007 if (dir_in)
3008 c_chmask = chmask;
3009 else
3010 p_chmask = chmask;
3011 }
3012
3013 usb_audio_dbg(mixer->chip,
3014 "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n",
3015 badd_profile, c_chmask, p_chmask);
3016
3017 /* check the mapping table */
3018 for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) {
3019 if (map->id == badd_profile)
3020 break;
3021 }
3022
3023 if (!map->id)
3024 return -EINVAL;
3025
3026 for (f = uac3_badd_profiles; f->name; f++) {
3027 if (badd_profile == f->subclass)
3028 break;
3029 }
3030 if (!f->name)
3031 return -EINVAL;
3032 if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask))
3033 return -EINVAL;
3034 st_chmask = f->st_chmask;
3035
3036 /* Playback */
3037 if (p_chmask) {
3038 /* Master channel, always writable */
3039 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3040 UAC3_BADD_FU_ID2, map->map);
3041 /* Mono/Stereo volume channels, always writable */
3042 build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME,
3043 UAC3_BADD_FU_ID2, map->map);
3044 }
3045
3046 /* Capture */
3047 if (c_chmask) {
3048 /* Master channel, always writable */
3049 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3050 UAC3_BADD_FU_ID5, map->map);
3051 /* Mono/Stereo volume channels, always writable */
3052 build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME,
3053 UAC3_BADD_FU_ID5, map->map);
3054 }
3055
3056 /* Side tone-mixing */
3057 if (st_chmask) {
3058 /* Master channel, always writable */
3059 build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
3060 UAC3_BADD_FU_ID7, map->map);
3061 /* Mono volume channel, always writable */
3062 build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME,
3063 UAC3_BADD_FU_ID7, map->map);
3064 }
3065
3528cd8f
JS
3066 /* Insertion Control */
3067 if (f->subclass == UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER) {
3068 struct usb_audio_term iterm, oterm;
3069
3070 /* Input Term - Insertion control */
3071 memset(&iterm, 0, sizeof(iterm));
3072 iterm.id = UAC3_BADD_IT_ID4;
3073 iterm.type = UAC_BIDIR_TERMINAL_HEADSET;
3074 build_connector_control(mixer, &iterm, true);
3075
3076 /* Output Term - Insertion control */
3077 memset(&oterm, 0, sizeof(oterm));
3078 oterm.id = UAC3_BADD_OT_ID3;
3079 oterm.type = UAC_BIDIR_TERMINAL_HEADSET;
3080 build_connector_control(mixer, &oterm, false);
3081 }
3082
17156f23
RB
3083 return 0;
3084}
3085
1da177e4
LT
3086/*
3087 * create mixer controls
3088 *
de48c7bc 3089 * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
1da177e4 3090 */
84957a8a 3091static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
1da177e4 3092{
86e07d34 3093 struct mixer_build state;
1da177e4
LT
3094 int err;
3095 const struct usbmix_ctl_map *map;
23caaf19 3096 void *p;
1da177e4
LT
3097
3098 memset(&state, 0, sizeof(state));
84957a8a
CL
3099 state.chip = mixer->chip;
3100 state.mixer = mixer;
1faa5d07
DM
3101 state.buffer = mixer->hostif->extra;
3102 state.buflen = mixer->hostif->extralen;
1da177e4
LT
3103
3104 /* check the mapping table */
27d10f56
CL
3105 for (map = usbmix_ctl_maps; map->id; map++) {
3106 if (map->id == state.chip->usb_id) {
1da177e4 3107 state.map = map->map;
8e062ec7 3108 state.selector_map = map->selector_map;
84957a8a 3109 mixer->ignore_ctl_error = map->ignore_ctl_error;
1da177e4
LT
3110 break;
3111 }
3112 }
1da177e4 3113
23caaf19 3114 p = NULL;
6bc170e4
DM
3115 while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
3116 mixer->hostif->extralen,
1faa5d07 3117 p, UAC_OUTPUT_TERMINAL)) != NULL) {
23caaf19 3118 if (mixer->protocol == UAC_VERSION_1) {
69da9bcb 3119 struct uac1_output_terminal_descriptor *desc = p;
23caaf19
DM
3120
3121 if (desc->bLength < sizeof(*desc))
3122 continue; /* invalid descriptor? */
6bc170e4
DM
3123 /* mark terminal ID as visited */
3124 set_bit(desc->bTerminalID, state.unitbitmap);
23caaf19
DM
3125 state.oterm.id = desc->bTerminalID;
3126 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3127 state.oterm.name = desc->iTerminal;
3128 err = parse_audio_unit(&state, desc->bSourceID);
83ea5d18 3129 if (err < 0 && err != -EINVAL)
23caaf19 3130 return err;
9a2fe9b8 3131 } else if (mixer->protocol == UAC_VERSION_2) {
23caaf19
DM
3132 struct uac2_output_terminal_descriptor *desc = p;
3133
3134 if (desc->bLength < sizeof(*desc))
3135 continue; /* invalid descriptor? */
6bc170e4
DM
3136 /* mark terminal ID as visited */
3137 set_bit(desc->bTerminalID, state.unitbitmap);
23caaf19
DM
3138 state.oterm.id = desc->bTerminalID;
3139 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3140 state.oterm.name = desc->iTerminal;
3141 err = parse_audio_unit(&state, desc->bSourceID);
83ea5d18 3142 if (err < 0 && err != -EINVAL)
23caaf19 3143 return err;
09414207 3144
6bc170e4
DM
3145 /*
3146 * For UAC2, use the same approach to also add the
3147 * clock selectors
3148 */
09414207 3149 err = parse_audio_unit(&state, desc->bCSourceID);
83ea5d18 3150 if (err < 0 && err != -EINVAL)
09414207 3151 return err;
5a222e84 3152
2b54f785 3153 if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
5a222e84 3154 UAC2_TE_CONNECTOR)) {
167e1fb1 3155 build_connector_control(state.mixer, &state.oterm,
5a222e84
AC
3156 false);
3157 }
9a2fe9b8
RB
3158 } else { /* UAC_VERSION_3 */
3159 struct uac3_output_terminal_descriptor *desc = p;
3160
3161 if (desc->bLength < sizeof(*desc))
3162 continue; /* invalid descriptor? */
3163 /* mark terminal ID as visited */
3164 set_bit(desc->bTerminalID, state.unitbitmap);
3165 state.oterm.id = desc->bTerminalID;
3166 state.oterm.type = le16_to_cpu(desc->wTerminalType);
3167 state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
3168 err = parse_audio_unit(&state, desc->bSourceID);
3169 if (err < 0 && err != -EINVAL)
3170 return err;
3171
3172 /*
3173 * For UAC3, use the same approach to also add the
3174 * clock selectors
3175 */
3176 err = parse_audio_unit(&state, desc->bCSourceID);
3177 if (err < 0 && err != -EINVAL)
3178 return err;
1d38f5d8
JS
3179
3180 if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls),
3181 UAC3_TE_INSERTION)) {
167e1fb1 3182 build_connector_control(state.mixer, &state.oterm,
1d38f5d8
JS
3183 false);
3184 }
23caaf19 3185 }
1da177e4 3186 }
23caaf19 3187
1da177e4
LT
3188 return 0;
3189}
84957a8a 3190
7b1eda22 3191void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
6639b6c2 3192{
3360b84b 3193 struct usb_mixer_elem_list *list;
6639b6c2 3194
8c558076 3195 for_each_mixer_elem(list, mixer, unitid) {
b2500b58 3196 struct usb_mixer_elem_info *info =
8c558076 3197 mixer_elem_list_to_info(list);
b2500b58
JS
3198 /* invalidate cache, so the value is read from the device */
3199 info->cached = 0;
6639b6c2 3200 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3360b84b 3201 &list->kctl->id);
b2500b58 3202 }
6639b6c2
CL
3203}
3204
ebfdeea3 3205static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
3360b84b 3206 struct usb_mixer_elem_list *list)
ebfdeea3 3207{
8c558076 3208 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
ebfdeea3
JK
3209 static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
3210 "S8", "U8", "S16", "U16"};
ebfdeea3 3211 snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
3360b84b 3212 "channels=%i, type=\"%s\"\n", cval->head.id,
ebfdeea3
JK
3213 cval->control, cval->cmask, cval->channels,
3214 val_types[cval->val_type]);
3215 snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
3216 cval->min, cval->max, cval->dBmin, cval->dBmax);
3217}
3218
3219static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
3220 struct snd_info_buffer *buffer)
3221{
3222 struct snd_usb_audio *chip = entry->private_data;
3223 struct usb_mixer_interface *mixer;
3360b84b 3224 struct usb_mixer_elem_list *list;
ebfdeea3
JK
3225 int unitid;
3226
3227 list_for_each_entry(mixer, &chip->mixer_list, list) {
3228 snd_iprintf(buffer,
7affdc17 3229 "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
3d8d4dcf 3230 chip->usb_id, snd_usb_ctrl_intf(chip),
7affdc17 3231 mixer->ignore_ctl_error);
ebfdeea3
JK
3232 snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
3233 for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
8c558076 3234 for_each_mixer_elem(list, mixer, unitid) {
3360b84b
TI
3235 snd_iprintf(buffer, " Unit: %i\n", list->id);
3236 if (list->kctl)
3237 snd_iprintf(buffer,
3238 " Control: name=\"%s\", index=%i\n",
3239 list->kctl->id.name,
3240 list->kctl->id.index);
3241 if (list->dump)
3242 list->dump(buffer, list);
3243 }
ebfdeea3
JK
3244 }
3245 }
3246}
3247
e213e9cf
DM
3248static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
3249 int attribute, int value, int index)
3250{
3360b84b 3251 struct usb_mixer_elem_list *list;
e213e9cf
DM
3252 __u8 unitid = (index >> 8) & 0xff;
3253 __u8 control = (value >> 8) & 0xff;
3254 __u8 channel = value & 0xff;
191227d9 3255 unsigned int count = 0;
e213e9cf
DM
3256
3257 if (channel >= MAX_CHANNELS) {
0ba41d91
TI
3258 usb_audio_dbg(mixer->chip,
3259 "%s(): bogus channel number %d\n",
3260 __func__, channel);
e213e9cf
DM
3261 return;
3262 }
3263
8c558076 3264 for_each_mixer_elem(list, mixer, unitid)
191227d9
DM
3265 count++;
3266
3267 if (count == 0)
3268 return;
3269
8c558076 3270 for_each_mixer_elem(list, mixer, unitid) {
3360b84b
TI
3271 struct usb_mixer_elem_info *info;
3272
3273 if (!list->kctl)
3274 continue;
3275
8c558076 3276 info = mixer_elem_list_to_info(list);
191227d9 3277 if (count > 1 && info->control != control)
e213e9cf
DM
3278 continue;
3279
3280 switch (attribute) {
3281 case UAC2_CS_CUR:
3282 /* invalidate cache, so the value is read from the device */
3283 if (channel)
3284 info->cached &= ~(1 << channel);
3285 else /* master channel */
3286 info->cached = 0;
3287
3288 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3360b84b 3289 &info->head.kctl->id);
e213e9cf
DM
3290 break;
3291
3292 case UAC2_CS_RANGE:
3293 /* TODO */
3294 break;
3295
3296 case UAC2_CS_MEM:
3297 /* TODO */
3298 break;
3299
3300 default:
0ba41d91
TI
3301 usb_audio_dbg(mixer->chip,
3302 "unknown attribute %d in interrupt\n",
3303 attribute);
e213e9cf
DM
3304 break;
3305 } /* switch */
3306 }
3307}
3308
3309static void snd_usb_mixer_interrupt(struct urb *urb)
6639b6c2
CL
3310{
3311 struct usb_mixer_interface *mixer = urb->context;
e213e9cf 3312 int len = urb->actual_length;
edf7de31 3313 int ustatus = urb->status;
e213e9cf 3314
edf7de31 3315 if (ustatus != 0)
e213e9cf 3316 goto requeue;
6639b6c2 3317
e213e9cf
DM
3318 if (mixer->protocol == UAC_VERSION_1) {
3319 struct uac1_status_word *status;
6639b6c2 3320
e213e9cf
DM
3321 for (status = urb->transfer_buffer;
3322 len >= sizeof(*status);
3323 len -= sizeof(*status), status++) {
0ba41d91 3324 dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
e213e9cf
DM
3325 status->bStatusType,
3326 status->bOriginator);
3327
6639b6c2 3328 /* ignore any notifications not from the control interface */
e213e9cf
DM
3329 if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
3330 UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
6639b6c2 3331 continue;
e213e9cf
DM
3332
3333 if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
3334 snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
b259b10c 3335 else
e213e9cf
DM
3336 snd_usb_mixer_notify_id(mixer, status->bOriginator);
3337 }
3338 } else { /* UAC_VERSION_2 */
3339 struct uac2_interrupt_data_msg *msg;
3340
3341 for (msg = urb->transfer_buffer;
3342 len >= sizeof(*msg);
3343 len -= sizeof(*msg), msg++) {
3344 /* drop vendor specific and endpoint requests */
3345 if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
3346 (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
3347 continue;
3348
3349 snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
3350 le16_to_cpu(msg->wValue),
3351 le16_to_cpu(msg->wIndex));
6639b6c2
CL
3352 }
3353 }
e213e9cf
DM
3354
3355requeue:
6bc170e4
DM
3356 if (ustatus != -ENOENT &&
3357 ustatus != -ECONNRESET &&
3358 ustatus != -ESHUTDOWN) {
6639b6c2
CL
3359 urb->dev = mixer->chip->dev;
3360 usb_submit_urb(urb, GFP_ATOMIC);
3361 }
3362}
3363
3364/* create the handler for the optional status interrupt endpoint */
3365static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
3366{
6639b6c2
CL
3367 struct usb_endpoint_descriptor *ep;
3368 void *transfer_buffer;
3369 int buffer_length;
3370 unsigned int epnum;
3371
6639b6c2 3372 /* we need one interrupt input endpoint */
1faa5d07 3373 if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
6639b6c2 3374 return 0;
1faa5d07 3375 ep = get_endpoint(mixer->hostif, 0);
913ae5a2 3376 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
6639b6c2
CL
3377 return 0;
3378
42a6e66f 3379 epnum = usb_endpoint_num(ep);
6639b6c2
CL
3380 buffer_length = le16_to_cpu(ep->wMaxPacketSize);
3381 transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
3382 if (!transfer_buffer)
3383 return -ENOMEM;
3384 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3385 if (!mixer->urb) {
3386 kfree(transfer_buffer);
3387 return -ENOMEM;
3388 }
3389 usb_fill_int_urb(mixer->urb, mixer->chip->dev,
3390 usb_rcvintpipe(mixer->chip->dev, epnum),
3391 transfer_buffer, buffer_length,
e213e9cf 3392 snd_usb_mixer_interrupt, mixer, ep->bInterval);
6639b6c2
CL
3393 usb_submit_urb(mixer->urb, GFP_KERNEL);
3394 return 0;
3395}
3396
4120fbed
TI
3397static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol,
3398 struct snd_ctl_elem_value *ucontrol)
3399{
3400 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3401
3402 ucontrol->value.integer.value[0] = mixer->chip->keep_iface;
3403 return 0;
3404}
3405
3406static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol,
3407 struct snd_ctl_elem_value *ucontrol)
3408{
3409 struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
3410 bool keep_iface = !!ucontrol->value.integer.value[0];
3411
3412 if (mixer->chip->keep_iface == keep_iface)
3413 return 0;
3414 mixer->chip->keep_iface = keep_iface;
3415 return 1;
3416}
3417
3418static const struct snd_kcontrol_new keep_iface_ctl = {
3419 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3420 .name = "Keep Interface",
3421 .info = snd_ctl_boolean_mono_info,
3422 .get = keep_iface_ctl_get,
3423 .put = keep_iface_ctl_put,
3424};
3425
3426static int create_keep_iface_ctl(struct usb_mixer_interface *mixer)
3427{
3428 struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer);
3429
3430 /* need only one control per card */
3431 if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) {
3432 snd_ctl_free_one(kctl);
3433 return 0;
3434 }
3435
3436 return snd_ctl_add(mixer->chip->card, kctl);
3437}
3438
7a9b8063
TI
3439int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
3440 int ignore_error)
84957a8a 3441{
86e07d34 3442 static struct snd_device_ops dev_ops = {
84957a8a
CL
3443 .dev_free = snd_usb_mixer_dev_free
3444 };
3445 struct usb_mixer_interface *mixer;
23caaf19 3446 int err;
84957a8a
CL
3447
3448 strcpy(chip->card->mixername, "USB Mixer");
3449
561b220a 3450 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
84957a8a
CL
3451 if (!mixer)
3452 return -ENOMEM;
3453 mixer->chip = chip;
7a9b8063 3454 mixer->ignore_ctl_error = ignore_error;
291186e0
JK
3455 mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
3456 GFP_KERNEL);
6639b6c2
CL
3457 if (!mixer->id_elems) {
3458 kfree(mixer);
3459 return -ENOMEM;
3460 }
84957a8a 3461
1faa5d07
DM
3462 mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
3463 switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
a2acad82
CL
3464 case UAC_VERSION_1:
3465 default:
3466 mixer->protocol = UAC_VERSION_1;
3467 break;
3468 case UAC_VERSION_2:
3469 mixer->protocol = UAC_VERSION_2;
3470 break;
9a2fe9b8
RB
3471 case UAC_VERSION_3:
3472 mixer->protocol = UAC_VERSION_3;
3473 break;
a2acad82 3474 }
7b8a043f 3475
17156f23
RB
3476 if (mixer->protocol == UAC_VERSION_3 &&
3477 chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
f25ecf8f
TI
3478 err = snd_usb_mixer_controls_badd(mixer, ctrlif);
3479 if (err < 0)
3480 goto _error;
3481 } else {
3482 err = snd_usb_mixer_controls(mixer);
3483 if (err < 0)
3484 goto _error;
17156f23 3485 }
ad6baae6
JS
3486
3487 err = snd_usb_mixer_status_create(mixer);
3488 if (err < 0)
3489 goto _error;
3490
4120fbed
TI
3491 err = create_keep_iface_ctl(mixer);
3492 if (err < 0)
3493 goto _error;
84957a8a 3494
328e9f69
TI
3495 err = snd_usb_mixer_apply_create_quirk(mixer);
3496 if (err < 0)
3497 goto _error;
468b8fde 3498
9cbb2808 3499 err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
93446edc
CL
3500 if (err < 0)
3501 goto _error;
ebfdeea3 3502
7449054a
TI
3503 if (list_empty(&chip->mixer_list))
3504 snd_card_ro_proc_new(chip->card, "usbmixer", chip,
3505 snd_usb_mixer_proc_read);
ebfdeea3 3506
84957a8a
CL
3507 list_add(&mixer->list, &chip->mixer_list);
3508 return 0;
93446edc
CL
3509
3510_error:
3511 snd_usb_mixer_free(mixer);
3512 return err;
84957a8a
CL
3513}
3514
a6cece9d 3515void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
84957a8a 3516{
124751d5
TI
3517 if (mixer->disconnected)
3518 return;
3519 if (mixer->urb)
3520 usb_kill_urb(mixer->urb);
3521 if (mixer->rc_urb)
3522 usb_kill_urb(mixer->rc_urb);
3523 mixer->disconnected = true;
84957a8a 3524}
400362f1
TI
3525
3526#ifdef CONFIG_PM
3527/* stop any bus activity of a mixer */
3528static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
3529{
3530 usb_kill_urb(mixer->urb);
3531 usb_kill_urb(mixer->rc_urb);
3532}
3533
3534static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
3535{
3536 int err;
3537
3538 if (mixer->urb) {
3539 err = usb_submit_urb(mixer->urb, GFP_NOIO);
3540 if (err < 0)
3541 return err;
3542 }
3543
3544 return 0;
3545}
3546
3547int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
3548{
3549 snd_usb_mixer_inactivate(mixer);
3550 return 0;
3551}
3552
3360b84b 3553static int restore_mixer_value(struct usb_mixer_elem_list *list)
400362f1 3554{
8c558076 3555 struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
400362f1
TI
3556 int c, err, idx;
3557
3558 if (cval->cmask) {
3559 idx = 0;
3560 for (c = 0; c < MAX_CHANNELS; c++) {
3561 if (!(cval->cmask & (1 << c)))
3562 continue;
6aa6925c 3563 if (cval->cached & (1 << (c + 1))) {
eef90451 3564 err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
400362f1
TI
3565 cval->cache_val[idx]);
3566 if (err < 0)
3567 return err;
3568 }
3569 idx++;
3570 }
3571 } else {
3572 /* master */
3573 if (cval->cached) {
eef90451 3574 err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
400362f1
TI
3575 if (err < 0)
3576 return err;
3577 }
3578 }
3579
3580 return 0;
3581}
3582
3583int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
3584{
3360b84b 3585 struct usb_mixer_elem_list *list;
400362f1
TI
3586 int id, err;
3587
400362f1
TI
3588 if (reset_resume) {
3589 /* restore cached mixer values */
3590 for (id = 0; id < MAX_ID_ELEMS; id++) {
8c558076 3591 for_each_mixer_elem(list, mixer, id) {
3360b84b
TI
3592 if (list->resume) {
3593 err = list->resume(list);
3594 if (err < 0)
3595 return err;
3596 }
400362f1
TI
3597 }
3598 }
3599 }
3600
964af639
TI
3601 snd_usb_mixer_resume_quirk(mixer);
3602
400362f1
TI
3603 return snd_usb_mixer_activate(mixer);
3604}
3605#endif
3360b84b
TI
3606
3607void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
3608 struct usb_mixer_interface *mixer,
3609 int unitid)
3610{
3611 list->mixer = mixer;
3612 list->id = unitid;
3613 list->dump = snd_usb_mixer_dump_cval;
3614#ifdef CONFIG_PM
3615 list->resume = restore_mixer_value;
3616#endif
3617}