rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Data Structures | Public Member Functions | Protected Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes
MassStorageController Class Reference

#include <mass_storage_device.h>

Inheritance diagram for MassStorageController:
Inheritance graph
[legend]
Collaboration diagram for MassStorageController:
Collaboration graph
[legend]

Data Structures

struct  LunEntry
 

Public Member Functions

 MassStorageController (USBDriver *usb)
 
void attachLun (uint8_t lunIndex, BaseBlockDevice *blkdev, uint8_t *blkbuf, size_t blkbufsize, const scsi_inquiry_response_t *inquiry, const scsi_unit_serial_number_inquiry_response_t *serialInquiry)
 
- Public Member Functions inherited from ThreadController< USB_MSD_THREAD_WA_SIZE >
 ThreadController (const char *name, tprio_t priority)
 
void start ()
 Start the thread.
 
void stop ()
 Request thread termination and waits for termination.
 

Protected Member Functions

void ThreadTask () override
 
- Protected Member Functions inherited from ThreadController< USB_MSD_THREAD_WA_SIZE >
void main () override
 

Private Member Functions

void sendCsw (uint8_t status, uint32_t residue)
 

Static Private Member Functions

static bool cbwValid (const msd_cbw_t &cbw, msg_t status)
 
static bool cbwMeaningful (const msd_cbw_t &cbw)
 

Private Attributes

usbmsdstate_t m_state
 
USBDriver *const m_usb
 
msd_cbw_t m_cbw
 
msd_csw_t m_csw
 
SCSITransport m_scsiTransport
 
usb_scsi_transport_handler_t m_scsiTransportHandler
 
chibios_rt::Mutex m_lunMutex
 
LunEntry m_luns [USB_MSD_LUN_COUNT]
 

Additional Inherited Members

- Protected Attributes inherited from ThreadController< USB_MSD_THREAD_WA_SIZE >
const char *const m_name
 

Detailed Description

Definition at line 15 of file mass_storage_device.h.

Constructor & Destructor Documentation

◆ MassStorageController()

MassStorageController::MassStorageController ( USBDriver *  usb)

Definition at line 76 of file mass_storage_device.cpp.

77 : ThreadController("MSD", MSD_THD_PRIO)
78 , m_usb(usb) {
79 for (size_t i = 0; i < USB_MSD_LUN_COUNT; i++) {
80 scsiObjectInit(&m_luns[i].target);
81 }
82
83 m_scsiTransportHandler.usbp = usb;
84 m_scsiTransportHandler.ep = USB_MSD_DATA_EP;
90}
LunEntry m_luns[USB_MSD_LUN_COUNT]
usb_scsi_transport_handler_t m_scsiTransportHandler
A base class for a controller that requires its own thread.
static uint32_t scsi_transport_transmit_start(const SCSITransport *transport, const uint8_t *data, size_t len)
static uint32_t scsi_transport_transmit_wait(const SCSITransport *transport)
static uint32_t scsi_transport_receive(const SCSITransport *transport, uint8_t *data, size_t len)
static uint32_t scsi_transport_transmit(const SCSITransport *transport, const uint8_t *data, size_t len)
Here is the call graph for this function:

Member Function Documentation

◆ attachLun()

void MassStorageController::attachLun ( uint8_t  lunIndex,
BaseBlockDevice *  blkdev,
uint8_t *  blkbuf,
size_t  blkbufsize,
const scsi_inquiry_response_t *  inquiry,
const scsi_unit_serial_number_inquiry_response_t *  serialInquiry 
)

Definition at line 182 of file mass_storage_device.cpp.

185 {
186 chibios_rt::MutexLocker lock(m_lunMutex);
187
188 auto& lun = m_luns[lunIndex];
189
190
191 if (NULL == inquiry) {
192 lun.config.inquiry_response = &default_scsi_inquiry_response;
193 }
194 else {
195 lun.config.inquiry_response = inquiry;
196 }
197 if (NULL == serialInquiry) {
198 lun.config.unit_serial_number_inquiry_response = &default_scsi_unit_serial_number_inquiry_response;
199 }
200 else {
201 lun.config.unit_serial_number_inquiry_response = serialInquiry;
202 }
203 lun.config.blkbuf = blkbuf;
204 lun.config.blkbufsize = blkbufsize;
205 lun.config.blkdev = blkdev;
206 lun.config.transport = &m_scsiTransport;
207
208 scsiStart(&lun.target, &lun.config);
209}
chibios_rt::Mutex m_lunMutex
static const scsi_unit_serial_number_inquiry_response_t default_scsi_unit_serial_number_inquiry_response
Hardcoded default SCSI unit serial number inquiry response structure.
static const scsi_inquiry_response_t default_scsi_inquiry_response
Hardcoded default SCSI inquiry response structure.
uint8_t blkbuf[4 *MMCSD_BLOCK_SIZE]
Definition mmc_card.cpp:292
SCSITargetConfig config

Referenced by attachMsdSdCard(), deattachMsdSdCard(), and initUsbMsd().

Here is the caller graph for this function:

◆ cbwMeaningful()

bool MassStorageController::cbwMeaningful ( const msd_cbw_t &  cbw)
staticprivate

Definition at line 128 of file mass_storage_device.cpp.

128 {
129 if ((cbw.cmd_len & CBW_CMD_LEN_RESERVED_MASK) != 0) {
130 return false;
131 }
132
133 if ((cbw.flags & CBW_FLAGS_RESERVED_MASK) != 0) {
134 return false;
135 }
136
137 if (cbw.lun >= USB_MSD_LUN_COUNT) {
138 return false;
139 }
140
141 return true;
142}

Referenced by ThreadTask().

Here is the caller graph for this function:

◆ cbwValid()

bool MassStorageController::cbwValid ( const msd_cbw_t &  cbw,
msg_t  status 
)
staticprivate

Definition at line 114 of file mass_storage_device.cpp.

114 {
115 // check valid size
116 if (sizeof(msd_cbw_t) != recvd) {
117 return false;
118 }
119
120 // Check valid signature
121 if (cbw.signature != MSD_CBW_SIGNATURE) {
122 return false;
123 }
124
125 return true;
126}

Referenced by ThreadTask().

Here is the caller graph for this function:

◆ sendCsw()

void MassStorageController::sendCsw ( uint8_t  status,
uint32_t  residue 
)
private

Definition at line 144 of file mass_storage_device.cpp.

144 {
145 m_csw.signature = MSD_CSW_SIGNATURE;
146 m_csw.data_residue = residue;
147 m_csw.tag = m_cbw.tag;
148 m_csw.status = status;
149
150 usbTransmit(m_usb, USB_MSD_DATA_EP, (uint8_t*)&m_csw, sizeof(m_csw));
151}

Referenced by ThreadTask().

Here is the caller graph for this function:

◆ ThreadTask()

void MassStorageController::ThreadTask ( )
overrideprotectedvirtual

Implements ThreadController< USB_MSD_THREAD_WA_SIZE >.

Definition at line 92 of file mass_storage_device.cpp.

92 {
93 while (!chThdShouldTerminateX()) {
94 const msg_t status = usbReceive(m_usb, USB_MSD_DATA_EP, (uint8_t*)&m_cbw, sizeof(m_cbw));
95
96 if (MSG_RESET == status) {
97 chThdSleepMilliseconds(50);
98 continue;
99 }
100
101 if (cbwValid(m_cbw, status) && cbwMeaningful(m_cbw)) {
102 auto target = &m_luns[m_cbw.lun].target;
103 if (SCSI_SUCCESS == scsiExecCmd(target, m_cbw.cmd_data)) {
104 sendCsw(CSW_STATUS_PASSED, 0);
105 } else {
106 sendCsw(CSW_STATUS_FAILED, scsiResidue(target));
107 }
108 } else {
109 // ignore incorrect CBW
110 }
111 }
112}
void sendCsw(uint8_t status, uint32_t residue)
static bool cbwValid(const msd_cbw_t &cbw, msg_t status)
static bool cbwMeaningful(const msd_cbw_t &cbw)
SCSITarget target
Here is the call graph for this function:

Field Documentation

◆ m_cbw

msd_cbw_t MassStorageController::m_cbw
private

Definition at line 36 of file mass_storage_device.h.

Referenced by sendCsw(), and ThreadTask().

◆ m_csw

msd_csw_t MassStorageController::m_csw
private

Definition at line 37 of file mass_storage_device.h.

Referenced by sendCsw().

◆ m_lunMutex

chibios_rt::Mutex MassStorageController::m_lunMutex
private

Definition at line 42 of file mass_storage_device.h.

Referenced by attachLun().

◆ m_luns

LunEntry MassStorageController::m_luns[USB_MSD_LUN_COUNT]
private

Definition at line 49 of file mass_storage_device.h.

Referenced by attachLun(), MassStorageController(), and ThreadTask().

◆ m_scsiTransport

SCSITransport MassStorageController::m_scsiTransport
private

Definition at line 39 of file mass_storage_device.h.

Referenced by attachLun(), and MassStorageController().

◆ m_scsiTransportHandler

usb_scsi_transport_handler_t MassStorageController::m_scsiTransportHandler
private

Definition at line 40 of file mass_storage_device.h.

Referenced by MassStorageController().

◆ m_state

usbmsdstate_t MassStorageController::m_state
private

Definition at line 31 of file mass_storage_device.h.

◆ m_usb

USBDriver* const MassStorageController::m_usb
private

Definition at line 33 of file mass_storage_device.h.

Referenced by sendCsw(), and ThreadTask().


The documentation for this class was generated from the following files: