📄 mdd.c
字号:
/*
@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_INIT|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,
pSerialHead->pHWObj->pFuncTbl->HWGetRxStart((PVOID)pSerialHead),
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 EXTERNAL
// @func HANDLE | COM_INIT | Serial device initialization.
//
// @parm ULONG | Identifier | Port identifier. The device loader
// passes in the registry key that contains information
// about the active device.
//
// @remark This routine is called at device load time in order
// to perform any initialization. Typically the init
// routine does as little as possible, postponing memory
// allocation and device power-on to Open time.
//
// @rdesc Returns a pointer to the serial head which is passed into
// the COM_OPEN and COM_DEINIT entry points as a device handle.
//
HANDLE
COM_Init(
ULONG Identifier
)
{
PVOID pHWHead = NULL;
PHW_INDEP_INFO pSerialHead = NULL;
ULONG HWBufferSize;
DWORD DevIndex;
PDEVICE_LIST pDL = NULL;
PHWOBJ pHWObj;
HKEY hKey;
ULONG kreserved = 0, kvaluetype;
ULONG datasize = sizeof(ULONG);
TCHAR devkeypath[256];
/*
* INTERNAL: this routine initializes the hardware abstraction interface
* via HWInit(). It allocates a data structure representing this
* instantiation of the device. It also creates an event and initializes
* a critical section for receiving as well as registering the logical
* interrupt dwIntID with NK via InterruptInitialize. This call
* requires that the hardware dependent portion export apis that return
* the physical address of the receive buffer and the size of that buffer.
* Finally, it creates a buffer to act as an intermediate
* buffer when receiving.
*/
DEBUGMSG (ZONE_INIT | ZONE_FUNCTION, (TEXT("+COM_Init\r\n")));
// Allocate our control structure.
pSerialHead = (PHW_INDEP_INFO)LocalAlloc(LPTR, sizeof(HW_INDEP_INFO));
// Check that LocalAlloc did stuff ok too.
if ( !pSerialHead ) {
DEBUGMSG(ZONE_INIT | ZONE_ERROR,
(TEXT("Error allocating memory for pSerialHead, COM_Init failed\n\r")));
return (NULL);
}
// Initially, open list is empty.
InitializeListHead( &pSerialHead->OpenList );
InitializeCriticalSection(&(pSerialHead->OpenListCS));
pSerialHead->pAccessOwner = NULL;
pSerialHead->fEventMask = 0;
// Init CommTimeouts.
pSerialHead->CommTimeouts.ReadIntervalTimeout = READ_TIMEOUT;
pSerialHead->CommTimeouts.ReadTotalTimeoutMultiplier =
READ_TIMEOUT_MULTIPLIER;
pSerialHead->CommTimeouts.ReadTotalTimeoutConstant =
READ_TIMEOUT_CONSTANT;
pSerialHead->CommTimeouts.WriteTotalTimeoutMultiplier= 0;
pSerialHead->CommTimeouts.WriteTotalTimeoutConstant = 0;
/* Create tx and rx events and stash in global struct field. Check return.
*/
pSerialHead->hSerialEvent = CreateEvent(0,FALSE,FALSE,NULL);
pSerialHead->hKillDispatchThread = CreateEvent(0, FALSE, FALSE, NULL);
pSerialHead->hTransmitEvent = CreateEvent(0, FALSE, FALSE, NULL);
pSerialHead->hReadEvent = CreateEvent(0, FALSE, FALSE, NULL);
if ( !pSerialHead->hSerialEvent || !pSerialHead->hKillDispatchThread ||
!pSerialHead->hTransmitEvent || !pSerialHead->hReadEvent ) {
DEBUGMSG(ZONE_ERROR | ZONE_INIT,
(TEXT("Error creating event, COM_Init failed\n\r")));
LocalFree(pSerialHead);
return (NULL);
}
/* Initialize the critical sections that will guard the parts of
* the receive and transmit buffers.
*/
InitializeCriticalSection(&(pSerialHead->ReceiveCritSec1));
InitializeCriticalSection(&(pSerialHead->TransmitCritSec1));
/* Initialize hardware dependent data.
*/
pDL = GetSerialObject();
if ( !pDL ) {
DEBUGMSG(ZONE_ERROR | ZONE_INIT,
(TEXT("Error in GetSerialObject, COM_Init failed\n\r")));
LocalFree(pSerialHead);
return (NULL);
}
DEBUGMSG (ZONE_INIT,(TEXT("%d serial objects detected\r\n"),
pDL->NumberOfDevices));
/* Want to use the Identifier to do RegOpenKey and RegQueryValue (?)
* to get the index to be used as an index into the DeviceArray.
* The HWObj will also have a flag denoting whether to start the
* listening thread or provide the callback.
*/
DEBUGMSG (ZONE_INIT,(TEXT("Try to open %s\r\n"), (LPCTSTR)Identifier));
if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)Identifier, 0,
KEY_ALL_ACCESS, &hKey) ) {
DEBUGMSG (ZONE_INIT | ZONE_ERROR,
(TEXT("Failed to open HKEY_LOCAL_MACHINE key, COM_Init failed\r\n")));
LocalFree(pSerialHead);
return (NULL);
}
datasize = sizeof(devkeypath);
datasize = RegQueryValueEx(hKey, DEVLOAD_DEVKEY_VALNAME,
NULL, &kvaluetype, (LPBYTE)devkeypath,
&datasize);
/* Close the active key
*/
RegCloseKey (hKey);
if ( datasize ) {
DEBUGMSG (ZONE_INIT | ZONE_ERROR,
(TEXT("Failed to get DEVLOAD_DEVKEY_VALNAME value, %d, COM_Init failed\r\n"), datasize));
LocalFree(pSerialHead);
return (NULL);
}
if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)devkeypath, 0,
KEY_ALL_ACCESS, &hKey) ) {
DEBUGMSG (ZONE_INIT | ZONE_ERROR,
(TEXT("Failed to open devkeypath, COM_Init failed\r\n")));
LocalFree(pSerialHead);
return (NULL);
}
datasize = sizeof(DWORD);
if ( RegQueryValueEx(hKey, L"DeviceArrayIndex", NULL, &kvaluetype,
(LPBYTE)&DevIndex, &datasize) ) {
DEBUGMSG (ZONE_INIT | ZONE_ERROR,
(TEXT("Failed to get DeviceArrayIndex value, COM_Init failed\r\n")));
RegCloseKey (hKey);
LocalFree(pSerialHead);
return (NULL);
}
datasize = sizeof(DWORD);
if ( RegQueryValueEx(hKey, L"Priority256", NULL, &kvaluetype,
(LPBYTE)&pSerialHead->Priority256, &datasize) ) {
pSerialHead->Priority256 = DEFAULT_CE_THREAD_PRIORITY;
DEBUGMSG (ZONE_INIT | ZONE_WARN,
(TEXT("Failed to get Priority256 value, defaulting to %d\r\n"), pSerialHead->Priority256));
}
// Close the device key
RegCloseKey (hKey);
DEBUGMSG (ZONE_INIT,
(TEXT("DevIndex %X\r\n"), DevIndex));
pHWObj = pDL->DeviceArray[DevIndex];
DEBUGMSG (ZONE_INIT, (TEXT("About to call HWInit(%s,0x%X)\r\n"),
Identifier, pSerialHead));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -