1 /* 2 * Copyright 2017 Rockchip Electronics Co., Ltd 3 * Frank Wang <frank.wang@rock-chips.com> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <asm/io.h> 9 #include <asm/arch/boot_mode.h> 10 #include <asm/arch/chip_info.h> 11 12 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 13 #include <asm/arch/vendor.h> 14 #endif 15 16 #include <rockusb.h> 17 18 #define ROCKUSB_INTERFACE_CLASS 0xff 19 #define ROCKUSB_INTERFACE_SUB_CLASS 0x06 20 #define ROCKUSB_INTERFACE_PROTOCOL 0x05 21 22 static struct usb_interface_descriptor rkusb_intf_desc = { 23 .bLength = USB_DT_INTERFACE_SIZE, 24 .bDescriptorType = USB_DT_INTERFACE, 25 .bInterfaceNumber = 0x00, 26 .bAlternateSetting = 0x00, 27 .bNumEndpoints = 0x02, 28 .bInterfaceClass = ROCKUSB_INTERFACE_CLASS, 29 .bInterfaceSubClass = ROCKUSB_INTERFACE_SUB_CLASS, 30 .bInterfaceProtocol = ROCKUSB_INTERFACE_PROTOCOL, 31 }; 32 33 static struct usb_descriptor_header *rkusb_fs_function[] = { 34 (struct usb_descriptor_header *)&rkusb_intf_desc, 35 (struct usb_descriptor_header *)&fsg_fs_bulk_in_desc, 36 (struct usb_descriptor_header *)&fsg_fs_bulk_out_desc, 37 NULL, 38 }; 39 40 static struct usb_descriptor_header *rkusb_hs_function[] = { 41 (struct usb_descriptor_header *)&rkusb_intf_desc, 42 (struct usb_descriptor_header *)&fsg_hs_bulk_in_desc, 43 (struct usb_descriptor_header *)&fsg_hs_bulk_out_desc, 44 NULL, 45 }; 46 47 struct rk_flash_info { 48 u32 flash_size; 49 u16 block_size; 50 u8 page_size; 51 u8 ecc_bits; 52 u8 access_time; 53 u8 manufacturer; 54 u8 flash_mask; 55 } __packed; 56 57 static int rkusb_rst_code; /* The subcode in reset command (0xFF) */ 58 59 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) 60 { 61 if (IS_RKUSB_UMS_DNL(name)) { 62 /* Fix to Rockchip's VID and PID */ 63 dev->idVendor = __constant_cpu_to_le16(0x2207); 64 dev->idProduct = __constant_cpu_to_le16(CONFIG_ROCKUSB_G_DNL_PID); 65 66 /* Enumerate as a loader device */ 67 dev->bcdUSB = cpu_to_le16(0x0201); 68 } else if (!strncmp(name, "usb_dnl_fastboot", 16)) { 69 /* Fix to Google's VID and PID */ 70 dev->idVendor = __constant_cpu_to_le16(0x18d1); 71 dev->idProduct = __constant_cpu_to_le16(0xd00d); 72 } 73 74 return 0; 75 } 76 77 __maybe_unused 78 static inline void dump_cbw(struct fsg_bulk_cb_wrap *cbw) 79 { 80 assert(!cbw); 81 82 debug("%s:\n", __func__); 83 debug("Signature %x\n", cbw->Signature); 84 debug("Tag %x\n", cbw->Tag); 85 debug("DataTransferLength %x\n", cbw->DataTransferLength); 86 debug("Flags %x\n", cbw->Flags); 87 debug("LUN %x\n", cbw->Lun); 88 debug("Length %x\n", cbw->Length); 89 debug("OptionCode %x\n", cbw->CDB[0]); 90 debug("SubCode %x\n", cbw->CDB[1]); 91 debug("SectorAddr %x\n", get_unaligned_be32(&cbw->CDB[2])); 92 debug("BlkSectors %x\n\n", get_unaligned_be16(&cbw->CDB[7])); 93 } 94 95 static int rkusb_check_lun(struct fsg_common *common) 96 { 97 struct fsg_lun *curlun; 98 99 /* Check the LUN */ 100 if (common->lun >= 0 && common->lun < common->nluns) { 101 curlun = &common->luns[common->lun]; 102 if (common->cmnd[0] != SC_REQUEST_SENSE) { 103 curlun->sense_data = SS_NO_SENSE; 104 curlun->info_valid = 0; 105 } 106 } else { 107 curlun = NULL; 108 common->bad_lun_okay = 0; 109 110 /* 111 * INQUIRY and REQUEST SENSE commands are explicitly allowed 112 * to use unsupported LUNs; all others may not. 113 */ 114 if (common->cmnd[0] != SC_INQUIRY && 115 common->cmnd[0] != SC_REQUEST_SENSE) { 116 debug("unsupported LUN %d\n", common->lun); 117 return -EINVAL; 118 } 119 } 120 121 return 0; 122 } 123 124 static void __do_reset(struct usb_ep *ep, struct usb_request *req) 125 { 126 u32 boot_flag = BOOT_NORMAL; 127 128 if (rkusb_rst_code == 0x03) 129 boot_flag = BOOT_BROM_DOWNLOAD; 130 131 rkusb_rst_code = 0; /* restore to default */ 132 writel(boot_flag, (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG); 133 134 do_reset(NULL, 0, 0, NULL); 135 } 136 137 static int rkusb_do_reset(struct fsg_common *common, 138 struct fsg_buffhd *bh) 139 { 140 common->data_size_from_cmnd = common->cmnd[4]; 141 common->residue = 0; 142 bh->inreq->complete = __do_reset; 143 bh->state = BUF_STATE_EMPTY; 144 145 rkusb_rst_code = !common->cmnd[1] ? 0xff : common->cmnd[1]; 146 return 0; 147 } 148 149 static int rkusb_do_test_unit_ready(struct fsg_common *common, 150 struct fsg_buffhd *bh) 151 { 152 common->residue = 0x06 << 24; /* Max block xfer support from host */ 153 common->data_dir = DATA_DIR_NONE; 154 bh->state = BUF_STATE_EMPTY; 155 156 return 0; 157 } 158 159 static int rkusb_do_read_flash_id(struct fsg_common *common, 160 struct fsg_buffhd *bh) 161 { 162 u8 *buf = (u8 *)bh->buf; 163 u32 len = 5; 164 enum if_type type = ums[common->lun].block_dev.if_type; 165 166 if (type == IF_TYPE_MMC) 167 memcpy((void *)&buf[0], "EMMC ", 5); 168 else if (type == IF_TYPE_RKNAND) 169 memcpy((void *)&buf[0], "NAND ", 5); 170 else 171 memcpy((void *)&buf[0], "UNKN ", 5); /* unknown */ 172 173 /* Set data xfer size */ 174 common->residue = common->data_size_from_cmnd = len; 175 common->data_size = len; 176 177 return len; 178 } 179 180 static int rkusb_do_test_bad_block(struct fsg_common *common, 181 struct fsg_buffhd *bh) 182 { 183 u8 *buf = (u8 *)bh->buf; 184 u32 len = 64; 185 186 memset((void *)&buf[0], 0, len); 187 188 /* Set data xfer size */ 189 common->residue = common->data_size_from_cmnd = len; 190 common->data_size = len; 191 192 return len; 193 } 194 195 static int rkusb_do_read_flash_info(struct fsg_common *common, 196 struct fsg_buffhd *bh) 197 { 198 u8 *buf = (u8 *)bh->buf; 199 u32 len = sizeof(struct rk_flash_info); 200 struct rk_flash_info finfo = { 201 .block_size = 1024, 202 .ecc_bits = 0, 203 .page_size = 4, 204 .access_time = 40, 205 .manufacturer = 0, 206 .flash_mask = 0 207 }; 208 209 finfo.flash_size = (u32)ums[common->lun].block_dev.lba; 210 if (finfo.flash_size) 211 finfo.flash_mask = 1; 212 213 memset((void *)&buf[0], 0, len); 214 memcpy((void *)&buf[0], (void *)&finfo, len); 215 216 /* Set data xfer size */ 217 common->residue = common->data_size_from_cmnd = len; 218 /* legacy upgrade_tool does not set correct transfer size */ 219 common->data_size = len; 220 221 return len; 222 } 223 224 static int rkusb_do_get_chip_info(struct fsg_common *common, 225 struct fsg_buffhd *bh) 226 { 227 u8 *buf = (u8 *)bh->buf; 228 u32 len = common->data_size; 229 u32 chip_info[4]; 230 231 memset((void *)chip_info, 0, sizeof(chip_info)); 232 rockchip_rockusb_get_chip_info(chip_info); 233 234 memset((void *)&buf[0], 0, len); 235 memcpy((void *)&buf[0], (void *)chip_info, len); 236 237 /* Set data xfer size */ 238 common->residue = common->data_size_from_cmnd = len; 239 240 return len; 241 } 242 243 static int rkusb_do_lba_erase(struct fsg_common *common, 244 struct fsg_buffhd *bh) 245 { 246 struct fsg_lun *curlun = &common->luns[common->lun]; 247 u32 lba, amount; 248 loff_t file_offset; 249 int rc; 250 251 lba = get_unaligned_be32(&common->cmnd[2]); 252 if (lba >= curlun->num_sectors) { 253 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 254 rc = -EINVAL; 255 goto out; 256 } 257 258 file_offset = ((loff_t) lba) << 9; 259 amount = get_unaligned_be16(&common->cmnd[7]) << 9; 260 if (unlikely(amount == 0)) { 261 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 262 rc = -EIO; 263 goto out; 264 } 265 266 /* Perform the erase */ 267 rc = ums[common->lun].erase_sector(&ums[common->lun], 268 file_offset / SECTOR_SIZE, 269 amount / SECTOR_SIZE); 270 if (!rc) { 271 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 272 rc = -EIO; 273 } 274 275 out: 276 common->data_dir = DATA_DIR_NONE; 277 bh->state = BUF_STATE_EMPTY; 278 279 return rc; 280 } 281 282 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 283 static int rkusb_do_vs_write(struct fsg_common *common) 284 { 285 struct fsg_lun *curlun = &common->luns[common->lun]; 286 u16 type = get_unaligned_be16(&common->cmnd[4]); 287 struct vendor_item *vhead; 288 struct fsg_buffhd *bh; 289 void *data; 290 int rc; 291 292 if (common->data_size >= (u32)65536) { 293 /* _MUST_ small than 64K */ 294 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 295 return -EINVAL; 296 } 297 298 common->residue = common->data_size; 299 common->usb_amount_left = common->data_size; 300 301 /* Carry out the file writes */ 302 if (unlikely(common->data_size == 0)) 303 return -EIO; /* No data to write */ 304 305 for (;;) { 306 if (common->usb_amount_left > 0) { 307 /* Wait for the next buffer to become available */ 308 bh = common->next_buffhd_to_fill; 309 if (bh->state != BUF_STATE_EMPTY) 310 goto wait; 311 312 /* Request the next buffer */ 313 common->usb_amount_left -= common->data_size; 314 bh->outreq->length = common->data_size; 315 bh->bulk_out_intended_length = common->data_size; 316 bh->outreq->short_not_ok = 1; 317 318 START_TRANSFER_OR(common, bulk_out, bh->outreq, 319 &bh->outreq_busy, &bh->state) 320 /* 321 * Don't know what to do if 322 * common->fsg is NULL 323 */ 324 return -EIO; 325 common->next_buffhd_to_fill = bh->next; 326 } else { 327 /* Then, wait for the data to become available */ 328 bh = common->next_buffhd_to_drain; 329 if (bh->state != BUF_STATE_FULL) 330 goto wait; 331 332 common->next_buffhd_to_drain = bh->next; 333 bh->state = BUF_STATE_EMPTY; 334 335 /* Did something go wrong with the transfer? */ 336 if (bh->outreq->status != 0) { 337 curlun->sense_data = SS_COMMUNICATION_FAILURE; 338 curlun->info_valid = 1; 339 break; 340 } 341 342 /* Perform the write */ 343 vhead = (struct vendor_item *)bh->buf; 344 data = bh->buf + sizeof(struct vendor_item); 345 346 if (!type) { 347 /* Vendor storage */ 348 rc = vendor_storage_write(vhead->id, 349 (char __user *)data, 350 vhead->size); 351 if (rc < 0) 352 return -EIO; 353 } else { 354 /* RPMB */ 355 } 356 357 common->residue -= common->data_size; 358 359 /* Did the host decide to stop early? */ 360 if (bh->outreq->actual != bh->outreq->length) 361 common->short_packet_received = 1; 362 break; /* Command done */ 363 } 364 wait: 365 /* Wait for something to happen */ 366 rc = sleep_thread(common); 367 if (rc) 368 return rc; 369 } 370 371 return -EIO; /* No default reply */ 372 } 373 374 static int rkusb_do_vs_read(struct fsg_common *common) 375 { 376 struct fsg_lun *curlun = &common->luns[common->lun]; 377 u16 type = get_unaligned_be16(&common->cmnd[4]); 378 struct vendor_item *vhead; 379 struct fsg_buffhd *bh; 380 void *data; 381 int rc; 382 383 if (common->data_size >= (u32)65536) { 384 /* _MUST_ small than 64K */ 385 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 386 return -EINVAL; 387 } 388 389 common->residue = common->data_size; 390 common->usb_amount_left = common->data_size; 391 392 /* Carry out the file reads */ 393 if (unlikely(common->data_size == 0)) 394 return -EIO; /* No default reply */ 395 396 for (;;) { 397 /* Wait for the next buffer to become available */ 398 bh = common->next_buffhd_to_fill; 399 while (bh->state != BUF_STATE_EMPTY) { 400 rc = sleep_thread(common); 401 if (rc) 402 return rc; 403 } 404 405 memset(bh->buf, 0, FSG_BUFLEN); 406 vhead = (struct vendor_item *)bh->buf; 407 data = bh->buf + sizeof(struct vendor_item); 408 vhead->id = get_unaligned_be16(&common->cmnd[2]); 409 410 if (!type) { 411 /* Vendor storage */ 412 rc = vendor_storage_read(vhead->id, 413 (char __user *)data, 414 common->data_size); 415 if (!rc) 416 return -EIO; 417 vhead->size = rc; 418 } else { 419 /* RPMB */ 420 } 421 422 common->residue -= common->data_size; 423 bh->inreq->length = common->data_size; 424 bh->state = BUF_STATE_FULL; 425 426 break; /* No more left to read */ 427 } 428 429 return -EIO; /* No default reply */ 430 } 431 #endif 432 433 static int rkusb_do_read_capacity(struct fsg_common *common, 434 struct fsg_buffhd *bh) 435 { 436 u8 *buf = (u8 *)bh->buf; 437 u32 len = common->data_size; 438 enum if_type type = ums[common->lun].block_dev.if_type; 439 440 /* 441 * bit[0]: Direct LBA, 0: Disabled; 442 * bit[1]: Vendor Storage API, 0: Disabed (default); 443 * bit[2]: First 4M Access, 0: Disabled; 444 * bit[3]: Read LBA On, 0: Disabed (default); 445 * bit[4]: New Vendor Storage API, 0: Disabed; 446 * bit[5:63}: Reserved. 447 */ 448 memset((void *)&buf[0], 0, len); 449 if (type == IF_TYPE_MMC) 450 buf[0] = BIT(0) | BIT(2) | BIT(4); 451 else 452 buf[0] = BIT(0) | BIT(4); 453 454 /* Set data xfer size */ 455 common->residue = common->data_size_from_cmnd = len; 456 457 return len; 458 } 459 460 static void rkusb_fixup_cbwcb(struct fsg_common *common, 461 struct fsg_buffhd *bh) 462 { 463 struct usb_request *req = bh->outreq; 464 struct fsg_bulk_cb_wrap *cbw = req->buf; 465 466 /* FIXME cbw.DataTransferLength was not set by Upgrade Tool */ 467 common->data_size = le32_to_cpu(cbw->DataTransferLength); 468 if (common->data_size == 0) { 469 common->data_size = 470 get_unaligned_be16(&common->cmnd[7]) << 9; 471 printf("Trasfer Length NOT set, please use new version tool\n"); 472 debug("%s %d, cmnd1 %x\n", __func__, 473 get_unaligned_be16(&common->cmnd[7]), 474 get_unaligned_be16(&common->cmnd[1])); 475 } 476 if (cbw->Flags & USB_BULK_IN_FLAG) 477 common->data_dir = DATA_DIR_TO_HOST; 478 else 479 common->data_dir = DATA_DIR_FROM_HOST; 480 481 /* Not support */ 482 common->cmnd[1] = 0; 483 } 484 485 static int rkusb_cmd_process(struct fsg_common *common, 486 struct fsg_buffhd *bh, int *reply) 487 { 488 struct usb_request *req = bh->outreq; 489 struct fsg_bulk_cb_wrap *cbw = req->buf; 490 int rc; 491 492 dump_cbw(cbw); 493 494 if (rkusb_check_lun(common)) { 495 *reply = -EINVAL; 496 return RKUSB_RC_ERROR; 497 } 498 499 switch (common->cmnd[0]) { 500 case RKUSB_TEST_UNIT_READY: 501 *reply = rkusb_do_test_unit_ready(common, bh); 502 rc = RKUSB_RC_FINISHED; 503 break; 504 505 case RKUSB_READ_FLASH_ID: 506 *reply = rkusb_do_read_flash_id(common, bh); 507 rc = RKUSB_RC_FINISHED; 508 break; 509 510 case RKUSB_TEST_BAD_BLOCK: 511 *reply = rkusb_do_test_bad_block(common, bh); 512 rc = RKUSB_RC_FINISHED; 513 break; 514 515 case RKUSB_LBA_READ_10: 516 rkusb_fixup_cbwcb(common, bh); 517 common->cmnd[0] = SC_READ_10; 518 common->cmnd[1] = 0; /* Not support */ 519 rc = RKUSB_RC_CONTINUE; 520 break; 521 522 case RKUSB_LBA_WRITE_10: 523 rkusb_fixup_cbwcb(common, bh); 524 common->cmnd[0] = SC_WRITE_10; 525 common->cmnd[1] = 0; /* Not support */ 526 rc = RKUSB_RC_CONTINUE; 527 break; 528 529 case RKUSB_READ_FLASH_INFO: 530 *reply = rkusb_do_read_flash_info(common, bh); 531 rc = RKUSB_RC_FINISHED; 532 break; 533 534 case RKUSB_GET_CHIP_VER: 535 *reply = rkusb_do_get_chip_info(common, bh); 536 rc = RKUSB_RC_FINISHED; 537 break; 538 539 case RKUSB_LBA_ERASE: 540 *reply = rkusb_do_lba_erase(common, bh); 541 rc = RKUSB_RC_FINISHED; 542 break; 543 544 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 545 case RKUSB_VS_WRITE: 546 *reply = rkusb_do_vs_write(common); 547 rc = RKUSB_RC_FINISHED; 548 break; 549 550 case RKUSB_VS_READ: 551 *reply = rkusb_do_vs_read(common); 552 rc = RKUSB_RC_FINISHED; 553 break; 554 #endif 555 556 case RKUSB_READ_CAPACITY: 557 *reply = rkusb_do_read_capacity(common, bh); 558 rc = RKUSB_RC_FINISHED; 559 break; 560 561 case RKUSB_RESET: 562 *reply = rkusb_do_reset(common, bh); 563 rc = RKUSB_RC_FINISHED; 564 break; 565 566 case RKUSB_READ_10: 567 case RKUSB_WRITE_10: 568 printf("CMD Not support, pls use new version Tool\n"); 569 case RKUSB_SET_DEVICE_ID: 570 case RKUSB_ERASE_10: 571 case RKUSB_WRITE_SPARE: 572 case RKUSB_READ_SPARE: 573 case RKUSB_ERASE_10_FORCE: 574 case RKUSB_GET_VERSION: 575 case RKUSB_ERASE_SYS_DISK: 576 case RKUSB_SDRAM_READ_10: 577 case RKUSB_SDRAM_WRITE_10: 578 case RKUSB_SDRAM_EXECUTE: 579 case RKUSB_LOW_FORMAT: 580 case RKUSB_SET_RESET_FLAG: 581 case RKUSB_SPI_READ_10: 582 case RKUSB_SPI_WRITE_10: 583 case RKUSB_SESSION: 584 /* Fall through */ 585 default: 586 rc = RKUSB_RC_UNKNOWN_CMND; 587 break; 588 } 589 590 return rc; 591 } 592 593 DECLARE_GADGET_BIND_CALLBACK(rkusb_ums_dnl, fsg_add); 594