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

staging: usbip: convert usbip-host driver to usb_device_driver

This driver was previously an interface driver. Since USB/IP
exports a whole device, not just an interface, it would make
sense to be a device driver.

This patch also modifies the way userspace sees and uses a
shared device:

* the usbip_status file is no longer created for interface 0, but for
the whole device (such as
/sys/devices/pci0000:00/0000:00:01.2/usb1/1-1/usbip_status).
* per interface information, such as interface class or protocol, is
no longer sent/received; only device specific information is
transmitted.
* since the driver was moved one level below in the USB architecture,
there is no need to bind/unbind each interface, just the device as a
whole.
Signed-off-by: default avatarValentina Manea <valentina.manea.m@gmail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent a6646ea6
...@@ -93,7 +93,7 @@ struct bus_id_priv { ...@@ -93,7 +93,7 @@ struct bus_id_priv {
extern struct kmem_cache *stub_priv_cache; extern struct kmem_cache *stub_priv_cache;
/* stub_dev.c */ /* stub_dev.c */
extern struct usb_driver stub_driver; extern struct usb_device_driver stub_driver;
/* stub_main.c */ /* stub_main.c */
struct bus_id_priv *get_busid_priv(const char *busid); struct bus_id_priv *get_busid_priv(const char *busid);
......
...@@ -279,21 +279,19 @@ static void stub_device_unusable(struct usbip_device *ud) ...@@ -279,21 +279,19 @@ static void stub_device_unusable(struct usbip_device *ud)
* *
* Allocates and initializes a new stub_device struct. * Allocates and initializes a new stub_device struct.
*/ */
static struct stub_device *stub_device_alloc(struct usb_device *udev, static struct stub_device *stub_device_alloc(struct usb_device *udev)
struct usb_interface *interface)
{ {
struct stub_device *sdev; struct stub_device *sdev;
int busnum = interface_to_busnum(interface); int busnum = udev->bus->busnum;
int devnum = interface_to_devnum(interface); int devnum = udev->devnum;
dev_dbg(&interface->dev, "allocating stub device"); dev_dbg(&udev->dev, "allocating stub device");
/* yes, it's a new device */ /* yes, it's a new device */
sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL); sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
if (!sdev) if (!sdev)
return NULL; return NULL;
sdev->interface = usb_get_intf(interface);
sdev->udev = usb_get_dev(udev); sdev->udev = usb_get_dev(udev);
/* /*
...@@ -322,7 +320,7 @@ static struct stub_device *stub_device_alloc(struct usb_device *udev, ...@@ -322,7 +320,7 @@ static struct stub_device *stub_device_alloc(struct usb_device *udev,
usbip_start_eh(&sdev->ud); usbip_start_eh(&sdev->ud);
dev_dbg(&interface->dev, "register new interface\n"); dev_dbg(&udev->dev, "register new device\n");
return sdev; return sdev;
} }
...@@ -332,32 +330,20 @@ static void stub_device_free(struct stub_device *sdev) ...@@ -332,32 +330,20 @@ static void stub_device_free(struct stub_device *sdev)
kfree(sdev); kfree(sdev);
} }
/* static int stub_probe(struct usb_device *udev)
* If a usb device has multiple active interfaces, this driver is bound to all
* the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
* active interface). Currently, a userland program must ensure that it
* looks at the usbip's sysfs entries of only the first active interface.
*
* TODO: use "struct usb_device_driver" to bind a usb device.
* However, it seems it is not fully supported in mainline kernel yet
* (2.6.19.2).
*/
static int stub_probe(struct usb_interface *interface,
const struct usb_device_id *id)
{ {
struct usb_device *udev = interface_to_usbdev(interface);
struct stub_device *sdev = NULL; struct stub_device *sdev = NULL;
const char *udev_busid = dev_name(interface->dev.parent); const char *udev_busid = dev_name(&udev->dev);
int err = 0; int err = 0, config;
struct bus_id_priv *busid_priv; struct bus_id_priv *busid_priv;
dev_dbg(&interface->dev, "Enter\n"); dev_dbg(&udev->dev, "Enter\n");
/* check we should claim or not by busid_table */ /* check we should claim or not by busid_table */
busid_priv = get_busid_priv(udev_busid); busid_priv = get_busid_priv(udev_busid);
if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) || if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
(busid_priv->status == STUB_BUSID_OTHER)) { (busid_priv->status == STUB_BUSID_OTHER)) {
dev_info(&interface->dev, dev_info(&udev->dev,
"%s is not in match_busid table... skip!\n", "%s is not in match_busid table... skip!\n",
udev_busid); udev_busid);
...@@ -383,60 +369,36 @@ static int stub_probe(struct usb_interface *interface, ...@@ -383,60 +369,36 @@ static int stub_probe(struct usb_interface *interface,
return -ENODEV; return -ENODEV;
} }
if (busid_priv->status == STUB_BUSID_ALLOC) {
sdev = busid_priv->sdev;
if (!sdev)
return -ENODEV;
busid_priv->interf_count++;
dev_info(&interface->dev,
"usbip-host: register new interface (bus %u dev %u ifn %u)\n",
udev->bus->busnum, udev->devnum,
interface->cur_altsetting->desc.bInterfaceNumber);
/* set private data to usb_interface */
usb_set_intfdata(interface, sdev);
err = stub_add_files(&interface->dev);
if (err) {
dev_err(&interface->dev, "stub_add_files for %s\n",
udev_busid);
usb_set_intfdata(interface, NULL);
busid_priv->interf_count--;
return err;
}
usb_get_intf(interface);
return 0;
}
/* ok, this is my device */ /* ok, this is my device */
sdev = stub_device_alloc(udev, interface); sdev = stub_device_alloc(udev);
if (!sdev) if (!sdev)
return -ENOMEM; return -ENOMEM;
dev_info(&interface->dev, dev_info(&udev->dev,
"usbip-host: register new device (bus %u dev %u ifn %u)\n", "usbip-host: register new device (bus %u dev %u)\n",
udev->bus->busnum, udev->devnum, udev->bus->busnum, udev->devnum);
interface->cur_altsetting->desc.bInterfaceNumber);
busid_priv->interf_count = 0;
busid_priv->shutdown_busid = 0; busid_priv->shutdown_busid = 0;
/* set private data to usb_interface */ config = usb_choose_configuration(udev);
usb_set_intfdata(interface, sdev); if (config >= 0) {
busid_priv->interf_count++; err = usb_set_configuration(udev, config);
if (err && err != -ENODEV)
dev_err(&udev->dev, "can't set config #%d, error %d\n",
config, err);
}
/* set private data to usb_device */
dev_set_drvdata(&udev->dev, sdev);
busid_priv->sdev = sdev; busid_priv->sdev = sdev;
err = stub_add_files(&interface->dev); err = stub_add_files(&udev->dev);
if (err) { if (err) {
dev_err(&interface->dev, "stub_add_files for %s\n", udev_busid); dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
usb_set_intfdata(interface, NULL); dev_set_drvdata(&udev->dev, NULL);
usb_put_intf(interface);
usb_put_dev(udev); usb_put_dev(udev);
kthread_stop_put(sdev->ud.eh); kthread_stop_put(sdev->ud.eh);
busid_priv->interf_count = 0;
busid_priv->sdev = NULL; busid_priv->sdev = NULL;
stub_device_free(sdev); stub_device_free(sdev);
return err; return err;
...@@ -461,13 +423,13 @@ static void shutdown_busid(struct bus_id_priv *busid_priv) ...@@ -461,13 +423,13 @@ static void shutdown_busid(struct bus_id_priv *busid_priv)
* called in usb_disconnect() or usb_deregister() * called in usb_disconnect() or usb_deregister()
* but only if actconfig(active configuration) exists * but only if actconfig(active configuration) exists
*/ */
static void stub_disconnect(struct usb_interface *interface) static void stub_disconnect(struct usb_device *udev)
{ {
struct stub_device *sdev; struct stub_device *sdev;
const char *udev_busid = dev_name(interface->dev.parent); const char *udev_busid = dev_name(&udev->dev);
struct bus_id_priv *busid_priv; struct bus_id_priv *busid_priv;
dev_dbg(&interface->dev, "Enter\n"); dev_dbg(&udev->dev, "Enter\n");
busid_priv = get_busid_priv(udev_busid); busid_priv = get_busid_priv(udev_busid);
if (!busid_priv) { if (!busid_priv) {
...@@ -475,41 +437,29 @@ static void stub_disconnect(struct usb_interface *interface) ...@@ -475,41 +437,29 @@ static void stub_disconnect(struct usb_interface *interface)
return; return;
} }
sdev = usb_get_intfdata(interface); sdev = dev_get_drvdata(&udev->dev);
/* get stub_device */ /* get stub_device */
if (!sdev) { if (!sdev) {
dev_err(&interface->dev, "could not get device"); dev_err(&udev->dev, "could not get device");
return; return;
} }
usb_set_intfdata(interface, NULL); dev_set_drvdata(&udev->dev, NULL);
/* /*
* NOTE: rx/tx threads are invoked for each usb_device. * NOTE: rx/tx threads are invoked for each usb_device.
*/ */
stub_remove_files(&interface->dev); stub_remove_files(&udev->dev);
/* If usb reset is called from event handler */ /* If usb reset is called from event handler */
if (busid_priv->sdev->ud.eh == current) { if (busid_priv->sdev->ud.eh == current)
busid_priv->interf_count--;
return; return;
}
if (busid_priv->interf_count > 1) {
busid_priv->interf_count--;
shutdown_busid(busid_priv);
usb_put_intf(interface);
return;
}
busid_priv->interf_count = 0;
/* shutdown the current connection */ /* shutdown the current connection */
shutdown_busid(busid_priv); shutdown_busid(busid_priv);
usb_put_dev(sdev->udev); usb_put_dev(sdev->udev);
usb_put_intf(interface);
/* free sdev */ /* free sdev */
busid_priv->sdev = NULL; busid_priv->sdev = NULL;
...@@ -523,28 +473,34 @@ static void stub_disconnect(struct usb_interface *interface) ...@@ -523,28 +473,34 @@ static void stub_disconnect(struct usb_interface *interface)
} }
} }
/* #ifdef CONFIG_PM
* Presence of pre_reset and post_reset prevents the driver from being unbound
* when the device is being reset
*/
static int stub_pre_reset(struct usb_interface *interface) /* These functions need usb_port_suspend and usb_port_resume,
* which reside in drivers/usb/core/usb.h. Skip for now. */
static int stub_suspend(struct usb_device *udev, pm_message_t message)
{ {
dev_dbg(&interface->dev, "pre_reset\n"); dev_dbg(&udev->dev, "stub_suspend\n");
return 0; return 0;
} }
static int stub_post_reset(struct usb_interface *interface) static int stub_resume(struct usb_device *udev, pm_message_t message)
{ {
dev_dbg(&interface->dev, "post_reset\n"); dev_dbg(&udev->dev, "stub_resume\n");
return 0; return 0;
} }
struct usb_driver stub_driver = { #endif /* CONFIG_PM */
struct usb_device_driver stub_driver = {
.name = "usbip-host", .name = "usbip-host",
.probe = stub_probe, .probe = stub_probe,
.disconnect = stub_disconnect, .disconnect = stub_disconnect,
.id_table = stub_table, #ifdef CONFIG_PM
.pre_reset = stub_pre_reset, .suspend = stub_suspend,
.post_reset = stub_post_reset, .resume = stub_resume,
#endif
.supports_autosuspend = 0,
}; };
...@@ -254,7 +254,7 @@ static int __init usbip_host_init(void) ...@@ -254,7 +254,7 @@ static int __init usbip_host_init(void)
return -ENOMEM; return -ENOMEM;
} }
ret = usb_register(&stub_driver); ret = usb_register_device_driver(&stub_driver, THIS_MODULE);
if (ret) { if (ret) {
pr_err("usb_register failed %d\n", ret); pr_err("usb_register failed %d\n", ret);
goto err_usb_register; goto err_usb_register;
...@@ -271,7 +271,7 @@ static int __init usbip_host_init(void) ...@@ -271,7 +271,7 @@ static int __init usbip_host_init(void)
return ret; return ret;
err_create_file: err_create_file:
usb_deregister(&stub_driver); usb_deregister_device_driver(&stub_driver);
err_usb_register: err_usb_register:
kmem_cache_destroy(stub_priv_cache); kmem_cache_destroy(stub_priv_cache);
return ret; return ret;
...@@ -286,7 +286,7 @@ static void __exit usbip_host_exit(void) ...@@ -286,7 +286,7 @@ static void __exit usbip_host_exit(void)
* deregister() calls stub_disconnect() for all devices. Device * deregister() calls stub_disconnect() for all devices. Device
* specific data is cleared in stub_disconnect(). * specific data is cleared in stub_disconnect().
*/ */
usb_deregister(&stub_driver); usb_deregister_device_driver(&stub_driver);
kmem_cache_destroy(stub_priv_cache); kmem_cache_destroy(stub_priv_cache);
} }
......
...@@ -550,7 +550,7 @@ static void stub_rx_pdu(struct usbip_device *ud) ...@@ -550,7 +550,7 @@ static void stub_rx_pdu(struct usbip_device *ud)
int ret; int ret;
struct usbip_header pdu; struct usbip_header pdu;
struct stub_device *sdev = container_of(ud, struct stub_device, ud); struct stub_device *sdev = container_of(ud, struct stub_device, ud);
struct device *dev = &sdev->interface->dev; struct device *dev = &sdev->udev->dev;
usbip_dbg_stub_rx("Enter\n"); usbip_dbg_stub_rx("Enter\n");
......
...@@ -32,7 +32,6 @@ struct usbip_host_driver *host_driver; ...@@ -32,7 +32,6 @@ struct usbip_host_driver *host_driver;
#define SYSFS_OPEN_RETRIES 100 #define SYSFS_OPEN_RETRIES 100
/* only the first interface value is true! */
static int32_t read_attr_usbip_status(struct usbip_usb_device *udev) static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
{ {
char attrpath[SYSFS_PATH_MAX]; char attrpath[SYSFS_PATH_MAX];
...@@ -56,8 +55,8 @@ static int32_t read_attr_usbip_status(struct usbip_usb_device *udev) ...@@ -56,8 +55,8 @@ static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
* usbip_status to reappear. * usbip_status to reappear.
*/ */
snprintf(attrpath, SYSFS_PATH_MAX, "%s/%s:%d.%d/usbip_status", snprintf(attrpath, SYSFS_PATH_MAX, "%s/usbip_status",
udev->path, udev->busid, udev->bConfigurationValue, 0); udev->path);
while (retries > 0) { while (retries > 0) {
if (stat(attrpath, &s) == 0) if (stat(attrpath, &s) == 0)
...@@ -168,19 +167,18 @@ static void delete_nothing(void *unused_data) ...@@ -168,19 +167,18 @@ static void delete_nothing(void *unused_data)
static int refresh_exported_devices(void) static int refresh_exported_devices(void)
{ {
/* sysfs_device of usb_interface */
struct sysfs_device *suintf;
struct dlist *suintf_list;
/* sysfs_device of usb_device */ /* sysfs_device of usb_device */
struct sysfs_device *sudev; struct sysfs_device *sudev;
struct dlist *sudev_list; struct dlist *sudev_list;
struct dlist *sudev_unique_list;
struct usbip_exported_device *edev; struct usbip_exported_device *edev;
sudev_list = dlist_new_with_delete(sizeof(struct sysfs_device), sudev_unique_list = dlist_new_with_delete(sizeof(struct sysfs_device),
delete_nothing); delete_nothing);
suintf_list = sysfs_get_driver_devices(host_driver->sysfs_driver); sudev_list = sysfs_get_driver_devices(host_driver->sysfs_driver);
if (!suintf_list) {
if (!sudev_list) {
/* /*
* Not an error condition. There are simply no devices bound to * Not an error condition. There are simply no devices bound to
* the driver yet. * the driver yet.
...@@ -190,23 +188,13 @@ static int refresh_exported_devices(void) ...@@ -190,23 +188,13 @@ static int refresh_exported_devices(void)
return 0; return 0;
} }
/* collect unique USB devices (not interfaces) */ dlist_for_each_data(sudev_list, sudev, struct sysfs_device)
dlist_for_each_data(suintf_list, suintf, struct sysfs_device) { if (check_new(sudev_unique_list, sudev))
/* get usb device of this usb interface */ dlist_unshift(sudev_unique_list, sudev);
sudev = sysfs_get_device_parent(suintf);
if (!sudev) {
dbg("sysfs_get_device_parent failed: %s", suintf->name);
continue;
}
if (check_new(sudev_list, sudev)) { dlist_for_each_data(sudev_unique_list, sudev, struct sysfs_device) {
/* insert item at head of list */
dlist_unshift(sudev_list, sudev);
}
}
dlist_for_each_data(sudev_list, sudev, struct sysfs_device) {
edev = usbip_exported_device_new(sudev->path); edev = usbip_exported_device_new(sudev->path);
if (!edev) { if (!edev) {
dbg("usbip_exported_device_new failed"); dbg("usbip_exported_device_new failed");
continue; continue;
...@@ -216,7 +204,7 @@ static int refresh_exported_devices(void) ...@@ -216,7 +204,7 @@ static int refresh_exported_devices(void)
host_driver->ndevs++; host_driver->ndevs++;
} }
dlist_destroy(sudev_list); dlist_destroy(sudev_unique_list);
return 0; return 0;
} }
...@@ -356,9 +344,8 @@ int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd) ...@@ -356,9 +344,8 @@ int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd)
} }
/* only the first interface is true */ /* only the first interface is true */
snprintf(attr_path, sizeof(attr_path), "%s/%s:%d.%d/%s", snprintf(attr_path, sizeof(attr_path), "%s/%s",
edev->udev.path, edev->udev.busid, edev->udev.path, attr_name);
edev->udev.bConfigurationValue, 0, attr_name);
attr = sysfs_open_attribute(attr_path); attr = sysfs_open_attribute(attr_path);
if (!attr) { if (!attr) {
......
...@@ -52,12 +52,8 @@ static int bind_usbip(char *busid) ...@@ -52,12 +52,8 @@ static int bind_usbip(char *busid)
char attr_name[] = "bind"; char attr_name[] = "bind";
char sysfs_mntpath[SYSFS_PATH_MAX]; char sysfs_mntpath[SYSFS_PATH_MAX];
char bind_attr_path[SYSFS_PATH_MAX]; char bind_attr_path[SYSFS_PATH_MAX];
char intf_busid[SYSFS_BUS_ID_SIZE];
struct sysfs_device *busid_dev;
struct sysfs_attribute *bind_attr; struct sysfs_attribute *bind_attr;
struct sysfs_attribute *bConfValue; int failed = 0;
struct sysfs_attribute *bNumIntfs;
int i, failed = 0;
int rc, ret = -1; int rc, ret = -1;
rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX); rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
...@@ -76,39 +72,15 @@ static int bind_usbip(char *busid) ...@@ -76,39 +72,15 @@ static int bind_usbip(char *busid)
return -1; return -1;
} }
busid_dev = sysfs_open_device(bus_type, busid); rc = sysfs_write_attribute(bind_attr, busid, SYSFS_BUS_ID_SIZE);
if (!busid_dev) { if (rc < 0) {
dbg("sysfs_open_device %s failed: %s", busid, strerror(errno)); dbg("bind driver at %s failed", busid);
goto err_close_bind_attr; failed = 1;
}
bConfValue = sysfs_get_device_attr(busid_dev, "bConfigurationValue");
bNumIntfs = sysfs_get_device_attr(busid_dev, "bNumInterfaces");
if (!bConfValue || !bNumIntfs) {
dbg("problem getting device attributes: %s",
strerror(errno));
goto err_close_busid_dev;
}
for (i = 0; i < atoi(bNumIntfs->value); i++) {
snprintf(intf_busid, SYSFS_BUS_ID_SIZE, "%s:%.1s.%d", busid,
bConfValue->value, i);
rc = sysfs_write_attribute(bind_attr, intf_busid,
SYSFS_BUS_ID_SIZE);
if (rc < 0) {
dbg("bind driver at %s failed", intf_busid);
failed = 1;
}
} }
if (!failed) if (!failed)
ret = 0; ret = 0;
err_close_busid_dev:
sysfs_close_device(busid_dev);
err_close_bind_attr:
sysfs_close_attribute(bind_attr); sysfs_close_attribute(bind_attr);
return ret; return ret;
...@@ -118,15 +90,12 @@ static int bind_usbip(char *busid) ...@@ -118,15 +90,12 @@ static int bind_usbip(char *busid)
static int unbind_other(char *busid) static int unbind_other(char *busid)
{ {
char bus_type[] = "usb"; char bus_type[] = "usb";
char intf_busid[SYSFS_BUS_ID_SIZE];
struct sysfs_device *busid_dev; struct sysfs_device *busid_dev;
struct sysfs_device *intf_dev; struct sysfs_device *dev;
struct sysfs_driver *intf_drv; struct sysfs_driver *drv;
struct sysfs_attribute *unbind_attr; struct sysfs_attribute *unbind_attr;
struct sysfs_attribute *bConfValue;
struct sysfs_attribute *bDevClass; struct sysfs_attribute *bDevClass;
struct sysfs_attribute *bNumIntfs; int rc;
int i, rc;
enum unbind_status status = UNBIND_ST_OK; enum unbind_status status = UNBIND_ST_OK;
busid_dev = sysfs_open_device(bus_type, busid); busid_dev = sysfs_open_device(bus_type, busid);
...@@ -134,12 +103,11 @@ static int unbind_other(char *busid) ...@@ -134,12 +103,11 @@ static int unbind_other(char *busid)
dbg("sysfs_open_device %s failed: %s", busid, strerror(errno)); dbg("sysfs_open_device %s failed: %s", busid, strerror(errno));
return -1; return -1;
} }
dbg("busid path: %s", busid_dev->path);
bConfValue = sysfs_get_device_attr(busid_dev, "bConfigurationValue");
bDevClass = sysfs_get_device_attr(busid_dev, "bDeviceClass"); bDevClass = sysfs_get_device_attr(busid_dev, "bDeviceClass");
bNumIntfs = sysfs_get_device_attr(busid_dev, "bNumInterfaces"); if (!bDevClass) {
if (!bConfValue || !bDevClass || !bNumIntfs) { dbg("problem getting device attribute: %s",
dbg("problem getting device attributes: %s",
strerror(errno)); strerror(errno));
goto err_close_busid_dev; goto err_close_busid_dev;
} }
...@@ -149,62 +117,62 @@ static int unbind_other(char *busid) ...@@ -149,62 +117,62 @@ static int unbind_other(char *busid)
goto err_close_busid_dev; goto err_close_busid_dev;
} }
for (i = 0; i < atoi(bNumIntfs->value); i++) { dev = sysfs_open_device(bus_type, busid);
snprintf(intf_busid, SYSFS_BUS_ID_SIZE, "%s:%.1s.%d", busid, if (!dev) {
bConfValue->value, i); dbg("could not open device: %s",
intf_dev = sysfs_open_device(bus_type, intf_busid); strerror(errno));
if (!intf_dev) { goto err_close_busid_dev;
dbg("could not open interface device: %s", }
strerror(errno));
goto err_close_busid_dev;
}
dbg("%s -> %s", intf_dev->name, intf_dev->driver_name);
if (!strncmp("unknown", intf_dev->driver_name, SYSFS_NAME_LEN)) dbg("%s -> %s", dev->name, dev->driver_name);
/* unbound interface */
continue;
if (!strncmp(USBIP_HOST_DRV_NAME, intf_dev->driver_name, if (!strncmp("unknown", dev->driver_name, SYSFS_NAME_LEN)) {
SYSFS_NAME_LEN)) { /* unbound interface */
/* already bound to usbip-host */ sysfs_close_device(dev);
status = UNBIND_ST_USBIP_HOST; goto out;
continue; }
}
/* unbinding */ if (!strncmp(USBIP_HOST_DRV_NAME, dev->driver_name,
intf_drv = sysfs_open_driver(bus_type, intf_dev->driver_name); SYSFS_NAME_LEN)) {
if (!intf_drv) { /* already bound to usbip-host */
dbg("could not open interface driver on %s: %s", status = UNBIND_ST_USBIP_HOST;
intf_dev->name, strerror(errno)); sysfs_close_device(dev);
goto err_close_intf_dev; goto out;
} }
unbind_attr = sysfs_get_driver_attr(intf_drv, "unbind"); /* unbinding */
if (!unbind_attr) { drv = sysfs_open_driver(bus_type, dev->driver_name);
dbg("problem getting interface driver attribute: %s", if (!drv) {
strerror(errno)); dbg("could not open device driver on %s: %s",
goto err_close_intf_drv; dev->name, strerror(errno));
} goto err_close_intf_dev;
}
dbg("device driver: %s", drv->path);
rc = sysfs_write_attribute(unbind_attr, intf_dev->bus_id, unbind_attr = sysfs_get_driver_attr(drv, "unbind");
SYSFS_BUS_ID_SIZE); if (!unbind_attr) {
if (rc < 0) { dbg("problem getting device driver attribute: %s",
/* NOTE: why keep unbinding other interfaces? */ strerror(errno));
dbg("unbind driver at %s failed", intf_dev->bus_id); goto err_close_intf_drv;
status = UNBIND_ST_FAILED; }
}
sysfs_close_driver(intf_drv); rc = sysfs_write_attribute(unbind_attr, dev->bus_id,
sysfs_close_device(intf_dev); SYSFS_BUS_ID_SIZE);
if (rc < 0) {
/* NOTE: why keep unbinding other interfaces? */
dbg("unbind driver at %s failed", dev->bus_id);
status = UNBIND_ST_FAILED;
} }
sysfs_close_driver(drv);
sysfs_close_device(dev);
goto out; goto out;
err_close_intf_drv: err_close_intf_drv:
sysfs_close_driver(intf_drv); sysfs_close_driver(drv);
err_close_intf_dev: err_close_intf_dev:
sysfs_close_device(intf_dev); sysfs_close_device(dev);
err_close_busid_dev: err_close_busid_dev:
status = UNBIND_ST_FAILED; status = UNBIND_ST_FAILED;
out: out:
......
...@@ -52,9 +52,8 @@ static int get_exported_devices(char *host, int sockfd) ...@@ -52,9 +52,8 @@ static int get_exported_devices(char *host, int sockfd)
struct op_devlist_reply reply; struct op_devlist_reply reply;
uint16_t code = OP_REP_DEVLIST; uint16_t code = OP_REP_DEVLIST;
struct usbip_usb_device udev; struct usbip_usb_device udev;
struct usbip_usb_interface uintf;
unsigned int i; unsigned int i;
int j, rc; int rc;
rc = usbip_net_send_op_common(sockfd, OP_REQ_DEVLIST, 0); rc = usbip_net_send_op_common(sockfd, OP_REQ_DEVLIST, 0);
if (rc < 0) { if (rc < 0) {
...@@ -104,22 +103,6 @@ static int get_exported_devices(char *host, int sockfd) ...@@ -104,22 +103,6 @@ static int get_exported_devices(char *host, int sockfd)
printf("%11s: %s\n", "", udev.path); printf("%11s: %s\n", "", udev.path);
printf("%11s: %s\n", "", class_name); printf("%11s: %s\n", "", class_name);
for (j = 0; j < udev.bNumInterfaces; j++) {
rc = usbip_net_recv(sockfd, &uintf, sizeof(uintf));
if (rc < 0) {
dbg("usbip_net_recv failed: usbip_usb_intf[%d]",
j);
return -1;
}
usbip_net_pack_usb_interface(0, &uintf);
usbip_names_get_class(class_name, sizeof(class_name),
uintf.bInterfaceClass,
uintf.bInterfaceSubClass,
uintf.bInterfaceProtocol);
printf("%11s: %2d - %s\n", "", j, class_name);
}
printf("\n"); printf("\n");
} }
......
...@@ -47,12 +47,10 @@ static int unbind_device(char *busid) ...@@ -47,12 +47,10 @@ static int unbind_device(char *busid)
int verified = 0; int verified = 0;
int rc, ret = -1; int rc, ret = -1;
char attr_name[] = "bConfigurationValue"; char attr_name[] = "unbind";
char sysfs_mntpath[SYSFS_PATH_MAX]; char sysfs_mntpath[SYSFS_PATH_MAX];
char busid_attr_path[SYSFS_PATH_MAX]; char unbind_attr_path[SYSFS_PATH_MAX];
struct sysfs_attribute *busid_attr; struct sysfs_attribute *unbind_attr;
char *val = NULL;
int len;
/* verify the busid device is using usbip-host */ /* verify the busid device is using usbip-host */
usbip_host_drv = sysfs_open_driver(bus_type, USBIP_HOST_DRV_NAME); usbip_host_drv = sysfs_open_driver(bus_type, USBIP_HOST_DRV_NAME);
...@@ -99,55 +97,34 @@ static int unbind_device(char *busid) ...@@ -99,55 +97,34 @@ static int unbind_device(char *busid)
return -1; return -1;
} }
snprintf(busid_attr_path, sizeof(busid_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_DEVICES_NAME, sysfs_mntpath, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
busid, attr_name); USBIP_HOST_DRV_NAME, attr_name);
/* read a device attribute */ /* read a device attribute */
busid_attr = sysfs_open_attribute(busid_attr_path); unbind_attr = sysfs_open_attribute(unbind_attr_path);
if (!busid_attr) { if (!unbind_attr) {
err("could not open %s/%s: %s", busid, attr_name, err("could not open %s/%s: %s", busid, attr_name,
strerror(errno)); strerror(errno));
return -1; return -1;
} }
if (sysfs_read_attribute(busid_attr) < 0) {
err("problem reading attribute: %s", strerror(errno));
goto err_out;
}
len = busid_attr->len;
val = malloc(len);
*val = *busid_attr->value;
sysfs_close_attribute(busid_attr);
/* 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_out;
}
/* write the device attribute */
busid_attr = sysfs_open_attribute(busid_attr_path);
if (!busid_attr) {
err("could not open %s/%s: %s", busid, attr_name,
strerror(errno));
return -1;
} }
rc = sysfs_write_attribute(busid_attr, val, len); rc = sysfs_write_attribute(unbind_attr, busid,
if (rc < 0) { SYSFS_BUS_ID_SIZE);
err("problem writing attribute: %s", strerror(errno)); if (rc < 0) {
goto err_out; dbg("bind driver at %s failed", busid);
} }
sysfs_close_attribute(busid_attr); sysfs_close_attribute(unbind_attr);
ret = 0; ret = 0;
printf("unbind device on busid %s: complete\n", busid); printf("unbind device on busid %s: complete\n", busid);
err_out:
free(val);
err_close_usbip_host_drv: err_close_usbip_host_drv:
sysfs_close_driver(usbip_host_drv); sysfs_close_driver(usbip_host_drv);
......
...@@ -159,9 +159,7 @@ static int send_reply_devlist(int connfd) ...@@ -159,9 +159,7 @@ static int send_reply_devlist(int connfd)
{ {
struct usbip_exported_device *edev; struct usbip_exported_device *edev;
struct usbip_usb_device pdu_udev; struct usbip_usb_device pdu_udev;
struct usbip_usb_interface pdu_uinf;
struct op_devlist_reply reply; struct op_devlist_reply reply;
int i;
int rc; int rc;
reply.ndev = 0; reply.ndev = 0;
...@@ -196,19 +194,6 @@ static int send_reply_devlist(int connfd) ...@@ -196,19 +194,6 @@ static int send_reply_devlist(int connfd)
dbg("usbip_net_send failed: pdu_udev"); dbg("usbip_net_send failed: pdu_udev");
return -1; return -1;
} }
for (i = 0; i < edev->udev.bNumInterfaces; i++) {
dump_usb_interface(&edev->uinf[i]);
memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf));
usbip_net_pack_usb_interface(1, &pdu_uinf);
rc = usbip_net_send(connfd, &pdu_uinf,
sizeof(pdu_uinf));
if (rc < 0) {
dbg("usbip_net_send failed: pdu_uinf");
return -1;
}
}
} }
return 0; return 0;
......
...@@ -155,6 +155,7 @@ int usb_choose_configuration(struct usb_device *udev) ...@@ -155,6 +155,7 @@ int usb_choose_configuration(struct usb_device *udev)
} }
return i; return i;
} }
EXPORT_SYMBOL_GPL(usb_choose_configuration);
static int generic_probe(struct usb_device *udev) static int generic_probe(struct usb_device *udev)
{ {
......
...@@ -1920,6 +1920,7 @@ int usb_set_configuration(struct usb_device *dev, int configuration) ...@@ -1920,6 +1920,7 @@ int usb_set_configuration(struct usb_device *dev, int configuration)
usb_autosuspend_device(dev); usb_autosuspend_device(dev);
return 0; return 0;
} }
EXPORT_SYMBOL_GPL(usb_set_configuration);
static LIST_HEAD(set_config_list); static LIST_HEAD(set_config_list);
static DEFINE_SPINLOCK(set_config_lock); static DEFINE_SPINLOCK(set_config_lock);
......
...@@ -1668,6 +1668,10 @@ extern void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr); ...@@ -1668,6 +1668,10 @@ extern void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr);
/* this request isn't really synchronous, but it belongs with the others */ /* this request isn't really synchronous, but it belongs with the others */
extern int usb_driver_set_configuration(struct usb_device *udev, int config); extern int usb_driver_set_configuration(struct usb_device *udev, int config);
/* choose and set configuration for device */
extern int usb_choose_configuration(struct usb_device *udev);
extern int usb_set_configuration(struct usb_device *dev, int configuration);
/* /*
* timeouts, in milliseconds, used for sending/receiving control messages * timeouts, in milliseconds, used for sending/receiving control messages
* they typically complete within a few frames (msec) after they're issued * they typically complete within a few frames (msec) after they're issued
......
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