1 // SPDX-License-Identifier: GPL-2.0+
6 * Lukasz Majewski <l.majewski@majess.pl>
8 * Copyright (C) 2012 Samsung Electronics
9 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
10 * Lukasz Majewski <l.majewski@samsung.com>
23 int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
25 bool dfu_reset = false;
28 ret = usb_gadget_initialize(usbctrl_index);
30 pr_err("usb_gadget_initialize failed\n");
31 return CMD_RET_FAILURE;
34 ret = g_dnl_register(usb_dnl_gadget);
36 pr_err("g_dnl_register failed");
37 return CMD_RET_FAILURE;
40 #ifdef CONFIG_DFU_TIMEOUT
41 unsigned long start_time = get_timer(0);
47 * Check if USB bus reset is performed after detach,
48 * which indicates that -R switch has been passed to
49 * dfu-util. In this case reboot the device
51 if (dfu_usb_get_reset()) {
57 * This extra number of usb_gadget_handle_interrupts()
58 * calls is necessary to assure correct transmission
59 * completion with dfu-util
68 if (dfu_get_defer_flush()) {
70 * Call to usb_gadget_handle_interrupts() is necessary
71 * to act on ZLP OUT transaction from HOST PC after
72 * transmitting the whole file.
74 * If this ZLP OUT packet is NAK'ed, the HOST libusb
75 * function fails after timeout (by default it is set to
76 * 5 seconds). In such situation the dfu-util program
77 * exits with error message.
79 usb_gadget_handle_interrupts(usbctrl_index);
80 ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
81 dfu_set_defer_flush(NULL);
83 pr_err("Deferred dfu_flush() failed!");
88 #ifdef CONFIG_DFU_TIMEOUT
89 unsigned long wait_time = dfu_get_timeout();
92 unsigned long current_time = get_timer(start_time);
94 if (current_time > wait_time) {
95 debug("Inactivity timeout, abort DFU\n");
101 if (dfu_reinit_needed)
105 usb_gadget_handle_interrupts(usbctrl_index);
109 usb_gadget_release(usbctrl_index);
112 do_reset(NULL, 0, 0, NULL);
114 g_dnl_clear_detach();