]> Git Repo - J-u-boot.git/blame - common/dfu.c
Merge https://source.denx.de/u-boot/custodians/u-boot-watchdog
[J-u-boot.git] / common / dfu.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
05341a87
R
2/*
3 * dfu.c -- dfu command
4 *
5 * Copyright (C) 2015
6 * Lukasz Majewski <[email protected]>
7 *
8 * Copyright (C) 2012 Samsung Electronics
9 * authors: Andrzej Pietrasiewicz <[email protected]>
10 * Lukasz Majewski <[email protected]>
05341a87
R
11 */
12
09140113 13#include <command.h>
f7ae49fc 14#include <log.h>
05341a87
R
15#include <watchdog.h>
16#include <dfu.h>
17#include <console.h>
18#include <g_dnl.h>
19#include <usb.h>
20#include <net.h>
1e94b46f 21#include <linux/printk.h>
05341a87
R
22
23int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
24{
25 bool dfu_reset = false;
2dfae175 26 struct udevice *udc;
05341a87
R
27 int ret, i = 0;
28
2dfae175 29 ret = udc_device_get_by_index(usbctrl_index, &udc);
dbdc2744 30 if (ret) {
2dfae175 31 pr_err("udc_device_get_by_index failed\n");
dbdc2744
MS
32 return CMD_RET_FAILURE;
33 }
05341a87 34 g_dnl_clear_detach();
54a708ca
SM
35 ret = g_dnl_register(usb_dnl_gadget);
36 if (ret) {
9b643e31 37 pr_err("g_dnl_register failed");
edd6042d
MV
38 ret = CMD_RET_FAILURE;
39 goto err_detach;
54a708ca
SM
40 }
41
98a8f445
AS
42#ifdef CONFIG_DFU_TIMEOUT
43 unsigned long start_time = get_timer(0);
44#endif
45
05341a87
R
46 while (1) {
47 if (g_dnl_detach()) {
48 /*
49 * Check if USB bus reset is performed after detach,
50 * which indicates that -R switch has been passed to
51 * dfu-util. In this case reboot the device
52 */
53 if (dfu_usb_get_reset()) {
54 dfu_reset = true;
55 goto exit;
56 }
57
58 /*
2dfae175 59 * This extra number of dm_usb_gadget_handle_interrupts()
05341a87
R
60 * calls is necessary to assure correct transmission
61 * completion with dfu-util
62 */
63 if (++i == 10000)
64 goto exit;
65 }
66
67 if (ctrlc())
68 goto exit;
69
70 if (dfu_get_defer_flush()) {
71 /*
2dfae175 72 * Call to dm_usb_gadget_handle_interrupts() is necessary
05341a87
R
73 * to act on ZLP OUT transaction from HOST PC after
74 * transmitting the whole file.
75 *
76 * If this ZLP OUT packet is NAK'ed, the HOST libusb
77 * function fails after timeout (by default it is set to
78 * 5 seconds). In such situation the dfu-util program
79 * exits with error message.
80 */
2dfae175 81 dm_usb_gadget_handle_interrupts(udc);
05341a87
R
82 ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
83 dfu_set_defer_flush(NULL);
84 if (ret) {
9b643e31 85 pr_err("Deferred dfu_flush() failed!");
05341a87
R
86 goto exit;
87 }
88 }
89
98a8f445
AS
90#ifdef CONFIG_DFU_TIMEOUT
91 unsigned long wait_time = dfu_get_timeout();
92
93 if (wait_time) {
94 unsigned long current_time = get_timer(start_time);
95
96 if (current_time > wait_time) {
97 debug("Inactivity timeout, abort DFU\n");
98 goto exit;
99 }
100 }
101#endif
102
9129f2f1
MS
103 if (dfu_reinit_needed)
104 goto exit;
105
29caf930 106 schedule();
2dfae175 107 dm_usb_gadget_handle_interrupts(udc);
05341a87
R
108 }
109exit:
110 g_dnl_unregister();
edd6042d 111err_detach:
2dfae175 112 udc_device_put(udc);
05341a87
R
113
114 if (dfu_reset)
66928afb 115 do_reset(NULL, 0, 0, NULL);
05341a87
R
116
117 g_dnl_clear_detach();
118
119 return ret;
120}
This page took 0.239146 seconds and 4 git commands to generate.