blob: 0e7529dcdbb65df289d1077153fee3babfa56c77 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski61e54932013-10-08 14:30:42 +02002/*
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 Majewski61e54932013-10-08 14:30:42 +020015 */
16
Simon Glassadaaa482019-11-14 12:57:43 -070017#include <command.h>
Lukasz Majewski61e54932013-10-08 14:30:42 +020018#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -070019#include <console.h>
Marek Vasut8bfcb342023-09-01 11:49:59 +020020#include <dm.h>
21#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060022#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060023#include <log.h>
Lukasz Majewski61e54932013-10-08 14:30:42 +020024#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060025#include <memalign.h>
Lukasz Majewski61e54932013-10-08 14:30:42 +020026#include <version.h>
Simon Glassdbd79542020-05-10 11:40:11 -060027#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060028#include <linux/printk.h>
Lukasz Majewski61e54932013-10-08 14:30:42 +020029#include <linux/usb/ch9.h>
30#include <linux/usb/gadget.h>
31#include <linux/usb/composite.h>
32#include <linux/usb/cdc.h>
33#include <g_dnl.h>
34#include <dfu.h>
Marek Szyprowskiee9b8552020-12-22 11:32:24 +010035#include <thor.h>
Lukasz Majewski61e54932013-10-08 14:30:42 +020036
37#include "f_thor.h"
38
Marek Vasut8bfcb342023-09-01 11:49:59 +020039static void thor_tx_data(struct udevice *udc, unsigned char *data, int len);
Lukasz Majewski61e54932013-10-08 14:30:42 +020040static void thor_set_dma(void *addr, int len);
Marek Vasut8bfcb342023-09-01 11:49:59 +020041static int thor_rx_data(struct udevice *udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +020042
43static struct f_thor *thor_func;
44static inline struct f_thor *func_to_thor(struct usb_function *f)
45{
46 return container_of(f, struct f_thor, usb_function);
47}
48
49DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_tx_data_buf,
50 sizeof(struct rsp_box));
51DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_rx_data_buf,
52 sizeof(struct rqt_box));
53
54/* ********************************************************** */
55/* THOR protocol - transmission handling */
56/* ********************************************************** */
Seung-Woo Kim16b74aa2018-05-10 10:52:14 +090057DEFINE_CACHE_ALIGN_BUFFER(char, f_name, F_NAME_BUF_SIZE + 1);
Lukasz Majewski61e54932013-10-08 14:30:42 +020058static unsigned long long int thor_file_size;
59static int alt_setting_num;
60
Marek Vasut8bfcb342023-09-01 11:49:59 +020061static void send_rsp(struct udevice *udc, const struct rsp_box *rsp)
Lukasz Majewski61e54932013-10-08 14:30:42 +020062{
63 memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box));
Marek Vasut8bfcb342023-09-01 11:49:59 +020064 thor_tx_data(udc, thor_tx_data_buf, sizeof(struct rsp_box));
Lukasz Majewski61e54932013-10-08 14:30:42 +020065
66 debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data);
67}
68
Marek Vasut8bfcb342023-09-01 11:49:59 +020069static void send_data_rsp(struct udevice *udc, s32 ack, s32 count)
Lukasz Majewski61e54932013-10-08 14:30:42 +020070{
71 ALLOC_CACHE_ALIGN_BUFFER(struct data_rsp_box, rsp,
72 sizeof(struct data_rsp_box));
73
74 rsp->ack = ack;
75 rsp->count = count;
76
77 memcpy(thor_tx_data_buf, rsp, sizeof(struct data_rsp_box));
Marek Vasut8bfcb342023-09-01 11:49:59 +020078 thor_tx_data(udc, thor_tx_data_buf, sizeof(struct data_rsp_box));
Lukasz Majewski61e54932013-10-08 14:30:42 +020079
80 debug("-DATA RSP: %d, %d\n", ack, count);
81}
82
Marek Vasut8bfcb342023-09-01 11:49:59 +020083static int process_rqt_info(struct udevice *udc, const struct rqt_box *rqt)
Lukasz Majewski61e54932013-10-08 14:30:42 +020084{
85 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
86 memset(rsp, 0, sizeof(struct rsp_box));
87
88 rsp->rsp = rqt->rqt;
89 rsp->rsp_data = rqt->rqt_data;
90
91 switch (rqt->rqt_data) {
92 case RQT_INFO_VER_PROTOCOL:
93 rsp->int_data[0] = VER_PROTOCOL_MAJOR;
94 rsp->int_data[1] = VER_PROTOCOL_MINOR;
95 break;
96 case RQT_INIT_VER_HW:
97 snprintf(rsp->str_data[0], sizeof(rsp->str_data[0]),
98 "%x", checkboard());
99 break;
100 case RQT_INIT_VER_BOOT:
101 sprintf(rsp->str_data[0], "%s", U_BOOT_VERSION);
102 break;
103 case RQT_INIT_VER_KERNEL:
104 sprintf(rsp->str_data[0], "%s", "k unknown");
105 break;
106 case RQT_INIT_VER_PLATFORM:
107 sprintf(rsp->str_data[0], "%s", "p unknown");
108 break;
109 case RQT_INIT_VER_CSC:
110 sprintf(rsp->str_data[0], "%s", "c unknown");
111 break;
112 default:
113 return -EINVAL;
114 }
115
Marek Vasut8bfcb342023-09-01 11:49:59 +0200116 send_rsp(udc, rsp);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200117 return true;
118}
119
Marek Vasut8bfcb342023-09-01 11:49:59 +0200120static int process_rqt_cmd(struct udevice *udc, const struct rqt_box *rqt)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200121{
122 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
123 memset(rsp, 0, sizeof(struct rsp_box));
124
125 rsp->rsp = rqt->rqt;
126 rsp->rsp_data = rqt->rqt_data;
127
128 switch (rqt->rqt_data) {
129 case RQT_CMD_REBOOT:
130 debug("TARGET RESET\n");
Marek Vasut8bfcb342023-09-01 11:49:59 +0200131 send_rsp(udc, rsp);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200132 g_dnl_unregister();
133 dfu_free_entities();
Siva Durga Prasad Paladugu4bad1062015-04-16 10:38:34 +0200134#ifdef CONFIG_THOR_RESET_OFF
135 return RESET_DONE;
136#endif
Lukasz Majewski61e54932013-10-08 14:30:42 +0200137 run_command("reset", 0);
138 break;
139 case RQT_CMD_POWEROFF:
140 case RQT_CMD_EFSCLEAR:
Marek Vasut8bfcb342023-09-01 11:49:59 +0200141 send_rsp(udc, rsp);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200142 default:
143 printf("Command not supported -> cmd: %d\n", rqt->rqt_data);
144 return -EINVAL;
145 }
146
147 return true;
148}
149
Marek Vasut8bfcb342023-09-01 11:49:59 +0200150static long long int download_head(struct udevice *udc,
151 unsigned long long total,
Lukasz Majewski61e54932013-10-08 14:30:42 +0200152 unsigned int packet_size,
153 long long int *left,
154 int *cnt)
155{
156 long long int rcv_cnt = 0, left_to_rcv, ret_rcv;
Stephen Warrenf9500982014-06-11 16:03:34 -0600157 struct dfu_entity *dfu_entity = dfu_get_entity(alt_setting_num);
158 void *transfer_buffer = dfu_get_buf(dfu_entity);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200159 void *buf = transfer_buffer;
160 int usb_pkt_cnt = 0, ret;
161
162 /*
163 * Files smaller than THOR_STORE_UNIT_SIZE (now 32 MiB) are stored on
164 * the medium.
165 * The packet response is sent on the purpose after successful data
166 * chunk write. There is a room for improvement when asynchronous write
167 * is performed.
168 */
169 while (total - rcv_cnt >= packet_size) {
170 thor_set_dma(buf, packet_size);
171 buf += packet_size;
Marek Vasut8bfcb342023-09-01 11:49:59 +0200172 ret_rcv = thor_rx_data(udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200173 if (ret_rcv < 0)
174 return ret_rcv;
175 rcv_cnt += ret_rcv;
176 debug("%d: RCV data count: %llu cnt: %d\n", usb_pkt_cnt,
177 rcv_cnt, *cnt);
178
179 if ((rcv_cnt % THOR_STORE_UNIT_SIZE) == 0) {
180 ret = dfu_write(dfu_get_entity(alt_setting_num),
181 transfer_buffer, THOR_STORE_UNIT_SIZE,
182 (*cnt)++);
183 if (ret) {
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900184 pr_err("DFU write failed [%d] cnt: %d\n",
Lukasz Majewski61e54932013-10-08 14:30:42 +0200185 ret, *cnt);
186 return ret;
187 }
188 buf = transfer_buffer;
189 }
Marek Vasut8bfcb342023-09-01 11:49:59 +0200190 send_data_rsp(udc, 0, ++usb_pkt_cnt);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200191 }
192
193 /* Calculate the amount of data to arrive from PC (in bytes) */
194 left_to_rcv = total - rcv_cnt;
195
196 /*
197 * Calculate number of data already received. but not yet stored
198 * on the medium (they are smaller than THOR_STORE_UNIT_SIZE)
199 */
200 *left = left_to_rcv + buf - transfer_buffer;
201 debug("%s: left: %llu left_to_rcv: %llu buf: 0x%p\n", __func__,
202 *left, left_to_rcv, buf);
203
204 if (left_to_rcv) {
205 thor_set_dma(buf, packet_size);
Marek Vasut8bfcb342023-09-01 11:49:59 +0200206 ret_rcv = thor_rx_data(udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200207 if (ret_rcv < 0)
208 return ret_rcv;
209 rcv_cnt += ret_rcv;
Marek Vasut8bfcb342023-09-01 11:49:59 +0200210 send_data_rsp(udc, 0, ++usb_pkt_cnt);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200211 }
212
213 debug("%s: %llu total: %llu cnt: %d\n", __func__, rcv_cnt, total, *cnt);
214
215 return rcv_cnt;
216}
217
218static int download_tail(long long int left, int cnt)
219{
Przemyslaw Marczaka5071a02014-12-15 10:34:10 +0100220 struct dfu_entity *dfu_entity;
221 void *transfer_buffer;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200222 int ret;
223
224 debug("%s: left: %llu cnt: %d\n", __func__, left, cnt);
225
Przemyslaw Marczaka5071a02014-12-15 10:34:10 +0100226 dfu_entity = dfu_get_entity(alt_setting_num);
227 if (!dfu_entity) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900228 pr_err("Alt setting: %d entity not found!\n", alt_setting_num);
Przemyslaw Marczaka5071a02014-12-15 10:34:10 +0100229 return -ENOENT;
230 }
231
232 transfer_buffer = dfu_get_buf(dfu_entity);
233 if (!transfer_buffer) {
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900234 pr_err("Transfer buffer not allocated!\n");
Przemyslaw Marczaka5071a02014-12-15 10:34:10 +0100235 return -ENXIO;
236 }
237
Lukasz Majewski61e54932013-10-08 14:30:42 +0200238 if (left) {
Przemyslaw Marczak13a41c82014-04-18 09:48:24 +0200239 ret = dfu_write(dfu_entity, transfer_buffer, left, cnt++);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200240 if (ret) {
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900241 pr_err("DFU write failed[%d]: left: %llu\n", ret, left);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200242 return ret;
243 }
244 }
245
246 /*
Przemyslaw Marczak065a27f2014-05-12 12:05:34 +0200247 * To store last "packet" or write file from buffer to filesystem
248 * DFU storage backend requires dfu_flush
Lukasz Majewski61e54932013-10-08 14:30:42 +0200249 *
250 * This also frees memory malloc'ed by dfu_get_buf(), so no explicit
251 * need fo call dfu_free_buf() is needed.
252 */
Przemyslaw Marczakef9d4d42014-04-18 09:48:25 +0200253 ret = dfu_flush(dfu_entity, transfer_buffer, 0, cnt);
Przemyslaw Marczak065a27f2014-05-12 12:05:34 +0200254 if (ret)
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900255 pr_err("DFU flush failed!\n");
Przemyslaw Marczakef9d4d42014-04-18 09:48:25 +0200256
Lukasz Majewski61e54932013-10-08 14:30:42 +0200257 return ret;
258}
259
Marek Vasut8bfcb342023-09-01 11:49:59 +0200260static long long int process_rqt_download(struct udevice *udc, const struct rqt_box *rqt)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200261{
262 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
263 static long long int left, ret_head;
264 int file_type, ret = 0;
265 static int cnt;
266
267 memset(rsp, 0, sizeof(struct rsp_box));
268 rsp->rsp = rqt->rqt;
269 rsp->rsp_data = rqt->rqt_data;
270
271 switch (rqt->rqt_data) {
272 case RQT_DL_INIT:
Seung-Woo Kim2753cec2020-10-16 16:29:02 +0900273 thor_file_size = (uint64_t)(uint32_t)rqt->int_data[0] +
274 (((uint64_t)(uint32_t)rqt->int_data[1])
Seung-Woo Kim7414a0f2018-05-10 10:52:15 +0900275 << 32);
276 debug("INIT: total %llu bytes\n", thor_file_size);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200277 break;
278 case RQT_DL_FILE_INFO:
279 file_type = rqt->int_data[0];
280 if (file_type == FILE_TYPE_PIT) {
281 puts("PIT table file - not supported\n");
282 rsp->ack = -ENOTSUPP;
283 ret = rsp->ack;
284 break;
285 }
286
Seung-Woo Kim2753cec2020-10-16 16:29:02 +0900287 thor_file_size = (uint64_t)(uint32_t)rqt->int_data[1] +
288 (((uint64_t)(uint32_t)rqt->int_data[2])
Seung-Woo Kim7414a0f2018-05-10 10:52:15 +0900289 << 32);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200290 memcpy(f_name, rqt->str_data[0], F_NAME_BUF_SIZE);
Seung-Woo Kim16b74aa2018-05-10 10:52:14 +0900291 f_name[F_NAME_BUF_SIZE] = '\0';
Lukasz Majewski61e54932013-10-08 14:30:42 +0200292
293 debug("INFO: name(%s, %d), size(%llu), type(%d)\n",
294 f_name, 0, thor_file_size, file_type);
295
296 rsp->int_data[0] = THOR_PACKET_SIZE;
297
298 alt_setting_num = dfu_get_alt(f_name);
299 if (alt_setting_num < 0) {
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900300 pr_err("Alt setting [%d] to write not found!\n",
Lukasz Majewski61e54932013-10-08 14:30:42 +0200301 alt_setting_num);
302 rsp->ack = -ENODEV;
303 ret = rsp->ack;
304 }
305 break;
306 case RQT_DL_FILE_START:
Marek Vasut8bfcb342023-09-01 11:49:59 +0200307 send_rsp(udc, rsp);
308 ret_head = download_head(udc, thor_file_size, THOR_PACKET_SIZE,
Lukasz Majewski61e54932013-10-08 14:30:42 +0200309 &left, &cnt);
310 if (ret_head < 0) {
311 left = 0;
312 cnt = 0;
313 }
314 return ret_head;
315 case RQT_DL_FILE_END:
316 debug("DL FILE_END\n");
317 rsp->ack = download_tail(left, cnt);
318 ret = rsp->ack;
319 left = 0;
320 cnt = 0;
321 break;
322 case RQT_DL_EXIT:
323 debug("DL EXIT\n");
324 break;
325 default:
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900326 pr_err("Operation not supported: %d\n", rqt->rqt_data);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200327 ret = -ENOTSUPP;
328 }
329
Marek Vasut8bfcb342023-09-01 11:49:59 +0200330 send_rsp(udc, rsp);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200331 return ret;
332}
333
Marek Vasut8bfcb342023-09-01 11:49:59 +0200334static int process_data(struct udevice *udc)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200335{
336 ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box));
337 int ret = -EINVAL;
338
Lukasz Majewski61e54932013-10-08 14:30:42 +0200339 memcpy(rqt, thor_rx_data_buf, sizeof(struct rqt_box));
340
341 debug("+RQT: %d, %d\n", rqt->rqt, rqt->rqt_data);
342
343 switch (rqt->rqt) {
344 case RQT_INFO:
Marek Vasut8bfcb342023-09-01 11:49:59 +0200345 ret = process_rqt_info(udc, rqt);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200346 break;
347 case RQT_CMD:
Marek Vasut8bfcb342023-09-01 11:49:59 +0200348 ret = process_rqt_cmd(udc, rqt);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200349 break;
350 case RQT_DL:
Marek Vasut8bfcb342023-09-01 11:49:59 +0200351 ret = (int) process_rqt_download(udc, rqt);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200352 break;
353 case RQT_UL:
354 puts("RQT: UPLOAD not supported!\n");
355 break;
356 default:
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900357 pr_err("unknown request (%d)\n", rqt->rqt);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200358 }
359
360 return ret;
361}
362
363/* ********************************************************** */
364/* THOR USB Function */
365/* ********************************************************** */
366
367static inline struct usb_endpoint_descriptor *
368ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
369 struct usb_endpoint_descriptor *fs)
370{
371 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
372 return hs;
373 return fs;
374}
375
376static struct usb_interface_descriptor thor_downloader_intf_data = {
377 .bLength = sizeof(thor_downloader_intf_data),
378 .bDescriptorType = USB_DT_INTERFACE,
379
380 .bNumEndpoints = 2,
381 .bInterfaceClass = USB_CLASS_CDC_DATA,
382};
383
384static struct usb_endpoint_descriptor fs_in_desc = {
385 .bLength = USB_DT_ENDPOINT_SIZE,
386 .bDescriptorType = USB_DT_ENDPOINT,
387
388 .bEndpointAddress = USB_DIR_IN,
389 .bmAttributes = USB_ENDPOINT_XFER_BULK,
390};
391
392static struct usb_endpoint_descriptor fs_out_desc = {
393 .bLength = USB_DT_ENDPOINT_SIZE,
394 .bDescriptorType = USB_DT_ENDPOINT,
395
396 .bEndpointAddress = USB_DIR_OUT,
397 .bmAttributes = USB_ENDPOINT_XFER_BULK,
398};
399
400/* CDC configuration */
401static struct usb_interface_descriptor thor_downloader_intf_int = {
402 .bLength = sizeof(thor_downloader_intf_int),
403 .bDescriptorType = USB_DT_INTERFACE,
404
405 .bNumEndpoints = 1,
406 .bInterfaceClass = USB_CLASS_COMM,
407 /* 0x02 Abstract Line Control Model */
408 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
409 /* 0x01 Common AT commands */
410 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
411};
412
413static struct usb_cdc_header_desc thor_downloader_cdc_header = {
414 .bLength = sizeof(thor_downloader_cdc_header),
415 .bDescriptorType = 0x24, /* CS_INTERFACE */
416 .bDescriptorSubType = 0x00,
417 .bcdCDC = 0x0110,
418};
419
420static struct usb_cdc_call_mgmt_descriptor thor_downloader_cdc_call = {
421 .bLength = sizeof(thor_downloader_cdc_call),
422 .bDescriptorType = 0x24, /* CS_INTERFACE */
423 .bDescriptorSubType = 0x01,
424 .bmCapabilities = 0x00,
425 .bDataInterface = 0x01,
426};
427
428static struct usb_cdc_acm_descriptor thor_downloader_cdc_abstract = {
429 .bLength = sizeof(thor_downloader_cdc_abstract),
430 .bDescriptorType = 0x24, /* CS_INTERFACE */
431 .bDescriptorSubType = 0x02,
432 .bmCapabilities = 0x00,
433};
434
435static struct usb_cdc_union_desc thor_downloader_cdc_union = {
436 .bLength = sizeof(thor_downloader_cdc_union),
437 .bDescriptorType = 0x24, /* CS_INTERFACE */
438 .bDescriptorSubType = USB_CDC_UNION_TYPE,
439};
440
441static struct usb_endpoint_descriptor fs_int_desc = {
442 .bLength = USB_DT_ENDPOINT_SIZE,
443 .bDescriptorType = USB_DT_ENDPOINT,
444
445 .bEndpointAddress = 3 | USB_DIR_IN,
446 .bmAttributes = USB_ENDPOINT_XFER_INT,
447 .wMaxPacketSize = __constant_cpu_to_le16(16),
448
449 .bInterval = 0x9,
450};
451
452static struct usb_interface_assoc_descriptor
453thor_iad_descriptor = {
454 .bLength = sizeof(thor_iad_descriptor),
455 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
456
457 .bFirstInterface = 0,
458 .bInterfaceCount = 2, /* control + data */
459 .bFunctionClass = USB_CLASS_COMM,
460 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
461 .bFunctionProtocol = USB_CDC_PROTO_NONE,
462};
463
464static struct usb_endpoint_descriptor hs_in_desc = {
465 .bLength = USB_DT_ENDPOINT_SIZE,
466 .bDescriptorType = USB_DT_ENDPOINT,
467
468 .bmAttributes = USB_ENDPOINT_XFER_BULK,
469 .wMaxPacketSize = __constant_cpu_to_le16(512),
470};
471
472static struct usb_endpoint_descriptor hs_out_desc = {
473 .bLength = USB_DT_ENDPOINT_SIZE,
474 .bDescriptorType = USB_DT_ENDPOINT,
475
476 .bmAttributes = USB_ENDPOINT_XFER_BULK,
477 .wMaxPacketSize = __constant_cpu_to_le16(512),
478};
479
480static struct usb_endpoint_descriptor hs_int_desc = {
481 .bLength = USB_DT_ENDPOINT_SIZE,
482 .bDescriptorType = USB_DT_ENDPOINT,
483
484 .bmAttributes = USB_ENDPOINT_XFER_INT,
485 .wMaxPacketSize = __constant_cpu_to_le16(16),
486
487 .bInterval = 0x9,
488};
489
Lukasz Majewski61e54932013-10-08 14:30:42 +0200490/*
491 * This attribute vendor descriptor is necessary for correct operation with
492 * Windows version of THOR download program
493 *
494 * It prevents windows driver from sending zero lenght packet (ZLP) after
495 * each THOR_PACKET_SIZE. This assures consistent behaviour with libusb
496 */
497static struct usb_cdc_attribute_vendor_descriptor thor_downloader_cdc_av = {
498 .bLength = sizeof(thor_downloader_cdc_av),
499 .bDescriptorType = 0x24,
500 .bDescriptorSubType = 0x80,
501 .DAUType = 0x0002,
502 .DAULength = 0x0001,
503 .DAUValue = 0x00,
504};
505
506static const struct usb_descriptor_header *hs_thor_downloader_function[] = {
507 (struct usb_descriptor_header *)&thor_iad_descriptor,
508
509 (struct usb_descriptor_header *)&thor_downloader_intf_int,
510 (struct usb_descriptor_header *)&thor_downloader_cdc_header,
511 (struct usb_descriptor_header *)&thor_downloader_cdc_call,
512 (struct usb_descriptor_header *)&thor_downloader_cdc_abstract,
513 (struct usb_descriptor_header *)&thor_downloader_cdc_union,
514 (struct usb_descriptor_header *)&hs_int_desc,
515
516 (struct usb_descriptor_header *)&thor_downloader_intf_data,
517 (struct usb_descriptor_header *)&thor_downloader_cdc_av,
518 (struct usb_descriptor_header *)&hs_in_desc,
519 (struct usb_descriptor_header *)&hs_out_desc,
520 NULL,
521};
522
523/*-------------------------------------------------------------------------*/
524static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
525{
526 struct usb_request *req;
527
528 req = usb_ep_alloc_request(ep, 0);
529 if (!req)
530 return req;
531
532 req->length = length;
533 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
534 if (!req->buf) {
535 usb_ep_free_request(ep, req);
536 req = NULL;
537 }
538
539 return req;
540}
541
Marek Vasut8bfcb342023-09-01 11:49:59 +0200542static int thor_rx_data(struct udevice *udc)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200543{
544 struct thor_dev *dev = thor_func->dev;
545 int data_to_rx, tmp, status;
546
547 data_to_rx = dev->out_req->length;
548 tmp = data_to_rx;
549 do {
550 dev->out_req->length = data_to_rx;
551 debug("dev->out_req->length:%d dev->rxdata:%d\n",
552 dev->out_req->length, dev->rxdata);
553
554 status = usb_ep_queue(dev->out_ep, dev->out_req, 0);
555 if (status) {
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900556 pr_err("kill %s: resubmit %d bytes --> %d\n",
Lukasz Majewski61e54932013-10-08 14:30:42 +0200557 dev->out_ep->name, dev->out_req->length, status);
558 usb_ep_set_halt(dev->out_ep);
559 return -EAGAIN;
560 }
561
562 while (!dev->rxdata) {
Marek Vasut8bfcb342023-09-01 11:49:59 +0200563 dm_usb_gadget_handle_interrupts(udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200564 if (ctrlc())
565 return -1;
566 }
567 dev->rxdata = 0;
568 data_to_rx -= dev->out_req->actual;
569 } while (data_to_rx);
570
571 return tmp;
572}
573
Marek Vasut8bfcb342023-09-01 11:49:59 +0200574static void thor_tx_data(struct udevice *udc, unsigned char *data, int len)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200575{
576 struct thor_dev *dev = thor_func->dev;
577 unsigned char *ptr = dev->in_req->buf;
578 int status;
579
580 memset(ptr, 0, len);
581 memcpy(ptr, data, len);
582
583 dev->in_req->length = len;
584
Michal Simek6c7d0032015-10-30 16:19:43 +0100585 debug("%s: dev->in_req->length:%d to_cpy:%zd\n", __func__,
Lukasz Majewski61e54932013-10-08 14:30:42 +0200586 dev->in_req->length, sizeof(data));
587
588 status = usb_ep_queue(dev->in_ep, dev->in_req, 0);
589 if (status) {
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900590 pr_err("kill %s: resubmit %d bytes --> %d\n",
Lukasz Majewski61e54932013-10-08 14:30:42 +0200591 dev->in_ep->name, dev->in_req->length, status);
592 usb_ep_set_halt(dev->in_ep);
593 }
594
595 /* Wait until tx interrupt received */
596 while (!dev->txdata)
Marek Vasut8bfcb342023-09-01 11:49:59 +0200597 dm_usb_gadget_handle_interrupts(udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200598
599 dev->txdata = 0;
600}
601
602static void thor_rx_tx_complete(struct usb_ep *ep, struct usb_request *req)
603{
604 struct thor_dev *dev = thor_func->dev;
605 int status = req->status;
606
607 debug("%s: ep_ptr:%p, req_ptr:%p\n", __func__, ep, req);
608 switch (status) {
609 case 0:
610 if (ep == dev->out_ep)
611 dev->rxdata = 1;
612 else
613 dev->txdata = 1;
614
615 break;
616
617 /* this endpoint is normally active while we're configured */
618 case -ECONNABORTED: /* hardware forced ep reset */
619 case -ECONNRESET: /* request dequeued */
620 case -ESHUTDOWN: /* disconnect from host */
621 case -EREMOTEIO: /* short read */
622 case -EOVERFLOW:
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900623 pr_err("ERROR:%d\n", status);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200624 break;
625 }
626
627 debug("%s complete --> %d, %d/%d\n", ep->name,
628 status, req->actual, req->length);
629}
630
Lukasz Majewski61e54932013-10-08 14:30:42 +0200631static void thor_setup_complete(struct usb_ep *ep, struct usb_request *req)
632{
633 if (req->status || req->actual != req->length)
634 debug("setup complete --> %d, %d/%d\n",
635 req->status, req->actual, req->length);
636}
637
638static int
639thor_func_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
640{
641 struct thor_dev *dev = thor_func->dev;
642 struct usb_request *req = dev->req;
643 struct usb_gadget *gadget = dev->gadget;
644 int value = 0;
645
646 u16 len = le16_to_cpu(ctrl->wLength);
647
648 debug("Req_Type: 0x%x Req: 0x%x wValue: 0x%x wIndex: 0x%x wLen: 0x%x\n",
649 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, ctrl->wIndex,
650 ctrl->wLength);
651
652 switch (ctrl->bRequest) {
653 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
654 value = 0;
655 break;
656 case USB_CDC_REQ_SET_LINE_CODING:
657 value = len;
658 /* Line Coding set done = configuration done */
659 thor_func->dev->configuration_done = 1;
660 break;
661
662 default:
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900663 pr_err("thor_setup: unknown request: %d\n", ctrl->bRequest);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200664 }
665
666 if (value >= 0) {
667 req->length = value;
668 req->zero = value < len;
669 value = usb_ep_queue(gadget->ep0, req, 0);
670 if (value < 0) {
671 debug("%s: ep_queue: %d\n", __func__, value);
672 req->status = 0;
673 }
674 }
675
676 return value;
677}
678
679/* Specific to the THOR protocol */
680static void thor_set_dma(void *addr, int len)
681{
682 struct thor_dev *dev = thor_func->dev;
683
684 debug("in_req:%p, out_req:%p\n", dev->in_req, dev->out_req);
685 debug("addr:%p, len:%d\n", addr, len);
686
687 dev->out_req->buf = addr;
688 dev->out_req->length = len;
689}
690
Marek Vasut8bfcb342023-09-01 11:49:59 +0200691int thor_init(struct udevice *udc)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200692{
693 struct thor_dev *dev = thor_func->dev;
694
695 /* Wait for a device enumeration and configuration settings */
696 debug("THOR enumeration/configuration setting....\n");
697 while (!dev->configuration_done)
Marek Vasut8bfcb342023-09-01 11:49:59 +0200698 dm_usb_gadget_handle_interrupts(udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200699
700 thor_set_dma(thor_rx_data_buf, strlen("THOR"));
701 /* detect the download request from Host PC */
Marek Vasut8bfcb342023-09-01 11:49:59 +0200702 if (thor_rx_data(udc) < 0) {
Lukasz Majewski61e54932013-10-08 14:30:42 +0200703 printf("%s: Data not received!\n", __func__);
704 return -1;
705 }
706
707 if (!strncmp((char *)thor_rx_data_buf, "THOR", strlen("THOR"))) {
708 puts("Download request from the Host PC\n");
709 udelay(30 * 1000); /* 30 ms */
710
711 strcpy((char *)thor_tx_data_buf, "ROHT");
Marek Vasut8bfcb342023-09-01 11:49:59 +0200712 thor_tx_data(udc, thor_tx_data_buf, strlen("ROHT"));
Lukasz Majewski61e54932013-10-08 14:30:42 +0200713 } else {
714 puts("Wrong reply information\n");
715 return -1;
716 }
717
718 return 0;
719}
720
Marek Vasut8bfcb342023-09-01 11:49:59 +0200721int thor_handle(struct udevice *udc)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200722{
723 int ret;
724
725 /* receive the data from Host PC */
726 while (1) {
727 thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box));
Marek Vasut8bfcb342023-09-01 11:49:59 +0200728 ret = thor_rx_data(udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200729
730 if (ret > 0) {
Marek Vasut8bfcb342023-09-01 11:49:59 +0200731 ret = process_data(udc);
Siva Durga Prasad Paladugu4bad1062015-04-16 10:38:34 +0200732#ifdef CONFIG_THOR_RESET_OFF
733 if (ret == RESET_DONE)
734 break;
735#endif
Lukasz Majewski61e54932013-10-08 14:30:42 +0200736 if (ret < 0)
737 return ret;
738 } else {
739 printf("%s: No data received!\n", __func__);
740 break;
741 }
Marek Szyprowskiee9b8552020-12-22 11:32:24 +0100742 if (dfu_reinit_needed)
743 return THOR_DFU_REINIT_NEEDED;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200744 }
745
746 return 0;
747}
748
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900749static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
750{
751 if (req->buf)
752 free(req->buf);
753 usb_ep_free_request(ep, req);
754}
755
Lukasz Majewski61e54932013-10-08 14:30:42 +0200756static int thor_func_bind(struct usb_configuration *c, struct usb_function *f)
757{
758 struct usb_gadget *gadget = c->cdev->gadget;
759 struct f_thor *f_thor = func_to_thor(f);
760 struct thor_dev *dev;
761 struct usb_ep *ep;
762 int status;
763
764 thor_func = f_thor;
765 dev = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*dev));
766 if (!dev)
767 return -ENOMEM;
768
769 memset(dev, 0, sizeof(*dev));
770 dev->gadget = gadget;
771 f_thor->dev = dev;
772
773 debug("%s: usb_configuration: 0x%p usb_function: 0x%p\n",
774 __func__, c, f);
775 debug("f_thor: 0x%p thor: 0x%p\n", f_thor, dev);
776
777 /* EP0 */
778 /* preallocate control response and buffer */
779 dev->req = usb_ep_alloc_request(gadget->ep0, 0);
780 if (!dev->req) {
781 status = -ENOMEM;
782 goto fail;
783 }
784 dev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
Siva Durga Prasad Paladugu13b24e02015-04-15 13:42:19 +0200785 THOR_PACKET_SIZE);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200786 if (!dev->req->buf) {
787 status = -ENOMEM;
788 goto fail;
789 }
790
791 dev->req->complete = thor_setup_complete;
792
793 /* DYNAMIC interface numbers assignments */
794 status = usb_interface_id(c, f);
795
796 if (status < 0)
797 goto fail;
798
799 thor_downloader_intf_int.bInterfaceNumber = status;
800 thor_downloader_cdc_union.bMasterInterface0 = status;
801
802 status = usb_interface_id(c, f);
803
804 if (status < 0)
805 goto fail;
806
807 thor_downloader_intf_data.bInterfaceNumber = status;
808 thor_downloader_cdc_union.bSlaveInterface0 = status;
809
810 /* allocate instance-specific endpoints */
811 ep = usb_ep_autoconfig(gadget, &fs_in_desc);
812 if (!ep) {
813 status = -ENODEV;
814 goto fail;
815 }
816
817 if (gadget_is_dualspeed(gadget)) {
818 hs_in_desc.bEndpointAddress =
819 fs_in_desc.bEndpointAddress;
820 }
821
822 dev->in_ep = ep; /* Store IN EP for enabling @ setup */
Marek Szyprowskida3f27f2015-03-03 17:32:16 +0100823 ep->driver_data = dev;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200824
825 ep = usb_ep_autoconfig(gadget, &fs_out_desc);
826 if (!ep) {
827 status = -ENODEV;
828 goto fail;
829 }
830
831 if (gadget_is_dualspeed(gadget))
832 hs_out_desc.bEndpointAddress =
833 fs_out_desc.bEndpointAddress;
834
835 dev->out_ep = ep; /* Store OUT EP for enabling @ setup */
Marek Szyprowskida3f27f2015-03-03 17:32:16 +0100836 ep->driver_data = dev;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200837
838 ep = usb_ep_autoconfig(gadget, &fs_int_desc);
839 if (!ep) {
840 status = -ENODEV;
841 goto fail;
842 }
843
844 dev->int_ep = ep;
Marek Szyprowskida3f27f2015-03-03 17:32:16 +0100845 ep->driver_data = dev;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200846
847 if (gadget_is_dualspeed(gadget)) {
848 hs_int_desc.bEndpointAddress =
849 fs_int_desc.bEndpointAddress;
850
851 f->hs_descriptors = (struct usb_descriptor_header **)
852 &hs_thor_downloader_function;
853
854 if (!f->hs_descriptors)
855 goto fail;
856 }
857
858 debug("%s: out_ep:%p out_req:%p\n", __func__,
859 dev->out_ep, dev->out_req);
860
861 return 0;
862
863 fail:
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900864 if (dev->req)
865 free_ep_req(gadget->ep0, dev->req);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200866 free(dev);
867 return status;
868}
869
Lukasz Majewski61e54932013-10-08 14:30:42 +0200870static void thor_unbind(struct usb_configuration *c, struct usb_function *f)
871{
872 struct f_thor *f_thor = func_to_thor(f);
873 struct thor_dev *dev = f_thor->dev;
874
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900875 free_ep_req(dev->gadget->ep0, dev->req);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200876 free(dev);
877 memset(thor_func, 0, sizeof(*thor_func));
878 thor_func = NULL;
879}
880
881static void thor_func_disable(struct usb_function *f)
882{
883 struct f_thor *f_thor = func_to_thor(f);
884 struct thor_dev *dev = f_thor->dev;
885
886 debug("%s:\n", __func__);
887
888 /* Avoid freeing memory when ep is still claimed */
889 if (dev->in_ep->driver_data) {
Lukasz Majewski61e54932013-10-08 14:30:42 +0200890 usb_ep_disable(dev->in_ep);
Seung-Woo Kim1661bf22018-06-04 15:53:39 +0900891 free_ep_req(dev->in_ep, dev->in_req);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200892 dev->in_ep->driver_data = NULL;
893 }
894
895 if (dev->out_ep->driver_data) {
Lukasz Majewski61e54932013-10-08 14:30:42 +0200896 usb_ep_disable(dev->out_ep);
Seung-Woo Kim1661bf22018-06-04 15:53:39 +0900897 usb_ep_free_request(dev->out_ep, dev->out_req);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200898 dev->out_ep->driver_data = NULL;
899 }
900
901 if (dev->int_ep->driver_data) {
902 usb_ep_disable(dev->int_ep);
903 dev->int_ep->driver_data = NULL;
904 }
905}
906
907static int thor_eps_setup(struct usb_function *f)
908{
909 struct usb_composite_dev *cdev = f->config->cdev;
910 struct usb_gadget *gadget = cdev->gadget;
911 struct thor_dev *dev = thor_func->dev;
912 struct usb_endpoint_descriptor *d;
913 struct usb_request *req;
914 struct usb_ep *ep;
915 int result;
916
917 ep = dev->in_ep;
918 d = ep_desc(gadget, &hs_in_desc, &fs_in_desc);
919 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
920
921 result = usb_ep_enable(ep, d);
922 if (result)
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900923 goto err;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200924
925 ep->driver_data = cdev; /* claim */
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900926 req = alloc_ep_req(ep, THOR_PACKET_SIZE);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200927 if (!req) {
Lukasz Majewski61e54932013-10-08 14:30:42 +0200928 result = -EIO;
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900929 goto err_disable_in_ep;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200930 }
931
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900932 memset(req->buf, 0, req->length);
933 req->complete = thor_rx_tx_complete;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200934 dev->in_req = req;
935 ep = dev->out_ep;
936 d = ep_desc(gadget, &hs_out_desc, &fs_out_desc);
937 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
938
939 result = usb_ep_enable(ep, d);
940 if (result)
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900941 goto err_free_in_req;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200942
943 ep->driver_data = cdev; /* claim */
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900944 req = usb_ep_alloc_request(ep, 0);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200945 if (!req) {
Lukasz Majewski61e54932013-10-08 14:30:42 +0200946 result = -EIO;
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900947 goto err_disable_out_ep;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200948 }
949
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900950 req->complete = thor_rx_tx_complete;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200951 dev->out_req = req;
952 /* ACM control EP */
953 ep = dev->int_ep;
Marek Szyprowskid2c76072019-10-02 12:27:18 +0200954 d = ep_desc(gadget, &hs_int_desc, &fs_int_desc);
955 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
956
957 result = usb_ep_enable(ep, d);
958 if (result)
959 goto err;
960
Lukasz Majewski61e54932013-10-08 14:30:42 +0200961 ep->driver_data = cdev; /* claim */
962
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900963 return 0;
964
965 err_disable_out_ep:
966 usb_ep_disable(dev->out_ep);
967
968 err_free_in_req:
969 free_ep_req(dev->in_ep, dev->in_req);
970 dev->in_req = NULL;
971
972 err_disable_in_ep:
973 usb_ep_disable(dev->in_ep);
974
975 err:
Lukasz Majewski61e54932013-10-08 14:30:42 +0200976 return result;
977}
978
979static int thor_func_set_alt(struct usb_function *f,
980 unsigned intf, unsigned alt)
981{
982 struct thor_dev *dev = thor_func->dev;
983 int result;
984
985 debug("%s: func: %s intf: %d alt: %d\n",
986 __func__, f->name, intf, alt);
987
988 switch (intf) {
989 case 0:
990 debug("ACM INTR interface\n");
991 break;
992 case 1:
993 debug("Communication Data interface\n");
994 result = thor_eps_setup(f);
995 if (result)
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900996 pr_err("%s: EPs setup failed!\n", __func__);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200997 dev->configuration_done = 1;
998 break;
999 }
1000
1001 return 0;
1002}
1003
1004static int thor_func_init(struct usb_configuration *c)
1005{
1006 struct f_thor *f_thor;
1007 int status;
1008
1009 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
1010
1011 f_thor = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_thor));
1012 if (!f_thor)
1013 return -ENOMEM;
1014
1015 memset(f_thor, 0, sizeof(*f_thor));
1016
1017 f_thor->usb_function.name = "f_thor";
1018 f_thor->usb_function.bind = thor_func_bind;
1019 f_thor->usb_function.unbind = thor_unbind;
1020 f_thor->usb_function.setup = thor_func_setup;
1021 f_thor->usb_function.set_alt = thor_func_set_alt;
1022 f_thor->usb_function.disable = thor_func_disable;
1023
1024 status = usb_add_function(c, &f_thor->usb_function);
1025 if (status)
1026 free(f_thor);
1027
1028 return status;
1029}
1030
1031int thor_add(struct usb_configuration *c)
1032{
1033 debug("%s:\n", __func__);
1034 return thor_func_init(c);
1035}
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +02001036
1037DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor, thor_add);