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