ps2port.cpp

来自「WinCE 3.0 BSP, 包含Inter SA1110, Intel_815」· C++ 代码 · 共 832 行 · 第 1/2 页

CPP
832
字号
    bRet = true;

  leave:
    return bRet;
}

/*
 * Ps2Port::SelfTest
 *
 * Writes the self test command to the 8042 and reads the response.
 *
 * Returns true if the success response is read from the 8042.
 */
bool Ps2Port::SelfTest(void)
{
    bool bRet = false;
    UINT8 ui8Data;

    EnterWrite();

    if (!CommandPut(cmd8042SelfTest))
        goto leave;

    if (!OutputBufPollRead(&ui8Data))
        goto leave;

    DEBUGMSG(1, (TEXT("Selftest response = 0x%X\r\n"), ui8Data));

    if (ui8Data != 0x55)
    {
        ASSERT(0);
        goto leave;
    }

    bRet = true;

leave:
    if (!bRet)
    {
    }

    LeaveWrite();
    return bRet;
}

/*
 * Ps2Port::KeyboardInterfaceTest
 *
 * Writes the keyboard interface test command to the 8042 command register
 * and reads the response.
 *
 * Returns true if the success response is read.
 */
bool Ps2Port::KeyboardInterfaceTest(void)
{
    bool bRet = false;
    UINT8 ui8Data;

    EnterWrite();

    if (!CommandPut(cmd8042KeyboardInterfaceTest))
        goto leave;

    if (!OutputBufPollRead(&ui8Data))
        goto leave;

    if (ui8Data != 0x00)
    {
        ASSERT(0);
        goto leave;
    }

    bRet = true;

  leave:
    if (!bRet)
    {
    }

    LeaveWrite();
    return bRet;
}

/*
 * Ps2Port::KeyboardCommandPut
 *
 * Writes a command to the keyboard and reads the response.
 *
 * Returns true if the success response code is read.
 */
bool Ps2Port::KeyboardCommandPut(UINT8 ui8Cmd)
{
    bool bRet = false;
    UINT8 ui8Data;

    EnterWrite();

    if (!InputBufPut(ui8Cmd))
        goto leave;

    if (!OutputBufPollRead(&ui8Data))
        goto leave;

    if (ui8Data != response8042Ack)
    {
        ASSERT(0);
        goto leave;
    }

    bRet = true;

leave:
    LeaveWrite();
    return bRet;
}

/*
 * Ps2Port::KeyboardReset:
 *
 * Sends the keyboard reset command to the keyboard and reads the response.
 *
 * Returns true if the success response is read.
 */
bool Ps2Port::KeyboardReset(void)
{
    bool bRet = false;
    UINT8 ui8Data;

    EnterWrite();

    if (!KeyboardCommandPut(cmdKeybdReset))
        goto leave;

    if (!OutputBufPollRead(&ui8Data))
        goto leave;

    if (ui8Data != 0xAA)
    {
        ASSERT(0);
        goto leave;
    }

    bRet = true;

  leave:
    if (!bRet)
    {
    }

    LeaveWrite();
    return bRet;
}

/*
 * Ps2Port::KeyboardLights
 *
 * Sets the keyboard indicator lights.
 */
void Ps2Port::KeyboardLights(unsigned int fLights)
{
    EnterWrite();

    if (!KeyboardCommandPut(cmdKeybdLights))
        goto leave;

    if (!KeyboardCommandPut(fLights))
        goto leave;

  leave:
    LeaveWrite();
    return;
}

/*
 * Ps2Port::CmdByteModify
 *
 * Modifies the command byte of the Ps2Port object and writes the changes to
 * the 8042 command byte.
 */
bool Ps2Port::CmdByteModify(UINT8 ui8Set, UINT8 ui8Clear)
{
    bool bRet = false;

    EnterWrite();

    m_ui8CmdByte |= ui8Set;
    m_ui8CmdByte &= ~ui8Clear;
    bRet = true;

    LeaveWrite();
    return bRet;
}

/*
 * Ps2Port::MouseCommandPut
 *
 * Writes a command to the auxiliary device.
 */
void Ps2Port::MouseCommandPut(UINT8 cmd)
{
    EnterWrite();

    CommandPut(cmd8042AuxDeviceWrite);
    InputBufPut(cmd);

    LeaveWrite();
    return;
}

/*
 * Ps2Port::MouseTest
 *
 * Tests for a mouse present on the 8042 auxiliary port.
 *
 * Returns true if a mouse is found.
 */
bool Ps2Port::MouseTest(void)
{
    bool bRet = false;
    UINT8 ui8Data;

    EnterWrite();

    MouseCommandPut(0xFF);

    if (!OutputBufPollRead(&ui8Data))
        goto leave;

    MouseCommandPut(cmdMouseReadId);

    if (!OutputBufPollRead(&ui8Data))
        //ui8Data should contain response8042Ack
        goto leave;

    if (!OutputBufPollRead(&ui8Data))
        //ui8Data should contain mouse ID
        goto leave;

    bRet = true;

  leave:
    if (!bRet)
        DEBUGMSG(1,(TEXT("MouseTest fails. Mouse probably not present.\r\n")));

    LeaveWrite();
    return bRet;
}

/*
 * Ps2Port::SetModeIntelliMouse
 *
 * Set mouse to IntelliMouse mode.  If an IntelliMouse is present:
 *
 *         1. mouse ID becomes response8042IntelliMouseId
 *         2. the motion report format changes
 *
 * If IntelliMouse not present the mouse ID and motion report format
 * remain at the default values
 */
void Ps2Port::SetModeIntelliMouse(void)
{
    EnterWrite();

    /*
     * IntelliMouse(R) is uniquely identified by issuing the specific
     *  series of Set Report Rate commands:
     *
     *     200Hz (0xC8), then
     *     100Hz (0x64), then
     *      80Hz (0x50).
     *
     * The Set Report Rate commands are valid and we therefore have to
     * set the report rate back to the default 100Hz (this is done by
     * MouseId()).
     */
    MouseCommandPut(cmdMouseSetReportRate);
    MouseCommandPut(0xC8);
    MouseCommandPut(cmdMouseSetReportRate);
    MouseCommandPut(0x64);
    MouseCommandPut(cmdMouseSetReportRate);
    MouseCommandPut(0x50);

    LeaveWrite();
}

UINT8 Ps2Port::MouseId(void)
{
    UINT8 ui8MouseId;

    EnterWrite();

    MouseCommandPut(cmdMouseReadId);
    OutputBufPollRead(&ui8MouseId);
    ASSERT(ui8MouseId == response8042Ack);

    OutputBufPollRead(&ui8MouseId);

    // Set the report rate back to the default 100Hz
    MouseCommandPut(cmdMouseSetReportRate);
    MouseCommandPut(0x64);

    LeaveWrite();
    return ui8MouseId;
}

/*
 * Ps2Port::MouseDataRead
 *
 * Reads data from the 8042 output port.
 */
bool Ps2Port::MouseDataRead(UINT8 *pui8Data)
{
    if (!AuxOutputBufIsFull())
    {
        DEBUGMSG(1, (TEXT("MouseDataRead: Mouse data not ready\r\n")));
        ERRORMSG(1, (TEXT("MouseDataRead: Mouse data not ready\r\n")));
        return false;
    }

    *pui8Data = ui8OutputBufRead();
    return true;
}

/*
 * Ps2Port::MouseInterruptEnable
 *
 * Enables 8042 auxiliary interrupts.
 */
bool Ps2Port::MouseInterruptEnable(void)
{
    EnterWrite();

    m_ui8CmdByte |= cmdByteEnableAuxInterrupts;

    LeaveWrite();
    return true;
}

/*
 * Ps2Port::KeybdInterruptEnable
 *
 * Enables 8042 keyboard interrupts.
 */
bool Ps2Port::KeybdInterruptEnable(void)
{
    EnterWrite();

    m_ui8CmdByte |= cmdByteEnableKeybdInterrupts;

    LeaveWrite();
    return true;
}

/*
 *
 * Ps2Port::KeybdDataRead
 *
 * Reads data from 8042 output port.
 */
bool Ps2Port::KeybdDataRead(UINT8 *pui8Data)
{
    if (!MainOutputBufIsFull())
        ERRORMSG(1, (TEXT("KeybdDataRead: Keybd data not ready\r\n")));

    *pui8Data = ui8OutputBufRead();
    return TRUE;
}

/*
 * Ps2Port::Initialize
 *
 * Initializes the Ps2Port object.
 */
bool Ps2Port::Initialize(unsigned int iopBase)
{
    bool bRet = false;

    m_iopBase = iopBase;

    InitializeCriticalSection(&m_csWrite);

    m_cEnterWrites = 0;

    m_bMouseFound = false;
    m_bIntelliMouseFound = false;

    if (!SelfTest())
        goto leave;


    if (MouseTest())
    {
        m_bMouseFound = true;
        DEBUGMSG(1, (TEXT("Found PS2 Mouse\r\n")));
        SetModeIntelliMouse();

        // Read mouse ID to determine if IntelliMouse is present.
        if(response8042IntelliMouseId == MouseId())
        {
            DEBUGMSG(1, (TEXT("What's more... it is an IntelliMouse\r\n")));
            m_bIntelliMouseFound = true;
        }

        // Turn on the aux interface
        CmdByteModify(0, cmdByteDisableAuxInterface);
        MouseCommandPut(cmdMouseEnable);
    }

    bRet = true;

  leave:
    return bRet;
}

/* EOF ps2port.cpp */

⌨️ 快捷键说明

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