1 // SPDX-License-Identifier: GPL-2.0+
5 * Eddie Cai <eddie.cai.linux@gmail.com>
16 #include <linux/usb/ch9.h>
17 #include <linux/usb/gadget.h>
18 #include <linux/usb/composite.h>
19 #include <linux/compiler.h>
22 #include <asm/arch-rockchip/f_rockusb.h>
24 static inline struct f_rockusb *func_to_rockusb(struct usb_function *f)
26 return container_of(f, struct f_rockusb, usb_function);
29 static struct usb_endpoint_descriptor fs_ep_in = {
30 .bLength = USB_DT_ENDPOINT_SIZE,
31 .bDescriptorType = USB_DT_ENDPOINT,
32 .bEndpointAddress = USB_DIR_IN,
33 .bmAttributes = USB_ENDPOINT_XFER_BULK,
34 .wMaxPacketSize = cpu_to_le16(64),
37 static struct usb_endpoint_descriptor fs_ep_out = {
38 .bLength = USB_DT_ENDPOINT_SIZE,
39 .bDescriptorType = USB_DT_ENDPOINT,
40 .bEndpointAddress = USB_DIR_OUT,
41 .bmAttributes = USB_ENDPOINT_XFER_BULK,
42 .wMaxPacketSize = cpu_to_le16(64),
45 static struct usb_endpoint_descriptor hs_ep_in = {
46 .bLength = USB_DT_ENDPOINT_SIZE,
47 .bDescriptorType = USB_DT_ENDPOINT,
48 .bEndpointAddress = USB_DIR_IN,
49 .bmAttributes = USB_ENDPOINT_XFER_BULK,
50 .wMaxPacketSize = cpu_to_le16(512),
53 static struct usb_endpoint_descriptor hs_ep_out = {
54 .bLength = USB_DT_ENDPOINT_SIZE,
55 .bDescriptorType = USB_DT_ENDPOINT,
56 .bEndpointAddress = USB_DIR_OUT,
57 .bmAttributes = USB_ENDPOINT_XFER_BULK,
58 .wMaxPacketSize = cpu_to_le16(512),
61 static struct usb_interface_descriptor interface_desc = {
62 .bLength = USB_DT_INTERFACE_SIZE,
63 .bDescriptorType = USB_DT_INTERFACE,
64 .bInterfaceNumber = 0x00,
65 .bAlternateSetting = 0x00,
66 .bNumEndpoints = 0x02,
67 .bInterfaceClass = ROCKUSB_INTERFACE_CLASS,
68 .bInterfaceSubClass = ROCKUSB_INTERFACE_SUB_CLASS,
69 .bInterfaceProtocol = ROCKUSB_INTERFACE_PROTOCOL,
72 static struct usb_descriptor_header *rkusb_fs_function[] = {
73 (struct usb_descriptor_header *)&interface_desc,
74 (struct usb_descriptor_header *)&fs_ep_in,
75 (struct usb_descriptor_header *)&fs_ep_out,
78 static struct usb_descriptor_header *rkusb_hs_function[] = {
79 (struct usb_descriptor_header *)&interface_desc,
80 (struct usb_descriptor_header *)&hs_ep_in,
81 (struct usb_descriptor_header *)&hs_ep_out,
85 static const char rkusb_name[] = "Rockchip Rockusb";
87 static struct usb_string rkusb_string_defs[] = {
92 static struct usb_gadget_strings stringtab_rkusb = {
93 .language = 0x0409, /* en-us */
94 .strings = rkusb_string_defs,
97 static struct usb_gadget_strings *rkusb_strings[] = {
102 static struct f_rockusb *rockusb_func;
103 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
104 static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size);
106 struct f_rockusb *get_rkusb(void)
108 struct f_rockusb *f_rkusb = rockusb_func;
111 f_rkusb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_rkusb));
115 rockusb_func = f_rkusb;
116 memset(f_rkusb, 0, sizeof(*f_rkusb));
119 if (!f_rkusb->buf_head) {
120 f_rkusb->buf_head = memalign(CONFIG_SYS_CACHELINE_SIZE,
122 if (!f_rkusb->buf_head)
125 f_rkusb->buf = f_rkusb->buf_head;
126 memset(f_rkusb->buf_head, 0, RKUSB_BUF_SIZE);
131 static struct usb_endpoint_descriptor *rkusb_ep_desc(
132 struct usb_gadget *g,
133 struct usb_endpoint_descriptor *fs,
134 struct usb_endpoint_descriptor *hs)
136 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
141 static void rockusb_complete(struct usb_ep *ep, struct usb_request *req)
143 int status = req->status;
147 debug("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
150 /* config the rockusb device*/
151 static int rockusb_bind(struct usb_configuration *c, struct usb_function *f)
154 struct usb_gadget *gadget = c->cdev->gadget;
155 struct f_rockusb *f_rkusb = func_to_rockusb(f);
158 id = usb_interface_id(c, f);
161 interface_desc.bInterfaceNumber = id;
163 id = usb_string_id(c->cdev);
167 rkusb_string_defs[0].id = id;
168 interface_desc.iInterface = id;
170 f_rkusb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
173 f_rkusb->in_ep->driver_data = c->cdev;
175 f_rkusb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
176 if (!f_rkusb->out_ep)
178 f_rkusb->out_ep->driver_data = c->cdev;
180 f->descriptors = rkusb_fs_function;
182 if (gadget_is_dualspeed(gadget)) {
183 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
184 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
185 f->hs_descriptors = rkusb_hs_function;
188 s = env_get("serial#");
190 g_dnl_set_serialnumber((char *)s);
195 static void rockusb_unbind(struct usb_configuration *c, struct usb_function *f)
197 /* clear the configuration*/
198 memset(rockusb_func, 0, sizeof(*rockusb_func));
201 static void rockusb_disable(struct usb_function *f)
203 struct f_rockusb *f_rkusb = func_to_rockusb(f);
205 usb_ep_disable(f_rkusb->out_ep);
206 usb_ep_disable(f_rkusb->in_ep);
208 if (f_rkusb->out_req) {
209 free(f_rkusb->out_req->buf);
210 usb_ep_free_request(f_rkusb->out_ep, f_rkusb->out_req);
211 f_rkusb->out_req = NULL;
213 if (f_rkusb->in_req) {
214 free(f_rkusb->in_req->buf);
215 usb_ep_free_request(f_rkusb->in_ep, f_rkusb->in_req);
216 f_rkusb->in_req = NULL;
218 if (f_rkusb->buf_head) {
219 free(f_rkusb->buf_head);
220 f_rkusb->buf_head = NULL;
225 static struct usb_request *rockusb_start_ep(struct usb_ep *ep)
227 struct usb_request *req;
229 req = usb_ep_alloc_request(ep, 0);
233 req->length = EP_BUFFER_SIZE;
234 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
236 usb_ep_free_request(ep, req);
239 memset(req->buf, 0, req->length);
244 static int rockusb_set_alt(struct usb_function *f, unsigned int interface,
248 struct usb_composite_dev *cdev = f->config->cdev;
249 struct usb_gadget *gadget = cdev->gadget;
250 struct f_rockusb *f_rkusb = func_to_rockusb(f);
251 const struct usb_endpoint_descriptor *d;
253 debug("%s: func: %s intf: %d alt: %d\n",
254 __func__, f->name, interface, alt);
256 d = rkusb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
257 ret = usb_ep_enable(f_rkusb->out_ep, d);
259 printf("failed to enable out ep\n");
263 f_rkusb->out_req = rockusb_start_ep(f_rkusb->out_ep);
264 if (!f_rkusb->out_req) {
265 printf("failed to alloc out req\n");
269 f_rkusb->out_req->complete = rx_handler_command;
271 d = rkusb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
272 ret = usb_ep_enable(f_rkusb->in_ep, d);
274 printf("failed to enable in ep\n");
278 f_rkusb->in_req = rockusb_start_ep(f_rkusb->in_ep);
279 if (!f_rkusb->in_req) {
280 printf("failed alloc req in\n");
284 f_rkusb->in_req->complete = rockusb_complete;
286 ret = usb_ep_queue(f_rkusb->out_ep, f_rkusb->out_req, 0);
296 static int rockusb_add(struct usb_configuration *c)
298 struct f_rockusb *f_rkusb = get_rkusb();
301 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
303 f_rkusb->usb_function.name = "f_rockusb";
304 f_rkusb->usb_function.bind = rockusb_bind;
305 f_rkusb->usb_function.unbind = rockusb_unbind;
306 f_rkusb->usb_function.set_alt = rockusb_set_alt;
307 f_rkusb->usb_function.disable = rockusb_disable;
308 f_rkusb->usb_function.strings = rkusb_strings;
310 status = usb_add_function(c, &f_rkusb->usb_function);
313 rockusb_func = f_rkusb;
318 void rockusb_dev_init(char *dev_type, int dev_index)
320 struct f_rockusb *f_rkusb = get_rkusb();
322 f_rkusb->dev_type = dev_type;
323 f_rkusb->dev_index = dev_index;
326 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_rockusb, rockusb_add);
328 static int rockusb_tx_write(const char *buffer, unsigned int buffer_size)
330 struct usb_request *in_req = rockusb_func->in_req;
333 memcpy(in_req->buf, buffer, buffer_size);
334 in_req->length = buffer_size;
335 debug("Transferring 0x%x bytes\n", buffer_size);
336 usb_ep_dequeue(rockusb_func->in_ep, in_req);
337 ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0);
339 printf("Error %d on queue\n", ret);
343 static int rockusb_tx_write_str(const char *buffer)
345 return rockusb_tx_write(buffer, strlen(buffer));
349 static void printcbw(char *buf)
351 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
352 sizeof(struct fsg_bulk_cb_wrap));
354 memcpy((char *)cbw, buf, USB_BULK_CB_WRAP_LEN);
356 debug("cbw: signature:%x\n", cbw->signature);
357 debug("cbw: tag=%x\n", cbw->tag);
358 debug("cbw: data_transfer_length=%d\n", cbw->data_transfer_length);
359 debug("cbw: flags=%x\n", cbw->flags);
360 debug("cbw: lun=%d\n", cbw->lun);
361 debug("cbw: length=%d\n", cbw->length);
362 debug("cbw: ucOperCode=%x\n", cbw->CDB[0]);
363 debug("cbw: ucReserved=%x\n", cbw->CDB[1]);
364 debug("cbw: dwAddress:%x %x %x %x\n", cbw->CDB[5], cbw->CDB[4],
365 cbw->CDB[3], cbw->CDB[2]);
366 debug("cbw: ucReserved2=%x\n", cbw->CDB[6]);
367 debug("cbw: uslength:%x %x\n", cbw->CDB[8], cbw->CDB[7]);
370 static void printcsw(char *buf)
372 ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
373 sizeof(struct bulk_cs_wrap));
374 memcpy((char *)csw, buf, USB_BULK_CS_WRAP_LEN);
375 debug("csw: signature:%x\n", csw->signature);
376 debug("csw: tag:%x\n", csw->tag);
377 debug("csw: residue:%x\n", csw->residue);
378 debug("csw: status:%x\n", csw->status);
382 static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size)
384 ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
385 sizeof(struct bulk_cs_wrap));
386 csw->signature = cpu_to_le32(USB_BULK_CS_SIG);
388 csw->residue = cpu_to_be32(residue);
389 csw->status = status;
391 printcsw((char *)csw);
393 return rockusb_tx_write((char *)csw, size);
396 static void tx_handler_send_csw(struct usb_ep *ep, struct usb_request *req)
398 struct f_rockusb *f_rkusb = get_rkusb();
399 int status = req->status;
402 debug("status: %d ep '%s' trans: %d\n",
403 status, ep->name, req->actual);
405 /* Return back to default in_req complete function after sending CSW */
406 req->complete = rockusb_complete;
407 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD, USB_BULK_CS_WRAP_LEN);
410 static unsigned int rx_bytes_expected(struct usb_ep *ep)
412 struct f_rockusb *f_rkusb = get_rkusb();
413 int rx_remain = f_rkusb->dl_size - f_rkusb->dl_bytes;
415 unsigned int maxpacket = ep->maxpacket;
419 else if (rx_remain > EP_BUFFER_SIZE)
420 return EP_BUFFER_SIZE;
422 rem = rx_remain % maxpacket;
424 rx_remain = rx_remain + (maxpacket - rem);
429 /* usb_request complete call back to handle upload image */
430 static void tx_handler_ul_image(struct usb_ep *ep, struct usb_request *req)
432 ALLOC_CACHE_ALIGN_BUFFER(char, rbuffer, RKBLOCK_BUF_SIZE);
433 struct f_rockusb *f_rkusb = get_rkusb();
434 struct usb_request *in_req = rockusb_func->in_req;
437 /* Print error status of previous transfer */
439 debug("status: %d ep '%s' trans: %d len %d\n", req->status,
440 ep->name, req->actual, req->length);
442 /* On transfer complete reset in_req and feedback host with CSW_GOOD */
443 if (f_rkusb->ul_bytes >= f_rkusb->ul_size) {
445 in_req->complete = rockusb_complete;
447 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD,
448 USB_BULK_CS_WRAP_LEN);
452 /* Proceed with current chunk */
453 unsigned int transfer_size = f_rkusb->ul_size - f_rkusb->ul_bytes;
455 if (transfer_size > RKBLOCK_BUF_SIZE)
456 transfer_size = RKBLOCK_BUF_SIZE;
457 /* Read at least one block */
458 unsigned int blkcount = (transfer_size + f_rkusb->desc->blksz - 1) /
459 f_rkusb->desc->blksz;
461 debug("ul %x bytes, %x blks, read lba %x, ul_size:%x, ul_bytes:%x, ",
462 transfer_size, blkcount, f_rkusb->lba,
463 f_rkusb->ul_size, f_rkusb->ul_bytes);
465 int blks = blk_dread(f_rkusb->desc, f_rkusb->lba, blkcount, rbuffer);
467 if (blks != blkcount) {
468 printf("failed reading from device %s: %d\n",
469 f_rkusb->dev_type, f_rkusb->dev_index);
470 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
471 USB_BULK_CS_WRAP_LEN);
474 f_rkusb->lba += blkcount;
475 f_rkusb->ul_bytes += transfer_size;
477 /* Proceed with USB request */
478 memcpy(in_req->buf, rbuffer, transfer_size);
479 in_req->length = transfer_size;
480 in_req->complete = tx_handler_ul_image;
481 debug("Uploading 0x%x bytes\n", transfer_size);
482 usb_ep_dequeue(rockusb_func->in_ep, in_req);
483 ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0);
485 printf("Error %d on queue\n", ret);
488 /* usb_request complete call back to handle down load image */
489 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
491 struct f_rockusb *f_rkusb = get_rkusb();
492 unsigned int transfer_size = 0;
493 const unsigned char *buffer = req->buf;
494 unsigned int buffer_size = req->actual;
496 transfer_size = f_rkusb->dl_size - f_rkusb->dl_bytes;
498 if (req->status != 0) {
499 printf("Bad status: %d\n", req->status);
500 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
501 USB_BULK_CS_WRAP_LEN);
505 if (buffer_size < transfer_size)
506 transfer_size = buffer_size;
508 memcpy((void *)f_rkusb->buf, buffer, transfer_size);
509 f_rkusb->dl_bytes += transfer_size;
510 int blks = 0, blkcnt = transfer_size / f_rkusb->desc->blksz;
512 debug("dl %x bytes, %x blks, write lba %x, dl_size:%x, dl_bytes:%x, ",
513 transfer_size, blkcnt, f_rkusb->lba, f_rkusb->dl_size,
515 blks = blk_dwrite(f_rkusb->desc, f_rkusb->lba, blkcnt, f_rkusb->buf);
516 if (blks != blkcnt) {
517 printf("failed writing to device %s: %d\n", f_rkusb->dev_type,
519 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
520 USB_BULK_CS_WRAP_LEN);
523 f_rkusb->lba += blkcnt;
525 /* Check if transfer is done */
526 if (f_rkusb->dl_bytes >= f_rkusb->dl_size) {
527 req->complete = rx_handler_command;
528 req->length = EP_BUFFER_SIZE;
529 f_rkusb->buf = f_rkusb->buf_head;
530 debug("transfer 0x%x bytes done\n", f_rkusb->dl_size);
531 f_rkusb->dl_size = 0;
532 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD,
533 USB_BULK_CS_WRAP_LEN);
535 req->length = rx_bytes_expected(ep);
536 if (f_rkusb->buf == f_rkusb->buf_head)
537 f_rkusb->buf = f_rkusb->buf_head + EP_BUFFER_SIZE;
539 f_rkusb->buf = f_rkusb->buf_head;
541 debug("remain %x bytes, %lx sectors\n", req->length,
542 req->length / f_rkusb->desc->blksz);
546 usb_ep_queue(ep, req, 0);
549 static void cb_test_unit_ready(struct usb_ep *ep, struct usb_request *req)
551 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
552 sizeof(struct fsg_bulk_cb_wrap));
554 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
556 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
557 CSW_GOOD, USB_BULK_CS_WRAP_LEN);
560 static void cb_read_storage_id(struct usb_ep *ep, struct usb_request *req)
562 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
563 sizeof(struct fsg_bulk_cb_wrap));
564 struct f_rockusb *f_rkusb = get_rkusb();
565 char emmc_id[] = "EMMC ";
567 printf("read storage id\n");
568 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
570 /* Prepare for sending subsequent CSW_GOOD */
571 f_rkusb->tag = cbw->tag;
572 f_rkusb->in_req->complete = tx_handler_send_csw;
574 rockusb_tx_write_str(emmc_id);
577 int __weak rk_get_bootrom_chip_version(unsigned int *chip_info, int size)
582 static void cb_get_chip_version(struct usb_ep *ep, struct usb_request *req)
584 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
585 sizeof(struct fsg_bulk_cb_wrap));
586 struct f_rockusb *f_rkusb = get_rkusb();
587 unsigned int chip_info[4], i;
589 memset(chip_info, 0, sizeof(chip_info));
590 rk_get_bootrom_chip_version(chip_info, 4);
593 * Chip Version is a string saved in BOOTROM address space Little Endian
595 * Ex for rk3288: 0x33323041 0x32303134 0x30383133 0x56323030
596 * which brings: 320A20140813V200
598 * Note that memory version do invert MSB/LSB so printing the char
599 * buffer will show: A02341023180002V
601 printf("read chip version: ");
602 for (i = 0; i < 4; i++) {
604 (chip_info[i] >> 24) & 0xFF,
605 (chip_info[i] >> 16) & 0xFF,
606 (chip_info[i] >> 8) & 0xFF,
607 (chip_info[i] >> 0) & 0xFF);
610 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
612 /* Prepare for sending subsequent CSW_GOOD */
613 f_rkusb->tag = cbw->tag;
614 f_rkusb->in_req->complete = tx_handler_send_csw;
616 rockusb_tx_write((char *)chip_info, sizeof(chip_info));
619 static void cb_read_lba(struct usb_ep *ep, struct usb_request *req)
621 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
622 sizeof(struct fsg_bulk_cb_wrap));
623 struct f_rockusb *f_rkusb = get_rkusb();
626 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
627 sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
628 f_rkusb->tag = cbw->tag;
630 if (!f_rkusb->desc) {
631 char *type = f_rkusb->dev_type;
632 int index = f_rkusb->dev_index;
634 f_rkusb->desc = blk_get_dev(type, index);
635 if (!f_rkusb->desc ||
636 f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
637 printf("invalid device \"%s\", %d\n", type, index);
638 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
639 USB_BULK_CS_WRAP_LEN);
644 f_rkusb->lba = get_unaligned_be32(&cbw->CDB[2]);
645 f_rkusb->ul_size = sector_count * f_rkusb->desc->blksz;
646 f_rkusb->ul_bytes = 0;
648 debug("require read %x bytes, %x sectors from lba %x\n",
649 f_rkusb->ul_size, sector_count, f_rkusb->lba);
651 if (f_rkusb->ul_size == 0) {
652 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
653 CSW_FAIL, USB_BULK_CS_WRAP_LEN);
657 /* Start right now sending first chunk */
658 tx_handler_ul_image(ep, req);
661 static void cb_write_lba(struct usb_ep *ep, struct usb_request *req)
663 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
664 sizeof(struct fsg_bulk_cb_wrap));
665 struct f_rockusb *f_rkusb = get_rkusb();
668 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
669 sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
670 f_rkusb->tag = cbw->tag;
672 if (!f_rkusb->desc) {
673 char *type = f_rkusb->dev_type;
674 int index = f_rkusb->dev_index;
676 f_rkusb->desc = blk_get_dev(type, index);
677 if (!f_rkusb->desc ||
678 f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
679 printf("invalid device \"%s\", %d\n", type, index);
680 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
681 USB_BULK_CS_WRAP_LEN);
686 f_rkusb->lba = get_unaligned_be32(&cbw->CDB[2]);
687 f_rkusb->dl_size = sector_count * f_rkusb->desc->blksz;
688 f_rkusb->dl_bytes = 0;
690 debug("require write %x bytes, %x sectors to lba %x\n",
691 f_rkusb->dl_size, sector_count, f_rkusb->lba);
693 if (f_rkusb->dl_size == 0) {
694 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
695 CSW_FAIL, USB_BULK_CS_WRAP_LEN);
697 req->complete = rx_handler_dl_image;
698 req->length = rx_bytes_expected(ep);
702 static void cb_erase_lba(struct usb_ep *ep, struct usb_request *req)
704 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
705 sizeof(struct fsg_bulk_cb_wrap));
706 struct f_rockusb *f_rkusb = get_rkusb();
707 int sector_count, lba, blks;
709 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
710 sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
711 f_rkusb->tag = cbw->tag;
713 if (!f_rkusb->desc) {
714 char *type = f_rkusb->dev_type;
715 int index = f_rkusb->dev_index;
717 f_rkusb->desc = blk_get_dev(type, index);
718 if (!f_rkusb->desc ||
719 f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
720 printf("invalid device \"%s\", %d\n", type, index);
721 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
722 USB_BULK_CS_WRAP_LEN);
727 lba = get_unaligned_be32(&cbw->CDB[2]);
729 debug("require erase %x sectors from lba %x\n",
732 blks = blk_derase(f_rkusb->desc, lba, sector_count);
733 if (blks != sector_count) {
734 printf("failed erasing device %s: %d\n", f_rkusb->dev_type,
736 rockusb_tx_write_csw(f_rkusb->tag,
737 cbw->data_transfer_length, CSW_FAIL,
738 USB_BULK_CS_WRAP_LEN);
742 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
743 USB_BULK_CS_WRAP_LEN);
746 void __weak rkusb_set_reboot_flag(int flag)
748 struct f_rockusb *f_rkusb = get_rkusb();
750 printf("rockkusb set reboot flag: %d\n", f_rkusb->reboot_flag);
753 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
755 struct f_rockusb *f_rkusb = get_rkusb();
757 rkusb_set_reboot_flag(f_rkusb->reboot_flag);
758 do_reset(NULL, 0, 0, NULL);
761 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
763 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
764 sizeof(struct fsg_bulk_cb_wrap));
765 struct f_rockusb *f_rkusb = get_rkusb();
767 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
768 f_rkusb->reboot_flag = cbw->CDB[1];
769 rockusb_func->in_req->complete = compl_do_reset;
770 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
771 USB_BULK_CS_WRAP_LEN);
774 static void cb_not_support(struct usb_ep *ep, struct usb_request *req)
776 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
777 sizeof(struct fsg_bulk_cb_wrap));
779 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
780 printf("Rockusb command %x not support yet\n", cbw->CDB[0]);
781 rockusb_tx_write_csw(cbw->tag, 0, CSW_FAIL, USB_BULK_CS_WRAP_LEN);
784 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
786 .cmd = K_FW_TEST_UNIT_READY,
787 .cb = cb_test_unit_ready,
790 .cmd = K_FW_READ_FLASH_ID,
791 .cb = cb_read_storage_id,
794 .cmd = K_FW_SET_DEVICE_ID,
795 .cb = cb_not_support,
798 .cmd = K_FW_TEST_BAD_BLOCK,
799 .cb = cb_not_support,
803 .cb = cb_not_support,
806 .cmd = K_FW_WRITE_10,
807 .cb = cb_not_support,
810 .cmd = K_FW_ERASE_10,
811 .cb = cb_not_support,
814 .cmd = K_FW_WRITE_SPARE,
815 .cb = cb_not_support,
818 .cmd = K_FW_READ_SPARE,
819 .cb = cb_not_support,
822 .cmd = K_FW_ERASE_10_FORCE,
823 .cb = cb_not_support,
826 .cmd = K_FW_GET_VERSION,
827 .cb = cb_not_support,
830 .cmd = K_FW_LBA_READ_10,
834 .cmd = K_FW_LBA_WRITE_10,
838 .cmd = K_FW_ERASE_SYS_DISK,
839 .cb = cb_not_support,
842 .cmd = K_FW_SDRAM_READ_10,
843 .cb = cb_not_support,
846 .cmd = K_FW_SDRAM_WRITE_10,
847 .cb = cb_not_support,
850 .cmd = K_FW_SDRAM_EXECUTE,
851 .cb = cb_not_support,
854 .cmd = K_FW_READ_FLASH_INFO,
855 .cb = cb_not_support,
858 .cmd = K_FW_GET_CHIP_VER,
859 .cb = cb_get_chip_version,
862 .cmd = K_FW_LOW_FORMAT,
863 .cb = cb_not_support,
866 .cmd = K_FW_SET_RESET_FLAG,
867 .cb = cb_not_support,
870 .cmd = K_FW_SPI_READ_10,
871 .cb = cb_not_support,
874 .cmd = K_FW_SPI_WRITE_10,
875 .cb = cb_not_support,
878 .cmd = K_FW_LBA_ERASE_10,
883 .cb = cb_not_support,
891 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
893 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
895 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
896 sizeof(struct fsg_bulk_cb_wrap));
897 char *cmdbuf = req->buf;
900 if (req->status || req->length == 0)
903 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
908 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
909 if (cmd_dispatch_info[i].cmd == cbw->CDB[0]) {
910 func_cb = cmd_dispatch_info[i].cb;
916 printf("unknown command: %s\n", (char *)req->buf);
917 rockusb_tx_write_str("FAILunknown command");
919 if (req->actual < req->length) {
920 u8 *buf = (u8 *)req->buf;
922 buf[req->actual] = 0;
925 puts("buffer overflow\n");
926 rockusb_tx_write_str("FAILbuffer overflow");
932 usb_ep_queue(ep, req, 0);