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>
52 #include <linux/rtnetlink.h>//James
53 #include <linux/proc_fs.h>
54 #include <linux/inetdevice.h>
55 #include <linux/reboot.h>
56 #include <linux/usb.h>
57 #include <linux/signal.h>
59 #include <asm/uaccess.h>
61 #define DEVICE_ETHTOOL_IOCTL_SUPPORT
62 #include <linux/ethtool.h>
64 #undef DEVICE_ETHTOOL_IOCTL_SUPPORT
66 /* Include Wireless Extension definition and check version - Jean II */
67 #include <linux/wireless.h>
68 #include <net/iw_handler.h> // New driver API
70 #ifndef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
71 #define WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
74 //2007-0920-01<Add>by MikeLiu
77 //please copy below macro to driver_event.c for API
78 #define RT_INSMOD_EVENT_FLAG 0x0101
79 #define RT_UPDEV_EVENT_FLAG 0x0102
80 #define RT_DISCONNECTED_EVENT_FLAG 0x0103
81 #define RT_WPACONNECTED_EVENT_FLAG 0x0104
82 #define RT_DOWNDEV_EVENT_FLAG 0x0105
83 #define RT_RMMOD_EVENT_FLAG 0x0106
90 #include "device_cfg.h"
103 /*--------------------- Export Definitions -------------------------*/
104 #define VNT_USB_VENDOR_ID 0x160a
105 #define VNT_USB_PRODUCT_ID 0x3184
107 #define MAC_MAX_CONTEXT_REG (256+128)
109 #define MAX_MULTICAST_ADDRESS_NUM 32
110 #define MULTICAST_ADDRESS_LIST_SIZE (MAX_MULTICAST_ADDRESS_NUM * ETH_ALEN)
113 //#define OP_MODE_INFRASTRUCTURE 0
114 //#define OP_MODE_ADHOC 1
115 //#define OP_MODE_AP 2
117 #define DUPLICATE_RX_CACHE_LENGTH 5
119 #define NUM_KEY_ENTRY 11
121 #define TX_WEP_NONE 0
124 #define TX_WEP_SWOTP 3
125 #define TX_WEP_OTPSW 4
126 #define TX_WEP_SW232 5
128 #define KEYSEL_WEP40 0
129 #define KEYSEL_WEP104 1
130 #define KEYSEL_TKIP 2
131 #define KEYSEL_CCMP 3
135 #define AUTO_FB_NONE 0
145 #define ANT_DIVERSITY 2
146 #define ANT_RXD_TXA 3
147 #define ANT_RXD_TXB 4
148 #define ANT_UNKNOWN 0xFF
155 #define MAXCHECKHANGCNT 4
158 #define TX_PKT_UNI 0x00
159 #define TX_PKT_MULTI 0x01
160 #define TX_PKT_BROAD 0x02
162 #define BB_VGA_LEVEL 4
163 #define BB_VGA_CHANGE_THRESHOLD 3
168 #define RUN_AT(x) (jiffies+(x))
172 #define RESERV_AC0DMA 4
174 #define PRIVATE_Message 0
176 /*--------------------- Export Types ------------------------------*/
178 #define DBG_PRT(l, p, args...) {if (l<=msglevel) printk( p ,##args);}
179 #define PRINT_K(p, args...) {if (PRIVATE_Message) printk( p ,##args);}
181 typedef enum __device_msg_level {
182 MSG_LEVEL_ERR=0, //Errors that will cause abnormal operation.
183 MSG_LEVEL_NOTICE=1, //Some errors need users to be notified.
184 MSG_LEVEL_INFO=2, //Normal message.
185 MSG_LEVEL_VERBOSE=3, //Will report all trival errors.
186 MSG_LEVEL_DEBUG=4 //Only for debug purpose.
187 } DEVICE_MSG_LEVEL, *PDEVICE_MSG_LEVEL;
189 typedef enum __device_init_type {
190 DEVICE_INIT_COLD=0, // cold init
191 DEVICE_INIT_RESET, // reset init or Dx to D0 power remain init
192 DEVICE_INIT_DXPL // Dx to D0 power lost init
193 } DEVICE_INIT_TYPE, *PDEVICE_INIT_TYPE;
199 // Enum of context types for SendPacket
201 typedef enum _CONTEXT_TYPE {
202 CONTEXT_DATA_PACKET = 1,
209 // RCB (Receive Control Block)
216 SRxMgmtPacket sMngPacket;
223 // used to track bulk out irps
224 typedef struct _USB_SEND_CONTEXT {
226 struct sk_buff *pPacket;
228 unsigned int uBufLen;
230 SEthernetHeader sEthHeader;
233 unsigned char Data[MAX_TOTAL_SIZE_WITH_ALL_HEADERS];
234 } USB_SEND_CONTEXT, *PUSB_SEND_CONTEXT;
237 /* structure got from configuration file as user-desired default settings */
238 typedef struct _DEFAULT_CONFIG {
240 signed int eConfigMode;
241 signed int eAuthenMode; /* open/wep/wpa */
242 signed int bShareKeyAlgorithm; /* open-open/{open,wep}-sharekey */
243 signed int keyidx; /* wepkey index */
244 signed int eEncryptionStatus;
245 } DEFAULT_CONFIG, *PDEFAULT_CONFIG;
248 // Structure to keep track of usb interrupt packets
251 unsigned int uDataLen;
255 } INT_BUFFER, *PINT_BUFFER;
260 typedef enum _VIA_BB_TYPE
265 } VIA_BB_TYPE, *PVIA_BB_TYPE;
267 //0:11a,1:11b,2:11gb(only CCK in BasicRate),3:11ga(OFDM in Basic Rate)
268 typedef enum _VIA_PKT_TYPE
274 } VIA_PKT_TYPE, *PVIA_PKT_TYPE;
281 #define NDIS_STATUS int
284 typedef enum __DEVICE_NDIS_STATUS {
289 } DEVICE_NDIS_STATUS, *PDEVICE_NDIS_STATUS;
292 #define MAX_BSSIDINFO_4_PMKID 16
293 #define MAX_PMKIDLIST 5
294 //Flags for PMKID Candidate list structure
295 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
298 typedef unsigned char NDIS_802_11_PMKID_VALUE[16];
301 typedef enum _NDIS_802_11_WEP_STATUS
303 Ndis802_11WEPEnabled,
304 Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled,
305 Ndis802_11WEPDisabled,
306 Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled,
307 Ndis802_11WEPKeyAbsent,
308 Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent,
309 Ndis802_11WEPNotSupported,
310 Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported,
311 Ndis802_11Encryption2Enabled,
312 Ndis802_11Encryption2KeyAbsent,
313 Ndis802_11Encryption3Enabled,
314 Ndis802_11Encryption3KeyAbsent
315 } NDIS_802_11_WEP_STATUS, *PNDIS_802_11_WEP_STATUS,
316 NDIS_802_11_ENCRYPTION_STATUS, *PNDIS_802_11_ENCRYPTION_STATUS;
319 typedef enum _NDIS_802_11_STATUS_TYPE
321 Ndis802_11StatusType_Authentication,
322 Ndis802_11StatusType_MediaStreamMode,
323 Ndis802_11StatusType_PMKID_CandidateList,
324 Ndis802_11StatusTypeMax // not a real type, defined as an upper bound
325 } NDIS_802_11_STATUS_TYPE, *PNDIS_802_11_STATUS_TYPE;
327 //Added new types for PMKID Candidate lists.
328 typedef struct _PMKID_CANDIDATE {
329 NDIS_802_11_MAC_ADDRESS BSSID;
331 } PMKID_CANDIDATE, *PPMKID_CANDIDATE;
334 typedef struct _BSSID_INFO
336 NDIS_802_11_MAC_ADDRESS BSSID;
337 NDIS_802_11_PMKID_VALUE PMKID;
338 } BSSID_INFO, *PBSSID_INFO;
340 typedef struct tagSPMKID {
341 unsigned long Length;
342 unsigned long BSSIDInfoCount;
343 BSSID_INFO BSSIDInfo[MAX_BSSIDINFO_4_PMKID];
346 typedef struct tagSPMKIDCandidateEvent {
347 NDIS_802_11_STATUS_TYPE StatusType;
348 unsigned long Version; /* Version of the structure */
349 unsigned long NumCandidates; /* No. of pmkid candidates */
350 PMKID_CANDIDATE CandidateList[MAX_PMKIDLIST];
351 } SPMKIDCandidateEvent, *PSPMKIDCandidateEvent;
356 #define MAX_QUIET_COUNT 8
358 typedef struct tagSQuietControl {
363 } SQuietControl, *PSQuietControl;
368 // The receive duplicate detection cache entry
369 typedef struct tagSCacheEntry{
371 BYTE abyAddr2[ETH_ALEN];
373 } SCacheEntry, *PSCacheEntry;
375 typedef struct tagSCache{
376 /* The receive cache is updated circularly. The next entry to be written is
377 * indexed by the "InPtr".
379 unsigned int uInPtr; /* Place to use next */
380 SCacheEntry asCacheEntry[DUPLICATE_RX_CACHE_LENGTH];
383 #define CB_MAX_RX_FRAG 64
384 // DeFragment Control Block, used for collecting fragments prior to reassembly
385 typedef struct tagSDeFragControlBlock
389 BYTE abyAddr2[ETH_ALEN];
390 unsigned int uLifetime;
393 unsigned int cbFrameLength;
395 } SDeFragControlBlock, *PSDeFragControlBlock;
400 #define DEVICE_FLAGS_UNPLUG 0x00000001UL
401 #define DEVICE_FLAGS_PREAMBLE_TYPE 0x00000002UL
402 #define DEVICE_FLAGS_OP_MODE 0x00000004UL
403 #define DEVICE_FLAGS_PS_MODE 0x00000008UL
404 #define DEVICE_FLAGS_80211h_MODE 0x00000010UL
406 //flags for driver status
407 #define DEVICE_FLAGS_OPENED 0x00010000UL
408 #define DEVICE_FLAGS_WOL_ENABLED 0x00080000UL
409 //flags for capbilities
410 #define DEVICE_FLAGS_TX_ALIGN 0x01000000UL
411 #define DEVICE_FLAGS_HAVE_CAM 0x02000000UL
412 #define DEVICE_FLAGS_FLOW_CTRL 0x04000000UL
414 //flags for MII status
415 #define DEVICE_LINK_FAIL 0x00000001UL
416 #define DEVICE_SPEED_10 0x00000002UL
417 #define DEVICE_SPEED_100 0x00000004UL
418 #define DEVICE_SPEED_1000 0x00000008UL
419 #define DEVICE_DUPLEX_FULL 0x00000010UL
420 #define DEVICE_AUTONEG_ENABLE 0x00000020UL
421 #define DEVICE_FORCED_BY_EEPROM 0x00000040UL
422 //for device_set_media_duplex
423 #define DEVICE_LINK_CHANGE 0x00000001UL
426 typedef struct __device_opt {
427 int nRxDescs0; //Number of RX descriptors0
428 int nTxDescs0; //Number of TX descriptors 0, 1, 2, 3
429 int rts_thresh; //rts threshold
438 } OPTIONS, *POPTIONS;
441 typedef struct __device_info {
444 struct usb_device* usb;
445 struct net_device* dev;
446 struct net_device_stats stats;
450 struct tasklet_struct CmdWorkItem;
451 struct tasklet_struct EventWorkItem;
452 struct tasklet_struct ReadWorkItem;
453 struct tasklet_struct RxMngWorkItem;
470 SDeFragControlBlock sRxDFCB[CB_MAX_RX_FRAG];
472 unsigned int cbFreeDFCB;
473 unsigned int uCurrentDFCBIdx;
477 struct urb *pControlURB;
478 struct urb *pInterruptURB;
479 struct usb_ctrlrequest sUsbCtlRequest;
481 unsigned int int_interval;
483 // Variables to track resources for the BULK In Pipe
486 PRCB apRCB[CB_MAX_RX_DESC];
488 PRCB FirstRecvFreeList;
489 PRCB LastRecvFreeList;
490 unsigned int NumRecvFreeList;
491 PRCB FirstRecvMngList;
492 PRCB LastRecvMngList;
493 unsigned int NumRecvMngList;
494 BOOL bIsRxWorkItemQueued;
495 BOOL bIsRxMngWorkItemQueued;
496 unsigned long ulRcvRefCount; /* packets that have not returned back */
499 // Variables to track resources for the BULK Out Pipe
502 PUSB_SEND_CONTEXT apTD[CB_MAX_TX_DESC];
506 // Variables to track resources for the Interript In Pipe
509 BOOL fKillEventPollingThread;
510 BOOL bEventAvailable;
513 //default config from file by user setting
514 DEFAULT_CONFIG config_file;
519 // protect with spinlock
520 unsigned long ulBulkInPosted;
521 unsigned long ulBulkInError;
522 unsigned long ulBulkInContCRCError;
523 unsigned long ulBulkInBytesRead;
525 unsigned long ulBulkOutPosted;
526 unsigned long ulBulkOutError;
527 unsigned long ulBulkOutContCRCError;
528 unsigned long ulBulkOutBytesWrite;
530 unsigned long ulIntInPosted;
531 unsigned long ulIntInError;
532 unsigned long ulIntInContCRCError;
533 unsigned long ulIntInBytesRead;
537 WORD wFirmwareVersion;
546 BYTE byOriginalZonetype;
548 BOOL bLinkPass; // link status: OK or fail
549 BYTE abyCurrentNetAddr[ETH_ALEN];
550 BYTE abyPermanentNetAddr[ETH_ALEN];
551 // SW network address
552 /* u8 abySoftwareNetAddr[ETH_ALEN]; */
553 BOOL bExistSWNetAddr;
555 // Adapter statistics
556 SStatCounter scStatistic;
558 SDot11Counters s802_11Counter;
561 // Maintain statistical debug info.
563 unsigned long packetsReceived;
564 unsigned long packetsReceivedDropped;
565 unsigned long packetsReceivedOverflow;
566 unsigned long packetsSent;
567 unsigned long packetsSentDropped;
568 unsigned long SendContextsInUse;
569 unsigned long RcvBuffersInUse;
573 SMgmtObject sMgmtObj;
576 unsigned int cbBulkInMax;
579 // 802.11 MAC specific
580 unsigned int uCurrRSSI;
586 DWORD dwRxAntennaSel;
587 DWORD dwTxAntennaSel;
589 BYTE byRxAntennaMode;
590 BYTE byTxAntennaMode;
594 //SQ3 functions for antenna diversity
595 struct timer_list TimerSQ3Tmax1;
596 struct timer_list TimerSQ3Tmax2;
597 struct timer_list TimerSQ3Tmax3;
599 BOOL bDiversityRegCtlON;
600 BOOL bDiversityEnable;
601 unsigned long ulDiversityNValue;
602 unsigned long ulDiversityMValue;
606 unsigned long ulSQ3TH;
608 unsigned long uDiversityCnt;
610 unsigned long ulRatio_State0;
611 unsigned long ulRatio_State1;
612 unsigned long ulSQ3_State0;
613 unsigned long ulSQ3_State1;
615 unsigned long aulSQ3Val[MAX_RATE];
616 unsigned long aulPktNum[MAX_RATE];
619 unsigned int uSIFS; /* Current SIFS */
620 unsigned int uDIFS; /* Current DIFS */
621 unsigned int uEIFS; /* Current EIFS */
622 unsigned int uSlot; /* Current SlotTime */
623 unsigned int uCwMin; /* Current CwMin */
624 unsigned int uCwMax; /* CwMax is fixed on 1023 */
634 VIA_BB_TYPE byBBType; //0: 11A, 1:11B, 2:11G
635 VIA_PKT_TYPE byPacketType; //0:11a,1:11b,2:11gb(only CCK in BasicRate),3:11ga(OFDM in Basic Rate)
638 BYTE byTopOFDMBasicRate;
639 BYTE byTopCCKBasicRate;
642 DWORD dwAotoRateTxOkCnt;
643 DWORD dwAotoRateTxFailCnt;
644 DWORD dwErrorRateThreshold[13];
645 DWORD dwTPTable[MAX_RATE];
646 BYTE abyEEPROM[EEP_MAX_CONTEXT_SIZE]; //DWORD alignment
650 unsigned int uConnectionRate;
653 BYTE byShortPreamble;
657 // For RF Power table
662 BYTE abyCCKPwrTbl[14];
663 BYTE abyOFDMPwrTbl[14];
664 BYTE abyOFDMAPwrTbl[42];
668 WORD wFragmentationThreshold;
669 BYTE byShortRetryLimit;
670 BYTE byLongRetryLimit;
671 CARD_OP_MODE eOPMode;
673 WORD wMaxTransmitMSDULifetime;
674 BYTE abyBSSID[ETH_ALEN];
675 BYTE abyDesireBSSID[ETH_ALEN];
676 WORD wCTSDuration; // update while speed change
677 WORD wACKDuration; // update while speed change
678 WORD wRTSTransmitLen; // update while speed change
679 BYTE byRTSServiceField; // update while speed change
680 BYTE byRTSSignalField; // update while speed change
682 DWORD dwMaxReceiveLifetime; // dot11MaxReceiveLifetime
685 BOOL bEncryptionEnable;
687 BOOL bSoftwareGenCrcErr;
691 BOOL bBarkerPreambleMd;
694 WORD wUseProtectCntDown;
696 BOOL bRadioControlOff;
701 WORD wListenInterval;
703 WMAC_POWER_MODE ePSMode;
704 unsigned long ulPSModeWaitTx;
709 BOOL bBeaconBufReady;
713 unsigned int uScanTime;
715 CMD_STATE eCommandState;
724 unsigned int uAutoReConnectTime;
725 unsigned int uIsroamingTime;
729 CMD_ITEM eCmdQueue[CMD_Q_SIZE];
730 unsigned int uCmdDequeueIdx;
731 unsigned int uCmdEnqueueIdx;
732 unsigned int cbFreeCmdQueue;
737 BOOL bEnableRoaming; //DavidWang
738 BOOL bIsRoaming; //DavidWang
739 BOOL bFastRoaming; //DavidWang
740 BYTE bSameBSSMaxNum; //Davidwang
741 BYTE bSameBSSCurNum; //DavidWang
744 unsigned long ulTxPower;
747 NDIS_802_11_WEP_STATUS eEncryptionStatus;
750 //2007-0925-01<Add>by MikeLiu
751 //mike add :save old Encryption
752 NDIS_802_11_WEP_STATUS eOldEncryptionStatus;
759 BYTE abyPRNG[WLAN_WEPMAX_KEYLEN+3];
765 unsigned int uKeyLength;
766 BYTE abyKey[WLAN_WEP232_KEYLEN];
769 unsigned int uAssocCount;
782 // For Update BaseBand VGA Gain Offset
784 unsigned int uBBVGADiffCount;
787 BYTE abyBBVGA[BB_VGA_LEVEL];
788 signed long ldBmThreshold[BB_VGA_LEVEL];
795 DWORD dwDiagRefCount;
800 // For Auto Power Tunning
802 BYTE byAutoPwrTunning;
804 // BaseBand Loopback Use
811 struct timer_list sTimerCommand;
813 //2007-0115-01<Add>by MikeLiu
815 struct timer_list sTimerTxData;
816 unsigned long nTxDataTimeCout;
818 BOOL IsTxDataTrigger;
821 #ifdef WPA_SM_Transtatus
822 BOOL fWPA_Authened; //is WPA/WPA-PSK or WPA2/WPA2-PSK authen??
824 BYTE byReAssocCount; //mike add:re-association retry times!
825 BYTE byLinkWaitCount;
827 SEthernetHeader sTxEthHeader;
828 SEthernetHeader sRxEthHeader;
829 BYTE abyBroadcastAddr[ETH_ALEN];
830 BYTE abySNAP_RFC1042[ETH_ALEN];
831 BYTE abySNAP_Bridgetunnel[ETH_ALEN];
833 // Pre-Authentication & PMK cache
835 SPMKIDCandidateEvent gsPMKIDCandidate;
843 BYTE byChannelSwitchCount;
845 //WPA supplicant daemon
846 struct net_device *wpadev;
851 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
856 BOOL bWPASuppWextEnabled;
860 // user space daemon: hostapd, is used for HOSTAP
864 struct net_device *apdev;
865 int (*tx_80211)(struct sk_buff *skb, struct net_device *dev);
867 unsigned int uChannel;
869 struct iw_statistics wstats; // wireless stats
872 } DEVICE_INFO, *PSDevice;
877 #define EnqueueRCB(_Head, _Tail, _RCB) \
883 _Tail->Next = _RCB; \
889 #define DequeueRCB(Head, Tail) \
899 #define ADD_ONE_WITH_WRAP_AROUND(uVar, uModulo) { \
900 if ((uVar) >= ((uModulo) - 1)) \
907 #define fMP_RESET_IN_PROGRESS 0x00000001
908 #define fMP_DISCONNECTED 0x00000002
909 #define fMP_HALT_IN_PROGRESS 0x00000004
910 #define fMP_SURPRISE_REMOVED 0x00000008
911 #define fMP_RECV_LOOKASIDE 0x00000010
912 #define fMP_INIT_IN_PROGRESS 0x00000020
913 #define fMP_SEND_SIDE_RESOURCE_ALLOCATED 0x00000040
914 #define fMP_RECV_SIDE_RESOURCE_ALLOCATED 0x00000080
915 #define fMP_POST_READS 0x00000100
916 #define fMP_POST_WRITES 0x00000200
917 #define fMP_CONTROL_READS 0x00000400
918 #define fMP_CONTROL_WRITES 0x00000800
922 #define MP_SET_FLAG(_M, _F) ((_M)->Flags |= (_F))
923 #define MP_CLEAR_FLAG(_M, _F) ((_M)->Flags &= ~(_F))
924 #define MP_TEST_FLAG(_M, _F) (((_M)->Flags & (_F)) != 0)
925 #define MP_TEST_FLAGS(_M, _F) (((_M)->Flags & (_F)) == (_F))
927 #define MP_IS_READY(_M) (((_M)->Flags & \
928 (fMP_DISCONNECTED | fMP_RESET_IN_PROGRESS | fMP_HALT_IN_PROGRESS | fMP_INIT_IN_PROGRESS | fMP_SURPRISE_REMOVED)) == 0)
930 /*--------------------- Export Functions --------------------------*/
932 /* BOOL device_dma0_xmit(PSDevice pDevice, struct sk_buff *skb,
933 * unsigned int uNodeIndex);
935 BOOL device_alloc_frag_buf(PSDevice pDevice, PSDeFragControlBlock pDeF);