Commit d561ef95 authored by Valentina Manea's avatar Valentina Manea Committed by Greg Kroah-Hartman

staging: usbip: userspace: migrate usbip_unbind to libudev

This patch modifies usbip_unbind to use libudev.
Signed-off-by: default avatarValentina Manea <valentina.manea.m@gmail.com>
Reviewed-by: default avatarShuah Khan <shuah.kh@samsung.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent c522a03a
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <sysfs/libsysfs.h> #include <libudev.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "usbip_common.h" #include "usbip_common.h"
#include "utils.h" #include "utils.h"
#include "usbip.h" #include "usbip.h"
#include "sysfs_utils.h"
static const char usbip_unbind_usage_string[] = static const char usbip_unbind_usage_string[] =
"usbip unbind <args>\n" "usbip unbind <args>\n"
...@@ -41,92 +42,56 @@ void usbip_unbind_usage(void) ...@@ -41,92 +42,56 @@ void usbip_unbind_usage(void)
static int unbind_device(char *busid) static int unbind_device(char *busid)
{ {
char bus_type[] = "usb"; char bus_type[] = "usb";
struct sysfs_driver *usbip_host_drv;
struct sysfs_device *dev;
struct dlist *devlist;
int verified = 0;
int rc, ret = -1; int rc, ret = -1;
char attr_name[] = "unbind"; char attr_name[] = "unbind";
char sysfs_mntpath[SYSFS_PATH_MAX];
char unbind_attr_path[SYSFS_PATH_MAX]; char unbind_attr_path[SYSFS_PATH_MAX];
struct sysfs_attribute *unbind_attr;
/* verify the busid device is using usbip-host */
usbip_host_drv = sysfs_open_driver(bus_type, USBIP_HOST_DRV_NAME);
if (!usbip_host_drv) {
err("could not open %s driver: %s", USBIP_HOST_DRV_NAME,
strerror(errno));
return -1;
}
devlist = sysfs_get_driver_devices(usbip_host_drv); struct udev *udev;
if (!devlist) { struct udev_device *dev;
err("%s is not in use by any devices", USBIP_HOST_DRV_NAME); const char *driver;
goto err_close_usbip_host_drv;
}
dlist_for_each_data(devlist, dev, struct sysfs_device) { /* Create libudev context. */
if (!strncmp(busid, dev->name, strlen(busid)) && udev = udev_new();
!strncmp(dev->driver_name, USBIP_HOST_DRV_NAME,
strlen(USBIP_HOST_DRV_NAME))) {
verified = 1;
break;
}
}
if (!verified) { /* Check whether the device with this bus ID exists. */
err("device on busid %s is not using %s", busid, dev = udev_device_new_from_subsystem_sysname(udev, "usb", busid);
USBIP_HOST_DRV_NAME); if (!dev) {
goto err_close_usbip_host_drv; err("device with the specified bus ID does not exist");
goto err_close_udev;
} }
/* /* Check whether the device is using usbip-host driver. */
* NOTE: A read and write of an attribute value of the device busid driver = udev_device_get_driver(dev);
* refers to must be done to start probing. That way a rebind of the if (!driver || strcmp(driver, "usbip-host")) {
* default driver for the device occurs. err("device is not bound to usbip-host driver");
* goto err_close_udev;
* This seems very hackish and adds a lot of pointless code. I think it
* should be done in the kernel by the driver after del_match_busid is
* finished!
*/
rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
if (rc < 0) {
err("sysfs must be mounted: %s", strerror(errno));
return -1;
} }
/* Unbind device from driver. */
snprintf(unbind_attr_path, sizeof(unbind_attr_path), "%s/%s/%s/%s/%s/%s", snprintf(unbind_attr_path, sizeof(unbind_attr_path), "%s/%s/%s/%s/%s/%s",
sysfs_mntpath, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME, SYSFS_MNT_PATH, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
USBIP_HOST_DRV_NAME, attr_name); USBIP_HOST_DRV_NAME, attr_name);
/* read a device attribute */ rc = write_sysfs_attribute(unbind_attr_path, busid, strlen(busid));
unbind_attr = sysfs_open_attribute(unbind_attr_path); if (rc < 0) {
if (!unbind_attr) { err("error unbinding device %s from driver", busid);
err("could not open %s/%s: %s", busid, attr_name, goto err_close_udev;
strerror(errno));
return -1;
} }
/* notify driver of unbind */ /* Notify driver of unbind. */
rc = modify_match_busid(busid, 0); rc = modify_match_busid(busid, 0);
if (rc < 0) { if (rc < 0) {
err("unable to unbind device on %s", busid); err("unable to unbind device on %s", busid);
goto err_close_udev;
} }
rc = sysfs_write_attribute(unbind_attr, busid,
SYSFS_BUS_ID_SIZE);
if (rc < 0) {
dbg("bind driver at %s failed", busid);
}
sysfs_close_attribute(unbind_attr);
ret = 0; ret = 0;
printf("unbind device on busid %s: complete\n", busid); info("unbind device on busid %s: complete", busid);
err_close_usbip_host_drv: err_close_udev:
sysfs_close_driver(usbip_host_drv); udev_device_unref(dev);
udev_unref(udev);
return ret; return ret;
} }
......
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