File Index
Files and directories
Sign in
FILE

mixer_quirks.c

File preview with syntax highlighting.

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