blob: 16bd1ba588ad5f02a97342710fec9b1eca2354de [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
B, Ravi603a4c12016-07-28 17:39:15 +05302/*
3 * dfu.c -- dfu command
4 *
5 * Copyright (C) 2015
6 * Lukasz Majewski <l.majewski@majess.pl>
7 *
8 * Copyright (C) 2012 Samsung Electronics
9 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
10 * Lukasz Majewski <l.majewski@samsung.com>
B, Ravi603a4c12016-07-28 17:39:15 +053011 */
12
13#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -060014#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
B, Ravi603a4c12016-07-28 17:39:15 +053016#include <watchdog.h>
17#include <dfu.h>
18#include <console.h>
19#include <g_dnl.h>
20#include <usb.h>
21#include <net.h>
22
23int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
24{
25 bool dfu_reset = false;
26 int ret, i = 0;
27
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +010028 ret = usb_gadget_initialize(usbctrl_index);
Michal Simekc809a972016-08-30 15:32:17 +020029 if (ret) {
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +010030 pr_err("usb_gadget_initialize failed\n");
Michal Simekc809a972016-08-30 15:32:17 +020031 return CMD_RET_FAILURE;
32 }
B, Ravi603a4c12016-07-28 17:39:15 +053033 g_dnl_clear_detach();
Sanchayan Maity06860662016-08-08 16:56:17 +053034 ret = g_dnl_register(usb_dnl_gadget);
35 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090036 pr_err("g_dnl_register failed");
Sanchayan Maity06860662016-08-08 16:56:17 +053037 return CMD_RET_FAILURE;
38 }
39
Andy Shevchenko1e414012019-11-27 18:12:15 +020040#ifdef CONFIG_DFU_TIMEOUT
41 unsigned long start_time = get_timer(0);
42#endif
43
B, Ravi603a4c12016-07-28 17:39:15 +053044 while (1) {
45 if (g_dnl_detach()) {
46 /*
47 * Check if USB bus reset is performed after detach,
48 * which indicates that -R switch has been passed to
49 * dfu-util. In this case reboot the device
50 */
51 if (dfu_usb_get_reset()) {
52 dfu_reset = true;
53 goto exit;
54 }
55
56 /*
57 * This extra number of usb_gadget_handle_interrupts()
58 * calls is necessary to assure correct transmission
59 * completion with dfu-util
60 */
61 if (++i == 10000)
62 goto exit;
63 }
64
65 if (ctrlc())
66 goto exit;
67
68 if (dfu_get_defer_flush()) {
69 /*
70 * Call to usb_gadget_handle_interrupts() is necessary
71 * to act on ZLP OUT transaction from HOST PC after
72 * transmitting the whole file.
73 *
74 * If this ZLP OUT packet is NAK'ed, the HOST libusb
75 * function fails after timeout (by default it is set to
76 * 5 seconds). In such situation the dfu-util program
77 * exits with error message.
78 */
79 usb_gadget_handle_interrupts(usbctrl_index);
80 ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
81 dfu_set_defer_flush(NULL);
82 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090083 pr_err("Deferred dfu_flush() failed!");
B, Ravi603a4c12016-07-28 17:39:15 +053084 goto exit;
85 }
86 }
Andy Shevchenko1e414012019-11-27 18:12:15 +020087
88#ifdef CONFIG_DFU_TIMEOUT
89 unsigned long wait_time = dfu_get_timeout();
90
91 if (wait_time) {
92 unsigned long current_time = get_timer(start_time);
93
94 if (current_time > wait_time) {
95 debug("Inactivity timeout, abort DFU\n");
96 goto exit;
97 }
98 }
99#endif
B, Ravi603a4c12016-07-28 17:39:15 +0530100
Marek Szyprowskif5af8e42020-12-22 11:32:23 +0100101 if (dfu_reinit_needed)
102 goto exit;
103
B, Ravi603a4c12016-07-28 17:39:15 +0530104 WATCHDOG_RESET();
105 usb_gadget_handle_interrupts(usbctrl_index);
106 }
107exit:
108 g_dnl_unregister();
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +0100109 usb_gadget_release(usbctrl_index);
B, Ravi603a4c12016-07-28 17:39:15 +0530110
111 if (dfu_reset)
B, Ravi4cca5cf2017-05-04 15:45:29 +0530112 do_reset(NULL, 0, 0, NULL);
B, Ravi603a4c12016-07-28 17:39:15 +0530113
114 g_dnl_clear_detach();
115
116 return ret;
117}