0a16ad5c2e3b576846d589561f43630936cb938b
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rtl8723bs / 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 #ifndef CONFIG_RTL8723A
536                         || ((pwrpriv->rpwm >= PS_STATE_S2)&&(pslv >= PS_STATE_S2))
537 #endif
538 #endif
539                         )
540                 {
541                         RT_TRACE(_module_rtl871x_pwrctrl_c_,_drv_err_,
542                                 ("%s: Already set rpwm[0x%02X], new=0x%02X!\n", __FUNCTION__, pwrpriv->rpwm, pslv));
543                         return;
544                 }
545         }
546
547         if ((padapter->bSurpriseRemoved == _TRUE) ||
548                 (padapter->hw_init_completed == _FALSE))
549         {
550                 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
551                                  ("%s: SurpriseRemoved(%d) hw_init_completed(%d)\n",
552                                   __FUNCTION__, padapter->bSurpriseRemoved, padapter->hw_init_completed));
553
554                 pwrpriv->cpwm = PS_STATE_S4;
555
556                 return;
557         }
558
559         if (padapter->bDriverStopped == _TRUE)
560         {
561                 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
562                                  ("%s: change power state(0x%02X) when DriverStopped\n", __FUNCTION__, pslv));
563
564                 if (pslv < PS_STATE_S2) {
565                         RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
566                                          ("%s: Reject to enter PS_STATE(0x%02X) lower than S2 when DriverStopped!!\n", __FUNCTION__, pslv));
567                         return;
568                 }
569         }
570
571         rpwm = pslv | pwrpriv->tog;
572 #ifdef CONFIG_LPS_LCLK
573         // only when from PS_STATE S0/S1 to S2 and higher needs ACK
574         if ((pwrpriv->cpwm < PS_STATE_S2) && (pslv >= PS_STATE_S2))
575                 rpwm |= PS_ACK;
576 #endif
577         RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
578                          ("rtw_set_rpwm: rpwm=0x%02x cpwm=0x%02x\n", rpwm, pwrpriv->cpwm));
579
580         pwrpriv->rpwm = pslv;
581
582 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
583         cpwm_orig = 0;
584         if (rpwm & PS_ACK)
585         {
586                 rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_orig);
587         }
588 #endif
589
590 #if defined(CONFIG_LPS_RPWM_TIMER) && !defined(CONFIG_DETECT_CPWM_BY_POLLING)
591         if (rpwm & PS_ACK)
592                 _set_timer(&pwrpriv->pwr_rpwm_timer, LPS_RPWM_WAIT_MS);
593 #endif // CONFIG_LPS_RPWM_TIMER & !CONFIG_DETECT_CPWM_BY_POLLING
594         rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&rpwm));
595
596         pwrpriv->tog += 0x80;
597
598 #ifdef CONFIG_LPS_LCLK
599         // No LPS 32K, No Ack
600         if (rpwm & PS_ACK)
601         {
602 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
603                 u32 start_time;
604                 u8 cpwm_now;
605                 u8 poll_cnt=0;
606
607                 start_time = rtw_get_current_time();
608
609                 // polling cpwm
610                 do {
611                         rtw_msleep_os(1);
612                         poll_cnt++;
613                         rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_now);
614                         if ((cpwm_orig ^ cpwm_now) & 0x80)
615                         {
616 #ifdef CONFIG_RTL8723A
617                                 pwrpriv->cpwm = PS_STATE(cpwm_now);
618 #else // !CONFIG_RTL8723A
619                                 pwrpriv->cpwm = PS_STATE_S4;
620 #endif // !CONFIG_RTL8723A
621                                 pwrpriv->cpwm_tog = cpwm_now & PS_TOGGLE;
622 #ifdef DBG_CHECK_FW_PS_STATE
623                                 DBG_871X("%s: polling cpwm OK! poll_cnt=%d, cpwm_orig=%02x, cpwm_now=%02x , 0x100=0x%x\n"
624                                 , __FUNCTION__,poll_cnt, cpwm_orig, cpwm_now, rtw_read8(padapter, REG_CR));
625                                 if(rtw_fw_ps_state(padapter) == _FAIL)
626                                 {
627                                         DBG_871X("leave 32k but fw state in 32k\n");
628                                         pdbgpriv->dbg_rpwm_toogle_cnt++;
629                                 }
630 #endif //DBG_CHECK_FW_PS_STATE
631                                 break;
632                         }
633
634                         if (rtw_get_passing_time_ms(start_time) > LPS_RPWM_WAIT_MS)
635                         {
636                                 DBG_871X("%s: polling cpwm timeout! poll_cnt=%d, cpwm_orig=%02x, cpwm_now=%02x \n", __FUNCTION__,poll_cnt, cpwm_orig, cpwm_now);
637 #ifdef DBG_CHECK_FW_PS_STATE
638                                 if(rtw_fw_ps_state(padapter) == _FAIL)
639                                 {
640                                         DBG_871X("rpwm timeout and fw ps state in 32k\n");
641                                         pdbgpriv->dbg_rpwm_timeout_fail_cnt++;
642                                 }
643 #endif //DBG_CHECK_FW_PS_STATE
644 #ifdef CONFIG_LPS_RPWM_TIMER
645                                 _set_timer(&pwrpriv->pwr_rpwm_timer, 1);
646 #endif // CONFIG_LPS_RPWM_TIMER
647                                 break;
648                         }
649                 } while (1);
650 #endif // CONFIG_DETECT_CPWM_BY_POLLING
651         }
652         else
653 #endif // CONFIG_LPS_LCLK
654         {
655                 pwrpriv->cpwm = pslv;
656         }
657
658 _func_exit_;
659 }
660
661 u8 PS_RDY_CHECK(_adapter * padapter)
662 {
663         u32 curr_time, delta_time;
664         struct pwrctrl_priv     *pwrpriv = adapter_to_pwrctl(padapter);
665         struct mlme_priv        *pmlmepriv = &(padapter->mlmepriv);
666 #ifdef CONFIG_P2P
667         struct wifidirect_info *pwdinfo = &(padapter->wdinfo);
668 #ifdef CONFIG_IOCTL_CFG80211
669         struct cfg80211_wifidirect_info *pcfg80211_wdinfo = &padapter->cfg80211_wdinfo;
670 #endif /* CONFIG_IOCTL_CFG80211 */
671 #endif /* CONFIG_P2P */
672
673 #if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN)
674         if(_TRUE == pwrpriv->bInSuspend && pwrpriv->wowlan_mode)
675                 return _TRUE;
676         else if(_TRUE == pwrpriv->bInSuspend && pwrpriv->wowlan_ap_mode)
677                 return _TRUE;
678         else if (_TRUE == pwrpriv->bInSuspend)
679                 return _FALSE;
680 #else
681         if(_TRUE == pwrpriv->bInSuspend )
682                 return _FALSE;
683 #endif
684
685         curr_time = rtw_get_current_time();     
686
687         delta_time = curr_time -pwrpriv->DelayLPSLastTimeStamp;
688
689         if(delta_time < LPS_DELAY_TIME)
690         {               
691                 return _FALSE;
692         }
693
694         if (check_fwstate(pmlmepriv, WIFI_SITE_MONITOR)
695                 || check_fwstate(pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
696                 || check_fwstate(pmlmepriv, WIFI_AP_STATE)
697                 || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
698                 #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211) && defined(CONFIG_P2P_IPS)
699                 || pcfg80211_wdinfo->is_ro_ch
700                 #elif defined(CONFIG_P2P)
701                 || !rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)
702                 #endif
703                 || rtw_is_scan_deny(padapter)
704 #ifdef CONFIG_TDLS
705                 // TDLS link is established.
706                 || ( padapter->tdlsinfo.link_established == _TRUE )
707 #endif // CONFIG_TDLS           
708         )
709                 return _FALSE;
710
711         if( (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) && (padapter->securitypriv.binstallGrpkey == _FALSE) )
712         {
713                 DBG_871X("Group handshake still in progress !!!\n");
714                 return _FALSE;
715         }
716
717 #ifdef CONFIG_IOCTL_CFG80211
718         if (!rtw_cfg80211_pwr_mgmt(padapter))
719                 return _FALSE;
720 #endif  
721
722         return _TRUE;
723 }
724
725 #if defined(CONFIG_FWLPS_IN_IPS) && defined(CONFIG_PNO_SUPPORT)
726 void rtw_set_fw_in_ips_mode(PADAPTER padapter, u8 enable)
727 {
728         struct hal_ops *pHalFunc = &padapter->HalFunc;
729         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
730         int cnt=0;
731         u32 start_time;
732         u8 val8 = 0;
733         u8 cpwm_orig, cpwm_now;
734         u8 parm[H2C_INACTIVE_PS_LEN]={0};
735
736         if (padapter->netif_up == _FALSE) {
737                 DBG_871X("%s: ERROR, netif is down\n", __func__);
738                 return;
739         }
740
741         if (pHalFunc->fill_h2c_cmd == NULL) {
742                 DBG_871X("%s: Please hook fill_h2c_cmd first!\n", __func__);
743                 return;
744         }
745
746         //u8 cmd_param; //BIT0:enable, BIT1:NoConnect32k
747         if (enable) {
748
749 #ifdef CONFIG_BT_COEXIST
750                 rtw_btcoex_IpsNotify(padapter, pwrpriv->ips_mode_req);
751 #endif
752                 //Enter IPS
753                 DBG_871X("%s: issue H2C to FW when entering IPS\n", __func__);
754                 parm[0] = 0x03;
755                 parm[1] = 0x01;
756                 parm[2] = 0x01;
757                 pHalFunc->fill_h2c_cmd(padapter, //H2C_FWLPS_IN_IPS_,
758                                         H2C_INACTIVE_PS_,
759                                         H2C_INACTIVE_PS_LEN, parm);
760                 //poll 0x1cc to make sure H2C command already finished by FW; MAC_0x1cc=0 means H2C done by FW.
761                 do{
762                         val8 = rtw_read8(padapter, REG_HMETFR);
763                         cnt++;
764                         DBG_871X("%s  polling REG_HMETFR=0x%x, cnt=%d \n",
765                                         __func__, val8, cnt);
766                         rtw_mdelay_os(10);
767                 }while(cnt<100 && (val8!=0));
768
769                 //H2C done, enter 32k
770                 if (val8 == 0) {
771                         //ser rpwm to enter 32k
772                         val8 = rtw_read8(padapter, SDIO_LOCAL_BASE|SDIO_REG_HRPWM1);
773                         DBG_871X("%s: read rpwm=%02x\n", __FUNCTION__, val8);
774                         val8 += 0x80;
775                         val8 |= BIT(0);
776                         rtw_write8(padapter, SDIO_LOCAL_BASE|SDIO_REG_HRPWM1, val8);
777                         DBG_871X("%s: write rpwm=%02x\n", __FUNCTION__, val8);
778                         adapter_to_pwrctl(padapter)->tog = (val8 + 0x80) & 0x80;
779                         cnt = val8 = 0;
780                         do {
781                                 val8 = rtw_read8(padapter, REG_CR);
782                                 cnt++;
783                                 DBG_871X("%s  polling 0x100=0x%x, cnt=%d \n",
784                                                 __func__, val8, cnt);
785                                 DBG_871X("%s 0x08:%02x, 0x03:%02x\n",
786                                                 __func__,
787                                                 rtw_read8(padapter, 0x08),
788                                                 rtw_read8(padapter, 0x03));
789                                 rtw_mdelay_os(10);
790                         } while(cnt<20 && (val8!=0xEA));
791 #ifdef DBG_CHECK_FW_PS_STATE
792                         if(val8 != 0xEA) {
793                                 DBG_871X("MAC_1B8=0x%08x\n",
794                                                 rtw_read32(padapter, 0x1b8));
795                                 DBG_871X("MAC_1C0=%08x, MAC_1C4=%08x, MAC_1C8=%08x, MAC_1CC=%08x\n",
796                                                 rtw_read32(padapter, 0x1c0),
797                                                 rtw_read32(padapter, 0x1c4),
798                                                 rtw_read32(padapter, 0x1c8),
799                                                 rtw_read32(padapter, 0x1cc));
800 #endif //DBG_CHECK_FW_PS_STATE
801                         } else {
802                                 DBG_871X("MAC_1C0=%08x, MAC_1C4=%08x, MAC_1C8=%08x, MAC_1CC=%08x\n",
803                                                 rtw_read32(padapter, 0x1c0),
804                                                 rtw_read32(padapter, 0x1c4),
805                                                 rtw_read32(padapter, 0x1c8),
806                                                 rtw_read32(padapter, 0x1cc));
807                         }
808                 }
809         } else {
810                 //Leave IPS
811                 DBG_871X("%s: Leaving IPS in FWLPS state\n", __func__);
812
813                 //for polling cpwm
814                 cpwm_orig = 0;
815                 rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_orig);
816
817                 //ser rpwm
818                 val8 = rtw_read8(padapter, SDIO_LOCAL_BASE|SDIO_REG_HRPWM1);
819                 val8 &= 0x80;
820                 val8 += 0x80;
821                 val8 |= BIT(6);
822                 rtw_write8(padapter, SDIO_LOCAL_BASE|SDIO_REG_HRPWM1, val8);
823                 DBG_871X("%s: write rpwm=%02x\n", __FUNCTION__, val8);
824                 adapter_to_pwrctl(padapter)->tog = (val8 + 0x80) & 0x80;
825
826                 //do polling cpwm
827                 start_time = rtw_get_current_time();
828                 do {
829
830                         rtw_mdelay_os(1);
831
832                         rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_now);
833                         if ((cpwm_orig ^ cpwm_now) & 0x80) {
834 #ifdef DBG_CHECK_FW_PS_STATE                            
835                                 DBG_871X("%s: polling cpwm ok when leaving IPS in FWLPS state, cpwm_orig=%02x, cpwm_now=%02x, 0x100=0x%x \n"
836                                 , __FUNCTION__, cpwm_orig, cpwm_now, rtw_read8(padapter, REG_CR));
837 #endif //DBG_CHECK_FW_PS_STATE
838                                 break;
839                         }
840
841                         if (rtw_get_passing_time_ms(start_time) > 100)
842                         {
843                                 DBG_871X("%s: polling cpwm timeout when leaving IPS in FWLPS state\n", __FUNCTION__);
844                                 break;
845                         }
846                 } while (1);
847
848                 parm[0] = 0x0;
849                 parm[1] = 0x0;
850                 parm[2] = 0x0;
851                 pHalFunc->fill_h2c_cmd(padapter, H2C_INACTIVE_PS_,
852                                         H2C_INACTIVE_PS_LEN, parm);
853 #ifdef CONFIG_BT_COEXIST
854                 rtw_btcoex_IpsNotify(padapter, IPS_NONE);
855 #endif
856         }
857 }
858 #endif //CONFIG_PNO_SUPPORT
859
860 void rtw_set_ps_mode(PADAPTER padapter, u8 ps_mode, u8 smart_ps, u8 bcn_ant_mode, const char *msg)
861 {
862         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
863         struct dvobj_priv *psdpriv = padapter->dvobj;
864         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
865 #ifdef CONFIG_P2P
866         struct wifidirect_info  *pwdinfo = &( padapter->wdinfo );
867 #endif //CONFIG_P2P
868 #ifdef CONFIG_TDLS
869         struct sta_priv *pstapriv = &padapter->stapriv;
870         _irqL irqL;
871         int i, j;
872         _list   *plist, *phead;
873         struct sta_info *ptdls_sta;
874 #endif //CONFIG_TDLS
875
876 _func_enter_;
877
878         RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
879                          ("%s: PowerMode=%d Smart_PS=%d\n",
880                           __FUNCTION__, ps_mode, smart_ps));
881
882         if(ps_mode > PM_Card_Disable) {
883                 RT_TRACE(_module_rtl871x_pwrctrl_c_,_drv_err_,("ps_mode:%d error\n", ps_mode));
884                 return;
885         }
886
887         if (pwrpriv->pwr_mode == ps_mode)
888         {
889                 if (PS_MODE_ACTIVE == ps_mode) return;
890
891 #ifndef CONFIG_BT_COEXIST
892                 if ((pwrpriv->smart_ps == smart_ps) &&
893                         (pwrpriv->bcn_ant_mode == bcn_ant_mode))
894                 {
895                         return;
896                 }
897 #endif // !CONFIG_BT_COEXIST
898         }
899
900 #ifdef CONFIG_LPS_LCLK
901         _enter_pwrlock(&pwrpriv->lock);
902 #endif
903
904         //if(pwrpriv->pwr_mode == PS_MODE_ACTIVE)
905         if(ps_mode == PS_MODE_ACTIVE)
906         {
907                 if (1
908 #ifdef CONFIG_BT_COEXIST
909                         && (((rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
910 #ifdef CONFIG_P2P_PS
911                                         && (pwdinfo->opp_ps == 0)
912 #endif // CONFIG_P2P_PS
913                                         )
914                                 || ((rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
915                                         && (rtw_btcoex_IsLpsOn(padapter) == _FALSE))
916                                 )
917 #else // !CONFIG_BT_COEXIST
918 #ifdef CONFIG_P2P_PS
919                         && (pwdinfo->opp_ps == 0)
920 #endif // CONFIG_P2P_PS
921 #endif // !CONFIG_BT_COEXIST
922                         )
923                 {
924                         DBG_871X(FUNC_ADPT_FMT" Leave 802.11 power save - %s\n",
925                                 FUNC_ADPT_ARG(padapter), msg);
926
927 #ifdef CONFIG_TDLS
928                         _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
929
930                         for(i=0; i< NUM_STA; i++)
931                         {
932                                 phead = &(pstapriv->sta_hash[i]);
933                                 plist = get_next(phead);
934
935                                 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE)
936                                 {
937                                         ptdls_sta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
938
939                                         if( ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE )
940                                                 issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta->hwaddr, 0, 0, 0);
941                                         plist = get_next(plist);
942                                 }
943                         }
944
945                         _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
946 #endif //CONFIG_TDLS
947
948                         pwrpriv->pwr_mode = ps_mode;
949                         rtw_set_rpwm(padapter, PS_STATE_S4);
950                         
951 #if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) || defined(CONFIG_P2P_WOWLAN)
952                         if (pwrpriv->wowlan_mode == _TRUE ||
953                                         pwrpriv->wowlan_ap_mode == _TRUE ||
954                                         pwrpriv->wowlan_p2p_mode == _TRUE)
955                         {
956                                 u32 start_time, delay_ms;
957                                 u8 val8;
958                                 delay_ms = 20;
959                                 start_time = rtw_get_current_time();
960                                 do { 
961                                         rtw_hal_get_hwreg(padapter, HW_VAR_SYS_CLKR, &val8);
962                                         if (!(val8 & BIT(4))){ //0x08 bit4 =1 --> in 32k, bit4 = 0 --> leave 32k
963                                                 pwrpriv->cpwm = PS_STATE_S4;
964                                                 break;
965                                         }
966                                         if (rtw_get_passing_time_ms(start_time) > delay_ms)
967                                         {
968                                                 DBG_871X("%s: Wait for FW 32K leave more than %u ms!!!\n", 
969                                                                 __FUNCTION__, delay_ms);
970                                                 pdbgpriv->dbg_wow_leave_ps_fail_cnt++;
971                                                 break;
972                                         }
973                                         rtw_usleep_os(100);
974                                 } while (1); 
975                         }
976 #endif
977                         rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
978                         pwrpriv->bFwCurrentInPSMode = _FALSE;
979
980 #ifdef CONFIG_BT_COEXIST
981                         rtw_btcoex_LpsNotify(padapter, ps_mode);
982 #endif // CONFIG_BT_COEXIST
983                 }
984         }
985         else
986         {
987                 if ((PS_RDY_CHECK(padapter) && check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE))
988 #ifdef CONFIG_BT_COEXIST
989                         || ((rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
990                                 && (rtw_btcoex_IsLpsOn(padapter) == _TRUE))
991 #endif
992 #ifdef CONFIG_P2P_WOWLAN
993                         ||( _TRUE == pwrpriv->wowlan_p2p_mode)
994 #endif //CONFIG_P2P_WOWLAN
995                         )
996                 {
997                         u8 pslv;
998
999                         DBG_871X(FUNC_ADPT_FMT" Enter 802.11 power save - %s\n",
1000                                 FUNC_ADPT_ARG(padapter), msg);
1001
1002 #ifdef CONFIG_TDLS
1003                         _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
1004
1005                         for(i=0; i< NUM_STA; i++)
1006                         {
1007                                 phead = &(pstapriv->sta_hash[i]);
1008                                 plist = get_next(phead);
1009
1010                                 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE)
1011                                 {
1012                                         ptdls_sta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
1013
1014                                         if( ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE )
1015                                                 issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta->hwaddr, 1, 0, 0);
1016                                         plist = get_next(plist);
1017                                 }
1018                         }
1019
1020                         _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
1021 #endif //CONFIG_TDLS
1022
1023 #ifdef CONFIG_BT_COEXIST
1024                         rtw_btcoex_LpsNotify(padapter, ps_mode);
1025 #endif // CONFIG_BT_COEXIST
1026
1027                         pwrpriv->bFwCurrentInPSMode = _TRUE;
1028                         pwrpriv->pwr_mode = ps_mode;
1029                         pwrpriv->smart_ps = smart_ps;
1030                         pwrpriv->bcn_ant_mode = bcn_ant_mode;
1031                         rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
1032
1033 #ifdef CONFIG_P2P_PS
1034                         // Set CTWindow after LPS
1035                         if(pwdinfo->opp_ps == 1)
1036                                 p2p_ps_wk_cmd(padapter, P2P_PS_ENABLE, 0);
1037 #endif //CONFIG_P2P_PS
1038
1039                         pslv = PS_STATE_S2;
1040 #ifdef CONFIG_LPS_LCLK
1041                         if (pwrpriv->alives == 0)
1042                                 pslv = PS_STATE_S0;
1043 #endif // CONFIG_LPS_LCLK
1044
1045 #ifdef CONFIG_BT_COEXIST
1046                         if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
1047                                 && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE))
1048                         {
1049                                 u8 val8;
1050
1051                                 val8 = rtw_btcoex_LpsVal(padapter);
1052                                 if (val8 & BIT(4))
1053                                         pslv = PS_STATE_S2;
1054
1055 #ifdef CONFIG_RTL8723A
1056                                 val8 = rtw_btcoex_RpwmVal(padapter);
1057                                 switch (val8)
1058                                 {
1059                                         case 0x4:
1060                                                 pslv = PS_STATE_S3;
1061                                                 break;
1062
1063                                         case 0xC:
1064                                                 pslv = PS_STATE_S4;
1065                                                 break;
1066                                 }
1067 #endif // CONFIG_RTL8723A
1068                         }
1069 #endif // CONFIG_BT_COEXIST
1070
1071                         rtw_set_rpwm(padapter, pslv);
1072                 }
1073         }
1074
1075 #ifdef CONFIG_LPS_LCLK
1076         _exit_pwrlock(&pwrpriv->lock);
1077 #endif
1078
1079 _func_exit_;
1080 }
1081
1082 /*
1083  * Return:
1084  *      0:      Leave OK
1085  *      -1:     Timeout
1086  *      -2:     Other error
1087  */
1088 s32 LPS_RF_ON_check(PADAPTER padapter, u32 delay_ms)
1089 {
1090         u32 start_time;
1091         u8 bAwake = _FALSE;
1092         s32 err = 0;
1093
1094
1095         start_time = rtw_get_current_time();
1096         while (1)
1097         {
1098                 rtw_hal_get_hwreg(padapter, HW_VAR_FWLPS_RF_ON, &bAwake);
1099                 if (_TRUE == bAwake)
1100                         break;
1101
1102                 if (_TRUE == padapter->bSurpriseRemoved)
1103                 {
1104                         err = -2;
1105                         DBG_871X("%s: device surprise removed!!\n", __FUNCTION__);
1106                         break;
1107                 }
1108
1109                 if (rtw_get_passing_time_ms(start_time) > delay_ms)
1110                 {
1111                         err = -1;
1112                         DBG_871X("%s: Wait for FW LPS leave more than %u ms!!!\n", __FUNCTION__, delay_ms);
1113                         break;
1114                 }
1115                 rtw_usleep_os(100);
1116         }
1117
1118         return err;
1119 }
1120
1121 //
1122 //      Description:
1123 //              Enter the leisure power save mode.
1124 //
1125 void LPS_Enter(PADAPTER padapter, const char *msg)
1126 {
1127         struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1128         struct pwrctrl_priv     *pwrpriv = dvobj_to_pwrctl(dvobj);
1129         struct mlme_priv        *pmlmepriv = &(padapter->mlmepriv);
1130         _adapter *buddy = padapter->pbuddy_adapter;
1131         int n_assoc_iface = 0;
1132         int i;
1133         char buf[32] = {0};
1134
1135 _func_enter_;
1136
1137 //      DBG_871X("+LeisurePSEnter\n");
1138
1139 #ifdef CONFIG_BT_COEXIST
1140         if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1141                 return;
1142 #endif
1143
1144         /* Skip lps enter request if number of assocated adapters is not 1 */
1145         for (i = 0; i < dvobj->iface_nums; i++) {
1146                 if (check_fwstate(&(dvobj->padapters[i]->mlmepriv), WIFI_ASOC_STATE))
1147                         n_assoc_iface++;
1148         }
1149         if (n_assoc_iface != 1)
1150                 return;
1151
1152         /* Skip lps enter request for adapter not port0 */
1153         if (get_iface_type(padapter) != IFACE_PORT0)
1154                 return;
1155
1156         for (i = 0; i < dvobj->iface_nums; i++) {
1157                 if (PS_RDY_CHECK(dvobj->padapters[i]) == _FALSE)
1158                         return;
1159         }
1160
1161 #ifdef CONFIG_P2P_PS
1162         if(padapter->wdinfo.p2p_ps_mode == P2P_PS_NOA)
1163         {
1164                 return;//supporting p2p client ps NOA via H2C_8723B_P2P_PS_OFFLOAD 
1165         }
1166 #endif //CONFIG_P2P_PS
1167
1168         if (pwrpriv->bLeisurePs)
1169         {
1170                 // Idle for a while if we connect to AP a while ago.
1171                 if (pwrpriv->LpsIdleCount >= 2) //  4 Sec
1172                 {
1173                         if(pwrpriv->pwr_mode == PS_MODE_ACTIVE)
1174                         {
1175                                 sprintf(buf, "WIFI-%s", msg);
1176                                 pwrpriv->bpower_saving = _TRUE;
1177                                 rtw_set_ps_mode(padapter, pwrpriv->power_mgnt, padapter->registrypriv.smart_ps, 0, buf);
1178                         }
1179                 }
1180                 else
1181                         pwrpriv->LpsIdleCount++;
1182         }
1183
1184 //      DBG_871X("-LeisurePSEnter\n");
1185
1186 _func_exit_;
1187 }
1188
1189 //
1190 //      Description:
1191 //              Leave the leisure power save mode.
1192 //
1193 void LPS_Leave(PADAPTER padapter, const char *msg)
1194 {
1195 #define LPS_LEAVE_TIMEOUT_MS 100
1196
1197         struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1198         struct pwrctrl_priv     *pwrpriv = dvobj_to_pwrctl(dvobj);
1199         u32 start_time;
1200         u8 bAwake = _FALSE;
1201         char buf[32] = {0};
1202         struct debug_priv *pdbgpriv = &dvobj->drv_dbg;
1203
1204 _func_enter_;
1205
1206 //      DBG_871X("+LeisurePSLeave\n");
1207
1208 #ifdef CONFIG_BT_COEXIST
1209         if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1210                 return;
1211 #endif
1212
1213         if (pwrpriv->bLeisurePs)
1214         {
1215                 if(pwrpriv->pwr_mode != PS_MODE_ACTIVE)
1216                 {
1217                         sprintf(buf, "WIFI-%s", msg);
1218                         rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0, buf);
1219
1220                         if(pwrpriv->pwr_mode == PS_MODE_ACTIVE)
1221                                 LPS_RF_ON_check(padapter, LPS_LEAVE_TIMEOUT_MS);
1222                 }
1223         }
1224
1225         pwrpriv->bpower_saving = _FALSE;
1226 #ifdef DBG_CHECK_FW_PS_STATE
1227         if(rtw_fw_ps_state(padapter) == _FAIL)
1228         {
1229                 DBG_871X("leave lps, fw in 32k\n");
1230                 pdbgpriv->dbg_leave_lps_fail_cnt++;
1231         }
1232 #endif //DBG_CHECK_FW_PS_STATE
1233 //      DBG_871X("-LeisurePSLeave\n");
1234
1235 _func_exit_;
1236 }
1237 #endif
1238
1239 void LeaveAllPowerSaveModeDirect(PADAPTER Adapter)
1240 {
1241         PADAPTER pri_padapter = GET_PRIMARY_ADAPTER(Adapter);
1242         struct mlme_priv        *pmlmepriv = &(Adapter->mlmepriv);
1243         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(Adapter);
1244         struct dvobj_priv *psdpriv = Adapter->dvobj;
1245         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
1246 #ifndef CONFIG_DETECT_CPWM_BY_POLLING
1247         u8 cpwm_orig, cpwm_now;
1248         u32 start_time;
1249 #endif // CONFIG_DETECT_CPWM_BY_POLLING
1250
1251 _func_enter_;
1252
1253         DBG_871X("%s.....\n",__FUNCTION__);
1254
1255         if (_TRUE == Adapter->bSurpriseRemoved)
1256         {
1257                 DBG_871X(FUNC_ADPT_FMT ": bSurpriseRemoved=%d Skip!\n",
1258                         FUNC_ADPT_ARG(Adapter), Adapter->bSurpriseRemoved);
1259                 return;
1260         }
1261
1262         if ((check_fwstate(pmlmepriv, _FW_LINKED) == _TRUE)
1263 #ifdef CONFIG_CONCURRENT_MODE
1264                 || (check_buddy_fwstate(Adapter,_FW_LINKED) == _TRUE)
1265 #endif
1266                 )
1267         { //connect
1268
1269                 if(pwrpriv->pwr_mode == PS_MODE_ACTIVE) {
1270                         DBG_871X("%s: Driver Already Leave LPS\n",__FUNCTION__);
1271                         return;
1272                 }
1273
1274 #ifdef CONFIG_LPS_LCLK
1275                 _enter_pwrlock(&pwrpriv->lock);
1276
1277 #ifndef CONFIG_DETECT_CPWM_BY_POLLING
1278                 cpwm_orig = 0;
1279                 rtw_hal_get_hwreg(Adapter, HW_VAR_CPWM, &cpwm_orig);
1280 #endif //CONFIG_DETECT_CPWM_BY_POLLING
1281                 rtw_set_rpwm(Adapter, PS_STATE_S4);
1282
1283 #ifndef CONFIG_DETECT_CPWM_BY_POLLING
1284
1285                 start_time = rtw_get_current_time();
1286
1287                 // polling cpwm
1288                 do {
1289                         rtw_mdelay_os(1);
1290
1291                         rtw_hal_get_hwreg(Adapter, HW_VAR_CPWM, &cpwm_now);
1292                         if ((cpwm_orig ^ cpwm_now) & 0x80)
1293                         {
1294 #ifdef CONFIG_RTL8723A
1295                                 pwrpriv->cpwm = PS_STATE(cpwm_now);
1296 #else // !CONFIG_RTL8723A
1297                                 pwrpriv->cpwm = PS_STATE_S4;
1298 #endif // !CONFIG_RTL8723A
1299                                 pwrpriv->cpwm_tog = cpwm_now & PS_TOGGLE;
1300 #ifdef DBG_CHECK_FW_PS_STATE
1301                                 DBG_871X("%s: polling cpwm OK! cpwm_orig=%02x, cpwm_now=%02x, 0x100=0x%x \n"
1302                                 , __FUNCTION__, cpwm_orig, cpwm_now, rtw_read8(Adapter, REG_CR));
1303                                 if(rtw_fw_ps_state(Adapter) == _FAIL)
1304                                 {
1305                                         DBG_871X("%s: leave 32k but fw state in 32k\n", __FUNCTION__);
1306                                         pdbgpriv->dbg_rpwm_toogle_cnt++;
1307                                 }
1308 #endif //DBG_CHECK_FW_PS_STATE
1309                                 break;
1310                         }
1311
1312                         if (rtw_get_passing_time_ms(start_time) > LPS_RPWM_WAIT_MS)
1313                         {
1314                                 DBG_871X("%s: polling cpwm timeout! cpwm_orig=%02x, cpwm_now=%02x \n", __FUNCTION__, cpwm_orig, cpwm_now);
1315 #ifdef DBG_CHECK_FW_PS_STATE
1316                                 if(rtw_fw_ps_state(Adapter) == _FAIL)
1317                                 {
1318                                         DBG_871X("rpwm timeout and fw ps state in 32k\n");
1319                                         pdbgpriv->dbg_rpwm_timeout_fail_cnt++;
1320                                 }
1321 #endif //DBG_CHECK_FW_PS_STATE
1322                                 break;
1323                         }
1324                 } while (1);
1325 #endif // CONFIG_DETECT_CPWM_BY_POLLING
1326
1327         _exit_pwrlock(&pwrpriv->lock);
1328 #endif
1329
1330 #ifdef CONFIG_P2P_PS
1331                 p2p_ps_wk_cmd(pri_padapter, P2P_PS_DISABLE, 0);
1332 #endif //CONFIG_P2P_PS
1333
1334 #ifdef CONFIG_LPS
1335                 rtw_lps_ctrl_wk_cmd(pri_padapter, LPS_CTRL_LEAVE, 0);
1336 #endif
1337         }
1338         else
1339         {
1340                 if(pwrpriv->rf_pwrstate== rf_off)
1341                 {
1342                         #ifdef CONFIG_AUTOSUSPEND
1343                         if(Adapter->registrypriv.usbss_enable)
1344                         {
1345                                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1346                                 usb_disable_autosuspend(adapter_to_dvobj(Adapter)->pusbdev);
1347                                 #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,22) && LINUX_VERSION_CODE<=KERNEL_VERSION(2,6,34))
1348                                 adapter_to_dvobj(Adapter)->pusbdev->autosuspend_disabled = Adapter->bDisableAutosuspend;//autosuspend disabled by the user
1349                                 #endif
1350                         }
1351                         else
1352                         #endif
1353                         {
1354 #if defined(CONFIG_FWLPS_IN_IPS) || defined(CONFIG_SWLPS_IN_IPS) || defined(CONFIG_RTL8188E)
1355                                 #ifdef CONFIG_IPS
1356                                 if(_FALSE == ips_leave(pri_padapter))
1357                                 {
1358                                         DBG_871X("======> ips_leave fail.............\n");                      
1359                                 }
1360                                 #endif
1361 #endif //CONFIG_SWLPS_IN_IPS || (CONFIG_PLATFORM_SPRD && CONFIG_RTL8188E)
1362                         }
1363                 }
1364         }
1365
1366 _func_exit_;
1367 }
1368
1369 //
1370 // Description: Leave all power save mode: LPS, FwLPS, IPS if needed.
1371 // Move code to function by tynli. 2010.03.26. 
1372 //
1373 void LeaveAllPowerSaveMode(IN PADAPTER Adapter)
1374 {
1375         struct dvobj_priv *dvobj = adapter_to_dvobj(Adapter);
1376         struct mlme_priv        *pmlmepriv = &(Adapter->mlmepriv);
1377         u8      enqueue = 0;
1378         int n_assoc_iface = 0;
1379         int i;
1380
1381 _func_enter_;
1382
1383         //DBG_871X("%s.....\n",__FUNCTION__);
1384
1385         if (_FALSE == Adapter->bup)
1386         {
1387                 DBG_871X(FUNC_ADPT_FMT ": bup=%d Skip!\n",
1388                         FUNC_ADPT_ARG(Adapter), Adapter->bup);
1389                 return;
1390         }
1391
1392         if (_TRUE == Adapter->bSurpriseRemoved)
1393         {
1394                 DBG_871X(FUNC_ADPT_FMT ": bSurpriseRemoved=%d Skip!\n",
1395                         FUNC_ADPT_ARG(Adapter), Adapter->bSurpriseRemoved);
1396                 return;
1397         }
1398
1399         for (i = 0; i < dvobj->iface_nums; i++) {
1400                 if (check_fwstate(&(dvobj->padapters[i]->mlmepriv), WIFI_ASOC_STATE))
1401                         n_assoc_iface++;
1402         }
1403
1404         if (n_assoc_iface)
1405         { //connect
1406 #ifdef CONFIG_LPS_LCLK
1407                 enqueue = 1;
1408 #endif
1409
1410 #ifdef CONFIG_P2P_PS
1411                 p2p_ps_wk_cmd(Adapter, P2P_PS_DISABLE, enqueue);
1412 #endif //CONFIG_P2P_PS
1413
1414 #ifdef CONFIG_LPS
1415                 rtw_lps_ctrl_wk_cmd(Adapter, LPS_CTRL_LEAVE, enqueue);
1416 #endif
1417
1418 #ifdef CONFIG_LPS_LCLK
1419                 LPS_Leave_check(Adapter);
1420 #endif  
1421         }
1422         else
1423         {
1424                 if(adapter_to_pwrctl(Adapter)->rf_pwrstate== rf_off)
1425                 {
1426                         #ifdef CONFIG_AUTOSUSPEND
1427                         if(Adapter->registrypriv.usbss_enable)
1428                         {
1429                                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1430                                 usb_disable_autosuspend(adapter_to_dvobj(Adapter)->pusbdev);
1431                                 #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,22) && LINUX_VERSION_CODE<=KERNEL_VERSION(2,6,34))
1432                                 adapter_to_dvobj(Adapter)->pusbdev->autosuspend_disabled = Adapter->bDisableAutosuspend;//autosuspend disabled by the user
1433                                 #endif
1434                         }
1435                         else
1436                         #endif
1437                         {
1438 #if defined(CONFIG_FWLPS_IN_IPS) || defined(CONFIG_SWLPS_IN_IPS) || (defined(CONFIG_PLATFORM_SPRD) && defined(CONFIG_RTL8188E))
1439                                 #ifdef CONFIG_IPS
1440                                 if(_FALSE == ips_leave(Adapter))
1441                                 {
1442                                         DBG_871X("======> ips_leave fail.............\n");                      
1443                                 }
1444                                 #endif
1445 #endif //CONFIG_SWLPS_IN_IPS || (CONFIG_PLATFORM_SPRD && CONFIG_RTL8188E)
1446                         }                               
1447                 }       
1448         }
1449
1450 _func_exit_;
1451 }
1452
1453 #ifdef CONFIG_LPS_LCLK
1454 void LPS_Leave_check(
1455         PADAPTER padapter)
1456 {
1457         struct pwrctrl_priv *pwrpriv;
1458         u32     start_time;
1459         u8      bReady;
1460
1461 _func_enter_;
1462
1463         pwrpriv = adapter_to_pwrctl(padapter);
1464
1465         bReady = _FALSE;
1466         start_time = rtw_get_current_time();
1467
1468         rtw_yield_os();
1469         
1470         while(1)
1471         {
1472                 _enter_pwrlock(&pwrpriv->lock);
1473
1474                 if ((padapter->bSurpriseRemoved == _TRUE)
1475                         || (padapter->hw_init_completed == _FALSE)
1476 #ifdef CONFIG_USB_HCI
1477                         || (padapter->bDriverStopped== _TRUE)
1478 #endif
1479                         || (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
1480                         )
1481                 {
1482                         bReady = _TRUE;
1483                 }
1484
1485                 _exit_pwrlock(&pwrpriv->lock);
1486
1487                 if(_TRUE == bReady)
1488                         break;
1489
1490                 if(rtw_get_passing_time_ms(start_time)>100)
1491                 {
1492                         DBG_871X("Wait for cpwm event  than 100 ms!!!\n");
1493                         break;
1494                 }
1495                 rtw_msleep_os(1);
1496         }
1497
1498 _func_exit_;
1499 }
1500
1501 /*
1502  * Caller:ISR handler...
1503  *
1504  * This will be called when CPWM interrupt is up.
1505  *
1506  * using to update cpwn of drv; and drv willl make a decision to up or down pwr level
1507  */
1508 void cpwm_int_hdl(
1509         PADAPTER padapter,
1510         struct reportpwrstate_parm *preportpwrstate)
1511 {
1512         struct pwrctrl_priv *pwrpriv;
1513
1514 _func_enter_;
1515
1516         pwrpriv = adapter_to_pwrctl(padapter);
1517 #if 0
1518         if (pwrpriv->cpwm_tog == (preportpwrstate->state & PS_TOGGLE)) {
1519                 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
1520                                  ("cpwm_int_hdl: tog(old)=0x%02x cpwm(new)=0x%02x toggle bit didn't change!?\n",
1521                                   pwrpriv->cpwm_tog, preportpwrstate->state));
1522                 goto exit;
1523         }
1524 #endif
1525
1526         _enter_pwrlock(&pwrpriv->lock);
1527
1528 #ifdef CONFIG_LPS_RPWM_TIMER
1529         if (pwrpriv->rpwm < PS_STATE_S2)
1530         {
1531                 DBG_871X("%s: Redundant CPWM Int. RPWM=0x%02X CPWM=0x%02x\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1532                 _exit_pwrlock(&pwrpriv->lock);
1533                 goto exit;
1534         }
1535 #endif // CONFIG_LPS_RPWM_TIMER
1536
1537         pwrpriv->cpwm = PS_STATE(preportpwrstate->state);
1538         pwrpriv->cpwm_tog = preportpwrstate->state & PS_TOGGLE;
1539
1540         if (pwrpriv->cpwm >= PS_STATE_S2)
1541         {
1542                 if (pwrpriv->alives & CMD_ALIVE)
1543                         _rtw_up_sema(&padapter->cmdpriv.cmd_queue_sema);
1544
1545                 if (pwrpriv->alives & XMIT_ALIVE)
1546                         _rtw_up_sema(&padapter->xmitpriv.xmit_sema);
1547         }
1548
1549         _exit_pwrlock(&pwrpriv->lock);
1550
1551 exit:
1552         RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
1553                          ("cpwm_int_hdl: cpwm=0x%02x\n", pwrpriv->cpwm));
1554
1555 _func_exit_;
1556 }
1557
1558 static void cpwm_event_callback(struct work_struct *work)
1559 {
1560         struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, cpwm_event);
1561         struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
1562         _adapter *adapter = dvobj->if1;
1563         struct reportpwrstate_parm report;
1564
1565         //DBG_871X("%s\n",__FUNCTION__);
1566
1567         report.state = PS_STATE_S2;
1568         cpwm_int_hdl(adapter, &report);
1569 }
1570
1571 #ifdef CONFIG_LPS_RPWM_TIMER
1572 static void rpwmtimeout_workitem_callback(struct work_struct *work)
1573 {
1574         PADAPTER padapter;
1575         struct dvobj_priv *dvobj;
1576         struct pwrctrl_priv *pwrpriv;
1577
1578
1579         pwrpriv = container_of(work, struct pwrctrl_priv, rpwmtimeoutwi);
1580         dvobj = pwrctl_to_dvobj(pwrpriv);
1581         padapter = dvobj->if1;
1582 //      DBG_871X("+%s: rpwm=0x%02X cpwm=0x%02X\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1583
1584         _enter_pwrlock(&pwrpriv->lock);
1585         if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
1586         {
1587                 DBG_871X("%s: rpwm=0x%02X cpwm=0x%02X CPWM done!\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1588                 goto exit;
1589         }
1590         _exit_pwrlock(&pwrpriv->lock);
1591
1592         if (rtw_read8(padapter, 0x100) != 0xEA)
1593         {
1594 #if 1
1595                 struct reportpwrstate_parm report;
1596
1597                 report.state = PS_STATE_S2;
1598                 DBG_871X("\n%s: FW already leave 32K!\n\n", __func__);
1599                 cpwm_int_hdl(padapter, &report);
1600 #else
1601                 DBG_871X("\n%s: FW already leave 32K!\n\n", __func__);
1602                 cpwm_event_callback(&pwrpriv->cpwm_event);
1603 #endif
1604                 return;
1605         }
1606
1607         _enter_pwrlock(&pwrpriv->lock);
1608
1609         if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
1610         {
1611                 DBG_871X("%s: cpwm=%d, nothing to do!\n", __func__, pwrpriv->cpwm);
1612                 goto exit;
1613         }
1614         pwrpriv->brpwmtimeout = _TRUE;
1615         rtw_set_rpwm(padapter, pwrpriv->rpwm);
1616         pwrpriv->brpwmtimeout = _FALSE;
1617
1618 exit:
1619         _exit_pwrlock(&pwrpriv->lock);
1620 }
1621
1622 /*
1623  * This function is a timer handler, can't do any IO in it.
1624  */
1625 static void pwr_rpwm_timeout_handler(void *FunctionContext)
1626 {
1627         PADAPTER padapter;
1628         struct pwrctrl_priv *pwrpriv;
1629
1630
1631         padapter = (PADAPTER)FunctionContext;
1632         pwrpriv = adapter_to_pwrctl(padapter);
1633         DBG_871X("+%s: rpwm=0x%02X cpwm=0x%02X\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1634
1635         if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
1636         {
1637                 DBG_871X("+%s: cpwm=%d, nothing to do!\n", __func__, pwrpriv->cpwm);
1638                 return;
1639         }
1640
1641         _set_workitem(&pwrpriv->rpwmtimeoutwi);
1642 }
1643 #endif // CONFIG_LPS_RPWM_TIMER
1644
1645 __inline static void register_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
1646 {
1647         pwrctrl->alives |= tag;
1648 }
1649
1650 __inline static void unregister_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
1651 {
1652         pwrctrl->alives &= ~tag;
1653 }
1654
1655
1656 /*
1657  * Description:
1658  *      Check if the fw_pwrstate is okay for I/O.
1659  *      If not (cpwm is less than S2), then the sub-routine
1660  *      will raise the cpwm to be greater than or equal to S2.
1661  *
1662  *      Calling Context: Passive
1663  *
1664  *      Constraint:
1665  *              1. this function will request pwrctrl->lock
1666  * 
1667  * Return Value:
1668  *      _SUCCESS        hardware is ready for I/O
1669  *      _FAIL           can't I/O right now
1670  */
1671 s32 rtw_register_task_alive(PADAPTER padapter, u32 task)
1672 {
1673         s32 res;
1674         struct pwrctrl_priv *pwrctrl;
1675         u8 pslv;
1676
1677 _func_enter_;
1678
1679         res = _SUCCESS;
1680         pwrctrl = adapter_to_pwrctl(padapter);
1681         pslv = PS_STATE_S2;
1682
1683 #if defined(CONFIG_RTL8723A) && defined(CONFIG_BT_COEXIST)
1684         if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1685         {
1686                 u8 btcoex_rpwm;
1687                 btcoex_rpwm = rtw_btcoex_RpwmVal(padapter);
1688                 switch (btcoex_rpwm)
1689                 {
1690                         case 0x4:
1691                                 pslv = PS_STATE_S3;
1692                                 break;
1693
1694                         case 0xC:
1695                                 pslv = PS_STATE_S4;
1696                                 break;
1697                 }
1698         }
1699 #endif // CONFIG_RTL8723A & CONFIG_BT_COEXIST
1700
1701         _enter_pwrlock(&pwrctrl->lock);
1702
1703         register_task_alive(pwrctrl, task);
1704
1705         if (pwrctrl->bFwCurrentInPSMode == _TRUE)
1706         {
1707                 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
1708                                  ("%s: task=0x%x cpwm=0x%02x alives=0x%08x\n",
1709                                   __FUNCTION__, task, pwrctrl->cpwm, pwrctrl->alives));
1710
1711                 if (pwrctrl->cpwm < pslv)
1712                 {
1713                         if (pwrctrl->cpwm < PS_STATE_S2)
1714                                 res = _FAIL;
1715                         if (pwrctrl->rpwm < pslv)
1716                                 rtw_set_rpwm(padapter, pslv);
1717                 }
1718         }
1719
1720         _exit_pwrlock(&pwrctrl->lock);
1721
1722 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1723         if (_FAIL == res)
1724         {
1725                 if (pwrctrl->cpwm >= PS_STATE_S2)
1726                         res = _SUCCESS;
1727         }
1728 #endif // CONFIG_DETECT_CPWM_BY_POLLING
1729
1730 _func_exit_;
1731
1732         return res;     
1733 }
1734
1735 /*
1736  * Description:
1737  *      If task is done, call this func. to power down firmware again.
1738  *
1739  *      Constraint:
1740  *              1. this function will request pwrctrl->lock
1741  *
1742  * Return Value:
1743  *      none
1744  */
1745 void rtw_unregister_task_alive(PADAPTER padapter, u32 task)
1746 {
1747         struct pwrctrl_priv *pwrctrl;
1748         u8 pslv;
1749
1750 _func_enter_;
1751
1752         pwrctrl = adapter_to_pwrctl(padapter);
1753         pslv = PS_STATE_S0;
1754
1755 #ifdef CONFIG_BT_COEXIST
1756         if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
1757                 && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE))
1758         {
1759                 u8 val8;
1760
1761                 val8 = rtw_btcoex_LpsVal(padapter);
1762                 if (val8 & BIT(4))
1763                         pslv = PS_STATE_S2;
1764
1765 #ifdef CONFIG_RTL8723A
1766                 val8 = rtw_btcoex_RpwmVal(padapter);
1767                 switch (val8)
1768                 {
1769                         case 0x4:
1770                                 pslv = PS_STATE_S3;
1771                                 break;
1772
1773                         case 0xC:
1774                                 pslv = PS_STATE_S4;
1775                                 break;
1776                 }
1777 #endif // CONFIG_RTL8723A
1778         }
1779 #endif // CONFIG_BT_COEXIST
1780
1781         _enter_pwrlock(&pwrctrl->lock);
1782
1783         unregister_task_alive(pwrctrl, task);
1784
1785         if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
1786                 && (pwrctrl->bFwCurrentInPSMode == _TRUE))
1787         {
1788                 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
1789                                  ("%s: cpwm=0x%02x alives=0x%08x\n",
1790                                   __FUNCTION__, pwrctrl->cpwm, pwrctrl->alives));
1791
1792                 if (pwrctrl->cpwm > pslv)
1793                 {
1794                         if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
1795                                 rtw_set_rpwm(padapter, pslv);
1796                 }
1797         }
1798
1799         _exit_pwrlock(&pwrctrl->lock);
1800
1801 _func_exit_;
1802 }
1803
1804 /*
1805  * Caller: rtw_xmit_thread
1806  * 
1807  * Check if the fw_pwrstate is okay for xmit.
1808  * If not (cpwm is less than S3), then the sub-routine
1809  * will raise the cpwm to be greater than or equal to S3. 
1810  *
1811  * Calling Context: Passive
1812  * 
1813  * Return Value:
1814  *       _SUCCESS       rtw_xmit_thread can write fifo/txcmd afterwards.
1815  *       _FAIL          rtw_xmit_thread can not do anything.
1816  */
1817 s32 rtw_register_tx_alive(PADAPTER padapter)
1818 {
1819         s32 res;
1820         struct pwrctrl_priv *pwrctrl;
1821         u8 pslv;
1822
1823 _func_enter_;
1824
1825         res = _SUCCESS;
1826         pwrctrl = adapter_to_pwrctl(padapter);
1827         pslv = PS_STATE_S2;
1828
1829 #if defined(CONFIG_RTL8723A) && defined(CONFIG_BT_COEXIST)
1830         if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1831         {
1832                 u8 btcoex_rpwm;
1833                 btcoex_rpwm = rtw_btcoex_RpwmVal(padapter);
1834                 switch (btcoex_rpwm)
1835                 {
1836                         case 0x4:
1837                                 pslv = PS_STATE_S3;
1838                                 break;
1839
1840                         case 0xC:
1841                                 pslv = PS_STATE_S4;
1842                                 break;
1843                 }
1844         }
1845 #endif // CONFIG_RTL8723A & CONFIG_BT_COEXIST
1846
1847         _enter_pwrlock(&pwrctrl->lock);
1848
1849         register_task_alive(pwrctrl, XMIT_ALIVE);
1850
1851         if (pwrctrl->bFwCurrentInPSMode == _TRUE)
1852         {
1853                 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
1854                                  ("rtw_register_tx_alive: cpwm=0x%02x alives=0x%08x\n",
1855                                   pwrctrl->cpwm, pwrctrl->alives));
1856
1857                 if (pwrctrl->cpwm < pslv)
1858                 {
1859                         if (pwrctrl->cpwm < PS_STATE_S2)
1860                                 res = _FAIL;
1861                         if (pwrctrl->rpwm < pslv)
1862                                 rtw_set_rpwm(padapter, pslv);
1863                 }
1864         }
1865
1866         _exit_pwrlock(&pwrctrl->lock);
1867
1868 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1869         if (_FAIL == res)
1870         {
1871                 if (pwrctrl->cpwm >= PS_STATE_S2)
1872                         res = _SUCCESS;
1873         }
1874 #endif // CONFIG_DETECT_CPWM_BY_POLLING
1875
1876 _func_exit_;
1877
1878         return res;     
1879 }
1880
1881 /*
1882  * Caller: rtw_cmd_thread
1883  *
1884  * Check if the fw_pwrstate is okay for issuing cmd.
1885  * If not (cpwm should be is less than S2), then the sub-routine
1886  * will raise the cpwm to be greater than or equal to S2.
1887  *
1888  * Calling Context: Passive
1889  *
1890  * Return Value:
1891  *      _SUCCESS        rtw_cmd_thread can issue cmds to firmware afterwards.
1892  *      _FAIL           rtw_cmd_thread can not do anything.
1893  */
1894 s32 rtw_register_cmd_alive(PADAPTER padapter)
1895 {
1896         s32 res;
1897         struct pwrctrl_priv *pwrctrl;
1898         u8 pslv;
1899
1900 _func_enter_;
1901
1902         res = _SUCCESS;
1903         pwrctrl = adapter_to_pwrctl(padapter);
1904         pslv = PS_STATE_S2;
1905
1906 #if defined(CONFIG_RTL8723A) && defined(CONFIG_BT_COEXIST)
1907         if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1908         {
1909                 u8 btcoex_rpwm;
1910                 btcoex_rpwm = rtw_btcoex_RpwmVal(padapter);
1911                 switch (btcoex_rpwm)
1912                 {
1913                         case 0x4:
1914                                 pslv = PS_STATE_S3;
1915                                 break;
1916
1917                         case 0xC:
1918                                 pslv = PS_STATE_S4;
1919                                 break;
1920                 }
1921         }
1922 #endif // CONFIG_RTL8723A & CONFIG_BT_COEXIST
1923
1924         _enter_pwrlock(&pwrctrl->lock);
1925
1926         register_task_alive(pwrctrl, CMD_ALIVE);
1927
1928         if (pwrctrl->bFwCurrentInPSMode == _TRUE)
1929         {
1930                 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_info_,
1931                                  ("rtw_register_cmd_alive: cpwm=0x%02x alives=0x%08x\n",
1932                                   pwrctrl->cpwm, pwrctrl->alives));
1933
1934                 if (pwrctrl->cpwm < pslv)
1935                 {
1936                         if (pwrctrl->cpwm < PS_STATE_S2)
1937                                 res = _FAIL;
1938                         if (pwrctrl->rpwm < pslv)
1939                                 rtw_set_rpwm(padapter, pslv);
1940                 }
1941         }
1942
1943         _exit_pwrlock(&pwrctrl->lock);
1944
1945 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1946         if (_FAIL == res)
1947         {
1948                 if (pwrctrl->cpwm >= PS_STATE_S2)
1949                         res = _SUCCESS;
1950         }
1951 #endif // CONFIG_DETECT_CPWM_BY_POLLING
1952
1953 _func_exit_;
1954
1955         return res;
1956 }
1957
1958 /*
1959  * Caller: rx_isr
1960  *
1961  * Calling Context: Dispatch/ISR
1962  *
1963  * Return Value:
1964  *      _SUCCESS
1965  *      _FAIL
1966  */
1967 s32 rtw_register_rx_alive(PADAPTER padapter)
1968 {
1969         struct pwrctrl_priv *pwrctrl;
1970
1971 _func_enter_;
1972
1973         pwrctrl = adapter_to_pwrctl(padapter);
1974
1975         _enter_pwrlock(&pwrctrl->lock);
1976
1977         register_task_alive(pwrctrl, RECV_ALIVE);
1978         RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
1979                          ("rtw_register_rx_alive: cpwm=0x%02x alives=0x%08x\n",
1980                           pwrctrl->cpwm, pwrctrl->alives));
1981
1982         _exit_pwrlock(&pwrctrl->lock);
1983
1984 _func_exit_;
1985
1986         return _SUCCESS;
1987 }
1988
1989 /*
1990  * Caller: evt_isr or evt_thread
1991  *
1992  * Calling Context: Dispatch/ISR or Passive
1993  *
1994  * Return Value:
1995  *      _SUCCESS
1996  *      _FAIL
1997  */
1998 s32 rtw_register_evt_alive(PADAPTER padapter)
1999 {
2000         struct pwrctrl_priv *pwrctrl;
2001
2002 _func_enter_;
2003
2004         pwrctrl = adapter_to_pwrctl(padapter);
2005
2006         _enter_pwrlock(&pwrctrl->lock);
2007
2008         register_task_alive(pwrctrl, EVT_ALIVE);
2009         RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
2010                          ("rtw_register_evt_alive: cpwm=0x%02x alives=0x%08x\n",
2011                           pwrctrl->cpwm, pwrctrl->alives));
2012
2013         _exit_pwrlock(&pwrctrl->lock);
2014
2015 _func_exit_;
2016
2017         return _SUCCESS;
2018 }
2019
2020 /*
2021  * Caller: ISR
2022  *
2023  * If ISR's txdone,
2024  * No more pkts for TX,
2025  * Then driver shall call this fun. to power down firmware again.
2026  */
2027 void rtw_unregister_tx_alive(PADAPTER padapter)
2028 {
2029         struct pwrctrl_priv *pwrctrl;
2030         u8 pslv;
2031
2032 _func_enter_;
2033
2034         pwrctrl = adapter_to_pwrctl(padapter);
2035         pslv = PS_STATE_S0;
2036
2037 #ifdef CONFIG_BT_COEXIST
2038         if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
2039                 && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE))
2040         {
2041                 u8 val8;
2042
2043                 val8 = rtw_btcoex_LpsVal(padapter);
2044                 if (val8 & BIT(4))
2045                         pslv = PS_STATE_S2;
2046
2047 #ifdef CONFIG_RTL8723A
2048                 val8 = rtw_btcoex_RpwmVal(padapter);
2049                 switch (val8)
2050                 {
2051                         case 0x4:
2052                                 pslv = PS_STATE_S3;
2053                                 break;
2054
2055                         case 0xC:
2056                                 pslv = PS_STATE_S4;
2057                                 break;
2058                 }
2059 #endif // CONFIG_RTL8723A
2060         }
2061 #endif // CONFIG_BT_COEXIST
2062
2063 #ifdef CONFIG_P2P_PS
2064         if(padapter->wdinfo.p2p_ps_mode > P2P_PS_NONE)
2065         {
2066                 pslv = PS_STATE_S2;
2067         }
2068 #ifdef CONFIG_CONCURRENT_MODE
2069         else if(rtw_buddy_adapter_up(padapter))
2070         {
2071                 if(padapter->pbuddy_adapter->wdinfo.p2p_ps_mode > P2P_PS_NONE)
2072                         pslv = PS_STATE_S2;
2073         }
2074 #endif
2075 #endif
2076
2077         _enter_pwrlock(&pwrctrl->lock);
2078
2079         unregister_task_alive(pwrctrl, XMIT_ALIVE);
2080
2081         if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
2082                 && (pwrctrl->bFwCurrentInPSMode == _TRUE))
2083         {
2084                 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
2085                                  ("%s: cpwm=0x%02x alives=0x%08x\n",
2086                                   __FUNCTION__, pwrctrl->cpwm, pwrctrl->alives));
2087
2088                 if (pwrctrl->cpwm > pslv)
2089                 {
2090                         if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
2091                                 rtw_set_rpwm(padapter, pslv);
2092                 }
2093         }
2094
2095         _exit_pwrlock(&pwrctrl->lock);
2096
2097 _func_exit_;
2098 }
2099
2100 /*
2101  * Caller: ISR
2102  *
2103  * If all commands have been done,
2104  * and no more command to do,
2105  * then driver shall call this fun. to power down firmware again.
2106  */
2107 void rtw_unregister_cmd_alive(PADAPTER padapter)
2108 {
2109         struct pwrctrl_priv *pwrctrl;
2110         u8 pslv;
2111
2112 _func_enter_;
2113
2114         pwrctrl = adapter_to_pwrctl(padapter);
2115         pslv = PS_STATE_S0;
2116
2117 #ifdef CONFIG_BT_COEXIST
2118         if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
2119                 && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE))
2120         {
2121                 u8 val8;
2122
2123                 val8 = rtw_btcoex_LpsVal(padapter);
2124                 if (val8 & BIT(4))
2125                         pslv = PS_STATE_S2;
2126
2127 #ifdef CONFIG_RTL8723A
2128                 val8 = rtw_btcoex_RpwmVal(padapter);
2129                 switch (val8)
2130                 {
2131                         case 0x4:
2132                                 pslv = PS_STATE_S3;
2133                                 break;
2134
2135                         case 0xC:
2136                                 pslv = PS_STATE_S4;
2137                                 break;
2138                 }
2139 #endif // CONFIG_RTL8723A
2140         }
2141 #endif // CONFIG_BT_COEXIST
2142
2143 #ifdef CONFIG_P2P_PS
2144         if(padapter->wdinfo.p2p_ps_mode > P2P_PS_NONE)
2145         {
2146                 pslv = PS_STATE_S2;
2147         }
2148 #ifdef CONFIG_CONCURRENT_MODE
2149         else if(rtw_buddy_adapter_up(padapter))
2150         {
2151                 if(padapter->pbuddy_adapter->wdinfo.p2p_ps_mode > P2P_PS_NONE)
2152                         pslv = PS_STATE_S2;
2153         }
2154 #endif
2155 #endif
2156
2157         _enter_pwrlock(&pwrctrl->lock);
2158
2159         unregister_task_alive(pwrctrl, CMD_ALIVE);
2160
2161         if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
2162                 && (pwrctrl->bFwCurrentInPSMode == _TRUE))
2163         {
2164                 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_info_,
2165                                  ("%s: cpwm=0x%02x alives=0x%08x\n",
2166                                   __FUNCTION__, pwrctrl->cpwm, pwrctrl->alives));
2167
2168                 if (pwrctrl->cpwm > pslv)
2169                 {
2170                         if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
2171                                 rtw_set_rpwm(padapter, pslv);
2172                 }
2173         }
2174
2175         _exit_pwrlock(&pwrctrl->lock);
2176
2177 _func_exit_;
2178 }
2179
2180 /*
2181  * Caller: ISR
2182  */
2183 void rtw_unregister_rx_alive(PADAPTER padapter)
2184 {
2185         struct pwrctrl_priv *pwrctrl;
2186
2187 _func_enter_;
2188
2189         pwrctrl = adapter_to_pwrctl(padapter);
2190
2191         _enter_pwrlock(&pwrctrl->lock);
2192
2193         unregister_task_alive(pwrctrl, RECV_ALIVE);
2194
2195         RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
2196                          ("rtw_unregister_rx_alive: cpwm=0x%02x alives=0x%08x\n",
2197                           pwrctrl->cpwm, pwrctrl->alives));
2198
2199         _exit_pwrlock(&pwrctrl->lock);
2200
2201 _func_exit_;
2202 }
2203
2204 void rtw_unregister_evt_alive(PADAPTER padapter)
2205 {
2206         struct pwrctrl_priv *pwrctrl;
2207
2208 _func_enter_;
2209
2210         pwrctrl = adapter_to_pwrctl(padapter);
2211
2212         unregister_task_alive(pwrctrl, EVT_ALIVE);
2213
2214         RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
2215                          ("rtw_unregister_evt_alive: cpwm=0x%02x alives=0x%08x\n",
2216                           pwrctrl->cpwm, pwrctrl->alives));
2217
2218         _exit_pwrlock(&pwrctrl->lock);
2219
2220 _func_exit_;
2221 }
2222 #endif  /* CONFIG_LPS_LCLK */
2223
2224 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2225 static void resume_workitem_callback(struct work_struct *work);
2226 #endif //CONFIG_RESUME_IN_WORKQUEUE
2227
2228 void rtw_init_pwrctrl_priv(PADAPTER padapter)
2229 {
2230         struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2231
2232 #if defined(CONFIG_CONCURRENT_MODE)
2233         if (padapter->adapter_type != PRIMARY_ADAPTER)
2234                 return;
2235 #endif
2236
2237 _func_enter_;
2238
2239 #ifdef PLATFORM_WINDOWS
2240         pwrctrlpriv->pnp_current_pwr_state=NdisDeviceStateD0;
2241 #endif
2242
2243         _init_pwrlock(&pwrctrlpriv->lock);
2244         _init_pwrlock(&pwrctrlpriv->check_32k_lock);
2245         pwrctrlpriv->rf_pwrstate = rf_on;
2246         pwrctrlpriv->ips_enter_cnts=0;
2247         pwrctrlpriv->ips_leave_cnts=0;
2248         pwrctrlpriv->bips_processing = _FALSE;
2249
2250         pwrctrlpriv->ips_mode = padapter->registrypriv.ips_mode;
2251         pwrctrlpriv->ips_mode_req = padapter->registrypriv.ips_mode;
2252
2253         pwrctrlpriv->pwr_state_check_interval = RTW_PWR_STATE_CHK_INTERVAL;
2254         pwrctrlpriv->pwr_state_check_cnts = 0;
2255         pwrctrlpriv->bInternalAutoSuspend = _FALSE;
2256         pwrctrlpriv->bInSuspend = _FALSE;
2257         pwrctrlpriv->bkeepfwalive = _FALSE;
2258
2259 #ifdef CONFIG_AUTOSUSPEND
2260 #ifdef SUPPORT_HW_RFOFF_DETECTED
2261         pwrctrlpriv->pwr_state_check_interval = (pwrctrlpriv->bHWPwrPindetect) ?1000:2000;              
2262 #endif
2263 #endif
2264
2265         pwrctrlpriv->LpsIdleCount = 0;
2266         //pwrctrlpriv->FWCtrlPSMode =padapter->registrypriv.power_mgnt;// PS_MODE_MIN;
2267         if (padapter->registrypriv.mp_mode == 1)
2268                 pwrctrlpriv->power_mgnt =PS_MODE_ACTIVE ;
2269         else    
2270                 pwrctrlpriv->power_mgnt =padapter->registrypriv.power_mgnt;// PS_MODE_MIN;
2271         pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt)?_TRUE:_FALSE;
2272
2273         pwrctrlpriv->bFwCurrentInPSMode = _FALSE;
2274
2275         pwrctrlpriv->rpwm = 0;
2276         pwrctrlpriv->cpwm = PS_STATE_S4;
2277
2278         pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
2279         pwrctrlpriv->smart_ps = padapter->registrypriv.smart_ps;
2280         pwrctrlpriv->bcn_ant_mode = 0;
2281         pwrctrlpriv->dtim = 0;
2282
2283         pwrctrlpriv->tog = 0x80;
2284
2285 #ifdef CONFIG_LPS_LCLK
2286         rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&pwrctrlpriv->rpwm));
2287
2288         _init_workitem(&pwrctrlpriv->cpwm_event, cpwm_event_callback, NULL);
2289
2290 #ifdef CONFIG_LPS_RPWM_TIMER
2291         pwrctrlpriv->brpwmtimeout = _FALSE;
2292         _init_workitem(&pwrctrlpriv->rpwmtimeoutwi, rpwmtimeout_workitem_callback, NULL);
2293         _init_timer(&pwrctrlpriv->pwr_rpwm_timer, padapter->pnetdev, pwr_rpwm_timeout_handler, padapter);
2294 #endif // CONFIG_LPS_RPWM_TIMER
2295 #endif // CONFIG_LPS_LCLK
2296
2297         rtw_init_timer(&pwrctrlpriv->pwr_state_check_timer, padapter, pwr_state_check_handler);
2298
2299         pwrctrlpriv->wowlan_mode = _FALSE;
2300         pwrctrlpriv->wowlan_ap_mode = _FALSE;
2301         pwrctrlpriv->wowlan_p2p_mode = _FALSE;
2302
2303         #ifdef CONFIG_RESUME_IN_WORKQUEUE
2304         _init_workitem(&pwrctrlpriv->resume_work, resume_workitem_callback, NULL);
2305         pwrctrlpriv->rtw_workqueue = create_singlethread_workqueue("rtw_workqueue");
2306         #endif //CONFIG_RESUME_IN_WORKQUEUE
2307
2308         #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
2309         pwrctrlpriv->early_suspend.suspend = NULL;
2310         rtw_register_early_suspend(pwrctrlpriv);
2311         #endif //CONFIG_HAS_EARLYSUSPEND || CONFIG_ANDROID_POWER
2312
2313 #ifdef CONFIG_WOWLAN
2314         pwrctrlpriv->wowlan_from_cmd = _FALSE;
2315 #endif
2316 #ifdef CONFIG_PNO_SUPPORT
2317         pwrctrlpriv->pno_inited = _FALSE;
2318         pwrctrlpriv->pnlo_info = NULL;
2319         pwrctrlpriv->pscan_info = NULL;
2320         pwrctrlpriv->pno_ssid_list = NULL;
2321         pwrctrlpriv->pno_in_resume = _TRUE;
2322 #endif
2323
2324 _func_exit_;
2325
2326 }
2327
2328
2329 void rtw_free_pwrctrl_priv(PADAPTER adapter)
2330 {
2331         struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(adapter);
2332
2333 #if defined(CONFIG_CONCURRENT_MODE)
2334         if (adapter->adapter_type != PRIMARY_ADAPTER)
2335                 return;
2336 #endif  
2337
2338 _func_enter_;
2339
2340         //_rtw_memset((unsigned char *)pwrctrlpriv, 0, sizeof(struct pwrctrl_priv));
2341
2342
2343         #ifdef CONFIG_RESUME_IN_WORKQUEUE
2344         if (pwrctrlpriv->rtw_workqueue) { 
2345                 flush_workqueue(pwrctrlpriv->rtw_workqueue);
2346                 destroy_workqueue(pwrctrlpriv->rtw_workqueue);
2347         }
2348         #endif
2349
2350 #ifdef CONFIG_PNO_SUPPORT
2351         if (pwrctrlpriv->pnlo_info != NULL)
2352                 printk("****** pnlo_info memory leak********\n");
2353
2354         if (pwrctrlpriv->pscan_info != NULL)
2355                 printk("****** pscan_info memory leak********\n");
2356
2357         if (pwrctrlpriv->pno_ssid_list != NULL)
2358                 printk("****** pno_ssid_list memory leak********\n");
2359 #endif
2360
2361         #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
2362         rtw_unregister_early_suspend(pwrctrlpriv);
2363         #endif //CONFIG_HAS_EARLYSUSPEND || CONFIG_ANDROID_POWER
2364
2365         _free_pwrlock(&pwrctrlpriv->lock);
2366         _free_pwrlock(&pwrctrlpriv->check_32k_lock);
2367
2368 _func_exit_;
2369 }
2370
2371 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2372 extern int rtw_resume_process(_adapter *padapter);
2373
2374 static void resume_workitem_callback(struct work_struct *work)
2375 {
2376         struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, resume_work);
2377         struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2378         _adapter *adapter = dvobj->if1;
2379
2380         DBG_871X("%s\n",__FUNCTION__);
2381
2382         rtw_resume_process(adapter);
2383
2384         rtw_resume_unlock_suspend();
2385 }
2386
2387 void rtw_resume_in_workqueue(struct pwrctrl_priv *pwrpriv)
2388 {
2389         // accquire system's suspend lock preventing from falliing asleep while resume in workqueue
2390         //rtw_lock_suspend();
2391
2392         rtw_resume_lock_suspend();
2393         
2394         #if 1
2395         queue_work(pwrpriv->rtw_workqueue, &pwrpriv->resume_work);      
2396         #else
2397         _set_workitem(&pwrpriv->resume_work);
2398         #endif
2399 }
2400 #endif //CONFIG_RESUME_IN_WORKQUEUE
2401
2402 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
2403 inline bool rtw_is_earlysuspend_registered(struct pwrctrl_priv *pwrpriv)
2404 {
2405         return (pwrpriv->early_suspend.suspend) ? _TRUE : _FALSE;
2406 }
2407
2408 inline bool rtw_is_do_late_resume(struct pwrctrl_priv *pwrpriv)
2409 {
2410         return (pwrpriv->do_late_resume) ? _TRUE : _FALSE;
2411 }
2412
2413 inline void rtw_set_do_late_resume(struct pwrctrl_priv *pwrpriv, bool enable)
2414 {
2415         pwrpriv->do_late_resume = enable;
2416 }
2417 #endif
2418
2419 #ifdef CONFIG_HAS_EARLYSUSPEND
2420 extern int rtw_resume_process(_adapter *padapter);
2421 static void rtw_early_suspend(struct early_suspend *h)
2422 {
2423         struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2424         DBG_871X("%s\n",__FUNCTION__);
2425
2426         rtw_set_do_late_resume(pwrpriv, _FALSE);
2427 }
2428
2429 static void rtw_late_resume(struct early_suspend *h)
2430 {
2431         struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2432         struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2433         _adapter *adapter = dvobj->if1;
2434
2435         DBG_871X("%s\n",__FUNCTION__);
2436
2437         if(pwrpriv->do_late_resume) {
2438                 rtw_set_do_late_resume(pwrpriv, _FALSE);
2439                 rtw_resume_process(adapter);
2440         }
2441 }
2442
2443 void rtw_register_early_suspend(struct pwrctrl_priv *pwrpriv)
2444 {
2445         DBG_871X("%s\n", __FUNCTION__);
2446
2447         //jeff: set the early suspend level before blank screen, so we wll do late resume after scree is lit
2448         pwrpriv->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
2449         pwrpriv->early_suspend.suspend = rtw_early_suspend;
2450         pwrpriv->early_suspend.resume = rtw_late_resume;
2451         register_early_suspend(&pwrpriv->early_suspend);        
2452
2453         
2454 }
2455
2456 void rtw_unregister_early_suspend(struct pwrctrl_priv *pwrpriv)
2457 {
2458         DBG_871X("%s\n", __FUNCTION__);
2459
2460         rtw_set_do_late_resume(pwrpriv, _FALSE);
2461
2462         if (pwrpriv->early_suspend.suspend) 
2463                 unregister_early_suspend(&pwrpriv->early_suspend);
2464
2465         pwrpriv->early_suspend.suspend = NULL;
2466         pwrpriv->early_suspend.resume = NULL;
2467 }
2468 #endif //CONFIG_HAS_EARLYSUSPEND
2469
2470 #ifdef CONFIG_ANDROID_POWER
2471 #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2472 extern int rtw_resume_process(PADAPTER padapter);
2473 #endif
2474 static void rtw_early_suspend(android_early_suspend_t *h)
2475 {
2476         struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2477         DBG_871X("%s\n",__FUNCTION__);
2478
2479         rtw_set_do_late_resume(pwrpriv, _FALSE);
2480 }
2481
2482 static void rtw_late_resume(android_early_suspend_t *h)
2483 {
2484         struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2485         struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2486         _adapter *adapter = dvobj->if1;
2487
2488         DBG_871X("%s\n",__FUNCTION__);
2489         if(pwrpriv->do_late_resume) {
2490                 #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2491                 rtw_set_do_late_resume(pwrpriv, _FALSE);
2492                 rtw_resume_process(adapter);
2493                 #endif
2494         }
2495 }
2496
2497 void rtw_register_early_suspend(struct pwrctrl_priv *pwrpriv)
2498 {
2499         DBG_871X("%s\n", __FUNCTION__);
2500
2501         //jeff: set the early suspend level before blank screen, so we wll do late resume after scree is lit
2502         pwrpriv->early_suspend.level = ANDROID_EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
2503         pwrpriv->early_suspend.suspend = rtw_early_suspend;
2504         pwrpriv->early_suspend.resume = rtw_late_resume;
2505         android_register_early_suspend(&pwrpriv->early_suspend);        
2506 }
2507
2508 void rtw_unregister_early_suspend(struct pwrctrl_priv *pwrpriv)
2509 {
2510         DBG_871X("%s\n", __FUNCTION__);
2511
2512         rtw_set_do_late_resume(pwrpriv, _FALSE);
2513
2514         if (pwrpriv->early_suspend.suspend) 
2515                 android_unregister_early_suspend(&pwrpriv->early_suspend);
2516
2517         pwrpriv->early_suspend.suspend = NULL;
2518         pwrpriv->early_suspend.resume = NULL;
2519 }
2520 #endif //CONFIG_ANDROID_POWER
2521
2522 u8 rtw_interface_ps_func(_adapter *padapter,HAL_INTF_PS_FUNC efunc_id,u8* val)
2523 {
2524         u8 bResult = _TRUE;
2525         rtw_hal_intf_ps_func(padapter,efunc_id,val);
2526         
2527         return bResult;
2528 }
2529
2530
2531 inline void rtw_set_ips_deny(_adapter *padapter, u32 ms)
2532 {
2533         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
2534         pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ms);
2535 }
2536
2537 /*
2538 * rtw_pwr_wakeup - Wake the NIC up from: 1)IPS. 2)USB autosuspend
2539 * @adapter: pointer to _adapter structure
2540 * @ips_deffer_ms: the ms wiil prevent from falling into IPS after wakeup
2541 * Return _SUCCESS or _FAIL
2542 */
2543
2544 int _rtw_pwr_wakeup(_adapter *padapter, u32 ips_deffer_ms, const char *caller)
2545 {
2546         struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
2547         struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
2548         struct mlme_priv *pmlmepriv;
2549         int ret = _SUCCESS;
2550         int i;
2551         u32 start = rtw_get_current_time();
2552
2553         /* for LPS */
2554         LeaveAllPowerSaveMode(padapter);
2555
2556         /* IPS still bound with primary adapter */
2557         padapter = GET_PRIMARY_ADAPTER(padapter);
2558         pmlmepriv = &padapter->mlmepriv;
2559
2560         if (pwrpriv->ips_deny_time < rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms))
2561                 pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms);
2562
2563
2564         if (pwrpriv->ps_processing) {
2565                 DBG_871X("%s wait ps_processing...\n", __func__);
2566                 while (pwrpriv->ps_processing && rtw_get_passing_time_ms(start) <= 3000)
2567                         rtw_msleep_os(10);
2568                 if (pwrpriv->ps_processing)
2569                         DBG_871X("%s wait ps_processing timeout\n", __func__);
2570                 else
2571                         DBG_871X("%s wait ps_processing done\n", __func__);
2572         }
2573
2574 #ifdef DBG_CONFIG_ERROR_DETECT
2575         if (rtw_hal_sreset_inprogress(padapter)) {
2576                 DBG_871X("%s wait sreset_inprogress...\n", __func__);
2577                 while (rtw_hal_sreset_inprogress(padapter) && rtw_get_passing_time_ms(start) <= 4000)
2578                         rtw_msleep_os(10);
2579                 if (rtw_hal_sreset_inprogress(padapter))
2580                         DBG_871X("%s wait sreset_inprogress timeout\n", __func__);
2581                 else
2582                         DBG_871X("%s wait sreset_inprogress done\n", __func__);
2583         }
2584 #endif
2585
2586         if (pwrpriv->bInternalAutoSuspend == _FALSE && pwrpriv->bInSuspend) {
2587                 DBG_871X("%s wait bInSuspend...\n", __func__);
2588                 while (pwrpriv->bInSuspend 
2589                         && ((rtw_get_passing_time_ms(start) <= 3000 && !rtw_is_do_late_resume(pwrpriv))
2590                                 || (rtw_get_passing_time_ms(start) <= 500 && rtw_is_do_late_resume(pwrpriv)))
2591                 ) {
2592                         rtw_msleep_os(10);
2593                 }
2594                 if (pwrpriv->bInSuspend)
2595                         DBG_871X("%s wait bInSuspend timeout\n", __func__);
2596                 else
2597                         DBG_871X("%s wait bInSuspend done\n", __func__);
2598         }
2599
2600         //System suspend is not allowed to wakeup
2601         if((pwrpriv->bInternalAutoSuspend == _FALSE) && (_TRUE == pwrpriv->bInSuspend )){
2602                 ret = _FAIL;
2603                 goto exit;
2604         }
2605
2606         //block???
2607         if((pwrpriv->bInternalAutoSuspend == _TRUE)  && (padapter->net_closed == _TRUE)) {
2608                 ret = _FAIL;
2609                 goto exit;
2610         }
2611
2612         //I think this should be check in IPS, LPS, autosuspend functions...
2613         if (check_fwstate(pmlmepriv, _FW_LINKED) == _TRUE)
2614         {
2615 #if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
2616                 if(_TRUE==pwrpriv->bInternalAutoSuspend){
2617                         if(0==pwrpriv->autopm_cnt){
2618                         #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,33))                                
2619                                 if (usb_autopm_get_interface(adapter_to_dvobj(padapter)->pusbintf) < 0) 
2620                                 {
2621                                         DBG_871X( "can't get autopm: \n");
2622                                 }                       
2623                         #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,20))                              
2624                                 usb_autopm_disable(adapter_to_dvobj(padapter)->pusbintf);
2625                         #else
2626                                 usb_autoresume_device(adapter_to_dvobj(padapter)->pusbdev, 1);
2627                         #endif
2628                         pwrpriv->autopm_cnt++;
2629                         }
2630 #endif  //#if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
2631                 ret = _SUCCESS;
2632                 goto exit;
2633 #if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
2634                 }
2635 #endif  //#if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
2636         }       
2637
2638         if(rf_off == pwrpriv->rf_pwrstate )
2639         {               
2640 #ifdef CONFIG_USB_HCI
2641 #ifdef CONFIG_AUTOSUSPEND
2642                  if(pwrpriv->brfoffbyhw==_TRUE)
2643                 {
2644                         DBG_8192C("hw still in rf_off state ...........\n");
2645                         ret = _FAIL;
2646                         goto exit;
2647                 }
2648                 else if(padapter->registrypriv.usbss_enable)
2649                 {
2650                         DBG_8192C("%s call autoresume_enter....\n",__FUNCTION__);
2651                         if(_FAIL ==  autoresume_enter(padapter))
2652                         {
2653                                 DBG_8192C("======> autoresume fail.............\n");
2654                                 ret = _FAIL;
2655                                 goto exit;
2656                         }       
2657                 }
2658                 else
2659 #endif
2660 #endif
2661                 {
2662 #ifdef CONFIG_IPS
2663                         DBG_8192C("%s call ips_leave....\n",__FUNCTION__);
2664                         if(_FAIL ==  ips_leave(padapter))
2665                         {
2666                                 DBG_8192C("======> ips_leave fail.............\n");
2667                                 ret = _FAIL;
2668                                 goto exit;
2669                         }
2670 #endif
2671                 }
2672         }
2673
2674         //TODO: the following checking need to be merged...
2675         if(padapter->bDriverStopped
2676                 || !padapter->bup
2677                 || !padapter->hw_init_completed
2678         ){
2679                 DBG_8192C("%s: bDriverStopped=%d, bup=%d, hw_init_completed=%u\n"
2680                         , caller
2681                         , padapter->bDriverStopped
2682                         , padapter->bup
2683                         , padapter->hw_init_completed);
2684                 ret= _FALSE;
2685                 goto exit;
2686         }
2687
2688 exit:
2689         if (pwrpriv->ips_deny_time < rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms))
2690                 pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms);
2691         return ret;
2692
2693 }
2694
2695 int rtw_pm_set_lps(_adapter *padapter, u8 mode)
2696 {
2697         int     ret = 0;        
2698         struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2699         
2700         if ( mode < PS_MODE_NUM )
2701         {
2702                 if(pwrctrlpriv->power_mgnt !=mode)
2703                 {
2704                         if(PS_MODE_ACTIVE == mode)
2705                         {
2706                                 LeaveAllPowerSaveMode(padapter);
2707                         }
2708                         else
2709                         {
2710                                 pwrctrlpriv->LpsIdleCount = 2;
2711                         }
2712                         pwrctrlpriv->power_mgnt = mode;
2713                         pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt)?_TRUE:_FALSE;
2714                 }
2715         }
2716         else
2717         {
2718                 ret = -EINVAL;
2719         }
2720
2721         return ret;
2722 }
2723
2724 int rtw_pm_set_ips(_adapter *padapter, u8 mode)
2725 {
2726         struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2727
2728         if( mode == IPS_NORMAL || mode == IPS_LEVEL_2 ) {
2729                 rtw_ips_mode_req(pwrctrlpriv, mode);
2730                 DBG_871X("%s %s\n", __FUNCTION__, mode == IPS_NORMAL?"IPS_NORMAL":"IPS_LEVEL_2");
2731                 return 0;
2732         } 
2733         else if(mode ==IPS_NONE){
2734                 rtw_ips_mode_req(pwrctrlpriv, mode);
2735                 DBG_871X("%s %s\n", __FUNCTION__, "IPS_NONE");
2736                 if((padapter->bSurpriseRemoved ==0)&&(_FAIL == rtw_pwr_wakeup(padapter)) )
2737                         return -EFAULT;
2738         }
2739         else {
2740                 return -EINVAL;
2741         }
2742         return 0;
2743 }
2744
2745 /*
2746  * ATTENTION:
2747  *      This function will request pwrctrl LOCK!
2748  */
2749 void rtw_ps_deny(PADAPTER padapter, PS_DENY_REASON reason)
2750 {
2751         struct pwrctrl_priv *pwrpriv;
2752         s32 ret;
2753
2754
2755 //      DBG_871X("+" FUNC_ADPT_FMT ": Request PS deny for %d (0x%08X)\n",
2756 //              FUNC_ADPT_ARG(padapter), reason, BIT(reason));
2757
2758         pwrpriv = adapter_to_pwrctl(padapter);
2759
2760         _enter_pwrlock(&pwrpriv->lock);
2761         if (pwrpriv->ps_deny & BIT(reason))
2762         {
2763                 DBG_871X(FUNC_ADPT_FMT ": [WARNING] Reason %d had been set before!!\n",
2764                         FUNC_ADPT_ARG(padapter), reason);
2765         }
2766         pwrpriv->ps_deny |= BIT(reason);
2767         _exit_pwrlock(&pwrpriv->lock);
2768
2769 //      DBG_871X("-" FUNC_ADPT_FMT ": Now PS deny for 0x%08X\n",
2770 //              FUNC_ADPT_ARG(padapter), pwrpriv->ps_deny);
2771 }
2772
2773 /*
2774  * ATTENTION:
2775  *      This function will request pwrctrl LOCK!
2776  */
2777 void rtw_ps_deny_cancel(PADAPTER padapter, PS_DENY_REASON reason)
2778 {
2779         struct pwrctrl_priv *pwrpriv;
2780
2781
2782 //      DBG_871X("+" FUNC_ADPT_FMT ": Cancel PS deny for %d(0x%08X)\n",
2783 //              FUNC_ADPT_ARG(padapter), reason, BIT(reason));
2784
2785         pwrpriv = adapter_to_pwrctl(padapter);
2786
2787         _enter_pwrlock(&pwrpriv->lock);
2788         if ((pwrpriv->ps_deny & BIT(reason)) == 0)
2789         {
2790                 DBG_871X(FUNC_ADPT_FMT ": [ERROR] Reason %d had been canceled before!!\n",
2791                         FUNC_ADPT_ARG(padapter), reason);
2792         }
2793         pwrpriv->ps_deny &= ~BIT(reason);
2794         _exit_pwrlock(&pwrpriv->lock);
2795
2796 //      DBG_871X("-" FUNC_ADPT_FMT ": Now PS deny for 0x%08X\n",
2797 //              FUNC_ADPT_ARG(padapter), pwrpriv->ps_deny);
2798 }
2799
2800 /*
2801  * ATTENTION:
2802  *      Before calling this function pwrctrl lock should be occupied already,
2803  *      otherwise it may return incorrect value.
2804  */
2805 u32 rtw_ps_deny_get(PADAPTER padapter)
2806 {
2807         u32 deny;
2808
2809
2810         deny = adapter_to_pwrctl(padapter)->ps_deny;
2811
2812         return deny;
2813 }
2814