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