xref: /linux/sound/usb/mixer_quirks.c (revision 177bf8620cf4ed290ee170a6c5966adc0924b336)
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/bitfield.h>
18 #include <linux/hid.h>
19 #include <linux/init.h>
20 #include <linux/input.h>
21 #include <linux/math64.h>
22 #include <linux/slab.h>
23 #include <linux/usb.h>
24 #include <linux/usb/audio.h>
25 
26 #include <sound/asoundef.h>
27 #include <sound/core.h>
28 #include <sound/control.h>
29 #include <sound/hda_verbs.h>
30 #include <sound/hwdep.h>
31 #include <sound/info.h>
32 #include <sound/tlv.h>
33 
34 #include "usbaudio.h"
35 #include "mixer.h"
36 #include "mixer_quirks.h"
37 #include "mixer_scarlett.h"
38 #include "mixer_scarlett2.h"
39 #include "mixer_us16x08.h"
40 #include "mixer_s1810c.h"
41 #include "helper.h"
42 #include "fcp.h"
43 
44 struct std_mono_table {
45 	unsigned int unitid, control, cmask;
46 	int val_type;
47 	const char *name;
48 	snd_kcontrol_tlv_rw_t *tlv_callback;
49 };
50 
51 /* This function allows for the creation of standard UAC controls.
52  * See the quirks for M-Audio FTUs or Ebox-44.
53  * If you don't want to set a TLV callback pass NULL.
54  *
55  * Since there doesn't seem to be a devices that needs a multichannel
56  * version, we keep it mono for simplicity.
57  */
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)58 static int snd_create_std_mono_ctl_offset(struct usb_mixer_interface *mixer,
59 					  unsigned int unitid,
60 					  unsigned int control,
61 					  unsigned int cmask,
62 					  int val_type,
63 					  unsigned int idx_off,
64 					  const char *name,
65 					  snd_kcontrol_tlv_rw_t *tlv_callback)
66 {
67 	struct usb_mixer_elem_info *cval;
68 	struct snd_kcontrol *kctl;
69 
70 	cval = kzalloc(sizeof(*cval), GFP_KERNEL);
71 	if (!cval)
72 		return -ENOMEM;
73 
74 	snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
75 	cval->val_type = val_type;
76 	cval->channels = 1;
77 	cval->control = control;
78 	cval->cmask = cmask;
79 	cval->idx_off = idx_off;
80 
81 	/* get_min_max() is called only for integer volumes later,
82 	 * so provide a short-cut for booleans
83 	 */
84 	cval->min = 0;
85 	cval->max = 1;
86 	cval->res = 0;
87 	cval->dBmin = 0;
88 	cval->dBmax = 0;
89 
90 	/* Create control */
91 	kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
92 	if (!kctl) {
93 		kfree(cval);
94 		return -ENOMEM;
95 	}
96 
97 	/* Set name */
98 	snprintf(kctl->id.name, sizeof(kctl->id.name), name);
99 	kctl->private_free = snd_usb_mixer_elem_free;
100 
101 	/* set TLV */
102 	if (tlv_callback) {
103 		kctl->tlv.c = tlv_callback;
104 		kctl->vd[0].access |=
105 			SNDRV_CTL_ELEM_ACCESS_TLV_READ |
106 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
107 	}
108 	/* Add control to mixer */
109 	return snd_usb_mixer_add_control(&cval->head, kctl);
110 }
111 
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)112 static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
113 				   unsigned int unitid,
114 				   unsigned int control,
115 				   unsigned int cmask,
116 				   int val_type,
117 				   const char *name,
118 				   snd_kcontrol_tlv_rw_t *tlv_callback)
119 {
120 	return snd_create_std_mono_ctl_offset(mixer, unitid, control, cmask,
121 					      val_type, 0 /* Offset */,
122 					      name, tlv_callback);
123 }
124 
125 /*
126  * Create a set of standard UAC controls from a table
127  */
snd_create_std_mono_table(struct usb_mixer_interface * mixer,const struct std_mono_table * t)128 static int snd_create_std_mono_table(struct usb_mixer_interface *mixer,
129 				     const struct std_mono_table *t)
130 {
131 	int err;
132 
133 	while (t->name) {
134 		err = snd_create_std_mono_ctl(mixer, t->unitid, t->control,
135 					      t->cmask, t->val_type, t->name,
136 					      t->tlv_callback);
137 		if (err < 0)
138 			return err;
139 		t++;
140 	}
141 
142 	return 0;
143 }
144 
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)145 static int add_single_ctl_with_resume(struct usb_mixer_interface *mixer,
146 				      int id,
147 				      usb_mixer_elem_resume_func_t resume,
148 				      const struct snd_kcontrol_new *knew,
149 				      struct usb_mixer_elem_list **listp)
150 {
151 	struct usb_mixer_elem_list *list;
152 	struct snd_kcontrol *kctl;
153 
154 	list = kzalloc(sizeof(*list), GFP_KERNEL);
155 	if (!list)
156 		return -ENOMEM;
157 	if (listp)
158 		*listp = list;
159 	list->mixer = mixer;
160 	list->id = id;
161 	list->resume = resume;
162 	kctl = snd_ctl_new1(knew, list);
163 	if (!kctl) {
164 		kfree(list);
165 		return -ENOMEM;
166 	}
167 	kctl->private_free = snd_usb_mixer_elem_free;
168 	/* don't use snd_usb_mixer_add_control() here, this is a special list element */
169 	return snd_usb_mixer_add_list(list, kctl, false);
170 }
171 
172 /*
173  * Sound Blaster remote control configuration
174  *
175  * format of remote control data:
176  * Extigy:       xx 00
177  * Audigy 2 NX:  06 80 xx 00 00 00
178  * Live! 24-bit: 06 80 xx yy 22 83
179  */
180 static const struct rc_config {
181 	u32 usb_id;
182 	u8  offset;
183 	u8  length;
184 	u8  packet_length;
185 	u8  min_packet_length; /* minimum accepted length of the URB result */
186 	u8  mute_mixer_id;
187 	u32 mute_code;
188 } rc_configs[] = {
189 	{ USB_ID(0x041e, 0x3000), 0, 1, 2, 1,  18, 0x0013 }, /* Extigy       */
190 	{ USB_ID(0x041e, 0x3020), 2, 1, 6, 6,  18, 0x0013 }, /* Audigy 2 NX  */
191 	{ USB_ID(0x041e, 0x3040), 2, 2, 6, 6,  2,  0x6e91 }, /* Live! 24-bit */
192 	{ USB_ID(0x041e, 0x3042), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 */
193 	{ USB_ID(0x041e, 0x30df), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
194 	{ USB_ID(0x041e, 0x3237), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
195 	{ USB_ID(0x041e, 0x3263), 0, 1, 1, 1,  1,  0x000d }, /* Usb X-Fi S51 Pro */
196 	{ USB_ID(0x041e, 0x3048), 2, 2, 6, 6,  2,  0x6e91 }, /* Toshiba SB0500 */
197 };
198 
snd_usb_soundblaster_remote_complete(struct urb * urb)199 static void snd_usb_soundblaster_remote_complete(struct urb *urb)
200 {
201 	struct usb_mixer_interface *mixer = urb->context;
202 	const struct rc_config *rc = mixer->rc_cfg;
203 	u32 code;
204 
205 	if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
206 		return;
207 
208 	code = mixer->rc_buffer[rc->offset];
209 	if (rc->length == 2)
210 		code |= mixer->rc_buffer[rc->offset + 1] << 8;
211 
212 	/* the Mute button actually changes the mixer control */
213 	if (code == rc->mute_code)
214 		snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
215 	mixer->rc_code = code;
216 	wake_up(&mixer->rc_waitq);
217 }
218 
snd_usb_sbrc_hwdep_read(struct snd_hwdep * hw,char __user * buf,long count,loff_t * offset)219 static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
220 				    long count, loff_t *offset)
221 {
222 	struct usb_mixer_interface *mixer = hw->private_data;
223 	int err;
224 	u32 rc_code;
225 
226 	if (count != 1 && count != 4)
227 		return -EINVAL;
228 	err = wait_event_interruptible(mixer->rc_waitq,
229 				       (rc_code = xchg(&mixer->rc_code, 0)) != 0);
230 	if (err == 0) {
231 		if (count == 1)
232 			err = put_user(rc_code, buf);
233 		else
234 			err = put_user(rc_code, (u32 __user *)buf);
235 	}
236 	return err < 0 ? err : count;
237 }
238 
snd_usb_sbrc_hwdep_poll(struct snd_hwdep * hw,struct file * file,poll_table * wait)239 static __poll_t snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
240 					poll_table *wait)
241 {
242 	struct usb_mixer_interface *mixer = hw->private_data;
243 
244 	poll_wait(file, &mixer->rc_waitq, wait);
245 	return mixer->rc_code ? EPOLLIN | EPOLLRDNORM : 0;
246 }
247 
snd_usb_soundblaster_remote_init(struct usb_mixer_interface * mixer)248 static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
249 {
250 	struct snd_hwdep *hwdep;
251 	int err, len, i;
252 
253 	for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
254 		if (rc_configs[i].usb_id == mixer->chip->usb_id)
255 			break;
256 	if (i >= ARRAY_SIZE(rc_configs))
257 		return 0;
258 	mixer->rc_cfg = &rc_configs[i];
259 
260 	len = mixer->rc_cfg->packet_length;
261 
262 	init_waitqueue_head(&mixer->rc_waitq);
263 	err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
264 	if (err < 0)
265 		return err;
266 	snprintf(hwdep->name, sizeof(hwdep->name),
267 		 "%s remote control", mixer->chip->card->shortname);
268 	hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
269 	hwdep->private_data = mixer;
270 	hwdep->ops.read = snd_usb_sbrc_hwdep_read;
271 	hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
272 	hwdep->exclusive = 1;
273 
274 	mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
275 	if (!mixer->rc_urb)
276 		return -ENOMEM;
277 	mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
278 	if (!mixer->rc_setup_packet) {
279 		usb_free_urb(mixer->rc_urb);
280 		mixer->rc_urb = NULL;
281 		return -ENOMEM;
282 	}
283 	mixer->rc_setup_packet->bRequestType =
284 		USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
285 	mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
286 	mixer->rc_setup_packet->wValue = cpu_to_le16(0);
287 	mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
288 	mixer->rc_setup_packet->wLength = cpu_to_le16(len);
289 	usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
290 			     usb_rcvctrlpipe(mixer->chip->dev, 0),
291 			     (u8 *)mixer->rc_setup_packet, mixer->rc_buffer, len,
292 			     snd_usb_soundblaster_remote_complete, mixer);
293 	return 0;
294 }
295 
296 #define snd_audigy2nx_led_info		snd_ctl_boolean_mono_info
297 
snd_audigy2nx_led_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)298 static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
299 {
300 	ucontrol->value.integer.value[0] = kcontrol->private_value >> 8;
301 	return 0;
302 }
303 
snd_audigy2nx_led_update(struct usb_mixer_interface * mixer,int value,int index)304 static int snd_audigy2nx_led_update(struct usb_mixer_interface *mixer,
305 				    int value, int index)
306 {
307 	struct snd_usb_audio *chip = mixer->chip;
308 	int err;
309 
310 	err = snd_usb_lock_shutdown(chip);
311 	if (err < 0)
312 		return err;
313 
314 	if (chip->usb_id == USB_ID(0x041e, 0x3042))
315 		err = snd_usb_ctl_msg(chip->dev,
316 				      usb_sndctrlpipe(chip->dev, 0), 0x24,
317 				      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
318 				      !value, 0, NULL, 0);
319 	/* USB X-Fi S51 Pro */
320 	if (chip->usb_id == USB_ID(0x041e, 0x30df))
321 		err = snd_usb_ctl_msg(chip->dev,
322 				      usb_sndctrlpipe(chip->dev, 0), 0x24,
323 				      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
324 				      !value, 0, NULL, 0);
325 	else
326 		err = snd_usb_ctl_msg(chip->dev,
327 				      usb_sndctrlpipe(chip->dev, 0), 0x24,
328 				      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
329 				      value, index + 2, NULL, 0);
330 	snd_usb_unlock_shutdown(chip);
331 	return err;
332 }
333 
snd_audigy2nx_led_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)334 static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol,
335 				 struct snd_ctl_elem_value *ucontrol)
336 {
337 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
338 	struct usb_mixer_interface *mixer = list->mixer;
339 	int index = kcontrol->private_value & 0xff;
340 	unsigned int value = ucontrol->value.integer.value[0];
341 	int old_value = kcontrol->private_value >> 8;
342 	int err;
343 
344 	if (value > 1)
345 		return -EINVAL;
346 	if (value == old_value)
347 		return 0;
348 	kcontrol->private_value = (value << 8) | index;
349 	err = snd_audigy2nx_led_update(mixer, value, index);
350 	return err < 0 ? err : 1;
351 }
352 
snd_audigy2nx_led_resume(struct usb_mixer_elem_list * list)353 static int snd_audigy2nx_led_resume(struct usb_mixer_elem_list *list)
354 {
355 	int priv_value = list->kctl->private_value;
356 
357 	return snd_audigy2nx_led_update(list->mixer, priv_value >> 8,
358 					priv_value & 0xff);
359 }
360 
361 /* name and private_value are set dynamically */
362 static const struct snd_kcontrol_new snd_audigy2nx_control = {
363 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
364 	.info = snd_audigy2nx_led_info,
365 	.get = snd_audigy2nx_led_get,
366 	.put = snd_audigy2nx_led_put,
367 };
368 
369 static const char * const snd_audigy2nx_led_names[] = {
370 	"CMSS LED Switch",
371 	"Power LED Switch",
372 	"Dolby Digital LED Switch",
373 };
374 
snd_audigy2nx_controls_create(struct usb_mixer_interface * mixer)375 static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
376 {
377 	int i, err;
378 
379 	for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_led_names); ++i) {
380 		struct snd_kcontrol_new knew;
381 
382 		/* USB X-Fi S51 doesn't have a CMSS LED */
383 		if (mixer->chip->usb_id == USB_ID(0x041e, 0x3042) && i == 0)
384 			continue;
385 		/* USB X-Fi S51 Pro doesn't have one either */
386 		if (mixer->chip->usb_id == USB_ID(0x041e, 0x30df) && i == 0)
387 			continue;
388 		if (i > 1 && /* Live24ext has 2 LEDs only */
389 			(mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
390 			 mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
391 			 mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
392 			 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
393 			break;
394 
395 		knew = snd_audigy2nx_control;
396 		knew.name = snd_audigy2nx_led_names[i];
397 		knew.private_value = (1 << 8) | i; /* LED on as default */
398 		err = add_single_ctl_with_resume(mixer, 0,
399 						 snd_audigy2nx_led_resume,
400 						 &knew, NULL);
401 		if (err < 0)
402 			return err;
403 	}
404 	return 0;
405 }
406 
snd_audigy2nx_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)407 static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
408 				    struct snd_info_buffer *buffer)
409 {
410 	static const struct sb_jack {
411 		int unitid;
412 		const char *name;
413 	}  jacks_audigy2nx[] = {
414 		{4,  "dig in "},
415 		{7,  "line in"},
416 		{19, "spk out"},
417 		{20, "hph out"},
418 		{-1, NULL}
419 	}, jacks_live24ext[] = {
420 		{4,  "line in"}, /* &1=Line, &2=Mic*/
421 		{3,  "hph out"}, /* headphones */
422 		{0,  "RC     "}, /* last command, 6 bytes see rc_config above */
423 		{-1, NULL}
424 	};
425 	const struct sb_jack *jacks;
426 	struct usb_mixer_interface *mixer = entry->private_data;
427 	int i, err;
428 	u8 buf[3];
429 
430 	snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
431 	if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
432 		jacks = jacks_audigy2nx;
433 	else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
434 		 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
435 		jacks = jacks_live24ext;
436 	else
437 		return;
438 
439 	for (i = 0; jacks[i].name; ++i) {
440 		snd_iprintf(buffer, "%s: ", jacks[i].name);
441 		err = snd_usb_lock_shutdown(mixer->chip);
442 		if (err < 0)
443 			return;
444 		err = snd_usb_ctl_msg(mixer->chip->dev,
445 				      usb_rcvctrlpipe(mixer->chip->dev, 0),
446 				      UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
447 				      USB_RECIP_INTERFACE, 0,
448 				      jacks[i].unitid << 8, buf, 3);
449 		snd_usb_unlock_shutdown(mixer->chip);
450 		if (err == 3 && (buf[0] == 3 || buf[0] == 6))
451 			snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
452 		else
453 			snd_iprintf(buffer, "?\n");
454 	}
455 }
456 
457 /* EMU0204 */
snd_emu0204_ch_switch_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)458 static int snd_emu0204_ch_switch_info(struct snd_kcontrol *kcontrol,
459 				      struct snd_ctl_elem_info *uinfo)
460 {
461 	static const char * const texts[2] = {"1/2", "3/4"};
462 
463 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
464 }
465 
snd_emu0204_ch_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)466 static int snd_emu0204_ch_switch_get(struct snd_kcontrol *kcontrol,
467 				     struct snd_ctl_elem_value *ucontrol)
468 {
469 	ucontrol->value.enumerated.item[0] = kcontrol->private_value;
470 	return 0;
471 }
472 
snd_emu0204_ch_switch_update(struct usb_mixer_interface * mixer,int value)473 static int snd_emu0204_ch_switch_update(struct usb_mixer_interface *mixer,
474 					int value)
475 {
476 	struct snd_usb_audio *chip = mixer->chip;
477 	int err;
478 	unsigned char buf[2];
479 
480 	err = snd_usb_lock_shutdown(chip);
481 	if (err < 0)
482 		return err;
483 
484 	buf[0] = 0x01;
485 	buf[1] = value ? 0x02 : 0x01;
486 	err = snd_usb_ctl_msg(chip->dev,
487 			      usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
488 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
489 			      0x0400, 0x0e00, buf, 2);
490 	snd_usb_unlock_shutdown(chip);
491 	return err;
492 }
493 
snd_emu0204_ch_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)494 static int snd_emu0204_ch_switch_put(struct snd_kcontrol *kcontrol,
495 				     struct snd_ctl_elem_value *ucontrol)
496 {
497 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
498 	struct usb_mixer_interface *mixer = list->mixer;
499 	unsigned int value = ucontrol->value.enumerated.item[0];
500 	int err;
501 
502 	if (value > 1)
503 		return -EINVAL;
504 
505 	if (value == kcontrol->private_value)
506 		return 0;
507 
508 	kcontrol->private_value = value;
509 	err = snd_emu0204_ch_switch_update(mixer, value);
510 	return err < 0 ? err : 1;
511 }
512 
snd_emu0204_ch_switch_resume(struct usb_mixer_elem_list * list)513 static int snd_emu0204_ch_switch_resume(struct usb_mixer_elem_list *list)
514 {
515 	return snd_emu0204_ch_switch_update(list->mixer,
516 					    list->kctl->private_value);
517 }
518 
519 static const struct snd_kcontrol_new snd_emu0204_control = {
520 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
521 	.name = "Front Jack Channels",
522 	.info = snd_emu0204_ch_switch_info,
523 	.get = snd_emu0204_ch_switch_get,
524 	.put = snd_emu0204_ch_switch_put,
525 	.private_value = 0,
526 };
527 
snd_emu0204_controls_create(struct usb_mixer_interface * mixer)528 static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
529 {
530 	return add_single_ctl_with_resume(mixer, 0,
531 					  snd_emu0204_ch_switch_resume,
532 					  &snd_emu0204_control, NULL);
533 }
534 
535 #if IS_REACHABLE(CONFIG_INPUT)
536 /*
537  * Sony DualSense controller (PS5) jack detection
538  *
539  * Since this is an UAC 1 device, it doesn't support jack detection.
540  * However, the controller hid-playstation driver reports HP & MIC
541  * insert events through a dedicated input device.
542  */
543 
544 #define SND_DUALSENSE_JACK_OUT_TERM_ID 3
545 #define SND_DUALSENSE_JACK_IN_TERM_ID 4
546 
547 struct dualsense_mixer_elem_info {
548 	struct usb_mixer_elem_info info;
549 	struct input_handler ih;
550 	struct input_device_id id_table[2];
551 	bool connected;
552 };
553 
snd_dualsense_ih_event(struct input_handle * handle,unsigned int type,unsigned int code,int value)554 static void snd_dualsense_ih_event(struct input_handle *handle,
555 				   unsigned int type, unsigned int code,
556 				   int value)
557 {
558 	struct dualsense_mixer_elem_info *mei;
559 	struct usb_mixer_elem_list *me;
560 
561 	if (type != EV_SW)
562 		return;
563 
564 	mei = container_of(handle->handler, struct dualsense_mixer_elem_info, ih);
565 	me = &mei->info.head;
566 
567 	if ((me->id == SND_DUALSENSE_JACK_OUT_TERM_ID && code == SW_HEADPHONE_INSERT) ||
568 	    (me->id == SND_DUALSENSE_JACK_IN_TERM_ID && code == SW_MICROPHONE_INSERT)) {
569 		mei->connected = !!value;
570 		snd_ctl_notify(me->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
571 			       &me->kctl->id);
572 	}
573 }
574 
snd_dualsense_ih_match(struct input_handler * handler,struct input_dev * dev)575 static bool snd_dualsense_ih_match(struct input_handler *handler,
576 				   struct input_dev *dev)
577 {
578 	struct dualsense_mixer_elem_info *mei;
579 	struct usb_device *snd_dev;
580 	char *input_dev_path, *usb_dev_path;
581 	size_t usb_dev_path_len;
582 	bool match = false;
583 
584 	mei = container_of(handler, struct dualsense_mixer_elem_info, ih);
585 	snd_dev = mei->info.head.mixer->chip->dev;
586 
587 	input_dev_path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
588 	if (!input_dev_path) {
589 		dev_warn(&snd_dev->dev, "Failed to get input dev path\n");
590 		return false;
591 	}
592 
593 	usb_dev_path = kobject_get_path(&snd_dev->dev.kobj, GFP_KERNEL);
594 	if (!usb_dev_path) {
595 		dev_warn(&snd_dev->dev, "Failed to get USB dev path\n");
596 		goto free_paths;
597 	}
598 
599 	/*
600 	 * Ensure the VID:PID matched input device supposedly owned by the
601 	 * hid-playstation driver belongs to the actual hardware handled by
602 	 * the current USB audio device, which implies input_dev_path being
603 	 * a subpath of usb_dev_path.
604 	 *
605 	 * This verification is necessary when there is more than one identical
606 	 * controller attached to the host system.
607 	 */
608 	usb_dev_path_len = strlen(usb_dev_path);
609 	if (usb_dev_path_len >= strlen(input_dev_path))
610 		goto free_paths;
611 
612 	usb_dev_path[usb_dev_path_len] = '/';
613 	match = !memcmp(input_dev_path, usb_dev_path, usb_dev_path_len + 1);
614 
615 free_paths:
616 	kfree(input_dev_path);
617 	kfree(usb_dev_path);
618 
619 	return match;
620 }
621 
snd_dualsense_ih_connect(struct input_handler * handler,struct input_dev * dev,const struct input_device_id * id)622 static int snd_dualsense_ih_connect(struct input_handler *handler,
623 				    struct input_dev *dev,
624 				    const struct input_device_id *id)
625 {
626 	struct input_handle *handle;
627 	int err;
628 
629 	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
630 	if (!handle)
631 		return -ENOMEM;
632 
633 	handle->dev = dev;
634 	handle->handler = handler;
635 	handle->name = handler->name;
636 
637 	err = input_register_handle(handle);
638 	if (err)
639 		goto err_free;
640 
641 	err = input_open_device(handle);
642 	if (err)
643 		goto err_unregister;
644 
645 	return 0;
646 
647 err_unregister:
648 	input_unregister_handle(handle);
649 err_free:
650 	kfree(handle);
651 	return err;
652 }
653 
snd_dualsense_ih_disconnect(struct input_handle * handle)654 static void snd_dualsense_ih_disconnect(struct input_handle *handle)
655 {
656 	input_close_device(handle);
657 	input_unregister_handle(handle);
658 	kfree(handle);
659 }
660 
snd_dualsense_ih_start(struct input_handle * handle)661 static void snd_dualsense_ih_start(struct input_handle *handle)
662 {
663 	struct dualsense_mixer_elem_info *mei;
664 	struct usb_mixer_elem_list *me;
665 	int status = -1;
666 
667 	mei = container_of(handle->handler, struct dualsense_mixer_elem_info, ih);
668 	me = &mei->info.head;
669 
670 	if (me->id == SND_DUALSENSE_JACK_OUT_TERM_ID &&
671 	    test_bit(SW_HEADPHONE_INSERT, handle->dev->swbit))
672 		status = test_bit(SW_HEADPHONE_INSERT, handle->dev->sw);
673 	else if (me->id == SND_DUALSENSE_JACK_IN_TERM_ID &&
674 		 test_bit(SW_MICROPHONE_INSERT, handle->dev->swbit))
675 		status = test_bit(SW_MICROPHONE_INSERT, handle->dev->sw);
676 
677 	if (status >= 0) {
678 		mei->connected = !!status;
679 		snd_ctl_notify(me->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
680 			       &me->kctl->id);
681 	}
682 }
683 
snd_dualsense_jack_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)684 static int snd_dualsense_jack_get(struct snd_kcontrol *kctl,
685 				  struct snd_ctl_elem_value *ucontrol)
686 {
687 	struct dualsense_mixer_elem_info *mei = snd_kcontrol_chip(kctl);
688 
689 	ucontrol->value.integer.value[0] = mei->connected;
690 
691 	return 0;
692 }
693 
694 static const struct snd_kcontrol_new snd_dualsense_jack_control = {
695 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
696 	.access = SNDRV_CTL_ELEM_ACCESS_READ,
697 	.info = snd_ctl_boolean_mono_info,
698 	.get = snd_dualsense_jack_get,
699 };
700 
snd_dualsense_resume_jack(struct usb_mixer_elem_list * list)701 static int snd_dualsense_resume_jack(struct usb_mixer_elem_list *list)
702 {
703 	snd_ctl_notify(list->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
704 		       &list->kctl->id);
705 	return 0;
706 }
707 
snd_dualsense_mixer_elem_free(struct snd_kcontrol * kctl)708 static void snd_dualsense_mixer_elem_free(struct snd_kcontrol *kctl)
709 {
710 	struct dualsense_mixer_elem_info *mei = snd_kcontrol_chip(kctl);
711 
712 	if (mei->ih.event)
713 		input_unregister_handler(&mei->ih);
714 
715 	snd_usb_mixer_elem_free(kctl);
716 }
717 
snd_dualsense_jack_create(struct usb_mixer_interface * mixer,const char * name,bool is_output)718 static int snd_dualsense_jack_create(struct usb_mixer_interface *mixer,
719 				     const char *name, bool is_output)
720 {
721 	struct dualsense_mixer_elem_info *mei;
722 	struct input_device_id *idev_id;
723 	struct snd_kcontrol *kctl;
724 	int err;
725 
726 	mei = kzalloc(sizeof(*mei), GFP_KERNEL);
727 	if (!mei)
728 		return -ENOMEM;
729 
730 	snd_usb_mixer_elem_init_std(&mei->info.head, mixer,
731 				    is_output ? SND_DUALSENSE_JACK_OUT_TERM_ID :
732 						SND_DUALSENSE_JACK_IN_TERM_ID);
733 
734 	mei->info.head.resume = snd_dualsense_resume_jack;
735 	mei->info.val_type = USB_MIXER_BOOLEAN;
736 	mei->info.channels = 1;
737 	mei->info.min = 0;
738 	mei->info.max = 1;
739 
740 	kctl = snd_ctl_new1(&snd_dualsense_jack_control, mei);
741 	if (!kctl) {
742 		kfree(mei);
743 		return -ENOMEM;
744 	}
745 
746 	strscpy(kctl->id.name, name, sizeof(kctl->id.name));
747 	kctl->private_free = snd_dualsense_mixer_elem_free;
748 
749 	err = snd_usb_mixer_add_control(&mei->info.head, kctl);
750 	if (err)
751 		return err;
752 
753 	idev_id = &mei->id_table[0];
754 	idev_id->flags = INPUT_DEVICE_ID_MATCH_VENDOR | INPUT_DEVICE_ID_MATCH_PRODUCT |
755 			 INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT;
756 	idev_id->vendor = USB_ID_VENDOR(mixer->chip->usb_id);
757 	idev_id->product = USB_ID_PRODUCT(mixer->chip->usb_id);
758 	idev_id->evbit[BIT_WORD(EV_SW)] = BIT_MASK(EV_SW);
759 	if (is_output)
760 		idev_id->swbit[BIT_WORD(SW_HEADPHONE_INSERT)] = BIT_MASK(SW_HEADPHONE_INSERT);
761 	else
762 		idev_id->swbit[BIT_WORD(SW_MICROPHONE_INSERT)] = BIT_MASK(SW_MICROPHONE_INSERT);
763 
764 	mei->ih.event = snd_dualsense_ih_event;
765 	mei->ih.match = snd_dualsense_ih_match;
766 	mei->ih.connect = snd_dualsense_ih_connect;
767 	mei->ih.disconnect = snd_dualsense_ih_disconnect;
768 	mei->ih.start = snd_dualsense_ih_start;
769 	mei->ih.name = name;
770 	mei->ih.id_table = mei->id_table;
771 
772 	err = input_register_handler(&mei->ih);
773 	if (err) {
774 		dev_warn(&mixer->chip->dev->dev,
775 			 "Could not register input handler: %d\n", err);
776 		mei->ih.event = NULL;
777 	}
778 
779 	return 0;
780 }
781 
snd_dualsense_controls_create(struct usb_mixer_interface * mixer)782 static int snd_dualsense_controls_create(struct usb_mixer_interface *mixer)
783 {
784 	int err;
785 
786 	err = snd_dualsense_jack_create(mixer, "Headphone Jack", true);
787 	if (err < 0)
788 		return err;
789 
790 	return snd_dualsense_jack_create(mixer, "Headset Mic Jack", false);
791 }
792 #endif /* IS_REACHABLE(CONFIG_INPUT) */
793 
794 /* ASUS Xonar U1 / U3 controls */
795 
snd_xonar_u1_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)796 static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
797 				   struct snd_ctl_elem_value *ucontrol)
798 {
799 	ucontrol->value.integer.value[0] = !!(kcontrol->private_value & 0x02);
800 	return 0;
801 }
802 
snd_xonar_u1_switch_update(struct usb_mixer_interface * mixer,unsigned char status)803 static int snd_xonar_u1_switch_update(struct usb_mixer_interface *mixer,
804 				      unsigned char status)
805 {
806 	struct snd_usb_audio *chip = mixer->chip;
807 	int err;
808 
809 	err = snd_usb_lock_shutdown(chip);
810 	if (err < 0)
811 		return err;
812 	err = snd_usb_ctl_msg(chip->dev,
813 			      usb_sndctrlpipe(chip->dev, 0), 0x08,
814 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
815 			      50, 0, &status, 1);
816 	snd_usb_unlock_shutdown(chip);
817 	return err;
818 }
819 
snd_xonar_u1_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)820 static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
821 				   struct snd_ctl_elem_value *ucontrol)
822 {
823 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
824 	u8 old_status, new_status;
825 	int err;
826 
827 	old_status = kcontrol->private_value;
828 	if (ucontrol->value.integer.value[0])
829 		new_status = old_status | 0x02;
830 	else
831 		new_status = old_status & ~0x02;
832 	if (new_status == old_status)
833 		return 0;
834 
835 	kcontrol->private_value = new_status;
836 	err = snd_xonar_u1_switch_update(list->mixer, new_status);
837 	return err < 0 ? err : 1;
838 }
839 
snd_xonar_u1_switch_resume(struct usb_mixer_elem_list * list)840 static int snd_xonar_u1_switch_resume(struct usb_mixer_elem_list *list)
841 {
842 	return snd_xonar_u1_switch_update(list->mixer,
843 					  list->kctl->private_value);
844 }
845 
846 static const struct snd_kcontrol_new snd_xonar_u1_output_switch = {
847 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
848 	.name = "Digital Playback Switch",
849 	.info = snd_ctl_boolean_mono_info,
850 	.get = snd_xonar_u1_switch_get,
851 	.put = snd_xonar_u1_switch_put,
852 	.private_value = 0x05,
853 };
854 
snd_xonar_u1_controls_create(struct usb_mixer_interface * mixer)855 static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
856 {
857 	return add_single_ctl_with_resume(mixer, 0,
858 					  snd_xonar_u1_switch_resume,
859 					  &snd_xonar_u1_output_switch, NULL);
860 }
861 
862 /* Digidesign Mbox 1 helper functions */
863 
snd_mbox1_is_spdif_synced(struct snd_usb_audio * chip)864 static int snd_mbox1_is_spdif_synced(struct snd_usb_audio *chip)
865 {
866 	unsigned char buff[3];
867 	int err;
868 	int is_spdif_synced;
869 
870 	/* Read clock source */
871 	err = snd_usb_ctl_msg(chip->dev,
872 			      usb_rcvctrlpipe(chip->dev, 0), 0x81,
873 			      USB_DIR_IN |
874 			      USB_TYPE_CLASS |
875 			      USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
876 	if (err < 0)
877 		return err;
878 
879 	/* spdif sync: buff is all zeroes */
880 	is_spdif_synced = !(buff[0] | buff[1] | buff[2]);
881 	return is_spdif_synced;
882 }
883 
snd_mbox1_set_clk_source(struct snd_usb_audio * chip,int rate_or_zero)884 static int snd_mbox1_set_clk_source(struct snd_usb_audio *chip, int rate_or_zero)
885 {
886 	/* 2 possibilities:	Internal    -> expects sample rate
887 	 *			S/PDIF sync -> expects rate = 0
888 	 */
889 	unsigned char buff[3];
890 
891 	buff[0] = (rate_or_zero >>  0) & 0xff;
892 	buff[1] = (rate_or_zero >>  8) & 0xff;
893 	buff[2] = (rate_or_zero >> 16) & 0xff;
894 
895 	/* Set clock source */
896 	return snd_usb_ctl_msg(chip->dev,
897 			       usb_sndctrlpipe(chip->dev, 0), 0x1,
898 			       USB_TYPE_CLASS |
899 			       USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
900 }
901 
snd_mbox1_is_spdif_input(struct snd_usb_audio * chip)902 static int snd_mbox1_is_spdif_input(struct snd_usb_audio *chip)
903 {
904 	/* Hardware gives 2 possibilities:	ANALOG Source  -> 0x01
905 	 *					S/PDIF Source  -> 0x02
906 	 */
907 	int err;
908 	unsigned char source[1];
909 
910 	/* Read input source */
911 	err = snd_usb_ctl_msg(chip->dev,
912 			      usb_rcvctrlpipe(chip->dev, 0), 0x81,
913 			      USB_DIR_IN |
914 			      USB_TYPE_CLASS |
915 			      USB_RECIP_INTERFACE, 0x00, 0x500, source, 1);
916 	if (err < 0)
917 		return err;
918 
919 	return (source[0] == 2);
920 }
921 
snd_mbox1_set_input_source(struct snd_usb_audio * chip,int is_spdif)922 static int snd_mbox1_set_input_source(struct snd_usb_audio *chip, int is_spdif)
923 {
924 	/* NB: Setting the input source to S/PDIF resets the clock source to S/PDIF
925 	 * Hardware expects 2 possibilities:	ANALOG Source  -> 0x01
926 	 *					S/PDIF Source  -> 0x02
927 	 */
928 	unsigned char buff[1];
929 
930 	buff[0] = (is_spdif & 1) + 1;
931 
932 	/* Set input source */
933 	return snd_usb_ctl_msg(chip->dev,
934 			       usb_sndctrlpipe(chip->dev, 0), 0x1,
935 			       USB_TYPE_CLASS |
936 			       USB_RECIP_INTERFACE, 0x00, 0x500, buff, 1);
937 }
938 
939 /* Digidesign Mbox 1 clock source switch (internal/spdif) */
940 
snd_mbox1_clk_switch_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)941 static int snd_mbox1_clk_switch_get(struct snd_kcontrol *kctl,
942 				    struct snd_ctl_elem_value *ucontrol)
943 {
944 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
945 	struct snd_usb_audio *chip = list->mixer->chip;
946 	int err;
947 
948 	err = snd_usb_lock_shutdown(chip);
949 	if (err < 0)
950 		goto err;
951 
952 	err = snd_mbox1_is_spdif_synced(chip);
953 	if (err < 0)
954 		goto err;
955 
956 	kctl->private_value = err;
957 	err = 0;
958 	ucontrol->value.enumerated.item[0] = kctl->private_value;
959 err:
960 	snd_usb_unlock_shutdown(chip);
961 	return err;
962 }
963 
snd_mbox1_clk_switch_update(struct usb_mixer_interface * mixer,int is_spdif_sync)964 static int snd_mbox1_clk_switch_update(struct usb_mixer_interface *mixer, int is_spdif_sync)
965 {
966 	struct snd_usb_audio *chip = mixer->chip;
967 	int err;
968 
969 	err = snd_usb_lock_shutdown(chip);
970 	if (err < 0)
971 		return err;
972 
973 	err = snd_mbox1_is_spdif_input(chip);
974 	if (err < 0)
975 		goto err;
976 
977 	err = snd_mbox1_is_spdif_synced(chip);
978 	if (err < 0)
979 		goto err;
980 
981 	/* FIXME: hardcoded sample rate */
982 	err = snd_mbox1_set_clk_source(chip, is_spdif_sync ? 0 : 48000);
983 	if (err < 0)
984 		goto err;
985 
986 	err = snd_mbox1_is_spdif_synced(chip);
987 err:
988 	snd_usb_unlock_shutdown(chip);
989 	return err;
990 }
991 
snd_mbox1_clk_switch_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)992 static int snd_mbox1_clk_switch_put(struct snd_kcontrol *kctl,
993 				    struct snd_ctl_elem_value *ucontrol)
994 {
995 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
996 	struct usb_mixer_interface *mixer = list->mixer;
997 	int err;
998 	bool cur_val, new_val;
999 
1000 	cur_val = kctl->private_value;
1001 	new_val = ucontrol->value.enumerated.item[0];
1002 	if (cur_val == new_val)
1003 		return 0;
1004 
1005 	kctl->private_value = new_val;
1006 	err = snd_mbox1_clk_switch_update(mixer, new_val);
1007 	return err < 0 ? err : 1;
1008 }
1009 
snd_mbox1_clk_switch_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1010 static int snd_mbox1_clk_switch_info(struct snd_kcontrol *kcontrol,
1011 				     struct snd_ctl_elem_info *uinfo)
1012 {
1013 	static const char *const texts[2] = {
1014 		"Internal",
1015 		"S/PDIF"
1016 	};
1017 
1018 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1019 }
1020 
snd_mbox1_clk_switch_resume(struct usb_mixer_elem_list * list)1021 static int snd_mbox1_clk_switch_resume(struct usb_mixer_elem_list *list)
1022 {
1023 	return snd_mbox1_clk_switch_update(list->mixer, list->kctl->private_value);
1024 }
1025 
1026 /* Digidesign Mbox 1 input source switch (analog/spdif) */
1027 
snd_mbox1_src_switch_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)1028 static int snd_mbox1_src_switch_get(struct snd_kcontrol *kctl,
1029 				    struct snd_ctl_elem_value *ucontrol)
1030 {
1031 	ucontrol->value.enumerated.item[0] = kctl->private_value;
1032 	return 0;
1033 }
1034 
snd_mbox1_src_switch_update(struct usb_mixer_interface * mixer,int is_spdif_input)1035 static int snd_mbox1_src_switch_update(struct usb_mixer_interface *mixer, int is_spdif_input)
1036 {
1037 	struct snd_usb_audio *chip = mixer->chip;
1038 	int err;
1039 
1040 	err = snd_usb_lock_shutdown(chip);
1041 	if (err < 0)
1042 		return err;
1043 
1044 	err = snd_mbox1_is_spdif_input(chip);
1045 	if (err < 0)
1046 		goto err;
1047 
1048 	err = snd_mbox1_set_input_source(chip, is_spdif_input);
1049 	if (err < 0)
1050 		goto err;
1051 
1052 	err = snd_mbox1_is_spdif_input(chip);
1053 	if (err < 0)
1054 		goto err;
1055 
1056 	err = snd_mbox1_is_spdif_synced(chip);
1057 err:
1058 	snd_usb_unlock_shutdown(chip);
1059 	return err;
1060 }
1061 
snd_mbox1_src_switch_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)1062 static int snd_mbox1_src_switch_put(struct snd_kcontrol *kctl,
1063 				    struct snd_ctl_elem_value *ucontrol)
1064 {
1065 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
1066 	struct usb_mixer_interface *mixer = list->mixer;
1067 	int err;
1068 	bool cur_val, new_val;
1069 
1070 	cur_val = kctl->private_value;
1071 	new_val = ucontrol->value.enumerated.item[0];
1072 	if (cur_val == new_val)
1073 		return 0;
1074 
1075 	kctl->private_value = new_val;
1076 	err = snd_mbox1_src_switch_update(mixer, new_val);
1077 	return err < 0 ? err : 1;
1078 }
1079 
snd_mbox1_src_switch_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1080 static int snd_mbox1_src_switch_info(struct snd_kcontrol *kcontrol,
1081 				     struct snd_ctl_elem_info *uinfo)
1082 {
1083 	static const char *const texts[2] = {
1084 		"Analog",
1085 		"S/PDIF"
1086 	};
1087 
1088 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1089 }
1090 
snd_mbox1_src_switch_resume(struct usb_mixer_elem_list * list)1091 static int snd_mbox1_src_switch_resume(struct usb_mixer_elem_list *list)
1092 {
1093 	return snd_mbox1_src_switch_update(list->mixer, list->kctl->private_value);
1094 }
1095 
1096 static const struct snd_kcontrol_new snd_mbox1_clk_switch = {
1097 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1098 	.name = "Clock Source",
1099 	.index = 0,
1100 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1101 	.info = snd_mbox1_clk_switch_info,
1102 	.get = snd_mbox1_clk_switch_get,
1103 	.put = snd_mbox1_clk_switch_put,
1104 	.private_value = 0
1105 };
1106 
1107 static const struct snd_kcontrol_new snd_mbox1_src_switch = {
1108 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1109 	.name = "Input Source",
1110 	.index = 1,
1111 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1112 	.info = snd_mbox1_src_switch_info,
1113 	.get = snd_mbox1_src_switch_get,
1114 	.put = snd_mbox1_src_switch_put,
1115 	.private_value = 0
1116 };
1117 
snd_mbox1_controls_create(struct usb_mixer_interface * mixer)1118 static int snd_mbox1_controls_create(struct usb_mixer_interface *mixer)
1119 {
1120 	int err;
1121 
1122 	err = add_single_ctl_with_resume(mixer, 0,
1123 					 snd_mbox1_clk_switch_resume,
1124 					 &snd_mbox1_clk_switch, NULL);
1125 	if (err < 0)
1126 		return err;
1127 
1128 	return add_single_ctl_with_resume(mixer, 1,
1129 					  snd_mbox1_src_switch_resume,
1130 					  &snd_mbox1_src_switch, NULL);
1131 }
1132 
1133 /* Native Instruments device quirks */
1134 
1135 #define _MAKE_NI_CONTROL(bRequest, wIndex) ((bRequest) << 16 | (wIndex))
1136 
snd_ni_control_init_val(struct usb_mixer_interface * mixer,struct snd_kcontrol * kctl)1137 static int snd_ni_control_init_val(struct usb_mixer_interface *mixer,
1138 				   struct snd_kcontrol *kctl)
1139 {
1140 	struct usb_device *dev = mixer->chip->dev;
1141 	unsigned int pval = kctl->private_value;
1142 	u8 value;
1143 	int err;
1144 
1145 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1146 			      (pval >> 16) & 0xff,
1147 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
1148 			      0, pval & 0xffff, &value, 1);
1149 	if (err < 0) {
1150 		dev_err(&dev->dev,
1151 			"unable to issue vendor read request (ret = %d)", err);
1152 		return err;
1153 	}
1154 
1155 	kctl->private_value |= ((unsigned int)value << 24);
1156 	return 0;
1157 }
1158 
snd_nativeinstruments_control_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1159 static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
1160 					     struct snd_ctl_elem_value *ucontrol)
1161 {
1162 	ucontrol->value.integer.value[0] = kcontrol->private_value >> 24;
1163 	return 0;
1164 }
1165 
snd_ni_update_cur_val(struct usb_mixer_elem_list * list)1166 static int snd_ni_update_cur_val(struct usb_mixer_elem_list *list)
1167 {
1168 	struct snd_usb_audio *chip = list->mixer->chip;
1169 	unsigned int pval = list->kctl->private_value;
1170 	int err;
1171 
1172 	err = snd_usb_lock_shutdown(chip);
1173 	if (err < 0)
1174 		return err;
1175 	err = usb_control_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
1176 			      (pval >> 16) & 0xff,
1177 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
1178 			      pval >> 24, pval & 0xffff, NULL, 0, 1000);
1179 	snd_usb_unlock_shutdown(chip);
1180 	return err;
1181 }
1182 
snd_nativeinstruments_control_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1183 static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
1184 					     struct snd_ctl_elem_value *ucontrol)
1185 {
1186 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1187 	u8 oldval = (kcontrol->private_value >> 24) & 0xff;
1188 	u8 newval = ucontrol->value.integer.value[0];
1189 	int err;
1190 
1191 	if (oldval == newval)
1192 		return 0;
1193 
1194 	kcontrol->private_value &= ~(0xff << 24);
1195 	kcontrol->private_value |= (unsigned int)newval << 24;
1196 	err = snd_ni_update_cur_val(list);
1197 	return err < 0 ? err : 1;
1198 }
1199 
1200 static const struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
1201 	{
1202 		.name = "Direct Thru Channel A",
1203 		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
1204 	},
1205 	{
1206 		.name = "Direct Thru Channel B",
1207 		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
1208 	},
1209 	{
1210 		.name = "Phono Input Channel A",
1211 		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
1212 	},
1213 	{
1214 		.name = "Phono Input Channel B",
1215 		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
1216 	},
1217 };
1218 
1219 static const struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
1220 	{
1221 		.name = "Direct Thru Channel A",
1222 		.private_value = _MAKE_NI_CONTROL(0x01, 0x03),
1223 	},
1224 	{
1225 		.name = "Direct Thru Channel B",
1226 		.private_value = _MAKE_NI_CONTROL(0x01, 0x05),
1227 	},
1228 	{
1229 		.name = "Direct Thru Channel C",
1230 		.private_value = _MAKE_NI_CONTROL(0x01, 0x07),
1231 	},
1232 	{
1233 		.name = "Direct Thru Channel D",
1234 		.private_value = _MAKE_NI_CONTROL(0x01, 0x09),
1235 	},
1236 	{
1237 		.name = "Phono Input Channel A",
1238 		.private_value = _MAKE_NI_CONTROL(0x02, 0x03),
1239 	},
1240 	{
1241 		.name = "Phono Input Channel B",
1242 		.private_value = _MAKE_NI_CONTROL(0x02, 0x05),
1243 	},
1244 	{
1245 		.name = "Phono Input Channel C",
1246 		.private_value = _MAKE_NI_CONTROL(0x02, 0x07),
1247 	},
1248 	{
1249 		.name = "Phono Input Channel D",
1250 		.private_value = _MAKE_NI_CONTROL(0x02, 0x09),
1251 	},
1252 };
1253 
snd_nativeinstruments_create_mixer(struct usb_mixer_interface * mixer,const struct snd_kcontrol_new * kc,unsigned int count)1254 static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
1255 					      const struct snd_kcontrol_new *kc,
1256 					      unsigned int count)
1257 {
1258 	int i, err = 0;
1259 	struct snd_kcontrol_new template = {
1260 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1261 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1262 		.get = snd_nativeinstruments_control_get,
1263 		.put = snd_nativeinstruments_control_put,
1264 		.info = snd_ctl_boolean_mono_info,
1265 	};
1266 
1267 	for (i = 0; i < count; i++) {
1268 		struct usb_mixer_elem_list *list;
1269 
1270 		template.name = kc[i].name;
1271 		template.private_value = kc[i].private_value;
1272 
1273 		err = add_single_ctl_with_resume(mixer, 0,
1274 						 snd_ni_update_cur_val,
1275 						 &template, &list);
1276 		if (err < 0)
1277 			break;
1278 		snd_ni_control_init_val(mixer, list->kctl);
1279 	}
1280 
1281 	return err;
1282 }
1283 
1284 /* M-Audio FastTrack Ultra quirks */
1285 /* FTU Effect switch (also used by C400/C600) */
snd_ftu_eff_switch_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1286 static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
1287 				   struct snd_ctl_elem_info *uinfo)
1288 {
1289 	static const char *const texts[8] = {
1290 		"Room 1", "Room 2", "Room 3", "Hall 1",
1291 		"Hall 2", "Plate", "Delay", "Echo"
1292 	};
1293 
1294 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1295 }
1296 
snd_ftu_eff_switch_init(struct usb_mixer_interface * mixer,struct snd_kcontrol * kctl)1297 static int snd_ftu_eff_switch_init(struct usb_mixer_interface *mixer,
1298 				   struct snd_kcontrol *kctl)
1299 {
1300 	struct usb_device *dev = mixer->chip->dev;
1301 	unsigned int pval = kctl->private_value;
1302 	int err;
1303 	unsigned char value[2];
1304 
1305 	value[0] = 0x00;
1306 	value[1] = 0x00;
1307 
1308 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
1309 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1310 			      pval & 0xff00,
1311 			      snd_usb_ctrl_intf(mixer->hostif) | ((pval & 0xff) << 8),
1312 			      value, 2);
1313 	if (err < 0)
1314 		return err;
1315 
1316 	kctl->private_value |= (unsigned int)value[0] << 24;
1317 	return 0;
1318 }
1319 
snd_ftu_eff_switch_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)1320 static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
1321 				  struct snd_ctl_elem_value *ucontrol)
1322 {
1323 	ucontrol->value.enumerated.item[0] = kctl->private_value >> 24;
1324 	return 0;
1325 }
1326 
snd_ftu_eff_switch_update(struct usb_mixer_elem_list * list)1327 static int snd_ftu_eff_switch_update(struct usb_mixer_elem_list *list)
1328 {
1329 	struct snd_usb_audio *chip = list->mixer->chip;
1330 	unsigned int pval = list->kctl->private_value;
1331 	unsigned char value[2];
1332 	int err;
1333 
1334 	value[0] = pval >> 24;
1335 	value[1] = 0;
1336 
1337 	err = snd_usb_lock_shutdown(chip);
1338 	if (err < 0)
1339 		return err;
1340 	err = snd_usb_ctl_msg(chip->dev,
1341 			      usb_sndctrlpipe(chip->dev, 0),
1342 			      UAC_SET_CUR,
1343 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1344 			      pval & 0xff00,
1345 			      snd_usb_ctrl_intf(list->mixer->hostif) | ((pval & 0xff) << 8),
1346 			      value, 2);
1347 	snd_usb_unlock_shutdown(chip);
1348 	return err;
1349 }
1350 
snd_ftu_eff_switch_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)1351 static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
1352 				  struct snd_ctl_elem_value *ucontrol)
1353 {
1354 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
1355 	unsigned int pval = list->kctl->private_value;
1356 	int cur_val, err, new_val;
1357 
1358 	cur_val = pval >> 24;
1359 	new_val = ucontrol->value.enumerated.item[0];
1360 	if (cur_val == new_val)
1361 		return 0;
1362 
1363 	kctl->private_value &= ~(0xff << 24);
1364 	kctl->private_value |= new_val << 24;
1365 	err = snd_ftu_eff_switch_update(list);
1366 	return err < 0 ? err : 1;
1367 }
1368 
snd_ftu_create_effect_switch(struct usb_mixer_interface * mixer,int validx,int bUnitID)1369 static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer,
1370 					int validx, int bUnitID)
1371 {
1372 	static struct snd_kcontrol_new template = {
1373 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1374 		.name = "Effect Program Switch",
1375 		.index = 0,
1376 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1377 		.info = snd_ftu_eff_switch_info,
1378 		.get = snd_ftu_eff_switch_get,
1379 		.put = snd_ftu_eff_switch_put
1380 	};
1381 	struct usb_mixer_elem_list *list;
1382 	int err;
1383 
1384 	err = add_single_ctl_with_resume(mixer, bUnitID,
1385 					 snd_ftu_eff_switch_update,
1386 					 &template, &list);
1387 	if (err < 0)
1388 		return err;
1389 	list->kctl->private_value = (validx << 8) | bUnitID;
1390 	snd_ftu_eff_switch_init(mixer, list->kctl);
1391 	return 0;
1392 }
1393 
1394 /* Create volume controls for FTU devices*/
snd_ftu_create_volume_ctls(struct usb_mixer_interface * mixer)1395 static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
1396 {
1397 	char name[64];
1398 	unsigned int control, cmask;
1399 	int in, out, err;
1400 
1401 	const unsigned int id = 5;
1402 	const int val_type = USB_MIXER_S16;
1403 
1404 	for (out = 0; out < 8; out++) {
1405 		control = out + 1;
1406 		for (in = 0; in < 8; in++) {
1407 			cmask = BIT(in);
1408 			snprintf(name, sizeof(name),
1409 				 "AIn%d - Out%d Capture Volume",
1410 				 in  + 1, out + 1);
1411 			err = snd_create_std_mono_ctl(mixer, id, control,
1412 						      cmask, val_type, name,
1413 						      &snd_usb_mixer_vol_tlv);
1414 			if (err < 0)
1415 				return err;
1416 		}
1417 		for (in = 8; in < 16; in++) {
1418 			cmask = BIT(in);
1419 			snprintf(name, sizeof(name),
1420 				 "DIn%d - Out%d Playback Volume",
1421 				 in - 7, out + 1);
1422 			err = snd_create_std_mono_ctl(mixer, id, control,
1423 						      cmask, val_type, name,
1424 						      &snd_usb_mixer_vol_tlv);
1425 			if (err < 0)
1426 				return err;
1427 		}
1428 	}
1429 
1430 	return 0;
1431 }
1432 
1433 /* This control needs a volume quirk, see mixer.c */
snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface * mixer)1434 static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1435 {
1436 	static const char name[] = "Effect Volume";
1437 	const unsigned int id = 6;
1438 	const int val_type = USB_MIXER_U8;
1439 	const unsigned int control = 2;
1440 	const unsigned int cmask = 0;
1441 
1442 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1443 					name, snd_usb_mixer_vol_tlv);
1444 }
1445 
1446 /* This control needs a volume quirk, see mixer.c */
snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface * mixer)1447 static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1448 {
1449 	static const char name[] = "Effect Duration";
1450 	const unsigned int id = 6;
1451 	const int val_type = USB_MIXER_S16;
1452 	const unsigned int control = 3;
1453 	const unsigned int cmask = 0;
1454 
1455 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1456 					name, snd_usb_mixer_vol_tlv);
1457 }
1458 
1459 /* This control needs a volume quirk, see mixer.c */
snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface * mixer)1460 static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1461 {
1462 	static const char name[] = "Effect Feedback Volume";
1463 	const unsigned int id = 6;
1464 	const int val_type = USB_MIXER_U8;
1465 	const unsigned int control = 4;
1466 	const unsigned int cmask = 0;
1467 
1468 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1469 					name, NULL);
1470 }
1471 
snd_ftu_create_effect_return_ctls(struct usb_mixer_interface * mixer)1472 static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
1473 {
1474 	unsigned int cmask;
1475 	int err, ch;
1476 	char name[48];
1477 
1478 	const unsigned int id = 7;
1479 	const int val_type = USB_MIXER_S16;
1480 	const unsigned int control = 7;
1481 
1482 	for (ch = 0; ch < 4; ++ch) {
1483 		cmask = BIT(ch);
1484 		snprintf(name, sizeof(name),
1485 			 "Effect Return %d Volume", ch + 1);
1486 		err = snd_create_std_mono_ctl(mixer, id, control,
1487 					      cmask, val_type, name,
1488 					      snd_usb_mixer_vol_tlv);
1489 		if (err < 0)
1490 			return err;
1491 	}
1492 
1493 	return 0;
1494 }
1495 
snd_ftu_create_effect_send_ctls(struct usb_mixer_interface * mixer)1496 static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
1497 {
1498 	unsigned int  cmask;
1499 	int err, ch;
1500 	char name[48];
1501 
1502 	const unsigned int id = 5;
1503 	const int val_type = USB_MIXER_S16;
1504 	const unsigned int control = 9;
1505 
1506 	for (ch = 0; ch < 8; ++ch) {
1507 		cmask = BIT(ch);
1508 		snprintf(name, sizeof(name),
1509 			 "Effect Send AIn%d Volume", ch + 1);
1510 		err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1511 					      val_type, name,
1512 					      snd_usb_mixer_vol_tlv);
1513 		if (err < 0)
1514 			return err;
1515 	}
1516 	for (ch = 8; ch < 16; ++ch) {
1517 		cmask = BIT(ch);
1518 		snprintf(name, sizeof(name),
1519 			 "Effect Send DIn%d Volume", ch - 7);
1520 		err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1521 					      val_type, name,
1522 					      snd_usb_mixer_vol_tlv);
1523 		if (err < 0)
1524 			return err;
1525 	}
1526 	return 0;
1527 }
1528 
snd_ftu_create_mixer(struct usb_mixer_interface * mixer)1529 static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
1530 {
1531 	int err;
1532 
1533 	err = snd_ftu_create_volume_ctls(mixer);
1534 	if (err < 0)
1535 		return err;
1536 
1537 	err = snd_ftu_create_effect_switch(mixer, 1, 6);
1538 	if (err < 0)
1539 		return err;
1540 
1541 	err = snd_ftu_create_effect_volume_ctl(mixer);
1542 	if (err < 0)
1543 		return err;
1544 
1545 	err = snd_ftu_create_effect_duration_ctl(mixer);
1546 	if (err < 0)
1547 		return err;
1548 
1549 	err = snd_ftu_create_effect_feedback_ctl(mixer);
1550 	if (err < 0)
1551 		return err;
1552 
1553 	err = snd_ftu_create_effect_return_ctls(mixer);
1554 	if (err < 0)
1555 		return err;
1556 
1557 	err = snd_ftu_create_effect_send_ctls(mixer);
1558 	if (err < 0)
1559 		return err;
1560 
1561 	return 0;
1562 }
1563 
snd_emuusb_set_samplerate(struct snd_usb_audio * chip,unsigned char samplerate_id)1564 void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
1565 			       unsigned char samplerate_id)
1566 {
1567 	struct usb_mixer_interface *mixer;
1568 	struct usb_mixer_elem_info *cval;
1569 	int unitid = 12; /* SampleRate ExtensionUnit ID */
1570 
1571 	list_for_each_entry(mixer, &chip->mixer_list, list) {
1572 		if (mixer->id_elems[unitid]) {
1573 			cval = mixer_elem_list_to_info(mixer->id_elems[unitid]);
1574 			snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1575 						    cval->control << 8,
1576 						    samplerate_id);
1577 			snd_usb_mixer_notify_id(mixer, unitid);
1578 			break;
1579 		}
1580 	}
1581 }
1582 
1583 /* M-Audio Fast Track C400/C600 */
1584 /* C400/C600 volume controls, this control needs a volume quirk, see mixer.c */
snd_c400_create_vol_ctls(struct usb_mixer_interface * mixer)1585 static int snd_c400_create_vol_ctls(struct usb_mixer_interface *mixer)
1586 {
1587 	char name[64];
1588 	unsigned int cmask, offset;
1589 	int out, chan, err;
1590 	int num_outs = 0;
1591 	int num_ins = 0;
1592 
1593 	const unsigned int id = 0x40;
1594 	const int val_type = USB_MIXER_S16;
1595 	const int control = 1;
1596 
1597 	switch (mixer->chip->usb_id) {
1598 	case USB_ID(0x0763, 0x2030):
1599 		num_outs = 6;
1600 		num_ins = 4;
1601 		break;
1602 	case USB_ID(0x0763, 0x2031):
1603 		num_outs = 8;
1604 		num_ins = 6;
1605 		break;
1606 	}
1607 
1608 	for (chan = 0; chan < num_outs + num_ins; chan++) {
1609 		for (out = 0; out < num_outs; out++) {
1610 			if (chan < num_outs) {
1611 				snprintf(name, sizeof(name),
1612 					 "PCM%d-Out%d Playback Volume",
1613 					 chan + 1, out + 1);
1614 			} else {
1615 				snprintf(name, sizeof(name),
1616 					 "In%d-Out%d Playback Volume",
1617 					 chan - num_outs + 1, out + 1);
1618 			}
1619 
1620 			cmask = (out == 0) ? 0 : BIT(out - 1);
1621 			offset = chan * num_outs;
1622 			err = snd_create_std_mono_ctl_offset(mixer, id, control,
1623 							     cmask, val_type, offset, name,
1624 							     &snd_usb_mixer_vol_tlv);
1625 			if (err < 0)
1626 				return err;
1627 		}
1628 	}
1629 
1630 	return 0;
1631 }
1632 
1633 /* This control needs a volume quirk, see mixer.c */
snd_c400_create_effect_volume_ctl(struct usb_mixer_interface * mixer)1634 static int snd_c400_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1635 {
1636 	static const char name[] = "Effect Volume";
1637 	const unsigned int id = 0x43;
1638 	const int val_type = USB_MIXER_U8;
1639 	const unsigned int control = 3;
1640 	const unsigned int cmask = 0;
1641 
1642 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1643 				       name, snd_usb_mixer_vol_tlv);
1644 }
1645 
1646 /* This control needs a volume quirk, see mixer.c */
snd_c400_create_effect_duration_ctl(struct usb_mixer_interface * mixer)1647 static int snd_c400_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1648 {
1649 	static const char name[] = "Effect Duration";
1650 	const unsigned int id = 0x43;
1651 	const int val_type = USB_MIXER_S16;
1652 	const unsigned int control = 4;
1653 	const unsigned int cmask = 0;
1654 
1655 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1656 				       name, snd_usb_mixer_vol_tlv);
1657 }
1658 
1659 /* This control needs a volume quirk, see mixer.c */
snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface * mixer)1660 static int snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1661 {
1662 	static const char name[] = "Effect Feedback Volume";
1663 	const unsigned int id = 0x43;
1664 	const int val_type = USB_MIXER_U8;
1665 	const unsigned int control = 5;
1666 	const unsigned int cmask = 0;
1667 
1668 	return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1669 				       name, NULL);
1670 }
1671 
snd_c400_create_effect_vol_ctls(struct usb_mixer_interface * mixer)1672 static int snd_c400_create_effect_vol_ctls(struct usb_mixer_interface *mixer)
1673 {
1674 	char name[64];
1675 	unsigned int cmask;
1676 	int chan, err;
1677 	int num_outs = 0;
1678 	int num_ins = 0;
1679 
1680 	const unsigned int id = 0x42;
1681 	const int val_type = USB_MIXER_S16;
1682 	const int control = 1;
1683 
1684 	switch (mixer->chip->usb_id) {
1685 	case USB_ID(0x0763, 0x2030):
1686 		num_outs = 6;
1687 		num_ins = 4;
1688 		break;
1689 	case USB_ID(0x0763, 0x2031):
1690 		num_outs = 8;
1691 		num_ins = 6;
1692 		break;
1693 	}
1694 
1695 	for (chan = 0; chan < num_outs + num_ins; chan++) {
1696 		if (chan < num_outs) {
1697 			snprintf(name, sizeof(name),
1698 				 "Effect Send DOut%d",
1699 				 chan + 1);
1700 		} else {
1701 			snprintf(name, sizeof(name),
1702 				 "Effect Send AIn%d",
1703 				 chan - num_outs + 1);
1704 		}
1705 
1706 		cmask = (chan == 0) ? 0 : BIT(chan - 1);
1707 		err = snd_create_std_mono_ctl(mixer, id, control,
1708 					      cmask, val_type, name,
1709 					      &snd_usb_mixer_vol_tlv);
1710 		if (err < 0)
1711 			return err;
1712 	}
1713 
1714 	return 0;
1715 }
1716 
snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface * mixer)1717 static int snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface *mixer)
1718 {
1719 	char name[64];
1720 	unsigned int cmask;
1721 	int chan, err;
1722 	int num_outs = 0;
1723 	int offset = 0;
1724 
1725 	const unsigned int id = 0x40;
1726 	const int val_type = USB_MIXER_S16;
1727 	const int control = 1;
1728 
1729 	switch (mixer->chip->usb_id) {
1730 	case USB_ID(0x0763, 0x2030):
1731 		num_outs = 6;
1732 		offset = 0x3c;
1733 		/* { 0x3c, 0x43, 0x3e, 0x45, 0x40, 0x47 } */
1734 		break;
1735 	case USB_ID(0x0763, 0x2031):
1736 		num_outs = 8;
1737 		offset = 0x70;
1738 		/* { 0x70, 0x79, 0x72, 0x7b, 0x74, 0x7d, 0x76, 0x7f } */
1739 		break;
1740 	}
1741 
1742 	for (chan = 0; chan < num_outs; chan++) {
1743 		snprintf(name, sizeof(name),
1744 			 "Effect Return %d",
1745 			 chan + 1);
1746 
1747 		cmask = (chan == 0) ? 0 :
1748 			BIT(chan + (chan % 2) * num_outs - 1);
1749 		err = snd_create_std_mono_ctl_offset(mixer, id, control,
1750 						     cmask, val_type, offset, name,
1751 						     &snd_usb_mixer_vol_tlv);
1752 		if (err < 0)
1753 			return err;
1754 	}
1755 
1756 	return 0;
1757 }
1758 
snd_c400_create_mixer(struct usb_mixer_interface * mixer)1759 static int snd_c400_create_mixer(struct usb_mixer_interface *mixer)
1760 {
1761 	int err;
1762 
1763 	err = snd_c400_create_vol_ctls(mixer);
1764 	if (err < 0)
1765 		return err;
1766 
1767 	err = snd_c400_create_effect_vol_ctls(mixer);
1768 	if (err < 0)
1769 		return err;
1770 
1771 	err = snd_c400_create_effect_ret_vol_ctls(mixer);
1772 	if (err < 0)
1773 		return err;
1774 
1775 	err = snd_ftu_create_effect_switch(mixer, 2, 0x43);
1776 	if (err < 0)
1777 		return err;
1778 
1779 	err = snd_c400_create_effect_volume_ctl(mixer);
1780 	if (err < 0)
1781 		return err;
1782 
1783 	err = snd_c400_create_effect_duration_ctl(mixer);
1784 	if (err < 0)
1785 		return err;
1786 
1787 	err = snd_c400_create_effect_feedback_ctl(mixer);
1788 	if (err < 0)
1789 		return err;
1790 
1791 	return 0;
1792 }
1793 
1794 /*
1795  * The mixer units for Ebox-44 are corrupt, and even where they
1796  * are valid they presents mono controls as L and R channels of
1797  * stereo. So we provide a good mixer here.
1798  */
1799 static const struct std_mono_table ebox44_table[] = {
1800 	{
1801 		.unitid = 4,
1802 		.control = 1,
1803 		.cmask = 0x0,
1804 		.val_type = USB_MIXER_INV_BOOLEAN,
1805 		.name = "Headphone Playback Switch"
1806 	},
1807 	{
1808 		.unitid = 4,
1809 		.control = 2,
1810 		.cmask = 0x1,
1811 		.val_type = USB_MIXER_S16,
1812 		.name = "Headphone A Mix Playback Volume"
1813 	},
1814 	{
1815 		.unitid = 4,
1816 		.control = 2,
1817 		.cmask = 0x2,
1818 		.val_type = USB_MIXER_S16,
1819 		.name = "Headphone B Mix Playback Volume"
1820 	},
1821 
1822 	{
1823 		.unitid = 7,
1824 		.control = 1,
1825 		.cmask = 0x0,
1826 		.val_type = USB_MIXER_INV_BOOLEAN,
1827 		.name = "Output Playback Switch"
1828 	},
1829 	{
1830 		.unitid = 7,
1831 		.control = 2,
1832 		.cmask = 0x1,
1833 		.val_type = USB_MIXER_S16,
1834 		.name = "Output A Playback Volume"
1835 	},
1836 	{
1837 		.unitid = 7,
1838 		.control = 2,
1839 		.cmask = 0x2,
1840 		.val_type = USB_MIXER_S16,
1841 		.name = "Output B Playback Volume"
1842 	},
1843 
1844 	{
1845 		.unitid = 10,
1846 		.control = 1,
1847 		.cmask = 0x0,
1848 		.val_type = USB_MIXER_INV_BOOLEAN,
1849 		.name = "Input Capture Switch"
1850 	},
1851 	{
1852 		.unitid = 10,
1853 		.control = 2,
1854 		.cmask = 0x1,
1855 		.val_type = USB_MIXER_S16,
1856 		.name = "Input A Capture Volume"
1857 	},
1858 	{
1859 		.unitid = 10,
1860 		.control = 2,
1861 		.cmask = 0x2,
1862 		.val_type = USB_MIXER_S16,
1863 		.name = "Input B Capture Volume"
1864 	},
1865 
1866 	{}
1867 };
1868 
1869 /* Audio Advantage Micro II findings:
1870  *
1871  * Mapping spdif AES bits to vendor register.bit:
1872  * AES0: [0 0 0 0 2.3 2.2 2.1 2.0] - default 0x00
1873  * AES1: [3.3 3.2.3.1.3.0 2.7 2.6 2.5 2.4] - default: 0x01
1874  * AES2: [0 0 0 0 0 0 0 0]
1875  * AES3: [0 0 0 0 0 0 x 0] - 'x' bit is set basing on standard usb request
1876  *                           (UAC_EP_CS_ATTR_SAMPLE_RATE) for Audio Devices
1877  *
1878  * power on values:
1879  * r2: 0x10
1880  * r3: 0x20 (b7 is zeroed just before playback (except IEC61937) and set
1881  *           just after it to 0xa0, presumably it disables/mutes some analog
1882  *           parts when there is no audio.)
1883  * r9: 0x28
1884  *
1885  * Optical transmitter on/off:
1886  * vendor register.bit: 9.1
1887  * 0 - on (0x28 register value)
1888  * 1 - off (0x2a register value)
1889  *
1890  */
snd_microii_spdif_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1891 static int snd_microii_spdif_info(struct snd_kcontrol *kcontrol,
1892 				  struct snd_ctl_elem_info *uinfo)
1893 {
1894 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1895 	uinfo->count = 1;
1896 	return 0;
1897 }
1898 
snd_microii_spdif_default_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1899 static int snd_microii_spdif_default_get(struct snd_kcontrol *kcontrol,
1900 					 struct snd_ctl_elem_value *ucontrol)
1901 {
1902 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1903 	struct snd_usb_audio *chip = list->mixer->chip;
1904 	int err;
1905 	struct usb_interface *iface;
1906 	struct usb_host_interface *alts;
1907 	unsigned int ep;
1908 	unsigned char data[3];
1909 	int rate;
1910 
1911 	err = snd_usb_lock_shutdown(chip);
1912 	if (err < 0)
1913 		return err;
1914 
1915 	ucontrol->value.iec958.status[0] = kcontrol->private_value & 0xff;
1916 	ucontrol->value.iec958.status[1] = (kcontrol->private_value >> 8) & 0xff;
1917 	ucontrol->value.iec958.status[2] = 0x00;
1918 
1919 	/* use known values for that card: interface#1 altsetting#1 */
1920 	iface = usb_ifnum_to_if(chip->dev, 1);
1921 	if (!iface || iface->num_altsetting < 2) {
1922 		err = -EINVAL;
1923 		goto end;
1924 	}
1925 	alts = &iface->altsetting[1];
1926 	if (get_iface_desc(alts)->bNumEndpoints < 1) {
1927 		err = -EINVAL;
1928 		goto end;
1929 	}
1930 	ep = get_endpoint(alts, 0)->bEndpointAddress;
1931 
1932 	err = snd_usb_ctl_msg(chip->dev,
1933 			      usb_rcvctrlpipe(chip->dev, 0),
1934 			      UAC_GET_CUR,
1935 			      USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
1936 			      UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
1937 			      ep,
1938 			      data,
1939 			      sizeof(data));
1940 	if (err < 0)
1941 		goto end;
1942 
1943 	rate = data[0] | (data[1] << 8) | (data[2] << 16);
1944 	ucontrol->value.iec958.status[3] = (rate == 48000) ?
1945 			IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100;
1946 
1947 	err = 0;
1948  end:
1949 	snd_usb_unlock_shutdown(chip);
1950 	return err;
1951 }
1952 
snd_microii_spdif_default_update(struct usb_mixer_elem_list * list)1953 static int snd_microii_spdif_default_update(struct usb_mixer_elem_list *list)
1954 {
1955 	struct snd_usb_audio *chip = list->mixer->chip;
1956 	unsigned int pval = list->kctl->private_value;
1957 	u8 reg;
1958 	int err;
1959 
1960 	err = snd_usb_lock_shutdown(chip);
1961 	if (err < 0)
1962 		return err;
1963 
1964 	reg = ((pval >> 4) & 0xf0) | (pval & 0x0f);
1965 	err = snd_usb_ctl_msg(chip->dev,
1966 			      usb_sndctrlpipe(chip->dev, 0),
1967 			      UAC_SET_CUR,
1968 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1969 			      reg,
1970 			      2,
1971 			      NULL,
1972 			      0);
1973 	if (err < 0)
1974 		goto end;
1975 
1976 	reg = (pval & IEC958_AES0_NONAUDIO) ? 0xa0 : 0x20;
1977 	reg |= (pval >> 12) & 0x0f;
1978 	err = snd_usb_ctl_msg(chip->dev,
1979 			      usb_sndctrlpipe(chip->dev, 0),
1980 			      UAC_SET_CUR,
1981 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1982 			      reg,
1983 			      3,
1984 			      NULL,
1985 			      0);
1986 	if (err < 0)
1987 		goto end;
1988 
1989  end:
1990 	snd_usb_unlock_shutdown(chip);
1991 	return err;
1992 }
1993 
snd_microii_spdif_default_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1994 static int snd_microii_spdif_default_put(struct snd_kcontrol *kcontrol,
1995 					 struct snd_ctl_elem_value *ucontrol)
1996 {
1997 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1998 	unsigned int pval, pval_old;
1999 	int err;
2000 
2001 	pval = kcontrol->private_value;
2002 	pval_old = pval;
2003 	pval &= 0xfffff0f0;
2004 	pval |= (ucontrol->value.iec958.status[1] & 0x0f) << 8;
2005 	pval |= (ucontrol->value.iec958.status[0] & 0x0f);
2006 
2007 	pval &= 0xffff0fff;
2008 	pval |= (ucontrol->value.iec958.status[1] & 0xf0) << 8;
2009 
2010 	/* The frequency bits in AES3 cannot be set via register access. */
2011 
2012 	/* Silently ignore any bits from the request that cannot be set. */
2013 
2014 	if (pval == pval_old)
2015 		return 0;
2016 
2017 	kcontrol->private_value = pval;
2018 	err = snd_microii_spdif_default_update(list);
2019 	return err < 0 ? err : 1;
2020 }
2021 
snd_microii_spdif_mask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2022 static int snd_microii_spdif_mask_get(struct snd_kcontrol *kcontrol,
2023 				      struct snd_ctl_elem_value *ucontrol)
2024 {
2025 	ucontrol->value.iec958.status[0] = 0x0f;
2026 	ucontrol->value.iec958.status[1] = 0xff;
2027 	ucontrol->value.iec958.status[2] = 0x00;
2028 	ucontrol->value.iec958.status[3] = 0x00;
2029 
2030 	return 0;
2031 }
2032 
snd_microii_spdif_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2033 static int snd_microii_spdif_switch_get(struct snd_kcontrol *kcontrol,
2034 					struct snd_ctl_elem_value *ucontrol)
2035 {
2036 	ucontrol->value.integer.value[0] = !(kcontrol->private_value & 0x02);
2037 
2038 	return 0;
2039 }
2040 
snd_microii_spdif_switch_update(struct usb_mixer_elem_list * list)2041 static int snd_microii_spdif_switch_update(struct usb_mixer_elem_list *list)
2042 {
2043 	struct snd_usb_audio *chip = list->mixer->chip;
2044 	u8 reg = list->kctl->private_value;
2045 	int err;
2046 
2047 	err = snd_usb_lock_shutdown(chip);
2048 	if (err < 0)
2049 		return err;
2050 
2051 	err = snd_usb_ctl_msg(chip->dev,
2052 			      usb_sndctrlpipe(chip->dev, 0),
2053 			      UAC_SET_CUR,
2054 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
2055 			      reg,
2056 			      9,
2057 			      NULL,
2058 			      0);
2059 
2060 	snd_usb_unlock_shutdown(chip);
2061 	return err;
2062 }
2063 
snd_microii_spdif_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2064 static int snd_microii_spdif_switch_put(struct snd_kcontrol *kcontrol,
2065 					struct snd_ctl_elem_value *ucontrol)
2066 {
2067 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2068 	u8 reg;
2069 	int err;
2070 
2071 	reg = ucontrol->value.integer.value[0] ? 0x28 : 0x2a;
2072 	if (reg != list->kctl->private_value)
2073 		return 0;
2074 
2075 	kcontrol->private_value = reg;
2076 	err = snd_microii_spdif_switch_update(list);
2077 	return err < 0 ? err : 1;
2078 }
2079 
2080 static const struct snd_kcontrol_new snd_microii_mixer_spdif[] = {
2081 	{
2082 		.iface =    SNDRV_CTL_ELEM_IFACE_PCM,
2083 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2084 		.info =     snd_microii_spdif_info,
2085 		.get =      snd_microii_spdif_default_get,
2086 		.put =      snd_microii_spdif_default_put,
2087 		.private_value = 0x00000100UL,/* reset value */
2088 	},
2089 	{
2090 		.access =   SNDRV_CTL_ELEM_ACCESS_READ,
2091 		.iface =    SNDRV_CTL_ELEM_IFACE_PCM,
2092 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
2093 		.info =     snd_microii_spdif_info,
2094 		.get =      snd_microii_spdif_mask_get,
2095 	},
2096 	{
2097 		.iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
2098 		.name =     SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2099 		.info =     snd_ctl_boolean_mono_info,
2100 		.get =      snd_microii_spdif_switch_get,
2101 		.put =      snd_microii_spdif_switch_put,
2102 		.private_value = 0x00000028UL,/* reset value */
2103 	}
2104 };
2105 
snd_microii_controls_create(struct usb_mixer_interface * mixer)2106 static int snd_microii_controls_create(struct usb_mixer_interface *mixer)
2107 {
2108 	int err, i;
2109 	static const usb_mixer_elem_resume_func_t resume_funcs[] = {
2110 		snd_microii_spdif_default_update,
2111 		NULL,
2112 		snd_microii_spdif_switch_update
2113 	};
2114 
2115 	for (i = 0; i < ARRAY_SIZE(snd_microii_mixer_spdif); ++i) {
2116 		err = add_single_ctl_with_resume(mixer, 0,
2117 						 resume_funcs[i],
2118 						 &snd_microii_mixer_spdif[i],
2119 						 NULL);
2120 		if (err < 0)
2121 			return err;
2122 	}
2123 
2124 	return 0;
2125 }
2126 
2127 /* Creative Sound Blaster E1 */
2128 
snd_soundblaster_e1_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2129 static int snd_soundblaster_e1_switch_get(struct snd_kcontrol *kcontrol,
2130 					  struct snd_ctl_elem_value *ucontrol)
2131 {
2132 	ucontrol->value.integer.value[0] = kcontrol->private_value;
2133 	return 0;
2134 }
2135 
snd_soundblaster_e1_switch_update(struct usb_mixer_interface * mixer,unsigned char state)2136 static int snd_soundblaster_e1_switch_update(struct usb_mixer_interface *mixer,
2137 					     unsigned char state)
2138 {
2139 	struct snd_usb_audio *chip = mixer->chip;
2140 	int err;
2141 	unsigned char buff[2];
2142 
2143 	buff[0] = 0x02;
2144 	buff[1] = state ? 0x02 : 0x00;
2145 
2146 	err = snd_usb_lock_shutdown(chip);
2147 	if (err < 0)
2148 		return err;
2149 	err = snd_usb_ctl_msg(chip->dev,
2150 			      usb_sndctrlpipe(chip->dev, 0), HID_REQ_SET_REPORT,
2151 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
2152 			      0x0202, 3, buff, 2);
2153 	snd_usb_unlock_shutdown(chip);
2154 	return err;
2155 }
2156 
snd_soundblaster_e1_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2157 static int snd_soundblaster_e1_switch_put(struct snd_kcontrol *kcontrol,
2158 					  struct snd_ctl_elem_value *ucontrol)
2159 {
2160 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2161 	unsigned char value = !!ucontrol->value.integer.value[0];
2162 	int err;
2163 
2164 	if (kcontrol->private_value == value)
2165 		return 0;
2166 	kcontrol->private_value = value;
2167 	err = snd_soundblaster_e1_switch_update(list->mixer, value);
2168 	return err < 0 ? err : 1;
2169 }
2170 
snd_soundblaster_e1_switch_resume(struct usb_mixer_elem_list * list)2171 static int snd_soundblaster_e1_switch_resume(struct usb_mixer_elem_list *list)
2172 {
2173 	return snd_soundblaster_e1_switch_update(list->mixer,
2174 						 list->kctl->private_value);
2175 }
2176 
snd_soundblaster_e1_switch_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2177 static int snd_soundblaster_e1_switch_info(struct snd_kcontrol *kcontrol,
2178 					   struct snd_ctl_elem_info *uinfo)
2179 {
2180 	static const char *const texts[2] = {
2181 		"Mic", "Aux"
2182 	};
2183 
2184 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
2185 }
2186 
2187 static const struct snd_kcontrol_new snd_soundblaster_e1_input_switch = {
2188 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2189 	.name = "Input Source",
2190 	.info = snd_soundblaster_e1_switch_info,
2191 	.get = snd_soundblaster_e1_switch_get,
2192 	.put = snd_soundblaster_e1_switch_put,
2193 	.private_value = 0,
2194 };
2195 
snd_soundblaster_e1_switch_create(struct usb_mixer_interface * mixer)2196 static int snd_soundblaster_e1_switch_create(struct usb_mixer_interface *mixer)
2197 {
2198 	return add_single_ctl_with_resume(mixer, 0,
2199 					  snd_soundblaster_e1_switch_resume,
2200 					  &snd_soundblaster_e1_input_switch,
2201 					  NULL);
2202 }
2203 
2204 /*
2205  * Dell WD15 dock jack detection
2206  *
2207  * The WD15 contains an ALC4020 USB audio controller and ALC3263 audio codec
2208  * from Realtek. It is a UAC 1 device, and UAC 1 does not support jack
2209  * detection. Instead, jack detection works by sending HD Audio commands over
2210  * vendor-type USB messages.
2211  */
2212 
2213 #define HDA_VERB_CMD(V, N, D) (((N) << 20) | ((V) << 8) | (D))
2214 
2215 #define REALTEK_HDA_VALUE 0x0038
2216 
2217 #define REALTEK_HDA_SET		62
2218 #define REALTEK_MANUAL_MODE	72
2219 #define REALTEK_HDA_GET_OUT	88
2220 #define REALTEK_HDA_GET_IN	89
2221 
2222 #define REALTEK_AUDIO_FUNCTION_GROUP	0x01
2223 #define REALTEK_LINE1			0x1a
2224 #define REALTEK_VENDOR_REGISTERS	0x20
2225 #define REALTEK_HP_OUT			0x21
2226 
2227 #define REALTEK_CBJ_CTRL2 0x50
2228 
2229 #define REALTEK_JACK_INTERRUPT_NODE 5
2230 
2231 #define REALTEK_MIC_FLAG 0x100
2232 
realtek_hda_set(struct snd_usb_audio * chip,u32 cmd)2233 static int realtek_hda_set(struct snd_usb_audio *chip, u32 cmd)
2234 {
2235 	struct usb_device *dev = chip->dev;
2236 	__be32 buf = cpu_to_be32(cmd);
2237 
2238 	return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), REALTEK_HDA_SET,
2239 			       USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_OUT,
2240 			       REALTEK_HDA_VALUE, 0, &buf, sizeof(buf));
2241 }
2242 
realtek_hda_get(struct snd_usb_audio * chip,u32 cmd,u32 * value)2243 static int realtek_hda_get(struct snd_usb_audio *chip, u32 cmd, u32 *value)
2244 {
2245 	struct usb_device *dev = chip->dev;
2246 	int err;
2247 	__be32 buf = cpu_to_be32(cmd);
2248 
2249 	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), REALTEK_HDA_GET_OUT,
2250 			      USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_OUT,
2251 			      REALTEK_HDA_VALUE, 0, &buf, sizeof(buf));
2252 	if (err < 0)
2253 		return err;
2254 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), REALTEK_HDA_GET_IN,
2255 			      USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_IN,
2256 			      REALTEK_HDA_VALUE, 0, &buf, sizeof(buf));
2257 	if (err < 0)
2258 		return err;
2259 
2260 	*value = be32_to_cpu(buf);
2261 	return 0;
2262 }
2263 
realtek_ctl_connector_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2264 static int realtek_ctl_connector_get(struct snd_kcontrol *kcontrol,
2265 				     struct snd_ctl_elem_value *ucontrol)
2266 {
2267 	struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol);
2268 	struct snd_usb_audio *chip = cval->head.mixer->chip;
2269 	u32 pv = kcontrol->private_value;
2270 	u32 node_id = pv & 0xff;
2271 	u32 sense;
2272 	u32 cbj_ctrl2;
2273 	bool presence;
2274 	int err;
2275 
2276 	err = snd_usb_lock_shutdown(chip);
2277 	if (err < 0)
2278 		return err;
2279 	err = realtek_hda_get(chip,
2280 			      HDA_VERB_CMD(AC_VERB_GET_PIN_SENSE, node_id, 0),
2281 			      &sense);
2282 	if (err < 0)
2283 		goto err;
2284 	if (pv & REALTEK_MIC_FLAG) {
2285 		err = realtek_hda_set(chip,
2286 				      HDA_VERB_CMD(AC_VERB_SET_COEF_INDEX,
2287 						   REALTEK_VENDOR_REGISTERS,
2288 						   REALTEK_CBJ_CTRL2));
2289 		if (err < 0)
2290 			goto err;
2291 		err = realtek_hda_get(chip,
2292 				      HDA_VERB_CMD(AC_VERB_GET_PROC_COEF,
2293 						   REALTEK_VENDOR_REGISTERS, 0),
2294 				      &cbj_ctrl2);
2295 		if (err < 0)
2296 			goto err;
2297 	}
2298 err:
2299 	snd_usb_unlock_shutdown(chip);
2300 	if (err < 0)
2301 		return err;
2302 
2303 	presence = sense & AC_PINSENSE_PRESENCE;
2304 	if (pv & REALTEK_MIC_FLAG)
2305 		presence = presence && (cbj_ctrl2 & 0x0070) == 0x0070;
2306 	ucontrol->value.integer.value[0] = presence;
2307 	return 0;
2308 }
2309 
2310 static const struct snd_kcontrol_new realtek_connector_ctl_ro = {
2311 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
2312 	.name = "", /* will be filled later manually */
2313 	.access = SNDRV_CTL_ELEM_ACCESS_READ,
2314 	.info = snd_ctl_boolean_mono_info,
2315 	.get = realtek_ctl_connector_get,
2316 };
2317 
realtek_resume_jack(struct usb_mixer_elem_list * list)2318 static int realtek_resume_jack(struct usb_mixer_elem_list *list)
2319 {
2320 	snd_ctl_notify(list->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2321 		       &list->kctl->id);
2322 	return 0;
2323 }
2324 
realtek_add_jack(struct usb_mixer_interface * mixer,char * name,u32 val)2325 static int realtek_add_jack(struct usb_mixer_interface *mixer,
2326 			    char *name, u32 val)
2327 {
2328 	struct usb_mixer_elem_info *cval;
2329 	struct snd_kcontrol *kctl;
2330 
2331 	cval = kzalloc(sizeof(*cval), GFP_KERNEL);
2332 	if (!cval)
2333 		return -ENOMEM;
2334 	snd_usb_mixer_elem_init_std(&cval->head, mixer,
2335 				    REALTEK_JACK_INTERRUPT_NODE);
2336 	cval->head.resume = realtek_resume_jack;
2337 	cval->val_type = USB_MIXER_BOOLEAN;
2338 	cval->channels = 1;
2339 	cval->min = 0;
2340 	cval->max = 1;
2341 	kctl = snd_ctl_new1(&realtek_connector_ctl_ro, cval);
2342 	if (!kctl) {
2343 		kfree(cval);
2344 		return -ENOMEM;
2345 	}
2346 	kctl->private_value = val;
2347 	strscpy(kctl->id.name, name, sizeof(kctl->id.name));
2348 	kctl->private_free = snd_usb_mixer_elem_free;
2349 	return snd_usb_mixer_add_control(&cval->head, kctl);
2350 }
2351 
dell_dock_mixer_create(struct usb_mixer_interface * mixer)2352 static int dell_dock_mixer_create(struct usb_mixer_interface *mixer)
2353 {
2354 	int err;
2355 	struct usb_device *dev = mixer->chip->dev;
2356 
2357 	/* Power down the audio codec to avoid loud pops in the next step. */
2358 	realtek_hda_set(mixer->chip,
2359 			HDA_VERB_CMD(AC_VERB_SET_POWER_STATE,
2360 				     REALTEK_AUDIO_FUNCTION_GROUP,
2361 				     AC_PWRST_D3));
2362 
2363 	/*
2364 	 * Turn off 'manual mode' in case it was enabled. This removes the need
2365 	 * to power cycle the dock after it was attached to a Windows machine.
2366 	 */
2367 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), REALTEK_MANUAL_MODE,
2368 			USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_DIR_OUT,
2369 			0, 0, NULL, 0);
2370 
2371 	err = realtek_add_jack(mixer, "Line Out Jack", REALTEK_LINE1);
2372 	if (err < 0)
2373 		return err;
2374 	err = realtek_add_jack(mixer, "Headphone Jack", REALTEK_HP_OUT);
2375 	if (err < 0)
2376 		return err;
2377 	err = realtek_add_jack(mixer, "Headset Mic Jack",
2378 			       REALTEK_HP_OUT | REALTEK_MIC_FLAG);
2379 	if (err < 0)
2380 		return err;
2381 	return 0;
2382 }
2383 
dell_dock_init_vol(struct usb_mixer_interface * mixer,int ch,int id)2384 static void dell_dock_init_vol(struct usb_mixer_interface *mixer, int ch, int id)
2385 {
2386 	struct snd_usb_audio *chip = mixer->chip;
2387 	u16 buf = 0;
2388 
2389 	snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
2390 			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
2391 			(UAC_FU_VOLUME << 8) | ch,
2392 			snd_usb_ctrl_intf(mixer->hostif) | (id << 8),
2393 			&buf, 2);
2394 }
2395 
dell_dock_mixer_init(struct usb_mixer_interface * mixer)2396 static int dell_dock_mixer_init(struct usb_mixer_interface *mixer)
2397 {
2398 	/* fix to 0dB playback volumes */
2399 	dell_dock_init_vol(mixer, 1, 16);
2400 	dell_dock_init_vol(mixer, 2, 16);
2401 	dell_dock_init_vol(mixer, 1, 19);
2402 	dell_dock_init_vol(mixer, 2, 19);
2403 	return 0;
2404 }
2405 
2406 /* RME Class Compliant device quirks */
2407 
2408 #define SND_RME_GET_STATUS1			23
2409 #define SND_RME_GET_CURRENT_FREQ		17
2410 #define SND_RME_CLK_SYSTEM_SHIFT		16
2411 #define SND_RME_CLK_SYSTEM_MASK			0x1f
2412 #define SND_RME_CLK_AES_SHIFT			8
2413 #define SND_RME_CLK_SPDIF_SHIFT			12
2414 #define SND_RME_CLK_AES_SPDIF_MASK		0xf
2415 #define SND_RME_CLK_SYNC_SHIFT			6
2416 #define SND_RME_CLK_SYNC_MASK			0x3
2417 #define SND_RME_CLK_FREQMUL_SHIFT		18
2418 #define SND_RME_CLK_FREQMUL_MASK		0x7
2419 #define SND_RME_CLK_SYSTEM(x) \
2420 	(((x) >> SND_RME_CLK_SYSTEM_SHIFT) & SND_RME_CLK_SYSTEM_MASK)
2421 #define SND_RME_CLK_AES(x) \
2422 	(((x) >> SND_RME_CLK_AES_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
2423 #define SND_RME_CLK_SPDIF(x) \
2424 	(((x) >> SND_RME_CLK_SPDIF_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
2425 #define SND_RME_CLK_SYNC(x) \
2426 	(((x) >> SND_RME_CLK_SYNC_SHIFT) & SND_RME_CLK_SYNC_MASK)
2427 #define SND_RME_CLK_FREQMUL(x) \
2428 	(((x) >> SND_RME_CLK_FREQMUL_SHIFT) & SND_RME_CLK_FREQMUL_MASK)
2429 #define SND_RME_CLK_AES_LOCK			0x1
2430 #define SND_RME_CLK_AES_SYNC			0x4
2431 #define SND_RME_CLK_SPDIF_LOCK			0x2
2432 #define SND_RME_CLK_SPDIF_SYNC			0x8
2433 #define SND_RME_SPDIF_IF_SHIFT			4
2434 #define SND_RME_SPDIF_FORMAT_SHIFT		5
2435 #define SND_RME_BINARY_MASK			0x1
2436 #define SND_RME_SPDIF_IF(x) \
2437 	(((x) >> SND_RME_SPDIF_IF_SHIFT) & SND_RME_BINARY_MASK)
2438 #define SND_RME_SPDIF_FORMAT(x) \
2439 	(((x) >> SND_RME_SPDIF_FORMAT_SHIFT) & SND_RME_BINARY_MASK)
2440 
2441 static const u32 snd_rme_rate_table[] = {
2442 	32000, 44100, 48000, 50000,
2443 	64000, 88200, 96000, 100000,
2444 	128000, 176400, 192000, 200000,
2445 	256000,	352800, 384000, 400000,
2446 	512000, 705600, 768000, 800000
2447 };
2448 
2449 /* maximum number of items for AES and S/PDIF rates for above table */
2450 #define SND_RME_RATE_IDX_AES_SPDIF_NUM		12
2451 
2452 enum snd_rme_domain {
2453 	SND_RME_DOMAIN_SYSTEM,
2454 	SND_RME_DOMAIN_AES,
2455 	SND_RME_DOMAIN_SPDIF
2456 };
2457 
2458 enum snd_rme_clock_status {
2459 	SND_RME_CLOCK_NOLOCK,
2460 	SND_RME_CLOCK_LOCK,
2461 	SND_RME_CLOCK_SYNC
2462 };
2463 
snd_rme_read_value(struct snd_usb_audio * chip,unsigned int item,u32 * value)2464 static int snd_rme_read_value(struct snd_usb_audio *chip,
2465 			      unsigned int item,
2466 			      u32 *value)
2467 {
2468 	struct usb_device *dev = chip->dev;
2469 	int err;
2470 
2471 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
2472 			      item,
2473 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2474 			      0, 0,
2475 			      value, sizeof(*value));
2476 	if (err < 0)
2477 		dev_err(&dev->dev,
2478 			"unable to issue vendor read request %d (ret = %d)",
2479 			item, err);
2480 	return err;
2481 }
2482 
snd_rme_get_status1(struct snd_kcontrol * kcontrol,u32 * status1)2483 static int snd_rme_get_status1(struct snd_kcontrol *kcontrol,
2484 			       u32 *status1)
2485 {
2486 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2487 	struct snd_usb_audio *chip = list->mixer->chip;
2488 	int err;
2489 
2490 	err = snd_usb_lock_shutdown(chip);
2491 	if (err < 0)
2492 		return err;
2493 	err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, status1);
2494 	snd_usb_unlock_shutdown(chip);
2495 	return err;
2496 }
2497 
snd_rme_rate_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2498 static int snd_rme_rate_get(struct snd_kcontrol *kcontrol,
2499 			    struct snd_ctl_elem_value *ucontrol)
2500 {
2501 	u32 status1;
2502 	u32 rate = 0;
2503 	int idx;
2504 	int err;
2505 
2506 	err = snd_rme_get_status1(kcontrol, &status1);
2507 	if (err < 0)
2508 		return err;
2509 	switch (kcontrol->private_value) {
2510 	case SND_RME_DOMAIN_SYSTEM:
2511 		idx = SND_RME_CLK_SYSTEM(status1);
2512 		if (idx < ARRAY_SIZE(snd_rme_rate_table))
2513 			rate = snd_rme_rate_table[idx];
2514 		break;
2515 	case SND_RME_DOMAIN_AES:
2516 		idx = SND_RME_CLK_AES(status1);
2517 		if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
2518 			rate = snd_rme_rate_table[idx];
2519 		break;
2520 	case SND_RME_DOMAIN_SPDIF:
2521 		idx = SND_RME_CLK_SPDIF(status1);
2522 		if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
2523 			rate = snd_rme_rate_table[idx];
2524 		break;
2525 	default:
2526 		return -EINVAL;
2527 	}
2528 	ucontrol->value.integer.value[0] = rate;
2529 	return 0;
2530 }
2531 
snd_rme_sync_state_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2532 static int snd_rme_sync_state_get(struct snd_kcontrol *kcontrol,
2533 				  struct snd_ctl_elem_value *ucontrol)
2534 {
2535 	u32 status1;
2536 	int idx = SND_RME_CLOCK_NOLOCK;
2537 	int err;
2538 
2539 	err = snd_rme_get_status1(kcontrol, &status1);
2540 	if (err < 0)
2541 		return err;
2542 	switch (kcontrol->private_value) {
2543 	case SND_RME_DOMAIN_AES:  /* AES */
2544 		if (status1 & SND_RME_CLK_AES_SYNC)
2545 			idx = SND_RME_CLOCK_SYNC;
2546 		else if (status1 & SND_RME_CLK_AES_LOCK)
2547 			idx = SND_RME_CLOCK_LOCK;
2548 		break;
2549 	case SND_RME_DOMAIN_SPDIF:  /* SPDIF */
2550 		if (status1 & SND_RME_CLK_SPDIF_SYNC)
2551 			idx = SND_RME_CLOCK_SYNC;
2552 		else if (status1 & SND_RME_CLK_SPDIF_LOCK)
2553 			idx = SND_RME_CLOCK_LOCK;
2554 		break;
2555 	default:
2556 		return -EINVAL;
2557 	}
2558 	ucontrol->value.enumerated.item[0] = idx;
2559 	return 0;
2560 }
2561 
snd_rme_spdif_if_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2562 static int snd_rme_spdif_if_get(struct snd_kcontrol *kcontrol,
2563 				struct snd_ctl_elem_value *ucontrol)
2564 {
2565 	u32 status1;
2566 	int err;
2567 
2568 	err = snd_rme_get_status1(kcontrol, &status1);
2569 	if (err < 0)
2570 		return err;
2571 	ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_IF(status1);
2572 	return 0;
2573 }
2574 
snd_rme_spdif_format_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2575 static int snd_rme_spdif_format_get(struct snd_kcontrol *kcontrol,
2576 				    struct snd_ctl_elem_value *ucontrol)
2577 {
2578 	u32 status1;
2579 	int err;
2580 
2581 	err = snd_rme_get_status1(kcontrol, &status1);
2582 	if (err < 0)
2583 		return err;
2584 	ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_FORMAT(status1);
2585 	return 0;
2586 }
2587 
snd_rme_sync_source_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2588 static int snd_rme_sync_source_get(struct snd_kcontrol *kcontrol,
2589 				   struct snd_ctl_elem_value *ucontrol)
2590 {
2591 	u32 status1;
2592 	int err;
2593 
2594 	err = snd_rme_get_status1(kcontrol, &status1);
2595 	if (err < 0)
2596 		return err;
2597 	ucontrol->value.enumerated.item[0] = SND_RME_CLK_SYNC(status1);
2598 	return 0;
2599 }
2600 
snd_rme_current_freq_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2601 static int snd_rme_current_freq_get(struct snd_kcontrol *kcontrol,
2602 				    struct snd_ctl_elem_value *ucontrol)
2603 {
2604 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2605 	struct snd_usb_audio *chip = list->mixer->chip;
2606 	u32 status1;
2607 	const u64 num = 104857600000000ULL;
2608 	u32 den;
2609 	unsigned int freq;
2610 	int err;
2611 
2612 	err = snd_usb_lock_shutdown(chip);
2613 	if (err < 0)
2614 		return err;
2615 	err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, &status1);
2616 	if (err < 0)
2617 		goto end;
2618 	err = snd_rme_read_value(chip, SND_RME_GET_CURRENT_FREQ, &den);
2619 	if (err < 0)
2620 		goto end;
2621 	freq = (den == 0) ? 0 : div64_u64(num, den);
2622 	freq <<= SND_RME_CLK_FREQMUL(status1);
2623 	ucontrol->value.integer.value[0] = freq;
2624 
2625 end:
2626 	snd_usb_unlock_shutdown(chip);
2627 	return err;
2628 }
2629 
snd_rme_rate_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2630 static int snd_rme_rate_info(struct snd_kcontrol *kcontrol,
2631 			     struct snd_ctl_elem_info *uinfo)
2632 {
2633 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2634 	uinfo->count = 1;
2635 	switch (kcontrol->private_value) {
2636 	case SND_RME_DOMAIN_SYSTEM:
2637 		uinfo->value.integer.min = 32000;
2638 		uinfo->value.integer.max = 800000;
2639 		break;
2640 	case SND_RME_DOMAIN_AES:
2641 	case SND_RME_DOMAIN_SPDIF:
2642 	default:
2643 		uinfo->value.integer.min = 0;
2644 		uinfo->value.integer.max = 200000;
2645 	}
2646 	uinfo->value.integer.step = 0;
2647 	return 0;
2648 }
2649 
snd_rme_sync_state_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2650 static int snd_rme_sync_state_info(struct snd_kcontrol *kcontrol,
2651 				   struct snd_ctl_elem_info *uinfo)
2652 {
2653 	static const char *const sync_states[] = {
2654 		"No Lock", "Lock", "Sync"
2655 	};
2656 
2657 	return snd_ctl_enum_info(uinfo, 1,
2658 				 ARRAY_SIZE(sync_states), sync_states);
2659 }
2660 
snd_rme_spdif_if_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2661 static int snd_rme_spdif_if_info(struct snd_kcontrol *kcontrol,
2662 				 struct snd_ctl_elem_info *uinfo)
2663 {
2664 	static const char *const spdif_if[] = {
2665 		"Coaxial", "Optical"
2666 	};
2667 
2668 	return snd_ctl_enum_info(uinfo, 1,
2669 				 ARRAY_SIZE(spdif_if), spdif_if);
2670 }
2671 
snd_rme_spdif_format_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2672 static int snd_rme_spdif_format_info(struct snd_kcontrol *kcontrol,
2673 				     struct snd_ctl_elem_info *uinfo)
2674 {
2675 	static const char *const optical_type[] = {
2676 		"Consumer", "Professional"
2677 	};
2678 
2679 	return snd_ctl_enum_info(uinfo, 1,
2680 				 ARRAY_SIZE(optical_type), optical_type);
2681 }
2682 
snd_rme_sync_source_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2683 static int snd_rme_sync_source_info(struct snd_kcontrol *kcontrol,
2684 				    struct snd_ctl_elem_info *uinfo)
2685 {
2686 	static const char *const sync_sources[] = {
2687 		"Internal", "AES", "SPDIF", "Internal"
2688 	};
2689 
2690 	return snd_ctl_enum_info(uinfo, 1,
2691 				 ARRAY_SIZE(sync_sources), sync_sources);
2692 }
2693 
2694 static const struct snd_kcontrol_new snd_rme_controls[] = {
2695 	{
2696 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2697 		.name = "AES Rate",
2698 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2699 		.info = snd_rme_rate_info,
2700 		.get = snd_rme_rate_get,
2701 		.private_value = SND_RME_DOMAIN_AES
2702 	},
2703 	{
2704 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2705 		.name = "AES Sync",
2706 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2707 		.info = snd_rme_sync_state_info,
2708 		.get = snd_rme_sync_state_get,
2709 		.private_value = SND_RME_DOMAIN_AES
2710 	},
2711 	{
2712 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2713 		.name = "SPDIF Rate",
2714 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2715 		.info = snd_rme_rate_info,
2716 		.get = snd_rme_rate_get,
2717 		.private_value = SND_RME_DOMAIN_SPDIF
2718 	},
2719 	{
2720 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2721 		.name = "SPDIF Sync",
2722 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2723 		.info = snd_rme_sync_state_info,
2724 		.get = snd_rme_sync_state_get,
2725 		.private_value = SND_RME_DOMAIN_SPDIF
2726 	},
2727 	{
2728 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2729 		.name = "SPDIF Interface",
2730 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2731 		.info = snd_rme_spdif_if_info,
2732 		.get = snd_rme_spdif_if_get,
2733 	},
2734 	{
2735 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2736 		.name = "SPDIF Format",
2737 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2738 		.info = snd_rme_spdif_format_info,
2739 		.get = snd_rme_spdif_format_get,
2740 	},
2741 	{
2742 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2743 		.name = "Sync Source",
2744 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2745 		.info = snd_rme_sync_source_info,
2746 		.get = snd_rme_sync_source_get
2747 	},
2748 	{
2749 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2750 		.name = "System Rate",
2751 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2752 		.info = snd_rme_rate_info,
2753 		.get = snd_rme_rate_get,
2754 		.private_value = SND_RME_DOMAIN_SYSTEM
2755 	},
2756 	{
2757 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2758 		.name = "Current Frequency",
2759 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2760 		.info = snd_rme_rate_info,
2761 		.get = snd_rme_current_freq_get
2762 	}
2763 };
2764 
snd_rme_controls_create(struct usb_mixer_interface * mixer)2765 static int snd_rme_controls_create(struct usb_mixer_interface *mixer)
2766 {
2767 	int err, i;
2768 
2769 	for (i = 0; i < ARRAY_SIZE(snd_rme_controls); ++i) {
2770 		err = add_single_ctl_with_resume(mixer, 0,
2771 						 NULL,
2772 						 &snd_rme_controls[i],
2773 						 NULL);
2774 		if (err < 0)
2775 			return err;
2776 	}
2777 
2778 	return 0;
2779 }
2780 
2781 /*
2782  * RME Babyface Pro (FS)
2783  *
2784  * These devices exposes a couple of DSP functions via request to EP0.
2785  * Switches are available via control registers, while routing is controlled
2786  * by controlling the volume on each possible crossing point.
2787  * Volume control is linear, from -inf (dec. 0) to +6dB (dec. 65536) with
2788  * 0dB being at dec. 32768.
2789  */
2790 enum {
2791 	SND_BBFPRO_CTL_REG1 = 0,
2792 	SND_BBFPRO_CTL_REG2
2793 };
2794 
2795 #define SND_BBFPRO_CTL_REG_MASK 1
2796 #define SND_BBFPRO_CTL_IDX_MASK 0xff
2797 #define SND_BBFPRO_CTL_IDX_SHIFT 1
2798 #define SND_BBFPRO_CTL_VAL_MASK 1
2799 #define SND_BBFPRO_CTL_VAL_SHIFT 9
2800 #define SND_BBFPRO_CTL_REG1_CLK_MASTER 0
2801 #define SND_BBFPRO_CTL_REG1_CLK_OPTICAL 1
2802 #define SND_BBFPRO_CTL_REG1_SPDIF_PRO 7
2803 #define SND_BBFPRO_CTL_REG1_SPDIF_EMPH 8
2804 #define SND_BBFPRO_CTL_REG1_SPDIF_OPTICAL 10
2805 #define SND_BBFPRO_CTL_REG2_48V_AN1 0
2806 #define SND_BBFPRO_CTL_REG2_48V_AN2 1
2807 #define SND_BBFPRO_CTL_REG2_SENS_IN3 2
2808 #define SND_BBFPRO_CTL_REG2_SENS_IN4 3
2809 #define SND_BBFPRO_CTL_REG2_PAD_AN1 4
2810 #define SND_BBFPRO_CTL_REG2_PAD_AN2 5
2811 
2812 #define SND_BBFPRO_MIXER_MAIN_OUT_CH_OFFSET 992
2813 #define SND_BBFPRO_MIXER_IDX_MASK 0x3ff
2814 #define SND_BBFPRO_MIXER_VAL_MASK 0x3ffff
2815 #define SND_BBFPRO_MIXER_VAL_SHIFT 9
2816 #define SND_BBFPRO_MIXER_VAL_MIN 0 // -inf
2817 #define SND_BBFPRO_MIXER_VAL_MAX 65536 // +6dB
2818 
2819 #define SND_BBFPRO_GAIN_CHANNEL_MASK 0x03
2820 #define SND_BBFPRO_GAIN_CHANNEL_SHIFT 7
2821 #define SND_BBFPRO_GAIN_VAL_MASK 0x7f
2822 #define SND_BBFPRO_GAIN_VAL_MIN 0
2823 #define SND_BBFPRO_GAIN_VAL_MIC_MAX 65
2824 #define SND_BBFPRO_GAIN_VAL_LINE_MAX 18 // 9db in 0.5db incraments
2825 
2826 #define SND_BBFPRO_USBREQ_CTL_REG1 0x10
2827 #define SND_BBFPRO_USBREQ_CTL_REG2 0x17
2828 #define SND_BBFPRO_USBREQ_GAIN 0x1a
2829 #define SND_BBFPRO_USBREQ_MIXER 0x12
2830 
snd_bbfpro_ctl_update(struct usb_mixer_interface * mixer,u8 reg,u8 index,u8 value)2831 static int snd_bbfpro_ctl_update(struct usb_mixer_interface *mixer, u8 reg,
2832 				 u8 index, u8 value)
2833 {
2834 	int err;
2835 	u16 usb_req, usb_idx, usb_val;
2836 	struct snd_usb_audio *chip = mixer->chip;
2837 
2838 	err = snd_usb_lock_shutdown(chip);
2839 	if (err < 0)
2840 		return err;
2841 
2842 	if (reg == SND_BBFPRO_CTL_REG1) {
2843 		usb_req = SND_BBFPRO_USBREQ_CTL_REG1;
2844 		if (index == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) {
2845 			usb_idx = 3;
2846 			usb_val = value ? 3 : 0;
2847 		} else {
2848 			usb_idx = BIT(index);
2849 			usb_val = value ? usb_idx : 0;
2850 		}
2851 	} else {
2852 		usb_req = SND_BBFPRO_USBREQ_CTL_REG2;
2853 		usb_idx = BIT(index);
2854 		usb_val = value ? usb_idx : 0;
2855 	}
2856 
2857 	err = snd_usb_ctl_msg(chip->dev,
2858 			      usb_sndctrlpipe(chip->dev, 0), usb_req,
2859 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2860 			      usb_val, usb_idx, NULL, 0);
2861 
2862 	snd_usb_unlock_shutdown(chip);
2863 	return err;
2864 }
2865 
snd_bbfpro_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2866 static int snd_bbfpro_ctl_get(struct snd_kcontrol *kcontrol,
2867 			      struct snd_ctl_elem_value *ucontrol)
2868 {
2869 	u8 reg, idx, val;
2870 	int pv;
2871 
2872 	pv = kcontrol->private_value;
2873 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2874 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2875 	val = kcontrol->private_value >> SND_BBFPRO_CTL_VAL_SHIFT;
2876 
2877 	if ((reg == SND_BBFPRO_CTL_REG1 &&
2878 	     idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) ||
2879 	    (reg == SND_BBFPRO_CTL_REG2 &&
2880 	    (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2881 	     idx == SND_BBFPRO_CTL_REG2_SENS_IN4))) {
2882 		ucontrol->value.enumerated.item[0] = val;
2883 	} else {
2884 		ucontrol->value.integer.value[0] = val;
2885 	}
2886 	return 0;
2887 }
2888 
snd_bbfpro_ctl_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2889 static int snd_bbfpro_ctl_info(struct snd_kcontrol *kcontrol,
2890 			       struct snd_ctl_elem_info *uinfo)
2891 {
2892 	u8 reg, idx;
2893 	int pv;
2894 
2895 	pv = kcontrol->private_value;
2896 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2897 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2898 
2899 	if (reg == SND_BBFPRO_CTL_REG1 &&
2900 	    idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) {
2901 		static const char * const texts[2] = {
2902 			"AutoSync",
2903 			"Internal"
2904 		};
2905 		return snd_ctl_enum_info(uinfo, 1, 2, texts);
2906 	} else if (reg == SND_BBFPRO_CTL_REG2 &&
2907 		   (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2908 		    idx == SND_BBFPRO_CTL_REG2_SENS_IN4)) {
2909 		static const char * const texts[2] = {
2910 			"-10dBV",
2911 			"+4dBu"
2912 		};
2913 		return snd_ctl_enum_info(uinfo, 1, 2, texts);
2914 	}
2915 
2916 	uinfo->count = 1;
2917 	uinfo->value.integer.min = 0;
2918 	uinfo->value.integer.max = 1;
2919 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2920 	return 0;
2921 }
2922 
snd_bbfpro_ctl_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2923 static int snd_bbfpro_ctl_put(struct snd_kcontrol *kcontrol,
2924 			      struct snd_ctl_elem_value *ucontrol)
2925 {
2926 	int err;
2927 	u8 reg, idx;
2928 	int old_value, pv, val;
2929 
2930 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2931 	struct usb_mixer_interface *mixer = list->mixer;
2932 
2933 	pv = kcontrol->private_value;
2934 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2935 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2936 	old_value = (pv >> SND_BBFPRO_CTL_VAL_SHIFT) & SND_BBFPRO_CTL_VAL_MASK;
2937 
2938 	if ((reg == SND_BBFPRO_CTL_REG1 &&
2939 	     idx == SND_BBFPRO_CTL_REG1_CLK_OPTICAL) ||
2940 	    (reg == SND_BBFPRO_CTL_REG2 &&
2941 	    (idx == SND_BBFPRO_CTL_REG2_SENS_IN3 ||
2942 	     idx == SND_BBFPRO_CTL_REG2_SENS_IN4))) {
2943 		val = ucontrol->value.enumerated.item[0];
2944 	} else {
2945 		val = ucontrol->value.integer.value[0];
2946 	}
2947 
2948 	if (val > 1)
2949 		return -EINVAL;
2950 
2951 	if (val == old_value)
2952 		return 0;
2953 
2954 	kcontrol->private_value = reg
2955 		| ((idx & SND_BBFPRO_CTL_IDX_MASK) << SND_BBFPRO_CTL_IDX_SHIFT)
2956 		| ((val & SND_BBFPRO_CTL_VAL_MASK) << SND_BBFPRO_CTL_VAL_SHIFT);
2957 
2958 	err = snd_bbfpro_ctl_update(mixer, reg, idx, val);
2959 	return err < 0 ? err : 1;
2960 }
2961 
snd_bbfpro_ctl_resume(struct usb_mixer_elem_list * list)2962 static int snd_bbfpro_ctl_resume(struct usb_mixer_elem_list *list)
2963 {
2964 	u8 reg, idx;
2965 	int value, pv;
2966 
2967 	pv = list->kctl->private_value;
2968 	reg = pv & SND_BBFPRO_CTL_REG_MASK;
2969 	idx = (pv >> SND_BBFPRO_CTL_IDX_SHIFT) & SND_BBFPRO_CTL_IDX_MASK;
2970 	value = (pv >> SND_BBFPRO_CTL_VAL_SHIFT) & SND_BBFPRO_CTL_VAL_MASK;
2971 
2972 	return snd_bbfpro_ctl_update(list->mixer, reg, idx, value);
2973 }
2974 
snd_bbfpro_gain_update(struct usb_mixer_interface * mixer,u8 channel,u8 gain)2975 static int snd_bbfpro_gain_update(struct usb_mixer_interface *mixer,
2976 				  u8 channel, u8 gain)
2977 {
2978 	int err;
2979 	struct snd_usb_audio *chip = mixer->chip;
2980 
2981 	if (channel < 2) {
2982 		// XLR preamp: 3-bit fine, 5-bit coarse; special case >60
2983 		if (gain < 60)
2984 			gain = ((gain % 3) << 5) | (gain / 3);
2985 		else
2986 			gain = ((gain % 6) << 5) | (60 / 3);
2987 	}
2988 
2989 	err = snd_usb_lock_shutdown(chip);
2990 	if (err < 0)
2991 		return err;
2992 
2993 	err = snd_usb_ctl_msg(chip->dev,
2994 			      usb_sndctrlpipe(chip->dev, 0),
2995 			      SND_BBFPRO_USBREQ_GAIN,
2996 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2997 			      gain, channel, NULL, 0);
2998 
2999 	snd_usb_unlock_shutdown(chip);
3000 	return err;
3001 }
3002 
snd_bbfpro_gain_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3003 static int snd_bbfpro_gain_get(struct snd_kcontrol *kcontrol,
3004 			       struct snd_ctl_elem_value *ucontrol)
3005 {
3006 	int value = kcontrol->private_value & SND_BBFPRO_GAIN_VAL_MASK;
3007 
3008 	ucontrol->value.integer.value[0] = value;
3009 	return 0;
3010 }
3011 
snd_bbfpro_gain_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)3012 static int snd_bbfpro_gain_info(struct snd_kcontrol *kcontrol,
3013 				struct snd_ctl_elem_info *uinfo)
3014 {
3015 	int pv, channel;
3016 
3017 	pv = kcontrol->private_value;
3018 	channel = (pv >> SND_BBFPRO_GAIN_CHANNEL_SHIFT) &
3019 		SND_BBFPRO_GAIN_CHANNEL_MASK;
3020 
3021 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3022 	uinfo->count = 1;
3023 	uinfo->value.integer.min = SND_BBFPRO_GAIN_VAL_MIN;
3024 
3025 	if (channel < 2)
3026 		uinfo->value.integer.max = SND_BBFPRO_GAIN_VAL_MIC_MAX;
3027 	else
3028 		uinfo->value.integer.max = SND_BBFPRO_GAIN_VAL_LINE_MAX;
3029 
3030 	return 0;
3031 }
3032 
snd_bbfpro_gain_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3033 static int snd_bbfpro_gain_put(struct snd_kcontrol *kcontrol,
3034 			       struct snd_ctl_elem_value *ucontrol)
3035 {
3036 	int pv, channel, old_value, value, err;
3037 
3038 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3039 	struct usb_mixer_interface *mixer = list->mixer;
3040 
3041 	pv = kcontrol->private_value;
3042 	channel = (pv >> SND_BBFPRO_GAIN_CHANNEL_SHIFT) &
3043 		SND_BBFPRO_GAIN_CHANNEL_MASK;
3044 	old_value = pv & SND_BBFPRO_GAIN_VAL_MASK;
3045 	value = ucontrol->value.integer.value[0];
3046 
3047 	if (value < SND_BBFPRO_GAIN_VAL_MIN)
3048 		return -EINVAL;
3049 
3050 	if (channel < 2) {
3051 		if (value > SND_BBFPRO_GAIN_VAL_MIC_MAX)
3052 			return -EINVAL;
3053 	} else {
3054 		if (value > SND_BBFPRO_GAIN_VAL_LINE_MAX)
3055 			return -EINVAL;
3056 	}
3057 
3058 	if (value == old_value)
3059 		return 0;
3060 
3061 	err = snd_bbfpro_gain_update(mixer, channel, value);
3062 	if (err < 0)
3063 		return err;
3064 
3065 	kcontrol->private_value =
3066 		(channel << SND_BBFPRO_GAIN_CHANNEL_SHIFT) | value;
3067 	return 1;
3068 }
3069 
snd_bbfpro_gain_resume(struct usb_mixer_elem_list * list)3070 static int snd_bbfpro_gain_resume(struct usb_mixer_elem_list *list)
3071 {
3072 	int pv, channel, value;
3073 	struct snd_kcontrol *kctl = list->kctl;
3074 
3075 	pv = kctl->private_value;
3076 	channel = (pv >> SND_BBFPRO_GAIN_CHANNEL_SHIFT) &
3077 		SND_BBFPRO_GAIN_CHANNEL_MASK;
3078 	value = pv & SND_BBFPRO_GAIN_VAL_MASK;
3079 
3080 	return snd_bbfpro_gain_update(list->mixer, channel, value);
3081 }
3082 
snd_bbfpro_vol_update(struct usb_mixer_interface * mixer,u16 index,u32 value)3083 static int snd_bbfpro_vol_update(struct usb_mixer_interface *mixer, u16 index,
3084 				 u32 value)
3085 {
3086 	struct snd_usb_audio *chip = mixer->chip;
3087 	int err;
3088 	u16 idx;
3089 	u16 usb_idx, usb_val;
3090 	u32 v;
3091 
3092 	err = snd_usb_lock_shutdown(chip);
3093 	if (err < 0)
3094 		return err;
3095 
3096 	idx = index & SND_BBFPRO_MIXER_IDX_MASK;
3097 	// 18 bit linear volume, split so 2 bits end up in index.
3098 	v = value & SND_BBFPRO_MIXER_VAL_MASK;
3099 	usb_idx = idx | (v & 0x3) << 14;
3100 	usb_val = (v >> 2) & 0xffff;
3101 
3102 	err = snd_usb_ctl_msg(chip->dev,
3103 			      usb_sndctrlpipe(chip->dev, 0),
3104 			      SND_BBFPRO_USBREQ_MIXER,
3105 			      USB_DIR_OUT | USB_TYPE_VENDOR |
3106 			      USB_RECIP_DEVICE,
3107 			      usb_val, usb_idx, NULL, 0);
3108 
3109 	snd_usb_unlock_shutdown(chip);
3110 	return err;
3111 }
3112 
snd_bbfpro_vol_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3113 static int snd_bbfpro_vol_get(struct snd_kcontrol *kcontrol,
3114 			      struct snd_ctl_elem_value *ucontrol)
3115 {
3116 	ucontrol->value.integer.value[0] =
3117 		kcontrol->private_value >> SND_BBFPRO_MIXER_VAL_SHIFT;
3118 	return 0;
3119 }
3120 
snd_bbfpro_vol_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)3121 static int snd_bbfpro_vol_info(struct snd_kcontrol *kcontrol,
3122 			       struct snd_ctl_elem_info *uinfo)
3123 {
3124 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3125 	uinfo->count = 1;
3126 	uinfo->value.integer.min = SND_BBFPRO_MIXER_VAL_MIN;
3127 	uinfo->value.integer.max = SND_BBFPRO_MIXER_VAL_MAX;
3128 	return 0;
3129 }
3130 
snd_bbfpro_vol_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3131 static int snd_bbfpro_vol_put(struct snd_kcontrol *kcontrol,
3132 			      struct snd_ctl_elem_value *ucontrol)
3133 {
3134 	int err;
3135 	u16 idx;
3136 	u32 new_val, old_value, uvalue;
3137 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3138 	struct usb_mixer_interface *mixer = list->mixer;
3139 
3140 	uvalue = ucontrol->value.integer.value[0];
3141 	idx = kcontrol->private_value & SND_BBFPRO_MIXER_IDX_MASK;
3142 	old_value = kcontrol->private_value >> SND_BBFPRO_MIXER_VAL_SHIFT;
3143 
3144 	if (uvalue > SND_BBFPRO_MIXER_VAL_MAX)
3145 		return -EINVAL;
3146 
3147 	if (uvalue == old_value)
3148 		return 0;
3149 
3150 	new_val = uvalue & SND_BBFPRO_MIXER_VAL_MASK;
3151 
3152 	kcontrol->private_value = idx
3153 		| (new_val << SND_BBFPRO_MIXER_VAL_SHIFT);
3154 
3155 	err = snd_bbfpro_vol_update(mixer, idx, new_val);
3156 	return err < 0 ? err : 1;
3157 }
3158 
snd_bbfpro_vol_resume(struct usb_mixer_elem_list * list)3159 static int snd_bbfpro_vol_resume(struct usb_mixer_elem_list *list)
3160 {
3161 	int pv = list->kctl->private_value;
3162 	u16 idx = pv & SND_BBFPRO_MIXER_IDX_MASK;
3163 	u32 val = (pv >> SND_BBFPRO_MIXER_VAL_SHIFT)
3164 		& SND_BBFPRO_MIXER_VAL_MASK;
3165 	return snd_bbfpro_vol_update(list->mixer, idx, val);
3166 }
3167 
3168 // Predfine elements
3169 static const struct snd_kcontrol_new snd_bbfpro_ctl_control = {
3170 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3171 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3172 	.index = 0,
3173 	.info = snd_bbfpro_ctl_info,
3174 	.get = snd_bbfpro_ctl_get,
3175 	.put = snd_bbfpro_ctl_put
3176 };
3177 
3178 static const struct snd_kcontrol_new snd_bbfpro_gain_control = {
3179 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3180 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3181 	.index = 0,
3182 	.info = snd_bbfpro_gain_info,
3183 	.get = snd_bbfpro_gain_get,
3184 	.put = snd_bbfpro_gain_put
3185 };
3186 
3187 static const struct snd_kcontrol_new snd_bbfpro_vol_control = {
3188 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3189 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3190 	.index = 0,
3191 	.info = snd_bbfpro_vol_info,
3192 	.get = snd_bbfpro_vol_get,
3193 	.put = snd_bbfpro_vol_put
3194 };
3195 
snd_bbfpro_ctl_add(struct usb_mixer_interface * mixer,u8 reg,u8 index,char * name)3196 static int snd_bbfpro_ctl_add(struct usb_mixer_interface *mixer, u8 reg,
3197 			      u8 index, char *name)
3198 {
3199 	struct snd_kcontrol_new knew = snd_bbfpro_ctl_control;
3200 
3201 	knew.name = name;
3202 	knew.private_value = (reg & SND_BBFPRO_CTL_REG_MASK)
3203 		| ((index & SND_BBFPRO_CTL_IDX_MASK)
3204 			<< SND_BBFPRO_CTL_IDX_SHIFT);
3205 
3206 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_ctl_resume,
3207 		&knew, NULL);
3208 }
3209 
snd_bbfpro_gain_add(struct usb_mixer_interface * mixer,u8 channel,char * name)3210 static int snd_bbfpro_gain_add(struct usb_mixer_interface *mixer, u8 channel,
3211 			       char *name)
3212 {
3213 	struct snd_kcontrol_new knew = snd_bbfpro_gain_control;
3214 
3215 	knew.name = name;
3216 	knew.private_value = channel << SND_BBFPRO_GAIN_CHANNEL_SHIFT;
3217 
3218 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_gain_resume,
3219 		&knew, NULL);
3220 }
3221 
snd_bbfpro_vol_add(struct usb_mixer_interface * mixer,u16 index,char * name)3222 static int snd_bbfpro_vol_add(struct usb_mixer_interface *mixer, u16 index,
3223 			      char *name)
3224 {
3225 	struct snd_kcontrol_new knew = snd_bbfpro_vol_control;
3226 
3227 	knew.name = name;
3228 	knew.private_value = index & SND_BBFPRO_MIXER_IDX_MASK;
3229 
3230 	return add_single_ctl_with_resume(mixer, 0, snd_bbfpro_vol_resume,
3231 		&knew, NULL);
3232 }
3233 
snd_bbfpro_controls_create(struct usb_mixer_interface * mixer)3234 static int snd_bbfpro_controls_create(struct usb_mixer_interface *mixer)
3235 {
3236 	int err, i, o;
3237 	char name[48];
3238 
3239 	static const char * const input[] = {
3240 		"AN1", "AN2", "IN3", "IN4", "AS1", "AS2", "ADAT3",
3241 		"ADAT4", "ADAT5", "ADAT6", "ADAT7", "ADAT8"};
3242 
3243 	static const char * const output[] = {
3244 		"AN1", "AN2", "PH3", "PH4", "AS1", "AS2", "ADAT3", "ADAT4",
3245 		"ADAT5", "ADAT6", "ADAT7", "ADAT8"};
3246 
3247 	for (o = 0 ; o < 12 ; ++o) {
3248 		for (i = 0 ; i < 12 ; ++i) {
3249 			// Line routing
3250 			snprintf(name, sizeof(name),
3251 				 "%s-%s-%s Playback Volume",
3252 				 (i < 2 ? "Mic" : "Line"),
3253 				 input[i], output[o]);
3254 			err = snd_bbfpro_vol_add(mixer, (26 * o + i), name);
3255 			if (err < 0)
3256 				return err;
3257 
3258 			// PCM routing... yes, it is output remapping
3259 			snprintf(name, sizeof(name),
3260 				 "PCM-%s-%s Playback Volume",
3261 				 output[i], output[o]);
3262 			err = snd_bbfpro_vol_add(mixer, (26 * o + 12 + i),
3263 						 name);
3264 			if (err < 0)
3265 				return err;
3266 		}
3267 	}
3268 
3269 	// Main out volume
3270 	for (i = 0 ; i < 12 ; ++i) {
3271 		snprintf(name, sizeof(name), "Main-Out %s", output[i]);
3272 		// Main outs are offset to 992
3273 		err = snd_bbfpro_vol_add(mixer,
3274 					 i + SND_BBFPRO_MIXER_MAIN_OUT_CH_OFFSET,
3275 					 name);
3276 		if (err < 0)
3277 			return err;
3278 	}
3279 
3280 	// Input gain
3281 	for (i = 0 ; i < 4 ; ++i) {
3282 		if (i < 2)
3283 			snprintf(name, sizeof(name), "Mic-%s Gain", input[i]);
3284 		else
3285 			snprintf(name, sizeof(name), "Line-%s Gain", input[i]);
3286 
3287 		err = snd_bbfpro_gain_add(mixer, i, name);
3288 		if (err < 0)
3289 			return err;
3290 	}
3291 
3292 	// Control Reg 1
3293 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3294 				 SND_BBFPRO_CTL_REG1_CLK_OPTICAL,
3295 				 "Sample Clock Source");
3296 	if (err < 0)
3297 		return err;
3298 
3299 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3300 				 SND_BBFPRO_CTL_REG1_SPDIF_PRO,
3301 				 "IEC958 Pro Mask");
3302 	if (err < 0)
3303 		return err;
3304 
3305 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3306 				 SND_BBFPRO_CTL_REG1_SPDIF_EMPH,
3307 				 "IEC958 Emphasis");
3308 	if (err < 0)
3309 		return err;
3310 
3311 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG1,
3312 				 SND_BBFPRO_CTL_REG1_SPDIF_OPTICAL,
3313 				 "IEC958 Switch");
3314 	if (err < 0)
3315 		return err;
3316 
3317 	// Control Reg 2
3318 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3319 				 SND_BBFPRO_CTL_REG2_48V_AN1,
3320 				 "Mic-AN1 48V");
3321 	if (err < 0)
3322 		return err;
3323 
3324 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3325 				 SND_BBFPRO_CTL_REG2_48V_AN2,
3326 				 "Mic-AN2 48V");
3327 	if (err < 0)
3328 		return err;
3329 
3330 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3331 				 SND_BBFPRO_CTL_REG2_SENS_IN3,
3332 				 "Line-IN3 Sens.");
3333 	if (err < 0)
3334 		return err;
3335 
3336 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3337 				 SND_BBFPRO_CTL_REG2_SENS_IN4,
3338 				 "Line-IN4 Sens.");
3339 	if (err < 0)
3340 		return err;
3341 
3342 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3343 				 SND_BBFPRO_CTL_REG2_PAD_AN1,
3344 				 "Mic-AN1 PAD");
3345 	if (err < 0)
3346 		return err;
3347 
3348 	err = snd_bbfpro_ctl_add(mixer, SND_BBFPRO_CTL_REG2,
3349 				 SND_BBFPRO_CTL_REG2_PAD_AN2,
3350 				 "Mic-AN2 PAD");
3351 	if (err < 0)
3352 		return err;
3353 
3354 	return 0;
3355 }
3356 
3357 /*
3358  * RME Digiface USB
3359  */
3360 
3361 #define RME_DIGIFACE_READ_STATUS 17
3362 #define RME_DIGIFACE_STATUS_REG0L 0
3363 #define RME_DIGIFACE_STATUS_REG0H 1
3364 #define RME_DIGIFACE_STATUS_REG1L 2
3365 #define RME_DIGIFACE_STATUS_REG1H 3
3366 #define RME_DIGIFACE_STATUS_REG2L 4
3367 #define RME_DIGIFACE_STATUS_REG2H 5
3368 #define RME_DIGIFACE_STATUS_REG3L 6
3369 #define RME_DIGIFACE_STATUS_REG3H 7
3370 
3371 #define RME_DIGIFACE_CTL_REG1 16
3372 #define RME_DIGIFACE_CTL_REG2 18
3373 
3374 /* Reg is overloaded, 0-7 for status halfwords or 16 or 18 for control registers */
3375 #define RME_DIGIFACE_REGISTER(reg, mask) (((reg) << 16) | (mask))
3376 #define RME_DIGIFACE_INVERT BIT(31)
3377 
3378 /* Nonconst helpers */
3379 #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
3380 #define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
3381 
snd_rme_digiface_write_reg(struct snd_kcontrol * kcontrol,int item,u16 mask,u16 val)3382 static int snd_rme_digiface_write_reg(struct snd_kcontrol *kcontrol, int item, u16 mask, u16 val)
3383 {
3384 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3385 	struct snd_usb_audio *chip = list->mixer->chip;
3386 	struct usb_device *dev = chip->dev;
3387 	int err;
3388 
3389 	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
3390 			      item,
3391 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3392 			      val, mask, NULL, 0);
3393 	if (err < 0)
3394 		dev_err(&dev->dev,
3395 			"unable to issue control set request %d (ret = %d)",
3396 			item, err);
3397 	return err;
3398 }
3399 
snd_rme_digiface_read_status(struct snd_kcontrol * kcontrol,u32 status[4])3400 static int snd_rme_digiface_read_status(struct snd_kcontrol *kcontrol, u32 status[4])
3401 {
3402 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
3403 	struct snd_usb_audio *chip = list->mixer->chip;
3404 	struct usb_device *dev = chip->dev;
3405 	__le32 buf[4];
3406 	int err;
3407 
3408 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
3409 			      RME_DIGIFACE_READ_STATUS,
3410 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3411 			      0, 0,
3412 			      buf, sizeof(buf));
3413 	if (err < 0) {
3414 		dev_err(&dev->dev,
3415 			"unable to issue status read request (ret = %d)",
3416 			err);
3417 	} else {
3418 		for (int i = 0; i < ARRAY_SIZE(buf); i++)
3419 			status[i] = le32_to_cpu(buf[i]);
3420 	}
3421 	return err;
3422 }
3423 
snd_rme_digiface_get_status_val(struct snd_kcontrol * kcontrol)3424 static int snd_rme_digiface_get_status_val(struct snd_kcontrol *kcontrol)
3425 {
3426 	int err;
3427 	u32 status[4];
3428 	bool invert = kcontrol->private_value & RME_DIGIFACE_INVERT;
3429 	u8 reg = (kcontrol->private_value >> 16) & 0xff;
3430 	u16 mask = kcontrol->private_value & 0xffff;
3431 	u16 val;
3432 
3433 	err = snd_rme_digiface_read_status(kcontrol, status);
3434 	if (err < 0)
3435 		return err;
3436 
3437 	switch (reg) {
3438 	/* Status register halfwords */
3439 	case RME_DIGIFACE_STATUS_REG0L ... RME_DIGIFACE_STATUS_REG3H:
3440 		break;
3441 	case RME_DIGIFACE_CTL_REG1: /* Control register 1, present in halfword 3L */
3442 		reg = RME_DIGIFACE_STATUS_REG3L;
3443 		break;
3444 	case RME_DIGIFACE_CTL_REG2: /* Control register 2, present in halfword 3H */
3445 		reg = RME_DIGIFACE_STATUS_REG3H;
3446 		break;
3447 	default:
3448 		return -EINVAL;
3449 	}
3450 
3451 	if (reg & 1)
3452 		val = status[reg >> 1] >> 16;
3453 	else
3454 		val = status[reg >> 1] & 0xffff;
3455 
3456 	if (invert)
3457 		val ^= mask;
3458 
3459 	return field_get(mask, val);
3460 }
3461 
snd_rme_digiface_rate_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3462 static int snd_rme_digiface_rate_get(struct snd_kcontrol *kcontrol,
3463 				     struct snd_ctl_elem_value *ucontrol)
3464 {
3465 	int freq = snd_rme_digiface_get_status_val(kcontrol);
3466 
3467 	if (freq < 0)
3468 		return freq;
3469 	if (freq >= ARRAY_SIZE(snd_rme_rate_table))
3470 		return -EIO;
3471 
3472 	ucontrol->value.integer.value[0] = snd_rme_rate_table[freq];
3473 	return 0;
3474 }
3475 
snd_rme_digiface_enum_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3476 static int snd_rme_digiface_enum_get(struct snd_kcontrol *kcontrol,
3477 				     struct snd_ctl_elem_value *ucontrol)
3478 {
3479 	int val = snd_rme_digiface_get_status_val(kcontrol);
3480 
3481 	if (val < 0)
3482 		return val;
3483 
3484 	ucontrol->value.enumerated.item[0] = val;
3485 	return 0;
3486 }
3487 
snd_rme_digiface_enum_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3488 static int snd_rme_digiface_enum_put(struct snd_kcontrol *kcontrol,
3489 				     struct snd_ctl_elem_value *ucontrol)
3490 {
3491 	bool invert = kcontrol->private_value & RME_DIGIFACE_INVERT;
3492 	u8 reg = (kcontrol->private_value >> 16) & 0xff;
3493 	u16 mask = kcontrol->private_value & 0xffff;
3494 	u16 val = field_prep(mask, ucontrol->value.enumerated.item[0]);
3495 
3496 	if (invert)
3497 		val ^= mask;
3498 
3499 	return snd_rme_digiface_write_reg(kcontrol, reg, mask, val);
3500 }
3501 
snd_rme_digiface_current_sync_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3502 static int snd_rme_digiface_current_sync_get(struct snd_kcontrol *kcontrol,
3503 					     struct snd_ctl_elem_value *ucontrol)
3504 {
3505 	int ret = snd_rme_digiface_enum_get(kcontrol, ucontrol);
3506 
3507 	/* 7 means internal for current sync */
3508 	if (ucontrol->value.enumerated.item[0] == 7)
3509 		ucontrol->value.enumerated.item[0] = 0;
3510 
3511 	return ret;
3512 }
3513 
snd_rme_digiface_sync_state_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3514 static int snd_rme_digiface_sync_state_get(struct snd_kcontrol *kcontrol,
3515 					   struct snd_ctl_elem_value *ucontrol)
3516 {
3517 	u32 status[4];
3518 	int err;
3519 	bool valid, sync;
3520 
3521 	err = snd_rme_digiface_read_status(kcontrol, status);
3522 	if (err < 0)
3523 		return err;
3524 
3525 	valid = status[0] & BIT(kcontrol->private_value);
3526 	sync = status[0] & BIT(5 + kcontrol->private_value);
3527 
3528 	if (!valid)
3529 		ucontrol->value.enumerated.item[0] = SND_RME_CLOCK_NOLOCK;
3530 	else if (!sync)
3531 		ucontrol->value.enumerated.item[0] = SND_RME_CLOCK_LOCK;
3532 	else
3533 		ucontrol->value.enumerated.item[0] = SND_RME_CLOCK_SYNC;
3534 	return 0;
3535 }
3536 
snd_rme_digiface_format_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)3537 static int snd_rme_digiface_format_info(struct snd_kcontrol *kcontrol,
3538 					struct snd_ctl_elem_info *uinfo)
3539 {
3540 	static const char *const format[] = {
3541 		"ADAT", "S/PDIF"
3542 	};
3543 
3544 	return snd_ctl_enum_info(uinfo, 1,
3545 				 ARRAY_SIZE(format), format);
3546 }
3547 
snd_rme_digiface_sync_source_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)3548 static int snd_rme_digiface_sync_source_info(struct snd_kcontrol *kcontrol,
3549 					     struct snd_ctl_elem_info *uinfo)
3550 {
3551 	static const char *const sync_sources[] = {
3552 		"Internal", "Input 1", "Input 2", "Input 3", "Input 4"
3553 	};
3554 
3555 	return snd_ctl_enum_info(uinfo, 1,
3556 				 ARRAY_SIZE(sync_sources), sync_sources);
3557 }
3558 
snd_rme_digiface_rate_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)3559 static int snd_rme_digiface_rate_info(struct snd_kcontrol *kcontrol,
3560 				      struct snd_ctl_elem_info *uinfo)
3561 {
3562 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3563 	uinfo->count = 1;
3564 	uinfo->value.integer.min = 0;
3565 	uinfo->value.integer.max = 200000;
3566 	uinfo->value.integer.step = 0;
3567 	return 0;
3568 }
3569 
3570 static const struct snd_kcontrol_new snd_rme_digiface_controls[] = {
3571 	{
3572 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3573 		.name = "Input 1 Sync",
3574 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3575 		.info = snd_rme_sync_state_info,
3576 		.get = snd_rme_digiface_sync_state_get,
3577 		.private_value = 0,
3578 	},
3579 	{
3580 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3581 		.name = "Input 1 Format",
3582 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3583 		.info = snd_rme_digiface_format_info,
3584 		.get = snd_rme_digiface_enum_get,
3585 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0H, BIT(0)) |
3586 			RME_DIGIFACE_INVERT,
3587 	},
3588 	{
3589 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3590 		.name = "Input 1 Rate",
3591 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3592 		.info = snd_rme_digiface_rate_info,
3593 		.get = snd_rme_digiface_rate_get,
3594 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(3, 0)),
3595 	},
3596 	{
3597 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3598 		.name = "Input 2 Sync",
3599 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3600 		.info = snd_rme_sync_state_info,
3601 		.get = snd_rme_digiface_sync_state_get,
3602 		.private_value = 1,
3603 	},
3604 	{
3605 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3606 		.name = "Input 2 Format",
3607 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3608 		.info = snd_rme_digiface_format_info,
3609 		.get = snd_rme_digiface_enum_get,
3610 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, BIT(13)) |
3611 			RME_DIGIFACE_INVERT,
3612 	},
3613 	{
3614 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3615 		.name = "Input 2 Rate",
3616 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3617 		.info = snd_rme_digiface_rate_info,
3618 		.get = snd_rme_digiface_rate_get,
3619 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(7, 4)),
3620 	},
3621 	{
3622 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3623 		.name = "Input 3 Sync",
3624 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3625 		.info = snd_rme_sync_state_info,
3626 		.get = snd_rme_digiface_sync_state_get,
3627 		.private_value = 2,
3628 	},
3629 	{
3630 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3631 		.name = "Input 3 Format",
3632 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3633 		.info = snd_rme_digiface_format_info,
3634 		.get = snd_rme_digiface_enum_get,
3635 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, BIT(14)) |
3636 			RME_DIGIFACE_INVERT,
3637 	},
3638 	{
3639 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3640 		.name = "Input 3 Rate",
3641 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3642 		.info = snd_rme_digiface_rate_info,
3643 		.get = snd_rme_digiface_rate_get,
3644 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(11, 8)),
3645 	},
3646 	{
3647 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3648 		.name = "Input 4 Sync",
3649 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3650 		.info = snd_rme_sync_state_info,
3651 		.get = snd_rme_digiface_sync_state_get,
3652 		.private_value = 3,
3653 	},
3654 	{
3655 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3656 		.name = "Input 4 Format",
3657 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3658 		.info = snd_rme_digiface_format_info,
3659 		.get = snd_rme_digiface_enum_get,
3660 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, GENMASK(15, 12)) |
3661 			RME_DIGIFACE_INVERT,
3662 	},
3663 	{
3664 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3665 		.name = "Input 4 Rate",
3666 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3667 		.info = snd_rme_digiface_rate_info,
3668 		.get = snd_rme_digiface_rate_get,
3669 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1L, GENMASK(3, 0)),
3670 	},
3671 	{
3672 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3673 		.name = "Output 1 Format",
3674 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3675 		.info = snd_rme_digiface_format_info,
3676 		.get = snd_rme_digiface_enum_get,
3677 		.put = snd_rme_digiface_enum_put,
3678 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(0)),
3679 	},
3680 	{
3681 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3682 		.name = "Output 2 Format",
3683 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3684 		.info = snd_rme_digiface_format_info,
3685 		.get = snd_rme_digiface_enum_get,
3686 		.put = snd_rme_digiface_enum_put,
3687 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(1)),
3688 	},
3689 	{
3690 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3691 		.name = "Output 3 Format",
3692 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3693 		.info = snd_rme_digiface_format_info,
3694 		.get = snd_rme_digiface_enum_get,
3695 		.put = snd_rme_digiface_enum_put,
3696 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(3)),
3697 	},
3698 	{
3699 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3700 		.name = "Output 4 Format",
3701 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3702 		.info = snd_rme_digiface_format_info,
3703 		.get = snd_rme_digiface_enum_get,
3704 		.put = snd_rme_digiface_enum_put,
3705 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG2, BIT(4)),
3706 	},
3707 	{
3708 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3709 		.name = "Sync Source",
3710 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3711 		.info = snd_rme_digiface_sync_source_info,
3712 		.get = snd_rme_digiface_enum_get,
3713 		.put = snd_rme_digiface_enum_put,
3714 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG1, GENMASK(2, 0)),
3715 	},
3716 	{
3717 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3718 		.name = "Current Sync Source",
3719 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3720 		.info = snd_rme_digiface_sync_source_info,
3721 		.get = snd_rme_digiface_current_sync_get,
3722 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG0L, GENMASK(12, 10)),
3723 	},
3724 	{
3725 		/*
3726 		 * This is writeable, but it is only set by the PCM rate.
3727 		 * Mixer apps currently need to drive the mixer using raw USB requests,
3728 		 * so they can also change this that way to configure the rate for
3729 		 * stand-alone operation when the PCM is closed.
3730 		 */
3731 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3732 		.name = "System Rate",
3733 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3734 		.info = snd_rme_rate_info,
3735 		.get = snd_rme_digiface_rate_get,
3736 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_CTL_REG1, GENMASK(6, 3)),
3737 	},
3738 	{
3739 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3740 		.name = "Current Rate",
3741 		.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3742 		.info = snd_rme_rate_info,
3743 		.get = snd_rme_digiface_rate_get,
3744 		.private_value = RME_DIGIFACE_REGISTER(RME_DIGIFACE_STATUS_REG1H, GENMASK(7, 4)),
3745 	}
3746 };
3747 
snd_rme_digiface_controls_create(struct usb_mixer_interface * mixer)3748 static int snd_rme_digiface_controls_create(struct usb_mixer_interface *mixer)
3749 {
3750 	int err, i;
3751 
3752 	for (i = 0; i < ARRAY_SIZE(snd_rme_digiface_controls); ++i) {
3753 		err = add_single_ctl_with_resume(mixer, 0,
3754 						 NULL,
3755 						 &snd_rme_digiface_controls[i],
3756 						 NULL);
3757 		if (err < 0)
3758 			return err;
3759 	}
3760 
3761 	return 0;
3762 }
3763 
3764 /*
3765  * Pioneer DJ / AlphaTheta DJM Mixers
3766  *
3767  * These devices generally have options for soft-switching the playback and
3768  * capture sources in addition to the recording level. Although different
3769  * devices have different configurations, there seems to be canonical values
3770  * for specific capture/playback types:  See the definitions of these below.
3771  *
3772  * The wValue is masked with the stereo channel number. e.g. Setting Ch2 to
3773  * capture phono would be 0x0203. Capture, playback and capture level have
3774  * different wIndexes.
3775  */
3776 
3777 // Capture types
3778 #define SND_DJM_CAP_LINE	0x00
3779 #define SND_DJM_CAP_CDLINE	0x01
3780 #define SND_DJM_CAP_DIGITAL	0x02
3781 #define SND_DJM_CAP_PHONO	0x03
3782 #define SND_DJM_CAP_PREFADER	0x05
3783 #define SND_DJM_CAP_PFADER	0x06
3784 #define SND_DJM_CAP_XFADERA	0x07
3785 #define SND_DJM_CAP_XFADERB	0x08
3786 #define SND_DJM_CAP_MIC		0x09
3787 #define SND_DJM_CAP_AUX		0x0d
3788 #define SND_DJM_CAP_RECOUT	0x0a
3789 #define SND_DJM_CAP_RECOUT_NOMIC	0x0e
3790 #define SND_DJM_CAP_NONE	0x0f
3791 #define SND_DJM_CAP_FXSEND	0x10
3792 #define SND_DJM_CAP_CH1PFADER	0x11
3793 #define SND_DJM_CAP_CH2PFADER	0x12
3794 #define SND_DJM_CAP_CH3PFADER	0x13
3795 #define SND_DJM_CAP_CH4PFADER	0x14
3796 #define SND_DJM_CAP_EXT1SEND	0x21
3797 #define SND_DJM_CAP_EXT2SEND	0x22
3798 #define SND_DJM_CAP_CH1PREFADER	0x31
3799 #define SND_DJM_CAP_CH2PREFADER	0x32
3800 #define SND_DJM_CAP_CH3PREFADER	0x33
3801 #define SND_DJM_CAP_CH4PREFADER	0x34
3802 
3803 // Playback types
3804 #define SND_DJM_PB_CH1		0x00
3805 #define SND_DJM_PB_CH2		0x01
3806 #define SND_DJM_PB_AUX		0x04
3807 
3808 #define SND_DJM_WINDEX_CAP	0x8002
3809 #define SND_DJM_WINDEX_CAPLVL	0x8003
3810 #define SND_DJM_WINDEX_PB	0x8016
3811 
3812 // kcontrol->private_value layout
3813 #define SND_DJM_VALUE_MASK	0x0000ffff
3814 #define SND_DJM_GROUP_MASK	0x00ff0000
3815 #define SND_DJM_DEVICE_MASK	0xff000000
3816 #define SND_DJM_GROUP_SHIFT	16
3817 #define SND_DJM_DEVICE_SHIFT	24
3818 
3819 // device table index
3820 // used for the snd_djm_devices table, so please update accordingly
3821 #define SND_DJM_250MK2_IDX	0x0
3822 #define SND_DJM_750_IDX		0x1
3823 #define SND_DJM_850_IDX		0x2
3824 #define SND_DJM_900NXS2_IDX	0x3
3825 #define SND_DJM_750MK2_IDX	0x4
3826 #define SND_DJM_450_IDX		0x5
3827 #define SND_DJM_A9_IDX		0x6
3828 #define SND_DJM_V10_IDX	0x7
3829 
3830 #define SND_DJM_CTL(_name, suffix, _default_value, _windex) { \
3831 	.name = _name, \
3832 	.options = snd_djm_opts_##suffix, \
3833 	.noptions = ARRAY_SIZE(snd_djm_opts_##suffix), \
3834 	.default_value = _default_value, \
3835 	.wIndex = _windex }
3836 
3837 #define SND_DJM_DEVICE(suffix) { \
3838 	.controls = snd_djm_ctls_##suffix, \
3839 	.ncontrols = ARRAY_SIZE(snd_djm_ctls_##suffix) }
3840 
3841 struct snd_djm_device {
3842 	const char *name;
3843 	const struct snd_djm_ctl *controls;
3844 	size_t ncontrols;
3845 };
3846 
3847 struct snd_djm_ctl {
3848 	const char *name;
3849 	const u16 *options;
3850 	size_t noptions;
3851 	u16 default_value;
3852 	u16 wIndex;
3853 };
3854 
snd_djm_get_label_caplevel_common(u16 wvalue)3855 static const char *snd_djm_get_label_caplevel_common(u16 wvalue)
3856 {
3857 	switch (wvalue) {
3858 	case 0x0000:	return "-19dB";
3859 	case 0x0100:	return "-15dB";
3860 	case 0x0200:	return "-10dB";
3861 	case 0x0300:	return "-5dB";
3862 	default:	return NULL;
3863 	}
3864 };
3865 
3866 // Models like DJM-A9 or DJM-V10 have different capture levels than others
snd_djm_get_label_caplevel_high(u16 wvalue)3867 static const char *snd_djm_get_label_caplevel_high(u16 wvalue)
3868 {
3869 	switch (wvalue) {
3870 	case 0x0000:	return "+15dB";
3871 	case 0x0100:	return "+12dB";
3872 	case 0x0200:	return "+9dB";
3873 	case 0x0300:	return "+6dB";
3874 	case 0x0400:	return "+3dB";
3875 	case 0x0500:	return "0dB";
3876 	default:	return NULL;
3877 	}
3878 };
3879 
snd_djm_get_label_cap_common(u16 wvalue)3880 static const char *snd_djm_get_label_cap_common(u16 wvalue)
3881 {
3882 	switch (wvalue & 0x00ff) {
3883 	case SND_DJM_CAP_LINE:		return "Control Tone LINE";
3884 	case SND_DJM_CAP_CDLINE:	return "Control Tone CD/LINE";
3885 	case SND_DJM_CAP_DIGITAL:	return "Control Tone DIGITAL";
3886 	case SND_DJM_CAP_PHONO:		return "Control Tone PHONO";
3887 	case SND_DJM_CAP_PFADER:	return "Post Fader";
3888 	case SND_DJM_CAP_XFADERA:	return "Cross Fader A";
3889 	case SND_DJM_CAP_XFADERB:	return "Cross Fader B";
3890 	case SND_DJM_CAP_MIC:		return "Mic";
3891 	case SND_DJM_CAP_RECOUT:	return "Rec Out";
3892 	case SND_DJM_CAP_RECOUT_NOMIC:	return "Rec Out without Mic";
3893 	case SND_DJM_CAP_AUX:		return "Aux";
3894 	case SND_DJM_CAP_NONE:		return "None";
3895 	case SND_DJM_CAP_FXSEND:	return "FX SEND";
3896 	case SND_DJM_CAP_CH1PREFADER:	return "Pre Fader Ch1";
3897 	case SND_DJM_CAP_CH2PREFADER:	return "Pre Fader Ch2";
3898 	case SND_DJM_CAP_CH3PREFADER:	return "Pre Fader Ch3";
3899 	case SND_DJM_CAP_CH4PREFADER:	return "Pre Fader Ch4";
3900 	case SND_DJM_CAP_CH1PFADER:	return "Post Fader Ch1";
3901 	case SND_DJM_CAP_CH2PFADER:	return "Post Fader Ch2";
3902 	case SND_DJM_CAP_CH3PFADER:	return "Post Fader Ch3";
3903 	case SND_DJM_CAP_CH4PFADER:	return "Post Fader Ch4";
3904 	case SND_DJM_CAP_EXT1SEND:	return "EXT1 SEND";
3905 	case SND_DJM_CAP_EXT2SEND:	return "EXT2 SEND";
3906 	default:			return NULL;
3907 	}
3908 };
3909 
3910 // The DJM-850 has different values for CD/LINE and LINE capture
3911 // control options than the other DJM declared in this file.
snd_djm_get_label_cap_850(u16 wvalue)3912 static const char *snd_djm_get_label_cap_850(u16 wvalue)
3913 {
3914 	switch (wvalue & 0x00ff) {
3915 	case 0x00:		return "Control Tone CD/LINE";
3916 	case 0x01:		return "Control Tone LINE";
3917 	default:		return snd_djm_get_label_cap_common(wvalue);
3918 	}
3919 };
3920 
snd_djm_get_label_caplevel(u8 device_idx,u16 wvalue)3921 static const char *snd_djm_get_label_caplevel(u8 device_idx, u16 wvalue)
3922 {
3923 	switch (device_idx) {
3924 	case SND_DJM_A9_IDX:		return snd_djm_get_label_caplevel_high(wvalue);
3925 	case SND_DJM_V10_IDX:		return snd_djm_get_label_caplevel_high(wvalue);
3926 	default:			return snd_djm_get_label_caplevel_common(wvalue);
3927 	}
3928 };
3929 
snd_djm_get_label_cap(u8 device_idx,u16 wvalue)3930 static const char *snd_djm_get_label_cap(u8 device_idx, u16 wvalue)
3931 {
3932 	switch (device_idx) {
3933 	case SND_DJM_850_IDX:		return snd_djm_get_label_cap_850(wvalue);
3934 	default:			return snd_djm_get_label_cap_common(wvalue);
3935 	}
3936 };
3937 
snd_djm_get_label_pb(u16 wvalue)3938 static const char *snd_djm_get_label_pb(u16 wvalue)
3939 {
3940 	switch (wvalue & 0x00ff) {
3941 	case SND_DJM_PB_CH1:	return "Ch1";
3942 	case SND_DJM_PB_CH2:	return "Ch2";
3943 	case SND_DJM_PB_AUX:	return "Aux";
3944 	default:		return NULL;
3945 	}
3946 };
3947 
snd_djm_get_label(u8 device_idx,u16 wvalue,u16 windex)3948 static const char *snd_djm_get_label(u8 device_idx, u16 wvalue, u16 windex)
3949 {
3950 	switch (windex) {
3951 	case SND_DJM_WINDEX_CAPLVL:	return snd_djm_get_label_caplevel(device_idx, wvalue);
3952 	case SND_DJM_WINDEX_CAP:	return snd_djm_get_label_cap(device_idx, wvalue);
3953 	case SND_DJM_WINDEX_PB:		return snd_djm_get_label_pb(wvalue);
3954 	default:			return NULL;
3955 	}
3956 };
3957 
3958 // common DJM capture level option values
3959 static const u16 snd_djm_opts_cap_level[] = {
3960 	0x0000, 0x0100, 0x0200, 0x0300 };
3961 
3962 // DJM-250MK2
3963 static const u16 snd_djm_opts_250mk2_cap1[] = {
3964 	0x0103, 0x0100, 0x0106, 0x0107, 0x0108, 0x0109, 0x010d, 0x010a };
3965 
3966 static const u16 snd_djm_opts_250mk2_cap2[] = {
3967 	0x0203, 0x0200, 0x0206, 0x0207, 0x0208, 0x0209, 0x020d, 0x020a };
3968 
3969 static const u16 snd_djm_opts_250mk2_cap3[] = {
3970 	0x030a, 0x0311, 0x0312, 0x0307, 0x0308, 0x0309, 0x030d };
3971 
3972 static const u16 snd_djm_opts_250mk2_pb1[] = { 0x0100, 0x0101, 0x0104 };
3973 static const u16 snd_djm_opts_250mk2_pb2[] = { 0x0200, 0x0201, 0x0204 };
3974 static const u16 snd_djm_opts_250mk2_pb3[] = { 0x0300, 0x0301, 0x0304 };
3975 
3976 static const struct snd_djm_ctl snd_djm_ctls_250mk2[] = {
3977 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
3978 	SND_DJM_CTL("Input 1 Capture Switch",  250mk2_cap1, 2, SND_DJM_WINDEX_CAP),
3979 	SND_DJM_CTL("Input 2 Capture Switch",  250mk2_cap2, 2, SND_DJM_WINDEX_CAP),
3980 	SND_DJM_CTL("Input 3 Capture Switch",  250mk2_cap3, 0, SND_DJM_WINDEX_CAP),
3981 	SND_DJM_CTL("Output 1 Playback Switch", 250mk2_pb1, 0, SND_DJM_WINDEX_PB),
3982 	SND_DJM_CTL("Output 2 Playback Switch", 250mk2_pb2, 1, SND_DJM_WINDEX_PB),
3983 	SND_DJM_CTL("Output 3 Playback Switch", 250mk2_pb3, 2, SND_DJM_WINDEX_PB)
3984 };
3985 
3986 // DJM-450
3987 static const u16 snd_djm_opts_450_cap1[] = {
3988 	0x0103, 0x0100, 0x0106, 0x0107, 0x0108, 0x0109, 0x010d, 0x010a };
3989 
3990 static const u16 snd_djm_opts_450_cap2[] = {
3991 	0x0203, 0x0200, 0x0206, 0x0207, 0x0208, 0x0209, 0x020d, 0x020a };
3992 
3993 static const u16 snd_djm_opts_450_cap3[] = {
3994 	0x030a, 0x0311, 0x0312, 0x0307, 0x0308, 0x0309, 0x030d };
3995 
3996 static const u16 snd_djm_opts_450_pb1[] = { 0x0100, 0x0101, 0x0104 };
3997 static const u16 snd_djm_opts_450_pb2[] = { 0x0200, 0x0201, 0x0204 };
3998 static const u16 snd_djm_opts_450_pb3[] = { 0x0300, 0x0301, 0x0304 };
3999 
4000 static const struct snd_djm_ctl snd_djm_ctls_450[] = {
4001 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4002 	SND_DJM_CTL("Input 1 Capture Switch",  450_cap1, 2, SND_DJM_WINDEX_CAP),
4003 	SND_DJM_CTL("Input 2 Capture Switch",  450_cap2, 2, SND_DJM_WINDEX_CAP),
4004 	SND_DJM_CTL("Input 3 Capture Switch",  450_cap3, 0, SND_DJM_WINDEX_CAP),
4005 	SND_DJM_CTL("Output 1 Playback Switch", 450_pb1, 0, SND_DJM_WINDEX_PB),
4006 	SND_DJM_CTL("Output 2 Playback Switch", 450_pb2, 1, SND_DJM_WINDEX_PB),
4007 	SND_DJM_CTL("Output 3 Playback Switch", 450_pb3, 2, SND_DJM_WINDEX_PB)
4008 };
4009 
4010 // DJM-750
4011 static const u16 snd_djm_opts_750_cap1[] = {
4012 	0x0101, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a, 0x010f };
4013 static const u16 snd_djm_opts_750_cap2[] = {
4014 	0x0200, 0x0201, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020f };
4015 static const u16 snd_djm_opts_750_cap3[] = {
4016 	0x0300, 0x0301, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030f };
4017 static const u16 snd_djm_opts_750_cap4[] = {
4018 	0x0401, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040f };
4019 
4020 static const struct snd_djm_ctl snd_djm_ctls_750[] = {
4021 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4022 	SND_DJM_CTL("Input 1 Capture Switch", 750_cap1, 2, SND_DJM_WINDEX_CAP),
4023 	SND_DJM_CTL("Input 2 Capture Switch", 750_cap2, 2, SND_DJM_WINDEX_CAP),
4024 	SND_DJM_CTL("Input 3 Capture Switch", 750_cap3, 0, SND_DJM_WINDEX_CAP),
4025 	SND_DJM_CTL("Input 4 Capture Switch", 750_cap4, 0, SND_DJM_WINDEX_CAP)
4026 };
4027 
4028 // DJM-850
4029 static const u16 snd_djm_opts_850_cap1[] = {
4030 	0x0100, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a, 0x010f };
4031 static const u16 snd_djm_opts_850_cap2[] = {
4032 	0x0200, 0x0201, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020f };
4033 static const u16 snd_djm_opts_850_cap3[] = {
4034 	0x0300, 0x0301, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030f };
4035 static const u16 snd_djm_opts_850_cap4[] = {
4036 	0x0400, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040f };
4037 
4038 static const struct snd_djm_ctl snd_djm_ctls_850[] = {
4039 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4040 	SND_DJM_CTL("Input 1 Capture Switch", 850_cap1, 1, SND_DJM_WINDEX_CAP),
4041 	SND_DJM_CTL("Input 2 Capture Switch", 850_cap2, 0, SND_DJM_WINDEX_CAP),
4042 	SND_DJM_CTL("Input 3 Capture Switch", 850_cap3, 0, SND_DJM_WINDEX_CAP),
4043 	SND_DJM_CTL("Input 4 Capture Switch", 850_cap4, 1, SND_DJM_WINDEX_CAP)
4044 };
4045 
4046 // DJM-900NXS2
4047 static const u16 snd_djm_opts_900nxs2_cap1[] = {
4048 	0x0100, 0x0102, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a };
4049 static const u16 snd_djm_opts_900nxs2_cap2[] = {
4050 	0x0200, 0x0202, 0x0203, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a };
4051 static const u16 snd_djm_opts_900nxs2_cap3[] = {
4052 	0x0300, 0x0302, 0x0303, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a };
4053 static const u16 snd_djm_opts_900nxs2_cap4[] = {
4054 	0x0400, 0x0402, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a };
4055 static const u16 snd_djm_opts_900nxs2_cap5[] = {
4056 	0x0507, 0x0508, 0x0509, 0x050a, 0x0511, 0x0512, 0x0513, 0x0514 };
4057 
4058 static const struct snd_djm_ctl snd_djm_ctls_900nxs2[] = {
4059 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4060 	SND_DJM_CTL("Input 1 Capture Switch", 900nxs2_cap1, 2, SND_DJM_WINDEX_CAP),
4061 	SND_DJM_CTL("Input 2 Capture Switch", 900nxs2_cap2, 2, SND_DJM_WINDEX_CAP),
4062 	SND_DJM_CTL("Input 3 Capture Switch", 900nxs2_cap3, 2, SND_DJM_WINDEX_CAP),
4063 	SND_DJM_CTL("Input 4 Capture Switch", 900nxs2_cap4, 2, SND_DJM_WINDEX_CAP),
4064 	SND_DJM_CTL("Input 5 Capture Switch", 900nxs2_cap5, 3, SND_DJM_WINDEX_CAP)
4065 };
4066 
4067 // DJM-750MK2
4068 static const u16 snd_djm_opts_750mk2_cap1[] = {
4069 	0x0100, 0x0102, 0x0103, 0x0106, 0x0107, 0x0108, 0x0109, 0x010a };
4070 static const u16 snd_djm_opts_750mk2_cap2[] = {
4071 	0x0200, 0x0202, 0x0203, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a };
4072 static const u16 snd_djm_opts_750mk2_cap3[] = {
4073 	0x0300, 0x0302, 0x0303, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a };
4074 static const u16 snd_djm_opts_750mk2_cap4[] = {
4075 	0x0400, 0x0402, 0x0403, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a };
4076 static const u16 snd_djm_opts_750mk2_cap5[] = {
4077 	0x0507, 0x0508, 0x0509, 0x050a, 0x0511, 0x0512, 0x0513, 0x0514 };
4078 
4079 static const u16 snd_djm_opts_750mk2_pb1[] = { 0x0100, 0x0101, 0x0104 };
4080 static const u16 snd_djm_opts_750mk2_pb2[] = { 0x0200, 0x0201, 0x0204 };
4081 static const u16 snd_djm_opts_750mk2_pb3[] = { 0x0300, 0x0301, 0x0304 };
4082 
4083 static const struct snd_djm_ctl snd_djm_ctls_750mk2[] = {
4084 	SND_DJM_CTL("Master Input Level Capture Switch", cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4085 	SND_DJM_CTL("Input 1 Capture Switch",   750mk2_cap1, 2, SND_DJM_WINDEX_CAP),
4086 	SND_DJM_CTL("Input 2 Capture Switch",   750mk2_cap2, 2, SND_DJM_WINDEX_CAP),
4087 	SND_DJM_CTL("Input 3 Capture Switch",   750mk2_cap3, 2, SND_DJM_WINDEX_CAP),
4088 	SND_DJM_CTL("Input 4 Capture Switch",   750mk2_cap4, 2, SND_DJM_WINDEX_CAP),
4089 	SND_DJM_CTL("Input 5 Capture Switch",   750mk2_cap5, 3, SND_DJM_WINDEX_CAP),
4090 	SND_DJM_CTL("Output 1 Playback Switch", 750mk2_pb1, 0, SND_DJM_WINDEX_PB),
4091 	SND_DJM_CTL("Output 2 Playback Switch", 750mk2_pb2, 1, SND_DJM_WINDEX_PB),
4092 	SND_DJM_CTL("Output 3 Playback Switch", 750mk2_pb3, 2, SND_DJM_WINDEX_PB)
4093 };
4094 
4095 // DJM-A9
4096 static const u16 snd_djm_opts_a9_cap_level[] = {
4097 	0x0000, 0x0100, 0x0200, 0x0300, 0x0400, 0x0500 };
4098 static const u16 snd_djm_opts_a9_cap1[] = {
4099 	0x0107, 0x0108, 0x0109, 0x010a, 0x010e,
4100 	0x111, 0x112, 0x113, 0x114, 0x0131, 0x132, 0x133, 0x134 };
4101 static const u16 snd_djm_opts_a9_cap2[] = {
4102 	0x0201, 0x0202, 0x0203, 0x0205, 0x0206, 0x0207, 0x0208, 0x0209, 0x020a, 0x020e };
4103 static const u16 snd_djm_opts_a9_cap3[] = {
4104 	0x0301, 0x0302, 0x0303, 0x0305, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030e };
4105 static const u16 snd_djm_opts_a9_cap4[] = {
4106 	0x0401, 0x0402, 0x0403, 0x0405, 0x0406, 0x0407, 0x0408, 0x0409, 0x040a, 0x040e };
4107 static const u16 snd_djm_opts_a9_cap5[] = {
4108 	0x0501, 0x0502, 0x0503, 0x0505, 0x0506, 0x0507, 0x0508, 0x0509, 0x050a, 0x050e };
4109 
4110 static const struct snd_djm_ctl snd_djm_ctls_a9[] = {
4111 	SND_DJM_CTL("Master Input Level Capture Switch", a9_cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4112 	SND_DJM_CTL("Master Input Capture Switch", a9_cap1, 3, SND_DJM_WINDEX_CAP),
4113 	SND_DJM_CTL("Input 1 Capture Switch",  a9_cap2, 2, SND_DJM_WINDEX_CAP),
4114 	SND_DJM_CTL("Input 2 Capture Switch",  a9_cap3, 2, SND_DJM_WINDEX_CAP),
4115 	SND_DJM_CTL("Input 3 Capture Switch",  a9_cap4, 2, SND_DJM_WINDEX_CAP),
4116 	SND_DJM_CTL("Input 4 Capture Switch",  a9_cap5, 2, SND_DJM_WINDEX_CAP)
4117 };
4118 
4119 // DJM-V10
4120 static const u16 snd_djm_opts_v10_cap_level[] = {
4121 	0x0000, 0x0100, 0x0200, 0x0300, 0x0400, 0x0500
4122 };
4123 
4124 static const u16 snd_djm_opts_v10_cap1[] = {
4125 	0x0103,
4126 	0x0100, 0x0102, 0x0106, 0x0110, 0x0107,
4127 	0x0108, 0x0109, 0x010a, 0x0121, 0x0122
4128 };
4129 
4130 static const u16 snd_djm_opts_v10_cap2[] = {
4131 	0x0200, 0x0202, 0x0206, 0x0210, 0x0207,
4132 	0x0208, 0x0209, 0x020a, 0x0221, 0x0222
4133 };
4134 
4135 static const u16 snd_djm_opts_v10_cap3[] = {
4136 	0x0303,
4137 	0x0300, 0x0302, 0x0306, 0x0310, 0x0307,
4138 	0x0308, 0x0309, 0x030a, 0x0321, 0x0322
4139 };
4140 
4141 static const u16 snd_djm_opts_v10_cap4[] = {
4142 	0x0403,
4143 	0x0400, 0x0402, 0x0406, 0x0410, 0x0407,
4144 	0x0408, 0x0409, 0x040a, 0x0421, 0x0422
4145 };
4146 
4147 static const u16 snd_djm_opts_v10_cap5[] = {
4148 	0x0500, 0x0502, 0x0506, 0x0510, 0x0507,
4149 	0x0508, 0x0509, 0x050a, 0x0521, 0x0522
4150 };
4151 
4152 static const u16 snd_djm_opts_v10_cap6[] = {
4153 	0x0603,
4154 	0x0600, 0x0602, 0x0606, 0x0610, 0x0607,
4155 	0x0608, 0x0609, 0x060a, 0x0621, 0x0622
4156 };
4157 
4158 static const struct snd_djm_ctl snd_djm_ctls_v10[] = {
4159 	SND_DJM_CTL("Master Input Level Capture Switch", v10_cap_level, 0, SND_DJM_WINDEX_CAPLVL),
4160 	SND_DJM_CTL("Input 1 Capture Switch", v10_cap1, 2, SND_DJM_WINDEX_CAP),
4161 	SND_DJM_CTL("Input 2 Capture Switch", v10_cap2, 2, SND_DJM_WINDEX_CAP),
4162 	SND_DJM_CTL("Input 3 Capture Switch", v10_cap3, 0, SND_DJM_WINDEX_CAP),
4163 	SND_DJM_CTL("Input 4 Capture Switch", v10_cap4, 0, SND_DJM_WINDEX_CAP),
4164 	SND_DJM_CTL("Input 5 Capture Switch", v10_cap5, 0, SND_DJM_WINDEX_CAP),
4165 	SND_DJM_CTL("Input 6 Capture Switch", v10_cap6, 0, SND_DJM_WINDEX_CAP)
4166 	// playback channels are fixed and controlled by hardware knobs on the mixer
4167 };
4168 
4169 static const struct snd_djm_device snd_djm_devices[] = {
4170 	[SND_DJM_250MK2_IDX] = SND_DJM_DEVICE(250mk2),
4171 	[SND_DJM_750_IDX] = SND_DJM_DEVICE(750),
4172 	[SND_DJM_850_IDX] = SND_DJM_DEVICE(850),
4173 	[SND_DJM_900NXS2_IDX] = SND_DJM_DEVICE(900nxs2),
4174 	[SND_DJM_750MK2_IDX] = SND_DJM_DEVICE(750mk2),
4175 	[SND_DJM_450_IDX] = SND_DJM_DEVICE(450),
4176 	[SND_DJM_A9_IDX] = SND_DJM_DEVICE(a9),
4177 	[SND_DJM_V10_IDX] = SND_DJM_DEVICE(v10),
4178 };
4179 
snd_djm_controls_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * info)4180 static int snd_djm_controls_info(struct snd_kcontrol *kctl,
4181 				 struct snd_ctl_elem_info *info)
4182 {
4183 	unsigned long private_value = kctl->private_value;
4184 	u8 device_idx = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
4185 	u8 ctl_idx = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
4186 	const struct snd_djm_device *device = &snd_djm_devices[device_idx];
4187 	const char *name;
4188 	const struct snd_djm_ctl *ctl;
4189 	size_t noptions;
4190 
4191 	if (ctl_idx >= device->ncontrols)
4192 		return -EINVAL;
4193 
4194 	ctl = &device->controls[ctl_idx];
4195 	noptions = ctl->noptions;
4196 	if (info->value.enumerated.item >= noptions)
4197 		info->value.enumerated.item = noptions - 1;
4198 
4199 	name = snd_djm_get_label(device_idx,
4200 				 ctl->options[info->value.enumerated.item],
4201 				 ctl->wIndex);
4202 	if (!name)
4203 		return -EINVAL;
4204 
4205 	strscpy(info->value.enumerated.name, name, sizeof(info->value.enumerated.name));
4206 	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4207 	info->count = 1;
4208 	info->value.enumerated.items = noptions;
4209 	return 0;
4210 }
4211 
snd_djm_controls_update(struct usb_mixer_interface * mixer,u8 device_idx,u8 group,u16 value)4212 static int snd_djm_controls_update(struct usb_mixer_interface *mixer,
4213 				   u8 device_idx, u8 group, u16 value)
4214 {
4215 	int err;
4216 	const struct snd_djm_device *device = &snd_djm_devices[device_idx];
4217 
4218 	if (group >= device->ncontrols || value >= device->controls[group].noptions)
4219 		return -EINVAL;
4220 
4221 	err = snd_usb_lock_shutdown(mixer->chip);
4222 	if (err)
4223 		return err;
4224 
4225 	err = snd_usb_ctl_msg(mixer->chip->dev,
4226 			      usb_sndctrlpipe(mixer->chip->dev, 0),
4227 			      USB_REQ_SET_FEATURE,
4228 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
4229 			      device->controls[group].options[value],
4230 			      device->controls[group].wIndex,
4231 			      NULL, 0);
4232 
4233 	snd_usb_unlock_shutdown(mixer->chip);
4234 	return err;
4235 }
4236 
snd_djm_controls_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * elem)4237 static int snd_djm_controls_get(struct snd_kcontrol *kctl,
4238 				struct snd_ctl_elem_value *elem)
4239 {
4240 	elem->value.enumerated.item[0] = kctl->private_value & SND_DJM_VALUE_MASK;
4241 	return 0;
4242 }
4243 
snd_djm_controls_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * elem)4244 static int snd_djm_controls_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *elem)
4245 {
4246 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
4247 	struct usb_mixer_interface *mixer = list->mixer;
4248 	unsigned long private_value = kctl->private_value;
4249 
4250 	u8 device = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
4251 	u8 group = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
4252 	u16 value = elem->value.enumerated.item[0];
4253 
4254 	kctl->private_value = (((unsigned long)device << SND_DJM_DEVICE_SHIFT) |
4255 			      (group << SND_DJM_GROUP_SHIFT) |
4256 			      value);
4257 
4258 	return snd_djm_controls_update(mixer, device, group, value);
4259 }
4260 
snd_djm_controls_resume(struct usb_mixer_elem_list * list)4261 static int snd_djm_controls_resume(struct usb_mixer_elem_list *list)
4262 {
4263 	unsigned long private_value = list->kctl->private_value;
4264 	u8 device = (private_value & SND_DJM_DEVICE_MASK) >> SND_DJM_DEVICE_SHIFT;
4265 	u8 group = (private_value & SND_DJM_GROUP_MASK) >> SND_DJM_GROUP_SHIFT;
4266 	u16 value = (private_value & SND_DJM_VALUE_MASK);
4267 
4268 	return snd_djm_controls_update(list->mixer, device, group, value);
4269 }
4270 
snd_djm_controls_create(struct usb_mixer_interface * mixer,const u8 device_idx)4271 static int snd_djm_controls_create(struct usb_mixer_interface *mixer,
4272 				   const u8 device_idx)
4273 {
4274 	int err, i;
4275 	u16 value;
4276 
4277 	const struct snd_djm_device *device = &snd_djm_devices[device_idx];
4278 
4279 	struct snd_kcontrol_new knew = {
4280 		.iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
4281 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
4282 		.index = 0,
4283 		.info = snd_djm_controls_info,
4284 		.get  = snd_djm_controls_get,
4285 		.put  = snd_djm_controls_put
4286 	};
4287 
4288 	for (i = 0; i < device->ncontrols; i++) {
4289 		value = device->controls[i].default_value;
4290 		knew.name = device->controls[i].name;
4291 		knew.private_value =
4292 			((unsigned long)device_idx << SND_DJM_DEVICE_SHIFT) |
4293 			(i << SND_DJM_GROUP_SHIFT) |
4294 			value;
4295 		err = snd_djm_controls_update(mixer, device_idx, i, value);
4296 		if (err)
4297 			return err;
4298 		err = add_single_ctl_with_resume(mixer, 0, snd_djm_controls_resume,
4299 						 &knew, NULL);
4300 		if (err)
4301 			return err;
4302 	}
4303 	return 0;
4304 }
4305 
snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface * mixer)4306 int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
4307 {
4308 	int err = 0;
4309 
4310 	err = snd_usb_soundblaster_remote_init(mixer);
4311 	if (err < 0)
4312 		return err;
4313 
4314 	switch (mixer->chip->usb_id) {
4315 	/* Tascam US-16x08 */
4316 	case USB_ID(0x0644, 0x8047):
4317 		err = snd_us16x08_controls_create(mixer);
4318 		break;
4319 	case USB_ID(0x041e, 0x3020):
4320 	case USB_ID(0x041e, 0x3040):
4321 	case USB_ID(0x041e, 0x3042):
4322 	case USB_ID(0x041e, 0x30df):
4323 	case USB_ID(0x041e, 0x3048):
4324 		err = snd_audigy2nx_controls_create(mixer);
4325 		if (err < 0)
4326 			break;
4327 		snd_card_ro_proc_new(mixer->chip->card, "audigy2nx",
4328 				     mixer, snd_audigy2nx_proc_read);
4329 		break;
4330 
4331 	/* EMU0204 */
4332 	case USB_ID(0x041e, 0x3f19):
4333 		err = snd_emu0204_controls_create(mixer);
4334 		break;
4335 
4336 #if IS_REACHABLE(CONFIG_INPUT)
4337 	case USB_ID(0x054c, 0x0ce6): /* Sony DualSense controller (PS5) */
4338 	case USB_ID(0x054c, 0x0df2): /* Sony DualSense Edge controller (PS5) */
4339 		err = snd_dualsense_controls_create(mixer);
4340 		break;
4341 #endif /* IS_REACHABLE(CONFIG_INPUT) */
4342 
4343 	case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
4344 	case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
4345 		err = snd_c400_create_mixer(mixer);
4346 		break;
4347 
4348 	case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
4349 	case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
4350 		err = snd_ftu_create_mixer(mixer);
4351 		break;
4352 
4353 	case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
4354 	case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
4355 	case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
4356 		err = snd_xonar_u1_controls_create(mixer);
4357 		break;
4358 
4359 	case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
4360 		err = snd_microii_controls_create(mixer);
4361 		break;
4362 
4363 	case USB_ID(0x0dba, 0x1000): /* Digidesign Mbox 1 */
4364 		err = snd_mbox1_controls_create(mixer);
4365 		break;
4366 
4367 	case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
4368 		err = snd_nativeinstruments_create_mixer(/* checkpatch hack */
4369 				mixer,
4370 				snd_nativeinstruments_ta6_mixers,
4371 				ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
4372 		break;
4373 
4374 	case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
4375 		err = snd_nativeinstruments_create_mixer(/* checkpatch hack */
4376 				mixer,
4377 				snd_nativeinstruments_ta10_mixers,
4378 				ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
4379 		break;
4380 
4381 	case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
4382 		/* detection is disabled in mixer_maps.c */
4383 		err = snd_create_std_mono_table(mixer, ebox44_table);
4384 		break;
4385 
4386 	case USB_ID(0x1235, 0x8012): /* Focusrite Scarlett 6i6 */
4387 	case USB_ID(0x1235, 0x8002): /* Focusrite Scarlett 8i6 */
4388 	case USB_ID(0x1235, 0x8004): /* Focusrite Scarlett 18i6 */
4389 	case USB_ID(0x1235, 0x8014): /* Focusrite Scarlett 18i8 */
4390 	case USB_ID(0x1235, 0x800c): /* Focusrite Scarlett 18i20 */
4391 		err = snd_scarlett_controls_create(mixer);
4392 		break;
4393 
4394 	case USB_ID(0x1235, 0x8203): /* Focusrite Scarlett 6i6 2nd Gen */
4395 	case USB_ID(0x1235, 0x8204): /* Focusrite Scarlett 18i8 2nd Gen */
4396 	case USB_ID(0x1235, 0x8201): /* Focusrite Scarlett 18i20 2nd Gen */
4397 	case USB_ID(0x1235, 0x8211): /* Focusrite Scarlett Solo 3rd Gen */
4398 	case USB_ID(0x1235, 0x8210): /* Focusrite Scarlett 2i2 3rd Gen */
4399 	case USB_ID(0x1235, 0x8212): /* Focusrite Scarlett 4i4 3rd Gen */
4400 	case USB_ID(0x1235, 0x8213): /* Focusrite Scarlett 8i6 3rd Gen */
4401 	case USB_ID(0x1235, 0x8214): /* Focusrite Scarlett 18i8 3rd Gen */
4402 	case USB_ID(0x1235, 0x8215): /* Focusrite Scarlett 18i20 3rd Gen */
4403 	case USB_ID(0x1235, 0x8216): /* Focusrite Vocaster One */
4404 	case USB_ID(0x1235, 0x8217): /* Focusrite Vocaster Two */
4405 	case USB_ID(0x1235, 0x8218): /* Focusrite Scarlett Solo 4th Gen */
4406 	case USB_ID(0x1235, 0x8219): /* Focusrite Scarlett 2i2 4th Gen */
4407 	case USB_ID(0x1235, 0x821a): /* Focusrite Scarlett 4i4 4th Gen */
4408 	case USB_ID(0x1235, 0x8206): /* Focusrite Clarett 2Pre USB */
4409 	case USB_ID(0x1235, 0x8207): /* Focusrite Clarett 4Pre USB */
4410 	case USB_ID(0x1235, 0x8208): /* Focusrite Clarett 8Pre USB */
4411 	case USB_ID(0x1235, 0x820a): /* Focusrite Clarett+ 2Pre */
4412 	case USB_ID(0x1235, 0x820b): /* Focusrite Clarett+ 4Pre */
4413 	case USB_ID(0x1235, 0x820c): /* Focusrite Clarett+ 8Pre */
4414 		err = snd_scarlett2_init(mixer);
4415 		break;
4416 
4417 	case USB_ID(0x1235, 0x821b): /* Focusrite Scarlett 16i16 4th Gen */
4418 	case USB_ID(0x1235, 0x821c): /* Focusrite Scarlett 18i16 4th Gen */
4419 	case USB_ID(0x1235, 0x821d): /* Focusrite Scarlett 18i20 4th Gen */
4420 		err = snd_fcp_init(mixer);
4421 		break;
4422 
4423 	case USB_ID(0x041e, 0x323b): /* Creative Sound Blaster E1 */
4424 		err = snd_soundblaster_e1_switch_create(mixer);
4425 		break;
4426 	case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
4427 		err = dell_dock_mixer_create(mixer);
4428 		if (err < 0)
4429 			break;
4430 		err = dell_dock_mixer_init(mixer);
4431 		break;
4432 	case USB_ID(0x0bda, 0x402e): /* Dell WD19 dock */
4433 		err = dell_dock_mixer_create(mixer);
4434 		break;
4435 
4436 	case USB_ID(0x2a39, 0x3fd2): /* RME ADI-2 Pro */
4437 	case USB_ID(0x2a39, 0x3fd3): /* RME ADI-2 DAC */
4438 	case USB_ID(0x2a39, 0x3fd4): /* RME */
4439 		err = snd_rme_controls_create(mixer);
4440 		break;
4441 
4442 	case USB_ID(0x194f, 0x010c): /* Presonus Studio 1810c */
4443 		err = snd_sc1810_init_mixer(mixer);
4444 		break;
4445 	case USB_ID(0x194f, 0x010d): /* Presonus Studio 1824c */
4446 		err = snd_sc1810_init_mixer(mixer);
4447 		break;
4448 	case USB_ID(0x2a39, 0x3fb0): /* RME Babyface Pro FS */
4449 		err = snd_bbfpro_controls_create(mixer);
4450 		break;
4451 	case USB_ID(0x2a39, 0x3f8c): /* RME Digiface USB */
4452 	case USB_ID(0x2a39, 0x3fa0): /* RME Digiface USB (alternate) */
4453 		err = snd_rme_digiface_controls_create(mixer);
4454 		break;
4455 	case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */
4456 		err = snd_djm_controls_create(mixer, SND_DJM_250MK2_IDX);
4457 		break;
4458 	case USB_ID(0x2b73, 0x0013): /* Pioneer DJ DJM-450 */
4459 		err = snd_djm_controls_create(mixer, SND_DJM_450_IDX);
4460 		break;
4461 	case USB_ID(0x08e4, 0x017f): /* Pioneer DJ DJM-750 */
4462 		err = snd_djm_controls_create(mixer, SND_DJM_750_IDX);
4463 		break;
4464 	case USB_ID(0x2b73, 0x001b): /* Pioneer DJ DJM-750MK2 */
4465 		err = snd_djm_controls_create(mixer, SND_DJM_750MK2_IDX);
4466 		break;
4467 	case USB_ID(0x08e4, 0x0163): /* Pioneer DJ DJM-850 */
4468 		err = snd_djm_controls_create(mixer, SND_DJM_850_IDX);
4469 		break;
4470 	case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */
4471 		err = snd_djm_controls_create(mixer, SND_DJM_900NXS2_IDX);
4472 		break;
4473 	case USB_ID(0x2b73, 0x003c): /* Pioneer DJ / AlphaTheta DJM-A9 */
4474 		err = snd_djm_controls_create(mixer, SND_DJM_A9_IDX);
4475 		break;
4476 	case USB_ID(0x2b73, 0x0034): /* Pioneer DJ DJM-V10 */
4477 		err = snd_djm_controls_create(mixer, SND_DJM_V10_IDX);
4478 		break;
4479 	}
4480 
4481 	return err;
4482 }
4483 
snd_usb_mixer_resume_quirk(struct usb_mixer_interface * mixer)4484 void snd_usb_mixer_resume_quirk(struct usb_mixer_interface *mixer)
4485 {
4486 	switch (mixer->chip->usb_id) {
4487 	case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
4488 		dell_dock_mixer_init(mixer);
4489 		break;
4490 	}
4491 }
4492 
snd_usb_mixer_rc_memory_change(struct usb_mixer_interface * mixer,int unitid)4493 void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
4494 				    int unitid)
4495 {
4496 	if (!mixer->rc_cfg)
4497 		return;
4498 	/* unit ids specific to Extigy/Audigy 2 NX: */
4499 	switch (unitid) {
4500 	case 0: /* remote control */
4501 		mixer->rc_urb->dev = mixer->chip->dev;
4502 		usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
4503 		break;
4504 	case 4: /* digital in jack */
4505 	case 7: /* line in jacks */
4506 	case 19: /* speaker out jacks */
4507 	case 20: /* headphones out jack */
4508 		break;
4509 	/* live24ext: 4 = line-in jack */
4510 	case 3:	/* hp-out jack (may actuate Mute) */
4511 		if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
4512 		    mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
4513 			snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
4514 		break;
4515 	default:
4516 		usb_audio_dbg(mixer->chip, "memory change in unknown unit %d\n", unitid);
4517 		break;
4518 	}
4519 }
4520 
snd_dragonfly_quirk_db_scale(struct usb_mixer_interface * mixer,struct usb_mixer_elem_info * cval,struct snd_kcontrol * kctl)4521 static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
4522 					 struct usb_mixer_elem_info *cval,
4523 					 struct snd_kcontrol *kctl)
4524 {
4525 	/* Approximation using 10 ranges based on output measurement on hw v1.2.
4526 	 * This seems close to the cubic mapping e.g. alsamixer uses.
4527 	 */
4528 	static const DECLARE_TLV_DB_RANGE(scale,
4529 		 0,  1, TLV_DB_MINMAX_ITEM(-5300, -4970),
4530 		 2,  5, TLV_DB_MINMAX_ITEM(-4710, -4160),
4531 		 6,  7, TLV_DB_MINMAX_ITEM(-3884, -3710),
4532 		 8, 14, TLV_DB_MINMAX_ITEM(-3443, -2560),
4533 		15, 16, TLV_DB_MINMAX_ITEM(-2475, -2324),
4534 		17, 19, TLV_DB_MINMAX_ITEM(-2228, -2031),
4535 		20, 26, TLV_DB_MINMAX_ITEM(-1910, -1393),
4536 		27, 31, TLV_DB_MINMAX_ITEM(-1322, -1032),
4537 		32, 40, TLV_DB_MINMAX_ITEM(-968, -490),
4538 		41, 50, TLV_DB_MINMAX_ITEM(-441, 0),
4539 	);
4540 
4541 	if (cval->min == 0 && cval->max == 50) {
4542 		usb_audio_info(mixer->chip, "applying DragonFly dB scale quirk (0-50 variant)\n");
4543 		kctl->tlv.p = scale;
4544 		kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
4545 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
4546 
4547 	} else if (cval->min == 0 && cval->max <= 1000) {
4548 		/* Some other clearly broken DragonFly variant.
4549 		 * At least a 0..53 variant (hw v1.0) exists.
4550 		 */
4551 		usb_audio_info(mixer->chip, "ignoring too narrow dB range on a DragonFly device");
4552 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
4553 	}
4554 }
4555 
4556 /*
4557  * Some Plantronics headsets have control names that don't meet ALSA naming
4558  * standards. This function fixes nonstandard source names. By the time
4559  * this function is called the control name should look like one of these:
4560  * "source names Playback Volume"
4561  * "source names Playback Switch"
4562  * "source names Capture Volume"
4563  * "source names Capture Switch"
4564  * If any of the trigger words are found in the name then the name will
4565  * be changed to:
4566  * "Headset Playback Volume"
4567  * "Headset Playback Switch"
4568  * "Headset Capture Volume"
4569  * "Headset Capture Switch"
4570  * depending on the current suffix.
4571  */
snd_fix_plt_name(struct snd_usb_audio * chip,struct snd_ctl_elem_id * id)4572 static void snd_fix_plt_name(struct snd_usb_audio *chip,
4573 			     struct snd_ctl_elem_id *id)
4574 {
4575 	/* no variant of "Sidetone" should be added to this list */
4576 	static const char * const trigger[] = {
4577 		"Earphone", "Microphone", "Receive", "Transmit"
4578 	};
4579 	static const char * const suffix[] = {
4580 		" Playback Volume", " Playback Switch",
4581 		" Capture Volume", " Capture Switch"
4582 	};
4583 	int i;
4584 
4585 	for (i = 0; i < ARRAY_SIZE(trigger); i++)
4586 		if (strstr(id->name, trigger[i]))
4587 			goto triggered;
4588 	usb_audio_dbg(chip, "no change in %s\n", id->name);
4589 	return;
4590 
4591 triggered:
4592 	for (i = 0; i < ARRAY_SIZE(suffix); i++)
4593 		if (strstr(id->name, suffix[i])) {
4594 			usb_audio_dbg(chip, "fixing kctl name %s\n", id->name);
4595 			snprintf(id->name, sizeof(id->name), "Headset%s",
4596 				 suffix[i]);
4597 			return;
4598 		}
4599 	usb_audio_dbg(chip, "something wrong in kctl name %s\n", id->name);
4600 }
4601 
snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface * mixer,struct usb_mixer_elem_info * cval,int unitid,struct snd_kcontrol * kctl)4602 void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
4603 				  struct usb_mixer_elem_info *cval, int unitid,
4604 				  struct snd_kcontrol *kctl)
4605 {
4606 	switch (mixer->chip->usb_id) {
4607 	case USB_ID(0x21b4, 0x0081): /* AudioQuest DragonFly */
4608 		if (unitid == 7 && cval->control == UAC_FU_VOLUME)
4609 			snd_dragonfly_quirk_db_scale(mixer, cval, kctl);
4610 		break;
4611 	/* lowest playback value is muted on some devices */
4612 	case USB_ID(0x0d8c, 0x000c): /* C-Media */
4613 	case USB_ID(0x0d8c, 0x0014): /* C-Media */
4614 	case USB_ID(0x19f7, 0x0003): /* RODE NT-USB */
4615 		if (strstr(kctl->id.name, "Playback"))
4616 			cval->min_mute = 1;
4617 		break;
4618 	}
4619 
4620 	/* ALSA-ify some Plantronics headset control names */
4621 	if (USB_ID_VENDOR(mixer->chip->usb_id) == 0x047f &&
4622 	    (cval->control == UAC_FU_MUTE || cval->control == UAC_FU_VOLUME))
4623 		snd_fix_plt_name(mixer->chip, &kctl->id);
4624 }
4625