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