xref: /linux/sound/usb/mixer_quirks.c (revision 2aa680df68062e4e0c356ec2aa7100c13654907b)
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 	CLASS(snd_usb_lock, pm)(chip);
2549 	if (pm.err < 0)
2550 		return pm.err;
2551 	return snd_rme_read_value(chip, SND_RME_GET_STATUS1, status1);
2552 }
2553 
2554 static int snd_rme_rate_get(struct snd_kcontrol *kcontrol,
2555 			    struct snd_ctl_elem_value *ucontrol)
2556 {
2557 	u32 status1;
2558 	u32 rate = 0;
2559 	int idx;
2560 	int err;
2561 
2562 	err = snd_rme_get_status1(kcontrol, &status1);
2563 	if (err < 0)
2564 		return err;
2565 	switch (kcontrol->private_value) {
2566 	case SND_RME_DOMAIN_SYSTEM:
2567 		idx = SND_RME_CLK_SYSTEM(status1);
2568 		if (idx < ARRAY_SIZE(snd_rme_rate_table))
2569 			rate = snd_rme_rate_table[idx];
2570 		break;
2571 	case SND_RME_DOMAIN_AES:
2572 		idx = SND_RME_CLK_AES(status1);
2573 		if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
2574 			rate = snd_rme_rate_table[idx];
2575 		break;
2576 	case SND_RME_DOMAIN_SPDIF:
2577 		idx = SND_RME_CLK_SPDIF(status1);
2578 		if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
2579 			rate = snd_rme_rate_table[idx];
2580 		break;
2581 	default:
2582 		return -EINVAL;
2583 	}
2584 	ucontrol->value.integer.value[0] = rate;
2585 	return 0;
2586 }
2587 
2588 static int snd_rme_sync_state_get(struct snd_kcontrol *kcontrol,
2589 				  struct snd_ctl_elem_value *ucontrol)
2590 {
2591 	u32 status1;
2592 	int idx = SND_RME_CLOCK_NOLOCK;
2593 	int err;
2594 
2595 	err = snd_rme_get_status1(kcontrol, &status1);
2596 	if (err < 0)
2597 		return err;
2598 	switch (kcontrol->private_value) {
2599 	case SND_RME_DOMAIN_AES:  /* AES */
2600 		if (status1 & SND_RME_CLK_AES_SYNC)
2601 			idx = SND_RME_CLOCK_SYNC;
2602 		else if (status1 & SND_RME_CLK_AES_LOCK)
2603 			idx = SND_RME_CLOCK_LOCK;
2604 		break;
2605 	case SND_RME_DOMAIN_SPDIF:  /* SPDIF */
2606 		if (status1 & SND_RME_CLK_SPDIF_SYNC)
2607 			idx = SND_RME_CLOCK_SYNC;
2608 		else if (status1 & SND_RME_CLK_SPDIF_LOCK)
2609 			idx = SND_RME_CLOCK_LOCK;
2610 		break;
2611 	default:
2612 		return -EINVAL;
2613 	}
2614 	ucontrol->value.enumerated.item[0] = idx;
2615 	return 0;
2616 }
2617 
2618 static int snd_rme_spdif_if_get(struct snd_kcontrol *kcontrol,
2619 				struct snd_ctl_elem_value *ucontrol)
2620 {
2621 	u32 status1;
2622 	int err;
2623 
2624 	err = snd_rme_get_status1(kcontrol, &status1);
2625 	if (err < 0)
2626 		return err;
2627 	ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_IF(status1);
2628 	return 0;
2629 }
2630 
2631 static int snd_rme_spdif_format_get(struct snd_kcontrol *kcontrol,
2632 				    struct snd_ctl_elem_value *ucontrol)
2633 {
2634 	u32 status1;
2635 	int err;
2636 
2637 	err = snd_rme_get_status1(kcontrol, &status1);
2638 	if (err < 0)
2639 		return err;
2640 	ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_FORMAT(status1);
2641 	return 0;
2642 }
2643 
2644 static int snd_rme_sync_source_get(struct snd_kcontrol *kcontrol,
2645 				   struct snd_ctl_elem_value *ucontrol)
2646 {
2647 	u32 status1;
2648 	int err;
2649 
2650 	err = snd_rme_get_status1(kcontrol, &status1);
2651 	if (err < 0)
2652 		return err;
2653 	ucontrol->value.enumerated.item[0] = SND_RME_CLK_SYNC(status1);
2654 	return 0;
2655 }
2656 
2657 static int snd_rme_current_freq_get(struct snd_kcontrol *kcontrol,
2658 				    struct snd_ctl_elem_value *ucontrol)
2659 {
2660 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2661 	struct snd_usb_audio *chip = list->mixer->chip;
2662 	u32 status1;
2663 	const u64 num = 104857600000000ULL;
2664 	u32 den;
2665 	unsigned int freq;
2666 	int err;
2667 
2668 	CLASS(snd_usb_lock, pm)(chip);
2669 	if (pm.err < 0)
2670 		return pm.err;
2671 	err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, &status1);
2672 	if (err < 0)
2673 		return err;
2674 	err = snd_rme_read_value(chip, SND_RME_GET_CURRENT_FREQ, &den);
2675 	if (err < 0)
2676 		return err;
2677 	freq = (den == 0) ? 0 : div64_u64(num, den);
2678 	freq <<= SND_RME_CLK_FREQMUL(status1);
2679 	ucontrol->value.integer.value[0] = freq;
2680 	return 0;
2681 }
2682 
2683 static int snd_rme_rate_info(struct snd_kcontrol *kcontrol,
2684 			     struct snd_ctl_elem_info *uinfo)
2685 {
2686 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2687 	uinfo->count = 1;
2688 	switch (kcontrol->private_value) {
2689 	case SND_RME_DOMAIN_SYSTEM:
2690 		uinfo->value.integer.min = 32000;
2691 		uinfo->value.integer.max = 800000;
2692 		break;
2693 	case SND_RME_DOMAIN_AES:
2694 	case SND_RME_DOMAIN_SPDIF:
2695 	default:
2696 		uinfo->value.integer.min = 0;
2697 		uinfo->value.integer.max = 200000;
2698 	}
2699 	uinfo->value.integer.step = 0;
2700 	return 0;
2701 }
2702 
2703 static int snd_rme_sync_state_info(struct snd_kcontrol *kcontrol,
2704 				   struct snd_ctl_elem_info *uinfo)
2705 {
2706 	static const char *const sync_states[] = {
2707 		"No Lock", "Lock", "Sync"
2708 	};
2709 
2710 	return snd_ctl_enum_info(uinfo, 1,
2711 				 ARRAY_SIZE(sync_states), sync_states);
2712 }
2713 
2714 static int snd_rme_spdif_if_info(struct snd_kcontrol *kcontrol,
2715 				 struct snd_ctl_elem_info *uinfo)
2716 {
2717 	static const char *const spdif_if[] = {
2718 		"Coaxial", "Optical"
2719 	};
2720 
2721 	return snd_ctl_enum_info(uinfo, 1,
2722 				 ARRAY_SIZE(spdif_if), spdif_if);
2723 }
2724 
2725 static int snd_rme_spdif_format_info(struct snd_kcontrol *kcontrol,
2726 				     struct snd_ctl_elem_info *uinfo)
2727 {
2728 	static const char *const optical_type[] = {
2729 		"Consumer", "Professional"
2730 	};
2731 
2732 	return snd_ctl_enum_info(uinfo, 1,
2733 				 ARRAY_SIZE(optical_type), optical_type);
2734 }
2735 
2736 static int snd_rme_sync_source_info(struct snd_kcontrol *kcontrol,
2737 				    struct snd_ctl_elem_info *uinfo)
2738 {
2739 	static const char *const sync_sources[] = {
2740 		"Internal", "AES", "SPDIF", "Internal"
2741 	};
2742 
2743 	return snd_ctl_enum_info(uinfo, 1,
2744 				 ARRAY_SIZE(sync_sources), sync_sources);
2745 }
2746 
2747 static const struct snd_kcontrol_new snd_rme_controls[] = {
2748 	{
2749 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2750 		.name = "AES Rate",
2751 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2752 		.info = snd_rme_rate_info,
2753 		.get = snd_rme_rate_get,
2754 		.private_value = SND_RME_DOMAIN_AES
2755 	},
2756 	{
2757 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2758 		.name = "AES Sync",
2759 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2760 		.info = snd_rme_sync_state_info,
2761 		.get = snd_rme_sync_state_get,
2762 		.private_value = SND_RME_DOMAIN_AES
2763 	},
2764 	{
2765 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2766 		.name = "SPDIF Rate",
2767 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2768 		.info = snd_rme_rate_info,
2769 		.get = snd_rme_rate_get,
2770 		.private_value = SND_RME_DOMAIN_SPDIF
2771 	},
2772 	{
2773 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2774 		.name = "SPDIF Sync",
2775 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2776 		.info = snd_rme_sync_state_info,
2777 		.get = snd_rme_sync_state_get,
2778 		.private_value = SND_RME_DOMAIN_SPDIF
2779 	},
2780 	{
2781 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2782 		.name = "SPDIF Interface",
2783 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2784 		.info = snd_rme_spdif_if_info,
2785 		.get = snd_rme_spdif_if_get,
2786 	},
2787 	{
2788 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2789 		.name = "SPDIF Format",
2790 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2791 		.info = snd_rme_spdif_format_info,
2792 		.get = snd_rme_spdif_format_get,
2793 	},
2794 	{
2795 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2796 		.name = "Sync Source",
2797 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2798 		.info = snd_rme_sync_source_info,
2799 		.get = snd_rme_sync_source_get
2800 	},
2801 	{
2802 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2803 		.name = "System Rate",
2804 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2805 		.info = snd_rme_rate_info,
2806 		.get = snd_rme_rate_get,
2807 		.private_value = SND_RME_DOMAIN_SYSTEM
2808 	},
2809 	{
2810 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2811 		.name = "Current Frequency",
2812 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2813 		.info = snd_rme_rate_info,
2814 		.get = snd_rme_current_freq_get
2815 	}
2816 };
2817 
2818 static int snd_rme_controls_create(struct usb_mixer_interface *mixer)
2819 {
2820 	int err, i;
2821 
2822 	for (i = 0; i < ARRAY_SIZE(snd_rme_controls); ++i) {
2823 		err = add_single_ctl_with_resume(mixer, 0,
2824 						 NULL,
2825 						 &snd_rme_controls[i],
2826 						 NULL);
2827 		if (err < 0)
2828 			return err;
2829 	}
2830 
2831 	return 0;
2832 }
2833 
2834 /*
2835  * RME Babyface Pro (FS)
2836  *
2837  * These devices exposes a couple of DSP functions via request to EP0.
2838  * Switches are available via control registers, while routing is controlled
2839  * by controlling the volume on each possible crossing point.
2840  * Volume control is linear, from -inf (dec. 0) to +6dB (dec. 65536) with
2841  * 0dB being at dec. 32768.
2842  */
2843 enum {
2844 	SND_BBFPRO_CTL_REG1 = 0,
2845 	SND_BBFPRO_CTL_REG2
2846 };
2847 
2848 #define SND_BBFPRO_CTL_REG_MASK 1
2849 #define SND_BBFPRO_CTL_IDX_MASK 0xff
2850 #define SND_BBFPRO_CTL_IDX_SHIFT 1
2851 #define SND_BBFPRO_CTL_VAL_MASK 1
2852 #define SND_BBFPRO_CTL_VAL_SHIFT 9
2853 #define SND_BBFPRO_CTL_REG1_CLK_MASTER 0
2854 #define SND_BBFPRO_CTL_REG1_CLK_OPTICAL 1
2855 #define SND_BBFPRO_CTL_REG1_SPDIF_PRO 7
2856 #define SND_BBFPRO_CTL_REG1_SPDIF_EMPH 8
2857 #define SND_BBFPRO_CTL_REG1_SPDIF_OPTICAL 10
2858 #define SND_BBFPRO_CTL_REG2_48V_AN1 0
2859 #define SND_BBFPRO_CTL_REG2_48V_AN2 1
2860 #define SND_BBFPRO_CTL_REG2_SENS_IN3 2
2861 #define SND_BBFPRO_CTL_REG2_SENS_IN4 3
2862 #define SND_BBFPRO_CTL_REG2_PAD_AN1 4
2863 #define SND_BBFPRO_CTL_REG2_PAD_AN2 5
2864 
2865 #define SND_BBFPRO_MIXER_MAIN_OUT_CH_OFFSET 992
2866 #define SND_BBFPRO_MIXER_IDX_MASK 0x3ff
2867 #define SND_BBFPRO_MIXER_VAL_MASK 0x3ffff
2868 #define SND_BBFPRO_MIXER_VAL_SHIFT 9
2869 #define SND_BBFPRO_MIXER_VAL_MIN 0 // -inf
2870 #define SND_BBFPRO_MIXER_VAL_MAX 65536 // +6dB
2871 
2872 #define SND_BBFPRO_GAIN_CHANNEL_MASK 0x03
2873 #define SND_BBFPRO_GAIN_CHANNEL_SHIFT 7
2874 #define SND_BBFPRO_GAIN_VAL_MASK 0x7f
2875 #define SND_BBFPRO_GAIN_VAL_MIN 0
2876 #define SND_BBFPRO_GAIN_VAL_MIC_MAX 65
2877 #define SND_BBFPRO_GAIN_VAL_LINE_MAX 18 // 9db in 0.5db incraments
2878 
2879 #define SND_BBFPRO_USBREQ_CTL_REG1 0x10
2880 #define SND_BBFPRO_USBREQ_CTL_REG2 0x17
2881 #define SND_BBFPRO_USBREQ_GAIN 0x1a
2882 #define SND_BBFPRO_USBREQ_MIXER 0x12
2883 
2884 static int snd_bbfpro_ctl_update(struct usb_mixer_interface *mixer, u8 reg,
2885 				 u8 index, u8 value)
2886 {
2887 	u16 usb_req, usb_idx, usb_val;
2888 	struct snd_usb_audio *chip = mixer->chip;
2889 
2890 	CLASS(snd_usb_lock, pm)(chip);
2891 	if (pm.err < 0)
2892 		return pm.err;
2893 
2894 	if (reg == SND_BBFPRO_CTL_REG1) {
2895 		usb_req = SND_BBFPRO_USBREQ_CTL_REG1;
2896 		if (index == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) {
2897 			usb_idx = 3;
2898 			usb_val = value ? 3 : 0;
2899 		} else {
2900 			usb_idx = BIT(index);
2901 			usb_val = value ? usb_idx : 0;
2902 		}
2903 	} else {
2904 		usb_req = SND_BBFPRO_USBREQ_CTL_REG2;
2905 		usb_idx = BIT(index);
2906 		usb_val = value ? usb_idx : 0;
2907 	}
2908 
2909 	return snd_usb_ctl_msg(chip->dev,
2910 			       usb_sndctrlpipe(chip->dev, 0), usb_req,
2911 			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2912 			       usb_val, usb_idx, NULL, 0);
2913 }
2914 
2915 static int snd_bbfpro_ctl_get(struct snd_kcontrol *kcontrol,
2916 			      struct snd_ctl_elem_value *ucontrol)
2917 {
2918 	u8 reg, idx, val;
2919 	int pv;
2920 
2921 	pv = kcontrol->private_value;
2922 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2923 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2924 	val = kcontrol->private_value >> SND_BBFPRO_CTL_VAL_SHIFT;
2925 
2926 	if ((reg == SND_BBFPRO_CTL_REG1 &&
2927 	     idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) ||
2928 	    (reg == SND_BBFPRO_CTL_REG2 &&
2929 	    (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2930 	     idx == SND_BBFPRO_CTL_REG2_SENS_IN4))) {
2931 		ucontrol->value.enumerated.item[0] = val;
2932 	} else {
2933 		ucontrol->value.integer.value[0] = val;
2934 	}
2935 	return 0;
2936 }
2937 
2938 static int snd_bbfpro_ctl_info(struct snd_kcontrol *kcontrol,
2939 			       struct snd_ctl_elem_info *uinfo)
2940 {
2941 	u8 reg, idx;
2942 	int pv;
2943 
2944 	pv = kcontrol->private_value;
2945 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2946 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2947 
2948 	if (reg == SND_BBFPRO_CTL_REG1 &&
2949 	    idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) {
2950 		static const char * const texts[2] = {
2951 			"AutoSync",
2952 			"Internal"
2953 		};
2954 		return snd_ctl_enum_info(uinfo, 1, 2, texts);
2955 	} else if (reg == SND_BBFPRO_CTL_REG2 &&
2956 		   (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2957 		    idx == SND_BBFPRO_CTL_REG2_SENS_IN4)) {
2958 		static const char * const texts[2] = {
2959 			"-10dBV",
2960 			"+4dBu"
2961 		};
2962 		return snd_ctl_enum_info(uinfo, 1, 2, texts);
2963 	}
2964 
2965 	uinfo->count = 1;
2966 	uinfo->value.integer.min = 0;
2967 	uinfo->value.integer.max = 1;
2968 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2969 	return 0;
2970 }
2971 
2972 static int snd_bbfpro_ctl_put(struct snd_kcontrol *kcontrol,
2973 			      struct snd_ctl_elem_value *ucontrol)
2974 {
2975 	int err;
2976 	u8 reg, idx;
2977 	int old_value, pv, val;
2978 
2979 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2980 	struct usb_mixer_interface *mixer = list->mixer;
2981 
2982 	pv = kcontrol->private_value;
2983 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2984 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2985 	old_value = (pv >> SND_BBFPRO_CTL_VAL_SHIFT) & SND_BBFPRO_CTL_VAL_MASK;
2986 
2987 	if ((reg == SND_BBFPRO_CTL_REG1 &&
2988 	     idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) ||
2989 	    (reg == SND_BBFPRO_CTL_REG2 &&
2990 	    (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2991 	     idx == SND_BBFPRO_CTL_REG2_SENS_IN4))) {
2992 		val = ucontrol->value.enumerated.item[0];
2993 	} else {
2994 		val = ucontrol->value.integer.value[0];
2995 	}
2996 
2997 	if (val > 1)
2998 		return -EINVAL;
2999 
3000 	if (val == old_value)
3001 		return 0;
3002 
3003 	kcontrol->private_value = reg
3004 		| ((idx & SND_BBFPRO_CTL_IDX_MASK) << SND_BBFPRO_CTL_IDX_SHIFT)
3005 		| ((val & SND_BBFPRO_CTL_VAL_MASK) << SND_BBFPRO_CTL_VAL_SHIFT);
3006 
3007 	err = snd_bbfpro_ctl_update(mixer, reg, idx, val);
3008 	return err < 0 ? err : 1;
3009 }
3010 
3011 static int snd_bbfpro_ctl_resume(struct usb_mixer_elem_list *list)
3012 {
3013 	u8 reg, idx;
3014 	int value, pv;
3015 
3016 	pv = list->kctl->private_value;
3017 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
3018 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
3019 	value = (pv >> SND_BBFPRO_CTL_VAL_SHIFT) & SND_BBFPRO_CTL_VAL_MASK;
3020 
3021 	return snd_bbfpro_ctl_update(list->mixer, reg, idx, value);
3022 }
3023 
3024 static int snd_bbfpro_gain_update(struct usb_mixer_interface *mixer,
3025 				  u8 channel, u8 gain)
3026 {
3027 	struct snd_usb_audio *chip = mixer->chip;
3028 
3029 	if (channel < 2) {
3030 		// XLR preamp: 3-bit fine, 5-bit coarse; special case >60
3031 		if (gain < 60)
3032 			gain = ((gain % 3) << 5) | (gain / 3);
3033 		else
3034 			gain = ((gain % 6) << 5) | (60 / 3);
3035 	}
3036 
3037 	CLASS(snd_usb_lock, pm)(chip);
3038 	if (pm.err < 0)
3039 		return pm.err;
3040 
3041 	return snd_usb_ctl_msg(chip->dev,
3042 			       usb_sndctrlpipe(chip->dev, 0),
3043 			       SND_BBFPRO_USBREQ_GAIN,
3044 			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3045 			       gain, channel, NULL, 0);
3046 }
3047 
3048 static int snd_bbfpro_gain_get(struct snd_kcontrol *kcontrol,
3049 			       struct snd_ctl_elem_value *ucontrol)
3050 {
3051 	int value = kcontrol->private_value & SND_BBFPRO_GAIN_VAL_MASK;
3052 
3053 	ucontrol->value.integer.value[0] = value;
3054 	return 0;
3055 }
3056 
3057 static int snd_bbfpro_gain_info(struct snd_kcontrol *kcontrol,
3058 				struct snd_ctl_elem_info *uinfo)
3059 {
3060 	int pv, channel;
3061 
3062 	pv = kcontrol->private_value;
3063 	channel = (pv >> SND_BBFPRO_GAIN_CHANNEL_SHIFT) &
3064 		SND_BBFPRO_GAIN_CHANNEL_MASK;
3065 
3066 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3067 	uinfo->count = 1;
3068 	uinfo->value.integer.min = SND_BBFPRO_GAIN_VAL_MIN;
3069 
3070 	if (channel < 2)
3071 		uinfo->value.integer.max = SND_BBFPRO_GAIN_VAL_MIC_MAX;
3072 	else
3073 		uinfo->value.integer.max = SND_BBFPRO_GAIN_VAL_LINE_MAX;
3074 
3075 	return 0;
3076 }
3077 
3078 static int snd_bbfpro_gain_put(struct snd_kcontrol *kcontrol,
3079 			       struct snd_ctl_elem_value *ucontrol)
3080 {
3081 	int pv, channel, old_value, value, err;
3082 
3083 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3084 	struct usb_mixer_interface *mixer = list->mixer;
3085 
3086 	pv = kcontrol->private_value;
3087 	channel = (pv >> SND_BBFPRO_GAIN_CHANNEL_SHIFT) &
3088 		SND_BBFPRO_GAIN_CHANNEL_MASK;
3089 	old_value = pv & SND_BBFPRO_GAIN_VAL_MASK;
3090 	value = ucontrol->value.integer.value[0];
3091 
3092 	if (value < SND_BBFPRO_GAIN_VAL_MIN)
3093 		return -EINVAL;
3094 
3095 	if (channel < 2) {
3096 		if (value > SND_BBFPRO_GAIN_VAL_MIC_MAX)
3097 			return -EINVAL;
3098 	} else {
3099 		if (value > SND_BBFPRO_GAIN_VAL_LINE_MAX)
3100 			return -EINVAL;
3101 	}
3102 
3103 	if (value == old_value)
3104 		return 0;
3105 
3106 	err = snd_bbfpro_gain_update(mixer, channel, value);
3107 	if (err < 0)
3108 		return err;
3109 
3110 	kcontrol->private_value =
3111 		(channel << SND_BBFPRO_GAIN_CHANNEL_SHIFT) | value;
3112 	return 1;
3113 }
3114 
3115 static int snd_bbfpro_gain_resume(struct usb_mixer_elem_list *list)
3116 {
3117 	int pv, channel, value;
3118 	struct snd_kcontrol *kctl = list->kctl;
3119 
3120 	pv = kctl->private_value;
3121 	channel = (pv >> SND_BBFPRO_GAIN_CHANNEL_SHIFT) &
3122 		SND_BBFPRO_GAIN_CHANNEL_MASK;
3123 	value = pv & SND_BBFPRO_GAIN_VAL_MASK;
3124 
3125 	return snd_bbfpro_gain_update(list->mixer, channel, value);
3126 }
3127 
3128 static int snd_bbfpro_vol_update(struct usb_mixer_interface *mixer, u16 index,
3129 				 u32 value)
3130 {
3131 	struct snd_usb_audio *chip = mixer->chip;
3132 	u16 idx;
3133 	u16 usb_idx, usb_val;
3134 	u32 v;
3135 
3136 	CLASS(snd_usb_lock, pm)(chip);
3137 	if (pm.err < 0)
3138 		return pm.err;
3139 
3140 	idx = index & SND_BBFPRO_MIXER_IDX_MASK;
3141 	// 18 bit linear volume, split so 2 bits end up in index.
3142 	v = value & SND_BBFPRO_MIXER_VAL_MASK;
3143 	usb_idx = idx | (v & 0x3) << 14;
3144 	usb_val = (v >> 2) & 0xffff;
3145 
3146 	return snd_usb_ctl_msg(chip->dev,
3147 			       usb_sndctrlpipe(chip->dev, 0),
3148 			       SND_BBFPRO_USBREQ_MIXER,
3149 			       USB_DIR_OUT | USB_TYPE_VENDOR |
3150 			       USB_RECIP_DEVICE,
3151 			       usb_val, usb_idx, NULL, 0);
3152 }
3153 
3154 static int snd_bbfpro_vol_get(struct snd_kcontrol *kcontrol,
3155 			      struct snd_ctl_elem_value *ucontrol)
3156 {
3157 	ucontrol->value.integer.value[0] =
3158 		kcontrol->private_value >> SND_BBFPRO_MIXER_VAL_SHIFT;
3159 	return 0;
3160 }
3161 
3162 static int snd_bbfpro_vol_info(struct snd_kcontrol *kcontrol,
3163 			       struct snd_ctl_elem_info *uinfo)
3164 {
3165 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3166 	uinfo->count = 1;
3167 	uinfo->value.integer.min = SND_BBFPRO_MIXER_VAL_MIN;
3168 	uinfo->value.integer.max = SND_BBFPRO_MIXER_VAL_MAX;
3169 	return 0;
3170 }
3171 
3172 static int snd_bbfpro_vol_put(struct snd_kcontrol *kcontrol,
3173 			      struct snd_ctl_elem_value *ucontrol)
3174 {
3175 	int err;
3176 	u16 idx;
3177 	u32 new_val, old_value, uvalue;
3178 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3179 	struct usb_mixer_interface *mixer = list->mixer;
3180 
3181 	uvalue = ucontrol->value.integer.value[0];
3182 	idx = kcontrol->private_value & SND_BBFPRO_MIXER_IDX_MASK;
3183 	old_value = kcontrol->private_value >> SND_BBFPRO_MIXER_VAL_SHIFT;
3184 
3185 	if (uvalue > SND_BBFPRO_MIXER_VAL_MAX)
3186 		return -EINVAL;
3187 
3188 	if (uvalue == old_value)
3189 		return 0;
3190 
3191 	new_val = uvalue & SND_BBFPRO_MIXER_VAL_MASK;
3192 
3193 	kcontrol->private_value = idx
3194 		| (new_val << SND_BBFPRO_MIXER_VAL_SHIFT);
3195 
3196 	err = snd_bbfpro_vol_update(mixer, idx, new_val);
3197 	return err < 0 ? err : 1;
3198 }
3199 
3200 static int snd_bbfpro_vol_resume(struct usb_mixer_elem_list *list)
3201 {
3202 	int pv = list->kctl->private_value;
3203 	u16 idx = pv & SND_BBFPRO_MIXER_IDX_MASK;
3204 	u32 val = (pv >> SND_BBFPRO_MIXER_VAL_SHIFT)
3205 		& SND_BBFPRO_MIXER_VAL_MASK;
3206 	return snd_bbfpro_vol_update(list->mixer, idx, val);
3207 }
3208 
3209 // Predfine elements
3210 static const struct snd_kcontrol_new snd_bbfpro_ctl_control = {
3211 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3212 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3213 	.index = 0,
3214 	.info = snd_bbfpro_ctl_info,
3215 	.get = snd_bbfpro_ctl_get,
3216 	.put = snd_bbfpro_ctl_put
3217 };
3218 
3219 static const struct snd_kcontrol_new snd_bbfpro_gain_control = {
3220 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3221 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3222 	.index = 0,
3223 	.info = snd_bbfpro_gain_info,
3224 	.get = snd_bbfpro_gain_get,
3225 	.put = snd_bbfpro_gain_put
3226 };
3227 
3228 static const struct snd_kcontrol_new snd_bbfpro_vol_control = {
3229 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3230 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3231 	.index = 0,
3232 	.info = snd_bbfpro_vol_info,
3233 	.get = snd_bbfpro_vol_get,
3234 	.put = snd_bbfpro_vol_put
3235 };
3236 
3237 static int snd_bbfpro_ctl_add(struct usb_mixer_interface *mixer, u8 reg,
3238 			      u8 index, char *name)
3239 {
3240 	struct snd_kcontrol_new knew = snd_bbfpro_ctl_control;
3241 
3242 	knew.name = name;
3243 	knew.private_value = (reg & SND_BBFPRO_CTL_REG_MASK)
3244 		| ((index & SND_BBFPRO_CTL_IDX_MASK)
3245 			<< SND_BBFPRO_CTL_IDX_SHIFT);
3246 
3247 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_ctl_resume,
3248 		&knew, NULL);
3249 }
3250 
3251 static int snd_bbfpro_gain_add(struct usb_mixer_interface *mixer, u8 channel,
3252 			       char *name)
3253 {
3254 	struct snd_kcontrol_new knew = snd_bbfpro_gain_control;
3255 
3256 	knew.name = name;
3257 	knew.private_value = channel << SND_BBFPRO_GAIN_CHANNEL_SHIFT;
3258 
3259 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_gain_resume,
3260 		&knew, NULL);
3261 }
3262 
3263 static int snd_bbfpro_vol_add(struct usb_mixer_interface *mixer, u16 index,
3264 			      char *name)
3265 {
3266 	struct snd_kcontrol_new knew = snd_bbfpro_vol_control;
3267 
3268 	knew.name = name;
3269 	knew.private_value = index & SND_BBFPRO_MIXER_IDX_MASK;
3270 
3271 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_vol_resume,
3272 		&knew, NULL);
3273 }
3274 
3275 static int snd_bbfpro_controls_create(struct usb_mixer_interface *mixer)
3276 {
3277 	int err, i, o;
3278 	char name[48];
3279 
3280 	static const char * const input[] = {
3281 		"AN1", "AN2", "IN3", "IN4", "AS1", "AS2", "ADAT3",
3282 		"ADAT4", "ADAT5", "ADAT6", "ADAT7", "ADAT8"};
3283 
3284 	static const char * const output[] = {
3285 		"AN1", "AN2", "PH3", "PH4", "AS1", "AS2", "ADAT3", "ADAT4",
3286 		"ADAT5", "ADAT6", "ADAT7", "ADAT8"};
3287 
3288 	for (o = 0 ; o < 12 ; ++o) {
3289 		for (i = 0 ; i < 12 ; ++i) {
3290 			// Line routing
3291 			snprintf(name, sizeof(name),
3292 				 "%s-%s-%s Playback Volume",
3293 				 (i < 2 ? "Mic" : "Line"),
3294 				 input[i], output[o]);
3295 			err = snd_bbfpro_vol_add(mixer, (26 * o + i), name);
3296 			if (err < 0)
3297 				return err;
3298 
3299 			// PCM routing... yes, it is output remapping
3300 			snprintf(name, sizeof(name),
3301 				 "PCM-%s-%s Playback Volume",
3302 				 output[i], output[o]);
3303 			err = snd_bbfpro_vol_add(mixer, (26 * o + 12 + i),
3304 						 name);
3305 			if (err < 0)
3306 				return err;
3307 		}
3308 	}
3309 
3310 	// Main out volume
3311 	for (i = 0 ; i < 12 ; ++i) {
3312 		snprintf(name, sizeof(name), "Main-Out %s", output[i]);
3313 		// Main outs are offset to 992
3314 		err = snd_bbfpro_vol_add(mixer,
3315 					 i + SND_BBFPRO_MIXER_MAIN_OUT_CH_OFFSET,
3316 					 name);
3317 		if (err < 0)
3318 			return err;
3319 	}
3320 
3321 	// Input gain
3322 	for (i = 0 ; i < 4 ; ++i) {
3323 		if (i < 2)
3324 			snprintf(name, sizeof(name), "Mic-%s Gain", input[i]);
3325 		else
3326 			snprintf(name, sizeof(name), "Line-%s Gain", input[i]);
3327 
3328 		err = snd_bbfpro_gain_add(mixer, i, name);
3329 		if (err < 0)
3330 			return err;
3331 	}
3332 
3333 	// Control Reg 1
3334 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3335 				 SND_BBFPRO_CTL_REG1_CLK_OPTICAL,
3336 				 "Sample Clock Source");
3337 	if (err < 0)
3338 		return err;
3339 
3340 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3341 				 SND_BBFPRO_CTL_REG1_SPDIF_PRO,
3342 				 "IEC958 Pro Mask");
3343 	if (err < 0)
3344 		return err;
3345 
3346 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3347 				 SND_BBFPRO_CTL_REG1_SPDIF_EMPH,
3348 				 "IEC958 Emphasis");
3349 	if (err < 0)
3350 		return err;
3351 
3352 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3353 				 SND_BBFPRO_CTL_REG1_SPDIF_OPTICAL,
3354 				 "IEC958 Switch");
3355 	if (err < 0)
3356 		return err;
3357 
3358 	// Control Reg 2
3359 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3360 				 SND_BBFPRO_CTL_REG2_48V_AN1,
3361 				 "Mic-AN1 48V");
3362 	if (err < 0)
3363 		return err;
3364 
3365 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3366 				 SND_BBFPRO_CTL_REG2_48V_AN2,
3367 				 "Mic-AN2 48V");
3368 	if (err < 0)
3369 		return err;
3370 
3371 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3372 				 SND_BBFPRO_CTL_REG2_SENS_IN3,
3373 				 "Line-IN3 Sens.");
3374 	if (err < 0)
3375 		return err;
3376 
3377 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3378 				 SND_BBFPRO_CTL_REG2_SENS_IN4,
3379 				 "Line-IN4 Sens.");
3380 	if (err < 0)
3381 		return err;
3382 
3383 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3384 				 SND_BBFPRO_CTL_REG2_PAD_AN1,
3385 				 "Mic-AN1 PAD");
3386 	if (err < 0)
3387 		return err;
3388 
3389 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3390 				 SND_BBFPRO_CTL_REG2_PAD_AN2,
3391 				 "Mic-AN2 PAD");
3392 	if (err < 0)
3393 		return err;
3394 
3395 	return 0;
3396 }
3397 
3398 /*
3399  * RME Digiface USB
3400  */
3401 
3402 #define RME_DIGIFACE_READ_STATUS 17
3403 #define RME_DIGIFACE_STATUS_REG0L 0
3404 #define RME_DIGIFACE_STATUS_REG0H 1
3405 #define RME_DIGIFACE_STATUS_REG1L 2
3406 #define RME_DIGIFACE_STATUS_REG1H 3
3407 #define RME_DIGIFACE_STATUS_REG2L 4
3408 #define RME_DIGIFACE_STATUS_REG2H 5
3409 #define RME_DIGIFACE_STATUS_REG3L 6
3410 #define RME_DIGIFACE_STATUS_REG3H 7
3411 
3412 #define RME_DIGIFACE_CTL_REG1 16
3413 #define RME_DIGIFACE_CTL_REG2 18
3414 
3415 /* Reg is overloaded, 0-7 for status halfwords or 16 or 18 for control registers */
3416 #define RME_DIGIFACE_REGISTER(reg, mask) (((reg) << 16) | (mask))
3417 #define RME_DIGIFACE_INVERT BIT(31)
3418 
3419 /* Nonconst helpers */
3420 #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
3421 #define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
3422 
3423 static int snd_rme_digiface_write_reg(struct snd_kcontrol *kcontrol, int item, u16 mask, u16 val)
3424 {
3425 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3426 	struct snd_usb_audio *chip = list->mixer->chip;
3427 	struct usb_device *dev = chip->dev;
3428 	int err;
3429 
3430 	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
3431 			      item,
3432 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3433 			      val, mask, NULL, 0);
3434 	if (err < 0)
3435 		dev_err(&dev->dev,
3436 			"unable to issue control set request %d (ret = %d)",
3437 			item, err);
3438 	return err;
3439 }
3440 
3441 static int snd_rme_digiface_read_status(struct snd_kcontrol *kcontrol, u32 status[4])
3442 {
3443 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3444 	struct snd_usb_audio *chip = list->mixer->chip;
3445 	struct usb_device *dev = chip->dev;
3446 	__le32 buf[4];
3447 	int err;
3448 
3449 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
3450 			      RME_DIGIFACE_READ_STATUS,
3451 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3452 			      0, 0,
3453 			      buf, sizeof(buf));
3454 	if (err < 0) {
3455 		dev_err(&dev->dev,
3456 			"unable to issue status read request (ret = %d)",
3457 			err);
3458 	} else {
3459 		for (int i = 0; i < ARRAY_SIZE(buf); i++)
3460 			status[i] = le32_to_cpu(buf[i]);
3461 	}
3462 	return err;
3463 }
3464 
3465 static int snd_rme_digiface_get_status_val(struct snd_kcontrol *kcontrol)
3466 {
3467 	int err;
3468 	u32 status[4];
3469 	bool invert = kcontrol->private_value & RME_DIGIFACE_INVERT;
3470 	u8 reg = (kcontrol->private_value >> 16) & 0xff;
3471 	u16 mask = kcontrol->private_value & 0xffff;
3472 	u16 val;
3473 
3474 	err = snd_rme_digiface_read_status(kcontrol, status);
3475 	if (err < 0)
3476 		return err;
3477 
3478 	switch (reg) {
3479 	/* Status register halfwords */
3480 	case RME_DIGIFACE_STATUS_REG0L ... RME_DIGIFACE_STATUS_REG3H:
3481 		break;
3482 	case RME_DIGIFACE_CTL_REG1: /* Control register 1, present in halfword 3L */
3483 		reg = RME_DIGIFACE_STATUS_REG3L;
3484 		break;
3485 	case RME_DIGIFACE_CTL_REG2: /* Control register 2, present in halfword 3H */
3486 		reg = RME_DIGIFACE_STATUS_REG3H;
3487 		break;
3488 	default:
3489 		return -EINVAL;
3490 	}
3491 
3492 	if (reg & 1)
3493 		val = status[reg >> 1] >> 16;
3494 	else
3495 		val = status[reg >> 1] & 0xffff;
3496 
3497 	if (invert)
3498 		val ^= mask;
3499 
3500 	return field_get(mask, val);
3501 }
3502 
3503 static int snd_rme_digiface_rate_get(struct snd_kcontrol *kcontrol,
3504 				     struct snd_ctl_elem_value *ucontrol)
3505 {
3506 	int freq = snd_rme_digiface_get_status_val(kcontrol);
3507 
3508 	if (freq < 0)
3509 		return freq;
3510 	if (freq >= ARRAY_SIZE(snd_rme_rate_table))
3511 		return -EIO;
3512 
3513 	ucontrol->value.integer.value[0] = snd_rme_rate_table[freq];
3514 	return 0;
3515 }
3516 
3517 static int snd_rme_digiface_enum_get(struct snd_kcontrol *kcontrol,
3518 				     struct snd_ctl_elem_value *ucontrol)
3519 {
3520 	int val = snd_rme_digiface_get_status_val(kcontrol);
3521 
3522 	if (val < 0)
3523 		return val;
3524 
3525 	ucontrol->value.enumerated.item[0] = val;
3526 	return 0;
3527 }
3528 
3529 static int snd_rme_digiface_enum_put(struct snd_kcontrol *kcontrol,
3530 				     struct snd_ctl_elem_value *ucontrol)
3531 {
3532 	bool invert = kcontrol->private_value & RME_DIGIFACE_INVERT;
3533 	u8 reg = (kcontrol->private_value >> 16) & 0xff;
3534 	u16 mask = kcontrol->private_value & 0xffff;
3535 	u16 val = field_prep(mask, ucontrol->value.enumerated.item[0]);
3536 
3537 	if (invert)
3538 		val ^= mask;
3539 
3540 	return snd_rme_digiface_write_reg(kcontrol, reg, mask, val);
3541 }
3542 
3543 static int snd_rme_digiface_current_sync_get(struct snd_kcontrol *kcontrol,
3544 					     struct snd_ctl_elem_value *ucontrol)
3545 {
3546 	int ret = snd_rme_digiface_enum_get(kcontrol, ucontrol);
3547 
3548 	/* 7 means internal for current sync */
3549 	if (ucontrol->value.enumerated.item[0] == 7)
3550 		ucontrol->value.enumerated.item[0] = 0;
3551 
3552 	return ret;
3553 }
3554 
3555 static int snd_rme_digiface_sync_state_get(struct snd_kcontrol *kcontrol,
3556 					   struct snd_ctl_elem_value *ucontrol)
3557 {
3558 	u32 status[4];
3559 	int err;
3560 	bool valid, sync;
3561 
3562 	err = snd_rme_digiface_read_status(kcontrol, status);
3563 	if (err < 0)
3564 		return err;
3565 
3566 	valid = status[0] & BIT(kcontrol->private_value);
3567 	sync = status[0] & BIT(5 + kcontrol->private_value);
3568 
3569 	if (!valid)
3570 		ucontrol->value.enumerated.item[0] = SND_RME_CLOCK_NOLOCK;
3571 	else if (!sync)
3572 		ucontrol->value.enumerated.item[0] = SND_RME_CLOCK_LOCK;
3573 	else
3574 		ucontrol->value.enumerated.item[0] = SND_RME_CLOCK_SYNC;
3575 	return 0;
3576 }
3577 
3578 static int snd_rme_digiface_format_info(struct snd_kcontrol *kcontrol,
3579 					struct snd_ctl_elem_info *uinfo)
3580 {
3581 	static const char *const format[] = {
3582 		"ADAT", "S/PDIF"
3583 	};
3584 
3585 	return snd_ctl_enum_info(uinfo, 1,
3586 				 ARRAY_SIZE(format), format);
3587 }
3588 
3589 static int snd_rme_digiface_sync_source_info(struct snd_kcontrol *kcontrol,
3590 					     struct snd_ctl_elem_info *uinfo)
3591 {
3592 	static const char *const sync_sources[] = {
3593 		"Internal", "Input 1", "Input 2", "Input 3", "Input 4"
3594 	};
3595 
3596 	return snd_ctl_enum_info(uinfo, 1,
3597 				 ARRAY_SIZE(sync_sources), sync_sources);
3598 }
3599 
3600 static int snd_rme_digiface_rate_info(struct snd_kcontrol *kcontrol,
3601 				      struct snd_ctl_elem_info *uinfo)
3602 {
3603 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3604 	uinfo->count = 1;
3605 	uinfo->value.integer.min = 0;
3606 	uinfo->value.integer.max = 200000;
3607 	uinfo->value.integer.step = 0;
3608 	return 0;
3609 }
3610 
3611 static const struct snd_kcontrol_new snd_rme_digiface_controls[] = {
3612 	{
3613 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3614 		.name = "Input 1 Sync",
3615 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3616 		.info = snd_rme_sync_state_info,
3617 		.get = snd_rme_digiface_sync_state_get,
3618 		.private_value = 0,
3619 	},
3620 	{
3621 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3622 		.name = "Input 1 Format",
3623 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3624 		.info = snd_rme_digiface_format_info,
3625 		.get = snd_rme_digiface_enum_get,
3626 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0H, BIT(0)) |
3627 			RME_DIGIFACE_INVERT,
3628 	},
3629 	{
3630 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3631 		.name = "Input 1 Rate",
3632 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3633 		.info = snd_rme_digiface_rate_info,
3634 		.get = snd_rme_digiface_rate_get,
3635 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(3, 0)),
3636 	},
3637 	{
3638 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3639 		.name = "Input 2 Sync",
3640 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3641 		.info = snd_rme_sync_state_info,
3642 		.get = snd_rme_digiface_sync_state_get,
3643 		.private_value = 1,
3644 	},
3645 	{
3646 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3647 		.name = "Input 2 Format",
3648 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3649 		.info = snd_rme_digiface_format_info,
3650 		.get = snd_rme_digiface_enum_get,
3651 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, BIT(13)) |
3652 			RME_DIGIFACE_INVERT,
3653 	},
3654 	{
3655 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3656 		.name = "Input 2 Rate",
3657 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3658 		.info = snd_rme_digiface_rate_info,
3659 		.get = snd_rme_digiface_rate_get,
3660 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(7, 4)),
3661 	},
3662 	{
3663 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3664 		.name = "Input 3 Sync",
3665 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3666 		.info = snd_rme_sync_state_info,
3667 		.get = snd_rme_digiface_sync_state_get,
3668 		.private_value = 2,
3669 	},
3670 	{
3671 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3672 		.name = "Input 3 Format",
3673 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3674 		.info = snd_rme_digiface_format_info,
3675 		.get = snd_rme_digiface_enum_get,
3676 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, BIT(14)) |
3677 			RME_DIGIFACE_INVERT,
3678 	},
3679 	{
3680 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3681 		.name = "Input 3 Rate",
3682 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3683 		.info = snd_rme_digiface_rate_info,
3684 		.get = snd_rme_digiface_rate_get,
3685 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(11, 8)),
3686 	},
3687 	{
3688 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3689 		.name = "Input 4 Sync",
3690 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3691 		.info = snd_rme_sync_state_info,
3692 		.get = snd_rme_digiface_sync_state_get,
3693 		.private_value = 3,
3694 	},
3695 	{
3696 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3697 		.name = "Input 4 Format",
3698 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3699 		.info = snd_rme_digiface_format_info,
3700 		.get = snd_rme_digiface_enum_get,
3701 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, GENMASK(15, 12)) |
3702 			RME_DIGIFACE_INVERT,
3703 	},
3704 	{
3705 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3706 		.name = "Input 4 Rate",
3707 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3708 		.info = snd_rme_digiface_rate_info,
3709 		.get = snd_rme_digiface_rate_get,
3710 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(3, 0)),
3711 	},
3712 	{
3713 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3714 		.name = "Output 1 Format",
3715 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3716 		.info = snd_rme_digiface_format_info,
3717 		.get = snd_rme_digiface_enum_get,
3718 		.put = snd_rme_digiface_enum_put,
3719 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(0)),
3720 	},
3721 	{
3722 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3723 		.name = "Output 2 Format",
3724 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3725 		.info = snd_rme_digiface_format_info,
3726 		.get = snd_rme_digiface_enum_get,
3727 		.put = snd_rme_digiface_enum_put,
3728 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(1)),
3729 	},
3730 	{
3731 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3732 		.name = "Output 3 Format",
3733 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3734 		.info = snd_rme_digiface_format_info,
3735 		.get = snd_rme_digiface_enum_get,
3736 		.put = snd_rme_digiface_enum_put,
3737 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(3)),
3738 	},
3739 	{
3740 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3741 		.name = "Output 4 Format",
3742 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3743 		.info = snd_rme_digiface_format_info,
3744 		.get = snd_rme_digiface_enum_get,
3745 		.put = snd_rme_digiface_enum_put,
3746 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(4)),
3747 	},
3748 	{
3749 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3750 		.name = "Sync Source",
3751 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3752 		.info = snd_rme_digiface_sync_source_info,
3753 		.get = snd_rme_digiface_enum_get,
3754 		.put = snd_rme_digiface_enum_put,
3755 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG1, GENMASK(2, 0)),
3756 	},
3757 	{
3758 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3759 		.name = "Current Sync Source",
3760 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3761 		.info = snd_rme_digiface_sync_source_info,
3762 		.get = snd_rme_digiface_current_sync_get,
3763 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, GENMASK(12, 10)),
3764 	},
3765 	{
3766 		/*
3767 		 * This is writeable, but it is only set by the PCM rate.
3768 		 * Mixer apps currently need to drive the mixer using raw USB requests,
3769 		 * so they can also change this that way to configure the rate for
3770 		 * stand-alone operation when the PCM is closed.
3771 		 */
3772 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3773 		.name = "System Rate",
3774 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3775 		.info = snd_rme_rate_info,
3776 		.get = snd_rme_digiface_rate_get,
3777 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG1, GENMASK(6, 3)),
3778 	},
3779 	{
3780 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3781 		.name = "Current Rate",
3782 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3783 		.info = snd_rme_rate_info,
3784 		.get = snd_rme_digiface_rate_get,
3785 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1H, GENMASK(7, 4)),
3786 	}
3787 };
3788 
3789 static int snd_rme_digiface_controls_create(struct usb_mixer_interface *mixer)
3790 {
3791 	int err, i;
3792 
3793 	for (i = 0; i < ARRAY_SIZE(snd_rme_digiface_controls); ++i) {
3794 		err = add_single_ctl_with_resume(mixer, 0,
3795 						 NULL,
3796 						 &snd_rme_digiface_controls[i],
3797 						 NULL);
3798 		if (err < 0)
3799 			return err;
3800 	}
3801 
3802 	return 0;
3803 }
3804 
3805 /*
3806  * Pioneer DJ / AlphaTheta DJM Mixers
3807  *
3808  * These devices generally have options for soft-switching the playback and
3809  * capture sources in addition to the recording level. Although different
3810  * devices have different configurations, there seems to be canonical values
3811  * for specific capture/playback types:  See the definitions of these below.
3812  *
3813  * The wValue is masked with the stereo channel number. e.g. Setting Ch2 to
3814  * capture phono would be 0x0203. Capture, playback and capture level have
3815  * different wIndexes.
3816  */
3817 
3818 // Capture types
3819 #define SND_DJM_CAP_LINE	0x00
3820 #define SND_DJM_CAP_CDLINE	0x01
3821 #define SND_DJM_CAP_DIGITAL	0x02
3822 #define SND_DJM_CAP_PHONO	0x03
3823 #define SND_DJM_CAP_PREFADER	0x05
3824 #define SND_DJM_CAP_PFADER	0x06
3825 #define SND_DJM_CAP_XFADERA	0x07
3826 #define SND_DJM_CAP_XFADERB	0x08
3827 #define SND_DJM_CAP_MIC		0x09
3828 #define SND_DJM_CAP_AUX		0x0d
3829 #define SND_DJM_CAP_RECOUT	0x0a
3830 #define SND_DJM_CAP_RECOUT_NOMIC	0x0e
3831 #define SND_DJM_CAP_NONE	0x0f
3832 #define SND_DJM_CAP_FXSEND	0x10
3833 #define SND_DJM_CAP_CH1PFADER	0x11
3834 #define SND_DJM_CAP_CH2PFADER	0x12
3835 #define SND_DJM_CAP_CH3PFADER	0x13
3836 #define SND_DJM_CAP_CH4PFADER	0x14
3837 #define SND_DJM_CAP_EXT1SEND	0x21
3838 #define SND_DJM_CAP_EXT2SEND	0x22
3839 #define SND_DJM_CAP_CH1PREFADER	0x31
3840 #define SND_DJM_CAP_CH2PREFADER	0x32
3841 #define SND_DJM_CAP_CH3PREFADER	0x33
3842 #define SND_DJM_CAP_CH4PREFADER	0x34
3843 
3844 // Playback types
3845 #define SND_DJM_PB_CH1		0x00
3846 #define SND_DJM_PB_CH2		0x01
3847 #define SND_DJM_PB_AUX		0x04
3848 
3849 #define SND_DJM_WINDEX_CAP	0x8002
3850 #define SND_DJM_WINDEX_CAPLVL	0x8003
3851 #define SND_DJM_WINDEX_PB	0x8016
3852 
3853 // kcontrol->private_value layout
3854 #define SND_DJM_VALUE_MASK	0x0000ffff
3855 #define SND_DJM_GROUP_MASK	0x00ff0000
3856 #define SND_DJM_DEVICE_MASK	0xff000000
3857 #define SND_DJM_GROUP_SHIFT	16
3858 #define SND_DJM_DEVICE_SHIFT	24
3859 
3860 // device table index
3861 // used for the snd_djm_devices table, so please update accordingly
3862 #define SND_DJM_250MK2_IDX	0x0
3863 #define SND_DJM_750_IDX		0x1
3864 #define SND_DJM_850_IDX		0x2
3865 #define SND_DJM_900NXS2_IDX	0x3
3866 #define SND_DJM_750MK2_IDX	0x4
3867 #define SND_DJM_450_IDX		0x5
3868 #define SND_DJM_A9_IDX		0x6
3869 #define SND_DJM_V10_IDX	0x7
3870 
3871 #define SND_DJM_CTL(_name, suffix, _default_value, _windex) { \
3872 	.name = _name, \
3873 	.options = snd_djm_opts_##suffix, \
3874 	.noptions = ARRAY_SIZE(snd_djm_opts_##suffix), \
3875 	.default_value = _default_value, \
3876 	.wIndex = _windex }
3877 
3878 #define SND_DJM_DEVICE(suffix) { \
3879 	.controls = snd_djm_ctls_##suffix, \
3880 	.ncontrols = ARRAY_SIZE(snd_djm_ctls_##suffix) }
3881 
3882 struct snd_djm_device {
3883 	const char *name;
3884 	const struct snd_djm_ctl *controls;
3885 	size_t ncontrols;
3886 };
3887 
3888 struct snd_djm_ctl {
3889 	const char *name;
3890 	const u16 *options;
3891 	size_t noptions;
3892 	u16 default_value;
3893 	u16 wIndex;
3894 };
3895 
3896 static const char *snd_djm_get_label_caplevel_common(u16 wvalue)
3897 {
3898 	switch (wvalue) {
3899 	case 0x0000:	return "-19dB";
3900 	case 0x0100:	return "-15dB";
3901 	case 0x0200:	return "-10dB";
3902 	case 0x0300:	return "-5dB";
3903 	default:	return NULL;
3904 	}
3905 };
3906 
3907 // Models like DJM-A9 or DJM-V10 have different capture levels than others
3908 static const char *snd_djm_get_label_caplevel_high(u16 wvalue)
3909 {
3910 	switch (wvalue) {
3911 	case 0x0000:	return "+15dB";
3912 	case 0x0100:	return "+12dB";
3913 	case 0x0200:	return "+9dB";
3914 	case 0x0300:	return "+6dB";
3915 	case 0x0400:	return "+3dB";
3916 	case 0x0500:	return "0dB";
3917 	default:	return NULL;
3918 	}
3919 };
3920 
3921 static const char *snd_djm_get_label_cap_common(u16 wvalue)
3922 {
3923 	switch (wvalue & 0x00ff) {
3924 	case SND_DJM_CAP_LINE:		return "Control Tone LINE";
3925 	case SND_DJM_CAP_CDLINE:	return "Control Tone CD/LINE";
3926 	case SND_DJM_CAP_DIGITAL:	return "Control Tone DIGITAL";
3927 	case SND_DJM_CAP_PHONO:		return "Control Tone PHONO";
3928 	case SND_DJM_CAP_PFADER:	return "Post Fader";
3929 	case SND_DJM_CAP_XFADERA:	return "Cross Fader A";
3930 	case SND_DJM_CAP_XFADERB:	return "Cross Fader B";
3931 	case SND_DJM_CAP_MIC:		return "Mic";
3932 	case SND_DJM_CAP_RECOUT:	return "Rec Out";
3933 	case SND_DJM_CAP_RECOUT_NOMIC:	return "Rec Out without Mic";
3934 	case SND_DJM_CAP_AUX:		return "Aux";
3935 	case SND_DJM_CAP_NONE:		return "None";
3936 	case SND_DJM_CAP_FXSEND:	return "FX SEND";
3937 	case SND_DJM_CAP_CH1PREFADER:	return "Pre Fader Ch1";
3938 	case SND_DJM_CAP_CH2PREFADER:	return "Pre Fader Ch2";
3939 	case SND_DJM_CAP_CH3PREFADER:	return "Pre Fader Ch3";
3940 	case SND_DJM_CAP_CH4PREFADER:	return "Pre Fader Ch4";
3941 	case SND_DJM_CAP_CH1PFADER:	return "Post Fader Ch1";
3942 	case SND_DJM_CAP_CH2PFADER:	return "Post Fader Ch2";
3943 	case SND_DJM_CAP_CH3PFADER:	return "Post Fader Ch3";
3944 	case SND_DJM_CAP_CH4PFADER:	return "Post Fader Ch4";
3945 	case SND_DJM_CAP_EXT1SEND:	return "EXT1 SEND";
3946 	case SND_DJM_CAP_EXT2SEND:	return "EXT2 SEND";
3947 	default:			return NULL;
3948 	}
3949 };
3950 
3951 // The DJM-850 has different values for CD/LINE and LINE capture
3952 // control options than the other DJM declared in this file.
3953 static const char *snd_djm_get_label_cap_850(u16 wvalue)
3954 {
3955 	switch (wvalue & 0x00ff) {
3956 	case 0x00:		return "Control Tone CD/LINE";
3957 	case 0x01:		return "Control Tone LINE";
3958 	default:		return snd_djm_get_label_cap_common(wvalue);
3959 	}
3960 };
3961 
3962 static const char *snd_djm_get_label_caplevel(u8 device_idx, u16 wvalue)
3963 {
3964 	switch (device_idx) {
3965 	case SND_DJM_A9_IDX:		return snd_djm_get_label_caplevel_high(wvalue);
3966 	case SND_DJM_V10_IDX:		return snd_djm_get_label_caplevel_high(wvalue);
3967 	default:			return snd_djm_get_label_caplevel_common(wvalue);
3968 	}
3969 };
3970 
3971 static const char *snd_djm_get_label_cap(u8 device_idx, u16 wvalue)
3972 {
3973 	switch (device_idx) {
3974 	case SND_DJM_850_IDX:		return snd_djm_get_label_cap_850(wvalue);
3975 	default:			return snd_djm_get_label_cap_common(wvalue);
3976 	}
3977 };
3978 
3979 static const char *snd_djm_get_label_pb(u16 wvalue)
3980 {
3981 	switch (wvalue & 0x00ff) {
3982 	case SND_DJM_PB_CH1:	return "Ch1";
3983 	case SND_DJM_PB_CH2:	return "Ch2";
3984 	case SND_DJM_PB_AUX:	return "Aux";
3985 	default:		return NULL;
3986 	}
3987 };
3988 
3989 static const char *snd_djm_get_label(u8 device_idx, u16 wvalue, u16 windex)
3990 {
3991 	switch (windex) {
3992 	case SND_DJM_WINDEX_CAPLVL:	return snd_djm_get_label_caplevel(device_idx, wvalue);
3993 	case SND_DJM_WINDEX_CAP:	return snd_djm_get_label_cap(device_idx, wvalue);
3994 	case SND_DJM_WINDEX_PB:		return snd_djm_get_label_pb(wvalue);
3995 	default:			return NULL;
3996 	}
3997 };
3998 
3999 // common DJM capture level option values
4000 static const u16 snd_djm_opts_cap_level[] = {
4001 	0x0000, 0x0100, 0x0200, 0x0300 };
4002 
4003 // DJM-250MK2
4004 static const u16 snd_djm_opts_250mk2_cap1[] = {
4005 	0x0103, 0x0100, 0x0106, 0x0107, 0x0108, 0x0109, 0x010d, 0x010a };
4006 
4007 static const u16 snd_djm_opts_250mk2_cap2[] = {
4008 	0x0203, 0x0200, 0x0206, 0x0207, 0x0208, 0x0209, 0x020d, 0x020a };
4009 
4010 static const u16 snd_djm_opts_250mk2_cap3[] = {
4011 	0x030a, 0x0311, 0x0312, 0x0307, 0x0308, 0x0309, 0x030d };
4012 
4013 static const u16 snd_djm_opts_250mk2_pb1[] = { 0x0100, 0x0101, 0x0104 };
4014 static const u16 snd_djm_opts_250mk2_pb2[] = { 0x0200, 0x0201, 0x0204 };
4015 static const u16 snd_djm_opts_250mk2_pb3[] = { 0x0300, 0x0301, 0x0304 };
4016 
4017 static const struct snd_djm_ctl snd_djm_ctls_250mk2[] = {
4018 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4019 	SND_DJM_CTL("Input 1 Capture Switch",  250mk2_cap1, 2, SND_DJM_WINDEX_CAP),
4020 	SND_DJM_CTL("Input 2 Capture Switch",  250mk2_cap2, 2, SND_DJM_WINDEX_CAP),
4021 	SND_DJM_CTL("Input 3 Capture Switch",  250mk2_cap3, 0, SND_DJM_WINDEX_CAP),
4022 	SND_DJM_CTL("Output 1 Playback Switch", 250mk2_pb1, 0, SND_DJM_WINDEX_PB),
4023 	SND_DJM_CTL("Output 2 Playback Switch", 250mk2_pb2, 1, SND_DJM_WINDEX_PB),
4024 	SND_DJM_CTL("Output 3 Playback Switch", 250mk2_pb3, 2, SND_DJM_WINDEX_PB)
4025 };
4026 
4027 // DJM-450
4028 static const u16 snd_djm_opts_450_cap1[] = {
4029 	0x0103, 0x0100, 0x0106, 0x0107, 0x0108, 0x0109, 0x010d, 0x010a };
4030 
4031 static const u16 snd_djm_opts_450_cap2[] = {
4032 	0x0203, 0x0200, 0x0206, 0x0207, 0x0208, 0x0209, 0x020d, 0x020a };
4033 
4034 static const u16 snd_djm_opts_450_cap3[] = {
4035 	0x030a, 0x0311, 0x0312, 0x0307, 0x0308, 0x0309, 0x030d };
4036 
4037 static const u16 snd_djm_opts_450_pb1[] = { 0x0100, 0x0101, 0x0104 };
4038 static const u16 snd_djm_opts_450_pb2[] = { 0x0200, 0x0201, 0x0204 };
4039 static const u16 snd_djm_opts_450_pb3[] = { 0x0300, 0x0301, 0x0304 };
4040 
4041 static const struct snd_djm_ctl snd_djm_ctls_450[] = {
4042 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4043 	SND_DJM_CTL("Input 1 Capture Switch",  450_cap1, 2, SND_DJM_WINDEX_CAP),
4044 	SND_DJM_CTL("Input 2 Capture Switch",  450_cap2, 2, SND_DJM_WINDEX_CAP),
4045 	SND_DJM_CTL("Input 3 Capture Switch",  450_cap3, 0, SND_DJM_WINDEX_CAP),
4046 	SND_DJM_CTL("Output 1 Playback Switch", 450_pb1, 0, SND_DJM_WINDEX_PB),
4047 	SND_DJM_CTL("Output 2 Playback Switch", 450_pb2, 1, SND_DJM_WINDEX_PB),
4048 	SND_DJM_CTL("Output 3 Playback Switch", 450_pb3, 2, SND_DJM_WINDEX_PB)
4049 };
4050 
4051 // DJM-750
4052 static const u16 snd_djm_opts_750_cap1[] = {
4053 	0x0101, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a, 0x010f };
4054 static const u16 snd_djm_opts_750_cap2[] = {
4055 	0x0200, 0x0201, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020f };
4056 static const u16 snd_djm_opts_750_cap3[] = {
4057 	0x0300, 0x0301, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030f };
4058 static const u16 snd_djm_opts_750_cap4[] = {
4059 	0x0401, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040f };
4060 
4061 static const struct snd_djm_ctl snd_djm_ctls_750[] = {
4062 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4063 	SND_DJM_CTL("Input 1 Capture Switch", 750_cap1, 2, SND_DJM_WINDEX_CAP),
4064 	SND_DJM_CTL("Input 2 Capture Switch", 750_cap2, 2, SND_DJM_WINDEX_CAP),
4065 	SND_DJM_CTL("Input 3 Capture Switch", 750_cap3, 0, SND_DJM_WINDEX_CAP),
4066 	SND_DJM_CTL("Input 4 Capture Switch", 750_cap4, 0, SND_DJM_WINDEX_CAP)
4067 };
4068 
4069 // DJM-850
4070 static const u16 snd_djm_opts_850_cap1[] = {
4071 	0x0100, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a, 0x010f };
4072 static const u16 snd_djm_opts_850_cap2[] = {
4073 	0x0200, 0x0201, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020f };
4074 static const u16 snd_djm_opts_850_cap3[] = {
4075 	0x0300, 0x0301, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030f };
4076 static const u16 snd_djm_opts_850_cap4[] = {
4077 	0x0400, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040f };
4078 
4079 static const struct snd_djm_ctl snd_djm_ctls_850[] = {
4080 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4081 	SND_DJM_CTL("Input 1 Capture Switch", 850_cap1, 1, SND_DJM_WINDEX_CAP),
4082 	SND_DJM_CTL("Input 2 Capture Switch", 850_cap2, 0, SND_DJM_WINDEX_CAP),
4083 	SND_DJM_CTL("Input 3 Capture Switch", 850_cap3, 0, SND_DJM_WINDEX_CAP),
4084 	SND_DJM_CTL("Input 4 Capture Switch", 850_cap4, 1, SND_DJM_WINDEX_CAP)
4085 };
4086 
4087 // DJM-900NXS2
4088 static const u16 snd_djm_opts_900nxs2_cap1[] = {
4089 	0x0100, 0x0102, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a };
4090 static const u16 snd_djm_opts_900nxs2_cap2[] = {
4091 	0x0200, 0x0202, 0x0203, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a };
4092 static const u16 snd_djm_opts_900nxs2_cap3[] = {
4093 	0x0300, 0x0302, 0x0303, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a };
4094 static const u16 snd_djm_opts_900nxs2_cap4[] = {
4095 	0x0400, 0x0402, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a };
4096 static const u16 snd_djm_opts_900nxs2_cap5[] = {
4097 	0x0507, 0x0508, 0x0509, 0x050a, 0x0511, 0x0512, 0x0513, 0x0514 };
4098 
4099 static const struct snd_djm_ctl snd_djm_ctls_900nxs2[] = {
4100 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4101 	SND_DJM_CTL("Input 1 Capture Switch", 900nxs2_cap1, 2, SND_DJM_WINDEX_CAP),
4102 	SND_DJM_CTL("Input 2 Capture Switch", 900nxs2_cap2, 2, SND_DJM_WINDEX_CAP),
4103 	SND_DJM_CTL("Input 3 Capture Switch", 900nxs2_cap3, 2, SND_DJM_WINDEX_CAP),
4104 	SND_DJM_CTL("Input 4 Capture Switch", 900nxs2_cap4, 2, SND_DJM_WINDEX_CAP),
4105 	SND_DJM_CTL("Input 5 Capture Switch", 900nxs2_cap5, 3, SND_DJM_WINDEX_CAP)
4106 };
4107 
4108 // DJM-750MK2
4109 static const u16 snd_djm_opts_750mk2_cap1[] = {
4110 	0x0100, 0x0102, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a };
4111 static const u16 snd_djm_opts_750mk2_cap2[] = {
4112 	0x0200, 0x0202, 0x0203, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a };
4113 static const u16 snd_djm_opts_750mk2_cap3[] = {
4114 	0x0300, 0x0302, 0x0303, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a };
4115 static const u16 snd_djm_opts_750mk2_cap4[] = {
4116 	0x0400, 0x0402, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a };
4117 static const u16 snd_djm_opts_750mk2_cap5[] = {
4118 	0x0507, 0x0508, 0x0509, 0x050a, 0x0511, 0x0512, 0x0513, 0x0514 };
4119 
4120 static const u16 snd_djm_opts_750mk2_pb1[] = { 0x0100, 0x0101, 0x0104 };
4121 static const u16 snd_djm_opts_750mk2_pb2[] = { 0x0200, 0x0201, 0x0204 };
4122 static const u16 snd_djm_opts_750mk2_pb3[] = { 0x0300, 0x0301, 0x0304 };
4123 
4124 static const struct snd_djm_ctl snd_djm_ctls_750mk2[] = {
4125 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4126 	SND_DJM_CTL("Input 1 Capture Switch",   750mk2_cap1, 2, SND_DJM_WINDEX_CAP),
4127 	SND_DJM_CTL("Input 2 Capture Switch",   750mk2_cap2, 2, SND_DJM_WINDEX_CAP),
4128 	SND_DJM_CTL("Input 3 Capture Switch",   750mk2_cap3, 2, SND_DJM_WINDEX_CAP),
4129 	SND_DJM_CTL("Input 4 Capture Switch",   750mk2_cap4, 2, SND_DJM_WINDEX_CAP),
4130 	SND_DJM_CTL("Input 5 Capture Switch",   750mk2_cap5, 3, SND_DJM_WINDEX_CAP),
4131 	SND_DJM_CTL("Output 1 Playback Switch", 750mk2_pb1, 0, SND_DJM_WINDEX_PB),
4132 	SND_DJM_CTL("Output 2 Playback Switch", 750mk2_pb2, 1, SND_DJM_WINDEX_PB),
4133 	SND_DJM_CTL("Output 3 Playback Switch", 750mk2_pb3, 2, SND_DJM_WINDEX_PB)
4134 };
4135 
4136 // DJM-A9
4137 static const u16 snd_djm_opts_a9_cap_level[] = {
4138 	0x0000, 0x0100, 0x0200, 0x0300, 0x0400, 0x0500 };
4139 static const u16 snd_djm_opts_a9_cap1[] = {
4140 	0x0107, 0x0108, 0x0109, 0x010a, 0x010e,
4141 	0x111, 0x112, 0x113, 0x114, 0x0131, 0x132, 0x133, 0x134 };
4142 static const u16 snd_djm_opts_a9_cap2[] = {
4143 	0x0201, 0x0202, 0x0203, 0x0205, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020e };
4144 static const u16 snd_djm_opts_a9_cap3[] = {
4145 	0x0301, 0x0302, 0x0303, 0x0305, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030e };
4146 static const u16 snd_djm_opts_a9_cap4[] = {
4147 	0x0401, 0x0402, 0x0403, 0x0405, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040e };
4148 static const u16 snd_djm_opts_a9_cap5[] = {
4149 	0x0501, 0x0502, 0x0503, 0x0505, 0x0506, 0x0507, 0x0508, 0x0509, 0x050a, 0x050e };
4150 
4151 static const struct snd_djm_ctl snd_djm_ctls_a9[] = {
4152 	SND_DJM_CTL("Master Input Level Capture Switch", a9_cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4153 	SND_DJM_CTL("Master Input Capture Switch", a9_cap1, 3, SND_DJM_WINDEX_CAP),
4154 	SND_DJM_CTL("Input 1 Capture Switch",  a9_cap2, 2, SND_DJM_WINDEX_CAP),
4155 	SND_DJM_CTL("Input 2 Capture Switch",  a9_cap3, 2, SND_DJM_WINDEX_CAP),
4156 	SND_DJM_CTL("Input 3 Capture Switch",  a9_cap4, 2, SND_DJM_WINDEX_CAP),
4157 	SND_DJM_CTL("Input 4 Capture Switch",  a9_cap5, 2, SND_DJM_WINDEX_CAP)
4158 };
4159 
4160 // DJM-V10
4161 static const u16 snd_djm_opts_v10_cap_level[] = {
4162 	0x0000, 0x0100, 0x0200, 0x0300, 0x0400, 0x0500
4163 };
4164 
4165 static const u16 snd_djm_opts_v10_cap1[] = {
4166 	0x0103,
4167 	0x0100, 0x0102, 0x0106, 0x0110, 0x0107,
4168 	0x0108, 0x0109, 0x010a, 0x0121, 0x0122
4169 };
4170 
4171 static const u16 snd_djm_opts_v10_cap2[] = {
4172 	0x0200, 0x0202, 0x0206, 0x0210, 0x0207,
4173 	0x0208, 0x0209, 0x020a, 0x0221, 0x0222
4174 };
4175 
4176 static const u16 snd_djm_opts_v10_cap3[] = {
4177 	0x0303,
4178 	0x0300, 0x0302, 0x0306, 0x0310, 0x0307,
4179 	0x0308, 0x0309, 0x030a, 0x0321, 0x0322
4180 };
4181 
4182 static const u16 snd_djm_opts_v10_cap4[] = {
4183 	0x0403,
4184 	0x0400, 0x0402, 0x0406, 0x0410, 0x0407,
4185 	0x0408, 0x0409, 0x040a, 0x0421, 0x0422
4186 };
4187 
4188 static const u16 snd_djm_opts_v10_cap5[] = {
4189 	0x0500, 0x0502, 0x0506, 0x0510, 0x0507,
4190 	0x0508, 0x0509, 0x050a, 0x0521, 0x0522
4191 };
4192 
4193 static const u16 snd_djm_opts_v10_cap6[] = {
4194 	0x0603,
4195 	0x0600, 0x0602, 0x0606, 0x0610, 0x0607,
4196 	0x0608, 0x0609, 0x060a, 0x0621, 0x0622
4197 };
4198 
4199 static const struct snd_djm_ctl snd_djm_ctls_v10[] = {
4200 	SND_DJM_CTL("Master Input Level Capture Switch", v10_cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4201 	SND_DJM_CTL("Input 1 Capture Switch", v10_cap1, 2, SND_DJM_WINDEX_CAP),
4202 	SND_DJM_CTL("Input 2 Capture Switch", v10_cap2, 2, SND_DJM_WINDEX_CAP),
4203 	SND_DJM_CTL("Input 3 Capture Switch", v10_cap3, 0, SND_DJM_WINDEX_CAP),
4204 	SND_DJM_CTL("Input 4 Capture Switch", v10_cap4, 0, SND_DJM_WINDEX_CAP),
4205 	SND_DJM_CTL("Input 5 Capture Switch", v10_cap5, 0, SND_DJM_WINDEX_CAP),
4206 	SND_DJM_CTL("Input 6 Capture Switch", v10_cap6, 0, SND_DJM_WINDEX_CAP)
4207 	// playback channels are fixed and controlled by hardware knobs on the mixer
4208 };
4209 
4210 static const struct snd_djm_device snd_djm_devices[] = {
4211 	[SND_DJM_250MK2_IDX] = SND_DJM_DEVICE(250mk2),
4212 	[SND_DJM_750_IDX] = SND_DJM_DEVICE(750),
4213 	[SND_DJM_850_IDX] = SND_DJM_DEVICE(850),
4214 	[SND_DJM_900NXS2_IDX] = SND_DJM_DEVICE(900nxs2),
4215 	[SND_DJM_750MK2_IDX] = SND_DJM_DEVICE(750mk2),
4216 	[SND_DJM_450_IDX] = SND_DJM_DEVICE(450),
4217 	[SND_DJM_A9_IDX] = SND_DJM_DEVICE(a9),
4218 	[SND_DJM_V10_IDX] = SND_DJM_DEVICE(v10),
4219 };
4220 
4221 static int snd_djm_controls_info(struct snd_kcontrol *kctl,
4222 				 struct snd_ctl_elem_info *info)
4223 {
4224 	unsigned long private_value = kctl->private_value;
4225 	u8 device_idx = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
4226 	u8 ctl_idx = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
4227 	const struct snd_djm_device *device = &snd_djm_devices[device_idx];
4228 	const char *name;
4229 	const struct snd_djm_ctl *ctl;
4230 	size_t noptions;
4231 
4232 	if (ctl_idx >= device->ncontrols)
4233 		return -EINVAL;
4234 
4235 	ctl = &device->controls[ctl_idx];
4236 	noptions = ctl->noptions;
4237 	if (info->value.enumerated.item >= noptions)
4238 		info->value.enumerated.item = noptions - 1;
4239 
4240 	name = snd_djm_get_label(device_idx,
4241 				 ctl->options[info->value.enumerated.item],
4242 				 ctl->wIndex);
4243 	if (!name)
4244 		return -EINVAL;
4245 
4246 	strscpy(info->value.enumerated.name, name, sizeof(info->value.enumerated.name));
4247 	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4248 	info->count = 1;
4249 	info->value.enumerated.items = noptions;
4250 	return 0;
4251 }
4252 
4253 static int snd_djm_controls_update(struct usb_mixer_interface *mixer,
4254 				   u8 device_idx, u8 group, u16 value)
4255 {
4256 	const struct snd_djm_device *device = &snd_djm_devices[device_idx];
4257 
4258 	if (group >= device->ncontrols || value >= device->controls[group].noptions)
4259 		return -EINVAL;
4260 
4261 	CLASS(snd_usb_lock, pm)(mixer->chip);
4262 	if (pm.err)
4263 		return pm.err;
4264 
4265 	return snd_usb_ctl_msg(mixer->chip->dev,
4266 			       usb_sndctrlpipe(mixer->chip->dev, 0),
4267 			       USB_REQ_SET_FEATURE,
4268 			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
4269 			       device->controls[group].options[value],
4270 			       device->controls[group].wIndex,
4271 			       NULL, 0);
4272 }
4273 
4274 static int snd_djm_controls_get(struct snd_kcontrol *kctl,
4275 				struct snd_ctl_elem_value *elem)
4276 {
4277 	elem->value.enumerated.item[0] = kctl->private_value & SND_DJM_VALUE_MASK;
4278 	return 0;
4279 }
4280 
4281 static int snd_djm_controls_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *elem)
4282 {
4283 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
4284 	struct usb_mixer_interface *mixer = list->mixer;
4285 	unsigned long private_value = kctl->private_value;
4286 
4287 	u8 device = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
4288 	u8 group = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
4289 	u16 value = elem->value.enumerated.item[0];
4290 
4291 	kctl->private_value = (((unsigned long)device << SND_DJM_DEVICE_SHIFT) |
4292 			      (group << SND_DJM_GROUP_SHIFT) |
4293 			      value);
4294 
4295 	return snd_djm_controls_update(mixer, device, group, value);
4296 }
4297 
4298 static int snd_djm_controls_resume(struct usb_mixer_elem_list *list)
4299 {
4300 	unsigned long private_value = list->kctl->private_value;
4301 	u8 device = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
4302 	u8 group = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
4303 	u16 value = (private_value & SND_DJM_VALUE_MASK);
4304 
4305 	return snd_djm_controls_update(list->mixer, device, group, value);
4306 }
4307 
4308 static int snd_djm_controls_create(struct usb_mixer_interface *mixer,
4309 				   const u8 device_idx)
4310 {
4311 	int err, i;
4312 	u16 value;
4313 
4314 	const struct snd_djm_device *device = &snd_djm_devices[device_idx];
4315 
4316 	struct snd_kcontrol_new knew = {
4317 		.iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
4318 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
4319 		.index = 0,
4320 		.info = snd_djm_controls_info,
4321 		.get  = snd_djm_controls_get,
4322 		.put  = snd_djm_controls_put
4323 	};
4324 
4325 	for (i = 0; i < device->ncontrols; i++) {
4326 		value = device->controls[i].default_value;
4327 		knew.name = device->controls[i].name;
4328 		knew.private_value =
4329 			((unsigned long)device_idx << SND_DJM_DEVICE_SHIFT) |
4330 			(i << SND_DJM_GROUP_SHIFT) |
4331 			value;
4332 		err = snd_djm_controls_update(mixer, device_idx, i, value);
4333 		if (err)
4334 			return err;
4335 		err = add_single_ctl_with_resume(mixer, 0, snd_djm_controls_resume,
4336 						 &knew, NULL);
4337 		if (err)
4338 			return err;
4339 	}
4340 	return 0;
4341 }
4342 
4343 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
4344 {
4345 	int err = 0;
4346 
4347 	err = snd_usb_soundblaster_remote_init(mixer);
4348 	if (err < 0)
4349 		return err;
4350 
4351 	switch (mixer->chip->usb_id) {
4352 	/* Tascam US-16x08 */
4353 	case USB_ID(0x0644, 0x8047):
4354 		err = snd_us16x08_controls_create(mixer);
4355 		break;
4356 	case USB_ID(0x041e, 0x3020):
4357 	case USB_ID(0x041e, 0x3040):
4358 	case USB_ID(0x041e, 0x3042):
4359 	case USB_ID(0x041e, 0x30df):
4360 	case USB_ID(0x041e, 0x3048):
4361 		err = snd_audigy2nx_controls_create(mixer);
4362 		if (err < 0)
4363 			break;
4364 		snd_card_ro_proc_new(mixer->chip->card, "audigy2nx",
4365 				     mixer, snd_audigy2nx_proc_read);
4366 		break;
4367 
4368 	/* EMU0204 */
4369 	case USB_ID(0x041e, 0x3f19):
4370 		err = snd_emu0204_controls_create(mixer);
4371 		break;
4372 
4373 #if IS_REACHABLE(CONFIG_INPUT)
4374 	case USB_ID(0x054c, 0x0ce6): /* Sony DualSense controller (PS5) */
4375 	case USB_ID(0x054c, 0x0df2): /* Sony DualSense Edge controller (PS5) */
4376 		err = snd_dualsense_controls_create(mixer);
4377 		break;
4378 #endif /* IS_REACHABLE(CONFIG_INPUT) */
4379 
4380 	case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
4381 	case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
4382 		err = snd_c400_create_mixer(mixer);
4383 		break;
4384 
4385 	case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
4386 	case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
4387 		err = snd_ftu_create_mixer(mixer);
4388 		break;
4389 
4390 	case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
4391 	case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
4392 	case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
4393 		err = snd_xonar_u1_controls_create(mixer);
4394 		break;
4395 
4396 	case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
4397 		err = snd_microii_controls_create(mixer);
4398 		break;
4399 
4400 	case USB_ID(0x0dba, 0x1000): /* Digidesign Mbox 1 */
4401 		err = snd_mbox1_controls_create(mixer);
4402 		break;
4403 
4404 	case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
4405 		err = snd_nativeinstruments_create_mixer(/* checkpatch hack */
4406 				mixer,
4407 				snd_nativeinstruments_ta6_mixers,
4408 				ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
4409 		break;
4410 
4411 	case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
4412 		err = snd_nativeinstruments_create_mixer(/* checkpatch hack */
4413 				mixer,
4414 				snd_nativeinstruments_ta10_mixers,
4415 				ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
4416 		break;
4417 
4418 	case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
4419 		/* detection is disabled in mixer_maps.c */
4420 		err = snd_create_std_mono_table(mixer, ebox44_table);
4421 		break;
4422 
4423 	case USB_ID(0x1235, 0x8012): /* Focusrite Scarlett 6i6 */
4424 	case USB_ID(0x1235, 0x8002): /* Focusrite Scarlett 8i6 */
4425 	case USB_ID(0x1235, 0x8004): /* Focusrite Scarlett 18i6 */
4426 	case USB_ID(0x1235, 0x8014): /* Focusrite Scarlett 18i8 */
4427 	case USB_ID(0x1235, 0x800c): /* Focusrite Scarlett 18i20 */
4428 		err = snd_scarlett_controls_create(mixer);
4429 		break;
4430 
4431 	case USB_ID(0x1235, 0x8203): /* Focusrite Scarlett 6i6 2nd Gen */
4432 	case USB_ID(0x1235, 0x8204): /* Focusrite Scarlett 18i8 2nd Gen */
4433 	case USB_ID(0x1235, 0x8201): /* Focusrite Scarlett 18i20 2nd Gen */
4434 	case USB_ID(0x1235, 0x8211): /* Focusrite Scarlett Solo 3rd Gen */
4435 	case USB_ID(0x1235, 0x8210): /* Focusrite Scarlett 2i2 3rd Gen */
4436 	case USB_ID(0x1235, 0x8212): /* Focusrite Scarlett 4i4 3rd Gen */
4437 	case USB_ID(0x1235, 0x8213): /* Focusrite Scarlett 8i6 3rd Gen */
4438 	case USB_ID(0x1235, 0x8214): /* Focusrite Scarlett 18i8 3rd Gen */
4439 	case USB_ID(0x1235, 0x8215): /* Focusrite Scarlett 18i20 3rd Gen */
4440 	case USB_ID(0x1235, 0x8216): /* Focusrite Vocaster One */
4441 	case USB_ID(0x1235, 0x8217): /* Focusrite Vocaster Two */
4442 	case USB_ID(0x1235, 0x8218): /* Focusrite Scarlett Solo 4th Gen */
4443 	case USB_ID(0x1235, 0x8219): /* Focusrite Scarlett 2i2 4th Gen */
4444 	case USB_ID(0x1235, 0x821a): /* Focusrite Scarlett 4i4 4th Gen */
4445 	case USB_ID(0x1235, 0x8206): /* Focusrite Clarett 2Pre USB */
4446 	case USB_ID(0x1235, 0x8207): /* Focusrite Clarett 4Pre USB */
4447 	case USB_ID(0x1235, 0x8208): /* Focusrite Clarett 8Pre USB */
4448 	case USB_ID(0x1235, 0x820a): /* Focusrite Clarett+ 2Pre */
4449 	case USB_ID(0x1235, 0x820b): /* Focusrite Clarett+ 4Pre */
4450 	case USB_ID(0x1235, 0x820c): /* Focusrite Clarett+ 8Pre */
4451 		err = snd_scarlett2_init(mixer);
4452 		break;
4453 
4454 	case USB_ID(0x1235, 0x821b): /* Focusrite Scarlett 16i16 4th Gen */
4455 	case USB_ID(0x1235, 0x821c): /* Focusrite Scarlett 18i16 4th Gen */
4456 	case USB_ID(0x1235, 0x821d): /* Focusrite Scarlett 18i20 4th Gen */
4457 		err = snd_fcp_init(mixer);
4458 		break;
4459 
4460 	case USB_ID(0x041e, 0x323b): /* Creative Sound Blaster E1 */
4461 		err = snd_soundblaster_e1_switch_create(mixer);
4462 		break;
4463 	case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
4464 		err = dell_dock_mixer_create(mixer);
4465 		if (err < 0)
4466 			break;
4467 		err = dell_dock_mixer_init(mixer);
4468 		break;
4469 	case USB_ID(0x0bda, 0x402e): /* Dell WD19 dock */
4470 		err = dell_dock_mixer_create(mixer);
4471 		break;
4472 
4473 	case USB_ID(0x2a39, 0x3fd2): /* RME ADI-2 Pro */
4474 	case USB_ID(0x2a39, 0x3fd3): /* RME ADI-2 DAC */
4475 	case USB_ID(0x2a39, 0x3fd4): /* RME */
4476 		err = snd_rme_controls_create(mixer);
4477 		break;
4478 
4479 	case USB_ID(0x194f, 0x010c): /* Presonus Studio 1810c */
4480 		err = snd_sc1810_init_mixer(mixer);
4481 		break;
4482 	case USB_ID(0x194f, 0x010d): /* Presonus Studio 1824c */
4483 		err = snd_sc1810_init_mixer(mixer);
4484 		break;
4485 	case USB_ID(0x2a39, 0x3fb0): /* RME Babyface Pro FS */
4486 		err = snd_bbfpro_controls_create(mixer);
4487 		break;
4488 	case USB_ID(0x2a39, 0x3f8c): /* RME Digiface USB */
4489 	case USB_ID(0x2a39, 0x3fa0): /* RME Digiface USB (alternate) */
4490 		err = snd_rme_digiface_controls_create(mixer);
4491 		break;
4492 	case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */
4493 		err = snd_djm_controls_create(mixer, SND_DJM_250MK2_IDX);
4494 		break;
4495 	case USB_ID(0x2b73, 0x0013): /* Pioneer DJ DJM-450 */
4496 		err = snd_djm_controls_create(mixer, SND_DJM_450_IDX);
4497 		break;
4498 	case USB_ID(0x08e4, 0x017f): /* Pioneer DJ DJM-750 */
4499 		err = snd_djm_controls_create(mixer, SND_DJM_750_IDX);
4500 		break;
4501 	case USB_ID(0x2b73, 0x001b): /* Pioneer DJ DJM-750MK2 */
4502 		err = snd_djm_controls_create(mixer, SND_DJM_750MK2_IDX);
4503 		break;
4504 	case USB_ID(0x08e4, 0x0163): /* Pioneer DJ DJM-850 */
4505 		err = snd_djm_controls_create(mixer, SND_DJM_850_IDX);
4506 		break;
4507 	case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */
4508 		err = snd_djm_controls_create(mixer, SND_DJM_900NXS2_IDX);
4509 		break;
4510 	case USB_ID(0x2b73, 0x003c): /* Pioneer DJ / AlphaTheta DJM-A9 */
4511 		err = snd_djm_controls_create(mixer, SND_DJM_A9_IDX);
4512 		break;
4513 	case USB_ID(0x2b73, 0x0034): /* Pioneer DJ DJM-V10 */
4514 		err = snd_djm_controls_create(mixer, SND_DJM_V10_IDX);
4515 		break;
4516 	case USB_ID(0x03f0, 0x0269): /* HP TB Dock G2 */
4517 		err = hp_dock_mixer_create(mixer);
4518 		break;
4519 	}
4520 
4521 	return err;
4522 }
4523 
4524 void snd_usb_mixer_resume_quirk(struct usb_mixer_interface *mixer)
4525 {
4526 	switch (mixer->chip->usb_id) {
4527 	case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
4528 		dell_dock_mixer_init(mixer);
4529 		break;
4530 	}
4531 }
4532 
4533 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
4534 				    int unitid)
4535 {
4536 	if (!mixer->rc_cfg)
4537 		return;
4538 	/* unit ids specific to Extigy/Audigy 2 NX: */
4539 	switch (unitid) {
4540 	case 0: /* remote control */
4541 		mixer->rc_urb->dev = mixer->chip->dev;
4542 		usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
4543 		break;
4544 	case 4: /* digital in jack */
4545 	case 7: /* line in jacks */
4546 	case 19: /* speaker out jacks */
4547 	case 20: /* headphones out jack */
4548 		break;
4549 	/* live24ext: 4 = line-in jack */
4550 	case 3:	/* hp-out jack (may actuate Mute) */
4551 		if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
4552 		    mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
4553 			snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
4554 		break;
4555 	default:
4556 		usb_audio_dbg(mixer->chip, "memory change in unknown unit %d\n", unitid);
4557 		break;
4558 	}
4559 }
4560 
4561 static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
4562 					 struct usb_mixer_elem_info *cval,
4563 					 struct snd_kcontrol *kctl)
4564 {
4565 	/* Approximation using 10 ranges based on output measurement on hw v1.2.
4566 	 * This seems close to the cubic mapping e.g. alsamixer uses.
4567 	 */
4568 	static const DECLARE_TLV_DB_RANGE(scale,
4569 		 0,  1, TLV_DB_MINMAX_ITEM(-5300, -4970),
4570 		 2,  5, TLV_DB_MINMAX_ITEM(-4710, -4160),
4571 		 6,  7, TLV_DB_MINMAX_ITEM(-3884, -3710),
4572 		 8, 14, TLV_DB_MINMAX_ITEM(-3443, -2560),
4573 		15, 16, TLV_DB_MINMAX_ITEM(-2475, -2324),
4574 		17, 19, TLV_DB_MINMAX_ITEM(-2228, -2031),
4575 		20, 26, TLV_DB_MINMAX_ITEM(-1910, -1393),
4576 		27, 31, TLV_DB_MINMAX_ITEM(-1322, -1032),
4577 		32, 40, TLV_DB_MINMAX_ITEM(-968, -490),
4578 		41, 50, TLV_DB_MINMAX_ITEM(-441, 0),
4579 	);
4580 
4581 	if (cval->min == 0 && cval->max == 50) {
4582 		usb_audio_info(mixer->chip, "applying DragonFly dB scale quirk (0-50 variant)\n");
4583 		kctl->tlv.p = scale;
4584 		kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
4585 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
4586 
4587 	} else if (cval->min == 0 && cval->max <= 1000) {
4588 		/* Some other clearly broken DragonFly variant.
4589 		 * At least a 0..53 variant (hw v1.0) exists.
4590 		 */
4591 		usb_audio_info(mixer->chip, "ignoring too narrow dB range on a DragonFly device");
4592 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
4593 	}
4594 }
4595 
4596 /*
4597  * Some Plantronics headsets have control names that don't meet ALSA naming
4598  * standards. This function fixes nonstandard source names. By the time
4599  * this function is called the control name should look like one of these:
4600  * "source names Playback Volume"
4601  * "source names Playback Switch"
4602  * "source names Capture Volume"
4603  * "source names Capture Switch"
4604  * If any of the trigger words are found in the name then the name will
4605  * be changed to:
4606  * "Headset Playback Volume"
4607  * "Headset Playback Switch"
4608  * "Headset Capture Volume"
4609  * "Headset Capture Switch"
4610  * depending on the current suffix.
4611  */
4612 static void snd_fix_plt_name(struct snd_usb_audio *chip,
4613 			     struct snd_ctl_elem_id *id)
4614 {
4615 	/* no variant of "Sidetone" should be added to this list */
4616 	static const char * const trigger[] = {
4617 		"Earphone", "Microphone", "Receive", "Transmit"
4618 	};
4619 	static const char * const suffix[] = {
4620 		" Playback Volume", " Playback Switch",
4621 		" Capture Volume", " Capture Switch"
4622 	};
4623 	int i;
4624 
4625 	for (i = 0; i < ARRAY_SIZE(trigger); i++)
4626 		if (strstr(id->name, trigger[i]))
4627 			goto triggered;
4628 	usb_audio_dbg(chip, "no change in %s\n", id->name);
4629 	return;
4630 
4631 triggered:
4632 	for (i = 0; i < ARRAY_SIZE(suffix); i++)
4633 		if (strstr(id->name, suffix[i])) {
4634 			usb_audio_dbg(chip, "fixing kctl name %s\n", id->name);
4635 			snprintf(id->name, sizeof(id->name), "Headset%s",
4636 				 suffix[i]);
4637 			return;
4638 		}
4639 	usb_audio_dbg(chip, "something wrong in kctl name %s\n", id->name);
4640 }
4641 
4642 void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
4643 				  struct usb_mixer_elem_info *cval, int unitid,
4644 				  struct snd_kcontrol *kctl)
4645 {
4646 	switch (mixer->chip->usb_id) {
4647 	case USB_ID(0x21b4, 0x0081): /* AudioQuest DragonFly */
4648 		if (unitid == 7 && cval->control == UAC_FU_VOLUME)
4649 			snd_dragonfly_quirk_db_scale(mixer, cval, kctl);
4650 		break;
4651 	}
4652 
4653 	/* lowest playback value is muted on some devices */
4654 	if (mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE)
4655 		if (strstr(kctl->id.name, "Playback")) {
4656 			usb_audio_info(mixer->chip,
4657 				       "applying playback min mute quirk\n");
4658 			cval->min_mute = 1;
4659 		}
4660 
4661 	/* lowest capture value is muted on some devices */
4662 	if (mixer->chip->quirk_flags & QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE)
4663 		if (strstr(kctl->id.name, "Capture")) {
4664 			usb_audio_info(mixer->chip,
4665 				       "applying capture min mute quirk\n");
4666 			cval->min_mute = 1;
4667 		}
4668 	/* ALSA-ify some Plantronics headset control names */
4669 	if (USB_ID_VENDOR(mixer->chip->usb_id) == 0x047f &&
4670 	    (cval->control == UAC_FU_MUTE || cval->control == UAC_FU_VOLUME))
4671 		snd_fix_plt_name(mixer->chip, &kctl->id);
4672 }
4673