Skip to content

Commit 3348aa5

Browse files
committed
add msc setID
1 parent 4324569 commit 3348aa5

File tree

4 files changed

+47
-18
lines changed

4 files changed

+47
-18
lines changed

examples/msc_ramdisk/msc_ramdisk.ino

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ Adafruit_USBD_MSC usb_msc;
3030
// the setup function runs once when you press reset or power the board
3131
void setup()
3232
{
33-
// Set disk size and callback for Logical Unit 0 (LUN 0)
33+
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
34+
usb_msc.setID(0, "Adafruit", "Mass Storage", "1.0");
35+
36+
// Set disk size
3437
usb_msc.setCapacity(0, DISK_BLOCK_NUM, DISK_BLOCK_SIZE);
38+
39+
// Set callback
3540
usb_msc.setCallback(0, ram_read_cb, ram_write_cb, ram_flush_cb);
3641

3742
usb_msc.begin();

examples/msc_ramdisk_dual/msc_ramdisk_dual.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ void setup()
3333
usb_msc.setMaxLun(2);
3434

3535
// Set disk size and callback for Logical Unit 0 (LUN 0)
36+
usb_msc.setID(0, "Adafruit", "Lun0", "1.0");
3637
usb_msc.setCapacity(0, DISK_BLOCK_NUM, DISK_BLOCK_SIZE);
3738
usb_msc.setCallback(0, ram0_read_cb, ram0_write_cb, ram0_flush_cb);
3839

3940
// Set disk size and callback for Logical Unit 1 (LUN 1)
41+
usb_msc.setID(1, "Adafruit", "Lun1", "1.0");
4042
usb_msc.setCapacity(1, DISK_BLOCK_NUM, DISK_BLOCK_SIZE);
4143
usb_msc.setCallback(1, ram1_read_cb, ram1_write_cb, ram1_flush_cb);
4244

src/Adafruit_USBD_MSC.cpp

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ uint8_t Adafruit_USBD_MSC::getMaxLun(void)
5656
return _maxlun;
5757
}
5858

59+
void Adafruit_USBD_MSC::setID(uint8_t lun, const char* vendor_id, const char* product_id, const char* product_rev)
60+
{
61+
_lun[lun]._inquiry_vid = vendor_id;
62+
_lun[lun]._inquiry_pid = product_id;
63+
_lun[lun]._inquiry_rev = product_rev;
64+
}
65+
5966
void Adafruit_USBD_MSC::setCapacity(uint8_t lun, uint32_t block_count, uint16_t block_size)
6067
{
6168
_lun[lun].block_count = block_count;
@@ -88,12 +95,36 @@ extern "C"
8895
{
8996

9097
// Invoked to determine max LUN
91-
uint8_t tud_msc_maxlun_cb(void)
98+
uint8_t tud_msc_get_maxlun_cb(void)
9299
{
93100
if (!_msc_dev) return 0;
94101
return _msc_dev->getMaxLun();
95102
}
96103

104+
// Invoked when received SCSI_CMD_INQUIRY
105+
// Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
106+
void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
107+
{
108+
if (!_msc_dev) return;
109+
110+
const char* vid = (_msc_dev->_lun[lun]._inquiry_vid ? _msc_dev->_lun[lun]._inquiry_vid : "Adafruit");
111+
const char* pid = (_msc_dev->_lun[lun]._inquiry_pid ? _msc_dev->_lun[lun]._inquiry_pid : "Mass Storage");
112+
const char* rev = (_msc_dev->_lun[lun]._inquiry_rev ? _msc_dev->_lun[lun]._inquiry_rev : "1.0");
113+
114+
memcpy(vendor_id , vid, tu_min32(strlen(vid), 8 ));
115+
memcpy(product_id , pid, tu_min32(strlen(pid), 16));
116+
memcpy(product_rev, rev, tu_min32(strlen(rev), 4 ));
117+
}
118+
119+
// Invoked when received Test Unit Ready command.
120+
// return true allowing host to read/write this LUN e.g SD card inserted
121+
bool tud_msc_test_unit_ready_cb(uint8_t lun)
122+
{
123+
(void) lun;
124+
125+
return true; // RAM disk is always ready
126+
}
127+
97128
// Callback invoked to determine disk's size
98129
void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size)
99130
{
@@ -111,27 +142,11 @@ int32_t tud_msc_scsi_cb (uint8_t lun, const uint8_t scsi_cmd[16], void* buffer,
111142

112143
switch ( scsi_cmd[0] )
113144
{
114-
case SCSI_CMD_TEST_UNIT_READY:
115-
// Command that host uses to check our readiness before sending other commands
116-
resplen = 0;
117-
break;
118-
119145
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
120146
// Host is about to read/write etc ... better not to disconnect disk
121147
resplen = 0;
122148
break;
123149

124-
case SCSI_CMD_START_STOP_UNIT:
125-
// Host try to eject/safe remove/poweroff us. We could safely disconnect with disk storage, or go into lower power
126-
/* scsi_start_stop_unit_t const * start_stop = (scsi_start_stop_unit_t const *) scsi_cmd;
127-
// Start bit = 0 : low power mode, if load_eject = 1 : unmount disk storage as well
128-
// Start bit = 1 : Ready mode, if load_eject = 1 : mount disk storage
129-
start_stop->start;
130-
start_stop->load_eject;
131-
*/
132-
resplen = 0;
133-
break;
134-
135150
default:
136151
// Set Sense = Invalid Command Operation
137152
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);

src/Adafruit_USBD_MSC.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Adafruit_USBD_MSC : Adafruit_USBD_Interface
4141
void setMaxLun(uint8_t maxlun);
4242
uint8_t getMaxLun(void);
4343

44+
void setID(uint8_t lun, const char* vendor_id, const char* product_id, const char* product_rev);
45+
4446
void setCapacity(uint8_t lun, uint32_t block_count, uint16_t block_size);
4547
void getCapacity(uint8_t lun, uint32_t* block_count, uint16_t* block_size);
4648

@@ -58,13 +60,18 @@ class Adafruit_USBD_MSC : Adafruit_USBD_Interface
5860

5961
uint32_t block_count;
6062
uint16_t block_size;
63+
64+
const char* _inquiry_vid;
65+
const char* _inquiry_pid;
66+
const char* _inquiry_rev;
6167
} _lun[MAX_LUN];
6268

6369
uint8_t _maxlun;
6470

6571
friend int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize);
6672
friend int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize);
6773
friend void tud_msc_write10_complete_cb (uint8_t lun);
74+
friend void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]);
6875
};
6976

7077
#endif /* ADAFRUIT_USBD_MSC_H_ */

0 commit comments

Comments
 (0)