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/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/device.h>
38 #include <linux/firmware.h>
39 #include <linux/etherdevice.h>
40 #include <linux/suspend.h>
41 #include <linux/if_arp.h>
42 #include <linux/wireless.h>
43 #include <net/iw_handler.h>
44 #include <net/cfg80211.h>
45 #include <linux/timer.h>
46 #include <linux/usb.h>
50 #define DEVICE_ETHTOOL_IOCTL_SUPPORT
51 #include <linux/ethtool.h>
53 #undef DEVICE_ETHTOOL_IOCTL_SUPPORT
56 //please copy below macro to driver_event.c for API
57 #define RT_INSMOD_EVENT_FLAG 0x0101
58 #define RT_UPDEV_EVENT_FLAG 0x0102
59 #define RT_DISCONNECTED_EVENT_FLAG 0x0103
60 #define RT_WPACONNECTED_EVENT_FLAG 0x0104
61 #define RT_DOWNDEV_EVENT_FLAG 0x0105
62 #define RT_RMMOD_EVENT_FLAG 0x0106
68 #include "device_cfg.h"
81 /*--------------------- Export Definitions -------------------------*/
82 #define VNT_USB_VENDOR_ID 0x160a
83 #define VNT_USB_PRODUCT_ID 0x3184
85 #define MAC_MAX_CONTEXT_REG (256+128)
87 #define MAX_MULTICAST_ADDRESS_NUM 32
88 #define MULTICAST_ADDRESS_LIST_SIZE (MAX_MULTICAST_ADDRESS_NUM * ETH_ALEN)
90 #define DUPLICATE_RX_CACHE_LENGTH 5
92 #define NUM_KEY_ENTRY 11
97 #define TX_WEP_SWOTP 3
98 #define TX_WEP_OTPSW 4
99 #define TX_WEP_SW232 5
101 #define KEYSEL_WEP40 0
102 #define KEYSEL_WEP104 1
103 #define KEYSEL_TKIP 2
104 #define KEYSEL_CCMP 3
106 #define AUTO_FB_NONE 0
116 #define ANT_DIVERSITY 2
117 #define ANT_RXD_TXA 3
118 #define ANT_RXD_TXB 4
119 #define ANT_UNKNOWN 0xFF
126 #define MAXCHECKHANGCNT 4
129 #define TX_PKT_UNI 0x00
130 #define TX_PKT_MULTI 0x01
131 #define TX_PKT_BROAD 0x02
133 #define BB_VGA_LEVEL 4
134 #define BB_VGA_CHANGE_THRESHOLD 3
137 #define RUN_AT(x) (jiffies+(x))
141 #define RESERV_AC0DMA 4
143 #define PRIVATE_Message 0
145 /*--------------------- Export Types ------------------------------*/
147 #define DBG_PRT(l, p, args...) { if (l <= msglevel) printk(p, ##args); }
148 #define PRINT_K(p, args...) { if (PRIVATE_Message) printk(p, ##args); }
150 typedef enum __device_msg_level {
151 MSG_LEVEL_ERR = 0, /* Errors causing abnormal operation */
152 MSG_LEVEL_NOTICE = 1, /* Errors needing user notification */
153 MSG_LEVEL_INFO = 2, /* Normal message. */
154 MSG_LEVEL_VERBOSE = 3, /* Will report all trival errors. */
155 MSG_LEVEL_DEBUG = 4 /* Only for debug purpose. */
156 } DEVICE_MSG_LEVEL, *PDEVICE_MSG_LEVEL;
158 typedef enum __device_init_type {
159 DEVICE_INIT_COLD = 0, /* cold init */
160 DEVICE_INIT_RESET, /* reset init or Dx to D0 power remain */
161 DEVICE_INIT_DXPL /* Dx to D0 power lost init */
162 } DEVICE_INIT_TYPE, *PDEVICE_INIT_TYPE;
167 // Enum of context types for SendPacket
169 typedef enum _CONTEXT_TYPE {
170 CONTEXT_DATA_PACKET = 1,
174 // RCB (Receive Control Block)
181 SRxMgmtPacket sMngPacket;
187 // used to track bulk out irps
188 typedef struct _USB_SEND_CONTEXT {
190 struct sk_buff *pPacket;
192 unsigned int uBufLen;
194 SEthernetHeader sEthHeader;
197 unsigned char Data[MAX_TOTAL_SIZE_WITH_ALL_HEADERS];
198 } USB_SEND_CONTEXT, *PUSB_SEND_CONTEXT;
200 /* structure got from configuration file as user-desired default settings */
201 typedef struct _DEFAULT_CONFIG {
203 signed int eConfigMode;
204 signed int eAuthenMode; /* open/wep/wpa */
205 signed int bShareKeyAlgorithm; /* open-open/{open,wep}-sharekey */
206 signed int keyidx; /* wepkey index */
207 signed int eEncryptionStatus;
208 } DEFAULT_CONFIG, *PDEFAULT_CONFIG;
211 // Structure to keep track of usb interrupt packets
214 unsigned int uDataLen;
218 } INT_BUFFER, *PINT_BUFFER;
221 typedef enum _VIA_BB_TYPE
226 } VIA_BB_TYPE, *PVIA_BB_TYPE;
228 //0:11a,1:11b,2:11gb(only CCK in BasicRate),3:11ga(OFDM in Basic Rate)
229 typedef enum _VIA_PKT_TYPE
235 } VIA_PKT_TYPE, *PVIA_PKT_TYPE;
239 typedef enum __DEVICE_NDIS_STATUS {
244 } DEVICE_NDIS_STATUS, *PDEVICE_NDIS_STATUS;
246 #define MAX_BSSIDINFO_4_PMKID 16
247 #define MAX_PMKIDLIST 5
248 //Flags for PMKID Candidate list structure
249 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
252 typedef unsigned char NDIS_802_11_PMKID_VALUE[16];
255 typedef enum _NDIS_802_11_WEP_STATUS
257 Ndis802_11WEPEnabled,
258 Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled,
259 Ndis802_11WEPDisabled,
260 Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled,
261 Ndis802_11WEPKeyAbsent,
262 Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent,
263 Ndis802_11WEPNotSupported,
264 Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported,
265 Ndis802_11Encryption2Enabled,
266 Ndis802_11Encryption2KeyAbsent,
267 Ndis802_11Encryption3Enabled,
268 Ndis802_11Encryption3KeyAbsent
269 } NDIS_802_11_WEP_STATUS, *PNDIS_802_11_WEP_STATUS,
270 NDIS_802_11_ENCRYPTION_STATUS, *PNDIS_802_11_ENCRYPTION_STATUS;
273 typedef enum _NDIS_802_11_STATUS_TYPE
275 Ndis802_11StatusType_Authentication,
276 Ndis802_11StatusType_MediaStreamMode,
277 Ndis802_11StatusType_PMKID_CandidateList,
278 Ndis802_11StatusTypeMax // not a real type, defined as an upper bound
279 } NDIS_802_11_STATUS_TYPE, *PNDIS_802_11_STATUS_TYPE;
281 //Added new types for PMKID Candidate lists.
282 typedef struct _PMKID_CANDIDATE {
283 NDIS_802_11_MAC_ADDRESS BSSID;
285 } PMKID_CANDIDATE, *PPMKID_CANDIDATE;
288 typedef struct _BSSID_INFO
290 NDIS_802_11_MAC_ADDRESS BSSID;
291 NDIS_802_11_PMKID_VALUE PMKID;
292 } BSSID_INFO, *PBSSID_INFO;
294 typedef struct tagSPMKID {
295 unsigned long Length;
296 unsigned long BSSIDInfoCount;
297 BSSID_INFO BSSIDInfo[MAX_BSSIDINFO_4_PMKID];
300 typedef struct tagSPMKIDCandidateEvent {
301 NDIS_802_11_STATUS_TYPE StatusType;
302 unsigned long Version; /* Version of the structure */
303 unsigned long NumCandidates; /* No. of pmkid candidates */
304 PMKID_CANDIDATE CandidateList[MAX_PMKIDLIST];
305 } SPMKIDCandidateEvent, *PSPMKIDCandidateEvent;
310 #define MAX_QUIET_COUNT 8
312 typedef struct tagSQuietControl {
317 } SQuietControl, *PSQuietControl;
322 // The receive duplicate detection cache entry
323 typedef struct tagSCacheEntry{
325 BYTE abyAddr2[ETH_ALEN];
327 } SCacheEntry, *PSCacheEntry;
329 typedef struct tagSCache{
330 /* The receive cache is updated circularly. The next entry to be written is
331 * indexed by the "InPtr".
333 unsigned int uInPtr; /* Place to use next */
334 SCacheEntry asCacheEntry[DUPLICATE_RX_CACHE_LENGTH];
337 #define CB_MAX_RX_FRAG 64
338 // DeFragment Control Block, used for collecting fragments prior to reassembly
339 typedef struct tagSDeFragControlBlock
343 BYTE abyAddr2[ETH_ALEN];
344 unsigned int uLifetime;
347 unsigned int cbFrameLength;
349 } SDeFragControlBlock, *PSDeFragControlBlock;
354 #define DEVICE_FLAGS_UNPLUG 0x00000001UL
355 #define DEVICE_FLAGS_PREAMBLE_TYPE 0x00000002UL
356 #define DEVICE_FLAGS_OP_MODE 0x00000004UL
357 #define DEVICE_FLAGS_PS_MODE 0x00000008UL
358 #define DEVICE_FLAGS_80211h_MODE 0x00000010UL
360 //flags for driver status
361 #define DEVICE_FLAGS_OPENED 0x00010000UL
362 #define DEVICE_FLAGS_WOL_ENABLED 0x00080000UL
363 //flags for capbilities
364 #define DEVICE_FLAGS_TX_ALIGN 0x01000000UL
365 #define DEVICE_FLAGS_HAVE_CAM 0x02000000UL
366 #define DEVICE_FLAGS_FLOW_CTRL 0x04000000UL
368 //flags for MII status
369 #define DEVICE_LINK_FAIL 0x00000001UL
370 #define DEVICE_SPEED_10 0x00000002UL
371 #define DEVICE_SPEED_100 0x00000004UL
372 #define DEVICE_SPEED_1000 0x00000008UL
373 #define DEVICE_DUPLEX_FULL 0x00000010UL
374 #define DEVICE_AUTONEG_ENABLE 0x00000020UL
375 #define DEVICE_FORCED_BY_EEPROM 0x00000040UL
376 //for device_set_media_duplex
377 #define DEVICE_LINK_CHANGE 0x00000001UL
380 typedef struct __device_opt {
381 int nRxDescs0; //Number of RX descriptors0
382 int nTxDescs0; //Number of TX descriptors 0, 1, 2, 3
383 int rts_thresh; //rts threshold
392 } OPTIONS, *POPTIONS;
395 typedef struct __device_info {
398 struct usb_device* usb;
399 struct net_device* dev;
400 struct net_device_stats stats;
405 struct tasklet_struct CmdWorkItem;
406 struct tasklet_struct EventWorkItem;
407 struct tasklet_struct ReadWorkItem;
408 struct tasklet_struct RxMngWorkItem;
425 SDeFragControlBlock sRxDFCB[CB_MAX_RX_FRAG];
427 unsigned int cbFreeDFCB;
428 unsigned int uCurrentDFCBIdx;
432 struct urb *pControlURB;
433 struct urb *pInterruptURB;
434 struct usb_ctrlrequest sUsbCtlRequest;
436 unsigned int int_interval;
438 // Variables to track resources for the BULK In Pipe
441 PRCB apRCB[CB_MAX_RX_DESC];
443 PRCB FirstRecvFreeList;
444 PRCB LastRecvFreeList;
445 unsigned int NumRecvFreeList;
446 PRCB FirstRecvMngList;
447 PRCB LastRecvMngList;
448 unsigned int NumRecvMngList;
449 BOOL bIsRxWorkItemQueued;
450 BOOL bIsRxMngWorkItemQueued;
451 unsigned long ulRcvRefCount; /* packets that have not returned back */
454 // Variables to track resources for the BULK Out Pipe
457 PUSB_SEND_CONTEXT apTD[CB_MAX_TX_DESC];
461 // Variables to track resources for the Interrupt In Pipe
464 BOOL fKillEventPollingThread;
465 BOOL bEventAvailable;
468 //default config from file by user setting
469 DEFAULT_CONFIG config_file;
474 // protect with spinlock
475 unsigned long ulBulkInPosted;
476 unsigned long ulBulkInError;
477 unsigned long ulBulkInContCRCError;
478 unsigned long ulBulkInBytesRead;
480 unsigned long ulBulkOutPosted;
481 unsigned long ulBulkOutError;
482 unsigned long ulBulkOutContCRCError;
483 unsigned long ulBulkOutBytesWrite;
485 unsigned long ulIntInPosted;
486 unsigned long ulIntInError;
487 unsigned long ulIntInContCRCError;
488 unsigned long ulIntInBytesRead;
492 WORD wFirmwareVersion;
501 BYTE byOriginalZonetype;
503 BOOL bLinkPass; // link status: OK or fail
504 BYTE abyCurrentNetAddr[ETH_ALEN];
505 BYTE abyPermanentNetAddr[ETH_ALEN];
506 // SW network address
507 /* u8 abySoftwareNetAddr[ETH_ALEN]; */
508 BOOL bExistSWNetAddr;
510 // Adapter statistics
511 SStatCounter scStatistic;
513 SDot11Counters s802_11Counter;
516 // Maintain statistical debug info.
518 unsigned long packetsReceived;
519 unsigned long packetsReceivedDropped;
520 unsigned long packetsReceivedOverflow;
521 unsigned long packetsSent;
522 unsigned long packetsSentDropped;
523 unsigned long SendContextsInUse;
524 unsigned long RcvBuffersInUse;
528 SMgmtObject sMgmtObj;
531 unsigned int cbBulkInMax;
534 // 802.11 MAC specific
535 unsigned int uCurrRSSI;
541 DWORD dwRxAntennaSel;
542 DWORD dwTxAntennaSel;
544 BYTE byRxAntennaMode;
545 BYTE byTxAntennaMode;
549 //SQ3 functions for antenna diversity
550 struct timer_list TimerSQ3Tmax1;
551 struct timer_list TimerSQ3Tmax2;
552 struct timer_list TimerSQ3Tmax3;
554 BOOL bDiversityRegCtlON;
555 BOOL bDiversityEnable;
556 unsigned long ulDiversityNValue;
557 unsigned long ulDiversityMValue;
561 unsigned long ulSQ3TH;
563 unsigned long uDiversityCnt;
565 unsigned long ulRatio_State0;
566 unsigned long ulRatio_State1;
567 unsigned long ulSQ3_State0;
568 unsigned long ulSQ3_State1;
570 unsigned long aulSQ3Val[MAX_RATE];
571 unsigned long aulPktNum[MAX_RATE];
574 unsigned int uSIFS; /* Current SIFS */
575 unsigned int uDIFS; /* Current DIFS */
576 unsigned int uEIFS; /* Current EIFS */
577 unsigned int uSlot; /* Current SlotTime */
578 unsigned int uCwMin; /* Current CwMin */
579 unsigned int uCwMax; /* CwMax is fixed on 1023 */
589 VIA_BB_TYPE byBBType; //0: 11A, 1:11B, 2:11G
590 VIA_PKT_TYPE byPacketType; //0:11a,1:11b,2:11gb(only CCK in BasicRate),3:11ga(OFDM in Basic Rate)
593 BYTE byTopOFDMBasicRate;
594 BYTE byTopCCKBasicRate;
597 DWORD dwAotoRateTxOkCnt;
598 DWORD dwAotoRateTxFailCnt;
599 DWORD dwErrorRateThreshold[13];
600 DWORD dwTPTable[MAX_RATE];
601 BYTE abyEEPROM[EEP_MAX_CONTEXT_SIZE]; //DWORD alignment
605 unsigned int uConnectionRate;
608 BYTE byShortPreamble;
612 // For RF Power table
617 BYTE abyCCKPwrTbl[14];
618 BYTE abyOFDMPwrTbl[14];
619 BYTE abyOFDMAPwrTbl[42];
623 WORD wFragmentationThreshold;
624 BYTE byShortRetryLimit;
625 BYTE byLongRetryLimit;
626 CARD_OP_MODE eOPMode;
628 WORD wMaxTransmitMSDULifetime;
629 BYTE abyBSSID[ETH_ALEN];
630 BYTE abyDesireBSSID[ETH_ALEN];
631 WORD wCTSDuration; // update while speed change
632 WORD wACKDuration; // update while speed change
633 WORD wRTSTransmitLen; // update while speed change
634 BYTE byRTSServiceField; // update while speed change
635 BYTE byRTSSignalField; // update while speed change
637 DWORD dwMaxReceiveLifetime; // dot11MaxReceiveLifetime
640 BOOL bEncryptionEnable;
642 BOOL bSoftwareGenCrcErr;
646 BOOL bBarkerPreambleMd;
649 WORD wUseProtectCntDown;
651 BOOL bRadioControlOff;
656 WORD wListenInterval;
658 WMAC_POWER_MODE ePSMode;
659 unsigned long ulPSModeWaitTx;
664 BOOL bBeaconBufReady;
668 unsigned int uScanTime;
670 CMD_STATE eCommandState;
679 unsigned int uAutoReConnectTime;
680 unsigned int uIsroamingTime;
684 CMD_ITEM eCmdQueue[CMD_Q_SIZE];
685 unsigned int uCmdDequeueIdx;
686 unsigned int uCmdEnqueueIdx;
687 unsigned int cbFreeCmdQueue;
699 unsigned long ulTxPower;
702 NDIS_802_11_WEP_STATUS eEncryptionStatus;
705 //mike add :save old Encryption
706 NDIS_802_11_WEP_STATUS eOldEncryptionStatus;
713 BYTE abyPRNG[WLAN_WEPMAX_KEYLEN+3];
718 unsigned int uKeyLength;
719 BYTE abyKey[WLAN_WEP232_KEYLEN];
722 unsigned int uAssocCount;
735 // For Update BaseBand VGA Gain Offset
737 unsigned int uBBVGADiffCount;
740 BYTE abyBBVGA[BB_VGA_LEVEL];
741 signed long ldBmThreshold[BB_VGA_LEVEL];
748 DWORD dwDiagRefCount;
753 // For Auto Power Tunning
755 BYTE byAutoPwrTunning;
757 // BaseBand Loopback Use
764 struct timer_list sTimerCommand;
766 struct timer_list sTimerTxData;
767 unsigned long nTxDataTimeCout;
769 BOOL IsTxDataTrigger;
771 BOOL fWPA_Authened; //is WPA/WPA-PSK or WPA2/WPA2-PSK authen??
772 BYTE byReAssocCount; //mike add:re-association retry times!
773 BYTE byLinkWaitCount;
775 SEthernetHeader sTxEthHeader;
776 SEthernetHeader sRxEthHeader;
777 BYTE abyBroadcastAddr[ETH_ALEN];
778 BYTE abySNAP_RFC1042[ETH_ALEN];
779 BYTE abySNAP_Bridgetunnel[ETH_ALEN];
781 // Pre-Authentication & PMK cache
783 SPMKIDCandidateEvent gsPMKIDCandidate;
791 BYTE byChannelSwitchCount;
793 //WPA supplicant daemon
794 struct net_device *wpadev;
802 BOOL bWPASuppWextEnabled;
805 // user space daemon: hostapd, is used for HOSTAP
809 struct net_device *apdev;
810 int (*tx_80211)(struct sk_buff *skb, struct net_device *dev);
812 unsigned int uChannel;
814 struct iw_statistics wstats; // wireless stats
817 } DEVICE_INFO, *PSDevice;
822 #define EnqueueRCB(_Head, _Tail, _RCB) \
828 _Tail->Next = _RCB; \
834 #define DequeueRCB(Head, Tail) \
844 #define ADD_ONE_WITH_WRAP_AROUND(uVar, uModulo) { \
845 if ((uVar) >= ((uModulo) - 1)) \
852 #define fMP_RESET_IN_PROGRESS 0x00000001
853 #define fMP_DISCONNECTED 0x00000002
854 #define fMP_HALT_IN_PROGRESS 0x00000004
855 #define fMP_SURPRISE_REMOVED 0x00000008
856 #define fMP_RECV_LOOKASIDE 0x00000010
857 #define fMP_INIT_IN_PROGRESS 0x00000020
858 #define fMP_SEND_SIDE_RESOURCE_ALLOCATED 0x00000040
859 #define fMP_RECV_SIDE_RESOURCE_ALLOCATED 0x00000080
860 #define fMP_POST_READS 0x00000100
861 #define fMP_POST_WRITES 0x00000200
862 #define fMP_CONTROL_READS 0x00000400
863 #define fMP_CONTROL_WRITES 0x00000800
865 #define MP_SET_FLAG(_M, _F) ((_M)->Flags |= (_F))
866 #define MP_CLEAR_FLAG(_M, _F) ((_M)->Flags &= ~(_F))
867 #define MP_TEST_FLAGS(_M, _F) (((_M)->Flags & (_F)) == (_F))
869 #define MP_IS_READY(_M) (((_M)->Flags & \
870 (fMP_DISCONNECTED | fMP_RESET_IN_PROGRESS | fMP_HALT_IN_PROGRESS | fMP_INIT_IN_PROGRESS | fMP_SURPRISE_REMOVED)) == 0)
872 /*--------------------- Export Functions --------------------------*/
874 /* BOOL device_dma0_xmit(PSDevice pDevice, struct sk_buff *skb,
875 * unsigned int uNodeIndex);
877 BOOL device_alloc_frag_buf(PSDevice pDevice, PSDeFragControlBlock pDeF);