19 #include <sysfs/libsysfs.h>
27 #include "usbip_common.h"
31 static const char usbip_unbind_usage_string[] =
32 "usbip unbind <args>\n"
34 "device on <busid>\n";
38 printf(
"usage: %s", usbip_unbind_usage_string);
41 static int unbind_device(
char *
busid)
44 struct sysfs_driver *usbip_host_drv;
45 struct sysfs_device *
dev;
53 struct sysfs_attribute *busid_attr;
59 if (!usbip_host_drv) {
65 devlist = sysfs_get_driver_devices(usbip_host_drv);
68 goto err_close_usbip_host_drv;
71 dlist_for_each_data(devlist, dev,
struct sysfs_device) {
81 err(
"device on busid %s is not using %s", busid,
83 goto err_close_usbip_host_drv;
102 snprintf(busid_attr_path,
sizeof(busid_attr_path),
"%s/%s/%s/%s/%s/%s",
103 sysfs_mntpath, SYSFS_BUS_NAME, bus_type, SYSFS_DEVICES_NAME,
107 busid_attr = sysfs_open_attribute(busid_attr_path);
109 err(
"could not open %s/%s: %s", busid, attr_name,
114 if (sysfs_read_attribute(busid_attr) < 0) {
115 err(
"problem reading attribute: %s",
strerror(errno));
119 len = busid_attr->len;
121 *val = *busid_attr->value;
122 sysfs_close_attribute(busid_attr);
127 err(
"unable to unbind device on %s", busid);
132 busid_attr = sysfs_open_attribute(busid_attr_path);
134 err(
"could not open %s/%s: %s", busid, attr_name,
139 rc = sysfs_write_attribute(busid_attr, val, len);
141 err(
"problem writing attribute: %s",
strerror(errno));
144 sysfs_close_attribute(busid_attr);
147 printf(
"unbind device on busid %s: complete\n", busid);
151 err_close_usbip_host_drv:
152 sysfs_close_driver(usbip_host_drv);
159 static const struct option opts[] = {
160 {
"busid", required_argument,
NULL,
'b' },
168 opt = getopt_long(argc, argv,
"b:", opts,
NULL);
175 ret = unbind_device(optarg);