5 * Lukasz Majewski <l.majewski@majess.pl>
7 * Copyright (C) 2012 Samsung Electronics
8 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
9 * Lukasz Majewski <l.majewski@samsung.com>
11 * SPDX-License-Identifier: GPL-2.0+
22 int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
24 bool dfu_reset = false;
27 ret = board_usb_init(usbctrl_index, USB_INIT_DEVICE);
29 pr_err("board usb init failed\n");
30 return CMD_RET_FAILURE;
33 ret = g_dnl_register(usb_dnl_gadget);
35 pr_err("g_dnl_register failed");
36 return CMD_RET_FAILURE;
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
46 if (dfu_usb_get_reset()) {
52 * This extra number of usb_gadget_handle_interrupts()
53 * calls is necessary to assure correct transmission
54 * completion with dfu-util
63 if (dfu_get_defer_flush()) {
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.
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.
74 usb_gadget_handle_interrupts(usbctrl_index);
75 ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
76 dfu_set_defer_flush(NULL);
78 pr_err("Deferred dfu_flush() failed!");
84 usb_gadget_handle_interrupts(usbctrl_index);
88 board_usb_cleanup(usbctrl_index, USB_INIT_DEVICE);
91 do_reset(NULL, 0, 0, NULL);