mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Michael Grzeschik <m.grzeschik@pengutronix.de>
To: shuah@kernel.org, linux-usb@vger.kernel.org
Cc: valentina.manea.m@gmail.com, gregkh@linuxfoundation.org,
	linux-kernel@vger.kernel.org, kernel@pengutronix.de
Subject: [PATCH v3] usbip: dynamically allocate idev by nports found in sysfs
Date: Wed, 23 May 2018 11:22:56 +0200	[thread overview]
Message-ID: <20180523092256.25922-1-m.grzeschik@pengutronix.de> (raw)
In-Reply-To: <c57fa7e9-d811-234b-fefc-1be998c5a8f1@kernel.org>

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>
---
v1 -> v2: - reworked memory allocation into one calloc call
          - added error path on allocation failure
v2 -> v3: - moved check for available nports to beginning of function

 tools/usb/usbip/libsrc/vhci_driver.c | 24 ++++++++++++++----------
 tools/usb/usbip/libsrc/vhci_driver.h |  3 +--
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsrc/vhci_driver.c
index c9c81614a66ad..c5db1be784bab 100644
--- a/tools/usb/usbip/libsrc/vhci_driver.c
+++ b/tools/usb/usbip/libsrc/vhci_driver.c
@@ -242,13 +242,25 @@ static int read_record(int rhport, char *host, unsigned long host_len,
 
 int usbip_vhci_driver_open(void)
 {
+	int nports = get_nports();
+
+	if (nports <= 0) {
+		err("no available ports");
+		return -1;
+	}
+
 	udev_context = udev_new();
 	if (!udev_context) {
 		err("udev_new failed");
 		return -1;
 	}
 
-	vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver));
+	vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver) +
+			nports * sizeof(struct usbip_imported_device));
+	if (!vhci_driver) {
+		err("vhci_driver allocation failed");
+		return -1;
+	}
 
 	/* will be freed in usbip_driver_close() */
 	vhci_driver->hc_device =
@@ -260,17 +272,9 @@ int usbip_vhci_driver_open(void)
 		goto err;
 	}
 
-	vhci_driver->nports = get_nports();
+	vhci_driver->nports = nports;
 	dbg("available ports: %d", vhci_driver->nports);
 
-	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->ncontrollers = get_ncontrollers();
 	dbg("available controllers: %d", vhci_driver->ncontrollers);
 
diff --git a/tools/usb/usbip/libsrc/vhci_driver.h b/tools/usb/usbip/libsrc/vhci_driver.h
index 418b404d51210..6c9aca2167051 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[];
 };
 
 
-- 
2.17.0

  reply	other threads:[~2018-05-23  9:22 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-18 14:39 [PATCH] " Michael Grzeschik
2018-05-22 16:06 ` Shuah Khan
2018-05-22 17:04   ` [PATCH v2] " Michael Grzeschik
2018-05-22 22:56     ` Shuah Khan
2018-05-23  9:22       ` Michael Grzeschik [this message]
2018-05-23 16:44         ` [PATCH v3] " Shuah Khan
2018-05-23 21:01           ` Michael Grzeschik
2018-05-24  7:34           ` Michael Grzeschik

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180523092256.25922-1-m.grzeschik@pengutronix.de \
    --to=m.grzeschik@pengutronix.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=kernel@pengutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=valentina.manea.m@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®