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