📄 mpu.cpp
字号:
/*****************************************************************************
* MPU.cpp - UART miniport implementation
*****************************************************************************
* Copyright (c) 1998-2000 Microsoft Corporation. All rights reserved.
*
* Sept 98 MartinP .
*/
#include "private.h"
#include "ksdebug.h"
#define STR_MODULENAME "DMusUART:MPU: "
#define UartFifoOkForWrite(status) ((status & MPU401_DRR) == 0)
#define UartFifoOkForRead(status) ((status & MPU401_DSR) == 0)
typedef struct
{
CMiniportDMusUART *Miniport;
PUCHAR PortBase;
PVOID BufferAddress;
ULONG Length;
PULONG BytesRead;
}
SYNCWRITECONTEXT, *PSYNCWRITECONTEXT;
BOOLEAN TryMPU(IN PUCHAR PortBase);
NTSTATUS WriteMPU(IN PUCHAR PortBase,IN BOOLEAN IsCommand,IN UCHAR Value);
#pragma code_seg("PAGE")
// make sure we're in UART mode
NTSTATUS ResetHardware(PUCHAR portBase)
{
PAGED_CODE();
return WriteMPU(portBase,COMMAND,MPU401_CMD_UART);
}
#pragma code_seg("PAGE")
//
// We initialize the UART with interrupts suppressed so we don't
// try to service the chip prematurely.
//
NTSTATUS CMiniportDMusUART::InitializeHardware(PINTERRUPTSYNC interruptSync,PUCHAR portBase)
{
PAGED_CODE();
NTSTATUS ntStatus;
if (m_UseIRQ)
{
ntStatus = interruptSync->CallSynchronizedRoutine(InitMPU,PVOID(portBase));
}
else
{
ntStatus = InitMPU(NULL,PVOID(portBase));
}
if (NT_SUCCESS(ntStatus))
{
//
// Start the UART (this should trigger an interrupt).
//
ntStatus = ResetHardware(portBase);
}
else
{
_DbgPrintF(DEBUGLVL_TERSE,("*** InitMPU returned with ntStatus 0x%08x ***",ntStatus));
}
m_fMPUInitialized = NT_SUCCESS(ntStatus);
return ntStatus;
}
#pragma code_seg()
/*****************************************************************************
* InitMPU()
*****************************************************************************
* Synchronized routine to initialize the MPU401.
*/
NTSTATUS
InitMPU
(
IN PINTERRUPTSYNC InterruptSync,
IN PVOID DynamicContext
)
{
_DbgPrintF(DEBUGLVL_BLAB, ("InitMPU"));
if (!DynamicContext)
{
return STATUS_INVALID_PARAMETER_2;
}
PUCHAR portBase = PUCHAR(DynamicContext);
UCHAR status;
ULONGLONG startTime;
BOOLEAN success;
NTSTATUS ntStatus = STATUS_SUCCESS;
//
// Reset the card (puts it into "smart mode")
//
ntStatus = WriteMPU(portBase,COMMAND,MPU401_CMD_RESET);
// wait for the acknowledgement
// NOTE: When the Ack arrives, it will trigger an interrupt.
// Normally the DPC routine would read in the ack byte and we
// would never see it, however since we have the hardware locked (HwEnter),
// we can read the port before the DPC can and thus we receive the Ack.
startTime = PcGetTimeInterval(0);
success = FALSE;
while(PcGetTimeInterval(startTime) < GTI_MILLISECONDS(50))
{
status = READ_PORT_UCHAR(portBase + MPU401_REG_STATUS);
if (UartFifoOkForRead(status)) // Is data waiting?
{
READ_PORT_UCHAR(portBase + MPU401_REG_DATA); // yep.. read ACK
success = TRUE; // don't need to do more
break;
}
KeStallExecutionProcessor(25); // microseconds
}
#if (DBG)
if (!success)
{
_DbgPrintF(DEBUGLVL_VERBOSE,("First attempt to reset the MPU didn't get ACKed.\n"));
}
#endif // (DBG)
// NOTE: We cannot check the ACK byte because if the card was already in
// UART mode it will not send an ACK but it will reset.
// reset the card again
(void) WriteMPU(portBase,COMMAND,MPU401_CMD_RESET);
// wait for ack (again)
startTime = PcGetTimeInterval(0); // This might take a while
BYTE dataByte = 0;
success = FALSE;
while (PcGetTimeInterval(startTime) < GTI_MILLISECONDS(50))
{
status = READ_PORT_UCHAR(portBase + MPU401_REG_STATUS);
if (UartFifoOkForRead(status)) // Is data waiting?
{
dataByte = READ_PORT_UCHAR(portBase + MPU401_REG_DATA); // yep.. read ACK
success = TRUE; // don't need to do more
break;
}
KeStallExecutionProcessor(25);
}
if ((0xFE != dataByte) || !success) // Did we succeed? If no second ACK, something is hosed
{
_DbgPrintF(DEBUGLVL_TERSE,("Second attempt to reset the MPU didn't get ACKed.\n"));
_DbgPrintF(DEBUGLVL_TERSE,("Init Reset failure error. Ack = %X", ULONG(dataByte) ) );
ntStatus = STATUS_IO_DEVICE_ERROR;
}
return ntStatus;
}
#pragma code_seg()
/*****************************************************************************
* CMiniportDMusUARTStream::Write()
*****************************************************************************
* Writes outgoing MIDI data.
*/
STDMETHODIMP_(NTSTATUS)
CMiniportDMusUARTStream::
Write
(
IN PVOID BufferAddress,
IN ULONG Length,
OUT PULONG BytesWritten
)
{
_DbgPrintF(DEBUGLVL_BLAB, ("Write"));
ASSERT(BytesWritten);
if (!BufferAddress)
{
Length = 0;
}
NTSTATUS ntStatus = STATUS_SUCCESS;
if (!m_fCapture)
{
PUCHAR pMidiData;
ULONG count;
count = 0;
pMidiData = PUCHAR(BufferAddress);
if (Length)
{
SYNCWRITECONTEXT context;
context.Miniport = (m_pMiniport);
context.PortBase = m_pPortBase;
context.BufferAddress = pMidiData;
context.Length = Length;
context.BytesRead = &count;
if (m_pMiniport->m_UseIRQ)
{
ntStatus = m_pMiniport->m_pInterruptSync->
CallSynchronizedRoutine(SynchronizedDMusMPUWrite,PVOID(&context));
}
else // !m_UseIRQ
{
ntStatus = SynchronizedDMusMPUWrite(NULL,PVOID(&context));
} // !m_UseIRQ
if (count == 0)
{
m_NumFailedMPUTries++;
if (m_NumFailedMPUTries >= 100)
{
ntStatus = STATUS_IO_DEVICE_ERROR;
m_NumFailedMPUTries = 0;
}
}
else
{
m_NumFailedMPUTries = 0;
}
} // if we have data at all
*BytesWritten = count;
}
else // called write on the read stream
{
ntStatus = STATUS_INVALID_DEVICE_REQUEST;
}
return ntStatus;
}
#pragma code_seg()
/*****************************************************************************
* SynchronizedDMusMPUWrite()
*****************************************************************************
* Writes outgoing MIDI data.
*/
NTSTATUS
SynchronizedDMusMPUWrite
(
IN PINTERRUPTSYNC InterruptSync,
IN PVOID syncWriteContext
)
{
PSYNCWRITECONTEXT context;
context = (PSYNCWRITECONTEXT)syncWriteContext;
ASSERT(context->Miniport);
ASSERT(context->PortBase);
ASSERT(context->BufferAddress);
ASSERT(context->Length);
ASSERT(context->BytesRead);
PUCHAR pChar = PUCHAR(context->BufferAddress);
NTSTATUS ntStatus,readStatus;
ntStatus = STATUS_SUCCESS;
//
// while we're not there yet, and
// while we don't have to wait on an aligned byte (including 0)
// (we never wait on a byte. Better to come back later)
readStatus = DMusMPUInterruptServiceRoutine(InterruptSync,PVOID(context->Miniport));
while ( (*(context->BytesRead) < context->Length)
&& ( TryMPU(context->PortBase)
|| (*(context->BytesRead)%3)
) )
{
ntStatus = WriteMPU(context->PortBase,DATA,*pChar);
if (NT_SUCCESS(ntStatus))
{
pChar++;
*(context->BytesRead) = *(context->BytesRead) + 1;
// readStatus = DMusMPUInterruptServiceRoutine(InterruptSync,PVOID(context->Miniport));
}
else
{
_DbgPrintF(DEBUGLVL_TERSE,("SynchronizedDMusMPUWrite failed (0x%08x)",ntStatus));
break;
}
}
readStatus = DMusMPUInterruptServiceRoutine(InterruptSync,PVOID(context->Miniport));
return ntStatus;
}
#define kMPUPollTimeout 2
#pragma code_seg()
/*****************************************************************************
* TryMPU()
*****************************************************************************
* See if the MPU401 is free.
*/
BOOLEAN
TryMPU
(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -