Re: [U-Boot] [PATCH v2 3/5] cmd: Add bind/unbind commands to bind a device to a driver from the command line

2018-06-21 Thread Michal Simek
On 18.6.2018 15:56, Jean-Jacques Hiblot wrote:
> 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@4838/usb@4839 usb_ether
> unbind /ocp/omap_dwc3@4838/usb@4839
> 
> Signed-off-by: Jean-Jacques Hiblot 
> 
> ---
> 
> 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 000..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, );
> + 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(), );
> + 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, );
> + 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 

Re: [U-Boot] [PATCH v2 3/5] cmd: Add bind/unbind commands to bind a device to a driver from the command line

2018-06-20 Thread Simon Glass
Hi Jean-Jacques,

On 18 June 2018 at 07:56, Jean-Jacques Hiblot  wrote:
> 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@4838/usb@4839 usb_ether
> unbind /ocp/omap_dwc3@4838/usb@4839
>
> Signed-off-by: Jean-Jacques Hiblot 
>
> ---
>
> 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

This looks good to me, with a few comments below. I think it is great
to get this sort of functionality in U-Boot.

Please can you add a test which calls issues a few of these commands?
You might find test_shell_basics.py useful as a starting point.

>
> 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 000..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, );
> +   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(), );
> +   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, );
> +   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,
> +   

Re: [U-Boot] [PATCH v2 3/5] cmd: Add bind/unbind commands to bind a device to a driver from the command line

2018-06-19 Thread Joe Hershberger
On Mon, Jun 18, 2018 at 8:56 AM, Jean-Jacques Hiblot  wrote:
> 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@4838/usb@4839 usb_ether
> unbind /ocp/omap_dwc3@4838/usb@4839
>
> Signed-off-by: Jean-Jacques Hiblot 
>

Looks great!

Reviewed-by: Joe Hershberger 
___
U-Boot mailing list
U-Boot@lists.denx.de
https://lists.denx.de/listinfo/u-boot


[U-Boot] [PATCH v2 3/5] cmd: Add bind/unbind commands to bind a device to a driver from the command line

2018-06-18 Thread Jean-Jacques Hiblot
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@4838/usb@4839 usb_ether
unbind /ocp/omap_dwc3@4838/usb@4839

Signed-off-by: Jean-Jacques Hiblot 

---

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 000..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, );
+   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(), );
+   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, );
+   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, );