S&B Volcano vaporizer remote control with Pi Pico W
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 6.4KB

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