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

staging: usbip: userspace: migrate usbip_list to libudev

This patch modifies usbip_list to use libudev.
Signed-off-by: default avatarValentina Manea <valentina.manea.m@gmail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent d561ef95
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
*/ */
#include <sys/types.h> #include <sys/types.h>
#include <sysfs/libsysfs.h> #include <libudev.h>
#include <errno.h> #include <errno.h>
#include <stdbool.h> #include <stdbool.h>
...@@ -133,8 +133,8 @@ static int list_exported_devices(char *host) ...@@ -133,8 +133,8 @@ static int list_exported_devices(char *host)
return 0; return 0;
} }
static void print_device(char *busid, char *vendor, char *product, static void print_device(const char *busid, const char *vendor,
bool parsable) const char *product, bool parsable)
{ {
if (parsable) if (parsable)
printf("busid=%s#usbid=%.4s:%.4s#", busid, vendor, product); printf("busid=%s#usbid=%.4s:%.4s#", busid, vendor, product);
...@@ -148,106 +148,73 @@ static void print_product_name(char *product_name, bool parsable) ...@@ -148,106 +148,73 @@ static void print_product_name(char *product_name, bool parsable)
printf(" %s\n", product_name); printf(" %s\n", product_name);
} }
static void print_interface(char *busid, char *driver, bool parsable)
{
if (parsable)
printf("%s=%s#", busid, driver);
else
printf("%9s%s -> %s\n", "", busid, driver);
}
static int is_device(void *x)
{
struct sysfs_attribute *devpath;
struct sysfs_device *dev = x;
int ret = 0;
devpath = sysfs_get_device_attr(dev, "devpath");
if (devpath && *devpath->value != '0')
ret = 1;
return ret;
}
static int devcmp(void *a, void *b)
{
return strcmp(a, b);
}
static int list_devices(bool parsable) static int list_devices(bool parsable)
{ {
char bus_type[] = "usb"; struct udev *udev;
char busid[SYSFS_BUS_ID_SIZE]; struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device *dev;
const char *path;
const char *idVendor;
const char *idProduct;
const char *bConfValue;
const char *bNumIntfs;
const char *busid;
char product_name[128]; char product_name[128];
struct sysfs_bus *ubus;
struct sysfs_device *dev;
struct sysfs_device *intf;
struct sysfs_attribute *idVendor;
struct sysfs_attribute *idProduct;
struct sysfs_attribute *bConfValue;
struct sysfs_attribute *bNumIntfs;
struct dlist *devlist;
int i;
int ret = -1; int ret = -1;
ubus = sysfs_open_bus(bus_type); /* Create libudev context. */
if (!ubus) { udev = udev_new();
err("could not open %s bus: %s", bus_type, strerror(errno));
return -1;
}
devlist = sysfs_get_bus_devices(ubus); /* Create libudev device enumeration. */
if (!devlist) { enumerate = udev_enumerate_new(udev);
err("could not get %s bus devices: %s", bus_type,
strerror(errno));
goto err_out;
}
/* remove interfaces and root hubs from device list */
dlist_filter_sort(devlist, is_device, devcmp);
if (!parsable) { /* Take only USB devices that are not hubs and do not have
printf("Local USB devices\n"); * the bInterfaceNumber attribute, i.e. are not interfaces.
printf("=================\n"); */
} udev_enumerate_add_match_subsystem(enumerate, "usb");
dlist_for_each_data(devlist, dev, struct sysfs_device) { udev_enumerate_add_nomatch_sysattr(enumerate, "bDeviceClass", "09");
idVendor = sysfs_get_device_attr(dev, "idVendor"); udev_enumerate_add_nomatch_sysattr(enumerate, "bInterfaceNumber", NULL);
idProduct = sysfs_get_device_attr(dev, "idProduct"); udev_enumerate_scan_devices(enumerate);
bConfValue = sysfs_get_device_attr(dev, "bConfigurationValue");
bNumIntfs = sysfs_get_device_attr(dev, "bNumInterfaces"); devices = udev_enumerate_get_list_entry(enumerate);
/* Show information about each device. */
udev_list_entry_foreach(dev_list_entry, devices) {
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev, path);
/* Get device information. */
idVendor = udev_device_get_sysattr_value(dev, "idVendor");
idProduct = udev_device_get_sysattr_value(dev, "idProduct");
bConfValue = udev_device_get_sysattr_value(dev, "bConfigurationValue");
bNumIntfs = udev_device_get_sysattr_value(dev, "bNumInterfaces");
busid = udev_device_get_sysname(dev);
if (!idVendor || !idProduct || !bConfValue || !bNumIntfs) { if (!idVendor || !idProduct || !bConfValue || !bNumIntfs) {
err("problem getting device attributes: %s", err("problem getting device attributes: %s",
strerror(errno)); strerror(errno));
goto err_out; goto err_out;
} }
/* get product name */ /* Get product name. */
usbip_names_get_product(product_name, sizeof(product_name), usbip_names_get_product(product_name, sizeof(product_name),
strtol(idVendor->value, NULL, 16), strtol(idVendor, NULL, 16),
strtol(idProduct->value, NULL, 16)); strtol(idProduct, NULL, 16));
print_device(dev->bus_id, idVendor->value, idProduct->value,
parsable); /* Print information. */
print_device(busid, idVendor, idProduct, parsable);
print_product_name(product_name, parsable); print_product_name(product_name, parsable);
for (i = 0; i < atoi(bNumIntfs->value); i++) {
snprintf(busid, sizeof(busid), "%s:%.1s.%d",
dev->bus_id, bConfValue->value, i);
intf = sysfs_open_device(bus_type, busid);
if (!intf) {
err("could not open device interface: %s",
strerror(errno));
goto err_out;
}
print_interface(busid, intf->driver_name, parsable);
sysfs_close_device(intf);
}
printf("\n"); printf("\n");
udev_device_unref(dev);
} }
ret = 0; ret = 0;
err_out: err_out:
sysfs_close_bus(ubus); udev_enumerate_unref(enumerate);
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