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 * This ALSA mixer gives access to:
36 * - input, output, mixer-matrix muxes
37 * - 18x10 mixer-matrix gain stages
38 * - gain/volume controls
40 * - line/inst level and pad controls
43 * /--------------\ 18chn 20chn /--------------\
44 * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
45 * \--------------/ | | | | \--------------/
49 * | +---------------+ | |
50 * | \ Matrix Mux / | |
57 * | +------------+ | |
61 * | | 18x10 Gain | | |
63 * | +-----+------+ | |
65 * |18chn |10chn | |20chn
70 * ===========================
71 * +---------------+ +--—------------+
72 * \ Output Mux / \ Capture Mux /
73 * +---+---+---+ +-----+-----+
77 * /--------------\ | | | /--------------\
78 * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
79 * | Hardware out | | \--------------/
82 * +-------------+ Software gain per channel.
83 * | Master Gain |<-- 18i20 only: Switch per channel
84 * +------+------+ to select HW or SW gain control.
88 * | Analogue |<------/
95 #include <linux/slab.h>
96 #include <linux/usb.h>
97 #include <linux/moduleparam.h>
99 #include <sound/control.h>
100 #include <sound/tlv.h>
102 #include "usbaudio.h"
106 #include "mixer_scarlett_gen2.h"
108 /* device_setup value to enable */
109 #define SCARLETT2_ENABLE 0x01
111 /* some gui mixers can't handle negative ctl values */
112 #define SCARLETT2_VOLUME_BIAS 127
114 /* mixer range from -80dB to +6dB in 0.5dB steps */
115 #define SCARLETT2_MIXER_MIN_DB -80
116 #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
117 #define SCARLETT2_MIXER_MAX_DB 6
118 #define SCARLETT2_MIXER_MAX_VALUE \
119 ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
121 /* map from (dB + 80) * 2 to mixer value
122 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
124 static const u16 scarlett2_mixer_values[173] = {
125 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
126 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
127 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
128 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
129 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
130 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
131 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
132 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
133 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
134 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
135 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
136 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
137 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
141 /* Maximum number of analogue outputs */
142 #define SCARLETT2_ANALOGUE_MAX 10
144 /* Maximum number of level and pad switches */
145 #define SCARLETT2_LEVEL_SWITCH_MAX 2
146 #define SCARLETT2_PAD_SWITCH_MAX 4
148 /* Maximum number of inputs to the mixer */
149 #define SCARLETT2_INPUT_MIX_MAX 18
151 /* Maximum number of outputs from the mixer */
152 #define SCARLETT2_OUTPUT_MIX_MAX 10
154 /* Maximum size of the data in the USB mux assignment message:
155 * 18 inputs, 20 outputs, 18 matrix inputs, 8 spare
157 #define SCARLETT2_MUX_MAX 64
160 * 18 inputs, 20 outputs, 18 matrix inputs
162 #define SCARLETT2_NUM_METERS 56
164 /* Hardware port types:
165 * - None (no input to mux)
173 SCARLETT2_PORT_TYPE_NONE = 0,
174 SCARLETT2_PORT_TYPE_ANALOGUE = 1,
175 SCARLETT2_PORT_TYPE_SPDIF = 2,
176 SCARLETT2_PORT_TYPE_ADAT = 3,
177 SCARLETT2_PORT_TYPE_MIX = 4,
178 SCARLETT2_PORT_TYPE_PCM = 5,
179 SCARLETT2_PORT_TYPE_COUNT = 6,
182 /* Count of total I/O and number available at each sample rate */
184 SCARLETT2_PORT_IN = 0,
185 SCARLETT2_PORT_OUT = 1,
186 SCARLETT2_PORT_OUT_44 = 2,
187 SCARLETT2_PORT_OUT_88 = 3,
188 SCARLETT2_PORT_OUT_176 = 4,
189 SCARLETT2_PORT_DIRECTIONS = 5,
192 /* Hardware buttons on the 18i20 */
193 #define SCARLETT2_BUTTON_MAX 2
195 static const char *const scarlett2_button_names[SCARLETT2_BUTTON_MAX] = {
199 /* Description of each hardware port type:
200 * - id: hardware ID for this port type
201 * - num: number of sources/destinations of this port type
202 * - src_descr: printf format string for mux input selections
203 * - src_num_offset: added to channel number for the fprintf
204 * - dst_descr: printf format string for mixer controls
206 struct scarlett2_ports {
208 int num[SCARLETT2_PORT_DIRECTIONS];
209 const char * const src_descr;
211 const char * const dst_descr;
214 struct scarlett2_device_info {
215 u8 line_out_hw_vol; /* line out hw volume is sw controlled */
216 u8 button_count; /* number of buttons */
217 u8 level_input_count; /* inputs with level selectable */
218 u8 pad_input_count; /* inputs with pad selectable */
219 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
220 struct scarlett2_ports ports[SCARLETT2_PORT_TYPE_COUNT];
223 struct scarlett2_mixer_data {
224 struct usb_mixer_interface *mixer;
225 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
226 struct mutex data_mutex; /* lock access to this data */
227 struct delayed_work work;
228 const struct scarlett2_device_info *info;
233 u8 vol[SCARLETT2_ANALOGUE_MAX];
234 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
235 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
236 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
237 u8 buttons[SCARLETT2_BUTTON_MAX];
238 struct snd_kcontrol *master_vol_ctl;
239 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
240 struct snd_kcontrol *button_ctls[SCARLETT2_BUTTON_MAX];
241 u8 mux[SCARLETT2_MUX_MAX];
242 u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
245 /*** Model-specific data ***/
247 static const struct scarlett2_device_info s6i6_gen2_info = {
248 /* The first two analogue inputs can be switched between line
249 * and instrument levels.
251 .level_input_count = 2,
253 /* The first two analogue inputs have an optional pad. */
254 .pad_input_count = 2,
264 [SCARLETT2_PORT_TYPE_NONE] = {
266 .num = { 1, 0, 8, 8, 8 },
270 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
272 .num = { 4, 4, 4, 4, 4 },
273 .src_descr = "Analogue %d",
275 .dst_descr = "Analogue Output %02d Playback"
277 [SCARLETT2_PORT_TYPE_SPDIF] = {
279 .num = { 2, 2, 2, 2, 2 },
280 .src_descr = "S/PDIF %d",
282 .dst_descr = "S/PDIF Output %d Playback"
284 [SCARLETT2_PORT_TYPE_MIX] = {
286 .num = { 10, 18, 18, 18, 18 },
287 .src_descr = "Mix %c",
288 .src_num_offset = 65,
289 .dst_descr = "Mixer Input %02d Capture"
291 [SCARLETT2_PORT_TYPE_PCM] = {
293 .num = { 6, 6, 6, 6, 6 },
294 .src_descr = "PCM %d",
296 .dst_descr = "PCM %02d Capture"
301 static const struct scarlett2_device_info s18i8_gen2_info = {
302 /* The first two analogue inputs can be switched between line
303 * and instrument levels.
305 .level_input_count = 2,
307 /* The first four analogue inputs have an optional pad. */
308 .pad_input_count = 4,
320 [SCARLETT2_PORT_TYPE_NONE] = {
322 .num = { 1, 0, 8, 8, 4 },
326 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
328 .num = { 8, 6, 6, 6, 6 },
329 .src_descr = "Analogue %d",
331 .dst_descr = "Analogue Output %02d Playback"
333 [SCARLETT2_PORT_TYPE_SPDIF] = {
335 /* S/PDIF outputs aren't available at 192kHz
336 * but are included in the USB mux I/O
337 * assignment message anyway
339 .num = { 2, 2, 2, 2, 2 },
340 .src_descr = "S/PDIF %d",
342 .dst_descr = "S/PDIF Output %d Playback"
344 [SCARLETT2_PORT_TYPE_ADAT] = {
346 .num = { 8, 0, 0, 0, 0 },
347 .src_descr = "ADAT %d",
350 [SCARLETT2_PORT_TYPE_MIX] = {
352 .num = { 10, 18, 18, 18, 18 },
353 .src_descr = "Mix %c",
354 .src_num_offset = 65,
355 .dst_descr = "Mixer Input %02d Capture"
357 [SCARLETT2_PORT_TYPE_PCM] = {
359 .num = { 8, 18, 18, 14, 10 },
360 .src_descr = "PCM %d",
362 .dst_descr = "PCM %02d Capture"
367 static const struct scarlett2_device_info s18i20_gen2_info = {
368 /* The analogue line outputs on the 18i20 can be switched
369 * between software and hardware volume control
371 .line_out_hw_vol = 1,
373 /* Mute and dim buttons */
390 [SCARLETT2_PORT_TYPE_NONE] = {
392 .num = { 1, 0, 8, 8, 6 },
396 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
398 .num = { 8, 10, 10, 10, 10 },
399 .src_descr = "Analogue %d",
401 .dst_descr = "Analogue Output %02d Playback"
403 [SCARLETT2_PORT_TYPE_SPDIF] = {
404 /* S/PDIF outputs aren't available at 192kHz
405 * but are included in the USB mux I/O
406 * assignment message anyway
409 .num = { 2, 2, 2, 2, 2 },
410 .src_descr = "S/PDIF %d",
412 .dst_descr = "S/PDIF Output %d Playback"
414 [SCARLETT2_PORT_TYPE_ADAT] = {
416 .num = { 8, 8, 8, 4, 0 },
417 .src_descr = "ADAT %d",
419 .dst_descr = "ADAT Output %d Playback"
421 [SCARLETT2_PORT_TYPE_MIX] = {
423 .num = { 10, 18, 18, 18, 18 },
424 .src_descr = "Mix %c",
425 .src_num_offset = 65,
426 .dst_descr = "Mixer Input %02d Capture"
428 [SCARLETT2_PORT_TYPE_PCM] = {
430 .num = { 20, 18, 18, 14, 10 },
431 .src_descr = "PCM %d",
433 .dst_descr = "PCM %02d Capture"
438 /* get the starting port index number for a given port type/direction */
439 static int scarlett2_get_port_start_num(const struct scarlett2_ports *ports,
440 int direction, int port_type)
444 for (i = 0; i < port_type; i++)
445 num += ports[i].num[direction];
450 /*** USB Interactions ***/
452 /* Vendor-Specific Interface, Endpoint, MaxPacketSize, Interval */
453 #define SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE 5
454 #define SCARLETT2_USB_INTERRUPT_ENDPOINT 4
455 #define SCARLETT2_USB_INTERRUPT_MAX_DATA 64
456 #define SCARLETT2_USB_INTERRUPT_INTERVAL 3
458 /* Interrupt flags for volume and mute/dim button changes */
459 #define SCARLETT2_USB_INTERRUPT_VOL_CHANGE 0x400000
460 #define SCARLETT2_USB_INTERRUPT_BUTTON_CHANGE 0x200000
462 /* Commands for sending/receiving requests/responses */
463 #define SCARLETT2_USB_VENDOR_SPECIFIC_CMD_REQ 2
464 #define SCARLETT2_USB_VENDOR_SPECIFIC_CMD_RESP 3
466 #define SCARLETT2_USB_INIT_SEQ 0x00000000
467 #define SCARLETT2_USB_GET_METER_LEVELS 0x00001001
468 #define SCARLETT2_USB_SET_MIX 0x00002002
469 #define SCARLETT2_USB_SET_MUX 0x00003002
470 #define SCARLETT2_USB_GET_DATA 0x00800000
471 #define SCARLETT2_USB_SET_DATA 0x00800001
472 #define SCARLETT2_USB_DATA_CMD 0x00800002
473 #define SCARLETT2_USB_CONFIG_SAVE 6
475 #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
476 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
478 /* volume status is read together (matches scarlett2_config_items[]) */
479 struct scarlett2_usb_volume_status {
480 /* mute & dim buttons */
481 u8 buttons[SCARLETT2_BUTTON_MAX];
485 /* software volume setting */
486 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
488 /* actual volume of output inc. dim (-18dB) */
489 s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
491 u8 pad2[SCARLETT2_ANALOGUE_MAX];
493 /* sw (0) or hw (1) controlled */
494 u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
498 /* front panel volume knob */
502 /* Configuration parameters that can be read and written */
504 SCARLETT2_CONFIG_BUTTONS = 0,
505 SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
506 SCARLETT2_CONFIG_SW_HW_SWITCH = 2,
507 SCARLETT2_CONFIG_LEVEL_SWITCH = 3,
508 SCARLETT2_CONFIG_PAD_SWITCH = 4,
509 SCARLETT2_CONFIG_COUNT = 5
512 /* Location, size, and activation command number for the configuration
515 struct scarlett2_config {
521 static const struct scarlett2_config
522 scarlett2_config_items[SCARLETT2_CONFIG_COUNT] = {
523 /* Mute/Dim Buttons */
530 /* Line Out Volume */
537 /* SW/HW Volume Switch */
559 /* proprietary request/response format */
560 struct scarlett2_usb_packet {
569 #define SCARLETT2_USB_PACKET_LEN (sizeof(struct scarlett2_usb_packet))
571 static void scarlett2_fill_request_header(struct scarlett2_mixer_data *private,
572 struct scarlett2_usb_packet *req,
573 u32 cmd, u16 req_size)
575 /* sequence must go up by 1 for each request */
576 u16 seq = private->scarlett2_seq++;
578 req->cmd = cpu_to_le32(cmd);
579 req->size = cpu_to_le16(req_size);
580 req->seq = cpu_to_le16(seq);
585 /* Send a proprietary format request to the Scarlett interface */
586 static int scarlett2_usb(
587 struct usb_mixer_interface *mixer, u32 cmd,
588 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
590 struct scarlett2_mixer_data *private = mixer->private_data;
591 u16 req_buf_size = sizeof(struct scarlett2_usb_packet) + req_size;
592 u16 resp_buf_size = sizeof(struct scarlett2_usb_packet) + resp_size;
593 struct scarlett2_usb_packet *req = NULL, *resp = NULL;
596 req = kmalloc(req_buf_size, GFP_KERNEL);
602 resp = kmalloc(resp_buf_size, GFP_KERNEL);
608 mutex_lock(&private->usb_mutex);
610 /* build request message and send it */
612 scarlett2_fill_request_header(private, req, cmd, req_size);
615 memcpy(req->data, req_data, req_size);
617 err = snd_usb_ctl_msg(mixer->chip->dev,
618 usb_sndctrlpipe(mixer->chip->dev, 0),
619 SCARLETT2_USB_VENDOR_SPECIFIC_CMD_REQ,
620 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
622 SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE,
626 if (err != req_buf_size) {
629 "Scarlett Gen 2 USB request result cmd %x was %d\n",
635 /* send a second message to get the response */
637 err = snd_usb_ctl_msg(mixer->chip->dev,
638 usb_rcvctrlpipe(mixer->chip->dev, 0),
639 SCARLETT2_USB_VENDOR_SPECIFIC_CMD_RESP,
640 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
642 SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE,
646 /* validate the response */
648 if (err != resp_buf_size) {
651 "Scarlett Gen 2 USB response result cmd %x was %d\n",
657 if (resp->cmd != req->cmd ||
658 resp->seq != req->seq ||
659 resp_size != le16_to_cpu(resp->size) ||
664 "Scarlett Gen 2 USB invalid response; "
665 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
667 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
668 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
669 resp_size, le16_to_cpu(resp->size),
670 le32_to_cpu(resp->error),
671 le32_to_cpu(resp->pad));
677 memcpy(resp_data, resp->data, resp_size);
680 mutex_unlock(&private->usb_mutex);
687 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
688 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
690 __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
692 scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
697 /* Delayed work to save config */
698 static void scarlett2_config_save_work(struct work_struct *work)
700 struct scarlett2_mixer_data *private =
701 container_of(work, struct scarlett2_mixer_data, work.work);
703 scarlett2_config_save(private->mixer);
706 /* Send a USB message to set a configuration parameter (volume level,
707 * sw/hw volume switch, line/inst level switch, or pad switch)
709 static int scarlett2_usb_set_config(
710 struct usb_mixer_interface *mixer,
711 int config_item_num, int index, int value)
713 const struct scarlett2_config config_item =
714 scarlett2_config_items[config_item_num];
722 struct scarlett2_mixer_data *private = mixer->private_data;
724 /* Cancel any pending NVRAM save */
725 cancel_delayed_work_sync(&private->work);
727 /* Send the configuration parameter data */
728 req.offset = cpu_to_le32(config_item.offset + index * config_item.size);
729 req.bytes = cpu_to_le32(config_item.size);
730 req.value = cpu_to_le32(value);
731 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
732 &req, sizeof(u32) * 2 + config_item.size,
737 /* Activate the change */
738 req2 = cpu_to_le32(config_item.activate);
739 err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
740 &req2, sizeof(req2), NULL, 0);
744 /* Schedule the change to be written to NVRAM */
745 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
750 /* Send a USB message to get data; result placed in *buf */
751 static int scarlett2_usb_get(
752 struct usb_mixer_interface *mixer,
753 int offset, void *buf, int size)
760 req.offset = cpu_to_le32(offset);
761 req.size = cpu_to_le32(size);
762 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
763 &req, sizeof(req), buf, size);
766 /* Send a USB message to get configuration parameters; result placed in *buf */
767 static int scarlett2_usb_get_config(
768 struct usb_mixer_interface *mixer,
769 int config_item_num, int count, void *buf)
771 const struct scarlett2_config config_item =
772 scarlett2_config_items[config_item_num];
773 int size = config_item.size * count;
775 return scarlett2_usb_get(mixer, config_item.offset, buf, size);
778 /* Send a USB message to get volume status; result placed in *buf */
779 static int scarlett2_usb_get_volume_status(
780 struct usb_mixer_interface *mixer,
781 struct scarlett2_usb_volume_status *buf)
783 return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
787 /* Send a USB message to set the volumes for all inputs of one mix
788 * (values obtained from private->mix[])
790 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
793 struct scarlett2_mixer_data *private = mixer->private_data;
794 const struct scarlett2_device_info *info = private->info;
798 __le16 data[SCARLETT2_INPUT_MIX_MAX];
803 info->ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
805 req.mix_num = cpu_to_le16(mix_num);
807 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
808 req.data[i] = cpu_to_le16(
809 scarlett2_mixer_values[private->mix[j]]
812 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
813 &req, (num_mixer_in + 1) * sizeof(u16),
817 /* Convert a port number index (per info->ports) to a hardware ID */
818 static u32 scarlett2_mux_src_num_to_id(const struct scarlett2_ports *ports,
824 port_type < SCARLETT2_PORT_TYPE_COUNT;
826 if (num < ports[port_type].num[SCARLETT2_PORT_IN])
827 return ports[port_type].id | num;
828 num -= ports[port_type].num[SCARLETT2_PORT_IN];
835 /* Send USB messages to set mux inputs */
836 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
838 struct scarlett2_mixer_data *private = mixer->private_data;
839 const struct scarlett2_device_info *info = private->info;
840 const struct scarlett2_ports *ports = info->ports;
841 int rate, port_dir_rate;
843 static const int assignment_order[SCARLETT2_PORT_TYPE_COUNT] = {
844 SCARLETT2_PORT_TYPE_PCM,
845 SCARLETT2_PORT_TYPE_ANALOGUE,
846 SCARLETT2_PORT_TYPE_SPDIF,
847 SCARLETT2_PORT_TYPE_ADAT,
848 SCARLETT2_PORT_TYPE_MIX,
849 SCARLETT2_PORT_TYPE_NONE,
855 __le32 data[SCARLETT2_MUX_MAX];
860 /* mux settings for each rate */
861 for (rate = 0, port_dir_rate = SCARLETT2_PORT_OUT_44;
862 port_dir_rate <= SCARLETT2_PORT_OUT_176;
863 rate++, port_dir_rate++) {
864 int order_num, i, err;
866 req.num = cpu_to_le16(rate);
868 for (order_num = 0, i = 0;
869 order_num < SCARLETT2_PORT_TYPE_COUNT;
871 int port_type = assignment_order[order_num];
872 int j = scarlett2_get_port_start_num(ports,
875 int port_id = ports[port_type].id;
879 channel < ports[port_type].num[port_dir_rate];
881 /* lower 12 bits for the destination and
882 * next 12 bits for the source
884 req.data[i] = !port_id
889 scarlett2_mux_src_num_to_id(
890 ports, private->mux[j]
894 /* skip private->mux[j] entries not output */
895 j += ports[port_type].num[SCARLETT2_PORT_OUT] -
896 ports[port_type].num[port_dir_rate];
899 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
900 &req, (i + 1) * sizeof(u32),
909 /* Send USB message to get meter levels */
910 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
918 u32 resp[SCARLETT2_NUM_METERS];
922 req.num_meters = cpu_to_le16(SCARLETT2_NUM_METERS);
923 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
924 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER_LEVELS,
925 &req, sizeof(req), resp, sizeof(resp));
929 /* copy, convert to u16 */
930 for (i = 0; i < SCARLETT2_NUM_METERS; i++)
936 /*** Control Functions ***/
938 /* helper function to create a new control */
939 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
940 const struct snd_kcontrol_new *ncontrol,
941 int index, int channels, const char *name,
942 struct snd_kcontrol **kctl_return)
944 struct snd_kcontrol *kctl;
945 struct usb_mixer_elem_info *elem;
948 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
952 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
953 * ignores them for resume and other operations.
954 * Also, the head.id field is set to 0, as we don't use this field.
956 elem->head.mixer = mixer;
957 elem->control = index;
959 elem->channels = channels;
960 elem->val_type = USB_MIXER_BESPOKEN;
962 kctl = snd_ctl_new1(ncontrol, elem);
967 kctl->private_free = snd_usb_mixer_elem_free;
969 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
971 err = snd_usb_mixer_add_control(&elem->head, kctl);
981 /*** Analogue Line Out Volume Controls ***/
983 /* Update hardware volume controls after receiving notification that
986 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
988 struct scarlett2_mixer_data *private = mixer->private_data;
989 const struct scarlett2_ports *ports = private->info->ports;
990 struct scarlett2_usb_volume_status volume_status;
992 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
995 private->vol_updated = 0;
997 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1001 private->master_vol = clamp(
1002 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1003 0, SCARLETT2_VOLUME_BIAS);
1005 for (i = 0; i < num_line_out; i++) {
1006 if (private->vol_sw_hw_switch[i])
1007 private->vol[i] = private->master_vol;
1010 for (i = 0; i < private->info->button_count; i++)
1011 private->buttons[i] = !!volume_status.buttons[i];
1016 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1017 struct snd_ctl_elem_info *uinfo)
1019 struct usb_mixer_elem_info *elem = kctl->private_data;
1021 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1022 uinfo->count = elem->channels;
1023 uinfo->value.integer.min = 0;
1024 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1025 uinfo->value.integer.step = 1;
1029 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1030 struct snd_ctl_elem_value *ucontrol)
1032 struct usb_mixer_elem_info *elem = kctl->private_data;
1033 struct usb_mixer_interface *mixer = elem->head.mixer;
1034 struct scarlett2_mixer_data *private = mixer->private_data;
1036 mutex_lock(&private->data_mutex);
1037 if (private->vol_updated)
1038 scarlett2_update_volumes(mixer);
1039 mutex_unlock(&private->data_mutex);
1041 ucontrol->value.integer.value[0] = private->master_vol;
1045 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1046 struct snd_ctl_elem_value *ucontrol)
1048 struct usb_mixer_elem_info *elem = kctl->private_data;
1049 struct usb_mixer_interface *mixer = elem->head.mixer;
1050 struct scarlett2_mixer_data *private = mixer->private_data;
1051 int index = elem->control;
1053 mutex_lock(&private->data_mutex);
1054 if (private->vol_updated)
1055 scarlett2_update_volumes(mixer);
1056 mutex_unlock(&private->data_mutex);
1058 ucontrol->value.integer.value[0] = private->vol[index];
1062 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1063 struct snd_ctl_elem_value *ucontrol)
1065 struct usb_mixer_elem_info *elem = kctl->private_data;
1066 struct usb_mixer_interface *mixer = elem->head.mixer;
1067 struct scarlett2_mixer_data *private = mixer->private_data;
1068 int index = elem->control;
1069 int oval, val, err = 0;
1071 mutex_lock(&private->data_mutex);
1073 oval = private->vol[index];
1074 val = ucontrol->value.integer.value[0];
1079 private->vol[index] = val;
1080 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1081 index, val - SCARLETT2_VOLUME_BIAS);
1086 mutex_unlock(&private->data_mutex);
1090 static const DECLARE_TLV_DB_MINMAX(
1091 db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1094 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1095 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1096 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1097 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1099 .info = scarlett2_volume_ctl_info,
1100 .get = scarlett2_master_volume_ctl_get,
1101 .private_value = 0, /* max value */
1102 .tlv = { .p = db_scale_scarlett2_gain }
1105 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1106 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1107 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1108 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1110 .info = scarlett2_volume_ctl_info,
1111 .get = scarlett2_volume_ctl_get,
1112 .put = scarlett2_volume_ctl_put,
1113 .private_value = 0, /* max value */
1114 .tlv = { .p = db_scale_scarlett2_gain }
1117 /*** HW/SW Volume Switch Controls ***/
1119 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
1120 struct snd_ctl_elem_info *uinfo)
1122 static const char *const values[2] = {
1126 return snd_ctl_enum_info(uinfo, 1, 2, values);
1129 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
1130 struct snd_ctl_elem_value *ucontrol)
1132 struct usb_mixer_elem_info *elem = kctl->private_data;
1133 struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1135 ucontrol->value.enumerated.item[0] =
1136 private->vol_sw_hw_switch[elem->control];
1140 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
1141 struct snd_ctl_elem_value *ucontrol)
1143 struct usb_mixer_elem_info *elem = kctl->private_data;
1144 struct usb_mixer_interface *mixer = elem->head.mixer;
1145 struct scarlett2_mixer_data *private = mixer->private_data;
1147 int index = elem->control;
1148 int oval, val, err = 0;
1150 mutex_lock(&private->data_mutex);
1152 oval = private->vol_sw_hw_switch[index];
1153 val = !!ucontrol->value.integer.value[0];
1158 private->vol_sw_hw_switch[index] = val;
1160 /* Change access mode to RO (hardware controlled volume)
1161 * or RW (software controlled volume)
1164 private->vol_ctls[index]->vd[0].access &=
1165 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1167 private->vol_ctls[index]->vd[0].access |=
1168 SNDRV_CTL_ELEM_ACCESS_WRITE;
1170 /* Reset volume to master volume */
1171 private->vol[index] = private->master_vol;
1173 /* Set SW volume to current HW volume */
1174 err = scarlett2_usb_set_config(
1175 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1176 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
1180 /* Notify of RO/RW change */
1181 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_INFO,
1182 &private->vol_ctls[index]->id);
1184 /* Send SW/HW switch change to the device */
1185 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
1191 mutex_unlock(&private->data_mutex);
1195 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
1196 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1198 .info = scarlett2_sw_hw_enum_ctl_info,
1199 .get = scarlett2_sw_hw_enum_ctl_get,
1200 .put = scarlett2_sw_hw_enum_ctl_put,
1203 /*** Line Level/Instrument Level Switch Controls ***/
1205 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
1206 struct snd_ctl_elem_info *uinfo)
1208 static const char *const values[2] = {
1212 return snd_ctl_enum_info(uinfo, 1, 2, values);
1215 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
1216 struct snd_ctl_elem_value *ucontrol)
1218 struct usb_mixer_elem_info *elem = kctl->private_data;
1219 struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1221 ucontrol->value.enumerated.item[0] =
1222 private->level_switch[elem->control];
1226 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
1227 struct snd_ctl_elem_value *ucontrol)
1229 struct usb_mixer_elem_info *elem = kctl->private_data;
1230 struct usb_mixer_interface *mixer = elem->head.mixer;
1231 struct scarlett2_mixer_data *private = mixer->private_data;
1233 int index = elem->control;
1234 int oval, val, err = 0;
1236 mutex_lock(&private->data_mutex);
1238 oval = private->level_switch[index];
1239 val = !!ucontrol->value.integer.value[0];
1244 private->level_switch[index] = val;
1246 /* Send switch change to the device */
1247 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
1253 mutex_unlock(&private->data_mutex);
1257 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
1258 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1260 .info = scarlett2_level_enum_ctl_info,
1261 .get = scarlett2_level_enum_ctl_get,
1262 .put = scarlett2_level_enum_ctl_put,
1265 /*** Pad Switch Controls ***/
1267 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
1268 struct snd_ctl_elem_value *ucontrol)
1270 struct usb_mixer_elem_info *elem = kctl->private_data;
1271 struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1273 ucontrol->value.enumerated.item[0] =
1274 private->pad_switch[elem->control];
1278 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
1279 struct snd_ctl_elem_value *ucontrol)
1281 struct usb_mixer_elem_info *elem = kctl->private_data;
1282 struct usb_mixer_interface *mixer = elem->head.mixer;
1283 struct scarlett2_mixer_data *private = mixer->private_data;
1285 int index = elem->control;
1286 int oval, val, err = 0;
1288 mutex_lock(&private->data_mutex);
1290 oval = private->pad_switch[index];
1291 val = !!ucontrol->value.integer.value[0];
1296 private->pad_switch[index] = val;
1298 /* Send switch change to the device */
1299 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
1305 mutex_unlock(&private->data_mutex);
1309 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
1310 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1312 .info = snd_ctl_boolean_mono_info,
1313 .get = scarlett2_pad_ctl_get,
1314 .put = scarlett2_pad_ctl_put,
1317 /*** Mute/Dim Controls ***/
1319 static int scarlett2_button_ctl_get(struct snd_kcontrol *kctl,
1320 struct snd_ctl_elem_value *ucontrol)
1322 struct usb_mixer_elem_info *elem = kctl->private_data;
1323 struct usb_mixer_interface *mixer = elem->head.mixer;
1324 struct scarlett2_mixer_data *private = mixer->private_data;
1326 mutex_lock(&private->data_mutex);
1327 if (private->vol_updated)
1328 scarlett2_update_volumes(mixer);
1329 mutex_unlock(&private->data_mutex);
1331 ucontrol->value.enumerated.item[0] = private->buttons[elem->control];
1335 static int scarlett2_button_ctl_put(struct snd_kcontrol *kctl,
1336 struct snd_ctl_elem_value *ucontrol)
1338 struct usb_mixer_elem_info *elem = kctl->private_data;
1339 struct usb_mixer_interface *mixer = elem->head.mixer;
1340 struct scarlett2_mixer_data *private = mixer->private_data;
1342 int index = elem->control;
1343 int oval, val, err = 0;
1345 mutex_lock(&private->data_mutex);
1347 oval = private->buttons[index];
1348 val = !!ucontrol->value.integer.value[0];
1353 private->buttons[index] = val;
1355 /* Send switch change to the device */
1356 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_BUTTONS,
1362 mutex_unlock(&private->data_mutex);
1366 static const struct snd_kcontrol_new scarlett2_button_ctl = {
1367 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1369 .info = snd_ctl_boolean_mono_info,
1370 .get = scarlett2_button_ctl_get,
1371 .put = scarlett2_button_ctl_put
1374 /*** Create the analogue output controls ***/
1376 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
1378 struct scarlett2_mixer_data *private = mixer->private_data;
1379 const struct scarlett2_device_info *info = private->info;
1380 const struct scarlett2_ports *ports = info->ports;
1382 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1384 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1386 /* Add R/O HW volume control */
1387 if (info->line_out_hw_vol) {
1388 snprintf(s, sizeof(s), "Master HW Playback Volume");
1389 err = scarlett2_add_new_ctl(mixer,
1390 &scarlett2_master_volume_ctl,
1391 0, 1, s, &private->master_vol_ctl);
1396 /* Add volume controls */
1397 for (i = 0; i < num_line_out; i++) {
1400 if (info->line_out_descrs[i])
1401 snprintf(s, sizeof(s),
1402 "Line %02d (%s) Playback Volume",
1403 i + 1, info->line_out_descrs[i]);
1405 snprintf(s, sizeof(s),
1406 "Line %02d Playback Volume",
1408 err = scarlett2_add_new_ctl(mixer,
1409 &scarlett2_line_out_volume_ctl,
1410 i, 1, s, &private->vol_ctls[i]);
1414 /* Make the fader read-only if the SW/HW switch is set to HW */
1415 if (private->vol_sw_hw_switch[i])
1416 private->vol_ctls[i]->vd[0].access &=
1417 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1420 if (info->line_out_hw_vol) {
1421 snprintf(s, sizeof(s),
1422 "Line Out %02d Volume Control Playback Enum",
1424 err = scarlett2_add_new_ctl(mixer,
1425 &scarlett2_sw_hw_enum_ctl,
1432 /* Add HW button controls */
1433 for (i = 0; i < private->info->button_count; i++) {
1434 err = scarlett2_add_new_ctl(mixer, &scarlett2_button_ctl,
1435 i, 1, scarlett2_button_names[i],
1436 &private->button_ctls[i]);
1444 /*** Create the analogue input controls ***/
1446 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
1448 struct scarlett2_mixer_data *private = mixer->private_data;
1449 const struct scarlett2_device_info *info = private->info;
1451 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1453 /* Add input level (line/inst) controls */
1454 for (i = 0; i < info->level_input_count; i++) {
1455 snprintf(s, sizeof(s), "Line In %d Level Capture Enum", i + 1);
1456 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
1462 /* Add input pad controls */
1463 for (i = 0; i < info->pad_input_count; i++) {
1464 snprintf(s, sizeof(s), "Line In %d Pad Capture Switch", i + 1);
1465 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
1474 /*** Mixer Volume Controls ***/
1476 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
1477 struct snd_ctl_elem_info *uinfo)
1479 struct usb_mixer_elem_info *elem = kctl->private_data;
1481 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1482 uinfo->count = elem->channels;
1483 uinfo->value.integer.min = 0;
1484 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
1485 uinfo->value.integer.step = 1;
1489 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
1490 struct snd_ctl_elem_value *ucontrol)
1492 struct usb_mixer_elem_info *elem = kctl->private_data;
1493 struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1495 ucontrol->value.integer.value[0] = private->mix[elem->control];
1499 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
1500 struct snd_ctl_elem_value *ucontrol)
1502 struct usb_mixer_elem_info *elem = kctl->private_data;
1503 struct usb_mixer_interface *mixer = elem->head.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;
1507 int oval, val, num_mixer_in, mix_num, err = 0;
1509 mutex_lock(&private->data_mutex);
1511 oval = private->mix[elem->control];
1512 val = ucontrol->value.integer.value[0];
1513 num_mixer_in = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
1514 mix_num = elem->control / num_mixer_in;
1519 private->mix[elem->control] = val;
1520 err = scarlett2_usb_set_mix(mixer, mix_num);
1525 mutex_unlock(&private->data_mutex);
1529 static const DECLARE_TLV_DB_MINMAX(
1530 db_scale_scarlett2_mixer,
1531 SCARLETT2_MIXER_MIN_DB * 100,
1532 SCARLETT2_MIXER_MAX_DB * 100
1535 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
1536 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1537 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1538 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1540 .info = scarlett2_mixer_ctl_info,
1541 .get = scarlett2_mixer_ctl_get,
1542 .put = scarlett2_mixer_ctl_put,
1543 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
1544 .tlv = { .p = db_scale_scarlett2_mixer }
1547 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
1549 struct scarlett2_mixer_data *private = mixer->private_data;
1550 const struct scarlett2_ports *ports = private->info->ports;
1553 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1555 int num_inputs = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
1556 int num_outputs = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_IN];
1558 for (i = 0, index = 0; i < num_outputs; i++) {
1559 for (j = 0; j < num_inputs; j++, index++) {
1560 snprintf(s, sizeof(s),
1561 "Mix %c Input %02d Playback Volume",
1563 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
1573 /*** Mux Source Selection Controls ***/
1575 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
1576 struct snd_ctl_elem_info *uinfo)
1578 struct usb_mixer_elem_info *elem = kctl->private_data;
1579 struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1580 const struct scarlett2_ports *ports = private->info->ports;
1581 unsigned int item = uinfo->value.enumerated.item;
1582 int items = private->num_mux_srcs;
1585 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1586 uinfo->count = elem->channels;
1587 uinfo->value.enumerated.items = items;
1590 item = uinfo->value.enumerated.item = items - 1;
1593 port_type < SCARLETT2_PORT_TYPE_COUNT;
1595 if (item < ports[port_type].num[SCARLETT2_PORT_IN]) {
1596 sprintf(uinfo->value.enumerated.name,
1597 ports[port_type].src_descr,
1598 item + ports[port_type].src_num_offset);
1601 item -= ports[port_type].num[SCARLETT2_PORT_IN];
1607 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
1608 struct snd_ctl_elem_value *ucontrol)
1610 struct usb_mixer_elem_info *elem = kctl->private_data;
1611 struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1613 ucontrol->value.enumerated.item[0] = private->mux[elem->control];
1617 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
1618 struct snd_ctl_elem_value *ucontrol)
1620 struct usb_mixer_elem_info *elem = kctl->private_data;
1621 struct usb_mixer_interface *mixer = elem->head.mixer;
1622 struct scarlett2_mixer_data *private = mixer->private_data;
1623 int index = elem->control;
1624 int oval, val, err = 0;
1626 mutex_lock(&private->data_mutex);
1628 oval = private->mux[index];
1629 val = clamp(ucontrol->value.integer.value[0],
1630 0L, private->num_mux_srcs - 1L);
1635 private->mux[index] = val;
1636 err = scarlett2_usb_set_mux(mixer);
1641 mutex_unlock(&private->data_mutex);
1645 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
1646 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1648 .info = scarlett2_mux_src_enum_ctl_info,
1649 .get = scarlett2_mux_src_enum_ctl_get,
1650 .put = scarlett2_mux_src_enum_ctl_put,
1653 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
1655 struct scarlett2_mixer_data *private = mixer->private_data;
1656 const struct scarlett2_ports *ports = private->info->ports;
1657 int port_type, channel, i;
1659 for (i = 0, port_type = 0;
1660 port_type < SCARLETT2_PORT_TYPE_COUNT;
1663 channel < ports[port_type].num[SCARLETT2_PORT_OUT];
1666 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1667 const char *const descr = ports[port_type].dst_descr;
1669 snprintf(s, sizeof(s) - 5, descr, channel + 1);
1672 err = scarlett2_add_new_ctl(mixer,
1673 &scarlett2_mux_src_enum_ctl,
1683 /*** Meter Controls ***/
1685 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
1686 struct snd_ctl_elem_info *uinfo)
1688 struct usb_mixer_elem_info *elem = kctl->private_data;
1690 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1691 uinfo->count = elem->channels;
1692 uinfo->value.integer.min = 0;
1693 uinfo->value.integer.max = 4095;
1694 uinfo->value.integer.step = 1;
1698 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
1699 struct snd_ctl_elem_value *ucontrol)
1701 struct usb_mixer_elem_info *elem = kctl->private_data;
1702 u16 meter_levels[SCARLETT2_NUM_METERS];
1705 err = scarlett2_usb_get_meter_levels(elem->head.mixer, meter_levels);
1709 for (i = 0; i < elem->channels; i++)
1710 ucontrol->value.integer.value[i] = meter_levels[i];
1715 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
1716 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1717 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1719 .info = scarlett2_meter_ctl_info,
1720 .get = scarlett2_meter_ctl_get
1723 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
1725 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
1726 0, SCARLETT2_NUM_METERS,
1727 "Level Meter", NULL);
1730 /*** Cleanup/Suspend Callbacks ***/
1732 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
1734 struct scarlett2_mixer_data *private = mixer->private_data;
1736 cancel_delayed_work_sync(&private->work);
1738 mixer->private_data = NULL;
1741 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
1743 struct scarlett2_mixer_data *private = mixer->private_data;
1745 if (cancel_delayed_work_sync(&private->work))
1746 scarlett2_config_save(private->mixer);
1749 /*** Initialisation ***/
1751 static int scarlett2_count_mux_srcs(const struct scarlett2_ports *ports)
1753 int port_type, count = 0;
1756 port_type < SCARLETT2_PORT_TYPE_COUNT;
1758 count += ports[port_type].num[SCARLETT2_PORT_IN];
1763 /* Default routing connects PCM outputs and inputs to Analogue,
1766 static void scarlett2_init_routing(u8 *mux,
1767 const struct scarlett2_ports *ports)
1769 int i, input_num, input_count, port_type;
1770 int output_num, output_count, port_type_connect_num;
1772 static const int connect_order[] = {
1773 SCARLETT2_PORT_TYPE_ANALOGUE,
1774 SCARLETT2_PORT_TYPE_SPDIF,
1775 SCARLETT2_PORT_TYPE_ADAT,
1779 /* Assign PCM inputs (routing outputs) */
1780 output_num = scarlett2_get_port_start_num(ports,
1782 SCARLETT2_PORT_TYPE_PCM);
1783 output_count = ports[SCARLETT2_PORT_TYPE_PCM].num[SCARLETT2_PORT_OUT];
1785 for (port_type = connect_order[port_type_connect_num = 0];
1787 port_type = connect_order[++port_type_connect_num]) {
1788 input_num = scarlett2_get_port_start_num(
1789 ports, SCARLETT2_PORT_IN, port_type);
1790 input_count = ports[port_type].num[SCARLETT2_PORT_IN];
1792 i < input_count && output_count;
1793 i++, output_count--)
1794 mux[output_num++] = input_num++;
1797 /* Assign PCM outputs (routing inputs) */
1798 input_num = scarlett2_get_port_start_num(ports,
1800 SCARLETT2_PORT_TYPE_PCM);
1801 input_count = ports[SCARLETT2_PORT_TYPE_PCM].num[SCARLETT2_PORT_IN];
1803 for (port_type = connect_order[port_type_connect_num = 0];
1805 port_type = connect_order[++port_type_connect_num]) {
1806 output_num = scarlett2_get_port_start_num(
1807 ports, SCARLETT2_PORT_OUT, port_type);
1808 output_count = ports[port_type].num[SCARLETT2_PORT_OUT];
1810 i < output_count && input_count;
1812 mux[output_num++] = input_num++;
1816 /* Initialise private data, routing, sequence number */
1817 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
1818 const struct scarlett2_device_info *info)
1820 struct scarlett2_mixer_data *private =
1821 kzalloc(sizeof(struct scarlett2_mixer_data), GFP_KERNEL);
1826 mutex_init(&private->usb_mutex);
1827 mutex_init(&private->data_mutex);
1828 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
1829 private->info = info;
1830 private->num_mux_srcs = scarlett2_count_mux_srcs(info->ports);
1831 private->scarlett2_seq = 0;
1832 private->mixer = mixer;
1833 mixer->private_data = private;
1834 mixer->private_free = scarlett2_private_free;
1835 mixer->private_suspend = scarlett2_private_suspend;
1837 /* Setup default routing */
1838 scarlett2_init_routing(private->mux, info->ports);
1840 /* Initialise the sequence number used for the proprietary commands */
1841 return scarlett2_usb(mixer, SCARLETT2_USB_INIT_SEQ, NULL, 0, NULL, 0);
1844 /* Read line-in config and line-out volume settings on start */
1845 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
1847 struct scarlett2_mixer_data *private = mixer->private_data;
1848 const struct scarlett2_device_info *info = private->info;
1849 const struct scarlett2_ports *ports = info->ports;
1851 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1852 u8 level_switches[SCARLETT2_LEVEL_SWITCH_MAX];
1853 u8 pad_switches[SCARLETT2_PAD_SWITCH_MAX];
1854 struct scarlett2_usb_volume_status volume_status;
1857 if (info->level_input_count) {
1858 err = scarlett2_usb_get_config(
1860 SCARLETT2_CONFIG_LEVEL_SWITCH,
1861 info->level_input_count,
1865 for (i = 0; i < info->level_input_count; i++)
1866 private->level_switch[i] = level_switches[i];
1869 if (info->pad_input_count) {
1870 err = scarlett2_usb_get_config(
1872 SCARLETT2_CONFIG_PAD_SWITCH,
1873 info->pad_input_count,
1877 for (i = 0; i < info->pad_input_count; i++)
1878 private->pad_switch[i] = pad_switches[i];
1881 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1885 private->master_vol = clamp(
1886 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1887 0, SCARLETT2_VOLUME_BIAS);
1889 for (i = 0; i < num_line_out; i++) {
1892 private->vol_sw_hw_switch[i] =
1893 info->line_out_hw_vol
1894 && volume_status.sw_hw_switch[i];
1896 volume = private->vol_sw_hw_switch[i]
1897 ? volume_status.master_vol
1898 : volume_status.sw_vol[i];
1899 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
1900 0, SCARLETT2_VOLUME_BIAS);
1901 private->vol[i] = volume;
1904 for (i = 0; i < info->button_count; i++)
1905 private->buttons[i] = !!volume_status.buttons[i];
1910 /* Notify on volume change */
1911 static void scarlett2_mixer_interrupt_vol_change(
1912 struct usb_mixer_interface *mixer)
1914 struct scarlett2_mixer_data *private = mixer->private_data;
1915 const struct scarlett2_ports *ports = private->info->ports;
1917 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1920 private->vol_updated = 1;
1922 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1923 &private->master_vol_ctl->id);
1925 for (i = 0; i < num_line_out; i++) {
1926 if (!private->vol_sw_hw_switch[i])
1928 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1929 &private->vol_ctls[i]->id);
1933 /* Notify on button change */
1934 static void scarlett2_mixer_interrupt_button_change(
1935 struct usb_mixer_interface *mixer)
1937 struct scarlett2_mixer_data *private = mixer->private_data;
1940 private->vol_updated = 1;
1942 for (i = 0; i < private->info->button_count; i++)
1943 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1944 &private->button_ctls[i]->id);
1947 /* Interrupt callback */
1948 static void scarlett2_mixer_interrupt(struct urb *urb)
1950 struct usb_mixer_interface *mixer = urb->context;
1951 int len = urb->actual_length;
1952 int ustatus = urb->status;
1959 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
1960 if (data & SCARLETT2_USB_INTERRUPT_VOL_CHANGE)
1961 scarlett2_mixer_interrupt_vol_change(mixer);
1962 if (data & SCARLETT2_USB_INTERRUPT_BUTTON_CHANGE)
1963 scarlett2_mixer_interrupt_button_change(mixer);
1965 usb_audio_err(mixer->chip,
1966 "scarlett mixer interrupt length %d\n", len);
1970 if (ustatus != -ENOENT &&
1971 ustatus != -ECONNRESET &&
1972 ustatus != -ESHUTDOWN) {
1973 urb->dev = mixer->chip->dev;
1974 usb_submit_urb(urb, GFP_ATOMIC);
1978 static int scarlett2_mixer_status_create(struct usb_mixer_interface *mixer)
1980 struct usb_device *dev = mixer->chip->dev;
1981 unsigned int pipe = usb_rcvintpipe(dev,
1982 SCARLETT2_USB_INTERRUPT_ENDPOINT);
1983 void *transfer_buffer;
1986 usb_audio_err(mixer->chip,
1987 "%s: mixer urb already in use!\n", __func__);
1991 if (usb_pipe_type_check(dev, pipe))
1994 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1998 transfer_buffer = kmalloc(SCARLETT2_USB_INTERRUPT_MAX_DATA, GFP_KERNEL);
1999 if (!transfer_buffer)
2002 usb_fill_int_urb(mixer->urb, dev, pipe,
2003 transfer_buffer, SCARLETT2_USB_INTERRUPT_MAX_DATA,
2004 scarlett2_mixer_interrupt, mixer,
2005 SCARLETT2_USB_INTERRUPT_INTERVAL);
2007 return usb_submit_urb(mixer->urb, GFP_KERNEL);
2010 static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer,
2011 const struct scarlett2_device_info *info)
2015 /* Initialise private data, routing, sequence number */
2016 err = scarlett2_init_private(mixer, info);
2020 /* Read volume levels and controls from the interface */
2021 err = scarlett2_read_configs(mixer);
2025 /* Create the analogue output controls */
2026 err = scarlett2_add_line_out_ctls(mixer);
2030 /* Create the analogue input controls */
2031 err = scarlett2_add_line_in_ctls(mixer);
2035 /* Create the input, output, and mixer mux input selections */
2036 err = scarlett2_add_mux_enums(mixer);
2040 /* Create the matrix mixer controls */
2041 err = scarlett2_add_mixer_ctls(mixer);
2045 /* Create the level meter controls */
2046 err = scarlett2_add_meter_ctl(mixer);
2050 /* Set up the interrupt polling if there are hardware buttons */
2051 if (info->button_count) {
2052 err = scarlett2_mixer_status_create(mixer);
2060 int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
2062 struct snd_usb_audio *chip = mixer->chip;
2063 const struct scarlett2_device_info *info;
2066 /* only use UAC_VERSION_2 */
2067 if (!mixer->protocol)
2070 switch (chip->usb_id) {
2071 case USB_ID(0x1235, 0x8203):
2072 info = &s6i6_gen2_info;
2074 case USB_ID(0x1235, 0x8204):
2075 info = &s18i8_gen2_info;
2077 case USB_ID(0x1235, 0x8201):
2078 info = &s18i20_gen2_info;
2080 default: /* device not (yet) supported */
2084 if (!(chip->setup & SCARLETT2_ENABLE)) {
2085 usb_audio_info(chip,
2086 "Focusrite Scarlett Gen 2 Mixer Driver disabled; "
2087 "use options snd_usb_audio vid=0x%04x pid=0x%04x "
2088 "device_setup=1 to enable and report any issues "
2090 USB_ID_VENDOR(chip->usb_id),
2091 USB_ID_PRODUCT(chip->usb_id));
2095 usb_audio_info(chip,
2096 "Focusrite Scarlett Gen 2 Mixer Driver enabled pid=0x%04x",
2097 USB_ID_PRODUCT(chip->usb_id));
2099 err = snd_scarlett_gen2_controls_create(mixer, info);
2101 usb_audio_err(mixer->chip,
2102 "Error initialising Scarlett Mixer Driver: %d",