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