Skip to contentSkip to conversation

tools/usbip: add usb event monitor into libusbip

PATCH New Lars Gunnarsson 1 participant 1 message Nov 30, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v4 3/5 tools/usbip: add usb event monitor into libusbip

This patch implements an usb monitor into libusbip to synchronously wait for usb events, which is used in coming patches. By hooking this api into "usb bind" before exporting a device, allows the command to wait for a device to be plugged in. By hooking this api in after a successful export allows the command to know when to restart bus monitoring again.

By hooking this api into usbipd at the beginning of an import request, allows the command "usbip attach" to wait for a device to become exported. By hooking this api into "usbip attach" right after a successful import, allows the command to know when to restart remote bus monitoring again.

Example of api usage:

// wait for an usb divce to be plugged in:
usbip_monitor_t *monitor = usbip_monitor_new(); usbip_monitor_set_busid(monitor, "3-3.1.2.3"); usbip_monitor_await_usb_bind(monitor, "usb"); // this is a blocking call // usb device with busid 3-3.1.2.3 is now bound to driver "usb". usbip_monitor_delete(monitor);

---
v2: Change title, fix style warnings, improve feature description, add timeout
into usbip_monitor.
v3: Change title and description.
v4: Change description and fix review comments.

Justifications of remaining warnings from "scripts/checkpatch.pl":

* Exception according to Linux kernel coding style 5.a where "usbip_monitor_t" is a totally opaque object:

WARNING: do not add new typedefs
#199: FILE: tools/usb/usbip/libsrc/usbip_monitor.h:8:
+typedef struct usbip_monitor usbip_monitor_t;

 tools/usb/usbip/.gitignore             |   1 +
 tools/usb/usbip/libsrc/Makefile.am     |   3 +-
 tools/usb/usbip/libsrc/usbip_common.h  |   1 +
 tools/usb/usbip/libsrc/usbip_monitor.c | 161 +++++++++++++++++++++++++
 tools/usb/usbip/libsrc/usbip_monitor.h |  36 ++++++
 5 files changed, 201 insertions(+), 1 deletion(-)
 create mode 100644 tools/usb/usbip/libsrc/usbip_monitor.c
 create mode 100644 tools/usb/usbip/libsrc/usbip_monitor.h
tools/usb/usbip/.gitignore +1 -0
index 597361a96dbb..6304adefb5e1 100644
@@ -28,6 +28,7 @@ libsrc/libusbip_la-usbip_common.lo
 libsrc/libusbip_la-usbip_device_driver.lo
 libsrc/libusbip_la-usbip_host_common.lo
 libsrc/libusbip_la-usbip_host_driver.lo
+libsrc/libusbip_la-usbip_monitor.lo
 libsrc/libusbip_la-vhci_driver.lo
 src/usbip
 src/usbipd
tools/usb/usbip/libsrc/Makefile.am +2 -1
index dabd2c91d311..3e31e33729cf 100644
@@ -8,4 +8,5 @@ libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h \
 		       usbip_device_driver.c usbip_device_driver.h \
 		       usbip_common.c usbip_common.h usbip_host_common.h \
 		       usbip_host_common.c vhci_driver.c vhci_driver.h \
-		       sysfs_utils.c sysfs_utils.h
+		       sysfs_utils.c sysfs_utils.h \
+		       usbip_monitor.c
tools/usb/usbip/libsrc/usbip_common.h +1 -0
index 73a367a7fa10..13f1d4ca47c5 100644
@@ -30,6 +30,7 @@
 
 /* kernel module names */
 #define USBIP_CORE_MOD_NAME	"usbip-core"
+#define USBIP_USB_DRV_NAME	"usb"
 #define USBIP_HOST_DRV_NAME	"usbip-host"
 #define USBIP_DEVICE_DRV_NAME	"usbip-vudc"
 #define USBIP_VHCI_DRV_NAME	"vhci_hcd"
tools/usb/usbip/libsrc/usbip_monitor.c new file +161 -0

161 lines not on this page · raw message

tools/usb/usbip/libsrc/usbip_monitor.h new file +36 -0
new file mode 100644
index 000000000000..209bc722529c
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/**
+ * Copyright (C) 2021 Lars Gunnarsson <[email protected]>
+ */
+#ifndef __USBIP_MONITOR_H
+#define __USBIP_MONITOR_H
+
+#include <stdbool.h>
+
+typedef struct usbip_monitor usbip_monitor_t;
+
+usbip_monitor_t *usbip_monitor_new(void);
+void usbip_monitor_delete(usbip_monitor_t *monitor);
+
+/**
+ * Set busid to await events on. If unset, any busid will be matched.
+ */
+void usbip_monitor_set_busid(usbip_monitor_t *monitor, const char *busid);
+
+/**
+ * Set timeout for await calls in milliseconds, default is no timeout (-1).
+ */
+void usbip_monitor_set_timeout(usbip_monitor_t *monitor, int milliseconds);
+
+/**
+ * Functions below is blocking. Returns true if event occurred, or false on
+ * timeouts.
+ */
+bool usbip_monitor_await_usb_add(const usbip_monitor_t *monitor,
+				 const char *driver);
+bool usbip_monitor_await_usb_bind(const usbip_monitor_t *monitor,
+				  const char *driver);
+bool usbip_monitor_await_usb_unbind(const usbip_monitor_t *monitor);
+bool usbip_monitor_await_usb_delete(const usbip_monitor_t *monitor);
+
+#endif /* __USBIP_MONITOR_H */

--
2.25.1