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>
|
2023-09-15 00:21:46 +00:00
|
|
|
#include <linux/printk.h>
|
2016-07-28 12:09:15 +00:00
|
|
|
|
|
|
|
int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
|
|
|
|
{
|
|
|
|
bool dfu_reset = false;
|
2023-09-01 09:49:56 +00:00
|
|
|
struct udevice *udc;
|
2016-07-28 12:09:15 +00:00
|
|
|
int ret, i = 0;
|
|
|
|
|
2023-09-01 09:49:56 +00:00
|
|
|
ret = udc_device_get_by_index(usbctrl_index, &udc);
|
2016-08-30 13:32:17 +00:00
|
|
|
if (ret) {
|
2023-09-01 09:49:56 +00:00
|
|
|
pr_err("udc_device_get_by_index 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");
|
2023-09-01 09:49:55 +00:00
|
|
|
ret = CMD_RET_FAILURE;
|
|
|
|
goto err_detach;
|
2016-08-08 11:26:17 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-09-01 09:49:56 +00:00
|
|
|
* This extra number of dm_usb_gadget_handle_interrupts()
|
2016-07-28 12:09:15 +00:00
|
|
|
* 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()) {
|
|
|
|
/*
|
2023-09-01 09:49:56 +00:00
|
|
|
* Call to dm_usb_gadget_handle_interrupts() is necessary
|
2016-07-28 12:09:15 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2023-09-01 09:49:56 +00:00
|
|
|
dm_usb_gadget_handle_interrupts(udc);
|
2016-07-28 12:09:15 +00:00
|
|
|
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
|
|
|
|
|
2020-12-22 10:32:23 +00:00
|
|
|
if (dfu_reinit_needed)
|
|
|
|
goto exit;
|
|
|
|
|
2022-09-02 12:10:46 +00:00
|
|
|
schedule();
|
2023-09-01 09:49:56 +00:00
|
|
|
dm_usb_gadget_handle_interrupts(udc);
|
2016-07-28 12:09:15 +00:00
|
|
|
}
|
|
|
|
exit:
|
|
|
|
g_dnl_unregister();
|
2023-09-01 09:49:55 +00:00
|
|
|
err_detach:
|
2023-09-01 09:49:56 +00:00
|
|
|
udc_device_put(udc);
|
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;
|
|
|
|
}
|