ZuluSCSI_platform_msc.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * Copyright (c) 2023-2024 zigzagjoe
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. /* platform specific MSC routines */
  22. #ifdef PLATFORM_MASS_STORAGE
  23. #include <SdFat.h>
  24. #include <device/usbd.h>
  25. #include <hardware/gpio.h>
  26. #include "ZuluSCSI_platform.h"
  27. #include "ZuluSCSI_log.h"
  28. #include "ZuluSCSI_msc.h"
  29. #include "ZuluSCSI_config.h"
  30. #include "ZuluSCSI_settings.h"
  31. #include <class/msc/msc.h>
  32. #include <class/msc/msc_device.h>
  33. #if CFG_TUD_MSC_EP_BUFSIZE < SD_SECTOR_SIZE
  34. #error "CFG_TUD_MSC_EP_BUFSIZE is too small! It needs to be at least 512 (SD_SECTOR_SIZE)"
  35. #endif
  36. // external global SD variable
  37. extern SdFs SD;
  38. static bool unitReady = false;
  39. /* return true if USB presence detected / eligble to enter CR mode */
  40. bool platform_sense_msc() {
  41. #ifdef ZULUSCSI_PICO
  42. // check if we're USB powered, if not, exit immediately
  43. // pin on the wireless module, see https://github.com/earlephilhower/arduino-pico/discussions/835
  44. if (rp2040.isPicoW() && !digitalRead(34))
  45. return false;
  46. if (!rp2040.isPicoW() && !digitalRead(24))
  47. return false;
  48. #endif
  49. logmsg("Waiting for USB enumeration to enter Card Reader mode.");
  50. // wait for up to a second to be enumerated
  51. uint32_t start = millis();
  52. bool timed_out = false;
  53. uint16_t usb_timeout = g_scsi_settings.getSystem()->usbMassStorageWaitPeriod;
  54. while (!tud_connected())
  55. {
  56. if ((uint32_t)(millis() - start) > usb_timeout)
  57. {
  58. logmsg("Waiting for USB enumeration timed out after ", usb_timeout, "ms.");
  59. logmsg("-- Try increasing 'USBMassStorageWaitPeriod' in the ", CONFIGFILE);
  60. timed_out = true;
  61. break;
  62. }
  63. delay(100);
  64. }
  65. if (!timed_out)
  66. dbgmsg("USB enumeration took ", (int)((uint32_t)(millis() - start)), "ms");
  67. // tud_connected returns True if just got out of Bus Reset and received the very first data from host
  68. // https://github.com/hathach/tinyusb/blob/master/src/device/usbd.h#L63
  69. return tud_connected();
  70. }
  71. /* return true if we should remain in card reader mode and perform periodic tasks */
  72. bool platform_run_msc() {
  73. return unitReady;
  74. }
  75. /* perform MSC class preinit tasks */
  76. void platform_enter_msc() {
  77. dbgmsg("USB MSC buffer size: ", CFG_TUD_MSC_EP_BUFSIZE);
  78. // MSC is ready for read/write
  79. // we don't need any prep, but the var is requried as the MSC callbacks are always active
  80. unitReady = true;
  81. }
  82. /* perform any cleanup tasks for the MSC-specific functionality */
  83. void platform_exit_msc() {
  84. unitReady = false;
  85. }
  86. /* TinyUSB mass storage callbacks follow */
  87. // usb framework checks this func exists for mass storage config. no code needed.
  88. void __USBInstallMassStorage() { }
  89. // Invoked when received SCSI_CMD_INQUIRY
  90. // fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
  91. extern "C" void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8],
  92. uint8_t product_id[16], uint8_t product_rev[4]) {
  93. // TODO: We could/should use strings from the platform, but they are too long
  94. const char vid[] = "ZuluSCSI";
  95. const char pid[] = "Pico";
  96. const char rev[] = "1.0";
  97. memcpy(vendor_id, vid, tu_min32(strlen(vid), 8));
  98. memcpy(product_id, pid, tu_min32(strlen(pid), 16));
  99. memcpy(product_rev, rev, tu_min32(strlen(rev), 4));
  100. }
  101. // max LUN supported
  102. // we only have the one SD card
  103. extern "C" uint8_t tud_msc_get_maxlun_cb(void) {
  104. return 1; // number of LUNs supported
  105. }
  106. // return writable status
  107. // on platform supporting write protect switch, could do that here.
  108. // otherwise this is not actually needed
  109. extern "C" bool tud_msc_is_writable_cb (uint8_t lun)
  110. {
  111. (void) lun;
  112. return unitReady;
  113. }
  114. // see https://www.seagate.com/files/staticfiles/support/docs/manual/Interface%20manuals/100293068j.pdf pg 221
  115. extern "C" bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
  116. {
  117. (void) lun;
  118. (void) power_condition;
  119. if (load_eject) {
  120. if (start) {
  121. // load disk storage
  122. // do nothing as we started "loaded"
  123. } else {
  124. unitReady = false;
  125. }
  126. }
  127. return true;
  128. }
  129. // return true if we are ready to service reads/writes
  130. extern "C" bool tud_msc_test_unit_ready_cb(uint8_t lun) {
  131. (void) lun;
  132. return unitReady;
  133. }
  134. // return size in blocks and block size
  135. extern "C" void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count,
  136. uint16_t *block_size) {
  137. (void) lun;
  138. *block_count = unitReady ? (SD.card()->sectorCount()) : 0;
  139. *block_size = SD_SECTOR_SIZE;
  140. }
  141. // Callback invoked when received an SCSI command not in built-in list (below) which have their own callbacks
  142. // - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE, READ10 and WRITE10
  143. extern "C" int32_t tud_msc_scsi_cb(uint8_t lun, const uint8_t scsi_cmd[16], void *buffer,
  144. uint16_t bufsize) {
  145. const void *response = NULL;
  146. uint16_t resplen = 0;
  147. switch (scsi_cmd[0]) {
  148. case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
  149. // Host is about to read/write etc ... better not to disconnect disk
  150. resplen = 0;
  151. break;
  152. default:
  153. // Set Sense = Invalid Command Operation
  154. tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
  155. // negative means error -> tinyusb could stall and/or response with failed status
  156. resplen = -1;
  157. break;
  158. }
  159. // return len must not larger than bufsize
  160. if (resplen > bufsize) {
  161. resplen = bufsize;
  162. }
  163. // copy response to stack's buffer if any
  164. if (response && resplen) {
  165. memcpy(buffer, response, resplen);
  166. }
  167. return resplen;
  168. }
  169. // Callback invoked when received READ10 command.
  170. // Copy disk's data to buffer (up to bufsize) and return number of copied bytes (must be multiple of block size)
  171. extern "C" int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset,
  172. void* buffer, uint32_t bufsize)
  173. {
  174. (void) lun;
  175. bool rc = SD.card()->readSectors(lba, (uint8_t*) buffer, bufsize/SD_SECTOR_SIZE);
  176. // only blink fast on reads; writes will override this
  177. if (MSC_LEDMode == LED_SOLIDON)
  178. MSC_LEDMode = LED_BLINK_FAST;
  179. return rc ? bufsize : -1;
  180. }
  181. // Callback invoked when receive WRITE10 command.
  182. // Process data in buffer to disk's storage and return number of written bytes (must be multiple of block size)
  183. extern "C" int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset,
  184. uint8_t *buffer, uint32_t bufsize) {
  185. (void) lun;
  186. bool rc = SD.card()->writeSectors(lba, buffer, bufsize/SD_SECTOR_SIZE);
  187. // always slow blink
  188. MSC_LEDMode = LED_BLINK_SLOW;
  189. return rc ? bufsize : -1;
  190. }
  191. // Callback invoked when WRITE10 command is completed (status received and accepted by host).
  192. // used to flush any pending cache to storage
  193. extern "C" void tud_msc_write10_complete_cb(uint8_t lun) {
  194. (void) lun;
  195. }
  196. #endif