blob: 4c529f7c36f909a2797b92478653aa2bbde6899d [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
27 board_usb_init(usbctrl_index, USB_INIT_DEVICE);
28 g_dnl_clear_detach();
Sanchayan Maity54a708c2016-08-08 16:56:17 +053029 ret = g_dnl_register(usb_dnl_gadget);
30 if (ret) {
31 error("g_dnl_register failed");
32 return CMD_RET_FAILURE;
33 }
34
B, Ravi05341a82016-07-28 17:39:15 +053035 while (1) {
36 if (g_dnl_detach()) {
37 /*
38 * Check if USB bus reset is performed after detach,
39 * which indicates that -R switch has been passed to
40 * dfu-util. In this case reboot the device
41 */
42 if (dfu_usb_get_reset()) {
43 dfu_reset = true;
44 goto exit;
45 }
46
47 /*
48 * This extra number of usb_gadget_handle_interrupts()
49 * calls is necessary to assure correct transmission
50 * completion with dfu-util
51 */
52 if (++i == 10000)
53 goto exit;
54 }
55
56 if (ctrlc())
57 goto exit;
58
59 if (dfu_get_defer_flush()) {
60 /*
61 * Call to usb_gadget_handle_interrupts() is necessary
62 * to act on ZLP OUT transaction from HOST PC after
63 * transmitting the whole file.
64 *
65 * If this ZLP OUT packet is NAK'ed, the HOST libusb
66 * function fails after timeout (by default it is set to
67 * 5 seconds). In such situation the dfu-util program
68 * exits with error message.
69 */
70 usb_gadget_handle_interrupts(usbctrl_index);
71 ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
72 dfu_set_defer_flush(NULL);
73 if (ret) {
74 error("Deferred dfu_flush() failed!");
75 goto exit;
76 }
77 }
78
79 WATCHDOG_RESET();
80 usb_gadget_handle_interrupts(usbctrl_index);
81 }
82exit:
83 g_dnl_unregister();
84 board_usb_cleanup(usbctrl_index, USB_INIT_DEVICE);
85
86 if (dfu_reset)
87 run_command("reset", 0);
88
89 g_dnl_clear_detach();
90
91 return ret;
92}