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)
112 //#define OP_MODE_INFRASTRUCTURE 0
113 //#define OP_MODE_ADHOC 1
114 //#define OP_MODE_AP 2
116 #define DUPLICATE_RX_CACHE_LENGTH 5
118 #define NUM_KEY_ENTRY 11
120 #define TX_WEP_NONE 0
123 #define TX_WEP_SWOTP 3
124 #define TX_WEP_OTPSW 4
125 #define TX_WEP_SW232 5
127 #define KEYSEL_WEP40 0
128 #define KEYSEL_WEP104 1
129 #define KEYSEL_TKIP 2
130 #define KEYSEL_CCMP 3
132 #define AUTO_FB_NONE 0
142 #define ANT_DIVERSITY 2
143 #define ANT_RXD_TXA 3
144 #define ANT_RXD_TXB 4
145 #define ANT_UNKNOWN 0xFF
152 #define MAXCHECKHANGCNT 4
155 #define TX_PKT_UNI 0x00
156 #define TX_PKT_MULTI 0x01
157 #define TX_PKT_BROAD 0x02
159 #define BB_VGA_LEVEL 4
160 #define BB_VGA_CHANGE_THRESHOLD 3
163 #define RUN_AT(x) (jiffies+(x))
167 #define RESERV_AC0DMA 4
169 #define PRIVATE_Message 0
171 /*--------------------- Export Types ------------------------------*/
173 #define DBG_PRT(l, p, args...) { if (l <= msglevel) printk(p, ##args); }
174 #define PRINT_K(p, args...) { if (PRIVATE_Message) printk(p, ##args); }
176 typedef enum __device_msg_level {
177 MSG_LEVEL_ERR = 0, /* Errors causing abnormal operation */
178 MSG_LEVEL_NOTICE = 1, /* Errors needing user notification */
179 MSG_LEVEL_INFO = 2, /* Normal message. */
180 MSG_LEVEL_VERBOSE = 3, /* Will report all trival errors. */
181 MSG_LEVEL_DEBUG = 4 /* Only for debug purpose. */
182 } DEVICE_MSG_LEVEL, *PDEVICE_MSG_LEVEL;
184 typedef enum __device_init_type {
185 DEVICE_INIT_COLD = 0, /* cold init */
186 DEVICE_INIT_RESET, /* reset init or Dx to D0 power remain */
187 DEVICE_INIT_DXPL /* Dx to D0 power lost init */
188 } DEVICE_INIT_TYPE, *PDEVICE_INIT_TYPE;
193 // Enum of context types for SendPacket
195 typedef enum _CONTEXT_TYPE {
196 CONTEXT_DATA_PACKET = 1,
200 // RCB (Receive Control Block)
207 SRxMgmtPacket sMngPacket;
213 // used to track bulk out irps
214 typedef struct _USB_SEND_CONTEXT {
216 struct sk_buff *pPacket;
218 unsigned int uBufLen;
220 SEthernetHeader sEthHeader;
223 unsigned char Data[MAX_TOTAL_SIZE_WITH_ALL_HEADERS];
224 } USB_SEND_CONTEXT, *PUSB_SEND_CONTEXT;
226 /* structure got from configuration file as user-desired default settings */
227 typedef struct _DEFAULT_CONFIG {
229 signed int eConfigMode;
230 signed int eAuthenMode; /* open/wep/wpa */
231 signed int bShareKeyAlgorithm; /* open-open/{open,wep}-sharekey */
232 signed int keyidx; /* wepkey index */
233 signed int eEncryptionStatus;
234 } DEFAULT_CONFIG, *PDEFAULT_CONFIG;
237 // Structure to keep track of usb interrupt packets
240 unsigned int uDataLen;
244 } INT_BUFFER, *PINT_BUFFER;
247 typedef enum _VIA_BB_TYPE
252 } VIA_BB_TYPE, *PVIA_BB_TYPE;
254 //0:11a,1:11b,2:11gb(only CCK in BasicRate),3:11ga(OFDM in Basic Rate)
255 typedef enum _VIA_PKT_TYPE
261 } VIA_PKT_TYPE, *PVIA_PKT_TYPE;
265 #define NDIS_STATUS int
268 typedef enum __DEVICE_NDIS_STATUS {
273 } DEVICE_NDIS_STATUS, *PDEVICE_NDIS_STATUS;
276 #define MAX_BSSIDINFO_4_PMKID 16
277 #define MAX_PMKIDLIST 5
278 //Flags for PMKID Candidate list structure
279 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
282 typedef unsigned char NDIS_802_11_PMKID_VALUE[16];
285 typedef enum _NDIS_802_11_WEP_STATUS
287 Ndis802_11WEPEnabled,
288 Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled,
289 Ndis802_11WEPDisabled,
290 Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled,
291 Ndis802_11WEPKeyAbsent,
292 Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent,
293 Ndis802_11WEPNotSupported,
294 Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported,
295 Ndis802_11Encryption2Enabled,
296 Ndis802_11Encryption2KeyAbsent,
297 Ndis802_11Encryption3Enabled,
298 Ndis802_11Encryption3KeyAbsent
299 } NDIS_802_11_WEP_STATUS, *PNDIS_802_11_WEP_STATUS,
300 NDIS_802_11_ENCRYPTION_STATUS, *PNDIS_802_11_ENCRYPTION_STATUS;
303 typedef enum _NDIS_802_11_STATUS_TYPE
305 Ndis802_11StatusType_Authentication,
306 Ndis802_11StatusType_MediaStreamMode,
307 Ndis802_11StatusType_PMKID_CandidateList,
308 Ndis802_11StatusTypeMax // not a real type, defined as an upper bound
309 } NDIS_802_11_STATUS_TYPE, *PNDIS_802_11_STATUS_TYPE;
311 //Added new types for PMKID Candidate lists.
312 typedef struct _PMKID_CANDIDATE {
313 NDIS_802_11_MAC_ADDRESS BSSID;
315 } PMKID_CANDIDATE, *PPMKID_CANDIDATE;
318 typedef struct _BSSID_INFO
320 NDIS_802_11_MAC_ADDRESS BSSID;
321 NDIS_802_11_PMKID_VALUE PMKID;
322 } BSSID_INFO, *PBSSID_INFO;
324 typedef struct tagSPMKID {
325 unsigned long Length;
326 unsigned long BSSIDInfoCount;
327 BSSID_INFO BSSIDInfo[MAX_BSSIDINFO_4_PMKID];
330 typedef struct tagSPMKIDCandidateEvent {
331 NDIS_802_11_STATUS_TYPE StatusType;
332 unsigned long Version; /* Version of the structure */
333 unsigned long NumCandidates; /* No. of pmkid candidates */
334 PMKID_CANDIDATE CandidateList[MAX_PMKIDLIST];
335 } SPMKIDCandidateEvent, *PSPMKIDCandidateEvent;
340 #define MAX_QUIET_COUNT 8
342 typedef struct tagSQuietControl {
347 } SQuietControl, *PSQuietControl;
352 // The receive duplicate detection cache entry
353 typedef struct tagSCacheEntry{
355 BYTE abyAddr2[ETH_ALEN];
357 } SCacheEntry, *PSCacheEntry;
359 typedef struct tagSCache{
360 /* The receive cache is updated circularly. The next entry to be written is
361 * indexed by the "InPtr".
363 unsigned int uInPtr; /* Place to use next */
364 SCacheEntry asCacheEntry[DUPLICATE_RX_CACHE_LENGTH];
367 #define CB_MAX_RX_FRAG 64
368 // DeFragment Control Block, used for collecting fragments prior to reassembly
369 typedef struct tagSDeFragControlBlock
373 BYTE abyAddr2[ETH_ALEN];
374 unsigned int uLifetime;
377 unsigned int cbFrameLength;
379 } SDeFragControlBlock, *PSDeFragControlBlock;
384 #define DEVICE_FLAGS_UNPLUG 0x00000001UL
385 #define DEVICE_FLAGS_PREAMBLE_TYPE 0x00000002UL
386 #define DEVICE_FLAGS_OP_MODE 0x00000004UL
387 #define DEVICE_FLAGS_PS_MODE 0x00000008UL
388 #define DEVICE_FLAGS_80211h_MODE 0x00000010UL
390 //flags for driver status
391 #define DEVICE_FLAGS_OPENED 0x00010000UL
392 #define DEVICE_FLAGS_WOL_ENABLED 0x00080000UL
393 //flags for capbilities
394 #define DEVICE_FLAGS_TX_ALIGN 0x01000000UL
395 #define DEVICE_FLAGS_HAVE_CAM 0x02000000UL
396 #define DEVICE_FLAGS_FLOW_CTRL 0x04000000UL
398 //flags for MII status
399 #define DEVICE_LINK_FAIL 0x00000001UL
400 #define DEVICE_SPEED_10 0x00000002UL
401 #define DEVICE_SPEED_100 0x00000004UL
402 #define DEVICE_SPEED_1000 0x00000008UL
403 #define DEVICE_DUPLEX_FULL 0x00000010UL
404 #define DEVICE_AUTONEG_ENABLE 0x00000020UL
405 #define DEVICE_FORCED_BY_EEPROM 0x00000040UL
406 //for device_set_media_duplex
407 #define DEVICE_LINK_CHANGE 0x00000001UL
410 typedef struct __device_opt {
411 int nRxDescs0; //Number of RX descriptors0
412 int nTxDescs0; //Number of TX descriptors 0, 1, 2, 3
413 int rts_thresh; //rts threshold
422 } OPTIONS, *POPTIONS;
425 typedef struct __device_info {
428 struct usb_device* usb;
429 struct net_device* dev;
430 struct net_device_stats stats;
434 struct tasklet_struct CmdWorkItem;
435 struct tasklet_struct EventWorkItem;
436 struct tasklet_struct ReadWorkItem;
437 struct tasklet_struct RxMngWorkItem;
454 SDeFragControlBlock sRxDFCB[CB_MAX_RX_FRAG];
456 unsigned int cbFreeDFCB;
457 unsigned int uCurrentDFCBIdx;
461 struct urb *pControlURB;
462 struct urb *pInterruptURB;
463 struct usb_ctrlrequest sUsbCtlRequest;
465 unsigned int int_interval;
467 // Variables to track resources for the BULK In Pipe
470 PRCB apRCB[CB_MAX_RX_DESC];
472 PRCB FirstRecvFreeList;
473 PRCB LastRecvFreeList;
474 unsigned int NumRecvFreeList;
475 PRCB FirstRecvMngList;
476 PRCB LastRecvMngList;
477 unsigned int NumRecvMngList;
478 BOOL bIsRxWorkItemQueued;
479 BOOL bIsRxMngWorkItemQueued;
480 unsigned long ulRcvRefCount; /* packets that have not returned back */
483 // Variables to track resources for the BULK Out Pipe
486 PUSB_SEND_CONTEXT apTD[CB_MAX_TX_DESC];
490 // Variables to track resources for the Interript In Pipe
493 BOOL fKillEventPollingThread;
494 BOOL bEventAvailable;
497 //default config from file by user setting
498 DEFAULT_CONFIG config_file;
503 // protect with spinlock
504 unsigned long ulBulkInPosted;
505 unsigned long ulBulkInError;
506 unsigned long ulBulkInContCRCError;
507 unsigned long ulBulkInBytesRead;
509 unsigned long ulBulkOutPosted;
510 unsigned long ulBulkOutError;
511 unsigned long ulBulkOutContCRCError;
512 unsigned long ulBulkOutBytesWrite;
514 unsigned long ulIntInPosted;
515 unsigned long ulIntInError;
516 unsigned long ulIntInContCRCError;
517 unsigned long ulIntInBytesRead;
521 WORD wFirmwareVersion;
530 BYTE byOriginalZonetype;
532 BOOL bLinkPass; // link status: OK or fail
533 BYTE abyCurrentNetAddr[ETH_ALEN];
534 BYTE abyPermanentNetAddr[ETH_ALEN];
535 // SW network address
536 /* u8 abySoftwareNetAddr[ETH_ALEN]; */
537 BOOL bExistSWNetAddr;
539 // Adapter statistics
540 SStatCounter scStatistic;
542 SDot11Counters s802_11Counter;
545 // Maintain statistical debug info.
547 unsigned long packetsReceived;
548 unsigned long packetsReceivedDropped;
549 unsigned long packetsReceivedOverflow;
550 unsigned long packetsSent;
551 unsigned long packetsSentDropped;
552 unsigned long SendContextsInUse;
553 unsigned long RcvBuffersInUse;
557 SMgmtObject sMgmtObj;
560 unsigned int cbBulkInMax;
563 // 802.11 MAC specific
564 unsigned int uCurrRSSI;
570 DWORD dwRxAntennaSel;
571 DWORD dwTxAntennaSel;
573 BYTE byRxAntennaMode;
574 BYTE byTxAntennaMode;
578 //SQ3 functions for antenna diversity
579 struct timer_list TimerSQ3Tmax1;
580 struct timer_list TimerSQ3Tmax2;
581 struct timer_list TimerSQ3Tmax3;
583 BOOL bDiversityRegCtlON;
584 BOOL bDiversityEnable;
585 unsigned long ulDiversityNValue;
586 unsigned long ulDiversityMValue;
590 unsigned long ulSQ3TH;
592 unsigned long uDiversityCnt;
594 unsigned long ulRatio_State0;
595 unsigned long ulRatio_State1;
596 unsigned long ulSQ3_State0;
597 unsigned long ulSQ3_State1;
599 unsigned long aulSQ3Val[MAX_RATE];
600 unsigned long aulPktNum[MAX_RATE];
603 unsigned int uSIFS; /* Current SIFS */
604 unsigned int uDIFS; /* Current DIFS */
605 unsigned int uEIFS; /* Current EIFS */
606 unsigned int uSlot; /* Current SlotTime */
607 unsigned int uCwMin; /* Current CwMin */
608 unsigned int uCwMax; /* CwMax is fixed on 1023 */
618 VIA_BB_TYPE byBBType; //0: 11A, 1:11B, 2:11G
619 VIA_PKT_TYPE byPacketType; //0:11a,1:11b,2:11gb(only CCK in BasicRate),3:11ga(OFDM in Basic Rate)
622 BYTE byTopOFDMBasicRate;
623 BYTE byTopCCKBasicRate;
626 DWORD dwAotoRateTxOkCnt;
627 DWORD dwAotoRateTxFailCnt;
628 DWORD dwErrorRateThreshold[13];
629 DWORD dwTPTable[MAX_RATE];
630 BYTE abyEEPROM[EEP_MAX_CONTEXT_SIZE]; //DWORD alignment
634 unsigned int uConnectionRate;
637 BYTE byShortPreamble;
641 // For RF Power table
646 BYTE abyCCKPwrTbl[14];
647 BYTE abyOFDMPwrTbl[14];
648 BYTE abyOFDMAPwrTbl[42];
652 WORD wFragmentationThreshold;
653 BYTE byShortRetryLimit;
654 BYTE byLongRetryLimit;
655 CARD_OP_MODE eOPMode;
657 WORD wMaxTransmitMSDULifetime;
658 BYTE abyBSSID[ETH_ALEN];
659 BYTE abyDesireBSSID[ETH_ALEN];
660 WORD wCTSDuration; // update while speed change
661 WORD wACKDuration; // update while speed change
662 WORD wRTSTransmitLen; // update while speed change
663 BYTE byRTSServiceField; // update while speed change
664 BYTE byRTSSignalField; // update while speed change
666 DWORD dwMaxReceiveLifetime; // dot11MaxReceiveLifetime
669 BOOL bEncryptionEnable;
671 BOOL bSoftwareGenCrcErr;
675 BOOL bBarkerPreambleMd;
678 WORD wUseProtectCntDown;
680 BOOL bRadioControlOff;
685 WORD wListenInterval;
687 WMAC_POWER_MODE ePSMode;
688 unsigned long ulPSModeWaitTx;
693 BOOL bBeaconBufReady;
697 unsigned int uScanTime;
699 CMD_STATE eCommandState;
708 unsigned int uAutoReConnectTime;
709 unsigned int uIsroamingTime;
713 CMD_ITEM eCmdQueue[CMD_Q_SIZE];
714 unsigned int uCmdDequeueIdx;
715 unsigned int uCmdEnqueueIdx;
716 unsigned int cbFreeCmdQueue;
721 BOOL bEnableRoaming; //DavidWang
722 BOOL bIsRoaming; //DavidWang
723 BOOL bFastRoaming; //DavidWang
724 BYTE bSameBSSMaxNum; //Davidwang
725 BYTE bSameBSSCurNum; //DavidWang
728 unsigned long ulTxPower;
731 NDIS_802_11_WEP_STATUS eEncryptionStatus;
734 //2007-0925-01<Add>by MikeLiu
735 //mike add :save old Encryption
736 NDIS_802_11_WEP_STATUS eOldEncryptionStatus;
743 BYTE abyPRNG[WLAN_WEPMAX_KEYLEN+3];
749 unsigned int uKeyLength;
750 BYTE abyKey[WLAN_WEP232_KEYLEN];
753 unsigned int uAssocCount;
766 // For Update BaseBand VGA Gain Offset
768 unsigned int uBBVGADiffCount;
771 BYTE abyBBVGA[BB_VGA_LEVEL];
772 signed long ldBmThreshold[BB_VGA_LEVEL];
779 DWORD dwDiagRefCount;
784 // For Auto Power Tunning
786 BYTE byAutoPwrTunning;
788 // BaseBand Loopback Use
795 struct timer_list sTimerCommand;
797 struct timer_list sTimerTxData;
798 unsigned long nTxDataTimeCout;
800 BOOL IsTxDataTrigger;
802 #ifdef WPA_SM_Transtatus
803 BOOL fWPA_Authened; //is WPA/WPA-PSK or WPA2/WPA2-PSK authen??
805 BYTE byReAssocCount; //mike add:re-association retry times!
806 BYTE byLinkWaitCount;
808 SEthernetHeader sTxEthHeader;
809 SEthernetHeader sRxEthHeader;
810 BYTE abyBroadcastAddr[ETH_ALEN];
811 BYTE abySNAP_RFC1042[ETH_ALEN];
812 BYTE abySNAP_Bridgetunnel[ETH_ALEN];
814 // Pre-Authentication & PMK cache
816 SPMKIDCandidateEvent gsPMKIDCandidate;
824 BYTE byChannelSwitchCount;
826 //WPA supplicant daemon
827 struct net_device *wpadev;
832 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
837 BOOL bWPASuppWextEnabled;
841 // user space daemon: hostapd, is used for HOSTAP
845 struct net_device *apdev;
846 int (*tx_80211)(struct sk_buff *skb, struct net_device *dev);
848 unsigned int uChannel;
850 struct iw_statistics wstats; // wireless stats
853 } DEVICE_INFO, *PSDevice;
858 #define EnqueueRCB(_Head, _Tail, _RCB) \
864 _Tail->Next = _RCB; \
870 #define DequeueRCB(Head, Tail) \
880 #define ADD_ONE_WITH_WRAP_AROUND(uVar, uModulo) { \
881 if ((uVar) >= ((uModulo) - 1)) \
888 #define fMP_RESET_IN_PROGRESS 0x00000001
889 #define fMP_DISCONNECTED 0x00000002
890 #define fMP_HALT_IN_PROGRESS 0x00000004
891 #define fMP_SURPRISE_REMOVED 0x00000008
892 #define fMP_RECV_LOOKASIDE 0x00000010
893 #define fMP_INIT_IN_PROGRESS 0x00000020
894 #define fMP_SEND_SIDE_RESOURCE_ALLOCATED 0x00000040
895 #define fMP_RECV_SIDE_RESOURCE_ALLOCATED 0x00000080
896 #define fMP_POST_READS 0x00000100
897 #define fMP_POST_WRITES 0x00000200
898 #define fMP_CONTROL_READS 0x00000400
899 #define fMP_CONTROL_WRITES 0x00000800
903 #define MP_SET_FLAG(_M, _F) ((_M)->Flags |= (_F))
904 #define MP_CLEAR_FLAG(_M, _F) ((_M)->Flags &= ~(_F))
905 #define MP_TEST_FLAG(_M, _F) (((_M)->Flags & (_F)) != 0)
906 #define MP_TEST_FLAGS(_M, _F) (((_M)->Flags & (_F)) == (_F))
908 #define MP_IS_READY(_M) (((_M)->Flags & \
909 (fMP_DISCONNECTED | fMP_RESET_IN_PROGRESS | fMP_HALT_IN_PROGRESS | fMP_INIT_IN_PROGRESS | fMP_SURPRISE_REMOVED)) == 0)
911 /*--------------------- Export Functions --------------------------*/
913 /* BOOL device_dma0_xmit(PSDevice pDevice, struct sk_buff *skb,
914 * unsigned int uNodeIndex);
916 BOOL device_alloc_frag_buf(PSDevice pDevice, PSDeFragControlBlock pDeF);