blob: 3caa4c36387c050fb26a92034bcbbd296dca93d1 [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>
Lukasz Majewski61e54932013-10-08 14:30:42 +020028#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 Szyprowskiee9b8552020-12-22 11:32:24 +010034#include <thor.h>
Lukasz Majewski61e54932013-10-08 14:30:42 +020035
36#include "f_thor.h"
37
Marek Vasut8bfcb342023-09-01 11:49:59 +020038static void thor_tx_data(struct udevice *udc, unsigned char *data, int len);
Lukasz Majewski61e54932013-10-08 14:30:42 +020039static void thor_set_dma(void *addr, int len);
Marek Vasut8bfcb342023-09-01 11:49:59 +020040static int thor_rx_data(struct udevice *udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +020041
42static struct f_thor *thor_func;
43static inline struct f_thor *func_to_thor(struct usb_function *f)
44{
45 return container_of(f, struct f_thor, usb_function);
46}
47
48DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_tx_data_buf,
49 sizeof(struct rsp_box));
50DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_rx_data_buf,
51 sizeof(struct rqt_box));
52
53/* ********************************************************** */
54/* THOR protocol - transmission handling */
55/* ********************************************************** */
Seung-Woo Kim16b74aa2018-05-10 10:52:14 +090056DEFINE_CACHE_ALIGN_BUFFER(char, f_name, F_NAME_BUF_SIZE + 1);
Lukasz Majewski61e54932013-10-08 14:30:42 +020057static unsigned long long int thor_file_size;
58static int alt_setting_num;
59
Marek Vasut8bfcb342023-09-01 11:49:59 +020060static void send_rsp(struct udevice *udc, const struct rsp_box *rsp)
Lukasz Majewski61e54932013-10-08 14:30:42 +020061{
62 memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box));
Marek Vasut8bfcb342023-09-01 11:49:59 +020063 thor_tx_data(udc, thor_tx_data_buf, sizeof(struct rsp_box));
Lukasz Majewski61e54932013-10-08 14:30:42 +020064
65 debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data);
66}
67
Marek Vasut8bfcb342023-09-01 11:49:59 +020068static void send_data_rsp(struct udevice *udc, s32 ack, s32 count)
Lukasz Majewski61e54932013-10-08 14:30:42 +020069{
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 Vasut8bfcb342023-09-01 11:49:59 +020077 thor_tx_data(udc, thor_tx_data_buf, sizeof(struct data_rsp_box));
Lukasz Majewski61e54932013-10-08 14:30:42 +020078
79 debug("-DATA RSP: %d, %d\n", ack, count);
80}
81
Marek Vasut8bfcb342023-09-01 11:49:59 +020082static int process_rqt_info(struct udevice *udc, const struct rqt_box *rqt)
Lukasz Majewski61e54932013-10-08 14:30:42 +020083{
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 Vasut8bfcb342023-09-01 11:49:59 +0200115 send_rsp(udc, rsp);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200116 return true;
117}
118
Marek Vasut8bfcb342023-09-01 11:49:59 +0200119static int process_rqt_cmd(struct udevice *udc, const struct rqt_box *rqt)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200120{
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 Vasut8bfcb342023-09-01 11:49:59 +0200130 send_rsp(udc, rsp);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200131 g_dnl_unregister();
132 dfu_free_entities();
Siva Durga Prasad Paladugu4bad1062015-04-16 10:38:34 +0200133#ifdef CONFIG_THOR_RESET_OFF
134 return RESET_DONE;
135#endif
Lukasz Majewski61e54932013-10-08 14:30:42 +0200136 run_command("reset", 0);
137 break;
138 case RQT_CMD_POWEROFF:
139 case RQT_CMD_EFSCLEAR:
Marek Vasut8bfcb342023-09-01 11:49:59 +0200140 send_rsp(udc, rsp);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200141 default:
142 printf("Command not supported -> cmd: %d\n", rqt->rqt_data);
143 return -EINVAL;
144 }
145
146 return true;
147}
148
Marek Vasut8bfcb342023-09-01 11:49:59 +0200149static long long int download_head(struct udevice *udc,
150 unsigned long long total,
Lukasz Majewski61e54932013-10-08 14:30:42 +0200151 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 Warrenf9500982014-06-11 16:03:34 -0600156 struct dfu_entity *dfu_entity = dfu_get_entity(alt_setting_num);
157 void *transfer_buffer = dfu_get_buf(dfu_entity);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200158 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 Vasut8bfcb342023-09-01 11:49:59 +0200171 ret_rcv = thor_rx_data(udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200172 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 Kimb87b94e2020-02-13 17:24:11 +0900183 pr_err("DFU write failed [%d] cnt: %d\n",
Lukasz Majewski61e54932013-10-08 14:30:42 +0200184 ret, *cnt);
185 return ret;
186 }
187 buf = transfer_buffer;
188 }
Marek Vasut8bfcb342023-09-01 11:49:59 +0200189 send_data_rsp(udc, 0, ++usb_pkt_cnt);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200190 }
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 Vasut8bfcb342023-09-01 11:49:59 +0200205 ret_rcv = thor_rx_data(udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200206 if (ret_rcv < 0)
207 return ret_rcv;
208 rcv_cnt += ret_rcv;
Marek Vasut8bfcb342023-09-01 11:49:59 +0200209 send_data_rsp(udc, 0, ++usb_pkt_cnt);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200210 }
211
212 debug("%s: %llu total: %llu cnt: %d\n", __func__, rcv_cnt, total, *cnt);
213
214 return rcv_cnt;
215}
216
217static int download_tail(long long int left, int cnt)
218{
Przemyslaw Marczaka5071a02014-12-15 10:34:10 +0100219 struct dfu_entity *dfu_entity;
220 void *transfer_buffer;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200221 int ret;
222
223 debug("%s: left: %llu cnt: %d\n", __func__, left, cnt);
224
Przemyslaw Marczaka5071a02014-12-15 10:34:10 +0100225 dfu_entity = dfu_get_entity(alt_setting_num);
226 if (!dfu_entity) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900227 pr_err("Alt setting: %d entity not found!\n", alt_setting_num);
Przemyslaw Marczaka5071a02014-12-15 10:34:10 +0100228 return -ENOENT;
229 }
230
231 transfer_buffer = dfu_get_buf(dfu_entity);
232 if (!transfer_buffer) {
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900233 pr_err("Transfer buffer not allocated!\n");
Przemyslaw Marczaka5071a02014-12-15 10:34:10 +0100234 return -ENXIO;
235 }
236
Lukasz Majewski61e54932013-10-08 14:30:42 +0200237 if (left) {
Przemyslaw Marczak13a41c82014-04-18 09:48:24 +0200238 ret = dfu_write(dfu_entity, transfer_buffer, left, cnt++);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200239 if (ret) {
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900240 pr_err("DFU write failed[%d]: left: %llu\n", ret, left);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200241 return ret;
242 }
243 }
244
245 /*
Przemyslaw Marczak065a27f2014-05-12 12:05:34 +0200246 * To store last "packet" or write file from buffer to filesystem
247 * DFU storage backend requires dfu_flush
Lukasz Majewski61e54932013-10-08 14:30:42 +0200248 *
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 Marczakef9d4d42014-04-18 09:48:25 +0200252 ret = dfu_flush(dfu_entity, transfer_buffer, 0, cnt);
Przemyslaw Marczak065a27f2014-05-12 12:05:34 +0200253 if (ret)
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900254 pr_err("DFU flush failed!\n");
Przemyslaw Marczakef9d4d42014-04-18 09:48:25 +0200255
Lukasz Majewski61e54932013-10-08 14:30:42 +0200256 return ret;
257}
258
Marek Vasut8bfcb342023-09-01 11:49:59 +0200259static long long int process_rqt_download(struct udevice *udc, const struct rqt_box *rqt)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200260{
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 Kim2753cec2020-10-16 16:29:02 +0900272 thor_file_size = (uint64_t)(uint32_t)rqt->int_data[0] +
273 (((uint64_t)(uint32_t)rqt->int_data[1])
Seung-Woo Kim7414a0f2018-05-10 10:52:15 +0900274 << 32);
275 debug("INIT: total %llu bytes\n", thor_file_size);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200276 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 Kim2753cec2020-10-16 16:29:02 +0900286 thor_file_size = (uint64_t)(uint32_t)rqt->int_data[1] +
287 (((uint64_t)(uint32_t)rqt->int_data[2])
Seung-Woo Kim7414a0f2018-05-10 10:52:15 +0900288 << 32);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200289 memcpy(f_name, rqt->str_data[0], F_NAME_BUF_SIZE);
Seung-Woo Kim16b74aa2018-05-10 10:52:14 +0900290 f_name[F_NAME_BUF_SIZE] = '\0';
Lukasz Majewski61e54932013-10-08 14:30:42 +0200291
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 Kimb87b94e2020-02-13 17:24:11 +0900299 pr_err("Alt setting [%d] to write not found!\n",
Lukasz Majewski61e54932013-10-08 14:30:42 +0200300 alt_setting_num);
301 rsp->ack = -ENODEV;
302 ret = rsp->ack;
303 }
304 break;
305 case RQT_DL_FILE_START:
Marek Vasut8bfcb342023-09-01 11:49:59 +0200306 send_rsp(udc, rsp);
307 ret_head = download_head(udc, thor_file_size, THOR_PACKET_SIZE,
Lukasz Majewski61e54932013-10-08 14:30:42 +0200308 &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 Kimb87b94e2020-02-13 17:24:11 +0900325 pr_err("Operation not supported: %d\n", rqt->rqt_data);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200326 ret = -ENOTSUPP;
327 }
328
Marek Vasut8bfcb342023-09-01 11:49:59 +0200329 send_rsp(udc, rsp);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200330 return ret;
331}
332
Marek Vasut8bfcb342023-09-01 11:49:59 +0200333static int process_data(struct udevice *udc)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200334{
335 ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box));
336 int ret = -EINVAL;
337
Lukasz Majewski61e54932013-10-08 14:30:42 +0200338 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 Vasut8bfcb342023-09-01 11:49:59 +0200344 ret = process_rqt_info(udc, rqt);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200345 break;
346 case RQT_CMD:
Marek Vasut8bfcb342023-09-01 11:49:59 +0200347 ret = process_rqt_cmd(udc, rqt);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200348 break;
349 case RQT_DL:
Marek Vasut8bfcb342023-09-01 11:49:59 +0200350 ret = (int) process_rqt_download(udc, rqt);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200351 break;
352 case RQT_UL:
353 puts("RQT: UPLOAD not supported!\n");
354 break;
355 default:
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900356 pr_err("unknown request (%d)\n", rqt->rqt);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200357 }
358
359 return ret;
360}
361
362/* ********************************************************** */
363/* THOR USB Function */
364/* ********************************************************** */
365
366static inline struct usb_endpoint_descriptor *
367ep_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
375static 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
383static 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
391static 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 */
400static 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
412static 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
419static 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
427static 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
434static 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
440static 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
451static struct usb_interface_assoc_descriptor
452thor_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
463static 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
471static 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
479static 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 Majewski61e54932013-10-08 14:30:42 +0200489/*
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 */
496static 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
505static 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/*-------------------------------------------------------------------------*/
523static 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 Vasut8bfcb342023-09-01 11:49:59 +0200541static int thor_rx_data(struct udevice *udc)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200542{
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 Kimb87b94e2020-02-13 17:24:11 +0900555 pr_err("kill %s: resubmit %d bytes --> %d\n",
Lukasz Majewski61e54932013-10-08 14:30:42 +0200556 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 Vasut8bfcb342023-09-01 11:49:59 +0200562 dm_usb_gadget_handle_interrupts(udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200563 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 Vasut8bfcb342023-09-01 11:49:59 +0200573static void thor_tx_data(struct udevice *udc, unsigned char *data, int len)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200574{
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 Simek6c7d0032015-10-30 16:19:43 +0100584 debug("%s: dev->in_req->length:%d to_cpy:%zd\n", __func__,
Lukasz Majewski61e54932013-10-08 14:30:42 +0200585 dev->in_req->length, sizeof(data));
586
587 status = usb_ep_queue(dev->in_ep, dev->in_req, 0);
588 if (status) {
Seung-Woo Kimb87b94e2020-02-13 17:24:11 +0900589 pr_err("kill %s: resubmit %d bytes --> %d\n",
Lukasz Majewski61e54932013-10-08 14:30:42 +0200590 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 Vasut8bfcb342023-09-01 11:49:59 +0200596 dm_usb_gadget_handle_interrupts(udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200597
598 dev->txdata = 0;
599}
600
601static 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 Kimb87b94e2020-02-13 17:24:11 +0900622 pr_err("ERROR:%d\n", status);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200623 break;
624 }
625
626 debug("%s complete --> %d, %d/%d\n", ep->name,
627 status, req->actual, req->length);
628}
629
Lukasz Majewski61e54932013-10-08 14:30:42 +0200630static 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
637static int
638thor_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 Kimb87b94e2020-02-13 17:24:11 +0900662 pr_err("thor_setup: unknown request: %d\n", ctrl->bRequest);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200663 }
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 */
679static 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 Vasut8bfcb342023-09-01 11:49:59 +0200690int thor_init(struct udevice *udc)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200691{
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 Vasut8bfcb342023-09-01 11:49:59 +0200697 dm_usb_gadget_handle_interrupts(udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200698
699 thor_set_dma(thor_rx_data_buf, strlen("THOR"));
700 /* detect the download request from Host PC */
Marek Vasut8bfcb342023-09-01 11:49:59 +0200701 if (thor_rx_data(udc) < 0) {
Lukasz Majewski61e54932013-10-08 14:30:42 +0200702 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 Vasut8bfcb342023-09-01 11:49:59 +0200711 thor_tx_data(udc, thor_tx_data_buf, strlen("ROHT"));
Lukasz Majewski61e54932013-10-08 14:30:42 +0200712 } else {
713 puts("Wrong reply information\n");
714 return -1;
715 }
716
717 return 0;
718}
719
Marek Vasut8bfcb342023-09-01 11:49:59 +0200720int thor_handle(struct udevice *udc)
Lukasz Majewski61e54932013-10-08 14:30:42 +0200721{
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 Vasut8bfcb342023-09-01 11:49:59 +0200727 ret = thor_rx_data(udc);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200728
729 if (ret > 0) {
Marek Vasut8bfcb342023-09-01 11:49:59 +0200730 ret = process_data(udc);
Siva Durga Prasad Paladugu4bad1062015-04-16 10:38:34 +0200731#ifdef CONFIG_THOR_RESET_OFF
732 if (ret == RESET_DONE)
733 break;
734#endif
Lukasz Majewski61e54932013-10-08 14:30:42 +0200735 if (ret < 0)
736 return ret;
737 } else {
738 printf("%s: No data received!\n", __func__);
739 break;
740 }
Marek Szyprowskiee9b8552020-12-22 11:32:24 +0100741 if (dfu_reinit_needed)
742 return THOR_DFU_REINIT_NEEDED;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200743 }
744
745 return 0;
746}
747
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900748static 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 Majewski61e54932013-10-08 14:30:42 +0200755static 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 Paladugu13b24e02015-04-15 13:42:19 +0200784 THOR_PACKET_SIZE);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200785 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 Szyprowskida3f27f2015-03-03 17:32:16 +0100822 ep->driver_data = dev;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200823
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 Szyprowskida3f27f2015-03-03 17:32:16 +0100835 ep->driver_data = dev;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200836
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 Szyprowskida3f27f2015-03-03 17:32:16 +0100844 ep->driver_data = dev;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200845
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 Kime78aac12018-05-25 14:21:01 +0900863 if (dev->req)
864 free_ep_req(gadget->ep0, dev->req);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200865 free(dev);
866 return status;
867}
868
Lukasz Majewski61e54932013-10-08 14:30:42 +0200869static 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 Kime78aac12018-05-25 14:21:01 +0900874 free_ep_req(dev->gadget->ep0, dev->req);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200875 free(dev);
876 memset(thor_func, 0, sizeof(*thor_func));
877 thor_func = NULL;
878}
879
880static 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 Majewski61e54932013-10-08 14:30:42 +0200889 usb_ep_disable(dev->in_ep);
Seung-Woo Kim1661bf22018-06-04 15:53:39 +0900890 free_ep_req(dev->in_ep, dev->in_req);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200891 dev->in_ep->driver_data = NULL;
892 }
893
894 if (dev->out_ep->driver_data) {
Lukasz Majewski61e54932013-10-08 14:30:42 +0200895 usb_ep_disable(dev->out_ep);
Seung-Woo Kim1661bf22018-06-04 15:53:39 +0900896 usb_ep_free_request(dev->out_ep, dev->out_req);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200897 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
906static 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 Kime78aac12018-05-25 14:21:01 +0900922 goto err;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200923
924 ep->driver_data = cdev; /* claim */
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900925 req = alloc_ep_req(ep, THOR_PACKET_SIZE);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200926 if (!req) {
Lukasz Majewski61e54932013-10-08 14:30:42 +0200927 result = -EIO;
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900928 goto err_disable_in_ep;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200929 }
930
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900931 memset(req->buf, 0, req->length);
932 req->complete = thor_rx_tx_complete;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200933 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 Kime78aac12018-05-25 14:21:01 +0900940 goto err_free_in_req;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200941
942 ep->driver_data = cdev; /* claim */
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900943 req = usb_ep_alloc_request(ep, 0);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200944 if (!req) {
Lukasz Majewski61e54932013-10-08 14:30:42 +0200945 result = -EIO;
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900946 goto err_disable_out_ep;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200947 }
948
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900949 req->complete = thor_rx_tx_complete;
Lukasz Majewski61e54932013-10-08 14:30:42 +0200950 dev->out_req = req;
951 /* ACM control EP */
952 ep = dev->int_ep;
Marek Szyprowskid2c76072019-10-02 12:27:18 +0200953 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 Majewski61e54932013-10-08 14:30:42 +0200960 ep->driver_data = cdev; /* claim */
961
Seung-Woo Kime78aac12018-05-25 14:21:01 +0900962 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 Majewski61e54932013-10-08 14:30:42 +0200975 return result;
976}
977
978static 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 Kimb87b94e2020-02-13 17:24:11 +0900995 pr_err("%s: EPs setup failed!\n", __func__);
Lukasz Majewski61e54932013-10-08 14:30:42 +0200996 dev->configuration_done = 1;
997 break;
998 }
999
1000 return 0;
1001}
1002
1003static 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
1030int thor_add(struct usb_configuration *c)
1031{
1032 debug("%s:\n", __func__);
1033 return thor_func_init(c);
1034}
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +02001035
1036DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor, thor_add);