Merge git://git.denx.de/u-boot-sh
[oweals/u-boot.git] / common / dfu.c
index 07dff317a6c2708bb7f0deff133bae7f3bd8e11c..d23cf67f1942447b9f7f0842ca40623cbfb5db69 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * dfu.c -- dfu command
  *
@@ -7,11 +8,11 @@
  * Copyright (C) 2012 Samsung Electronics
  * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  *         Lukasz Majewski <l.majewski@samsung.com>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
+#include <command.h>
+#include <log.h>
 #include <watchdog.h>
 #include <dfu.h>
 #include <console.h>
@@ -24,9 +25,9 @@ int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
        bool dfu_reset = false;
        int ret, i = 0;
 
-       ret = board_usb_init(usbctrl_index, USB_INIT_DEVICE);
+       ret = usb_gadget_initialize(usbctrl_index);
        if (ret) {
-               pr_err("board usb init failed\n");
+               pr_err("usb_gadget_initialize failed\n");
                return CMD_RET_FAILURE;
        }
        g_dnl_clear_detach();
@@ -36,6 +37,10 @@ int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
                return CMD_RET_FAILURE;
        }
 
+#ifdef CONFIG_DFU_TIMEOUT
+       unsigned long start_time = get_timer(0);
+#endif
+
        while (1) {
                if (g_dnl_detach()) {
                        /*
@@ -80,12 +85,25 @@ int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
                        }
                }
 
+#ifdef CONFIG_DFU_TIMEOUT
+               unsigned long wait_time = dfu_get_timeout();
+
+               if (wait_time) {
+                       unsigned long current_time = get_timer(start_time);
+
+                       if (current_time > wait_time) {
+                               debug("Inactivity timeout, abort DFU\n");
+                               goto exit;
+                       }
+               }
+#endif
+
                WATCHDOG_RESET();
                usb_gadget_handle_interrupts(usbctrl_index);
        }
 exit:
        g_dnl_unregister();
-       board_usb_cleanup(usbctrl_index, USB_INIT_DEVICE);
+       usb_gadget_release(usbctrl_index);
 
        if (dfu_reset)
                do_reset(NULL, 0, 0, NULL);