1 // SPDX-License-Identifier: GPL-2.0+
5 * Eddie Cai <eddie.cai.linux@gmail.com>
14 #include <linux/usb/ch9.h>
15 #include <linux/usb/gadget.h>
16 #include <linux/usb/composite.h>
17 #include <linux/compiler.h>
20 #include <asm/arch-rockchip/f_rockusb.h>
22 static inline struct f_rockusb *func_to_rockusb(struct usb_function *f)
24 return container_of(f, struct f_rockusb, usb_function);
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),
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),
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),
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),
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,
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,
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,
83 static const char rkusb_name[] = "Rockchip Rockusb";
85 static struct usb_string rkusb_string_defs[] = {
90 static struct usb_gadget_strings stringtab_rkusb = {
91 .language = 0x0409, /* en-us */
92 .strings = rkusb_string_defs,
95 static struct usb_gadget_strings *rkusb_strings[] = {
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);
104 struct f_rockusb *get_rkusb(void)
106 struct f_rockusb *f_rkusb = rockusb_func;
109 f_rkusb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_rkusb));
113 rockusb_func = f_rkusb;
114 memset(f_rkusb, 0, sizeof(*f_rkusb));
117 if (!f_rkusb->buf_head) {
118 f_rkusb->buf_head = memalign(CONFIG_SYS_CACHELINE_SIZE,
120 if (!f_rkusb->buf_head)
123 f_rkusb->buf = f_rkusb->buf_head;
124 memset(f_rkusb->buf_head, 0, RKUSB_BUF_SIZE);
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)
134 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
139 static void rockusb_complete(struct usb_ep *ep, struct usb_request *req)
141 int status = req->status;
145 debug("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
148 /* config the rockusb device*/
149 static int rockusb_bind(struct usb_configuration *c, struct usb_function *f)
152 struct usb_gadget *gadget = c->cdev->gadget;
153 struct f_rockusb *f_rkusb = func_to_rockusb(f);
156 id = usb_interface_id(c, f);
159 interface_desc.bInterfaceNumber = id;
161 id = usb_string_id(c->cdev);
165 rkusb_string_defs[0].id = id;
166 interface_desc.iInterface = id;
168 f_rkusb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
171 f_rkusb->in_ep->driver_data = c->cdev;
173 f_rkusb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
174 if (!f_rkusb->out_ep)
176 f_rkusb->out_ep->driver_data = c->cdev;
178 f->descriptors = rkusb_fs_function;
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;
186 s = env_get("serial#");
188 g_dnl_set_serialnumber((char *)s);
193 static void rockusb_unbind(struct usb_configuration *c, struct usb_function *f)
195 /* clear the configuration*/
196 memset(rockusb_func, 0, sizeof(*rockusb_func));
199 static void rockusb_disable(struct usb_function *f)
201 struct f_rockusb *f_rkusb = func_to_rockusb(f);
203 usb_ep_disable(f_rkusb->out_ep);
204 usb_ep_disable(f_rkusb->in_ep);
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;
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;
216 if (f_rkusb->buf_head) {
217 free(f_rkusb->buf_head);
218 f_rkusb->buf_head = NULL;
223 static struct usb_request *rockusb_start_ep(struct usb_ep *ep)
225 struct usb_request *req;
227 req = usb_ep_alloc_request(ep, 0);
231 req->length = EP_BUFFER_SIZE;
232 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
234 usb_ep_free_request(ep, req);
237 memset(req->buf, 0, req->length);
242 static int rockusb_set_alt(struct usb_function *f, unsigned int interface,
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;
251 debug("%s: func: %s intf: %d alt: %d\n",
252 __func__, f->name, interface, alt);
254 d = rkusb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
255 ret = usb_ep_enable(f_rkusb->out_ep, d);
257 printf("failed to enable out ep\n");
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");
267 f_rkusb->out_req->complete = rx_handler_command;
269 d = rkusb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
270 ret = usb_ep_enable(f_rkusb->in_ep, d);
272 printf("failed to enable in ep\n");
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");
282 f_rkusb->in_req->complete = rockusb_complete;
284 ret = usb_ep_queue(f_rkusb->out_ep, f_rkusb->out_req, 0);
294 static int rockusb_add(struct usb_configuration *c)
296 struct f_rockusb *f_rkusb = get_rkusb();
299 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
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;
308 status = usb_add_function(c, &f_rkusb->usb_function);
311 rockusb_func = f_rkusb;
316 void rockusb_dev_init(char *dev_type, int dev_index)
318 struct f_rockusb *f_rkusb = get_rkusb();
320 f_rkusb->dev_type = dev_type;
321 f_rkusb->dev_index = dev_index;
324 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_rockusb, rockusb_add);
326 static int rockusb_tx_write(const char *buffer, unsigned int buffer_size)
328 struct usb_request *in_req = rockusb_func->in_req;
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);
337 printf("Error %d on queue\n", ret);
341 static int rockusb_tx_write_str(const char *buffer)
343 return rockusb_tx_write(buffer, strlen(buffer));
347 static void printcbw(char *buf)
349 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
350 sizeof(struct fsg_bulk_cb_wrap));
352 memcpy((char *)cbw, buf, USB_BULK_CB_WRAP_LEN);
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]);
368 static void printcsw(char *buf)
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);
380 static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size)
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);
386 csw->residue = cpu_to_be32(residue);
387 csw->status = status;
389 printcsw((char *)csw);
391 return rockusb_tx_write((char *)csw, size);
394 static void tx_handler_send_csw(struct usb_ep *ep, struct usb_request *req)
396 struct f_rockusb *f_rkusb = get_rkusb();
397 int status = req->status;
400 debug("status: %d ep '%s' trans: %d\n",
401 status, ep->name, req->actual);
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);
408 static unsigned int rx_bytes_expected(struct usb_ep *ep)
410 struct f_rockusb *f_rkusb = get_rkusb();
411 int rx_remain = f_rkusb->dl_size - f_rkusb->dl_bytes;
413 unsigned int maxpacket = ep->maxpacket;
417 else if (rx_remain > EP_BUFFER_SIZE)
418 return EP_BUFFER_SIZE;
420 rem = rx_remain % maxpacket;
422 rx_remain = rx_remain + (maxpacket - rem);
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)
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;
435 /* Print error status of previous transfer */
437 debug("status: %d ep '%s' trans: %d len %d\n", req->status,
438 ep->name, req->actual, req->length);
440 /* On transfer complete reset in_req and feedback host with CSW_GOOD */
441 if (f_rkusb->ul_bytes >= f_rkusb->ul_size) {
443 in_req->complete = rockusb_complete;
445 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD,
446 USB_BULK_CS_WRAP_LEN);
450 /* Proceed with current chunk */
451 unsigned int transfer_size = f_rkusb->ul_size - f_rkusb->ul_bytes;
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;
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);
463 int blks = blk_dread(f_rkusb->desc, f_rkusb->lba, blkcount, rbuffer);
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);
472 f_rkusb->lba += blkcount;
473 f_rkusb->ul_bytes += transfer_size;
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);
483 printf("Error %d on queue\n", ret);
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)
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;
494 transfer_size = f_rkusb->dl_size - f_rkusb->dl_bytes;
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);
503 if (buffer_size < transfer_size)
504 transfer_size = buffer_size;
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;
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,
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,
517 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
518 USB_BULK_CS_WRAP_LEN);
521 f_rkusb->lba += blkcnt;
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);
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;
537 f_rkusb->buf = f_rkusb->buf_head;
539 debug("remain %x bytes, %lx sectors\n", req->length,
540 req->length / f_rkusb->desc->blksz);
544 usb_ep_queue(ep, req, 0);
547 static void cb_test_unit_ready(struct usb_ep *ep, struct usb_request *req)
549 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
550 sizeof(struct fsg_bulk_cb_wrap));
552 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
554 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
555 CSW_GOOD, USB_BULK_CS_WRAP_LEN);
558 static void cb_read_storage_id(struct usb_ep *ep, struct usb_request *req)
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 ";
565 printf("read storage id\n");
566 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
568 /* Prepare for sending subsequent CSW_GOOD */
569 f_rkusb->tag = cbw->tag;
570 f_rkusb->in_req->complete = tx_handler_send_csw;
572 rockusb_tx_write_str(emmc_id);
575 int __weak rk_get_bootrom_chip_version(unsigned int *chip_info, int size)
580 static void cb_get_chip_version(struct usb_ep *ep, struct usb_request *req)
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;
587 memset(chip_info, 0, sizeof(chip_info));
588 rk_get_bootrom_chip_version(chip_info, 4);
591 * Chip Version is a string saved in BOOTROM address space Little Endian
593 * Ex for rk3288: 0x33323041 0x32303134 0x30383133 0x56323030
594 * which brings: 320A20140813V200
596 * Note that memory version do invert MSB/LSB so printing the char
597 * buffer will show: A02341023180002V
599 printf("read chip version: ");
600 for (i = 0; i < 4; i++) {
602 (chip_info[i] >> 24) & 0xFF,
603 (chip_info[i] >> 16) & 0xFF,
604 (chip_info[i] >> 8) & 0xFF,
605 (chip_info[i] >> 0) & 0xFF);
608 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
610 /* Prepare for sending subsequent CSW_GOOD */
611 f_rkusb->tag = cbw->tag;
612 f_rkusb->in_req->complete = tx_handler_send_csw;
614 rockusb_tx_write((char *)chip_info, sizeof(chip_info));
617 static void cb_read_lba(struct usb_ep *ep, struct usb_request *req)
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();
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;
628 if (!f_rkusb->desc) {
629 char *type = f_rkusb->dev_type;
630 int index = f_rkusb->dev_index;
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);
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;
646 debug("require read %x bytes, %x sectors from lba %x\n",
647 f_rkusb->ul_size, sector_count, f_rkusb->lba);
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);
655 /* Start right now sending first chunk */
656 tx_handler_ul_image(ep, req);
659 static void cb_write_lba(struct usb_ep *ep, struct usb_request *req)
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();
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;
670 if (!f_rkusb->desc) {
671 char *type = f_rkusb->dev_type;
672 int index = f_rkusb->dev_index;
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);
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;
688 debug("require write %x bytes, %x sectors to lba %x\n",
689 f_rkusb->dl_size, sector_count, f_rkusb->lba);
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);
695 req->complete = rx_handler_dl_image;
696 req->length = rx_bytes_expected(ep);
700 static void cb_erase_lba(struct usb_ep *ep, struct usb_request *req)
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;
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;
711 if (!f_rkusb->desc) {
712 char *type = f_rkusb->dev_type;
713 int index = f_rkusb->dev_index;
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);
725 lba = get_unaligned_be32(&cbw->CDB[2]);
727 debug("require erase %x sectors from lba %x\n",
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,
734 rockusb_tx_write_csw(f_rkusb->tag,
735 cbw->data_transfer_length, CSW_FAIL,
736 USB_BULK_CS_WRAP_LEN);
740 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
741 USB_BULK_CS_WRAP_LEN);
744 void __weak rkusb_set_reboot_flag(int flag)
746 struct f_rockusb *f_rkusb = get_rkusb();
748 printf("rockkusb set reboot flag: %d\n", f_rkusb->reboot_flag);
751 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
753 struct f_rockusb *f_rkusb = get_rkusb();
755 rkusb_set_reboot_flag(f_rkusb->reboot_flag);
756 do_reset(NULL, 0, 0, NULL);
759 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
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();
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);
772 static void cb_not_support(struct usb_ep *ep, struct usb_request *req)
774 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
775 sizeof(struct fsg_bulk_cb_wrap));
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);
782 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
784 .cmd = K_FW_TEST_UNIT_READY,
785 .cb = cb_test_unit_ready,
788 .cmd = K_FW_READ_FLASH_ID,
789 .cb = cb_read_storage_id,
792 .cmd = K_FW_SET_DEVICE_ID,
793 .cb = cb_not_support,
796 .cmd = K_FW_TEST_BAD_BLOCK,
797 .cb = cb_not_support,
801 .cb = cb_not_support,
804 .cmd = K_FW_WRITE_10,
805 .cb = cb_not_support,
808 .cmd = K_FW_ERASE_10,
809 .cb = cb_not_support,
812 .cmd = K_FW_WRITE_SPARE,
813 .cb = cb_not_support,
816 .cmd = K_FW_READ_SPARE,
817 .cb = cb_not_support,
820 .cmd = K_FW_ERASE_10_FORCE,
821 .cb = cb_not_support,
824 .cmd = K_FW_GET_VERSION,
825 .cb = cb_not_support,
828 .cmd = K_FW_LBA_READ_10,
832 .cmd = K_FW_LBA_WRITE_10,
836 .cmd = K_FW_ERASE_SYS_DISK,
837 .cb = cb_not_support,
840 .cmd = K_FW_SDRAM_READ_10,
841 .cb = cb_not_support,
844 .cmd = K_FW_SDRAM_WRITE_10,
845 .cb = cb_not_support,
848 .cmd = K_FW_SDRAM_EXECUTE,
849 .cb = cb_not_support,
852 .cmd = K_FW_READ_FLASH_INFO,
853 .cb = cb_not_support,
856 .cmd = K_FW_GET_CHIP_VER,
857 .cb = cb_get_chip_version,
860 .cmd = K_FW_LOW_FORMAT,
861 .cb = cb_not_support,
864 .cmd = K_FW_SET_RESET_FLAG,
865 .cb = cb_not_support,
868 .cmd = K_FW_SPI_READ_10,
869 .cb = cb_not_support,
872 .cmd = K_FW_SPI_WRITE_10,
873 .cb = cb_not_support,
876 .cmd = K_FW_LBA_ERASE_10,
881 .cb = cb_not_support,
889 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
891 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
893 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
894 sizeof(struct fsg_bulk_cb_wrap));
895 char *cmdbuf = req->buf;
898 if (req->status || req->length == 0)
901 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
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;
914 printf("unknown command: %s\n", (char *)req->buf);
915 rockusb_tx_write_str("FAILunknown command");
917 if (req->actual < req->length) {
918 u8 *buf = (u8 *)req->buf;
920 buf[req->actual] = 0;
923 puts("buffer overflow\n");
924 rockusb_tx_write_str("FAILbuffer overflow");
930 usb_ep_queue(ep, req, 0);