Skip to content

Commit d30a9a6

Browse files
committed
USHostMSD: rely on BlockDevice
1 parent 0a3b256 commit d30a9a6

File tree

2 files changed

+103
-59
lines changed

2 files changed

+103
-59
lines changed

features/unsupported/USBHost/USBHostMSD/USBHostMSD.cpp

Lines changed: 76 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "USBHostMSD.h"
1818

1919
#if USBHOST_MSD
20-
2120
#include "dbg.h"
2221

2322
#define CBW_SIGNATURE 0x43425355
@@ -29,13 +28,16 @@
2928
#define GET_MAX_LUN (0xFE)
3029
#define BO_MASS_STORAGE_RESET (0xFF)
3130

32-
USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir)
31+
USBHostMSD::USBHostMSD()
3332
{
3433
host = USBHost::getHostInst();
35-
init();
34+
/* register an object in FAT */
35+
36+
init_usb();
3637
}
3738

38-
void USBHostMSD::init() {
39+
void USBHostMSD::init_usb()
40+
{
3941
dev_connected = false;
4042
dev = NULL;
4143
bulk_in = NULL;
@@ -80,14 +82,14 @@ bool USBHostMSD::connect()
8082

8183
USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf);
8284
dev->setName("MSD", msd_intf);
83-
host->registerDriver(dev, msd_intf, this, &USBHostMSD::init);
85+
host->registerDriver(dev, msd_intf, this, &USBHostMSD::init_usb);
8486

8587
dev_connected = true;
8688
return true;
8789
}
8890
} //if()
8991
} //for()
90-
init();
92+
init_usb();
9193
return false;
9294
}
9395

@@ -99,9 +101,9 @@ bool USBHostMSD::connect()
99101
/*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
100102
{
101103
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)) {
105107
msd_intf = intf_nb;
106108
return true;
107109
}
@@ -122,13 +124,15 @@ bool USBHostMSD::connect()
122124
}
123125

124126

125-
int USBHostMSD::testUnitReady() {
127+
int USBHostMSD::testUnitReady()
128+
{
126129
USB_DBG("Test unit ready");
127130
return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0);
128131
}
129132

130133

131-
int USBHostMSD::readCapacity() {
134+
int USBHostMSD::readCapacity()
135+
{
132136
USB_DBG("Read capacity");
133137
uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0};
134138
uint8_t result[8];
@@ -142,7 +146,8 @@ int USBHostMSD::readCapacity() {
142146
}
143147

144148

145-
int USBHostMSD::SCSIRequestSense() {
149+
int USBHostMSD::SCSIRequestSense()
150+
{
146151
USB_DBG("Request sense");
147152
uint8_t cmd[6] = {0x03,0,0,0,18,0};
148153
uint8_t result[18];
@@ -151,7 +156,8 @@ int USBHostMSD::SCSIRequestSense() {
151156
}
152157

153158

154-
int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) {
159+
int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code)
160+
{
155161
USB_DBG("Inquiry");
156162
uint8_t evpd = (page_code == 0) ? 0 : 1;
157163
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) {
174180
return status;
175181
}
176182

177-
int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) {
183+
int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep)
184+
{
178185
// if ep stalled: send clear feature
179186
if (res == USB_TYPE_STALL_ERROR) {
180187
res = host->controlWrite( dev,
@@ -194,7 +201,8 @@ int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) {
194201
}
195202

196203

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+
{
198206

199207
int res = 0;
200208

@@ -277,7 +285,8 @@ int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t
277285
}
278286

279287

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+
{
281290
uint8_t cmd[10];
282291
memset(cmd,0,10);
283292
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
293302
return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock);
294303
}
295304

296-
int USBHostMSD::getMaxLun() {
305+
int USBHostMSD::getMaxLun()
306+
{
297307
uint8_t buf[1], res;
298308
res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
299309
0xfe, 0, msd_intf, buf, 1);
300310
USB_DBG("max lun: %d", buf[0]);
301311
return res;
302312
}
303313

304-
int USBHostMSD::disk_initialize() {
314+
int USBHostMSD::init()
315+
{
305316
USB_DBG("FILESYSTEM: init");
306-
uint16_t i, timeout = 10;
307-
317+
uint16_t i, timeout = 10, ret;
308318
getMaxLun();
309-
310319
for (i = 0; i < timeout; i++) {
311320
Thread::wait(100);
312321
if (!testUnitReady())
@@ -323,44 +332,70 @@ int USBHostMSD::disk_initialize() {
323332
return readCapacity();
324333
}
325334

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;
328339
if (!disk_init) {
329-
disk_initialize();
340+
init();
330341
}
331-
if (!disk_init)
342+
if (!disk_init) {
332343
return -1;
344+
}
345+
block_number = addr / blockSize;
346+
count = size /blockSize;
347+
333348
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))
335350
return -1;
336-
buffer += 512;
351+
buf += blockSize;
337352
}
338353
return 0;
339354
}
340355

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;
343360
if (!disk_init) {
344-
disk_initialize();
361+
init();
345362
}
346-
if (!disk_init)
363+
if (!disk_init) {
347364
return -1;
365+
}
366+
block_number = addr / blockSize;
367+
count = size / blockSize;
368+
348369
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))
350371
return -1;
351-
buffer += 512;
372+
buf += blockSize;
352373
}
353374
return 0;
354375
}
355376

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);
364385
}
365386

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+
}
366401
#endif

features/unsupported/USBHost/USBHostMSD/USBHostMSD.h

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,26 @@
2323

2424
#include "USBHost.h"
2525
#include "FATFileSystem.h"
26+
#include "BlockDevice.h"
2627

2728
/**
2829
* A class to communicate a USB flash disk
2930
*/
30-
class USBHostMSD : public IUSBEnumerator, public FATFileSystem {
31+
class USBHostMSD : public IUSBEnumerator, public BlockDevice
32+
{
3133
public:
3234
/**
33-
* Constructor
34-
*
35-
* @param rootdir mount name
36-
*/
37-
USBHostMSD(const char * rootdir);
35+
* Constructor
36+
*
37+
* @param rootdir mount name
38+
*/
39+
USBHostMSD();
3840

3941
/**
40-
* Check if a MSD device is connected
41-
*
42-
* @return true if a MSD device is connected
43-
*/
42+
* Check if a MSD device is connected
43+
*
44+
* @return true if a MSD device is connected
45+
*/
4446
bool connected();
4547

4648
/**
@@ -49,20 +51,27 @@ class USBHostMSD : public IUSBEnumerator, public FATFileSystem {
4951
* @return true if connection was successful
5052
*/
5153
bool connect();
54+
virtual int init();
55+
virtual int deinit()
56+
{
57+
return BD_ERROR_OK;
58+
};
59+
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
60+
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
61+
virtual int erase(bd_addr_t addr, bd_size_t size);
62+
virtual bd_size_t get_read_size() const;
63+
virtual bd_size_t get_program_size() const;
64+
virtual bd_size_t get_erase_size() const;
65+
virtual bd_size_t size() const;
66+
67+
5268

5369
protected:
5470
//From IUSBEnumerator
5571
virtual void setVidPid(uint16_t vid, uint16_t pid);
5672
virtual bool 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
5773
virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
5874

59-
// From FATFileSystem
60-
virtual int disk_initialize();
61-
virtual int disk_status() {return 0;};
62-
virtual int disk_read(uint8_t* buffer, uint32_t sector, uint32_t count);
63-
virtual int disk_write(const uint8_t* buffer, uint32_t sector, uint32_t count);
64-
virtual int disk_sync() {return 0;};
65-
virtual uint32_t disk_sectors();
6675

6776
private:
6877
USBHost * host;
@@ -110,7 +119,7 @@ class USBHostMSD : public IUSBEnumerator, public FATFileSystem {
110119
bool msd_device_found;
111120
bool disk_init;
112121

113-
void init();
122+
void init_usb();
114123

115124
};
116125

0 commit comments

Comments
 (0)