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>
21 int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
23 bool dfu_reset = false;
26 ret = board_usb_init(usbctrl_index, USB_INIT_DEVICE);
28 pr_err("board usb init failed\n");
29 return CMD_RET_FAILURE;
32 ret = g_dnl_register(usb_dnl_gadget);
34 pr_err("g_dnl_register failed");
35 return CMD_RET_FAILURE;
41 * Check if USB bus reset is performed after detach,
42 * which indicates that -R switch has been passed to
43 * dfu-util. In this case reboot the device
45 if (dfu_usb_get_reset()) {
51 * This extra number of usb_gadget_handle_interrupts()
52 * calls is necessary to assure correct transmission
53 * completion with dfu-util
62 if (dfu_get_defer_flush()) {
64 * Call to usb_gadget_handle_interrupts() is necessary
65 * to act on ZLP OUT transaction from HOST PC after
66 * transmitting the whole file.
68 * If this ZLP OUT packet is NAK'ed, the HOST libusb
69 * function fails after timeout (by default it is set to
70 * 5 seconds). In such situation the dfu-util program
71 * exits with error message.
73 usb_gadget_handle_interrupts(usbctrl_index);
74 ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
75 dfu_set_defer_flush(NULL);
77 pr_err("Deferred dfu_flush() failed!");
83 usb_gadget_handle_interrupts(usbctrl_index);
87 board_usb_cleanup(usbctrl_index, USB_INIT_DEVICE);
90 do_reset(NULL, 0, 0, NULL);