Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

usb_msc.c 6.4KB

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