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 struct ChnlAccessSetting
{
87 }*PCHANNEL_ACCESS_SETTING
,CHANNEL_ACCESS_SETTING
;
94 typedef u32 AC_CODING
;
95 #define AC0_BE 0 // ACI: 0x00 // Best Effort
96 #define AC1_BK 1 // ACI: 0x01 // Background
97 #define AC2_VI 2 // ACI: 0x10 // Video
98 #define AC3_VO 3 // ACI: 0x11 // Voice
99 #define AC_MAX 4 // Max: define total number; Should not to be used as a real enum.
102 // ECWmin/ECWmax field.
103 // Ref: WMM spec 2.2.2: WME Parameter Element, p.13.
116 // Ref: WMM spec 2.2.2: WME Parameter Element, p.12.
118 typedef union _ACI_AIFSN
{
128 }ACI_AIFSN
, *PACI_AIFSN
;
131 // AC Parameters Record Format.
132 // Ref: WMM spec 2.2.2: WME Parameter Element, p.12.
134 typedef union _AC_PARAM
{
144 }AC_PARAM
, *PAC_PARAM
;
146 /* it is a wrong definition. -xiong-2006-11-17
147 typedef struct ThreeWireReg {
158 typedef union _ThreeWire
{
159 struct _ThreeWireStruc
{
166 // u2Byte ThreeWireEnable:1;
173 typedef struct buffer
185 unsigned long rxnolast
;
186 unsigned long rxnodata
;
187 // unsigned long rxreset;
188 // unsigned long rxwrkaround;
189 unsigned long rxnopointer
;
190 unsigned long txnperr
;
191 unsigned long txresumed
;
193 unsigned long rxoverflow
;
195 unsigned long txbkpokint
;
196 unsigned long txbepoking
;
197 unsigned long txbkperr
;
198 unsigned long txbeperr
;
199 unsigned long txnpokint
;
200 unsigned long txhpokint
;
201 unsigned long txhperr
;
203 unsigned long shints
;
204 unsigned long txoverflow
;
205 unsigned long rxdmafail
;
206 unsigned long txbeacon
;
207 unsigned long txbeaconerr
;
208 unsigned long txlpokint
;
209 unsigned long txlperr
;
210 unsigned long txretry
;//retry number tony 20060601
211 unsigned long rxcrcerrmin
;//crc error (0-500)
212 unsigned long rxcrcerrmid
;//crc error (500-1000)
213 unsigned long rxcrcerrmax
;//crc error (>1000)
214 unsigned long rxicverr
;//ICV error
217 #define MAX_LD_SLOT_NUM 10
218 #define KEEP_ALIVE_INTERVAL 20 // in seconds.
219 #define CHECK_FOR_HANG_PERIOD 2 //be equal to watchdog check time
220 #define DEFAULT_KEEP_ALIVE_LEVEL 1
221 #define DEFAULT_SLOT_NUM 2
222 #define POWER_PROFILE_AC 0
223 #define POWER_PROFILE_BATTERY 1
225 typedef struct _link_detect_t
227 u32 RxFrameNum
[MAX_LD_SLOT_NUM
]; // number of Rx Frame / CheckForHang_period to determine link status
228 u16 SlotNum
; // number of CheckForHang period to determine link status, default is 2
231 u32 NumTxOkInPeriod
; //number of packet transmitted during CheckForHang
232 u32 NumRxOkInPeriod
; //number of packet received during CheckForHang
234 u8 IdleCount
; // (KEEP_ALIVE_INTERVAL / CHECK_FOR_HANG_PERIOD)
235 u32 LastNumTxUnicast
;
236 u32 LastNumRxUnicast
;
238 bool bBusyTraffic
; //when it is set to 1, UI cann't scan at will.
239 }link_detect_t
, *plink_detect_t
;
241 //YJ,modified,080828,end
244 //================================================================================
245 // LED customization.
246 //================================================================================
248 typedef enum _LED_STRATEGY_8185
{
251 HW_LED
, // HW control 2 LEDs, LED0 and LED1 (there are 4 different control modes)
252 }LED_STRATEGY_8185
, *PLED_STRATEGY_8185
;
254 //by amy for power save
255 typedef enum _LED_CTL_MODE
{
256 LED_CTL_POWER_ON
= 1,
261 LED_CTL_SITE_SURVEY
= 6,
262 LED_CTL_POWER_OFF
= 7
265 typedef enum _RT_RF_POWER_STATE
274 auth_not_valid
= 0x2,
287 // Reason code defined in 802.11i D10.0 p.28.
289 four_way_tmout
= 0x0f,
290 two_way_tmout
= 0x10,
292 invalid_Gcipher
= 0x12,
293 invalid_Pcipher
= 0x13,
295 unsup_RSNIEver
= 0x15,
296 invalid_RSNIE
= 0x16,
297 auth_802_1x_fail
= 0x17,
300 // Reason code defined in 7.3.1.7, 802.1e D13.0, p.42. Added by Annie, 2005-11-15.
301 QoS_unspec
= 0x20, // 32
302 QAP_bandwidth
= 0x21, // 33
303 poor_condition
= 0x22, // 34
304 no_facility
= 0x23, // 35
306 req_declined
= 0x25, // 37
307 invalid_param
= 0x26, // 38
308 req_not_honored
= 0x27, // 39
309 TS_not_created
= 0x2F, // 47
310 DL_not_allowed
= 0x30, // 48
311 dest_not_exist
= 0x31, // 49
312 dest_not_QSTA
= 0x32, // 50
314 typedef enum _RT_PS_MODE
316 eActive
, // Active/Continuous access.
317 eMaxPs
, // Max power save mode.
318 eFastPs
// Fast power save mode.
320 //by amy for power save
321 typedef struct r8180_priv
323 struct pci_dev
*pdev
;
327 struct ieee80211_device
*ieee80211
;
329 short phy_ver
; /* meaningful for rtl8225 1:A 2:B 3:C */
332 short plcp_preamble_mode
; // 0:auto 1:short 2:long
335 spinlock_t irq_th_lock
;
338 spinlock_t rf_ps_lock
;
342 struct net_device
*dev
;
346 u8 chtxpwr
[15]; //channels from 1 to 14, 0 not used
347 u8 chtxpwr_ofdm
[15]; //channels from 1 to 14, 0 not used
348 //u8 challow[15]; //channels from 1 to 14, 0 not used
349 u8 channel_plan
; // it's the channel plan index
351 short crcmon
; //if 1 allow bad crc frame reception in monitor mode
354 struct timer_list scan_timer
;
357 spinlock_t scan_lock
;
359 //u8 active_scan_num;
360 struct semaphore wx_sem
;
361 struct semaphore rf_state
;
371 short (*rf_set_sens
)(struct net_device
*dev
,short sens
);
372 void (*rf_set_chan
)(struct net_device
*dev
,short ch
);
373 void (*rf_close
)(struct net_device
*dev
);
374 void (*rf_init
)(struct net_device
*dev
);
375 void (*rf_sleep
)(struct net_device
*dev
);
376 void (*rf_wakeup
)(struct net_device
*dev
);
381 struct _link_detect_t link_detect
; //YJ,add,080828
382 struct iw_statistics wstats
;
383 struct proc_dir_entry
*dir_dev
;
388 dma_addr_t rxringdma
;
389 struct buffer
*rxbuffer
;
390 struct buffer
*rxbufferhead
;
394 struct sk_buff
*rx_skb
;
396 short rx_skb_complete
;
405 dma_addr_t txlpringdma;
406 dma_addr_t txhpringdma;
407 dma_addr_t txnpringdma;
414 struct buffer *txlpbufs;
415 struct buffer *txhpbufs;
416 struct buffer *txnpbufs;
417 struct buffer *txlpbufstail;
418 struct buffer *txhpbufstail;
419 struct buffer *txnpbufstail;
427 dma_addr_t txmapringdma
;
428 dma_addr_t txbkpringdma
;
429 dma_addr_t txbepringdma
;
430 dma_addr_t txvipringdma
;
431 dma_addr_t txvopringdma
;
432 dma_addr_t txhpringdma
;
445 struct buffer
*txmapbufs
;
446 struct buffer
*txbkpbufs
;
447 struct buffer
*txbepbufs
;
448 struct buffer
*txvipbufs
;
449 struct buffer
*txvopbufs
;
450 struct buffer
*txhpbufs
;
451 struct buffer
*txmapbufstail
;
452 struct buffer
*txbkpbufstail
;
453 struct buffer
*txbepbufstail
;
454 struct buffer
*txvipbufstail
;
455 struct buffer
*txvopbufstail
;
456 struct buffer
*txhpbufstail
;
460 //struct tx_pendingbuf txnp_pending;
461 //struct tasklet_struct irq_tx_tasklet;
462 struct tasklet_struct irq_rx_tasklet
;
466 /* adhoc/master mode stuff */
467 u32
*txbeaconringtail
;
468 dma_addr_t txbeaconringdma
;
471 struct buffer
*txbeaconbufs
;
472 struct buffer
*txbeaconbufstail
;
473 //char *master_essid;
474 //u16 master_beaconinterval;
475 //u32 master_beaconsize;
476 //u16 beacon_interval;
482 //add for RF power on power off by lizhaoming 080512
483 u8 RegThreeWireMode
; // See "Three wire mode" defined above, 2006.05.31, by rcnjko.
486 LED_STRATEGY_8185 LedStrategy
;
489 //by amy for power save
490 struct timer_list watch_dog_timer
;
492 bool bSwRfProcessing
;
493 RT_RF_POWER_STATE eInactivePowerState
;
494 RT_RF_POWER_STATE eRFPowerState
;
496 bool RFChangeInProgress
;
498 bool SetRFPowerStateInProgress
;
501 RT_PS_MODE dot11PowerSaveMode
;
502 //u32 NumRxOkInPeriod; //YJ,del,080828
503 //u32 NumTxOkInPeriod; //YJ,del,080828
506 bool bApBufOurFrame
;// TRUE if AP buffer our unicast data , we will keep eAwake until receive data or timeout.
507 u8 WaitBufDataBcnCount
;
508 u8 WaitBufDataTimeOut
;
510 //by amy for power save
512 u8 EEPROMSwAntennaDiversity
;
513 bool EEPROMDefaultAntenna1
;
514 u8 RegSwAntennaDiversityMechanism
;
515 bool bSwAntennaDiverity
;
516 u8 RegDefaultAntenna
;
517 bool bDefaultAntenna1
;
519 long Stats_SignalStrength
;
520 long LastSignalStrengthInPercent
; // In percentange, used for smoothing, e.g. Moving Average.
521 u8 SignalQuality
; // in 0-100 index.
522 long Stats_SignalQuality
;
523 long RecvSignalPower
; // in dBm.
524 long Stats_RecvSignalPower
;
525 u8 LastRxPktAntenna
; // +by amy 080312 Antenn which received the lasted packet. 0: Aux, 1:Main. Added by Roger, 2008.01.25.
527 long AdRxSignalStrength
;
528 u8 CurrAntennaIndex
; // Index to current Antenna (both Tx and Rx).
529 u8 AdTickCount
; // Times of SwAntennaDiversityTimer happened.
530 u8 AdCheckPeriod
; // # of period SwAntennaDiversityTimer to check Rx signal strength for SW Antenna Diversity.
531 u8 AdMinCheckPeriod
; // Min value of AdCheckPeriod.
532 u8 AdMaxCheckPeriod
; // Max value of AdCheckPeriod.
533 long AdRxSsThreshold
; // Signal strength threshold to switch antenna.
534 long AdMaxRxSsThreshold
; // Max value of AdRxSsThreshold.
535 bool bAdSwitchedChecking
; // TRUE if we shall shall check Rx signal strength for last time switching antenna.
536 long AdRxSsBeforeSwitched
; // Rx signal strength before we swithed antenna.
537 struct timer_list SwAntennaDiversityTimer
;
541 // Crystal calibration.
542 // Added by Roger, 2007.12.11.
544 bool bXtalCalibration
; // Crystal calibration.
545 u8 XtalCal_Xin
; // Crystal calibration for Xin. 0~7.5pF
546 u8 XtalCal_Xout
; // Crystal calibration for Xout. 0~7.5pF
548 // Tx power tracking with thermal meter indication.
549 // Added by Roger, 2007.12.11.
551 bool bTxPowerTrack
; // Tx Power tracking.
552 u8 ThermalMeter
; // Thermal meter reference indication.
554 // Dynamic Initial Gain Adjustment Mechanism. Added by Bruce, 2007-02-14.
556 bool bDigMechanism
; // TRUE if DIG is enabled, FALSE ow.
557 bool bRegHighPowerMechanism
; // For High Power Mechanism. 061010, by rcnjko.
558 u32 FalseAlarmRegValue
;
559 u8 RegDigOfdmFaUpTh
; // Upper threhold of OFDM false alarm, which is used in DIG.
560 u8 DIG_NumberFallbackVote
;
561 u8 DIG_NumberUpgradeVote
;
562 // For HW antenna diversity, added by Roger, 2008.01.30.
563 u32 AdMainAntennaRxOkCnt
; // Main antenna Rx OK count.
564 u32 AdAuxAntennaRxOkCnt
; // Aux antenna Rx OK count.
565 bool bHWAdSwitched
; // TRUE if we has switched default antenna by HW evaluation.
566 // RF High Power upper/lower threshold.
569 // RF RSSI High Power upper/lower Threshold.
570 u8 RegRSSIHiPwrUpperTh
;
571 u8 RegRSSIHiPwrLowerTh
;
572 // Current CCK RSSI value to determine CCK high power, asked by SD3 DZ, by Bruce, 2007-04-12.
576 // High Power Mechanism. Added by amy, 080312.
579 long UndecoratedSmoothedSS
;
580 long UndercorateSmoothedRxPower
;
584 //For adjust Dig Threshold during Legacy/Leisure Power Save Mode
585 u32 DozePeriodInPast2Sec
;
586 // Don't access BB/RF under disable PLL situation.
587 u8 InitialGainBackUp
;
588 u8 RegBModeGainStage
;
589 //by amy for rate adaptive
590 struct timer_list rateadapter_timer
;
591 u32 RateAdaptivePeriod
;
594 int ForcedDataRate
; // Force Data Rate. 0: Auto, 0x02: 1M ~ 0x6C: 54M.)
595 u32 NumTxUnicast
; //YJ,add,080828,for keep alive
596 u8 keepAliveLevel
; //YJ,add,080828,for KeepAlive
597 unsigned long NumTxOkTotal
;
600 unsigned long LastTxokCnt
;
601 unsigned long LastRxokCnt
;
603 unsigned long LastTxOKBytes
;
604 unsigned long NumTxOkBytesTotal
;
606 long LastFailTxRateSS
;
608 u32 LastTxThroughput
;
610 unsigned short bTryuping
;
611 u8 CurrTxRate
; //the rate before up
614 u8 TryDownCountLowData
;
615 u8 TryupingCountNoData
;
618 //by amy for rate adaptive
621 // struct workqueue_struct *workqueue;
622 struct work_struct reset_wq
;
623 struct work_struct watch_dog_wq
;
624 struct work_struct tx_irq_wq
;
625 short ack_tx_to_ieee
;
631 u8 dma_poll_stop_mask
;
633 //u8 RegThreeWireMode;
637 u16 EarlyRxThreshold
;
642 struct ChnlAccessSetting ChannelAccessSetting
;
645 #define MANAGE_PRIORITY 0
646 #define BK_PRIORITY 1
647 #define BE_PRIORITY 2
648 #define VI_PRIORITY 3
649 #define VO_PRIORITY 4
650 #define HI_PRIORITY 5
651 #define BEACON_PRIORITY 6
653 #define LOW_PRIORITY VI_PRIORITY
654 #define NORM_PRIORITY VO_PRIORITY
656 #define AC2Q(_ac) (((_ac) == WME_AC_VO) ? VO_PRIORITY : \
657 ((_ac) == WME_AC_VI) ? VI_PRIORITY : \
658 ((_ac) == WME_AC_BK) ? BK_PRIORITY : \
661 short rtl8180_tx(struct net_device
*dev
,u8
* skbuf
, int len
,int priority
,
662 short morefrag
,short fragdesc
,int rate
);
664 u8
read_nic_byte(struct net_device
*dev
, int x
);
665 u32
read_nic_dword(struct net_device
*dev
, int x
);
666 u16
read_nic_word(struct net_device
*dev
, int x
) ;
667 void write_nic_byte(struct net_device
*dev
, int x
,u8 y
);
668 void write_nic_word(struct net_device
*dev
, int x
,u16 y
);
669 void write_nic_dword(struct net_device
*dev
, int x
,u32 y
);
670 void force_pci_posting(struct net_device
*dev
);
672 void rtl8180_rtx_disable(struct net_device
*);
673 void rtl8180_rx_enable(struct net_device
*);
674 void rtl8180_tx_enable(struct net_device
*);
675 void rtl8180_start_scanning(struct net_device
*dev
);
676 void rtl8180_start_scanning_s(struct net_device
*dev
);
677 void rtl8180_stop_scanning(struct net_device
*dev
);
678 void rtl8180_disassociate(struct net_device
*dev
);
679 //void fix_rx_fifo(struct net_device *dev);
680 void rtl8180_set_anaparam(struct net_device
*dev
,u32 a
);
681 void rtl8185_set_anaparam2(struct net_device
*dev
,u32 a
);
682 void rtl8180_set_hw_wep(struct net_device
*dev
);
683 void rtl8180_no_hw_wep(struct net_device
*dev
);
684 void rtl8180_update_msr(struct net_device
*dev
);
685 //void rtl8180_BSS_create(struct net_device *dev);
686 void rtl8180_beacon_tx_disable(struct net_device
*dev
);
687 void rtl8180_beacon_rx_disable(struct net_device
*dev
);
688 void rtl8180_conttx_enable(struct net_device
*dev
);
689 void rtl8180_conttx_disable(struct net_device
*dev
);
690 int rtl8180_down(struct net_device
*dev
);
691 int rtl8180_up(struct net_device
*dev
);
692 void rtl8180_commit(struct net_device
*dev
);
693 void rtl8180_set_chan(struct net_device
*dev
,short ch
);
694 void rtl8180_set_master_essid(struct net_device
*dev
,char *essid
);
695 void rtl8180_update_beacon_security(struct net_device
*dev
);
696 void write_phy(struct net_device
*dev
, u8 adr
, u8 data
);
697 void write_phy_cck(struct net_device
*dev
, u8 adr
, u32 data
);
698 void write_phy_ofdm(struct net_device
*dev
, u8 adr
, u32 data
);
699 void rtl8185_tx_antenna(struct net_device
*dev
, u8 ant
);
700 void rtl8185_rf_pins_enable(struct net_device
*dev
);
701 void IBSS_randomize_cell(struct net_device
*dev
);
702 void IPSEnter(struct net_device
*dev
);
703 void IPSLeave(struct net_device
*dev
);
704 int get_curr_tx_free_desc(struct net_device
*dev
, int priority
);
705 void UpdateInitialGain(struct net_device
*dev
);
706 bool SetAntennaConfig87SE(struct net_device
*dev
, u8 DefaultAnt
, bool bAntDiversity
);
708 //#ifdef CONFIG_RTL8185B
709 void rtl8185b_adapter_start(struct net_device
*dev
);
710 void rtl8185b_rx_enable(struct net_device
*dev
);
711 void rtl8185b_tx_enable(struct net_device
*dev
);
712 void rtl8180_reset(struct net_device
*dev
);
713 void rtl8185b_irq_enable(struct net_device
*dev
);
714 void fix_rx_fifo(struct net_device
*dev
);
715 void fix_tx_fifo(struct net_device
*dev
);
716 void rtl8225z2_SetTXPowerLevel(struct net_device
*dev
, short ch
);
717 void rtl8180_rate_adapter(struct work_struct
* work
);
719 bool MgntActSet_RF_State(struct net_device
*dev
, RT_RF_POWER_STATE StateToSet
, u32 ChangeSource
);