📄 atapiio.cpp
字号:
//
// Copyright(C) Renesas Technology Corp. 2005. All rights reserved.
//
// ATAPI(UDFS) driver for ITS-DS7
//
// FILE : atamain.cpp
// CREATED : 2005.02.10
// MODIFIED : 2005.04.14
// AUTHOR : Renesas Technology Corp.
// HARDWARE : RENESAS ITS-DS7
// HISTORY :
// 2005.02.10
// - Created release code.
// (based on PUBLIC ATAPI driver for WCE5.0)
// 2005.04.14
// - Modified adjustment of a timing.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include <atamain.h>
// ----------------------------------------------------------------------------
// Function: SendIOCommand
// Issue I/O command
//
// Parameters:
// pId -
// dwNumberOfSectors -
// bCmd -
// ----------------------------------------------------------------------------
BOOL
CDisk::SendIOCommand(
DWORD dwStartSector,
DWORD dwNumberOfSectors,
BYTE bCmd
)
{
DEBUGMSG(ZONE_IO, (TEXT(
"Atapi!CDisk::SendIOCommand> sector(%d), sectors left(%x), command(%x)\r\n"
), dwStartSector,dwNumberOfSectors,bCmd));
if (ZONE_CELOG) CeLogData(TRUE, CELID_ATAPI_IOCOMMAND, &bCmd, sizeof(bCmd), 0, CELZONE_ALWAYSON, 0, FALSE);
SelectDevice();
if (WaitOnBusy(FALSE)) {
DEBUGMSG(ZONE_IO, (TEXT(
"Atapi!CDisk::SendIOCommand> Failed to send command; status(%x), error(%x)\r\n"
), GetAltStatus(),GetError()));
return FALSE;
}
// to transfer 256 sectors, set number of sectors to 0
if (dwNumberOfSectors == MAX_SECT_PER_COMMAND) {
dwNumberOfSectors = 0;
}
WriteSectorCount((BYTE)dwNumberOfSectors);
if (m_fLBAMode == TRUE) {
WriteSectorNumber( (BYTE)dwStartSector);
WriteLowCount((BYTE)(dwStartSector >> 8));
WriteHighCount((BYTE)(dwStartSector >> 16));
WriteDriveHeadReg((BYTE)((dwStartSector >> 24) | ATA_HEAD_LBA_MODE) | ((m_dwDevice == 0 ) ? ATA_HEAD_DRIVE_1 : ATA_HEAD_DRIVE_2));
}
else {
DWORD dwSectors = m_DiskInfo.di_sectors;
DWORD dwHeads = m_DiskInfo.di_heads;
WriteSectorNumber((BYTE)((dwStartSector % dwSectors) + 1));
WriteLowCount((BYTE) (dwStartSector /(dwSectors*dwHeads)));
WriteHighCount((BYTE)((dwStartSector /(dwSectors*dwHeads)) >> 8));
WriteDriveHeadReg((BYTE)(((dwStartSector/dwSectors)% dwHeads) | ((m_dwDevice == 0 ) ? ATA_HEAD_DRIVE_1 : ATA_HEAD_DRIVE_2)));
}
WriteCommand(bCmd);
return (TRUE);
}
// ----------------------------------------------------------------------------
// Function: WaitOnBusy
// Wait for BSY=0
//
// Parameters:
// fBase -
// ----------------------------------------------------------------------------
BYTE
CDisk::WaitOnBusy(
BOOL fBase
)
{
DWORD i, j;
BYTE bStatus = 0;
for (i = 0; i < m_dwWaitCheckIter; i++) {
for (j = 0; j < m_dwWaitSampleTimes; j++) {
bStatus = fBase ? GetBaseStatus() : GetAltStatus();
if (!(bStatus & ATA_STATUS_BUSY)) {
return bStatus & (ATA_STATUS_ERROR |ATA_STATUS_BUSY);
}
}
StallExecution(m_dwWaitStallTime);
}
return bStatus & (ATA_STATUS_ERROR |ATA_STATUS_BUSY);
}
// ----------------------------------------------------------------------------
// Function: WaitForDisc
// Generic wait routine; wait for @bStatusType
//
// Parameters:
// bStatusType -
// dwTimeOut -
// dwPeriod -
// ----------------------------------------------------------------------------
BOOL
CDisk::WaitForDisc(
BYTE bStatusType,
DWORD dwTimeOut,
DWORD dwPeriod
)
{
BYTE bStatusRead = 0;
if (dwPeriod == 0) {
dwPeriod = dwTimeOut;
}
while( TRUE) {
bStatusRead = GetAltStatus();
switch (bStatusType) {
case WAIT_TYPE_BUSY:
if (bStatusRead & ATA_STATUS_BUSY) {
DEBUGMSG(ZONE_IO, (TEXT("Atapi!CDisk::WaitForDisc> WAIT_TYPE_BUSY\r\n")));
goto ExitDone;
}
break;
case WAIT_TYPE_NOT_BUSY:
if (!(bStatusRead & ATA_STATUS_BUSY)) {
DEBUGMSG(ZONE_IO, (TEXT("Atapi!CDisk::WaitForDisc> WAIT_TYPE_NOT_BUSY\r\n")));
goto ExitDone;
}
break;
case WAIT_TYPE_READY:
if (bStatusRead & ATA_STATUS_READY) {
DEBUGMSG(ZONE_IO, (TEXT("Atapi!CDisk::WaitForDisc> WAIT_TYPE_READY\r\n")));
StallExecution(100);
goto ExitDone;
}
break;
case WAIT_TYPE_DRQ:
if (bStatusRead & ATA_STATUS_DATA_REQ) {
DEBUGMSG(ZONE_IO, (TEXT("Atapi!CDisk::WaitForDisc> WAIT_TYPE_DRQ\r\n")));
goto ExitDone;
}
break;
case WAIT_TYPE_NOT_DRQ:
if (!(bStatusRead & ATA_STATUS_DATA_REQ)) {
DEBUGMSG(ZONE_IO, (TEXT("Atapi!CDisk::WaitForDisc> WAIT_TYPE_NOT_DRQ\r\n")));
goto ExitDone;
}
break;
case WAIT_TYPE_ERROR:
if (bStatusRead & ATA_STATUS_ERROR) {
DEBUGMSG(ZONE_IO, (TEXT("Atapi!CDisk::WaitForDisc> WAIT_TYPE_ERROR\r\n")));
goto ExitDone;
}
break;
}
if ((int)dwTimeOut > 0) {
StallExecution(dwPeriod);
dwTimeOut -= dwPeriod;
}
else {
DEBUGMSG(ZONE_ERROR, (TEXT(
"Atapi!CDisk::WaitForDisc> timeout; bStatusType(%d); status(%02X)\r\n"
), bStatusType, bStatusRead));
return ERROR_GEN_FAILURE;
}
}
ExitDone:
return ERROR_SUCCESS;
}
// ----------------------------------------------------------------------------
// Function: WaitForDRQ
// Wait for DRQ=1 (and device ready to transfer) or timeout
//
// Parameters:
// None
// ----------------------------------------------------------------------------
BOOL
CDisk::WaitForDRQ(
)
{
DWORD i,j;
BYTE bStatus = 0;
if (ZONE_CELOG) CeLogData(TRUE, CELID_ATAPI_WAITDRQ, NULL, 0, 0, CELZONE_ALWAYSON, 0, FALSE);
for (i = 0; i < m_dwWaitCheckIter; i++) {
for (j = 0; j < m_dwWaitSampleTimes; j++) {
StallExecution(m_dwWaitStallTime);
bStatus = GetAltStatus() & (ATA_STATUS_BUSY|ATA_STATUS_DATA_REQ);
if ((bStatus & ATA_STATUS_BUSY) == 0) {
if (bStatus & ATA_STATUS_DATA_REQ){
if (ZONE_CELOG) CeLogData(TRUE, CELID_ATAPI_STATUSWAITDRQ, &bStatus, sizeof(bStatus), 0, CELZONE_ALWAYSON, 0, FALSE);
return TRUE;
}
}
}
DEBUGMSG(ZONE_WARNING, (TEXT(
"Atapi!CDisk::WaitForDRQ> status(%02X), error(%02X), reason(%02X)\r\n"
), GetAltStatus(), GetError(), GetReason()));
}
if (ZONE_CELOG) CeLogData(TRUE, CELID_ATAPI_STATUSWAITDRQ, &bStatus, sizeof(bStatus), 0, CELZONE_ALWAYSON, 0, FALSE);
return (bStatus == ATA_STATUS_DATA_REQ);
}
// ----------------------------------------------------------------------------
// Function: CheckIntrState
// Return interrupt reason/status
//
// Parameters:
// None
// ----------------------------------------------------------------------------
WORD
CDisk::CheckIntrState(
)
{
BYTE bReason, bDRQ;
WaitOnBusy(FALSE);
bReason = GetReason() & (ATA_IR_CoD | ATA_IR_IO);
bDRQ = GetAltStatus() & ATA_STATUS_DATA_REQ;
if (bDRQ) {
bReason |= 4;
}
if (bReason < 3) {
return((WORD) ATA_INTR_READY);
}
return ((WORD)bReason);
}
// ----------------------------------------------------------------------------
// Function: ReadBuffer
// Fill buffer from data register
//
// Parameters:
// pBuffer -
// dwCount -
// ----------------------------------------------------------------------------
void
CDisk::ReadBuffer(
PBYTE pBuffer,
DWORD dwCount
)
{
union {
WORD us;
BYTE uc[2];
} unisc;
if (ZONE_CELOG) CeLogData(TRUE, CELID_ATAPI_STARTREADBUFFER, &dwCount, sizeof(dwCount), 0, CELZONE_ALWAYSON, 0, FALSE);
if (dwCount == 0) {
return;
}
// determine whether required byte was read in previous read;
// m_wNextByte=(-1) implies byte not read in previous read
if (m_wNextByte != 0xFFFF) {
DEBUGMSG(ZONE_WARNING, (TEXT("Atapi!CDisk::ReadBuffer> Unaligned buffer on prevous read\r\n")));
// update first byte
*pBuffer++ = (BYTE)m_wNextByte;
dwCount--;
}
// check alignment of pBuffer
if ((DWORD)pBuffer & 1) {
DEBUGMSG(ZONE_WARNING, (TEXT("Atapi!CDisk::ReadBuffer> Unaligned buffer\r\n")));
while (dwCount> 1) {
unisc.us = ReadWord();
*pBuffer++= unisc.uc[0];
*pBuffer++= unisc.uc[1];
dwCount-=2;
}
}
else {
ReadWordBuffer((PWORD)pBuffer,(DWORD)(dwCount)/sizeof(SHORT));
pBuffer += dwCount;
dwCount &= 1; // if 1, we need to read the next byte still
pBuffer -= dwCount; // adjust pBuffer if its address is odd
}
// read one word even if we only need one byte; save the unused byte and
// use it as the first byte of the next read (scatter/gather buffer)
if (dwCount == 1) {
DEBUGMSG(ZONE_WARNING, (TEXT("Atapi!CDisk::ReadBuffer> Reading word for one byte\r\n")));
unisc.us = ReadWord();
*pBuffer= unisc.uc[0];
m_wNextByte = (WORD)unisc.uc[1]; // save the second byte
}
BYTE bStatus = GetAltStatus();
if (ZONE_CELOG) CeLogData(TRUE, CELID_ATAPI_COMPLETEREADBUFFER, &bStatus, sizeof(bStatus), 0, CELZONE_ALWAYSON, 0, FALSE);
}
// ----------------------------------------------------------------------------
// Function: WriteBuffer
// Empty buffer to data register
//
// Parameters:
// pBuffer -
// dwCount -
// ----------------------------------------------------------------------------
void
CDisk::WriteBuffer(
PBYTE pBuffer,
DWORD dwCount
)
{
union {
WORD us;
BYTE uc[2];
} unisc;
if (ZONE_CELOG) CeLogData(TRUE, CELID_ATAPI_STARTWRITEBUFFER, &dwCount, sizeof(dwCount), 0, CELZONE_ALWAYSON, 0, FALSE);
if (dwCount == 0) {
return;
}
// determine whether required byte was written in previous write;
// m_wNextByte=(-1) implies byte not written in previous write
if (m_wNextByte != 0xFFFF) {
// update first byte
DEBUGMSG(ZONE_WARNING, (TEXT("Atapi!CDisk::WriteBuffer> Unaligned buffer on previous write\r\n")));
unisc.uc[0] = (BYTE) m_wNextByte;
unisc.uc[1] = *pBuffer++;
dwCount--;
WriteWord(unisc.us);
}
// check alignment of pBuffer
if ((DWORD) pBuffer & 1) {
DEBUGMSG(ZONE_WARNING, (TEXT("Atapi!CDisk::WriteBuffer> Unaligned buffer\r\n")));
while (dwCount> 1) {
unisc.uc[0] = *pBuffer++;
unisc.uc[1] = *pBuffer++;
WriteWord(unisc.us);
dwCount-=2;
}
}
else {
WriteWordBuffer((PWORD)pBuffer,(DWORD)(dwCount)/sizeof(SHORT));
pBuffer += dwCount;
dwCount &= 1; // if 1, we need to write the next byte still
pBuffer -= dwCount; // adjust pBuffer if its address is odd
}
// write one word even if we only need one byte; save the unused byte and
// use it as the first byte of the next read (scatter/gather buffer)
if (dwCount == 1) {
DEBUGMSG( ZONE_WARNING, (TEXT("Atapi!CDisk::WriteBuffer> Writing one word for one byte\r\n")));
m_wNextByte = (WORD) *pBuffer; // save the second byte
}
BYTE bStatus = GetAltStatus();
if (ZONE_CELOG) CeLogData(TRUE, CELID_ATAPI_COMPLETEWRITEBUFFER, &bStatus, sizeof(bStatus), 0, CELZONE_ALWAYSON, 0, FALSE);
}
#define IDE_COMMAND_SET_FEATURE 0xEF
// ----------------------------------------------------------------------------
// Function: SetTransferMode
// Set a device's transfer mode through the SET FEATURES command
//
// Parameters:
// bMode - desired transfer mode
//
// Notes:
// See ATA/ATAPI-6 R3B 8.15 IDENTIFY DEVICE for more information regarding
// IDENTIFY DEVICE data.
// See ATA/ATAPI-6 R3B 8.46 SET FEATURES for more information regarding
// transfer mode encodings.
// ----------------------------------------------------------------------------
BOOL
CDisk::SetTransferMode(
BYTE bMode
)
{
BYTE bStatus; // Status register
BYTE bError; // Error register
BOOL fOk = TRUE; // result
// HI:Check_Status (Host Idle); wait until BSY=0 and DRQ=0
// read Status register
while (1) {
bStatus = GetAltStatus();
if (!(bStatus & (0x80|0x08))) break; // BSY := Bit 7, DRQ := Bit 3
Sleep(5);
}
// HI:Device_Select; select device
SelectDevice();
// HI:Check_Status (Host Idle); wait until BSY=0 and DRQ=0
// read Status register
while (1) {
bStatus = GetAltStatus();
if (!(bStatus & (0x80|0x08))) break; // BSY := Bit 7, DRQ := Bit 3
Sleep(5);
}
// HI:Write_Parameters
WriteFeature(0x03); // set transfer mode based on value in Sector Count register (Table 44)
WriteSectorCount(bMode); // (Table 45)
WriteAltDriveController(0x00); // disable interrupt (nIEN := Bit 1 of Device Control register)
// HI:Write_Command
WriteCommand(0xEF); // SET FEATURES command code := EFh
// transition to non-data command protocol
// HND:INTRQ_Wait
// transition to HND:Check_Status
// read Status register
while (1) { // BSY := Bit 7
bStatus = GetAltStatus();
bError = GetError();
if (bError & 0x04) { // ABRT := Bit 2
// command was aborted
DEBUGMSG(ZONE_ERROR, (_T(
"Atapi!CDisk::SetTransferMode> Failed to send SET FEATURES command, parameter 0x%x\r\n"
), bMode));
fOk = FALSE;
break;
}
if (!(bStatus & 0x80)) break; // BSY := Bit 7
Sleep(5);
}
// transition to host idle protocol
return fOk;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -