]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
05341a87 R |
2 | /* |
3 | * dfu.c -- dfu command | |
4 | * | |
5 | * Copyright (C) 2015 | |
6 | * Lukasz Majewski <[email protected]> | |
7 | * | |
8 | * Copyright (C) 2012 Samsung Electronics | |
9 | * authors: Andrzej Pietrasiewicz <[email protected]> | |
10 | * Lukasz Majewski <[email protected]> | |
05341a87 R |
11 | */ |
12 | ||
13 | #include <common.h> | |
09140113 | 14 | #include <command.h> |
f7ae49fc | 15 | #include <log.h> |
05341a87 R |
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> | |
1e94b46f | 22 | #include <linux/printk.h> |
05341a87 R |
23 | |
24 | int run_usb_dnl_gadget(int usbctrl_index, char *usb_dnl_gadget) | |
25 | { | |
26 | bool dfu_reset = false; | |
2dfae175 | 27 | struct udevice *udc; |
05341a87 R |
28 | int ret, i = 0; |
29 | ||
2dfae175 | 30 | ret = udc_device_get_by_index(usbctrl_index, &udc); |
dbdc2744 | 31 | if (ret) { |
2dfae175 | 32 | pr_err("udc_device_get_by_index failed\n"); |
dbdc2744 MS |
33 | return CMD_RET_FAILURE; |
34 | } | |
05341a87 | 35 | g_dnl_clear_detach(); |
54a708ca SM |
36 | ret = g_dnl_register(usb_dnl_gadget); |
37 | if (ret) { | |
9b643e31 | 38 | pr_err("g_dnl_register failed"); |
edd6042d MV |
39 | ret = CMD_RET_FAILURE; |
40 | goto err_detach; | |
54a708ca SM |
41 | } |
42 | ||
98a8f445 AS |
43 | #ifdef CONFIG_DFU_TIMEOUT |
44 | unsigned long start_time = get_timer(0); | |
45 | #endif | |
46 | ||
05341a87 R |
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 | /* | |
2dfae175 | 60 | * This extra number of dm_usb_gadget_handle_interrupts() |
05341a87 R |
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 | /* | |
2dfae175 | 73 | * Call to dm_usb_gadget_handle_interrupts() is necessary |
05341a87 R |
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 | */ | |
2dfae175 | 82 | dm_usb_gadget_handle_interrupts(udc); |
05341a87 R |
83 | ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0); |
84 | dfu_set_defer_flush(NULL); | |
85 | if (ret) { | |
9b643e31 | 86 | pr_err("Deferred dfu_flush() failed!"); |
05341a87 R |
87 | goto exit; |
88 | } | |
89 | } | |
90 | ||
98a8f445 AS |
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 | ||
9129f2f1 MS |
104 | if (dfu_reinit_needed) |
105 | goto exit; | |
106 | ||
29caf930 | 107 | schedule(); |
2dfae175 | 108 | dm_usb_gadget_handle_interrupts(udc); |
05341a87 R |
109 | } |
110 | exit: | |
111 | g_dnl_unregister(); | |
edd6042d | 112 | err_detach: |
2dfae175 | 113 | udc_device_put(udc); |
05341a87 R |
114 | |
115 | if (dfu_reset) | |
66928afb | 116 | do_reset(NULL, 0, 0, NULL); |
05341a87 R |
117 | |
118 | g_dnl_clear_detach(); | |
119 | ||
120 | return ret; | |
121 | } |