Linux Audio

Check our new training course

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