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 DEFAULT_RTS_THRESHOLD 2342U
56 #define MIN_RTS_THRESHOLD 0U
57 #define MAX_RTS_THRESHOLD 2342U
58 #define DEFAULT_BEACONINTERVAL 0x64U
60 #define DEFAULT_RETRY_RTS 7
61 #define DEFAULT_RETRY_DATA 7
63 #define BEACON_QUEUE 6
68 #define sAckCtsLng 112 // bits in ACK and CTS frames
70 #define RATE_ADAPTIVE_TIMER_PERIOD 300
72 typedef enum _WIRELESS_MODE
{
73 WIRELESS_MODE_UNKNOWN
= 0x00,
74 WIRELESS_MODE_A
= 0x01,
75 WIRELESS_MODE_B
= 0x02,
76 WIRELESS_MODE_G
= 0x04,
77 WIRELESS_MODE_AUTO
= 0x08,
80 typedef enum _VERSION_8185
{
83 VERSION_8185_C
, // C-cut
84 VERSION_8185_D
, // D-cut
86 VERSION_8185B_B
, // B-cut
87 VERSION_8185B_D
, // D-cut
88 VERSION_8185B_E
, // E-cut
90 VERSION_8187S_B
, // B-cut
91 VERSION_8187S_C
, // C-cut
92 VERSION_8187S_D
, // D-cut
94 }VERSION_8185
,*PVERSION_8185
;
95 typedef struct ChnlAccessSetting
{
102 }*PCHANNEL_ACCESS_SETTING
,CHANNEL_ACCESS_SETTING
;
109 typedef u32 AC_CODING
;
110 #define AC0_BE 0 // ACI: 0x00 // Best Effort
111 #define AC1_BK 1 // ACI: 0x01 // Background
112 #define AC2_VI 2 // ACI: 0x10 // Video
113 #define AC3_VO 3 // ACI: 0x11 // Voice
114 #define AC_MAX 4 // Max: define total number; Should not to be used as a real enum.
117 // ECWmin/ECWmax field.
118 // Ref: WMM spec 2.2.2: WME Parameter Element, p.13.
131 // Ref: WMM spec 2.2.2: WME Parameter Element, p.12.
133 typedef union _ACI_AIFSN
{
143 }ACI_AIFSN
, *PACI_AIFSN
;
146 // AC Parameters Record Format.
147 // Ref: WMM spec 2.2.2: WME Parameter Element, p.12.
149 typedef union _AC_PARAM
{
159 }AC_PARAM
, *PAC_PARAM
;
161 /* it is a wrong definition. -xiong-2006-11-17
162 typedef struct ThreeWireReg {
173 typedef union _ThreeWire
{
174 struct _ThreeWireStruc
{
181 // u2Byte ThreeWireEnable:1;
188 typedef struct buffer
200 unsigned long rxnolast
;
201 unsigned long rxnodata
;
202 // unsigned long rxreset;
203 // unsigned long rxwrkaround;
204 unsigned long rxnopointer
;
205 unsigned long txnperr
;
206 unsigned long txresumed
;
208 unsigned long rxoverflow
;
210 unsigned long txbkpokint
;
211 unsigned long txbepoking
;
212 unsigned long txbkperr
;
213 unsigned long txbeperr
;
214 unsigned long txnpokint
;
215 unsigned long txhpokint
;
216 unsigned long txhperr
;
218 unsigned long shints
;
219 unsigned long txoverflow
;
220 unsigned long rxdmafail
;
221 unsigned long txbeacon
;
222 unsigned long txbeaconerr
;
223 unsigned long txlpokint
;
224 unsigned long txlperr
;
225 unsigned long txretry
;//retry number tony 20060601
226 unsigned long rxcrcerrmin
;//crc error (0-500)
227 unsigned long rxcrcerrmid
;//crc error (500-1000)
228 unsigned long rxcrcerrmax
;//crc error (>1000)
229 unsigned long rxicverr
;//ICV error
232 #define MAX_LD_SLOT_NUM 10
233 #define KEEP_ALIVE_INTERVAL 20 // in seconds.
234 #define CHECK_FOR_HANG_PERIOD 2 //be equal to watchdog check time
235 #define DEFAULT_KEEP_ALIVE_LEVEL 1
236 #define DEFAULT_SLOT_NUM 2
237 #define POWER_PROFILE_AC 0
238 #define POWER_PROFILE_BATTERY 1
240 typedef struct _link_detect_t
242 u32 RxFrameNum
[MAX_LD_SLOT_NUM
]; // number of Rx Frame / CheckForHang_period to determine link status
243 u16 SlotNum
; // number of CheckForHang period to determine link status, default is 2
246 u32 NumTxOkInPeriod
; //number of packet transmitted during CheckForHang
247 u32 NumRxOkInPeriod
; //number of packet received during CheckForHang
249 u8 IdleCount
; // (KEEP_ALIVE_INTERVAL / CHECK_FOR_HANG_PERIOD)
250 u32 LastNumTxUnicast
;
251 u32 LastNumRxUnicast
;
253 bool bBusyTraffic
; //when it is set to 1, UI cann't scan at will.
254 }link_detect_t
, *plink_detect_t
;
256 //YJ,modified,080828,end
259 //================================================================================
260 // LED customization.
261 //================================================================================
263 typedef enum _LED_STRATEGY_8185
{
266 HW_LED
, // HW control 2 LEDs, LED0 and LED1 (there are 4 different control modes)
267 }LED_STRATEGY_8185
, *PLED_STRATEGY_8185
;
269 //by amy for power save
270 typedef enum _LED_CTL_MODE
{
271 LED_CTL_POWER_ON
= 1,
276 LED_CTL_SITE_SURVEY
= 6,
277 LED_CTL_POWER_OFF
= 7
280 typedef enum _RT_RF_POWER_STATE
289 auth_not_valid
= 0x2,
302 // Reason code defined in 802.11i D10.0 p.28.
304 four_way_tmout
= 0x0f,
305 two_way_tmout
= 0x10,
307 invalid_Gcipher
= 0x12,
308 invalid_Pcipher
= 0x13,
310 unsup_RSNIEver
= 0x15,
311 invalid_RSNIE
= 0x16,
312 auth_802_1x_fail
= 0x17,
315 // Reason code defined in 7.3.1.7, 802.1e D13.0, p.42. Added by Annie, 2005-11-15.
316 QoS_unspec
= 0x20, // 32
317 QAP_bandwidth
= 0x21, // 33
318 poor_condition
= 0x22, // 34
319 no_facility
= 0x23, // 35
321 req_declined
= 0x25, // 37
322 invalid_param
= 0x26, // 38
323 req_not_honored
= 0x27, // 39
324 TS_not_created
= 0x2F, // 47
325 DL_not_allowed
= 0x30, // 48
326 dest_not_exist
= 0x31, // 49
327 dest_not_QSTA
= 0x32, // 50
329 typedef enum _RT_PS_MODE
331 eActive
, // Active/Continuous access.
332 eMaxPs
, // Max power save mode.
333 eFastPs
// Fast power save mode.
335 //by amy for power save
336 typedef struct r8180_priv
338 struct pci_dev
*pdev
;
342 struct ieee80211_device
*ieee80211
;
344 short card_8185
; /* O: rtl8180, 1:rtl8185 V B/C, 2:rtl8185 V D, 3:rtl8185B */
345 short card_8185_Bversion
; /* if TCR reports card V B/C this discriminates */
346 short phy_ver
; /* meaningful for rtl8225 1:A 2:B 3:C */
348 enum card_type
{PCI
,MINIPCI
,CARDBUS
,USB
/*rtl8187*/}card_type
;
350 short plcp_preamble_mode
; // 0:auto 1:short 2:long
353 spinlock_t irq_th_lock
;
356 spinlock_t rf_ps_lock
;
360 struct net_device
*dev
;
364 u8 chtxpwr
[15]; //channels from 1 to 14, 0 not used
365 u8 chtxpwr_ofdm
[15]; //channels from 1 to 14, 0 not used
366 //u8 challow[15]; //channels from 1 to 14, 0 not used
367 u8 channel_plan
; // it's the channel plan index
369 short crcmon
; //if 1 allow bad crc frame reception in monitor mode
372 struct timer_list scan_timer
;
375 spinlock_t scan_lock
;
377 //u8 active_scan_num;
378 struct semaphore wx_sem
;
379 struct semaphore rf_state
;
389 short (*rf_set_sens
)(struct net_device
*dev
,short sens
);
390 void (*rf_set_chan
)(struct net_device
*dev
,short ch
);
391 void (*rf_close
)(struct net_device
*dev
);
392 void (*rf_init
)(struct net_device
*dev
);
393 void (*rf_sleep
)(struct net_device
*dev
);
394 void (*rf_wakeup
)(struct net_device
*dev
);
399 struct _link_detect_t link_detect
; //YJ,add,080828
400 struct iw_statistics wstats
;
401 struct proc_dir_entry
*dir_dev
;
406 dma_addr_t rxringdma
;
407 struct buffer
*rxbuffer
;
408 struct buffer
*rxbufferhead
;
412 struct sk_buff
*rx_skb
;
414 short rx_skb_complete
;
423 dma_addr_t txlpringdma;
424 dma_addr_t txhpringdma;
425 dma_addr_t txnpringdma;
432 struct buffer *txlpbufs;
433 struct buffer *txhpbufs;
434 struct buffer *txnpbufs;
435 struct buffer *txlpbufstail;
436 struct buffer *txhpbufstail;
437 struct buffer *txnpbufstail;
445 dma_addr_t txmapringdma
;
446 dma_addr_t txbkpringdma
;
447 dma_addr_t txbepringdma
;
448 dma_addr_t txvipringdma
;
449 dma_addr_t txvopringdma
;
450 dma_addr_t txhpringdma
;
463 struct buffer
*txmapbufs
;
464 struct buffer
*txbkpbufs
;
465 struct buffer
*txbepbufs
;
466 struct buffer
*txvipbufs
;
467 struct buffer
*txvopbufs
;
468 struct buffer
*txhpbufs
;
469 struct buffer
*txmapbufstail
;
470 struct buffer
*txbkpbufstail
;
471 struct buffer
*txbepbufstail
;
472 struct buffer
*txvipbufstail
;
473 struct buffer
*txvopbufstail
;
474 struct buffer
*txhpbufstail
;
478 //struct tx_pendingbuf txnp_pending;
479 //struct tasklet_struct irq_tx_tasklet;
480 struct tasklet_struct irq_rx_tasklet
;
484 /* adhoc/master mode stuff */
485 u32
*txbeaconringtail
;
486 dma_addr_t txbeaconringdma
;
489 struct buffer
*txbeaconbufs
;
490 struct buffer
*txbeaconbufstail
;
491 //char *master_essid;
492 //u16 master_beaconinterval;
493 //u32 master_beaconsize;
494 //u16 beacon_interval;
500 //add for RF power on power off by lizhaoming 080512
501 u8 RegThreeWireMode
; // See "Three wire mode" defined above, 2006.05.31, by rcnjko.
504 LED_STRATEGY_8185 LedStrategy
;
507 //by amy for power save
508 struct timer_list watch_dog_timer
;
510 bool bSwRfProcessing
;
511 RT_RF_POWER_STATE eInactivePowerState
;
512 RT_RF_POWER_STATE eRFPowerState
;
514 bool RFChangeInProgress
;
516 bool SetRFPowerStateInProgress
;
519 RT_PS_MODE dot11PowerSaveMode
;
520 //u32 NumRxOkInPeriod; //YJ,del,080828
521 //u32 NumTxOkInPeriod; //YJ,del,080828
524 bool bApBufOurFrame
;// TRUE if AP buffer our unicast data , we will keep eAwake untill receive data or timeout.
525 u8 WaitBufDataBcnCount
;
526 u8 WaitBufDataTimeOut
;
528 //by amy for power save
530 u8 EEPROMSwAntennaDiversity
;
531 bool EEPROMDefaultAntenna1
;
532 u8 RegSwAntennaDiversityMechanism
;
533 bool bSwAntennaDiverity
;
534 u8 RegDefaultAntenna
;
535 bool bDefaultAntenna1
;
537 long Stats_SignalStrength
;
538 long LastSignalStrengthInPercent
; // In percentange, used for smoothing, e.g. Moving Average.
539 u8 SignalQuality
; // in 0-100 index.
540 long Stats_SignalQuality
;
541 long RecvSignalPower
; // in dBm.
542 long Stats_RecvSignalPower
;
543 u8 LastRxPktAntenna
; // +by amy 080312 Antenn which received the lasted packet. 0: Aux, 1:Main. Added by Roger, 2008.01.25.
545 long AdRxSignalStrength
;
546 u8 CurrAntennaIndex
; // Index to current Antenna (both Tx and Rx).
547 u8 AdTickCount
; // Times of SwAntennaDiversityTimer happened.
548 u8 AdCheckPeriod
; // # of period SwAntennaDiversityTimer to check Rx signal strength for SW Antenna Diversity.
549 u8 AdMinCheckPeriod
; // Min value of AdCheckPeriod.
550 u8 AdMaxCheckPeriod
; // Max value of AdCheckPeriod.
551 long AdRxSsThreshold
; // Signal strength threshold to switch antenna.
552 long AdMaxRxSsThreshold
; // Max value of AdRxSsThreshold.
553 bool bAdSwitchedChecking
; // TRUE if we shall shall check Rx signal strength for last time switching antenna.
554 long AdRxSsBeforeSwitched
; // Rx signal strength before we swithed antenna.
555 struct timer_list SwAntennaDiversityTimer
;
559 // Crystal calibration.
560 // Added by Roger, 2007.12.11.
562 bool bXtalCalibration
; // Crystal calibration.
563 u8 XtalCal_Xin
; // Crystal calibration for Xin. 0~7.5pF
564 u8 XtalCal_Xout
; // Crystal calibration for Xout. 0~7.5pF
566 // Tx power tracking with thermal meter indication.
567 // Added by Roger, 2007.12.11.
569 bool bTxPowerTrack
; // Tx Power tracking.
570 u8 ThermalMeter
; // Thermal meter reference indication.
572 // Dynamic Initial Gain Adjustment Mechanism. Added by Bruce, 2007-02-14.
574 bool bDigMechanism
; // TRUE if DIG is enabled, FALSE ow.
575 bool bRegHighPowerMechanism
; // For High Power Mechanism. 061010, by rcnjko.
576 u32 FalseAlarmRegValue
;
577 u8 RegDigOfdmFaUpTh
; // Upper threhold of OFDM false alarm, which is used in DIG.
578 u8 DIG_NumberFallbackVote
;
579 u8 DIG_NumberUpgradeVote
;
580 // For HW antenna diversity, added by Roger, 2008.01.30.
581 u32 AdMainAntennaRxOkCnt
; // Main antenna Rx OK count.
582 u32 AdAuxAntennaRxOkCnt
; // Aux antenna Rx OK count.
583 bool bHWAdSwitched
; // TRUE if we has switched default antenna by HW evaluation.
584 // RF High Power upper/lower threshold.
587 // RF RSSI High Power upper/lower Threshold.
588 u8 RegRSSIHiPwrUpperTh
;
589 u8 RegRSSIHiPwrLowerTh
;
590 // Current CCK RSSI value to determine CCK high power, asked by SD3 DZ, by Bruce, 2007-04-12.
594 // High Power Mechanism. Added by amy, 080312.
597 long UndecoratedSmoothedSS
;
598 long UndercorateSmoothedRxPower
;
602 //For adjust Dig Threshhold during Legacy/Leisure Power Save Mode
603 u32 DozePeriodInPast2Sec
;
604 // Don't access BB/RF under disable PLL situation.
605 u8 InitialGainBackUp
;
606 u8 RegBModeGainStage
;
607 //by amy for rate adaptive
608 struct timer_list rateadapter_timer
;
609 u32 RateAdaptivePeriod
;
612 int ForcedDataRate
; // Force Data Rate. 0: Auto, 0x02: 1M ~ 0x6C: 54M.)
613 u32 NumTxUnicast
; //YJ,add,080828,for keep alive
614 u8 keepAliveLevel
; //YJ,add,080828,for KeepAlive
615 unsigned long NumTxOkTotal
;
618 unsigned long LastTxokCnt
;
619 unsigned long LastRxokCnt
;
621 unsigned long LastTxOKBytes
;
622 unsigned long NumTxOkBytesTotal
;
624 long LastFailTxRateSS
;
626 u32 LastTxThroughput
;
628 unsigned short bTryuping
;
629 u8 CurrTxRate
; //the rate before up
632 u8 TryDownCountLowData
;
633 u8 TryupingCountNoData
;
636 //by amy for rate adaptive
639 // struct workqueue_struct *workqueue;
640 struct work_struct reset_wq
;
641 struct work_struct watch_dog_wq
;
642 struct work_struct tx_irq_wq
;
643 short ack_tx_to_ieee
;
649 u8 dma_poll_stop_mask
;
651 //u8 RegThreeWireMode;
655 u16 EarlyRxThreshold
;
660 struct ChnlAccessSetting ChannelAccessSetting
;
663 #define MANAGE_PRIORITY 0
664 #define BK_PRIORITY 1
665 #define BE_PRIORITY 2
666 #define VI_PRIORITY 3
667 #define VO_PRIORITY 4
668 #define HI_PRIORITY 5
669 #define BEACON_PRIORITY 6
671 #define LOW_PRIORITY VI_PRIORITY
672 #define NORM_PRIORITY VO_PRIORITY
674 #define AC2Q(_ac) (((_ac) == WME_AC_VO) ? VO_PRIORITY : \
675 ((_ac) == WME_AC_VI) ? VI_PRIORITY : \
676 ((_ac) == WME_AC_BK) ? BK_PRIORITY : \
679 short rtl8180_tx(struct net_device
*dev
,u8
* skbuf
, int len
,int priority
,
680 short morefrag
,short fragdesc
,int rate
);
682 u8
read_nic_byte(struct net_device
*dev
, int x
);
683 u32
read_nic_dword(struct net_device
*dev
, int x
);
684 u16
read_nic_word(struct net_device
*dev
, int x
) ;
685 void write_nic_byte(struct net_device
*dev
, int x
,u8 y
);
686 void write_nic_word(struct net_device
*dev
, int x
,u16 y
);
687 void write_nic_dword(struct net_device
*dev
, int x
,u32 y
);
688 void force_pci_posting(struct net_device
*dev
);
690 void rtl8180_rtx_disable(struct net_device
*);
691 void rtl8180_rx_enable(struct net_device
*);
692 void rtl8180_tx_enable(struct net_device
*);
693 void rtl8180_start_scanning(struct net_device
*dev
);
694 void rtl8180_start_scanning_s(struct net_device
*dev
);
695 void rtl8180_stop_scanning(struct net_device
*dev
);
696 void rtl8180_disassociate(struct net_device
*dev
);
697 //void fix_rx_fifo(struct net_device *dev);
698 void rtl8180_set_anaparam(struct net_device
*dev
,u32 a
);
699 void rtl8185_set_anaparam2(struct net_device
*dev
,u32 a
);
700 void rtl8180_set_hw_wep(struct net_device
*dev
);
701 void rtl8180_no_hw_wep(struct net_device
*dev
);
702 void rtl8180_update_msr(struct net_device
*dev
);
703 //void rtl8180_BSS_create(struct net_device *dev);
704 void rtl8180_beacon_tx_disable(struct net_device
*dev
);
705 void rtl8180_beacon_rx_disable(struct net_device
*dev
);
706 void rtl8180_conttx_enable(struct net_device
*dev
);
707 void rtl8180_conttx_disable(struct net_device
*dev
);
708 int rtl8180_down(struct net_device
*dev
);
709 int rtl8180_up(struct net_device
*dev
);
710 void rtl8180_commit(struct net_device
*dev
);
711 void rtl8180_set_chan(struct net_device
*dev
,short ch
);
712 void rtl8180_set_master_essid(struct net_device
*dev
,char *essid
);
713 void rtl8180_update_beacon_security(struct net_device
*dev
);
714 void write_phy(struct net_device
*dev
, u8 adr
, u8 data
);
715 void write_phy_cck(struct net_device
*dev
, u8 adr
, u32 data
);
716 void write_phy_ofdm(struct net_device
*dev
, u8 adr
, u32 data
);
717 void rtl8185_tx_antenna(struct net_device
*dev
, u8 ant
);
718 void rtl8185_rf_pins_enable(struct net_device
*dev
);
719 void IBSS_randomize_cell(struct net_device
*dev
);
720 void IPSEnter(struct net_device
*dev
);
721 void IPSLeave(struct net_device
*dev
);
722 int get_curr_tx_free_desc(struct net_device
*dev
, int priority
);
723 void UpdateInitialGain(struct net_device
*dev
);
724 bool SetAntennaConfig87SE(struct net_device
*dev
, u8 DefaultAnt
, bool bAntDiversity
);
726 //#ifdef CONFIG_RTL8185B
727 void rtl8185b_adapter_start(struct net_device
*dev
);
728 void rtl8185b_rx_enable(struct net_device
*dev
);
729 void rtl8185b_tx_enable(struct net_device
*dev
);
730 void rtl8180_reset(struct net_device
*dev
);
731 void rtl8185b_irq_enable(struct net_device
*dev
);
732 void fix_rx_fifo(struct net_device
*dev
);
733 void fix_tx_fifo(struct net_device
*dev
);
734 void rtl8225z2_SetTXPowerLevel(struct net_device
*dev
, short ch
);
735 void rtl8180_rate_adapter(struct work_struct
* work
);
737 bool MgntActSet_RF_State(struct net_device
*dev
, RT_RF_POWER_STATE StateToSet
, u32 ChangeSource
);