📄 mdd.c
字号:
memmove (RxBuffWrite(pSerialHead)+CharIndex,
RxBuffWrite(pSerialHead)+CharIndex+1,
RoomLeft - CharIndex);
RoomLeft--;
// We disabled TX on XOFF, so now we need to start sending
// again. Easiest way is to pretend we saw a TX interrupt
it |= INTR_TX;
continue;
}
CharIndex++;
}
}
pSerialHead->RxBytes += RoomLeft;
RxWrite(pSerialHead) += RoomLeft;
RxWrite(pSerialHead) %= RxLength(pSerialHead);
if ( RoomLeft ) {
RxDataAvail = TRUE;
}
/* Support DTR_CONTROL_HANDSHAKE/RTS_CONTROL_HANDSHAKE
* signal is cleared when the input buffer is more than 3/4 full.
*/
if ( (pSerialHead->DCB.fDtrControl == DTR_CONTROL_HANDSHAKE) &&
(!pSerialHead->DtrFlow) &&
(4*RxBytesAvail(pSerialHead) > (3*RxLength(pSerialHead))) ) {
DEBUGMSG (ZONE_READ|ZONE_FLOW,
(TEXT("DTR_CONTROL_HANDSHAKE Clearing DTR\r\n")));
pSerialHead->DtrFlow = 1;
pFuncTbl->HWClearDTR(pHWHead);
}
if ( (pSerialHead->DCB.fRtsControl == RTS_CONTROL_HANDSHAKE) &&
(!pSerialHead->RtsFlow) &&
(4*RxBytesAvail(pSerialHead) > (3*RxLength(pSerialHead))) ) {
DEBUGMSG (ZONE_READ|ZONE_FLOW,
(TEXT("RTS_CONTROL_HANDSHAKE Clearing RTS\r\n")));
pSerialHead->RtsFlow = 1;
pFuncTbl->HWClearRTS(pHWHead);
}
/* If Xon/Xoff flow control is desired. check the limit against
* the remaining room and act accordingly.
*/
if ( pSerialHead->DCB.fInX && !(pSerialHead->SentXoff) &&
( pSerialHead->DCB.XoffLim >=
(RxLength(pSerialHead) - RxBytesAvail(pSerialHead))) ) {
DEBUGMSG (ZONE_FLOW, (TEXT("Sending XOFF\r\n")));
pFuncTbl->HWXmitComChar(pHWHead, pSerialHead->DCB.XoffChar);
pSerialHead->SentXoff = 1;
if ( !pSerialHead->DCB.fTXContinueOnXoff ) {
pSerialHead->StopXmit = 1;
}
}
}
if ( it & INTR_TX ) {
DEBUGMSG (ZONE_THREAD|ZONE_WRITE , (TEXT("Tx Event\r\n")));
DoTxData( pSerialHead );
}
if ( (it & INTR_MODEM) ) {
DEBUGMSG (ZONE_THREAD, (TEXT("Other Event, it:%x\r\n"), it));
/* Call low level status clean up code.
*/
pFuncTbl->HWModemIntrHandler(pHWHead);
}
if ( it & INTR_LINE ) {
DEBUGMSG (ZONE_THREAD, (TEXT("Line Event, it:%x\r\n"), it));
/* Call low level line status clean up code.
* Then unmask the interrupt
*/
pFuncTbl->HWLineIntrHandler(pHWHead);
}
}
// We kept this till the end to optimize the above loop
if ( RxDataAvail ) {
// Signal COM_Read that bytes are available.
SetEvent(pSerialHead->hReadEvent);
EvaluateEventFlag(pSerialHead, EV_RXCHAR);
}
DEBUGMSG (ZONE_THREAD ,
(TEXT("-SerialEventHandler, Fifo(R=%d,W=%d,L=%d)\r\n"),
RxRead(pSerialHead), RxWrite(pSerialHead),
RxLength(pSerialHead)));
if ( pSerialHead->pAccessOwner )
COM_DEC_USAGE_CNT(pSerialHead->pAccessOwner);
return;
}
/*
@doc INTERNAL
@func ULONG | SerialDispatchThread | Main serial event dispatch thread code.
* This is the reading and dispatching thread. It gets the
* event associated with the logical interrupt dwIntID and calls
* hardware specific routines to determine whether it's a receive event
* or a transmit event. If it's a transmit event, it calls the HW tx handler.
* If it's a receive event, it calls for the number of characters and calls
* atomic GetByte() to extract characters and put them into the drivers
* buffer represented by pSerialHead->pTargetBuffer, managing waiting
* for events and checking to see if those signals correspond to reading.
* It relies on NK masking the interrupts while it does it's thing, calling
* InterruptDone() to unmask them for each of the above cases.
*
* Not exported to users.
*
@rdesc This thread technically returns a status, but in practice, doesn't return
* while the device is open.
*/
static DWORD WINAPI
SerialDispatchThread(
PVOID pContext /* @parm [IN] Pointer to main data structure. */
)
{
PHW_INDEP_INFO pSerialHead = (PHW_INDEP_INFO)pContext;
ULONG WaitReturn;
DEBUGMSG (ZONE_THREAD, (TEXT("Entered SerialDispatchThread %X\r\n"),
pSerialHead));
// It is possible for a PDD to use this routine in its private thread, so
// don't just assume that the MDD synchronization mechanism is in place.
if ( pSerialHead->pHWObj->BindFlags & THREAD_IN_MDD ) {
DEBUGMSG(ZONE_INIT,
(TEXT("Spinning in dispatch thread %X %X\n\r"), pSerialHead, pSerialHead->pHWObj));
while ( !pSerialHead->pDispatchThread ) {
Sleep(20);
}
}
/* Wait for the event that any serial port action creates.
*/
while ( !pSerialHead->KillRxThread ) {
DEBUGMSG (ZONE_THREAD, (TEXT("Event %X, %d\r\n"),
pSerialHead->hSerialEvent,
pSerialHead->pHWObj->dwIntID ));
WaitReturn = WaitForSingleObject(pSerialHead->hSerialEvent, INFINITE);
SerialEventHandler(pSerialHead);
InterruptDone(pSerialHead->pHWObj->dwIntID);
}
DEBUGMSG (ZONE_THREAD, (TEXT("SerialDispatchThread %x exiting\r\n"),
pSerialHead));
return(0);
}
// ****************************************************************
//
// @doc INTERNAL
// @func BOOL | StartDispatchThread | Start thread if requested by PDD.
//
// @parm ULONG | pSerialHead
//
// @rdesc TRUE if success, FALSE if failed.
//
BOOL
StartDispatchThread(
PHW_INDEP_INFO pSerialHead
)
{
// Initialize the interrupt to be associated with the hSerialEvent
// event. GetByte waits on this event and acts as a second
// level decoder determining the type of serial event. If this return
// fails, then another process has registered for the interrupt, so
// fail the init and set the hSerialEvent to NULL.
DEBUGMSG(ZONE_INIT,
(TEXT("Initializing interrupt 0x%X, 0x%X\n\r"),
pSerialHead->pHWObj->dwIntID, pSerialHead->hSerialEvent));
if ( !InterruptInitialize(pSerialHead->pHWObj->dwIntID,
pSerialHead->hSerialEvent,
NULL,
0) ) {
DEBUGMSG(ZONE_INIT | ZONE_ERROR,
(TEXT("Error initializing interrupt\n\r")));
return(FALSE);
}
InterruptDone(pSerialHead->pHWObj->dwIntID);
// Set up the dispatch thread and it's kill flag. Note that the thread
// fills in its own handle in pSerialHead.
pSerialHead->KillRxThread = 0;
pSerialHead->pDispatchThread = NULL;
DEBUGMSG(ZONE_INIT,
(TEXT("Spinning thread%X\n\r"), pSerialHead));
pSerialHead->pDispatchThread = CreateThread(NULL,0, SerialDispatchThread,
pSerialHead, 0,NULL);
if ( pSerialHead->pDispatchThread == NULL ) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR,
(TEXT("Error creating dispatch thread (%d)\n\r"),
GetLastError()));
return(FALSE);
}
DEBUGMSG (ZONE_INIT, (TEXT("Created receive thread %X\r\n"),
pSerialHead->pDispatchThread));
return(TRUE);
}
// ****************************************************************
//
// @doc INTERNAL
// @func BOOL | StartDispatchThread | Stop thread, disable interrupt.
//
// @parm ULONG | pSerialHead
//
// @rdesc TRUE if success, FALSE if failed.
//
BOOL
StopDispatchThread(
PHW_INDEP_INFO pSerialHead
)
{
HANDLE pThisThread = GetCurrentThread();
ULONG priority256;
/* If we have an interrupt handler thread, kill it */
if ( pSerialHead->pDispatchThread ) {
DEBUGMSG (ZONE_INIT, (TEXT("\r\nTrying to close dispatch thread\r\n")));
/* Set the priority of the dispatch thread to be equal to this one,
* so that it shuts down before we free its memory. If this routine
* has been called from SerialDllEntry then RxCharBuffer is set to
* NULL and the dispatch thread is already dead, so just skip the
* code which kills the thread.
*/
priority256 = CeGetThreadPriority(pThisThread);
CeSetThreadPriority(pSerialHead->pDispatchThread, priority256);
/* Signal the Dispatch thread to die.
*/
pSerialHead->KillRxThread = 1;
DEBUGMSG (ZONE_INIT, (TEXT("\r\nTrying to signal serial thread.\r\n")));
SetEvent(pSerialHead->hSerialEvent);
WaitForSingleObject(pSerialHead->hKillDispatchThread, 3000);
Sleep(10);
DEBUGMSG (ZONE_INIT, (TEXT("\r\nTrying to call CloseHandle\r\n")));
CloseHandle(pSerialHead->pDispatchThread);
pSerialHead->pDispatchThread = NULL;
DEBUGMSG (ZONE_INIT, (TEXT("\r\nReturned from CloseHandle\r\n")));
}
if ( pSerialHead->pHWObj ) {
/* Ack any remaining interrupts and unregister the event from the
* logical interrupt.
*/
InterruptDone(pSerialHead->pHWObj->dwIntID);
InterruptDisable(pSerialHead->pHWObj->dwIntID);
}
return(TRUE);
}
/*
* @doc INTERNAL
* @func BOOL | ApplyDCB | Apply the current DCB.
*
* This function will apply the current DCB settings to the device.
* It will also call to the PDD to set the PDD values.
*/
BOOL
ApplyDCB (PHW_INDEP_INFO pSerialHead, DCB *pDCB, BOOL fOpen)
{
PHWOBJ pHWObj = pSerialHead->pHWObj;
if ( !pHWObj->pFuncTbl->HWSetDCB(pSerialHead->pHWHead,
pDCB) ) {
return(FALSE);
}
if ( !fOpen ) {
return(TRUE);
}
// If PDD SetDCB was successful, save the supplied DCB and
// configure port to match these settings.
memcpy(&(pSerialHead->DCB), pDCB, sizeof(DCB));
if ( pSerialHead->DCB.fDtrControl == DTR_CONTROL_DISABLE ) {
pHWObj->pFuncTbl->HWClearDTR(pSerialHead->pHWHead);
} else if ( pSerialHead->DCB.fDtrControl == DTR_CONTROL_ENABLE ) {
pHWObj->pFuncTbl->HWSetDTR(pSerialHead->pHWHead);
}
if ( pSerialHead->DCB.fRtsControl == RTS_CONTROL_DISABLE ) {
pHWObj->pFuncTbl->HWClearRTS(pSerialHead->pHWHead);
} else if ( pSerialHead->DCB.fRtsControl == RTS_CONTROL_ENABLE ) {
pHWObj->pFuncTbl->HWSetRTS(pSerialHead->pHWHead);
}
if ( pSerialHead->DCB.fDtrControl == DTR_CONTROL_HANDSHAKE ) {
if ( (!pSerialHead->DtrFlow) &&
(4*RxBytesAvail(pSerialHead) > (3*RxLength(pSerialHead))) ) {
DEBUGMSG (ZONE_READ|ZONE_FLOW,
(TEXT("IOCTL:DTR_CONTROL_HANDSHAKE Clearing DTR\r\n")));
pSerialHead->DtrFlow = 1;
pHWObj->pFuncTbl->HWClearDTR(pSerialHead->pHWHead);
} else {
DEBUGMSG (ZONE_READ|ZONE_FLOW,
(TEXT("IOCTL:DTR_CONTROL_HANDSHAKE Setting DTR\r\n")));
pSerialHead->DtrFlow = 0;
pHWObj->pFuncTbl->HWSetDTR(pSerialHead->pHWHead);
}
}
if ( pSerialHead->DCB.fRtsControl == RTS_CONTROL_HANDSHAKE ) {
if ( (pSerialHead->RtsFlow) &&
(4*RxBytesAvail(pSerialHead) > (3*RxLength(pSerialHead))) ) {
DEBUGMSG (ZONE_READ|ZONE_FLOW,
(TEXT("IOCTL:RTS_CONTROL_HANDSHAKE Clearing RTS\r\n")));
pSerialHead->RtsFlow = 1;
pHWObj->pFuncTbl->HWClearRTS(pSerialHead->pHWHead);
} else {
DEBUGMSG (ZONE_READ|ZONE_FLOW,
(TEXT("IOCTL:RTS_CONTROL_HANDSHAKE Setting RTS\r\n")));
pSerialHead->RtsFlow = 0;
pHWObj->pFuncTbl->HWSetRTS(pSerialHead->pHWHead);
}
}
if ( pSerialHead->DCB.fOutX || pSerialHead->DCB.fInX ) {
pSerialHead->XFlow = 1;
} else {
pSerialHead->XFlow = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -