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 <android_avb/avb_ops_user.h> 10 #include <android_avb/rk_avb_ops_user.h> 11 #include <asm/arch/boot_mode.h> 12 #include <asm/arch/chip_info.h> 13 #include <asm/arch/rk_atags.h> 14 #include <write_keybox.h> 15 #include <linux/mtd/mtd.h> 16 #include <optee_include/OpteeClientInterface.h> 17 18 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 19 #include <asm/arch/vendor.h> 20 #endif 21 #include <rockusb.h> 22 23 #define ROCKUSB_INTERFACE_CLASS 0xff 24 #define ROCKUSB_INTERFACE_SUB_CLASS 0x06 25 #define ROCKUSB_INTERFACE_PROTOCOL 0x05 26 27 #define ROCKCHIP_FLASH_BLOCK_SIZE 1024 28 #define ROCKCHIP_FLASH_PAGE_SIZE 4 29 30 static struct usb_interface_descriptor rkusb_intf_desc = { 31 .bLength = USB_DT_INTERFACE_SIZE, 32 .bDescriptorType = USB_DT_INTERFACE, 33 .bInterfaceNumber = 0x00, 34 .bAlternateSetting = 0x00, 35 .bNumEndpoints = 0x02, 36 .bInterfaceClass = ROCKUSB_INTERFACE_CLASS, 37 .bInterfaceSubClass = ROCKUSB_INTERFACE_SUB_CLASS, 38 .bInterfaceProtocol = ROCKUSB_INTERFACE_PROTOCOL, 39 }; 40 41 static struct usb_descriptor_header *rkusb_fs_function[] = { 42 (struct usb_descriptor_header *)&rkusb_intf_desc, 43 (struct usb_descriptor_header *)&fsg_fs_bulk_in_desc, 44 (struct usb_descriptor_header *)&fsg_fs_bulk_out_desc, 45 NULL, 46 }; 47 48 static struct usb_descriptor_header *rkusb_hs_function[] = { 49 (struct usb_descriptor_header *)&rkusb_intf_desc, 50 (struct usb_descriptor_header *)&fsg_hs_bulk_in_desc, 51 (struct usb_descriptor_header *)&fsg_hs_bulk_out_desc, 52 NULL, 53 }; 54 55 static struct usb_descriptor_header *rkusb_ss_function[] = { 56 (struct usb_descriptor_header *)&rkusb_intf_desc, 57 (struct usb_descriptor_header *)&fsg_ss_bulk_in_desc, 58 (struct usb_descriptor_header *)&fsg_ss_bulk_in_comp_desc, 59 (struct usb_descriptor_header *)&fsg_ss_bulk_out_desc, 60 (struct usb_descriptor_header *)&fsg_ss_bulk_out_comp_desc, 61 NULL, 62 }; 63 64 struct rk_flash_info { 65 u32 flash_size; 66 u16 block_size; 67 u8 page_size; 68 u8 ecc_bits; 69 u8 access_time; 70 u8 manufacturer; 71 u8 flash_mask; 72 } __packed; 73 74 static int rkusb_rst_code; /* The subcode in reset command (0xFF) */ 75 76 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) 77 { 78 if (IS_RKUSB_UMS_DNL(name)) { 79 /* Fix to Rockchip's VID and PID */ 80 dev->idVendor = __constant_cpu_to_le16(0x2207); 81 dev->idProduct = __constant_cpu_to_le16(CONFIG_ROCKUSB_G_DNL_PID); 82 83 /* Enumerate as a loader device */ 84 #if defined(CONFIG_SUPPORT_USBPLUG) 85 dev->bcdUSB = cpu_to_le16(0x0200); 86 #else 87 dev->bcdUSB = cpu_to_le16(0x0201); 88 #endif 89 } else if (!strncmp(name, "usb_dnl_fastboot", 16)) { 90 /* Fix to Google's VID and PID */ 91 dev->idVendor = __constant_cpu_to_le16(0x18d1); 92 dev->idProduct = __constant_cpu_to_le16(0xd00d); 93 } else if (!strncmp(name, "usb_dnl_dfu", 11)) { 94 /* Fix to Rockchip's VID and PID for DFU */ 95 dev->idVendor = cpu_to_le16(0x2207); 96 dev->idProduct = cpu_to_le16(0x0107); 97 } 98 99 return 0; 100 } 101 102 __maybe_unused 103 static inline void dump_cbw(struct fsg_bulk_cb_wrap *cbw) 104 { 105 assert(!cbw); 106 107 debug("%s:\n", __func__); 108 debug("Signature %x\n", cbw->Signature); 109 debug("Tag %x\n", cbw->Tag); 110 debug("DataTransferLength %x\n", cbw->DataTransferLength); 111 debug("Flags %x\n", cbw->Flags); 112 debug("LUN %x\n", cbw->Lun); 113 debug("Length %x\n", cbw->Length); 114 debug("OptionCode %x\n", cbw->CDB[0]); 115 debug("SubCode %x\n", cbw->CDB[1]); 116 debug("SectorAddr %x\n", get_unaligned_be32(&cbw->CDB[2])); 117 debug("BlkSectors %x\n\n", get_unaligned_be16(&cbw->CDB[7])); 118 } 119 120 static int rkusb_check_lun(struct fsg_common *common) 121 { 122 struct fsg_lun *curlun; 123 124 /* Check the LUN */ 125 if (common->lun >= 0 && common->lun < common->nluns) { 126 curlun = &common->luns[common->lun]; 127 if (common->cmnd[0] != SC_REQUEST_SENSE) { 128 curlun->sense_data = SS_NO_SENSE; 129 curlun->info_valid = 0; 130 } 131 } else { 132 curlun = NULL; 133 common->bad_lun_okay = 0; 134 135 /* 136 * INQUIRY and REQUEST SENSE commands are explicitly allowed 137 * to use unsupported LUNs; all others may not. 138 */ 139 if (common->cmnd[0] != SC_INQUIRY && 140 common->cmnd[0] != SC_REQUEST_SENSE) { 141 debug("unsupported LUN %d\n", common->lun); 142 return -EINVAL; 143 } 144 } 145 146 return 0; 147 } 148 149 static void __do_reset(struct usb_ep *ep, struct usb_request *req) 150 { 151 u32 boot_flag = BOOT_NORMAL; 152 153 if (rkusb_rst_code == 0x03) 154 boot_flag = BOOT_BROM_DOWNLOAD; 155 156 rkusb_rst_code = 0; /* restore to default */ 157 writel(boot_flag, (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG); 158 159 do_reset(NULL, 0, 0, NULL); 160 } 161 162 static int rkusb_do_reset(struct fsg_common *common, 163 struct fsg_buffhd *bh) 164 { 165 common->data_size_from_cmnd = common->cmnd[4]; 166 common->residue = 0; 167 bh->inreq->complete = __do_reset; 168 bh->state = BUF_STATE_EMPTY; 169 170 rkusb_rst_code = !common->cmnd[1] ? 0xff : common->cmnd[1]; 171 return 0; 172 } 173 174 static int rkusb_do_test_unit_ready(struct fsg_common *common, 175 struct fsg_buffhd *bh) 176 { 177 common->residue = 0x06 << 24; /* Max block xfer support from host */ 178 common->data_dir = DATA_DIR_NONE; 179 bh->state = BUF_STATE_EMPTY; 180 181 return 0; 182 } 183 184 static int rkusb_do_read_flash_id(struct fsg_common *common, 185 struct fsg_buffhd *bh) 186 { 187 u8 *buf = (u8 *)bh->buf; 188 u32 len = 5; 189 enum if_type type = ums[common->lun].block_dev.if_type; 190 191 if (type == IF_TYPE_MMC) 192 memcpy((void *)&buf[0], "EMMC ", 5); 193 else if (type == IF_TYPE_RKNAND) 194 memcpy((void *)&buf[0], "NAND ", 5); 195 else 196 memcpy((void *)&buf[0], "UNKN ", 5); /* unknown */ 197 198 /* Set data xfer size */ 199 common->residue = common->data_size_from_cmnd = len; 200 common->data_size = len; 201 202 return len; 203 } 204 205 static int rkusb_do_test_bad_block(struct fsg_common *common, 206 struct fsg_buffhd *bh) 207 { 208 u8 *buf = (u8 *)bh->buf; 209 u32 len = 64; 210 211 memset((void *)&buf[0], 0, len); 212 213 /* Set data xfer size */ 214 common->residue = common->data_size_from_cmnd = len; 215 common->data_size = len; 216 217 return len; 218 } 219 220 static int rkusb_do_read_flash_info(struct fsg_common *common, 221 struct fsg_buffhd *bh) 222 { 223 struct blk_desc *desc = &ums[common->lun].block_dev; 224 u8 *buf = (u8 *)bh->buf; 225 u32 len = sizeof(struct rk_flash_info); 226 struct rk_flash_info finfo = { 227 .block_size = ROCKCHIP_FLASH_BLOCK_SIZE, 228 .ecc_bits = 0, 229 .page_size = ROCKCHIP_FLASH_PAGE_SIZE, 230 .access_time = 40, 231 .manufacturer = 0, 232 .flash_mask = 0 233 }; 234 235 finfo.flash_size = (u32)desc->lba; 236 237 if (desc->if_type == IF_TYPE_MTD && 238 (desc->devnum == BLK_MTD_NAND || 239 desc->devnum == BLK_MTD_SPI_NAND)) { 240 struct mtd_info *mtd = (struct mtd_info *)desc->bdev->priv; 241 242 if (mtd) { 243 finfo.block_size = mtd->erasesize >> 9; 244 finfo.page_size = mtd->writesize >> 9; 245 } 246 } 247 248 if (desc->if_type == IF_TYPE_MTD && desc->devnum == BLK_MTD_SPI_NOR) { 249 /* RV1126/RK3308 mtd spinor keep the former upgrade mode */ 250 #if !defined(CONFIG_ROCKCHIP_RV1126) && !defined(CONFIG_ROCKCHIP_RK3308) 251 finfo.block_size = 0x100; /* Aligned to 128KB */ 252 #else 253 finfo.block_size = ROCKCHIP_FLASH_BLOCK_SIZE; 254 #endif 255 } 256 257 debug("Flash info: block_size= %x page_size= %x\n", finfo.block_size, 258 finfo.page_size); 259 260 if (finfo.flash_size) 261 finfo.flash_mask = 1; 262 263 memset((void *)&buf[0], 0, len); 264 memcpy((void *)&buf[0], (void *)&finfo, len); 265 266 /* Set data xfer size */ 267 common->residue = common->data_size_from_cmnd = len; 268 /* legacy upgrade_tool does not set correct transfer size */ 269 common->data_size = len; 270 271 return len; 272 } 273 274 static int rkusb_do_get_chip_info(struct fsg_common *common, 275 struct fsg_buffhd *bh) 276 { 277 u8 *buf = (u8 *)bh->buf; 278 u32 len = common->data_size; 279 u32 chip_info[4]; 280 281 memset((void *)chip_info, 0, sizeof(chip_info)); 282 rockchip_rockusb_get_chip_info(chip_info); 283 284 memset((void *)&buf[0], 0, len); 285 memcpy((void *)&buf[0], (void *)chip_info, len); 286 287 /* Set data xfer size */ 288 common->residue = common->data_size_from_cmnd = len; 289 290 return len; 291 } 292 293 static int rkusb_do_lba_erase(struct fsg_common *common, 294 struct fsg_buffhd *bh) 295 { 296 struct fsg_lun *curlun = &common->luns[common->lun]; 297 u32 lba, amount; 298 loff_t file_offset; 299 int rc; 300 301 lba = get_unaligned_be32(&common->cmnd[2]); 302 if (lba >= curlun->num_sectors) { 303 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 304 rc = -EINVAL; 305 goto out; 306 } 307 308 file_offset = ((loff_t) lba) << 9; 309 amount = get_unaligned_be16(&common->cmnd[7]) << 9; 310 if (unlikely(amount == 0)) { 311 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 312 rc = -EIO; 313 goto out; 314 } 315 316 /* Perform the erase */ 317 rc = ums[common->lun].erase_sector(&ums[common->lun], 318 file_offset / SECTOR_SIZE, 319 amount / SECTOR_SIZE); 320 if (!rc) { 321 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 322 rc = -EIO; 323 } 324 325 out: 326 common->data_dir = DATA_DIR_NONE; 327 bh->state = BUF_STATE_EMPTY; 328 329 return rc; 330 } 331 332 static int rkusb_do_erase_force(struct fsg_common *common, 333 struct fsg_buffhd *bh) 334 { 335 struct blk_desc *desc = &ums[common->lun].block_dev; 336 struct fsg_lun *curlun = &common->luns[common->lun]; 337 u16 block_size = ROCKCHIP_FLASH_BLOCK_SIZE; 338 u32 lba, amount; 339 loff_t file_offset; 340 int rc; 341 342 lba = get_unaligned_be32(&common->cmnd[2]); 343 if (lba >= curlun->num_sectors) { 344 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 345 rc = -EINVAL; 346 goto out; 347 } 348 349 if (desc->if_type == IF_TYPE_MTD && 350 (desc->devnum == BLK_MTD_NAND || 351 desc->devnum == BLK_MTD_SPI_NAND)) { 352 struct mtd_info *mtd = (struct mtd_info *)desc->bdev->priv; 353 354 if (mtd) 355 block_size = mtd->erasesize >> 9; 356 } 357 358 file_offset = ((loff_t)lba) * block_size; 359 amount = get_unaligned_be16(&common->cmnd[7]) * block_size; 360 361 debug("%s lba= %x, nsec= %x\n", __func__, lba, 362 (u32)get_unaligned_be16(&common->cmnd[7])); 363 364 if (unlikely(amount == 0)) { 365 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 366 rc = -EIO; 367 goto out; 368 } 369 370 /* Perform the erase */ 371 rc = ums[common->lun].erase_sector(&ums[common->lun], 372 file_offset, 373 amount); 374 if (!rc) { 375 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 376 rc = -EIO; 377 } 378 379 out: 380 common->data_dir = DATA_DIR_NONE; 381 bh->state = BUF_STATE_EMPTY; 382 383 return rc; 384 } 385 386 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 387 static int rkusb_do_vs_write(struct fsg_common *common) 388 { 389 struct fsg_lun *curlun = &common->luns[common->lun]; 390 u16 type = get_unaligned_be16(&common->cmnd[4]); 391 struct vendor_item *vhead; 392 struct fsg_buffhd *bh; 393 void *data; 394 int rc; 395 396 if (common->data_size >= (u32)65536) { 397 /* _MUST_ small than 64K */ 398 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 399 return -EINVAL; 400 } 401 402 common->residue = common->data_size; 403 common->usb_amount_left = common->data_size; 404 405 /* Carry out the file writes */ 406 if (unlikely(common->data_size == 0)) 407 return -EIO; /* No data to write */ 408 409 for (;;) { 410 if (common->usb_amount_left > 0) { 411 /* Wait for the next buffer to become available */ 412 bh = common->next_buffhd_to_fill; 413 if (bh->state != BUF_STATE_EMPTY) 414 goto wait; 415 416 /* Request the next buffer */ 417 common->usb_amount_left -= common->data_size; 418 bh->outreq->length = common->data_size; 419 bh->bulk_out_intended_length = common->data_size; 420 bh->outreq->short_not_ok = 1; 421 422 START_TRANSFER_OR(common, bulk_out, bh->outreq, 423 &bh->outreq_busy, &bh->state) 424 /* 425 * Don't know what to do if 426 * common->fsg is NULL 427 */ 428 return -EIO; 429 common->next_buffhd_to_fill = bh->next; 430 } else { 431 /* Then, wait for the data to become available */ 432 bh = common->next_buffhd_to_drain; 433 if (bh->state != BUF_STATE_FULL) 434 goto wait; 435 436 common->next_buffhd_to_drain = bh->next; 437 bh->state = BUF_STATE_EMPTY; 438 439 /* Did something go wrong with the transfer? */ 440 if (bh->outreq->status != 0) { 441 curlun->sense_data = SS_COMMUNICATION_FAILURE; 442 curlun->info_valid = 1; 443 break; 444 } 445 446 /* Perform the write */ 447 vhead = (struct vendor_item *)bh->buf; 448 data = bh->buf + sizeof(struct vendor_item); 449 450 if (!type) { 451 if (vhead->id == HDCP_14_HDMI_ID || 452 vhead->id == HDCP_14_HDMIRX_ID || 453 vhead->id == HDCP_14_DP_ID) { 454 rc = vendor_handle_hdcp(vhead); 455 if (rc < 0) { 456 curlun->sense_data = SS_WRITE_ERROR; 457 return -EIO; 458 } 459 } 460 461 /* Vendor storage */ 462 rc = vendor_storage_write(vhead->id, 463 (char __user *)data, 464 vhead->size); 465 if (rc < 0) { 466 curlun->sense_data = SS_WRITE_ERROR; 467 return -EIO; 468 } 469 } else if (type == 1) { 470 /* RPMB */ 471 rc = 472 write_keybox_to_secure_storage((u8 *)data, 473 vhead->size); 474 if (rc < 0) { 475 curlun->sense_data = SS_WRITE_ERROR; 476 return -EIO; 477 } 478 } else if (type == 2) { 479 /* security storage */ 480 #ifdef CONFIG_RK_AVB_LIBAVB_USER 481 debug("%s call rk_avb_write_perm_attr %d, %d\n", 482 __func__, vhead->id, vhead->size); 483 rc = rk_avb_write_perm_attr(vhead->id, 484 (char __user *)data, 485 vhead->size); 486 if (rc < 0) { 487 curlun->sense_data = SS_WRITE_ERROR; 488 return -EIO; 489 } 490 #else 491 printf("Please enable CONFIG_RK_AVB_LIBAVB_USER\n"); 492 #endif 493 } else if (type == 3) { 494 /* efuse or otp*/ 495 #ifdef CONFIG_OPTEE_CLIENT 496 if (memcmp(data, "TAEK", 4) == 0) { 497 if (vhead->size - 8 != 32) { 498 printf("check ta encryption key size fail!\n"); 499 curlun->sense_data = SS_WRITE_ERROR; 500 return -EIO; 501 } 502 if (trusty_write_ta_encryption_key((uint32_t *)(data + 8), 8) != 0) { 503 printf("trusty_write_ta_encryption_key error!"); 504 curlun->sense_data = SS_WRITE_ERROR; 505 return -EIO; 506 } 507 } else if (memcmp(data, "EHUK", 4) == 0) { 508 if (vhead->size - 8 != 32) { 509 printf("check oem huk size fail!\n"); 510 curlun->sense_data = SS_WRITE_ERROR; 511 return -EIO; 512 } 513 if (trusty_write_oem_huk((uint32_t *)(data + 8), 8) != 0) { 514 printf("trusty_write_oem_huk error!"); 515 curlun->sense_data = SS_WRITE_ERROR; 516 return -EIO; 517 } 518 } else { 519 printf("Unknown tag\n"); 520 curlun->sense_data = SS_WRITE_ERROR; 521 return -EIO; 522 } 523 #else 524 printf("Please enable CONFIG_OPTEE_CLIENT\n"); 525 #endif 526 } else { 527 return -EINVAL; 528 } 529 530 common->residue -= common->data_size; 531 532 /* Did the host decide to stop early? */ 533 if (bh->outreq->actual != bh->outreq->length) 534 common->short_packet_received = 1; 535 break; /* Command done */ 536 } 537 wait: 538 /* Wait for something to happen */ 539 rc = sleep_thread(common); 540 if (rc) 541 return rc; 542 } 543 544 return -EIO; /* No default reply */ 545 } 546 547 static int rkusb_do_vs_read(struct fsg_common *common) 548 { 549 struct fsg_lun *curlun = &common->luns[common->lun]; 550 u16 type = get_unaligned_be16(&common->cmnd[4]); 551 struct vendor_item *vhead; 552 struct fsg_buffhd *bh; 553 void *data; 554 int rc; 555 556 if (common->data_size >= (u32)65536) { 557 /* _MUST_ small than 64K */ 558 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 559 return -EINVAL; 560 } 561 562 common->residue = common->data_size; 563 common->usb_amount_left = common->data_size; 564 565 /* Carry out the file reads */ 566 if (unlikely(common->data_size == 0)) 567 return -EIO; /* No default reply */ 568 569 for (;;) { 570 /* Wait for the next buffer to become available */ 571 bh = common->next_buffhd_to_fill; 572 while (bh->state != BUF_STATE_EMPTY) { 573 rc = sleep_thread(common); 574 if (rc) 575 return rc; 576 } 577 578 memset(bh->buf, 0, FSG_BUFLEN); 579 vhead = (struct vendor_item *)bh->buf; 580 data = bh->buf + sizeof(struct vendor_item); 581 vhead->id = get_unaligned_be16(&common->cmnd[2]); 582 583 if (!type) { 584 /* Vendor storage */ 585 rc = vendor_storage_read(vhead->id, 586 (char __user *)data, 587 common->data_size); 588 if (!rc) { 589 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 590 return -EIO; 591 } 592 vhead->size = rc; 593 } else if (type == 1) { 594 /* RPMB */ 595 rc = 596 read_raw_data_from_secure_storage((u8 *)data, 597 common->data_size); 598 if (!rc) { 599 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 600 return -EIO; 601 } 602 vhead->size = rc; 603 } else if (type == 2) { 604 /* security storage */ 605 #ifdef CONFIG_RK_AVB_LIBAVB_USER 606 rc = rk_avb_read_perm_attr(vhead->id, 607 (char __user *)data, 608 vhead->size); 609 if (rc < 0) 610 return -EIO; 611 vhead->size = rc; 612 #else 613 printf("Please enable CONFIG_RK_AVB_LIBAVB_USER!\n"); 614 #endif 615 } else { 616 return -EINVAL; 617 } 618 619 common->residue -= common->data_size; 620 bh->inreq->length = common->data_size; 621 bh->state = BUF_STATE_FULL; 622 623 break; /* No more left to read */ 624 } 625 626 return -EIO; /* No default reply */ 627 } 628 #endif 629 630 static int rkusb_do_get_storage_info(struct fsg_common *common, 631 struct fsg_buffhd *bh) 632 { 633 enum if_type type = ums[common->lun].block_dev.if_type; 634 int devnum = ums[common->lun].block_dev.devnum; 635 u32 media = BOOT_TYPE_UNKNOWN; 636 u32 len = common->data_size; 637 u8 *buf = (u8 *)bh->buf; 638 639 if (len > 4) 640 len = 4; 641 642 switch (type) { 643 case IF_TYPE_MMC: 644 media = BOOT_TYPE_EMMC; 645 break; 646 647 case IF_TYPE_SD: 648 media = BOOT_TYPE_SD0; 649 break; 650 651 case IF_TYPE_MTD: 652 if (devnum == BLK_MTD_SPI_NAND) 653 media = BOOT_TYPE_MTD_BLK_SPI_NAND; 654 else if (devnum == BLK_MTD_NAND) 655 media = BOOT_TYPE_NAND; 656 else 657 media = BOOT_TYPE_MTD_BLK_SPI_NOR; 658 break; 659 660 case IF_TYPE_SCSI: 661 media = BOOT_TYPE_SATA; 662 break; 663 664 case IF_TYPE_RKNAND: 665 media = BOOT_TYPE_NAND; 666 break; 667 668 case IF_TYPE_NVME: 669 media = BOOT_TYPE_PCIE; 670 break; 671 672 default: 673 break; 674 } 675 676 memcpy((void *)&buf[0], (void *)&media, len); 677 common->residue = len; 678 common->data_size_from_cmnd = len; 679 680 return len; 681 } 682 683 static int rkusb_do_read_capacity(struct fsg_common *common, 684 struct fsg_buffhd *bh) 685 { 686 u8 *buf = (u8 *)bh->buf; 687 u32 len = common->data_size; 688 enum if_type type = ums[common->lun].block_dev.if_type; 689 int devnum = ums[common->lun].block_dev.devnum; 690 691 /* 692 * bit[0]: Direct LBA, 0: Disabled; 693 * bit[1]: Vendor Storage API, 0: Disabed (default); 694 * bit[2]: First 4M Access, 0: Disabled; 695 * bit[3]: Read LBA On, 0: Disabed (default); 696 * bit[4]: New Vendor Storage API, 0: Disabed; 697 * bit[5]: Read uart data from ram 698 * bit[6]: Read IDB config 699 * bit[7]: Read SecureMode 700 * bit[8]: New IDB feature 701 * bit[9]: Get storage media info 702 * bit[10:63}: Reserved. 703 */ 704 memset((void *)&buf[0], 0, len); 705 if (type == IF_TYPE_MMC || type == IF_TYPE_SD || type == IF_TYPE_NVME) 706 buf[0] = BIT(0) | BIT(2) | BIT(4); 707 else 708 buf[0] = BIT(0) | BIT(4); 709 710 if (type == IF_TYPE_MTD && 711 (devnum == BLK_MTD_NAND || 712 devnum == BLK_MTD_SPI_NAND)) 713 buf[0] |= (1 << 6); 714 715 #if !defined(CONFIG_ROCKCHIP_RV1126) && !defined(CONFIG_ROCKCHIP_RK3308) 716 if (type == IF_TYPE_MTD && devnum == BLK_MTD_SPI_NOR) 717 buf[0] |= (1 << 6); 718 #endif 719 720 #if defined(CONFIG_ROCKCHIP_NEW_IDB) 721 buf[1] = BIT(0); 722 #endif 723 buf[1] |= BIT(1); 724 725 /* Set data xfer size */ 726 common->residue = len; 727 common->data_size_from_cmnd = len; 728 729 return len; 730 } 731 732 static void rkusb_fixup_cbwcb(struct fsg_common *common, 733 struct fsg_buffhd *bh) 734 { 735 struct usb_request *req = bh->outreq; 736 struct fsg_bulk_cb_wrap *cbw = req->buf; 737 738 /* FIXME cbw.DataTransferLength was not set by Upgrade Tool */ 739 common->data_size = le32_to_cpu(cbw->DataTransferLength); 740 if (common->data_size == 0) { 741 common->data_size = 742 get_unaligned_be16(&common->cmnd[7]) << 9; 743 printf("Trasfer Length NOT set, please use new version tool\n"); 744 debug("%s %d, cmnd1 %x\n", __func__, 745 get_unaligned_be16(&common->cmnd[7]), 746 get_unaligned_be16(&common->cmnd[1])); 747 } 748 if (cbw->Flags & USB_BULK_IN_FLAG) 749 common->data_dir = DATA_DIR_TO_HOST; 750 else 751 common->data_dir = DATA_DIR_FROM_HOST; 752 753 /* Not support */ 754 common->cmnd[1] = 0; 755 } 756 757 static int rkusb_cmd_process(struct fsg_common *common, 758 struct fsg_buffhd *bh, int *reply) 759 { 760 struct usb_request *req = bh->outreq; 761 struct fsg_bulk_cb_wrap *cbw = req->buf; 762 int rc; 763 764 dump_cbw(cbw); 765 766 if (rkusb_check_lun(common)) { 767 *reply = -EINVAL; 768 return RKUSB_RC_ERROR; 769 } 770 771 switch (common->cmnd[0]) { 772 case RKUSB_TEST_UNIT_READY: 773 *reply = rkusb_do_test_unit_ready(common, bh); 774 rc = RKUSB_RC_FINISHED; 775 break; 776 777 case RKUSB_READ_FLASH_ID: 778 *reply = rkusb_do_read_flash_id(common, bh); 779 rc = RKUSB_RC_FINISHED; 780 break; 781 782 case RKUSB_TEST_BAD_BLOCK: 783 *reply = rkusb_do_test_bad_block(common, bh); 784 rc = RKUSB_RC_FINISHED; 785 break; 786 787 case RKUSB_ERASE_10_FORCE: 788 *reply = rkusb_do_erase_force(common, bh); 789 rc = RKUSB_RC_FINISHED; 790 break; 791 792 case RKUSB_LBA_READ_10: 793 rkusb_fixup_cbwcb(common, bh); 794 common->cmnd[0] = SC_READ_10; 795 common->cmnd[1] = 0; /* Not support */ 796 rc = RKUSB_RC_CONTINUE; 797 break; 798 799 case RKUSB_LBA_WRITE_10: 800 rkusb_fixup_cbwcb(common, bh); 801 common->cmnd[0] = SC_WRITE_10; 802 common->cmnd[1] = 0; /* Not support */ 803 rc = RKUSB_RC_CONTINUE; 804 break; 805 806 case RKUSB_READ_FLASH_INFO: 807 *reply = rkusb_do_read_flash_info(common, bh); 808 rc = RKUSB_RC_FINISHED; 809 break; 810 811 case RKUSB_GET_CHIP_VER: 812 *reply = rkusb_do_get_chip_info(common, bh); 813 rc = RKUSB_RC_FINISHED; 814 break; 815 816 case RKUSB_LBA_ERASE: 817 *reply = rkusb_do_lba_erase(common, bh); 818 rc = RKUSB_RC_FINISHED; 819 break; 820 821 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 822 case RKUSB_VS_WRITE: 823 *reply = rkusb_do_vs_write(common); 824 rc = RKUSB_RC_FINISHED; 825 break; 826 827 case RKUSB_VS_READ: 828 *reply = rkusb_do_vs_read(common); 829 rc = RKUSB_RC_FINISHED; 830 break; 831 #endif 832 case RKUSB_GET_STORAGE_MEDIA: 833 *reply = rkusb_do_get_storage_info(common, bh); 834 rc = RKUSB_RC_FINISHED; 835 break; 836 837 case RKUSB_READ_CAPACITY: 838 *reply = rkusb_do_read_capacity(common, bh); 839 rc = RKUSB_RC_FINISHED; 840 break; 841 842 case RKUSB_RESET: 843 *reply = rkusb_do_reset(common, bh); 844 rc = RKUSB_RC_FINISHED; 845 break; 846 847 case RKUSB_READ_10: 848 case RKUSB_WRITE_10: 849 printf("CMD Not support, pls use new version Tool\n"); 850 case RKUSB_SET_DEVICE_ID: 851 case RKUSB_ERASE_10: 852 case RKUSB_WRITE_SPARE: 853 case RKUSB_READ_SPARE: 854 case RKUSB_GET_VERSION: 855 case RKUSB_ERASE_SYS_DISK: 856 case RKUSB_SDRAM_READ_10: 857 case RKUSB_SDRAM_WRITE_10: 858 case RKUSB_SDRAM_EXECUTE: 859 case RKUSB_LOW_FORMAT: 860 case RKUSB_SET_RESET_FLAG: 861 case RKUSB_SPI_READ_10: 862 case RKUSB_SPI_WRITE_10: 863 case RKUSB_SESSION: 864 /* Fall through */ 865 default: 866 rc = RKUSB_RC_UNKNOWN_CMND; 867 break; 868 } 869 870 return rc; 871 } 872 873 DECLARE_GADGET_BIND_CALLBACK(rkusb_ums_dnl, fsg_add); 874