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