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