Skip to contentSkip to conversation

Add generic serial MIDI driver using serial bus API

PATCH Superseded Daniel Kaehn 2 participants 5 messages Apr 21, 2022 – Apr 22, 2022

Subsystems (3)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add generic serial MIDI driver using serial bus API

Changes in v2:
- Fix 'snd_serial_generic_write_wakeup' missing static keyword as reported by the kernel test robot
- Correct 3.125 kBaud -> 31.25 kBaud and 3.84 kBaud -> 38.4 kBaud in documentation for MIDI

Background as included in the initial cover letter:

Generic serial MIDI driver adding support for using serial devices compatible with the serial bus as raw MIDI devices, allowing using additional serial devices not compatible with the existing serial-u16550 driver. Supports only setting standard serial baudrates on the underlying serial device; however, the underlying serial device can be configured so that a requested 38.4 kBaud is actually the standard MIDI 31.25 kBaud. Supports DeviceTree configuration.

The need for this driver arose from a project using a Raspberry Pi4 which needed to receive and send raw MIDI with low latency. The pl011 UART used is not compatible with the existing serial MIDI driver made for u16550-style devices. Using a userspace program such as ttymidi to feed input from the TTY device to a virtual ALSA MIDI device was functional, but not ideal.

I am not sure if a MIDI driver needing the mentioned 'hack' to clock 38.4 kBaud down to the standard MIDI baud is permissible in the mainline kernel, but am submitting nevertheless in case it is useful. To my knowledge, it doesn't seem that there would be any way for this driver to manually configure a serial port to 31.25 kBaud using the serial bus API (please correct me f I'm wrong). In my use case, I am actually configuring one port to run at 115.2 kBaud for faster communication with a custom onboard MIDI controller.

Daniel Kaehn (2):
dt-bindings: sound: Add generic serial MIDI device Add generic serial MIDI driver using serial bus API

 .../devicetree/bindings/sound/serialmidi.yaml |  41 +++
 sound/drivers/Kconfig                         |  17 +
 sound/drivers/Makefile                        |   2 +
 sound/drivers/serial-generic.c                | 344 ++++++++++++++++++
 4 files changed, 404 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/serialmidi.yaml
 create mode 100644 sound/drivers/serial-generic.c

base-commit: b253435746d9a4a701b5f09211b9c14d3370d0da

Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 1/2 dt-bindings: sound: Add generic serial MIDI device

Adds dt-binding for snd-serial-generic serial MIDI driver

---
 .../devicetree/bindings/sound/serialmidi.yaml | 41 +++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/serialmidi.yaml
Documentation/devicetree/bindings/sound/serialmidi.yaml new file +41 -0
new file mode 100644
index 000000000000..74f02a9e1190
@@ -0,0 +1,41 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/sound/serialmidi.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Generic Serial MIDI Device
+
+maintainers:
+  - Daniel Kaehn <[email protected]>
+
+description: |
+  Generic MIDI interface using a serial device. Can only be set to use standard speeds
+  corresponding to supported baud rates of the underlying serial device. If standard MIDI
+  speed of 31.25 kBaud is needed, configure the clocks of the underlying serial device
+  so that a requested speed of 38.4 kBaud resuts in the standard MIDI baud rate.
+
+properties:
+  compatible:
+    const: serialmidi
+
+  speed:
+    maxItems: 1
+    description: |
+      Speed to set the serial port to when the MIDI device is opened.
+      If not specified, the underlying serial device is allowed to use its configured default speed.
+
+required:
+  - compatible
+
+additionalProperties: false
+
+examples:
+  - |
+    serial {
+        midi {
+            compatible = "serialmidi";
+            speed = <38400>;
+        };
+    };
Signature · 1 line
2.35.1
#2 lore raw

PATCH v2 2/2 Add generic serial MIDI driver using serial bus API

In reply to Daniel Kaehn

Generic serial MIDI driver adding support for using serial devices compatible with the serial bus as raw MIDI devices, allowing using additional serial devices not compatible with the existing serial-u16550 driver. Supports only setting standard serial baudrates on the underlying serial device; however, the underlying serial device can be configured so that a requested 38.4 kBaud is actually the standard MIDI 31.25 kBaud. Supports DeviceTree configuration.

---

One ugly portion in the code I wanted to point out, but didn't find a 'nice' way of solving. snd_serial_generic_output_write is called to read from ALSA's output MIDI buffer and write to the serdev_device's input buffer. While copying directly from the former to the later would be desirable for performance, I assume violating the abstraction would never be permissable. The current implementation creates an internal buffer of an arbitrary size (currently 256) and copies there as an intermediate step. Any advice on how to make this better is appreciated.

 sound/drivers/Kconfig          |  17 ++
 sound/drivers/Makefile         |   2 +
 sound/drivers/serial-generic.c | 344 +++++++++++++++++++++++++++++++++
 3 files changed, 363 insertions(+)
 create mode 100644 sound/drivers/serial-generic.c
sound/drivers/Kconfig +17 -0
index ca4cdf666f82..edea507fb8e7 100644
@@ -165,6 +165,23 @@ config SND_SERIAL_U16550
 	  To compile this driver as a module, choose M here: the module
 	  will be called snd-serial-u16550.
 
+config SND_SERIAL_GENERIC
+	tristate "Generic serial MIDI driver"
+	depends on SERIAL_DEV_BUS
+	select SND_RAWMIDI
+	help
+	  To include support for mapping generic serial devices as raw
+	  ALSA MIDI devices, say Y here. The driver only supports setting
+	  the serial port to standard baudrates. To attain the standard MIDI
+	  baudrate of 31.25 kBaud, configure the clock of the underlying serial
+	  device so that a requested 38.4 kBaud will result in the standard speed.
+
+	  Use this devicetree binding to configure serial port mapping
+	  <file:Documentation/devicetree/bindings/sound/serialmidi.yaml>
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called snd-serial-generic.
+
 config SND_MPU401
 	tristate "Generic MPU-401 UART driver"
 	select SND_MPU401_UART
sound/drivers/Makefile +2 -0
index c0fe4eccdaef..b60303180a1b 100644
@@ -10,6 +10,7 @@ snd-mtpav-objs := mtpav.o
 snd-mts64-objs := mts64.o
 snd-portman2x4-objs := portman2x4.o
 snd-serial-u16550-objs := serial-u16550.o
+snd-serial-generic-objs := serial-generic.o
 snd-virmidi-objs := virmidi.o
 
 # Toplevel Module Dependency
@@ -17,6 +18,7 @@ obj-$(CONFIG_SND_DUMMY) += snd-dummy.o
 obj-$(CONFIG_SND_ALOOP) += snd-aloop.o
 obj-$(CONFIG_SND_VIRMIDI) += snd-virmidi.o
 obj-$(CONFIG_SND_SERIAL_U16550) += snd-serial-u16550.o
+obj-$(CONFIG_SND_SERIAL_GENERIC) += snd-serial-generic.o
 obj-$(CONFIG_SND_MTPAV) += snd-mtpav.o
 obj-$(CONFIG_SND_MTS64) += snd-mts64.o
 obj-$(CONFIG_SND_PORTMAN2X4) += snd-portman2x4.o
sound/drivers/serial-generic.c new file +344 -0

344 lines not on this page · raw message

Signature · 1 line
2.35.1
#3 lore raw
Quoted 12 lines · Daniel Kaehn

Generic serial MIDI driver adding support for using serial devices compatible with the serial bus as raw MIDI devices, allowing using additional serial devices not compatible with the existing serial-u16550 driver. Supports only setting standard serial baudrates on the underlying serial device; however, the underlying serial device can be configured so that a requested 38.4 kBaud is actually the standard MIDI 31.25 kBaud. Supports DeviceTree configuration.

---

Quoted 8 lines

One ugly portion in the code I wanted to point out, but didn't find a 'nice' way of solving. snd_serial_generic_output_write is called to read from ALSA's output MIDI buffer and write to the serdev_device's input buffer. While copying directly from the former to the later would be desirable for performance, I assume violating the abstraction would never be permissable. The current implementation creates an internal buffer of an arbitrary size (currently 256) and copies there as an intermediate step. Any advice on how to make this better is appreciated.

It's OK, as MIDI data isn't that huge and fast, and the optimization is done at any time later.

About the code: in general, please avoid the use of snd_printk() and co. Those are old helpers, and better to use dev_err(), dev_dbg(), etc, if possible.

Some more nitpicking:

sound/drivers/serial-generic.c line 52

Quoted 3 lines
sound/drivers/serial-generic.c new file +3 -0
+static int snd_serial_generic_ensure_serdev_open(struct snd_serial_generic *drvdata)
+{
+	int err = 0;

Superfluous initialization.

sound/drivers/serial-generic.c line 55

Quoted 3 lines
sound/drivers/serial-generic.c new file +3 -0
+	unsigned int actual_baud;
+
+	if (drvdata->filemode == SERIAL_MODE_NOT_OPENED) {

This expression is rather confusing. It's essentially a zero check, and the simple zero check is rather easier to understand that there is no opener, i.e.

	if (!drvdata->filemode) {
		.....

sound/drivers/serial-generic.c line 73

Quoted 3 lines
sound/drivers/serial-generic.c new file +3 -0
+static int snd_serial_generic_input_open(struct snd_rawmidi_substream *substream)
+{
+	int err = 0;

Superfluous.

sound/drivers/serial-generic.c line 82

Quoted 9 lines
sound/drivers/serial-generic.c new file +9 -0
+	struct snd_serial_generic *drvdata = substream->rmidi->private_data;
+
+	snd_printd("snd-serial-generic: DEBUG - Opening input for card %s\n",
+		drvdata->card->shortname);
+
+	err = snd_serial_generic_ensure_serdev_open(drvdata);
+	if (err < 0) {
+		snd_printk(KERN_WARNING "snd-serial-generic: failed to open input for card %s",
+			drvdata->card->shortname);

Spewing an error message at each time would fill up the kernel log unnecessarily. Make it a debug message, if you really need to print something.

sound/drivers/serial-generic.c line 97

Quoted 7 lines
sound/drivers/serial-generic.c new file +7 -0
+static int snd_serial_generic_input_close(struct snd_rawmidi_substream *substream)
+{
+	struct snd_serial_generic *drvdata = substream->rmidi->private_data;
+
+	drvdata->filemode &= ~SERIAL_MODE_INPUT_OPEN;
+	drvdata->midi_input = NULL;
+	if (drvdata->filemode == SERIAL_MODE_NOT_OPENED)

Use zero check instead. (Ditto for *_output functions).

sound/drivers/serial-generic.c line 154

Quoted 11 lines
sound/drivers/serial-generic.c new file +11 -0
+#define INTERNAL_BUF_SIZE 256
+
+static void snd_serial_generic_output_write(struct snd_rawmidi_substream *substream)
+{
+	static char buf[INTERNAL_BUF_SIZE];
+	int num_bytes;
+	struct snd_serial_generic *drvdata = substream->rmidi->private_data;
+
+	num_bytes = snd_rawmidi_transmit_peek(substream, buf, INTERNAL_BUF_SIZE);
+	num_bytes = serdev_device_write_buf(drvdata->serdev, buf, num_bytes);
+	snd_rawmidi_transmit_ack(substream, num_bytes);

This needs to be a loop to process all pending bytes?

sound/drivers/serial-generic.c line 185

Quoted 4 lines
sound/drivers/serial-generic.c new file +4 -0
+static int snd_serial_generic_receive_buf(struct serdev_device *serdev,
+				const unsigned char *buf, size_t count)
+{
+	int ret = 0;

Superfluous initialization.

sound/drivers/serial-generic.c line 218

Quoted 13 lines
sound/drivers/serial-generic.c new file +13 -0
+static int snd_serial_generic_create(struct serdev_device *serdev,
+				struct snd_card *card,
+				struct snd_serial_generic **rserialmidi)
+{
+	struct snd_serial_generic *drvdata;
+	int err;
+
+	drvdata = devm_kzalloc(card->dev, sizeof(*drvdata), GFP_KERNEL);
+	if (!drvdata)
+		return -ENOMEM;
+
+	drvdata->serdev = serdev;
+	drvdata->card = card;

You can use card's private_data instead of an extra kmalloc(). (Pass sizeof(*drvdata) to the extra_size argument of snd_devm_card_new()).

sound/drivers/serial-generic.c line 221

Quoted 2 lines
sound/drivers/serial-generic.c new file +2 -0
+	if (serdev->dev.of_node) {
+		err = of_property_read_u32(serdev->dev.of_node, "speed", &drvdata->baudrate);

So, as we rely on of_node, the Kconfig should have the dependency, too?

sound/drivers/serial-generic.c line 344

Quoted 13 lines
sound/drivers/serial-generic.c new file +13 -0
+static int __init alsa_card_serial_generic_init(void)
+{
+	snd_printk(KERN_INFO "snd-serial-generic: Generic serial-based MIDI device\n");
+	return serdev_device_driver_register(&snd_serial_generic_driver);
+}
+
+static void __exit alsa_card_serial_generic_exit(void)
+{
+	serdev_device_driver_unregister(&snd_serial_generic_driver);
+}
+
+module_init(alsa_card_serial_generic_init)
+module_exit(alsa_card_serial_generic_exit)

Those are simplified with module_serdev_device_driver()?

thanks,

Takashi

#4 lore raw

Thanks for taking the time to look at this. I've responded to your comments below, but ultimately agree with all of them, and will update accordingly.

Quoted 23 lines · Takashi Iwai
Quoted 12 lines · Daniel Kaehn

Generic serial MIDI driver adding support for using serial devices compatible with the serial bus as raw MIDI devices, allowing using additional serial devices not compatible with the existing serial-u16550 driver. Supports only setting standard serial baudrates on the underlying serial device; however, the underlying serial device can be configured so that a requested 38.4 kBaud is actually the standard MIDI 31.25 kBaud. Supports DeviceTree configuration.

---

Quoted 8 lines

One ugly portion in the code I wanted to point out, but didn't find a 'nice' way of solving. snd_serial_generic_output_write is called to read from ALSA's output MIDI buffer and write to the serdev_device's input buffer. While copying directly from the former to the later would be desirable for performance, I assume violating the abstraction would never be permissable. The current implementation creates an internal buffer of an arbitrary size (currently 256) and copies there as an intermediate step. Any advice on how to make this better is appreciated.

Quoted 6 lines

It's OK, as MIDI data isn't that huge and fast, and the optimization is done at any time later.

Quoted 1 line

About the code: in general, please avoid the use of snd_printk() and

co. Those are old helpers, and better to use dev_err(), dev_dbg(), etc, if possible.

Good to know, will fix. Saw them in the ALSA headers and assumed that's what I should be using.

Quoted 8 lines

Some more nitpicking:

sound/drivers/serial-generic.c line 52

Quoted 3 lines
sound/drivers/serial-generic.c new file +3 -0
+static int snd_serial_generic_ensure_serdev_open(struct snd_serial_generic *drvdata)
+{
+	int err = 0;

Superfluous initialization.

That it is... will fix.

Quoted 11 lines

sound/drivers/serial-generic.c line 55

Quoted 3 lines
sound/drivers/serial-generic.c new file +3 -0
+	unsigned int actual_baud;
+
+	if (drvdata->filemode == SERIAL_MODE_NOT_OPENED) {

This expression is rather confusing. It's essentially a zero check, and the simple zero check is rather easier to understand that there is no opener, i.e.

	if (!drvdata->filemode) {
		.....

Fair enough. My goal was to be as consistent as I could with the existing serial midi driver - but I suppose that is quite old. This will be changed.

Quoted 6 lines

sound/drivers/serial-generic.c line 73

Quoted 3 lines
sound/drivers/serial-generic.c new file +3 -0
+static int snd_serial_generic_input_open(struct snd_rawmidi_substream *substream)
+{
+	int err = 0;

Superfluous.

Will fix as well.

Quoted 5 lines
Quoted 5 lines
sound/drivers/serial-generic.c new file +5 -0
+	struct snd_serial_generic *drvdata = substream->rmidi->private_data;
+
+	snd_printd("snd-serial-generic: DEBUG - Opening input for card %s\n",
+		drvdata->card->shortname);
+
Quoted 9 lines

sound/drivers/serial-generic.c line 82

Quoted 4 lines
sound/drivers/serial-generic.c new file +4 -0
+	err = snd_serial_generic_ensure_serdev_open(drvdata);
+	if (err < 0) {
+		snd_printk(KERN_WARNING "snd-serial-generic: failed to open input for card %s",
+			drvdata->card->shortname);

Spewing an error message at each time would fill up the kernel log unnecessarily. Make it a debug message, if you really need to print something.

Good point - now that I think of it, the real purpose of this was for debugging.

Will remove.

Quoted 10 lines

sound/drivers/serial-generic.c line 97

Quoted 7 lines
sound/drivers/serial-generic.c new file +7 -0
+static int snd_serial_generic_input_close(struct snd_rawmidi_substream *substream)
+{
+	struct snd_serial_generic *drvdata = substream->rmidi->private_data;
+
+	drvdata->filemode &= ~SERIAL_MODE_INPUT_OPEN;
+	drvdata->midi_input = NULL;
+	if (drvdata->filemode == SERIAL_MODE_NOT_OPENED)

Use zero check instead. (Ditto for *_output functions).

Will update.

Quoted 5 lines
Quoted 5 lines
sound/drivers/serial-generic.c new file +5 -0
+#define INTERNAL_BUF_SIZE 256
+
+static void snd_serial_generic_output_write(struct snd_rawmidi_substream *substream)
+{
+	static char buf[INTERNAL_BUF_SIZE];
Quoted 9 lines

sound/drivers/serial-generic.c line 154

Quoted 6 lines
sound/drivers/serial-generic.c new file +6 -0
+	int num_bytes;
+	struct snd_serial_generic *drvdata = substream->rmidi->private_data;
+
+	num_bytes = snd_rawmidi_transmit_peek(substream, buf, INTERNAL_BUF_SIZE);
+	num_bytes = serdev_device_write_buf(drvdata->serdev, buf, num_bytes);
+	snd_rawmidi_transmit_ack(substream, num_bytes);

This needs to be a loop to process all pending bytes?

Part of me was assuming the buffer size should be made big enough so that this wouldn't have to loop (and the rest of the data would still get processed when _wakeup is called) - but agree that it'd definitely better to make sure that either the rawmidi buffer is empty or the serdev buffer is full before exiting. Will update.

Quoted 7 lines

sound/drivers/serial-generic.c line 185

Quoted 4 lines
sound/drivers/serial-generic.c new file +4 -0
+static int snd_serial_generic_receive_buf(struct serdev_device *serdev,
+				const unsigned char *buf, size_t count)
+{
+	int ret = 0;

Superfluous initialization.

I guess I really got in the habit of initializing values to zero.. will fix.

Quoted 18 lines

sound/drivers/serial-generic.c line 218

Quoted 13 lines
sound/drivers/serial-generic.c new file +13 -0
+static int snd_serial_generic_create(struct serdev_device *serdev,
+				struct snd_card *card,
+				struct snd_serial_generic **rserialmidi)
+{
+	struct snd_serial_generic *drvdata;
+	int err;
+
+	drvdata = devm_kzalloc(card->dev, sizeof(*drvdata), GFP_KERNEL);
+	if (!drvdata)
+		return -ENOMEM;
+
+	drvdata->serdev = serdev;
+	drvdata->card = card;

You can use card's private_data instead of an extra kmalloc(). (Pass sizeof(*drvdata) to the extra_size argument of snd_devm_card_new()).

Quoted 6 lines

sound/drivers/serial-generic.c line 221

Quoted 2 lines
sound/drivers/serial-generic.c new file +2 -0
+	if (serdev->dev.of_node) {
+		err = of_property_read_u32(serdev->dev.of_node, "speed", &drvdata->baudrate);

So, as we rely on of_node, the Kconfig should have the dependency, too?

Good point. I thought about this for a while, which is partially why I wrote in the ability for the only DT param of 'speed' to not be specified, using the default of the underlying serial device, in case there was a use case for this on a non DT-enabled system, where someone might manually bind the driver. I do agree though, since this was definitely intended to be used with DT.

I'll add that as a dependency for now, and see if there's a future need to use without DT.

Quoted 7 lines
Quoted 7 lines
sound/drivers/serial-generic.c new file +7 -0
+static int __init alsa_card_serial_generic_init(void)
+{
+	snd_printk(KERN_INFO "snd-serial-generic: Generic serial-based MIDI device\n");
+	return serdev_device_driver_register(&snd_serial_generic_driver);
+}
+
+static void __exit alsa_card_serial_generic_exit(void)
Quoted 9 lines

sound/drivers/serial-generic.c line 344

Quoted 6 lines
sound/drivers/serial-generic.c new file +6 -0
+{
+	serdev_device_driver_unregister(&snd_serial_generic_driver);
+}
+
+module_init(alsa_card_serial_generic_init)
+module_exit(alsa_card_serial_generic_exit)

Those are simplified with module_serdev_device_driver()?

Will update.

Quoted 4 lines

thanks,

Takashi

Thanks again for the comments!

Daniel Kaehn