blob: 07dff317a6c2708bb7f0deff133bae7f3bd8e11c [file] [log] [blame]
B, Ravi05341a82016-07-28 17:39:15 +05301/*
2 * dfu.c -- dfu command
3 *
4 * Copyright (C) 2015
5 * Lukasz Majewski <l.majewski@majess.pl>
6 *
7 * Copyright (C) 2012 Samsung Electronics
8 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
9 * Lukasz Majewski <l.majewski@samsung.com>
10 *
11 * SPDX-License-Identifier: GPL-2.0+
12 */
13
14#include <common.h>
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>
21
22int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
23{
24 bool dfu_reset = false;
25 int ret, i = 0;
26
Michal Simekdbdc2742016-08-30 15:32:17 +020027 ret = board_usb_init(usbctrl_index, USB_INIT_DEVICE);
28 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090029 pr_err("board usb init failed\n");
Michal Simekdbdc2742016-08-30 15:32:17 +020030 return CMD_RET_FAILURE;
31 }
B, Ravi05341a82016-07-28 17:39:15 +053032 g_dnl_clear_detach();
Sanchayan Maity54a708c2016-08-08 16:56:17 +053033 ret = g_dnl_register(usb_dnl_gadget);
34 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090035 pr_err("g_dnl_register failed");
Sanchayan Maity54a708c2016-08-08 16:56:17 +053036 return CMD_RET_FAILURE;
37 }
38
B, Ravi05341a82016-07-28 17:39:15 +053039 while (1) {
40 if (g_dnl_detach()) {
41 /*
42 * Check if USB bus reset is performed after detach,
43 * which indicates that -R switch has been passed to
44 * dfu-util. In this case reboot the device
45 */
46 if (dfu_usb_get_reset()) {
47 dfu_reset = true;
48 goto exit;
49 }
50
51 /*
52 * This extra number of usb_gadget_handle_interrupts()
53 * calls is necessary to assure correct transmission
54 * completion with dfu-util
55 */
56 if (++i == 10000)
57 goto exit;
58 }
59
60 if (ctrlc())
61 goto exit;
62
63 if (dfu_get_defer_flush()) {
64 /*
65 * Call to usb_gadget_handle_interrupts() is necessary
66 * to act on ZLP OUT transaction from HOST PC after
67 * transmitting the whole file.
68 *
69 * If this ZLP OUT packet is NAK'ed, the HOST libusb
70 * function fails after timeout (by default it is set to
71 * 5 seconds). In such situation the dfu-util program
72 * exits with error message.
73 */
74 usb_gadget_handle_interrupts(usbctrl_index);
75 ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
76 dfu_set_defer_flush(NULL);
77 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090078 pr_err("Deferred dfu_flush() failed!");
B, Ravi05341a82016-07-28 17:39:15 +053079 goto exit;
80 }
81 }
82
83 WATCHDOG_RESET();
84 usb_gadget_handle_interrupts(usbctrl_index);
85 }
86exit:
87 g_dnl_unregister();
88 board_usb_cleanup(usbctrl_index, USB_INIT_DEVICE);
89
90 if (dfu_reset)
B, Ravi66928af2017-05-04 15:45:29 +053091 do_reset(NULL, 0, 0, NULL);
B, Ravi05341a82016-07-28 17:39:15 +053092
93 g_dnl_clear_detach();
94
95 return ret;
96}