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 /* Hardware buttons on the 18i20 */
198 #define SCARLETT2_BUTTON_MAX 2
200 static const char *const scarlett2_button_names[SCARLETT2_BUTTON_MAX] = {
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 button_count; /* number of buttons */
222 u8 level_input_count; /* inputs with level selectable */
223 u8 pad_input_count; /* inputs with pad selectable */
224 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
225 struct scarlett2_ports ports[SCARLETT2_PORT_TYPE_COUNT];
228 struct scarlett2_mixer_data {
229 struct usb_mixer_interface *mixer;
230 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
231 struct mutex data_mutex; /* lock access to this data */
232 struct delayed_work work;
233 const struct scarlett2_device_info *info;
239 u8 vol[SCARLETT2_ANALOGUE_MAX];
240 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
241 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
242 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
243 u8 buttons[SCARLETT2_BUTTON_MAX];
244 struct snd_kcontrol *master_vol_ctl;
245 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
246 struct snd_kcontrol *button_ctls[SCARLETT2_BUTTON_MAX];
247 u8 mux[SCARLETT2_MUX_MAX];
248 u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
251 /*** Model-specific data ***/
253 static const struct scarlett2_device_info s6i6_gen2_info = {
254 /* The first two analogue inputs can be switched between line
255 * and instrument levels.
257 .level_input_count = 2,
259 /* The first two analogue inputs have an optional pad. */
260 .pad_input_count = 2,
270 [SCARLETT2_PORT_TYPE_NONE] = {
272 .num = { 1, 0, 8, 8, 8 },
276 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
278 .num = { 4, 4, 4, 4, 4 },
279 .src_descr = "Analogue %d",
281 .dst_descr = "Analogue Output %02d Playback"
283 [SCARLETT2_PORT_TYPE_SPDIF] = {
285 .num = { 2, 2, 2, 2, 2 },
286 .src_descr = "S/PDIF %d",
288 .dst_descr = "S/PDIF Output %d Playback"
290 [SCARLETT2_PORT_TYPE_MIX] = {
292 .num = { 10, 18, 18, 18, 18 },
293 .src_descr = "Mix %c",
294 .src_num_offset = 65,
295 .dst_descr = "Mixer Input %02d Capture"
297 [SCARLETT2_PORT_TYPE_PCM] = {
299 .num = { 6, 6, 6, 6, 6 },
300 .src_descr = "PCM %d",
302 .dst_descr = "PCM %02d Capture"
307 static const struct scarlett2_device_info s18i8_gen2_info = {
308 /* The first two analogue inputs can be switched between line
309 * and instrument levels.
311 .level_input_count = 2,
313 /* The first four analogue inputs have an optional pad. */
314 .pad_input_count = 4,
326 [SCARLETT2_PORT_TYPE_NONE] = {
328 .num = { 1, 0, 8, 8, 4 },
332 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
334 .num = { 8, 6, 6, 6, 6 },
335 .src_descr = "Analogue %d",
337 .dst_descr = "Analogue Output %02d Playback"
339 [SCARLETT2_PORT_TYPE_SPDIF] = {
341 .num = { 2, 2, 2, 2, 2 },
342 .src_descr = "S/PDIF %d",
344 .dst_descr = "S/PDIF Output %d Playback"
346 [SCARLETT2_PORT_TYPE_ADAT] = {
348 .num = { 8, 0, 0, 0, 0 },
349 .src_descr = "ADAT %d",
352 [SCARLETT2_PORT_TYPE_MIX] = {
354 .num = { 10, 18, 18, 18, 18 },
355 .src_descr = "Mix %c",
356 .src_num_offset = 65,
357 .dst_descr = "Mixer Input %02d Capture"
359 [SCARLETT2_PORT_TYPE_PCM] = {
361 .num = { 8, 18, 18, 14, 10 },
362 .src_descr = "PCM %d",
364 .dst_descr = "PCM %02d Capture"
369 static const struct scarlett2_device_info s18i20_gen2_info = {
370 /* The analogue line outputs on the 18i20 can be switched
371 * between software and hardware volume control
373 .line_out_hw_vol = 1,
375 /* Mute and dim buttons */
392 [SCARLETT2_PORT_TYPE_NONE] = {
394 .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 /* Vendor-Specific Interface, Endpoint, MaxPacketSize, Interval */
455 #define SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE 5
456 #define SCARLETT2_USB_INTERRUPT_ENDPOINT 4
457 #define SCARLETT2_USB_INTERRUPT_MAX_DATA 64
458 #define SCARLETT2_USB_INTERRUPT_INTERVAL 3
460 /* Interrupt flags for volume and mute/dim button changes */
461 #define SCARLETT2_USB_INTERRUPT_VOL_CHANGE 0x00400000
462 #define SCARLETT2_USB_INTERRUPT_BUTTON_CHANGE 0x00200000
464 /* Commands for sending/receiving requests/responses */
465 #define SCARLETT2_USB_VENDOR_SPECIFIC_CMD_REQ 2
466 #define SCARLETT2_USB_VENDOR_SPECIFIC_CMD_RESP 3
468 #define SCARLETT2_USB_INIT_SEQ 0x00000000
469 #define SCARLETT2_USB_GET_METER 0x00001001
470 #define SCARLETT2_USB_GET_MIX 0x00002001
471 #define SCARLETT2_USB_SET_MIX 0x00002002
472 #define SCARLETT2_USB_GET_MUX 0x00003001
473 #define SCARLETT2_USB_SET_MUX 0x00003002
474 #define SCARLETT2_USB_GET_DATA 0x00800000
475 #define SCARLETT2_USB_SET_DATA 0x00800001
476 #define SCARLETT2_USB_DATA_CMD 0x00800002
478 #define SCARLETT2_USB_CONFIG_SAVE 6
480 #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
481 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
483 /* volume status is read together (matches scarlett2_config_items[]) */
484 struct scarlett2_usb_volume_status {
485 /* mute & dim buttons */
486 u8 buttons[SCARLETT2_BUTTON_MAX];
490 /* software volume setting */
491 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
493 /* actual volume of output inc. dim (-18dB) */
494 s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
496 u8 pad2[SCARLETT2_ANALOGUE_MAX];
498 /* sw (0) or hw (1) controlled */
499 u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
503 /* front panel volume knob */
507 /* Configuration parameters that can be read and written */
509 SCARLETT2_CONFIG_BUTTONS = 0,
510 SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
511 SCARLETT2_CONFIG_SW_HW_SWITCH = 2,
512 SCARLETT2_CONFIG_LEVEL_SWITCH = 3,
513 SCARLETT2_CONFIG_PAD_SWITCH = 4,
514 SCARLETT2_CONFIG_COUNT = 5
517 /* Location, size, and activation command number for the configuration
520 struct scarlett2_config {
526 static const struct scarlett2_config
527 scarlett2_config_items[SCARLETT2_CONFIG_COUNT] = {
528 /* Mute/Dim Buttons */
535 /* Line Out Volume */
542 /* SW/HW Volume Switch */
564 /* proprietary request/response format */
565 struct scarlett2_usb_packet {
574 #define SCARLETT2_USB_PACKET_LEN (sizeof(struct scarlett2_usb_packet))
576 static void scarlett2_fill_request_header(struct scarlett2_mixer_data *private,
577 struct scarlett2_usb_packet *req,
578 u32 cmd, u16 req_size)
580 /* sequence must go up by 1 for each request */
581 u16 seq = private->scarlett2_seq++;
583 req->cmd = cpu_to_le32(cmd);
584 req->size = cpu_to_le16(req_size);
585 req->seq = cpu_to_le16(seq);
590 /* Send a proprietary format request to the Scarlett interface */
591 static int scarlett2_usb(
592 struct usb_mixer_interface *mixer, u32 cmd,
593 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
595 struct scarlett2_mixer_data *private = mixer->private_data;
596 u16 req_buf_size = sizeof(struct scarlett2_usb_packet) + req_size;
597 u16 resp_buf_size = sizeof(struct scarlett2_usb_packet) + resp_size;
598 struct scarlett2_usb_packet *req = NULL, *resp = NULL;
601 req = kmalloc(req_buf_size, GFP_KERNEL);
607 resp = kmalloc(resp_buf_size, GFP_KERNEL);
613 mutex_lock(&private->usb_mutex);
615 /* build request message and send it */
617 scarlett2_fill_request_header(private, req, cmd, req_size);
620 memcpy(req->data, req_data, req_size);
622 err = snd_usb_ctl_msg(mixer->chip->dev,
623 usb_sndctrlpipe(mixer->chip->dev, 0),
624 SCARLETT2_USB_VENDOR_SPECIFIC_CMD_REQ,
625 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
627 SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE,
631 if (err != req_buf_size) {
634 "Scarlett Gen 2 USB request result cmd %x was %d\n",
640 /* send a second message to get the response */
642 err = snd_usb_ctl_msg(mixer->chip->dev,
643 usb_rcvctrlpipe(mixer->chip->dev, 0),
644 SCARLETT2_USB_VENDOR_SPECIFIC_CMD_RESP,
645 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
647 SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE,
651 /* validate the response */
653 if (err != resp_buf_size) {
656 "Scarlett Gen 2 USB response result cmd %x was %d\n",
662 if (resp->cmd != req->cmd ||
663 resp->seq != req->seq ||
664 resp_size != le16_to_cpu(resp->size) ||
669 "Scarlett Gen 2 USB invalid response; "
670 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
672 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
673 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
674 resp_size, le16_to_cpu(resp->size),
675 le32_to_cpu(resp->error),
676 le32_to_cpu(resp->pad));
682 memcpy(resp_data, resp->data, resp_size);
685 mutex_unlock(&private->usb_mutex);
692 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
693 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
695 __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
697 scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
702 /* Delayed work to save config */
703 static void scarlett2_config_save_work(struct work_struct *work)
705 struct scarlett2_mixer_data *private =
706 container_of(work, struct scarlett2_mixer_data, work.work);
708 scarlett2_config_save(private->mixer);
711 /* Send a USB message to set a configuration parameter (volume level,
712 * sw/hw volume switch, line/inst level switch, or pad switch)
714 static int scarlett2_usb_set_config(
715 struct usb_mixer_interface *mixer,
716 int config_item_num, int index, int value)
718 const struct scarlett2_config config_item =
719 scarlett2_config_items[config_item_num];
727 struct scarlett2_mixer_data *private = mixer->private_data;
729 /* Cancel any pending NVRAM save */
730 cancel_delayed_work_sync(&private->work);
732 /* Send the configuration parameter data */
733 req.offset = cpu_to_le32(config_item.offset + index * config_item.size);
734 req.bytes = cpu_to_le32(config_item.size);
735 req.value = cpu_to_le32(value);
736 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
737 &req, sizeof(u32) * 2 + config_item.size,
742 /* Activate the change */
743 req2 = cpu_to_le32(config_item.activate);
744 err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
745 &req2, sizeof(req2), NULL, 0);
749 /* Schedule the change to be written to NVRAM */
750 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
755 /* Send a USB message to get data; result placed in *buf */
756 static int scarlett2_usb_get(
757 struct usb_mixer_interface *mixer,
758 int offset, void *buf, int size)
765 req.offset = cpu_to_le32(offset);
766 req.size = cpu_to_le32(size);
767 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
768 &req, sizeof(req), buf, size);
771 /* Send a USB message to get configuration parameters; result placed in *buf */
772 static int scarlett2_usb_get_config(
773 struct usb_mixer_interface *mixer,
774 int config_item_num, int count, void *buf)
776 const struct scarlett2_config config_item =
777 scarlett2_config_items[config_item_num];
778 int size = config_item.size * count;
780 return scarlett2_usb_get(mixer, config_item.offset, buf, size);
783 /* Send a USB message to get volume status; result placed in *buf */
784 static int scarlett2_usb_get_volume_status(
785 struct usb_mixer_interface *mixer,
786 struct scarlett2_usb_volume_status *buf)
788 return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
792 /* Send a USB message to get the volumes for all inputs of one mix
793 * and put the values into private->mix[]
795 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
798 struct scarlett2_mixer_data *private = mixer->private_data;
799 const struct scarlett2_device_info *info = private->info;
802 info->ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
810 __le16 data[SCARLETT2_INPUT_MIX_MAX];
812 req.mix_num = cpu_to_le16(mix_num);
813 req.count = cpu_to_le16(num_mixer_in);
815 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
817 data, num_mixer_in * sizeof(u16));
821 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
822 u16 mixer_value = le16_to_cpu(data[i]);
824 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
825 if (scarlett2_mixer_values[k] >= mixer_value)
827 if (k == SCARLETT2_MIXER_VALUE_COUNT)
828 k = SCARLETT2_MIXER_MAX_VALUE;
835 /* Send a USB message to set the volumes for all inputs of one mix
836 * (values obtained from private->mix[])
838 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
841 struct scarlett2_mixer_data *private = mixer->private_data;
842 const struct scarlett2_device_info *info = private->info;
846 __le16 data[SCARLETT2_INPUT_MIX_MAX];
851 info->ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
853 req.mix_num = cpu_to_le16(mix_num);
855 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
856 req.data[i] = cpu_to_le16(
857 scarlett2_mixer_values[private->mix[j]]
860 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
861 &req, (num_mixer_in + 1) * sizeof(u16),
865 /* Convert a port number index (per info->ports) to a hardware ID */
866 static u32 scarlett2_mux_src_num_to_id(const struct scarlett2_ports *ports,
872 port_type < SCARLETT2_PORT_TYPE_COUNT;
874 if (num < ports[port_type].num[SCARLETT2_PORT_IN])
875 return ports[port_type].id | num;
876 num -= ports[port_type].num[SCARLETT2_PORT_IN];
883 /* Convert a hardware ID to a port number index */
884 static u32 scarlett2_mux_id_to_num(const struct scarlett2_ports *ports,
892 port_type < SCARLETT2_PORT_TYPE_COUNT;
894 struct scarlett2_ports port = ports[port_type];
895 int count = port.num[direction];
897 if (id >= port.id && id < port.id + count)
898 return port_num + id - port.id;
906 /* Convert one mux entry from the interface and load into private->mux[] */
907 static void scarlett2_usb_populate_mux(struct scarlett2_mixer_data *private,
910 const struct scarlett2_device_info *info = private->info;
911 const struct scarlett2_ports *ports = info->ports;
913 int dst_idx, src_idx;
915 dst_idx = scarlett2_mux_id_to_num(ports, SCARLETT2_PORT_OUT,
920 if (dst_idx >= private->num_mux_dsts) {
921 usb_audio_err(private->mixer->chip,
922 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
923 mux_entry, dst_idx, private->num_mux_dsts);
927 src_idx = scarlett2_mux_id_to_num(ports, SCARLETT2_PORT_IN,
932 if (src_idx >= private->num_mux_srcs) {
933 usb_audio_err(private->mixer->chip,
934 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
935 mux_entry, src_idx, private->num_mux_srcs);
939 private->mux[dst_idx] = src_idx;
942 /* Send USB message to get mux inputs and then populate private->mux[] */
943 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
945 struct scarlett2_mixer_data *private = mixer->private_data;
946 int count = private->num_mux_dsts;
954 __le32 data[SCARLETT2_MUX_MAX];
957 req.count = cpu_to_le16(count);
959 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
961 data, count * sizeof(u32));
965 for (i = 0; i < count; i++)
966 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
971 /* Send USB messages to set mux inputs */
972 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
974 struct scarlett2_mixer_data *private = mixer->private_data;
975 const struct scarlett2_device_info *info = private->info;
976 const struct scarlett2_ports *ports = info->ports;
977 int rate, port_dir_rate;
979 static const int assignment_order[SCARLETT2_PORT_TYPE_COUNT] = {
980 SCARLETT2_PORT_TYPE_PCM,
981 SCARLETT2_PORT_TYPE_ANALOGUE,
982 SCARLETT2_PORT_TYPE_SPDIF,
983 SCARLETT2_PORT_TYPE_ADAT,
984 SCARLETT2_PORT_TYPE_MIX,
985 SCARLETT2_PORT_TYPE_NONE,
991 __le32 data[SCARLETT2_MUX_MAX];
996 /* mux settings for each rate */
997 for (rate = 0, port_dir_rate = SCARLETT2_PORT_OUT_44;
998 port_dir_rate <= SCARLETT2_PORT_OUT_176;
999 rate++, port_dir_rate++) {
1000 int order_num, i, err;
1002 req.num = cpu_to_le16(rate);
1004 for (order_num = 0, i = 0;
1005 order_num < SCARLETT2_PORT_TYPE_COUNT;
1007 int port_type = assignment_order[order_num];
1008 int j = scarlett2_get_port_start_num(ports,
1011 int port_id = ports[port_type].id;
1015 channel < ports[port_type].num[port_dir_rate];
1016 channel++, i++, j++)
1017 /* lower 12 bits for the destination and
1018 * next 12 bits for the source
1020 req.data[i] = !port_id
1025 scarlett2_mux_src_num_to_id(
1026 ports, private->mux[j]
1030 /* skip private->mux[j] entries not output */
1031 j += ports[port_type].num[SCARLETT2_PORT_OUT] -
1032 ports[port_type].num[port_dir_rate];
1035 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1036 &req, (i + 1) * sizeof(u32),
1045 /* Send USB message to get meter levels */
1046 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1054 u32 resp[SCARLETT2_NUM_METERS];
1058 req.num_meters = cpu_to_le16(SCARLETT2_NUM_METERS);
1059 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
1060 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
1061 &req, sizeof(req), resp, sizeof(resp));
1065 /* copy, convert to u16 */
1066 for (i = 0; i < SCARLETT2_NUM_METERS; i++)
1067 levels[i] = resp[i];
1072 /*** Control Functions ***/
1074 /* helper function to create a new control */
1075 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
1076 const struct snd_kcontrol_new *ncontrol,
1077 int index, int channels, const char *name,
1078 struct snd_kcontrol **kctl_return)
1080 struct snd_kcontrol *kctl;
1081 struct usb_mixer_elem_info *elem;
1084 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
1088 elem->head.mixer = mixer;
1089 elem->control = index;
1090 elem->head.id = index;
1091 elem->channels = channels;
1093 kctl = snd_ctl_new1(ncontrol, elem);
1098 kctl->private_free = snd_usb_mixer_elem_free;
1100 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
1102 err = snd_usb_mixer_add_control(&elem->head, kctl);
1107 *kctl_return = kctl;
1112 /*** Analogue Line Out Volume Controls ***/
1114 /* Update hardware volume controls after receiving notification that
1117 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
1119 struct scarlett2_mixer_data *private = mixer->private_data;
1120 const struct scarlett2_ports *ports = private->info->ports;
1121 struct scarlett2_usb_volume_status volume_status;
1123 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1126 private->vol_updated = 0;
1128 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1132 private->master_vol = clamp(
1133 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1134 0, SCARLETT2_VOLUME_BIAS);
1136 for (i = 0; i < num_line_out; i++) {
1137 if (private->vol_sw_hw_switch[i])
1138 private->vol[i] = private->master_vol;
1141 for (i = 0; i < private->info->button_count; i++)
1142 private->buttons[i] = !!volume_status.buttons[i];
1147 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1148 struct snd_ctl_elem_info *uinfo)
1150 struct usb_mixer_elem_info *elem = kctl->private_data;
1152 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1153 uinfo->count = elem->channels;
1154 uinfo->value.integer.min = 0;
1155 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1156 uinfo->value.integer.step = 1;
1160 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1161 struct snd_ctl_elem_value *ucontrol)
1163 struct usb_mixer_elem_info *elem = kctl->private_data;
1164 struct usb_mixer_interface *mixer = elem->head.mixer;
1165 struct scarlett2_mixer_data *private = mixer->private_data;
1167 if (private->vol_updated) {
1168 mutex_lock(&private->data_mutex);
1169 scarlett2_update_volumes(mixer);
1170 mutex_unlock(&private->data_mutex);
1173 ucontrol->value.integer.value[0] = private->master_vol;
1177 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1178 struct snd_ctl_elem_value *ucontrol)
1180 struct usb_mixer_elem_info *elem = kctl->private_data;
1181 struct usb_mixer_interface *mixer = elem->head.mixer;
1182 struct scarlett2_mixer_data *private = mixer->private_data;
1183 int index = elem->control;
1185 if (private->vol_updated) {
1186 mutex_lock(&private->data_mutex);
1187 scarlett2_update_volumes(mixer);
1188 mutex_unlock(&private->data_mutex);
1191 ucontrol->value.integer.value[0] = private->vol[index];
1195 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1196 struct snd_ctl_elem_value *ucontrol)
1198 struct usb_mixer_elem_info *elem = kctl->private_data;
1199 struct usb_mixer_interface *mixer = elem->head.mixer;
1200 struct scarlett2_mixer_data *private = mixer->private_data;
1201 int index = elem->control;
1202 int oval, val, err = 0;
1204 mutex_lock(&private->data_mutex);
1206 oval = private->vol[index];
1207 val = ucontrol->value.integer.value[0];
1212 private->vol[index] = val;
1213 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1214 index, val - SCARLETT2_VOLUME_BIAS);
1219 mutex_unlock(&private->data_mutex);
1223 static const DECLARE_TLV_DB_MINMAX(
1224 db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1227 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1228 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1229 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1230 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1232 .info = scarlett2_volume_ctl_info,
1233 .get = scarlett2_master_volume_ctl_get,
1234 .private_value = 0, /* max value */
1235 .tlv = { .p = db_scale_scarlett2_gain }
1238 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1239 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1240 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1241 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1243 .info = scarlett2_volume_ctl_info,
1244 .get = scarlett2_volume_ctl_get,
1245 .put = scarlett2_volume_ctl_put,
1246 .private_value = 0, /* max value */
1247 .tlv = { .p = db_scale_scarlett2_gain }
1250 /*** HW/SW Volume Switch Controls ***/
1252 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
1253 struct snd_ctl_elem_info *uinfo)
1255 static const char *const values[2] = {
1259 return snd_ctl_enum_info(uinfo, 1, 2, values);
1262 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
1263 struct snd_ctl_elem_value *ucontrol)
1265 struct usb_mixer_elem_info *elem = kctl->private_data;
1266 struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1268 ucontrol->value.enumerated.item[0] =
1269 private->vol_sw_hw_switch[elem->control];
1273 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
1274 struct snd_ctl_elem_value *ucontrol)
1276 struct usb_mixer_elem_info *elem = kctl->private_data;
1277 struct usb_mixer_interface *mixer = elem->head.mixer;
1278 struct scarlett2_mixer_data *private = mixer->private_data;
1280 int index = elem->control;
1281 int oval, val, err = 0;
1283 mutex_lock(&private->data_mutex);
1285 oval = private->vol_sw_hw_switch[index];
1286 val = !!ucontrol->value.integer.value[0];
1291 private->vol_sw_hw_switch[index] = val;
1293 /* Change access mode to RO (hardware controlled volume)
1294 * or RW (software controlled volume)
1297 private->vol_ctls[index]->vd[0].access &=
1298 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1300 private->vol_ctls[index]->vd[0].access |=
1301 SNDRV_CTL_ELEM_ACCESS_WRITE;
1303 /* Reset volume to master volume */
1304 private->vol[index] = private->master_vol;
1306 /* Set SW volume to current HW volume */
1307 err = scarlett2_usb_set_config(
1308 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1309 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
1313 /* Notify of RO/RW change */
1314 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_INFO,
1315 &private->vol_ctls[index]->id);
1317 /* Send SW/HW switch change to the device */
1318 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
1322 mutex_unlock(&private->data_mutex);
1326 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
1327 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1329 .info = scarlett2_sw_hw_enum_ctl_info,
1330 .get = scarlett2_sw_hw_enum_ctl_get,
1331 .put = scarlett2_sw_hw_enum_ctl_put,
1334 /*** Line Level/Instrument Level Switch Controls ***/
1336 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
1337 struct snd_ctl_elem_info *uinfo)
1339 static const char *const values[2] = {
1343 return snd_ctl_enum_info(uinfo, 1, 2, values);
1346 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
1347 struct snd_ctl_elem_value *ucontrol)
1349 struct usb_mixer_elem_info *elem = kctl->private_data;
1350 struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1352 ucontrol->value.enumerated.item[0] =
1353 private->level_switch[elem->control];
1357 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
1358 struct snd_ctl_elem_value *ucontrol)
1360 struct usb_mixer_elem_info *elem = kctl->private_data;
1361 struct usb_mixer_interface *mixer = elem->head.mixer;
1362 struct scarlett2_mixer_data *private = mixer->private_data;
1364 int index = elem->control;
1365 int oval, val, err = 0;
1367 mutex_lock(&private->data_mutex);
1369 oval = private->level_switch[index];
1370 val = !!ucontrol->value.integer.value[0];
1375 private->level_switch[index] = val;
1377 /* Send switch change to the device */
1378 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
1382 mutex_unlock(&private->data_mutex);
1386 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
1387 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1389 .info = scarlett2_level_enum_ctl_info,
1390 .get = scarlett2_level_enum_ctl_get,
1391 .put = scarlett2_level_enum_ctl_put,
1394 /*** Pad Switch Controls ***/
1396 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
1397 struct snd_ctl_elem_value *ucontrol)
1399 struct usb_mixer_elem_info *elem = kctl->private_data;
1400 struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1402 ucontrol->value.enumerated.item[0] =
1403 private->pad_switch[elem->control];
1407 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
1408 struct snd_ctl_elem_value *ucontrol)
1410 struct usb_mixer_elem_info *elem = kctl->private_data;
1411 struct usb_mixer_interface *mixer = elem->head.mixer;
1412 struct scarlett2_mixer_data *private = mixer->private_data;
1414 int index = elem->control;
1415 int oval, val, err = 0;
1417 mutex_lock(&private->data_mutex);
1419 oval = private->pad_switch[index];
1420 val = !!ucontrol->value.integer.value[0];
1425 private->pad_switch[index] = val;
1427 /* Send switch change to the device */
1428 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
1432 mutex_unlock(&private->data_mutex);
1436 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
1437 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1439 .info = snd_ctl_boolean_mono_info,
1440 .get = scarlett2_pad_ctl_get,
1441 .put = scarlett2_pad_ctl_put,
1444 /*** Mute/Dim Controls ***/
1446 static int scarlett2_button_ctl_get(struct snd_kcontrol *kctl,
1447 struct snd_ctl_elem_value *ucontrol)
1449 struct usb_mixer_elem_info *elem = kctl->private_data;
1450 struct usb_mixer_interface *mixer = elem->head.mixer;
1451 struct scarlett2_mixer_data *private = mixer->private_data;
1453 if (private->vol_updated) {
1454 mutex_lock(&private->data_mutex);
1455 scarlett2_update_volumes(mixer);
1456 mutex_unlock(&private->data_mutex);
1459 ucontrol->value.enumerated.item[0] = private->buttons[elem->control];
1463 static int scarlett2_button_ctl_put(struct snd_kcontrol *kctl,
1464 struct snd_ctl_elem_value *ucontrol)
1466 struct usb_mixer_elem_info *elem = kctl->private_data;
1467 struct usb_mixer_interface *mixer = elem->head.mixer;
1468 struct scarlett2_mixer_data *private = mixer->private_data;
1470 int index = elem->control;
1471 int oval, val, err = 0;
1473 mutex_lock(&private->data_mutex);
1475 oval = private->buttons[index];
1476 val = !!ucontrol->value.integer.value[0];
1481 private->buttons[index] = val;
1483 /* Send switch change to the device */
1484 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_BUTTONS,
1488 mutex_unlock(&private->data_mutex);
1492 static const struct snd_kcontrol_new scarlett2_button_ctl = {
1493 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1495 .info = snd_ctl_boolean_mono_info,
1496 .get = scarlett2_button_ctl_get,
1497 .put = scarlett2_button_ctl_put
1500 /*** Create the analogue output controls ***/
1502 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
1504 struct scarlett2_mixer_data *private = mixer->private_data;
1505 const struct scarlett2_device_info *info = private->info;
1506 const struct scarlett2_ports *ports = info->ports;
1508 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1510 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1512 /* Add R/O HW volume control */
1513 if (info->line_out_hw_vol) {
1514 snprintf(s, sizeof(s), "Master HW Playback Volume");
1515 err = scarlett2_add_new_ctl(mixer,
1516 &scarlett2_master_volume_ctl,
1517 0, 1, s, &private->master_vol_ctl);
1522 /* Add volume controls */
1523 for (i = 0; i < num_line_out; i++) {
1526 if (info->line_out_descrs[i])
1527 snprintf(s, sizeof(s),
1528 "Line %02d (%s) Playback Volume",
1529 i + 1, info->line_out_descrs[i]);
1531 snprintf(s, sizeof(s),
1532 "Line %02d Playback Volume",
1534 err = scarlett2_add_new_ctl(mixer,
1535 &scarlett2_line_out_volume_ctl,
1536 i, 1, s, &private->vol_ctls[i]);
1540 /* Make the fader read-only if the SW/HW switch is set to HW */
1541 if (private->vol_sw_hw_switch[i])
1542 private->vol_ctls[i]->vd[0].access &=
1543 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1546 if (info->line_out_hw_vol) {
1547 snprintf(s, sizeof(s),
1548 "Line Out %02d Volume Control Playback Enum",
1550 err = scarlett2_add_new_ctl(mixer,
1551 &scarlett2_sw_hw_enum_ctl,
1558 /* Add HW button controls */
1559 for (i = 0; i < private->info->button_count; i++) {
1560 err = scarlett2_add_new_ctl(mixer, &scarlett2_button_ctl,
1561 i, 1, scarlett2_button_names[i],
1562 &private->button_ctls[i]);
1570 /*** Create the analogue input controls ***/
1572 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
1574 struct scarlett2_mixer_data *private = mixer->private_data;
1575 const struct scarlett2_device_info *info = private->info;
1577 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1579 /* Add input level (line/inst) controls */
1580 for (i = 0; i < info->level_input_count; i++) {
1581 snprintf(s, sizeof(s), "Line In %d Level Capture Enum", i + 1);
1582 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
1588 /* Add input pad controls */
1589 for (i = 0; i < info->pad_input_count; i++) {
1590 snprintf(s, sizeof(s), "Line In %d Pad Capture Switch", i + 1);
1591 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
1600 /*** Mixer Volume Controls ***/
1602 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
1603 struct snd_ctl_elem_info *uinfo)
1605 struct usb_mixer_elem_info *elem = kctl->private_data;
1607 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1608 uinfo->count = elem->channels;
1609 uinfo->value.integer.min = 0;
1610 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
1611 uinfo->value.integer.step = 1;
1615 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
1616 struct snd_ctl_elem_value *ucontrol)
1618 struct usb_mixer_elem_info *elem = kctl->private_data;
1619 struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1621 ucontrol->value.integer.value[0] = private->mix[elem->control];
1625 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
1626 struct snd_ctl_elem_value *ucontrol)
1628 struct usb_mixer_elem_info *elem = kctl->private_data;
1629 struct usb_mixer_interface *mixer = elem->head.mixer;
1630 struct scarlett2_mixer_data *private = mixer->private_data;
1631 const struct scarlett2_device_info *info = private->info;
1632 const struct scarlett2_ports *ports = info->ports;
1633 int oval, val, num_mixer_in, mix_num, err = 0;
1635 mutex_lock(&private->data_mutex);
1637 oval = private->mix[elem->control];
1638 val = ucontrol->value.integer.value[0];
1639 num_mixer_in = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
1640 mix_num = elem->control / num_mixer_in;
1645 private->mix[elem->control] = val;
1646 err = scarlett2_usb_set_mix(mixer, mix_num);
1651 mutex_unlock(&private->data_mutex);
1655 static const DECLARE_TLV_DB_MINMAX(
1656 db_scale_scarlett2_mixer,
1657 SCARLETT2_MIXER_MIN_DB * 100,
1658 SCARLETT2_MIXER_MAX_DB * 100
1661 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
1662 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1663 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1664 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1666 .info = scarlett2_mixer_ctl_info,
1667 .get = scarlett2_mixer_ctl_get,
1668 .put = scarlett2_mixer_ctl_put,
1669 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
1670 .tlv = { .p = db_scale_scarlett2_mixer }
1673 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
1675 struct scarlett2_mixer_data *private = mixer->private_data;
1676 const struct scarlett2_ports *ports = private->info->ports;
1679 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1681 int num_inputs = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
1682 int num_outputs = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_IN];
1684 for (i = 0, index = 0; i < num_outputs; i++) {
1685 for (j = 0; j < num_inputs; j++, index++) {
1686 snprintf(s, sizeof(s),
1687 "Mix %c Input %02d Playback Volume",
1689 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
1699 /*** Mux Source Selection Controls ***/
1701 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
1702 struct snd_ctl_elem_info *uinfo)
1704 struct usb_mixer_elem_info *elem = kctl->private_data;
1705 struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1706 const struct scarlett2_ports *ports = private->info->ports;
1707 unsigned int item = uinfo->value.enumerated.item;
1708 int items = private->num_mux_srcs;
1711 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1712 uinfo->count = elem->channels;
1713 uinfo->value.enumerated.items = items;
1716 item = uinfo->value.enumerated.item = items - 1;
1719 port_type < SCARLETT2_PORT_TYPE_COUNT;
1721 if (item < ports[port_type].num[SCARLETT2_PORT_IN]) {
1722 sprintf(uinfo->value.enumerated.name,
1723 ports[port_type].src_descr,
1724 item + ports[port_type].src_num_offset);
1727 item -= ports[port_type].num[SCARLETT2_PORT_IN];
1733 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
1734 struct snd_ctl_elem_value *ucontrol)
1736 struct usb_mixer_elem_info *elem = kctl->private_data;
1737 struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1739 ucontrol->value.enumerated.item[0] = private->mux[elem->control];
1743 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
1744 struct snd_ctl_elem_value *ucontrol)
1746 struct usb_mixer_elem_info *elem = kctl->private_data;
1747 struct usb_mixer_interface *mixer = elem->head.mixer;
1748 struct scarlett2_mixer_data *private = mixer->private_data;
1749 int index = elem->control;
1750 int oval, val, err = 0;
1752 mutex_lock(&private->data_mutex);
1754 oval = private->mux[index];
1755 val = clamp(ucontrol->value.integer.value[0],
1756 0L, private->num_mux_srcs - 1L);
1761 private->mux[index] = val;
1762 err = scarlett2_usb_set_mux(mixer);
1767 mutex_unlock(&private->data_mutex);
1771 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
1772 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1774 .info = scarlett2_mux_src_enum_ctl_info,
1775 .get = scarlett2_mux_src_enum_ctl_get,
1776 .put = scarlett2_mux_src_enum_ctl_put,
1779 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
1781 struct scarlett2_mixer_data *private = mixer->private_data;
1782 const struct scarlett2_ports *ports = private->info->ports;
1783 int port_type, channel, i;
1785 for (i = 0, port_type = 0;
1786 port_type < SCARLETT2_PORT_TYPE_COUNT;
1789 channel < ports[port_type].num[SCARLETT2_PORT_OUT];
1792 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1793 const char *const descr = ports[port_type].dst_descr;
1795 snprintf(s, sizeof(s) - 5, descr, channel + 1);
1798 err = scarlett2_add_new_ctl(mixer,
1799 &scarlett2_mux_src_enum_ctl,
1809 /*** Meter Controls ***/
1811 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
1812 struct snd_ctl_elem_info *uinfo)
1814 struct usb_mixer_elem_info *elem = kctl->private_data;
1816 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1817 uinfo->count = elem->channels;
1818 uinfo->value.integer.min = 0;
1819 uinfo->value.integer.max = 4095;
1820 uinfo->value.integer.step = 1;
1824 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
1825 struct snd_ctl_elem_value *ucontrol)
1827 struct usb_mixer_elem_info *elem = kctl->private_data;
1828 u16 meter_levels[SCARLETT2_NUM_METERS];
1831 err = scarlett2_usb_get_meter_levels(elem->head.mixer, meter_levels);
1835 for (i = 0; i < elem->channels; i++)
1836 ucontrol->value.integer.value[i] = meter_levels[i];
1841 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
1842 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1843 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1845 .info = scarlett2_meter_ctl_info,
1846 .get = scarlett2_meter_ctl_get
1849 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
1851 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
1852 0, SCARLETT2_NUM_METERS,
1853 "Level Meter", NULL);
1856 /*** Cleanup/Suspend Callbacks ***/
1858 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
1860 struct scarlett2_mixer_data *private = mixer->private_data;
1862 cancel_delayed_work_sync(&private->work);
1864 mixer->private_data = NULL;
1867 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
1869 struct scarlett2_mixer_data *private = mixer->private_data;
1871 if (cancel_delayed_work_sync(&private->work))
1872 scarlett2_config_save(private->mixer);
1875 /*** Initialisation ***/
1877 static void scarlett2_count_mux_io(struct scarlett2_mixer_data *private)
1879 const struct scarlett2_ports *ports = private->info->ports;
1880 int port_type, srcs = 0, dsts = 0;
1883 port_type < SCARLETT2_PORT_TYPE_COUNT;
1885 srcs += ports[port_type].num[SCARLETT2_PORT_IN];
1886 dsts += ports[port_type].num[SCARLETT2_PORT_OUT_44];
1889 private->num_mux_srcs = srcs;
1890 private->num_mux_dsts = dsts;
1893 /* Initialise private data and sequence number */
1894 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
1895 const struct scarlett2_device_info *info)
1897 struct scarlett2_mixer_data *private =
1898 kzalloc(sizeof(struct scarlett2_mixer_data), GFP_KERNEL);
1903 mutex_init(&private->usb_mutex);
1904 mutex_init(&private->data_mutex);
1905 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
1906 private->info = info;
1907 scarlett2_count_mux_io(private);
1908 private->scarlett2_seq = 0;
1909 private->mixer = mixer;
1910 mixer->private_data = private;
1911 mixer->private_free = scarlett2_private_free;
1912 mixer->private_suspend = scarlett2_private_suspend;
1914 /* Initialise the sequence number used for the proprietary commands */
1915 return scarlett2_usb(mixer, SCARLETT2_USB_INIT_SEQ, NULL, 0, NULL, 0);
1918 /* Read configuration from the interface on start */
1919 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
1921 struct scarlett2_mixer_data *private = mixer->private_data;
1922 const struct scarlett2_device_info *info = private->info;
1923 const struct scarlett2_ports *ports = info->ports;
1925 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1927 ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_IN];
1928 u8 level_switches[SCARLETT2_LEVEL_SWITCH_MAX];
1929 u8 pad_switches[SCARLETT2_PAD_SWITCH_MAX];
1930 struct scarlett2_usb_volume_status volume_status;
1933 if (info->level_input_count) {
1934 err = scarlett2_usb_get_config(
1936 SCARLETT2_CONFIG_LEVEL_SWITCH,
1937 info->level_input_count,
1941 for (i = 0; i < info->level_input_count; i++)
1942 private->level_switch[i] = level_switches[i];
1945 if (info->pad_input_count) {
1946 err = scarlett2_usb_get_config(
1948 SCARLETT2_CONFIG_PAD_SWITCH,
1949 info->pad_input_count,
1953 for (i = 0; i < info->pad_input_count; i++)
1954 private->pad_switch[i] = pad_switches[i];
1957 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1961 private->master_vol = clamp(
1962 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1963 0, SCARLETT2_VOLUME_BIAS);
1965 for (i = 0; i < num_line_out; i++) {
1968 private->vol_sw_hw_switch[i] =
1969 info->line_out_hw_vol
1970 && volume_status.sw_hw_switch[i];
1972 volume = private->vol_sw_hw_switch[i]
1973 ? volume_status.master_vol
1974 : volume_status.sw_vol[i];
1975 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
1976 0, SCARLETT2_VOLUME_BIAS);
1977 private->vol[i] = volume;
1980 for (i = 0; i < info->button_count; i++)
1981 private->buttons[i] = !!volume_status.buttons[i];
1983 for (i = 0; i < num_mixer_out; i++) {
1984 err = scarlett2_usb_get_mix(mixer, i);
1989 return scarlett2_usb_get_mux(mixer);
1992 /* Notify on volume change */
1993 static void scarlett2_mixer_interrupt_vol_change(
1994 struct usb_mixer_interface *mixer)
1996 struct scarlett2_mixer_data *private = mixer->private_data;
1997 const struct scarlett2_ports *ports = private->info->ports;
1999 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
2002 private->vol_updated = 1;
2004 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2005 &private->master_vol_ctl->id);
2007 for (i = 0; i < num_line_out; i++) {
2008 if (!private->vol_sw_hw_switch[i])
2010 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2011 &private->vol_ctls[i]->id);
2015 /* Notify on button change */
2016 static void scarlett2_mixer_interrupt_button_change(
2017 struct usb_mixer_interface *mixer)
2019 struct scarlett2_mixer_data *private = mixer->private_data;
2022 private->vol_updated = 1;
2024 for (i = 0; i < private->info->button_count; i++)
2025 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2026 &private->button_ctls[i]->id);
2029 /* Interrupt callback */
2030 static void scarlett2_mixer_interrupt(struct urb *urb)
2032 struct usb_mixer_interface *mixer = urb->context;
2033 int len = urb->actual_length;
2034 int ustatus = urb->status;
2041 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
2042 if (data & SCARLETT2_USB_INTERRUPT_VOL_CHANGE)
2043 scarlett2_mixer_interrupt_vol_change(mixer);
2044 if (data & SCARLETT2_USB_INTERRUPT_BUTTON_CHANGE)
2045 scarlett2_mixer_interrupt_button_change(mixer);
2047 usb_audio_err(mixer->chip,
2048 "scarlett mixer interrupt length %d\n", len);
2052 if (ustatus != -ENOENT &&
2053 ustatus != -ECONNRESET &&
2054 ustatus != -ESHUTDOWN) {
2055 urb->dev = mixer->chip->dev;
2056 usb_submit_urb(urb, GFP_ATOMIC);
2060 static int scarlett2_mixer_status_create(struct usb_mixer_interface *mixer)
2062 struct usb_device *dev = mixer->chip->dev;
2063 unsigned int pipe = usb_rcvintpipe(dev,
2064 SCARLETT2_USB_INTERRUPT_ENDPOINT);
2065 void *transfer_buffer;
2068 usb_audio_err(mixer->chip,
2069 "%s: mixer urb already in use!\n", __func__);
2073 if (usb_pipe_type_check(dev, pipe))
2076 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
2080 transfer_buffer = kmalloc(SCARLETT2_USB_INTERRUPT_MAX_DATA, GFP_KERNEL);
2081 if (!transfer_buffer)
2084 usb_fill_int_urb(mixer->urb, dev, pipe,
2085 transfer_buffer, SCARLETT2_USB_INTERRUPT_MAX_DATA,
2086 scarlett2_mixer_interrupt, mixer,
2087 SCARLETT2_USB_INTERRUPT_INTERVAL);
2089 return usb_submit_urb(mixer->urb, GFP_KERNEL);
2092 static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer,
2093 const struct scarlett2_device_info *info)
2097 /* Initialise private data and sequence number */
2098 err = scarlett2_init_private(mixer, info);
2102 /* Read volume levels and controls from the interface */
2103 err = scarlett2_read_configs(mixer);
2107 /* Create the analogue output controls */
2108 err = scarlett2_add_line_out_ctls(mixer);
2112 /* Create the analogue input controls */
2113 err = scarlett2_add_line_in_ctls(mixer);
2117 /* Create the input, output, and mixer mux input selections */
2118 err = scarlett2_add_mux_enums(mixer);
2122 /* Create the matrix mixer controls */
2123 err = scarlett2_add_mixer_ctls(mixer);
2127 /* Create the level meter controls */
2128 err = scarlett2_add_meter_ctl(mixer);
2132 /* Set up the interrupt polling if there are hardware buttons */
2133 if (info->button_count) {
2134 err = scarlett2_mixer_status_create(mixer);
2142 int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
2144 struct snd_usb_audio *chip = mixer->chip;
2145 const struct scarlett2_device_info *info;
2148 /* only use UAC_VERSION_2 */
2149 if (!mixer->protocol)
2152 switch (chip->usb_id) {
2153 case USB_ID(0x1235, 0x8203):
2154 info = &s6i6_gen2_info;
2156 case USB_ID(0x1235, 0x8204):
2157 info = &s18i8_gen2_info;
2159 case USB_ID(0x1235, 0x8201):
2160 info = &s18i20_gen2_info;
2162 default: /* device not (yet) supported */
2166 if (!(chip->setup & SCARLETT2_ENABLE)) {
2167 usb_audio_info(chip,
2168 "Focusrite Scarlett Gen 2 Mixer Driver disabled; "
2169 "use options snd_usb_audio vid=0x%04x pid=0x%04x "
2170 "device_setup=1 to enable and report any issues "
2172 USB_ID_VENDOR(chip->usb_id),
2173 USB_ID_PRODUCT(chip->usb_id));
2177 usb_audio_info(chip,
2178 "Focusrite Scarlett Gen 2 Mixer Driver enabled pid=0x%04x",
2179 USB_ID_PRODUCT(chip->usb_id));
2181 err = snd_scarlett_gen2_controls_create(mixer, info);
2183 usb_audio_err(mixer->chip,
2184 "Error initialising Scarlett Mixer Driver: %d",