1 /******************************************************************************
2  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3  *
4  * Based on the r8180 driver, which is:
5  * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * The full GNU General Public License is included in this distribution in the
16  * file called LICENSE.
17  *
18  * Contact Information:
19  * wlanfae <wlanfae@realtek.com>
20 ******************************************************************************/
21 
22 #ifndef _RTL_CORE_H
23 #define _RTL_CORE_H
24 
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/ioport.h>
28 #include <linux/sched.h>
29 #include <linux/types.h>
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
32 #include <linux/netdevice.h>
33 #include <linux/pci.h>
34 #include <linux/etherdevice.h>
35 #include <linux/delay.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/wireless.h>
38 #include <linux/timer.h>
39 #include <linux/proc_fs.h>
40 #include <linux/if_arp.h>
41 #include <linux/random.h>
42 #include <linux/io.h>
43 
44 /* Need this defined before including local include files */
45 #define DRV_NAME "rtl819xE"
46 
47 #include "../rtllib.h"
48 
49 #include "../dot11d.h"
50 
51 #include "r8192E_firmware.h"
52 #include "r8192E_hw.h"
53 
54 #include "r8190P_def.h"
55 #include "r8192E_dev.h"
56 
57 #include "rtl_eeprom.h"
58 #include "rtl_ps.h"
59 #include "rtl_pci.h"
60 #include "rtl_cam.h"
61 
62 #define DRV_COPYRIGHT		\
63 	"Copyright(c) 2008 - 2010 Realsil Semiconductor Corporation"
64 #define DRV_AUTHOR  "<wlanfae@realtek.com>"
65 #define DRV_VERSION  "0014.0401.2010"
66 
67 #define IS_HARDWARE_TYPE_8192SE(_priv)		\
68 	(((struct r8192_priv *)rtllib_priv(dev))->card_8192 == NIC_8192SE)
69 
70 #define RTL_PCI_DEVICE(vend, dev, cfg) \
71 	.vendor = (vend), .device = (dev), \
72 	.subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, \
73 	.driver_data = (kernel_ulong_t)&(cfg)
74 
75 #define TOTAL_CAM_ENTRY		32
76 #define CAM_CONTENT_COUNT	8
77 
78 #define HAL_HW_PCI_REVISION_ID_8192PCIE		0x01
79 #define HAL_HW_PCI_REVISION_ID_8192SE	0x10
80 
81 #define RTL819X_DEFAULT_RF_TYPE		RF_1T2R
82 
83 #define RTLLIB_WATCH_DOG_TIME		2000
84 
85 #define MAX_DEV_ADDR_SIZE		8  /*support till 64 bit bus width OS*/
86 #define MAX_FIRMWARE_INFORMATION_SIZE   32
87 #define MAX_802_11_HEADER_LENGTH	(40 + MAX_FIRMWARE_INFORMATION_SIZE)
88 #define ENCRYPTION_MAX_OVERHEAD		128
89 #define MAX_FRAGMENT_COUNT		8
90 #define MAX_TRANSMIT_BUFFER_SIZE	\
91 	(1600 + (MAX_802_11_HEADER_LENGTH + ENCRYPTION_MAX_OVERHEAD) *	\
92 	 MAX_FRAGMENT_COUNT)
93 
94 #define CMDPACKET_FRAG_SIZE (4 * (MAX_TRANSMIT_BUFFER_SIZE / 4) - 8)
95 
96 #define DEFAULT_FRAG_THRESHOLD	2342U
97 #define MIN_FRAG_THRESHOLD	256U
98 #define DEFAULT_BEACONINTERVAL	0x64U
99 
100 #define DEFAULT_RETRY_RTS	7
101 #define DEFAULT_RETRY_DATA	7
102 
103 #define	PHY_RSSI_SLID_WIN_MAX			100
104 
105 #define RTL_IOCTL_WPA_SUPPLICANT		(SIOCIWFIRSTPRIV + 30)
106 
107 #define TxBBGainTableLength			37
108 #define CCKTxBBGainTableLength			23
109 
110 #define CHANNEL_PLAN_LEN			10
111 #define sCrcLng					4
112 
113 #define NIC_SEND_HANG_THRESHOLD_NORMAL		4
114 #define NIC_SEND_HANG_THRESHOLD_POWERSAVE	8
115 
116 #define MAX_TX_QUEUE				9
117 
118 #define MAX_RX_QUEUE				1
119 
120 #define MAX_RX_COUNT				64
121 #define MAX_TX_QUEUE_COUNT			9
122 
123 extern int hwwep;
124 
125 enum nic_t {
126 	NIC_UNKNOWN     = 0,
127 	NIC_8192E       = 1,
128 	NIC_8190P       = 2,
129 	NIC_8192SE      = 4,
130 	NIC_8192CE	= 5,
131 	NIC_8192CU	= 6,
132 	NIC_8192DE	= 7,
133 	NIC_8192DU	= 8,
134 };
135 
136 enum rt_eeprom_type {
137 	EEPROM_93C46,
138 	EEPROM_93C56,
139 };
140 
141 enum dcmg_txcmd_op {
142 	TXCMD_TXRA_HISTORY_CTRL		= 0xFF900000,
143 	TXCMD_RESET_TX_PKT_BUFF		= 0xFF900001,
144 	TXCMD_RESET_RX_PKT_BUFF		= 0xFF900002,
145 	TXCMD_SET_TX_DURATION		= 0xFF900003,
146 	TXCMD_SET_RX_RSSI		= 0xFF900004,
147 	TXCMD_SET_TX_PWR_TRACKING	= 0xFF900005,
148 	TXCMD_XXXX_CTRL,
149 };
150 
151 enum rt_rf_type_819xu {
152 	RF_TYPE_MIN = 0,
153 	RF_8225,
154 	RF_8256,
155 	RF_8258,
156 	RF_6052 = 4,
157 	RF_PSEUDO_11N = 5,
158 };
159 
160 enum rt_customer_id {
161 	RT_CID_DEFAULT	  = 0,
162 	RT_CID_8187_ALPHA0      = 1,
163 	RT_CID_8187_SERCOMM_PS  = 2,
164 	RT_CID_8187_HW_LED      = 3,
165 	RT_CID_8187_NETGEAR     = 4,
166 	RT_CID_WHQL	     = 5,
167 	RT_CID_819x_CAMEO       = 6,
168 	RT_CID_819x_RUNTOP      = 7,
169 	RT_CID_819x_Senao       = 8,
170 	RT_CID_TOSHIBA	  = 9,
171 	RT_CID_819x_Netcore     = 10,
172 	RT_CID_Nettronix	= 11,
173 	RT_CID_DLINK	    = 12,
174 	RT_CID_PRONET	   = 13,
175 	RT_CID_COREGA	   = 14,
176 	RT_CID_819x_ALPHA       = 15,
177 	RT_CID_819x_Sitecom     = 16,
178 	RT_CID_CCX	      = 17,
179 	RT_CID_819x_Lenovo      = 18,
180 	RT_CID_819x_QMI	 = 19,
181 	RT_CID_819x_Edimax_Belkin = 20,
182 	RT_CID_819x_Sercomm_Belkin = 21,
183 	RT_CID_819x_CAMEO1 = 22,
184 	RT_CID_819x_MSI = 23,
185 	RT_CID_819x_Acer = 24,
186 	RT_CID_819x_HP	= 27,
187 	RT_CID_819x_CLEVO = 28,
188 	RT_CID_819x_Arcadyan_Belkin = 29,
189 	RT_CID_819x_SAMSUNG = 30,
190 	RT_CID_819x_WNC_COREGA = 31,
191 };
192 
193 enum reset_type {
194 	RESET_TYPE_NORESET = 0x00,
195 	RESET_TYPE_NORMAL = 0x01,
196 	RESET_TYPE_SILENT = 0x02
197 };
198 
199 struct rt_stats {
200 	unsigned long rxrdu;
201 	unsigned long rxok;
202 	unsigned long rxdatacrcerr;
203 	unsigned long rxmgmtcrcerr;
204 	unsigned long rxcrcerrmin;
205 	unsigned long rxcrcerrmid;
206 	unsigned long rxcrcerrmax;
207 	unsigned long received_rate_histogram[4][32];
208 	unsigned long received_preamble_GI[2][32];
209 	unsigned long numpacket_matchbssid;
210 	unsigned long numpacket_toself;
211 	unsigned long num_process_phyinfo;
212 	unsigned long numqry_phystatus;
213 	unsigned long numqry_phystatusCCK;
214 	unsigned long numqry_phystatusHT;
215 	unsigned long received_bwtype[5];
216 	unsigned long rxoverflow;
217 	unsigned long rxint;
218 	unsigned long ints;
219 	unsigned long shints;
220 	unsigned long txoverflow;
221 	unsigned long txbeokint;
222 	unsigned long txbkokint;
223 	unsigned long txviokint;
224 	unsigned long txvookint;
225 	unsigned long txbeaconokint;
226 	unsigned long txbeaconerr;
227 	unsigned long txmanageokint;
228 	unsigned long txcmdpktokint;
229 	unsigned long txbytesmulticast;
230 	unsigned long txbytesbroadcast;
231 	unsigned long txbytesunicast;
232 	unsigned long rxbytesunicast;
233 	unsigned long txretrycount;
234 	u8	last_packet_rate;
235 	unsigned long slide_signal_strength[100];
236 	unsigned long slide_evm[100];
237 	unsigned long	slide_rssi_total;
238 	unsigned long slide_evm_total;
239 	long signal_strength;
240 	long signal_quality;
241 	long last_signal_strength_inpercent;
242 	long	recv_signal_power;
243 	u8 rx_rssi_percentage[4];
244 	u8 rx_evm_percentage[2];
245 	long rxSNRdB[4];
246 	u32 Slide_Beacon_pwdb[100];
247 	u32 Slide_Beacon_Total;
248 	u32	CurrentShowTxate;
249 };
250 
251 struct channel_access_setting {
252 	u16 SIFS_Timer;
253 	u16 DIFS_Timer;
254 	u16 SlotTimeTimer;
255 	u16 EIFS_Timer;
256 	u16 CWminIndex;
257 	u16 CWmaxIndex;
258 };
259 
260 struct init_gain {
261 	u8	xaagccore1;
262 	u8	xbagccore1;
263 	u8	xcagccore1;
264 	u8	xdagccore1;
265 	u8	cca;
266 
267 };
268 
269 struct tx_ring {
270 	u32 *desc;
271 	u8 nStuckCount;
272 	struct tx_ring *next;
273 } __packed;
274 
275 struct rtl8192_tx_ring {
276 	struct tx_desc *desc;
277 	dma_addr_t dma;
278 	unsigned int idx;
279 	unsigned int entries;
280 	struct sk_buff_head queue;
281 };
282 
283 
284 
285 struct rtl819x_ops {
286 	enum nic_t nic_type;
287 	void (*get_eeprom_size)(struct net_device *dev);
288 	void (*init_adapter_variable)(struct net_device *dev);
289 	void (*init_before_adapter_start)(struct net_device *dev);
290 	bool (*initialize_adapter)(struct net_device *dev);
291 	void (*link_change)(struct net_device *dev);
292 	void (*tx_fill_descriptor)(struct net_device *dev,
293 				   struct tx_desc *tx_desc,
294 				   struct cb_desc *cb_desc,
295 				   struct sk_buff *skb);
296 	void (*tx_fill_cmd_descriptor)(struct net_device *dev,
297 				       struct tx_desc_cmd *entry,
298 				       struct cb_desc *cb_desc,
299 				       struct sk_buff *skb);
300 	bool (*rx_query_status_descriptor)(struct net_device *dev,
301 					   struct rtllib_rx_stats *stats,
302 					   struct rx_desc *pdesc,
303 					   struct sk_buff *skb);
304 	bool (*rx_command_packet_handler)(struct net_device *dev,
305 					  struct sk_buff *skb,
306 					  struct rx_desc *pdesc);
307 	void (*stop_adapter)(struct net_device *dev, bool reset);
308 	void (*update_ratr_table)(struct net_device *dev);
309 	void (*irq_enable)(struct net_device *dev);
310 	void (*irq_disable)(struct net_device *dev);
311 	void (*irq_clear)(struct net_device *dev);
312 	void (*rx_enable)(struct net_device *dev);
313 	void (*tx_enable)(struct net_device *dev);
314 	void (*interrupt_recognized)(struct net_device *dev,
315 				     u32 *p_inta, u32 *p_intb);
316 	bool (*TxCheckStuckHandler)(struct net_device *dev);
317 	bool (*RxCheckStuckHandler)(struct net_device *dev);
318 };
319 
320 struct r8192_priv {
321 	struct pci_dev *pdev;
322 	struct pci_dev *bridge_pdev;
323 
324 	bool		bfirst_init;
325 	bool		bfirst_after_down;
326 	bool		initialized_at_probe;
327 	bool		being_init_adapter;
328 	bool		bDriverIsGoingToUnload;
329 
330 	int		irq;
331 	short	irq_enabled;
332 
333 	short	up;
334 	short	up_first_time;
335 	struct delayed_work		update_beacon_wq;
336 	struct delayed_work		watch_dog_wq;
337 	struct delayed_work		txpower_tracking_wq;
338 	struct delayed_work		rfpath_check_wq;
339 	struct delayed_work		gpio_change_rf_wq;
340 
341 	struct workqueue_struct		*priv_wq;
342 
343 	struct channel_access_setting ChannelAccessSetting;
344 
345 	struct rtl819x_ops			*ops;
346 	struct rtllib_device			*rtllib;
347 
348 	struct work_struct				reset_wq;
349 
350 	struct log_int_8190 InterruptLog;
351 
352 	enum rt_customer_id CustomerID;
353 
354 
355 	enum rt_rf_type_819xu rf_chip;
356 	enum ht_channel_width CurrentChannelBW;
357 	struct bb_reg_definition PHYRegDef[4];
358 	struct rate_adaptive rate_adaptive;
359 
360 	enum acm_method AcmMethod;
361 
362 	struct rt_firmware			*pFirmware;
363 	enum rtl819x_loopback LoopbackMode;
364 
365 	struct timer_list			watch_dog_timer;
366 	struct timer_list			fsync_timer;
367 	struct timer_list			gpio_polling_timer;
368 
369 	spinlock_t				irq_th_lock;
370 	spinlock_t				tx_lock;
371 	spinlock_t				rf_ps_lock;
372 	spinlock_t				ps_lock;
373 
374 	struct sk_buff_head		skb_queue;
375 
376 	struct tasklet_struct		irq_rx_tasklet;
377 	struct tasklet_struct		irq_tx_tasklet;
378 	struct tasklet_struct		irq_prepare_beacon_tasklet;
379 
380 	struct semaphore			wx_sem;
381 	struct semaphore			rf_sem;
382 	struct mutex				mutex;
383 
384 	struct rt_stats stats;
385 	struct iw_statistics			wstats;
386 
387 	short (*rf_set_sens)(struct net_device *dev, short sens);
388 	u8 (*rf_set_chan)(struct net_device *dev, u8 ch);
389 
390 	struct rx_desc *rx_ring[MAX_RX_QUEUE];
391 	struct sk_buff	*rx_buf[MAX_RX_QUEUE][MAX_RX_COUNT];
392 	dma_addr_t	rx_ring_dma[MAX_RX_QUEUE];
393 	unsigned int	rx_idx[MAX_RX_QUEUE];
394 	int		rxringcount;
395 	u16		rxbuffersize;
396 
397 	u64		LastRxDescTSF;
398 
399 	u32		ReceiveConfig;
400 	u8		retry_data;
401 	u8		retry_rts;
402 	u16		rts;
403 
404 	struct rtl8192_tx_ring tx_ring[MAX_TX_QUEUE_COUNT];
405 	int		 txringcount;
406 	atomic_t	tx_pending[0x10];
407 
408 	u16		ShortRetryLimit;
409 	u16		LongRetryLimit;
410 
411 	bool		bHwRadioOff;
412 	bool		blinked_ingpio;
413 	u8		polling_timer_on;
414 
415 	/**********************************************************/
416 
417 	enum card_type {
418 		PCI, MINIPCI,
419 		CARDBUS, USB
420 	} card_type;
421 
422 	struct work_struct qos_activate;
423 
424 	short	promisc;
425 
426 	short	chan;
427 	short	sens;
428 	short	max_sens;
429 
430 	u8 ScanDelay;
431 	bool ps_force;
432 
433 	u32 irq_mask[2];
434 
435 	u8 Rf_Mode;
436 	enum nic_t card_8192;
437 	u8 card_8192_version;
438 
439 	u8 rf_type;
440 	u8 IC_Cut;
441 	char nick[IW_ESSID_MAX_SIZE + 1];
442 	u8 check_roaming_cnt;
443 
444 	u32 SilentResetRxSlotIndex;
445 	u32 SilentResetRxStuckEvent[MAX_SILENT_RESET_RX_SLOT_NUM];
446 
447 	u16 basic_rate;
448 	u8 short_preamble;
449 	u8 dot11CurrentPreambleMode;
450 	u8 slot_time;
451 	u16 SifsTime;
452 
453 	bool AutoloadFailFlag;
454 
455 	short	epromtype;
456 	u16 eeprom_vid;
457 	u16 eeprom_did;
458 	u8 eeprom_CustomerID;
459 	u16 eeprom_ChannelPlan;
460 
461 	u8 EEPROMTxPowerLevelCCK[14];
462 	u8 EEPROMTxPowerLevelOFDM24G[14];
463 	u8 EEPROMRfACCKChnl1TxPwLevel[3];
464 	u8 EEPROMRfAOfdmChnlTxPwLevel[3];
465 	u8 EEPROMRfCCCKChnl1TxPwLevel[3];
466 	u8 EEPROMRfCOfdmChnlTxPwLevel[3];
467 	u16 EEPROMAntPwDiff;
468 	u8 EEPROMThermalMeter;
469 	u8 EEPROMCrystalCap;
470 
471 	u8 EEPROMLegacyHTTxPowerDiff;
472 
473 	u8 CrystalCap;
474 	u8 ThermalMeter[2];
475 
476 	u8 SwChnlInProgress;
477 	u8 SwChnlStage;
478 	u8 SwChnlStep;
479 	u8 SetBWModeInProgress;
480 
481 	u8 nCur40MhzPrimeSC;
482 
483 	u32 RfReg0Value[4];
484 	u8 NumTotalRFPath;
485 	bool brfpath_rxenable[4];
486 
487 	bool bTXPowerDataReadFromEEPORM;
488 
489 	u16 RegChannelPlan;
490 	u16 ChannelPlan;
491 
492 	bool RegRfOff;
493 	bool isRFOff;
494 	bool bInPowerSaveMode;
495 	u8 bHwRfOffAction;
496 
497 	bool RFChangeInProgress;
498 	bool SetRFPowerStateInProgress;
499 	bool bdisable_nic;
500 
501 	u8 DM_Type;
502 
503 	u8 CckPwEnl;
504 	u16 TSSI_13dBm;
505 	u32 Pwr_Track;
506 	u8 CCKPresentAttentuation_20Mdefault;
507 	u8 CCKPresentAttentuation_40Mdefault;
508 	char CCKPresentAttentuation_difference;
509 	char CCKPresentAttentuation;
510 	long undecorated_smoothed_pwdb;
511 
512 	u32 MCSTxPowerLevelOriginalOffset[6];
513 	u8 TxPowerLevelCCK[14];
514 	u8 TxPowerLevelCCK_A[14];
515 	u8 TxPowerLevelCCK_C[14];
516 	u8		TxPowerLevelOFDM24G[14];
517 	u8		TxPowerLevelOFDM24G_A[14];
518 	u8		TxPowerLevelOFDM24G_C[14];
519 	u8		LegacyHTTxPowerDiff;
520 	s8		RF_C_TxPwDiff;
521 	u8		AntennaTxPwDiff[3];
522 
523 	bool		bDynamicTxHighPower;
524 	bool		bDynamicTxLowPower;
525 	bool		bLastDTPFlag_High;
526 	bool		bLastDTPFlag_Low;
527 
528 	u8		rfa_txpowertrackingindex;
529 	u8		rfa_txpowertrackingindex_real;
530 	u8		rfa_txpowertracking_default;
531 	u8		rfc_txpowertrackingindex;
532 	u8		rfc_txpowertrackingindex_real;
533 	bool		btxpower_tracking;
534 	bool		bcck_in_ch14;
535 
536 	u8		txpower_count;
537 	bool		btxpower_trackingInit;
538 
539 	u8		OFDM_index[2];
540 	u8		CCK_index;
541 
542 	u8		Record_CCK_20Mindex;
543 	u8		Record_CCK_40Mindex;
544 
545 	struct init_gain initgain_backup;
546 	u8		DefaultInitialGain[4];
547 	bool		bis_any_nonbepkts;
548 	bool		bcurrent_turbo_EDCA;
549 	bool		bis_cur_rdlstate;
550 
551 	bool		bfsync_processing;
552 	u32		rate_record;
553 	u32		rateCountDiffRecord;
554 	u32		ContinueDiffCount;
555 	bool		bswitch_fsync;
556 	u8		framesync;
557 	u32		framesyncC34;
558 	u8		framesyncMonitor;
559 
560 	u32		reset_count;
561 
562 	enum reset_type ResetProgress;
563 	bool		bForcedSilentReset;
564 	bool		bDisableNormalResetCheck;
565 	u16		TxCounter;
566 	u16		RxCounter;
567 	bool		bResetInProgress;
568 	bool		force_reset;
569 	bool		force_lps;
570 
571 	bool		chan_forced;
572 
573 	u8		PwrDomainProtect;
574 	u8		H2CTxCmdSeq;
575 };
576 
577 extern const struct ethtool_ops rtl819x_ethtool_ops;
578 
579 u8 rtl92e_readb(struct net_device *dev, int x);
580 u32 rtl92e_readl(struct net_device *dev, int x);
581 u16 rtl92e_readw(struct net_device *dev, int x);
582 void rtl92e_writeb(struct net_device *dev, int x, u8 y);
583 void rtl92e_writew(struct net_device *dev, int x, u16 y);
584 void rtl92e_writel(struct net_device *dev, int x, u32 y);
585 
586 void force_pci_posting(struct net_device *dev);
587 
588 void rtl92e_rx_enable(struct net_device *);
589 void rtl92e_tx_enable(struct net_device *);
590 
591 void rtl92e_hw_sleep_wq(void *data);
592 void rtl92e_commit(struct net_device *dev);
593 
594 void rtl92e_check_rfctrl_gpio_timer(unsigned long data);
595 
596 void rtl92e_hw_wakeup_wq(void *data);
597 
598 void rtl92e_reset_desc_ring(struct net_device *dev);
599 void rtl92e_set_wireless_mode(struct net_device *dev, u8 wireless_mode);
600 void rtl92e_irq_enable(struct net_device *dev);
601 void rtl92e_config_rate(struct net_device *dev, u16 *rate_config);
602 void rtl92e_irq_disable(struct net_device *dev);
603 
604 void rtl92e_update_rx_pkt_timestamp(struct net_device *dev,
605 				    struct rtllib_rx_stats *stats);
606 long rtl92e_translate_to_dbm(struct r8192_priv *priv, u8 signal_strength_index);
607 void rtl92e_update_rx_statistics(struct r8192_priv *priv,
608 				 struct rtllib_rx_stats *pprevious_stats);
609 u8 rtl92e_evm_db_to_percent(char value);
610 u8 rtl92e_rx_db_to_percent(char antpower);
611 void rtl92e_copy_mpdu_stats(struct rtllib_rx_stats *psrc_stats,
612 			    struct rtllib_rx_stats *ptarget_stats);
613 bool rtl92e_enable_nic(struct net_device *dev);
614 bool rtl92e_disable_nic(struct net_device *dev);
615 
616 bool rtl92e_set_rf_state(struct net_device *dev,
617 			 enum rt_rf_power_state StateToSet,
618 			 RT_RF_CHANGE_SOURCE ChangeSource);
619 #endif
620