1 // SPDX-License-Identifier: GPL-2.0+
8 * Copyright (C) 2012 Samsung Electronics
21 #include <linux/printk.h>
23 int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget)
25 bool dfu_reset = false;
29 ret = udc_device_get_by_index(usbctrl_index, &udc);
31 pr_err("udc_device_get_by_index failed\n");
32 return CMD_RET_FAILURE;
35 ret = g_dnl_register(usb_dnl_gadget);
37 pr_err("g_dnl_register failed");
38 ret = CMD_RET_FAILURE;
42 #ifdef CONFIG_DFU_TIMEOUT
43 unsigned long start_time = get_timer(0);
49 * Check if USB bus reset is performed after detach,
50 * which indicates that -R switch has been passed to
51 * dfu-util. In this case reboot the device
53 if (dfu_usb_get_reset()) {
59 * This extra number of dm_usb_gadget_handle_interrupts()
60 * calls is necessary to assure correct transmission
61 * completion with dfu-util
70 if (dfu_get_defer_flush()) {
72 * Call to dm_usb_gadget_handle_interrupts() is necessary
73 * to act on ZLP OUT transaction from HOST PC after
74 * transmitting the whole file.
76 * If this ZLP OUT packet is NAK'ed, the HOST libusb
77 * function fails after timeout (by default it is set to
78 * 5 seconds). In such situation the dfu-util program
79 * exits with error message.
81 dm_usb_gadget_handle_interrupts(udc);
82 ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
83 dfu_set_defer_flush(NULL);
85 pr_err("Deferred dfu_flush() failed!");
90 #ifdef CONFIG_DFU_TIMEOUT
91 unsigned long wait_time = dfu_get_timeout();
94 unsigned long current_time = get_timer(start_time);
96 if (current_time > wait_time) {
97 debug("Inactivity timeout, abort DFU\n");
103 if (dfu_reinit_needed)
107 dm_usb_gadget_handle_interrupts(udc);
115 do_reset(NULL, 0, 0, NULL);
117 g_dnl_clear_detach();