xref: /openbmc/linux/sound/usb/mixer_quirks.c (revision 020c5260)
1 /*
2  *   USB Audio Driver for ALSA
3  *
4  *   Quirks and vendor-specific extensions for mixer interfaces
5  *
6  *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7  *
8  *   Many codes borrowed from audio.c by
9  *	    Alan Cox (alan@lxorguk.ukuu.org.uk)
10  *	    Thomas Sailer (sailer@ife.ee.ethz.ch)
11  *
12  *   Audio Advantage Micro II support added by:
13  *	    Przemek Rudy (prudy1@o2.pl)
14  *
15  *   This program is free software; you can redistribute it and/or modify
16  *   it under the terms of the GNU General Public License as published by
17  *   the Free Software Foundation; either version 2 of the License, or
18  *   (at your option) any later version.
19  *
20  *   This program is distributed in the hope that it will be useful,
21  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   GNU General Public License for more details.
24  *
25  *   You should have received a copy of the GNU General Public License
26  *   along with this program; if not, write to the Free Software
27  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28  */
29 
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/usb.h>
33 #include <linux/usb/audio.h>
34 
35 #include <sound/asoundef.h>
36 #include <sound/core.h>
37 #include <sound/control.h>
38 #include <sound/hwdep.h>
39 #include <sound/info.h>
40 #include <sound/tlv.h>
41 
42 #include "usbaudio.h"
43 #include "mixer.h"
44 #include "mixer_quirks.h"
45 #include "mixer_scarlett.h"
46 #include "mixer_us16x08.h"
47 #include "helper.h"
48 
49 extern struct snd_kcontrol_new *snd_usb_feature_unit_ctl;
50 
51 struct std_mono_table {
52 	unsigned int unitid, control, cmask;
53 	int val_type;
54 	const char *name;
55 	snd_kcontrol_tlv_rw_t *tlv_callback;
56 };
57 
58 /* This function allows for the creation of standard UAC controls.
59  * See the quirks for M-Audio FTUs or Ebox-44.
60  * If you don't want to set a TLV callback pass NULL.
61  *
62  * Since there doesn't seem to be a devices that needs a multichannel
63  * version, we keep it mono for simplicity.
64  */
65 static int snd_create_std_mono_ctl_offset(struct usb_mixer_interface *mixer,
66 				unsigned int unitid,
67 				unsigned int control,
68 				unsigned int cmask,
69 				int val_type,
70 				unsigned int idx_off,
71 				const char *name,
72 				snd_kcontrol_tlv_rw_t *tlv_callback)
73 {
74 	struct usb_mixer_elem_info *cval;
75 	struct snd_kcontrol *kctl;
76 
77 	cval = kzalloc(sizeof(*cval), GFP_KERNEL);
78 	if (!cval)
79 		return -ENOMEM;
80 
81 	snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
82 	cval->val_type = val_type;
83 	cval->channels = 1;
84 	cval->control = control;
85 	cval->cmask = cmask;
86 	cval->idx_off = idx_off;
87 
88 	/* get_min_max() is called only for integer volumes later,
89 	 * so provide a short-cut for booleans */
90 	cval->min = 0;
91 	cval->max = 1;
92 	cval->res = 0;
93 	cval->dBmin = 0;
94 	cval->dBmax = 0;
95 
96 	/* Create control */
97 	kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
98 	if (!kctl) {
99 		kfree(cval);
100 		return -ENOMEM;
101 	}
102 
103 	/* Set name */
104 	snprintf(kctl->id.name, sizeof(kctl->id.name), name);
105 	kctl->private_free = snd_usb_mixer_elem_free;
106 
107 	/* set TLV */
108 	if (tlv_callback) {
109 		kctl->tlv.c = tlv_callback;
110 		kctl->vd[0].access |=
111 			SNDRV_CTL_ELEM_ACCESS_TLV_READ |
112 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
113 	}
114 	/* Add control to mixer */
115 	return snd_usb_mixer_add_control(&cval->head, kctl);
116 }
117 
118 static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
119 				unsigned int unitid,
120 				unsigned int control,
121 				unsigned int cmask,
122 				int val_type,
123 				const char *name,
124 				snd_kcontrol_tlv_rw_t *tlv_callback)
125 {
126 	return snd_create_std_mono_ctl_offset(mixer, unitid, control, cmask,
127 		val_type, 0 /* Offset */, name, tlv_callback);
128 }
129 
130 /*
131  * Create a set of standard UAC controls from a table
132  */
133 static int snd_create_std_mono_table(struct usb_mixer_interface *mixer,
134 				struct std_mono_table *t)
135 {
136 	int err;
137 
138 	while (t->name != NULL) {
139 		err = snd_create_std_mono_ctl(mixer, t->unitid, t->control,
140 				t->cmask, t->val_type, t->name, t->tlv_callback);
141 		if (err < 0)
142 			return err;
143 		t++;
144 	}
145 
146 	return 0;
147 }
148 
149 static int add_single_ctl_with_resume(struct usb_mixer_interface *mixer,
150 				      int id,
151 				      usb_mixer_elem_resume_func_t resume,
152 				      const struct snd_kcontrol_new *knew,
153 				      struct usb_mixer_elem_list **listp)
154 {
155 	struct usb_mixer_elem_list *list;
156 	struct snd_kcontrol *kctl;
157 
158 	list = kzalloc(sizeof(*list), GFP_KERNEL);
159 	if (!list)
160 		return -ENOMEM;
161 	if (listp)
162 		*listp = list;
163 	list->mixer = mixer;
164 	list->id = id;
165 	list->resume = resume;
166 	kctl = snd_ctl_new1(knew, list);
167 	if (!kctl) {
168 		kfree(list);
169 		return -ENOMEM;
170 	}
171 	kctl->private_free = snd_usb_mixer_elem_free;
172 	return snd_usb_mixer_add_control(list, kctl);
173 }
174 
175 /*
176  * Sound Blaster remote control configuration
177  *
178  * format of remote control data:
179  * Extigy:       xx 00
180  * Audigy 2 NX:  06 80 xx 00 00 00
181  * Live! 24-bit: 06 80 xx yy 22 83
182  */
183 static const struct rc_config {
184 	u32 usb_id;
185 	u8  offset;
186 	u8  length;
187 	u8  packet_length;
188 	u8  min_packet_length; /* minimum accepted length of the URB result */
189 	u8  mute_mixer_id;
190 	u32 mute_code;
191 } rc_configs[] = {
192 	{ USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
193 	{ USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
194 	{ USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
195 	{ USB_ID(0x041e, 0x3042), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 */
196 	{ USB_ID(0x041e, 0x30df), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
197 	{ USB_ID(0x041e, 0x3237), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
198 	{ USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
199 };
200 
201 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
202 {
203 	struct usb_mixer_interface *mixer = urb->context;
204 	const struct rc_config *rc = mixer->rc_cfg;
205 	u32 code;
206 
207 	if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
208 		return;
209 
210 	code = mixer->rc_buffer[rc->offset];
211 	if (rc->length == 2)
212 		code |= mixer->rc_buffer[rc->offset + 1] << 8;
213 
214 	/* the Mute button actually changes the mixer control */
215 	if (code == rc->mute_code)
216 		snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
217 	mixer->rc_code = code;
218 	wmb();
219 	wake_up(&mixer->rc_waitq);
220 }
221 
222 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
223 				     long count, loff_t *offset)
224 {
225 	struct usb_mixer_interface *mixer = hw->private_data;
226 	int err;
227 	u32 rc_code;
228 
229 	if (count != 1 && count != 4)
230 		return -EINVAL;
231 	err = wait_event_interruptible(mixer->rc_waitq,
232 				       (rc_code = xchg(&mixer->rc_code, 0)) != 0);
233 	if (err == 0) {
234 		if (count == 1)
235 			err = put_user(rc_code, buf);
236 		else
237 			err = put_user(rc_code, (u32 __user *)buf);
238 	}
239 	return err < 0 ? err : count;
240 }
241 
242 static unsigned int snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
243 					    poll_table *wait)
244 {
245 	struct usb_mixer_interface *mixer = hw->private_data;
246 
247 	poll_wait(file, &mixer->rc_waitq, wait);
248 	return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
249 }
250 
251 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
252 {
253 	struct snd_hwdep *hwdep;
254 	int err, len, i;
255 
256 	for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
257 		if (rc_configs[i].usb_id == mixer->chip->usb_id)
258 			break;
259 	if (i >= ARRAY_SIZE(rc_configs))
260 		return 0;
261 	mixer->rc_cfg = &rc_configs[i];
262 
263 	len = mixer->rc_cfg->packet_length;
264 
265 	init_waitqueue_head(&mixer->rc_waitq);
266 	err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
267 	if (err < 0)
268 		return err;
269 	snprintf(hwdep->name, sizeof(hwdep->name),
270 		 "%s remote control", mixer->chip->card->shortname);
271 	hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
272 	hwdep->private_data = mixer;
273 	hwdep->ops.read = snd_usb_sbrc_hwdep_read;
274 	hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
275 	hwdep->exclusive = 1;
276 
277 	mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
278 	if (!mixer->rc_urb)
279 		return -ENOMEM;
280 	mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
281 	if (!mixer->rc_setup_packet) {
282 		usb_free_urb(mixer->rc_urb);
283 		mixer->rc_urb = NULL;
284 		return -ENOMEM;
285 	}
286 	mixer->rc_setup_packet->bRequestType =
287 		USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
288 	mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
289 	mixer->rc_setup_packet->wValue = cpu_to_le16(0);
290 	mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
291 	mixer->rc_setup_packet->wLength = cpu_to_le16(len);
292 	usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
293 			     usb_rcvctrlpipe(mixer->chip->dev, 0),
294 			     (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
295 			     snd_usb_soundblaster_remote_complete, mixer);
296 	return 0;
297 }
298 
299 #define snd_audigy2nx_led_info		snd_ctl_boolean_mono_info
300 
301 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
302 {
303 	ucontrol->value.integer.value[0] = kcontrol->private_value >> 8;
304 	return 0;
305 }
306 
307 static int snd_audigy2nx_led_update(struct usb_mixer_interface *mixer,
308 				    int value, int index)
309 {
310 	struct snd_usb_audio *chip = mixer->chip;
311 	int err;
312 
313 	err = snd_usb_lock_shutdown(chip);
314 	if (err < 0)
315 		return err;
316 
317 	if (chip->usb_id == USB_ID(0x041e, 0x3042))
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, 0, NULL, 0);
322 	/* USB X-Fi S51 Pro */
323 	if (chip->usb_id == USB_ID(0x041e, 0x30df))
324 		err = snd_usb_ctl_msg(chip->dev,
325 			      usb_sndctrlpipe(chip->dev, 0), 0x24,
326 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
327 			      !value, 0, NULL, 0);
328 	else
329 		err = snd_usb_ctl_msg(chip->dev,
330 			      usb_sndctrlpipe(chip->dev, 0), 0x24,
331 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
332 			      value, index + 2, NULL, 0);
333 	snd_usb_unlock_shutdown(chip);
334 	return err;
335 }
336 
337 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol,
338 				 struct snd_ctl_elem_value *ucontrol)
339 {
340 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
341 	struct usb_mixer_interface *mixer = list->mixer;
342 	int index = kcontrol->private_value & 0xff;
343 	unsigned int value = ucontrol->value.integer.value[0];
344 	int old_value = kcontrol->private_value >> 8;
345 	int err;
346 
347 	if (value > 1)
348 		return -EINVAL;
349 	if (value == old_value)
350 		return 0;
351 	kcontrol->private_value = (value << 8) | index;
352 	err = snd_audigy2nx_led_update(mixer, value, index);
353 	return err < 0 ? err : 1;
354 }
355 
356 static int snd_audigy2nx_led_resume(struct usb_mixer_elem_list *list)
357 {
358 	int priv_value = list->kctl->private_value;
359 
360 	return snd_audigy2nx_led_update(list->mixer, priv_value >> 8,
361 					priv_value & 0xff);
362 }
363 
364 /* name and private_value are set dynamically */
365 static struct snd_kcontrol_new snd_audigy2nx_control = {
366 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
367 	.info = snd_audigy2nx_led_info,
368 	.get = snd_audigy2nx_led_get,
369 	.put = snd_audigy2nx_led_put,
370 };
371 
372 static const char * const snd_audigy2nx_led_names[] = {
373 	"CMSS LED Switch",
374 	"Power LED Switch",
375 	"Dolby Digital LED Switch",
376 };
377 
378 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
379 {
380 	int i, err;
381 
382 	for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_led_names); ++i) {
383 		struct snd_kcontrol_new knew;
384 
385 		/* USB X-Fi S51 doesn't have a CMSS LED */
386 		if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
387 			continue;
388 		/* USB X-Fi S51 Pro doesn't have one either */
389 		if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
390 			continue;
391 		if (i > 1 && /* Live24ext has 2 LEDs only */
392 			(mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
393 			 mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
394 			 mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
395 			 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
396 			break;
397 
398 		knew = snd_audigy2nx_control;
399 		knew.name = snd_audigy2nx_led_names[i];
400 		knew.private_value = (1 << 8) | i; /* LED on as default */
401 		err = add_single_ctl_with_resume(mixer, 0,
402 						 snd_audigy2nx_led_resume,
403 						 &knew, NULL);
404 		if (err < 0)
405 			return err;
406 	}
407 	return 0;
408 }
409 
410 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
411 				    struct snd_info_buffer *buffer)
412 {
413 	static const struct sb_jack {
414 		int unitid;
415 		const char *name;
416 	}  jacks_audigy2nx[] = {
417 		{4,  "dig in "},
418 		{7,  "line in"},
419 		{19, "spk out"},
420 		{20, "hph out"},
421 		{-1, NULL}
422 	}, jacks_live24ext[] = {
423 		{4,  "line in"}, /* &1=Line, &2=Mic*/
424 		{3,  "hph out"}, /* headphones */
425 		{0,  "RC     "}, /* last command, 6 bytes see rc_config above */
426 		{-1, NULL}
427 	};
428 	const struct sb_jack *jacks;
429 	struct usb_mixer_interface *mixer = entry->private_data;
430 	int i, err;
431 	u8 buf[3];
432 
433 	snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
434 	if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
435 		jacks = jacks_audigy2nx;
436 	else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
437 		 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
438 		jacks = jacks_live24ext;
439 	else
440 		return;
441 
442 	for (i = 0; jacks[i].name; ++i) {
443 		snd_iprintf(buffer, "%s: ", jacks[i].name);
444 		err = snd_usb_lock_shutdown(mixer->chip);
445 		if (err < 0)
446 			return;
447 		err = snd_usb_ctl_msg(mixer->chip->dev,
448 				      usb_rcvctrlpipe(mixer->chip->dev, 0),
449 				      UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
450 				      USB_RECIP_INTERFACE, 0,
451 				      jacks[i].unitid << 8, buf, 3);
452 		snd_usb_unlock_shutdown(mixer->chip);
453 		if (err == 3 && (buf[0] == 3 || buf[0] == 6))
454 			snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
455 		else
456 			snd_iprintf(buffer, "?\n");
457 	}
458 }
459 
460 /* EMU0204 */
461 static int snd_emu0204_ch_switch_info(struct snd_kcontrol *kcontrol,
462 				      struct snd_ctl_elem_info *uinfo)
463 {
464 	static const char * const texts[2] = {"1/2", "3/4"};
465 
466 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
467 }
468 
469 static int snd_emu0204_ch_switch_get(struct snd_kcontrol *kcontrol,
470 				     struct snd_ctl_elem_value *ucontrol)
471 {
472 	ucontrol->value.enumerated.item[0] = kcontrol->private_value;
473 	return 0;
474 }
475 
476 static int snd_emu0204_ch_switch_update(struct usb_mixer_interface *mixer,
477 					int value)
478 {
479 	struct snd_usb_audio *chip = mixer->chip;
480 	int err;
481 	unsigned char buf[2];
482 
483 	err = snd_usb_lock_shutdown(chip);
484 	if (err < 0)
485 		return err;
486 
487 	buf[0] = 0x01;
488 	buf[1] = value ? 0x02 : 0x01;
489 	err = snd_usb_ctl_msg(chip->dev,
490 		      usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
491 		      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
492 		      0x0400, 0x0e00, buf, 2);
493 	snd_usb_unlock_shutdown(chip);
494 	return err;
495 }
496 
497 static int snd_emu0204_ch_switch_put(struct snd_kcontrol *kcontrol,
498 				     struct snd_ctl_elem_value *ucontrol)
499 {
500 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
501 	struct usb_mixer_interface *mixer = list->mixer;
502 	unsigned int value = ucontrol->value.enumerated.item[0];
503 	int err;
504 
505 	if (value > 1)
506 		return -EINVAL;
507 
508 	if (value == kcontrol->private_value)
509 		return 0;
510 
511 	kcontrol->private_value = value;
512 	err = snd_emu0204_ch_switch_update(mixer, value);
513 	return err < 0 ? err : 1;
514 }
515 
516 static int snd_emu0204_ch_switch_resume(struct usb_mixer_elem_list *list)
517 {
518 	return snd_emu0204_ch_switch_update(list->mixer,
519 					    list->kctl->private_value);
520 }
521 
522 static struct snd_kcontrol_new snd_emu0204_control = {
523 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
524 	.name = "Front Jack Channels",
525 	.info = snd_emu0204_ch_switch_info,
526 	.get = snd_emu0204_ch_switch_get,
527 	.put = snd_emu0204_ch_switch_put,
528 	.private_value = 0,
529 };
530 
531 static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
532 {
533 	return add_single_ctl_with_resume(mixer, 0,
534 					  snd_emu0204_ch_switch_resume,
535 					  &snd_emu0204_control, NULL);
536 }
537 
538 /* ASUS Xonar U1 / U3 controls */
539 
540 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
541 				   struct snd_ctl_elem_value *ucontrol)
542 {
543 	ucontrol->value.integer.value[0] = !!(kcontrol->private_value & 0x02);
544 	return 0;
545 }
546 
547 static int snd_xonar_u1_switch_update(struct usb_mixer_interface *mixer,
548 				      unsigned char status)
549 {
550 	struct snd_usb_audio *chip = mixer->chip;
551 	int err;
552 
553 	err = snd_usb_lock_shutdown(chip);
554 	if (err < 0)
555 		return err;
556 	err = snd_usb_ctl_msg(chip->dev,
557 			      usb_sndctrlpipe(chip->dev, 0), 0x08,
558 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
559 			      50, 0, &status, 1);
560 	snd_usb_unlock_shutdown(chip);
561 	return err;
562 }
563 
564 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
565 				   struct snd_ctl_elem_value *ucontrol)
566 {
567 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
568 	u8 old_status, new_status;
569 	int err;
570 
571 	old_status = kcontrol->private_value;
572 	if (ucontrol->value.integer.value[0])
573 		new_status = old_status | 0x02;
574 	else
575 		new_status = old_status & ~0x02;
576 	if (new_status == old_status)
577 		return 0;
578 
579 	kcontrol->private_value = new_status;
580 	err = snd_xonar_u1_switch_update(list->mixer, new_status);
581 	return err < 0 ? err : 1;
582 }
583 
584 static int snd_xonar_u1_switch_resume(struct usb_mixer_elem_list *list)
585 {
586 	return snd_xonar_u1_switch_update(list->mixer,
587 					  list->kctl->private_value);
588 }
589 
590 static struct snd_kcontrol_new snd_xonar_u1_output_switch = {
591 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
592 	.name = "Digital Playback Switch",
593 	.info = snd_ctl_boolean_mono_info,
594 	.get = snd_xonar_u1_switch_get,
595 	.put = snd_xonar_u1_switch_put,
596 	.private_value = 0x05,
597 };
598 
599 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
600 {
601 	return add_single_ctl_with_resume(mixer, 0,
602 					  snd_xonar_u1_switch_resume,
603 					  &snd_xonar_u1_output_switch, NULL);
604 }
605 
606 /* Digidesign Mbox 1 clock source switch (internal/spdif) */
607 
608 static int snd_mbox1_switch_get(struct snd_kcontrol *kctl,
609 				struct snd_ctl_elem_value *ucontrol)
610 {
611 	ucontrol->value.enumerated.item[0] = kctl->private_value;
612 	return 0;
613 }
614 
615 static int snd_mbox1_switch_update(struct usb_mixer_interface *mixer, int val)
616 {
617 	struct snd_usb_audio *chip = mixer->chip;
618 	int err;
619 	unsigned char buff[3];
620 
621 	err = snd_usb_lock_shutdown(chip);
622 	if (err < 0)
623 		return err;
624 
625 	/* Prepare for magic command to toggle clock source */
626 	err = snd_usb_ctl_msg(chip->dev,
627 				usb_rcvctrlpipe(chip->dev, 0), 0x81,
628 				USB_DIR_IN |
629 				USB_TYPE_CLASS |
630 				USB_RECIP_INTERFACE, 0x00, 0x500, buff, 1);
631 	if (err < 0)
632 		goto err;
633 	err = snd_usb_ctl_msg(chip->dev,
634 				usb_rcvctrlpipe(chip->dev, 0), 0x81,
635 				USB_DIR_IN |
636 				USB_TYPE_CLASS |
637 				USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
638 	if (err < 0)
639 		goto err;
640 
641 	/* 2 possibilities:	Internal    -> send sample rate
642 	 *			S/PDIF sync -> send zeroes
643 	 * NB: Sample rate locked to 48kHz on purpose to
644 	 *     prevent user from resetting the sample rate
645 	 *     while S/PDIF sync is enabled and confusing
646 	 *     this configuration.
647 	 */
648 	if (val == 0) {
649 		buff[0] = 0x80;
650 		buff[1] = 0xbb;
651 		buff[2] = 0x00;
652 	} else {
653 		buff[0] = buff[1] = buff[2] = 0x00;
654 	}
655 
656 	/* Send the magic command to toggle the clock source */
657 	err = snd_usb_ctl_msg(chip->dev,
658 				usb_sndctrlpipe(chip->dev, 0), 0x1,
659 				USB_TYPE_CLASS |
660 				USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
661 	if (err < 0)
662 		goto err;
663 	err = snd_usb_ctl_msg(chip->dev,
664 				usb_rcvctrlpipe(chip->dev, 0), 0x81,
665 				USB_DIR_IN |
666 				USB_TYPE_CLASS |
667 				USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
668 	if (err < 0)
669 		goto err;
670 	err = snd_usb_ctl_msg(chip->dev,
671 				usb_rcvctrlpipe(chip->dev, 0), 0x81,
672 				USB_DIR_IN |
673 				USB_TYPE_CLASS |
674 				USB_RECIP_ENDPOINT, 0x100, 0x2, buff, 3);
675 	if (err < 0)
676 		goto err;
677 
678 err:
679 	snd_usb_unlock_shutdown(chip);
680 	return err;
681 }
682 
683 static int snd_mbox1_switch_put(struct snd_kcontrol *kctl,
684 				struct snd_ctl_elem_value *ucontrol)
685 {
686 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
687 	struct usb_mixer_interface *mixer = list->mixer;
688 	int err;
689 	bool cur_val, new_val;
690 
691 	cur_val = kctl->private_value;
692 	new_val = ucontrol->value.enumerated.item[0];
693 	if (cur_val == new_val)
694 		return 0;
695 
696 	kctl->private_value = new_val;
697 	err = snd_mbox1_switch_update(mixer, new_val);
698 	return err < 0 ? err : 1;
699 }
700 
701 static int snd_mbox1_switch_info(struct snd_kcontrol *kcontrol,
702 				 struct snd_ctl_elem_info *uinfo)
703 {
704 	static const char *const texts[2] = {
705 		"Internal",
706 		"S/PDIF"
707 	};
708 
709 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
710 }
711 
712 static int snd_mbox1_switch_resume(struct usb_mixer_elem_list *list)
713 {
714 	return snd_mbox1_switch_update(list->mixer, list->kctl->private_value);
715 }
716 
717 static struct snd_kcontrol_new snd_mbox1_switch = {
718 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
719 	.name = "Clock Source",
720 	.index = 0,
721 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
722 	.info = snd_mbox1_switch_info,
723 	.get = snd_mbox1_switch_get,
724 	.put = snd_mbox1_switch_put,
725 	.private_value = 0
726 };
727 
728 static int snd_mbox1_create_sync_switch(struct usb_mixer_interface *mixer)
729 {
730 	return add_single_ctl_with_resume(mixer, 0,
731 					  snd_mbox1_switch_resume,
732 					  &snd_mbox1_switch, NULL);
733 }
734 
735 /* Native Instruments device quirks */
736 
737 #define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
738 
739 static int snd_ni_control_init_val(struct usb_mixer_interface *mixer,
740 				   struct snd_kcontrol *kctl)
741 {
742 	struct usb_device *dev = mixer->chip->dev;
743 	unsigned int pval = kctl->private_value;
744 	u8 value;
745 	int err;
746 
747 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
748 			      (pval >> 16) & 0xff,
749 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
750 			      0, pval & 0xffff, &value, 1);
751 	if (err < 0) {
752 		dev_err(&dev->dev,
753 			"unable to issue vendor read request (ret = %d)", err);
754 		return err;
755 	}
756 
757 	kctl->private_value |= (value << 24);
758 	return 0;
759 }
760 
761 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
762 					     struct snd_ctl_elem_value *ucontrol)
763 {
764 	ucontrol->value.integer.value[0] = kcontrol->private_value >> 24;
765 	return 0;
766 }
767 
768 static int snd_ni_update_cur_val(struct usb_mixer_elem_list *list)
769 {
770 	struct snd_usb_audio *chip = list->mixer->chip;
771 	unsigned int pval = list->kctl->private_value;
772 	int err;
773 
774 	err = snd_usb_lock_shutdown(chip);
775 	if (err < 0)
776 		return err;
777 	err = usb_control_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
778 			      (pval >> 16) & 0xff,
779 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
780 			      pval >> 24, pval & 0xffff, NULL, 0, 1000);
781 	snd_usb_unlock_shutdown(chip);
782 	return err;
783 }
784 
785 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
786 					     struct snd_ctl_elem_value *ucontrol)
787 {
788 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
789 	u8 oldval = (kcontrol->private_value >> 24) & 0xff;
790 	u8 newval = ucontrol->value.integer.value[0];
791 	int err;
792 
793 	if (oldval == newval)
794 		return 0;
795 
796 	kcontrol->private_value &= ~(0xff << 24);
797 	kcontrol->private_value |= (unsigned int)newval << 24;
798 	err = snd_ni_update_cur_val(list);
799 	return err < 0 ? err : 1;
800 }
801 
802 static struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
803 	{
804 		.name = "Direct Thru Channel A",
805 		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
806 	},
807 	{
808 		.name = "Direct Thru Channel B",
809 		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
810 	},
811 	{
812 		.name = "Phono Input Channel A",
813 		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
814 	},
815 	{
816 		.name = "Phono Input Channel B",
817 		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
818 	},
819 };
820 
821 static struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
822 	{
823 		.name = "Direct Thru Channel A",
824 		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
825 	},
826 	{
827 		.name = "Direct Thru Channel B",
828 		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
829 	},
830 	{
831 		.name = "Direct Thru Channel C",
832 		.private_value = _MAKE_NI_CONTROL(0x01, 0x07),
833 	},
834 	{
835 		.name = "Direct Thru Channel D",
836 		.private_value = _MAKE_NI_CONTROL(0x01, 0x09),
837 	},
838 	{
839 		.name = "Phono Input Channel A",
840 		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
841 	},
842 	{
843 		.name = "Phono Input Channel B",
844 		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
845 	},
846 	{
847 		.name = "Phono Input Channel C",
848 		.private_value = _MAKE_NI_CONTROL(0x02, 0x07),
849 	},
850 	{
851 		.name = "Phono Input Channel D",
852 		.private_value = _MAKE_NI_CONTROL(0x02, 0x09),
853 	},
854 };
855 
856 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
857 					      const struct snd_kcontrol_new *kc,
858 					      unsigned int count)
859 {
860 	int i, err = 0;
861 	struct snd_kcontrol_new template = {
862 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
863 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
864 		.get = snd_nativeinstruments_control_get,
865 		.put = snd_nativeinstruments_control_put,
866 		.info = snd_ctl_boolean_mono_info,
867 	};
868 
869 	for (i = 0; i < count; i++) {
870 		struct usb_mixer_elem_list *list;
871 
872 		template.name = kc[i].name;
873 		template.private_value = kc[i].private_value;
874 
875 		err = add_single_ctl_with_resume(mixer, 0,
876 						 snd_ni_update_cur_val,
877 						 &template, &list);
878 		if (err < 0)
879 			break;
880 		snd_ni_control_init_val(mixer, list->kctl);
881 	}
882 
883 	return err;
884 }
885 
886 /* M-Audio FastTrack Ultra quirks */
887 /* FTU Effect switch (also used by C400/C600) */
888 static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
889 					struct snd_ctl_elem_info *uinfo)
890 {
891 	static const char *const texts[8] = {
892 		"Room 1", "Room 2", "Room 3", "Hall 1",
893 		"Hall 2", "Plate", "Delay", "Echo"
894 	};
895 
896 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
897 }
898 
899 static int snd_ftu_eff_switch_init(struct usb_mixer_interface *mixer,
900 				   struct snd_kcontrol *kctl)
901 {
902 	struct usb_device *dev = mixer->chip->dev;
903 	unsigned int pval = kctl->private_value;
904 	int err;
905 	unsigned char value[2];
906 
907 	value[0] = 0x00;
908 	value[1] = 0x00;
909 
910 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
911 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
912 			      pval & 0xff00,
913 			      snd_usb_ctrl_intf(mixer->chip) | ((pval & 0xff) << 8),
914 			      value, 2);
915 	if (err < 0)
916 		return err;
917 
918 	kctl->private_value |= value[0] << 24;
919 	return 0;
920 }
921 
922 static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
923 					struct snd_ctl_elem_value *ucontrol)
924 {
925 	ucontrol->value.enumerated.item[0] = kctl->private_value >> 24;
926 	return 0;
927 }
928 
929 static int snd_ftu_eff_switch_update(struct usb_mixer_elem_list *list)
930 {
931 	struct snd_usb_audio *chip = list->mixer->chip;
932 	unsigned int pval = list->kctl->private_value;
933 	unsigned char value[2];
934 	int err;
935 
936 	value[0] = pval >> 24;
937 	value[1] = 0;
938 
939 	err = snd_usb_lock_shutdown(chip);
940 	if (err < 0)
941 		return err;
942 	err = snd_usb_ctl_msg(chip->dev,
943 			      usb_sndctrlpipe(chip->dev, 0),
944 			      UAC_SET_CUR,
945 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
946 			      pval & 0xff00,
947 			      snd_usb_ctrl_intf(chip) | ((pval & 0xff) << 8),
948 			      value, 2);
949 	snd_usb_unlock_shutdown(chip);
950 	return err;
951 }
952 
953 static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
954 					struct snd_ctl_elem_value *ucontrol)
955 {
956 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
957 	unsigned int pval = list->kctl->private_value;
958 	int cur_val, err, new_val;
959 
960 	cur_val = pval >> 24;
961 	new_val = ucontrol->value.enumerated.item[0];
962 	if (cur_val == new_val)
963 		return 0;
964 
965 	kctl->private_value &= ~(0xff << 24);
966 	kctl->private_value |= new_val << 24;
967 	err = snd_ftu_eff_switch_update(list);
968 	return err < 0 ? err : 1;
969 }
970 
971 static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer,
972 	int validx, int bUnitID)
973 {
974 	static struct snd_kcontrol_new template = {
975 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
976 		.name = "Effect Program Switch",
977 		.index = 0,
978 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
979 		.info = snd_ftu_eff_switch_info,
980 		.get = snd_ftu_eff_switch_get,
981 		.put = snd_ftu_eff_switch_put
982 	};
983 	struct usb_mixer_elem_list *list;
984 	int err;
985 
986 	err = add_single_ctl_with_resume(mixer, bUnitID,
987 					 snd_ftu_eff_switch_update,
988 					 &template, &list);
989 	if (err < 0)
990 		return err;
991 	list->kctl->private_value = (validx << 8) | bUnitID;
992 	snd_ftu_eff_switch_init(mixer, list->kctl);
993 	return 0;
994 }
995 
996 /* Create volume controls for FTU devices*/
997 static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
998 {
999 	char name[64];
1000 	unsigned int control, cmask;
1001 	int in, out, err;
1002 
1003 	const unsigned int id = 5;
1004 	const int val_type = USB_MIXER_S16;
1005 
1006 	for (out = 0; out < 8; out++) {
1007 		control = out + 1;
1008 		for (in = 0; in < 8; in++) {
1009 			cmask = 1 << in;
1010 			snprintf(name, sizeof(name),
1011 				"AIn%d - Out%d Capture Volume",
1012 				in  + 1, 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 		for (in = 8; in < 16; in++) {
1020 			cmask = 1 << in;
1021 			snprintf(name, sizeof(name),
1022 				"DIn%d - Out%d Playback Volume",
1023 				in - 7, out + 1);
1024 			err = snd_create_std_mono_ctl(mixer, id, control,
1025 							cmask, val_type, name,
1026 							&snd_usb_mixer_vol_tlv);
1027 			if (err < 0)
1028 				return err;
1029 		}
1030 	}
1031 
1032 	return 0;
1033 }
1034 
1035 /* This control needs a volume quirk, see mixer.c */
1036 static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1037 {
1038 	static const char name[] = "Effect Volume";
1039 	const unsigned int id = 6;
1040 	const int val_type = USB_MIXER_U8;
1041 	const unsigned int control = 2;
1042 	const unsigned int cmask = 0;
1043 
1044 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1045 					name, snd_usb_mixer_vol_tlv);
1046 }
1047 
1048 /* This control needs a volume quirk, see mixer.c */
1049 static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1050 {
1051 	static const char name[] = "Effect Duration";
1052 	const unsigned int id = 6;
1053 	const int val_type = USB_MIXER_S16;
1054 	const unsigned int control = 3;
1055 	const unsigned int cmask = 0;
1056 
1057 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1058 					name, snd_usb_mixer_vol_tlv);
1059 }
1060 
1061 /* This control needs a volume quirk, see mixer.c */
1062 static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1063 {
1064 	static const char name[] = "Effect Feedback Volume";
1065 	const unsigned int id = 6;
1066 	const int val_type = USB_MIXER_U8;
1067 	const unsigned int control = 4;
1068 	const unsigned int cmask = 0;
1069 
1070 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1071 					name, NULL);
1072 }
1073 
1074 static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
1075 {
1076 	unsigned int cmask;
1077 	int err, ch;
1078 	char name[48];
1079 
1080 	const unsigned int id = 7;
1081 	const int val_type = USB_MIXER_S16;
1082 	const unsigned int control = 7;
1083 
1084 	for (ch = 0; ch < 4; ++ch) {
1085 		cmask = 1 << ch;
1086 		snprintf(name, sizeof(name),
1087 			"Effect Return %d Volume", ch + 1);
1088 		err = snd_create_std_mono_ctl(mixer, id, control,
1089 						cmask, val_type, name,
1090 						snd_usb_mixer_vol_tlv);
1091 		if (err < 0)
1092 			return err;
1093 	}
1094 
1095 	return 0;
1096 }
1097 
1098 static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
1099 {
1100 	unsigned int  cmask;
1101 	int err, ch;
1102 	char name[48];
1103 
1104 	const unsigned int id = 5;
1105 	const int val_type = USB_MIXER_S16;
1106 	const unsigned int control = 9;
1107 
1108 	for (ch = 0; ch < 8; ++ch) {
1109 		cmask = 1 << ch;
1110 		snprintf(name, sizeof(name),
1111 			"Effect Send AIn%d Volume", ch + 1);
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 	for (ch = 8; ch < 16; ++ch) {
1119 		cmask = 1 << ch;
1120 		snprintf(name, sizeof(name),
1121 			"Effect Send DIn%d Volume", ch - 7);
1122 		err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1123 						val_type, name,
1124 						snd_usb_mixer_vol_tlv);
1125 		if (err < 0)
1126 			return err;
1127 	}
1128 	return 0;
1129 }
1130 
1131 static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
1132 {
1133 	int err;
1134 
1135 	err = snd_ftu_create_volume_ctls(mixer);
1136 	if (err < 0)
1137 		return err;
1138 
1139 	err = snd_ftu_create_effect_switch(mixer, 1, 6);
1140 	if (err < 0)
1141 		return err;
1142 
1143 	err = snd_ftu_create_effect_volume_ctl(mixer);
1144 	if (err < 0)
1145 		return err;
1146 
1147 	err = snd_ftu_create_effect_duration_ctl(mixer);
1148 	if (err < 0)
1149 		return err;
1150 
1151 	err = snd_ftu_create_effect_feedback_ctl(mixer);
1152 	if (err < 0)
1153 		return err;
1154 
1155 	err = snd_ftu_create_effect_return_ctls(mixer);
1156 	if (err < 0)
1157 		return err;
1158 
1159 	err = snd_ftu_create_effect_send_ctls(mixer);
1160 	if (err < 0)
1161 		return err;
1162 
1163 	return 0;
1164 }
1165 
1166 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
1167 			       unsigned char samplerate_id)
1168 {
1169 	struct usb_mixer_interface *mixer;
1170 	struct usb_mixer_elem_info *cval;
1171 	int unitid = 12; /* SamleRate ExtensionUnit ID */
1172 
1173 	list_for_each_entry(mixer, &chip->mixer_list, list) {
1174 		cval = (struct usb_mixer_elem_info *)mixer->id_elems[unitid];
1175 		if (cval) {
1176 			snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1177 						    cval->control << 8,
1178 						    samplerate_id);
1179 			snd_usb_mixer_notify_id(mixer, unitid);
1180 		}
1181 		break;
1182 	}
1183 }
1184 
1185 /* M-Audio Fast Track C400/C600 */
1186 /* C400/C600 volume controls, this control needs a volume quirk, see mixer.c */
1187 static int snd_c400_create_vol_ctls(struct usb_mixer_interface *mixer)
1188 {
1189 	char name[64];
1190 	unsigned int cmask, offset;
1191 	int out, chan, err;
1192 	int num_outs = 0;
1193 	int num_ins = 0;
1194 
1195 	const unsigned int id = 0x40;
1196 	const int val_type = USB_MIXER_S16;
1197 	const int control = 1;
1198 
1199 	switch (mixer->chip->usb_id) {
1200 	case USB_ID(0x0763, 0x2030):
1201 		num_outs = 6;
1202 		num_ins = 4;
1203 		break;
1204 	case USB_ID(0x0763, 0x2031):
1205 		num_outs = 8;
1206 		num_ins = 6;
1207 		break;
1208 	}
1209 
1210 	for (chan = 0; chan < num_outs + num_ins; chan++) {
1211 		for (out = 0; out < num_outs; out++) {
1212 			if (chan < num_outs) {
1213 				snprintf(name, sizeof(name),
1214 					"PCM%d-Out%d Playback Volume",
1215 					chan + 1, out + 1);
1216 			} else {
1217 				snprintf(name, sizeof(name),
1218 					"In%d-Out%d Playback Volume",
1219 					chan - num_outs + 1, out + 1);
1220 			}
1221 
1222 			cmask = (out == 0) ? 0 : 1 << (out - 1);
1223 			offset = chan * num_outs;
1224 			err = snd_create_std_mono_ctl_offset(mixer, id, control,
1225 						cmask, val_type, offset, name,
1226 						&snd_usb_mixer_vol_tlv);
1227 			if (err < 0)
1228 				return err;
1229 		}
1230 	}
1231 
1232 	return 0;
1233 }
1234 
1235 /* This control needs a volume quirk, see mixer.c */
1236 static int snd_c400_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1237 {
1238 	static const char name[] = "Effect Volume";
1239 	const unsigned int id = 0x43;
1240 	const int val_type = USB_MIXER_U8;
1241 	const unsigned int control = 3;
1242 	const unsigned int cmask = 0;
1243 
1244 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1245 					name, snd_usb_mixer_vol_tlv);
1246 }
1247 
1248 /* This control needs a volume quirk, see mixer.c */
1249 static int snd_c400_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1250 {
1251 	static const char name[] = "Effect Duration";
1252 	const unsigned int id = 0x43;
1253 	const int val_type = USB_MIXER_S16;
1254 	const unsigned int control = 4;
1255 	const unsigned int cmask = 0;
1256 
1257 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1258 					name, snd_usb_mixer_vol_tlv);
1259 }
1260 
1261 /* This control needs a volume quirk, see mixer.c */
1262 static int snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1263 {
1264 	static const char name[] = "Effect Feedback Volume";
1265 	const unsigned int id = 0x43;
1266 	const int val_type = USB_MIXER_U8;
1267 	const unsigned int control = 5;
1268 	const unsigned int cmask = 0;
1269 
1270 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1271 					name, NULL);
1272 }
1273 
1274 static int snd_c400_create_effect_vol_ctls(struct usb_mixer_interface *mixer)
1275 {
1276 	char name[64];
1277 	unsigned int cmask;
1278 	int chan, err;
1279 	int num_outs = 0;
1280 	int num_ins = 0;
1281 
1282 	const unsigned int id = 0x42;
1283 	const int val_type = USB_MIXER_S16;
1284 	const int control = 1;
1285 
1286 	switch (mixer->chip->usb_id) {
1287 	case USB_ID(0x0763, 0x2030):
1288 		num_outs = 6;
1289 		num_ins = 4;
1290 		break;
1291 	case USB_ID(0x0763, 0x2031):
1292 		num_outs = 8;
1293 		num_ins = 6;
1294 		break;
1295 	}
1296 
1297 	for (chan = 0; chan < num_outs + num_ins; chan++) {
1298 		if (chan < num_outs) {
1299 			snprintf(name, sizeof(name),
1300 				"Effect Send DOut%d",
1301 				chan + 1);
1302 		} else {
1303 			snprintf(name, sizeof(name),
1304 				"Effect Send AIn%d",
1305 				chan - num_outs + 1);
1306 		}
1307 
1308 		cmask = (chan == 0) ? 0 : 1 << (chan - 1);
1309 		err = snd_create_std_mono_ctl(mixer, id, control,
1310 						cmask, val_type, name,
1311 						&snd_usb_mixer_vol_tlv);
1312 		if (err < 0)
1313 			return err;
1314 	}
1315 
1316 	return 0;
1317 }
1318 
1319 static int snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface *mixer)
1320 {
1321 	char name[64];
1322 	unsigned int cmask;
1323 	int chan, err;
1324 	int num_outs = 0;
1325 	int offset = 0;
1326 
1327 	const unsigned int id = 0x40;
1328 	const int val_type = USB_MIXER_S16;
1329 	const int control = 1;
1330 
1331 	switch (mixer->chip->usb_id) {
1332 	case USB_ID(0x0763, 0x2030):
1333 		num_outs = 6;
1334 		offset = 0x3c;
1335 		/* { 0x3c, 0x43, 0x3e, 0x45, 0x40, 0x47 } */
1336 		break;
1337 	case USB_ID(0x0763, 0x2031):
1338 		num_outs = 8;
1339 		offset = 0x70;
1340 		/* { 0x70, 0x79, 0x72, 0x7b, 0x74, 0x7d, 0x76, 0x7f } */
1341 		break;
1342 	}
1343 
1344 	for (chan = 0; chan < num_outs; chan++) {
1345 		snprintf(name, sizeof(name),
1346 			"Effect Return %d",
1347 			chan + 1);
1348 
1349 		cmask = (chan == 0) ? 0 :
1350 			1 << (chan + (chan % 2) * num_outs - 1);
1351 		err = snd_create_std_mono_ctl_offset(mixer, id, control,
1352 						cmask, val_type, offset, name,
1353 						&snd_usb_mixer_vol_tlv);
1354 		if (err < 0)
1355 			return err;
1356 	}
1357 
1358 	return 0;
1359 }
1360 
1361 static int snd_c400_create_mixer(struct usb_mixer_interface *mixer)
1362 {
1363 	int err;
1364 
1365 	err = snd_c400_create_vol_ctls(mixer);
1366 	if (err < 0)
1367 		return err;
1368 
1369 	err = snd_c400_create_effect_vol_ctls(mixer);
1370 	if (err < 0)
1371 		return err;
1372 
1373 	err = snd_c400_create_effect_ret_vol_ctls(mixer);
1374 	if (err < 0)
1375 		return err;
1376 
1377 	err = snd_ftu_create_effect_switch(mixer, 2, 0x43);
1378 	if (err < 0)
1379 		return err;
1380 
1381 	err = snd_c400_create_effect_volume_ctl(mixer);
1382 	if (err < 0)
1383 		return err;
1384 
1385 	err = snd_c400_create_effect_duration_ctl(mixer);
1386 	if (err < 0)
1387 		return err;
1388 
1389 	err = snd_c400_create_effect_feedback_ctl(mixer);
1390 	if (err < 0)
1391 		return err;
1392 
1393 	return 0;
1394 }
1395 
1396 /*
1397  * The mixer units for Ebox-44 are corrupt, and even where they
1398  * are valid they presents mono controls as L and R channels of
1399  * stereo. So we provide a good mixer here.
1400  */
1401 static struct std_mono_table ebox44_table[] = {
1402 	{
1403 		.unitid = 4,
1404 		.control = 1,
1405 		.cmask = 0x0,
1406 		.val_type = USB_MIXER_INV_BOOLEAN,
1407 		.name = "Headphone Playback Switch"
1408 	},
1409 	{
1410 		.unitid = 4,
1411 		.control = 2,
1412 		.cmask = 0x1,
1413 		.val_type = USB_MIXER_S16,
1414 		.name = "Headphone A Mix Playback Volume"
1415 	},
1416 	{
1417 		.unitid = 4,
1418 		.control = 2,
1419 		.cmask = 0x2,
1420 		.val_type = USB_MIXER_S16,
1421 		.name = "Headphone B Mix Playback Volume"
1422 	},
1423 
1424 	{
1425 		.unitid = 7,
1426 		.control = 1,
1427 		.cmask = 0x0,
1428 		.val_type = USB_MIXER_INV_BOOLEAN,
1429 		.name = "Output Playback Switch"
1430 	},
1431 	{
1432 		.unitid = 7,
1433 		.control = 2,
1434 		.cmask = 0x1,
1435 		.val_type = USB_MIXER_S16,
1436 		.name = "Output A Playback Volume"
1437 	},
1438 	{
1439 		.unitid = 7,
1440 		.control = 2,
1441 		.cmask = 0x2,
1442 		.val_type = USB_MIXER_S16,
1443 		.name = "Output B Playback Volume"
1444 	},
1445 
1446 	{
1447 		.unitid = 10,
1448 		.control = 1,
1449 		.cmask = 0x0,
1450 		.val_type = USB_MIXER_INV_BOOLEAN,
1451 		.name = "Input Capture Switch"
1452 	},
1453 	{
1454 		.unitid = 10,
1455 		.control = 2,
1456 		.cmask = 0x1,
1457 		.val_type = USB_MIXER_S16,
1458 		.name = "Input A Capture Volume"
1459 	},
1460 	{
1461 		.unitid = 10,
1462 		.control = 2,
1463 		.cmask = 0x2,
1464 		.val_type = USB_MIXER_S16,
1465 		.name = "Input B Capture Volume"
1466 	},
1467 
1468 	{}
1469 };
1470 
1471 /* Audio Advantage Micro II findings:
1472  *
1473  * Mapping spdif AES bits to vendor register.bit:
1474  * AES0: [0 0 0 0 2.3 2.2 2.1 2.0] - default 0x00
1475  * AES1: [3.3 3.2.3.1.3.0 2.7 2.6 2.5 2.4] - default: 0x01
1476  * AES2: [0 0 0 0 0 0 0 0]
1477  * AES3: [0 0 0 0 0 0 x 0] - 'x' bit is set basing on standard usb request
1478  *                           (UAC_EP_CS_ATTR_SAMPLE_RATE) for Audio Devices
1479  *
1480  * power on values:
1481  * r2: 0x10
1482  * r3: 0x20 (b7 is zeroed just before playback (except IEC61937) and set
1483  *           just after it to 0xa0, presumably it disables/mutes some analog
1484  *           parts when there is no audio.)
1485  * r9: 0x28
1486  *
1487  * Optical transmitter on/off:
1488  * vendor register.bit: 9.1
1489  * 0 - on (0x28 register value)
1490  * 1 - off (0x2a register value)
1491  *
1492  */
1493 static int snd_microii_spdif_info(struct snd_kcontrol *kcontrol,
1494 	struct snd_ctl_elem_info *uinfo)
1495 {
1496 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1497 	uinfo->count = 1;
1498 	return 0;
1499 }
1500 
1501 static int snd_microii_spdif_default_get(struct snd_kcontrol *kcontrol,
1502 	struct snd_ctl_elem_value *ucontrol)
1503 {
1504 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1505 	struct snd_usb_audio *chip = list->mixer->chip;
1506 	int err;
1507 	struct usb_interface *iface;
1508 	struct usb_host_interface *alts;
1509 	unsigned int ep;
1510 	unsigned char data[3];
1511 	int rate;
1512 
1513 	err = snd_usb_lock_shutdown(chip);
1514 	if (err < 0)
1515 		return err;
1516 
1517 	ucontrol->value.iec958.status[0] = kcontrol->private_value & 0xff;
1518 	ucontrol->value.iec958.status[1] = (kcontrol->private_value >> 8) & 0xff;
1519 	ucontrol->value.iec958.status[2] = 0x00;
1520 
1521 	/* use known values for that card: interface#1 altsetting#1 */
1522 	iface = usb_ifnum_to_if(chip->dev, 1);
1523 	if (!iface || iface->num_altsetting < 2)
1524 		return -EINVAL;
1525 	alts = &iface->altsetting[1];
1526 	if (get_iface_desc(alts)->bNumEndpoints < 1)
1527 		return -EINVAL;
1528 	ep = get_endpoint(alts, 0)->bEndpointAddress;
1529 
1530 	err = snd_usb_ctl_msg(chip->dev,
1531 			usb_rcvctrlpipe(chip->dev, 0),
1532 			UAC_GET_CUR,
1533 			USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
1534 			UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
1535 			ep,
1536 			data,
1537 			sizeof(data));
1538 	if (err < 0)
1539 		goto end;
1540 
1541 	rate = data[0] | (data[1] << 8) | (data[2] << 16);
1542 	ucontrol->value.iec958.status[3] = (rate == 48000) ?
1543 			IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100;
1544 
1545 	err = 0;
1546  end:
1547 	snd_usb_unlock_shutdown(chip);
1548 	return err;
1549 }
1550 
1551 static int snd_microii_spdif_default_update(struct usb_mixer_elem_list *list)
1552 {
1553 	struct snd_usb_audio *chip = list->mixer->chip;
1554 	unsigned int pval = list->kctl->private_value;
1555 	u8 reg;
1556 	int err;
1557 
1558 	err = snd_usb_lock_shutdown(chip);
1559 	if (err < 0)
1560 		return err;
1561 
1562 	reg = ((pval >> 4) & 0xf0) | (pval & 0x0f);
1563 	err = snd_usb_ctl_msg(chip->dev,
1564 			usb_sndctrlpipe(chip->dev, 0),
1565 			UAC_SET_CUR,
1566 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1567 			reg,
1568 			2,
1569 			NULL,
1570 			0);
1571 	if (err < 0)
1572 		goto end;
1573 
1574 	reg = (pval & IEC958_AES0_NONAUDIO) ? 0xa0 : 0x20;
1575 	reg |= (pval >> 12) & 0x0f;
1576 	err = snd_usb_ctl_msg(chip->dev,
1577 			usb_sndctrlpipe(chip->dev, 0),
1578 			UAC_SET_CUR,
1579 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1580 			reg,
1581 			3,
1582 			NULL,
1583 			0);
1584 	if (err < 0)
1585 		goto end;
1586 
1587  end:
1588 	snd_usb_unlock_shutdown(chip);
1589 	return err;
1590 }
1591 
1592 static int snd_microii_spdif_default_put(struct snd_kcontrol *kcontrol,
1593 	struct snd_ctl_elem_value *ucontrol)
1594 {
1595 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1596 	unsigned int pval, pval_old;
1597 	int err;
1598 
1599 	pval = pval_old = kcontrol->private_value;
1600 	pval &= 0xfffff0f0;
1601 	pval |= (ucontrol->value.iec958.status[1] & 0x0f) << 8;
1602 	pval |= (ucontrol->value.iec958.status[0] & 0x0f);
1603 
1604 	pval &= 0xffff0fff;
1605 	pval |= (ucontrol->value.iec958.status[1] & 0xf0) << 8;
1606 
1607 	/* The frequency bits in AES3 cannot be set via register access. */
1608 
1609 	/* Silently ignore any bits from the request that cannot be set. */
1610 
1611 	if (pval == pval_old)
1612 		return 0;
1613 
1614 	kcontrol->private_value = pval;
1615 	err = snd_microii_spdif_default_update(list);
1616 	return err < 0 ? err : 1;
1617 }
1618 
1619 static int snd_microii_spdif_mask_get(struct snd_kcontrol *kcontrol,
1620 	struct snd_ctl_elem_value *ucontrol)
1621 {
1622 	ucontrol->value.iec958.status[0] = 0x0f;
1623 	ucontrol->value.iec958.status[1] = 0xff;
1624 	ucontrol->value.iec958.status[2] = 0x00;
1625 	ucontrol->value.iec958.status[3] = 0x00;
1626 
1627 	return 0;
1628 }
1629 
1630 static int snd_microii_spdif_switch_get(struct snd_kcontrol *kcontrol,
1631 	struct snd_ctl_elem_value *ucontrol)
1632 {
1633 	ucontrol->value.integer.value[0] = !(kcontrol->private_value & 0x02);
1634 
1635 	return 0;
1636 }
1637 
1638 static int snd_microii_spdif_switch_update(struct usb_mixer_elem_list *list)
1639 {
1640 	struct snd_usb_audio *chip = list->mixer->chip;
1641 	u8 reg = list->kctl->private_value;
1642 	int err;
1643 
1644 	err = snd_usb_lock_shutdown(chip);
1645 	if (err < 0)
1646 		return err;
1647 
1648 	err = snd_usb_ctl_msg(chip->dev,
1649 			usb_sndctrlpipe(chip->dev, 0),
1650 			UAC_SET_CUR,
1651 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1652 			reg,
1653 			9,
1654 			NULL,
1655 			0);
1656 
1657 	snd_usb_unlock_shutdown(chip);
1658 	return err;
1659 }
1660 
1661 static int snd_microii_spdif_switch_put(struct snd_kcontrol *kcontrol,
1662 	struct snd_ctl_elem_value *ucontrol)
1663 {
1664 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1665 	u8 reg;
1666 	int err;
1667 
1668 	reg = ucontrol->value.integer.value[0] ? 0x28 : 0x2a;
1669 	if (reg != list->kctl->private_value)
1670 		return 0;
1671 
1672 	kcontrol->private_value = reg;
1673 	err = snd_microii_spdif_switch_update(list);
1674 	return err < 0 ? err : 1;
1675 }
1676 
1677 static struct snd_kcontrol_new snd_microii_mixer_spdif[] = {
1678 	{
1679 		.iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1680 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1681 		.info =     snd_microii_spdif_info,
1682 		.get =      snd_microii_spdif_default_get,
1683 		.put =      snd_microii_spdif_default_put,
1684 		.private_value = 0x00000100UL,/* reset value */
1685 	},
1686 	{
1687 		.access =   SNDRV_CTL_ELEM_ACCESS_READ,
1688 		.iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1689 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1690 		.info =     snd_microii_spdif_info,
1691 		.get =      snd_microii_spdif_mask_get,
1692 	},
1693 	{
1694 		.iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
1695 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1696 		.info =     snd_ctl_boolean_mono_info,
1697 		.get =      snd_microii_spdif_switch_get,
1698 		.put =      snd_microii_spdif_switch_put,
1699 		.private_value = 0x00000028UL,/* reset value */
1700 	}
1701 };
1702 
1703 static int snd_microii_controls_create(struct usb_mixer_interface *mixer)
1704 {
1705 	int err, i;
1706 	static usb_mixer_elem_resume_func_t resume_funcs[] = {
1707 		snd_microii_spdif_default_update,
1708 		NULL,
1709 		snd_microii_spdif_switch_update
1710 	};
1711 
1712 	for (i = 0; i < ARRAY_SIZE(snd_microii_mixer_spdif); ++i) {
1713 		err = add_single_ctl_with_resume(mixer, 0,
1714 						 resume_funcs[i],
1715 						 &snd_microii_mixer_spdif[i],
1716 						 NULL);
1717 		if (err < 0)
1718 			return err;
1719 	}
1720 
1721 	return 0;
1722 }
1723 
1724 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
1725 {
1726 	int err = 0;
1727 	struct snd_info_entry *entry;
1728 
1729 	if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0)
1730 		return err;
1731 
1732 	switch (mixer->chip->usb_id) {
1733 	/* Tascam US-16x08 */
1734 	case USB_ID(0x0644, 0x8047):
1735 		err = snd_us16x08_controls_create(mixer);
1736 		break;
1737 	case USB_ID(0x041e, 0x3020):
1738 	case USB_ID(0x041e, 0x3040):
1739 	case USB_ID(0x041e, 0x3042):
1740 	case USB_ID(0x041e, 0x30df):
1741 	case USB_ID(0x041e, 0x3048):
1742 		err = snd_audigy2nx_controls_create(mixer);
1743 		if (err < 0)
1744 			break;
1745 		if (!snd_card_proc_new(mixer->chip->card, "audigy2nx", &entry))
1746 			snd_info_set_text_ops(entry, mixer,
1747 					      snd_audigy2nx_proc_read);
1748 		break;
1749 
1750 	/* EMU0204 */
1751 	case USB_ID(0x041e, 0x3f19):
1752 		err = snd_emu0204_controls_create(mixer);
1753 		if (err < 0)
1754 			break;
1755 		break;
1756 
1757 	case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
1758 	case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
1759 		err = snd_c400_create_mixer(mixer);
1760 		break;
1761 
1762 	case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
1763 	case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
1764 		err = snd_ftu_create_mixer(mixer);
1765 		break;
1766 
1767 	case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
1768 	case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
1769 	case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
1770 		err = snd_xonar_u1_controls_create(mixer);
1771 		break;
1772 
1773 	case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
1774 		err = snd_microii_controls_create(mixer);
1775 		break;
1776 
1777 	case USB_ID(0x0dba, 0x1000): /* Digidesign Mbox 1 */
1778 		err = snd_mbox1_create_sync_switch(mixer);
1779 		break;
1780 
1781 	case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
1782 		err = snd_nativeinstruments_create_mixer(mixer,
1783 				snd_nativeinstruments_ta6_mixers,
1784 				ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
1785 		break;
1786 
1787 	case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
1788 		err = snd_nativeinstruments_create_mixer(mixer,
1789 				snd_nativeinstruments_ta10_mixers,
1790 				ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
1791 		break;
1792 
1793 	case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
1794 		/* detection is disabled in mixer_maps.c */
1795 		err = snd_create_std_mono_table(mixer, ebox44_table);
1796 		break;
1797 
1798 	case USB_ID(0x1235, 0x8012): /* Focusrite Scarlett 6i6 */
1799 	case USB_ID(0x1235, 0x8002): /* Focusrite Scarlett 8i6 */
1800 	case USB_ID(0x1235, 0x8004): /* Focusrite Scarlett 18i6 */
1801 	case USB_ID(0x1235, 0x8014): /* Focusrite Scarlett 18i8 */
1802 	case USB_ID(0x1235, 0x800c): /* Focusrite Scarlett 18i20 */
1803 		err = snd_scarlett_controls_create(mixer);
1804 		break;
1805 	}
1806 
1807 	return err;
1808 }
1809 
1810 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
1811 				    int unitid)
1812 {
1813 	if (!mixer->rc_cfg)
1814 		return;
1815 	/* unit ids specific to Extigy/Audigy 2 NX: */
1816 	switch (unitid) {
1817 	case 0: /* remote control */
1818 		mixer->rc_urb->dev = mixer->chip->dev;
1819 		usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
1820 		break;
1821 	case 4: /* digital in jack */
1822 	case 7: /* line in jacks */
1823 	case 19: /* speaker out jacks */
1824 	case 20: /* headphones out jack */
1825 		break;
1826 	/* live24ext: 4 = line-in jack */
1827 	case 3:	/* hp-out jack (may actuate Mute) */
1828 		if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
1829 		    mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
1830 			snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
1831 		break;
1832 	default:
1833 		usb_audio_dbg(mixer->chip, "memory change in unknown unit %d\n", unitid);
1834 		break;
1835 	}
1836 }
1837 
1838 static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
1839 					 struct usb_mixer_elem_info *cval,
1840 					 struct snd_kcontrol *kctl)
1841 {
1842 	/* Approximation using 10 ranges based on output measurement on hw v1.2.
1843 	 * This seems close to the cubic mapping e.g. alsamixer uses. */
1844 	static const DECLARE_TLV_DB_RANGE(scale,
1845 		 0,  1, TLV_DB_MINMAX_ITEM(-5300, -4970),
1846 		 2,  5, TLV_DB_MINMAX_ITEM(-4710, -4160),
1847 		 6,  7, TLV_DB_MINMAX_ITEM(-3884, -3710),
1848 		 8, 14, TLV_DB_MINMAX_ITEM(-3443, -2560),
1849 		15, 16, TLV_DB_MINMAX_ITEM(-2475, -2324),
1850 		17, 19, TLV_DB_MINMAX_ITEM(-2228, -2031),
1851 		20, 26, TLV_DB_MINMAX_ITEM(-1910, -1393),
1852 		27, 31, TLV_DB_MINMAX_ITEM(-1322, -1032),
1853 		32, 40, TLV_DB_MINMAX_ITEM(-968, -490),
1854 		41, 50, TLV_DB_MINMAX_ITEM(-441, 0),
1855 	);
1856 
1857 	if (cval->min == 0 && cval->max == 50) {
1858 		usb_audio_info(mixer->chip, "applying DragonFly dB scale quirk (0-50 variant)\n");
1859 		kctl->tlv.p = scale;
1860 		kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1861 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1862 
1863 	} else if (cval->min == 0 && cval->max <= 1000) {
1864 		/* Some other clearly broken DragonFly variant.
1865 		 * At least a 0..53 variant (hw v1.0) exists.
1866 		 */
1867 		usb_audio_info(mixer->chip, "ignoring too narrow dB range on a DragonFly device");
1868 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1869 	}
1870 }
1871 
1872 void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
1873 				  struct usb_mixer_elem_info *cval, int unitid,
1874 				  struct snd_kcontrol *kctl)
1875 {
1876 	switch (mixer->chip->usb_id) {
1877 	case USB_ID(0x21b4, 0x0081): /* AudioQuest DragonFly */
1878 		if (unitid == 7 && cval->control == UAC_FU_VOLUME)
1879 			snd_dragonfly_quirk_db_scale(mixer, cval, kctl);
1880 		break;
1881 	}
1882 }
1883 
1884