blob: 0d154e8d4c482a0f48dcf311cbef17eacfade4b8 [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>
Simon Glassbdd5f812023-09-14 18:21:46 -060022#include <linux/printk.h>
B, Ravi603a4c12016-07-28 17:39:15 +053023
24int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
25{
26 bool dfu_reset = false;
Marek Vasutce2f2d92023-09-01 11:49:56 +020027 struct udevice *udc;
B, Ravi603a4c12016-07-28 17:39:15 +053028 int ret, i = 0;
29
Marek Vasutce2f2d92023-09-01 11:49:56 +020030 ret = udc_device_get_by_index(usbctrl_index, &udc);
Michal Simekc809a972016-08-30 15:32:17 +020031 if (ret) {
Marek Vasutce2f2d92023-09-01 11:49:56 +020032 pr_err("udc_device_get_by_index failed\n");
Michal Simekc809a972016-08-30 15:32:17 +020033 return CMD_RET_FAILURE;
34 }
B, Ravi603a4c12016-07-28 17:39:15 +053035 g_dnl_clear_detach();
Sanchayan Maity06860662016-08-08 16:56:17 +053036 ret = g_dnl_register(usb_dnl_gadget);
37 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090038 pr_err("g_dnl_register failed");
Marek Vasut61d5d9a2023-09-01 11:49:55 +020039 ret = CMD_RET_FAILURE;
40 goto err_detach;
Sanchayan Maity06860662016-08-08 16:56:17 +053041 }
42
Andy Shevchenko1e414012019-11-27 18:12:15 +020043#ifdef CONFIG_DFU_TIMEOUT
44 unsigned long start_time = get_timer(0);
45#endif
46
B, Ravi603a4c12016-07-28 17:39:15 +053047 while (1) {
48 if (g_dnl_detach()) {
49 /*
50 * Check if USB bus reset is performed after detach,
51 * which indicates that -R switch has been passed to
52 * dfu-util. In this case reboot the device
53 */
54 if (dfu_usb_get_reset()) {
55 dfu_reset = true;
56 goto exit;
57 }
58
59 /*
Marek Vasutce2f2d92023-09-01 11:49:56 +020060 * This extra number of dm_usb_gadget_handle_interrupts()
B, Ravi603a4c12016-07-28 17:39:15 +053061 * calls is necessary to assure correct transmission
62 * completion with dfu-util
63 */
64 if (++i == 10000)
65 goto exit;
66 }
67
68 if (ctrlc())
69 goto exit;
70
71 if (dfu_get_defer_flush()) {
72 /*
Marek Vasutce2f2d92023-09-01 11:49:56 +020073 * Call to dm_usb_gadget_handle_interrupts() is necessary
B, Ravi603a4c12016-07-28 17:39:15 +053074 * to act on ZLP OUT transaction from HOST PC after
75 * transmitting the whole file.
76 *
77 * If this ZLP OUT packet is NAK'ed, the HOST libusb
78 * function fails after timeout (by default it is set to
79 * 5 seconds). In such situation the dfu-util program
80 * exits with error message.
81 */
Marek Vasutce2f2d92023-09-01 11:49:56 +020082 dm_usb_gadget_handle_interrupts(udc);
B, Ravi603a4c12016-07-28 17:39:15 +053083 ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
84 dfu_set_defer_flush(NULL);
85 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090086 pr_err("Deferred dfu_flush() failed!");
B, Ravi603a4c12016-07-28 17:39:15 +053087 goto exit;
88 }
89 }
Andy Shevchenko1e414012019-11-27 18:12:15 +020090
91#ifdef CONFIG_DFU_TIMEOUT
92 unsigned long wait_time = dfu_get_timeout();
93
94 if (wait_time) {
95 unsigned long current_time = get_timer(start_time);
96
97 if (current_time > wait_time) {
98 debug("Inactivity timeout, abort DFU\n");
99 goto exit;
100 }
101 }
102#endif
B, Ravi603a4c12016-07-28 17:39:15 +0530103
Marek Szyprowskif5af8e42020-12-22 11:32:23 +0100104 if (dfu_reinit_needed)
105 goto exit;
106
Stefan Roese80877fa2022-09-02 14:10:46 +0200107 schedule();
Marek Vasutce2f2d92023-09-01 11:49:56 +0200108 dm_usb_gadget_handle_interrupts(udc);
B, Ravi603a4c12016-07-28 17:39:15 +0530109 }
110exit:
111 g_dnl_unregister();
Marek Vasut61d5d9a2023-09-01 11:49:55 +0200112err_detach:
Marek Vasutce2f2d92023-09-01 11:49:56 +0200113 udc_device_put(udc);
B, Ravi603a4c12016-07-28 17:39:15 +0530114
115 if (dfu_reset)
B, Ravi4cca5cf2017-05-04 15:45:29 +0530116 do_reset(NULL, 0, 0, NULL);
B, Ravi603a4c12016-07-28 17:39:15 +0530117
118 g_dnl_clear_detach();
119
120 return ret;
121}