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