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