17 uint8_t SCSI_Sense_Head;
18 uint8_t SCSI_Sense_Tail;
20 uint32_t SCSI_blk_size;
21 uint32_t SCSI_blk_nbr;
23 uint32_t SCSI_blk_addr;
24 uint32_t SCSI_blk_len;
29 static int8_t SCSI_TestUnitReady (uint8_t Lun, uint8_t *params);
30 static int8_t SCSI_Inquiry (uint8_t Lun, uint8_t *params);
31 static int8_t SCSI_ReadFormatCapacity (uint8_t Lun, uint8_t *params);
32 static int8_t SCSI_ReadCapacity10 (uint8_t Lun, uint8_t *params);
33 static int8_t SCSI_RequestSense (uint8_t Lun, uint8_t *params);
34 static int8_t SCSI_StartStopUnit (uint8_t Lun, uint8_t *params);
35 static int8_t SCSI_AllowMediumRemoval (uint8_t Lun, uint8_t *params);
36 static int8_t SCSI_ModeSense6 (uint8_t Lun, uint8_t *params);
37 static int8_t SCSI_ModeSense10 (uint8_t Lun, uint8_t *params);
38 static int8_t SCSI_Write10 (uint8_t Lun, uint8_t *params);
39 static int8_t SCSI_Read10 (uint8_t Lun, uint8_t *params);
40 static int8_t SCSI_Verify10 (uint8_t Lun, uint8_t *params);
41 static int8_t SCSI_CheckAddressRange (uint8_t Lun,
46 static int8_t SCSI_Format_Cmd (uint8_t Lun);
65 case SCSI_TEST_UNIT_READY:
66 return SCSI_TestUnitReady(Lun, params);
68 case SCSI_REQUEST_SENSE:
69 return SCSI_RequestSense(Lun, params);
72 return SCSI_Inquiry(Lun, params);
74 case SCSI_START_STOP_UNIT:
75 return SCSI_StartStopUnit(Lun, params);
77 case SCSI_ALLOW_MEDIUM_REMOVAL:
78 return SCSI_AllowMediumRemoval(Lun, params);
80 case SCSI_MODE_SENSE6:
81 return SCSI_ModeSense6(Lun, params);
83 case SCSI_MODE_SENSE10:
84 return SCSI_ModeSense10(Lun, params);
86 case SCSI_READ_FORMAT_CAPACITIES:
87 return SCSI_ReadFormatCapacity(Lun, params);
89 case SCSI_READ_CAPACITY10:
90 return SCSI_ReadCapacity10(Lun, params);
93 return SCSI_Read10(Lun, params);
96 return SCSI_Write10(Lun, params);
99 return SCSI_Verify10(Lun, params);
101 case SCSI_FORMAT_UNIT:
102 return SCSI_Format_Cmd(Lun);
119 static int8_t SCSI_TestUnitReady (uint8_t Lun, uint8_t *params)
122 if (MSC_BBB_cbw.dCBWDataTransferLength != 0)
130 if(USBD_STORAGE_fops->IsReady(Lun) != 0)
149 static int8_t SCSI_Inquiry (uint8_t Lun, uint8_t *params)
154 if (params[1] & 0x01)
158 pPage = (uint8_t *)MSC_Page00_Inquiry_Data;
159 Len = INQUIRY_PAGE00_LENGTH;
165 pPage = (uint8_t *)&USBD_STORAGE_fops->pInquiryData[Lun * USBD_STD_INQUIRY_LENGTH];
168 if (params[4] <= Len)
174 MSC_BBB_DataLen = Len;
179 MSC_BBB_Data[Len] = pPage[Len];
191 static int8_t SCSI_ReadCapacity10 (uint8_t Lun, uint8_t *params)
193 if(USBD_STORAGE_fops->GetCapacity(Lun, &SCSI_blk_nbr, &SCSI_blk_size) != 0)
203 uint32_t BlkNum = SCSI_blk_nbr - 1;
205 MSC_BBB_Data[0] = (uint8_t)(BlkNum >> 24);
206 MSC_BBB_Data[1] = (uint8_t)(BlkNum >> 16);
207 MSC_BBB_Data[2] = (uint8_t)(BlkNum >> 8);
208 MSC_BBB_Data[3] = (uint8_t)(BlkNum);
210 MSC_BBB_Data[4] = (uint8_t)(SCSI_blk_size >> 24);
211 MSC_BBB_Data[5] = (uint8_t)(SCSI_blk_size >> 16);
212 MSC_BBB_Data[6] = (uint8_t)(SCSI_blk_size >> 8);
213 MSC_BBB_Data[7] = (uint8_t)(SCSI_blk_size);
226 static int8_t SCSI_ReadFormatCapacity (uint8_t Lun, uint8_t *params)
232 for(i = 0; i < 12; i++)
237 if(USBD_STORAGE_fops->GetCapacity(Lun, &BlkNum, &BlkSize) != 0)
246 uint32_t BlkNbr = BlkNum - 1;
248 MSC_BBB_Data[3] = 0x08;
249 MSC_BBB_Data[4] = (uint8_t)(BlkNbr >> 24);
250 MSC_BBB_Data[5] = (uint8_t)(BlkNbr >> 16);
251 MSC_BBB_Data[6] = (uint8_t)(BlkNbr >> 8);
252 MSC_BBB_Data[7] = (uint8_t)(BlkNbr);
254 MSC_BBB_Data[8] = 0x02;
255 MSC_BBB_Data[9] = (uint8_t)(BlkSize >> 16);
256 MSC_BBB_Data[10] = (uint8_t)(BlkSize >> 8);
257 MSC_BBB_Data[11] = (uint8_t)(BlkSize);
259 MSC_BBB_DataLen = 12;
271 static int8_t SCSI_ModeSense6 (uint8_t Lun, uint8_t *params)
275 MSC_BBB_DataLen = len;
280 MSC_BBB_Data[len] = MSC_Mode_Sense6_data[len];
292 static int8_t SCSI_ModeSense10 (uint8_t Lun, uint8_t *params)
296 MSC_BBB_DataLen = Len;
301 MSC_BBB_Data[Len] = MSC_Mode_Sense10_data[Len];
313 static int8_t SCSI_RequestSense (uint8_t Lun, uint8_t *params)
317 for(i = 0; i < REQUEST_SENSE_DATA_LEN; i++)
322 MSC_BBB_Data[0] = 0x70;
323 MSC_BBB_Data[7] = REQUEST_SENSE_DATA_LEN - 6;
325 if((SCSI_Sense_Head != SCSI_Sense_Tail))
327 MSC_BBB_Data[2] = SCSI_Sense[SCSI_Sense_Head].SenseKey;
328 MSC_BBB_Data[12] = SCSI_Sense[SCSI_Sense_Head].ASCQ;
329 MSC_BBB_Data[13] = SCSI_Sense[SCSI_Sense_Head].ASC;
333 if (SCSI_Sense_Head == SENSE_LIST_DEEPTH)
339 MSC_BBB_DataLen = MIN(REQUEST_SENSE_DATA_LEN, params[4]);
353 SCSI_Sense[SCSI_Sense_Tail].SenseKey = SKey;
354 SCSI_Sense[SCSI_Sense_Tail].ASC = ASC << 8;
357 if (SCSI_Sense_Tail == SENSE_LIST_DEEPTH)
369 static int8_t SCSI_StartStopUnit(uint8_t Lun, uint8_t *params)
382 static int8_t SCSI_AllowMediumRemoval(uint8_t Lun, uint8_t *params)
395 static int8_t SCSI_Read10(uint8_t Lun, uint8_t *params)
397 if(MSC_BBB_State == BBB_IDLE)
400 if ((MSC_BBB_cbw.bmCBWFlags & 0x80) != 0x80)
408 if(USBD_STORAGE_fops->IsReady(Lun) != 0)
416 SCSI_blk_addr = (params[2] << 24) | (params[3] << 16) | \
417 (params[4] << 8) | params[5];
419 SCSI_blk_len = (params[7] << 8) | params[8];
421 if(SCSI_CheckAddressRange(Lun, SCSI_blk_addr, SCSI_blk_len) < 0)
426 MSC_BBB_State = BBB_DATA_IN;
427 SCSI_blk_addr *= SCSI_blk_size;
428 SCSI_blk_len *= SCSI_blk_size;
431 if (MSC_BBB_cbw.dCBWDataTransferLength != SCSI_blk_len)
441 MSC_BBB_DataLen = MSC_MEDIA_PACKET_SIZE;
452 static int8_t SCSI_Write10 (uint8_t Lun, uint8_t *params)
454 if (MSC_BBB_State == BBB_IDLE)
457 if ((MSC_BBB_cbw.bmCBWFlags & 0x80) == 0x80)
467 if(USBD_STORAGE_fops->IsReady(Lun) !=0)
477 if(USBD_STORAGE_fops->IsWriteProtected(Lun) !=0)
486 SCSI_blk_addr = (params[2] << 24) | (params[3] << 16) | \
487 (params[4] << 8) | params[5];
489 SCSI_blk_len = (params[7] << 8) | params[8];
492 if(SCSI_CheckAddressRange(Lun, SCSI_blk_addr, SCSI_blk_len) < 0)
497 SCSI_blk_addr *= SCSI_blk_size;
498 SCSI_blk_len *= SCSI_blk_size;
501 if (MSC_BBB_cbw.dCBWDataTransferLength != SCSI_blk_len)
511 MSC_BBB_State = BBB_DATA_OUT;
515 MIN (SCSI_blk_len, MSC_MEDIA_PACKET_SIZE));
532 static int8_t SCSI_Verify10 (uint8_t Lun, uint8_t *params)
534 if ((params[1] & 0x02) == 0x02)
541 if(SCSI_CheckAddressRange(Lun, SCSI_blk_addr, SCSI_blk_len) < 0)
558 static int8_t SCSI_CheckAddressRange (uint8_t Lun, uint32_t BlkOffset, uint16_t BlkNbr)
560 if ((BlkOffset + BlkNbr) > SCSI_blk_nbr)
579 len = MIN(SCSI_blk_len, MSC_MEDIA_PACKET_SIZE);
581 if(USBD_STORAGE_fops->Read(Lun,
584 len / SCSI_blk_size) < 0)
596 SCSI_blk_addr += len;
600 MSC_BBB_csw.dCSWDataResidue -= len;
602 if (SCSI_blk_len == 0)
604 MSC_BBB_State = BBB_LAST_DATA_IN;
617 uint32_t len = MIN(SCSI_blk_len, MSC_MEDIA_PACKET_SIZE);
619 if(USBD_STORAGE_fops->Write(Lun,
622 len / SCSI_blk_size) < 0)
629 SCSI_blk_addr += len;
633 MSC_BBB_csw.dCSWDataResidue -= len;
635 if (SCSI_blk_len == 0)
645 MIN (SCSI_blk_len, MSC_MEDIA_PACKET_SIZE));
656 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.
void MSC_BBB_SendCSW(USB_DEVICE_HANDLE *pudev, uint8_t CSW_Status)
Send the CSW(command status wrapper)
int8_t SCSI_ProcessRead(uint8_t Lun)
Handle Read Process.
int8_t SCSI_ProcessWrite(uint8_t Lun)
Handle write process.
Header file for the usbd_msc_scsi.c file.
void SCSI_SenseCode(uint8_t Lun, uint8_t SKey, uint8_t ASC)
Load the last error code in the error list.
int8_t SCSI_ProcessCmd(USB_DEVICE_HANDLE *pudev, uint8_t Lun, uint8_t *params)
Process SCSI commands.
void USB_EP_Rx(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t *pbuf, uint16_t BufLen)
Endpoint prepare to receive data.