This series is adding support to mount 9pfs exported filesystems via the
usb gadget interface. It also includes tools and descriptions on how to
translate an tcp 9pfs and use it via the usb interface.
Signed-off-by: Michael Grzeschik <[email protected]>
---
Michael Grzeschik (3):
usb: gadget: function: 9pfs
usb: gadget: legacy: add 9pfs multi gadget
tools: usb: p9_fwd: add usb gadget packet forwarder script
Documentation/filesystems/9p.rst | 12 +
drivers/usb/gadget/Kconfig | 11 +
drivers/usb/gadget/function/Makefile | 2 +
drivers/usb/gadget/function/f_9pfs.c | 849 +++++++++++++++++++++++++++++++++++
drivers/usb/gadget/legacy/9pfs.c | 260 +++++++++++
drivers/usb/gadget/legacy/Kconfig | 15 +
drivers/usb/gadget/legacy/Makefile | 2 +
tools/usb/p9_fwd.py | 194 ++++++++
8 files changed, 1345 insertions(+)
---
base-commit: 052d534373b7ed33712a63d5e17b2b6cdbce84fd
change-id: 20240116-ml-topic-u9p-895274530eb1
Best regards,
--
Michael Grzeschik <[email protected]>
Add the newly introduced 9pfs transport gadget interface with an new
multi composed gadget together with acm and eem.
The usb forwarding can be started as follows:
Start the Network export via diod to localhost:
$ diod -f -n -d 0 -S -l 0.0.0.0:9999 -e $PWD
Start the python transport:
$ python p9_fwd.py
The gadget can then mount the exported filesystem:
$ mount -t 9p -o trans=usbg,aname=$PWD usb9pfs0 /mnt/9p
When using this legacy module, it is also possible to
mount the 9PFS usb dir as root filesystem. The kernel
commandline looks like this:
root=usb9pfs0 rootfstype=9p rootflags=trans=usbg,cache=loose,uname=root,access=0,dfltuid=0,dfltgid=0,aname=/path/to/rootfs
Signed-off-by: Michael Grzeschik <[email protected]>
---
drivers/usb/gadget/legacy/9pfs.c | 260 +++++++++++++++++++++++++++++++++++++
drivers/usb/gadget/legacy/Kconfig | 15 +++
drivers/usb/gadget/legacy/Makefile | 2 +
3 files changed, 277 insertions(+)
diff --git a/drivers/usb/gadget/legacy/9pfs.c b/drivers/usb/gadget/legacy/9pfs.c
new file mode 100644
index 0000000000000..e38fc89c89ded
--- /dev/null
+++ b/drivers/usb/gadget/legacy/9pfs.c
@@ -0,0 +1,260 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * usb9pfs.c -- Gadget usb9pfs
+ *
+ * Copyright (C) 2023 Michael Grzeschik
+ */
+
+/*
+ * Gadget usb9pfs only needs two bulk endpoints, and will use the usb9pfs usb
+ * transport to mount host filesystem via usb gadget.
+ */
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/err.h>
+#include <linux/usb/composite.h>
+#include <linux/netdevice.h>
+
+#include "u_eem.h"
+#include "u_ether.h"
+
+/*-------------------------------------------------------------------------*/
+USB_GADGET_COMPOSITE_OPTIONS();
+
+USB_ETHERNET_MODULE_PARAMETERS();
+
+/* Defines */
+
+#define DRIVER_VERSION_STR "v1.0"
+#define DRIVER_VERSION_NUM 0x1000
+
+#define DRIVER_DESC "Composite Gadget (9P + ACM + NCM)"
+
+/*-------------------------------------------------------------------------*/
+
+#define DRIVER_VENDOR_NUM 0x1d6b /* Linux Foundation */
+#define DRIVER_PRODUCT_NUM 0x0109 /* Linux-USB 9PFS Gadget */
+
+/*-------------------------------------------------------------------------*/
+
+static struct usb_device_descriptor device_desc = {
+ .bLength = sizeof(device_desc),
+ .bDescriptorType = USB_DT_DEVICE,
+
+ /* .bcdUSB = DYNAMIC */
+
+ .bDeviceClass = USB_CLASS_MISC,
+ .bDeviceSubClass = 2,
+ .bDeviceProtocol = 1,
+
+ /* .bMaxPacketSize0 = f(hardware) */
+
+ /* Vendor and product id can be overridden by module parameters. */
+ .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
+ .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
+ /* .bcdDevice = f(hardware) */
+ /* .iManufacturer = DYNAMIC */
+ /* .iProduct = DYNAMIC */
+ /* NO SERIAL NUMBER */
+ /*.bNumConfigurations = DYNAMIC*/
+};
+
+static const struct usb_descriptor_header *otg_desc[2];
+
+static struct usb_string strings_dev[] = {
+ [USB_GADGET_MANUFACTURER_IDX].s = "",
+ [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
+ [USB_GADGET_SERIAL_IDX].s = "",
+ { } /* end of list */
+};
+
+static struct usb_gadget_strings stringtab_dev = {
+ .language = 0x0409, /* en-us */
+ .strings = strings_dev,
+};
+
+static struct usb_gadget_strings *dev_strings[] = {
+ &stringtab_dev,
+ NULL,
+};
+
+static struct usb_configuration cdc_driver_conf = {
+ .label = DRIVER_DESC,
+ .bConfigurationValue = 1,
+ /* .iConfiguration = DYNAMIC */
+ .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
+};
+
+static struct usb_function *f_9pfs;
+static struct usb_function_instance *fi_9pfs;
+
+static struct usb_function *f_acm;
+static struct usb_function_instance *fi_acm;
+
+static struct usb_function *f_eem;
+static struct usb_function_instance *fi_eem;
+
+static int cdc_do_config(struct usb_configuration *c)
+{
+ int ret;
+
+ if (gadget_is_otg(c->cdev->gadget)) {
+ c->descriptors = otg_desc;
+ c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
+ }
+
+ f_9pfs = usb_get_function(fi_9pfs);
+ if (IS_ERR(f_9pfs))
+ return PTR_ERR(f_9pfs);
+
+ ret = usb_add_function(c, f_9pfs);
+ if (ret < 0)
+ goto err_func_9pfs;
+
+ f_acm = usb_get_function(fi_acm);
+ if (IS_ERR(f_acm)) {
+ ret = PTR_ERR(f_acm);
+ goto err_func_acm;
+ }
+
+ ret = usb_add_function(c, f_acm);
+ if (ret)
+ goto err_conf;
+
+ f_eem = usb_get_function(fi_eem);
+ if (IS_ERR(f_eem)) {
+ ret = PTR_ERR(f_eem);
+ goto err_eem;
+ }
+
+ ret = usb_add_function(c, f_eem);
+ if (ret)
+ goto err_run;
+
+ return 0;
+err_run:
+ usb_put_function(f_eem);
+err_eem:
+ usb_remove_function(c, f_acm);
+err_conf:
+ usb_put_function(f_acm);
+err_func_acm:
+ usb_remove_function(c, f_9pfs);
+err_func_9pfs:
+ usb_put_function(f_9pfs);
+ return ret;
+}
+
+static int usb9pfs_bind(struct usb_composite_dev *cdev)
+{
+ struct f_eem_opts *eem_opts = NULL;
+ int status;
+
+ fi_9pfs = usb_get_function_instance("usb9pfs");
+ if (IS_ERR(fi_9pfs))
+ return PTR_ERR(fi_9pfs);
+
+ /* set up serial link layer */
+ fi_acm = usb_get_function_instance("acm");
+ if (IS_ERR(fi_acm)) {
+ status = PTR_ERR(fi_acm);
+ goto err_conf_acm;
+ }
+
+ fi_eem = usb_get_function_instance("eem");
+ if (IS_ERR(fi_eem)) {
+ status = PTR_ERR(fi_eem);
+ goto err_conf_eem;
+ }
+
+ eem_opts = container_of(fi_eem, struct f_eem_opts, func_inst);
+
+ gether_set_qmult(eem_opts->net, qmult);
+ if (!gether_set_host_addr(eem_opts->net, host_addr))
+ pr_info("using host ethernet address: %s", host_addr);
+ if (!gether_set_dev_addr(eem_opts->net, dev_addr))
+ pr_info("using self ethernet address: %s", dev_addr);
+
+ /* Allocate string descriptor numbers ... note that string
+ * contents can be overridden by the composite_dev glue.
+ */
+ status = usb_string_ids_tab(cdev, strings_dev);
+ if (status < 0)
+ return status;
+
+ device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
+ device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
+ device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
+
+ /* support OTG systems */
+ if (gadget_is_otg(cdev->gadget)) {
+ if (!otg_desc[0]) {
+ struct usb_descriptor_header *usb_desc;
+
+ usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
+ if (!usb_desc) {
+ status = -ENOMEM;
+ goto err_conf_otg;
+ }
+ usb_otg_descriptor_init(cdev->gadget, usb_desc);
+ otg_desc[0] = usb_desc;
+ otg_desc[1] = NULL;
+ }
+ }
+
+ status = usb_add_config(cdev, &cdc_driver_conf, cdc_do_config);
+ if (status)
+ goto err_free_otg_desc;
+
+ usb_ep_autoconfig_reset(cdev->gadget);
+ usb_composite_overwrite_options(cdev, &coverwrite);
+
+ dev_info(&cdev->gadget->dev, DRIVER_DESC " version: " DRIVER_VERSION_STR "\n");
+
+ return 0;
+
+err_free_otg_desc:
+ kfree(otg_desc[0]);
+ otg_desc[0] = NULL;
+err_conf_otg:
+ usb_put_function_instance(fi_eem);
+err_conf_eem:
+ usb_put_function_instance(fi_acm);
+err_conf_acm:
+ usb_put_function_instance(fi_9pfs);
+ return status;
+}
+
+static int usb9pfs_unbind(struct usb_composite_dev *cdev)
+{
+ if (!IS_ERR_OR_NULL(f_eem))
+ usb_put_function(f_eem);
+ usb_put_function_instance(fi_eem);
+ if (!IS_ERR_OR_NULL(f_acm))
+ usb_put_function(f_acm);
+ usb_put_function_instance(fi_acm);
+ if (!IS_ERR_OR_NULL(f_9pfs))
+ usb_put_function(f_9pfs);
+ usb_put_function_instance(fi_9pfs);
+ kfree(otg_desc[0]);
+ otg_desc[0] = NULL;
+
+ return 0;
+}
+
+static struct usb_composite_driver usb9pfs_driver = {
+ .name = "usb9pfs",
+ .dev = &device_desc,
+ .strings = dev_strings,
+ .max_speed = USB_SPEED_SUPER,
+ .bind = usb9pfs_bind,
+ .unbind = usb9pfs_unbind,
+};
+
+module_usb_composite_driver(usb9pfs_driver);
+
+MODULE_AUTHOR("Michael Grzeschik");
+MODULE_LICENSE("GPL");
diff --git a/drivers/usb/gadget/legacy/Kconfig b/drivers/usb/gadget/legacy/Kconfig
index 0a7b382fbe27c..8569aecc1487b 100644
--- a/drivers/usb/gadget/legacy/Kconfig
+++ b/drivers/usb/gadget/legacy/Kconfig
@@ -62,6 +62,21 @@ config USB_ZERO
Say "y" to link the driver statically, or "m" to build a
dynamically linked module called "g_zero".
+config USB_9PFS
+ tristate "Gadget 9PFS"
+ select USB_LIBCOMPOSITE
+ select USB_U_SERIAL
+ select USB_U_ETHER
+ select USB_F_ACM
+ select USB_F_EEM
+ select USB_F_9PFS
+ select NET_9P
+ help
+ 9PFS Transport Layer
+
+ Say "y" to link the driver statically, or "m" to build a
+ dynamically linked module called "g_9pfs".
+
config USB_ZERO_HNPTEST
bool "HNP Test Device"
depends on USB_ZERO && USB_OTG
diff --git a/drivers/usb/gadget/legacy/Makefile b/drivers/usb/gadget/legacy/Makefile
index 4d864bf82799d..99c098800d898 100644
--- a/drivers/usb/gadget/legacy/Makefile
+++ b/drivers/usb/gadget/legacy/Makefile
@@ -8,6 +8,7 @@ ccflags-y += -I$(srctree)/drivers/usb/gadget/udc/
ccflags-y += -I$(srctree)/drivers/usb/gadget/function/
g_zero-y := zero.o
+g_9pfs-y := 9pfs.o
g_audio-y := audio.o
g_ether-y := ether.o
g_serial-y := serial.o
@@ -26,6 +27,7 @@ g_acm_ms-y := acm_ms.o
g_tcm_usb_gadget-y := tcm_usb_gadget.o
obj-$(CONFIG_USB_ZERO) += g_zero.o
+obj-$(CONFIG_USB_9PFS) += g_9pfs.o
obj-$(CONFIG_USB_AUDIO) += g_audio.o
obj-$(CONFIG_USB_ETH) += g_ether.o
obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o
--
2.39.2
This patch is adding an small python tool to forward 9pfs requests
from the USB gadget to an existing 9pfs TCP server. Since currently all
9pfs servers lack support for the usb transport this tool is an useful
helper to get started.
Signed-off-by: Michael Grzeschik <[email protected]>
---
tools/usb/p9_fwd.py | 194 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 194 insertions(+)
diff --git a/tools/usb/p9_fwd.py b/tools/usb/p9_fwd.py
new file mode 100755
index 0000000000000..95208df11abef
--- /dev/null
+++ b/tools/usb/p9_fwd.py
@@ -0,0 +1,194 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+
+import argparse
+import errno
+import logging
+import socket
+import struct
+import sys
+import time
+
+import usb.core
+import usb.util
+
+
+class Forwarder:
+ HEXDUMP_FILTER = (
+ "".join(chr(x).isprintable() and chr(x) or "." for x in range(128)) + "." * 128
+ )
+
+ @staticmethod
+ def _log_hexdump(data):
+ if not logging.root.isEnabledFor(logging.TRACE):
+ return
+ L = 16
+ for c in range(0, len(data), L):
+ chars = data[c : c + L]
+ dump = " ".join(f"{x:02x}" for x in chars)
+ printable = "".join(HEXDUMP_FILTER[x] for x in chars)
+ line = f"{c:08x} {dump:{L*3}s} |{printable:{L}s}|"
+ logging.root.log(logging.TRACE, "%s", line)
+
+ def __init__(self, server):
+ self.stats = {
+ "c2s packets": 0,
+ "c2s bytes": 0,
+ "s2c packets": 0,
+ "s2c bytes": 0,
+ }
+ self.stats_logged = time.monotonic()
+
+ dev = usb.core.find(idVendor=0x1D6B, idProduct=0x0109)
+ if dev is None:
+ raise ValueError("Device not found")
+
+ logging.info(f"found device: {dev.bus}/{dev.address}")
+
+ # dev.set_configuration() is not necessary since g_multi has only one
+ usb9pfs = None
+ # g_multi adds 9pfs as last interface
+ cfg = dev.get_active_configuration()
+ for intf in cfg:
+ # we have to detach the usb-storage driver from multi gadget since
+ # stall option could be set, which will lead to spontaneous port
+ # resets and our transfers will run dead
+ if intf.bInterfaceClass == 0x08:
+ if dev.is_kernel_driver_active(intf.bInterfaceNumber):
+ dev.detach_kernel_driver(intf.bInterfaceNumber)
+
+ if (
+ intf.bInterfaceClass == 0xFF
+ and intf.bInterfaceSubClass == 0xFF
+ and intf.bInterfaceProtocol == 0x09
+ ):
+ usb9pfs = intf
+ if usb9pfs is None:
+ raise ValueError("Interface not found")
+
+ logging.info(f"claiming interface:\n{usb9pfs}")
+ usb.util.claim_interface(dev, usb9pfs.bInterfaceNumber)
+ ep_out = usb.util.find_descriptor(
+ usb9pfs,
+ custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress)
+ == usb.util.ENDPOINT_OUT,
+ )
+ assert ep_out is not None
+ ep_in = usb.util.find_descriptor(
+ usb9pfs,
+ custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress)
+ == usb.util.ENDPOINT_IN,
+ )
+ assert ep_in is not None
+ logging.info(f"interface claimed")
+
+ self.ep_out = ep_out
+ self.ep_in = ep_in
+ self.dev = dev
+
+ # create and connect socket
+ self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.s.connect(server)
+
+ logging.info(f"connected to server")
+
+ def c2s(self):
+ """forward a request from the USB client to the TCP server"""
+ data = None
+ while data is None:
+ try:
+ logging.log(logging.TRACE, "c2s: reading")
+ data = self.ep_in.read(self.ep_in.wMaxPacketSize)
+ except usb.core.USBTimeoutError:
+ logging.log(logging.TRACE, "c2s: reading timed out")
+ continue
+ except usb.core.USBError as e:
+ if e.errno == errno.EIO:
+ logging.debug("c2s: reading failed with %s, retrying", repr(e))
+ time.sleep(0.5)
+ continue
+ else:
+ logging.error("c2s: reading failed with %s, aborting", repr(e))
+ raise
+ size = struct.unpack("<I", data[:4])[0]
+ while len(data) < size:
+ data += self.ep_in.read(size - len(data))
+ logging.log(logging.TRACE, "c2s: writing")
+ self._log_hexdump(data)
+ self.s.send(data)
+ logging.debug("c2s: forwarded %i bytes", size)
+ self.stats["c2s packets"] += 1
+ self.stats["c2s bytes"] += size
+
+ def s2c(self):
+ """forward a response from the TCP server to the USB client"""
+ logging.log(logging.TRACE, "s2c: reading")
+ data = self.s.recv(4)
+ size = struct.unpack("<I", data[:4])[0]
+ while len(data) < size:
+ data += self.s.recv(size - len(data))
+ logging.log(logging.TRACE, "s2c: writing")
+ self._log_hexdump(data)
+ while data:
+ written = self.ep_out.write(data)
+ assert written > 0
+ data = data[written:]
+ if size % self.ep_out.wMaxPacketSize == 0:
+ logging.log(logging.TRACE, "sending zero length packet")
+ self.ep_out.write(b"")
+ logging.debug("s2c: forwarded %i bytes", size)
+ self.stats["s2c packets"] += 1
+ self.stats["s2c bytes"] += size
+
+ def log_stats(self):
+ logging.info("statistics:")
+ for k, v in self.stats.items():
+ logging.info(f" {k+':':14s} {v}")
+
+ def log_stats_interval(self, interval=5):
+ if (time.monotonic() - self.stats_logged) < interval:
+ return
+
+ self.log_stats()
+ self.stats_logged = time.monotonic()
+
+
+def main():
+ parser = argparse.ArgumentParser(
+ description="Forward 9PFS requests from USB to TCP",
+ )
+
+ parser.add_argument(
+ "-s", "--server", type=str, default="127.0.0.1", help="server hostname"
+ )
+ parser.add_argument("-p", "--port", type=int, default=564, help="server port")
+ parser.add_argument("-v", "--verbose", action="count", default=0)
+
+ args = parser.parse_args()
+
+ logging.TRACE = logging.DEBUG - 5
+ logging.addLevelName(logging.TRACE, "TRACE")
+
+ if args.verbose >= 2:
+ level = logging.TRACE
+ elif args.verbose:
+ level = logging.DEBUG
+ else:
+ level = logging.INFO
+ logging.basicConfig(
+ level=level, format="%(asctime)-15s %(levelname)-8s %(message)s"
+ )
+
+ f = Forwarder(server=(args.server, args.port))
+
+ try:
+ while True:
+ f.c2s()
+ f.s2c()
+ f.log_stats_interval()
+ finally:
+ f.log_stats()
+
+
+if __name__ == "__main__":
+ main()
--
2.39.2
Add the new gadget function for 9pfs transport. This function is
defining an simple 9pfs transport interface that consists of one in and
one out endpoint. The endpoints transmit and receive the 9pfs protocol
payload when mounting a 9p filesystem over usb.
Signed-off-by: Michael Grzeschik <[email protected]>
---
Documentation/filesystems/9p.rst | 12 +
drivers/usb/gadget/Kconfig | 11 +
drivers/usb/gadget/function/Makefile | 2 +
drivers/usb/gadget/function/f_9pfs.c | 849 +++++++++++++++++++++++++++++++++++
4 files changed, 874 insertions(+)
diff --git a/Documentation/filesystems/9p.rst b/Documentation/filesystems/9p.rst
index 1e0e0bb6fdf91..963267085218b 100644
--- a/Documentation/filesystems/9p.rst
+++ b/Documentation/filesystems/9p.rst
@@ -53,6 +53,17 @@ mount points. Each 9P export is seen by the client as a virtio device with an
associated "mount_tag" property. Available mount tags can be
seen by reading /sys/bus/virtio/drivers/9pnet_virtio/virtio<n>/mount_tag files.
+For usb gadget file server::
+
+ mount -t 9p -o trans=usbg,aname=/path/to/fs <mount_tag> /mnt/9
+
+For usb gadget root file system::
+
+ root=<mount_tag> rootfstype=9p rootflags=trans=usbg,cache=loose,uname=root,access=0,dfltuid=0,dfltgid=0,aname=/path/to/rootfs
+
+where mount_tag is the tag associated by the usb gadget transport. The
+pattern is usb9pfs0, usb9pfs1, ...
+
Options
=======
@@ -68,6 +79,7 @@ Options
virtio connect to the next virtio channel available
(from QEMU with trans_virtio module)
rdma connect to a specified RDMA channel
+ usbg connect to a specified usb gadget channel
======== ============================================
uname=name user name to attempt mount as on the remote server. The
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index b3592bcb0f966..72cdecaef6aa9 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -153,6 +153,10 @@ config USB_F_ACM
config USB_F_SS_LB
tristate
+config USB_F_9PFS
+ tristate
+ select NET_9P
+
config USB_U_SERIAL
tristate
@@ -363,6 +367,13 @@ config USB_CONFIGFS_F_LB_SS
test software, like the "usbtest" driver, to put your hardware
and its driver through a basic set of functional tests.
+config USB_CONFIGFS_F_9PFS
+ bool "9pfs over usb gadget"
+ depends on USB_CONFIGFS
+ select USB_F_9PFS
+ help
+ 9pfs support for usb gadget
+
config USB_CONFIGFS_F_FS
bool "Function filesystem (FunctionFS)"
depends on USB_CONFIGFS
diff --git a/drivers/usb/gadget/function/Makefile b/drivers/usb/gadget/function/Makefile
index 87917a7d4a9be..5e6b637fdeff1 100644
--- a/drivers/usb/gadget/function/Makefile
+++ b/drivers/usb/gadget/function/Makefile
@@ -11,6 +11,8 @@ usb_f_acm-y := f_acm.o
obj-$(CONFIG_USB_F_ACM) += usb_f_acm.o
usb_f_ss_lb-y := f_loopback.o f_sourcesink.o
obj-$(CONFIG_USB_F_SS_LB) += usb_f_ss_lb.o
+usb_f_9pfs-y := f_9pfs.o
+obj-$(CONFIG_USB_F_9PFS) += usb_f_9pfs.o
obj-$(CONFIG_USB_U_SERIAL) += u_serial.o
usb_f_serial-y := f_serial.o
obj-$(CONFIG_USB_F_SERIAL) += usb_f_serial.o
diff --git a/drivers/usb/gadget/function/f_9pfs.c b/drivers/usb/gadget/function/f_9pfs.c
new file mode 100644
index 0000000000000..3cfca40e94538
--- /dev/null
+++ b/drivers/usb/gadget/function/f_9pfs.c
@@ -0,0 +1,849 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * f_9pfs.c - USB peripheral usb9pfs configuration driver and
+ * transposrt layer.
+ *
+ * Copyright (C) 2023 Michael Grzeschik <[email protected]>
+ */
+
+/*
+ * Gadget usb9pfs only needs two bulk endpoints, and will use the usb9pfs usb
+ * transport to mount host filesystem via usb gadget.
+ */
+
+#include <linux/slab.h>
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/err.h>
+#include <linux/usb/composite.h>
+
+#include <net/9p/9p.h>
+#include <net/9p/client.h>
+#include <net/9p/transport.h>
+
+#include "u_f.h"
+
+#define DEFAULT_BUFLEN 16384
+
+struct f_usb9pfs {
+ struct p9_client *client;
+
+ struct p9_req_t *p9_tx_req;
+
+ struct list_head tx_req_list;
+
+ spinlock_t lock;
+ spinlock_t req_lock;
+
+ struct usb_request *in_req;
+ struct usb_request *out_req;
+
+ struct usb_ep *in_ep;
+ struct usb_ep *out_ep;
+
+ unsigned int buflen;
+
+ bool inuse;
+ char *tag;
+
+ int index;
+
+ struct usb_function function;
+
+ struct list_head function_list;
+};
+
+static inline struct f_usb9pfs *func_to_usb9pfs(struct usb_function *f)
+{
+ return container_of(f, struct f_usb9pfs, function);
+}
+
+struct f_usb9pfs_opts {
+ struct usb_function_instance func_inst;
+ unsigned int buflen;
+
+ /*
+ * Read/write access to configfs attributes is handled by configfs.
+ *
+ * This is to protect the data from concurrent access by read/write
+ * and create symlink/remove symlink.
+ */
+ struct mutex lock;
+ int refcnt;
+};
+
+static DEFINE_IDA(usb9pfs_ida);
+static DEFINE_MUTEX(usb9pfs_ida_lock); /* protects access do usb9pfs_ida */
+
+static DEFINE_MUTEX(usb9pfs_lock);
+static struct list_head usbg_function_list;
+
+static int usb9pfs_tx_fill(struct f_usb9pfs *usb9pfs, struct usb_request *req,
+ gfp_t gfp_flags)
+{
+ struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
+ int retval = -ENOMEM;
+
+ if (!(usb9pfs->p9_tx_req->tc.size % usb9pfs->in_ep->maxpacket))
+ req->zero = 1;
+
+ req->buf = usb9pfs->p9_tx_req->tc.sdata;
+ req->length = usb9pfs->p9_tx_req->tc.size;
+
+ dev_dbg(&cdev->gadget->dev, "%s usb9pfs send --> %d/%d, zero: %d\n",
+ usb9pfs->in_ep->name, req->actual, req->length, req->zero);
+
+ retval = usb_ep_queue(usb9pfs->in_ep, req, gfp_flags);
+
+ dev_dbg(&cdev->gadget->dev, "tx submit --> %d\n", retval);
+
+ return retval;
+}
+
+static int usb9pfs_rx_fill(struct f_usb9pfs *usb9pfs, struct usb_request *req,
+ gfp_t gfp_flags)
+{
+ struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
+ int retval = -ENOMEM;
+
+ retval = usb_ep_queue(usb9pfs->out_ep, req, gfp_flags);
+
+ dev_dbg(&cdev->gadget->dev, "rx submit --> %d\n", retval);
+
+ return retval;
+}
+
+static int usb9pfs_transmit(struct f_usb9pfs *usb9pfs)
+{
+ struct p9_req_t *p9_req = NULL;
+ unsigned long flags;
+ int ret = 0;
+
+ spin_lock_irqsave(&usb9pfs->lock, flags);
+
+ if (usb9pfs->p9_tx_req) {
+ spin_unlock_irqrestore(&usb9pfs->lock, flags);
+ return -EBUSY;
+ }
+
+ p9_req = list_first_entry_or_null(&usb9pfs->tx_req_list, struct p9_req_t, req_list);
+ if (!p9_req) {
+ spin_unlock_irqrestore(&usb9pfs->lock, flags);
+ return -ENOENT;
+ }
+
+ list_del(&p9_req->req_list);
+
+ usb9pfs->p9_tx_req = p9_req;
+
+ p9_req_get(usb9pfs->p9_tx_req);
+
+ usb9pfs_tx_fill(usb9pfs, usb9pfs->in_req, GFP_ATOMIC);
+
+ spin_unlock_irqrestore(&usb9pfs->lock, flags);
+
+ return ret;
+}
+
+static void usb9pfs_tx_complete(struct usb_ep *ep, struct usb_request *req)
+{
+ struct f_usb9pfs *usb9pfs = ep->driver_data;
+ struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
+
+ /* reset zero packages */
+ req->zero = 0;
+
+ dev_dbg(&cdev->gadget->dev, "%s usb9pfs complete --> %d, %d/%d\n",
+ ep->name, req->status, req->actual, req->length);
+
+ switch (req->status) {
+ case 0: /* normal completion? */
+ spin_lock(&usb9pfs->req_lock);
+
+ WRITE_ONCE(usb9pfs->p9_tx_req->status, REQ_STATUS_SENT);
+
+ p9_req_put(usb9pfs->client, usb9pfs->p9_tx_req);
+
+ usb9pfs_rx_fill(usb9pfs, usb9pfs->out_req, GFP_ATOMIC);
+
+ spin_unlock(&usb9pfs->req_lock);
+
+ break;
+
+ default:
+ dev_err(&cdev->gadget->dev, "%s usb9pfs complete --> %d, %d/%d\n",
+ ep->name, req->status, req->actual, req->length);
+ usb_ep_free_request(ep == usb9pfs->in_ep ?
+ usb9pfs->out_ep : usb9pfs->in_ep,
+ req->context);
+ free_ep_req(ep, req);
+ return;
+ }
+}
+
+static struct p9_req_t *usb9pfs_rx_header(struct f_usb9pfs *usb9pfs, struct usb_request *req)
+{
+ struct p9_req_t *p9_rx_req;
+ struct p9_fcall rc;
+ int ret;
+
+ /* start by reading header */
+ rc.sdata = req->buf;
+ rc.offset = 0;
+ rc.capacity = rc.size = P9_HDRSZ;
+
+ p9_debug(P9_DEBUG_TRANS, "mux %p got %u bytes\n", usb9pfs,
+ rc.capacity - rc.offset);
+
+ ret = p9_parse_header(&rc, &rc.size, NULL, NULL, 0);
+ if (ret) {
+ p9_debug(P9_DEBUG_ERROR,
+ "error parsing header: %d\n", ret);
+ return NULL;
+ }
+
+ p9_debug(P9_DEBUG_TRANS,
+ "mux %p pkt: size: %d bytes tag: %d\n",
+ usb9pfs, rc.size, rc.tag);
+
+ p9_rx_req = p9_tag_lookup(usb9pfs->client, rc.tag);
+ if (!p9_rx_req || (p9_rx_req->status != REQ_STATUS_SENT)) {
+ p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n", rc.tag);
+ return NULL;
+ }
+
+ if (rc.size > p9_rx_req->rc.capacity) {
+ p9_debug(P9_DEBUG_ERROR,
+ "requested packet size too big: %d for tag %d with capacity %zd\n",
+ rc.size, rc.tag, p9_rx_req->rc.capacity);
+ return NULL;
+ }
+
+ if (!p9_rx_req->rc.sdata) {
+ p9_debug(P9_DEBUG_ERROR,
+ "No recv fcall for tag %d (req %p), disconnecting!\n",
+ rc.tag, p9_rx_req);
+ p9_req_put(usb9pfs->client, p9_rx_req);
+ return NULL;
+ }
+
+ return p9_rx_req;
+}
+
+static void usb9pfs_rx_complete(struct usb_ep *ep, struct usb_request *req)
+{
+ struct f_usb9pfs *usb9pfs = ep->driver_data;
+ struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
+ struct p9_req_t *p9_rx_req;
+ unsigned long flags;
+
+ switch (req->status) {
+ case 0: /* normal completion? */
+ spin_lock_irqsave(&usb9pfs->req_lock, flags);
+ p9_rx_req = usb9pfs_rx_header(usb9pfs, req);
+ if (!p9_rx_req) {
+ spin_unlock_irqrestore(&usb9pfs->req_lock, flags);
+ goto free_req;
+ }
+
+ memcpy(p9_rx_req->rc.sdata, req->buf, req->actual);
+ p9_rx_req->rc.size = req->actual;
+
+ p9_client_cb(usb9pfs->client, p9_rx_req, REQ_STATUS_RCVD);
+ p9_req_put(usb9pfs->client, p9_rx_req);
+
+ usb9pfs->p9_tx_req = NULL;
+
+ usb9pfs_transmit(usb9pfs);
+
+ spin_unlock_irqrestore(&usb9pfs->req_lock, flags);
+
+ return;
+free_req:
+ default:
+ dev_err(&cdev->gadget->dev, "%s usb9pfs complete --> %d, %d/%d\n",
+ ep->name, req->status, req->actual, req->length);
+ usb_ep_free_request(ep == usb9pfs->in_ep ?
+ usb9pfs->out_ep : usb9pfs->in_ep,
+ req->context);
+ free_ep_req(ep, req);
+ return;
+ }
+
+ p9_client_cb(usb9pfs->client, p9_rx_req, REQ_STATUS_ERROR);
+}
+
+static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
+{
+ int value;
+
+ value = usb_ep_disable(ep);
+ if (value < 0)
+ dev_info(&cdev->gadget->dev, "disable %s --> %d\n", ep->name, value);
+}
+
+void disable_endpoints(struct usb_composite_dev *cdev,
+ struct usb_ep *in, struct usb_ep *out,
+ struct usb_ep *iso_in, struct usb_ep *iso_out)
+{
+ disable_ep(cdev, in);
+ disable_ep(cdev, out);
+}
+
+static void disable_usb9pfs(struct f_usb9pfs *usb9pfs)
+{
+ struct usb_composite_dev *cdev;
+
+ cdev = usb9pfs->function.config->cdev;
+ disable_endpoints(cdev, usb9pfs->in_ep, usb9pfs->out_ep, NULL, NULL);
+ dev_dbg(&cdev->gadget->dev, "%s disabled\n", usb9pfs->function.name);
+}
+
+static inline struct usb_request *usb9pfs_alloc_ep_req(struct usb_ep *ep, int len)
+{
+ return alloc_ep_req(ep, len);
+}
+
+static int alloc_requests(struct usb_composite_dev *cdev,
+ struct f_usb9pfs *usb9pfs)
+{
+ int result = 0;
+
+ usb9pfs->in_req = usb_ep_alloc_request(usb9pfs->in_ep, GFP_ATOMIC);
+ if (!usb9pfs->in_req)
+ goto fail;
+
+ usb9pfs->out_req = usb9pfs_alloc_ep_req(usb9pfs->out_ep, usb9pfs->buflen);
+ if (!usb9pfs->out_req)
+ goto fail_in;
+
+ usb9pfs->in_req->complete = usb9pfs_tx_complete;
+ usb9pfs->out_req->complete = usb9pfs_rx_complete;
+
+ /* length will be set in complete routine */
+ usb9pfs->in_req->context = usb9pfs->out_req->context = usb9pfs;
+
+ return 0;
+
+fail_in:
+ usb_ep_free_request(usb9pfs->in_ep, usb9pfs->in_req);
+fail:
+ return result;
+}
+
+static int enable_endpoint(struct usb_composite_dev *cdev,
+ struct f_usb9pfs *usb9pfs, struct usb_ep *ep)
+{
+ int result;
+
+ result = config_ep_by_speed(cdev->gadget, &(usb9pfs->function), ep);
+ if (result)
+ goto out;
+
+ result = usb_ep_enable(ep);
+ if (result < 0)
+ goto out;
+
+ ep->driver_data = usb9pfs;
+
+ result = 0;
+
+out:
+ return result;
+}
+
+static int
+enable_usb9pfs(struct usb_composite_dev *cdev, struct f_usb9pfs *usb9pfs)
+{
+ int result = 0;
+
+ result = enable_endpoint(cdev, usb9pfs, usb9pfs->in_ep);
+ if (result)
+ goto out;
+
+ result = enable_endpoint(cdev, usb9pfs, usb9pfs->out_ep);
+ if (result)
+ goto disable_in;
+
+ result = alloc_requests(cdev, usb9pfs);
+ if (result)
+ goto disable_out;
+
+ dev_dbg(&cdev->gadget->dev, "%s enabled\n", usb9pfs->function.name);
+ return 0;
+
+disable_out:
+ usb_ep_disable(usb9pfs->out_ep);
+disable_in:
+ usb_ep_disable(usb9pfs->in_ep);
+out:
+ return result;
+}
+
+static int p9_usbg_create(struct p9_client *client, const char *devname, char *args)
+{
+ struct usb_composite_dev *cdev;
+ struct f_usb9pfs *usb9pfs;
+ struct f_usb9pfs *tmp;
+ struct usb_function *f;
+ int ret = -ENOENT;
+ int found = 0;
+
+ if (devname == NULL)
+ return -EINVAL;
+
+ mutex_lock(&usb9pfs_lock);
+ list_for_each_entry_safe(usb9pfs, tmp, &usbg_function_list, function_list) {
+ if (usb9pfs->tag == NULL)
+ continue;
+ if (!strncmp(devname, usb9pfs->tag, strlen(devname))) {
+ if (!usb9pfs->inuse) {
+ usb9pfs->inuse = true;
+ found = 1;
+ break;
+ }
+ ret = -EBUSY;
+ break;
+ }
+ }
+ mutex_unlock(&usb9pfs_lock);
+
+ if (!found) {
+ pr_err("no channels available for device %s\n", devname);
+ return ret;
+ }
+
+ INIT_LIST_HEAD(&usb9pfs->tx_req_list);
+
+ spin_lock_init(&usb9pfs->lock);
+ spin_lock_init(&usb9pfs->req_lock);
+
+ f = &usb9pfs->function;
+ cdev = f->config->cdev;
+
+ client->trans = (void *)usb9pfs;
+ client->status = Connected;
+ usb9pfs->client = client;
+
+ client->trans_mod->maxsize = usb9pfs->buflen;
+
+ disable_usb9pfs(usb9pfs);
+
+ return enable_usb9pfs(cdev, usb9pfs);
+}
+
+static void p9_usbg_close(struct p9_client *client)
+{
+ struct f_usb9pfs *usb9pfs = client->trans;
+
+ disable_usb9pfs(usb9pfs);
+}
+
+static int p9_usbg_request(struct p9_client *client, struct p9_req_t *p9_req)
+{
+ struct f_usb9pfs *usb9pfs = client->trans;
+ unsigned long flags;
+
+ spin_lock_irqsave(&usb9pfs->lock, flags);
+ list_add_tail(&p9_req->req_list, &usb9pfs->tx_req_list);
+ spin_unlock_irqrestore(&usb9pfs->lock, flags);
+
+ spin_lock_irqsave(&usb9pfs->req_lock, flags);
+ usb9pfs_transmit(usb9pfs);
+ spin_unlock_irqrestore(&usb9pfs->req_lock, flags);
+
+ return 0;
+}
+
+/* We don't currently allow canceling of requests */
+static int p9_usbg_cancel(struct p9_client *client, struct p9_req_t *req)
+{
+ return 1;
+}
+
+static struct p9_trans_module p9_usbg_trans = {
+ .name = "usbg",
+ .create = p9_usbg_create,
+ .close = p9_usbg_close,
+ .request = p9_usbg_request,
+ .cancel = p9_usbg_cancel,
+ .owner = THIS_MODULE,
+};
+
+/*-------------------------------------------------------------------------*/
+
+#define USB_PROTOCOL_9PFS 0x09
+
+static struct usb_interface_descriptor usb9pfs_intf = {
+ .bLength = sizeof(usb9pfs_intf),
+ .bDescriptorType = USB_DT_INTERFACE,
+
+ .bNumEndpoints = 2,
+ .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
+ .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
+ .bInterfaceProtocol = USB_PROTOCOL_9PFS,
+
+ /* .iInterface = DYNAMIC */
+};
+
+/* full speed support: */
+
+static struct usb_endpoint_descriptor fs_usb9pfs_source_desc = {
+ .bLength = USB_DT_ENDPOINT_SIZE,
+ .bDescriptorType = USB_DT_ENDPOINT,
+
+ .bEndpointAddress = USB_DIR_IN,
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+};
+
+static struct usb_endpoint_descriptor fs_usb9pfs_sink_desc = {
+ .bLength = USB_DT_ENDPOINT_SIZE,
+ .bDescriptorType = USB_DT_ENDPOINT,
+
+ .bEndpointAddress = USB_DIR_OUT,
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+};
+
+static struct usb_descriptor_header *fs_usb9pfs_descs[] = {
+ (struct usb_descriptor_header *) &usb9pfs_intf,
+ (struct usb_descriptor_header *) &fs_usb9pfs_sink_desc,
+ (struct usb_descriptor_header *) &fs_usb9pfs_source_desc,
+ NULL,
+};
+
+/* high speed support: */
+
+static struct usb_endpoint_descriptor hs_usb9pfs_source_desc = {
+ .bLength = USB_DT_ENDPOINT_SIZE,
+ .bDescriptorType = USB_DT_ENDPOINT,
+
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = cpu_to_le16(512),
+};
+
+static struct usb_endpoint_descriptor hs_usb9pfs_sink_desc = {
+ .bLength = USB_DT_ENDPOINT_SIZE,
+ .bDescriptorType = USB_DT_ENDPOINT,
+
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = cpu_to_le16(512),
+};
+
+static struct usb_descriptor_header *hs_usb9pfs_descs[] = {
+ (struct usb_descriptor_header *) &usb9pfs_intf,
+ (struct usb_descriptor_header *) &hs_usb9pfs_source_desc,
+ (struct usb_descriptor_header *) &hs_usb9pfs_sink_desc,
+ NULL,
+};
+
+/* super speed support: */
+
+static struct usb_endpoint_descriptor ss_usb9pfs_source_desc = {
+ .bLength = USB_DT_ENDPOINT_SIZE,
+ .bDescriptorType = USB_DT_ENDPOINT,
+
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = cpu_to_le16(1024),
+};
+
+static struct usb_ss_ep_comp_descriptor ss_usb9pfs_source_comp_desc = {
+ .bLength = USB_DT_SS_EP_COMP_SIZE,
+ .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
+ .bMaxBurst = 0,
+ .bmAttributes = 0,
+ .wBytesPerInterval = 0,
+};
+
+static struct usb_endpoint_descriptor ss_usb9pfs_sink_desc = {
+ .bLength = USB_DT_ENDPOINT_SIZE,
+ .bDescriptorType = USB_DT_ENDPOINT,
+
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = cpu_to_le16(1024),
+};
+
+static struct usb_ss_ep_comp_descriptor ss_usb9pfs_sink_comp_desc = {
+ .bLength = USB_DT_SS_EP_COMP_SIZE,
+ .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
+ .bMaxBurst = 0,
+ .bmAttributes = 0,
+ .wBytesPerInterval = 0,
+};
+
+static struct usb_descriptor_header *ss_usb9pfs_descs[] = {
+ (struct usb_descriptor_header *) &usb9pfs_intf,
+ (struct usb_descriptor_header *) &ss_usb9pfs_source_desc,
+ (struct usb_descriptor_header *) &ss_usb9pfs_source_comp_desc,
+ (struct usb_descriptor_header *) &ss_usb9pfs_sink_desc,
+ (struct usb_descriptor_header *) &ss_usb9pfs_sink_comp_desc,
+ NULL,
+};
+
+/* function-specific strings: */
+static struct usb_string strings_usb9pfs[] = {
+ [0].s = "usb9pfs input to output",
+ { } /* end of list */
+};
+
+static struct usb_gadget_strings stringtab_usb9pfs = {
+ .language = 0x0409, /* en-us */
+ .strings = strings_usb9pfs,
+};
+
+static struct usb_gadget_strings *usb9pfs_strings[] = {
+ &stringtab_usb9pfs,
+ NULL,
+};
+
+/*-------------------------------------------------------------------------*/
+
+static int usb9pfs_func_bind(struct usb_configuration *c, struct usb_function *f)
+{
+ struct f_usb9pfs *usb9pfs = func_to_usb9pfs(f);
+ struct usb_composite_dev *cdev = c->cdev;
+ int ret;
+ int id;
+
+ /* allocate interface ID(s) */
+ id = usb_interface_id(c, f);
+ if (id < 0)
+ return id;
+ usb9pfs_intf.bInterfaceNumber = id;
+
+ id = usb_string_id(cdev);
+ if (id < 0)
+ return id;
+ strings_usb9pfs[0].id = id;
+ usb9pfs_intf.iInterface = id;
+
+ /* allocate endpoints */
+ usb9pfs->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_usb9pfs_source_desc);
+ if (!usb9pfs->in_ep)
+ goto autoconf_fail;
+
+ usb9pfs->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_usb9pfs_sink_desc);
+ if (!usb9pfs->out_ep)
+ goto autoconf_fail;
+
+ /* support high speed hardware */
+ hs_usb9pfs_source_desc.bEndpointAddress =
+ fs_usb9pfs_source_desc.bEndpointAddress;
+ hs_usb9pfs_sink_desc.bEndpointAddress = fs_usb9pfs_sink_desc.bEndpointAddress;
+
+ /* support super speed hardware */
+ ss_usb9pfs_source_desc.bEndpointAddress =
+ fs_usb9pfs_source_desc.bEndpointAddress;
+ ss_usb9pfs_sink_desc.bEndpointAddress = fs_usb9pfs_sink_desc.bEndpointAddress;
+
+ ret = usb_assign_descriptors(f, fs_usb9pfs_descs, hs_usb9pfs_descs,
+ ss_usb9pfs_descs, ss_usb9pfs_descs);
+ if (ret)
+ return ret;
+
+ dev_dbg(&cdev->gadget->dev, "%s speed %s: IN/%s, OUT/%s\n",
+ (gadget_is_superspeed(c->cdev->gadget) ? "super" :
+ (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
+ f->name, usb9pfs->in_ep->name, usb9pfs->out_ep->name);
+ return 0;
+
+autoconf_fail:
+ ERROR(cdev, "%s: can't autoconfigure on %s\n",
+ f->name, cdev->gadget->name);
+ return -ENODEV;
+}
+
+static void usb9pfs_free_func(struct usb_function *f)
+{
+ struct f_usb9pfs_opts *opts;
+
+ opts = container_of(f->fi, struct f_usb9pfs_opts, func_inst);
+
+ mutex_lock(&opts->lock);
+ opts->refcnt--;
+ mutex_unlock(&opts->lock);
+
+ usb_free_all_descriptors(f);
+ kfree(func_to_usb9pfs(f));
+}
+
+static int usb9pfs_set_alt(struct usb_function *f,
+ unsigned int intf, unsigned int alt)
+{
+ return 0;
+}
+
+static void usb9pfs_disable(struct usb_function *f)
+{
+}
+
+static struct usb_function *usb9pfs_alloc(struct usb_function_instance *fi)
+{
+ struct f_usb9pfs_opts *usb9pfs_opts;
+ struct f_usb9pfs *usb9pfs;
+
+ usb9pfs = kzalloc(sizeof(*usb9pfs), GFP_KERNEL);
+ if (!usb9pfs)
+ return ERR_PTR(-ENOMEM);
+
+ usb9pfs_opts = container_of(fi, struct f_usb9pfs_opts, func_inst);
+
+ mutex_lock(&usb9pfs_opts->lock);
+ usb9pfs_opts->refcnt++;
+ mutex_unlock(&usb9pfs_opts->lock);
+
+ usb9pfs->buflen = usb9pfs_opts->buflen;
+
+ usb9pfs->function.name = "usb9pfs";
+ usb9pfs->function.bind = usb9pfs_func_bind;
+ usb9pfs->function.set_alt = usb9pfs_set_alt;
+ usb9pfs->function.disable = usb9pfs_disable;
+ usb9pfs->function.strings = usb9pfs_strings;
+
+ usb9pfs->function.free_func = usb9pfs_free_func;
+
+ mutex_lock(&usb9pfs_ida_lock);
+
+ usb9pfs->index = ida_simple_get(&usb9pfs_ida, 0, 100, GFP_KERNEL);
+ if (usb9pfs->index < 0) {
+ struct usb_function *ret = ERR_PTR(usb9pfs->index);
+
+ kfree(usb9pfs);
+ mutex_unlock(&usb9pfs_ida_lock);
+ return ret;
+ }
+
+ mutex_unlock(&usb9pfs_ida_lock);
+
+ usb9pfs->tag = kasprintf(GFP_KERNEL, "%s%d", usb9pfs->function.name,
+ usb9pfs->index);
+
+ INIT_LIST_HEAD(&usb9pfs->function_list);
+
+ mutex_lock(&usb9pfs_lock);
+ list_add_tail(&usb9pfs->function_list, &usbg_function_list);
+ mutex_unlock(&usb9pfs_lock);
+
+ return &usb9pfs->function;
+}
+
+static inline struct f_usb9pfs_opts *to_f_usb9pfs_opts(struct config_item *item)
+{
+ return container_of(to_config_group(item), struct f_usb9pfs_opts,
+ func_inst.group);
+}
+
+static void usb9pfs_attr_release(struct config_item *item)
+{
+ struct f_usb9pfs_opts *usb9pfs_opts = to_f_usb9pfs_opts(item);
+
+ usb_put_function_instance(&usb9pfs_opts->func_inst);
+}
+
+static struct configfs_item_operations usb9pfs_item_ops = {
+ .release = usb9pfs_attr_release,
+};
+
+static ssize_t f_usb9pfs_opts_buflen_show(struct config_item *item, char *page)
+{
+ struct f_usb9pfs_opts *opts = to_f_usb9pfs_opts(item);
+ int result;
+
+ mutex_lock(&opts->lock);
+ result = sprintf(page, "%d\n", opts->buflen);
+ mutex_unlock(&opts->lock);
+
+ return result;
+}
+
+static ssize_t f_usb9pfs_opts_buflen_store(struct config_item *item,
+ const char *page, size_t len)
+{
+ struct f_usb9pfs_opts *opts = to_f_usb9pfs_opts(item);
+ int ret;
+ u32 num;
+
+ mutex_lock(&opts->lock);
+ if (opts->refcnt) {
+ ret = -EBUSY;
+ goto end;
+ }
+
+ ret = kstrtou32(page, 0, &num);
+ if (ret)
+ goto end;
+
+ opts->buflen = num;
+ ret = len;
+end:
+ mutex_unlock(&opts->lock);
+ return ret;
+}
+
+CONFIGFS_ATTR(f_usb9pfs_opts_, buflen);
+
+static struct configfs_attribute *usb9pfs_attrs[] = {
+ &f_usb9pfs_opts_attr_buflen,
+ NULL,
+};
+
+static const struct config_item_type usb9pfs_func_type = {
+ .ct_item_ops = &usb9pfs_item_ops,
+ .ct_attrs = usb9pfs_attrs,
+ .ct_owner = THIS_MODULE,
+};
+
+static void usb9pfs_free_instance(struct usb_function_instance *fi)
+{
+ struct f_usb9pfs_opts *usb9pfs_opts;
+
+ usb9pfs_opts = container_of(fi, struct f_usb9pfs_opts, func_inst);
+ kfree(usb9pfs_opts);
+}
+
+static struct usb_function_instance *usb9pfs_alloc_instance(void)
+{
+ struct f_usb9pfs_opts *usb9pfs_opts;
+
+ usb9pfs_opts = kzalloc(sizeof(*usb9pfs_opts), GFP_KERNEL);
+ if (!usb9pfs_opts)
+ return ERR_PTR(-ENOMEM);
+
+ mutex_init(&usb9pfs_opts->lock);
+
+ usb9pfs_opts->func_inst.free_func_inst = usb9pfs_free_instance;
+
+ usb9pfs_opts->buflen = DEFAULT_BUFLEN;
+
+ config_group_init_type_name(&usb9pfs_opts->func_inst.group, "",
+ &usb9pfs_func_type);
+
+ return &usb9pfs_opts->func_inst;
+}
+DECLARE_USB_FUNCTION(usb9pfs, usb9pfs_alloc_instance, usb9pfs_alloc);
+
+int __init usb9pfs_modinit(void)
+{
+ int ret;
+
+ INIT_LIST_HEAD(&usbg_function_list);
+
+ ret = usb_function_register(&usb9pfsusb_func);
+ if (!ret)
+ v9fs_register_trans(&p9_usbg_trans);
+
+ return ret;
+}
+
+void __exit usb9pfs_modexit(void)
+{
+ usb_function_unregister(&usb9pfsusb_func);
+ v9fs_unregister_trans(&p9_usbg_trans);
+}
+
+module_init(usb9pfs_modinit);
+module_exit(usb9pfs_modexit);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("USB gadget 9pfs transport");
+MODULE_AUTHOR("Michael Grzeschik");
--
2.39.2
On Tue, Jan 16, 2024 at 02:49:41AM +0100, Michael Grzeschik wrote:
> Add the new gadget function for 9pfs transport. This function is
> defining an simple 9pfs transport interface that consists of one in and
> one out endpoint. The endpoints transmit and receive the 9pfs protocol
> payload when mounting a 9p filesystem over usb.
>
> Signed-off-by: Michael Grzeschik <[email protected]>
> ---
> Documentation/filesystems/9p.rst | 12 +
> drivers/usb/gadget/Kconfig | 11 +
> drivers/usb/gadget/function/Makefile | 2 +
> drivers/usb/gadget/function/f_9pfs.c | 849 +++++++++++++++++++++++++++++++++++
> 4 files changed, 874 insertions(+)
>
> diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
> index b3592bcb0f966..72cdecaef6aa9 100644
> --- a/drivers/usb/gadget/Kconfig
> +++ b/drivers/usb/gadget/Kconfig
> @@ -153,6 +153,10 @@ config USB_F_ACM
> config USB_F_SS_LB
> tristate
>
> +config USB_F_9PFS
> + tristate
> + select NET_9P
> +
> config USB_U_SERIAL
> tristate
>
> @@ -363,6 +367,13 @@ config USB_CONFIGFS_F_LB_SS
> test software, like the "usbtest" driver, to put your hardware
> and its driver through a basic set of functional tests.
>
> +config USB_CONFIGFS_F_9PFS
> + bool "9pfs over usb gadget"
> + depends on USB_CONFIGFS
> + select USB_F_9PFS
> + help
> + 9pfs support for usb gadget
This may be a dumb question, but what is the purpose of this CONFIG
symbol? It doesn't get used by any of the patches in this series, as
far as I can see.
Alan Stern
Alan Stern wrote on Mon, Jan 15, 2024 at 10:17:34PM -0500:
> > diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
> > index b3592bcb0f966..72cdecaef6aa9 100644
> > --- a/drivers/usb/gadget/Kconfig
> > +++ b/drivers/usb/gadget/Kconfig
> > @@ -153,6 +153,10 @@ config USB_F_ACM
> > config USB_F_SS_LB
> > tristate
> >
> > +config USB_F_9PFS
> > + tristate
> > + select NET_9P
> > +
> > config USB_U_SERIAL
> > tristate
> >
> > @@ -363,6 +367,13 @@ config USB_CONFIGFS_F_LB_SS
> > test software, like the "usbtest" driver, to put your hardware
> > and its driver through a basic set of functional tests.
> >
> > +config USB_CONFIGFS_F_9PFS
> > + bool "9pfs over usb gadget"
> > + depends on USB_CONFIGFS
> > + select USB_F_9PFS
> > + help
> > + 9pfs support for usb gadget
>
> This may be a dumb question, but what is the purpose of this CONFIG
> symbol? It doesn't get used by any of the patches in this series, as
> far as I can see.
USB_F_9PFS cannot be selected directly in menuconfig so this allows
configuring the build option -- that appears to be how the other usb
gadgets are configured so I assume it's done that way for consistency
more than out of necessity (I don't see a problem in making the build
system use USB_CONFIGFS_F_9PFS directly, it'd just be different from the
rest)
--
Dominique Martinet | Asmadeus
Hi Michael,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 052d534373b7ed33712a63d5e17b2b6cdbce84fd]
url: https://github.com/intel-lab-lkp/linux/commits/Michael-Grzeschik/usb-gadget-function-9pfs/20240116-095914
base: 052d534373b7ed33712a63d5e17b2b6cdbce84fd
patch link: https://lore.kernel.org/r/20240116-ml-topic-u9p-v1-1-ad8c306f9a4e%40pengutronix.de
patch subject: [PATCH 1/3] usb: gadget: function: 9pfs
config: arm64-allyesconfig (https://download.01.org/0day-ci/archive/20240116/[email protected]/config)
compiler: clang version 18.0.0git (https://github.com/llvm/llvm-project 9bde5becb44ea071f5e1fa1f5d4071dc8788b18c)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240116/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> drivers/usb/gadget/function/f_9pfs.c:197:4: warning: format specifies type 'unsigned int' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
196 | p9_debug(P9_DEBUG_TRANS, "mux %p got %u bytes\n", usb9pfs,
| ~~
| %zu
197 | rc.capacity - rc.offset);
| ^~~~~~~~~~~~~~~~~~~~~~~
include/net/9p/9p.h:55:36: note: expanded from macro 'p9_debug'
55 | _p9_debug(level, __func__, fmt, ##__VA_ARGS__)
| ~~~ ^~~~~~~~~~~
>> drivers/usb/gadget/function/f_9pfs.c:286:6: warning: no previous prototype for function 'disable_endpoints' [-Wmissing-prototypes]
286 | void disable_endpoints(struct usb_composite_dev *cdev,
| ^
drivers/usb/gadget/function/f_9pfs.c:286:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
286 | void disable_endpoints(struct usb_composite_dev *cdev,
| ^
| static
>> drivers/usb/gadget/function/f_9pfs.c:825:12: warning: no previous prototype for function 'usb9pfs_modinit' [-Wmissing-prototypes]
825 | int __init usb9pfs_modinit(void)
| ^
drivers/usb/gadget/function/f_9pfs.c:825:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
825 | int __init usb9pfs_modinit(void)
| ^
| static
>> drivers/usb/gadget/function/f_9pfs.c:838:13: warning: no previous prototype for function 'usb9pfs_modexit' [-Wmissing-prototypes]
838 | void __exit usb9pfs_modexit(void)
| ^
drivers/usb/gadget/function/f_9pfs.c:838:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
838 | void __exit usb9pfs_modexit(void)
| ^
| static
4 warnings generated.
vim +197 drivers/usb/gadget/function/f_9pfs.c
184
185 static struct p9_req_t *usb9pfs_rx_header(struct f_usb9pfs *usb9pfs, struct usb_request *req)
186 {
187 struct p9_req_t *p9_rx_req;
188 struct p9_fcall rc;
189 int ret;
190
191 /* start by reading header */
192 rc.sdata = req->buf;
193 rc.offset = 0;
194 rc.capacity = rc.size = P9_HDRSZ;
195
196 p9_debug(P9_DEBUG_TRANS, "mux %p got %u bytes\n", usb9pfs,
> 197 rc.capacity - rc.offset);
198
199 ret = p9_parse_header(&rc, &rc.size, NULL, NULL, 0);
200 if (ret) {
201 p9_debug(P9_DEBUG_ERROR,
202 "error parsing header: %d\n", ret);
203 return NULL;
204 }
205
206 p9_debug(P9_DEBUG_TRANS,
207 "mux %p pkt: size: %d bytes tag: %d\n",
208 usb9pfs, rc.size, rc.tag);
209
210 p9_rx_req = p9_tag_lookup(usb9pfs->client, rc.tag);
211 if (!p9_rx_req || (p9_rx_req->status != REQ_STATUS_SENT)) {
212 p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n", rc.tag);
213 return NULL;
214 }
215
216 if (rc.size > p9_rx_req->rc.capacity) {
217 p9_debug(P9_DEBUG_ERROR,
218 "requested packet size too big: %d for tag %d with capacity %zd\n",
219 rc.size, rc.tag, p9_rx_req->rc.capacity);
220 return NULL;
221 }
222
223 if (!p9_rx_req->rc.sdata) {
224 p9_debug(P9_DEBUG_ERROR,
225 "No recv fcall for tag %d (req %p), disconnecting!\n",
226 rc.tag, p9_rx_req);
227 p9_req_put(usb9pfs->client, p9_rx_req);
228 return NULL;
229 }
230
231 return p9_rx_req;
232 }
233
234 static void usb9pfs_rx_complete(struct usb_ep *ep, struct usb_request *req)
235 {
236 struct f_usb9pfs *usb9pfs = ep->driver_data;
237 struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
238 struct p9_req_t *p9_rx_req;
239 unsigned long flags;
240
241 switch (req->status) {
242 case 0: /* normal completion? */
243 spin_lock_irqsave(&usb9pfs->req_lock, flags);
244 p9_rx_req = usb9pfs_rx_header(usb9pfs, req);
245 if (!p9_rx_req) {
246 spin_unlock_irqrestore(&usb9pfs->req_lock, flags);
247 goto free_req;
248 }
249
250 memcpy(p9_rx_req->rc.sdata, req->buf, req->actual);
251 p9_rx_req->rc.size = req->actual;
252
253 p9_client_cb(usb9pfs->client, p9_rx_req, REQ_STATUS_RCVD);
254 p9_req_put(usb9pfs->client, p9_rx_req);
255
256 usb9pfs->p9_tx_req = NULL;
257
258 usb9pfs_transmit(usb9pfs);
259
260 spin_unlock_irqrestore(&usb9pfs->req_lock, flags);
261
262 return;
263 free_req:
264 default:
265 dev_err(&cdev->gadget->dev, "%s usb9pfs complete --> %d, %d/%d\n",
266 ep->name, req->status, req->actual, req->length);
267 usb_ep_free_request(ep == usb9pfs->in_ep ?
268 usb9pfs->out_ep : usb9pfs->in_ep,
269 req->context);
270 free_ep_req(ep, req);
271 return;
272 }
273
274 p9_client_cb(usb9pfs->client, p9_rx_req, REQ_STATUS_ERROR);
275 }
276
277 static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
278 {
279 int value;
280
281 value = usb_ep_disable(ep);
282 if (value < 0)
283 dev_info(&cdev->gadget->dev, "disable %s --> %d\n", ep->name, value);
284 }
285
> 286 void disable_endpoints(struct usb_composite_dev *cdev,
287 struct usb_ep *in, struct usb_ep *out,
288 struct usb_ep *iso_in, struct usb_ep *iso_out)
289 {
290 disable_ep(cdev, in);
291 disable_ep(cdev, out);
292 }
293
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Michael Grzeschik wrote on Tue, Jan 16, 2024 at 02:49:40AM +0100:
> This series is adding support to mount 9pfs exported filesystems via the
> usb gadget interface. It also includes tools and descriptions on how to
> translate an tcp 9pfs and use it via the usb interface.
So I didn't have time to look at everything through, just want to make
sure, this series allows sharing data from an usb gadget (e.g. some
device with storage) over 9p as an alternative to things like MTP ?
I don't quite understand what the forwarder and diod have to do with
this; you're emulating a fake usb device with the forwarder that just
transmits requests to diod as backend implementation?
But 'usb.core.find(idVendor=0x1D6B, idProduct=0x0109)' looks like it's
searching for a real device not creating one, so that doesn't seem to
match up...
If you have any background information on where you're coming from and
where this is headed it'd be great to include in the cover letter.
While I had a quick look I'll spare you a second mail for the first
patch:
Michael Grzeschik wrote on Tue, Jan 16, 2024 at 02:49:41AM +0100:
> +static struct p9_trans_module p9_usbg_trans = {
> + .name = "usbg",
> + .create = p9_usbg_create,
> + .close = p9_usbg_close,
> + .request = p9_usbg_request,
> + .cancel = p9_usbg_cancel,
> + .owner = THIS_MODULE,
> +};
This is missing a MODULE_ALIAS_9P("usbg") if you want the module to
auto-load on `mount -t trans=usbg` -- assuming this can build as a
module.
I'm also a bit worried that this net/9p-centric code is now also split
with drivers/usb/gadget/function/f_9pfs.c and I'll bet you the build
will break once in a while when we update global 9p client.c or
similar -- I'd be more comfortable having a net/9p/trans_usbg.c or
equivalent if possible.
Is there a reason this has to be in the usb gadget tree?
(Well, I assume from the usb gadget point of view, it's reasonable to
similarily prefer this code to stay close to drivers/usb/gadget..)
Thanks,
--
Dominique Martinet | Asmadeus
Hi Michael,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 052d534373b7ed33712a63d5e17b2b6cdbce84fd]
url: https://github.com/intel-lab-lkp/linux/commits/Michael-Grzeschik/usb-gadget-function-9pfs/20240116-095914
base: 052d534373b7ed33712a63d5e17b2b6cdbce84fd
patch link: https://lore.kernel.org/r/20240116-ml-topic-u9p-v1-1-ad8c306f9a4e%40pengutronix.de
patch subject: [PATCH 1/3] usb: gadget: function: 9pfs
config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20240116/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240116/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from drivers/usb/gadget/function/f_9pfs.c:21:
drivers/usb/gadget/function/f_9pfs.c: In function 'usb9pfs_rx_header':
>> drivers/usb/gadget/function/f_9pfs.c:196:34: warning: format '%u' expects argument of type 'unsigned int', but argument 5 has type 'size_t' {aka 'long unsigned int'} [-Wformat=]
196 | p9_debug(P9_DEBUG_TRANS, "mux %p got %u bytes\n", usb9pfs,
| ^~~~~~~~~~~~~~~~~~~~~~~
197 | rc.capacity - rc.offset);
| ~~~~~~~~~~~~~~~~~~~~~~~
| |
| size_t {aka long unsigned int}
include/net/9p/9p.h:55:36: note: in definition of macro 'p9_debug'
55 | _p9_debug(level, __func__, fmt, ##__VA_ARGS__)
| ^~~
drivers/usb/gadget/function/f_9pfs.c:196:47: note: format string is defined here
196 | p9_debug(P9_DEBUG_TRANS, "mux %p got %u bytes\n", usb9pfs,
| ~^
| |
| unsigned int
| %lu
drivers/usb/gadget/function/f_9pfs.c: At top level:
>> drivers/usb/gadget/function/f_9pfs.c:286:6: warning: no previous prototype for 'disable_endpoints' [-Wmissing-prototypes]
286 | void disable_endpoints(struct usb_composite_dev *cdev,
| ^~~~~~~~~~~~~~~~~
>> drivers/usb/gadget/function/f_9pfs.c:825:12: warning: no previous prototype for 'usb9pfs_modinit' [-Wmissing-prototypes]
825 | int __init usb9pfs_modinit(void)
| ^~~~~~~~~~~~~~~
>> drivers/usb/gadget/function/f_9pfs.c:838:13: warning: no previous prototype for 'usb9pfs_modexit' [-Wmissing-prototypes]
838 | void __exit usb9pfs_modexit(void)
| ^~~~~~~~~~~~~~~
vim +196 drivers/usb/gadget/function/f_9pfs.c
184
185 static struct p9_req_t *usb9pfs_rx_header(struct f_usb9pfs *usb9pfs, struct usb_request *req)
186 {
187 struct p9_req_t *p9_rx_req;
188 struct p9_fcall rc;
189 int ret;
190
191 /* start by reading header */
192 rc.sdata = req->buf;
193 rc.offset = 0;
194 rc.capacity = rc.size = P9_HDRSZ;
195
> 196 p9_debug(P9_DEBUG_TRANS, "mux %p got %u bytes\n", usb9pfs,
197 rc.capacity - rc.offset);
198
199 ret = p9_parse_header(&rc, &rc.size, NULL, NULL, 0);
200 if (ret) {
201 p9_debug(P9_DEBUG_ERROR,
202 "error parsing header: %d\n", ret);
203 return NULL;
204 }
205
206 p9_debug(P9_DEBUG_TRANS,
207 "mux %p pkt: size: %d bytes tag: %d\n",
208 usb9pfs, rc.size, rc.tag);
209
210 p9_rx_req = p9_tag_lookup(usb9pfs->client, rc.tag);
211 if (!p9_rx_req || (p9_rx_req->status != REQ_STATUS_SENT)) {
212 p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n", rc.tag);
213 return NULL;
214 }
215
216 if (rc.size > p9_rx_req->rc.capacity) {
217 p9_debug(P9_DEBUG_ERROR,
218 "requested packet size too big: %d for tag %d with capacity %zd\n",
219 rc.size, rc.tag, p9_rx_req->rc.capacity);
220 return NULL;
221 }
222
223 if (!p9_rx_req->rc.sdata) {
224 p9_debug(P9_DEBUG_ERROR,
225 "No recv fcall for tag %d (req %p), disconnecting!\n",
226 rc.tag, p9_rx_req);
227 p9_req_put(usb9pfs->client, p9_rx_req);
228 return NULL;
229 }
230
231 return p9_rx_req;
232 }
233
234 static void usb9pfs_rx_complete(struct usb_ep *ep, struct usb_request *req)
235 {
236 struct f_usb9pfs *usb9pfs = ep->driver_data;
237 struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
238 struct p9_req_t *p9_rx_req;
239 unsigned long flags;
240
241 switch (req->status) {
242 case 0: /* normal completion? */
243 spin_lock_irqsave(&usb9pfs->req_lock, flags);
244 p9_rx_req = usb9pfs_rx_header(usb9pfs, req);
245 if (!p9_rx_req) {
246 spin_unlock_irqrestore(&usb9pfs->req_lock, flags);
247 goto free_req;
248 }
249
250 memcpy(p9_rx_req->rc.sdata, req->buf, req->actual);
251 p9_rx_req->rc.size = req->actual;
252
253 p9_client_cb(usb9pfs->client, p9_rx_req, REQ_STATUS_RCVD);
254 p9_req_put(usb9pfs->client, p9_rx_req);
255
256 usb9pfs->p9_tx_req = NULL;
257
258 usb9pfs_transmit(usb9pfs);
259
260 spin_unlock_irqrestore(&usb9pfs->req_lock, flags);
261
262 return;
263 free_req:
264 default:
265 dev_err(&cdev->gadget->dev, "%s usb9pfs complete --> %d, %d/%d\n",
266 ep->name, req->status, req->actual, req->length);
267 usb_ep_free_request(ep == usb9pfs->in_ep ?
268 usb9pfs->out_ep : usb9pfs->in_ep,
269 req->context);
270 free_ep_req(ep, req);
271 return;
272 }
273
274 p9_client_cb(usb9pfs->client, p9_rx_req, REQ_STATUS_ERROR);
275 }
276
277 static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
278 {
279 int value;
280
281 value = usb_ep_disable(ep);
282 if (value < 0)
283 dev_info(&cdev->gadget->dev, "disable %s --> %d\n", ep->name, value);
284 }
285
> 286 void disable_endpoints(struct usb_composite_dev *cdev,
287 struct usb_ep *in, struct usb_ep *out,
288 struct usb_ep *iso_in, struct usb_ep *iso_out)
289 {
290 disable_ep(cdev, in);
291 disable_ep(cdev, out);
292 }
293
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
On Tue, Jan 16, 2024 at 01:04:08PM +0900, Dominique Martinet wrote:
> Alan Stern wrote on Mon, Jan 15, 2024 at 10:17:34PM -0500:
> > > diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
> > > index b3592bcb0f966..72cdecaef6aa9 100644
> > > --- a/drivers/usb/gadget/Kconfig
> > > +++ b/drivers/usb/gadget/Kconfig
> > > @@ -153,6 +153,10 @@ config USB_F_ACM
> > > config USB_F_SS_LB
> > > tristate
> > >
> > > +config USB_F_9PFS
> > > + tristate
> > > + select NET_9P
> > > +
> > > config USB_U_SERIAL
> > > tristate
> > >
> > > @@ -363,6 +367,13 @@ config USB_CONFIGFS_F_LB_SS
> > > test software, like the "usbtest" driver, to put your hardware
> > > and its driver through a basic set of functional tests.
> > >
> > > +config USB_CONFIGFS_F_9PFS
> > > + bool "9pfs over usb gadget"
> > > + depends on USB_CONFIGFS
> > > + select USB_F_9PFS
> > > + help
> > > + 9pfs support for usb gadget
> >
> > This may be a dumb question, but what is the purpose of this CONFIG
> > symbol? It doesn't get used by any of the patches in this series, as
> > far as I can see.
>
> USB_F_9PFS cannot be selected directly in menuconfig so this allows
> configuring the build option -- that appears to be how the other usb
> gadgets are configured so I assume it's done that way for consistency
> more than out of necessity (I don't see a problem in making the build
> system use USB_CONFIGFS_F_9PFS directly, it'd just be different from the
> rest)
Oh, I see. Patch 2/3 selects USB_F_9PFS directly in legacy/Kconfig
without touching USB_CONFIGFS_F_9PFS. Thus they need to be separate
symbols.
That explains it, thanks.
Alan Stern
On Tue, 2024-01-16 at 20:45 +0900, Dominique Martinet wrote:
> Michael Grzeschik wrote on Tue, Jan 16, 2024 at 02:49:40AM +0100:
> > This series is adding support to mount 9pfs exported filesystems via the
> > usb gadget interface. It also includes tools and descriptions on how to
> > translate an tcp 9pfs and use it via the usb interface.
>
> So I didn't have time to look at everything through, just want to make
> sure, this series allows sharing data from an usb gadget (e.g. some
> device with storage) over 9p as an alternative to things like MTP ?
It's the other way around. :) The USB host exports a filesystem, while the
gadget on the USB device side makes it mountable. Our main use-case is to use it
as an alternative to NFS root booting during the development of embedded Linux
devices. NFS root works in many cases, but has some downsides, which make it
cumbersome to use in more and more cases.
NFS root needs correctly configured Ethernet interfaces on both the development
host and the target device. On the target, this can interfere with the network
configuration that is used for the normal device operation (DHCP client, ..).
For the host, configuring a NFS (and perhaps DHCP) server can be an obstacle.
For target devices which don't have a real Ethernet interface, NFS root would
also work with the USB Ethernet gadget, but this increases the complexity
further.
As many embedded boards have a USB device port anyway, which is used during
development for uploading the boot-loader and to flash filesystem images (ie.
via the fastboot protocol), we want to just reuse that single data cable to
allow access to the root filesystem as well.
Compared to flashing images, using a network filesystem like NFS and 9P reduces
the time between compiling on the host and running the binary on the target, as
no flash and reboot cycle is needed. That can get rid of many minutes of waiting
over a day. :)
> I don't quite understand what the forwarder and diod have to do with
> this; you're emulating a fake usb device with the forwarder that just
> transmits requests to diod as backend implementation?
> But 'usb.core.find(idVendor=0x1D6B, idProduct=0x0109)' looks like it's
> searching for a real device not creating one, so that doesn't seem to
> match up...
diod (9pfs server) and the forwarder are on the development host, where the root
filesystem is actually stored. The gadget is initialized during boot (or later)
on the embedded board. Then the forwarder will find it on the USB bus and start
forwarding requests.
It may seem a bit unusual that in this case the requests come from the device
and are handled by the host. The reason is that USB device ports are normally
not available on PCs, so a connection in the other direction would not work.
In the future, the functionality of the forwarder could be integrated into the
9pfs server. Alternatively, an improved forwarder could also react to udev
events of gadgets showing up and forward them to different 9PFS server over the
network (when you have multiple target devices connected to one USB host).
Perhaps, the inverse setup (9PFS server on the USB gadget side, mounted on a PC)
also would be useful in the future and could share some of this code. Then,
you'd have an alternative to MTP.
> If you have any background information on where you're coming from and
> where this is headed it'd be great to include in the cover letter.
Yes. Probably also in Documentation/.
Thanks,
Jan
Hi Michael,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 052d534373b7ed33712a63d5e17b2b6cdbce84fd]
url: https://github.com/intel-lab-lkp/linux/commits/Michael-Grzeschik/usb-gadget-function-9pfs/20240116-095914
base: 052d534373b7ed33712a63d5e17b2b6cdbce84fd
patch link: https://lore.kernel.org/r/20240116-ml-topic-u9p-v1-2-ad8c306f9a4e%40pengutronix.de
patch subject: [PATCH 2/3] usb: gadget: legacy: add 9pfs multi gadget
config: alpha-kismet-CONFIG_NET_9P-CONFIG_USB_9PFS-0-0 (https://download.01.org/0day-ci/archive/20240117/[email protected]/config)
reproduce: (https://download.01.org/0day-ci/archive/20240117/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
kismet warnings: (new ones prefixed by >>)
>> kismet: WARNING: unmet direct dependencies detected for NET_9P when selected by USB_9PFS
WARNING: unmet direct dependencies detected for NET_9P
Depends on [n]: NET [=n]
Selected by [y]:
- USB_F_9PFS [=y] && USB_SUPPORT [=y] && USB_GADGET [=y]
- USB_9PFS [=y] && USB_SUPPORT [=y] && USB_GADGET [=y]
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Hi Michael,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 052d534373b7ed33712a63d5e17b2b6cdbce84fd]
url: https://github.com/intel-lab-lkp/linux/commits/Michael-Grzeschik/usb-gadget-function-9pfs/20240116-095914
base: 052d534373b7ed33712a63d5e17b2b6cdbce84fd
patch link: https://lore.kernel.org/r/20240116-ml-topic-u9p-v1-1-ad8c306f9a4e%40pengutronix.de
patch subject: [PATCH 1/3] usb: gadget: function: 9pfs
config: alpha-kismet-CONFIG_NET_9P-CONFIG_USB_F_9PFS-0-0 (https://download.01.org/0day-ci/archive/20240117/[email protected]/config)
reproduce: (https://download.01.org/0day-ci/archive/20240117/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
kismet warnings: (new ones prefixed by >>)
>> kismet: WARNING: unmet direct dependencies detected for NET_9P when selected by USB_F_9PFS
WARNING: unmet direct dependencies detected for NET_9P
Depends on [n]: NET [=n]
Selected by [y]:
- USB_F_9PFS [=y] && USB_SUPPORT [=y] && USB_GADGET [=y]
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Le 16/01/2024 à 02:49, Michael Grzeschik a écrit :
> Add the new gadget function for 9pfs transport. This function is
> defining an simple 9pfs transport interface that consists of one in and
> one out endpoint. The endpoints transmit and receive the 9pfs protocol
> payload when mounting a 9p filesystem over usb.
>
> Signed-off-by: Michael Grzeschik <[email protected]>
> ---
> Documentation/filesystems/9p.rst | 12 +
> drivers/usb/gadget/Kconfig | 11 +
> drivers/usb/gadget/function/Makefile | 2 +
> drivers/usb/gadget/function/f_9pfs.c | 849 +++++++++++++++++++++++++++++++++++
> 4 files changed, 874 insertions(+)
..
> +static int alloc_requests(struct usb_composite_dev *cdev,
> + struct f_usb9pfs *usb9pfs)
> +{
> + int result = 0;
> +
> + usb9pfs->in_req = usb_ep_alloc_request(usb9pfs->in_ep, GFP_ATOMIC);
> + if (!usb9pfs->in_req)
> + goto fail;
> +
> + usb9pfs->out_req = usb9pfs_alloc_ep_req(usb9pfs->out_ep, usb9pfs->buflen);
> + if (!usb9pfs->out_req)
> + goto fail_in;
> +
> + usb9pfs->in_req->complete = usb9pfs_tx_complete;
> + usb9pfs->out_req->complete = usb9pfs_rx_complete;
> +
> + /* length will be set in complete routine */
> + usb9pfs->in_req->context = usb9pfs->out_req->context = usb9pfs;
> +
> + return 0;
> +
> +fail_in:
> + usb_ep_free_request(usb9pfs->in_ep, usb9pfs->in_req);
> +fail:
> + return result;
'result' is never overwritten, so we always return 0.
Is it on purpose?
> +}
..
> +static struct usb_function *usb9pfs_alloc(struct usb_function_instance *fi)
> +{
> + struct f_usb9pfs_opts *usb9pfs_opts;
> + struct f_usb9pfs *usb9pfs;
> +
> + usb9pfs = kzalloc(sizeof(*usb9pfs), GFP_KERNEL);
> + if (!usb9pfs)
> + return ERR_PTR(-ENOMEM);
> +
> + usb9pfs_opts = container_of(fi, struct f_usb9pfs_opts, func_inst);
> +
> + mutex_lock(&usb9pfs_opts->lock);
> + usb9pfs_opts->refcnt++;
> + mutex_unlock(&usb9pfs_opts->lock);
> +
> + usb9pfs->buflen = usb9pfs_opts->buflen;
> +
> + usb9pfs->function.name = "usb9pfs";
> + usb9pfs->function.bind = usb9pfs_func_bind;
> + usb9pfs->function.set_alt = usb9pfs_set_alt;
> + usb9pfs->function.disable = usb9pfs_disable;
> + usb9pfs->function.strings = usb9pfs_strings;
> +
> + usb9pfs->function.free_func = usb9pfs_free_func;
> +
> + mutex_lock(&usb9pfs_ida_lock);
> +
> + usb9pfs->index = ida_simple_get(&usb9pfs_ida, 0, 100, GFP_KERNEL);
This API will soon be removed.
Please use ida_alloc_max() instead.
Also, there is no corresponding ida_free()? (or isa_simple_remove())
> + if (usb9pfs->index < 0) {
> + struct usb_function *ret = ERR_PTR(usb9pfs->index);
> +
> + kfree(usb9pfs);
> + mutex_unlock(&usb9pfs_ida_lock);
> + return ret;
> + }
> +
> + mutex_unlock(&usb9pfs_ida_lock);
> +
> + usb9pfs->tag = kasprintf(GFP_KERNEL, "%s%d", usb9pfs->function.name,
> + usb9pfs->index);
Same here, it seems to be freed nowhere?
I think that both should be added in usb9pfs_free_func().
CJ
> +
> + INIT_LIST_HEAD(&usb9pfs->function_list);
> +
> + mutex_lock(&usb9pfs_lock);
> + list_add_tail(&usb9pfs->function_list, &usbg_function_list);
> + mutex_unlock(&usb9pfs_lock);
> +
> + return &usb9pfs->function;
> +}
..
Hi Michael,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 052d534373b7ed33712a63d5e17b2b6cdbce84fd]
url: https://github.com/intel-lab-lkp/linux/commits/Michael-Grzeschik/usb-gadget-function-9pfs/20240116-095914
base: 052d534373b7ed33712a63d5e17b2b6cdbce84fd
patch link: https://lore.kernel.org/r/20240116-ml-topic-u9p-v1-1-ad8c306f9a4e%40pengutronix.de
patch subject: [PATCH 1/3] usb: gadget: function: 9pfs
config: microblaze-randconfig-r132-20240117 (https://download.01.org/0day-ci/archive/20240117/[email protected]/config)
compiler: microblaze-linux-gcc (GCC) 13.2.0
reproduce: (https://download.01.org/0day-ci/archive/20240117/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
>> drivers/usb/gadget/function/f_9pfs.c:825:12: sparse: sparse: symbol 'usb9pfs_modinit' was not declared. Should it be static?
>> drivers/usb/gadget/function/f_9pfs.c:838:13: sparse: sparse: symbol 'usb9pfs_modexit' was not declared. Should it be static?
vim +/usb9pfs_modinit +825 drivers/usb/gadget/function/f_9pfs.c
824
> 825 int __init usb9pfs_modinit(void)
826 {
827 int ret;
828
829 INIT_LIST_HEAD(&usbg_function_list);
830
831 ret = usb_function_register(&usb9pfsusb_func);
832 if (!ret)
833 v9fs_register_trans(&p9_usbg_trans);
834
835 return ret;
836 }
837
> 838 void __exit usb9pfs_modexit(void)
839 {
840 usb_function_unregister(&usb9pfsusb_func);
841 v9fs_unregister_trans(&p9_usbg_trans);
842 }
843
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Jan Lübbe wrote on Tue, Jan 16, 2024 at 04:51:41PM +0100:
> > So I didn't have time to look at everything through, just want to make
> > sure, this series allows sharing data from an usb gadget (e.g. some
> > device with storage) over 9p as an alternative to things like MTP ?
>
> It's the other way around. :) The USB host exports a filesystem, while the
> gadget on the USB device side makes it mountable. Our main use-case is to use it
> as an alternative to NFS root booting during the development of embedded Linux
> devices. NFS root works in many cases, but has some downsides, which make it
> cumbersome to use in more and more cases.
Oh!
Okay, this makes a lot more sense. And that'll need a bit more
explanations in the commits & Documentation/ as you've concluded :)
> NFS root needs correctly configured Ethernet interfaces on both the development
> host and the target device. On the target, this can interfere with the network
> configuration that is used for the normal device operation (DHCP client, ...).
> For the host, configuring a NFS (and perhaps DHCP) server can be an obstacle.
>
> For target devices which don't have a real Ethernet interface, NFS root would
> also work with the USB Ethernet gadget, but this increases the complexity
> further.
>
> As many embedded boards have a USB device port anyway, which is used during
> development for uploading the boot-loader and to flash filesystem images (i.e.
> via the fastboot protocol), we want to just reuse that single data cable to
> allow access to the root filesystem as well.
>
> Compared to flashing images, using a network filesystem like NFS and 9P reduces
> the time between compiling on the host and running the binary on the target, as
> no flash and reboot cycle is needed. That can get rid of many minutes of waiting
> over a day. :)
My other hat is on embedded development (dayjob at Atmark Techno[1], the
only english page linked is about 4 years out of date but I guess it's
better than no page at all), so I can understand where you're coming
from -- thanks for the background.
[1] https://www.atmark-techno.com/english
That means I'll actually want to test this, but kind of always busy so
it might take a few weeks...
Or better, do you happen to know if qemu can create a USB controller
that supports OTG so it'll be easy to test for folks with no such
hardware?
We've got enough 9p protocols that aren't actually tested on a regular
basis, it'd be great if we could have something that can run anywhere.
> diod (9pfs server) and the forwarder are on the development host, where the root
> filesystem is actually stored. The gadget is initialized during boot (or later)
> on the embedded board. Then the forwarder will find it on the USB bus and start
> forwarding requests.
>
> It may seem a bit unusual that in this case the requests come from the device
> and are handled by the host. The reason is that USB device ports are normally
> not available on PCs, so a connection in the other direction would not work.
Right, most host PCs won't have OTG available...
I was also perplexed by the linux foundation (0x1d6b):0x0109 id, that
might be clearer once it's properly documented -- I'll let someone from
the usb side chime on this as I have no idea what's appropriate.
> In the future, the functionality of the forwarder could be integrated into the
> 9pfs server. Alternatively, an improved forwarder could also react to udev
> events of gadgets showing up and forward them to different 9PFS server over the
> network (when you have multiple target devices connected to one USB host).
Plenty of potential work ahead :)
Frankly at this stage I don't think it's much simpler than e.g. CDC
ethernet gadget and mounting nfs over tcp, but with further improvements
it can definitely get simpler.
> Perhaps, the inverse setup (9PFS server on the USB gadget side, mounted on a PC)
> also would be useful in the future and could share some of this code. Then,
> you'd have an alternative to MTP.
(Yeah, I'm not actively looking for that -- was just asking because MTP
has been kind of dead lately and I'm not aware of any potential
alternative, but I didn't go looking for them either -- let's leave that
to later)
--
Dominique Martinet | Asmadeus
Hi,
W dniu 17.01.2024 o 11:54, Dominique Martinet pisze:
> Jan Lübbe wrote on Tue, Jan 16, 2024 at 04:51:41PM +0100:
>>> So I didn't have time to look at everything through, just want to make
>>> sure, this series allows sharing data from an usb gadget (e.g. some
>>> device with storage) over 9p as an alternative to things like MTP ?
>>
>> It's the other way around. :) The USB host exports a filesystem, while the
>> gadget on the USB device side makes it mountable. Our main use-case is to use it
>> as an alternative to NFS root booting during the development of embedded Linux
>> devices. NFS root works in many cases, but has some downsides, which make it
>> cumbersome to use in more and more cases.
>
> Oh!
> Okay, this makes a lot more sense. And that'll need a bit more
> explanations in the commits & Documentation/ as you've concluded :)
>
>
>> NFS root needs correctly configured Ethernet interfaces on both the development
>> host and the target device. On the target, this can interfere with the network
>> configuration that is used for the normal device operation (DHCP client, ...).
>> For the host, configuring a NFS (and perhaps DHCP) server can be an obstacle.
>>
>> For target devices which don't have a real Ethernet interface, NFS root would
>> also work with the USB Ethernet gadget, but this increases the complexity
>> further.
>>
>> As many embedded boards have a USB device port anyway, which is used during
>> development for uploading the boot-loader and to flash filesystem images (i.e.
>> via the fastboot protocol), we want to just reuse that single data cable to
>> allow access to the root filesystem as well.
>>
>> Compared to flashing images, using a network filesystem like NFS and 9P reduces
>> the time between compiling on the host and running the binary on the target, as
>> no flash and reboot cycle is needed. That can get rid of many minutes of waiting
>> over a day. :)
>
> My other hat is on embedded development (dayjob at Atmark Techno[1], the
> only english page linked is about 4 years out of date but I guess it's
> better than no page at all), so I can understand where you're coming
> from -- thanks for the background.
>
> [1] https://www.atmark-techno.com/english
>
> That means I'll actually want to test this, but kind of always busy so
> it might take a few weeks...
> Or better, do you happen to know if qemu can create a USB controller
> that supports OTG so it'll be easy to test for folks with no such
> hardware?
Maybe dummy_hcd is what you want?
Regards,
Andrzej
> We've got enough 9p protocols that aren't actually tested on a regular
> basis, it'd be great if we could have something that can run anywhere.
>
>
>> diod (9pfs server) and the forwarder are on the development host, where the root
>> filesystem is actually stored. The gadget is initialized during boot (or later)
>> on the embedded board. Then the forwarder will find it on the USB bus and start
>> forwarding requests.
>>
>> It may seem a bit unusual that in this case the requests come from the device
>> and are handled by the host. The reason is that USB device ports are normally
>> not available on PCs, so a connection in the other direction would not work.
>
> Right, most host PCs won't have OTG available...
> I was also perplexed by the linux foundation (0x1d6b):0x0109 id, that
> might be clearer once it's properly documented -- I'll let someone from
> the usb side chime on this as I have no idea what's appropriate.
>
>
>> In the future, the functionality of the forwarder could be integrated into the
>> 9pfs server. Alternatively, an improved forwarder could also react to udev
>> events of gadgets showing up and forward them to different 9PFS server over the
>> network (when you have multiple target devices connected to one USB host).
>
> Plenty of potential work ahead :)
> Frankly at this stage I don't think it's much simpler than e.g. CDC
> ethernet gadget and mounting nfs over tcp, but with further improvements
> it can definitely get simpler.
>
>
>> Perhaps, the inverse setup (9PFS server on the USB gadget side, mounted on a PC)
>> also would be useful in the future and could share some of this code. Then,
>> you'd have an alternative to MTP.
>
> (Yeah, I'm not actively looking for that -- was just asking because MTP
> has been kind of dead lately and I'm not aware of any potential
> alternative, but I didn't go looking for them either -- let's leave that
> to later)
>
On Fri, Jan 26, 2024 at 08:47:22PM +0100, Andrzej Pietrasiewicz wrote:
>Hi,
>
>W dniu 17.01.2024 o?11:54, Dominique Martinet pisze:
>>Jan L?bbe wrote on Tue, Jan 16, 2024 at 04:51:41PM +0100:
>>>>So I didn't have time to look at everything through, just want to make
>>>>sure, this series allows sharing data from an usb gadget (e.g. some
>>>>device with storage) over 9p as an alternative to things like MTP ?
>>>
>>>It's the other way around. :) The USB host exports a filesystem, while the
>>>gadget on the USB device side makes it mountable. Our main use-case is to use it
>>>as an alternative to NFS root booting during the development of embedded Linux
>>>devices. NFS root works in many cases, but has some downsides, which make it
>>>cumbersome to use in more and more cases.
>>
>>Oh!
>>Okay, this makes a lot more sense. And that'll need a bit more
>>explanations in the commits & Documentation/ as you've concluded :)
>>
>>
>>>NFS root needs correctly configured Ethernet interfaces on both the development
>>>host and the target device. On the target, this can interfere with the network
>>>configuration that is used for the normal device operation (DHCP client, ...).
>>>For the host, configuring a NFS (and perhaps DHCP) server can be an obstacle.
>>>
>>>For target devices which don't have a real Ethernet interface, NFS root would
>>>also work with the USB Ethernet gadget, but this increases the complexity
>>>further.
>>>
>>>As many embedded boards have a USB device port anyway, which is used during
>>>development for uploading the boot-loader and to flash filesystem images (i.e.
>>>via the fastboot protocol), we want to just reuse that single data cable to
>>>allow access to the root filesystem as well.
>>>
>>>Compared to flashing images, using a network filesystem like NFS and 9P reduces
>>>the time between compiling on the host and running the binary on the target, as
>>>no flash and reboot cycle is needed. That can get rid of many minutes of waiting
>>>over a day. :)
>>
>>My other hat is on embedded development (dayjob at Atmark Techno[1], the
>>only english page linked is about 4 years out of date but I guess it's
>>better than no page at all), so I can understand where you're coming
>>from -- thanks for the background.
>>
>>[1] https://www.atmark-techno.com/english
>>
>>That means I'll actually want to test this, but kind of always busy so
>>it might take a few weeks...
>>Or better, do you happen to know if qemu can create a USB controller
>>that supports OTG so it'll be easy to test for folks with no such
>>hardware?
>
>Maybe dummy_hcd is what you want?
I did a lot of testing with dummy_hcd. So this should work.
But of course testing the special case of rootfs is tricky. Since you
will have to share the gadget with qemu to boot the rootfs from
somewhere else.
Regards,
Michael
>>We've got enough 9p protocols that aren't actually tested on a regular
>>basis, it'd be great if we could have something that can run anywhere.
>>
>>
>>>diod (9pfs server) and the forwarder are on the development host, where the root
>>>filesystem is actually stored. The gadget is initialized during boot (or later)
>>>on the embedded board. Then the forwarder will find it on the USB bus and start
>>>forwarding requests.
>>>
>>>It may seem a bit unusual that in this case the requests come from the device
>>>and are handled by the host. The reason is that USB device ports are normally
>>>not available on PCs, so a connection in the other direction would not work.
>>
>>Right, most host PCs won't have OTG available...
>>I was also perplexed by the linux foundation (0x1d6b):0x0109 id, that
>>might be clearer once it's properly documented -- I'll let someone from
>>the usb side chime on this as I have no idea what's appropriate.
>>
>>
>>>In the future, the functionality of the forwarder could be integrated into the
>>>9pfs server. Alternatively, an improved forwarder could also react to udev
>>>events of gadgets showing up and forward them to different 9PFS server over the
>>>network (when you have multiple target devices connected to one USB host).
>>
>>Plenty of potential work ahead :)
>>Frankly at this stage I don't think it's much simpler than e.g. CDC
>>ethernet gadget and mounting nfs over tcp, but with further improvements
>>it can definitely get simpler.
>>
>>
>>>Perhaps, the inverse setup (9PFS server on the USB gadget side, mounted on a PC)
>>>also would be useful in the future and could share some of this code. Then,
>>>you'd have an alternative to MTP.
>>
>>(Yeah, I'm not actively looking for that -- was just asking because MTP
>>has been kind of dead lately and I'm not aware of any potential
>>alternative, but I didn't go looking for them either -- let's leave that
>>to later)
>>
>
>
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |