|
41 | 41 |
|
42 | 42 | static bool ejected[1];
|
43 | 43 |
|
| 44 | +void usb_msc_mount(void) { |
| 45 | + // Reset the ejection tracking every time we're plugged into USB. This allows for us to battery |
| 46 | + // power the device, eject, unplug and plug it back in to get the drive. |
| 47 | + for (uint8_t i = 0; i < sizeof(ejected); i++) { |
| 48 | + ejected[i] = false; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +void usb_msc_umount(void) { |
| 53 | + |
| 54 | +} |
| 55 | + |
44 | 56 | // The root FS is always at the end of the list.
|
45 | 57 | static fs_user_mount_t* get_vfs(int lun) {
|
46 | 58 | // TODO(tannewt): Return the mount which matches the lun where 0 is the end
|
@@ -198,19 +210,34 @@ bool tud_msc_test_unit_ready_cb(uint8_t lun) {
|
198 | 210 | // - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
|
199 | 211 | // - Start = 1 : active mode, if load_eject = 1 : load disk storage
|
200 | 212 | bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) {
|
| 213 | + if (lun > 1) { |
| 214 | + return false; |
| 215 | + } |
| 216 | + fs_user_mount_t* current_mount = get_vfs(lun); |
| 217 | + if (current_mount == NULL) { |
| 218 | + return false; |
| 219 | + } |
201 | 220 | if (load_eject) {
|
202 |
| - if (lun > 1) { |
203 |
| - return false; |
204 |
| - } else { |
205 |
| - fs_user_mount_t* current_mount = get_vfs(lun); |
206 |
| - if (current_mount == NULL) { |
207 |
| - return false; |
208 |
| - } |
| 221 | + if (!start) { |
| 222 | + // Eject but first flush. |
209 | 223 | if (disk_ioctl(current_mount, CTRL_SYNC, NULL) != RES_OK) {
|
210 | 224 | return false;
|
211 | 225 | } else {
|
212 | 226 | ejected[lun] = true;
|
213 | 227 | }
|
| 228 | + } else { |
| 229 | + // We can only load if it hasn't been ejected. |
| 230 | + return !ejected[lun]; |
| 231 | + } |
| 232 | + } else { |
| 233 | + if (!start) { |
| 234 | + // Stop the unit but don't eject. |
| 235 | + if (disk_ioctl(current_mount, CTRL_SYNC, NULL) != RES_OK) { |
| 236 | + return false; |
| 237 | + } |
| 238 | + } else { |
| 239 | + // Start the unit, but only if not ejected. |
| 240 | + return !ejected[lun]; |
214 | 241 | }
|
215 | 242 | }
|
216 | 243 |
|
|
0 commit comments