Skip to contentSkip to conversation

tools/usbip: persistently forward USB devices on a given bus

PATCH Superseded Lars Gunnarsson 1 participant 1 message Oct 25, 2021

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 3/3 tools/usbip: persistently forward USB devices on a given bus

To forward a remote usb device over usbip the following steps is required:

1. Execute "usbip bind" on remote end.
2. Execute "usbip attach" on local end.

These steps must be perfomed in above order and after usb device is plugged in. If the usb device is unplugged on the remote end the steps above needs to be performed again to establish the connection. This patch set implements a feature to persistently forward devices on a given bus. When using flag "-p|--persistent" on remot end, the USB device becomes exported when plugged in. When using flag "-p|--persistent" on local end, the USB device becomes imported when available on remote end. Thus it is only required to run the usbip command once on each end, in any order, to persistently forward usb devices on a given bus.

This patch extends the command "usbip attach" with flag "-p|--persistent" to poll and import a devices on given remote busid when available, instead of returning an error if the device is not exported. When the usb device is unplugged, it will start monitor the given remote bus again. Increment protocol version to 1.1.2 (0x112) since this patch affects both ends: usbipd (server) and command "usbip attach" (client).

---
v2: Change title, fix style warnings, improve feature description, refactor
cmdline flag usage.

 Documentation/usb/usbip_protocol.rst  |  56 +++++++++--
 tools/usb/usbip/configure.ac          |   2 +-
 tools/usb/usbip/libsrc/usbip_common.c |   1 +
 tools/usb/usbip/libsrc/usbip_common.h |   1 +
 tools/usb/usbip/libsrc/vhci_driver.c  |  16 ++++
 tools/usb/usbip/libsrc/vhci_driver.h  |   1 +
 tools/usb/usbip/src/usbip_attach.c    | 132 +++++++++++++++++++-------
 tools/usb/usbip/src/usbip_network.h   |   2 +
 tools/usb/usbip/src/usbipd.c          |  92 ++++++++++++++++--
 9 files changed, 251 insertions(+), 52 deletions(-)
Documentation/usb/usbip_protocol.rst +48 -8
index 2ed7d97278e8..1afe6d297efc 100644
@@ -36,12 +36,7 @@ TCP/IP connection is closed.
 
 Once the client knows the list of exported USB devices it may decide to use one
 of them. First the client opens a TCP/IP connection to the server and
-sends an OP_REQ_IMPORT packet. The server replies with OP_REP_IMPORT. If the
-import was successful the TCP/IP connection remains open and will be used
-to transfer the URB traffic between the client and the server. The client may
-send two types of packets: the USBIP_CMD_SUBMIT to submit an URB, and
-USBIP_CMD_UNLINK to unlink a previously submitted URB. The answers of the
-server may be USBIP_RET_SUBMIT and USBIP_RET_UNLINK respectively.
+sends an OP_REQ_IMPORT packet. The server replies with OP_REP_IMPORT.
 
 ::
 
@@ -55,6 +50,47 @@ server may be USBIP_RET_SUBMIT and USBIP_RET_UNLINK respectively.
           |                  OP_REP_IMPORT                  |
           | <---------------------------------------------- |
           |                                                 |
+
+The client may also request to poll for devices to become exported on a given
+busid, instead of immediately receive an error. If no device becomes exported
+within the chosen time, the server replies with timeout. The TCP/IP connection
+remains open and subsequent poll requests can be sent.
+
+::
+
+ virtual host controller                                 usb host
+      "client"                                           "server"
+  (imports USB devices)                             (exports USB devices)
+          |                                                 |
+          |                  OP_REQ_IMPORT                  |
+          | ----------------------------------------------> |
+          |                        .                        |
+          |                        :                        |
+          |                                                 |
+          |                  OP_REP_IMPORT                  |
+          | <---------------------------------------------- |
+          |                                                 |
+          |                  OP_REQ_IMPORT                  |
+          | ----------------------------------------------> |
+          |                        .                        |
+          |                        :                        |
+          |                                                 |
+          |                  OP_REP_IMPORT                  |
+          | <---------------------------------------------- |
+          |                        .                        |
+          |                        :                        |
+
+If the import was successful the TCP/IP connection remains open and will be used
+to transfer the URB traffic between the client and the server. The client may
+send two types of packets: the USBIP_CMD_SUBMIT to submit an URB, and
+USBIP_CMD_UNLINK to unlink a previously submitted URB. The answers of the
+server may be USBIP_RET_SUBMIT and USBIP_RET_UNLINK respectively.
+
+::
+
+ virtual host controller                                 usb host
+      "client"                                           "server"
+  (imports USB devices)                             (exports USB devices)
           |                                                 |
           |            USBIP_CMD_SUBMIT(seqnum = n)         |
           | ----------------------------------------------> |
@@ -137,8 +173,8 @@ byte (MSB) is stored at the lowest address.
 Protocol Version
 ================
 
-The documented USBIP version is v1.1.1. The binary representation of this
-version in message headers is 0x0111.
+The documented USBIP version is v1.1.2. The binary representation of this
+version in message headers is 0x0112.
 
 This is defined in tools/usb/usbip/configure.ac
 
@@ -248,6 +284,10 @@ OP_REQ_IMPORT:
 |           |        |            | A string closed with zero, the unused bytes       |
 |           |        |            | shall be filled with zeros.                       |
 +-----------+--------+------------+---------------------------------------------------+
+| 40        | 4      |            | poll timeout: instead of returning immediately if |
+|           |        |            | device is not available, wait until usb device    |
+|           |        |            | becomes exported or a timeout occurs.             |
++-----------+--------+------------+---------------------------------------------------+
 
 OP_REP_IMPORT:
 	Reply to import (attach) a remote USB device.
tools/usb/usbip/configure.ac +1 -1
index 607d05c5ccfd..156e42456423 100644
@@ -2,7 +2,7 @@ dnl Process this file with autoconf to produce a configure script.
 
 AC_PREREQ(2.59)
 AC_INIT([usbip-utils], [2.0], [[email protected]])
-AC_DEFINE([USBIP_VERSION], [0x00000111], [binary-coded decimal version number])
+AC_DEFINE([USBIP_VERSION], [0x00000112], [binary-coded decimal version number])
 
 CURRENT=0
 REVISION=1
tools/usb/usbip/libsrc/usbip_common.c +1 -0
index b8d7d480595a..30efb6a9f76d 100644
@@ -78,6 +78,7 @@ static struct op_common_status_string op_common_status_strings[] = {
 	{ ST_DEV_ERR,	"Device in error state" },
 	{ ST_NODEV,	"Device not found" },
 	{ ST_ERROR,	"Unexpected response" },
+	{ ST_POLL_TIMEOUT,	"Poll timeout" },
 	{ 0, NULL}
 };
 
tools/usb/usbip/libsrc/usbip_common.h +1 -0
index 13f1d4ca47c5..3df351877a33 100644
@@ -53,6 +53,7 @@
 #define ST_DEV_ERR	0x03
 #define ST_NODEV	0x04
 #define ST_ERROR	0x05
+#define ST_POLL_TIMEOUT 0x06
 
 extern int usbip_use_syslog;
 extern int usbip_use_stderr;
tools/usb/usbip/libsrc/vhci_driver.c +16 -0
index 8159fd98680b..4fc75e8bad66 100644
@@ -465,3 +465,19 @@ int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev)
 
 	return 0;
 }
+
+int usbip_vhci_get_local_busid_from(int port, char *local_busid)
+{
+	int rc = -1;
+
+	for (int i = 0; i < vhci_driver->nports; ++i) {
+		struct usbip_imported_device *idev = &vhci_driver->idev[i];
+
+		if (idev->port == port && strnlen(idev->udev.busid, SYSFS_BUS_ID_SIZE)) {
+			memcpy(local_busid, idev->udev.busid, SYSFS_BUS_ID_SIZE);
+			rc = 0;
+			break;
+		}
+	}
+	return rc;
+}
tools/usb/usbip/libsrc/vhci_driver.h +1 -0
index 6c9aca216705..4aecd4013cbe 100644
@@ -63,5 +63,6 @@ int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum,
 int usbip_vhci_detach_device(uint8_t port);
 
 int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev);
+int usbip_vhci_get_local_busid_from(int port, char *local_busid);
 
 #endif /* __VHCI_DRIVER_H */
tools/usb/usbip/src/usbip_attach.c +99 -33

215 lines not on this page · raw message

tools/usb/usbip/src/usbip_network.h +2 -0
index 83b4c5344f72..1c25d06ab688 100644
@@ -61,6 +61,7 @@ struct op_devinfo_reply {
 
 struct op_import_request {
 	char busid[SYSFS_BUS_ID_SIZE];
+	uint32_t poll_timeout_ms;
 } __attribute__((packed));
 
 struct op_import_reply {
@@ -69,6 +70,7 @@ struct op_import_reply {
 } __attribute__((packed));
 
 #define PACK_OP_IMPORT_REQUEST(pack, request)  do {\
+	(request)->poll_timeout_ms = usbip_net_pack_uint32_t(pack, (request)->poll_timeout_ms);\
 } while (0)
 
 #define PACK_OP_IMPORT_REPLY(pack, reply)  do {\
tools/usb/usbip/src/usbipd.c +82 -10

123 lines not on this page · raw message

Signature · 1 line
2.25.1