xref: /openbmc/linux/sound/usb/mixer_quirks.c (revision 83146efc)
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/hid.h>
18 #include <linux/init.h>
19 #include <linux/math64.h>
20 #include <linux/slab.h>
21 #include <linux/usb.h>
22 #include <linux/usb/audio.h>
23 
24 #include <sound/asoundef.h>
25 #include <sound/core.h>
26 #include <sound/control.h>
27 #include <sound/hda_verbs.h>
28 #include <sound/hwdep.h>
29 #include <sound/info.h>
30 #include <sound/tlv.h>
31 
32 #include "usbaudio.h"
33 #include "mixer.h"
34 #include "mixer_quirks.h"
35 #include "mixer_scarlett.h"
36 #include "mixer_scarlett_gen2.h"
37 #include "mixer_us16x08.h"
38 #include "mixer_s1810c.h"
39 #include "helper.h"
40 
41 struct std_mono_table {
42 	unsigned int unitid, control, cmask;
43 	int val_type;
44 	const char *name;
45 	snd_kcontrol_tlv_rw_t *tlv_callback;
46 };
47 
48 /* This function allows for the creation of standard UAC controls.
49  * See the quirks for M-Audio FTUs or Ebox-44.
50  * If you don't want to set a TLV callback pass NULL.
51  *
52  * Since there doesn't seem to be a devices that needs a multichannel
53  * version, we keep it mono for simplicity.
54  */
55 static int snd_create_std_mono_ctl_offset(struct usb_mixer_interface *mixer,
56 				unsigned int unitid,
57 				unsigned int control,
58 				unsigned int cmask,
59 				int val_type,
60 				unsigned int idx_off,
61 				const char *name,
62 				snd_kcontrol_tlv_rw_t *tlv_callback)
63 {
64 	struct usb_mixer_elem_info *cval;
65 	struct snd_kcontrol *kctl;
66 
67 	cval = kzalloc(sizeof(*cval), GFP_KERNEL);
68 	if (!cval)
69 		return -ENOMEM;
70 
71 	snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
72 	cval->val_type = val_type;
73 	cval->channels = 1;
74 	cval->control = control;
75 	cval->cmask = cmask;
76 	cval->idx_off = idx_off;
77 
78 	/* get_min_max() is called only for integer volumes later,
79 	 * so provide a short-cut for booleans */
80 	cval->min = 0;
81 	cval->max = 1;
82 	cval->res = 0;
83 	cval->dBmin = 0;
84 	cval->dBmax = 0;
85 
86 	/* Create control */
87 	kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
88 	if (!kctl) {
89 		kfree(cval);
90 		return -ENOMEM;
91 	}
92 
93 	/* Set name */
94 	snprintf(kctl->id.name, sizeof(kctl->id.name), name);
95 	kctl->private_free = snd_usb_mixer_elem_free;
96 
97 	/* set TLV */
98 	if (tlv_callback) {
99 		kctl->tlv.c = tlv_callback;
100 		kctl->vd[0].access |=
101 			SNDRV_CTL_ELEM_ACCESS_TLV_READ |
102 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
103 	}
104 	/* Add control to mixer */
105 	return snd_usb_mixer_add_control(&cval->head, kctl);
106 }
107 
108 static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
109 				unsigned int unitid,
110 				unsigned int control,
111 				unsigned int cmask,
112 				int val_type,
113 				const char *name,
114 				snd_kcontrol_tlv_rw_t *tlv_callback)
115 {
116 	return snd_create_std_mono_ctl_offset(mixer, unitid, control, cmask,
117 		val_type, 0 /* Offset */, name, tlv_callback);
118 }
119 
120 /*
121  * Create a set of standard UAC controls from a table
122  */
123 static int snd_create_std_mono_table(struct usb_mixer_interface *mixer,
124 				     const struct std_mono_table *t)
125 {
126 	int err;
127 
128 	while (t->name != NULL) {
129 		err = snd_create_std_mono_ctl(mixer, t->unitid, t->control,
130 				t->cmask, t->val_type, t->name, t->tlv_callback);
131 		if (err < 0)
132 			return err;
133 		t++;
134 	}
135 
136 	return 0;
137 }
138 
139 static int add_single_ctl_with_resume(struct usb_mixer_interface *mixer,
140 				      int id,
141 				      usb_mixer_elem_resume_func_t resume,
142 				      const struct snd_kcontrol_new *knew,
143 				      struct usb_mixer_elem_list **listp)
144 {
145 	struct usb_mixer_elem_list *list;
146 	struct snd_kcontrol *kctl;
147 
148 	list = kzalloc(sizeof(*list), GFP_KERNEL);
149 	if (!list)
150 		return -ENOMEM;
151 	if (listp)
152 		*listp = list;
153 	list->mixer = mixer;
154 	list->id = id;
155 	list->resume = resume;
156 	kctl = snd_ctl_new1(knew, list);
157 	if (!kctl) {
158 		kfree(list);
159 		return -ENOMEM;
160 	}
161 	kctl->private_free = snd_usb_mixer_elem_free;
162 	/* don't use snd_usb_mixer_add_control() here, this is a special list element */
163 	return snd_usb_mixer_add_list(list, kctl, false);
164 }
165 
166 /*
167  * Sound Blaster remote control configuration
168  *
169  * format of remote control data:
170  * Extigy:       xx 00
171  * Audigy 2 NX:  06 80 xx 00 00 00
172  * Live! 24-bit: 06 80 xx yy 22 83
173  */
174 static const struct rc_config {
175 	u32 usb_id;
176 	u8  offset;
177 	u8  length;
178 	u8  packet_length;
179 	u8  min_packet_length; /* minimum accepted length of the URB result */
180 	u8  mute_mixer_id;
181 	u32 mute_code;
182 } rc_configs[] = {
183 	{ USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
184 	{ USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
185 	{ USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
186 	{ USB_ID(0x041e, 0x3042), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 */
187 	{ USB_ID(0x041e, 0x30df), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
188 	{ USB_ID(0x041e, 0x3237), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
189 	{ USB_ID(0x041e, 0x3263), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
190 	{ USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
191 };
192 
193 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
194 {
195 	struct usb_mixer_interface *mixer = urb->context;
196 	const struct rc_config *rc = mixer->rc_cfg;
197 	u32 code;
198 
199 	if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
200 		return;
201 
202 	code = mixer->rc_buffer[rc->offset];
203 	if (rc->length == 2)
204 		code |= mixer->rc_buffer[rc->offset + 1] << 8;
205 
206 	/* the Mute button actually changes the mixer control */
207 	if (code == rc->mute_code)
208 		snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
209 	mixer->rc_code = code;
210 	wmb();
211 	wake_up(&mixer->rc_waitq);
212 }
213 
214 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
215 				     long count, loff_t *offset)
216 {
217 	struct usb_mixer_interface *mixer = hw->private_data;
218 	int err;
219 	u32 rc_code;
220 
221 	if (count != 1 && count != 4)
222 		return -EINVAL;
223 	err = wait_event_interruptible(mixer->rc_waitq,
224 				       (rc_code = xchg(&mixer->rc_code, 0)) != 0);
225 	if (err == 0) {
226 		if (count == 1)
227 			err = put_user(rc_code, buf);
228 		else
229 			err = put_user(rc_code, (u32 __user *)buf);
230 	}
231 	return err < 0 ? err : count;
232 }
233 
234 static __poll_t snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
235 					    poll_table *wait)
236 {
237 	struct usb_mixer_interface *mixer = hw->private_data;
238 
239 	poll_wait(file, &mixer->rc_waitq, wait);
240 	return mixer->rc_code ? EPOLLIN | EPOLLRDNORM : 0;
241 }
242 
243 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
244 {
245 	struct snd_hwdep *hwdep;
246 	int err, len, i;
247 
248 	for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
249 		if (rc_configs[i].usb_id == mixer->chip->usb_id)
250 			break;
251 	if (i >= ARRAY_SIZE(rc_configs))
252 		return 0;
253 	mixer->rc_cfg = &rc_configs[i];
254 
255 	len = mixer->rc_cfg->packet_length;
256 
257 	init_waitqueue_head(&mixer->rc_waitq);
258 	err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
259 	if (err < 0)
260 		return err;
261 	snprintf(hwdep->name, sizeof(hwdep->name),
262 		 "%s remote control", mixer->chip->card->shortname);
263 	hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
264 	hwdep->private_data = mixer;
265 	hwdep->ops.read = snd_usb_sbrc_hwdep_read;
266 	hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
267 	hwdep->exclusive = 1;
268 
269 	mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
270 	if (!mixer->rc_urb)
271 		return -ENOMEM;
272 	mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
273 	if (!mixer->rc_setup_packet) {
274 		usb_free_urb(mixer->rc_urb);
275 		mixer->rc_urb = NULL;
276 		return -ENOMEM;
277 	}
278 	mixer->rc_setup_packet->bRequestType =
279 		USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
280 	mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
281 	mixer->rc_setup_packet->wValue = cpu_to_le16(0);
282 	mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
283 	mixer->rc_setup_packet->wLength = cpu_to_le16(len);
284 	usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
285 			     usb_rcvctrlpipe(mixer->chip->dev, 0),
286 			     (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
287 			     snd_usb_soundblaster_remote_complete, mixer);
288 	return 0;
289 }
290 
291 #define snd_audigy2nx_led_info		snd_ctl_boolean_mono_info
292 
293 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
294 {
295 	ucontrol->value.integer.value[0] = kcontrol->private_value >> 8;
296 	return 0;
297 }
298 
299 static int snd_audigy2nx_led_update(struct usb_mixer_interface *mixer,
300 				    int value, int index)
301 {
302 	struct snd_usb_audio *chip = mixer->chip;
303 	int err;
304 
305 	err = snd_usb_lock_shutdown(chip);
306 	if (err < 0)
307 		return err;
308 
309 	if (chip->usb_id == USB_ID(0x041e, 0x3042))
310 		err = snd_usb_ctl_msg(chip->dev,
311 			      usb_sndctrlpipe(chip->dev, 0), 0x24,
312 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
313 			      !value, 0, NULL, 0);
314 	/* USB X-Fi S51 Pro */
315 	if (chip->usb_id == USB_ID(0x041e, 0x30df))
316 		err = snd_usb_ctl_msg(chip->dev,
317 			      usb_sndctrlpipe(chip->dev, 0), 0x24,
318 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
319 			      !value, 0, NULL, 0);
320 	else
321 		err = snd_usb_ctl_msg(chip->dev,
322 			      usb_sndctrlpipe(chip->dev, 0), 0x24,
323 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
324 			      value, index + 2, NULL, 0);
325 	snd_usb_unlock_shutdown(chip);
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 	int err;
338 
339 	if (value > 1)
340 		return -EINVAL;
341 	if (value == old_value)
342 		return 0;
343 	kcontrol->private_value = (value << 8) | index;
344 	err = snd_audigy2nx_led_update(mixer, value, index);
345 	return err < 0 ? err : 1;
346 }
347 
348 static int snd_audigy2nx_led_resume(struct usb_mixer_elem_list *list)
349 {
350 	int priv_value = list->kctl->private_value;
351 
352 	return snd_audigy2nx_led_update(list->mixer, priv_value >> 8,
353 					priv_value & 0xff);
354 }
355 
356 /* name and private_value are set dynamically */
357 static const struct snd_kcontrol_new snd_audigy2nx_control = {
358 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
359 	.info = snd_audigy2nx_led_info,
360 	.get = snd_audigy2nx_led_get,
361 	.put = snd_audigy2nx_led_put,
362 };
363 
364 static const char * const snd_audigy2nx_led_names[] = {
365 	"CMSS LED Switch",
366 	"Power LED Switch",
367 	"Dolby Digital LED Switch",
368 };
369 
370 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
371 {
372 	int i, err;
373 
374 	for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_led_names); ++i) {
375 		struct snd_kcontrol_new knew;
376 
377 		/* USB X-Fi S51 doesn't have a CMSS LED */
378 		if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
379 			continue;
380 		/* USB X-Fi S51 Pro doesn't have one either */
381 		if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
382 			continue;
383 		if (i > 1 && /* Live24ext has 2 LEDs only */
384 			(mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
385 			 mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
386 			 mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
387 			 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
388 			break;
389 
390 		knew = snd_audigy2nx_control;
391 		knew.name = snd_audigy2nx_led_names[i];
392 		knew.private_value = (1 << 8) | i; /* LED on as default */
393 		err = add_single_ctl_with_resume(mixer, 0,
394 						 snd_audigy2nx_led_resume,
395 						 &knew, NULL);
396 		if (err < 0)
397 			return err;
398 	}
399 	return 0;
400 }
401 
402 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
403 				    struct snd_info_buffer *buffer)
404 {
405 	static const struct sb_jack {
406 		int unitid;
407 		const char *name;
408 	}  jacks_audigy2nx[] = {
409 		{4,  "dig in "},
410 		{7,  "line in"},
411 		{19, "spk out"},
412 		{20, "hph out"},
413 		{-1, NULL}
414 	}, jacks_live24ext[] = {
415 		{4,  "line in"}, /* &1=Line, &2=Mic*/
416 		{3,  "hph out"}, /* headphones */
417 		{0,  "RC     "}, /* last command, 6 bytes see rc_config above */
418 		{-1, NULL}
419 	};
420 	const struct sb_jack *jacks;
421 	struct usb_mixer_interface *mixer = entry->private_data;
422 	int i, err;
423 	u8 buf[3];
424 
425 	snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
426 	if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
427 		jacks = jacks_audigy2nx;
428 	else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
429 		 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
430 		jacks = jacks_live24ext;
431 	else
432 		return;
433 
434 	for (i = 0; jacks[i].name; ++i) {
435 		snd_iprintf(buffer, "%s: ", jacks[i].name);
436 		err = snd_usb_lock_shutdown(mixer->chip);
437 		if (err < 0)
438 			return;
439 		err = snd_usb_ctl_msg(mixer->chip->dev,
440 				      usb_rcvctrlpipe(mixer->chip->dev, 0),
441 				      UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
442 				      USB_RECIP_INTERFACE, 0,
443 				      jacks[i].unitid << 8, buf, 3);
444 		snd_usb_unlock_shutdown(mixer->chip);
445 		if (err == 3 && (buf[0] == 3 || buf[0] == 6))
446 			snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
447 		else
448 			snd_iprintf(buffer, "?\n");
449 	}
450 }
451 
452 /* EMU0204 */
453 static int snd_emu0204_ch_switch_info(struct snd_kcontrol *kcontrol,
454 				      struct snd_ctl_elem_info *uinfo)
455 {
456 	static const char * const texts[2] = {"1/2", "3/4"};
457 
458 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
459 }
460 
461 static int snd_emu0204_ch_switch_get(struct snd_kcontrol *kcontrol,
462 				     struct snd_ctl_elem_value *ucontrol)
463 {
464 	ucontrol->value.enumerated.item[0] = kcontrol->private_value;
465 	return 0;
466 }
467 
468 static int snd_emu0204_ch_switch_update(struct usb_mixer_interface *mixer,
469 					int value)
470 {
471 	struct snd_usb_audio *chip = mixer->chip;
472 	int err;
473 	unsigned char buf[2];
474 
475 	err = snd_usb_lock_shutdown(chip);
476 	if (err < 0)
477 		return err;
478 
479 	buf[0] = 0x01;
480 	buf[1] = value ? 0x02 : 0x01;
481 	err = snd_usb_ctl_msg(chip->dev,
482 		      usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
483 		      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
484 		      0x0400, 0x0e00, buf, 2);
485 	snd_usb_unlock_shutdown(chip);
486 	return err;
487 }
488 
489 static int snd_emu0204_ch_switch_put(struct snd_kcontrol *kcontrol,
490 				     struct snd_ctl_elem_value *ucontrol)
491 {
492 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
493 	struct usb_mixer_interface *mixer = list->mixer;
494 	unsigned int value = ucontrol->value.enumerated.item[0];
495 	int err;
496 
497 	if (value > 1)
498 		return -EINVAL;
499 
500 	if (value == kcontrol->private_value)
501 		return 0;
502 
503 	kcontrol->private_value = value;
504 	err = snd_emu0204_ch_switch_update(mixer, value);
505 	return err < 0 ? err : 1;
506 }
507 
508 static int snd_emu0204_ch_switch_resume(struct usb_mixer_elem_list *list)
509 {
510 	return snd_emu0204_ch_switch_update(list->mixer,
511 					    list->kctl->private_value);
512 }
513 
514 static const struct snd_kcontrol_new snd_emu0204_control = {
515 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
516 	.name = "Front Jack Channels",
517 	.info = snd_emu0204_ch_switch_info,
518 	.get = snd_emu0204_ch_switch_get,
519 	.put = snd_emu0204_ch_switch_put,
520 	.private_value = 0,
521 };
522 
523 static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
524 {
525 	return add_single_ctl_with_resume(mixer, 0,
526 					  snd_emu0204_ch_switch_resume,
527 					  &snd_emu0204_control, NULL);
528 }
529 
530 /* ASUS Xonar U1 / U3 controls */
531 
532 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
533 				   struct snd_ctl_elem_value *ucontrol)
534 {
535 	ucontrol->value.integer.value[0] = !!(kcontrol->private_value & 0x02);
536 	return 0;
537 }
538 
539 static int snd_xonar_u1_switch_update(struct usb_mixer_interface *mixer,
540 				      unsigned char status)
541 {
542 	struct snd_usb_audio *chip = mixer->chip;
543 	int err;
544 
545 	err = snd_usb_lock_shutdown(chip);
546 	if (err < 0)
547 		return err;
548 	err = snd_usb_ctl_msg(chip->dev,
549 			      usb_sndctrlpipe(chip->dev, 0), 0x08,
550 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
551 			      50, 0, &status, 1);
552 	snd_usb_unlock_shutdown(chip);
553 	return err;
554 }
555 
556 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
557 				   struct snd_ctl_elem_value *ucontrol)
558 {
559 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
560 	u8 old_status, new_status;
561 	int err;
562 
563 	old_status = kcontrol->private_value;
564 	if (ucontrol->value.integer.value[0])
565 		new_status = old_status | 0x02;
566 	else
567 		new_status = old_status & ~0x02;
568 	if (new_status == old_status)
569 		return 0;
570 
571 	kcontrol->private_value = new_status;
572 	err = snd_xonar_u1_switch_update(list->mixer, new_status);
573 	return err < 0 ? err : 1;
574 }
575 
576 static int snd_xonar_u1_switch_resume(struct usb_mixer_elem_list *list)
577 {
578 	return snd_xonar_u1_switch_update(list->mixer,
579 					  list->kctl->private_value);
580 }
581 
582 static const struct snd_kcontrol_new snd_xonar_u1_output_switch = {
583 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
584 	.name = "Digital Playback Switch",
585 	.info = snd_ctl_boolean_mono_info,
586 	.get = snd_xonar_u1_switch_get,
587 	.put = snd_xonar_u1_switch_put,
588 	.private_value = 0x05,
589 };
590 
591 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
592 {
593 	return add_single_ctl_with_resume(mixer, 0,
594 					  snd_xonar_u1_switch_resume,
595 					  &snd_xonar_u1_output_switch, NULL);
596 }
597 
598 /* Digidesign Mbox 1 helper functions */
599 
600 static int snd_mbox1_is_spdif_synced(struct snd_usb_audio *chip)
601 {
602 	unsigned char buff[3];
603 	int err;
604 	int is_spdif_synced;
605 
606 	/* Read clock source */
607 	err = snd_usb_ctl_msg(chip->dev,
608 			      usb_rcvctrlpipe(chip->dev, 0), 0x81,
609 			      USB_DIR_IN |
610 			      USB_TYPE_CLASS |
611 			      USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
612 	if (err < 0)
613 		return err;
614 
615 	/* spdif sync: buff is all zeroes */
616 	is_spdif_synced = !(buff[0] | buff[1] | buff[2]);
617 	return is_spdif_synced;
618 }
619 
620 static int snd_mbox1_set_clk_source(struct snd_usb_audio *chip, int rate_or_zero)
621 {
622 	/* 2 possibilities:	Internal    -> expects sample rate
623 	 *			S/PDIF sync -> expects rate = 0
624 	 */
625 	unsigned char buff[3];
626 
627 	buff[0] = (rate_or_zero >>  0) & 0xff;
628 	buff[1] = (rate_or_zero >>  8) & 0xff;
629 	buff[2] = (rate_or_zero >> 16) & 0xff;
630 
631 	/* Set clock source */
632 	return snd_usb_ctl_msg(chip->dev,
633 			       usb_sndctrlpipe(chip->dev, 0), 0x1,
634 			       USB_TYPE_CLASS |
635 			       USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
636 }
637 
638 static int snd_mbox1_is_spdif_input(struct snd_usb_audio *chip)
639 {
640 	/* Hardware gives 2 possibilities:	ANALOG Source  -> 0x01
641 	 *					S/PDIF Source  -> 0x02
642 	 */
643 	int err;
644 	unsigned char source[1];
645 
646 	/* Read input source */
647 	err = snd_usb_ctl_msg(chip->dev,
648 			      usb_rcvctrlpipe(chip->dev, 0), 0x81,
649 			      USB_DIR_IN |
650 			      USB_TYPE_CLASS |
651 			      USB_RECIP_INTERFACE, 0x00, 0x500, source, 1);
652 	if (err < 0)
653 		return err;
654 
655 	return (source[0] == 2);
656 }
657 
658 static int snd_mbox1_set_input_source(struct snd_usb_audio *chip, int is_spdif)
659 {
660 	/* NB: Setting the input source to S/PDIF resets the clock source to S/PDIF
661 	 * Hardware expects 2 possibilities:	ANALOG Source  -> 0x01
662 	 *					S/PDIF Source  -> 0x02
663 	 */
664 	unsigned char buff[1];
665 
666 	buff[0] = (is_spdif & 1) + 1;
667 
668 	/* Set input source */
669 	return snd_usb_ctl_msg(chip->dev,
670 			       usb_sndctrlpipe(chip->dev, 0), 0x1,
671 			       USB_TYPE_CLASS |
672 			       USB_RECIP_INTERFACE, 0x00, 0x500, buff, 1);
673 }
674 
675 /* Digidesign Mbox 1 clock source switch (internal/spdif) */
676 
677 static int snd_mbox1_clk_switch_get(struct snd_kcontrol *kctl,
678 				    struct snd_ctl_elem_value *ucontrol)
679 {
680 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
681 	struct snd_usb_audio *chip = list->mixer->chip;
682 	int err;
683 
684 	err = snd_usb_lock_shutdown(chip);
685 	if (err < 0)
686 		goto err;
687 
688 	err = snd_mbox1_is_spdif_synced(chip);
689 	if (err < 0)
690 		goto err;
691 
692 	kctl->private_value = err;
693 	err = 0;
694 	ucontrol->value.enumerated.item[0] = kctl->private_value;
695 err:
696 	snd_usb_unlock_shutdown(chip);
697 	return err;
698 }
699 
700 static int snd_mbox1_clk_switch_update(struct usb_mixer_interface *mixer, int is_spdif_sync)
701 {
702 	struct snd_usb_audio *chip = mixer->chip;
703 	int err;
704 
705 	err = snd_usb_lock_shutdown(chip);
706 	if (err < 0)
707 		return err;
708 
709 	err = snd_mbox1_is_spdif_input(chip);
710 	if (err < 0)
711 		goto err;
712 
713 	err = snd_mbox1_is_spdif_synced(chip);
714 	if (err < 0)
715 		goto err;
716 
717 	/* FIXME: hardcoded sample rate */
718 	err = snd_mbox1_set_clk_source(chip, is_spdif_sync ? 0 : 48000);
719 	if (err < 0)
720 		goto err;
721 
722 	err = snd_mbox1_is_spdif_synced(chip);
723 err:
724 	snd_usb_unlock_shutdown(chip);
725 	return err;
726 }
727 
728 static int snd_mbox1_clk_switch_put(struct snd_kcontrol *kctl,
729 				    struct snd_ctl_elem_value *ucontrol)
730 {
731 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
732 	struct usb_mixer_interface *mixer = list->mixer;
733 	int err;
734 	bool cur_val, new_val;
735 
736 	cur_val = kctl->private_value;
737 	new_val = ucontrol->value.enumerated.item[0];
738 	if (cur_val == new_val)
739 		return 0;
740 
741 	kctl->private_value = new_val;
742 	err = snd_mbox1_clk_switch_update(mixer, new_val);
743 	return err < 0 ? err : 1;
744 }
745 
746 static int snd_mbox1_clk_switch_info(struct snd_kcontrol *kcontrol,
747 				     struct snd_ctl_elem_info *uinfo)
748 {
749 	static const char *const texts[2] = {
750 		"Internal",
751 		"S/PDIF"
752 	};
753 
754 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
755 }
756 
757 static int snd_mbox1_clk_switch_resume(struct usb_mixer_elem_list *list)
758 {
759 	return snd_mbox1_clk_switch_update(list->mixer, list->kctl->private_value);
760 }
761 
762 /* Digidesign Mbox 1 input source switch (analog/spdif) */
763 
764 static int snd_mbox1_src_switch_get(struct snd_kcontrol *kctl,
765 				    struct snd_ctl_elem_value *ucontrol)
766 {
767 	ucontrol->value.enumerated.item[0] = kctl->private_value;
768 	return 0;
769 }
770 
771 static int snd_mbox1_src_switch_update(struct usb_mixer_interface *mixer, int is_spdif_input)
772 {
773 	struct snd_usb_audio *chip = mixer->chip;
774 	int err;
775 
776 	err = snd_usb_lock_shutdown(chip);
777 	if (err < 0)
778 		return err;
779 
780 	err = snd_mbox1_is_spdif_input(chip);
781 	if (err < 0)
782 		goto err;
783 
784 	err = snd_mbox1_set_input_source(chip, is_spdif_input);
785 	if (err < 0)
786 		goto err;
787 
788 	err = snd_mbox1_is_spdif_input(chip);
789 	if (err < 0)
790 		goto err;
791 
792 	err = snd_mbox1_is_spdif_synced(chip);
793 err:
794 	snd_usb_unlock_shutdown(chip);
795 	return err;
796 }
797 
798 static int snd_mbox1_src_switch_put(struct snd_kcontrol *kctl,
799 				    struct snd_ctl_elem_value *ucontrol)
800 {
801 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
802 	struct usb_mixer_interface *mixer = list->mixer;
803 	int err;
804 	bool cur_val, new_val;
805 
806 	cur_val = kctl->private_value;
807 	new_val = ucontrol->value.enumerated.item[0];
808 	if (cur_val == new_val)
809 		return 0;
810 
811 	kctl->private_value = new_val;
812 	err = snd_mbox1_src_switch_update(mixer, new_val);
813 	return err < 0 ? err : 1;
814 }
815 
816 static int snd_mbox1_src_switch_info(struct snd_kcontrol *kcontrol,
817 				     struct snd_ctl_elem_info *uinfo)
818 {
819 	static const char *const texts[2] = {
820 		"Analog",
821 		"S/PDIF"
822 	};
823 
824 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
825 }
826 
827 static int snd_mbox1_src_switch_resume(struct usb_mixer_elem_list *list)
828 {
829 	return snd_mbox1_src_switch_update(list->mixer, list->kctl->private_value);
830 }
831 
832 static const struct snd_kcontrol_new snd_mbox1_clk_switch = {
833 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
834 	.name = "Clock Source",
835 	.index = 0,
836 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
837 	.info = snd_mbox1_clk_switch_info,
838 	.get = snd_mbox1_clk_switch_get,
839 	.put = snd_mbox1_clk_switch_put,
840 	.private_value = 0
841 };
842 
843 static const struct snd_kcontrol_new snd_mbox1_src_switch = {
844 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
845 	.name = "Input Source",
846 	.index = 1,
847 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
848 	.info = snd_mbox1_src_switch_info,
849 	.get = snd_mbox1_src_switch_get,
850 	.put = snd_mbox1_src_switch_put,
851 	.private_value = 0
852 };
853 
854 static int snd_mbox1_controls_create(struct usb_mixer_interface *mixer)
855 {
856 	int err;
857 	err = add_single_ctl_with_resume(mixer, 0,
858 					 snd_mbox1_clk_switch_resume,
859 					 &snd_mbox1_clk_switch, NULL);
860 	if (err < 0)
861 		return err;
862 
863 	return add_single_ctl_with_resume(mixer, 1,
864 					  snd_mbox1_src_switch_resume,
865 					  &snd_mbox1_src_switch, NULL);
866 }
867 
868 /* Native Instruments device quirks */
869 
870 #define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
871 
872 static int snd_ni_control_init_val(struct usb_mixer_interface *mixer,
873 				   struct snd_kcontrol *kctl)
874 {
875 	struct usb_device *dev = mixer->chip->dev;
876 	unsigned int pval = kctl->private_value;
877 	u8 value;
878 	int err;
879 
880 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
881 			      (pval >> 16) & 0xff,
882 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
883 			      0, pval & 0xffff, &value, 1);
884 	if (err < 0) {
885 		dev_err(&dev->dev,
886 			"unable to issue vendor read request (ret = %d)", err);
887 		return err;
888 	}
889 
890 	kctl->private_value |= ((unsigned int)value << 24);
891 	return 0;
892 }
893 
894 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
895 					     struct snd_ctl_elem_value *ucontrol)
896 {
897 	ucontrol->value.integer.value[0] = kcontrol->private_value >> 24;
898 	return 0;
899 }
900 
901 static int snd_ni_update_cur_val(struct usb_mixer_elem_list *list)
902 {
903 	struct snd_usb_audio *chip = list->mixer->chip;
904 	unsigned int pval = list->kctl->private_value;
905 	int err;
906 
907 	err = snd_usb_lock_shutdown(chip);
908 	if (err < 0)
909 		return err;
910 	err = usb_control_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
911 			      (pval >> 16) & 0xff,
912 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
913 			      pval >> 24, pval & 0xffff, NULL, 0, 1000);
914 	snd_usb_unlock_shutdown(chip);
915 	return err;
916 }
917 
918 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
919 					     struct snd_ctl_elem_value *ucontrol)
920 {
921 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
922 	u8 oldval = (kcontrol->private_value >> 24) & 0xff;
923 	u8 newval = ucontrol->value.integer.value[0];
924 	int err;
925 
926 	if (oldval == newval)
927 		return 0;
928 
929 	kcontrol->private_value &= ~(0xff << 24);
930 	kcontrol->private_value |= (unsigned int)newval << 24;
931 	err = snd_ni_update_cur_val(list);
932 	return err < 0 ? err : 1;
933 }
934 
935 static const struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
936 	{
937 		.name = "Direct Thru Channel A",
938 		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
939 	},
940 	{
941 		.name = "Direct Thru Channel B",
942 		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
943 	},
944 	{
945 		.name = "Phono Input Channel A",
946 		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
947 	},
948 	{
949 		.name = "Phono Input Channel B",
950 		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
951 	},
952 };
953 
954 static const struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
955 	{
956 		.name = "Direct Thru Channel A",
957 		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
958 	},
959 	{
960 		.name = "Direct Thru Channel B",
961 		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
962 	},
963 	{
964 		.name = "Direct Thru Channel C",
965 		.private_value = _MAKE_NI_CONTROL(0x01, 0x07),
966 	},
967 	{
968 		.name = "Direct Thru Channel D",
969 		.private_value = _MAKE_NI_CONTROL(0x01, 0x09),
970 	},
971 	{
972 		.name = "Phono Input Channel A",
973 		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
974 	},
975 	{
976 		.name = "Phono Input Channel B",
977 		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
978 	},
979 	{
980 		.name = "Phono Input Channel C",
981 		.private_value = _MAKE_NI_CONTROL(0x02, 0x07),
982 	},
983 	{
984 		.name = "Phono Input Channel D",
985 		.private_value = _MAKE_NI_CONTROL(0x02, 0x09),
986 	},
987 };
988 
989 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
990 					      const struct snd_kcontrol_new *kc,
991 					      unsigned int count)
992 {
993 	int i, err = 0;
994 	struct snd_kcontrol_new template = {
995 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
996 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
997 		.get = snd_nativeinstruments_control_get,
998 		.put = snd_nativeinstruments_control_put,
999 		.info = snd_ctl_boolean_mono_info,
1000 	};
1001 
1002 	for (i = 0; i < count; i++) {
1003 		struct usb_mixer_elem_list *list;
1004 
1005 		template.name = kc[i].name;
1006 		template.private_value = kc[i].private_value;
1007 
1008 		err = add_single_ctl_with_resume(mixer, 0,
1009 						 snd_ni_update_cur_val,
1010 						 &template, &list);
1011 		if (err < 0)
1012 			break;
1013 		snd_ni_control_init_val(mixer, list->kctl);
1014 	}
1015 
1016 	return err;
1017 }
1018 
1019 /* M-Audio FastTrack Ultra quirks */
1020 /* FTU Effect switch (also used by C400/C600) */
1021 static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
1022 					struct snd_ctl_elem_info *uinfo)
1023 {
1024 	static const char *const texts[8] = {
1025 		"Room 1", "Room 2", "Room 3", "Hall 1",
1026 		"Hall 2", "Plate", "Delay", "Echo"
1027 	};
1028 
1029 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1030 }
1031 
1032 static int snd_ftu_eff_switch_init(struct usb_mixer_interface *mixer,
1033 				   struct snd_kcontrol *kctl)
1034 {
1035 	struct usb_device *dev = mixer->chip->dev;
1036 	unsigned int pval = kctl->private_value;
1037 	int err;
1038 	unsigned char value[2];
1039 
1040 	value[0] = 0x00;
1041 	value[1] = 0x00;
1042 
1043 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
1044 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1045 			      pval & 0xff00,
1046 			      snd_usb_ctrl_intf(mixer->chip) | ((pval & 0xff) << 8),
1047 			      value, 2);
1048 	if (err < 0)
1049 		return err;
1050 
1051 	kctl->private_value |= (unsigned int)value[0] << 24;
1052 	return 0;
1053 }
1054 
1055 static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
1056 					struct snd_ctl_elem_value *ucontrol)
1057 {
1058 	ucontrol->value.enumerated.item[0] = kctl->private_value >> 24;
1059 	return 0;
1060 }
1061 
1062 static int snd_ftu_eff_switch_update(struct usb_mixer_elem_list *list)
1063 {
1064 	struct snd_usb_audio *chip = list->mixer->chip;
1065 	unsigned int pval = list->kctl->private_value;
1066 	unsigned char value[2];
1067 	int err;
1068 
1069 	value[0] = pval >> 24;
1070 	value[1] = 0;
1071 
1072 	err = snd_usb_lock_shutdown(chip);
1073 	if (err < 0)
1074 		return err;
1075 	err = snd_usb_ctl_msg(chip->dev,
1076 			      usb_sndctrlpipe(chip->dev, 0),
1077 			      UAC_SET_CUR,
1078 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1079 			      pval & 0xff00,
1080 			      snd_usb_ctrl_intf(chip) | ((pval & 0xff) << 8),
1081 			      value, 2);
1082 	snd_usb_unlock_shutdown(chip);
1083 	return err;
1084 }
1085 
1086 static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
1087 					struct snd_ctl_elem_value *ucontrol)
1088 {
1089 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
1090 	unsigned int pval = list->kctl->private_value;
1091 	int cur_val, err, new_val;
1092 
1093 	cur_val = pval >> 24;
1094 	new_val = ucontrol->value.enumerated.item[0];
1095 	if (cur_val == new_val)
1096 		return 0;
1097 
1098 	kctl->private_value &= ~(0xff << 24);
1099 	kctl->private_value |= new_val << 24;
1100 	err = snd_ftu_eff_switch_update(list);
1101 	return err < 0 ? err : 1;
1102 }
1103 
1104 static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer,
1105 	int validx, int bUnitID)
1106 {
1107 	static struct snd_kcontrol_new template = {
1108 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1109 		.name = "Effect Program Switch",
1110 		.index = 0,
1111 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1112 		.info = snd_ftu_eff_switch_info,
1113 		.get = snd_ftu_eff_switch_get,
1114 		.put = snd_ftu_eff_switch_put
1115 	};
1116 	struct usb_mixer_elem_list *list;
1117 	int err;
1118 
1119 	err = add_single_ctl_with_resume(mixer, bUnitID,
1120 					 snd_ftu_eff_switch_update,
1121 					 &template, &list);
1122 	if (err < 0)
1123 		return err;
1124 	list->kctl->private_value = (validx << 8) | bUnitID;
1125 	snd_ftu_eff_switch_init(mixer, list->kctl);
1126 	return 0;
1127 }
1128 
1129 /* Create volume controls for FTU devices*/
1130 static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
1131 {
1132 	char name[64];
1133 	unsigned int control, cmask;
1134 	int in, out, err;
1135 
1136 	const unsigned int id = 5;
1137 	const int val_type = USB_MIXER_S16;
1138 
1139 	for (out = 0; out < 8; out++) {
1140 		control = out + 1;
1141 		for (in = 0; in < 8; in++) {
1142 			cmask = 1 << in;
1143 			snprintf(name, sizeof(name),
1144 				"AIn%d - Out%d Capture Volume",
1145 				in  + 1, out + 1);
1146 			err = snd_create_std_mono_ctl(mixer, id, control,
1147 							cmask, val_type, name,
1148 							&snd_usb_mixer_vol_tlv);
1149 			if (err < 0)
1150 				return err;
1151 		}
1152 		for (in = 8; in < 16; in++) {
1153 			cmask = 1 << in;
1154 			snprintf(name, sizeof(name),
1155 				"DIn%d - Out%d Playback Volume",
1156 				in - 7, out + 1);
1157 			err = snd_create_std_mono_ctl(mixer, id, control,
1158 							cmask, val_type, name,
1159 							&snd_usb_mixer_vol_tlv);
1160 			if (err < 0)
1161 				return err;
1162 		}
1163 	}
1164 
1165 	return 0;
1166 }
1167 
1168 /* This control needs a volume quirk, see mixer.c */
1169 static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1170 {
1171 	static const char name[] = "Effect Volume";
1172 	const unsigned int id = 6;
1173 	const int val_type = USB_MIXER_U8;
1174 	const unsigned int control = 2;
1175 	const unsigned int cmask = 0;
1176 
1177 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1178 					name, snd_usb_mixer_vol_tlv);
1179 }
1180 
1181 /* This control needs a volume quirk, see mixer.c */
1182 static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1183 {
1184 	static const char name[] = "Effect Duration";
1185 	const unsigned int id = 6;
1186 	const int val_type = USB_MIXER_S16;
1187 	const unsigned int control = 3;
1188 	const unsigned int cmask = 0;
1189 
1190 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1191 					name, snd_usb_mixer_vol_tlv);
1192 }
1193 
1194 /* This control needs a volume quirk, see mixer.c */
1195 static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1196 {
1197 	static const char name[] = "Effect Feedback Volume";
1198 	const unsigned int id = 6;
1199 	const int val_type = USB_MIXER_U8;
1200 	const unsigned int control = 4;
1201 	const unsigned int cmask = 0;
1202 
1203 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1204 					name, NULL);
1205 }
1206 
1207 static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
1208 {
1209 	unsigned int cmask;
1210 	int err, ch;
1211 	char name[48];
1212 
1213 	const unsigned int id = 7;
1214 	const int val_type = USB_MIXER_S16;
1215 	const unsigned int control = 7;
1216 
1217 	for (ch = 0; ch < 4; ++ch) {
1218 		cmask = 1 << ch;
1219 		snprintf(name, sizeof(name),
1220 			"Effect Return %d Volume", ch + 1);
1221 		err = snd_create_std_mono_ctl(mixer, id, control,
1222 						cmask, val_type, name,
1223 						snd_usb_mixer_vol_tlv);
1224 		if (err < 0)
1225 			return err;
1226 	}
1227 
1228 	return 0;
1229 }
1230 
1231 static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
1232 {
1233 	unsigned int  cmask;
1234 	int err, ch;
1235 	char name[48];
1236 
1237 	const unsigned int id = 5;
1238 	const int val_type = USB_MIXER_S16;
1239 	const unsigned int control = 9;
1240 
1241 	for (ch = 0; ch < 8; ++ch) {
1242 		cmask = 1 << ch;
1243 		snprintf(name, sizeof(name),
1244 			"Effect Send AIn%d Volume", ch + 1);
1245 		err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1246 						val_type, name,
1247 						snd_usb_mixer_vol_tlv);
1248 		if (err < 0)
1249 			return err;
1250 	}
1251 	for (ch = 8; ch < 16; ++ch) {
1252 		cmask = 1 << ch;
1253 		snprintf(name, sizeof(name),
1254 			"Effect Send DIn%d Volume", ch - 7);
1255 		err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1256 						val_type, name,
1257 						snd_usb_mixer_vol_tlv);
1258 		if (err < 0)
1259 			return err;
1260 	}
1261 	return 0;
1262 }
1263 
1264 static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
1265 {
1266 	int err;
1267 
1268 	err = snd_ftu_create_volume_ctls(mixer);
1269 	if (err < 0)
1270 		return err;
1271 
1272 	err = snd_ftu_create_effect_switch(mixer, 1, 6);
1273 	if (err < 0)
1274 		return err;
1275 
1276 	err = snd_ftu_create_effect_volume_ctl(mixer);
1277 	if (err < 0)
1278 		return err;
1279 
1280 	err = snd_ftu_create_effect_duration_ctl(mixer);
1281 	if (err < 0)
1282 		return err;
1283 
1284 	err = snd_ftu_create_effect_feedback_ctl(mixer);
1285 	if (err < 0)
1286 		return err;
1287 
1288 	err = snd_ftu_create_effect_return_ctls(mixer);
1289 	if (err < 0)
1290 		return err;
1291 
1292 	err = snd_ftu_create_effect_send_ctls(mixer);
1293 	if (err < 0)
1294 		return err;
1295 
1296 	return 0;
1297 }
1298 
1299 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
1300 			       unsigned char samplerate_id)
1301 {
1302 	struct usb_mixer_interface *mixer;
1303 	struct usb_mixer_elem_info *cval;
1304 	int unitid = 12; /* SampleRate ExtensionUnit ID */
1305 
1306 	list_for_each_entry(mixer, &chip->mixer_list, list) {
1307 		if (mixer->id_elems[unitid]) {
1308 			cval = mixer_elem_list_to_info(mixer->id_elems[unitid]);
1309 			snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1310 						    cval->control << 8,
1311 						    samplerate_id);
1312 			snd_usb_mixer_notify_id(mixer, unitid);
1313 			break;
1314 		}
1315 	}
1316 }
1317 
1318 /* M-Audio Fast Track C400/C600 */
1319 /* C400/C600 volume controls, this control needs a volume quirk, see mixer.c */
1320 static int snd_c400_create_vol_ctls(struct usb_mixer_interface *mixer)
1321 {
1322 	char name[64];
1323 	unsigned int cmask, offset;
1324 	int out, chan, err;
1325 	int num_outs = 0;
1326 	int num_ins = 0;
1327 
1328 	const unsigned int id = 0x40;
1329 	const int val_type = USB_MIXER_S16;
1330 	const int control = 1;
1331 
1332 	switch (mixer->chip->usb_id) {
1333 	case USB_ID(0x0763, 0x2030):
1334 		num_outs = 6;
1335 		num_ins = 4;
1336 		break;
1337 	case USB_ID(0x0763, 0x2031):
1338 		num_outs = 8;
1339 		num_ins = 6;
1340 		break;
1341 	}
1342 
1343 	for (chan = 0; chan < num_outs + num_ins; chan++) {
1344 		for (out = 0; out < num_outs; out++) {
1345 			if (chan < num_outs) {
1346 				snprintf(name, sizeof(name),
1347 					"PCM%d-Out%d Playback Volume",
1348 					chan + 1, out + 1);
1349 			} else {
1350 				snprintf(name, sizeof(name),
1351 					"In%d-Out%d Playback Volume",
1352 					chan - num_outs + 1, out + 1);
1353 			}
1354 
1355 			cmask = (out == 0) ? 0 : 1 << (out - 1);
1356 			offset = chan * num_outs;
1357 			err = snd_create_std_mono_ctl_offset(mixer, id, control,
1358 						cmask, val_type, offset, name,
1359 						&snd_usb_mixer_vol_tlv);
1360 			if (err < 0)
1361 				return err;
1362 		}
1363 	}
1364 
1365 	return 0;
1366 }
1367 
1368 /* This control needs a volume quirk, see mixer.c */
1369 static int snd_c400_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1370 {
1371 	static const char name[] = "Effect Volume";
1372 	const unsigned int id = 0x43;
1373 	const int val_type = USB_MIXER_U8;
1374 	const unsigned int control = 3;
1375 	const unsigned int cmask = 0;
1376 
1377 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1378 					name, snd_usb_mixer_vol_tlv);
1379 }
1380 
1381 /* This control needs a volume quirk, see mixer.c */
1382 static int snd_c400_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1383 {
1384 	static const char name[] = "Effect Duration";
1385 	const unsigned int id = 0x43;
1386 	const int val_type = USB_MIXER_S16;
1387 	const unsigned int control = 4;
1388 	const unsigned int cmask = 0;
1389 
1390 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1391 					name, snd_usb_mixer_vol_tlv);
1392 }
1393 
1394 /* This control needs a volume quirk, see mixer.c */
1395 static int snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1396 {
1397 	static const char name[] = "Effect Feedback Volume";
1398 	const unsigned int id = 0x43;
1399 	const int val_type = USB_MIXER_U8;
1400 	const unsigned int control = 5;
1401 	const unsigned int cmask = 0;
1402 
1403 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1404 					name, NULL);
1405 }
1406 
1407 static int snd_c400_create_effect_vol_ctls(struct usb_mixer_interface *mixer)
1408 {
1409 	char name[64];
1410 	unsigned int cmask;
1411 	int chan, err;
1412 	int num_outs = 0;
1413 	int num_ins = 0;
1414 
1415 	const unsigned int id = 0x42;
1416 	const int val_type = USB_MIXER_S16;
1417 	const int control = 1;
1418 
1419 	switch (mixer->chip->usb_id) {
1420 	case USB_ID(0x0763, 0x2030):
1421 		num_outs = 6;
1422 		num_ins = 4;
1423 		break;
1424 	case USB_ID(0x0763, 0x2031):
1425 		num_outs = 8;
1426 		num_ins = 6;
1427 		break;
1428 	}
1429 
1430 	for (chan = 0; chan < num_outs + num_ins; chan++) {
1431 		if (chan < num_outs) {
1432 			snprintf(name, sizeof(name),
1433 				"Effect Send DOut%d",
1434 				chan + 1);
1435 		} else {
1436 			snprintf(name, sizeof(name),
1437 				"Effect Send AIn%d",
1438 				chan - num_outs + 1);
1439 		}
1440 
1441 		cmask = (chan == 0) ? 0 : 1 << (chan - 1);
1442 		err = snd_create_std_mono_ctl(mixer, id, control,
1443 						cmask, val_type, name,
1444 						&snd_usb_mixer_vol_tlv);
1445 		if (err < 0)
1446 			return err;
1447 	}
1448 
1449 	return 0;
1450 }
1451 
1452 static int snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface *mixer)
1453 {
1454 	char name[64];
1455 	unsigned int cmask;
1456 	int chan, err;
1457 	int num_outs = 0;
1458 	int offset = 0;
1459 
1460 	const unsigned int id = 0x40;
1461 	const int val_type = USB_MIXER_S16;
1462 	const int control = 1;
1463 
1464 	switch (mixer->chip->usb_id) {
1465 	case USB_ID(0x0763, 0x2030):
1466 		num_outs = 6;
1467 		offset = 0x3c;
1468 		/* { 0x3c, 0x43, 0x3e, 0x45, 0x40, 0x47 } */
1469 		break;
1470 	case USB_ID(0x0763, 0x2031):
1471 		num_outs = 8;
1472 		offset = 0x70;
1473 		/* { 0x70, 0x79, 0x72, 0x7b, 0x74, 0x7d, 0x76, 0x7f } */
1474 		break;
1475 	}
1476 
1477 	for (chan = 0; chan < num_outs; chan++) {
1478 		snprintf(name, sizeof(name),
1479 			"Effect Return %d",
1480 			chan + 1);
1481 
1482 		cmask = (chan == 0) ? 0 :
1483 			1 << (chan + (chan % 2) * num_outs - 1);
1484 		err = snd_create_std_mono_ctl_offset(mixer, id, control,
1485 						cmask, val_type, offset, name,
1486 						&snd_usb_mixer_vol_tlv);
1487 		if (err < 0)
1488 			return err;
1489 	}
1490 
1491 	return 0;
1492 }
1493 
1494 static int snd_c400_create_mixer(struct usb_mixer_interface *mixer)
1495 {
1496 	int err;
1497 
1498 	err = snd_c400_create_vol_ctls(mixer);
1499 	if (err < 0)
1500 		return err;
1501 
1502 	err = snd_c400_create_effect_vol_ctls(mixer);
1503 	if (err < 0)
1504 		return err;
1505 
1506 	err = snd_c400_create_effect_ret_vol_ctls(mixer);
1507 	if (err < 0)
1508 		return err;
1509 
1510 	err = snd_ftu_create_effect_switch(mixer, 2, 0x43);
1511 	if (err < 0)
1512 		return err;
1513 
1514 	err = snd_c400_create_effect_volume_ctl(mixer);
1515 	if (err < 0)
1516 		return err;
1517 
1518 	err = snd_c400_create_effect_duration_ctl(mixer);
1519 	if (err < 0)
1520 		return err;
1521 
1522 	err = snd_c400_create_effect_feedback_ctl(mixer);
1523 	if (err < 0)
1524 		return err;
1525 
1526 	return 0;
1527 }
1528 
1529 /*
1530  * The mixer units for Ebox-44 are corrupt, and even where they
1531  * are valid they presents mono controls as L and R channels of
1532  * stereo. So we provide a good mixer here.
1533  */
1534 static const struct std_mono_table ebox44_table[] = {
1535 	{
1536 		.unitid = 4,
1537 		.control = 1,
1538 		.cmask = 0x0,
1539 		.val_type = USB_MIXER_INV_BOOLEAN,
1540 		.name = "Headphone Playback Switch"
1541 	},
1542 	{
1543 		.unitid = 4,
1544 		.control = 2,
1545 		.cmask = 0x1,
1546 		.val_type = USB_MIXER_S16,
1547 		.name = "Headphone A Mix Playback Volume"
1548 	},
1549 	{
1550 		.unitid = 4,
1551 		.control = 2,
1552 		.cmask = 0x2,
1553 		.val_type = USB_MIXER_S16,
1554 		.name = "Headphone B Mix Playback Volume"
1555 	},
1556 
1557 	{
1558 		.unitid = 7,
1559 		.control = 1,
1560 		.cmask = 0x0,
1561 		.val_type = USB_MIXER_INV_BOOLEAN,
1562 		.name = "Output Playback Switch"
1563 	},
1564 	{
1565 		.unitid = 7,
1566 		.control = 2,
1567 		.cmask = 0x1,
1568 		.val_type = USB_MIXER_S16,
1569 		.name = "Output A Playback Volume"
1570 	},
1571 	{
1572 		.unitid = 7,
1573 		.control = 2,
1574 		.cmask = 0x2,
1575 		.val_type = USB_MIXER_S16,
1576 		.name = "Output B Playback Volume"
1577 	},
1578 
1579 	{
1580 		.unitid = 10,
1581 		.control = 1,
1582 		.cmask = 0x0,
1583 		.val_type = USB_MIXER_INV_BOOLEAN,
1584 		.name = "Input Capture Switch"
1585 	},
1586 	{
1587 		.unitid = 10,
1588 		.control = 2,
1589 		.cmask = 0x1,
1590 		.val_type = USB_MIXER_S16,
1591 		.name = "Input A Capture Volume"
1592 	},
1593 	{
1594 		.unitid = 10,
1595 		.control = 2,
1596 		.cmask = 0x2,
1597 		.val_type = USB_MIXER_S16,
1598 		.name = "Input B Capture Volume"
1599 	},
1600 
1601 	{}
1602 };
1603 
1604 /* Audio Advantage Micro II findings:
1605  *
1606  * Mapping spdif AES bits to vendor register.bit:
1607  * AES0: [0 0 0 0 2.3 2.2 2.1 2.0] - default 0x00
1608  * AES1: [3.3 3.2.3.1.3.0 2.7 2.6 2.5 2.4] - default: 0x01
1609  * AES2: [0 0 0 0 0 0 0 0]
1610  * AES3: [0 0 0 0 0 0 x 0] - 'x' bit is set basing on standard usb request
1611  *                           (UAC_EP_CS_ATTR_SAMPLE_RATE) for Audio Devices
1612  *
1613  * power on values:
1614  * r2: 0x10
1615  * r3: 0x20 (b7 is zeroed just before playback (except IEC61937) and set
1616  *           just after it to 0xa0, presumably it disables/mutes some analog
1617  *           parts when there is no audio.)
1618  * r9: 0x28
1619  *
1620  * Optical transmitter on/off:
1621  * vendor register.bit: 9.1
1622  * 0 - on (0x28 register value)
1623  * 1 - off (0x2a register value)
1624  *
1625  */
1626 static int snd_microii_spdif_info(struct snd_kcontrol *kcontrol,
1627 	struct snd_ctl_elem_info *uinfo)
1628 {
1629 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1630 	uinfo->count = 1;
1631 	return 0;
1632 }
1633 
1634 static int snd_microii_spdif_default_get(struct snd_kcontrol *kcontrol,
1635 	struct snd_ctl_elem_value *ucontrol)
1636 {
1637 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1638 	struct snd_usb_audio *chip = list->mixer->chip;
1639 	int err;
1640 	struct usb_interface *iface;
1641 	struct usb_host_interface *alts;
1642 	unsigned int ep;
1643 	unsigned char data[3];
1644 	int rate;
1645 
1646 	err = snd_usb_lock_shutdown(chip);
1647 	if (err < 0)
1648 		return err;
1649 
1650 	ucontrol->value.iec958.status[0] = kcontrol->private_value & 0xff;
1651 	ucontrol->value.iec958.status[1] = (kcontrol->private_value >> 8) & 0xff;
1652 	ucontrol->value.iec958.status[2] = 0x00;
1653 
1654 	/* use known values for that card: interface#1 altsetting#1 */
1655 	iface = usb_ifnum_to_if(chip->dev, 1);
1656 	if (!iface || iface->num_altsetting < 2) {
1657 		err = -EINVAL;
1658 		goto end;
1659 	}
1660 	alts = &iface->altsetting[1];
1661 	if (get_iface_desc(alts)->bNumEndpoints < 1) {
1662 		err = -EINVAL;
1663 		goto end;
1664 	}
1665 	ep = get_endpoint(alts, 0)->bEndpointAddress;
1666 
1667 	err = snd_usb_ctl_msg(chip->dev,
1668 			usb_rcvctrlpipe(chip->dev, 0),
1669 			UAC_GET_CUR,
1670 			USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
1671 			UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
1672 			ep,
1673 			data,
1674 			sizeof(data));
1675 	if (err < 0)
1676 		goto end;
1677 
1678 	rate = data[0] | (data[1] << 8) | (data[2] << 16);
1679 	ucontrol->value.iec958.status[3] = (rate == 48000) ?
1680 			IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100;
1681 
1682 	err = 0;
1683  end:
1684 	snd_usb_unlock_shutdown(chip);
1685 	return err;
1686 }
1687 
1688 static int snd_microii_spdif_default_update(struct usb_mixer_elem_list *list)
1689 {
1690 	struct snd_usb_audio *chip = list->mixer->chip;
1691 	unsigned int pval = list->kctl->private_value;
1692 	u8 reg;
1693 	int err;
1694 
1695 	err = snd_usb_lock_shutdown(chip);
1696 	if (err < 0)
1697 		return err;
1698 
1699 	reg = ((pval >> 4) & 0xf0) | (pval & 0x0f);
1700 	err = snd_usb_ctl_msg(chip->dev,
1701 			usb_sndctrlpipe(chip->dev, 0),
1702 			UAC_SET_CUR,
1703 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1704 			reg,
1705 			2,
1706 			NULL,
1707 			0);
1708 	if (err < 0)
1709 		goto end;
1710 
1711 	reg = (pval & IEC958_AES0_NONAUDIO) ? 0xa0 : 0x20;
1712 	reg |= (pval >> 12) & 0x0f;
1713 	err = snd_usb_ctl_msg(chip->dev,
1714 			usb_sndctrlpipe(chip->dev, 0),
1715 			UAC_SET_CUR,
1716 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1717 			reg,
1718 			3,
1719 			NULL,
1720 			0);
1721 	if (err < 0)
1722 		goto end;
1723 
1724  end:
1725 	snd_usb_unlock_shutdown(chip);
1726 	return err;
1727 }
1728 
1729 static int snd_microii_spdif_default_put(struct snd_kcontrol *kcontrol,
1730 	struct snd_ctl_elem_value *ucontrol)
1731 {
1732 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1733 	unsigned int pval, pval_old;
1734 	int err;
1735 
1736 	pval = pval_old = kcontrol->private_value;
1737 	pval &= 0xfffff0f0;
1738 	pval |= (ucontrol->value.iec958.status[1] & 0x0f) << 8;
1739 	pval |= (ucontrol->value.iec958.status[0] & 0x0f);
1740 
1741 	pval &= 0xffff0fff;
1742 	pval |= (ucontrol->value.iec958.status[1] & 0xf0) << 8;
1743 
1744 	/* The frequency bits in AES3 cannot be set via register access. */
1745 
1746 	/* Silently ignore any bits from the request that cannot be set. */
1747 
1748 	if (pval == pval_old)
1749 		return 0;
1750 
1751 	kcontrol->private_value = pval;
1752 	err = snd_microii_spdif_default_update(list);
1753 	return err < 0 ? err : 1;
1754 }
1755 
1756 static int snd_microii_spdif_mask_get(struct snd_kcontrol *kcontrol,
1757 	struct snd_ctl_elem_value *ucontrol)
1758 {
1759 	ucontrol->value.iec958.status[0] = 0x0f;
1760 	ucontrol->value.iec958.status[1] = 0xff;
1761 	ucontrol->value.iec958.status[2] = 0x00;
1762 	ucontrol->value.iec958.status[3] = 0x00;
1763 
1764 	return 0;
1765 }
1766 
1767 static int snd_microii_spdif_switch_get(struct snd_kcontrol *kcontrol,
1768 	struct snd_ctl_elem_value *ucontrol)
1769 {
1770 	ucontrol->value.integer.value[0] = !(kcontrol->private_value & 0x02);
1771 
1772 	return 0;
1773 }
1774 
1775 static int snd_microii_spdif_switch_update(struct usb_mixer_elem_list *list)
1776 {
1777 	struct snd_usb_audio *chip = list->mixer->chip;
1778 	u8 reg = list->kctl->private_value;
1779 	int err;
1780 
1781 	err = snd_usb_lock_shutdown(chip);
1782 	if (err < 0)
1783 		return err;
1784 
1785 	err = snd_usb_ctl_msg(chip->dev,
1786 			usb_sndctrlpipe(chip->dev, 0),
1787 			UAC_SET_CUR,
1788 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1789 			reg,
1790 			9,
1791 			NULL,
1792 			0);
1793 
1794 	snd_usb_unlock_shutdown(chip);
1795 	return err;
1796 }
1797 
1798 static int snd_microii_spdif_switch_put(struct snd_kcontrol *kcontrol,
1799 	struct snd_ctl_elem_value *ucontrol)
1800 {
1801 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1802 	u8 reg;
1803 	int err;
1804 
1805 	reg = ucontrol->value.integer.value[0] ? 0x28 : 0x2a;
1806 	if (reg != list->kctl->private_value)
1807 		return 0;
1808 
1809 	kcontrol->private_value = reg;
1810 	err = snd_microii_spdif_switch_update(list);
1811 	return err < 0 ? err : 1;
1812 }
1813 
1814 static const struct snd_kcontrol_new snd_microii_mixer_spdif[] = {
1815 	{
1816 		.iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1817 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1818 		.info =     snd_microii_spdif_info,
1819 		.get =      snd_microii_spdif_default_get,
1820 		.put =      snd_microii_spdif_default_put,
1821 		.private_value = 0x00000100UL,/* reset value */
1822 	},
1823 	{
1824 		.access =   SNDRV_CTL_ELEM_ACCESS_READ,
1825 		.iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1826 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1827 		.info =     snd_microii_spdif_info,
1828 		.get =      snd_microii_spdif_mask_get,
1829 	},
1830 	{
1831 		.iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
1832 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1833 		.info =     snd_ctl_boolean_mono_info,
1834 		.get =      snd_microii_spdif_switch_get,
1835 		.put =      snd_microii_spdif_switch_put,
1836 		.private_value = 0x00000028UL,/* reset value */
1837 	}
1838 };
1839 
1840 static int snd_microii_controls_create(struct usb_mixer_interface *mixer)
1841 {
1842 	int err, i;
1843 	static const usb_mixer_elem_resume_func_t resume_funcs[] = {
1844 		snd_microii_spdif_default_update,
1845 		NULL,
1846 		snd_microii_spdif_switch_update
1847 	};
1848 
1849 	for (i = 0; i < ARRAY_SIZE(snd_microii_mixer_spdif); ++i) {
1850 		err = add_single_ctl_with_resume(mixer, 0,
1851 						 resume_funcs[i],
1852 						 &snd_microii_mixer_spdif[i],
1853 						 NULL);
1854 		if (err < 0)
1855 			return err;
1856 	}
1857 
1858 	return 0;
1859 }
1860 
1861 /* Creative Sound Blaster E1 */
1862 
1863 static int snd_soundblaster_e1_switch_get(struct snd_kcontrol *kcontrol,
1864 					  struct snd_ctl_elem_value *ucontrol)
1865 {
1866 	ucontrol->value.integer.value[0] = kcontrol->private_value;
1867 	return 0;
1868 }
1869 
1870 static int snd_soundblaster_e1_switch_update(struct usb_mixer_interface *mixer,
1871 					     unsigned char state)
1872 {
1873 	struct snd_usb_audio *chip = mixer->chip;
1874 	int err;
1875 	unsigned char buff[2];
1876 
1877 	buff[0] = 0x02;
1878 	buff[1] = state ? 0x02 : 0x00;
1879 
1880 	err = snd_usb_lock_shutdown(chip);
1881 	if (err < 0)
1882 		return err;
1883 	err = snd_usb_ctl_msg(chip->dev,
1884 			usb_sndctrlpipe(chip->dev, 0), HID_REQ_SET_REPORT,
1885 			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
1886 			0x0202, 3, buff, 2);
1887 	snd_usb_unlock_shutdown(chip);
1888 	return err;
1889 }
1890 
1891 static int snd_soundblaster_e1_switch_put(struct snd_kcontrol *kcontrol,
1892 					  struct snd_ctl_elem_value *ucontrol)
1893 {
1894 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1895 	unsigned char value = !!ucontrol->value.integer.value[0];
1896 	int err;
1897 
1898 	if (kcontrol->private_value == value)
1899 		return 0;
1900 	kcontrol->private_value = value;
1901 	err = snd_soundblaster_e1_switch_update(list->mixer, value);
1902 	return err < 0 ? err : 1;
1903 }
1904 
1905 static int snd_soundblaster_e1_switch_resume(struct usb_mixer_elem_list *list)
1906 {
1907 	return snd_soundblaster_e1_switch_update(list->mixer,
1908 						 list->kctl->private_value);
1909 }
1910 
1911 static int snd_soundblaster_e1_switch_info(struct snd_kcontrol *kcontrol,
1912 					   struct snd_ctl_elem_info *uinfo)
1913 {
1914 	static const char *const texts[2] = {
1915 		"Mic", "Aux"
1916 	};
1917 
1918 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1919 }
1920 
1921 static const struct snd_kcontrol_new snd_soundblaster_e1_input_switch = {
1922 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1923 	.name = "Input Source",
1924 	.info = snd_soundblaster_e1_switch_info,
1925 	.get = snd_soundblaster_e1_switch_get,
1926 	.put = snd_soundblaster_e1_switch_put,
1927 	.private_value = 0,
1928 };
1929 
1930 static int snd_soundblaster_e1_switch_create(struct usb_mixer_interface *mixer)
1931 {
1932 	return add_single_ctl_with_resume(mixer, 0,
1933 					  snd_soundblaster_e1_switch_resume,
1934 					  &snd_soundblaster_e1_input_switch,
1935 					  NULL);
1936 }
1937 
1938 /*
1939  * Dell WD15 dock jack detection
1940  *
1941  * The WD15 contains an ALC4020 USB audio controller and ALC3263 audio codec
1942  * from Realtek. It is a UAC 1 device, and UAC 1 does not support jack
1943  * detection. Instead, jack detection works by sending HD Audio commands over
1944  * vendor-type USB messages.
1945  */
1946 
1947 #define HDA_VERB_CMD(V, N, D) (((N) << 20) | ((V) << 8) | (D))
1948 
1949 #define REALTEK_HDA_VALUE 0x0038
1950 
1951 #define REALTEK_HDA_SET		62
1952 #define REALTEK_MANUAL_MODE	72
1953 #define REALTEK_HDA_GET_OUT	88
1954 #define REALTEK_HDA_GET_IN	89
1955 
1956 #define REALTEK_AUDIO_FUNCTION_GROUP	0x01
1957 #define REALTEK_LINE1			0x1a
1958 #define REALTEK_VENDOR_REGISTERS	0x20
1959 #define REALTEK_HP_OUT			0x21
1960 
1961 #define REALTEK_CBJ_CTRL2 0x50
1962 
1963 #define REALTEK_JACK_INTERRUPT_NODE 5
1964 
1965 #define REALTEK_MIC_FLAG 0x100
1966 
1967 static int realtek_hda_set(struct snd_usb_audio *chip, u32 cmd)
1968 {
1969 	struct usb_device *dev = chip->dev;
1970 	__be32 buf = cpu_to_be32(cmd);
1971 
1972 	return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), REALTEK_HDA_SET,
1973 			       USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_OUT,
1974 			       REALTEK_HDA_VALUE, 0, &buf, sizeof(buf));
1975 }
1976 
1977 static int realtek_hda_get(struct snd_usb_audio *chip, u32 cmd, u32 *value)
1978 {
1979 	struct usb_device *dev = chip->dev;
1980 	int err;
1981 	__be32 buf = cpu_to_be32(cmd);
1982 
1983 	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), REALTEK_HDA_GET_OUT,
1984 			      USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_OUT,
1985 			      REALTEK_HDA_VALUE, 0, &buf, sizeof(buf));
1986 	if (err < 0)
1987 		return err;
1988 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), REALTEK_HDA_GET_IN,
1989 			      USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_IN,
1990 			      REALTEK_HDA_VALUE, 0, &buf, sizeof(buf));
1991 	if (err < 0)
1992 		return err;
1993 
1994 	*value = be32_to_cpu(buf);
1995 	return 0;
1996 }
1997 
1998 static int realtek_ctl_connector_get(struct snd_kcontrol *kcontrol,
1999 				     struct snd_ctl_elem_value *ucontrol)
2000 {
2001 	struct usb_mixer_elem_info *cval = kcontrol->private_data;
2002 	struct snd_usb_audio *chip = cval->head.mixer->chip;
2003 	u32 pv = kcontrol->private_value;
2004 	u32 node_id = pv & 0xff;
2005 	u32 sense;
2006 	u32 cbj_ctrl2;
2007 	bool presence;
2008 	int err;
2009 
2010 	err = snd_usb_lock_shutdown(chip);
2011 	if (err < 0)
2012 		return err;
2013 	err = realtek_hda_get(chip,
2014 			      HDA_VERB_CMD(AC_VERB_GET_PIN_SENSE, node_id, 0),
2015 			      &sense);
2016 	if (err < 0)
2017 		goto err;
2018 	if (pv & REALTEK_MIC_FLAG) {
2019 		err = realtek_hda_set(chip,
2020 				      HDA_VERB_CMD(AC_VERB_SET_COEF_INDEX,
2021 						   REALTEK_VENDOR_REGISTERS,
2022 						   REALTEK_CBJ_CTRL2));
2023 		if (err < 0)
2024 			goto err;
2025 		err = realtek_hda_get(chip,
2026 				      HDA_VERB_CMD(AC_VERB_GET_PROC_COEF,
2027 						   REALTEK_VENDOR_REGISTERS, 0),
2028 				      &cbj_ctrl2);
2029 		if (err < 0)
2030 			goto err;
2031 	}
2032 err:
2033 	snd_usb_unlock_shutdown(chip);
2034 	if (err < 0)
2035 		return err;
2036 
2037 	presence = sense & AC_PINSENSE_PRESENCE;
2038 	if (pv & REALTEK_MIC_FLAG)
2039 		presence = presence && (cbj_ctrl2 & 0x0070) == 0x0070;
2040 	ucontrol->value.integer.value[0] = presence;
2041 	return 0;
2042 }
2043 
2044 static const struct snd_kcontrol_new realtek_connector_ctl_ro = {
2045 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
2046 	.name = "", /* will be filled later manually */
2047 	.access = SNDRV_CTL_ELEM_ACCESS_READ,
2048 	.info = snd_ctl_boolean_mono_info,
2049 	.get = realtek_ctl_connector_get,
2050 };
2051 
2052 static int realtek_resume_jack(struct usb_mixer_elem_list *list)
2053 {
2054 	snd_ctl_notify(list->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2055 		       &list->kctl->id);
2056 	return 0;
2057 }
2058 
2059 static int realtek_add_jack(struct usb_mixer_interface *mixer,
2060 			    char *name, u32 val)
2061 {
2062 	struct usb_mixer_elem_info *cval;
2063 	struct snd_kcontrol *kctl;
2064 
2065 	cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2066 	if (!cval)
2067 		return -ENOMEM;
2068 	snd_usb_mixer_elem_init_std(&cval->head, mixer,
2069 				    REALTEK_JACK_INTERRUPT_NODE);
2070 	cval->head.resume = realtek_resume_jack;
2071 	cval->val_type = USB_MIXER_BOOLEAN;
2072 	cval->channels = 1;
2073 	cval->min = 0;
2074 	cval->max = 1;
2075 	kctl = snd_ctl_new1(&realtek_connector_ctl_ro, cval);
2076 	if (!kctl) {
2077 		kfree(cval);
2078 		return -ENOMEM;
2079 	}
2080 	kctl->private_value = val;
2081 	strscpy(kctl->id.name, name, sizeof(kctl->id.name));
2082 	kctl->private_free = snd_usb_mixer_elem_free;
2083 	return snd_usb_mixer_add_control(&cval->head, kctl);
2084 }
2085 
2086 static int dell_dock_mixer_create(struct usb_mixer_interface *mixer)
2087 {
2088 	int err;
2089 	struct usb_device *dev = mixer->chip->dev;
2090 
2091 	/* Power down the audio codec to avoid loud pops in the next step. */
2092 	realtek_hda_set(mixer->chip,
2093 			HDA_VERB_CMD(AC_VERB_SET_POWER_STATE,
2094 				     REALTEK_AUDIO_FUNCTION_GROUP,
2095 				     AC_PWRST_D3));
2096 
2097 	/*
2098 	 * Turn off 'manual mode' in case it was enabled. This removes the need
2099 	 * to power cycle the dock after it was attached to a Windows machine.
2100 	 */
2101 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), REALTEK_MANUAL_MODE,
2102 			USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_OUT,
2103 			0, 0, NULL, 0);
2104 
2105 	err = realtek_add_jack(mixer, "Line Out Jack", REALTEK_LINE1);
2106 	if (err < 0)
2107 		return err;
2108 	err = realtek_add_jack(mixer, "Headphone Jack", REALTEK_HP_OUT);
2109 	if (err < 0)
2110 		return err;
2111 	err = realtek_add_jack(mixer, "Headset Mic Jack",
2112 			       REALTEK_HP_OUT | REALTEK_MIC_FLAG);
2113 	if (err < 0)
2114 		return err;
2115 	return 0;
2116 }
2117 
2118 static void dell_dock_init_vol(struct snd_usb_audio *chip, int ch, int id)
2119 {
2120 	u16 buf = 0;
2121 
2122 	snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
2123 			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
2124 			(UAC_FU_VOLUME << 8) | ch,
2125 			snd_usb_ctrl_intf(chip) | (id << 8),
2126 			&buf, 2);
2127 }
2128 
2129 static int dell_dock_mixer_init(struct usb_mixer_interface *mixer)
2130 {
2131 	/* fix to 0dB playback volumes */
2132 	dell_dock_init_vol(mixer->chip, 1, 16);
2133 	dell_dock_init_vol(mixer->chip, 2, 16);
2134 	dell_dock_init_vol(mixer->chip, 1, 19);
2135 	dell_dock_init_vol(mixer->chip, 2, 19);
2136 	return 0;
2137 }
2138 
2139 /* RME Class Compliant device quirks */
2140 
2141 #define SND_RME_GET_STATUS1			23
2142 #define SND_RME_GET_CURRENT_FREQ		17
2143 #define SND_RME_CLK_SYSTEM_SHIFT		16
2144 #define SND_RME_CLK_SYSTEM_MASK			0x1f
2145 #define SND_RME_CLK_AES_SHIFT			8
2146 #define SND_RME_CLK_SPDIF_SHIFT			12
2147 #define SND_RME_CLK_AES_SPDIF_MASK		0xf
2148 #define SND_RME_CLK_SYNC_SHIFT			6
2149 #define SND_RME_CLK_SYNC_MASK			0x3
2150 #define SND_RME_CLK_FREQMUL_SHIFT		18
2151 #define SND_RME_CLK_FREQMUL_MASK		0x7
2152 #define SND_RME_CLK_SYSTEM(x) \
2153 	((x >> SND_RME_CLK_SYSTEM_SHIFT) & SND_RME_CLK_SYSTEM_MASK)
2154 #define SND_RME_CLK_AES(x) \
2155 	((x >> SND_RME_CLK_AES_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
2156 #define SND_RME_CLK_SPDIF(x) \
2157 	((x >> SND_RME_CLK_SPDIF_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
2158 #define SND_RME_CLK_SYNC(x) \
2159 	((x >> SND_RME_CLK_SYNC_SHIFT) & SND_RME_CLK_SYNC_MASK)
2160 #define SND_RME_CLK_FREQMUL(x) \
2161 	((x >> SND_RME_CLK_FREQMUL_SHIFT) & SND_RME_CLK_FREQMUL_MASK)
2162 #define SND_RME_CLK_AES_LOCK			0x1
2163 #define SND_RME_CLK_AES_SYNC			0x4
2164 #define SND_RME_CLK_SPDIF_LOCK			0x2
2165 #define SND_RME_CLK_SPDIF_SYNC			0x8
2166 #define SND_RME_SPDIF_IF_SHIFT			4
2167 #define SND_RME_SPDIF_FORMAT_SHIFT		5
2168 #define SND_RME_BINARY_MASK			0x1
2169 #define SND_RME_SPDIF_IF(x) \
2170 	((x >> SND_RME_SPDIF_IF_SHIFT) & SND_RME_BINARY_MASK)
2171 #define SND_RME_SPDIF_FORMAT(x) \
2172 	((x >> SND_RME_SPDIF_FORMAT_SHIFT) & SND_RME_BINARY_MASK)
2173 
2174 static const u32 snd_rme_rate_table[] = {
2175 	32000, 44100, 48000, 50000,
2176 	64000, 88200, 96000, 100000,
2177 	128000, 176400, 192000, 200000,
2178 	256000,	352800, 384000, 400000,
2179 	512000, 705600, 768000, 800000
2180 };
2181 /* maximum number of items for AES and S/PDIF rates for above table */
2182 #define SND_RME_RATE_IDX_AES_SPDIF_NUM		12
2183 
2184 enum snd_rme_domain {
2185 	SND_RME_DOMAIN_SYSTEM,
2186 	SND_RME_DOMAIN_AES,
2187 	SND_RME_DOMAIN_SPDIF
2188 };
2189 
2190 enum snd_rme_clock_status {
2191 	SND_RME_CLOCK_NOLOCK,
2192 	SND_RME_CLOCK_LOCK,
2193 	SND_RME_CLOCK_SYNC
2194 };
2195 
2196 static int snd_rme_read_value(struct snd_usb_audio *chip,
2197 			      unsigned int item,
2198 			      u32 *value)
2199 {
2200 	struct usb_device *dev = chip->dev;
2201 	int err;
2202 
2203 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
2204 			      item,
2205 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2206 			      0, 0,
2207 			      value, sizeof(*value));
2208 	if (err < 0)
2209 		dev_err(&dev->dev,
2210 			"unable to issue vendor read request %d (ret = %d)",
2211 			item, err);
2212 	return err;
2213 }
2214 
2215 static int snd_rme_get_status1(struct snd_kcontrol *kcontrol,
2216 			       u32 *status1)
2217 {
2218 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2219 	struct snd_usb_audio *chip = list->mixer->chip;
2220 	int err;
2221 
2222 	err = snd_usb_lock_shutdown(chip);
2223 	if (err < 0)
2224 		return err;
2225 	err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, status1);
2226 	snd_usb_unlock_shutdown(chip);
2227 	return err;
2228 }
2229 
2230 static int snd_rme_rate_get(struct snd_kcontrol *kcontrol,
2231 			    struct snd_ctl_elem_value *ucontrol)
2232 {
2233 	u32 status1;
2234 	u32 rate = 0;
2235 	int idx;
2236 	int err;
2237 
2238 	err = snd_rme_get_status1(kcontrol, &status1);
2239 	if (err < 0)
2240 		return err;
2241 	switch (kcontrol->private_value) {
2242 	case SND_RME_DOMAIN_SYSTEM:
2243 		idx = SND_RME_CLK_SYSTEM(status1);
2244 		if (idx < ARRAY_SIZE(snd_rme_rate_table))
2245 			rate = snd_rme_rate_table[idx];
2246 		break;
2247 	case SND_RME_DOMAIN_AES:
2248 		idx = SND_RME_CLK_AES(status1);
2249 		if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
2250 			rate = snd_rme_rate_table[idx];
2251 		break;
2252 	case SND_RME_DOMAIN_SPDIF:
2253 		idx = SND_RME_CLK_SPDIF(status1);
2254 		if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
2255 			rate = snd_rme_rate_table[idx];
2256 		break;
2257 	default:
2258 		return -EINVAL;
2259 	}
2260 	ucontrol->value.integer.value[0] = rate;
2261 	return 0;
2262 }
2263 
2264 static int snd_rme_sync_state_get(struct snd_kcontrol *kcontrol,
2265 				  struct snd_ctl_elem_value *ucontrol)
2266 {
2267 	u32 status1;
2268 	int idx = SND_RME_CLOCK_NOLOCK;
2269 	int err;
2270 
2271 	err = snd_rme_get_status1(kcontrol, &status1);
2272 	if (err < 0)
2273 		return err;
2274 	switch (kcontrol->private_value) {
2275 	case SND_RME_DOMAIN_AES:  /* AES */
2276 		if (status1 & SND_RME_CLK_AES_SYNC)
2277 			idx = SND_RME_CLOCK_SYNC;
2278 		else if (status1 & SND_RME_CLK_AES_LOCK)
2279 			idx = SND_RME_CLOCK_LOCK;
2280 		break;
2281 	case SND_RME_DOMAIN_SPDIF:  /* SPDIF */
2282 		if (status1 & SND_RME_CLK_SPDIF_SYNC)
2283 			idx = SND_RME_CLOCK_SYNC;
2284 		else if (status1 & SND_RME_CLK_SPDIF_LOCK)
2285 			idx = SND_RME_CLOCK_LOCK;
2286 		break;
2287 	default:
2288 		return -EINVAL;
2289 	}
2290 	ucontrol->value.enumerated.item[0] = idx;
2291 	return 0;
2292 }
2293 
2294 static int snd_rme_spdif_if_get(struct snd_kcontrol *kcontrol,
2295 				struct snd_ctl_elem_value *ucontrol)
2296 {
2297 	u32 status1;
2298 	int err;
2299 
2300 	err = snd_rme_get_status1(kcontrol, &status1);
2301 	if (err < 0)
2302 		return err;
2303 	ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_IF(status1);
2304 	return 0;
2305 }
2306 
2307 static int snd_rme_spdif_format_get(struct snd_kcontrol *kcontrol,
2308 				    struct snd_ctl_elem_value *ucontrol)
2309 {
2310 	u32 status1;
2311 	int err;
2312 
2313 	err = snd_rme_get_status1(kcontrol, &status1);
2314 	if (err < 0)
2315 		return err;
2316 	ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_FORMAT(status1);
2317 	return 0;
2318 }
2319 
2320 static int snd_rme_sync_source_get(struct snd_kcontrol *kcontrol,
2321 				   struct snd_ctl_elem_value *ucontrol)
2322 {
2323 	u32 status1;
2324 	int err;
2325 
2326 	err = snd_rme_get_status1(kcontrol, &status1);
2327 	if (err < 0)
2328 		return err;
2329 	ucontrol->value.enumerated.item[0] = SND_RME_CLK_SYNC(status1);
2330 	return 0;
2331 }
2332 
2333 static int snd_rme_current_freq_get(struct snd_kcontrol *kcontrol,
2334 				    struct snd_ctl_elem_value *ucontrol)
2335 {
2336 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2337 	struct snd_usb_audio *chip = list->mixer->chip;
2338 	u32 status1;
2339 	const u64 num = 104857600000000ULL;
2340 	u32 den;
2341 	unsigned int freq;
2342 	int err;
2343 
2344 	err = snd_usb_lock_shutdown(chip);
2345 	if (err < 0)
2346 		return err;
2347 	err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, &status1);
2348 	if (err < 0)
2349 		goto end;
2350 	err = snd_rme_read_value(chip, SND_RME_GET_CURRENT_FREQ, &den);
2351 	if (err < 0)
2352 		goto end;
2353 	freq = (den == 0) ? 0 : div64_u64(num, den);
2354 	freq <<= SND_RME_CLK_FREQMUL(status1);
2355 	ucontrol->value.integer.value[0] = freq;
2356 
2357 end:
2358 	snd_usb_unlock_shutdown(chip);
2359 	return err;
2360 }
2361 
2362 static int snd_rme_rate_info(struct snd_kcontrol *kcontrol,
2363 			     struct snd_ctl_elem_info *uinfo)
2364 {
2365 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2366 	uinfo->count = 1;
2367 	switch (kcontrol->private_value) {
2368 	case SND_RME_DOMAIN_SYSTEM:
2369 		uinfo->value.integer.min = 32000;
2370 		uinfo->value.integer.max = 800000;
2371 		break;
2372 	case SND_RME_DOMAIN_AES:
2373 	case SND_RME_DOMAIN_SPDIF:
2374 	default:
2375 		uinfo->value.integer.min = 0;
2376 		uinfo->value.integer.max = 200000;
2377 	}
2378 	uinfo->value.integer.step = 0;
2379 	return 0;
2380 }
2381 
2382 static int snd_rme_sync_state_info(struct snd_kcontrol *kcontrol,
2383 				   struct snd_ctl_elem_info *uinfo)
2384 {
2385 	static const char *const sync_states[] = {
2386 		"No Lock", "Lock", "Sync"
2387 	};
2388 
2389 	return snd_ctl_enum_info(uinfo, 1,
2390 				 ARRAY_SIZE(sync_states), sync_states);
2391 }
2392 
2393 static int snd_rme_spdif_if_info(struct snd_kcontrol *kcontrol,
2394 				 struct snd_ctl_elem_info *uinfo)
2395 {
2396 	static const char *const spdif_if[] = {
2397 		"Coaxial", "Optical"
2398 	};
2399 
2400 	return snd_ctl_enum_info(uinfo, 1,
2401 				 ARRAY_SIZE(spdif_if), spdif_if);
2402 }
2403 
2404 static int snd_rme_spdif_format_info(struct snd_kcontrol *kcontrol,
2405 				     struct snd_ctl_elem_info *uinfo)
2406 {
2407 	static const char *const optical_type[] = {
2408 		"Consumer", "Professional"
2409 	};
2410 
2411 	return snd_ctl_enum_info(uinfo, 1,
2412 				 ARRAY_SIZE(optical_type), optical_type);
2413 }
2414 
2415 static int snd_rme_sync_source_info(struct snd_kcontrol *kcontrol,
2416 				    struct snd_ctl_elem_info *uinfo)
2417 {
2418 	static const char *const sync_sources[] = {
2419 		"Internal", "AES", "SPDIF", "Internal"
2420 	};
2421 
2422 	return snd_ctl_enum_info(uinfo, 1,
2423 				 ARRAY_SIZE(sync_sources), sync_sources);
2424 }
2425 
2426 static const struct snd_kcontrol_new snd_rme_controls[] = {
2427 	{
2428 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2429 		.name = "AES Rate",
2430 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2431 		.info = snd_rme_rate_info,
2432 		.get = snd_rme_rate_get,
2433 		.private_value = SND_RME_DOMAIN_AES
2434 	},
2435 	{
2436 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2437 		.name = "AES Sync",
2438 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2439 		.info = snd_rme_sync_state_info,
2440 		.get = snd_rme_sync_state_get,
2441 		.private_value = SND_RME_DOMAIN_AES
2442 	},
2443 	{
2444 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2445 		.name = "SPDIF Rate",
2446 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2447 		.info = snd_rme_rate_info,
2448 		.get = snd_rme_rate_get,
2449 		.private_value = SND_RME_DOMAIN_SPDIF
2450 	},
2451 	{
2452 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2453 		.name = "SPDIF Sync",
2454 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2455 		.info = snd_rme_sync_state_info,
2456 		.get = snd_rme_sync_state_get,
2457 		.private_value = SND_RME_DOMAIN_SPDIF
2458 	},
2459 	{
2460 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2461 		.name = "SPDIF Interface",
2462 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2463 		.info = snd_rme_spdif_if_info,
2464 		.get = snd_rme_spdif_if_get,
2465 	},
2466 	{
2467 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2468 		.name = "SPDIF Format",
2469 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2470 		.info = snd_rme_spdif_format_info,
2471 		.get = snd_rme_spdif_format_get,
2472 	},
2473 	{
2474 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2475 		.name = "Sync Source",
2476 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2477 		.info = snd_rme_sync_source_info,
2478 		.get = snd_rme_sync_source_get
2479 	},
2480 	{
2481 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2482 		.name = "System Rate",
2483 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2484 		.info = snd_rme_rate_info,
2485 		.get = snd_rme_rate_get,
2486 		.private_value = SND_RME_DOMAIN_SYSTEM
2487 	},
2488 	{
2489 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2490 		.name = "Current Frequency",
2491 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2492 		.info = snd_rme_rate_info,
2493 		.get = snd_rme_current_freq_get
2494 	}
2495 };
2496 
2497 static int snd_rme_controls_create(struct usb_mixer_interface *mixer)
2498 {
2499 	int err, i;
2500 
2501 	for (i = 0; i < ARRAY_SIZE(snd_rme_controls); ++i) {
2502 		err = add_single_ctl_with_resume(mixer, 0,
2503 						 NULL,
2504 						 &snd_rme_controls[i],
2505 						 NULL);
2506 		if (err < 0)
2507 			return err;
2508 	}
2509 
2510 	return 0;
2511 }
2512 
2513 /*
2514  * RME Babyface Pro (FS)
2515  *
2516  * These devices exposes a couple of DSP functions via request to EP0.
2517  * Switches are available via control registers, while routing is controlled
2518  * by controlling the volume on each possible crossing point.
2519  * Volume control is linear, from -inf (dec. 0) to +6dB (dec. 65536) with
2520  * 0dB being at dec. 32768.
2521  */
2522 enum {
2523 	SND_BBFPRO_CTL_REG1 = 0,
2524 	SND_BBFPRO_CTL_REG2
2525 };
2526 
2527 #define SND_BBFPRO_CTL_REG_MASK 1
2528 #define SND_BBFPRO_CTL_IDX_MASK 0xff
2529 #define SND_BBFPRO_CTL_IDX_SHIFT 1
2530 #define SND_BBFPRO_CTL_VAL_MASK 1
2531 #define SND_BBFPRO_CTL_VAL_SHIFT 9
2532 #define SND_BBFPRO_CTL_REG1_CLK_MASTER 0
2533 #define SND_BBFPRO_CTL_REG1_CLK_OPTICAL 1
2534 #define SND_BBFPRO_CTL_REG1_SPDIF_PRO 7
2535 #define SND_BBFPRO_CTL_REG1_SPDIF_EMPH 8
2536 #define SND_BBFPRO_CTL_REG1_SPDIF_OPTICAL 10
2537 #define SND_BBFPRO_CTL_REG2_48V_AN1 0
2538 #define SND_BBFPRO_CTL_REG2_48V_AN2 1
2539 #define SND_BBFPRO_CTL_REG2_SENS_IN3 2
2540 #define SND_BBFPRO_CTL_REG2_SENS_IN4 3
2541 #define SND_BBFPRO_CTL_REG2_PAD_AN1 4
2542 #define SND_BBFPRO_CTL_REG2_PAD_AN2 5
2543 
2544 #define SND_BBFPRO_MIXER_IDX_MASK 0x1ff
2545 #define SND_BBFPRO_MIXER_VAL_MASK 0x3ffff
2546 #define SND_BBFPRO_MIXER_VAL_SHIFT 9
2547 #define SND_BBFPRO_MIXER_VAL_MIN 0 // -inf
2548 #define SND_BBFPRO_MIXER_VAL_MAX 65536 // +6dB
2549 
2550 #define SND_BBFPRO_USBREQ_CTL_REG1 0x10
2551 #define SND_BBFPRO_USBREQ_CTL_REG2 0x17
2552 #define SND_BBFPRO_USBREQ_MIXER 0x12
2553 
2554 static int snd_bbfpro_ctl_update(struct usb_mixer_interface *mixer, u8 reg,
2555 				 u8 index, u8 value)
2556 {
2557 	int err;
2558 	u16 usb_req, usb_idx, usb_val;
2559 	struct snd_usb_audio *chip = mixer->chip;
2560 
2561 	err = snd_usb_lock_shutdown(chip);
2562 	if (err < 0)
2563 		return err;
2564 
2565 	if (reg == SND_BBFPRO_CTL_REG1) {
2566 		usb_req = SND_BBFPRO_USBREQ_CTL_REG1;
2567 		if (index == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) {
2568 			usb_idx = 3;
2569 			usb_val = value ? 3 : 0;
2570 		} else {
2571 			usb_idx = 1 << index;
2572 			usb_val = value ? usb_idx : 0;
2573 		}
2574 	} else {
2575 		usb_req = SND_BBFPRO_USBREQ_CTL_REG2;
2576 		usb_idx = 1 << index;
2577 		usb_val = value ? usb_idx : 0;
2578 	}
2579 
2580 	err = snd_usb_ctl_msg(chip->dev,
2581 			      usb_sndctrlpipe(chip->dev, 0), usb_req,
2582 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2583 			      usb_val, usb_idx, NULL, 0);
2584 
2585 	snd_usb_unlock_shutdown(chip);
2586 	return err;
2587 }
2588 
2589 static int snd_bbfpro_ctl_get(struct snd_kcontrol *kcontrol,
2590 			      struct snd_ctl_elem_value *ucontrol)
2591 {
2592 	u8 reg, idx, val;
2593 	int pv;
2594 
2595 	pv = kcontrol->private_value;
2596 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2597 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2598 	val = kcontrol->private_value >> SND_BBFPRO_CTL_VAL_SHIFT;
2599 
2600 	if ((reg == SND_BBFPRO_CTL_REG1 &&
2601 	     idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) ||
2602 	    (reg == SND_BBFPRO_CTL_REG2 &&
2603 	    (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2604 	     idx == SND_BBFPRO_CTL_REG2_SENS_IN4))) {
2605 		ucontrol->value.enumerated.item[0] = val;
2606 	} else {
2607 		ucontrol->value.integer.value[0] = val;
2608 	}
2609 	return 0;
2610 }
2611 
2612 static int snd_bbfpro_ctl_info(struct snd_kcontrol *kcontrol,
2613 			       struct snd_ctl_elem_info *uinfo)
2614 {
2615 	u8 reg, idx;
2616 	int pv;
2617 
2618 	pv = kcontrol->private_value;
2619 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2620 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2621 
2622 	if (reg == SND_BBFPRO_CTL_REG1 &&
2623 	    idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) {
2624 		static const char * const texts[2] = {
2625 			"AutoSync",
2626 			"Internal"
2627 		};
2628 		return snd_ctl_enum_info(uinfo, 1, 2, texts);
2629 	} else if (reg == SND_BBFPRO_CTL_REG2 &&
2630 		   (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2631 		    idx == SND_BBFPRO_CTL_REG2_SENS_IN4)) {
2632 		static const char * const texts[2] = {
2633 			"-10dBV",
2634 			"+4dBu"
2635 		};
2636 		return snd_ctl_enum_info(uinfo, 1, 2, texts);
2637 	}
2638 
2639 	uinfo->count = 1;
2640 	uinfo->value.integer.min = 0;
2641 	uinfo->value.integer.max = 1;
2642 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2643 	return 0;
2644 }
2645 
2646 static int snd_bbfpro_ctl_put(struct snd_kcontrol *kcontrol,
2647 			      struct snd_ctl_elem_value *ucontrol)
2648 {
2649 	int err;
2650 	u8 reg, idx;
2651 	int old_value, pv, val;
2652 
2653 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2654 	struct usb_mixer_interface *mixer = list->mixer;
2655 
2656 	pv = kcontrol->private_value;
2657 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2658 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2659 	old_value = (pv >> SND_BBFPRO_CTL_VAL_SHIFT) & SND_BBFPRO_CTL_VAL_MASK;
2660 
2661 	if ((reg == SND_BBFPRO_CTL_REG1 &&
2662 	     idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) ||
2663 	    (reg == SND_BBFPRO_CTL_REG2 &&
2664 	    (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2665 	     idx == SND_BBFPRO_CTL_REG2_SENS_IN4))) {
2666 		val = ucontrol->value.enumerated.item[0];
2667 	} else {
2668 		val = ucontrol->value.integer.value[0];
2669 	}
2670 
2671 	if (val > 1)
2672 		return -EINVAL;
2673 
2674 	if (val == old_value)
2675 		return 0;
2676 
2677 	kcontrol->private_value = reg
2678 		| ((idx & SND_BBFPRO_CTL_IDX_MASK) << SND_BBFPRO_CTL_IDX_SHIFT)
2679 		| ((val & SND_BBFPRO_CTL_VAL_MASK) << SND_BBFPRO_CTL_VAL_SHIFT);
2680 
2681 	err = snd_bbfpro_ctl_update(mixer, reg, idx, val);
2682 	return err < 0 ? err : 1;
2683 }
2684 
2685 static int snd_bbfpro_ctl_resume(struct usb_mixer_elem_list *list)
2686 {
2687 	u8 reg, idx;
2688 	int value, pv;
2689 
2690 	pv = list->kctl->private_value;
2691 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2692 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2693 	value = (pv >> SND_BBFPRO_CTL_VAL_SHIFT) & SND_BBFPRO_CTL_VAL_MASK;
2694 
2695 	return snd_bbfpro_ctl_update(list->mixer, reg, idx, value);
2696 }
2697 
2698 static int snd_bbfpro_vol_update(struct usb_mixer_interface *mixer, u16 index,
2699 				 u32 value)
2700 {
2701 	struct snd_usb_audio *chip = mixer->chip;
2702 	int err;
2703 	u16 idx;
2704 	u16 usb_idx, usb_val;
2705 	u32 v;
2706 
2707 	err = snd_usb_lock_shutdown(chip);
2708 	if (err < 0)
2709 		return err;
2710 
2711 	idx = index & SND_BBFPRO_MIXER_IDX_MASK;
2712 	// 18 bit linear volume, split so 2 bits end up in index.
2713 	v = value & SND_BBFPRO_MIXER_VAL_MASK;
2714 	usb_idx = idx | (v & 0x3) << 14;
2715 	usb_val = (v >> 2) & 0xffff;
2716 
2717 	err = snd_usb_ctl_msg(chip->dev,
2718 			      usb_sndctrlpipe(chip->dev, 0),
2719 			      SND_BBFPRO_USBREQ_MIXER,
2720 			      USB_DIR_OUT | USB_TYPE_VENDOR |
2721 			      USB_RECIP_DEVICE,
2722 			      usb_val, usb_idx, NULL, 0);
2723 
2724 	snd_usb_unlock_shutdown(chip);
2725 	return err;
2726 }
2727 
2728 static int snd_bbfpro_vol_get(struct snd_kcontrol *kcontrol,
2729 			      struct snd_ctl_elem_value *ucontrol)
2730 {
2731 	ucontrol->value.integer.value[0] =
2732 		kcontrol->private_value >> SND_BBFPRO_MIXER_VAL_SHIFT;
2733 	return 0;
2734 }
2735 
2736 static int snd_bbfpro_vol_info(struct snd_kcontrol *kcontrol,
2737 			       struct snd_ctl_elem_info *uinfo)
2738 {
2739 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2740 	uinfo->count = 1;
2741 	uinfo->value.integer.min = SND_BBFPRO_MIXER_VAL_MIN;
2742 	uinfo->value.integer.max = SND_BBFPRO_MIXER_VAL_MAX;
2743 	return 0;
2744 }
2745 
2746 static int snd_bbfpro_vol_put(struct snd_kcontrol *kcontrol,
2747 			      struct snd_ctl_elem_value *ucontrol)
2748 {
2749 	int err;
2750 	u16 idx;
2751 	u32 new_val, old_value, uvalue;
2752 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2753 	struct usb_mixer_interface *mixer = list->mixer;
2754 
2755 	uvalue = ucontrol->value.integer.value[0];
2756 	idx = kcontrol->private_value & SND_BBFPRO_MIXER_IDX_MASK;
2757 	old_value = kcontrol->private_value >> SND_BBFPRO_MIXER_VAL_SHIFT;
2758 
2759 	if (uvalue > SND_BBFPRO_MIXER_VAL_MAX)
2760 		return -EINVAL;
2761 
2762 	if (uvalue == old_value)
2763 		return 0;
2764 
2765 	new_val = uvalue & SND_BBFPRO_MIXER_VAL_MASK;
2766 
2767 	kcontrol->private_value = idx
2768 		| (new_val << SND_BBFPRO_MIXER_VAL_SHIFT);
2769 
2770 	err = snd_bbfpro_vol_update(mixer, idx, new_val);
2771 	return err < 0 ? err : 1;
2772 }
2773 
2774 static int snd_bbfpro_vol_resume(struct usb_mixer_elem_list *list)
2775 {
2776 	int pv = list->kctl->private_value;
2777 	u16 idx = pv & SND_BBFPRO_MIXER_IDX_MASK;
2778 	u32 val = (pv >> SND_BBFPRO_MIXER_VAL_SHIFT)
2779 		& SND_BBFPRO_MIXER_VAL_MASK;
2780 	return snd_bbfpro_vol_update(list->mixer, idx, val);
2781 }
2782 
2783 // Predfine elements
2784 static const struct snd_kcontrol_new snd_bbfpro_ctl_control = {
2785 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2786 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2787 	.index = 0,
2788 	.info = snd_bbfpro_ctl_info,
2789 	.get = snd_bbfpro_ctl_get,
2790 	.put = snd_bbfpro_ctl_put
2791 };
2792 
2793 static const struct snd_kcontrol_new snd_bbfpro_vol_control = {
2794 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2795 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2796 	.index = 0,
2797 	.info = snd_bbfpro_vol_info,
2798 	.get = snd_bbfpro_vol_get,
2799 	.put = snd_bbfpro_vol_put
2800 };
2801 
2802 static int snd_bbfpro_ctl_add(struct usb_mixer_interface *mixer, u8 reg,
2803 			      u8 index, char *name)
2804 {
2805 	struct snd_kcontrol_new knew = snd_bbfpro_ctl_control;
2806 
2807 	knew.name = name;
2808 	knew.private_value = (reg & SND_BBFPRO_CTL_REG_MASK)
2809 		| ((index & SND_BBFPRO_CTL_IDX_MASK)
2810 			<< SND_BBFPRO_CTL_IDX_SHIFT);
2811 
2812 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_ctl_resume,
2813 		&knew, NULL);
2814 }
2815 
2816 static int snd_bbfpro_vol_add(struct usb_mixer_interface *mixer, u16 index,
2817 			      char *name)
2818 {
2819 	struct snd_kcontrol_new knew = snd_bbfpro_vol_control;
2820 
2821 	knew.name = name;
2822 	knew.private_value = index & SND_BBFPRO_MIXER_IDX_MASK;
2823 
2824 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_vol_resume,
2825 		&knew, NULL);
2826 }
2827 
2828 static int snd_bbfpro_controls_create(struct usb_mixer_interface *mixer)
2829 {
2830 	int err, i, o;
2831 	char name[48];
2832 
2833 	static const char * const input[] = {
2834 		"AN1", "AN2", "IN3", "IN4", "AS1", "AS2", "ADAT3",
2835 		"ADAT4", "ADAT5", "ADAT6", "ADAT7", "ADAT8"};
2836 
2837 	static const char * const output[] = {
2838 		"AN1", "AN2", "PH3", "PH4", "AS1", "AS2", "ADAT3", "ADAT4",
2839 		"ADAT5", "ADAT6", "ADAT7", "ADAT8"};
2840 
2841 	for (o = 0 ; o < 12 ; ++o) {
2842 		for (i = 0 ; i < 12 ; ++i) {
2843 			// Line routing
2844 			snprintf(name, sizeof(name),
2845 				 "%s-%s-%s Playback Volume",
2846 				 (i < 2 ? "Mic" : "Line"),
2847 				 input[i], output[o]);
2848 			err = snd_bbfpro_vol_add(mixer, (26 * o + i), name);
2849 			if (err < 0)
2850 				return err;
2851 
2852 			// PCM routing... yes, it is output remapping
2853 			snprintf(name, sizeof(name),
2854 				 "PCM-%s-%s Playback Volume",
2855 				 output[i], output[o]);
2856 			err = snd_bbfpro_vol_add(mixer, (26 * o + 12 + i),
2857 						 name);
2858 			if (err < 0)
2859 				return err;
2860 		}
2861 	}
2862 
2863 	// Control Reg 1
2864 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
2865 				 SND_BBFPRO_CTL_REG1_CLK_OPTICAL,
2866 				 "Sample Clock Source");
2867 	if (err < 0)
2868 		return err;
2869 
2870 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
2871 				 SND_BBFPRO_CTL_REG1_SPDIF_PRO,
2872 				 "IEC958 Pro Mask");
2873 	if (err < 0)
2874 		return err;
2875 
2876 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
2877 				 SND_BBFPRO_CTL_REG1_SPDIF_EMPH,
2878 				 "IEC958 Emphasis");
2879 	if (err < 0)
2880 		return err;
2881 
2882 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
2883 				 SND_BBFPRO_CTL_REG1_SPDIF_OPTICAL,
2884 				 "IEC958 Switch");
2885 	if (err < 0)
2886 		return err;
2887 
2888 	// Control Reg 2
2889 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
2890 				 SND_BBFPRO_CTL_REG2_48V_AN1,
2891 				 "Mic-AN1 48V");
2892 	if (err < 0)
2893 		return err;
2894 
2895 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
2896 				 SND_BBFPRO_CTL_REG2_48V_AN2,
2897 				 "Mic-AN2 48V");
2898 	if (err < 0)
2899 		return err;
2900 
2901 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
2902 				 SND_BBFPRO_CTL_REG2_SENS_IN3,
2903 				 "Line-IN3 Sens.");
2904 	if (err < 0)
2905 		return err;
2906 
2907 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
2908 				 SND_BBFPRO_CTL_REG2_SENS_IN4,
2909 				 "Line-IN4 Sens.");
2910 	if (err < 0)
2911 		return err;
2912 
2913 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
2914 				 SND_BBFPRO_CTL_REG2_PAD_AN1,
2915 				 "Mic-AN1 PAD");
2916 	if (err < 0)
2917 		return err;
2918 
2919 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
2920 				 SND_BBFPRO_CTL_REG2_PAD_AN2,
2921 				 "Mic-AN2 PAD");
2922 	if (err < 0)
2923 		return err;
2924 
2925 	return 0;
2926 }
2927 
2928 /*
2929  * Pioneer DJ DJM Mixers
2930  *
2931  * These devices generally have options for soft-switching the playback and
2932  * capture sources in addition to the recording level. Although different
2933  * devices have different configurations, there seems to be canonical values
2934  * for specific capture/playback types:  See the definitions of these below.
2935  *
2936  * The wValue is masked with the stereo channel number. e.g. Setting Ch2 to
2937  * capture phono would be 0x0203. Capture, playback and capture level have
2938  * different wIndexes.
2939  */
2940 
2941 // Capture types
2942 #define SND_DJM_CAP_LINE	0x00
2943 #define SND_DJM_CAP_CDLINE	0x01
2944 #define SND_DJM_CAP_DIGITAL	0x02
2945 #define SND_DJM_CAP_PHONO	0x03
2946 #define SND_DJM_CAP_PFADER	0x06
2947 #define SND_DJM_CAP_XFADERA	0x07
2948 #define SND_DJM_CAP_XFADERB	0x08
2949 #define SND_DJM_CAP_MIC		0x09
2950 #define SND_DJM_CAP_AUX		0x0d
2951 #define SND_DJM_CAP_RECOUT	0x0a
2952 #define SND_DJM_CAP_NONE	0x0f
2953 #define SND_DJM_CAP_CH1PFADER	0x11
2954 #define SND_DJM_CAP_CH2PFADER	0x12
2955 #define SND_DJM_CAP_CH3PFADER	0x13
2956 #define SND_DJM_CAP_CH4PFADER	0x14
2957 
2958 // Playback types
2959 #define SND_DJM_PB_CH1		0x00
2960 #define SND_DJM_PB_CH2		0x01
2961 #define SND_DJM_PB_AUX		0x04
2962 
2963 #define SND_DJM_WINDEX_CAP	0x8002
2964 #define SND_DJM_WINDEX_CAPLVL	0x8003
2965 #define SND_DJM_WINDEX_PB	0x8016
2966 
2967 // kcontrol->private_value layout
2968 #define SND_DJM_VALUE_MASK	0x0000ffff
2969 #define SND_DJM_GROUP_MASK	0x00ff0000
2970 #define SND_DJM_DEVICE_MASK	0xff000000
2971 #define SND_DJM_GROUP_SHIFT	16
2972 #define SND_DJM_DEVICE_SHIFT	24
2973 
2974 // device table index
2975 // used for the snd_djm_devices table, so please update accordingly
2976 #define SND_DJM_250MK2_IDX	0x0
2977 #define SND_DJM_750_IDX		0x1
2978 #define SND_DJM_850_IDX		0x2
2979 #define SND_DJM_900NXS2_IDX	0x3
2980 #define SND_DJM_750MK2_IDX	0x4
2981 #define SND_DJM_450_IDX		0x5
2982 
2983 
2984 #define SND_DJM_CTL(_name, suffix, _default_value, _windex) { \
2985 	.name = _name, \
2986 	.options = snd_djm_opts_##suffix, \
2987 	.noptions = ARRAY_SIZE(snd_djm_opts_##suffix), \
2988 	.default_value = _default_value, \
2989 	.wIndex = _windex }
2990 
2991 #define SND_DJM_DEVICE(suffix) { \
2992 	.controls = snd_djm_ctls_##suffix, \
2993 	.ncontrols = ARRAY_SIZE(snd_djm_ctls_##suffix) }
2994 
2995 
2996 struct snd_djm_device {
2997 	const char *name;
2998 	const struct snd_djm_ctl *controls;
2999 	size_t ncontrols;
3000 };
3001 
3002 struct snd_djm_ctl {
3003 	const char *name;
3004 	const u16 *options;
3005 	size_t noptions;
3006 	u16 default_value;
3007 	u16 wIndex;
3008 };
3009 
3010 static const char *snd_djm_get_label_caplevel(u16 wvalue)
3011 {
3012 	switch (wvalue) {
3013 	case 0x0000:	return "-19dB";
3014 	case 0x0100:	return "-15dB";
3015 	case 0x0200:	return "-10dB";
3016 	case 0x0300:	return "-5dB";
3017 	default:	return NULL;
3018 	}
3019 };
3020 
3021 static const char *snd_djm_get_label_cap_common(u16 wvalue)
3022 {
3023 	switch (wvalue & 0x00ff) {
3024 	case SND_DJM_CAP_LINE:		return "Control Tone LINE";
3025 	case SND_DJM_CAP_CDLINE:	return "Control Tone CD/LINE";
3026 	case SND_DJM_CAP_DIGITAL:	return "Control Tone DIGITAL";
3027 	case SND_DJM_CAP_PHONO:		return "Control Tone PHONO";
3028 	case SND_DJM_CAP_PFADER:	return "Post Fader";
3029 	case SND_DJM_CAP_XFADERA:	return "Cross Fader A";
3030 	case SND_DJM_CAP_XFADERB:	return "Cross Fader B";
3031 	case SND_DJM_CAP_MIC:		return "Mic";
3032 	case SND_DJM_CAP_RECOUT:	return "Rec Out";
3033 	case SND_DJM_CAP_AUX:		return "Aux";
3034 	case SND_DJM_CAP_NONE:		return "None";
3035 	case SND_DJM_CAP_CH1PFADER:	return "Post Fader Ch1";
3036 	case SND_DJM_CAP_CH2PFADER:	return "Post Fader Ch2";
3037 	case SND_DJM_CAP_CH3PFADER:	return "Post Fader Ch3";
3038 	case SND_DJM_CAP_CH4PFADER:	return "Post Fader Ch4";
3039 	default:			return NULL;
3040 	}
3041 };
3042 
3043 // The DJM-850 has different values for CD/LINE and LINE capture
3044 // control options than the other DJM declared in this file.
3045 static const char *snd_djm_get_label_cap_850(u16 wvalue)
3046 {
3047 	switch (wvalue & 0x00ff) {
3048 	case 0x00:		return "Control Tone CD/LINE";
3049 	case 0x01:		return "Control Tone LINE";
3050 	default:		return snd_djm_get_label_cap_common(wvalue);
3051 	}
3052 };
3053 
3054 static const char *snd_djm_get_label_cap(u8 device_idx, u16 wvalue)
3055 {
3056 	switch (device_idx) {
3057 	case SND_DJM_850_IDX:		return snd_djm_get_label_cap_850(wvalue);
3058 	default:			return snd_djm_get_label_cap_common(wvalue);
3059 	}
3060 };
3061 
3062 static const char *snd_djm_get_label_pb(u16 wvalue)
3063 {
3064 	switch (wvalue & 0x00ff) {
3065 	case SND_DJM_PB_CH1:	return "Ch1";
3066 	case SND_DJM_PB_CH2:	return "Ch2";
3067 	case SND_DJM_PB_AUX:	return "Aux";
3068 	default:		return NULL;
3069 	}
3070 };
3071 
3072 static const char *snd_djm_get_label(u8 device_idx, u16 wvalue, u16 windex)
3073 {
3074 	switch (windex) {
3075 	case SND_DJM_WINDEX_CAPLVL:	return snd_djm_get_label_caplevel(wvalue);
3076 	case SND_DJM_WINDEX_CAP:	return snd_djm_get_label_cap(device_idx, wvalue);
3077 	case SND_DJM_WINDEX_PB:		return snd_djm_get_label_pb(wvalue);
3078 	default:			return NULL;
3079 	}
3080 };
3081 
3082 // common DJM capture level option values
3083 static const u16 snd_djm_opts_cap_level[] = {
3084 	0x0000, 0x0100, 0x0200, 0x0300 };
3085 
3086 
3087 // DJM-250MK2
3088 static const u16 snd_djm_opts_250mk2_cap1[] = {
3089 	0x0103, 0x0100, 0x0106, 0x0107, 0x0108, 0x0109, 0x010d, 0x010a };
3090 
3091 static const u16 snd_djm_opts_250mk2_cap2[] = {
3092 	0x0203, 0x0200, 0x0206, 0x0207, 0x0208, 0x0209, 0x020d, 0x020a };
3093 
3094 static const u16 snd_djm_opts_250mk2_cap3[] = {
3095 	0x030a, 0x0311, 0x0312, 0x0307, 0x0308, 0x0309, 0x030d };
3096 
3097 static const u16 snd_djm_opts_250mk2_pb1[] = { 0x0100, 0x0101, 0x0104 };
3098 static const u16 snd_djm_opts_250mk2_pb2[] = { 0x0200, 0x0201, 0x0204 };
3099 static const u16 snd_djm_opts_250mk2_pb3[] = { 0x0300, 0x0301, 0x0304 };
3100 
3101 static const struct snd_djm_ctl snd_djm_ctls_250mk2[] = {
3102 	SND_DJM_CTL("Capture Level", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3103 	SND_DJM_CTL("Ch1 Input",   250mk2_cap1, 2, SND_DJM_WINDEX_CAP),
3104 	SND_DJM_CTL("Ch2 Input",   250mk2_cap2, 2, SND_DJM_WINDEX_CAP),
3105 	SND_DJM_CTL("Ch3 Input",   250mk2_cap3, 0, SND_DJM_WINDEX_CAP),
3106 	SND_DJM_CTL("Ch1 Output",   250mk2_pb1, 0, SND_DJM_WINDEX_PB),
3107 	SND_DJM_CTL("Ch2 Output",   250mk2_pb2, 1, SND_DJM_WINDEX_PB),
3108 	SND_DJM_CTL("Ch3 Output",   250mk2_pb3, 2, SND_DJM_WINDEX_PB)
3109 };
3110 
3111 
3112 // DJM-450
3113 static const u16 snd_djm_opts_450_cap1[] = {
3114 	0x0103, 0x0100, 0x0106, 0x0107, 0x0108, 0x0109, 0x010d, 0x010a };
3115 
3116 static const u16 snd_djm_opts_450_cap2[] = {
3117 	0x0203, 0x0200, 0x0206, 0x0207, 0x0208, 0x0209, 0x020d, 0x020a };
3118 
3119 static const u16 snd_djm_opts_450_cap3[] = {
3120 	0x030a, 0x0311, 0x0312, 0x0307, 0x0308, 0x0309, 0x030d };
3121 
3122 static const u16 snd_djm_opts_450_pb1[] = { 0x0100, 0x0101, 0x0104 };
3123 static const u16 snd_djm_opts_450_pb2[] = { 0x0200, 0x0201, 0x0204 };
3124 static const u16 snd_djm_opts_450_pb3[] = { 0x0300, 0x0301, 0x0304 };
3125 
3126 static const struct snd_djm_ctl snd_djm_ctls_450[] = {
3127 	SND_DJM_CTL("Capture Level", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3128 	SND_DJM_CTL("Ch1 Input",   450_cap1, 2, SND_DJM_WINDEX_CAP),
3129 	SND_DJM_CTL("Ch2 Input",   450_cap2, 2, SND_DJM_WINDEX_CAP),
3130 	SND_DJM_CTL("Ch3 Input",   450_cap3, 0, SND_DJM_WINDEX_CAP),
3131 	SND_DJM_CTL("Ch1 Output",   450_pb1, 0, SND_DJM_WINDEX_PB),
3132 	SND_DJM_CTL("Ch2 Output",   450_pb2, 1, SND_DJM_WINDEX_PB),
3133 	SND_DJM_CTL("Ch3 Output",   450_pb3, 2, SND_DJM_WINDEX_PB)
3134 };
3135 
3136 
3137 // DJM-750
3138 static const u16 snd_djm_opts_750_cap1[] = {
3139 	0x0101, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a, 0x010f };
3140 static const u16 snd_djm_opts_750_cap2[] = {
3141 	0x0200, 0x0201, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020f };
3142 static const u16 snd_djm_opts_750_cap3[] = {
3143 	0x0300, 0x0301, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030f };
3144 static const u16 snd_djm_opts_750_cap4[] = {
3145 	0x0401, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040f };
3146 
3147 static const struct snd_djm_ctl snd_djm_ctls_750[] = {
3148 	SND_DJM_CTL("Capture Level", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3149 	SND_DJM_CTL("Ch1 Input",   750_cap1, 2, SND_DJM_WINDEX_CAP),
3150 	SND_DJM_CTL("Ch2 Input",   750_cap2, 2, SND_DJM_WINDEX_CAP),
3151 	SND_DJM_CTL("Ch3 Input",   750_cap3, 0, SND_DJM_WINDEX_CAP),
3152 	SND_DJM_CTL("Ch4 Input",   750_cap4, 0, SND_DJM_WINDEX_CAP)
3153 };
3154 
3155 
3156 // DJM-850
3157 static const u16 snd_djm_opts_850_cap1[] = {
3158 	0x0100, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a, 0x010f };
3159 static const u16 snd_djm_opts_850_cap2[] = {
3160 	0x0200, 0x0201, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020f };
3161 static const u16 snd_djm_opts_850_cap3[] = {
3162 	0x0300, 0x0301, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030f };
3163 static const u16 snd_djm_opts_850_cap4[] = {
3164 	0x0400, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040f };
3165 
3166 static const struct snd_djm_ctl snd_djm_ctls_850[] = {
3167 	SND_DJM_CTL("Capture Level", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3168 	SND_DJM_CTL("Ch1 Input",   850_cap1, 1, SND_DJM_WINDEX_CAP),
3169 	SND_DJM_CTL("Ch2 Input",   850_cap2, 0, SND_DJM_WINDEX_CAP),
3170 	SND_DJM_CTL("Ch3 Input",   850_cap3, 0, SND_DJM_WINDEX_CAP),
3171 	SND_DJM_CTL("Ch4 Input",   850_cap4, 1, SND_DJM_WINDEX_CAP)
3172 };
3173 
3174 
3175 // DJM-900NXS2
3176 static const u16 snd_djm_opts_900nxs2_cap1[] = {
3177 	0x0100, 0x0102, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a };
3178 static const u16 snd_djm_opts_900nxs2_cap2[] = {
3179 	0x0200, 0x0202, 0x0203, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a };
3180 static const u16 snd_djm_opts_900nxs2_cap3[] = {
3181 	0x0300, 0x0302, 0x0303, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a };
3182 static const u16 snd_djm_opts_900nxs2_cap4[] = {
3183 	0x0400, 0x0402, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a };
3184 static const u16 snd_djm_opts_900nxs2_cap5[] = {
3185 	0x0507, 0x0508, 0x0509, 0x050a, 0x0511, 0x0512, 0x0513, 0x0514 };
3186 
3187 static const struct snd_djm_ctl snd_djm_ctls_900nxs2[] = {
3188 	SND_DJM_CTL("Capture Level", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3189 	SND_DJM_CTL("Ch1 Input",   900nxs2_cap1, 2, SND_DJM_WINDEX_CAP),
3190 	SND_DJM_CTL("Ch2 Input",   900nxs2_cap2, 2, SND_DJM_WINDEX_CAP),
3191 	SND_DJM_CTL("Ch3 Input",   900nxs2_cap3, 2, SND_DJM_WINDEX_CAP),
3192 	SND_DJM_CTL("Ch4 Input",   900nxs2_cap4, 2, SND_DJM_WINDEX_CAP),
3193 	SND_DJM_CTL("Ch5 Input",   900nxs2_cap5, 3, SND_DJM_WINDEX_CAP)
3194 };
3195 
3196 // DJM-750MK2
3197 static const u16 snd_djm_opts_750mk2_cap1[] = {
3198 	0x0100, 0x0102, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a };
3199 static const u16 snd_djm_opts_750mk2_cap2[] = {
3200 	0x0200, 0x0202, 0x0203, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a };
3201 static const u16 snd_djm_opts_750mk2_cap3[] = {
3202 	0x0300, 0x0302, 0x0303, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a };
3203 static const u16 snd_djm_opts_750mk2_cap4[] = {
3204 	0x0400, 0x0402, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a };
3205 static const u16 snd_djm_opts_750mk2_cap5[] = {
3206 	0x0507, 0x0508, 0x0509, 0x050a, 0x0511, 0x0512, 0x0513, 0x0514 };
3207 
3208 static const u16 snd_djm_opts_750mk2_pb1[] = { 0x0100, 0x0101, 0x0104 };
3209 static const u16 snd_djm_opts_750mk2_pb2[] = { 0x0200, 0x0201, 0x0204 };
3210 static const u16 snd_djm_opts_750mk2_pb3[] = { 0x0300, 0x0301, 0x0304 };
3211 
3212 
3213 static const struct snd_djm_ctl snd_djm_ctls_750mk2[] = {
3214 	SND_DJM_CTL("Capture Level", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3215 	SND_DJM_CTL("Ch1 Input",   750mk2_cap1, 2, SND_DJM_WINDEX_CAP),
3216 	SND_DJM_CTL("Ch2 Input",   750mk2_cap2, 2, SND_DJM_WINDEX_CAP),
3217 	SND_DJM_CTL("Ch3 Input",   750mk2_cap3, 2, SND_DJM_WINDEX_CAP),
3218 	SND_DJM_CTL("Ch4 Input",   750mk2_cap4, 2, SND_DJM_WINDEX_CAP),
3219 	SND_DJM_CTL("Ch5 Input",   750mk2_cap5, 3, SND_DJM_WINDEX_CAP),
3220 	SND_DJM_CTL("Ch1 Output",   750mk2_pb1, 0, SND_DJM_WINDEX_PB),
3221 	SND_DJM_CTL("Ch2 Output",   750mk2_pb2, 1, SND_DJM_WINDEX_PB),
3222 	SND_DJM_CTL("Ch3 Output",   750mk2_pb3, 2, SND_DJM_WINDEX_PB)
3223 };
3224 
3225 
3226 static const struct snd_djm_device snd_djm_devices[] = {
3227 	[SND_DJM_250MK2_IDX] = SND_DJM_DEVICE(250mk2),
3228 	[SND_DJM_750_IDX] = SND_DJM_DEVICE(750),
3229 	[SND_DJM_850_IDX] = SND_DJM_DEVICE(850),
3230 	[SND_DJM_900NXS2_IDX] = SND_DJM_DEVICE(900nxs2),
3231 	[SND_DJM_750MK2_IDX] = SND_DJM_DEVICE(750mk2),
3232 	[SND_DJM_450_IDX] = SND_DJM_DEVICE(450),
3233 };
3234 
3235 
3236 static int snd_djm_controls_info(struct snd_kcontrol *kctl,
3237 				struct snd_ctl_elem_info *info)
3238 {
3239 	unsigned long private_value = kctl->private_value;
3240 	u8 device_idx = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
3241 	u8 ctl_idx = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
3242 	const struct snd_djm_device *device = &snd_djm_devices[device_idx];
3243 	const char *name;
3244 	const struct snd_djm_ctl *ctl;
3245 	size_t noptions;
3246 
3247 	if (ctl_idx >= device->ncontrols)
3248 		return -EINVAL;
3249 
3250 	ctl = &device->controls[ctl_idx];
3251 	noptions = ctl->noptions;
3252 	if (info->value.enumerated.item >= noptions)
3253 		info->value.enumerated.item = noptions - 1;
3254 
3255 	name = snd_djm_get_label(device_idx,
3256 				ctl->options[info->value.enumerated.item],
3257 				ctl->wIndex);
3258 	if (!name)
3259 		return -EINVAL;
3260 
3261 	strscpy(info->value.enumerated.name, name, sizeof(info->value.enumerated.name));
3262 	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3263 	info->count = 1;
3264 	info->value.enumerated.items = noptions;
3265 	return 0;
3266 }
3267 
3268 static int snd_djm_controls_update(struct usb_mixer_interface *mixer,
3269 				u8 device_idx, u8 group, u16 value)
3270 {
3271 	int err;
3272 	const struct snd_djm_device *device = &snd_djm_devices[device_idx];
3273 
3274 	if ((group >= device->ncontrols) || value >= device->controls[group].noptions)
3275 		return -EINVAL;
3276 
3277 	err = snd_usb_lock_shutdown(mixer->chip);
3278 	if (err)
3279 		return err;
3280 
3281 	err = snd_usb_ctl_msg(
3282 		mixer->chip->dev, usb_sndctrlpipe(mixer->chip->dev, 0),
3283 		USB_REQ_SET_FEATURE,
3284 		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3285 		device->controls[group].options[value],
3286 		device->controls[group].wIndex,
3287 		NULL, 0);
3288 
3289 	snd_usb_unlock_shutdown(mixer->chip);
3290 	return err;
3291 }
3292 
3293 static int snd_djm_controls_get(struct snd_kcontrol *kctl,
3294 				struct snd_ctl_elem_value *elem)
3295 {
3296 	elem->value.enumerated.item[0] = kctl->private_value & SND_DJM_VALUE_MASK;
3297 	return 0;
3298 }
3299 
3300 static int snd_djm_controls_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *elem)
3301 {
3302 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
3303 	struct usb_mixer_interface *mixer = list->mixer;
3304 	unsigned long private_value = kctl->private_value;
3305 
3306 	u8 device = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
3307 	u8 group = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
3308 	u16 value = elem->value.enumerated.item[0];
3309 
3310 	kctl->private_value = (((unsigned long)device << SND_DJM_DEVICE_SHIFT) |
3311 			      (group << SND_DJM_GROUP_SHIFT) |
3312 			      value);
3313 
3314 	return snd_djm_controls_update(mixer, device, group, value);
3315 }
3316 
3317 static int snd_djm_controls_resume(struct usb_mixer_elem_list *list)
3318 {
3319 	unsigned long private_value = list->kctl->private_value;
3320 	u8 device = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
3321 	u8 group = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
3322 	u16 value = (private_value & SND_DJM_VALUE_MASK);
3323 
3324 	return snd_djm_controls_update(list->mixer, device, group, value);
3325 }
3326 
3327 static int snd_djm_controls_create(struct usb_mixer_interface *mixer,
3328 		const u8 device_idx)
3329 {
3330 	int err, i;
3331 	u16 value;
3332 
3333 	const struct snd_djm_device *device = &snd_djm_devices[device_idx];
3334 
3335 	struct snd_kcontrol_new knew = {
3336 		.iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
3337 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3338 		.index = 0,
3339 		.info = snd_djm_controls_info,
3340 		.get  = snd_djm_controls_get,
3341 		.put  = snd_djm_controls_put
3342 	};
3343 
3344 	for (i = 0; i < device->ncontrols; i++) {
3345 		value = device->controls[i].default_value;
3346 		knew.name = device->controls[i].name;
3347 		knew.private_value = (
3348 			((unsigned long)device_idx << SND_DJM_DEVICE_SHIFT) |
3349 			(i << SND_DJM_GROUP_SHIFT) |
3350 			value);
3351 		err = snd_djm_controls_update(mixer, device_idx, i, value);
3352 		if (err)
3353 			return err;
3354 		err = add_single_ctl_with_resume(mixer, 0, snd_djm_controls_resume,
3355 						 &knew, NULL);
3356 		if (err)
3357 			return err;
3358 	}
3359 	return 0;
3360 }
3361 
3362 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
3363 {
3364 	int err = 0;
3365 
3366 	err = snd_usb_soundblaster_remote_init(mixer);
3367 	if (err < 0)
3368 		return err;
3369 
3370 	switch (mixer->chip->usb_id) {
3371 	/* Tascam US-16x08 */
3372 	case USB_ID(0x0644, 0x8047):
3373 		err = snd_us16x08_controls_create(mixer);
3374 		break;
3375 	case USB_ID(0x041e, 0x3020):
3376 	case USB_ID(0x041e, 0x3040):
3377 	case USB_ID(0x041e, 0x3042):
3378 	case USB_ID(0x041e, 0x30df):
3379 	case USB_ID(0x041e, 0x3048):
3380 		err = snd_audigy2nx_controls_create(mixer);
3381 		if (err < 0)
3382 			break;
3383 		snd_card_ro_proc_new(mixer->chip->card, "audigy2nx",
3384 				     mixer, snd_audigy2nx_proc_read);
3385 		break;
3386 
3387 	/* EMU0204 */
3388 	case USB_ID(0x041e, 0x3f19):
3389 		err = snd_emu0204_controls_create(mixer);
3390 		break;
3391 
3392 	case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
3393 	case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
3394 		err = snd_c400_create_mixer(mixer);
3395 		break;
3396 
3397 	case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
3398 	case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
3399 		err = snd_ftu_create_mixer(mixer);
3400 		break;
3401 
3402 	case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
3403 	case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
3404 	case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
3405 		err = snd_xonar_u1_controls_create(mixer);
3406 		break;
3407 
3408 	case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
3409 		err = snd_microii_controls_create(mixer);
3410 		break;
3411 
3412 	case USB_ID(0x0dba, 0x1000): /* Digidesign Mbox 1 */
3413 		err = snd_mbox1_controls_create(mixer);
3414 		break;
3415 
3416 	case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
3417 		err = snd_nativeinstruments_create_mixer(mixer,
3418 				snd_nativeinstruments_ta6_mixers,
3419 				ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
3420 		break;
3421 
3422 	case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
3423 		err = snd_nativeinstruments_create_mixer(mixer,
3424 				snd_nativeinstruments_ta10_mixers,
3425 				ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
3426 		break;
3427 
3428 	case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
3429 		/* detection is disabled in mixer_maps.c */
3430 		err = snd_create_std_mono_table(mixer, ebox44_table);
3431 		break;
3432 
3433 	case USB_ID(0x1235, 0x8012): /* Focusrite Scarlett 6i6 */
3434 	case USB_ID(0x1235, 0x8002): /* Focusrite Scarlett 8i6 */
3435 	case USB_ID(0x1235, 0x8004): /* Focusrite Scarlett 18i6 */
3436 	case USB_ID(0x1235, 0x8014): /* Focusrite Scarlett 18i8 */
3437 	case USB_ID(0x1235, 0x800c): /* Focusrite Scarlett 18i20 */
3438 		err = snd_scarlett_controls_create(mixer);
3439 		break;
3440 
3441 	case USB_ID(0x1235, 0x8203): /* Focusrite Scarlett 6i6 2nd Gen */
3442 	case USB_ID(0x1235, 0x8204): /* Focusrite Scarlett 18i8 2nd Gen */
3443 	case USB_ID(0x1235, 0x8201): /* Focusrite Scarlett 18i20 2nd Gen */
3444 	case USB_ID(0x1235, 0x8211): /* Focusrite Scarlett Solo 3rd Gen */
3445 	case USB_ID(0x1235, 0x8210): /* Focusrite Scarlett 2i2 3rd Gen */
3446 	case USB_ID(0x1235, 0x8212): /* Focusrite Scarlett 4i4 3rd Gen */
3447 	case USB_ID(0x1235, 0x8213): /* Focusrite Scarlett 8i6 3rd Gen */
3448 	case USB_ID(0x1235, 0x8214): /* Focusrite Scarlett 18i8 3rd Gen */
3449 	case USB_ID(0x1235, 0x8215): /* Focusrite Scarlett 18i20 3rd Gen */
3450 	case USB_ID(0x1235, 0x820c): /* Focusrite Clarett+ 8Pre */
3451 		err = snd_scarlett_gen2_init(mixer);
3452 		break;
3453 
3454 	case USB_ID(0x041e, 0x323b): /* Creative Sound Blaster E1 */
3455 		err = snd_soundblaster_e1_switch_create(mixer);
3456 		break;
3457 	case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
3458 		err = dell_dock_mixer_create(mixer);
3459 		if (err < 0)
3460 			break;
3461 		err = dell_dock_mixer_init(mixer);
3462 		break;
3463 
3464 	case USB_ID(0x2a39, 0x3fd2): /* RME ADI-2 Pro */
3465 	case USB_ID(0x2a39, 0x3fd3): /* RME ADI-2 DAC */
3466 	case USB_ID(0x2a39, 0x3fd4): /* RME */
3467 		err = snd_rme_controls_create(mixer);
3468 		break;
3469 
3470 	case USB_ID(0x194f, 0x010c): /* Presonus Studio 1810c */
3471 		err = snd_sc1810_init_mixer(mixer);
3472 		break;
3473 	case USB_ID(0x2a39, 0x3fb0): /* RME Babyface Pro FS */
3474 		err = snd_bbfpro_controls_create(mixer);
3475 		break;
3476 	case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */
3477 		err = snd_djm_controls_create(mixer, SND_DJM_250MK2_IDX);
3478 		break;
3479 	case USB_ID(0x2b73, 0x0013): /* Pioneer DJ DJM-450 */
3480 		err = snd_djm_controls_create(mixer, SND_DJM_450_IDX);
3481 		break;
3482 	case USB_ID(0x08e4, 0x017f): /* Pioneer DJ DJM-750 */
3483 		err = snd_djm_controls_create(mixer, SND_DJM_750_IDX);
3484 		break;
3485 	case USB_ID(0x2b73, 0x001b): /* Pioneer DJ DJM-750MK2 */
3486 		err = snd_djm_controls_create(mixer, SND_DJM_750MK2_IDX);
3487 		break;
3488 	case USB_ID(0x08e4, 0x0163): /* Pioneer DJ DJM-850 */
3489 		err = snd_djm_controls_create(mixer, SND_DJM_850_IDX);
3490 		break;
3491 	case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */
3492 		err = snd_djm_controls_create(mixer, SND_DJM_900NXS2_IDX);
3493 		break;
3494 	}
3495 
3496 	return err;
3497 }
3498 
3499 void snd_usb_mixer_resume_quirk(struct usb_mixer_interface *mixer)
3500 {
3501 	switch (mixer->chip->usb_id) {
3502 	case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
3503 		dell_dock_mixer_init(mixer);
3504 		break;
3505 	}
3506 }
3507 
3508 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
3509 				    int unitid)
3510 {
3511 	if (!mixer->rc_cfg)
3512 		return;
3513 	/* unit ids specific to Extigy/Audigy 2 NX: */
3514 	switch (unitid) {
3515 	case 0: /* remote control */
3516 		mixer->rc_urb->dev = mixer->chip->dev;
3517 		usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
3518 		break;
3519 	case 4: /* digital in jack */
3520 	case 7: /* line in jacks */
3521 	case 19: /* speaker out jacks */
3522 	case 20: /* headphones out jack */
3523 		break;
3524 	/* live24ext: 4 = line-in jack */
3525 	case 3:	/* hp-out jack (may actuate Mute) */
3526 		if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
3527 		    mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
3528 			snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
3529 		break;
3530 	default:
3531 		usb_audio_dbg(mixer->chip, "memory change in unknown unit %d\n", unitid);
3532 		break;
3533 	}
3534 }
3535 
3536 static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
3537 					 struct usb_mixer_elem_info *cval,
3538 					 struct snd_kcontrol *kctl)
3539 {
3540 	/* Approximation using 10 ranges based on output measurement on hw v1.2.
3541 	 * This seems close to the cubic mapping e.g. alsamixer uses. */
3542 	static const DECLARE_TLV_DB_RANGE(scale,
3543 		 0,  1, TLV_DB_MINMAX_ITEM(-5300, -4970),
3544 		 2,  5, TLV_DB_MINMAX_ITEM(-4710, -4160),
3545 		 6,  7, TLV_DB_MINMAX_ITEM(-3884, -3710),
3546 		 8, 14, TLV_DB_MINMAX_ITEM(-3443, -2560),
3547 		15, 16, TLV_DB_MINMAX_ITEM(-2475, -2324),
3548 		17, 19, TLV_DB_MINMAX_ITEM(-2228, -2031),
3549 		20, 26, TLV_DB_MINMAX_ITEM(-1910, -1393),
3550 		27, 31, TLV_DB_MINMAX_ITEM(-1322, -1032),
3551 		32, 40, TLV_DB_MINMAX_ITEM(-968, -490),
3552 		41, 50, TLV_DB_MINMAX_ITEM(-441, 0),
3553 	);
3554 
3555 	if (cval->min == 0 && cval->max == 50) {
3556 		usb_audio_info(mixer->chip, "applying DragonFly dB scale quirk (0-50 variant)\n");
3557 		kctl->tlv.p = scale;
3558 		kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
3559 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
3560 
3561 	} else if (cval->min == 0 && cval->max <= 1000) {
3562 		/* Some other clearly broken DragonFly variant.
3563 		 * At least a 0..53 variant (hw v1.0) exists.
3564 		 */
3565 		usb_audio_info(mixer->chip, "ignoring too narrow dB range on a DragonFly device");
3566 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
3567 	}
3568 }
3569 
3570 void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
3571 				  struct usb_mixer_elem_info *cval, int unitid,
3572 				  struct snd_kcontrol *kctl)
3573 {
3574 	switch (mixer->chip->usb_id) {
3575 	case USB_ID(0x21b4, 0x0081): /* AudioQuest DragonFly */
3576 		if (unitid == 7 && cval->control == UAC_FU_VOLUME)
3577 			snd_dragonfly_quirk_db_scale(mixer, cval, kctl);
3578 		break;
3579 	/* lowest playback value is muted on some devices */
3580 	case USB_ID(0x0d8c, 0x000c): /* C-Media */
3581 	case USB_ID(0x0d8c, 0x0014): /* C-Media */
3582 	case USB_ID(0x19f7, 0x0003): /* RODE NT-USB */
3583 		if (strstr(kctl->id.name, "Playback"))
3584 			cval->min_mute = 1;
3585 		break;
3586 	}
3587 }
3588 
3589