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