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