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 ;
@@ -80,14 +82,14 @@ bool USBHostMSD::connect()
80
82
81
83
USB_INFO (" New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]" , dev->getVid (), dev->getPid (), dev, msd_intf);
82
84
dev->setName (" MSD" , msd_intf);
83
- host->registerDriver (dev, msd_intf, this , &USBHostMSD::init );
85
+ host->registerDriver (dev, msd_intf, this , &USBHostMSD::init_usb );
84
86
85
87
dev_connected = true ;
86
88
return true ;
87
89
}
88
90
} // if()
89
91
} // for()
90
- init ();
92
+ init_usb ();
91
93
return false ;
92
94
}
93
95
@@ -99,9 +101,9 @@ bool USBHostMSD::connect()
99
101
/* 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
102
{
101
103
if ((msd_intf == -1 ) &&
102
- (intf_class == MSD_CLASS) &&
103
- (intf_subclass == 0x06 ) &&
104
- (intf_protocol == 0x50 )) {
104
+ (intf_class == MSD_CLASS) &&
105
+ (intf_subclass == 0x06 ) &&
106
+ (intf_protocol == 0x50 )) {
105
107
msd_intf = intf_nb;
106
108
return true ;
107
109
}
@@ -122,13 +124,15 @@ bool USBHostMSD::connect()
122
124
}
123
125
124
126
125
- int USBHostMSD::testUnitReady () {
127
+ int USBHostMSD::testUnitReady ()
128
+ {
126
129
USB_DBG (" Test unit ready" );
127
130
return SCSITransfer (NULL , 6 , DEVICE_TO_HOST, 0 , 0 );
128
131
}
129
132
130
133
131
- int USBHostMSD::readCapacity () {
134
+ int USBHostMSD::readCapacity ()
135
+ {
132
136
USB_DBG (" Read capacity" );
133
137
uint8_t cmd[10 ] = {0x25 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
134
138
uint8_t result[8 ];
@@ -142,7 +146,8 @@ int USBHostMSD::readCapacity() {
142
146
}
143
147
144
148
145
- int USBHostMSD::SCSIRequestSense () {
149
+ int USBHostMSD::SCSIRequestSense ()
150
+ {
146
151
USB_DBG (" Request sense" );
147
152
uint8_t cmd[6 ] = {0x03 ,0 ,0 ,0 ,18 ,0 };
148
153
uint8_t result[18 ];
@@ -151,7 +156,8 @@ int USBHostMSD::SCSIRequestSense() {
151
156
}
152
157
153
158
154
- int USBHostMSD::inquiry (uint8_t lun, uint8_t page_code) {
159
+ int USBHostMSD::inquiry (uint8_t lun, uint8_t page_code)
160
+ {
155
161
USB_DBG (" Inquiry" );
156
162
uint8_t evpd = (page_code == 0 ) ? 0 : 1 ;
157
163
uint8_t cmd[6 ] = {0x12 , uint8_t ((lun << 5 ) | evpd), page_code, 0 , 36 , 0 };
@@ -174,7 +180,8 @@ int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) {
174
180
return status;
175
181
}
176
182
177
- int USBHostMSD::checkResult (uint8_t res, USBEndpoint * ep) {
183
+ int USBHostMSD::checkResult (uint8_t res, USBEndpoint * ep)
184
+ {
178
185
// if ep stalled: send clear feature
179
186
if (res == USB_TYPE_STALL_ERROR) {
180
187
res = host->controlWrite ( dev,
@@ -194,7 +201,8 @@ int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) {
194
201
}
195
202
196
203
197
- int USBHostMSD::SCSITransfer (uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) {
204
+ int USBHostMSD::SCSITransfer (uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len)
205
+ {
198
206
199
207
int res = 0 ;
200
208
@@ -277,7 +285,8 @@ int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t
277
285
}
278
286
279
287
280
- int USBHostMSD::dataTransfer (uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) {
288
+ int USBHostMSD::dataTransfer (uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction)
289
+ {
281
290
uint8_t cmd[10 ];
282
291
memset (cmd,0 ,10 );
283
292
cmd[0 ] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A ;
@@ -293,20 +302,20 @@ int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int
293
302
return SCSITransfer (cmd, 10 , direction, buf, blockSize*nbBlock);
294
303
}
295
304
296
- int USBHostMSD::getMaxLun () {
305
+ int USBHostMSD::getMaxLun ()
306
+ {
297
307
uint8_t buf[1 ], res;
298
308
res = host->controlRead ( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
299
309
0xfe , 0 , msd_intf, buf, 1 );
300
310
USB_DBG (" max lun: %d" , buf[0 ]);
301
311
return res;
302
312
}
303
313
304
- int USBHostMSD::disk_initialize () {
314
+ int USBHostMSD::init ()
315
+ {
305
316
USB_DBG (" FILESYSTEM: init" );
306
- uint16_t i, timeout = 10 ;
307
-
317
+ uint16_t i, timeout = 10 , ret;
308
318
getMaxLun ();
309
-
310
319
for (i = 0 ; i < timeout; i++) {
311
320
Thread::wait (100 );
312
321
if (!testUnitReady ())
@@ -323,44 +332,70 @@ int USBHostMSD::disk_initialize() {
323
332
return readCapacity ();
324
333
}
325
334
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);
335
+ int USBHostMSD::program (const void *buffer, bd_addr_t addr, bd_size_t size)
336
+ {
337
+ uint32_t block_number, count;
338
+ uint8_t *buf = (uint8_t *)buffer;
328
339
if (!disk_init) {
329
- disk_initialize ();
340
+ init ();
330
341
}
331
- if (!disk_init)
342
+ if (!disk_init) {
332
343
return -1 ;
344
+ }
345
+ block_number = addr / blockSize;
346
+ count = size /blockSize;
347
+
333
348
for (uint32_t b = block_number; b < block_number + count; b++) {
334
- if (dataTransfer (( uint8_t *)buffer , b, 1 , HOST_TO_DEVICE))
349
+ if (dataTransfer (buf , b, 1 , HOST_TO_DEVICE))
335
350
return -1 ;
336
- buffer += 512 ;
351
+ buf += blockSize ;
337
352
}
338
353
return 0 ;
339
354
}
340
355
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);
356
+ int USBHostMSD::read (void *buffer, bd_addr_t addr, bd_size_t size)
357
+ {
358
+ uint32_t block_number, count;
359
+ uint8_t *buf = (uint8_t *)buffer;
343
360
if (!disk_init) {
344
- disk_initialize ();
361
+ init ();
345
362
}
346
- if (!disk_init)
363
+ if (!disk_init) {
347
364
return -1 ;
365
+ }
366
+ block_number = addr / blockSize;
367
+ count = size / blockSize;
368
+
348
369
for (uint32_t b = block_number; b < block_number + count; b++) {
349
- if (dataTransfer (( uint8_t *)buffer , b, 1 , DEVICE_TO_HOST))
370
+ if (dataTransfer (buf , b, 1 , DEVICE_TO_HOST))
350
371
return -1 ;
351
- buffer += 512 ;
372
+ buf += blockSize ;
352
373
}
353
374
return 0 ;
354
375
}
355
376
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 ;
377
+ int USBHostMSD::erase ( bd_addr_t addr, bd_size_t size)
378
+ {
379
+ return 0 ;
380
+ }
381
+
382
+ bd_size_t USBHostMSD::get_read_size () const
383
+ {
384
+ return (disk_init ? ( bd_size_t )blockSize : - 1 ) ;
364
385
}
365
386
387
+ bd_size_t USBHostMSD::get_program_size () const
388
+ {
389
+ return (disk_init ? (bd_size_t )blockSize : -1 );
390
+ }
391
+ bd_size_t USBHostMSD::get_erase_size () const
392
+ {
393
+ return (disk_init ? (bd_size_t )blockSize : -1 );
394
+ }
395
+
396
+ bd_size_t USBHostMSD::size () const
397
+ {
398
+ USB_DBG (" FILESYSTEM: size " );
399
+ return (disk_init ? (bd_size_t )blockSize : 0 );
400
+ }
366
401
#endif
0 commit comments