1 /*
2 * SPDX-FileCopyrightText: 2019 Ha Thach (tinyusb.org)
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * SPDX-FileContributor: 2019-2021 Espressif Systems (Shanghai) CO LTD
7 *
8 */
9
10 /*
11 * The MIT License (MIT)
12 *
13 * Copyright (c) 2019 Ha Thach (tinyusb.org)
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this software and associated documentation files (the "Software"), to deal
17 * in the Software without restriction, including without limitation the rights
18 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 * copies of the Software, and to permit persons to whom the Software is
20 * furnished to do so, subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 * THE SOFTWARE.
32 *
33 */
34
35 #include <stdint.h>
36 #include "esp_log.h"
37 #include "freertos/FreeRTOS.h"
38 #include "freertos/task.h"
39 #include "tinyusb.h"
40 #include "test_common.h"
41 #include "soc/soc_caps.h"
42
43 #if SOC_USB_OTG_SUPPORTED
44
45 #define MASS_STORAGE_CLASS 0x08
46 #define SCSI_COMMAND_SET 0x06
47 #define BULK_ONLY_TRANSFER 0x50
48
49 static const char *TAG = "msc_example";
50
51
52 /**** Kconfig driven Descriptor ****/
53 tusb_desc_device_t device_descriptor = {
54 .bLength = sizeof(device_descriptor),
55 .bDescriptorType = TUSB_DESC_DEVICE,
56 .bcdUSB = 0x0200,
57 .bDeviceClass = MASS_STORAGE_CLASS,
58 .bDeviceSubClass = SCSI_COMMAND_SET,
59 .bDeviceProtocol = BULK_ONLY_TRANSFER,
60 .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
61 .idVendor = USB_ESPRESSIF_VID,
62 .idProduct = 0x1234,
63 .bcdDevice = 0x0100,
64 .iManufacturer = 0x01,
65 .iProduct = 0x02,
66 .iSerialNumber = 0x03,
67 .bNumConfigurations = 0x01
68 };
69
device_app(void)70 void device_app(void)
71 {
72 ESP_LOGI(TAG, "USB initialization");
73
74 tinyusb_config_t tusb_cfg = {
75 .descriptor = &device_descriptor
76 };
77
78 ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
79 ESP_LOGI(TAG, "USB initialization DONE");
80
81 while (1) {
82 vTaskDelay(100);
83 }
84 }
85
86
87 // whether host does safe-eject
88 static bool ejected = false;
89
90 // Some MCU doesn't have enough 8KB SRAM to store the whole disk
91 // We will use Flash as read-only disk with board that has
92 // CFG_EXAMPLE_MSC_READONLY defined
93
94 uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
95 //------------- Block0: Boot Sector -------------//
96 // byte_per_sector = DISK_BLOCK_SIZE; fat12_sector_num_16 = DISK_BLOCK_NUM;
97 // sector_per_cluster = 1; reserved_sectors = 1;
98 // fat_num = 1; fat12_root_entry_num = 16;
99 // sector_per_fat = 1; sector_per_track = 1; head_num = 1; hidden_sectors = 0;
100 // drive_number = 0x80; media_type = 0xf8; extended_boot_signature = 0x29;
101 // filesystem_type = "FAT12 "; volume_serial_number = 0x1234; volume_label = "TinyUSB MSC";
102 // FAT magic code at offset 510-511
103 {
104 0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53, 0x35, 0x2E, 0x30, 0x00, 0x02, 0x01, 0x01, 0x00,
105 0x01, 0x10, 0x00, 0x10, 0x00, 0xF8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
106 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x29, 0x34, 0x12, 0x00, 0x00, 'T', 'i', 'n', 'y', 'U',
107 'S', 'B', ' ', 'M', 'S', 'C', 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x00, 0x00,
108
109 // Zero up to 2 last bytes of FAT magic code
110 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
116 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118
119 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
120 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
121 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
122 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127
128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
133 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136
137 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
139 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
140 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA
141 },
142
143 //------------- Block1: FAT12 Table -------------//
144 {
145 0xF8, 0xFF, 0xFF, 0xFF, 0x0F // // first 2 entries must be F8FF, third entry is cluster end of readme file
146 },
147
148 //------------- Block2: Root Directory -------------//
149 {
150 // first entry is volume label
151 'T', 'i', 'n', 'y', 'U', 'S', 'B', ' ', 'M', 'S', 'C', 0x08, 0x00, 0x00, 0x00, 0x00,
152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0x6D, 0x65, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153 // second entry is readme file
154 'R', 'E', 'A', 'D', 'M', 'E', ' ', ' ', 'T', 'X', 'T', 0x20, 0x00, 0xC6, 0x52, 0x6D,
155 0x65, 0x43, 0x65, 0x43, 0x00, 0x00, 0x88, 0x6D, 0x65, 0x43, 0x02, 0x00,
156 sizeof(README_CONTENTS) - 1, 0x00, 0x00, 0x00 // readme's files size (4 Bytes)
157 },
158
159 //------------- Block3: Readme Content -------------//
160 README_CONTENTS
161 };
162
163 // Invoked when received SCSI_CMD_INQUIRY
164 // Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
tud_msc_inquiry_cb(uint8_t lun,uint8_t vendor_id[8],uint8_t product_id[16],uint8_t product_rev[4])165 void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
166 {
167 (void) lun;
168
169 const char vid[] = "TinyUSB";
170 const char pid[] = "Mass Storage";
171 const char rev[] = "1.0";
172
173 memcpy(vendor_id, vid, strlen(vid));
174 memcpy(product_id, pid, strlen(pid));
175 memcpy(product_rev, rev, strlen(rev));
176 }
177
178 // Invoked when received Test Unit Ready command.
179 // return true allowing host to read/write this LUN e.g SD card inserted
tud_msc_test_unit_ready_cb(uint8_t lun)180 bool tud_msc_test_unit_ready_cb(uint8_t lun)
181 {
182 (void) lun;
183
184 // RAM disk is ready until ejected
185 if (ejected) {
186 tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
187 return false;
188 }
189
190 return true;
191 }
192
193 // Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
194 // Application update block count and block size
tud_msc_capacity_cb(uint8_t lun,uint32_t * block_count,uint16_t * block_size)195 void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size)
196 {
197 (void) lun;
198
199 *block_count = DISK_BLOCK_NUM;
200 *block_size = DISK_BLOCK_SIZE;
201 }
202
203 // Invoked when received Start Stop Unit command
204 // - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
205 // - Start = 1 : active mode, if load_eject = 1 : load disk storage
tud_msc_start_stop_cb(uint8_t lun,uint8_t power_condition,bool start,bool load_eject)206 bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
207 {
208 (void) lun;
209 (void) power_condition;
210
211 if ( load_eject ) {
212 if (start) {
213 // load disk storage
214 } else {
215 // unload disk storage
216 ejected = true;
217 }
218 }
219
220 return true;
221 }
222
223 // Callback invoked when received READ10 command.
224 // Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
tud_msc_read10_cb(uint8_t lun,uint32_t lba,uint32_t offset,void * buffer,uint32_t bufsize)225 int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize)
226 {
227 (void) lun;
228
229 uint8_t const *addr = msc_disk[lba] + offset;
230 memcpy(buffer, addr, bufsize);
231
232 return bufsize;
233 }
234
235 // Callback invoked when received WRITE10 command.
236 // Process data in buffer to disk's storage and return number of written bytes
tud_msc_write10_cb(uint8_t lun,uint32_t lba,uint32_t offset,uint8_t * buffer,uint32_t bufsize)237 int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize)
238 {
239 (void) lun;
240
241 #ifndef CFG_EXAMPLE_MSC_READONLY
242 uint8_t *addr = msc_disk[lba] + offset;
243 memcpy(addr, buffer, bufsize);
244 #else
245 (void) lba; (void) offset; (void) buffer;
246 #endif
247
248 return bufsize;
249 }
250
251 // Callback invoked when received an SCSI command not in built-in list below
252 // - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
253 // - READ10 and WRITE10 has their own callbacks
tud_msc_scsi_cb(uint8_t lun,uint8_t const scsi_cmd[16],void * buffer,uint16_t bufsize)254 int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize)
255 {
256 // read10 & write10 has their own callback and MUST not be handled here
257
258 void const *response = NULL;
259 uint16_t resplen = 0;
260
261 // most scsi handled is input
262 bool in_xfer = true;
263
264 switch (scsi_cmd[0]) {
265 case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
266 // Host is about to read/write etc ... better not to disconnect disk
267 resplen = 0;
268 break;
269
270 default:
271 // Set Sense = Invalid Command Operation
272 tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
273
274 // negative means error -> tinyusb could stall and/or response with failed status
275 resplen = -1;
276 break;
277 }
278
279 // return resplen must not larger than bufsize
280 if ( resplen > bufsize ) {
281 resplen = bufsize;
282 }
283
284 if ( response && (resplen > 0) ) {
285 if (in_xfer) {
286 memcpy(buffer, response, resplen);
287 } else {
288 // SCSI output
289 }
290 }
291
292 return resplen;
293 }
294
295 #endif
296