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