2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Purpose: MAC Data structure
32 #include <linux/module.h>
33 #include <linux/types.h>
34 #include <linux/init.h>
36 #include <linux/errno.h>
37 #include <linux/ioport.h>
38 #include <linux/pci.h>
39 #include <linux/kernel.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/skbuff.h>
43 #include <linux/delay.h>
44 #include <linux/timer.h>
45 #include <linux/slab.h>
46 #include <linux/interrupt.h>
47 #include <linux/string.h>
48 #include <linux/wait.h>
49 #include <linux/if_arp.h>
50 #include <linux/sched.h>
53 //#include <linux/config.h>
54 #include <asm/uaccess.h>
55 #include <linux/proc_fs.h>
56 #include <linux/inetdevice.h>
57 #include <linux/reboot.h>
59 #define DEVICE_ETHTOOL_IOCTL_SUPPORT
60 #include <linux/ethtool.h>
62 #undef DEVICE_ETHTOOL_IOCTL_SUPPORT
64 /* Include Wireless Extension definition and check version - Jean II */
65 #include <linux/wireless.h>
66 #include <net/iw_handler.h> // New driver API
68 //2008-0409-07, <Add> by Einsn Liu
69 #ifndef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
70 #define WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
72 //2008-4-14<add> by chester for led issue
73 //#define FOR_LED_ON_NOTEBOOK
81 #include "device_cfg.h"
101 /*--------------------- Export Definitions -------------------------*/
103 #define MAC_MAX_CONTEXT_REG (256+128)
105 #define MAX_MULTICAST_ADDRESS_NUM 32
106 #define MULTICAST_ADDRESS_LIST_SIZE (MAX_MULTICAST_ADDRESS_NUM * ETH_ALEN)
109 //#define OP_MODE_INFRASTRUCTURE 0
110 //#define OP_MODE_ADHOC 1
111 //#define OP_MODE_AP 2
113 #define DUPLICATE_RX_CACHE_LENGTH 5
115 #define NUM_KEY_ENTRY 11
117 #define TX_WEP_NONE 0
120 #define TX_WEP_SWOTP 3
121 #define TX_WEP_OTPSW 4
122 #define TX_WEP_SW232 5
124 #define KEYSEL_WEP40 0
125 #define KEYSEL_WEP104 1
126 #define KEYSEL_TKIP 2
127 #define KEYSEL_CCMP 3
131 #define AUTO_FB_NONE 0
141 #define ANT_DIVERSITY 2
142 #define ANT_RXD_TXA 3
143 #define ANT_RXD_TXB 4
144 #define ANT_UNKNOWN 0xFF
146 #define MAXCHECKHANGCNT 4
148 #define BB_VGA_LEVEL 4
149 #define BB_VGA_CHANGE_THRESHOLD 16
154 #define RUN_AT(x) (jiffies+(x))
158 #define RESERV_AC0DMA 4
164 #define AVAIL_TD(p,q) ((p)->sOpts.nTxDescs[(q)]-((p)->iTDUsed[(q)]))
172 #define PRIVATE_Message 0
174 /*--------------------- Export Types ------------------------------*/
177 #define DBG_PRT(l, p, args...) {if (l<=msglevel) printk( p ,##args);}
178 #define PRINT_K(p, args...) {if (PRIVATE_Message) printk( p ,##args);}
181 typedef enum _VIA_BB_TYPE
186 } VIA_BB_TYPE, *PVIA_BB_TYPE;
188 //0:11a,1:11b,2:11gb(only CCK in BasicRate),3:11ga(OFDM in Basic Rate)
189 typedef enum _VIA_PKT_TYPE
195 } VIA_PKT_TYPE, *PVIA_PKT_TYPE;
198 typedef enum __device_msg_level {
199 MSG_LEVEL_ERR=0, //Errors that will cause abnormal operation.
200 MSG_LEVEL_NOTICE=1, //Some errors need users to be notified.
201 MSG_LEVEL_INFO=2, //Normal message.
202 MSG_LEVEL_VERBOSE=3, //Will report all trival errors.
203 MSG_LEVEL_DEBUG=4 //Only for debug purpose.
204 } DEVICE_MSG_LEVEL, *PDEVICE_MSG_LEVEL;
206 typedef enum __device_init_type {
207 DEVICE_INIT_COLD=0, // cold init
208 DEVICE_INIT_RESET, // reset init or Dx to D0 power remain init
209 DEVICE_INIT_DXPL // Dx to D0 power lost init
210 } DEVICE_INIT_TYPE, *PDEVICE_INIT_TYPE;
215 #define MAX_BSSIDINFO_4_PMKID 16
216 #define MAX_PMKIDLIST 5
217 //Flags for PMKID Candidate list structure
218 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
221 typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
224 typedef enum _NDIS_802_11_WEP_STATUS
226 Ndis802_11WEPEnabled,
227 Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled,
228 Ndis802_11WEPDisabled,
229 Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled,
230 Ndis802_11WEPKeyAbsent,
231 Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent,
232 Ndis802_11WEPNotSupported,
233 Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported,
234 Ndis802_11Encryption2Enabled,
235 Ndis802_11Encryption2KeyAbsent,
236 Ndis802_11Encryption3Enabled,
237 Ndis802_11Encryption3KeyAbsent
238 } NDIS_802_11_WEP_STATUS, *PNDIS_802_11_WEP_STATUS,
239 NDIS_802_11_ENCRYPTION_STATUS, *PNDIS_802_11_ENCRYPTION_STATUS;
242 typedef enum _NDIS_802_11_STATUS_TYPE
244 Ndis802_11StatusType_Authentication,
245 Ndis802_11StatusType_MediaStreamMode,
246 Ndis802_11StatusType_PMKID_CandidateList,
247 Ndis802_11StatusTypeMax // not a real type, defined as an upper bound
248 } NDIS_802_11_STATUS_TYPE, *PNDIS_802_11_STATUS_TYPE;
250 //Added new types for PMKID Candidate lists.
251 typedef struct _PMKID_CANDIDATE {
252 NDIS_802_11_MAC_ADDRESS BSSID;
254 } PMKID_CANDIDATE, *PPMKID_CANDIDATE;
257 typedef struct _BSSID_INFO
259 NDIS_802_11_MAC_ADDRESS BSSID;
260 NDIS_802_11_PMKID_VALUE PMKID;
261 } BSSID_INFO, *PBSSID_INFO;
263 typedef struct tagSPMKID {
265 ULONG BSSIDInfoCount;
266 BSSID_INFO BSSIDInfo[MAX_BSSIDINFO_4_PMKID];
269 typedef struct tagSPMKIDCandidateEvent {
270 NDIS_802_11_STATUS_TYPE StatusType;
271 ULONG Version; // Version of the structure
272 ULONG NumCandidates; // No. of pmkid candidates
273 PMKID_CANDIDATE CandidateList[MAX_PMKIDLIST];
274 } SPMKIDCandidateEvent, *PSPMKIDCandidateEvent;
279 #define MAX_QUIET_COUNT 8
281 typedef struct tagSQuietControl {
286 } SQuietControl, *PSQuietControl;
289 typedef struct __chip_info_tbl{
295 } CHIP_INFO, *PCHIP_INFO;
301 } DEVICE_OWNER_TYPE, *PDEVICE_OWNER_TYPE;
304 // The receive duplicate detection cache entry
305 typedef struct tagSCacheEntry{
307 BYTE abyAddr2[ETH_ALEN];
308 } SCacheEntry, *PSCacheEntry;
310 typedef struct tagSCache{
311 /* The receive cache is updated circularly. The next entry to be written is
312 * indexed by the "InPtr".
314 UINT uInPtr; // Place to use next
315 SCacheEntry asCacheEntry[DUPLICATE_RX_CACHE_LENGTH];
318 #define CB_MAX_RX_FRAG 64
319 // DeFragment Control Block, used for collecting fragments prior to reassembly
320 typedef struct tagSDeFragControlBlock
324 BYTE abyAddr2[ETH_ALEN];
330 } SDeFragControlBlock, *PSDeFragControlBlock;
335 #define DEVICE_FLAGS_IP_ALIGN 0x00000001UL
336 #define DEVICE_FLAGS_PREAMBLE_TYPE 0x00000002UL
337 #define DEVICE_FLAGS_OP_MODE 0x00000004UL
338 #define DEVICE_FLAGS_PS_MODE 0x00000008UL
339 #define DEVICE_FLAGS_80211h_MODE 0x00000010UL
340 #define DEVICE_FLAGS_DiversityANT 0x00000020UL
342 //flags for driver status
343 #define DEVICE_FLAGS_OPENED 0x00010000UL
344 #define DEVICE_FLAGS_WOL_ENABLED 0x00080000UL
345 //flags for capbilities
346 #define DEVICE_FLAGS_TX_ALIGN 0x01000000UL
347 #define DEVICE_FLAGS_HAVE_CAM 0x02000000UL
348 #define DEVICE_FLAGS_FLOW_CTRL 0x04000000UL
350 //flags for MII status
351 #define DEVICE_LINK_FAIL 0x00000001UL
352 #define DEVICE_SPEED_10 0x00000002UL
353 #define DEVICE_SPEED_100 0x00000004UL
354 #define DEVICE_SPEED_1000 0x00000008UL
355 #define DEVICE_DUPLEX_FULL 0x00000010UL
356 #define DEVICE_AUTONEG_ENABLE 0x00000020UL
357 #define DEVICE_FORCED_BY_EEPROM 0x00000040UL
358 //for device_set_media_duplex
359 #define DEVICE_LINK_CHANGE 0x00000001UL
365 typedef struct _RxManagementQueue
369 PSRxMgmtPacket Q[NUM];
370 } RxManagementQueue,*PSRxManagementQueue;
377 typedef struct __device_opt {
378 int nRxDescs0; //Number of RX descriptors0
379 int nRxDescs1; //Number of RX descriptors1
380 int nTxDescs[2]; //Number of TX descriptors 0, 1
381 int int_works; //interrupt limits
382 int rts_thresh; //rts threshold
390 } OPTIONS, *POPTIONS;
393 typedef struct __device_info {
394 struct __device_info* next;
395 struct __device_info* prev;
397 struct pci_dev* pcid;
404 struct net_device* dev;
405 struct net_device* next_module;
406 struct net_device_stats stats;
408 //dma addr, rx/tx pool
410 dma_addr_t rd0_pool_dma;
411 dma_addr_t rd1_pool_dma;
413 dma_addr_t td0_pool_dma;
414 dma_addr_t td1_pool_dma;
416 dma_addr_t tx_bufs_dma0;
417 dma_addr_t tx_bufs_dma1;
418 dma_addr_t tx_beacon_dma;
422 PBYTE tx_beacon_bufs;
437 volatile int iTDUsed[TYPE_MAXTD];
439 volatile PSTxDesc apCurrTD[TYPE_MAXTD];
440 volatile PSTxDesc apTailTD[TYPE_MAXTD];
442 volatile PSTxDesc apTD0Rings;
443 volatile PSTxDesc apTD1Rings;
445 volatile PSRxDesc aRD0Ring;
446 volatile PSRxDesc aRD1Ring;
447 volatile PSRxDesc pCurrRD[TYPE_MAXRD];
450 SDeFragControlBlock sRxDFCB[CB_MAX_RX_FRAG];
453 UINT uCurrentDFCBIdx;
465 struct tasklet_struct RxMngWorkItem;
466 RxManagementQueue rxManeQueue;
470 struct completion notify;
471 struct semaphore mlme_semaphore;
484 BYTE byOriginalZonetype;
485 BYTE abyMacContext[MAC_MAX_CONTEXT_REG];
486 BOOL bLinkPass; // link status: OK or fail
487 BYTE abyCurrentNetAddr[ETH_ALEN];
489 // Adapter statistics
490 SStatCounter scStatistic;
492 SDot11Counters s802_11Counter;
497 SMgmtObject sMgmtObj;
499 // 802.11 MAC specific
503 DWORD dwTxAntennaSel;
504 DWORD dwRxAntennaSel;
506 BYTE byRxAntennaMode;
507 BYTE byTxAntennaMode;
511 UINT uSIFS; //Current SIFS
512 UINT uDIFS; //Current DIFS
513 UINT uEIFS; //Current EIFS
514 UINT uSlot; //Current SlotTime
515 UINT uCwMin; //Current CwMin
516 UINT uCwMax; //CwMax is fixed on 1023.
523 CARD_PHY_TYPE eCurrentPHYType;
526 VIA_BB_TYPE byBBType; //0: 11A, 1:11B, 2:11G
527 VIA_PKT_TYPE byPacketType; //0:11a,1:11b,2:11gb(only CCK in BasicRate),3:11ga(OFDM in Basic Rate)
530 BYTE byTopOFDMBasicRate;
531 BYTE byTopCCKBasicRate;
535 UINT uConnectionRate;
538 BYTE byShortPreamble;
542 WORD wFragmentationThreshold;
543 BYTE byShortRetryLimit;
544 BYTE byLongRetryLimit;
545 CARD_OP_MODE eOPMode;
548 WORD wMaxTransmitMSDULifetime;
549 BYTE abyBSSID[ETH_ALEN];
550 BYTE abyDesireBSSID[ETH_ALEN];
551 WORD wCTSDuration; // update while speed change
552 WORD wACKDuration; // update while speed change
553 WORD wRTSTransmitLen; // update while speed change
554 BYTE byRTSServiceField; // update while speed change
555 BYTE byRTSSignalField; // update while speed change
557 DWORD dwMaxReceiveLifetime; // dot11MaxReceiveLifetime
560 BOOL bEncryptionEnable;
565 BOOL bBarkerPreambleMd;
568 WORD wUseProtectCntDown;
570 BOOL bRadioControlOff;
573 WORD wListenInterval;
575 WMAC_POWER_MODE ePSMode;
578 // GPIO Radio Control
582 BOOL bPrvActive4RadioOFF;
588 BOOL bBeaconBufReady;
590 BOOL bIsBeaconBufReadySet;
591 UINT cbBeaconBufReadySetCnt;
596 CMD_STATE eCommandState;
604 UINT uAutoReConnectTime;
608 CMD_ITEM eCmdQueue[CMD_Q_SIZE];
622 NDIS_802_11_WEP_STATUS eEncryptionStatus;
624 //2007-0925-01<Add>by MikeLiu
625 //mike add :save old Encryption
626 NDIS_802_11_WEP_STATUS eOldEncryptionStatus;
631 QWORD qwPacketNumber; //For CCMP and TKIP as TSC(6 bytes)
632 UINT uCurrentWEPMode;
635 BYTE abyPRNG[WLAN_WEPMAX_KEYLEN+3];
638 BYTE abyKey[WLAN_WEP232_KEYLEN];
650 // for OID_802_11_ASSOCIATION_INFORMATION
663 // For Update BaseBand VGA Gain Offset
665 UINT uBBVGADiffCount;
668 BYTE abyBBVGA[BB_VGA_LEVEL];
669 LONG ldBmThreshold[BB_VGA_LEVEL];
676 DWORD dwDiagRefCount;
681 // For Auto Power Tunning
683 BYTE byAutoPwrTunning;
685 SHORT sPSetPointOFDMG;
686 SHORT sPSetPointOFDMA;
687 LONG lPFormulaOffset;
692 // For RF Power table
697 BYTE abyCCKPwrTbl[CB_MAX_CHANNEL_24G+1];
698 BYTE abyOFDMPwrTbl[CB_MAX_CHANNEL+1];
699 char abyCCKDefaultPwr[CB_MAX_CHANNEL_24G+1];
700 char abyOFDMDefaultPwr[CB_MAX_CHANNEL+1];
701 char abyRegPwr[CB_MAX_CHANNEL+1];
702 char abyLocalPwr[CB_MAX_CHANNEL+1];
705 // BaseBand Loopback Use
712 struct timer_list sTimerCommand;
714 struct timer_list sTimerTxData;
715 ULONG nTxDataTimeCout;
717 BOOL IsTxDataTrigger;
720 #ifdef WPA_SM_Transtatus
721 BOOL fWPA_Authened; //is WPA/WPA-PSK or WPA2/WPA2-PSK authen??
723 BYTE byReAssocCount; //mike add:re-association retry times!
724 BYTE byLinkWaitCount;
727 BYTE abyNodeName[17];
729 BOOL bDiversityRegCtlON;
730 BOOL bDiversityEnable;
731 ULONG ulDiversityNValue;
732 ULONG ulDiversityMValue;
741 ULONG ulRatio_State0;
742 ULONG ulRatio_State1;
744 //SQ3 functions for antenna diversity
745 struct timer_list TimerSQ3Tmax1;
746 struct timer_list TimerSQ3Tmax2;
747 struct timer_list TimerSQ3Tmax3;
750 ULONG uNumSQ3[MAX_RATE];
751 WORD wAntDiversityMaxRate;
754 SEthernetHeader sTxEthHeader;
755 SEthernetHeader sRxEthHeader;
756 BYTE abyBroadcastAddr[ETH_ALEN];
757 BYTE abySNAP_RFC1042[ETH_ALEN];
758 BYTE abySNAP_Bridgetunnel[ETH_ALEN];
759 BYTE abyEEPROM[EEP_MAX_CONTEXT_SIZE]; //DWORD alignment
760 // Pre-Authentication & PMK cache
762 SPMKIDCandidateEvent gsPMKIDCandidate;
767 BYTE abyCountryCode[3];
769 UINT uNumOfMeasureEIDs;
770 PWLAN_IE_MEASURE_REQ pCurrMeasureEID;
771 BOOL bMeasureInProgress;
782 BYTE byChannelSwitchCount;
784 BOOL bEnableFirstQuiet;
785 BYTE byQuietStartCount;
787 DWORD dwCurrentQuietEndTime;
788 SQuietControl sQuiet[MAX_QUIET_COUNT];
791 BOOL bCountryInfo24G;
793 WORD wBeaconInterval;
795 //WPA supplicant deamon
796 struct net_device *wpadev;
799 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
807 BOOL bWPASuppWextEnabled;
812 // user space daemon: hostapd, is used for HOSTAP
816 struct net_device *apdev;
817 int (*tx_80211)(struct sk_buff *skb, struct net_device *dev);
822 struct iw_statistics wstats; // wireless stats
825 } DEVICE_INFO, *PSDevice;
831 inline static void EnQueue (PSDevice pDevice,PSRxMgmtPacket pRxMgmtPacket)
833 //printk("Enter EnQueue:tail is %d\n",pDevice->rxManeQueue.tail);
834 if ((pDevice->rxManeQueue.tail+1) % NUM == pDevice->rxManeQueue.head)
836 //printk("Queue is Full,tail is %d\n",pDevice->rxManeQueue.tail);
841 pDevice->rxManeQueue.tail = (pDevice->rxManeQueue.tail+1)% NUM;
842 pDevice->rxManeQueue.Q[pDevice->rxManeQueue.tail] = pRxMgmtPacket;
843 pDevice->rxManeQueue.packet_num++;
844 //printk("packet num is %d\n",pDevice->rxManeQueue.packet_num);
851 inline static PSRxMgmtPacket DeQueue (PSDevice pDevice)
853 PSRxMgmtPacket pRxMgmtPacket;
854 if (pDevice->rxManeQueue.tail == pDevice->rxManeQueue.head)
856 printk("Queue is Empty\n");
862 //x=pDevice->rxManeQueue.head = (pDevice->rxManeQueue.head+1)%NUM;
863 pDevice->rxManeQueue.head = (pDevice->rxManeQueue.head+1)%NUM;
864 x = pDevice->rxManeQueue.head;
865 //printk("Enter DeQueue:head is %d\n",x);
866 pRxMgmtPacket = pDevice->rxManeQueue.Q[x];
867 pDevice->rxManeQueue.packet_num--;
868 return pRxMgmtPacket;
872 void InitRxManagementQueue(PSDevice pDevice);
883 inline static BOOL device_get_ip(PSDevice pInfo) {
884 struct in_device* in_dev=(struct in_device*) pInfo->dev->ip_ptr;
885 struct in_ifaddr* ifa;
888 ifa=(struct in_ifaddr*) in_dev->ifa_list;
890 memcpy(pInfo->abyIPAddr,&ifa->ifa_address,4);
899 static inline PDEVICE_RD_INFO alloc_rd_info(void) {
901 ptr = (PDEVICE_RD_INFO)kmalloc((int)sizeof(DEVICE_RD_INFO), (int)GFP_ATOMIC);
905 memset(ptr,0,sizeof(DEVICE_RD_INFO));
910 static inline PDEVICE_TD_INFO alloc_td_info(void) {
912 ptr = (PDEVICE_TD_INFO)kmalloc((int)sizeof(DEVICE_TD_INFO), (int)GFP_ATOMIC);
916 memset(ptr,0,sizeof(DEVICE_TD_INFO));
921 /*--------------------- Export Functions --------------------------*/
923 BOOL device_dma0_xmit(PSDevice pDevice, struct sk_buff *skb, UINT uNodeIndex);
924 BOOL device_alloc_frag_buf(PSDevice pDevice, PSDeFragControlBlock pDeF);
925 int Config_FileOperation(PSDevice pDevice,BOOL fwrite,unsigned char *Parameter);