📄 openclos.c
字号:
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) 1995-1998 Microsoft Corporation
Module Name:
openclos.c
Abstract:
This file implements the IrDA Serial IR NDIS MAC device initialization
and cleanup functions. This is provided as a sample to platform writers
and is expected to be able to be used without modification on most (if not
all) hardware platforms.
Functions:
IrsirInitialize
IrsirHalt
IrsirReset
StartDevice
StopDevice
InitializeIrDevice
DeinitializeIrDevice
GetDeviceConfiguration
Notes:
IrsirInitialize will only allocate the device memory and get the
device configuration. DeviceStart will be called via an OID from the
protocol to start the send and recv threads and open the COM port.
--*/
#include "irsirp.h"
static NDIS_STATUS
InitializeIrDevice(
IN OUT PIR_DEVICE pIrDevice
);
static NDIS_STATUS
DeinitializeIrDevice(
IN OUT PIR_DEVICE pIrDevice
);
static NDIS_STATUS
GetDeviceConfiguration(
IN OUT PIR_DEVICE pIrDevice,
IN HANDLE hConfigCtxt
);
static DWORD
MergeSpeedMasks(
DWORD dwDongleSpeedMask,
DWORD dwRegSpeedMask,
DWORD dwUartSpeedMask
);
/*++
Function: IrsirInitialize
Description: Initializes the NIC and allocates all resources
required to carry out network operations.
Arguments:
OpenErrorStatus - Additional error code (NDIS_STATUS_Xxx) if returning
NDIS_STATUS_OPEN_FAILED.
piMedium - Indicates to NDIS which medium type the miniport is using.
rgMedium - Array of NdisMediumXxx the miniport can choose to use.
cMediums - The number of entries in the rgMedium array.
hNdisAdapter - Pointer to handle identifying NDIS' miniport context.
hConfiguration - This is an NDIS wrapper handle context used for
configuration.
Returns:
NDIS_STATUS_SUCCESS - IFF miniport is properly configured and all resources
allocated.
NDIS_STATUS_OPEN_FAILED, otherwise with extended error code in OpenErrorStatus
NDIS_STATUS_UNSUPPORTED_MEDIA - Driver doesn't support mediums.
NDIS_STATUS_ADAPTER_NOT_FOUND - NdisOpenConfiguration failure.
NDIS_STATUS_RESOURCES - Could not claim sufficient resources.
Comments:
--*/
NDIS_STATUS
IrsirInitialize(
OUT PNDIS_STATUS OpenErrorStatus,
OUT PUINT piMedium,
IN PNDIS_MEDIUM rgMediums,
IN UINT cMediums,
IN NDIS_HANDLE hNdisAdapter,
IN NDIS_HANDLE hConfiguration
)
{
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
PIR_DEVICE pIrDevice = NULL;
DWORD i;
DEBUGMSG(ZONE_INIT,
(TEXT("+IrsirInitialize(0x%.8X, 0x%.8X, 0x%.8X, %d, ")
TEXT("0x%.8X, 0x%.8X)\r\n"),
OpenErrorStatus,
piMedium,
rgMediums,
cMediums,
hNdisAdapter,
hConfiguration)
);
//
// Search for the IrDA medium in the array.
//
for (i = 0; i < cMediums; i++)
{
if (rgMediums[i] == NdisMediumIrda) { break; }
}
if (i < cMediums)
{
*piMedium = i;
}
else
{
// IrDA not found!
status = NDIS_STATUS_UNSUPPORTED_MEDIA;
goto done;
}
//
// Allocate and initialize our ir device.
//
pIrDevice = IrsirAlloc(MT_IR_DEVICE, sizeof(IR_DEVICE));
if (pIrDevice == NULL)
{
status = NDIS_STATUS_RESOURCES;
goto done;
}
// Set handle value to invalid. The rest of the block should be zeroed
// from our alloc.
pIrDevice->hSerial = INVALID_HANDLE_VALUE;
// Inits spinlock, defaults, and allocates packets/buffers.
status = InitializeIrDevice(
pIrDevice
);
if (status != NDIS_STATUS_SUCCESS)
{
goto done;
}
// Get device's configuration from registry. This includes port to use
// and the correct dongle interface.
status = GetDeviceConfiguration(
pIrDevice,
hConfiguration
);
if (status != NDIS_STATUS_SUCCESS)
{
goto done;
}
// Set NDIS wrapper context for this miniport.
pIrDevice->hNdisAdapter = hNdisAdapter;
//
// NdisMSetAttributes will associate our adapter handle (pIrDevice)
// with the wrapper's adapter handle. The wrapper will then always use
// our adapter handle when calling us.
//
NdisMSetAttributes(
hNdisAdapter,
(NDIS_HANDLE)pIrDevice,
FALSE, // TRUE if busmaster DMA.
NdisInterfaceInternal // I/O Bus interface type.
);
done:
if (status != NDIS_STATUS_SUCCESS)
{
if (pIrDevice)
{
DeinitializeIrDevice(pIrDevice);
IrsirFree(pIrDevice);
}
*OpenErrorStatus = status;
status = NDIS_STATUS_OPEN_FAILED;
}
DEBUGMSG(ZONE_INIT, (TEXT("-IrsirInitialize [0x%.8X]\r\n"), status));
return (status);
}
/*++
Function: IrsirHalt
Description: Deallocates resources and halts the device. Does opposite
of IrsirInitialize.
Arguments:
hContext - IR device miniport context.
Returns:
None.
Comments:
--*/
VOID
IrsirHalt(
IN NDIS_HANDLE hContext
)
{
PIR_DEVICE pIrDevice = (PIR_DEVICE)hContext;
ASSERT(pIrDevice != NULL);
ASSERT(pIrDevice->hSerial != INVALID_HANDLE_VALUE);
DEBUGMSG(ZONE_INIT, (TEXT("+IrsirHalt(0x%.8X)\r\n"), pIrDevice));
StopDevice(pIrDevice);
DeinitializeIrDevice(pIrDevice);
IrsirFree(pIrDevice);
DEBUGMSG(ZONE_INIT, (TEXT("-IrsirHalt\r\n")));
return;
}
/*++
Function: IrsirReset
Description: Resets the miniports software/hardware state.
Arguments:
lpfAddressingReset - Points to a variable that MiniportReset sets to
TRUE if the NDIS library should call
IrsirSetInformation to restore addressing
information to the current values.
hContext - Pointer to IR device context.
Returns:
NDIS_STATUS Code.
Success - NDIS_STATUS_SUCCESS.
Failure - NDIS_STATUS_Xxx
Comments:
--*/
NDIS_STATUS
IrsirReset(
OUT PBOOLEAN lpfAddressingReset,
IN NDIS_HANDLE hContext
)
{
PIR_DEVICE pIrDevice = (PIR_DEVICE)hContext;
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
BOOL fRestartDevice = FALSE;
DEBUGMSG(ZONE_INIT,
(TEXT("+IrsirReset(0x%.8X, 0x%.8X)\r\n"),
lpfAddressingReset, hContext)
);
*lpfAddressingReset = FALSE;
// Shutdown device.
if (pIrDevice->hSerial != INVALID_HANDLE_VALUE)
{
fRestartDevice = TRUE;
pIrDevice->IDongle.Deinitialize(pIrDevice->hSerial);
StopDevice(pIrDevice);
}
DeinitializeIrDevice(pIrDevice);
// Restart device.
status = InitializeIrDevice(pIrDevice);
if (status != NDIS_STATUS_SUCCESS)
{
goto done;
}
if (fRestartDevice == TRUE)
{
status = StartDevice(pIrDevice);
if (status != NDIS_STATUS_SUCCESS)
{
DeinitializeIrDevice(pIrDevice);
}
}
done:
DEBUGMSG(ZONE_INIT, (TEXT("-IrsirReset [0x%.8X]\r\n"), status));
return (status);
}
/*++
Function: InitializeIrDevice
Description: Initializes the IR_DEVICE. Inits defaults, gets registry
config, and setting up dongle iface.
Arguments:
pIrDevice - Pointer to the IR devices object.
Returns:
NDIS_STATUS
Comments:
ASSUMPTIONS: All invalid memory pointers etc. are NULL'd before calling
this function.
NOTE: We don't initialize the following IR device object values since
they are part of the configuration and these values will outlast
an IrsirReset:
dwPort
tTransceiver
fIntIR
IDongle
capsDongle
hSerial
hNdisAdapter
--*/
NDIS_STATUS
InitializeIrDevice(
IN OUT PIR_DEVICE pIrDevice
)
{
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
DWORD i;
DEBUGMSG(ZONE_INIT, (TEXT("+InitializeIrDevice(0x%.8X)\r\n"), pIrDevice));
ASSERT(pIrDevice != NULL);
NdisAllocateSpinLock(&pIrDevice->slDevice);
pIrDevice->pCurrBaud = &v_rgSupportedBaudRates[DEFAULT_BAUDRATE];
//
// Thread handles maintained for synchronization.
//
pIrDevice->hRxThread = NULL;
pIrDevice->hTxThread = NULL;
//
// Init stats.
//
pIrDevice->cPacketsRx = 0;
pIrDevice->cPacketsRxDropped = 0;
pIrDevice->cPacketsRxOverflow = 0;
pIrDevice->cPacketsTx = 0;
pIrDevice->cPacketsTxDropped = 0;
//
// Media busy.
//
pIrDevice->fMediaBusy = FALSE;
#ifdef DEBUG
pIrDevice->cMediaBusyRxErrs = 0;
pIrDevice->cMediaBusyRxBytes = 0;
#endif // DEBUG
//
// Send event and buffers.
//
// Auto-reset, init non-signalled event.
pIrDevice->hSendEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
pIrDevice->cbSendBuf = MAX_IRDA_DATA_SIZE;
pIrDevice->pSendHead = NULL;
pIrDevice->pSendTail = NULL;
pIrDevice->pSendActive = NULL;
pIrDevice->pSendBuf = IrsirAlloc(MT_IR_DEVICE_SEND,
MAX_IRDA_DATA_SIZE);
if (pIrDevice->pSendBuf == NULL)
{
status = NDIS_STATUS_RESOURCES;
goto done;
}
//
// TAT.
//
pIrDevice->fMinTATRequired = TRUE;
//
// Receive state, buffers, packets.
//
NdisInitializeListHead(&pIrDevice->RxBufFree);
NdisInitializeListHead(&pIrDevice->RxBufPending);
pIrDevice->RxState = RX_STATE_READY;
pIrDevice->cbRxNumBytes = 0;
pIrDevice->iRxCurrByte = 0;
pIrDevice->cbRxBuf = 256;
pIrDevice->lpRxBuf = IrsirAlloc(MT_IR_DEVICE_RECV, pIrDevice->cbRxBuf);
if (pIrDevice->lpRxBuf == NULL)
{
status = NDIS_STATUS_RESOURCES;
goto done;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -