2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-07-28 12:09:15 +00:00
|
|
|
/*
|
|
|
|
* dfu.c -- dfu command
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015
|
|
|
|
* Lukasz Majewski <l.majewski@majess.pl>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Samsung Electronics
|
|
|
|
* authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
|
|
|
|
* Lukasz Majewski <l.majewski@samsung.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-05-10 17:40:03 +00:00
|
|
|
#include <command.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2016-07-28 12:09:15 +00:00
|
|
|
#include <watchdog.h>
|
|
|
|
#include <dfu.h>
|
|
|
|
#include <console.h>
|
|
|
|
#include <g_dnl.h>
|
|
|
|
#include <usb.h>
|
|
|
|
#include <net.h>
|
|
|
|
|
|
|
|
int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
|
|
|
|
{
|
|
|
|
bool dfu_reset = false;
|
|
|
|
int ret, i = 0;
|
|
|
|
|
2018-11-29 09:52:41 +00:00
|
|
|
ret = usb_gadget_initialize(usbctrl_index);
|
2016-08-30 13:32:17 +00:00
|
|
|
if (ret) {
|
2018-11-29 09:52:41 +00:00
|
|
|
pr_err("usb_gadget_initialize failed\n");
|
2016-08-30 13:32:17 +00:00
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
2016-07-28 12:09:15 +00:00
|
|
|
g_dnl_clear_detach();
|
2016-08-08 11:26:17 +00:00
|
|
|
ret = g_dnl_register(usb_dnl_gadget);
|
|
|
|
if (ret) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("g_dnl_register failed");
|
2016-08-08 11:26:17 +00:00
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:12:15 +00:00
|
|
|
#ifdef CONFIG_DFU_TIMEOUT
|
|
|
|
unsigned long start_time = get_timer(0);
|
|
|
|
#endif
|
|
|
|
|
2016-07-28 12:09:15 +00:00
|
|
|
while (1) {
|
|
|
|
if (g_dnl_detach()) {
|
|
|
|
/*
|
|
|
|
* Check if USB bus reset is performed after detach,
|
|
|
|
* which indicates that -R switch has been passed to
|
|
|
|
* dfu-util. In this case reboot the device
|
|
|
|
*/
|
|
|
|
if (dfu_usb_get_reset()) {
|
|
|
|
dfu_reset = true;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This extra number of usb_gadget_handle_interrupts()
|
|
|
|
* calls is necessary to assure correct transmission
|
|
|
|
* completion with dfu-util
|
|
|
|
*/
|
|
|
|
if (++i == 10000)
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctrlc())
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if (dfu_get_defer_flush()) {
|
|
|
|
/*
|
|
|
|
* Call to usb_gadget_handle_interrupts() is necessary
|
|
|
|
* to act on ZLP OUT transaction from HOST PC after
|
|
|
|
* transmitting the whole file.
|
|
|
|
*
|
|
|
|
* If this ZLP OUT packet is NAK'ed, the HOST libusb
|
|
|
|
* function fails after timeout (by default it is set to
|
|
|
|
* 5 seconds). In such situation the dfu-util program
|
|
|
|
* exits with error message.
|
|
|
|
*/
|
|
|
|
usb_gadget_handle_interrupts(usbctrl_index);
|
|
|
|
ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
|
|
|
|
dfu_set_defer_flush(NULL);
|
|
|
|
if (ret) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("Deferred dfu_flush() failed!");
|
2016-07-28 12:09:15 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:12:15 +00:00
|
|
|
#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
|
|
|
|
|
2016-07-28 12:09:15 +00:00
|
|
|
WATCHDOG_RESET();
|
|
|
|
usb_gadget_handle_interrupts(usbctrl_index);
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
g_dnl_unregister();
|
2018-11-29 09:52:41 +00:00
|
|
|
usb_gadget_release(usbctrl_index);
|
2016-07-28 12:09:15 +00:00
|
|
|
|
|
|
|
if (dfu_reset)
|
2017-05-04 10:15:29 +00:00
|
|
|
do_reset(NULL, 0, 0, NULL);
|
2016-07-28 12:09:15 +00:00
|
|
|
|
|
|
|
g_dnl_clear_detach();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|