common: Drop part.h from common header
[platform/kernel/u-boot.git] / drivers / usb / gadget / f_rockusb.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2017
4  *
5  * Eddie Cai <eddie.cai.linux@gmail.com>
6  */
7 #include <config.h>
8 #include <common.h>
9 #include <env.h>
10 #include <errno.h>
11 #include <malloc.h>
12 #include <memalign.h>
13 #include <part.h>
14 #include <linux/usb/ch9.h>
15 #include <linux/usb/gadget.h>
16 #include <linux/usb/composite.h>
17 #include <linux/compiler.h>
18 #include <version.h>
19 #include <g_dnl.h>
20 #include <asm/arch-rockchip/f_rockusb.h>
21
22 static inline struct f_rockusb *func_to_rockusb(struct usb_function *f)
23 {
24         return container_of(f, struct f_rockusb, usb_function);
25 }
26
27 static struct usb_endpoint_descriptor fs_ep_in = {
28         .bLength            = USB_DT_ENDPOINT_SIZE,
29         .bDescriptorType    = USB_DT_ENDPOINT,
30         .bEndpointAddress   = USB_DIR_IN,
31         .bmAttributes       = USB_ENDPOINT_XFER_BULK,
32         .wMaxPacketSize     = cpu_to_le16(64),
33 };
34
35 static struct usb_endpoint_descriptor fs_ep_out = {
36         .bLength                = USB_DT_ENDPOINT_SIZE,
37         .bDescriptorType        = USB_DT_ENDPOINT,
38         .bEndpointAddress       = USB_DIR_OUT,
39         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
40         .wMaxPacketSize         = cpu_to_le16(64),
41 };
42
43 static struct usb_endpoint_descriptor hs_ep_in = {
44         .bLength                = USB_DT_ENDPOINT_SIZE,
45         .bDescriptorType        = USB_DT_ENDPOINT,
46         .bEndpointAddress       = USB_DIR_IN,
47         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
48         .wMaxPacketSize         = cpu_to_le16(512),
49 };
50
51 static struct usb_endpoint_descriptor hs_ep_out = {
52         .bLength                = USB_DT_ENDPOINT_SIZE,
53         .bDescriptorType        = USB_DT_ENDPOINT,
54         .bEndpointAddress       = USB_DIR_OUT,
55         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
56         .wMaxPacketSize         = cpu_to_le16(512),
57 };
58
59 static struct usb_interface_descriptor interface_desc = {
60         .bLength                = USB_DT_INTERFACE_SIZE,
61         .bDescriptorType        = USB_DT_INTERFACE,
62         .bInterfaceNumber       = 0x00,
63         .bAlternateSetting      = 0x00,
64         .bNumEndpoints          = 0x02,
65         .bInterfaceClass        = ROCKUSB_INTERFACE_CLASS,
66         .bInterfaceSubClass     = ROCKUSB_INTERFACE_SUB_CLASS,
67         .bInterfaceProtocol     = ROCKUSB_INTERFACE_PROTOCOL,
68 };
69
70 static struct usb_descriptor_header *rkusb_fs_function[] = {
71         (struct usb_descriptor_header *)&interface_desc,
72         (struct usb_descriptor_header *)&fs_ep_in,
73         (struct usb_descriptor_header *)&fs_ep_out,
74 };
75
76 static struct usb_descriptor_header *rkusb_hs_function[] = {
77         (struct usb_descriptor_header *)&interface_desc,
78         (struct usb_descriptor_header *)&hs_ep_in,
79         (struct usb_descriptor_header *)&hs_ep_out,
80         NULL,
81 };
82
83 static const char rkusb_name[] = "Rockchip Rockusb";
84
85 static struct usb_string rkusb_string_defs[] = {
86         [0].s = rkusb_name,
87         {  }                    /* end of list */
88 };
89
90 static struct usb_gadget_strings stringtab_rkusb = {
91         .language       = 0x0409,       /* en-us */
92         .strings        = rkusb_string_defs,
93 };
94
95 static struct usb_gadget_strings *rkusb_strings[] = {
96         &stringtab_rkusb,
97         NULL,
98 };
99
100 static struct f_rockusb *rockusb_func;
101 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
102 static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size);
103
104 struct f_rockusb *get_rkusb(void)
105 {
106         struct f_rockusb *f_rkusb = rockusb_func;
107
108         if (!f_rkusb) {
109                 f_rkusb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_rkusb));
110                 if (!f_rkusb)
111                         return 0;
112
113                 rockusb_func = f_rkusb;
114                 memset(f_rkusb, 0, sizeof(*f_rkusb));
115         }
116
117         if (!f_rkusb->buf_head) {
118                 f_rkusb->buf_head = memalign(CONFIG_SYS_CACHELINE_SIZE,
119                                              RKUSB_BUF_SIZE);
120                 if (!f_rkusb->buf_head)
121                         return 0;
122
123                 f_rkusb->buf = f_rkusb->buf_head;
124                 memset(f_rkusb->buf_head, 0, RKUSB_BUF_SIZE);
125         }
126         return f_rkusb;
127 }
128
129 static struct usb_endpoint_descriptor *rkusb_ep_desc(
130 struct usb_gadget *g,
131 struct usb_endpoint_descriptor *fs,
132 struct usb_endpoint_descriptor *hs)
133 {
134         if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
135                 return hs;
136         return fs;
137 }
138
139 static void rockusb_complete(struct usb_ep *ep, struct usb_request *req)
140 {
141         int status = req->status;
142
143         if (!status)
144                 return;
145         debug("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
146 }
147
148 /* config the rockusb device*/
149 static int rockusb_bind(struct usb_configuration *c, struct usb_function *f)
150 {
151         int id;
152         struct usb_gadget *gadget = c->cdev->gadget;
153         struct f_rockusb *f_rkusb = func_to_rockusb(f);
154         const char *s;
155
156         id = usb_interface_id(c, f);
157         if (id < 0)
158                 return id;
159         interface_desc.bInterfaceNumber = id;
160
161         id = usb_string_id(c->cdev);
162         if (id < 0)
163                 return id;
164
165         rkusb_string_defs[0].id = id;
166         interface_desc.iInterface = id;
167
168         f_rkusb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
169         if (!f_rkusb->in_ep)
170                 return -ENODEV;
171         f_rkusb->in_ep->driver_data = c->cdev;
172
173         f_rkusb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
174         if (!f_rkusb->out_ep)
175                 return -ENODEV;
176         f_rkusb->out_ep->driver_data = c->cdev;
177
178         f->descriptors = rkusb_fs_function;
179
180         if (gadget_is_dualspeed(gadget)) {
181                 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
182                 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
183                 f->hs_descriptors = rkusb_hs_function;
184         }
185
186         s = env_get("serial#");
187         if (s)
188                 g_dnl_set_serialnumber((char *)s);
189
190         return 0;
191 }
192
193 static void rockusb_unbind(struct usb_configuration *c, struct usb_function *f)
194 {
195         /* clear the configuration*/
196         memset(rockusb_func, 0, sizeof(*rockusb_func));
197 }
198
199 static void rockusb_disable(struct usb_function *f)
200 {
201         struct f_rockusb *f_rkusb = func_to_rockusb(f);
202
203         usb_ep_disable(f_rkusb->out_ep);
204         usb_ep_disable(f_rkusb->in_ep);
205
206         if (f_rkusb->out_req) {
207                 free(f_rkusb->out_req->buf);
208                 usb_ep_free_request(f_rkusb->out_ep, f_rkusb->out_req);
209                 f_rkusb->out_req = NULL;
210         }
211         if (f_rkusb->in_req) {
212                 free(f_rkusb->in_req->buf);
213                 usb_ep_free_request(f_rkusb->in_ep, f_rkusb->in_req);
214                 f_rkusb->in_req = NULL;
215         }
216         if (f_rkusb->buf_head) {
217                 free(f_rkusb->buf_head);
218                 f_rkusb->buf_head = NULL;
219                 f_rkusb->buf = NULL;
220         }
221 }
222
223 static struct usb_request *rockusb_start_ep(struct usb_ep *ep)
224 {
225         struct usb_request *req;
226
227         req = usb_ep_alloc_request(ep, 0);
228         if (!req)
229                 return NULL;
230
231         req->length = EP_BUFFER_SIZE;
232         req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
233         if (!req->buf) {
234                 usb_ep_free_request(ep, req);
235                 return NULL;
236         }
237         memset(req->buf, 0, req->length);
238
239         return req;
240 }
241
242 static int rockusb_set_alt(struct usb_function *f, unsigned int interface,
243                            unsigned int alt)
244 {
245         int ret;
246         struct usb_composite_dev *cdev = f->config->cdev;
247         struct usb_gadget *gadget = cdev->gadget;
248         struct f_rockusb *f_rkusb = func_to_rockusb(f);
249         const struct usb_endpoint_descriptor *d;
250
251         debug("%s: func: %s intf: %d alt: %d\n",
252               __func__, f->name, interface, alt);
253
254         d = rkusb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
255         ret = usb_ep_enable(f_rkusb->out_ep, d);
256         if (ret) {
257                 printf("failed to enable out ep\n");
258                 return ret;
259         }
260
261         f_rkusb->out_req = rockusb_start_ep(f_rkusb->out_ep);
262         if (!f_rkusb->out_req) {
263                 printf("failed to alloc out req\n");
264                 ret = -EINVAL;
265                 goto err;
266         }
267         f_rkusb->out_req->complete = rx_handler_command;
268
269         d = rkusb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
270         ret = usb_ep_enable(f_rkusb->in_ep, d);
271         if (ret) {
272                 printf("failed to enable in ep\n");
273                 goto err;
274         }
275
276         f_rkusb->in_req = rockusb_start_ep(f_rkusb->in_ep);
277         if (!f_rkusb->in_req) {
278                 printf("failed alloc req in\n");
279                 ret = -EINVAL;
280                 goto err;
281         }
282         f_rkusb->in_req->complete = rockusb_complete;
283
284         ret = usb_ep_queue(f_rkusb->out_ep, f_rkusb->out_req, 0);
285         if (ret)
286                 goto err;
287
288         return 0;
289 err:
290         rockusb_disable(f);
291         return ret;
292 }
293
294 static int rockusb_add(struct usb_configuration *c)
295 {
296         struct f_rockusb *f_rkusb = get_rkusb();
297         int status;
298
299         debug("%s: cdev: 0x%p\n", __func__, c->cdev);
300
301         f_rkusb->usb_function.name = "f_rockusb";
302         f_rkusb->usb_function.bind = rockusb_bind;
303         f_rkusb->usb_function.unbind = rockusb_unbind;
304         f_rkusb->usb_function.set_alt = rockusb_set_alt;
305         f_rkusb->usb_function.disable = rockusb_disable;
306         f_rkusb->usb_function.strings = rkusb_strings;
307
308         status = usb_add_function(c, &f_rkusb->usb_function);
309         if (status) {
310                 free(f_rkusb);
311                 rockusb_func = f_rkusb;
312         }
313         return status;
314 }
315
316 void rockusb_dev_init(char *dev_type, int dev_index)
317 {
318         struct f_rockusb *f_rkusb = get_rkusb();
319
320         f_rkusb->dev_type = dev_type;
321         f_rkusb->dev_index = dev_index;
322 }
323
324 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_rockusb, rockusb_add);
325
326 static int rockusb_tx_write(const char *buffer, unsigned int buffer_size)
327 {
328         struct usb_request *in_req = rockusb_func->in_req;
329         int ret;
330
331         memcpy(in_req->buf, buffer, buffer_size);
332         in_req->length = buffer_size;
333         debug("Transferring 0x%x bytes\n", buffer_size);
334         usb_ep_dequeue(rockusb_func->in_ep, in_req);
335         ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0);
336         if (ret)
337                 printf("Error %d on queue\n", ret);
338         return 0;
339 }
340
341 static int rockusb_tx_write_str(const char *buffer)
342 {
343         return rockusb_tx_write(buffer, strlen(buffer));
344 }
345
346 #ifdef DEBUG
347 static void printcbw(char *buf)
348 {
349         ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
350                                  sizeof(struct fsg_bulk_cb_wrap));
351
352         memcpy((char *)cbw, buf, USB_BULK_CB_WRAP_LEN);
353
354         debug("cbw: signature:%x\n", cbw->signature);
355         debug("cbw: tag=%x\n", cbw->tag);
356         debug("cbw: data_transfer_length=%d\n", cbw->data_transfer_length);
357         debug("cbw: flags=%x\n", cbw->flags);
358         debug("cbw: lun=%d\n", cbw->lun);
359         debug("cbw: length=%d\n", cbw->length);
360         debug("cbw: ucOperCode=%x\n", cbw->CDB[0]);
361         debug("cbw: ucReserved=%x\n", cbw->CDB[1]);
362         debug("cbw: dwAddress:%x %x %x %x\n", cbw->CDB[5], cbw->CDB[4],
363               cbw->CDB[3], cbw->CDB[2]);
364         debug("cbw: ucReserved2=%x\n", cbw->CDB[6]);
365         debug("cbw: uslength:%x %x\n", cbw->CDB[8], cbw->CDB[7]);
366 }
367
368 static void printcsw(char *buf)
369 {
370         ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
371                                  sizeof(struct bulk_cs_wrap));
372         memcpy((char *)csw, buf, USB_BULK_CS_WRAP_LEN);
373         debug("csw: signature:%x\n", csw->signature);
374         debug("csw: tag:%x\n", csw->tag);
375         debug("csw: residue:%x\n", csw->residue);
376         debug("csw: status:%x\n", csw->status);
377 }
378 #endif
379
380 static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size)
381 {
382         ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
383                                  sizeof(struct bulk_cs_wrap));
384         csw->signature = cpu_to_le32(USB_BULK_CS_SIG);
385         csw->tag = tag;
386         csw->residue = cpu_to_be32(residue);
387         csw->status = status;
388 #ifdef DEBUG
389         printcsw((char *)csw);
390 #endif
391         return rockusb_tx_write((char *)csw, size);
392 }
393
394 static void tx_handler_send_csw(struct usb_ep *ep, struct usb_request *req)
395 {
396         struct f_rockusb *f_rkusb = get_rkusb();
397         int status = req->status;
398
399         if (status)
400                 debug("status: %d ep '%s' trans: %d\n",
401                       status, ep->name, req->actual);
402
403         /* Return back to default in_req complete function after sending CSW */
404         req->complete = rockusb_complete;
405         rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD, USB_BULK_CS_WRAP_LEN);
406 }
407
408 static unsigned int rx_bytes_expected(struct usb_ep *ep)
409 {
410         struct f_rockusb *f_rkusb = get_rkusb();
411         int rx_remain = f_rkusb->dl_size - f_rkusb->dl_bytes;
412         unsigned int rem;
413         unsigned int maxpacket = ep->maxpacket;
414
415         if (rx_remain <= 0)
416                 return 0;
417         else if (rx_remain > EP_BUFFER_SIZE)
418                 return EP_BUFFER_SIZE;
419
420         rem = rx_remain % maxpacket;
421         if (rem > 0)
422                 rx_remain = rx_remain + (maxpacket - rem);
423
424         return rx_remain;
425 }
426
427 /* usb_request complete call back to handle upload image */
428 static void tx_handler_ul_image(struct usb_ep *ep, struct usb_request *req)
429 {
430         ALLOC_CACHE_ALIGN_BUFFER(char, rbuffer, RKBLOCK_BUF_SIZE);
431         struct f_rockusb *f_rkusb = get_rkusb();
432         struct usb_request *in_req = rockusb_func->in_req;
433         int ret;
434
435         /* Print error status of previous transfer */
436         if (req->status)
437                 debug("status: %d ep '%s' trans: %d len %d\n", req->status,
438                       ep->name, req->actual, req->length);
439
440         /* On transfer complete reset in_req and feedback host with CSW_GOOD */
441         if (f_rkusb->ul_bytes >= f_rkusb->ul_size) {
442                 in_req->length = 0;
443                 in_req->complete = rockusb_complete;
444
445                 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD,
446                                      USB_BULK_CS_WRAP_LEN);
447                 return;
448         }
449
450         /* Proceed with current chunk */
451         unsigned int transfer_size = f_rkusb->ul_size - f_rkusb->ul_bytes;
452
453         if (transfer_size > RKBLOCK_BUF_SIZE)
454                 transfer_size = RKBLOCK_BUF_SIZE;
455         /* Read at least one block */
456         unsigned int blkcount = (transfer_size + f_rkusb->desc->blksz - 1) /
457                                 f_rkusb->desc->blksz;
458
459         debug("ul %x bytes, %x blks, read lba %x, ul_size:%x, ul_bytes:%x, ",
460               transfer_size, blkcount, f_rkusb->lba,
461               f_rkusb->ul_size, f_rkusb->ul_bytes);
462
463         int blks = blk_dread(f_rkusb->desc, f_rkusb->lba, blkcount, rbuffer);
464
465         if (blks != blkcount) {
466                 printf("failed reading from device %s: %d\n",
467                        f_rkusb->dev_type, f_rkusb->dev_index);
468                 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
469                                      USB_BULK_CS_WRAP_LEN);
470                 return;
471         }
472         f_rkusb->lba += blkcount;
473         f_rkusb->ul_bytes += transfer_size;
474
475         /* Proceed with USB request */
476         memcpy(in_req->buf, rbuffer, transfer_size);
477         in_req->length = transfer_size;
478         in_req->complete = tx_handler_ul_image;
479         debug("Uploading 0x%x bytes\n", transfer_size);
480         usb_ep_dequeue(rockusb_func->in_ep, in_req);
481         ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0);
482         if (ret)
483                 printf("Error %d on queue\n", ret);
484 }
485
486 /* usb_request complete call back to handle down load image */
487 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
488 {
489         struct f_rockusb *f_rkusb = get_rkusb();
490         unsigned int transfer_size = 0;
491         const unsigned char *buffer = req->buf;
492         unsigned int buffer_size = req->actual;
493
494         transfer_size = f_rkusb->dl_size - f_rkusb->dl_bytes;
495
496         if (req->status != 0) {
497                 printf("Bad status: %d\n", req->status);
498                 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
499                                      USB_BULK_CS_WRAP_LEN);
500                 return;
501         }
502
503         if (buffer_size < transfer_size)
504                 transfer_size = buffer_size;
505
506         memcpy((void *)f_rkusb->buf, buffer, transfer_size);
507         f_rkusb->dl_bytes += transfer_size;
508         int blks = 0, blkcnt = transfer_size  / f_rkusb->desc->blksz;
509
510         debug("dl %x bytes, %x blks, write lba %x, dl_size:%x, dl_bytes:%x, ",
511               transfer_size, blkcnt, f_rkusb->lba, f_rkusb->dl_size,
512               f_rkusb->dl_bytes);
513         blks = blk_dwrite(f_rkusb->desc, f_rkusb->lba, blkcnt, f_rkusb->buf);
514         if (blks != blkcnt) {
515                 printf("failed writing to device %s: %d\n", f_rkusb->dev_type,
516                        f_rkusb->dev_index);
517                 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
518                                      USB_BULK_CS_WRAP_LEN);
519                 return;
520         }
521         f_rkusb->lba += blkcnt;
522
523         /* Check if transfer is done */
524         if (f_rkusb->dl_bytes >= f_rkusb->dl_size) {
525                 req->complete = rx_handler_command;
526                 req->length = EP_BUFFER_SIZE;
527                 f_rkusb->buf = f_rkusb->buf_head;
528                 debug("transfer 0x%x bytes done\n", f_rkusb->dl_size);
529                 f_rkusb->dl_size = 0;
530                 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD,
531                                      USB_BULK_CS_WRAP_LEN);
532         } else {
533                 req->length = rx_bytes_expected(ep);
534                 if (f_rkusb->buf == f_rkusb->buf_head)
535                         f_rkusb->buf = f_rkusb->buf_head + EP_BUFFER_SIZE;
536                 else
537                         f_rkusb->buf = f_rkusb->buf_head;
538
539                 debug("remain %x bytes, %lx sectors\n", req->length,
540                       req->length / f_rkusb->desc->blksz);
541         }
542
543         req->actual = 0;
544         usb_ep_queue(ep, req, 0);
545 }
546
547 static void cb_test_unit_ready(struct usb_ep *ep, struct usb_request *req)
548 {
549         ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
550                                  sizeof(struct fsg_bulk_cb_wrap));
551
552         memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
553
554         rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
555                              CSW_GOOD, USB_BULK_CS_WRAP_LEN);
556 }
557
558 static void cb_read_storage_id(struct usb_ep *ep, struct usb_request *req)
559 {
560         ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
561                                  sizeof(struct fsg_bulk_cb_wrap));
562         struct f_rockusb *f_rkusb = get_rkusb();
563         char emmc_id[] = "EMMC ";
564
565         printf("read storage id\n");
566         memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
567
568         /* Prepare for sending subsequent CSW_GOOD */
569         f_rkusb->tag = cbw->tag;
570         f_rkusb->in_req->complete = tx_handler_send_csw;
571
572         rockusb_tx_write_str(emmc_id);
573 }
574
575 int __weak rk_get_bootrom_chip_version(unsigned int *chip_info, int size)
576 {
577         return 0;
578 }
579
580 static void cb_get_chip_version(struct usb_ep *ep, struct usb_request *req)
581 {
582         ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
583                                  sizeof(struct fsg_bulk_cb_wrap));
584         struct f_rockusb *f_rkusb = get_rkusb();
585         unsigned int chip_info[4], i;
586
587         memset(chip_info, 0, sizeof(chip_info));
588         rk_get_bootrom_chip_version(chip_info, 4);
589
590         /*
591          * Chip Version is a string saved in BOOTROM address space Little Endian
592          *
593          * Ex for rk3288: 0x33323041 0x32303134 0x30383133 0x56323030
594          * which brings:  320A20140813V200
595          *
596          * Note that memory version do invert MSB/LSB so printing the char
597          * buffer will show: A02341023180002V
598          */
599         printf("read chip version: ");
600         for (i = 0; i < 4; i++) {
601                 printf("%c%c%c%c",
602                        (chip_info[i] >> 24) & 0xFF,
603                        (chip_info[i] >> 16) & 0xFF,
604                        (chip_info[i] >>  8) & 0xFF,
605                        (chip_info[i] >>  0) & 0xFF);
606         }
607         printf("\n");
608         memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
609
610         /* Prepare for sending subsequent CSW_GOOD */
611         f_rkusb->tag = cbw->tag;
612         f_rkusb->in_req->complete = tx_handler_send_csw;
613
614         rockusb_tx_write((char *)chip_info, sizeof(chip_info));
615 }
616
617 static void cb_read_lba(struct usb_ep *ep, struct usb_request *req)
618 {
619         ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
620                                  sizeof(struct fsg_bulk_cb_wrap));
621         struct f_rockusb *f_rkusb = get_rkusb();
622         int sector_count;
623
624         memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
625         sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
626         f_rkusb->tag = cbw->tag;
627
628         if (!f_rkusb->desc) {
629                 char *type = f_rkusb->dev_type;
630                 int index = f_rkusb->dev_index;
631
632                 f_rkusb->desc = blk_get_dev(type, index);
633                 if (!f_rkusb->desc ||
634                     f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
635                         printf("invalid device \"%s\", %d\n", type, index);
636                         rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
637                                              USB_BULK_CS_WRAP_LEN);
638                         return;
639                 }
640         }
641
642         f_rkusb->lba = get_unaligned_be32(&cbw->CDB[2]);
643         f_rkusb->ul_size = sector_count * f_rkusb->desc->blksz;
644         f_rkusb->ul_bytes = 0;
645
646         debug("require read %x bytes, %x sectors from lba %x\n",
647               f_rkusb->ul_size, sector_count, f_rkusb->lba);
648
649         if (f_rkusb->ul_size == 0)  {
650                 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
651                                      CSW_FAIL, USB_BULK_CS_WRAP_LEN);
652                 return;
653         }
654
655         /* Start right now sending first chunk */
656         tx_handler_ul_image(ep, req);
657 }
658
659 static void cb_write_lba(struct usb_ep *ep, struct usb_request *req)
660 {
661         ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
662                                  sizeof(struct fsg_bulk_cb_wrap));
663         struct f_rockusb *f_rkusb = get_rkusb();
664         int sector_count;
665
666         memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
667         sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
668         f_rkusb->tag = cbw->tag;
669
670         if (!f_rkusb->desc) {
671                 char *type = f_rkusb->dev_type;
672                 int index = f_rkusb->dev_index;
673
674                 f_rkusb->desc = blk_get_dev(type, index);
675                 if (!f_rkusb->desc ||
676                     f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
677                         printf("invalid device \"%s\", %d\n", type, index);
678                         rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
679                                              USB_BULK_CS_WRAP_LEN);
680                         return;
681                 }
682         }
683
684         f_rkusb->lba = get_unaligned_be32(&cbw->CDB[2]);
685         f_rkusb->dl_size = sector_count * f_rkusb->desc->blksz;
686         f_rkusb->dl_bytes = 0;
687
688         debug("require write %x bytes, %x sectors to lba %x\n",
689               f_rkusb->dl_size, sector_count, f_rkusb->lba);
690
691         if (f_rkusb->dl_size == 0)  {
692                 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
693                                      CSW_FAIL, USB_BULK_CS_WRAP_LEN);
694         } else {
695                 req->complete = rx_handler_dl_image;
696                 req->length = rx_bytes_expected(ep);
697         }
698 }
699
700 static void cb_erase_lba(struct usb_ep *ep, struct usb_request *req)
701 {
702         ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
703                                  sizeof(struct fsg_bulk_cb_wrap));
704         struct f_rockusb *f_rkusb = get_rkusb();
705         int sector_count, lba, blks;
706
707         memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
708         sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
709         f_rkusb->tag = cbw->tag;
710
711         if (!f_rkusb->desc) {
712                 char *type = f_rkusb->dev_type;
713                 int index = f_rkusb->dev_index;
714
715                 f_rkusb->desc = blk_get_dev(type, index);
716                 if (!f_rkusb->desc ||
717                     f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
718                         printf("invalid device \"%s\", %d\n", type, index);
719                         rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
720                                              USB_BULK_CS_WRAP_LEN);
721                         return;
722                 }
723         }
724
725         lba = get_unaligned_be32(&cbw->CDB[2]);
726
727         debug("require erase %x sectors from lba %x\n",
728               sector_count, lba);
729
730         blks = blk_derase(f_rkusb->desc, lba, sector_count);
731         if (blks != sector_count) {
732                 printf("failed erasing device %s: %d\n", f_rkusb->dev_type,
733                        f_rkusb->dev_index);
734                 rockusb_tx_write_csw(f_rkusb->tag,
735                                      cbw->data_transfer_length, CSW_FAIL,
736                                      USB_BULK_CS_WRAP_LEN);
737                 return;
738         }
739
740         rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
741                              USB_BULK_CS_WRAP_LEN);
742 }
743
744 void __weak rkusb_set_reboot_flag(int flag)
745 {
746         struct f_rockusb *f_rkusb = get_rkusb();
747
748         printf("rockkusb set reboot flag: %d\n", f_rkusb->reboot_flag);
749 }
750
751 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
752 {
753         struct f_rockusb *f_rkusb = get_rkusb();
754
755         rkusb_set_reboot_flag(f_rkusb->reboot_flag);
756         do_reset(NULL, 0, 0, NULL);
757 }
758
759 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
760 {
761         ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
762                                  sizeof(struct fsg_bulk_cb_wrap));
763         struct f_rockusb *f_rkusb = get_rkusb();
764
765         memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
766         f_rkusb->reboot_flag = cbw->CDB[1];
767         rockusb_func->in_req->complete = compl_do_reset;
768         rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
769                              USB_BULK_CS_WRAP_LEN);
770 }
771
772 static void cb_not_support(struct usb_ep *ep, struct usb_request *req)
773 {
774         ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
775                                  sizeof(struct fsg_bulk_cb_wrap));
776
777         memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
778         printf("Rockusb command %x not support yet\n", cbw->CDB[0]);
779         rockusb_tx_write_csw(cbw->tag, 0, CSW_FAIL, USB_BULK_CS_WRAP_LEN);
780 }
781
782 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
783         {
784                 .cmd = K_FW_TEST_UNIT_READY,
785                 .cb = cb_test_unit_ready,
786         },
787         {
788                 .cmd = K_FW_READ_FLASH_ID,
789                 .cb = cb_read_storage_id,
790         },
791         {
792                 .cmd = K_FW_SET_DEVICE_ID,
793                 .cb = cb_not_support,
794         },
795         {
796                 .cmd = K_FW_TEST_BAD_BLOCK,
797                 .cb = cb_not_support,
798         },
799         {
800                 .cmd = K_FW_READ_10,
801                 .cb = cb_not_support,
802         },
803         {
804                 .cmd = K_FW_WRITE_10,
805                 .cb = cb_not_support,
806         },
807         {
808                 .cmd = K_FW_ERASE_10,
809                 .cb = cb_not_support,
810         },
811         {
812                 .cmd = K_FW_WRITE_SPARE,
813                 .cb = cb_not_support,
814         },
815         {
816                 .cmd = K_FW_READ_SPARE,
817                 .cb = cb_not_support,
818         },
819         {
820                 .cmd = K_FW_ERASE_10_FORCE,
821                 .cb = cb_not_support,
822         },
823         {
824                 .cmd = K_FW_GET_VERSION,
825                 .cb = cb_not_support,
826         },
827         {
828                 .cmd = K_FW_LBA_READ_10,
829                 .cb = cb_read_lba,
830         },
831         {
832                 .cmd = K_FW_LBA_WRITE_10,
833                 .cb = cb_write_lba,
834         },
835         {
836                 .cmd = K_FW_ERASE_SYS_DISK,
837                 .cb = cb_not_support,
838         },
839         {
840                 .cmd = K_FW_SDRAM_READ_10,
841                 .cb = cb_not_support,
842         },
843         {
844                 .cmd = K_FW_SDRAM_WRITE_10,
845                 .cb = cb_not_support,
846         },
847         {
848                 .cmd = K_FW_SDRAM_EXECUTE,
849                 .cb = cb_not_support,
850         },
851         {
852                 .cmd = K_FW_READ_FLASH_INFO,
853                 .cb = cb_not_support,
854         },
855         {
856                 .cmd = K_FW_GET_CHIP_VER,
857                 .cb = cb_get_chip_version,
858         },
859         {
860                 .cmd = K_FW_LOW_FORMAT,
861                 .cb = cb_not_support,
862         },
863         {
864                 .cmd = K_FW_SET_RESET_FLAG,
865                 .cb = cb_not_support,
866         },
867         {
868                 .cmd = K_FW_SPI_READ_10,
869                 .cb = cb_not_support,
870         },
871         {
872                 .cmd = K_FW_SPI_WRITE_10,
873                 .cb = cb_not_support,
874         },
875         {
876                 .cmd = K_FW_LBA_ERASE_10,
877                 .cb = cb_erase_lba,
878         },
879         {
880                 .cmd = K_FW_SESSION,
881                 .cb = cb_not_support,
882         },
883         {
884                 .cmd = K_FW_RESET,
885                 .cb = cb_reboot,
886         },
887 };
888
889 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
890 {
891         void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
892
893         ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
894                                  sizeof(struct fsg_bulk_cb_wrap));
895         char *cmdbuf = req->buf;
896         int i;
897
898         if (req->status || req->length == 0)
899                 return;
900
901         memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
902 #ifdef DEBUG
903         printcbw(req->buf);
904 #endif
905
906         for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
907                 if (cmd_dispatch_info[i].cmd == cbw->CDB[0]) {
908                         func_cb = cmd_dispatch_info[i].cb;
909                         break;
910                 }
911         }
912
913         if (!func_cb) {
914                 printf("unknown command: %s\n", (char *)req->buf);
915                 rockusb_tx_write_str("FAILunknown command");
916         } else {
917                 if (req->actual < req->length) {
918                         u8 *buf = (u8 *)req->buf;
919
920                         buf[req->actual] = 0;
921                         func_cb(ep, req);
922                 } else {
923                         puts("buffer overflow\n");
924                         rockusb_tx_write_str("FAILbuffer overflow");
925                 }
926         }
927
928         *cmdbuf = '\0';
929         req->actual = 0;
930         usb_ep_queue(ep, req, 0);
931 }