Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Michael Grzeschik and committed by
Greg Kroah-Hartman
de19ca6f dbafc289

+20 -15
+19 -13
tools/usb/usbip/libsrc/vhci_driver.c
··· 135 135 return 0; 136 136 } 137 137 138 - static int get_nports(void) 138 + static int get_nports(struct udev_device *hc_device) 139 139 { 140 140 const char *attr_nports; 141 141 142 - attr_nports = udev_device_get_sysattr_value(vhci_driver->hc_device, "nports"); 142 + attr_nports = udev_device_get_sysattr_value(hc_device, "nports"); 143 143 if (!attr_nports) { 144 144 err("udev_device_get_sysattr_value nports failed"); 145 145 return -1; ··· 242 242 243 243 int usbip_vhci_driver_open(void) 244 244 { 245 + int nports; 246 + struct udev_device *hc_device; 247 + 245 248 udev_context = udev_new(); 246 249 if (!udev_context) { 247 250 err("udev_new failed"); 248 251 return -1; 249 252 } 250 253 251 - vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver)); 252 - 253 254 /* will be freed in usbip_driver_close() */ 254 - vhci_driver->hc_device = 255 + hc_device = 255 256 udev_device_new_from_subsystem_sysname(udev_context, 256 257 USBIP_VHCI_BUS_TYPE, 257 258 USBIP_VHCI_DEVICE_NAME); 258 - if (!vhci_driver->hc_device) { 259 + if (!hc_device) { 259 260 err("udev_device_new_from_subsystem_sysname failed"); 260 261 goto err; 261 262 } 262 263 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) { 267 266 err("no available ports"); 268 267 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"); 271 275 goto err; 272 276 } 273 277 278 + vhci_driver->nports = nports; 279 + vhci_driver->hc_device = hc_device; 274 280 vhci_driver->ncontrollers = get_ncontrollers(); 275 281 dbg("available controllers: %d", vhci_driver->ncontrollers); 276 282 ··· 291 285 return 0; 292 286 293 287 err: 294 - udev_device_unref(vhci_driver->hc_device); 288 + udev_device_unref(hc_device); 295 289 296 290 if (vhci_driver) 297 291 free(vhci_driver);
+1 -2
tools/usb/usbip/libsrc/vhci_driver.h
··· 13 13 14 14 #define USBIP_VHCI_BUS_TYPE "platform" 15 15 #define USBIP_VHCI_DEVICE_NAME "vhci_hcd.0" 16 - #define MAXNPORT 128 17 16 18 17 enum hub_speed { 19 18 HUB_SPEED_HIGH = 0, ··· 40 41 41 42 int ncontrollers; 42 43 int nports; 43 - struct usbip_imported_device idev[MAXNPORT]; 44 + struct usbip_imported_device idev[]; 44 45 }; 45 46 46 47