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

📄 diskmain.h

📁 EP931X系列的WinCE显卡驱动源代码
💻 H
字号:
//
// 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.
//
#ifndef _DISKMAIN_H_
#define _DISKMAIN_H_

#include <atapi2.h>
#include <atapiio.h>

//
// Device Extension Device Flags
//

#define DFLAGS_DEVICE_PRESENT        (1 << 0)    // Indicates that some device is present.
#define DFLAGS_ATAPI_DEVICE          (1 << 1)    // Indicates whether Atapi commands can be used.
#define DFLAGS_TAPE_DEVICE           (1 << 2)    // Indicates whether this is a tape device.
#define DFLAGS_INT_DRQ               (1 << 3)    // Indicates whether device interrupts as DRQ is set after
                                                 // receiving Atapi Packet Command

#define DFLAGS_REMOVABLE_DRIVE       (1 << 4)    // Indicates that the drive has the 'removable' bit set in
                                                 // identify data (offset 128)
#define DFLAGS_MEDIA_STATUS_ENABLED  (1 << 5)    // Media status notification enabled

#define DFLAGS_DEVICE_ISDVD          (1 << 6)    // Indicates that the drive is DVD and we use 12 bit reads
#define DFLAGS_MEDIA_ISDVD           (1 << 7)    // Indicates that the media is DVD

#define DFLAGS_USE_DMA               (1 << 9)    // Indicates whether device can use DMA
#define DFLAGS_LBA                   (1 << 10)   // support LBA addressing
#define DFLAGS_MULTI_LUN_INITED      (1 << 11)   // Indicates that the init path for multi-lun has already been done.

#define DFLAGS_MSN_SUPPORT           (1 << 12)   // Device support media status notification
#define DFLAGS_AUTO_EJECT_ZIP        (1 << 13)   // bootup default enables auto eject
#define DFLAGS_WD_MODE               (1 << 14)   // Indicates that unit is WD-Mode(not SFF-Mode).

#define DFLAGS_USE_UDMA              (1 << 16)    // Indicates whether device can use UDMA
#define DFLAGS_IDENTIFY_VALID        (1 << 17)    // Indicates whether the Identify data is valid or not

#define DFLAGS_DEVICE_PWRDN          (1 << 24)  // Indicates that the device has been powered down

#define DFLAGS_DEVICE_BUSY           (1 << 25)    // Indicates whether Device is in use
#define DFLAGS_DEVICE_CDROM          (1 << 26)    // Indicates whether Device CD-ROM
#define DFLAGS_MEDIA_CHANGED         (1 << 27)    // Set If Media Changed
#define DFLAGS_DEVICE_READY          (1 << 28)    // If Removeable Device  is Ready
#define DFLAGS_DEVICE_ACTIVATED      (1 << 29)    // If Removeable Device Activated / Mounted
#define DFLAGS_DEVICE_INITIALIZED    (1 << 30)    // If FS is Loaded & Asking for Dev Info.


#ifdef FAKE_CRC_ERROR
#define DFLAGS_FAKING_CRC_ERROR      (1 << 31)    // faking crc error
#endif // FAKE_CRC_ERROR


#define     ATA_VERSION_MASK    (0xfffe)
#define     ATA1_COMPLIANCE     (1 << 1)
#define     ATA2_COMPLIANCE     (1 << 2)
#define     ATA3_COMPLIANCE     (1 << 3)
#define     ATA4_COMPLIANCE     (1 << 4)


//
// Wait type codes for WaitForDisc
//
#define WAIT_TYPE_BUSY      1
#define WAIT_TYPE_NOT_BUSY  2
#define WAIT_TYPE_READY     3
#define WAIT_TYPE_DRQ       4   // wait for data request
#define WAIT_TYPE_NOT_DRQ   5 
#define WAIT_TYPE_ERROR     6

#if (_WIN32_WCE==400)
    void StallExecution(ULONG ulMicroSec);
#endif 

class CDisk
{
public:
    DWORD m_dwDeviceFlags;
    DWORD dwPktSize ; //  ATAPI command packet size, currently 12 or 16 bytes
public:
    CDisk *m_pNextDisk;
    HKEY m_hDevKey;  // Points to the driver key
    HANDLE m_hDevice;
    CRITICAL_SECTION m_csDisk;
    DWORD m_dwOpenCount; // Open Count;
    DWORD m_dwStateFlag;
    TCHAR m_szActiveKey[MAX_PATH];
    TCHAR m_szDeviceKey[MAX_PATH];
public:
    //volatile PBYTE m_pATAReg;          // MmMapped pointer to ATA register range (Is in IO Space for x86)
    //volatile PBYTE m_pATARegAlt;       // MmMapped pinter to ATA Alt regsiters (Is in IO Space for x86)
    IDENTIFY_DATA m_Id;               // Data filled from ATA Identify command
    BOOL m_fAtapiDevice;
    BOOL m_fDMAActive;
 
public:
    DWORD m_dwDevice;
    DWORD m_dwDeviceId;
    DWORD m_dwPort;
    BOOL m_f16Bit; // 8 bit or 16 bit access to the port ...should only be set for now on PCMCIA
    PTSTR  m_szDiskName;
public:
    DISK_INFO m_DiskInfo;                             // Disk Geometry Info
    BOOL    m_fLBAMode;                          // TRUE => logical block addressing supported
    BYTE    m_bSectorsPerBlock;                // Number of Sector Per Block
    BYTE    m_bReadCommand;                 // Current Read Command 
    BYTE    m_bWriteCommand;                //Current Write Command;
    WORD    m_wNextByte;                     // Read but nod used byte (if not -1)
    BOOL    m_fInterruptSupported;
     
public:
    // These are required for removable media
    DWORD m_dwUnitReadyTime;                // Tick count from last unit ready check
    DWORD m_dwLastCheckTime;                

    // IO Operation 
    DWORD   m_dwBestPioCycleTime;
    DWORD   m_dwBestSwDmaCycleTime;
    DWORD   m_dwBestMwDmaCycleTime;
    DWORD   m_dwBestUDmaCycleTime;

    DWORD   m_dwTransferModeSupported;
    DWORD   m_dwBestPioMode;
    DWORD   m_dwBestSwDmaMode;
    DWORD   m_dwBestMwDmaMode;
    DWORD   m_dwBestUDmaMode;
    DWORD   m_dwTransferModeCurrent;

protected:

    BOOL        m_fUDMAActive;
    DWORD       m_ulBestMwDmaModeReg;
    DWORD       m_ulBestUDmaModeReg;



public: // ATAPI stuff
    INQUIRY_DATA m_InqData;
    
public:
    CDisk()    {}
    CDisk(HKEY hKey) :
        m_dwDeviceFlags(0),
        m_pNextDisk(NULL),
        // m_pATAReg(NULL),
        // m_pATARegAlt(NULL),
        m_dwDevice(0),
        m_hDevKey( hKey),
        m_dwDeviceId(0),
        m_dwPort(0),
        m_f16Bit(FALSE),
        m_fAtapiDevice(FALSE),
        m_fInterruptSupported(FALSE),
        m_szDiskName(NULL),
        m_fDMAActive(FALSE),
        m_dwOpenCount(0),
        m_dwUnitReadyTime(0),                
        m_dwBestPioCycleTime(0),
        m_dwBestSwDmaCycleTime(0),
        m_dwBestMwDmaCycleTime(0),
        m_dwBestUDmaCycleTime(0),
        m_dwTransferModeSupported(0),
        m_dwBestPioMode(0),
        m_dwBestSwDmaMode(0),
        m_dwBestMwDmaMode(0),
        m_dwBestUDmaMode(0),
        m_dwTransferModeCurrent(0),
        m_dwStateFlag(0),
        m_dwLastCheckTime(0),
        m_fUDMAActive(FALSE),
        m_ulBestMwDmaModeReg(2),
        m_ulBestUDmaModeReg(4)
    {
        InitializeCriticalSection( &m_csDisk);
        memset( &m_Id, 0, sizeof(IDENTIFY_DATA));
        memset( &m_DiskInfo, 0, sizeof(DISK_INFO));
        memset( &m_InqData, 0, sizeof(INQUIRY_DATA));
    }
    virtual ~CDisk() 
    {
        if (m_hDevKey) 
            RegCloseKey( m_hDevKey);
        DeleteCriticalSection( &m_csDisk);            
    }
    virtual BOOL Init(HKEY hActiveKey);
    void SetActiveKey(TCHAR *szActiveKey)
    {
        wcscpy( m_szActiveKey, szActiveKey);
    }
    void SetDeviceKey(TCHAR *szDeviceKey)
    {
        wcscpy( m_szDeviceKey, szDeviceKey);
    }
    BOOL PostInit(PPOST_INIT_BUF pPostInitBuf);
    BOOL PerformIoctl(PIOREQ pIOReq);
    BOOL ResetController(BOOL bSoftReset=FALSE);
    BOOL Identify();
    BYTE WaitOnBusy(BOOL fBase);
    void AnalyzeDeviceCapabilities ();
    BOOL SetTransferMode(BYTE bMode);
    virtual void SetBestTransferMode() = 0;
public: // Actual DISK functions
    DWORD GetDiskInfo(PIOREQ pIOReq);       
    DWORD SetDiskInfo(PIOREQ pIOReq);       
    virtual DWORD GetDeviceInfo(PIOREQ pIOReq);
    DWORD GetDiskName( PIOREQ pIOReq);
    DWORD ReadWriteDisk( PIOREQ pIOReq, BOOL fRead = TRUE);
    DWORD ReadWriteDiskDMA(PIOREQ pIOReq, BOOL fRead = TRUE);
    DWORD GetStorageId(PIOREQ pIOReq);
    void  PowerUp() { m_dwDeviceFlags &= ~DFLAGS_DEVICE_PWRDN;}
    void  PowerDown() { m_dwDeviceFlags |= DFLAGS_DEVICE_PWRDN;}
public: // ATAPI
    DWORD AtapiIoctl(PIOREQ pIOReq);
    DWORD ReadCdRom(CDROM_READ *pReadInfo, PDWORD pBytesReturned);
    DWORD SetupCdRomRead(BOOL bRawMode, DWORD dwLBAAddr, DWORD dwTransferLength, PATAPI_COMMAND_PACKET pCmdPkt);
    virtual DWORD ReadCdRomDMA(DWORD dwLBAAddr, DWORD dwTransferLength, WORD wSectorSize, DWORD dwSgCount, SGX_BUF *pSgBuf);
    BOOL  AtapiSendCommand(PATAPI_COMMAND_PACKET pCmdPkt, WORD wCount = 0, BOOL fDMA = FALSE);
    BOOL  AtapiReceiveData(PSGX_BUF pSgBuf, DWORD dwSgCount,LPDWORD pdwBytesRead);
    BOOL  AtapiSendData(PSGX_BUF pSgBuf, DWORD dwSgCount,LPDWORD pdwBytesWritten);
    BOOL  AtapiIsUnitReady(PIOREQ pIOReq = NULL);
    BOOL  AtapiIsUnitReadyEx();
    BOOL  AtapiGetSenseInfo(CD_SENSE_DATA *pSenseData);
    BOOL  AtapiIssueInquiry(INQUIRY_DATA *pInqData);
    BOOL  AtapiGetToc(CDROM_TOC *pTOC);
    DWORD AtapiGetDiscInfo(PIOREQ pIOReq);
    DWORD AtapiReadQChannel(PIOREQ pIOReq);
    DWORD AtapiLoadMedia(BOOL bEject=FALSE);
    DWORD AtapiStartDisc();
    BOOL  AtapiDetectDVD();
    void  AtapiDumpSenseData();
public: // CDAUDIO Functions
    DWORD ControlAudio(PIOREQ pIOReq);

public: //DVD Functions
    DWORD DVDReadKey(PIOREQ pIOReq);
    DWORD DVDGetRegion(PIOREQ pIOReq);
    DWORD DVDSendKey(PIOREQ pIOReq);
    DWORD DVDSetRegion(PIOREQ pIOReq);
    BOOL  DVDGetCopySystem(LPBYTE pbCopySystem);


    
public: // IO Functions    
    BOOL SendIOCommand(DWORD dwStartSector, DWORD dwNumberOfSectors, BYTE bCmd);
    virtual BOOL WaitForInterrupt(DWORD dwTimeOut) = 0; 
    WORD CheckIntrState();
    virtual void EnableInterrupt() = 0;
    void  ReadBuffer(PBYTE pBuffer,DWORD dwCount);
    void  WriteBuffer(PBYTE pBuffer,DWORD dwCount);
    BOOL WaitForDRQ();
    BOOL WaitForDisc(BYTE bStatusType, DWORD dwTimeOut, DWORD dwPeriod = 0); 
 public:
    virtual BOOL SetupDMA( PSG_BUF pSgBuf, DWORD dwSgCount, BOOL fRead) = 0;
    virtual BOOL BeginDMA(BOOL fRead) = 0;
    //virtual BOOL ContinueDMA() = 0;
    virtual BOOL EndDMA() = 0;
    virtual BOOL AbortDMA() = 0;
    virtual BOOL CompleteDMA(PSG_BUF pSgBuf, DWORD dwSgCount, BOOL fRead) = 0;

public: // Generic functions
    inline void StallExecution(DWORD dwTime) 
    {
        if ((dwTime >= 100) && (m_dwDeviceFlags & DFLAGS_DEVICE_CDROM)) 
            Sleep (dwTime / 100);
        else 
            ::StallExecution(dwTime*10);         
    }    
    virtual void TakeCS() 
    { 
        EnterCriticalSection( &m_csDisk); 
    }
    virtual void ReleaseCS() 
    { 
        LeaveCriticalSection( &m_csDisk); 
    }
    inline void Open() { InterlockedIncrement( (LONG *)&m_dwOpenCount); }
    void Close() { InterlockedDecrement( (LONG *)&m_dwOpenCount); }
    
public: // inline IO Functions
    // This status function does does NOT ack the interrupt
    virtual BYTE GetAltStatus()                                    = 0;
    // This status function ack's the interrupt
    virtual BYTE GetBaseStatus()                                   = 0;
    virtual BYTE GetError()                                        = 0;
    virtual BYTE GetReason()                                       = 0;
    virtual void SelectDevice()                                    = 0;
    virtual void WriteDriveController(BYTE bData)                  = 0;
    virtual void WriteAltDriveController(BYTE bData)               = 0;
    virtual void SetDriveHead(BYTE bDriveHead)                     = 0;  
    virtual void WriteCommand(BYTE bCommand)                       = 0;
    virtual void WriteFeature(BYTE bFeature)                       = 0;
    virtual void WriteSectorCount(BYTE bValue)                     = 0;
    virtual void WriteDriveHeadReg(BYTE bValue)                    = 0;
    virtual void WriteSectorNumber(BYTE bValue)                    = 0;
    virtual void WriteLowCount(BYTE bValue)                        = 0;
    virtual void WriteHighCount(BYTE bValue)                       = 0;
    virtual BYTE GetLowCount()                                     = 0;
    virtual BYTE GetHighCount()                                    = 0;
    virtual void ReadWordBuffer( PWORD pBuffer, DWORD dwCount)     = 0;
    virtual void WriteWordBuffer( PWORD pBuffer, DWORD dwCount)    = 0;
    virtual void ReadByteBuffer( PBYTE pBuffer, DWORD dwCount)     = 0;
    virtual void WriteByteBuffer( PBYTE pBuffer, DWORD dwCount)    = 0;
    virtual void WriteWord(WORD wData)                             = 0;
    virtual WORD ReadWord()                                        = 0;
    virtual void WriteByte(BYTE bData)                             = 0;
    virtual WORD ReadByte()                                        = 0;
    inline WORD GetCount() 
    {
        return GetLowCount() + ((WORD)GetHighCount() <<8);
    }    

    
public:    // Helper functions
    inline BOOL IsAtapiDevice() 
    {
//        return (m_Id.GeneralConfiguration & IDE_IDDATA_ATAPI_DEVICE);
        return m_fAtapiDevice;
    }    
    inline void AtapiSoftReset()
    {
        WriteCommand( ATAPI_CMD_SOFT_RESET);
        WaitForDisc(WAIT_TYPE_NOT_BUSY,400);
        WaitForDisc(WAIT_TYPE_READY,500);
    }

    inline BOOL IsCDRomDevice() 
    {
        return (((m_Id.GeneralConfiguration >> 8) & 0x1f) == ATA_IDDEVICE_CDROM);
    }    

    inline BOOL IsDVDROMDevice()
    {
         return TRUE;
    }

    inline BOOL IsDiskDevice() 
    {
        return (((m_Id.GeneralConfiguration >> 8) & 0x1f) == ATA_IDDEVICE_DISK);
    }    

    inline BOOL IsRemoveableDevice() 
    {
        return (m_Id.GeneralConfiguration & IDE_IDDATA_REMOVABLE);
    }    
    inline BOOL IsDMASupported()
    {
        return ((m_Id.Capabilities & IDENTIFY_CAPABILITIES_DMA_SUPPORTED) && m_fDMAActive &&
                (int)m_dwBestUDmaMode!=-1 && m_fUDMAActive);
    }    
    inline BOOL IsDRQTypeIRQ()
    {
        return ((m_Id.GeneralConfiguration >> 5) & 0x0003) == ATA_DRQTYPE_INTRQ;
    }    
    inline WORD GetPacketSize()
    {
        return m_Id.GeneralConfiguration & 0x0003 ? 16 : 12;
    }
private:
    BOOL static ValidateSg(PSG_REQ pSgReq ,DWORD InBufLen);
    BOOL static ValidateSg(PCDROM_READ pCdrom,DWORD InBufLen);
};


#endif // _DISKMAIN_H_

⌨️ 快捷键说明

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