|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020-2021 Damien P. George |
| 7 | + * Copyright (c) 2025 Andrew Leech |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "py/mpconfig.h" |
| 29 | + |
| 30 | +#if MICROPY_HW_USB_MSC && MICROPY_HW_TINYUSB_STACK |
| 31 | + |
| 32 | +#include "py/misc.h" |
| 33 | +#include "tusb.h" |
| 34 | +#include "storage.h" |
| 35 | +#include "sdcard.h" |
| 36 | +#include "usb.h" |
| 37 | + |
| 38 | +// Storage medium selection (shared with main.c for compatibility) |
| 39 | +pyb_usb_storage_medium_t pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_NONE; |
| 40 | + |
| 41 | +// Ejection state |
| 42 | +static bool msc_ejected = false; |
| 43 | + |
| 44 | +// Invoked on SCSI_CMD_INQUIRY. |
| 45 | +// Fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively. |
| 46 | +void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) { |
| 47 | + (void)lun; |
| 48 | + memcpy(vendor_id, MICROPY_HW_USB_MSC_INQUIRY_VENDOR_STRING, MIN(strlen(MICROPY_HW_USB_MSC_INQUIRY_VENDOR_STRING), 8)); |
| 49 | + memcpy(product_id, MICROPY_HW_USB_MSC_INQUIRY_PRODUCT_STRING, MIN(strlen(MICROPY_HW_USB_MSC_INQUIRY_PRODUCT_STRING), 16)); |
| 50 | + memcpy(product_rev, MICROPY_HW_USB_MSC_INQUIRY_REVISION_STRING, MIN(strlen(MICROPY_HW_USB_MSC_INQUIRY_REVISION_STRING), 4)); |
| 51 | +} |
| 52 | + |
| 53 | +// Invoked on Test-Unit-Ready command. |
| 54 | +// Return true allowing host to read/write this LUN (e.g., SD card inserted). |
| 55 | +bool tud_msc_test_unit_ready_cb(uint8_t lun) { |
| 56 | + (void)lun; |
| 57 | + |
| 58 | + if (msc_ejected || pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) { |
| 59 | + tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00); |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + #if MICROPY_HW_ENABLE_SDCARD |
| 64 | + if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD) { |
| 65 | + if (!sdcard_is_present()) { |
| 66 | + tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00); |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + #endif |
| 71 | + |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +// Invoked on SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size. |
| 76 | +void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size) { |
| 77 | + (void)lun; |
| 78 | + |
| 79 | + if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_FLASH) { |
| 80 | + *block_size = storage_get_block_size(); |
| 81 | + *block_count = storage_get_block_count(); |
| 82 | + #if MICROPY_HW_ENABLE_SDCARD |
| 83 | + } else if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD) { |
| 84 | + *block_size = SDCARD_BLOCK_SIZE; |
| 85 | + *block_count = sdcard_get_capacity_in_bytes() / (uint64_t)SDCARD_BLOCK_SIZE; |
| 86 | + #endif |
| 87 | + } else { |
| 88 | + *block_size = 0; |
| 89 | + *block_count = 0; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// Invoked on Start-Stop-Unit command: |
| 94 | +// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage |
| 95 | +// - Start = 1 : active mode, if load_eject = 1 : load disk storage |
| 96 | +bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) { |
| 97 | + (void)lun; |
| 98 | + (void)power_condition; |
| 99 | + |
| 100 | + if (load_eject) { |
| 101 | + if (start) { |
| 102 | + // Load disk storage |
| 103 | + msc_ejected = false; |
| 104 | + } else { |
| 105 | + // Unload disk storage |
| 106 | + msc_ejected = true; |
| 107 | + } |
| 108 | + } |
| 109 | + return true; |
| 110 | +} |
| 111 | + |
| 112 | +// Callback invoked on READ10 command. |
| 113 | +// Copy disk's data to buffer (up to bufsize) and return number of read bytes. |
| 114 | +int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) { |
| 115 | + (void)lun; |
| 116 | + (void)offset; |
| 117 | + |
| 118 | + if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_FLASH) { |
| 119 | + uint32_t block_count = bufsize / FLASH_BLOCK_SIZE; |
| 120 | + int ret = storage_read_blocks(buffer, lba, block_count); |
| 121 | + if (ret != 0) { |
| 122 | + return -1; |
| 123 | + } |
| 124 | + return block_count * FLASH_BLOCK_SIZE; |
| 125 | + #if MICROPY_HW_ENABLE_SDCARD |
| 126 | + } else if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD) { |
| 127 | + uint32_t block_count = bufsize / SDCARD_BLOCK_SIZE; |
| 128 | + if (sdcard_read_blocks(buffer, lba, block_count) != 0) { |
| 129 | + return -1; |
| 130 | + } |
| 131 | + return block_count * SDCARD_BLOCK_SIZE; |
| 132 | + #endif |
| 133 | + } |
| 134 | + |
| 135 | + return -1; |
| 136 | +} |
| 137 | + |
| 138 | +// Callback invoked on WRITE10 command. |
| 139 | +// Process data in buffer to disk's storage and return number of written bytes. |
| 140 | +int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize) { |
| 141 | + (void)lun; |
| 142 | + (void)offset; |
| 143 | + |
| 144 | + if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_FLASH) { |
| 145 | + uint32_t block_count = bufsize / FLASH_BLOCK_SIZE; |
| 146 | + int ret = storage_write_blocks(buffer, lba, block_count); |
| 147 | + if (ret != 0) { |
| 148 | + return -1; |
| 149 | + } |
| 150 | + return block_count * FLASH_BLOCK_SIZE; |
| 151 | + #if MICROPY_HW_ENABLE_SDCARD |
| 152 | + } else if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD) { |
| 153 | + uint32_t block_count = bufsize / SDCARD_BLOCK_SIZE; |
| 154 | + if (sdcard_write_blocks(buffer, lba, block_count) != 0) { |
| 155 | + return -1; |
| 156 | + } |
| 157 | + return block_count * SDCARD_BLOCK_SIZE; |
| 158 | + #endif |
| 159 | + } |
| 160 | + |
| 161 | + return -1; |
| 162 | +} |
| 163 | + |
| 164 | +// Callback invoked on a SCSI command that's not handled by TinyUSB. |
| 165 | +int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize) { |
| 166 | + (void)lun; |
| 167 | + (void)buffer; |
| 168 | + (void)bufsize; |
| 169 | + |
| 170 | + int32_t resplen = 0; |
| 171 | + switch (scsi_cmd[0]) { |
| 172 | + case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL: |
| 173 | + // Sync the logical unit if needed. |
| 174 | + #if MICROPY_HW_ENABLE_STORAGE |
| 175 | + if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_FLASH) { |
| 176 | + storage_flush(); |
| 177 | + } |
| 178 | + #endif |
| 179 | + break; |
| 180 | + |
| 181 | + default: |
| 182 | + // Set Sense = Invalid Command Operation |
| 183 | + tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); |
| 184 | + // negative means error -> tinyusb could stall and/or response with failed status |
| 185 | + resplen = -1; |
| 186 | + break; |
| 187 | + } |
| 188 | + return resplen; |
| 189 | +} |
| 190 | + |
| 191 | +#endif // MICROPY_HW_USB_MSC && MICROPY_HW_TINYUSB_STACK |
0 commit comments