[PATCH v2 01/17] dm: usb: udc: Factor out plain udevice handler functions

Pull the functionality of UDC uclass that operates on plain udevice and does not use this dev_array array into separate functions and expose those functions, so that as much code as possible can be switched over to these functions and the dev_array can be dropped.
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: Add RB from Mattijs --- drivers/usb/gadget/udc/Makefile | 2 +- drivers/usb/gadget/udc/udc-uclass.c | 57 +++++++++++++++++++++++++---- include/linux/usb/gadget.h | 17 +++++++++ 3 files changed, 68 insertions(+), 8 deletions(-)
diff --git a/drivers/usb/gadget/udc/Makefile b/drivers/usb/gadget/udc/Makefile index 95dbf0c82ee..467c566f6d3 100644 --- a/drivers/usb/gadget/udc/Makefile +++ b/drivers/usb/gadget/udc/Makefile @@ -7,4 +7,4 @@ obj-$(CONFIG_USB_DWC3_GADGET) += udc-core.o endif
obj-$(CONFIG_$(SPL_)DM_USB_GADGET) += udc-core.o -obj-$(CONFIG_$(SPL_)DM) += udc-uclass.o +obj-y += udc-uclass.o diff --git a/drivers/usb/gadget/udc/udc-uclass.c b/drivers/usb/gadget/udc/udc-uclass.c index de8861829c7..b4271b4be9f 100644 --- a/drivers/usb/gadget/udc/udc-uclass.c +++ b/drivers/usb/gadget/udc/udc-uclass.c @@ -14,6 +14,37 @@ #if CONFIG_IS_ENABLED(DM_USB_GADGET) #define MAX_UDC_DEVICES 4 static struct udevice *dev_array[MAX_UDC_DEVICES]; + +int udc_device_get_by_index(int index, struct udevice **udev) +{ + struct udevice *dev = NULL; + int ret; + + ret = uclass_get_device_by_seq(UCLASS_USB_GADGET_GENERIC, index, &dev); + if (!ret && dev) { + *udev = dev; + return 0; + } + + ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, index, &dev); + if (!ret && dev) { + *udev = dev; + return 0; + } + + pr_err("No USB device found\n"); + return -ENODEV; +} + +int udc_device_put(struct udevice *udev) +{ +#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) + return device_remove(udev, DM_REMOVE_NORMAL); +#else + return -ENOSYS; +#endif +} + int usb_gadget_initialize(int index) { int ret; @@ -23,13 +54,10 @@ int usb_gadget_initialize(int index) return -EINVAL; if (dev_array[index]) return 0; - ret = uclass_get_device_by_seq(UCLASS_USB_GADGET_GENERIC, index, &dev); + ret = udc_device_get_by_index(index, &dev); if (!dev || ret) { - ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, index, &dev); - if (!dev || ret) { - pr_err("No USB device found\n"); - return -ENODEV; - } + pr_err("No USB device found\n"); + return -ENODEV; } dev_array[index] = dev; return 0; @@ -42,7 +70,7 @@ int usb_gadget_release(int index) if (index < 0 || index >= ARRAY_SIZE(dev_array)) return -EINVAL;
- ret = device_remove(dev_array[index], DM_REMOVE_NORMAL); + ret = device_remove(dev_array[index]); if (!ret) dev_array[index] = NULL; return ret; @@ -57,10 +85,25 @@ int usb_gadget_handle_interrupts(int index) return -EINVAL; return dm_usb_gadget_handle_interrupts(dev_array[index]); } +#else +/* Backwards hardware compatibility -- switch to DM_USB_GADGET */ +static int legacy_index; +int udc_device_get_by_index(int index, struct udevice **udev) +{ + legacy_index = index; + return board_usb_init(index, USB_INIT_DEVICE); +} + +int udc_device_put(struct udevice *udev) +{ + return board_usb_cleanup(legacy_index, USB_INIT_DEVICE); +} #endif
+#if CONFIG_IS_ENABLED(DM) UCLASS_DRIVER(usb_gadget_generic) = { .id = UCLASS_USB_GADGET_GENERIC, .name = "usb", .flags = DM_UC_FLAG_SEQ_ALIAS, }; +#endif diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h index 2f694fc25c1..5e9a6513d5b 100644 --- a/include/linux/usb/gadget.h +++ b/include/linux/usb/gadget.h @@ -1006,6 +1006,23 @@ extern void usb_ep_autoconfig_reset(struct usb_gadget *);
extern int usb_gadget_handle_interrupts(int index);
+/** + * udc_device_get_by_index() - Get UDC udevice by index + * @index: UDC device index + * @udev: UDC udevice matching the index (if found) + * + * Return: 0 if Ok, -ve on error + */ +int udc_device_get_by_index(int index, struct udevice **udev); + +/** + * udc_device_put() - Put UDC udevice + * @udev: UDC udevice + * + * Return: 0 if Ok, -ve on error + */ +int udc_device_put(struct udevice *udev); + #if CONFIG_IS_ENABLED(DM_USB_GADGET) int usb_gadget_initialize(int index); int usb_gadget_release(int index);

Remove local usb_gadget_register_driver()/usb_gadget_unregister_driver() implementation which is implemented in udc-core.c instead if DM_USB_GADGET is enabled. Add no-op dm_usb_gadget_handle_interrupts() implementation.
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Reviewed-by: Simon Glass sjg@chromium.org Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: Add RB from Mattijs, Simon --- drivers/usb/host/usb-sandbox.c | 7 +++++++ 1 file changed, 7 insertions(+)
diff --git a/drivers/usb/host/usb-sandbox.c b/drivers/usb/host/usb-sandbox.c index d1103dcb2e9..582f72d00c1 100644 --- a/drivers/usb/host/usb-sandbox.c +++ b/drivers/usb/host/usb-sandbox.c @@ -124,6 +124,12 @@ static int sandbox_submit_int(struct udevice *bus, struct usb_device *udev, return ret; }
+#if CONFIG_IS_ENABLED(DM_USB_GADGET) +int dm_usb_gadget_handle_interrupts(struct udevice *dev) +{ + return 0; +} +#else int usb_gadget_handle_interrupts(int index) { return 0; @@ -144,6 +150,7 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
return 0; } +#endif
static int sandbox_alloc_device(struct udevice *dev, struct usb_device *udev) {

Switch sandbox to DM_USB_GADGET, DM is the future.
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Reviewed-by: Simon Glass sjg@chromium.org Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: Add RB from Mattijs, Simon --- configs/sandbox64_defconfig | 1 + configs/sandbox_defconfig | 1 + configs/sandbox_flattree_defconfig | 1 + configs/sandbox_noinst_defconfig | 1 + configs/sandbox_spl_defconfig | 1 + configs/sandbox_vpl_defconfig | 1 + 6 files changed, 6 insertions(+)
diff --git a/configs/sandbox64_defconfig b/configs/sandbox64_defconfig index 55a01b7eb9d..1104b86ff7c 100644 --- a/configs/sandbox64_defconfig +++ b/configs/sandbox64_defconfig @@ -236,6 +236,7 @@ CONFIG_TIMER=y CONFIG_TIMER_EARLY=y CONFIG_SANDBOX_TIMER=y CONFIG_USB=y +CONFIG_DM_USB_GADGET=y CONFIG_USB_EMUL=y CONFIG_USB_KEYBOARD=y CONFIG_VIDEO=y diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig index 98f0bc13a5f..d667cb9ae47 100644 --- a/configs/sandbox_defconfig +++ b/configs/sandbox_defconfig @@ -299,6 +299,7 @@ CONFIG_TIMER=y CONFIG_TIMER_EARLY=y CONFIG_SANDBOX_TIMER=y CONFIG_USB=y +CONFIG_DM_USB_GADGET=y CONFIG_USB_EMUL=y CONFIG_USB_KEYBOARD=y CONFIG_USB_GADGET=y diff --git a/configs/sandbox_flattree_defconfig b/configs/sandbox_flattree_defconfig index db916e6c4e9..29ae4532c50 100644 --- a/configs/sandbox_flattree_defconfig +++ b/configs/sandbox_flattree_defconfig @@ -203,6 +203,7 @@ CONFIG_TIMER=y CONFIG_TIMER_EARLY=y CONFIG_SANDBOX_TIMER=y CONFIG_USB=y +CONFIG_DM_USB_GADGET=y CONFIG_USB_EMUL=y CONFIG_USB_KEYBOARD=y CONFIG_VIDEO=y diff --git a/configs/sandbox_noinst_defconfig b/configs/sandbox_noinst_defconfig index ee04fbbc2e9..d39e54f98d2 100644 --- a/configs/sandbox_noinst_defconfig +++ b/configs/sandbox_noinst_defconfig @@ -217,6 +217,7 @@ CONFIG_TIMER=y CONFIG_TIMER_EARLY=y CONFIG_SANDBOX_TIMER=y CONFIG_USB=y +CONFIG_DM_USB_GADGET=y CONFIG_USB_EMUL=y CONFIG_USB_KEYBOARD=y CONFIG_VIDEO=y diff --git a/configs/sandbox_spl_defconfig b/configs/sandbox_spl_defconfig index 69b413382a1..4a67af2f088 100644 --- a/configs/sandbox_spl_defconfig +++ b/configs/sandbox_spl_defconfig @@ -223,6 +223,7 @@ CONFIG_TIMER=y CONFIG_TIMER_EARLY=y CONFIG_SANDBOX_TIMER=y CONFIG_USB=y +CONFIG_DM_USB_GADGET=y CONFIG_USB_EMUL=y CONFIG_USB_KEYBOARD=y CONFIG_VIDEO=y diff --git a/configs/sandbox_vpl_defconfig b/configs/sandbox_vpl_defconfig index 27354b8b5ed..8d76f19729b 100644 --- a/configs/sandbox_vpl_defconfig +++ b/configs/sandbox_vpl_defconfig @@ -236,6 +236,7 @@ CONFIG_VPL_TIMER=y CONFIG_TIMER_EARLY=y CONFIG_SANDBOX_TIMER=y CONFIG_USB=y +CONFIG_DM_USB_GADGET=y CONFIG_USB_EMUL=y CONFIG_USB_KEYBOARD=y CONFIG_VIDEO=y

Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Tested-by: Mattijs Korpershoek mkorpershoek@baylibre.com # on khadas vim3 Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: Add RB, TB from Mattijs --- cmd/fastboot.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/cmd/fastboot.c b/cmd/fastboot.c index 3d5ff951eb6..17fb0a0aa7b 100644 --- a/cmd/fastboot.c +++ b/cmd/fastboot.c @@ -61,6 +61,7 @@ static int do_fastboot_usb(int argc, char *const argv[], { int controller_index; char *usb_controller; + struct udevice *udc; char *endp; int ret;
@@ -79,7 +80,7 @@ static int do_fastboot_usb(int argc, char *const argv[], return CMD_RET_FAILURE; }
- ret = usb_gadget_initialize(controller_index); + ret = udc_device_get_by_index(controller_index, &udc); if (ret) { pr_err("USB init failed: %d\n", ret); return CMD_RET_FAILURE; @@ -103,13 +104,13 @@ static int do_fastboot_usb(int argc, char *const argv[], if (ctrlc()) break; schedule(); - usb_gadget_handle_interrupts(controller_index); + dm_usb_gadget_handle_interrupts(udc); }
ret = CMD_RET_SUCCESS;
exit: - usb_gadget_release(controller_index); + udc_device_put(udc); g_dnl_unregister(); g_dnl_clear_detach();

Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: No change --- cmd/rockusb.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/cmd/rockusb.c b/cmd/rockusb.c index f181ec61119..07088564a10 100644 --- a/cmd/rockusb.c +++ b/cmd/rockusb.c @@ -15,6 +15,7 @@ static int do_rockusb(struct cmd_tbl *cmdtp, int flag, int argc, { int controller_index, dev_index; char *usb_controller; + struct udevice *udc; char *devtype; char *devnum; int ret; @@ -34,7 +35,7 @@ static int do_rockusb(struct cmd_tbl *cmdtp, int flag, int argc, dev_index = simple_strtoul(devnum, NULL, 0); rockusb_dev_init(devtype, dev_index);
- ret = usb_gadget_initialize(controller_index); + ret = udc_device_get_by_index(controller_index, &udc); if (ret) { printf("USB init failed: %d\n", ret); return CMD_RET_FAILURE; @@ -56,14 +57,14 @@ static int do_rockusb(struct cmd_tbl *cmdtp, int flag, int argc, break; if (ctrlc()) break; - usb_gadget_handle_interrupts(controller_index); + dm_usb_gadget_handle_interrupts(udc); } ret = CMD_RET_SUCCESS;
exit: g_dnl_unregister(); g_dnl_clear_detach(); - usb_gadget_release(controller_index); + udc_device_put(udc);
return ret; }

On ven., sept. 01, 2023 at 11:49, Marek Vasut marex@denx.de wrote:
Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Signed-off-by: Marek Vasut marex@denx.de
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com
Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm
V2: No change
cmd/rockusb.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/cmd/rockusb.c b/cmd/rockusb.c index f181ec61119..07088564a10 100644 --- a/cmd/rockusb.c +++ b/cmd/rockusb.c @@ -15,6 +15,7 @@ static int do_rockusb(struct cmd_tbl *cmdtp, int flag, int argc, { int controller_index, dev_index; char *usb_controller;
- struct udevice *udc; char *devtype; char *devnum; int ret;
@@ -34,7 +35,7 @@ static int do_rockusb(struct cmd_tbl *cmdtp, int flag, int argc, dev_index = simple_strtoul(devnum, NULL, 0); rockusb_dev_init(devtype, dev_index);
- ret = usb_gadget_initialize(controller_index);
- ret = udc_device_get_by_index(controller_index, &udc); if (ret) { printf("USB init failed: %d\n", ret); return CMD_RET_FAILURE;
@@ -56,14 +57,14 @@ static int do_rockusb(struct cmd_tbl *cmdtp, int flag, int argc, break; if (ctrlc()) break;
usb_gadget_handle_interrupts(controller_index);
} ret = CMD_RET_SUCCESS;dm_usb_gadget_handle_interrupts(udc);
exit: g_dnl_unregister(); g_dnl_clear_detach();
- usb_gadget_release(controller_index);
udc_device_put(udc);
return ret;
}
2.40.1

Move the variable declaration around to improve code readability. No functional change.
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: Add RB from Mattijs --- cmd/usb_gadget_sdp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/cmd/usb_gadget_sdp.c b/cmd/usb_gadget_sdp.c index 1af82e11366..784d1b49768 100644 --- a/cmd/usb_gadget_sdp.c +++ b/cmd/usb_gadget_sdp.c @@ -14,13 +14,13 @@
static int do_sdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { + int controller_index; int ret;
if (argc < 2) return CMD_RET_USAGE;
- char *usb_controller = argv[1]; - int controller_index = simple_strtoul(usb_controller, NULL, 0); + controller_index = simple_strtoul(argv[1], NULL, 0); usb_gadget_initialize(controller_index);
g_dnl_clear_detach();

Move the variable declaration around to improve code readability. No functional change.
Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: No change --- cmd/thordown.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/cmd/thordown.c b/cmd/thordown.c index 838764ccef7..0d8dcee5314 100644 --- a/cmd/thordown.c +++ b/cmd/thordown.c @@ -15,22 +15,23 @@
int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { + char *interface, *devstring; + int controller_index; + int ret; + if (argc < 4) return CMD_RET_USAGE;
- char *usb_controller = argv[1]; - char *interface = argv[2]; - char *devstring = argv[3]; - - int ret; - puts("TIZEN "THOR" Downloader\n");
+ interface = argv[2]; + devstring = argv[3]; + ret = dfu_init_env_entities(interface, devstring); if (ret) goto done;
- int controller_index = simple_strtoul(usb_controller, NULL, 0); + controller_index = simple_strtoul(argv[1], NULL, 0); ret = usb_gadget_initialize(controller_index); if (ret) { pr_err("USB init failed: %d\n", ret);

On ven., sept. 01, 2023 at 11:49, Marek Vasut marex@denx.de wrote:
Move the variable declaration around to improve code readability. No functional change.
Signed-off-by: Marek Vasut marex@denx.de
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com
Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm
V2: No change
cmd/thordown.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/cmd/thordown.c b/cmd/thordown.c index 838764ccef7..0d8dcee5314 100644 --- a/cmd/thordown.c +++ b/cmd/thordown.c @@ -15,22 +15,23 @@
int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) {
- char *interface, *devstring;
- int controller_index;
- int ret;
- if (argc < 4) return CMD_RET_USAGE;
- char *usb_controller = argv[1];
- char *interface = argv[2];
- char *devstring = argv[3];
- int ret;
- puts("TIZEN "THOR" Downloader\n");
- interface = argv[2];
- devstring = argv[3];
- ret = dfu_init_env_entities(interface, devstring); if (ret) goto done;
- int controller_index = simple_strtoul(usb_controller, NULL, 0);
- controller_index = simple_strtoul(argv[1], NULL, 0); ret = usb_gadget_initialize(controller_index); if (ret) { pr_err("USB init failed: %d\n", ret);
-- 2.40.1

Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Tested-by: Mattijs Korpershoek mkorpershoek@baylibre.com # on khadas vim3 Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: Add RB, TB from Mattijs --- cmd/usb_mass_storage.c | 10 ++++++---- drivers/usb/gadget/f_mass_storage.c | 8 ++++---- include/usb_mass_storage.h | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-)
diff --git a/cmd/usb_mass_storage.c b/cmd/usb_mass_storage.c index c3cc1975f9d..9c51ae0967f 100644 --- a/cmd/usb_mass_storage.c +++ b/cmd/usb_mass_storage.c @@ -143,6 +143,7 @@ static int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag, const char *devtype; const char *devnum; unsigned int controller_index; + struct udevice *udc; int rc; int cable_ready_timeout __maybe_unused;
@@ -164,13 +165,14 @@ static int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag,
controller_index = (unsigned int)(simple_strtoul( usb_controller, NULL, 0)); - if (usb_gadget_initialize(controller_index)) { + rc = udc_device_get_by_index(controller_index, &udc); + if (rc) { pr_err("Couldn't init USB controller.\n"); rc = CMD_RET_FAILURE; goto cleanup_ums_init; }
- rc = fsg_init(ums, ums_count, controller_index); + rc = fsg_init(ums, ums_count, udc); if (rc) { pr_err("fsg_init failed\n"); rc = CMD_RET_FAILURE; @@ -215,7 +217,7 @@ static int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag, }
while (1) { - usb_gadget_handle_interrupts(controller_index); + dm_usb_gadget_handle_interrupts(udc);
rc = fsg_main_thread(NULL); if (rc) { @@ -247,7 +249,7 @@ static int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag, cleanup_register: g_dnl_unregister(); cleanup_board: - usb_gadget_release(controller_index); + udc_device_put(udc); cleanup_ums_init: ums_fini();
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c index f46829eb7ad..1d17331cb03 100644 --- a/drivers/usb/gadget/f_mass_storage.c +++ b/drivers/usb/gadget/f_mass_storage.c @@ -435,7 +435,7 @@ static void set_bulk_out_req_length(struct fsg_common *common, static struct ums *ums; static int ums_count; static struct fsg_common *the_fsg_common; -static unsigned int controller_index; +static struct udevice *udcdev;
static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep) { @@ -680,7 +680,7 @@ static int sleep_thread(struct fsg_common *common) k = 0; }
- usb_gadget_handle_interrupts(controller_index); + dm_usb_gadget_handle_interrupts(udcdev); } common->thread_wakeup_needed = 0; return rc; @@ -2764,11 +2764,11 @@ int fsg_add(struct usb_configuration *c) return fsg_bind_config(c->cdev, c, fsg_common); }
-int fsg_init(struct ums *ums_devs, int count, unsigned int controller_idx) +int fsg_init(struct ums *ums_devs, int count, struct udevice *udc) { ums = ums_devs; ums_count = count; - controller_index = controller_idx; + udcdev = udc;
return 0; } diff --git a/include/usb_mass_storage.h b/include/usb_mass_storage.h index 08ccc97cf22..83ab93b530d 100644 --- a/include/usb_mass_storage.h +++ b/include/usb_mass_storage.h @@ -25,7 +25,7 @@ struct ums { struct blk_desc block_dev; };
-int fsg_init(struct ums *ums_devs, int count, unsigned int controller_idx); +int fsg_init(struct ums *ums_devs, int count, struct udevice *udc); void fsg_cleanup(void); int fsg_main_thread(void *); int fsg_add(struct usb_configuration *c);

In case anything errors out during the DFU registration, detach the controller instead of bailing out right away. This way, the controller can be reattached on next attempt.
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: Add RB from Mattijs --- common/dfu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/common/dfu.c b/common/dfu.c index 96190889ab7..32fba84da16 100644 --- a/common/dfu.c +++ b/common/dfu.c @@ -34,7 +34,8 @@ int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget) ret = g_dnl_register(usb_dnl_gadget); if (ret) { pr_err("g_dnl_register failed"); - return CMD_RET_FAILURE; + ret = CMD_RET_FAILURE; + goto err_detach; }
#ifdef CONFIG_DFU_TIMEOUT @@ -106,6 +107,7 @@ int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget) } exit: g_dnl_unregister(); +err_detach: usb_gadget_release(usbctrl_index);
if (dfu_reset)

Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: Add RB from Mattijs --- common/dfu.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/common/dfu.c b/common/dfu.c index 32fba84da16..45206b9e225 100644 --- a/common/dfu.c +++ b/common/dfu.c @@ -23,11 +23,12 @@ int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget) { bool dfu_reset = false; + struct udevice *udc; int ret, i = 0;
- ret = usb_gadget_initialize(usbctrl_index); + ret = udc_device_get_by_index(usbctrl_index, &udc); if (ret) { - pr_err("usb_gadget_initialize failed\n"); + pr_err("udc_device_get_by_index failed\n"); return CMD_RET_FAILURE; } g_dnl_clear_detach(); @@ -55,7 +56,7 @@ int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget) }
/* - * This extra number of usb_gadget_handle_interrupts() + * This extra number of dm_usb_gadget_handle_interrupts() * calls is necessary to assure correct transmission * completion with dfu-util */ @@ -68,7 +69,7 @@ int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
if (dfu_get_defer_flush()) { /* - * Call to usb_gadget_handle_interrupts() is necessary + * Call to dm_usb_gadget_handle_interrupts() is necessary * to act on ZLP OUT transaction from HOST PC after * transmitting the whole file. * @@ -77,7 +78,7 @@ int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget) * 5 seconds). In such situation the dfu-util program * exits with error message. */ - usb_gadget_handle_interrupts(usbctrl_index); + dm_usb_gadget_handle_interrupts(udc); ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0); dfu_set_defer_flush(NULL); if (ret) { @@ -103,12 +104,12 @@ int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget) goto exit;
schedule(); - usb_gadget_handle_interrupts(usbctrl_index); + dm_usb_gadget_handle_interrupts(udc); } exit: g_dnl_unregister(); err_detach: - usb_gadget_release(usbctrl_index); + udc_device_put(udc);
if (dfu_reset) do_reset(NULL, 0, 0, NULL);

In case anything errors out during the SDP transfer, detach the controller instead of bailing out right away. This way, the controller can be reattached on next attempt.
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: - Call g_dnl_unregister() in case sdp_init() fails and on regular exit - Add RB from Mattijs --- common/spl/spl_sdp.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/common/spl/spl_sdp.c b/common/spl/spl_sdp.c index cc4fb4f7cca..aae886d2e57 100644 --- a/common/spl/spl_sdp.c +++ b/common/spl/spl_sdp.c @@ -25,13 +25,13 @@ static int spl_sdp_load_image(struct spl_image_info *spl_image, ret = g_dnl_register("usb_dnl_sdp"); if (ret) { pr_err("SDP dnl register failed: %d\n", ret); - return ret; + goto err_detach; }
ret = sdp_init(controller_index); if (ret) { pr_err("SDP init failed: %d\n", ret); - return -ENODEV; + goto err_unregister; }
/* @@ -42,6 +42,9 @@ static int spl_sdp_load_image(struct spl_image_info *spl_image, ret = spl_sdp_handle(controller_index, spl_image, bootdev); debug("SDP ended\n");
+err_unregister: + g_dnl_unregister(); +err_detach: usb_gadget_release(controller_index); return ret; }

Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: Add RB from Mattijs --- cmd/usb_gadget_sdp.c | 11 +++++++---- common/spl/spl_sdp.c | 13 ++++++++----- drivers/usb/gadget/f_sdp.c | 10 +++++----- include/sdp.h | 6 +++--- 4 files changed, 23 insertions(+), 17 deletions(-)
diff --git a/cmd/usb_gadget_sdp.c b/cmd/usb_gadget_sdp.c index 784d1b49768..748aa0a7488 100644 --- a/cmd/usb_gadget_sdp.c +++ b/cmd/usb_gadget_sdp.c @@ -15,13 +15,16 @@ static int do_sdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { int controller_index; + struct udevice *udc; int ret;
if (argc < 2) return CMD_RET_USAGE;
controller_index = simple_strtoul(argv[1], NULL, 0); - usb_gadget_initialize(controller_index); + ret = udc_device_get_by_index(controller_index, &udc); + if (ret) + return ret;
g_dnl_clear_detach(); ret = g_dnl_register("usb_dnl_sdp"); @@ -30,20 +33,20 @@ static int do_sdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) goto exit_register; }
- ret = sdp_init(controller_index); + ret = sdp_init(udc); if (ret) { pr_err("SDP init failed: %d\n", ret); goto exit; }
/* This command typically does not return but jumps to an image */ - sdp_handle(controller_index); + sdp_handle(udc); pr_err("SDP ended\n");
exit: g_dnl_unregister(); exit_register: - usb_gadget_release(controller_index); + udc_device_put(udc);
return CMD_RET_FAILURE; } diff --git a/common/spl/spl_sdp.c b/common/spl/spl_sdp.c index aae886d2e57..5a5ccd0676c 100644 --- a/common/spl/spl_sdp.c +++ b/common/spl/spl_sdp.c @@ -14,10 +14,13 @@ static int spl_sdp_load_image(struct spl_image_info *spl_image, struct spl_boot_device *bootdev) { - int ret; const int controller_index = CONFIG_SPL_SDP_USB_DEV; + struct udevice *udc; + int ret;
- usb_gadget_initialize(controller_index); + ret = udc_device_get_by_index(controller_index, &udc); + if (ret) + return ret;
board_usb_init(controller_index, USB_INIT_DEVICE);
@@ -28,7 +31,7 @@ static int spl_sdp_load_image(struct spl_image_info *spl_image, goto err_detach; }
- ret = sdp_init(controller_index); + ret = sdp_init(udc); if (ret) { pr_err("SDP init failed: %d\n", ret); goto err_unregister; @@ -39,13 +42,13 @@ static int spl_sdp_load_image(struct spl_image_info *spl_image, * or it loads a FIT image and returns it to be handled by the SPL * code. */ - ret = spl_sdp_handle(controller_index, spl_image, bootdev); + ret = spl_sdp_handle(udc, spl_image, bootdev); debug("SDP ended\n");
err_unregister: g_dnl_unregister(); err_detach: - usb_gadget_release(controller_index); + udc_device_put(udc); return ret; } SPL_LOAD_IMAGE_METHOD("USB SDP", 0, BOOT_DEVICE_BOARD, spl_sdp_load_image); diff --git a/drivers/usb/gadget/f_sdp.c b/drivers/usb/gadget/f_sdp.c index 4da5a160a09..37f6281abfb 100644 --- a/drivers/usb/gadget/f_sdp.c +++ b/drivers/usb/gadget/f_sdp.c @@ -702,7 +702,7 @@ static int sdp_bind_config(struct usb_configuration *c) return status; }
-int sdp_init(int controller_index) +int sdp_init(struct udevice *udc) { printf("SDP: initialize...\n"); while (!sdp_func->configuration_done) { @@ -712,7 +712,7 @@ int sdp_init(int controller_index) }
schedule(); - usb_gadget_handle_interrupts(controller_index); + dm_usb_gadget_handle_interrupts(udc); }
return 0; @@ -911,9 +911,9 @@ static void sdp_handle_out_ep(void) }
#ifndef CONFIG_SPL_BUILD -int sdp_handle(int controller_index) +int sdp_handle(struct udevice *udc) #else -int spl_sdp_handle(int controller_index, struct spl_image_info *spl_image, +int spl_sdp_handle(struct udevice *udc, struct spl_image_info *spl_image, struct spl_boot_device *bootdev) #endif { @@ -929,7 +929,7 @@ int spl_sdp_handle(int controller_index, struct spl_image_info *spl_image, return 0;
schedule(); - usb_gadget_handle_interrupts(controller_index); + dm_usb_gadget_handle_interrupts(udc);
#ifdef CONFIG_SPL_BUILD flag = sdp_handle_in_ep(spl_image, bootdev); diff --git a/include/sdp.h b/include/sdp.h index 6d89baa04ec..5492f9c47d2 100644 --- a/include/sdp.h +++ b/include/sdp.h @@ -9,15 +9,15 @@ #ifndef __SDP_H_ #define __SDP_H_
-int sdp_init(int controller_index); +int sdp_init(struct udevice *udc);
#ifdef CONFIG_SPL_BUILD #include <spl.h>
-int spl_sdp_handle(int controller_index, struct spl_image_info *spl_image, +int spl_sdp_handle(struct udevice *udc, struct spl_image_info *spl_image, struct spl_boot_device *bootdev); #else -int sdp_handle(int controller_index); +int sdp_handle(struct udevice *udc); #endif
#endif /* __SDP_H_ */

Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: No change --- cmd/thordown.c | 9 +++-- drivers/usb/gadget/f_thor.c | 74 +++++++++++++++++++------------------ include/thor.h | 4 +- 3 files changed, 45 insertions(+), 42 deletions(-)
diff --git a/cmd/thordown.c b/cmd/thordown.c index 0d8dcee5314..fcfd38f523c 100644 --- a/cmd/thordown.c +++ b/cmd/thordown.c @@ -17,6 +17,7 @@ int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { char *interface, *devstring; int controller_index; + struct udevice *udc; int ret;
if (argc < 4) @@ -32,7 +33,7 @@ int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) goto done;
controller_index = simple_strtoul(argv[1], NULL, 0); - ret = usb_gadget_initialize(controller_index); + ret = udc_device_get_by_index(controller_index, &udc); if (ret) { pr_err("USB init failed: %d\n", ret); ret = CMD_RET_FAILURE; @@ -46,7 +47,7 @@ int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) goto exit; }
- ret = thor_init(); + ret = thor_init(udc); if (ret) { pr_err("THOR DOWNLOAD failed: %d\n", ret); ret = CMD_RET_FAILURE; @@ -54,7 +55,7 @@ int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) }
do { - ret = thor_handle(); + ret = thor_handle(udc); if (ret == THOR_DFU_REINIT_NEEDED) { dfu_free_entities(); ret = dfu_init_env_entities(interface, devstring); @@ -67,7 +68,7 @@ int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) } while (ret == 0); exit: g_dnl_unregister(); - usb_gadget_release(controller_index); + udc_device_put(udc); done: dfu_free_entities();
diff --git a/drivers/usb/gadget/f_thor.c b/drivers/usb/gadget/f_thor.c index 47ef55b2fd3..3caa4c36387 100644 --- a/drivers/usb/gadget/f_thor.c +++ b/drivers/usb/gadget/f_thor.c @@ -15,9 +15,10 @@ */
#include <command.h> -#include <errno.h> #include <common.h> #include <console.h> +#include <dm.h> +#include <errno.h> #include <init.h> #include <log.h> #include <malloc.h> @@ -34,9 +35,9 @@
#include "f_thor.h"
-static void thor_tx_data(unsigned char *data, int len); +static void thor_tx_data(struct udevice *udc, unsigned char *data, int len); static void thor_set_dma(void *addr, int len); -static int thor_rx_data(void); +static int thor_rx_data(struct udevice *udc);
static struct f_thor *thor_func; static inline struct f_thor *func_to_thor(struct usb_function *f) @@ -56,15 +57,15 @@ DEFINE_CACHE_ALIGN_BUFFER(char, f_name, F_NAME_BUF_SIZE + 1); static unsigned long long int thor_file_size; static int alt_setting_num;
-static void send_rsp(const struct rsp_box *rsp) +static void send_rsp(struct udevice *udc, const struct rsp_box *rsp) { memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box)); - thor_tx_data(thor_tx_data_buf, sizeof(struct rsp_box)); + thor_tx_data(udc, thor_tx_data_buf, sizeof(struct rsp_box));
debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data); }
-static void send_data_rsp(s32 ack, s32 count) +static void send_data_rsp(struct udevice *udc, s32 ack, s32 count) { ALLOC_CACHE_ALIGN_BUFFER(struct data_rsp_box, rsp, sizeof(struct data_rsp_box)); @@ -73,12 +74,12 @@ static void send_data_rsp(s32 ack, s32 count) rsp->count = count;
memcpy(thor_tx_data_buf, rsp, sizeof(struct data_rsp_box)); - thor_tx_data(thor_tx_data_buf, sizeof(struct data_rsp_box)); + thor_tx_data(udc, thor_tx_data_buf, sizeof(struct data_rsp_box));
debug("-DATA RSP: %d, %d\n", ack, count); }
-static int process_rqt_info(const struct rqt_box *rqt) +static int process_rqt_info(struct udevice *udc, const struct rqt_box *rqt) { ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box)); memset(rsp, 0, sizeof(struct rsp_box)); @@ -111,11 +112,11 @@ static int process_rqt_info(const struct rqt_box *rqt) return -EINVAL; }
- send_rsp(rsp); + send_rsp(udc, rsp); return true; }
-static int process_rqt_cmd(const struct rqt_box *rqt) +static int process_rqt_cmd(struct udevice *udc, const struct rqt_box *rqt) { ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box)); memset(rsp, 0, sizeof(struct rsp_box)); @@ -126,7 +127,7 @@ static int process_rqt_cmd(const struct rqt_box *rqt) switch (rqt->rqt_data) { case RQT_CMD_REBOOT: debug("TARGET RESET\n"); - send_rsp(rsp); + send_rsp(udc, rsp); g_dnl_unregister(); dfu_free_entities(); #ifdef CONFIG_THOR_RESET_OFF @@ -136,7 +137,7 @@ static int process_rqt_cmd(const struct rqt_box *rqt) break; case RQT_CMD_POWEROFF: case RQT_CMD_EFSCLEAR: - send_rsp(rsp); + send_rsp(udc, rsp); default: printf("Command not supported -> cmd: %d\n", rqt->rqt_data); return -EINVAL; @@ -145,7 +146,8 @@ static int process_rqt_cmd(const struct rqt_box *rqt) return true; }
-static long long int download_head(unsigned long long total, +static long long int download_head(struct udevice *udc, + unsigned long long total, unsigned int packet_size, long long int *left, int *cnt) @@ -166,7 +168,7 @@ static long long int download_head(unsigned long long total, while (total - rcv_cnt >= packet_size) { thor_set_dma(buf, packet_size); buf += packet_size; - ret_rcv = thor_rx_data(); + ret_rcv = thor_rx_data(udc); if (ret_rcv < 0) return ret_rcv; rcv_cnt += ret_rcv; @@ -184,7 +186,7 @@ static long long int download_head(unsigned long long total, } buf = transfer_buffer; } - send_data_rsp(0, ++usb_pkt_cnt); + send_data_rsp(udc, 0, ++usb_pkt_cnt); }
/* Calculate the amount of data to arrive from PC (in bytes) */ @@ -200,11 +202,11 @@ static long long int download_head(unsigned long long total,
if (left_to_rcv) { thor_set_dma(buf, packet_size); - ret_rcv = thor_rx_data(); + ret_rcv = thor_rx_data(udc); if (ret_rcv < 0) return ret_rcv; rcv_cnt += ret_rcv; - send_data_rsp(0, ++usb_pkt_cnt); + send_data_rsp(udc, 0, ++usb_pkt_cnt); }
debug("%s: %llu total: %llu cnt: %d\n", __func__, rcv_cnt, total, *cnt); @@ -254,7 +256,7 @@ static int download_tail(long long int left, int cnt) return ret; }
-static long long int process_rqt_download(const struct rqt_box *rqt) +static long long int process_rqt_download(struct udevice *udc, const struct rqt_box *rqt) { ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box)); static long long int left, ret_head; @@ -301,8 +303,8 @@ static long long int process_rqt_download(const struct rqt_box *rqt) } break; case RQT_DL_FILE_START: - send_rsp(rsp); - ret_head = download_head(thor_file_size, THOR_PACKET_SIZE, + send_rsp(udc, rsp); + ret_head = download_head(udc, thor_file_size, THOR_PACKET_SIZE, &left, &cnt); if (ret_head < 0) { left = 0; @@ -324,11 +326,11 @@ static long long int process_rqt_download(const struct rqt_box *rqt) ret = -ENOTSUPP; }
- send_rsp(rsp); + send_rsp(udc, rsp); return ret; }
-static int process_data(void) +static int process_data(struct udevice *udc) { ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box)); int ret = -EINVAL; @@ -339,13 +341,13 @@ static int process_data(void)
switch (rqt->rqt) { case RQT_INFO: - ret = process_rqt_info(rqt); + ret = process_rqt_info(udc, rqt); break; case RQT_CMD: - ret = process_rqt_cmd(rqt); + ret = process_rqt_cmd(udc, rqt); break; case RQT_DL: - ret = (int) process_rqt_download(rqt); + ret = (int) process_rqt_download(udc, rqt); break; case RQT_UL: puts("RQT: UPLOAD not supported!\n"); @@ -536,7 +538,7 @@ static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length) return req; }
-static int thor_rx_data(void) +static int thor_rx_data(struct udevice *udc) { struct thor_dev *dev = thor_func->dev; int data_to_rx, tmp, status; @@ -557,7 +559,7 @@ static int thor_rx_data(void) }
while (!dev->rxdata) { - usb_gadget_handle_interrupts(0); + dm_usb_gadget_handle_interrupts(udc); if (ctrlc()) return -1; } @@ -568,7 +570,7 @@ static int thor_rx_data(void) return tmp; }
-static void thor_tx_data(unsigned char *data, int len) +static void thor_tx_data(struct udevice *udc, unsigned char *data, int len) { struct thor_dev *dev = thor_func->dev; unsigned char *ptr = dev->in_req->buf; @@ -591,7 +593,7 @@ static void thor_tx_data(unsigned char *data, int len)
/* Wait until tx interrupt received */ while (!dev->txdata) - usb_gadget_handle_interrupts(0); + dm_usb_gadget_handle_interrupts(udc);
dev->txdata = 0; } @@ -685,18 +687,18 @@ static void thor_set_dma(void *addr, int len) dev->out_req->length = len; }
-int thor_init(void) +int thor_init(struct udevice *udc) { struct thor_dev *dev = thor_func->dev;
/* Wait for a device enumeration and configuration settings */ debug("THOR enumeration/configuration setting....\n"); while (!dev->configuration_done) - usb_gadget_handle_interrupts(0); + dm_usb_gadget_handle_interrupts(udc);
thor_set_dma(thor_rx_data_buf, strlen("THOR")); /* detect the download request from Host PC */ - if (thor_rx_data() < 0) { + if (thor_rx_data(udc) < 0) { printf("%s: Data not received!\n", __func__); return -1; } @@ -706,7 +708,7 @@ int thor_init(void) udelay(30 * 1000); /* 30 ms */
strcpy((char *)thor_tx_data_buf, "ROHT"); - thor_tx_data(thor_tx_data_buf, strlen("ROHT")); + thor_tx_data(udc, thor_tx_data_buf, strlen("ROHT")); } else { puts("Wrong reply information\n"); return -1; @@ -715,17 +717,17 @@ int thor_init(void) return 0; }
-int thor_handle(void) +int thor_handle(struct udevice *udc) { int ret;
/* receive the data from Host PC */ while (1) { thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box)); - ret = thor_rx_data(); + ret = thor_rx_data(udc);
if (ret > 0) { - ret = process_data(); + ret = process_data(udc); #ifdef CONFIG_THOR_RESET_OFF if (ret == RESET_DONE) break; diff --git a/include/thor.h b/include/thor.h index ee67ab0a270..3cb56b654ae 100644 --- a/include/thor.h +++ b/include/thor.h @@ -14,7 +14,7 @@
#define THOR_DFU_REINIT_NEEDED 0xFFFFFFFE
-int thor_handle(void); -int thor_init(void); +int thor_handle(struct udevice *udc); +int thor_init(struct udevice *udc); int thor_add(struct usb_configuration *c); #endif /* __THOR_H_ */

On ven., sept. 01, 2023 at 11:49, Marek Vasut marex@denx.de wrote:
Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Signed-off-by: Marek Vasut marex@denx.de
Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com
V2: No change
cmd/thordown.c | 9 +++-- drivers/usb/gadget/f_thor.c | 74 +++++++++++++++++++------------------ include/thor.h | 4 +- 3 files changed, 45 insertions(+), 42 deletions(-)
diff --git a/cmd/thordown.c b/cmd/thordown.c index 0d8dcee5314..fcfd38f523c 100644 --- a/cmd/thordown.c +++ b/cmd/thordown.c @@ -17,6 +17,7 @@ int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { char *interface, *devstring; int controller_index;
struct udevice *udc; int ret;
if (argc < 4)
@@ -32,7 +33,7 @@ int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) goto done;
controller_index = simple_strtoul(argv[1], NULL, 0);
- ret = usb_gadget_initialize(controller_index);
- ret = udc_device_get_by_index(controller_index, &udc); if (ret) { pr_err("USB init failed: %d\n", ret); ret = CMD_RET_FAILURE;
@@ -46,7 +47,7 @@ int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) goto exit; }
- ret = thor_init();
- ret = thor_init(udc); if (ret) { pr_err("THOR DOWNLOAD failed: %d\n", ret); ret = CMD_RET_FAILURE;
@@ -54,7 +55,7 @@ int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) }
do {
ret = thor_handle();
if (ret == THOR_DFU_REINIT_NEEDED) { dfu_free_entities(); ret = dfu_init_env_entities(interface, devstring);ret = thor_handle(udc);
@@ -67,7 +68,7 @@ int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) } while (ret == 0); exit: g_dnl_unregister();
- usb_gadget_release(controller_index);
- udc_device_put(udc);
done: dfu_free_entities();
diff --git a/drivers/usb/gadget/f_thor.c b/drivers/usb/gadget/f_thor.c index 47ef55b2fd3..3caa4c36387 100644 --- a/drivers/usb/gadget/f_thor.c +++ b/drivers/usb/gadget/f_thor.c @@ -15,9 +15,10 @@ */
#include <command.h> -#include <errno.h> #include <common.h> #include <console.h> +#include <dm.h> +#include <errno.h> #include <init.h> #include <log.h> #include <malloc.h> @@ -34,9 +35,9 @@
#include "f_thor.h"
-static void thor_tx_data(unsigned char *data, int len); +static void thor_tx_data(struct udevice *udc, unsigned char *data, int len); static void thor_set_dma(void *addr, int len); -static int thor_rx_data(void); +static int thor_rx_data(struct udevice *udc);
static struct f_thor *thor_func; static inline struct f_thor *func_to_thor(struct usb_function *f) @@ -56,15 +57,15 @@ DEFINE_CACHE_ALIGN_BUFFER(char, f_name, F_NAME_BUF_SIZE + 1); static unsigned long long int thor_file_size; static int alt_setting_num;
-static void send_rsp(const struct rsp_box *rsp) +static void send_rsp(struct udevice *udc, const struct rsp_box *rsp) { memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box));
- thor_tx_data(thor_tx_data_buf, sizeof(struct rsp_box));
thor_tx_data(udc, thor_tx_data_buf, sizeof(struct rsp_box));
debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data);
}
-static void send_data_rsp(s32 ack, s32 count) +static void send_data_rsp(struct udevice *udc, s32 ack, s32 count) { ALLOC_CACHE_ALIGN_BUFFER(struct data_rsp_box, rsp, sizeof(struct data_rsp_box)); @@ -73,12 +74,12 @@ static void send_data_rsp(s32 ack, s32 count) rsp->count = count;
memcpy(thor_tx_data_buf, rsp, sizeof(struct data_rsp_box));
- thor_tx_data(thor_tx_data_buf, sizeof(struct data_rsp_box));
thor_tx_data(udc, thor_tx_data_buf, sizeof(struct data_rsp_box));
debug("-DATA RSP: %d, %d\n", ack, count);
}
-static int process_rqt_info(const struct rqt_box *rqt) +static int process_rqt_info(struct udevice *udc, const struct rqt_box *rqt) { ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box)); memset(rsp, 0, sizeof(struct rsp_box)); @@ -111,11 +112,11 @@ static int process_rqt_info(const struct rqt_box *rqt) return -EINVAL; }
- send_rsp(rsp);
- send_rsp(udc, rsp); return true;
}
-static int process_rqt_cmd(const struct rqt_box *rqt) +static int process_rqt_cmd(struct udevice *udc, const struct rqt_box *rqt) { ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box)); memset(rsp, 0, sizeof(struct rsp_box)); @@ -126,7 +127,7 @@ static int process_rqt_cmd(const struct rqt_box *rqt) switch (rqt->rqt_data) { case RQT_CMD_REBOOT: debug("TARGET RESET\n");
send_rsp(rsp);
g_dnl_unregister(); dfu_free_entities();send_rsp(udc, rsp);
#ifdef CONFIG_THOR_RESET_OFF @@ -136,7 +137,7 @@ static int process_rqt_cmd(const struct rqt_box *rqt) break; case RQT_CMD_POWEROFF: case RQT_CMD_EFSCLEAR:
send_rsp(rsp);
default: printf("Command not supported -> cmd: %d\n", rqt->rqt_data); return -EINVAL;send_rsp(udc, rsp);
@@ -145,7 +146,8 @@ static int process_rqt_cmd(const struct rqt_box *rqt) return true; }
-static long long int download_head(unsigned long long total, +static long long int download_head(struct udevice *udc,
unsigned long long total, unsigned int packet_size, long long int *left, int *cnt)
@@ -166,7 +168,7 @@ static long long int download_head(unsigned long long total, while (total - rcv_cnt >= packet_size) { thor_set_dma(buf, packet_size); buf += packet_size;
ret_rcv = thor_rx_data();
if (ret_rcv < 0) return ret_rcv; rcv_cnt += ret_rcv;ret_rcv = thor_rx_data(udc);
@@ -184,7 +186,7 @@ static long long int download_head(unsigned long long total, } buf = transfer_buffer; }
send_data_rsp(0, ++usb_pkt_cnt);
send_data_rsp(udc, 0, ++usb_pkt_cnt);
}
/* Calculate the amount of data to arrive from PC (in bytes) */
@@ -200,11 +202,11 @@ static long long int download_head(unsigned long long total,
if (left_to_rcv) { thor_set_dma(buf, packet_size);
ret_rcv = thor_rx_data();
if (ret_rcv < 0) return ret_rcv; rcv_cnt += ret_rcv;ret_rcv = thor_rx_data(udc);
send_data_rsp(0, ++usb_pkt_cnt);
send_data_rsp(udc, 0, ++usb_pkt_cnt);
}
debug("%s: %llu total: %llu cnt: %d\n", __func__, rcv_cnt, total, *cnt);
@@ -254,7 +256,7 @@ static int download_tail(long long int left, int cnt) return ret; }
-static long long int process_rqt_download(const struct rqt_box *rqt) +static long long int process_rqt_download(struct udevice *udc, const struct rqt_box *rqt) { ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box)); static long long int left, ret_head; @@ -301,8 +303,8 @@ static long long int process_rqt_download(const struct rqt_box *rqt) } break; case RQT_DL_FILE_START:
send_rsp(rsp);
ret_head = download_head(thor_file_size, THOR_PACKET_SIZE,
send_rsp(udc, rsp);
if (ret_head < 0) { left = 0;ret_head = download_head(udc, thor_file_size, THOR_PACKET_SIZE, &left, &cnt);
@@ -324,11 +326,11 @@ static long long int process_rqt_download(const struct rqt_box *rqt) ret = -ENOTSUPP; }
- send_rsp(rsp);
- send_rsp(udc, rsp); return ret;
}
-static int process_data(void) +static int process_data(struct udevice *udc) { ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box)); int ret = -EINVAL; @@ -339,13 +341,13 @@ static int process_data(void)
switch (rqt->rqt) { case RQT_INFO:
ret = process_rqt_info(rqt);
break; case RQT_CMD:ret = process_rqt_info(udc, rqt);
ret = process_rqt_cmd(rqt);
break; case RQT_DL:ret = process_rqt_cmd(udc, rqt);
ret = (int) process_rqt_download(rqt);
break; case RQT_UL: puts("RQT: UPLOAD not supported!\n");ret = (int) process_rqt_download(udc, rqt);
@@ -536,7 +538,7 @@ static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length) return req; }
-static int thor_rx_data(void) +static int thor_rx_data(struct udevice *udc) { struct thor_dev *dev = thor_func->dev; int data_to_rx, tmp, status; @@ -557,7 +559,7 @@ static int thor_rx_data(void) }
while (!dev->rxdata) {
usb_gadget_handle_interrupts(0);
}dm_usb_gadget_handle_interrupts(udc); if (ctrlc()) return -1;
@@ -568,7 +570,7 @@ static int thor_rx_data(void) return tmp; }
-static void thor_tx_data(unsigned char *data, int len) +static void thor_tx_data(struct udevice *udc, unsigned char *data, int len) { struct thor_dev *dev = thor_func->dev; unsigned char *ptr = dev->in_req->buf; @@ -591,7 +593,7 @@ static void thor_tx_data(unsigned char *data, int len)
/* Wait until tx interrupt received */ while (!dev->txdata)
usb_gadget_handle_interrupts(0);
dm_usb_gadget_handle_interrupts(udc);
dev->txdata = 0;
} @@ -685,18 +687,18 @@ static void thor_set_dma(void *addr, int len) dev->out_req->length = len; }
-int thor_init(void) +int thor_init(struct udevice *udc) { struct thor_dev *dev = thor_func->dev;
/* Wait for a device enumeration and configuration settings */ debug("THOR enumeration/configuration setting....\n"); while (!dev->configuration_done)
usb_gadget_handle_interrupts(0);
dm_usb_gadget_handle_interrupts(udc);
thor_set_dma(thor_rx_data_buf, strlen("THOR")); /* detect the download request from Host PC */
- if (thor_rx_data() < 0) {
- if (thor_rx_data(udc) < 0) { printf("%s: Data not received!\n", __func__); return -1; }
@@ -706,7 +708,7 @@ int thor_init(void) udelay(30 * 1000); /* 30 ms */
strcpy((char *)thor_tx_data_buf, "ROHT");
thor_tx_data(thor_tx_data_buf, strlen("ROHT"));
} else { puts("Wrong reply information\n"); return -1;thor_tx_data(udc, thor_tx_data_buf, strlen("ROHT"));
@@ -715,17 +717,17 @@ int thor_init(void) return 0; }
-int thor_handle(void) +int thor_handle(struct udevice *udc) { int ret;
/* receive the data from Host PC */ while (1) { thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box));
ret = thor_rx_data();
ret = thor_rx_data(udc);
if (ret > 0) {
ret = process_data();
ret = process_data(udc);
#ifdef CONFIG_THOR_RESET_OFF if (ret == RESET_DONE) break; diff --git a/include/thor.h b/include/thor.h index ee67ab0a270..3cb56b654ae 100644 --- a/include/thor.h +++ b/include/thor.h @@ -14,7 +14,7 @@
#define THOR_DFU_REINIT_NEEDED 0xFFFFFFFE
-int thor_handle(void); -int thor_init(void); +int thor_handle(struct udevice *udc); +int thor_init(struct udevice *udc); int thor_add(struct usb_configuration *c);
#endif /* __THOR_H_ */
2.40.1

Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: No change --- drivers/usb/gadget/f_acm.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/drivers/usb/gadget/f_acm.c b/drivers/usb/gadget/f_acm.c index b2ddd1ada8b..de42e0189e8 100644 --- a/drivers/usb/gadget/f_acm.c +++ b/drivers/usb/gadget/f_acm.c @@ -51,7 +51,7 @@ struct f_acm { #define ACM_CTRL_RTS BIT(1) /* unused with full duplex */ #define ACM_CTRL_DTR BIT(0) /* host is ready for data r/w */
- int controller_index; + struct udevice *udc; };
static struct f_acm *default_acm_function; @@ -489,7 +489,7 @@ static void __acm_tx(struct f_acm *f_acm) int len, ret;
do { - usb_gadget_handle_interrupts(f_acm->controller_index); + dm_usb_gadget_handle_interrupts(f_acm->udc);
if (!(f_acm->handshake_bits & ACM_CTRL_DTR)) break; @@ -520,7 +520,7 @@ static bool acm_connected(struct stdio_dev *dev) struct f_acm *f_acm = stdio_to_acm(dev);
/* give a chance to process udc irq */ - usb_gadget_handle_interrupts(f_acm->controller_index); + dm_usb_gadget_handle_interrupts(f_acm->udc);
return f_acm->connected; } @@ -543,7 +543,10 @@ static int acm_add(struct usb_configuration *c) f_acm->usb_function.descriptors = acm_fs_function; f_acm->usb_function.hs_descriptors = acm_hs_function; f_acm->usb_function.setup = acm_setup; - f_acm->controller_index = 0; + + status = udc_device_get_by_index(0, &f_acm->udc); + if (status) + return status;
status = usb_add_function(c, &f_acm->usb_function); if (status) { @@ -567,7 +570,7 @@ static int acm_stdio_tstc(struct stdio_dev *dev) { struct f_acm *f_acm = stdio_to_acm(dev);
- usb_gadget_handle_interrupts(f_acm->controller_index); + dm_usb_gadget_handle_interrupts(f_acm->udc);
return (f_acm->rx_buf.size > 0); }

On ven., sept. 01, 2023 at 11:50, Marek Vasut marex@denx.de wrote:
Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Signed-off-by: Marek Vasut marex@denx.de
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com
Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm
V2: No change
drivers/usb/gadget/f_acm.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/drivers/usb/gadget/f_acm.c b/drivers/usb/gadget/f_acm.c index b2ddd1ada8b..de42e0189e8 100644 --- a/drivers/usb/gadget/f_acm.c +++ b/drivers/usb/gadget/f_acm.c @@ -51,7 +51,7 @@ struct f_acm { #define ACM_CTRL_RTS BIT(1) /* unused with full duplex */ #define ACM_CTRL_DTR BIT(0) /* host is ready for data r/w */
- int controller_index;
- struct udevice *udc;
};
static struct f_acm *default_acm_function; @@ -489,7 +489,7 @@ static void __acm_tx(struct f_acm *f_acm) int len, ret;
do {
usb_gadget_handle_interrupts(f_acm->controller_index);
dm_usb_gadget_handle_interrupts(f_acm->udc);
if (!(f_acm->handshake_bits & ACM_CTRL_DTR)) break;
@@ -520,7 +520,7 @@ static bool acm_connected(struct stdio_dev *dev) struct f_acm *f_acm = stdio_to_acm(dev);
/* give a chance to process udc irq */
- usb_gadget_handle_interrupts(f_acm->controller_index);
dm_usb_gadget_handle_interrupts(f_acm->udc);
return f_acm->connected;
} @@ -543,7 +543,10 @@ static int acm_add(struct usb_configuration *c) f_acm->usb_function.descriptors = acm_fs_function; f_acm->usb_function.hs_descriptors = acm_hs_function; f_acm->usb_function.setup = acm_setup;
- f_acm->controller_index = 0;
status = udc_device_get_by_index(0, &f_acm->udc);
if (status)
return status;
status = usb_add_function(c, &f_acm->usb_function); if (status) {
@@ -567,7 +570,7 @@ static int acm_stdio_tstc(struct stdio_dev *dev) { struct f_acm *f_acm = stdio_to_acm(dev);
- usb_gadget_handle_interrupts(f_acm->controller_index);
dm_usb_gadget_handle_interrupts(f_acm->udc);
return (f_acm->rx_buf.size > 0);
}
2.40.1

Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: Use udev->parent for UDC access --- drivers/usb/gadget/ether.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 5ff06d3814b..11b1a6221b3 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -1880,8 +1880,10 @@ static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) } }
-static int eth_stop(struct eth_dev *dev) +static int eth_stop(struct udevice *udev) { + struct ether_priv *priv = dev_get_priv(udev); + struct eth_dev *dev = &priv->ethdev; #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT unsigned long ts; unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */ @@ -1895,7 +1897,7 @@ static int eth_stop(struct eth_dev *dev) /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */ ts = get_timer(0); while (get_timer(ts) < timeout) - usb_gadget_handle_interrupts(0); + dm_usb_gadget_handle_interrupts(udev->parent); #endif
rndis_uninit(dev->rndis_config); @@ -2300,7 +2302,7 @@ static int usb_eth_start(struct udevice *udev) pr_err("The remote end did not respond in time."); goto fail; } - usb_gadget_handle_interrupts(0); + dm_usb_gadget_handle_interrupts(udev->parent); }
packet_received = 0; @@ -2370,7 +2372,7 @@ static int usb_eth_send(struct udevice *udev, void *packet, int length) printf("timeout sending packets to usb ethernet\n"); return -1; } - usb_gadget_handle_interrupts(0); + dm_usb_gadget_handle_interrupts(udev->parent); } free(rndis_pkt);
@@ -2400,13 +2402,13 @@ static void usb_eth_stop(struct udevice *udev) * 2) 'pullup' callback in your UDC driver can be improved to perform * this deinitialization. */ - eth_stop(dev); + eth_stop(udev);
usb_gadget_disconnect(dev->gadget);
/* Clear pending interrupt */ if (dev->network_started) { - usb_gadget_handle_interrupts(0); + dm_usb_gadget_handle_interrupts(udev->parent); dev->network_started = 0; } } @@ -2416,7 +2418,7 @@ static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp) struct ether_priv *priv = dev_get_priv(dev); struct eth_dev *ethdev = &priv->ethdev;
- usb_gadget_handle_interrupts(0); + dm_usb_gadget_handle_interrupts(dev->parent);
if (packet_received) { if (ethdev->rx_req) { @@ -2467,7 +2469,7 @@ int usb_ether_init(void) return ret; }
- return usb_gadget_initialize(0); + return 0; }
static int usb_eth_probe(struct udevice *dev) @@ -2528,7 +2530,7 @@ static int usb_eth_remove(struct udevice *dev)
static int usb_eth_unbind(struct udevice *dev) { - usb_gadget_release(0); + udc_device_put(dev->parent);
return 0; }

On ven., sept. 01, 2023 at 11:50, Marek Vasut marex@denx.de wrote:
Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Signed-off-by: Marek Vasut marex@denx.de
Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm
V2: Use udev->parent for UDC access
drivers/usb/gadget/ether.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 5ff06d3814b..11b1a6221b3 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -1880,8 +1880,10 @@ static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) } }
-static int eth_stop(struct eth_dev *dev) +static int eth_stop(struct udevice *udev) {
- struct ether_priv *priv = dev_get_priv(udev);
- struct eth_dev *dev = &priv->ethdev;
#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT unsigned long ts; unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */ @@ -1895,7 +1897,7 @@ static int eth_stop(struct eth_dev *dev) /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */ ts = get_timer(0); while (get_timer(ts) < timeout)
usb_gadget_handle_interrupts(0);
dm_usb_gadget_handle_interrupts(udev->parent);
#endif
rndis_uninit(dev->rndis_config);
@@ -2300,7 +2302,7 @@ static int usb_eth_start(struct udevice *udev) pr_err("The remote end did not respond in time."); goto fail; }
usb_gadget_handle_interrupts(0);
dm_usb_gadget_handle_interrupts(udev->parent);
}
packet_received = 0;
@@ -2370,7 +2372,7 @@ static int usb_eth_send(struct udevice *udev, void *packet, int length) printf("timeout sending packets to usb ethernet\n"); return -1; }
usb_gadget_handle_interrupts(0);
} free(rndis_pkt);dm_usb_gadget_handle_interrupts(udev->parent);
@@ -2400,13 +2402,13 @@ static void usb_eth_stop(struct udevice *udev) * 2) 'pullup' callback in your UDC driver can be improved to perform * this deinitialization. */
- eth_stop(dev);
eth_stop(udev);
usb_gadget_disconnect(dev->gadget);
/* Clear pending interrupt */ if (dev->network_started) {
usb_gadget_handle_interrupts(0);
dev->network_started = 0; }dm_usb_gadget_handle_interrupts(udev->parent);
} @@ -2416,7 +2418,7 @@ static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp) struct ether_priv *priv = dev_get_priv(dev); struct eth_dev *ethdev = &priv->ethdev;
- usb_gadget_handle_interrupts(0);
dm_usb_gadget_handle_interrupts(dev->parent);
if (packet_received) { if (ethdev->rx_req) {
@@ -2467,7 +2469,7 @@ int usb_ether_init(void) return ret; }
- return usb_gadget_initialize(0);
- return 0;
No udc_device_get() here?
This looks a little suspicious, because in the unbind() we call udc_device_put().
}
static int usb_eth_probe(struct udevice *dev) @@ -2528,7 +2530,7 @@ static int usb_eth_remove(struct udevice *dev)
static int usb_eth_unbind(struct udevice *dev) {
- usb_gadget_release(0);
udc_device_put(dev->parent);
return 0;
}
2.40.1

On 9/4/23 09:56, Mattijs Korpershoek wrote:
On ven., sept. 01, 2023 at 11:50, Marek Vasut marex@denx.de wrote:
Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Signed-off-by: Marek Vasut marex@denx.de
Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm
V2: Use udev->parent for UDC access
drivers/usb/gadget/ether.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 5ff06d3814b..11b1a6221b3 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -1880,8 +1880,10 @@ static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) } }
-static int eth_stop(struct eth_dev *dev) +static int eth_stop(struct udevice *udev) {
- struct ether_priv *priv = dev_get_priv(udev);
- struct eth_dev *dev = &priv->ethdev; #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT unsigned long ts; unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
@@ -1895,7 +1897,7 @@ static int eth_stop(struct eth_dev *dev) /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */ ts = get_timer(0); while (get_timer(ts) < timeout)
usb_gadget_handle_interrupts(0);
dm_usb_gadget_handle_interrupts(udev->parent);
#endif
rndis_uninit(dev->rndis_config);
@@ -2300,7 +2302,7 @@ static int usb_eth_start(struct udevice *udev) pr_err("The remote end did not respond in time."); goto fail; }
usb_gadget_handle_interrupts(0);
dm_usb_gadget_handle_interrupts(udev->parent);
}
packet_received = 0;
@@ -2370,7 +2372,7 @@ static int usb_eth_send(struct udevice *udev, void *packet, int length) printf("timeout sending packets to usb ethernet\n"); return -1; }
usb_gadget_handle_interrupts(0);
} free(rndis_pkt);dm_usb_gadget_handle_interrupts(udev->parent);
@@ -2400,13 +2402,13 @@ static void usb_eth_stop(struct udevice *udev) * 2) 'pullup' callback in your UDC driver can be improved to perform * this deinitialization. */
- eth_stop(dev);
eth_stop(udev);
usb_gadget_disconnect(dev->gadget);
/* Clear pending interrupt */ if (dev->network_started) {
usb_gadget_handle_interrupts(0);
dev->network_started = 0; } }dm_usb_gadget_handle_interrupts(udev->parent);
@@ -2416,7 +2418,7 @@ static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp) struct ether_priv *priv = dev_get_priv(dev); struct eth_dev *ethdev = &priv->ethdev;
- usb_gadget_handle_interrupts(0);
dm_usb_gadget_handle_interrupts(dev->parent);
if (packet_received) { if (ethdev->rx_req) {
@@ -2467,7 +2469,7 @@ int usb_ether_init(void) return ret; }
- return usb_gadget_initialize(0);
- return 0;
No udc_device_get() here?
This looks a little suspicious, because in the unbind() we call udc_device_put().
Look at how udc_device_get_by_index() is implemented and what it does, compared to what usb_ether_init() does (hint: they are basically doing the same thing). This udc_device_put() here is only a workaround for platforms which are not using the bind command to bind USB ethernet yet.

On lun., sept. 04, 2023 at 11:10, Marek Vasut marex@denx.de wrote:
On 9/4/23 09:56, Mattijs Korpershoek wrote:
On ven., sept. 01, 2023 at 11:50, Marek Vasut marex@denx.de wrote:
Convert to plain udevice interaction with UDC controller device, avoid the use of UDC uclass dev_array .
Signed-off-by: Marek Vasut marex@denx.de
Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm
V2: Use udev->parent for UDC access
drivers/usb/gadget/ether.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 5ff06d3814b..11b1a6221b3 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c @@ -1880,8 +1880,10 @@ static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) } }
-static int eth_stop(struct eth_dev *dev) +static int eth_stop(struct udevice *udev) {
- struct ether_priv *priv = dev_get_priv(udev);
- struct eth_dev *dev = &priv->ethdev; #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT unsigned long ts; unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
@@ -1895,7 +1897,7 @@ static int eth_stop(struct eth_dev *dev) /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */ ts = get_timer(0); while (get_timer(ts) < timeout)
usb_gadget_handle_interrupts(0);
dm_usb_gadget_handle_interrupts(udev->parent);
#endif
rndis_uninit(dev->rndis_config);
@@ -2300,7 +2302,7 @@ static int usb_eth_start(struct udevice *udev) pr_err("The remote end did not respond in time."); goto fail; }
usb_gadget_handle_interrupts(0);
dm_usb_gadget_handle_interrupts(udev->parent);
}
packet_received = 0;
@@ -2370,7 +2372,7 @@ static int usb_eth_send(struct udevice *udev, void *packet, int length) printf("timeout sending packets to usb ethernet\n"); return -1; }
usb_gadget_handle_interrupts(0);
} free(rndis_pkt);dm_usb_gadget_handle_interrupts(udev->parent);
@@ -2400,13 +2402,13 @@ static void usb_eth_stop(struct udevice *udev) * 2) 'pullup' callback in your UDC driver can be improved to perform * this deinitialization. */
- eth_stop(dev);
eth_stop(udev);
usb_gadget_disconnect(dev->gadget);
/* Clear pending interrupt */ if (dev->network_started) {
usb_gadget_handle_interrupts(0);
dev->network_started = 0; } }dm_usb_gadget_handle_interrupts(udev->parent);
@@ -2416,7 +2418,7 @@ static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp) struct ether_priv *priv = dev_get_priv(dev); struct eth_dev *ethdev = &priv->ethdev;
- usb_gadget_handle_interrupts(0);
dm_usb_gadget_handle_interrupts(dev->parent);
if (packet_received) { if (ethdev->rx_req) {
@@ -2467,7 +2469,7 @@ int usb_ether_init(void) return ret; }
- return usb_gadget_initialize(0);
- return 0;
No udc_device_get() here?
This looks a little suspicious, because in the unbind() we call udc_device_put().
Look at how udc_device_get_by_index() is implemented and what it does, compared to what usb_ether_init() does (hint: they are basically doing the same thing). This udc_device_put() here is only a workaround for platforms which are not using the bind command to bind USB ethernet yet.
Oh right. Thank you for explaining.
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com

Remove legacy functions limited by the dev_array array, those are no longer used anywhere, all the code uses plain udevice based access now.
The usb_gadget_handle_interrupts() is doing udevice look up until all call sites use dm_usb_gadget_handle_interrupts().
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Signed-off-by: Marek Vasut marex@denx.de --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: Add RB from Mattijs --- drivers/usb/gadget/udc/udc-uclass.c | 44 ++++------------------------- include/linux/usb/gadget.h | 17 ----------- 2 files changed, 6 insertions(+), 55 deletions(-)
diff --git a/drivers/usb/gadget/udc/udc-uclass.c b/drivers/usb/gadget/udc/udc-uclass.c index b4271b4be9f..7f54a3b00cb 100644 --- a/drivers/usb/gadget/udc/udc-uclass.c +++ b/drivers/usb/gadget/udc/udc-uclass.c @@ -12,9 +12,6 @@ #include <linux/usb/gadget.h>
#if CONFIG_IS_ENABLED(DM_USB_GADGET) -#define MAX_UDC_DEVICES 4 -static struct udevice *dev_array[MAX_UDC_DEVICES]; - int udc_device_get_by_index(int index, struct udevice **udev) { struct udevice *dev = NULL; @@ -45,45 +42,16 @@ int udc_device_put(struct udevice *udev) #endif }
-int usb_gadget_initialize(int index) -{ - int ret; - struct udevice *dev = NULL; - - if (index < 0 || index >= ARRAY_SIZE(dev_array)) - return -EINVAL; - if (dev_array[index]) - return 0; - ret = udc_device_get_by_index(index, &dev); - if (!dev || ret) { - pr_err("No USB device found\n"); - return -ENODEV; - } - dev_array[index] = dev; - return 0; -} - -int usb_gadget_release(int index) +int usb_gadget_handle_interrupts(int index) { -#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) + struct udevice *udc; int ret; - if (index < 0 || index >= ARRAY_SIZE(dev_array)) - return -EINVAL;
- ret = device_remove(dev_array[index]); - if (!ret) - dev_array[index] = NULL; - return ret; -#else - return -ENOSYS; -#endif -} + ret = udc_device_get_by_index(index, &udc); + if (ret) + return ret;
-int usb_gadget_handle_interrupts(int index) -{ - if (index < 0 || index >= ARRAY_SIZE(dev_array)) - return -EINVAL; - return dm_usb_gadget_handle_interrupts(dev_array[index]); + return dm_usb_gadget_handle_interrupts(udc); } #else /* Backwards hardware compatibility -- switch to DM_USB_GADGET */ diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h index 5e9a6513d5b..54875d2716e 100644 --- a/include/linux/usb/gadget.h +++ b/include/linux/usb/gadget.h @@ -1023,21 +1023,4 @@ int udc_device_get_by_index(int index, struct udevice **udev); */ int udc_device_put(struct udevice *udev);
-#if CONFIG_IS_ENABLED(DM_USB_GADGET) -int usb_gadget_initialize(int index); -int usb_gadget_release(int index); -int dm_usb_gadget_handle_interrupts(struct udevice *dev); -#else -#include <usb.h> -static inline int usb_gadget_initialize(int index) -{ - return board_usb_init(index, USB_INIT_DEVICE); -} - -static inline int usb_gadget_release(int index) -{ - return board_usb_cleanup(index, USB_INIT_DEVICE); -} -#endif - #endif /* __LINUX_USB_GADGET_H */

The usb_gadget_handle_interrupts() is no longer used anywhere, replace the remaining uses with dm_usb_gadget_handle_interrupts() which takes udevice as a parameter.
Some of the UDC drivers currently ignore the index parameter altogether, those also ignore the udevice and have to be reworked. Other like the dwc3_uboot_handle_interrupt() had to be switched from index to udevice look up to avoid breakage.
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Tested-by: Mattijs Korpershoek mkorpershoek@baylibre.com # on khadas vim3 Signed-off-by: Marek Vasut marex@denx.de --- NOTE: Next step, get rid of dwc3_uboot_init()/dwc3_uboot_exit() --- Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm --- V2: Add RB, TB from Mattijs --- arch/arm/mach-rockchip/board.c | 4 ++-- board/purism/librem5/spl.c | 4 ++-- board/samsung/common/exynos5-dt.c | 4 ++-- board/st/stih410-b2260/board.c | 4 ++-- board/ti/am43xx/board.c | 6 +++--- drivers/usb/dwc3/core.c | 6 +++--- drivers/usb/dwc3/dwc3-omap.c | 8 ++++---- drivers/usb/gadget/at91_udc.c | 2 +- drivers/usb/gadget/atmel_usba_udc.c | 3 +-- drivers/usb/gadget/ci_udc.c | 4 ++-- drivers/usb/gadget/dwc2_udc_otg.c | 12 ++---------- drivers/usb/gadget/udc/udc-uclass.c | 12 ------------ drivers/usb/host/usb-sandbox.c | 5 ----- drivers/usb/musb-new/musb_uboot.c | 2 +- include/dwc3-omap-uboot.h | 2 +- include/dwc3-uboot.h | 2 +- include/linux/usb/gadget.h | 2 +- 17 files changed, 28 insertions(+), 54 deletions(-)
diff --git a/arch/arm/mach-rockchip/board.c b/arch/arm/mach-rockchip/board.c index 8d7b39ba157..57f08e0be0e 100644 --- a/arch/arm/mach-rockchip/board.c +++ b/arch/arm/mach-rockchip/board.c @@ -299,9 +299,9 @@ static struct dwc3_device dwc3_device_data = { .hsphy_mode = USBPHY_INTERFACE_MODE_UTMIW, };
-int usb_gadget_handle_interrupts(int index) +int dm_usb_gadget_handle_interrupts(struct udevice *dev) { - dwc3_uboot_handle_interrupt(0); + dwc3_uboot_handle_interrupt(dev); return 0; }
diff --git a/board/purism/librem5/spl.c b/board/purism/librem5/spl.c index 90f1fcf415f..581f0929662 100644 --- a/board/purism/librem5/spl.c +++ b/board/purism/librem5/spl.c @@ -418,9 +418,9 @@ out: return rv; }
-int usb_gadget_handle_interrupts(int index) +int dm_usb_gadget_handle_interrupts(struct udevice *dev) { - dwc3_uboot_handle_interrupt(0); + dwc3_uboot_handle_interrupt(dev); return 0; }
diff --git a/board/samsung/common/exynos5-dt.c b/board/samsung/common/exynos5-dt.c index cde77d79a0f..726b7f0667a 100644 --- a/board/samsung/common/exynos5-dt.c +++ b/board/samsung/common/exynos5-dt.c @@ -126,9 +126,9 @@ static struct dwc3_device dwc3_device_data = { .index = 0, };
-int usb_gadget_handle_interrupts(int index) +int dm_usb_gadget_handle_interrupts(struct udevice *dev) { - dwc3_uboot_handle_interrupt(0); + dwc3_uboot_handle_interrupt(dev); return 0; }
diff --git a/board/st/stih410-b2260/board.c b/board/st/stih410-b2260/board.c index cd3a7dc51a2..e21cbc270e9 100644 --- a/board/st/stih410-b2260/board.c +++ b/board/st/stih410-b2260/board.c @@ -50,9 +50,9 @@ static struct dwc3_device dwc3_device_data = { .index = 0, };
-int usb_gadget_handle_interrupts(int index) +int dm_usb_gadget_handle_interrupts(struct udevice *dev) { - dwc3_uboot_handle_interrupt(index); + dwc3_uboot_handle_interrupt(dev); return 0; }
diff --git a/board/ti/am43xx/board.c b/board/ti/am43xx/board.c index 87e552a4701..58bfe7cd455 100644 --- a/board/ti/am43xx/board.c +++ b/board/ti/am43xx/board.c @@ -760,13 +760,13 @@ static struct ti_usb_phy_device usb_phy2_device = { .index = 1, };
-int usb_gadget_handle_interrupts(int index) +int dm_usb_gadget_handle_interrupts(struct udevice *dev) { u32 status;
- status = dwc3_omap_uboot_interrupt_status(index); + status = dwc3_omap_uboot_interrupt_status(dev); if (status) - dwc3_uboot_handle_interrupt(index); + dwc3_uboot_handle_interrupt(dev);
return 0; } diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c index 49f6a1900b0..7ca9d09824e 100644 --- a/drivers/usb/dwc3/core.c +++ b/drivers/usb/dwc3/core.c @@ -986,18 +986,18 @@ void dwc3_uboot_exit(int index)
/** * dwc3_uboot_handle_interrupt - handle dwc3 core interrupt - * @index: index of this controller + * @dev: device of this controller * * Invokes dwc3 gadget interrupts. * * Generally called from board file. */ -void dwc3_uboot_handle_interrupt(int index) +void dwc3_uboot_handle_interrupt(struct udevice *dev) { struct dwc3 *dwc = NULL;
list_for_each_entry(dwc, &dwc3_list, list) { - if (dwc->index != index) + if (dwc->dev != dev) continue;
dwc3_gadget_uboot_handle_interrupt(dwc); diff --git a/drivers/usb/dwc3/dwc3-omap.c b/drivers/usb/dwc3/dwc3-omap.c index 9596bf144c3..ff4ebfb4447 100644 --- a/drivers/usb/dwc3/dwc3-omap.c +++ b/drivers/usb/dwc3/dwc3-omap.c @@ -119,7 +119,7 @@ #define USBOTGSS_UTMI_OTG_STATUS_VBUSVALID (1 << 1)
struct dwc3_omap { - struct device *dev; + struct udevice *dev;
void __iomem *base;
@@ -429,19 +429,19 @@ void dwc3_omap_uboot_exit(int index)
/** * dwc3_omap_uboot_interrupt_status - check the status of interrupt - * @index: index of this controller + * @dev: device of this controller * * Checks the status of interrupts and returns true if an interrupt * is detected or false otherwise. * * Generally called from board file. */ -int dwc3_omap_uboot_interrupt_status(int index) +int dwc3_omap_uboot_interrupt_status(struct udevice *dev) { struct dwc3_omap *omap = NULL;
list_for_each_entry(omap, &dwc3_omap_list, list) - if (omap->index == index) + if (omap->dev == dev) return dwc3_omap_interrupt(-1, omap);
return 0; diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c index baa4c431fed..61d2aa5b929 100644 --- a/drivers/usb/gadget/at91_udc.c +++ b/drivers/usb/gadget/at91_udc.c @@ -1429,7 +1429,7 @@ static const struct at91_udc_caps at91sam9261_udc_caps = { }; #endif
-int usb_gadget_handle_interrupts(int index) +int dm_usb_gadget_handle_interrupts(struct udevice *dev) { struct at91_udc *udc = controller;
diff --git a/drivers/usb/gadget/atmel_usba_udc.c b/drivers/usb/gadget/atmel_usba_udc.c index f4dcd205a4f..90b248a0777 100644 --- a/drivers/usb/gadget/atmel_usba_udc.c +++ b/drivers/usb/gadget/atmel_usba_udc.c @@ -1198,14 +1198,13 @@ static struct usba_udc controller = { }, };
-int usb_gadget_handle_interrupts(int index) +int dm_usb_gadget_handle_interrupts(struct udevice *dev) { struct usba_udc *udc = &controller;
return usba_udc_irq(udc); }
- int usb_gadget_register_driver(struct usb_gadget_driver *driver) { struct usba_udc *udc = &controller; diff --git a/drivers/usb/gadget/ci_udc.c b/drivers/usb/gadget/ci_udc.c index 5bb26d7ab4a..44d2da0b4fb 100644 --- a/drivers/usb/gadget/ci_udc.c +++ b/drivers/usb/gadget/ci_udc.c @@ -869,10 +869,10 @@ void udc_irq(void) } }
-int usb_gadget_handle_interrupts(int index) +int dm_usb_gadget_handle_interrupts(struct udevice *dev) { - u32 value; struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor; + u32 value;
value = readl(&udc->usbsts); if (value) diff --git a/drivers/usb/gadget/dwc2_udc_otg.c b/drivers/usb/gadget/dwc2_udc_otg.c index 0a3bc0886e6..ff5173eda68 100644 --- a/drivers/usb/gadget/dwc2_udc_otg.c +++ b/drivers/usb/gadget/dwc2_udc_otg.c @@ -941,15 +941,12 @@ int dwc2_udc_handle_interrupt(void) return 0; }
-#if !CONFIG_IS_ENABLED(DM_USB_GADGET) - -int usb_gadget_handle_interrupts(int index) +int dm_usb_gadget_handle_interrupts(struct udevice *dev) { return dwc2_udc_handle_interrupt(); }
-#else /* CONFIG_IS_ENABLED(DM_USB_GADGET) */ - +#if CONFIG_IS_ENABLED(DM_USB_GADGET) struct dwc2_priv_data { struct clk_bulk clks; struct reset_ctl_bulk resets; @@ -957,11 +954,6 @@ struct dwc2_priv_data { struct udevice *usb33d_supply; };
-int dm_usb_gadget_handle_interrupts(struct udevice *dev) -{ - return dwc2_udc_handle_interrupt(); -} - static int dwc2_phy_setup(struct udevice *dev, struct phy_bulk *phys) { int ret; diff --git a/drivers/usb/gadget/udc/udc-uclass.c b/drivers/usb/gadget/udc/udc-uclass.c index 7f54a3b00cb..9dfae08313b 100644 --- a/drivers/usb/gadget/udc/udc-uclass.c +++ b/drivers/usb/gadget/udc/udc-uclass.c @@ -41,18 +41,6 @@ int udc_device_put(struct udevice *udev) return -ENOSYS; #endif } - -int usb_gadget_handle_interrupts(int index) -{ - struct udevice *udc; - int ret; - - ret = udc_device_get_by_index(index, &udc); - if (ret) - return ret; - - return dm_usb_gadget_handle_interrupts(udc); -} #else /* Backwards hardware compatibility -- switch to DM_USB_GADGET */ static int legacy_index; diff --git a/drivers/usb/host/usb-sandbox.c b/drivers/usb/host/usb-sandbox.c index 582f72d00c1..3d4f8d653b5 100644 --- a/drivers/usb/host/usb-sandbox.c +++ b/drivers/usb/host/usb-sandbox.c @@ -130,11 +130,6 @@ int dm_usb_gadget_handle_interrupts(struct udevice *dev) return 0; } #else -int usb_gadget_handle_interrupts(int index) -{ - return 0; -} - int usb_gadget_register_driver(struct usb_gadget_driver *driver) { struct sandbox_udc *dev = this_controller; diff --git a/drivers/usb/musb-new/musb_uboot.c b/drivers/usb/musb-new/musb_uboot.c index 67dc11669d2..0dcc428efeb 100644 --- a/drivers/usb/musb-new/musb_uboot.c +++ b/drivers/usb/musb-new/musb_uboot.c @@ -376,7 +376,7 @@ struct dm_usb_ops musb_usb_ops = { #if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET) static struct musb *gadget;
-int usb_gadget_handle_interrupts(int index) +int dm_usb_gadget_handle_interrupts(struct udevice *dev) { schedule(); if (!gadget || !gadget->isr) diff --git a/include/dwc3-omap-uboot.h b/include/dwc3-omap-uboot.h index 7c982e3798b..9e0e717dc98 100644 --- a/include/dwc3-omap-uboot.h +++ b/include/dwc3-omap-uboot.h @@ -27,5 +27,5 @@ struct dwc3_omap_device {
int dwc3_omap_uboot_init(struct dwc3_omap_device *dev); void dwc3_omap_uboot_exit(int index); -int dwc3_omap_uboot_interrupt_status(int index); +int dwc3_omap_uboot_interrupt_status(struct udevice *dev); #endif /* __DWC3_OMAP_UBOOT_H_ */ diff --git a/include/dwc3-uboot.h b/include/dwc3-uboot.h index e08530ec4e5..bb0436c0973 100644 --- a/include/dwc3-uboot.h +++ b/include/dwc3-uboot.h @@ -44,7 +44,7 @@ struct dwc3_device {
int dwc3_uboot_init(struct dwc3_device *dev); void dwc3_uboot_exit(int index); -void dwc3_uboot_handle_interrupt(int index); +void dwc3_uboot_handle_interrupt(struct udevice *dev);
struct phy; #if CONFIG_IS_ENABLED(PHY) && CONFIG_IS_ENABLED(DM_USB) diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h index 54875d2716e..40228e320b3 100644 --- a/include/linux/usb/gadget.h +++ b/include/linux/usb/gadget.h @@ -1004,7 +1004,7 @@ extern struct usb_ep *usb_ep_autoconfig(struct usb_gadget *,
extern void usb_ep_autoconfig_reset(struct usb_gadget *);
-extern int usb_gadget_handle_interrupts(int index); +extern int dm_usb_gadget_handle_interrupts(struct udevice *);
/** * udc_device_get_by_index() - Get UDC udevice by index

Hi Marek,
I'm answering here as there is no cover letter. Just to let you know I'm still concerned by the series and want to test it but did not had the time to do so recently. Hopefully next week.
Sorry for the delay. Miquèl
marex@denx.de wrote on Fri, 1 Sep 2023 11:49:47 +0200:
Pull the functionality of UDC uclass that operates on plain udevice and does not use this dev_array array into separate functions and expose those functions, so that as much code as possible can be switched over to these functions and the dev_array can be dropped.
Reviewed-by: Mattijs Korpershoek mkorpershoek@baylibre.com Signed-off-by: Marek Vasut marex@denx.de
Cc: Angus Ainslie angus@akkea.ca Cc: Dmitrii Merkurev dimorinny@google.com Cc: Eddie Cai eddie.cai.linux@gmail.com Cc: Kever Yang kever.yang@rock-chips.com Cc: Lukasz Majewski lukma@denx.de Cc: Miquel Raynal miquel.raynal@bootlin.com Cc: Mattijs Korpershoek mkorpershoek@baylibre.com Cc: Nishanth Menon nm@ti.com Cc: Patrice Chotard patrice.chotard@foss.st.com Cc: Patrick Delaunay patrick.delaunay@foss.st.com Cc: Philipp Tomsich philipp.tomsich@vrull.eu Cc: Simon Glass sjg@chromium.org Cc: Stefan Roese sr@denx.de Cc: kernel@puri.sm
V2: Add RB from Mattijs
drivers/usb/gadget/udc/Makefile | 2 +- drivers/usb/gadget/udc/udc-uclass.c | 57 +++++++++++++++++++++++++---- include/linux/usb/gadget.h | 17 +++++++++ 3 files changed, 68 insertions(+), 8 deletions(-)

Hi Marek,
miquel.raynal@bootlin.com wrote on Fri, 22 Sep 2023 12:00:12 +0200:
Hi Marek,
I'm answering here as there is no cover letter. Just to let you know I'm still concerned by the series and want to test it but did not had the time to do so recently. Hopefully next week.
The series looks good to me and works as well on a Beagle Bone Black with no visible functional changes regarding the use of the UDC. The whole series is:
Tested-by: Miquel Raynal miquel.raynal@bootlin.com
By the way, following your initial series there have been three followup patches trying to improve a little bit the doc, one got merged and two others were delegated to you: https://patchwork.ozlabs.org/project/uboot/list/?series=367635
They are almost 2 months old now, would you mind acking or merging them so both your initial USB gadget rework and the additional (related) doc can be in the same release please?
Thanks, Miquèl

On 9/27/23 15:59, Miquel Raynal wrote:
Hi Marek,
Hi,
miquel.raynal@bootlin.com wrote on Fri, 22 Sep 2023 12:00:12 +0200:
Hi Marek,
I'm answering here as there is no cover letter. Just to let you know I'm still concerned by the series and want to test it but did not had the time to do so recently. Hopefully next week.
The series looks good to me and works as well on a Beagle Bone Black with no visible functional changes regarding the use of the UDC. The whole series is:
Tested-by: Miquel Raynal miquel.raynal@bootlin.com
By the way, following your initial series there have been three followup patches trying to improve a little bit the doc, one got merged and two others were delegated to you: https://patchwork.ozlabs.org/project/uboot/list/?series=367635
They are almost 2 months old now, would you mind acking or merging them so both your initial USB gadget rework and the additional (related) doc can be in the same release please?
Can you resend those and CC this mail address ?
That said, 1/2 should be in some sort of README instead, and the 'dm tree' command depends on CMD_DM=y .
The usb_gadget_probe_driver() is code synchronized from kernel, you likely want to add any such clarification to usb_gadget_register_driver() . That said, the usb_gadget_register_driver() should be reworked into some new functions, which takes UDC controller *udevice pointer to avoid this binding heuristics.

Hi Marek,
marex@denx.de wrote on Sat, 30 Sep 2023 23:11:17 +0200:
On 9/27/23 15:59, Miquel Raynal wrote:
Hi Marek,
Hi,
miquel.raynal@bootlin.com wrote on Fri, 22 Sep 2023 12:00:12 +0200:
Hi Marek,
I'm answering here as there is no cover letter. Just to let you know I'm still concerned by the series and want to test it but did not had the time to do so recently. Hopefully next week.
The series looks good to me and works as well on a Beagle Bone Black with no visible functional changes regarding the use of the UDC. The whole series is:
Tested-by: Miquel Raynal miquel.raynal@bootlin.com
By the way, following your initial series there have been three followup patches trying to improve a little bit the doc, one got merged and two others were delegated to you: https://patchwork.ozlabs.org/project/uboot/list/?series=367635
They are almost 2 months old now, would you mind acking or merging them so both your initial USB gadget rework and the additional (related) doc can be in the same release please?
Can you resend those and CC this mail address ?
Of course!
That said, 1/2 should be in some sort of README instead, and the 'dm tree' command depends on CMD_DM=y .
Well, the README is more something which targets the developer IMO, whereas here I want to target the users. Many people only see U-Boot through Yocto now (that's a different topic) and the purpose of this additional text is to help them in finding their way into U-Boot device model *alone*. It's really short, but I bet it can really help, given how I felt when I actually understood why you advised to type a couple of semi-random bind/unbind commands which eventually worked. Anybody not involved in any DM conversion is likely just not aware of all of that. Having people finding their own way through the DM means less complaints/needs for help on the mailing list.
The usb_gadget_probe_driver() is code synchronized from kernel, you likely want to add any such clarification to usb_gadget_register_driver() .
Actually the kernel functions do have some kind of alternate error message as well now :-) Clearly different given that this code has been adapted to U-Boot's DM, but the idea is close. However usb_gadget_register_driver() is kind of blind regarding the number of UDCs in the system and if they are already bound or not, so I don't really see the point in moving the error message there along with all the logic duplication involved.
That said, the usb_gadget_register_driver() should be reworked into some new functions, which takes UDC controller *udevice pointer to avoid this binding heuristics.
Yes, I don't know many boards with two UDCs but it's a clear limitation.
Thanks, Miquèl

On 10/2/23 15:42, Miquel Raynal wrote:
Hi Marek,
marex@denx.de wrote on Sat, 30 Sep 2023 23:11:17 +0200:
On 9/27/23 15:59, Miquel Raynal wrote:
Hi Marek,
Hi,
miquel.raynal@bootlin.com wrote on Fri, 22 Sep 2023 12:00:12 +0200:
Hi Marek,
I'm answering here as there is no cover letter. Just to let you know I'm still concerned by the series and want to test it but did not had the time to do so recently. Hopefully next week.
The series looks good to me and works as well on a Beagle Bone Black with no visible functional changes regarding the use of the UDC. The whole series is:
Tested-by: Miquel Raynal miquel.raynal@bootlin.com
By the way, following your initial series there have been three followup patches trying to improve a little bit the doc, one got merged and two others were delegated to you: https://patchwork.ozlabs.org/project/uboot/list/?series=367635
They are almost 2 months old now, would you mind acking or merging them so both your initial USB gadget rework and the additional (related) doc can be in the same release please?
Can you resend those and CC this mail address ?
Of course!
That said, 1/2 should be in some sort of README instead, and the 'dm tree' command depends on CMD_DM=y .
Well, the README is more something which targets the developer IMO, whereas here I want to target the users. Many people only see U-Boot through Yocto now (that's a different topic) and the purpose of this additional text is to help them in finding their way into U-Boot device model *alone*. It's really short, but I bet it can really help, given how I felt when I actually understood why you advised to type a couple of semi-random bind/unbind commands which eventually worked. Anybody not involved in any DM conversion is likely just not aware of all of that. Having people finding their own way through the DM means less complaints/needs for help on the mailing list.
Thinking about it back and forth, I think you've got a point, yes.
The usb_gadget_probe_driver() is code synchronized from kernel, you likely want to add any such clarification to usb_gadget_register_driver() .
Actually the kernel functions do have some kind of alternate error message as well now :-) Clearly different given that this code has been adapted to U-Boot's DM, but the idea is close. However usb_gadget_register_driver() is kind of blind regarding the number of UDCs in the system and if they are already bound or not, so I don't really see the point in moving the error message there along with all the logic duplication involved.
That said, the usb_gadget_register_driver() should be reworked into some new functions, which takes UDC controller *udevice pointer to avoid this binding heuristics.
Yes, I don't know many boards with two UDCs but it's a clear limitation.
i.MX8M Plus is one such example, Quad I think is another, and pretty much anything with more than one DWC3 controller is. The CI HDRC is also often present in OTG capable configuration, so a lot of i.MXes are multi UDC systems.
participants (3)
-
Marek Vasut
-
Mattijs Korpershoek
-
Miquel Raynal