Skip to contentSkip to conversation

Add generic serial MIDI driver using serial bus API

PATCH Superseded Daniel Kaehn 2 participants 5 messages Apr 20, 2022 – Apr 25, 2022

Subsystems (3)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover Add generic serial MIDI driver using serial bus API

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 3.125 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 3.125 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
Signature · 1 line
2.32.0
#1 lore raw

PATCH 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..900c57c1cfca
@@ -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 3.125 kBaud is needed, configure the clocks of the underlying serial device
+  so that a requested speed of 3.840 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.32.0
#2 lore raw

PATCH 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 3.125 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..be9a161cb593 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 3.125 kBaud, configure the clock of the underlying serial
+	  device so that a requested 3.84 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.32.0
#3 lore raw
Thank you for the patch! Perhaps something to improve:

Hi Daniel,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on tiwai-sound/for-next]
[also build test WARNING on v5.18-rc3 next-20220420]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Kaehn/Add-generic-serial-MIDI-driver-using-serial-bus-API/20220421-035024
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next
config: ia64-allyesconfig (https://download.01.org/0day-ci/archive/20220421/[email protected]/config)
compiler: ia64-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/65e00db0913a4019c3158e31aa6b2fd2c28ba03f
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Daniel-Kaehn/Add-generic-serial-MIDI-driver-using-serial-bus-API/20220421-035024 git checkout 65e00db0913a4019c3158e31aa6b2fd2c28ba03f
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross W=1 O=build_dir ARCH=ia64 SHELL=/bin/bash sound/drivers/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> sound/drivers/serial-generic.c:192:6: warning: no previous prototype for 'snd_serial_generic_write_wakeup' [-Wmissing-prototypes]

192 | void snd_serial_generic_write_wakeup(struct serdev_device *serdev)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +/snd_serial_generic_write_wakeup +192 sound/drivers/serial-generic.c

191

 > 192	void snd_serial_generic_write_wakeup(struct serdev_device *serdev)

193 {
194 struct snd_serial_generic *drvdata = serdev_device_get_drvdata(serdev);
195
196 if (!snd_rawmidi_transmit_empty(drvdata->midi_output))
197 snd_serial_generic_output_write(drvdata->midi_output);
198 }
199

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#4 lore raw
In reply to Daniel Kaehn
Thank you for the patch! Perhaps something to improve:

Hi Daniel,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on tiwai-sound/for-next]
[also build test WARNING on v5.18-rc4 next-20220422]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Kaehn/Add-generic-serial-MIDI-driver-using-serial-bus-API/20220421-035024
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next
config: arm64-allyesconfig (https://download.01.org/0day-ci/archive/20220426/[email protected]/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project 1cddcfdc3c683b393df1a5c9063252eb60e52818)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/65e00db0913a4019c3158e31aa6b2fd2c28ba03f
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Daniel-Kaehn/Add-generic-serial-MIDI-driver-using-serial-bus-API/20220421-035024
git checkout 65e00db0913a4019c3158e31aa6b2fd2c28ba03f
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash arch/arm64/kernel/ drivers/acpi/arm64/ sound/drivers/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> sound/drivers/serial-generic.c:192:6: warning: no previous prototype for function 'snd_serial_generic_write_wakeup' [-Wmissing-prototypes]
   void snd_serial_generic_write_wakeup(struct serdev_device *serdev)
        ^

sound/drivers/serial-generic.c:192:1: note: declare 'static' if the function is not intended to be used outside of this translation unit void snd_serial_generic_write_wakeup(struct serdev_device *serdev)
^
static
1 warning generated.

vim +/snd_serial_generic_write_wakeup +192 sound/drivers/serial-generic.c

191

 > 192	void snd_serial_generic_write_wakeup(struct serdev_device *serdev)

193 {
194 struct snd_serial_generic *drvdata = serdev_device_get_drvdata(serdev);
195
196 if (!snd_rawmidi_transmit_empty(drvdata->midi_output))
197 snd_serial_generic_output_write(drvdata->midi_output);
198 }
199

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp