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