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