/*---------------------------------------------------------------------------- * Exec_UFI_Command * * Mass storage UFI command * Input: Non * Output: None * Remark: After the Houst get the get LUN comman. then requir the command * 1: INQUIRY 2-->READ CAPACITY 3-->Read 4-->MODE SENSE ....... N:READ * Send a CSW command when complete data transimit *---------------------------------------------------------------------------*/ void Exec_UFI_Command() { U32 cnt=0, len=0; U32 Addr=0, tmp=0, sum=0, old=0; U8* p_buff,*p_temp,*p_save; Format_list *p_format_desc; switch(pufi1->bOpCode) { case TEST_UNIT_READY: //0X00 TEST UNIT READY 测试单元准备直接回复CSW csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; case Request_Sense: //0X03 REQUEST SENSE 请求判断 ReqSense[ 0] = 0x70; // Response Code ReqSense[ 1] = 0x00; ReqSense[ 2] = 0x02; // Sense Key ReqSense[ 3] = 0x00; ReqSense[ 4] = 0x00; ReqSense[ 5] = 0x00; ReqSense[ 6] = 0x00; ReqSense[ 7] = 0x0A; // Additional Length ReqSense[ 8] = 0x00; ReqSense[ 9] = 0x00; ReqSense[10] = 0x00; ReqSense[11] = 0x00; ReqSense[12] = 0x30; // ASC ReqSense[13] = 0x01; // ASCQ ReqSense[14] = 0x00; ReqSense[15] = 0x00; ReqSense[16] = 0x00; ReqSense[17] = 0x00; USB_Send_Data(18,ReqSense); break; case INQUIRY: //0x12 INQUIRY 查询 USB_Send_Data(36,query_ret); csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - 0x24; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; case FORMAT_UNIT: //FORMAT if((pufi1->bLun & 0x1F!=0x17) || (cbw1.bCBWLUN > Disk_number)) csw1.bCSWStatus = 0x01; else { sum = pufi1->wLength[1]; sum = sum << 8; sum += pufi1->wLength[0]; p_buff=RX_BUFF; while(!(event_table & EVT_USB_BULK_RX)); //waitting for the RX flag clear_event(EVT_USB_BULK_RX); //clear the RX flag ordor to receive the data in loop USB_bulk_fast_read(TX_BULK_EP_FIFO_SIZE,p_buff); p_format_desc=(Format_list*)p_buff; if((p_format_desc->Number_block != partition_table[cbw1.bCBWLUN]->total_sector)||(p_format_desc->Block_length != partition_table[cbw1.bCBWLUN]->fs->bpb.byte_per_sector)) ReqSense[2] = 0x01; //sense data : SENSE else { ReqSense[2] = 0x00; //sense data : NO SENSE FAT_FORMAT(cbw1.bCBWLUN); } } csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; case MODE_SELECT6: //MODE SELECT ReqSense[2] = 0x00; //sense data : NO SENSE csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; case MODE_SENSE6: //0x1A USB_Send_Data(4,MODE00_RET); csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - DevModePage.MediumType[1] - 2; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; case MODE_SENSE10: //0x5A MODE SENSE(10) 模式判别 SendBuf[ 0] = 0x00; SendBuf[ 1] = 0x06; //Mode data length in byte(not include itself) SendBuf[ 2] = 0x00; //Medium type code SendBuf[ 3] = 0x00; SendBuf[ 4] = 0x00; SendBuf[ 5] = 0x00; SendBuf[ 6] = 0x00; SendBuf[ 7] = 0x00; USB_Send_Data(8,SendBuf); csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - DevModePage.MediumType[1] - 2; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; case Read_Format_Capacity: //0x23 READ FORMAT CAPACITIES 读格式化容量 SendBuf[0] = 0x00; //reserved SendBuf[1] = 0x00; //reserved SendBuf[2] = 0x00; //reserved SendBuf[3] = 0x08; //Capacity List Length // Block Count SendBuf[4] = ((partition_table[cbw1.bCBWLUN]->total_sector ) >> 24) & 0xFF; //BIT[4..7] number of sector SendBuf[5] = ((partition_table[cbw1.bCBWLUN]->total_sector ) >> 16) & 0xFF; SendBuf[6] = ((partition_table[cbw1.bCBWLUN]->total_sector ) >> 8) & 0xFF; SendBuf[7] = ((partition_table[cbw1.bCBWLUN]->total_sector ) >> 0) & 0xFF; SendBuf[8] = 0x02; //Descriptor Code: Formatted Media // Block Length SendBuf[9] = (partition_table[cbw1.bCBWLUN]->fs->bpb.byte_per_sector >> 16) & 0xFF;; //bytes per sector SendBuf[10] = (partition_table[cbw1.bCBWLUN]->fs->bpb.byte_per_sector >> 8) & 0xFF;; SendBuf[11] = (partition_table[cbw1.bCBWLUN]->fs->bpb.byte_per_sector >> 0) & 0xFF;; USB_Send_Data(12,(U8*)SendBuf); csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - 12; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; case READ_CAPACITY: //0x25 读容量 SendBuf[0-3]:returned logic block address SendBuf[4-7]: Block length in byte SendBuf[0] = ((partition_table[cbw1.bCBWLUN]->total_sector - 1) >> 24) & 0xFF; SendBuf[1] = ((partition_table[cbw1.bCBWLUN]->total_sector - 1) >> 16) & 0xFF; SendBuf[2] = ((partition_table[cbw1.bCBWLUN]->total_sector - 1) >> 8) & 0xFF; SendBuf[3] = ((partition_table[cbw1.bCBWLUN]->total_sector - 1) >> 0) & 0xFF; //max block number is 0xfff.暂时使用K9K1G nand flash=128M bytes = 128*1024/32(每蔟=64扇*1page)=0x1000 SendBuf[4] = (partition_table[cbw1.bCBWLUN]->fs->bpb.byte_per_sector >> 24) & 0xFF; SendBuf[5] = (partition_table[cbw1.bCBWLUN]->fs->bpb.byte_per_sector >> 16) & 0xFF; SendBuf[6] = (partition_table[cbw1.bCBWLUN]->fs->bpb.byte_per_sector >> 8) & 0xFF; SendBuf[7] = (partition_table[cbw1.bCBWLUN]->fs->bpb.byte_per_sector >> 0) & 0xFF; //Block length 每block = 512bytes
USB_Send_Data(8,(U8*)SendBuf); csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - 0x08; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; case PREVENT_ALLOW: //PREVENT-ALLOW MEDIUM REMOVAL 直接回复CSW. csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; case READ10: //0x28 READ(10) //把相应地址的数据一直发送完,最后送一个CSW if(MSC_RWSetup()) { old = Length * (partition_table[cbw1.bCBWLUN]->fs->bpb.byte_per_sector); //length of data to receive (in byte) p_buff=A_malloc(old); p_save=p_buff; p_temp=p_buff; sum=Offset + partition_table[cbw1.bCBWLUN]->offset_sector; fnIDE_BufferSector((WORD*)p_save,sum,Length); for(len=0;len < Length<<3; len++) { USB_Send_Data(64,p_temp); //写数据到端点2 p_temp += 64; } A_free(p_buff); ReqSense[2] = 0x00; //sense data : NO SENSE csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - old; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); //send CSW command and complite this SCSI } else { csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x01; USB_Send_Data(13,(U8*)&csw1); } break; case WRITE10: //0x2A WRITE(10) if(MSC_RWSetup()) { old = Length * (partition_table[cbw1.bCBWLUN]->fs->bpb.byte_per_sector); //length of data to receive (in byte) p_buff=A_malloc(old); p_save=p_buff; p_temp=p_buff; sum=Offset + partition_table[cbw1.bCBWLUN]->offset_sector; for(len=0;len < Length<<3; len++) { while(!(event_table & EVT_USB_BULK_RX)); //waitting for the RX flag clear_event(EVT_USB_BULK_RX); //clear the RX flag ordor to receive the data in loop USB_bulk_fast_read(64,p_save); p_save += 64; } fnIDE_WriteBufferSector((WORD*)p_temp,sum,Length); A_free(p_buff); ReqSense[2] = 0x00; //sense data : NO SENSE csw1.dCSWDataResidue = cbw1.dCBWDataTransferLength - sum; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); } else { csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x01; USB_Send_Data(13,(U8*)&csw1); } break; case Verify: //0x2F 验证 if(MSC_RWSetup()) { ReqSense[2] = 0x00; //sense data : NO SENSE csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); } else { csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x01; USB_Send_Data(13,(U8*)&csw1); } break; case REZERO_UNIT: //REZERO UNIT csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; case SEEK10: //SEEK csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; case SEND_DIAGNOSTIC: //SEND DIAGNOSTIC csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; case Star_Stop: //START-STOP UNIT csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x00; USB_Send_Data(13,(U8*)&csw1); break; default://Error csw1.dCSWDataResidue = 0; csw1.bCSWStatus = 0x01; USB_Send_Data(13,(U8*)&csw1); break; } } |