Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp601945imm; Wed, 23 May 2018 02:23:48 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpPpgiC4lGljtruMuFZMfgwIuFSTTbSt9NLsPnFGs4sTWHZFBpBnN7PxCX2RD8UAu+nG2hv X-Received: by 2002:a62:3085:: with SMTP id w127-v6mr2115599pfw.224.1527067428703; Wed, 23 May 2018 02:23:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527067428; cv=none; d=google.com; s=arc-20160816; b=DJADC4R/KDiGmV7Yi3N6O5KS2z+8MDJOokT+w4FHt+qNz/3cA8SDIn14LgzVSYHdWm oP2IDFN7EXgQChyTVK35MTqjksd0I1oscquTw1CO+Pj5LEutOFGedPbtJD3tDJEJ3jA3 xAGgACDiu/vtND/dPUWMWgvEnTGqTgF2E9kNdatDhrMvp4WGSTMdRp1rgLca9XBYrqpu o0/5o/q/roIEewY4JBWW/HtJS74ND8PAu2MSHfvKXoA4d8OBzQ/zltLtruU79mBrjZQM 1zGzqqPRcReALu200hE1o3wUZKOGuSvIp4b3rE6ZsyGZwQ7s3AfWMxyCmS/3vm4kCuTo fC3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=M8hShWgHUdJ00rnUBKzLhpbxuAMSQtcwt9waEzMBXMc=; b=qnSl9VwL9izqdVEcfY7z83dM2czAWWEZ/tV0/qSdHnGdpUPp3dPtfeM3MiI4M3S94u ZrQT4iW+bYTtHtRR+NJWa6B15L3DZlZ0jntvPZ6yfdFH2b55LWUUdWcnLQy8b/h3HE2U joOo1bRc9IO5uuN6ZQl1+4cHrqXtpdQPtbq9SJguVy6rqRhSr88/yKGMXmTmMp9kduxA B1As2eQF1XVTSIjmv2DbSji39CDglEe/3DVLkkPkX4OtfzIKw8DLtk6dhJHZUiUsjBZU 8JGepdPyAbY1oafo4eHfCQ6/7iXDswmveAG/tJfGZqSVAjZeEFF+THhoMZb0jBSGf/EN T1yA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w24-v6si17434821plq.254.2018.05.23.02.23.33; Wed, 23 May 2018 02:23:48 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932164AbeEWJXU (ORCPT + 99 others); Wed, 23 May 2018 05:23:20 -0400 Received: from metis.ext.pengutronix.de ([85.220.165.71]:51961 "EHLO metis.ext.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932111AbeEWJXQ (ORCPT ); Wed, 23 May 2018 05:23:16 -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 1fLPzE-0002TW-Ad; Wed, 23 May 2018 11:23:12 +0200 Received: from mgr by dude.hi.pengutronix.de with local (Exim 4.91) (envelope-from ) id 1fLPzD-0004o4-HO; Wed, 23 May 2018 11:23:11 +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 v3] usbip: dynamically allocate idev by nports found in sysfs Date: Wed, 23 May 2018 11:22:56 +0200 Message-Id: <20180523092256.25922-1-m.grzeschik@pengutronix.de> X-Mailer: git-send-email 2.17.0 In-Reply-To: References: 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-kernel@vger.kernel.org Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.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 --- 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