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