Skip to contentSkip to conversation

ALSA: usb-audio: Add quirk for MOTU MicroBook II

PATCH In review Manuel Reinhardt 2 participants 3 messages Feb 27, 2019 – Feb 28, 2019 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: usb-audio: Add quirk for MOTU MicroBook II

Add an entry to the quirks-table to for usb-audio to recognize the Microbook II (although it only exposes vendor interfaces). A simple boot quirk is also implemented to set up the sample rate and make sure that no audio urbs are sent before the device is ready.

This patch only provides audio playback and capture at 96kHz sample rate. Notice the following shortcomings:

- The sample rate is currently hardcoded to 96k although the device also supports 48k and 44.1k.

- The various mixer controls of the MicroBook are not made available.

- The keep-iface control should be on by default because the device shuts down whenever the altsetting is reset which is usually unwanted. (I don't know the best way to do this)

- The communication format used by the MicroBook for sample rate setting and also other setup has been reverse engineered by looking at the usbmon output while running the windows driver through virtualbox. In this patch the first byte of every message is set to \0 while in the observed communications the first byte acts as a "message-counter" increasing its value with every message sent. Leaving it at \0 does not seem to affect the device.

---
 sound/usb/pcm.c          |  4 ++
 sound/usb/quirks-table.h | 65 +++++++++++++++++++++++++++
 sound/usb/quirks.c       | 97 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 166 insertions(+)
sound/usb/pcm.c +4 -0
index db114f3977e0..056af0a57b22 100644
@@ -354,6 +354,10 @@ static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
 		ep = 0x81;
 		ifnum = 1;
 		goto add_sync_ep_from_ifnum;
+	case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II */
+		ep = 0x84;
+		ifnum = 0;
+		goto add_sync_ep_from_ifnum;
 	}
 
 	if (attr == USB_ENDPOINT_SYNC_ASYNC &&
sound/usb/quirks-table.h +65 -0

70 lines not on this page · raw message

sound/usb/quirks.c +97 -0

109 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw
Quoted 9 lines · Manuel Reinhardt

Add an entry to the quirks-table to for usb-audio to recognize the Microbook II (although it only exposes vendor interfaces). A simple boot quirk is also implemented to set up the sample rate and make sure that no audio urbs are sent before the device is ready.

This patch only provides audio playback and capture at 96kHz sample rate. Notice the following shortcomings:

Quoted 8 lines

- The sample rate is currently hardcoded to 96k although the device also supports 48k and 44.1k.

- The various mixer controls of the MicroBook are not made available.

- The keep-iface control should be on by default because the device shuts down whenever the altsetting is reset which is usually unwanted. (I don't know the best way to do this)

Can be set either in the quirk callback you write. Or it'd be not bad to create a white list, too.

Quoted 8 lines
sound/usb/quirks.c +8 -0
+static int snd_usb_motu_microbookii_boot_quirk(struct usb_device *dev)
+{
+	int err, actual_length, poll_attempts = 0;
+	bool setup_finished = false;
+	static const u8 set_samplerate_seq[] = { 0x00, 0x00, 0x00, 0x00,
+						 0x00, 0x00, 0x0b, 0x14,
+						 0x00, 0x00, 0x00, 0x01 };
+	static const u8 poll_ready_seq[] = { 0x00, 0x04, 0x00, 0x00,

sound/usb/quirks.c static int snd_usb_axefx3_boot_quirk(struct usb_device *dev) · line 1050

Quoted 10 lines
sound/usb/quirks.c +10 -0
+					     0x00, 0x00, 0x0b, 0x18 };
+	u8 *buf = kzalloc(MICROBOOK_BUF_SIZE, GFP_KERNEL);
+
+	if (!buf)
+		return -ENOMEM;
+
+	dev_info(&dev->dev, "Waiting for MOTU Microbook II to boot up...\n");
+
+	/* First we tell the device which sample rate to use. */
+	memcpy(buf, set_samplerate_seq, ARRAY_SIZE(set_samplerate_seq));

This is about byte-size, so better to use sizeof(). ARRAY_SIZE() confuses readers as if he needs to care about the type.

sound/usb/quirks.c static int snd_usb_axefx3_boot_quirk(struct usb_device *dev) · line 1051

Quoted 1 line
sound/usb/quirks.c +1 -0
+	actual_length = ARRAY_SIZE(set_samplerate_seq);

Ditto here. Also some other codes below, too.

sound/usb/quirks.c static int snd_usb_axefx3_boot_quirk(struct usb_device *dev) · line 1060

Quoted 9 lines
sound/usb/quirks.c +9 -0
+	err = snd_usb_motu_microbookii_communicate(dev, buf, MICROBOOK_BUF_SIZE,
+						   &actual_length);
+
+	if (err < 0) {
+		kfree(buf);
+		dev_err(&dev->dev,
+			"failed setting the sample rate for Motu MicroBook II: %d\n",
+			err);
+		return err;

Since kfree() is mandatory in the error path, it's better to use goto and kfree() there, instead of spreading over all places.

Quoted 16 lines
sound/usb/quirks.c +16 -0
+	}
+
+	/* Then we poll every 100 ms until the device informs of its readiness. */
+	while (!setup_finished) {
+		if (++poll_attempts > 100) {
+			kfree(buf);
+			dev_err(&dev->dev,
+				"failed booting Motu MicroBook II: timeout\n");
+			return -ENODEV;
+		}
+
+		memset(buf, 0, MICROBOOK_BUF_SIZE);
+		memcpy(buf, poll_ready_seq, ARRAY_SIZE(poll_ready_seq));
+
+		actual_length = ARRAY_SIZE(poll_ready_seq);
+		err = snd_usb_motu_microbookii_communicate(

sound/usb/quirks.c static int snd_usb_axefx3_boot_quirk(struct usb_device *dev) · line 1086

Quoted 10 lines
sound/usb/quirks.c +10 -0
+			dev, buf, MICROBOOK_BUF_SIZE, &actual_length);
+		if (err < 0) {
+			kfree(buf);
+			dev_err(&dev->dev,
+				"failed booting Motu MicroBook II: communication error\n");
+			return err;
+		}
+
+		if (buf[actual_length - 1] == 1)
+			setup_finished = true;

I wouldn't trust the returned value. Let's be paranoid and check whether the actual_length is within the expected range.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+		msleep(100);

BTW, setup_finished is superfluous. You can just write like

		msleep(100);
		if (buf[actual_lenght - 1] == 1)
			break;

Or if 100ms sleep isn't needed in the successful case, it can be

		if (buf[actual_lenght - 1] == 1)
			break;
		msleep(100);

thanks,

Takashi

#2 lore raw

Thank you for your time and comments.

Am Mittwoch, den 27.02.2019, 18:14 +0100 schrieb Takashi Iwai:

Quoted 19 lines
Quoted 17 lines · Manuel Reinhardt

Add an entry to the quirks-table to for usb-audio to recognize the Microbook II (although it only exposes vendor interfaces). A simple boot
quirk is also implemented to set up the sample rate and make sure that
no audio urbs are sent before the device is ready.

This patch only provides audio playback and capture at 96kHz sample rate. Notice the following shortcomings:

- The sample rate is currently hardcoded to 96k although the device also
supports 48k and 44.1k.

- The various mixer controls of the MicroBook are not made available.

Quoted 7 lines
Quoted 4 lines

- The keep-iface control should be on by default because the device shuts down whenever the altsetting is reset which is usually
unwanted.
(I don't know the best way to do this)

Can be set either in the quirk callback you write. Or it'd be not bad to create a white list, too.

I will try to address that in a future patch.

I have fixed the patch to address your remaining concerns.