8532e0c21e754e6830c956fa9232ef7cfa2a4091
[linux-2.6-block.git] / drivers / staging / rtl8192e / rtl8192e / rtl_dm.c
1 /******************************************************************************
2  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3  *
4  * This program is distributed in the hope that it will be useful, but WITHOUT
5  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
7  * more details.
8  *
9  * You should have received a copy of the GNU General Public License along with
10  * this program; if not, write to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
12  *
13  * The full GNU General Public License is included in this distribution in the
14  * file called LICENSE.
15  *
16  * Contact Information:
17  * wlanfae <wlanfae@realtek.com>
18 ******************************************************************************/
19 #include "rtl_core.h"
20 #include "rtl_dm.h"
21 #include "r8192E_hw.h"
22 #include "r8192E_phy.h"
23 #include "r8192E_phyreg.h"
24 #include "r8190P_rtl8256.h"
25 #include "r8192E_cmdpkt.h"
26
27 /*---------------------------Define Local Constant---------------------------*/
28 static u32 edca_setting_DL[HT_IOT_PEER_MAX] = {
29         0x5e4322,
30         0x5e4322,
31         0x5ea44f,
32         0x5e4322,
33         0x604322,
34         0xa44f,
35         0x5e4322,
36         0x5e4332
37 };
38
39 static u32 edca_setting_DL_GMode[HT_IOT_PEER_MAX] = {
40         0x5e4322,
41         0x5e4322,
42         0x5e4322,
43         0x5e4322,
44         0x604322,
45         0xa44f,
46         0x5e4322,
47         0x5e4322
48 };
49
50 static u32 edca_setting_UL[HT_IOT_PEER_MAX] = {
51         0x5e4322,
52         0xa44f,
53         0x5ea44f,
54         0x5e4322,
55         0x604322,
56         0x5e4322,
57         0x5e4322,
58         0x5e4332
59 };
60
61 #define RTK_UL_EDCA 0xa44f
62 #define RTK_DL_EDCA 0x5e4322
63
64 const u32 dm_tx_bb_gain[TxBBGainTableLength] = {
65         0x7f8001fe, /* 12 dB */
66         0x788001e2, /* 11 dB */
67         0x71c001c7,
68         0x6b8001ae,
69         0x65400195,
70         0x5fc0017f,
71         0x5a400169,
72         0x55400155,
73         0x50800142,
74         0x4c000130,
75         0x47c0011f,
76         0x43c0010f,
77         0x40000100,
78         0x3c8000f2,
79         0x390000e4,
80         0x35c000d7,
81         0x32c000cb,
82         0x300000c0,
83         0x2d4000b5,
84         0x2ac000ab,
85         0x288000a2,
86         0x26000098,
87         0x24000090,
88         0x22000088,
89         0x20000080,
90         0x1a00006c,
91         0x1c800072,
92         0x18000060,
93         0x19800066,
94         0x15800056,
95         0x26c0005b,
96         0x14400051,
97         0x24400051,
98         0x1300004c,
99         0x12000048,
100         0x11000044,
101         0x10000040, /* -24 dB */
102 };
103
104 const u8 dm_cck_tx_bb_gain[CCKTxBBGainTableLength][8] = {
105         {0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04},
106         {0x33, 0x32, 0x2b, 0x23, 0x1a, 0x11, 0x08, 0x04},
107         {0x30, 0x2f, 0x29, 0x21, 0x19, 0x10, 0x08, 0x03},
108         {0x2d, 0x2d, 0x27, 0x1f, 0x18, 0x0f, 0x08, 0x03},
109         {0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03},
110         {0x28, 0x28, 0x22, 0x1c, 0x15, 0x0d, 0x07, 0x03},
111         {0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03},
112         {0x24, 0x23, 0x1f, 0x19, 0x13, 0x0c, 0x06, 0x03},
113         {0x22, 0x21, 0x1d, 0x18, 0x11, 0x0b, 0x06, 0x02},
114         {0x20, 0x20, 0x1b, 0x16, 0x11, 0x08, 0x05, 0x02},
115         {0x1f, 0x1e, 0x1a, 0x15, 0x10, 0x0a, 0x05, 0x02},
116         {0x1d, 0x1c, 0x18, 0x14, 0x0f, 0x0a, 0x05, 0x02},
117         {0x1b, 0x1a, 0x17, 0x13, 0x0e, 0x09, 0x04, 0x02},
118         {0x1a, 0x19, 0x16, 0x12, 0x0d, 0x09, 0x04, 0x02},
119         {0x18, 0x17, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x02},
120         {0x17, 0x16, 0x13, 0x10, 0x0c, 0x08, 0x04, 0x02},
121         {0x16, 0x15, 0x12, 0x0f, 0x0b, 0x07, 0x04, 0x01},
122         {0x14, 0x14, 0x11, 0x0e, 0x0b, 0x07, 0x03, 0x02},
123         {0x13, 0x13, 0x10, 0x0d, 0x0a, 0x06, 0x03, 0x01},
124         {0x12, 0x12, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01},
125         {0x11, 0x11, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01},
126         {0x10, 0x10, 0x0e, 0x0b, 0x08, 0x05, 0x03, 0x01},
127         {0x0f, 0x0f, 0x0d, 0x0b, 0x08, 0x05, 0x03, 0x01}
128 };
129
130 const u8 dm_cck_tx_bb_gain_ch14[CCKTxBBGainTableLength][8] = {
131         {0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00},
132         {0x33, 0x32, 0x2b, 0x19, 0x00, 0x00, 0x00, 0x00},
133         {0x30, 0x2f, 0x29, 0x18, 0x00, 0x00, 0x00, 0x00},
134         {0x2d, 0x2d, 0x27, 0x17, 0x00, 0x00, 0x00, 0x00},
135         {0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00},
136         {0x28, 0x28, 0x22, 0x14, 0x00, 0x00, 0x00, 0x00},
137         {0x26, 0x25, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00},
138         {0x24, 0x23, 0x1f, 0x12, 0x00, 0x00, 0x00, 0x00},
139         {0x22, 0x21, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00},
140         {0x20, 0x20, 0x1b, 0x10, 0x00, 0x00, 0x00, 0x00},
141         {0x1f, 0x1e, 0x1a, 0x0f, 0x00, 0x00, 0x00, 0x00},
142         {0x1d, 0x1c, 0x18, 0x0e, 0x00, 0x00, 0x00, 0x00},
143         {0x1b, 0x1a, 0x17, 0x0e, 0x00, 0x00, 0x00, 0x00},
144         {0x1a, 0x19, 0x16, 0x0d, 0x00, 0x00, 0x00, 0x00},
145         {0x18, 0x17, 0x15, 0x0c, 0x00, 0x00, 0x00, 0x00},
146         {0x17, 0x16, 0x13, 0x0b, 0x00, 0x00, 0x00, 0x00},
147         {0x16, 0x15, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00},
148         {0x14, 0x14, 0x11, 0x0a, 0x00, 0x00, 0x00, 0x00},
149         {0x13, 0x13, 0x10, 0x0a, 0x00, 0x00, 0x00, 0x00},
150         {0x12, 0x12, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00},
151         {0x11, 0x11, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00},
152         {0x10, 0x10, 0x0e, 0x08, 0x00, 0x00, 0x00, 0x00},
153         {0x0f, 0x0f, 0x0d, 0x08, 0x00, 0x00, 0x00, 0x00}
154 };
155
156 /*---------------------------Define Local Constant---------------------------*/
157
158
159 /*------------------------Define global variable-----------------------------*/
160 struct dig_t dm_digtable;
161
162 struct drx_path_sel DM_RxPathSelTable;
163 /*------------------------Define global variable-----------------------------*/
164
165
166 /*------------------------Define local variable------------------------------*/
167 /*------------------------Define local variable------------------------------*/
168
169
170
171 /*---------------------Define local function prototype-----------------------*/
172 static  void    dm_check_rate_adaptive(struct net_device *dev);
173
174 static  void    dm_init_bandwidth_autoswitch(struct net_device *dev);
175 static  void    dm_bandwidth_autoswitch(struct net_device *dev);
176
177
178 static  void    dm_check_txpower_tracking(struct net_device *dev);
179
180
181
182
183
184 static  void    dm_bb_initialgain_restore(struct net_device *dev);
185
186
187 static  void    dm_bb_initialgain_backup(struct net_device *dev);
188
189 static  void dm_dig_init(struct net_device *dev);
190 static  void dm_ctrl_initgain_byrssi(struct net_device *dev);
191 static  void dm_ctrl_initgain_byrssi_highpwr(struct net_device *dev);
192 static  void dm_ctrl_initgain_byrssi_by_driverrssi(struct net_device *dev);
193 static  void dm_ctrl_initgain_byrssi_by_fwfalse_alarm(struct net_device *dev);
194 static  void dm_initial_gain(struct net_device *dev);
195 static  void dm_pd_th(struct net_device *dev);
196 static  void dm_cs_ratio(struct net_device *dev);
197
198 static  void dm_init_ctstoself(struct net_device *dev);
199 static  void dm_Init_WA_Broadcom_IOT(struct net_device *dev);
200
201 static  void    dm_check_edca_turbo(struct net_device *dev);
202
203 static  void dm_check_pbc_gpio(struct net_device *dev);
204
205
206 static  void dm_check_rx_path_selection(struct net_device *dev);
207 static  void dm_init_rxpath_selection(struct net_device *dev);
208 static  void dm_rxpath_sel_byrssi(struct net_device *dev);
209
210
211 static void dm_init_fsync(struct net_device *dev);
212 static void dm_deInit_fsync(struct net_device *dev);
213
214 static  void dm_check_txrateandretrycount(struct net_device *dev);
215 static  void dm_check_ac_dc_power(struct net_device *dev);
216
217 /*---------------------Define local function prototype-----------------------*/
218
219 static  void    dm_init_dynamic_txpower(struct net_device *dev);
220 static  void    dm_dynamic_txpower(struct net_device *dev);
221
222
223 static  void dm_send_rssi_tofw(struct net_device *dev);
224 static  void    dm_ctstoself(struct net_device *dev);
225 /*---------------------------Define function prototype------------------------*/
226
227 void init_hal_dm(struct net_device *dev)
228 {
229         struct r8192_priv *priv = rtllib_priv(dev);
230
231         priv->DM_Type = DM_Type_ByDriver;
232
233         priv->undecorated_smoothed_pwdb = -1;
234
235         dm_init_dynamic_txpower(dev);
236
237         init_rate_adaptive(dev);
238
239         dm_dig_init(dev);
240         dm_init_edca_turbo(dev);
241         dm_init_bandwidth_autoswitch(dev);
242         dm_init_fsync(dev);
243         dm_init_rxpath_selection(dev);
244         dm_init_ctstoself(dev);
245         if (IS_HARDWARE_TYPE_8192SE(dev))
246                 dm_Init_WA_Broadcom_IOT(dev);
247
248         INIT_DELAYED_WORK_RSL(&priv->gpio_change_rf_wq,
249                               (void *)dm_CheckRfCtrlGPIO, dev);
250 }
251
252 void deinit_hal_dm(struct net_device *dev)
253 {
254
255         dm_deInit_fsync(dev);
256
257 }
258
259 void hal_dm_watchdog(struct net_device *dev)
260 {
261         struct r8192_priv *priv = rtllib_priv(dev);
262
263         if (priv->being_init_adapter)
264                 return;
265
266         dm_check_ac_dc_power(dev);
267
268         dm_check_pbc_gpio(dev);
269         dm_check_txrateandretrycount(dev);
270         dm_check_edca_turbo(dev);
271
272         dm_check_rate_adaptive(dev);
273         dm_dynamic_txpower(dev);
274         dm_check_txpower_tracking(dev);
275
276         dm_ctrl_initgain_byrssi(dev);
277         dm_bandwidth_autoswitch(dev);
278
279         dm_check_rx_path_selection(dev);
280         dm_check_fsync(dev);
281
282         dm_send_rssi_tofw(dev);
283         dm_ctstoself(dev);
284 }
285
286 static void dm_check_ac_dc_power(struct net_device *dev)
287 {
288         struct r8192_priv *priv = rtllib_priv(dev);
289         static char *ac_dc_script = "/etc/acpi/wireless-rtl-ac-dc-power.sh";
290         char *argv[] = {ac_dc_script, DRV_NAME, NULL};
291         static char *envp[] = {"HOME=/",
292                         "TERM=linux",
293                         "PATH=/usr/bin:/bin",
294                          NULL};
295
296         if (priv->ResetProgress == RESET_TYPE_SILENT) {
297                 RT_TRACE((COMP_INIT | COMP_POWER | COMP_RF),
298                          "GPIOChangeRFWorkItemCallBack(): Silent Reset!!!!!!!\n");
299                 return;
300         }
301
302         if (priv->rtllib->state != RTLLIB_LINKED)
303                 return;
304         call_usermodehelper(ac_dc_script, argv, envp, UMH_WAIT_PROC);
305
306         return;
307 };
308
309
310 void init_rate_adaptive(struct net_device *dev)
311 {
312
313         struct r8192_priv *priv = rtllib_priv(dev);
314         struct rate_adaptive *pra = &priv->rate_adaptive;
315
316         pra->ratr_state = DM_RATR_STA_MAX;
317         pra->high2low_rssi_thresh_for_ra = RateAdaptiveTH_High;
318         pra->low2high_rssi_thresh_for_ra20M = RateAdaptiveTH_Low_20M+5;
319         pra->low2high_rssi_thresh_for_ra40M = RateAdaptiveTH_Low_40M+5;
320
321         pra->high_rssi_thresh_for_ra = RateAdaptiveTH_High+5;
322         pra->low_rssi_thresh_for_ra20M = RateAdaptiveTH_Low_20M;
323         pra->low_rssi_thresh_for_ra40M = RateAdaptiveTH_Low_40M;
324
325         if (priv->CustomerID == RT_CID_819x_Netcore)
326                 pra->ping_rssi_enable = 1;
327         else
328                 pra->ping_rssi_enable = 0;
329         pra->ping_rssi_thresh_for_ra = 15;
330
331
332         if (priv->rf_type == RF_2T4R) {
333                 pra->upper_rssi_threshold_ratr          =       0x8f0f0000;
334                 pra->middle_rssi_threshold_ratr         =       0x8f0ff000;
335                 pra->low_rssi_threshold_ratr            =       0x8f0ff001;
336                 pra->low_rssi_threshold_ratr_40M        =       0x8f0ff005;
337                 pra->low_rssi_threshold_ratr_20M        =       0x8f0ff001;
338                 pra->ping_rssi_ratr     =       0x0000000d;
339         } else if (priv->rf_type == RF_1T2R) {
340                 pra->upper_rssi_threshold_ratr          =       0x000fc000;
341                 pra->middle_rssi_threshold_ratr         =       0x000ff000;
342                 pra->low_rssi_threshold_ratr            =       0x000ff001;
343                 pra->low_rssi_threshold_ratr_40M        =       0x000ff005;
344                 pra->low_rssi_threshold_ratr_20M        =       0x000ff001;
345                 pra->ping_rssi_ratr     =       0x0000000d;
346         }
347
348 }
349
350
351 static void dm_check_rate_adaptive(struct net_device *dev)
352 {
353         struct r8192_priv *priv = rtllib_priv(dev);
354         struct rt_hi_throughput *pHTInfo = priv->rtllib->pHTInfo;
355         struct rate_adaptive *pra = &priv->rate_adaptive;
356         u32 currentRATR, targetRATR = 0;
357         u32 LowRSSIThreshForRA = 0, HighRSSIThreshForRA = 0;
358         bool bshort_gi_enabled = false;
359         static u8 ping_rssi_state;
360
361         if (!priv->up) {
362                 RT_TRACE(COMP_RATE,
363                          "<---- dm_check_rate_adaptive(): driver is going to unload\n");
364                 return;
365         }
366
367         if (pra->rate_adaptive_disabled)
368                 return;
369
370         if (!(priv->rtllib->mode == WIRELESS_MODE_N_24G ||
371             priv->rtllib->mode == WIRELESS_MODE_N_5G))
372                 return;
373
374         if (priv->rtllib->state == RTLLIB_LINKED) {
375
376                 bshort_gi_enabled = (pHTInfo->bCurTxBW40MHz &&
377                                      pHTInfo->bCurShortGI40MHz) ||
378                                     (!pHTInfo->bCurTxBW40MHz &&
379                                      pHTInfo->bCurShortGI20MHz);
380
381                 pra->upper_rssi_threshold_ratr =
382                                 (pra->upper_rssi_threshold_ratr & (~BIT31)) |
383                                 ((bshort_gi_enabled) ? BIT31 : 0);
384
385                 pra->middle_rssi_threshold_ratr =
386                                 (pra->middle_rssi_threshold_ratr & (~BIT31)) |
387                                 ((bshort_gi_enabled) ? BIT31 : 0);
388
389                 if (priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20) {
390                         pra->low_rssi_threshold_ratr =
391                                 (pra->low_rssi_threshold_ratr_40M & (~BIT31)) |
392                                 ((bshort_gi_enabled) ? BIT31 : 0);
393                 } else {
394                         pra->low_rssi_threshold_ratr =
395                                 (pra->low_rssi_threshold_ratr_20M & (~BIT31)) |
396                                 ((bshort_gi_enabled) ? BIT31 : 0);
397                 }
398                 pra->ping_rssi_ratr =
399                                 (pra->ping_rssi_ratr & (~BIT31)) |
400                                 ((bshort_gi_enabled) ? BIT31 : 0);
401
402                 if (pra->ratr_state == DM_RATR_STA_HIGH) {
403                         HighRSSIThreshForRA = pra->high2low_rssi_thresh_for_ra;
404                         LowRSSIThreshForRA = (priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20) ?
405                                         (pra->low_rssi_thresh_for_ra40M) : (pra->low_rssi_thresh_for_ra20M);
406                 } else if (pra->ratr_state == DM_RATR_STA_LOW) {
407                         HighRSSIThreshForRA = pra->high_rssi_thresh_for_ra;
408                         LowRSSIThreshForRA = (priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20) ?
409                                         (pra->low2high_rssi_thresh_for_ra40M) : (pra->low2high_rssi_thresh_for_ra20M);
410                 } else {
411                         HighRSSIThreshForRA = pra->high_rssi_thresh_for_ra;
412                         LowRSSIThreshForRA = (priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20) ?
413                                         (pra->low_rssi_thresh_for_ra40M) : (pra->low_rssi_thresh_for_ra20M);
414                 }
415
416                 if (priv->undecorated_smoothed_pwdb >=
417                     (long)HighRSSIThreshForRA) {
418                         pra->ratr_state = DM_RATR_STA_HIGH;
419                         targetRATR = pra->upper_rssi_threshold_ratr;
420                 } else if (priv->undecorated_smoothed_pwdb >=
421                            (long)LowRSSIThreshForRA) {
422                         pra->ratr_state = DM_RATR_STA_MIDDLE;
423                         targetRATR = pra->middle_rssi_threshold_ratr;
424                 } else {
425                         pra->ratr_state = DM_RATR_STA_LOW;
426                         targetRATR = pra->low_rssi_threshold_ratr;
427                 }
428
429                 if (pra->ping_rssi_enable) {
430                         if (priv->undecorated_smoothed_pwdb <
431                             (long)(pra->ping_rssi_thresh_for_ra+5)) {
432                                 if ((priv->undecorated_smoothed_pwdb <
433                                      (long)pra->ping_rssi_thresh_for_ra) ||
434                                     ping_rssi_state) {
435                                         pra->ratr_state = DM_RATR_STA_LOW;
436                                         targetRATR = pra->ping_rssi_ratr;
437                                         ping_rssi_state = 1;
438                                 }
439                         } else {
440                                 ping_rssi_state = 0;
441                         }
442                 }
443
444                 if (priv->rtllib->GetHalfNmodeSupportByAPsHandler(dev))
445                         targetRATR &=  0xf00fffff;
446
447                 currentRATR = read_nic_dword(dev, RATR0);
448                 if (targetRATR !=  currentRATR) {
449                         u32 ratr_value;
450
451                         ratr_value = targetRATR;
452                         RT_TRACE(COMP_RATE,
453                                  "currentRATR = %x, targetRATR = %x\n",
454                                  currentRATR, targetRATR);
455                         if (priv->rf_type == RF_1T2R)
456                                 ratr_value &= ~(RATE_ALL_OFDM_2SS);
457                         write_nic_dword(dev, RATR0, ratr_value);
458                         write_nic_byte(dev, UFWP, 1);
459
460                         pra->last_ratr = targetRATR;
461                 }
462
463         } else {
464                 pra->ratr_state = DM_RATR_STA_MAX;
465         }
466 }
467
468 static void dm_init_bandwidth_autoswitch(struct net_device *dev)
469 {
470         struct r8192_priv *priv = rtllib_priv(dev);
471
472         priv->rtllib->bandwidth_auto_switch.threshold_20Mhzto40Mhz = BW_AUTO_SWITCH_LOW_HIGH;
473         priv->rtllib->bandwidth_auto_switch.threshold_40Mhzto20Mhz = BW_AUTO_SWITCH_HIGH_LOW;
474         priv->rtllib->bandwidth_auto_switch.bforced_tx20Mhz = false;
475         priv->rtllib->bandwidth_auto_switch.bautoswitch_enable = false;
476 }
477
478 static void dm_bandwidth_autoswitch(struct net_device *dev)
479 {
480         struct r8192_priv *priv = rtllib_priv(dev);
481
482         if (priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20 ||
483            !priv->rtllib->bandwidth_auto_switch.bautoswitch_enable)
484                 return;
485         if (priv->rtllib->bandwidth_auto_switch.bforced_tx20Mhz == false) {
486                 if (priv->undecorated_smoothed_pwdb <=
487                     priv->rtllib->bandwidth_auto_switch.threshold_40Mhzto20Mhz)
488                         priv->rtllib->bandwidth_auto_switch.bforced_tx20Mhz = true;
489         } else {
490                 if (priv->undecorated_smoothed_pwdb >=
491                     priv->rtllib->bandwidth_auto_switch.threshold_20Mhzto40Mhz)
492                         priv->rtllib->bandwidth_auto_switch.bforced_tx20Mhz = false;
493         }
494 }
495
496 static u32 OFDMSwingTable[OFDM_Table_Length] = {
497         0x7f8001fe,
498         0x71c001c7,
499         0x65400195,
500         0x5a400169,
501         0x50800142,
502         0x47c0011f,
503         0x40000100,
504         0x390000e4,
505         0x32c000cb,
506         0x2d4000b5,
507         0x288000a2,
508         0x24000090,
509         0x20000080,
510         0x1c800072,
511         0x19800066,
512         0x26c0005b,
513         0x24400051,
514         0x12000048,
515         0x10000040
516 };
517
518 static u8       CCKSwingTable_Ch1_Ch13[CCK_Table_length][8] = {
519         {0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04},
520         {0x30, 0x2f, 0x29, 0x21, 0x19, 0x10, 0x08, 0x03},
521         {0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03},
522         {0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03},
523         {0x22, 0x21, 0x1d, 0x18, 0x11, 0x0b, 0x06, 0x02},
524         {0x1f, 0x1e, 0x1a, 0x15, 0x10, 0x0a, 0x05, 0x02},
525         {0x1b, 0x1a, 0x17, 0x13, 0x0e, 0x09, 0x04, 0x02},
526         {0x18, 0x17, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x02},
527         {0x16, 0x15, 0x12, 0x0f, 0x0b, 0x07, 0x04, 0x01},
528         {0x13, 0x13, 0x10, 0x0d, 0x0a, 0x06, 0x03, 0x01},
529         {0x11, 0x11, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01},
530         {0x0f, 0x0f, 0x0d, 0x0b, 0x08, 0x05, 0x03, 0x01}
531 };
532
533 static u8       CCKSwingTable_Ch14[CCK_Table_length][8] = {
534         {0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00},
535         {0x30, 0x2f, 0x29, 0x18, 0x00, 0x00, 0x00, 0x00},
536         {0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00},
537         {0x26, 0x25, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00},
538         {0x22, 0x21, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00},
539         {0x1f, 0x1e, 0x1a, 0x0f, 0x00, 0x00, 0x00, 0x00},
540         {0x1b, 0x1a, 0x17, 0x0e, 0x00, 0x00, 0x00, 0x00},
541         {0x18, 0x17, 0x15, 0x0c, 0x00, 0x00, 0x00, 0x00},
542         {0x16, 0x15, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00},
543         {0x13, 0x13, 0x10, 0x0a, 0x00, 0x00, 0x00, 0x00},
544         {0x11, 0x11, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00},
545         {0x0f, 0x0f, 0x0d, 0x08, 0x00, 0x00, 0x00, 0x00}
546 };
547
548 #define         Pw_Track_Flag                           0x11d
549 #define         Tssi_Mea_Value                          0x13c
550 #define         Tssi_Report_Value1                      0x134
551 #define         Tssi_Report_Value2                      0x13e
552 #define         FW_Busy_Flag                            0x13f
553
554 static void dm_tx_update_tssi_weak_signal(struct net_device *dev, u8 RF_Type)
555 {
556         struct r8192_priv *p = rtllib_priv(dev);
557
558         if (RF_Type == RF_2T4R) {
559                 if ((p->rfa_txpowertrackingindex > 0) &&
560                     (p->rfc_txpowertrackingindex > 0)) {
561                         p->rfa_txpowertrackingindex--;
562                         if (p->rfa_txpowertrackingindex_real > 4) {
563                                 p->rfa_txpowertrackingindex_real--;
564                                 rtl8192_setBBreg(dev, rOFDM0_XATxIQImbalance,
565                                                  bMaskDWord,
566                                                  dm_tx_bb_gain[p->rfa_txpowertrackingindex_real]);
567                         }
568
569                         p->rfc_txpowertrackingindex--;
570                         if (p->rfc_txpowertrackingindex_real > 4) {
571                                 p->rfc_txpowertrackingindex_real--;
572                                 rtl8192_setBBreg(dev,
573                                                  rOFDM0_XCTxIQImbalance,
574                                                  bMaskDWord,
575                                                  dm_tx_bb_gain[p->rfc_txpowertrackingindex_real]);
576                         }
577                 } else {
578                         rtl8192_setBBreg(dev, rOFDM0_XATxIQImbalance,
579                                          bMaskDWord,
580                                          dm_tx_bb_gain[4]);
581                         rtl8192_setBBreg(dev,
582                                          rOFDM0_XCTxIQImbalance,
583                                          bMaskDWord, dm_tx_bb_gain[4]);
584                 }
585         } else {
586                 if (p->rfa_txpowertrackingindex > 0) {
587                         p->rfa_txpowertrackingindex--;
588                         if (p->rfa_txpowertrackingindex_real > 4) {
589                                 p->rfa_txpowertrackingindex_real--;
590                                 rtl8192_setBBreg(dev,
591                                                  rOFDM0_XATxIQImbalance,
592                                                  bMaskDWord,
593                                                  dm_tx_bb_gain[p->rfa_txpowertrackingindex_real]);
594                         }
595                 } else {
596                         rtl8192_setBBreg(dev, rOFDM0_XATxIQImbalance,
597                                          bMaskDWord, dm_tx_bb_gain[4]);
598                 }
599         }
600 }
601
602 static void dm_tx_update_tssi_strong_signal(struct net_device *dev, u8 RF_Type)
603 {
604         struct r8192_priv *p = rtllib_priv(dev);
605
606         if (RF_Type == RF_2T4R) {
607                 if ((p->rfa_txpowertrackingindex < TxBBGainTableLength - 1) &&
608                     (p->rfc_txpowertrackingindex < TxBBGainTableLength - 1)) {
609                         p->rfa_txpowertrackingindex++;
610                         p->rfa_txpowertrackingindex_real++;
611                         rtl8192_setBBreg(dev,
612                                  rOFDM0_XATxIQImbalance,
613                                  bMaskDWord,
614                                  dm_tx_bb_gain[p->rfa_txpowertrackingindex_real]);
615                         p->rfc_txpowertrackingindex++;
616                         p->rfc_txpowertrackingindex_real++;
617                         rtl8192_setBBreg(dev,
618                                  rOFDM0_XCTxIQImbalance,
619                                  bMaskDWord,
620                                  dm_tx_bb_gain[p->rfc_txpowertrackingindex_real]);
621                 } else {
622                         rtl8192_setBBreg(dev,
623                                  rOFDM0_XATxIQImbalance,
624                                  bMaskDWord,
625                                  dm_tx_bb_gain[TxBBGainTableLength - 1]);
626                         rtl8192_setBBreg(dev, rOFDM0_XCTxIQImbalance,
627                                          bMaskDWord,
628                                          dm_tx_bb_gain[TxBBGainTableLength - 1]);
629                 }
630         } else {
631                 if (p->rfa_txpowertrackingindex < (TxBBGainTableLength - 1)) {
632                         p->rfa_txpowertrackingindex++;
633                         p->rfa_txpowertrackingindex_real++;
634                         rtl8192_setBBreg(dev, rOFDM0_XATxIQImbalance,
635                                          bMaskDWord,
636                                          dm_tx_bb_gain[p->rfa_txpowertrackingindex_real]);
637                 } else {
638                         rtl8192_setBBreg(dev, rOFDM0_XATxIQImbalance,
639                                          bMaskDWord,
640                                          dm_tx_bb_gain[TxBBGainTableLength - 1]);
641                 }
642         }
643 }
644
645 static void dm_TXPowerTrackingCallback_TSSI(struct net_device *dev)
646 {
647         struct r8192_priv *priv = rtllib_priv(dev);
648         bool    bHighpowerstate, viviflag = false;
649         struct dcmd_txcmd tx_cmd;
650         u8      powerlevelOFDM24G;
651         int     i = 0, j = 0, k = 0;
652         u8      RF_Type, tmp_report[5] = {0, 0, 0, 0, 0};
653         u32     Value;
654         u8      Pwr_Flag;
655         u16     Avg_TSSI_Meas, TSSI_13dBm, Avg_TSSI_Meas_from_driver = 0;
656         u32     delta = 0;
657
658         RT_TRACE(COMP_POWER_TRACKING, "%s()\n", __func__);
659         write_nic_byte(dev, Pw_Track_Flag, 0);
660         write_nic_byte(dev, FW_Busy_Flag, 0);
661         priv->rtllib->bdynamic_txpower_enable = false;
662         bHighpowerstate = priv->bDynamicTxHighPower;
663
664         powerlevelOFDM24G = (u8)(priv->Pwr_Track>>24);
665         RF_Type = priv->rf_type;
666         Value = (RF_Type<<8) | powerlevelOFDM24G;
667
668         RT_TRACE(COMP_POWER_TRACKING, "powerlevelOFDM24G = %x\n",
669                  powerlevelOFDM24G);
670
671
672         for (j = 0; j <= 30; j++) {
673
674                 tx_cmd.Op               = TXCMD_SET_TX_PWR_TRACKING;
675                 tx_cmd.Length   = 4;
676                 tx_cmd.Value            = Value;
677                 cmpk_message_handle_tx(dev, (u8 *)&tx_cmd,
678                                        DESC_PACKET_TYPE_INIT,
679                                        sizeof(struct dcmd_txcmd));
680                 mdelay(1);
681                 for (i = 0; i <= 30; i++) {
682                         Pwr_Flag = read_nic_byte(dev, Pw_Track_Flag);
683
684                         if (Pwr_Flag == 0) {
685                                 mdelay(1);
686
687                                 if (priv->bResetInProgress) {
688                                         RT_TRACE(COMP_POWER_TRACKING,
689                                                  "we are in silent reset progress, so return\n");
690                                         write_nic_byte(dev, Pw_Track_Flag, 0);
691                                         write_nic_byte(dev, FW_Busy_Flag, 0);
692                                         return;
693                                 }
694                                 if (priv->rtllib->eRFPowerState != eRfOn) {
695                                         RT_TRACE(COMP_POWER_TRACKING,
696                                                  "we are in power save, so return\n");
697                                         write_nic_byte(dev, Pw_Track_Flag, 0);
698                                         write_nic_byte(dev, FW_Busy_Flag, 0);
699                                         return;
700                                 }
701
702                                 continue;
703                         }
704
705                         Avg_TSSI_Meas = read_nic_word(dev, Tssi_Mea_Value);
706
707                         if (Avg_TSSI_Meas == 0) {
708                                 write_nic_byte(dev, Pw_Track_Flag, 0);
709                                 write_nic_byte(dev, FW_Busy_Flag, 0);
710                                 return;
711                         }
712
713                         for (k = 0; k < 5; k++) {
714                                 if (k != 4)
715                                         tmp_report[k] = read_nic_byte(dev,
716                                                          Tssi_Report_Value1+k);
717                                 else
718                                         tmp_report[k] = read_nic_byte(dev,
719                                                          Tssi_Report_Value2);
720
721                                 RT_TRACE(COMP_POWER_TRACKING,
722                                          "TSSI_report_value = %d\n",
723                                          tmp_report[k]);
724
725                                if (tmp_report[k] <= 20) {
726                                         viviflag = true;
727                                         break;
728                                 }
729                         }
730
731                         if (viviflag) {
732                                 write_nic_byte(dev, Pw_Track_Flag, 0);
733                                 viviflag = false;
734                                 RT_TRACE(COMP_POWER_TRACKING,
735                                          "we filted this data\n");
736                                 for (k = 0; k < 5; k++)
737                                         tmp_report[k] = 0;
738                                 break;
739                         }
740
741                         for (k = 0; k < 5; k++)
742                                 Avg_TSSI_Meas_from_driver += tmp_report[k];
743
744                         Avg_TSSI_Meas_from_driver *= 100 / 5;
745                         RT_TRACE(COMP_POWER_TRACKING,
746                                  "Avg_TSSI_Meas_from_driver = %d\n",
747                                  Avg_TSSI_Meas_from_driver);
748                         TSSI_13dBm = priv->TSSI_13dBm;
749                         RT_TRACE(COMP_POWER_TRACKING, "TSSI_13dBm = %d\n",
750                                  TSSI_13dBm);
751
752                         if (Avg_TSSI_Meas_from_driver > TSSI_13dBm)
753                                 delta = Avg_TSSI_Meas_from_driver - TSSI_13dBm;
754                         else
755                                 delta = TSSI_13dBm - Avg_TSSI_Meas_from_driver;
756
757                         if (delta <= E_FOR_TX_POWER_TRACK) {
758                                 priv->rtllib->bdynamic_txpower_enable = true;
759                                 write_nic_byte(dev, Pw_Track_Flag, 0);
760                                 write_nic_byte(dev, FW_Busy_Flag, 0);
761                                 RT_TRACE(COMP_POWER_TRACKING,
762                                          "tx power track is done\n");
763                                 RT_TRACE(COMP_POWER_TRACKING,
764                                          "priv->rfa_txpowertrackingindex = %d\n",
765                                          priv->rfa_txpowertrackingindex);
766                                 RT_TRACE(COMP_POWER_TRACKING,
767                                          "priv->rfa_txpowertrackingindex_real = %d\n",
768                                          priv->rfa_txpowertrackingindex_real);
769                                 RT_TRACE(COMP_POWER_TRACKING,
770                                          "priv->CCKPresentAttentuation_difference = %d\n",
771                                          priv->CCKPresentAttentuation_difference);
772                                 RT_TRACE(COMP_POWER_TRACKING,
773                                          "priv->CCKPresentAttentuation = %d\n",
774                                          priv->CCKPresentAttentuation);
775                                 return;
776                         }
777                         if (Avg_TSSI_Meas_from_driver < TSSI_13dBm - E_FOR_TX_POWER_TRACK)
778                                 dm_tx_update_tssi_weak_signal(dev, RF_Type);
779                         else
780                                 dm_tx_update_tssi_strong_signal(dev, RF_Type);
781
782                         if (RF_Type == RF_2T4R) {
783                                 priv->CCKPresentAttentuation_difference
784                                         = priv->rfa_txpowertrackingindex - priv->rfa_txpowertracking_default;
785                         } else {
786                                 priv->CCKPresentAttentuation_difference
787                                         = priv->rfa_txpowertrackingindex_real - priv->rfa_txpowertracking_default;
788                         }
789
790                         if (priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20)
791                                 priv->CCKPresentAttentuation =
792                                          priv->CCKPresentAttentuation_20Mdefault +
793                                          priv->CCKPresentAttentuation_difference;
794                         else
795                                 priv->CCKPresentAttentuation =
796                                          priv->CCKPresentAttentuation_40Mdefault +
797                                          priv->CCKPresentAttentuation_difference;
798
799                         if (priv->CCKPresentAttentuation > (CCKTxBBGainTableLength-1))
800                                 priv->CCKPresentAttentuation = CCKTxBBGainTableLength-1;
801                         if (priv->CCKPresentAttentuation < 0)
802                                 priv->CCKPresentAttentuation = 0;
803
804                         if (priv->CCKPresentAttentuation > -1 &&
805                             priv->CCKPresentAttentuation < CCKTxBBGainTableLength) {
806                                 if (priv->rtllib->current_network.channel == 14 &&
807                                     !priv->bcck_in_ch14) {
808                                         priv->bcck_in_ch14 = true;
809                                         dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
810                                 } else if (priv->rtllib->current_network.channel != 14 && priv->bcck_in_ch14) {
811                                         priv->bcck_in_ch14 = false;
812                                         dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
813                                 } else
814                                         dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
815                         }
816                         RT_TRACE(COMP_POWER_TRACKING,
817                                  "priv->rfa_txpowertrackingindex = %d\n",
818                                  priv->rfa_txpowertrackingindex);
819                         RT_TRACE(COMP_POWER_TRACKING,
820                                  "priv->rfa_txpowertrackingindex_real = %d\n",
821                                  priv->rfa_txpowertrackingindex_real);
822                         RT_TRACE(COMP_POWER_TRACKING,
823                                  "priv->CCKPresentAttentuation_difference = %d\n",
824                                  priv->CCKPresentAttentuation_difference);
825                         RT_TRACE(COMP_POWER_TRACKING,
826                                  "priv->CCKPresentAttentuation = %d\n",
827                                  priv->CCKPresentAttentuation);
828
829                         if (priv->CCKPresentAttentuation_difference <= -12 ||
830                             priv->CCKPresentAttentuation_difference >= 24) {
831                                 priv->rtllib->bdynamic_txpower_enable = true;
832                                 write_nic_byte(dev, Pw_Track_Flag, 0);
833                                 write_nic_byte(dev, FW_Busy_Flag, 0);
834                                 RT_TRACE(COMP_POWER_TRACKING,
835                                          "tx power track--->limited\n");
836                                 return;
837                         }
838
839                         write_nic_byte(dev, Pw_Track_Flag, 0);
840                         Avg_TSSI_Meas_from_driver = 0;
841                         for (k = 0; k < 5; k++)
842                                 tmp_report[k] = 0;
843                         break;
844                 }
845                 write_nic_byte(dev, FW_Busy_Flag, 0);
846         }
847         priv->rtllib->bdynamic_txpower_enable = true;
848         write_nic_byte(dev, Pw_Track_Flag, 0);
849 }
850
851 static void dm_TXPowerTrackingCallback_ThermalMeter(struct net_device *dev)
852 {
853 #define ThermalMeterVal 9
854         struct r8192_priv *priv = rtllib_priv(dev);
855         u32 tmpRegA, TempCCk;
856         u8 tmpOFDMindex, tmpCCKindex, tmpCCK20Mindex, tmpCCK40Mindex, tmpval;
857         int i = 0, CCKSwingNeedUpdate = 0;
858
859         if (!priv->btxpower_trackingInit) {
860                 tmpRegA = rtl8192_QueryBBReg(dev, rOFDM0_XATxIQImbalance,
861                                              bMaskDWord);
862                 for (i = 0; i < OFDM_Table_Length; i++) {
863                         if (tmpRegA == OFDMSwingTable[i]) {
864                                 priv->OFDM_index[0] = (u8)i;
865                                 RT_TRACE(COMP_POWER_TRACKING,
866                                          "Initial reg0x%x = 0x%x, OFDM_index = 0x%x\n",
867                                          rOFDM0_XATxIQImbalance, tmpRegA,
868                                          priv->OFDM_index[0]);
869                         }
870                 }
871
872                 TempCCk = rtl8192_QueryBBReg(dev, rCCK0_TxFilter1, bMaskByte2);
873                 for (i = 0; i < CCK_Table_length; i++) {
874                         if (TempCCk == (u32)CCKSwingTable_Ch1_Ch13[i][0]) {
875                                 priv->CCK_index = (u8) i;
876                                 RT_TRACE(COMP_POWER_TRACKING,
877                                          "Initial reg0x%x = 0x%x, CCK_index = 0x%x\n",
878                                          rCCK0_TxFilter1, TempCCk,
879                                          priv->CCK_index);
880                                 break;
881                         }
882                 }
883                 priv->btxpower_trackingInit = true;
884                 return;
885         }
886
887         tmpRegA = rtl8192_phy_QueryRFReg(dev, RF90_PATH_A, 0x12, 0x078);
888         RT_TRACE(COMP_POWER_TRACKING, "Readback ThermalMeterA = %d\n", tmpRegA);
889         if (tmpRegA < 3 || tmpRegA > 13)
890                 return;
891         if (tmpRegA >= 12)
892                 tmpRegA = 12;
893         RT_TRACE(COMP_POWER_TRACKING, "Valid ThermalMeterA = %d\n", tmpRegA);
894         priv->ThermalMeter[0] = ThermalMeterVal;
895         priv->ThermalMeter[1] = ThermalMeterVal;
896
897         if (priv->ThermalMeter[0] >= (u8)tmpRegA) {
898                 tmpOFDMindex = tmpCCK20Mindex = 6+(priv->ThermalMeter[0] -
899                               (u8)tmpRegA);
900                 tmpCCK40Mindex = tmpCCK20Mindex - 6;
901                 if (tmpOFDMindex >= OFDM_Table_Length)
902                         tmpOFDMindex = OFDM_Table_Length-1;
903                 if (tmpCCK20Mindex >= CCK_Table_length)
904                         tmpCCK20Mindex = CCK_Table_length-1;
905                 if (tmpCCK40Mindex >= CCK_Table_length)
906                         tmpCCK40Mindex = CCK_Table_length-1;
907         } else {
908                 tmpval = ((u8)tmpRegA - priv->ThermalMeter[0]);
909                 if (tmpval >= 6)
910                         tmpOFDMindex = tmpCCK20Mindex = 0;
911                 else
912                         tmpOFDMindex = tmpCCK20Mindex = 6 - tmpval;
913                 tmpCCK40Mindex = 0;
914         }
915         if (priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20)
916                 tmpCCKindex = tmpCCK40Mindex;
917         else
918                 tmpCCKindex = tmpCCK20Mindex;
919
920         priv->Record_CCK_20Mindex = tmpCCK20Mindex;
921         priv->Record_CCK_40Mindex = tmpCCK40Mindex;
922         RT_TRACE(COMP_POWER_TRACKING,
923                  "Record_CCK_20Mindex / Record_CCK_40Mindex = %d / %d.\n",
924                  priv->Record_CCK_20Mindex, priv->Record_CCK_40Mindex);
925
926         if (priv->rtllib->current_network.channel == 14 &&
927             !priv->bcck_in_ch14) {
928                 priv->bcck_in_ch14 = true;
929                 CCKSwingNeedUpdate = 1;
930         } else if (priv->rtllib->current_network.channel != 14 &&
931                    priv->bcck_in_ch14) {
932                 priv->bcck_in_ch14 = false;
933                 CCKSwingNeedUpdate = 1;
934         }
935
936         if (priv->CCK_index != tmpCCKindex) {
937                 priv->CCK_index = tmpCCKindex;
938                 CCKSwingNeedUpdate = 1;
939         }
940
941         if (CCKSwingNeedUpdate)
942                 dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
943         if (priv->OFDM_index[0] != tmpOFDMindex) {
944                 priv->OFDM_index[0] = tmpOFDMindex;
945                 rtl8192_setBBreg(dev, rOFDM0_XATxIQImbalance, bMaskDWord,
946                                  OFDMSwingTable[priv->OFDM_index[0]]);
947                 RT_TRACE(COMP_POWER_TRACKING, "Update OFDMSwing[%d] = 0x%x\n",
948                          priv->OFDM_index[0],
949                          OFDMSwingTable[priv->OFDM_index[0]]);
950         }
951         priv->txpower_count = 0;
952 }
953
954 void    dm_txpower_trackingcallback(void *data)
955 {
956         struct r8192_priv *priv = container_of_dwork_rsl(data,
957                                   struct r8192_priv, txpower_tracking_wq);
958         struct net_device *dev = priv->rtllib->dev;
959
960         if (priv->IC_Cut >= IC_VersionCut_D)
961                 dm_TXPowerTrackingCallback_TSSI(dev);
962         else
963                 dm_TXPowerTrackingCallback_ThermalMeter(dev);
964 }
965
966 static void dm_InitializeTXPowerTracking_TSSI(struct net_device *dev)
967 {
968
969         struct r8192_priv *priv = rtllib_priv(dev);
970
971         priv->btxpower_tracking = true;
972         priv->txpower_count       = 0;
973         priv->btxpower_trackingInit = false;
974
975 }
976
977 static void dm_InitializeTXPowerTracking_ThermalMeter(struct net_device *dev)
978 {
979         struct r8192_priv *priv = rtllib_priv(dev);
980
981
982         if (priv->rtllib->FwRWRF)
983                 priv->btxpower_tracking = true;
984         else
985                 priv->btxpower_tracking = false;
986         priv->txpower_count       = 0;
987         priv->btxpower_trackingInit = false;
988         RT_TRACE(COMP_POWER_TRACKING, "pMgntInfo->bTXPowerTracking = %d\n",
989                  priv->btxpower_tracking);
990 }
991
992 void dm_initialize_txpower_tracking(struct net_device *dev)
993 {
994         struct r8192_priv *priv = rtllib_priv(dev);
995
996         if (priv->IC_Cut >= IC_VersionCut_D)
997                 dm_InitializeTXPowerTracking_TSSI(dev);
998         else
999                 dm_InitializeTXPowerTracking_ThermalMeter(dev);
1000 }
1001
1002 static void dm_CheckTXPowerTracking_TSSI(struct net_device *dev)
1003 {
1004         struct r8192_priv *priv = rtllib_priv(dev);
1005         static u32 tx_power_track_counter;
1006
1007         RT_TRACE(COMP_POWER_TRACKING, "%s()\n", __func__);
1008         if (read_nic_byte(dev, 0x11e) == 1)
1009                 return;
1010         if (!priv->btxpower_tracking)
1011                 return;
1012         tx_power_track_counter++;
1013
1014
1015          if (tx_power_track_counter >= 180) {
1016                 queue_delayed_work_rsl(priv->priv_wq,
1017                                        &priv->txpower_tracking_wq, 0);
1018                 tx_power_track_counter = 0;
1019         }
1020
1021 }
1022 static void dm_CheckTXPowerTracking_ThermalMeter(struct net_device *dev)
1023 {
1024         struct r8192_priv *priv = rtllib_priv(dev);
1025         static u8       TM_Trigger;
1026         u8              TxPowerCheckCnt = 0;
1027
1028         if (IS_HARDWARE_TYPE_8192SE(dev))
1029                 TxPowerCheckCnt = 5;
1030         else
1031                 TxPowerCheckCnt = 2;
1032         if (!priv->btxpower_tracking)
1033                 return;
1034
1035         if (priv->txpower_count  <= TxPowerCheckCnt) {
1036                 priv->txpower_count++;
1037                 return;
1038         }
1039
1040         if (!TM_Trigger) {
1041                 {
1042                 rtl8192_phy_SetRFReg(dev, RF90_PATH_A, 0x02, bMask12Bits, 0x4d);
1043                 rtl8192_phy_SetRFReg(dev, RF90_PATH_A, 0x02, bMask12Bits, 0x4f);
1044                 rtl8192_phy_SetRFReg(dev, RF90_PATH_A, 0x02, bMask12Bits, 0x4d);
1045                 rtl8192_phy_SetRFReg(dev, RF90_PATH_A, 0x02, bMask12Bits, 0x4f);
1046                 }
1047                 TM_Trigger = 1;
1048                 return;
1049         }
1050         netdev_info(dev, "===============>Schedule TxPowerTrackingWorkItem\n");
1051         queue_delayed_work_rsl(priv->priv_wq, &priv->txpower_tracking_wq, 0);
1052         TM_Trigger = 0;
1053
1054 }
1055
1056 static void dm_check_txpower_tracking(struct net_device *dev)
1057 {
1058         struct r8192_priv *priv = rtllib_priv(dev);
1059
1060         if (priv->IC_Cut >= IC_VersionCut_D)
1061                 dm_CheckTXPowerTracking_TSSI(dev);
1062         else
1063                 dm_CheckTXPowerTracking_ThermalMeter(dev);
1064 }
1065
1066 static void dm_CCKTxPowerAdjust_TSSI(struct net_device *dev, bool  bInCH14)
1067 {
1068         u32 TempVal;
1069         struct r8192_priv *priv = rtllib_priv(dev);
1070         u8 attenuation = (u8)priv->CCKPresentAttentuation;
1071
1072         TempVal = 0;
1073         if (!bInCH14) {
1074                 TempVal = (u32)(dm_cck_tx_bb_gain[attenuation][0] +
1075                           (dm_cck_tx_bb_gain[attenuation][1] << 8));
1076
1077                 rtl8192_setBBreg(dev, rCCK0_TxFilter1, bMaskHWord, TempVal);
1078                 TempVal = (u32)((dm_cck_tx_bb_gain[attenuation][2]) +
1079                           (dm_cck_tx_bb_gain[attenuation][3] << 8) +
1080                           (dm_cck_tx_bb_gain[attenuation][4] << 16)+
1081                           (dm_cck_tx_bb_gain[attenuation][5] << 24));
1082                 rtl8192_setBBreg(dev, rCCK0_TxFilter2, bMaskDWord, TempVal);
1083                 TempVal = (u32)(dm_cck_tx_bb_gain[attenuation][6] +
1084                           (dm_cck_tx_bb_gain[attenuation][7] << 8));
1085
1086                 rtl8192_setBBreg(dev, rCCK0_DebugPort, bMaskLWord, TempVal);
1087         } else {
1088                 TempVal = (u32)((dm_cck_tx_bb_gain_ch14[attenuation][0]) +
1089                           (dm_cck_tx_bb_gain_ch14[attenuation][1] << 8));
1090
1091                 rtl8192_setBBreg(dev, rCCK0_TxFilter1, bMaskHWord, TempVal);
1092                 TempVal = (u32)((dm_cck_tx_bb_gain_ch14[attenuation][2]) +
1093                           (dm_cck_tx_bb_gain_ch14[attenuation][3] << 8) +
1094                           (dm_cck_tx_bb_gain_ch14[attenuation][4] << 16)+
1095                           (dm_cck_tx_bb_gain_ch14[attenuation][5] << 24));
1096                 rtl8192_setBBreg(dev, rCCK0_TxFilter2, bMaskDWord, TempVal);
1097                 TempVal = (u32)((dm_cck_tx_bb_gain_ch14[attenuation][6]) +
1098                           (dm_cck_tx_bb_gain_ch14[attenuation][7] << 8));
1099
1100                 rtl8192_setBBreg(dev, rCCK0_DebugPort, bMaskLWord, TempVal);
1101         }
1102 }
1103
1104 static void dm_CCKTxPowerAdjust_ThermalMeter(struct net_device *dev,
1105                                              bool bInCH14)
1106 {
1107         u32 TempVal;
1108         struct r8192_priv *priv = rtllib_priv(dev);
1109
1110         TempVal = 0;
1111         if (!bInCH14) {
1112                 TempVal = CCKSwingTable_Ch1_Ch13[priv->CCK_index][0] +
1113                           (CCKSwingTable_Ch1_Ch13[priv->CCK_index][1] << 8);
1114                 rtl8192_setBBreg(dev, rCCK0_TxFilter1, bMaskHWord, TempVal);
1115                 RT_TRACE(COMP_POWER_TRACKING,
1116                          "CCK not chnl 14, reg 0x%x = 0x%x\n", rCCK0_TxFilter1,
1117                          TempVal);
1118                 TempVal = CCKSwingTable_Ch1_Ch13[priv->CCK_index][2] +
1119                           (CCKSwingTable_Ch1_Ch13[priv->CCK_index][3] << 8) +
1120                           (CCKSwingTable_Ch1_Ch13[priv->CCK_index][4] << 16)+
1121                           (CCKSwingTable_Ch1_Ch13[priv->CCK_index][5] << 24);
1122                 rtl8192_setBBreg(dev, rCCK0_TxFilter2, bMaskDWord, TempVal);
1123                 RT_TRACE(COMP_POWER_TRACKING,
1124                          "CCK not chnl 14, reg 0x%x = 0x%x\n", rCCK0_TxFilter2,
1125                          TempVal);
1126                 TempVal = CCKSwingTable_Ch1_Ch13[priv->CCK_index][6] +
1127                           (CCKSwingTable_Ch1_Ch13[priv->CCK_index][7] << 8);
1128
1129                 rtl8192_setBBreg(dev, rCCK0_DebugPort, bMaskLWord, TempVal);
1130                 RT_TRACE(COMP_POWER_TRACKING,
1131                          "CCK not chnl 14, reg 0x%x = 0x%x\n", rCCK0_DebugPort,
1132                          TempVal);
1133         } else {
1134                 TempVal = CCKSwingTable_Ch14[priv->CCK_index][0] +
1135                           (CCKSwingTable_Ch14[priv->CCK_index][1] << 8);
1136
1137                 rtl8192_setBBreg(dev, rCCK0_TxFilter1, bMaskHWord, TempVal);
1138                 RT_TRACE(COMP_POWER_TRACKING, "CCK chnl 14, reg 0x%x = 0x%x\n",
1139                         rCCK0_TxFilter1, TempVal);
1140                 TempVal = CCKSwingTable_Ch14[priv->CCK_index][2] +
1141                           (CCKSwingTable_Ch14[priv->CCK_index][3] << 8) +
1142                           (CCKSwingTable_Ch14[priv->CCK_index][4] << 16)+
1143                           (CCKSwingTable_Ch14[priv->CCK_index][5] << 24);
1144                 rtl8192_setBBreg(dev, rCCK0_TxFilter2, bMaskDWord, TempVal);
1145                 RT_TRACE(COMP_POWER_TRACKING, "CCK chnl 14, reg 0x%x = 0x%x\n",
1146                         rCCK0_TxFilter2, TempVal);
1147                 TempVal = CCKSwingTable_Ch14[priv->CCK_index][6] +
1148                           (CCKSwingTable_Ch14[priv->CCK_index][7]<<8);
1149
1150                 rtl8192_setBBreg(dev, rCCK0_DebugPort, bMaskLWord, TempVal);
1151                 RT_TRACE(COMP_POWER_TRACKING, "CCK chnl 14, reg 0x%x = 0x%x\n",
1152                         rCCK0_DebugPort, TempVal);
1153         }
1154 }
1155
1156 void dm_cck_txpower_adjust(struct net_device *dev, bool  binch14)
1157 {
1158         struct r8192_priv *priv = rtllib_priv(dev);
1159
1160         if (priv->IC_Cut >= IC_VersionCut_D)
1161                 dm_CCKTxPowerAdjust_TSSI(dev, binch14);
1162         else
1163                 dm_CCKTxPowerAdjust_ThermalMeter(dev, binch14);
1164 }
1165
1166 static void dm_txpower_reset_recovery(struct net_device *dev)
1167 {
1168         struct r8192_priv *priv = rtllib_priv(dev);
1169
1170         RT_TRACE(COMP_POWER_TRACKING, "Start Reset Recovery ==>\n");
1171         rtl8192_setBBreg(dev, rOFDM0_XATxIQImbalance, bMaskDWord,
1172                          dm_tx_bb_gain[priv->rfa_txpowertrackingindex]);
1173         RT_TRACE(COMP_POWER_TRACKING, "Reset Recovery: Fill in 0xc80 is %08x\n",
1174                  dm_tx_bb_gain[priv->rfa_txpowertrackingindex]);
1175         RT_TRACE(COMP_POWER_TRACKING,
1176                  "Reset Recovery: Fill in RFA_txPowerTrackingIndex is %x\n",
1177                  priv->rfa_txpowertrackingindex);
1178         RT_TRACE(COMP_POWER_TRACKING,
1179                  "Reset Recovery : RF A I/Q Amplify Gain is %d\n",
1180                  dm_tx_bb_gain_idx_to_amplify(priv->rfa_txpowertrackingindex));
1181         RT_TRACE(COMP_POWER_TRACKING,
1182                  "Reset Recovery: CCK Attenuation is %d dB\n",
1183                  priv->CCKPresentAttentuation);
1184         dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
1185
1186         rtl8192_setBBreg(dev, rOFDM0_XCTxIQImbalance, bMaskDWord,
1187                          dm_tx_bb_gain[priv->rfc_txpowertrackingindex]);
1188         RT_TRACE(COMP_POWER_TRACKING, "Reset Recovery: Fill in 0xc90 is %08x\n",
1189                  dm_tx_bb_gain[priv->rfc_txpowertrackingindex]);
1190         RT_TRACE(COMP_POWER_TRACKING,
1191                  "Reset Recovery: Fill in RFC_txPowerTrackingIndex is %x\n",
1192                  priv->rfc_txpowertrackingindex);
1193         RT_TRACE(COMP_POWER_TRACKING,
1194                  "Reset Recovery : RF C I/Q Amplify Gain is %d\n",
1195                  dm_tx_bb_gain_idx_to_amplify(priv->rfc_txpowertrackingindex));
1196 }
1197
1198 void dm_restore_dynamic_mechanism_state(struct net_device *dev)
1199 {
1200         struct r8192_priv *priv = rtllib_priv(dev);
1201         u32     reg_ratr = priv->rate_adaptive.last_ratr;
1202         u32 ratr_value;
1203
1204         if (!priv->up) {
1205                 RT_TRACE(COMP_RATE,
1206                          "<---- dm_restore_dynamic_mechanism_state(): driver is going to unload\n");
1207                 return;
1208         }
1209
1210         if (priv->rate_adaptive.rate_adaptive_disabled)
1211                 return;
1212         if (!(priv->rtllib->mode == WIRELESS_MODE_N_24G ||
1213               priv->rtllib->mode == WIRELESS_MODE_N_5G))
1214                 return;
1215         ratr_value = reg_ratr;
1216         if (priv->rf_type == RF_1T2R)
1217                 ratr_value &= ~(RATE_ALL_OFDM_2SS);
1218         write_nic_dword(dev, RATR0, ratr_value);
1219         write_nic_byte(dev, UFWP, 1);
1220         if (priv->btxpower_trackingInit && priv->btxpower_tracking)
1221                 dm_txpower_reset_recovery(dev);
1222
1223         dm_bb_initialgain_restore(dev);
1224
1225 }
1226
1227 static void dm_bb_initialgain_restore(struct net_device *dev)
1228 {
1229         struct r8192_priv *priv = rtllib_priv(dev);
1230         u32 bit_mask = 0x7f;
1231
1232         if (dm_digtable.dig_algorithm == DIG_ALGO_BY_RSSI)
1233                 return;
1234
1235         rtl8192_setBBreg(dev, UFWP, bMaskByte1, 0x8);
1236         rtl8192_setBBreg(dev, rOFDM0_XAAGCCore1, bit_mask,
1237                          (u32)priv->initgain_backup.xaagccore1);
1238         rtl8192_setBBreg(dev, rOFDM0_XBAGCCore1, bit_mask,
1239                          (u32)priv->initgain_backup.xbagccore1);
1240         rtl8192_setBBreg(dev, rOFDM0_XCAGCCore1, bit_mask,
1241                          (u32)priv->initgain_backup.xcagccore1);
1242         rtl8192_setBBreg(dev, rOFDM0_XDAGCCore1, bit_mask,
1243                          (u32)priv->initgain_backup.xdagccore1);
1244         bit_mask  = bMaskByte2;
1245         rtl8192_setBBreg(dev, rCCK0_CCA, bit_mask,
1246                          (u32)priv->initgain_backup.cca);
1247
1248         RT_TRACE(COMP_DIG, "dm_BBInitialGainRestore 0xc50 is %x\n",
1249                  priv->initgain_backup.xaagccore1);
1250         RT_TRACE(COMP_DIG, "dm_BBInitialGainRestore 0xc58 is %x\n",
1251                  priv->initgain_backup.xbagccore1);
1252         RT_TRACE(COMP_DIG, "dm_BBInitialGainRestore 0xc60 is %x\n",
1253                  priv->initgain_backup.xcagccore1);
1254         RT_TRACE(COMP_DIG, "dm_BBInitialGainRestore 0xc68 is %x\n",
1255                  priv->initgain_backup.xdagccore1);
1256         RT_TRACE(COMP_DIG, "dm_BBInitialGainRestore 0xa0a is %x\n",
1257                  priv->initgain_backup.cca);
1258         rtl8192_setBBreg(dev, UFWP, bMaskByte1, 0x1);
1259
1260 }
1261
1262
1263 void dm_backup_dynamic_mechanism_state(struct net_device *dev)
1264 {
1265         struct r8192_priv *priv = rtllib_priv(dev);
1266
1267         priv->bswitch_fsync  = false;
1268         priv->bfsync_processing = false;
1269         dm_bb_initialgain_backup(dev);
1270
1271 }
1272
1273
1274 static void dm_bb_initialgain_backup(struct net_device *dev)
1275 {
1276         struct r8192_priv *priv = rtllib_priv(dev);
1277         u32 bit_mask = bMaskByte0;
1278
1279         if (dm_digtable.dig_algorithm == DIG_ALGO_BY_RSSI)
1280                 return;
1281
1282         rtl8192_setBBreg(dev, UFWP, bMaskByte1, 0x8);
1283         priv->initgain_backup.xaagccore1 = (u8)rtl8192_QueryBBReg(dev, rOFDM0_XAAGCCore1, bit_mask);
1284         priv->initgain_backup.xbagccore1 = (u8)rtl8192_QueryBBReg(dev, rOFDM0_XBAGCCore1, bit_mask);
1285         priv->initgain_backup.xcagccore1 = (u8)rtl8192_QueryBBReg(dev, rOFDM0_XCAGCCore1, bit_mask);
1286         priv->initgain_backup.xdagccore1 = (u8)rtl8192_QueryBBReg(dev, rOFDM0_XDAGCCore1, bit_mask);
1287         bit_mask  = bMaskByte2;
1288         priv->initgain_backup.cca = (u8)rtl8192_QueryBBReg(dev, rCCK0_CCA, bit_mask);
1289
1290         RT_TRACE(COMP_DIG, "BBInitialGainBackup 0xc50 is %x\n",
1291                  priv->initgain_backup.xaagccore1);
1292         RT_TRACE(COMP_DIG, "BBInitialGainBackup 0xc58 is %x\n",
1293                  priv->initgain_backup.xbagccore1);
1294         RT_TRACE(COMP_DIG, "BBInitialGainBackup 0xc60 is %x\n",
1295                  priv->initgain_backup.xcagccore1);
1296         RT_TRACE(COMP_DIG, "BBInitialGainBackup 0xc68 is %x\n",
1297                  priv->initgain_backup.xdagccore1);
1298         RT_TRACE(COMP_DIG, "BBInitialGainBackup 0xa0a is %x\n",
1299                  priv->initgain_backup.cca);
1300
1301 }
1302
1303 static void dm_dig_init(struct net_device *dev)
1304 {
1305         struct r8192_priv *priv = rtllib_priv(dev);
1306
1307         dm_digtable.dig_enable_flag     = true;
1308
1309         dm_digtable.dig_algorithm = DIG_ALGO_BY_RSSI;
1310
1311         dm_digtable.dig_algorithm_switch = 0;
1312
1313         dm_digtable.dig_state           = DM_STA_DIG_MAX;
1314         dm_digtable.dig_highpwr_state   = DM_STA_DIG_MAX;
1315         dm_digtable.CurSTAConnectState = DIG_STA_DISCONNECT;
1316         dm_digtable.PreSTAConnectState = DIG_STA_DISCONNECT;
1317
1318         dm_digtable.rssi_low_thresh     = DM_DIG_THRESH_LOW;
1319         dm_digtable.rssi_high_thresh    = DM_DIG_THRESH_HIGH;
1320
1321         dm_digtable.rssi_high_power_lowthresh = DM_DIG_HIGH_PWR_THRESH_LOW;
1322         dm_digtable.rssi_high_power_highthresh = DM_DIG_HIGH_PWR_THRESH_HIGH;
1323
1324         dm_digtable.rssi_val = 50;
1325         dm_digtable.backoff_val = DM_DIG_BACKOFF;
1326         dm_digtable.rx_gain_range_max = DM_DIG_MAX;
1327         if (priv->CustomerID == RT_CID_819x_Netcore)
1328                 dm_digtable.rx_gain_range_min = DM_DIG_MIN_Netcore;
1329         else
1330                 dm_digtable.rx_gain_range_min = DM_DIG_MIN;
1331 }
1332
1333 static void dm_ctrl_initgain_byrssi(struct net_device *dev)
1334 {
1335
1336         if (dm_digtable.dig_enable_flag == false)
1337                 return;
1338
1339         if (dm_digtable.dig_algorithm == DIG_ALGO_BY_FALSE_ALARM)
1340                 dm_ctrl_initgain_byrssi_by_fwfalse_alarm(dev);
1341         else if (dm_digtable.dig_algorithm == DIG_ALGO_BY_RSSI)
1342                 dm_ctrl_initgain_byrssi_by_driverrssi(dev);
1343         else
1344                 return;
1345 }
1346
1347 /*-----------------------------------------------------------------------------
1348  * Function:    dm_CtrlInitGainBeforeConnectByRssiAndFalseAlarm()
1349  *
1350  * Overview:    Driver monitor RSSI and False Alarm to change initial gain.
1351                         Only change initial gain during link in progress.
1352  *
1353  * Input:               IN      PADAPTER        pAdapter
1354  *
1355  * Output:              NONE
1356  *
1357  * Return:              NONE
1358  *
1359  * Revised History:
1360  *      When            Who             Remark
1361  *      03/04/2009      hpfan   Create Version 0.
1362  *
1363  *---------------------------------------------------------------------------*/
1364
1365 static void dm_ctrl_initgain_byrssi_by_driverrssi(
1366         struct net_device *dev)
1367 {
1368         struct r8192_priv *priv = rtllib_priv(dev);
1369         u8 i;
1370         static u8       fw_dig;
1371
1372         if (dm_digtable.dig_enable_flag == false)
1373                 return;
1374
1375         if (dm_digtable.dig_algorithm_switch)
1376                 fw_dig = 0;
1377         if (fw_dig <= 3) {
1378                 for (i = 0; i < 3; i++)
1379                         rtl8192_setBBreg(dev, UFWP, bMaskByte1, 0x8);
1380                 fw_dig++;
1381                 dm_digtable.dig_state = DM_STA_DIG_OFF;
1382         }
1383
1384         if (priv->rtllib->state == RTLLIB_LINKED)
1385                 dm_digtable.CurSTAConnectState = DIG_STA_CONNECT;
1386         else
1387                 dm_digtable.CurSTAConnectState = DIG_STA_DISCONNECT;
1388
1389
1390         dm_digtable.rssi_val = priv->undecorated_smoothed_pwdb;
1391         dm_initial_gain(dev);
1392         dm_pd_th(dev);
1393         dm_cs_ratio(dev);
1394         if (dm_digtable.dig_algorithm_switch)
1395                 dm_digtable.dig_algorithm_switch = 0;
1396         dm_digtable.PreSTAConnectState = dm_digtable.CurSTAConnectState;
1397
1398 }
1399
1400 static void dm_ctrl_initgain_byrssi_by_fwfalse_alarm(
1401         struct net_device *dev)
1402 {
1403         struct r8192_priv *priv = rtllib_priv(dev);
1404         static u32 reset_cnt;
1405         u8 i;
1406
1407         if (dm_digtable.dig_enable_flag == false)
1408                 return;
1409
1410         if (dm_digtable.dig_algorithm_switch) {
1411                 dm_digtable.dig_state = DM_STA_DIG_MAX;
1412                 for (i = 0; i < 3; i++)
1413                         rtl8192_setBBreg(dev, UFWP, bMaskByte1, 0x1);
1414                 dm_digtable.dig_algorithm_switch = 0;
1415         }
1416
1417         if (priv->rtllib->state != RTLLIB_LINKED)
1418                 return;
1419
1420         if ((priv->undecorated_smoothed_pwdb > dm_digtable.rssi_low_thresh) &&
1421                 (priv->undecorated_smoothed_pwdb < dm_digtable.rssi_high_thresh))
1422                 return;
1423         if (priv->undecorated_smoothed_pwdb <= dm_digtable.rssi_low_thresh) {
1424                 if (dm_digtable.dig_state == DM_STA_DIG_OFF &&
1425                         (priv->reset_count == reset_cnt))
1426                         return;
1427                 reset_cnt = priv->reset_count;
1428
1429                 dm_digtable.dig_highpwr_state = DM_STA_DIG_MAX;
1430                 dm_digtable.dig_state = DM_STA_DIG_OFF;
1431
1432                 rtl8192_setBBreg(dev, UFWP, bMaskByte1, 0x8);
1433
1434                 write_nic_byte(dev, rOFDM0_XAAGCCore1, 0x17);
1435                 write_nic_byte(dev, rOFDM0_XBAGCCore1, 0x17);
1436                 write_nic_byte(dev, rOFDM0_XCAGCCore1, 0x17);
1437                 write_nic_byte(dev, rOFDM0_XDAGCCore1, 0x17);
1438
1439                 if (priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20)
1440                         write_nic_byte(dev, (rOFDM0_XATxAFE+3), 0x00);
1441                 else
1442                         write_nic_byte(dev, rOFDM0_RxDetector1, 0x42);
1443
1444                 write_nic_byte(dev, 0xa0a, 0x08);
1445
1446                 return;
1447         }
1448
1449         if (priv->undecorated_smoothed_pwdb >= dm_digtable.rssi_high_thresh) {
1450                 u8 reset_flag = 0;
1451
1452                 if (dm_digtable.dig_state == DM_STA_DIG_ON &&
1453                     (priv->reset_count == reset_cnt)) {
1454                         dm_ctrl_initgain_byrssi_highpwr(dev);
1455                         return;
1456                 }
1457                 if (priv->reset_count != reset_cnt)
1458                         reset_flag = 1;
1459
1460                 reset_cnt = priv->reset_count;
1461
1462                 dm_digtable.dig_state = DM_STA_DIG_ON;
1463
1464                 if (reset_flag == 1) {
1465                         write_nic_byte(dev, rOFDM0_XAAGCCore1, 0x2c);
1466                         write_nic_byte(dev, rOFDM0_XBAGCCore1, 0x2c);
1467                         write_nic_byte(dev, rOFDM0_XCAGCCore1, 0x2c);
1468                         write_nic_byte(dev, rOFDM0_XDAGCCore1, 0x2c);
1469                 } else {
1470                         write_nic_byte(dev, rOFDM0_XAAGCCore1, 0x20);
1471                         write_nic_byte(dev, rOFDM0_XBAGCCore1, 0x20);
1472                         write_nic_byte(dev, rOFDM0_XCAGCCore1, 0x20);
1473                         write_nic_byte(dev, rOFDM0_XDAGCCore1, 0x20);
1474                 }
1475
1476                 if (priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20)
1477                         write_nic_byte(dev, (rOFDM0_XATxAFE+3), 0x20);
1478                 else
1479                         write_nic_byte(dev, rOFDM0_RxDetector1, 0x44);
1480
1481                 write_nic_byte(dev, 0xa0a, 0xcd);
1482
1483                 rtl8192_setBBreg(dev, UFWP, bMaskByte1, 0x1);
1484         }
1485         dm_ctrl_initgain_byrssi_highpwr(dev);
1486 }
1487
1488
1489 static void dm_ctrl_initgain_byrssi_highpwr(struct net_device *dev)
1490 {
1491         struct r8192_priv *priv = rtllib_priv(dev);
1492         static u32 reset_cnt_highpwr;
1493
1494         if ((priv->undecorated_smoothed_pwdb >
1495              dm_digtable.rssi_high_power_lowthresh) &&
1496             (priv->undecorated_smoothed_pwdb <
1497              dm_digtable.rssi_high_power_highthresh))
1498                 return;
1499
1500         if (priv->undecorated_smoothed_pwdb >=
1501             dm_digtable.rssi_high_power_highthresh) {
1502                 if (dm_digtable.dig_highpwr_state == DM_STA_DIG_ON &&
1503                         (priv->reset_count == reset_cnt_highpwr))
1504                         return;
1505                 dm_digtable.dig_highpwr_state = DM_STA_DIG_ON;
1506
1507                 if (priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20)
1508                                 write_nic_byte(dev, (rOFDM0_XATxAFE+3), 0x10);
1509                 else
1510                         write_nic_byte(dev, rOFDM0_RxDetector1, 0x43);
1511         } else {
1512                 if (dm_digtable.dig_highpwr_state == DM_STA_DIG_OFF &&
1513                         (priv->reset_count == reset_cnt_highpwr))
1514                         return;
1515                 dm_digtable.dig_highpwr_state = DM_STA_DIG_OFF;
1516
1517                 if ((priv->undecorated_smoothed_pwdb <
1518                      dm_digtable.rssi_high_power_lowthresh) &&
1519                     (priv->undecorated_smoothed_pwdb >=
1520                     dm_digtable.rssi_high_thresh)) {
1521                         if (priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20)
1522                                 write_nic_byte(dev, (rOFDM0_XATxAFE+3), 0x20);
1523                         else
1524                                 write_nic_byte(dev, rOFDM0_RxDetector1, 0x44);
1525                 }
1526         }
1527         reset_cnt_highpwr = priv->reset_count;
1528 }
1529
1530 static void dm_initial_gain(struct net_device *dev)
1531 {
1532         struct r8192_priv *priv = rtllib_priv(dev);
1533         u8 initial_gain = 0;
1534         static u8 initialized, force_write;
1535         static u32 reset_cnt;
1536
1537         if (dm_digtable.dig_algorithm_switch) {
1538                 initialized = 0;
1539                 reset_cnt = 0;
1540         }
1541
1542         if (rtllib_act_scanning(priv->rtllib, true) == true) {
1543                 force_write = 1;
1544                 return;
1545         }
1546
1547         if (dm_digtable.PreSTAConnectState == dm_digtable.CurSTAConnectState) {
1548                 if (dm_digtable.CurSTAConnectState == DIG_STA_CONNECT) {
1549                         long gain_range = dm_digtable.rssi_val + 10 -
1550                                           dm_digtable.backoff_val;
1551                         gain_range = clamp_t(long, gain_range,
1552                                              dm_digtable.rx_gain_range_min,
1553                                              dm_digtable.rx_gain_range_max);
1554                         dm_digtable.cur_ig_value = gain_range;
1555                 } else {
1556                         if (dm_digtable.cur_ig_value == 0)
1557                                 dm_digtable.cur_ig_value = priv->DefaultInitialGain[0];
1558                         else
1559                                 dm_digtable.cur_ig_value = dm_digtable.pre_ig_value;
1560                 }
1561         } else {
1562                 dm_digtable.cur_ig_value = priv->DefaultInitialGain[0];
1563                 dm_digtable.pre_ig_value = 0;
1564         }
1565
1566         if (priv->reset_count != reset_cnt) {
1567                 force_write = 1;
1568                 reset_cnt = priv->reset_count;
1569         }
1570
1571         if (dm_digtable.pre_ig_value != read_nic_byte(dev, rOFDM0_XAAGCCore1))
1572                 force_write = 1;
1573
1574         if ((dm_digtable.pre_ig_value != dm_digtable.cur_ig_value)
1575             || !initialized || force_write) {
1576                 initial_gain = (u8)dm_digtable.cur_ig_value;
1577                 write_nic_byte(dev, rOFDM0_XAAGCCore1, initial_gain);
1578                 write_nic_byte(dev, rOFDM0_XBAGCCore1, initial_gain);
1579                 write_nic_byte(dev, rOFDM0_XCAGCCore1, initial_gain);
1580                 write_nic_byte(dev, rOFDM0_XDAGCCore1, initial_gain);
1581                 dm_digtable.pre_ig_value = dm_digtable.cur_ig_value;
1582                 initialized = 1;
1583                 force_write = 0;
1584         }
1585 }
1586
1587 static void dm_pd_th(struct net_device *dev)
1588 {
1589         struct r8192_priv *priv = rtllib_priv(dev);
1590         static u8 initialized, force_write;
1591         static u32 reset_cnt;
1592
1593         if (dm_digtable.dig_algorithm_switch) {
1594                 initialized = 0;
1595                 reset_cnt = 0;
1596         }
1597
1598         if (dm_digtable.PreSTAConnectState == dm_digtable.CurSTAConnectState) {
1599                 if (dm_digtable.CurSTAConnectState == DIG_STA_CONNECT) {
1600                         if (dm_digtable.rssi_val >=
1601                             dm_digtable.rssi_high_power_highthresh)
1602                                 dm_digtable.curpd_thstate =
1603                                                         DIG_PD_AT_HIGH_POWER;
1604                         else if (dm_digtable.rssi_val <=
1605                                  dm_digtable.rssi_low_thresh)
1606                                 dm_digtable.curpd_thstate =
1607                                                         DIG_PD_AT_LOW_POWER;
1608                         else if ((dm_digtable.rssi_val >=
1609                                   dm_digtable.rssi_high_thresh) &&
1610                                  (dm_digtable.rssi_val <
1611                                   dm_digtable.rssi_high_power_lowthresh))
1612                                 dm_digtable.curpd_thstate =
1613                                                         DIG_PD_AT_NORMAL_POWER;
1614                         else
1615                                 dm_digtable.curpd_thstate =
1616                                                 dm_digtable.prepd_thstate;
1617                 } else {
1618                         dm_digtable.curpd_thstate = DIG_PD_AT_LOW_POWER;
1619                 }
1620         } else {
1621                 dm_digtable.curpd_thstate = DIG_PD_AT_LOW_POWER;
1622         }
1623
1624         if (priv->reset_count != reset_cnt) {
1625                 force_write = 1;
1626                 reset_cnt = priv->reset_count;
1627         }
1628
1629         if ((dm_digtable.prepd_thstate != dm_digtable.curpd_thstate) ||
1630             (initialized <= 3) || force_write) {
1631                 if (dm_digtable.curpd_thstate == DIG_PD_AT_LOW_POWER) {
1632                         if (priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20)
1633                                 write_nic_byte(dev, (rOFDM0_XATxAFE+3), 0x00);
1634                         else
1635                                 write_nic_byte(dev, rOFDM0_RxDetector1, 0x42);
1636                 } else if (dm_digtable.curpd_thstate ==
1637                            DIG_PD_AT_NORMAL_POWER) {
1638                         if (priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20)
1639                                 write_nic_byte(dev, (rOFDM0_XATxAFE+3), 0x20);
1640                         else
1641                                 write_nic_byte(dev, rOFDM0_RxDetector1, 0x44);
1642                 } else if (dm_digtable.curpd_thstate == DIG_PD_AT_HIGH_POWER) {
1643                         if (priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20)
1644                                 write_nic_byte(dev, (rOFDM0_XATxAFE+3), 0x10);
1645                         else
1646                                 write_nic_byte(dev, rOFDM0_RxDetector1, 0x43);
1647                 }
1648                 dm_digtable.prepd_thstate = dm_digtable.curpd_thstate;
1649                 if (initialized <= 3)
1650                         initialized++;
1651                 force_write = 0;
1652         }
1653 }
1654
1655 static  void dm_cs_ratio(struct net_device *dev)
1656 {
1657         struct r8192_priv *priv = rtllib_priv(dev);
1658         static u8 initialized, force_write;
1659         static u32 reset_cnt;
1660
1661         if (dm_digtable.dig_algorithm_switch) {
1662                 initialized = 0;
1663                 reset_cnt = 0;
1664         }
1665
1666         if (dm_digtable.PreSTAConnectState == dm_digtable.CurSTAConnectState) {
1667                 if (dm_digtable.CurSTAConnectState == DIG_STA_CONNECT) {
1668                         if (dm_digtable.rssi_val <= dm_digtable.rssi_low_thresh)
1669                                 dm_digtable.curcs_ratio_state = DIG_CS_RATIO_LOWER;
1670                         else if (dm_digtable.rssi_val >= dm_digtable.rssi_high_thresh)
1671                                 dm_digtable.curcs_ratio_state = DIG_CS_RATIO_HIGHER;
1672                         else
1673                                 dm_digtable.curcs_ratio_state = dm_digtable.precs_ratio_state;
1674                 } else {
1675                         dm_digtable.curcs_ratio_state = DIG_CS_RATIO_LOWER;
1676                 }
1677         } else {
1678                 dm_digtable.curcs_ratio_state = DIG_CS_RATIO_LOWER;
1679         }
1680
1681         if (priv->reset_count != reset_cnt) {
1682                 force_write = 1;
1683                 reset_cnt = priv->reset_count;
1684         }
1685
1686
1687         if ((dm_digtable.precs_ratio_state != dm_digtable.curcs_ratio_state) ||
1688             !initialized || force_write) {
1689                 if (dm_digtable.curcs_ratio_state == DIG_CS_RATIO_LOWER)
1690                         write_nic_byte(dev, 0xa0a, 0x08);
1691                 else if (dm_digtable.curcs_ratio_state == DIG_CS_RATIO_HIGHER)
1692                         write_nic_byte(dev, 0xa0a, 0xcd);
1693                 dm_digtable.precs_ratio_state = dm_digtable.curcs_ratio_state;
1694                 initialized = 1;
1695                 force_write = 0;
1696         }
1697 }
1698
1699 void dm_init_edca_turbo(struct net_device *dev)
1700 {
1701         struct r8192_priv *priv = rtllib_priv(dev);
1702
1703         priv->bcurrent_turbo_EDCA = false;
1704         priv->rtllib->bis_any_nonbepkts = false;
1705         priv->bis_cur_rdlstate = false;
1706 }
1707
1708 static void dm_check_edca_turbo(struct net_device *dev)
1709 {
1710         struct r8192_priv *priv = rtllib_priv(dev);
1711         struct rt_hi_throughput *pHTInfo = priv->rtllib->pHTInfo;
1712
1713         static unsigned long lastTxOkCnt;
1714         static unsigned long lastRxOkCnt;
1715         unsigned long curTxOkCnt = 0;
1716         unsigned long curRxOkCnt = 0;
1717
1718         if (priv->rtllib->iw_mode == IW_MODE_ADHOC)
1719                 goto dm_CheckEdcaTurbo_EXIT;
1720         if (priv->rtllib->state != RTLLIB_LINKED)
1721                 goto dm_CheckEdcaTurbo_EXIT;
1722         if (priv->rtllib->pHTInfo->IOTAction & HT_IOT_ACT_DISABLE_EDCA_TURBO)
1723                 goto dm_CheckEdcaTurbo_EXIT;
1724
1725         {
1726                 u8 *peername[11] = {
1727                         "unknown", "realtek_90", "realtek_92se", "broadcom",
1728                         "ralink", "atheros", "cisco", "marvell", "92u_softap",
1729                         "self_softap"
1730                 };
1731                 static int wb_tmp;
1732
1733                 if (wb_tmp == 0) {
1734                         netdev_info(dev,
1735                                     "%s():iot peer is %s, bssid: %pM\n",
1736                                     __func__, peername[pHTInfo->IOTPeer],
1737                                     priv->rtllib->current_network.bssid);
1738                         wb_tmp = 1;
1739                 }
1740         }
1741         if (!priv->rtllib->bis_any_nonbepkts) {
1742                 curTxOkCnt = priv->stats.txbytesunicast - lastTxOkCnt;
1743                 curRxOkCnt = priv->stats.rxbytesunicast - lastRxOkCnt;
1744                 if (pHTInfo->IOTAction & HT_IOT_ACT_EDCA_BIAS_ON_RX) {
1745                         if (curTxOkCnt > 4*curRxOkCnt) {
1746                                 if (priv->bis_cur_rdlstate ||
1747                                     !priv->bcurrent_turbo_EDCA) {
1748                                         write_nic_dword(dev, EDCAPARA_BE,
1749                                                  edca_setting_UL[pHTInfo->IOTPeer]);
1750                                         priv->bis_cur_rdlstate = false;
1751                                 }
1752                         } else {
1753                                 if (!priv->bis_cur_rdlstate ||
1754                                     !priv->bcurrent_turbo_EDCA) {
1755                                         if (priv->rtllib->mode == WIRELESS_MODE_G)
1756                                                 write_nic_dword(dev,
1757                                                                 EDCAPARA_BE,
1758                                                          edca_setting_DL_GMode[pHTInfo->IOTPeer]);
1759                                         else
1760                                                 write_nic_dword(dev,
1761                                                                 EDCAPARA_BE,
1762                                                          edca_setting_DL[pHTInfo->IOTPeer]);
1763                                         priv->bis_cur_rdlstate = true;
1764                                 }
1765                         }
1766                         priv->bcurrent_turbo_EDCA = true;
1767                 } else {
1768                         if (curRxOkCnt > 4*curTxOkCnt) {
1769                                 if (!priv->bis_cur_rdlstate ||
1770                                     !priv->bcurrent_turbo_EDCA) {
1771                                         if (priv->rtllib->mode == WIRELESS_MODE_G)
1772                                                 write_nic_dword(dev,
1773                                                                 EDCAPARA_BE,
1774                                                          edca_setting_DL_GMode[pHTInfo->IOTPeer]);
1775                                         else
1776                                                 write_nic_dword(dev,
1777                                                                 EDCAPARA_BE,
1778                                                          edca_setting_DL[pHTInfo->IOTPeer]);
1779                                         priv->bis_cur_rdlstate = true;
1780                                 }
1781                         } else {
1782                                 if (priv->bis_cur_rdlstate ||
1783                                     !priv->bcurrent_turbo_EDCA) {
1784                                         write_nic_dword(dev, EDCAPARA_BE,
1785                                                         edca_setting_UL[pHTInfo->IOTPeer]);
1786                                         priv->bis_cur_rdlstate = false;
1787                                 }
1788
1789                         }
1790
1791                         priv->bcurrent_turbo_EDCA = true;
1792                 }
1793         } else {
1794                  if (priv->bcurrent_turbo_EDCA) {
1795                         u8 tmp = AC0_BE;
1796
1797                         priv->rtllib->SetHwRegHandler(dev, HW_VAR_AC_PARAM,
1798                                                       (u8 *)(&tmp));
1799                         priv->bcurrent_turbo_EDCA = false;
1800                 }
1801         }
1802
1803
1804 dm_CheckEdcaTurbo_EXIT:
1805         priv->rtllib->bis_any_nonbepkts = false;
1806         lastTxOkCnt = priv->stats.txbytesunicast;
1807         lastRxOkCnt = priv->stats.rxbytesunicast;
1808 }
1809
1810 static void dm_init_ctstoself(struct net_device *dev)
1811 {
1812         struct r8192_priv *priv = rtllib_priv((struct net_device *)dev);
1813
1814         priv->rtllib->bCTSToSelfEnable = true;
1815         priv->rtllib->CTSToSelfTH = CTSToSelfTHVal;
1816 }
1817
1818 static void dm_ctstoself(struct net_device *dev)
1819 {
1820         struct r8192_priv *priv = rtllib_priv((struct net_device *)dev);
1821         struct rt_hi_throughput *pHTInfo = priv->rtllib->pHTInfo;
1822         static unsigned long lastTxOkCnt;
1823         static unsigned long lastRxOkCnt;
1824         unsigned long curTxOkCnt = 0;
1825         unsigned long curRxOkCnt = 0;
1826
1827         if (priv->rtllib->bCTSToSelfEnable != true) {
1828                 pHTInfo->IOTAction &= ~HT_IOT_ACT_FORCED_CTS2SELF;
1829                 return;
1830         }
1831         if (pHTInfo->IOTPeer == HT_IOT_PEER_BROADCOM) {
1832                 curTxOkCnt = priv->stats.txbytesunicast - lastTxOkCnt;
1833                 curRxOkCnt = priv->stats.rxbytesunicast - lastRxOkCnt;
1834                 if (curRxOkCnt > 4*curTxOkCnt)
1835                         pHTInfo->IOTAction &= ~HT_IOT_ACT_FORCED_CTS2SELF;
1836                 else
1837                         pHTInfo->IOTAction |= HT_IOT_ACT_FORCED_CTS2SELF;
1838
1839                 lastTxOkCnt = priv->stats.txbytesunicast;
1840                 lastRxOkCnt = priv->stats.rxbytesunicast;
1841         }
1842 }
1843
1844
1845 static  void dm_Init_WA_Broadcom_IOT(struct net_device *dev)
1846 {
1847         struct r8192_priv *priv = rtllib_priv((struct net_device *)dev);
1848         struct rt_hi_throughput *pHTInfo = priv->rtllib->pHTInfo;
1849
1850         pHTInfo->bWAIotBroadcom = false;
1851         pHTInfo->WAIotTH = WAIotTHVal;
1852 }
1853
1854 static  void    dm_check_pbc_gpio(struct net_device *dev)
1855 {
1856 }
1857
1858 void dm_CheckRfCtrlGPIO(void *data)
1859 {
1860         struct r8192_priv *priv = container_of_dwork_rsl(data,
1861                                   struct r8192_priv, gpio_change_rf_wq);
1862         struct net_device *dev = priv->rtllib->dev;
1863         u8 tmp1byte;
1864         enum rt_rf_power_state eRfPowerStateToSet;
1865         bool bActuallySet = false;
1866         char *argv[3];
1867         static char *RadioPowerPath = "/etc/acpi/events/RadioPower.sh";
1868         static char *envp[] = {"HOME=/", "TERM=linux", "PATH=/usr/bin:/bin",
1869                                NULL};
1870
1871         bActuallySet = false;
1872
1873         if ((priv->up_first_time == 1) || (priv->being_init_adapter))
1874                 return;
1875
1876         if (priv->bfirst_after_down) {
1877                 priv->bfirst_after_down = true;
1878                 return;
1879         }
1880
1881         tmp1byte = read_nic_byte(dev, GPI);
1882
1883         eRfPowerStateToSet = (tmp1byte&BIT1) ?  eRfOn : eRfOff;
1884
1885         if (priv->bHwRadioOff && (eRfPowerStateToSet == eRfOn)) {
1886                 RT_TRACE(COMP_RF, "gpiochangeRF  - HW Radio ON\n");
1887                 netdev_info(dev, "gpiochangeRF  - HW Radio ON\n");
1888                 priv->bHwRadioOff = false;
1889                 bActuallySet = true;
1890         } else if (!priv->bHwRadioOff && (eRfPowerStateToSet == eRfOff)) {
1891                 RT_TRACE(COMP_RF, "gpiochangeRF  - HW Radio OFF\n");
1892                 netdev_info(dev, "gpiochangeRF  - HW Radio OFF\n");
1893                 priv->bHwRadioOff = true;
1894                 bActuallySet = true;
1895         }
1896
1897         if (bActuallySet) {
1898                 mdelay(1000);
1899                 priv->bHwRfOffAction = 1;
1900                 MgntActSet_RF_State(dev, eRfPowerStateToSet, RF_CHANGE_BY_HW,
1901                                     true);
1902                 if (priv->bHwRadioOff)
1903                         argv[1] = "RFOFF";
1904                 else
1905                         argv[1] = "RFON";
1906
1907                 argv[0] = RadioPowerPath;
1908                 argv[2] = NULL;
1909                 call_usermodehelper(RadioPowerPath, argv, envp, UMH_WAIT_PROC);
1910         }
1911 }
1912
1913 void    dm_rf_pathcheck_workitemcallback(void *data)
1914 {
1915         struct r8192_priv *priv = container_of_dwork_rsl(data,
1916                                   struct r8192_priv,
1917                                   rfpath_check_wq);
1918         struct net_device *dev = priv->rtllib->dev;
1919         u8 rfpath = 0, i;
1920
1921         rfpath = read_nic_byte(dev, 0xc04);
1922
1923         for (i = 0; i < RF90_PATH_MAX; i++) {
1924                 if (rfpath & (0x01<<i))
1925                         priv->brfpath_rxenable[i] = true;
1926                 else
1927                         priv->brfpath_rxenable[i] = false;
1928         }
1929         if (!DM_RxPathSelTable.Enable)
1930                 return;
1931
1932         dm_rxpath_sel_byrssi(dev);
1933 }
1934
1935 static void dm_init_rxpath_selection(struct net_device *dev)
1936 {
1937         u8 i;
1938         struct r8192_priv *priv = rtllib_priv(dev);
1939
1940         DM_RxPathSelTable.Enable = 1;
1941         DM_RxPathSelTable.SS_TH_low = RxPathSelection_SS_TH_low;
1942         DM_RxPathSelTable.diff_TH = RxPathSelection_diff_TH;
1943         if (priv->CustomerID == RT_CID_819x_Netcore)
1944                 DM_RxPathSelTable.cck_method = CCK_Rx_Version_2;
1945         else
1946                 DM_RxPathSelTable.cck_method = CCK_Rx_Version_1;
1947         DM_RxPathSelTable.disabledRF = 0;
1948         for (i = 0; i < 4; i++) {
1949                 DM_RxPathSelTable.rf_rssi[i] = 50;
1950                 DM_RxPathSelTable.cck_pwdb_sta[i] = -64;
1951                 DM_RxPathSelTable.rf_enable_rssi_th[i] = 100;
1952         }
1953 }
1954
1955 #define PWDB_IN_RANGE   ((cur_cck_pwdb < tmp_cck_max_pwdb) &&   \
1956                         (cur_cck_pwdb > tmp_cck_sec_pwdb))
1957
1958 static void dm_rxpath_sel_byrssi(struct net_device *dev)
1959 {
1960         struct r8192_priv *priv = rtllib_priv(dev);
1961         u8 i, max_rssi_index = 0, min_rssi_index = 0;
1962         u8 sec_rssi_index = 0, rf_num = 0;
1963         u8 tmp_max_rssi = 0, tmp_min_rssi = 0, tmp_sec_rssi = 0;
1964         u8 cck_default_Rx = 0x2;
1965         u8 cck_optional_Rx = 0x3;
1966         long tmp_cck_max_pwdb = 0, tmp_cck_min_pwdb = 0, tmp_cck_sec_pwdb = 0;
1967         u8 cck_rx_ver2_max_index = 0, cck_rx_ver2_min_index = 0;
1968         u8 cck_rx_ver2_sec_index = 0;
1969         u8 cur_rf_rssi;
1970         long cur_cck_pwdb;
1971         static u8 disabled_rf_cnt, cck_Rx_Path_initialized;
1972         u8 update_cck_rx_path;
1973
1974         if (priv->rf_type != RF_2T4R)
1975                 return;
1976
1977         if (!cck_Rx_Path_initialized) {
1978                 DM_RxPathSelTable.cck_Rx_path = (read_nic_byte(dev, 0xa07)&0xf);
1979                 cck_Rx_Path_initialized = 1;
1980         }
1981
1982         DM_RxPathSelTable.disabledRF = 0xf;
1983         DM_RxPathSelTable.disabledRF &= ~(read_nic_byte(dev, 0xc04));
1984
1985         if (priv->rtllib->mode == WIRELESS_MODE_B)
1986                 DM_RxPathSelTable.cck_method = CCK_Rx_Version_2;
1987
1988         for (i = 0; i < RF90_PATH_MAX; i++) {
1989                 DM_RxPathSelTable.rf_rssi[i] = priv->stats.rx_rssi_percentage[i];
1990
1991                 if (priv->brfpath_rxenable[i]) {
1992                         rf_num++;
1993                         cur_rf_rssi = DM_RxPathSelTable.rf_rssi[i];
1994
1995                         if (rf_num == 1) {
1996                                 max_rssi_index = min_rssi_index = sec_rssi_index = i;
1997                                 tmp_max_rssi = tmp_min_rssi = tmp_sec_rssi = cur_rf_rssi;
1998                         } else if (rf_num == 2) {
1999                                 if (cur_rf_rssi >= tmp_max_rssi) {
2000                                         tmp_max_rssi = cur_rf_rssi;
2001                                         max_rssi_index = i;
2002                                 } else {
2003                                         tmp_sec_rssi = tmp_min_rssi = cur_rf_rssi;
2004                                         sec_rssi_index = min_rssi_index = i;
2005                                 }
2006                         } else {
2007                                 if (cur_rf_rssi > tmp_max_rssi) {
2008                                         tmp_sec_rssi = tmp_max_rssi;
2009                                         sec_rssi_index = max_rssi_index;
2010                                         tmp_max_rssi = cur_rf_rssi;
2011                                         max_rssi_index = i;
2012                                 } else if (cur_rf_rssi == tmp_max_rssi) {
2013                                         tmp_sec_rssi = cur_rf_rssi;
2014                                         sec_rssi_index = i;
2015                                 } else if ((cur_rf_rssi < tmp_max_rssi) &&
2016                                            (cur_rf_rssi > tmp_sec_rssi)) {
2017                                         tmp_sec_rssi = cur_rf_rssi;
2018                                         sec_rssi_index = i;
2019                                 } else if (cur_rf_rssi == tmp_sec_rssi) {
2020                                         if (tmp_sec_rssi == tmp_min_rssi) {
2021                                                 tmp_sec_rssi = cur_rf_rssi;
2022                                                 sec_rssi_index = i;
2023                                         }
2024                                 } else if ((cur_rf_rssi < tmp_sec_rssi) &&
2025                                            (cur_rf_rssi > tmp_min_rssi)) {
2026                                         ;
2027                                 } else if (cur_rf_rssi == tmp_min_rssi) {
2028                                         if (tmp_sec_rssi == tmp_min_rssi) {
2029                                                 tmp_min_rssi = cur_rf_rssi;
2030                                                 min_rssi_index = i;
2031                                         }
2032                                 } else if (cur_rf_rssi < tmp_min_rssi) {
2033                                         tmp_min_rssi = cur_rf_rssi;
2034                                         min_rssi_index = i;
2035                                 }
2036                         }
2037                 }
2038         }
2039
2040         rf_num = 0;
2041         if (DM_RxPathSelTable.cck_method == CCK_Rx_Version_2) {
2042                 for (i = 0; i < RF90_PATH_MAX; i++) {
2043                         if (priv->brfpath_rxenable[i]) {
2044                                 rf_num++;
2045                                 cur_cck_pwdb =
2046                                          DM_RxPathSelTable.cck_pwdb_sta[i];
2047
2048                                 if (rf_num == 1) {
2049                                         cck_rx_ver2_max_index = i;
2050                                         cck_rx_ver2_min_index = i;
2051                                         cck_rx_ver2_sec_index = i;
2052                                         tmp_cck_max_pwdb = cur_cck_pwdb;
2053                                         tmp_cck_min_pwdb = cur_cck_pwdb;
2054                                         tmp_cck_sec_pwdb = cur_cck_pwdb;
2055                                 } else if (rf_num == 2) {
2056                                         if (cur_cck_pwdb >= tmp_cck_max_pwdb) {
2057                                                 tmp_cck_max_pwdb = cur_cck_pwdb;
2058                                                 cck_rx_ver2_max_index = i;
2059                                         } else {
2060                                                 tmp_cck_sec_pwdb = cur_cck_pwdb;
2061                                                 tmp_cck_min_pwdb = cur_cck_pwdb;
2062                                                 cck_rx_ver2_sec_index = i;
2063                                                 cck_rx_ver2_min_index = i;
2064                                         }
2065                                 } else {
2066                                         if (cur_cck_pwdb > tmp_cck_max_pwdb) {
2067                                                 tmp_cck_sec_pwdb =
2068                                                          tmp_cck_max_pwdb;
2069                                                 cck_rx_ver2_sec_index =
2070                                                          cck_rx_ver2_max_index;
2071                                                 tmp_cck_max_pwdb = cur_cck_pwdb;
2072                                                 cck_rx_ver2_max_index = i;
2073                                         } else if (cur_cck_pwdb ==
2074                                                    tmp_cck_max_pwdb) {
2075                                                 tmp_cck_sec_pwdb = cur_cck_pwdb;
2076                                                 cck_rx_ver2_sec_index = i;
2077                                         } else if (PWDB_IN_RANGE) {
2078                                                 tmp_cck_sec_pwdb = cur_cck_pwdb;
2079                                                 cck_rx_ver2_sec_index = i;
2080                                         } else if (cur_cck_pwdb ==
2081                                                    tmp_cck_sec_pwdb) {
2082                                                 if (tmp_cck_sec_pwdb ==
2083                                                     tmp_cck_min_pwdb) {
2084                                                         tmp_cck_sec_pwdb =
2085                                                                  cur_cck_pwdb;
2086                                                         cck_rx_ver2_sec_index =
2087                                                                  i;
2088                                                 }
2089                                         } else if ((cur_cck_pwdb < tmp_cck_sec_pwdb) &&
2090                                                    (cur_cck_pwdb > tmp_cck_min_pwdb)) {
2091                                                 ;
2092                                         } else if (cur_cck_pwdb == tmp_cck_min_pwdb) {
2093                                                 if (tmp_cck_sec_pwdb == tmp_cck_min_pwdb) {
2094                                                         tmp_cck_min_pwdb = cur_cck_pwdb;
2095                                                         cck_rx_ver2_min_index = i;
2096                                                 }
2097                                         } else if (cur_cck_pwdb < tmp_cck_min_pwdb) {
2098                                                 tmp_cck_min_pwdb = cur_cck_pwdb;
2099                                                 cck_rx_ver2_min_index = i;
2100                                         }
2101                                 }
2102
2103                         }
2104                 }
2105         }
2106
2107         update_cck_rx_path = 0;
2108         if (DM_RxPathSelTable.cck_method == CCK_Rx_Version_2) {
2109                 cck_default_Rx = cck_rx_ver2_max_index;
2110                 cck_optional_Rx = cck_rx_ver2_sec_index;
2111                 if (tmp_cck_max_pwdb != -64)
2112                         update_cck_rx_path = 1;
2113         }
2114
2115         if (tmp_min_rssi < DM_RxPathSelTable.SS_TH_low && disabled_rf_cnt < 2) {
2116                 if ((tmp_max_rssi - tmp_min_rssi) >=
2117                      DM_RxPathSelTable.diff_TH) {
2118                         DM_RxPathSelTable.rf_enable_rssi_th[min_rssi_index] =
2119                                  tmp_max_rssi+5;
2120                         rtl8192_setBBreg(dev, rOFDM0_TRxPathEnable,
2121                                  0x1<<min_rssi_index, 0x0);
2122                         rtl8192_setBBreg(dev, rOFDM1_TRxPathEnable,
2123                                  0x1<<min_rssi_index, 0x0);
2124                         disabled_rf_cnt++;
2125                 }
2126                 if (DM_RxPathSelTable.cck_method == CCK_Rx_Version_1) {
2127                         cck_default_Rx = max_rssi_index;
2128                         cck_optional_Rx = sec_rssi_index;
2129                         if (tmp_max_rssi)
2130                                 update_cck_rx_path = 1;
2131                 }
2132         }
2133
2134         if (update_cck_rx_path) {
2135                 DM_RxPathSelTable.cck_Rx_path = (cck_default_Rx<<2) |
2136                                                 (cck_optional_Rx);
2137                 rtl8192_setBBreg(dev, rCCK0_AFESetting, 0x0f000000,
2138                                  DM_RxPathSelTable.cck_Rx_path);
2139         }
2140
2141         if (DM_RxPathSelTable.disabledRF) {
2142                 for (i = 0; i < 4; i++) {
2143                         if ((DM_RxPathSelTable.disabledRF>>i) & 0x1) {
2144                                 if (tmp_max_rssi >=
2145                                     DM_RxPathSelTable.rf_enable_rssi_th[i]) {
2146                                         rtl8192_setBBreg(dev,
2147                                                  rOFDM0_TRxPathEnable, 0x1 << i,
2148                                                  0x1);
2149                                         rtl8192_setBBreg(dev,
2150                                                  rOFDM1_TRxPathEnable,
2151                                                  0x1 << i, 0x1);
2152                                         DM_RxPathSelTable.rf_enable_rssi_th[i]
2153                                                  = 100;
2154                                         disabled_rf_cnt--;
2155                                 }
2156                         }
2157                 }
2158         }
2159 }
2160
2161 static  void    dm_check_rx_path_selection(struct net_device *dev)
2162 {
2163         struct r8192_priv *priv = rtllib_priv(dev);
2164
2165         queue_delayed_work_rsl(priv->priv_wq, &priv->rfpath_check_wq, 0);
2166 }
2167
2168
2169 static void dm_init_fsync(struct net_device *dev)
2170 {
2171         struct r8192_priv *priv = rtllib_priv(dev);
2172
2173         priv->rtllib->fsync_time_interval = 500;
2174         priv->rtllib->fsync_rate_bitmap = 0x0f000800;
2175         priv->rtllib->fsync_rssi_threshold = 30;
2176         priv->rtllib->bfsync_enable = false;
2177         priv->rtllib->fsync_multiple_timeinterval = 3;
2178         priv->rtllib->fsync_firstdiff_ratethreshold = 100;
2179         priv->rtllib->fsync_seconddiff_ratethreshold = 200;
2180         priv->rtllib->fsync_state = Default_Fsync;
2181         priv->framesyncMonitor = 1;
2182
2183         init_timer(&priv->fsync_timer);
2184         setup_timer(&priv->fsync_timer, dm_fsync_timer_callback,
2185                    (unsigned long) dev);
2186 }
2187
2188
2189 static void dm_deInit_fsync(struct net_device *dev)
2190 {
2191         struct r8192_priv *priv = rtllib_priv(dev);
2192
2193         del_timer_sync(&priv->fsync_timer);
2194 }
2195
2196 void dm_fsync_timer_callback(unsigned long data)
2197 {
2198         struct net_device *dev = (struct net_device *)data;
2199         struct r8192_priv *priv = rtllib_priv((struct net_device *)data);
2200         u32 rate_index, rate_count = 0, rate_count_diff = 0;
2201         bool            bSwitchFromCountDiff = false;
2202         bool            bDoubleTimeInterval = false;
2203
2204         if (priv->rtllib->state == RTLLIB_LINKED &&
2205             priv->rtllib->bfsync_enable &&
2206             (priv->rtllib->pHTInfo->IOTAction & HT_IOT_ACT_CDD_FSYNC)) {
2207                 u32 rate_bitmap;
2208
2209                 for (rate_index = 0; rate_index <= 27; rate_index++) {
2210                         rate_bitmap  = 1 << rate_index;
2211                         if (priv->rtllib->fsync_rate_bitmap &  rate_bitmap)
2212                                 rate_count +=
2213                                    priv->stats.received_rate_histogram[1]
2214                                    [rate_index];
2215                 }
2216
2217                 if (rate_count < priv->rate_record)
2218                         rate_count_diff = 0xffffffff - rate_count +
2219                                           priv->rate_record;
2220                 else
2221                         rate_count_diff = rate_count - priv->rate_record;
2222                 if (rate_count_diff < priv->rateCountDiffRecord) {
2223
2224                         u32 DiffNum = priv->rateCountDiffRecord -
2225                                       rate_count_diff;
2226                         if (DiffNum >=
2227                             priv->rtllib->fsync_seconddiff_ratethreshold)
2228                                 priv->ContinueDiffCount++;
2229                         else
2230                                 priv->ContinueDiffCount = 0;
2231
2232                         if (priv->ContinueDiffCount >= 2) {
2233                                 bSwitchFromCountDiff = true;
2234                                 priv->ContinueDiffCount = 0;
2235                         }
2236                 } else {
2237                         priv->ContinueDiffCount = 0;
2238                 }
2239
2240                 if (rate_count_diff <=
2241                     priv->rtllib->fsync_firstdiff_ratethreshold) {
2242                         bSwitchFromCountDiff = true;
2243                         priv->ContinueDiffCount = 0;
2244                 }
2245                 priv->rate_record = rate_count;
2246                 priv->rateCountDiffRecord = rate_count_diff;
2247                 RT_TRACE(COMP_HALDM,
2248                          "rateRecord %d rateCount %d, rateCountdiff %d bSwitchFsync %d\n",
2249                          priv->rate_record, rate_count, rate_count_diff,
2250                          priv->bswitch_fsync);
2251                 if (priv->undecorated_smoothed_pwdb >
2252                     priv->rtllib->fsync_rssi_threshold &&
2253                     bSwitchFromCountDiff) {
2254                         bDoubleTimeInterval = true;
2255                         priv->bswitch_fsync = !priv->bswitch_fsync;
2256                         if (priv->bswitch_fsync) {
2257                                 write_nic_byte(dev, 0xC36, 0x1c);
2258                                 write_nic_byte(dev, 0xC3e, 0x90);
2259                         } else {
2260                                 write_nic_byte(dev, 0xC36, 0x5c);
2261                                 write_nic_byte(dev, 0xC3e, 0x96);
2262                         }
2263                 } else if (priv->undecorated_smoothed_pwdb <=
2264                            priv->rtllib->fsync_rssi_threshold) {
2265                         if (priv->bswitch_fsync) {
2266                                 priv->bswitch_fsync  = false;
2267                                 write_nic_byte(dev, 0xC36, 0x5c);
2268                                 write_nic_byte(dev, 0xC3e, 0x96);
2269                         }
2270                 }
2271                 if (bDoubleTimeInterval) {
2272                         if (timer_pending(&priv->fsync_timer))
2273                                 del_timer_sync(&priv->fsync_timer);
2274                         priv->fsync_timer.expires = jiffies +
2275                                  msecs_to_jiffies(priv->rtllib->fsync_time_interval *
2276                                  priv->rtllib->fsync_multiple_timeinterval);
2277                         add_timer(&priv->fsync_timer);
2278                 } else {
2279                         if (timer_pending(&priv->fsync_timer))
2280                                 del_timer_sync(&priv->fsync_timer);
2281                         priv->fsync_timer.expires = jiffies +
2282                                  msecs_to_jiffies(priv->rtllib->fsync_time_interval);
2283                         add_timer(&priv->fsync_timer);
2284                 }
2285         } else {
2286                 if (priv->bswitch_fsync) {
2287                         priv->bswitch_fsync  = false;
2288                         write_nic_byte(dev, 0xC36, 0x5c);
2289                         write_nic_byte(dev, 0xC3e, 0x96);
2290                 }
2291                 priv->ContinueDiffCount = 0;
2292                 write_nic_dword(dev, rOFDM0_RxDetector2, 0x465c52cd);
2293         }
2294         RT_TRACE(COMP_HALDM, "ContinueDiffCount %d\n", priv->ContinueDiffCount);
2295         RT_TRACE(COMP_HALDM,
2296                  "rateRecord %d rateCount %d, rateCountdiff %d bSwitchFsync %d\n",
2297                  priv->rate_record, rate_count, rate_count_diff,
2298                  priv->bswitch_fsync);
2299 }
2300
2301 static void dm_StartHWFsync(struct net_device *dev)
2302 {
2303         u8 rf_timing = 0x77;
2304         struct r8192_priv *priv = rtllib_priv(dev);
2305
2306         RT_TRACE(COMP_HALDM, "%s\n", __func__);
2307         write_nic_dword(dev, rOFDM0_RxDetector2, 0x465c12cf);
2308         priv->rtllib->SetHwRegHandler(dev, HW_VAR_RF_TIMING,
2309                                       (u8 *)(&rf_timing));
2310         write_nic_byte(dev, 0xc3b, 0x41);
2311 }
2312
2313 static void dm_EndHWFsync(struct net_device *dev)
2314 {
2315         u8 rf_timing = 0xaa;
2316         struct r8192_priv *priv = rtllib_priv(dev);
2317
2318         RT_TRACE(COMP_HALDM, "%s\n", __func__);
2319         write_nic_dword(dev, rOFDM0_RxDetector2, 0x465c52cd);
2320         priv->rtllib->SetHwRegHandler(dev, HW_VAR_RF_TIMING, (u8 *)
2321                                      (&rf_timing));
2322         write_nic_byte(dev, 0xc3b, 0x49);
2323 }
2324
2325 static void dm_EndSWFsync(struct net_device *dev)
2326 {
2327         struct r8192_priv *priv = rtllib_priv(dev);
2328
2329         RT_TRACE(COMP_HALDM, "%s\n", __func__);
2330         del_timer_sync(&(priv->fsync_timer));
2331
2332         if (priv->bswitch_fsync) {
2333                 priv->bswitch_fsync  = false;
2334
2335                 write_nic_byte(dev, 0xC36, 0x5c);
2336
2337                 write_nic_byte(dev, 0xC3e, 0x96);
2338         }
2339
2340         priv->ContinueDiffCount = 0;
2341         write_nic_dword(dev, rOFDM0_RxDetector2, 0x465c52cd);
2342 }
2343
2344 static void dm_StartSWFsync(struct net_device *dev)
2345 {
2346         struct r8192_priv *priv = rtllib_priv(dev);
2347         u32                     rateIndex;
2348         u32                     rateBitmap;
2349
2350         RT_TRACE(COMP_HALDM, "%s\n", __func__);
2351         priv->rate_record = 0;
2352         priv->ContinueDiffCount = 0;
2353         priv->rateCountDiffRecord = 0;
2354         priv->bswitch_fsync  = false;
2355
2356         if (priv->rtllib->mode == WIRELESS_MODE_N_24G) {
2357                 priv->rtllib->fsync_firstdiff_ratethreshold = 600;
2358                 priv->rtllib->fsync_seconddiff_ratethreshold = 0xffff;
2359         } else {
2360                 priv->rtllib->fsync_firstdiff_ratethreshold = 200;
2361                 priv->rtllib->fsync_seconddiff_ratethreshold = 200;
2362         }
2363         for (rateIndex = 0; rateIndex <= 27; rateIndex++) {
2364                 rateBitmap  = 1 << rateIndex;
2365                 if (priv->rtllib->fsync_rate_bitmap & rateBitmap)
2366                         priv->rate_record +=
2367                                  priv->stats.received_rate_histogram[1]
2368                                 [rateIndex];
2369         }
2370         if (timer_pending(&priv->fsync_timer))
2371                 del_timer_sync(&priv->fsync_timer);
2372         priv->fsync_timer.expires = jiffies +
2373                                     msecs_to_jiffies(priv->rtllib->fsync_time_interval);
2374         add_timer(&priv->fsync_timer);
2375
2376         write_nic_dword(dev, rOFDM0_RxDetector2, 0x465c12cd);
2377
2378 }
2379
2380 void dm_check_fsync(struct net_device *dev)
2381 {
2382 #define RegC38_Default                  0
2383 #define RegC38_NonFsync_Other_AP        1
2384 #define RegC38_Fsync_AP_BCM             2
2385         struct r8192_priv *priv = rtllib_priv(dev);
2386         static u8 reg_c38_State = RegC38_Default;
2387         static u32 reset_cnt;
2388
2389         RT_TRACE(COMP_HALDM,
2390                  "RSSI %d TimeInterval %d MultipleTimeInterval %d\n",
2391                  priv->rtllib->fsync_rssi_threshold,
2392                  priv->rtllib->fsync_time_interval,
2393                  priv->rtllib->fsync_multiple_timeinterval);
2394         RT_TRACE(COMP_HALDM,
2395                  "RateBitmap 0x%x FirstDiffRateThreshold %d SecondDiffRateThreshold %d\n",
2396                  priv->rtllib->fsync_rate_bitmap,
2397                  priv->rtllib->fsync_firstdiff_ratethreshold,
2398                  priv->rtllib->fsync_seconddiff_ratethreshold);
2399
2400         if (priv->rtllib->state == RTLLIB_LINKED &&
2401             priv->rtllib->pHTInfo->IOTPeer == HT_IOT_PEER_BROADCOM) {
2402                 if (priv->rtllib->bfsync_enable == 0) {
2403                         switch (priv->rtllib->fsync_state) {
2404                         case Default_Fsync:
2405                                 dm_StartHWFsync(dev);
2406                                 priv->rtllib->fsync_state = HW_Fsync;
2407                                 break;
2408                         case SW_Fsync:
2409                                 dm_EndSWFsync(dev);
2410                                 dm_StartHWFsync(dev);
2411                                 priv->rtllib->fsync_state = HW_Fsync;
2412                                 break;
2413                         case HW_Fsync:
2414                         default:
2415                                 break;
2416                         }
2417                 } else {
2418                         switch (priv->rtllib->fsync_state) {
2419                         case Default_Fsync:
2420                                 dm_StartSWFsync(dev);
2421                                 priv->rtllib->fsync_state = SW_Fsync;
2422                                 break;
2423                         case HW_Fsync:
2424                                 dm_EndHWFsync(dev);
2425                                 dm_StartSWFsync(dev);
2426                                 priv->rtllib->fsync_state = SW_Fsync;
2427                                 break;
2428                         case SW_Fsync:
2429                         default:
2430                                 break;
2431
2432                         }
2433                 }
2434                 if (priv->framesyncMonitor) {
2435                         if (reg_c38_State != RegC38_Fsync_AP_BCM) {
2436                                 write_nic_byte(dev, rOFDM0_RxDetector3, 0x95);
2437
2438                                 reg_c38_State = RegC38_Fsync_AP_BCM;
2439                         }
2440                 }
2441         } else {
2442                 switch (priv->rtllib->fsync_state) {
2443                 case HW_Fsync:
2444                         dm_EndHWFsync(dev);
2445                         priv->rtllib->fsync_state = Default_Fsync;
2446                         break;
2447                 case SW_Fsync:
2448                         dm_EndSWFsync(dev);
2449                         priv->rtllib->fsync_state = Default_Fsync;
2450                         break;
2451                 case Default_Fsync:
2452                 default:
2453                         break;
2454                 }
2455
2456                 if (priv->framesyncMonitor) {
2457                         if (priv->rtllib->state == RTLLIB_LINKED) {
2458                                 if (priv->undecorated_smoothed_pwdb <=
2459                                     RegC38_TH) {
2460                                         if (reg_c38_State !=
2461                                             RegC38_NonFsync_Other_AP) {
2462                                                         write_nic_byte(dev,
2463                                                             rOFDM0_RxDetector3,
2464                                                             0x90);
2465
2466                                                 reg_c38_State =
2467                                                      RegC38_NonFsync_Other_AP;
2468                                         }
2469                                 } else if (priv->undecorated_smoothed_pwdb >=
2470                                            (RegC38_TH+5)) {
2471                                         if (reg_c38_State) {
2472                                                 write_nic_byte(dev,
2473                                                         rOFDM0_RxDetector3,
2474                                                         priv->framesync);
2475                                                 reg_c38_State = RegC38_Default;
2476                                         }
2477                                 }
2478                         } else {
2479                                 if (reg_c38_State) {
2480                                         write_nic_byte(dev, rOFDM0_RxDetector3,
2481                                                        priv->framesync);
2482                                         reg_c38_State = RegC38_Default;
2483                                 }
2484                         }
2485                 }
2486         }
2487         if (priv->framesyncMonitor) {
2488                 if (priv->reset_count != reset_cnt) {
2489                         write_nic_byte(dev, rOFDM0_RxDetector3,
2490                                        priv->framesync);
2491                         reg_c38_State = RegC38_Default;
2492                         reset_cnt = priv->reset_count;
2493                 }
2494         } else {
2495                 if (reg_c38_State) {
2496                         write_nic_byte(dev, rOFDM0_RxDetector3,
2497                                        priv->framesync);
2498                         reg_c38_State = RegC38_Default;
2499                 }
2500         }
2501 }
2502
2503 /*---------------------------Define function prototype------------------------*/
2504 static void dm_init_dynamic_txpower(struct net_device *dev)
2505 {
2506         struct r8192_priv *priv = rtllib_priv(dev);
2507
2508         priv->rtllib->bdynamic_txpower_enable = true;
2509         priv->bLastDTPFlag_High = false;
2510         priv->bLastDTPFlag_Low = false;
2511         priv->bDynamicTxHighPower = false;
2512         priv->bDynamicTxLowPower = false;
2513 }
2514
2515 static void dm_dynamic_txpower(struct net_device *dev)
2516 {
2517         struct r8192_priv *priv = rtllib_priv(dev);
2518         unsigned int txhipower_threshhold = 0;
2519         unsigned int txlowpower_threshold = 0;
2520
2521         if (priv->rtllib->bdynamic_txpower_enable != true) {
2522                 priv->bDynamicTxHighPower = false;
2523                 priv->bDynamicTxLowPower = false;
2524                 return;
2525         }
2526         if ((priv->rtllib->pHTInfo->IOTPeer == HT_IOT_PEER_ATHEROS) &&
2527             (priv->rtllib->mode == IEEE_G)) {
2528                 txhipower_threshhold = TX_POWER_ATHEROAP_THRESH_HIGH;
2529                 txlowpower_threshold = TX_POWER_ATHEROAP_THRESH_LOW;
2530         } else {
2531                 txhipower_threshhold = TX_POWER_NEAR_FIELD_THRESH_HIGH;
2532                 txlowpower_threshold = TX_POWER_NEAR_FIELD_THRESH_LOW;
2533         }
2534
2535         RT_TRACE(COMP_TXAGC, "priv->undecorated_smoothed_pwdb = %ld\n",
2536                  priv->undecorated_smoothed_pwdb);
2537
2538         if (priv->rtllib->state == RTLLIB_LINKED) {
2539                 if (priv->undecorated_smoothed_pwdb >= txhipower_threshhold) {
2540                         priv->bDynamicTxHighPower = true;
2541                         priv->bDynamicTxLowPower = false;
2542                 } else {
2543                         if (priv->undecorated_smoothed_pwdb <
2544                             txlowpower_threshold && priv->bDynamicTxHighPower)
2545                                 priv->bDynamicTxHighPower = false;
2546                         if (priv->undecorated_smoothed_pwdb < 35)
2547                                 priv->bDynamicTxLowPower = true;
2548                         else if (priv->undecorated_smoothed_pwdb >= 40)
2549                                 priv->bDynamicTxLowPower = false;
2550                 }
2551         } else {
2552                 priv->bDynamicTxHighPower = false;
2553                 priv->bDynamicTxLowPower = false;
2554         }
2555
2556         if ((priv->bDynamicTxHighPower != priv->bLastDTPFlag_High) ||
2557             (priv->bDynamicTxLowPower != priv->bLastDTPFlag_Low)) {
2558                 RT_TRACE(COMP_TXAGC, "SetTxPowerLevel8190()  channel = %d\n",
2559                          priv->rtllib->current_network.channel);
2560
2561                 rtl8192_phy_setTxPower(dev,
2562                                  priv->rtllib->current_network.channel);
2563         }
2564         priv->bLastDTPFlag_High = priv->bDynamicTxHighPower;
2565         priv->bLastDTPFlag_Low = priv->bDynamicTxLowPower;
2566
2567 }
2568
2569 static void dm_check_txrateandretrycount(struct net_device *dev)
2570 {
2571         struct r8192_priv *priv = rtllib_priv(dev);
2572         struct rtllib_device *ieee = priv->rtllib;
2573
2574         ieee->softmac_stats.CurrentShowTxate = read_nic_byte(dev,
2575                                                  Current_Tx_Rate_Reg);
2576
2577         ieee->softmac_stats.last_packet_rate = read_nic_byte(dev,
2578                                                  Initial_Tx_Rate_Reg);
2579
2580         ieee->softmac_stats.txretrycount = read_nic_dword(dev,
2581                                                  Tx_Retry_Count_Reg);
2582 }
2583
2584 static void dm_send_rssi_tofw(struct net_device *dev)
2585 {
2586         struct r8192_priv *priv = rtllib_priv(dev);
2587
2588         write_nic_byte(dev, DRIVER_RSSI, (u8)priv->undecorated_smoothed_pwdb);
2589 }