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