17
17
#include " USBHostMSD.h"
18
18
19
19
#if USBHOST_MSD
20
-
21
20
#include " dbg.h"
22
21
23
22
#define CBW_SIGNATURE 0x43425355
29
28
#define GET_MAX_LUN (0xFE )
30
29
#define BO_MASS_STORAGE_RESET (0xFF )
31
30
32
- USBHostMSD::USBHostMSD (const char * rootdir) : FATFileSystem(rootdir )
31
+ USBHostMSD::USBHostMSD ()
33
32
{
34
33
host = USBHost::getHostInst ();
35
- init ();
34
+ /* register an object in FAT */
35
+
36
+ init_usb ();
36
37
}
37
38
38
- void USBHostMSD::init () {
39
+ void USBHostMSD::init_usb ()
40
+ {
39
41
dev_connected = false ;
40
42
dev = NULL ;
41
43
bulk_in = NULL ;
@@ -72,6 +74,11 @@ bool USBHostMSD::connect()
72
74
break ;
73
75
74
76
if (msd_device_found) {
77
+ /* As this is done in a specific thread
78
+ * this lock is taken to avoid to process a disconnection in
79
+ * usb process during the device registering */
80
+ USBHost::Lock Lock (host);
81
+
75
82
bulk_in = dev->getEndpoint (msd_intf, BULK_ENDPOINT, IN);
76
83
bulk_out = dev->getEndpoint (msd_intf, BULK_ENDPOINT, OUT);
77
84
@@ -80,14 +87,14 @@ bool USBHostMSD::connect()
80
87
81
88
USB_INFO (" New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]" , dev->getVid (), dev->getPid (), dev, msd_intf);
82
89
dev->setName (" MSD" , msd_intf);
83
- host->registerDriver (dev, msd_intf, this , &USBHostMSD::init );
90
+ host->registerDriver (dev, msd_intf, this , &USBHostMSD::init_usb );
84
91
85
92
dev_connected = true ;
86
93
return true ;
87
94
}
88
95
} // if()
89
96
} // for()
90
- init ();
97
+ init_usb ();
91
98
return false ;
92
99
}
93
100
@@ -99,9 +106,9 @@ bool USBHostMSD::connect()
99
106
/* virtual*/ bool USBHostMSD::parseInterface (uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) // Must return true if the interface should be parsed
100
107
{
101
108
if ((msd_intf == -1 ) &&
102
- (intf_class == MSD_CLASS) &&
103
- (intf_subclass == 0x06 ) &&
104
- (intf_protocol == 0x50 )) {
109
+ (intf_class == MSD_CLASS) &&
110
+ (intf_subclass == 0x06 ) &&
111
+ (intf_protocol == 0x50 )) {
105
112
msd_intf = intf_nb;
106
113
return true ;
107
114
}
@@ -122,13 +129,15 @@ bool USBHostMSD::connect()
122
129
}
123
130
124
131
125
- int USBHostMSD::testUnitReady () {
132
+ int USBHostMSD::testUnitReady ()
133
+ {
126
134
USB_DBG (" Test unit ready" );
127
135
return SCSITransfer (NULL , 6 , DEVICE_TO_HOST, 0 , 0 );
128
136
}
129
137
130
138
131
- int USBHostMSD::readCapacity () {
139
+ int USBHostMSD::readCapacity ()
140
+ {
132
141
USB_DBG (" Read capacity" );
133
142
uint8_t cmd[10 ] = {0x25 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
134
143
uint8_t result[8 ];
@@ -142,7 +151,8 @@ int USBHostMSD::readCapacity() {
142
151
}
143
152
144
153
145
- int USBHostMSD::SCSIRequestSense () {
154
+ int USBHostMSD::SCSIRequestSense ()
155
+ {
146
156
USB_DBG (" Request sense" );
147
157
uint8_t cmd[6 ] = {0x03 ,0 ,0 ,0 ,18 ,0 };
148
158
uint8_t result[18 ];
@@ -151,7 +161,8 @@ int USBHostMSD::SCSIRequestSense() {
151
161
}
152
162
153
163
154
- int USBHostMSD::inquiry (uint8_t lun, uint8_t page_code) {
164
+ int USBHostMSD::inquiry (uint8_t lun, uint8_t page_code)
165
+ {
155
166
USB_DBG (" Inquiry" );
156
167
uint8_t evpd = (page_code == 0 ) ? 0 : 1 ;
157
168
uint8_t cmd[6 ] = {0x12 , uint8_t ((lun << 5 ) | evpd), page_code, 0 , 36 , 0 };
@@ -174,7 +185,8 @@ int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) {
174
185
return status;
175
186
}
176
187
177
- int USBHostMSD::checkResult (uint8_t res, USBEndpoint * ep) {
188
+ int USBHostMSD::checkResult (uint8_t res, USBEndpoint * ep)
189
+ {
178
190
// if ep stalled: send clear feature
179
191
if (res == USB_TYPE_STALL_ERROR) {
180
192
res = host->controlWrite ( dev,
@@ -194,7 +206,8 @@ int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) {
194
206
}
195
207
196
208
197
- int USBHostMSD::SCSITransfer (uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) {
209
+ int USBHostMSD::SCSITransfer (uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len)
210
+ {
198
211
199
212
int res = 0 ;
200
213
@@ -277,7 +290,8 @@ int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t
277
290
}
278
291
279
292
280
- int USBHostMSD::dataTransfer (uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) {
293
+ int USBHostMSD::dataTransfer (uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction)
294
+ {
281
295
uint8_t cmd[10 ];
282
296
memset (cmd,0 ,10 );
283
297
cmd[0 ] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A ;
@@ -293,20 +307,20 @@ int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int
293
307
return SCSITransfer (cmd, 10 , direction, buf, blockSize*nbBlock);
294
308
}
295
309
296
- int USBHostMSD::getMaxLun () {
310
+ int USBHostMSD::getMaxLun ()
311
+ {
297
312
uint8_t buf[1 ], res;
298
313
res = host->controlRead ( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
299
314
0xfe , 0 , msd_intf, buf, 1 );
300
315
USB_DBG (" max lun: %d" , buf[0 ]);
301
316
return res;
302
317
}
303
318
304
- int USBHostMSD::disk_initialize () {
319
+ int USBHostMSD::init ()
320
+ {
305
321
USB_DBG (" FILESYSTEM: init" );
306
- uint16_t i, timeout = 10 ;
307
-
322
+ uint16_t i, timeout = 10 , ret;
308
323
getMaxLun ();
309
-
310
324
for (i = 0 ; i < timeout; i++) {
311
325
Thread::wait (100 );
312
326
if (!testUnitReady ())
@@ -323,44 +337,70 @@ int USBHostMSD::disk_initialize() {
323
337
return readCapacity ();
324
338
}
325
339
326
- int USBHostMSD::disk_write (const uint8_t * buffer, uint32_t block_number, uint32_t count) {
327
- USB_DBG (" FILESYSTEM: write block: %lld, count: %d" , block_number, count);
340
+ int USBHostMSD::program (const void *buffer, bd_addr_t addr, bd_size_t size)
341
+ {
342
+ uint32_t block_number, count;
343
+ uint8_t *buf = (uint8_t *)buffer;
328
344
if (!disk_init) {
329
- disk_initialize ();
345
+ init ();
330
346
}
331
- if (!disk_init)
347
+ if (!disk_init) {
332
348
return -1 ;
349
+ }
350
+ block_number = addr / blockSize;
351
+ count = size /blockSize;
352
+
333
353
for (uint32_t b = block_number; b < block_number + count; b++) {
334
- if (dataTransfer (( uint8_t *)buffer , b, 1 , HOST_TO_DEVICE))
354
+ if (dataTransfer (buf , b, 1 , HOST_TO_DEVICE))
335
355
return -1 ;
336
- buffer += 512 ;
356
+ buf += blockSize ;
337
357
}
338
358
return 0 ;
339
359
}
340
360
341
- int USBHostMSD::disk_read (uint8_t * buffer, uint32_t block_number, uint32_t count) {
342
- USB_DBG (" FILESYSTEM: read block: %lld, count: %d" , block_number, count);
361
+ int USBHostMSD::read (void *buffer, bd_addr_t addr, bd_size_t size)
362
+ {
363
+ uint32_t block_number, count;
364
+ uint8_t *buf = (uint8_t *)buffer;
343
365
if (!disk_init) {
344
- disk_initialize ();
366
+ init ();
345
367
}
346
- if (!disk_init)
368
+ if (!disk_init) {
347
369
return -1 ;
370
+ }
371
+ block_number = addr / blockSize;
372
+ count = size / blockSize;
373
+
348
374
for (uint32_t b = block_number; b < block_number + count; b++) {
349
- if (dataTransfer (( uint8_t *)buffer , b, 1 , DEVICE_TO_HOST))
375
+ if (dataTransfer (buf , b, 1 , DEVICE_TO_HOST))
350
376
return -1 ;
351
- buffer += 512 ;
377
+ buf += blockSize ;
352
378
}
353
379
return 0 ;
354
380
}
355
381
356
- uint32_t USBHostMSD::disk_sectors () {
357
- USB_DBG ( " FILESYSTEM: sectors " );
358
- if (!disk_init) {
359
- disk_initialize ();
360
- }
361
- if (!disk_init)
362
- return 0 ;
363
- return blockCount ;
382
+ int USBHostMSD::erase ( bd_addr_t addr, bd_size_t size)
383
+ {
384
+ return 0 ;
385
+ }
386
+
387
+ bd_size_t USBHostMSD::get_read_size () const
388
+ {
389
+ return (disk_init ? ( bd_size_t )blockSize : - 1 ) ;
364
390
}
365
391
392
+ bd_size_t USBHostMSD::get_program_size () const
393
+ {
394
+ return (disk_init ? (bd_size_t )blockSize : -1 );
395
+ }
396
+ bd_size_t USBHostMSD::get_erase_size () const
397
+ {
398
+ return (disk_init ? (bd_size_t )blockSize : -1 );
399
+ }
400
+
401
+ bd_size_t USBHostMSD::size () const
402
+ {
403
+ USB_DBG (" FILESYSTEM: size " );
404
+ return (disk_init ? (bd_size_t )blockSize : 0 );
405
+ }
366
406
#endif
0 commit comments