Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4A206C001DB for ; Fri, 4 Aug 2023 15:41:42 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 57E3886933; Fri, 4 Aug 2023 17:41:27 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1691163687; bh=wDgpu86ZayewV6McMk2Y7YKLNA+qOdcbEuYxKJ/aMpI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=hJxA3zrEwp5TauXY0ObThWlcif60sZYG1VvX8lGe3FO4baJxrzWpfq41rIlNYWzC4 bi/gEHr45MBBYAlmaJ0+4wpm0oYKfpWTHifDwSGyP+Mw4VRtKGYDkcd27we46EOKE8 hNDeW0LMIfSh1CeWsOgYFJBl5rXGattlnpcRnnD1VjRXeHEnyWXUbPqcGXp+ocDwDe kdgnpzrFUip2LgCCTBHjRYxpElXnSiXL4eIg2cR4Hdx0C0MEE2OKCSnBSERhZSCJef axH2yWlD9Jzz5k8nM/k7U0pY4HtSGFDp1lpNG0mmbwrVhP8zLmDgXFY6fsPHn1CYtN BjEbzH/3Os1Fw== Received: from tr.lan (ip-86-49-120-218.bb.vodafone.cz [86.49.120.218]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: marex@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id 90B0486916; Fri, 4 Aug 2023 17:41:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1691163684; bh=wDgpu86ZayewV6McMk2Y7YKLNA+qOdcbEuYxKJ/aMpI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V33OCri+ubLgmmm5O+h5DAy9OsZFMvj99gwy/9K5XiOOUmHq1Wlh9s9BXZGvtsZo8 rVxwVL2HrxUgxUc1VY4J2YnqgI32BPgA2p/67WPjMadGTBpoQOpRGK0n5kyQiBxrhm pYsmB1P9WGxd1LbBX/hLvM54FQvHQWgOMQM+G9Vg61AK4QgRK1YnMAISRy1lrfVoIz Ah0oqYHpyNP22UxuAynPHAHiowtK70NZVR6dVJ/W+7sL9Xw0cZiAZ6ysVwoAThfVkf +Dy1X3mZahkYUOlel31aqW2tHLfQkL+OUtmIhVBtrcOec6GvpgDonkF//sBdIQLAfw i3yQBXytNAp1g== From: Marek Vasut To: u-boot@lists.denx.de Cc: Marek Vasut , Kevin Hilman , Lukasz Majewski , Miquel Raynal , Simon Glass Subject: [PATCH v5 3/3] usb: gadget: ether: Handle gadget driver registration in probe and remove Date: Fri, 4 Aug 2023 17:41:11 +0200 Message-Id: <20230804154111.97042-3-marex@denx.de> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230804154111.97042-1-marex@denx.de> References: <20230804154111.97042-1-marex@denx.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Move the ethernet gadget driver registration and removal from ethernet bind and unbind callbacks into driver DM probe and remove callbacks. This way, when the driver is bound, which is triggered deliberately using 'bind' command, the USB ethernet gadget driver is instantiated and bound to the matching UDC. In reverse, when the driver is unbound, which is again triggered deliberately using 'unbind' command, the USB ethernet gadget driver instance is removed. Effectively, this now behaves like running either 'ums' or 'dfu' or any other commands utilizing USB gadget functionality. This also drops use of usb_gadget_release() and moves the use of usb_gadget_initialize() into usb_ether_init() used only by legacy platforms that do not use 'bind' command properly yet. Those have no place in drivers. Signed-off-by: Marek Vasut --- NOTE: This must be thoroughly tested. --- Cc: Kevin Hilman Cc: Lukasz Majewski Cc: Miquel Raynal Cc: Simon Glass --- V2: Fix up return value handling in probe V3: Reinstate usb_gadget_initialize() in usb_ether_init() to retain this obscure workaround for legacy platforms that fail to use bind command V4: Call usb_gadget_release() in unbind callback to further help legacy platforms V5: No change --- drivers/usb/gadget/ether.c | 97 ++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 45 deletions(-) diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 2040b5b5081..5ff06d3814b 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -2281,49 +2281,8 @@ static int usb_eth_start(struct udevice *udev) struct eth_dev *dev = &priv->ethdev; struct usb_gadget *gadget; unsigned long ts; - int ret; unsigned long timeout = USB_CONNECT_TIMEOUT; - ret = usb_gadget_initialize(0); - if (ret) - return ret; - - /* Configure default mac-addresses for the USB ethernet device */ -#ifdef CONFIG_USBNET_DEV_ADDR - strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr)); -#endif -#ifdef CONFIG_USBNET_HOST_ADDR - strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr)); -#endif - /* Check if the user overruled the MAC addresses */ - if (env_get("usbnet_devaddr")) - strlcpy(dev_addr, env_get("usbnet_devaddr"), - sizeof(dev_addr)); - - if (env_get("usbnet_hostaddr")) - strlcpy(host_addr, env_get("usbnet_hostaddr"), - sizeof(host_addr)); - - if (!is_eth_addr_valid(dev_addr)) { - pr_err("Need valid 'usbnet_devaddr' to be set"); - goto fail; - } - if (!is_eth_addr_valid(host_addr)) { - pr_err("Need valid 'usbnet_hostaddr' to be set"); - goto fail; - } - - priv->eth_driver.speed = DEVSPEED; - priv->eth_driver.bind = eth_bind; - priv->eth_driver.unbind = eth_unbind; - priv->eth_driver.setup = eth_setup; - priv->eth_driver.reset = eth_disconnect; - priv->eth_driver.disconnect = eth_disconnect; - priv->eth_driver.suspend = eth_suspend; - priv->eth_driver.resume = eth_resume; - if (usb_gadget_register_driver(&priv->eth_driver) < 0) - goto fail; - dev->network_started = 0; packet_received = 0; @@ -2450,9 +2409,6 @@ static void usb_eth_stop(struct udevice *udev) usb_gadget_handle_interrupts(0); dev->network_started = 0; } - - usb_gadget_unregister_driver(&priv->eth_driver); - usb_gadget_release(0); } static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp) @@ -2511,7 +2467,7 @@ int usb_ether_init(void) return ret; } - return 0; + return usb_gadget_initialize(0); } static int usb_eth_probe(struct udevice *dev) @@ -2525,6 +2481,55 @@ static int usb_eth_probe(struct udevice *dev) get_ether_addr(CONFIG_USBNET_DEV_ADDR, pdata->enetaddr); eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr); + /* Configure default mac-addresses for the USB ethernet device */ +#ifdef CONFIG_USBNET_DEV_ADDR + strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr)); +#endif +#ifdef CONFIG_USBNET_HOST_ADDR + strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr)); +#endif + /* Check if the user overruled the MAC addresses */ + if (env_get("usbnet_devaddr")) + strlcpy(dev_addr, env_get("usbnet_devaddr"), + sizeof(dev_addr)); + + if (env_get("usbnet_hostaddr")) + strlcpy(host_addr, env_get("usbnet_hostaddr"), + sizeof(host_addr)); + + if (!is_eth_addr_valid(dev_addr)) { + pr_err("Need valid 'usbnet_devaddr' to be set"); + return -EINVAL; + } + if (!is_eth_addr_valid(host_addr)) { + pr_err("Need valid 'usbnet_hostaddr' to be set"); + return -EINVAL; + } + + priv->eth_driver.speed = DEVSPEED; + priv->eth_driver.bind = eth_bind; + priv->eth_driver.unbind = eth_unbind; + priv->eth_driver.setup = eth_setup; + priv->eth_driver.reset = eth_disconnect; + priv->eth_driver.disconnect = eth_disconnect; + priv->eth_driver.suspend = eth_suspend; + priv->eth_driver.resume = eth_resume; + return usb_gadget_register_driver(&priv->eth_driver); +} + +static int usb_eth_remove(struct udevice *dev) +{ + struct ether_priv *priv = dev_get_priv(dev); + + usb_gadget_unregister_driver(&priv->eth_driver); + + return 0; +} + +static int usb_eth_unbind(struct udevice *dev) +{ + usb_gadget_release(0); + return 0; } @@ -2532,6 +2537,8 @@ U_BOOT_DRIVER(eth_usb) = { .name = "usb_ether", .id = UCLASS_ETH, .probe = usb_eth_probe, + .remove = usb_eth_remove, + .unbind = usb_eth_unbind, .ops = &usb_eth_ops, .priv_auto = sizeof(struct ether_priv), .plat_auto = sizeof(struct eth_pdata), -- 2.40.1