net: wireless: rockchip_wlan: add rtl8723ds support
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rtl8723ds / core / rtw_pwrctrl.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _RTW_PWRCTRL_C_
21
22 #include <drv_types.h>
23 #include <hal_data.h>
24 #include <hal_com_h2c.h>
25
26 int rtw_fw_ps_state(PADAPTER padapter)
27 {
28         struct dvobj_priv *psdpriv = padapter->dvobj;
29         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
30         int ret = _FAIL, dont_care = 0;
31         u16 fw_ps_state = 0;
32         u32 start_time;
33         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
34         struct registry_priv  *registry_par = &padapter->registrypriv;
35
36         if (registry_par->check_fw_ps != 1)
37                 return _SUCCESS;
38
39         _enter_pwrlock(&pwrpriv->check_32k_lock);
40
41         if (RTW_CANNOT_RUN(padapter)) {
42                 RTW_INFO("%s: bSurpriseRemoved=%s , hw_init_completed=%d, bDriverStopped=%s\n", __func__
43                          , rtw_is_surprise_removed(padapter) ? "True" : "False"
44                          , rtw_get_hw_init_completed(padapter)
45                          , rtw_is_drv_stopped(padapter) ? "True" : "False");
46                 goto exit_fw_ps_state;
47         }
48         rtw_hal_set_hwreg(padapter, HW_VAR_SET_REQ_FW_PS, (u8 *)&dont_care);
49         {
50                 /* 4. if 0x88[7]=1, driver set cmd to leave LPS/IPS. */
51                 /* Else, hw will keep in active mode. */
52                 /* debug info: */
53                 /* 0x88[7] = 32kpermission, */
54                 /* 0x88[6:0] = current_ps_state */
55                 /* 0x89[7:0] = last_rpwm */
56
57                 rtw_hal_get_hwreg(padapter, HW_VAR_FW_PS_STATE, (u8 *)&fw_ps_state);
58
59                 if ((fw_ps_state & 0x80) == 0)
60                         ret = _SUCCESS;
61                 else {
62                         pdbgpriv->dbg_poll_fail_cnt++;
63                         RTW_INFO("%s: fw_ps_state=%04x\n", __FUNCTION__, fw_ps_state);
64                 }
65         }
66
67
68 exit_fw_ps_state:
69         _exit_pwrlock(&pwrpriv->check_32k_lock);
70         return ret;
71 }
72
73 #ifdef CONFIG_IPS
74 void _ips_enter(_adapter *padapter)
75 {
76         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
77
78         pwrpriv->bips_processing = _TRUE;
79
80         /* syn ips_mode with request */
81         pwrpriv->ips_mode = pwrpriv->ips_mode_req;
82
83         pwrpriv->ips_enter_cnts++;
84         RTW_INFO("==>ips_enter cnts:%d\n", pwrpriv->ips_enter_cnts);
85
86         if (rf_off == pwrpriv->change_rfpwrstate) {
87                 pwrpriv->bpower_saving = _TRUE;
88                 RTW_PRINT("nolinked power save enter\n");
89
90                 if (pwrpriv->ips_mode == IPS_LEVEL_2)
91                         pwrpriv->bkeepfwalive = _TRUE;
92
93                 rtw_ips_pwr_down(padapter);
94                 pwrpriv->rf_pwrstate = rf_off;
95         }
96         pwrpriv->bips_processing = _FALSE;
97
98 }
99
100 void ips_enter(_adapter *padapter)
101 {
102         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
103
104
105 #ifdef CONFIG_BT_COEXIST
106         rtw_btcoex_IpsNotify(padapter, pwrpriv->ips_mode_req);
107 #endif /* CONFIG_BT_COEXIST */
108
109         _enter_pwrlock(&pwrpriv->lock);
110         _ips_enter(padapter);
111         _exit_pwrlock(&pwrpriv->lock);
112 }
113
114 int _ips_leave(_adapter *padapter)
115 {
116         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
117         int result = _SUCCESS;
118
119         if ((pwrpriv->rf_pwrstate == rf_off) && (!pwrpriv->bips_processing)) {
120                 pwrpriv->bips_processing = _TRUE;
121                 pwrpriv->change_rfpwrstate = rf_on;
122                 pwrpriv->ips_leave_cnts++;
123                 RTW_INFO("==>ips_leave cnts:%d\n", pwrpriv->ips_leave_cnts);
124
125                 result = rtw_ips_pwr_up(padapter);
126                 if (result == _SUCCESS)
127                         pwrpriv->rf_pwrstate = rf_on;
128                 RTW_PRINT("nolinked power save leave\n");
129
130                 RTW_INFO("==> ips_leave.....LED(0x%08x)...\n", rtw_read32(padapter, 0x4c));
131                 pwrpriv->bips_processing = _FALSE;
132
133                 pwrpriv->bkeepfwalive = _FALSE;
134                 pwrpriv->bpower_saving = _FALSE;
135         }
136
137         return result;
138 }
139
140 int ips_leave(_adapter *padapter)
141 {
142         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
143         struct dvobj_priv *psdpriv = padapter->dvobj;
144         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
145         int ret;
146
147         if (!is_primary_adapter(padapter))
148                 return _SUCCESS;
149
150         _enter_pwrlock(&pwrpriv->lock);
151         ret = _ips_leave(padapter);
152 #ifdef DBG_CHECK_FW_PS_STATE
153         if (rtw_fw_ps_state(padapter) == _FAIL) {
154                 RTW_INFO("ips leave doesn't leave 32k\n");
155                 pdbgpriv->dbg_leave_ips_fail_cnt++;
156         }
157 #endif /* DBG_CHECK_FW_PS_STATE */
158         _exit_pwrlock(&pwrpriv->lock);
159
160         if (_SUCCESS == ret)
161                 ODM_DMReset(&GET_HAL_DATA(padapter)->odmpriv);
162
163 #ifdef CONFIG_BT_COEXIST
164         if (_SUCCESS == ret)
165                 rtw_btcoex_IpsNotify(padapter, IPS_NONE);
166 #endif /* CONFIG_BT_COEXIST */
167
168         return ret;
169 }
170 #endif /* CONFIG_IPS */
171
172 #ifdef CONFIG_AUTOSUSPEND
173         extern void autosuspend_enter(_adapter *padapter);
174         extern int autoresume_enter(_adapter *padapter);
175 #endif
176
177 #ifdef SUPPORT_HW_RFOFF_DETECTED
178         int rtw_hw_suspend(_adapter *padapter);
179         int rtw_hw_resume(_adapter *padapter);
180 #endif
181
182 bool rtw_pwr_unassociated_idle(_adapter *adapter)
183 {
184         u8 i;
185         _adapter *iface;
186         struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
187         struct xmit_priv *pxmit_priv = &adapter->xmitpriv;
188         struct mlme_priv *pmlmepriv;
189 #ifdef CONFIG_P2P
190         struct wifidirect_info  *pwdinfo;
191 #ifdef CONFIG_IOCTL_CFG80211
192         struct cfg80211_wifidirect_info *pcfg80211_wdinfo;
193 #endif
194 #endif
195
196         bool ret = _FALSE;
197
198         if (adapter_to_pwrctl(adapter)->bpower_saving == _TRUE) {
199                 /* RTW_INFO("%s: already in LPS or IPS mode\n", __func__); */
200                 goto exit;
201         }
202
203         if (adapter_to_pwrctl(adapter)->ips_deny_time >= rtw_get_current_time()) {
204                 /* RTW_INFO("%s ips_deny_time\n", __func__); */
205                 goto exit;
206         }
207
208         for (i = 0; i < dvobj->iface_nums; i++) {
209                 iface = dvobj->padapters[i];
210                 if ((iface) && rtw_is_adapter_up(iface)) {
211                         pmlmepriv = &(iface->mlmepriv);
212 #ifdef CONFIG_P2P
213                         pwdinfo = &(iface->wdinfo);
214 #ifdef CONFIG_IOCTL_CFG80211
215                         pcfg80211_wdinfo = &iface->cfg80211_wdinfo;
216 #endif
217 #endif
218                         if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE | WIFI_SITE_MONITOR)
219                             || check_fwstate(pmlmepriv, WIFI_UNDER_LINKING | WIFI_UNDER_WPS)
220                             || check_fwstate(pmlmepriv, WIFI_AP_STATE)
221                             || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE | WIFI_ADHOC_STATE)
222 #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211)
223                             || rtw_cfg80211_get_is_roch(iface) == _TRUE
224 #elif defined(CONFIG_P2P)
225                             || rtw_p2p_chk_state(pwdinfo, P2P_STATE_IDLE)
226                             || rtw_p2p_chk_state(pwdinfo, P2P_STATE_LISTEN)
227 #endif
228 #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211)
229                             || rtw_get_passing_time_ms(pcfg80211_wdinfo->last_ro_ch_time) < 3000
230 #endif
231                            )
232                                 goto exit;
233
234                 }
235         }
236
237 #if (MP_DRIVER == 1)
238         if (adapter->registrypriv.mp_mode == 1)
239                 goto exit;
240 #endif
241
242 #ifdef CONFIG_INTEL_PROXIM
243         if (adapter->proximity.proxim_on == _TRUE)
244                 return;
245 #endif
246
247         if (pxmit_priv->free_xmitbuf_cnt != NR_XMITBUFF ||
248             pxmit_priv->free_xmit_extbuf_cnt != NR_XMIT_EXTBUFF) {
249                 RTW_PRINT("There are some pkts to transmit\n");
250                 RTW_PRINT("free_xmitbuf_cnt: %d, free_xmit_extbuf_cnt: %d\n",
251                         pxmit_priv->free_xmitbuf_cnt, pxmit_priv->free_xmit_extbuf_cnt);
252                 goto exit;
253         }
254
255         ret = _TRUE;
256
257 exit:
258         return ret;
259 }
260
261
262 /*
263  * ATTENTION:
264  *      rtw_ps_processor() doesn't handle LPS.
265  */
266 void rtw_ps_processor(_adapter *padapter)
267 {
268 #ifdef CONFIG_P2P
269         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
270 #endif /* CONFIG_P2P */
271         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
272         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
273         struct dvobj_priv *psdpriv = padapter->dvobj;
274         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
275 #ifdef SUPPORT_HW_RFOFF_DETECTED
276         rt_rf_power_state rfpwrstate;
277 #endif /* SUPPORT_HW_RFOFF_DETECTED */
278         u32 ps_deny = 0;
279
280         _enter_pwrlock(&adapter_to_pwrctl(padapter)->lock);
281         ps_deny = rtw_ps_deny_get(padapter);
282         _exit_pwrlock(&adapter_to_pwrctl(padapter)->lock);
283         if (ps_deny != 0) {
284                 RTW_INFO(FUNC_ADPT_FMT ": ps_deny=0x%08X, skip power save!\n",
285                          FUNC_ADPT_ARG(padapter), ps_deny);
286                 goto exit;
287         }
288
289         if (pwrpriv->bInSuspend == _TRUE) { /* system suspend or autosuspend */
290                 pdbgpriv->dbg_ps_insuspend_cnt++;
291                 RTW_INFO("%s, pwrpriv->bInSuspend == _TRUE ignore this process\n", __FUNCTION__);
292                 return;
293         }
294
295         pwrpriv->ps_processing = _TRUE;
296
297 #ifdef SUPPORT_HW_RFOFF_DETECTED
298         if (pwrpriv->bips_processing == _TRUE)
299                 goto exit;
300
301         /* RTW_INFO("==> fw report state(0x%x)\n",rtw_read8(padapter,0x1ca));    */
302         if (pwrpriv->bHWPwrPindetect) {
303 #ifdef CONFIG_AUTOSUSPEND
304                 if (padapter->registrypriv.usbss_enable) {
305                         if (pwrpriv->rf_pwrstate == rf_on) {
306                                 if (padapter->net_closed == _TRUE)
307                                         pwrpriv->ps_flag = _TRUE;
308
309                                 rfpwrstate = RfOnOffDetect(padapter);
310                                 RTW_INFO("@@@@- #1  %s==> rfstate:%s\n", __FUNCTION__, (rfpwrstate == rf_on) ? "rf_on" : "rf_off");
311                                 if (rfpwrstate != pwrpriv->rf_pwrstate) {
312                                         if (rfpwrstate == rf_off) {
313                                                 pwrpriv->change_rfpwrstate = rf_off;
314
315                                                 pwrpriv->bkeepfwalive = _TRUE;
316                                                 pwrpriv->brfoffbyhw = _TRUE;
317
318                                                 autosuspend_enter(padapter);
319                                         }
320                                 }
321                         }
322                 } else
323 #endif /* CONFIG_AUTOSUSPEND */
324                 {
325                         rfpwrstate = RfOnOffDetect(padapter);
326                         RTW_INFO("@@@@- #2  %s==> rfstate:%s\n", __FUNCTION__, (rfpwrstate == rf_on) ? "rf_on" : "rf_off");
327
328                         if (rfpwrstate != pwrpriv->rf_pwrstate) {
329                                 if (rfpwrstate == rf_off) {
330                                         pwrpriv->change_rfpwrstate = rf_off;
331                                         pwrpriv->brfoffbyhw = _TRUE;
332                                         rtw_hw_suspend(padapter);
333                                 } else {
334                                         pwrpriv->change_rfpwrstate = rf_on;
335                                         rtw_hw_resume(padapter);
336                                 }
337                                 RTW_INFO("current rf_pwrstate(%s)\n", (pwrpriv->rf_pwrstate == rf_off) ? "rf_off" : "rf_on");
338                         }
339                 }
340                 pwrpriv->pwr_state_check_cnts++;
341         }
342 #endif /* SUPPORT_HW_RFOFF_DETECTED */
343
344         if (pwrpriv->ips_mode_req == IPS_NONE)
345                 goto exit;
346
347         if (rtw_pwr_unassociated_idle(padapter) == _FALSE)
348                 goto exit;
349
350         if ((pwrpriv->rf_pwrstate == rf_on) && ((pwrpriv->pwr_state_check_cnts % 4) == 0)) {
351                 RTW_INFO("==>%s .fw_state(%x)\n", __FUNCTION__, get_fwstate(pmlmepriv));
352 #if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
353 #else
354                 pwrpriv->change_rfpwrstate = rf_off;
355 #endif
356 #ifdef CONFIG_AUTOSUSPEND
357                 if (padapter->registrypriv.usbss_enable) {
358                         if (pwrpriv->bHWPwrPindetect)
359                                 pwrpriv->bkeepfwalive = _TRUE;
360
361                         if (padapter->net_closed == _TRUE)
362                                 pwrpriv->ps_flag = _TRUE;
363
364 #if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
365                         if (_TRUE == pwrpriv->bInternalAutoSuspend)
366                                 RTW_INFO("<==%s .pwrpriv->bInternalAutoSuspend)(%x)\n", __FUNCTION__, pwrpriv->bInternalAutoSuspend);
367                         else {
368                                 pwrpriv->change_rfpwrstate = rf_off;
369                                 RTW_INFO("<==%s .pwrpriv->bInternalAutoSuspend)(%x) call autosuspend_enter\n", __FUNCTION__, pwrpriv->bInternalAutoSuspend);
370                                 autosuspend_enter(padapter);
371                         }
372 #else
373                         autosuspend_enter(padapter);
374 #endif  /* if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND) */
375                 } else if (pwrpriv->bHWPwrPindetect) {
376                 } else
377 #endif /* CONFIG_AUTOSUSPEND */
378                 {
379 #if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
380                         pwrpriv->change_rfpwrstate = rf_off;
381 #endif  /* defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND) */
382
383 #ifdef CONFIG_IPS
384                         ips_enter(padapter);
385 #endif
386                 }
387         }
388 exit:
389 #ifndef CONFIG_IPS_CHECK_IN_WD
390         rtw_set_pwr_state_check_timer(pwrpriv);
391 #endif
392         pwrpriv->ps_processing = _FALSE;
393         return;
394 }
395
396 void pwr_state_check_handler(RTW_TIMER_HDL_ARGS);
397 void pwr_state_check_handler(RTW_TIMER_HDL_ARGS)
398 {
399         _adapter *padapter = (_adapter *)FunctionContext;
400         rtw_ps_cmd(padapter);
401 }
402
403 #ifdef CONFIG_LPS
404 void    traffic_check_for_leave_lps(PADAPTER padapter, u8 tx, u32 tx_packets)
405 {
406 #ifdef CONFIG_CHECK_LEAVE_LPS
407         static u32 start_time = 0;
408         static u32 xmit_cnt = 0;
409         u8      bLeaveLPS = _FALSE;
410         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
411
412
413
414         if (tx) { /* from tx */
415                 xmit_cnt += tx_packets;
416
417                 if (start_time == 0)
418                         start_time = rtw_get_current_time();
419
420                 if (rtw_get_passing_time_ms(start_time) > 2000) { /* 2 sec == watch dog timer */
421                         if (xmit_cnt > 8) {
422                                 if ((adapter_to_pwrctl(padapter)->bLeisurePs)
423                                     && (adapter_to_pwrctl(padapter)->pwr_mode != PS_MODE_ACTIVE)
424 #ifdef CONFIG_BT_COEXIST
425                                     && (rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
426 #endif
427                                    ) {
428                                         /* RTW_INFO("leave lps via Tx = %d\n", xmit_cnt);                        */
429                                         bLeaveLPS = _TRUE;
430                                 }
431                         }
432
433                         start_time = rtw_get_current_time();
434                         xmit_cnt = 0;
435                 }
436
437         } else { /* from rx path */
438                 if (pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod > 4/*2*/) {
439                         if ((adapter_to_pwrctl(padapter)->bLeisurePs)
440                             && (adapter_to_pwrctl(padapter)->pwr_mode != PS_MODE_ACTIVE)
441 #ifdef CONFIG_BT_COEXIST
442                             && (rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
443 #endif
444                            ) {
445                                 /* RTW_INFO("leave lps via Rx = %d\n", pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod);        */
446                                 bLeaveLPS = _TRUE;
447                         }
448                 }
449         }
450
451         if (bLeaveLPS) {
452                 /* RTW_INFO("leave lps via %s, Tx = %d, Rx = %d\n", tx?"Tx":"Rx", pmlmepriv->LinkDetectInfo.NumTxOkInPeriod,pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod);   */
453                 /* rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, 1); */
454                 rtw_lps_ctrl_wk_cmd(padapter, tx ? LPS_CTRL_TX_TRAFFIC_LEAVE : LPS_CTRL_RX_TRAFFIC_LEAVE, tx ? 0 : 1);
455         }
456 #endif /* CONFIG_CHECK_LEAVE_LPS */
457 }
458
459 /*
460  * Description:
461  *      This function MUST be called under power lock protect
462  *
463  * Parameters
464  *      padapter
465  *      pslv                    power state level, only could be PS_STATE_S0 ~ PS_STATE_S4
466  *
467  */
468 void rtw_set_rpwm(PADAPTER padapter, u8 pslv)
469 {
470         u8      rpwm;
471         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
472 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
473         u8 cpwm_orig;
474 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
475         struct dvobj_priv *psdpriv = padapter->dvobj;
476         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
477
478         pslv = PS_STATE(pslv);
479
480 #ifdef CONFIG_LPS_RPWM_TIMER
481         if (pwrpriv->brpwmtimeout == _TRUE)
482                 RTW_INFO("%s: RPWM timeout, force to set RPWM(0x%02X) again!\n", __FUNCTION__, pslv);
483         else
484 #endif /* CONFIG_LPS_RPWM_TIMER */
485         {
486                 if ((pwrpriv->rpwm == pslv)
487 #ifdef CONFIG_LPS_LCLK
488                     || ((pwrpriv->rpwm >= PS_STATE_S2) && (pslv >= PS_STATE_S2))
489 #endif
490                    ) {
491                         return;
492                 }
493         }
494
495         if (rtw_is_surprise_removed(padapter) ||
496             (!rtw_is_hw_init_completed(padapter))) {
497
498                 pwrpriv->cpwm = PS_STATE_S4;
499
500                 return;
501         }
502
503         if (rtw_is_drv_stopped(padapter)) {
504
505                 if (pslv < PS_STATE_S2) {
506                         return;
507                 }
508         }
509
510         rpwm = pslv | pwrpriv->tog;
511 #ifdef CONFIG_LPS_LCLK
512         /* only when from PS_STATE S0/S1 to S2 and higher needs ACK */
513         if ((pwrpriv->cpwm < PS_STATE_S2) && (pslv >= PS_STATE_S2))
514                 rpwm |= PS_ACK;
515 #endif
516
517         pwrpriv->rpwm = pslv;
518
519 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
520         cpwm_orig = 0;
521         if (rpwm & PS_ACK)
522                 rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_orig);
523 #endif
524
525 #if defined(CONFIG_LPS_RPWM_TIMER) && !defined(CONFIG_DETECT_CPWM_BY_POLLING)
526         if (rpwm & PS_ACK)
527                 _set_timer(&pwrpriv->pwr_rpwm_timer, LPS_RPWM_WAIT_MS);
528 #endif /* CONFIG_LPS_RPWM_TIMER & !CONFIG_DETECT_CPWM_BY_POLLING */
529         rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&rpwm));
530
531         pwrpriv->tog += 0x80;
532
533 #ifdef CONFIG_LPS_LCLK
534         /* No LPS 32K, No Ack */
535         if (rpwm & PS_ACK) {
536 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
537                 u32 start_time;
538                 u8 cpwm_now;
539                 u8 poll_cnt = 0;
540
541                 start_time = rtw_get_current_time();
542
543                 /* polling cpwm */
544                 do {
545                         rtw_msleep_os(1);
546                         poll_cnt++;
547                         cpwm_now = 0;
548                         rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_now);
549                         if ((cpwm_orig ^ cpwm_now) & 0x80) {
550                                 pwrpriv->cpwm = PS_STATE_S4;
551                                 pwrpriv->cpwm_tog = cpwm_now & PS_TOGGLE;
552 #ifdef DBG_CHECK_FW_PS_STATE
553                                 RTW_INFO("%s: polling cpwm OK! poll_cnt=%d, cpwm_orig=%02x, cpwm_now=%02x , 0x100=0x%x\n"
554                                         , __FUNCTION__, poll_cnt, cpwm_orig, cpwm_now, rtw_read8(padapter, REG_CR));
555                                 if (rtw_fw_ps_state(padapter) == _FAIL) {
556                                         RTW_INFO("leave 32k but fw state in 32k\n");
557                                         pdbgpriv->dbg_rpwm_toogle_cnt++;
558                                 }
559 #endif /* DBG_CHECK_FW_PS_STATE */
560                                 break;
561                         }
562
563                         if (rtw_get_passing_time_ms(start_time) > LPS_RPWM_WAIT_MS) {
564                                 RTW_INFO("%s: polling cpwm timeout! poll_cnt=%d, cpwm_orig=%02x, cpwm_now=%02x\n", __FUNCTION__, poll_cnt, cpwm_orig, cpwm_now);
565 #ifdef DBG_CHECK_FW_PS_STATE
566                                 if (rtw_fw_ps_state(padapter) == _FAIL) {
567                                         RTW_INFO("rpwm timeout and fw ps state in 32k\n");
568                                         pdbgpriv->dbg_rpwm_timeout_fail_cnt++;
569                                 }
570 #endif /* DBG_CHECK_FW_PS_STATE */
571 #ifdef CONFIG_LPS_RPWM_TIMER
572                                 _set_timer(&pwrpriv->pwr_rpwm_timer, 1);
573 #endif /* CONFIG_LPS_RPWM_TIMER */
574                                 break;
575                         }
576                 } while (1);
577 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
578         } else
579 #endif /* CONFIG_LPS_LCLK */
580         {
581                 pwrpriv->cpwm = pslv;
582         }
583
584 }
585
586 u8 PS_RDY_CHECK(_adapter *padapter)
587 {
588         u32 curr_time, delta_time;
589         struct pwrctrl_priv     *pwrpriv = adapter_to_pwrctl(padapter);
590         struct mlme_priv        *pmlmepriv = &(padapter->mlmepriv);
591 #ifdef CONFIG_P2P
592         struct wifidirect_info *pwdinfo = &(padapter->wdinfo);
593 #ifdef CONFIG_IOCTL_CFG80211
594         struct cfg80211_wifidirect_info *pcfg80211_wdinfo = &padapter->cfg80211_wdinfo;
595 #endif /* CONFIG_IOCTL_CFG80211 */
596 #endif /* CONFIG_P2P */
597
598 #if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN)
599         if (_TRUE == pwrpriv->bInSuspend && pwrpriv->wowlan_mode)
600                 return _TRUE;
601         else if (_TRUE == pwrpriv->bInSuspend && pwrpriv->wowlan_ap_mode)
602                 return _TRUE;
603         else if (_TRUE == pwrpriv->bInSuspend)
604                 return _FALSE;
605 #else
606         if (_TRUE == pwrpriv->bInSuspend)
607                 return _FALSE;
608 #endif
609
610         curr_time = rtw_get_current_time();
611
612         delta_time = curr_time - pwrpriv->DelayLPSLastTimeStamp;
613
614         if (delta_time < LPS_DELAY_TIME)
615                 return _FALSE;
616
617         if (check_fwstate(pmlmepriv, WIFI_SITE_MONITOR)
618             || check_fwstate(pmlmepriv, WIFI_UNDER_LINKING | WIFI_UNDER_WPS)
619             || check_fwstate(pmlmepriv, WIFI_AP_STATE)
620             || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE | WIFI_ADHOC_STATE)
621 #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211)
622             || rtw_cfg80211_get_is_roch(padapter) == _TRUE
623 #endif
624             || rtw_is_scan_deny(padapter)
625 #ifdef CONFIG_TDLS
626             /* TDLS link is established. */
627             || (padapter->tdlsinfo.link_established == _TRUE)
628 #endif /* CONFIG_TDLS            */
629            )
630                 return _FALSE;
631
632         if ((padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) && (padapter->securitypriv.binstallGrpkey == _FALSE)) {
633                 RTW_INFO("Group handshake still in progress !!!\n");
634                 return _FALSE;
635         }
636
637 #ifdef CONFIG_IOCTL_CFG80211
638         if (!rtw_cfg80211_pwr_mgmt(padapter))
639                 return _FALSE;
640 #endif
641
642         return _TRUE;
643 }
644
645 #if defined(CONFIG_FWLPS_IN_IPS)
646 void rtw_set_fw_in_ips_mode(PADAPTER padapter, u8 enable)
647 {
648         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
649         int cnt = 0;
650         u32 start_time;
651         u8 val8 = 0;
652         u8 cpwm_orig = 0, cpwm_now = 0;
653         u8 parm[H2C_INACTIVE_PS_LEN] = {0};
654
655         if (padapter->netif_up == _FALSE) {
656                 RTW_INFO("%s: ERROR, netif is down\n", __func__);
657                 return;
658         }
659
660         /* u8 cmd_param; */ /* BIT0:enable, BIT1:NoConnect32k */
661         if (enable) {
662 #ifdef CONFIG_BT_COEXIST
663                 rtw_btcoex_IpsNotify(padapter, pwrpriv->ips_mode_req);
664 #endif
665                 /* Enter IPS */
666                 RTW_INFO("%s: issue H2C to FW when entering IPS\n", __func__);
667
668                 parm[0] = 0x1;/* suggest by Isaac.Hsu*/
669 #ifdef CONFIG_PNO_SUPPORT
670                 if (pwrpriv->pno_inited) {
671                         parm[1] = pwrpriv->pnlo_info->fast_scan_iterations;
672                         parm[2] = pwrpriv->pnlo_info->slow_scan_period;
673                 }
674 #endif
675
676                 rtw_hal_fill_h2c_cmd(padapter, /* H2C_FWLPS_IN_IPS_, */
677                                      H2C_INACTIVE_PS_,
678                                      H2C_INACTIVE_PS_LEN, parm);
679                 /* poll 0x1cc to make sure H2C command already finished by FW; MAC_0x1cc=0 means H2C done by FW. */
680                 do {
681                         val8 = rtw_read8(padapter, REG_HMETFR);
682                         cnt++;
683                         RTW_INFO("%s  polling REG_HMETFR=0x%x, cnt=%d\n",
684                                  __func__, val8, cnt);
685                         rtw_mdelay_os(10);
686                 } while (cnt < 100 && (val8 != 0));
687
688 #ifdef CONFIG_LPS_LCLK
689                 /* H2C done, enter 32k */
690                 if (val8 == 0) {
691                         /* ser rpwm to enter 32k */
692                         val8 = rtw_read8(padapter, SDIO_LOCAL_BASE | SDIO_REG_HRPWM1);
693                         RTW_INFO("%s: read rpwm=%02x\n", __FUNCTION__, val8);
694                         val8 += 0x80;
695                         val8 |= BIT(0);
696                         rtw_write8(padapter, SDIO_LOCAL_BASE | SDIO_REG_HRPWM1, val8);
697                         RTW_INFO("%s: write rpwm=%02x\n", __FUNCTION__, val8);
698                         adapter_to_pwrctl(padapter)->tog = (val8 + 0x80) & 0x80;
699                         cnt = val8 = 0;
700                         if (parm[1] == 0 || parm[2] == 0) {
701                                 do {
702                                         val8 = rtw_read8(padapter, REG_CR);
703                                         cnt++;
704                                         RTW_INFO("%s  polling 0x100=0x%x, cnt=%d\n",
705                                                  __func__, val8, cnt);
706                                         RTW_INFO("%s 0x08:%02x, 0x03:%02x\n",
707                                                  __func__,
708                                                  rtw_read8(padapter, 0x08),
709                                                  rtw_read8(padapter, 0x03));
710                                         rtw_mdelay_os(10);
711                                 } while (cnt < 20 && (val8 != 0xEA));
712                         }
713                 }
714 #endif
715         } else {
716                 /* Leave IPS */
717                 RTW_INFO("%s: Leaving IPS in FWLPS state\n", __func__);
718
719 #ifdef CONFIG_LPS_LCLK
720                 /* for polling cpwm */
721                 cpwm_orig = 0;
722                 rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_orig);
723
724                 /* ser rpwm */
725                 val8 = rtw_read8(padapter, SDIO_LOCAL_BASE | SDIO_REG_HRPWM1);
726                 val8 &= 0x80;
727                 val8 += 0x80;
728                 val8 |= BIT(6);
729                 rtw_write8(padapter, SDIO_LOCAL_BASE | SDIO_REG_HRPWM1, val8);
730                 RTW_INFO("%s: write rpwm=%02x\n", __FUNCTION__, val8);
731                 adapter_to_pwrctl(padapter)->tog = (val8 + 0x80) & 0x80;
732
733                 /* do polling cpwm */
734                 start_time = rtw_get_current_time();
735                 do {
736
737                         rtw_mdelay_os(1);
738
739                         rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_now);
740                         if ((cpwm_orig ^ cpwm_now) & 0x80)
741                                 break;
742
743                         if (rtw_get_passing_time_ms(start_time) > 100) {
744                                 RTW_INFO("%s: polling cpwm timeout when leaving IPS in FWLPS state\n", __FUNCTION__);
745                                 break;
746                         }
747                 } while (1);
748
749 #endif
750                 parm[0] = 0x0;
751                 parm[1] = 0x0;
752                 parm[2] = 0x0;
753                 rtw_hal_fill_h2c_cmd(padapter, H2C_INACTIVE_PS_,
754                                      H2C_INACTIVE_PS_LEN, parm);
755 #ifdef CONFIG_BT_COEXIST
756                 rtw_btcoex_IpsNotify(padapter, IPS_NONE);
757 #endif
758         }
759 }
760 #endif /* CONFIG_PNO_SUPPORT */
761
762 void rtw_set_ps_mode(PADAPTER padapter, u8 ps_mode, u8 smart_ps, u8 bcn_ant_mode, const char *msg)
763 {
764         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
765         struct dvobj_priv *psdpriv = padapter->dvobj;
766         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
767 #ifdef CONFIG_P2P
768         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
769 #endif /* CONFIG_P2P */
770 #ifdef CONFIG_TDLS
771         struct sta_priv *pstapriv = &padapter->stapriv;
772         _irqL irqL;
773         int i, j;
774         _list   *plist, *phead;
775         struct sta_info *ptdls_sta;
776 #endif /* CONFIG_TDLS */
777
778
779
780         if (ps_mode > PM_Card_Disable) {
781                 return;
782         }
783
784         if (pwrpriv->pwr_mode == ps_mode) {
785                 if (PS_MODE_ACTIVE == ps_mode)
786                         return;
787
788 #ifndef CONFIG_BT_COEXIST
789                 if ((pwrpriv->smart_ps == smart_ps) &&
790                     (pwrpriv->bcn_ant_mode == bcn_ant_mode))
791                         return;
792 #endif /* !CONFIG_BT_COEXIST */
793         }
794
795 #ifdef CONFIG_LPS_PG
796         if ((PS_MODE_ACTIVE != ps_mode) && (pwrpriv->blpspg_info_up))
797                 rtw_hal_set_lps_pg_info(padapter);
798 #endif
799
800 #ifdef CONFIG_LPS_LCLK
801         _enter_pwrlock(&pwrpriv->lock);
802 #endif
803
804         /* if(pwrpriv->pwr_mode == PS_MODE_ACTIVE) */
805         if (ps_mode == PS_MODE_ACTIVE) {
806                 if (1
807 #ifdef CONFIG_BT_COEXIST
808                     && (((rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
809 #ifdef CONFIG_P2P_PS
810                          && (pwdinfo->opp_ps == 0)
811 #endif /* CONFIG_P2P_PS */
812                         )
813                         || ((rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
814                             && (rtw_btcoex_IsLpsOn(padapter) == _FALSE))
815                        )
816 #else /* !CONFIG_BT_COEXIST */
817 #ifdef CONFIG_P2P_PS
818                     && (pwdinfo->opp_ps == 0)
819 #endif /* CONFIG_P2P_PS */
820 #endif /* !CONFIG_BT_COEXIST */
821                    ) {
822                         RTW_INFO(FUNC_ADPT_FMT" Leave 802.11 power save - %s\n",
823                                  FUNC_ADPT_ARG(padapter), msg);
824
825                         if (pwrpriv->lps_leave_cnts < UINT_MAX)
826                                 pwrpriv->lps_leave_cnts++;
827                         else
828                                 pwrpriv->lps_leave_cnts = 0;
829 #ifdef CONFIG_TDLS
830                         for (i = 0; i < NUM_STA; i++) {
831                                 phead = &(pstapriv->sta_hash[i]);
832                                 plist = get_next(phead);
833
834                                 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
835                                         ptdls_sta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
836
837                                         if (ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE)
838                                                 issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta->hwaddr, 0, 0, 0);
839                                         plist = get_next(plist);
840                                 }
841                         }
842 #endif /* CONFIG_TDLS */
843
844                         pwrpriv->pwr_mode = ps_mode;
845                         rtw_set_rpwm(padapter, PS_STATE_S4);
846
847 #if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) || defined(CONFIG_P2P_WOWLAN)
848                         if (pwrpriv->wowlan_mode == _TRUE ||
849                             pwrpriv->wowlan_ap_mode == _TRUE ||
850                             pwrpriv->wowlan_p2p_mode == _TRUE) {
851                                 u32 start_time, delay_ms;
852                                 u8 val8;
853                                 delay_ms = 20;
854                                 start_time = rtw_get_current_time();
855                                 do {
856                                         rtw_hal_get_hwreg(padapter, HW_VAR_SYS_CLKR, &val8);
857                                         if (!(val8 & BIT(4))) { /* 0x08 bit4 =1 --> in 32k, bit4 = 0 --> leave 32k */
858                                                 pwrpriv->cpwm = PS_STATE_S4;
859                                                 break;
860                                         }
861                                         if (rtw_get_passing_time_ms(start_time) > delay_ms) {
862                                                 RTW_INFO("%s: Wait for FW 32K leave more than %u ms!!!\n",
863                                                         __FUNCTION__, delay_ms);
864                                                 pdbgpriv->dbg_wow_leave_ps_fail_cnt++;
865                                                 break;
866                                         }
867                                         rtw_usleep_os(100);
868                                 } while (1);
869                         }
870 #endif
871                         rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
872 #ifdef CONFIG_LPS_POFF
873                         rtw_hal_set_hwreg(padapter, HW_VAR_LPS_POFF_SET_MODE,
874                                           (u8 *)(&ps_mode));
875 #endif /*CONFIG_LPS_POFF*/
876
877                         pwrpriv->bFwCurrentInPSMode = _FALSE;
878
879 #ifdef CONFIG_BT_COEXIST
880                         rtw_btcoex_LpsNotify(padapter, ps_mode);
881 #endif /* CONFIG_BT_COEXIST */
882                 }
883         } else {
884                 if ((PS_RDY_CHECK(padapter) && check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE))
885 #ifdef CONFIG_BT_COEXIST
886                     || ((rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
887                         && (rtw_btcoex_IsLpsOn(padapter) == _TRUE))
888 #endif
889 #ifdef CONFIG_P2P_WOWLAN
890                     || (_TRUE == pwrpriv->wowlan_p2p_mode)
891 #endif /* CONFIG_P2P_WOWLAN */
892                    ) {
893                         u8 pslv;
894
895                         RTW_INFO(FUNC_ADPT_FMT" Enter 802.11 power save - %s\n",
896                                  FUNC_ADPT_ARG(padapter), msg);
897
898                         if (pwrpriv->lps_enter_cnts < UINT_MAX)
899                                 pwrpriv->lps_enter_cnts++;
900                         else
901                                 pwrpriv->lps_enter_cnts = 0;
902 #ifdef CONFIG_TDLS
903                         for (i = 0; i < NUM_STA; i++) {
904                                 phead = &(pstapriv->sta_hash[i]);
905                                 plist = get_next(phead);
906
907                                 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
908                                         ptdls_sta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
909
910                                         if (ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE)
911                                                 issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta->hwaddr, 1, 0, 0);
912                                         plist = get_next(plist);
913                                 }
914                         }
915 #endif /* CONFIG_TDLS */
916
917 #ifdef CONFIG_BT_COEXIST
918                         rtw_btcoex_LpsNotify(padapter, ps_mode);
919 #endif /* CONFIG_BT_COEXIST */
920
921 #ifdef CONFIG_LPS_POFF
922                         rtw_hal_set_hwreg(padapter, HW_VAR_LPS_POFF_SET_MODE,
923                                           (u8 *)(&ps_mode));
924 #endif /*CONFIG_LPS_POFF*/
925
926                         pwrpriv->bFwCurrentInPSMode = _TRUE;
927                         pwrpriv->pwr_mode = ps_mode;
928                         pwrpriv->smart_ps = smart_ps;
929                         pwrpriv->bcn_ant_mode = bcn_ant_mode;
930                         rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
931
932 #ifdef CONFIG_P2P_PS
933                         /* Set CTWindow after LPS */
934                         if (pwdinfo->opp_ps == 1)
935                                 p2p_ps_wk_cmd(padapter, P2P_PS_ENABLE, 0);
936 #endif /* CONFIG_P2P_PS */
937
938                         pslv = PS_STATE_S2;
939 #ifdef CONFIG_LPS_LCLK
940                         if (pwrpriv->alives == 0)
941                                 pslv = PS_STATE_S0;
942 #endif /* CONFIG_LPS_LCLK */
943
944 #ifdef CONFIG_BT_COEXIST
945                         if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
946                             && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)) {
947                                 u8 val8;
948
949                                 val8 = rtw_btcoex_LpsVal(padapter);
950                                 if (val8 & BIT(4))
951                                         pslv = PS_STATE_S2;
952
953                         }
954 #endif /* CONFIG_BT_COEXIST */
955
956                         rtw_set_rpwm(padapter, pslv);
957                 }
958         }
959
960 #ifdef CONFIG_LPS_LCLK
961         _exit_pwrlock(&pwrpriv->lock);
962 #endif
963
964 }
965
966 /*
967  * Return:
968  *      0:      Leave OK
969  *      -1:     Timeout
970  *      -2:     Other error
971  */
972 s32 LPS_RF_ON_check(PADAPTER padapter, u32 delay_ms)
973 {
974         u32 start_time;
975         u8 bAwake = _FALSE;
976         s32 err = 0;
977
978
979         start_time = rtw_get_current_time();
980         while (1) {
981                 rtw_hal_get_hwreg(padapter, HW_VAR_FWLPS_RF_ON, &bAwake);
982                 if (_TRUE == bAwake)
983                         break;
984
985                 if (rtw_is_surprise_removed(padapter)) {
986                         err = -2;
987                         RTW_INFO("%s: device surprise removed!!\n", __FUNCTION__);
988                         break;
989                 }
990
991                 if (rtw_get_passing_time_ms(start_time) > delay_ms) {
992                         err = -1;
993                         RTW_INFO("%s: Wait for FW LPS leave more than %u ms!!!\n", __FUNCTION__, delay_ms);
994                         break;
995                 }
996                 rtw_usleep_os(100);
997         }
998
999         return err;
1000 }
1001
1002 /*
1003  *      Description:
1004  *              Enter the leisure power save mode.
1005  *   */
1006 void LPS_Enter(PADAPTER padapter, const char *msg)
1007 {
1008         struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1009         struct pwrctrl_priv     *pwrpriv = dvobj_to_pwrctl(dvobj);
1010         struct mlme_priv        *pmlmepriv = &(padapter->mlmepriv);
1011         int n_assoc_iface = 0;
1012         int i;
1013         char buf[32] = {0};
1014
1015
1016         /*      RTW_INFO("+LeisurePSEnter\n"); */
1017         if (_FALSE == padapter->bFWReady)
1018                 return;
1019
1020 #ifdef CONFIG_BT_COEXIST
1021         if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1022                 return;
1023 #endif
1024
1025         /* Skip lps enter request if number of assocated adapters is not 1 */
1026         for (i = 0; i < dvobj->iface_nums; i++) {
1027                 if (check_fwstate(&(dvobj->padapters[i]->mlmepriv), WIFI_ASOC_STATE))
1028                         n_assoc_iface++;
1029         }
1030         if (n_assoc_iface != 1)
1031                 return;
1032
1033         /* Skip lps enter request for adapter not port0 */
1034         if (get_hw_port(padapter) != HW_PORT0)
1035                 return;
1036
1037         for (i = 0; i < dvobj->iface_nums; i++) {
1038                 if (PS_RDY_CHECK(dvobj->padapters[i]) == _FALSE)
1039                         return;
1040         }
1041
1042 #ifdef CONFIG_P2P_PS
1043         if (padapter->wdinfo.p2p_ps_mode == P2P_PS_NOA) {
1044                 return;/* supporting p2p client ps NOA via H2C_8723B_P2P_PS_OFFLOAD */
1045         }
1046 #endif /* CONFIG_P2P_PS */
1047
1048         if (pwrpriv->bLeisurePs) {
1049                 /* Idle for a while if we connect to AP a while ago. */
1050                 if (pwrpriv->LpsIdleCount >= 2) { /* 4 Sec */
1051                         if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) {
1052                                 sprintf(buf, "WIFI-%s", msg);
1053                                 pwrpriv->bpower_saving = _TRUE;
1054                                 rtw_set_ps_mode(padapter, pwrpriv->power_mgnt, padapter->registrypriv.smart_ps, 0, buf);
1055                         }
1056                 } else
1057                         pwrpriv->LpsIdleCount++;
1058         }
1059
1060         /*      RTW_INFO("-LeisurePSEnter\n"); */
1061
1062 }
1063
1064 /*
1065  *      Description:
1066  *              Leave the leisure power save mode.
1067  *   */
1068 void LPS_Leave(PADAPTER padapter, const char *msg)
1069 {
1070 #define LPS_LEAVE_TIMEOUT_MS 100
1071
1072         struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1073         struct pwrctrl_priv     *pwrpriv = dvobj_to_pwrctl(dvobj);
1074         u32 start_time;
1075         u8 bAwake = _FALSE;
1076         char buf[32] = {0};
1077         struct debug_priv *pdbgpriv = &dvobj->drv_dbg;
1078
1079
1080         /*      RTW_INFO("+LeisurePSLeave\n"); */
1081
1082 #ifdef CONFIG_BT_COEXIST
1083         if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1084                 return;
1085 #endif
1086
1087         if (pwrpriv->bLeisurePs) {
1088                 if (pwrpriv->pwr_mode != PS_MODE_ACTIVE) {
1089                         sprintf(buf, "WIFI-%s", msg);
1090                         rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0, buf);
1091
1092                         if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
1093                                 LPS_RF_ON_check(padapter, LPS_LEAVE_TIMEOUT_MS);
1094                 }
1095         }
1096
1097         pwrpriv->bpower_saving = _FALSE;
1098 #ifdef DBG_CHECK_FW_PS_STATE
1099         if (rtw_fw_ps_state(padapter) == _FAIL) {
1100                 RTW_INFO("leave lps, fw in 32k\n");
1101                 pdbgpriv->dbg_leave_lps_fail_cnt++;
1102         }
1103 #endif /* DBG_CHECK_FW_PS_STATE
1104  *      RTW_INFO("-LeisurePSLeave\n"); */
1105
1106 }
1107 #endif
1108
1109 void LeaveAllPowerSaveModeDirect(PADAPTER Adapter)
1110 {
1111         PADAPTER pri_padapter = GET_PRIMARY_ADAPTER(Adapter);
1112         struct mlme_priv        *pmlmepriv = &(Adapter->mlmepriv);
1113         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(Adapter);
1114         struct dvobj_priv *psdpriv = Adapter->dvobj;
1115         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
1116 #ifndef CONFIG_DETECT_CPWM_BY_POLLING
1117         u8 cpwm_orig, cpwm_now;
1118         u32 start_time;
1119 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1120
1121
1122         RTW_INFO("%s.....\n", __FUNCTION__);
1123
1124         if (rtw_is_surprise_removed(Adapter)) {
1125                 RTW_INFO(FUNC_ADPT_FMT ": bSurpriseRemoved=_TRUE Skip!\n", FUNC_ADPT_ARG(Adapter));
1126                 return;
1127         }
1128
1129         if (rtw_mi_check_status(Adapter, MI_LINKED)) { /*connect*/
1130
1131                 if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) {
1132                         RTW_INFO("%s: Driver Already Leave LPS\n", __FUNCTION__);
1133                         return;
1134                 }
1135
1136 #ifdef CONFIG_LPS_LCLK
1137                 _enter_pwrlock(&pwrpriv->lock);
1138
1139 #ifndef CONFIG_DETECT_CPWM_BY_POLLING
1140                 cpwm_orig = 0;
1141                 rtw_hal_get_hwreg(Adapter, HW_VAR_CPWM, &cpwm_orig);
1142 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1143                 rtw_set_rpwm(Adapter, PS_STATE_S4);
1144
1145 #ifndef CONFIG_DETECT_CPWM_BY_POLLING
1146
1147                 start_time = rtw_get_current_time();
1148
1149                 /* polling cpwm */
1150                 do {
1151                         rtw_mdelay_os(1);
1152
1153                         rtw_hal_get_hwreg(Adapter, HW_VAR_CPWM, &cpwm_now);
1154                         if ((cpwm_orig ^ cpwm_now) & 0x80) {
1155                                 pwrpriv->cpwm = PS_STATE_S4;
1156                                 pwrpriv->cpwm_tog = cpwm_now & PS_TOGGLE;
1157 #ifdef DBG_CHECK_FW_PS_STATE
1158                                 RTW_INFO("%s: polling cpwm OK! cpwm_orig=%02x, cpwm_now=%02x, 0x100=0x%x\n"
1159                                         , __FUNCTION__, cpwm_orig, cpwm_now, rtw_read8(Adapter, REG_CR));
1160                                 if (rtw_fw_ps_state(Adapter) == _FAIL) {
1161                                         RTW_INFO("%s: leave 32k but fw state in 32k\n", __FUNCTION__);
1162                                         pdbgpriv->dbg_rpwm_toogle_cnt++;
1163                                 }
1164 #endif /* DBG_CHECK_FW_PS_STATE */
1165                                 break;
1166                         }
1167
1168                         if (rtw_get_passing_time_ms(start_time) > LPS_RPWM_WAIT_MS) {
1169                                 RTW_INFO("%s: polling cpwm timeout! cpwm_orig=%02x, cpwm_now=%02x\n", __FUNCTION__, cpwm_orig, cpwm_now);
1170 #ifdef DBG_CHECK_FW_PS_STATE
1171                                 if (rtw_fw_ps_state(Adapter) == _FAIL) {
1172                                         RTW_INFO("rpwm timeout and fw ps state in 32k\n");
1173                                         pdbgpriv->dbg_rpwm_timeout_fail_cnt++;
1174                                 }
1175 #endif /* DBG_CHECK_FW_PS_STATE */
1176                                 break;
1177                         }
1178                 } while (1);
1179 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1180
1181                 _exit_pwrlock(&pwrpriv->lock);
1182 #endif
1183
1184 #ifdef CONFIG_P2P_PS
1185                 p2p_ps_wk_cmd(pri_padapter, P2P_PS_DISABLE, 0);
1186 #endif /* CONFIG_P2P_PS */
1187
1188 #ifdef CONFIG_LPS
1189                 rtw_lps_ctrl_wk_cmd(pri_padapter, LPS_CTRL_LEAVE, 0);
1190 #endif
1191         } else {
1192                 if (pwrpriv->rf_pwrstate == rf_off) {
1193 #ifdef CONFIG_AUTOSUSPEND
1194                         if (Adapter->registrypriv.usbss_enable) {
1195 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
1196                                 usb_disable_autosuspend(adapter_to_dvobj(Adapter)->pusbdev);
1197 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22) && LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 34))
1198                                 adapter_to_dvobj(Adapter)->pusbdev->autosuspend_disabled = Adapter->bDisableAutosuspend;/* autosuspend disabled by the user */
1199 #endif
1200                         } else
1201 #endif
1202                         {
1203 #if defined(CONFIG_FWLPS_IN_IPS) || defined(CONFIG_SWLPS_IN_IPS)
1204 #ifdef CONFIG_IPS
1205                                 if (_FALSE == ips_leave(pri_padapter))
1206                                         RTW_INFO("======> ips_leave fail.............\n");
1207 #endif
1208 #endif /* CONFIG_SWLPS_IN_IPS || (CONFIG_PLATFORM_SPRD && CONFIG_RTL8188E) */
1209                         }
1210                 }
1211         }
1212
1213 }
1214
1215 /*
1216  * Description: Leave all power save mode: LPS, FwLPS, IPS if needed.
1217  * Move code to function by tynli. 2010.03.26.
1218  *   */
1219 void LeaveAllPowerSaveMode(IN PADAPTER Adapter)
1220 {
1221         struct dvobj_priv *dvobj = adapter_to_dvobj(Adapter);
1222         struct mlme_priv        *pmlmepriv = &(Adapter->mlmepriv);
1223         u8      enqueue = 0;
1224         int n_assoc_iface = 0;
1225         int i;
1226
1227
1228         /* RTW_INFO("%s.....\n",__FUNCTION__); */
1229
1230         if (_FALSE == Adapter->bup) {
1231                 RTW_INFO(FUNC_ADPT_FMT ": bup=%d Skip!\n",
1232                          FUNC_ADPT_ARG(Adapter), Adapter->bup);
1233                 return;
1234         }
1235
1236         if (rtw_is_surprise_removed(Adapter)) {
1237                 RTW_INFO(FUNC_ADPT_FMT ": bSurpriseRemoved=_TRUE Skip!\n", FUNC_ADPT_ARG(Adapter));
1238                 return;
1239         }
1240
1241         for (i = 0; i < dvobj->iface_nums; i++) {
1242                 if (check_fwstate(&(dvobj->padapters[i]->mlmepriv), WIFI_ASOC_STATE))
1243                         n_assoc_iface++;
1244         }
1245
1246         if (n_assoc_iface) {
1247                 /* connect */
1248 #ifdef CONFIG_LPS_LCLK
1249                 enqueue = 1;
1250 #endif
1251
1252 #ifdef CONFIG_P2P_PS
1253                 p2p_ps_wk_cmd(Adapter, P2P_PS_DISABLE, enqueue);
1254 #endif /* CONFIG_P2P_PS */
1255
1256 #ifdef CONFIG_LPS
1257                 rtw_lps_ctrl_wk_cmd(Adapter, LPS_CTRL_LEAVE, enqueue);
1258 #endif
1259
1260 #ifdef CONFIG_LPS_LCLK
1261                 LPS_Leave_check(Adapter);
1262 #endif
1263         } else {
1264                 if (adapter_to_pwrctl(Adapter)->rf_pwrstate == rf_off) {
1265 #ifdef CONFIG_AUTOSUSPEND
1266                         if (Adapter->registrypriv.usbss_enable) {
1267 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
1268                                 usb_disable_autosuspend(adapter_to_dvobj(Adapter)->pusbdev);
1269 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22) && LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 34))
1270                                 adapter_to_dvobj(Adapter)->pusbdev->autosuspend_disabled = Adapter->bDisableAutosuspend;/* autosuspend disabled by the user */
1271 #endif
1272                         } else
1273 #endif
1274                         {
1275 #if defined(CONFIG_FWLPS_IN_IPS) || defined(CONFIG_SWLPS_IN_IPS) || (defined(CONFIG_PLATFORM_SPRD) && defined(CONFIG_RTL8188E))
1276 #ifdef CONFIG_IPS
1277                                 if (_FALSE == ips_leave(Adapter))
1278                                         RTW_INFO("======> ips_leave fail.............\n");
1279 #endif
1280 #endif /* CONFIG_SWLPS_IN_IPS || (CONFIG_PLATFORM_SPRD && CONFIG_RTL8188E) */
1281                         }
1282                 }
1283         }
1284
1285 }
1286
1287 #ifdef CONFIG_LPS_LCLK
1288 void LPS_Leave_check(
1289         PADAPTER padapter)
1290 {
1291         struct pwrctrl_priv *pwrpriv;
1292         u32     start_time;
1293         u8      bReady;
1294
1295
1296         pwrpriv = adapter_to_pwrctl(padapter);
1297
1298         bReady = _FALSE;
1299         start_time = rtw_get_current_time();
1300
1301         rtw_yield_os();
1302
1303         while (1) {
1304                 _enter_pwrlock(&pwrpriv->lock);
1305
1306                 if (rtw_is_surprise_removed(padapter)
1307                     || (!rtw_is_hw_init_completed(padapter))
1308 #ifdef CONFIG_USB_HCI
1309                     || rtw_is_drv_stopped(padapter)
1310 #endif
1311                     || (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
1312                    )
1313                         bReady = _TRUE;
1314
1315                 _exit_pwrlock(&pwrpriv->lock);
1316
1317                 if (_TRUE == bReady)
1318                         break;
1319
1320                 if (rtw_get_passing_time_ms(start_time) > 100) {
1321                         RTW_INFO("Wait for cpwm event  than 100 ms!!!\n");
1322                         break;
1323                 }
1324                 rtw_msleep_os(1);
1325         }
1326
1327 }
1328
1329 /*
1330  * Caller:ISR handler...
1331  *
1332  * This will be called when CPWM interrupt is up.
1333  *
1334  * using to update cpwn of drv; and drv willl make a decision to up or down pwr level
1335  */
1336 void cpwm_int_hdl(
1337         PADAPTER padapter,
1338         struct reportpwrstate_parm *preportpwrstate)
1339 {
1340         struct pwrctrl_priv *pwrpriv;
1341
1342
1343         pwrpriv = adapter_to_pwrctl(padapter);
1344 #if 0
1345         if (pwrpriv->cpwm_tog == (preportpwrstate->state & PS_TOGGLE)) {
1346                 goto exit;
1347         }
1348 #endif
1349
1350         _enter_pwrlock(&pwrpriv->lock);
1351
1352 #ifdef CONFIG_LPS_RPWM_TIMER
1353         if (pwrpriv->rpwm < PS_STATE_S2) {
1354                 RTW_INFO("%s: Redundant CPWM Int. RPWM=0x%02X CPWM=0x%02x\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1355                 _exit_pwrlock(&pwrpriv->lock);
1356                 goto exit;
1357         }
1358 #endif /* CONFIG_LPS_RPWM_TIMER */
1359
1360         pwrpriv->cpwm = PS_STATE(preportpwrstate->state);
1361         pwrpriv->cpwm_tog = preportpwrstate->state & PS_TOGGLE;
1362
1363         if (pwrpriv->cpwm >= PS_STATE_S2) {
1364                 if (pwrpriv->alives & CMD_ALIVE)
1365                         _rtw_up_sema(&padapter->cmdpriv.cmd_queue_sema);
1366
1367                 if (pwrpriv->alives & XMIT_ALIVE)
1368                         _rtw_up_sema(&padapter->xmitpriv.xmit_sema);
1369         }
1370
1371         _exit_pwrlock(&pwrpriv->lock);
1372
1373 exit:
1374         return;
1375 }
1376
1377 static void cpwm_event_callback(struct work_struct *work)
1378 {
1379         struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, cpwm_event);
1380         struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
1381         _adapter *adapter = dvobj->padapters[IFACE_ID0];
1382         struct reportpwrstate_parm report;
1383
1384         /* RTW_INFO("%s\n",__FUNCTION__); */
1385
1386         report.state = PS_STATE_S2;
1387         cpwm_int_hdl(adapter, &report);
1388 }
1389
1390 #ifdef CONFIG_LPS_RPWM_TIMER
1391 static void rpwmtimeout_workitem_callback(struct work_struct *work)
1392 {
1393         PADAPTER padapter;
1394         struct dvobj_priv *dvobj;
1395         struct pwrctrl_priv *pwrpriv;
1396
1397
1398         pwrpriv = container_of(work, struct pwrctrl_priv, rpwmtimeoutwi);
1399         dvobj = pwrctl_to_dvobj(pwrpriv);
1400         padapter = dvobj->padapters[IFACE_ID0];
1401         /*      RTW_INFO("+%s: rpwm=0x%02X cpwm=0x%02X\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm); */
1402
1403         _enter_pwrlock(&pwrpriv->lock);
1404         if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2)) {
1405                 RTW_INFO("%s: rpwm=0x%02X cpwm=0x%02X CPWM done!\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1406                 goto exit;
1407         }
1408         _exit_pwrlock(&pwrpriv->lock);
1409
1410         if (rtw_read8(padapter, 0x100) != 0xEA) {
1411 #if 1
1412                 struct reportpwrstate_parm report;
1413
1414                 report.state = PS_STATE_S2;
1415                 RTW_INFO("\n%s: FW already leave 32K!\n\n", __func__);
1416                 cpwm_int_hdl(padapter, &report);
1417 #else
1418                 RTW_INFO("\n%s: FW already leave 32K!\n\n", __func__);
1419                 cpwm_event_callback(&pwrpriv->cpwm_event);
1420 #endif
1421                 return;
1422         }
1423
1424         _enter_pwrlock(&pwrpriv->lock);
1425
1426         if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2)) {
1427                 RTW_INFO("%s: cpwm=%d, nothing to do!\n", __func__, pwrpriv->cpwm);
1428                 goto exit;
1429         }
1430         pwrpriv->brpwmtimeout = _TRUE;
1431         rtw_set_rpwm(padapter, pwrpriv->rpwm);
1432         pwrpriv->brpwmtimeout = _FALSE;
1433
1434 exit:
1435         _exit_pwrlock(&pwrpriv->lock);
1436 }
1437
1438 /*
1439  * This function is a timer handler, can't do any IO in it.
1440  */
1441 static void pwr_rpwm_timeout_handler(void *FunctionContext)
1442 {
1443         PADAPTER padapter;
1444         struct pwrctrl_priv *pwrpriv;
1445
1446
1447         padapter = (PADAPTER)FunctionContext;
1448         pwrpriv = adapter_to_pwrctl(padapter);
1449         RTW_INFO("+%s: rpwm=0x%02X cpwm=0x%02X\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1450
1451         if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2)) {
1452                 RTW_INFO("+%s: cpwm=%d, nothing to do!\n", __func__, pwrpriv->cpwm);
1453                 return;
1454         }
1455
1456         _set_workitem(&pwrpriv->rpwmtimeoutwi);
1457 }
1458 #endif /* CONFIG_LPS_RPWM_TIMER */
1459
1460 __inline static void register_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
1461 {
1462         pwrctrl->alives |= tag;
1463 }
1464
1465 __inline static void unregister_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
1466 {
1467         pwrctrl->alives &= ~tag;
1468 }
1469
1470
1471 /*
1472  * Description:
1473  *      Check if the fw_pwrstate is okay for I/O.
1474  *      If not (cpwm is less than S2), then the sub-routine
1475  *      will raise the cpwm to be greater than or equal to S2.
1476  *
1477  *      Calling Context: Passive
1478  *
1479  *      Constraint:
1480  *              1. this function will request pwrctrl->lock
1481  *
1482  * Return Value:
1483  *      _SUCCESS        hardware is ready for I/O
1484  *      _FAIL           can't I/O right now
1485  */
1486 s32 rtw_register_task_alive(PADAPTER padapter, u32 task)
1487 {
1488         s32 res;
1489         struct pwrctrl_priv *pwrctrl;
1490         u8 pslv;
1491
1492
1493         res = _SUCCESS;
1494         pwrctrl = adapter_to_pwrctl(padapter);
1495         pslv = PS_STATE_S2;
1496
1497         _enter_pwrlock(&pwrctrl->lock);
1498
1499         register_task_alive(pwrctrl, task);
1500
1501         if (pwrctrl->bFwCurrentInPSMode == _TRUE) {
1502
1503                 if (pwrctrl->cpwm < pslv) {
1504                         if (pwrctrl->cpwm < PS_STATE_S2)
1505                                 res = _FAIL;
1506                         if (pwrctrl->rpwm < pslv)
1507                                 rtw_set_rpwm(padapter, pslv);
1508                 }
1509         }
1510
1511         _exit_pwrlock(&pwrctrl->lock);
1512
1513 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1514         if (_FAIL == res) {
1515                 if (pwrctrl->cpwm >= PS_STATE_S2)
1516                         res = _SUCCESS;
1517         }
1518 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1519
1520
1521         return res;
1522 }
1523
1524 /*
1525  * Description:
1526  *      If task is done, call this func. to power down firmware again.
1527  *
1528  *      Constraint:
1529  *              1. this function will request pwrctrl->lock
1530  *
1531  * Return Value:
1532  *      none
1533  */
1534 void rtw_unregister_task_alive(PADAPTER padapter, u32 task)
1535 {
1536         struct pwrctrl_priv *pwrctrl;
1537         u8 pslv;
1538
1539
1540         pwrctrl = adapter_to_pwrctl(padapter);
1541         pslv = PS_STATE_S0;
1542
1543 #ifdef CONFIG_BT_COEXIST
1544         if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
1545             && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)) {
1546                 u8 val8;
1547
1548                 val8 = rtw_btcoex_LpsVal(padapter);
1549                 if (val8 & BIT(4))
1550                         pslv = PS_STATE_S2;
1551
1552         }
1553 #endif /* CONFIG_BT_COEXIST */
1554
1555         _enter_pwrlock(&pwrctrl->lock);
1556
1557         unregister_task_alive(pwrctrl, task);
1558
1559         if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
1560             && (pwrctrl->bFwCurrentInPSMode == _TRUE)) {
1561
1562                 if (pwrctrl->cpwm > pslv) {
1563                         if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
1564                                 rtw_set_rpwm(padapter, pslv);
1565                 }
1566         }
1567
1568         _exit_pwrlock(&pwrctrl->lock);
1569
1570 }
1571
1572 /*
1573  * Caller: rtw_xmit_thread
1574  *
1575  * Check if the fw_pwrstate is okay for xmit.
1576  * If not (cpwm is less than S3), then the sub-routine
1577  * will raise the cpwm to be greater than or equal to S3.
1578  *
1579  * Calling Context: Passive
1580  *
1581  * Return Value:
1582  *       _SUCCESS       rtw_xmit_thread can write fifo/txcmd afterwards.
1583  *       _FAIL          rtw_xmit_thread can not do anything.
1584  */
1585 s32 rtw_register_tx_alive(PADAPTER padapter)
1586 {
1587         s32 res;
1588         struct pwrctrl_priv *pwrctrl;
1589         u8 pslv;
1590
1591
1592         res = _SUCCESS;
1593         pwrctrl = adapter_to_pwrctl(padapter);
1594         pslv = PS_STATE_S2;
1595
1596         _enter_pwrlock(&pwrctrl->lock);
1597
1598         register_task_alive(pwrctrl, XMIT_ALIVE);
1599
1600         if (pwrctrl->bFwCurrentInPSMode == _TRUE) {
1601
1602                 if (pwrctrl->cpwm < pslv) {
1603                         if (pwrctrl->cpwm < PS_STATE_S2)
1604                                 res = _FAIL;
1605                         if (pwrctrl->rpwm < pslv)
1606                                 rtw_set_rpwm(padapter, pslv);
1607                 }
1608         }
1609
1610         _exit_pwrlock(&pwrctrl->lock);
1611
1612 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1613         if (_FAIL == res) {
1614                 if (pwrctrl->cpwm >= PS_STATE_S2)
1615                         res = _SUCCESS;
1616         }
1617 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1618
1619
1620         return res;
1621 }
1622
1623 /*
1624  * Caller: rtw_cmd_thread
1625  *
1626  * Check if the fw_pwrstate is okay for issuing cmd.
1627  * If not (cpwm should be is less than S2), then the sub-routine
1628  * will raise the cpwm to be greater than or equal to S2.
1629  *
1630  * Calling Context: Passive
1631  *
1632  * Return Value:
1633  *      _SUCCESS        rtw_cmd_thread can issue cmds to firmware afterwards.
1634  *      _FAIL           rtw_cmd_thread can not do anything.
1635  */
1636 s32 rtw_register_cmd_alive(PADAPTER padapter)
1637 {
1638         s32 res;
1639         struct pwrctrl_priv *pwrctrl;
1640         u8 pslv;
1641
1642
1643         res = _SUCCESS;
1644         pwrctrl = adapter_to_pwrctl(padapter);
1645         pslv = PS_STATE_S2;
1646
1647         _enter_pwrlock(&pwrctrl->lock);
1648
1649         register_task_alive(pwrctrl, CMD_ALIVE);
1650
1651         if (pwrctrl->bFwCurrentInPSMode == _TRUE) {
1652
1653                 if (pwrctrl->cpwm < pslv) {
1654                         if (pwrctrl->cpwm < PS_STATE_S2)
1655                                 res = _FAIL;
1656                         if (pwrctrl->rpwm < pslv)
1657                                 rtw_set_rpwm(padapter, pslv);
1658                 }
1659         }
1660
1661         _exit_pwrlock(&pwrctrl->lock);
1662
1663 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1664         if (_FAIL == res) {
1665                 if (pwrctrl->cpwm >= PS_STATE_S2)
1666                         res = _SUCCESS;
1667         }
1668 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1669
1670
1671         return res;
1672 }
1673
1674 /*
1675  * Caller: rx_isr
1676  *
1677  * Calling Context: Dispatch/ISR
1678  *
1679  * Return Value:
1680  *      _SUCCESS
1681  *      _FAIL
1682  */
1683 s32 rtw_register_rx_alive(PADAPTER padapter)
1684 {
1685         struct pwrctrl_priv *pwrctrl;
1686
1687
1688         pwrctrl = adapter_to_pwrctl(padapter);
1689
1690         _enter_pwrlock(&pwrctrl->lock);
1691
1692         register_task_alive(pwrctrl, RECV_ALIVE);
1693
1694         _exit_pwrlock(&pwrctrl->lock);
1695
1696
1697         return _SUCCESS;
1698 }
1699
1700 /*
1701  * Caller: evt_isr or evt_thread
1702  *
1703  * Calling Context: Dispatch/ISR or Passive
1704  *
1705  * Return Value:
1706  *      _SUCCESS
1707  *      _FAIL
1708  */
1709 s32 rtw_register_evt_alive(PADAPTER padapter)
1710 {
1711         struct pwrctrl_priv *pwrctrl;
1712
1713
1714         pwrctrl = adapter_to_pwrctl(padapter);
1715
1716         _enter_pwrlock(&pwrctrl->lock);
1717
1718         register_task_alive(pwrctrl, EVT_ALIVE);
1719
1720         _exit_pwrlock(&pwrctrl->lock);
1721
1722
1723         return _SUCCESS;
1724 }
1725
1726 /*
1727  * Caller: ISR
1728  *
1729  * If ISR's txdone,
1730  * No more pkts for TX,
1731  * Then driver shall call this fun. to power down firmware again.
1732  */
1733 void rtw_unregister_tx_alive(PADAPTER padapter)
1734 {
1735         struct pwrctrl_priv *pwrctrl;
1736         _adapter *iface;
1737         struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1738         u8 pslv, i;
1739
1740
1741         pwrctrl = adapter_to_pwrctl(padapter);
1742         pslv = PS_STATE_S0;
1743
1744 #ifdef CONFIG_BT_COEXIST
1745         if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
1746             && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)) {
1747                 u8 val8;
1748
1749                 val8 = rtw_btcoex_LpsVal(padapter);
1750                 if (val8 & BIT(4))
1751                         pslv = PS_STATE_S2;
1752
1753         }
1754 #endif /* CONFIG_BT_COEXIST */
1755
1756 #ifdef CONFIG_P2P_PS
1757         for (i = 0; i < dvobj->iface_nums; i++) {
1758                 iface = dvobj->padapters[i];
1759                 if ((iface) && rtw_is_adapter_up(iface)) {
1760                         if (iface->wdinfo.p2p_ps_mode > P2P_PS_NONE) {
1761                                 pslv = PS_STATE_S2;
1762                                 break;
1763                         }
1764                 }
1765         }
1766 #endif
1767         _enter_pwrlock(&pwrctrl->lock);
1768
1769         unregister_task_alive(pwrctrl, XMIT_ALIVE);
1770
1771         if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
1772             && (pwrctrl->bFwCurrentInPSMode == _TRUE)) {
1773
1774                 if (pwrctrl->cpwm > pslv) {
1775                         if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
1776                                 rtw_set_rpwm(padapter, pslv);
1777                 }
1778         }
1779
1780         _exit_pwrlock(&pwrctrl->lock);
1781
1782 }
1783
1784 /*
1785  * Caller: ISR
1786  *
1787  * If all commands have been done,
1788  * and no more command to do,
1789  * then driver shall call this fun. to power down firmware again.
1790  */
1791 void rtw_unregister_cmd_alive(PADAPTER padapter)
1792 {
1793         _adapter *iface;
1794         struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1795         struct pwrctrl_priv *pwrctrl;
1796         u8 pslv, i;
1797
1798
1799         pwrctrl = adapter_to_pwrctl(padapter);
1800         pslv = PS_STATE_S0;
1801
1802 #ifdef CONFIG_BT_COEXIST
1803         if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
1804             && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)) {
1805                 u8 val8;
1806
1807                 val8 = rtw_btcoex_LpsVal(padapter);
1808                 if (val8 & BIT(4))
1809                         pslv = PS_STATE_S2;
1810
1811         }
1812 #endif /* CONFIG_BT_COEXIST */
1813
1814 #ifdef CONFIG_P2P_PS
1815         for (i = 0; i < dvobj->iface_nums; i++) {
1816                 iface = dvobj->padapters[i];
1817                 if ((iface) && rtw_is_adapter_up(iface)) {
1818                         if (iface->wdinfo.p2p_ps_mode > P2P_PS_NONE) {
1819                                 pslv = PS_STATE_S2;
1820                                 break;
1821                         }
1822                 }
1823         }
1824 #endif
1825
1826         _enter_pwrlock(&pwrctrl->lock);
1827
1828         unregister_task_alive(pwrctrl, CMD_ALIVE);
1829
1830         if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
1831             && (pwrctrl->bFwCurrentInPSMode == _TRUE)) {
1832
1833                 if (pwrctrl->cpwm > pslv) {
1834                         if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
1835                                 rtw_set_rpwm(padapter, pslv);
1836                 }
1837         }
1838
1839         _exit_pwrlock(&pwrctrl->lock);
1840
1841 }
1842
1843 /*
1844  * Caller: ISR
1845  */
1846 void rtw_unregister_rx_alive(PADAPTER padapter)
1847 {
1848         struct pwrctrl_priv *pwrctrl;
1849
1850
1851         pwrctrl = adapter_to_pwrctl(padapter);
1852
1853         _enter_pwrlock(&pwrctrl->lock);
1854
1855         unregister_task_alive(pwrctrl, RECV_ALIVE);
1856
1857
1858         _exit_pwrlock(&pwrctrl->lock);
1859
1860 }
1861
1862 void rtw_unregister_evt_alive(PADAPTER padapter)
1863 {
1864         struct pwrctrl_priv *pwrctrl;
1865
1866
1867         pwrctrl = adapter_to_pwrctl(padapter);
1868
1869         unregister_task_alive(pwrctrl, EVT_ALIVE);
1870
1871
1872         _exit_pwrlock(&pwrctrl->lock);
1873
1874 }
1875 #endif  /* CONFIG_LPS_LCLK */
1876
1877 #ifdef CONFIG_RESUME_IN_WORKQUEUE
1878         static void resume_workitem_callback(struct work_struct *work);
1879 #endif /* CONFIG_RESUME_IN_WORKQUEUE */
1880
1881 void rtw_init_pwrctrl_priv(PADAPTER padapter)
1882 {
1883         struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
1884         int i = 0;
1885         u8 val8 = 0;
1886
1887 #if defined(CONFIG_CONCURRENT_MODE)
1888         if (padapter->adapter_type != PRIMARY_ADAPTER)
1889                 return;
1890 #endif
1891
1892
1893 #ifdef PLATFORM_WINDOWS
1894         pwrctrlpriv->pnp_current_pwr_state = NdisDeviceStateD0;
1895 #endif
1896
1897         _init_pwrlock(&pwrctrlpriv->lock);
1898         _init_pwrlock(&pwrctrlpriv->check_32k_lock);
1899         pwrctrlpriv->rf_pwrstate = rf_on;
1900         pwrctrlpriv->ips_enter_cnts = 0;
1901         pwrctrlpriv->ips_leave_cnts = 0;
1902         pwrctrlpriv->lps_enter_cnts = 0;
1903         pwrctrlpriv->lps_leave_cnts = 0;
1904         pwrctrlpriv->bips_processing = _FALSE;
1905
1906         pwrctrlpriv->ips_mode = padapter->registrypriv.ips_mode;
1907         pwrctrlpriv->ips_mode_req = padapter->registrypriv.ips_mode;
1908
1909         pwrctrlpriv->pwr_state_check_interval = RTW_PWR_STATE_CHK_INTERVAL;
1910         pwrctrlpriv->pwr_state_check_cnts = 0;
1911         pwrctrlpriv->bInternalAutoSuspend = _FALSE;
1912         pwrctrlpriv->bInSuspend = _FALSE;
1913         pwrctrlpriv->bkeepfwalive = _FALSE;
1914
1915 #ifdef CONFIG_AUTOSUSPEND
1916 #ifdef SUPPORT_HW_RFOFF_DETECTED
1917         pwrctrlpriv->pwr_state_check_interval = (pwrctrlpriv->bHWPwrPindetect) ? 1000 : 2000;
1918 #endif
1919 #endif
1920
1921         pwrctrlpriv->LpsIdleCount = 0;
1922
1923 #ifdef CONFIG_LPS_PG
1924         pwrctrlpriv->lpspg_rsvd_page_locate = 0;
1925 #endif
1926
1927         /* pwrctrlpriv->FWCtrlPSMode =padapter->registrypriv.power_mgnt; */ /* PS_MODE_MIN; */
1928         if (padapter->registrypriv.mp_mode == 1)
1929                 pwrctrlpriv->power_mgnt = PS_MODE_ACTIVE ;
1930         else
1931                 pwrctrlpriv->power_mgnt = padapter->registrypriv.power_mgnt; /* PS_MODE_MIN; */
1932         pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt) ? _TRUE : _FALSE;
1933
1934         pwrctrlpriv->bFwCurrentInPSMode = _FALSE;
1935
1936         pwrctrlpriv->rpwm = 0;
1937         pwrctrlpriv->cpwm = PS_STATE_S4;
1938
1939         pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
1940         pwrctrlpriv->smart_ps = padapter->registrypriv.smart_ps;
1941         pwrctrlpriv->bcn_ant_mode = 0;
1942         pwrctrlpriv->dtim = 0;
1943
1944         pwrctrlpriv->tog = 0x80;
1945
1946 #ifdef CONFIG_LPS_LCLK
1947         rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&pwrctrlpriv->rpwm));
1948
1949         _init_workitem(&pwrctrlpriv->cpwm_event, cpwm_event_callback, NULL);
1950
1951 #ifdef CONFIG_LPS_RPWM_TIMER
1952         pwrctrlpriv->brpwmtimeout = _FALSE;
1953         _init_workitem(&pwrctrlpriv->rpwmtimeoutwi, rpwmtimeout_workitem_callback, NULL);
1954         _init_timer(&pwrctrlpriv->pwr_rpwm_timer, padapter->pnetdev, pwr_rpwm_timeout_handler, padapter);
1955 #endif /* CONFIG_LPS_RPWM_TIMER */
1956 #endif /* CONFIG_LPS_LCLK */
1957
1958         rtw_init_timer(&pwrctrlpriv->pwr_state_check_timer, padapter, pwr_state_check_handler);
1959
1960         pwrctrlpriv->wowlan_mode = _FALSE;
1961         pwrctrlpriv->wowlan_ap_mode = _FALSE;
1962         pwrctrlpriv->wowlan_p2p_mode = _FALSE;
1963         pwrctrlpriv->wowlan_last_wake_reason = 0;
1964
1965 #ifdef CONFIG_RESUME_IN_WORKQUEUE
1966         _init_workitem(&pwrctrlpriv->resume_work, resume_workitem_callback, NULL);
1967         pwrctrlpriv->rtw_workqueue = create_singlethread_workqueue("rtw_workqueue");
1968 #endif /* CONFIG_RESUME_IN_WORKQUEUE */
1969
1970 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
1971         pwrctrlpriv->early_suspend.suspend = NULL;
1972         rtw_register_early_suspend(pwrctrlpriv);
1973 #endif /* CONFIG_HAS_EARLYSUSPEND || CONFIG_ANDROID_POWER */
1974
1975 #ifdef CONFIG_GPIO_WAKEUP
1976         /*default low active*/
1977         pwrctrlpriv->is_high_active = HIGH_ACTIVE;
1978         val8 = (pwrctrlpriv->is_high_active == 0) ? 1 : 0;
1979         rtw_hal_switch_gpio_wl_ctrl(padapter, WAKEUP_GPIO_IDX, _TRUE);
1980         rtw_hal_set_output_gpio(padapter, WAKEUP_GPIO_IDX, val8);
1981         RTW_INFO("%s: set GPIO_%d %d as default.\n",
1982                  __func__, WAKEUP_GPIO_IDX, val8);
1983 #endif /* CONFIG_GPIO_WAKEUP */
1984
1985 #ifdef CONFIG_WOWLAN
1986         pwrctrlpriv->wowlan_pattern_idx = DEFAULT_PATTERN_NUM;
1987         pwrctrlpriv->wowlan_in_resume = _FALSE;
1988         for (i = 0 ; i < MAX_WKFM_NUM; i++) {
1989                 _rtw_memset(pwrctrlpriv->patterns[i].content, '\0',
1990                             sizeof(pwrctrlpriv->patterns[i].content));
1991                 _rtw_memset(pwrctrlpriv->patterns[i].mask, '\0',
1992                             sizeof(pwrctrlpriv->patterns[i].mask));
1993                 pwrctrlpriv->patterns[i].len = 0;
1994         }
1995
1996 #ifdef CONFIG_PNO_SUPPORT
1997         pwrctrlpriv->pno_inited = _FALSE;
1998         pwrctrlpriv->pnlo_info = NULL;
1999         pwrctrlpriv->pscan_info = NULL;
2000         pwrctrlpriv->pno_ssid_list = NULL;
2001 #endif /* CONFIG_PNO_SUPPORT */
2002         pwrctrlpriv->wowlan_aoac_rpt_loc = 0;
2003 #endif /* CONFIG_WOWLAN */
2004
2005 #ifdef CONFIG_LPS_POFF
2006         rtw_hal_set_hwreg(padapter, HW_VAR_LPS_POFF_INIT, 0);
2007 #endif
2008
2009
2010 }
2011
2012
2013 void rtw_free_pwrctrl_priv(PADAPTER adapter)
2014 {
2015         struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(adapter);
2016
2017 #if defined(CONFIG_CONCURRENT_MODE)
2018         if (adapter->adapter_type != PRIMARY_ADAPTER)
2019                 return;
2020 #endif
2021
2022
2023         /* _rtw_memset((unsigned char *)pwrctrlpriv, 0, sizeof(struct pwrctrl_priv)); */
2024
2025
2026 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2027         if (pwrctrlpriv->rtw_workqueue) {
2028                 flush_workqueue(pwrctrlpriv->rtw_workqueue);
2029                 destroy_workqueue(pwrctrlpriv->rtw_workqueue);
2030         }
2031 #endif
2032
2033 #ifdef CONFIG_LPS_POFF
2034         rtw_hal_set_hwreg(adapter, HW_VAR_LPS_POFF_DEINIT, 0);
2035 #endif
2036
2037 #ifdef CONFIG_WOWLAN
2038 #ifdef CONFIG_PNO_SUPPORT
2039         if (pwrctrlpriv->pnlo_info != NULL)
2040                 printk("****** pnlo_info memory leak********\n");
2041
2042         if (pwrctrlpriv->pscan_info != NULL)
2043                 printk("****** pscan_info memory leak********\n");
2044
2045         if (pwrctrlpriv->pno_ssid_list != NULL)
2046                 printk("****** pno_ssid_list memory leak********\n");
2047 #endif
2048 #endif /* CONFIG_WOWLAN */
2049
2050 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
2051         rtw_unregister_early_suspend(pwrctrlpriv);
2052 #endif /* CONFIG_HAS_EARLYSUSPEND || CONFIG_ANDROID_POWER */
2053
2054         _free_pwrlock(&pwrctrlpriv->lock);
2055         _free_pwrlock(&pwrctrlpriv->check_32k_lock);
2056
2057 }
2058
2059 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2060 extern int rtw_resume_process(_adapter *padapter);
2061
2062 static void resume_workitem_callback(struct work_struct *work)
2063 {
2064         struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, resume_work);
2065         struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2066         _adapter *adapter = dvobj->padapters[IFACE_ID0];
2067
2068         RTW_INFO("%s\n", __FUNCTION__);
2069
2070         rtw_resume_process(adapter);
2071
2072         rtw_resume_unlock_suspend();
2073 }
2074
2075 void rtw_resume_in_workqueue(struct pwrctrl_priv *pwrpriv)
2076 {
2077         /* accquire system's suspend lock preventing from falliing asleep while resume in workqueue */
2078         /* rtw_lock_suspend(); */
2079
2080         rtw_resume_lock_suspend();
2081
2082 #if 1
2083         queue_work(pwrpriv->rtw_workqueue, &pwrpriv->resume_work);
2084 #else
2085         _set_workitem(&pwrpriv->resume_work);
2086 #endif
2087 }
2088 #endif /* CONFIG_RESUME_IN_WORKQUEUE */
2089
2090 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
2091 inline bool rtw_is_earlysuspend_registered(struct pwrctrl_priv *pwrpriv)
2092 {
2093         return (pwrpriv->early_suspend.suspend) ? _TRUE : _FALSE;
2094 }
2095
2096 inline bool rtw_is_do_late_resume(struct pwrctrl_priv *pwrpriv)
2097 {
2098         return (pwrpriv->do_late_resume) ? _TRUE : _FALSE;
2099 }
2100
2101 inline void rtw_set_do_late_resume(struct pwrctrl_priv *pwrpriv, bool enable)
2102 {
2103         pwrpriv->do_late_resume = enable;
2104 }
2105 #endif
2106
2107 #ifdef CONFIG_HAS_EARLYSUSPEND
2108 extern int rtw_resume_process(_adapter *padapter);
2109 static void rtw_early_suspend(struct early_suspend *h)
2110 {
2111         struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2112         RTW_INFO("%s\n", __FUNCTION__);
2113
2114         rtw_set_do_late_resume(pwrpriv, _FALSE);
2115 }
2116
2117 static void rtw_late_resume(struct early_suspend *h)
2118 {
2119         struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2120         struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2121         _adapter *adapter = dvobj->padapters[IFACE_ID0];
2122
2123         RTW_INFO("%s\n", __FUNCTION__);
2124
2125         if (pwrpriv->do_late_resume) {
2126                 rtw_set_do_late_resume(pwrpriv, _FALSE);
2127                 rtw_resume_process(adapter);
2128         }
2129 }
2130
2131 void rtw_register_early_suspend(struct pwrctrl_priv *pwrpriv)
2132 {
2133         RTW_INFO("%s\n", __FUNCTION__);
2134
2135         /* jeff: set the early suspend level before blank screen, so we wll do late resume after scree is lit */
2136         pwrpriv->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
2137         pwrpriv->early_suspend.suspend = rtw_early_suspend;
2138         pwrpriv->early_suspend.resume = rtw_late_resume;
2139         register_early_suspend(&pwrpriv->early_suspend);
2140
2141
2142 }
2143
2144 void rtw_unregister_early_suspend(struct pwrctrl_priv *pwrpriv)
2145 {
2146         RTW_INFO("%s\n", __FUNCTION__);
2147
2148         rtw_set_do_late_resume(pwrpriv, _FALSE);
2149
2150         if (pwrpriv->early_suspend.suspend)
2151                 unregister_early_suspend(&pwrpriv->early_suspend);
2152
2153         pwrpriv->early_suspend.suspend = NULL;
2154         pwrpriv->early_suspend.resume = NULL;
2155 }
2156 #endif /* CONFIG_HAS_EARLYSUSPEND */
2157
2158 #ifdef CONFIG_ANDROID_POWER
2159 #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2160         extern int rtw_resume_process(PADAPTER padapter);
2161 #endif
2162 static void rtw_early_suspend(android_early_suspend_t *h)
2163 {
2164         struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2165         RTW_INFO("%s\n", __FUNCTION__);
2166
2167         rtw_set_do_late_resume(pwrpriv, _FALSE);
2168 }
2169
2170 static void rtw_late_resume(android_early_suspend_t *h)
2171 {
2172         struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2173         struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2174         _adapter *adapter = dvobj->padapters[IFACE_ID0];
2175
2176         RTW_INFO("%s\n", __FUNCTION__);
2177         if (pwrpriv->do_late_resume) {
2178 #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2179                 rtw_set_do_late_resume(pwrpriv, _FALSE);
2180                 rtw_resume_process(adapter);
2181 #endif
2182         }
2183 }
2184
2185 void rtw_register_early_suspend(struct pwrctrl_priv *pwrpriv)
2186 {
2187         RTW_INFO("%s\n", __FUNCTION__);
2188
2189         /* jeff: set the early suspend level before blank screen, so we wll do late resume after scree is lit */
2190         pwrpriv->early_suspend.level = ANDROID_EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
2191         pwrpriv->early_suspend.suspend = rtw_early_suspend;
2192         pwrpriv->early_suspend.resume = rtw_late_resume;
2193         android_register_early_suspend(&pwrpriv->early_suspend);
2194 }
2195
2196 void rtw_unregister_early_suspend(struct pwrctrl_priv *pwrpriv)
2197 {
2198         RTW_INFO("%s\n", __FUNCTION__);
2199
2200         rtw_set_do_late_resume(pwrpriv, _FALSE);
2201
2202         if (pwrpriv->early_suspend.suspend)
2203                 android_unregister_early_suspend(&pwrpriv->early_suspend);
2204
2205         pwrpriv->early_suspend.suspend = NULL;
2206         pwrpriv->early_suspend.resume = NULL;
2207 }
2208 #endif /* CONFIG_ANDROID_POWER */
2209
2210 u8 rtw_interface_ps_func(_adapter *padapter, HAL_INTF_PS_FUNC efunc_id, u8 *val)
2211 {
2212         u8 bResult = _TRUE;
2213         rtw_hal_intf_ps_func(padapter, efunc_id, val);
2214
2215         return bResult;
2216 }
2217
2218
2219 inline void rtw_set_ips_deny(_adapter *padapter, u32 ms)
2220 {
2221         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
2222         pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ms);
2223 }
2224
2225 /*
2226 * rtw_pwr_wakeup - Wake the NIC up from: 1)IPS. 2)USB autosuspend
2227 * @adapter: pointer to _adapter structure
2228 * @ips_deffer_ms: the ms wiil prevent from falling into IPS after wakeup
2229 * Return _SUCCESS or _FAIL
2230 */
2231
2232 int _rtw_pwr_wakeup(_adapter *padapter, u32 ips_deffer_ms, const char *caller)
2233 {
2234         struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
2235         struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
2236         struct mlme_priv *pmlmepriv;
2237         int ret = _SUCCESS;
2238         int i;
2239         u32 start = rtw_get_current_time();
2240
2241         /* for LPS */
2242         LeaveAllPowerSaveMode(padapter);
2243
2244         /* IPS still bound with primary adapter */
2245         padapter = GET_PRIMARY_ADAPTER(padapter);
2246         pmlmepriv = &padapter->mlmepriv;
2247
2248         if (pwrpriv->ips_deny_time < rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms))
2249                 pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms);
2250
2251
2252         if (pwrpriv->ps_processing) {
2253                 RTW_INFO("%s wait ps_processing...\n", __func__);
2254                 while (pwrpriv->ps_processing && rtw_get_passing_time_ms(start) <= 3000)
2255                         rtw_msleep_os(10);
2256                 if (pwrpriv->ps_processing)
2257                         RTW_INFO("%s wait ps_processing timeout\n", __func__);
2258                 else
2259                         RTW_INFO("%s wait ps_processing done\n", __func__);
2260         }
2261
2262 #ifdef DBG_CONFIG_ERROR_DETECT
2263         if (rtw_hal_sreset_inprogress(padapter)) {
2264                 RTW_INFO("%s wait sreset_inprogress...\n", __func__);
2265                 while (rtw_hal_sreset_inprogress(padapter) && rtw_get_passing_time_ms(start) <= 4000)
2266                         rtw_msleep_os(10);
2267                 if (rtw_hal_sreset_inprogress(padapter))
2268                         RTW_INFO("%s wait sreset_inprogress timeout\n", __func__);
2269                 else
2270                         RTW_INFO("%s wait sreset_inprogress done\n", __func__);
2271         }
2272 #endif
2273
2274         if (pwrpriv->bInternalAutoSuspend == _FALSE && pwrpriv->bInSuspend) {
2275                 RTW_INFO("%s wait bInSuspend...\n", __func__);
2276                 while (pwrpriv->bInSuspend
2277                        && ((rtw_get_passing_time_ms(start) <= 3000 && !rtw_is_do_late_resume(pwrpriv))
2278                         || (rtw_get_passing_time_ms(start) <= 500 && rtw_is_do_late_resume(pwrpriv)))
2279                       )
2280                         rtw_msleep_os(10);
2281                 if (pwrpriv->bInSuspend)
2282                         RTW_INFO("%s wait bInSuspend timeout\n", __func__);
2283                 else
2284                         RTW_INFO("%s wait bInSuspend done\n", __func__);
2285         }
2286
2287         /* System suspend is not allowed to wakeup */
2288         if ((pwrpriv->bInternalAutoSuspend == _FALSE) && (_TRUE == pwrpriv->bInSuspend)) {
2289                 ret = _FAIL;
2290                 goto exit;
2291         }
2292
2293         /* block??? */
2294         if ((pwrpriv->bInternalAutoSuspend == _TRUE)  && (padapter->net_closed == _TRUE)) {
2295                 ret = _FAIL;
2296                 goto exit;
2297         }
2298
2299         /* I think this should be check in IPS, LPS, autosuspend functions... */
2300         if (check_fwstate(pmlmepriv, _FW_LINKED) == _TRUE) {
2301 #if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
2302                 if (_TRUE == pwrpriv->bInternalAutoSuspend) {
2303                         if (0 == pwrpriv->autopm_cnt) {
2304 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33))
2305                                 if (usb_autopm_get_interface(adapter_to_dvobj(padapter)->pusbintf) < 0)
2306                                         RTW_INFO("can't get autopm:\n");
2307 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20))
2308                                 usb_autopm_disable(adapter_to_dvobj(padapter)->pusbintf);
2309 #else
2310                                 usb_autoresume_device(adapter_to_dvobj(padapter)->pusbdev, 1);
2311 #endif
2312                                 pwrpriv->autopm_cnt++;
2313                         }
2314 #endif  /* #if defined (CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND) */
2315                         ret = _SUCCESS;
2316                         goto exit;
2317 #if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
2318                 }
2319 #endif  /* #if defined (CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND) */
2320         }
2321
2322         if (rf_off == pwrpriv->rf_pwrstate) {
2323 #ifdef CONFIG_USB_HCI
2324 #ifdef CONFIG_AUTOSUSPEND
2325                 if (pwrpriv->brfoffbyhw == _TRUE) {
2326                         RTW_INFO("hw still in rf_off state ...........\n");
2327                         ret = _FAIL;
2328                         goto exit;
2329                 } else if (padapter->registrypriv.usbss_enable) {
2330                         RTW_INFO("%s call autoresume_enter....\n", __FUNCTION__);
2331                         if (_FAIL ==  autoresume_enter(padapter)) {
2332                                 RTW_INFO("======> autoresume fail.............\n");
2333                                 ret = _FAIL;
2334                                 goto exit;
2335                         }
2336                 } else
2337 #endif
2338 #endif
2339                 {
2340 #ifdef CONFIG_IPS
2341                         RTW_INFO("%s call ips_leave....\n", __FUNCTION__);
2342                         if (_FAIL ==  ips_leave(padapter)) {
2343                                 RTW_INFO("======> ips_leave fail.............\n");
2344                                 ret = _FAIL;
2345                                 goto exit;
2346                         }
2347 #endif
2348                 }
2349         }
2350
2351         /* TODO: the following checking need to be merged... */
2352         if (rtw_is_drv_stopped(padapter)
2353             || !padapter->bup
2354             || !rtw_is_hw_init_completed(padapter)
2355            ) {
2356                 RTW_INFO("%s: bDriverStopped=%s, bup=%d, hw_init_completed=%u\n"
2357                          , caller
2358                          , rtw_is_drv_stopped(padapter) ? "True" : "False"
2359                          , padapter->bup
2360                          , rtw_get_hw_init_completed(padapter));
2361                 ret = _FALSE;
2362                 goto exit;
2363         }
2364
2365 exit:
2366         if (pwrpriv->ips_deny_time < rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms))
2367                 pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms);
2368         return ret;
2369
2370 }
2371
2372 int rtw_pm_set_lps(_adapter *padapter, u8 mode)
2373 {
2374         int     ret = 0;
2375         struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2376
2377         if (mode < PS_MODE_NUM) {
2378                 if (pwrctrlpriv->power_mgnt != mode) {
2379                         if (PS_MODE_ACTIVE == mode)
2380                                 LeaveAllPowerSaveMode(padapter);
2381                         else
2382                                 pwrctrlpriv->LpsIdleCount = 2;
2383                         pwrctrlpriv->power_mgnt = mode;
2384                         pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt) ? _TRUE : _FALSE;
2385                 }
2386         } else
2387                 ret = -EINVAL;
2388
2389         return ret;
2390 }
2391
2392 int rtw_pm_set_ips(_adapter *padapter, u8 mode)
2393 {
2394         struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2395
2396         if (mode == IPS_NORMAL || mode == IPS_LEVEL_2) {
2397                 rtw_ips_mode_req(pwrctrlpriv, mode);
2398                 RTW_INFO("%s %s\n", __FUNCTION__, mode == IPS_NORMAL ? "IPS_NORMAL" : "IPS_LEVEL_2");
2399                 return 0;
2400         } else if (mode == IPS_NONE) {
2401                 rtw_ips_mode_req(pwrctrlpriv, mode);
2402                 RTW_INFO("%s %s\n", __FUNCTION__, "IPS_NONE");
2403                 if (!rtw_is_surprise_removed(padapter) && (_FAIL == rtw_pwr_wakeup(padapter)))
2404                         return -EFAULT;
2405         } else
2406                 return -EINVAL;
2407         return 0;
2408 }
2409
2410 /*
2411  * ATTENTION:
2412  *      This function will request pwrctrl LOCK!
2413  */
2414 void rtw_ps_deny(PADAPTER padapter, PS_DENY_REASON reason)
2415 {
2416         struct pwrctrl_priv *pwrpriv;
2417         s32 ret;
2418
2419
2420         /*      RTW_INFO("+" FUNC_ADPT_FMT ": Request PS deny for %d (0x%08X)\n",
2421          *              FUNC_ADPT_ARG(padapter), reason, BIT(reason)); */
2422
2423         pwrpriv = adapter_to_pwrctl(padapter);
2424
2425         _enter_pwrlock(&pwrpriv->lock);
2426         if (pwrpriv->ps_deny & BIT(reason)) {
2427                 RTW_INFO(FUNC_ADPT_FMT ": [WARNING] Reason %d had been set before!!\n",
2428                          FUNC_ADPT_ARG(padapter), reason);
2429         }
2430         pwrpriv->ps_deny |= BIT(reason);
2431         _exit_pwrlock(&pwrpriv->lock);
2432
2433         /*      RTW_INFO("-" FUNC_ADPT_FMT ": Now PS deny for 0x%08X\n",
2434          *              FUNC_ADPT_ARG(padapter), pwrpriv->ps_deny); */
2435 }
2436
2437 /*
2438  * ATTENTION:
2439  *      This function will request pwrctrl LOCK!
2440  */
2441 void rtw_ps_deny_cancel(PADAPTER padapter, PS_DENY_REASON reason)
2442 {
2443         struct pwrctrl_priv *pwrpriv;
2444
2445
2446         /*      RTW_INFO("+" FUNC_ADPT_FMT ": Cancel PS deny for %d(0x%08X)\n",
2447          *              FUNC_ADPT_ARG(padapter), reason, BIT(reason)); */
2448
2449         pwrpriv = adapter_to_pwrctl(padapter);
2450
2451         _enter_pwrlock(&pwrpriv->lock);
2452         if ((pwrpriv->ps_deny & BIT(reason)) == 0) {
2453                 RTW_INFO(FUNC_ADPT_FMT ": [ERROR] Reason %d had been canceled before!!\n",
2454                          FUNC_ADPT_ARG(padapter), reason);
2455         }
2456         pwrpriv->ps_deny &= ~BIT(reason);
2457         _exit_pwrlock(&pwrpriv->lock);
2458
2459         /*      RTW_INFO("-" FUNC_ADPT_FMT ": Now PS deny for 0x%08X\n",
2460          *              FUNC_ADPT_ARG(padapter), pwrpriv->ps_deny); */
2461 }
2462
2463 /*
2464  * ATTENTION:
2465  *      Before calling this function pwrctrl lock should be occupied already,
2466  *      otherwise it may return incorrect value.
2467  */
2468 u32 rtw_ps_deny_get(PADAPTER padapter)
2469 {
2470         u32 deny;
2471
2472
2473         deny = adapter_to_pwrctl(padapter)->ps_deny;
2474
2475         return deny;
2476 }