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