forked from cilynx/rtl88x2bu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtw_pwrctrl.c
2792 lines (2291 loc) · 73.4 KB
/
rtw_pwrctrl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Copyright(c) 2007 - 2017 Realtek Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
*****************************************************************************/
#define _RTW_PWRCTRL_C_
#include <drv_types.h>
#include <hal_data.h>
#include <hal_com_h2c.h>
#ifdef DBG_CHECK_FW_PS_STATE
int rtw_fw_ps_state(PADAPTER padapter)
{
struct dvobj_priv *psdpriv = padapter->dvobj;
struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
int ret = _FAIL, dont_care = 0;
u16 fw_ps_state = 0;
struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
struct registry_priv *registry_par = &padapter->registrypriv;
if (registry_par->check_fw_ps != 1)
return _SUCCESS;
_enter_pwrlock(&pwrpriv->check_32k_lock);
if (RTW_CANNOT_RUN(padapter)) {
RTW_INFO("%s: bSurpriseRemoved=%s , hw_init_completed=%d, bDriverStopped=%s\n", __func__
, rtw_is_surprise_removed(padapter) ? "True" : "False"
, rtw_get_hw_init_completed(padapter)
, rtw_is_drv_stopped(padapter) ? "True" : "False");
goto exit_fw_ps_state;
}
#if defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8821C) || defined(CONFIG_RTL8822C)
rtw_hal_get_hwreg(padapter, HW_VAR_FW_PS_STATE, (u8 *)&fw_ps_state);
if ((fw_ps_state & BIT_LPS_STATUS) == 0)
ret = _SUCCESS;
else {
pdbgpriv->dbg_poll_fail_cnt++;
RTW_INFO("%s: fw_ps_state=%04x\n", __FUNCTION__, fw_ps_state);
}
#else
rtw_hal_set_hwreg(padapter, HW_VAR_SET_REQ_FW_PS, (u8 *)&dont_care);
{
/* 4. if 0x88[7]=1, driver set cmd to leave LPS/IPS. */
/* Else, hw will keep in active mode. */
/* debug info: */
/* 0x88[7] = 32kpermission, */
/* 0x88[6:0] = current_ps_state */
/* 0x89[7:0] = last_rpwm */
rtw_hal_get_hwreg(padapter, HW_VAR_FW_PS_STATE, (u8 *)&fw_ps_state);
if ((fw_ps_state & 0x80) == 0)
ret = _SUCCESS;
else {
pdbgpriv->dbg_poll_fail_cnt++;
RTW_INFO("%s: fw_ps_state=%04x\n", __FUNCTION__, fw_ps_state);
}
}
#endif
exit_fw_ps_state:
_exit_pwrlock(&pwrpriv->check_32k_lock);
return ret;
}
#endif /*DBG_CHECK_FW_PS_STATE*/
#ifdef CONFIG_IPS
void _ips_enter(_adapter *padapter)
{
struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
pwrpriv->bips_processing = _TRUE;
/* syn ips_mode with request */
pwrpriv->ips_mode = pwrpriv->ips_mode_req;
pwrpriv->ips_enter_cnts++;
RTW_INFO("==>ips_enter cnts:%d\n", pwrpriv->ips_enter_cnts);
if (rf_off == pwrpriv->change_rfpwrstate) {
pwrpriv->bpower_saving = _TRUE;
RTW_PRINT("nolinked power save enter\n");
if (pwrpriv->ips_mode == IPS_LEVEL_2)
pwrpriv->bkeepfwalive = _TRUE;
#ifdef CONFIG_RTW_CFGVEDNOR_LLSTATS
pwrpriv->pwr_saving_start_time = rtw_get_current_time();
#endif /* CONFIG_RTW_CFGVEDNOR_LLSTATS */
rtw_ips_pwr_down(padapter);
pwrpriv->rf_pwrstate = rf_off;
}
pwrpriv->bips_processing = _FALSE;
}
void ips_enter(_adapter *padapter)
{
struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
#ifdef CONFIG_BT_COEXIST
rtw_btcoex_IpsNotify(padapter, pwrpriv->ips_mode_req);
#endif /* CONFIG_BT_COEXIST */
_enter_pwrlock(&pwrpriv->lock);
_ips_enter(padapter);
_exit_pwrlock(&pwrpriv->lock);
}
int _ips_leave(_adapter *padapter)
{
struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
int result = _SUCCESS;
if ((pwrpriv->rf_pwrstate == rf_off) && (!pwrpriv->bips_processing)) {
pwrpriv->bips_processing = _TRUE;
pwrpriv->change_rfpwrstate = rf_on;
pwrpriv->ips_leave_cnts++;
RTW_INFO("==>ips_leave cnts:%d\n", pwrpriv->ips_leave_cnts);
result = rtw_ips_pwr_up(padapter);
if (result == _SUCCESS)
pwrpriv->rf_pwrstate = rf_on;
#ifdef CONFIG_RTW_CFGVEDNOR_LLSTATS
pwrpriv->pwr_saving_time += rtw_get_passing_time_ms(pwrpriv->pwr_saving_start_time);
#endif /* CONFIG_RTW_CFGVEDNOR_LLSTATS */
RTW_PRINT("nolinked power save leave\n");
RTW_INFO("==> ips_leave.....LED(0x%08x)...\n", rtw_read32(padapter, 0x4c));
pwrpriv->bips_processing = _FALSE;
pwrpriv->bkeepfwalive = _FALSE;
pwrpriv->bpower_saving = _FALSE;
}
return result;
}
int ips_leave(_adapter *padapter)
{
struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
#ifdef DBG_CHECK_FW_PS_STATE
struct dvobj_priv *psdpriv = padapter->dvobj;
struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
#endif
int ret;
if (!is_primary_adapter(padapter))
return _SUCCESS;
_enter_pwrlock(&pwrpriv->lock);
ret = _ips_leave(padapter);
#ifdef DBG_CHECK_FW_PS_STATE
if (rtw_fw_ps_state(padapter) == _FAIL) {
RTW_INFO("ips leave doesn't leave 32k\n");
pdbgpriv->dbg_leave_ips_fail_cnt++;
}
#endif /* DBG_CHECK_FW_PS_STATE */
_exit_pwrlock(&pwrpriv->lock);
if (_SUCCESS == ret)
odm_dm_reset(&GET_HAL_DATA(padapter)->odmpriv);
#ifdef CONFIG_BT_COEXIST
if (_SUCCESS == ret)
rtw_btcoex_IpsNotify(padapter, IPS_NONE);
#endif /* CONFIG_BT_COEXIST */
return ret;
}
#endif /* CONFIG_IPS */
#ifdef CONFIG_AUTOSUSPEND
extern void autosuspend_enter(_adapter *padapter);
extern int autoresume_enter(_adapter *padapter);
#endif
#ifdef SUPPORT_HW_RFOFF_DETECTED
int rtw_hw_suspend(_adapter *padapter);
int rtw_hw_resume(_adapter *padapter);
#endif
bool rtw_pwr_unassociated_idle(_adapter *adapter)
{
u8 i;
_adapter *iface;
struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
struct xmit_priv *pxmit_priv = &adapter->xmitpriv;
struct mlme_priv *pmlmepriv;
#ifdef CONFIG_P2P
struct wifidirect_info *pwdinfo;
#endif
bool ret = _FALSE;
if (adapter_to_pwrctl(adapter)->bpower_saving == _TRUE) {
/* RTW_INFO("%s: already in LPS or IPS mode\n", __func__); */
goto exit;
}
if (rtw_time_after(adapter_to_pwrctl(adapter)->ips_deny_time, rtw_get_current_time())) {
/* RTW_INFO("%s ips_deny_time\n", __func__); */
goto exit;
}
for (i = 0; i < dvobj->iface_nums; i++) {
iface = dvobj->padapters[i];
if ((iface) && rtw_is_adapter_up(iface)) {
pmlmepriv = &(iface->mlmepriv);
#ifdef CONFIG_P2P
pwdinfo = &(iface->wdinfo);
#endif
if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE | WIFI_SITE_MONITOR)
|| check_fwstate(pmlmepriv, WIFI_UNDER_LINKING | WIFI_UNDER_WPS)
|| MLME_IS_AP(iface)
|| MLME_IS_MESH(iface)
|| check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE | WIFI_ADHOC_STATE)
#if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211)
|| rtw_cfg80211_get_is_roch(iface) == _TRUE
|| (rtw_cfg80211_is_ro_ch_once(adapter)
&& rtw_cfg80211_get_last_ro_ch_passing_ms(adapter) < 3000)
#elif defined(CONFIG_P2P)
|| rtw_p2p_chk_state(pwdinfo, P2P_STATE_IDLE)
|| rtw_p2p_chk_state(pwdinfo, P2P_STATE_LISTEN)
#endif
)
goto exit;
}
}
#if (MP_DRIVER == 1)
if (adapter->registrypriv.mp_mode == 1)
goto exit;
#endif
#ifdef CONFIG_INTEL_PROXIM
if (adapter->proximity.proxim_on == _TRUE)
return;
#endif
if (pxmit_priv->free_xmitbuf_cnt != NR_XMITBUFF ||
pxmit_priv->free_xmit_extbuf_cnt != NR_XMIT_EXTBUFF) {
RTW_PRINT("There are some pkts to transmit\n");
RTW_PRINT("free_xmitbuf_cnt: %d, free_xmit_extbuf_cnt: %d\n",
pxmit_priv->free_xmitbuf_cnt, pxmit_priv->free_xmit_extbuf_cnt);
goto exit;
}
ret = _TRUE;
exit:
return ret;
}
/*
* ATTENTION:
* rtw_ps_processor() doesn't handle LPS.
*/
void rtw_ps_processor(_adapter *padapter)
{
struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
struct dvobj_priv *psdpriv = padapter->dvobj;
struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
#ifdef SUPPORT_HW_RFOFF_DETECTED
rt_rf_power_state rfpwrstate;
#endif /* SUPPORT_HW_RFOFF_DETECTED */
u32 ps_deny = 0;
_enter_pwrlock(&adapter_to_pwrctl(padapter)->lock);
ps_deny = rtw_ps_deny_get(padapter);
_exit_pwrlock(&adapter_to_pwrctl(padapter)->lock);
if (ps_deny != 0) {
RTW_INFO(FUNC_ADPT_FMT ": ps_deny=0x%08X, skip power save!\n",
FUNC_ADPT_ARG(padapter), ps_deny);
goto exit;
}
if (pwrpriv->bInSuspend == _TRUE) { /* system suspend or autosuspend */
pdbgpriv->dbg_ps_insuspend_cnt++;
RTW_INFO("%s, pwrpriv->bInSuspend == _TRUE ignore this process\n", __FUNCTION__);
return;
}
pwrpriv->ps_processing = _TRUE;
#ifdef SUPPORT_HW_RFOFF_DETECTED
if (pwrpriv->bips_processing == _TRUE)
goto exit;
/* RTW_INFO("==> fw report state(0x%x)\n",rtw_read8(padapter,0x1ca)); */
if (pwrpriv->bHWPwrPindetect) {
#ifdef CONFIG_AUTOSUSPEND
if (padapter->registrypriv.usbss_enable) {
if (pwrpriv->rf_pwrstate == rf_on) {
if (padapter->net_closed == _TRUE)
pwrpriv->ps_flag = _TRUE;
rfpwrstate = RfOnOffDetect(padapter);
RTW_INFO("@@@@- #1 %s==> rfstate:%s\n", __FUNCTION__, (rfpwrstate == rf_on) ? "rf_on" : "rf_off");
if (rfpwrstate != pwrpriv->rf_pwrstate) {
if (rfpwrstate == rf_off) {
pwrpriv->change_rfpwrstate = rf_off;
pwrpriv->bkeepfwalive = _TRUE;
pwrpriv->brfoffbyhw = _TRUE;
autosuspend_enter(padapter);
}
}
}
} else
#endif /* CONFIG_AUTOSUSPEND */
{
rfpwrstate = RfOnOffDetect(padapter);
RTW_INFO("@@@@- #2 %s==> rfstate:%s\n", __FUNCTION__, (rfpwrstate == rf_on) ? "rf_on" : "rf_off");
if (rfpwrstate != pwrpriv->rf_pwrstate) {
if (rfpwrstate == rf_off) {
pwrpriv->change_rfpwrstate = rf_off;
pwrpriv->brfoffbyhw = _TRUE;
rtw_hw_suspend(padapter);
} else {
pwrpriv->change_rfpwrstate = rf_on;
rtw_hw_resume(padapter);
}
RTW_INFO("current rf_pwrstate(%s)\n", (pwrpriv->rf_pwrstate == rf_off) ? "rf_off" : "rf_on");
}
}
pwrpriv->pwr_state_check_cnts++;
}
#endif /* SUPPORT_HW_RFOFF_DETECTED */
if (pwrpriv->ips_mode_req == IPS_NONE)
goto exit;
if (rtw_pwr_unassociated_idle(padapter) == _FALSE)
goto exit;
if ((pwrpriv->rf_pwrstate == rf_on) && ((pwrpriv->pwr_state_check_cnts % 4) == 0)) {
RTW_INFO("==>%s .fw_state(%x)\n", __FUNCTION__, get_fwstate(pmlmepriv));
#if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
#else
pwrpriv->change_rfpwrstate = rf_off;
#endif
#ifdef CONFIG_AUTOSUSPEND
if (padapter->registrypriv.usbss_enable) {
if (pwrpriv->bHWPwrPindetect)
pwrpriv->bkeepfwalive = _TRUE;
if (padapter->net_closed == _TRUE)
pwrpriv->ps_flag = _TRUE;
#if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
if (_TRUE == pwrpriv->bInternalAutoSuspend)
RTW_INFO("<==%s .pwrpriv->bInternalAutoSuspend)(%x)\n", __FUNCTION__, pwrpriv->bInternalAutoSuspend);
else {
pwrpriv->change_rfpwrstate = rf_off;
RTW_INFO("<==%s .pwrpriv->bInternalAutoSuspend)(%x) call autosuspend_enter\n", __FUNCTION__, pwrpriv->bInternalAutoSuspend);
autosuspend_enter(padapter);
}
#else
autosuspend_enter(padapter);
#endif /* if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND) */
} else if (pwrpriv->bHWPwrPindetect) {
} else
#endif /* CONFIG_AUTOSUSPEND */
{
#if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
pwrpriv->change_rfpwrstate = rf_off;
#endif /* defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND) */
#ifdef CONFIG_IPS
ips_enter(padapter);
#endif
}
}
exit:
#ifndef CONFIG_IPS_CHECK_IN_WD
rtw_set_pwr_state_check_timer(pwrpriv);
#endif
pwrpriv->ps_processing = _FALSE;
return;
}
void pwr_state_check_handler(void *ctx)
{
_adapter *padapter = (_adapter *)ctx;
rtw_ps_cmd(padapter);
}
#ifdef CONFIG_LPS
#ifdef CONFIG_CHECK_LEAVE_LPS
#ifdef CONFIG_LPS_CHK_BY_TP
void traffic_check_for_leave_lps_by_tp(PADAPTER padapter, u8 tx, struct sta_info *sta)
{
struct stainfo_stats *pstats = &sta->sta_stats;
u64 cur_acc_tx_bytes = 0, cur_acc_rx_bytes = 0;
u32 tx_tp_kbyte = 0, rx_tp_kbyte = 0;
u32 tx_tp_th = 0, rx_tp_th = 0;
struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
u8 leave_lps = _FALSE;
if (tx) { /* from tx */
cur_acc_tx_bytes = pstats->tx_bytes - pstats->acc_tx_bytes;
tx_tp_kbyte = cur_acc_tx_bytes >> 10;
tx_tp_th = pwrpriv->lps_tx_tp_th * 1024 / 8 * 2; /*KBytes @2s*/
if (tx_tp_kbyte >= tx_tp_th ||
padapter->mlmepriv.LinkDetectInfo.NumTxOkInPeriod >= pwrpriv->lps_tx_pkts){
if (pwrpriv->bLeisurePs
&& (pwrpriv->pwr_mode != PS_MODE_ACTIVE)
#ifdef CONFIG_BT_COEXIST
&& (rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
#endif
) {
leave_lps = _TRUE;
}
}
} else { /* from rx path */
cur_acc_rx_bytes = pstats->rx_bytes - pstats->acc_rx_bytes;
rx_tp_kbyte = cur_acc_rx_bytes >> 10;
rx_tp_th = pwrpriv->lps_rx_tp_th * 1024 / 8 * 2;
if (rx_tp_kbyte>= rx_tp_th ||
padapter->mlmepriv.LinkDetectInfo.NumRxUnicastOkInPeriod >= pwrpriv->lps_rx_pkts) {
if (pwrpriv->bLeisurePs
&& (pwrpriv->pwr_mode != PS_MODE_ACTIVE)
#ifdef CONFIG_BT_COEXIST
&& (rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
#endif
) {
leave_lps = _TRUE;
}
}
}
if (leave_lps) {
#ifdef DBG_LPS_CHK_BY_TP
RTW_INFO("leave lps via %s, ", tx ? "Tx" : "Rx");
if (tx)
RTW_INFO("Tx = %d [%d] (KB)\n", tx_tp_kbyte, tx_tp_th);
else
RTW_INFO("Rx = %d [%d] (KB)\n", rx_tp_kbyte, rx_tp_th);
#endif
pwrpriv->lps_chk_cnt = pwrpriv->lps_chk_cnt_th;
/* rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, 1); */
rtw_lps_ctrl_wk_cmd(padapter, tx ? LPS_CTRL_TX_TRAFFIC_LEAVE : LPS_CTRL_RX_TRAFFIC_LEAVE, 1);
}
}
#endif /*CONFIG_LPS_CHK_BY_TP*/
void traffic_check_for_leave_lps(PADAPTER padapter, u8 tx, u32 tx_packets)
{
static systime start_time = 0;
static u32 xmit_cnt = 0;
u8 bLeaveLPS = _FALSE;
struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
if (tx) { /* from tx */
xmit_cnt += tx_packets;
if (start_time == 0)
start_time = rtw_get_current_time();
if (rtw_get_passing_time_ms(start_time) > 2000) { /* 2 sec == watch dog timer */
if (xmit_cnt > 8) {
if ((adapter_to_pwrctl(padapter)->bLeisurePs)
&& (adapter_to_pwrctl(padapter)->pwr_mode != PS_MODE_ACTIVE)
#ifdef CONFIG_BT_COEXIST
&& (rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
#endif
) {
/* RTW_INFO("leave lps via Tx = %d\n", xmit_cnt); */
bLeaveLPS = _TRUE;
}
}
start_time = rtw_get_current_time();
xmit_cnt = 0;
}
} else { /* from rx path */
if (pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod > 4/*2*/) {
if ((adapter_to_pwrctl(padapter)->bLeisurePs)
&& (adapter_to_pwrctl(padapter)->pwr_mode != PS_MODE_ACTIVE)
#ifdef CONFIG_BT_COEXIST
&& (rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
#endif
) {
/* RTW_INFO("leave lps via Rx = %d\n", pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod); */
bLeaveLPS = _TRUE;
}
}
}
if (bLeaveLPS) {
/* RTW_INFO("leave lps via %s, Tx = %d, Rx = %d\n", tx?"Tx":"Rx", pmlmepriv->LinkDetectInfo.NumTxOkInPeriod,pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod); */
/* rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, 1); */
rtw_lps_ctrl_wk_cmd(padapter, tx ? LPS_CTRL_TX_TRAFFIC_LEAVE : LPS_CTRL_RX_TRAFFIC_LEAVE, tx ? 0 : 1);
}
}
#endif /* CONFIG_CHECK_LEAVE_LPS */
#ifdef CONFIG_LPS_LCLK
#define LPS_CPWM_TIMEOUT_MS 10 /*ms*/
#define LPS_RPWM_RETRY_CNT 3
u8 rtw_cpwm_polling(_adapter *adapter, u8 rpwm, u8 cpwm_orig)
{
u8 rst = _FAIL;
u8 cpwm_now = 0;
systime start_time;
struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter);
#ifdef DBG_CHECK_FW_PS_STATE
struct debug_priv *pdbgpriv = &(adapter_to_dvobj(adapter)->drv_dbg);
#endif
pwrpriv->rpwm_retry = 0;
do {
start_time = rtw_get_current_time();
do {
rtw_msleep_os(1);
rtw_hal_get_hwreg(adapter, HW_VAR_CPWM, &cpwm_now);
if ((cpwm_orig ^ cpwm_now) & 0x80) {
pwrpriv->cpwm = PS_STATE_S4;
pwrpriv->cpwm_tog = cpwm_now & PS_TOGGLE;
rst = _SUCCESS;
break;
}
} while (rtw_get_passing_time_ms(start_time) < LPS_CPWM_TIMEOUT_MS && !RTW_CANNOT_RUN(adapter));
if (rst == _SUCCESS)
break;
else {
/* rpwm retry */
cpwm_orig = cpwm_now;
rpwm &= ~PS_TOGGLE;
rpwm |= pwrpriv->tog;
rtw_hal_set_hwreg(adapter, HW_VAR_SET_RPWM, (u8 *)(&rpwm));
pwrpriv->tog += 0x80;
}
} while (pwrpriv->rpwm_retry++ < LPS_RPWM_RETRY_CNT && !RTW_CANNOT_RUN(adapter));
if (rst == _SUCCESS) {
#ifdef DBG_CHECK_FW_PS_STATE
RTW_INFO("%s: polling cpwm OK! rpwm_retry=%d, cpwm_orig=%02x, cpwm_now=%02x , 0x100=0x%x\n"
, __func__, pwrpriv->rpwm_retry, cpwm_orig, cpwm_now, rtw_read8(adapter, REG_CR));
if (rtw_fw_ps_state(adapter) == _FAIL) {
RTW_INFO("leave 32k but fw state in 32k\n");
pdbgpriv->dbg_rpwm_toogle_cnt++;
}
#endif /* DBG_CHECK_FW_PS_STATE */
} else {
RTW_ERR("%s: polling cpwm timeout! rpwm_retry=%d, cpwm_orig=%02x, cpwm_now=%02x\n"
, __func__, pwrpriv->rpwm_retry, cpwm_orig, cpwm_now);
#ifdef DBG_CHECK_FW_PS_STATE
if (rtw_fw_ps_state(adapter) == _FAIL) {
RTW_INFO("rpwm timeout and fw ps state in 32k\n");
pdbgpriv->dbg_rpwm_timeout_fail_cnt++;
}
#endif /* DBG_CHECK_FW_PS_STATE */
#ifdef CONFIG_LPS_RPWM_TIMER
_set_timer(&pwrpriv->pwr_rpwm_timer, 1);
#endif /* CONFIG_LPS_RPWM_TIMER */
}
return rst;
}
#endif
/*
* Description:
* This function MUST be called under power lock protect
*
* Parameters
* padapter
* pslv power state level, only could be PS_STATE_S0 ~ PS_STATE_S4
*
*/
u8 rtw_set_rpwm(PADAPTER padapter, u8 pslv)
{
u8 rpwm = 0xFF;
struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
#ifdef CONFIG_LPS_LCLK
u8 cpwm_orig;
#endif
pslv = PS_STATE(pslv);
#ifdef CONFIG_LPS_RPWM_TIMER
if (pwrpriv->brpwmtimeout == _TRUE)
RTW_INFO("%s: RPWM timeout, force to set RPWM(0x%02X) again!\n", __FUNCTION__, pslv);
else
#endif /* CONFIG_LPS_RPWM_TIMER */
{
if ((pwrpriv->rpwm == pslv)
#ifdef CONFIG_LPS_LCLK
|| ((pwrpriv->rpwm >= PS_STATE_S2) && (pslv >= PS_STATE_S2))
#endif
|| (pwrpriv->lps_level == LPS_NORMAL)
) {
return rpwm;
}
}
if (rtw_is_surprise_removed(padapter) ||
(!rtw_is_hw_init_completed(padapter))) {
pwrpriv->cpwm = PS_STATE_S4;
return rpwm;
}
if (rtw_is_drv_stopped(padapter))
if (pslv < PS_STATE_S2)
return rpwm;
rpwm = pslv | pwrpriv->tog;
#ifdef CONFIG_LPS_LCLK
/* only when from PS_STATE S0/S1 to S2 and higher needs ACK */
if ((pwrpriv->cpwm < PS_STATE_S2) && (pslv >= PS_STATE_S2))
rpwm |= PS_ACK;
#endif
pwrpriv->rpwm = pslv;
#ifdef CONFIG_LPS_LCLK
cpwm_orig = 0;
if (rpwm & PS_ACK)
rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_orig);
#endif
#if defined(CONFIG_LPS_RPWM_TIMER) && !defined(CONFIG_DETECT_CPWM_BY_POLLING)
if (rpwm & PS_ACK) {
#if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) || defined(CONFIG_P2P_WOWLAN)
if (pwrpriv->wowlan_mode != _TRUE &&
pwrpriv->wowlan_ap_mode != _TRUE &&
pwrpriv->wowlan_p2p_mode != _TRUE)
#endif
_set_timer(&pwrpriv->pwr_rpwm_timer, LPS_CPWM_TIMEOUT_MS);
}
#endif /* CONFIG_LPS_RPWM_TIMER & !CONFIG_DETECT_CPWM_BY_POLLING */
rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&rpwm));
pwrpriv->tog += 0x80;
#ifdef CONFIG_LPS_LCLK
/* No LPS 32K, No Ack */
if (rpwm & PS_ACK) {
#ifdef CONFIG_DETECT_CPWM_BY_POLLING
rtw_cpwm_polling(padapter, rpwm, cpwm_orig);
#else
#if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) || defined(CONFIG_P2P_WOWLAN)
if (pwrpriv->wowlan_mode == _TRUE ||
pwrpriv->wowlan_ap_mode == _TRUE ||
pwrpriv->wowlan_p2p_mode == _TRUE)
rtw_cpwm_polling(padapter, rpwm, cpwm_orig);
#endif /*#if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) || defined(CONFIG_P2P_WOWLAN)*/
#endif /*#ifdef CONFIG_DETECT_CPWM_BY_POLLING*/
} else
#endif /* CONFIG_LPS_LCLK */
{
pwrpriv->cpwm = pslv;
}
return rpwm;
}
u8 PS_RDY_CHECK(_adapter *padapter)
{
u32 delta_ms;
struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
#if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN)
if (_TRUE == pwrpriv->bInSuspend && pwrpriv->wowlan_mode)
return _TRUE;
else if (_TRUE == pwrpriv->bInSuspend && pwrpriv->wowlan_ap_mode)
return _TRUE;
else if (_TRUE == pwrpriv->bInSuspend)
return _FALSE;
#else
if (_TRUE == pwrpriv->bInSuspend)
return _FALSE;
#endif
delta_ms = rtw_get_passing_time_ms(pwrpriv->DelayLPSLastTimeStamp);
if (delta_ms < LPS_DELAY_MS)
return _FALSE;
if (check_fwstate(pmlmepriv, WIFI_SITE_MONITOR)
|| check_fwstate(pmlmepriv, WIFI_UNDER_LINKING | WIFI_UNDER_WPS)
|| MLME_IS_AP(padapter)
|| MLME_IS_MESH(padapter)
|| check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE | WIFI_ADHOC_STATE)
#if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211)
|| rtw_cfg80211_get_is_roch(padapter) == _TRUE
#endif
|| rtw_is_scan_deny(padapter)
#ifdef CONFIG_TDLS
/* TDLS link is established. */
|| (padapter->tdlsinfo.link_established == _TRUE)
#endif /* CONFIG_TDLS */
#ifdef CONFIG_DFS_MASTER
|| adapter_to_rfctl(padapter)->radar_detect_enabled
#endif
)
return _FALSE;
if ((padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) && (padapter->securitypriv.binstallGrpkey == _FALSE)) {
RTW_INFO("Group handshake still in progress !!!\n");
return _FALSE;
}
#ifdef CONFIG_IOCTL_CFG80211
if (!rtw_cfg80211_pwr_mgmt(padapter))
return _FALSE;
#endif
return _TRUE;
}
#if defined(CONFIG_FWLPS_IN_IPS)
void rtw_set_fw_in_ips_mode(PADAPTER padapter, u8 enable)
{
struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
int cnt = 0;
systime start_time;
u8 val8 = 0;
u8 cpwm_orig = 0, cpwm_now = 0;
u8 parm[H2C_INACTIVE_PS_LEN] = {0};
if (padapter->netif_up == _FALSE) {
RTW_INFO("%s: ERROR, netif is down\n", __func__);
return;
}
/* u8 cmd_param; */ /* BIT0:enable, BIT1:NoConnect32k */
if (enable) {
#ifdef CONFIG_BT_COEXIST
rtw_btcoex_IpsNotify(padapter, pwrpriv->ips_mode_req);
#endif
/* Enter IPS */
RTW_INFO("%s: issue H2C to FW when entering IPS\n", __func__);
parm[0] = 0x1;/* suggest by Isaac.Hsu*/
#ifdef CONFIG_PNO_SUPPORT
if (pwrpriv->pno_inited) {
parm[1] = pwrpriv->pnlo_info->fast_scan_iterations;
parm[2] = pwrpriv->pnlo_info->slow_scan_period;
}
#endif
rtw_hal_fill_h2c_cmd(padapter, /* H2C_FWLPS_IN_IPS_, */
H2C_INACTIVE_PS_,
H2C_INACTIVE_PS_LEN, parm);
/* poll 0x1cc to make sure H2C command already finished by FW; MAC_0x1cc=0 means H2C done by FW. */
do {
val8 = rtw_read8(padapter, REG_HMETFR);
cnt++;
RTW_INFO("%s polling REG_HMETFR=0x%x, cnt=%d\n",
__func__, val8, cnt);
rtw_mdelay_os(10);
} while (cnt < 100 && (val8 != 0));
#ifdef CONFIG_LPS_LCLK
/* H2C done, enter 32k */
if (val8 == 0) {
/* ser rpwm to enter 32k */
rtw_hal_get_hwreg(padapter, HW_VAR_RPWM_TOG, &val8);
RTW_INFO("%s: read rpwm=%02x\n", __FUNCTION__, val8);
val8 += 0x80;
val8 |= BIT(0);
rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&val8));
RTW_INFO("%s: write rpwm=%02x\n", __FUNCTION__, val8);
adapter_to_pwrctl(padapter)->tog = (val8 + 0x80) & 0x80;
cnt = val8 = 0;
if (parm[1] == 0 || parm[2] == 0) {
do {
val8 = rtw_read8(padapter, REG_CR);
cnt++;
RTW_INFO("%s polling 0x100=0x%x, cnt=%d\n",
__func__, val8, cnt);
RTW_INFO("%s 0x08:%02x, 0x03:%02x\n",
__func__,
rtw_read8(padapter, 0x08),
rtw_read8(padapter, 0x03));
rtw_mdelay_os(10);
} while (cnt < 20 && (val8 != 0xEA));
}
}
#endif
} else {
/* Leave IPS */
RTW_INFO("%s: Leaving IPS in FWLPS state\n", __func__);
#ifdef CONFIG_LPS_LCLK
/* for polling cpwm */
cpwm_orig = 0;
rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_orig);
/* ser rpwm */
rtw_hal_get_hwreg(padapter, HW_VAR_RPWM_TOG, &val8);
val8 += 0x80;
val8 |= BIT(6);
rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&val8));
RTW_INFO("%s: write rpwm=%02x\n", __FUNCTION__, val8);
adapter_to_pwrctl(padapter)->tog = (val8 + 0x80) & 0x80;
/* do polling cpwm */
start_time = rtw_get_current_time();
do {
rtw_mdelay_os(1);
rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_now);
if ((cpwm_orig ^ cpwm_now) & 0x80)
break;
if (rtw_get_passing_time_ms(start_time) > 100) {
RTW_INFO("%s: polling cpwm timeout when leaving IPS in FWLPS state\n", __FUNCTION__);
break;
}
} while (1);
#endif
parm[0] = 0x0;
parm[1] = 0x0;
parm[2] = 0x0;
rtw_hal_fill_h2c_cmd(padapter, H2C_INACTIVE_PS_,
H2C_INACTIVE_PS_LEN, parm);
#ifdef CONFIG_BT_COEXIST
rtw_btcoex_IpsNotify(padapter, IPS_NONE);
#endif
}
}
#endif /* CONFIG_PNO_SUPPORT */
void rtw_set_ps_mode(PADAPTER padapter, u8 ps_mode, u8 smart_ps, u8 bcn_ant_mode, const char *msg)
{
struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
#if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) || defined(CONFIG_P2P_WOWLAN)
struct dvobj_priv *psdpriv = padapter->dvobj;
struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
#endif
#ifdef CONFIG_WMMPS_STA
struct registry_priv *pregistrypriv = &padapter->registrypriv;
#endif
#ifdef CONFIG_P2P
struct wifidirect_info *pwdinfo = &(padapter->wdinfo);
#endif /* CONFIG_P2P */
#ifdef CONFIG_TDLS
struct sta_priv *pstapriv = &padapter->stapriv;
_irqL irqL;
int i, j;
_list *plist, *phead;
struct sta_info *ptdls_sta;
#endif /* CONFIG_TDLS */
#ifdef CONFIG_LPS_PG
u8 lps_pg_hdl_id = 0;
#endif
if (ps_mode > PM_Card_Disable) {
return;
}
if (pwrpriv->pwr_mode == ps_mode) {
if (PS_MODE_ACTIVE == ps_mode)
return;
#ifndef CONFIG_BT_COEXIST
#ifdef CONFIG_WMMPS_STA
if (!rtw_is_wmmps_mode(padapter))
#endif /* CONFIG_WMMPS_STA */
if ((pwrpriv->smart_ps == smart_ps) &&
(pwrpriv->bcn_ant_mode == bcn_ant_mode))
return;
#endif /* !CONFIG_BT_COEXIST */
}
#ifdef CONFIG_FW_MULTI_PORT_SUPPORT
if (PS_MODE_ACTIVE != ps_mode) {
rtw_set_ps_rsvd_page(padapter);
rtw_set_default_port_id(padapter);
}
#endif
#ifdef CONFIG_LPS_PG
if ((PS_MODE_ACTIVE != ps_mode) && (pwrpriv->blpspg_info_up)) {
/*rtw_hal_set_lps_pg_info(padapter);*/
lps_pg_hdl_id = LPS_PG_INFO_CFG;
rtw_hal_set_hwreg(padapter, HW_VAR_LPS_PG_HANDLE, (u8 *)(&lps_pg_hdl_id));
}
#endif
#ifdef CONFIG_LPS_LCLK
_enter_pwrlock(&pwrpriv->lock);
#endif
/* if(pwrpriv->pwr_mode == PS_MODE_ACTIVE) */
if (ps_mode == PS_MODE_ACTIVE) {
if (1
#ifdef CONFIG_BT_COEXIST
&& (((rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
#ifdef CONFIG_P2P_PS
&& (pwdinfo->opp_ps == 0)
#endif /* CONFIG_P2P_PS */
)
|| ((rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
&& (rtw_btcoex_IsLpsOn(padapter) == _FALSE))
)
#else /* !CONFIG_BT_COEXIST */
#ifdef CONFIG_P2P_PS
&& (pwdinfo->opp_ps == 0)
#endif /* CONFIG_P2P_PS */
#endif /* !CONFIG_BT_COEXIST */
) {
RTW_INFO(FUNC_ADPT_FMT" Leave 802.11 power save - %s\n",
FUNC_ADPT_ARG(padapter), msg);
if (pwrpriv->lps_leave_cnts < UINT_MAX)
pwrpriv->lps_leave_cnts++;
else
pwrpriv->lps_leave_cnts = 0;
#ifdef CONFIG_TDLS
for (i = 0; i < NUM_STA; i++) {
phead = &(pstapriv->sta_hash[i]);
plist = get_next(phead);
while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
ptdls_sta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
if (ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE)
issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta->cmn.mac_addr, 0, 0, 0);
plist = get_next(plist);
}
}
#endif /* CONFIG_TDLS */
pwrpriv->pwr_mode = ps_mode;
rtw_set_rpwm(padapter, PS_STATE_S4);
#if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) || defined(CONFIG_P2P_WOWLAN)
if (pwrpriv->wowlan_mode == _TRUE ||
pwrpriv->wowlan_ap_mode == _TRUE ||
pwrpriv->wowlan_p2p_mode == _TRUE) {
systime start_time;
u32 delay_ms;
u8 val8;
delay_ms = 20;
start_time = rtw_get_current_time();
do {
rtw_hal_get_hwreg(padapter, HW_VAR_SYS_CLKR, &val8);
if (!(val8 & BIT(4))) { /* 0x08 bit4 =1 --> in 32k, bit4 = 0 --> leave 32k */
pwrpriv->cpwm = PS_STATE_S4;
break;
}
if (rtw_get_passing_time_ms(start_time) > delay_ms) {
RTW_INFO("%s: Wait for FW 32K leave more than %u ms!!!\n",
__FUNCTION__, delay_ms);
pdbgpriv->dbg_wow_leave_ps_fail_cnt++;
break;
}
rtw_usleep_os(100);
} while (1);
}
#endif
#ifdef CONFIG_LPS_PG
if (pwrpriv->lps_level == LPS_PG) {
lps_pg_hdl_id = LPS_PG_REDLEMEM;
rtw_hal_set_hwreg(padapter, HW_VAR_LPS_PG_HANDLE, (u8 *)(&lps_pg_hdl_id));
}
#endif
#ifdef CONFIG_WOWLAN