1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
4 * 2005-2007 Takahiro Hirofuchi
15 #include "usbip_common.h"
18 #include "sysfs_utils.h"
20 static const char usbip_unbind_usage_string[] =
21 "usbip unbind <args>\n"
22 " -b, --busid=<busid> Unbind " USBIP_HOST_DRV_NAME ".ko from "
23 "device on <busid>\n";
25 void usbip_unbind_usage(void)
27 printf("usage: %s", usbip_unbind_usage_string);
30 static int unbind_device(char *busid)
32 char bus_type[] = "usb";
35 char unbind_attr_name[] = "unbind";
36 char unbind_attr_path[SYSFS_PATH_MAX];
37 char rebind_attr_name[] = "rebind";
38 char rebind_attr_path[SYSFS_PATH_MAX];
41 struct udev_device *dev;
44 /* Create libudev context. */
47 /* Check whether the device with this bus ID exists. */
48 dev = udev_device_new_from_subsystem_sysname(udev, "usb", busid);
50 err("device with the specified bus ID does not exist");
54 /* Check whether the device is using usbip-host driver. */
55 driver = udev_device_get_driver(dev);
56 if (!driver || strcmp(driver, "usbip-host")) {
57 err("device is not bound to usbip-host driver");
61 /* Unbind device from driver. */
62 snprintf(unbind_attr_path, sizeof(unbind_attr_path), "%s/%s/%s/%s/%s/%s",
63 SYSFS_MNT_PATH, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
64 USBIP_HOST_DRV_NAME, unbind_attr_name);
66 rc = write_sysfs_attribute(unbind_attr_path, busid, strlen(busid));
68 err("error unbinding device %s from driver", busid);
72 /* Notify driver of unbind. */
73 rc = modify_match_busid(busid, 0);
75 err("unable to unbind device on %s", busid);
79 /* Trigger new probing. */
80 snprintf(rebind_attr_path, sizeof(unbind_attr_path), "%s/%s/%s/%s/%s/%s",
81 SYSFS_MNT_PATH, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
82 USBIP_HOST_DRV_NAME, rebind_attr_name);
84 rc = write_sysfs_attribute(rebind_attr_path, busid, strlen(busid));
86 err("error rebinding");
91 info("unbind device on busid %s: complete", busid);
94 udev_device_unref(dev);
100 int usbip_unbind(int argc, char *argv[])
102 static const struct option opts[] = {
103 { "busid", required_argument, NULL, 'b' },
111 opt = getopt_long(argc, argv, "b:", opts, NULL);
118 ret = unbind_device(optarg);
126 usbip_unbind_usage();