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.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
67 #ifdef CONFIG_RTL8185B
75 #define BEACON_QUEUE 6
77 #define LOW_QUEUE BE_QUEUE
78 #define NORMAL_QUEUE MGNT_QUEUE
83 #define sAckCtsLng 112 // bits in ACK and CTS frames
85 #define RATE_ADAPTIVE_TIMER_PERIOD 300
87 typedef enum _WIRELESS_MODE {
88 WIRELESS_MODE_UNKNOWN = 0x00,
89 WIRELESS_MODE_A = 0x01,
90 WIRELESS_MODE_B = 0x02,
91 WIRELESS_MODE_G = 0x04,
92 WIRELESS_MODE_AUTO = 0x08,
95 typedef enum _VERSION_8185{
98 VERSION_8185_C, // C-cut
99 VERSION_8185_D, // D-cut
101 VERSION_8185B_B, // B-cut
102 VERSION_8185B_D, // D-cut
103 VERSION_8185B_E, // E-cut
105 VERSION_8187S_B, // B-cut
106 VERSION_8187S_C, // C-cut
107 VERSION_8187S_D, // D-cut
109 }VERSION_8185,*PVERSION_8185;
110 typedef struct ChnlAccessSetting {
117 }*PCHANNEL_ACCESS_SETTING,CHANNEL_ACCESS_SETTING;
124 typedef u32 AC_CODING;
125 #define AC0_BE 0 // ACI: 0x00 // Best Effort
126 #define AC1_BK 1 // ACI: 0x01 // Background
127 #define AC2_VI 2 // ACI: 0x10 // Video
128 #define AC3_VO 3 // ACI: 0x11 // Voice
129 #define AC_MAX 4 // Max: define total number; Should not to be used as a real enum.
132 // ECWmin/ECWmax field.
133 // Ref: WMM spec 2.2.2: WME Parameter Element, p.13.
146 // Ref: WMM spec 2.2.2: WME Parameter Element, p.12.
148 typedef union _ACI_AIFSN{
158 }ACI_AIFSN, *PACI_AIFSN;
161 // AC Parameters Record Format.
162 // Ref: WMM spec 2.2.2: WME Parameter Element, p.12.
164 typedef union _AC_PARAM{
174 }AC_PARAM, *PAC_PARAM;
176 /* it is a wrong definition. -xiong-2006-11-17
177 typedef struct ThreeWireReg {
188 typedef union _ThreeWire{
189 struct _ThreeWireStruc{
196 // u2Byte ThreeWireEnable:1;
204 typedef struct buffer
216 unsigned long rxnolast;
217 unsigned long rxnodata;
218 // unsigned long rxreset;
219 // unsigned long rxwrkaround;
220 unsigned long rxnopointer;
221 unsigned long txnperr;
222 unsigned long txresumed;
224 unsigned long rxoverflow;
226 unsigned long txbkpokint;
227 unsigned long txbepoking;
228 unsigned long txbkperr;
229 unsigned long txbeperr;
230 unsigned long txnpokint;
231 unsigned long txhpokint;
232 unsigned long txhperr;
234 unsigned long shints;
235 unsigned long txoverflow;
236 unsigned long rxdmafail;
237 unsigned long txbeacon;
238 unsigned long txbeaconerr;
239 unsigned long txlpokint;
240 unsigned long txlperr;
241 unsigned long txretry;//retry number tony 20060601
242 unsigned long rxcrcerrmin;//crc error (0-500)
243 unsigned long rxcrcerrmid;//crc error (500-1000)
244 unsigned long rxcrcerrmax;//crc error (>1000)
245 unsigned long rxicverr;//ICV error
248 #define MAX_LD_SLOT_NUM 10
249 #define KEEP_ALIVE_INTERVAL 20 // in seconds.
250 #define CHECK_FOR_HANG_PERIOD 2 //be equal to watchdog check time
251 #define DEFAULT_KEEP_ALIVE_LEVEL 1
252 #define DEFAULT_SLOT_NUM 2
253 #define POWER_PROFILE_AC 0
254 #define POWER_PROFILE_BATTERY 1
256 typedef struct _link_detect_t
258 u32 RxFrameNum[MAX_LD_SLOT_NUM]; // number of Rx Frame / CheckForHang_period to determine link status
259 u16 SlotNum; // number of CheckForHang period to determine link status, default is 2
262 u32 NumTxOkInPeriod; //number of packet transmitted during CheckForHang
263 u32 NumRxOkInPeriod; //number of packet received during CheckForHang
265 u8 IdleCount; // (KEEP_ALIVE_INTERVAL / CHECK_FOR_HANG_PERIOD)
266 u32 LastNumTxUnicast;
267 u32 LastNumRxUnicast;
269 bool bBusyTraffic; //when it is set to 1, UI cann't scan at will.
270 }link_detect_t, *plink_detect_t;
272 //YJ,modified,080828,end
275 //================================================================================
276 // LED customization.
277 //================================================================================
279 typedef enum _LED_STRATEGY_8185{
282 HW_LED, // HW control 2 LEDs, LED0 and LED1 (there are 4 different control modes)
283 }LED_STRATEGY_8185, *PLED_STRATEGY_8185;
285 //by amy for power save
286 typedef enum _LED_CTL_MODE{
287 LED_CTL_POWER_ON = 1,
292 LED_CTL_SITE_SURVEY = 6,
293 LED_CTL_POWER_OFF = 7
296 typedef enum _RT_RF_POWER_STATE
305 auth_not_valid = 0x2,
318 // Reason code defined in 802.11i D10.0 p.28.
320 four_way_tmout = 0x0f,
321 two_way_tmout = 0x10,
323 invalid_Gcipher = 0x12,
324 invalid_Pcipher = 0x13,
326 unsup_RSNIEver = 0x15,
327 invalid_RSNIE = 0x16,
328 auth_802_1x_fail= 0x17,
331 // Reason code defined in 7.3.1.7, 802.1e D13.0, p.42. Added by Annie, 2005-11-15.
332 QoS_unspec = 0x20, // 32
333 QAP_bandwidth = 0x21, // 33
334 poor_condition = 0x22, // 34
335 no_facility = 0x23, // 35
337 req_declined = 0x25, // 37
338 invalid_param = 0x26, // 38
339 req_not_honored= 0x27, // 39
340 TS_not_created = 0x2F, // 47
341 DL_not_allowed = 0x30, // 48
342 dest_not_exist = 0x31, // 49
343 dest_not_QSTA = 0x32, // 50
345 typedef enum _RT_PS_MODE
347 eActive, // Active/Continuous access.
348 eMaxPs, // Max power save mode.
349 eFastPs // Fast power save mode.
351 //by amy for power save
352 typedef struct r8180_priv
354 struct pci_dev *pdev;
358 struct ieee80211_device *ieee80211;
360 short card_8185; /* O: rtl8180, 1:rtl8185 V B/C, 2:rtl8185 V D, 3:rtl8185B */
361 short card_8185_Bversion; /* if TCR reports card V B/C this discriminates */
362 short phy_ver; /* meaningful for rtl8225 1:A 2:B 3:C */
364 enum card_type {PCI,MINIPCI,CARDBUS,USB/*rtl8187*/}card_type;
366 short plcp_preamble_mode; // 0:auto 1:short 2:long
369 spinlock_t irq_th_lock;
372 spinlock_t rf_ps_lock;
376 struct net_device *dev;
380 u8 chtxpwr[15]; //channels from 1 to 14, 0 not used
381 u8 chtxpwr_ofdm[15]; //channels from 1 to 14, 0 not used
382 //u8 challow[15]; //channels from 1 to 14, 0 not used
383 u8 channel_plan; // it's the channel plan index
385 short crcmon; //if 1 allow bad crc frame reception in monitor mode
388 struct timer_list scan_timer;
391 spinlock_t scan_lock;
393 //u8 active_scan_num;
394 struct semaphore wx_sem;
395 struct semaphore rf_state;
405 short (*rf_set_sens)(struct net_device *dev,short sens);
406 void (*rf_set_chan)(struct net_device *dev,short ch);
407 void (*rf_close)(struct net_device *dev);
408 void (*rf_init)(struct net_device *dev);
409 void (*rf_sleep)(struct net_device *dev);
410 void (*rf_wakeup)(struct net_device *dev);
415 struct _link_detect_t link_detect; //YJ,add,080828
416 struct iw_statistics wstats;
417 struct proc_dir_entry *dir_dev;
422 dma_addr_t rxringdma;
423 struct buffer *rxbuffer;
424 struct buffer *rxbufferhead;
428 struct sk_buff *rx_skb;
430 short rx_skb_complete;
439 dma_addr_t txlpringdma;
440 dma_addr_t txhpringdma;
441 dma_addr_t txnpringdma;
448 struct buffer *txlpbufs;
449 struct buffer *txhpbufs;
450 struct buffer *txnpbufs;
451 struct buffer *txlpbufstail;
452 struct buffer *txhpbufstail;
453 struct buffer *txnpbufstail;
461 dma_addr_t txmapringdma;
462 dma_addr_t txbkpringdma;
463 dma_addr_t txbepringdma;
464 dma_addr_t txvipringdma;
465 dma_addr_t txvopringdma;
466 dma_addr_t txhpringdma;
479 struct buffer *txmapbufs;
480 struct buffer *txbkpbufs;
481 struct buffer *txbepbufs;
482 struct buffer *txvipbufs;
483 struct buffer *txvopbufs;
484 struct buffer *txhpbufs;
485 struct buffer *txmapbufstail;
486 struct buffer *txbkpbufstail;
487 struct buffer *txbepbufstail;
488 struct buffer *txvipbufstail;
489 struct buffer *txvopbufstail;
490 struct buffer *txhpbufstail;
494 //struct tx_pendingbuf txnp_pending;
495 //struct tasklet_struct irq_tx_tasklet;
496 struct tasklet_struct irq_rx_tasklet;
500 /* adhoc/master mode stuff */
501 u32 *txbeaconringtail;
502 dma_addr_t txbeaconringdma;
505 struct buffer *txbeaconbufs;
506 struct buffer *txbeaconbufstail;
507 //char *master_essid;
508 //u16 master_beaconinterval;
509 //u32 master_beaconsize;
510 //u16 beacon_interval;
516 //add for RF power on power off by lizhaoming 080512
517 u8 RegThreeWireMode; // See "Three wire mode" defined above, 2006.05.31, by rcnjko.
520 LED_STRATEGY_8185 LedStrategy;
523 //by amy for power save
524 struct timer_list watch_dog_timer;
526 bool bSwRfProcessing;
527 RT_RF_POWER_STATE eInactivePowerState;
528 RT_RF_POWER_STATE eRFPowerState;
530 bool RFChangeInProgress;
532 bool SetRFPowerStateInProgress;
535 RT_PS_MODE dot11PowerSaveMode;
536 //u32 NumRxOkInPeriod; //YJ,del,080828
537 //u32 NumTxOkInPeriod; //YJ,del,080828
540 bool bApBufOurFrame;// TRUE if AP buffer our unicast data , we will keep eAwake untill receive data or timeout.
541 u8 WaitBufDataBcnCount;
542 u8 WaitBufDataTimeOut;
544 //by amy for power save
546 u8 EEPROMSwAntennaDiversity;
547 bool EEPROMDefaultAntenna1;
548 u8 RegSwAntennaDiversityMechanism;
549 bool bSwAntennaDiverity;
550 u8 RegDefaultAntenna;
551 bool bDefaultAntenna1;
553 long Stats_SignalStrength;
554 long LastSignalStrengthInPercent; // In percentange, used for smoothing, e.g. Moving Average.
555 u8 SignalQuality; // in 0-100 index.
556 long Stats_SignalQuality;
557 long RecvSignalPower; // in dBm.
558 long Stats_RecvSignalPower;
559 u8 LastRxPktAntenna; // +by amy 080312 Antenn which received the lasted packet. 0: Aux, 1:Main. Added by Roger, 2008.01.25.
561 long AdRxSignalStrength;
562 u8 CurrAntennaIndex; // Index to current Antenna (both Tx and Rx).
563 u8 AdTickCount; // Times of SwAntennaDiversityTimer happened.
564 u8 AdCheckPeriod; // # of period SwAntennaDiversityTimer to check Rx signal strength for SW Antenna Diversity.
565 u8 AdMinCheckPeriod; // Min value of AdCheckPeriod.
566 u8 AdMaxCheckPeriod; // Max value of AdCheckPeriod.
567 long AdRxSsThreshold; // Signal strength threshold to switch antenna.
568 long AdMaxRxSsThreshold; // Max value of AdRxSsThreshold.
569 bool bAdSwitchedChecking; // TRUE if we shall shall check Rx signal strength for last time switching antenna.
570 long AdRxSsBeforeSwitched; // Rx signal strength before we swithed antenna.
571 struct timer_list SwAntennaDiversityTimer;
575 // Crystal calibration.
576 // Added by Roger, 2007.12.11.
578 bool bXtalCalibration; // Crystal calibration.
579 u8 XtalCal_Xin; // Crystal calibration for Xin. 0~7.5pF
580 u8 XtalCal_Xout; // Crystal calibration for Xout. 0~7.5pF
582 // Tx power tracking with thermal meter indication.
583 // Added by Roger, 2007.12.11.
585 bool bTxPowerTrack; // Tx Power tracking.
586 u8 ThermalMeter; // Thermal meter reference indication.
588 // Dynamic Initial Gain Adjustment Mechanism. Added by Bruce, 2007-02-14.
590 bool bDigMechanism; // TRUE if DIG is enabled, FALSE ow.
591 bool bRegHighPowerMechanism; // For High Power Mechanism. 061010, by rcnjko.
592 u32 FalseAlarmRegValue;
593 u8 RegDigOfdmFaUpTh; // Upper threhold of OFDM false alarm, which is used in DIG.
594 u8 DIG_NumberFallbackVote;
595 u8 DIG_NumberUpgradeVote;
596 // For HW antenna diversity, added by Roger, 2008.01.30.
597 u32 AdMainAntennaRxOkCnt; // Main antenna Rx OK count.
598 u32 AdAuxAntennaRxOkCnt; // Aux antenna Rx OK count.
599 bool bHWAdSwitched; // TRUE if we has switched default antenna by HW evaluation.
600 // RF High Power upper/lower threshold.
603 // RF RSSI High Power upper/lower Threshold.
604 u8 RegRSSIHiPwrUpperTh;
605 u8 RegRSSIHiPwrLowerTh;
606 // Current CCK RSSI value to determine CCK high power, asked by SD3 DZ, by Bruce, 2007-04-12.
610 // High Power Mechanism. Added by amy, 080312.
613 long UndecoratedSmoothedSS;
614 long UndercorateSmoothedRxPower;
618 //For adjust Dig Threshhold during Legacy/Leisure Power Save Mode
619 u32 DozePeriodInPast2Sec;
620 // Don't access BB/RF under disable PLL situation.
621 u8 InitialGainBackUp;
622 u8 RegBModeGainStage;
623 //by amy for rate adaptive
624 struct timer_list rateadapter_timer;
625 u32 RateAdaptivePeriod;
628 int ForcedDataRate; // Force Data Rate. 0: Auto, 0x02: 1M ~ 0x6C: 54M.)
629 u32 NumTxUnicast; //YJ,add,080828,for keep alive
630 u8 keepAliveLevel; //YJ,add,080828,for KeepAlive
631 unsigned long NumTxOkTotal;
634 unsigned long LastTxokCnt;
635 unsigned long LastRxokCnt;
637 unsigned long LastTxOKBytes;
638 unsigned long NumTxOkBytesTotal;
640 long LastFailTxRateSS;
642 u32 LastTxThroughput;
644 unsigned short bTryuping;
645 u8 CurrTxRate; //the rate before up
648 u8 TryDownCountLowData;
649 u8 TryupingCountNoData;
652 //by amy for rate adaptive
655 // struct workqueue_struct *workqueue;
656 struct work_struct reset_wq;
657 struct work_struct watch_dog_wq;
658 struct work_struct tx_irq_wq;
659 short ack_tx_to_ieee;
662 #ifdef CONFIG_RTL8185B
666 u8 dma_poll_stop_mask;
668 //u8 RegThreeWireMode;
672 u16 EarlyRxThreshold;
677 struct ChnlAccessSetting ChannelAccessSetting;
681 #define MANAGE_PRIORITY 0
682 #define BK_PRIORITY 1
683 #define BE_PRIORITY 2
684 #define VI_PRIORITY 3
685 #define VO_PRIORITY 4
686 #define HI_PRIORITY 5
687 #define BEACON_PRIORITY 6
689 #define LOW_PRIORITY VI_PRIORITY
690 #define NORM_PRIORITY VO_PRIORITY
692 #define AC2Q(_ac) (((_ac) == WME_AC_VO) ? VO_PRIORITY : \
693 ((_ac) == WME_AC_VI) ? VI_PRIORITY : \
694 ((_ac) == WME_AC_BK) ? BK_PRIORITY : \
697 short rtl8180_tx(struct net_device *dev,u8* skbuf, int len,int priority,
698 short morefrag,short fragdesc,int rate);
700 u8 read_nic_byte(struct net_device *dev, int x);
701 u32 read_nic_dword(struct net_device *dev, int x);
702 u16 read_nic_word(struct net_device *dev, int x) ;
703 void write_nic_byte(struct net_device *dev, int x,u8 y);
704 void write_nic_word(struct net_device *dev, int x,u16 y);
705 void write_nic_dword(struct net_device *dev, int x,u32 y);
706 void force_pci_posting(struct net_device *dev);
708 void rtl8180_rtx_disable(struct net_device *);
709 void rtl8180_rx_enable(struct net_device *);
710 void rtl8180_tx_enable(struct net_device *);
711 void rtl8180_start_scanning(struct net_device *dev);
712 void rtl8180_start_scanning_s(struct net_device *dev);
713 void rtl8180_stop_scanning(struct net_device *dev);
714 void rtl8180_disassociate(struct net_device *dev);
715 //void fix_rx_fifo(struct net_device *dev);
716 void rtl8180_set_anaparam(struct net_device *dev,u32 a);
717 void rtl8185_set_anaparam2(struct net_device *dev,u32 a);
718 void rtl8180_set_hw_wep(struct net_device *dev);
719 void rtl8180_no_hw_wep(struct net_device *dev);
720 void rtl8180_update_msr(struct net_device *dev);
721 //void rtl8180_BSS_create(struct net_device *dev);
722 void rtl8180_beacon_tx_disable(struct net_device *dev);
723 void rtl8180_beacon_rx_disable(struct net_device *dev);
724 void rtl8180_conttx_enable(struct net_device *dev);
725 void rtl8180_conttx_disable(struct net_device *dev);
726 int rtl8180_down(struct net_device *dev);
727 int rtl8180_up(struct net_device *dev);
728 void rtl8180_commit(struct net_device *dev);
729 void rtl8180_set_chan(struct net_device *dev,short ch);
730 void rtl8180_set_master_essid(struct net_device *dev,char *essid);
731 void rtl8180_update_beacon_security(struct net_device *dev);
732 void write_phy(struct net_device *dev, u8 adr, u8 data);
733 void write_phy_cck(struct net_device *dev, u8 adr, u32 data);
734 void write_phy_ofdm(struct net_device *dev, u8 adr, u32 data);
735 void rtl8185_tx_antenna(struct net_device *dev, u8 ant);
736 void rtl8185_rf_pins_enable(struct net_device *dev);
737 void IBSS_randomize_cell(struct net_device *dev);
738 void IPSEnter(struct net_device *dev);
739 void IPSLeave(struct net_device *dev);
740 int get_curr_tx_free_desc(struct net_device *dev, int priority);
741 void UpdateInitialGain(struct net_device *dev);
742 bool SetAntennaConfig87SE(struct net_device *dev, u8 DefaultAnt, bool bAntDiversity);
744 //#ifdef CONFIG_RTL8185B
745 void rtl8185b_adapter_start(struct net_device *dev);
746 void rtl8185b_rx_enable(struct net_device *dev);
747 void rtl8185b_tx_enable(struct net_device *dev);
748 void rtl8180_reset(struct net_device *dev);
749 void rtl8185b_irq_enable(struct net_device *dev);
750 void fix_rx_fifo(struct net_device *dev);
751 void fix_tx_fifo(struct net_device *dev);
752 void rtl8225z2_SetTXPowerLevel(struct net_device *dev, short ch);
753 #if LINUX_VERSION_CODE >=KERNEL_VERSION(2,6,20)
754 void rtl8180_rate_adapter(struct work_struct * work);
756 void rtl8180_rate_adapter(struct net_device *dev);
759 bool MgntActSet_RF_State(struct net_device *dev, RT_RF_POWER_STATE StateToSet, u32 ChangeSource);