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
92 #include "device_cfg.h"
105 /*--------------------- Export Definitions -------------------------*/
106 #define VNT_USB_VENDOR_ID 0x160a
107 #define VNT_USB_PRODUCT_ID 0x3184
109 #define MAC_MAX_CONTEXT_REG (256+128)
111 #define MAX_MULTICAST_ADDRESS_NUM 32
112 #define MULTICAST_ADDRESS_LIST_SIZE (MAX_MULTICAST_ADDRESS_NUM * U_ETHER_ADDR_LEN)
115 //#define OP_MODE_INFRASTRUCTURE 0
116 //#define OP_MODE_ADHOC 1
117 //#define OP_MODE_AP 2
119 #define DUPLICATE_RX_CACHE_LENGTH 5
121 #define NUM_KEY_ENTRY 11
123 #define TX_WEP_NONE 0
126 #define TX_WEP_SWOTP 3
127 #define TX_WEP_OTPSW 4
128 #define TX_WEP_SW232 5
130 #define KEYSEL_WEP40 0
131 #define KEYSEL_WEP104 1
132 #define KEYSEL_TKIP 2
133 #define KEYSEL_CCMP 3
137 #define AUTO_FB_NONE 0
147 #define ANT_DIVERSITY 2
148 #define ANT_RXD_TXA 3
149 #define ANT_RXD_TXB 4
150 #define ANT_UNKNOWN 0xFF
157 #define MAXCHECKHANGCNT 4
160 #define TX_PKT_UNI 0x00
161 #define TX_PKT_MULTI 0x01
162 #define TX_PKT_BROAD 0x02
164 #define BB_VGA_LEVEL 4
165 #define BB_VGA_CHANGE_THRESHOLD 3
170 #define RUN_AT(x) (jiffies+(x))
174 #define RESERV_AC0DMA 4
176 #define PRIVATE_Message 0
178 /*--------------------- Export Types ------------------------------*/
180 #define DBG_PRT(l, p, args...) {if (l<=msglevel) printk( p ,##args);}
181 #define PRINT_K(p, args...) {if (PRIVATE_Message) printk( p ,##args);}
183 typedef enum __device_msg_level {
184 MSG_LEVEL_ERR=0, //Errors that will cause abnormal operation.
185 MSG_LEVEL_NOTICE=1, //Some errors need users to be notified.
186 MSG_LEVEL_INFO=2, //Normal message.
187 MSG_LEVEL_VERBOSE=3, //Will report all trival errors.
188 MSG_LEVEL_DEBUG=4 //Only for debug purpose.
189 } DEVICE_MSG_LEVEL, *PDEVICE_MSG_LEVEL;
191 typedef enum __device_init_type {
192 DEVICE_INIT_COLD=0, // cold init
193 DEVICE_INIT_RESET, // reset init or Dx to D0 power remain init
194 DEVICE_INIT_DXPL // Dx to D0 power lost init
195 } DEVICE_INIT_TYPE, *PDEVICE_INIT_TYPE;
201 // Enum of context types for SendPacket
203 typedef enum _CONTEXT_TYPE {
204 CONTEXT_DATA_PACKET = 1,
211 // RCB (Receive Control Block)
218 SRxMgmtPacket sMngPacket;
225 // used to track bulk out irps
226 typedef struct _USB_SEND_CONTEXT {
228 struct sk_buff *pPacket;
232 SEthernetHeader sEthHeader;
235 UCHAR Data[MAX_TOTAL_SIZE_WITH_ALL_HEADERS];
236 } USB_SEND_CONTEXT, *PUSB_SEND_CONTEXT;
239 //structure got from configuration file as user desired default setting.
240 typedef struct _DEFAULT_CONFIG{
243 INT eAuthenMode; //open/wep/wpa
244 INT bShareKeyAlgorithm; //open-open/open-sharekey/wep-sharekey
245 INT keyidx; //wepkey index
246 INT eEncryptionStatus;
248 }DEFAULT_CONFIG,*PDEFAULT_CONFIG;
251 // Structure to keep track of usb interrupt packets
258 } INT_BUFFER, *PINT_BUFFER;
263 typedef enum _VIA_BB_TYPE
268 } VIA_BB_TYPE, *PVIA_BB_TYPE;
270 //0:11a,1:11b,2:11gb(only CCK in BasicRate),3:11ga(OFDM in Basic Rate)
271 typedef enum _VIA_PKT_TYPE
277 } VIA_PKT_TYPE, *PVIA_PKT_TYPE;
284 #define NDIS_STATUS int
287 typedef enum __DEVICE_NDIS_STATUS {
292 } DEVICE_NDIS_STATUS, *PDEVICE_NDIS_STATUS;
295 #define MAX_BSSIDINFO_4_PMKID 16
296 #define MAX_PMKIDLIST 5
297 //Flags for PMKID Candidate list structure
298 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
301 typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
304 typedef enum _NDIS_802_11_WEP_STATUS
306 Ndis802_11WEPEnabled,
307 Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled,
308 Ndis802_11WEPDisabled,
309 Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled,
310 Ndis802_11WEPKeyAbsent,
311 Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent,
312 Ndis802_11WEPNotSupported,
313 Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported,
314 Ndis802_11Encryption2Enabled,
315 Ndis802_11Encryption2KeyAbsent,
316 Ndis802_11Encryption3Enabled,
317 Ndis802_11Encryption3KeyAbsent
318 } NDIS_802_11_WEP_STATUS, *PNDIS_802_11_WEP_STATUS,
319 NDIS_802_11_ENCRYPTION_STATUS, *PNDIS_802_11_ENCRYPTION_STATUS;
322 typedef enum _NDIS_802_11_STATUS_TYPE
324 Ndis802_11StatusType_Authentication,
325 Ndis802_11StatusType_MediaStreamMode,
326 Ndis802_11StatusType_PMKID_CandidateList,
327 Ndis802_11StatusTypeMax // not a real type, defined as an upper bound
328 } NDIS_802_11_STATUS_TYPE, *PNDIS_802_11_STATUS_TYPE;
330 //Added new types for PMKID Candidate lists.
331 typedef struct _PMKID_CANDIDATE {
332 NDIS_802_11_MAC_ADDRESS BSSID;
334 } PMKID_CANDIDATE, *PPMKID_CANDIDATE;
337 typedef struct _BSSID_INFO
339 NDIS_802_11_MAC_ADDRESS BSSID;
340 NDIS_802_11_PMKID_VALUE PMKID;
341 } BSSID_INFO, *PBSSID_INFO;
343 typedef struct tagSPMKID {
345 ULONG BSSIDInfoCount;
346 BSSID_INFO BSSIDInfo[MAX_BSSIDINFO_4_PMKID];
349 typedef struct tagSPMKIDCandidateEvent {
350 NDIS_802_11_STATUS_TYPE StatusType;
351 ULONG Version; // Version of the structure
352 ULONG NumCandidates; // No. of pmkid candidates
353 PMKID_CANDIDATE CandidateList[MAX_PMKIDLIST];
354 } SPMKIDCandidateEvent, *PSPMKIDCandidateEvent;
359 #define MAX_QUIET_COUNT 8
361 typedef struct tagSQuietControl {
366 } SQuietControl, *PSQuietControl;
371 // The receive duplicate detection cache entry
372 typedef struct tagSCacheEntry{
374 BYTE abyAddr2[U_ETHER_ADDR_LEN];
376 } SCacheEntry, *PSCacheEntry;
378 typedef struct tagSCache{
379 /* The receive cache is updated circularly. The next entry to be written is
380 * indexed by the "InPtr".
382 UINT uInPtr; // Place to use next
383 SCacheEntry asCacheEntry[DUPLICATE_RX_CACHE_LENGTH];
386 #define CB_MAX_RX_FRAG 64
387 // DeFragment Control Block, used for collecting fragments prior to reassembly
388 typedef struct tagSDeFragControlBlock
392 BYTE abyAddr2[U_ETHER_ADDR_LEN];
398 } SDeFragControlBlock, *PSDeFragControlBlock;
403 #define DEVICE_FLAGS_UNPLUG 0x00000001UL
404 #define DEVICE_FLAGS_PREAMBLE_TYPE 0x00000002UL
405 #define DEVICE_FLAGS_OP_MODE 0x00000004UL
406 #define DEVICE_FLAGS_PS_MODE 0x00000008UL
407 #define DEVICE_FLAGS_80211h_MODE 0x00000010UL
409 //flags for driver status
410 #define DEVICE_FLAGS_OPENED 0x00010000UL
411 #define DEVICE_FLAGS_WOL_ENABLED 0x00080000UL
412 //flags for capbilities
413 #define DEVICE_FLAGS_TX_ALIGN 0x01000000UL
414 #define DEVICE_FLAGS_HAVE_CAM 0x02000000UL
415 #define DEVICE_FLAGS_FLOW_CTRL 0x04000000UL
417 //flags for MII status
418 #define DEVICE_LINK_FAIL 0x00000001UL
419 #define DEVICE_SPEED_10 0x00000002UL
420 #define DEVICE_SPEED_100 0x00000004UL
421 #define DEVICE_SPEED_1000 0x00000008UL
422 #define DEVICE_DUPLEX_FULL 0x00000010UL
423 #define DEVICE_AUTONEG_ENABLE 0x00000020UL
424 #define DEVICE_FORCED_BY_EEPROM 0x00000040UL
425 //for device_set_media_duplex
426 #define DEVICE_LINK_CHANGE 0x00000001UL
429 typedef struct __device_opt {
430 int nRxDescs0; //Number of RX descriptors0
431 int nTxDescs0; //Number of TX descriptors 0, 1, 2, 3
432 int rts_thresh; //rts threshold
441 } OPTIONS, *POPTIONS;
444 typedef struct __device_info {
447 struct usb_device* usb;
448 struct net_device* dev;
449 struct net_device_stats stats;
453 struct tasklet_struct CmdWorkItem;
454 struct tasklet_struct EventWorkItem;
455 struct tasklet_struct ReadWorkItem;
456 struct tasklet_struct RxMngWorkItem;
473 SDeFragControlBlock sRxDFCB[CB_MAX_RX_FRAG];
476 UINT uCurrentDFCBIdx;
480 struct urb *pControlURB;
481 struct urb *pInterruptURB;
482 struct usb_ctrlrequest sUsbCtlRequest;
486 // Variables to track resources for the BULK In Pipe
489 PRCB apRCB[CB_MAX_RX_DESC];
491 PRCB FirstRecvFreeList;
492 PRCB LastRecvFreeList;
493 UINT NumRecvFreeList;
494 PRCB FirstRecvMngList;
495 PRCB LastRecvMngList;
497 BOOL bIsRxWorkItemQueued;
498 BOOL bIsRxMngWorkItemQueued;
499 ULONG ulRcvRefCount; // number of packets that have not been returned back
502 // Variables to track resources for the BULK Out Pipe
505 PUSB_SEND_CONTEXT apTD[CB_MAX_TX_DESC];
509 // Variables to track resources for the Interript In Pipe
512 BOOL fKillEventPollingThread;
513 BOOL bEventAvailable;
516 //default config from file by user setting
517 DEFAULT_CONFIG config_file;
522 // protect with spinlock
523 ULONG ulBulkInPosted;
525 ULONG ulBulkInContCRCError;
526 ULONG ulBulkInBytesRead;
528 ULONG ulBulkOutPosted;
529 ULONG ulBulkOutError;
530 ULONG ulBulkOutContCRCError;
531 ULONG ulBulkOutBytesWrite;
535 ULONG ulIntInContCRCError;
536 ULONG ulIntInBytesRead;
540 WORD wFirmwareVersion;
549 BYTE byOriginalZonetype;
551 BOOL bLinkPass; // link status: OK or fail
552 BYTE abyCurrentNetAddr[U_ETHER_ADDR_LEN];
553 BYTE abyPermanentNetAddr[U_ETHER_ADDR_LEN];
554 // SW network address
555 // BYTE abySoftwareNetAddr[U_ETHER_ADDR_LEN];
556 BOOL bExistSWNetAddr;
558 // Adapter statistics
559 SStatCounter scStatistic;
561 SDot11Counters s802_11Counter;
564 // Maintain statistical debug info.
566 ULONG packetsReceived;
567 ULONG packetsReceivedDropped;
568 ULONG packetsReceivedOverflow;
570 ULONG packetsSentDropped;
571 ULONG SendContextsInUse;
572 ULONG RcvBuffersInUse;
576 SMgmtObject sMgmtObj;
582 // 802.11 MAC specific
589 DWORD dwRxAntennaSel;
590 DWORD dwTxAntennaSel;
592 BYTE byRxAntennaMode;
593 BYTE byTxAntennaMode;
597 //SQ3 functions for antenna diversity
598 struct timer_list TimerSQ3Tmax1;
599 struct timer_list TimerSQ3Tmax2;
600 struct timer_list TimerSQ3Tmax3;
602 BOOL bDiversityRegCtlON;
603 BOOL bDiversityEnable;
604 ULONG ulDiversityNValue;
605 ULONG ulDiversityMValue;
613 ULONG ulRatio_State0;
614 ULONG ulRatio_State1;
618 ULONG aulSQ3Val[MAX_RATE];
619 ULONG aulPktNum[MAX_RATE];
622 UINT uSIFS; //Current SIFS
623 UINT uDIFS; //Current DIFS
624 UINT uEIFS; //Current EIFS
625 UINT uSlot; //Current SlotTime
626 UINT uCwMin; //Current CwMin
627 UINT uCwMax; //CwMax is fixed on 1023.
636 VIA_BB_TYPE byBBType; //0: 11A, 1:11B, 2:11G
637 VIA_PKT_TYPE byPacketType; //0:11a,1:11b,2:11gb(only CCK in BasicRate),3:11ga(OFDM in Basic Rate)
640 BYTE byTopOFDMBasicRate;
641 BYTE byTopCCKBasicRate;
644 DWORD dwAotoRateTxOkCnt;
645 DWORD dwAotoRateTxFailCnt;
646 DWORD dwErrorRateThreshold[13];
647 DWORD dwTPTable[MAX_RATE];
648 BYTE abyEEPROM[EEP_MAX_CONTEXT_SIZE]; //DWORD alignment
652 UINT uConnectionRate;
655 BYTE byShortPreamble;
659 // For RF Power table
664 BYTE abyCCKPwrTbl[14];
665 BYTE abyOFDMPwrTbl[14];
666 BYTE abyOFDMAPwrTbl[42];
670 WORD wFragmentationThreshold;
671 BYTE byShortRetryLimit;
672 BYTE byLongRetryLimit;
673 CARD_OP_MODE eOPMode;
675 WORD wMaxTransmitMSDULifetime;
676 BYTE abyBSSID[U_ETHER_ADDR_LEN];
677 BYTE abyDesireBSSID[U_ETHER_ADDR_LEN];
678 WORD wCTSDuration; // update while speed change
679 WORD wACKDuration; // update while speed change
680 WORD wRTSTransmitLen; // update while speed change
681 BYTE byRTSServiceField; // update while speed change
682 BYTE byRTSSignalField; // update while speed change
684 DWORD dwMaxReceiveLifetime; // dot11MaxReceiveLifetime
687 BOOL bEncryptionEnable;
689 BOOL bSoftwareGenCrcErr;
693 BOOL bBarkerPreambleMd;
696 WORD wUseProtectCntDown;
698 BOOL bRadioControlOff;
703 WORD wListenInterval;
705 WMAC_POWER_MODE ePSMode;
706 ULONG ulPSModeWaitTx;
711 BOOL bBeaconBufReady;
717 CMD_STATE eCommandState;
726 UINT uAutoReConnectTime;
731 CMD_ITEM eCmdQueue[CMD_Q_SIZE];
739 BOOL bEnableRoaming; //DavidWang
740 BOOL bIsRoaming; //DavidWang
741 BOOL bFastRoaming; //DavidWang
742 BYTE bSameBSSMaxNum; //Davidwang
743 BYTE bSameBSSCurNum; //DavidWang
749 NDIS_802_11_WEP_STATUS eEncryptionStatus;
752 //2007-0925-01<Add>by MikeLiu
753 //mike add :save old Encryption
754 NDIS_802_11_WEP_STATUS eOldEncryptionStatus;
761 BYTE abyPRNG[WLAN_WEPMAX_KEYLEN+3];
768 BYTE abyKey[WLAN_WEP232_KEYLEN];
784 // For Update BaseBand VGA Gain Offset
786 UINT uBBVGADiffCount;
789 BYTE abyBBVGA[BB_VGA_LEVEL];
790 LONG ldBmThreshold[BB_VGA_LEVEL];
797 DWORD dwDiagRefCount;
802 // For Auto Power Tunning
804 BYTE byAutoPwrTunning;
806 // BaseBand Loopback Use
813 struct timer_list sTimerCommand;
815 //2007-0115-01<Add>by MikeLiu
817 struct timer_list sTimerTxData;
818 ULONG nTxDataTimeCout;
820 BOOL IsTxDataTrigger;
823 #ifdef WPA_SM_Transtatus
824 BOOL fWPA_Authened; //is WPA/WPA-PSK or WPA2/WPA2-PSK authen??
826 BYTE byReAssocCount; //mike add:re-association retry times!
827 BYTE byLinkWaitCount;
829 SEthernetHeader sTxEthHeader;
830 SEthernetHeader sRxEthHeader;
831 BYTE abyBroadcastAddr[U_ETHER_ADDR_LEN];
832 BYTE abySNAP_RFC1042[U_ETHER_ADDR_LEN];
833 BYTE abySNAP_Bridgetunnel[U_ETHER_ADDR_LEN];
835 // Pre-Authentication & PMK cache
837 SPMKIDCandidateEvent gsPMKIDCandidate;
845 BYTE byChannelSwitchCount;
847 //WPA supplicant daemon
848 struct net_device *wpadev;
853 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
858 BOOL bWPASuppWextEnabled;
862 // user space daemon: hostapd, is used for HOSTAP
866 struct net_device *apdev;
867 int (*tx_80211)(struct sk_buff *skb, struct net_device *dev);
871 struct iw_statistics wstats; // wireless stats
874 } DEVICE_INFO, *PSDevice;
879 #define EnqueueRCB(_Head, _Tail, _RCB) \
885 _Tail->Next = _RCB; \
891 #define DequeueRCB(Head, Tail) \
901 #define ADD_ONE_WITH_WRAP_AROUND(uVar, uModulo) { \
902 if ((uVar) >= ((uModulo) - 1)) \
909 #define fMP_RESET_IN_PROGRESS 0x00000001
910 #define fMP_DISCONNECTED 0x00000002
911 #define fMP_HALT_IN_PROGRESS 0x00000004
912 #define fMP_SURPRISE_REMOVED 0x00000008
913 #define fMP_RECV_LOOKASIDE 0x00000010
914 #define fMP_INIT_IN_PROGRESS 0x00000020
915 #define fMP_SEND_SIDE_RESOURCE_ALLOCATED 0x00000040
916 #define fMP_RECV_SIDE_RESOURCE_ALLOCATED 0x00000080
917 #define fMP_POST_READS 0x00000100
918 #define fMP_POST_WRITES 0x00000200
919 #define fMP_CONTROL_READS 0x00000400
920 #define fMP_CONTROL_WRITES 0x00000800
924 #define MP_SET_FLAG(_M, _F) ((_M)->Flags |= (_F))
925 #define MP_CLEAR_FLAG(_M, _F) ((_M)->Flags &= ~(_F))
926 #define MP_TEST_FLAG(_M, _F) (((_M)->Flags & (_F)) != 0)
927 #define MP_TEST_FLAGS(_M, _F) (((_M)->Flags & (_F)) == (_F))
929 #define MP_IS_READY(_M) (((_M)->Flags & \
930 (fMP_DISCONNECTED | fMP_RESET_IN_PROGRESS | fMP_HALT_IN_PROGRESS | fMP_INIT_IN_PROGRESS | fMP_SURPRISE_REMOVED)) == 0)
932 /*--------------------- Export Functions --------------------------*/
934 //BOOL device_dma0_xmit(PSDevice pDevice, struct sk_buff *skb, UINT uNodeIndex);
935 BOOL device_alloc_frag_buf(PSDevice pDevice, PSDeFragControlBlock pDeF);