From patchwork Mon Jun 18 13:56:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Jacques Hiblot X-Patchwork-Id: 930871 X-Patchwork-Delegate: lukma@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=quarantine dis=none) header.from=ti.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ti.com header.i=@ti.com header.b="dMIz+DZV"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 418Xkd70FWz9s2t for ; Mon, 18 Jun 2018 23:58:49 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 97913C21E50; Mon, 18 Jun 2018 13:58:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 3E059C21E90; Mon, 18 Jun 2018 13:57:22 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id CA99EC21E1E; Mon, 18 Jun 2018 13:57:16 +0000 (UTC) Received: from lelnx194.ext.ti.com (lelnx194.ext.ti.com [198.47.27.80]) by lists.denx.de (Postfix) with ESMTPS id 1BBD9C21E1E for ; Mon, 18 Jun 2018 13:57:12 +0000 (UTC) Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by lelnx194.ext.ti.com (8.15.1/8.15.1) with ESMTP id w5IDv2lC013228; Mon, 18 Jun 2018 08:57:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1529330222; bh=WwKHtQbK42IEOxgJ3b6g3fC6UYN+7Q3OSufm7hLFVQQ=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=dMIz+DZVv7kfNJb0m4pyp03LtRYszxW91ceoXpP7pSxlz8EsGAX8xh6P75YtB0nJb zSqOlGQuXDyrxq84RZ4bUaJu6l+Dcp1pfMC2IDJWK/OS3eTOwHVrwZuAocGjZp5DNS izgqftFggATTirCI0x7THjpgcguXALgzXFTOfKWY= Received: from DFLE103.ent.ti.com (dfle103.ent.ti.com [10.64.6.24]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id w5IDv2Z4012444; Mon, 18 Jun 2018 08:57:02 -0500 Received: from DFLE105.ent.ti.com (10.64.6.26) by DFLE103.ent.ti.com (10.64.6.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Mon, 18 Jun 2018 08:57:02 -0500 Received: from dlep33.itg.ti.com (157.170.170.75) by DFLE105.ent.ti.com (10.64.6.26) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.1466.3 via Frontend Transport; Mon, 18 Jun 2018 08:57:02 -0500 Received: from localhost (ileax41-snat.itg.ti.com [10.172.224.153]) by dlep33.itg.ti.com (8.14.3/8.13.8) with ESMTP id w5IDv1x4001105; Mon, 18 Jun 2018 08:57:01 -0500 From: Jean-Jacques Hiblot To: Date: Mon, 18 Jun 2018 15:56:50 +0200 Message-ID: <1529330212-9022-4-git-send-email-jjhiblot@ti.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1529330212-9022-1-git-send-email-jjhiblot@ti.com> References: <1529330212-9022-1-git-send-email-jjhiblot@ti.com> MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Cc: trini@konsulko.com, u-boot@lists.denx.de, Joe Hershberger , Maxime Ripard , sr@denx.de, Jagan Teki , Michalis Pappas Subject: [U-Boot] [PATCH v2 3/5] cmd: Add bind/unbind commands to bind a device to a driver from the command line X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 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" In some cases it can be useful to be able to bind a device to a driver from the command line. The obvious example is for versatile devices such as USB gadget. Another use case is when the devices are not yet ready at startup and require some setup before the drivers are bound (ex: FPGA which bitsream is fetched from a mass storage or ethernet) usage example: bind usb_dev_generic 0 usb_ether unbind usb_dev_generic 0 usb_ether or unbind eth 1 bind /ocp/omap_dwc3@48380000/usb@48390000 usb_ether unbind /ocp/omap_dwc3@48380000/usb@48390000 Signed-off-by: Jean-Jacques Hiblot Reviewed-by: Joe Hershberger --- Changes in v2: - Make the bind/unbind command generic, not specific to usb device. - Update the API to be able to bind/unbind based on DTS node path - Add a Kconfig option to select the bind/unbind commands cmd/Kconfig | 9 +++ cmd/Makefile | 1 + cmd/bind.c | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 266 insertions(+) create mode 100644 cmd/bind.c diff --git a/cmd/Kconfig b/cmd/Kconfig index 1eb55e5..d6bbfba 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -607,6 +607,15 @@ config CMD_ADC Shows ADC device info and permit printing one-shot analog converted data from a named Analog to Digital Converter. +config CMD_BIND + bool "bind/unbind - Bind or unbind a device to/from a driver" + depends on DM + help + Bind or unbind a device to/from a driver from the command line. + This is useful in situations where a device may be handled by several + drivers. For example, this can be used to bind a UDC to the usb ether + gadget driver from the command line. + config CMD_CLK bool "clk - Show clock frequencies" help diff --git a/cmd/Makefile b/cmd/Makefile index e0088df..b12aca3 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -19,6 +19,7 @@ obj-$(CONFIG_SOURCE) += source.o obj-$(CONFIG_CMD_SOURCE) += source.o obj-$(CONFIG_CMD_BDI) += bdinfo.o obj-$(CONFIG_CMD_BEDBUG) += bedbug.o +obj-$(CONFIG_CMD_BIND) += bind.o obj-$(CONFIG_CMD_BINOP) += binop.o obj-$(CONFIG_CMD_BLOCK_CACHE) += blkcache.o obj-$(CONFIG_CMD_BMP) += bmp.o diff --git a/cmd/bind.c b/cmd/bind.c new file mode 100644 index 0000000..60d84d6 --- /dev/null +++ b/cmd/bind.c @@ -0,0 +1,256 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2018 JJ Hiblot + */ + +#include +#include +#include +#include +#include + +static int bind_by_class_index(const char *uclass, int index, + const char *drv_name) +{ + static enum uclass_id uclass_id; + struct udevice *dev; + struct udevice *parent; + int ret; + struct driver *drv; + + drv = lists_driver_lookup_name(drv_name); + if (!drv) { + printf("Cannot find driver '%s'\n", drv_name); + return -ENOENT; + } + + uclass_id = uclass_get_by_name(uclass); + if (uclass_id == UCLASS_INVALID) { + printf("%s is not a valid uclass\n", uclass); + return -EINVAL; + } + + ret = uclass_find_device(uclass_id, index, &parent); + if (!parent || ret) { + printf("Cannot find device %d of class %s\n", index, uclass); + return ret; + } + + ret = device_bind_with_driver_data(parent, drv, drv->name, 0, + ofnode_null(), &dev); + if (!dev || ret) { + printf("Unable to bind. err:%d\n", ret); + return ret; + } + + return 0; +} + +static int unbind_by_class_index(const char *uclass, int index) +{ + static enum uclass_id uclass_id; + struct udevice *dev; + int ret; + + uclass_id = uclass_get_by_name(uclass); + if (uclass_id == UCLASS_INVALID) { + printf("%s is not a valid uclass\n", uclass); + return -EINVAL; + } + + ret = uclass_find_device(uclass_id, index, &dev); + if (!dev || ret) { + printf("Cannot find device %d of class %s\n", index, uclass); + return ret; + } + + ret = device_unbind(dev); + if (ret) { + printf("Unable to unbind. err:%d\n", ret); + return ret; + } + + return 0; +} + +static int unbind_child_by_class_index(const char *uclass, int index, + const char *drv_name) +{ + static enum uclass_id uclass_id; + struct udevice *parent; + struct udevice *dev, *n; + int ret; + struct driver *drv; + + drv = lists_driver_lookup_name(drv_name); + if (!drv) { + printf("Cannot find driver '%s'\n", drv_name); + return -ENOENT; + } + + uclass_id = uclass_get_by_name(uclass); + if (uclass_id == UCLASS_INVALID) { + printf("%s is not a valid uclass\n", uclass); + return -EINVAL; + } + + ret = uclass_find_device(uclass_id, index, &parent); + if (!parent || ret) { + printf("Cannot find device %d of class %s\n", index, uclass); + return ret; + } + + list_for_each_entry_safe(dev, n, &parent->child_head, sibling_node) { + int rc; + + if (dev->driver != drv) + continue; + + rc = device_unbind(dev); + if (rc && !ret) { + printf("failed to unbind %s/%s\n", dev->name, + drv->name); + ret = rc; + } + } + + return ret; +} + +static int bind_by_node_path(const char *path, const char *drv_name) +{ + struct udevice *dev; + struct udevice *parent = NULL; + int ret; + int id; + ofnode ofnode; + struct driver *drv; + + drv = lists_driver_lookup_name(drv_name); + if (!drv) { + printf("%s is not a valid driver name\n", drv_name); + return -ENOENT; + } + + ofnode = ofnode_path(path); + if (!ofnode_valid(ofnode)) { + printf("%s is not a valid node path\n", path); + return -EINVAL; + } + + while (ofnode_valid(ofnode)) { + for (id = 0; id < UCLASS_COUNT; id++) { + if (!uclass_find_device_by_ofnode(id, ofnode, &parent)) + break; + } + if (parent) + break; + ofnode = ofnode_get_parent(ofnode); + } + + if (!parent) { + printf("Cannot find a parent device for node path %s\n", path); + return -ENODEV; + } + + ret = device_bind_with_driver_data(parent, drv, drv->name, 0, + ofnode_path(path), &dev); + if (!dev || ret) { + printf("Unable to bind. err:%d\n", ret); + return ret; + } + + return 0; +} + +static int unbind_by_node_path(const char *path) +{ + struct udevice *dev; + int ret; + int id; + ofnode ofnode; + + ofnode = ofnode_path(path); + if (!ofnode_valid(ofnode)) { + printf("%s is not a valid node path\n", path); + return -EINVAL; + } + + for (id = 0; id < UCLASS_COUNT; id++) { + if (uclass_find_device_by_ofnode(id, ofnode, &dev) == 0) + break; + } + + if (!dev) { + printf("Cannot find a device with path %s\n", path); + return -ENODEV; + } + + ret = device_unbind(dev); + if (ret) { + printf("Unable to unbind. err:%d\n", ret); + return ret; + } + + return 0; +} + +static int do_bind_unbind(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + int ret; + bool bind; + bool by_node; + + if (argc < 2) + return CMD_RET_USAGE; + + bind = (argv[0][0] == 'b'); + by_node = (argv[1][0] == '/'); + + if (by_node && bind) { + if (argc != 3) + return CMD_RET_USAGE; + ret = bind_by_node_path(argv[1], argv[2]); + } else if (by_node && !bind) { + if (argc != 2) + return CMD_RET_USAGE; + ret = unbind_by_node_path(argv[1]); + } else if (!by_node && bind) { + int index = (argc > 2) ? simple_strtoul(argv[2], NULL, 10) : 0; + + if (argc != 4) + return CMD_RET_USAGE; + ret = bind_by_class_index(argv[1], index, argv[3]); + } else if (!by_node && !bind) { + int index = (argc > 2) ? simple_strtoul(argv[2], NULL, 10) : 0; + + if (argc == 3) + ret = unbind_by_class_index(argv[1], index); + else if (argc == 4) + ret = unbind_child_by_class_index(argv[1], index, + argv[3]); + else + return CMD_RET_USAGE; + } + + if (ret) + return CMD_RET_FAILURE; + else + return CMD_RET_SUCCESS; +} + +U_BOOT_CMD( + bind, 4, 0, do_bind_unbind, + "Bind a device to a driver", + " \n" + "bind \n" +); + +U_BOOT_CMD( + unbind, 4, 0, do_bind_unbind, + "Unbind a device from a driver", + "\n" + "unbind \n" + "unbind \n" +);