forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathusb_msc_flash.c
349 lines (297 loc) · 11 KB
/
usb_msc_flash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2018 hathach for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include "tusb.h"
// // #include "supervisor/flash.h"
// For updating fatfs's cache
#include "extmod/vfs.h"
#include "extmod/vfs_fat.h"
#include "lib/oofatfs/diskio.h"
#include "lib/oofatfs/ff.h"
#include "py/mpstate.h"
#include "shared-module/storage/__init__.h"
#include "supervisor/filesystem.h"
#include "supervisor/shared/reload.h"
#define MSC_FLASH_BLOCK_SIZE 512
#if CIRCUITPY_SAVES_PARTITION_SIZE > 0
#define LUN_COUNT 2
#else
#define LUN_COUNT 1
#endif
// The ellipsis range in the designated initializer of `ejected` is not standard C,
// but it works in both gcc and clang.
static bool ejected[LUN_COUNT] = { [0 ... (LUN_COUNT - 1)] = true};
static bool locked[LUN_COUNT] = {false};
#include "tusb.h"
static const uint8_t usb_msc_descriptor_template[] = {
// MSC Interface Descriptor
0x09, // 0 bLength
0x04, // 1 bDescriptorType (Interface)
0xFF, // 2 bInterfaceNumber [SET AT RUNTIME]
#define MSC_INTERFACE_INDEX (2)
0x00, // 3 bAlternateSetting
0x02, // 4 bNumEndpoints 2
0x08, // 5 bInterfaceClass: MSC
0x06, // 6 bInterfaceSubClass: TRANSPARENT
0x50, // 7 bInterfaceProtocol: BULK
0xFF, // 8 iInterface (String Index) [SET AT RUNTIME]
#define MSC_INTERFACE_STRING_INDEX (8)
// MSC Endpoint IN Descriptor
0x07, // 9 bLength
0x05, // 10 bDescriptorType (Endpoint)
0xFF, // 11 bEndpointAddress (IN/D2H) [SET AT RUNTIME: 0x80 | number]
#define MSC_IN_ENDPOINT_INDEX (11)
0x02, // 12 bmAttributes (Bulk)
#if USB_HIGHSPEED
0x00, 0x02, // 13,14 wMaxPacketSize 512
#else
0x40, 0x00, // 13,14 wMaxPacketSize 64
#endif
0x00, // 15 bInterval 0 (unit depends on device speed)
// MSC Endpoint OUT Descriptor
0x07, // 16 bLength
0x05, // 17 bDescriptorType (Endpoint)
0xFF, // 18 bEndpointAddress (OUT/H2D) [SET AT RUNTIME]
#define MSC_OUT_ENDPOINT_INDEX (18)
0x02, // 19 bmAttributes (Bulk)
#if USB_HIGHSPEED
0x00, 0x02, // 20,21 wMaxPacketSize 512
#else
0x40, 0x00, // 20,21 wMaxPacketSize 64
#endif
0x00, // 22 bInterval 0 (unit depends on device speed)
};
size_t usb_msc_descriptor_length(void) {
return sizeof(usb_msc_descriptor_template);
}
static const char storage_interface_name[] = USB_INTERFACE_NAME " Mass Storage";
size_t usb_msc_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *descriptor_counts, uint8_t *current_interface_string) {
memcpy(descriptor_buf, usb_msc_descriptor_template, sizeof(usb_msc_descriptor_template));
descriptor_buf[MSC_INTERFACE_INDEX] = descriptor_counts->current_interface;
descriptor_counts->current_interface++;
descriptor_buf[MSC_IN_ENDPOINT_INDEX] =
0x80 | (USB_MSC_EP_NUM_IN ? USB_MSC_EP_NUM_IN : descriptor_counts->current_endpoint);
descriptor_counts->num_in_endpoints++;
// Some TinyUSB devices have issues with bi-directional endpoints
#ifdef TUD_ENDPOINT_ONE_DIRECTION_ONLY
descriptor_counts->current_endpoint++;
#endif
descriptor_buf[MSC_OUT_ENDPOINT_INDEX] =
USB_MSC_EP_NUM_OUT ? USB_MSC_EP_NUM_OUT : descriptor_counts->current_endpoint;
descriptor_counts->num_out_endpoints++;
descriptor_counts->current_endpoint++;
usb_add_interface_string(*current_interface_string, storage_interface_name);
descriptor_buf[MSC_INTERFACE_STRING_INDEX] = *current_interface_string;
(*current_interface_string)++;
return sizeof(usb_msc_descriptor_template);
}
// The root FS is always at the end of the list.
static fs_user_mount_t *get_vfs(int lun) {
// Keep a history of the mounts we pass so we can search back.
mp_vfs_mount_t *mounts[LUN_COUNT];
mp_vfs_mount_t *current_mount = MP_STATE_VM(vfs_mount_table);
if (current_mount == NULL) {
return NULL;
}
// i is the last entry filled
size_t i = 0;
mounts[i] = current_mount;
while (current_mount->next != NULL) {
current_mount = current_mount->next;
i = (i + 1) % LUN_COUNT;
mounts[i] = current_mount;
}
fs_user_mount_t *vfs = mounts[(i - lun) % LUN_COUNT]->obj;
return vfs;
}
static void _usb_msc_uneject(void) {
for (uint8_t i = 0; i < LUN_COUNT; i++) {
ejected[i] = false;
locked[i] = false;
}
}
void usb_msc_mount(void) {
_usb_msc_uneject();
}
void usb_msc_umount(void) {
for (uint8_t i = 0; i < LUN_COUNT; i++) {
fs_user_mount_t *vfs = get_vfs(i + 1);
if (vfs == NULL) {
continue;
}
blockdev_unlock(vfs);
locked[i] = false;
}
}
bool usb_msc_ejected(void) {
bool all_ejected = true;
for (uint8_t i = 0; i < LUN_COUNT; i++) {
all_ejected &= ejected[i];
}
return all_ejected;
}
uint8_t tud_msc_get_maxlun_cb(void) {
return LUN_COUNT;
}
// Callback invoked when received an SCSI command not in built-in list below
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, TEST_UNIT_READY, START_STOP_UNIT, MODE_SENSE6, REQUEST_SENSE
// - READ10 and WRITE10 have their own callbacks
int32_t tud_msc_scsi_cb(uint8_t lun, const uint8_t scsi_cmd[16], void *buffer, uint16_t bufsize) {
const void *response = NULL;
int32_t resplen = 0;
switch (scsi_cmd[0]) {
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
// Host is about to read/write etc ... better not to disconnect disk
resplen = 0;
break;
default:
// Set Sense = Invalid Command Operation
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
// negative means error -> tinyusb could stall and/or response with failed status
resplen = -1;
break;
}
// return len must not larger than bufsize
if (resplen > bufsize) {
resplen = bufsize;
}
// copy response to stack's buffer if any
if (response && (resplen > 0)) {
memcpy(buffer, response, resplen);
}
return resplen;
}
void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size) {
fs_user_mount_t *vfs = get_vfs(lun);
disk_ioctl(vfs, GET_SECTOR_COUNT, block_count);
disk_ioctl(vfs, GET_SECTOR_SIZE, block_size);
}
bool tud_msc_is_writable_cb(uint8_t lun) {
if (lun >= LUN_COUNT) {
return false;
}
fs_user_mount_t *vfs = get_vfs(lun);
if (vfs == NULL) {
return false;
}
if (vfs->blockdev.writeblocks[0] == MP_OBJ_NULL || !filesystem_is_writable_by_usb(vfs)) {
return false;
}
// Lock the blockdev once we say we're writable.
if (!locked[lun] && !blockdev_lock(vfs)) {
return false;
}
locked[lun] = true;
return true;
}
// Callback invoked when received READ10 command.
// Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) {
(void)offset;
const uint32_t block_count = bufsize / MSC_FLASH_BLOCK_SIZE;
fs_user_mount_t *vfs = get_vfs(lun);
uint32_t disk_block_count;
disk_ioctl(vfs, GET_SECTOR_COUNT, &disk_block_count);
if (lba + block_count > disk_block_count) {
return -1;
}
disk_read(vfs, buffer, lba, block_count);
return block_count * MSC_FLASH_BLOCK_SIZE;
}
// Callback invoked when received WRITE10 command.
// Process data in buffer to disk's storage and return number of written bytes
int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize) {
(void)lun;
(void)offset;
autoreload_suspend(AUTORELOAD_SUSPEND_USB);
const uint32_t block_count = bufsize / MSC_FLASH_BLOCK_SIZE;
fs_user_mount_t *vfs = get_vfs(lun);
disk_write(vfs, buffer, lba, block_count);
// Since by getting here we assume the mount is read-only to
// MicroPython let's update the cached FatFs sector if it's the one
// we just wrote.
#if FF_MAX_SS != FF_MIN_SS
if (vfs->fatfs.ssize == MSC_FLASH_BLOCK_SIZE) {
#else
// The compiler can optimize this away.
if (FF_MAX_SS == FILESYSTEM_BLOCK_SIZE) {
#endif
if (lba == vfs->fatfs.winsect && lba > 0) {
memcpy(vfs->fatfs.win,
buffer + MSC_FLASH_BLOCK_SIZE * (vfs->fatfs.winsect - lba),
MSC_FLASH_BLOCK_SIZE);
}
}
return block_count * MSC_FLASH_BLOCK_SIZE;
}
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
// used to flush any pending cache.
void tud_msc_write10_complete_cb(uint8_t lun) {
(void)lun;
// This write is complete; initiate an autoreload.
autoreload_resume(AUTORELOAD_SUSPEND_USB);
autoreload_trigger();
}
// Invoked when received SCSI_CMD_INQUIRY
// Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {
(void)lun;
memcpy(vendor_id, CFG_TUD_MSC_VENDOR, strlen(CFG_TUD_MSC_VENDOR));
memcpy(product_id, CFG_TUD_MSC_PRODUCT, strlen(CFG_TUD_MSC_PRODUCT));
memcpy(product_rev, CFG_TUD_MSC_PRODUCT_REV, strlen(CFG_TUD_MSC_PRODUCT_REV));
}
// Invoked when received Test Unit Ready command.
// return true allowing host to read/write this LUN e.g SD card inserted
bool tud_msc_test_unit_ready_cb(uint8_t lun) {
if (lun >= LUN_COUNT) {
return false;
}
fs_user_mount_t *current_mount = get_vfs(lun);
if (current_mount == NULL) {
return false;
}
if (ejected[lun]) {
// Set 0x3a for media not present.
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3A, 0x00);
return false;
}
return true;
}
// Invoked when received Start Stop Unit command
// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
// - Start = 1 : active mode, if load_eject = 1 : load disk storage
bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) {
if (lun >= LUN_COUNT) {
return false;
}
fs_user_mount_t *current_mount = get_vfs(lun);
if (current_mount == NULL) {
return false;
}
if (load_eject) {
if (!start) {
// Eject but first flush.
if (disk_ioctl(current_mount, CTRL_SYNC, NULL) != RES_OK) {
return false;
} else {
blockdev_unlock(current_mount);
ejected[lun] = true;
locked[lun] = false;
}
} else {
// We can only load if it hasn't been ejected.
return !ejected[lun];
}
} else {
if (!start) {
// Stop the unit but don't eject.
if (disk_ioctl(current_mount, CTRL_SYNC, NULL) != RES_OK) {
return false;
}
}
// Always start the unit, even if ejected. Whether media is present is a separate check.
}
return true;
}