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