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