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