No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usb_msc.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. #include "bsp/board.h"
  26. #include "tusb.h"
  27. #include "config.h"
  28. #include "fat_disk.h"
  29. #include "log.h"
  30. static bool ejected = false;
  31. static bool medium_available = false;
  32. bool msc_is_medium_available(void) {
  33. return medium_available;
  34. }
  35. void msc_set_medium_available(bool state) {
  36. medium_available = state;
  37. if (state) {
  38. ejected = false;
  39. }
  40. }
  41. // Invoked when received SCSI_CMD_INQUIRY
  42. // Application fill vendor id, product id and revision
  43. // with string up to 8, 16, 4 characters respectively
  44. void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8],
  45. uint8_t product_id[16], uint8_t product_rev[4]) {
  46. (void) lun;
  47. const char vid[] = "xythobuz";
  48. const char pid[] = "Debug Storage";
  49. const char rev[] = "0.01";
  50. memcpy(vendor_id , vid, strlen(vid));
  51. memcpy(product_id , pid, strlen(pid));
  52. memcpy(product_rev, rev, strlen(rev));
  53. }
  54. // Invoked when received Test Unit Ready command.
  55. // return true allowing host to read/write this LUN e.g SD card inserted
  56. bool tud_msc_test_unit_ready_cb(uint8_t lun) {
  57. if (ejected || !medium_available) {
  58. // Additional Sense 3A-00 is NOT_FOUND
  59. tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
  60. return false;
  61. }
  62. return true;
  63. }
  64. // Invoked when received SCSI_CMD_READ_CAPACITY_10 and
  65. // SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
  66. // Application update block count and block size
  67. void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) {
  68. (void) lun;
  69. *block_count = DISK_BLOCK_COUNT;
  70. *block_size = DISK_BLOCK_SIZE;
  71. }
  72. // Invoked when received Start Stop Unit command
  73. // - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
  74. // - Start = 1 : active mode, if load_eject = 1 : load disk storage
  75. bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition,
  76. bool start, bool load_eject) {
  77. (void) lun;
  78. (void) power_condition;
  79. if (start) {
  80. // load disk storage
  81. debug("load disk storage %d", load_eject);
  82. } else {
  83. // unload disk storage
  84. debug("unload disk storage %d", load_eject);
  85. ejected = true;
  86. }
  87. return true;
  88. }
  89. // Callback invoked when received READ10 command.
  90. // Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
  91. int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba,
  92. uint32_t offset, void* buffer, uint32_t bufsize) {
  93. (void) lun;
  94. // out of ramdisk
  95. if (lba >= DISK_BLOCK_COUNT) {
  96. return -1;
  97. }
  98. // TODO better range checking and length calculation?
  99. uint8_t const* addr = fat_disk_get_sector(lba) + offset;
  100. memcpy(buffer, addr, bufsize);
  101. return (int32_t) bufsize;
  102. }
  103. bool tud_msc_is_writable_cb (uint8_t lun) {
  104. (void) lun;
  105. return true;
  106. }
  107. // Callback invoked when received WRITE10 command.
  108. // Process data in buffer to disk's storage and return number of written bytes
  109. int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba,
  110. uint32_t offset, uint8_t* buffer, uint32_t bufsize) {
  111. (void) lun;
  112. // out of ramdisk
  113. if (lba >= DISK_BLOCK_COUNT) {
  114. return -1;
  115. }
  116. // TODO better range checking and length calculation?
  117. uint8_t* addr = fat_disk_get_sector(lba) + offset;
  118. memcpy(addr, buffer, bufsize);
  119. return (int32_t) bufsize;
  120. }
  121. // Callback invoked when received an SCSI command not in built-in list below
  122. // - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
  123. // - READ10 and WRITE10 has their own callbacks and MUST not be handled here
  124. int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16],
  125. void* buffer, uint16_t bufsize) {
  126. void const* response = NULL;
  127. int32_t resplen = 0;
  128. // most scsi handled is input
  129. bool in_xfer = true;
  130. switch (scsi_cmd[0]) {
  131. default:
  132. // Set Sense = Invalid Command Operation
  133. tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
  134. // negative means error -> tinyusb could stall and/or response with failed status
  135. resplen = -1;
  136. break;
  137. }
  138. // return resplen must not larger than bufsize
  139. if (resplen > bufsize) {
  140. resplen = bufsize;
  141. }
  142. if (response && (resplen > 0)) {
  143. if (in_xfer) {
  144. memcpy(buffer, response, (size_t) resplen);
  145. } else {
  146. // SCSI output
  147. }
  148. }
  149. return (int32_t) resplen;
  150. }