msc_device_port.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-10-20 tfx2001 first version
  9. */
  10. #include <tusb.h>
  11. #include <stdint.h>
  12. #include <board.h>
  13. #include <rtdevice.h>
  14. static bool ejected = false;
  15. static rt_device_t flash_device;
  16. static struct rt_device_blk_geometry blk_geom;
  17. #ifdef __CC_ARM
  18. uint16_t __builtin_bswap16(uint16_t x)
  19. {
  20. return (x << 8) | (x >> 8);
  21. }
  22. #endif
  23. void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
  24. {
  25. (void) lun;
  26. const char vid[] = PKG_TINYUSB_DEVICE_MSC_VID;
  27. const char pid[] = PKG_TINYUSB_DEVICE_MSC_PID;
  28. const char rev[] = PKG_TINYUSB_DEVICE_MSC_REV;
  29. memcpy(vendor_id, vid, strlen(vid));
  30. memcpy(product_id, pid, strlen(pid));
  31. memcpy(product_rev, rev, strlen(rev));
  32. }
  33. bool tud_msc_test_unit_ready_cb(uint8_t lun)
  34. {
  35. (void) lun;
  36. if (ejected)
  37. {
  38. tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
  39. return false;
  40. }
  41. if (flash_device == NULL)
  42. {
  43. flash_device = rt_device_find(PKG_TINYUSB_DEVICE_MSC_NAME);
  44. }
  45. if (flash_device != NULL)
  46. {
  47. rt_device_open(flash_device, 0);
  48. rt_device_control(flash_device, RT_DEVICE_CTRL_BLK_GETGEOME, &blk_geom);
  49. return true;
  50. }
  51. return false;
  52. }
  53. void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size)
  54. {
  55. (void) lun;
  56. *block_count = blk_geom.sector_count;
  57. *block_size = blk_geom.bytes_per_sector;
  58. }
  59. bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
  60. {
  61. (void) lun;
  62. (void) power_condition;
  63. if (load_eject)
  64. {
  65. if (start)
  66. {
  67. ejected = false;
  68. } else {
  69. // unload disk storage
  70. ejected = true;
  71. }
  72. }
  73. return true;
  74. }
  75. int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize)
  76. {
  77. (void) lun;
  78. (void) offset;
  79. (void) bufsize;
  80. return (int32_t) rt_device_read(flash_device, (rt_off_t) lba, buffer, 1) * blk_geom.bytes_per_sector;
  81. }
  82. int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize)
  83. {
  84. (void) lun;
  85. (void) offset;
  86. (void) bufsize;
  87. return (int32_t) rt_device_write(flash_device, (rt_off_t) lba, buffer, 1) * blk_geom.bytes_per_sector;
  88. }
  89. int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize)
  90. {
  91. void const *response = NULL;
  92. uint16_t resplen = 0;
  93. // most scsi handled is input
  94. bool in_xfer = true;
  95. switch (scsi_cmd[0])
  96. {
  97. case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
  98. // Host is about to read/write etc ... better not to disconnect disk
  99. resplen = 0;
  100. break;
  101. default:
  102. // Set Sense = Invalid Command Operation
  103. tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
  104. // negative means error -> tinyusb could stall and/or response with failed status
  105. resplen = -1;
  106. break;
  107. }
  108. // return resplen must not larger than bufsize
  109. if (resplen > bufsize) resplen = bufsize;
  110. if (response && (resplen > 0))
  111. {
  112. if (in_xfer)
  113. {
  114. memcpy(buffer, response, resplen);
  115. } else {
  116. // SCSI output
  117. }
  118. }
  119. return resplen;
  120. }