[PATCH v7 1/2] usbip: vhci extension: modifications to userspace

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Modification to the userspace tools including usbip/libsrc and 
usbip/src.

Changed corresponding to new vhci_sysfs.c.

nports in sysfs is used to get total number of ports. 

Old get_nports() ignores the last status line because 
udev_device_get_sysattr_value() drops last new line. New version uses 
nports attribute so it's doesn't have this problem.

status[.N] in sysfs are used.

parse_status() which reads all status lines is broken into open, close, 
read-line and parse-line. Parse-line is reused to find free port and 
get imported device.

In daemon, status was loaded into memory by 
usbip_vhci_refresh_device_list() at receiving every request. The loaded 
status is used to find free port. It is changed to read status directly 
to find free port.

Wording inconsistencies are fixed according to the rule below.

rhport, HC_PORTS: ports within a controller (or root hub).
port, nports: ports across the controllers.

Signed-off-by: Nobuo Iwata <nobuo.iwata@xxxxxxxxxxxxxxx>
---
 tools/usb/usbip/libsrc/vhci_driver.c | 398 +++++++++++++++------------
 tools/usb/usbip/libsrc/vhci_driver.h |  45 +--
 tools/usb/usbip/src/usbip_attach.c   |   8 +-
 tools/usb/usbip/src/usbip_port.c     |  13 +-
 tools/usb/usbip/src/usbipd_app.c     |  48 ++--
 5 files changed, 253 insertions(+), 259 deletions(-)

diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsrc/vhci_driver.c
index 3fe92ff..720a0e9 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -15,11 +15,24 @@
 #undef  PROGNAME
 #define PROGNAME "libusbip"
 
-struct usbip_vhci_driver *vhci_driver;
-struct udev *udev_context;
+static struct udev_device *vhci_hc_device;
+static struct udev *udev_context;
+static int vhci_nports;
 
-static struct usbip_imported_device *
-imported_device_init(struct usbip_imported_device *idev, char *busid)
+struct usbip_vhci_device {
+	int port;
+	uint32_t status;
+
+	uint32_t devid;
+
+	uint8_t busnum;
+	uint8_t devnum;
+
+	/* usbip_class_device list */
+	struct usbip_usb_device udev;
+};
+
+static int imported_device_init(struct usbip_vhci_device *vdev, char *busid)
 {
 	struct udev_device *sudev;
 
@@ -27,132 +40,131 @@ imported_device_init(struct usbip_imported_device *idev, char *busid)
 						       "usb", busid);
 	if (!sudev) {
 		dbg("udev_device_new_from_subsystem_sysname failed: %s", busid);
-		goto err;
+		return -1;
 	}
-	read_usb_device(sudev, &idev->udev);
+	read_usb_device(sudev, &vdev->udev);
 	udev_device_unref(sudev);
 
-	return idev;
-
-err:
-	return NULL;
+	return 0;
 }
 
+struct status_context {
+	int controller;
+	const char *c;
+};
 
+#define OPEN_MODE_FIRST      0
+#define OPEN_MODE_REOPEN     1
 
-static int parse_status(const char *value)
-{
-	int ret = 0;
-	char *c;
+static int open_hc_device(int mode);
 
+#define MAX_STATUS_NAME 16
 
-	for (int i = 0; i < vhci_driver->nports; i++)
-		memset(&vhci_driver->idev[i], 0, sizeof(vhci_driver->idev[i]));
+static int open_status(struct status_context *ctx, int mode)
+{
+	char name[MAX_STATUS_NAME+1];
 
+	if (mode == OPEN_MODE_FIRST)
+		ctx->controller = 0;
+	else
+		(ctx->controller)++;
 
-	/* skip a header line */
-	c = strchr(value, '\n');
-	if (!c)
+	if (open_hc_device(OPEN_MODE_REOPEN))
 		return -1;
-	c++;
-
-	while (*c != '\0') {
-		int port, status, speed, devid;
-		unsigned long socket;
-		char lbusid[SYSFS_BUS_ID_SIZE];
-
-		ret = sscanf(c, "%d %d %d %x %lx %31s\n",
-				&port, &status, &speed,
-				&devid, &socket, lbusid);
-
-		if (ret < 5) {
-			dbg("sscanf failed: %d", ret);
-			BUG();
-		}
 
-		dbg("port %d status %d speed %d devid %x",
-				port, status, speed, devid);
-		dbg("socket %lx lbusid %s", socket, lbusid);
+	if (ctx->controller == 0)
+		strcpy(name, "status");
+	else
+		snprintf(name, MAX_STATUS_NAME + 1,
+				"status.%d", ctx->controller);
+	ctx->c = udev_device_get_sysattr_value(vhci_hc_device, name);
+	if (ctx->c == NULL)
+		return -1;
 
+	return 0;
+}
 
-		/* if a device is connected, look at it */
-		{
-			struct usbip_imported_device *idev = &vhci_driver->idev[port];
+static void close_status(struct status_context *ctx)
+{
+	ctx->c = NULL;
+}
 
-			idev->port	= port;
-			idev->status	= status;
+static int next_status_line(struct status_context *ctx)
+{
+	const char *c = ctx->c;
 
-			idev->devid	= devid;
+	while ((c = strchr(c, '\n')) == NULL) {
+		if (open_status(ctx, OPEN_MODE_REOPEN))
+			return -1;
+		c = ctx->c;
+	}
+	ctx->c = c + 1;
+	return 0;
+}
 
-			idev->busnum	= (devid >> 16);
-			idev->devnum	= (devid & 0x0000ffff);
+static int parse_status_line(struct status_context *ctx,
+			     struct usbip_vhci_device *vdev)
+{
+	int port, status, speed, devid;
+	unsigned long socket;
+	char lbusid[SYSFS_BUS_ID_SIZE];
+	int ret;
 
-			if (idev->status != VDEV_ST_NULL
-			    && idev->status != VDEV_ST_NOTASSIGNED) {
-				idev = imported_device_init(idev, lbusid);
-				if (!idev) {
-					dbg("imported_device_init failed");
-					return -1;
-				}
-			}
-		}
+	if (next_status_line(ctx))
+		return -1;
 
+	memset(vdev, 0, sizeof(struct usbip_vhci_device));
 
-		/* go to the next line */
-		c = strchr(c, '\n');
-		if (!c)
-			break;
-		c++;
+	if (ctx->c == NULL || *(ctx->c) == '\0') {
+		dbg("no more data to scan");
+		return -1;
 	}
 
-	dbg("exit");
+	ret = sscanf(ctx->c, "%d %d %d %x %lx %31s\n",
+				&port, &status, &speed,
+				&devid, &socket, lbusid);
+	if (ret < 6) {
+		dbg("sscanf failed: %d", ret);
+		return -1;
+	}
 
-	return 0;
-}
+	dbg("port %d status %d speed %d devid %x", port, status, speed, devid);
+	dbg("socket %lx lbusid %s", socket, lbusid);
 
-static int refresh_imported_device_list(void)
-{
-	const char *attr_status;
+	vdev->port	= port;
+	vdev->status	= status;
+	vdev->devid	= devid;
+	vdev->busnum	= (devid >> 16);
+	vdev->devnum	= (devid & 0x0000ffff);
 
-	attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device,
-					       "status");
-	if (!attr_status) {
-		err("udev_device_get_sysattr_value failed");
-		return -1;
+	if (vdev->status != VDEV_ST_NULL &&
+	    vdev->status != VDEV_ST_NOTASSIGNED) {
+		if (imported_device_init(vdev, lbusid)) {
+			dbg("imported_device_init failed");
+			return -1;
+		}
 	}
 
-	return parse_status(attr_status);
+	return 0;
 }
 
 static int get_nports(void)
 {
-	char *c;
-	int nports = 0;
-	const char *attr_status;
+	const char *attr_nports;
 
-	attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device,
-					       "status");
-	if (!attr_status) {
+	attr_nports = udev_device_get_sysattr_value(vhci_hc_device, "nports");
+	if (!attr_nports) {
 		err("udev_device_get_sysattr_value failed");
 		return -1;
 	}
 
-	/* skip a header line */
-	c = strchr(attr_status, '\n');
-	if (!c)
-		return 0;
-	c++;
-
-	while (*c != '\0') {
-		/* go to the next line */
-		c = strchr(c, '\n');
-		if (!c)
-			return nports;
-		c++;
-		nports += 1;
+	vhci_nports = strtol(attr_nports, NULL, 10);
+	if (vhci_nports <= 0) {
+		err("invalid nports value");
+		return -1;
 	}
 
-	return nports;
+	return 0;
 }
 
 /*
@@ -164,32 +176,32 @@ static int get_nports(void)
  * which is needed to properly validate the 3rd part without it being
  * truncated to an acceptable length.
  */
-static int read_record(int rhport, char *host, unsigned long host_len,
-		char *port, unsigned long port_len, char *busid)
+static int read_record(int port, char *host, unsigned long host_len,
+		char *port_s, unsigned long port_slen, char *busid)
 {
 	int part;
 	FILE *file;
 	char path[PATH_MAX+1];
 	char *buffer, *start, *end;
 	char delim[] = {' ', ' ', '\n'};
-	int max_len[] = {(int)host_len, (int)port_len, SYSFS_BUS_ID_SIZE};
-	size_t buffer_len = host_len + port_len + SYSFS_BUS_ID_SIZE + 4;
+	int max_len[] = {(int)host_len, (int)port_slen, SYSFS_BUS_ID_SIZE};
+	size_t buffer_len = host_len + port_slen + SYSFS_BUS_ID_SIZE + 4;
 
-	buffer = malloc(buffer_len);
+	buffer = (char *)malloc(buffer_len);
 	if (!buffer)
 		return -1;
 
-	snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
+	snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", port);
 
 	file = fopen(path, "r");
 	if (!file) {
-		err("fopen");
+		err("fopen %s", path);
 		free(buffer);
 		return -1;
 	}
 
 	if (fgets(buffer, buffer_len, file) == NULL) {
-		err("fgets");
+		err("fgets %s", path);
 		free(buffer);
 		fclose(file);
 		return -1;
@@ -207,7 +219,7 @@ static int read_record(int rhport, char *host, unsigned long host_len,
 		start = end + 1;
 	}
 
-	if (sscanf(buffer, "%s %s %s\n", host, port, busid) != 3) {
+	if (sscanf(buffer, "%s %s %s\n", host, port_s, busid) != 3) {
 		err("sscanf");
 		free(buffer);
 		return -1;
@@ -218,25 +230,28 @@ static int read_record(int rhport, char *host, unsigned long host_len,
 	return 0;
 }
 
-#define OPEN_HC_MODE_FIRST	0
-#define OPEN_HC_MODE_REOPEN	1
-
 static int open_hc_device(int mode)
 {
-	if (mode == OPEN_HC_MODE_REOPEN)
-		udev_device_unref(vhci_driver->hc_device);
+	if (vhci_hc_device && mode == OPEN_MODE_REOPEN)
+		udev_device_unref(vhci_hc_device);
 
-	vhci_driver->hc_device =
+	vhci_hc_device =
 		udev_device_new_from_subsystem_sysname(udev_context,
 						       USBIP_VHCI_BUS_TYPE,
 						       USBIP_VHCI_DRV_NAME);
-	if (!vhci_driver->hc_device) {
+	if (!vhci_hc_device) {
 		err("udev_device_new_from_subsystem_sysname failed");
 		return -1;
 	}
 	return 0;
 }
 
+static void close_hc_device(void)
+{
+	udev_device_unref(vhci_hc_device);
+	vhci_hc_device = NULL;
+}
+
 /* ---------------------------------------------------------------------- */
 
 int usbip_vhci_driver_open(void)
@@ -244,97 +259,88 @@ int usbip_vhci_driver_open(void)
 	udev_context = udev_new();
 	if (!udev_context) {
 		err("udev_new failed");
-		return -1;
+		goto err_out;
 	}
 
-	vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver));
+	if (open_hc_device(OPEN_MODE_FIRST))
+		goto err_unref_udev;
 
-	if (open_hc_device(OPEN_HC_MODE_FIRST))
-		goto err_free_driver;
-
-	vhci_driver->nports = get_nports();
-
-	dbg("available ports: %d", vhci_driver->nports);
-
-	if (refresh_imported_device_list())
-		goto err_unref_device;
+	if (get_nports() || vhci_nports <= 0) {
+		err("failed to get nports");
+		goto err_close_hc;
+	}
+	dbg("available ports: %d", vhci_nports);
 
 	return 0;
 
-err_unref_device:
-	udev_device_unref(vhci_driver->hc_device);
-err_free_driver:
-	if (vhci_driver)
-		free(vhci_driver);
-
-	vhci_driver = NULL;
-
+err_close_hc:
+	close_hc_device();
+err_unref_udev:
 	udev_unref(udev_context);
-
+	udev_context = NULL;
+err_out:
 	return -1;
 }
 
-
 void usbip_vhci_driver_close(void)
 {
-	if (!vhci_driver)
-		return;
-
-	udev_device_unref(vhci_driver->hc_device);
-
-	free(vhci_driver);
-
-	vhci_driver = NULL;
+	close_hc_device();
 
-	udev_unref(udev_context);
+	if (udev_context)
+		udev_unref(udev_context);
 }
 
-
-int usbip_vhci_refresh_device_list(void)
+int usbip_vhci_get_free_port(void)
 {
-	if (open_hc_device(OPEN_HC_MODE_REOPEN))
-		goto err;
-	if (refresh_imported_device_list())
-		goto err;
-
-	return 0;
-err:
-	dbg("failed to refresh device list");
-	return -1;
-}
+	struct status_context context;
+	struct usbip_vhci_device vdev;
 
+	if (open_status(&context, OPEN_MODE_FIRST))
+		return -1;
 
-int usbip_vhci_get_free_port(void)
-{
-	for (int i = 0; i < vhci_driver->nports; i++) {
-		if (vhci_driver->idev[i].status == VDEV_ST_NULL)
-			return i;
+	while (!parse_status_line(&context, &vdev)) {
+		if (vdev.status == VDEV_ST_NULL) {
+			dbg("found free port %d", vdev.port);
+			close_status(&context);
+			return vdev.port;
+		}
 	}
-
+	close_status(&context);
 	return -1;
 }
 
-struct usbip_imported_device *usbip_vhci_find_device(char *host, char *busid)
+int usbip_vhci_find_device(const char *host, const char *busid)
 {
 	int ret;
 	char rhost[NI_MAXHOST] = "unknown host";
 	char rserv[NI_MAXSERV] = "unknown port";
 	char rbusid[SYSFS_BUS_ID_SIZE];
+	struct status_context context;
+	struct usbip_vhci_device vdev;
+
+	if (open_status(&context, OPEN_MODE_FIRST))
+		return -1;
 
-	for (int i = 0; i < vhci_driver->nports; i++) {
-		ret = read_record(vhci_driver->idev[i].port, rhost, NI_MAXHOST,
-				rserv, NI_MAXSERV, rbusid);
+	while (!parse_status_line(&context, &vdev)) {
+		if (vdev.status != VDEV_ST_USED)
+			continue;
+
+		ret = read_record(vdev.port, rhost, NI_MAXHOST,
+				  rserv, NI_MAXSERV, rbusid);
 		if (!ret &&
 			!strncmp(host, rhost, NI_MAXHOST) &&
 			!strncmp(busid, rbusid, SYSFS_BUS_ID_SIZE)) {
-			return vhci_driver->idev + i;
+			close_status(&context);
+			return vdev.port;
 		}
 	}
-	return NULL;
+	close_status(&context);
+	return -1;
 }
 
-int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,
-		uint32_t speed) {
+static int usbip_vhci_attach_device2(int port, int sockfd, uint32_t devid,
+		uint32_t speed)
+{
 	char buff[200]; /* what size should be ? */
 	char attach_attr_path[SYSFS_PATH_MAX];
 	char attr_attach[] = "attach";
@@ -345,7 +351,7 @@ int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,
 			port, sockfd, devid, speed);
 	dbg("writing: %s", buff);
 
-	path = udev_device_get_syspath(vhci_driver->hc_device);
+	path = udev_device_get_syspath(vhci_hc_device);
 	snprintf(attach_attr_path, sizeof(attach_attr_path), "%s/%s",
 		 path, attr_attach);
 	dbg("attach attribute path: %s", attach_attr_path);
@@ -367,7 +373,7 @@ static unsigned long get_devid(uint8_t busnum, uint8_t devnum)
 }
 
 /* will be removed */
-int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum,
+int usbip_vhci_attach_device(int port, int sockfd, uint8_t busnum,
 		uint8_t devnum, uint32_t speed)
 {
 	int devid = get_devid(busnum, devnum);
@@ -375,7 +381,7 @@ int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum,
 	return usbip_vhci_attach_device2(port, sockfd, devid, speed);
 }
 
-int usbip_vhci_detach_device(uint8_t port)
+int usbip_vhci_detach_device(int port)
 {
 	char detach_attr_path[SYSFS_PATH_MAX];
 	char attr_detach[] = "detach";
@@ -386,7 +392,7 @@ int usbip_vhci_detach_device(uint8_t port)
 	snprintf(buff, sizeof(buff), "%u", port);
 	dbg("writing: %s", buff);
 
-	path = udev_device_get_syspath(vhci_driver->hc_device);
+	path = udev_device_get_syspath(vhci_hc_device);
 	snprintf(detach_attr_path, sizeof(detach_attr_path), "%s/%s",
 		 path, attr_detach);
 	dbg("detach attribute path: %s", detach_attr_path);
@@ -402,7 +408,7 @@ int usbip_vhci_detach_device(uint8_t port)
 	return 0;
 }
 
-int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev)
+static int usbip_vhci_imported_device_dump(struct usbip_vhci_device *vdev)
 {
 	char product_name[100];
 	char host[NI_MAXHOST] = "unknown host";
@@ -411,42 +417,68 @@ int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev)
 	int ret;
 	int read_record_error = 0;
 
-	if (idev->status == VDEV_ST_NULL || idev->status == VDEV_ST_NOTASSIGNED)
+	if (vdev->status == VDEV_ST_NULL || vdev->status == VDEV_ST_NOTASSIGNED)
 		return 0;
 
-	ret = read_record(idev->port, host, sizeof(host), serv, sizeof(serv),
+	ret = read_record(vdev->port, host, sizeof(host), serv, sizeof(serv),
 			  remote_busid);
 	if (ret) {
 		err("read_record");
 		read_record_error = 1;
 	}
 
-	printf("Port %02d: <%s> at %s\n", idev->port,
-	       usbip_status_string(idev->status),
-	       usbip_speed_string(idev->udev.speed));
+	printf("Port %02d: <%s> at %s\n", vdev->port,
+	       usbip_status_string(vdev->status),
+	       usbip_speed_string(vdev->udev.speed));
 
 	usbip_names_get_product(product_name, sizeof(product_name),
-				idev->udev.idVendor, idev->udev.idProduct);
+				vdev->udev.idVendor, vdev->udev.idProduct);
 
 	printf("       %s\n",  product_name);
 
 	if (!read_record_error) {
-		printf("%10s -> usbip://%s:%s/%s\n", idev->udev.busid,
+		printf("%10s -> usbip://%s:%s/%s\n", vdev->udev.busid,
 		       host, serv, remote_busid);
 		printf("%10s -> remote bus/dev %03d/%03d\n", " ",
-		       idev->busnum, idev->devnum);
+		       vdev->busnum, vdev->devnum);
 	} else {
 		printf("%10s -> unknown host, remote port and remote busid\n",
-		       idev->udev.busid);
+		       vdev->udev.busid);
 		printf("%10s -> remote bus/dev %03d/%03d\n", " ",
-		       idev->busnum, idev->devnum);
+		       vdev->busnum, vdev->devnum);
 	}
 
 	return 0;
 }
 
+int usbip_vhci_imported_devices_dump(void)
+{
+	struct status_context context;
+	struct usbip_vhci_device vdev;
+
+	if (open_status(&context, OPEN_MODE_FIRST))
+		goto err_out;
+
+	while (!parse_status_line(&context, &vdev)) {
+		if (vdev.status == VDEV_ST_NULL ||
+		    vdev.status == VDEV_ST_NOTASSIGNED)
+			continue;
+
+		if (usbip_vhci_imported_device_dump(&vdev))
+			goto err_close;
+	}
+	close_status(&context);
+
+	return 0;
+err_close:
+	close_status(&context);
+err_out:
+	return -1;
+}
+
 #define MAX_BUFF 100
-int usbip_vhci_create_record(char *host, char *port, char *busid, int rhport)
+int usbip_vhci_create_record(const char *host, const char *port_s,
+			     const char *busid, int port)
 {
 	int fd;
 	char path[PATH_MAX+1];
@@ -468,14 +500,14 @@ int usbip_vhci_create_record(char *host, char *port, char *busid, int rhport)
 			return -1;
 	}
 
-	snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
+	snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", port);
 
 	fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0700);
 	if (fd < 0)
 		return -1;
 
 	snprintf(buff, MAX_BUFF, "%s %s %s\n",
-			host, port, busid);
+			host, port_s, busid);
 
 	ret = write(fd, buff, strlen(buff));
 	if (ret != (ssize_t) strlen(buff)) {
@@ -488,11 +520,11 @@ int usbip_vhci_create_record(char *host, char *port, char *busid, int rhport)
 	return 0;
 }
 
-int usbip_vhci_delete_record(int rhport)
+int usbip_vhci_delete_record(int port)
 {
 	char path[PATH_MAX+1];
 
-	snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
+	snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", port);
 
 	remove(path);
 	rmdir(VHCI_STATE_PATH);
diff --git a/tools/usb/usbip/libsrc/vhci_driver.h b/tools/usb/usbip/libsrc/vhci_driver.h
index acb427d..037b51f 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.h
+++ b/tools/usb/usbip/libsrc/vhci_driver.h
@@ -12,53 +12,22 @@
 #include "usbip_common.h"
 
 #define USBIP_VHCI_BUS_TYPE "platform"
-#define MAXNPORT 128
-
-struct usbip_imported_device {
-	uint8_t port;
-	uint32_t status;
-
-	uint32_t devid;
-
-	uint8_t busnum;
-	uint8_t devnum;
-
-	/* usbip_class_device list */
-	struct usbip_usb_device udev;
-};
-
-struct usbip_vhci_driver {
-
-	/* /sys/devices/platform/vhci_hcd */
-	struct udev_device *hc_device;
-
-	int nports;
-	struct usbip_imported_device idev[MAXNPORT];
-};
-
-
-extern struct usbip_vhci_driver *vhci_driver;
 
 int usbip_vhci_driver_open(void);
 void usbip_vhci_driver_close(void);
 
-int  usbip_vhci_refresh_device_list(void);
-
-
 int usbip_vhci_get_free_port(void);
-struct usbip_imported_device *usbip_vhci_find_device(char *host, char *busid);
-int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,
-		uint32_t speed);
+int usbip_vhci_find_device(const char *host, const char *busid);
 
 /* will be removed */
-int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum,
-		uint8_t devnum, uint32_t speed);
-
-int usbip_vhci_detach_device(uint8_t port);
+int usbip_vhci_attach_device(int port, int sockfd, uint8_t busnum,
+			     uint8_t devnum, uint32_t speed);
+int usbip_vhci_detach_device(int port);
 
-int usbip_vhci_create_record(char *host, char *port, char *busid, int rhport);
+int usbip_vhci_create_record(const char *host, const char *port,
+			     const char *busid, int rhport);
 int usbip_vhci_delete_record(int rhport);
 
-int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev);
+int usbip_vhci_imported_devices_dump(void);
 
 #endif /* __VHCI_DRIVER_H */
diff --git a/tools/usb/usbip/src/usbip_attach.c b/tools/usb/usbip/src/usbip_attach.c
index ddef875..d7fd907 100644
--- a/tools/usb/usbip/src/usbip_attach.c
+++ b/tools/usb/usbip/src/usbip_attach.c
@@ -134,7 +134,7 @@ static int attach_device(char *host, char *busid)
 {
 	int sockfd;
 	int rc;
-	int rhport;
+	int port_nr;
 
 	sockfd = usbip_net_tcp_connect(host, usbip_port_string);
 	if (sockfd < 0) {
@@ -142,8 +142,8 @@ static int attach_device(char *host, char *busid)
 		return -1;
 	}
 
-	rhport = query_import_device(sockfd, busid);
-	if (rhport < 0) {
+	port_nr = query_import_device(sockfd, busid);
+	if (port_nr < 0) {
 		err("query");
 		close(sockfd);
 		return -1;
@@ -151,7 +151,7 @@ static int attach_device(char *host, char *busid)
 
 	close(sockfd);
 
-	rc = usbip_vhci_create_record(host, usbip_port_string, busid, rhport);
+	rc = usbip_vhci_create_record(host, usbip_port_string, busid, port_nr);
 	if (rc < 0) {
 		err("record connection");
 		return -1;
diff --git a/tools/usb/usbip/src/usbip_port.c b/tools/usb/usbip/src/usbip_port.c
index 7bd74fb..77cac68 100644
--- a/tools/usb/usbip/src/usbip_port.c
+++ b/tools/usb/usbip/src/usbip_port.c
@@ -18,8 +18,6 @@
 
 static int list_imported_devices(void)
 {
-	int i;
-	struct usbip_imported_device *idev;
 	int ret;
 
 	if (usbip_names_init(USBIDS_FILE))
@@ -27,18 +25,17 @@ static int list_imported_devices(void)
 
 	ret = usbip_vhci_driver_open();
 	if (ret < 0) {
-		err("open vhci_driver");
+		err("open vhci driver");
 		goto err_names_free;
 	}
 
 	printf("Imported USB devices\n");
 	printf("====================\n");
 
-	for (i = 0; i < vhci_driver->nports; i++) {
-		idev = &vhci_driver->idev[i];
-
-		if (usbip_vhci_imported_device_dump(idev) < 0)
-			goto err_driver_close;
+	ret = usbip_vhci_imported_devices_dump();
+	if (ret < 0) {
+		err("dump vhci devices");
+		goto err_driver_close;
 	}
 
 	usbip_vhci_driver_close();
diff --git a/tools/usb/usbip/src/usbipd_app.c b/tools/usb/usbip/src/usbipd_app.c
index 41f42f1..f7e249c 100644
--- a/tools/usb/usbip/src/usbipd_app.c
+++ b/tools/usb/usbip/src/usbipd_app.c
@@ -82,7 +82,7 @@ static int recv_request_export(int sockfd, char *host, char *port)
 {
 	struct op_export_request req;
 	struct op_export_reply reply;
-	int rhport = 0;
+	int port_nr = 0;
 	int error = 0;
 	int rc;
 
@@ -96,8 +96,8 @@ static int recv_request_export(int sockfd, char *host, char *port)
 	}
 	PACK_OP_EXPORT_REQUEST(0, &req);
 
-	rhport = import_device(sockfd, &req.udev);
-	if (rhport < 0) {
+	port_nr = import_device(sockfd, &req.udev);
+	if (port_nr < 0) {
 		dbg("export request busid %s: failed", req.udev.busid);
 		error = 1;
 	}
@@ -122,41 +122,42 @@ static int recv_request_export(int sockfd, char *host, char *port)
 		return -1;
 	}
 
-	rc = usbip_vhci_create_record(host, port, req.udev.busid, rhport);
-	if (rc < 0) {
-		err("record connection");
-		return -1;
+	if (!error) {
+		rc = usbip_vhci_create_record(host, port, req.udev.busid,
+					      port_nr);
+		if (rc < 0) {
+			err("record connection");
+			return -1;
+		}
 	}
-
-	dbg("export request busid %s: complete", req.udev.busid);
+	dbg("export request busid %s: complete %d", req.udev.busid, error);
 
 	return 0;
 }
 
 static int unimport_device(char *host, struct usbip_usb_device *udev)
 {
-	int rc;
-	struct usbip_imported_device *idev;
+	int port, rc;
 
-	idev = usbip_vhci_find_device(host, udev->busid);
-	if (idev == NULL) {
+	port = usbip_vhci_find_device(host, udev->busid);
+	if (port < 0) {
 		err("no imported port %s %s", host, udev->busid);
 		return -1;
 	}
 
-	rc = usbip_vhci_detach_device(idev->port);
+	rc = usbip_vhci_detach_device(port);
 	if (rc < 0) {
-		err("no imported port %d %s %s", idev->port, host, udev->busid);
+		err("no imported port %d %s %s", port, host, udev->busid);
 		return -1;
 	}
-	return idev->port;
+	return port;
 }
 
 static int recv_request_unexport(int sockfd, char *host)
 {
 	struct op_unexport_request req;
 	struct op_unexport_reply reply;
-	int rhport = 0;
+	int port_nr = 0;
 	int error = 0;
 	int rc;
 
@@ -170,8 +171,8 @@ static int recv_request_unexport(int sockfd, char *host)
 	}
 	PACK_OP_UNEXPORT_REQUEST(0, &req);
 
-	rhport = unimport_device(host, &req.udev);
-	if (rhport < 0)
+	port_nr = unimport_device(host, &req.udev);
+	if (port_nr < 0)
 		error = 1;
 
 	rc = usbip_net_send_op_common(sockfd, OP_REP_UNEXPORT,
@@ -196,7 +197,8 @@ static int recv_request_unexport(int sockfd, char *host)
 		return -1;
 	}
 
-	usbip_vhci_delete_record(rhport);
+	if (!error)
+		usbip_vhci_delete_record(port_nr);
 
 	dbg("unexport request busid %s: complete", req.udev.busid);
 
@@ -214,12 +216,6 @@ int usbip_recv_pdu(int connfd, char *host, char *port)
 		return -1;
 	}
 
-	ret = usbip_vhci_refresh_device_list();
-	if (ret < 0) {
-		dbg("could not refresh device list: %d", ret);
-		return -1;
-	}
-
 	info("received request: %#0x(%d)", code, connfd);
 	switch (code) {
 	case OP_REQ_EXPORT:
-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux