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