rtmp_init.c

来自「D-link 无线usb网卡的Linux无线网卡驱动程序」· C语言 代码 · 共 1,957 行 · 第 1/5 页

C
1,957
字号
		PTX_CONTEXT pMLMEContext = &(pAd->MLMEContext[i]);
		
		if ( NULL != pMLMEContext->pUrb )
		{
			RTUSB_UNLINK_URB(pMLMEContext->pUrb);
			if (IsFree)
			usb_free_urb(pMLMEContext->pUrb);
			pMLMEContext->pUrb = NULL;
		}
		
		if ( NULL != pMLMEContext->TransferBuffer )
		{
			kfree( pMLMEContext->TransferBuffer);
			pMLMEContext->TransferBuffer = NULL;
		}
	}
	
	// Free Tx frame resource
	for (acidx = 0; acidx < 4; acidx++)
	{
		for ( i= 0; i < TX_RING_SIZE; i++ )
		{
			PTX_CONTEXT pTxContext = &(pAd->TxContext[acidx][i]);

			if ( NULL != pTxContext->pUrb )
			{
				RTUSB_UNLINK_URB(pTxContext->pUrb);
				if (IsFree)
				usb_free_urb(pTxContext->pUrb);
				pTxContext->pUrb = NULL;
			}
		
			if ( NULL != pTxContext->TransferBuffer )
			{
				kfree( pTxContext->TransferBuffer);
				pTxContext->TransferBuffer = NULL;
			}
		}
	}

	DBGPRINT(RT_DEBUG_TRACE, "<--- ReleaseAdapter\n");

}

/*
	========================================================================
	
	Routine Description:
		Allocate DMA memory blocks for send, receive

	Arguments:
		Adapter		Pointer to our adapter

	Return Value:
		NDIS_STATUS_SUCCESS
		NDIS_STATUS_FAILURE
		NDIS_STATUS_RESOURCES

	Note:
	
	========================================================================
*/
NDIS_STATUS	RTMPInitAdapterBlock(
	IN	PRTMP_ADAPTER	pAd)
{
	NDIS_STATUS		Status=NDIS_STATUS_SUCCESS;
	UINT			i;
	PCmdQElmt		cmdqelmt;

	DBGPRINT(RT_DEBUG_TRACE, "--> RTMPInitAdapterBlock\n");

	// init counter
	pAd->WlanCounters.TransmittedFragmentCount.vv.LowPart =  0;
	pAd->WlanCounters.MulticastTransmittedFrameCount.vv.LowPart =0;
	pAd->WlanCounters.FailedCount.vv.LowPart =0;
	pAd->WlanCounters.NoRetryCount.vv.LowPart =0;
	pAd->WlanCounters.RetryCount.vv.LowPart =0;
	pAd->WlanCounters.MultipleRetryCount.vv.LowPart =0;
	pAd->WlanCounters.RTSSuccessCount.vv.LowPart =0;
	pAd->WlanCounters.RTSFailureCount.vv.LowPart =0;
	pAd->WlanCounters.ACKFailureCount.vv.LowPart =0;
	pAd->WlanCounters.FrameDuplicateCount.vv.LowPart =0;
	pAd->WlanCounters.ReceivedFragmentCount.vv.LowPart =0;
	pAd->WlanCounters.MulticastReceivedFrameCount.vv.LowPart =0;
	pAd->WlanCounters.FCSErrorCount.vv.LowPart =0;

	pAd->WlanCounters.TransmittedFragmentCount.vv.HighPart =  0;
	pAd->WlanCounters.MulticastTransmittedFrameCount.vv.HighPart =0;
	pAd->WlanCounters.FailedCount.vv.HighPart =0;
	pAd->WlanCounters.NoRetryCount.vv.HighPart =0;
	pAd->WlanCounters.RetryCount.vv.HighPart =0;
	pAd->WlanCounters.MultipleRetryCount.vv.HighPart =0;
	pAd->WlanCounters.RTSSuccessCount.vv.HighPart =0;
	pAd->WlanCounters.RTSFailureCount.vv.HighPart =0;
	pAd->WlanCounters.ACKFailureCount.vv.HighPart =0;
	pAd->WlanCounters.FrameDuplicateCount.vv.HighPart =0;
	pAd->WlanCounters.ReceivedFragmentCount.vv.HighPart =0;
	pAd->WlanCounters.MulticastReceivedFrameCount.vv.HighPart =0;
	pAd->WlanCounters.FCSErrorCount.vv.HighPart =0;
	
	do
	{
		for (i = 0; i < COMMAND_QUEUE_SIZE; i++)
		{
			cmdqelmt = &(pAd->CmdQElements[i]);
			NdisZeroMemory(cmdqelmt, sizeof(CmdQElmt));
			cmdqelmt->buffer = NULL;
			cmdqelmt->CmdFromNdis = FALSE;
			cmdqelmt->InUse = FALSE;
		}
		RTUSBInitializeCmdQ(&pAd->CmdQ);

		init_MUTEX(&(pAd->usbdev_semaphore));
		init_MUTEX_LOCKED(&(pAd->mlme_semaphore));
		init_MUTEX_LOCKED(&(pAd->RTUSBCmd_semaphore));
		init_completion (&pAd->notify); 	// event initially non-signalled
		
		////////////////////////
		// Spinlock
		NdisAllocateSpinLock(&pAd->MLMEQLock);
		NdisAllocateSpinLock(&pAd->BulkOutLock[0]);
		NdisAllocateSpinLock(&pAd->BulkOutLock[1]);
		NdisAllocateSpinLock(&pAd->BulkOutLock[2]);
		NdisAllocateSpinLock(&pAd->BulkOutLock[3]);
		NdisAllocateSpinLock(&pAd->CmdQLock);
		NdisAllocateSpinLock(&pAd->SendTxWaitQueueLock[0]);
		NdisAllocateSpinLock(&pAd->SendTxWaitQueueLock[1]);
		NdisAllocateSpinLock(&pAd->SendTxWaitQueueLock[2]);
		NdisAllocateSpinLock(&pAd->SendTxWaitQueueLock[3]);
		NdisAllocateSpinLock(&pAd->DeQueueLock[0]);
		NdisAllocateSpinLock(&pAd->DeQueueLock[1]);
		NdisAllocateSpinLock(&pAd->DeQueueLock[2]);
		NdisAllocateSpinLock(&pAd->DeQueueLock[3]);
		NdisAllocateSpinLock(&pAd->DataQLock[0]);
		NdisAllocateSpinLock(&pAd->DataQLock[1]);
		NdisAllocateSpinLock(&pAd->DataQLock[2]);
		NdisAllocateSpinLock(&pAd->DataQLock[3]);
		
		NdisAllocateSpinLock(&pAd->MLMEWaitQueueLock);

	}	while (FALSE);

	DBGPRINT(RT_DEBUG_TRACE, "<-- RTMPInitAdapterBlock\n");
	
	return Status;
}

NDIS_STATUS	RTUSBWriteHWMACAddress(
	IN	PRTMP_ADAPTER		pAd)
{
	MAC_CSR2_STRUC		StaMacReg0;
	MAC_CSR3_STRUC		StaMacReg1;
	NDIS_STATUS			Status = NDIS_STATUS_SUCCESS;

	if (pAd->bLocalAdminMAC != TRUE)
	{
		pAd->CurrentAddress[0] = pAd->PermanentAddress[0];
		pAd->CurrentAddress[1] = pAd->PermanentAddress[1];
		pAd->CurrentAddress[2] = pAd->PermanentAddress[2];
		pAd->CurrentAddress[3] = pAd->PermanentAddress[3];
		pAd->CurrentAddress[4] = pAd->PermanentAddress[4];
		pAd->CurrentAddress[5] = pAd->PermanentAddress[5];
	}
	// Write New MAC address to MAC_CSR2 & MAC_CSR3 & let ASIC know our new MAC
	StaMacReg0.field.Byte0 = pAd->CurrentAddress[0];
	StaMacReg0.field.Byte1 = pAd->CurrentAddress[1];
	StaMacReg0.field.Byte2 = pAd->CurrentAddress[2];
	StaMacReg0.field.Byte3 = pAd->CurrentAddress[3];
	StaMacReg1.field.Byte4 = pAd->CurrentAddress[4];
	StaMacReg1.field.Byte5 = pAd->CurrentAddress[5];
	StaMacReg1.field.U2MeMask = 0xff;

	DBGPRINT_RAW(RT_DEBUG_TRACE, "Local MAC = %02x:%02x:%02x:%02x:%02x:%02x\n",
			pAd->CurrentAddress[0], pAd->CurrentAddress[1], pAd->CurrentAddress[2],
			pAd->CurrentAddress[3], pAd->CurrentAddress[4], pAd->CurrentAddress[5]);

	RTUSBWriteMACRegister(pAd, MAC_CSR2, StaMacReg0.word);
	RTUSBWriteMACRegister(pAd, MAC_CSR3, StaMacReg1.word);

	return Status;
}

/*
	========================================================================
	
	Routine Description:
		Read initial parameters from EEPROM
		
	Arguments:
		Adapter						Pointer to our adapter

	Return Value:
		None

	Note:
		
	========================================================================
*/
VOID NICReadEEPROMParameters(
	IN	PRTMP_ADAPTER	pAd)
{
	USHORT					i, value, value2;
	EEPROM_ANTENNA_STRUC	Antenna;
	EEPROM_VERSION_STRUC	Version;
	CHAR					ChannelTxPower[MAX_NUM_OF_CHANNELS];
	EEPROM_LED_STRUC		LedSetting;

	DBGPRINT(RT_DEBUG_TRACE, "--> NICReadEEPROMParameters\n");

	//Read MAC address.
	RTUSBReadEEPROM(pAd, EEPROM_MAC_ADDRESS_BASE_OFFSET, pAd->PermanentAddress, MAC_ADDR_LEN);
	DBGPRINT_RAW(RT_DEBUG_TRACE, "Local MAC = %02x:%02x:%02x:%02x:%02x:%02x\n",
			pAd->PermanentAddress[0], pAd->PermanentAddress[1], pAd->PermanentAddress[2],
			pAd->PermanentAddress[3], pAd->PermanentAddress[4], pAd->PermanentAddress[5]);

	// Init the channel number for TX channel power
	// 0. 11b/g
	for (i = 0; i < 14; i++)
		pAd->TxPower[i].Channel = i + 1;
	// 1. UNI 36 - 64
	for (i = 0; i < 8; i++)
		pAd->TxPower[i + 14].Channel = 36 + i * 4;
	// 2. HipperLAN 2 100 - 140
	for (i = 0; i < 11; i++)
		pAd->TxPower[i + 22].Channel = 100 + i * 4;
	// 3. UNI 140 - 165
	for (i = 0; i < 5; i++)
		pAd->TxPower[i + 33].Channel = 149 + i * 4; 	   

	// if E2PROM version mismatch with driver's expectation, then skip
	// all subsequent E2RPOM retieval and set a system error bit to notify GUI
	RTUSBReadEEPROM(pAd, EEPROM_VERSION_OFFSET, (PUCHAR)&Version.word, 2);
	pAd->EepromVersion = Version.field.Version + Version.field.FaeReleaseNumber * 256;
	DBGPRINT(RT_DEBUG_TRACE, "E2PROM: Version = %d, FAE release #%d\n", Version.field.Version, Version.field.FaeReleaseNumber);

	// Read BBP default value from EEPROM and store to array(EEPROMDefaultValue) in pAd
	RTUSBReadEEPROM(pAd, EEPROM_BBP_BASE_OFFSET, (PUCHAR)(pAd->EEPROMDefaultValue), 2 * NUM_EEPROM_BBP_PARMS);

	// We have to parse NIC configuration 0 at here.
	// If TSSI did not have preloaded value, it should reset the TxAutoAgc to false
	// Therefore, we have to read TxAutoAgc control beforehand.
	// Read Tx AGC control bit
	Antenna.word = pAd->EEPROMDefaultValue[0];
	if (Antenna.field.DynamicTxAgcControl == 1)
		pAd->bAutoTxAgcA = pAd->bAutoTxAgcG = TRUE;
	else
		pAd->bAutoTxAgcA = pAd->bAutoTxAgcG = FALSE;		
	
	//
	// Reset PhyMode if we don't support 802.11a
	//
	if ((pAd->PortCfg.PhyMode == PHY_11ABG_MIXED) || (pAd->PortCfg.PhyMode == PHY_11A))
	{
		//
		// Only RFIC_5226, RFIC_5225 suport 11a
		//
		if ((Antenna.field.RfIcType == RFIC_2528) || (Antenna.field.RfIcType == RFIC_2527))
			pAd->PortCfg.PhyMode = PHY_11BG_MIXED;
	}
	
	// Read Tx power value for all 14 channels
	// Value from 1 - 0x7f. Default value is 24.
	// 0. 11b/g
	// Power value 0xFA (-6) ~ 0x24 (36)
	RTUSBReadEEPROM(pAd, EEPROM_G_TX_PWR_OFFSET, ChannelTxPower, 2 * NUM_EEPROM_TX_G_PARMS);
	for (i = 0; i < 2 * NUM_EEPROM_TX_G_PARMS; i++)
	{
		if ((ChannelTxPower[i] > 36) || (ChannelTxPower[i] < -6))
			pAd->TxPower[i].Power = 24;			
		else
			pAd->TxPower[i].Power = ChannelTxPower[i];

		DBGPRINT_RAW(RT_DEBUG_INFO, "Tx power for channel %d : %0x\n", pAd->TxPower[i].Channel, pAd->TxPower[i].Power);
	}

	// 1. UNI 36 - 64, HipperLAN 2 100 - 140, UNI 140 - 165
	// Power value 0xFA (-6) ~ 0x24 (36)
	RTUSBReadEEPROM(pAd, EEPROM_A_TX_PWR_OFFSET, ChannelTxPower, MAX_NUM_OF_A_CHANNELS);
	for (i = 0; i < MAX_NUM_OF_A_CHANNELS; i++)
	{
		if ((ChannelTxPower[i] > 36) || (ChannelTxPower[i] < -6))
			pAd->TxPower[i + 14].Power = 24;
		else			
			pAd->TxPower[i + 14].Power = ChannelTxPower[i + 14];
		DBGPRINT_RAW(RT_DEBUG_INFO, "Tx power for channel %d : %0x\n", pAd->TxPower[i].Channel, pAd->TxPower[i].Power);
	}

	// Read TSSI reference and TSSI boundary for temperature compensation.
	// 0. 11b/g
	{
		RTUSBReadEEPROM(pAd, EEPROM_BG_TSSI_CALIBRAION, ChannelTxPower, 10);
		pAd->TssiMinusBoundaryG[4] = ChannelTxPower[0];
		pAd->TssiMinusBoundaryG[3] = ChannelTxPower[1];
		pAd->TssiMinusBoundaryG[2] = ChannelTxPower[2];
		pAd->TssiMinusBoundaryG[1] = ChannelTxPower[3];
		pAd->TssiPlusBoundaryG[1] = ChannelTxPower[4];
		pAd->TssiPlusBoundaryG[2] = ChannelTxPower[5];
		pAd->TssiPlusBoundaryG[3] = ChannelTxPower[6];
		pAd->TssiPlusBoundaryG[4] = ChannelTxPower[7];
		pAd->TssiRefG	= ChannelTxPower[8];
		pAd->TxAgcStepG = ChannelTxPower[9];  
		pAd->TxAgcCompensateG = 0;
		pAd->TssiMinusBoundaryG[0] = pAd->TssiRefG;
		pAd->TssiPlusBoundaryG[0]  = pAd->TssiRefG;

		// Disable TxAgc if the based value is not right
		if (pAd->TssiRefG == 0xff)
			pAd->bAutoTxAgcG = FALSE;

		DBGPRINT(RT_DEBUG_TRACE,"E2PROM: G Tssi[-4 .. +4] = %d %d %d %d - %d -%d %d %d %d, step=%d, tuning=%d\n",
			pAd->TssiMinusBoundaryG[4], pAd->TssiMinusBoundaryG[3], pAd->TssiMinusBoundaryG[2], pAd->TssiMinusBoundaryG[1],
			pAd->TssiRefG,
			pAd->TssiPlusBoundaryG[1], pAd->TssiPlusBoundaryG[2], pAd->TssiPlusBoundaryG[3], pAd->TssiPlusBoundaryG[4],
			pAd->TxAgcStepG, pAd->bAutoTxAgcG);
	}	
	// 1. 11a
	{
		RTUSBReadEEPROM(pAd, EEPROM_A_TSSI_CALIBRAION, ChannelTxPower, 10);
		pAd->TssiMinusBoundaryA[4] = ChannelTxPower[0];
		pAd->TssiMinusBoundaryA[3] = ChannelTxPower[1];
		pAd->TssiMinusBoundaryA[2] = ChannelTxPower[2];
		pAd->TssiMinusBoundaryA[1] = ChannelTxPower[3];
		pAd->TssiPlusBoundaryA[1] = ChannelTxPower[4];
		pAd->TssiPlusBoundaryA[2] = ChannelTxPower[5];
		pAd->TssiPlusBoundaryA[3] = ChannelTxPower[6];
		pAd->TssiPlusBoundaryA[4] = ChannelTxPower[7];
		pAd->TssiRefA	= ChannelTxPower[8];
		pAd->TxAgcStepA = ChannelTxPower[9]; 
		pAd->TxAgcCompensateA = 0;
		pAd->TssiMinusBoundaryA[0] = pAd->TssiRefA;
		pAd->TssiPlusBoundaryA[0]  = pAd->TssiRefA;

		// Disable TxAgc if the based value is not right
		if (pAd->TssiRefA == 0xff)
			pAd->bAutoTxAgcA = FALSE;

		DBGPRINT(RT_DEBUG_TRACE,"E2PROM: A Tssi[-4 .. +4] = %d %d %d %d - %d -%d %d %d %d, step=%d, tuning=%d\n",
			pAd->TssiMinusBoundaryA[4], pAd->TssiMinusBoundaryA[3], pAd->TssiMinusBoundaryA[2], pAd->TssiMinusBoundaryA[1],
			pAd->TssiRefA,
			pAd->TssiPlusBoundaryA[1], pAd->TssiPlusBoundaryA[2], pAd->TssiPlusBoundaryA[3], pAd->TssiPlusBoundaryA[4],
			pAd->TxAgcStepA, pAd->bAutoTxAgcA);
	}	
	pAd->BbpRssiToDbmDelta = 0x79;

	RTUSBReadEEPROM(pAd, EEPROM_FREQ_OFFSET, (PUCHAR) &value, 2);
	if ((value & 0xFF00) == 0xFF00)
	{
		pAd->RFProgSeq = 0;
	}
	else
	{
		pAd->RFProgSeq = (value & 0x0300) >> 8;	// bit 8,9
	}

	value &= 0x00FF;
	if (value != 0x00FF)
		pAd->RfFreqOffset = (ULONG) value;
	else
		pAd->RfFreqOffset = 0;
	DBGPRINT(RT_DEBUG_TRACE, "E2PROM: RF freq offset=0x%x\n", pAd->RfFreqOffset);

	//CountryRegion byte offset = 0x25
	value = pAd->EEPROMDefaultValue[2] >> 8;
	value2 = pAd->EEPROMDefaultValue[2] & 0x00FF;
	if ((value <= 6) && (value2 <= 7))
	{
		pAd->PortCfg.CountryRegion = ((UCHAR) value) | 0x80;
		pAd->PortCfg.CountryRegionForABand = ((UCHAR) value2) | 0x80;
	}

	//
	// Get RSSI Offset on EEPROM 0x9Ah & 0x9Ch.
	// The valid value are (-10 ~ 10) 
	// 
	RTUSBReadEEPROM(pAd, EEPROM_RSSI_BG_OFFSET, (PUCHAR) &value, 2);
	pAd->BGRssiOffset1 = value & 0x00ff;
	pAd->BGRssiOffset2 = (value >> 8);

	// Validate 11b/g RSSI_1 offset.
	if ((pAd->BGRssiOffset1 < -10) || (pAd->BGRssiOffset1 > 10))
		pAd->BGRssiOffset1 = 0;

	// Validate 11b/g RSSI_2 offset.
	if ((pAd->BGRssiOffset2 < -10) || (pAd->BGRssiOffset2 > 10))
		pAd->BGRssiOffset2 = 0;
		
	RTUSBReadEEPROM(pAd, EEPROM_RSSI_A_OFFSET, (PUCHAR) &value, 2);
	pAd->ARssiOffset1 = value & 0x00ff;
	pAd->ARssiOffset2 = (value >> 8);

	// Validate 11a RSSI_1 offset.
	if ((pAd->ARssiOffset1 < -10) || (pAd->ARssiOffset1 > 10))

⌨️ 快捷键说明

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