Commit 06c465f5 authored by matt mooney's avatar matt mooney Committed by Greg Kroah-Hartman

staging: usbip: userspace: usbip_bind.c: major rewrite of the implementation

Rewrite functions in terms of libsysfs, which eliminates a lot of
helper functions simplifying the file layout. Now, the two processes
taking place here, an unbind of the old driver and a bind of
usbip-host, are single functions and have been renamed along with the
controlling function. A check to see if the device is already bound to
usbip-host is now included.
Signed-off-by: default avatarmatt mooney <mfm@muteddisk.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 30f05546
...@@ -18,18 +18,23 @@ ...@@ -18,18 +18,23 @@
#include <sysfs/libsysfs.h> #include <sysfs/libsysfs.h>
#include <limits.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <fcntl.h>
#include <getopt.h> #include <getopt.h>
#include <unistd.h>
#include "usbip_common.h" #include "usbip_common.h"
#include "utils.h" #include "utils.h"
#include "usbip.h" #include "usbip.h"
enum unbind_status {
UNBIND_ST_OK,
UNBIND_ST_USBIP_HOST,
UNBIND_ST_FAILED
};
static const char usbip_bind_usage_string[] = static const char usbip_bind_usage_string[] =
"usbip bind <args>\n" "usbip bind <args>\n"
" -b, --busid=<busid> Bind " USBIP_HOST_DRV_NAME ".ko to device " " -b, --busid=<busid> Bind " USBIP_HOST_DRV_NAME ".ko to device "
...@@ -40,195 +45,202 @@ void usbip_bind_usage(void) ...@@ -40,195 +45,202 @@ void usbip_bind_usage(void)
printf("usage: %s", usbip_bind_usage_string); printf("usage: %s", usbip_bind_usage_string);
} }
static const char unbind_path_format[] = "/sys/bus/usb/devices/%s/driver/unbind"; /* call at unbound state */
static int bind_usbip(char *busid)
/* buggy driver may cause dead lock */
static int unbind_interface_busid(char *busid)
{ {
char unbind_path[SYSFS_PATH_MAX]; char bus_type[] = "usb";
int fd; char attr_name[] = "bind";
int ret; char sysfs_mntpath[SYSFS_PATH_MAX];
char bind_attr_path[SYSFS_PATH_MAX];
snprintf(unbind_path, sizeof(unbind_path), unbind_path_format, busid); char intf_busid[SYSFS_BUS_ID_SIZE];
struct sysfs_device *busid_dev;
fd = open(unbind_path, O_WRONLY); struct sysfs_attribute *bind_attr;
if (fd < 0) { struct sysfs_attribute *bConfValue;
dbg("opening unbind_path failed: %d", fd); struct sysfs_attribute *bNumIntfs;
int i, failed = 0;
int rc, ret = -1;
rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
if (rc < 0) {
err("sysfs must be mounted: %s", strerror(errno));
return -1; return -1;
} }
ret = write(fd, busid, strnlen(busid, SYSFS_BUS_ID_SIZE)); snprintf(bind_attr_path, sizeof(bind_attr_path), "%s/%s/%s/%s/%s/%s",
if (ret < 0) { sysfs_mntpath, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
dbg("write to unbind_path failed: %d", ret); USBIP_HOST_DRV_NAME, attr_name);
close(fd);
return -1;
}
close(fd);
return 0;
}
static int unbind_interface(char *busid, int configvalue, int interface)
{
char inf_busid[SYSFS_BUS_ID_SIZE];
dbg("unbinding interface");
snprintf(inf_busid, SYSFS_BUS_ID_SIZE, "%s:%d.%d", busid, configvalue,
interface);
return unbind_interface_busid(inf_busid); bind_attr = sysfs_open_attribute(bind_attr_path);
} if (!bind_attr) {
dbg("problem getting bind attribute: %s", strerror(errno));
static int unbind(char *busid)
{
int configvalue = 0;
int ninterface = 0;
int devclass = 0;
int i;
int failed = 0;
configvalue = read_bConfigurationValue(busid);
ninterface = read_bNumInterfaces(busid);
devclass = read_bDeviceClass(busid);
if (configvalue < 0 || ninterface < 0 || devclass < 0) {
dbg("read config and ninf value, removed?");
return -1; return -1;
} }
if (devclass == 0x09) { busid_dev = sysfs_open_device(bus_type, busid);
dbg("skip unbinding of hub"); if (!busid_dev) {
return -1; dbg("sysfs_open_device %s failed: %s", busid, strerror(errno));
goto err_close_bind_attr;
} }
for (i = 0; i < ninterface; i++) { bConfValue = sysfs_get_device_attr(busid_dev, "bConfigurationValue");
char driver[PATH_MAX]; bNumIntfs = sysfs_get_device_attr(busid_dev, "bNumInterfaces");
int ret;
memset(&driver, 0, sizeof(driver));
getdriver(busid, configvalue, i, driver, PATH_MAX-1);
dbg(" %s:%d.%d -> %s ", busid, configvalue, i, driver); if (!bConfValue || !bNumIntfs) {
dbg("problem getting device attributes: %s",
if (!strncmp("none", driver, PATH_MAX)) strerror(errno));
continue; /* unbound interface */ goto err_close_busid_dev;
}
#if 0 for (i = 0; i < atoi(bNumIntfs->value); i++) {
if (!strncmp("usbip", driver, PATH_MAX)) snprintf(intf_busid, SYSFS_BUS_ID_SIZE, "%s:%.1s.%d", busid,
continue; /* already bound to usbip */ bConfValue->value, i);
#endif
/* unbinding */ rc = sysfs_write_attribute(bind_attr, intf_busid,
ret = unbind_interface(busid, configvalue, i); SYSFS_BUS_ID_SIZE);
if (ret < 0) { if (rc < 0) {
dbg("unbind driver at %s:%d.%d failed", dbg("bind driver at %s failed", intf_busid);
busid, configvalue, i);
failed = 1; failed = 1;
} }
} }
if (failed) if (!failed)
return -1; ret = 0;
else
return 0;
}
static const char bind_path_format[] = "/sys/bus/usb/drivers/%s/bind";
static int bind_interface_busid(char *busid, char *driver) err_close_busid_dev:
{ sysfs_close_device(busid_dev);
char bind_path[PATH_MAX]; err_close_bind_attr:
int fd; sysfs_close_attribute(bind_attr);
int ret;
snprintf(bind_path, sizeof(bind_path), bind_path_format, driver); return ret;
}
fd = open(bind_path, O_WRONLY); /* buggy driver may cause dead lock */
if (fd < 0) static int unbind_other(char *busid)
{
char bus_type[] = "usb";
char intf_busid[SYSFS_BUS_ID_SIZE];
struct sysfs_device *busid_dev;
struct sysfs_device *intf_dev;
struct sysfs_driver *intf_drv;
struct sysfs_attribute *unbind_attr;
struct sysfs_attribute *bConfValue;
struct sysfs_attribute *bDevClass;
struct sysfs_attribute *bNumIntfs;
int i, rc;
enum unbind_status status = UNBIND_ST_OK;
busid_dev = sysfs_open_device(bus_type, busid);
if (!busid_dev) {
dbg("sysfs_open_device %s failed: %s", busid, strerror(errno));
return -1; return -1;
}
ret = write(fd, busid, strnlen(busid, SYSFS_BUS_ID_SIZE)); bConfValue = sysfs_get_device_attr(busid_dev, "bConfigurationValue");
if (ret < 0) { bDevClass = sysfs_get_device_attr(busid_dev, "bDeviceClass");
close(fd); bNumIntfs = sysfs_get_device_attr(busid_dev, "bNumInterfaces");
return -1; if (!bConfValue || !bDevClass || !bNumIntfs) {
dbg("problem getting device attributes: %s",
strerror(errno));
goto err_close_busid_dev;
} }
close(fd); if (!strncmp(bDevClass->value, "09", bDevClass->len)) {
dbg("skip unbinding of hub");
goto err_close_busid_dev;
}
return 0; for (i = 0; i < atoi(bNumIntfs->value); i++) {
} snprintf(intf_busid, SYSFS_BUS_ID_SIZE, "%s:%.1s.%d", busid,
bConfValue->value, i);
intf_dev = sysfs_open_device(bus_type, intf_busid);
if (!intf_dev) {
dbg("could not open interface device: %s",
strerror(errno));
goto err_close_busid_dev;
}
static int bind_interface(char *busid, int configvalue, int interface, char *driver) dbg("%s -> %s", intf_dev->name, intf_dev->driver_name);
{
char inf_busid[SYSFS_BUS_ID_SIZE];
snprintf(inf_busid, SYSFS_BUS_ID_SIZE, "%s:%d.%d", busid, configvalue, if (!strncmp("unknown", intf_dev->driver_name, SYSFS_NAME_LEN))
interface); /* unbound interface */
continue;
return bind_interface_busid(inf_busid, driver); if (!strncmp(USBIP_HOST_DRV_NAME, intf_dev->driver_name,
} SYSFS_NAME_LEN)) {
/* already bound to usbip-host */
status = UNBIND_ST_USBIP_HOST;
continue;
}
/* call at unbound state */ /* unbinding */
static int bind_to_usbip(char *busid) intf_drv = sysfs_open_driver(bus_type, intf_dev->driver_name);
{ if (!intf_drv) {
int configvalue = 0; dbg("could not open interface driver on %s: %s",
int ninterface = 0; intf_dev->name, strerror(errno));
int i; goto err_close_intf_dev;
int failed = 0; }
configvalue = read_bConfigurationValue(busid); unbind_attr = sysfs_get_driver_attr(intf_drv, "unbind");
ninterface = read_bNumInterfaces(busid); if (!unbind_attr) {
dbg("problem getting interface driver attribute: %s",
strerror(errno));
goto err_close_intf_drv;
}
if (configvalue < 0 || ninterface < 0) { rc = sysfs_write_attribute(unbind_attr, intf_dev->bus_id,
dbg("read config and ninf value, removed?"); SYSFS_BUS_ID_SIZE);
return -1; if (rc < 0) {
/* NOTE: why keep unbinding other interfaces? */
dbg("unbind driver at %s failed", intf_dev->bus_id);
status = UNBIND_ST_FAILED;
}
sysfs_close_driver(intf_drv);
sysfs_close_device(intf_dev);
} }
for (i = 0; i < ninterface; i++) { goto out;
int ret;
ret = bind_interface(busid, configvalue, i, err_close_intf_drv:
USBIP_HOST_DRV_NAME); sysfs_close_driver(intf_drv);
if (ret < 0) { err_close_intf_dev:
dbg("bind usbip at %s:%d.%d, failed", sysfs_close_device(intf_dev);
busid, configvalue, i); err_close_busid_dev:
failed = 1; status = UNBIND_ST_FAILED;
/* need to contine binding at other interfaces */ out:
} sysfs_close_device(busid_dev);
}
if (failed) return status;
return -1;
else
return 0;
} }
static int use_device_by_usbip(char *busid) static int bind_device(char *busid)
{ {
int ret; int rc;
ret = unbind(busid); rc = unbind_other(busid);
if (ret < 0) { if (rc == UNBIND_ST_FAILED) {
dbg("unbind drivers of %s, failed", busid); err("could not unbind driver from device on busid %s", busid);
return -1;
} else if (rc == UNBIND_ST_USBIP_HOST) {
err("device on busid %s is already bound to %s", busid,
USBIP_HOST_DRV_NAME);
return -1; return -1;
} }
ret = modify_match_busid(busid, 1); rc = modify_match_busid(busid, 1);
if (ret < 0) { if (rc < 0) {
dbg("add %s to match_busid, failed", busid); err("unable to bind device on %s", busid);
return -1; return -1;
} }
ret = bind_to_usbip(busid); rc = bind_usbip(busid);
if (ret < 0) { if (rc < 0) {
dbg("bind usbip to %s, failed", busid); err("could not bind device to %s", USBIP_HOST_DRV_NAME);
modify_match_busid(busid, 0); modify_match_busid(busid, 0);
return -1; return -1;
} }
dbg("bind %s complete!", busid); printf("bind device on busid %s: complete\n", busid);
return 0; return 0;
} }
...@@ -237,8 +249,9 @@ int usbip_bind(int argc, char *argv[]) ...@@ -237,8 +249,9 @@ int usbip_bind(int argc, char *argv[])
{ {
static const struct option opts[] = { static const struct option opts[] = {
{ "busid", required_argument, NULL, 'b' }, { "busid", required_argument, NULL, 'b' },
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
int opt; int opt;
int ret = -1; int ret = -1;
...@@ -250,7 +263,7 @@ int usbip_bind(int argc, char *argv[]) ...@@ -250,7 +263,7 @@ int usbip_bind(int argc, char *argv[])
switch (opt) { switch (opt) {
case 'b': case 'b':
ret = use_device_by_usbip(optarg); ret = bind_device(optarg);
goto out; goto out;
default: default:
goto err_out; goto err_out;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment