📄 resource.cpp
字号:
// Function: NewDevice
//
// This function allocates and initializes the device resources.
//
// Parameters:
// None.
//
// Returns:
// This function returns the pointer to the created Fir Device.
//
//-----------------------------------------------------------------------------
pFirDevice_t NewDevice( void )
{
pFirDevice_t newdev;
newdev = (pFirDevice_t)MyMemAlloc(sizeof(FirDevice_t));
if (newdev)
InitDevice(newdev);
return newdev;
}
//-----------------------------------------------------------------------------
//
// Function: FreeDevice
//
// This function frees the device resources.
//
// Parameters:
// dev
// [in] .
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
VOID FreeDevice( pFirDevice_t dev )
{
CloseDevice(dev);
NdisReleaseSpinLock(&dev->Lock);
NdisFreeSpinLock(&dev->Lock);
MyMemFree((PVOID)dev, sizeof(FirDevice_t));
}
//-----------------------------------------------------------------------------
//
// Function: OpenDevice
//
// This function opens the Fir device.
//
// Parameters:
// thisDev
// [in] .
//
// Returns:
// This function returns status of open device.
//
//-----------------------------------------------------------------------------
BOOLEAN OpenDevice( pFirDevice_t thisDev )
{
NDIS_STATUS stat;
BOOLEAN result = FALSE;
UINT bufIndex;
PLIST_ENTRY pListEntry;
DEBUGMSG(ZONE_OPEN, (TEXT("Resource: +OpenDevice()\r\n")));
if (!thisDev)
{
return FALSE;
}
thisDev->SirPhyAddr = CSP_BASE_REG_PA_UART2;
if(!SirInitialize(thisDev))
{
goto done;
}
thisDev->FirPhyAddr = CSP_BASE_REG_PA_FIRI;
if(!FirInitialize(thisDev))
{
goto done;
}
// ---------------------------------------------------------------
// Initialize memory resouces for NDIS of various buffers
// ---------------------------------------------------------------
// Allocate the NDIS packet and NDIS buffer pools
// for this device's RECEIVE buffer queue.
// Our receive packets must only contain one buffer apiece,
// so #buffers == #packets.
NdisAllocatePacketPool(&stat, &thisDev->packetPoolHandle, NUM_RCV_BUFS, 16);
if (stat != NDIS_STATUS_SUCCESS)
{
goto done;
}
NdisAllocateBufferPool(&stat, &thisDev->bufferPoolHandle, NUM_RCV_BUFS);
if (stat != NDIS_STATUS_SUCCESS)
{
goto done;
}
// Initialize each of the RECEIVE packet objects for this device.
for (bufIndex = 0; bufIndex < NUM_RCV_BUFS; bufIndex++)
{
PVOID buf;
rcvBuffer *rcvBuf = (pRcvBuffer_t)MyMemAlloc(sizeof(rcvBuffer));
if (!rcvBuf)
{
goto done;
}
rcvBuf->state = STATE_FREE;
rcvBuf->dataBuf = NULL;
// Allocate the NDIS_PACKET.
NdisAllocatePacket(&stat, &rcvBuf->packet, thisDev->packetPoolHandle);
if (stat != NDIS_STATUS_SUCCESS)
{
goto done;
}
// For future convenience, set the MiniportReserved portion of the packet
// to the index of the rcv buffer that contains it.
// This will be used in ReturnPacketHandler.
*(ULONG *)rcvBuf->packet->MiniportReserved = (ULONG)rcvBuf;
rcvBuf->dataLen = 0;
InsertHeadList(&thisDev->rcvBufFree, &rcvBuf->listEntry);
buf = MyMemAlloc(RCV_BUFFER_SIZE);
if(!buf)
{
goto done;
}
InsertHeadList(&thisDev->rcvBufBuf, (PLIST_ENTRY)buf);
}
thisDev->writeBuf = (PUCHAR)MyMemAlloc(MAX_IRDA_DATA_SIZE);
if (!thisDev->writeBuf)
{
goto done;
}
pListEntry = MyRemoveHeadList(&thisDev->rcvBufBuf);
if(pListEntry)
thisDev->readBuf = (PUCHAR) LIST_ENTRY_TO_RCV_BUF(pListEntry);
else
goto done;
// Set mediaBusy to TRUE initially. That way, we won't
// IndicateStatus to the protocol in the ISR unless the
// protocol has expressed interest by clearing this flag
// via MiniportSetInformation(OID_IRDA_MEDIA_BUSY).
thisDev->mediaBusy = FALSE;
// Set default speed as 9600
thisDev->newSpeed = DEFAULT_BAUD_RATE;
thisDev->lastPacketAtOldSpeed = NULL;
thisDev->setSpeedAfterCurrentSendPacket = FALSE;
result = TRUE;
done:
if (!result)
{
// If we're failing, close the device to free up any resources
// that were allocated for it.
CloseDevice(thisDev);
DEBUGMSG(ZONE_ERROR, (TEXT("Resource: OpenDevice() failed\r\n")));
}
else
{
DEBUGMSG(ZONE_OPEN, (TEXT("Resource: OpenDevice() succeeded\r\n")));
}
DEBUGMSG(ZONE_OPEN, (TEXT("Resource: -OpenDevice\r\n")));
return result;
}
//-----------------------------------------------------------------------------
//
// Function: CloseDevice
//
// This function closes the Fir device.
//
// Parameters:
// thisDev
// [in] .
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
VOID CloseDevice( pFirDevice_t thisDev )
{
PLIST_ENTRY ListEntry;
DEBUGMSG(ZONE_CLOSE, (TEXT("Resource: +CloseDevice()\r\n")));
if (!thisDev)
return;
// ---------------------------------------------------------------
// Free memory resouces for NDIS of various buffers
// ---------------------------------------------------------------
// Free all resources for the RECEIVE buffer queue.
while (!IsListEmpty(&thisDev->rcvBufFree))
{
pRcvBuffer_t rcvBuf;
ListEntry = RemoveHeadList(&thisDev->rcvBufFree);
rcvBuf = CONTAINING_RECORD(ListEntry,rcvBuffer,listEntry);
if (rcvBuf->packet)
{
NdisFreePacket(rcvBuf->packet);
rcvBuf->packet = NULL;
}
MyMemFree(rcvBuf, sizeof(rcvBuffer));
}
while (!IsListEmpty(&thisDev->rcvBufBuf))
{
ListEntry = RemoveHeadList(&thisDev->rcvBufBuf);
MyMemFree(ListEntry, RCV_BUFFER_SIZE);
}
// Free the packet and buffer pool handles for this device.
if (thisDev->packetPoolHandle)
{
NdisFreePacketPool(thisDev->packetPoolHandle);
thisDev->packetPoolHandle = NULL;
}
if (thisDev->bufferPoolHandle)
{
NdisFreeBufferPool(thisDev->bufferPoolHandle);
thisDev->bufferPoolHandle = NULL;
}
// Free all resources for the SEND buffer queue.
while (ListEntry = MyRemoveHeadList(&thisDev->SendQueue))
{
PNDIS_PACKET Packet = CONTAINING_RECORD(ListEntry, NDIS_PACKET, MiniportReserved);
NdisReleaseSpinLock(&thisDev->Lock);
NdisMSendComplete(thisDev->ndisAdapterHandle, Packet, NDIS_STATUS_FAILURE);
NdisAcquireSpinLock(&thisDev->Lock);
}
thisDev->mediaBusy = FALSE;
thisDev->newSpeed = 0;
thisDev->linkSpeedInfo = NULL;
thisDev->lastPacketAtOldSpeed = NULL;
thisDev->setSpeedAfterCurrentSendPacket = FALSE;
//-----------------------------------------------------------------
// Free hardware memory
//-----------------------------------------------------------------
MyMemFree(thisDev->writeBuf, MAX_IRDA_DATA_SIZE * 8);
thisDev->readBuf = NULL;
thisDev->writeBuf = NULL;
FirDeinitialize(thisDev);
SirDeInitialize(thisDev);
}
//-----------------------------------------------------------------------------
//
// Function: QueueReceivePacket
//
// This function queue the received packet.
//
// Parameters:
// thisDev
// [in] .
// data
// [in] .
// dataLen
// [in] .
//
// Returns:
// None.
//
//-----------------------------------------------------------------------------
VOID QueueReceivePacket(pFirDevice_t thisDev, PUCHAR data, UINT dataLen)
{
rcvBuffer *rcvBuf = NULL;
PLIST_ENTRY ListEntry;
// Note: We cannot use a spinlock to protect the rcv buffer structures
// in an ISR. This is ok, since we used a sync-with-isr function
// the the deferred callback routine to access the rcv buffers.
DEBUGMSG(ZONE_FUNCTION, (TEXT("Resource: +QueueReceivePacket(0x%x, 0x%lx, 0x%x)\r\n"),
(UINT) thisDev, data, dataLen));
if (IsListEmpty(&thisDev->rcvBufFree))
{
ListEntry = NULL;
}
else
{
ListEntry = MyRemoveHeadList(&thisDev->rcvBufFree);
}
if (ListEntry)
{
if (rcvBuf = CONTAINING_RECORD(ListEntry, rcvBuffer, listEntry))
{
rcvBuf->dataBuf = data;
rcvBuf->state = STATE_FULL;
rcvBuf->dataLen = dataLen;
InsertTailList(&thisDev->rcvBufFull, ListEntry);
}
}
DEBUGMSG(ZONE_FUNCTION, (TEXT("Resource: -QueueReceivePacket\r\n")));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -