1 // SPDX-License-Identifier: GPL-2.0
3 * Focusrite Scarlett 6i6/18i8/18i20 Gen 2 Driver for ALSA
5 * Copyright (c) 2018-2019 by Geoffrey D. Bennett <g at b4.vu>
7 * Based on the Scarlett (Gen 1) Driver for ALSA:
9 * Copyright (c) 2013 by Tobias Hoffmann
10 * Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
11 * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
12 * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
14 * Many codes borrowed from audio.c by
15 * Alan Cox (alan at lxorguk.ukuu.org.uk)
16 * Thomas Sailer (sailer at ife.ee.ethz.ch)
19 * David Henningsson <david.henningsson at canonical.com>
22 /* Mixer Interface for the Focusrite Scarlett 6i6/18i8/18i20 Gen 2 audio
23 * interface. Based on the Gen 1 driver and rewritten.
26 /* The protocol was reverse engineered by looking at the communication
27 * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
28 * (firmware 1083) using usbmon in July-August 2018.
30 * Scarlett 18i8 support added in April 2019.
32 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
33 * for providing usbmon output and testing).
35 * Support for loading mixer volume and mux configuration from the
36 * interface during driver initialisation added in May 2021 (thanks to
37 * Vladimir Sadovnikov for figuring out how).
39 * This ALSA mixer gives access to:
40 * - input, output, mixer-matrix muxes
41 * - 18x10 mixer-matrix gain stages
42 * - gain/volume controls
44 * - line/inst level and pad controls
47 * /--------------\ 18chn 20chn /--------------\
48 * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
49 * \--------------/ | | | | \--------------/
53 * | +---------------+ | |
54 * | \ Matrix Mux / | |
61 * | +------------+ | |
65 * | | 18x10 Gain | | |
67 * | +-----+------+ | |
69 * |18chn |10chn | |20chn
74 * ===========================
75 * +---------------+ +--—------------+
76 * \ Output Mux / \ Capture Mux /
77 * +---+---+---+ +-----+-----+
81 * /--------------\ | | | /--------------\
82 * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
83 * | Hardware out | | \--------------/
86 * +-------------+ Software gain per channel.
87 * | Master Gain |<-- 18i20 only: Switch per channel
88 * +------+------+ to select HW or SW gain control.
92 * | Analogue |<------/
99 #include <linux/slab.h>
100 #include <linux/usb.h>
101 #include <linux/moduleparam.h>
103 #include <sound/control.h>
104 #include <sound/tlv.h>
106 #include "usbaudio.h"
110 #include "mixer_scarlett_gen2.h"
112 /* device_setup value to enable */
113 #define SCARLETT2_ENABLE 0x01
115 /* some gui mixers can't handle negative ctl values */
116 #define SCARLETT2_VOLUME_BIAS 127
118 /* mixer range from -80dB to +6dB in 0.5dB steps */
119 #define SCARLETT2_MIXER_MIN_DB -80
120 #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
121 #define SCARLETT2_MIXER_MAX_DB 6
122 #define SCARLETT2_MIXER_MAX_VALUE \
123 ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
124 #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
126 /* map from (dB + 80) * 2 to mixer value
127 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
129 static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
130 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
131 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
132 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
133 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
134 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
135 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
136 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
137 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
138 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
139 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
140 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
141 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
142 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
146 /* Maximum number of analogue outputs */
147 #define SCARLETT2_ANALOGUE_MAX 10
149 /* Maximum number of level and pad switches */
150 #define SCARLETT2_LEVEL_SWITCH_MAX 2
151 #define SCARLETT2_PAD_SWITCH_MAX 4
153 /* Maximum number of inputs to the mixer */
154 #define SCARLETT2_INPUT_MIX_MAX 18
156 /* Maximum number of outputs from the mixer */
157 #define SCARLETT2_OUTPUT_MIX_MAX 10
159 /* Maximum size of the data in the USB mux assignment message:
160 * 18 inputs, 20 outputs, 18 matrix inputs, 8 spare
162 #define SCARLETT2_MUX_MAX 64
165 * 18 inputs, 20 outputs, 18 matrix inputs
167 #define SCARLETT2_NUM_METERS 56
169 /* Hardware port types:
170 * - None (no input to mux)
178 SCARLETT2_PORT_TYPE_NONE = 0,
179 SCARLETT2_PORT_TYPE_ANALOGUE = 1,
180 SCARLETT2_PORT_TYPE_SPDIF = 2,
181 SCARLETT2_PORT_TYPE_ADAT = 3,
182 SCARLETT2_PORT_TYPE_MIX = 4,
183 SCARLETT2_PORT_TYPE_PCM = 5,
184 SCARLETT2_PORT_TYPE_COUNT = 6,
187 /* Count of total I/O and number available at each sample rate */
189 SCARLETT2_PORT_IN = 0,
190 SCARLETT2_PORT_OUT = 1,
191 SCARLETT2_PORT_OUT_44 = 2,
192 SCARLETT2_PORT_OUT_88 = 3,
193 SCARLETT2_PORT_OUT_176 = 4,
194 SCARLETT2_PORT_DIRNS = 5,
197 /* Dim/Mute buttons on the 18i20 */
198 #define SCARLETT2_DIM_MUTE_COUNT 2
200 static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
204 /* Description of each hardware port type:
205 * - id: hardware ID for this port type
206 * - num: number of sources/destinations of this port type
207 * - src_descr: printf format string for mux input selections
208 * - src_num_offset: added to channel number for the fprintf
209 * - dst_descr: printf format string for mixer controls
211 struct scarlett2_ports {
213 int num[SCARLETT2_PORT_DIRNS];
214 const char * const src_descr;
216 const char * const dst_descr;
219 struct scarlett2_device_info {
220 u8 line_out_hw_vol; /* line out hw volume is sw controlled */
221 u8 level_input_count; /* inputs with level selectable */
222 u8 pad_input_count; /* inputs with pad selectable */
223 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
224 struct scarlett2_ports ports[SCARLETT2_PORT_TYPE_COUNT];
227 struct scarlett2_data {
228 struct usb_mixer_interface *mixer;
229 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
230 struct mutex data_mutex; /* lock access to this data */
231 struct delayed_work work;
232 const struct scarlett2_device_info *info;
233 __u8 bInterfaceNumber;
234 __u8 bEndpointAddress;
235 __u16 wMaxPacketSize;
244 u8 vol[SCARLETT2_ANALOGUE_MAX];
245 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
246 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
247 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
248 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
249 struct snd_kcontrol *sync_ctl;
250 struct snd_kcontrol *master_vol_ctl;
251 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
252 struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
253 u8 mux[SCARLETT2_MUX_MAX];
254 u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
257 /*** Model-specific data ***/
259 static const struct scarlett2_device_info s6i6_gen2_info = {
260 /* The first two analogue inputs can be switched between line
261 * and instrument levels.
263 .level_input_count = 2,
265 /* The first two analogue inputs have an optional pad. */
266 .pad_input_count = 2,
276 [SCARLETT2_PORT_TYPE_NONE] = {
278 .num = { 1, 0, 8, 8, 8 },
281 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
283 .num = { 4, 4, 4, 4, 4 },
284 .src_descr = "Analogue %d",
286 .dst_descr = "Analogue Output %02d Playback"
288 [SCARLETT2_PORT_TYPE_SPDIF] = {
290 .num = { 2, 2, 2, 2, 2 },
291 .src_descr = "S/PDIF %d",
293 .dst_descr = "S/PDIF Output %d Playback"
295 [SCARLETT2_PORT_TYPE_MIX] = {
297 .num = { 10, 18, 18, 18, 18 },
298 .src_descr = "Mix %c",
299 .src_num_offset = 65,
300 .dst_descr = "Mixer Input %02d Capture"
302 [SCARLETT2_PORT_TYPE_PCM] = {
304 .num = { 6, 6, 6, 6, 6 },
305 .src_descr = "PCM %d",
307 .dst_descr = "PCM %02d Capture"
312 static const struct scarlett2_device_info s18i8_gen2_info = {
313 /* The first two analogue inputs can be switched between line
314 * and instrument levels.
316 .level_input_count = 2,
318 /* The first four analogue inputs have an optional pad. */
319 .pad_input_count = 4,
331 [SCARLETT2_PORT_TYPE_NONE] = {
333 .num = { 1, 0, 8, 8, 4 },
336 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
338 .num = { 8, 6, 6, 6, 6 },
339 .src_descr = "Analogue %d",
341 .dst_descr = "Analogue Output %02d Playback"
343 [SCARLETT2_PORT_TYPE_SPDIF] = {
345 .num = { 2, 2, 2, 2, 2 },
346 .src_descr = "S/PDIF %d",
348 .dst_descr = "S/PDIF Output %d Playback"
350 [SCARLETT2_PORT_TYPE_ADAT] = {
352 .num = { 8, 0, 0, 0, 0 },
353 .src_descr = "ADAT %d",
356 [SCARLETT2_PORT_TYPE_MIX] = {
358 .num = { 10, 18, 18, 18, 18 },
359 .src_descr = "Mix %c",
360 .src_num_offset = 65,
361 .dst_descr = "Mixer Input %02d Capture"
363 [SCARLETT2_PORT_TYPE_PCM] = {
365 .num = { 8, 18, 18, 14, 10 },
366 .src_descr = "PCM %d",
368 .dst_descr = "PCM %02d Capture"
373 static const struct scarlett2_device_info s18i20_gen2_info = {
374 /* The analogue line outputs on the 18i20 can be switched
375 * between software and hardware volume control
377 .line_out_hw_vol = 1,
393 [SCARLETT2_PORT_TYPE_NONE] = {
395 .num = { 1, 0, 8, 8, 6 },
398 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
400 .num = { 8, 10, 10, 10, 10 },
401 .src_descr = "Analogue %d",
403 .dst_descr = "Analogue Output %02d Playback"
405 [SCARLETT2_PORT_TYPE_SPDIF] = {
406 /* S/PDIF outputs aren't available at 192kHz
407 * but are included in the USB mux I/O
408 * assignment message anyway
411 .num = { 2, 2, 2, 2, 2 },
412 .src_descr = "S/PDIF %d",
414 .dst_descr = "S/PDIF Output %d Playback"
416 [SCARLETT2_PORT_TYPE_ADAT] = {
418 .num = { 8, 8, 8, 4, 0 },
419 .src_descr = "ADAT %d",
421 .dst_descr = "ADAT Output %d Playback"
423 [SCARLETT2_PORT_TYPE_MIX] = {
425 .num = { 10, 18, 18, 18, 18 },
426 .src_descr = "Mix %c",
427 .src_num_offset = 65,
428 .dst_descr = "Mixer Input %02d Capture"
430 [SCARLETT2_PORT_TYPE_PCM] = {
432 .num = { 20, 18, 18, 14, 10 },
433 .src_descr = "PCM %d",
435 .dst_descr = "PCM %02d Capture"
440 /* get the starting port index number for a given port type/direction */
441 static int scarlett2_get_port_start_num(const struct scarlett2_ports *ports,
442 int direction, int port_type)
446 for (i = 0; i < port_type; i++)
447 num += ports[i].num[direction];
452 /*** USB Interactions ***/
454 /* Notifications from the interface */
455 #define SCARLETT2_USB_NOTIFY_SYNC 0x00000008
456 #define SCARLETT2_USB_NOTIFY_DIM_MUTE 0x00200000
457 #define SCARLETT2_USB_NOTIFY_MONITOR 0x00400000
459 /* Commands for sending/receiving requests/responses */
460 #define SCARLETT2_USB_CMD_INIT 0
461 #define SCARLETT2_USB_CMD_REQ 2
462 #define SCARLETT2_USB_CMD_RESP 3
464 #define SCARLETT2_USB_INIT_1 0x00000000
465 #define SCARLETT2_USB_INIT_2 0x00000002
466 #define SCARLETT2_USB_GET_METER 0x00001001
467 #define SCARLETT2_USB_GET_MIX 0x00002001
468 #define SCARLETT2_USB_SET_MIX 0x00002002
469 #define SCARLETT2_USB_GET_MUX 0x00003001
470 #define SCARLETT2_USB_SET_MUX 0x00003002
471 #define SCARLETT2_USB_GET_SYNC 0x00006004
472 #define SCARLETT2_USB_GET_DATA 0x00800000
473 #define SCARLETT2_USB_SET_DATA 0x00800001
474 #define SCARLETT2_USB_DATA_CMD 0x00800002
476 #define SCARLETT2_USB_CONFIG_SAVE 6
478 #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
479 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
481 /* volume status is read together (matches scarlett2_config_items[]) */
482 struct scarlett2_usb_volume_status {
483 /* dim/mute buttons */
484 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
488 /* software volume setting */
489 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
491 /* actual volume of output inc. dim (-18dB) */
492 s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
494 u8 pad2[SCARLETT2_ANALOGUE_MAX];
496 /* sw (0) or hw (1) controlled */
497 u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
501 /* front panel volume knob */
505 /* Configuration parameters that can be read and written */
507 SCARLETT2_CONFIG_DIM_MUTE = 0,
508 SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
509 SCARLETT2_CONFIG_SW_HW_SWITCH = 2,
510 SCARLETT2_CONFIG_LEVEL_SWITCH = 3,
511 SCARLETT2_CONFIG_PAD_SWITCH = 4,
512 SCARLETT2_CONFIG_COUNT = 5
515 /* Location, size, and activation command number for the configuration
518 struct scarlett2_config {
524 static const struct scarlett2_config
525 scarlett2_config_items[SCARLETT2_CONFIG_COUNT] = {
526 /* Dim/Mute Buttons */
533 /* Line Out Volume */
540 /* SW/HW Volume Switch */
562 /* proprietary request/response format */
563 struct scarlett2_usb_packet {
572 static void scarlett2_fill_request_header(struct scarlett2_data *private,
573 struct scarlett2_usb_packet *req,
574 u32 cmd, u16 req_size)
576 /* sequence must go up by 1 for each request */
577 u16 seq = private->scarlett2_seq++;
579 req->cmd = cpu_to_le32(cmd);
580 req->size = cpu_to_le16(req_size);
581 req->seq = cpu_to_le16(seq);
586 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
589 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
590 SCARLETT2_USB_CMD_REQ,
591 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
592 0, interface, buf, size);
595 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
596 u32 usb_req, void *buf, u16 size)
598 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
600 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
601 0, interface, buf, size);
604 /* Send a proprietary format request to the Scarlett interface */
605 static int scarlett2_usb(
606 struct usb_mixer_interface *mixer, u32 cmd,
607 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
609 struct scarlett2_data *private = mixer->private_data;
610 struct usb_device *dev = mixer->chip->dev;
611 u16 req_buf_size = sizeof(struct scarlett2_usb_packet) + req_size;
612 u16 resp_buf_size = sizeof(struct scarlett2_usb_packet) + resp_size;
613 struct scarlett2_usb_packet *req, *resp = NULL;
616 req = kmalloc(req_buf_size, GFP_KERNEL);
622 resp = kmalloc(resp_buf_size, GFP_KERNEL);
628 mutex_lock(&private->usb_mutex);
630 /* build request message and send it */
632 scarlett2_fill_request_header(private, req, cmd, req_size);
635 memcpy(req->data, req_data, req_size);
637 err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
640 if (err != req_buf_size) {
643 "Scarlett Gen 2 USB request result cmd %x was %d\n",
649 /* send a second message to get the response */
651 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
652 SCARLETT2_USB_CMD_RESP,
653 resp, resp_buf_size);
655 /* validate the response */
657 if (err != resp_buf_size) {
660 "Scarlett Gen 2 USB response result cmd %x was %d "
662 cmd, err, resp_buf_size);
667 /* cmd/seq/size should match except when initialising
668 * seq sent = 1, response = 0
670 if (resp->cmd != req->cmd ||
671 (resp->seq != req->seq &&
672 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
673 resp_size != le16_to_cpu(resp->size) ||
678 "Scarlett Gen 2 USB invalid response; "
679 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
681 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
682 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
683 resp_size, le16_to_cpu(resp->size),
684 le32_to_cpu(resp->error),
685 le32_to_cpu(resp->pad));
690 if (resp_data && resp_size > 0)
691 memcpy(resp_data, resp->data, resp_size);
694 mutex_unlock(&private->usb_mutex);
701 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
702 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
704 __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
706 scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
711 /* Delayed work to save config */
712 static void scarlett2_config_save_work(struct work_struct *work)
714 struct scarlett2_data *private =
715 container_of(work, struct scarlett2_data, work.work);
717 scarlett2_config_save(private->mixer);
720 /* Send a USB message to set a configuration parameter (volume level,
721 * sw/hw volume switch, line/inst level switch, or pad switch)
723 static int scarlett2_usb_set_config(
724 struct usb_mixer_interface *mixer,
725 int config_item_num, int index, int value)
727 const struct scarlett2_config *config_item =
728 &scarlett2_config_items[config_item_num];
736 struct scarlett2_data *private = mixer->private_data;
738 /* Cancel any pending NVRAM save */
739 cancel_delayed_work_sync(&private->work);
741 /* Send the configuration parameter data */
742 req.offset = cpu_to_le32(config_item->offset + index * config_item->size);
743 req.bytes = cpu_to_le32(config_item->size);
744 req.value = cpu_to_le32(value);
745 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
746 &req, sizeof(u32) * 2 + config_item->size,
751 /* Activate the change */
752 req2 = cpu_to_le32(config_item->activate);
753 err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
754 &req2, sizeof(req2), NULL, 0);
758 /* Schedule the change to be written to NVRAM */
759 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
764 /* Send a USB message to get data; result placed in *buf */
765 static int scarlett2_usb_get(
766 struct usb_mixer_interface *mixer,
767 int offset, void *buf, int size)
774 req.offset = cpu_to_le32(offset);
775 req.size = cpu_to_le32(size);
776 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
777 &req, sizeof(req), buf, size);
780 /* Send a USB message to get configuration parameters; result placed in *buf */
781 static int scarlett2_usb_get_config(
782 struct usb_mixer_interface *mixer,
783 int config_item_num, int count, void *buf)
785 const struct scarlett2_config *config_item =
786 &scarlett2_config_items[config_item_num];
787 int size = config_item->size * count;
789 return scarlett2_usb_get(mixer, config_item->offset, buf, size);
792 /* Send a USB message to get sync status; result placed in *sync */
793 static int scarlett2_usb_get_sync_status(
794 struct usb_mixer_interface *mixer,
800 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
801 NULL, 0, &data, sizeof(data));
809 /* Send a USB message to get volume status; result placed in *buf */
810 static int scarlett2_usb_get_volume_status(
811 struct usb_mixer_interface *mixer,
812 struct scarlett2_usb_volume_status *buf)
814 return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
818 /* Send a USB message to get the volumes for all inputs of one mix
819 * and put the values into private->mix[]
821 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
824 struct scarlett2_data *private = mixer->private_data;
825 const struct scarlett2_device_info *info = private->info;
828 info->ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
836 __le16 data[SCARLETT2_INPUT_MIX_MAX];
838 req.mix_num = cpu_to_le16(mix_num);
839 req.count = cpu_to_le16(num_mixer_in);
841 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
843 data, num_mixer_in * sizeof(u16));
847 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
848 u16 mixer_value = le16_to_cpu(data[i]);
850 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
851 if (scarlett2_mixer_values[k] >= mixer_value)
853 if (k == SCARLETT2_MIXER_VALUE_COUNT)
854 k = SCARLETT2_MIXER_MAX_VALUE;
861 /* Send a USB message to set the volumes for all inputs of one mix
862 * (values obtained from private->mix[])
864 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
867 struct scarlett2_data *private = mixer->private_data;
868 const struct scarlett2_device_info *info = private->info;
872 __le16 data[SCARLETT2_INPUT_MIX_MAX];
877 info->ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
879 req.mix_num = cpu_to_le16(mix_num);
881 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
882 req.data[i] = cpu_to_le16(
883 scarlett2_mixer_values[private->mix[j]]
886 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
887 &req, (num_mixer_in + 1) * sizeof(u16),
891 /* Convert a port number index (per info->ports) to a hardware ID */
892 static u32 scarlett2_mux_src_num_to_id(const struct scarlett2_ports *ports,
898 port_type < SCARLETT2_PORT_TYPE_COUNT;
900 if (num < ports[port_type].num[SCARLETT2_PORT_IN])
901 return ports[port_type].id | num;
902 num -= ports[port_type].num[SCARLETT2_PORT_IN];
909 /* Convert a hardware ID to a port number index */
910 static u32 scarlett2_mux_id_to_num(const struct scarlett2_ports *ports,
918 port_type < SCARLETT2_PORT_TYPE_COUNT;
920 struct scarlett2_ports port = ports[port_type];
921 int count = port.num[direction];
923 if (id >= port.id && id < port.id + count)
924 return port_num + id - port.id;
932 /* Convert one mux entry from the interface and load into private->mux[] */
933 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
936 const struct scarlett2_device_info *info = private->info;
937 const struct scarlett2_ports *ports = info->ports;
939 int dst_idx, src_idx;
941 dst_idx = scarlett2_mux_id_to_num(ports, SCARLETT2_PORT_OUT,
946 if (dst_idx >= private->num_mux_dsts) {
947 usb_audio_err(private->mixer->chip,
948 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
949 mux_entry, dst_idx, private->num_mux_dsts);
953 src_idx = scarlett2_mux_id_to_num(ports, SCARLETT2_PORT_IN,
958 if (src_idx >= private->num_mux_srcs) {
959 usb_audio_err(private->mixer->chip,
960 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
961 mux_entry, src_idx, private->num_mux_srcs);
965 private->mux[dst_idx] = src_idx;
968 /* Send USB message to get mux inputs and then populate private->mux[] */
969 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
971 struct scarlett2_data *private = mixer->private_data;
972 int count = private->num_mux_dsts;
980 __le32 data[SCARLETT2_MUX_MAX];
983 req.count = cpu_to_le16(count);
985 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
987 data, count * sizeof(u32));
991 for (i = 0; i < count; i++)
992 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
997 /* Send USB messages to set mux inputs */
998 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
1000 struct scarlett2_data *private = mixer->private_data;
1001 const struct scarlett2_device_info *info = private->info;
1002 const struct scarlett2_ports *ports = info->ports;
1003 int rate, port_dir_rate;
1005 static const int assignment_order[SCARLETT2_PORT_TYPE_COUNT] = {
1006 SCARLETT2_PORT_TYPE_PCM,
1007 SCARLETT2_PORT_TYPE_ANALOGUE,
1008 SCARLETT2_PORT_TYPE_SPDIF,
1009 SCARLETT2_PORT_TYPE_ADAT,
1010 SCARLETT2_PORT_TYPE_MIX,
1011 SCARLETT2_PORT_TYPE_NONE,
1017 __le32 data[SCARLETT2_MUX_MAX];
1022 /* mux settings for each rate */
1023 for (rate = 0, port_dir_rate = SCARLETT2_PORT_OUT_44;
1024 port_dir_rate <= SCARLETT2_PORT_OUT_176;
1025 rate++, port_dir_rate++) {
1026 int order_num, i, err;
1028 req.num = cpu_to_le16(rate);
1030 for (order_num = 0, i = 0;
1031 order_num < SCARLETT2_PORT_TYPE_COUNT;
1033 int port_type = assignment_order[order_num];
1034 int j = scarlett2_get_port_start_num(ports,
1037 int port_id = ports[port_type].id;
1041 channel < ports[port_type].num[port_dir_rate];
1042 channel++, i++, j++)
1043 /* lower 12 bits for the destination and
1044 * next 12 bits for the source
1046 req.data[i] = !port_id
1051 scarlett2_mux_src_num_to_id(
1052 ports, private->mux[j]
1057 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1058 &req, (i + 1) * sizeof(u32),
1067 /* Send USB message to get meter levels */
1068 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1076 u32 resp[SCARLETT2_NUM_METERS];
1080 req.num_meters = cpu_to_le16(SCARLETT2_NUM_METERS);
1081 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
1082 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
1083 &req, sizeof(req), resp, sizeof(resp));
1087 /* copy, convert to u16 */
1088 for (i = 0; i < SCARLETT2_NUM_METERS; i++)
1089 levels[i] = resp[i];
1094 /*** Control Functions ***/
1096 /* helper function to create a new control */
1097 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
1098 const struct snd_kcontrol_new *ncontrol,
1099 int index, int channels, const char *name,
1100 struct snd_kcontrol **kctl_return)
1102 struct snd_kcontrol *kctl;
1103 struct usb_mixer_elem_info *elem;
1106 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
1110 elem->head.mixer = mixer;
1111 elem->control = index;
1112 elem->head.id = index;
1113 elem->channels = channels;
1115 kctl = snd_ctl_new1(ncontrol, elem);
1120 kctl->private_free = snd_usb_mixer_elem_free;
1122 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
1124 err = snd_usb_mixer_add_control(&elem->head, kctl);
1129 *kctl_return = kctl;
1134 /*** Sync Control ***/
1136 /* Update sync control after receiving notification that the status
1139 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
1141 struct scarlett2_data *private = mixer->private_data;
1143 private->sync_updated = 0;
1144 return scarlett2_usb_get_sync_status(mixer, &private->sync);
1147 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
1148 struct snd_ctl_elem_info *uinfo)
1150 static const char *texts[2] = {
1151 "Unlocked", "Locked"
1153 return snd_ctl_enum_info(uinfo, 1, 2, texts);
1156 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
1157 struct snd_ctl_elem_value *ucontrol)
1159 struct usb_mixer_elem_info *elem = kctl->private_data;
1160 struct usb_mixer_interface *mixer = elem->head.mixer;
1161 struct scarlett2_data *private = mixer->private_data;
1163 mutex_lock(&private->data_mutex);
1164 if (private->sync_updated)
1165 scarlett2_update_sync(mixer);
1166 ucontrol->value.enumerated.item[0] = private->sync;
1167 mutex_unlock(&private->data_mutex);
1172 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
1173 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1174 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1176 .info = scarlett2_sync_ctl_info,
1177 .get = scarlett2_sync_ctl_get
1180 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
1182 struct scarlett2_data *private = mixer->private_data;
1184 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
1185 0, 1, "Sync Status", &private->sync_ctl);
1188 /*** Analogue Line Out Volume Controls ***/
1190 /* Update hardware volume controls after receiving notification that
1193 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
1195 struct scarlett2_data *private = mixer->private_data;
1196 const struct scarlett2_device_info *info = private->info;
1197 const struct scarlett2_ports *ports = info->ports;
1198 struct scarlett2_usb_volume_status volume_status;
1200 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1203 private->vol_updated = 0;
1205 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1209 private->master_vol = clamp(
1210 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1211 0, SCARLETT2_VOLUME_BIAS);
1213 for (i = 0; i < num_line_out; i++) {
1214 if (private->vol_sw_hw_switch[i])
1215 private->vol[i] = private->master_vol;
1218 if (info->line_out_hw_vol)
1219 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
1220 private->dim_mute[i] = !!volume_status.dim_mute[i];
1225 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1226 struct snd_ctl_elem_info *uinfo)
1228 struct usb_mixer_elem_info *elem = kctl->private_data;
1230 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1231 uinfo->count = elem->channels;
1232 uinfo->value.integer.min = 0;
1233 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1234 uinfo->value.integer.step = 1;
1238 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1239 struct snd_ctl_elem_value *ucontrol)
1241 struct usb_mixer_elem_info *elem = kctl->private_data;
1242 struct usb_mixer_interface *mixer = elem->head.mixer;
1243 struct scarlett2_data *private = mixer->private_data;
1245 mutex_lock(&private->data_mutex);
1246 if (private->vol_updated)
1247 scarlett2_update_volumes(mixer);
1248 mutex_unlock(&private->data_mutex);
1250 ucontrol->value.integer.value[0] = private->master_vol;
1254 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1255 struct snd_ctl_elem_value *ucontrol)
1257 struct usb_mixer_elem_info *elem = kctl->private_data;
1258 struct usb_mixer_interface *mixer = elem->head.mixer;
1259 struct scarlett2_data *private = mixer->private_data;
1260 int index = elem->control;
1262 mutex_lock(&private->data_mutex);
1263 if (private->vol_updated)
1264 scarlett2_update_volumes(mixer);
1265 mutex_unlock(&private->data_mutex);
1267 ucontrol->value.integer.value[0] = private->vol[index];
1271 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1272 struct snd_ctl_elem_value *ucontrol)
1274 struct usb_mixer_elem_info *elem = kctl->private_data;
1275 struct usb_mixer_interface *mixer = elem->head.mixer;
1276 struct scarlett2_data *private = mixer->private_data;
1277 int index = elem->control;
1278 int oval, val, err = 0;
1280 mutex_lock(&private->data_mutex);
1282 oval = private->vol[index];
1283 val = ucontrol->value.integer.value[0];
1288 private->vol[index] = val;
1289 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1290 index, val - SCARLETT2_VOLUME_BIAS);
1295 mutex_unlock(&private->data_mutex);
1299 static const DECLARE_TLV_DB_MINMAX(
1300 db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1303 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1304 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1305 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1306 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1308 .info = scarlett2_volume_ctl_info,
1309 .get = scarlett2_master_volume_ctl_get,
1310 .private_value = 0, /* max value */
1311 .tlv = { .p = db_scale_scarlett2_gain }
1314 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1315 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1316 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1317 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1319 .info = scarlett2_volume_ctl_info,
1320 .get = scarlett2_volume_ctl_get,
1321 .put = scarlett2_volume_ctl_put,
1322 .private_value = 0, /* max value */
1323 .tlv = { .p = db_scale_scarlett2_gain }
1326 /*** HW/SW Volume Switch Controls ***/
1328 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
1329 struct snd_ctl_elem_info *uinfo)
1331 static const char *const values[2] = {
1335 return snd_ctl_enum_info(uinfo, 1, 2, values);
1338 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
1339 struct snd_ctl_elem_value *ucontrol)
1341 struct usb_mixer_elem_info *elem = kctl->private_data;
1342 struct scarlett2_data *private = elem->head.mixer->private_data;
1344 ucontrol->value.enumerated.item[0] =
1345 private->vol_sw_hw_switch[elem->control];
1349 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
1350 struct snd_ctl_elem_value *ucontrol)
1352 struct usb_mixer_elem_info *elem = kctl->private_data;
1353 struct usb_mixer_interface *mixer = elem->head.mixer;
1354 struct scarlett2_data *private = mixer->private_data;
1356 int index = elem->control;
1357 int oval, val, err = 0;
1359 mutex_lock(&private->data_mutex);
1361 oval = private->vol_sw_hw_switch[index];
1362 val = !!ucontrol->value.enumerated.item[0];
1367 private->vol_sw_hw_switch[index] = val;
1369 /* Change access mode to RO (hardware controlled volume)
1370 * or RW (software controlled volume)
1373 private->vol_ctls[index]->vd[0].access &=
1374 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1376 private->vol_ctls[index]->vd[0].access |=
1377 SNDRV_CTL_ELEM_ACCESS_WRITE;
1379 /* Reset volume to master volume */
1380 private->vol[index] = private->master_vol;
1382 /* Set SW volume to current HW volume */
1383 err = scarlett2_usb_set_config(
1384 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1385 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
1389 /* Notify of RO/RW change */
1390 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_INFO,
1391 &private->vol_ctls[index]->id);
1393 /* Send SW/HW switch change to the device */
1394 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
1400 mutex_unlock(&private->data_mutex);
1404 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
1405 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1407 .info = scarlett2_sw_hw_enum_ctl_info,
1408 .get = scarlett2_sw_hw_enum_ctl_get,
1409 .put = scarlett2_sw_hw_enum_ctl_put,
1412 /*** Line Level/Instrument Level Switch Controls ***/
1414 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
1415 struct snd_ctl_elem_info *uinfo)
1417 static const char *const values[2] = {
1421 return snd_ctl_enum_info(uinfo, 1, 2, values);
1424 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
1425 struct snd_ctl_elem_value *ucontrol)
1427 struct usb_mixer_elem_info *elem = kctl->private_data;
1428 struct scarlett2_data *private = elem->head.mixer->private_data;
1430 ucontrol->value.enumerated.item[0] =
1431 private->level_switch[elem->control];
1435 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
1436 struct snd_ctl_elem_value *ucontrol)
1438 struct usb_mixer_elem_info *elem = kctl->private_data;
1439 struct usb_mixer_interface *mixer = elem->head.mixer;
1440 struct scarlett2_data *private = mixer->private_data;
1442 int index = elem->control;
1443 int oval, val, err = 0;
1445 mutex_lock(&private->data_mutex);
1447 oval = private->level_switch[index];
1448 val = !!ucontrol->value.enumerated.item[0];
1453 private->level_switch[index] = val;
1455 /* Send switch change to the device */
1456 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
1462 mutex_unlock(&private->data_mutex);
1466 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
1467 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1469 .info = scarlett2_level_enum_ctl_info,
1470 .get = scarlett2_level_enum_ctl_get,
1471 .put = scarlett2_level_enum_ctl_put,
1474 /*** Pad Switch Controls ***/
1476 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
1477 struct snd_ctl_elem_value *ucontrol)
1479 struct usb_mixer_elem_info *elem = kctl->private_data;
1480 struct scarlett2_data *private = elem->head.mixer->private_data;
1482 ucontrol->value.integer.value[0] =
1483 private->pad_switch[elem->control];
1487 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
1488 struct snd_ctl_elem_value *ucontrol)
1490 struct usb_mixer_elem_info *elem = kctl->private_data;
1491 struct usb_mixer_interface *mixer = elem->head.mixer;
1492 struct scarlett2_data *private = mixer->private_data;
1494 int index = elem->control;
1495 int oval, val, err = 0;
1497 mutex_lock(&private->data_mutex);
1499 oval = private->pad_switch[index];
1500 val = !!ucontrol->value.integer.value[0];
1505 private->pad_switch[index] = val;
1507 /* Send switch change to the device */
1508 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
1514 mutex_unlock(&private->data_mutex);
1518 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
1519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1521 .info = snd_ctl_boolean_mono_info,
1522 .get = scarlett2_pad_ctl_get,
1523 .put = scarlett2_pad_ctl_put,
1526 /*** Dim/Mute Controls ***/
1528 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
1529 struct snd_ctl_elem_value *ucontrol)
1531 struct usb_mixer_elem_info *elem = kctl->private_data;
1532 struct usb_mixer_interface *mixer = elem->head.mixer;
1533 struct scarlett2_data *private = mixer->private_data;
1535 mutex_lock(&private->data_mutex);
1536 if (private->vol_updated)
1537 scarlett2_update_volumes(mixer);
1538 mutex_unlock(&private->data_mutex);
1540 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
1544 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
1545 struct snd_ctl_elem_value *ucontrol)
1547 struct usb_mixer_elem_info *elem = kctl->private_data;
1548 struct usb_mixer_interface *mixer = elem->head.mixer;
1549 struct scarlett2_data *private = mixer->private_data;
1551 int index = elem->control;
1552 int oval, val, err = 0;
1554 mutex_lock(&private->data_mutex);
1556 oval = private->dim_mute[index];
1557 val = !!ucontrol->value.integer.value[0];
1562 private->dim_mute[index] = val;
1564 /* Send switch change to the device */
1565 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
1571 mutex_unlock(&private->data_mutex);
1575 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
1576 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1578 .info = snd_ctl_boolean_mono_info,
1579 .get = scarlett2_dim_mute_ctl_get,
1580 .put = scarlett2_dim_mute_ctl_put
1583 /*** Create the analogue output controls ***/
1585 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
1587 struct scarlett2_data *private = mixer->private_data;
1588 const struct scarlett2_device_info *info = private->info;
1589 const struct scarlett2_ports *ports = info->ports;
1591 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1593 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1595 /* Add R/O HW volume control */
1596 if (info->line_out_hw_vol) {
1597 snprintf(s, sizeof(s), "Master HW Playback Volume");
1598 err = scarlett2_add_new_ctl(mixer,
1599 &scarlett2_master_volume_ctl,
1600 0, 1, s, &private->master_vol_ctl);
1605 /* Add volume controls */
1606 for (i = 0; i < num_line_out; i++) {
1609 if (info->line_out_descrs[i])
1610 snprintf(s, sizeof(s),
1611 "Line %02d (%s) Playback Volume",
1612 i + 1, info->line_out_descrs[i]);
1614 snprintf(s, sizeof(s),
1615 "Line %02d Playback Volume",
1617 err = scarlett2_add_new_ctl(mixer,
1618 &scarlett2_line_out_volume_ctl,
1619 i, 1, s, &private->vol_ctls[i]);
1623 /* Make the fader read-only if the SW/HW switch is set to HW */
1624 if (private->vol_sw_hw_switch[i])
1625 private->vol_ctls[i]->vd[0].access &=
1626 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1629 if (info->line_out_hw_vol) {
1630 snprintf(s, sizeof(s),
1631 "Line Out %02d Volume Control Playback Enum",
1633 err = scarlett2_add_new_ctl(mixer,
1634 &scarlett2_sw_hw_enum_ctl,
1641 /* Add dim/mute controls */
1642 if (info->line_out_hw_vol)
1643 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
1644 err = scarlett2_add_new_ctl(
1645 mixer, &scarlett2_dim_mute_ctl,
1646 i, 1, scarlett2_dim_mute_names[i],
1647 &private->dim_mute_ctls[i]);
1655 /*** Create the analogue input controls ***/
1657 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
1659 struct scarlett2_data *private = mixer->private_data;
1660 const struct scarlett2_device_info *info = private->info;
1662 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1663 const char *fmt = "Line In %d %s Capture %s";
1665 /* Add input level (line/inst) controls */
1666 for (i = 0; i < info->level_input_count; i++) {
1667 snprintf(s, sizeof(s), fmt, i + 1, "Level", "Enum");
1668 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
1674 /* Add input pad controls */
1675 for (i = 0; i < info->pad_input_count; i++) {
1676 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
1677 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
1686 /*** Mixer Volume Controls ***/
1688 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
1689 struct snd_ctl_elem_info *uinfo)
1691 struct usb_mixer_elem_info *elem = kctl->private_data;
1693 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1694 uinfo->count = elem->channels;
1695 uinfo->value.integer.min = 0;
1696 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
1697 uinfo->value.integer.step = 1;
1701 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
1702 struct snd_ctl_elem_value *ucontrol)
1704 struct usb_mixer_elem_info *elem = kctl->private_data;
1705 struct scarlett2_data *private = elem->head.mixer->private_data;
1707 ucontrol->value.integer.value[0] = private->mix[elem->control];
1711 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
1712 struct snd_ctl_elem_value *ucontrol)
1714 struct usb_mixer_elem_info *elem = kctl->private_data;
1715 struct usb_mixer_interface *mixer = elem->head.mixer;
1716 struct scarlett2_data *private = mixer->private_data;
1717 const struct scarlett2_device_info *info = private->info;
1718 const struct scarlett2_ports *ports = info->ports;
1719 int oval, val, num_mixer_in, mix_num, err = 0;
1720 int index = elem->control;
1722 mutex_lock(&private->data_mutex);
1724 oval = private->mix[index];
1725 val = ucontrol->value.integer.value[0];
1726 num_mixer_in = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
1727 mix_num = index / num_mixer_in;
1732 private->mix[index] = val;
1733 err = scarlett2_usb_set_mix(mixer, mix_num);
1738 mutex_unlock(&private->data_mutex);
1742 static const DECLARE_TLV_DB_MINMAX(
1743 db_scale_scarlett2_mixer,
1744 SCARLETT2_MIXER_MIN_DB * 100,
1745 SCARLETT2_MIXER_MAX_DB * 100
1748 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
1749 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1750 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1751 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1753 .info = scarlett2_mixer_ctl_info,
1754 .get = scarlett2_mixer_ctl_get,
1755 .put = scarlett2_mixer_ctl_put,
1756 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
1757 .tlv = { .p = db_scale_scarlett2_mixer }
1760 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
1762 struct scarlett2_data *private = mixer->private_data;
1763 const struct scarlett2_ports *ports = private->info->ports;
1766 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1768 int num_inputs = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
1769 int num_outputs = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_IN];
1771 for (i = 0, index = 0; i < num_outputs; i++)
1772 for (j = 0; j < num_inputs; j++, index++) {
1773 snprintf(s, sizeof(s),
1774 "Mix %c Input %02d Playback Volume",
1776 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
1785 /*** Mux Source Selection Controls ***/
1787 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
1788 struct snd_ctl_elem_info *uinfo)
1790 struct usb_mixer_elem_info *elem = kctl->private_data;
1791 struct scarlett2_data *private = elem->head.mixer->private_data;
1792 const struct scarlett2_ports *ports = private->info->ports;
1793 unsigned int item = uinfo->value.enumerated.item;
1794 int items = private->num_mux_srcs;
1797 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1798 uinfo->count = elem->channels;
1799 uinfo->value.enumerated.items = items;
1802 item = uinfo->value.enumerated.item = items - 1;
1805 port_type < SCARLETT2_PORT_TYPE_COUNT;
1807 if (item < ports[port_type].num[SCARLETT2_PORT_IN]) {
1808 sprintf(uinfo->value.enumerated.name,
1809 ports[port_type].src_descr,
1810 item + ports[port_type].src_num_offset);
1813 item -= ports[port_type].num[SCARLETT2_PORT_IN];
1819 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
1820 struct snd_ctl_elem_value *ucontrol)
1822 struct usb_mixer_elem_info *elem = kctl->private_data;
1823 struct scarlett2_data *private = elem->head.mixer->private_data;
1825 ucontrol->value.enumerated.item[0] = private->mux[elem->control];
1829 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
1830 struct snd_ctl_elem_value *ucontrol)
1832 struct usb_mixer_elem_info *elem = kctl->private_data;
1833 struct usb_mixer_interface *mixer = elem->head.mixer;
1834 struct scarlett2_data *private = mixer->private_data;
1835 int index = elem->control;
1836 int oval, val, err = 0;
1838 mutex_lock(&private->data_mutex);
1840 oval = private->mux[index];
1841 val = min(ucontrol->value.enumerated.item[0],
1842 private->num_mux_srcs - 1U);
1847 private->mux[index] = val;
1848 err = scarlett2_usb_set_mux(mixer);
1853 mutex_unlock(&private->data_mutex);
1857 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
1858 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1860 .info = scarlett2_mux_src_enum_ctl_info,
1861 .get = scarlett2_mux_src_enum_ctl_get,
1862 .put = scarlett2_mux_src_enum_ctl_put,
1865 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
1867 struct scarlett2_data *private = mixer->private_data;
1868 const struct scarlett2_ports *ports = private->info->ports;
1869 int port_type, channel, i;
1871 for (i = 0, port_type = 0;
1872 port_type < SCARLETT2_PORT_TYPE_COUNT;
1875 channel < ports[port_type].num[SCARLETT2_PORT_OUT];
1878 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1879 const char *const descr = ports[port_type].dst_descr;
1881 snprintf(s, sizeof(s) - 5, descr, channel + 1);
1884 err = scarlett2_add_new_ctl(mixer,
1885 &scarlett2_mux_src_enum_ctl,
1895 /*** Meter Controls ***/
1897 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
1898 struct snd_ctl_elem_info *uinfo)
1900 struct usb_mixer_elem_info *elem = kctl->private_data;
1902 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1903 uinfo->count = elem->channels;
1904 uinfo->value.integer.min = 0;
1905 uinfo->value.integer.max = 4095;
1906 uinfo->value.integer.step = 1;
1910 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
1911 struct snd_ctl_elem_value *ucontrol)
1913 struct usb_mixer_elem_info *elem = kctl->private_data;
1914 u16 meter_levels[SCARLETT2_NUM_METERS];
1917 err = scarlett2_usb_get_meter_levels(elem->head.mixer, meter_levels);
1921 for (i = 0; i < elem->channels; i++)
1922 ucontrol->value.integer.value[i] = meter_levels[i];
1927 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
1928 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1929 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1931 .info = scarlett2_meter_ctl_info,
1932 .get = scarlett2_meter_ctl_get
1935 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
1937 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
1938 0, SCARLETT2_NUM_METERS,
1939 "Level Meter", NULL);
1942 /*** Cleanup/Suspend Callbacks ***/
1944 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
1946 struct scarlett2_data *private = mixer->private_data;
1948 cancel_delayed_work_sync(&private->work);
1950 mixer->private_data = NULL;
1953 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
1955 struct scarlett2_data *private = mixer->private_data;
1957 if (cancel_delayed_work_sync(&private->work))
1958 scarlett2_config_save(private->mixer);
1961 /*** Initialisation ***/
1963 static void scarlett2_count_mux_io(struct scarlett2_data *private)
1965 const struct scarlett2_ports *ports = private->info->ports;
1966 int port_type, srcs = 0, dsts = 0;
1969 port_type < SCARLETT2_PORT_TYPE_COUNT;
1971 srcs += ports[port_type].num[SCARLETT2_PORT_IN];
1972 dsts += ports[port_type].num[SCARLETT2_PORT_OUT_44];
1975 private->num_mux_srcs = srcs;
1976 private->num_mux_dsts = dsts;
1979 /* Look through the interface descriptors for the Focusrite Control
1980 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
1981 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
1984 static int scarlett2_find_fc_interface(struct usb_device *dev,
1985 struct scarlett2_data *private)
1987 struct usb_host_config *config = dev->actconfig;
1990 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1991 struct usb_interface *intf = config->interface[i];
1992 struct usb_interface_descriptor *desc =
1993 &intf->altsetting[0].desc;
1994 struct usb_endpoint_descriptor *epd;
1996 if (desc->bInterfaceClass != 255)
1999 epd = get_endpoint(intf->altsetting, 0);
2000 private->bInterfaceNumber = desc->bInterfaceNumber;
2001 private->bEndpointAddress = epd->bEndpointAddress &
2002 USB_ENDPOINT_NUMBER_MASK;
2003 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
2004 private->bInterval = epd->bInterval;
2011 /* Initialise private data */
2012 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
2013 const struct scarlett2_device_info *info)
2015 struct scarlett2_data *private =
2016 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
2021 mutex_init(&private->usb_mutex);
2022 mutex_init(&private->data_mutex);
2023 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
2025 mixer->private_data = private;
2026 mixer->private_free = scarlett2_private_free;
2027 mixer->private_suspend = scarlett2_private_suspend;
2029 private->info = info;
2030 scarlett2_count_mux_io(private);
2031 private->scarlett2_seq = 0;
2032 private->mixer = mixer;
2034 return scarlett2_find_fc_interface(mixer->chip->dev, private);
2037 /* Cargo cult proprietary initialisation sequence */
2038 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
2040 struct usb_device *dev = mixer->chip->dev;
2041 struct scarlett2_data *private = mixer->private_data;
2045 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
2049 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
2050 SCARLETT2_USB_CMD_INIT, buf, sizeof(buf));
2055 private->scarlett2_seq = 1;
2056 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
2061 private->scarlett2_seq = 1;
2062 return scarlett2_usb(mixer, SCARLETT2_USB_INIT_2, NULL, 0, NULL, 84);
2065 /* Read configuration from the interface on start */
2066 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
2068 struct scarlett2_data *private = mixer->private_data;
2069 const struct scarlett2_device_info *info = private->info;
2070 const struct scarlett2_ports *ports = info->ports;
2072 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
2074 ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_IN];
2075 struct scarlett2_usb_volume_status volume_status;
2078 if (info->level_input_count) {
2079 err = scarlett2_usb_get_config(
2081 SCARLETT2_CONFIG_LEVEL_SWITCH,
2082 info->level_input_count,
2083 private->level_switch);
2088 if (info->pad_input_count) {
2089 err = scarlett2_usb_get_config(
2091 SCARLETT2_CONFIG_PAD_SWITCH,
2092 info->pad_input_count,
2093 private->pad_switch);
2098 err = scarlett2_update_sync(mixer);
2102 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
2106 private->master_vol = clamp(
2107 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
2108 0, SCARLETT2_VOLUME_BIAS);
2110 for (i = 0; i < num_line_out; i++) {
2113 private->vol_sw_hw_switch[i] =
2114 info->line_out_hw_vol
2115 && volume_status.sw_hw_switch[i];
2117 volume = private->vol_sw_hw_switch[i]
2118 ? volume_status.master_vol
2119 : volume_status.sw_vol[i];
2120 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
2121 0, SCARLETT2_VOLUME_BIAS);
2122 private->vol[i] = volume;
2125 if (info->line_out_hw_vol)
2126 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
2127 private->dim_mute[i] = !!volume_status.dim_mute[i];
2129 for (i = 0; i < num_mixer_out; i++) {
2130 err = scarlett2_usb_get_mix(mixer, i);
2135 return scarlett2_usb_get_mux(mixer);
2138 /* Notify on sync change */
2139 static void scarlett2_notify_sync(
2140 struct usb_mixer_interface *mixer)
2142 struct scarlett2_data *private = mixer->private_data;
2144 private->sync_updated = 1;
2146 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2147 &private->sync_ctl->id);
2150 /* Notify on monitor change */
2151 static void scarlett2_notify_monitor(
2152 struct usb_mixer_interface *mixer)
2154 struct scarlett2_data *private = mixer->private_data;
2155 const struct scarlett2_device_info *info = private->info;
2156 const struct scarlett2_ports *ports = info->ports;
2158 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
2161 /* if line_out_hw_vol is 0, there are no controls to update */
2162 if (!info->line_out_hw_vol)
2165 private->vol_updated = 1;
2167 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2168 &private->master_vol_ctl->id);
2170 for (i = 0; i < num_line_out; i++) {
2171 if (!private->vol_sw_hw_switch[i])
2173 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2174 &private->vol_ctls[i]->id);
2178 /* Notify on dim/mute change */
2179 static void scarlett2_notify_dim_mute(
2180 struct usb_mixer_interface *mixer)
2182 struct scarlett2_data *private = mixer->private_data;
2183 const struct scarlett2_device_info *info = private->info;
2186 private->vol_updated = 1;
2188 if (!info->line_out_hw_vol)
2191 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
2192 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2193 &private->dim_mute_ctls[i]->id);
2196 /* Interrupt callback */
2197 static void scarlett2_notify(struct urb *urb)
2199 struct usb_mixer_interface *mixer = urb->context;
2200 int len = urb->actual_length;
2201 int ustatus = urb->status;
2204 if (ustatus != 0 || len != 8)
2207 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
2208 if (data & SCARLETT2_USB_NOTIFY_SYNC)
2209 scarlett2_notify_sync(mixer);
2210 if (data & SCARLETT2_USB_NOTIFY_MONITOR)
2211 scarlett2_notify_monitor(mixer);
2212 if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE)
2213 scarlett2_notify_dim_mute(mixer);
2216 if (ustatus != -ENOENT &&
2217 ustatus != -ECONNRESET &&
2218 ustatus != -ESHUTDOWN) {
2219 urb->dev = mixer->chip->dev;
2220 usb_submit_urb(urb, GFP_ATOMIC);
2224 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
2226 struct usb_device *dev = mixer->chip->dev;
2227 struct scarlett2_data *private = mixer->private_data;
2228 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
2229 void *transfer_buffer;
2232 usb_audio_err(mixer->chip,
2233 "%s: mixer urb already in use!\n", __func__);
2237 if (usb_pipe_type_check(dev, pipe))
2240 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
2244 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
2245 if (!transfer_buffer)
2248 usb_fill_int_urb(mixer->urb, dev, pipe,
2249 transfer_buffer, private->wMaxPacketSize,
2250 scarlett2_notify, mixer, private->bInterval);
2252 return usb_submit_urb(mixer->urb, GFP_KERNEL);
2255 static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer,
2256 const struct scarlett2_device_info *info)
2260 /* Initialise private data */
2261 err = scarlett2_init_private(mixer, info);
2265 /* Send proprietary USB initialisation sequence */
2266 err = scarlett2_usb_init(mixer);
2270 /* Read volume levels and controls from the interface */
2271 err = scarlett2_read_configs(mixer);
2275 /* Create the analogue output controls */
2276 err = scarlett2_add_line_out_ctls(mixer);
2280 /* Create the analogue input controls */
2281 err = scarlett2_add_line_in_ctls(mixer);
2285 /* Create the input, output, and mixer mux input selections */
2286 err = scarlett2_add_mux_enums(mixer);
2290 /* Create the matrix mixer controls */
2291 err = scarlett2_add_mixer_ctls(mixer);
2295 /* Create the level meter controls */
2296 err = scarlett2_add_meter_ctl(mixer);
2300 /* Create the sync control */
2301 err = scarlett2_add_sync_ctl(mixer);
2305 /* Set up the interrupt polling */
2306 err = scarlett2_init_notify(mixer);
2313 int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
2315 struct snd_usb_audio *chip = mixer->chip;
2316 const struct scarlett2_device_info *info;
2319 /* only use UAC_VERSION_2 */
2320 if (!mixer->protocol)
2323 switch (chip->usb_id) {
2324 case USB_ID(0x1235, 0x8203):
2325 info = &s6i6_gen2_info;
2327 case USB_ID(0x1235, 0x8204):
2328 info = &s18i8_gen2_info;
2330 case USB_ID(0x1235, 0x8201):
2331 info = &s18i20_gen2_info;
2333 default: /* device not (yet) supported */
2337 if (!(chip->setup & SCARLETT2_ENABLE)) {
2338 usb_audio_info(chip,
2339 "Focusrite Scarlett Gen 2 Mixer Driver disabled; "
2340 "use options snd_usb_audio vid=0x%04x pid=0x%04x "
2341 "device_setup=1 to enable and report any issues "
2343 USB_ID_VENDOR(chip->usb_id),
2344 USB_ID_PRODUCT(chip->usb_id));
2348 usb_audio_info(chip,
2349 "Focusrite Scarlett Gen 2 Mixer Driver enabled pid=0x%04x",
2350 USB_ID_PRODUCT(chip->usb_id));
2352 err = snd_scarlett_gen2_controls_create(mixer, info);
2354 usb_audio_err(mixer->chip,
2355 "Error initialising Scarlett Mixer Driver: %d",