This patch should fix the crash from simtrace app when SIMtrace hardware
is unpluged from usb or when it is reseted.
This feature was proposed on simtrace workshop on ccc camp.
Signed-off-by: Luka Perkov < lists ->-to->-
lukaperkov.net >
---
diff --git a/host/main.c b/host/main.c
index a1e5600..61656be 100644
--- a/host/main.c
+++ b/host/main.c
@@ -187,7 +187,14 @@ int main(int argc, char **argv)
rc = usb_bulk_read(udev, SIMTRACE_IN_EP, buf, sizeof(buf), 100000);
if (rc < 0 && rc != -EAGAIN) {
fprintf(stderr, "Error submitting BULK IN urb: %s\n", usb_strerror());
- exit(1);
+ while (1) {
+ udev = usb_find_reopen(SIMTRACE_USB_VENDOR, SIMTRACE_USB_PRODUCT);
+ if (!udev) {
+ usleep(1000000);
+ } else {
+ break;
+ }
+ }
}
if (rc > 0) {
//printf("URB: %s\n", osmo_hexdump(buf, rc));
diff --git a/host/usb_helper.c b/host/usb_helper.c
index 231363a..97ea7b6 100644
--- a/host/usb_helper.c
+++ b/host/usb_helper.c
@@ -28,6 +28,8 @@
#include <usb.h>
+static struct usb_dev_handle *hdl;
+
static struct usb_device *find_usb_device (uint16_t vendor_id, uint16_t product_id)
{
struct usb_bus *bus;
@@ -46,8 +48,6 @@ static struct usb_device *find_usb_device (uint16_t vendor_id, uint16_t
product_
struct usb_dev_handle *usb_find_open(uint16_t vendor_id, uint16_t product_id)
{
struct usb_device *dev;
- struct usb_dev_handle *hdl;
-
usb_init();
usb_find_busses();
usb_find_devices();
@@ -56,7 +56,7 @@ struct usb_dev_handle *usb_find_open(uint16_t vendor_id, uint16_t
product_id)
if (!dev) {
fprintf(stderr, "Cannot find matching USB Device. "
"Are you sure it is connected?\n");
- exit(1);
+ return NULL;
}
hdl = usb_open(dev);
@@ -74,3 +74,12 @@ struct usb_dev_handle *usb_find_open(uint16_t vendor_id, uint16_t
product_id)
return hdl;
}
+
+struct usb_dev_handle *usb_find_reopen(uint16_t vendor_id, uint16_t product_id)
+{
+ if (hdl) {
+ usb_detach_kernel_driver_np(hdl, 0);
+ }
+
+ return usb_find_open(vendor_id, product_id);
+}
diff --git a/host/usb_helper.h b/host/usb_helper.h
index 13f96f8..0a9714c 100644
--- a/host/usb_helper.h
+++ b/host/usb_helper.h
@@ -22,5 +22,6 @@
#include <stdint.h>
struct usb_dev_handle *usb_find_open(uint16_t vendor_id, uint16_t product_id);
+struct usb_dev_handle *usb_find_reopen(uint16_t vendor_id, uint16_t product_id);
#endif