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/version.h>
48 #include <linux/string.h>
49 #include <linux/wait.h>
50 #include <linux/if_arp.h>
51 #include <linux/sched.h>
53 #include <linux/rtnetlink.h>//James
54 #include <linux/proc_fs.h>
55 #include <linux/inetdevice.h>
56 #include <linux/reboot.h>
57 #include <linux/usb.h>
58 #include <linux/signal.h>
60 #include <asm/uaccess.h>
62 #define DEVICE_ETHTOOL_IOCTL_SUPPORT
63 #include <linux/ethtool.h>
65 #undef DEVICE_ETHTOOL_IOCTL_SUPPORT
67 /* Include Wireless Extension definition and check version - Jean II */
68 #include <linux/wireless.h>
69 #include <net/iw_handler.h> // New driver API
71 #ifndef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
72 #define WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
75 //2007-0920-01<Add>by MikeLiu
78 //please copy below macro to driver_event.c for API
79 #define RT_INSMOD_EVENT_FLAG 0x0101
80 #define RT_UPDEV_EVENT_FLAG 0x0102
81 #define RT_DISCONNECTED_EVENT_FLAG 0x0103
82 #define RT_WPACONNECTED_EVENT_FLAG 0x0104
83 #define RT_DOWNDEV_EVENT_FLAG 0x0105
84 #define RT_RMMOD_EVENT_FLAG 0x0106
91 #include "device_cfg.h"
104 /*--------------------- Export Definitions -------------------------*/
105 #define VNT_USB_VENDOR_ID 0x160a
106 #define VNT_USB_PRODUCT_ID 0x3184
108 #define MAC_MAX_CONTEXT_REG (256+128)
110 #define MAX_MULTICAST_ADDRESS_NUM 32
111 #define MULTICAST_ADDRESS_LIST_SIZE (MAX_MULTICAST_ADDRESS_NUM * U_ETHER_ADDR_LEN)
114 //#define OP_MODE_INFRASTRUCTURE 0
115 //#define OP_MODE_ADHOC 1
116 //#define OP_MODE_AP 2
118 #define DUPLICATE_RX_CACHE_LENGTH 5
120 #define NUM_KEY_ENTRY 11
122 #define TX_WEP_NONE 0
125 #define TX_WEP_SWOTP 3
126 #define TX_WEP_OTPSW 4
127 #define TX_WEP_SW232 5
129 #define KEYSEL_WEP40 0
130 #define KEYSEL_WEP104 1
131 #define KEYSEL_TKIP 2
132 #define KEYSEL_CCMP 3
136 #define AUTO_FB_NONE 0
146 #define ANT_DIVERSITY 2
147 #define ANT_RXD_TXA 3
148 #define ANT_RXD_TXB 4
149 #define ANT_UNKNOWN 0xFF
156 #define MAXCHECKHANGCNT 4
159 #define TX_PKT_UNI 0x00
160 #define TX_PKT_MULTI 0x01
161 #define TX_PKT_BROAD 0x02
163 #define BB_VGA_LEVEL 4
164 #define BB_VGA_CHANGE_THRESHOLD 3
169 #define RUN_AT(x) (jiffies+(x))
173 #define RESERV_AC0DMA 4
175 #define PRIVATE_Message 0
177 /*--------------------- Export Types ------------------------------*/
179 #define DBG_PRT(l, p, args...) {if (l<=msglevel) printk( p ,##args);}
180 #define PRINT_K(p, args...) {if (PRIVATE_Message) printk( p ,##args);}
182 typedef enum __device_msg_level {
183 MSG_LEVEL_ERR=0, //Errors that will cause abnormal operation.
184 MSG_LEVEL_NOTICE=1, //Some errors need users to be notified.
185 MSG_LEVEL_INFO=2, //Normal message.
186 MSG_LEVEL_VERBOSE=3, //Will report all trival errors.
187 MSG_LEVEL_DEBUG=4 //Only for debug purpose.
188 } DEVICE_MSG_LEVEL, *PDEVICE_MSG_LEVEL;
190 typedef enum __device_init_type {
191 DEVICE_INIT_COLD=0, // cold init
192 DEVICE_INIT_RESET, // reset init or Dx to D0 power remain init
193 DEVICE_INIT_DXPL // Dx to D0 power lost init
194 } DEVICE_INIT_TYPE, *PDEVICE_INIT_TYPE;
200 // Enum of context types for SendPacket
202 typedef enum _CONTEXT_TYPE {
203 CONTEXT_DATA_PACKET = 1,
210 // RCB (Receive Control Block)
217 SRxMgmtPacket sMngPacket;
224 // used to track bulk out irps
225 typedef struct _USB_SEND_CONTEXT {
227 struct sk_buff *pPacket;
231 SEthernetHeader sEthHeader;
234 UCHAR Data[MAX_TOTAL_SIZE_WITH_ALL_HEADERS];
235 } USB_SEND_CONTEXT, *PUSB_SEND_CONTEXT;
238 //structure got from configuration file as user desired default setting.
239 typedef struct _DEFAULT_CONFIG{
242 INT eAuthenMode; //open/wep/wpa
243 INT bShareKeyAlgorithm; //open-open/open-sharekey/wep-sharekey
244 INT keyidx; //wepkey index
245 INT eEncryptionStatus;
247 }DEFAULT_CONFIG,*PDEFAULT_CONFIG;
250 // Structure to keep track of usb interrupt packets
257 } INT_BUFFER, *PINT_BUFFER;
262 typedef enum _VIA_BB_TYPE
267 } VIA_BB_TYPE, *PVIA_BB_TYPE;
269 //0:11a,1:11b,2:11gb(only CCK in BasicRate),3:11ga(OFDM in Basic Rate)
270 typedef enum _VIA_PKT_TYPE
276 } VIA_PKT_TYPE, *PVIA_PKT_TYPE;
283 #define NDIS_STATUS int
286 typedef enum __DEVICE_NDIS_STATUS {
291 } DEVICE_NDIS_STATUS, *PDEVICE_NDIS_STATUS;
294 #define MAX_BSSIDINFO_4_PMKID 16
295 #define MAX_PMKIDLIST 5
296 //Flags for PMKID Candidate list structure
297 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
300 typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
303 typedef enum _NDIS_802_11_WEP_STATUS
305 Ndis802_11WEPEnabled,
306 Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled,
307 Ndis802_11WEPDisabled,
308 Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled,
309 Ndis802_11WEPKeyAbsent,
310 Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent,
311 Ndis802_11WEPNotSupported,
312 Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported,
313 Ndis802_11Encryption2Enabled,
314 Ndis802_11Encryption2KeyAbsent,
315 Ndis802_11Encryption3Enabled,
316 Ndis802_11Encryption3KeyAbsent
317 } NDIS_802_11_WEP_STATUS, *PNDIS_802_11_WEP_STATUS,
318 NDIS_802_11_ENCRYPTION_STATUS, *PNDIS_802_11_ENCRYPTION_STATUS;
321 typedef enum _NDIS_802_11_STATUS_TYPE
323 Ndis802_11StatusType_Authentication,
324 Ndis802_11StatusType_MediaStreamMode,
325 Ndis802_11StatusType_PMKID_CandidateList,
326 Ndis802_11StatusTypeMax // not a real type, defined as an upper bound
327 } NDIS_802_11_STATUS_TYPE, *PNDIS_802_11_STATUS_TYPE;
329 //Added new types for PMKID Candidate lists.
330 typedef struct _PMKID_CANDIDATE {
331 NDIS_802_11_MAC_ADDRESS BSSID;
333 } PMKID_CANDIDATE, *PPMKID_CANDIDATE;
336 typedef struct _BSSID_INFO
338 NDIS_802_11_MAC_ADDRESS BSSID;
339 NDIS_802_11_PMKID_VALUE PMKID;
340 } BSSID_INFO, *PBSSID_INFO;
342 typedef struct tagSPMKID {
344 ULONG BSSIDInfoCount;
345 BSSID_INFO BSSIDInfo[MAX_BSSIDINFO_4_PMKID];
348 typedef struct tagSPMKIDCandidateEvent {
349 NDIS_802_11_STATUS_TYPE StatusType;
350 ULONG Version; // Version of the structure
351 ULONG NumCandidates; // No. of pmkid candidates
352 PMKID_CANDIDATE CandidateList[MAX_PMKIDLIST];
353 } SPMKIDCandidateEvent, *PSPMKIDCandidateEvent;
358 #define MAX_QUIET_COUNT 8
360 typedef struct tagSQuietControl {
365 } SQuietControl, *PSQuietControl;
370 // The receive duplicate detection cache entry
371 typedef struct tagSCacheEntry{
373 BYTE abyAddr2[U_ETHER_ADDR_LEN];
375 } SCacheEntry, *PSCacheEntry;
377 typedef struct tagSCache{
378 /* The receive cache is updated circularly. The next entry to be written is
379 * indexed by the "InPtr".
381 UINT uInPtr; // Place to use next
382 SCacheEntry asCacheEntry[DUPLICATE_RX_CACHE_LENGTH];
385 #define CB_MAX_RX_FRAG 64
386 // DeFragment Control Block, used for collecting fragments prior to reassembly
387 typedef struct tagSDeFragControlBlock
391 BYTE abyAddr2[U_ETHER_ADDR_LEN];
397 } SDeFragControlBlock, *PSDeFragControlBlock;
402 #define DEVICE_FLAGS_UNPLUG 0x00000001UL
403 #define DEVICE_FLAGS_PREAMBLE_TYPE 0x00000002UL
404 #define DEVICE_FLAGS_OP_MODE 0x00000004UL
405 #define DEVICE_FLAGS_PS_MODE 0x00000008UL
406 #define DEVICE_FLAGS_80211h_MODE 0x00000010UL
408 //flags for driver status
409 #define DEVICE_FLAGS_OPENED 0x00010000UL
410 #define DEVICE_FLAGS_WOL_ENABLED 0x00080000UL
411 //flags for capbilities
412 #define DEVICE_FLAGS_TX_ALIGN 0x01000000UL
413 #define DEVICE_FLAGS_HAVE_CAM 0x02000000UL
414 #define DEVICE_FLAGS_FLOW_CTRL 0x04000000UL
416 //flags for MII status
417 #define DEVICE_LINK_FAIL 0x00000001UL
418 #define DEVICE_SPEED_10 0x00000002UL
419 #define DEVICE_SPEED_100 0x00000004UL
420 #define DEVICE_SPEED_1000 0x00000008UL
421 #define DEVICE_DUPLEX_FULL 0x00000010UL
422 #define DEVICE_AUTONEG_ENABLE 0x00000020UL
423 #define DEVICE_FORCED_BY_EEPROM 0x00000040UL
424 //for device_set_media_duplex
425 #define DEVICE_LINK_CHANGE 0x00000001UL
428 typedef struct __device_opt {
429 int nRxDescs0; //Number of RX descriptors0
430 int nTxDescs0; //Number of TX descriptors 0, 1, 2, 3
431 int rts_thresh; //rts threshold
440 } OPTIONS, *POPTIONS;
443 typedef struct __device_info {
446 struct usb_device* usb;
447 struct net_device* dev;
448 struct net_device_stats stats;
452 struct tasklet_struct CmdWorkItem;
453 struct tasklet_struct EventWorkItem;
454 struct tasklet_struct ReadWorkItem;
455 struct tasklet_struct RxMngWorkItem;
472 SDeFragControlBlock sRxDFCB[CB_MAX_RX_FRAG];
475 UINT uCurrentDFCBIdx;
479 struct urb *pControlURB;
480 struct urb *pInterruptURB;
481 struct usb_ctrlrequest sUsbCtlRequest;
485 // Variables to track resources for the BULK In Pipe
488 PRCB apRCB[CB_MAX_RX_DESC];
490 PRCB FirstRecvFreeList;
491 PRCB LastRecvFreeList;
492 UINT NumRecvFreeList;
493 PRCB FirstRecvMngList;
494 PRCB LastRecvMngList;
496 BOOL bIsRxWorkItemQueued;
497 BOOL bIsRxMngWorkItemQueued;
498 ULONG ulRcvRefCount; // number of packets that have not been returned back
501 // Variables to track resources for the BULK Out Pipe
504 PUSB_SEND_CONTEXT apTD[CB_MAX_TX_DESC];
508 // Variables to track resources for the Interript In Pipe
511 BOOL fKillEventPollingThread;
512 BOOL bEventAvailable;
515 //default config from file by user setting
516 DEFAULT_CONFIG config_file;
521 // protect with spinlock
522 ULONG ulBulkInPosted;
524 ULONG ulBulkInContCRCError;
525 ULONG ulBulkInBytesRead;
527 ULONG ulBulkOutPosted;
528 ULONG ulBulkOutError;
529 ULONG ulBulkOutContCRCError;
530 ULONG ulBulkOutBytesWrite;
534 ULONG ulIntInContCRCError;
535 ULONG ulIntInBytesRead;
539 WORD wFirmwareVersion;
548 BYTE byOriginalZonetype;
550 BOOL bLinkPass; // link status: OK or fail
551 BYTE abyCurrentNetAddr[U_ETHER_ADDR_LEN];
552 BYTE abyPermanentNetAddr[U_ETHER_ADDR_LEN];
553 // SW network address
554 // BYTE abySoftwareNetAddr[U_ETHER_ADDR_LEN];
555 BOOL bExistSWNetAddr;
557 // Adapter statistics
558 SStatCounter scStatistic;
560 SDot11Counters s802_11Counter;
563 // Maintain statistical debug info.
565 ULONG packetsReceived;
566 ULONG packetsReceivedDropped;
567 ULONG packetsReceivedOverflow;
569 ULONG packetsSentDropped;
570 ULONG SendContextsInUse;
571 ULONG RcvBuffersInUse;
575 SMgmtObject sMgmtObj;
581 // 802.11 MAC specific
588 DWORD dwRxAntennaSel;
589 DWORD dwTxAntennaSel;
591 BYTE byRxAntennaMode;
592 BYTE byTxAntennaMode;
596 //SQ3 functions for antenna diversity
597 struct timer_list TimerSQ3Tmax1;
598 struct timer_list TimerSQ3Tmax2;
599 struct timer_list TimerSQ3Tmax3;
601 BOOL bDiversityRegCtlON;
602 BOOL bDiversityEnable;
603 ULONG ulDiversityNValue;
604 ULONG ulDiversityMValue;
612 ULONG ulRatio_State0;
613 ULONG ulRatio_State1;
617 ULONG aulSQ3Val[MAX_RATE];
618 ULONG aulPktNum[MAX_RATE];
621 UINT uSIFS; //Current SIFS
622 UINT uDIFS; //Current DIFS
623 UINT uEIFS; //Current EIFS
624 UINT uSlot; //Current SlotTime
625 UINT uCwMin; //Current CwMin
626 UINT uCwMax; //CwMax is fixed on 1023.
635 VIA_BB_TYPE byBBType; //0: 11A, 1:11B, 2:11G
636 VIA_PKT_TYPE byPacketType; //0:11a,1:11b,2:11gb(only CCK in BasicRate),3:11ga(OFDM in Basic Rate)
639 BYTE byTopOFDMBasicRate;
640 BYTE byTopCCKBasicRate;
643 DWORD dwAotoRateTxOkCnt;
644 DWORD dwAotoRateTxFailCnt;
645 DWORD dwErrorRateThreshold[13];
646 DWORD dwTPTable[MAX_RATE];
647 BYTE abyEEPROM[EEP_MAX_CONTEXT_SIZE]; //DWORD alignment
651 UINT uConnectionRate;
654 BYTE byShortPreamble;
658 // For RF Power table
663 BYTE abyCCKPwrTbl[14];
664 BYTE abyOFDMPwrTbl[14];
665 BYTE abyOFDMAPwrTbl[42];
669 WORD wFragmentationThreshold;
670 BYTE byShortRetryLimit;
671 BYTE byLongRetryLimit;
672 CARD_OP_MODE eOPMode;
674 WORD wMaxTransmitMSDULifetime;
675 BYTE abyBSSID[U_ETHER_ADDR_LEN];
676 BYTE abyDesireBSSID[U_ETHER_ADDR_LEN];
677 WORD wCTSDuration; // update while speed change
678 WORD wACKDuration; // update while speed change
679 WORD wRTSTransmitLen; // update while speed change
680 BYTE byRTSServiceField; // update while speed change
681 BYTE byRTSSignalField; // update while speed change
683 DWORD dwMaxReceiveLifetime; // dot11MaxReceiveLifetime
686 BOOL bEncryptionEnable;
688 BOOL bSoftwareGenCrcErr;
692 BOOL bBarkerPreambleMd;
695 WORD wUseProtectCntDown;
697 BOOL bRadioControlOff;
702 WORD wListenInterval;
704 WMAC_POWER_MODE ePSMode;
705 ULONG ulPSModeWaitTx;
710 BOOL bBeaconBufReady;
716 CMD_STATE eCommandState;
725 UINT uAutoReConnectTime;
730 CMD_ITEM eCmdQueue[CMD_Q_SIZE];
738 BOOL bEnableRoaming; //DavidWang
739 BOOL bIsRoaming; //DavidWang
740 BOOL bFastRoaming; //DavidWang
741 BYTE bSameBSSMaxNum; //Davidwang
742 BYTE bSameBSSCurNum; //DavidWang
748 NDIS_802_11_WEP_STATUS eEncryptionStatus;
751 //2007-0925-01<Add>by MikeLiu
752 //mike add :save old Encryption
753 NDIS_802_11_WEP_STATUS eOldEncryptionStatus;
760 BYTE abyPRNG[WLAN_WEPMAX_KEYLEN+3];
767 BYTE abyKey[WLAN_WEP232_KEYLEN];
783 // For Update BaseBand VGA Gain Offset
785 UINT uBBVGADiffCount;
788 BYTE abyBBVGA[BB_VGA_LEVEL];
789 LONG ldBmThreshold[BB_VGA_LEVEL];
796 DWORD dwDiagRefCount;
801 // For Auto Power Tunning
803 BYTE byAutoPwrTunning;
805 // BaseBand Loopback Use
812 struct timer_list sTimerCommand;
814 //2007-0115-01<Add>by MikeLiu
816 struct timer_list sTimerTxData;
817 ULONG nTxDataTimeCout;
819 BOOL IsTxDataTrigger;
822 #ifdef WPA_SM_Transtatus
823 BOOL fWPA_Authened; //is WPA/WPA-PSK or WPA2/WPA2-PSK authen??
825 BYTE byReAssocCount; //mike add:re-association retry times!
826 BYTE byLinkWaitCount;
828 SEthernetHeader sTxEthHeader;
829 SEthernetHeader sRxEthHeader;
830 BYTE abyBroadcastAddr[U_ETHER_ADDR_LEN];
831 BYTE abySNAP_RFC1042[U_ETHER_ADDR_LEN];
832 BYTE abySNAP_Bridgetunnel[U_ETHER_ADDR_LEN];
834 // Pre-Authentication & PMK cache
836 SPMKIDCandidateEvent gsPMKIDCandidate;
844 BYTE byChannelSwitchCount;
846 //WPA supplicant daemon
847 struct net_device *wpadev;
852 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
857 BOOL bWPASuppWextEnabled;
861 // user space daemon: hostapd, is used for HOSTAP
865 struct net_device *apdev;
866 int (*tx_80211)(struct sk_buff *skb, struct net_device *dev);
870 struct iw_statistics wstats; // wireless stats
873 } DEVICE_INFO, *PSDevice;
878 #define EnqueueRCB(_Head, _Tail, _RCB) \
884 _Tail->Next = _RCB; \
890 #define DequeueRCB(Head, Tail) \
900 #define ADD_ONE_WITH_WRAP_AROUND(uVar, uModulo) { \
901 if ((uVar) >= ((uModulo) - 1)) \
908 #define fMP_RESET_IN_PROGRESS 0x00000001
909 #define fMP_DISCONNECTED 0x00000002
910 #define fMP_HALT_IN_PROGRESS 0x00000004
911 #define fMP_SURPRISE_REMOVED 0x00000008
912 #define fMP_RECV_LOOKASIDE 0x00000010
913 #define fMP_INIT_IN_PROGRESS 0x00000020
914 #define fMP_SEND_SIDE_RESOURCE_ALLOCATED 0x00000040
915 #define fMP_RECV_SIDE_RESOURCE_ALLOCATED 0x00000080
916 #define fMP_POST_READS 0x00000100
917 #define fMP_POST_WRITES 0x00000200
918 #define fMP_CONTROL_READS 0x00000400
919 #define fMP_CONTROL_WRITES 0x00000800
923 #define MP_SET_FLAG(_M, _F) ((_M)->Flags |= (_F))
924 #define MP_CLEAR_FLAG(_M, _F) ((_M)->Flags &= ~(_F))
925 #define MP_TEST_FLAG(_M, _F) (((_M)->Flags & (_F)) != 0)
926 #define MP_TEST_FLAGS(_M, _F) (((_M)->Flags & (_F)) == (_F))
928 #define MP_IS_READY(_M) (((_M)->Flags & \
929 (fMP_DISCONNECTED | fMP_RESET_IN_PROGRESS | fMP_HALT_IN_PROGRESS | fMP_INIT_IN_PROGRESS | fMP_SURPRISE_REMOVED)) == 0)
931 /*--------------------- Export Functions --------------------------*/
933 //BOOL device_dma0_xmit(PSDevice pDevice, struct sk_buff *skb, UINT uNodeIndex);
934 BOOL device_alloc_frag_buf(PSDevice pDevice, PSDeFragControlBlock pDeF);