From patchwork Tue May 22 17:04:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Grzeschik X-Patchwork-Id: 10419205 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4C443600CC for ; Tue, 22 May 2018 17:04:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3C8B028FC9 for ; Tue, 22 May 2018 17:04:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 317DC28FCA; Tue, 22 May 2018 17:04:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 987E028FCC for ; Tue, 22 May 2018 17:04:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751735AbeEVREn (ORCPT ); Tue, 22 May 2018 13:04:43 -0400 Received: from metis.ext.pengutronix.de ([85.220.165.71]:38911 "EHLO metis.ext.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751731AbeEVREm (ORCPT ); Tue, 22 May 2018 13:04:42 -0400 Received: from dude.hi.pengutronix.de ([2001:67c:670:100:1d::7]) by metis.ext.pengutronix.de with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1fLAiG-0004Bx-Vq; Tue, 22 May 2018 19:04:40 +0200 Received: from mgr by dude.hi.pengutronix.de with local (Exim 4.91) (envelope-from ) id 1fLAiG-0002jN-ER; Tue, 22 May 2018 19:04:40 +0200 From: Michael Grzeschik 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 v2] usbip: dynamically allocate idev by nports found in sysfs Date: Tue, 22 May 2018 19:04:07 +0200 Message-Id: <20180522170407.10102-1-m.grzeschik@pengutronix.de> X-Mailer: git-send-email 2.17.0 In-Reply-To: <05a0fd16-cd54-1bb8-7566-0d88e1bd73cf@kernel.org> References: <05a0fd16-cd54-1bb8-7566-0d88e1bd73cf@kernel.org> X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::7 X-SA-Exim-Mail-From: mgr@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-usb@vger.kernel.org Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- v1 -> v2: - reworked memory allocation into one calloc call - added error path on allocation failure tools/usb/usbip/libsrc/vhci_driver.c | 14 +++++++++----- tools/usb/usbip/libsrc/vhci_driver.h | 3 +-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsrc/vhci_driver.c index c9c81614a66ad..6e2a9edfd1f0d 100644 --- a/tools/usb/usbip/libsrc/vhci_driver.c +++ b/tools/usb/usbip/libsrc/vhci_driver.c @@ -242,13 +242,20 @@ static int read_record(int rhport, char *host, unsigned long host_len, int usbip_vhci_driver_open(void) { + int nports = get_nports(); + 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,15 +267,12 @@ 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(); 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[]; };