Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
B, Ravi | 603a4c1 | 2016-07-28 17:39:15 +0530 | [diff] [blame] | 2 | /* |
| 3 | * dfu.c -- dfu command |
| 4 | * |
| 5 | * Copyright (C) 2015 |
| 6 | * Lukasz Majewski <l.majewski@majess.pl> |
| 7 | * |
| 8 | * Copyright (C) 2012 Samsung Electronics |
| 9 | * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com> |
| 10 | * Lukasz Majewski <l.majewski@samsung.com> |
B, Ravi | 603a4c1 | 2016-07-28 17:39:15 +0530 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <watchdog.h> |
| 15 | #include <dfu.h> |
| 16 | #include <console.h> |
| 17 | #include <g_dnl.h> |
| 18 | #include <usb.h> |
| 19 | #include <net.h> |
| 20 | |
| 21 | int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget) |
| 22 | { |
| 23 | bool dfu_reset = false; |
| 24 | int ret, i = 0; |
| 25 | |
Jean-Jacques Hiblot | 585e199 | 2018-11-29 10:52:41 +0100 | [diff] [blame] | 26 | ret = usb_gadget_initialize(usbctrl_index); |
Michal Simek | c809a97 | 2016-08-30 15:32:17 +0200 | [diff] [blame] | 27 | if (ret) { |
Jean-Jacques Hiblot | 585e199 | 2018-11-29 10:52:41 +0100 | [diff] [blame] | 28 | pr_err("usb_gadget_initialize failed\n"); |
Michal Simek | c809a97 | 2016-08-30 15:32:17 +0200 | [diff] [blame] | 29 | return CMD_RET_FAILURE; |
| 30 | } |
B, Ravi | 603a4c1 | 2016-07-28 17:39:15 +0530 | [diff] [blame] | 31 | g_dnl_clear_detach(); |
Sanchayan Maity | 0686066 | 2016-08-08 16:56:17 +0530 | [diff] [blame] | 32 | ret = g_dnl_register(usb_dnl_gadget); |
| 33 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 34 | pr_err("g_dnl_register failed"); |
Sanchayan Maity | 0686066 | 2016-08-08 16:56:17 +0530 | [diff] [blame] | 35 | return CMD_RET_FAILURE; |
| 36 | } |
| 37 | |
Andy Shevchenko | 1e41401 | 2019-11-27 18:12:15 +0200 | [diff] [blame] | 38 | #ifdef CONFIG_DFU_TIMEOUT |
| 39 | unsigned long start_time = get_timer(0); |
| 40 | #endif |
| 41 | |
B, Ravi | 603a4c1 | 2016-07-28 17:39:15 +0530 | [diff] [blame] | 42 | while (1) { |
| 43 | if (g_dnl_detach()) { |
| 44 | /* |
| 45 | * Check if USB bus reset is performed after detach, |
| 46 | * which indicates that -R switch has been passed to |
| 47 | * dfu-util. In this case reboot the device |
| 48 | */ |
| 49 | if (dfu_usb_get_reset()) { |
| 50 | dfu_reset = true; |
| 51 | goto exit; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * This extra number of usb_gadget_handle_interrupts() |
| 56 | * calls is necessary to assure correct transmission |
| 57 | * completion with dfu-util |
| 58 | */ |
| 59 | if (++i == 10000) |
| 60 | goto exit; |
| 61 | } |
| 62 | |
| 63 | if (ctrlc()) |
| 64 | goto exit; |
| 65 | |
| 66 | if (dfu_get_defer_flush()) { |
| 67 | /* |
| 68 | * Call to usb_gadget_handle_interrupts() is necessary |
| 69 | * to act on ZLP OUT transaction from HOST PC after |
| 70 | * transmitting the whole file. |
| 71 | * |
| 72 | * If this ZLP OUT packet is NAK'ed, the HOST libusb |
| 73 | * function fails after timeout (by default it is set to |
| 74 | * 5 seconds). In such situation the dfu-util program |
| 75 | * exits with error message. |
| 76 | */ |
| 77 | usb_gadget_handle_interrupts(usbctrl_index); |
| 78 | ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0); |
| 79 | dfu_set_defer_flush(NULL); |
| 80 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 81 | pr_err("Deferred dfu_flush() failed!"); |
B, Ravi | 603a4c1 | 2016-07-28 17:39:15 +0530 | [diff] [blame] | 82 | goto exit; |
| 83 | } |
| 84 | } |
Andy Shevchenko | 1e41401 | 2019-11-27 18:12:15 +0200 | [diff] [blame] | 85 | |
| 86 | #ifdef CONFIG_DFU_TIMEOUT |
| 87 | unsigned long wait_time = dfu_get_timeout(); |
| 88 | |
| 89 | if (wait_time) { |
| 90 | unsigned long current_time = get_timer(start_time); |
| 91 | |
| 92 | if (current_time > wait_time) { |
| 93 | debug("Inactivity timeout, abort DFU\n"); |
| 94 | goto exit; |
| 95 | } |
| 96 | } |
| 97 | #endif |
B, Ravi | 603a4c1 | 2016-07-28 17:39:15 +0530 | [diff] [blame] | 98 | |
| 99 | WATCHDOG_RESET(); |
| 100 | usb_gadget_handle_interrupts(usbctrl_index); |
| 101 | } |
| 102 | exit: |
| 103 | g_dnl_unregister(); |
Jean-Jacques Hiblot | 585e199 | 2018-11-29 10:52:41 +0100 | [diff] [blame] | 104 | usb_gadget_release(usbctrl_index); |
B, Ravi | 603a4c1 | 2016-07-28 17:39:15 +0530 | [diff] [blame] | 105 | |
| 106 | if (dfu_reset) |
B, Ravi | 4cca5cf | 2017-05-04 15:45:29 +0530 | [diff] [blame] | 107 | do_reset(NULL, 0, 0, NULL); |
B, Ravi | 603a4c1 | 2016-07-28 17:39:15 +0530 | [diff] [blame] | 108 | |
| 109 | g_dnl_clear_detach(); |
| 110 | |
| 111 | return ret; |
| 112 | } |