xref: /openbmc/linux/sound/usb/mixer_quirks.c (revision 547840bd)
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/hwdep.h>
28 #include <sound/info.h>
29 #include <sound/tlv.h>
30 
31 #include "usbaudio.h"
32 #include "mixer.h"
33 #include "mixer_quirks.h"
34 #include "mixer_scarlett.h"
35 #include "mixer_scarlett_gen2.h"
36 #include "mixer_us16x08.h"
37 #include "mixer_s1810c.h"
38 #include "helper.h"
39 
40 struct std_mono_table {
41 	unsigned int unitid, control, cmask;
42 	int val_type;
43 	const char *name;
44 	snd_kcontrol_tlv_rw_t *tlv_callback;
45 };
46 
47 /* This function allows for the creation of standard UAC controls.
48  * See the quirks for M-Audio FTUs or Ebox-44.
49  * If you don't want to set a TLV callback pass NULL.
50  *
51  * Since there doesn't seem to be a devices that needs a multichannel
52  * version, we keep it mono for simplicity.
53  */
54 static int snd_create_std_mono_ctl_offset(struct usb_mixer_interface *mixer,
55 				unsigned int unitid,
56 				unsigned int control,
57 				unsigned int cmask,
58 				int val_type,
59 				unsigned int idx_off,
60 				const char *name,
61 				snd_kcontrol_tlv_rw_t *tlv_callback)
62 {
63 	struct usb_mixer_elem_info *cval;
64 	struct snd_kcontrol *kctl;
65 
66 	cval = kzalloc(sizeof(*cval), GFP_KERNEL);
67 	if (!cval)
68 		return -ENOMEM;
69 
70 	snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
71 	cval->val_type = val_type;
72 	cval->channels = 1;
73 	cval->control = control;
74 	cval->cmask = cmask;
75 	cval->idx_off = idx_off;
76 
77 	/* get_min_max() is called only for integer volumes later,
78 	 * so provide a short-cut for booleans */
79 	cval->min = 0;
80 	cval->max = 1;
81 	cval->res = 0;
82 	cval->dBmin = 0;
83 	cval->dBmax = 0;
84 
85 	/* Create control */
86 	kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
87 	if (!kctl) {
88 		kfree(cval);
89 		return -ENOMEM;
90 	}
91 
92 	/* Set name */
93 	snprintf(kctl->id.name, sizeof(kctl->id.name), name);
94 	kctl->private_free = snd_usb_mixer_elem_free;
95 
96 	/* set TLV */
97 	if (tlv_callback) {
98 		kctl->tlv.c = tlv_callback;
99 		kctl->vd[0].access |=
100 			SNDRV_CTL_ELEM_ACCESS_TLV_READ |
101 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
102 	}
103 	/* Add control to mixer */
104 	return snd_usb_mixer_add_control(&cval->head, kctl);
105 }
106 
107 static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
108 				unsigned int unitid,
109 				unsigned int control,
110 				unsigned int cmask,
111 				int val_type,
112 				const char *name,
113 				snd_kcontrol_tlv_rw_t *tlv_callback)
114 {
115 	return snd_create_std_mono_ctl_offset(mixer, unitid, control, cmask,
116 		val_type, 0 /* Offset */, name, tlv_callback);
117 }
118 
119 /*
120  * Create a set of standard UAC controls from a table
121  */
122 static int snd_create_std_mono_table(struct usb_mixer_interface *mixer,
123 				     const struct std_mono_table *t)
124 {
125 	int err;
126 
127 	while (t->name != NULL) {
128 		err = snd_create_std_mono_ctl(mixer, t->unitid, t->control,
129 				t->cmask, t->val_type, t->name, t->tlv_callback);
130 		if (err < 0)
131 			return err;
132 		t++;
133 	}
134 
135 	return 0;
136 }
137 
138 static int add_single_ctl_with_resume(struct usb_mixer_interface *mixer,
139 				      int id,
140 				      usb_mixer_elem_resume_func_t resume,
141 				      const struct snd_kcontrol_new *knew,
142 				      struct usb_mixer_elem_list **listp)
143 {
144 	struct usb_mixer_elem_list *list;
145 	struct snd_kcontrol *kctl;
146 
147 	list = kzalloc(sizeof(*list), GFP_KERNEL);
148 	if (!list)
149 		return -ENOMEM;
150 	if (listp)
151 		*listp = list;
152 	list->mixer = mixer;
153 	list->id = id;
154 	list->resume = resume;
155 	kctl = snd_ctl_new1(knew, list);
156 	if (!kctl) {
157 		kfree(list);
158 		return -ENOMEM;
159 	}
160 	kctl->private_free = snd_usb_mixer_elem_free;
161 	return snd_usb_mixer_add_control(list, kctl);
162 }
163 
164 /*
165  * Sound Blaster remote control configuration
166  *
167  * format of remote control data:
168  * Extigy:       xx 00
169  * Audigy 2 NX:  06 80 xx 00 00 00
170  * Live! 24-bit: 06 80 xx yy 22 83
171  */
172 static const struct rc_config {
173 	u32 usb_id;
174 	u8  offset;
175 	u8  length;
176 	u8  packet_length;
177 	u8  min_packet_length; /* minimum accepted length of the URB result */
178 	u8  mute_mixer_id;
179 	u32 mute_code;
180 } rc_configs[] = {
181 	{ USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
182 	{ USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
183 	{ USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
184 	{ USB_ID(0x041e, 0x3042), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 */
185 	{ USB_ID(0x041e, 0x30df), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
186 	{ USB_ID(0x041e, 0x3237), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
187 	{ USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
188 };
189 
190 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
191 {
192 	struct usb_mixer_interface *mixer = urb->context;
193 	const struct rc_config *rc = mixer->rc_cfg;
194 	u32 code;
195 
196 	if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
197 		return;
198 
199 	code = mixer->rc_buffer[rc->offset];
200 	if (rc->length == 2)
201 		code |= mixer->rc_buffer[rc->offset + 1] << 8;
202 
203 	/* the Mute button actually changes the mixer control */
204 	if (code == rc->mute_code)
205 		snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
206 	mixer->rc_code = code;
207 	wmb();
208 	wake_up(&mixer->rc_waitq);
209 }
210 
211 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
212 				     long count, loff_t *offset)
213 {
214 	struct usb_mixer_interface *mixer = hw->private_data;
215 	int err;
216 	u32 rc_code;
217 
218 	if (count != 1 && count != 4)
219 		return -EINVAL;
220 	err = wait_event_interruptible(mixer->rc_waitq,
221 				       (rc_code = xchg(&mixer->rc_code, 0)) != 0);
222 	if (err == 0) {
223 		if (count == 1)
224 			err = put_user(rc_code, buf);
225 		else
226 			err = put_user(rc_code, (u32 __user *)buf);
227 	}
228 	return err < 0 ? err : count;
229 }
230 
231 static __poll_t snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
232 					    poll_table *wait)
233 {
234 	struct usb_mixer_interface *mixer = hw->private_data;
235 
236 	poll_wait(file, &mixer->rc_waitq, wait);
237 	return mixer->rc_code ? EPOLLIN | EPOLLRDNORM : 0;
238 }
239 
240 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
241 {
242 	struct snd_hwdep *hwdep;
243 	int err, len, i;
244 
245 	for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
246 		if (rc_configs[i].usb_id == mixer->chip->usb_id)
247 			break;
248 	if (i >= ARRAY_SIZE(rc_configs))
249 		return 0;
250 	mixer->rc_cfg = &rc_configs[i];
251 
252 	len = mixer->rc_cfg->packet_length;
253 
254 	init_waitqueue_head(&mixer->rc_waitq);
255 	err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
256 	if (err < 0)
257 		return err;
258 	snprintf(hwdep->name, sizeof(hwdep->name),
259 		 "%s remote control", mixer->chip->card->shortname);
260 	hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
261 	hwdep->private_data = mixer;
262 	hwdep->ops.read = snd_usb_sbrc_hwdep_read;
263 	hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
264 	hwdep->exclusive = 1;
265 
266 	mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
267 	if (!mixer->rc_urb)
268 		return -ENOMEM;
269 	mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
270 	if (!mixer->rc_setup_packet) {
271 		usb_free_urb(mixer->rc_urb);
272 		mixer->rc_urb = NULL;
273 		return -ENOMEM;
274 	}
275 	mixer->rc_setup_packet->bRequestType =
276 		USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
277 	mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
278 	mixer->rc_setup_packet->wValue = cpu_to_le16(0);
279 	mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
280 	mixer->rc_setup_packet->wLength = cpu_to_le16(len);
281 	usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
282 			     usb_rcvctrlpipe(mixer->chip->dev, 0),
283 			     (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
284 			     snd_usb_soundblaster_remote_complete, mixer);
285 	return 0;
286 }
287 
288 #define snd_audigy2nx_led_info		snd_ctl_boolean_mono_info
289 
290 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
291 {
292 	ucontrol->value.integer.value[0] = kcontrol->private_value >> 8;
293 	return 0;
294 }
295 
296 static int snd_audigy2nx_led_update(struct usb_mixer_interface *mixer,
297 				    int value, int index)
298 {
299 	struct snd_usb_audio *chip = mixer->chip;
300 	int err;
301 
302 	err = snd_usb_lock_shutdown(chip);
303 	if (err < 0)
304 		return err;
305 
306 	if (chip->usb_id == USB_ID(0x041e, 0x3042))
307 		err = snd_usb_ctl_msg(chip->dev,
308 			      usb_sndctrlpipe(chip->dev, 0), 0x24,
309 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
310 			      !value, 0, NULL, 0);
311 	/* USB X-Fi S51 Pro */
312 	if (chip->usb_id == USB_ID(0x041e, 0x30df))
313 		err = snd_usb_ctl_msg(chip->dev,
314 			      usb_sndctrlpipe(chip->dev, 0), 0x24,
315 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
316 			      !value, 0, NULL, 0);
317 	else
318 		err = snd_usb_ctl_msg(chip->dev,
319 			      usb_sndctrlpipe(chip->dev, 0), 0x24,
320 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
321 			      value, index + 2, NULL, 0);
322 	snd_usb_unlock_shutdown(chip);
323 	return err;
324 }
325 
326 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol,
327 				 struct snd_ctl_elem_value *ucontrol)
328 {
329 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
330 	struct usb_mixer_interface *mixer = list->mixer;
331 	int index = kcontrol->private_value & 0xff;
332 	unsigned int value = ucontrol->value.integer.value[0];
333 	int old_value = kcontrol->private_value >> 8;
334 	int err;
335 
336 	if (value > 1)
337 		return -EINVAL;
338 	if (value == old_value)
339 		return 0;
340 	kcontrol->private_value = (value << 8) | index;
341 	err = snd_audigy2nx_led_update(mixer, value, index);
342 	return err < 0 ? err : 1;
343 }
344 
345 static int snd_audigy2nx_led_resume(struct usb_mixer_elem_list *list)
346 {
347 	int priv_value = list->kctl->private_value;
348 
349 	return snd_audigy2nx_led_update(list->mixer, priv_value >> 8,
350 					priv_value & 0xff);
351 }
352 
353 /* name and private_value are set dynamically */
354 static const struct snd_kcontrol_new snd_audigy2nx_control = {
355 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
356 	.info = snd_audigy2nx_led_info,
357 	.get = snd_audigy2nx_led_get,
358 	.put = snd_audigy2nx_led_put,
359 };
360 
361 static const char * const snd_audigy2nx_led_names[] = {
362 	"CMSS LED Switch",
363 	"Power LED Switch",
364 	"Dolby Digital LED Switch",
365 };
366 
367 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
368 {
369 	int i, err;
370 
371 	for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_led_names); ++i) {
372 		struct snd_kcontrol_new knew;
373 
374 		/* USB X-Fi S51 doesn't have a CMSS LED */
375 		if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
376 			continue;
377 		/* USB X-Fi S51 Pro doesn't have one either */
378 		if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
379 			continue;
380 		if (i > 1 && /* Live24ext has 2 LEDs only */
381 			(mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
382 			 mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
383 			 mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
384 			 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
385 			break;
386 
387 		knew = snd_audigy2nx_control;
388 		knew.name = snd_audigy2nx_led_names[i];
389 		knew.private_value = (1 << 8) | i; /* LED on as default */
390 		err = add_single_ctl_with_resume(mixer, 0,
391 						 snd_audigy2nx_led_resume,
392 						 &knew, NULL);
393 		if (err < 0)
394 			return err;
395 	}
396 	return 0;
397 }
398 
399 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
400 				    struct snd_info_buffer *buffer)
401 {
402 	static const struct sb_jack {
403 		int unitid;
404 		const char *name;
405 	}  jacks_audigy2nx[] = {
406 		{4,  "dig in "},
407 		{7,  "line in"},
408 		{19, "spk out"},
409 		{20, "hph out"},
410 		{-1, NULL}
411 	}, jacks_live24ext[] = {
412 		{4,  "line in"}, /* &1=Line, &2=Mic*/
413 		{3,  "hph out"}, /* headphones */
414 		{0,  "RC     "}, /* last command, 6 bytes see rc_config above */
415 		{-1, NULL}
416 	};
417 	const struct sb_jack *jacks;
418 	struct usb_mixer_interface *mixer = entry->private_data;
419 	int i, err;
420 	u8 buf[3];
421 
422 	snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
423 	if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
424 		jacks = jacks_audigy2nx;
425 	else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
426 		 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
427 		jacks = jacks_live24ext;
428 	else
429 		return;
430 
431 	for (i = 0; jacks[i].name; ++i) {
432 		snd_iprintf(buffer, "%s: ", jacks[i].name);
433 		err = snd_usb_lock_shutdown(mixer->chip);
434 		if (err < 0)
435 			return;
436 		err = snd_usb_ctl_msg(mixer->chip->dev,
437 				      usb_rcvctrlpipe(mixer->chip->dev, 0),
438 				      UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
439 				      USB_RECIP_INTERFACE, 0,
440 				      jacks[i].unitid << 8, buf, 3);
441 		snd_usb_unlock_shutdown(mixer->chip);
442 		if (err == 3 && (buf[0] == 3 || buf[0] == 6))
443 			snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
444 		else
445 			snd_iprintf(buffer, "?\n");
446 	}
447 }
448 
449 /* EMU0204 */
450 static int snd_emu0204_ch_switch_info(struct snd_kcontrol *kcontrol,
451 				      struct snd_ctl_elem_info *uinfo)
452 {
453 	static const char * const texts[2] = {"1/2", "3/4"};
454 
455 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
456 }
457 
458 static int snd_emu0204_ch_switch_get(struct snd_kcontrol *kcontrol,
459 				     struct snd_ctl_elem_value *ucontrol)
460 {
461 	ucontrol->value.enumerated.item[0] = kcontrol->private_value;
462 	return 0;
463 }
464 
465 static int snd_emu0204_ch_switch_update(struct usb_mixer_interface *mixer,
466 					int value)
467 {
468 	struct snd_usb_audio *chip = mixer->chip;
469 	int err;
470 	unsigned char buf[2];
471 
472 	err = snd_usb_lock_shutdown(chip);
473 	if (err < 0)
474 		return err;
475 
476 	buf[0] = 0x01;
477 	buf[1] = value ? 0x02 : 0x01;
478 	err = snd_usb_ctl_msg(chip->dev,
479 		      usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
480 		      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
481 		      0x0400, 0x0e00, buf, 2);
482 	snd_usb_unlock_shutdown(chip);
483 	return err;
484 }
485 
486 static int snd_emu0204_ch_switch_put(struct snd_kcontrol *kcontrol,
487 				     struct snd_ctl_elem_value *ucontrol)
488 {
489 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
490 	struct usb_mixer_interface *mixer = list->mixer;
491 	unsigned int value = ucontrol->value.enumerated.item[0];
492 	int err;
493 
494 	if (value > 1)
495 		return -EINVAL;
496 
497 	if (value == kcontrol->private_value)
498 		return 0;
499 
500 	kcontrol->private_value = value;
501 	err = snd_emu0204_ch_switch_update(mixer, value);
502 	return err < 0 ? err : 1;
503 }
504 
505 static int snd_emu0204_ch_switch_resume(struct usb_mixer_elem_list *list)
506 {
507 	return snd_emu0204_ch_switch_update(list->mixer,
508 					    list->kctl->private_value);
509 }
510 
511 static const struct snd_kcontrol_new snd_emu0204_control = {
512 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
513 	.name = "Front Jack Channels",
514 	.info = snd_emu0204_ch_switch_info,
515 	.get = snd_emu0204_ch_switch_get,
516 	.put = snd_emu0204_ch_switch_put,
517 	.private_value = 0,
518 };
519 
520 static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
521 {
522 	return add_single_ctl_with_resume(mixer, 0,
523 					  snd_emu0204_ch_switch_resume,
524 					  &snd_emu0204_control, NULL);
525 }
526 
527 /* ASUS Xonar U1 / U3 controls */
528 
529 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
530 				   struct snd_ctl_elem_value *ucontrol)
531 {
532 	ucontrol->value.integer.value[0] = !!(kcontrol->private_value & 0x02);
533 	return 0;
534 }
535 
536 static int snd_xonar_u1_switch_update(struct usb_mixer_interface *mixer,
537 				      unsigned char status)
538 {
539 	struct snd_usb_audio *chip = mixer->chip;
540 	int err;
541 
542 	err = snd_usb_lock_shutdown(chip);
543 	if (err < 0)
544 		return err;
545 	err = snd_usb_ctl_msg(chip->dev,
546 			      usb_sndctrlpipe(chip->dev, 0), 0x08,
547 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
548 			      50, 0, &status, 1);
549 	snd_usb_unlock_shutdown(chip);
550 	return err;
551 }
552 
553 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
554 				   struct snd_ctl_elem_value *ucontrol)
555 {
556 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
557 	u8 old_status, new_status;
558 	int err;
559 
560 	old_status = kcontrol->private_value;
561 	if (ucontrol->value.integer.value[0])
562 		new_status = old_status | 0x02;
563 	else
564 		new_status = old_status & ~0x02;
565 	if (new_status == old_status)
566 		return 0;
567 
568 	kcontrol->private_value = new_status;
569 	err = snd_xonar_u1_switch_update(list->mixer, new_status);
570 	return err < 0 ? err : 1;
571 }
572 
573 static int snd_xonar_u1_switch_resume(struct usb_mixer_elem_list *list)
574 {
575 	return snd_xonar_u1_switch_update(list->mixer,
576 					  list->kctl->private_value);
577 }
578 
579 static const struct snd_kcontrol_new snd_xonar_u1_output_switch = {
580 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
581 	.name = "Digital Playback Switch",
582 	.info = snd_ctl_boolean_mono_info,
583 	.get = snd_xonar_u1_switch_get,
584 	.put = snd_xonar_u1_switch_put,
585 	.private_value = 0x05,
586 };
587 
588 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
589 {
590 	return add_single_ctl_with_resume(mixer, 0,
591 					  snd_xonar_u1_switch_resume,
592 					  &snd_xonar_u1_output_switch, NULL);
593 }
594 
595 /* Digidesign Mbox 1 clock source switch (internal/spdif) */
596 
597 static int snd_mbox1_switch_get(struct snd_kcontrol *kctl,
598 				struct snd_ctl_elem_value *ucontrol)
599 {
600 	ucontrol->value.enumerated.item[0] = kctl->private_value;
601 	return 0;
602 }
603 
604 static int snd_mbox1_switch_update(struct usb_mixer_interface *mixer, int val)
605 {
606 	struct snd_usb_audio *chip = mixer->chip;
607 	int err;
608 	unsigned char buff[3];
609 
610 	err = snd_usb_lock_shutdown(chip);
611 	if (err < 0)
612 		return err;
613 
614 	/* Prepare for magic command to toggle clock source */
615 	err = snd_usb_ctl_msg(chip->dev,
616 				usb_rcvctrlpipe(chip->dev, 0), 0x81,
617 				USB_DIR_IN |
618 				USB_TYPE_CLASS |
619 				USB_RECIP_INTERFACE, 0x00, 0x500, buff, 1);
620 	if (err < 0)
621 		goto err;
622 	err = snd_usb_ctl_msg(chip->dev,
623 				usb_rcvctrlpipe(chip->dev, 0), 0x81,
624 				USB_DIR_IN |
625 				USB_TYPE_CLASS |
626 				USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
627 	if (err < 0)
628 		goto err;
629 
630 	/* 2 possibilities:	Internal    -> send sample rate
631 	 *			S/PDIF sync -> send zeroes
632 	 * NB: Sample rate locked to 48kHz on purpose to
633 	 *     prevent user from resetting the sample rate
634 	 *     while S/PDIF sync is enabled and confusing
635 	 *     this configuration.
636 	 */
637 	if (val == 0) {
638 		buff[0] = 0x80;
639 		buff[1] = 0xbb;
640 		buff[2] = 0x00;
641 	} else {
642 		buff[0] = buff[1] = buff[2] = 0x00;
643 	}
644 
645 	/* Send the magic command to toggle the clock source */
646 	err = snd_usb_ctl_msg(chip->dev,
647 				usb_sndctrlpipe(chip->dev, 0), 0x1,
648 				USB_TYPE_CLASS |
649 				USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
650 	if (err < 0)
651 		goto err;
652 	err = snd_usb_ctl_msg(chip->dev,
653 				usb_rcvctrlpipe(chip->dev, 0), 0x81,
654 				USB_DIR_IN |
655 				USB_TYPE_CLASS |
656 				USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
657 	if (err < 0)
658 		goto err;
659 	err = snd_usb_ctl_msg(chip->dev,
660 				usb_rcvctrlpipe(chip->dev, 0), 0x81,
661 				USB_DIR_IN |
662 				USB_TYPE_CLASS |
663 				USB_RECIP_ENDPOINT, 0x100, 0x2, buff, 3);
664 	if (err < 0)
665 		goto err;
666 
667 err:
668 	snd_usb_unlock_shutdown(chip);
669 	return err;
670 }
671 
672 static int snd_mbox1_switch_put(struct snd_kcontrol *kctl,
673 				struct snd_ctl_elem_value *ucontrol)
674 {
675 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
676 	struct usb_mixer_interface *mixer = list->mixer;
677 	int err;
678 	bool cur_val, new_val;
679 
680 	cur_val = kctl->private_value;
681 	new_val = ucontrol->value.enumerated.item[0];
682 	if (cur_val == new_val)
683 		return 0;
684 
685 	kctl->private_value = new_val;
686 	err = snd_mbox1_switch_update(mixer, new_val);
687 	return err < 0 ? err : 1;
688 }
689 
690 static int snd_mbox1_switch_info(struct snd_kcontrol *kcontrol,
691 				 struct snd_ctl_elem_info *uinfo)
692 {
693 	static const char *const texts[2] = {
694 		"Internal",
695 		"S/PDIF"
696 	};
697 
698 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
699 }
700 
701 static int snd_mbox1_switch_resume(struct usb_mixer_elem_list *list)
702 {
703 	return snd_mbox1_switch_update(list->mixer, list->kctl->private_value);
704 }
705 
706 static const struct snd_kcontrol_new snd_mbox1_switch = {
707 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
708 	.name = "Clock Source",
709 	.index = 0,
710 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
711 	.info = snd_mbox1_switch_info,
712 	.get = snd_mbox1_switch_get,
713 	.put = snd_mbox1_switch_put,
714 	.private_value = 0
715 };
716 
717 static int snd_mbox1_create_sync_switch(struct usb_mixer_interface *mixer)
718 {
719 	return add_single_ctl_with_resume(mixer, 0,
720 					  snd_mbox1_switch_resume,
721 					  &snd_mbox1_switch, NULL);
722 }
723 
724 /* Native Instruments device quirks */
725 
726 #define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
727 
728 static int snd_ni_control_init_val(struct usb_mixer_interface *mixer,
729 				   struct snd_kcontrol *kctl)
730 {
731 	struct usb_device *dev = mixer->chip->dev;
732 	unsigned int pval = kctl->private_value;
733 	u8 value;
734 	int err;
735 
736 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
737 			      (pval >> 16) & 0xff,
738 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
739 			      0, pval & 0xffff, &value, 1);
740 	if (err < 0) {
741 		dev_err(&dev->dev,
742 			"unable to issue vendor read request (ret = %d)", err);
743 		return err;
744 	}
745 
746 	kctl->private_value |= ((unsigned int)value << 24);
747 	return 0;
748 }
749 
750 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
751 					     struct snd_ctl_elem_value *ucontrol)
752 {
753 	ucontrol->value.integer.value[0] = kcontrol->private_value >> 24;
754 	return 0;
755 }
756 
757 static int snd_ni_update_cur_val(struct usb_mixer_elem_list *list)
758 {
759 	struct snd_usb_audio *chip = list->mixer->chip;
760 	unsigned int pval = list->kctl->private_value;
761 	int err;
762 
763 	err = snd_usb_lock_shutdown(chip);
764 	if (err < 0)
765 		return err;
766 	err = usb_control_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
767 			      (pval >> 16) & 0xff,
768 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
769 			      pval >> 24, pval & 0xffff, NULL, 0, 1000);
770 	snd_usb_unlock_shutdown(chip);
771 	return err;
772 }
773 
774 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
775 					     struct snd_ctl_elem_value *ucontrol)
776 {
777 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
778 	u8 oldval = (kcontrol->private_value >> 24) & 0xff;
779 	u8 newval = ucontrol->value.integer.value[0];
780 	int err;
781 
782 	if (oldval == newval)
783 		return 0;
784 
785 	kcontrol->private_value &= ~(0xff << 24);
786 	kcontrol->private_value |= (unsigned int)newval << 24;
787 	err = snd_ni_update_cur_val(list);
788 	return err < 0 ? err : 1;
789 }
790 
791 static const struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
792 	{
793 		.name = "Direct Thru Channel A",
794 		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
795 	},
796 	{
797 		.name = "Direct Thru Channel B",
798 		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
799 	},
800 	{
801 		.name = "Phono Input Channel A",
802 		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
803 	},
804 	{
805 		.name = "Phono Input Channel B",
806 		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
807 	},
808 };
809 
810 static const struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
811 	{
812 		.name = "Direct Thru Channel A",
813 		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
814 	},
815 	{
816 		.name = "Direct Thru Channel B",
817 		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
818 	},
819 	{
820 		.name = "Direct Thru Channel C",
821 		.private_value = _MAKE_NI_CONTROL(0x01, 0x07),
822 	},
823 	{
824 		.name = "Direct Thru Channel D",
825 		.private_value = _MAKE_NI_CONTROL(0x01, 0x09),
826 	},
827 	{
828 		.name = "Phono Input Channel A",
829 		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
830 	},
831 	{
832 		.name = "Phono Input Channel B",
833 		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
834 	},
835 	{
836 		.name = "Phono Input Channel C",
837 		.private_value = _MAKE_NI_CONTROL(0x02, 0x07),
838 	},
839 	{
840 		.name = "Phono Input Channel D",
841 		.private_value = _MAKE_NI_CONTROL(0x02, 0x09),
842 	},
843 };
844 
845 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
846 					      const struct snd_kcontrol_new *kc,
847 					      unsigned int count)
848 {
849 	int i, err = 0;
850 	struct snd_kcontrol_new template = {
851 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
852 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
853 		.get = snd_nativeinstruments_control_get,
854 		.put = snd_nativeinstruments_control_put,
855 		.info = snd_ctl_boolean_mono_info,
856 	};
857 
858 	for (i = 0; i < count; i++) {
859 		struct usb_mixer_elem_list *list;
860 
861 		template.name = kc[i].name;
862 		template.private_value = kc[i].private_value;
863 
864 		err = add_single_ctl_with_resume(mixer, 0,
865 						 snd_ni_update_cur_val,
866 						 &template, &list);
867 		if (err < 0)
868 			break;
869 		snd_ni_control_init_val(mixer, list->kctl);
870 	}
871 
872 	return err;
873 }
874 
875 /* M-Audio FastTrack Ultra quirks */
876 /* FTU Effect switch (also used by C400/C600) */
877 static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
878 					struct snd_ctl_elem_info *uinfo)
879 {
880 	static const char *const texts[8] = {
881 		"Room 1", "Room 2", "Room 3", "Hall 1",
882 		"Hall 2", "Plate", "Delay", "Echo"
883 	};
884 
885 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
886 }
887 
888 static int snd_ftu_eff_switch_init(struct usb_mixer_interface *mixer,
889 				   struct snd_kcontrol *kctl)
890 {
891 	struct usb_device *dev = mixer->chip->dev;
892 	unsigned int pval = kctl->private_value;
893 	int err;
894 	unsigned char value[2];
895 
896 	value[0] = 0x00;
897 	value[1] = 0x00;
898 
899 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
900 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
901 			      pval & 0xff00,
902 			      snd_usb_ctrl_intf(mixer->chip) | ((pval & 0xff) << 8),
903 			      value, 2);
904 	if (err < 0)
905 		return err;
906 
907 	kctl->private_value |= (unsigned int)value[0] << 24;
908 	return 0;
909 }
910 
911 static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
912 					struct snd_ctl_elem_value *ucontrol)
913 {
914 	ucontrol->value.enumerated.item[0] = kctl->private_value >> 24;
915 	return 0;
916 }
917 
918 static int snd_ftu_eff_switch_update(struct usb_mixer_elem_list *list)
919 {
920 	struct snd_usb_audio *chip = list->mixer->chip;
921 	unsigned int pval = list->kctl->private_value;
922 	unsigned char value[2];
923 	int err;
924 
925 	value[0] = pval >> 24;
926 	value[1] = 0;
927 
928 	err = snd_usb_lock_shutdown(chip);
929 	if (err < 0)
930 		return err;
931 	err = snd_usb_ctl_msg(chip->dev,
932 			      usb_sndctrlpipe(chip->dev, 0),
933 			      UAC_SET_CUR,
934 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
935 			      pval & 0xff00,
936 			      snd_usb_ctrl_intf(chip) | ((pval & 0xff) << 8),
937 			      value, 2);
938 	snd_usb_unlock_shutdown(chip);
939 	return err;
940 }
941 
942 static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
943 					struct snd_ctl_elem_value *ucontrol)
944 {
945 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
946 	unsigned int pval = list->kctl->private_value;
947 	int cur_val, err, new_val;
948 
949 	cur_val = pval >> 24;
950 	new_val = ucontrol->value.enumerated.item[0];
951 	if (cur_val == new_val)
952 		return 0;
953 
954 	kctl->private_value &= ~(0xff << 24);
955 	kctl->private_value |= new_val << 24;
956 	err = snd_ftu_eff_switch_update(list);
957 	return err < 0 ? err : 1;
958 }
959 
960 static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer,
961 	int validx, int bUnitID)
962 {
963 	static struct snd_kcontrol_new template = {
964 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
965 		.name = "Effect Program Switch",
966 		.index = 0,
967 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
968 		.info = snd_ftu_eff_switch_info,
969 		.get = snd_ftu_eff_switch_get,
970 		.put = snd_ftu_eff_switch_put
971 	};
972 	struct usb_mixer_elem_list *list;
973 	int err;
974 
975 	err = add_single_ctl_with_resume(mixer, bUnitID,
976 					 snd_ftu_eff_switch_update,
977 					 &template, &list);
978 	if (err < 0)
979 		return err;
980 	list->kctl->private_value = (validx << 8) | bUnitID;
981 	snd_ftu_eff_switch_init(mixer, list->kctl);
982 	return 0;
983 }
984 
985 /* Create volume controls for FTU devices*/
986 static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
987 {
988 	char name[64];
989 	unsigned int control, cmask;
990 	int in, out, err;
991 
992 	const unsigned int id = 5;
993 	const int val_type = USB_MIXER_S16;
994 
995 	for (out = 0; out < 8; out++) {
996 		control = out + 1;
997 		for (in = 0; in < 8; in++) {
998 			cmask = 1 << in;
999 			snprintf(name, sizeof(name),
1000 				"AIn%d - Out%d Capture Volume",
1001 				in  + 1, out + 1);
1002 			err = snd_create_std_mono_ctl(mixer, id, control,
1003 							cmask, val_type, name,
1004 							&snd_usb_mixer_vol_tlv);
1005 			if (err < 0)
1006 				return err;
1007 		}
1008 		for (in = 8; in < 16; in++) {
1009 			cmask = 1 << in;
1010 			snprintf(name, sizeof(name),
1011 				"DIn%d - Out%d Playback Volume",
1012 				in - 7, out + 1);
1013 			err = snd_create_std_mono_ctl(mixer, id, control,
1014 							cmask, val_type, name,
1015 							&snd_usb_mixer_vol_tlv);
1016 			if (err < 0)
1017 				return err;
1018 		}
1019 	}
1020 
1021 	return 0;
1022 }
1023 
1024 /* This control needs a volume quirk, see mixer.c */
1025 static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1026 {
1027 	static const char name[] = "Effect Volume";
1028 	const unsigned int id = 6;
1029 	const int val_type = USB_MIXER_U8;
1030 	const unsigned int control = 2;
1031 	const unsigned int cmask = 0;
1032 
1033 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1034 					name, snd_usb_mixer_vol_tlv);
1035 }
1036 
1037 /* This control needs a volume quirk, see mixer.c */
1038 static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1039 {
1040 	static const char name[] = "Effect Duration";
1041 	const unsigned int id = 6;
1042 	const int val_type = USB_MIXER_S16;
1043 	const unsigned int control = 3;
1044 	const unsigned int cmask = 0;
1045 
1046 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1047 					name, snd_usb_mixer_vol_tlv);
1048 }
1049 
1050 /* This control needs a volume quirk, see mixer.c */
1051 static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1052 {
1053 	static const char name[] = "Effect Feedback Volume";
1054 	const unsigned int id = 6;
1055 	const int val_type = USB_MIXER_U8;
1056 	const unsigned int control = 4;
1057 	const unsigned int cmask = 0;
1058 
1059 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1060 					name, NULL);
1061 }
1062 
1063 static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
1064 {
1065 	unsigned int cmask;
1066 	int err, ch;
1067 	char name[48];
1068 
1069 	const unsigned int id = 7;
1070 	const int val_type = USB_MIXER_S16;
1071 	const unsigned int control = 7;
1072 
1073 	for (ch = 0; ch < 4; ++ch) {
1074 		cmask = 1 << ch;
1075 		snprintf(name, sizeof(name),
1076 			"Effect Return %d Volume", ch + 1);
1077 		err = snd_create_std_mono_ctl(mixer, id, control,
1078 						cmask, val_type, name,
1079 						snd_usb_mixer_vol_tlv);
1080 		if (err < 0)
1081 			return err;
1082 	}
1083 
1084 	return 0;
1085 }
1086 
1087 static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
1088 {
1089 	unsigned int  cmask;
1090 	int err, ch;
1091 	char name[48];
1092 
1093 	const unsigned int id = 5;
1094 	const int val_type = USB_MIXER_S16;
1095 	const unsigned int control = 9;
1096 
1097 	for (ch = 0; ch < 8; ++ch) {
1098 		cmask = 1 << ch;
1099 		snprintf(name, sizeof(name),
1100 			"Effect Send AIn%d Volume", ch + 1);
1101 		err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1102 						val_type, name,
1103 						snd_usb_mixer_vol_tlv);
1104 		if (err < 0)
1105 			return err;
1106 	}
1107 	for (ch = 8; ch < 16; ++ch) {
1108 		cmask = 1 << ch;
1109 		snprintf(name, sizeof(name),
1110 			"Effect Send DIn%d Volume", ch - 7);
1111 		err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1112 						val_type, name,
1113 						snd_usb_mixer_vol_tlv);
1114 		if (err < 0)
1115 			return err;
1116 	}
1117 	return 0;
1118 }
1119 
1120 static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
1121 {
1122 	int err;
1123 
1124 	err = snd_ftu_create_volume_ctls(mixer);
1125 	if (err < 0)
1126 		return err;
1127 
1128 	err = snd_ftu_create_effect_switch(mixer, 1, 6);
1129 	if (err < 0)
1130 		return err;
1131 
1132 	err = snd_ftu_create_effect_volume_ctl(mixer);
1133 	if (err < 0)
1134 		return err;
1135 
1136 	err = snd_ftu_create_effect_duration_ctl(mixer);
1137 	if (err < 0)
1138 		return err;
1139 
1140 	err = snd_ftu_create_effect_feedback_ctl(mixer);
1141 	if (err < 0)
1142 		return err;
1143 
1144 	err = snd_ftu_create_effect_return_ctls(mixer);
1145 	if (err < 0)
1146 		return err;
1147 
1148 	err = snd_ftu_create_effect_send_ctls(mixer);
1149 	if (err < 0)
1150 		return err;
1151 
1152 	return 0;
1153 }
1154 
1155 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
1156 			       unsigned char samplerate_id)
1157 {
1158 	struct usb_mixer_interface *mixer;
1159 	struct usb_mixer_elem_info *cval;
1160 	int unitid = 12; /* SampleRate ExtensionUnit ID */
1161 
1162 	list_for_each_entry(mixer, &chip->mixer_list, list) {
1163 		if (mixer->id_elems[unitid]) {
1164 			cval = mixer_elem_list_to_info(mixer->id_elems[unitid]);
1165 			snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1166 						    cval->control << 8,
1167 						    samplerate_id);
1168 			snd_usb_mixer_notify_id(mixer, unitid);
1169 			break;
1170 		}
1171 	}
1172 }
1173 
1174 /* M-Audio Fast Track C400/C600 */
1175 /* C400/C600 volume controls, this control needs a volume quirk, see mixer.c */
1176 static int snd_c400_create_vol_ctls(struct usb_mixer_interface *mixer)
1177 {
1178 	char name[64];
1179 	unsigned int cmask, offset;
1180 	int out, chan, err;
1181 	int num_outs = 0;
1182 	int num_ins = 0;
1183 
1184 	const unsigned int id = 0x40;
1185 	const int val_type = USB_MIXER_S16;
1186 	const int control = 1;
1187 
1188 	switch (mixer->chip->usb_id) {
1189 	case USB_ID(0x0763, 0x2030):
1190 		num_outs = 6;
1191 		num_ins = 4;
1192 		break;
1193 	case USB_ID(0x0763, 0x2031):
1194 		num_outs = 8;
1195 		num_ins = 6;
1196 		break;
1197 	}
1198 
1199 	for (chan = 0; chan < num_outs + num_ins; chan++) {
1200 		for (out = 0; out < num_outs; out++) {
1201 			if (chan < num_outs) {
1202 				snprintf(name, sizeof(name),
1203 					"PCM%d-Out%d Playback Volume",
1204 					chan + 1, out + 1);
1205 			} else {
1206 				snprintf(name, sizeof(name),
1207 					"In%d-Out%d Playback Volume",
1208 					chan - num_outs + 1, out + 1);
1209 			}
1210 
1211 			cmask = (out == 0) ? 0 : 1 << (out - 1);
1212 			offset = chan * num_outs;
1213 			err = snd_create_std_mono_ctl_offset(mixer, id, control,
1214 						cmask, val_type, offset, name,
1215 						&snd_usb_mixer_vol_tlv);
1216 			if (err < 0)
1217 				return err;
1218 		}
1219 	}
1220 
1221 	return 0;
1222 }
1223 
1224 /* This control needs a volume quirk, see mixer.c */
1225 static int snd_c400_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1226 {
1227 	static const char name[] = "Effect Volume";
1228 	const unsigned int id = 0x43;
1229 	const int val_type = USB_MIXER_U8;
1230 	const unsigned int control = 3;
1231 	const unsigned int cmask = 0;
1232 
1233 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1234 					name, snd_usb_mixer_vol_tlv);
1235 }
1236 
1237 /* This control needs a volume quirk, see mixer.c */
1238 static int snd_c400_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1239 {
1240 	static const char name[] = "Effect Duration";
1241 	const unsigned int id = 0x43;
1242 	const int val_type = USB_MIXER_S16;
1243 	const unsigned int control = 4;
1244 	const unsigned int cmask = 0;
1245 
1246 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1247 					name, snd_usb_mixer_vol_tlv);
1248 }
1249 
1250 /* This control needs a volume quirk, see mixer.c */
1251 static int snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1252 {
1253 	static const char name[] = "Effect Feedback Volume";
1254 	const unsigned int id = 0x43;
1255 	const int val_type = USB_MIXER_U8;
1256 	const unsigned int control = 5;
1257 	const unsigned int cmask = 0;
1258 
1259 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1260 					name, NULL);
1261 }
1262 
1263 static int snd_c400_create_effect_vol_ctls(struct usb_mixer_interface *mixer)
1264 {
1265 	char name[64];
1266 	unsigned int cmask;
1267 	int chan, err;
1268 	int num_outs = 0;
1269 	int num_ins = 0;
1270 
1271 	const unsigned int id = 0x42;
1272 	const int val_type = USB_MIXER_S16;
1273 	const int control = 1;
1274 
1275 	switch (mixer->chip->usb_id) {
1276 	case USB_ID(0x0763, 0x2030):
1277 		num_outs = 6;
1278 		num_ins = 4;
1279 		break;
1280 	case USB_ID(0x0763, 0x2031):
1281 		num_outs = 8;
1282 		num_ins = 6;
1283 		break;
1284 	}
1285 
1286 	for (chan = 0; chan < num_outs + num_ins; chan++) {
1287 		if (chan < num_outs) {
1288 			snprintf(name, sizeof(name),
1289 				"Effect Send DOut%d",
1290 				chan + 1);
1291 		} else {
1292 			snprintf(name, sizeof(name),
1293 				"Effect Send AIn%d",
1294 				chan - num_outs + 1);
1295 		}
1296 
1297 		cmask = (chan == 0) ? 0 : 1 << (chan - 1);
1298 		err = snd_create_std_mono_ctl(mixer, id, control,
1299 						cmask, val_type, name,
1300 						&snd_usb_mixer_vol_tlv);
1301 		if (err < 0)
1302 			return err;
1303 	}
1304 
1305 	return 0;
1306 }
1307 
1308 static int snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface *mixer)
1309 {
1310 	char name[64];
1311 	unsigned int cmask;
1312 	int chan, err;
1313 	int num_outs = 0;
1314 	int offset = 0;
1315 
1316 	const unsigned int id = 0x40;
1317 	const int val_type = USB_MIXER_S16;
1318 	const int control = 1;
1319 
1320 	switch (mixer->chip->usb_id) {
1321 	case USB_ID(0x0763, 0x2030):
1322 		num_outs = 6;
1323 		offset = 0x3c;
1324 		/* { 0x3c, 0x43, 0x3e, 0x45, 0x40, 0x47 } */
1325 		break;
1326 	case USB_ID(0x0763, 0x2031):
1327 		num_outs = 8;
1328 		offset = 0x70;
1329 		/* { 0x70, 0x79, 0x72, 0x7b, 0x74, 0x7d, 0x76, 0x7f } */
1330 		break;
1331 	}
1332 
1333 	for (chan = 0; chan < num_outs; chan++) {
1334 		snprintf(name, sizeof(name),
1335 			"Effect Return %d",
1336 			chan + 1);
1337 
1338 		cmask = (chan == 0) ? 0 :
1339 			1 << (chan + (chan % 2) * num_outs - 1);
1340 		err = snd_create_std_mono_ctl_offset(mixer, id, control,
1341 						cmask, val_type, offset, name,
1342 						&snd_usb_mixer_vol_tlv);
1343 		if (err < 0)
1344 			return err;
1345 	}
1346 
1347 	return 0;
1348 }
1349 
1350 static int snd_c400_create_mixer(struct usb_mixer_interface *mixer)
1351 {
1352 	int err;
1353 
1354 	err = snd_c400_create_vol_ctls(mixer);
1355 	if (err < 0)
1356 		return err;
1357 
1358 	err = snd_c400_create_effect_vol_ctls(mixer);
1359 	if (err < 0)
1360 		return err;
1361 
1362 	err = snd_c400_create_effect_ret_vol_ctls(mixer);
1363 	if (err < 0)
1364 		return err;
1365 
1366 	err = snd_ftu_create_effect_switch(mixer, 2, 0x43);
1367 	if (err < 0)
1368 		return err;
1369 
1370 	err = snd_c400_create_effect_volume_ctl(mixer);
1371 	if (err < 0)
1372 		return err;
1373 
1374 	err = snd_c400_create_effect_duration_ctl(mixer);
1375 	if (err < 0)
1376 		return err;
1377 
1378 	err = snd_c400_create_effect_feedback_ctl(mixer);
1379 	if (err < 0)
1380 		return err;
1381 
1382 	return 0;
1383 }
1384 
1385 /*
1386  * The mixer units for Ebox-44 are corrupt, and even where they
1387  * are valid they presents mono controls as L and R channels of
1388  * stereo. So we provide a good mixer here.
1389  */
1390 static const struct std_mono_table ebox44_table[] = {
1391 	{
1392 		.unitid = 4,
1393 		.control = 1,
1394 		.cmask = 0x0,
1395 		.val_type = USB_MIXER_INV_BOOLEAN,
1396 		.name = "Headphone Playback Switch"
1397 	},
1398 	{
1399 		.unitid = 4,
1400 		.control = 2,
1401 		.cmask = 0x1,
1402 		.val_type = USB_MIXER_S16,
1403 		.name = "Headphone A Mix Playback Volume"
1404 	},
1405 	{
1406 		.unitid = 4,
1407 		.control = 2,
1408 		.cmask = 0x2,
1409 		.val_type = USB_MIXER_S16,
1410 		.name = "Headphone B Mix Playback Volume"
1411 	},
1412 
1413 	{
1414 		.unitid = 7,
1415 		.control = 1,
1416 		.cmask = 0x0,
1417 		.val_type = USB_MIXER_INV_BOOLEAN,
1418 		.name = "Output Playback Switch"
1419 	},
1420 	{
1421 		.unitid = 7,
1422 		.control = 2,
1423 		.cmask = 0x1,
1424 		.val_type = USB_MIXER_S16,
1425 		.name = "Output A Playback Volume"
1426 	},
1427 	{
1428 		.unitid = 7,
1429 		.control = 2,
1430 		.cmask = 0x2,
1431 		.val_type = USB_MIXER_S16,
1432 		.name = "Output B Playback Volume"
1433 	},
1434 
1435 	{
1436 		.unitid = 10,
1437 		.control = 1,
1438 		.cmask = 0x0,
1439 		.val_type = USB_MIXER_INV_BOOLEAN,
1440 		.name = "Input Capture Switch"
1441 	},
1442 	{
1443 		.unitid = 10,
1444 		.control = 2,
1445 		.cmask = 0x1,
1446 		.val_type = USB_MIXER_S16,
1447 		.name = "Input A Capture Volume"
1448 	},
1449 	{
1450 		.unitid = 10,
1451 		.control = 2,
1452 		.cmask = 0x2,
1453 		.val_type = USB_MIXER_S16,
1454 		.name = "Input B Capture Volume"
1455 	},
1456 
1457 	{}
1458 };
1459 
1460 /* Audio Advantage Micro II findings:
1461  *
1462  * Mapping spdif AES bits to vendor register.bit:
1463  * AES0: [0 0 0 0 2.3 2.2 2.1 2.0] - default 0x00
1464  * AES1: [3.3 3.2.3.1.3.0 2.7 2.6 2.5 2.4] - default: 0x01
1465  * AES2: [0 0 0 0 0 0 0 0]
1466  * AES3: [0 0 0 0 0 0 x 0] - 'x' bit is set basing on standard usb request
1467  *                           (UAC_EP_CS_ATTR_SAMPLE_RATE) for Audio Devices
1468  *
1469  * power on values:
1470  * r2: 0x10
1471  * r3: 0x20 (b7 is zeroed just before playback (except IEC61937) and set
1472  *           just after it to 0xa0, presumably it disables/mutes some analog
1473  *           parts when there is no audio.)
1474  * r9: 0x28
1475  *
1476  * Optical transmitter on/off:
1477  * vendor register.bit: 9.1
1478  * 0 - on (0x28 register value)
1479  * 1 - off (0x2a register value)
1480  *
1481  */
1482 static int snd_microii_spdif_info(struct snd_kcontrol *kcontrol,
1483 	struct snd_ctl_elem_info *uinfo)
1484 {
1485 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1486 	uinfo->count = 1;
1487 	return 0;
1488 }
1489 
1490 static int snd_microii_spdif_default_get(struct snd_kcontrol *kcontrol,
1491 	struct snd_ctl_elem_value *ucontrol)
1492 {
1493 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1494 	struct snd_usb_audio *chip = list->mixer->chip;
1495 	int err;
1496 	struct usb_interface *iface;
1497 	struct usb_host_interface *alts;
1498 	unsigned int ep;
1499 	unsigned char data[3];
1500 	int rate;
1501 
1502 	err = snd_usb_lock_shutdown(chip);
1503 	if (err < 0)
1504 		return err;
1505 
1506 	ucontrol->value.iec958.status[0] = kcontrol->private_value & 0xff;
1507 	ucontrol->value.iec958.status[1] = (kcontrol->private_value >> 8) & 0xff;
1508 	ucontrol->value.iec958.status[2] = 0x00;
1509 
1510 	/* use known values for that card: interface#1 altsetting#1 */
1511 	iface = usb_ifnum_to_if(chip->dev, 1);
1512 	if (!iface || iface->num_altsetting < 2) {
1513 		err = -EINVAL;
1514 		goto end;
1515 	}
1516 	alts = &iface->altsetting[1];
1517 	if (get_iface_desc(alts)->bNumEndpoints < 1) {
1518 		err = -EINVAL;
1519 		goto end;
1520 	}
1521 	ep = get_endpoint(alts, 0)->bEndpointAddress;
1522 
1523 	err = snd_usb_ctl_msg(chip->dev,
1524 			usb_rcvctrlpipe(chip->dev, 0),
1525 			UAC_GET_CUR,
1526 			USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
1527 			UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
1528 			ep,
1529 			data,
1530 			sizeof(data));
1531 	if (err < 0)
1532 		goto end;
1533 
1534 	rate = data[0] | (data[1] << 8) | (data[2] << 16);
1535 	ucontrol->value.iec958.status[3] = (rate == 48000) ?
1536 			IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100;
1537 
1538 	err = 0;
1539  end:
1540 	snd_usb_unlock_shutdown(chip);
1541 	return err;
1542 }
1543 
1544 static int snd_microii_spdif_default_update(struct usb_mixer_elem_list *list)
1545 {
1546 	struct snd_usb_audio *chip = list->mixer->chip;
1547 	unsigned int pval = list->kctl->private_value;
1548 	u8 reg;
1549 	int err;
1550 
1551 	err = snd_usb_lock_shutdown(chip);
1552 	if (err < 0)
1553 		return err;
1554 
1555 	reg = ((pval >> 4) & 0xf0) | (pval & 0x0f);
1556 	err = snd_usb_ctl_msg(chip->dev,
1557 			usb_sndctrlpipe(chip->dev, 0),
1558 			UAC_SET_CUR,
1559 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1560 			reg,
1561 			2,
1562 			NULL,
1563 			0);
1564 	if (err < 0)
1565 		goto end;
1566 
1567 	reg = (pval & IEC958_AES0_NONAUDIO) ? 0xa0 : 0x20;
1568 	reg |= (pval >> 12) & 0x0f;
1569 	err = snd_usb_ctl_msg(chip->dev,
1570 			usb_sndctrlpipe(chip->dev, 0),
1571 			UAC_SET_CUR,
1572 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1573 			reg,
1574 			3,
1575 			NULL,
1576 			0);
1577 	if (err < 0)
1578 		goto end;
1579 
1580  end:
1581 	snd_usb_unlock_shutdown(chip);
1582 	return err;
1583 }
1584 
1585 static int snd_microii_spdif_default_put(struct snd_kcontrol *kcontrol,
1586 	struct snd_ctl_elem_value *ucontrol)
1587 {
1588 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1589 	unsigned int pval, pval_old;
1590 	int err;
1591 
1592 	pval = pval_old = kcontrol->private_value;
1593 	pval &= 0xfffff0f0;
1594 	pval |= (ucontrol->value.iec958.status[1] & 0x0f) << 8;
1595 	pval |= (ucontrol->value.iec958.status[0] & 0x0f);
1596 
1597 	pval &= 0xffff0fff;
1598 	pval |= (ucontrol->value.iec958.status[1] & 0xf0) << 8;
1599 
1600 	/* The frequency bits in AES3 cannot be set via register access. */
1601 
1602 	/* Silently ignore any bits from the request that cannot be set. */
1603 
1604 	if (pval == pval_old)
1605 		return 0;
1606 
1607 	kcontrol->private_value = pval;
1608 	err = snd_microii_spdif_default_update(list);
1609 	return err < 0 ? err : 1;
1610 }
1611 
1612 static int snd_microii_spdif_mask_get(struct snd_kcontrol *kcontrol,
1613 	struct snd_ctl_elem_value *ucontrol)
1614 {
1615 	ucontrol->value.iec958.status[0] = 0x0f;
1616 	ucontrol->value.iec958.status[1] = 0xff;
1617 	ucontrol->value.iec958.status[2] = 0x00;
1618 	ucontrol->value.iec958.status[3] = 0x00;
1619 
1620 	return 0;
1621 }
1622 
1623 static int snd_microii_spdif_switch_get(struct snd_kcontrol *kcontrol,
1624 	struct snd_ctl_elem_value *ucontrol)
1625 {
1626 	ucontrol->value.integer.value[0] = !(kcontrol->private_value & 0x02);
1627 
1628 	return 0;
1629 }
1630 
1631 static int snd_microii_spdif_switch_update(struct usb_mixer_elem_list *list)
1632 {
1633 	struct snd_usb_audio *chip = list->mixer->chip;
1634 	u8 reg = list->kctl->private_value;
1635 	int err;
1636 
1637 	err = snd_usb_lock_shutdown(chip);
1638 	if (err < 0)
1639 		return err;
1640 
1641 	err = snd_usb_ctl_msg(chip->dev,
1642 			usb_sndctrlpipe(chip->dev, 0),
1643 			UAC_SET_CUR,
1644 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1645 			reg,
1646 			9,
1647 			NULL,
1648 			0);
1649 
1650 	snd_usb_unlock_shutdown(chip);
1651 	return err;
1652 }
1653 
1654 static int snd_microii_spdif_switch_put(struct snd_kcontrol *kcontrol,
1655 	struct snd_ctl_elem_value *ucontrol)
1656 {
1657 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1658 	u8 reg;
1659 	int err;
1660 
1661 	reg = ucontrol->value.integer.value[0] ? 0x28 : 0x2a;
1662 	if (reg != list->kctl->private_value)
1663 		return 0;
1664 
1665 	kcontrol->private_value = reg;
1666 	err = snd_microii_spdif_switch_update(list);
1667 	return err < 0 ? err : 1;
1668 }
1669 
1670 static const struct snd_kcontrol_new snd_microii_mixer_spdif[] = {
1671 	{
1672 		.iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1673 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1674 		.info =     snd_microii_spdif_info,
1675 		.get =      snd_microii_spdif_default_get,
1676 		.put =      snd_microii_spdif_default_put,
1677 		.private_value = 0x00000100UL,/* reset value */
1678 	},
1679 	{
1680 		.access =   SNDRV_CTL_ELEM_ACCESS_READ,
1681 		.iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1682 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1683 		.info =     snd_microii_spdif_info,
1684 		.get =      snd_microii_spdif_mask_get,
1685 	},
1686 	{
1687 		.iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
1688 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1689 		.info =     snd_ctl_boolean_mono_info,
1690 		.get =      snd_microii_spdif_switch_get,
1691 		.put =      snd_microii_spdif_switch_put,
1692 		.private_value = 0x00000028UL,/* reset value */
1693 	}
1694 };
1695 
1696 static int snd_microii_controls_create(struct usb_mixer_interface *mixer)
1697 {
1698 	int err, i;
1699 	static const usb_mixer_elem_resume_func_t resume_funcs[] = {
1700 		snd_microii_spdif_default_update,
1701 		NULL,
1702 		snd_microii_spdif_switch_update
1703 	};
1704 
1705 	for (i = 0; i < ARRAY_SIZE(snd_microii_mixer_spdif); ++i) {
1706 		err = add_single_ctl_with_resume(mixer, 0,
1707 						 resume_funcs[i],
1708 						 &snd_microii_mixer_spdif[i],
1709 						 NULL);
1710 		if (err < 0)
1711 			return err;
1712 	}
1713 
1714 	return 0;
1715 }
1716 
1717 /* Creative Sound Blaster E1 */
1718 
1719 static int snd_soundblaster_e1_switch_get(struct snd_kcontrol *kcontrol,
1720 					  struct snd_ctl_elem_value *ucontrol)
1721 {
1722 	ucontrol->value.integer.value[0] = kcontrol->private_value;
1723 	return 0;
1724 }
1725 
1726 static int snd_soundblaster_e1_switch_update(struct usb_mixer_interface *mixer,
1727 					     unsigned char state)
1728 {
1729 	struct snd_usb_audio *chip = mixer->chip;
1730 	int err;
1731 	unsigned char buff[2];
1732 
1733 	buff[0] = 0x02;
1734 	buff[1] = state ? 0x02 : 0x00;
1735 
1736 	err = snd_usb_lock_shutdown(chip);
1737 	if (err < 0)
1738 		return err;
1739 	err = snd_usb_ctl_msg(chip->dev,
1740 			usb_sndctrlpipe(chip->dev, 0), HID_REQ_SET_REPORT,
1741 			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
1742 			0x0202, 3, buff, 2);
1743 	snd_usb_unlock_shutdown(chip);
1744 	return err;
1745 }
1746 
1747 static int snd_soundblaster_e1_switch_put(struct snd_kcontrol *kcontrol,
1748 					  struct snd_ctl_elem_value *ucontrol)
1749 {
1750 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1751 	unsigned char value = !!ucontrol->value.integer.value[0];
1752 	int err;
1753 
1754 	if (kcontrol->private_value == value)
1755 		return 0;
1756 	kcontrol->private_value = value;
1757 	err = snd_soundblaster_e1_switch_update(list->mixer, value);
1758 	return err < 0 ? err : 1;
1759 }
1760 
1761 static int snd_soundblaster_e1_switch_resume(struct usb_mixer_elem_list *list)
1762 {
1763 	return snd_soundblaster_e1_switch_update(list->mixer,
1764 						 list->kctl->private_value);
1765 }
1766 
1767 static int snd_soundblaster_e1_switch_info(struct snd_kcontrol *kcontrol,
1768 					   struct snd_ctl_elem_info *uinfo)
1769 {
1770 	static const char *const texts[2] = {
1771 		"Mic", "Aux"
1772 	};
1773 
1774 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1775 }
1776 
1777 static const struct snd_kcontrol_new snd_soundblaster_e1_input_switch = {
1778 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1779 	.name = "Input Source",
1780 	.info = snd_soundblaster_e1_switch_info,
1781 	.get = snd_soundblaster_e1_switch_get,
1782 	.put = snd_soundblaster_e1_switch_put,
1783 	.private_value = 0,
1784 };
1785 
1786 static int snd_soundblaster_e1_switch_create(struct usb_mixer_interface *mixer)
1787 {
1788 	return add_single_ctl_with_resume(mixer, 0,
1789 					  snd_soundblaster_e1_switch_resume,
1790 					  &snd_soundblaster_e1_input_switch,
1791 					  NULL);
1792 }
1793 
1794 static void dell_dock_init_vol(struct snd_usb_audio *chip, int ch, int id)
1795 {
1796 	u16 buf = 0;
1797 
1798 	snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
1799 			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1800 			ch, snd_usb_ctrl_intf(chip) | (id << 8),
1801 			&buf, 2);
1802 }
1803 
1804 static int dell_dock_mixer_init(struct usb_mixer_interface *mixer)
1805 {
1806 	/* fix to 0dB playback volumes */
1807 	dell_dock_init_vol(mixer->chip, 1, 16);
1808 	dell_dock_init_vol(mixer->chip, 2, 16);
1809 	dell_dock_init_vol(mixer->chip, 1, 19);
1810 	dell_dock_init_vol(mixer->chip, 2, 19);
1811 	return 0;
1812 }
1813 
1814 /* RME Class Compliant device quirks */
1815 
1816 #define SND_RME_GET_STATUS1			23
1817 #define SND_RME_GET_CURRENT_FREQ		17
1818 #define SND_RME_CLK_SYSTEM_SHIFT		16
1819 #define SND_RME_CLK_SYSTEM_MASK			0x1f
1820 #define SND_RME_CLK_AES_SHIFT			8
1821 #define SND_RME_CLK_SPDIF_SHIFT			12
1822 #define SND_RME_CLK_AES_SPDIF_MASK		0xf
1823 #define SND_RME_CLK_SYNC_SHIFT			6
1824 #define SND_RME_CLK_SYNC_MASK			0x3
1825 #define SND_RME_CLK_FREQMUL_SHIFT		18
1826 #define SND_RME_CLK_FREQMUL_MASK		0x7
1827 #define SND_RME_CLK_SYSTEM(x) \
1828 	((x >> SND_RME_CLK_SYSTEM_SHIFT) & SND_RME_CLK_SYSTEM_MASK)
1829 #define SND_RME_CLK_AES(x) \
1830 	((x >> SND_RME_CLK_AES_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
1831 #define SND_RME_CLK_SPDIF(x) \
1832 	((x >> SND_RME_CLK_SPDIF_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
1833 #define SND_RME_CLK_SYNC(x) \
1834 	((x >> SND_RME_CLK_SYNC_SHIFT) & SND_RME_CLK_SYNC_MASK)
1835 #define SND_RME_CLK_FREQMUL(x) \
1836 	((x >> SND_RME_CLK_FREQMUL_SHIFT) & SND_RME_CLK_FREQMUL_MASK)
1837 #define SND_RME_CLK_AES_LOCK			0x1
1838 #define SND_RME_CLK_AES_SYNC			0x4
1839 #define SND_RME_CLK_SPDIF_LOCK			0x2
1840 #define SND_RME_CLK_SPDIF_SYNC			0x8
1841 #define SND_RME_SPDIF_IF_SHIFT			4
1842 #define SND_RME_SPDIF_FORMAT_SHIFT		5
1843 #define SND_RME_BINARY_MASK			0x1
1844 #define SND_RME_SPDIF_IF(x) \
1845 	((x >> SND_RME_SPDIF_IF_SHIFT) & SND_RME_BINARY_MASK)
1846 #define SND_RME_SPDIF_FORMAT(x) \
1847 	((x >> SND_RME_SPDIF_FORMAT_SHIFT) & SND_RME_BINARY_MASK)
1848 
1849 static const u32 snd_rme_rate_table[] = {
1850 	32000, 44100, 48000, 50000,
1851 	64000, 88200, 96000, 100000,
1852 	128000, 176400, 192000, 200000,
1853 	256000,	352800, 384000, 400000,
1854 	512000, 705600, 768000, 800000
1855 };
1856 /* maximum number of items for AES and S/PDIF rates for above table */
1857 #define SND_RME_RATE_IDX_AES_SPDIF_NUM		12
1858 
1859 enum snd_rme_domain {
1860 	SND_RME_DOMAIN_SYSTEM,
1861 	SND_RME_DOMAIN_AES,
1862 	SND_RME_DOMAIN_SPDIF
1863 };
1864 
1865 enum snd_rme_clock_status {
1866 	SND_RME_CLOCK_NOLOCK,
1867 	SND_RME_CLOCK_LOCK,
1868 	SND_RME_CLOCK_SYNC
1869 };
1870 
1871 static int snd_rme_read_value(struct snd_usb_audio *chip,
1872 			      unsigned int item,
1873 			      u32 *value)
1874 {
1875 	struct usb_device *dev = chip->dev;
1876 	int err;
1877 
1878 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1879 			      item,
1880 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1881 			      0, 0,
1882 			      value, sizeof(*value));
1883 	if (err < 0)
1884 		dev_err(&dev->dev,
1885 			"unable to issue vendor read request %d (ret = %d)",
1886 			item, err);
1887 	return err;
1888 }
1889 
1890 static int snd_rme_get_status1(struct snd_kcontrol *kcontrol,
1891 			       u32 *status1)
1892 {
1893 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1894 	struct snd_usb_audio *chip = list->mixer->chip;
1895 	int err;
1896 
1897 	err = snd_usb_lock_shutdown(chip);
1898 	if (err < 0)
1899 		return err;
1900 	err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, status1);
1901 	snd_usb_unlock_shutdown(chip);
1902 	return err;
1903 }
1904 
1905 static int snd_rme_rate_get(struct snd_kcontrol *kcontrol,
1906 			    struct snd_ctl_elem_value *ucontrol)
1907 {
1908 	u32 status1;
1909 	u32 rate = 0;
1910 	int idx;
1911 	int err;
1912 
1913 	err = snd_rme_get_status1(kcontrol, &status1);
1914 	if (err < 0)
1915 		return err;
1916 	switch (kcontrol->private_value) {
1917 	case SND_RME_DOMAIN_SYSTEM:
1918 		idx = SND_RME_CLK_SYSTEM(status1);
1919 		if (idx < ARRAY_SIZE(snd_rme_rate_table))
1920 			rate = snd_rme_rate_table[idx];
1921 		break;
1922 	case SND_RME_DOMAIN_AES:
1923 		idx = SND_RME_CLK_AES(status1);
1924 		if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
1925 			rate = snd_rme_rate_table[idx];
1926 		break;
1927 	case SND_RME_DOMAIN_SPDIF:
1928 		idx = SND_RME_CLK_SPDIF(status1);
1929 		if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
1930 			rate = snd_rme_rate_table[idx];
1931 		break;
1932 	default:
1933 		return -EINVAL;
1934 	}
1935 	ucontrol->value.integer.value[0] = rate;
1936 	return 0;
1937 }
1938 
1939 static int snd_rme_sync_state_get(struct snd_kcontrol *kcontrol,
1940 				  struct snd_ctl_elem_value *ucontrol)
1941 {
1942 	u32 status1;
1943 	int idx = SND_RME_CLOCK_NOLOCK;
1944 	int err;
1945 
1946 	err = snd_rme_get_status1(kcontrol, &status1);
1947 	if (err < 0)
1948 		return err;
1949 	switch (kcontrol->private_value) {
1950 	case SND_RME_DOMAIN_AES:  /* AES */
1951 		if (status1 & SND_RME_CLK_AES_SYNC)
1952 			idx = SND_RME_CLOCK_SYNC;
1953 		else if (status1 & SND_RME_CLK_AES_LOCK)
1954 			idx = SND_RME_CLOCK_LOCK;
1955 		break;
1956 	case SND_RME_DOMAIN_SPDIF:  /* SPDIF */
1957 		if (status1 & SND_RME_CLK_SPDIF_SYNC)
1958 			idx = SND_RME_CLOCK_SYNC;
1959 		else if (status1 & SND_RME_CLK_SPDIF_LOCK)
1960 			idx = SND_RME_CLOCK_LOCK;
1961 		break;
1962 	default:
1963 		return -EINVAL;
1964 	}
1965 	ucontrol->value.enumerated.item[0] = idx;
1966 	return 0;
1967 }
1968 
1969 static int snd_rme_spdif_if_get(struct snd_kcontrol *kcontrol,
1970 				struct snd_ctl_elem_value *ucontrol)
1971 {
1972 	u32 status1;
1973 	int err;
1974 
1975 	err = snd_rme_get_status1(kcontrol, &status1);
1976 	if (err < 0)
1977 		return err;
1978 	ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_IF(status1);
1979 	return 0;
1980 }
1981 
1982 static int snd_rme_spdif_format_get(struct snd_kcontrol *kcontrol,
1983 				    struct snd_ctl_elem_value *ucontrol)
1984 {
1985 	u32 status1;
1986 	int err;
1987 
1988 	err = snd_rme_get_status1(kcontrol, &status1);
1989 	if (err < 0)
1990 		return err;
1991 	ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_FORMAT(status1);
1992 	return 0;
1993 }
1994 
1995 static int snd_rme_sync_source_get(struct snd_kcontrol *kcontrol,
1996 				   struct snd_ctl_elem_value *ucontrol)
1997 {
1998 	u32 status1;
1999 	int err;
2000 
2001 	err = snd_rme_get_status1(kcontrol, &status1);
2002 	if (err < 0)
2003 		return err;
2004 	ucontrol->value.enumerated.item[0] = SND_RME_CLK_SYNC(status1);
2005 	return 0;
2006 }
2007 
2008 static int snd_rme_current_freq_get(struct snd_kcontrol *kcontrol,
2009 				    struct snd_ctl_elem_value *ucontrol)
2010 {
2011 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2012 	struct snd_usb_audio *chip = list->mixer->chip;
2013 	u32 status1;
2014 	const u64 num = 104857600000000ULL;
2015 	u32 den;
2016 	unsigned int freq;
2017 	int err;
2018 
2019 	err = snd_usb_lock_shutdown(chip);
2020 	if (err < 0)
2021 		return err;
2022 	err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, &status1);
2023 	if (err < 0)
2024 		goto end;
2025 	err = snd_rme_read_value(chip, SND_RME_GET_CURRENT_FREQ, &den);
2026 	if (err < 0)
2027 		goto end;
2028 	freq = (den == 0) ? 0 : div64_u64(num, den);
2029 	freq <<= SND_RME_CLK_FREQMUL(status1);
2030 	ucontrol->value.integer.value[0] = freq;
2031 
2032 end:
2033 	snd_usb_unlock_shutdown(chip);
2034 	return err;
2035 }
2036 
2037 static int snd_rme_rate_info(struct snd_kcontrol *kcontrol,
2038 			     struct snd_ctl_elem_info *uinfo)
2039 {
2040 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2041 	uinfo->count = 1;
2042 	switch (kcontrol->private_value) {
2043 	case SND_RME_DOMAIN_SYSTEM:
2044 		uinfo->value.integer.min = 32000;
2045 		uinfo->value.integer.max = 800000;
2046 		break;
2047 	case SND_RME_DOMAIN_AES:
2048 	case SND_RME_DOMAIN_SPDIF:
2049 	default:
2050 		uinfo->value.integer.min = 0;
2051 		uinfo->value.integer.max = 200000;
2052 	}
2053 	uinfo->value.integer.step = 0;
2054 	return 0;
2055 }
2056 
2057 static int snd_rme_sync_state_info(struct snd_kcontrol *kcontrol,
2058 				   struct snd_ctl_elem_info *uinfo)
2059 {
2060 	static const char *const sync_states[] = {
2061 		"No Lock", "Lock", "Sync"
2062 	};
2063 
2064 	return snd_ctl_enum_info(uinfo, 1,
2065 				 ARRAY_SIZE(sync_states), sync_states);
2066 }
2067 
2068 static int snd_rme_spdif_if_info(struct snd_kcontrol *kcontrol,
2069 				 struct snd_ctl_elem_info *uinfo)
2070 {
2071 	static const char *const spdif_if[] = {
2072 		"Coaxial", "Optical"
2073 	};
2074 
2075 	return snd_ctl_enum_info(uinfo, 1,
2076 				 ARRAY_SIZE(spdif_if), spdif_if);
2077 }
2078 
2079 static int snd_rme_spdif_format_info(struct snd_kcontrol *kcontrol,
2080 				     struct snd_ctl_elem_info *uinfo)
2081 {
2082 	static const char *const optical_type[] = {
2083 		"Consumer", "Professional"
2084 	};
2085 
2086 	return snd_ctl_enum_info(uinfo, 1,
2087 				 ARRAY_SIZE(optical_type), optical_type);
2088 }
2089 
2090 static int snd_rme_sync_source_info(struct snd_kcontrol *kcontrol,
2091 				    struct snd_ctl_elem_info *uinfo)
2092 {
2093 	static const char *const sync_sources[] = {
2094 		"Internal", "AES", "SPDIF", "Internal"
2095 	};
2096 
2097 	return snd_ctl_enum_info(uinfo, 1,
2098 				 ARRAY_SIZE(sync_sources), sync_sources);
2099 }
2100 
2101 static const struct snd_kcontrol_new snd_rme_controls[] = {
2102 	{
2103 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2104 		.name = "AES Rate",
2105 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2106 		.info = snd_rme_rate_info,
2107 		.get = snd_rme_rate_get,
2108 		.private_value = SND_RME_DOMAIN_AES
2109 	},
2110 	{
2111 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2112 		.name = "AES Sync",
2113 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2114 		.info = snd_rme_sync_state_info,
2115 		.get = snd_rme_sync_state_get,
2116 		.private_value = SND_RME_DOMAIN_AES
2117 	},
2118 	{
2119 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2120 		.name = "SPDIF Rate",
2121 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2122 		.info = snd_rme_rate_info,
2123 		.get = snd_rme_rate_get,
2124 		.private_value = SND_RME_DOMAIN_SPDIF
2125 	},
2126 	{
2127 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2128 		.name = "SPDIF Sync",
2129 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2130 		.info = snd_rme_sync_state_info,
2131 		.get = snd_rme_sync_state_get,
2132 		.private_value = SND_RME_DOMAIN_SPDIF
2133 	},
2134 	{
2135 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2136 		.name = "SPDIF Interface",
2137 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2138 		.info = snd_rme_spdif_if_info,
2139 		.get = snd_rme_spdif_if_get,
2140 	},
2141 	{
2142 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2143 		.name = "SPDIF Format",
2144 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2145 		.info = snd_rme_spdif_format_info,
2146 		.get = snd_rme_spdif_format_get,
2147 	},
2148 	{
2149 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2150 		.name = "Sync Source",
2151 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2152 		.info = snd_rme_sync_source_info,
2153 		.get = snd_rme_sync_source_get
2154 	},
2155 	{
2156 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2157 		.name = "System Rate",
2158 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2159 		.info = snd_rme_rate_info,
2160 		.get = snd_rme_rate_get,
2161 		.private_value = SND_RME_DOMAIN_SYSTEM
2162 	},
2163 	{
2164 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2165 		.name = "Current Frequency",
2166 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2167 		.info = snd_rme_rate_info,
2168 		.get = snd_rme_current_freq_get
2169 	}
2170 };
2171 
2172 static int snd_rme_controls_create(struct usb_mixer_interface *mixer)
2173 {
2174 	int err, i;
2175 
2176 	for (i = 0; i < ARRAY_SIZE(snd_rme_controls); ++i) {
2177 		err = add_single_ctl_with_resume(mixer, 0,
2178 						 NULL,
2179 						 &snd_rme_controls[i],
2180 						 NULL);
2181 		if (err < 0)
2182 			return err;
2183 	}
2184 
2185 	return 0;
2186 }
2187 
2188 /*
2189  * RME Babyface Pro (FS)
2190  *
2191  * These devices exposes a couple of DSP functions via request to EP0.
2192  * Switches are available via control registers, while routing is controlled
2193  * by controlling the volume on each possible crossing point.
2194  * Volume control is linear, from -inf (dec. 0) to +6dB (dec. 65536) with
2195  * 0dB being at dec. 32768.
2196  */
2197 enum {
2198 	SND_BBFPRO_CTL_REG1 = 0,
2199 	SND_BBFPRO_CTL_REG2
2200 };
2201 
2202 #define SND_BBFPRO_CTL_REG_MASK 1
2203 #define SND_BBFPRO_CTL_IDX_MASK 0xff
2204 #define SND_BBFPRO_CTL_IDX_SHIFT 1
2205 #define SND_BBFPRO_CTL_VAL_MASK 1
2206 #define SND_BBFPRO_CTL_VAL_SHIFT 9
2207 #define SND_BBFPRO_CTL_REG1_CLK_MASTER 0
2208 #define SND_BBFPRO_CTL_REG1_CLK_OPTICAL 1
2209 #define SND_BBFPRO_CTL_REG1_SPDIF_PRO 7
2210 #define SND_BBFPRO_CTL_REG1_SPDIF_EMPH 8
2211 #define SND_BBFPRO_CTL_REG1_SPDIF_OPTICAL 10
2212 #define SND_BBFPRO_CTL_REG2_48V_AN1 0
2213 #define SND_BBFPRO_CTL_REG2_48V_AN2 1
2214 #define SND_BBFPRO_CTL_REG2_SENS_IN3 2
2215 #define SND_BBFPRO_CTL_REG2_SENS_IN4 3
2216 #define SND_BBFPRO_CTL_REG2_PAD_AN1 4
2217 #define SND_BBFPRO_CTL_REG2_PAD_AN2 5
2218 
2219 #define SND_BBFPRO_MIXER_IDX_MASK 0x1ff
2220 #define SND_BBFPRO_MIXER_VAL_MASK 0x3ffff
2221 #define SND_BBFPRO_MIXER_VAL_SHIFT 9
2222 #define SND_BBFPRO_MIXER_VAL_MIN 0 // -inf
2223 #define SND_BBFPRO_MIXER_VAL_MAX 65536 // +6dB
2224 
2225 #define SND_BBFPRO_USBREQ_CTL_REG1 0x10
2226 #define SND_BBFPRO_USBREQ_CTL_REG2 0x17
2227 #define SND_BBFPRO_USBREQ_MIXER 0x12
2228 
2229 static int snd_bbfpro_ctl_update(struct usb_mixer_interface *mixer, u8 reg,
2230 				 u8 index, u8 value)
2231 {
2232 	int err;
2233 	u16 usb_req, usb_idx, usb_val;
2234 	struct snd_usb_audio *chip = mixer->chip;
2235 
2236 	err = snd_usb_lock_shutdown(chip);
2237 	if (err < 0)
2238 		return err;
2239 
2240 	if (reg == SND_BBFPRO_CTL_REG1) {
2241 		usb_req = SND_BBFPRO_USBREQ_CTL_REG1;
2242 		if (index == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) {
2243 			usb_idx = 3;
2244 			usb_val = value ? 3 : 0;
2245 		} else {
2246 			usb_idx = 1 << index;
2247 			usb_val = value ? usb_idx : 0;
2248 		}
2249 	} else {
2250 		usb_req = SND_BBFPRO_USBREQ_CTL_REG2;
2251 		usb_idx = 1 << index;
2252 		usb_val = value ? usb_idx : 0;
2253 	}
2254 
2255 	err = snd_usb_ctl_msg(chip->dev,
2256 			      usb_sndctrlpipe(chip->dev, 0), usb_req,
2257 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2258 			      usb_val, usb_idx, NULL, 0);
2259 
2260 	snd_usb_unlock_shutdown(chip);
2261 	return err;
2262 }
2263 
2264 static int snd_bbfpro_ctl_get(struct snd_kcontrol *kcontrol,
2265 			      struct snd_ctl_elem_value *ucontrol)
2266 {
2267 	u8 reg, idx, val;
2268 	int pv;
2269 
2270 	pv = kcontrol->private_value;
2271 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2272 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2273 	val = kcontrol->private_value >> SND_BBFPRO_CTL_VAL_SHIFT;
2274 
2275 	if ((reg == SND_BBFPRO_CTL_REG1 &&
2276 	     idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) ||
2277 	    (reg == SND_BBFPRO_CTL_REG2 &&
2278 	    (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2279 	     idx == SND_BBFPRO_CTL_REG2_SENS_IN4))) {
2280 		ucontrol->value.enumerated.item[0] = val;
2281 	} else {
2282 		ucontrol->value.integer.value[0] = val;
2283 	}
2284 	return 0;
2285 }
2286 
2287 static int snd_bbfpro_ctl_info(struct snd_kcontrol *kcontrol,
2288 			       struct snd_ctl_elem_info *uinfo)
2289 {
2290 	u8 reg, idx;
2291 	int pv;
2292 
2293 	pv = kcontrol->private_value;
2294 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2295 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2296 
2297 	if (reg == SND_BBFPRO_CTL_REG1 &&
2298 	    idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) {
2299 		static const char * const texts[2] = {
2300 			"AutoSync",
2301 			"Internal"
2302 		};
2303 		return snd_ctl_enum_info(uinfo, 1, 2, texts);
2304 	} else if (reg == SND_BBFPRO_CTL_REG2 &&
2305 		   (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2306 		    idx == SND_BBFPRO_CTL_REG2_SENS_IN4)) {
2307 		static const char * const texts[2] = {
2308 			"-10dBV",
2309 			"+4dBu"
2310 		};
2311 		return snd_ctl_enum_info(uinfo, 1, 2, texts);
2312 	}
2313 
2314 	uinfo->count = 1;
2315 	uinfo->value.integer.min = 0;
2316 	uinfo->value.integer.max = 1;
2317 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2318 	return 0;
2319 }
2320 
2321 static int snd_bbfpro_ctl_put(struct snd_kcontrol *kcontrol,
2322 			      struct snd_ctl_elem_value *ucontrol)
2323 {
2324 	int err;
2325 	u8 reg, idx;
2326 	int old_value, pv, val;
2327 
2328 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2329 	struct usb_mixer_interface *mixer = list->mixer;
2330 
2331 	pv = kcontrol->private_value;
2332 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2333 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2334 	old_value = (pv >> SND_BBFPRO_CTL_VAL_SHIFT) & SND_BBFPRO_CTL_VAL_MASK;
2335 
2336 	if ((reg == SND_BBFPRO_CTL_REG1 &&
2337 	     idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) ||
2338 	    (reg == SND_BBFPRO_CTL_REG2 &&
2339 	    (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2340 	     idx == SND_BBFPRO_CTL_REG2_SENS_IN4))) {
2341 		val = ucontrol->value.enumerated.item[0];
2342 	} else {
2343 		val = ucontrol->value.integer.value[0];
2344 	}
2345 
2346 	if (val > 1)
2347 		return -EINVAL;
2348 
2349 	if (val == old_value)
2350 		return 0;
2351 
2352 	kcontrol->private_value = reg
2353 		| ((idx & SND_BBFPRO_CTL_IDX_MASK) << SND_BBFPRO_CTL_IDX_SHIFT)
2354 		| ((val & SND_BBFPRO_CTL_VAL_MASK) << SND_BBFPRO_CTL_VAL_SHIFT);
2355 
2356 	err = snd_bbfpro_ctl_update(mixer, reg, idx, val);
2357 	return err < 0 ? err : 1;
2358 }
2359 
2360 static int snd_bbfpro_ctl_resume(struct usb_mixer_elem_list *list)
2361 {
2362 	u8 reg, idx;
2363 	int value, pv;
2364 
2365 	pv = list->kctl->private_value;
2366 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2367 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2368 	value = (pv >> SND_BBFPRO_CTL_VAL_SHIFT) & SND_BBFPRO_CTL_VAL_MASK;
2369 
2370 	return snd_bbfpro_ctl_update(list->mixer, reg, idx, value);
2371 }
2372 
2373 static int snd_bbfpro_vol_update(struct usb_mixer_interface *mixer, u16 index,
2374 				 u32 value)
2375 {
2376 	struct snd_usb_audio *chip = mixer->chip;
2377 	int err;
2378 	u16 idx;
2379 	u16 usb_idx, usb_val;
2380 	u32 v;
2381 
2382 	err = snd_usb_lock_shutdown(chip);
2383 	if (err < 0)
2384 		return err;
2385 
2386 	idx = index & SND_BBFPRO_MIXER_IDX_MASK;
2387 	// 18 bit linear volume, split so 2 bits end up in index.
2388 	v = value & SND_BBFPRO_MIXER_VAL_MASK;
2389 	usb_idx = idx | (v & 0x3) << 14;
2390 	usb_val = (v >> 2) & 0xffff;
2391 
2392 	err = snd_usb_ctl_msg(chip->dev,
2393 			      usb_sndctrlpipe(chip->dev, 0),
2394 			      SND_BBFPRO_USBREQ_MIXER,
2395 			      USB_DIR_OUT | USB_TYPE_VENDOR |
2396 			      USB_RECIP_DEVICE,
2397 			      usb_val, usb_idx, NULL, 0);
2398 
2399 	snd_usb_unlock_shutdown(chip);
2400 	return err;
2401 }
2402 
2403 static int snd_bbfpro_vol_get(struct snd_kcontrol *kcontrol,
2404 			      struct snd_ctl_elem_value *ucontrol)
2405 {
2406 	ucontrol->value.integer.value[0] =
2407 		kcontrol->private_value >> SND_BBFPRO_MIXER_VAL_SHIFT;
2408 	return 0;
2409 }
2410 
2411 static int snd_bbfpro_vol_info(struct snd_kcontrol *kcontrol,
2412 			       struct snd_ctl_elem_info *uinfo)
2413 {
2414 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2415 	uinfo->count = 1;
2416 	uinfo->value.integer.min = SND_BBFPRO_MIXER_VAL_MIN;
2417 	uinfo->value.integer.max = SND_BBFPRO_MIXER_VAL_MAX;
2418 	return 0;
2419 }
2420 
2421 static int snd_bbfpro_vol_put(struct snd_kcontrol *kcontrol,
2422 			      struct snd_ctl_elem_value *ucontrol)
2423 {
2424 	int err;
2425 	u16 idx;
2426 	u32 new_val, old_value, uvalue;
2427 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2428 	struct usb_mixer_interface *mixer = list->mixer;
2429 
2430 	uvalue = ucontrol->value.integer.value[0];
2431 	idx = kcontrol->private_value & SND_BBFPRO_MIXER_IDX_MASK;
2432 	old_value = kcontrol->private_value >> SND_BBFPRO_MIXER_VAL_SHIFT;
2433 
2434 	if (uvalue > SND_BBFPRO_MIXER_VAL_MAX)
2435 		return -EINVAL;
2436 
2437 	if (uvalue == old_value)
2438 		return 0;
2439 
2440 	new_val = uvalue & SND_BBFPRO_MIXER_VAL_MASK;
2441 
2442 	kcontrol->private_value = idx
2443 		| (new_val << SND_BBFPRO_MIXER_VAL_SHIFT);
2444 
2445 	err = snd_bbfpro_vol_update(mixer, idx, new_val);
2446 	return err < 0 ? err : 1;
2447 }
2448 
2449 static int snd_bbfpro_vol_resume(struct usb_mixer_elem_list *list)
2450 {
2451 	int pv = list->kctl->private_value;
2452 	u16 idx = pv & SND_BBFPRO_MIXER_IDX_MASK;
2453 	u32 val = (pv >> SND_BBFPRO_MIXER_VAL_SHIFT)
2454 		& SND_BBFPRO_MIXER_VAL_MASK;
2455 	return snd_bbfpro_vol_update(list->mixer, idx, val);
2456 }
2457 
2458 // Predfine elements
2459 static const struct snd_kcontrol_new snd_bbfpro_ctl_control = {
2460 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2461 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2462 	.index = 0,
2463 	.info = snd_bbfpro_ctl_info,
2464 	.get = snd_bbfpro_ctl_get,
2465 	.put = snd_bbfpro_ctl_put
2466 };
2467 
2468 static const struct snd_kcontrol_new snd_bbfpro_vol_control = {
2469 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2470 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
2471 	.index = 0,
2472 	.info = snd_bbfpro_vol_info,
2473 	.get = snd_bbfpro_vol_get,
2474 	.put = snd_bbfpro_vol_put
2475 };
2476 
2477 static int snd_bbfpro_ctl_add(struct usb_mixer_interface *mixer, u8 reg,
2478 			      u8 index, char *name)
2479 {
2480 	struct snd_kcontrol_new knew = snd_bbfpro_ctl_control;
2481 
2482 	knew.name = name;
2483 	knew.private_value = (reg & SND_BBFPRO_CTL_REG_MASK)
2484 		| ((index & SND_BBFPRO_CTL_IDX_MASK)
2485 			<< SND_BBFPRO_CTL_IDX_SHIFT);
2486 
2487 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_ctl_resume,
2488 		&knew, NULL);
2489 }
2490 
2491 static int snd_bbfpro_vol_add(struct usb_mixer_interface *mixer, u16 index,
2492 			      char *name)
2493 {
2494 	struct snd_kcontrol_new knew = snd_bbfpro_vol_control;
2495 
2496 	knew.name = name;
2497 	knew.private_value = index & SND_BBFPRO_MIXER_IDX_MASK;
2498 
2499 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_vol_resume,
2500 		&knew, NULL);
2501 }
2502 
2503 static int snd_bbfpro_controls_create(struct usb_mixer_interface *mixer)
2504 {
2505 	int err, i, o;
2506 	char name[48];
2507 
2508 	static const char * const input[] = {
2509 		"AN1", "AN2", "IN3", "IN4", "AS1", "AS2", "ADAT3",
2510 		"ADAT4", "ADAT5", "ADAT6", "ADAT7", "ADAT8"};
2511 
2512 	static const char * const output[] = {
2513 		"AN1", "AN2", "PH3", "PH4", "AS1", "AS2", "ADAT3", "ADAT4",
2514 		"ADAT5", "ADAT6", "ADAT7", "ADAT8"};
2515 
2516 	for (o = 0 ; o < 12 ; ++o) {
2517 		for (i = 0 ; i < 12 ; ++i) {
2518 			// Line routing
2519 			snprintf(name, sizeof(name),
2520 				 "%s-%s-%s Playback Volume",
2521 				 (i < 2 ? "Mic" : "Line"),
2522 				 input[i], output[o]);
2523 			err = snd_bbfpro_vol_add(mixer, (26 * o + i), name);
2524 			if (err < 0)
2525 				return err;
2526 
2527 			// PCM routing... yes, it is output remapping
2528 			snprintf(name, sizeof(name),
2529 				 "PCM-%s-%s Playback Volume",
2530 				 output[i], output[o]);
2531 			err = snd_bbfpro_vol_add(mixer, (26 * o + 12 + i),
2532 						 name);
2533 			if (err < 0)
2534 				return err;
2535 		}
2536 	}
2537 
2538 	// Control Reg 1
2539 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
2540 				 SND_BBFPRO_CTL_REG1_CLK_OPTICAL,
2541 				 "Sample Clock Source");
2542 	if (err < 0)
2543 		return err;
2544 
2545 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
2546 				 SND_BBFPRO_CTL_REG1_SPDIF_PRO,
2547 				 "IEC958 Pro Mask");
2548 	if (err < 0)
2549 		return err;
2550 
2551 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
2552 				 SND_BBFPRO_CTL_REG1_SPDIF_EMPH,
2553 				 "IEC958 Emphasis");
2554 	if (err < 0)
2555 		return err;
2556 
2557 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
2558 				 SND_BBFPRO_CTL_REG1_SPDIF_OPTICAL,
2559 				 "IEC958 Switch");
2560 	if (err < 0)
2561 		return err;
2562 
2563 	// Control Reg 2
2564 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
2565 				 SND_BBFPRO_CTL_REG2_48V_AN1,
2566 				 "Mic-AN1 48V");
2567 	if (err < 0)
2568 		return err;
2569 
2570 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
2571 				 SND_BBFPRO_CTL_REG2_48V_AN2,
2572 				 "Mic-AN2 48V");
2573 	if (err < 0)
2574 		return err;
2575 
2576 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
2577 				 SND_BBFPRO_CTL_REG2_SENS_IN3,
2578 				 "Line-IN3 Sens.");
2579 	if (err < 0)
2580 		return err;
2581 
2582 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
2583 				 SND_BBFPRO_CTL_REG2_SENS_IN4,
2584 				 "Line-IN4 Sens.");
2585 	if (err < 0)
2586 		return err;
2587 
2588 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
2589 				 SND_BBFPRO_CTL_REG2_PAD_AN1,
2590 				 "Mic-AN1 PAD");
2591 	if (err < 0)
2592 		return err;
2593 
2594 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
2595 				 SND_BBFPRO_CTL_REG2_PAD_AN2,
2596 				 "Mic-AN2 PAD");
2597 	if (err < 0)
2598 		return err;
2599 
2600 	return 0;
2601 }
2602 
2603 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
2604 {
2605 	int err = 0;
2606 
2607 	err = snd_usb_soundblaster_remote_init(mixer);
2608 	if (err < 0)
2609 		return err;
2610 
2611 	switch (mixer->chip->usb_id) {
2612 	/* Tascam US-16x08 */
2613 	case USB_ID(0x0644, 0x8047):
2614 		err = snd_us16x08_controls_create(mixer);
2615 		break;
2616 	case USB_ID(0x041e, 0x3020):
2617 	case USB_ID(0x041e, 0x3040):
2618 	case USB_ID(0x041e, 0x3042):
2619 	case USB_ID(0x041e, 0x30df):
2620 	case USB_ID(0x041e, 0x3048):
2621 		err = snd_audigy2nx_controls_create(mixer);
2622 		if (err < 0)
2623 			break;
2624 		snd_card_ro_proc_new(mixer->chip->card, "audigy2nx",
2625 				     mixer, snd_audigy2nx_proc_read);
2626 		break;
2627 
2628 	/* EMU0204 */
2629 	case USB_ID(0x041e, 0x3f19):
2630 		err = snd_emu0204_controls_create(mixer);
2631 		break;
2632 
2633 	case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
2634 	case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
2635 		err = snd_c400_create_mixer(mixer);
2636 		break;
2637 
2638 	case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
2639 	case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
2640 		err = snd_ftu_create_mixer(mixer);
2641 		break;
2642 
2643 	case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
2644 	case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
2645 	case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
2646 		err = snd_xonar_u1_controls_create(mixer);
2647 		break;
2648 
2649 	case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
2650 		err = snd_microii_controls_create(mixer);
2651 		break;
2652 
2653 	case USB_ID(0x0dba, 0x1000): /* Digidesign Mbox 1 */
2654 		err = snd_mbox1_create_sync_switch(mixer);
2655 		break;
2656 
2657 	case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
2658 		err = snd_nativeinstruments_create_mixer(mixer,
2659 				snd_nativeinstruments_ta6_mixers,
2660 				ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
2661 		break;
2662 
2663 	case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
2664 		err = snd_nativeinstruments_create_mixer(mixer,
2665 				snd_nativeinstruments_ta10_mixers,
2666 				ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
2667 		break;
2668 
2669 	case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
2670 		/* detection is disabled in mixer_maps.c */
2671 		err = snd_create_std_mono_table(mixer, ebox44_table);
2672 		break;
2673 
2674 	case USB_ID(0x1235, 0x8012): /* Focusrite Scarlett 6i6 */
2675 	case USB_ID(0x1235, 0x8002): /* Focusrite Scarlett 8i6 */
2676 	case USB_ID(0x1235, 0x8004): /* Focusrite Scarlett 18i6 */
2677 	case USB_ID(0x1235, 0x8014): /* Focusrite Scarlett 18i8 */
2678 	case USB_ID(0x1235, 0x800c): /* Focusrite Scarlett 18i20 */
2679 		err = snd_scarlett_controls_create(mixer);
2680 		break;
2681 
2682 	case USB_ID(0x1235, 0x8203): /* Focusrite Scarlett 6i6 2nd Gen */
2683 	case USB_ID(0x1235, 0x8204): /* Focusrite Scarlett 18i8 2nd Gen */
2684 	case USB_ID(0x1235, 0x8201): /* Focusrite Scarlett 18i20 2nd Gen */
2685 		err = snd_scarlett_gen2_controls_create(mixer);
2686 		break;
2687 
2688 	case USB_ID(0x041e, 0x323b): /* Creative Sound Blaster E1 */
2689 		err = snd_soundblaster_e1_switch_create(mixer);
2690 		break;
2691 	case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
2692 		err = dell_dock_mixer_init(mixer);
2693 		break;
2694 
2695 	case USB_ID(0x2a39, 0x3fd2): /* RME ADI-2 Pro */
2696 	case USB_ID(0x2a39, 0x3fd3): /* RME ADI-2 DAC */
2697 	case USB_ID(0x2a39, 0x3fd4): /* RME */
2698 		err = snd_rme_controls_create(mixer);
2699 		break;
2700 
2701 	case USB_ID(0x0194f, 0x010c): /* Presonus Studio 1810c */
2702 		err = snd_sc1810_init_mixer(mixer);
2703 		break;
2704 	case USB_ID(0x2a39, 0x3fb0): /* RME Babyface Pro FS */
2705 		err = snd_bbfpro_controls_create(mixer);
2706 		break;
2707 	}
2708 
2709 	return err;
2710 }
2711 
2712 #ifdef CONFIG_PM
2713 void snd_usb_mixer_resume_quirk(struct usb_mixer_interface *mixer)
2714 {
2715 	switch (mixer->chip->usb_id) {
2716 	case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
2717 		dell_dock_mixer_init(mixer);
2718 		break;
2719 	}
2720 }
2721 #endif
2722 
2723 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
2724 				    int unitid)
2725 {
2726 	if (!mixer->rc_cfg)
2727 		return;
2728 	/* unit ids specific to Extigy/Audigy 2 NX: */
2729 	switch (unitid) {
2730 	case 0: /* remote control */
2731 		mixer->rc_urb->dev = mixer->chip->dev;
2732 		usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
2733 		break;
2734 	case 4: /* digital in jack */
2735 	case 7: /* line in jacks */
2736 	case 19: /* speaker out jacks */
2737 	case 20: /* headphones out jack */
2738 		break;
2739 	/* live24ext: 4 = line-in jack */
2740 	case 3:	/* hp-out jack (may actuate Mute) */
2741 		if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2742 		    mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
2743 			snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
2744 		break;
2745 	default:
2746 		usb_audio_dbg(mixer->chip, "memory change in unknown unit %d\n", unitid);
2747 		break;
2748 	}
2749 }
2750 
2751 static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
2752 					 struct usb_mixer_elem_info *cval,
2753 					 struct snd_kcontrol *kctl)
2754 {
2755 	/* Approximation using 10 ranges based on output measurement on hw v1.2.
2756 	 * This seems close to the cubic mapping e.g. alsamixer uses. */
2757 	static const DECLARE_TLV_DB_RANGE(scale,
2758 		 0,  1, TLV_DB_MINMAX_ITEM(-5300, -4970),
2759 		 2,  5, TLV_DB_MINMAX_ITEM(-4710, -4160),
2760 		 6,  7, TLV_DB_MINMAX_ITEM(-3884, -3710),
2761 		 8, 14, TLV_DB_MINMAX_ITEM(-3443, -2560),
2762 		15, 16, TLV_DB_MINMAX_ITEM(-2475, -2324),
2763 		17, 19, TLV_DB_MINMAX_ITEM(-2228, -2031),
2764 		20, 26, TLV_DB_MINMAX_ITEM(-1910, -1393),
2765 		27, 31, TLV_DB_MINMAX_ITEM(-1322, -1032),
2766 		32, 40, TLV_DB_MINMAX_ITEM(-968, -490),
2767 		41, 50, TLV_DB_MINMAX_ITEM(-441, 0),
2768 	);
2769 
2770 	if (cval->min == 0 && cval->max == 50) {
2771 		usb_audio_info(mixer->chip, "applying DragonFly dB scale quirk (0-50 variant)\n");
2772 		kctl->tlv.p = scale;
2773 		kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
2774 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
2775 
2776 	} else if (cval->min == 0 && cval->max <= 1000) {
2777 		/* Some other clearly broken DragonFly variant.
2778 		 * At least a 0..53 variant (hw v1.0) exists.
2779 		 */
2780 		usb_audio_info(mixer->chip, "ignoring too narrow dB range on a DragonFly device");
2781 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
2782 	}
2783 }
2784 
2785 void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
2786 				  struct usb_mixer_elem_info *cval, int unitid,
2787 				  struct snd_kcontrol *kctl)
2788 {
2789 	switch (mixer->chip->usb_id) {
2790 	case USB_ID(0x21b4, 0x0081): /* AudioQuest DragonFly */
2791 		if (unitid == 7 && cval->control == UAC_FU_VOLUME)
2792 			snd_dragonfly_quirk_db_scale(mixer, cval, kctl);
2793 		break;
2794 	/* lowest playback value is muted on C-Media devices */
2795 	case USB_ID(0x0d8c, 0x000c):
2796 	case USB_ID(0x0d8c, 0x0014):
2797 		if (strstr(kctl->id.name, "Playback"))
2798 			cval->min_mute = 1;
2799 		break;
2800 	}
2801 }
2802 
2803