blob: 1af8194139c336389d9a331e8873022d13ef1ab4 [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
Simon Glassed38aef2020-05-10 11:40:03 -060013#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
B, Ravi603a4c12016-07-28 17:39:15 +053015#include <watchdog.h>
16#include <dfu.h>
17#include <console.h>
18#include <g_dnl.h>
19#include <usb.h>
20#include <net.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060021#include <linux/printk.h>
B, Ravi603a4c12016-07-28 17:39:15 +053022
23int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
24{
25 bool dfu_reset = false;
Marek Vasutce2f2d92023-09-01 11:49:56 +020026 struct udevice *udc;
B, Ravi603a4c12016-07-28 17:39:15 +053027 int ret, i = 0;
28
Marek Vasutce2f2d92023-09-01 11:49:56 +020029 ret = udc_device_get_by_index(usbctrl_index, &udc);
Michal Simekc809a972016-08-30 15:32:17 +020030 if (ret) {
Marek Vasutce2f2d92023-09-01 11:49:56 +020031 pr_err("udc_device_get_by_index failed\n");
Michal Simekc809a972016-08-30 15:32:17 +020032 return CMD_RET_FAILURE;
33 }
B, Ravi603a4c12016-07-28 17:39:15 +053034 g_dnl_clear_detach();
Sanchayan Maity06860662016-08-08 16:56:17 +053035 ret = g_dnl_register(usb_dnl_gadget);
36 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090037 pr_err("g_dnl_register failed");
Marek Vasut61d5d9a2023-09-01 11:49:55 +020038 ret = CMD_RET_FAILURE;
39 goto err_detach;
Sanchayan Maity06860662016-08-08 16:56:17 +053040 }
41
Andy Shevchenko1e414012019-11-27 18:12:15 +020042#ifdef CONFIG_DFU_TIMEOUT
43 unsigned long start_time = get_timer(0);
44#endif
45
B, Ravi603a4c12016-07-28 17:39:15 +053046 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 /*
Marek Vasutce2f2d92023-09-01 11:49:56 +020059 * This extra number of dm_usb_gadget_handle_interrupts()
B, Ravi603a4c12016-07-28 17:39:15 +053060 * 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 /*
Marek Vasutce2f2d92023-09-01 11:49:56 +020072 * Call to dm_usb_gadget_handle_interrupts() is necessary
B, Ravi603a4c12016-07-28 17:39:15 +053073 * 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 */
Marek Vasutce2f2d92023-09-01 11:49:56 +020081 dm_usb_gadget_handle_interrupts(udc);
B, Ravi603a4c12016-07-28 17:39:15 +053082 ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
83 dfu_set_defer_flush(NULL);
84 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090085 pr_err("Deferred dfu_flush() failed!");
B, Ravi603a4c12016-07-28 17:39:15 +053086 goto exit;
87 }
88 }
Andy Shevchenko1e414012019-11-27 18:12:15 +020089
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
B, Ravi603a4c12016-07-28 17:39:15 +0530102
Marek Szyprowskif5af8e42020-12-22 11:32:23 +0100103 if (dfu_reinit_needed)
104 goto exit;
105
Stefan Roese80877fa2022-09-02 14:10:46 +0200106 schedule();
Marek Vasutce2f2d92023-09-01 11:49:56 +0200107 dm_usb_gadget_handle_interrupts(udc);
B, Ravi603a4c12016-07-28 17:39:15 +0530108 }
109exit:
110 g_dnl_unregister();
Marek Vasut61d5d9a2023-09-01 11:49:55 +0200111err_detach:
Marek Vasutce2f2d92023-09-01 11:49:56 +0200112 udc_device_put(udc);
B, Ravi603a4c12016-07-28 17:39:15 +0530113
114 if (dfu_reset)
B, Ravi4cca5cf2017-05-04 15:45:29 +0530115 do_reset(NULL, 0, 0, NULL);
B, Ravi603a4c12016-07-28 17:39:15 +0530116
117 g_dnl_clear_detach();
118
119 return ret;
120}