⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smsc100fd.c

📁 smsc公司的网络芯片smsc100fd的wince源码。
💻 C
📖 第 1 页 / 共 5 页
字号:
	// Wait for some time. Read back again.
	// for(glbl_tmp = 0; glbl_tmp < 1000000000; glbl_tmp ++);
	// SelectBank(BANK0);
	// SelectBank(BANK1);
	DEBUGMSG (ZONE_INIT, (TEXT ("SMSC100FD: CardSetup: CONFIG register re-reads back 0x%x\r\n"), ReadWord(pAdapter,CONFIG) ));

	// Initialize memory configuration register
	DEBUGMSG (ZONE_INIT, (TEXT ("SMSC100FD: CardSetup: Initializing Memory Configration register\r\n")));
	SelectBank (pAdapter,BANK0);
	WriteWord (pAdapter,MCR, 4);	// Reserve 4x512 Bytes for TX


	//
	// Set Loopback Mode
	//
	DEBUGMSG (ZONE_INIT, (TEXT ("SMSC100FD: CardSetup: Initializing loopback mode\r\n")));
	SelectBank (pAdapter,BANK0);
	pAdapter->usNicTransmitConfig = TCR_LOOP;
	WriteWord (pAdapter,TCR, pAdapter->usNicTransmitConfig);

	//
	// Clear all pending interrupts
	//
	DEBUGMSG (ZONE_INIT, (TEXT ("SMSC100FD: CardSetup: Clearing pending interrupts\r\n")));
	SelectBank (pAdapter,BANK2);
	WriteWord (pAdapter,INTERRUPT_REGISTER, ReadWord (pAdapter,INTERRUPT_REGISTER));
	
	//
	// Set Station Address
	//
	DEBUGMSG (ZONE_INIT, (TEXT ("SMSC100FD: CardSetup: Initializing station address registers\r\n")));
	SelectBank (pAdapter,BANK1);
	WriteWord (pAdapter,ADDR0, (UINT16)(pAdapter->ucStationAddress[0] | (pAdapter->ucStationAddress[1] << 8)));
	WriteWord (pAdapter,ADDR1, (UINT16)(pAdapter->ucStationAddress[2] | (pAdapter->ucStationAddress[3] << 8)));
	WriteWord (pAdapter,ADDR2, (UINT16)(pAdapter->ucStationAddress[4] | (pAdapter->ucStationAddress[5] << 8)));


	//
	// Write out the multicast addresses
	//
	SelectBank (pAdapter,BANK3);
	for (uiIndex = 0; uiIndex < sizeof (pAdapter->ucNicMulticastRegs); uiIndex += 2)
		WriteWord (pAdapter,MULTICAST1 + uiIndex, (USHORT)((pAdapter->ulPacketFilter & NDIS_PACKET_TYPE_ALL_MULTICAST) ? 0xffff : pAdapter->ucNicMulticastRegs[uiIndex] | pAdapter->ucNicMulticastRegs[uiIndex + 1] << 8));

	//
	// Reset the PHY.
	//
    for (uiDelay = 0; uiDelay < 10; uiDelay++){ //Can not take out this loop. It fix the following reset problem.
		usPhy = Smsc100FdReadPhy (pAdapter, PHY_CONTROL);
    	DEBUGMSG (ZONE_INIT, (TEXT ("SMSC100FD: CardSetup: PHY_CONTOL register is 0x%x\r\n"),usPhy));
		if (!(usPhy & PHY_CTRL_RESET))
			break;
	}
    DEBUGMSG (ZONE_INIT, (TEXT ("SMSC100FD: CardSetup: Resetting PHY\r\n")));
	Smsc100FdWritePhy (pAdapter, PHY_CONTROL, PHY_CTRL_RESET);
	for (uiDelay = 0; uiDelay < 100; uiDelay++)
		{
		usPhy = Smsc100FdReadPhy (pAdapter, PHY_CONTROL);
		if (!(usPhy & PHY_CTRL_RESET))
			break;
		Sleep (10);
		}

	DEBUGMSG (ZONE_INIT, (TEXT ("SMSC100FD: CardSetup: Done Resetting PHY, usPhy = 0x%x\r\n"), usPhy));
	if (usPhy & PHY_CTRL_RESET)
		return (FALSE);
	//
	// Wait for the autonegotiation to complete
	//
	for (uiDelay = 0; uiDelay < 10; uiDelay++)
		{
		usPhy = Smsc100FdReadPhy (pAdapter, PHY_STATUS);
		if (usPhy & PHY_STAT_ANCOMPLETE)
			break;
		Sleep (10);
		}


	//
	// Force the PHY into restricted modes (for debugging)
	//
#if 0
	Smsc100FdWritePhy (pAdapter, PHY_ANADVERTISE, Smsc100FdReadPhy (pAdapter, PHY_ANADVERTISE) & ~(PHY_ANA_100FDX | PHY_ANA_10FDX));	// Disable full duplex
//	Smsc100FdWritePhy (pAdapter, PHY_ANADVERTISE, Smsc100FdReadPhy (pAdapter, PHY_ANADVERTISE) & ~(PHY_ANA_100FDX | PHY_ANA_100HDX));	// Disable 100Mb/s
	//
	// Restart the autonegotiation
	//
	Smsc100FdWritePhy (pAdapter, PHY_CONTROL, Smsc100FdReadPhy (pAdapter, PHY_CONTROL) | PHY_CTRL_ANRESTART);
	//
	// Wait for the autonegotiation to complete
	//
	for (uiDelay = 0; uiDelay < 10; uiDelay++)
		{
		usPhy = Smsc100FdReadPhy (pAdapter, PHY_STATUS);
		if (usPhy & PHY_STAT_ANCOMPLETE)
			break;
		Sleep (10);
		}
#endif

	//
	// Reset the Isolation bit in the PHY, and adjust the TCR based on the PHY autonegotiation.
	//
	usPhy = Smsc100FdReadPhy (pAdapter, PHY_CONTROL);
	Smsc100FdWritePhy (pAdapter, PHY_CONTROL, (UINT16)(usPhy & ~PHY_CTRL_ISOLATION));
	pAdapter->usNicTransmitConfig = TCR_PAD_ENABLE | TCR_ENABLE;
	if (usPhy & PHY_CTRL_DUPLEX)
		pAdapter->usNicTransmitConfig |= TCR_SWFDUP;
	SelectBank (pAdapter,BANK0);
	WriteWord (pAdapter,TCR, pAdapter->usNicTransmitConfig);


	//
	// Set Interrupt Mask Register
	//
	DEBUGMSG (ZONE_INIT, (TEXT ("SMSC100FD: CardSetup: Initializing Interrupt Mask register %02x\r\n"), pAdapter->usNicInterruptMask));
	SelectBank (pAdapter,BANK2);
	WriteWord (pAdapter,INTERRUPT_REGISTER, pAdapter->usNicInterruptMask);
	
	DEBUGMSG(ZONE_FUNCTION, (TEXT("-SMSC100FD: CardSetup\r\n")));
    //
    // NOTE: Still in loopback mode.
    //
    return (TRUE);
	}

/*
 * Description:
 *	Starts the SMSC91C100FD.
 *
 * Arguments:
 *	pAdapter - Pointer to the adapter structure.
 *
 * Return Value:
 *	None.
 */
#pragma NDIS_PAGEABLE_FUNCTION(CardStart)
VOID
CardStart (IN PSMSC100FD_ADAPTER pAdapter)
{
	DEBUGMSG (ZONE_FUNCTION, (TEXT ("+SMSC100FD: CardStart\r\n")));
	// Initialize the trasmit control register
	SelectBank (pAdapter,BANK0);
	WriteWord (pAdapter,TCR, pAdapter->usNicTransmitConfig);
	//
	// Initialize the receive control register
	//
	SelectBank (pAdapter,BANK0);
	WriteWord (pAdapter,RCR, pAdapter->usNicReceiveConfig);
	DEBUGMSG (ZONE_FUNCTION, (TEXT ("-SMSC100FD: CardStart\r\n")));
}

/*
 * Description:
 *	Erases and refills the card multicast registers. Used when
 *	an address has been deleted and all bits must be recomputed.
 *
 * Arguments:
 *	pAdapter - Pointer to the adapter structure.
 *
 * Return Value:
 *	None.
 */
VOID
CardFillMulticastRegs (IN PSMSC100FD_ADAPTER pAdapter)
	{
    UINT uiIndex;
    UCHAR ucByte, ucBit;

	DEBUGMSG (ZONE_FUNCTION, (TEXT ("+SMSC100FD: CardFillMulticastRegs\r\n")));
    //
    // First turn all bits off.
    //
    for (uiIndex = 0; uiIndex < sizeof (pAdapter->ucNicMulticastRegs); uiIndex++)
		pAdapter->ucNicMulticastRegs[uiIndex] = 0;
    //
    // Now turn on the bit for each address in the multicast list.
    //
    for (uiIndex = 0; uiIndex < DEFAULT_MULTICASTLISTMAX; uiIndex++)
		{
        CardGetMulticastBit (pAdapter->ucAddresses[uiIndex], &ucByte, &ucBit);
        pAdapter->ucNicMulticastRegs[ucByte] |= ucBit;
		}
	DEBUGMSG (ZONE_FUNCTION, (TEXT ("-SMSC100FD: CardFillMulticastRegs\r\n")));
	}

/*
 * Description:
 *	Turns on all the bits in the multicast registers. Used when
 *	the card must receive all multicast packets.
 *
 * Arguments:
 *	pSynchronizeContext - Pointer to the adapter block
 *
 * Return Value:
 *	None.
 */
BOOLEAN
SyncCardSetAllMulticast (IN PVOID pSynchronizeContext)
	{
    UINT uiIndex;
    PSMSC100FD_ADAPTER pAdapter = ((PSMSC100FD_ADAPTER)pSynchronizeContext);

	DEBUGMSG (ZONE_FUNCTION, (TEXT ("+SMSC100FD: SyncCardSetAllMulticast\r\n")));
	SelectBank (pAdapter,BANK3);
	for (uiIndex = 0; uiIndex < sizeof (pAdapter->ucNicMulticastRegs); uiIndex += 2)
		WriteWord (pAdapter,MULTICAST1 + uiIndex, 0xffff);
	DEBUGMSG (ZONE_FUNCTION, (TEXT ("-SMSC100FD: CardSetAllMulticast\r\n")));
    return (FALSE);
	}

/*
 * Description:
 *	Sets the eight bytes in the card multicast registers.
 *
 * Arguments:
 *	pSynchronizeContext - Pointer to the adapter block
 *
 * Return Value:
 *	None.
 */
BOOLEAN
SyncCardCopyMulticastRegs (IN PVOID pSynchronizeContext)
	{
	UINT uiIndex;
    PSMSC100FD_ADAPTER pAdapter = ((PSMSC100FD_ADAPTER)pSynchronizeContext);

	DEBUGMSG (ZONE_FUNCTION, (TEXT ("+SMSC100FD: SyncCardCopyMulticastRegs\r\n")));
	SelectBank (pAdapter,BANK3);
	for (uiIndex = 0; uiIndex < sizeof (pAdapter->ucNicMulticastRegs); uiIndex += 2)
		WriteWord (pAdapter,MULTICAST1 + uiIndex, (UINT16)(pAdapter->ucNicMulticastRegs[uiIndex] | (pAdapter->ucNicMulticastRegs[uiIndex + 1] << 8)));
	DEBUGMSG (ZONE_FUNCTION, (TEXT("Multicast bits\r\n")));
	for (uiIndex = 0; uiIndex < sizeof (pAdapter->ucNicMulticastRegs); uiIndex++)
		DEBUGMSG (ZONE_FUNCTION, (TEXT ("%02x "), pAdapter->ucNicMulticastRegs[uiIndex]));
	DEBUGMSG (ZONE_FUNCTION, (TEXT ("\r\n")));

	DEBUGMSG (ZONE_FUNCTION, (TEXT ("-SMSC100FD: SyncCardCopyMulticastRegs\r\n")));
	return (FALSE);
	}

/*
 * Description:
 *	Sets the value of the "receive configuration" NIC register to
 *	the value of Adapter->NicReceiveConfig.
 *
 * Arguments:
 *	pSynchronizeContext - Pointer to the adapter block
 *
 * Return Value:
 *	None.
 */
BOOLEAN
SyncCardSetReceiveConfig (IN PVOID pSynchronizeContext)
	{
    PSMSC100FD_ADAPTER pAdapter = ((PSMSC100FD_ADAPTER)pSynchronizeContext);

	DEBUGMSG (ZONE_FUNCTION, (TEXT ("+SMSC100FD: SyncCardSetReceiveConfig\r\n")));
	SelectBank (pAdapter,BANK0);
	WriteWord (pAdapter,RCR, pAdapter->usNicReceiveConfig);
	DEBUGMSG (ZONE_FUNCTION, (TEXT ("-SMSC100FD: SyncCardSetReceiveConfig\r\n")));
    return (FALSE);
	}

/*
 * Description:
 *	Runs the AUTODIN II CRC algorithm on buffer Buffer of length Length.
 *
 * Arguments:
 *	pBuffer - The input buffer
 *	uiLength - The length of the input
 *
 * Return Value:
 *	The 32-bit CRC value.
 */
ULONG
CardComputeCrc (IN PUCHAR pBuffer, IN UINT uiLength)
	{
	ULONG ulCrc, ulCarry;
	UINT uiByteIndex, uiBitIndex;
	UCHAR ucCurByte;

	DEBUGMSG (ZONE_FUNCTION, (TEXT ("+SMSC100FD: CardComputeCrc\r\n")));
	ulCrc = 0xffffffff;
	for (uiByteIndex = 0; uiByteIndex < uiLength; uiByteIndex++)
		{
		ucCurByte = pBuffer[uiByteIndex];
		for (uiBitIndex = 0; uiBitIndex < 8; uiBitIndex++)
			{
            ulCarry = ((ulCrc & 0x80000000) ? 1 : 0) ^ (ucCurByte & 0x01);
            ulCrc <<= 1;
            ucCurByte >>= 1;
            if (ulCarry)
                ulCrc = (ulCrc ^ 0x04c11db6) | ulCarry;
	        }
		}
	DEBUGMSG (ZONE_FUNCTION, (TEXT ("-SMSC100FD: CardComputeCrc\r\n")));
    return (ulCrc);
	}

/*
 * Description:
 *	For a given multicast address, returns the byte and bit in
 *	the card multicast registers that it hashes to. Calls
 *	CardComputeCrc() to determine the CRC value.
 *
 * Arguments:
 *	ucAddress - The MAC address to hash.
 *	pByte - Pointer to the byte offset in the multicast registers.
 *	pValue - Pointer to the value for the multicast register. Will have a 1 in the relevant bit.
 *
 * Return Value:
 *	None.
 */
VOID
CardGetMulticastBit(IN UCHAR ucAddress[ETHER_LENGTH_OF_ADDRESS], OUT PUCHAR pByte, OUT PUCHAR pValue)
	{
    ULONG ulCrc;
    UINT uiBitNumber;

	DEBUGMSG (ZONE_FUNCTION, (TEXT ("+SMSC100FD: CardGetMulticastBit\r\n")));
    //
    // First compute the CRC.
    //
    ulCrc = CardComputeCrc (ucAddress, ETHER_LENGTH_OF_ADDRESS);
    //
    // The bit number is now in the 6 most significant bits of CRC.
    //
    uiBitNumber = (UINT)((ulCrc >> 26) & 0x3f);
    *pByte = (UCHAR)(uiBitNumber / 8);
    *pValue = (UCHAR)((UCHAR)1 << (uiBitNumber % 8));
	DEBUGMSG (ZONE_FUNCTION, (TEXT ("SMSC100FD: CardGetMulticastBit\r\n")));
	}

/*
 * Description:
 *	This is the interrupt handler which is registered with the operating
 *	system. If several interrupts are pending (i.e. transmit complete and receive),
 *	handle them all. Block new interrupts until all pending interrupts
 *	are handled.
 *
 * Arguments:
 *	pbInterruptRecognized - Pointer to a boolean value which returns TRUE if the
 *							ISR recognizes the interrupt as coming from this adapter.
 *	pbQueueDpc - TRUE if a DPC should be queued.
 *	pContext - Pointer to the adapter object
 *
 * Return Value:
 *	None.
 */
VOID
Smsc100FdIsr (OUT PBOOLEAN pbInterruptRecognized, OUT PBOOLEAN pbQueueDpc, IN PVOID pContext)
	{
    PSMSC100FD_ADAPTER pAdapter = ((PSMSC100FD_ADAPTER)pContext);

    DEBUGMSG (ZONE_INTR, (TEXT ("+SMSC100FD: ISR Entry\r\n")));
    //
    // Force the INT signal from the chip low. When all
    // interrupts are acknowledged interrupts will be unblocked,
    //
    CardBlockInterrupts (pAdapter);
    *pbInterruptRecognized = TRUE;
    *pbQueueDpc = TRUE;
    DEBUGMSG (ZONE_INTR, (TEXT ("-SMSC100FD: ISR Exit\r\n")));
	}

static void
DumpEtherFrame (BYTE *pFrame, WORD cwFrameLength)
	{
	int i;

	DEBUGMSG (ZONE_INTR, (TEXT ("Frame Buffer Address: 0x%X\r\n"), pFrame));
	DEBUGMSG (ZONE_INTR, (TEXT ("To: %02x:%02x:%02x:%02x:%02x:%02x  From: %02x:%02x:%02x:%02x:%02x:%02x  Type: 0x%04x  Length: %d\r\n"), pFrame[0], pFrame[1], pFrame[2], pFrame[3], pFrame[4], pFrame[5], pFrame[6], pFrame[7], pFrame[8], pFrame[9], pFrame[10], pFrame[11], *((UINT16 *)(pFrame + 12)), cwFrameLength));
	for (i = 0; i < cwFrameLength / 16; i++)
		DEBUGMSG (ZONE_INTR, (TEXT (" %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\r\n"), pFrame[i*16 + 0], pFrame[i*16 + 1], pFrame[i*16 + 2], pFrame[i*16 + 3], pFrame[i*16 + 4], pFrame[i*16 + 5], pFrame[i*16 + 6], pFrame[i*16 + 7], pFrame[i*16 + 8], pFrame[i*16 + 9], pFrame[i*16 + 10], pFrame[i*16 + 11], pFrame[i*16 + 12], pFrame[i*16 + 13], pFrame[i*16 + 14], pFrame[i*16 + 15]));
	switch (cwFrameLength % 16)
		{
		case 0:
			break;
		case 1:
			DEBUGMSG (ZONE_INTR, (TEXT (" %02x\r\n"), pFrame[i*16 + 0], pFrame[i*16 + 1], pFrame[i*16 + 2], pFrame[i*16 + 3], pFrame[i*16 + 4], pFrame[i*16 + 5], pFrame[i*16 + 6], pFrame[i*16 + 7], pFrame[i*16 + 8], pFrame[i*16 + 9], pFrame[i*16 + 10], pFrame[i*16 + 11], pFrame[i*16 + 12], pFrame[i*16 + 13], pFrame[i*16 + 14], pFrame[i*16 + 15]));
			break;
		case 2:
			DEBUGMSG (ZONE_INTR, (TEXT (" %02x %02x\r\n"), pFrame[i*16 + 0], pFrame[i*16 + 1], pFrame[i*16 + 2], pFrame[i*16 + 3], pFrame[i*16 + 4], pFrame[i*16 + 5], pFrame[i*16 + 6], pFrame[i*16 + 7], pFrame[i*16 + 8], pFrame[i*16 + 9], pFrame[i*16 + 10], pFrame[i*16 + 11], pFrame[i*16 + 12], pFrame[i*16 + 13], pFrame[i*16 + 14], pFrame[i*16 + 15]));
			break;
		case 3:
			DEBUGMSG (ZONE_INTR, (TEXT (" %02x %02x %02x\r\n"), pFrame[i*16 + 0], pFrame[i*16 + 1], pFrame[i*16 + 2], pFrame[i*16 + 3], pFrame[i*16 + 4], pFrame[i*16 + 5], pFrame[i*16 + 6], pFrame[i*16 + 7], pFrame[i*16 + 8], pFrame[i*16 + 9], pFrame[i*16 + 10], pFrame[i*16 + 11], pFrame[i*16 + 12], pFrame[i*16 + 13], pFrame[i*16 + 14], pFrame[i*16 + 15]));
			break;
		case 4:
			DEBUGMSG (ZONE_INTR, (TEXT (" %02x %02x %02x %02x\r\n"), pFrame[i*16 + 0], pFrame[i*16 + 1], pFrame[i*16 + 2], p

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -