2 * This is part of rtl8180 OpenSource driver.
3 * Copyright (C) Andrea Merello 2004-2005 <andrea.merello@gmail.com>
4 * Released under the terms of GPL (General Public Licence)
6 * Parts of this driver are based on the GPL part of the official realtek driver
8 * Parts of this driver are based on the rtl8180 driver skeleton from Patric
9 * Schenke & Andres Salomon
11 * Parts of this driver are based on the Intel Pro Wireless 2100 GPL driver
13 * We want to thanks the Authors of those projects and the Ndiswrapper project
20 #include <linux/interrupt.h>
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/ioport.h>
30 #include <linux/sched.h>
31 #include <linux/types.h>
32 #include <linux/slab.h>
33 #include <linux/netdevice.h>
34 #include <linux/pci.h>
35 #include <linux/etherdevice.h>
36 #include <linux/delay.h>
37 #include <linux/rtnetlink.h> /* for rtnl_lock() */
38 #include <linux/wireless.h>
39 #include <linux/timer.h>
40 #include <linux/proc_fs.h> /* Necessary because we use the proc fs. */
41 #include <linux/if_arp.h>
42 #include "ieee80211/ieee80211.h"
48 #define RTL_IOCTL_WPA_SUPPLICANT (SIOCIWFIRSTPRIV + 30)
50 #define DEFAULT_FRAG_THRESHOLD 2342U
51 #define MIN_FRAG_THRESHOLD 256U
52 #define DEFAULT_RTS_THRESHOLD 2342U
53 #define MIN_RTS_THRESHOLD 0U
54 #define MAX_RTS_THRESHOLD 2342U
55 #define DEFAULT_BEACONINTERVAL 0x64U
57 #define DEFAULT_RETRY_RTS 7
58 #define DEFAULT_RETRY_DATA 7
60 #define BEACON_QUEUE 6
65 #define sAckCtsLng 112 /* bits in ACK and CTS frames. */
67 #define RATE_ADAPTIVE_TIMER_PERIOD 300
70 WIRELESS_MODE_UNKNOWN = 0x00,
71 WIRELESS_MODE_A = 0x01,
72 WIRELESS_MODE_B = 0x02,
73 WIRELESS_MODE_G = 0x04,
74 WIRELESS_MODE_AUTO = 0x08,
77 struct chnl_access_setting {
91 typedef u32 AC_CODING;
92 #define AC0_BE 0 /* ACI: 0x00 */ /* Best Effort. */
93 #define AC1_BK 1 /* ACI: 0x01 */ /* Background. */
94 #define AC2_VI 2 /* ACI: 0x10 */ /* Video. */
95 #define AC3_VO 3 /* ACI: 0x11 */ /* Voice. */
96 #define AC_MAX 4 /* Max: define total number; Should not to be used as a real
101 * ECWmin/ECWmax field.
102 * Ref: WMM spec 2.2.2: WME Parameter Element, p.13.
113 * ACI/AIFSN Field. Ref: WMM spec 2.2.2: WME Parameter Element, p.12.
115 typedef union _ACI_AIFSN {
124 } ACI_AIFSN, *PACI_AIFSN;
127 * AC Parameters Record Format.
128 * Ref: WMM spec 2.2.2: WME Parameter Element, p.12.
130 typedef union _AC_PARAM {
139 } AC_PARAM, *PAC_PARAM;
147 /* YJ,modified,080828. */
151 unsigned long rxnolast;
152 unsigned long rxnodata;
153 unsigned long rxnopointer;
154 unsigned long txnperr;
155 unsigned long txresumed;
157 unsigned long rxoverflow;
159 unsigned long txbkpokint;
160 unsigned long txbepoking;
161 unsigned long txbkperr;
162 unsigned long txbeperr;
163 unsigned long txnpokint;
164 unsigned long txhpokint;
165 unsigned long txhperr;
167 unsigned long shints;
168 unsigned long txoverflow;
169 unsigned long rxdmafail;
170 unsigned long txbeacon;
171 unsigned long txbeaconerr;
172 unsigned long txlpokint;
173 unsigned long txlperr;
174 unsigned long txretry; /* retry number tony 20060601 */
175 unsigned long rxcrcerrmin; /* crc error (0-500) */
176 unsigned long rxcrcerrmid; /* crc error (500-1000) */
177 unsigned long rxcrcerrmax; /* crc error (>1000) */
178 unsigned long rxicverr; /* ICV error */
181 #define MAX_LD_SLOT_NUM 10
182 #define KEEP_ALIVE_INTERVAL 20 /* in seconds. */
183 #define CHECK_FOR_HANG_PERIOD 2 /* be equal to watchdog check time. */
184 #define DEFAULT_KEEP_ALIVE_LEVEL 1
185 #define DEFAULT_SLOT_NUM 2
186 #define POWER_PROFILE_AC 0
187 #define POWER_PROFILE_BATTERY 1
189 struct link_detect_t {
190 u32 rx_frame_num[MAX_LD_SLOT_NUM]; /* number of Rx Frame.
191 * CheckForHang_period to determine
194 u16 slot_num; /* number of CheckForHang period to determine link status,
198 u32 num_tx_ok_in_period; /* number of packet transmitted during
201 u32 num_rx_ok_in_period; /* number of packet received during
204 u8 idle_count; /* (KEEP_ALIVE_INTERVAL / CHECK_FOR_HANG_PERIOD) */
205 u32 last_num_tx_unicast;
206 u32 last_num_rx_unicast;
208 bool b_busy_traffic; /* when it is set to 1, UI cann't scan at will. */
211 /* YJ,modified,080828,end */
214 * ==========================================================================
216 * ==========================================================================
218 enum led_strategy_8185 {
221 HW_LED, /* HW control 2 LEDs, LED0 and LED1 (there are 4 different
225 enum rt_rf_power_state {
233 auth_not_valid = 0x2,
244 /* ----END MIC_CHECK */
246 /* Reason code defined in 802.11i D10.0 p.28. */
248 four_way_tmout = 0x0f,
249 two_way_tmout = 0x10,
251 invalid_Gcipher = 0x12,
252 invalid_Pcipher = 0x13,
254 unsup_RSNIEver = 0x15,
255 invalid_RSNIE = 0x16,
256 auth_802_1x_fail = 0x17,
259 /* Reason code defined in 7.3.1.7, 802.1e D13.0, p.42. Added by Annie,
262 QoS_unspec = 0x20, /* 32 */
263 QAP_bandwidth = 0x21, /* 33 */
264 poor_condition = 0x22, /* 34 */
265 no_facility = 0x23, /* 35 */
267 req_declined = 0x25, /* 37 */
268 invalid_param = 0x26, /* 38 */
269 req_not_honored = 0x27, /* 39 */
270 TS_not_created = 0x2F, /* 47 */
271 DL_not_allowed = 0x30, /* 48 */
272 dest_not_exist = 0x31, /* 49 */
273 dest_not_QSTA = 0x32, /* 50 */
277 ACTIVE, /* Active/Continuous access. */
278 MAX_PS, /* Max power save mode. */
279 FAST_PS /* Fast power save mode. */
282 /* by amy for power save. */
284 struct pci_dev *pdev;
288 struct ieee80211_device *ieee80211;
290 short plcp_preamble_mode; /* 0:auto 1:short 2:long */
292 spinlock_t irq_th_lock;
295 spinlock_t rf_ps_lock;
299 struct net_device *dev;
303 u8 chtxpwr[15]; /* channels from 1 to 14, 0 not used. */
304 u8 chtxpwr_ofdm[15]; /* channels from 1 to 14, 0 not used. */
305 u8 channel_plan; /* it's the channel plan index. */
307 short crcmon; /* if 1 allow bad crc frame reception in monitor mode. */
309 struct timer_list scan_timer;
310 spinlock_t scan_lock;
312 struct semaphore wx_sem;
319 short (*rf_set_sens)(struct net_device *dev, short sens);
320 void (*rf_set_chan)(struct net_device *dev, short ch);
321 void (*rf_close)(struct net_device *dev);
322 void (*rf_init)(struct net_device *dev);
323 void (*rf_sleep)(struct net_device *dev);
324 void (*rf_wakeup)(struct net_device *dev);
329 struct link_detect_t link_detect; /* YJ,add,080828 */
330 struct iw_statistics wstats;
335 dma_addr_t rxringdma;
336 struct buffer *rxbuffer;
337 struct buffer *rxbufferhead;
341 struct sk_buff *rx_skb;
343 short rx_skb_complete;
353 dma_addr_t txmapringdma;
354 dma_addr_t txbkpringdma;
355 dma_addr_t txbepringdma;
356 dma_addr_t txvipringdma;
357 dma_addr_t txvopringdma;
358 dma_addr_t txhpringdma;
371 struct buffer *txmapbufs;
372 struct buffer *txbkpbufs;
373 struct buffer *txbepbufs;
374 struct buffer *txvipbufs;
375 struct buffer *txvopbufs;
376 struct buffer *txhpbufs;
377 struct buffer *txmapbufstail;
378 struct buffer *txbkpbufstail;
379 struct buffer *txbepbufstail;
380 struct buffer *txvipbufstail;
381 struct buffer *txvopbufstail;
382 struct buffer *txhpbufstail;
386 struct tasklet_struct irq_rx_tasklet;
389 /* adhoc/master mode stuff. */
390 u32 *txbeaconringtail;
391 dma_addr_t txbeaconringdma;
394 struct buffer *txbeaconbufs;
395 struct buffer *txbeaconbufstail;
401 /* by amy for led. */
402 enum led_strategy_8185 led_strategy;
403 /* by amy for led. */
405 /* by amy for power save. */
406 struct timer_list watch_dog_timer;
408 bool bSwRfProcessing;
409 enum rt_rf_power_state eInactivePowerState;
410 enum rt_rf_power_state eRFPowerState;
412 bool RFChangeInProgress;
413 bool SetRFPowerStateInProgress;
416 enum rt_ps_mode dot11PowerSaveMode;
419 bool bApBufOurFrame; /* TRUE if AP buffer our unicast data , we will
420 * keep eAwake until receive data or timeout.
422 u8 WaitBufDataBcnCount;
423 u8 WaitBufDataTimeOut;
425 /* by amy for power save. */
426 /* by amy for antenna. */
427 u8 EEPROMSwAntennaDiversity;
428 bool EEPROMDefaultAntenna1;
429 u8 RegSwAntennaDiversityMechanism;
430 bool bSwAntennaDiverity;
431 u8 RegDefaultAntenna;
432 bool bDefaultAntenna1;
434 long Stats_SignalStrength;
435 long LastSignalStrengthInPercent; /* In percentage, used for smoothing,
436 * e.g. Moving Average.
438 u8 SignalQuality; /* in 0-100 index. */
439 long Stats_SignalQuality;
440 long RecvSignalPower; /* in dBm. */
441 long Stats_RecvSignalPower;
442 u8 LastRxPktAntenna; /* +by amy 080312 Antenna which received the lasted
443 * packet. 0: Aux, 1:Main. Added by Roger,
447 long AdRxSignalStrength;
448 u8 CurrAntennaIndex; /* Index to current Antenna (both Tx and Rx). */
449 u8 AdTickCount; /* Times of SwAntennaDiversityTimer happened. */
450 u8 AdCheckPeriod; /* # of period SwAntennaDiversityTimer to check Rx
451 * signal strength for SW Antenna Diversity.
453 u8 AdMinCheckPeriod; /* Min value of AdCheckPeriod. */
454 u8 AdMaxCheckPeriod; /* Max value of AdCheckPeriod. */
455 long AdRxSsThreshold; /* Signal strength threshold to switch antenna. */
456 long AdMaxRxSsThreshold; /* Max value of AdRxSsThreshold. */
457 bool bAdSwitchedChecking; /* TRUE if we shall shall check Rx signal
458 * strength for last time switching antenna.
460 long AdRxSsBeforeSwitched; /* Rx signal strength before we switched
463 struct timer_list SwAntennaDiversityTimer;
464 /* by amy for antenna {by amy 080312 */
466 /* Crystal calibration. Added by Roger, 2007.12.11. */
468 bool bXtalCalibration; /* Crystal calibration.*/
469 u8 XtalCal_Xin; /* Crystal calibration for Xin. 0~7.5pF */
470 u8 XtalCal_Xout; /* Crystal calibration for Xout. 0~7.5pF */
472 /* Tx power tracking with thermal meter indication.
473 * Added by Roger, 2007.12.11.
476 bool bTxPowerTrack; /* Tx Power tracking. */
477 u8 ThermalMeter; /* Thermal meter reference indication. */
479 /* Dynamic Initial Gain Adjustment Mechanism. Added by Bruce,
482 bool bDigMechanism; /* TRUE if DIG is enabled, FALSE ow. */
483 bool bRegHighPowerMechanism; /* For High Power Mechanism. 061010,
486 u32 FalseAlarmRegValue;
487 u8 RegDigOfdmFaUpTh; /* Upper threshold of OFDM false alarm, which is
490 u8 DIG_NumberFallbackVote;
491 u8 DIG_NumberUpgradeVote;
492 /* For HW antenna diversity, added by Roger, 2008.01.30. */
493 u32 AdMainAntennaRxOkCnt; /* Main antenna Rx OK count. */
494 u32 AdAuxAntennaRxOkCnt; /* Aux antenna Rx OK count. */
495 bool bHWAdSwitched; /* TRUE if we has switched default antenna by HW
498 /* RF High Power upper/lower threshold. */
501 /* RF RSSI High Power upper/lower Threshold. */
502 u8 RegRSSIHiPwrUpperTh;
503 u8 RegRSSIHiPwrLowerTh;
504 /* Current CCK RSSI value to determine CCK high power, asked by SD3 DZ,
505 * by Bruce, 2007-04-12.
509 /* High Power Mechanism. Added by amy, 080312. */
511 long UndecoratedSmoothedSS;
512 long UndecoratedSmoothedRxPower;
516 /* For adjust Dig Threshold during Legacy/Leisure Power Save Mode. */
517 u32 DozePeriodInPast2Sec;
518 /* Don't access BB/RF under disable PLL situation. */
519 u8 InitialGainBackUp;
520 u8 RegBModeGainStage;
521 /* by amy for rate adaptive */
522 struct timer_list rateadapter_timer;
523 u32 RateAdaptivePeriod;
526 int ForcedDataRate; /* Force Data Rate. 0: Auto, 0x02: 1M ~ 0x6C: 54M.)
528 u32 NumTxUnicast; /* YJ,add,080828,for keep alive. */
529 u8 keepAliveLevel; /*YJ,add,080828,for KeepAlive. */
530 unsigned long NumTxOkTotal;
533 unsigned long LastTxokCnt;
534 unsigned long LastRxokCnt;
536 unsigned long LastTxOKBytes;
537 unsigned long NumTxOkBytesTotal;
539 long LastFailTxRateSS;
541 u32 LastTxThroughput;
543 unsigned short bTryuping;
544 u8 CurrTxRate; /* the rate before up. */
547 u8 TryDownCountLowData;
548 u8 TryupingCountNoData;
551 struct work_struct reset_wq;
552 struct work_struct watch_dog_wq;
553 short ack_tx_to_ieee;
555 u8 dma_poll_stop_mask;
559 u16 EarlyRxThreshold;
564 struct chnl_access_setting ChannelAccessSetting;
567 #define MANAGE_PRIORITY 0
568 #define BK_PRIORITY 1
569 #define BE_PRIORITY 2
570 #define VI_PRIORITY 3
571 #define VO_PRIORITY 4
572 #define HI_PRIORITY 5
573 #define BEACON_PRIORITY 6
575 #define LOW_PRIORITY VI_PRIORITY
576 #define NORM_PRIORITY VO_PRIORITY
577 /* AC2Queue mapping. */
578 #define AC2Q(_ac) (((_ac) == WME_AC_VO) ? VO_PRIORITY : \
579 ((_ac) == WME_AC_VI) ? VI_PRIORITY : \
580 ((_ac) == WME_AC_BK) ? BK_PRIORITY : \
583 short rtl8180_tx(struct net_device *dev, u8 *skbuf, int len, int priority,
584 bool morefrag, short fragdesc, int rate);
586 u8 read_nic_byte(struct net_device *dev, int x);
587 u32 read_nic_dword(struct net_device *dev, int x);
588 u16 read_nic_word(struct net_device *dev, int x);
589 void write_nic_byte(struct net_device *dev, int x, u8 y);
590 void write_nic_word(struct net_device *dev, int x, u16 y);
591 void write_nic_dword(struct net_device *dev, int x, u32 y);
592 void force_pci_posting(struct net_device *dev);
594 void rtl8180_rtx_disable(struct net_device *);
595 void rtl8180_set_anaparam(struct net_device *dev, u32 a);
596 void rtl8185_set_anaparam2(struct net_device *dev, u32 a);
597 void rtl8180_set_hw_wep(struct net_device *dev);
598 void rtl8180_no_hw_wep(struct net_device *dev);
599 void rtl8180_update_msr(struct net_device *dev);
600 void rtl8180_beacon_tx_disable(struct net_device *dev);
601 void rtl8180_beacon_rx_disable(struct net_device *dev);
602 int rtl8180_down(struct net_device *dev);
603 int rtl8180_up(struct net_device *dev);
604 void rtl8180_commit(struct net_device *dev);
605 void rtl8180_set_chan(struct net_device *dev, short ch);
606 void write_phy(struct net_device *dev, u8 adr, u8 data);
607 void write_phy_cck(struct net_device *dev, u8 adr, u32 data);
608 void write_phy_ofdm(struct net_device *dev, u8 adr, u32 data);
609 void rtl8185_tx_antenna(struct net_device *dev, u8 ant);
610 void rtl8185_rf_pins_enable(struct net_device *dev);
611 void IPSEnter(struct net_device *dev);
612 void IPSLeave(struct net_device *dev);
613 int get_curr_tx_free_desc(struct net_device *dev, int priority);
614 void UpdateInitialGain(struct net_device *dev);
615 bool SetAntennaConfig87SE(struct net_device *dev, u8 DefaultAnt,
618 void rtl8185b_adapter_start(struct net_device *dev);
619 void rtl8185b_rx_enable(struct net_device *dev);
620 void rtl8185b_tx_enable(struct net_device *dev);
621 void rtl8180_reset(struct net_device *dev);
622 void rtl8185b_irq_enable(struct net_device *dev);
623 void fix_rx_fifo(struct net_device *dev);
624 void fix_tx_fifo(struct net_device *dev);
625 void rtl8225z2_SetTXPowerLevel(struct net_device *dev, short ch);
626 void rtl8180_rate_adapter(struct work_struct *work);
627 bool MgntActSet_RF_State(struct net_device *dev, enum rt_rf_power_state StateToSet,
632 /* fun with the built-in ieee80211 stack... */
633 extern int ieee80211_crypto_init(void);
634 extern void ieee80211_crypto_deinit(void);
635 extern int ieee80211_crypto_tkip_init(void);
636 extern void ieee80211_crypto_tkip_exit(void);
637 extern int ieee80211_crypto_ccmp_init(void);
638 extern void ieee80211_crypto_ccmp_exit(void);
639 extern int ieee80211_crypto_wep_init(void);
640 extern void ieee80211_crypto_wep_exit(void);