From patchwork Fri Aug 21 02:55:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Chen X-Patchwork-Id: 11727459 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D85FD722 for ; Fri, 21 Aug 2020 02:57:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B97FB207DE for ; Fri, 21 Aug 2020 02:57:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="b3rQdKKz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727045AbgHUC5V (ORCPT ); Thu, 20 Aug 2020 22:57:21 -0400 Received: from mail-eopbgr50073.outbound.protection.outlook.com ([40.107.5.73]:27655 "EHLO EUR03-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726840AbgHUC5T (ORCPT ); Thu, 20 Aug 2020 22:57:19 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ENN57ja4kOXUUAyXyoNVgwqec8CKrv2STpvEa154UPjruxr6ZyIkfCgxy81fwE3X/TAoQ42XiSCq4tTcEN8B7RKpsveMarN96lN3q2VCnj8X0fFvZmee9NLkZgJYWA7Orrlj9xe5G+rgyo2lb+I32SVzIzzJTPyZQyxi6AJWm2CoCVI7IM0aKzNsUtE8UGQKqYVLtTd40VG9rqdSt9KQxQeaeSgL9C0yW56+CvN9YBrE40LQfxM4ld+QDFvAWsp0gPOVrQj/W9LVjrQnv4U3b8i2AtPd7WapJdF8Cv2YNgJTouG6pphF1MQVIiyThY451n8x5WVgCUgv9CVq8UeAow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=zcJgp55CMnKk0PxezJBoVVKxO6IRcMCgPjdetmsCfoA=; b=JhRwUwwN6BQZ/HnnP8MHWZmWk4ZcIejqJTsVGRtWlGarfjWJwGk39VOW4UH2LM9Djt26QkwdsN+SsW3VezU+jXK7bMJRuATPjiHO7NSvnzhLbjbkD3FLGRmpGkVbhiYJTLDDvLdHZQWlB5Nx1aYv4p2N6acgTK+J67py6+rwS3JRu3gqXc7oGNS4dZNubg4R9E3W5gL+qAh5ZNSC53yezLu0iPdM7Shi6P/bSVo1V8Cm+qX4Ma3UQ+K3wHqZZCvH7hFUyg6+uATVuHp3gLQWLz167s7gMF07npPoXoUd4fuPrLA32HbH06/rF5kV2JU2XPugwUxf0APlSCqyzET3tA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=zcJgp55CMnKk0PxezJBoVVKxO6IRcMCgPjdetmsCfoA=; b=b3rQdKKzQAUPOsjFVfjuI4AJCqHA10778NOk/Ek2pj0urTfxCZ6JWItIVBnBv3SBaLcwvXl/f8KtCCqFvT2LIObFbHXfLzj08mvGfNqBxX2uBpNC+3aFpf4E4lSEXKrjj/Lleth7yI83IC9yUF+I1QCcotrntRFbrcseUnd5NTk= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=nxp.com; Received: from DB8PR04MB7162.eurprd04.prod.outlook.com (2603:10a6:10:12c::13) by DB7PR04MB5577.eurprd04.prod.outlook.com (2603:10a6:10:8a::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.26; Fri, 21 Aug 2020 02:57:12 +0000 Received: from DB8PR04MB7162.eurprd04.prod.outlook.com ([fe80::acee:9763:7898:84a2]) by DB8PR04MB7162.eurprd04.prod.outlook.com ([fe80::acee:9763:7898:84a2%9]) with mapi id 15.20.3283.027; Fri, 21 Aug 2020 02:57:12 +0000 From: Peter Chen To: balbi@kernel.org Cc: linux-usb@vger.kernel.org, linux-imx@nxp.com, gregkh@linuxfoundation.org, stern@rowland.harvard.edu, Anton Vasilyev , Evgeny Novikov , Benjamin Herrenschmidt , Peter Chen Subject: [PATCH v4 1/6] USB: UDC: Expand device model API interface Date: Fri, 21 Aug 2020 10:55:44 +0800 Message-Id: <20200821025549.4591-2-peter.chen@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200821025549.4591-1-peter.chen@nxp.com> References: <20200821025549.4591-1-peter.chen@nxp.com> X-ClientProxiedBy: SG2PR01CA0132.apcprd01.prod.exchangelabs.com (2603:1096:4:40::36) To DB8PR04MB7162.eurprd04.prod.outlook.com (2603:10a6:10:12c::13) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from b29397-desktop.ap.freescale.net (119.31.174.67) by SG2PR01CA0132.apcprd01.prod.exchangelabs.com (2603:1096:4:40::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.24 via Frontend Transport; Fri, 21 Aug 2020 02:57:09 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [119.31.174.67] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 7a043302-8a7d-4304-6530-08d8457de6c4 X-MS-TrafficTypeDiagnostic: DB7PR04MB5577: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3631; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aFSOWFHlAoU2sEONsnxCYWgyMuTlgVza3Jwq5CxG7XGVyEt8fWWctbpGVmgNAbeWWcX7ZglOlTw5u4ygJDR3HRqU1KytT3IIhxv+sLmYxVI/nnc0lPEVZIJgqDVkAO3qnPk6mX4TvfehV8MKLAHDdWqEywH+L6HN/b7LcR0C44lnRgS5Gw2LO6nS+UG9nmSUwwwmfthBgYpg+MEvgeINEXq5K8qvs20XoX42Urf0IuwquIyvvP2C+Gc2cjOOBmgcCNnNC0KjT/DybExHB64NAWjxLMe2yyR9KjADWDupU6igMt7R5whYdEp61T1rUyDaXmX5oCSc0UUEwYjxCVDRDA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DB8PR04MB7162.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(376002)(136003)(366004)(346002)(39860400002)(52116002)(66946007)(2906002)(8676002)(6486002)(4326008)(6506007)(478600001)(316002)(6666004)(44832011)(36756003)(54906003)(956004)(26005)(66476007)(66556008)(2616005)(8936002)(1076003)(6512007)(5660300002)(6916009)(186003)(83380400001)(86362001)(16526019);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: q3knvGC28wQbQbN0ndPZmxNYYkFKXyujPIkDLAZU548RgigcMjJ0IfexWiKna3FLTL5X5vU7d23m+QrV0Xp45uO+JC6n7Y/Oag9B41MUJRAR6LlWWBfr1OfCsoXwI+Ac0MGBEUWGA4tWapBFHMkDftUvU4VqcS2kmpzdnx1V3zJqiO3Teurq+u7UyoJUvDaZ/8vPwiK1ef5Qx8kemt4BW26GQRj5MpqR5BomFDedczYE/9ryUYCEaCbqgDuUQPAj15vDbGKvn6+fXZlLTw1Z85fQhiv7K2fAAnD2TzLa8xw+NA7es39936iKwqp2e/0bt5+tqQTImoCgympxlL7my7OeTRWUsChPd3fvFSk4+n+htKDmfXPvw3n6e7U6VAMJhb8Z0k13WsA0NiBa/5nK3h0qrcLD+dH6U4DHPWriR5uYVPNB38R1oNPWJtuFxslbtY84Lic4JQymViyP0ACqT1Ns9612I9zJSFUp6sMC+r/HXvjaP6SwM11mHQrrwydC2VwxYVVR9iFrnJSao5mdBhRK0x/2lQRBBKJz9NNdY7MQl1dMEv57LRxKrrELBskAjcDBkcEPp2RDwYkaH5euT9K7NSNJS4PhpkEL/hgqqx7RyLF/uo2j3Lt9fcuhemaa18xN7k4h5IUWh1dzojyglQ== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7a043302-8a7d-4304-6530-08d8457de6c4 X-MS-Exchange-CrossTenant-AuthSource: DB8PR04MB7162.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Aug 2020 02:57:11.9740 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: REfhCgkDg9RqYLH6jKf2CfQ6dyvKR+zkzYjUEC0/v6fvRR6WeHsy5GpvU+R4FkYsVjcy1U0g5IPrjXnkDj80bQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB7PR04MB5577 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org From: Alan Stern The routines used by the UDC core to interface with the kernel's device model, namely usb_add_gadget_udc(), usb_add_gadget_udc_release(), and usb_del_gadget_udc(), provide access to only a subset of the device model's full API. They include functionality equivalent to device_register() and device_unregister() for gadgets, but they omit device_initialize(), device_add(), device_del(), get_device(), and put_device(). This patch expands the UDC API by adding usb_initialize_gadget(), usb_add_gadget(), usb_del_gadget(), usb_get_gadget(), and usb_put_gadget() to fill in the gap. It rewrites the existing routines to call the new ones. CC: Anton Vasilyev CC: Evgeny Novikov CC: Benjamin Herrenschmidt Reviewed-by: Greg Kroah-Hartman Signed-off-by: Alan Stern Signed-off-by: Peter Chen --- drivers/usb/gadget/udc/core.c | 78 ++++++++++++++++++++++++++++------- include/linux/usb/gadget.h | 27 +++++++++--- 2 files changed, 84 insertions(+), 21 deletions(-) diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c index 4f82bcd31fd3..2b6770d9fb3f 100644 --- a/drivers/usb/gadget/udc/core.c +++ b/drivers/usb/gadget/udc/core.c @@ -1164,21 +1164,18 @@ static int check_pending_gadget_drivers(struct usb_udc *udc) } /** - * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list + * usb_initialize_gadget - initialize a gadget and its embedded struct device * @parent: the parent device to this udc. Usually the controller driver's * device. - * @gadget: the gadget to be added to the list. + * @gadget: the gadget to be initialized. * @release: a gadget release function. * * Returns zero on success, negative errno otherwise. * Calls the gadget release function in the latter case. */ -int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, +void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget, void (*release)(struct device *dev)) { - struct usb_udc *udc; - int ret = -ENOMEM; - dev_set_name(&gadget->dev, "gadget"); INIT_WORK(&gadget->work, usb_gadget_state_work); gadget->dev.parent = parent; @@ -1189,17 +1186,32 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, gadget->dev.release = usb_udc_nop_release; device_initialize(&gadget->dev); +} +EXPORT_SYMBOL_GPL(usb_initialize_gadget); + +/** + * usb_add_gadget - adds a new gadget to the udc class driver list + * @gadget: the gadget to be added to the list. + * + * Returns zero on success, negative errno otherwise. + * Does not do a final usb_put_gadget() if an error occurs. + */ +int usb_add_gadget(struct usb_gadget *gadget) +{ + struct usb_udc *udc; + int ret = -ENOMEM; udc = kzalloc(sizeof(*udc), GFP_KERNEL); if (!udc) - goto err_put_gadget; + goto error; device_initialize(&udc->dev); udc->dev.release = usb_udc_release; udc->dev.class = udc_class; udc->dev.groups = usb_udc_attr_groups; - udc->dev.parent = parent; - ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj)); + udc->dev.parent = gadget->dev.parent; + ret = dev_set_name(&udc->dev, "%s", + kobject_name(&gadget->dev.parent->kobj)); if (ret) goto err_put_udc; @@ -1242,8 +1254,30 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, err_put_udc: put_device(&udc->dev); - err_put_gadget: - put_device(&gadget->dev); + error: + return ret; +} +EXPORT_SYMBOL_GPL(usb_add_gadget); + +/** + * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list + * @parent: the parent device to this udc. Usually the controller driver's + * device. + * @gadget: the gadget to be added to the list. + * @release: a gadget release function. + * + * Returns zero on success, negative errno otherwise. + * Calls the gadget release function in the latter case. + */ +int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, + void (*release)(struct device *dev)) +{ + int ret; + + usb_initialize_gadget(parent, gadget, release); + ret = usb_add_gadget(gadget); + if (ret) + usb_put_gadget(gadget); return ret; } EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release); @@ -1311,13 +1345,14 @@ static void usb_gadget_remove_driver(struct usb_udc *udc) } /** - * usb_del_gadget_udc - deletes @udc from udc_list + * usb_del_gadget - deletes @udc from udc_list * @gadget: the gadget to be removed. * - * This, will call usb_gadget_unregister_driver() if + * This will call usb_gadget_unregister_driver() if * the @udc is still busy. + * It will not do a final usb_put_gadget(). */ -void usb_del_gadget_udc(struct usb_gadget *gadget) +void usb_del_gadget(struct usb_gadget *gadget) { struct usb_udc *udc = gadget->udc; @@ -1340,7 +1375,20 @@ void usb_del_gadget_udc(struct usb_gadget *gadget) kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); flush_work(&gadget->work); device_unregister(&udc->dev); - device_unregister(&gadget->dev); + device_del(&gadget->dev); +} +EXPORT_SYMBOL_GPL(usb_del_gadget); + +/** + * usb_del_gadget_udc - deletes @udc from udc_list + * @gadget: the gadget to be removed. + * + * Calls usb_del_gadget() and does a final usb_put_gadget(). + */ +void usb_del_gadget_udc(struct usb_gadget *gadget) +{ + usb_del_gadget(gadget); + usb_put_gadget(gadget); memset(&gadget->dev, 0x00, sizeof(gadget->dev)); } EXPORT_SYMBOL_GPL(usb_del_gadget_udc); diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h index 52ce1f6b8f83..e7351d64f11f 100644 --- a/include/linux/usb/gadget.h +++ b/include/linux/usb/gadget.h @@ -436,6 +436,7 @@ struct usb_gadget { }; #define work_to_gadget(w) (container_of((w), struct usb_gadget, work)) +/* Interface to the device model */ static inline void set_gadget_data(struct usb_gadget *gadget, void *data) { dev_set_drvdata(&gadget->dev, data); } static inline void *get_gadget_data(struct usb_gadget *gadget) @@ -444,6 +445,26 @@ static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev) { return container_of(dev, struct usb_gadget, dev); } +static inline struct usb_gadget *usb_get_gadget(struct usb_gadget *gadget) +{ + get_device(&gadget->dev); + return gadget; +} +static inline void usb_put_gadget(struct usb_gadget *gadget) +{ + put_device(&gadget->dev); +} +extern void usb_initialize_gadget(struct device *parent, + struct usb_gadget *gadget, void (*release)(struct device *dev)); +extern int usb_add_gadget(struct usb_gadget *gadget); +extern void usb_del_gadget(struct usb_gadget *gadget); + +/* Legacy device-model interface */ +extern int usb_add_gadget_udc_release(struct device *parent, + struct usb_gadget *gadget, void (*release)(struct device *dev)); +extern int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget); +extern void usb_del_gadget_udc(struct usb_gadget *gadget); +extern char *usb_get_gadget_udc_name(void); /* iterates the non-control endpoints; 'tmp' is a struct usb_ep pointer */ #define gadget_for_each_ep(tmp, gadget) \ @@ -735,12 +756,6 @@ int usb_gadget_probe_driver(struct usb_gadget_driver *driver); */ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver); -extern int usb_add_gadget_udc_release(struct device *parent, - struct usb_gadget *gadget, void (*release)(struct device *dev)); -extern int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget); -extern void usb_del_gadget_udc(struct usb_gadget *gadget); -extern char *usb_get_gadget_udc_name(void); - /*-------------------------------------------------------------------------*/ /* utility to simplify dealing with string descriptors */ From patchwork Fri Aug 21 02:55:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Chen X-Patchwork-Id: 11727461 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CC5D4722 for ; Fri, 21 Aug 2020 02:57:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ABCC72076E for ; Fri, 21 Aug 2020 02:57:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="XAqrXbk3" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727048AbgHUC5X (ORCPT ); Thu, 20 Aug 2020 22:57:23 -0400 Received: from mail-eopbgr50073.outbound.protection.outlook.com ([40.107.5.73]:27655 "EHLO EUR03-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725852AbgHUC5W (ORCPT ); Thu, 20 Aug 2020 22:57:22 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=H0kxJBn1W5r8jDEQqZ9jvzfb+hEDNha7ML6eV0cUXyifREdrTsLbd6RpgNDu1Gp5/JII7TV55raA2YTn5TPgaozT0U2Dpyo/5YaHUnLtidjpJaZTw2SAt+I2rYX0AELwrBoWZ31AdRGjtldJc7/tUYaqf6QTq3YUyMNeIbegLea5n3gREco4Xdmstt/yMVjXAOoFHym95jbBPsvQQeJxAFjSx/sy/FaSZ9npRccGNO5Otob+KVpTIlP9OjhgSbevQQ7YRmYtQ0QMEk52yoRx5WIk/WQ3F/ZAG5hHDVN1CMN20I7fT1TTYe9yuFoQLwKbyFe5ypQ4HhL08LY9sxKflA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uWXQdHo3prLIR1gqseQQ6qkOo4GyDPRznnvcOyp9Yio=; b=PbPYqGxg7f7c4DArZcHEn0c+WoSvHcCdyzMqGdS4n5hz6qsdqVy3H8c7WeDDMFkUOAJrlx6uFJCDq36ljFrTOILvi4aId4ESrKEb3Nhi8Jtp8q1afakX3AjY/2j4x+Jywt2u1MshQwjg30JQRBSyS5vflgB0qHLxsAAXYfOafDc0E5tJDsLJqspl7BTjF3ydXldiLl8vWq+vbsmwEiAm5hqqPXi8GCHANZLWG6olN/jOZQrJNzSEm0PNM4+PmeqVtr06rQEiGAGVSnXWkQr1RR74aEPO3xmqIsbR+dJV3rXFlEQjbFj7kYeQggPYWphznlyNKKYEmB5ljKw3FnNf4Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uWXQdHo3prLIR1gqseQQ6qkOo4GyDPRznnvcOyp9Yio=; b=XAqrXbk3sVZlz05FMy6wOLXRFswpXuGs9EyF1CYGZaPEg5dHeLEfWqztHBHPR7c/sBmvuKDr45wFR2kOqylHhi/TGPMpoAypvzD9khoJtDVJOsJ+zkGBOiKx7yoBTTVdCzu1ra5QrBQLoIYey+49OF+XSc8lx0AecXFcNRklbHI= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=nxp.com; Received: from DB8PR04MB7162.eurprd04.prod.outlook.com (2603:10a6:10:12c::13) by DB7PR04MB5577.eurprd04.prod.outlook.com (2603:10a6:10:8a::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.26; Fri, 21 Aug 2020 02:57:14 +0000 Received: from DB8PR04MB7162.eurprd04.prod.outlook.com ([fe80::acee:9763:7898:84a2]) by DB8PR04MB7162.eurprd04.prod.outlook.com ([fe80::acee:9763:7898:84a2%9]) with mapi id 15.20.3283.027; Fri, 21 Aug 2020 02:57:14 +0000 From: Peter Chen To: balbi@kernel.org Cc: linux-usb@vger.kernel.org, linux-imx@nxp.com, gregkh@linuxfoundation.org, stern@rowland.harvard.edu, Benjamin Herrenschmidt , Peter Chen Subject: [PATCH v4 2/6] USB: UDC: net2280: Fix memory leaks Date: Fri, 21 Aug 2020 10:55:45 +0800 Message-Id: <20200821025549.4591-3-peter.chen@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200821025549.4591-1-peter.chen@nxp.com> References: <20200821025549.4591-1-peter.chen@nxp.com> X-ClientProxiedBy: SG2PR01CA0132.apcprd01.prod.exchangelabs.com (2603:1096:4:40::36) To DB8PR04MB7162.eurprd04.prod.outlook.com (2603:10a6:10:12c::13) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from b29397-desktop.ap.freescale.net (119.31.174.67) by SG2PR01CA0132.apcprd01.prod.exchangelabs.com (2603:1096:4:40::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.24 via Frontend Transport; Fri, 21 Aug 2020 02:57:12 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [119.31.174.67] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: ed68b430-9a80-43e9-b85a-08d8457de859 X-MS-TrafficTypeDiagnostic: DB7PR04MB5577: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LRJHVxpZhM2Ke+/6x31wmqaNbVBbsf2Fwt8k3C5pnAcPEvt4XG5aDoEimSmlRVOSYD8D0t3aL9MUzCOxXO/TMf77FaIubjrmp58ZyRKnRqVtWBPR7GI9loUtHQlzJc6FrAw+k45KLG3BI138ZiSzW8mfhPyDU1STIsu/RMaEBKFua8Gl4o2Y8k1ySFhQ30bOwcwtbsz+n6fdbQC36ynjB2Dj1jkufC+UB7NrUy6ZftqBATJAm6FU1Iz/ecA7wFDrhRCEy7H+NVw/nn/reumy3C9VPjSCXEmlCzlL4cXPEf9Nh3UPasPBYJgyik3N2BEyALGmsSV/dAMAnTuphSsXQA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DB8PR04MB7162.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(376002)(136003)(366004)(346002)(39860400002)(52116002)(66946007)(2906002)(8676002)(6486002)(4326008)(6506007)(478600001)(316002)(6666004)(44832011)(36756003)(54906003)(956004)(26005)(66476007)(66556008)(2616005)(8936002)(1076003)(6512007)(5660300002)(6916009)(186003)(83380400001)(86362001)(16526019);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: ZWrnBAKxzCQtJbX1etTs/g/ziE9XnG3NYTDN9c6RMP9nxwhxD3TKnZEJn+a7BzP3CjG8JmqPa6cs1lMbmAvdKYJ67Lc5JWyhivFci96aYFF1XZMATj0S+/2cJPT02Rln2WfUZv9eQ7ej7TxXOBpEh+oJma7tTbPQDxjG7WcQ4Ia2XOSFLuH5WTf7UUQue7J50QDoCXrxg1/KFb2C3sYszrUkYwcDIhAt5EuxdGdQbjFDLur9SitE7M8QOE9mV55z5BqM55g4mNoCyyB5J9Np00WfmxeAUowJjUX4jtPnkMYY5FHN2BnZ5LI9Kto251nakzjA72RKw/yKm75U1XNkxmgYIYp8XytXRj3P4zKqswBZ1yvoiQhhCWRHTrAJNVYrLXOE0njh7/tUUCrt/CRy/JrDfbt72lUmKpvXyysktCNPv0Cf0uyidvECz3jlWQJOCEGaMatLwbpSmjBPtEv1o+OfAQoExJ/aIZZGenyQcLPdIkzwJa6GNJ9jZhgAVjf9b9JxdX/4yCss1h2mYdIgxnW9o/3sg9upVLcjamtyB/NFRJpNyxFthfIWXximAEoxlDaoCYk8pujOHY8ZtyLngUogZNvmP2ex8ym15CjK9ms5DjPdMpj84OzuZ0RDfCzFwp9LPRlQFhdU16QkUoUZKA== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: ed68b430-9a80-43e9-b85a-08d8457de859 X-MS-Exchange-CrossTenant-AuthSource: DB8PR04MB7162.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Aug 2020 02:57:14.6214 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: a6nq6ivCtwt81dAszVNZ2lUsQQ7CcDoIjNr/f4tccElMOM6n06ALSpp37A74Ph4yGz09Nbz4h8m72HioXisOew== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB7PR04MB5577 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org From: Alan Stern As Anton and Evgeny have noted, the net2280 UDC driver has a problem with leaking memory along some of its failure pathways. It also has another problem, not previously noted, in that some of the failure pathways will call usb_del_gadget_udc() without first calling usb_add_gadget_udc_release(). And it leaks memory by calling kfree() when it should call put_device(). Previous attempts to fix the problems have failed because of lack of support in the UDC core for separately initializing and adding gadgets, or for separately deleting and freeing gadgets. The previous patch in this series adds the necessary support, making it possible to fix the outstanding problems properly. This patch adds an "added" flag to the net2280 structure to indicate whether or not the gadget has been registered (and thus whether or not to call usb_del_gadget()), and it fixes the deallocation issues by calling usb_put_gadget() at the appropriate point. A similar memory leak issue, apparently never before recognized, stems from the fact that the driver never initializes the drvdata field in the gadget's embedded struct device! Evidently this wasn't noticed because the pointer is only ever used as an argument to kfree(), which doesn't mind getting called with a NULL pointer. In fact, the drvdata for gadget device will be written by usb_composite_dev structure if any gadget class is loaded, so it needs to use usb_gadget structure to get net2280 private data. CC: Benjamin Herrenschmidt Reported-by: Anton Vasilyev Reported-by: Evgeny Novikov Reviewed-by: Greg Kroah-Hartman Signed-off-by: Alan Stern Signed-off-by: Peter Chen --- drivers/usb/gadget/udc/net2280.c | 11 +++++++---- drivers/usb/gadget/udc/net2280.h | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/usb/gadget/udc/net2280.c b/drivers/usb/gadget/udc/net2280.c index 7530bd9a08c4..d50bc6e19f2a 100644 --- a/drivers/usb/gadget/udc/net2280.c +++ b/drivers/usb/gadget/udc/net2280.c @@ -3561,7 +3561,7 @@ static irqreturn_t net2280_irq(int irq, void *_dev) static void gadget_release(struct device *_dev) { - struct net2280 *dev = dev_get_drvdata(_dev); + struct net2280 *dev = container_of(_dev, struct net2280, gadget.dev); kfree(dev); } @@ -3572,7 +3572,8 @@ static void net2280_remove(struct pci_dev *pdev) { struct net2280 *dev = pci_get_drvdata(pdev); - usb_del_gadget_udc(&dev->gadget); + if (dev->added) + usb_del_gadget(&dev->gadget); BUG_ON(dev->driver); @@ -3603,6 +3604,7 @@ static void net2280_remove(struct pci_dev *pdev) device_remove_file(&pdev->dev, &dev_attr_registers); ep_info(dev, "unbind\n"); + usb_put_gadget(&dev->gadget); } /* wrap this driver around the specified device, but @@ -3624,6 +3626,7 @@ static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id) } pci_set_drvdata(pdev, dev); + usb_initialize_gadget(&pdev->dev, &dev->gadget, gadget_release); spin_lock_init(&dev->lock); dev->quirks = id->driver_data; dev->pdev = pdev; @@ -3774,10 +3777,10 @@ static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id) if (retval) goto done; - retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget, - gadget_release); + retval = usb_add_gadget(&dev->gadget); if (retval) goto done; + dev->added = 1; return 0; done: diff --git a/drivers/usb/gadget/udc/net2280.h b/drivers/usb/gadget/udc/net2280.h index 85d3ca1698ba..7da3dc1e9729 100644 --- a/drivers/usb/gadget/udc/net2280.h +++ b/drivers/usb/gadget/udc/net2280.h @@ -156,6 +156,7 @@ struct net2280 { softconnect : 1, got_irq : 1, region:1, + added:1, u1_enable:1, u2_enable:1, ltm_enable:1, From patchwork Fri Aug 21 02:55:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Chen X-Patchwork-Id: 11727463 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 28E23722 for ; Fri, 21 Aug 2020 02:57:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0CD1920855 for ; Fri, 21 Aug 2020 02:57:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="nr0q3Th3" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727053AbgHUC5Z (ORCPT ); Thu, 20 Aug 2020 22:57:25 -0400 Received: from mail-eopbgr50073.outbound.protection.outlook.com ([40.107.5.73]:27655 "EHLO EUR03-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726840AbgHUC5Y (ORCPT ); Thu, 20 Aug 2020 22:57:24 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YCfdkldMgoEZh7fThrgsfSU4LxmvRZeAkQLUViO+cVZLOmaNwXoByy2sX4XyslB9SxJJ1ap7bQkz9CqzKD0V5curkf6AJU8LabZ2ldH8lUCwKGYqrsuh3Fv5xEXja6M3pef6hUT9xRKXC3Obd79/RGKraKMONTFbtzExROVZLfuyhfZYkMH+QfOL1EyDMrQ/IN5ZElo/V6yjTb2xbVz4bEnfZas+gmdnlwgzRUTmpSxCeJqvQ+XbnuSOlO08KjfyUP3KGUtT5mJIIMkmW4xQZ+APJB2UGWgHKhhor5+6EkPjILN/3JtJu+f+zrxQ0guvdSt4gdsZeLTkRFOZvlrzXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/krRLvd/SuWTMIjpxNrkzqnZ+jgfBdjVuApIpNte0f4=; b=mdrAyTG2F7Fni2cB5EJJQft8y1E0fF112ySNr/0HAVjxzJpKVVKzW9ur+CW0N8REMipukro8EFKWP1oLY/1fkRZTZOX8h0y62VjCsbKfHbdT40YD1ysE2zMAjjsYWjQwIBeFN3MfECDkpTEJSW7Mf3LY/l9vyZNd3gX9kCYP21IFj9S9cpiEmGe3Mrj7KQpT3UPFSkGWIlu25Rqoene38eu9PI4jDi4Tb3gG0kwJLdZSXmGMkR1giTeRbJwQ3Cwu8itp2aVW6955XnUyyb/WUhO6NvukdaCRn2s39s09gtrBCIIpkpl1KFlf7ROKHEXGJNVyoFff0NG3Ht7kNN8MtQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/krRLvd/SuWTMIjpxNrkzqnZ+jgfBdjVuApIpNte0f4=; b=nr0q3Th3ZMQga3DUHOAAAsKfCOYHnkVx/laOHNShxNrE/oZc/mnAWCQbKOmHXcF8oIm53jZ2HrHKbLPQBjhxafnom9byEWrB6bju+1pDGzMb8lS6jgxFMFkCqO+b18fqGgh5s4rnqd64IGVvsQYsNSr+YwYr4Pkb75PJxhy5KI8= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=nxp.com; Received: from DB8PR04MB7162.eurprd04.prod.outlook.com (2603:10a6:10:12c::13) by DB7PR04MB5577.eurprd04.prod.outlook.com (2603:10a6:10:8a::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.26; Fri, 21 Aug 2020 02:57:17 +0000 Received: from DB8PR04MB7162.eurprd04.prod.outlook.com ([fe80::acee:9763:7898:84a2]) by DB8PR04MB7162.eurprd04.prod.outlook.com ([fe80::acee:9763:7898:84a2%9]) with mapi id 15.20.3283.027; Fri, 21 Aug 2020 02:57:17 +0000 From: Peter Chen To: balbi@kernel.org Cc: linux-usb@vger.kernel.org, linux-imx@nxp.com, gregkh@linuxfoundation.org, stern@rowland.harvard.edu, Anton Vasilyev , Evgeny Novikov , Benjamin Herrenschmidt , Peter Chen Subject: [PATCH v4 3/6] USB: UDC: net2272: Fix memory leaks Date: Fri, 21 Aug 2020 10:55:46 +0800 Message-Id: <20200821025549.4591-4-peter.chen@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200821025549.4591-1-peter.chen@nxp.com> References: <20200821025549.4591-1-peter.chen@nxp.com> X-ClientProxiedBy: SG2PR01CA0132.apcprd01.prod.exchangelabs.com (2603:1096:4:40::36) To DB8PR04MB7162.eurprd04.prod.outlook.com (2603:10a6:10:12c::13) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from b29397-desktop.ap.freescale.net (119.31.174.67) by SG2PR01CA0132.apcprd01.prod.exchangelabs.com (2603:1096:4:40::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.24 via Frontend Transport; Fri, 21 Aug 2020 02:57:14 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [119.31.174.67] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: efdec1b8-e4b8-43b1-a32a-08d8457dea33 X-MS-TrafficTypeDiagnostic: DB7PR04MB5577: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: l0Fa6BVQ/Ngw9zumzp4j5cyCAyVdp2YzKLXgvqnoCnwIkiab9SEbIkdqYl9ijKtuZaZXqBxutYuz5CB7wdgJY/NcuOi/1x8cLOIOFw5fDdcfCl9/6c+456jR3OVKa8TSW3eYJa0MSJnnWb9Vk0MjKvuioLIKOq2JjdaLgVU6N25oXJvpk2Y5HM1L7+35GqozSZKLgjtb/vZINoStZ67LsjCw6DhZ4eXIKOqucY3oOuhtPjh35VLGU42U2OtfOBw34gQ5ppEii8dnEdp9CvY3nj7NIrJ4cJr8d5IqFeeZBG4adAQFzBQ3y7h9lIYPxYgHBTNP4vL0V1jfFufwG2sCcA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DB8PR04MB7162.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(376002)(136003)(366004)(346002)(39860400002)(52116002)(66946007)(2906002)(8676002)(6486002)(4326008)(6506007)(478600001)(316002)(6666004)(44832011)(36756003)(54906003)(956004)(26005)(66476007)(66556008)(2616005)(8936002)(1076003)(6512007)(5660300002)(6916009)(186003)(83380400001)(86362001)(16526019);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: Ru49j7kptQFSaEmS9YcA63we0LOZZwxQGxjWAF1fS7tvpZ9kN7q9mXcjIKTqpLTH80nrfvPKJam019L6gKlbgfvzY8Auz/yKNBKIGZSz5bmKKmATBrqjls6e0gn4qRdm7W0bDJKV/D7T9MiqJdO6Of4T1snZpHjKEB8iZYMDR8r0dibYHNg6LpYN1xr7Fksdk2WBZa2a2XfIRm+Nj8Pgp5cMXNDPO6OZ2+oJtW1FcomTsbWeeMG5MNjhWvBv6zdTWxFIwopJJn51XrpIhDDGd6v9RMQoCjFDdBKNVA4Fbk6I12w0Mw8XLnA6DwZGcx6yT/n1fL401mqSKbUBXMkgaIKm2qyEPJHzsoFNVygsv4Uu1VfAUCcewiKRFyRSGun0C6+hdeMS28/baWtfi8bqNFVa5iYduBtK3IS77MFDpzOGA/SkFvgBv1wT9YEoNw9KmgGVrf3kdXlQHHncMmcuZf41B4ONj8mdq6MXosNo3dI/httWuK3p09PiT6ioiMs6t6TYArs4djQG755f1zn6PShB0pweZES6qSwRCmHBlb9rf/wKe+TBQwkwRZ3/7I4i91IJX2xt/ik2jsyNhFo9q9fKR3rJ8+49MtQIn6dpS2wdzSDOOlAiIjvlbVayVi4agjaBW3ir1v92InV9GwJZzQ== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: efdec1b8-e4b8-43b1-a32a-08d8457dea33 X-MS-Exchange-CrossTenant-AuthSource: DB8PR04MB7162.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Aug 2020 02:57:17.7263 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: BgQCFyZsZpj/grlCp8JkqrTDfLRYieQMt4PRxlsc0y1MLRLcis05tKWW8BDyTMNKrHg4IYPtkW4QV177utbirg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB7PR04MB5577 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org From: Alan Stern Like net2280 (on which it was based), the net2272 UDC driver has a problem with leaking memory along some of its failure pathways. It also has another problem, not previously noted, in that some of the failure pathways will call usb_del_gadget_udc() without first calling usb_add_gadget_udc_release(). And it leaks memory by calling kfree() when it should call put_device(). Until now it has been impossible to handle the memory leaks, because of lack of support in the UDC core for separately initializing and adding gadgets, or for separately deleting and freeing gadgets. An earlier patch in this series adds the necessary support, making it possible to fix the outstanding problems properly. This patch adds an "added" flag to the net2272 structure to indicate whether or not the gadget has been registered (and thus whether or not to call usb_del_gadget()), and it fixes the deallocation issues by calling usb_put_gadget() at the appropriate places. A similar memory leak issue, apparently never before recognized, stems from the fact that the driver never initializes the drvdata field in the gadget's embedded struct device! Evidently this wasn't noticed because the pointer is only ever used as an argument to kfree(), which doesn't mind getting called with a NULL pointer. In fact, the drvdata for gadget device will be written by usb_composite_dev structure if any gadget class is loaded, so it needs to use usb_gadget structure to get net2280 private data. CC: Anton Vasilyev CC: Evgeny Novikov CC: Benjamin Herrenschmidt Reviewed-by: Greg Kroah-Hartman Signed-off-by: Alan Stern Signed-off-by: Peter Chen --- drivers/usb/gadget/udc/net2272.c | 23 +++++++++++++---------- drivers/usb/gadget/udc/net2272.h | 1 + 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/drivers/usb/gadget/udc/net2272.c b/drivers/usb/gadget/udc/net2272.c index 44d1ea2307bb..46d1e23c575a 100644 --- a/drivers/usb/gadget/udc/net2272.c +++ b/drivers/usb/gadget/udc/net2272.c @@ -2196,7 +2196,8 @@ static int net2272_present(struct net2272 *dev) static void net2272_gadget_release(struct device *_dev) { - struct net2272 *dev = dev_get_drvdata(_dev); + struct net2272 *dev = container_of(_dev, struct net2272, gadget.dev); + kfree(dev); } @@ -2205,7 +2206,8 @@ net2272_gadget_release(struct device *_dev) static void net2272_remove(struct net2272 *dev) { - usb_del_gadget_udc(&dev->gadget); + if (dev->added) + usb_del_gadget(&dev->gadget); free_irq(dev->irq, dev); iounmap(dev->base_addr); device_remove_file(dev->dev, &dev_attr_registers); @@ -2235,6 +2237,7 @@ static struct net2272 *net2272_probe_init(struct device *dev, unsigned int irq) /* the "gadget" abstracts/virtualizes the controller */ ret->gadget.name = driver_name; + usb_initialize_gadget(dev, &ret->gadget, net2272_gadget_release); return ret; } @@ -2273,10 +2276,10 @@ net2272_probe_fin(struct net2272 *dev, unsigned int irqflags) if (ret) goto err_irq; - ret = usb_add_gadget_udc_release(dev->dev, &dev->gadget, - net2272_gadget_release); + ret = usb_add_gadget(&dev->gadget); if (ret) goto err_add_udc; + dev->added = 1; return 0; @@ -2451,7 +2454,7 @@ net2272_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) if (pci_enable_device(pdev) < 0) { ret = -ENODEV; - goto err_free; + goto err_put; } pci_set_master(pdev); @@ -2474,8 +2477,8 @@ net2272_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) err_pci: pci_disable_device(pdev); - err_free: - kfree(dev); + err_put: + usb_put_gadget(&dev->gadget); return ret; } @@ -2536,7 +2539,7 @@ net2272_pci_remove(struct pci_dev *pdev) pci_disable_device(pdev); - kfree(dev); + usb_put_gadget(&dev->gadget); } /* Table of matching PCI IDs */ @@ -2649,7 +2652,7 @@ net2272_plat_probe(struct platform_device *pdev) err_req: release_mem_region(base, len); err: - kfree(dev); + usb_put_gadget(&dev->gadget); return ret; } @@ -2664,7 +2667,7 @@ net2272_plat_remove(struct platform_device *pdev) release_mem_region(pdev->resource[0].start, resource_size(&pdev->resource[0])); - kfree(dev); + usb_put_gadget(&dev->gadget); return 0; } diff --git a/drivers/usb/gadget/udc/net2272.h b/drivers/usb/gadget/udc/net2272.h index 87d0ab9ffeeb..c669308111c2 100644 --- a/drivers/usb/gadget/udc/net2272.h +++ b/drivers/usb/gadget/udc/net2272.h @@ -441,6 +441,7 @@ struct net2272 { unsigned protocol_stall:1, softconnect:1, wakeup:1, + added:1, dma_eot_polarity:1, dma_dack_polarity:1, dma_dreq_polarity:1, From patchwork Fri Aug 21 02:55:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Chen X-Patchwork-Id: 11727465 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E8E35109B for ; Fri, 21 Aug 2020 02:57:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CCC69207BB for ; Fri, 21 Aug 2020 02:57:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="TGAPCn2W" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727064AbgHUC52 (ORCPT ); Thu, 20 Aug 2020 22:57:28 -0400 Received: from mail-eopbgr50073.outbound.protection.outlook.com ([40.107.5.73]:27655 "EHLO EUR03-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725852AbgHUC51 (ORCPT ); Thu, 20 Aug 2020 22:57:27 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C/SypZR//bkp+bHlYBtjrLpoJmFQkKsj8tUB2DyKYIFqUS3f53bfEjwkeNERb7IZyTefsZjMSOgA7f3u4ACnW7SXa2XMajxsYMIpf+1km0xdmHg7Yg/zbSfVAgYfwUG8pkWSezArLTV35m91Ak7J3kJ1tbYgX6X81vg6FnFU3fcFmcgA9ZLM6TUdU39omu/0ZUzKzpYTSkXNtHh6z+BBp1h6Ylk9xtH3UvkLbVFuOi93cYb55Wqc8ld+nynmmWnqYyRL26w7xlo+4vgNJDVCoxdx7FdTzHTODeY6pZ4fCsF4AMeJXJM7tBQXJ+nkyO0qsKT1dxVacfTxnUZcZzblkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=m1W3WCbN8OC3F7KdKMlG7SPTkA/1CC+6n6vCaMelW7k=; b=B6akdni5a+2N6ndtiwoN8B2EZpaM30I+y3j3blFeiLGOAVPGVwft6/hfAREW1IlRKh2ebs1AILfGK+9DVU1mQQwQqNzVHYxL5DmTc9WBsE6JaGzPbMeJqn9YW1yIdFqi8k9cVIZu5V/YXuR9RSVQc1dIGSUBWgxfknZHAsMo1XzoQncLvWqq64H4xvJtJ9efG32JCukHh5V3FZ+HAc2Gr03lEsMQQ6j/4agq/YnjEta5HeaW4IlyqWduUtIZE71OccVyb85PQSz0kps2eQTRoTITH8QObEDV06TRDmWQIGRCgkl4OHKR+J0UCZQ4Jz7datDPkSMcgTFsTsC4+x8nfw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=m1W3WCbN8OC3F7KdKMlG7SPTkA/1CC+6n6vCaMelW7k=; b=TGAPCn2WH0S+giZVU9q3biVVfxlBrhwjfa7U89vtHV01ga4quyYCPhz6qW7ea73C5v3i/JDjWh25kDXOReYWFqYFhCeu3NnRxe5s7PsDrA9nHQtK9/H8pv1uIhJ+Q94b5jnqqyh+1wms8L2hZfD8kQPps6pfAwAR+puNTaMmwIU= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=nxp.com; Received: from DB8PR04MB7162.eurprd04.prod.outlook.com (2603:10a6:10:12c::13) by DB7PR04MB5577.eurprd04.prod.outlook.com (2603:10a6:10:8a::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.26; Fri, 21 Aug 2020 02:57:20 +0000 Received: from DB8PR04MB7162.eurprd04.prod.outlook.com ([fe80::acee:9763:7898:84a2]) by DB8PR04MB7162.eurprd04.prod.outlook.com ([fe80::acee:9763:7898:84a2%9]) with mapi id 15.20.3283.027; Fri, 21 Aug 2020 02:57:20 +0000 From: Peter Chen To: balbi@kernel.org Cc: linux-usb@vger.kernel.org, linux-imx@nxp.com, gregkh@linuxfoundation.org, stern@rowland.harvard.edu, Peter Chen Subject: [PATCH v4 4/6] usb: cdns3: gadget: fix possible memory leak Date: Fri, 21 Aug 2020 10:55:47 +0800 Message-Id: <20200821025549.4591-5-peter.chen@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200821025549.4591-1-peter.chen@nxp.com> References: <20200821025549.4591-1-peter.chen@nxp.com> X-ClientProxiedBy: SG2PR01CA0132.apcprd01.prod.exchangelabs.com (2603:1096:4:40::36) To DB8PR04MB7162.eurprd04.prod.outlook.com (2603:10a6:10:12c::13) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from b29397-desktop.ap.freescale.net (119.31.174.67) by SG2PR01CA0132.apcprd01.prod.exchangelabs.com (2603:1096:4:40::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.24 via Frontend Transport; Fri, 21 Aug 2020 02:57:18 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [119.31.174.67] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 893f73c3-e1bc-4365-1ed8-08d8457deb9e X-MS-TrafficTypeDiagnostic: DB7PR04MB5577: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: S0+OSk/k+/tw7SfU4q49OzdGlrxMI0FbO9Q0l3eSbLA5FoFTQvF+lQAUJMtXyr+85829ValTqpdWrT1kj2+j2yUqQwyr7w+Lea5QfbpOcISBvI6IUFUQSOff71S2qe1ol9u2p/npagt0Ln/GpbQfMTu5ErXDRLge+fE+L4wPlV8BftpCkKY40ZaGp46nmNPWepWcoyP5WhSZrYXf4qC6n81OH+aPCq6tAmyjqFka9DZSoiJPZMjVeNMNYM/Izj9gUe2ZH9K5W08MRKwSiqyMlltGPHPC36nvTNOfAjqQdcSishIevSHszfqs1iG5oqJK X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DB8PR04MB7162.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(376002)(136003)(366004)(346002)(39860400002)(52116002)(66946007)(2906002)(8676002)(6486002)(4326008)(6506007)(478600001)(316002)(6666004)(44832011)(36756003)(956004)(26005)(66476007)(66556008)(2616005)(8936002)(1076003)(6512007)(5660300002)(6916009)(186003)(83380400001)(86362001)(16526019);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 3vjcwPuJroD6TXCd4qDiY6lPEiYndoIM+EiMVq4IO5KXJGVF5qTfEsxpcL/SYJSxbkbzFaQSih8oQKIE8ilP1vwPLTXy4MbujJb6fVsfUTCFuiSZ0nsQfLF3EnsJznqobFC408KlvQ6wqpFpVTi6YHmWXYOQ6sK5ty90RmG31hnjzPOmadNXtCUtJntKR6LNUlYEIm/Vtig8EtQEZ2X4K+t+dasPhj7f9vAA0Kyh/+IFeSjlBRiP8Sp9vos8DKLwdBVNRFBWXmSJ3OrIOvoCBlDahwC4c+olyT9Bi3rbf4Ttj/JHe2iW+Bv33iLqRKLyNP5G9+M22gJ5ZnEV2eTVZ4QIdvjld8TxeoZlxS0yanFb/ihMCJ2rt38ciupNHGWNWx/20o9jbSuhv2jxeda05E/cI5poJW7pN31Z8jGkc+MU17Gy2ewe5SSstBIwUGKYacfbS9COqsfCnjDJ2Q6Nvq4gk8toS2te6yAaGkky3TliNgp4Pu4BuFBE6xb8Frk6iE4ZwtiJ9ijHFKQiOxBVsJkUKG86BE/SlkAb+Mmeql6IeAS+Wwb0VMCMQ13JLaokv+nOv5txO7aoNotyTQ+MlP6p2t+eNyDqE0ZYSHZTtrh22VCeH6oiauKpYmAXIOOr1WEqCvOJyiAGxXxzI1BLMw== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 893f73c3-e1bc-4365-1ed8-08d8457deb9e X-MS-Exchange-CrossTenant-AuthSource: DB8PR04MB7162.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Aug 2020 02:57:20.0999 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: zMPxGDTFsr3dh2H1BcGo7brngOj9PRDrxpYKv3bxPE6ZQ2KZBeWnlKTEu9mavTr9STp9+lFp9iNp9s5w+C4fuw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB7PR04MB5577 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org If cdns3_gadget_start is failed, it never frees cdns3_device structure. Meanwhile, there is no release function for gadget device, it causes there is no sync with driver core. To fix this, we add release function for gadget device, and free cdns3_device structure at there. Meanwhile, With the new UDC core APIs, we could work with driver core better to handle memory leak issue. Reviewed-by: Greg Kroah-Hartman Signed-off-by: Peter Chen --- drivers/usb/cdns3/gadget.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/drivers/usb/cdns3/gadget.c b/drivers/usb/cdns3/gadget.c index 8bbb38cd560b..721e7914d91c 100644 --- a/drivers/usb/cdns3/gadget.c +++ b/drivers/usb/cdns3/gadget.c @@ -2988,6 +2988,14 @@ static int cdns3_init_eps(struct cdns3_device *priv_dev) return -ENOMEM; } +static void cdns3_gadget_release(struct device *dev) +{ + struct cdns3_device *priv_dev = container_of(dev, + struct cdns3_device, gadget.dev); + + kfree(priv_dev); +} + void cdns3_gadget_exit(struct cdns3 *cdns) { struct cdns3_device *priv_dev; @@ -2999,7 +3007,7 @@ void cdns3_gadget_exit(struct cdns3 *cdns) pm_runtime_mark_last_busy(cdns->dev); pm_runtime_put_autosuspend(cdns->dev); - usb_del_gadget_udc(&priv_dev->gadget); + usb_del_gadget(&priv_dev->gadget); cdns3_free_all_eps(priv_dev); @@ -3019,7 +3027,7 @@ void cdns3_gadget_exit(struct cdns3 *cdns) priv_dev->setup_dma); kfree(priv_dev->zlp_buf); - kfree(priv_dev); + usb_put_gadget(&priv_dev->gadget); cdns->gadget_dev = NULL; cdns3_drd_gadget_off(cdns); } @@ -3034,6 +3042,8 @@ static int cdns3_gadget_start(struct cdns3 *cdns) if (!priv_dev) return -ENOMEM; + usb_initialize_gadget(cdns->dev, &priv_dev->gadget, + cdns3_gadget_release); cdns->gadget_dev = priv_dev; priv_dev->sysdev = cdns->dev; priv_dev->dev = cdns->dev; @@ -3121,10 +3131,9 @@ static int cdns3_gadget_start(struct cdns3 *cdns) } /* add USB gadget device */ - ret = usb_add_gadget_udc(priv_dev->dev, &priv_dev->gadget); + ret = usb_add_gadget(&priv_dev->gadget); if (ret < 0) { - dev_err(priv_dev->dev, - "Failed to register USB device controller\n"); + dev_err(priv_dev->dev, "Failed to add gadget\n"); goto err4; } @@ -3137,6 +3146,7 @@ static int cdns3_gadget_start(struct cdns3 *cdns) err2: cdns3_free_all_eps(priv_dev); err1: + usb_put_gadget(&priv_dev->gadget); cdns->gadget_dev = NULL; return ret; } From patchwork Fri Aug 21 02:55:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Chen X-Patchwork-Id: 11727467 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C666215E4 for ; Fri, 21 Aug 2020 02:57:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 955EC207DE for ; Fri, 21 Aug 2020 02:57:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="HPxSc6ge" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727068AbgHUC5c (ORCPT ); Thu, 20 Aug 2020 22:57:32 -0400 Received: from mail-eopbgr50073.outbound.protection.outlook.com ([40.107.5.73]:27655 "EHLO EUR03-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726840AbgHUC5a (ORCPT ); Thu, 20 Aug 2020 22:57:30 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YV3FfV1yfXyy2ZIFopnGYJ/CS3vkih5JaT3G5+eU6yLhD7j5nsppBTAl1F7PLkgrYiapprXHXr4aFPr2yLuZA9xE2f5r36mdF/fH4kvIJnGNd3kkIS0EeyZEx5x4KocICETZ3HNwlnZ2ystxsRG1V0AEjhTT015oBhzwPQv3rYAu1ddnGPzAwz1np2R/DN5b2Z7sJH1m2px1YfRmN7eve1Z9kG/QrhkDvM9qj0/P5vp70oW0aEIqZDZaER5u+JoSrDeOdcoKJUoF1NJIAXYv4wx3+vtd+dxSLfnVoSfg5XM1V+JKjH81Bmn8LvEGfNIyUmjdyBg997x6SixCAsXR1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=smp2OXtAij6UGWdQwcFsorYzHWkVWgbxvqKvc8me1Ck=; b=kxddbY3Gz6m4ZKv26vKEg8K8nyvSxwPmJ3gDRiPLiRZFNWj3wiP1IsjbeUmOipiqzY0uGPCGDF0iPeNk2UTKdODnqv/1QNZb2e60iaWVtQoYpnGriDf1zLBjvKzuZ8IrFcP4P6uzU7JhCUzdmgYQYTztFRnTxnsvX1jj8A2pzjQuilXxtfltS8hfCqeCflcttESGqN5rdkjwCH7YOkEGTi3OqRgZbCaKFyoovNQ64XoQUj4b9ckh3OLy0JeLGbcFC24x8ecUvCTIjeVl2G+N2ryW7Njx+50PPjCYy42TUfRgG/fjq5gqCCm7xxBqdmtNgy0b6O30CctVHzlPlxpvdA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=smp2OXtAij6UGWdQwcFsorYzHWkVWgbxvqKvc8me1Ck=; b=HPxSc6geQ9doq+QIoriwbT9FU6cdD9B6Vj2EItB7LcgVkAWgvIR3gtNjjejzoUXMAP7mBb3kdmPfTppTa7kiSEtXRAf19c2RxcYl+7rQVY9xcZk/RjXltjlCMc3DTKslmXh/M5fXy1eMhqdmZpehjpXHKlaW/k5qL+VN4Pa9PyE= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=nxp.com; Received: from DB8PR04MB7162.eurprd04.prod.outlook.com (2603:10a6:10:12c::13) by DB7PR04MB5577.eurprd04.prod.outlook.com (2603:10a6:10:8a::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.26; Fri, 21 Aug 2020 02:57:22 +0000 Received: from DB8PR04MB7162.eurprd04.prod.outlook.com ([fe80::acee:9763:7898:84a2]) by DB8PR04MB7162.eurprd04.prod.outlook.com ([fe80::acee:9763:7898:84a2%9]) with mapi id 15.20.3283.027; Fri, 21 Aug 2020 02:57:22 +0000 From: Peter Chen To: balbi@kernel.org Cc: linux-usb@vger.kernel.org, linux-imx@nxp.com, gregkh@linuxfoundation.org, stern@rowland.harvard.edu, Peter Chen Subject: [PATCH v4 5/6] usb: dwc3: allocate gadget structure dynamically Date: Fri, 21 Aug 2020 10:55:48 +0800 Message-Id: <20200821025549.4591-6-peter.chen@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200821025549.4591-1-peter.chen@nxp.com> References: <20200821025549.4591-1-peter.chen@nxp.com> X-ClientProxiedBy: SG2PR01CA0132.apcprd01.prod.exchangelabs.com (2603:1096:4:40::36) To DB8PR04MB7162.eurprd04.prod.outlook.com (2603:10a6:10:12c::13) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from b29397-desktop.ap.freescale.net (119.31.174.67) by SG2PR01CA0132.apcprd01.prod.exchangelabs.com (2603:1096:4:40::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.24 via Frontend Transport; Fri, 21 Aug 2020 02:57:20 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [119.31.174.67] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 89188372-8851-4445-3661-08d8457ded0f X-MS-TrafficTypeDiagnostic: DB7PR04MB5577: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:352; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wdeAPDJ+IvPf7ir/A0HNpKqxLf95fxtlS4sSYC3nScKFKuI6qlB38Cjf6A5LYtAn3RqF1Lx2aFnKkQNRK0noIz0zJOQIgxm4wJfinI1ZsJk5pzDw9zZpGxQcAL2yYrTvJIQAfj4mKLROVR4FUP4M01MTruohJTyPgeXi2/eqTzugLRDSxxiiUo4Q6triwToCG318WGHMyNN8C06hDL/HdVGB72yVMXFxh6j0p3S2opSmhnyfcIqav7GI9byyChQ9fP1PMQ2rAUaS5UVMWvuRT+ErflhLklT8D8CwKNQtN8G5m7YpGW1ASJjdg4bargU1JXMrFG3Qel6awzihcgQ38A== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DB8PR04MB7162.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(376002)(136003)(366004)(346002)(39860400002)(30864003)(52116002)(66946007)(2906002)(8676002)(6486002)(4326008)(6506007)(478600001)(316002)(6666004)(19627235002)(44832011)(36756003)(956004)(26005)(66476007)(66556008)(2616005)(8936002)(1076003)(6512007)(5660300002)(6916009)(186003)(83380400001)(86362001)(16526019);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: jRf6ofP/z5lpxmrjek0Am0u0wgZ1xQQ4f5WX/7owZQgQfv73JKoBaEEGXfW6Adoz4w6lxTngyjmV3qzEjbZrEcdzo8TfSndCccF8THl6KE7rAjvrRdzN4gG7CpUx/spgn9qklITf//CsaNyHcbAl36PfRXIQUGX007ZrHkWN7vF5PU3uCBGC5ccZodLuxOEgDIKm77Fb8Ptiv+dAhv4f70oiJ3zhiviZSSSEicUtiC7FBgqM6Dj+6YVo4ar9A5ET91CvgdS7RB2Vnb0cdhxy4e9/m55Qkh01KpaFu9sKAF5nX9mR5X/zBu08G01rm61ra/D1ze08CZMvU6qDjQuEdLXVwFli936OUnB5uY2DQkU1+tfErbVOEO20qyZiVmrXH0VKvO5BpZBfToLmshEeR2DFUotzsceJn9v1eA1EgxD7rPqmwOh7OuOTAu9c3knexzreSyD/8JoRRULcRYZpE3h6JNeic7qxXB3b57956RySU4/vOQgMOCLQ8ADwTeHP77SoSk3KHuwYwQLZhwPyTLS8DrAh8u4FFYZUaA5n+7JxaW7c2V7/9LrCaaZQ3U64jBHivdk0xPqFK6yM1KoJIBbojQNZLS2XOD6KhjsxdPc3Z9l+S6zaGmyWCicKe/V6e4phFl7pGNr997UwanV32w== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 89188372-8851-4445-3661-08d8457ded0f X-MS-Exchange-CrossTenant-AuthSource: DB8PR04MB7162.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Aug 2020 02:57:22.6940 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: EQw1dIbf996nr9Qwe1oHgY02jwykbMRkt2RN0z4ZyELLlLM+vTtD+g1hdMBIUPDh5JtTNcdhP63sGeLnWX8Oqg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB7PR04MB5577 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org The current code uses commit fac323471df6 ("usb: udc: allow adding and removing the same gadget device") as the workaround to let the gadget device is re-used, but it is not allowed from driver core point. In this commit, we allocate gadget structure dynamically, and free it at its release function. Since the gadget device's driver_data has already occupied by usb_composite_dev structure, we have to use gadget device's platform data to store dwc3 structure. Cc: Greg Kroah-Hartman Cc: Alan Stern Reviewed-by: Greg Kroah-Hartman Signed-off-by: Peter Chen --- drivers/usb/dwc3/core.h | 2 +- drivers/usb/dwc3/ep0.c | 26 ++++----- drivers/usb/dwc3/gadget.c | 108 ++++++++++++++++++++++---------------- drivers/usb/dwc3/gadget.h | 2 +- 4 files changed, 78 insertions(+), 60 deletions(-) diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index 2f04b3e42bf1..219446bebeb6 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -1079,7 +1079,7 @@ struct dwc3 { struct dwc3_event_buffer *ev_buf; struct dwc3_ep *eps[DWC3_ENDPOINTS_NUM]; - struct usb_gadget gadget; + struct usb_gadget *gadget; struct usb_gadget_driver *gadget_driver; struct clk_bulk_data *clks; diff --git a/drivers/usb/dwc3/ep0.c b/drivers/usb/dwc3/ep0.c index 59f2e8c31bd1..9367ba960e01 100644 --- a/drivers/usb/dwc3/ep0.c +++ b/drivers/usb/dwc3/ep0.c @@ -131,7 +131,7 @@ static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep, direction = !dwc->ep0_expect_in; dwc->delayed_status = false; - usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED); + usb_gadget_set_state(dwc->gadget, USB_STATE_CONFIGURED); if (dwc->ep0state == EP0_STATUS_PHASE) __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]); @@ -325,7 +325,7 @@ static int dwc3_ep0_handle_status(struct dwc3 *dwc, /* * LTM will be set once we know how to set this in HW. */ - usb_status |= dwc->gadget.is_selfpowered; + usb_status |= dwc->gadget->is_selfpowered; if ((dwc->speed == DWC3_DSTS_SUPERSPEED) || (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) { @@ -450,7 +450,7 @@ static int dwc3_ep0_handle_device(struct dwc3 *dwc, wValue = le16_to_cpu(ctrl->wValue); wIndex = le16_to_cpu(ctrl->wIndex); - state = dwc->gadget.state; + state = dwc->gadget->state; switch (wValue) { case USB_DEVICE_REMOTE_WAKEUP: @@ -559,7 +559,7 @@ static int dwc3_ep0_handle_feature(struct dwc3 *dwc, static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) { - enum usb_device_state state = dwc->gadget.state; + enum usb_device_state state = dwc->gadget->state; u32 addr; u32 reg; @@ -580,9 +580,9 @@ static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) dwc3_writel(dwc->regs, DWC3_DCFG, reg); if (addr) - usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS); + usb_gadget_set_state(dwc->gadget, USB_STATE_ADDRESS); else - usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT); + usb_gadget_set_state(dwc->gadget, USB_STATE_DEFAULT); return 0; } @@ -592,14 +592,14 @@ static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) int ret; spin_unlock(&dwc->lock); - ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl); + ret = dwc->gadget_driver->setup(dwc->gadget, ctrl); spin_lock(&dwc->lock); return ret; } static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) { - enum usb_device_state state = dwc->gadget.state; + enum usb_device_state state = dwc->gadget->state; u32 cfg; int ret; u32 reg; @@ -622,7 +622,7 @@ static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) * to change the state on the next usb_ep_queue() */ if (ret == 0) - usb_gadget_set_state(&dwc->gadget, + usb_gadget_set_state(dwc->gadget, USB_STATE_CONFIGURED); /* @@ -641,7 +641,7 @@ static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) case USB_STATE_CONFIGURED: ret = dwc3_ep0_delegate_req(dwc, ctrl); if (!cfg && !ret) - usb_gadget_set_state(&dwc->gadget, + usb_gadget_set_state(dwc->gadget, USB_STATE_ADDRESS); break; default: @@ -697,7 +697,7 @@ static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req) static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl) { struct dwc3_ep *dep; - enum usb_device_state state = dwc->gadget.state; + enum usb_device_state state = dwc->gadget->state; u16 wLength; if (state == USB_STATE_DEFAULT) @@ -741,7 +741,7 @@ static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ct if (wIndex || wLength) return -EINVAL; - dwc->gadget.isoch_delay = wValue; + dwc->gadget->isoch_delay = wValue; return 0; } @@ -1102,7 +1102,7 @@ static void dwc3_ep0_xfernotready(struct dwc3 *dwc, */ if (!list_empty(&dep->pending_list)) { dwc->delayed_status = false; - usb_gadget_set_state(&dwc->gadget, + usb_gadget_set_state(dwc->gadget, USB_STATE_CONFIGURED); dwc3_ep0_do_control_status(dwc, event); } diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index e44bfc3b5096..adc8e22e30a2 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -290,7 +290,7 @@ int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd, * * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2 */ - if (dwc->gadget.speed <= USB_SPEED_HIGH) { + if (dwc->gadget->speed <= USB_SPEED_HIGH) { reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { saved_config |= DWC3_GUSB2PHYCFG_SUSPHY; @@ -422,7 +422,7 @@ static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep) */ if (dep->direction && !DWC3_VER_IS_PRIOR(DWC3, 260A) && - (dwc->gadget.speed >= USB_SPEED_SUPER)) + (dwc->gadget->speed >= USB_SPEED_SUPER)) cmd |= DWC3_DEPCMD_CLEARPENDIN; memset(¶ms, 0, sizeof(params)); @@ -562,7 +562,7 @@ static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action) | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); /* Burst size is only needed in SuperSpeed mode */ - if (dwc->gadget.speed >= USB_SPEED_SUPER) { + if (dwc->gadget->speed >= USB_SPEED_SUPER) { u32 burst = dep->endpoint.maxburst; params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); } @@ -947,7 +947,7 @@ static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb, unsigned no_interrupt, unsigned is_last) { struct dwc3 *dwc = dep->dwc; - struct usb_gadget *gadget = &dwc->gadget; + struct usb_gadget *gadget = dwc->gadget; enum usb_device_speed speed = gadget->speed; trb->size = DWC3_TRB_SIZE_LENGTH(length); @@ -1477,12 +1477,12 @@ static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep) if (!dwc->dis_start_transfer_quirk && (DWC3_VER_IS_PRIOR(DWC31, 170A) || DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) { - if (dwc->gadget.speed <= USB_SPEED_HIGH && dep->direction) + if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction) return dwc3_gadget_start_isoc_quirk(dep); } if (desc->bInterval <= 14 && - dwc->gadget.speed >= USB_SPEED_HIGH) { + dwc->gadget->speed >= USB_SPEED_HIGH) { u32 frame = __dwc3_gadget_get_frame(dwc); bool rollover = frame < (dep->frame_number & DWC3_FRNUMBER_MASK); @@ -2182,7 +2182,7 @@ static int dwc3_gadget_start(struct usb_gadget *g, spin_lock_irqsave(&dwc->lock, flags); if (dwc->gadget_driver) { dev_err(dwc->dev, "%s is already bound to %s\n", - dwc->gadget.name, + dwc->gadget->name, dwc->gadget_driver->driver.name); ret = -EBUSY; goto err1; @@ -2354,7 +2354,7 @@ static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep) dep->endpoint.maxburst = 1; dep->endpoint.ops = &dwc3_gadget_ep0_ops; if (!dep->direction) - dwc->gadget.ep0 = &dep->endpoint; + dwc->gadget->ep0 = &dep->endpoint; dep->endpoint.caps.type_control = true; @@ -2400,7 +2400,7 @@ static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) dep->endpoint.max_streams = 15; dep->endpoint.ops = &dwc3_gadget_ep_ops; list_add_tail(&dep->endpoint.ep_list, - &dwc->gadget.ep_list); + &dwc->gadget->ep_list); dep->endpoint.caps.type_iso = true; dep->endpoint.caps.type_bulk = true; dep->endpoint.caps.type_int = true; @@ -2449,7 +2449,7 @@ static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep) dep->endpoint.max_streams = 15; dep->endpoint.ops = &dwc3_gadget_ep_ops; list_add_tail(&dep->endpoint.ep_list, - &dwc->gadget.ep_list); + &dwc->gadget->ep_list); dep->endpoint.caps.type_iso = true; dep->endpoint.caps.type_bulk = true; dep->endpoint.caps.type_int = true; @@ -2510,7 +2510,7 @@ static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total) { u8 epnum; - INIT_LIST_HEAD(&dwc->gadget.ep_list); + INIT_LIST_HEAD(&dwc->gadget->ep_list); for (epnum = 0; epnum < total; epnum++) { int ret; @@ -2948,7 +2948,7 @@ static void dwc3_disconnect_gadget(struct dwc3 *dwc) { if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { spin_unlock(&dwc->lock); - dwc->gadget_driver->disconnect(&dwc->gadget); + dwc->gadget_driver->disconnect(dwc->gadget); spin_lock(&dwc->lock); } } @@ -2957,7 +2957,7 @@ static void dwc3_suspend_gadget(struct dwc3 *dwc) { if (dwc->gadget_driver && dwc->gadget_driver->suspend) { spin_unlock(&dwc->lock); - dwc->gadget_driver->suspend(&dwc->gadget); + dwc->gadget_driver->suspend(dwc->gadget); spin_lock(&dwc->lock); } } @@ -2966,7 +2966,7 @@ static void dwc3_resume_gadget(struct dwc3 *dwc) { if (dwc->gadget_driver && dwc->gadget_driver->resume) { spin_unlock(&dwc->lock); - dwc->gadget_driver->resume(&dwc->gadget); + dwc->gadget_driver->resume(dwc->gadget); spin_lock(&dwc->lock); } } @@ -2976,9 +2976,9 @@ static void dwc3_reset_gadget(struct dwc3 *dwc) if (!dwc->gadget_driver) return; - if (dwc->gadget.speed != USB_SPEED_UNKNOWN) { + if (dwc->gadget->speed != USB_SPEED_UNKNOWN) { spin_unlock(&dwc->lock); - usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver); + usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver); spin_lock(&dwc->lock); } } @@ -3079,9 +3079,9 @@ static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) dwc3_disconnect_gadget(dwc); - dwc->gadget.speed = USB_SPEED_UNKNOWN; + dwc->gadget->speed = USB_SPEED_UNKNOWN; dwc->setup_packet_pending = false; - usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED); + usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED); dwc->connected = false; } @@ -3160,8 +3160,8 @@ static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) switch (speed) { case DWC3_DSTS_SUPERSPEED_PLUS: dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); - dwc->gadget.ep0->maxpacket = 512; - dwc->gadget.speed = USB_SPEED_SUPER_PLUS; + dwc->gadget->ep0->maxpacket = 512; + dwc->gadget->speed = USB_SPEED_SUPER_PLUS; break; case DWC3_DSTS_SUPERSPEED: /* @@ -3181,27 +3181,27 @@ static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) dwc3_gadget_reset_interrupt(dwc); dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); - dwc->gadget.ep0->maxpacket = 512; - dwc->gadget.speed = USB_SPEED_SUPER; + dwc->gadget->ep0->maxpacket = 512; + dwc->gadget->speed = USB_SPEED_SUPER; break; case DWC3_DSTS_HIGHSPEED: dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); - dwc->gadget.ep0->maxpacket = 64; - dwc->gadget.speed = USB_SPEED_HIGH; + dwc->gadget->ep0->maxpacket = 64; + dwc->gadget->speed = USB_SPEED_HIGH; break; case DWC3_DSTS_FULLSPEED: dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); - dwc->gadget.ep0->maxpacket = 64; - dwc->gadget.speed = USB_SPEED_FULL; + dwc->gadget->ep0->maxpacket = 64; + dwc->gadget->speed = USB_SPEED_FULL; break; case DWC3_DSTS_LOWSPEED: dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8); - dwc->gadget.ep0->maxpacket = 8; - dwc->gadget.speed = USB_SPEED_LOW; + dwc->gadget->ep0->maxpacket = 8; + dwc->gadget->speed = USB_SPEED_LOW; break; } - dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket; + dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket; /* Enable USB2 LPM Capability */ @@ -3269,7 +3269,7 @@ static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc) if (dwc->gadget_driver && dwc->gadget_driver->resume) { spin_unlock(&dwc->lock); - dwc->gadget_driver->resume(&dwc->gadget); + dwc->gadget_driver->resume(dwc->gadget); spin_lock(&dwc->lock); } } @@ -3440,7 +3440,7 @@ static void dwc3_gadget_interrupt(struct dwc3 *dwc, * Ignore suspend event until the gadget enters into * USB_STATE_CONFIGURED state. */ - if (dwc->gadget.state >= USB_STATE_CONFIGURED) + if (dwc->gadget->state >= USB_STATE_CONFIGURED) dwc3_gadget_suspend_interrupt(dwc, event->event_info); } @@ -3615,6 +3615,13 @@ static int dwc3_gadget_get_irq(struct dwc3 *dwc) return irq; } +static void dwc_gadget_release(struct device *dev) +{ + struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev); + + kfree(gadget); +} + /** * dwc3_gadget_init - initializes gadget related registers * @dwc: pointer to our controller context structure @@ -3625,6 +3632,7 @@ int dwc3_gadget_init(struct dwc3 *dwc) { int ret; int irq; + struct device *dev; irq = dwc3_gadget_get_irq(dwc); if (irq < 0) { @@ -3657,12 +3665,21 @@ int dwc3_gadget_init(struct dwc3 *dwc) } init_completion(&dwc->ep0_in_setup); + dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL); + if (!dwc->gadget) { + ret = -ENOMEM; + goto err3; + } - dwc->gadget.ops = &dwc3_gadget_ops; - dwc->gadget.speed = USB_SPEED_UNKNOWN; - dwc->gadget.sg_supported = true; - dwc->gadget.name = "dwc3-gadget"; - dwc->gadget.lpm_capable = true; + + usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release); + dev = &dwc->gadget->dev; + dev->platform_data = dwc; + dwc->gadget->ops = &dwc3_gadget_ops; + dwc->gadget->speed = USB_SPEED_UNKNOWN; + dwc->gadget->sg_supported = true; + dwc->gadget->name = "dwc3-gadget"; + dwc->gadget->lpm_capable = true; /* * FIXME We might be setting max_speed to dev, "changing max_speed on rev %08x\n", dwc->revision); - dwc->gadget.max_speed = dwc->maximum_speed; + dwc->gadget->max_speed = dwc->maximum_speed; /* * REVISIT: Here we should clear all pending IRQs to be @@ -3694,21 +3711,22 @@ int dwc3_gadget_init(struct dwc3 *dwc) ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps); if (ret) - goto err3; + goto err4; - ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget); + ret = usb_add_gadget(dwc->gadget); if (ret) { - dev_err(dwc->dev, "failed to register udc\n"); - goto err4; + dev_err(dwc->dev, "failed to add gadget\n"); + goto err5; } - dwc3_gadget_set_speed(&dwc->gadget, dwc->maximum_speed); + dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed); return 0; -err4: +err5: dwc3_gadget_free_endpoints(dwc); - +err4: + usb_put_gadget(dwc->gadget); err3: dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, dwc->bounce_addr); @@ -3728,7 +3746,7 @@ int dwc3_gadget_init(struct dwc3 *dwc) void dwc3_gadget_exit(struct dwc3 *dwc) { - usb_del_gadget_udc(&dwc->gadget); + usb_del_gadget_udc(dwc->gadget); dwc3_gadget_free_endpoints(dwc); dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, dwc->bounce_addr); diff --git a/drivers/usb/dwc3/gadget.h b/drivers/usb/dwc3/gadget.h index bd85eb7fa9ef..6beb9263c2fb 100644 --- a/drivers/usb/dwc3/gadget.h +++ b/drivers/usb/dwc3/gadget.h @@ -17,7 +17,7 @@ struct dwc3; #define to_dwc3_ep(ep) (container_of(ep, struct dwc3_ep, endpoint)) -#define gadget_to_dwc(g) (container_of(g, struct dwc3, gadget)) +#define gadget_to_dwc(g) (dev_get_platdata(&g->dev)) /* DEPCFG parameter 1 */ #define DWC3_DEPCFG_INT_NUM(n) (((n) & 0x1f) << 0) From patchwork Fri Aug 21 02:55:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Chen X-Patchwork-Id: 11727469 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3FAAF109B for ; Fri, 21 Aug 2020 02:57:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2816F207DE for ; Fri, 21 Aug 2020 02:57:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="JPtYyj4S" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727073AbgHUC5f (ORCPT ); Thu, 20 Aug 2020 22:57:35 -0400 Received: from mail-eopbgr50073.outbound.protection.outlook.com ([40.107.5.73]:27655 "EHLO EUR03-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725852AbgHUC5d (ORCPT ); Thu, 20 Aug 2020 22:57:33 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hSAuI9IuNCmyWUqKys2SQE0BrIMa0FN1abkvoFtzx1CifZNKpMPCF6GXyAmLB/QjG45d+b/yELjFxSuV07pvbsJpJtjHnO2Hk4suDPW3466CzSc7I+Zs2FPoiMZz40Y/8knR/BegvN+BfxYxh9hn6o6slbVqda+ox5WOTuPkcMOHLeHjVjKThAfasEKWbEHYBp6rlJoTiW8hu5axXsHV7G3Bx96yejj+X510AVjI7/PMJ273TudWhtr12rbgoeAgo7CMZvy7wDK7AB6ks8tjNApCskUsYnNU/EMe3OgwHliixXl3ggd1DhWtDPChuLxOFhGJD9rnui6jOyYbKKKxhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ci2E9RhjzD16z5shhZe3379+Kb0uePAdK/CxEu5X2DU=; b=C2DxFUDTfAxXDs85pWR64Id50KqVFGXo5gPxo7a7+lZ8yqVVg0lJryLegroEpwuoprj42OSjIGeChR/nK3HypdzFlJ4RfYodZBP1rZ2duYVMu0eU32QtGSXZs0dwP4CHg5Hy2D72UVhdM3RbVenKL2XWpjWbIucZ1pZthKKiB9+YCwwY14XyVSfXdEl5wIcbrDB59fWye5ubIG28odJ4Trtaot2FeP3NyvpwQ3V4dO6uM1fzVXwAEPDecvC4BK7/twrW1hYQ2aNpRt+koJA2fDvu3vYvDGC2WkYnpM8tyvT1fIJLrcQZE+nzipBqXy5fB8KL7Nkz946bxOuovW2JgQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ci2E9RhjzD16z5shhZe3379+Kb0uePAdK/CxEu5X2DU=; b=JPtYyj4SZIxQc0BAGrLGZlJnZu+aDswVAtqY4+vTdfqSVdZRGYQ6y3c6WBXhsaPlbh4Px8cNx69HNGK6PgvBXjTtFre1Q9q+qR6j9EE6GMN3uJh+v+4B0bQaJkKhqIQcQBguau7Ya9/wrdPNTzm9DA1S30W9rN9LKeg6yYkMKq8= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=nxp.com; Received: from DB8PR04MB7162.eurprd04.prod.outlook.com (2603:10a6:10:12c::13) by DB7PR04MB5577.eurprd04.prod.outlook.com (2603:10a6:10:8a::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.26; Fri, 21 Aug 2020 02:57:25 +0000 Received: from DB8PR04MB7162.eurprd04.prod.outlook.com ([fe80::acee:9763:7898:84a2]) by DB8PR04MB7162.eurprd04.prod.outlook.com ([fe80::acee:9763:7898:84a2%9]) with mapi id 15.20.3283.027; Fri, 21 Aug 2020 02:57:25 +0000 From: Peter Chen To: balbi@kernel.org Cc: linux-usb@vger.kernel.org, linux-imx@nxp.com, gregkh@linuxfoundation.org, stern@rowland.harvard.edu, Peter Chen Subject: [PATCH v4 6/6] Revert "usb: udc: allow adding and removing the same gadget device" Date: Fri, 21 Aug 2020 10:55:49 +0800 Message-Id: <20200821025549.4591-7-peter.chen@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200821025549.4591-1-peter.chen@nxp.com> References: <20200821025549.4591-1-peter.chen@nxp.com> X-ClientProxiedBy: SG2PR01CA0132.apcprd01.prod.exchangelabs.com (2603:1096:4:40::36) To DB8PR04MB7162.eurprd04.prod.outlook.com (2603:10a6:10:12c::13) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from b29397-desktop.ap.freescale.net (119.31.174.67) by SG2PR01CA0132.apcprd01.prod.exchangelabs.com (2603:1096:4:40::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3305.24 via Frontend Transport; Fri, 21 Aug 2020 02:57:23 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [119.31.174.67] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: e193ab8e-dd45-49c3-556b-08d8457dee9c X-MS-TrafficTypeDiagnostic: DB7PR04MB5577: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:196; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5euJVqLC3ugQCk8ES/3bFNcWPTM4KCa3A8OFyLx9bCTYgQANc8El/45lzEmgja6OSL7qb6S7blEnJrcCXb3wR+JuwOK4J0eE//IWvtmwrRtb1rdYK1B4kd2kVr39HQYb3vcLWWk4PjulUw/kT8ZhR6QWIgGtfYjEkeu3HAbFBZpW3vaVwdWgOBhnhXQUdIHbvEOgFJTEgFd6sTSPlY+2LgzrkKPjRJBmhMr6sDLFbb6oRWM9f8+k4Wx2Ro0foYm2QnUPmZ9GLCdqdnMhQoPFx/PSOFXxv5yAxvSBJVb8dtghndPkVMIgZjngUJbfpnHeBypK0jFFSqhtOwHBzXvxTw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DB8PR04MB7162.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(376002)(136003)(366004)(346002)(39860400002)(52116002)(66946007)(2906002)(8676002)(6486002)(4326008)(6506007)(478600001)(316002)(6666004)(44832011)(36756003)(956004)(26005)(66476007)(66556008)(4744005)(2616005)(8936002)(1076003)(6512007)(5660300002)(6916009)(186003)(83380400001)(86362001)(16526019);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: Wm6Dt4WRbeQfXQmcw/wbdFrRZEPOUgEV+SUV7kAriwRmWf1QcC5iCs6mBC8eAIwsX6PF02VsN/d6/WDexQKzs76x4/BmxCIRDgIKHVcoc478MWcZoOKtzGmXq1igNqFt0ixn2A1XXo7NPArPRDQUpUxYRQszwrFm/9NNeoTEnQ/S1kCfVEDAO9iBUwIAPqP77b82LySCdUhKrHNddM29zjMIbBg8V6giObaZWI3MFMe/09w/pgtXVuz2qjvQzwkCG7OyWGDeN7/TyWexWeMJtE07cizKNITGtEE1mxCMPYDeuwGzsLM24D+jl3lqrRuRBG8rQ6eHnI4dyYNbPs9t9NsGLATWdLA2NYvkWAm1CyiphWrJI2GCaFY9AjdClTwvfT7HDUFZcpUVpKu8XxgkX68CxukgJhSOL9E8V6O/UsPUv4tYoEu0lxxdrXZBvkaqdl/0YnFG7SO+yajjKkQr+LFKTs7P13h48kWvQV7Ck+jYXukKQo2L/a/q2adLoI84aW242bAwtCyuNUKt6HG9aF9OTAdzG3inJs2Ylta2I6QVeNWnuZeAciUXFu0320pouayphX2+pWllMuaZDy6l3fr/14p1axHUP2sn8dlK6RdB2BX0FJXDVebNKcCDIOvwDeBLIY7FI5WnL+xPAyckLg== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: e193ab8e-dd45-49c3-556b-08d8457dee9c X-MS-Exchange-CrossTenant-AuthSource: DB8PR04MB7162.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Aug 2020 02:57:25.2721 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 4Nx2owVMQK1Bx0MP85AmB54T7vIC8bfFFT8sn+iTJLOf/srWsBwY5z1SwWWaY2S66ijVoLA2e0loKkj11rS89g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB7PR04MB5577 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org We have already allocated gadget structure dynamically at UDC (dwc3) driver, so commit fac323471df6 ("usb: udc: allow adding and removing the same gadget device")could be reverted. Cc: Greg Kroah-Hartman Cc: Alan Stern Reviewed-by: Greg Kroah-Hartman Reviewed-by: Alan Stern Signed-off-by: Peter Chen --- drivers/usb/gadget/udc/core.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c index 2b6770d9fb3f..bf1b0efcfaac 100644 --- a/drivers/usb/gadget/udc/core.c +++ b/drivers/usb/gadget/udc/core.c @@ -1389,7 +1389,6 @@ void usb_del_gadget_udc(struct usb_gadget *gadget) { usb_del_gadget(gadget); usb_put_gadget(gadget); - memset(&gadget->dev, 0x00, sizeof(gadget->dev)); } EXPORT_SYMBOL_GPL(usb_del_gadget_udc);