Skip to content

Commit de19ca6

Browse files
Michael Grzeschikgregkh
authored andcommitted
usbip: dynamically allocate idev by nports found in sysfs
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> Acked-by: Shuah Khan (Samsung OSG) <shuah@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent dbafc28 commit de19ca6

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

‎tools/usb/usbip/libsrc/vhci_driver.c‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ static int refresh_imported_device_list(void)
135135
return 0;
136136
}
137137

138-
static int get_nports(void)
138+
static int get_nports(struct udev_device *hc_device)
139139
{
140140
const char *attr_nports;
141141

142-
attr_nports = udev_device_get_sysattr_value(vhci_driver->hc_device, "nports");
142+
attr_nports = udev_device_get_sysattr_value(hc_device, "nports");
143143
if (!attr_nports) {
144144
err("udev_device_get_sysattr_value nports failed");
145145
return -1;
@@ -242,35 +242,41 @@ static int read_record(int rhport, char *host, unsigned long host_len,
242242

243243
int usbip_vhci_driver_open(void)
244244
{
245+
int nports;
246+
struct udev_device *hc_device;
247+
245248
udev_context = udev_new();
246249
if (!udev_context) {
247250
err("udev_new failed");
248251
return -1;
249252
}
250253

251-
vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver));
252-
253254
/* will be freed in usbip_driver_close() */
254-
vhci_driver->hc_device =
255+
hc_device =
255256
udev_device_new_from_subsystem_sysname(udev_context,
256257
USBIP_VHCI_BUS_TYPE,
257258
USBIP_VHCI_DEVICE_NAME);
258-
if (!vhci_driver->hc_device) {
259+
if (!hc_device) {
259260
err("udev_device_new_from_subsystem_sysname failed");
260261
goto err;
261262
}
262263

263-
vhci_driver->nports = get_nports();
264-
dbg("available ports: %d", vhci_driver->nports);
265-
266-
if (vhci_driver->nports <= 0) {
264+
nports = get_nports(hc_device);
265+
if (nports <= 0) {
267266
err("no available ports");
268267
goto err;
269-
} else if (vhci_driver->nports > MAXNPORT) {
270-
err("port number exceeds %d", MAXNPORT);
268+
}
269+
dbg("available ports: %d", nports);
270+
271+
vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver) +
272+
nports * sizeof(struct usbip_imported_device));
273+
if (!vhci_driver) {
274+
err("vhci_driver allocation failed");
271275
goto err;
272276
}
273277

278+
vhci_driver->nports = nports;
279+
vhci_driver->hc_device = hc_device;
274280
vhci_driver->ncontrollers = get_ncontrollers();
275281
dbg("available controllers: %d", vhci_driver->ncontrollers);
276282

@@ -285,7 +291,7 @@ int usbip_vhci_driver_open(void)
285291
return 0;
286292

287293
err:
288-
udev_device_unref(vhci_driver->hc_device);
294+
udev_device_unref(hc_device);
289295

290296
if (vhci_driver)
291297
free(vhci_driver);

‎tools/usb/usbip/libsrc/vhci_driver.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#define USBIP_VHCI_BUS_TYPE "platform"
1515
#define USBIP_VHCI_DEVICE_NAME "vhci_hcd.0"
16-
#define MAXNPORT 128
1716

1817
enum hub_speed {
1918
HUB_SPEED_HIGH = 0,
@@ -41,7 +40,7 @@ struct usbip_vhci_driver {
4140

4241
int ncontrollers;
4342
int nports;
44-
struct usbip_imported_device idev[MAXNPORT];
43+
struct usbip_imported_device idev[];
4544
};
4645

4746

0 commit comments

Comments
 (0)