9ed0ce3d31328674f982e8b32a96643b8e77c361
[oweals/u-boot.git] / drivers / usb / gadget / f_thor.c
1 /*
2  * f_thor.c -- USB TIZEN THOR Downloader gadget function
3  *
4  * Copyright (C) 2013 Samsung Electronics
5  * Lukasz Majewski <l.majewski@samsung.com>
6  *
7  * Based on code from:
8  * git://review.tizen.org/kernel/u-boot
9  *
10  * Developed by:
11  * Copyright (C) 2009 Samsung Electronics
12  * Minkyu Kang <mk7.kang@samsung.com>
13  * Sanghee Kim <sh0130.kim@samsung.com>
14  *
15  * SPDX-License-Identifier:     GPL-2.0+
16  */
17
18 #include <errno.h>
19 #include <common.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;
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);
51 static unsigned long long int thor_file_size;
52 static int alt_setting_num;
53
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
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
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
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
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                                 error("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
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                 error("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                 error("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                         error("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                 error("DFU flush failed!");
248
249         return ret;
250 }
251
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 = rqt->int_data[0];
266                 debug("INIT: total %d bytes\n", rqt->int_data[0]);
267                 break;
268         case RQT_DL_FILE_INFO:
269                 file_type = rqt->int_data[0];
270                 if (file_type == FILE_TYPE_PIT) {
271                         puts("PIT table file - not supported\n");
272                         rsp->ack = -ENOTSUPP;
273                         ret = rsp->ack;
274                         break;
275                 }
276
277                 thor_file_size = rqt->int_data[1];
278                 memcpy(f_name, rqt->str_data[0], F_NAME_BUF_SIZE);
279
280                 debug("INFO: name(%s, %d), size(%llu), type(%d)\n",
281                       f_name, 0, thor_file_size, file_type);
282
283                 rsp->int_data[0] = THOR_PACKET_SIZE;
284
285                 alt_setting_num = dfu_get_alt(f_name);
286                 if (alt_setting_num < 0) {
287                         error("Alt setting [%d] to write not found!",
288                               alt_setting_num);
289                         rsp->ack = -ENODEV;
290                         ret = rsp->ack;
291                 }
292                 break;
293         case RQT_DL_FILE_START:
294                 send_rsp(rsp);
295                 ret_head = download_head(thor_file_size, THOR_PACKET_SIZE,
296                                          &left, &cnt);
297                 if (ret_head < 0) {
298                         left = 0;
299                         cnt = 0;
300                 }
301                 return ret_head;
302         case RQT_DL_FILE_END:
303                 debug("DL FILE_END\n");
304                 rsp->ack = download_tail(left, cnt);
305                 ret = rsp->ack;
306                 left = 0;
307                 cnt = 0;
308                 break;
309         case RQT_DL_EXIT:
310                 debug("DL EXIT\n");
311                 break;
312         default:
313                 error("Operation not supported: %d", rqt->rqt_data);
314                 ret = -ENOTSUPP;
315         }
316
317         send_rsp(rsp);
318         return ret;
319 }
320
321 static int process_data(void)
322 {
323         ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box));
324         int ret = -EINVAL;
325
326         memcpy(rqt, thor_rx_data_buf, sizeof(struct rqt_box));
327
328         debug("+RQT: %d, %d\n", rqt->rqt, rqt->rqt_data);
329
330         switch (rqt->rqt) {
331         case RQT_INFO:
332                 ret = process_rqt_info(rqt);
333                 break;
334         case RQT_CMD:
335                 ret = process_rqt_cmd(rqt);
336                 break;
337         case RQT_DL:
338                 ret = (int) process_rqt_download(rqt);
339                 break;
340         case RQT_UL:
341                 puts("RQT: UPLOAD not supported!\n");
342                 break;
343         default:
344                 error("unknown request (%d)", rqt->rqt);
345         }
346
347         return ret;
348 }
349
350 /* ********************************************************** */
351 /*         THOR USB Function                                  */
352 /* ********************************************************** */
353
354 static inline struct usb_endpoint_descriptor *
355 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
356         struct usb_endpoint_descriptor *fs)
357 {
358         if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
359                 return hs;
360         return fs;
361 }
362
363 static struct usb_interface_descriptor thor_downloader_intf_data = {
364         .bLength =              sizeof(thor_downloader_intf_data),
365         .bDescriptorType =      USB_DT_INTERFACE,
366
367         .bNumEndpoints =        2,
368         .bInterfaceClass =      USB_CLASS_CDC_DATA,
369 };
370
371 static struct usb_endpoint_descriptor fs_in_desc = {
372         .bLength =              USB_DT_ENDPOINT_SIZE,
373         .bDescriptorType =      USB_DT_ENDPOINT,
374
375         .bEndpointAddress =     USB_DIR_IN,
376         .bmAttributes = USB_ENDPOINT_XFER_BULK,
377 };
378
379 static struct usb_endpoint_descriptor fs_out_desc = {
380         .bLength =              USB_DT_ENDPOINT_SIZE,
381         .bDescriptorType =      USB_DT_ENDPOINT,
382
383         .bEndpointAddress =     USB_DIR_OUT,
384         .bmAttributes = USB_ENDPOINT_XFER_BULK,
385 };
386
387 /* CDC configuration */
388 static struct usb_interface_descriptor thor_downloader_intf_int = {
389         .bLength =              sizeof(thor_downloader_intf_int),
390         .bDescriptorType =      USB_DT_INTERFACE,
391
392         .bNumEndpoints =        1,
393         .bInterfaceClass =      USB_CLASS_COMM,
394          /* 0x02 Abstract Line Control Model */
395         .bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM,
396         /* 0x01 Common AT commands */
397         .bInterfaceProtocol =   USB_CDC_ACM_PROTO_AT_V25TER,
398 };
399
400 static struct usb_cdc_header_desc thor_downloader_cdc_header = {
401         .bLength         =    sizeof(thor_downloader_cdc_header),
402         .bDescriptorType =    0x24, /* CS_INTERFACE */
403         .bDescriptorSubType = 0x00,
404         .bcdCDC =             0x0110,
405 };
406
407 static struct usb_cdc_call_mgmt_descriptor thor_downloader_cdc_call = {
408         .bLength         =    sizeof(thor_downloader_cdc_call),
409         .bDescriptorType =    0x24, /* CS_INTERFACE */
410         .bDescriptorSubType = 0x01,
411         .bmCapabilities =     0x00,
412         .bDataInterface =     0x01,
413 };
414
415 static struct usb_cdc_acm_descriptor thor_downloader_cdc_abstract = {
416         .bLength         =    sizeof(thor_downloader_cdc_abstract),
417         .bDescriptorType =    0x24, /* CS_INTERFACE */
418         .bDescriptorSubType = 0x02,
419         .bmCapabilities =     0x00,
420 };
421
422 static struct usb_cdc_union_desc thor_downloader_cdc_union = {
423         .bLength         =     sizeof(thor_downloader_cdc_union),
424         .bDescriptorType =     0x24, /* CS_INTERFACE */
425         .bDescriptorSubType =  USB_CDC_UNION_TYPE,
426 };
427
428 static struct usb_endpoint_descriptor fs_int_desc = {
429         .bLength = USB_DT_ENDPOINT_SIZE,
430         .bDescriptorType = USB_DT_ENDPOINT,
431
432         .bEndpointAddress = 3 | USB_DIR_IN,
433         .bmAttributes = USB_ENDPOINT_XFER_INT,
434         .wMaxPacketSize = __constant_cpu_to_le16(16),
435
436         .bInterval = 0x9,
437 };
438
439 static struct usb_interface_assoc_descriptor
440 thor_iad_descriptor = {
441         .bLength =              sizeof(thor_iad_descriptor),
442         .bDescriptorType =      USB_DT_INTERFACE_ASSOCIATION,
443
444         .bFirstInterface =      0,
445         .bInterfaceCount =      2,      /* control + data */
446         .bFunctionClass =       USB_CLASS_COMM,
447         .bFunctionSubClass =    USB_CDC_SUBCLASS_ACM,
448         .bFunctionProtocol =    USB_CDC_PROTO_NONE,
449 };
450
451 static struct usb_endpoint_descriptor hs_in_desc = {
452         .bLength =              USB_DT_ENDPOINT_SIZE,
453         .bDescriptorType =      USB_DT_ENDPOINT,
454
455         .bmAttributes = USB_ENDPOINT_XFER_BULK,
456         .wMaxPacketSize =       __constant_cpu_to_le16(512),
457 };
458
459 static struct usb_endpoint_descriptor hs_out_desc = {
460         .bLength =              USB_DT_ENDPOINT_SIZE,
461         .bDescriptorType =      USB_DT_ENDPOINT,
462
463         .bmAttributes = USB_ENDPOINT_XFER_BULK,
464         .wMaxPacketSize =       __constant_cpu_to_le16(512),
465 };
466
467 static struct usb_endpoint_descriptor hs_int_desc = {
468         .bLength = USB_DT_ENDPOINT_SIZE,
469         .bDescriptorType = USB_DT_ENDPOINT,
470
471         .bmAttributes = USB_ENDPOINT_XFER_INT,
472         .wMaxPacketSize = __constant_cpu_to_le16(16),
473
474         .bInterval = 0x9,
475 };
476
477 /*
478  * This attribute vendor descriptor is necessary for correct operation with
479  * Windows version of THOR download program
480  *
481  * It prevents windows driver from sending zero lenght packet (ZLP) after
482  * each THOR_PACKET_SIZE. This assures consistent behaviour with libusb
483  */
484 static struct usb_cdc_attribute_vendor_descriptor thor_downloader_cdc_av = {
485         .bLength =              sizeof(thor_downloader_cdc_av),
486         .bDescriptorType =      0x24,
487         .bDescriptorSubType =   0x80,
488         .DAUType =              0x0002,
489         .DAULength =            0x0001,
490         .DAUValue =             0x00,
491 };
492
493 static const struct usb_descriptor_header *hs_thor_downloader_function[] = {
494         (struct usb_descriptor_header *)&thor_iad_descriptor,
495
496         (struct usb_descriptor_header *)&thor_downloader_intf_int,
497         (struct usb_descriptor_header *)&thor_downloader_cdc_header,
498         (struct usb_descriptor_header *)&thor_downloader_cdc_call,
499         (struct usb_descriptor_header *)&thor_downloader_cdc_abstract,
500         (struct usb_descriptor_header *)&thor_downloader_cdc_union,
501         (struct usb_descriptor_header *)&hs_int_desc,
502
503         (struct usb_descriptor_header *)&thor_downloader_intf_data,
504         (struct usb_descriptor_header *)&thor_downloader_cdc_av,
505         (struct usb_descriptor_header *)&hs_in_desc,
506         (struct usb_descriptor_header *)&hs_out_desc,
507         NULL,
508 };
509
510 /*-------------------------------------------------------------------------*/
511 static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
512 {
513         struct usb_request *req;
514
515         req = usb_ep_alloc_request(ep, 0);
516         if (!req)
517                 return req;
518
519         req->length = length;
520         req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
521         if (!req->buf) {
522                 usb_ep_free_request(ep, req);
523                 req = NULL;
524         }
525
526         return req;
527 }
528
529 static int thor_rx_data(void)
530 {
531         struct thor_dev *dev = thor_func->dev;
532         int data_to_rx, tmp, status;
533
534         data_to_rx = dev->out_req->length;
535         tmp = data_to_rx;
536         do {
537                 dev->out_req->length = data_to_rx;
538                 debug("dev->out_req->length:%d dev->rxdata:%d\n",
539                       dev->out_req->length, dev->rxdata);
540
541                 status = usb_ep_queue(dev->out_ep, dev->out_req, 0);
542                 if (status) {
543                         error("kill %s:  resubmit %d bytes --> %d",
544                               dev->out_ep->name, dev->out_req->length, status);
545                         usb_ep_set_halt(dev->out_ep);
546                         return -EAGAIN;
547                 }
548
549                 while (!dev->rxdata) {
550                         usb_gadget_handle_interrupts(0);
551                         if (ctrlc())
552                                 return -1;
553                 }
554                 dev->rxdata = 0;
555                 data_to_rx -= dev->out_req->actual;
556         } while (data_to_rx);
557
558         return tmp;
559 }
560
561 static void thor_tx_data(unsigned char *data, int len)
562 {
563         struct thor_dev *dev = thor_func->dev;
564         unsigned char *ptr = dev->in_req->buf;
565         int status;
566
567         memset(ptr, 0, len);
568         memcpy(ptr, data, len);
569
570         dev->in_req->length = len;
571
572         debug("%s: dev->in_req->length:%d to_cpy:%zd\n", __func__,
573               dev->in_req->length, sizeof(data));
574
575         status = usb_ep_queue(dev->in_ep, dev->in_req, 0);
576         if (status) {
577                 error("kill %s:  resubmit %d bytes --> %d",
578                       dev->in_ep->name, dev->in_req->length, status);
579                 usb_ep_set_halt(dev->in_ep);
580         }
581
582         /* Wait until tx interrupt received */
583         while (!dev->txdata)
584                 usb_gadget_handle_interrupts(0);
585
586         dev->txdata = 0;
587 }
588
589 static void thor_rx_tx_complete(struct usb_ep *ep, struct usb_request *req)
590 {
591         struct thor_dev *dev = thor_func->dev;
592         int status = req->status;
593
594         debug("%s: ep_ptr:%p, req_ptr:%p\n", __func__, ep, req);
595         switch (status) {
596         case 0:
597                 if (ep == dev->out_ep)
598                         dev->rxdata = 1;
599                 else
600                         dev->txdata = 1;
601
602                 break;
603
604         /* this endpoint is normally active while we're configured */
605         case -ECONNABORTED:             /* hardware forced ep reset */
606         case -ECONNRESET:               /* request dequeued */
607         case -ESHUTDOWN:                /* disconnect from host */
608         case -EREMOTEIO:                /* short read */
609         case -EOVERFLOW:
610                 error("ERROR:%d", status);
611                 break;
612         }
613
614         debug("%s complete --> %d, %d/%d\n", ep->name,
615               status, req->actual, req->length);
616 }
617
618 static struct usb_request *thor_start_ep(struct usb_ep *ep)
619 {
620         struct usb_request *req;
621
622         req = alloc_ep_req(ep, THOR_PACKET_SIZE);
623         debug("%s: ep:%p req:%p\n", __func__, ep, req);
624
625         if (!req)
626                 return NULL;
627
628         memset(req->buf, 0, req->length);
629         req->complete = thor_rx_tx_complete;
630
631         return req;
632 }
633
634 static void thor_setup_complete(struct usb_ep *ep, struct usb_request *req)
635 {
636         if (req->status || req->actual != req->length)
637                 debug("setup complete --> %d, %d/%d\n",
638                       req->status, req->actual, req->length);
639 }
640
641 static int
642 thor_func_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
643 {
644         struct thor_dev *dev = thor_func->dev;
645         struct usb_request *req = dev->req;
646         struct usb_gadget *gadget = dev->gadget;
647         int value = 0;
648
649         u16 len = le16_to_cpu(ctrl->wLength);
650
651         debug("Req_Type: 0x%x Req: 0x%x wValue: 0x%x wIndex: 0x%x wLen: 0x%x\n",
652               ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, ctrl->wIndex,
653               ctrl->wLength);
654
655         switch (ctrl->bRequest) {
656         case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
657                 value = 0;
658                 break;
659         case USB_CDC_REQ_SET_LINE_CODING:
660                 value = len;
661                 /* Line Coding set done = configuration done */
662                 thor_func->dev->configuration_done = 1;
663                 break;
664
665         default:
666                 error("thor_setup: unknown request: %d", ctrl->bRequest);
667         }
668
669         if (value >= 0) {
670                 req->length = value;
671                 req->zero = value < len;
672                 value = usb_ep_queue(gadget->ep0, req, 0);
673                 if (value < 0) {
674                         debug("%s: ep_queue: %d\n", __func__, value);
675                         req->status = 0;
676                 }
677         }
678
679         return value;
680 }
681
682 /* Specific to the THOR protocol */
683 static void thor_set_dma(void *addr, int len)
684 {
685         struct thor_dev *dev = thor_func->dev;
686
687         debug("in_req:%p, out_req:%p\n", dev->in_req, dev->out_req);
688         debug("addr:%p, len:%d\n", addr, len);
689
690         dev->out_req->buf = addr;
691         dev->out_req->length = len;
692 }
693
694 int thor_init(void)
695 {
696         struct thor_dev *dev = thor_func->dev;
697
698         /* Wait for a device enumeration and configuration settings */
699         debug("THOR enumeration/configuration setting....\n");
700         while (!dev->configuration_done)
701                 usb_gadget_handle_interrupts(0);
702
703         thor_set_dma(thor_rx_data_buf, strlen("THOR"));
704         /* detect the download request from Host PC */
705         if (thor_rx_data() < 0) {
706                 printf("%s: Data not received!\n", __func__);
707                 return -1;
708         }
709
710         if (!strncmp((char *)thor_rx_data_buf, "THOR", strlen("THOR"))) {
711                 puts("Download request from the Host PC\n");
712                 udelay(30 * 1000); /* 30 ms */
713
714                 strcpy((char *)thor_tx_data_buf, "ROHT");
715                 thor_tx_data(thor_tx_data_buf, strlen("ROHT"));
716         } else {
717                 puts("Wrong reply information\n");
718                 return -1;
719         }
720
721         return 0;
722 }
723
724 int thor_handle(void)
725 {
726         int ret;
727
728         /* receive the data from Host PC */
729         while (1) {
730                 thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box));
731                 ret = thor_rx_data();
732
733                 if (ret > 0) {
734                         ret = process_data();
735 #ifdef CONFIG_THOR_RESET_OFF
736                         if (ret == RESET_DONE)
737                                 break;
738 #endif
739                         if (ret < 0)
740                                 return ret;
741                 } else {
742                         printf("%s: No data received!\n", __func__);
743                         break;
744                 }
745         }
746
747         return 0;
748 }
749
750 static int thor_func_bind(struct usb_configuration *c, struct usb_function *f)
751 {
752         struct usb_gadget *gadget = c->cdev->gadget;
753         struct f_thor *f_thor = func_to_thor(f);
754         struct thor_dev *dev;
755         struct usb_ep *ep;
756         int status;
757
758         thor_func = f_thor;
759         dev = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*dev));
760         if (!dev)
761                 return -ENOMEM;
762
763         memset(dev, 0, sizeof(*dev));
764         dev->gadget = gadget;
765         f_thor->dev = dev;
766
767         debug("%s: usb_configuration: 0x%p usb_function: 0x%p\n",
768               __func__, c, f);
769         debug("f_thor: 0x%p thor: 0x%p\n", f_thor, dev);
770
771         /* EP0  */
772         /* preallocate control response and buffer */
773         dev->req = usb_ep_alloc_request(gadget->ep0, 0);
774         if (!dev->req) {
775                 status = -ENOMEM;
776                 goto fail;
777         }
778         dev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
779                                  THOR_PACKET_SIZE);
780         if (!dev->req->buf) {
781                 status = -ENOMEM;
782                 goto fail;
783         }
784
785         dev->req->complete = thor_setup_complete;
786
787         /* DYNAMIC interface numbers assignments */
788         status = usb_interface_id(c, f);
789
790         if (status < 0)
791                 goto fail;
792
793         thor_downloader_intf_int.bInterfaceNumber = status;
794         thor_downloader_cdc_union.bMasterInterface0 = status;
795
796         status = usb_interface_id(c, f);
797
798         if (status < 0)
799                 goto fail;
800
801         thor_downloader_intf_data.bInterfaceNumber = status;
802         thor_downloader_cdc_union.bSlaveInterface0 = status;
803
804         /* allocate instance-specific endpoints */
805         ep = usb_ep_autoconfig(gadget, &fs_in_desc);
806         if (!ep) {
807                 status = -ENODEV;
808                 goto fail;
809         }
810
811         if (gadget_is_dualspeed(gadget)) {
812                 hs_in_desc.bEndpointAddress =
813                                 fs_in_desc.bEndpointAddress;
814         }
815
816         dev->in_ep = ep; /* Store IN EP for enabling @ setup */
817         ep->driver_data = dev;
818
819         ep = usb_ep_autoconfig(gadget, &fs_out_desc);
820         if (!ep) {
821                 status = -ENODEV;
822                 goto fail;
823         }
824
825         if (gadget_is_dualspeed(gadget))
826                 hs_out_desc.bEndpointAddress =
827                                 fs_out_desc.bEndpointAddress;
828
829         dev->out_ep = ep; /* Store OUT EP for enabling @ setup */
830         ep->driver_data = dev;
831
832         ep = usb_ep_autoconfig(gadget, &fs_int_desc);
833         if (!ep) {
834                 status = -ENODEV;
835                 goto fail;
836         }
837
838         dev->int_ep = ep;
839         ep->driver_data = dev;
840
841         if (gadget_is_dualspeed(gadget)) {
842                 hs_int_desc.bEndpointAddress =
843                                 fs_int_desc.bEndpointAddress;
844
845                 f->hs_descriptors = (struct usb_descriptor_header **)
846                         &hs_thor_downloader_function;
847
848                 if (!f->hs_descriptors)
849                         goto fail;
850         }
851
852         debug("%s: out_ep:%p out_req:%p\n", __func__,
853               dev->out_ep, dev->out_req);
854
855         return 0;
856
857  fail:
858         free(dev);
859         return status;
860 }
861
862 static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
863 {
864         free(req->buf);
865         usb_ep_free_request(ep, req);
866 }
867
868 static void thor_unbind(struct usb_configuration *c, struct usb_function *f)
869 {
870         struct f_thor *f_thor = func_to_thor(f);
871         struct thor_dev *dev = f_thor->dev;
872
873         free(dev);
874         memset(thor_func, 0, sizeof(*thor_func));
875         thor_func = NULL;
876 }
877
878 static 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) {
887                 free_ep_req(dev->in_ep, dev->in_req);
888                 usb_ep_disable(dev->in_ep);
889                 dev->in_ep->driver_data = NULL;
890         }
891
892         if (dev->out_ep->driver_data) {
893                 dev->out_req->buf = NULL;
894                 usb_ep_free_request(dev->out_ep, dev->out_req);
895                 usb_ep_disable(dev->out_ep);
896                 dev->out_ep->driver_data = NULL;
897         }
898
899         if (dev->int_ep->driver_data) {
900                 usb_ep_disable(dev->int_ep);
901                 dev->int_ep->driver_data = NULL;
902         }
903 }
904
905 static int thor_eps_setup(struct usb_function *f)
906 {
907         struct usb_composite_dev *cdev = f->config->cdev;
908         struct usb_gadget *gadget = cdev->gadget;
909         struct thor_dev *dev = thor_func->dev;
910         struct usb_endpoint_descriptor *d;
911         struct usb_request *req;
912         struct usb_ep *ep;
913         int result;
914
915         ep = dev->in_ep;
916         d = ep_desc(gadget, &hs_in_desc, &fs_in_desc);
917         debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
918
919         result = usb_ep_enable(ep, d);
920         if (result)
921                 goto exit;
922
923         ep->driver_data = cdev; /* claim */
924         req = thor_start_ep(ep);
925         if (!req) {
926                 usb_ep_disable(ep);
927                 result = -EIO;
928                 goto exit;
929         }
930
931         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)
938                 goto exit;
939
940         ep->driver_data = cdev; /* claim */
941         req = thor_start_ep(ep);
942         if (!req) {
943                 usb_ep_disable(ep);
944                 result = -EIO;
945                 goto exit;
946         }
947
948         dev->out_req = req;
949         /* ACM control EP */
950         ep = dev->int_ep;
951         ep->driver_data = cdev; /* claim */
952
953  exit:
954         return result;
955 }
956
957 static int thor_func_set_alt(struct usb_function *f,
958                              unsigned intf, unsigned alt)
959 {
960         struct thor_dev *dev = thor_func->dev;
961         int result;
962
963         debug("%s: func: %s intf: %d alt: %d\n",
964               __func__, f->name, intf, alt);
965
966         switch (intf) {
967         case 0:
968                 debug("ACM INTR interface\n");
969                 break;
970         case 1:
971                 debug("Communication Data interface\n");
972                 result = thor_eps_setup(f);
973                 if (result)
974                         error("%s: EPs setup failed!", __func__);
975                 dev->configuration_done = 1;
976                 break;
977         }
978
979         return 0;
980 }
981
982 static int thor_func_init(struct usb_configuration *c)
983 {
984         struct f_thor *f_thor;
985         int status;
986
987         debug("%s: cdev: 0x%p\n", __func__, c->cdev);
988
989         f_thor = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_thor));
990         if (!f_thor)
991                 return -ENOMEM;
992
993         memset(f_thor, 0, sizeof(*f_thor));
994
995         f_thor->usb_function.name = "f_thor";
996         f_thor->usb_function.bind = thor_func_bind;
997         f_thor->usb_function.unbind = thor_unbind;
998         f_thor->usb_function.setup = thor_func_setup;
999         f_thor->usb_function.set_alt = thor_func_set_alt;
1000         f_thor->usb_function.disable = thor_func_disable;
1001
1002         status = usb_add_function(c, &f_thor->usb_function);
1003         if (status)
1004                 free(f_thor);
1005
1006         return status;
1007 }
1008
1009 int thor_add(struct usb_configuration *c)
1010 {
1011         debug("%s:\n", __func__);
1012         return thor_func_init(c);
1013 }
1014
1015 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor, thor_add);