Linux Audio

Check our new training course

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