⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diskmain.cpp

📁 EP931X系列的WinCE显卡驱动源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    {
        return ERROR_INVALID_PARAMETER; 
    }
    *(pIOReq->pBytesReturned) = 0;
    if (m_szDiskName) {
        if (wcslen( m_szDiskName)) {    
            szDiskName = m_szDiskName;
        } else {
            return ERROR_NOT_SUPPORTED;
        }    
    } else {
        szDiskName = szDefaultDiscDrive;
    }    
    dwSize = (wcslen( szDiskName)+1) * sizeof(TCHAR);
    if (pIOReq->dwOutBufSize < dwSize) {
        return ERROR_INSUFFICIENT_BUFFER;
    }    
    wcscpy( (PTCHAR) pIOReq->pOutBuf, szDiskName);
    *(pIOReq->pBytesReturned) = dwSize; 
    return ERROR_SUCCESS;
}   


/*------------------------------------------------------------------------------------------*/

DWORD CDisk::ReadWriteDisk(PIOREQ pIOReq, BOOL fRead)
{
    DWORD dwError = ERROR_SUCCESS;
    PBYTE pBuffer;
    PSG_REQ pSgReq = (PSG_REQ)pIOReq->pInBuf;
    DWORD dwStartSector, dwNumSectors, dwSectorsToTransfer, dwSectorsPerBlock, dwSgLeft, dwSgLen;
    DWORD dwMaxPerBlock = m_bSectorsPerBlock;

    PSG_BUF pSgBuf;

 
    BYTE bCmd = fRead ? m_bReadCommand : m_bWriteCommand;
    DEBUGMSG( ZONE_IOCTL, (TEXT("ATAPI:ReadWriteDisk Entered\r\n")));
    
    if ((pSgReq == NULL) ||
       (pIOReq->dwInBufSize < sizeof(SG_REQ))) {
        return ERROR_INVALID_PARAMETER;
    } 
    if ((pSgReq->sr_num_sec == 0) || 
       (pSgReq->sr_num_sg == 0)) {
        return  ERROR_INVALID_PARAMETER;
    }       
    
    GetBaseStatus(); // Clear Interrupt if it is already set 

    
    dwSgLeft = pSgReq->sr_num_sg;           //  The number of SG segments 
    dwNumSectors = pSgReq->sr_num_sec;
    dwStartSector = pSgReq->sr_start;   // Get the Start Sector 
    pSgBuf = &(pSgReq->sr_sglist[0]);
    pBuffer = (PBYTE)MapPtrToProcess((LPVOID)pSgBuf->sb_buf, GetCallerProcess());
    

    m_wNextByte = 0xFFFF;

    DEBUGMSG( ZONE_IO, (TEXT("ATAPI:ReadWriteDisk StartSector=%ld NumSectors=%ld NumSg=%ld\r\n"), dwStartSector, dwNumSectors, dwSgLeft));
    
    while(dwNumSectors) {
        if (dwNumSectors > dwMaxPerBlock)    
            dwSectorsToTransfer = dwMaxPerBlock;
        //  The Maximal Number of Sectors per Command is 256.
//      if (dwNumSectors > MAX_SECT_PER_COMMAND)    
//          dwSectorsToTransfer = MAX_SECT_PER_COMMAND;
        else    
            dwSectorsToTransfer = dwNumSectors;

        dwNumSectors -= dwSectorsToTransfer;

        if (!SendIOCommand(dwStartSector,dwSectorsToTransfer, bCmd)) {
            dwError = fRead ? ERROR_READ_FAULT : ERROR_WRITE_FAULT;
            break;
        }

        dwStartSector += dwSectorsToTransfer;

        //  The read command expects an interrupt before data transfer.
        //  The write command, on the other side, expects an interrupt after

        dwSectorsPerBlock = dwMaxPerBlock;

        while(dwSectorsToTransfer && (dwSgLeft > 0)) {
            
            if (dwSectorsPerBlock > dwSectorsToTransfer)
                    dwSectorsPerBlock = dwSectorsToTransfer;

            dwSectorsToTransfer -= dwSectorsPerBlock;
            
            if (fRead && m_fInterruptSupported) {       
                if (!WaitForInterrupt(DISK_IO_TIME_OUT) || (CheckIntrState() == ATA_INTR_ERROR)) {  
                    DEBUGMSG( ZONE_IO, (TEXT("ATAPI:ReadWrite- WaitforInterrupt failed (DevId %x) \r\n"),m_dwDeviceId));
                    dwError = ERROR_READ_FAULT;
                    break;
                } 
            }   
            if (!WaitForDRQ()) {
                DEBUGMSG( ZONE_IO, (TEXT("ATAPI:ReadWrite- WaitforDRQ failed (DevId %x) \r\n"),m_dwDeviceId));
                dwError = ERROR_READ_FAULT;
                break;
            }

            DWORD dwBytesPerBlock = dwSectorsPerBlock * m_DiskInfo.di_bytes_per_sect;

            dwSgLen = pSgBuf->sb_len;

            while ((dwBytesPerBlock>0) && (dwSgLeft >0))
            {
                DWORD dwIndex = (dwSgLen < dwBytesPerBlock) ? dwSgLen : dwBytesPerBlock;
                dwSgLen -= dwIndex;
                dwBytesPerBlock -= dwIndex;

                fRead ? ReadBuffer(pBuffer,dwIndex) : WriteBuffer(pBuffer,dwIndex);
                    
                pBuffer += dwIndex; // 

                // Go to the next scatter/gather if no more space left

                if ((dwSgLen == 0) && ( --dwSgLeft > 0)) {
                    pSgBuf++;
                    pBuffer = (PBYTE)MapPtrToProcess((LPVOID)pSgBuf->sb_buf, GetCallerProcess());
                    dwSgLen = pSgBuf->sb_len;
                }
            }   

            if (fRead) {
                while (dwBytesPerBlock > 0)  {
                    (void) ReadWord();
                    dwBytesPerBlock-=2 ;            
                }
            }
                
        }
        
        if (ERROR_SUCCESS != dwError)
            break;

        //  In case of write command an interrupt is generated upon each block.
        //  We can either wait for the interrupt or simply ignore it.
        //  However this is danger because an interrupt is pending and blocks all
        //  other interrupts on the same or lower level.

        if ( !fRead && m_fInterruptSupported) {     
            if (!WaitForInterrupt(DISK_IO_TIME_OUT) || (CheckIntrState() == ATA_INTR_ERROR)) {  
                DEBUGMSG( ZONE_IO, (TEXT("ATAPI:ReadWrite- WaitforInterrupt failed (DevId %x) \r\n"),m_dwDeviceId));
                dwError = ERROR_READ_FAULT;
                break;
            } 
        }            
    }
    
    if (ERROR_SUCCESS != dwError) {
        ResetController();
        if (!(m_Id.Capabilities & 0x0200) && m_fLBAMode) {
            m_fLBAMode = FALSE;
            dwError = ReadWriteDisk(pIOReq, fRead);
        } else {
        }   
    }    
    pSgReq->sr_status = dwError;
    return dwError;

}

/*------------------------------------------------------------------------------------------*/

DWORD CDisk::ReadWriteDiskDMA(PIOREQ pIOReq, BOOL fRead)
{
    DWORD dwError = ERROR_SUCCESS;
    PSG_REQ pSgReq = (PSG_REQ)pIOReq->pInBuf;
    DWORD dwSectorsToTransfer;
    SG_BUF CurBuffer[MAX_SG_BUF];
    BYTE bCmd;

    DEBUGMSG( ZONE_IOCTL, (TEXT("ATAPI:ReadWriteDisk Entered\r\n")));

    if ((pSgReq == NULL) || (pIOReq->dwInBufSize < sizeof(SG_REQ))) 
    {
        return ERROR_INVALID_PARAMETER;
    }   
    if ((pSgReq->sr_num_sec == 0) || 
       (pSgReq->sr_num_sg == 0)) 
    {
        return  ERROR_INVALID_PARAMETER;
    }       

    DEBUGMSG( ZONE_DMA, (TEXT("ATAPI:ReadWriteDiskDMA StartSector=%ld NumSectors=%ld NumSg=%ld\r\n"), pSgReq->sr_start, pSgReq->sr_num_sec, pSgReq->sr_num_sg));

    GetBaseStatus(); // Clear Interrupt if it is already set 

    DWORD dwStartBufferNum = 0, dwEndBufferNum = 0, dwEndBufferOffset = 0;
    DWORD dwNumSectors = pSgReq->sr_num_sec;
    DWORD dwStartSector = pSgReq->sr_start;

    // Process the SG buffers in blocks of MAX_SECT_PER_COMMAND.  Each DMA request will have a new SG_BUF array 
    // which will be a subset of the original request, and may start/stop in the middle of the original buffer.
    while (dwNumSectors) 
    {

        //dwSectorsToTransfer = (dwNumSectors > MAX_SECT_PER_COMMAND) ? MAX_SECT_PER_COMMAND : dwNumSectors;
        dwSectorsToTransfer = (dwNumSectors > MAX_DMA_SECT_PER_COMMAND) ? 
                                MAX_DMA_SECT_PER_COMMAND : dwNumSectors;
    
        DWORD dwBufferLeft = dwSectorsToTransfer * BYTES_PER_SECTOR;
        DWORD dwNumSg = 0;

        while (dwBufferLeft) 
        {
            DWORD dwCurBufferLen = pSgReq->sr_sglist[dwEndBufferNum].sb_len - dwEndBufferOffset;

            if (dwBufferLeft < dwCurBufferLen) 
            {
                // The buffer left for this block is less than the current SG buffer length
                CurBuffer[dwEndBufferNum - dwStartBufferNum].sb_buf = pSgReq->sr_sglist[dwEndBufferNum].sb_buf + dwEndBufferOffset;
                CurBuffer[dwEndBufferNum - dwStartBufferNum].sb_len = dwBufferLeft;
                dwEndBufferOffset += dwBufferLeft;
                dwBufferLeft = 0;
            } 
            else 
            {
                // The buffer left for this block is greater than or equal to the current SG buffer length.  Move on to the next SG buffer.
                CurBuffer[dwEndBufferNum - dwStartBufferNum].sb_buf = pSgReq->sr_sglist[dwEndBufferNum].sb_buf + dwEndBufferOffset;
                CurBuffer[dwEndBufferNum - dwStartBufferNum].sb_len = dwCurBufferLen;
                dwEndBufferOffset = 0;
                dwEndBufferNum++;
                dwBufferLeft -= dwCurBufferLen;
            }    
            dwNumSg++;
        }
      
        bCmd = fRead ? ATA_CMD_READ_DMA : ATA_CMD_WRITE_DMA;

        WaitForInterrupt(0);    // Clear Interrupt 
        
        if (!SetupDMA(CurBuffer, dwNumSg, fRead)) 
        {
            dwError = fRead ? ERROR_READ_FAULT : ERROR_WRITE_FAULT;
            goto ExitFailure;
        }    
        

        if (!SendIOCommand(dwStartSector, dwSectorsToTransfer, bCmd)) 
        {
            dwError = fRead ? ERROR_READ_FAULT : ERROR_WRITE_FAULT;
            AbortDMA();
            goto ExitFailure;
        }

        if (BeginDMA(fRead)) 
        {
            if (m_fInterruptSupported) 
            {
                if (!WaitForInterrupt(DISK_IO_TIME_OUT) || (CheckIntrState() == ATA_INTR_ERROR)) 
                {  
                    DEBUGMSG( ZONE_ERROR, (TEXT("ATAPI:ReadWrite- WaitforInterrupt failed (DevId %x) \r\n"),m_dwDeviceId));
                    dwError = ERROR_READ_FAULT;
                    AbortDMA();
                    goto ExitFailure;
                }
            } 
      
            if (EndDMA()) 
            {
                WaitOnBusy(FALSE);
                CompleteDMA( CurBuffer, pSgReq->sr_num_sg, fRead);
            }    
        }   

        dwStartSector += dwSectorsToTransfer;
        dwStartBufferNum = dwEndBufferNum;
        dwNumSectors -= dwSectorsToTransfer;        
    }    
    
ExitFailure:    
    if (ERROR_SUCCESS != dwError) 
    {
        ResetController();
    }    
    pSgReq->sr_status = dwError;
    return dwError;
}

/*------------------------------------------------------------------------------------------*/
DWORD CDisk::GetStorageId(PIOREQ pIOReq)
{
    DWORD dwError = ERROR_SUCCESS;
    DWORD dwBytesLeft;
    PBYTE pDstOffset;
    PSTORAGE_IDENTIFICATION pStorageId = (PSTORAGE_IDENTIFICATION)pIOReq->pOutBuf;
    if (!pStorageId || (pIOReq->dwOutBufSize < sizeof(STORAGE_IDENTIFICATION)) || !pIOReq->pBytesReturned) 
        return ERROR_INVALID_PARAMETER;
    pStorageId->dwSize = sizeof(STORAGE_IDENTIFICATION);
    pStorageId->dwFlags = 0;  // can be or of {MANUFACTUREID,SERIALNUM}_INVALID

    dwBytesLeft = pIOReq->dwOutBufSize - sizeof(STORAGE_IDENTIFICATION);
    pDstOffset = (PBYTE)(pStorageId + 1);
    
    pStorageId->dwManufactureIDOffset = pDstOffset - (PBYTE)pStorageId;

    SetLastError(ERROR_SUCCESS);    // TODO: Once ATAParseIdString is changed we can modify this
    if (!ATAParseIdString((PBYTE)m_Id.ModelNumber, sizeof(m_Id.ModelNumber), &(pStorageId->dwManufactureIDOffset), &pDstOffset, &dwBytesLeft))
        pStorageId->dwFlags |= MANUFACTUREID_INVALID;
        
    pStorageId->dwSerialNumOffset = pDstOffset - (PBYTE)pStorageId;
    
    if (!ATAParseIdString((PBYTE)m_Id.SerialNumber, sizeof(m_Id.SerialNumber), &(pStorageId->dwSerialNumOffset), &pDstOffset, &dwBytesLeft))
        pStorageId->dwFlags |= SERIALNUM_INVALID;
    
    pStorageId->dwSize = pDstOffset - (PBYTE)pStorageId;    // required size

    *(pIOReq->pBytesReturned)= min(pStorageId->dwSize, pIOReq->dwOutBufSize);       // bytes written
    
    // ATAParseIdString sets the error value
    dwError = GetLastError(); 
        
    return dwError;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -