2 This is part of rtl8180 OpenSource driver.
3 Copyright (C) Andrea Merello 2004-2005 <andreamrl@tiscali.it>
4 Released under the terms of GPL (General Public Licence)
6 Parts of this driver are based on the GPL part of the
7 official realtek driver
9 Parts of this driver are based on the rtl8180 driver skeleton
10 from Patric Schenke & Andres Salomon
12 Parts of this driver are based on the Intel Pro Wireless 2100 GPL driver
14 We want to tanks the Authors of those projects and the Ndiswrapper
22 #define RTL8180_MODULE_NAME "r8180"
23 #define DMESG(x,a...) printk(KERN_INFO RTL8180_MODULE_NAME ": " x "\n", ## a)
24 #define DMESGW(x,a...) printk(KERN_WARNING RTL8180_MODULE_NAME ": WW:" x "\n", ## a)
25 #define DMESGE(x,a...) printk(KERN_WARNING RTL8180_MODULE_NAME ": EE:" x "\n", ## a)
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 //#include <linux/config.h>
30 #include <linux/init.h>
31 #include <linux/ioport.h>
32 #include <linux/sched.h>
33 #include <linux/types.h>
34 #include <linux/slab.h>
35 #include <linux/netdevice.h>
36 #include <linux/pci.h>
37 #include <linux/etherdevice.h>
38 #include <linux/delay.h>
39 #include <linux/rtnetlink.h> //for rtnl_lock()
40 #include <linux/wireless.h>
41 #include <linux/timer.h>
42 #include <linux/proc_fs.h> // Necessary because we use the proc fs
43 #include <linux/if_arp.h>
44 #include "ieee80211/ieee80211.h"
46 //#include <asm/semaphore.h>
51 #define RTL_IOCTL_WPA_SUPPLICANT SIOCIWFIRSTPRIV+30
53 #define DEFAULT_FRAG_THRESHOLD 2342U
54 #define MIN_FRAG_THRESHOLD 256U
55 //#define MAX_FRAG_THRESHOLD 2342U
56 #define DEFAULT_RTS_THRESHOLD 2342U
57 #define MIN_RTS_THRESHOLD 0U
58 #define MAX_RTS_THRESHOLD 2342U
59 #define DEFAULT_BEACONINTERVAL 0x64U
60 #define DEFAULT_BEACON_ESSID "Rtl8180"
62 #define DEFAULT_SSID ""
63 #define DEFAULT_RETRY_RTS 7
64 #define DEFAULT_RETRY_DATA 7
65 #define PRISM_HDR_SIZE 64
74 #define BEACON_QUEUE 6
76 #define LOW_QUEUE BE_QUEUE
77 #define NORMAL_QUEUE MGNT_QUEUE
82 #define sAckCtsLng 112 // bits in ACK and CTS frames
84 #define RATE_ADAPTIVE_TIMER_PERIOD 300
86 typedef enum _WIRELESS_MODE {
87 WIRELESS_MODE_UNKNOWN = 0x00,
88 WIRELESS_MODE_A = 0x01,
89 WIRELESS_MODE_B = 0x02,
90 WIRELESS_MODE_G = 0x04,
91 WIRELESS_MODE_AUTO = 0x08,
94 typedef enum _VERSION_8185{
97 VERSION_8185_C, // C-cut
98 VERSION_8185_D, // D-cut
100 VERSION_8185B_B, // B-cut
101 VERSION_8185B_D, // D-cut
102 VERSION_8185B_E, // E-cut
104 VERSION_8187S_B, // B-cut
105 VERSION_8187S_C, // C-cut
106 VERSION_8187S_D, // D-cut
108 }VERSION_8185,*PVERSION_8185;
109 typedef struct ChnlAccessSetting {
116 }*PCHANNEL_ACCESS_SETTING,CHANNEL_ACCESS_SETTING;
123 typedef u32 AC_CODING;
124 #define AC0_BE 0 // ACI: 0x00 // Best Effort
125 #define AC1_BK 1 // ACI: 0x01 // Background
126 #define AC2_VI 2 // ACI: 0x10 // Video
127 #define AC3_VO 3 // ACI: 0x11 // Voice
128 #define AC_MAX 4 // Max: define total number; Should not to be used as a real enum.
131 // ECWmin/ECWmax field.
132 // Ref: WMM spec 2.2.2: WME Parameter Element, p.13.
145 // Ref: WMM spec 2.2.2: WME Parameter Element, p.12.
147 typedef union _ACI_AIFSN{
157 }ACI_AIFSN, *PACI_AIFSN;
160 // AC Parameters Record Format.
161 // Ref: WMM spec 2.2.2: WME Parameter Element, p.12.
163 typedef union _AC_PARAM{
173 }AC_PARAM, *PAC_PARAM;
175 /* it is a wrong definition. -xiong-2006-11-17
176 typedef struct ThreeWireReg {
187 typedef union _ThreeWire{
188 struct _ThreeWireStruc{
195 // u2Byte ThreeWireEnable:1;
202 typedef struct buffer
214 unsigned long rxnolast;
215 unsigned long rxnodata;
216 // unsigned long rxreset;
217 // unsigned long rxwrkaround;
218 unsigned long rxnopointer;
219 unsigned long txnperr;
220 unsigned long txresumed;
222 unsigned long rxoverflow;
224 unsigned long txbkpokint;
225 unsigned long txbepoking;
226 unsigned long txbkperr;
227 unsigned long txbeperr;
228 unsigned long txnpokint;
229 unsigned long txhpokint;
230 unsigned long txhperr;
232 unsigned long shints;
233 unsigned long txoverflow;
234 unsigned long rxdmafail;
235 unsigned long txbeacon;
236 unsigned long txbeaconerr;
237 unsigned long txlpokint;
238 unsigned long txlperr;
239 unsigned long txretry;//retry number tony 20060601
240 unsigned long rxcrcerrmin;//crc error (0-500)
241 unsigned long rxcrcerrmid;//crc error (500-1000)
242 unsigned long rxcrcerrmax;//crc error (>1000)
243 unsigned long rxicverr;//ICV error
246 #define MAX_LD_SLOT_NUM 10
247 #define KEEP_ALIVE_INTERVAL 20 // in seconds.
248 #define CHECK_FOR_HANG_PERIOD 2 //be equal to watchdog check time
249 #define DEFAULT_KEEP_ALIVE_LEVEL 1
250 #define DEFAULT_SLOT_NUM 2
251 #define POWER_PROFILE_AC 0
252 #define POWER_PROFILE_BATTERY 1
254 typedef struct _link_detect_t
256 u32 RxFrameNum[MAX_LD_SLOT_NUM]; // number of Rx Frame / CheckForHang_period to determine link status
257 u16 SlotNum; // number of CheckForHang period to determine link status, default is 2
260 u32 NumTxOkInPeriod; //number of packet transmitted during CheckForHang
261 u32 NumRxOkInPeriod; //number of packet received during CheckForHang
263 u8 IdleCount; // (KEEP_ALIVE_INTERVAL / CHECK_FOR_HANG_PERIOD)
264 u32 LastNumTxUnicast;
265 u32 LastNumRxUnicast;
267 bool bBusyTraffic; //when it is set to 1, UI cann't scan at will.
268 }link_detect_t, *plink_detect_t;
270 //YJ,modified,080828,end
273 //================================================================================
274 // LED customization.
275 //================================================================================
277 typedef enum _LED_STRATEGY_8185{
280 HW_LED, // HW control 2 LEDs, LED0 and LED1 (there are 4 different control modes)
281 }LED_STRATEGY_8185, *PLED_STRATEGY_8185;
283 //by amy for power save
284 typedef enum _LED_CTL_MODE{
285 LED_CTL_POWER_ON = 1,
290 LED_CTL_SITE_SURVEY = 6,
291 LED_CTL_POWER_OFF = 7
294 typedef enum _RT_RF_POWER_STATE
303 auth_not_valid = 0x2,
316 // Reason code defined in 802.11i D10.0 p.28.
318 four_way_tmout = 0x0f,
319 two_way_tmout = 0x10,
321 invalid_Gcipher = 0x12,
322 invalid_Pcipher = 0x13,
324 unsup_RSNIEver = 0x15,
325 invalid_RSNIE = 0x16,
326 auth_802_1x_fail= 0x17,
329 // Reason code defined in 7.3.1.7, 802.1e D13.0, p.42. Added by Annie, 2005-11-15.
330 QoS_unspec = 0x20, // 32
331 QAP_bandwidth = 0x21, // 33
332 poor_condition = 0x22, // 34
333 no_facility = 0x23, // 35
335 req_declined = 0x25, // 37
336 invalid_param = 0x26, // 38
337 req_not_honored= 0x27, // 39
338 TS_not_created = 0x2F, // 47
339 DL_not_allowed = 0x30, // 48
340 dest_not_exist = 0x31, // 49
341 dest_not_QSTA = 0x32, // 50
343 typedef enum _RT_PS_MODE
345 eActive, // Active/Continuous access.
346 eMaxPs, // Max power save mode.
347 eFastPs // Fast power save mode.
349 //by amy for power save
350 typedef struct r8180_priv
352 struct pci_dev *pdev;
356 struct ieee80211_device *ieee80211;
358 short card_8185; /* O: rtl8180, 1:rtl8185 V B/C, 2:rtl8185 V D, 3:rtl8185B */
359 short card_8185_Bversion; /* if TCR reports card V B/C this discriminates */
360 short phy_ver; /* meaningful for rtl8225 1:A 2:B 3:C */
362 enum card_type {PCI,MINIPCI,CARDBUS,USB/*rtl8187*/}card_type;
364 short plcp_preamble_mode; // 0:auto 1:short 2:long
367 spinlock_t irq_th_lock;
370 spinlock_t rf_ps_lock;
374 struct net_device *dev;
378 u8 chtxpwr[15]; //channels from 1 to 14, 0 not used
379 u8 chtxpwr_ofdm[15]; //channels from 1 to 14, 0 not used
380 //u8 challow[15]; //channels from 1 to 14, 0 not used
381 u8 channel_plan; // it's the channel plan index
383 short crcmon; //if 1 allow bad crc frame reception in monitor mode
386 struct timer_list scan_timer;
389 spinlock_t scan_lock;
391 //u8 active_scan_num;
392 struct semaphore wx_sem;
393 struct semaphore rf_state;
403 short (*rf_set_sens)(struct net_device *dev,short sens);
404 void (*rf_set_chan)(struct net_device *dev,short ch);
405 void (*rf_close)(struct net_device *dev);
406 void (*rf_init)(struct net_device *dev);
407 void (*rf_sleep)(struct net_device *dev);
408 void (*rf_wakeup)(struct net_device *dev);
413 struct _link_detect_t link_detect; //YJ,add,080828
414 struct iw_statistics wstats;
415 struct proc_dir_entry *dir_dev;
420 dma_addr_t rxringdma;
421 struct buffer *rxbuffer;
422 struct buffer *rxbufferhead;
426 struct sk_buff *rx_skb;
428 short rx_skb_complete;
437 dma_addr_t txlpringdma;
438 dma_addr_t txhpringdma;
439 dma_addr_t txnpringdma;
446 struct buffer *txlpbufs;
447 struct buffer *txhpbufs;
448 struct buffer *txnpbufs;
449 struct buffer *txlpbufstail;
450 struct buffer *txhpbufstail;
451 struct buffer *txnpbufstail;
459 dma_addr_t txmapringdma;
460 dma_addr_t txbkpringdma;
461 dma_addr_t txbepringdma;
462 dma_addr_t txvipringdma;
463 dma_addr_t txvopringdma;
464 dma_addr_t txhpringdma;
477 struct buffer *txmapbufs;
478 struct buffer *txbkpbufs;
479 struct buffer *txbepbufs;
480 struct buffer *txvipbufs;
481 struct buffer *txvopbufs;
482 struct buffer *txhpbufs;
483 struct buffer *txmapbufstail;
484 struct buffer *txbkpbufstail;
485 struct buffer *txbepbufstail;
486 struct buffer *txvipbufstail;
487 struct buffer *txvopbufstail;
488 struct buffer *txhpbufstail;
492 //struct tx_pendingbuf txnp_pending;
493 //struct tasklet_struct irq_tx_tasklet;
494 struct tasklet_struct irq_rx_tasklet;
498 /* adhoc/master mode stuff */
499 u32 *txbeaconringtail;
500 dma_addr_t txbeaconringdma;
503 struct buffer *txbeaconbufs;
504 struct buffer *txbeaconbufstail;
505 //char *master_essid;
506 //u16 master_beaconinterval;
507 //u32 master_beaconsize;
508 //u16 beacon_interval;
514 //add for RF power on power off by lizhaoming 080512
515 u8 RegThreeWireMode; // See "Three wire mode" defined above, 2006.05.31, by rcnjko.
518 LED_STRATEGY_8185 LedStrategy;
521 //by amy for power save
522 struct timer_list watch_dog_timer;
524 bool bSwRfProcessing;
525 RT_RF_POWER_STATE eInactivePowerState;
526 RT_RF_POWER_STATE eRFPowerState;
528 bool RFChangeInProgress;
530 bool SetRFPowerStateInProgress;
533 RT_PS_MODE dot11PowerSaveMode;
534 //u32 NumRxOkInPeriod; //YJ,del,080828
535 //u32 NumTxOkInPeriod; //YJ,del,080828
538 bool bApBufOurFrame;// TRUE if AP buffer our unicast data , we will keep eAwake untill receive data or timeout.
539 u8 WaitBufDataBcnCount;
540 u8 WaitBufDataTimeOut;
542 //by amy for power save
544 u8 EEPROMSwAntennaDiversity;
545 bool EEPROMDefaultAntenna1;
546 u8 RegSwAntennaDiversityMechanism;
547 bool bSwAntennaDiverity;
548 u8 RegDefaultAntenna;
549 bool bDefaultAntenna1;
551 long Stats_SignalStrength;
552 long LastSignalStrengthInPercent; // In percentange, used for smoothing, e.g. Moving Average.
553 u8 SignalQuality; // in 0-100 index.
554 long Stats_SignalQuality;
555 long RecvSignalPower; // in dBm.
556 long Stats_RecvSignalPower;
557 u8 LastRxPktAntenna; // +by amy 080312 Antenn which received the lasted packet. 0: Aux, 1:Main. Added by Roger, 2008.01.25.
559 long AdRxSignalStrength;
560 u8 CurrAntennaIndex; // Index to current Antenna (both Tx and Rx).
561 u8 AdTickCount; // Times of SwAntennaDiversityTimer happened.
562 u8 AdCheckPeriod; // # of period SwAntennaDiversityTimer to check Rx signal strength for SW Antenna Diversity.
563 u8 AdMinCheckPeriod; // Min value of AdCheckPeriod.
564 u8 AdMaxCheckPeriod; // Max value of AdCheckPeriod.
565 long AdRxSsThreshold; // Signal strength threshold to switch antenna.
566 long AdMaxRxSsThreshold; // Max value of AdRxSsThreshold.
567 bool bAdSwitchedChecking; // TRUE if we shall shall check Rx signal strength for last time switching antenna.
568 long AdRxSsBeforeSwitched; // Rx signal strength before we swithed antenna.
569 struct timer_list SwAntennaDiversityTimer;
573 // Crystal calibration.
574 // Added by Roger, 2007.12.11.
576 bool bXtalCalibration; // Crystal calibration.
577 u8 XtalCal_Xin; // Crystal calibration for Xin. 0~7.5pF
578 u8 XtalCal_Xout; // Crystal calibration for Xout. 0~7.5pF
580 // Tx power tracking with thermal meter indication.
581 // Added by Roger, 2007.12.11.
583 bool bTxPowerTrack; // Tx Power tracking.
584 u8 ThermalMeter; // Thermal meter reference indication.
586 // Dynamic Initial Gain Adjustment Mechanism. Added by Bruce, 2007-02-14.
588 bool bDigMechanism; // TRUE if DIG is enabled, FALSE ow.
589 bool bRegHighPowerMechanism; // For High Power Mechanism. 061010, by rcnjko.
590 u32 FalseAlarmRegValue;
591 u8 RegDigOfdmFaUpTh; // Upper threhold of OFDM false alarm, which is used in DIG.
592 u8 DIG_NumberFallbackVote;
593 u8 DIG_NumberUpgradeVote;
594 // For HW antenna diversity, added by Roger, 2008.01.30.
595 u32 AdMainAntennaRxOkCnt; // Main antenna Rx OK count.
596 u32 AdAuxAntennaRxOkCnt; // Aux antenna Rx OK count.
597 bool bHWAdSwitched; // TRUE if we has switched default antenna by HW evaluation.
598 // RF High Power upper/lower threshold.
601 // RF RSSI High Power upper/lower Threshold.
602 u8 RegRSSIHiPwrUpperTh;
603 u8 RegRSSIHiPwrLowerTh;
604 // Current CCK RSSI value to determine CCK high power, asked by SD3 DZ, by Bruce, 2007-04-12.
608 // High Power Mechanism. Added by amy, 080312.
611 long UndecoratedSmoothedSS;
612 long UndercorateSmoothedRxPower;
616 //For adjust Dig Threshhold during Legacy/Leisure Power Save Mode
617 u32 DozePeriodInPast2Sec;
618 // Don't access BB/RF under disable PLL situation.
619 u8 InitialGainBackUp;
620 u8 RegBModeGainStage;
621 //by amy for rate adaptive
622 struct timer_list rateadapter_timer;
623 u32 RateAdaptivePeriod;
626 int ForcedDataRate; // Force Data Rate. 0: Auto, 0x02: 1M ~ 0x6C: 54M.)
627 u32 NumTxUnicast; //YJ,add,080828,for keep alive
628 u8 keepAliveLevel; //YJ,add,080828,for KeepAlive
629 unsigned long NumTxOkTotal;
632 unsigned long LastTxokCnt;
633 unsigned long LastRxokCnt;
635 unsigned long LastTxOKBytes;
636 unsigned long NumTxOkBytesTotal;
638 long LastFailTxRateSS;
640 u32 LastTxThroughput;
642 unsigned short bTryuping;
643 u8 CurrTxRate; //the rate before up
646 u8 TryDownCountLowData;
647 u8 TryupingCountNoData;
650 //by amy for rate adaptive
653 // struct workqueue_struct *workqueue;
654 struct work_struct reset_wq;
655 struct work_struct watch_dog_wq;
656 struct work_struct tx_irq_wq;
657 short ack_tx_to_ieee;
663 u8 dma_poll_stop_mask;
665 //u8 RegThreeWireMode;
669 u16 EarlyRxThreshold;
674 struct ChnlAccessSetting ChannelAccessSetting;
677 #define MANAGE_PRIORITY 0
678 #define BK_PRIORITY 1
679 #define BE_PRIORITY 2
680 #define VI_PRIORITY 3
681 #define VO_PRIORITY 4
682 #define HI_PRIORITY 5
683 #define BEACON_PRIORITY 6
685 #define LOW_PRIORITY VI_PRIORITY
686 #define NORM_PRIORITY VO_PRIORITY
688 #define AC2Q(_ac) (((_ac) == WME_AC_VO) ? VO_PRIORITY : \
689 ((_ac) == WME_AC_VI) ? VI_PRIORITY : \
690 ((_ac) == WME_AC_BK) ? BK_PRIORITY : \
693 short rtl8180_tx(struct net_device *dev,u8* skbuf, int len,int priority,
694 short morefrag,short fragdesc,int rate);
696 u8 read_nic_byte(struct net_device *dev, int x);
697 u32 read_nic_dword(struct net_device *dev, int x);
698 u16 read_nic_word(struct net_device *dev, int x) ;
699 void write_nic_byte(struct net_device *dev, int x,u8 y);
700 void write_nic_word(struct net_device *dev, int x,u16 y);
701 void write_nic_dword(struct net_device *dev, int x,u32 y);
702 void force_pci_posting(struct net_device *dev);
704 void rtl8180_rtx_disable(struct net_device *);
705 void rtl8180_rx_enable(struct net_device *);
706 void rtl8180_tx_enable(struct net_device *);
707 void rtl8180_start_scanning(struct net_device *dev);
708 void rtl8180_start_scanning_s(struct net_device *dev);
709 void rtl8180_stop_scanning(struct net_device *dev);
710 void rtl8180_disassociate(struct net_device *dev);
711 //void fix_rx_fifo(struct net_device *dev);
712 void rtl8180_set_anaparam(struct net_device *dev,u32 a);
713 void rtl8185_set_anaparam2(struct net_device *dev,u32 a);
714 void rtl8180_set_hw_wep(struct net_device *dev);
715 void rtl8180_no_hw_wep(struct net_device *dev);
716 void rtl8180_update_msr(struct net_device *dev);
717 //void rtl8180_BSS_create(struct net_device *dev);
718 void rtl8180_beacon_tx_disable(struct net_device *dev);
719 void rtl8180_beacon_rx_disable(struct net_device *dev);
720 void rtl8180_conttx_enable(struct net_device *dev);
721 void rtl8180_conttx_disable(struct net_device *dev);
722 int rtl8180_down(struct net_device *dev);
723 int rtl8180_up(struct net_device *dev);
724 void rtl8180_commit(struct net_device *dev);
725 void rtl8180_set_chan(struct net_device *dev,short ch);
726 void rtl8180_set_master_essid(struct net_device *dev,char *essid);
727 void rtl8180_update_beacon_security(struct net_device *dev);
728 void write_phy(struct net_device *dev, u8 adr, u8 data);
729 void write_phy_cck(struct net_device *dev, u8 adr, u32 data);
730 void write_phy_ofdm(struct net_device *dev, u8 adr, u32 data);
731 void rtl8185_tx_antenna(struct net_device *dev, u8 ant);
732 void rtl8185_rf_pins_enable(struct net_device *dev);
733 void IBSS_randomize_cell(struct net_device *dev);
734 void IPSEnter(struct net_device *dev);
735 void IPSLeave(struct net_device *dev);
736 int get_curr_tx_free_desc(struct net_device *dev, int priority);
737 void UpdateInitialGain(struct net_device *dev);
738 bool SetAntennaConfig87SE(struct net_device *dev, u8 DefaultAnt, bool bAntDiversity);
740 //#ifdef CONFIG_RTL8185B
741 void rtl8185b_adapter_start(struct net_device *dev);
742 void rtl8185b_rx_enable(struct net_device *dev);
743 void rtl8185b_tx_enable(struct net_device *dev);
744 void rtl8180_reset(struct net_device *dev);
745 void rtl8185b_irq_enable(struct net_device *dev);
746 void fix_rx_fifo(struct net_device *dev);
747 void fix_tx_fifo(struct net_device *dev);
748 void rtl8225z2_SetTXPowerLevel(struct net_device *dev, short ch);
749 void rtl8180_rate_adapter(struct work_struct * work);
751 bool MgntActSet_RF_State(struct net_device *dev, RT_RF_POWER_STATE StateToSet, u32 ChangeSource);