2 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3 * 2005-2007 Takahiro Hirofuchi
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "usbip_common.h"
30 #include "sysfs_utils.h"
32 static const char usbip_unbind_usage_string[] =
33 "usbip unbind <args>\n"
34 " -b, --busid=<busid> Unbind " USBIP_HOST_DRV_NAME ".ko from "
35 "device on <busid>\n";
37 void usbip_unbind_usage(void)
39 printf("usage: %s", usbip_unbind_usage_string);
42 static int unbind_device(char *busid)
44 char bus_type[] = "usb";
47 char unbind_attr_name[] = "unbind";
48 char unbind_attr_path[SYSFS_PATH_MAX];
49 char rebind_attr_name[] = "rebind";
50 char rebind_attr_path[SYSFS_PATH_MAX];
53 struct udev_device *dev;
56 /* Create libudev context. */
59 /* Check whether the device with this bus ID exists. */
60 dev = udev_device_new_from_subsystem_sysname(udev, "usb", busid);
62 err("device with the specified bus ID does not exist");
66 /* Check whether the device is using usbip-host driver. */
67 driver = udev_device_get_driver(dev);
68 if (!driver || strcmp(driver, "usbip-host")) {
69 err("device is not bound to usbip-host driver");
73 /* Unbind device from driver. */
74 snprintf(unbind_attr_path, sizeof(unbind_attr_path), "%s/%s/%s/%s/%s/%s",
75 SYSFS_MNT_PATH, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
76 USBIP_HOST_DRV_NAME, unbind_attr_name);
78 rc = write_sysfs_attribute(unbind_attr_path, busid, strlen(busid));
80 err("error unbinding device %s from driver", busid);
84 /* Notify driver of unbind. */
85 rc = modify_match_busid(busid, 0);
87 err("unable to unbind device on %s", busid);
91 /* Trigger new probing. */
92 snprintf(rebind_attr_path, sizeof(unbind_attr_path), "%s/%s/%s/%s/%s/%s",
93 SYSFS_MNT_PATH, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
94 USBIP_HOST_DRV_NAME, rebind_attr_name);
96 rc = write_sysfs_attribute(rebind_attr_path, busid, strlen(busid));
98 err("error rebinding");
103 info("unbind device on busid %s: complete", busid);
106 udev_device_unref(dev);
112 int usbip_unbind(int argc, char *argv[])
114 static const struct option opts[] = {
115 { "busid", required_argument, NULL, 'b' },
123 opt = getopt_long(argc, argv, "b:", opts, NULL);
130 ret = unbind_device(optarg);
138 usbip_unbind_usage();