blob: 0d154e8d4c482a0f48dcf311cbef17eacfade4b8 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
B, Ravi05341a82016-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, Ravi05341a82016-07-28 17:39:15 +053011 */
12
13#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060014#include <command.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
B, Ravi05341a82016-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 Glass1e94b462023-09-14 18:21:46 -060022#include <linux/printk.h>
B, Ravi05341a82016-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 Vasut2dfae172023-09-01 11:49:56 +020027 struct udevice *udc;
B, Ravi05341a82016-07-28 17:39:15 +053028 int ret, i = 0;
29
Marek Vasut2dfae172023-09-01 11:49:56 +020030 ret = udc_device_get_by_index(usbctrl_index, &udc);
Michal Simekdbdc2742016-08-30 15:32:17 +020031 if (ret) {
Marek Vasut2dfae172023-09-01 11:49:56 +020032 pr_err("udc_device_get_by_index failed\n");
Michal Simekdbdc2742016-08-30 15:32:17 +020033 return CMD_RET_FAILURE;
34 }
B, Ravi05341a82016-07-28 17:39:15 +053035 g_dnl_clear_detach();
Sanchayan Maity54a708c2016-08-08 16:56:17 +053036 ret = g_dnl_register(usb_dnl_gadget);
37 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090038 pr_err("g_dnl_register failed");
Marek Vasutedd60422023-09-01 11:49:55 +020039 ret = CMD_RET_FAILURE;
40 goto err_detach;
Sanchayan Maity54a708c2016-08-08 16:56:17 +053041 }
42
Andy Shevchenko98a8f442019-11-27 18:12:15 +020043#ifdef CONFIG_DFU_TIMEOUT
44 unsigned long start_time = get_timer(0);
45#endif
46
B, Ravi05341a82016-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 Vasut2dfae172023-09-01 11:49:56 +020060 * This extra number of dm_usb_gadget_handle_interrupts()
B, Ravi05341a82016-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 Vasut2dfae172023-09-01 11:49:56 +020073 * Call to dm_usb_gadget_handle_interrupts() is necessary
B, Ravi05341a82016-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 Vasut2dfae172023-09-01 11:49:56 +020082 dm_usb_gadget_handle_interrupts(udc);
B, Ravi05341a82016-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 Yamada9b643e32017-09-16 14:10:41 +090086 pr_err("Deferred dfu_flush() failed!");
B, Ravi05341a82016-07-28 17:39:15 +053087 goto exit;
88 }
89 }
90
Andy Shevchenko98a8f442019-11-27 18:12:15 +020091#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
103
Marek Szyprowski9129f2f2020-12-22 11:32:23 +0100104 if (dfu_reinit_needed)
105 goto exit;
106
Stefan Roese29caf932022-09-02 14:10:46 +0200107 schedule();
Marek Vasut2dfae172023-09-01 11:49:56 +0200108 dm_usb_gadget_handle_interrupts(udc);
B, Ravi05341a82016-07-28 17:39:15 +0530109 }
110exit:
111 g_dnl_unregister();
Marek Vasutedd60422023-09-01 11:49:55 +0200112err_detach:
Marek Vasut2dfae172023-09-01 11:49:56 +0200113 udc_device_put(udc);
B, Ravi05341a82016-07-28 17:39:15 +0530114
115 if (dfu_reset)
B, Ravi66928af2017-05-04 15:45:29 +0530116 do_reset(NULL, 0, 0, NULL);
B, Ravi05341a82016-07-28 17:39:15 +0530117
118 g_dnl_clear_detach();
119
120 return ret;
121}