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