xref: /linux/sound/usb/mixer_quirks.c (revision 7693c0cc415f3a16a7a3355f245474a5e661be4e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   USB Audio Driver for ALSA
4  *
5  *   Quirks and vendor-specific extensions for mixer interfaces
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  *   Audio Advantage Micro II support added by:
14  *	    Przemek Rudy (prudy1@o2.pl)
15  */
16 
17 #include <linux/bitfield.h>
18 #include <linux/hid.h>
19 #include <linux/init.h>
20 #include <linux/input.h>
21 #include <linux/math64.h>
22 #include <linux/slab.h>
23 #include <linux/usb.h>
24 #include <linux/usb/audio.h>
25 
26 #include <sound/asoundef.h>
27 #include <sound/core.h>
28 #include <sound/control.h>
29 #include <sound/hda_verbs.h>
30 #include <sound/hwdep.h>
31 #include <sound/info.h>
32 #include <sound/tlv.h>
33 
34 #include "usbaudio.h"
35 #include "mixer.h"
36 #include "mixer_quirks.h"
37 #include "mixer_scarlett.h"
38 #include "mixer_scarlett2.h"
39 #include "mixer_us16x08.h"
40 #include "mixer_s1810c.h"
41 #include "helper.h"
42 #include "fcp.h"
43 
44 struct std_mono_table {
45 	unsigned int unitid, control, cmask;
46 	int val_type;
47 	const char *name;
48 	snd_kcontrol_tlv_rw_t *tlv_callback;
49 };
50 
51 /* This function allows for the creation of standard UAC controls.
52  * See the quirks for M-Audio FTUs or Ebox-44.
53  * If you don't want to set a TLV callback pass NULL.
54  *
55  * Since there doesn't seem to be a devices that needs a multichannel
56  * version, we keep it mono for simplicity.
57  */
58 static int snd_create_std_mono_ctl_offset(struct usb_mixer_interface *mixer,
59 					  unsigned int unitid,
60 					  unsigned int control,
61 					  unsigned int cmask,
62 					  int val_type,
63 					  unsigned int idx_off,
64 					  const char *name,
65 					  snd_kcontrol_tlv_rw_t *tlv_callback)
66 {
67 	struct usb_mixer_elem_info *cval;
68 	struct snd_kcontrol *kctl;
69 
70 	cval = kzalloc_obj(*cval);
71 	if (!cval)
72 		return -ENOMEM;
73 
74 	snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
75 	cval->val_type = val_type;
76 	cval->channels = 1;
77 	cval->control = control;
78 	cval->cmask = cmask;
79 	cval->idx_off = idx_off;
80 
81 	/* get_min_max() is called only for integer volumes later,
82 	 * so provide a short-cut for booleans
83 	 */
84 	cval->min = 0;
85 	cval->max = 1;
86 	cval->res = 0;
87 	cval->dBmin = 0;
88 	cval->dBmax = 0;
89 
90 	/* Create control */
91 	kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
92 	if (!kctl) {
93 		kfree(cval);
94 		return -ENOMEM;
95 	}
96 
97 	/* Set name */
98 	snprintf(kctl->id.name, sizeof(kctl->id.name), name);
99 	kctl->private_free = snd_usb_mixer_elem_free;
100 
101 	/* set TLV */
102 	if (tlv_callback) {
103 		kctl->tlv.c = tlv_callback;
104 		kctl->vd[0].access |=
105 			SNDRV_CTL_ELEM_ACCESS_TLV_READ |
106 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
107 	}
108 	/* Add control to mixer */
109 	return snd_usb_mixer_add_control(&cval->head, kctl);
110 }
111 
112 static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
113 				   unsigned int unitid,
114 				   unsigned int control,
115 				   unsigned int cmask,
116 				   int val_type,
117 				   const char *name,
118 				   snd_kcontrol_tlv_rw_t *tlv_callback)
119 {
120 	return snd_create_std_mono_ctl_offset(mixer, unitid, control, cmask,
121 					      val_type, 0 /* Offset */,
122 					      name, tlv_callback);
123 }
124 
125 /*
126  * Create a set of standard UAC controls from a table
127  */
128 static int snd_create_std_mono_table(struct usb_mixer_interface *mixer,
129 				     const struct std_mono_table *t)
130 {
131 	int err;
132 
133 	while (t->name) {
134 		err = snd_create_std_mono_ctl(mixer, t->unitid, t->control,
135 					      t->cmask, t->val_type, t->name,
136 					      t->tlv_callback);
137 		if (err < 0)
138 			return err;
139 		t++;
140 	}
141 
142 	return 0;
143 }
144 
145 static int add_single_ctl_with_resume(struct usb_mixer_interface *mixer,
146 				      int id,
147 				      usb_mixer_elem_resume_func_t resume,
148 				      const struct snd_kcontrol_new *knew,
149 				      struct usb_mixer_elem_list **listp)
150 {
151 	struct usb_mixer_elem_list *list;
152 	struct snd_kcontrol *kctl;
153 
154 	list = kzalloc_obj(*list);
155 	if (!list)
156 		return -ENOMEM;
157 	if (listp)
158 		*listp = list;
159 	list->mixer = mixer;
160 	list->id = id;
161 	list->resume = resume;
162 	kctl = snd_ctl_new1(knew, list);
163 	if (!kctl) {
164 		kfree(list);
165 		return -ENOMEM;
166 	}
167 	kctl->private_free = snd_usb_mixer_elem_free;
168 	/* don't use snd_usb_mixer_add_control() here, this is a special list element */
169 	return snd_usb_mixer_add_list(list, kctl, false);
170 }
171 
172 /*
173  * Sound Blaster remote control configuration
174  *
175  * format of remote control data:
176  * Extigy:       xx 00
177  * Audigy 2 NX:  06 80 xx 00 00 00
178  * Live! 24-bit: 06 80 xx yy 22 83
179  */
180 static const struct rc_config {
181 	u32 usb_id;
182 	u8  offset;
183 	u8  length;
184 	u8  packet_length;
185 	u8  min_packet_length; /* minimum accepted length of the URB result */
186 	u8  mute_mixer_id;
187 	u32 mute_code;
188 } rc_configs[] = {
189 	{ USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
190 	{ USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
191 	{ USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
192 	{ USB_ID(0x041e, 0x3042), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 */
193 	{ USB_ID(0x041e, 0x30df), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
194 	{ USB_ID(0x041e, 0x3237), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
195 	{ USB_ID(0x041e, 0x3263), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
196 	{ USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
197 };
198 
199 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
200 {
201 	struct usb_mixer_interface *mixer = urb->context;
202 	const struct rc_config *rc = mixer->rc_cfg;
203 	u32 code;
204 
205 	if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
206 		return;
207 
208 	code = mixer->rc_buffer[rc->offset];
209 	if (rc->length == 2)
210 		code |= mixer->rc_buffer[rc->offset + 1] << 8;
211 
212 	/* the Mute button actually changes the mixer control */
213 	if (code == rc->mute_code)
214 		snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
215 	mixer->rc_code = code;
216 	wake_up(&mixer->rc_waitq);
217 }
218 
219 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
220 				    long count, loff_t *offset)
221 {
222 	struct usb_mixer_interface *mixer = hw->private_data;
223 	int err;
224 	u32 rc_code;
225 
226 	if (count != 1 && count != 4)
227 		return -EINVAL;
228 	err = wait_event_interruptible(mixer->rc_waitq,
229 				       (rc_code = xchg(&mixer->rc_code, 0)) != 0);
230 	if (err == 0) {
231 		if (count == 1)
232 			err = put_user(rc_code, buf);
233 		else
234 			err = put_user(rc_code, (u32 __user *)buf);
235 	}
236 	return err < 0 ? err : count;
237 }
238 
239 static __poll_t snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
240 					poll_table *wait)
241 {
242 	struct usb_mixer_interface *mixer = hw->private_data;
243 
244 	poll_wait(file, &mixer->rc_waitq, wait);
245 	return mixer->rc_code ? EPOLLIN | EPOLLRDNORM : 0;
246 }
247 
248 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
249 {
250 	struct snd_hwdep *hwdep;
251 	int err, len, i;
252 
253 	for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
254 		if (rc_configs[i].usb_id == mixer->chip->usb_id)
255 			break;
256 	if (i >= ARRAY_SIZE(rc_configs))
257 		return 0;
258 	mixer->rc_cfg = &rc_configs[i];
259 
260 	len = mixer->rc_cfg->packet_length;
261 
262 	init_waitqueue_head(&mixer->rc_waitq);
263 	err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
264 	if (err < 0)
265 		return err;
266 	snprintf(hwdep->name, sizeof(hwdep->name),
267 		 "%s remote control", mixer->chip->card->shortname);
268 	hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
269 	hwdep->private_data = mixer;
270 	hwdep->ops.read = snd_usb_sbrc_hwdep_read;
271 	hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
272 	hwdep->exclusive = 1;
273 
274 	mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
275 	if (!mixer->rc_urb)
276 		return -ENOMEM;
277 	mixer->rc_setup_packet = kmalloc_obj(*mixer->rc_setup_packet);
278 	if (!mixer->rc_setup_packet) {
279 		usb_free_urb(mixer->rc_urb);
280 		mixer->rc_urb = NULL;
281 		return -ENOMEM;
282 	}
283 	mixer->rc_setup_packet->bRequestType =
284 		USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
285 	mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
286 	mixer->rc_setup_packet->wValue = cpu_to_le16(0);
287 	mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
288 	mixer->rc_setup_packet->wLength = cpu_to_le16(len);
289 	usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
290 			     usb_rcvctrlpipe(mixer->chip->dev, 0),
291 			     (u8 *)mixer->rc_setup_packet, mixer->rc_buffer, len,
292 			     snd_usb_soundblaster_remote_complete, mixer);
293 	return 0;
294 }
295 
296 #define snd_audigy2nx_led_info		snd_ctl_boolean_mono_info
297 
298 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
299 {
300 	ucontrol->value.integer.value[0] = kcontrol->private_value >> 8;
301 	return 0;
302 }
303 
304 static int snd_audigy2nx_led_update(struct usb_mixer_interface *mixer,
305 				    int value, int index)
306 {
307 	struct snd_usb_audio *chip = mixer->chip;
308 	int err;
309 
310 	CLASS(snd_usb_lock, pm)(chip);
311 	if (pm.err < 0)
312 		return pm.err;
313 
314 	if (chip->usb_id == USB_ID(0x041e, 0x3042) ||	/* USB X-Fi S51 */
315 	    chip->usb_id == USB_ID(0x041e, 0x30df))	/* USB X-Fi S51 Pro */
316 		err = snd_usb_ctl_msg(chip->dev,
317 				      usb_sndctrlpipe(chip->dev, 0), 0x24,
318 				      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
319 				      !value, 0, NULL, 0);
320 	else
321 		err = snd_usb_ctl_msg(chip->dev,
322 				      usb_sndctrlpipe(chip->dev, 0), 0x24,
323 				      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
324 				      value, index + 2, NULL, 0);
325 	return err;
326 }
327 
328 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol,
329 				 struct snd_ctl_elem_value *ucontrol)
330 {
331 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
332 	struct usb_mixer_interface *mixer = list->mixer;
333 	int index = kcontrol->private_value & 0xff;
334 	unsigned int value = ucontrol->value.integer.value[0];
335 	int old_value = kcontrol->private_value >> 8;
336 	unsigned long old_pval = kcontrol->private_value;
337 	int err;
338 
339 	if (value > 1)
340 		return -EINVAL;
341 	if (value == old_value)
342 		return 0;
343 	kcontrol->private_value = (value << 8) | index;
344 	err = snd_audigy2nx_led_update(mixer, value, index);
345 	if (err < 0) {
346 		kcontrol->private_value = old_pval;
347 		return err;
348 	}
349 	return 1;
350 }
351 
352 static int snd_audigy2nx_led_resume(struct usb_mixer_elem_list *list)
353 {
354 	int priv_value = list->kctl->private_value;
355 
356 	return snd_audigy2nx_led_update(list->mixer, priv_value >> 8,
357 					priv_value & 0xff);
358 }
359 
360 /* name and private_value are set dynamically */
361 static const struct snd_kcontrol_new snd_audigy2nx_control = {
362 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
363 	.info = snd_audigy2nx_led_info,
364 	.get = snd_audigy2nx_led_get,
365 	.put = snd_audigy2nx_led_put,
366 };
367 
368 static const char * const snd_audigy2nx_led_names[] = {
369 	"CMSS LED Switch",
370 	"Power LED Switch",
371 	"Dolby Digital LED Switch",
372 };
373 
374 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
375 {
376 	int i, err;
377 
378 	for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_led_names); ++i) {
379 		struct snd_kcontrol_new knew;
380 
381 		/* USB X-Fi S51 doesn't have a CMSS LED */
382 		if (mixer->chip->usb_id == USB_ID(0x041e, 0x3042) && i == 0)
383 			continue;
384 		/* USB X-Fi S51 Pro doesn't have one either */
385 		if (mixer->chip->usb_id == USB_ID(0x041e, 0x30df) && i == 0)
386 			continue;
387 		if (i > 1 && /* Live24ext has 2 LEDs only */
388 			(mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
389 			 mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
390 			 mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
391 			 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
392 			break;
393 
394 		knew = snd_audigy2nx_control;
395 		knew.name = snd_audigy2nx_led_names[i];
396 		knew.private_value = (1 << 8) | i; /* LED on as default */
397 		err = add_single_ctl_with_resume(mixer, 0,
398 						 snd_audigy2nx_led_resume,
399 						 &knew, NULL);
400 		if (err < 0)
401 			return err;
402 	}
403 	return 0;
404 }
405 
406 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
407 				    struct snd_info_buffer *buffer)
408 {
409 	static const struct sb_jack {
410 		int unitid;
411 		const char *name;
412 	}  jacks_audigy2nx[] = {
413 		{4,  "dig in "},
414 		{7,  "line in"},
415 		{19, "spk out"},
416 		{20, "hph out"},
417 		{-1, NULL}
418 	}, jacks_live24ext[] = {
419 		{4,  "line in"}, /* &1=Line, &2=Mic*/
420 		{3,  "hph out"}, /* headphones */
421 		{0,  "RC     "}, /* last command, 6 bytes see rc_config above */
422 		{-1, NULL}
423 	};
424 	const struct sb_jack *jacks;
425 	struct usb_mixer_interface *mixer = entry->private_data;
426 	int i, err;
427 	u8 buf[3];
428 
429 	snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
430 	if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
431 		jacks = jacks_audigy2nx;
432 	else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
433 		 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
434 		jacks = jacks_live24ext;
435 	else
436 		return;
437 
438 	for (i = 0; jacks[i].name; ++i) {
439 		snd_iprintf(buffer, "%s: ", jacks[i].name);
440 		CLASS(snd_usb_lock, pm)(mixer->chip);
441 		if (pm.err < 0)
442 			return;
443 		err = snd_usb_ctl_msg(mixer->chip->dev,
444 				      usb_rcvctrlpipe(mixer->chip->dev, 0),
445 				      UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
446 				      USB_RECIP_INTERFACE, 0,
447 				      jacks[i].unitid << 8, buf, 3);
448 		if (err == 3 && (buf[0] == 3 || buf[0] == 6))
449 			snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
450 		else
451 			snd_iprintf(buffer, "?\n");
452 	}
453 }
454 
455 /* EMU0204 */
456 static int snd_emu0204_ch_switch_info(struct snd_kcontrol *kcontrol,
457 				      struct snd_ctl_elem_info *uinfo)
458 {
459 	static const char * const texts[2] = {"1/2", "3/4"};
460 
461 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
462 }
463 
464 static int snd_emu0204_ch_switch_get(struct snd_kcontrol *kcontrol,
465 				     struct snd_ctl_elem_value *ucontrol)
466 {
467 	ucontrol->value.enumerated.item[0] = kcontrol->private_value;
468 	return 0;
469 }
470 
471 static int snd_emu0204_ch_switch_update(struct usb_mixer_interface *mixer,
472 					int value)
473 {
474 	struct snd_usb_audio *chip = mixer->chip;
475 	unsigned char buf[2];
476 
477 	CLASS(snd_usb_lock, pm)(chip);
478 	if (pm.err < 0)
479 		return pm.err;
480 
481 	buf[0] = 0x01;
482 	buf[1] = value ? 0x02 : 0x01;
483 	return snd_usb_ctl_msg(chip->dev,
484 			       usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
485 			       USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
486 			       0x0400, 0x0e00, buf, 2);
487 }
488 
489 static int snd_emu0204_ch_switch_put(struct snd_kcontrol *kcontrol,
490 				     struct snd_ctl_elem_value *ucontrol)
491 {
492 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
493 	struct usb_mixer_interface *mixer = list->mixer;
494 	unsigned int value = ucontrol->value.enumerated.item[0];
495 	unsigned long old_pval = kcontrol->private_value;
496 	int err;
497 
498 	if (value > 1)
499 		return -EINVAL;
500 
501 	if (value == kcontrol->private_value)
502 		return 0;
503 
504 	kcontrol->private_value = value;
505 	err = snd_emu0204_ch_switch_update(mixer, value);
506 	if (err < 0) {
507 		kcontrol->private_value = old_pval;
508 		return err;
509 	}
510 	return 1;
511 }
512 
513 static int snd_emu0204_ch_switch_resume(struct usb_mixer_elem_list *list)
514 {
515 	return snd_emu0204_ch_switch_update(list->mixer,
516 					    list->kctl->private_value);
517 }
518 
519 static const struct snd_kcontrol_new snd_emu0204_control = {
520 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
521 	.name = "Front Jack Channels",
522 	.info = snd_emu0204_ch_switch_info,
523 	.get = snd_emu0204_ch_switch_get,
524 	.put = snd_emu0204_ch_switch_put,
525 	.private_value = 0,
526 };
527 
528 static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
529 {
530 	return add_single_ctl_with_resume(mixer, 0,
531 					  snd_emu0204_ch_switch_resume,
532 					  &snd_emu0204_control, NULL);
533 }
534 
535 #if IS_REACHABLE(CONFIG_INPUT)
536 /*
537  * Sony DualSense controller (PS5) jack detection
538  *
539  * Since this is an UAC 1 device, it doesn't support jack detection.
540  * However, the controller hid-playstation driver reports HP & MIC
541  * insert events through a dedicated input device.
542  */
543 
544 #define SND_DUALSENSE_JACK_OUT_TERM_ID 3
545 #define SND_DUALSENSE_JACK_IN_TERM_ID 4
546 
547 struct dualsense_mixer_elem_info {
548 	struct usb_mixer_elem_info info;
549 	struct input_handler ih;
550 	struct input_device_id id_table[2];
551 	bool connected;
552 };
553 
554 static void snd_dualsense_ih_event(struct input_handle *handle,
555 				   unsigned int type, unsigned int code,
556 				   int value)
557 {
558 	struct dualsense_mixer_elem_info *mei;
559 	struct usb_mixer_elem_list *me;
560 
561 	if (type != EV_SW)
562 		return;
563 
564 	mei = container_of(handle->handler, struct dualsense_mixer_elem_info, ih);
565 	me = &mei->info.head;
566 
567 	if ((me->id == SND_DUALSENSE_JACK_OUT_TERM_ID && code == SW_HEADPHONE_INSERT) ||
568 	    (me->id == SND_DUALSENSE_JACK_IN_TERM_ID && code == SW_MICROPHONE_INSERT)) {
569 		mei->connected = !!value;
570 		snd_ctl_notify(me->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
571 			       &me->kctl->id);
572 	}
573 }
574 
575 static bool snd_dualsense_ih_match(struct input_handler *handler,
576 				   struct input_dev *dev)
577 {
578 	struct dualsense_mixer_elem_info *mei;
579 	struct usb_device *snd_dev;
580 	struct device *parent;
581 
582 	mei = container_of(handler, struct dualsense_mixer_elem_info, ih);
583 	snd_dev = mei->info.head.mixer->chip->dev;
584 
585 	/*
586 	 * Ensure the VID:PID matched input device supposedly owned by the
587 	 * hid-playstation driver belongs to the actual hardware handled by
588 	 * the current USB audio device.
589 	 *
590 	 * This verification is necessary when there is more than one identical
591 	 * controller attached to the host system.
592 	 *
593 	 * The input device is registered below the HID device, USB interface and
594 	 * USB device, so compare the parent chain directly instead of building
595 	 * kobject path strings. This avoids dereferencing kobject names while the
596 	 * USB device hierarchy is being torn down during disconnect.
597 	 */
598 	for (parent = dev->dev.parent; parent; parent = parent->parent) {
599 		if (parent == &snd_dev->dev)
600 			return true;
601 	}
602 
603 	return false;
604 }
605 
606 static int snd_dualsense_ih_connect(struct input_handler *handler,
607 				    struct input_dev *dev,
608 				    const struct input_device_id *id)
609 {
610 	struct input_handle *handle;
611 	int err;
612 
613 	handle = kzalloc_obj(*handle);
614 	if (!handle)
615 		return -ENOMEM;
616 
617 	handle->dev = dev;
618 	handle->handler = handler;
619 	handle->name = handler->name;
620 
621 	err = input_register_handle(handle);
622 	if (err)
623 		goto err_free;
624 
625 	err = input_open_device(handle);
626 	if (err)
627 		goto err_unregister;
628 
629 	return 0;
630 
631 err_unregister:
632 	input_unregister_handle(handle);
633 err_free:
634 	kfree(handle);
635 	return err;
636 }
637 
638 static void snd_dualsense_ih_disconnect(struct input_handle *handle)
639 {
640 	input_close_device(handle);
641 	input_unregister_handle(handle);
642 	kfree(handle);
643 }
644 
645 static void snd_dualsense_ih_start(struct input_handle *handle)
646 {
647 	struct dualsense_mixer_elem_info *mei;
648 	struct usb_mixer_elem_list *me;
649 	int status = -1;
650 
651 	mei = container_of(handle->handler, struct dualsense_mixer_elem_info, ih);
652 	me = &mei->info.head;
653 
654 	if (me->id == SND_DUALSENSE_JACK_OUT_TERM_ID &&
655 	    test_bit(SW_HEADPHONE_INSERT, handle->dev->swbit))
656 		status = test_bit(SW_HEADPHONE_INSERT, handle->dev->sw);
657 	else if (me->id == SND_DUALSENSE_JACK_IN_TERM_ID &&
658 		 test_bit(SW_MICROPHONE_INSERT, handle->dev->swbit))
659 		status = test_bit(SW_MICROPHONE_INSERT, handle->dev->sw);
660 
661 	if (status >= 0) {
662 		mei->connected = !!status;
663 		snd_ctl_notify(me->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
664 			       &me->kctl->id);
665 	}
666 }
667 
668 static int snd_dualsense_jack_get(struct snd_kcontrol *kctl,
669 				  struct snd_ctl_elem_value *ucontrol)
670 {
671 	struct dualsense_mixer_elem_info *mei = snd_kcontrol_chip(kctl);
672 
673 	ucontrol->value.integer.value[0] = mei->connected;
674 
675 	return 0;
676 }
677 
678 static const struct snd_kcontrol_new snd_dualsense_jack_control = {
679 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
680 	.access = SNDRV_CTL_ELEM_ACCESS_READ,
681 	.info = snd_ctl_boolean_mono_info,
682 	.get = snd_dualsense_jack_get,
683 };
684 
685 static int snd_dualsense_resume_jack(struct usb_mixer_elem_list *list)
686 {
687 	snd_ctl_notify(list->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
688 		       &list->kctl->id);
689 	return 0;
690 }
691 
692 static void snd_dualsense_mixer_elem_free(struct snd_kcontrol *kctl)
693 {
694 	struct dualsense_mixer_elem_info *mei = snd_kcontrol_chip(kctl);
695 
696 	if (mei->ih.event)
697 		input_unregister_handler(&mei->ih);
698 
699 	snd_usb_mixer_elem_free(kctl);
700 }
701 
702 static int snd_dualsense_jack_create(struct usb_mixer_interface *mixer,
703 				     const char *name, bool is_output)
704 {
705 	struct dualsense_mixer_elem_info *mei;
706 	struct input_device_id *idev_id;
707 	struct snd_kcontrol *kctl;
708 	int err;
709 
710 	mei = kzalloc_obj(*mei);
711 	if (!mei)
712 		return -ENOMEM;
713 
714 	snd_usb_mixer_elem_init_std(&mei->info.head, mixer,
715 				    is_output ? SND_DUALSENSE_JACK_OUT_TERM_ID :
716 						SND_DUALSENSE_JACK_IN_TERM_ID);
717 
718 	mei->info.head.resume = snd_dualsense_resume_jack;
719 	mei->info.val_type = USB_MIXER_BOOLEAN;
720 	mei->info.channels = 1;
721 	mei->info.min = 0;
722 	mei->info.max = 1;
723 
724 	kctl = snd_ctl_new1(&snd_dualsense_jack_control, mei);
725 	if (!kctl) {
726 		kfree(mei);
727 		return -ENOMEM;
728 	}
729 
730 	strscpy(kctl->id.name, name, sizeof(kctl->id.name));
731 	kctl->private_free = snd_dualsense_mixer_elem_free;
732 
733 	err = snd_usb_mixer_add_control(&mei->info.head, kctl);
734 	if (err)
735 		return err;
736 
737 	idev_id = &mei->id_table[0];
738 	idev_id->flags = INPUT_DEVICE_ID_MATCH_VENDOR | INPUT_DEVICE_ID_MATCH_PRODUCT |
739 			 INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT;
740 	idev_id->vendor = USB_ID_VENDOR(mixer->chip->usb_id);
741 	idev_id->product = USB_ID_PRODUCT(mixer->chip->usb_id);
742 	idev_id->evbit[BIT_WORD(EV_SW)] = BIT_MASK(EV_SW);
743 	if (is_output)
744 		idev_id->swbit[BIT_WORD(SW_HEADPHONE_INSERT)] = BIT_MASK(SW_HEADPHONE_INSERT);
745 	else
746 		idev_id->swbit[BIT_WORD(SW_MICROPHONE_INSERT)] = BIT_MASK(SW_MICROPHONE_INSERT);
747 
748 	mei->ih.event = snd_dualsense_ih_event;
749 	mei->ih.match = snd_dualsense_ih_match;
750 	mei->ih.connect = snd_dualsense_ih_connect;
751 	mei->ih.disconnect = snd_dualsense_ih_disconnect;
752 	mei->ih.start = snd_dualsense_ih_start;
753 	mei->ih.name = name;
754 	mei->ih.id_table = mei->id_table;
755 
756 	err = input_register_handler(&mei->ih);
757 	if (err) {
758 		dev_warn(&mixer->chip->dev->dev,
759 			 "Could not register input handler: %d\n", err);
760 		mei->ih.event = NULL;
761 	}
762 
763 	return 0;
764 }
765 
766 static int snd_dualsense_controls_create(struct usb_mixer_interface *mixer)
767 {
768 	int err;
769 
770 	err = snd_dualsense_jack_create(mixer, "Headphone Jack", true);
771 	if (err < 0)
772 		return err;
773 
774 	return snd_dualsense_jack_create(mixer, "Headset Mic Jack", false);
775 }
776 #endif /* IS_REACHABLE(CONFIG_INPUT) */
777 
778 /* ASUS Xonar U1 / U3 controls */
779 
780 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
781 				   struct snd_ctl_elem_value *ucontrol)
782 {
783 	ucontrol->value.integer.value[0] = !!(kcontrol->private_value & 0x02);
784 	return 0;
785 }
786 
787 static int snd_xonar_u1_switch_update(struct usb_mixer_interface *mixer,
788 				      unsigned char status)
789 {
790 	struct snd_usb_audio *chip = mixer->chip;
791 
792 	CLASS(snd_usb_lock, pm)(chip);
793 	if (pm.err < 0)
794 		return pm.err;
795 	return snd_usb_ctl_msg(chip->dev,
796 			       usb_sndctrlpipe(chip->dev, 0), 0x08,
797 			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
798 			       50, 0, &status, 1);
799 }
800 
801 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
802 				   struct snd_ctl_elem_value *ucontrol)
803 {
804 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
805 	u8 old_status, new_status;
806 	int err;
807 
808 	old_status = kcontrol->private_value;
809 	if (ucontrol->value.integer.value[0])
810 		new_status = old_status | 0x02;
811 	else
812 		new_status = old_status & ~0x02;
813 	if (new_status == old_status)
814 		return 0;
815 
816 	kcontrol->private_value = new_status;
817 	err = snd_xonar_u1_switch_update(list->mixer, new_status);
818 	if (err < 0) {
819 		kcontrol->private_value = old_status;
820 		return err;
821 	}
822 	return 1;
823 }
824 
825 static int snd_xonar_u1_switch_resume(struct usb_mixer_elem_list *list)
826 {
827 	return snd_xonar_u1_switch_update(list->mixer,
828 					  list->kctl->private_value);
829 }
830 
831 static const struct snd_kcontrol_new snd_xonar_u1_output_switch = {
832 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
833 	.name = "Digital Playback Switch",
834 	.info = snd_ctl_boolean_mono_info,
835 	.get = snd_xonar_u1_switch_get,
836 	.put = snd_xonar_u1_switch_put,
837 	.private_value = 0x05,
838 };
839 
840 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
841 {
842 	return add_single_ctl_with_resume(mixer, 0,
843 					  snd_xonar_u1_switch_resume,
844 					  &snd_xonar_u1_output_switch, NULL);
845 }
846 
847 /* Digidesign Mbox 1 helper functions */
848 
849 static int snd_mbox1_is_spdif_synced(struct snd_usb_audio *chip)
850 {
851 	unsigned char buff[3];
852 	int err;
853 	int is_spdif_synced;
854 
855 	/* Read clock source */
856 	err = snd_usb_ctl_msg(chip->dev,
857 			      usb_rcvctrlpipe(chip->dev, 0), 0x81,
858 			      USB_DIR_IN |
859 			      USB_TYPE_CLASS |
860 			      USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
861 	if (err < 0)
862 		return err;
863 
864 	/* spdif sync: buff is all zeroes */
865 	is_spdif_synced = !(buff[0] | buff[1] | buff[2]);
866 	return is_spdif_synced;
867 }
868 
869 static int snd_mbox1_set_clk_source(struct snd_usb_audio *chip, int rate_or_zero)
870 {
871 	/* 2 possibilities:	Internal    -> expects sample rate
872 	 *			S/PDIF sync -> expects rate = 0
873 	 */
874 	unsigned char buff[3];
875 
876 	buff[0] = (rate_or_zero >>  0) & 0xff;
877 	buff[1] = (rate_or_zero >>  8) & 0xff;
878 	buff[2] = (rate_or_zero >> 16) & 0xff;
879 
880 	/* Set clock source */
881 	return snd_usb_ctl_msg(chip->dev,
882 			       usb_sndctrlpipe(chip->dev, 0), 0x1,
883 			       USB_TYPE_CLASS |
884 			       USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
885 }
886 
887 static int snd_mbox1_is_spdif_input(struct snd_usb_audio *chip)
888 {
889 	/* Hardware gives 2 possibilities:	ANALOG Source  -> 0x01
890 	 *					S/PDIF Source  -> 0x02
891 	 */
892 	int err;
893 	unsigned char source[1];
894 
895 	/* Read input source */
896 	err = snd_usb_ctl_msg(chip->dev,
897 			      usb_rcvctrlpipe(chip->dev, 0), 0x81,
898 			      USB_DIR_IN |
899 			      USB_TYPE_CLASS |
900 			      USB_RECIP_INTERFACE, 0x00, 0x500, source, 1);
901 	if (err < 0)
902 		return err;
903 
904 	return (source[0] == 2);
905 }
906 
907 static int snd_mbox1_set_input_source(struct snd_usb_audio *chip, int is_spdif)
908 {
909 	/* NB: Setting the input source to S/PDIF resets the clock source to S/PDIF
910 	 * Hardware expects 2 possibilities:	ANALOG Source  -> 0x01
911 	 *					S/PDIF Source  -> 0x02
912 	 */
913 	unsigned char buff[1];
914 
915 	buff[0] = (is_spdif & 1) + 1;
916 
917 	/* Set input source */
918 	return snd_usb_ctl_msg(chip->dev,
919 			       usb_sndctrlpipe(chip->dev, 0), 0x1,
920 			       USB_TYPE_CLASS |
921 			       USB_RECIP_INTERFACE, 0x00, 0x500, buff, 1);
922 }
923 
924 /* Digidesign Mbox 1 clock source switch (internal/spdif) */
925 
926 static int snd_mbox1_clk_switch_get(struct snd_kcontrol *kctl,
927 				    struct snd_ctl_elem_value *ucontrol)
928 {
929 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
930 	struct snd_usb_audio *chip = list->mixer->chip;
931 	int err;
932 
933 	CLASS(snd_usb_lock, pm)(chip);
934 	if (pm.err < 0)
935 		return pm.err;
936 
937 	err = snd_mbox1_is_spdif_synced(chip);
938 	if (err < 0)
939 		return err;
940 
941 	kctl->private_value = err;
942 	ucontrol->value.enumerated.item[0] = kctl->private_value;
943 	return 0;
944 }
945 
946 static int snd_mbox1_clk_switch_update(struct usb_mixer_interface *mixer, int is_spdif_sync)
947 {
948 	struct snd_usb_audio *chip = mixer->chip;
949 	int err;
950 
951 	CLASS(snd_usb_lock, pm)(chip);
952 	if (pm.err < 0)
953 		return pm.err;
954 
955 	err = snd_mbox1_is_spdif_input(chip);
956 	if (err < 0)
957 		return err;
958 
959 	err = snd_mbox1_is_spdif_synced(chip);
960 	if (err < 0)
961 		return err;
962 
963 	/* FIXME: hardcoded sample rate */
964 	err = snd_mbox1_set_clk_source(chip, is_spdif_sync ? 0 : 48000);
965 	if (err < 0)
966 		return err;
967 
968 	return snd_mbox1_is_spdif_synced(chip);
969 }
970 
971 static int snd_mbox1_clk_switch_put(struct snd_kcontrol *kctl,
972 				    struct snd_ctl_elem_value *ucontrol)
973 {
974 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
975 	struct usb_mixer_interface *mixer = list->mixer;
976 	int err;
977 	bool cur_val, new_val;
978 
979 	cur_val = kctl->private_value;
980 	new_val = ucontrol->value.enumerated.item[0];
981 	if (cur_val == new_val)
982 		return 0;
983 
984 	kctl->private_value = new_val;
985 	err = snd_mbox1_clk_switch_update(mixer, new_val);
986 	return err < 0 ? err : 1;
987 }
988 
989 static int snd_mbox1_clk_switch_info(struct snd_kcontrol *kcontrol,
990 				     struct snd_ctl_elem_info *uinfo)
991 {
992 	static const char *const texts[2] = {
993 		"Internal",
994 		"S/PDIF"
995 	};
996 
997 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
998 }
999 
1000 static int snd_mbox1_clk_switch_resume(struct usb_mixer_elem_list *list)
1001 {
1002 	return snd_mbox1_clk_switch_update(list->mixer, list->kctl->private_value);
1003 }
1004 
1005 /* Digidesign Mbox 1 input source switch (analog/spdif) */
1006 
1007 static int snd_mbox1_src_switch_get(struct snd_kcontrol *kctl,
1008 				    struct snd_ctl_elem_value *ucontrol)
1009 {
1010 	ucontrol->value.enumerated.item[0] = kctl->private_value;
1011 	return 0;
1012 }
1013 
1014 static int snd_mbox1_src_switch_update(struct usb_mixer_interface *mixer, int is_spdif_input)
1015 {
1016 	struct snd_usb_audio *chip = mixer->chip;
1017 	int err;
1018 
1019 	CLASS(snd_usb_lock, pm)(chip);
1020 	if (pm.err < 0)
1021 		return pm.err;
1022 
1023 	err = snd_mbox1_is_spdif_input(chip);
1024 	if (err < 0)
1025 		return err;
1026 
1027 	err = snd_mbox1_set_input_source(chip, is_spdif_input);
1028 	if (err < 0)
1029 		return err;
1030 
1031 	err = snd_mbox1_is_spdif_input(chip);
1032 	if (err < 0)
1033 		return err;
1034 
1035 	return snd_mbox1_is_spdif_synced(chip);
1036 }
1037 
1038 static int snd_mbox1_src_switch_put(struct snd_kcontrol *kctl,
1039 				    struct snd_ctl_elem_value *ucontrol)
1040 {
1041 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
1042 	struct usb_mixer_interface *mixer = list->mixer;
1043 	int err;
1044 	bool cur_val, new_val;
1045 
1046 	cur_val = kctl->private_value;
1047 	new_val = ucontrol->value.enumerated.item[0];
1048 	if (cur_val == new_val)
1049 		return 0;
1050 
1051 	kctl->private_value = new_val;
1052 	err = snd_mbox1_src_switch_update(mixer, new_val);
1053 	return err < 0 ? err : 1;
1054 }
1055 
1056 static int snd_mbox1_src_switch_info(struct snd_kcontrol *kcontrol,
1057 				     struct snd_ctl_elem_info *uinfo)
1058 {
1059 	static const char *const texts[2] = {
1060 		"Analog",
1061 		"S/PDIF"
1062 	};
1063 
1064 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1065 }
1066 
1067 static int snd_mbox1_src_switch_resume(struct usb_mixer_elem_list *list)
1068 {
1069 	return snd_mbox1_src_switch_update(list->mixer, list->kctl->private_value);
1070 }
1071 
1072 static const struct snd_kcontrol_new snd_mbox1_clk_switch = {
1073 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1074 	.name = "Clock Source",
1075 	.index = 0,
1076 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1077 	.info = snd_mbox1_clk_switch_info,
1078 	.get = snd_mbox1_clk_switch_get,
1079 	.put = snd_mbox1_clk_switch_put,
1080 	.private_value = 0
1081 };
1082 
1083 static const struct snd_kcontrol_new snd_mbox1_src_switch = {
1084 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1085 	.name = "Input Source",
1086 	.index = 1,
1087 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1088 	.info = snd_mbox1_src_switch_info,
1089 	.get = snd_mbox1_src_switch_get,
1090 	.put = snd_mbox1_src_switch_put,
1091 	.private_value = 0
1092 };
1093 
1094 static int snd_mbox1_controls_create(struct usb_mixer_interface *mixer)
1095 {
1096 	int err;
1097 
1098 	err = add_single_ctl_with_resume(mixer, 0,
1099 					 snd_mbox1_clk_switch_resume,
1100 					 &snd_mbox1_clk_switch, NULL);
1101 	if (err < 0)
1102 		return err;
1103 
1104 	return add_single_ctl_with_resume(mixer, 1,
1105 					  snd_mbox1_src_switch_resume,
1106 					  &snd_mbox1_src_switch, NULL);
1107 }
1108 
1109 /* Native Instruments device quirks */
1110 
1111 #define _MAKE_NI_CONTROL(bRequest, wIndex) ((bRequest) << 16 | (wIndex))
1112 
1113 static int snd_ni_control_init_val(struct usb_mixer_interface *mixer,
1114 				   struct snd_kcontrol *kctl)
1115 {
1116 	struct usb_device *dev = mixer->chip->dev;
1117 	unsigned int pval = kctl->private_value;
1118 	u8 value;
1119 	int err;
1120 
1121 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1122 			      (pval >> 16) & 0xff,
1123 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
1124 			      0, pval & 0xffff, &value, 1);
1125 	if (err < 0) {
1126 		dev_err(&dev->dev,
1127 			"unable to issue vendor read request (ret = %d)", err);
1128 		return err;
1129 	}
1130 
1131 	kctl->private_value |= ((unsigned int)value << 24);
1132 	return 0;
1133 }
1134 
1135 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
1136 					     struct snd_ctl_elem_value *ucontrol)
1137 {
1138 	ucontrol->value.integer.value[0] = kcontrol->private_value >> 24;
1139 	return 0;
1140 }
1141 
1142 static int snd_ni_update_cur_val(struct usb_mixer_elem_list *list)
1143 {
1144 	struct snd_usb_audio *chip = list->mixer->chip;
1145 	unsigned int pval = list->kctl->private_value;
1146 
1147 	CLASS(snd_usb_lock, pm)(chip);
1148 	if (pm.err < 0)
1149 		return pm.err;
1150 	return usb_control_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
1151 			       (pval >> 16) & 0xff,
1152 			       USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
1153 			       pval >> 24, pval & 0xffff, NULL, 0, 1000);
1154 }
1155 
1156 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
1157 					     struct snd_ctl_elem_value *ucontrol)
1158 {
1159 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1160 	unsigned long old_pval = kcontrol->private_value;
1161 	u8 oldval = (old_pval >> 24) & 0xff;
1162 	u8 newval = ucontrol->value.integer.value[0];
1163 	int err;
1164 
1165 	if (oldval == newval)
1166 		return 0;
1167 
1168 	kcontrol->private_value &= ~(0xff << 24);
1169 	kcontrol->private_value |= (unsigned int)newval << 24;
1170 	err = snd_ni_update_cur_val(list);
1171 	if (err < 0) {
1172 		kcontrol->private_value = old_pval;
1173 		return err;
1174 	}
1175 	return 1;
1176 }
1177 
1178 static const struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
1179 	{
1180 		.name = "Direct Thru Channel A",
1181 		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
1182 	},
1183 	{
1184 		.name = "Direct Thru Channel B",
1185 		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
1186 	},
1187 	{
1188 		.name = "Phono Input Channel A",
1189 		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
1190 	},
1191 	{
1192 		.name = "Phono Input Channel B",
1193 		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
1194 	},
1195 };
1196 
1197 static const struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
1198 	{
1199 		.name = "Direct Thru Channel A",
1200 		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
1201 	},
1202 	{
1203 		.name = "Direct Thru Channel B",
1204 		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
1205 	},
1206 	{
1207 		.name = "Direct Thru Channel C",
1208 		.private_value = _MAKE_NI_CONTROL(0x01, 0x07),
1209 	},
1210 	{
1211 		.name = "Direct Thru Channel D",
1212 		.private_value = _MAKE_NI_CONTROL(0x01, 0x09),
1213 	},
1214 	{
1215 		.name = "Phono Input Channel A",
1216 		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
1217 	},
1218 	{
1219 		.name = "Phono Input Channel B",
1220 		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
1221 	},
1222 	{
1223 		.name = "Phono Input Channel C",
1224 		.private_value = _MAKE_NI_CONTROL(0x02, 0x07),
1225 	},
1226 	{
1227 		.name = "Phono Input Channel D",
1228 		.private_value = _MAKE_NI_CONTROL(0x02, 0x09),
1229 	},
1230 };
1231 
1232 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
1233 					      const struct snd_kcontrol_new *kc,
1234 					      unsigned int count)
1235 {
1236 	int i, err = 0;
1237 	struct snd_kcontrol_new template = {
1238 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1239 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1240 		.get = snd_nativeinstruments_control_get,
1241 		.put = snd_nativeinstruments_control_put,
1242 		.info = snd_ctl_boolean_mono_info,
1243 	};
1244 
1245 	for (i = 0; i < count; i++) {
1246 		struct usb_mixer_elem_list *list;
1247 
1248 		template.name = kc[i].name;
1249 		template.private_value = kc[i].private_value;
1250 
1251 		err = add_single_ctl_with_resume(mixer, 0,
1252 						 snd_ni_update_cur_val,
1253 						 &template, &list);
1254 		if (err < 0)
1255 			break;
1256 		snd_ni_control_init_val(mixer, list->kctl);
1257 	}
1258 
1259 	return err;
1260 }
1261 
1262 /* M-Audio FastTrack Ultra quirks */
1263 /* FTU Effect switch (also used by C400/C600) */
1264 static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
1265 				   struct snd_ctl_elem_info *uinfo)
1266 {
1267 	static const char *const texts[8] = {
1268 		"Room 1", "Room 2", "Room 3", "Hall 1",
1269 		"Hall 2", "Plate", "Delay", "Echo"
1270 	};
1271 
1272 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1273 }
1274 
1275 static int snd_ftu_eff_switch_init(struct usb_mixer_interface *mixer,
1276 				   struct snd_kcontrol *kctl)
1277 {
1278 	struct usb_device *dev = mixer->chip->dev;
1279 	unsigned int pval = kctl->private_value;
1280 	int err;
1281 	unsigned char value[2];
1282 
1283 	value[0] = 0x00;
1284 	value[1] = 0x00;
1285 
1286 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
1287 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1288 			      (pval & 0xff00) | ((pval & 0xff0000) >> 16),
1289 			      snd_usb_ctrl_intf(mixer->hostif) | ((pval & 0xff) << 8),
1290 			      value, 2);
1291 	if (err < 0)
1292 		return err;
1293 
1294 	kctl->private_value |= (unsigned int)value[0] << 24;
1295 	return 0;
1296 }
1297 
1298 static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
1299 				  struct snd_ctl_elem_value *ucontrol)
1300 {
1301 	ucontrol->value.enumerated.item[0] = kctl->private_value >> 24;
1302 	return 0;
1303 }
1304 
1305 static int snd_ftu_eff_switch_update(struct usb_mixer_elem_list *list)
1306 {
1307 	struct snd_usb_audio *chip = list->mixer->chip;
1308 	unsigned int pval = list->kctl->private_value;
1309 	unsigned char value[2];
1310 
1311 	value[0] = pval >> 24;
1312 	value[1] = 0;
1313 
1314 	CLASS(snd_usb_lock, pm)(chip);
1315 	if (pm.err < 0)
1316 		return pm.err;
1317 	return snd_usb_ctl_msg(chip->dev,
1318 			       usb_sndctrlpipe(chip->dev, 0),
1319 			       UAC_SET_CUR,
1320 			       USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1321 			       (pval & 0xff00) | ((pval & 0xff0000) >> 16),
1322 			       snd_usb_ctrl_intf(list->mixer->hostif) | ((pval & 0xff) << 8),
1323 			       value, 2);
1324 }
1325 
1326 static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
1327 				  struct snd_ctl_elem_value *ucontrol)
1328 {
1329 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
1330 	unsigned long old_pval = list->kctl->private_value;
1331 	unsigned int pval = old_pval;
1332 	int cur_val, err, new_val;
1333 
1334 	cur_val = pval >> 24;
1335 	new_val = ucontrol->value.enumerated.item[0];
1336 	if (cur_val == new_val)
1337 		return 0;
1338 
1339 	kctl->private_value &= ~(0xff << 24);
1340 	kctl->private_value |= new_val << 24;
1341 	err = snd_ftu_eff_switch_update(list);
1342 	if (err < 0) {
1343 		kctl->private_value = old_pval;
1344 		return err;
1345 	}
1346 	return 1;
1347 }
1348 
1349 static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer,
1350 					int validx, int bUnitID)
1351 {
1352 	static struct snd_kcontrol_new template = {
1353 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1354 		.name = "Effect Program Switch",
1355 		.index = 0,
1356 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1357 		.info = snd_ftu_eff_switch_info,
1358 		.get = snd_ftu_eff_switch_get,
1359 		.put = snd_ftu_eff_switch_put
1360 	};
1361 	struct usb_mixer_elem_list *list;
1362 	int err;
1363 
1364 	err = add_single_ctl_with_resume(mixer, bUnitID,
1365 					 snd_ftu_eff_switch_update,
1366 					 &template, &list);
1367 	if (err < 0)
1368 		return err;
1369 	list->kctl->private_value = (validx << 8) | bUnitID;
1370 	snd_ftu_eff_switch_init(mixer, list->kctl);
1371 	return 0;
1372 }
1373 
1374 /* Create volume controls for FTU devices*/
1375 static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
1376 {
1377 	char name[64];
1378 	unsigned int control, cmask;
1379 	int in, out, err;
1380 
1381 	const unsigned int id = 5;
1382 	const int val_type = USB_MIXER_S16;
1383 
1384 	for (out = 0; out < 8; out++) {
1385 		control = out + 1;
1386 		for (in = 0; in < 8; in++) {
1387 			cmask = BIT(in);
1388 			snprintf(name, sizeof(name),
1389 				 "AIn%d - Out%d Capture Volume",
1390 				 in  + 1, out + 1);
1391 			err = snd_create_std_mono_ctl(mixer, id, control,
1392 						      cmask, val_type, name,
1393 						      &snd_usb_mixer_vol_tlv);
1394 			if (err < 0)
1395 				return err;
1396 		}
1397 		for (in = 8; in < 16; in++) {
1398 			cmask = BIT(in);
1399 			snprintf(name, sizeof(name),
1400 				 "DIn%d - Out%d Playback Volume",
1401 				 in - 7, out + 1);
1402 			err = snd_create_std_mono_ctl(mixer, id, control,
1403 						      cmask, val_type, name,
1404 						      &snd_usb_mixer_vol_tlv);
1405 			if (err < 0)
1406 				return err;
1407 		}
1408 	}
1409 
1410 	return 0;
1411 }
1412 
1413 /* This control needs a volume quirk, see mixer.c */
1414 static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1415 {
1416 	static const char name[] = "Effect Volume";
1417 	const unsigned int id = 6;
1418 	const int val_type = USB_MIXER_U8;
1419 	const unsigned int control = 2;
1420 	const unsigned int cmask = 0;
1421 
1422 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1423 					name, snd_usb_mixer_vol_tlv);
1424 }
1425 
1426 /* This control needs a volume quirk, see mixer.c */
1427 static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1428 {
1429 	static const char name[] = "Effect Duration";
1430 	const unsigned int id = 6;
1431 	const int val_type = USB_MIXER_S16;
1432 	const unsigned int control = 3;
1433 	const unsigned int cmask = 0;
1434 
1435 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1436 					name, snd_usb_mixer_vol_tlv);
1437 }
1438 
1439 /* This control needs a volume quirk, see mixer.c */
1440 static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1441 {
1442 	static const char name[] = "Effect Feedback Volume";
1443 	const unsigned int id = 6;
1444 	const int val_type = USB_MIXER_U8;
1445 	const unsigned int control = 4;
1446 	const unsigned int cmask = 0;
1447 
1448 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1449 					name, NULL);
1450 }
1451 
1452 static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
1453 {
1454 	unsigned int cmask;
1455 	int err, ch;
1456 	char name[48];
1457 
1458 	const unsigned int id = 7;
1459 	const int val_type = USB_MIXER_S16;
1460 	const unsigned int control = 7;
1461 
1462 	for (ch = 0; ch < 4; ++ch) {
1463 		cmask = BIT(ch);
1464 		snprintf(name, sizeof(name),
1465 			 "Effect Return %d Volume", ch + 1);
1466 		err = snd_create_std_mono_ctl(mixer, id, control,
1467 					      cmask, val_type, name,
1468 					      snd_usb_mixer_vol_tlv);
1469 		if (err < 0)
1470 			return err;
1471 	}
1472 
1473 	return 0;
1474 }
1475 
1476 static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
1477 {
1478 	unsigned int  cmask;
1479 	int err, ch;
1480 	char name[48];
1481 
1482 	const unsigned int id = 5;
1483 	const int val_type = USB_MIXER_S16;
1484 	const unsigned int control = 9;
1485 
1486 	for (ch = 0; ch < 8; ++ch) {
1487 		cmask = BIT(ch);
1488 		snprintf(name, sizeof(name),
1489 			 "Effect Send AIn%d Volume", ch + 1);
1490 		err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1491 					      val_type, name,
1492 					      snd_usb_mixer_vol_tlv);
1493 		if (err < 0)
1494 			return err;
1495 	}
1496 	for (ch = 8; ch < 16; ++ch) {
1497 		cmask = BIT(ch);
1498 		snprintf(name, sizeof(name),
1499 			 "Effect Send DIn%d Volume", ch - 7);
1500 		err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1501 					      val_type, name,
1502 					      snd_usb_mixer_vol_tlv);
1503 		if (err < 0)
1504 			return err;
1505 	}
1506 	return 0;
1507 }
1508 
1509 static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
1510 {
1511 	int err;
1512 
1513 	err = snd_ftu_create_volume_ctls(mixer);
1514 	if (err < 0)
1515 		return err;
1516 
1517 	err = snd_ftu_create_effect_switch(mixer, 1, 6);
1518 	if (err < 0)
1519 		return err;
1520 
1521 	err = snd_ftu_create_effect_volume_ctl(mixer);
1522 	if (err < 0)
1523 		return err;
1524 
1525 	err = snd_ftu_create_effect_duration_ctl(mixer);
1526 	if (err < 0)
1527 		return err;
1528 
1529 	err = snd_ftu_create_effect_feedback_ctl(mixer);
1530 	if (err < 0)
1531 		return err;
1532 
1533 	err = snd_ftu_create_effect_return_ctls(mixer);
1534 	if (err < 0)
1535 		return err;
1536 
1537 	err = snd_ftu_create_effect_send_ctls(mixer);
1538 	if (err < 0)
1539 		return err;
1540 
1541 	return 0;
1542 }
1543 
1544 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
1545 			       unsigned char samplerate_id)
1546 {
1547 	struct usb_mixer_interface *mixer;
1548 	struct usb_mixer_elem_info *cval;
1549 	int err;
1550 	int unitid = 12; /* SampleRate ExtensionUnit ID */
1551 
1552 	list_for_each_entry(mixer, &chip->mixer_list, list) {
1553 		if (mixer->id_elems[unitid]) {
1554 			cval = mixer_elem_list_to_info(mixer->id_elems[unitid]);
1555 			err = snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1556 							  cval->control << 8,
1557 							  samplerate_id);
1558 			if (!err)
1559 				snd_usb_mixer_notify_id(mixer, unitid);
1560 			break;
1561 		}
1562 	}
1563 }
1564 
1565 /* M-Audio Fast Track C400/C600 */
1566 /* C400/C600 volume controls, this control needs a volume quirk, see mixer.c */
1567 static int snd_c400_create_vol_ctls(struct usb_mixer_interface *mixer)
1568 {
1569 	char name[64];
1570 	unsigned int cmask, offset;
1571 	int out, chan, err;
1572 	int num_outs = 0;
1573 	int num_ins = 0;
1574 
1575 	const unsigned int id = 0x40;
1576 	const int val_type = USB_MIXER_S16;
1577 	const int control = 1;
1578 
1579 	switch (mixer->chip->usb_id) {
1580 	case USB_ID(0x0763, 0x2030):
1581 		num_outs = 6;
1582 		num_ins = 4;
1583 		break;
1584 	case USB_ID(0x0763, 0x2031):
1585 		num_outs = 8;
1586 		num_ins = 6;
1587 		break;
1588 	}
1589 
1590 	for (chan = 0; chan < num_outs + num_ins; chan++) {
1591 		for (out = 0; out < num_outs; out++) {
1592 			if (chan < num_outs) {
1593 				snprintf(name, sizeof(name),
1594 					 "PCM%d-Out%d Playback Volume",
1595 					 chan + 1, out + 1);
1596 			} else {
1597 				snprintf(name, sizeof(name),
1598 					 "In%d-Out%d Playback Volume",
1599 					 chan - num_outs + 1, out + 1);
1600 			}
1601 
1602 			cmask = (out == 0) ? 0 : BIT(out - 1);
1603 			offset = chan * num_outs;
1604 			err = snd_create_std_mono_ctl_offset(mixer, id, control,
1605 							     cmask, val_type, offset, name,
1606 							     &snd_usb_mixer_vol_tlv);
1607 			if (err < 0)
1608 				return err;
1609 		}
1610 	}
1611 
1612 	return 0;
1613 }
1614 
1615 /* This control needs a volume quirk, see mixer.c */
1616 static int snd_c400_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1617 {
1618 	static const char name[] = "Effect Volume";
1619 	const unsigned int id = 0x43;
1620 	const int val_type = USB_MIXER_U8;
1621 	const unsigned int control = 3;
1622 	const unsigned int cmask = 0;
1623 
1624 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1625 				       name, snd_usb_mixer_vol_tlv);
1626 }
1627 
1628 /* This control needs a volume quirk, see mixer.c */
1629 static int snd_c400_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1630 {
1631 	static const char name[] = "Effect Duration";
1632 	const unsigned int id = 0x43;
1633 	const int val_type = USB_MIXER_S16;
1634 	const unsigned int control = 4;
1635 	const unsigned int cmask = 0;
1636 
1637 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1638 				       name, snd_usb_mixer_vol_tlv);
1639 }
1640 
1641 /* This control needs a volume quirk, see mixer.c */
1642 static int snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1643 {
1644 	static const char name[] = "Effect Feedback Volume";
1645 	const unsigned int id = 0x43;
1646 	const int val_type = USB_MIXER_U8;
1647 	const unsigned int control = 5;
1648 	const unsigned int cmask = 0;
1649 
1650 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1651 				       name, NULL);
1652 }
1653 
1654 static int snd_c400_create_effect_vol_ctls(struct usb_mixer_interface *mixer)
1655 {
1656 	char name[64];
1657 	unsigned int cmask;
1658 	int chan, err;
1659 	int num_outs = 0;
1660 	int num_ins = 0;
1661 
1662 	const unsigned int id = 0x42;
1663 	const int val_type = USB_MIXER_S16;
1664 	const int control = 1;
1665 
1666 	switch (mixer->chip->usb_id) {
1667 	case USB_ID(0x0763, 0x2030):
1668 		num_outs = 6;
1669 		num_ins = 4;
1670 		break;
1671 	case USB_ID(0x0763, 0x2031):
1672 		num_outs = 8;
1673 		num_ins = 6;
1674 		break;
1675 	}
1676 
1677 	for (chan = 0; chan < num_outs + num_ins; chan++) {
1678 		if (chan < num_outs) {
1679 			snprintf(name, sizeof(name),
1680 				 "Effect Send DOut%d",
1681 				 chan + 1);
1682 		} else {
1683 			snprintf(name, sizeof(name),
1684 				 "Effect Send AIn%d",
1685 				 chan - num_outs + 1);
1686 		}
1687 
1688 		cmask = (chan == 0) ? 0 : BIT(chan - 1);
1689 		err = snd_create_std_mono_ctl(mixer, id, control,
1690 					      cmask, val_type, name,
1691 					      &snd_usb_mixer_vol_tlv);
1692 		if (err < 0)
1693 			return err;
1694 	}
1695 
1696 	return 0;
1697 }
1698 
1699 static int snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface *mixer)
1700 {
1701 	char name[64];
1702 	unsigned int cmask;
1703 	int chan, err;
1704 	int num_outs = 0;
1705 	int offset = 0;
1706 
1707 	const unsigned int id = 0x40;
1708 	const int val_type = USB_MIXER_S16;
1709 	const int control = 1;
1710 
1711 	switch (mixer->chip->usb_id) {
1712 	case USB_ID(0x0763, 0x2030):
1713 		num_outs = 6;
1714 		offset = 0x3c;
1715 		/* { 0x3c, 0x43, 0x3e, 0x45, 0x40, 0x47 } */
1716 		break;
1717 	case USB_ID(0x0763, 0x2031):
1718 		num_outs = 8;
1719 		offset = 0x70;
1720 		/* { 0x70, 0x79, 0x72, 0x7b, 0x74, 0x7d, 0x76, 0x7f } */
1721 		break;
1722 	}
1723 
1724 	for (chan = 0; chan < num_outs; chan++) {
1725 		snprintf(name, sizeof(name),
1726 			 "Effect Return %d",
1727 			 chan + 1);
1728 
1729 		cmask = (chan == 0) ? 0 :
1730 			BIT(chan + (chan % 2) * num_outs - 1);
1731 		err = snd_create_std_mono_ctl_offset(mixer, id, control,
1732 						     cmask, val_type, offset, name,
1733 						     &snd_usb_mixer_vol_tlv);
1734 		if (err < 0)
1735 			return err;
1736 	}
1737 
1738 	return 0;
1739 }
1740 
1741 /* output gain knob selectively adjusts outputs as stereo pairs */
1742 /* reuses functions from FTU effect switch */
1743 static int snd_c400_knob_switch_info(struct snd_kcontrol *kcontrol,
1744 					struct snd_ctl_elem_info *uinfo)
1745 {
1746 	static const char *const texts[8] = {
1747 		"None", "1/2", "3/4", "1/2 3/4",
1748 		"5/6", "1/2 5/6", "3/4 5/6", "1/2 3/4 5/6"
1749 	};
1750 
1751 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1752 }
1753 
1754 static int snd_c400_create_knob_switch(struct usb_mixer_interface *mixer,
1755 	int validx, int bUnitID)
1756 {
1757 	static struct snd_kcontrol_new template = {
1758 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1759 		.name = "Output Gain Knob",
1760 		.index = 0,
1761 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1762 		.info = snd_c400_knob_switch_info,
1763 		.get = snd_ftu_eff_switch_get,
1764 		.put = snd_ftu_eff_switch_put
1765 	};
1766 	struct usb_mixer_elem_list *list;
1767 	int err;
1768 
1769 	err = add_single_ctl_with_resume(mixer, bUnitID,
1770 					 snd_ftu_eff_switch_update,
1771 					 &template, &list);
1772 	if (err < 0)
1773 		return err;
1774 	list->kctl->private_value = (validx << 8) | bUnitID;
1775 	snd_ftu_eff_switch_init(mixer, list->kctl);
1776 	return 0;
1777 }
1778 
1779 static int snd_c400_create_mixer(struct usb_mixer_interface *mixer)
1780 {
1781 	int err;
1782 
1783 	err = snd_c400_create_vol_ctls(mixer);
1784 	if (err < 0)
1785 		return err;
1786 
1787 	err = snd_c400_create_effect_vol_ctls(mixer);
1788 	if (err < 0)
1789 		return err;
1790 
1791 	err = snd_c400_create_effect_ret_vol_ctls(mixer);
1792 	if (err < 0)
1793 		return err;
1794 
1795 	err = snd_ftu_create_effect_switch(mixer, 2, 0x43);
1796 	if (err < 0)
1797 		return err;
1798 
1799 	err = snd_c400_create_effect_volume_ctl(mixer);
1800 	if (err < 0)
1801 		return err;
1802 
1803 	err = snd_c400_create_effect_duration_ctl(mixer);
1804 	if (err < 0)
1805 		return err;
1806 
1807 	err = snd_c400_create_effect_feedback_ctl(mixer);
1808 	if (err < 0)
1809 		return err;
1810 
1811 	err = snd_c400_create_knob_switch(mixer, 0x0900, 0x20);
1812 	if (err < 0)
1813 		return err;
1814 
1815 	return 0;
1816 }
1817 
1818 /*
1819  * The mixer units for Ebox-44 are corrupt, and even where they
1820  * are valid they presents mono controls as L and R channels of
1821  * stereo. So we provide a good mixer here.
1822  */
1823 static const struct std_mono_table ebox44_table[] = {
1824 	{
1825 		.unitid = 4,
1826 		.control = 1,
1827 		.cmask = 0x0,
1828 		.val_type = USB_MIXER_INV_BOOLEAN,
1829 		.name = "Headphone Playback Switch"
1830 	},
1831 	{
1832 		.unitid = 4,
1833 		.control = 2,
1834 		.cmask = 0x1,
1835 		.val_type = USB_MIXER_S16,
1836 		.name = "Headphone A Mix Playback Volume"
1837 	},
1838 	{
1839 		.unitid = 4,
1840 		.control = 2,
1841 		.cmask = 0x2,
1842 		.val_type = USB_MIXER_S16,
1843 		.name = "Headphone B Mix Playback Volume"
1844 	},
1845 
1846 	{
1847 		.unitid = 7,
1848 		.control = 1,
1849 		.cmask = 0x0,
1850 		.val_type = USB_MIXER_INV_BOOLEAN,
1851 		.name = "Output Playback Switch"
1852 	},
1853 	{
1854 		.unitid = 7,
1855 		.control = 2,
1856 		.cmask = 0x1,
1857 		.val_type = USB_MIXER_S16,
1858 		.name = "Output A Playback Volume"
1859 	},
1860 	{
1861 		.unitid = 7,
1862 		.control = 2,
1863 		.cmask = 0x2,
1864 		.val_type = USB_MIXER_S16,
1865 		.name = "Output B Playback Volume"
1866 	},
1867 
1868 	{
1869 		.unitid = 10,
1870 		.control = 1,
1871 		.cmask = 0x0,
1872 		.val_type = USB_MIXER_INV_BOOLEAN,
1873 		.name = "Input Capture Switch"
1874 	},
1875 	{
1876 		.unitid = 10,
1877 		.control = 2,
1878 		.cmask = 0x1,
1879 		.val_type = USB_MIXER_S16,
1880 		.name = "Input A Capture Volume"
1881 	},
1882 	{
1883 		.unitid = 10,
1884 		.control = 2,
1885 		.cmask = 0x2,
1886 		.val_type = USB_MIXER_S16,
1887 		.name = "Input B Capture Volume"
1888 	},
1889 
1890 	{}
1891 };
1892 
1893 /* Audio Advantage Micro II findings:
1894  *
1895  * Mapping spdif AES bits to vendor register.bit:
1896  * AES0: [0 0 0 0 2.3 2.2 2.1 2.0] - default 0x00
1897  * AES1: [3.3 3.2.3.1.3.0 2.7 2.6 2.5 2.4] - default: 0x01
1898  * AES2: [0 0 0 0 0 0 0 0]
1899  * AES3: [0 0 0 0 0 0 x 0] - 'x' bit is set basing on standard usb request
1900  *                           (UAC_EP_CS_ATTR_SAMPLE_RATE) for Audio Devices
1901  *
1902  * power on values:
1903  * r2: 0x10
1904  * r3: 0x20 (b7 is zeroed just before playback (except IEC61937) and set
1905  *           just after it to 0xa0, presumably it disables/mutes some analog
1906  *           parts when there is no audio.)
1907  * r9: 0x28
1908  *
1909  * Optical transmitter on/off:
1910  * vendor register.bit: 9.1
1911  * 0 - on (0x28 register value)
1912  * 1 - off (0x2a register value)
1913  *
1914  */
1915 static int snd_microii_spdif_info(struct snd_kcontrol *kcontrol,
1916 				  struct snd_ctl_elem_info *uinfo)
1917 {
1918 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1919 	uinfo->count = 1;
1920 	return 0;
1921 }
1922 
1923 static int snd_microii_spdif_default_get(struct snd_kcontrol *kcontrol,
1924 					 struct snd_ctl_elem_value *ucontrol)
1925 {
1926 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1927 	struct snd_usb_audio *chip = list->mixer->chip;
1928 	int err;
1929 	struct usb_interface *iface;
1930 	struct usb_host_interface *alts;
1931 	unsigned int ep;
1932 	unsigned char data[3];
1933 	int rate;
1934 
1935 	CLASS(snd_usb_lock, pm)(chip);
1936 	if (pm.err < 0)
1937 		return pm.err;
1938 
1939 	ucontrol->value.iec958.status[0] = kcontrol->private_value & 0xff;
1940 	ucontrol->value.iec958.status[1] = (kcontrol->private_value >> 8) & 0xff;
1941 	ucontrol->value.iec958.status[2] = 0x00;
1942 
1943 	/* use known values for that card: interface#1 altsetting#1 */
1944 	iface = usb_ifnum_to_if(chip->dev, 1);
1945 	if (!iface || iface->num_altsetting < 2)
1946 		return -EINVAL;
1947 	alts = &iface->altsetting[1];
1948 	if (get_iface_desc(alts)->bNumEndpoints < 1)
1949 		return -EINVAL;
1950 	ep = get_endpoint(alts, 0)->bEndpointAddress;
1951 
1952 	err = snd_usb_ctl_msg(chip->dev,
1953 			      usb_rcvctrlpipe(chip->dev, 0),
1954 			      UAC_GET_CUR,
1955 			      USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
1956 			      UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
1957 			      ep,
1958 			      data,
1959 			      sizeof(data));
1960 	if (err < 0)
1961 		return err;
1962 
1963 	rate = data[0] | (data[1] << 8) | (data[2] << 16);
1964 	ucontrol->value.iec958.status[3] = (rate == 48000) ?
1965 			IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100;
1966 
1967 	return 0;
1968 }
1969 
1970 static int snd_microii_spdif_default_update(struct usb_mixer_elem_list *list)
1971 {
1972 	struct snd_usb_audio *chip = list->mixer->chip;
1973 	unsigned int pval = list->kctl->private_value;
1974 	u8 reg;
1975 	int err;
1976 
1977 	CLASS(snd_usb_lock, pm)(chip);
1978 	if (pm.err < 0)
1979 		return pm.err;
1980 
1981 	reg = ((pval >> 4) & 0xf0) | (pval & 0x0f);
1982 	err = snd_usb_ctl_msg(chip->dev,
1983 			      usb_sndctrlpipe(chip->dev, 0),
1984 			      UAC_SET_CUR,
1985 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1986 			      reg,
1987 			      2,
1988 			      NULL,
1989 			      0);
1990 	if (err < 0)
1991 		return err;
1992 
1993 	reg = (pval & IEC958_AES0_NONAUDIO) ? 0xa0 : 0x20;
1994 	reg |= (pval >> 12) & 0x0f;
1995 	err = snd_usb_ctl_msg(chip->dev,
1996 			      usb_sndctrlpipe(chip->dev, 0),
1997 			      UAC_SET_CUR,
1998 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1999 			      reg,
2000 			      3,
2001 			      NULL,
2002 			      0);
2003 	return err;
2004 }
2005 
2006 static int snd_microii_spdif_default_put(struct snd_kcontrol *kcontrol,
2007 					 struct snd_ctl_elem_value *ucontrol)
2008 {
2009 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2010 	unsigned int pval, pval_old;
2011 	int err;
2012 
2013 	pval = kcontrol->private_value;
2014 	pval_old = pval;
2015 	pval &= 0xfffff0f0;
2016 	pval |= (ucontrol->value.iec958.status[1] & 0x0f) << 8;
2017 	pval |= (ucontrol->value.iec958.status[0] & 0x0f);
2018 
2019 	pval &= 0xffff0fff;
2020 	pval |= (ucontrol->value.iec958.status[1] & 0xf0) << 8;
2021 
2022 	/* The frequency bits in AES3 cannot be set via register access. */
2023 
2024 	/* Silently ignore any bits from the request that cannot be set. */
2025 
2026 	if (pval == pval_old)
2027 		return 0;
2028 
2029 	kcontrol->private_value = pval;
2030 	err = snd_microii_spdif_default_update(list);
2031 	return err < 0 ? err : 1;
2032 }
2033 
2034 static int snd_microii_spdif_mask_get(struct snd_kcontrol *kcontrol,
2035 				      struct snd_ctl_elem_value *ucontrol)
2036 {
2037 	ucontrol->value.iec958.status[0] = 0x0f;
2038 	ucontrol->value.iec958.status[1] = 0xff;
2039 	ucontrol->value.iec958.status[2] = 0x00;
2040 	ucontrol->value.iec958.status[3] = 0x00;
2041 
2042 	return 0;
2043 }
2044 
2045 static int snd_microii_spdif_switch_get(struct snd_kcontrol *kcontrol,
2046 					struct snd_ctl_elem_value *ucontrol)
2047 {
2048 	ucontrol->value.integer.value[0] = !(kcontrol->private_value & 0x02);
2049 
2050 	return 0;
2051 }
2052 
2053 static int snd_microii_spdif_switch_update(struct usb_mixer_elem_list *list)
2054 {
2055 	struct snd_usb_audio *chip = list->mixer->chip;
2056 	u8 reg = list->kctl->private_value;
2057 
2058 	CLASS(snd_usb_lock, pm)(chip);
2059 	if (pm.err < 0)
2060 		return pm.err;
2061 
2062 	return snd_usb_ctl_msg(chip->dev,
2063 			       usb_sndctrlpipe(chip->dev, 0),
2064 			       UAC_SET_CUR,
2065 			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2066 			       reg,
2067 			       9,
2068 			       NULL,
2069 			       0);
2070 }
2071 
2072 static int snd_microii_spdif_switch_put(struct snd_kcontrol *kcontrol,
2073 					struct snd_ctl_elem_value *ucontrol)
2074 {
2075 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2076 	u8 reg;
2077 	int err;
2078 
2079 	reg = ucontrol->value.integer.value[0] ? 0x28 : 0x2a;
2080 	if (reg == list->kctl->private_value)
2081 		return 0;
2082 
2083 	kcontrol->private_value = reg;
2084 	err = snd_microii_spdif_switch_update(list);
2085 	return err < 0 ? err : 1;
2086 }
2087 
2088 static const struct snd_kcontrol_new snd_microii_mixer_spdif[] = {
2089 	{
2090 		.iface =    SNDRV_CTL_ELEM_IFACE_PCM,
2091 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2092 		.info =     snd_microii_spdif_info,
2093 		.get =      snd_microii_spdif_default_get,
2094 		.put =      snd_microii_spdif_default_put,
2095 		.private_value = 0x00000100UL,/* reset value */
2096 	},
2097 	{
2098 		.access =   SNDRV_CTL_ELEM_ACCESS_READ,
2099 		.iface =    SNDRV_CTL_ELEM_IFACE_PCM,
2100 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
2101 		.info =     snd_microii_spdif_info,
2102 		.get =      snd_microii_spdif_mask_get,
2103 	},
2104 	{
2105 		.iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
2106 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2107 		.info =     snd_ctl_boolean_mono_info,
2108 		.get =      snd_microii_spdif_switch_get,
2109 		.put =      snd_microii_spdif_switch_put,
2110 		.private_value = 0x00000028UL,/* reset value */
2111 	}
2112 };
2113 
2114 static int snd_microii_controls_create(struct usb_mixer_interface *mixer)
2115 {
2116 	int err, i;
2117 	static const usb_mixer_elem_resume_func_t resume_funcs[] = {
2118 		snd_microii_spdif_default_update,
2119 		NULL,
2120 		snd_microii_spdif_switch_update
2121 	};
2122 
2123 	for (i = 0; i < ARRAY_SIZE(snd_microii_mixer_spdif); ++i) {
2124 		err = add_single_ctl_with_resume(mixer, 0,
2125 						 resume_funcs[i],
2126 						 &snd_microii_mixer_spdif[i],
2127 						 NULL);
2128 		if (err < 0)
2129 			return err;
2130 	}
2131 
2132 	return 0;
2133 }
2134 
2135 /* Creative Sound Blaster E1 */
2136 
2137 static int snd_soundblaster_e1_switch_get(struct snd_kcontrol *kcontrol,
2138 					  struct snd_ctl_elem_value *ucontrol)
2139 {
2140 	ucontrol->value.integer.value[0] = kcontrol->private_value;
2141 	return 0;
2142 }
2143 
2144 static int snd_soundblaster_e1_switch_update(struct usb_mixer_interface *mixer,
2145 					     unsigned char state)
2146 {
2147 	struct snd_usb_audio *chip = mixer->chip;
2148 	unsigned char buff[2];
2149 
2150 	buff[0] = 0x02;
2151 	buff[1] = state ? 0x02 : 0x00;
2152 
2153 	CLASS(snd_usb_lock, pm)(chip);
2154 	if (pm.err < 0)
2155 		return pm.err;
2156 	return snd_usb_ctl_msg(chip->dev,
2157 			       usb_sndctrlpipe(chip->dev, 0), HID_REQ_SET_REPORT,
2158 			       USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
2159 			       0x0202, 3, buff, 2);
2160 }
2161 
2162 static int snd_soundblaster_e1_switch_put(struct snd_kcontrol *kcontrol,
2163 					  struct snd_ctl_elem_value *ucontrol)
2164 {
2165 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2166 	unsigned char value = !!ucontrol->value.integer.value[0];
2167 	unsigned long old_pval = kcontrol->private_value;
2168 	int err;
2169 
2170 	if (kcontrol->private_value == value)
2171 		return 0;
2172 	kcontrol->private_value = value;
2173 	err = snd_soundblaster_e1_switch_update(list->mixer, value);
2174 	if (err < 0) {
2175 		kcontrol->private_value = old_pval;
2176 		return err;
2177 	}
2178 	return 1;
2179 }
2180 
2181 static int snd_soundblaster_e1_switch_resume(struct usb_mixer_elem_list *list)
2182 {
2183 	return snd_soundblaster_e1_switch_update(list->mixer,
2184 						 list->kctl->private_value);
2185 }
2186 
2187 static int snd_soundblaster_e1_switch_info(struct snd_kcontrol *kcontrol,
2188 					   struct snd_ctl_elem_info *uinfo)
2189 {
2190 	static const char *const texts[2] = {
2191 		"Mic", "Aux"
2192 	};
2193 
2194 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
2195 }
2196 
2197 static const struct snd_kcontrol_new snd_soundblaster_e1_input_switch = {
2198 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2199 	.name = "Input Source",
2200 	.info = snd_soundblaster_e1_switch_info,
2201 	.get = snd_soundblaster_e1_switch_get,
2202 	.put = snd_soundblaster_e1_switch_put,
2203 	.private_value = 0,
2204 };
2205 
2206 static int snd_soundblaster_e1_switch_create(struct usb_mixer_interface *mixer)
2207 {
2208 	return add_single_ctl_with_resume(mixer, 0,
2209 					  snd_soundblaster_e1_switch_resume,
2210 					  &snd_soundblaster_e1_input_switch,
2211 					  NULL);
2212 }
2213 
2214 /*
2215  * Dell WD15 dock jack detection
2216  *
2217  * The WD15 contains an ALC4020 USB audio controller and ALC3263 audio codec
2218  * from Realtek. It is a UAC 1 device, and UAC 1 does not support jack
2219  * detection. Instead, jack detection works by sending HD Audio commands over
2220  * vendor-type USB messages.
2221  */
2222 
2223 #define HDA_VERB_CMD(V, N, D) (((N) << 20) | ((V) << 8) | (D))
2224 
2225 #define REALTEK_HDA_VALUE 0x0038
2226 
2227 #define REALTEK_HDA_SET		62
2228 #define REALTEK_MANUAL_MODE	72
2229 #define REALTEK_HDA_GET_OUT	88
2230 #define REALTEK_HDA_GET_IN	89
2231 
2232 #define REALTEK_AUDIO_FUNCTION_GROUP	0x01
2233 #define REALTEK_LINE1			0x1a
2234 #define REALTEK_VENDOR_REGISTERS	0x20
2235 #define REALTEK_HP_OUT			0x21
2236 
2237 #define REALTEK_CBJ_CTRL2 0x50
2238 
2239 #define REALTEK_JACK_INTERRUPT_NODE 5
2240 
2241 #define REALTEK_MIC_FLAG 0x100
2242 
2243 static int realtek_hda_set(struct snd_usb_audio *chip, u32 cmd)
2244 {
2245 	struct usb_device *dev = chip->dev;
2246 	__be32 buf = cpu_to_be32(cmd);
2247 
2248 	return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), REALTEK_HDA_SET,
2249 			       USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_OUT,
2250 			       REALTEK_HDA_VALUE, 0, &buf, sizeof(buf));
2251 }
2252 
2253 static int realtek_hda_get(struct snd_usb_audio *chip, u32 cmd, u32 *value)
2254 {
2255 	struct usb_device *dev = chip->dev;
2256 	int err;
2257 	__be32 buf = cpu_to_be32(cmd);
2258 
2259 	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), REALTEK_HDA_GET_OUT,
2260 			      USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_OUT,
2261 			      REALTEK_HDA_VALUE, 0, &buf, sizeof(buf));
2262 	if (err < 0)
2263 		return err;
2264 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), REALTEK_HDA_GET_IN,
2265 			      USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_IN,
2266 			      REALTEK_HDA_VALUE, 0, &buf, sizeof(buf));
2267 	if (err < 0)
2268 		return err;
2269 
2270 	*value = be32_to_cpu(buf);
2271 	return 0;
2272 }
2273 
2274 static int realtek_ctl_connector_get(struct snd_kcontrol *kcontrol,
2275 				     struct snd_ctl_elem_value *ucontrol)
2276 {
2277 	struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol);
2278 	struct snd_usb_audio *chip = cval->head.mixer->chip;
2279 	u32 pv = kcontrol->private_value;
2280 	u32 node_id = pv & 0xff;
2281 	u32 sense;
2282 	u32 cbj_ctrl2;
2283 	bool presence;
2284 	int err;
2285 
2286 	CLASS(snd_usb_lock, pm)(chip);
2287 	if (pm.err < 0)
2288 		return pm.err;
2289 	err = realtek_hda_get(chip,
2290 			      HDA_VERB_CMD(AC_VERB_GET_PIN_SENSE, node_id, 0),
2291 			      &sense);
2292 	if (err < 0)
2293 		return err;
2294 	if (pv & REALTEK_MIC_FLAG) {
2295 		err = realtek_hda_set(chip,
2296 				      HDA_VERB_CMD(AC_VERB_SET_COEF_INDEX,
2297 						   REALTEK_VENDOR_REGISTERS,
2298 						   REALTEK_CBJ_CTRL2));
2299 		if (err < 0)
2300 			return err;
2301 		err = realtek_hda_get(chip,
2302 				      HDA_VERB_CMD(AC_VERB_GET_PROC_COEF,
2303 						   REALTEK_VENDOR_REGISTERS, 0),
2304 				      &cbj_ctrl2);
2305 		if (err < 0)
2306 			return err;
2307 	}
2308 
2309 	presence = sense & AC_PINSENSE_PRESENCE;
2310 	if (pv & REALTEK_MIC_FLAG)
2311 		presence = presence && (cbj_ctrl2 & 0x0070) == 0x0070;
2312 	ucontrol->value.integer.value[0] = presence;
2313 	return 0;
2314 }
2315 
2316 static const struct snd_kcontrol_new realtek_connector_ctl_ro = {
2317 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
2318 	.name = "", /* will be filled later manually */
2319 	.access = SNDRV_CTL_ELEM_ACCESS_READ,
2320 	.info = snd_ctl_boolean_mono_info,
2321 	.get = realtek_ctl_connector_get,
2322 };
2323 
2324 static int realtek_resume_jack(struct usb_mixer_elem_list *list)
2325 {
2326 	snd_ctl_notify(list->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2327 		       &list->kctl->id);
2328 	return 0;
2329 }
2330 
2331 static int realtek_add_jack(struct usb_mixer_interface *mixer,
2332 			    char *name, u32 val, int unitid,
2333 			    const struct snd_kcontrol_new *kctl_new)
2334 {
2335 	struct usb_mixer_elem_info *cval;
2336 	struct snd_kcontrol *kctl;
2337 
2338 	cval = kzalloc_obj(*cval);
2339 	if (!cval)
2340 		return -ENOMEM;
2341 	snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
2342 	cval->head.resume = realtek_resume_jack;
2343 	cval->val_type = USB_MIXER_BOOLEAN;
2344 	cval->channels = 1;
2345 	cval->min = 0;
2346 	cval->max = 1;
2347 	kctl = snd_ctl_new1(kctl_new, cval);
2348 	if (!kctl) {
2349 		kfree(cval);
2350 		return -ENOMEM;
2351 	}
2352 	kctl->private_value = val;
2353 	strscpy(kctl->id.name, name, sizeof(kctl->id.name));
2354 	kctl->private_free = snd_usb_mixer_elem_free;
2355 	return snd_usb_mixer_add_control(&cval->head, kctl);
2356 }
2357 
2358 static int dell_dock_mixer_create(struct usb_mixer_interface *mixer)
2359 {
2360 	int err;
2361 	struct usb_device *dev = mixer->chip->dev;
2362 
2363 	/* Power down the audio codec to avoid loud pops in the next step. */
2364 	realtek_hda_set(mixer->chip,
2365 			HDA_VERB_CMD(AC_VERB_SET_POWER_STATE,
2366 				     REALTEK_AUDIO_FUNCTION_GROUP,
2367 				     AC_PWRST_D3));
2368 
2369 	/*
2370 	 * Turn off 'manual mode' in case it was enabled. This removes the need
2371 	 * to power cycle the dock after it was attached to a Windows machine.
2372 	 */
2373 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), REALTEK_MANUAL_MODE,
2374 			USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_OUT,
2375 			0, 0, NULL, 0);
2376 
2377 	err = realtek_add_jack(mixer, "Line Out Jack", REALTEK_LINE1,
2378 			       REALTEK_JACK_INTERRUPT_NODE,
2379 			       &realtek_connector_ctl_ro);
2380 	if (err < 0)
2381 		return err;
2382 	err = realtek_add_jack(mixer, "Headphone Jack", REALTEK_HP_OUT,
2383 			       REALTEK_JACK_INTERRUPT_NODE,
2384 			       &realtek_connector_ctl_ro);
2385 	if (err < 0)
2386 		return err;
2387 	err = realtek_add_jack(mixer, "Headset Mic Jack",
2388 			       REALTEK_HP_OUT | REALTEK_MIC_FLAG,
2389 			       REALTEK_JACK_INTERRUPT_NODE,
2390 			       &realtek_connector_ctl_ro);
2391 	if (err < 0)
2392 		return err;
2393 	return 0;
2394 }
2395 
2396 static void dell_dock_init_vol(struct usb_mixer_interface *mixer, int ch, int id)
2397 {
2398 	struct snd_usb_audio *chip = mixer->chip;
2399 	u16 buf = 0;
2400 
2401 	snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
2402 			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
2403 			(UAC_FU_VOLUME << 8) | ch,
2404 			snd_usb_ctrl_intf(mixer->hostif) | (id << 8),
2405 			&buf, 2);
2406 }
2407 
2408 static int dell_dock_mixer_init(struct usb_mixer_interface *mixer)
2409 {
2410 	/* fix to 0dB playback volumes */
2411 	dell_dock_init_vol(mixer, 1, 16);
2412 	dell_dock_init_vol(mixer, 2, 16);
2413 	dell_dock_init_vol(mixer, 1, 19);
2414 	dell_dock_init_vol(mixer, 2, 19);
2415 	return 0;
2416 }
2417 
2418 /*
2419  * HP Thunderbolt Dock G2 jack detection
2420  *
2421  * Similar to the Dell WD15/WD19, but with different commands.
2422  */
2423 
2424 #define HP_DOCK_JACK_INTERRUPT_NODE	7
2425 
2426 #define HP_DOCK_GET			37
2427 
2428 #define HP_DOCK_JACK_PRESENCE		0xffb8
2429 #define HP_DOCK_JACK_PRESENCE_BIT	BIT(2)
2430 
2431 #define HP_DOCK_MIC_SENSE		0xf753
2432 #define HP_DOCK_MIC_SENSE_COMPLETE_BIT	BIT(4)
2433 
2434 #define HP_DOCK_MIC_SENSE_MASK		(BIT(2) | BIT(1) | BIT(0))
2435 /* #define HP_DOCK_MIC_SENSE_PRESENT	0x2 */
2436 #define HP_DOCK_MIC_SENSE_NOT_PRESENT	0x4
2437 
2438 static int hp_dock_ctl_connector_get(struct snd_kcontrol *kcontrol,
2439 				     struct snd_ctl_elem_value *ucontrol)
2440 {
2441 	struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol);
2442 	struct snd_usb_audio *chip = cval->head.mixer->chip;
2443 	u32 pv = kcontrol->private_value;
2444 	bool presence;
2445 	int err;
2446 	u8 buf;
2447 
2448 	CLASS(snd_usb_lock, pm)(chip);
2449 	if (pm.err < 0)
2450 		return pm.err;
2451 
2452 	err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
2453 		       HP_DOCK_GET,
2454 		       USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_IN,
2455 		       0, HP_DOCK_JACK_PRESENCE, &buf, sizeof(buf));
2456 	if (err < 0)
2457 		return err;
2458 
2459 	presence = !(buf & HP_DOCK_JACK_PRESENCE_BIT);
2460 
2461 	if (pv && presence) {
2462 		for (int i = 0; i < 20; i++) {
2463 			err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
2464 			       HP_DOCK_GET,
2465 			       USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_IN,
2466 			       0, HP_DOCK_MIC_SENSE, &buf, sizeof(buf));
2467 			if (err < 0)
2468 				return err;
2469 
2470 			/* Mic sense is complete, we have a result. */
2471 			if (buf & HP_DOCK_MIC_SENSE_COMPLETE_BIT)
2472 				break;
2473 
2474 			msleep(100);
2475 		}
2476 
2477 		/*
2478 		 * If we reach the retry limit without mic sense having
2479 		 * completed, buf will contain HP_DOCK_MIC_SENSE_PRESENT,
2480 		 * thus presence remains true even when detection fails.
2481 		 */
2482 		if ((buf & HP_DOCK_MIC_SENSE_MASK) == HP_DOCK_MIC_SENSE_NOT_PRESENT)
2483 			presence = false;
2484 	}
2485 	ucontrol->value.integer.value[0] = presence;
2486 	return 0;
2487 }
2488 
2489 static const struct snd_kcontrol_new hp_dock_connector_ctl_ro = {
2490 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
2491 	.name = "", /* will be filled later manually */
2492 	.access = SNDRV_CTL_ELEM_ACCESS_READ,
2493 	.info = snd_ctl_boolean_mono_info,
2494 	.get = hp_dock_ctl_connector_get,
2495 };
2496 
2497 static int hp_dock_mixer_create(struct usb_mixer_interface *mixer)
2498 {
2499 	int err;
2500 
2501 	err = realtek_add_jack(mixer, "Headsets Playback Jack", 0,
2502 			       HP_DOCK_JACK_INTERRUPT_NODE,
2503 			       &hp_dock_connector_ctl_ro);
2504 	if (err < 0)
2505 		return err;
2506 
2507 	err = realtek_add_jack(mixer, "Headset Capture Jack", 1,
2508 			       HP_DOCK_JACK_INTERRUPT_NODE,
2509 			       &hp_dock_connector_ctl_ro);
2510 	if (err < 0)
2511 		return err;
2512 
2513 	return 0;
2514 }
2515 
2516 
2517 /* RME Class Compliant device quirks */
2518 
2519 #define SND_RME_GET_STATUS1			23
2520 #define SND_RME_GET_CURRENT_FREQ		17
2521 #define SND_RME_CLK_SYSTEM_SHIFT		16
2522 #define SND_RME_CLK_SYSTEM_MASK			0x1f
2523 #define SND_RME_CLK_AES_SHIFT			8
2524 #define SND_RME_CLK_SPDIF_SHIFT			12
2525 #define SND_RME_CLK_AES_SPDIF_MASK		0xf
2526 #define SND_RME_CLK_SYNC_SHIFT			6
2527 #define SND_RME_CLK_SYNC_MASK			0x3
2528 #define SND_RME_CLK_FREQMUL_SHIFT		18
2529 #define SND_RME_CLK_FREQMUL_MASK		0x7
2530 #define SND_RME_CLK_SYSTEM(x) \
2531 	(((x) >> SND_RME_CLK_SYSTEM_SHIFT) & SND_RME_CLK_SYSTEM_MASK)
2532 #define SND_RME_CLK_AES(x) \
2533 	(((x) >> SND_RME_CLK_AES_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
2534 #define SND_RME_CLK_SPDIF(x) \
2535 	(((x) >> SND_RME_CLK_SPDIF_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
2536 #define SND_RME_CLK_SYNC(x) \
2537 	(((x) >> SND_RME_CLK_SYNC_SHIFT) & SND_RME_CLK_SYNC_MASK)
2538 #define SND_RME_CLK_FREQMUL(x) \
2539 	(((x) >> SND_RME_CLK_FREQMUL_SHIFT) & SND_RME_CLK_FREQMUL_MASK)
2540 #define SND_RME_CLK_AES_LOCK			0x1
2541 #define SND_RME_CLK_AES_SYNC			0x4
2542 #define SND_RME_CLK_SPDIF_LOCK			0x2
2543 #define SND_RME_CLK_SPDIF_SYNC			0x8
2544 #define SND_RME_SPDIF_IF_SHIFT			4
2545 #define SND_RME_SPDIF_FORMAT_SHIFT		5
2546 #define SND_RME_BINARY_MASK			0x1
2547 #define SND_RME_SPDIF_IF(x) \
2548 	(((x) >> SND_RME_SPDIF_IF_SHIFT) & SND_RME_BINARY_MASK)
2549 #define SND_RME_SPDIF_FORMAT(x) \
2550 	(((x) >> SND_RME_SPDIF_FORMAT_SHIFT) & SND_RME_BINARY_MASK)
2551 
2552 static const u32 snd_rme_rate_table[] = {
2553 	32000, 44100, 48000, 50000,
2554 	64000, 88200, 96000, 100000,
2555 	128000, 176400, 192000, 200000,
2556 	256000,	352800, 384000, 400000,
2557 	512000, 705600, 768000, 800000
2558 };
2559 
2560 /* maximum number of items for AES and S/PDIF rates for above table */
2561 #define SND_RME_RATE_IDX_AES_SPDIF_NUM		12
2562 
2563 enum snd_rme_domain {
2564 	SND_RME_DOMAIN_SYSTEM,
2565 	SND_RME_DOMAIN_AES,
2566 	SND_RME_DOMAIN_SPDIF
2567 };
2568 
2569 enum snd_rme_clock_status {
2570 	SND_RME_CLOCK_NOLOCK,
2571 	SND_RME_CLOCK_LOCK,
2572 	SND_RME_CLOCK_SYNC
2573 };
2574 
2575 static int snd_rme_read_value(struct snd_usb_audio *chip,
2576 			      unsigned int item,
2577 			      u32 *value)
2578 {
2579 	struct usb_device *dev = chip->dev;
2580 	int err;
2581 
2582 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
2583 			      item,
2584 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2585 			      0, 0,
2586 			      value, sizeof(*value));
2587 	if (err < 0)
2588 		dev_err(&dev->dev,
2589 			"unable to issue vendor read request %d (ret = %d)",
2590 			item, err);
2591 	return err;
2592 }
2593 
2594 static int snd_rme_get_status1(struct snd_kcontrol *kcontrol,
2595 			       u32 *status1)
2596 {
2597 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2598 	struct snd_usb_audio *chip = list->mixer->chip;
2599 
2600 	*status1 = 0;
2601 	CLASS(snd_usb_lock, pm)(chip);
2602 	if (pm.err < 0)
2603 		return pm.err;
2604 	return snd_rme_read_value(chip, SND_RME_GET_STATUS1, status1);
2605 }
2606 
2607 static int snd_rme_rate_get(struct snd_kcontrol *kcontrol,
2608 			    struct snd_ctl_elem_value *ucontrol)
2609 {
2610 	u32 status1;
2611 	u32 rate = 0;
2612 	int idx;
2613 	int err;
2614 
2615 	err = snd_rme_get_status1(kcontrol, &status1);
2616 	if (err < 0)
2617 		return err;
2618 	switch (kcontrol->private_value) {
2619 	case SND_RME_DOMAIN_SYSTEM:
2620 		idx = SND_RME_CLK_SYSTEM(status1);
2621 		if (idx < ARRAY_SIZE(snd_rme_rate_table))
2622 			rate = snd_rme_rate_table[idx];
2623 		break;
2624 	case SND_RME_DOMAIN_AES:
2625 		idx = SND_RME_CLK_AES(status1);
2626 		if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
2627 			rate = snd_rme_rate_table[idx];
2628 		break;
2629 	case SND_RME_DOMAIN_SPDIF:
2630 		idx = SND_RME_CLK_SPDIF(status1);
2631 		if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
2632 			rate = snd_rme_rate_table[idx];
2633 		break;
2634 	default:
2635 		return -EINVAL;
2636 	}
2637 	ucontrol->value.integer.value[0] = rate;
2638 	return 0;
2639 }
2640 
2641 static int snd_rme_sync_state_get(struct snd_kcontrol *kcontrol,
2642 				  struct snd_ctl_elem_value *ucontrol)
2643 {
2644 	u32 status1;
2645 	int idx = SND_RME_CLOCK_NOLOCK;
2646 	int err;
2647 
2648 	err = snd_rme_get_status1(kcontrol, &status1);
2649 	if (err < 0)
2650 		return err;
2651 	switch (kcontrol->private_value) {
2652 	case SND_RME_DOMAIN_AES:  /* AES */
2653 		if (status1 & SND_RME_CLK_AES_SYNC)
2654 			idx = SND_RME_CLOCK_SYNC;
2655 		else if (status1 & SND_RME_CLK_AES_LOCK)
2656 			idx = SND_RME_CLOCK_LOCK;
2657 		break;
2658 	case SND_RME_DOMAIN_SPDIF:  /* SPDIF */
2659 		if (status1 & SND_RME_CLK_SPDIF_SYNC)
2660 			idx = SND_RME_CLOCK_SYNC;
2661 		else if (status1 & SND_RME_CLK_SPDIF_LOCK)
2662 			idx = SND_RME_CLOCK_LOCK;
2663 		break;
2664 	default:
2665 		return -EINVAL;
2666 	}
2667 	ucontrol->value.enumerated.item[0] = idx;
2668 	return 0;
2669 }
2670 
2671 static int snd_rme_spdif_if_get(struct snd_kcontrol *kcontrol,
2672 				struct snd_ctl_elem_value *ucontrol)
2673 {
2674 	u32 status1;
2675 	int err;
2676 
2677 	err = snd_rme_get_status1(kcontrol, &status1);
2678 	if (err < 0)
2679 		return err;
2680 	ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_IF(status1);
2681 	return 0;
2682 }
2683 
2684 static int snd_rme_spdif_format_get(struct snd_kcontrol *kcontrol,
2685 				    struct snd_ctl_elem_value *ucontrol)
2686 {
2687 	u32 status1;
2688 	int err;
2689 
2690 	err = snd_rme_get_status1(kcontrol, &status1);
2691 	if (err < 0)
2692 		return err;
2693 	ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_FORMAT(status1);
2694 	return 0;
2695 }
2696 
2697 static int snd_rme_sync_source_get(struct snd_kcontrol *kcontrol,
2698 				   struct snd_ctl_elem_value *ucontrol)
2699 {
2700 	u32 status1;
2701 	int err;
2702 
2703 	err = snd_rme_get_status1(kcontrol, &status1);
2704 	if (err < 0)
2705 		return err;
2706 	ucontrol->value.enumerated.item[0] = SND_RME_CLK_SYNC(status1);
2707 	return 0;
2708 }
2709 
2710 static int snd_rme_current_freq_get(struct snd_kcontrol *kcontrol,
2711 				    struct snd_ctl_elem_value *ucontrol)
2712 {
2713 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2714 	struct snd_usb_audio *chip = list->mixer->chip;
2715 	u32 status1;
2716 	const u64 num = 104857600000000ULL;
2717 	u32 den;
2718 	unsigned int freq;
2719 	int err;
2720 
2721 	CLASS(snd_usb_lock, pm)(chip);
2722 	if (pm.err < 0)
2723 		return pm.err;
2724 	err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, &status1);
2725 	if (err < 0)
2726 		return err;
2727 	err = snd_rme_read_value(chip, SND_RME_GET_CURRENT_FREQ, &den);
2728 	if (err < 0)
2729 		return err;
2730 	freq = (den == 0) ? 0 : div64_u64(num, den);
2731 	freq <<= SND_RME_CLK_FREQMUL(status1);
2732 	ucontrol->value.integer.value[0] = freq;
2733 	return 0;
2734 }
2735 
2736 static int snd_rme_rate_info(struct snd_kcontrol *kcontrol,
2737 			     struct snd_ctl_elem_info *uinfo)
2738 {
2739 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2740 	uinfo->count = 1;
2741 	switch (kcontrol->private_value) {
2742 	case SND_RME_DOMAIN_SYSTEM:
2743 		uinfo->value.integer.min = 32000;
2744 		uinfo->value.integer.max = 800000;
2745 		break;
2746 	case SND_RME_DOMAIN_AES:
2747 	case SND_RME_DOMAIN_SPDIF:
2748 	default:
2749 		uinfo->value.integer.min = 0;
2750 		uinfo->value.integer.max = 200000;
2751 	}
2752 	uinfo->value.integer.step = 0;
2753 	return 0;
2754 }
2755 
2756 static int snd_rme_sync_state_info(struct snd_kcontrol *kcontrol,
2757 				   struct snd_ctl_elem_info *uinfo)
2758 {
2759 	static const char *const sync_states[] = {
2760 		"No Lock", "Lock", "Sync"
2761 	};
2762 
2763 	return snd_ctl_enum_info(uinfo, 1,
2764 				 ARRAY_SIZE(sync_states), sync_states);
2765 }
2766 
2767 static int snd_rme_spdif_if_info(struct snd_kcontrol *kcontrol,
2768 				 struct snd_ctl_elem_info *uinfo)
2769 {
2770 	static const char *const spdif_if[] = {
2771 		"Coaxial", "Optical"
2772 	};
2773 
2774 	return snd_ctl_enum_info(uinfo, 1,
2775 				 ARRAY_SIZE(spdif_if), spdif_if);
2776 }
2777 
2778 static int snd_rme_spdif_format_info(struct snd_kcontrol *kcontrol,
2779 				     struct snd_ctl_elem_info *uinfo)
2780 {
2781 	static const char *const optical_type[] = {
2782 		"Consumer", "Professional"
2783 	};
2784 
2785 	return snd_ctl_enum_info(uinfo, 1,
2786 				 ARRAY_SIZE(optical_type), optical_type);
2787 }
2788 
2789 static int snd_rme_sync_source_info(struct snd_kcontrol *kcontrol,
2790 				    struct snd_ctl_elem_info *uinfo)
2791 {
2792 	static const char *const sync_sources[] = {
2793 		"Internal", "AES", "SPDIF", "Internal"
2794 	};
2795 
2796 	return snd_ctl_enum_info(uinfo, 1,
2797 				 ARRAY_SIZE(sync_sources), sync_sources);
2798 }
2799 
2800 static const struct snd_kcontrol_new snd_rme_controls[] = {
2801 	{
2802 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2803 		.name = "AES Rate",
2804 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2805 		.info = snd_rme_rate_info,
2806 		.get = snd_rme_rate_get,
2807 		.private_value = SND_RME_DOMAIN_AES
2808 	},
2809 	{
2810 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2811 		.name = "AES Sync",
2812 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2813 		.info = snd_rme_sync_state_info,
2814 		.get = snd_rme_sync_state_get,
2815 		.private_value = SND_RME_DOMAIN_AES
2816 	},
2817 	{
2818 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2819 		.name = "SPDIF Rate",
2820 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2821 		.info = snd_rme_rate_info,
2822 		.get = snd_rme_rate_get,
2823 		.private_value = SND_RME_DOMAIN_SPDIF
2824 	},
2825 	{
2826 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2827 		.name = "SPDIF Sync",
2828 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2829 		.info = snd_rme_sync_state_info,
2830 		.get = snd_rme_sync_state_get,
2831 		.private_value = SND_RME_DOMAIN_SPDIF
2832 	},
2833 	{
2834 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2835 		.name = "SPDIF Interface",
2836 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2837 		.info = snd_rme_spdif_if_info,
2838 		.get = snd_rme_spdif_if_get,
2839 	},
2840 	{
2841 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2842 		.name = "SPDIF Format",
2843 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2844 		.info = snd_rme_spdif_format_info,
2845 		.get = snd_rme_spdif_format_get,
2846 	},
2847 	{
2848 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2849 		.name = "Sync Source",
2850 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2851 		.info = snd_rme_sync_source_info,
2852 		.get = snd_rme_sync_source_get
2853 	},
2854 	{
2855 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2856 		.name = "System Rate",
2857 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2858 		.info = snd_rme_rate_info,
2859 		.get = snd_rme_rate_get,
2860 		.private_value = SND_RME_DOMAIN_SYSTEM
2861 	},
2862 	{
2863 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2864 		.name = "Current Frequency",
2865 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2866 		.info = snd_rme_rate_info,
2867 		.get = snd_rme_current_freq_get
2868 	}
2869 };
2870 
2871 static int snd_rme_controls_create(struct usb_mixer_interface *mixer)
2872 {
2873 	int err, i;
2874 
2875 	for (i = 0; i < ARRAY_SIZE(snd_rme_controls); ++i) {
2876 		err = add_single_ctl_with_resume(mixer, 0,
2877 						 NULL,
2878 						 &snd_rme_controls[i],
2879 						 NULL);
2880 		if (err < 0)
2881 			return err;
2882 	}
2883 
2884 	return 0;
2885 }
2886 
2887 /*
2888  * RME Babyface Pro (FS)
2889  *
2890  * These devices exposes a couple of DSP functions via request to EP0.
2891  * Switches are available via control registers, while routing is controlled
2892  * by controlling the volume on each possible crossing point.
2893  * Volume control is linear, from -inf (dec. 0) to +6dB (dec. 65536) with
2894  * 0dB being at dec. 32768.
2895  */
2896 enum {
2897 	SND_BBFPRO_CTL_REG1 = 0,
2898 	SND_BBFPRO_CTL_REG2
2899 };
2900 
2901 #define SND_BBFPRO_CTL_REG_MASK 1
2902 #define SND_BBFPRO_CTL_IDX_MASK 0xff
2903 #define SND_BBFPRO_CTL_IDX_SHIFT 1
2904 #define SND_BBFPRO_CTL_VAL_MASK 1
2905 #define SND_BBFPRO_CTL_VAL_SHIFT 9
2906 #define SND_BBFPRO_CTL_REG1_CLK_MASTER 0
2907 #define SND_BBFPRO_CTL_REG1_CLK_OPTICAL 1
2908 #define SND_BBFPRO_CTL_REG1_SPDIF_PRO 7
2909 #define SND_BBFPRO_CTL_REG1_SPDIF_EMPH 8
2910 #define SND_BBFPRO_CTL_REG1_SPDIF_OPTICAL 10
2911 #define SND_BBFPRO_CTL_REG2_48V_AN1 0
2912 #define SND_BBFPRO_CTL_REG2_48V_AN2 1
2913 #define SND_BBFPRO_CTL_REG2_SENS_IN3 2
2914 #define SND_BBFPRO_CTL_REG2_SENS_IN4 3
2915 #define SND_BBFPRO_CTL_REG2_PAD_AN1 4
2916 #define SND_BBFPRO_CTL_REG2_PAD_AN2 5
2917 
2918 #define SND_BBFPRO_MIXER_MAIN_OUT_CH_OFFSET 992
2919 #define SND_BBFPRO_MIXER_IDX_MASK 0x3ff
2920 #define SND_BBFPRO_MIXER_VAL_MASK 0x3ffff
2921 #define SND_BBFPRO_MIXER_VAL_SHIFT 9
2922 #define SND_BBFPRO_MIXER_VAL_MIN 0 // -inf
2923 #define SND_BBFPRO_MIXER_VAL_MAX 65536 // +6dB
2924 
2925 #define SND_BBFPRO_GAIN_CHANNEL_MASK 0x03
2926 #define SND_BBFPRO_GAIN_CHANNEL_SHIFT 7
2927 #define SND_BBFPRO_GAIN_VAL_MASK 0x7f
2928 #define SND_BBFPRO_GAIN_VAL_MIN 0
2929 #define SND_BBFPRO_GAIN_VAL_MIC_MAX 65
2930 #define SND_BBFPRO_GAIN_VAL_LINE_MAX 18 // 9db in 0.5db incraments
2931 
2932 #define SND_BBFPRO_USBREQ_CTL_REG1 0x10
2933 #define SND_BBFPRO_USBREQ_CTL_REG2 0x17
2934 #define SND_BBFPRO_USBREQ_GAIN 0x1a
2935 #define SND_BBFPRO_USBREQ_MIXER 0x12
2936 
2937 static int snd_bbfpro_ctl_update(struct usb_mixer_interface *mixer, u8 reg,
2938 				 u8 index, u8 value)
2939 {
2940 	u16 usb_req, usb_idx, usb_val;
2941 	struct snd_usb_audio *chip = mixer->chip;
2942 
2943 	CLASS(snd_usb_lock, pm)(chip);
2944 	if (pm.err < 0)
2945 		return pm.err;
2946 
2947 	if (reg == SND_BBFPRO_CTL_REG1) {
2948 		usb_req = SND_BBFPRO_USBREQ_CTL_REG1;
2949 		if (index == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) {
2950 			usb_idx = 3;
2951 			usb_val = value ? 3 : 0;
2952 		} else {
2953 			usb_idx = BIT(index);
2954 			usb_val = value ? usb_idx : 0;
2955 		}
2956 	} else {
2957 		usb_req = SND_BBFPRO_USBREQ_CTL_REG2;
2958 		usb_idx = BIT(index);
2959 		usb_val = value ? usb_idx : 0;
2960 	}
2961 
2962 	return snd_usb_ctl_msg(chip->dev,
2963 			       usb_sndctrlpipe(chip->dev, 0), usb_req,
2964 			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2965 			       usb_val, usb_idx, NULL, 0);
2966 }
2967 
2968 static int snd_bbfpro_ctl_get(struct snd_kcontrol *kcontrol,
2969 			      struct snd_ctl_elem_value *ucontrol)
2970 {
2971 	u8 reg, idx, val;
2972 	int pv;
2973 
2974 	pv = kcontrol->private_value;
2975 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2976 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2977 	val = kcontrol->private_value >> SND_BBFPRO_CTL_VAL_SHIFT;
2978 
2979 	if ((reg == SND_BBFPRO_CTL_REG1 &&
2980 	     idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) ||
2981 	    (reg == SND_BBFPRO_CTL_REG2 &&
2982 	    (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2983 	     idx == SND_BBFPRO_CTL_REG2_SENS_IN4))) {
2984 		ucontrol->value.enumerated.item[0] = val;
2985 	} else {
2986 		ucontrol->value.integer.value[0] = val;
2987 	}
2988 	return 0;
2989 }
2990 
2991 static int snd_bbfpro_ctl_info(struct snd_kcontrol *kcontrol,
2992 			       struct snd_ctl_elem_info *uinfo)
2993 {
2994 	u8 reg, idx;
2995 	int pv;
2996 
2997 	pv = kcontrol->private_value;
2998 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2999 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
3000 
3001 	if (reg == SND_BBFPRO_CTL_REG1 &&
3002 	    idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) {
3003 		static const char * const texts[2] = {
3004 			"AutoSync",
3005 			"Internal"
3006 		};
3007 		return snd_ctl_enum_info(uinfo, 1, 2, texts);
3008 	} else if (reg == SND_BBFPRO_CTL_REG2 &&
3009 		   (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
3010 		    idx == SND_BBFPRO_CTL_REG2_SENS_IN4)) {
3011 		static const char * const texts[2] = {
3012 			"-10dBV",
3013 			"+4dBu"
3014 		};
3015 		return snd_ctl_enum_info(uinfo, 1, 2, texts);
3016 	}
3017 
3018 	uinfo->count = 1;
3019 	uinfo->value.integer.min = 0;
3020 	uinfo->value.integer.max = 1;
3021 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3022 	return 0;
3023 }
3024 
3025 static int snd_bbfpro_ctl_put(struct snd_kcontrol *kcontrol,
3026 			      struct snd_ctl_elem_value *ucontrol)
3027 {
3028 	int err;
3029 	u8 reg, idx;
3030 	int old_value, pv, val;
3031 
3032 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3033 	struct usb_mixer_interface *mixer = list->mixer;
3034 
3035 	pv = kcontrol->private_value;
3036 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
3037 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
3038 	old_value = (pv >> SND_BBFPRO_CTL_VAL_SHIFT) & SND_BBFPRO_CTL_VAL_MASK;
3039 
3040 	if ((reg == SND_BBFPRO_CTL_REG1 &&
3041 	     idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) ||
3042 	    (reg == SND_BBFPRO_CTL_REG2 &&
3043 	    (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
3044 	     idx == SND_BBFPRO_CTL_REG2_SENS_IN4))) {
3045 		val = ucontrol->value.enumerated.item[0];
3046 	} else {
3047 		val = ucontrol->value.integer.value[0];
3048 	}
3049 
3050 	if (val > 1)
3051 		return -EINVAL;
3052 
3053 	if (val == old_value)
3054 		return 0;
3055 
3056 	err = snd_bbfpro_ctl_update(mixer, reg, idx, val);
3057 	if (err < 0)
3058 		return err;
3059 
3060 	kcontrol->private_value = reg
3061 		| ((idx & SND_BBFPRO_CTL_IDX_MASK) << SND_BBFPRO_CTL_IDX_SHIFT)
3062 		| ((val & SND_BBFPRO_CTL_VAL_MASK) << SND_BBFPRO_CTL_VAL_SHIFT);
3063 	return 1;
3064 }
3065 
3066 static int snd_bbfpro_ctl_resume(struct usb_mixer_elem_list *list)
3067 {
3068 	u8 reg, idx;
3069 	int value, pv;
3070 
3071 	pv = list->kctl->private_value;
3072 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
3073 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
3074 	value = (pv >> SND_BBFPRO_CTL_VAL_SHIFT) & SND_BBFPRO_CTL_VAL_MASK;
3075 
3076 	return snd_bbfpro_ctl_update(list->mixer, reg, idx, value);
3077 }
3078 
3079 static int snd_bbfpro_gain_update(struct usb_mixer_interface *mixer,
3080 				  u8 channel, u8 gain)
3081 {
3082 	struct snd_usb_audio *chip = mixer->chip;
3083 
3084 	if (channel < 2) {
3085 		// XLR preamp: 3-bit fine, 5-bit coarse; special case >60
3086 		if (gain < 60)
3087 			gain = ((gain % 3) << 5) | (gain / 3);
3088 		else
3089 			gain = ((gain % 6) << 5) | (60 / 3);
3090 	}
3091 
3092 	CLASS(snd_usb_lock, pm)(chip);
3093 	if (pm.err < 0)
3094 		return pm.err;
3095 
3096 	return snd_usb_ctl_msg(chip->dev,
3097 			       usb_sndctrlpipe(chip->dev, 0),
3098 			       SND_BBFPRO_USBREQ_GAIN,
3099 			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3100 			       gain, channel, NULL, 0);
3101 }
3102 
3103 static int snd_bbfpro_gain_get(struct snd_kcontrol *kcontrol,
3104 			       struct snd_ctl_elem_value *ucontrol)
3105 {
3106 	int value = kcontrol->private_value & SND_BBFPRO_GAIN_VAL_MASK;
3107 
3108 	ucontrol->value.integer.value[0] = value;
3109 	return 0;
3110 }
3111 
3112 static int snd_bbfpro_gain_info(struct snd_kcontrol *kcontrol,
3113 				struct snd_ctl_elem_info *uinfo)
3114 {
3115 	int pv, channel;
3116 
3117 	pv = kcontrol->private_value;
3118 	channel = (pv >> SND_BBFPRO_GAIN_CHANNEL_SHIFT) &
3119 		SND_BBFPRO_GAIN_CHANNEL_MASK;
3120 
3121 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3122 	uinfo->count = 1;
3123 	uinfo->value.integer.min = SND_BBFPRO_GAIN_VAL_MIN;
3124 
3125 	if (channel < 2)
3126 		uinfo->value.integer.max = SND_BBFPRO_GAIN_VAL_MIC_MAX;
3127 	else
3128 		uinfo->value.integer.max = SND_BBFPRO_GAIN_VAL_LINE_MAX;
3129 
3130 	return 0;
3131 }
3132 
3133 static int snd_bbfpro_gain_put(struct snd_kcontrol *kcontrol,
3134 			       struct snd_ctl_elem_value *ucontrol)
3135 {
3136 	int pv, channel, old_value, value, err;
3137 
3138 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3139 	struct usb_mixer_interface *mixer = list->mixer;
3140 
3141 	pv = kcontrol->private_value;
3142 	channel = (pv >> SND_BBFPRO_GAIN_CHANNEL_SHIFT) &
3143 		SND_BBFPRO_GAIN_CHANNEL_MASK;
3144 	old_value = pv & SND_BBFPRO_GAIN_VAL_MASK;
3145 	value = ucontrol->value.integer.value[0];
3146 
3147 	if (value < SND_BBFPRO_GAIN_VAL_MIN)
3148 		return -EINVAL;
3149 
3150 	if (channel < 2) {
3151 		if (value > SND_BBFPRO_GAIN_VAL_MIC_MAX)
3152 			return -EINVAL;
3153 	} else {
3154 		if (value > SND_BBFPRO_GAIN_VAL_LINE_MAX)
3155 			return -EINVAL;
3156 	}
3157 
3158 	if (value == old_value)
3159 		return 0;
3160 
3161 	err = snd_bbfpro_gain_update(mixer, channel, value);
3162 	if (err < 0)
3163 		return err;
3164 
3165 	kcontrol->private_value =
3166 		(channel << SND_BBFPRO_GAIN_CHANNEL_SHIFT) | value;
3167 	return 1;
3168 }
3169 
3170 static int snd_bbfpro_gain_resume(struct usb_mixer_elem_list *list)
3171 {
3172 	int pv, channel, value;
3173 	struct snd_kcontrol *kctl = list->kctl;
3174 
3175 	pv = kctl->private_value;
3176 	channel = (pv >> SND_BBFPRO_GAIN_CHANNEL_SHIFT) &
3177 		SND_BBFPRO_GAIN_CHANNEL_MASK;
3178 	value = pv & SND_BBFPRO_GAIN_VAL_MASK;
3179 
3180 	return snd_bbfpro_gain_update(list->mixer, channel, value);
3181 }
3182 
3183 static int snd_bbfpro_vol_update(struct usb_mixer_interface *mixer, u16 index,
3184 				 u32 value)
3185 {
3186 	struct snd_usb_audio *chip = mixer->chip;
3187 	u16 idx;
3188 	u16 usb_idx, usb_val;
3189 	u32 v;
3190 
3191 	CLASS(snd_usb_lock, pm)(chip);
3192 	if (pm.err < 0)
3193 		return pm.err;
3194 
3195 	idx = index & SND_BBFPRO_MIXER_IDX_MASK;
3196 	// 18 bit linear volume, split so 2 bits end up in index.
3197 	v = value & SND_BBFPRO_MIXER_VAL_MASK;
3198 	usb_idx = idx | (v & 0x3) << 14;
3199 	usb_val = (v >> 2) & 0xffff;
3200 
3201 	return snd_usb_ctl_msg(chip->dev,
3202 			       usb_sndctrlpipe(chip->dev, 0),
3203 			       SND_BBFPRO_USBREQ_MIXER,
3204 			       USB_DIR_OUT | USB_TYPE_VENDOR |
3205 			       USB_RECIP_DEVICE,
3206 			       usb_val, usb_idx, NULL, 0);
3207 }
3208 
3209 static int snd_bbfpro_vol_get(struct snd_kcontrol *kcontrol,
3210 			      struct snd_ctl_elem_value *ucontrol)
3211 {
3212 	ucontrol->value.integer.value[0] =
3213 		kcontrol->private_value >> SND_BBFPRO_MIXER_VAL_SHIFT;
3214 	return 0;
3215 }
3216 
3217 static int snd_bbfpro_vol_info(struct snd_kcontrol *kcontrol,
3218 			       struct snd_ctl_elem_info *uinfo)
3219 {
3220 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3221 	uinfo->count = 1;
3222 	uinfo->value.integer.min = SND_BBFPRO_MIXER_VAL_MIN;
3223 	uinfo->value.integer.max = SND_BBFPRO_MIXER_VAL_MAX;
3224 	return 0;
3225 }
3226 
3227 static int snd_bbfpro_vol_put(struct snd_kcontrol *kcontrol,
3228 			      struct snd_ctl_elem_value *ucontrol)
3229 {
3230 	int err;
3231 	u16 idx;
3232 	u32 new_val, old_value, uvalue;
3233 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3234 	struct usb_mixer_interface *mixer = list->mixer;
3235 
3236 	uvalue = ucontrol->value.integer.value[0];
3237 	idx = kcontrol->private_value & SND_BBFPRO_MIXER_IDX_MASK;
3238 	old_value = kcontrol->private_value >> SND_BBFPRO_MIXER_VAL_SHIFT;
3239 
3240 	if (uvalue > SND_BBFPRO_MIXER_VAL_MAX)
3241 		return -EINVAL;
3242 
3243 	if (uvalue == old_value)
3244 		return 0;
3245 
3246 	new_val = uvalue & SND_BBFPRO_MIXER_VAL_MASK;
3247 
3248 	err = snd_bbfpro_vol_update(mixer, idx, new_val);
3249 	if (err < 0)
3250 		return err;
3251 
3252 	kcontrol->private_value = idx
3253 		| (new_val << SND_BBFPRO_MIXER_VAL_SHIFT);
3254 	return 1;
3255 }
3256 
3257 static int snd_bbfpro_vol_resume(struct usb_mixer_elem_list *list)
3258 {
3259 	int pv = list->kctl->private_value;
3260 	u16 idx = pv & SND_BBFPRO_MIXER_IDX_MASK;
3261 	u32 val = (pv >> SND_BBFPRO_MIXER_VAL_SHIFT)
3262 		& SND_BBFPRO_MIXER_VAL_MASK;
3263 	return snd_bbfpro_vol_update(list->mixer, idx, val);
3264 }
3265 
3266 // Predfine elements
3267 static const struct snd_kcontrol_new snd_bbfpro_ctl_control = {
3268 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3269 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3270 	.index = 0,
3271 	.info = snd_bbfpro_ctl_info,
3272 	.get = snd_bbfpro_ctl_get,
3273 	.put = snd_bbfpro_ctl_put
3274 };
3275 
3276 static const struct snd_kcontrol_new snd_bbfpro_gain_control = {
3277 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3278 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3279 	.index = 0,
3280 	.info = snd_bbfpro_gain_info,
3281 	.get = snd_bbfpro_gain_get,
3282 	.put = snd_bbfpro_gain_put
3283 };
3284 
3285 static const struct snd_kcontrol_new snd_bbfpro_vol_control = {
3286 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3287 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3288 	.index = 0,
3289 	.info = snd_bbfpro_vol_info,
3290 	.get = snd_bbfpro_vol_get,
3291 	.put = snd_bbfpro_vol_put
3292 };
3293 
3294 static int snd_bbfpro_ctl_add(struct usb_mixer_interface *mixer, u8 reg,
3295 			      u8 index, char *name)
3296 {
3297 	struct snd_kcontrol_new knew = snd_bbfpro_ctl_control;
3298 
3299 	knew.name = name;
3300 	knew.private_value = (reg & SND_BBFPRO_CTL_REG_MASK)
3301 		| ((index & SND_BBFPRO_CTL_IDX_MASK)
3302 			<< SND_BBFPRO_CTL_IDX_SHIFT);
3303 
3304 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_ctl_resume,
3305 		&knew, NULL);
3306 }
3307 
3308 static int snd_bbfpro_gain_add(struct usb_mixer_interface *mixer, u8 channel,
3309 			       char *name)
3310 {
3311 	struct snd_kcontrol_new knew = snd_bbfpro_gain_control;
3312 
3313 	knew.name = name;
3314 	knew.private_value = channel << SND_BBFPRO_GAIN_CHANNEL_SHIFT;
3315 
3316 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_gain_resume,
3317 		&knew, NULL);
3318 }
3319 
3320 static int snd_bbfpro_vol_add(struct usb_mixer_interface *mixer, u16 index,
3321 			      char *name)
3322 {
3323 	struct snd_kcontrol_new knew = snd_bbfpro_vol_control;
3324 
3325 	knew.name = name;
3326 	knew.private_value = index & SND_BBFPRO_MIXER_IDX_MASK;
3327 
3328 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_vol_resume,
3329 		&knew, NULL);
3330 }
3331 
3332 static int snd_bbfpro_controls_create(struct usb_mixer_interface *mixer)
3333 {
3334 	int err, i, o;
3335 	char name[48];
3336 
3337 	static const char * const input[] = {
3338 		"AN1", "AN2", "IN3", "IN4", "AS1", "AS2", "ADAT3",
3339 		"ADAT4", "ADAT5", "ADAT6", "ADAT7", "ADAT8"};
3340 
3341 	static const char * const output[] = {
3342 		"AN1", "AN2", "PH3", "PH4", "AS1", "AS2", "ADAT3", "ADAT4",
3343 		"ADAT5", "ADAT6", "ADAT7", "ADAT8"};
3344 
3345 	for (o = 0 ; o < 12 ; ++o) {
3346 		for (i = 0 ; i < 12 ; ++i) {
3347 			// Line routing
3348 			snprintf(name, sizeof(name),
3349 				 "%s-%s-%s Playback Volume",
3350 				 (i < 2 ? "Mic" : "Line"),
3351 				 input[i], output[o]);
3352 			err = snd_bbfpro_vol_add(mixer, (26 * o + i), name);
3353 			if (err < 0)
3354 				return err;
3355 
3356 			// PCM routing... yes, it is output remapping
3357 			snprintf(name, sizeof(name),
3358 				 "PCM-%s-%s Playback Volume",
3359 				 output[i], output[o]);
3360 			err = snd_bbfpro_vol_add(mixer, (26 * o + 12 + i),
3361 						 name);
3362 			if (err < 0)
3363 				return err;
3364 		}
3365 	}
3366 
3367 	// Main out volume
3368 	for (i = 0 ; i < 12 ; ++i) {
3369 		snprintf(name, sizeof(name), "Main-Out %s", output[i]);
3370 		// Main outs are offset to 992
3371 		err = snd_bbfpro_vol_add(mixer,
3372 					 i + SND_BBFPRO_MIXER_MAIN_OUT_CH_OFFSET,
3373 					 name);
3374 		if (err < 0)
3375 			return err;
3376 	}
3377 
3378 	// Input gain
3379 	for (i = 0 ; i < 4 ; ++i) {
3380 		if (i < 2)
3381 			snprintf(name, sizeof(name), "Mic-%s Gain", input[i]);
3382 		else
3383 			snprintf(name, sizeof(name), "Line-%s Gain", input[i]);
3384 
3385 		err = snd_bbfpro_gain_add(mixer, i, name);
3386 		if (err < 0)
3387 			return err;
3388 	}
3389 
3390 	// Control Reg 1
3391 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3392 				 SND_BBFPRO_CTL_REG1_CLK_OPTICAL,
3393 				 "Sample Clock Source");
3394 	if (err < 0)
3395 		return err;
3396 
3397 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3398 				 SND_BBFPRO_CTL_REG1_SPDIF_PRO,
3399 				 "IEC958 Pro Mask");
3400 	if (err < 0)
3401 		return err;
3402 
3403 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3404 				 SND_BBFPRO_CTL_REG1_SPDIF_EMPH,
3405 				 "IEC958 Emphasis");
3406 	if (err < 0)
3407 		return err;
3408 
3409 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3410 				 SND_BBFPRO_CTL_REG1_SPDIF_OPTICAL,
3411 				 "IEC958 Switch");
3412 	if (err < 0)
3413 		return err;
3414 
3415 	// Control Reg 2
3416 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3417 				 SND_BBFPRO_CTL_REG2_48V_AN1,
3418 				 "Mic-AN1 48V");
3419 	if (err < 0)
3420 		return err;
3421 
3422 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3423 				 SND_BBFPRO_CTL_REG2_48V_AN2,
3424 				 "Mic-AN2 48V");
3425 	if (err < 0)
3426 		return err;
3427 
3428 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3429 				 SND_BBFPRO_CTL_REG2_SENS_IN3,
3430 				 "Line-IN3 Sens.");
3431 	if (err < 0)
3432 		return err;
3433 
3434 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3435 				 SND_BBFPRO_CTL_REG2_SENS_IN4,
3436 				 "Line-IN4 Sens.");
3437 	if (err < 0)
3438 		return err;
3439 
3440 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3441 				 SND_BBFPRO_CTL_REG2_PAD_AN1,
3442 				 "Mic-AN1 PAD");
3443 	if (err < 0)
3444 		return err;
3445 
3446 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3447 				 SND_BBFPRO_CTL_REG2_PAD_AN2,
3448 				 "Mic-AN2 PAD");
3449 	if (err < 0)
3450 		return err;
3451 
3452 	return 0;
3453 }
3454 
3455 /*
3456  * RME Digiface USB
3457  */
3458 
3459 #define RME_DIGIFACE_READ_STATUS 17
3460 #define RME_DIGIFACE_STATUS_REG0L 0
3461 #define RME_DIGIFACE_STATUS_REG0H 1
3462 #define RME_DIGIFACE_STATUS_REG1L 2
3463 #define RME_DIGIFACE_STATUS_REG1H 3
3464 #define RME_DIGIFACE_STATUS_REG2L 4
3465 #define RME_DIGIFACE_STATUS_REG2H 5
3466 #define RME_DIGIFACE_STATUS_REG3L 6
3467 #define RME_DIGIFACE_STATUS_REG3H 7
3468 
3469 #define RME_DIGIFACE_CTL_REG1 16
3470 #define RME_DIGIFACE_CTL_REG2 18
3471 
3472 /* Reg is overloaded, 0-7 for status halfwords or 16 or 18 for control registers */
3473 #define RME_DIGIFACE_REGISTER(reg, mask) (((reg) << 16) | (mask))
3474 #define RME_DIGIFACE_INVERT BIT(31)
3475 
3476 static int snd_rme_digiface_write_reg(struct snd_kcontrol *kcontrol, int item, u16 mask, u16 val)
3477 {
3478 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3479 	struct snd_usb_audio *chip = list->mixer->chip;
3480 	struct usb_device *dev = chip->dev;
3481 	int err;
3482 
3483 	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
3484 			      item,
3485 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3486 			      val, mask, NULL, 0);
3487 	if (err < 0)
3488 		dev_err(&dev->dev,
3489 			"unable to issue control set request %d (ret = %d)",
3490 			item, err);
3491 	return err;
3492 }
3493 
3494 static int snd_rme_digiface_read_status(struct snd_kcontrol *kcontrol, u32 status[4])
3495 {
3496 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3497 	struct snd_usb_audio *chip = list->mixer->chip;
3498 	struct usb_device *dev = chip->dev;
3499 	__le32 buf[4];
3500 	int err;
3501 
3502 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
3503 			      RME_DIGIFACE_READ_STATUS,
3504 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3505 			      0, 0,
3506 			      buf, sizeof(buf));
3507 	if (err < 0) {
3508 		dev_err(&dev->dev,
3509 			"unable to issue status read request (ret = %d)",
3510 			err);
3511 	} else {
3512 		for (int i = 0; i < ARRAY_SIZE(buf); i++)
3513 			status[i] = le32_to_cpu(buf[i]);
3514 	}
3515 	return err;
3516 }
3517 
3518 static int snd_rme_digiface_get_status_val(struct snd_kcontrol *kcontrol)
3519 {
3520 	int err;
3521 	u32 status[4];
3522 	bool invert = kcontrol->private_value & RME_DIGIFACE_INVERT;
3523 	u8 reg = (kcontrol->private_value >> 16) & 0xff;
3524 	u16 mask = kcontrol->private_value & 0xffff;
3525 	u16 val;
3526 
3527 	err = snd_rme_digiface_read_status(kcontrol, status);
3528 	if (err < 0)
3529 		return err;
3530 
3531 	switch (reg) {
3532 	/* Status register halfwords */
3533 	case RME_DIGIFACE_STATUS_REG0L ... RME_DIGIFACE_STATUS_REG3H:
3534 		break;
3535 	case RME_DIGIFACE_CTL_REG1: /* Control register 1, present in halfword 3L */
3536 		reg = RME_DIGIFACE_STATUS_REG3L;
3537 		break;
3538 	case RME_DIGIFACE_CTL_REG2: /* Control register 2, present in halfword 3H */
3539 		reg = RME_DIGIFACE_STATUS_REG3H;
3540 		break;
3541 	default:
3542 		return -EINVAL;
3543 	}
3544 
3545 	if (reg & 1)
3546 		val = status[reg >> 1] >> 16;
3547 	else
3548 		val = status[reg >> 1] & 0xffff;
3549 
3550 	if (invert)
3551 		val ^= mask;
3552 
3553 	return field_get(mask, val);
3554 }
3555 
3556 static int snd_rme_digiface_rate_get(struct snd_kcontrol *kcontrol,
3557 				     struct snd_ctl_elem_value *ucontrol)
3558 {
3559 	int freq = snd_rme_digiface_get_status_val(kcontrol);
3560 
3561 	if (freq < 0)
3562 		return freq;
3563 	if (freq >= ARRAY_SIZE(snd_rme_rate_table))
3564 		return -EIO;
3565 
3566 	ucontrol->value.integer.value[0] = snd_rme_rate_table[freq];
3567 	return 0;
3568 }
3569 
3570 static int snd_rme_digiface_enum_get(struct snd_kcontrol *kcontrol,
3571 				     struct snd_ctl_elem_value *ucontrol)
3572 {
3573 	int val = snd_rme_digiface_get_status_val(kcontrol);
3574 
3575 	if (val < 0)
3576 		return val;
3577 
3578 	ucontrol->value.enumerated.item[0] = val;
3579 	return 0;
3580 }
3581 
3582 static int snd_rme_digiface_enum_put(struct snd_kcontrol *kcontrol,
3583 				     struct snd_ctl_elem_value *ucontrol)
3584 {
3585 	bool invert = kcontrol->private_value & RME_DIGIFACE_INVERT;
3586 	u8 reg = (kcontrol->private_value >> 16) & 0xff;
3587 	u16 mask = kcontrol->private_value & 0xffff;
3588 	u16 val = field_prep(mask, ucontrol->value.enumerated.item[0]);
3589 
3590 	if (invert)
3591 		val ^= mask;
3592 
3593 	return snd_rme_digiface_write_reg(kcontrol, reg, mask, val);
3594 }
3595 
3596 static int snd_rme_digiface_current_sync_get(struct snd_kcontrol *kcontrol,
3597 					     struct snd_ctl_elem_value *ucontrol)
3598 {
3599 	int ret = snd_rme_digiface_enum_get(kcontrol, ucontrol);
3600 
3601 	/* 7 means internal for current sync */
3602 	if (ucontrol->value.enumerated.item[0] == 7)
3603 		ucontrol->value.enumerated.item[0] = 0;
3604 
3605 	return ret;
3606 }
3607 
3608 static int snd_rme_digiface_sync_state_get(struct snd_kcontrol *kcontrol,
3609 					   struct snd_ctl_elem_value *ucontrol)
3610 {
3611 	u32 status[4];
3612 	int err;
3613 	bool valid, sync;
3614 
3615 	err = snd_rme_digiface_read_status(kcontrol, status);
3616 	if (err < 0)
3617 		return err;
3618 
3619 	valid = status[0] & BIT(kcontrol->private_value);
3620 	sync = status[0] & BIT(5 + kcontrol->private_value);
3621 
3622 	if (!valid)
3623 		ucontrol->value.enumerated.item[0] = SND_RME_CLOCK_NOLOCK;
3624 	else if (!sync)
3625 		ucontrol->value.enumerated.item[0] = SND_RME_CLOCK_LOCK;
3626 	else
3627 		ucontrol->value.enumerated.item[0] = SND_RME_CLOCK_SYNC;
3628 	return 0;
3629 }
3630 
3631 static int snd_rme_digiface_format_info(struct snd_kcontrol *kcontrol,
3632 					struct snd_ctl_elem_info *uinfo)
3633 {
3634 	static const char *const format[] = {
3635 		"ADAT", "S/PDIF"
3636 	};
3637 
3638 	return snd_ctl_enum_info(uinfo, 1,
3639 				 ARRAY_SIZE(format), format);
3640 }
3641 
3642 static int snd_rme_digiface_sync_source_info(struct snd_kcontrol *kcontrol,
3643 					     struct snd_ctl_elem_info *uinfo)
3644 {
3645 	static const char *const sync_sources[] = {
3646 		"Internal", "Input 1", "Input 2", "Input 3", "Input 4"
3647 	};
3648 
3649 	return snd_ctl_enum_info(uinfo, 1,
3650 				 ARRAY_SIZE(sync_sources), sync_sources);
3651 }
3652 
3653 static int snd_rme_digiface_rate_info(struct snd_kcontrol *kcontrol,
3654 				      struct snd_ctl_elem_info *uinfo)
3655 {
3656 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3657 	uinfo->count = 1;
3658 	uinfo->value.integer.min = 0;
3659 	uinfo->value.integer.max = 200000;
3660 	uinfo->value.integer.step = 0;
3661 	return 0;
3662 }
3663 
3664 static const struct snd_kcontrol_new snd_rme_digiface_controls[] = {
3665 	{
3666 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3667 		.name = "Input 1 Sync",
3668 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3669 		.info = snd_rme_sync_state_info,
3670 		.get = snd_rme_digiface_sync_state_get,
3671 		.private_value = 0,
3672 	},
3673 	{
3674 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3675 		.name = "Input 1 Format",
3676 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3677 		.info = snd_rme_digiface_format_info,
3678 		.get = snd_rme_digiface_enum_get,
3679 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0H, BIT(0)) |
3680 			RME_DIGIFACE_INVERT,
3681 	},
3682 	{
3683 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3684 		.name = "Input 1 Rate",
3685 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3686 		.info = snd_rme_digiface_rate_info,
3687 		.get = snd_rme_digiface_rate_get,
3688 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(3, 0)),
3689 	},
3690 	{
3691 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3692 		.name = "Input 2 Sync",
3693 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3694 		.info = snd_rme_sync_state_info,
3695 		.get = snd_rme_digiface_sync_state_get,
3696 		.private_value = 1,
3697 	},
3698 	{
3699 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3700 		.name = "Input 2 Format",
3701 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3702 		.info = snd_rme_digiface_format_info,
3703 		.get = snd_rme_digiface_enum_get,
3704 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, BIT(13)) |
3705 			RME_DIGIFACE_INVERT,
3706 	},
3707 	{
3708 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3709 		.name = "Input 2 Rate",
3710 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3711 		.info = snd_rme_digiface_rate_info,
3712 		.get = snd_rme_digiface_rate_get,
3713 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(7, 4)),
3714 	},
3715 	{
3716 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3717 		.name = "Input 3 Sync",
3718 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3719 		.info = snd_rme_sync_state_info,
3720 		.get = snd_rme_digiface_sync_state_get,
3721 		.private_value = 2,
3722 	},
3723 	{
3724 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3725 		.name = "Input 3 Format",
3726 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3727 		.info = snd_rme_digiface_format_info,
3728 		.get = snd_rme_digiface_enum_get,
3729 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, BIT(14)) |
3730 			RME_DIGIFACE_INVERT,
3731 	},
3732 	{
3733 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3734 		.name = "Input 3 Rate",
3735 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3736 		.info = snd_rme_digiface_rate_info,
3737 		.get = snd_rme_digiface_rate_get,
3738 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(11, 8)),
3739 	},
3740 	{
3741 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3742 		.name = "Input 4 Sync",
3743 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3744 		.info = snd_rme_sync_state_info,
3745 		.get = snd_rme_digiface_sync_state_get,
3746 		.private_value = 3,
3747 	},
3748 	{
3749 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3750 		.name = "Input 4 Format",
3751 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3752 		.info = snd_rme_digiface_format_info,
3753 		.get = snd_rme_digiface_enum_get,
3754 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, GENMASK(15, 12)) |
3755 			RME_DIGIFACE_INVERT,
3756 	},
3757 	{
3758 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3759 		.name = "Input 4 Rate",
3760 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3761 		.info = snd_rme_digiface_rate_info,
3762 		.get = snd_rme_digiface_rate_get,
3763 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(3, 0)),
3764 	},
3765 	{
3766 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3767 		.name = "Output 1 Format",
3768 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3769 		.info = snd_rme_digiface_format_info,
3770 		.get = snd_rme_digiface_enum_get,
3771 		.put = snd_rme_digiface_enum_put,
3772 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(0)),
3773 	},
3774 	{
3775 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3776 		.name = "Output 2 Format",
3777 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3778 		.info = snd_rme_digiface_format_info,
3779 		.get = snd_rme_digiface_enum_get,
3780 		.put = snd_rme_digiface_enum_put,
3781 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(1)),
3782 	},
3783 	{
3784 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3785 		.name = "Output 3 Format",
3786 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3787 		.info = snd_rme_digiface_format_info,
3788 		.get = snd_rme_digiface_enum_get,
3789 		.put = snd_rme_digiface_enum_put,
3790 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(3)),
3791 	},
3792 	{
3793 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3794 		.name = "Output 4 Format",
3795 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3796 		.info = snd_rme_digiface_format_info,
3797 		.get = snd_rme_digiface_enum_get,
3798 		.put = snd_rme_digiface_enum_put,
3799 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(4)),
3800 	},
3801 	{
3802 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3803 		.name = "Sync Source",
3804 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3805 		.info = snd_rme_digiface_sync_source_info,
3806 		.get = snd_rme_digiface_enum_get,
3807 		.put = snd_rme_digiface_enum_put,
3808 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG1, GENMASK(2, 0)),
3809 	},
3810 	{
3811 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3812 		.name = "Current Sync Source",
3813 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3814 		.info = snd_rme_digiface_sync_source_info,
3815 		.get = snd_rme_digiface_current_sync_get,
3816 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, GENMASK(12, 10)),
3817 	},
3818 	{
3819 		/*
3820 		 * This is writeable, but it is only set by the PCM rate.
3821 		 * Mixer apps currently need to drive the mixer using raw USB requests,
3822 		 * so they can also change this that way to configure the rate for
3823 		 * stand-alone operation when the PCM is closed.
3824 		 */
3825 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3826 		.name = "System Rate",
3827 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3828 		.info = snd_rme_rate_info,
3829 		.get = snd_rme_digiface_rate_get,
3830 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG1, GENMASK(6, 3)),
3831 	},
3832 	{
3833 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3834 		.name = "Current Rate",
3835 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3836 		.info = snd_rme_rate_info,
3837 		.get = snd_rme_digiface_rate_get,
3838 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1H, GENMASK(7, 4)),
3839 	}
3840 };
3841 
3842 static int snd_rme_digiface_controls_create(struct usb_mixer_interface *mixer)
3843 {
3844 	int err, i;
3845 
3846 	for (i = 0; i < ARRAY_SIZE(snd_rme_digiface_controls); ++i) {
3847 		err = add_single_ctl_with_resume(mixer, 0,
3848 						 NULL,
3849 						 &snd_rme_digiface_controls[i],
3850 						 NULL);
3851 		if (err < 0)
3852 			return err;
3853 	}
3854 
3855 	return 0;
3856 }
3857 
3858 /*
3859  * Pioneer DJ / AlphaTheta DJM Mixers
3860  *
3861  * These devices generally have options for soft-switching the playback and
3862  * capture sources in addition to the recording level. Although different
3863  * devices have different configurations, there seems to be canonical values
3864  * for specific capture/playback types:  See the definitions of these below.
3865  *
3866  * The wValue is masked with the stereo channel number. e.g. Setting Ch2 to
3867  * capture phono would be 0x0203. Capture, playback and capture level have
3868  * different wIndexes.
3869  */
3870 
3871 // Capture types
3872 #define SND_DJM_CAP_LINE	0x00
3873 #define SND_DJM_CAP_CDLINE	0x01
3874 #define SND_DJM_CAP_DIGITAL	0x02
3875 #define SND_DJM_CAP_PHONO	0x03
3876 #define SND_DJM_CAP_PREFADER	0x05
3877 #define SND_DJM_CAP_PFADER	0x06
3878 #define SND_DJM_CAP_XFADERA	0x07
3879 #define SND_DJM_CAP_XFADERB	0x08
3880 #define SND_DJM_CAP_MIC		0x09
3881 #define SND_DJM_CAP_AUX		0x0d
3882 #define SND_DJM_CAP_RECOUT	0x0a
3883 #define SND_DJM_CAP_RECOUT_NOMIC	0x0e
3884 #define SND_DJM_CAP_NONE	0x0f
3885 #define SND_DJM_CAP_FXSEND	0x10
3886 #define SND_DJM_CAP_CH1PFADER	0x11
3887 #define SND_DJM_CAP_CH2PFADER	0x12
3888 #define SND_DJM_CAP_CH3PFADER	0x13
3889 #define SND_DJM_CAP_CH4PFADER	0x14
3890 #define SND_DJM_CAP_EXT1SEND	0x21
3891 #define SND_DJM_CAP_EXT2SEND	0x22
3892 #define SND_DJM_CAP_CH1PREFADER	0x31
3893 #define SND_DJM_CAP_CH2PREFADER	0x32
3894 #define SND_DJM_CAP_CH3PREFADER	0x33
3895 #define SND_DJM_CAP_CH4PREFADER	0x34
3896 
3897 // Playback types
3898 #define SND_DJM_PB_CH1		0x00
3899 #define SND_DJM_PB_CH2		0x01
3900 #define SND_DJM_PB_AUX		0x04
3901 
3902 #define SND_DJM_WINDEX_CAP	0x8002
3903 #define SND_DJM_WINDEX_CAPLVL	0x8003
3904 #define SND_DJM_WINDEX_PB	0x8016
3905 
3906 // kcontrol->private_value layout
3907 #define SND_DJM_VALUE_MASK	0x0000ffff
3908 #define SND_DJM_GROUP_MASK	0x00ff0000
3909 #define SND_DJM_DEVICE_MASK	0xff000000
3910 #define SND_DJM_GROUP_SHIFT	16
3911 #define SND_DJM_DEVICE_SHIFT	24
3912 
3913 // device table index
3914 // used for the snd_djm_devices table, so please update accordingly
3915 #define SND_DJM_250MK2_IDX	0x0
3916 #define SND_DJM_750_IDX		0x1
3917 #define SND_DJM_850_IDX		0x2
3918 #define SND_DJM_900NXS2_IDX	0x3
3919 #define SND_DJM_750MK2_IDX	0x4
3920 #define SND_DJM_450_IDX		0x5
3921 #define SND_DJM_A9_IDX		0x6
3922 #define SND_DJM_V10_IDX	0x7
3923 
3924 #define SND_DJM_CTL(_name, suffix, _default_value, _windex) { \
3925 	.name = _name, \
3926 	.options = snd_djm_opts_##suffix, \
3927 	.noptions = ARRAY_SIZE(snd_djm_opts_##suffix), \
3928 	.default_value = _default_value, \
3929 	.wIndex = _windex }
3930 
3931 #define SND_DJM_DEVICE(suffix) { \
3932 	.controls = snd_djm_ctls_##suffix, \
3933 	.ncontrols = ARRAY_SIZE(snd_djm_ctls_##suffix) }
3934 
3935 struct snd_djm_device {
3936 	const char *name;
3937 	const struct snd_djm_ctl *controls;
3938 	size_t ncontrols;
3939 };
3940 
3941 struct snd_djm_ctl {
3942 	const char *name;
3943 	const u16 *options;
3944 	size_t noptions;
3945 	u16 default_value;
3946 	u16 wIndex;
3947 };
3948 
3949 static const char *snd_djm_get_label_caplevel_common(u16 wvalue)
3950 {
3951 	switch (wvalue) {
3952 	case 0x0000:	return "-19dB";
3953 	case 0x0100:	return "-15dB";
3954 	case 0x0200:	return "-10dB";
3955 	case 0x0300:	return "-5dB";
3956 	default:	return NULL;
3957 	}
3958 };
3959 
3960 // Models like DJM-A9 or DJM-V10 have different capture levels than others
3961 static const char *snd_djm_get_label_caplevel_high(u16 wvalue)
3962 {
3963 	switch (wvalue) {
3964 	case 0x0000:	return "+15dB";
3965 	case 0x0100:	return "+12dB";
3966 	case 0x0200:	return "+9dB";
3967 	case 0x0300:	return "+6dB";
3968 	case 0x0400:	return "+3dB";
3969 	case 0x0500:	return "0dB";
3970 	default:	return NULL;
3971 	}
3972 };
3973 
3974 static const char *snd_djm_get_label_cap_common(u16 wvalue)
3975 {
3976 	switch (wvalue & 0x00ff) {
3977 	case SND_DJM_CAP_LINE:		return "Control Tone LINE";
3978 	case SND_DJM_CAP_CDLINE:	return "Control Tone CD/LINE";
3979 	case SND_DJM_CAP_DIGITAL:	return "Control Tone DIGITAL";
3980 	case SND_DJM_CAP_PHONO:		return "Control Tone PHONO";
3981 	case SND_DJM_CAP_PFADER:	return "Post Fader";
3982 	case SND_DJM_CAP_XFADERA:	return "Cross Fader A";
3983 	case SND_DJM_CAP_XFADERB:	return "Cross Fader B";
3984 	case SND_DJM_CAP_MIC:		return "Mic";
3985 	case SND_DJM_CAP_RECOUT:	return "Rec Out";
3986 	case SND_DJM_CAP_RECOUT_NOMIC:	return "Rec Out without Mic";
3987 	case SND_DJM_CAP_AUX:		return "Aux";
3988 	case SND_DJM_CAP_NONE:		return "None";
3989 	case SND_DJM_CAP_FXSEND:	return "FX SEND";
3990 	case SND_DJM_CAP_CH1PREFADER:	return "Pre Fader Ch1";
3991 	case SND_DJM_CAP_CH2PREFADER:	return "Pre Fader Ch2";
3992 	case SND_DJM_CAP_CH3PREFADER:	return "Pre Fader Ch3";
3993 	case SND_DJM_CAP_CH4PREFADER:	return "Pre Fader Ch4";
3994 	case SND_DJM_CAP_CH1PFADER:	return "Post Fader Ch1";
3995 	case SND_DJM_CAP_CH2PFADER:	return "Post Fader Ch2";
3996 	case SND_DJM_CAP_CH3PFADER:	return "Post Fader Ch3";
3997 	case SND_DJM_CAP_CH4PFADER:	return "Post Fader Ch4";
3998 	case SND_DJM_CAP_EXT1SEND:	return "EXT1 SEND";
3999 	case SND_DJM_CAP_EXT2SEND:	return "EXT2 SEND";
4000 	default:			return NULL;
4001 	}
4002 };
4003 
4004 // The DJM-850 has different values for CD/LINE and LINE capture
4005 // control options than the other DJM declared in this file.
4006 static const char *snd_djm_get_label_cap_850(u16 wvalue)
4007 {
4008 	switch (wvalue & 0x00ff) {
4009 	case 0x00:		return "Control Tone CD/LINE";
4010 	case 0x01:		return "Control Tone LINE";
4011 	default:		return snd_djm_get_label_cap_common(wvalue);
4012 	}
4013 };
4014 
4015 static const char *snd_djm_get_label_caplevel(u8 device_idx, u16 wvalue)
4016 {
4017 	switch (device_idx) {
4018 	case SND_DJM_A9_IDX:		return snd_djm_get_label_caplevel_high(wvalue);
4019 	case SND_DJM_V10_IDX:		return snd_djm_get_label_caplevel_high(wvalue);
4020 	default:			return snd_djm_get_label_caplevel_common(wvalue);
4021 	}
4022 };
4023 
4024 static const char *snd_djm_get_label_cap(u8 device_idx, u16 wvalue)
4025 {
4026 	switch (device_idx) {
4027 	case SND_DJM_850_IDX:		return snd_djm_get_label_cap_850(wvalue);
4028 	default:			return snd_djm_get_label_cap_common(wvalue);
4029 	}
4030 };
4031 
4032 static const char *snd_djm_get_label_pb(u16 wvalue)
4033 {
4034 	switch (wvalue & 0x00ff) {
4035 	case SND_DJM_PB_CH1:	return "Ch1";
4036 	case SND_DJM_PB_CH2:	return "Ch2";
4037 	case SND_DJM_PB_AUX:	return "Aux";
4038 	default:		return NULL;
4039 	}
4040 };
4041 
4042 static const char *snd_djm_get_label(u8 device_idx, u16 wvalue, u16 windex)
4043 {
4044 	switch (windex) {
4045 	case SND_DJM_WINDEX_CAPLVL:	return snd_djm_get_label_caplevel(device_idx, wvalue);
4046 	case SND_DJM_WINDEX_CAP:	return snd_djm_get_label_cap(device_idx, wvalue);
4047 	case SND_DJM_WINDEX_PB:		return snd_djm_get_label_pb(wvalue);
4048 	default:			return NULL;
4049 	}
4050 };
4051 
4052 // common DJM capture level option values
4053 static const u16 snd_djm_opts_cap_level[] = {
4054 	0x0000, 0x0100, 0x0200, 0x0300 };
4055 
4056 // DJM-250MK2
4057 static const u16 snd_djm_opts_250mk2_cap1[] = {
4058 	0x0103, 0x0100, 0x0106, 0x0107, 0x0108, 0x0109, 0x010d, 0x010a };
4059 
4060 static const u16 snd_djm_opts_250mk2_cap2[] = {
4061 	0x0203, 0x0200, 0x0206, 0x0207, 0x0208, 0x0209, 0x020d, 0x020a };
4062 
4063 static const u16 snd_djm_opts_250mk2_cap3[] = {
4064 	0x030a, 0x0311, 0x0312, 0x0307, 0x0308, 0x0309, 0x030d };
4065 
4066 static const u16 snd_djm_opts_250mk2_pb1[] = { 0x0100, 0x0101, 0x0104 };
4067 static const u16 snd_djm_opts_250mk2_pb2[] = { 0x0200, 0x0201, 0x0204 };
4068 static const u16 snd_djm_opts_250mk2_pb3[] = { 0x0300, 0x0301, 0x0304 };
4069 
4070 static const struct snd_djm_ctl snd_djm_ctls_250mk2[] = {
4071 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4072 	SND_DJM_CTL("Input 1 Capture Switch",  250mk2_cap1, 2, SND_DJM_WINDEX_CAP),
4073 	SND_DJM_CTL("Input 2 Capture Switch",  250mk2_cap2, 2, SND_DJM_WINDEX_CAP),
4074 	SND_DJM_CTL("Input 3 Capture Switch",  250mk2_cap3, 0, SND_DJM_WINDEX_CAP),
4075 	SND_DJM_CTL("Output 1 Playback Switch", 250mk2_pb1, 0, SND_DJM_WINDEX_PB),
4076 	SND_DJM_CTL("Output 2 Playback Switch", 250mk2_pb2, 1, SND_DJM_WINDEX_PB),
4077 	SND_DJM_CTL("Output 3 Playback Switch", 250mk2_pb3, 2, SND_DJM_WINDEX_PB)
4078 };
4079 
4080 // DJM-450
4081 static const u16 snd_djm_opts_450_cap1[] = {
4082 	0x0103, 0x0100, 0x0106, 0x0107, 0x0108, 0x0109, 0x010d, 0x010a };
4083 
4084 static const u16 snd_djm_opts_450_cap2[] = {
4085 	0x0203, 0x0200, 0x0206, 0x0207, 0x0208, 0x0209, 0x020d, 0x020a };
4086 
4087 static const u16 snd_djm_opts_450_cap3[] = {
4088 	0x030a, 0x0311, 0x0312, 0x0307, 0x0308, 0x0309, 0x030d };
4089 
4090 static const u16 snd_djm_opts_450_pb1[] = { 0x0100, 0x0101, 0x0104 };
4091 static const u16 snd_djm_opts_450_pb2[] = { 0x0200, 0x0201, 0x0204 };
4092 static const u16 snd_djm_opts_450_pb3[] = { 0x0300, 0x0301, 0x0304 };
4093 
4094 static const struct snd_djm_ctl snd_djm_ctls_450[] = {
4095 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4096 	SND_DJM_CTL("Input 1 Capture Switch",  450_cap1, 2, SND_DJM_WINDEX_CAP),
4097 	SND_DJM_CTL("Input 2 Capture Switch",  450_cap2, 2, SND_DJM_WINDEX_CAP),
4098 	SND_DJM_CTL("Input 3 Capture Switch",  450_cap3, 0, SND_DJM_WINDEX_CAP),
4099 	SND_DJM_CTL("Output 1 Playback Switch", 450_pb1, 0, SND_DJM_WINDEX_PB),
4100 	SND_DJM_CTL("Output 2 Playback Switch", 450_pb2, 1, SND_DJM_WINDEX_PB),
4101 	SND_DJM_CTL("Output 3 Playback Switch", 450_pb3, 2, SND_DJM_WINDEX_PB)
4102 };
4103 
4104 // DJM-750
4105 static const u16 snd_djm_opts_750_cap1[] = {
4106 	0x0101, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a, 0x010f };
4107 static const u16 snd_djm_opts_750_cap2[] = {
4108 	0x0200, 0x0201, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020f };
4109 static const u16 snd_djm_opts_750_cap3[] = {
4110 	0x0300, 0x0301, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030f };
4111 static const u16 snd_djm_opts_750_cap4[] = {
4112 	0x0401, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040f };
4113 
4114 static const struct snd_djm_ctl snd_djm_ctls_750[] = {
4115 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4116 	SND_DJM_CTL("Input 1 Capture Switch", 750_cap1, 2, SND_DJM_WINDEX_CAP),
4117 	SND_DJM_CTL("Input 2 Capture Switch", 750_cap2, 2, SND_DJM_WINDEX_CAP),
4118 	SND_DJM_CTL("Input 3 Capture Switch", 750_cap3, 0, SND_DJM_WINDEX_CAP),
4119 	SND_DJM_CTL("Input 4 Capture Switch", 750_cap4, 0, SND_DJM_WINDEX_CAP)
4120 };
4121 
4122 // DJM-850
4123 static const u16 snd_djm_opts_850_cap1[] = {
4124 	0x0100, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a, 0x010f };
4125 static const u16 snd_djm_opts_850_cap2[] = {
4126 	0x0200, 0x0201, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020f };
4127 static const u16 snd_djm_opts_850_cap3[] = {
4128 	0x0300, 0x0301, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030f };
4129 static const u16 snd_djm_opts_850_cap4[] = {
4130 	0x0400, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040f };
4131 
4132 static const struct snd_djm_ctl snd_djm_ctls_850[] = {
4133 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4134 	SND_DJM_CTL("Input 1 Capture Switch", 850_cap1, 1, SND_DJM_WINDEX_CAP),
4135 	SND_DJM_CTL("Input 2 Capture Switch", 850_cap2, 0, SND_DJM_WINDEX_CAP),
4136 	SND_DJM_CTL("Input 3 Capture Switch", 850_cap3, 0, SND_DJM_WINDEX_CAP),
4137 	SND_DJM_CTL("Input 4 Capture Switch", 850_cap4, 1, SND_DJM_WINDEX_CAP)
4138 };
4139 
4140 // DJM-900NXS2
4141 static const u16 snd_djm_opts_900nxs2_cap1[] = {
4142 	0x0100, 0x0102, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a };
4143 static const u16 snd_djm_opts_900nxs2_cap2[] = {
4144 	0x0200, 0x0202, 0x0203, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a };
4145 static const u16 snd_djm_opts_900nxs2_cap3[] = {
4146 	0x0300, 0x0302, 0x0303, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a };
4147 static const u16 snd_djm_opts_900nxs2_cap4[] = {
4148 	0x0400, 0x0402, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a };
4149 static const u16 snd_djm_opts_900nxs2_cap5[] = {
4150 	0x0507, 0x0508, 0x0509, 0x050a, 0x0511, 0x0512, 0x0513, 0x0514 };
4151 
4152 static const struct snd_djm_ctl snd_djm_ctls_900nxs2[] = {
4153 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4154 	SND_DJM_CTL("Input 1 Capture Switch", 900nxs2_cap1, 2, SND_DJM_WINDEX_CAP),
4155 	SND_DJM_CTL("Input 2 Capture Switch", 900nxs2_cap2, 2, SND_DJM_WINDEX_CAP),
4156 	SND_DJM_CTL("Input 3 Capture Switch", 900nxs2_cap3, 2, SND_DJM_WINDEX_CAP),
4157 	SND_DJM_CTL("Input 4 Capture Switch", 900nxs2_cap4, 2, SND_DJM_WINDEX_CAP),
4158 	SND_DJM_CTL("Input 5 Capture Switch", 900nxs2_cap5, 3, SND_DJM_WINDEX_CAP)
4159 };
4160 
4161 // DJM-750MK2
4162 static const u16 snd_djm_opts_750mk2_cap1[] = {
4163 	0x0100, 0x0102, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a };
4164 static const u16 snd_djm_opts_750mk2_cap2[] = {
4165 	0x0200, 0x0202, 0x0203, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a };
4166 static const u16 snd_djm_opts_750mk2_cap3[] = {
4167 	0x0300, 0x0302, 0x0303, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a };
4168 static const u16 snd_djm_opts_750mk2_cap4[] = {
4169 	0x0400, 0x0402, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a };
4170 static const u16 snd_djm_opts_750mk2_cap5[] = {
4171 	0x0507, 0x0508, 0x0509, 0x050a, 0x0511, 0x0512, 0x0513, 0x0514 };
4172 
4173 static const u16 snd_djm_opts_750mk2_pb1[] = { 0x0100, 0x0101, 0x0104 };
4174 static const u16 snd_djm_opts_750mk2_pb2[] = { 0x0200, 0x0201, 0x0204 };
4175 static const u16 snd_djm_opts_750mk2_pb3[] = { 0x0300, 0x0301, 0x0304 };
4176 
4177 static const struct snd_djm_ctl snd_djm_ctls_750mk2[] = {
4178 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4179 	SND_DJM_CTL("Input 1 Capture Switch",   750mk2_cap1, 2, SND_DJM_WINDEX_CAP),
4180 	SND_DJM_CTL("Input 2 Capture Switch",   750mk2_cap2, 2, SND_DJM_WINDEX_CAP),
4181 	SND_DJM_CTL("Input 3 Capture Switch",   750mk2_cap3, 2, SND_DJM_WINDEX_CAP),
4182 	SND_DJM_CTL("Input 4 Capture Switch",   750mk2_cap4, 2, SND_DJM_WINDEX_CAP),
4183 	SND_DJM_CTL("Input 5 Capture Switch",   750mk2_cap5, 3, SND_DJM_WINDEX_CAP),
4184 	SND_DJM_CTL("Output 1 Playback Switch", 750mk2_pb1, 0, SND_DJM_WINDEX_PB),
4185 	SND_DJM_CTL("Output 2 Playback Switch", 750mk2_pb2, 1, SND_DJM_WINDEX_PB),
4186 	SND_DJM_CTL("Output 3 Playback Switch", 750mk2_pb3, 2, SND_DJM_WINDEX_PB)
4187 };
4188 
4189 // DJM-A9
4190 static const u16 snd_djm_opts_a9_cap_level[] = {
4191 	0x0000, 0x0100, 0x0200, 0x0300, 0x0400, 0x0500 };
4192 static const u16 snd_djm_opts_a9_cap1[] = {
4193 	0x0107, 0x0108, 0x0109, 0x010a, 0x010e,
4194 	0x111, 0x112, 0x113, 0x114, 0x0131, 0x132, 0x133, 0x134 };
4195 static const u16 snd_djm_opts_a9_cap2[] = {
4196 	0x0201, 0x0202, 0x0203, 0x0205, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020e };
4197 static const u16 snd_djm_opts_a9_cap3[] = {
4198 	0x0301, 0x0302, 0x0303, 0x0305, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030e };
4199 static const u16 snd_djm_opts_a9_cap4[] = {
4200 	0x0401, 0x0402, 0x0403, 0x0405, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040e };
4201 static const u16 snd_djm_opts_a9_cap5[] = {
4202 	0x0501, 0x0502, 0x0503, 0x0505, 0x0506, 0x0507, 0x0508, 0x0509, 0x050a, 0x050e };
4203 
4204 static const struct snd_djm_ctl snd_djm_ctls_a9[] = {
4205 	SND_DJM_CTL("Master Input Level Capture Switch", a9_cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4206 	SND_DJM_CTL("Master Input Capture Switch", a9_cap1, 3, SND_DJM_WINDEX_CAP),
4207 	SND_DJM_CTL("Input 1 Capture Switch",  a9_cap2, 2, SND_DJM_WINDEX_CAP),
4208 	SND_DJM_CTL("Input 2 Capture Switch",  a9_cap3, 2, SND_DJM_WINDEX_CAP),
4209 	SND_DJM_CTL("Input 3 Capture Switch",  a9_cap4, 2, SND_DJM_WINDEX_CAP),
4210 	SND_DJM_CTL("Input 4 Capture Switch",  a9_cap5, 2, SND_DJM_WINDEX_CAP)
4211 };
4212 
4213 // DJM-V10
4214 static const u16 snd_djm_opts_v10_cap_level[] = {
4215 	0x0000, 0x0100, 0x0200, 0x0300, 0x0400, 0x0500
4216 };
4217 
4218 static const u16 snd_djm_opts_v10_cap1[] = {
4219 	0x0103,
4220 	0x0100, 0x0102, 0x0106, 0x0110, 0x0107,
4221 	0x0108, 0x0109, 0x010a, 0x0121, 0x0122
4222 };
4223 
4224 static const u16 snd_djm_opts_v10_cap2[] = {
4225 	0x0200, 0x0202, 0x0206, 0x0210, 0x0207,
4226 	0x0208, 0x0209, 0x020a, 0x0221, 0x0222
4227 };
4228 
4229 static const u16 snd_djm_opts_v10_cap3[] = {
4230 	0x0303,
4231 	0x0300, 0x0302, 0x0306, 0x0310, 0x0307,
4232 	0x0308, 0x0309, 0x030a, 0x0321, 0x0322
4233 };
4234 
4235 static const u16 snd_djm_opts_v10_cap4[] = {
4236 	0x0403,
4237 	0x0400, 0x0402, 0x0406, 0x0410, 0x0407,
4238 	0x0408, 0x0409, 0x040a, 0x0421, 0x0422
4239 };
4240 
4241 static const u16 snd_djm_opts_v10_cap5[] = {
4242 	0x0500, 0x0502, 0x0506, 0x0510, 0x0507,
4243 	0x0508, 0x0509, 0x050a, 0x0521, 0x0522
4244 };
4245 
4246 static const u16 snd_djm_opts_v10_cap6[] = {
4247 	0x0603,
4248 	0x0600, 0x0602, 0x0606, 0x0610, 0x0607,
4249 	0x0608, 0x0609, 0x060a, 0x0621, 0x0622
4250 };
4251 
4252 static const struct snd_djm_ctl snd_djm_ctls_v10[] = {
4253 	SND_DJM_CTL("Master Input Level Capture Switch", v10_cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4254 	SND_DJM_CTL("Input 1 Capture Switch", v10_cap1, 2, SND_DJM_WINDEX_CAP),
4255 	SND_DJM_CTL("Input 2 Capture Switch", v10_cap2, 2, SND_DJM_WINDEX_CAP),
4256 	SND_DJM_CTL("Input 3 Capture Switch", v10_cap3, 0, SND_DJM_WINDEX_CAP),
4257 	SND_DJM_CTL("Input 4 Capture Switch", v10_cap4, 0, SND_DJM_WINDEX_CAP),
4258 	SND_DJM_CTL("Input 5 Capture Switch", v10_cap5, 0, SND_DJM_WINDEX_CAP),
4259 	SND_DJM_CTL("Input 6 Capture Switch", v10_cap6, 0, SND_DJM_WINDEX_CAP)
4260 	// playback channels are fixed and controlled by hardware knobs on the mixer
4261 };
4262 
4263 static const struct snd_djm_device snd_djm_devices[] = {
4264 	[SND_DJM_250MK2_IDX] = SND_DJM_DEVICE(250mk2),
4265 	[SND_DJM_750_IDX] = SND_DJM_DEVICE(750),
4266 	[SND_DJM_850_IDX] = SND_DJM_DEVICE(850),
4267 	[SND_DJM_900NXS2_IDX] = SND_DJM_DEVICE(900nxs2),
4268 	[SND_DJM_750MK2_IDX] = SND_DJM_DEVICE(750mk2),
4269 	[SND_DJM_450_IDX] = SND_DJM_DEVICE(450),
4270 	[SND_DJM_A9_IDX] = SND_DJM_DEVICE(a9),
4271 	[SND_DJM_V10_IDX] = SND_DJM_DEVICE(v10),
4272 };
4273 
4274 static int snd_djm_controls_info(struct snd_kcontrol *kctl,
4275 				 struct snd_ctl_elem_info *info)
4276 {
4277 	unsigned long private_value = kctl->private_value;
4278 	u8 device_idx = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
4279 	u8 ctl_idx = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
4280 	const struct snd_djm_device *device = &snd_djm_devices[device_idx];
4281 	const char *name;
4282 	const struct snd_djm_ctl *ctl;
4283 	size_t noptions;
4284 
4285 	if (ctl_idx >= device->ncontrols)
4286 		return -EINVAL;
4287 
4288 	ctl = &device->controls[ctl_idx];
4289 	noptions = ctl->noptions;
4290 	if (info->value.enumerated.item >= noptions)
4291 		info->value.enumerated.item = noptions - 1;
4292 
4293 	name = snd_djm_get_label(device_idx,
4294 				 ctl->options[info->value.enumerated.item],
4295 				 ctl->wIndex);
4296 	if (!name)
4297 		return -EINVAL;
4298 
4299 	strscpy(info->value.enumerated.name, name, sizeof(info->value.enumerated.name));
4300 	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4301 	info->count = 1;
4302 	info->value.enumerated.items = noptions;
4303 	return 0;
4304 }
4305 
4306 static int snd_djm_controls_update(struct usb_mixer_interface *mixer,
4307 				   u8 device_idx, u8 group, u16 value)
4308 {
4309 	const struct snd_djm_device *device = &snd_djm_devices[device_idx];
4310 
4311 	if (group >= device->ncontrols || value >= device->controls[group].noptions)
4312 		return -EINVAL;
4313 
4314 	CLASS(snd_usb_lock, pm)(mixer->chip);
4315 	if (pm.err)
4316 		return pm.err;
4317 
4318 	return snd_usb_ctl_msg(mixer->chip->dev,
4319 			       usb_sndctrlpipe(mixer->chip->dev, 0),
4320 			       USB_REQ_SET_FEATURE,
4321 			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
4322 			       device->controls[group].options[value],
4323 			       device->controls[group].wIndex,
4324 			       NULL, 0);
4325 }
4326 
4327 static int snd_djm_controls_get(struct snd_kcontrol *kctl,
4328 				struct snd_ctl_elem_value *elem)
4329 {
4330 	elem->value.enumerated.item[0] = kctl->private_value & SND_DJM_VALUE_MASK;
4331 	return 0;
4332 }
4333 
4334 static int snd_djm_controls_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *elem)
4335 {
4336 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
4337 	struct usb_mixer_interface *mixer = list->mixer;
4338 	unsigned long private_value = kctl->private_value;
4339 
4340 	u8 device = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
4341 	u8 group = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
4342 	u16 value = elem->value.enumerated.item[0];
4343 
4344 	kctl->private_value = (((unsigned long)device << SND_DJM_DEVICE_SHIFT) |
4345 			      (group << SND_DJM_GROUP_SHIFT) |
4346 			      value);
4347 
4348 	return snd_djm_controls_update(mixer, device, group, value);
4349 }
4350 
4351 static int snd_djm_controls_resume(struct usb_mixer_elem_list *list)
4352 {
4353 	unsigned long private_value = list->kctl->private_value;
4354 	u8 device = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
4355 	u8 group = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
4356 	u16 value = (private_value & SND_DJM_VALUE_MASK);
4357 
4358 	return snd_djm_controls_update(list->mixer, device, group, value);
4359 }
4360 
4361 static int snd_djm_controls_create(struct usb_mixer_interface *mixer,
4362 				   const u8 device_idx)
4363 {
4364 	int err, i;
4365 	u16 value;
4366 
4367 	const struct snd_djm_device *device = &snd_djm_devices[device_idx];
4368 
4369 	struct snd_kcontrol_new knew = {
4370 		.iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
4371 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
4372 		.index = 0,
4373 		.info = snd_djm_controls_info,
4374 		.get  = snd_djm_controls_get,
4375 		.put  = snd_djm_controls_put
4376 	};
4377 
4378 	for (i = 0; i < device->ncontrols; i++) {
4379 		value = device->controls[i].default_value;
4380 		knew.name = device->controls[i].name;
4381 		knew.private_value =
4382 			((unsigned long)device_idx << SND_DJM_DEVICE_SHIFT) |
4383 			(i << SND_DJM_GROUP_SHIFT) |
4384 			value;
4385 		err = snd_djm_controls_update(mixer, device_idx, i, value);
4386 		if (err)
4387 			return err;
4388 		err = add_single_ctl_with_resume(mixer, 0, snd_djm_controls_resume,
4389 						 &knew, NULL);
4390 		if (err)
4391 			return err;
4392 	}
4393 	return 0;
4394 }
4395 
4396 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
4397 {
4398 	int err = 0;
4399 
4400 	err = snd_usb_soundblaster_remote_init(mixer);
4401 	if (err < 0)
4402 		return err;
4403 
4404 	switch (mixer->chip->usb_id) {
4405 	/* Tascam US-16x08 */
4406 	case USB_ID(0x0644, 0x8047):
4407 		err = snd_us16x08_controls_create(mixer);
4408 		break;
4409 	case USB_ID(0x041e, 0x3020):
4410 	case USB_ID(0x041e, 0x3040):
4411 	case USB_ID(0x041e, 0x3042):
4412 	case USB_ID(0x041e, 0x30df):
4413 	case USB_ID(0x041e, 0x3048):
4414 		err = snd_audigy2nx_controls_create(mixer);
4415 		if (err < 0)
4416 			break;
4417 		snd_card_ro_proc_new(mixer->chip->card, "audigy2nx",
4418 				     mixer, snd_audigy2nx_proc_read);
4419 		break;
4420 
4421 	/* EMU0204 */
4422 	case USB_ID(0x041e, 0x3f19):
4423 		err = snd_emu0204_controls_create(mixer);
4424 		break;
4425 
4426 #if IS_REACHABLE(CONFIG_INPUT)
4427 	case USB_ID(0x054c, 0x0ce6): /* Sony DualSense controller (PS5) */
4428 	case USB_ID(0x054c, 0x0df2): /* Sony DualSense Edge controller (PS5) */
4429 		err = snd_dualsense_controls_create(mixer);
4430 		break;
4431 #endif /* IS_REACHABLE(CONFIG_INPUT) */
4432 
4433 	case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
4434 	case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
4435 		err = snd_c400_create_mixer(mixer);
4436 		break;
4437 
4438 	case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
4439 	case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
4440 		err = snd_ftu_create_mixer(mixer);
4441 		break;
4442 
4443 	case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
4444 	case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
4445 	case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
4446 		err = snd_xonar_u1_controls_create(mixer);
4447 		break;
4448 
4449 	case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
4450 		err = snd_microii_controls_create(mixer);
4451 		break;
4452 
4453 	case USB_ID(0x0dba, 0x1000): /* Digidesign Mbox 1 */
4454 		err = snd_mbox1_controls_create(mixer);
4455 		break;
4456 
4457 	case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
4458 		err = snd_nativeinstruments_create_mixer(/* checkpatch hack */
4459 				mixer,
4460 				snd_nativeinstruments_ta6_mixers,
4461 				ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
4462 		break;
4463 
4464 	case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
4465 		err = snd_nativeinstruments_create_mixer(/* checkpatch hack */
4466 				mixer,
4467 				snd_nativeinstruments_ta10_mixers,
4468 				ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
4469 		break;
4470 
4471 	case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
4472 		/* detection is disabled in mixer_maps.c */
4473 		err = snd_create_std_mono_table(mixer, ebox44_table);
4474 		break;
4475 
4476 	case USB_ID(0x1235, 0x8010): /* Focusrite Forte */
4477 		err = snd_forte_controls_create(mixer);
4478 		break;
4479 	case USB_ID(0x1235, 0x8012): /* Focusrite Scarlett 6i6 */
4480 	case USB_ID(0x1235, 0x8002): /* Focusrite Scarlett 8i6 */
4481 	case USB_ID(0x1235, 0x8004): /* Focusrite Scarlett 18i6 */
4482 	case USB_ID(0x1235, 0x8014): /* Focusrite Scarlett 18i8 */
4483 	case USB_ID(0x1235, 0x800c): /* Focusrite Scarlett 18i20 */
4484 		err = snd_scarlett_controls_create(mixer);
4485 		break;
4486 
4487 	case USB_ID(0x1235, 0x8203): /* Focusrite Scarlett 6i6 2nd Gen */
4488 	case USB_ID(0x1235, 0x8204): /* Focusrite Scarlett 18i8 2nd Gen */
4489 	case USB_ID(0x1235, 0x8201): /* Focusrite Scarlett 18i20 2nd Gen */
4490 	case USB_ID(0x1235, 0x8211): /* Focusrite Scarlett Solo 3rd Gen */
4491 	case USB_ID(0x1235, 0x8210): /* Focusrite Scarlett 2i2 3rd Gen */
4492 	case USB_ID(0x1235, 0x8212): /* Focusrite Scarlett 4i4 3rd Gen */
4493 	case USB_ID(0x1235, 0x8213): /* Focusrite Scarlett 8i6 3rd Gen */
4494 	case USB_ID(0x1235, 0x8214): /* Focusrite Scarlett 18i8 3rd Gen */
4495 	case USB_ID(0x1235, 0x8215): /* Focusrite Scarlett 18i20 3rd Gen */
4496 	case USB_ID(0x1235, 0x8216): /* Focusrite Vocaster One */
4497 	case USB_ID(0x1235, 0x8217): /* Focusrite Vocaster Two */
4498 	case USB_ID(0x1235, 0x8218): /* Focusrite Scarlett Solo 4th Gen */
4499 	case USB_ID(0x1235, 0x8219): /* Focusrite Scarlett 2i2 4th Gen */
4500 	case USB_ID(0x1235, 0x821a): /* Focusrite Scarlett 4i4 4th Gen */
4501 	case USB_ID(0x1235, 0x8206): /* Focusrite Clarett 2Pre USB */
4502 	case USB_ID(0x1235, 0x8207): /* Focusrite Clarett 4Pre USB */
4503 	case USB_ID(0x1235, 0x8208): /* Focusrite Clarett 8Pre USB */
4504 	case USB_ID(0x1235, 0x820a): /* Focusrite Clarett+ 2Pre */
4505 	case USB_ID(0x1235, 0x820b): /* Focusrite Clarett+ 4Pre */
4506 	case USB_ID(0x1235, 0x820c): /* Focusrite Clarett+ 8Pre */
4507 		err = snd_scarlett2_init(mixer);
4508 		break;
4509 
4510 	case USB_ID(0x1235, 0x821b): /* Focusrite Scarlett 16i16 4th Gen */
4511 	case USB_ID(0x1235, 0x821c): /* Focusrite Scarlett 18i16 4th Gen */
4512 	case USB_ID(0x1235, 0x821d): /* Focusrite Scarlett 18i20 4th Gen */
4513 	case USB_ID(0x1235, 0x821e): /* Focusrite ISA C8X */
4514 		err = snd_fcp_init(mixer);
4515 		break;
4516 
4517 	case USB_ID(0x041e, 0x323b): /* Creative Sound Blaster E1 */
4518 		err = snd_soundblaster_e1_switch_create(mixer);
4519 		break;
4520 	case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
4521 		err = dell_dock_mixer_create(mixer);
4522 		if (err < 0)
4523 			break;
4524 		err = dell_dock_mixer_init(mixer);
4525 		break;
4526 	case USB_ID(0x0bda, 0x402e): /* Dell WD19 dock */
4527 		err = dell_dock_mixer_create(mixer);
4528 		break;
4529 
4530 	case USB_ID(0x2a39, 0x3fd2): /* RME ADI-2 Pro */
4531 	case USB_ID(0x2a39, 0x3fd3): /* RME ADI-2 DAC */
4532 	case USB_ID(0x2a39, 0x3fd4): /* RME */
4533 		err = snd_rme_controls_create(mixer);
4534 		break;
4535 
4536 	case USB_ID(0x194f, 0x010c): /* Presonus Studio 1810c */
4537 		err = snd_sc1810_init_mixer(mixer);
4538 		break;
4539 	case USB_ID(0x194f, 0x010d): /* Presonus Studio 1824c */
4540 		err = snd_sc1810_init_mixer(mixer);
4541 		break;
4542 	case USB_ID(0x194f, 0x0107): /* Presonus Studio 1824 */
4543 		err = snd_sc1810_init_mixer(mixer);
4544 		break;
4545 	case USB_ID(0x2a39, 0x3fb0): /* RME Babyface Pro FS */
4546 		err = snd_bbfpro_controls_create(mixer);
4547 		break;
4548 	case USB_ID(0x2a39, 0x3f8c): /* RME Digiface USB */
4549 	case USB_ID(0x2a39, 0x3fa0): /* RME Digiface USB (alternate) */
4550 		err = snd_rme_digiface_controls_create(mixer);
4551 		break;
4552 	case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */
4553 		err = snd_djm_controls_create(mixer, SND_DJM_250MK2_IDX);
4554 		break;
4555 	case USB_ID(0x2b73, 0x0013): /* Pioneer DJ DJM-450 */
4556 		err = snd_djm_controls_create(mixer, SND_DJM_450_IDX);
4557 		break;
4558 	case USB_ID(0x08e4, 0x017f): /* Pioneer DJ DJM-750 */
4559 		err = snd_djm_controls_create(mixer, SND_DJM_750_IDX);
4560 		break;
4561 	case USB_ID(0x2b73, 0x001b): /* Pioneer DJ DJM-750MK2 */
4562 		err = snd_djm_controls_create(mixer, SND_DJM_750MK2_IDX);
4563 		break;
4564 	case USB_ID(0x08e4, 0x0163): /* Pioneer DJ DJM-850 */
4565 		err = snd_djm_controls_create(mixer, SND_DJM_850_IDX);
4566 		break;
4567 	case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */
4568 		err = snd_djm_controls_create(mixer, SND_DJM_900NXS2_IDX);
4569 		break;
4570 	case USB_ID(0x2b73, 0x003c): /* Pioneer DJ / AlphaTheta DJM-A9 */
4571 		err = snd_djm_controls_create(mixer, SND_DJM_A9_IDX);
4572 		break;
4573 	case USB_ID(0x2b73, 0x0034): /* Pioneer DJ DJM-V10 */
4574 		err = snd_djm_controls_create(mixer, SND_DJM_V10_IDX);
4575 		break;
4576 	case USB_ID(0x03f0, 0x0269): /* HP TB Dock G2 */
4577 		err = hp_dock_mixer_create(mixer);
4578 		break;
4579 	}
4580 
4581 	return err;
4582 }
4583 
4584 void snd_usb_mixer_resume_quirk(struct usb_mixer_interface *mixer)
4585 {
4586 	switch (mixer->chip->usb_id) {
4587 	case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
4588 		dell_dock_mixer_init(mixer);
4589 		break;
4590 	}
4591 }
4592 
4593 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
4594 				    int unitid)
4595 {
4596 	if (!mixer->rc_cfg)
4597 		return;
4598 	/* unit ids specific to Extigy/Audigy 2 NX: */
4599 	switch (unitid) {
4600 	case 0: /* remote control */
4601 		mixer->rc_urb->dev = mixer->chip->dev;
4602 		usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
4603 		break;
4604 	case 4: /* digital in jack */
4605 	case 7: /* line in jacks */
4606 	case 19: /* speaker out jacks */
4607 	case 20: /* headphones out jack */
4608 		break;
4609 	/* live24ext: 4 = line-in jack */
4610 	case 3:	/* hp-out jack (may actuate Mute) */
4611 		if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
4612 		    mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
4613 			snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
4614 		break;
4615 	default:
4616 		usb_audio_dbg(mixer->chip, "memory change in unknown unit %d\n", unitid);
4617 		break;
4618 	}
4619 }
4620 
4621 static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
4622 					 struct usb_mixer_elem_info *cval,
4623 					 struct snd_kcontrol *kctl)
4624 {
4625 	/* Approximation using 10 ranges based on output measurement on hw v1.2.
4626 	 * This seems close to the cubic mapping e.g. alsamixer uses.
4627 	 */
4628 	static const DECLARE_TLV_DB_RANGE(scale,
4629 		 0,  1, TLV_DB_MINMAX_ITEM(-5300, -4970),
4630 		 2,  5, TLV_DB_MINMAX_ITEM(-4710, -4160),
4631 		 6,  7, TLV_DB_MINMAX_ITEM(-3884, -3710),
4632 		 8, 14, TLV_DB_MINMAX_ITEM(-3443, -2560),
4633 		15, 16, TLV_DB_MINMAX_ITEM(-2475, -2324),
4634 		17, 19, TLV_DB_MINMAX_ITEM(-2228, -2031),
4635 		20, 26, TLV_DB_MINMAX_ITEM(-1910, -1393),
4636 		27, 31, TLV_DB_MINMAX_ITEM(-1322, -1032),
4637 		32, 40, TLV_DB_MINMAX_ITEM(-968, -490),
4638 		41, 50, TLV_DB_MINMAX_ITEM(-441, 0),
4639 	);
4640 
4641 	if (cval->min == 0 && cval->max == 50) {
4642 		usb_audio_info(mixer->chip, "applying DragonFly dB scale quirk (0-50 variant)\n");
4643 		kctl->tlv.p = scale;
4644 		kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
4645 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
4646 
4647 	} else if (cval->min == 0 && cval->max <= 1000) {
4648 		/* Some other clearly broken DragonFly variant.
4649 		 * At least a 0..53 variant (hw v1.0) exists.
4650 		 */
4651 		usb_audio_info(mixer->chip, "ignoring too narrow dB range on a DragonFly device");
4652 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
4653 	}
4654 }
4655 
4656 static void snd_usb_mv_silicon_quirks(struct usb_mixer_interface *mixer,
4657 				      struct usb_mixer_elem_info *cval,
4658 				      struct snd_kcontrol *kctl)
4659 {
4660 	if (cval->min == 0 && cval->max == 4096 && cval->res == 1) {
4661 		/* The final effects will be printed later. */
4662 		usb_audio_info(mixer->chip, "applying MV-SILICON quirks (0/4096/1 variant)\n");
4663 
4664 		/* Respect MIN_MUTE set by module parameters. */
4665 		if (!(mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE))
4666 			mixer->chip->quirk_flags |= QUIRK_FLAG_MIXER_PLAYBACK_LINEAR_VOL;
4667 		if (!(mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE))
4668 			mixer->chip->quirk_flags |= QUIRK_FLAG_MIXER_CAPTURE_LINEAR_VOL;
4669 	} else {
4670 		usb_audio_dbg(mixer->chip, "not applying MV-SILICON quirks on unknown variant");
4671 	}
4672 }
4673 
4674 /*
4675  * Some Plantronics headsets have control names that don't meet ALSA naming
4676  * standards. This function fixes nonstandard source names. By the time
4677  * this function is called the control name should look like one of these:
4678  * "source names Playback Volume"
4679  * "source names Playback Switch"
4680  * "source names Capture Volume"
4681  * "source names Capture Switch"
4682  * If any of the trigger words are found in the name then the name will
4683  * be changed to:
4684  * "Headset Playback Volume"
4685  * "Headset Playback Switch"
4686  * "Headset Capture Volume"
4687  * "Headset Capture Switch"
4688  * depending on the current suffix.
4689  */
4690 static void snd_fix_plt_name(struct snd_usb_audio *chip,
4691 			     struct snd_ctl_elem_id *id)
4692 {
4693 	/* no variant of "Sidetone" should be added to this list */
4694 	static const char * const trigger[] = {
4695 		"Earphone", "Microphone", "Receive", "Transmit"
4696 	};
4697 	static const char * const suffix[] = {
4698 		" Playback Volume", " Playback Switch",
4699 		" Capture Volume", " Capture Switch"
4700 	};
4701 	int i;
4702 
4703 	for (i = 0; i < ARRAY_SIZE(trigger); i++)
4704 		if (strstr(id->name, trigger[i]))
4705 			goto triggered;
4706 	usb_audio_dbg(chip, "no change in %s\n", id->name);
4707 	return;
4708 
4709 triggered:
4710 	for (i = 0; i < ARRAY_SIZE(suffix); i++)
4711 		if (strstr(id->name, suffix[i])) {
4712 			usb_audio_dbg(chip, "fixing kctl name %s\n", id->name);
4713 			snprintf(id->name, sizeof(id->name), "Headset%s",
4714 				 suffix[i]);
4715 			return;
4716 		}
4717 	usb_audio_dbg(chip, "something wrong in kctl name %s\n", id->name);
4718 }
4719 
4720 static void snd_usb_mixer_fu_quirk_linear_scale(struct usb_mixer_interface *mixer,
4721 						struct usb_mixer_elem_info *cval,
4722 						struct snd_kcontrol *kctl)
4723 {
4724 	static const DECLARE_TLV_DB_LINEAR(scale, TLV_DB_GAIN_MUTE, 0);
4725 
4726 	if (cval->min_mute) {
4727 		/*
4728 		 * We are clearing SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
4729 		 * resulting in min_mute being a no-op.
4730 		 */
4731 		usb_audio_warn(mixer->chip, "LINEAR_VOL overrides MIN_MUTE\n");
4732 	}
4733 
4734 	kctl->tlv.p = scale;
4735 	kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
4736 	kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
4737 }
4738 
4739 void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
4740 				  struct usb_mixer_elem_info *cval, int unitid,
4741 				  struct snd_kcontrol *kctl)
4742 {
4743 	switch (mixer->chip->usb_id) {
4744 	case USB_ID(0x21b4, 0x0081): /* AudioQuest DragonFly */
4745 		if (unitid == 7 && cval->control == UAC_FU_VOLUME)
4746 			snd_dragonfly_quirk_db_scale(mixer, cval, kctl);
4747 		break;
4748 	}
4749 
4750 	if (cval->control == UAC_FU_VOLUME &&
4751 	    !strncmp(mixer->chip->card->longname, "MV-SILICON", 10))
4752 		snd_usb_mv_silicon_quirks(mixer, cval, kctl);
4753 
4754 	/* lowest playback value is muted on some devices */
4755 	if (mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE)
4756 		if (strstr(kctl->id.name, "Playback")) {
4757 			usb_audio_info(mixer->chip,
4758 				       "applying playback min mute quirk\n");
4759 			cval->min_mute = 1;
4760 		}
4761 
4762 	/* lowest capture value is muted on some devices */
4763 	if (mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE)
4764 		if (strstr(kctl->id.name, "Capture")) {
4765 			usb_audio_info(mixer->chip,
4766 				       "applying capture min mute quirk\n");
4767 			cval->min_mute = 1;
4768 		}
4769 
4770 	if (mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_PLAYBACK_LINEAR_VOL)
4771 		if (cval->control == UAC_FU_VOLUME && strstr(kctl->id.name, "Playback")) {
4772 			usb_audio_info(mixer->chip,
4773 				       "applying playback linear volume quirk\n");
4774 			snd_usb_mixer_fu_quirk_linear_scale(mixer, cval, kctl);
4775 		}
4776 
4777 	if (mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_CAPTURE_LINEAR_VOL)
4778 		if (cval->control == UAC_FU_VOLUME && strstr(kctl->id.name, "Capture")) {
4779 			usb_audio_info(mixer->chip,
4780 				       "applying capture linear volume quirk\n");
4781 			snd_usb_mixer_fu_quirk_linear_scale(mixer, cval, kctl);
4782 		}
4783 
4784 	/* ALSA-ify some Plantronics headset control names */
4785 	if (USB_ID_VENDOR(mixer->chip->usb_id) == 0x047f &&
4786 	    (cval->control == UAC_FU_MUTE || cval->control == UAC_FU_VOLUME))
4787 		snd_fix_plt_name(mixer->chip, &kctl->id);
4788 }
4789