Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 2 | /* |
| 3 | * f_thor.c -- USB TIZEN THOR Downloader gadget function |
| 4 | * |
| 5 | * Copyright (C) 2013 Samsung Electronics |
| 6 | * Lukasz Majewski <l.majewski@samsung.com> |
| 7 | * |
| 8 | * Based on code from: |
| 9 | * git://review.tizen.org/kernel/u-boot |
| 10 | * |
| 11 | * Developed by: |
| 12 | * Copyright (C) 2009 Samsung Electronics |
| 13 | * Minkyu Kang <mk7.kang@samsung.com> |
| 14 | * Sanghee Kim <sh0130.kim@samsung.com> |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 17 | #include <command.h> |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 18 | #include <common.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 19 | #include <console.h> |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 20 | #include <dm.h> |
| 21 | #include <errno.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 22 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 23 | #include <log.h> |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 24 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 25 | #include <memalign.h> |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 26 | #include <version.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 27 | #include <linux/delay.h> |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 28 | #include <linux/usb/ch9.h> |
| 29 | #include <linux/usb/gadget.h> |
| 30 | #include <linux/usb/composite.h> |
| 31 | #include <linux/usb/cdc.h> |
| 32 | #include <g_dnl.h> |
| 33 | #include <dfu.h> |
Marek Szyprowski | ee9b855 | 2020-12-22 11:32:24 +0100 | [diff] [blame] | 34 | #include <thor.h> |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 35 | |
| 36 | #include "f_thor.h" |
| 37 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 38 | static void thor_tx_data(struct udevice *udc, unsigned char *data, int len); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 39 | static void thor_set_dma(void *addr, int len); |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 40 | static int thor_rx_data(struct udevice *udc); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 41 | |
| 42 | static struct f_thor *thor_func; |
| 43 | static inline struct f_thor *func_to_thor(struct usb_function *f) |
| 44 | { |
| 45 | return container_of(f, struct f_thor, usb_function); |
| 46 | } |
| 47 | |
| 48 | DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_tx_data_buf, |
| 49 | sizeof(struct rsp_box)); |
| 50 | DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_rx_data_buf, |
| 51 | sizeof(struct rqt_box)); |
| 52 | |
| 53 | /* ********************************************************** */ |
| 54 | /* THOR protocol - transmission handling */ |
| 55 | /* ********************************************************** */ |
Seung-Woo Kim | 16b74aa | 2018-05-10 10:52:14 +0900 | [diff] [blame] | 56 | DEFINE_CACHE_ALIGN_BUFFER(char, f_name, F_NAME_BUF_SIZE + 1); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 57 | static unsigned long long int thor_file_size; |
| 58 | static int alt_setting_num; |
| 59 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 60 | static void send_rsp(struct udevice *udc, const struct rsp_box *rsp) |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 61 | { |
| 62 | memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box)); |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 63 | thor_tx_data(udc, thor_tx_data_buf, sizeof(struct rsp_box)); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 64 | |
| 65 | debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data); |
| 66 | } |
| 67 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 68 | static void send_data_rsp(struct udevice *udc, s32 ack, s32 count) |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 69 | { |
| 70 | ALLOC_CACHE_ALIGN_BUFFER(struct data_rsp_box, rsp, |
| 71 | sizeof(struct data_rsp_box)); |
| 72 | |
| 73 | rsp->ack = ack; |
| 74 | rsp->count = count; |
| 75 | |
| 76 | memcpy(thor_tx_data_buf, rsp, sizeof(struct data_rsp_box)); |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 77 | thor_tx_data(udc, thor_tx_data_buf, sizeof(struct data_rsp_box)); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 78 | |
| 79 | debug("-DATA RSP: %d, %d\n", ack, count); |
| 80 | } |
| 81 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 82 | static int process_rqt_info(struct udevice *udc, const struct rqt_box *rqt) |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 83 | { |
| 84 | ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box)); |
| 85 | memset(rsp, 0, sizeof(struct rsp_box)); |
| 86 | |
| 87 | rsp->rsp = rqt->rqt; |
| 88 | rsp->rsp_data = rqt->rqt_data; |
| 89 | |
| 90 | switch (rqt->rqt_data) { |
| 91 | case RQT_INFO_VER_PROTOCOL: |
| 92 | rsp->int_data[0] = VER_PROTOCOL_MAJOR; |
| 93 | rsp->int_data[1] = VER_PROTOCOL_MINOR; |
| 94 | break; |
| 95 | case RQT_INIT_VER_HW: |
| 96 | snprintf(rsp->str_data[0], sizeof(rsp->str_data[0]), |
| 97 | "%x", checkboard()); |
| 98 | break; |
| 99 | case RQT_INIT_VER_BOOT: |
| 100 | sprintf(rsp->str_data[0], "%s", U_BOOT_VERSION); |
| 101 | break; |
| 102 | case RQT_INIT_VER_KERNEL: |
| 103 | sprintf(rsp->str_data[0], "%s", "k unknown"); |
| 104 | break; |
| 105 | case RQT_INIT_VER_PLATFORM: |
| 106 | sprintf(rsp->str_data[0], "%s", "p unknown"); |
| 107 | break; |
| 108 | case RQT_INIT_VER_CSC: |
| 109 | sprintf(rsp->str_data[0], "%s", "c unknown"); |
| 110 | break; |
| 111 | default: |
| 112 | return -EINVAL; |
| 113 | } |
| 114 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 115 | send_rsp(udc, rsp); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 119 | static int process_rqt_cmd(struct udevice *udc, const struct rqt_box *rqt) |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 120 | { |
| 121 | ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box)); |
| 122 | memset(rsp, 0, sizeof(struct rsp_box)); |
| 123 | |
| 124 | rsp->rsp = rqt->rqt; |
| 125 | rsp->rsp_data = rqt->rqt_data; |
| 126 | |
| 127 | switch (rqt->rqt_data) { |
| 128 | case RQT_CMD_REBOOT: |
| 129 | debug("TARGET RESET\n"); |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 130 | send_rsp(udc, rsp); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 131 | g_dnl_unregister(); |
| 132 | dfu_free_entities(); |
Siva Durga Prasad Paladugu | 4bad106 | 2015-04-16 10:38:34 +0200 | [diff] [blame] | 133 | #ifdef CONFIG_THOR_RESET_OFF |
| 134 | return RESET_DONE; |
| 135 | #endif |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 136 | run_command("reset", 0); |
| 137 | break; |
| 138 | case RQT_CMD_POWEROFF: |
| 139 | case RQT_CMD_EFSCLEAR: |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 140 | send_rsp(udc, rsp); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 141 | default: |
| 142 | printf("Command not supported -> cmd: %d\n", rqt->rqt_data); |
| 143 | return -EINVAL; |
| 144 | } |
| 145 | |
| 146 | return true; |
| 147 | } |
| 148 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 149 | static long long int download_head(struct udevice *udc, |
| 150 | unsigned long long total, |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 151 | unsigned int packet_size, |
| 152 | long long int *left, |
| 153 | int *cnt) |
| 154 | { |
| 155 | long long int rcv_cnt = 0, left_to_rcv, ret_rcv; |
Stephen Warren | f950098 | 2014-06-11 16:03:34 -0600 | [diff] [blame] | 156 | struct dfu_entity *dfu_entity = dfu_get_entity(alt_setting_num); |
| 157 | void *transfer_buffer = dfu_get_buf(dfu_entity); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 158 | void *buf = transfer_buffer; |
| 159 | int usb_pkt_cnt = 0, ret; |
| 160 | |
| 161 | /* |
| 162 | * Files smaller than THOR_STORE_UNIT_SIZE (now 32 MiB) are stored on |
| 163 | * the medium. |
| 164 | * The packet response is sent on the purpose after successful data |
| 165 | * chunk write. There is a room for improvement when asynchronous write |
| 166 | * is performed. |
| 167 | */ |
| 168 | while (total - rcv_cnt >= packet_size) { |
| 169 | thor_set_dma(buf, packet_size); |
| 170 | buf += packet_size; |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 171 | ret_rcv = thor_rx_data(udc); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 172 | if (ret_rcv < 0) |
| 173 | return ret_rcv; |
| 174 | rcv_cnt += ret_rcv; |
| 175 | debug("%d: RCV data count: %llu cnt: %d\n", usb_pkt_cnt, |
| 176 | rcv_cnt, *cnt); |
| 177 | |
| 178 | if ((rcv_cnt % THOR_STORE_UNIT_SIZE) == 0) { |
| 179 | ret = dfu_write(dfu_get_entity(alt_setting_num), |
| 180 | transfer_buffer, THOR_STORE_UNIT_SIZE, |
| 181 | (*cnt)++); |
| 182 | if (ret) { |
Seung-Woo Kim | b87b94e | 2020-02-13 17:24:11 +0900 | [diff] [blame] | 183 | pr_err("DFU write failed [%d] cnt: %d\n", |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 184 | ret, *cnt); |
| 185 | return ret; |
| 186 | } |
| 187 | buf = transfer_buffer; |
| 188 | } |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 189 | send_data_rsp(udc, 0, ++usb_pkt_cnt); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* Calculate the amount of data to arrive from PC (in bytes) */ |
| 193 | left_to_rcv = total - rcv_cnt; |
| 194 | |
| 195 | /* |
| 196 | * Calculate number of data already received. but not yet stored |
| 197 | * on the medium (they are smaller than THOR_STORE_UNIT_SIZE) |
| 198 | */ |
| 199 | *left = left_to_rcv + buf - transfer_buffer; |
| 200 | debug("%s: left: %llu left_to_rcv: %llu buf: 0x%p\n", __func__, |
| 201 | *left, left_to_rcv, buf); |
| 202 | |
| 203 | if (left_to_rcv) { |
| 204 | thor_set_dma(buf, packet_size); |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 205 | ret_rcv = thor_rx_data(udc); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 206 | if (ret_rcv < 0) |
| 207 | return ret_rcv; |
| 208 | rcv_cnt += ret_rcv; |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 209 | send_data_rsp(udc, 0, ++usb_pkt_cnt); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | debug("%s: %llu total: %llu cnt: %d\n", __func__, rcv_cnt, total, *cnt); |
| 213 | |
| 214 | return rcv_cnt; |
| 215 | } |
| 216 | |
| 217 | static int download_tail(long long int left, int cnt) |
| 218 | { |
Przemyslaw Marczak | a5071a0 | 2014-12-15 10:34:10 +0100 | [diff] [blame] | 219 | struct dfu_entity *dfu_entity; |
| 220 | void *transfer_buffer; |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 221 | int ret; |
| 222 | |
| 223 | debug("%s: left: %llu cnt: %d\n", __func__, left, cnt); |
| 224 | |
Przemyslaw Marczak | a5071a0 | 2014-12-15 10:34:10 +0100 | [diff] [blame] | 225 | dfu_entity = dfu_get_entity(alt_setting_num); |
| 226 | if (!dfu_entity) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 227 | pr_err("Alt setting: %d entity not found!\n", alt_setting_num); |
Przemyslaw Marczak | a5071a0 | 2014-12-15 10:34:10 +0100 | [diff] [blame] | 228 | return -ENOENT; |
| 229 | } |
| 230 | |
| 231 | transfer_buffer = dfu_get_buf(dfu_entity); |
| 232 | if (!transfer_buffer) { |
Seung-Woo Kim | b87b94e | 2020-02-13 17:24:11 +0900 | [diff] [blame] | 233 | pr_err("Transfer buffer not allocated!\n"); |
Przemyslaw Marczak | a5071a0 | 2014-12-15 10:34:10 +0100 | [diff] [blame] | 234 | return -ENXIO; |
| 235 | } |
| 236 | |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 237 | if (left) { |
Przemyslaw Marczak | 13a41c8 | 2014-04-18 09:48:24 +0200 | [diff] [blame] | 238 | ret = dfu_write(dfu_entity, transfer_buffer, left, cnt++); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 239 | if (ret) { |
Seung-Woo Kim | b87b94e | 2020-02-13 17:24:11 +0900 | [diff] [blame] | 240 | pr_err("DFU write failed[%d]: left: %llu\n", ret, left); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 241 | return ret; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* |
Przemyslaw Marczak | 065a27f | 2014-05-12 12:05:34 +0200 | [diff] [blame] | 246 | * To store last "packet" or write file from buffer to filesystem |
| 247 | * DFU storage backend requires dfu_flush |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 248 | * |
| 249 | * This also frees memory malloc'ed by dfu_get_buf(), so no explicit |
| 250 | * need fo call dfu_free_buf() is needed. |
| 251 | */ |
Przemyslaw Marczak | ef9d4d4 | 2014-04-18 09:48:25 +0200 | [diff] [blame] | 252 | ret = dfu_flush(dfu_entity, transfer_buffer, 0, cnt); |
Przemyslaw Marczak | 065a27f | 2014-05-12 12:05:34 +0200 | [diff] [blame] | 253 | if (ret) |
Seung-Woo Kim | b87b94e | 2020-02-13 17:24:11 +0900 | [diff] [blame] | 254 | pr_err("DFU flush failed!\n"); |
Przemyslaw Marczak | ef9d4d4 | 2014-04-18 09:48:25 +0200 | [diff] [blame] | 255 | |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 256 | return ret; |
| 257 | } |
| 258 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 259 | static long long int process_rqt_download(struct udevice *udc, const struct rqt_box *rqt) |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 260 | { |
| 261 | ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box)); |
| 262 | static long long int left, ret_head; |
| 263 | int file_type, ret = 0; |
| 264 | static int cnt; |
| 265 | |
| 266 | memset(rsp, 0, sizeof(struct rsp_box)); |
| 267 | rsp->rsp = rqt->rqt; |
| 268 | rsp->rsp_data = rqt->rqt_data; |
| 269 | |
| 270 | switch (rqt->rqt_data) { |
| 271 | case RQT_DL_INIT: |
Seung-Woo Kim | 2753cec | 2020-10-16 16:29:02 +0900 | [diff] [blame] | 272 | thor_file_size = (uint64_t)(uint32_t)rqt->int_data[0] + |
| 273 | (((uint64_t)(uint32_t)rqt->int_data[1]) |
Seung-Woo Kim | 7414a0f | 2018-05-10 10:52:15 +0900 | [diff] [blame] | 274 | << 32); |
| 275 | debug("INIT: total %llu bytes\n", thor_file_size); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 276 | break; |
| 277 | case RQT_DL_FILE_INFO: |
| 278 | file_type = rqt->int_data[0]; |
| 279 | if (file_type == FILE_TYPE_PIT) { |
| 280 | puts("PIT table file - not supported\n"); |
| 281 | rsp->ack = -ENOTSUPP; |
| 282 | ret = rsp->ack; |
| 283 | break; |
| 284 | } |
| 285 | |
Seung-Woo Kim | 2753cec | 2020-10-16 16:29:02 +0900 | [diff] [blame] | 286 | thor_file_size = (uint64_t)(uint32_t)rqt->int_data[1] + |
| 287 | (((uint64_t)(uint32_t)rqt->int_data[2]) |
Seung-Woo Kim | 7414a0f | 2018-05-10 10:52:15 +0900 | [diff] [blame] | 288 | << 32); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 289 | memcpy(f_name, rqt->str_data[0], F_NAME_BUF_SIZE); |
Seung-Woo Kim | 16b74aa | 2018-05-10 10:52:14 +0900 | [diff] [blame] | 290 | f_name[F_NAME_BUF_SIZE] = '\0'; |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 291 | |
| 292 | debug("INFO: name(%s, %d), size(%llu), type(%d)\n", |
| 293 | f_name, 0, thor_file_size, file_type); |
| 294 | |
| 295 | rsp->int_data[0] = THOR_PACKET_SIZE; |
| 296 | |
| 297 | alt_setting_num = dfu_get_alt(f_name); |
| 298 | if (alt_setting_num < 0) { |
Seung-Woo Kim | b87b94e | 2020-02-13 17:24:11 +0900 | [diff] [blame] | 299 | pr_err("Alt setting [%d] to write not found!\n", |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 300 | alt_setting_num); |
| 301 | rsp->ack = -ENODEV; |
| 302 | ret = rsp->ack; |
| 303 | } |
| 304 | break; |
| 305 | case RQT_DL_FILE_START: |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 306 | send_rsp(udc, rsp); |
| 307 | ret_head = download_head(udc, thor_file_size, THOR_PACKET_SIZE, |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 308 | &left, &cnt); |
| 309 | if (ret_head < 0) { |
| 310 | left = 0; |
| 311 | cnt = 0; |
| 312 | } |
| 313 | return ret_head; |
| 314 | case RQT_DL_FILE_END: |
| 315 | debug("DL FILE_END\n"); |
| 316 | rsp->ack = download_tail(left, cnt); |
| 317 | ret = rsp->ack; |
| 318 | left = 0; |
| 319 | cnt = 0; |
| 320 | break; |
| 321 | case RQT_DL_EXIT: |
| 322 | debug("DL EXIT\n"); |
| 323 | break; |
| 324 | default: |
Seung-Woo Kim | b87b94e | 2020-02-13 17:24:11 +0900 | [diff] [blame] | 325 | pr_err("Operation not supported: %d\n", rqt->rqt_data); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 326 | ret = -ENOTSUPP; |
| 327 | } |
| 328 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 329 | send_rsp(udc, rsp); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 330 | return ret; |
| 331 | } |
| 332 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 333 | static int process_data(struct udevice *udc) |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 334 | { |
| 335 | ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box)); |
| 336 | int ret = -EINVAL; |
| 337 | |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 338 | memcpy(rqt, thor_rx_data_buf, sizeof(struct rqt_box)); |
| 339 | |
| 340 | debug("+RQT: %d, %d\n", rqt->rqt, rqt->rqt_data); |
| 341 | |
| 342 | switch (rqt->rqt) { |
| 343 | case RQT_INFO: |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 344 | ret = process_rqt_info(udc, rqt); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 345 | break; |
| 346 | case RQT_CMD: |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 347 | ret = process_rqt_cmd(udc, rqt); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 348 | break; |
| 349 | case RQT_DL: |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 350 | ret = (int) process_rqt_download(udc, rqt); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 351 | break; |
| 352 | case RQT_UL: |
| 353 | puts("RQT: UPLOAD not supported!\n"); |
| 354 | break; |
| 355 | default: |
Seung-Woo Kim | b87b94e | 2020-02-13 17:24:11 +0900 | [diff] [blame] | 356 | pr_err("unknown request (%d)\n", rqt->rqt); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | /* ********************************************************** */ |
| 363 | /* THOR USB Function */ |
| 364 | /* ********************************************************** */ |
| 365 | |
| 366 | static inline struct usb_endpoint_descriptor * |
| 367 | ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, |
| 368 | struct usb_endpoint_descriptor *fs) |
| 369 | { |
| 370 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 371 | return hs; |
| 372 | return fs; |
| 373 | } |
| 374 | |
| 375 | static struct usb_interface_descriptor thor_downloader_intf_data = { |
| 376 | .bLength = sizeof(thor_downloader_intf_data), |
| 377 | .bDescriptorType = USB_DT_INTERFACE, |
| 378 | |
| 379 | .bNumEndpoints = 2, |
| 380 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 381 | }; |
| 382 | |
| 383 | static struct usb_endpoint_descriptor fs_in_desc = { |
| 384 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 385 | .bDescriptorType = USB_DT_ENDPOINT, |
| 386 | |
| 387 | .bEndpointAddress = USB_DIR_IN, |
| 388 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 389 | }; |
| 390 | |
| 391 | static struct usb_endpoint_descriptor fs_out_desc = { |
| 392 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 393 | .bDescriptorType = USB_DT_ENDPOINT, |
| 394 | |
| 395 | .bEndpointAddress = USB_DIR_OUT, |
| 396 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 397 | }; |
| 398 | |
| 399 | /* CDC configuration */ |
| 400 | static struct usb_interface_descriptor thor_downloader_intf_int = { |
| 401 | .bLength = sizeof(thor_downloader_intf_int), |
| 402 | .bDescriptorType = USB_DT_INTERFACE, |
| 403 | |
| 404 | .bNumEndpoints = 1, |
| 405 | .bInterfaceClass = USB_CLASS_COMM, |
| 406 | /* 0x02 Abstract Line Control Model */ |
| 407 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, |
| 408 | /* 0x01 Common AT commands */ |
| 409 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER, |
| 410 | }; |
| 411 | |
| 412 | static struct usb_cdc_header_desc thor_downloader_cdc_header = { |
| 413 | .bLength = sizeof(thor_downloader_cdc_header), |
| 414 | .bDescriptorType = 0x24, /* CS_INTERFACE */ |
| 415 | .bDescriptorSubType = 0x00, |
| 416 | .bcdCDC = 0x0110, |
| 417 | }; |
| 418 | |
| 419 | static struct usb_cdc_call_mgmt_descriptor thor_downloader_cdc_call = { |
| 420 | .bLength = sizeof(thor_downloader_cdc_call), |
| 421 | .bDescriptorType = 0x24, /* CS_INTERFACE */ |
| 422 | .bDescriptorSubType = 0x01, |
| 423 | .bmCapabilities = 0x00, |
| 424 | .bDataInterface = 0x01, |
| 425 | }; |
| 426 | |
| 427 | static struct usb_cdc_acm_descriptor thor_downloader_cdc_abstract = { |
| 428 | .bLength = sizeof(thor_downloader_cdc_abstract), |
| 429 | .bDescriptorType = 0x24, /* CS_INTERFACE */ |
| 430 | .bDescriptorSubType = 0x02, |
| 431 | .bmCapabilities = 0x00, |
| 432 | }; |
| 433 | |
| 434 | static struct usb_cdc_union_desc thor_downloader_cdc_union = { |
| 435 | .bLength = sizeof(thor_downloader_cdc_union), |
| 436 | .bDescriptorType = 0x24, /* CS_INTERFACE */ |
| 437 | .bDescriptorSubType = USB_CDC_UNION_TYPE, |
| 438 | }; |
| 439 | |
| 440 | static struct usb_endpoint_descriptor fs_int_desc = { |
| 441 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 442 | .bDescriptorType = USB_DT_ENDPOINT, |
| 443 | |
| 444 | .bEndpointAddress = 3 | USB_DIR_IN, |
| 445 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 446 | .wMaxPacketSize = __constant_cpu_to_le16(16), |
| 447 | |
| 448 | .bInterval = 0x9, |
| 449 | }; |
| 450 | |
| 451 | static struct usb_interface_assoc_descriptor |
| 452 | thor_iad_descriptor = { |
| 453 | .bLength = sizeof(thor_iad_descriptor), |
| 454 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, |
| 455 | |
| 456 | .bFirstInterface = 0, |
| 457 | .bInterfaceCount = 2, /* control + data */ |
| 458 | .bFunctionClass = USB_CLASS_COMM, |
| 459 | .bFunctionSubClass = USB_CDC_SUBCLASS_ACM, |
| 460 | .bFunctionProtocol = USB_CDC_PROTO_NONE, |
| 461 | }; |
| 462 | |
| 463 | static struct usb_endpoint_descriptor hs_in_desc = { |
| 464 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 465 | .bDescriptorType = USB_DT_ENDPOINT, |
| 466 | |
| 467 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 468 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 469 | }; |
| 470 | |
| 471 | static struct usb_endpoint_descriptor hs_out_desc = { |
| 472 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 473 | .bDescriptorType = USB_DT_ENDPOINT, |
| 474 | |
| 475 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 476 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 477 | }; |
| 478 | |
| 479 | static struct usb_endpoint_descriptor hs_int_desc = { |
| 480 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 481 | .bDescriptorType = USB_DT_ENDPOINT, |
| 482 | |
| 483 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 484 | .wMaxPacketSize = __constant_cpu_to_le16(16), |
| 485 | |
| 486 | .bInterval = 0x9, |
| 487 | }; |
| 488 | |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 489 | /* |
| 490 | * This attribute vendor descriptor is necessary for correct operation with |
| 491 | * Windows version of THOR download program |
| 492 | * |
| 493 | * It prevents windows driver from sending zero lenght packet (ZLP) after |
| 494 | * each THOR_PACKET_SIZE. This assures consistent behaviour with libusb |
| 495 | */ |
| 496 | static struct usb_cdc_attribute_vendor_descriptor thor_downloader_cdc_av = { |
| 497 | .bLength = sizeof(thor_downloader_cdc_av), |
| 498 | .bDescriptorType = 0x24, |
| 499 | .bDescriptorSubType = 0x80, |
| 500 | .DAUType = 0x0002, |
| 501 | .DAULength = 0x0001, |
| 502 | .DAUValue = 0x00, |
| 503 | }; |
| 504 | |
| 505 | static const struct usb_descriptor_header *hs_thor_downloader_function[] = { |
| 506 | (struct usb_descriptor_header *)&thor_iad_descriptor, |
| 507 | |
| 508 | (struct usb_descriptor_header *)&thor_downloader_intf_int, |
| 509 | (struct usb_descriptor_header *)&thor_downloader_cdc_header, |
| 510 | (struct usb_descriptor_header *)&thor_downloader_cdc_call, |
| 511 | (struct usb_descriptor_header *)&thor_downloader_cdc_abstract, |
| 512 | (struct usb_descriptor_header *)&thor_downloader_cdc_union, |
| 513 | (struct usb_descriptor_header *)&hs_int_desc, |
| 514 | |
| 515 | (struct usb_descriptor_header *)&thor_downloader_intf_data, |
| 516 | (struct usb_descriptor_header *)&thor_downloader_cdc_av, |
| 517 | (struct usb_descriptor_header *)&hs_in_desc, |
| 518 | (struct usb_descriptor_header *)&hs_out_desc, |
| 519 | NULL, |
| 520 | }; |
| 521 | |
| 522 | /*-------------------------------------------------------------------------*/ |
| 523 | static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length) |
| 524 | { |
| 525 | struct usb_request *req; |
| 526 | |
| 527 | req = usb_ep_alloc_request(ep, 0); |
| 528 | if (!req) |
| 529 | return req; |
| 530 | |
| 531 | req->length = length; |
| 532 | req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length); |
| 533 | if (!req->buf) { |
| 534 | usb_ep_free_request(ep, req); |
| 535 | req = NULL; |
| 536 | } |
| 537 | |
| 538 | return req; |
| 539 | } |
| 540 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 541 | static int thor_rx_data(struct udevice *udc) |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 542 | { |
| 543 | struct thor_dev *dev = thor_func->dev; |
| 544 | int data_to_rx, tmp, status; |
| 545 | |
| 546 | data_to_rx = dev->out_req->length; |
| 547 | tmp = data_to_rx; |
| 548 | do { |
| 549 | dev->out_req->length = data_to_rx; |
| 550 | debug("dev->out_req->length:%d dev->rxdata:%d\n", |
| 551 | dev->out_req->length, dev->rxdata); |
| 552 | |
| 553 | status = usb_ep_queue(dev->out_ep, dev->out_req, 0); |
| 554 | if (status) { |
Seung-Woo Kim | b87b94e | 2020-02-13 17:24:11 +0900 | [diff] [blame] | 555 | pr_err("kill %s: resubmit %d bytes --> %d\n", |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 556 | dev->out_ep->name, dev->out_req->length, status); |
| 557 | usb_ep_set_halt(dev->out_ep); |
| 558 | return -EAGAIN; |
| 559 | } |
| 560 | |
| 561 | while (!dev->rxdata) { |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 562 | dm_usb_gadget_handle_interrupts(udc); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 563 | if (ctrlc()) |
| 564 | return -1; |
| 565 | } |
| 566 | dev->rxdata = 0; |
| 567 | data_to_rx -= dev->out_req->actual; |
| 568 | } while (data_to_rx); |
| 569 | |
| 570 | return tmp; |
| 571 | } |
| 572 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 573 | static void thor_tx_data(struct udevice *udc, unsigned char *data, int len) |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 574 | { |
| 575 | struct thor_dev *dev = thor_func->dev; |
| 576 | unsigned char *ptr = dev->in_req->buf; |
| 577 | int status; |
| 578 | |
| 579 | memset(ptr, 0, len); |
| 580 | memcpy(ptr, data, len); |
| 581 | |
| 582 | dev->in_req->length = len; |
| 583 | |
Michal Simek | 6c7d003 | 2015-10-30 16:19:43 +0100 | [diff] [blame] | 584 | debug("%s: dev->in_req->length:%d to_cpy:%zd\n", __func__, |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 585 | dev->in_req->length, sizeof(data)); |
| 586 | |
| 587 | status = usb_ep_queue(dev->in_ep, dev->in_req, 0); |
| 588 | if (status) { |
Seung-Woo Kim | b87b94e | 2020-02-13 17:24:11 +0900 | [diff] [blame] | 589 | pr_err("kill %s: resubmit %d bytes --> %d\n", |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 590 | dev->in_ep->name, dev->in_req->length, status); |
| 591 | usb_ep_set_halt(dev->in_ep); |
| 592 | } |
| 593 | |
| 594 | /* Wait until tx interrupt received */ |
| 595 | while (!dev->txdata) |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 596 | dm_usb_gadget_handle_interrupts(udc); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 597 | |
| 598 | dev->txdata = 0; |
| 599 | } |
| 600 | |
| 601 | static void thor_rx_tx_complete(struct usb_ep *ep, struct usb_request *req) |
| 602 | { |
| 603 | struct thor_dev *dev = thor_func->dev; |
| 604 | int status = req->status; |
| 605 | |
| 606 | debug("%s: ep_ptr:%p, req_ptr:%p\n", __func__, ep, req); |
| 607 | switch (status) { |
| 608 | case 0: |
| 609 | if (ep == dev->out_ep) |
| 610 | dev->rxdata = 1; |
| 611 | else |
| 612 | dev->txdata = 1; |
| 613 | |
| 614 | break; |
| 615 | |
| 616 | /* this endpoint is normally active while we're configured */ |
| 617 | case -ECONNABORTED: /* hardware forced ep reset */ |
| 618 | case -ECONNRESET: /* request dequeued */ |
| 619 | case -ESHUTDOWN: /* disconnect from host */ |
| 620 | case -EREMOTEIO: /* short read */ |
| 621 | case -EOVERFLOW: |
Seung-Woo Kim | b87b94e | 2020-02-13 17:24:11 +0900 | [diff] [blame] | 622 | pr_err("ERROR:%d\n", status); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 623 | break; |
| 624 | } |
| 625 | |
| 626 | debug("%s complete --> %d, %d/%d\n", ep->name, |
| 627 | status, req->actual, req->length); |
| 628 | } |
| 629 | |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 630 | static void thor_setup_complete(struct usb_ep *ep, struct usb_request *req) |
| 631 | { |
| 632 | if (req->status || req->actual != req->length) |
| 633 | debug("setup complete --> %d, %d/%d\n", |
| 634 | req->status, req->actual, req->length); |
| 635 | } |
| 636 | |
| 637 | static int |
| 638 | thor_func_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 639 | { |
| 640 | struct thor_dev *dev = thor_func->dev; |
| 641 | struct usb_request *req = dev->req; |
| 642 | struct usb_gadget *gadget = dev->gadget; |
| 643 | int value = 0; |
| 644 | |
| 645 | u16 len = le16_to_cpu(ctrl->wLength); |
| 646 | |
| 647 | debug("Req_Type: 0x%x Req: 0x%x wValue: 0x%x wIndex: 0x%x wLen: 0x%x\n", |
| 648 | ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, ctrl->wIndex, |
| 649 | ctrl->wLength); |
| 650 | |
| 651 | switch (ctrl->bRequest) { |
| 652 | case USB_CDC_REQ_SET_CONTROL_LINE_STATE: |
| 653 | value = 0; |
| 654 | break; |
| 655 | case USB_CDC_REQ_SET_LINE_CODING: |
| 656 | value = len; |
| 657 | /* Line Coding set done = configuration done */ |
| 658 | thor_func->dev->configuration_done = 1; |
| 659 | break; |
| 660 | |
| 661 | default: |
Seung-Woo Kim | b87b94e | 2020-02-13 17:24:11 +0900 | [diff] [blame] | 662 | pr_err("thor_setup: unknown request: %d\n", ctrl->bRequest); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | if (value >= 0) { |
| 666 | req->length = value; |
| 667 | req->zero = value < len; |
| 668 | value = usb_ep_queue(gadget->ep0, req, 0); |
| 669 | if (value < 0) { |
| 670 | debug("%s: ep_queue: %d\n", __func__, value); |
| 671 | req->status = 0; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | return value; |
| 676 | } |
| 677 | |
| 678 | /* Specific to the THOR protocol */ |
| 679 | static void thor_set_dma(void *addr, int len) |
| 680 | { |
| 681 | struct thor_dev *dev = thor_func->dev; |
| 682 | |
| 683 | debug("in_req:%p, out_req:%p\n", dev->in_req, dev->out_req); |
| 684 | debug("addr:%p, len:%d\n", addr, len); |
| 685 | |
| 686 | dev->out_req->buf = addr; |
| 687 | dev->out_req->length = len; |
| 688 | } |
| 689 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 690 | int thor_init(struct udevice *udc) |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 691 | { |
| 692 | struct thor_dev *dev = thor_func->dev; |
| 693 | |
| 694 | /* Wait for a device enumeration and configuration settings */ |
| 695 | debug("THOR enumeration/configuration setting....\n"); |
| 696 | while (!dev->configuration_done) |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 697 | dm_usb_gadget_handle_interrupts(udc); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 698 | |
| 699 | thor_set_dma(thor_rx_data_buf, strlen("THOR")); |
| 700 | /* detect the download request from Host PC */ |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 701 | if (thor_rx_data(udc) < 0) { |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 702 | printf("%s: Data not received!\n", __func__); |
| 703 | return -1; |
| 704 | } |
| 705 | |
| 706 | if (!strncmp((char *)thor_rx_data_buf, "THOR", strlen("THOR"))) { |
| 707 | puts("Download request from the Host PC\n"); |
| 708 | udelay(30 * 1000); /* 30 ms */ |
| 709 | |
| 710 | strcpy((char *)thor_tx_data_buf, "ROHT"); |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 711 | thor_tx_data(udc, thor_tx_data_buf, strlen("ROHT")); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 712 | } else { |
| 713 | puts("Wrong reply information\n"); |
| 714 | return -1; |
| 715 | } |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 720 | int thor_handle(struct udevice *udc) |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 721 | { |
| 722 | int ret; |
| 723 | |
| 724 | /* receive the data from Host PC */ |
| 725 | while (1) { |
| 726 | thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box)); |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 727 | ret = thor_rx_data(udc); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 728 | |
| 729 | if (ret > 0) { |
Marek Vasut | 8bfcb34 | 2023-09-01 11:49:59 +0200 | [diff] [blame^] | 730 | ret = process_data(udc); |
Siva Durga Prasad Paladugu | 4bad106 | 2015-04-16 10:38:34 +0200 | [diff] [blame] | 731 | #ifdef CONFIG_THOR_RESET_OFF |
| 732 | if (ret == RESET_DONE) |
| 733 | break; |
| 734 | #endif |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 735 | if (ret < 0) |
| 736 | return ret; |
| 737 | } else { |
| 738 | printf("%s: No data received!\n", __func__); |
| 739 | break; |
| 740 | } |
Marek Szyprowski | ee9b855 | 2020-12-22 11:32:24 +0100 | [diff] [blame] | 741 | if (dfu_reinit_needed) |
| 742 | return THOR_DFU_REINIT_NEEDED; |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | |
Seung-Woo Kim | e78aac1 | 2018-05-25 14:21:01 +0900 | [diff] [blame] | 748 | static void free_ep_req(struct usb_ep *ep, struct usb_request *req) |
| 749 | { |
| 750 | if (req->buf) |
| 751 | free(req->buf); |
| 752 | usb_ep_free_request(ep, req); |
| 753 | } |
| 754 | |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 755 | static int thor_func_bind(struct usb_configuration *c, struct usb_function *f) |
| 756 | { |
| 757 | struct usb_gadget *gadget = c->cdev->gadget; |
| 758 | struct f_thor *f_thor = func_to_thor(f); |
| 759 | struct thor_dev *dev; |
| 760 | struct usb_ep *ep; |
| 761 | int status; |
| 762 | |
| 763 | thor_func = f_thor; |
| 764 | dev = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*dev)); |
| 765 | if (!dev) |
| 766 | return -ENOMEM; |
| 767 | |
| 768 | memset(dev, 0, sizeof(*dev)); |
| 769 | dev->gadget = gadget; |
| 770 | f_thor->dev = dev; |
| 771 | |
| 772 | debug("%s: usb_configuration: 0x%p usb_function: 0x%p\n", |
| 773 | __func__, c, f); |
| 774 | debug("f_thor: 0x%p thor: 0x%p\n", f_thor, dev); |
| 775 | |
| 776 | /* EP0 */ |
| 777 | /* preallocate control response and buffer */ |
| 778 | dev->req = usb_ep_alloc_request(gadget->ep0, 0); |
| 779 | if (!dev->req) { |
| 780 | status = -ENOMEM; |
| 781 | goto fail; |
| 782 | } |
| 783 | dev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, |
Siva Durga Prasad Paladugu | 13b24e0 | 2015-04-15 13:42:19 +0200 | [diff] [blame] | 784 | THOR_PACKET_SIZE); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 785 | if (!dev->req->buf) { |
| 786 | status = -ENOMEM; |
| 787 | goto fail; |
| 788 | } |
| 789 | |
| 790 | dev->req->complete = thor_setup_complete; |
| 791 | |
| 792 | /* DYNAMIC interface numbers assignments */ |
| 793 | status = usb_interface_id(c, f); |
| 794 | |
| 795 | if (status < 0) |
| 796 | goto fail; |
| 797 | |
| 798 | thor_downloader_intf_int.bInterfaceNumber = status; |
| 799 | thor_downloader_cdc_union.bMasterInterface0 = status; |
| 800 | |
| 801 | status = usb_interface_id(c, f); |
| 802 | |
| 803 | if (status < 0) |
| 804 | goto fail; |
| 805 | |
| 806 | thor_downloader_intf_data.bInterfaceNumber = status; |
| 807 | thor_downloader_cdc_union.bSlaveInterface0 = status; |
| 808 | |
| 809 | /* allocate instance-specific endpoints */ |
| 810 | ep = usb_ep_autoconfig(gadget, &fs_in_desc); |
| 811 | if (!ep) { |
| 812 | status = -ENODEV; |
| 813 | goto fail; |
| 814 | } |
| 815 | |
| 816 | if (gadget_is_dualspeed(gadget)) { |
| 817 | hs_in_desc.bEndpointAddress = |
| 818 | fs_in_desc.bEndpointAddress; |
| 819 | } |
| 820 | |
| 821 | dev->in_ep = ep; /* Store IN EP for enabling @ setup */ |
Marek Szyprowski | da3f27f | 2015-03-03 17:32:16 +0100 | [diff] [blame] | 822 | ep->driver_data = dev; |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 823 | |
| 824 | ep = usb_ep_autoconfig(gadget, &fs_out_desc); |
| 825 | if (!ep) { |
| 826 | status = -ENODEV; |
| 827 | goto fail; |
| 828 | } |
| 829 | |
| 830 | if (gadget_is_dualspeed(gadget)) |
| 831 | hs_out_desc.bEndpointAddress = |
| 832 | fs_out_desc.bEndpointAddress; |
| 833 | |
| 834 | dev->out_ep = ep; /* Store OUT EP for enabling @ setup */ |
Marek Szyprowski | da3f27f | 2015-03-03 17:32:16 +0100 | [diff] [blame] | 835 | ep->driver_data = dev; |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 836 | |
| 837 | ep = usb_ep_autoconfig(gadget, &fs_int_desc); |
| 838 | if (!ep) { |
| 839 | status = -ENODEV; |
| 840 | goto fail; |
| 841 | } |
| 842 | |
| 843 | dev->int_ep = ep; |
Marek Szyprowski | da3f27f | 2015-03-03 17:32:16 +0100 | [diff] [blame] | 844 | ep->driver_data = dev; |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 845 | |
| 846 | if (gadget_is_dualspeed(gadget)) { |
| 847 | hs_int_desc.bEndpointAddress = |
| 848 | fs_int_desc.bEndpointAddress; |
| 849 | |
| 850 | f->hs_descriptors = (struct usb_descriptor_header **) |
| 851 | &hs_thor_downloader_function; |
| 852 | |
| 853 | if (!f->hs_descriptors) |
| 854 | goto fail; |
| 855 | } |
| 856 | |
| 857 | debug("%s: out_ep:%p out_req:%p\n", __func__, |
| 858 | dev->out_ep, dev->out_req); |
| 859 | |
| 860 | return 0; |
| 861 | |
| 862 | fail: |
Seung-Woo Kim | e78aac1 | 2018-05-25 14:21:01 +0900 | [diff] [blame] | 863 | if (dev->req) |
| 864 | free_ep_req(gadget->ep0, dev->req); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 865 | free(dev); |
| 866 | return status; |
| 867 | } |
| 868 | |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 869 | static void thor_unbind(struct usb_configuration *c, struct usb_function *f) |
| 870 | { |
| 871 | struct f_thor *f_thor = func_to_thor(f); |
| 872 | struct thor_dev *dev = f_thor->dev; |
| 873 | |
Seung-Woo Kim | e78aac1 | 2018-05-25 14:21:01 +0900 | [diff] [blame] | 874 | free_ep_req(dev->gadget->ep0, dev->req); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 875 | free(dev); |
| 876 | memset(thor_func, 0, sizeof(*thor_func)); |
| 877 | thor_func = NULL; |
| 878 | } |
| 879 | |
| 880 | static void thor_func_disable(struct usb_function *f) |
| 881 | { |
| 882 | struct f_thor *f_thor = func_to_thor(f); |
| 883 | struct thor_dev *dev = f_thor->dev; |
| 884 | |
| 885 | debug("%s:\n", __func__); |
| 886 | |
| 887 | /* Avoid freeing memory when ep is still claimed */ |
| 888 | if (dev->in_ep->driver_data) { |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 889 | usb_ep_disable(dev->in_ep); |
Seung-Woo Kim | 1661bf2 | 2018-06-04 15:53:39 +0900 | [diff] [blame] | 890 | free_ep_req(dev->in_ep, dev->in_req); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 891 | dev->in_ep->driver_data = NULL; |
| 892 | } |
| 893 | |
| 894 | if (dev->out_ep->driver_data) { |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 895 | usb_ep_disable(dev->out_ep); |
Seung-Woo Kim | 1661bf2 | 2018-06-04 15:53:39 +0900 | [diff] [blame] | 896 | usb_ep_free_request(dev->out_ep, dev->out_req); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 897 | dev->out_ep->driver_data = NULL; |
| 898 | } |
| 899 | |
| 900 | if (dev->int_ep->driver_data) { |
| 901 | usb_ep_disable(dev->int_ep); |
| 902 | dev->int_ep->driver_data = NULL; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | static int thor_eps_setup(struct usb_function *f) |
| 907 | { |
| 908 | struct usb_composite_dev *cdev = f->config->cdev; |
| 909 | struct usb_gadget *gadget = cdev->gadget; |
| 910 | struct thor_dev *dev = thor_func->dev; |
| 911 | struct usb_endpoint_descriptor *d; |
| 912 | struct usb_request *req; |
| 913 | struct usb_ep *ep; |
| 914 | int result; |
| 915 | |
| 916 | ep = dev->in_ep; |
| 917 | d = ep_desc(gadget, &hs_in_desc, &fs_in_desc); |
| 918 | debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress); |
| 919 | |
| 920 | result = usb_ep_enable(ep, d); |
| 921 | if (result) |
Seung-Woo Kim | e78aac1 | 2018-05-25 14:21:01 +0900 | [diff] [blame] | 922 | goto err; |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 923 | |
| 924 | ep->driver_data = cdev; /* claim */ |
Seung-Woo Kim | e78aac1 | 2018-05-25 14:21:01 +0900 | [diff] [blame] | 925 | req = alloc_ep_req(ep, THOR_PACKET_SIZE); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 926 | if (!req) { |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 927 | result = -EIO; |
Seung-Woo Kim | e78aac1 | 2018-05-25 14:21:01 +0900 | [diff] [blame] | 928 | goto err_disable_in_ep; |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 929 | } |
| 930 | |
Seung-Woo Kim | e78aac1 | 2018-05-25 14:21:01 +0900 | [diff] [blame] | 931 | memset(req->buf, 0, req->length); |
| 932 | req->complete = thor_rx_tx_complete; |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 933 | dev->in_req = req; |
| 934 | ep = dev->out_ep; |
| 935 | d = ep_desc(gadget, &hs_out_desc, &fs_out_desc); |
| 936 | debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress); |
| 937 | |
| 938 | result = usb_ep_enable(ep, d); |
| 939 | if (result) |
Seung-Woo Kim | e78aac1 | 2018-05-25 14:21:01 +0900 | [diff] [blame] | 940 | goto err_free_in_req; |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 941 | |
| 942 | ep->driver_data = cdev; /* claim */ |
Seung-Woo Kim | e78aac1 | 2018-05-25 14:21:01 +0900 | [diff] [blame] | 943 | req = usb_ep_alloc_request(ep, 0); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 944 | if (!req) { |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 945 | result = -EIO; |
Seung-Woo Kim | e78aac1 | 2018-05-25 14:21:01 +0900 | [diff] [blame] | 946 | goto err_disable_out_ep; |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 947 | } |
| 948 | |
Seung-Woo Kim | e78aac1 | 2018-05-25 14:21:01 +0900 | [diff] [blame] | 949 | req->complete = thor_rx_tx_complete; |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 950 | dev->out_req = req; |
| 951 | /* ACM control EP */ |
| 952 | ep = dev->int_ep; |
Marek Szyprowski | d2c7607 | 2019-10-02 12:27:18 +0200 | [diff] [blame] | 953 | d = ep_desc(gadget, &hs_int_desc, &fs_int_desc); |
| 954 | debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress); |
| 955 | |
| 956 | result = usb_ep_enable(ep, d); |
| 957 | if (result) |
| 958 | goto err; |
| 959 | |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 960 | ep->driver_data = cdev; /* claim */ |
| 961 | |
Seung-Woo Kim | e78aac1 | 2018-05-25 14:21:01 +0900 | [diff] [blame] | 962 | return 0; |
| 963 | |
| 964 | err_disable_out_ep: |
| 965 | usb_ep_disable(dev->out_ep); |
| 966 | |
| 967 | err_free_in_req: |
| 968 | free_ep_req(dev->in_ep, dev->in_req); |
| 969 | dev->in_req = NULL; |
| 970 | |
| 971 | err_disable_in_ep: |
| 972 | usb_ep_disable(dev->in_ep); |
| 973 | |
| 974 | err: |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 975 | return result; |
| 976 | } |
| 977 | |
| 978 | static int thor_func_set_alt(struct usb_function *f, |
| 979 | unsigned intf, unsigned alt) |
| 980 | { |
| 981 | struct thor_dev *dev = thor_func->dev; |
| 982 | int result; |
| 983 | |
| 984 | debug("%s: func: %s intf: %d alt: %d\n", |
| 985 | __func__, f->name, intf, alt); |
| 986 | |
| 987 | switch (intf) { |
| 988 | case 0: |
| 989 | debug("ACM INTR interface\n"); |
| 990 | break; |
| 991 | case 1: |
| 992 | debug("Communication Data interface\n"); |
| 993 | result = thor_eps_setup(f); |
| 994 | if (result) |
Seung-Woo Kim | b87b94e | 2020-02-13 17:24:11 +0900 | [diff] [blame] | 995 | pr_err("%s: EPs setup failed!\n", __func__); |
Lukasz Majewski | 61e5493 | 2013-10-08 14:30:42 +0200 | [diff] [blame] | 996 | dev->configuration_done = 1; |
| 997 | break; |
| 998 | } |
| 999 | |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | static int thor_func_init(struct usb_configuration *c) |
| 1004 | { |
| 1005 | struct f_thor *f_thor; |
| 1006 | int status; |
| 1007 | |
| 1008 | debug("%s: cdev: 0x%p\n", __func__, c->cdev); |
| 1009 | |
| 1010 | f_thor = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_thor)); |
| 1011 | if (!f_thor) |
| 1012 | return -ENOMEM; |
| 1013 | |
| 1014 | memset(f_thor, 0, sizeof(*f_thor)); |
| 1015 | |
| 1016 | f_thor->usb_function.name = "f_thor"; |
| 1017 | f_thor->usb_function.bind = thor_func_bind; |
| 1018 | f_thor->usb_function.unbind = thor_unbind; |
| 1019 | f_thor->usb_function.setup = thor_func_setup; |
| 1020 | f_thor->usb_function.set_alt = thor_func_set_alt; |
| 1021 | f_thor->usb_function.disable = thor_func_disable; |
| 1022 | |
| 1023 | status = usb_add_function(c, &f_thor->usb_function); |
| 1024 | if (status) |
| 1025 | free(f_thor); |
| 1026 | |
| 1027 | return status; |
| 1028 | } |
| 1029 | |
| 1030 | int thor_add(struct usb_configuration *c) |
| 1031 | { |
| 1032 | debug("%s:\n", __func__); |
| 1033 | return thor_func_init(c); |
| 1034 | } |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 1035 | |
| 1036 | DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor, thor_add); |