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