diff mbox

usbip: dynamically allocate idev by nports found in sysfs

Message ID 20180518143947.27963-1-m.grzeschik@pengutronix.de (mailing list archive)
State New, archived
Headers show

Commit Message

Michael Grzeschik May 18, 2018, 2:39 p.m. UTC
As the amount of available ports varies by the kernels build
configuration. To remove the limitation of the fixed 128 ports
we allocate the amount of idevs by using the number we get
from the kernel.

Signed-off-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
---
 tools/usb/usbip/libsrc/vhci_driver.c | 11 ++++++++---
 tools/usb/usbip/libsrc/vhci_driver.h |  3 +--
 2 files changed, 9 insertions(+), 5 deletions(-)

Comments

shuah May 22, 2018, 4:06 p.m. UTC | #1
Hi Michael,

Thanks for the patch. Couple of comments below:

On 05/18/2018 08:39 AM, Michael Grzeschik wrote:
> As the amount of available ports varies by the kernels build
> configuration. To remove the limitation of the fixed 128 ports
> we allocate the amount of idevs by using the number we get
> from the kernel.
> 
> Signed-off-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
> ---
>  tools/usb/usbip/libsrc/vhci_driver.c | 11 ++++++++---
>  tools/usb/usbip/libsrc/vhci_driver.h |  3 +--
>  2 files changed, 9 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsrc/vhci_driver.c
> index c9c81614a66ad..9a8acfc7697fa 100644
> --- a/tools/usb/usbip/libsrc/vhci_driver.c
> +++ b/tools/usb/usbip/libsrc/vhci_driver.c
> @@ -266,11 +266,11 @@ int usbip_vhci_driver_open(void)
>  	if (vhci_driver->nports <= 0) {
>  		err("no available ports");
>  		goto err;
> -	} else if (vhci_driver->nports > MAXNPORT) {
> -		err("port number exceeds %d", MAXNPORT);
> -		goto err;
>  	}
>  
> +	vhci_driver->idev = calloc(vhci_driver->nports,
> +			sizeof(struct usbip_imported_device));
> +

Missing check for memory allocation failure. Please add it.

>  	vhci_driver->ncontrollers = get_ncontrollers();
>  	dbg("available controllers: %d", vhci_driver->ncontrollers);
>  
> @@ -287,6 +287,9 @@ int usbip_vhci_driver_open(void)
>  err:
>  	udev_device_unref(vhci_driver->hc_device);
>  
> +	if (vhci_driver->idev)
> +		free(vhci_driver->idev);
> +
>  	if (vhci_driver)
>  		free(vhci_driver);
>  
> @@ -305,6 +308,8 @@ void usbip_vhci_driver_close(void)
>  
>  	udev_device_unref(vhci_driver->hc_device);
>  
> +	free(vhci_driver->idev);
> +
>  	free(vhci_driver);
>  
>  	vhci_driver = NULL;
> diff --git a/tools/usb/usbip/libsrc/vhci_driver.h b/tools/usb/usbip/libsrc/vhci_driver.h
> index 418b404d51210..67dbd1551e159 100644
> --- a/tools/usb/usbip/libsrc/vhci_driver.h
> +++ b/tools/usb/usbip/libsrc/vhci_driver.h
> @@ -13,7 +13,6 @@
>  
>  #define USBIP_VHCI_BUS_TYPE "platform"
>  #define USBIP_VHCI_DEVICE_NAME "vhci_hcd.0"
> -#define MAXNPORT 128
>  
>  enum hub_speed {
>  	HUB_SPEED_HIGH = 0,
> @@ -41,7 +40,7 @@ struct usbip_vhci_driver {
>  
>  	int ncontrollers;
>  	int nports;
> -	struct usbip_imported_device idev[MAXNPORT];
> +	struct usbip_imported_device *idev;
>  };
>  
>  
> 

Rest looks good.

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

Patch

diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsrc/vhci_driver.c
index c9c81614a66ad..9a8acfc7697fa 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -266,11 +266,11 @@  int usbip_vhci_driver_open(void)
 	if (vhci_driver->nports <= 0) {
 		err("no available ports");
 		goto err;
-	} else if (vhci_driver->nports > MAXNPORT) {
-		err("port number exceeds %d", MAXNPORT);
-		goto err;
 	}
 
+	vhci_driver->idev = calloc(vhci_driver->nports,
+			sizeof(struct usbip_imported_device));
+
 	vhci_driver->ncontrollers = get_ncontrollers();
 	dbg("available controllers: %d", vhci_driver->ncontrollers);
 
@@ -287,6 +287,9 @@  int usbip_vhci_driver_open(void)
 err:
 	udev_device_unref(vhci_driver->hc_device);
 
+	if (vhci_driver->idev)
+		free(vhci_driver->idev);
+
 	if (vhci_driver)
 		free(vhci_driver);
 
@@ -305,6 +308,8 @@  void usbip_vhci_driver_close(void)
 
 	udev_device_unref(vhci_driver->hc_device);
 
+	free(vhci_driver->idev);
+
 	free(vhci_driver);
 
 	vhci_driver = NULL;
diff --git a/tools/usb/usbip/libsrc/vhci_driver.h b/tools/usb/usbip/libsrc/vhci_driver.h
index 418b404d51210..67dbd1551e159 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.h
+++ b/tools/usb/usbip/libsrc/vhci_driver.h
@@ -13,7 +13,6 @@ 
 
 #define USBIP_VHCI_BUS_TYPE "platform"
 #define USBIP_VHCI_DEVICE_NAME "vhci_hcd.0"
-#define MAXNPORT 128
 
 enum hub_speed {
 	HUB_SPEED_HIGH = 0,
@@ -41,7 +40,7 @@  struct usbip_vhci_driver {
 
 	int ncontrollers;
 	int nports;
-	struct usbip_imported_device idev[MAXNPORT];
+	struct usbip_imported_device *idev;
 };