Merge git://git.denx.de/u-boot-sh
[oweals/u-boot.git] / common / dfu.c
index 2620d3238b1613b7fa367d1f84160f93625e11b4..d23cf67f1942447b9f7f0842ca40623cbfb5db69 100644 (file)
@@ -11,6 +11,8 @@
  */
 
 #include <common.h>
+#include <command.h>
+#include <log.h>
 #include <watchdog.h>
 #include <dfu.h>
 #include <console.h>
@@ -23,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();
@@ -35,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()) {
                        /*
@@ -79,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);