ALSA: ump: Improve legacy rawmidi & co
Subsystems (2)
cover ALSA: ump: Improve legacy rawmidi & co
Hi,
this is a patch set to fix / enhance the legacy rawmidi handling in UMP core and USB-audio driver, as well as the update of MIDI 2.0 documentation.
Takashi
===
Takashi Iwai (4):
ALSA: usb-audio: Attach legacy rawmidi after probing all UMP EPs
ALSA: ump: Fill group names for legacy rawmidi substreams
ALSA: ump: Don't create unused substreams for static blocks
ALSA: documentation: Add description for USB MIDI 2.0 gadget driver
Documentation/sound/designs/midi-2.0.rst | 188 +++++++++++++++++++++++ include/sound/ump.h | 1 + sound/core/ump.c | 58 ++++++- sound/usb/midi2.c | 15 +- 4 files changed, 250 insertions(+), 12 deletions(-)
Signature · 1 line
2.35.3
PATCH 1/4 ALSA: usb-audio: Attach legacy rawmidi after probing all UMP EPs
The legacy rawmidi devices are the shadows of the main UMP devices, hence it's better to initialize them after all UMP Endpoints are parsed. Then, at the moment the legacy rawmidi is created, we already know the static flag or the proper EP name string, and we can fill those information at UMP core side instead of fiddling the attributes at a later point.
--- sound/core/ump.c | 2 ++ sound/usb/midi2.c | 15 ++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-)
sound/core/ump.c +2 -0
@@ -1150,6 +1150,8 @@ int snd_ump_attach_legacy_rawmidi(struct snd_ump_endpoint *ump,if (output)snd_rawmidi_set_ops (rmidi,SNDRV_RAWMIDI_STREAM_OUTPUT , &snd_ump_legacy_output_ops); +snprintf (rmidi->name,sizeof (rmidi->name),"%s (MIDI 1.0)" , + ump->info.name); rmidi->info_flags = ump->core.info_flags & ~SNDRV_RAWMIDI_INFO_UMP ; rmidi->ops = &snd_ump_legacy_ops; rmidi->private_data = ump;
sound/usb/midi2.c +8 -7
@@ -990,7 +990,7 @@ static int parse_midi_2_0(struct snd_usb_midi2_interface *umidi) } } -return attach_legacy_rawmidi (umidi); +return 0 ; }/* is the given interface for MIDI 2.0? */
@@ -1059,12 +1059,6 @@ static void set_fallback_rawmidi_names(struct snd_usb_midi2_interface *umidi)usb_string (dev, dev->descriptor.iSerialNumber, ump->info.product_id,sizeof (ump->info.product_id)); -#if IS_ENABLED (CONFIG_SND_UMP_LEGACY_RAWMIDI ) -if (ump->legacy_rmidi && !*ump->legacy_rmidi->name) -snprintf (ump->legacy_rmidi->name, -sizeof (ump->legacy_rmidi->name), -"%s (MIDI 1.0)" , ump->info.name); -#endif } }
@@ -1157,6 +1151,13 @@ int snd_usb_midi_v2_create(struct snd_usb_audio *chip, }set_fallback_rawmidi_names (umidi); + + err =attach_legacy_rawmidi (umidi); +if (err <0 ) { +usb_audio_err (chip,"Failed to create legacy rawmidi\n" ); + goto error; + } +return 0 ; error:
Signature · 1 line
2.35.3
PATCH 2/4 ALSA: ump: Fill group names for legacy rawmidi substreams
In reply to Takashi IwaiTo make it clearer which legacy substream corresponds to which UMP group, fill the subname field of each substream object with the group number and the endpoint name, e.g. "Group 1 (My Device)".
Ideally speaking, we should have some better link information to the derived UMP, but it's another feature extension.
--- sound/core/ump.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
sound/core/ump.c +15 -0
@@ -1123,6 +1123,16 @@ static void process_legacy_input(struct snd_ump_endpoint *ump, const u32 *src,spin_unlock_irqrestore (&ump->legacy_locks[dir], flags); } +static void fill_substream_names (struct snd_ump_endpoint *ump, +struct snd_rawmidi *rmidi,int dir) +{ +struct snd_rawmidi_substream *s; + +list_for_each_entry (s, &rmidi->streams[dir].substreams, list) +snprintf (s->name,sizeof (s->name),"Group %d (%s)" , + s->number +1 , ump->info.name); +} +int snd_ump_attach_legacy_rawmidi (struct snd_ump_endpoint *ump,char *id,int device) {
@@ -1156,6 +1166,11 @@ int snd_ump_attach_legacy_rawmidi(struct snd_ump_endpoint *ump, rmidi->ops = &snd_ump_legacy_ops; rmidi->private_data = ump; ump->legacy_rmidi = rmidi; +if (input) +fill_substream_names (ump, rmidi,SNDRV_RAWMIDI_STREAM_INPUT ); +if (output) +fill_substream_names (ump, rmidi,SNDRV_RAWMIDI_STREAM_OUTPUT ); +ump_dbg (ump,"Created a legacy rawmidi #%d (%s)\n" , device, id);return 0 ; }
Signature · 1 line
2.35.3
PATCH 3/4 ALSA: ump: Don't create unused substreams for static blocks
In reply to Takashi IwaiWhen the UMP Endpoint is declared as "static", that is, no dynamic reassignment of UMP Groups, it makes little sense to expose always all 16 groups with 16 substreams. Many of those substreams are disabled groups, hence they are useless, but applications don't know it and try to open / access all those substreams unnecessarily.
This patch limits the number of UMP legacy rawmidi substreams only to the active groups. The behavior is changed only for the static endpoint (i.e. devices without UMP v1.1 feature implemented or with the static block flag is set).
--- include/sound/ump.h | 1 + sound/core/ump.c | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 6 deletions(-)
include/sound/ump.h +1 -0
@@ -45,6 +45,7 @@ struct snd_ump_endpoint {spinlock_t legacy_locks[2 ];struct snd_rawmidi *legacy_rmidi;struct snd_rawmidi_substream *legacy_substreams[2 ][SNDRV_UMP_MAX_GROUPS ]; +unsigned char legacy_mapping[SNDRV_UMP_MAX_GROUPS ];/* for legacy output; need to open the actual substream unlike input */ int legacy_out_opens;
sound/core/ump.c +37 -6
80 lines not on this page · raw message
Signature · 1 line
2.35.3
PATCH 4/4 ALSA: documentation: Add description for USB MIDI 2.0 gadget driver
In reply to Takashi IwaiThe USB MIDI 2.0 gadget driver is now supported for 6.6 kernel, and here we show a brief instruction how to enable and use it.
--- Documentation/sound/designs/midi-2.0.rst | 188 +++++++++++++++++++++++ 1 file changed, 188 insertions(+)
Documentation/sound/designs/midi-2.0.rst +188 -0
@@ -376,3 +376,191 @@ Sequencer API Extensions name and attributes accordingly, and notifies the changes via the announcement to the ALSA sequencer system port, similarly like the normal port change notification. + + +MIDI2 USB Gadget Function Driver +================================ + +The latest kernel contains the support for USB MIDI 2.0 gadget +function driver, which can be used for prototyping and debugging MIDI +2.0 features. + +`CONFIG_USB_GADGET`, `CONFIG_USB_CONFIGFS` and +`CONFIG_USB_CONFIGFS_F_MIDI2` need to be enabled for the MIDI2 gadget +driver. + +In addition, for using a gadget driver, you need a working UDC driver. +In the example below, we use `dummy_hcd` driver (enabled via +`CONFIG_USB_DUMMY_HCD`) that is available on PC and VM for debugging +purpose. There are other UDC drivers depending on the platform, and +those can be used for a real device, instead, too. + +At first, on a system to run the gadget, load `libcomposite` module:: + + % modprobe libcomposite + +and you'll have `usb_gadget` subdirectory under configfs space +(typically `/sys/kernel/config` on modern OS). Then create a gadget +instance and add configurations there, for example:: + + % cd /sys/kernel/config + % mkdir usb_gadget/g1 + + % cd usb_gadget/g1 + % mkdir configs/c.1 + % mkdir functions/midi2.usb0 + + % echo 0x0004 > idProduct + % echo 0x17b3 > idVendor + % mkdir strings/0x409 + % echo "ACME Enterprises" > strings/0x409/manufacturer + % echo "ACMESynth" > strings/0x409/product + % echo "ABCD12345" > strings/0x409/serialnumber + + % mkdir configs/c.1/strings/0x409 + % echo "Monosynth" > configs/c.1/strings/0x409/configuration + % echo 120 > configs/c.1/MaxPower + +At this point, there must be a subdirectory `ep.0`, and that is the +configuration for a UMP Endpoint. You can fill the Endpoint +information like:: + + % echo "ACMESynth" > functions/midi2.usb0/iface_name + % echo "ACMESynth" > functions/midi2.usb0/ep.0/ep_name + % echo "ABCD12345" > functions/midi2.usb0/ep.0/product_id + % echo 0x0123 > functions/midi2.usb0/ep.0/family + % echo 0x4567 > functions/midi2.usb0/ep.0/model + % echo 0x123456 > functions/midi2.usb0/ep.0/manufacturer + % echo 0x12345678 > functions/midi2.usb0/ep.0/sw_revision + +The default MIDI protocol can be set either 1 or 2:: + + % echo 2 > functions/midi2.usb0/ep.0/protocol + +And, you can find a subdirectory `block.0` under this Endpoint +subdirectory. This defines the Function Block information:: + + % echo "Monosynth" > functions/midi2.usb0/ep.0/block.0/name + % echo 0 > functions/midi2.usb0/ep.0/block.0/first_group + % echo 1 > functions/midi2.usb0/ep.0/block.0/num_groups + +Finally, link the configuration and enable it:: + + % ln -s functions/midi2.usb0 configs/c.1 + % echo dummy_udc.0 > UDC + +where `dummy_udc.0` is an example case and it differs depending on the +system. You can find the UDC instances in `/sys/class/udc` and pass +the found name instead:: + + % ls /sys/class/udc + dummy_udc.0 + +Now, the MIDI 2.0 gadget device is enabled, and the gadget host +creates a new sound card instance containing a UMP rawmidi device by +`f_midi2` driver:: + + % cat /proc/asound/cards + .... + 1 [Gadget ]: f_midi2 - MIDI 2.0 Gadget + MIDI 2.0 Gadget + +And on the connected host, a similar card should appear, too, but with +the card and device names given in the configfs above:: + + % cat /proc/asound/cards + .... + 2 [ACMESynth ]: USB-Audio - ACMESynth + ACME Enterprises ACMESynth at usb-dummy_hcd.0-1, high speed + +You can play a MIDI file on the gadget side:: + + % aplaymidi -p 20:1 to_host.mid + +and this will appear as an input from a MIDI device on the connected +host:: + + % aseqdump -p 20:0 -u 2 + +Vice versa, a playback on the connected host will work as an input on +the gadget, too. + +Each Function Block may have different direction and UI-hint, +specified via `direction` and `ui_hint` attributes. +Passing `1` is for input-only, `2` for out-only and `3` for +bidirectional (the default value). For example:: + + % echo 2 > functions/midi2.usb0/ep.0/block.0/direction + % echo 2 > functions/midi2.usb0/ep.0/block.0/ui_hint + +When you need more than one Function Blocks, you can create +subdirectories `block.1`, `block.2`, etc dynamically, and configure +them in the configuration procedure above before linking. +For example, to create a second Function Block for a keyboard:: + + % mkdir functions/midi2.usb0/ep.0/block.1 + % echo "Keyboard" > functions/midi2.usb0/ep.0/block.1/name + % echo 1 > functions/midi2.usb0/ep.0/block.1/first_group + % echo 1 > functions/midi2.usb0/ep.0/block.1/num_groups + % echo 1 > functions/midi2.usb0/ep.0/block.1/direction + % echo 1 > functions/midi2.usb0/ep.0/block.1/ui_hint + +The `block.*` subdirectories can be removed dynamically, too (except +for `block.0` which is persistent). + +For assigning a Function Block for MIDI 1.0 I/O, set up in `is_midi1` +attribute. 1 is for MIDI 1.0, and 2 is for MIDI 1.0 with low speed +connection:: + + % echo 2 > functions/midi2.usb0/ep.0/block.1/is_midi1 + +For disabling the processing of UMP Stream messages in the gadget +driver, pass `0` to `process_ump attribute in the top-level config:: + + % echo 0 > functions/midi2.usb0/process_ump + +The MIDI 1.0 interface at altset 0 is supported by the gadget driver, +too. When MIDI 1.0 interface is selected by the connected host, the +UMP I/O on the gadget is translated from/to USB MIDI 1.0 packets +accordingly while the gadget driver keeps communicating with the +user-space over UMP rawmidi. + +MIDI 1.0 ports are set up from the config in each Function Block. +For example:: + + % echo 0 > functions/midi2.usb0/ep.0/block.0/midi1_first_group + % echo 1 > functions/midi2.usb0/ep.0/block.0/midi1_num_groups + +The configuration above will enable the Group 1 (the index 0) for MIDI +1.0 interface. Note that those groups must be in the groups defined +for the Function Block itself. + +The gadget driver supports more than one UMP Endpoints, too. +Similarly like the Function Blocks, you can create a new subdirectory +`ep.1` (but under the card top-level config) to enable a new Endpoint:: + + % mkdir functions/midi2.usb0/ep.1 + +and create a new Function Block there. For example, to create 4 +Groups for the Function Block of this new Endpoint:: + + % mkdir functions/midi2.usb0/ep.1/block.0 + % echo 4 > functions/midi2.usb0/ep.1/block.0/num_groups + +Now, you'll have 4 rawmidi devices in total: the first two are UMP +rawmidi devices for Endpoint 0 and Endpoint 1, and other two for the +legacy MIDI 1.0 rawmidi devices corresponding to both EP 0 and EP 1. + +The current altsetting on the gadget can be informed via a control +element "Operation Mode" with `RAWMIDI` iface. e.g. you can read it +via `amixer` program running on the gadget host like:: + + % amixer -c1 cget iface=RAWMIDI,name='Operation Mode' + ; type=INTEGER,access=r--v----,values=1,min=0,max=2,step=0 + : values=2 + +The value (shown in the second returned line with `: values=`) +indicates 1 for MIDI 1.0 (altset 0), 2 for MIDI 2.0 (altset 1) and 0 +for unset. + +As of now, the configurations can't be changed after binding.
Signature · 1 line
2.35.3