35 uint8_t SCSI_Sense_Head;
36 uint8_t SCSI_Sense_Tail;
38 uint32_t SCSI_blk_size;
39 uint32_t SCSI_blk_nbr;
41 uint32_t SCSI_blk_addr;
42 uint32_t SCSI_blk_len;
53 static int8_t SCSI_TestUnitReady (uint8_t Lun, uint8_t *params);
54 static int8_t SCSI_Inquiry (uint8_t Lun, uint8_t *params);
55 static int8_t SCSI_ReadFormatCapacity (uint8_t Lun, uint8_t *params);
56 static int8_t SCSI_ReadCapacity10 (uint8_t Lun, uint8_t *params);
57 static int8_t SCSI_RequestSense (uint8_t Lun, uint8_t *params);
58 static int8_t SCSI_StartStopUnit (uint8_t Lun, uint8_t *params);
59 static int8_t SCSI_AllowMediumRemoval (uint8_t Lun, uint8_t *params);
60 static int8_t SCSI_ModeSense6 (uint8_t Lun, uint8_t *params);
61 static int8_t SCSI_ModeSense10 (uint8_t Lun, uint8_t *params);
62 static int8_t SCSI_Write10 (uint8_t Lun, uint8_t *params);
63 static int8_t SCSI_Read10 (uint8_t Lun, uint8_t *params);
64 static int8_t SCSI_Verify10 (uint8_t Lun, uint8_t *params);
65 static int8_t SCSI_CheckAddressRange (uint8_t Lun,
70 static int8_t SCSI_Format_Cmd (uint8_t Lun);
95 case SCSI_TEST_UNIT_READY:
96 return SCSI_TestUnitReady(Lun, params);
98 case SCSI_REQUEST_SENSE:
99 return SCSI_RequestSense(Lun, params);
102 return SCSI_Inquiry(Lun, params);
104 case SCSI_START_STOP_UNIT:
105 return SCSI_StartStopUnit(Lun, params);
107 case SCSI_ALLOW_MEDIUM_REMOVAL:
108 return SCSI_AllowMediumRemoval(Lun, params);
110 case SCSI_MODE_SENSE6:
111 return SCSI_ModeSense6(Lun, params);
113 case SCSI_MODE_SENSE10:
114 return SCSI_ModeSense10(Lun, params);
116 case SCSI_READ_FORMAT_CAPACITIES:
117 return SCSI_ReadFormatCapacity(Lun, params);
119 case SCSI_READ_CAPACITY10:
120 return SCSI_ReadCapacity10(Lun, params);
123 return SCSI_Read10(Lun, params);
126 return SCSI_Write10(Lun, params);
129 return SCSI_Verify10(Lun, params);
131 case SCSI_FORMAT_UNIT:
132 return SCSI_Format_Cmd(Lun);
149 static int8_t SCSI_TestUnitReady (uint8_t Lun, uint8_t *params)
152 if (MSC_BBB_cbw.dCBWDataTransferLength != 0)
160 if(USBD_STORAGE_fops->IsReady(Lun) != 0)
179 static int8_t SCSI_Inquiry (uint8_t Lun, uint8_t *params)
184 if (params[1] & 0x01)
188 pPage = (uint8_t *)MSC_Page00_Inquiry_Data;
189 Len = INQUIRY_PAGE00_LENGTH;
195 pPage = (uint8_t *)&USBD_STORAGE_fops->pInquiryData[Lun * USBD_STD_INQUIRY_LENGTH];
199 MSC_BBB_DataLen = MIN(Len, params[4]);
204 MSC_BBB_Data[Len] = pPage[Len];
216 static int8_t SCSI_ReadCapacity10 (uint8_t Lun, uint8_t *params)
218 if(USBD_STORAGE_fops->GetCapacity(Lun, &SCSI_blk_nbr, &SCSI_blk_size) != 0)
228 uint32_t BlkNum = SCSI_blk_nbr - 1;
230 MSC_BBB_Data[0] = (uint8_t)(BlkNum >> 24);
231 MSC_BBB_Data[1] = (uint8_t)(BlkNum >> 16);
232 MSC_BBB_Data[2] = (uint8_t)(BlkNum >> 8);
233 MSC_BBB_Data[3] = (uint8_t)(BlkNum);
235 MSC_BBB_Data[4] = (uint8_t)(SCSI_blk_size >> 24);
236 MSC_BBB_Data[5] = (uint8_t)(SCSI_blk_size >> 16);
237 MSC_BBB_Data[6] = (uint8_t)(SCSI_blk_size >> 8);
238 MSC_BBB_Data[7] = (uint8_t)(SCSI_blk_size);
251 static int8_t SCSI_ReadFormatCapacity (uint8_t Lun, uint8_t *params)
257 for(i = 0; i < 12; i++)
262 if(USBD_STORAGE_fops->GetCapacity(Lun, &BlkNum, &BlkSize) != 0)
271 uint32_t BlkNbr = BlkNum - 1;
273 MSC_BBB_Data[3] = 0x08;
274 MSC_BBB_Data[4] = (uint8_t)(BlkNbr >> 24);
275 MSC_BBB_Data[5] = (uint8_t)(BlkNbr >> 16);
276 MSC_BBB_Data[6] = (uint8_t)(BlkNbr >> 8);
277 MSC_BBB_Data[7] = (uint8_t)(BlkNbr);
279 MSC_BBB_Data[8] = 0x02;
280 MSC_BBB_Data[9] = (uint8_t)(BlkSize >> 16);
281 MSC_BBB_Data[10] = (uint8_t)(BlkSize >> 8);
282 MSC_BBB_Data[11] = (uint8_t)(BlkSize);
284 MSC_BBB_DataLen = 12;
296 static int8_t SCSI_ModeSense6 (uint8_t Lun, uint8_t *params)
300 MSC_BBB_DataLen = len;
305 MSC_BBB_Data[len] = MSC_Mode_Sense6_data[len];
317 static int8_t SCSI_ModeSense10 (uint8_t Lun, uint8_t *params)
321 MSC_BBB_DataLen = Len;
326 MSC_BBB_Data[Len] = MSC_Mode_Sense10_data[Len];
338 static int8_t SCSI_RequestSense (uint8_t Lun, uint8_t *params)
342 for(i = 0; i < REQUEST_SENSE_DATA_LEN; i++)
347 MSC_BBB_Data[0] = 0x70;
348 MSC_BBB_Data[7] = REQUEST_SENSE_DATA_LEN - 6;
350 if((SCSI_Sense_Head != SCSI_Sense_Tail))
352 MSC_BBB_Data[2] = SCSI_Sense[SCSI_Sense_Head].SenseKey;
353 MSC_BBB_Data[12] = SCSI_Sense[SCSI_Sense_Head].ASCQ;
354 MSC_BBB_Data[13] = SCSI_Sense[SCSI_Sense_Head].ASC;
358 if (SCSI_Sense_Head == SENSE_LIST_DEEPTH)
364 MSC_BBB_DataLen = MIN(REQUEST_SENSE_DATA_LEN, params[4]);
378 SCSI_Sense[SCSI_Sense_Tail].SenseKey = SKey;
379 SCSI_Sense[SCSI_Sense_Tail].ASC = ASC << 8;
382 if (SCSI_Sense_Tail == SENSE_LIST_DEEPTH)
394 static int8_t SCSI_StartStopUnit(uint8_t Lun, uint8_t *params)
407 static int8_t SCSI_AllowMediumRemoval(uint8_t Lun, uint8_t *params)
420 static int8_t SCSI_Read10(uint8_t Lun , uint8_t *params)
422 if(MSC_BBB_State == BBB_IDLE)
425 if ((MSC_BBB_cbw.bmCBWFlags & 0x80) != 0x80)
433 if(USBD_STORAGE_fops->IsReady(Lun) != 0)
441 SCSI_blk_addr = (params[2] << 24) | (params[3] << 16) | \
442 (params[4] << 8) | params[5];
444 SCSI_blk_len = (params[7] << 8) | params[8];
446 if(SCSI_CheckAddressRange(Lun, SCSI_blk_addr, SCSI_blk_len) < 0)
451 MSC_BBB_State = BBB_DATA_IN;
452 SCSI_blk_addr *= SCSI_blk_size;
453 SCSI_blk_len *= SCSI_blk_size;
456 if (MSC_BBB_cbw.dCBWDataTransferLength != SCSI_blk_len)
466 MSC_BBB_DataLen = MSC_MEDIA_PACKET_SIZE;
477 static int8_t SCSI_Write10 (uint8_t Lun, uint8_t *params)
479 if (MSC_BBB_State == BBB_IDLE)
482 if ((MSC_BBB_cbw.bmCBWFlags & 0x80) == 0x80)
492 if(USBD_STORAGE_fops->IsReady(Lun) !=0 )
502 if(USBD_STORAGE_fops->IsWriteProtected(Lun) !=0)
511 SCSI_blk_addr = (params[2] << 24) | (params[3] << 16) | \
512 (params[4] << 8) | params[5];
514 SCSI_blk_len = (params[7] << 8) | params[8];
517 if(SCSI_CheckAddressRange(Lun, SCSI_blk_addr, SCSI_blk_len) < 0)
522 SCSI_blk_addr *= SCSI_blk_size;
523 SCSI_blk_len *= SCSI_blk_size;
526 if (MSC_BBB_cbw.dCBWDataTransferLength != SCSI_blk_len)
536 MSC_BBB_State = BBB_DATA_OUT;
540 MIN (SCSI_blk_len, MSC_MEDIA_PACKET_SIZE));
556 static int8_t SCSI_Verify10 (uint8_t Lun, uint8_t *params)
558 if ((params[1] & 0x02) == 0x02)
565 if(SCSI_CheckAddressRange(Lun, SCSI_blk_addr, SCSI_blk_len) < 0)
582 static int8_t SCSI_CheckAddressRange (uint8_t Lun, uint32_t BlkOffset, uint16_t BlkNbr)
584 if ((BlkOffset + BlkNbr) > SCSI_blk_nbr)
603 len = MIN(SCSI_blk_len, MSC_MEDIA_PACKET_SIZE);
605 if(USBD_STORAGE_fops->Read(Lun,
608 len / SCSI_blk_size) < 0)
620 SCSI_blk_addr += len;
624 MSC_BBB_csw.dCSWDataResidue -= len;
626 if (SCSI_blk_len == 0)
628 MSC_BBB_State = BBB_LAST_DATA_IN;
641 uint32_t len = MIN(SCSI_blk_len, MSC_MEDIA_PACKET_SIZE);
643 if(USBD_STORAGE_fops->Write(Lun,
646 len / SCSI_blk_size) < 0)
653 SCSI_blk_addr += len;
657 MSC_BBB_csw.dCSWDataResidue -= len;
659 if (SCSI_blk_len == 0)
671 MIN (SCSI_blk_len, MSC_MEDIA_PACKET_SIZE));
682 static int8_t SCSI_Format_Cmd (uint8_t Lun)
void USB_EP_Tx(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t *pbuf, uint16_t BufLen)
Endpoint prepare to transmit data.
int8_t SCSI_ProcessCmd(USB_DEVICE_HANDLE *pudev, uint8_t Lun, uint8_t *params)
Process SCSI commands.
int8_t SCSI_ProcessRead(uint8_t Lun)
Handle Read Process.
void SCSI_SenseCode(uint8_t Lun, uint8_t SKey, uint8_t ASC)
Load the last error code in the error list.
int8_t SCSI_ProcessWrite(uint8_t Lun)
Handle write process.
void MSC_BBB_SendCSW(USB_DEVICE_HANDLE *pudev, uint8_t CSW_Status)
Send the CSW(command status wrapper)
Header file for the usbd_msc_scsi.c file.
void USB_EP_Rx(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t *pbuf, uint16_t BufLen)
Endpoint prepare to receive data.