1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
4 * 2005-2007 Takahiro Hirofuchi
17 #include "vhci_driver.h"
18 #include "usbip_common.h"
19 #include "usbip_network.h"
22 static const char usbip_detach_usage_string[] =
23 "usbip detach <args>\n"
24 " -p, --port=<port> " USBIP_VHCI_DRV_NAME
25 " port the device is on\n";
27 void usbip_detach_usage(void)
29 printf("usage: %s", usbip_detach_usage_string);
32 static int detach_port(char *port)
36 char path[PATH_MAX+1];
38 struct usbip_imported_device *idev;
41 unsigned int port_len = strlen(port);
43 for (unsigned int i = 0; i < port_len; i++)
44 if (!isdigit(port[i])) {
45 err("invalid port %s", port);
51 ret = usbip_vhci_driver_open();
53 err("open vhci_driver");
57 /* check for invalid port */
58 for (i = 0; i < vhci_driver->nports; i++) {
59 idev = &vhci_driver->idev[i];
61 if (idev->port == portnum) {
63 if (idev->status != VDEV_ST_NULL)
65 info("Port %d is already detached!\n", idev->port);
66 goto call_driver_close;
71 err("Invalid port %s > maxports %d",
72 port, vhci_driver->nports);
73 goto call_driver_close;
76 /* remove the port state file */
77 snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", portnum);
80 rmdir(VHCI_STATE_PATH);
82 ret = usbip_vhci_detach_device(portnum);
85 err("Port %d detach request failed!\n", portnum);
86 goto call_driver_close;
88 info("Port %d is now detached!\n", portnum);
91 usbip_vhci_driver_close();
96 int usbip_detach(int argc, char *argv[])
98 static const struct option opts[] = {
99 { "port", required_argument, NULL, 'p' },
106 opt = getopt_long(argc, argv, "p:", opts, NULL);
113 ret = detach_port(optarg);
121 usbip_detach_usage();