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

staging: usbip: userspace: migrate usbip_host_driver to libudev

This patch modifies usbip_host_driver 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 ecc13b72
......@@ -2,6 +2,7 @@
* Copyright (C) 2005-2007 Takahiro Hirofuchi
*/
#include <libudev.h>
#include "usbip_common.h"
#include "names.h"
......@@ -12,6 +13,8 @@ int usbip_use_syslog;
int usbip_use_stderr;
int usbip_use_debug;
extern struct udev *udev_context;
struct speed_string {
int num;
char *speed;
......@@ -111,75 +114,48 @@ void dump_usb_device(struct usbip_usb_device *udev)
}
int read_attr_value(struct sysfs_device *dev, const char *name,
int read_attr_value(struct udev_device *dev, const char *name,
const char *format)
{
char attrpath[SYSFS_PATH_MAX];
struct sysfs_attribute *attr;
const char *attr;
int num = 0;
int ret;
snprintf(attrpath, sizeof(attrpath), "%s/%s", dev->path, name);
attr = sysfs_open_attribute(attrpath);
attr = udev_device_get_sysattr_value(dev, name);
if (!attr) {
dbg("sysfs_open_attribute failed: %s", attrpath);
return 0;
}
ret = sysfs_read_attribute(attr);
if (ret < 0) {
dbg("sysfs_read_attribute failed");
err("udev_device_get_sysattr_value failed");
goto err;
}
ret = sscanf(attr->value, format, &num);
ret = sscanf(attr, format, &num);
if (ret < 1) {
dbg("sscanf failed");
err("sscanf failed");
goto err;
}
err:
sysfs_close_attribute(attr);
return num;
}
int read_attr_speed(struct sysfs_device *dev)
int read_attr_speed(struct udev_device *dev)
{
char attrpath[SYSFS_PATH_MAX];
struct sysfs_attribute *attr;
char speed[100];
int ret;
snprintf(attrpath, sizeof(attrpath), "%s/%s", dev->path, "speed");
attr = sysfs_open_attribute(attrpath);
if (!attr) {
dbg("sysfs_open_attribute failed: %s", attrpath);
return 0;
}
const char *speed;
ret = sysfs_read_attribute(attr);
if (ret < 0) {
dbg("sysfs_read_attribute failed");
goto err;
}
ret = sscanf(attr->value, "%99s\n", speed);
if (ret < 1) {
dbg("sscanf failed");
speed = udev_device_get_sysattr_value(dev, "speed");
if (!speed) {
err("udev_device_get_sysattr_value failed");
goto err;
}
err:
sysfs_close_attribute(attr);
for (int i = 0; speed_strings[i].speed != NULL; i++) {
if (!strcmp(speed, speed_strings[i].speed))
return speed_strings[i].num;
}
err:
return USB_SPEED_UNKNOWN;
}
......@@ -190,9 +166,10 @@ int read_attr_speed(struct sysfs_device *dev)
} while (0)
int read_usb_device(struct sysfs_device *sdev, struct usbip_usb_device *udev)
int read_usb_device(struct udev_device *sdev, struct usbip_usb_device *udev)
{
uint32_t busnum, devnum;
const char *path, *name;
READ_ATTR(udev, uint8_t, sdev, bDeviceClass, "%02x\n");
READ_ATTR(udev, uint8_t, sdev, bDeviceSubClass, "%02x\n");
......@@ -209,10 +186,13 @@ int read_usb_device(struct sysfs_device *sdev, struct usbip_usb_device *udev)
READ_ATTR(udev, uint8_t, sdev, devnum, "%d\n");
udev->speed = read_attr_speed(sdev);
strncpy(udev->path, sdev->path, SYSFS_PATH_MAX);
strncpy(udev->busid, sdev->name, SYSFS_BUS_ID_SIZE);
path = udev_device_get_syspath(sdev);
name = udev_device_get_sysname(sdev);
sscanf(sdev->name, "%u-%u", &busnum, &devnum);
strncpy(udev->path, path, SYSFS_PATH_MAX);
strncpy(udev->busid, name, SYSFS_BUS_ID_SIZE);
sscanf(name, "%u-%u", &busnum, &devnum);
udev->busnum = busnum;
return 0;
......@@ -222,13 +202,13 @@ int read_usb_interface(struct usbip_usb_device *udev, int i,
struct usbip_usb_interface *uinf)
{
char busid[SYSFS_BUS_ID_SIZE];
struct sysfs_device *sif;
struct udev_device *sif;
sprintf(busid, "%s:%d.%d", udev->busid, udev->bConfigurationValue, i);
sif = sysfs_open_device("usb", busid);
sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid);
if (!sif) {
dbg("sysfs_open_device(\"usb\", \"%s\") failed", busid);
err("udev_device_new_from_subsystem_sysname %s failed", busid);
return -1;
}
......@@ -236,8 +216,6 @@ int read_usb_interface(struct usbip_usb_device *udev, int i,
READ_ATTR(uinf, uint8_t, sif, bInterfaceSubClass, "%02x\n");
READ_ATTR(uinf, uint8_t, sif, bInterfaceProtocol, "%02x\n");
sysfs_close_device(sif);
return 0;
}
......
......@@ -6,6 +6,7 @@
#define __USBIP_COMMON_H
#include <sysfs/libsysfs.h>
#include <libudev.h>
#include <stdint.h>
#include <stdio.h>
......@@ -118,8 +119,8 @@ struct usbip_usb_device {
void dump_usb_interface(struct usbip_usb_interface *);
void dump_usb_device(struct usbip_usb_device *);
int read_usb_device(struct sysfs_device *sdev, struct usbip_usb_device *udev);
int read_attr_value(struct sysfs_device *dev, const char *name,
int read_usb_device(struct udev_device *sdev, struct usbip_usb_device *udev);
int read_attr_value(struct udev_device *dev, const char *name,
const char *format);
int read_usb_interface(struct usbip_usb_device *udev, int i,
struct usbip_usb_interface *uinf);
......
......@@ -21,18 +21,19 @@
#include <stdint.h>
#include "usbip_common.h"
#include "list.h"
struct usbip_host_driver {
int ndevs;
struct sysfs_driver *sysfs_driver;
/* list of exported device */
struct dlist *edev_list;
struct list_head edev_list;
};
struct usbip_exported_device {
struct sysfs_device *sudev;
struct udev_device *sudev;
int32_t status;
struct usbip_usb_device udev;
struct list_head node;
struct usbip_usb_interface uinf[];
};
......
......@@ -6,24 +6,27 @@
#include "vhci_driver.h"
#include <limits.h>
#include <netdb.h>
#include <libudev.h>
#undef PROGNAME
#define PROGNAME "libusbip"
struct usbip_vhci_driver *vhci_driver;
struct udev *udev_context;
static struct usbip_imported_device *
imported_device_init(struct usbip_imported_device *idev, char *busid)
{
struct sysfs_device *sudev;
struct udev_device *sudev;
sudev = sysfs_open_device("usb", busid);
sudev = udev_device_new_from_subsystem_sysname(udev_context,
"usb", busid);
if (!sudev) {
dbg("sysfs_open_device failed: %s", busid);
dbg("udev_device_new_from_subsystem_sysname failed: %s", busid);
goto err;
}
read_usb_device(sudev, &idev->udev);
sysfs_close_device(sudev);
udev_device_unref(sudev);
/* add class devices of this imported device */
struct usbip_class_device *cdev;
......@@ -410,6 +413,12 @@ int usbip_vhci_driver_open(void)
int ret;
char hc_busid[SYSFS_BUS_ID_SIZE];
udev_context = udev_new();
if (!udev_context) {
err("udev_new failed");
return -1;
}
vhci_driver = (struct usbip_vhci_driver *) calloc(1, sizeof(*vhci_driver));
if (!vhci_driver) {
dbg("calloc failed");
......@@ -461,6 +470,9 @@ int usbip_vhci_driver_open(void)
free(vhci_driver);
vhci_driver = NULL;
udev_unref(udev_context);
return -1;
}
......@@ -483,6 +495,8 @@ void usbip_vhci_driver_close()
free(vhci_driver);
vhci_driver = NULL;
udev_unref(udev_context);
}
......
......@@ -43,6 +43,7 @@
#include "usbip_host_driver.h"
#include "usbip_common.h"
#include "usbip_network.h"
#include "list.h"
#undef PROGNAME
#define PROGNAME "usbipd"
......@@ -93,6 +94,7 @@ static int recv_request_import(int sockfd)
struct op_common reply;
struct usbip_exported_device *edev;
struct usbip_usb_device pdu_udev;
struct list_head *i;
int found = 0;
int error = 0;
int rc;
......@@ -107,8 +109,8 @@ static int recv_request_import(int sockfd)
}
PACK_OP_IMPORT_REQUEST(0, &req);
dlist_for_each_data(host_driver->edev_list, edev,
struct usbip_exported_device) {
list_for_each(i, &host_driver->edev_list) {
edev = list_entry(i, struct usbip_exported_device, node);
if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
info("found requested device: %s", req.busid);
found = 1;
......@@ -161,12 +163,12 @@ static int send_reply_devlist(int connfd)
struct usbip_usb_device pdu_udev;
struct usbip_usb_interface pdu_uinf;
struct op_devlist_reply reply;
struct list_head *j;
int rc, i;
reply.ndev = 0;
/* number of exported devices */
dlist_for_each_data(host_driver->edev_list, edev,
struct usbip_exported_device) {
list_for_each(j, &host_driver->edev_list) {
reply.ndev += 1;
}
info("exportable devices: %d", reply.ndev);
......@@ -184,8 +186,8 @@ static int send_reply_devlist(int connfd)
return -1;
}
dlist_for_each_data(host_driver->edev_list, edev,
struct usbip_exported_device) {
list_for_each(j, &host_driver->edev_list) {
edev = list_entry(j, struct usbip_exported_device, node);
dump_usb_device(&edev->udev);
memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
usbip_net_pack_usb_device(1, &pdu_udev);
......
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