Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | * |
| 24 | * This file is part of the TinyUSB stack. |
| 25 | */ |
| 26 | |
| 27 | #ifndef _TUSB_MSC_DEVICE_H_ |
| 28 | #define _TUSB_MSC_DEVICE_H_ |
| 29 | |
| 30 | #include "common/tusb_common.h" |
| 31 | #include "msc.h" |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | //--------------------------------------------------------------------+ |
| 38 | // Class Driver Configuration |
| 39 | //--------------------------------------------------------------------+ |
| 40 | |
| 41 | #if !defined(CFG_TUD_MSC_EP_BUFSIZE) & defined(CFG_TUD_MSC_BUFSIZE) |
| 42 | // TODO warn user to use new name later on |
| 43 | // #warning CFG_TUD_MSC_BUFSIZE is renamed to CFG_TUD_MSC_EP_BUFSIZE, please update to use the new name |
| 44 | #define CFG_TUD_MSC_EP_BUFSIZE CFG_TUD_MSC_BUFSIZE |
| 45 | #endif |
| 46 | |
| 47 | #ifndef CFG_TUD_MSC_EP_BUFSIZE |
| 48 | #error CFG_TUD_MSC_EP_BUFSIZE must be defined, value of a block size should work well, the more the better |
| 49 | #endif |
| 50 | |
| 51 | TU_VERIFY_STATIC(CFG_TUD_MSC_EP_BUFSIZE < UINT16_MAX, "Size is not correct"); |
| 52 | |
| 53 | //--------------------------------------------------------------------+ |
| 54 | // Application API |
| 55 | //--------------------------------------------------------------------+ |
| 56 | |
| 57 | // Set SCSI sense response |
| 58 | bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, uint8_t add_sense_qualifier); |
| 59 | |
| 60 | //--------------------------------------------------------------------+ |
| 61 | // Application Callbacks (WEAK is optional) |
| 62 | //--------------------------------------------------------------------+ |
| 63 | |
| 64 | // Invoked when received SCSI READ10 command |
| 65 | // - Address = lba * BLOCK_SIZE + offset |
| 66 | // - offset is only needed if CFG_TUD_MSC_EP_BUFSIZE is smaller than BLOCK_SIZE. |
| 67 | // |
| 68 | // - Application fill the buffer (up to bufsize) with address contents and return number of read byte. If |
| 69 | // - read < bufsize : These bytes are transferred first and callback invoked again for remaining data. |
| 70 | // |
| 71 | // - read == 0 : Indicate application is not ready yet e.g disk I/O busy. |
| 72 | // Callback invoked again with the same parameters later on. |
| 73 | // |
| 74 | // - read < 0 : Indicate application error e.g invalid address. This request will be STALLed |
| 75 | // and return failed status in command status wrapper phase. |
| 76 | int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize); |
| 77 | |
| 78 | // Invoked when received SCSI WRITE10 command |
| 79 | // - Address = lba * BLOCK_SIZE + offset |
| 80 | // - offset is only needed if CFG_TUD_MSC_EP_BUFSIZE is smaller than BLOCK_SIZE. |
| 81 | // |
| 82 | // - Application write data from buffer to address contents (up to bufsize) and return number of written byte. If |
| 83 | // - write < bufsize : callback invoked again with remaining data later on. |
| 84 | // |
| 85 | // - write == 0 : Indicate application is not ready yet e.g disk I/O busy. |
| 86 | // Callback invoked again with the same parameters later on. |
| 87 | // |
| 88 | // - write < 0 : Indicate application error e.g invalid address. This request will be STALLed |
| 89 | // and return failed status in command status wrapper phase. |
| 90 | // |
| 91 | // TODO change buffer to const uint8_t* |
| 92 | int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize); |
| 93 | |
| 94 | // Invoked when received SCSI_CMD_INQUIRY |
| 95 | // Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively |
| 96 | void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]); |
| 97 | |
| 98 | // Invoked when received Test Unit Ready command. |
| 99 | // return true allowing host to read/write this LUN e.g SD card inserted |
| 100 | bool tud_msc_test_unit_ready_cb(uint8_t lun); |
| 101 | |
| 102 | // Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size |
| 103 | // Application update block count and block size |
| 104 | void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size); |
| 105 | |
| 106 | /** |
| 107 | * Invoked when received an SCSI command not in built-in list below. |
| 108 | * - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, TEST_UNIT_READY, START_STOP_UNIT, MODE_SENSE6, REQUEST_SENSE |
| 109 | * - READ10 and WRITE10 has their own callbacks |
| 110 | * |
| 111 | * \param[in] lun Logical unit number |
| 112 | * \param[in] scsi_cmd SCSI command contents which application must examine to response accordingly |
| 113 | * \param[out] buffer Buffer for SCSI Data Stage. |
| 114 | * - For INPUT: application must fill this with response. |
| 115 | * - For OUTPUT it holds the Data from host |
| 116 | * \param[in] bufsize Buffer's length. |
| 117 | * |
| 118 | * \return Actual bytes processed, can be zero for no-data command. |
| 119 | * \retval negative Indicate error e.g unsupported command, tinyusb will \b STALL the corresponding |
| 120 | * endpoint and return failed status in command status wrapper phase. |
| 121 | */ |
| 122 | int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize); |
| 123 | |
| 124 | /*------------- Optional callbacks -------------*/ |
| 125 | |
| 126 | // Invoked when received GET_MAX_LUN request, required for multiple LUNs implementation |
| 127 | TU_ATTR_WEAK uint8_t tud_msc_get_maxlun_cb(void); |
| 128 | |
| 129 | // Invoked when received Start Stop Unit command |
| 130 | // - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage |
| 131 | // - Start = 1 : active mode, if load_eject = 1 : load disk storage |
| 132 | TU_ATTR_WEAK bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject); |
| 133 | |
| 134 | // Invoked when Read10 command is complete |
| 135 | TU_ATTR_WEAK void tud_msc_read10_complete_cb(uint8_t lun); |
| 136 | |
| 137 | // Invoke when Write10 command is complete, can be used to flush flash caching |
| 138 | TU_ATTR_WEAK void tud_msc_write10_complete_cb(uint8_t lun); |
| 139 | |
| 140 | // Invoked when command in tud_msc_scsi_cb is complete |
| 141 | TU_ATTR_WEAK void tud_msc_scsi_complete_cb(uint8_t lun, uint8_t const scsi_cmd[16]); |
| 142 | |
| 143 | // Invoked to check if device is writable as part of SCSI WRITE10 |
| 144 | TU_ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun); |
| 145 | |
| 146 | //--------------------------------------------------------------------+ |
| 147 | // Internal Class Driver API |
| 148 | //--------------------------------------------------------------------+ |
| 149 | void mscd_init (void); |
| 150 | void mscd_reset (uint8_t rhport); |
| 151 | uint16_t mscd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len); |
| 152 | bool mscd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * p_request); |
| 153 | bool mscd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); |
| 154 | |
| 155 | #ifdef __cplusplus |
| 156 | } |
| 157 | #endif |
| 158 | |
| 159 | #endif /* _TUSB_MSC_DEVICE_H_ */ |