usb: Add USB MIDI 2.0 Gadget Function Driver
Subsystems (2)
cover usb: Add USB MIDI 2.0 Gadget Function Driver
Hi,
This is a patch set for adding a USB gadget function driver for USB MIDI 2.0 interface. The MIDI 2.0 support in the host side was already enabled since 6.5 kernel in snd-usb-audio driver. Now, this provides a similar gadget functionality using the existing ALSA UMP helper functions.
The gadget driver provides both MIDI 2.0 and MIDI 1.0 fallback interfaces with one or more loopback UMP rawmidi devices. The configfs implementation is flexible enough to allow users to set up multiple UMP Endpoints and Function Blocks.
The first patch adds the bare f_midi2 driver without configfs support, followed by the configfs support, MIDI 1.0 interface support and a few other extensions and documentation.
Takashi
===
Takashi Iwai (7):
usb: gadget: Add support for USB MIDI 2.0 function driver
usb: gadget: midi2: Add configfs support
usb: gadget: midi2: Dynamically create MIDI 1.0 altset descriptors
usb: gadget: midi2: MIDI 1.0 interface (altset 0) support
usb: gadget: midi2: Add testing documentation
usb: gadget: midi2: Add "Operation Mode" control
usb: gadget: midi2: More flexible MIDI 1.0 configuration
.../ABI/testing/configfs-usb-gadget-midi2 | 54 + Documentation/usb/gadget-testing.rst | 154 + drivers/usb/gadget/Kconfig | 18 + drivers/usb/gadget/function/Makefile | 2 + drivers/usb/gadget/function/f_midi2.c | 2883 +++++++++++++++++ drivers/usb/gadget/function/u_midi2.h | 81 + 6 files changed, 3192 insertions(+) create mode 100644 Documentation/ABI/testing/configfs-usb-gadget-midi2 create mode 100644 drivers/usb/gadget/function/f_midi2.c create mode 100644 drivers/usb/gadget/function/u_midi2.h
Signature · 1 line
2.35.3
PATCH 1/7 usb: gadget: Add support for USB MIDI 2.0 function driver
This patch adds the support for USB MIDI 2.0 gadget function driver. The driver emulates a USB MIDI 2.0 interface with one or more UMP Endpoints, where each of UMP Endpoint is a pair of MIDI Endpoints for handling MIDI 2.0 UMP packets. When the function driver is bound, the driver creates an ALSA card object with UMP rawmidi devices. This is a kind of loop-back where the incoming and upcoming UMP packets from/to the MIDI 2.0 UMP Endpoints are transferred as-is. In addition, legacy (MIDI 1.0) rawmidi devices are created, so that legacy applications can work in the gadget side, too.
When a USB MIDI 2.0 gadget interface appears, the connected host can use it with the snd-usb-audio driver where MIDI 2.0 support is enabled. Both gadget and connected hosts will have the similar UMP Endpoint and Function Block (or Group Terminal Block) information. Slight differences are the direction and UI-hint bits; it's due to the nature of gadget driver, and the input/output direction is swapped in both sides (the input for gadget is the output for host, and vice versa).
The driver supports the brand-new UMP v1.1 feature, including the UMP Stream message handling for providing UMP Endpoint and Function Block information as well as dealing with the MIDI protocol switch. The driver responds to UMP Stream messages by itself. OTOH, MIDI-CI message handling isn't implemented in the kernel driver; it should be processed in the user-space through the loopback UMP device.
As of this patch, the whole configuration is fixed, providing only one bidirectional UMP Endpoint containing a single FB/GTB with a single UMP Group. The configuration will be dynamically changeable in the following patches.
The traditional MIDI 1.0 is still provided in the altset 0 (which is mandatory per spec). But it's only about the configuration, and no actual I/O will be running for the altset 0 as of this patch. The proper support MIDI 1.0 altset will follow in later patches, too.
--- drivers/usb/gadget/Kconfig | 18 + drivers/usb/gadget/function/Makefile | 2 + drivers/usb/gadget/function/f_midi2.c | 1691 +++++++++++++++++++++++++ drivers/usb/gadget/function/u_midi2.h | 79 ++ 4 files changed, 1790 insertions(+) create mode 100644 drivers/usb/gadget/function/f_midi2.c create mode 100644 drivers/usb/gadget/function/u_midi2.h
drivers/usb/gadget/Kconfig +18 -0
@@ -208,6 +208,9 @@ config USB_F_UVC config USB_F_MIDI tristate +config USB_F_MIDI2 + tristate + config USB_F_HID tristate
@@ -436,6 +439,21 @@ config USB_CONFIGFS_F_MIDI connections can then be made on the gadget system, using ALSA's aconnect utility etc. +config USB_CONFIGFS_F_MIDI2 + bool "MIDI 2.0 function" + depends on USB_CONFIGFS + depends on SND + select USB_LIBCOMPOSITE + select SND_UMP + select SND_UMP_LEGACY_RAWMIDI + select USB_F_MIDI2 + help + The MIDI 2.0 function driver provides the generic emulated + USB MIDI 2.0 interface, looped back to ALSA UMP rawmidi + device on the gadget host. It supports UMP 1.1 spec and + responds UMP Stream messages for UMP Endpoint and Function + Block information / configuration. + config USB_CONFIGFS_F_HID bool "HID function" depends on USB_CONFIGFS
drivers/usb/gadget/function/Makefile +2 -0
@@ -44,6 +44,8 @@ usb_f_uvc-y := f_uvc.o uvc_queue.o uvc_v4l2.o uvc_video.o uvc_configfs.o obj-$(CONFIG_USB_F_UVC) += usb_f_uvc.o usb_f_midi-y := f_midi.o obj-$(CONFIG_USB_F_MIDI) += usb_f_midi.o +usb_f_midi2-y := f_midi2.o +obj-$(CONFIG_USB_F_MIDI2) += usb_f_midi2.o usb_f_hid-y := f_hid.o obj-$(CONFIG_USB_F_HID) += usb_f_hid.o usb_f_printer-y := f_printer.o
drivers/usb/gadget/function/f_midi2.c new file +1691 -0
1,691 lines not on this page · raw message
drivers/usb/gadget/function/u_midi2.h new file +79 -0
79 lines not on this page · raw message
Signature · 1 line
2.35.3
PATCH 2/7 usb: gadget: midi2: Add configfs support
In reply to Takashi IwaiThis patch adds the support of configfs to MIDI 2.0 function driver for users to allow configuring the UMP Endpoint and Function Blocks more flexibly. The configuration is in a tree form. The top-most contains some card-level configurations. UMP Endpoints are defined in subdirectories (ep.0, ep.1, etc) that contain Endpoint-specific configurations such as ep_name, etc. And, UMP Function Blocks are defined in the subdirectories (block.0, block.1, etc) under EP subdirectory. As default, the driver creates a single UMP Endpoint (ep.0) and a single Function Block (block.0) to work in a minimalistic manner. User can modify those attributes freely to fit with the demands.
When multiple Function Blocks are required, user can create another directory as block.1, block.2, and so on (up to block.31). A block.* directory can be deleted dynamically, too. A caveat is that the block number has to be continuous.
Similarly, when multiple UMP Endpoints are required, user can create another directory as ep.1, ep.2, up to ep.3.
Also, some driver behavior can be controlled in the card top-level configs. e.g. you can pass process_ump=0 to disable the processing of UMP Stream messages. This would be equivalent with the older MIDI 2.0 spec that doesn't support UMP v1.1 features.
The configfs interface checks upper- / lower-bound of input values, and more sanity checks are performed at binding.
Attributes can't be changed any longer once when the instance is linked to UDC.
--- .../ABI/testing/configfs-usb-gadget-midi2 | 52 ++ drivers/usb/gadget/function/f_midi2.c | 621 +++++++++++++++++- 2 files changed, 650 insertions(+), 23 deletions(-) create mode 100644 Documentation/ABI/testing/configfs-usb-gadget-midi2
Documentation/ABI/testing/configfs-usb-gadget-midi2 new file +52 -0
@@ -0,0 +1,52 @@ +What: /config/usb-gadget/gadget/functions/midi2.name +Date: Jul 2023 +KernelVersion: 6.6 +Description: + The attributes: + + ============ =============================================== + process_ump Flag to process UMP Stream messages (0 or 1) + static_block Flag for static blocks (0 or 1) + iface_name MIDI interface name string + ============ =============================================== + +What: /config/usb-gadget/gadget/functions/midi2.name/ep.number +Date: Jul 2023 +KernelVersion: 6.6 +Description: + This group contains a UMP Endpoint configuration. + A new Endpoint starts from 0, and can be up to 3. + + The attributes: + + ============= =============================================== + protocol_caps MIDI protocol capabilities (1, 2 or 3 for both) + protocol Default MIDI protocol (1 or 2) + ep_name UMP Endpoint name string + product_id Product ID string + manufacturer Manufacture ID (24 bit) + family Device family ID (16 bit) + model Device model ID (16 bit) + sw_revision Software Revision (32 bit) + ============= =============================================== + +What: /config/usb-gadget/gadget/functions/midi2.name/ep.number/block.number +Date: Jul 2023 +KernelVersion: 6.6 +Description: + This group contains a UMP Function Block configuration. + A new block starts from 0, and can be up to 31. + + The attributes: + + =============== =============================================== + name Function Block name string + direction 1: input, 2: output, 3: bidirectional + first_group The first UMP Group number (0-15) + num_groups The number of groups in this FB (1-16) + ui_hint 0: unknown, 1: receiver, 2: sender, 3: both + midi_ci_verison Supported MIDI-CI version number (8 bit) + is_midi1 Legacy MIDI 1.0 device (0, 1 or 2) + sysex8_streams Max number of SysEx8 streams (8 bit) + active Active FB flag (0 or 1) + =============== ===============================================
drivers/usb/gadget/function/f_midi2.c +598 -23
711 lines not on this page · raw message
Signature · 1 line
2.35.3
PATCH 3/7 usb: gadget: midi2: Dynamically create MIDI 1.0 altset descriptors
In reply to Takashi IwaiThis patch extends MIDI 2.0 function driver to deal with more MIDI1 Jacks depending on the given Block configuration.
For MIDI 1.0, we take the configuration given in Function Block 0, and create MIDI Jacks and Endpoints depending on the definition there. That is, when more UMP Groups are defined in the Block 0, the corresponding MIDI1 Jacks will be created.
--- drivers/usb/gadget/function/f_midi2.c | 228 ++++++++++++++++++-------- 1 file changed, 157 insertions(+), 71 deletions(-)
drivers/usb/gadget/function/f_midi2.c +157 -71
@@ -151,7 +151,7 @@ static struct usb_ms20_gr_trm_block_descriptor gtb_desc = { };DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR (1 ); -DECLARE_USB_MS_ENDPOINT_DESCRIPTOR (1 ); +DECLARE_USB_MS_ENDPOINT_DESCRIPTOR (16 );DECLARE_UAC_AC_HEADER_DESCRIPTOR (1 );DECLARE_USB_MS20_ENDPOINT_DESCRIPTOR (32 );
@@ -185,7 +185,7 @@ static struct usb_interface_descriptor midi2_midi1_if_desc = { .bDescriptorType = USB_DT_INTERFACE, .bInterfaceNumber = 0, // to be filled .bAlternateSetting = 0, - .bNumEndpoints = 2, + .bNumEndpoints = 2, // to be filled .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING, .bInterfaceProtocol = 0,
@@ -200,50 +200,6 @@ static struct usb_ms_header_descriptor midi2_midi1_class_desc = { .wTotalLength = __cpu_to_le16(0x41), // to be calculated }; -/* MIDI 1.0 IN (Embedded) Jack */ -static struct usb_midi_in_jack_descriptor midi2_midi1_in_jack1_desc = { - .bLength =0x06 , - .bDescriptorType =USB_DT_CS_INTERFACE , - .bDescriptorSubtype =USB_MS_MIDI_IN_JACK , - .bJackType =USB_MS_EMBEDDED , - .bJackID =0x01 , - .iJack =0 , -}; - -/* MIDI 1.0 IN (External) Jack */ -static struct usb_midi_in_jack_descriptor midi2_midi1_in_jack2_desc = { - .bLength =0x06 , - .bDescriptorType =USB_DT_CS_INTERFACE , - .bDescriptorSubtype =USB_MS_MIDI_IN_JACK , - .bJackType =USB_MS_EXTERNAL , - .bJackID =0x02 , - .iJack =0 , -}; - -/* MIDI 1.0 OUT (Embedded) Jack */ -static struct usb_midi_out_jack_descriptor_1 midi2_midi1_out_jack1_desc = { - .bLength =0x09 , - .bDescriptorType =USB_DT_CS_INTERFACE , - .bDescriptorSubtype =USB_MS_MIDI_OUT_JACK , - .bJackType =USB_MS_EMBEDDED , - .bJackID =0x03 , - .bNrInputPins =1 , - .pins = { {0x02 ,0x01 } }, - .iJack =0 , -}; - -/* MIDI 1.0 OUT (External) Jack */ -static struct usb_midi_out_jack_descriptor_1 midi2_midi1_out_jack2_desc = { - .bLength =0x09 , - .bDescriptorType =USB_DT_CS_INTERFACE , - .bDescriptorSubtype =USB_MS_MIDI_OUT_JACK , - .bJackType =USB_MS_EXTERNAL , - .bJackID =0x04 , - .bNrInputPins =1 , - .pins = { {0x01 ,0x01 } }, - .iJack =0 , -}; -/* MIDI 1.0 EP OUT */ static struct usb_endpoint_descriptor midi2_midi1_ep_out_desc = { .bLength =USB_DT_ENDPOINT_AUDIO_SIZE ,
@@ -257,8 +213,8 @@ static struct usb_ss_ep_comp_descriptor midi2_midi1_ep_out_ss_comp_desc = { .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, }; -static struct usb_ms_endpoint_descriptor_1 midi2_midi1_ep_out_class_desc = { - .bLength =0x05 , +static struct usb_ms_endpoint_descriptor_16 midi2_midi1_ep_out_class_desc = { + .bLength =0x05 ,// to be filled .bDescriptorType =USB_DT_CS_ENDPOINT , .bDescriptorSubtype =USB_MS_GENERAL , .bNumEmbMIDIJack =1 ,
@@ -278,8 +234,8 @@ static struct usb_ss_ep_comp_descriptor midi2_midi1_ep_in_ss_comp_desc = { .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, }; -static struct usb_ms_endpoint_descriptor_1 midi2_midi1_ep_in_class_desc = { - .bLength =0x05 , +static struct usb_ms_endpoint_descriptor_16 midi2_midi1_ep_in_class_desc = { + .bLength =0x05 ,// to be filled .bDescriptorType =USB_DT_CS_ENDPOINT , .bDescriptorSubtype =USB_MS_GENERAL , .bNumEmbMIDIJack =1 ,
@@ -337,25 +293,29 @@ static void *midi2_audio_descs[] = {static void *midi2_midi1_descs[] = { &midi2_midi1_if_desc, &midi2_midi1_class_desc, - &midi2_midi1_in_jack1_desc, - &midi2_midi1_in_jack2_desc, - &midi2_midi1_out_jack1_desc, - &midi2_midi1_out_jack2_desc,NULL }; -static void *midi2_midi1_ep_descs[] = { +static void *midi2_midi1_ep_out_descs[] = { &midi2_midi1_ep_out_desc, &midi2_midi1_ep_out_class_desc, +NULL +}; + +static void *midi2_midi1_ep_in_descs[] = { &midi2_midi1_ep_in_desc, &midi2_midi1_ep_in_class_desc,NULL }; -static void *midi2_midi1_ep_ss_descs[] = { +static void *midi2_midi1_ep_out_ss_descs[] = { &midi2_midi1_ep_out_desc, &midi2_midi1_ep_out_ss_comp_desc, &midi2_midi1_ep_out_class_desc, +NULL +}; + +static void *midi2_midi1_ep_in_ss_descs[] = { &midi2_midi1_ep_in_desc, &midi2_midi1_ep_in_ss_comp_desc, &midi2_midi1_ep_in_class_desc,
@@ -1197,6 +1157,11 @@ struct f_midi2_usb_config {struct usb_descriptor_header **list;unsigned int size;unsigned int alloc; + +/* MIDI 1.0 jacks */ +unsigned char jack_in, jack_out, jack_id; +struct usb_midi_in_jack_descriptor jack_ins[16 ]; +struct usb_midi_out_jack_descriptor_1 jack_outs[16 ]; };static int append_config (struct f_midi2_usb_config *config,void *d)
@@ -1231,12 +1196,61 @@ static int append_configs(struct f_midi2_usb_config *config, void **d)return 0 ; } +static int append_midi1_in_jack (struct f_midi2 *midi2, +struct f_midi2_usb_config *config, +unsigned int type) +{ +struct usb_midi_in_jack_descriptor *jack = + &config->jack_ins[config->jack_in++]; +int id = ++config->jack_id; +int err; + + jack->bLength =0x06 ; + jack->bDescriptorType =USB_DT_CS_INTERFACE ; + jack->bDescriptorSubtype =USB_MS_MIDI_IN_JACK ; + jack->bJackType = type; + jack->bJackID = id; + jack->iJack = midi2->strings[STR_GTB1 ].id;// TODO: better names? + + err =append_config (config, jack); +if (err <0 ) +return err; +return id; +} + +static int append_midi1_out_jack (struct f_midi2 *midi2, +struct f_midi2_usb_config *config, +unsigned int type,unsigned int source) +{ +struct usb_midi_out_jack_descriptor_1 *jack = + &config->jack_outs[config->jack_out++]; +int id = ++config->jack_id; +int err; + + jack->bLength =0x09 ; + jack->bDescriptorType =USB_DT_CS_INTERFACE ; + jack->bDescriptorSubtype =USB_MS_MIDI_OUT_JACK ; + jack->bJackType = type; + jack->bJackID = id; + jack->bNrInputPins =1 ; + jack->pins[0 ].baSourceID = source; + jack->pins[0 ].baSourcePin =0x01 ; + jack->iJack = midi2->strings[STR_GTB1 ].id;// TODO: better names? + + err =append_config (config, jack); +if (err <0 ) +return err; +return id; +} +static int f_midi2_create_usb_configs (struct f_midi2 *midi2,struct f_midi2_usb_config *config,int speed) { -void **midi1_eps; -int i, err; +struct f_midi2_block *blk = &midi2->midi2_eps[0 ].blks[0 ]; +void **midi1_in_eps, **midi1_out_eps; +int i, jack, total; +int err;switch (speed) {default :
@@ -1248,7 +1262,8 @@ static int f_midi2_create_usb_configs(struct f_midi2 *midi2,cpu_to_le16 (512 ); fallthrough;case USB_SPEED_FULL : - midi1_eps = midi2_midi1_ep_descs; + midi1_in_eps = midi2_midi1_ep_in_descs; + midi1_out_eps = midi2_midi1_ep_out_descs;break ;case USB_SPEED_SUPER :case USB_SPEED_SUPER_PLUS :
@@ -1257,19 +1272,85 @@ static int f_midi2_create_usb_configs(struct f_midi2 *midi2,for (i =0 ; i < midi2->num_eps; i++) midi2_midi2_ep_out_desc[i].wMaxPacketSize =cpu_to_le16 (1024 ); - midi1_eps = midi2_midi1_ep_ss_descs; + midi1_in_eps = midi2_midi1_ep_in_ss_descs; + midi1_out_eps = midi2_midi1_ep_out_ss_descs;break ; } err =append_configs (config, midi2_audio_descs);if (err <0 )return err; + +switch (blk->info.direction) { +case SNDRV_UMP_DIR_INPUT : +case SNDRV_UMP_DIR_OUTPUT : + midi2_midi1_if_desc.bNumEndpoints =1 ; +break ; +default : + midi2_midi1_if_desc.bNumEndpoints =2 ; +break ; + } + err =append_configs (config, midi2_midi1_descs);if (err <0 )return err; - err =append_configs (config, midi1_eps); -if (err <0 ) -return err; + + total =USB_DT_MS_HEADER_SIZE ; +if (blk->info.direction !=SNDRV_UMP_DIR_INPUT ) { + midi2_midi1_ep_out_class_desc.bLength = +USB_DT_MS_ENDPOINT_SIZE (blk->info.num_groups); + total += midi2_midi1_ep_out_class_desc.bLength; + midi2_midi1_ep_out_class_desc.bNumEmbMIDIJack = + blk->info.num_groups; + total += blk->info.num_groups * + (USB_DT_MIDI_IN_SIZE +USB_DT_MIDI_OUT_SIZE (1 )); +for (i =0 ; i < blk->info.num_groups; i++) { + jack =append_midi1_in_jack (midi2, config, +USB_MS_EMBEDDED ); +if (jack <0 ) +return jack; + midi2_midi1_ep_out_class_desc.baAssocJackID[i] = jack; + jack =append_midi1_out_jack (midi2, config, +USB_MS_EXTERNAL , jack); +if (jack <0 ) +return jack; + } + } + +if (blk->info.direction !=SNDRV_UMP_DIR_OUTPUT ) { + midi2_midi1_ep_in_class_desc.bLength = +USB_DT_MS_ENDPOINT_SIZE (blk->info.num_groups); + total += midi2_midi1_ep_in_class_desc.bLength; + midi2_midi1_ep_in_class_desc.bNumEmbMIDIJack = + blk->info.num_groups; + total += blk->info.num_groups * + (USB_DT_MIDI_IN_SIZE +USB_DT_MIDI_OUT_SIZE (1 )); +for (i =0 ; i < blk->info.num_groups; i++) { + jack =append_midi1_in_jack (midi2, config, +USB_MS_EXTERNAL ); +if (jack <0 ) +return jack; + jack =append_midi1_out_jack (midi2, config, +USB_MS_EMBEDDED , jack); +if (jack <0 ) +return jack; + midi2_midi1_ep_in_class_desc.baAssocJackID[i] = jack; + } + } + + midi2_midi1_class_desc.wTotalLength =cpu_to_le16 (total); + +if (blk->info.direction !=SNDRV_UMP_DIR_INPUT ) { + err =append_configs (config, midi1_out_eps); +if (err <0 ) +return err; + } +if (blk->info.direction !=SNDRV_UMP_DIR_OUTPUT ) { + err =append_configs (config, midi1_in_eps); +if (err <0 ) +return err; + } + err =append_configs (config, midi2_midi2_descs);if (err <0 )return err;
@@ -1421,14 +1502,19 @@ static int f_midi2_bind(struct usb_configuration *c, struct usb_function *f) midi2_audio_class_desc.baInterfaceNr[0 ] = status;/* allocate instance-specific endpoints */ - status =f_midi2_init_ep (midi2,NULL , &midi2->midi1_ep_in, - &midi2_midi1_ep_in_desc,0 ,NULL ); -if (status) - goto fail; - status =f_midi2_init_ep (midi2,NULL , &midi2->midi1_ep_out, - &midi2_midi1_ep_out_desc,0 ,NULL ); -if (status) - goto fail; +if (midi2->midi2_eps[0 ].blks[0 ].info.direction !=SNDRV_UMP_DIR_OUTPUT ) { + status =f_midi2_init_ep (midi2,NULL , &midi2->midi1_ep_in, + &midi2_midi1_ep_in_desc,0 ,NULL ); +if (status) + goto fail; + } + +if (midi2->midi2_eps[0 ].blks[0 ].info.direction !=SNDRV_UMP_DIR_INPUT ) { + status =f_midi2_init_ep (midi2,NULL , &midi2->midi1_ep_out, + &midi2_midi1_ep_out_desc,0 ,NULL ); +if (status) + goto fail; + }for (i =0 ; i < midi2->num_eps; i++) { status =f_midi2_init_midi2_ep_in (midi2, i);
Signature · 1 line
2.35.3
PATCH 4/7 usb: gadget: midi2: MIDI 1.0 interface (altset 0) support
In reply to Takashi IwaiThis patch extends MIDI 2.0 function driver to add more proper support for MIDI 1.0 interface. Before this patch, the driver only exposes the USB descriptor of a MIDI 1.0 interface in altset 0 while no actual I/O is running for it. This patch enables the actual I/O for the altset 0; the backend UMP rawmidi is translated from/to the MIDI 1.0 USB commands.
For converting to USB MIDI 1.0 data protocol, a helper function is copied from the existing f_midi driver, in addition to a few other UMP Core helper functions. For the MIDI 1.0 OUT (that is, input for gadget), the incoming USB MIDI 1.0 packet is translated to UMP packets via UMP Core helper, and tossed to the attached UMP rawmidi. It's a relatively straightforward. OTOH, for MIDI 1.0 IN (i.e. output for gadget), it's a bit more complex: we need to convert a source UMP packet once to the standard MIDI 1.0 byte stream, and convert it again to USB MIDI 1.0 packets, then send them out.
--- drivers/usb/gadget/function/f_midi2.c | 494 +++++++++++++++++++++++--- 1 file changed, 452 insertions(+), 42 deletions(-)
drivers/usb/gadget/function/f_midi2.c +452 -42
@@ -11,6 +11,7 @@#include <sound/core.h> #include <sound/ump.h> #include <sound/ump_msg.h> +#include <sound/ump_convert.h> #include <linux/usb/ch9.h> #include <linux/usb/gadget.h>
@@ -50,6 +51,27 @@ struct f_midi2_block {unsigned int string_id;/* assigned string id */ }; +/* Temporary buffer for altset 0 MIDI 1.0 handling */ +struct f_midi2_midi1_port { +unsigned int pending;/* pending bytes on the input buffer */ +u8 buf[32 ];/* raw MIDI 1.0 byte input */ +u8 state;/* running status */ +u8 data[2 ];/* rendered USB MIDI 1.0 packet data */ +}; + +/* MIDI 1.0 message states */ +enum { +STATE_INITIAL =0 ,/* pseudo state */ +STATE_1PARAM , +STATE_2PARAM_1 , +STATE_2PARAM_2 , +STATE_SYSEX_0 , +STATE_SYSEX_1 , +STATE_SYSEX_2 , +STATE_REAL_TIME , +STATE_FINISHED ,/* pseudo state */ +}; +/* Resources for UMP Endpoint */ struct f_midi2_ep {struct snd_ump_endpoint *ump;/* assigned UMP EP */
@@ -89,6 +111,11 @@ struct f_midi2 {struct f_midi2_usb_ep midi1_ep_in;struct f_midi2_usb_ep midi1_ep_out; +/* conversion for MIDI 1.0 EP-in */ +struct f_midi2_midi1_port midi1_port[MAX_CABLES ]; +/* conversion for MIDI 1.0 EP-out */ +struct ump_cvt_to_ump midi1_ump_cvt; +int midi_if;/* USB MIDI interface number */ int operation_mode;/* current operation mode */
@@ -707,12 +734,353 @@ static void f_midi2_ep_in_complete(struct usb_ep *usb_ep,process_ump_transmit (ep); } +/* +* MIDI1 (altset 0) USB request handling +*/ + +/* process one MIDI byte -- copied from f_midi.c +* +* fill the packet or request if needed +* returns true if the request became empty (queued) +*/ +static bool process_midi1_byte (struct f_midi2 *midi2,u8 cable,u8 b, +struct usb_request **req_p) +{ +struct f_midi2_midi1_port *port = &midi2->midi1_port[cable]; +u8 p[4 ] = { cable <<4 ,0 ,0 ,0 }; +int next_state =STATE_INITIAL ; +struct usb_request *req = *req_p; + +switch (b) { + case 0xf8 ... 0xff: +/* System Real-Time Messages */ + p[0 ] |=0x0f ; + p[1 ] = b; + next_state = port->state; + port->state =STATE_REAL_TIME ; +break ; + +case 0xf7 : +/* End of SysEx */ +switch (port->state) { +case STATE_SYSEX_0 : + p[0 ] |=0x05 ; + p[1 ] =0xf7 ; + next_state =STATE_FINISHED ; +break ; +case STATE_SYSEX_1 : + p[0 ] |=0x06 ; + p[1 ] = port->data[0 ]; + p[2 ] =0xf7 ; + next_state =STATE_FINISHED ; +break ; +case STATE_SYSEX_2 : + p[0 ] |=0x07 ; + p[1 ] = port->data[0 ]; + p[2 ] = port->data[1 ]; + p[3 ] =0xf7 ; + next_state =STATE_FINISHED ; +break ; +default : +/* Ignore byte */ + next_state = port->state; + port->state =STATE_INITIAL ; + } +break ; + + case 0xf0 ... 0xf6: +/* System Common Messages */ + port->data[0 ] = port->data[1 ] =0 ; + port->state =STATE_INITIAL ; +switch (b) { +case 0xf0 : + port->data[0 ] = b; + port->data[1 ] =0 ; + next_state =STATE_SYSEX_1 ; +break ; +case 0xf1 : +case 0xf3 : + port->data[0 ] = b; + next_state =STATE_1PARAM ; +break ; +case 0xf2 : + port->data[0 ] = b; + next_state =STATE_2PARAM_1 ; +break ; +case 0xf4 : +case 0xf5 : + next_state =STATE_INITIAL ; +break ; +case 0xf6 : + p[0 ] |=0x05 ; + p[1 ] =0xf6 ; + next_state =STATE_FINISHED ; +break ; + } +break ; + + case 0x80 ... 0xef: +/* +* Channel Voice Messages, Channel Mode Messages +* and Control Change Messages. +*/ + port->data[0 ] = b; + port->data[1 ] =0 ; + port->state =STATE_INITIAL ; +if (b >=0xc0 && b <=0xdf ) + next_state =STATE_1PARAM ; +else + next_state =STATE_2PARAM_1 ; +break ; + + case 0x00 ... 0x7f: +/* Message parameters */ +switch (port->state) { +case STATE_1PARAM : +if (port->data[0 ] <0xf0 ) + p[0 ] |= port->data[0 ] >>4 ; +else + p[0 ] |=0x02 ; + + p[1 ] = port->data[0 ]; + p[2 ] = b; +/* This is to allow Running State Messages */ + next_state =STATE_1PARAM ; +break ; +case STATE_2PARAM_1 : + port->data[1 ] = b; + next_state =STATE_2PARAM_2 ; +break ; +case STATE_2PARAM_2 : +if (port->data[0 ] <0xf0 ) + p[0 ] |= port->data[0 ] >>4 ; +else + p[0 ] |=0x03 ; + + p[1 ] = port->data[0 ]; + p[2 ] = port->data[1 ]; + p[3 ] = b; +/* This is to allow Running State Messages */ + next_state =STATE_2PARAM_1 ; +break ; +case STATE_SYSEX_0 : + port->data[0 ] = b; + next_state =STATE_SYSEX_1 ; +break ; +case STATE_SYSEX_1 : + port->data[1 ] = b; + next_state =STATE_SYSEX_2 ; +break ; +case STATE_SYSEX_2 : + p[0 ] |=0x04 ; + p[1 ] = port->data[0 ]; + p[2 ] = port->data[1 ]; + p[3 ] = b; + next_state =STATE_SYSEX_0 ; +break ; + } +break ; + } + +/* States where we have to write into the USB request */ +if (next_state ==STATE_FINISHED || + port->state ==STATE_SYSEX_2 || + port->state ==STATE_1PARAM || + port->state ==STATE_2PARAM_2 || + port->state ==STATE_REAL_TIME ) { +memcpy (req->buf + req->length, p,sizeof (p)); + req->length +=sizeof (p); + +if (next_state ==STATE_FINISHED ) { + next_state =STATE_INITIAL ; + port->data[0 ] = port->data[1 ] =0 ; + } + +if (midi2->info.req_buf_size - req->length <=4 ) { +queue_request_ep_raw (req); + *req_p =NULL ; +return true; + } + } + + port->state = next_state; +return false; +} + +/* process all pending MIDI bytes in the internal buffer; +* returns true if the request gets empty +* returns false if all have been processed +*/ +static bool process_midi1_pending_buf (struct f_midi2 *midi2, +struct usb_request **req_p) +{ +unsigned int cable, c; + +for (cable =0 ; cable < midi2->midi2_eps[0 ].blks[0 ].info.num_groups; + cable++) { +struct f_midi2_midi1_port *port = &midi2->midi1_port[cable]; + +if (!port->pending) +continue ; +for (c =0 ; c < port->pending; c++) { +if (process_midi1_byte (midi2, cable, port->buf[c], + req_p)) { + port->pending -= c; +if (port->pending) +memmove (port->buf, port->buf + c, + port->pending); +return true; + } + } + port->pending =0 ; + } + +return false; +} + +/* fill the MIDI bytes onto the temporary buffer +*/ +static void fill_midi1_pending_buf (struct f_midi2 *midi2,u8 cable,u8 *buf, +unsigned int size) +{ +struct f_midi2_midi1_port *port = &midi2->midi1_port[cable]; + +if (port->pending + size >sizeof (port->buf)) +return ; +memcpy (port->buf + port->pending, buf, size); + port->pending += size; +} + +/* try to process data given from the associated UMP stream */ +static void process_midi1_transmit (struct f_midi2 *midi2) +{ +struct f_midi2_usb_ep *usb_ep = &midi2->midi1_ep_in; +struct f_midi2_ep *ep = &midi2->midi2_eps[0 ]; +struct usb_request *req =NULL ; +/* 12 is the largest outcome (4 MIDI1 cmds) for a single UMP packet */ +unsigned char outbuf[12 ]; +unsigned char group; +int len, size, cable; +u32 ump; + +if (!usb_ep->usb_ep || !usb_ep->usb_ep->enabled) +return ; + +for (;;) { +if (!req) { + req =get_empty_request (usb_ep); +if (!req) +break ; + } + +if (process_midi1_pending_buf (midi2, &req)) +continue ; + + len =snd_ump_transmit (ep->ump, &ump,4 ); +if (len <=0 ) +break ; +if (snd_ump_receive_ump_val (ep->ump, ump) <=0 ) +continue ; + size =snd_ump_convert_from_ump (ep->ump->input_buf, outbuf, + &group); +if (size <=0 ) +continue ; + cable = group - ep->blks[0 ].info.first_group; +if (cable <0 || cable >= ep->blks[0 ].info.num_groups) +continue ; +fill_midi1_pending_buf (midi2, cable, outbuf, size); + } + +if (req) { +if (req->length) +queue_request_ep_raw (req); +else +put_empty_request (req); + } +} + +/* complete handler for MIDI1 EP-in requests */ +static void f_midi2_midi1_ep_in_complete (struct usb_ep *usb_ep, +struct usb_request *req) +{ +struct f_midi2_req_ctx *ctx = req->context; +struct f_midi2 *midi2 = ctx->usb_ep->card; +int status = req->status; + +put_empty_request (req); + +if (status) { +DBG (midi2,"%s complete error %d: %d/%d\n" , + usb_ep->name, status, req->actual, req->length); +return ; + } + +process_midi1_transmit (midi2); +} + +/* complete handler for MIDI1 EP-out requests */ +static void f_midi2_midi1_ep_out_complete (struct usb_ep *usb_ep, +struct usb_request *req) +{ +struct f_midi2_req_ctx *ctx = req->context; +struct f_midi2 *midi2 = ctx->usb_ep->card; +struct f_midi2_ep *ep = &midi2->midi2_eps[0 ]; +struct ump_cvt_to_ump *cvt = &midi2->midi1_ump_cvt; +static const u8 midi1_packet_bytes[16 ] = { +0 ,0 ,2 ,3 ,3 ,1 ,2 ,3 ,3 ,3 ,3 ,3 ,2 ,2 ,3 ,1 + }; +unsigned int group, bytes, c, len; +int status = req->status; +const u8 *buf = req->buf; + +if (status) { +DBG (midi2,"%s complete error %d: %d/%d\n" , + usb_ep->name, status, req->actual, req->length); + goto error; + } + + len = req->actual >>2 ; +for (; len; len--, buf +=4 ) { + group = *buf >>4 ; +if (group >= ep->blks[0 ].info.num_groups) +continue ; + group += ep->blks[0 ].info.first_group; + bytes = midi1_packet_bytes[*buf &0x0f ]; +for (c =0 ; c < bytes; c++) { +snd_ump_convert_to_ump (cvt, group, ep->info.protocol, + buf[c +1 ]); +if (cvt->ump_bytes) { +snd_ump_receive (ep->ump, cvt->ump, + cvt->ump_bytes); + cvt->ump_bytes =0 ; + } + } + } + +if (midi2->operation_mode !=MIDI_OP_MODE_MIDI1 ) + goto error; + +if (queue_request_ep_raw (req)) + goto error; +return ; + + error: +put_empty_request (req); +} + +/* +* Common EP handling helpers +*/ +/* Start MIDI EP */ static int f_midi2_start_ep (struct f_midi2_usb_ep *usb_ep,struct usb_function *fn) {int err; +if (!usb_ep->usb_ep) +return 0 ; +usb_ep_disable (usb_ep->usb_ep); err =config_ep_by_speed (usb_ep->card->gadget, fn, usb_ep->usb_ep);if (err)
@@ -725,7 +1093,7 @@ static void f_midi2_drop_reqs(struct f_midi2_usb_ep *usb_ep) {int i; -if (!usb_ep->num_reqs) +if (!usb_ep->usb_ep || !usb_ep->num_reqs)return ;for (i =0 ; i < usb_ep->num_reqs; i++) {
@@ -742,6 +1110,8 @@ static int f_midi2_alloc_ep_reqs(struct f_midi2_usb_ep *usb_ep)struct f_midi2 *midi2 = usb_ep->card;int i; +if (!usb_ep->usb_ep) +return 0 ;if (!usb_ep->reqs)return -EINVAL ;
@@ -774,7 +1144,7 @@ static void f_midi2_free_ep_reqs(struct f_midi2_usb_ep *usb_ep)/* Initialize EP */ static int f_midi2_init_ep (struct f_midi2 *midi2,struct f_midi2_ep *ep,struct f_midi2_usb_ep *usb_ep, -void *desc,int num_reqs, +void *desc,void (*complete)(struct usb_ep *usb_ep,struct usb_request *req)) {
@@ -787,17 +1157,15 @@ static int f_midi2_init_ep(struct f_midi2 *midi2, struct f_midi2_ep *ep,return -ENODEV ; usb_ep->complete = complete; -if (num_reqs) { - usb_ep->reqs =kcalloc (num_reqs,sizeof (*usb_ep->reqs), -GFP_KERNEL ); -if (!usb_ep->reqs) -return -ENOMEM ; -for (i =0 ; i < num_reqs; i++) { - usb_ep->reqs[i].index = i; - usb_ep->reqs[i].usb_ep = usb_ep; -set_bit (i, &usb_ep->free_reqs); - usb_ep->num_reqs++; - } + usb_ep->reqs =kcalloc (midi2->info.num_reqs,sizeof (*usb_ep->reqs), +GFP_KERNEL ); +if (!usb_ep->reqs) +return -ENOMEM ; +for (i =0 ; i < midi2->info.num_reqs; i++) { + usb_ep->reqs[i].index = i; + usb_ep->reqs[i].usb_ep = usb_ep; +set_bit (i, &usb_ep->free_reqs); + usb_ep->num_reqs++; }return 0 ;
@@ -821,6 +1189,9 @@ static void f_midi2_queue_out_reqs(struct f_midi2_usb_ep *usb_ep) {int i, err; +if (!usb_ep->usb_ep) +return ; +for (i =0 ; i < usb_ep->num_reqs; i++) {if (!test_bit (i, &usb_ep->free_reqs) || !usb_ep->reqs[i].req)continue ;
@@ -836,6 +1207,41 @@ static void f_midi2_queue_out_reqs(struct f_midi2_usb_ep *usb_ep)* Gadget Function callbacks */ +/* stop both IN and OUT EPs */ +static void f_midi2_stop_eps (struct f_midi2_usb_ep *ep_in, +struct f_midi2_usb_ep *ep_out) +{ +f_midi2_drop_reqs (ep_in); +f_midi2_drop_reqs (ep_out); +f_midi2_free_ep_reqs (ep_in); +f_midi2_free_ep_reqs (ep_out); +} + +/* start/queue both IN and OUT EPs */ +static int f_midi2_start_eps (struct f_midi2_usb_ep *ep_in, +struct f_midi2_usb_ep *ep_out, +struct usb_function *fn) +{ +int err; + + err =f_midi2_start_ep (ep_in, fn); +if (err) +return err; + err =f_midi2_start_ep (ep_out, fn); +if (err) +return err; + + err =f_midi2_alloc_ep_reqs (ep_in); +if (err) +return err; + err =f_midi2_alloc_ep_reqs (ep_out); +if (err) +return err; + +f_midi2_queue_out_reqs (ep_out); +return 0 ; +} +/* gadget function set_alt callback */ static int f_midi2_set_alt (struct usb_function *fn,unsigned int intf,unsigned int alt)
@@ -859,35 +1265,28 @@ static int f_midi2_set_alt(struct usb_function *fn, unsigned int intf, midi2->operation_mode = op_mode; +if (op_mode !=MIDI_OP_MODE_MIDI1 ) +f_midi2_stop_eps (&midi2->midi1_ep_in, &midi2->midi1_ep_out); +if (op_mode !=MIDI_OP_MODE_MIDI2 ) {for (i =0 ; i < midi2->num_eps; i++) { ep = &midi2->midi2_eps[i]; -f_midi2_drop_reqs (&ep->ep_in); -f_midi2_drop_reqs (&ep->ep_out); -f_midi2_free_ep_reqs (&ep->ep_in); -f_midi2_free_ep_reqs (&ep->ep_out); +f_midi2_stop_eps (&ep->ep_in, &ep->ep_out); } -return 0 ; } -for (i =0 ; i < midi2->num_eps; i++) { - ep = &midi2->midi2_eps[i]; +if (op_mode ==MIDI_OP_MODE_MIDI1 ) +return f_midi2_start_eps (&midi2->midi1_ep_in, + &midi2->midi1_ep_out, fn); - err =f_midi2_start_ep (&ep->ep_in, fn); -if (err) -return err; - err =f_midi2_start_ep (&ep->ep_out, fn); -if (err) -return err; +if (op_mode ==MIDI_OP_MODE_MIDI2 ) { +for (i =0 ; i < midi2->num_eps; i++) { + ep = &midi2->midi2_eps[i]; - err =f_midi2_alloc_ep_reqs (&ep->ep_in); -if (err) -return err; - err =f_midi2_alloc_ep_reqs (&ep->ep_out); -if (err) -return err; - -f_midi2_queue_out_reqs (&ep->ep_out); + err =f_midi2_start_eps (&ep->ep_in, &ep->ep_out, fn); +if (err) +return err; + } }return 0 ;
@@ -1026,9 +1425,18 @@ static void f_midi2_ump_close(struct snd_ump_endpoint *ump, int dir)static void f_midi2_ump_trigger (struct snd_ump_endpoint *ump,int dir,int up) {struct f_midi2_ep *ep = ump->private_data; +struct f_midi2 *midi2 = ep->card; -if (up && dir ==SNDRV_RAWMIDI_STREAM_OUTPUT ) -process_ump_transmit (ep); +if (up && dir ==SNDRV_RAWMIDI_STREAM_OUTPUT ) { +switch (midi2->operation_mode) { +case MIDI_OP_MODE_MIDI1 : +process_midi1_transmit (midi2); +break ; +case MIDI_OP_MODE_MIDI2 : +process_ump_transmit (ep); +break ; + } + } }static void f_midi2_ump_drain (struct snd_ump_endpoint *ump,int dir)
@@ -1160,8 +1568,8 @@ struct f_midi2_usb_config {/* MIDI 1.0 jacks */ unsigned char jack_in, jack_out, jack_id; -struct usb_midi_in_jack_descriptor jack_ins[16 ]; -struct usb_midi_out_jack_descriptor_1 jack_outs[16 ]; +struct usb_midi_in_jack_descriptor jack_ins[MAX_CABLES ]; +struct usb_midi_out_jack_descriptor_1 jack_outs[MAX_CABLES ]; };static int append_config (struct f_midi2_usb_config *config,void *d)
@@ -1422,7 +1830,7 @@ static int f_midi2_init_midi2_ep_in(struct f_midi2 *midi2, int index)fill_midi2_class_desc (ep, &midi2_midi2_ep_in_class_desc[index]);return f_midi2_init_ep (midi2, ep, &ep->ep_in, desc, - midi2->info.num_reqs, f_midi2_ep_in_complete); + f_midi2_ep_in_complete); }/* initialize MIDI2 EP-out */
@@ -1439,7 +1847,7 @@ static int f_midi2_init_midi2_ep_out(struct f_midi2 *midi2, int index)fill_midi2_class_desc (ep, &midi2_midi2_ep_out_class_desc[index]);return f_midi2_init_ep (midi2, ep, &ep->ep_out, desc, - midi2->info.num_reqs, f_midi2_ep_out_complete); + f_midi2_ep_out_complete); }/* gadget function bind callback */
@@ -1504,14 +1912,16 @@ static int f_midi2_bind(struct usb_configuration *c, struct usb_function *f)/* allocate instance-specific endpoints */ if (midi2->midi2_eps[0 ].blks[0 ].info.direction !=SNDRV_UMP_DIR_OUTPUT ) { status =f_midi2_init_ep (midi2,NULL , &midi2->midi1_ep_in, - &midi2_midi1_ep_in_desc,0 ,NULL ); + &midi2_midi1_ep_in_desc, + f_midi2_midi1_ep_in_complete);if (status) goto fail; }if (midi2->midi2_eps[0 ].blks[0 ].info.direction !=SNDRV_UMP_DIR_INPUT ) { status =f_midi2_init_ep (midi2,NULL , &midi2->midi1_ep_out, - &midi2_midi1_ep_out_desc,0 ,NULL ); + &midi2_midi1_ep_out_desc, + f_midi2_midi1_ep_out_complete);if (status) goto fail; }
Signature · 1 line
2.35.3
PATCH 5/7 usb: gadget: midi2: Add testing documentation
In reply to Takashi IwaiDescribing brief testing of the new MIDI 2.0 function driver.
--- Documentation/usb/gadget-testing.rst | 141 +++++++++++++++++++++++++++ 1 file changed, 141 insertions(+)
Documentation/usb/gadget-testing.rst +141 -0
@@ -27,6 +27,7 @@ provided by gadgets. 18. UVC function 19. PRINTER function 20. UAC1 function (new API) + 21. MIDI2 function 1. ACM function
@@ -965,3 +966,143 @@ e.g.:: $ arecord -f dat -t wav -D hw:CARD=UAC1Gadget,DEV=0 | \ aplay -D default:CARD=OdroidU3 + + +21. MIDI2 function +================== + +The function is provided by usb_f_midi2.ko module. +It will create a virtual ALSA card containing a UMP rawmidi device +where the UMP packet is looped back. In addition, a legacy rawmidi +device is created. The UMP rawmidi is bound with ALSA sequencer +clients, too. + +Function-specific configfs interface +------------------------------------ + +The function name to use when creating the function directory is "midi2". +The midi2 function provides these attributes in its function directory +as the card top-level information: + + ============= ================================================= + process_ump Bool flag to process UMP Stream messages (0 or 1) + static_block Bool flag for static blocks (0 or 1) + iface_name Optional interface name string + ============= ================================================= + +The directory contains a subdirectory "ep.0", and this provides the +attributes for a UMP Endpoint (which is a pair of USB MIDI Endpoints): + + ============= ================================================= + protocol_caps MIDI protocol capabilities; + 1: MIDI 1.0, 2: MIDI 2.0, or 3: both protocols + protocol Default MIDI protocol (either 1 or 2) + ep_name UMP Endpoint name string + product_id Product ID string + manufacturer Manufacture ID number (24 bit) + family Device family ID number (16 bit) + model Device model ID number (16 bit) + sw_revision Software revision (32 bit) + ============= ================================================= + +Each Endpoint subdirectory contains a subdirectory "block.0", which +represents the Function Block for Block 0 information. +Its attributes are: + + =============== =============================================== + name Function Block name string + direction Direction of this FB + 1: input, 2: output, or 3: bidirectional + first_group The first UMP Group number (0-15) + num_groups The number of groups in this FB (1-16) + ui_hint UI-hint of this FB + 0: unknown, 1: receiver, 2: sender, 3: both + midi_ci_verison Supported MIDI-CI version number (8 bit) + is_midi1 Legacy MIDI 1.0 device (0-2) + 0: MIDI 2.0 device, + 1: MIDI 1.0 without restriction, or + 2: MIDI 1.0 with low speed + sysex8_streams Max number of SysEx8 streams (8 bit) + active Bool flag for FB activity (0 or 1) + =============== =============================================== + +If multiple Function Blocks are required, you can add more Function +Blocks by creating subdirectories "block.<num>" with the corresponding +Function Block number (1, 2, ....). The FB subdirectories can be +dynamically removed, too. Note that the Function Block numbers must be +continuous. + +Similarly, if you multiple UMP Endpoints are required, you can add +more Endpoints by creating subdirectories "ep.<num>". The number must +be continuous. + +For emulating the old MIDI 2.0 device without UMP v1.1 support, pass 0 +to `process_ump` flag. Then the whole UMP v1.1 requests are ignored. + +Testing the MIDI2 function +-------------------------- + +On the device: run the gadget, and running:: + + $ cat /proc/asound/cards + +will show a new sound card containing a MIDI2 device. + +OTOH, on the host:: + + $ cat /proc/asound/cards + +will show a new sound card containing either MIDI1 or MIDI2 device, +depending on the USB audio driver configuration. + +On both, when ALSA sequencer is enabled on the host, you can find the +UMP MIDI client such as "MIDI 2.0 Gadget". + +As the driver simply loops back the data, there is no need for a real +device just for testing. + +For testing a MIDI input from the gadget to the host (e.g. emulating a +MIDI keyboard), you can send a MIDI stream like the following. + +On the gadget:: + + $ aconnect -o + .... + client 20: 'MIDI 2.0 Gadget' [type=kernel,card=1] + 0 'MIDI 2.0 ' + 1 'Group 1 (MIDI 2.0 Gadget I/O)' + $ aplaymidi -p 20:1 to_host.mid + +On the host:: + + $ aconnect -i + .... + client 24: 'MIDI 2.0 Gadget' [type=kernel,card=2] + 0 'MIDI 2.0 ' + 1 'Group 1 (MIDI 2.0 Gadget I/O)' + $ arecordmidi -p 24:1 from_gadget.mid + +If you have a UMP-capable application, you can use the UMP port to +send/receive the raw UMP packets, too. For example, aseqdump program +with UMP support can receive from UMP port. On the host:: + + $ aseqdump -u 2 -p 24:1 + Waiting for data. Press Ctrl+C to end. + Source Group Event Ch Data + 24:1 Group 0, Program change 0, program 0, Bank select 0:0 + 24:1 Group 0, Channel pressure 0, value 0x80000000 + +For testing a MIDI output to the gadget to the host (e.g. emulating a +MIDI synth), it'll be just other way round. + +On the gadget:: + + $ arecordmidi -p 20:1 from_host.mid + +On the host:: + + $ aplaymidi -p 24:1 to_gadget.mid + +The access to MIDI 1.0 on altset 0 on the host is supported, and it's +translated from/to UMP packets on the gadget. It's bound to only +Function Block 0.
Signature · 1 line
2.35.3
PATCH 6/7 usb: gadget: midi2: Add "Operation Mode" control
In reply to Takashi IwaiAdd a new ALSA control element to watch the current operation mode (MIDI 1.0 or MIDI 2.0). It's a read-only control that reflects the current value of altsetting, and 0 means unused, 1 for MIDI 1.0 (altset 0) and 2 for MIDI 2.0 (altset 1).
--- Documentation/usb/gadget-testing.rst | 11 +++++++++ drivers/usb/gadget/function/f_midi2.c | 35 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)
Documentation/usb/gadget-testing.rst +11 -0
@@ -1106,3 +1106,14 @@ On the host:: The access to MIDI 1.0 on altset 0 on the host is supported, and it's translated from/to UMP packets on the gadget. It's bound to only Function Block 0. + +The current operation mode can be observed in ALSA control element +"Operation Mode" for SND_CTL_IFACE_RAWMIDI. For example:: + + $ amixer -c1 contents + numid=1,iface=RAWMIDI,name='Operation Mode' + ; type=INTEGER,access=r--v----,values=1,min=0,max=2,step=0 + : values=2 + +where 0 = unused, 1 = MIDI 1.0 (altset 0), 2 = MIDI 2.0 (altset 1). +The example above shows it's running in 2, i.e. MIDI 2.0.
drivers/usb/gadget/function/f_midi2.c +35 -0
@@ -9,6 +9,7 @@#include <linux/slab.h> #include <sound/core.h> +#include <sound/control.h> #include <sound/ump.h> #include <sound/ump_msg.h> #include <sound/ump_convert.h>
@@ -1450,6 +1451,36 @@ static const struct snd_ump_ops f_midi2_ump_ops = { .drain = f_midi2_ump_drain, }; +/* +* "Operation Mode" control element +*/ +static int f_midi2_operation_mode_info (struct snd_kcontrol *kcontrol, +struct snd_ctl_elem_info *uinfo) +{ + uinfo->type =SNDRV_CTL_ELEM_TYPE_INTEGER ; + uinfo->count =1 ; + uinfo->value.integer.min =MIDI_OP_MODE_UNSET ; + uinfo->value.integer.max =MIDI_OP_MODE_MIDI2 ; +return 0 ; +} + +static int f_midi2_operation_mode_get (struct snd_kcontrol *kcontrol, +struct snd_ctl_elem_value *ucontrol) +{ +struct f_midi2 *midi2 =snd_kcontrol_chip (kcontrol); + + ucontrol->value.integer.value[0 ] = midi2->operation_mode; +return 0 ; +} + +static const struct snd_kcontrol_new operation_mode_ctl = { + .iface =SNDRV_CTL_ELEM_IFACE_RAWMIDI , + .name ="Operation Mode" , + .access =SNDRV_CTL_ELEM_ACCESS_READ |SNDRV_CTL_ELEM_ACCESS_VOLATILE , + .info = f_midi2_operation_mode_info, + .get = f_midi2_operation_mode_get, +}; +/* * ALSA UMP instance creation / deletion */
@@ -1547,6 +1578,10 @@ static int f_midi2_create_card(struct f_midi2 *midi2) id++; } + err =snd_ctl_add (card,snd_ctl_new1 (&operation_mode_ctl, midi2)); +if (err <0 ) + goto error; + err =snd_card_register (card);if (err <0 ) goto error;
Signature · 1 line
2.35.3
PATCH 7/7 usb: gadget: midi2: More flexible MIDI 1.0 configuration
In reply to Takashi IwaiThis patch allows users to set up MIDI 1.0 ports more flexibly. Namely, instead of the fixed mapping only from FB 0, now multiple block definitions are applied to build up the MIDI 1.0 mapping.
The each block config has midi1_first_group and midi1_num_groups attributes, and those specify which Groups are used for MIDI 1.0. Those fields must be within the UMP Groups defined in the block itself.
--- .../ABI/testing/configfs-usb-gadget-midi2 | 24 +-- Documentation/usb/gadget-testing.rst | 34 ++-- drivers/usb/gadget/function/f_midi2.c | 154 ++++++++++++++---- drivers/usb/gadget/function/u_midi2.h | 2 + 4 files changed, 153 insertions(+), 61 deletions(-)
Documentation/ABI/testing/configfs-usb-gadget-midi2 +13 -11
@@ -39,14 +39,16 @@ Description: The attributes: - =============== =============================================== - name Function Block name string - direction 1: input, 2: output, 3: bidirectional - first_group The first UMP Group number (0-15) - num_groups The number of groups in this FB (1-16) - ui_hint 0: unknown, 1: receiver, 2: sender, 3: both - midi_ci_verison Supported MIDI-CI version number (8 bit) - is_midi1 Legacy MIDI 1.0 device (0, 1 or 2) - sysex8_streams Max number of SysEx8 streams (8 bit) - active Active FB flag (0 or 1) - =============== =============================================== + ================= ============================================== + name Function Block name string + direction 1: input, 2: output, 3: bidirectional + first_group The first UMP Group number (0-15) + num_groups The number of groups in this FB (1-16) + midi1_first_group The first UMP Group number for MIDI 1.0 (0-15) + midi1_num_groups The number of groups for MIDI 1.0 (0-16) + ui_hint 0: unknown, 1: receiver, 2: sender, 3: both + midi_ci_verison Supported MIDI-CI version number (8 bit) + is_midi1 Legacy MIDI 1.0 device (0, 1 or 2) + sysex8_streams Max number of SysEx8 streams (8 bit) + active Active FB flag (0 or 1) + ================= ==============================================
Documentation/usb/gadget-testing.rst +18 -16
@@ -1009,22 +1009,24 @@ Each Endpoint subdirectory contains a subdirectory "block.0", which represents the Function Block for Block 0 information. Its attributes are: - =============== =============================================== - name Function Block name string - direction Direction of this FB - 1: input, 2: output, or 3: bidirectional - first_group The first UMP Group number (0-15) - num_groups The number of groups in this FB (1-16) - ui_hint UI-hint of this FB - 0: unknown, 1: receiver, 2: sender, 3: both - midi_ci_verison Supported MIDI-CI version number (8 bit) - is_midi1 Legacy MIDI 1.0 device (0-2) - 0: MIDI 2.0 device, - 1: MIDI 1.0 without restriction, or - 2: MIDI 1.0 with low speed - sysex8_streams Max number of SysEx8 streams (8 bit) - active Bool flag for FB activity (0 or 1) - =============== =============================================== + ================= =============================================== + name Function Block name string + direction Direction of this FB + 1: input, 2: output, or 3: bidirectional + first_group The first UMP Group number (0-15) + num_groups The number of groups in this FB (1-16) + midi1_first_group The first UMP Group number for MIDI 1.0 (0-15) + midi1_num_groups The number of groups for MIDI 1.0 (0-16) + ui_hint UI-hint of this FB + 0: unknown, 1: receiver, 2: sender, 3: both + midi_ci_verison Supported MIDI-CI version number (8 bit) + is_midi1 Legacy MIDI 1.0 device (0-2) + 0: MIDI 2.0 device, + 1: MIDI 1.0 without restriction, or + 2: MIDI 1.0 with low speed + sysex8_streams Max number of SysEx8 streams (8 bit) + active Bool flag for FB activity (0 or 1) + ================= =============================================== If multiple Function Blocks are required, you can add more Function Blocks by creating subdirectories "block.<num>" with the corresponding
drivers/usb/gadget/function/f_midi2.c +120 -34
329 lines not on this page · raw message
drivers/usb/gadget/function/u_midi2.h +2 -0
@@ -18,6 +18,8 @@ struct f_midi2_block_info {unsigned int direction;/* FB direction: 1-3 */ unsigned int first_group;/* first UMP group: 0-15 */ unsigned int num_groups;/* number of UMP groups: 1-16 */ +unsigned int midi1_first_group;/* first UMP group for MIDI 1.0 */ +unsigned int midi1_num_groups;/* number of UMP groups for MIDI 1.0 */ unsigned int ui_hint;/* UI-hint: 0-3 */ unsigned int midi_ci_version;/* MIDI-CI version: 0-255 */ unsigned int sysex8_streams;/* number of sysex8 streams: 0-255 */
Signature · 1 line
2.35.3
Greg Kroah-Hartman
maintainerQuoted 8 lines · Takashi Iwai
Hi,
This is a patch set for adding a USB gadget function driver for USB MIDI 2.0 interface. The MIDI 2.0 support in the host side was already enabled since 6.5 kernel in snd-usb-audio driver. Now, this provides a similar gadget functionality using the existing ALSA UMP helper functions.
Quoted 9 lines
The gadget driver provides both MIDI 2.0 and MIDI 1.0 fallback interfaces with one or more loopback UMP rawmidi devices. The configfs implementation is flexible enough to allow users to set up multiple UMP Endpoints and Function Blocks.
The first patch adds the bare
f_midi2driver without configfs support, followed by the configfs support, MIDI 1.0 interface support and a few other extensions and documentation.
Very cool, thanks for doing this! All now queued up, let's see what the builders say...
greg k-h
Thank you so very much for this gadget driver. Is this expected to be merged into 6.5rc2 ?
Never mind. Got the notice about 6.6 . Looking forward to see it in 6.5 next or the next merge window.