diff options
Diffstat (limited to 'tools/usb/usbip/src/usbip_detach.c')
-rw-r--r-- | tools/usb/usbip/src/usbip_detach.c | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/tools/usb/usbip/src/usbip_detach.c b/tools/usb/usbip/src/usbip_detach.c index 9db9d21bb2ec..777f7286a0c5 100644 --- a/tools/usb/usbip/src/usbip_detach.c +++ b/tools/usb/usbip/src/usbip_detach.c @@ -43,9 +43,12 @@ void usbip_detach_usage(void) static int detach_port(char *port) { - int ret; + int ret = 0; uint8_t portnum; char path[PATH_MAX+1]; + int i; + struct usbip_imported_device *idev; + int found = 0; unsigned int port_len = strlen(port); @@ -55,27 +58,48 @@ static int detach_port(char *port) return -1; } - /* check max port */ - portnum = atoi(port); - /* remove the port state file */ + ret = usbip_vhci_driver_open(); + if (ret < 0) { + err("open vhci_driver"); + return -1; + } + + /* check for invalid port */ + for (i = 0; i < vhci_driver->nports; i++) { + idev = &vhci_driver->idev[i]; + + if (idev->port == portnum) { + found = 1; + if (idev->status != VDEV_ST_NULL) + break; + info("Port %d is already detached!\n", idev->port); + goto call_driver_close; + } + } + if (!found) { + err("Invalid port %s > maxports %d", + port, vhci_driver->nports); + goto call_driver_close; + } + + /* remove the port state file */ snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", portnum); remove(path); rmdir(VHCI_STATE_PATH); - ret = usbip_vhci_driver_open(); + ret = usbip_vhci_detach_device(portnum); if (ret < 0) { - err("open vhci_driver"); - return -1; + ret = -1; + err("Port %d detach request failed!\n", portnum); + goto call_driver_close; } + info("Port %d is now detached!\n", portnum); - ret = usbip_vhci_detach_device(portnum); - if (ret < 0) - return -1; - +call_driver_close: usbip_vhci_driver_close(); return ret; |