Skip to contentSkip to conversation

ALSA: usb-audio: Scarlett mixer interface for 18i20 Gen 2

RFC PATCH Superseded Geoffrey D. Bennett 2 participants 3 messages Apr 23, 2019 – Apr 24, 2019 Sound

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 ALSA: usb-audio: Scarlett mixer interface for 18i20 Gen 2

Add mixer quirk for the Focusrite Scarlett 18i20 Gen 2 audio interface. Although the interface is USB compliant, additional hardware mixing, routing, and metering functionality is available using proprietary USB requests.

---
(ping resend (no changes) as no feedback received for 3 weeks)

Hi all,

This patch adds the following controls for the Scarlett 18i20 Gen 2:
- Master volume knob indicator
- Volume controls for the 10 analogue HW outputs
- HW/SW volume switches for the 10 analogue HW outputs
- Output mux (where the sound for the HW outputs comes from; defaults to PCM outputs 1-20)
- Capture mux (where the sound for PCM recording comes from; defaults to HW inputs 1-18)
- Matrix mux (where the sound going into the mixer comes from; 18 inputs default off)
- Mixer matrix (18 inputs * 10 outputs = 180 controls)
- Mute and dim indicators
- Level meters

This is my first attempt at writing any kernel device driver code, so apologies in advance if I missed something. There's a couple of things I'm particularly unsure about:

- I added a private field to struct snd_usb_audio for storing the private mixer data. This seems wrong, but I didn't know where else I could/should put that data.

- I've #define'd the vendor-specific interface, endpoint, max_data, and interval values. Is it okay to leave these hard-coded (they match the values provided by lsusb -v; I presume they never change?), or should they be looked up at runtime instead? (and if at runtime, how?)

- I'm not sure if those snd_printk()s for unexpected responses from the hardware should be left in there? Removed? Reworded?

Comments and feedback would be very welcome.

Regards,
Geoffrey.

sound/usb/Makefile +1 -0
index d330f74c90e6..8cf7081e17cb 100644
@@ -11,6 +11,7 @@ snd-usb-audio-objs := 	card.o \
 			mixer.o \
 			mixer_quirks.o \
 			mixer_scarlett.o \
+			mixer_scarlett_gen2.o \
 			mixer_us16x08.o \
 			pcm.o \
 			power.o \
sound/usb/card.c +2 -0
index 746a72e23cf9..06bd839e9ab2 100644
@@ -354,6 +354,8 @@ static void snd_usb_audio_free(struct snd_card *card)
 	list_for_each_entry_safe(ep, n, &chip->ep_list, list)
 		snd_usb_endpoint_free(ep);
 
+	kfree(chip->private);
+
 	mutex_destroy(&chip->mutex);
 	if (!atomic_read(&chip->shutdown))
 		dev_set_drvdata(&chip->dev->dev, NULL);
sound/usb/mixer_quirks.c +5 -0
index 85ae0ff2382a..331f0596e42d 100644
@@ -45,6 +45,7 @@
 #include "mixer.h"
 #include "mixer_quirks.h"
 #include "mixer_scarlett.h"
+#include "mixer_scarlett_gen2.h"
 #include "mixer_us16x08.h"
 #include "helper.h"
 
@@ -2273,6 +2274,10 @@ int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
 		err = snd_scarlett_controls_create(mixer);
 		break;
 
+	case USB_ID(0x1235, 0x8201): /* Focusrite Scarlett 18i20 2nd Gen */
+		err = snd_scarlett_gen2_controls_create(mixer);
+		break;
+
 	case USB_ID(0x041e, 0x323b): /* Creative Sound Blaster E1 */
 		err = snd_soundblaster_e1_switch_create(mixer);
 		break;
sound/usb/mixer_scarlett_gen2.c new file +1592 -0

1,592 lines not on this page · raw message

sound/usb/mixer_scarlett_gen2.h new file +7 -0
new file mode 100644
index 000000000000..52e1dad77afd
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __USB_MIXER_SCARLETT_GEN2_H
+#define __USB_MIXER_SCARLETT_GEN2_H
+
+int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer);
+
+#endif /* __USB_MIXER_SCARLETT_GEN2_H */
sound/usb/usbaudio.h +2 -0
index b9faeca645fd..1357a73c3a20 100644
@@ -66,6 +66,8 @@ struct snd_usb_audio {
 					 */
 
 	struct usb_host_interface *ctrl_intf;	/* the audio control interface */
+
+	void *private;
 };
 
 #define usb_audio_err(chip, fmt, args...) \

_______________________________________________
Alsa-devel mailing list
[email protected]
https://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#1 lore raw
Quoted 9 lines · Geoffrey D. Bennett

Add mixer quirk for the Focusrite Scarlett 18i20 Gen 2 audio interface. Although the interface is USB compliant, additional hardware mixing, routing, and metering functionality is available using proprietary USB requests.

---
(ping resend (no changes) as no feedback received for 3 weeks)

Oh sorry, I must have overlooked the post.

Quoted 17 lines

Hi all,

This patch adds the following controls for the Scarlett 18i20 Gen 2:
- Master volume knob indicator
- Volume controls for the 10 analogue HW outputs
- HW/SW volume switches for the 10 analogue HW outputs
- Output mux (where the sound for the HW outputs comes from; defaults to PCM outputs 1-20)
- Capture mux (where the sound for PCM recording comes from; defaults to HW inputs 1-18)
- Matrix mux (where the sound going into the mixer comes from; 18 inputs default off)
- Mixer matrix (18 inputs * 10 outputs = 180 controls)
- Mute and dim indicators
- Level meters

Quoted 7 lines

This is my first attempt at writing any kernel device driver code, so apologies in advance if I missed something. There's a couple of things I'm particularly unsure about:

- I added a private field to struct snd_usb_audio for storing the private mixer data. This seems wrong, but I didn't know where else I could/should put that data.

Hm, this is a thing I'd like to avoid as much as possible. Currently, each usb_mixer_elem_info may have a private pointer, and the original scarlett quirk uses it for storing its own "type" information. This could be used for storing the data instead?

Quoted 5 lines

- I've #define'd the vendor-specific interface, endpoint, max_data, and interval values. Is it okay to leave these hard-coded (they match the values provided by lsusb -v; I presume they never change?), or should they be looked up at runtime instead? (and if at runtime, how?)

In a case of USB-audio quirk entry, it's not too bad to hard-code the values in the entry table, indeed.

Quoted 2 lines

- I'm not sure if those snd_printk()s for unexpected responses from the hardware should be left in there? Removed? Reworded?

First off, avoid snd_printk() but use usb_audio_err() or dev_err() directly. The error should be fine to be there unless you can trigger the error too frequently from user-space intentionally.

Quoted 1 line

Comments and feedback would be very welcome.

Below are some more comments:

sound/usb/mixer_scarlett_gen2.c line 125

Quoted 4 lines
sound/usb/mixer_scarlett_gen2.c new file +4 -0
+/* map from (dB + 80) * 2 to mixer value
+ * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
+ */
+static const __u16 scarlett2_mixer_values[173] = {

The "__" prefix can be dropped here. In general, __u16 or friends are used for uapi definitions, and for the explicit type in the code, use u16, etc, instead.

Quoted 1 line

+struct __attribute__((__packed__)) scarlett2_usb_volume_status {

We have __packed definition for __attribute__((__packed__). And put it rather after the closing brace, e.g.

struct scarlett2_usb_volume_status {
	....
} __packed;
Quoted 6 lines
sound/usb/mixer_scarlett_gen2.c new file +6 -0
+/* Send a proprietary format request to the Scarlett interface. */
+static int scarlett2_usb(
+	struct usb_mixer_interface *mixer, __u32 cmd,
+	void *req_data, __u16 req_size, void *resp_data, __u16 resp_size)
+{
+	struct scarlett2_chip_data *private = mixer->chip->private;

sound/usb/mixer_scarlett_gen2.c line 368

Quoted 10 lines
sound/usb/mixer_scarlett_gen2.c new file +10 -0
+
+	/* proprietary request/response format */
+	struct scarlett2_usb_packet {
+		__u32 cmd;
+		__u16 size;
+		__u16 seq;
+		__u32 error;
+		__u32 pad;
+		__u8 data[];
+	};

Better to define outside the function.

Quoted 11 lines
sound/usb/mixer_scarlett_gen2.c new file +11 -0
+
+	__u16 req_buf_size =
+		sizeof(struct scarlett2_usb_packet) + req_size;
+	__u16 resp_buf_size =
+		sizeof(struct scarlett2_usb_packet) + resp_size;
+
+	struct scarlett2_usb_packet *req, *resp;
+
+	int seq, err;
+	__u32 resp_cmd;
+	__u32 resp_seq;

sound/usb/mixer_scarlett_gen2.c line 389

Quoted 10 lines
sound/usb/mixer_scarlett_gen2.c new file +10 -0
+	__u16 size;
+
+	mutex_lock(&private->usb_mutex);
+
+	/* build request message and send it */
+
+	req = kmalloc(req_buf_size, GFP_KERNEL);
+	if (!req) {
+		mutex_unlock(&private->usb_mutex);
+		return -ENOMEM;

The standard idiom is to move the whole mutex_unlock() calls into the end of the function and use goto here.

	if (!req) {
		err = -ENOMEM;
		goto unlock;
	}
	....
unlock:
	mutex_lock(&private->usb_mutex);
	return err;
Quoted 2 lines
sound/usb/mixer_scarlett_gen2.c new file +2 -0
+int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer)
+{

....

sound/usb/mixer_scarlett_gen2.c line 1565

Quoted 3 lines
sound/usb/mixer_scarlett_gen2.c new file +3 -0
+	/* Initialise private data, routing, sequence number */
+	if ((err = scarlett2_init_private(mixer, info)) < 0)
+		return err;

Try to run checkpatch.pl once and fix the errors reported there.

thanks,

Takashi

#2 lore raw
Quoted 8 lines · Takashi Iwai
Quoted 6 lines · Geoffrey D. Bennett

Add mixer quirk for the Focusrite Scarlett 18i20 Gen 2 audio interface. Although the interface is USB compliant, additional hardware mixing, routing, and metering functionality is available using proprietary USB requests.

Quoted 5 lines
Quoted 3 lines

---
(ping resend (no changes) as no feedback received for 3 weeks)

Oh sorry, I must have overlooked the post.

No problem, thank you for your feedback.

Quoted 8 lines
Quoted 3 lines

- I added a private field to struct snd_usb_audio for storing the private mixer data. This seems wrong, but I didn't know where else I could/should put that data.

Hm, this is a thing I'd like to avoid as much as possible. Currently, each usb_mixer_elem_info may have a private pointer, and the original scarlett quirk uses it for storing its own "type" information. This could be used for storing the data instead?

[...]

The original scarlett quirk private data is all related to each of the mixer elements that they are attached to, but the private data I am storing is unrelated to any specific mixer element (e.g. an incrementing sequence number that is included in every proprietary USB request). Do you think that I should just attach the data to the first mixer element?

I have started work on extending the driver to support the 18i8 2nd Gen interface as well, so I will incorporate all your feedback into that and resend once complete.

Thanks again,
Geoffrey.