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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "fat_disk.h"
  32. #include "log.h"
  33. static bool ejected = false;
  34. static bool medium_available = false;
  35. bool msc_is_medium_available(void) {
  36. return medium_available;
  37. }
  38. void msc_set_medium_available(bool state) {
  39. medium_available = state;
  40. if (state) {
  41. ejected = false;
  42. }
  43. }
  44. // Invoked when received SCSI_CMD_INQUIRY
  45. // Application fill vendor id, product id and revision
  46. // with string up to 8, 16, 4 characters respectively
  47. void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8],
  48. uint8_t product_id[16], uint8_t product_rev[4]) {
  49. (void) lun;
  50. const char vid[] = "xythobuz";
  51. const char pid[] = "Debug Storage";
  52. const char rev[] = "0.01";
  53. memcpy(vendor_id , vid, strlen(vid));
  54. memcpy(product_id , pid, strlen(pid));
  55. memcpy(product_rev, rev, strlen(rev));
  56. }
  57. // Invoked when received Test Unit Ready command.
  58. // return true allowing host to read/write this LUN e.g SD card inserted
  59. bool tud_msc_test_unit_ready_cb(uint8_t lun) {
  60. if (!medium_available) {
  61. // Additional Sense 3A-00 is NOT_FOUND
  62. tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
  63. }
  64. return medium_available;
  65. }
  66. // Invoked when received SCSI_CMD_READ_CAPACITY_10 and
  67. // SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
  68. // Application update block count and block size
  69. void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) {
  70. (void) lun;
  71. if (!medium_available) {
  72. *block_count = 0;
  73. *block_size = 0;
  74. } else {
  75. *block_count = DISK_BLOCK_COUNT;
  76. *block_size = DISK_BLOCK_SIZE;
  77. }
  78. }
  79. // Invoked when received Start Stop Unit command
  80. // - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
  81. // - Start = 1 : active mode, if load_eject = 1 : load disk storage
  82. bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition,
  83. bool start, bool load_eject) {
  84. (void) lun;
  85. (void) power_condition;
  86. if (start) {
  87. // load disk storage
  88. debug("load disk storage %d", load_eject);
  89. } else {
  90. // unload disk storage
  91. debug("unload disk storage %d", load_eject);
  92. if (load_eject) {
  93. medium_available = false;
  94. }
  95. }
  96. return true;
  97. }
  98. // Callback invoked when received READ10 command.
  99. // Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
  100. int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba,
  101. uint32_t offset, void* buffer, uint32_t bufsize) {
  102. (void) lun;
  103. // out of ramdisk
  104. if (lba >= DISK_BLOCK_COUNT) {
  105. return -1;
  106. }
  107. // TODO better range checking and length calculation?
  108. uint8_t const* addr = fat_disk_get_sector(lba) + offset;
  109. memcpy(buffer, addr, bufsize);
  110. return (int32_t) bufsize;
  111. }
  112. bool tud_msc_is_writable_cb (uint8_t lun) {
  113. (void) lun;
  114. return true;
  115. }
  116. // Callback invoked when received WRITE10 command.
  117. // Process data in buffer to disk's storage and return number of written bytes
  118. int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba,
  119. uint32_t offset, uint8_t* buffer, uint32_t bufsize) {
  120. (void) lun;
  121. // out of ramdisk
  122. if (lba >= DISK_BLOCK_COUNT) {
  123. return -1;
  124. }
  125. // TODO better range checking and length calculation?
  126. uint8_t* addr = fat_disk_get_sector(lba) + offset;
  127. memcpy(addr, buffer, bufsize);
  128. return (int32_t) bufsize;
  129. }
  130. // Callback invoked when received an SCSI command not in built-in list below
  131. // - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
  132. // - READ10 and WRITE10 has their own callbacks and MUST not be handled here
  133. int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16],
  134. void* buffer, uint16_t bufsize) {
  135. void const* response = NULL;
  136. int32_t resplen = 0;
  137. // most scsi handled is input
  138. bool in_xfer = true;
  139. switch (scsi_cmd[0]) {
  140. case 0x1E:
  141. // Prevent/Allow Medium Removal
  142. if (scsi_cmd[4] & 0x01) {
  143. // Prevent medium removal
  144. if (!medium_available) {
  145. debug("Host wants to lock non-existing medium. Not supported.");
  146. resplen = -1;
  147. } else {
  148. debug("Host wants to lock medium.");
  149. }
  150. } else {
  151. // Allow medium removal
  152. if (medium_available) {
  153. debug("Host ejected medium. Unplugging disk.");
  154. medium_available = false;
  155. } else {
  156. debug("host ejected 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. }