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
= { 20, 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_sndctrlpipe(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 elem
->head
.mixer
= mixer
;
953 elem
->control
= index
;
954 elem
->head
.id
= index
;
955 elem
->channels
= channels
;
957 kctl
= snd_ctl_new1(ncontrol
, elem
);
962 kctl
->private_free
= snd_usb_mixer_elem_free
;
964 strlcpy(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
));
966 err
= snd_usb_mixer_add_control(&elem
->head
, kctl
);
976 /*** Analogue Line Out Volume Controls ***/
978 /* Update hardware volume controls after receiving notification that
981 static int scarlett2_update_volumes(struct usb_mixer_interface
*mixer
)
983 struct scarlett2_mixer_data
*private = mixer
->private_data
;
984 const struct scarlett2_ports
*ports
= private->info
->ports
;
985 struct scarlett2_usb_volume_status volume_status
;
987 ports
[SCARLETT2_PORT_TYPE_ANALOGUE
].num
[SCARLETT2_PORT_OUT
];
990 private->vol_updated
= 0;
992 err
= scarlett2_usb_get_volume_status(mixer
, &volume_status
);
996 private->master_vol
= clamp(
997 volume_status
.master_vol
+ SCARLETT2_VOLUME_BIAS
,
998 0, SCARLETT2_VOLUME_BIAS
);
1000 for (i
= 0; i
< num_line_out
; i
++) {
1001 if (private->vol_sw_hw_switch
[i
])
1002 private->vol
[i
] = private->master_vol
;
1005 for (i
= 0; i
< private->info
->button_count
; i
++)
1006 private->buttons
[i
] = !!volume_status
.buttons
[i
];
1011 static int scarlett2_volume_ctl_info(struct snd_kcontrol
*kctl
,
1012 struct snd_ctl_elem_info
*uinfo
)
1014 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1016 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1017 uinfo
->count
= elem
->channels
;
1018 uinfo
->value
.integer
.min
= 0;
1019 uinfo
->value
.integer
.max
= SCARLETT2_VOLUME_BIAS
;
1020 uinfo
->value
.integer
.step
= 1;
1024 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol
*kctl
,
1025 struct snd_ctl_elem_value
*ucontrol
)
1027 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1028 struct usb_mixer_interface
*mixer
= elem
->head
.mixer
;
1029 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1031 if (private->vol_updated
) {
1032 mutex_lock(&private->data_mutex
);
1033 scarlett2_update_volumes(mixer
);
1034 mutex_unlock(&private->data_mutex
);
1037 ucontrol
->value
.integer
.value
[0] = private->master_vol
;
1041 static int scarlett2_volume_ctl_get(struct snd_kcontrol
*kctl
,
1042 struct snd_ctl_elem_value
*ucontrol
)
1044 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1045 struct usb_mixer_interface
*mixer
= elem
->head
.mixer
;
1046 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1047 int index
= elem
->control
;
1049 if (private->vol_updated
) {
1050 mutex_lock(&private->data_mutex
);
1051 scarlett2_update_volumes(mixer
);
1052 mutex_unlock(&private->data_mutex
);
1055 ucontrol
->value
.integer
.value
[0] = private->vol
[index
];
1059 static int scarlett2_volume_ctl_put(struct snd_kcontrol
*kctl
,
1060 struct snd_ctl_elem_value
*ucontrol
)
1062 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1063 struct usb_mixer_interface
*mixer
= elem
->head
.mixer
;
1064 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1065 int index
= elem
->control
;
1066 int oval
, val
, err
= 0;
1068 mutex_lock(&private->data_mutex
);
1070 oval
= private->vol
[index
];
1071 val
= ucontrol
->value
.integer
.value
[0];
1076 private->vol
[index
] = val
;
1077 err
= scarlett2_usb_set_config(mixer
, SCARLETT2_CONFIG_LINE_OUT_VOLUME
,
1078 index
, val
- SCARLETT2_VOLUME_BIAS
);
1083 mutex_unlock(&private->data_mutex
);
1087 static const DECLARE_TLV_DB_MINMAX(
1088 db_scale_scarlett2_gain
, -SCARLETT2_VOLUME_BIAS
* 100, 0
1091 static const struct snd_kcontrol_new scarlett2_master_volume_ctl
= {
1092 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1093 .access
= SNDRV_CTL_ELEM_ACCESS_READ
|
1094 SNDRV_CTL_ELEM_ACCESS_TLV_READ
,
1096 .info
= scarlett2_volume_ctl_info
,
1097 .get
= scarlett2_master_volume_ctl_get
,
1098 .private_value
= 0, /* max value */
1099 .tlv
= { .p
= db_scale_scarlett2_gain
}
1102 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl
= {
1103 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1104 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
|
1105 SNDRV_CTL_ELEM_ACCESS_TLV_READ
,
1107 .info
= scarlett2_volume_ctl_info
,
1108 .get
= scarlett2_volume_ctl_get
,
1109 .put
= scarlett2_volume_ctl_put
,
1110 .private_value
= 0, /* max value */
1111 .tlv
= { .p
= db_scale_scarlett2_gain
}
1114 /*** HW/SW Volume Switch Controls ***/
1116 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol
*kctl
,
1117 struct snd_ctl_elem_info
*uinfo
)
1119 static const char *const values
[2] = {
1123 return snd_ctl_enum_info(uinfo
, 1, 2, values
);
1126 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol
*kctl
,
1127 struct snd_ctl_elem_value
*ucontrol
)
1129 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1130 struct scarlett2_mixer_data
*private = elem
->head
.mixer
->private_data
;
1132 ucontrol
->value
.enumerated
.item
[0] =
1133 private->vol_sw_hw_switch
[elem
->control
];
1137 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol
*kctl
,
1138 struct snd_ctl_elem_value
*ucontrol
)
1140 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1141 struct usb_mixer_interface
*mixer
= elem
->head
.mixer
;
1142 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1144 int index
= elem
->control
;
1145 int oval
, val
, err
= 0;
1147 mutex_lock(&private->data_mutex
);
1149 oval
= private->vol_sw_hw_switch
[index
];
1150 val
= !!ucontrol
->value
.integer
.value
[0];
1155 private->vol_sw_hw_switch
[index
] = val
;
1157 /* Change access mode to RO (hardware controlled volume)
1158 * or RW (software controlled volume)
1161 private->vol_ctls
[index
]->vd
[0].access
&=
1162 ~SNDRV_CTL_ELEM_ACCESS_WRITE
;
1164 private->vol_ctls
[index
]->vd
[0].access
|=
1165 SNDRV_CTL_ELEM_ACCESS_WRITE
;
1167 /* Reset volume to master volume */
1168 private->vol
[index
] = private->master_vol
;
1170 /* Set SW volume to current HW volume */
1171 err
= scarlett2_usb_set_config(
1172 mixer
, SCARLETT2_CONFIG_LINE_OUT_VOLUME
,
1173 index
, private->master_vol
- SCARLETT2_VOLUME_BIAS
);
1177 /* Notify of RO/RW change */
1178 snd_ctl_notify(mixer
->chip
->card
, SNDRV_CTL_EVENT_MASK_INFO
,
1179 &private->vol_ctls
[index
]->id
);
1181 /* Send SW/HW switch change to the device */
1182 err
= scarlett2_usb_set_config(mixer
, SCARLETT2_CONFIG_SW_HW_SWITCH
,
1186 mutex_unlock(&private->data_mutex
);
1190 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl
= {
1191 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1193 .info
= scarlett2_sw_hw_enum_ctl_info
,
1194 .get
= scarlett2_sw_hw_enum_ctl_get
,
1195 .put
= scarlett2_sw_hw_enum_ctl_put
,
1198 /*** Line Level/Instrument Level Switch Controls ***/
1200 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol
*kctl
,
1201 struct snd_ctl_elem_info
*uinfo
)
1203 static const char *const values
[2] = {
1207 return snd_ctl_enum_info(uinfo
, 1, 2, values
);
1210 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol
*kctl
,
1211 struct snd_ctl_elem_value
*ucontrol
)
1213 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1214 struct scarlett2_mixer_data
*private = elem
->head
.mixer
->private_data
;
1216 ucontrol
->value
.enumerated
.item
[0] =
1217 private->level_switch
[elem
->control
];
1221 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol
*kctl
,
1222 struct snd_ctl_elem_value
*ucontrol
)
1224 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1225 struct usb_mixer_interface
*mixer
= elem
->head
.mixer
;
1226 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1228 int index
= elem
->control
;
1229 int oval
, val
, err
= 0;
1231 mutex_lock(&private->data_mutex
);
1233 oval
= private->level_switch
[index
];
1234 val
= !!ucontrol
->value
.integer
.value
[0];
1239 private->level_switch
[index
] = val
;
1241 /* Send switch change to the device */
1242 err
= scarlett2_usb_set_config(mixer
, SCARLETT2_CONFIG_LEVEL_SWITCH
,
1246 mutex_unlock(&private->data_mutex
);
1250 static const struct snd_kcontrol_new scarlett2_level_enum_ctl
= {
1251 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1253 .info
= scarlett2_level_enum_ctl_info
,
1254 .get
= scarlett2_level_enum_ctl_get
,
1255 .put
= scarlett2_level_enum_ctl_put
,
1258 /*** Pad Switch Controls ***/
1260 static int scarlett2_pad_ctl_get(struct snd_kcontrol
*kctl
,
1261 struct snd_ctl_elem_value
*ucontrol
)
1263 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1264 struct scarlett2_mixer_data
*private = elem
->head
.mixer
->private_data
;
1266 ucontrol
->value
.enumerated
.item
[0] =
1267 private->pad_switch
[elem
->control
];
1271 static int scarlett2_pad_ctl_put(struct snd_kcontrol
*kctl
,
1272 struct snd_ctl_elem_value
*ucontrol
)
1274 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1275 struct usb_mixer_interface
*mixer
= elem
->head
.mixer
;
1276 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1278 int index
= elem
->control
;
1279 int oval
, val
, err
= 0;
1281 mutex_lock(&private->data_mutex
);
1283 oval
= private->pad_switch
[index
];
1284 val
= !!ucontrol
->value
.integer
.value
[0];
1289 private->pad_switch
[index
] = val
;
1291 /* Send switch change to the device */
1292 err
= scarlett2_usb_set_config(mixer
, SCARLETT2_CONFIG_PAD_SWITCH
,
1296 mutex_unlock(&private->data_mutex
);
1300 static const struct snd_kcontrol_new scarlett2_pad_ctl
= {
1301 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1303 .info
= snd_ctl_boolean_mono_info
,
1304 .get
= scarlett2_pad_ctl_get
,
1305 .put
= scarlett2_pad_ctl_put
,
1308 /*** Mute/Dim Controls ***/
1310 static int scarlett2_button_ctl_get(struct snd_kcontrol
*kctl
,
1311 struct snd_ctl_elem_value
*ucontrol
)
1313 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1314 struct usb_mixer_interface
*mixer
= elem
->head
.mixer
;
1315 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1317 if (private->vol_updated
) {
1318 mutex_lock(&private->data_mutex
);
1319 scarlett2_update_volumes(mixer
);
1320 mutex_unlock(&private->data_mutex
);
1323 ucontrol
->value
.enumerated
.item
[0] = private->buttons
[elem
->control
];
1327 static int scarlett2_button_ctl_put(struct snd_kcontrol
*kctl
,
1328 struct snd_ctl_elem_value
*ucontrol
)
1330 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1331 struct usb_mixer_interface
*mixer
= elem
->head
.mixer
;
1332 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1334 int index
= elem
->control
;
1335 int oval
, val
, err
= 0;
1337 mutex_lock(&private->data_mutex
);
1339 oval
= private->buttons
[index
];
1340 val
= !!ucontrol
->value
.integer
.value
[0];
1345 private->buttons
[index
] = val
;
1347 /* Send switch change to the device */
1348 err
= scarlett2_usb_set_config(mixer
, SCARLETT2_CONFIG_BUTTONS
,
1352 mutex_unlock(&private->data_mutex
);
1356 static const struct snd_kcontrol_new scarlett2_button_ctl
= {
1357 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1359 .info
= snd_ctl_boolean_mono_info
,
1360 .get
= scarlett2_button_ctl_get
,
1361 .put
= scarlett2_button_ctl_put
1364 /*** Create the analogue output controls ***/
1366 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface
*mixer
)
1368 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1369 const struct scarlett2_device_info
*info
= private->info
;
1370 const struct scarlett2_ports
*ports
= info
->ports
;
1372 ports
[SCARLETT2_PORT_TYPE_ANALOGUE
].num
[SCARLETT2_PORT_OUT
];
1374 char s
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
1376 /* Add R/O HW volume control */
1377 if (info
->line_out_hw_vol
) {
1378 snprintf(s
, sizeof(s
), "Master HW Playback Volume");
1379 err
= scarlett2_add_new_ctl(mixer
,
1380 &scarlett2_master_volume_ctl
,
1381 0, 1, s
, &private->master_vol_ctl
);
1386 /* Add volume controls */
1387 for (i
= 0; i
< num_line_out
; i
++) {
1390 if (info
->line_out_descrs
[i
])
1391 snprintf(s
, sizeof(s
),
1392 "Line %02d (%s) Playback Volume",
1393 i
+ 1, info
->line_out_descrs
[i
]);
1395 snprintf(s
, sizeof(s
),
1396 "Line %02d Playback Volume",
1398 err
= scarlett2_add_new_ctl(mixer
,
1399 &scarlett2_line_out_volume_ctl
,
1400 i
, 1, s
, &private->vol_ctls
[i
]);
1404 /* Make the fader read-only if the SW/HW switch is set to HW */
1405 if (private->vol_sw_hw_switch
[i
])
1406 private->vol_ctls
[i
]->vd
[0].access
&=
1407 ~SNDRV_CTL_ELEM_ACCESS_WRITE
;
1410 if (info
->line_out_hw_vol
) {
1411 snprintf(s
, sizeof(s
),
1412 "Line Out %02d Volume Control Playback Enum",
1414 err
= scarlett2_add_new_ctl(mixer
,
1415 &scarlett2_sw_hw_enum_ctl
,
1422 /* Add HW button controls */
1423 for (i
= 0; i
< private->info
->button_count
; i
++) {
1424 err
= scarlett2_add_new_ctl(mixer
, &scarlett2_button_ctl
,
1425 i
, 1, scarlett2_button_names
[i
],
1426 &private->button_ctls
[i
]);
1434 /*** Create the analogue input controls ***/
1436 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface
*mixer
)
1438 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1439 const struct scarlett2_device_info
*info
= private->info
;
1441 char s
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
1443 /* Add input level (line/inst) controls */
1444 for (i
= 0; i
< info
->level_input_count
; i
++) {
1445 snprintf(s
, sizeof(s
), "Line In %d Level Capture Enum", i
+ 1);
1446 err
= scarlett2_add_new_ctl(mixer
, &scarlett2_level_enum_ctl
,
1452 /* Add input pad controls */
1453 for (i
= 0; i
< info
->pad_input_count
; i
++) {
1454 snprintf(s
, sizeof(s
), "Line In %d Pad Capture Switch", i
+ 1);
1455 err
= scarlett2_add_new_ctl(mixer
, &scarlett2_pad_ctl
,
1464 /*** Mixer Volume Controls ***/
1466 static int scarlett2_mixer_ctl_info(struct snd_kcontrol
*kctl
,
1467 struct snd_ctl_elem_info
*uinfo
)
1469 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1471 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1472 uinfo
->count
= elem
->channels
;
1473 uinfo
->value
.integer
.min
= 0;
1474 uinfo
->value
.integer
.max
= SCARLETT2_MIXER_MAX_VALUE
;
1475 uinfo
->value
.integer
.step
= 1;
1479 static int scarlett2_mixer_ctl_get(struct snd_kcontrol
*kctl
,
1480 struct snd_ctl_elem_value
*ucontrol
)
1482 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1483 struct scarlett2_mixer_data
*private = elem
->head
.mixer
->private_data
;
1485 ucontrol
->value
.integer
.value
[0] = private->mix
[elem
->control
];
1489 static int scarlett2_mixer_ctl_put(struct snd_kcontrol
*kctl
,
1490 struct snd_ctl_elem_value
*ucontrol
)
1492 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1493 struct usb_mixer_interface
*mixer
= elem
->head
.mixer
;
1494 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1495 const struct scarlett2_device_info
*info
= private->info
;
1496 const struct scarlett2_ports
*ports
= info
->ports
;
1497 int oval
, val
, num_mixer_in
, mix_num
, err
= 0;
1499 mutex_lock(&private->data_mutex
);
1501 oval
= private->mix
[elem
->control
];
1502 val
= ucontrol
->value
.integer
.value
[0];
1503 num_mixer_in
= ports
[SCARLETT2_PORT_TYPE_MIX
].num
[SCARLETT2_PORT_OUT
];
1504 mix_num
= elem
->control
/ num_mixer_in
;
1509 private->mix
[elem
->control
] = val
;
1510 err
= scarlett2_usb_set_mix(mixer
, mix_num
);
1515 mutex_unlock(&private->data_mutex
);
1519 static const DECLARE_TLV_DB_MINMAX(
1520 db_scale_scarlett2_mixer
,
1521 SCARLETT2_MIXER_MIN_DB
* 100,
1522 SCARLETT2_MIXER_MAX_DB
* 100
1525 static const struct snd_kcontrol_new scarlett2_mixer_ctl
= {
1526 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1527 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
|
1528 SNDRV_CTL_ELEM_ACCESS_TLV_READ
,
1530 .info
= scarlett2_mixer_ctl_info
,
1531 .get
= scarlett2_mixer_ctl_get
,
1532 .put
= scarlett2_mixer_ctl_put
,
1533 .private_value
= SCARLETT2_MIXER_MAX_DB
, /* max value */
1534 .tlv
= { .p
= db_scale_scarlett2_mixer
}
1537 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface
*mixer
)
1539 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1540 const struct scarlett2_ports
*ports
= private->info
->ports
;
1543 char s
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
1545 int num_inputs
= ports
[SCARLETT2_PORT_TYPE_MIX
].num
[SCARLETT2_PORT_OUT
];
1546 int num_outputs
= ports
[SCARLETT2_PORT_TYPE_MIX
].num
[SCARLETT2_PORT_IN
];
1548 for (i
= 0, index
= 0; i
< num_outputs
; i
++) {
1549 for (j
= 0; j
< num_inputs
; j
++, index
++) {
1550 snprintf(s
, sizeof(s
),
1551 "Mix %c Input %02d Playback Volume",
1553 err
= scarlett2_add_new_ctl(mixer
, &scarlett2_mixer_ctl
,
1563 /*** Mux Source Selection Controls ***/
1565 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol
*kctl
,
1566 struct snd_ctl_elem_info
*uinfo
)
1568 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1569 struct scarlett2_mixer_data
*private = elem
->head
.mixer
->private_data
;
1570 const struct scarlett2_ports
*ports
= private->info
->ports
;
1571 unsigned int item
= uinfo
->value
.enumerated
.item
;
1572 int items
= private->num_mux_srcs
;
1575 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1576 uinfo
->count
= elem
->channels
;
1577 uinfo
->value
.enumerated
.items
= items
;
1580 item
= uinfo
->value
.enumerated
.item
= items
- 1;
1583 port_type
< SCARLETT2_PORT_TYPE_COUNT
;
1585 if (item
< ports
[port_type
].num
[SCARLETT2_PORT_IN
]) {
1586 sprintf(uinfo
->value
.enumerated
.name
,
1587 ports
[port_type
].src_descr
,
1588 item
+ ports
[port_type
].src_num_offset
);
1591 item
-= ports
[port_type
].num
[SCARLETT2_PORT_IN
];
1597 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol
*kctl
,
1598 struct snd_ctl_elem_value
*ucontrol
)
1600 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1601 struct scarlett2_mixer_data
*private = elem
->head
.mixer
->private_data
;
1603 ucontrol
->value
.enumerated
.item
[0] = private->mux
[elem
->control
];
1607 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol
*kctl
,
1608 struct snd_ctl_elem_value
*ucontrol
)
1610 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1611 struct usb_mixer_interface
*mixer
= elem
->head
.mixer
;
1612 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1613 int index
= elem
->control
;
1614 int oval
, val
, err
= 0;
1616 mutex_lock(&private->data_mutex
);
1618 oval
= private->mux
[index
];
1619 val
= clamp(ucontrol
->value
.integer
.value
[0],
1620 0L, private->num_mux_srcs
- 1L);
1625 private->mux
[index
] = val
;
1626 err
= scarlett2_usb_set_mux(mixer
);
1631 mutex_unlock(&private->data_mutex
);
1635 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl
= {
1636 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1638 .info
= scarlett2_mux_src_enum_ctl_info
,
1639 .get
= scarlett2_mux_src_enum_ctl_get
,
1640 .put
= scarlett2_mux_src_enum_ctl_put
,
1643 static int scarlett2_add_mux_enums(struct usb_mixer_interface
*mixer
)
1645 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1646 const struct scarlett2_ports
*ports
= private->info
->ports
;
1647 int port_type
, channel
, i
;
1649 for (i
= 0, port_type
= 0;
1650 port_type
< SCARLETT2_PORT_TYPE_COUNT
;
1653 channel
< ports
[port_type
].num
[SCARLETT2_PORT_OUT
];
1656 char s
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
1657 const char *const descr
= ports
[port_type
].dst_descr
;
1659 snprintf(s
, sizeof(s
) - 5, descr
, channel
+ 1);
1662 err
= scarlett2_add_new_ctl(mixer
,
1663 &scarlett2_mux_src_enum_ctl
,
1673 /*** Meter Controls ***/
1675 static int scarlett2_meter_ctl_info(struct snd_kcontrol
*kctl
,
1676 struct snd_ctl_elem_info
*uinfo
)
1678 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1680 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1681 uinfo
->count
= elem
->channels
;
1682 uinfo
->value
.integer
.min
= 0;
1683 uinfo
->value
.integer
.max
= 4095;
1684 uinfo
->value
.integer
.step
= 1;
1688 static int scarlett2_meter_ctl_get(struct snd_kcontrol
*kctl
,
1689 struct snd_ctl_elem_value
*ucontrol
)
1691 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
1692 u16 meter_levels
[SCARLETT2_NUM_METERS
];
1695 err
= scarlett2_usb_get_meter_levels(elem
->head
.mixer
, meter_levels
);
1699 for (i
= 0; i
< elem
->channels
; i
++)
1700 ucontrol
->value
.integer
.value
[i
] = meter_levels
[i
];
1705 static const struct snd_kcontrol_new scarlett2_meter_ctl
= {
1706 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
1707 .access
= SNDRV_CTL_ELEM_ACCESS_READ
| SNDRV_CTL_ELEM_ACCESS_VOLATILE
,
1709 .info
= scarlett2_meter_ctl_info
,
1710 .get
= scarlett2_meter_ctl_get
1713 static int scarlett2_add_meter_ctl(struct usb_mixer_interface
*mixer
)
1715 return scarlett2_add_new_ctl(mixer
, &scarlett2_meter_ctl
,
1716 0, SCARLETT2_NUM_METERS
,
1717 "Level Meter", NULL
);
1720 /*** Cleanup/Suspend Callbacks ***/
1722 static void scarlett2_private_free(struct usb_mixer_interface
*mixer
)
1724 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1726 cancel_delayed_work_sync(&private->work
);
1728 mixer
->private_data
= NULL
;
1731 static void scarlett2_private_suspend(struct usb_mixer_interface
*mixer
)
1733 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1735 if (cancel_delayed_work_sync(&private->work
))
1736 scarlett2_config_save(private->mixer
);
1739 /*** Initialisation ***/
1741 static int scarlett2_count_mux_srcs(const struct scarlett2_ports
*ports
)
1743 int port_type
, count
= 0;
1746 port_type
< SCARLETT2_PORT_TYPE_COUNT
;
1748 count
+= ports
[port_type
].num
[SCARLETT2_PORT_IN
];
1753 /* Default routing connects PCM outputs and inputs to Analogue,
1756 static void scarlett2_init_routing(u8
*mux
,
1757 const struct scarlett2_ports
*ports
)
1759 int i
, input_num
, input_count
, port_type
;
1760 int output_num
, output_count
, port_type_connect_num
;
1762 static const int connect_order
[] = {
1763 SCARLETT2_PORT_TYPE_ANALOGUE
,
1764 SCARLETT2_PORT_TYPE_SPDIF
,
1765 SCARLETT2_PORT_TYPE_ADAT
,
1769 /* Assign PCM inputs (routing outputs) */
1770 output_num
= scarlett2_get_port_start_num(ports
,
1772 SCARLETT2_PORT_TYPE_PCM
);
1773 output_count
= ports
[SCARLETT2_PORT_TYPE_PCM
].num
[SCARLETT2_PORT_OUT
];
1775 for (port_type
= connect_order
[port_type_connect_num
= 0];
1777 port_type
= connect_order
[++port_type_connect_num
]) {
1778 input_num
= scarlett2_get_port_start_num(
1779 ports
, SCARLETT2_PORT_IN
, port_type
);
1780 input_count
= ports
[port_type
].num
[SCARLETT2_PORT_IN
];
1782 i
< input_count
&& output_count
;
1783 i
++, output_count
--)
1784 mux
[output_num
++] = input_num
++;
1787 /* Assign PCM outputs (routing inputs) */
1788 input_num
= scarlett2_get_port_start_num(ports
,
1790 SCARLETT2_PORT_TYPE_PCM
);
1791 input_count
= ports
[SCARLETT2_PORT_TYPE_PCM
].num
[SCARLETT2_PORT_IN
];
1793 for (port_type
= connect_order
[port_type_connect_num
= 0];
1795 port_type
= connect_order
[++port_type_connect_num
]) {
1796 output_num
= scarlett2_get_port_start_num(
1797 ports
, SCARLETT2_PORT_OUT
, port_type
);
1798 output_count
= ports
[port_type
].num
[SCARLETT2_PORT_OUT
];
1800 i
< output_count
&& input_count
;
1802 mux
[output_num
++] = input_num
++;
1806 /* Initialise private data, routing, sequence number */
1807 static int scarlett2_init_private(struct usb_mixer_interface
*mixer
,
1808 const struct scarlett2_device_info
*info
)
1810 struct scarlett2_mixer_data
*private =
1811 kzalloc(sizeof(struct scarlett2_mixer_data
), GFP_KERNEL
);
1816 mutex_init(&private->usb_mutex
);
1817 mutex_init(&private->data_mutex
);
1818 INIT_DELAYED_WORK(&private->work
, scarlett2_config_save_work
);
1819 private->info
= info
;
1820 private->num_mux_srcs
= scarlett2_count_mux_srcs(info
->ports
);
1821 private->scarlett2_seq
= 0;
1822 private->mixer
= mixer
;
1823 mixer
->private_data
= private;
1824 mixer
->private_free
= scarlett2_private_free
;
1825 mixer
->private_suspend
= scarlett2_private_suspend
;
1827 /* Setup default routing */
1828 scarlett2_init_routing(private->mux
, info
->ports
);
1830 /* Initialise the sequence number used for the proprietary commands */
1831 return scarlett2_usb(mixer
, SCARLETT2_USB_INIT_SEQ
, NULL
, 0, NULL
, 0);
1834 /* Read line-in config and line-out volume settings on start */
1835 static int scarlett2_read_configs(struct usb_mixer_interface
*mixer
)
1837 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1838 const struct scarlett2_device_info
*info
= private->info
;
1839 const struct scarlett2_ports
*ports
= info
->ports
;
1841 ports
[SCARLETT2_PORT_TYPE_ANALOGUE
].num
[SCARLETT2_PORT_OUT
];
1842 u8 level_switches
[SCARLETT2_LEVEL_SWITCH_MAX
];
1843 u8 pad_switches
[SCARLETT2_PAD_SWITCH_MAX
];
1844 struct scarlett2_usb_volume_status volume_status
;
1847 if (info
->level_input_count
) {
1848 err
= scarlett2_usb_get_config(
1850 SCARLETT2_CONFIG_LEVEL_SWITCH
,
1851 info
->level_input_count
,
1855 for (i
= 0; i
< info
->level_input_count
; i
++)
1856 private->level_switch
[i
] = level_switches
[i
];
1859 if (info
->pad_input_count
) {
1860 err
= scarlett2_usb_get_config(
1862 SCARLETT2_CONFIG_PAD_SWITCH
,
1863 info
->pad_input_count
,
1867 for (i
= 0; i
< info
->pad_input_count
; i
++)
1868 private->pad_switch
[i
] = pad_switches
[i
];
1871 err
= scarlett2_usb_get_volume_status(mixer
, &volume_status
);
1875 private->master_vol
= clamp(
1876 volume_status
.master_vol
+ SCARLETT2_VOLUME_BIAS
,
1877 0, SCARLETT2_VOLUME_BIAS
);
1879 for (i
= 0; i
< num_line_out
; i
++) {
1882 private->vol_sw_hw_switch
[i
] =
1883 info
->line_out_hw_vol
1884 && volume_status
.sw_hw_switch
[i
];
1886 volume
= private->vol_sw_hw_switch
[i
]
1887 ? volume_status
.master_vol
1888 : volume_status
.sw_vol
[i
];
1889 volume
= clamp(volume
+ SCARLETT2_VOLUME_BIAS
,
1890 0, SCARLETT2_VOLUME_BIAS
);
1891 private->vol
[i
] = volume
;
1894 for (i
= 0; i
< info
->button_count
; i
++)
1895 private->buttons
[i
] = !!volume_status
.buttons
[i
];
1900 /* Notify on volume change */
1901 static void scarlett2_mixer_interrupt_vol_change(
1902 struct usb_mixer_interface
*mixer
)
1904 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1905 const struct scarlett2_ports
*ports
= private->info
->ports
;
1907 ports
[SCARLETT2_PORT_TYPE_ANALOGUE
].num
[SCARLETT2_PORT_OUT
];
1910 private->vol_updated
= 1;
1912 snd_ctl_notify(mixer
->chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
1913 &private->master_vol_ctl
->id
);
1915 for (i
= 0; i
< num_line_out
; i
++) {
1916 if (!private->vol_sw_hw_switch
[i
])
1918 snd_ctl_notify(mixer
->chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
1919 &private->vol_ctls
[i
]->id
);
1923 /* Notify on button change */
1924 static void scarlett2_mixer_interrupt_button_change(
1925 struct usb_mixer_interface
*mixer
)
1927 struct scarlett2_mixer_data
*private = mixer
->private_data
;
1930 private->vol_updated
= 1;
1932 for (i
= 0; i
< private->info
->button_count
; i
++)
1933 snd_ctl_notify(mixer
->chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
1934 &private->button_ctls
[i
]->id
);
1937 /* Interrupt callback */
1938 static void scarlett2_mixer_interrupt(struct urb
*urb
)
1940 struct usb_mixer_interface
*mixer
= urb
->context
;
1941 int len
= urb
->actual_length
;
1942 int ustatus
= urb
->status
;
1949 data
= le32_to_cpu(*(__le32
*)urb
->transfer_buffer
);
1950 if (data
& SCARLETT2_USB_INTERRUPT_VOL_CHANGE
)
1951 scarlett2_mixer_interrupt_vol_change(mixer
);
1952 if (data
& SCARLETT2_USB_INTERRUPT_BUTTON_CHANGE
)
1953 scarlett2_mixer_interrupt_button_change(mixer
);
1955 usb_audio_err(mixer
->chip
,
1956 "scarlett mixer interrupt length %d\n", len
);
1960 if (ustatus
!= -ENOENT
&&
1961 ustatus
!= -ECONNRESET
&&
1962 ustatus
!= -ESHUTDOWN
) {
1963 urb
->dev
= mixer
->chip
->dev
;
1964 usb_submit_urb(urb
, GFP_ATOMIC
);
1968 static int scarlett2_mixer_status_create(struct usb_mixer_interface
*mixer
)
1970 struct usb_device
*dev
= mixer
->chip
->dev
;
1971 unsigned int pipe
= usb_rcvintpipe(dev
,
1972 SCARLETT2_USB_INTERRUPT_ENDPOINT
);
1973 void *transfer_buffer
;
1976 usb_audio_err(mixer
->chip
,
1977 "%s: mixer urb already in use!\n", __func__
);
1981 if (usb_pipe_type_check(dev
, pipe
))
1984 mixer
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
1988 transfer_buffer
= kmalloc(SCARLETT2_USB_INTERRUPT_MAX_DATA
, GFP_KERNEL
);
1989 if (!transfer_buffer
)
1992 usb_fill_int_urb(mixer
->urb
, dev
, pipe
,
1993 transfer_buffer
, SCARLETT2_USB_INTERRUPT_MAX_DATA
,
1994 scarlett2_mixer_interrupt
, mixer
,
1995 SCARLETT2_USB_INTERRUPT_INTERVAL
);
1997 return usb_submit_urb(mixer
->urb
, GFP_KERNEL
);
2001 int snd_scarlett_gen2_controls_create(struct usb_mixer_interface
*mixer
)
2003 const struct scarlett2_device_info
*info
;
2006 /* only use UAC_VERSION_2 */
2007 if (!mixer
->protocol
)
2010 switch (mixer
->chip
->usb_id
) {
2011 case USB_ID(0x1235, 0x8203):
2012 info
= &s6i6_gen2_info
;
2014 case USB_ID(0x1235, 0x8204):
2015 info
= &s18i8_gen2_info
;
2017 case USB_ID(0x1235, 0x8201):
2018 info
= &s18i20_gen2_info
;
2020 default: /* device not (yet) supported */
2024 if (!(mixer
->chip
->setup
& SCARLETT2_ENABLE
)) {
2025 usb_audio_err(mixer
->chip
,
2026 "Focusrite Scarlett Gen 2 Mixer Driver disabled; "
2027 "use options snd_usb_audio device_setup=1 "
2028 "to enable and report any issues to g@b4.vu");
2032 /* Initialise private data, routing, sequence number */
2033 err
= scarlett2_init_private(mixer
, info
);
2037 /* Read volume levels and controls from the interface */
2038 err
= scarlett2_read_configs(mixer
);
2042 /* Create the analogue output controls */
2043 err
= scarlett2_add_line_out_ctls(mixer
);
2047 /* Create the analogue input controls */
2048 err
= scarlett2_add_line_in_ctls(mixer
);
2052 /* Create the input, output, and mixer mux input selections */
2053 err
= scarlett2_add_mux_enums(mixer
);
2057 /* Create the matrix mixer controls */
2058 err
= scarlett2_add_mixer_ctls(mixer
);
2062 /* Create the level meter controls */
2063 err
= scarlett2_add_meter_ctl(mixer
);
2067 /* Set up the interrupt polling if there are hardware buttons */
2068 if (info
->button_count
) {
2069 err
= scarlett2_mixer_status_create(mixer
);