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