blob: c237e24f08d9bbf51f7b7ce16f5ddb4ffe92a9a0 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Daniel Mack7b1eda22010-03-11 21:13:22 +01002/*
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 *
Przemek Rudy066624c2013-06-27 23:52:33 +020013 * Audio Advantage Micro II support added by:
14 * Przemek Rudy (prudy1@o2.pl)
Daniel Mack7b1eda22010-03-11 21:13:22 +010015 */
16
Ian Douglas Scott388fdb82018-01-16 15:34:50 -080017#include <linux/hid.h>
Daniel Mack7b1eda22010-03-11 21:13:22 +010018#include <linux/init.h>
Jussi Laakod39f1d62018-10-05 11:00:04 +030019#include <linux/math64.h>
Stephen Rothwell36db0452010-03-29 16:02:50 +110020#include <linux/slab.h>
Daniel Mack7b1eda22010-03-11 21:13:22 +010021#include <linux/usb.h>
22#include <linux/usb/audio.h>
23
Przemek Rudy066624c2013-06-27 23:52:33 +020024#include <sound/asoundef.h>
Daniel Mack7b1eda22010-03-11 21:13:22 +010025#include <sound/core.h>
26#include <sound/control.h>
27#include <sound/hwdep.h>
28#include <sound/info.h>
Anssi Hannula42e31212015-12-13 20:49:58 +020029#include <sound/tlv.h>
Daniel Mack7b1eda22010-03-11 21:13:22 +010030
31#include "usbaudio.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010032#include "mixer.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010033#include "mixer_quirks.h"
Chris J Arges76b188c2014-11-12 12:07:02 -060034#include "mixer_scarlett.h"
Geoffrey D. Bennett9e4d5c12019-07-29 00:42:45 +093035#include "mixer_scarlett_gen2.h"
Detlef Urband2bb390a2017-02-20 09:47:59 +010036#include "mixer_us16x08.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010037#include "helper.h"
38
Mark Hillsb71dad182012-06-09 13:16:38 +010039struct std_mono_table {
40 unsigned int unitid, control, cmask;
41 int val_type;
42 const char *name;
43 snd_kcontrol_tlv_rw_t *tlv_callback;
44};
45
Felix Homann8a4d1d32012-04-23 20:24:23 +020046/* This function allows for the creation of standard UAC controls.
47 * See the quirks for M-Audio FTUs or Ebox-44.
48 * If you don't want to set a TLV callback pass NULL.
49 *
50 * Since there doesn't seem to be a devices that needs a multichannel
51 * version, we keep it mono for simplicity.
52 */
Eldad Zack9f814102012-11-28 23:55:35 +010053static int snd_create_std_mono_ctl_offset(struct usb_mixer_interface *mixer,
Felix Homann8a4d1d32012-04-23 20:24:23 +020054 unsigned int unitid,
55 unsigned int control,
56 unsigned int cmask,
57 int val_type,
Eldad Zack9f814102012-11-28 23:55:35 +010058 unsigned int idx_off,
Felix Homann8a4d1d32012-04-23 20:24:23 +020059 const char *name,
60 snd_kcontrol_tlv_rw_t *tlv_callback)
61{
Felix Homann8a4d1d32012-04-23 20:24:23 +020062 struct usb_mixer_elem_info *cval;
63 struct snd_kcontrol *kctl;
64
65 cval = kzalloc(sizeof(*cval), GFP_KERNEL);
66 if (!cval)
67 return -ENOMEM;
68
Takashi Iwai3360b842014-11-18 11:47:04 +010069 snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
Felix Homann8a4d1d32012-04-23 20:24:23 +020070 cval->val_type = val_type;
71 cval->channels = 1;
72 cval->control = control;
73 cval->cmask = cmask;
Eldad Zack9f814102012-11-28 23:55:35 +010074 cval->idx_off = idx_off;
Felix Homann8a4d1d32012-04-23 20:24:23 +020075
Mark Hills7df4a692012-05-11 18:31:55 +010076 /* get_min_max() is called only for integer volumes later,
77 * so provide a short-cut for booleans */
Felix Homann8a4d1d32012-04-23 20:24:23 +020078 cval->min = 0;
79 cval->max = 1;
80 cval->res = 0;
81 cval->dBmin = 0;
82 cval->dBmax = 0;
83
84 /* Create control */
85 kctl = snd_ctl_new1(snd_usb_feature_unit_ctl, cval);
86 if (!kctl) {
87 kfree(cval);
88 return -ENOMEM;
89 }
90
91 /* Set name */
92 snprintf(kctl->id.name, sizeof(kctl->id.name), name);
Chris J Argeseef90452014-11-12 12:07:01 -060093 kctl->private_free = snd_usb_mixer_elem_free;
Felix Homann8a4d1d32012-04-23 20:24:23 +020094
95 /* set TLV */
96 if (tlv_callback) {
97 kctl->tlv.c = tlv_callback;
98 kctl->vd[0].access |=
99 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
100 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
101 }
102 /* Add control to mixer */
Takashi Iwai3360b842014-11-18 11:47:04 +0100103 return snd_usb_mixer_add_control(&cval->head, kctl);
Felix Homann8a4d1d32012-04-23 20:24:23 +0200104}
105
Eldad Zack9f814102012-11-28 23:55:35 +0100106static int snd_create_std_mono_ctl(struct usb_mixer_interface *mixer,
107 unsigned int unitid,
108 unsigned int control,
109 unsigned int cmask,
110 int val_type,
111 const char *name,
112 snd_kcontrol_tlv_rw_t *tlv_callback)
113{
114 return snd_create_std_mono_ctl_offset(mixer, unitid, control, cmask,
115 val_type, 0 /* Offset */, name, tlv_callback);
116}
117
Daniel Mack7b1eda22010-03-11 21:13:22 +0100118/*
Mark Hillsb71dad182012-06-09 13:16:38 +0100119 * Create a set of standard UAC controls from a table
120 */
121static int snd_create_std_mono_table(struct usb_mixer_interface *mixer,
Takashi Iwaia01df922020-01-05 15:47:26 +0100122 const struct std_mono_table *t)
Mark Hillsb71dad182012-06-09 13:16:38 +0100123{
124 int err;
125
126 while (t->name != NULL) {
127 err = snd_create_std_mono_ctl(mixer, t->unitid, t->control,
128 t->cmask, t->val_type, t->name, t->tlv_callback);
129 if (err < 0)
130 return err;
131 t++;
132 }
133
134 return 0;
135}
136
Takashi Iwai9cf36892014-11-18 12:58:51 +0100137static int add_single_ctl_with_resume(struct usb_mixer_interface *mixer,
138 int id,
139 usb_mixer_elem_resume_func_t resume,
140 const struct snd_kcontrol_new *knew,
141 struct usb_mixer_elem_list **listp)
142{
143 struct usb_mixer_elem_list *list;
144 struct snd_kcontrol *kctl;
145
146 list = kzalloc(sizeof(*list), GFP_KERNEL);
147 if (!list)
148 return -ENOMEM;
149 if (listp)
150 *listp = list;
151 list->mixer = mixer;
152 list->id = id;
153 list->resume = resume;
154 kctl = snd_ctl_new1(knew, list);
155 if (!kctl) {
156 kfree(list);
157 return -ENOMEM;
158 }
159 kctl->private_free = snd_usb_mixer_elem_free;
160 return snd_usb_mixer_add_control(list, kctl);
161}
162
Mark Hillsb71dad182012-06-09 13:16:38 +0100163/*
Daniel Mack7b1eda22010-03-11 21:13:22 +0100164 * Sound Blaster remote control configuration
165 *
166 * format of remote control data:
167 * Extigy: xx 00
168 * Audigy 2 NX: 06 80 xx 00 00 00
169 * Live! 24-bit: 06 80 xx yy 22 83
170 */
171static const struct rc_config {
172 u32 usb_id;
173 u8 offset;
174 u8 length;
175 u8 packet_length;
176 u8 min_packet_length; /* minimum accepted length of the URB result */
177 u8 mute_mixer_id;
178 u32 mute_code;
179} rc_configs[] = {
180 { USB_ID(0x041e, 0x3000), 0, 1, 2, 1, 18, 0x0013 }, /* Extigy */
181 { USB_ID(0x041e, 0x3020), 2, 1, 6, 6, 18, 0x0013 }, /* Audigy 2 NX */
182 { USB_ID(0x041e, 0x3040), 2, 2, 6, 6, 2, 0x6e91 }, /* Live! 24-bit */
Mandar Joshica8dc342010-11-02 14:43:19 +0000183 { USB_ID(0x041e, 0x3042), 0, 1, 1, 1, 1, 0x000d }, /* Usb X-Fi S51 */
Mathieu Bouffard7cdd8d72011-05-18 17:09:17 +0200184 { USB_ID(0x041e, 0x30df), 0, 1, 1, 1, 1, 0x000d }, /* Usb X-Fi S51 Pro */
Dmitry M. Fedin3dc85232015-04-09 17:37:03 +0300185 { USB_ID(0x041e, 0x3237), 0, 1, 1, 1, 1, 0x000d }, /* Usb X-Fi S51 Pro */
Daniel Mack7b1eda22010-03-11 21:13:22 +0100186 { USB_ID(0x041e, 0x3048), 2, 2, 6, 6, 2, 0x6e91 }, /* Toshiba SB0500 */
187};
188
189static void snd_usb_soundblaster_remote_complete(struct urb *urb)
190{
191 struct usb_mixer_interface *mixer = urb->context;
192 const struct rc_config *rc = mixer->rc_cfg;
193 u32 code;
194
195 if (urb->status < 0 || urb->actual_length < rc->min_packet_length)
196 return;
197
198 code = mixer->rc_buffer[rc->offset];
199 if (rc->length == 2)
200 code |= mixer->rc_buffer[rc->offset + 1] << 8;
201
202 /* the Mute button actually changes the mixer control */
203 if (code == rc->mute_code)
204 snd_usb_mixer_notify_id(mixer, rc->mute_mixer_id);
205 mixer->rc_code = code;
206 wmb();
207 wake_up(&mixer->rc_waitq);
208}
209
210static long snd_usb_sbrc_hwdep_read(struct snd_hwdep *hw, char __user *buf,
211 long count, loff_t *offset)
212{
213 struct usb_mixer_interface *mixer = hw->private_data;
214 int err;
215 u32 rc_code;
216
217 if (count != 1 && count != 4)
218 return -EINVAL;
219 err = wait_event_interruptible(mixer->rc_waitq,
220 (rc_code = xchg(&mixer->rc_code, 0)) != 0);
221 if (err == 0) {
222 if (count == 1)
223 err = put_user(rc_code, buf);
224 else
225 err = put_user(rc_code, (u32 __user *)buf);
226 }
227 return err < 0 ? err : count;
228}
229
Al Viro680ef722017-07-02 23:27:36 -0400230static __poll_t snd_usb_sbrc_hwdep_poll(struct snd_hwdep *hw, struct file *file,
Daniel Mack7b1eda22010-03-11 21:13:22 +0100231 poll_table *wait)
232{
233 struct usb_mixer_interface *mixer = hw->private_data;
234
235 poll_wait(file, &mixer->rc_waitq, wait);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800236 return mixer->rc_code ? EPOLLIN | EPOLLRDNORM : 0;
Daniel Mack7b1eda22010-03-11 21:13:22 +0100237}
238
239static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
240{
241 struct snd_hwdep *hwdep;
242 int err, len, i;
243
244 for (i = 0; i < ARRAY_SIZE(rc_configs); ++i)
245 if (rc_configs[i].usb_id == mixer->chip->usb_id)
246 break;
247 if (i >= ARRAY_SIZE(rc_configs))
248 return 0;
249 mixer->rc_cfg = &rc_configs[i];
250
251 len = mixer->rc_cfg->packet_length;
252
253 init_waitqueue_head(&mixer->rc_waitq);
254 err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
255 if (err < 0)
256 return err;
257 snprintf(hwdep->name, sizeof(hwdep->name),
258 "%s remote control", mixer->chip->card->shortname);
259 hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
260 hwdep->private_data = mixer;
261 hwdep->ops.read = snd_usb_sbrc_hwdep_read;
262 hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
263 hwdep->exclusive = 1;
264
265 mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
266 if (!mixer->rc_urb)
267 return -ENOMEM;
268 mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
269 if (!mixer->rc_setup_packet) {
270 usb_free_urb(mixer->rc_urb);
271 mixer->rc_urb = NULL;
272 return -ENOMEM;
273 }
274 mixer->rc_setup_packet->bRequestType =
275 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
276 mixer->rc_setup_packet->bRequest = UAC_GET_MEM;
277 mixer->rc_setup_packet->wValue = cpu_to_le16(0);
278 mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
279 mixer->rc_setup_packet->wLength = cpu_to_le16(len);
280 usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
281 usb_rcvctrlpipe(mixer->chip->dev, 0),
282 (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
283 snd_usb_soundblaster_remote_complete, mixer);
284 return 0;
285}
286
287#define snd_audigy2nx_led_info snd_ctl_boolean_mono_info
288
289static int snd_audigy2nx_led_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
290{
Takashi Iwai9cf36892014-11-18 12:58:51 +0100291 ucontrol->value.integer.value[0] = kcontrol->private_value >> 8;
Daniel Mack7b1eda22010-03-11 21:13:22 +0100292 return 0;
293}
294
Takashi Iwai9cf36892014-11-18 12:58:51 +0100295static int snd_audigy2nx_led_update(struct usb_mixer_interface *mixer,
296 int value, int index)
Daniel Mack7b1eda22010-03-11 21:13:22 +0100297{
Takashi Iwai9cf36892014-11-18 12:58:51 +0100298 struct snd_usb_audio *chip = mixer->chip;
299 int err;
Daniel Mack7b1eda22010-03-11 21:13:22 +0100300
Takashi Iwai47ab1542015-08-25 16:09:00 +0200301 err = snd_usb_lock_shutdown(chip);
302 if (err < 0)
303 return err;
304
Takashi Iwai9cf36892014-11-18 12:58:51 +0100305 if (chip->usb_id == USB_ID(0x041e, 0x3042))
306 err = snd_usb_ctl_msg(chip->dev,
307 usb_sndctrlpipe(chip->dev, 0), 0x24,
Mandar Joshica8dc342010-11-02 14:43:19 +0000308 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200309 !value, 0, NULL, 0);
Mathieu Bouffard7cdd8d72011-05-18 17:09:17 +0200310 /* USB X-Fi S51 Pro */
Takashi Iwai9cf36892014-11-18 12:58:51 +0100311 if (chip->usb_id == USB_ID(0x041e, 0x30df))
312 err = snd_usb_ctl_msg(chip->dev,
313 usb_sndctrlpipe(chip->dev, 0), 0x24,
Mathieu Bouffard7cdd8d72011-05-18 17:09:17 +0200314 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200315 !value, 0, NULL, 0);
Mandar Joshica8dc342010-11-02 14:43:19 +0000316 else
Takashi Iwai9cf36892014-11-18 12:58:51 +0100317 err = snd_usb_ctl_msg(chip->dev,
318 usb_sndctrlpipe(chip->dev, 0), 0x24,
Daniel Mack7b1eda22010-03-11 21:13:22 +0100319 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200320 value, index + 2, NULL, 0);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200321 snd_usb_unlock_shutdown(chip);
Takashi Iwai9cf36892014-11-18 12:58:51 +0100322 return err;
Daniel Mack7b1eda22010-03-11 21:13:22 +0100323}
324
Takashi Iwai9cf36892014-11-18 12:58:51 +0100325static int snd_audigy2nx_led_put(struct snd_kcontrol *kcontrol,
326 struct snd_ctl_elem_value *ucontrol)
327{
328 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
329 struct usb_mixer_interface *mixer = list->mixer;
330 int index = kcontrol->private_value & 0xff;
Dan Carpentere87359e2015-09-28 13:06:20 +0300331 unsigned int value = ucontrol->value.integer.value[0];
Takashi Iwai9cf36892014-11-18 12:58:51 +0100332 int old_value = kcontrol->private_value >> 8;
333 int err;
334
335 if (value > 1)
336 return -EINVAL;
337 if (value == old_value)
338 return 0;
339 kcontrol->private_value = (value << 8) | index;
340 err = snd_audigy2nx_led_update(mixer, value, index);
341 return err < 0 ? err : 1;
342}
343
344static int snd_audigy2nx_led_resume(struct usb_mixer_elem_list *list)
345{
346 int priv_value = list->kctl->private_value;
347
348 return snd_audigy2nx_led_update(list->mixer, priv_value >> 8,
349 priv_value & 0xff);
350}
351
352/* name and private_value are set dynamically */
Bhumika Goyal905e46a2017-05-27 20:16:15 +0530353static const struct snd_kcontrol_new snd_audigy2nx_control = {
Takashi Iwai9cf36892014-11-18 12:58:51 +0100354 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
355 .info = snd_audigy2nx_led_info,
356 .get = snd_audigy2nx_led_get,
357 .put = snd_audigy2nx_led_put,
358};
359
360static const char * const snd_audigy2nx_led_names[] = {
361 "CMSS LED Switch",
362 "Power LED Switch",
363 "Dolby Digital LED Switch",
Daniel Mack7b1eda22010-03-11 21:13:22 +0100364};
365
366static int snd_audigy2nx_controls_create(struct usb_mixer_interface *mixer)
367{
368 int i, err;
369
Takashi Iwai9cf36892014-11-18 12:58:51 +0100370 for (i = 0; i < ARRAY_SIZE(snd_audigy2nx_led_names); ++i) {
371 struct snd_kcontrol_new knew;
372
Mandar Joshica8dc342010-11-02 14:43:19 +0000373 /* USB X-Fi S51 doesn't have a CMSS LED */
374 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x3042)) && i == 0)
375 continue;
Mathieu Bouffard7cdd8d72011-05-18 17:09:17 +0200376 /* USB X-Fi S51 Pro doesn't have one either */
377 if ((mixer->chip->usb_id == USB_ID(0x041e, 0x30df)) && i == 0)
378 continue;
Daniel Mack7b1eda22010-03-11 21:13:22 +0100379 if (i > 1 && /* Live24ext has 2 LEDs only */
380 (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
Mandar Joshica8dc342010-11-02 14:43:19 +0000381 mixer->chip->usb_id == USB_ID(0x041e, 0x3042) ||
Mathieu Bouffard7cdd8d72011-05-18 17:09:17 +0200382 mixer->chip->usb_id == USB_ID(0x041e, 0x30df) ||
Daniel Mack7b1eda22010-03-11 21:13:22 +0100383 mixer->chip->usb_id == USB_ID(0x041e, 0x3048)))
384 break;
Takashi Iwai9cf36892014-11-18 12:58:51 +0100385
386 knew = snd_audigy2nx_control;
387 knew.name = snd_audigy2nx_led_names[i];
388 knew.private_value = (1 << 8) | i; /* LED on as default */
389 err = add_single_ctl_with_resume(mixer, 0,
390 snd_audigy2nx_led_resume,
391 &knew, NULL);
Daniel Mack7b1eda22010-03-11 21:13:22 +0100392 if (err < 0)
393 return err;
394 }
Daniel Mack7b1eda22010-03-11 21:13:22 +0100395 return 0;
396}
397
398static void snd_audigy2nx_proc_read(struct snd_info_entry *entry,
399 struct snd_info_buffer *buffer)
400{
401 static const struct sb_jack {
402 int unitid;
403 const char *name;
404 } jacks_audigy2nx[] = {
405 {4, "dig in "},
406 {7, "line in"},
407 {19, "spk out"},
408 {20, "hph out"},
409 {-1, NULL}
410 }, jacks_live24ext[] = {
411 {4, "line in"}, /* &1=Line, &2=Mic*/
412 {3, "hph out"}, /* headphones */
413 {0, "RC "}, /* last command, 6 bytes see rc_config above */
414 {-1, NULL}
415 };
416 const struct sb_jack *jacks;
417 struct usb_mixer_interface *mixer = entry->private_data;
418 int i, err;
419 u8 buf[3];
420
421 snd_iprintf(buffer, "%s jacks\n\n", mixer->chip->card->shortname);
422 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3020))
423 jacks = jacks_audigy2nx;
424 else if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
425 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
426 jacks = jacks_live24ext;
427 else
428 return;
429
430 for (i = 0; jacks[i].name; ++i) {
431 snd_iprintf(buffer, "%s: ", jacks[i].name);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200432 err = snd_usb_lock_shutdown(mixer->chip);
433 if (err < 0)
434 return;
435 err = snd_usb_ctl_msg(mixer->chip->dev,
Daniel Mack7b1eda22010-03-11 21:13:22 +0100436 usb_rcvctrlpipe(mixer->chip->dev, 0),
437 UAC_GET_MEM, USB_DIR_IN | USB_TYPE_CLASS |
438 USB_RECIP_INTERFACE, 0,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200439 jacks[i].unitid << 8, buf, 3);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200440 snd_usb_unlock_shutdown(mixer->chip);
Daniel Mack7b1eda22010-03-11 21:13:22 +0100441 if (err == 3 && (buf[0] == 3 || buf[0] == 6))
442 snd_iprintf(buffer, "%02x %02x\n", buf[1], buf[2]);
443 else
444 snd_iprintf(buffer, "?\n");
445 }
446}
447
Vasily Khoruzhick44832a72013-11-13 13:13:35 +0300448/* EMU0204 */
449static int snd_emu0204_ch_switch_info(struct snd_kcontrol *kcontrol,
450 struct snd_ctl_elem_info *uinfo)
451{
Takashi Iwai7bbd03e2014-10-20 18:21:42 +0200452 static const char * const texts[2] = {"1/2", "3/4"};
Vasily Khoruzhick44832a72013-11-13 13:13:35 +0300453
Takashi Iwai7bbd03e2014-10-20 18:21:42 +0200454 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
Vasily Khoruzhick44832a72013-11-13 13:13:35 +0300455}
456
457static int snd_emu0204_ch_switch_get(struct snd_kcontrol *kcontrol,
458 struct snd_ctl_elem_value *ucontrol)
459{
460 ucontrol->value.enumerated.item[0] = kcontrol->private_value;
461 return 0;
462}
463
Takashi Iwai5f503ee2014-11-18 16:11:37 +0100464static int snd_emu0204_ch_switch_update(struct usb_mixer_interface *mixer,
465 int value)
Vasily Khoruzhick44832a72013-11-13 13:13:35 +0300466{
Takashi Iwai5f503ee2014-11-18 16:11:37 +0100467 struct snd_usb_audio *chip = mixer->chip;
468 int err;
Vasily Khoruzhick44832a72013-11-13 13:13:35 +0300469 unsigned char buf[2];
470
Takashi Iwai47ab1542015-08-25 16:09:00 +0200471 err = snd_usb_lock_shutdown(chip);
472 if (err < 0)
473 return err;
Takashi Iwai5f503ee2014-11-18 16:11:37 +0100474
475 buf[0] = 0x01;
476 buf[1] = value ? 0x02 : 0x01;
477 err = snd_usb_ctl_msg(chip->dev,
478 usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
Vasily Khoruzhick44832a72013-11-13 13:13:35 +0300479 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
480 0x0400, 0x0e00, buf, 2);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200481 snd_usb_unlock_shutdown(chip);
Takashi Iwai5f503ee2014-11-18 16:11:37 +0100482 return err;
Vasily Khoruzhick44832a72013-11-13 13:13:35 +0300483}
484
Takashi Iwai5f503ee2014-11-18 16:11:37 +0100485static int snd_emu0204_ch_switch_put(struct snd_kcontrol *kcontrol,
486 struct snd_ctl_elem_value *ucontrol)
487{
488 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
489 struct usb_mixer_interface *mixer = list->mixer;
490 unsigned int value = ucontrol->value.enumerated.item[0];
491 int err;
Vasily Khoruzhick44832a72013-11-13 13:13:35 +0300492
Takashi Iwai5f503ee2014-11-18 16:11:37 +0100493 if (value > 1)
494 return -EINVAL;
495
496 if (value == kcontrol->private_value)
497 return 0;
498
499 kcontrol->private_value = value;
500 err = snd_emu0204_ch_switch_update(mixer, value);
501 return err < 0 ? err : 1;
502}
503
504static int snd_emu0204_ch_switch_resume(struct usb_mixer_elem_list *list)
505{
506 return snd_emu0204_ch_switch_update(list->mixer,
507 list->kctl->private_value);
508}
509
Takashi Iwai195727e2020-01-03 09:16:57 +0100510static const struct snd_kcontrol_new snd_emu0204_control = {
Takashi Iwai5f503ee2014-11-18 16:11:37 +0100511 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
512 .name = "Front Jack Channels",
513 .info = snd_emu0204_ch_switch_info,
514 .get = snd_emu0204_ch_switch_get,
515 .put = snd_emu0204_ch_switch_put,
516 .private_value = 0,
Vasily Khoruzhick44832a72013-11-13 13:13:35 +0300517};
518
519static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
520{
Takashi Iwai5f503ee2014-11-18 16:11:37 +0100521 return add_single_ctl_with_resume(mixer, 0,
522 snd_emu0204_ch_switch_resume,
523 &snd_emu0204_control, NULL);
Vasily Khoruzhick44832a72013-11-13 13:13:35 +0300524}
Takashi Iwai5f503ee2014-11-18 16:11:37 +0100525
Denis Washington1d31aff2012-12-11 11:38:32 +0100526/* ASUS Xonar U1 / U3 controls */
527
Daniel Mack7b1eda22010-03-11 21:13:22 +0100528static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
529 struct snd_ctl_elem_value *ucontrol)
530{
Takashi Iwai2bfb14c2014-11-18 16:18:15 +0100531 ucontrol->value.integer.value[0] = !!(kcontrol->private_value & 0x02);
Daniel Mack7b1eda22010-03-11 21:13:22 +0100532 return 0;
533}
534
Takashi Iwai2bfb14c2014-11-18 16:18:15 +0100535static int snd_xonar_u1_switch_update(struct usb_mixer_interface *mixer,
536 unsigned char status)
537{
538 struct snd_usb_audio *chip = mixer->chip;
539 int err;
540
Takashi Iwai47ab1542015-08-25 16:09:00 +0200541 err = snd_usb_lock_shutdown(chip);
542 if (err < 0)
543 return err;
544 err = snd_usb_ctl_msg(chip->dev,
Takashi Iwai2bfb14c2014-11-18 16:18:15 +0100545 usb_sndctrlpipe(chip->dev, 0), 0x08,
546 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
547 50, 0, &status, 1);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200548 snd_usb_unlock_shutdown(chip);
Takashi Iwai2bfb14c2014-11-18 16:18:15 +0100549 return err;
550}
551
Daniel Mack7b1eda22010-03-11 21:13:22 +0100552static int snd_xonar_u1_switch_put(struct snd_kcontrol *kcontrol,
553 struct snd_ctl_elem_value *ucontrol)
554{
Takashi Iwai2bfb14c2014-11-18 16:18:15 +0100555 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
Daniel Mack7b1eda22010-03-11 21:13:22 +0100556 u8 old_status, new_status;
Takashi Iwai2bfb14c2014-11-18 16:18:15 +0100557 int err;
Daniel Mack7b1eda22010-03-11 21:13:22 +0100558
Takashi Iwai2bfb14c2014-11-18 16:18:15 +0100559 old_status = kcontrol->private_value;
Daniel Mack7b1eda22010-03-11 21:13:22 +0100560 if (ucontrol->value.integer.value[0])
561 new_status = old_status | 0x02;
562 else
563 new_status = old_status & ~0x02;
Takashi Iwai2bfb14c2014-11-18 16:18:15 +0100564 if (new_status == old_status)
565 return 0;
566
567 kcontrol->private_value = new_status;
568 err = snd_xonar_u1_switch_update(list->mixer, new_status);
569 return err < 0 ? err : 1;
570}
571
572static int snd_xonar_u1_switch_resume(struct usb_mixer_elem_list *list)
573{
574 return snd_xonar_u1_switch_update(list->mixer,
575 list->kctl->private_value);
Daniel Mack7b1eda22010-03-11 21:13:22 +0100576}
577
Takashi Iwai195727e2020-01-03 09:16:57 +0100578static const struct snd_kcontrol_new snd_xonar_u1_output_switch = {
Daniel Mack7b1eda22010-03-11 21:13:22 +0100579 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
580 .name = "Digital Playback Switch",
581 .info = snd_ctl_boolean_mono_info,
582 .get = snd_xonar_u1_switch_get,
583 .put = snd_xonar_u1_switch_put,
Takashi Iwai2bfb14c2014-11-18 16:18:15 +0100584 .private_value = 0x05,
Daniel Mack7b1eda22010-03-11 21:13:22 +0100585};
586
587static int snd_xonar_u1_controls_create(struct usb_mixer_interface *mixer)
588{
Takashi Iwai2bfb14c2014-11-18 16:18:15 +0100589 return add_single_ctl_with_resume(mixer, 0,
590 snd_xonar_u1_switch_resume,
591 &snd_xonar_u1_output_switch, NULL);
Daniel Mack7b1eda22010-03-11 21:13:22 +0100592}
593
Damien Zammitd497a822014-11-12 01:09:54 +1100594/* Digidesign Mbox 1 clock source switch (internal/spdif) */
595
596static int snd_mbox1_switch_get(struct snd_kcontrol *kctl,
597 struct snd_ctl_elem_value *ucontrol)
598{
599 ucontrol->value.enumerated.item[0] = kctl->private_value;
600 return 0;
601}
602
Takashi Iwai25a9a4f2014-11-18 16:31:35 +0100603static int snd_mbox1_switch_update(struct usb_mixer_interface *mixer, int val)
Damien Zammitd497a822014-11-12 01:09:54 +1100604{
Takashi Iwai25a9a4f2014-11-18 16:31:35 +0100605 struct snd_usb_audio *chip = mixer->chip;
Damien Zammitd497a822014-11-12 01:09:54 +1100606 int err;
Damien Zammitd497a822014-11-12 01:09:54 +1100607 unsigned char buff[3];
608
Takashi Iwai47ab1542015-08-25 16:09:00 +0200609 err = snd_usb_lock_shutdown(chip);
610 if (err < 0)
611 return err;
Damien Zammitd497a822014-11-12 01:09:54 +1100612
613 /* Prepare for magic command to toggle clock source */
614 err = snd_usb_ctl_msg(chip->dev,
615 usb_rcvctrlpipe(chip->dev, 0), 0x81,
616 USB_DIR_IN |
617 USB_TYPE_CLASS |
618 USB_RECIP_INTERFACE, 0x00, 0x500, buff, 1);
619 if (err < 0)
620 goto err;
621 err = snd_usb_ctl_msg(chip->dev,
622 usb_rcvctrlpipe(chip->dev, 0), 0x81,
623 USB_DIR_IN |
624 USB_TYPE_CLASS |
625 USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
626 if (err < 0)
627 goto err;
628
629 /* 2 possibilities: Internal -> send sample rate
630 * S/PDIF sync -> send zeroes
631 * NB: Sample rate locked to 48kHz on purpose to
632 * prevent user from resetting the sample rate
633 * while S/PDIF sync is enabled and confusing
634 * this configuration.
635 */
Takashi Iwai25a9a4f2014-11-18 16:31:35 +0100636 if (val == 0) {
Damien Zammitd497a822014-11-12 01:09:54 +1100637 buff[0] = 0x80;
638 buff[1] = 0xbb;
639 buff[2] = 0x00;
640 } else {
641 buff[0] = buff[1] = buff[2] = 0x00;
642 }
643
644 /* Send the magic command to toggle the clock source */
645 err = snd_usb_ctl_msg(chip->dev,
646 usb_sndctrlpipe(chip->dev, 0), 0x1,
647 USB_TYPE_CLASS |
648 USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
649 if (err < 0)
650 goto err;
651 err = snd_usb_ctl_msg(chip->dev,
652 usb_rcvctrlpipe(chip->dev, 0), 0x81,
653 USB_DIR_IN |
654 USB_TYPE_CLASS |
655 USB_RECIP_ENDPOINT, 0x100, 0x81, buff, 3);
656 if (err < 0)
657 goto err;
658 err = snd_usb_ctl_msg(chip->dev,
659 usb_rcvctrlpipe(chip->dev, 0), 0x81,
660 USB_DIR_IN |
661 USB_TYPE_CLASS |
662 USB_RECIP_ENDPOINT, 0x100, 0x2, buff, 3);
663 if (err < 0)
664 goto err;
Damien Zammitd497a822014-11-12 01:09:54 +1100665
666err:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200667 snd_usb_unlock_shutdown(chip);
Takashi Iwai25a9a4f2014-11-18 16:31:35 +0100668 return err;
669}
670
671static int snd_mbox1_switch_put(struct snd_kcontrol *kctl,
672 struct snd_ctl_elem_value *ucontrol)
673{
674 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
675 struct usb_mixer_interface *mixer = list->mixer;
676 int err;
677 bool cur_val, new_val;
678
679 cur_val = kctl->private_value;
680 new_val = ucontrol->value.enumerated.item[0];
681 if (cur_val == new_val)
682 return 0;
683
684 kctl->private_value = new_val;
685 err = snd_mbox1_switch_update(mixer, new_val);
Damien Zammitd497a822014-11-12 01:09:54 +1100686 return err < 0 ? err : 1;
687}
688
689static int snd_mbox1_switch_info(struct snd_kcontrol *kcontrol,
690 struct snd_ctl_elem_info *uinfo)
691{
692 static const char *const texts[2] = {
693 "Internal",
694 "S/PDIF"
695 };
696
697 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
698}
699
Takashi Iwai25a9a4f2014-11-18 16:31:35 +0100700static int snd_mbox1_switch_resume(struct usb_mixer_elem_list *list)
701{
702 return snd_mbox1_switch_update(list->mixer, list->kctl->private_value);
703}
704
Takashi Iwai195727e2020-01-03 09:16:57 +0100705static const struct snd_kcontrol_new snd_mbox1_switch = {
Damien Zammitd497a822014-11-12 01:09:54 +1100706 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
707 .name = "Clock Source",
708 .index = 0,
709 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
710 .info = snd_mbox1_switch_info,
711 .get = snd_mbox1_switch_get,
712 .put = snd_mbox1_switch_put,
713 .private_value = 0
714};
715
716static int snd_mbox1_create_sync_switch(struct usb_mixer_interface *mixer)
717{
Takashi Iwai25a9a4f2014-11-18 16:31:35 +0100718 return add_single_ctl_with_resume(mixer, 0,
719 snd_mbox1_switch_resume,
720 &snd_mbox1_switch, NULL);
Damien Zammitd497a822014-11-12 01:09:54 +1100721}
722
Daniel Mack54a8c502011-02-11 11:08:06 +0000723/* Native Instruments device quirks */
724
725#define _MAKE_NI_CONTROL(bRequest,wIndex) ((bRequest) << 16 | (wIndex))
726
Takashi Iwaida6d2762014-11-18 16:59:47 +0100727static int snd_ni_control_init_val(struct usb_mixer_interface *mixer,
728 struct snd_kcontrol *kctl)
729{
730 struct usb_device *dev = mixer->chip->dev;
731 unsigned int pval = kctl->private_value;
732 u8 value;
733 int err;
734
735 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
736 (pval >> 16) & 0xff,
737 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
738 0, pval & 0xffff, &value, 1);
739 if (err < 0) {
740 dev_err(&dev->dev,
741 "unable to issue vendor read request (ret = %d)", err);
742 return err;
743 }
744
Colin Ian King2acf5a32019-06-27 17:43:08 +0100745 kctl->private_value |= ((unsigned int)value << 24);
Takashi Iwaida6d2762014-11-18 16:59:47 +0100746 return 0;
747}
748
Daniel Mack54a8c502011-02-11 11:08:06 +0000749static int snd_nativeinstruments_control_get(struct snd_kcontrol *kcontrol,
750 struct snd_ctl_elem_value *ucontrol)
751{
Takashi Iwaida6d2762014-11-18 16:59:47 +0100752 ucontrol->value.integer.value[0] = kcontrol->private_value >> 24;
Daniel Mack54a8c502011-02-11 11:08:06 +0000753 return 0;
754}
755
Takashi Iwaida6d2762014-11-18 16:59:47 +0100756static int snd_ni_update_cur_val(struct usb_mixer_elem_list *list)
757{
758 struct snd_usb_audio *chip = list->mixer->chip;
759 unsigned int pval = list->kctl->private_value;
760 int err;
761
Takashi Iwai47ab1542015-08-25 16:09:00 +0200762 err = snd_usb_lock_shutdown(chip);
763 if (err < 0)
764 return err;
765 err = usb_control_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
766 (pval >> 16) & 0xff,
767 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
768 pval >> 24, pval & 0xffff, NULL, 0, 1000);
769 snd_usb_unlock_shutdown(chip);
Takashi Iwaida6d2762014-11-18 16:59:47 +0100770 return err;
771}
772
Daniel Mack54a8c502011-02-11 11:08:06 +0000773static int snd_nativeinstruments_control_put(struct snd_kcontrol *kcontrol,
774 struct snd_ctl_elem_value *ucontrol)
775{
Takashi Iwaida6d2762014-11-18 16:59:47 +0100776 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
777 u8 oldval = (kcontrol->private_value >> 24) & 0xff;
778 u8 newval = ucontrol->value.integer.value[0];
779 int err;
Daniel Mack54a8c502011-02-11 11:08:06 +0000780
Takashi Iwaida6d2762014-11-18 16:59:47 +0100781 if (oldval == newval)
782 return 0;
Daniel Mack54a8c502011-02-11 11:08:06 +0000783
Takashi Iwaida6d2762014-11-18 16:59:47 +0100784 kcontrol->private_value &= ~(0xff << 24);
Takashi Iwaic4a359a2016-01-13 07:20:13 +0100785 kcontrol->private_value |= (unsigned int)newval << 24;
Takashi Iwaida6d2762014-11-18 16:59:47 +0100786 err = snd_ni_update_cur_val(list);
787 return err < 0 ? err : 1;
Daniel Mack54a8c502011-02-11 11:08:06 +0000788}
789
Takashi Iwai195727e2020-01-03 09:16:57 +0100790static const struct snd_kcontrol_new snd_nativeinstruments_ta6_mixers[] = {
Daniel Mack54a8c502011-02-11 11:08:06 +0000791 {
792 .name = "Direct Thru Channel A",
793 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
794 },
795 {
796 .name = "Direct Thru Channel B",
797 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
798 },
799 {
800 .name = "Phono Input Channel A",
801 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
802 },
803 {
804 .name = "Phono Input Channel B",
805 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
806 },
807};
808
Takashi Iwai195727e2020-01-03 09:16:57 +0100809static const struct snd_kcontrol_new snd_nativeinstruments_ta10_mixers[] = {
Daniel Mack54a8c502011-02-11 11:08:06 +0000810 {
811 .name = "Direct Thru Channel A",
812 .private_value = _MAKE_NI_CONTROL(0x01, 0x03),
813 },
814 {
815 .name = "Direct Thru Channel B",
816 .private_value = _MAKE_NI_CONTROL(0x01, 0x05),
817 },
818 {
819 .name = "Direct Thru Channel C",
820 .private_value = _MAKE_NI_CONTROL(0x01, 0x07),
821 },
822 {
823 .name = "Direct Thru Channel D",
824 .private_value = _MAKE_NI_CONTROL(0x01, 0x09),
825 },
826 {
827 .name = "Phono Input Channel A",
828 .private_value = _MAKE_NI_CONTROL(0x02, 0x03),
829 },
830 {
831 .name = "Phono Input Channel B",
832 .private_value = _MAKE_NI_CONTROL(0x02, 0x05),
833 },
834 {
835 .name = "Phono Input Channel C",
836 .private_value = _MAKE_NI_CONTROL(0x02, 0x07),
837 },
838 {
839 .name = "Phono Input Channel D",
840 .private_value = _MAKE_NI_CONTROL(0x02, 0x09),
841 },
842};
843
844static int snd_nativeinstruments_create_mixer(struct usb_mixer_interface *mixer,
845 const struct snd_kcontrol_new *kc,
846 unsigned int count)
847{
848 int i, err = 0;
849 struct snd_kcontrol_new template = {
850 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
851 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
852 .get = snd_nativeinstruments_control_get,
853 .put = snd_nativeinstruments_control_put,
854 .info = snd_ctl_boolean_mono_info,
855 };
856
857 for (i = 0; i < count; i++) {
Takashi Iwaida6d2762014-11-18 16:59:47 +0100858 struct usb_mixer_elem_list *list;
Daniel Mack54a8c502011-02-11 11:08:06 +0000859
860 template.name = kc[i].name;
861 template.private_value = kc[i].private_value;
862
Takashi Iwaida6d2762014-11-18 16:59:47 +0100863 err = add_single_ctl_with_resume(mixer, 0,
864 snd_ni_update_cur_val,
865 &template, &list);
Daniel Mack54a8c502011-02-11 11:08:06 +0000866 if (err < 0)
867 break;
Takashi Iwaida6d2762014-11-18 16:59:47 +0100868 snd_ni_control_init_val(mixer, list->kctl);
Daniel Mack54a8c502011-02-11 11:08:06 +0000869 }
870
871 return err;
872}
873
Daniel Mackd5a0bf62011-05-25 09:09:03 +0200874/* M-Audio FastTrack Ultra quirks */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500875/* FTU Effect switch (also used by C400/C600) */
Felix Homannd34bf142012-04-23 20:24:27 +0200876static int snd_ftu_eff_switch_info(struct snd_kcontrol *kcontrol,
877 struct snd_ctl_elem_info *uinfo)
878{
Takashi Iwai7bbd03e2014-10-20 18:21:42 +0200879 static const char *const texts[8] = {
880 "Room 1", "Room 2", "Room 3", "Hall 1",
881 "Hall 2", "Plate", "Delay", "Echo"
Felix Homannd34bf142012-04-23 20:24:27 +0200882 };
883
Takashi Iwai7bbd03e2014-10-20 18:21:42 +0200884 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
Felix Homannd34bf142012-04-23 20:24:27 +0200885}
886
Takashi Iwai0b4e9cf2014-11-18 17:37:40 +0100887static int snd_ftu_eff_switch_init(struct usb_mixer_interface *mixer,
888 struct snd_kcontrol *kctl)
Felix Homannd34bf142012-04-23 20:24:27 +0200889{
Takashi Iwai0b4e9cf2014-11-18 17:37:40 +0100890 struct usb_device *dev = mixer->chip->dev;
891 unsigned int pval = kctl->private_value;
Felix Homannd34bf142012-04-23 20:24:27 +0200892 int err;
893 unsigned char value[2];
Felix Homannd34bf142012-04-23 20:24:27 +0200894
895 value[0] = 0x00;
896 value[1] = 0x00;
897
Takashi Iwai0b4e9cf2014-11-18 17:37:40 +0100898 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
899 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
900 pval & 0xff00,
901 snd_usb_ctrl_intf(mixer->chip) | ((pval & 0xff) << 8),
902 value, 2);
Felix Homannd34bf142012-04-23 20:24:27 +0200903 if (err < 0)
904 return err;
905
Colin Ian King2acf5a32019-06-27 17:43:08 +0100906 kctl->private_value |= (unsigned int)value[0] << 24;
Felix Homannd34bf142012-04-23 20:24:27 +0200907 return 0;
908}
909
Takashi Iwai0b4e9cf2014-11-18 17:37:40 +0100910static int snd_ftu_eff_switch_get(struct snd_kcontrol *kctl,
911 struct snd_ctl_elem_value *ucontrol)
912{
913 ucontrol->value.enumerated.item[0] = kctl->private_value >> 24;
914 return 0;
915}
916
917static int snd_ftu_eff_switch_update(struct usb_mixer_elem_list *list)
918{
919 struct snd_usb_audio *chip = list->mixer->chip;
920 unsigned int pval = list->kctl->private_value;
921 unsigned char value[2];
922 int err;
923
924 value[0] = pval >> 24;
925 value[1] = 0;
926
Takashi Iwai47ab1542015-08-25 16:09:00 +0200927 err = snd_usb_lock_shutdown(chip);
928 if (err < 0)
929 return err;
930 err = snd_usb_ctl_msg(chip->dev,
931 usb_sndctrlpipe(chip->dev, 0),
932 UAC_SET_CUR,
933 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
934 pval & 0xff00,
935 snd_usb_ctrl_intf(chip) | ((pval & 0xff) << 8),
936 value, 2);
937 snd_usb_unlock_shutdown(chip);
Takashi Iwai0b4e9cf2014-11-18 17:37:40 +0100938 return err;
939}
940
Felix Homannd34bf142012-04-23 20:24:27 +0200941static int snd_ftu_eff_switch_put(struct snd_kcontrol *kctl,
942 struct snd_ctl_elem_value *ucontrol)
943{
Takashi Iwai0b4e9cf2014-11-18 17:37:40 +0100944 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
945 unsigned int pval = list->kctl->private_value;
946 int cur_val, err, new_val;
Felix Homannd34bf142012-04-23 20:24:27 +0200947
Takashi Iwai0b4e9cf2014-11-18 17:37:40 +0100948 cur_val = pval >> 24;
Felix Homannd34bf142012-04-23 20:24:27 +0200949 new_val = ucontrol->value.enumerated.item[0];
Takashi Iwai0b4e9cf2014-11-18 17:37:40 +0100950 if (cur_val == new_val)
951 return 0;
Felix Homannd34bf142012-04-23 20:24:27 +0200952
Takashi Iwai0b4e9cf2014-11-18 17:37:40 +0100953 kctl->private_value &= ~(0xff << 24);
954 kctl->private_value |= new_val << 24;
955 err = snd_ftu_eff_switch_update(list);
956 return err < 0 ? err : 1;
Takashi Iwai1a290582014-11-11 15:45:57 +0100957}
958
Eldad Zackd847ce02012-11-28 23:55:37 +0100959static int snd_ftu_create_effect_switch(struct usb_mixer_interface *mixer,
960 int validx, int bUnitID)
Felix Homannd34bf142012-04-23 20:24:27 +0200961{
962 static struct snd_kcontrol_new template = {
963 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
964 .name = "Effect Program Switch",
965 .index = 0,
966 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
967 .info = snd_ftu_eff_switch_info,
968 .get = snd_ftu_eff_switch_get,
969 .put = snd_ftu_eff_switch_put
970 };
Takashi Iwai0b4e9cf2014-11-18 17:37:40 +0100971 struct usb_mixer_elem_list *list;
Felix Homannd34bf142012-04-23 20:24:27 +0200972 int err;
Felix Homannd34bf142012-04-23 20:24:27 +0200973
Takashi Iwai0b4e9cf2014-11-18 17:37:40 +0100974 err = add_single_ctl_with_resume(mixer, bUnitID,
975 snd_ftu_eff_switch_update,
976 &template, &list);
Felix Homannd34bf142012-04-23 20:24:27 +0200977 if (err < 0)
978 return err;
Takashi Iwai0b4e9cf2014-11-18 17:37:40 +0100979 list->kctl->private_value = (validx << 8) | bUnitID;
980 snd_ftu_eff_switch_init(mixer, list->kctl);
Felix Homannd34bf142012-04-23 20:24:27 +0200981 return 0;
982}
Daniel Mackd5a0bf62011-05-25 09:09:03 +0200983
Felix Homanncfe8f972012-04-23 20:24:26 +0200984/* Create volume controls for FTU devices*/
985static int snd_ftu_create_volume_ctls(struct usb_mixer_interface *mixer)
Daniel Mackd5a0bf62011-05-25 09:09:03 +0200986{
987 char name[64];
Felix Homann8a4d1d32012-04-23 20:24:23 +0200988 unsigned int control, cmask;
Daniel Mackd5a0bf62011-05-25 09:09:03 +0200989 int in, out, err;
990
Felix Homann8a4d1d32012-04-23 20:24:23 +0200991 const unsigned int id = 5;
992 const int val_type = USB_MIXER_S16;
993
Daniel Mackd5a0bf62011-05-25 09:09:03 +0200994 for (out = 0; out < 8; out++) {
Felix Homann8a4d1d32012-04-23 20:24:23 +0200995 control = out + 1;
Daniel Mackd5a0bf62011-05-25 09:09:03 +0200996 for (in = 0; in < 8; in++) {
Felix Homann8a4d1d32012-04-23 20:24:23 +0200997 cmask = 1 << in;
Daniel Mackd5a0bf62011-05-25 09:09:03 +0200998 snprintf(name, sizeof(name),
Felix Homann8a4d1d32012-04-23 20:24:23 +0200999 "AIn%d - Out%d Capture Volume",
1000 in + 1, out + 1);
1001 err = snd_create_std_mono_ctl(mixer, id, control,
1002 cmask, val_type, name,
Felix Homann25ee7ef2012-04-23 20:24:25 +02001003 &snd_usb_mixer_vol_tlv);
Daniel Mackd5a0bf62011-05-25 09:09:03 +02001004 if (err < 0)
1005 return err;
1006 }
Daniel Mackd5a0bf62011-05-25 09:09:03 +02001007 for (in = 8; in < 16; in++) {
Felix Homann8a4d1d32012-04-23 20:24:23 +02001008 cmask = 1 << in;
Daniel Mackd5a0bf62011-05-25 09:09:03 +02001009 snprintf(name, sizeof(name),
Felix Homann8a4d1d32012-04-23 20:24:23 +02001010 "DIn%d - Out%d Playback Volume",
1011 in - 7, out + 1);
1012 err = snd_create_std_mono_ctl(mixer, id, control,
1013 cmask, val_type, name,
Felix Homann25ee7ef2012-04-23 20:24:25 +02001014 &snd_usb_mixer_vol_tlv);
Daniel Mackd5a0bf62011-05-25 09:09:03 +02001015 if (err < 0)
1016 return err;
1017 }
1018 }
1019
1020 return 0;
1021}
1022
Felix Homannd34bf142012-04-23 20:24:27 +02001023/* This control needs a volume quirk, see mixer.c */
1024static int snd_ftu_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1025{
1026 static const char name[] = "Effect Volume";
1027 const unsigned int id = 6;
1028 const int val_type = USB_MIXER_U8;
1029 const unsigned int control = 2;
1030 const unsigned int cmask = 0;
1031
1032 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1033 name, snd_usb_mixer_vol_tlv);
1034}
1035
1036/* This control needs a volume quirk, see mixer.c */
1037static int snd_ftu_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1038{
1039 static const char name[] = "Effect Duration";
1040 const unsigned int id = 6;
1041 const int val_type = USB_MIXER_S16;
1042 const unsigned int control = 3;
1043 const unsigned int cmask = 0;
1044
1045 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1046 name, snd_usb_mixer_vol_tlv);
1047}
1048
1049/* This control needs a volume quirk, see mixer.c */
1050static int snd_ftu_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1051{
1052 static const char name[] = "Effect Feedback Volume";
1053 const unsigned int id = 6;
1054 const int val_type = USB_MIXER_U8;
1055 const unsigned int control = 4;
1056 const unsigned int cmask = 0;
1057
1058 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1059 name, NULL);
1060}
1061
1062static int snd_ftu_create_effect_return_ctls(struct usb_mixer_interface *mixer)
1063{
1064 unsigned int cmask;
1065 int err, ch;
1066 char name[48];
1067
1068 const unsigned int id = 7;
1069 const int val_type = USB_MIXER_S16;
1070 const unsigned int control = 7;
1071
1072 for (ch = 0; ch < 4; ++ch) {
1073 cmask = 1 << ch;
1074 snprintf(name, sizeof(name),
1075 "Effect Return %d Volume", ch + 1);
1076 err = snd_create_std_mono_ctl(mixer, id, control,
1077 cmask, val_type, name,
1078 snd_usb_mixer_vol_tlv);
1079 if (err < 0)
1080 return err;
1081 }
1082
1083 return 0;
1084}
1085
1086static int snd_ftu_create_effect_send_ctls(struct usb_mixer_interface *mixer)
1087{
1088 unsigned int cmask;
1089 int err, ch;
1090 char name[48];
1091
1092 const unsigned int id = 5;
1093 const int val_type = USB_MIXER_S16;
1094 const unsigned int control = 9;
1095
1096 for (ch = 0; ch < 8; ++ch) {
1097 cmask = 1 << ch;
1098 snprintf(name, sizeof(name),
1099 "Effect Send AIn%d Volume", ch + 1);
1100 err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1101 val_type, name,
1102 snd_usb_mixer_vol_tlv);
1103 if (err < 0)
1104 return err;
1105 }
1106 for (ch = 8; ch < 16; ++ch) {
1107 cmask = 1 << ch;
1108 snprintf(name, sizeof(name),
1109 "Effect Send DIn%d Volume", ch - 7);
1110 err = snd_create_std_mono_ctl(mixer, id, control, cmask,
1111 val_type, name,
1112 snd_usb_mixer_vol_tlv);
1113 if (err < 0)
1114 return err;
1115 }
1116 return 0;
1117}
1118
Felix Homanncfe8f972012-04-23 20:24:26 +02001119static int snd_ftu_create_mixer(struct usb_mixer_interface *mixer)
Mark Hills7536c302012-04-14 17:19:24 +01001120{
Felix Homann8a4d1d32012-04-23 20:24:23 +02001121 int err;
Mark Hills7536c302012-04-14 17:19:24 +01001122
Felix Homanncfe8f972012-04-23 20:24:26 +02001123 err = snd_ftu_create_volume_ctls(mixer);
Felix Homann8a4d1d32012-04-23 20:24:23 +02001124 if (err < 0)
1125 return err;
Mark Hills7536c302012-04-14 17:19:24 +01001126
Eldad Zackd847ce02012-11-28 23:55:37 +01001127 err = snd_ftu_create_effect_switch(mixer, 1, 6);
Felix Homannd34bf142012-04-23 20:24:27 +02001128 if (err < 0)
1129 return err;
Eldad Zackd847ce02012-11-28 23:55:37 +01001130
Felix Homannd34bf142012-04-23 20:24:27 +02001131 err = snd_ftu_create_effect_volume_ctl(mixer);
1132 if (err < 0)
1133 return err;
1134
1135 err = snd_ftu_create_effect_duration_ctl(mixer);
1136 if (err < 0)
1137 return err;
1138
1139 err = snd_ftu_create_effect_feedback_ctl(mixer);
1140 if (err < 0)
1141 return err;
1142
1143 err = snd_ftu_create_effect_return_ctls(mixer);
1144 if (err < 0)
1145 return err;
1146
1147 err = snd_ftu_create_effect_send_ctls(mixer);
1148 if (err < 0)
1149 return err;
1150
Felix Homann8a4d1d32012-04-23 20:24:23 +02001151 return 0;
Mark Hills7536c302012-04-14 17:19:24 +01001152}
1153
Daniel Mack7b1eda22010-03-11 21:13:22 +01001154void snd_emuusb_set_samplerate(struct snd_usb_audio *chip,
1155 unsigned char samplerate_id)
1156{
1157 struct usb_mixer_interface *mixer;
1158 struct usb_mixer_elem_info *cval;
Takashi Iwai6de3c9e2019-08-15 11:41:06 +02001159 int unitid = 12; /* SampleRate ExtensionUnit ID */
Daniel Mack7b1eda22010-03-11 21:13:22 +01001160
1161 list_for_each_entry(mixer, &chip->mixer_list, list) {
Takashi Iwai6de3c9e2019-08-15 11:41:06 +02001162 if (mixer->id_elems[unitid]) {
1163 cval = mixer_elem_list_to_info(mixer->id_elems[unitid]);
Daniel Mack7b1eda22010-03-11 21:13:22 +01001164 snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR,
1165 cval->control << 8,
1166 samplerate_id);
1167 snd_usb_mixer_notify_id(mixer, unitid);
Takashi Iwai6de3c9e2019-08-15 11:41:06 +02001168 break;
Daniel Mack7b1eda22010-03-11 21:13:22 +01001169 }
Daniel Mack7b1eda22010-03-11 21:13:22 +01001170 }
1171}
1172
Matt Gruskine9a25e02013-02-09 12:56:35 -05001173/* M-Audio Fast Track C400/C600 */
1174/* C400/C600 volume controls, this control needs a volume quirk, see mixer.c */
Eldad Zack09d8e3a2012-11-28 23:55:40 +01001175static int snd_c400_create_vol_ctls(struct usb_mixer_interface *mixer)
1176{
1177 char name[64];
1178 unsigned int cmask, offset;
1179 int out, chan, err;
Matt Gruskine9a25e02013-02-09 12:56:35 -05001180 int num_outs = 0;
1181 int num_ins = 0;
Eldad Zack09d8e3a2012-11-28 23:55:40 +01001182
1183 const unsigned int id = 0x40;
1184 const int val_type = USB_MIXER_S16;
1185 const int control = 1;
1186
Matt Gruskine9a25e02013-02-09 12:56:35 -05001187 switch (mixer->chip->usb_id) {
1188 case USB_ID(0x0763, 0x2030):
1189 num_outs = 6;
1190 num_ins = 4;
1191 break;
1192 case USB_ID(0x0763, 0x2031):
1193 num_outs = 8;
1194 num_ins = 6;
1195 break;
1196 }
1197
1198 for (chan = 0; chan < num_outs + num_ins; chan++) {
1199 for (out = 0; out < num_outs; out++) {
1200 if (chan < num_outs) {
Eldad Zack09d8e3a2012-11-28 23:55:40 +01001201 snprintf(name, sizeof(name),
1202 "PCM%d-Out%d Playback Volume",
1203 chan + 1, out + 1);
1204 } else {
1205 snprintf(name, sizeof(name),
1206 "In%d-Out%d Playback Volume",
Matt Gruskine9a25e02013-02-09 12:56:35 -05001207 chan - num_outs + 1, out + 1);
Eldad Zack09d8e3a2012-11-28 23:55:40 +01001208 }
1209
1210 cmask = (out == 0) ? 0 : 1 << (out - 1);
Matt Gruskine9a25e02013-02-09 12:56:35 -05001211 offset = chan * num_outs;
Eldad Zack09d8e3a2012-11-28 23:55:40 +01001212 err = snd_create_std_mono_ctl_offset(mixer, id, control,
1213 cmask, val_type, offset, name,
1214 &snd_usb_mixer_vol_tlv);
1215 if (err < 0)
1216 return err;
1217 }
1218 }
1219
1220 return 0;
1221}
1222
1223/* This control needs a volume quirk, see mixer.c */
1224static int snd_c400_create_effect_volume_ctl(struct usb_mixer_interface *mixer)
1225{
1226 static const char name[] = "Effect Volume";
1227 const unsigned int id = 0x43;
1228 const int val_type = USB_MIXER_U8;
1229 const unsigned int control = 3;
1230 const unsigned int cmask = 0;
1231
1232 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1233 name, snd_usb_mixer_vol_tlv);
1234}
1235
1236/* This control needs a volume quirk, see mixer.c */
1237static int snd_c400_create_effect_duration_ctl(struct usb_mixer_interface *mixer)
1238{
1239 static const char name[] = "Effect Duration";
1240 const unsigned int id = 0x43;
1241 const int val_type = USB_MIXER_S16;
1242 const unsigned int control = 4;
1243 const unsigned int cmask = 0;
1244
1245 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1246 name, snd_usb_mixer_vol_tlv);
1247}
1248
1249/* This control needs a volume quirk, see mixer.c */
1250static int snd_c400_create_effect_feedback_ctl(struct usb_mixer_interface *mixer)
1251{
1252 static const char name[] = "Effect Feedback Volume";
1253 const unsigned int id = 0x43;
1254 const int val_type = USB_MIXER_U8;
1255 const unsigned int control = 5;
1256 const unsigned int cmask = 0;
1257
1258 return snd_create_std_mono_ctl(mixer, id, control, cmask, val_type,
1259 name, NULL);
1260}
1261
1262static int snd_c400_create_effect_vol_ctls(struct usb_mixer_interface *mixer)
1263{
1264 char name[64];
1265 unsigned int cmask;
1266 int chan, err;
Matt Gruskine9a25e02013-02-09 12:56:35 -05001267 int num_outs = 0;
1268 int num_ins = 0;
Eldad Zack09d8e3a2012-11-28 23:55:40 +01001269
1270 const unsigned int id = 0x42;
1271 const int val_type = USB_MIXER_S16;
1272 const int control = 1;
1273
Matt Gruskine9a25e02013-02-09 12:56:35 -05001274 switch (mixer->chip->usb_id) {
1275 case USB_ID(0x0763, 0x2030):
1276 num_outs = 6;
1277 num_ins = 4;
1278 break;
1279 case USB_ID(0x0763, 0x2031):
1280 num_outs = 8;
1281 num_ins = 6;
1282 break;
1283 }
1284
1285 for (chan = 0; chan < num_outs + num_ins; chan++) {
1286 if (chan < num_outs) {
Eldad Zack09d8e3a2012-11-28 23:55:40 +01001287 snprintf(name, sizeof(name),
1288 "Effect Send DOut%d",
1289 chan + 1);
1290 } else {
1291 snprintf(name, sizeof(name),
1292 "Effect Send AIn%d",
Matt Gruskine9a25e02013-02-09 12:56:35 -05001293 chan - num_outs + 1);
Eldad Zack09d8e3a2012-11-28 23:55:40 +01001294 }
1295
1296 cmask = (chan == 0) ? 0 : 1 << (chan - 1);
1297 err = snd_create_std_mono_ctl(mixer, id, control,
1298 cmask, val_type, name,
1299 &snd_usb_mixer_vol_tlv);
1300 if (err < 0)
1301 return err;
1302 }
1303
1304 return 0;
1305}
1306
1307static int snd_c400_create_effect_ret_vol_ctls(struct usb_mixer_interface *mixer)
1308{
1309 char name[64];
1310 unsigned int cmask;
1311 int chan, err;
Matt Gruskine9a25e02013-02-09 12:56:35 -05001312 int num_outs = 0;
1313 int offset = 0;
Eldad Zack09d8e3a2012-11-28 23:55:40 +01001314
1315 const unsigned int id = 0x40;
1316 const int val_type = USB_MIXER_S16;
1317 const int control = 1;
Eldad Zack09d8e3a2012-11-28 23:55:40 +01001318
Matt Gruskine9a25e02013-02-09 12:56:35 -05001319 switch (mixer->chip->usb_id) {
1320 case USB_ID(0x0763, 0x2030):
1321 num_outs = 6;
1322 offset = 0x3c;
1323 /* { 0x3c, 0x43, 0x3e, 0x45, 0x40, 0x47 } */
1324 break;
1325 case USB_ID(0x0763, 0x2031):
1326 num_outs = 8;
1327 offset = 0x70;
1328 /* { 0x70, 0x79, 0x72, 0x7b, 0x74, 0x7d, 0x76, 0x7f } */
1329 break;
1330 }
1331
1332 for (chan = 0; chan < num_outs; chan++) {
Eldad Zack09d8e3a2012-11-28 23:55:40 +01001333 snprintf(name, sizeof(name),
1334 "Effect Return %d",
1335 chan + 1);
1336
Matt Gruskine9a25e02013-02-09 12:56:35 -05001337 cmask = (chan == 0) ? 0 :
1338 1 << (chan + (chan % 2) * num_outs - 1);
Eldad Zack09d8e3a2012-11-28 23:55:40 +01001339 err = snd_create_std_mono_ctl_offset(mixer, id, control,
1340 cmask, val_type, offset, name,
1341 &snd_usb_mixer_vol_tlv);
1342 if (err < 0)
1343 return err;
1344 }
1345
1346 return 0;
1347}
1348
1349static int snd_c400_create_mixer(struct usb_mixer_interface *mixer)
1350{
1351 int err;
1352
1353 err = snd_c400_create_vol_ctls(mixer);
1354 if (err < 0)
1355 return err;
1356
1357 err = snd_c400_create_effect_vol_ctls(mixer);
1358 if (err < 0)
1359 return err;
1360
1361 err = snd_c400_create_effect_ret_vol_ctls(mixer);
1362 if (err < 0)
1363 return err;
1364
1365 err = snd_ftu_create_effect_switch(mixer, 2, 0x43);
1366 if (err < 0)
1367 return err;
1368
1369 err = snd_c400_create_effect_volume_ctl(mixer);
1370 if (err < 0)
1371 return err;
1372
1373 err = snd_c400_create_effect_duration_ctl(mixer);
1374 if (err < 0)
1375 return err;
1376
1377 err = snd_c400_create_effect_feedback_ctl(mixer);
1378 if (err < 0)
1379 return err;
1380
1381 return 0;
1382}
1383
Mark Hillsb71dad182012-06-09 13:16:38 +01001384/*
1385 * The mixer units for Ebox-44 are corrupt, and even where they
1386 * are valid they presents mono controls as L and R channels of
1387 * stereo. So we provide a good mixer here.
1388 */
Takashi Iwaia01df922020-01-05 15:47:26 +01001389static const struct std_mono_table ebox44_table[] = {
Mark Hills989b0132012-06-09 13:16:39 +01001390 {
1391 .unitid = 4,
1392 .control = 1,
1393 .cmask = 0x0,
1394 .val_type = USB_MIXER_INV_BOOLEAN,
1395 .name = "Headphone Playback Switch"
1396 },
1397 {
1398 .unitid = 4,
1399 .control = 2,
1400 .cmask = 0x1,
1401 .val_type = USB_MIXER_S16,
1402 .name = "Headphone A Mix Playback Volume"
1403 },
1404 {
1405 .unitid = 4,
1406 .control = 2,
1407 .cmask = 0x2,
1408 .val_type = USB_MIXER_S16,
1409 .name = "Headphone B Mix Playback Volume"
1410 },
Mark Hillsb71dad182012-06-09 13:16:38 +01001411
Mark Hills989b0132012-06-09 13:16:39 +01001412 {
1413 .unitid = 7,
1414 .control = 1,
1415 .cmask = 0x0,
1416 .val_type = USB_MIXER_INV_BOOLEAN,
1417 .name = "Output Playback Switch"
1418 },
1419 {
1420 .unitid = 7,
1421 .control = 2,
1422 .cmask = 0x1,
1423 .val_type = USB_MIXER_S16,
1424 .name = "Output A Playback Volume"
1425 },
1426 {
1427 .unitid = 7,
1428 .control = 2,
1429 .cmask = 0x2,
1430 .val_type = USB_MIXER_S16,
1431 .name = "Output B Playback Volume"
1432 },
Mark Hillsb71dad182012-06-09 13:16:38 +01001433
Mark Hills989b0132012-06-09 13:16:39 +01001434 {
1435 .unitid = 10,
1436 .control = 1,
1437 .cmask = 0x0,
1438 .val_type = USB_MIXER_INV_BOOLEAN,
1439 .name = "Input Capture Switch"
1440 },
1441 {
1442 .unitid = 10,
1443 .control = 2,
1444 .cmask = 0x1,
1445 .val_type = USB_MIXER_S16,
1446 .name = "Input A Capture Volume"
1447 },
1448 {
1449 .unitid = 10,
1450 .control = 2,
1451 .cmask = 0x2,
1452 .val_type = USB_MIXER_S16,
1453 .name = "Input B Capture Volume"
1454 },
Mark Hillsb71dad182012-06-09 13:16:38 +01001455
Mark Hills989b0132012-06-09 13:16:39 +01001456 {}
Mark Hillsb71dad182012-06-09 13:16:38 +01001457};
1458
Przemek Rudy066624c2013-06-27 23:52:33 +02001459/* Audio Advantage Micro II findings:
1460 *
1461 * Mapping spdif AES bits to vendor register.bit:
1462 * AES0: [0 0 0 0 2.3 2.2 2.1 2.0] - default 0x00
1463 * AES1: [3.3 3.2.3.1.3.0 2.7 2.6 2.5 2.4] - default: 0x01
1464 * AES2: [0 0 0 0 0 0 0 0]
1465 * AES3: [0 0 0 0 0 0 x 0] - 'x' bit is set basing on standard usb request
1466 * (UAC_EP_CS_ATTR_SAMPLE_RATE) for Audio Devices
1467 *
1468 * power on values:
1469 * r2: 0x10
1470 * r3: 0x20 (b7 is zeroed just before playback (except IEC61937) and set
1471 * just after it to 0xa0, presumably it disables/mutes some analog
1472 * parts when there is no audio.)
1473 * r9: 0x28
1474 *
1475 * Optical transmitter on/off:
1476 * vendor register.bit: 9.1
1477 * 0 - on (0x28 register value)
1478 * 1 - off (0x2a register value)
1479 *
1480 */
1481static int snd_microii_spdif_info(struct snd_kcontrol *kcontrol,
1482 struct snd_ctl_elem_info *uinfo)
1483{
1484 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1485 uinfo->count = 1;
1486 return 0;
1487}
1488
1489static int snd_microii_spdif_default_get(struct snd_kcontrol *kcontrol,
1490 struct snd_ctl_elem_value *ucontrol)
1491{
Takashi Iwai288673b2014-11-18 18:06:17 +01001492 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1493 struct snd_usb_audio *chip = list->mixer->chip;
Przemek Rudy066624c2013-06-27 23:52:33 +02001494 int err;
1495 struct usb_interface *iface;
1496 struct usb_host_interface *alts;
1497 unsigned int ep;
1498 unsigned char data[3];
1499 int rate;
1500
Takashi Iwai47ab1542015-08-25 16:09:00 +02001501 err = snd_usb_lock_shutdown(chip);
1502 if (err < 0)
1503 return err;
Takashi Iwai288673b2014-11-18 18:06:17 +01001504
Przemek Rudy066624c2013-06-27 23:52:33 +02001505 ucontrol->value.iec958.status[0] = kcontrol->private_value & 0xff;
1506 ucontrol->value.iec958.status[1] = (kcontrol->private_value >> 8) & 0xff;
1507 ucontrol->value.iec958.status[2] = 0x00;
1508
1509 /* use known values for that card: interface#1 altsetting#1 */
Takashi Iwai288673b2014-11-18 18:06:17 +01001510 iface = usb_ifnum_to_if(chip->dev, 1);
Takashi Iwai447d6272016-03-15 15:20:58 +01001511 if (!iface || iface->num_altsetting < 2)
1512 return -EINVAL;
Przemek Rudy066624c2013-06-27 23:52:33 +02001513 alts = &iface->altsetting[1];
Takashi Iwai447d6272016-03-15 15:20:58 +01001514 if (get_iface_desc(alts)->bNumEndpoints < 1)
1515 return -EINVAL;
Przemek Rudy066624c2013-06-27 23:52:33 +02001516 ep = get_endpoint(alts, 0)->bEndpointAddress;
1517
Takashi Iwai288673b2014-11-18 18:06:17 +01001518 err = snd_usb_ctl_msg(chip->dev,
1519 usb_rcvctrlpipe(chip->dev, 0),
Przemek Rudy066624c2013-06-27 23:52:33 +02001520 UAC_GET_CUR,
1521 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
1522 UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
1523 ep,
1524 data,
1525 sizeof(data));
1526 if (err < 0)
1527 goto end;
1528
1529 rate = data[0] | (data[1] << 8) | (data[2] << 16);
1530 ucontrol->value.iec958.status[3] = (rate == 48000) ?
1531 IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100;
1532
1533 err = 0;
Takashi Iwai288673b2014-11-18 18:06:17 +01001534 end:
Takashi Iwai47ab1542015-08-25 16:09:00 +02001535 snd_usb_unlock_shutdown(chip);
Przemek Rudy066624c2013-06-27 23:52:33 +02001536 return err;
1537}
1538
Takashi Iwai288673b2014-11-18 18:06:17 +01001539static int snd_microii_spdif_default_update(struct usb_mixer_elem_list *list)
Przemek Rudy066624c2013-06-27 23:52:33 +02001540{
Takashi Iwai288673b2014-11-18 18:06:17 +01001541 struct snd_usb_audio *chip = list->mixer->chip;
1542 unsigned int pval = list->kctl->private_value;
Przemek Rudy066624c2013-06-27 23:52:33 +02001543 u8 reg;
Takashi Iwai288673b2014-11-18 18:06:17 +01001544 int err;
Przemek Rudy066624c2013-06-27 23:52:33 +02001545
Takashi Iwai47ab1542015-08-25 16:09:00 +02001546 err = snd_usb_lock_shutdown(chip);
1547 if (err < 0)
1548 return err;
Takashi Iwai288673b2014-11-18 18:06:17 +01001549
1550 reg = ((pval >> 4) & 0xf0) | (pval & 0x0f);
1551 err = snd_usb_ctl_msg(chip->dev,
1552 usb_sndctrlpipe(chip->dev, 0),
Przemek Rudy066624c2013-06-27 23:52:33 +02001553 UAC_SET_CUR,
1554 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1555 reg,
1556 2,
1557 NULL,
1558 0);
1559 if (err < 0)
1560 goto end;
1561
Takashi Iwai288673b2014-11-18 18:06:17 +01001562 reg = (pval & IEC958_AES0_NONAUDIO) ? 0xa0 : 0x20;
1563 reg |= (pval >> 12) & 0x0f;
1564 err = snd_usb_ctl_msg(chip->dev,
1565 usb_sndctrlpipe(chip->dev, 0),
Przemek Rudy066624c2013-06-27 23:52:33 +02001566 UAC_SET_CUR,
1567 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1568 reg,
1569 3,
1570 NULL,
1571 0);
1572 if (err < 0)
1573 goto end;
1574
Takashi Iwai288673b2014-11-18 18:06:17 +01001575 end:
Takashi Iwai47ab1542015-08-25 16:09:00 +02001576 snd_usb_unlock_shutdown(chip);
Takashi Iwai288673b2014-11-18 18:06:17 +01001577 return err;
1578}
1579
1580static int snd_microii_spdif_default_put(struct snd_kcontrol *kcontrol,
1581 struct snd_ctl_elem_value *ucontrol)
1582{
1583 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1584 unsigned int pval, pval_old;
1585 int err;
1586
1587 pval = pval_old = kcontrol->private_value;
1588 pval &= 0xfffff0f0;
1589 pval |= (ucontrol->value.iec958.status[1] & 0x0f) << 8;
1590 pval |= (ucontrol->value.iec958.status[0] & 0x0f);
1591
1592 pval &= 0xffff0fff;
1593 pval |= (ucontrol->value.iec958.status[1] & 0xf0) << 8;
Przemek Rudy066624c2013-06-27 23:52:33 +02001594
1595 /* The frequency bits in AES3 cannot be set via register access. */
1596
1597 /* Silently ignore any bits from the request that cannot be set. */
1598
Takashi Iwai288673b2014-11-18 18:06:17 +01001599 if (pval == pval_old)
1600 return 0;
1601
1602 kcontrol->private_value = pval;
1603 err = snd_microii_spdif_default_update(list);
1604 return err < 0 ? err : 1;
Przemek Rudy066624c2013-06-27 23:52:33 +02001605}
1606
1607static int snd_microii_spdif_mask_get(struct snd_kcontrol *kcontrol,
1608 struct snd_ctl_elem_value *ucontrol)
1609{
1610 ucontrol->value.iec958.status[0] = 0x0f;
1611 ucontrol->value.iec958.status[1] = 0xff;
1612 ucontrol->value.iec958.status[2] = 0x00;
1613 ucontrol->value.iec958.status[3] = 0x00;
1614
1615 return 0;
1616}
1617
1618static int snd_microii_spdif_switch_get(struct snd_kcontrol *kcontrol,
1619 struct snd_ctl_elem_value *ucontrol)
1620{
1621 ucontrol->value.integer.value[0] = !(kcontrol->private_value & 0x02);
1622
1623 return 0;
1624}
1625
Takashi Iwai288673b2014-11-18 18:06:17 +01001626static int snd_microii_spdif_switch_update(struct usb_mixer_elem_list *list)
Przemek Rudy066624c2013-06-27 23:52:33 +02001627{
Takashi Iwai288673b2014-11-18 18:06:17 +01001628 struct snd_usb_audio *chip = list->mixer->chip;
1629 u8 reg = list->kctl->private_value;
Przemek Rudy066624c2013-06-27 23:52:33 +02001630 int err;
Przemek Rudy066624c2013-06-27 23:52:33 +02001631
Takashi Iwai47ab1542015-08-25 16:09:00 +02001632 err = snd_usb_lock_shutdown(chip);
1633 if (err < 0)
1634 return err;
Takashi Iwai288673b2014-11-18 18:06:17 +01001635
1636 err = snd_usb_ctl_msg(chip->dev,
1637 usb_sndctrlpipe(chip->dev, 0),
Przemek Rudy066624c2013-06-27 23:52:33 +02001638 UAC_SET_CUR,
1639 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
1640 reg,
1641 9,
1642 NULL,
1643 0);
1644
Takashi Iwai47ab1542015-08-25 16:09:00 +02001645 snd_usb_unlock_shutdown(chip);
Przemek Rudy066624c2013-06-27 23:52:33 +02001646 return err;
1647}
1648
Takashi Iwai288673b2014-11-18 18:06:17 +01001649static int snd_microii_spdif_switch_put(struct snd_kcontrol *kcontrol,
1650 struct snd_ctl_elem_value *ucontrol)
1651{
1652 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1653 u8 reg;
1654 int err;
1655
1656 reg = ucontrol->value.integer.value[0] ? 0x28 : 0x2a;
1657 if (reg != list->kctl->private_value)
1658 return 0;
1659
1660 kcontrol->private_value = reg;
1661 err = snd_microii_spdif_switch_update(list);
1662 return err < 0 ? err : 1;
1663}
1664
Takashi Iwai195727e2020-01-03 09:16:57 +01001665static const struct snd_kcontrol_new snd_microii_mixer_spdif[] = {
Przemek Rudy066624c2013-06-27 23:52:33 +02001666 {
1667 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1668 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1669 .info = snd_microii_spdif_info,
1670 .get = snd_microii_spdif_default_get,
1671 .put = snd_microii_spdif_default_put,
1672 .private_value = 0x00000100UL,/* reset value */
1673 },
1674 {
1675 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1676 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1677 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1678 .info = snd_microii_spdif_info,
1679 .get = snd_microii_spdif_mask_get,
1680 },
1681 {
1682 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1683 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1684 .info = snd_ctl_boolean_mono_info,
1685 .get = snd_microii_spdif_switch_get,
1686 .put = snd_microii_spdif_switch_put,
1687 .private_value = 0x00000028UL,/* reset value */
1688 }
1689};
1690
1691static int snd_microii_controls_create(struct usb_mixer_interface *mixer)
1692{
1693 int err, i;
Pierre-Louis Bossartff40e0d42020-01-11 15:47:36 -06001694 static const usb_mixer_elem_resume_func_t resume_funcs[] = {
Takashi Iwai288673b2014-11-18 18:06:17 +01001695 snd_microii_spdif_default_update,
1696 NULL,
1697 snd_microii_spdif_switch_update
1698 };
Przemek Rudy066624c2013-06-27 23:52:33 +02001699
1700 for (i = 0; i < ARRAY_SIZE(snd_microii_mixer_spdif); ++i) {
Takashi Iwai288673b2014-11-18 18:06:17 +01001701 err = add_single_ctl_with_resume(mixer, 0,
1702 resume_funcs[i],
1703 &snd_microii_mixer_spdif[i],
1704 NULL);
Przemek Rudy066624c2013-06-27 23:52:33 +02001705 if (err < 0)
1706 return err;
1707 }
1708
Mikulas Patocka18e47532013-12-05 14:32:43 -05001709 return 0;
Przemek Rudy066624c2013-06-27 23:52:33 +02001710}
1711
Ian Douglas Scott388fdb82018-01-16 15:34:50 -08001712/* Creative Sound Blaster E1 */
1713
1714static int snd_soundblaster_e1_switch_get(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_value *ucontrol)
1716{
1717 ucontrol->value.integer.value[0] = kcontrol->private_value;
1718 return 0;
1719}
1720
1721static int snd_soundblaster_e1_switch_update(struct usb_mixer_interface *mixer,
1722 unsigned char state)
1723{
1724 struct snd_usb_audio *chip = mixer->chip;
1725 int err;
1726 unsigned char buff[2];
1727
1728 buff[0] = 0x02;
1729 buff[1] = state ? 0x02 : 0x00;
1730
1731 err = snd_usb_lock_shutdown(chip);
1732 if (err < 0)
1733 return err;
1734 err = snd_usb_ctl_msg(chip->dev,
1735 usb_sndctrlpipe(chip->dev, 0), HID_REQ_SET_REPORT,
1736 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
1737 0x0202, 3, buff, 2);
1738 snd_usb_unlock_shutdown(chip);
1739 return err;
1740}
1741
1742static int snd_soundblaster_e1_switch_put(struct snd_kcontrol *kcontrol,
1743 struct snd_ctl_elem_value *ucontrol)
1744{
1745 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1746 unsigned char value = !!ucontrol->value.integer.value[0];
1747 int err;
1748
1749 if (kcontrol->private_value == value)
1750 return 0;
1751 kcontrol->private_value = value;
1752 err = snd_soundblaster_e1_switch_update(list->mixer, value);
1753 return err < 0 ? err : 1;
1754}
1755
1756static int snd_soundblaster_e1_switch_resume(struct usb_mixer_elem_list *list)
1757{
1758 return snd_soundblaster_e1_switch_update(list->mixer,
1759 list->kctl->private_value);
1760}
1761
1762static int snd_soundblaster_e1_switch_info(struct snd_kcontrol *kcontrol,
1763 struct snd_ctl_elem_info *uinfo)
1764{
1765 static const char *const texts[2] = {
1766 "Mic", "Aux"
1767 };
1768
1769 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1770}
1771
Takashi Iwai195727e2020-01-03 09:16:57 +01001772static const struct snd_kcontrol_new snd_soundblaster_e1_input_switch = {
Ian Douglas Scott388fdb82018-01-16 15:34:50 -08001773 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1774 .name = "Input Source",
1775 .info = snd_soundblaster_e1_switch_info,
1776 .get = snd_soundblaster_e1_switch_get,
1777 .put = snd_soundblaster_e1_switch_put,
1778 .private_value = 0,
1779};
1780
1781static int snd_soundblaster_e1_switch_create(struct usb_mixer_interface *mixer)
1782{
1783 return add_single_ctl_with_resume(mixer, 0,
1784 snd_soundblaster_e1_switch_resume,
1785 &snd_soundblaster_e1_input_switch,
1786 NULL);
1787}
1788
Takashi Iwai964af632018-04-27 14:23:37 +02001789static void dell_dock_init_vol(struct snd_usb_audio *chip, int ch, int id)
1790{
1791 u16 buf = 0;
1792
1793 snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC_SET_CUR,
1794 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1795 ch, snd_usb_ctrl_intf(chip) | (id << 8),
1796 &buf, 2);
1797}
1798
1799static int dell_dock_mixer_init(struct usb_mixer_interface *mixer)
1800{
1801 /* fix to 0dB playback volumes */
1802 dell_dock_init_vol(mixer->chip, 1, 16);
1803 dell_dock_init_vol(mixer->chip, 2, 16);
1804 dell_dock_init_vol(mixer->chip, 1, 19);
1805 dell_dock_init_vol(mixer->chip, 2, 19);
1806 return 0;
1807}
1808
Jussi Laakod39f1d62018-10-05 11:00:04 +03001809/* RME Class Compliant device quirks */
1810
1811#define SND_RME_GET_STATUS1 23
1812#define SND_RME_GET_CURRENT_FREQ 17
1813#define SND_RME_CLK_SYSTEM_SHIFT 16
1814#define SND_RME_CLK_SYSTEM_MASK 0x1f
1815#define SND_RME_CLK_AES_SHIFT 8
1816#define SND_RME_CLK_SPDIF_SHIFT 12
1817#define SND_RME_CLK_AES_SPDIF_MASK 0xf
1818#define SND_RME_CLK_SYNC_SHIFT 6
1819#define SND_RME_CLK_SYNC_MASK 0x3
1820#define SND_RME_CLK_FREQMUL_SHIFT 18
1821#define SND_RME_CLK_FREQMUL_MASK 0x7
1822#define SND_RME_CLK_SYSTEM(x) \
1823 ((x >> SND_RME_CLK_SYSTEM_SHIFT) & SND_RME_CLK_SYSTEM_MASK)
1824#define SND_RME_CLK_AES(x) \
1825 ((x >> SND_RME_CLK_AES_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
1826#define SND_RME_CLK_SPDIF(x) \
1827 ((x >> SND_RME_CLK_SPDIF_SHIFT) & SND_RME_CLK_AES_SPDIF_MASK)
1828#define SND_RME_CLK_SYNC(x) \
1829 ((x >> SND_RME_CLK_SYNC_SHIFT) & SND_RME_CLK_SYNC_MASK)
1830#define SND_RME_CLK_FREQMUL(x) \
1831 ((x >> SND_RME_CLK_FREQMUL_SHIFT) & SND_RME_CLK_FREQMUL_MASK)
1832#define SND_RME_CLK_AES_LOCK 0x1
1833#define SND_RME_CLK_AES_SYNC 0x4
1834#define SND_RME_CLK_SPDIF_LOCK 0x2
1835#define SND_RME_CLK_SPDIF_SYNC 0x8
1836#define SND_RME_SPDIF_IF_SHIFT 4
1837#define SND_RME_SPDIF_FORMAT_SHIFT 5
1838#define SND_RME_BINARY_MASK 0x1
1839#define SND_RME_SPDIF_IF(x) \
1840 ((x >> SND_RME_SPDIF_IF_SHIFT) & SND_RME_BINARY_MASK)
1841#define SND_RME_SPDIF_FORMAT(x) \
1842 ((x >> SND_RME_SPDIF_FORMAT_SHIFT) & SND_RME_BINARY_MASK)
1843
1844static const u32 snd_rme_rate_table[] = {
1845 32000, 44100, 48000, 50000,
1846 64000, 88200, 96000, 100000,
1847 128000, 176400, 192000, 200000,
1848 256000, 352800, 384000, 400000,
1849 512000, 705600, 768000, 800000
1850};
1851/* maximum number of items for AES and S/PDIF rates for above table */
1852#define SND_RME_RATE_IDX_AES_SPDIF_NUM 12
1853
1854enum snd_rme_domain {
1855 SND_RME_DOMAIN_SYSTEM,
1856 SND_RME_DOMAIN_AES,
1857 SND_RME_DOMAIN_SPDIF
1858};
1859
1860enum snd_rme_clock_status {
1861 SND_RME_CLOCK_NOLOCK,
1862 SND_RME_CLOCK_LOCK,
1863 SND_RME_CLOCK_SYNC
1864};
1865
1866static int snd_rme_read_value(struct snd_usb_audio *chip,
1867 unsigned int item,
1868 u32 *value)
1869{
1870 struct usb_device *dev = chip->dev;
1871 int err;
1872
1873 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1874 item,
1875 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1876 0, 0,
1877 value, sizeof(*value));
1878 if (err < 0)
1879 dev_err(&dev->dev,
1880 "unable to issue vendor read request %d (ret = %d)",
1881 item, err);
1882 return err;
1883}
1884
1885static int snd_rme_get_status1(struct snd_kcontrol *kcontrol,
1886 u32 *status1)
1887{
1888 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
1889 struct snd_usb_audio *chip = list->mixer->chip;
1890 int err;
1891
1892 err = snd_usb_lock_shutdown(chip);
1893 if (err < 0)
1894 return err;
1895 err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, status1);
1896 snd_usb_unlock_shutdown(chip);
1897 return err;
1898}
1899
1900static int snd_rme_rate_get(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_value *ucontrol)
1902{
1903 u32 status1;
1904 u32 rate = 0;
1905 int idx;
1906 int err;
1907
1908 err = snd_rme_get_status1(kcontrol, &status1);
1909 if (err < 0)
1910 return err;
1911 switch (kcontrol->private_value) {
1912 case SND_RME_DOMAIN_SYSTEM:
1913 idx = SND_RME_CLK_SYSTEM(status1);
1914 if (idx < ARRAY_SIZE(snd_rme_rate_table))
1915 rate = snd_rme_rate_table[idx];
1916 break;
1917 case SND_RME_DOMAIN_AES:
1918 idx = SND_RME_CLK_AES(status1);
1919 if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
1920 rate = snd_rme_rate_table[idx];
1921 break;
1922 case SND_RME_DOMAIN_SPDIF:
1923 idx = SND_RME_CLK_SPDIF(status1);
1924 if (idx < SND_RME_RATE_IDX_AES_SPDIF_NUM)
1925 rate = snd_rme_rate_table[idx];
1926 break;
1927 default:
1928 return -EINVAL;
1929 }
1930 ucontrol->value.integer.value[0] = rate;
1931 return 0;
1932}
1933
1934static int snd_rme_sync_state_get(struct snd_kcontrol *kcontrol,
1935 struct snd_ctl_elem_value *ucontrol)
1936{
1937 u32 status1;
1938 int idx = SND_RME_CLOCK_NOLOCK;
1939 int err;
1940
1941 err = snd_rme_get_status1(kcontrol, &status1);
1942 if (err < 0)
1943 return err;
1944 switch (kcontrol->private_value) {
1945 case SND_RME_DOMAIN_AES: /* AES */
1946 if (status1 & SND_RME_CLK_AES_SYNC)
1947 idx = SND_RME_CLOCK_SYNC;
1948 else if (status1 & SND_RME_CLK_AES_LOCK)
1949 idx = SND_RME_CLOCK_LOCK;
1950 break;
1951 case SND_RME_DOMAIN_SPDIF: /* SPDIF */
1952 if (status1 & SND_RME_CLK_SPDIF_SYNC)
1953 idx = SND_RME_CLOCK_SYNC;
1954 else if (status1 & SND_RME_CLK_SPDIF_LOCK)
1955 idx = SND_RME_CLOCK_LOCK;
1956 break;
1957 default:
1958 return -EINVAL;
1959 }
1960 ucontrol->value.enumerated.item[0] = idx;
1961 return 0;
1962}
1963
1964static int snd_rme_spdif_if_get(struct snd_kcontrol *kcontrol,
1965 struct snd_ctl_elem_value *ucontrol)
1966{
1967 u32 status1;
1968 int err;
1969
1970 err = snd_rme_get_status1(kcontrol, &status1);
1971 if (err < 0)
1972 return err;
1973 ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_IF(status1);
1974 return 0;
1975}
1976
1977static int snd_rme_spdif_format_get(struct snd_kcontrol *kcontrol,
1978 struct snd_ctl_elem_value *ucontrol)
1979{
1980 u32 status1;
1981 int err;
1982
1983 err = snd_rme_get_status1(kcontrol, &status1);
1984 if (err < 0)
1985 return err;
1986 ucontrol->value.enumerated.item[0] = SND_RME_SPDIF_FORMAT(status1);
1987 return 0;
1988}
1989
1990static int snd_rme_sync_source_get(struct snd_kcontrol *kcontrol,
1991 struct snd_ctl_elem_value *ucontrol)
1992{
1993 u32 status1;
1994 int err;
1995
1996 err = snd_rme_get_status1(kcontrol, &status1);
1997 if (err < 0)
1998 return err;
1999 ucontrol->value.enumerated.item[0] = SND_RME_CLK_SYNC(status1);
2000 return 0;
2001}
2002
2003static int snd_rme_current_freq_get(struct snd_kcontrol *kcontrol,
2004 struct snd_ctl_elem_value *ucontrol)
2005{
2006 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);
2007 struct snd_usb_audio *chip = list->mixer->chip;
2008 u32 status1;
2009 const u64 num = 104857600000000ULL;
2010 u32 den;
2011 unsigned int freq;
2012 int err;
2013
2014 err = snd_usb_lock_shutdown(chip);
2015 if (err < 0)
2016 return err;
2017 err = snd_rme_read_value(chip, SND_RME_GET_STATUS1, &status1);
2018 if (err < 0)
2019 goto end;
2020 err = snd_rme_read_value(chip, SND_RME_GET_CURRENT_FREQ, &den);
2021 if (err < 0)
2022 goto end;
2023 freq = (den == 0) ? 0 : div64_u64(num, den);
2024 freq <<= SND_RME_CLK_FREQMUL(status1);
2025 ucontrol->value.integer.value[0] = freq;
2026
2027end:
2028 snd_usb_unlock_shutdown(chip);
2029 return err;
2030}
2031
2032static int snd_rme_rate_info(struct snd_kcontrol *kcontrol,
2033 struct snd_ctl_elem_info *uinfo)
2034{
2035 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2036 uinfo->count = 1;
2037 switch (kcontrol->private_value) {
2038 case SND_RME_DOMAIN_SYSTEM:
2039 uinfo->value.integer.min = 32000;
2040 uinfo->value.integer.max = 800000;
2041 break;
2042 case SND_RME_DOMAIN_AES:
2043 case SND_RME_DOMAIN_SPDIF:
2044 default:
2045 uinfo->value.integer.min = 0;
2046 uinfo->value.integer.max = 200000;
2047 }
2048 uinfo->value.integer.step = 0;
2049 return 0;
2050}
2051
2052static int snd_rme_sync_state_info(struct snd_kcontrol *kcontrol,
2053 struct snd_ctl_elem_info *uinfo)
2054{
2055 static const char *const sync_states[] = {
2056 "No Lock", "Lock", "Sync"
2057 };
2058
2059 return snd_ctl_enum_info(uinfo, 1,
2060 ARRAY_SIZE(sync_states), sync_states);
2061}
2062
2063static int snd_rme_spdif_if_info(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_info *uinfo)
2065{
2066 static const char *const spdif_if[] = {
2067 "Coaxial", "Optical"
2068 };
2069
2070 return snd_ctl_enum_info(uinfo, 1,
2071 ARRAY_SIZE(spdif_if), spdif_if);
2072}
2073
2074static int snd_rme_spdif_format_info(struct snd_kcontrol *kcontrol,
2075 struct snd_ctl_elem_info *uinfo)
2076{
2077 static const char *const optical_type[] = {
2078 "Consumer", "Professional"
2079 };
2080
2081 return snd_ctl_enum_info(uinfo, 1,
2082 ARRAY_SIZE(optical_type), optical_type);
2083}
2084
2085static int snd_rme_sync_source_info(struct snd_kcontrol *kcontrol,
2086 struct snd_ctl_elem_info *uinfo)
2087{
2088 static const char *const sync_sources[] = {
2089 "Internal", "AES", "SPDIF", "Internal"
2090 };
2091
2092 return snd_ctl_enum_info(uinfo, 1,
2093 ARRAY_SIZE(sync_sources), sync_sources);
2094}
2095
Takashi Iwai195727e2020-01-03 09:16:57 +01002096static const struct snd_kcontrol_new snd_rme_controls[] = {
Jussi Laakod39f1d62018-10-05 11:00:04 +03002097 {
2098 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2099 .name = "AES Rate",
2100 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2101 .info = snd_rme_rate_info,
2102 .get = snd_rme_rate_get,
2103 .private_value = SND_RME_DOMAIN_AES
2104 },
2105 {
2106 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2107 .name = "AES Sync",
2108 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2109 .info = snd_rme_sync_state_info,
2110 .get = snd_rme_sync_state_get,
2111 .private_value = SND_RME_DOMAIN_AES
2112 },
2113 {
2114 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2115 .name = "SPDIF Rate",
2116 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2117 .info = snd_rme_rate_info,
2118 .get = snd_rme_rate_get,
2119 .private_value = SND_RME_DOMAIN_SPDIF
2120 },
2121 {
2122 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2123 .name = "SPDIF Sync",
2124 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2125 .info = snd_rme_sync_state_info,
2126 .get = snd_rme_sync_state_get,
2127 .private_value = SND_RME_DOMAIN_SPDIF
2128 },
2129 {
2130 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2131 .name = "SPDIF Interface",
2132 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2133 .info = snd_rme_spdif_if_info,
2134 .get = snd_rme_spdif_if_get,
2135 },
2136 {
2137 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2138 .name = "SPDIF Format",
2139 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2140 .info = snd_rme_spdif_format_info,
2141 .get = snd_rme_spdif_format_get,
2142 },
2143 {
2144 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2145 .name = "Sync Source",
2146 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2147 .info = snd_rme_sync_source_info,
2148 .get = snd_rme_sync_source_get
2149 },
2150 {
2151 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2152 .name = "System Rate",
2153 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2154 .info = snd_rme_rate_info,
2155 .get = snd_rme_rate_get,
2156 .private_value = SND_RME_DOMAIN_SYSTEM
2157 },
2158 {
2159 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2160 .name = "Current Frequency",
2161 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2162 .info = snd_rme_rate_info,
2163 .get = snd_rme_current_freq_get
2164 }
2165};
2166
2167static int snd_rme_controls_create(struct usb_mixer_interface *mixer)
2168{
2169 int err, i;
2170
2171 for (i = 0; i < ARRAY_SIZE(snd_rme_controls); ++i) {
2172 err = add_single_ctl_with_resume(mixer, 0,
2173 NULL,
2174 &snd_rme_controls[i],
2175 NULL);
2176 if (err < 0)
2177 return err;
2178 }
2179
2180 return 0;
2181}
2182
Daniel Mack7b1eda22010-03-11 21:13:22 +01002183int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
2184{
Daniel Mack3347b262011-02-11 11:34:12 +00002185 int err = 0;
Daniel Mack7b1eda22010-03-11 21:13:22 +01002186
Takashi Iwaif25ecf82018-05-27 15:18:22 +02002187 err = snd_usb_soundblaster_remote_init(mixer);
2188 if (err < 0)
Daniel Mack7b1eda22010-03-11 21:13:22 +01002189 return err;
2190
Daniel Mack3347b262011-02-11 11:34:12 +00002191 switch (mixer->chip->usb_id) {
Detlef Urband2bb390a2017-02-20 09:47:59 +01002192 /* Tascam US-16x08 */
2193 case USB_ID(0x0644, 0x8047):
2194 err = snd_us16x08_controls_create(mixer);
2195 break;
Daniel Mack3347b262011-02-11 11:34:12 +00002196 case USB_ID(0x041e, 0x3020):
2197 case USB_ID(0x041e, 0x3040):
2198 case USB_ID(0x041e, 0x3042):
Mathieu Bouffard7cdd8d72011-05-18 17:09:17 +02002199 case USB_ID(0x041e, 0x30df):
Daniel Mack3347b262011-02-11 11:34:12 +00002200 case USB_ID(0x041e, 0x3048):
2201 err = snd_audigy2nx_controls_create(mixer);
2202 if (err < 0)
2203 break;
Takashi Iwai74490542019-02-04 16:07:35 +01002204 snd_card_ro_proc_new(mixer->chip->card, "audigy2nx",
2205 mixer, snd_audigy2nx_proc_read);
Daniel Mack3347b262011-02-11 11:34:12 +00002206 break;
Daniel Mack7b1eda22010-03-11 21:13:22 +01002207
Vasily Khoruzhick44832a72013-11-13 13:13:35 +03002208 /* EMU0204 */
2209 case USB_ID(0x041e, 0x3f19):
2210 err = snd_emu0204_controls_create(mixer);
Vasily Khoruzhick44832a72013-11-13 13:13:35 +03002211 break;
2212
Eldad Zack09d8e3a2012-11-28 23:55:40 +01002213 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -05002214 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
Eldad Zack09d8e3a2012-11-28 23:55:40 +01002215 err = snd_c400_create_mixer(mixer);
2216 break;
2217
Daniel Mackd5a0bf62011-05-25 09:09:03 +02002218 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
2219 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
Felix Homanncfe8f972012-04-23 20:24:26 +02002220 err = snd_ftu_create_mixer(mixer);
Daniel Mackd5a0bf62011-05-25 09:09:03 +02002221 break;
2222
Denis Washington1d31aff2012-12-11 11:38:32 +01002223 case USB_ID(0x0b05, 0x1739): /* ASUS Xonar U1 */
2224 case USB_ID(0x0b05, 0x1743): /* ASUS Xonar U1 (2) */
2225 case USB_ID(0x0b05, 0x17a0): /* ASUS Xonar U3 */
Daniel Mack7b1eda22010-03-11 21:13:22 +01002226 err = snd_xonar_u1_controls_create(mixer);
Daniel Mack3347b262011-02-11 11:34:12 +00002227 break;
Daniel Mack7b1eda22010-03-11 21:13:22 +01002228
Przemek Rudy066624c2013-06-27 23:52:33 +02002229 case USB_ID(0x0d8c, 0x0103): /* Audio Advantage Micro II */
2230 err = snd_microii_controls_create(mixer);
2231 break;
2232
Damien Zammitd497a822014-11-12 01:09:54 +11002233 case USB_ID(0x0dba, 0x1000): /* Digidesign Mbox 1 */
2234 err = snd_mbox1_create_sync_switch(mixer);
2235 break;
2236
Daniel Mack3347b262011-02-11 11:34:12 +00002237 case USB_ID(0x17cc, 0x1011): /* Traktor Audio 6 */
Daniel Mack54a8c502011-02-11 11:08:06 +00002238 err = snd_nativeinstruments_create_mixer(mixer,
2239 snd_nativeinstruments_ta6_mixers,
2240 ARRAY_SIZE(snd_nativeinstruments_ta6_mixers));
Daniel Mack3347b262011-02-11 11:34:12 +00002241 break;
Daniel Mack54a8c502011-02-11 11:08:06 +00002242
Daniel Mack3347b262011-02-11 11:34:12 +00002243 case USB_ID(0x17cc, 0x1021): /* Traktor Audio 10 */
Daniel Mack54a8c502011-02-11 11:08:06 +00002244 err = snd_nativeinstruments_create_mixer(mixer,
2245 snd_nativeinstruments_ta10_mixers,
2246 ARRAY_SIZE(snd_nativeinstruments_ta10_mixers));
Daniel Mack3347b262011-02-11 11:34:12 +00002247 break;
Mark Hills7536c302012-04-14 17:19:24 +01002248
2249 case USB_ID(0x200c, 0x1018): /* Electrix Ebox-44 */
Mark Hillsb71dad182012-06-09 13:16:38 +01002250 /* detection is disabled in mixer_maps.c */
2251 err = snd_create_std_mono_table(mixer, ebox44_table);
Mark Hills7536c302012-04-14 17:19:24 +01002252 break;
Chris J Arges76b188c2014-11-12 12:07:02 -06002253
2254 case USB_ID(0x1235, 0x8012): /* Focusrite Scarlett 6i6 */
2255 case USB_ID(0x1235, 0x8002): /* Focusrite Scarlett 8i6 */
2256 case USB_ID(0x1235, 0x8004): /* Focusrite Scarlett 18i6 */
2257 case USB_ID(0x1235, 0x8014): /* Focusrite Scarlett 18i8 */
2258 case USB_ID(0x1235, 0x800c): /* Focusrite Scarlett 18i20 */
2259 err = snd_scarlett_controls_create(mixer);
2260 break;
Ian Douglas Scott388fdb82018-01-16 15:34:50 -08002261
Geoffrey D. Bennett9e4d5c12019-07-29 00:42:45 +09302262 case USB_ID(0x1235, 0x8203): /* Focusrite Scarlett 6i6 2nd Gen */
2263 case USB_ID(0x1235, 0x8204): /* Focusrite Scarlett 18i8 2nd Gen */
2264 case USB_ID(0x1235, 0x8201): /* Focusrite Scarlett 18i20 2nd Gen */
2265 err = snd_scarlett_gen2_controls_create(mixer);
2266 break;
2267
Ian Douglas Scott388fdb82018-01-16 15:34:50 -08002268 case USB_ID(0x041e, 0x323b): /* Creative Sound Blaster E1 */
2269 err = snd_soundblaster_e1_switch_create(mixer);
2270 break;
Takashi Iwai964af632018-04-27 14:23:37 +02002271 case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
2272 err = dell_dock_mixer_init(mixer);
2273 break;
Jussi Laakod39f1d62018-10-05 11:00:04 +03002274
2275 case USB_ID(0x2a39, 0x3fd2): /* RME ADI-2 Pro */
2276 case USB_ID(0x2a39, 0x3fd3): /* RME ADI-2 DAC */
2277 case USB_ID(0x2a39, 0x3fd4): /* RME */
2278 err = snd_rme_controls_create(mixer);
2279 break;
Daniel Mack54a8c502011-02-11 11:08:06 +00002280 }
2281
Daniel Mack3347b262011-02-11 11:34:12 +00002282 return err;
Daniel Mack7b1eda22010-03-11 21:13:22 +01002283}
2284
Takashi Iwai964af632018-04-27 14:23:37 +02002285#ifdef CONFIG_PM
2286void snd_usb_mixer_resume_quirk(struct usb_mixer_interface *mixer)
2287{
2288 switch (mixer->chip->usb_id) {
2289 case USB_ID(0x0bda, 0x4014): /* Dell WD15 dock */
2290 dell_dock_mixer_init(mixer);
2291 break;
2292 }
2293}
2294#endif
2295
Daniel Mack7b1eda22010-03-11 21:13:22 +01002296void snd_usb_mixer_rc_memory_change(struct usb_mixer_interface *mixer,
2297 int unitid)
2298{
2299 if (!mixer->rc_cfg)
2300 return;
2301 /* unit ids specific to Extigy/Audigy 2 NX: */
2302 switch (unitid) {
2303 case 0: /* remote control */
2304 mixer->rc_urb->dev = mixer->chip->dev;
2305 usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
2306 break;
2307 case 4: /* digital in jack */
2308 case 7: /* line in jacks */
2309 case 19: /* speaker out jacks */
2310 case 20: /* headphones out jack */
2311 break;
2312 /* live24ext: 4 = line-in jack */
2313 case 3: /* hp-out jack (may actuate Mute) */
2314 if (mixer->chip->usb_id == USB_ID(0x041e, 0x3040) ||
2315 mixer->chip->usb_id == USB_ID(0x041e, 0x3048))
2316 snd_usb_mixer_notify_id(mixer, mixer->rc_cfg->mute_mixer_id);
2317 break;
2318 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002319 usb_audio_dbg(mixer->chip, "memory change in unknown unit %d\n", unitid);
Daniel Mack7b1eda22010-03-11 21:13:22 +01002320 break;
2321 }
2322}
2323
Anssi Hannula42e31212015-12-13 20:49:58 +02002324static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
Anssi Hannulaeb1a74b2016-09-23 06:43:47 +03002325 struct usb_mixer_elem_info *cval,
Anssi Hannula42e31212015-12-13 20:49:58 +02002326 struct snd_kcontrol *kctl)
2327{
2328 /* Approximation using 10 ranges based on output measurement on hw v1.2.
2329 * This seems close to the cubic mapping e.g. alsamixer uses. */
2330 static const DECLARE_TLV_DB_RANGE(scale,
2331 0, 1, TLV_DB_MINMAX_ITEM(-5300, -4970),
2332 2, 5, TLV_DB_MINMAX_ITEM(-4710, -4160),
2333 6, 7, TLV_DB_MINMAX_ITEM(-3884, -3710),
2334 8, 14, TLV_DB_MINMAX_ITEM(-3443, -2560),
2335 15, 16, TLV_DB_MINMAX_ITEM(-2475, -2324),
2336 17, 19, TLV_DB_MINMAX_ITEM(-2228, -2031),
2337 20, 26, TLV_DB_MINMAX_ITEM(-1910, -1393),
2338 27, 31, TLV_DB_MINMAX_ITEM(-1322, -1032),
2339 32, 40, TLV_DB_MINMAX_ITEM(-968, -490),
2340 41, 50, TLV_DB_MINMAX_ITEM(-441, 0),
2341 );
2342
Anssi Hannulaeb1a74b2016-09-23 06:43:47 +03002343 if (cval->min == 0 && cval->max == 50) {
2344 usb_audio_info(mixer->chip, "applying DragonFly dB scale quirk (0-50 variant)\n");
2345 kctl->tlv.p = scale;
2346 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
2347 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
2348
2349 } else if (cval->min == 0 && cval->max <= 1000) {
2350 /* Some other clearly broken DragonFly variant.
2351 * At least a 0..53 variant (hw v1.0) exists.
2352 */
2353 usb_audio_info(mixer->chip, "ignoring too narrow dB range on a DragonFly device");
2354 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
2355 }
Anssi Hannula42e31212015-12-13 20:49:58 +02002356}
2357
2358void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
2359 struct usb_mixer_elem_info *cval, int unitid,
2360 struct snd_kcontrol *kctl)
2361{
2362 switch (mixer->chip->usb_id) {
2363 case USB_ID(0x21b4, 0x0081): /* AudioQuest DragonFly */
Anssi Hannulaeb1a74b2016-09-23 06:43:47 +03002364 if (unitid == 7 && cval->control == UAC_FU_VOLUME)
2365 snd_dragonfly_quirk_db_scale(mixer, cval, kctl);
Anssi Hannula42e31212015-12-13 20:49:58 +02002366 break;
Takashi Iwai0f174b32017-08-16 14:18:37 +02002367 /* lowest playback value is muted on C-Media devices */
2368 case USB_ID(0x0d8c, 0x000c):
2369 case USB_ID(0x0d8c, 0x0014):
2370 if (strstr(kctl->id.name, "Playback"))
2371 cval->min_mute = 1;
2372 break;
Anssi Hannula42e31212015-12-13 20:49:58 +02002373 }
2374}
2375