OSDN Git Service

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