// Called in ISR context when a data has been transferred bool USBMSD::EP2_IN_callback() { switch (stage) { // the device has to send data to the host case PROCESS_CBW: switch (cbw.CB[0]) { case READ10: case READ12: memoryRead(); break; } break; //the device has to send a CSW case SEND_CSW: sendCSW(); break; // an error has occured case ERROR: stallEndpoint(EPBULK_IN); sendCSW(); break; // the host has received the CSW -> we wait a CBW case WAIT_CSW: stage = READ_CBW; break; case READ_CBW: break; } return true; }
void USBMSD::memoryVerify (uint8_t * buf, uint16_t size) { uint32_t n; if ((addr + size) > MemorySize) { size = MemorySize - addr; stage = ERROR; stallEndpoint(EPBULK_OUT); } // beginning of a new block -> load a whole block in RAM if (!(addr%BlockSize)) disk_read(page, addr/BlockSize); // info are in RAM -> no need to re-read memory for (n = 0; n < size; n++) { if (page[addr%BlockSize + n] != buf[n]) { memOK = false; break; } } addr += size; length -= size; csw.DataResidue -= size; if ( !length || (stage != PROCESS_CBW)) { csw.Status = (memOK && (stage == PROCESS_CBW)) ? CSW_PASSED : CSW_FAILED; sendCSW(); } }
void USBMSD::memoryWrite (uint8_t * buf, uint16_t size) { if ((addr + size) > MemorySize) { size = MemorySize - addr; stage = ERROR; stallEndpoint(EPBULK_OUT); } // we fill an array in RAM of 1 block before writing it in memory for (int i = 0; i < size; i++) page[addr%BlockSize + i] = buf[i]; // if the array is filled, write it in memory if (!((addr + size)%BlockSize)) { if (!(disk_status() & WRITE_PROTECT)) { disk_write(page, addr/BlockSize); } } addr += size; length -= size; csw.DataResidue -= size; if ((!length) || (stage != PROCESS_CBW)) { csw.Status = (stage == ERROR) ? CSW_FAILED : CSW_PASSED; sendCSW(); } }
// Called in ISR context called when a data is received bool USBMSD::EP2_OUT_callback() { uint32_t size = 0; uint8_t buf[MAX_PACKET_SIZE_EPBULK]; readEP(EPBULK_OUT, buf, &size, MAX_PACKET_SIZE_EPBULK); switch (stage) { // the device has to decode the CBW received case READ_CBW: CBWDecode(buf, size); break; // the device has to receive data from the host case PROCESS_CBW: switch (cbw.CB[0]) { case WRITE10: case WRITE12: memoryWrite(buf, size); break; case VERIFY10: memoryVerify(buf, size); break; } break; // an error has occured: stall endpoint and send CSW default: stallEndpoint(EPBULK_OUT); csw.Status = CSW_ERROR; sendCSW(); break; } //reactivate readings on the OUT bulk endpoint readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); return true; }
bool USBCDCMSC::infoTransfer (void) { uint32_t n; // Logical Block Address of First Block n = (cbw.CB[2] << 24) | (cbw.CB[3] << 16) | (cbw.CB[4] << 8) | (cbw.CB[5] << 0); addr = n * BlockSize; // Number of Blocks to transfer switch (cbw.CB[0]) { case READ10: case WRITE10: case VERIFY10: n = (cbw.CB[7] << 8) | (cbw.CB[8] << 0); break; case READ12: case WRITE12: n = (cbw.CB[6] << 24) | (cbw.CB[7] << 16) | (cbw.CB[8] << 8) | (cbw.CB[9] << 0); break; } length = n * BlockSize; if (!cbw.DataLength) { // host requests no data csw.Status = CSW_FAILED; sendCSW(); return false; } if (cbw.DataLength != length) { if ((cbw.Flags & 0x80) != 0) { stallEndpoint(MSDBULK_IN); } else { stallEndpoint(MSDBULK_OUT); } csw.Status = CSW_FAILED; sendCSW(); return false; } return true; }
void USBCDCMSC::testUnitReady (void) { if (cbw.DataLength != 0) { if ((cbw.Flags & 0x80) != 0) { stallEndpoint(MSDBULK_IN); } else { stallEndpoint(MSDBULK_OUT); } } csw.Status = CSW_PASSED; sendCSW(); }
static void testUnitReady(void) { if (cbw.DataLength != 0) { if ((cbw.Flags & 0x80) != 0) { SYS_LOG_DBG("TUR - BI - STALL"); usb_ep_set_stall(EPBULK_IN); } else { SYS_LOG_DBG("TUR - BO - STALL"); usb_ep_set_stall(EPBULK_OUT); } } csw.Status = CSW_PASSED; sendCSW(); }
void USBCDCMSC::CBWDecode(uint8_t * buf, uint16_t size) { if (size == sizeof(cbw)) { memcpy((uint8_t *)&cbw, buf, size); if (cbw.Signature == CBW_Signature) { csw.Tag = cbw.Tag; csw.DataResidue = cbw.DataLength; if ((cbw.CBLength < 1) || (cbw.CBLength > 16) ) { fail(); } else { switch (cbw.CB[0]) { case TEST_UNIT_READY: testUnitReady(); break; case REQUEST_SENSE: requestSense(); break; case INQUIRY: inquiryRequest(); break; case MODE_SENSE6: modeSense6(); break; case READ_FORMAT_CAPACITIES: readFormatCapacity(); break; case MEDIA_REMOVAL: mediaRemoval(); break; case READ_CAPACITY: readCapacity(); break; case READ10: case READ12: if (infoTransfer()) { if ((cbw.Flags & 0x80)) { stage = PROCESS_CBW; memoryRead(); } else { stallEndpoint(MSDBULK_OUT); csw.Status = CSW_ERROR; sendCSW(); } } break; case WRITE10: case WRITE12: if (infoTransfer()) { if (!(cbw.Flags & 0x80)) { stage = PROCESS_CBW; } else { stallEndpoint(MSDBULK_IN); csw.Status = CSW_ERROR; sendCSW(); } } break; case VERIFY10: if (!(cbw.CB[1] & 0x02)) { csw.Status = CSW_PASSED; sendCSW(); break; } if (infoTransfer()) { if (!(cbw.Flags & 0x80)) { stage = PROCESS_CBW; memOK = true; } else { stallEndpoint(MSDBULK_IN); csw.Status = CSW_ERROR; sendCSW(); } } break; default: fail(); break; } } } } }
void USBCDCMSC::fail() { csw.Status = CSW_FAILED; sendCSW(); }
void USBCDCMSC::mediaRemoval() { csw.Status = CSW_PASSED; sendCSW(); }
void USBMSD::fail() { csw.Status = CSW_FAILED; sendCSW(); }