📄 atapiio.cpp
字号:
//**********************************************************************
//
// Filename: atapiio.cpp
//
// Description:
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Use of this source code is subject to the terms of the Cirrus 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
// EULA.RTF on your install media.
//
// Copyright(c) Cirrus Logic Corporation 2005, All Rights Reserved
//
//**********************************************************************
//
// 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>
#define MAX_SECT_PER_EXT_COMMAND 0x10000
BOOL CDisk::SendIOCommand(DWORD dwStartSector, DWORD dwNumberOfSectors, BYTE bCmd)
{
volatile ULONG ulIdeCtrl;
DEBUGMSG( ZONE_IO, (TEXT("ATAPI:SendIOCommand - Sector %d SectorsLeft %x Command %x\r\n"), dwStartSector,dwNumberOfSectors,bCmd));
SelectDevice();
if (WaitOnBusy(FALSE) & ATA_STATUS_BUSY)
{
DEBUGMSG( ZONE_WARNING, (TEXT("ATAPI:SendIOCommand - Can't send a command!!! Status:%x Error:%x \r\n"), GetAltStatus(),GetError()));
DEBUGMSG( ZONE_WARNING, (TEXT("ATAPI:SendIOCommand BAD EXIT\r\n")));
return (FALSE);
}
GetBaseStatus();
ulIdeCtrl = *IDE_CTRL;
if (m_fUseLBA48) {
ASSERT(dwNumberOfSectors <= MAX_SECT_PER_EXT_COMMAND);
// to transfer 65536 sectors, set number of sectors to 0
if (dwNumberOfSectors == MAX_SECT_PER_EXT_COMMAND) {
dwNumberOfSectors = 0;
}
WriteSectorCount((BYTE)(dwNumberOfSectors >> 8)); // Sector Count 15:8
WriteSectorCount((BYTE)(dwNumberOfSectors)); // Sector Count 7:0
// CE supports only 32-bit LBA as of now. Therefore, clear the upper 16 bits of LBA.
WriteHighCount(0); // LBA 47:40
WriteLowCount(0); // LBA 39:32
WriteSectorNumber((BYTE)(dwStartSector >> 24)); // LBA 31:24
WriteHighCount((BYTE)(dwStartSector >> 16)); // LBA 23:16
WriteLowCount((BYTE)(dwStartSector >> 8)); // LBA 15:8
WriteSectorNumber((BYTE)dwStartSector); // LBA 7:0
WriteDriveHeadReg( ATA_HEAD_LBA_MODE | ((m_dwDevice == 0 ) ? ATA_HEAD_DRIVE_1 : ATA_HEAD_DRIVE_2));
}else {
//
// To transfer 256 Sectors set Sector Count Register to 0.
//
if(dwNumberOfSectors == MAX_SECT_PER_COMMAND)
dwNumberOfSectors = 0;
WriteSectorCount((BYTE)dwNumberOfSectors);
if (m_fLBAMode == TRUE)
{
if (dwStartSector & 0xf0000000) { // -wlg
DEBUGMSG( ZONE_WARNING,
(TEXT("ATAPI:SendIOCommand - dwStartSector %08x 28-bit overflow!\r\n"),
dwStartSector));
}
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 { // translate LBA to CHS format
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)));
}
} //48 bit
//* * * CAMSDB - Took this out for test, put it back 04/20/04 (START)
WriteCommand(bCmd);
//* * * CAMSDB - Took this out for test, put it back 04/20/04 (END)
//according ATA specification, we should wait for busy when issue a command.
//otherwise, when we don't use interrupt, check status register just now, error maybe occur.
if( WaitForDisc( WAIT_TYPE_BUSY, 100, 1) )
{
DEBUGMSG( ZONE_IO, (TEXT("ATAPI:WaitFor WAIT_TYPE_BUSY Failed\r\n")));
}
DEBUGMSG( ZONE_IO, (TEXT("ATAPI:SendIOCommand GOOD EXIT\r\n")));
return (TRUE);
}
/*------------------------------------------------------------------------------------------*/
BYTE CDisk::WaitOnBusy(BOOL fBase)
{
DWORD i,j;
BYTE bStatus;
for (i=0; i< DEFAULT_WAIT_CHECK_ITER; i++)
{
for (j=0; j<DEFAULT_WAIT_SAMPLE_TIMES; j++)
{
bStatus = fBase ? GetBaseStatus() : GetAltStatus();
if (!(bStatus & ATA_STATUS_BUSY))
{
return bStatus & (ATA_STATUS_ERROR |ATA_STATUS_BUSY);
}
}
StallExecution(DEFAULT_WAIT_STALL_TIME);
}
return bStatus & (ATA_STATUS_ERROR |ATA_STATUS_BUSY);
}
/*------------------------------------------------------------------------------------------*/
BOOL CDisk::WaitForDisc(BYTE bStatusType, DWORD dwTimeOut, DWORD dwPeriod)
{
BYTE bStatusRead;
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:WaitForDisc - WAIT_TYPE_BUSY\r\n")));
goto ExitDone;
}
break;
case WAIT_TYPE_NOT_BUSY:
if (!(bStatusRead & ATA_STATUS_BUSY))
{
DEBUGMSG(ZONE_IO, (TEXT("ATAPI:WaitForDisc - WAIT_TYPE_NOT_BUSY\r\n")));
goto ExitDone;
}
break;
case WAIT_TYPE_READY:
if (bStatusRead & ATA_STATUS_READY)
{
DEBUGMSG(ZONE_IO, (TEXT("ATAPI: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: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:WaitForDisc - WAIT_TYPE_NOT_DRQ\r\n")));
goto ExitDone;
}
break;
case WAIT_TYPE_ERROR:
if (bStatusRead & ATA_STATUS_ERROR)
{
DEBUGMSG(ZONE_IO, (TEXT("ATAPI:WaitForDisc - WAIT_TYPE_ERROR\r\n")));
goto ExitDone;
}
break;
}
if ((int)dwTimeOut > 0)
{
StallExecution(dwPeriod);
dwTimeOut -= dwPeriod;
}
else
{
DEBUGMSG(ZONE_ERROR, (TEXT("ATAPI:WaitForDisc - TimeOut !!! WaitType = %ld Status=%02X\r\n"), bStatusType, bStatusRead));
return ERROR_GEN_FAILURE;
}
}
ExitDone:
return ERROR_SUCCESS;
}
/*------------------------------------------------------------------------------------------*/
//
// Wait for DRQ is set and device is read
// to transfer data or timeout is occured
// Return (Status == ATA_STATUS_DATA_REQ)
//
BOOL CDisk::WaitForDRQ()
{
DWORD i,j;
BYTE bStatus;
for (i=0; i< DEFAULT_WAIT_CHECK_ITER; i++)
{
for (j=0; j<DEFAULT_WAIT_SAMPLE_TIMES; j++) {
bStatus = GetAltStatus() & (ATA_STATUS_BUSY|ATA_STATUS_DATA_REQ);
if (bStatus & ATA_STATUS_BUSY) {
continue;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -