4 * Eddie Cai <eddie.cai.linux@gmail.com>
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/usb/ch9.h>
14 #include <linux/usb/gadget.h>
15 #include <linux/usb/composite.h>
16 #include <linux/compiler.h>
19 #include <asm/arch/f_rockusb.h>
21 static inline struct f_rockusb *func_to_rockusb(struct usb_function *f)
23 return container_of(f, struct f_rockusb, usb_function);
26 static struct usb_endpoint_descriptor fs_ep_in = {
27 .bLength = USB_DT_ENDPOINT_SIZE,
28 .bDescriptorType = USB_DT_ENDPOINT,
29 .bEndpointAddress = USB_DIR_IN,
30 .bmAttributes = USB_ENDPOINT_XFER_BULK,
31 .wMaxPacketSize = cpu_to_le16(64),
34 static struct usb_endpoint_descriptor fs_ep_out = {
35 .bLength = USB_DT_ENDPOINT_SIZE,
36 .bDescriptorType = USB_DT_ENDPOINT,
37 .bEndpointAddress = USB_DIR_OUT,
38 .bmAttributes = USB_ENDPOINT_XFER_BULK,
39 .wMaxPacketSize = cpu_to_le16(64),
42 static struct usb_endpoint_descriptor hs_ep_in = {
43 .bLength = USB_DT_ENDPOINT_SIZE,
44 .bDescriptorType = USB_DT_ENDPOINT,
45 .bEndpointAddress = USB_DIR_IN,
46 .bmAttributes = USB_ENDPOINT_XFER_BULK,
47 .wMaxPacketSize = cpu_to_le16(512),
50 static struct usb_endpoint_descriptor hs_ep_out = {
51 .bLength = USB_DT_ENDPOINT_SIZE,
52 .bDescriptorType = USB_DT_ENDPOINT,
53 .bEndpointAddress = USB_DIR_OUT,
54 .bmAttributes = USB_ENDPOINT_XFER_BULK,
55 .wMaxPacketSize = cpu_to_le16(512),
58 static struct usb_interface_descriptor interface_desc = {
59 .bLength = USB_DT_INTERFACE_SIZE,
60 .bDescriptorType = USB_DT_INTERFACE,
61 .bInterfaceNumber = 0x00,
62 .bAlternateSetting = 0x00,
63 .bNumEndpoints = 0x02,
64 .bInterfaceClass = ROCKUSB_INTERFACE_CLASS,
65 .bInterfaceSubClass = ROCKUSB_INTERFACE_SUB_CLASS,
66 .bInterfaceProtocol = ROCKUSB_INTERFACE_PROTOCOL,
69 static struct usb_descriptor_header *rkusb_fs_function[] = {
70 (struct usb_descriptor_header *)&interface_desc,
71 (struct usb_descriptor_header *)&fs_ep_in,
72 (struct usb_descriptor_header *)&fs_ep_out,
75 static struct usb_descriptor_header *rkusb_hs_function[] = {
76 (struct usb_descriptor_header *)&interface_desc,
77 (struct usb_descriptor_header *)&hs_ep_in,
78 (struct usb_descriptor_header *)&hs_ep_out,
82 static const char rkusb_name[] = "Rockchip Rockusb";
84 static struct usb_string rkusb_string_defs[] = {
89 static struct usb_gadget_strings stringtab_rkusb = {
90 .language = 0x0409, /* en-us */
91 .strings = rkusb_string_defs,
94 static struct usb_gadget_strings *rkusb_strings[] = {
99 static struct f_rockusb *rockusb_func;
100 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
101 static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size);
103 struct f_rockusb *get_rkusb(void)
105 struct f_rockusb *f_rkusb = rockusb_func;
108 f_rkusb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_rkusb));
112 rockusb_func = f_rkusb;
113 memset(f_rkusb, 0, sizeof(*f_rkusb));
116 if (!f_rkusb->buf_head) {
117 f_rkusb->buf_head = memalign(CONFIG_SYS_CACHELINE_SIZE,
119 if (!f_rkusb->buf_head)
122 f_rkusb->buf = f_rkusb->buf_head;
123 memset(f_rkusb->buf_head, 0, RKUSB_BUF_SIZE);
128 static struct usb_endpoint_descriptor *rkusb_ep_desc(
129 struct usb_gadget *g,
130 struct usb_endpoint_descriptor *fs,
131 struct usb_endpoint_descriptor *hs)
133 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
138 static void rockusb_complete(struct usb_ep *ep, struct usb_request *req)
140 int status = req->status;
144 debug("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
147 /* config the rockusb device*/
148 static int rockusb_bind(struct usb_configuration *c, struct usb_function *f)
151 struct usb_gadget *gadget = c->cdev->gadget;
152 struct f_rockusb *f_rkusb = func_to_rockusb(f);
155 id = usb_interface_id(c, f);
158 interface_desc.bInterfaceNumber = id;
160 id = usb_string_id(c->cdev);
164 rkusb_string_defs[0].id = id;
165 interface_desc.iInterface = id;
167 f_rkusb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
170 f_rkusb->in_ep->driver_data = c->cdev;
172 f_rkusb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
173 if (!f_rkusb->out_ep)
175 f_rkusb->out_ep->driver_data = c->cdev;
177 f->descriptors = rkusb_fs_function;
179 if (gadget_is_dualspeed(gadget)) {
180 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
181 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
182 f->hs_descriptors = rkusb_hs_function;
185 s = env_get("serial#");
187 g_dnl_set_serialnumber((char *)s);
192 static void rockusb_unbind(struct usb_configuration *c, struct usb_function *f)
194 /* clear the configuration*/
195 memset(rockusb_func, 0, sizeof(*rockusb_func));
198 static void rockusb_disable(struct usb_function *f)
200 struct f_rockusb *f_rkusb = func_to_rockusb(f);
202 usb_ep_disable(f_rkusb->out_ep);
203 usb_ep_disable(f_rkusb->in_ep);
205 if (f_rkusb->out_req) {
206 free(f_rkusb->out_req->buf);
207 usb_ep_free_request(f_rkusb->out_ep, f_rkusb->out_req);
208 f_rkusb->out_req = NULL;
210 if (f_rkusb->in_req) {
211 free(f_rkusb->in_req->buf);
212 usb_ep_free_request(f_rkusb->in_ep, f_rkusb->in_req);
213 f_rkusb->in_req = NULL;
215 if (f_rkusb->buf_head) {
216 free(f_rkusb->buf_head);
217 f_rkusb->buf_head = NULL;
222 static struct usb_request *rockusb_start_ep(struct usb_ep *ep)
224 struct usb_request *req;
226 req = usb_ep_alloc_request(ep, 0);
230 req->length = EP_BUFFER_SIZE;
231 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
233 usb_ep_free_request(ep, req);
236 memset(req->buf, 0, req->length);
241 static int rockusb_set_alt(struct usb_function *f, unsigned int interface,
245 struct usb_composite_dev *cdev = f->config->cdev;
246 struct usb_gadget *gadget = cdev->gadget;
247 struct f_rockusb *f_rkusb = func_to_rockusb(f);
248 const struct usb_endpoint_descriptor *d;
250 debug("%s: func: %s intf: %d alt: %d\n",
251 __func__, f->name, interface, alt);
253 d = rkusb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
254 ret = usb_ep_enable(f_rkusb->out_ep, d);
256 printf("failed to enable out ep\n");
260 f_rkusb->out_req = rockusb_start_ep(f_rkusb->out_ep);
261 if (!f_rkusb->out_req) {
262 printf("failed to alloc out req\n");
266 f_rkusb->out_req->complete = rx_handler_command;
268 d = rkusb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
269 ret = usb_ep_enable(f_rkusb->in_ep, d);
271 printf("failed to enable in ep\n");
275 f_rkusb->in_req = rockusb_start_ep(f_rkusb->in_ep);
276 if (!f_rkusb->in_req) {
277 printf("failed alloc req in\n");
281 f_rkusb->in_req->complete = rockusb_complete;
283 ret = usb_ep_queue(f_rkusb->out_ep, f_rkusb->out_req, 0);
293 static int rockusb_add(struct usb_configuration *c)
295 struct f_rockusb *f_rkusb = get_rkusb();
298 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
300 f_rkusb->usb_function.name = "f_rockusb";
301 f_rkusb->usb_function.bind = rockusb_bind;
302 f_rkusb->usb_function.unbind = rockusb_unbind;
303 f_rkusb->usb_function.set_alt = rockusb_set_alt;
304 f_rkusb->usb_function.disable = rockusb_disable;
305 f_rkusb->usb_function.strings = rkusb_strings;
307 status = usb_add_function(c, &f_rkusb->usb_function);
310 rockusb_func = f_rkusb;
315 void rockusb_dev_init(char *dev_type, int dev_index)
317 struct f_rockusb *f_rkusb = get_rkusb();
319 f_rkusb->dev_type = dev_type;
320 f_rkusb->dev_index = dev_index;
323 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_rockusb, rockusb_add);
325 static int rockusb_tx_write(const char *buffer, unsigned int buffer_size)
327 struct usb_request *in_req = rockusb_func->in_req;
330 memcpy(in_req->buf, buffer, buffer_size);
331 in_req->length = buffer_size;
332 usb_ep_dequeue(rockusb_func->in_ep, in_req);
333 ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0);
335 printf("Error %d on queue\n", ret);
339 static int rockusb_tx_write_str(const char *buffer)
341 return rockusb_tx_write(buffer, strlen(buffer));
345 static void printcbw(char *buf)
347 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
348 sizeof(struct fsg_bulk_cb_wrap));
350 memcpy((char *)cbw, buf, USB_BULK_CB_WRAP_LEN);
352 debug("cbw: signature:%x\n", cbw->signature);
353 debug("cbw: tag=%x\n", cbw->tag);
354 debug("cbw: data_transfer_length=%d\n", cbw->data_transfer_length);
355 debug("cbw: flags=%x\n", cbw->flags);
356 debug("cbw: lun=%d\n", cbw->lun);
357 debug("cbw: length=%d\n", cbw->length);
358 debug("cbw: ucOperCode=%x\n", cbw->CDB[0]);
359 debug("cbw: ucReserved=%x\n", cbw->CDB[1]);
360 debug("cbw: dwAddress:%x %x %x %x\n", cbw->CDB[5], cbw->CDB[4],
361 cbw->CDB[3], cbw->CDB[2]);
362 debug("cbw: ucReserved2=%x\n", cbw->CDB[6]);
363 debug("cbw: uslength:%x %x\n", cbw->CDB[8], cbw->CDB[7]);
366 static void printcsw(char *buf)
368 ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
369 sizeof(struct bulk_cs_wrap));
370 memcpy((char *)csw, buf, USB_BULK_CS_WRAP_LEN);
371 debug("csw: signature:%x\n", csw->signature);
372 debug("csw: tag:%x\n", csw->tag);
373 debug("csw: residue:%x\n", csw->residue);
374 debug("csw: status:%x\n", csw->status);
378 static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size)
380 ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
381 sizeof(struct bulk_cs_wrap));
382 csw->signature = cpu_to_le32(USB_BULK_CS_SIG);
384 csw->residue = cpu_to_be32(residue);
385 csw->status = status;
387 printcsw((char *)&csw);
389 return rockusb_tx_write((char *)csw, size);
392 static unsigned int rx_bytes_expected(struct usb_ep *ep)
394 struct f_rockusb *f_rkusb = get_rkusb();
395 int rx_remain = f_rkusb->dl_size - f_rkusb->dl_bytes;
397 unsigned int maxpacket = ep->maxpacket;
401 else if (rx_remain > EP_BUFFER_SIZE)
402 return EP_BUFFER_SIZE;
404 rem = rx_remain % maxpacket;
406 rx_remain = rx_remain + (maxpacket - rem);
411 /* usb_request complete call back to handle down load image */
412 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
414 struct f_rockusb *f_rkusb = get_rkusb();
415 unsigned int transfer_size = 0;
416 const unsigned char *buffer = req->buf;
417 unsigned int buffer_size = req->actual;
419 transfer_size = f_rkusb->dl_size - f_rkusb->dl_bytes;
420 if (!f_rkusb->desc) {
421 char *type = f_rkusb->dev_type;
422 int index = f_rkusb->dev_index;
424 f_rkusb->desc = blk_get_dev(type, index);
425 if (!f_rkusb->desc ||
426 f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
427 puts("invalid mmc device\n");
428 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
429 USB_BULK_CS_WRAP_LEN);
434 if (req->status != 0) {
435 printf("Bad status: %d\n", req->status);
436 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
437 USB_BULK_CS_WRAP_LEN);
441 if (buffer_size < transfer_size)
442 transfer_size = buffer_size;
444 memcpy((void *)f_rkusb->buf, buffer, transfer_size);
445 f_rkusb->dl_bytes += transfer_size;
446 int blks = 0, blkcnt = transfer_size / 512;
448 debug("dl %x bytes, %x blks, write lba %x, dl_size:%x, dl_bytes:%x, ",
449 transfer_size, blkcnt, f_rkusb->lba, f_rkusb->dl_size,
451 blks = blk_dwrite(f_rkusb->desc, f_rkusb->lba, blkcnt, f_rkusb->buf);
452 if (blks != blkcnt) {
453 printf("failed writing to device %s: %d\n", f_rkusb->dev_type,
455 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
456 USB_BULK_CS_WRAP_LEN);
459 f_rkusb->lba += blkcnt;
461 /* Check if transfer is done */
462 if (f_rkusb->dl_bytes >= f_rkusb->dl_size) {
463 req->complete = rx_handler_command;
464 req->length = EP_BUFFER_SIZE;
465 f_rkusb->buf = f_rkusb->buf_head;
466 printf("transfer 0x%x bytes done\n", f_rkusb->dl_size);
467 f_rkusb->dl_size = 0;
468 rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD,
469 USB_BULK_CS_WRAP_LEN);
471 req->length = rx_bytes_expected(ep);
472 if (f_rkusb->buf == f_rkusb->buf_head)
473 f_rkusb->buf = f_rkusb->buf_head + EP_BUFFER_SIZE;
475 f_rkusb->buf = f_rkusb->buf_head;
477 debug("remain %x bytes, %x sectors\n", req->length,
482 usb_ep_queue(ep, req, 0);
485 static void cb_test_unit_ready(struct usb_ep *ep, struct usb_request *req)
487 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
488 sizeof(struct fsg_bulk_cb_wrap));
490 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
492 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
493 CSW_GOOD, USB_BULK_CS_WRAP_LEN);
496 static void cb_read_storage_id(struct usb_ep *ep, struct usb_request *req)
498 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
499 sizeof(struct fsg_bulk_cb_wrap));
500 char emmc_id[] = "EMMC ";
502 printf("read storage id\n");
503 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
504 rockusb_tx_write_str(emmc_id);
505 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
506 USB_BULK_CS_WRAP_LEN);
509 static void cb_write_lba(struct usb_ep *ep, struct usb_request *req)
511 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
512 sizeof(struct fsg_bulk_cb_wrap));
513 struct f_rockusb *f_rkusb = get_rkusb();
516 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
517 sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
518 f_rkusb->lba = get_unaligned_be32(&cbw->CDB[2]);
519 f_rkusb->dl_size = sector_count * 512;
520 f_rkusb->dl_bytes = 0;
521 f_rkusb->tag = cbw->tag;
522 debug("require write %x bytes, %x sectors to lba %x\n",
523 f_rkusb->dl_size, sector_count, f_rkusb->lba);
525 if (f_rkusb->dl_size == 0) {
526 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
527 CSW_FAIL, USB_BULK_CS_WRAP_LEN);
529 req->complete = rx_handler_dl_image;
530 req->length = rx_bytes_expected(ep);
534 void __weak rkusb_set_reboot_flag(int flag)
536 struct f_rockusb *f_rkusb = get_rkusb();
538 printf("rockkusb set reboot flag: %d\n", f_rkusb->reboot_flag);
541 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
543 struct f_rockusb *f_rkusb = get_rkusb();
545 rkusb_set_reboot_flag(f_rkusb->reboot_flag);
546 do_reset(NULL, 0, 0, NULL);
549 static void cb_reboot(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));
553 struct f_rockusb *f_rkusb = get_rkusb();
555 f_rkusb->reboot_flag = 0;
556 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
557 f_rkusb->reboot_flag = cbw->CDB[1];
558 rockusb_func->in_req->complete = compl_do_reset;
559 rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
560 USB_BULK_CS_WRAP_LEN);
563 static void cb_not_support(struct usb_ep *ep, struct usb_request *req)
565 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
566 sizeof(struct fsg_bulk_cb_wrap));
568 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
569 printf("Rockusb command %x not support yet\n", cbw->CDB[0]);
570 rockusb_tx_write_csw(cbw->tag, 0, CSW_FAIL, USB_BULK_CS_WRAP_LEN);
573 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
575 .cmd = K_FW_TEST_UNIT_READY,
576 .cb = cb_test_unit_ready,
579 .cmd = K_FW_READ_FLASH_ID,
580 .cb = cb_read_storage_id,
583 .cmd = K_FW_SET_DEVICE_ID,
584 .cb = cb_not_support,
587 .cmd = K_FW_TEST_BAD_BLOCK,
588 .cb = cb_not_support,
592 .cb = cb_not_support,
595 .cmd = K_FW_WRITE_10,
596 .cb = cb_not_support,
599 .cmd = K_FW_ERASE_10,
600 .cb = cb_not_support,
603 .cmd = K_FW_WRITE_SPARE,
604 .cb = cb_not_support,
607 .cmd = K_FW_READ_SPARE,
608 .cb = cb_not_support,
611 .cmd = K_FW_ERASE_10_FORCE,
612 .cb = cb_not_support,
615 .cmd = K_FW_GET_VERSION,
616 .cb = cb_not_support,
619 .cmd = K_FW_LBA_READ_10,
620 .cb = cb_not_support,
623 .cmd = K_FW_LBA_WRITE_10,
627 .cmd = K_FW_ERASE_SYS_DISK,
628 .cb = cb_not_support,
631 .cmd = K_FW_SDRAM_READ_10,
632 .cb = cb_not_support,
635 .cmd = K_FW_SDRAM_WRITE_10,
636 .cb = cb_not_support,
639 .cmd = K_FW_SDRAM_EXECUTE,
640 .cb = cb_not_support,
643 .cmd = K_FW_READ_FLASH_INFO,
644 .cb = cb_not_support,
647 .cmd = K_FW_GET_CHIP_VER,
648 .cb = cb_not_support,
651 .cmd = K_FW_LOW_FORMAT,
652 .cb = cb_not_support,
655 .cmd = K_FW_SET_RESET_FLAG,
656 .cb = cb_not_support,
659 .cmd = K_FW_SPI_READ_10,
660 .cb = cb_not_support,
663 .cmd = K_FW_SPI_WRITE_10,
664 .cb = cb_not_support,
668 .cb = cb_not_support,
676 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
678 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
680 ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
681 sizeof(struct fsg_bulk_cb_wrap));
682 char *cmdbuf = req->buf;
685 if (req->status || req->length == 0)
688 memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
693 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
694 if (cmd_dispatch_info[i].cmd == cbw->CDB[0]) {
695 func_cb = cmd_dispatch_info[i].cb;
701 printf("unknown command: %s\n", (char *)req->buf);
702 rockusb_tx_write_str("FAILunknown command");
704 if (req->actual < req->length) {
705 u8 *buf = (u8 *)req->buf;
707 buf[req->actual] = 0;
710 puts("buffer overflow\n");
711 rockusb_tx_write_str("FAILbuffer overflow");
717 usb_ep_queue(ep, req, 0);