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