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> |
| 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 | |
Michal Simek | dbdc274 | 2016-08-30 15:32:17 +0200 | [diff] [blame] | 26 | ret = board_usb_init(usbctrl_index, USB_INIT_DEVICE); |
| 27 | if (ret) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 28 | pr_err("board usb init failed\n"); |
Michal Simek | dbdc274 | 2016-08-30 15:32:17 +0200 | [diff] [blame] | 29 | return CMD_RET_FAILURE; |
| 30 | } |
B, Ravi | 05341a8 | 2016-07-28 17:39:15 +0530 | [diff] [blame] | 31 | g_dnl_clear_detach(); |
Sanchayan Maity | 54a708c | 2016-08-08 16:56:17 +0530 | [diff] [blame] | 32 | ret = g_dnl_register(usb_dnl_gadget); |
| 33 | if (ret) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 34 | pr_err("g_dnl_register failed"); |
Sanchayan Maity | 54a708c | 2016-08-08 16:56:17 +0530 | [diff] [blame] | 35 | return CMD_RET_FAILURE; |
| 36 | } |
| 37 | |
B, Ravi | 05341a8 | 2016-07-28 17:39:15 +0530 | [diff] [blame] | 38 | while (1) { |
| 39 | if (g_dnl_detach()) { |
| 40 | /* |
| 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 |
| 44 | */ |
| 45 | if (dfu_usb_get_reset()) { |
| 46 | dfu_reset = true; |
| 47 | goto exit; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * This extra number of usb_gadget_handle_interrupts() |
| 52 | * calls is necessary to assure correct transmission |
| 53 | * completion with dfu-util |
| 54 | */ |
| 55 | if (++i == 10000) |
| 56 | goto exit; |
| 57 | } |
| 58 | |
| 59 | if (ctrlc()) |
| 60 | goto exit; |
| 61 | |
| 62 | if (dfu_get_defer_flush()) { |
| 63 | /* |
| 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. |
| 67 | * |
| 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. |
| 72 | */ |
| 73 | usb_gadget_handle_interrupts(usbctrl_index); |
| 74 | ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0); |
| 75 | dfu_set_defer_flush(NULL); |
| 76 | if (ret) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 77 | pr_err("Deferred dfu_flush() failed!"); |
B, Ravi | 05341a8 | 2016-07-28 17:39:15 +0530 | [diff] [blame] | 78 | goto exit; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | WATCHDOG_RESET(); |
| 83 | usb_gadget_handle_interrupts(usbctrl_index); |
| 84 | } |
| 85 | exit: |
| 86 | g_dnl_unregister(); |
| 87 | board_usb_cleanup(usbctrl_index, USB_INIT_DEVICE); |
| 88 | |
| 89 | if (dfu_reset) |
B, Ravi | 66928af | 2017-05-04 15:45:29 +0530 | [diff] [blame] | 90 | do_reset(NULL, 0, 0, NULL); |
B, Ravi | 05341a8 | 2016-07-28 17:39:15 +0530 | [diff] [blame] | 91 | |
| 92 | g_dnl_clear_detach(); |
| 93 | |
| 94 | return ret; |
| 95 | } |