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

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

 



On 12/26/2016 12:18 AM, Nobuo Iwata wrote:
> Modification to the userspace tools including usbip/libsrc and 
> usbip/src.

Modification to do what? Could you please write a concise change
log describing what this patch does and why? Please note that the
information in the cover-letter doesn't go int this commit.

> 
> 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 | 396 ++++++++++++++-------------
>  tools/usb/usbip/libsrc/vhci_driver.h |  45 +--
>  tools/usb/usbip/src/usbip_attach.c   |  12 +-
>  tools/usb/usbip/src/usbip_port.c     |  13 +-
>  tools/usb/usbip/src/usbipd_app.c     |  31 +--
>  5 files changed, 242 insertions(+), 255 deletions(-)
> 
> diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsrc/vhci_driver.c
> index 8b94ab5..844187d 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;
>  }
>  
>  static int __read_record(int rhport, char *buffer, size_t buffer_len)
> @@ -164,11 +176,11 @@ static int __read_record(int rhport, char *buffer, size_t buffer_len)
>  
>  	file = fopen(path, "r");
>  	if (!file) {
> -		err("fopen");
> +		err("fopen %s", path);
>  		return -1;
>  	}
>  	if (fgets(buffer, buffer_len, file) == NULL) {
> -		err("fgets");
> +		err("fgets %s", path);
>  		fclose(file);
>  		return -1;
>  	}
> @@ -186,22 +198,22 @@ static int __read_record(int rhport, char *buffer, size_t buffer_len)
>   * 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, int host_len,
> -		char *port, int port_len, char *busid, int busid_len)
> +static int read_record(int port, char *host, int host_len,
> +		char *serv, int serv_len, char *busid, int busid_len)
>  {
>  	int part;
>  	char *buffer, *start, *end;
>  	char delim[] = {' ', ' ', '\n'};
> -	char * const str[] = {host, port, busid};
> -	int max_len[] = {host_len, port_len, busid_len};
> +	char * const str[] = {host, serv, busid};
> +	int max_len[] = {host_len, serv_len, busid_len};
>  	int str_len;
> -	size_t buffer_len = host_len + port_len + busid_len + 4;
> +	size_t buffer_len = host_len + serv_len + busid_len + 4;
>  
> -	buffer = malloc(buffer_len);
> +	buffer = (char *)malloc(buffer_len);
>  	if (!buffer)
>  		goto err_out;
>  
> -	if (__read_record(rhport, buffer, buffer_len))
> +	if (__read_record(port, buffer, buffer_len))
>  		goto err_free_buffer;
>  
>  	/* validate the length of each of the 3 parts */
> @@ -229,25 +241,28 @@ static int read_record(int rhport, char *host, int host_len,
>  	return -1;
>  }
>  
> -#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)
> @@ -255,103 +270,90 @@ 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_HC_MODE_FIRST))
> -		goto err_free_driver;
> +	if (open_hc_device(OPEN_MODE_FIRST))
> +		goto err_unref_udev;
>  
> -	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);
> +	close_hc_device();
>  
> -	vhci_driver = NULL;
> -
> -	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)
>  {
> -	struct usbip_imported_device *idev;
>  	int ret;
>  	char rhost[NI_MAXHOST];
>  	char rbusid[SYSFS_BUS_ID_SIZE];
> +	struct status_context context;
> +	struct usbip_vhci_device vdev;
>  
> -	for (int i = 0; i < vhci_driver->nports; i++) {
> -		idev = &vhci_driver->idev[i];
> +	if (open_status(&context, OPEN_MODE_FIRST))
> +		return -1;
>  
> -		if (idev->status == VDEV_ST_NULL ||
> -			idev->status == VDEV_ST_NOTASSIGNED)
> +	while (!parse_status_line(&context, &vdev)) {
> +		if (vdev.status == VDEV_ST_NULL ||
> +			vdev.status == VDEV_ST_NOTASSIGNED)
>  			continue;
>  
> -		ret = read_record(idev->port, rhost, sizeof(rhost), NULL, 0,
> +		ret = read_record(vdev.port, rhost, sizeof(rhost), NULL, 0,
>  				  rbusid, sizeof(rbusid));
>  		if (!ret &&
>  			!strncmp(host, rhost, sizeof(rhost)) &&
>  			!strncmp(busid, rbusid, sizeof(rbusid))) {
> -			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) {
> +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";
> @@ -362,7 +364,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);
> @@ -384,7 +386,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);
> @@ -392,7 +394,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";
> @@ -403,7 +405,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);
> @@ -419,7 +421,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";
> @@ -428,42 +430,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, sizeof(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 *serv,
> +			     const char *busid, int port)
>  {
>  	int fd;
>  	char path[PATH_MAX+1];
> @@ -485,14 +513,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, serv, busid);
>  
>  	ret = write(fd, buff, strlen(buff));
>  	if (ret != (ssize_t) strlen(buff)) {
> @@ -505,11 +533,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 a812851..5981bf3 100644
> --- a/tools/usb/usbip/libsrc/vhci_driver.h
> +++ b/tools/usb/usbip/libsrc/vhci_driver.h
> @@ -11,53 +11,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 17a84ea..05e7666 100644
> --- a/tools/usb/usbip/src/usbip_attach.c
> +++ b/tools/usb/usbip/src/usbip_attach.c
> @@ -51,7 +51,7 @@ static int import_device(int sockfd, struct usbip_usb_device *udev,
>  			 char *host, char *port, char *busid)
>  {
>  	int rc;
> -	int rhport;
> +	int port_nr;
>  
>  	rc = usbip_vhci_driver_open();
>  	if (rc < 0) {
> @@ -60,13 +60,13 @@ static int import_device(int sockfd, struct usbip_usb_device *udev,
>  	}
>  
>  	do {
> -		rhport = usbip_vhci_get_free_port();
> -		if (rhport < 0) {
> +		port_nr = usbip_vhci_get_free_port();
> +		if (port_nr < 0) {
>  			err("no free port");
>  			goto err_driver_close;
>  		}
>  
> -		rc = usbip_vhci_attach_device(rhport, sockfd, udev->busnum,
> +		rc = usbip_vhci_attach_device(port_nr, sockfd, udev->busnum,
>  					      udev->devnum, udev->speed);
>  		if (rc < 0 && errno != EBUSY) {
>  			err("import device");
> @@ -75,7 +75,7 @@ static int import_device(int sockfd, struct usbip_usb_device *udev,
>  		}
>  	} while (rc < 0);
>  
> -	rc = usbip_vhci_create_record(host, port, busid, rhport);
> +	rc = usbip_vhci_create_record(host, port, busid, port_nr);
>  	if (rc < 0) {
>  		err("record connection");
>  		goto err_detach_device;
> @@ -86,7 +86,7 @@ static int import_device(int sockfd, struct usbip_usb_device *udev,
>  	return 0;
>  
>  err_detach_device:
> -	usbip_vhci_detach_device(rhport);
> +	usbip_vhci_detach_device(port_nr);
>  err_driver_close:
>  	usbip_vhci_driver_close();
>  err_out:
> 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 ce7438e..c5c0b77 100644
> --- a/tools/usb/usbip/src/usbipd_app.c
> +++ b/tools/usb/usbip/src/usbipd_app.c
> @@ -52,15 +52,9 @@ void driver_close(void)
>  	usbip_vhci_driver_close();
>  }
>  
> -static int refresh_device_list(void)
> -{
> -	return usbip_vhci_refresh_device_list();
> -}
> -
>  struct usbipd_driver_ops usbipd_driver_ops = {
>  	.open = driver_open,
>  	.close = driver_close,
> -	.refresh_device_list = refresh_device_list,
>  };
>  
>  static int import_device(int sockfd, struct usbip_usb_device *udev,
> @@ -68,20 +62,20 @@ static int import_device(int sockfd, struct usbip_usb_device *udev,
>  			 uint32_t *status)
>  {
>  	int rc;
> -	int rhport;
> +	int port_nr;
>  
>  	dbg("Sockfd:%d", sockfd);
>  	dump_usb_device(udev);
>  
>  	do {
> -		rhport = usbip_vhci_get_free_port();
> -		if (rhport < 0) {
> +		port_nr = usbip_vhci_get_free_port();
> +		if (port_nr < 0) {
>  			err("no free port");
>  			*status = ST_NO_FREE_PORT;
>  			goto err_out;
>  		}
>  
> -		rc = usbip_vhci_attach_device(rhport, sockfd, udev->busnum,
> +		rc = usbip_vhci_attach_device(port_nr, sockfd, udev->busnum,
>  					      udev->devnum, udev->speed);
>  		if (rc < 0 && errno != EBUSY) {
>  			err("import device");
> @@ -90,7 +84,7 @@ static int import_device(int sockfd, struct usbip_usb_device *udev,
>  		}
>  	} while (rc < 0);
>  
> -	rc = usbip_vhci_create_record(host, port, busid, rhport);
> +	rc = usbip_vhci_create_record(host, port, busid, port_nr);
>  	if (rc < 0) {
>  		err("record connection");
>  		*status = ST_NA;
> @@ -100,7 +94,7 @@ static int import_device(int sockfd, struct usbip_usb_device *udev,
>  	return 0;
>  
>  err_detach_device:
> -	usbip_vhci_detach_device(rhport);
> +	usbip_vhci_detach_device(port_nr);
>  err_out:
>  	return -1;
>  }
> @@ -139,24 +133,23 @@ static int recv_request_export(int sockfd, char *host, char *port)
>  static int unimport_device(char *host, struct usbip_usb_device *udev,
>  			   uint32_t *status)
>  {
> -	int rc;
> -	struct usbip_imported_device *idev;
> +	int port_nr, rc;
>  
> -	idev = usbip_vhci_find_device(host, udev->busid);
> -	if (idev == NULL) {
> +	port_nr = usbip_vhci_find_device(host, udev->busid);
> +	if (port_nr < 0) {
>  		err("no imported port %s %s", host, udev->busid);
>  		*status = ST_DEVICE_NOT_FOUND;
>  		return -1;
>  	}
>  
> -	rc = usbip_vhci_detach_device(idev->port);
> +	rc = usbip_vhci_detach_device(port_nr);
>  	if (rc < 0) {
> -		err("no imported port %d %s %s", idev->port, host, udev->busid);
> +		err("no imported port %d %s %s", port_nr, host, udev->busid);
>  		*status = ST_NA;
>  		return -1;
>  	}
>  
> -	usbip_vhci_delete_record(idev->port);
> +	usbip_vhci_delete_record(port_nr);
>  
>  	return 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