1 /******************************************************************************
3 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
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.
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
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
19 ******************************************************************************/
20 #define _RTW_PWRCTRL_C_
22 #include <drv_types.h>
25 int rtw_fw_ps_state(PADAPTER padapter)
27 struct dvobj_priv *psdpriv = padapter->dvobj;
28 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
29 int ret=_FAIL, dont_care=0;
32 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
33 struct registry_priv *registry_par = &padapter->registrypriv;
35 if(registry_par->check_fw_ps != 1)
38 _enter_pwrlock(&pwrpriv->check_32k_lock);
40 if ((padapter->bSurpriseRemoved == _TRUE))
42 DBG_871X("%s: bSurpriseRemoved=%d , hw_init_completed=%d, bDriverStopped=%d \n", __FUNCTION__, padapter->bSurpriseRemoved,
43 padapter->hw_init_completed,padapter->bDriverStopped);
44 goto exit_fw_ps_state;
46 rtw_hal_set_hwreg(padapter, HW_VAR_SET_REQ_FW_PS, (u8 *)&dont_care);
48 //4. if 0x88[7]=1, driver set cmd to leave LPS/IPS.
49 //Else, hw will keep in active mode.
51 //0x88[7] = 32kpermission,
52 //0x88[6:0] = current_ps_state
53 //0x89[7:0] = last_rpwm
55 rtw_hal_get_hwreg(padapter, HW_VAR_FW_PS_STATE, (u8 *)&fw_ps_state);
57 if((fw_ps_state & 0x80) == 0)
61 pdbgpriv->dbg_poll_fail_cnt++;
62 DBG_871X("%s: fw_ps_state=%04x \n", __FUNCTION__, fw_ps_state);
68 _exit_pwrlock(&pwrpriv->check_32k_lock);
73 void _ips_enter(_adapter * padapter)
75 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
77 pwrpriv->bips_processing = _TRUE;
79 // syn ips_mode with request
80 pwrpriv->ips_mode = pwrpriv->ips_mode_req;
82 pwrpriv->ips_enter_cnts++;
83 DBG_871X("==>ips_enter cnts:%d\n",pwrpriv->ips_enter_cnts);
85 if(rf_off == pwrpriv->change_rfpwrstate )
87 pwrpriv->bpower_saving = _TRUE;
88 DBG_871X_LEVEL(_drv_always_, "nolinked power save enter\n");
90 if(pwrpriv->ips_mode == IPS_LEVEL_2)
91 pwrpriv->bkeepfwalive = _TRUE;
93 rtw_ips_pwr_down(padapter);
94 pwrpriv->rf_pwrstate = rf_off;
96 pwrpriv->bips_processing = _FALSE;
100 void ips_enter(_adapter * padapter)
102 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
105 #ifdef CONFIG_BT_COEXIST
106 rtw_btcoex_IpsNotify(padapter, pwrpriv->ips_mode_req);
107 #endif // CONFIG_BT_COEXIST
109 _enter_pwrlock(&pwrpriv->lock);
110 _ips_enter(padapter);
111 _exit_pwrlock(&pwrpriv->lock);
114 int _ips_leave(_adapter * padapter)
116 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
117 int result = _SUCCESS;
119 if((pwrpriv->rf_pwrstate == rf_off) &&(!pwrpriv->bips_processing))
121 pwrpriv->bips_processing = _TRUE;
122 pwrpriv->change_rfpwrstate = rf_on;
123 pwrpriv->ips_leave_cnts++;
124 DBG_871X("==>ips_leave cnts:%d\n",pwrpriv->ips_leave_cnts);
126 if ((result = rtw_ips_pwr_up(padapter)) == _SUCCESS) {
127 pwrpriv->rf_pwrstate = rf_on;
129 DBG_871X_LEVEL(_drv_always_, "nolinked power save leave\n");
131 DBG_871X("==> ips_leave.....LED(0x%08x)...\n",rtw_read32(padapter,0x4c));
132 pwrpriv->bips_processing = _FALSE;
134 pwrpriv->bkeepfwalive = _FALSE;
135 pwrpriv->bpower_saving = _FALSE;
141 int ips_leave(_adapter * padapter)
143 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
144 struct dvobj_priv *psdpriv = padapter->dvobj;
145 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
148 if(!is_primary_adapter(padapter))
151 _enter_pwrlock(&pwrpriv->lock);
152 ret = _ips_leave(padapter);
153 #ifdef DBG_CHECK_FW_PS_STATE
154 if(rtw_fw_ps_state(padapter) == _FAIL)
156 DBG_871X("ips leave doesn't leave 32k\n");
157 pdbgpriv->dbg_leave_ips_fail_cnt++;
159 #endif //DBG_CHECK_FW_PS_STATE
160 _exit_pwrlock(&pwrpriv->lock);
162 #ifdef CONFIG_BT_COEXIST
164 rtw_btcoex_IpsNotify(padapter, IPS_NONE);
165 #endif // CONFIG_BT_COEXIST
169 #endif /* CONFIG_IPS */
171 #ifdef CONFIG_AUTOSUSPEND
172 extern void autosuspend_enter(_adapter* padapter);
173 extern int autoresume_enter(_adapter* padapter);
176 #ifdef SUPPORT_HW_RFOFF_DETECTED
177 int rtw_hw_suspend(_adapter *padapter );
178 int rtw_hw_resume(_adapter *padapter);
181 bool rtw_pwr_unassociated_idle(_adapter *adapter)
183 _adapter *buddy = adapter->pbuddy_adapter;
184 struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
185 struct xmit_priv *pxmit_priv = &adapter->xmitpriv;
187 struct wifidirect_info *pwdinfo = &(adapter->wdinfo);
188 #ifdef CONFIG_IOCTL_CFG80211
189 struct cfg80211_wifidirect_info *pcfg80211_wdinfo = &adapter->cfg80211_wdinfo;
195 if (adapter_to_pwrctl(adapter)->bpower_saving ==_TRUE ) {
196 //DBG_871X("%s: already in LPS or IPS mode\n", __func__);
200 if (adapter_to_pwrctl(adapter)->ips_deny_time >= rtw_get_current_time()) {
201 //DBG_871X("%s ips_deny_time\n", __func__);
205 if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE|WIFI_SITE_MONITOR)
206 || check_fwstate(pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
207 || check_fwstate(pmlmepriv, WIFI_AP_STATE)
208 || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
209 #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211) && defined(CONFIG_P2P_IPS)
210 || pcfg80211_wdinfo->is_ro_ch
211 #elif defined(CONFIG_P2P)
212 || !rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)
214 #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211)
215 || rtw_get_passing_time_ms(pcfg80211_wdinfo->last_ro_ch_time) < 3000
221 /* consider buddy, if exist */
223 struct mlme_priv *b_pmlmepriv = &(buddy->mlmepriv);
225 struct wifidirect_info *b_pwdinfo = &(buddy->wdinfo);
226 #ifdef CONFIG_IOCTL_CFG80211
227 struct cfg80211_wifidirect_info *b_pcfg80211_wdinfo = &buddy->cfg80211_wdinfo;
231 if (check_fwstate(b_pmlmepriv, WIFI_ASOC_STATE|WIFI_SITE_MONITOR)
232 || check_fwstate(b_pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
233 || check_fwstate(b_pmlmepriv, WIFI_AP_STATE)
234 || check_fwstate(b_pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
235 #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211) && defined(CONFIG_P2P_IPS)
236 || b_pcfg80211_wdinfo->is_ro_ch
237 #elif defined(CONFIG_P2P)
238 || !rtw_p2p_chk_state(b_pwdinfo, P2P_STATE_NONE)
240 #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211)
241 || rtw_get_passing_time_ms(b_pcfg80211_wdinfo->last_ro_ch_time) < 3000
249 if (adapter->registrypriv.mp_mode == 1)
253 #ifdef CONFIG_INTEL_PROXIM
254 if(adapter->proximity.proxim_on==_TRUE){
259 if (pxmit_priv->free_xmitbuf_cnt != NR_XMITBUFF ||
260 pxmit_priv->free_xmit_extbuf_cnt != NR_XMIT_EXTBUFF) {
261 DBG_871X_LEVEL(_drv_always_, "There are some pkts to transmit\n");
262 DBG_871X_LEVEL(_drv_always_, "free_xmitbuf_cnt: %d, free_xmit_extbuf_cnt: %d\n",
263 pxmit_priv->free_xmitbuf_cnt, pxmit_priv->free_xmit_extbuf_cnt);
276 * rtw_ps_processor() doesn't handle LPS.
278 void rtw_ps_processor(_adapter*padapter)
281 struct wifidirect_info *pwdinfo = &( padapter->wdinfo );
283 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
284 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
285 struct dvobj_priv *psdpriv = padapter->dvobj;
286 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
287 #ifdef SUPPORT_HW_RFOFF_DETECTED
288 rt_rf_power_state rfpwrstate;
289 #endif //SUPPORT_HW_RFOFF_DETECTED
292 _enter_pwrlock(&adapter_to_pwrctl(padapter)->lock);
293 ps_deny = rtw_ps_deny_get(padapter);
294 _exit_pwrlock(&adapter_to_pwrctl(padapter)->lock);
297 DBG_871X(FUNC_ADPT_FMT ": ps_deny=0x%08X, skip power save!\n",
298 FUNC_ADPT_ARG(padapter), ps_deny);
302 if(pwrpriv->bInSuspend == _TRUE){//system suspend or autosuspend
303 pdbgpriv->dbg_ps_insuspend_cnt++;
304 DBG_871X("%s, pwrpriv->bInSuspend == _TRUE ignore this process\n",__FUNCTION__);
308 pwrpriv->ps_processing = _TRUE;
310 #ifdef SUPPORT_HW_RFOFF_DETECTED
311 if(pwrpriv->bips_processing == _TRUE)
314 //DBG_871X("==> fw report state(0x%x)\n",rtw_read8(padapter,0x1ca));
315 if(pwrpriv->bHWPwrPindetect)
317 #ifdef CONFIG_AUTOSUSPEND
318 if(padapter->registrypriv.usbss_enable)
320 if(pwrpriv->rf_pwrstate == rf_on)
322 if(padapter->net_closed == _TRUE)
323 pwrpriv->ps_flag = _TRUE;
325 rfpwrstate = RfOnOffDetect(padapter);
326 DBG_871X("@@@@- #1 %s==> rfstate:%s \n",__FUNCTION__,(rfpwrstate==rf_on)?"rf_on":"rf_off");
327 if(rfpwrstate!= pwrpriv->rf_pwrstate)
329 if(rfpwrstate == rf_off)
331 pwrpriv->change_rfpwrstate = rf_off;
333 pwrpriv->bkeepfwalive = _TRUE;
334 pwrpriv->brfoffbyhw = _TRUE;
336 autosuspend_enter(padapter);
342 #endif //CONFIG_AUTOSUSPEND
344 rfpwrstate = RfOnOffDetect(padapter);
345 DBG_871X("@@@@- #2 %s==> rfstate:%s \n",__FUNCTION__,(rfpwrstate==rf_on)?"rf_on":"rf_off");
347 if(rfpwrstate!= pwrpriv->rf_pwrstate)
349 if(rfpwrstate == rf_off)
351 pwrpriv->change_rfpwrstate = rf_off;
352 pwrpriv->brfoffbyhw = _TRUE;
353 padapter->bCardDisableWOHSM = _TRUE;
354 rtw_hw_suspend(padapter );
358 pwrpriv->change_rfpwrstate = rf_on;
359 rtw_hw_resume(padapter );
361 DBG_871X("current rf_pwrstate(%s)\n",(pwrpriv->rf_pwrstate == rf_off)?"rf_off":"rf_on");
364 pwrpriv->pwr_state_check_cnts ++;
366 #endif //SUPPORT_HW_RFOFF_DETECTED
368 if (pwrpriv->ips_mode_req == IPS_NONE)
371 if (rtw_pwr_unassociated_idle(padapter) == _FALSE)
374 if((pwrpriv->rf_pwrstate == rf_on) && ((pwrpriv->pwr_state_check_cnts%4)==0))
376 DBG_871X("==>%s .fw_state(%x)\n",__FUNCTION__,get_fwstate(pmlmepriv));
377 #if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
379 pwrpriv->change_rfpwrstate = rf_off;
381 #ifdef CONFIG_AUTOSUSPEND
382 if(padapter->registrypriv.usbss_enable)
384 if(pwrpriv->bHWPwrPindetect)
385 pwrpriv->bkeepfwalive = _TRUE;
387 if(padapter->net_closed == _TRUE)
388 pwrpriv->ps_flag = _TRUE;
390 #if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
391 if (_TRUE==pwrpriv->bInternalAutoSuspend) {
392 DBG_871X("<==%s .pwrpriv->bInternalAutoSuspend)(%x)\n",__FUNCTION__,pwrpriv->bInternalAutoSuspend);
394 pwrpriv->change_rfpwrstate = rf_off;
395 padapter->bCardDisableWOHSM = _TRUE;
396 DBG_871X("<==%s .pwrpriv->bInternalAutoSuspend)(%x) call autosuspend_enter\n",__FUNCTION__,pwrpriv->bInternalAutoSuspend);
397 autosuspend_enter(padapter);
400 padapter->bCardDisableWOHSM = _TRUE;
401 autosuspend_enter(padapter);
402 #endif //if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
404 else if(pwrpriv->bHWPwrPindetect)
408 #endif //CONFIG_AUTOSUSPEND
410 #if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
411 pwrpriv->change_rfpwrstate = rf_off;
412 #endif //defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
420 #ifndef CONFIG_IPS_CHECK_IN_WD
421 rtw_set_pwr_state_check_timer(pwrpriv);
423 pwrpriv->ps_processing = _FALSE;
427 void pwr_state_check_handler(RTW_TIMER_HDL_ARGS);
428 void pwr_state_check_handler(RTW_TIMER_HDL_ARGS)
430 _adapter *padapter = (_adapter *)FunctionContext;
431 rtw_ps_cmd(padapter);
435 void traffic_check_for_leave_lps(PADAPTER padapter, u8 tx, u32 tx_packets)
437 #ifdef CONFIG_CHECK_LEAVE_LPS
438 static u32 start_time = 0;
439 static u32 xmit_cnt = 0;
440 u8 bLeaveLPS = _FALSE;
441 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
447 xmit_cnt += tx_packets;
450 start_time= rtw_get_current_time();
452 if (rtw_get_passing_time_ms(start_time) > 2000) // 2 sec == watch dog timer
456 if ((adapter_to_pwrctl(padapter)->bLeisurePs)
457 && (adapter_to_pwrctl(padapter)->pwr_mode != PS_MODE_ACTIVE)
458 #ifdef CONFIG_BT_COEXIST
459 && (rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
463 DBG_871X("leave lps via Tx = %d\n", xmit_cnt);
468 start_time= rtw_get_current_time();
475 if(pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod > 4/*2*/)
477 if ((adapter_to_pwrctl(padapter)->bLeisurePs)
478 && (adapter_to_pwrctl(padapter)->pwr_mode != PS_MODE_ACTIVE)
479 #ifdef CONFIG_BT_COEXIST
480 && (rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
484 DBG_871X("leave lps via Rx = %d\n", pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod);
492 //DBG_871X("leave lps via %s, Tx = %d, Rx = %d \n", tx?"Tx":"Rx", pmlmepriv->LinkDetectInfo.NumTxOkInPeriod,pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod);
493 //rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, 1);
494 rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, tx?0:1);
496 #endif //CONFIG_CHECK_LEAVE_LPS
501 * This function MUST be called under power lock protect
505 * pslv power state level, only could be PS_STATE_S0 ~ PS_STATE_S4
508 void rtw_set_rpwm(PADAPTER padapter, u8 pslv)
511 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
512 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
514 #endif // CONFIG_DETECT_CPWM_BY_POLLING
515 struct dvobj_priv *psdpriv = padapter->dvobj;
516 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
519 pslv = PS_STATE(pslv);
521 #ifdef CONFIG_LPS_RPWM_TIMER
522 if (pwrpriv->brpwmtimeout == _TRUE)
524 DBG_871X("%s: RPWM timeout, force to set RPWM(0x%02X) again!\n", __FUNCTION__, pslv);
527 #endif // CONFIG_LPS_RPWM_TIMER
529 if ( (pwrpriv->rpwm == pslv)
530 #ifdef CONFIG_LPS_LCLK
531 #ifndef CONFIG_RTL8723A
532 || ((pwrpriv->rpwm >= PS_STATE_S2)&&(pslv >= PS_STATE_S2))
537 RT_TRACE(_module_rtl871x_pwrctrl_c_,_drv_err_,
538 ("%s: Already set rpwm[0x%02X], new=0x%02X!\n", __FUNCTION__, pwrpriv->rpwm, pslv));
543 if ((padapter->bSurpriseRemoved == _TRUE) ||
544 (padapter->hw_init_completed == _FALSE))
546 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
547 ("%s: SurpriseRemoved(%d) hw_init_completed(%d)\n",
548 __FUNCTION__, padapter->bSurpriseRemoved, padapter->hw_init_completed));
550 pwrpriv->cpwm = PS_STATE_S4;
555 if (padapter->bDriverStopped == _TRUE)
557 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
558 ("%s: change power state(0x%02X) when DriverStopped\n", __FUNCTION__, pslv));
560 if (pslv < PS_STATE_S2) {
561 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
562 ("%s: Reject to enter PS_STATE(0x%02X) lower than S2 when DriverStopped!!\n", __FUNCTION__, pslv));
567 rpwm = pslv | pwrpriv->tog;
568 #ifdef CONFIG_LPS_LCLK
569 // only when from PS_STATE S0/S1 to S2 and higher needs ACK
570 if ((pwrpriv->cpwm < PS_STATE_S2) && (pslv >= PS_STATE_S2))
573 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
574 ("rtw_set_rpwm: rpwm=0x%02x cpwm=0x%02x\n", rpwm, pwrpriv->cpwm));
576 pwrpriv->rpwm = pslv;
578 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
582 rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_orig);
586 #if defined(CONFIG_LPS_RPWM_TIMER) && !defined(CONFIG_DETECT_CPWM_BY_POLLING)
588 _set_timer(&pwrpriv->pwr_rpwm_timer, LPS_RPWM_WAIT_MS);
589 #endif // CONFIG_LPS_RPWM_TIMER & !CONFIG_DETECT_CPWM_BY_POLLING
590 rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&rpwm));
592 pwrpriv->tog += 0x80;
594 #ifdef CONFIG_LPS_LCLK
595 // No LPS 32K, No Ack
598 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
603 start_time = rtw_get_current_time();
609 rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_now);
610 if ((cpwm_orig ^ cpwm_now) & 0x80)
612 #ifdef CONFIG_RTL8723A
613 pwrpriv->cpwm = PS_STATE(cpwm_now);
614 #else // !CONFIG_RTL8723A
615 pwrpriv->cpwm = PS_STATE_S4;
616 #endif // !CONFIG_RTL8723A
617 pwrpriv->cpwm_tog = cpwm_now & PS_TOGGLE;
618 #ifdef DBG_CHECK_FW_PS_STATE
619 DBG_871X("%s: polling cpwm OK! poll_cnt=%d, cpwm_orig=%02x, cpwm_now=%02x , 0x100=0x%x\n"
620 , __FUNCTION__,poll_cnt, cpwm_orig, cpwm_now, rtw_read8(padapter, REG_CR));
621 if(rtw_fw_ps_state(padapter) == _FAIL)
623 DBG_871X("leave 32k but fw state in 32k\n");
624 pdbgpriv->dbg_rpwm_toogle_cnt++;
626 #endif //DBG_CHECK_FW_PS_STATE
630 if (rtw_get_passing_time_ms(start_time) > LPS_RPWM_WAIT_MS)
632 DBG_871X("%s: polling cpwm timeout! poll_cnt=%d, cpwm_orig=%02x, cpwm_now=%02x \n", __FUNCTION__,poll_cnt, cpwm_orig, cpwm_now);
633 #ifdef DBG_CHECK_FW_PS_STATE
634 if(rtw_fw_ps_state(padapter) == _FAIL)
636 DBG_871X("rpwm timeout and fw ps state in 32k\n");
637 pdbgpriv->dbg_rpwm_timeout_fail_cnt++;
639 #endif //DBG_CHECK_FW_PS_STATE
640 #ifdef CONFIG_LPS_RPWM_TIMER
641 _set_timer(&pwrpriv->pwr_rpwm_timer, 1);
642 #endif // CONFIG_LPS_RPWM_TIMER
646 #endif // CONFIG_DETECT_CPWM_BY_POLLING
649 #endif // CONFIG_LPS_LCLK
651 pwrpriv->cpwm = pslv;
657 u8 PS_RDY_CHECK(_adapter * padapter)
659 u32 curr_time, delta_time;
660 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
661 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
663 struct wifidirect_info *pwdinfo = &(padapter->wdinfo);
664 #ifdef CONFIG_IOCTL_CFG80211
665 struct cfg80211_wifidirect_info *pcfg80211_wdinfo = &padapter->cfg80211_wdinfo;
666 #endif /* CONFIG_IOCTL_CFG80211 */
667 #endif /* CONFIG_P2P */
669 #if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN)
670 if(_TRUE == pwrpriv->bInSuspend && pwrpriv->wowlan_mode)
672 else if(_TRUE == pwrpriv->bInSuspend && pwrpriv->wowlan_ap_mode)
674 else if (_TRUE == pwrpriv->bInSuspend)
677 if(_TRUE == pwrpriv->bInSuspend )
681 curr_time = rtw_get_current_time();
683 delta_time = curr_time -pwrpriv->DelayLPSLastTimeStamp;
685 if(delta_time < LPS_DELAY_TIME)
690 if (check_fwstate(pmlmepriv, WIFI_SITE_MONITOR)
691 || check_fwstate(pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
692 || check_fwstate(pmlmepriv, WIFI_AP_STATE)
693 || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
694 #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211) && defined(CONFIG_P2P_IPS)
695 || pcfg80211_wdinfo->is_ro_ch
696 #elif defined(CONFIG_P2P)
697 || !rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)
699 || rtw_is_scan_deny(padapter)
701 // TDLS link is established.
702 || ( padapter->tdlsinfo.link_established == _TRUE )
703 #endif // CONFIG_TDLS
707 if( (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) && (padapter->securitypriv.binstallGrpkey == _FALSE) )
709 DBG_871X("Group handshake still in progress !!!\n");
713 #ifdef CONFIG_IOCTL_CFG80211
714 if (!rtw_cfg80211_pwr_mgmt(padapter))
721 void rtw_set_ps_mode(PADAPTER padapter, u8 ps_mode, u8 smart_ps, u8 bcn_ant_mode, const char *msg)
723 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
724 struct dvobj_priv *psdpriv = padapter->dvobj;
725 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
727 struct wifidirect_info *pwdinfo = &( padapter->wdinfo );
730 struct sta_priv *pstapriv = &padapter->stapriv;
733 _list *plist, *phead;
734 struct sta_info *ptdls_sta;
739 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
740 ("%s: PowerMode=%d Smart_PS=%d\n",
741 __FUNCTION__, ps_mode, smart_ps));
743 if(ps_mode > PM_Card_Disable) {
744 RT_TRACE(_module_rtl871x_pwrctrl_c_,_drv_err_,("ps_mode:%d error\n", ps_mode));
748 if (pwrpriv->pwr_mode == ps_mode)
750 if (PS_MODE_ACTIVE == ps_mode) return;
752 #ifndef CONFIG_BT_COEXIST
753 if ((pwrpriv->smart_ps == smart_ps) &&
754 (pwrpriv->bcn_ant_mode == bcn_ant_mode))
758 #endif // !CONFIG_BT_COEXIST
761 #ifdef CONFIG_LPS_LCLK
762 _enter_pwrlock(&pwrpriv->lock);
765 //if(pwrpriv->pwr_mode == PS_MODE_ACTIVE)
766 if(ps_mode == PS_MODE_ACTIVE)
769 #ifdef CONFIG_BT_COEXIST
770 && (((rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
772 && (pwdinfo->opp_ps == 0)
773 #endif // CONFIG_P2P_PS
775 || ((rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
776 && (rtw_btcoex_IsLpsOn(padapter) == _FALSE))
778 #else // !CONFIG_BT_COEXIST
780 && (pwdinfo->opp_ps == 0)
781 #endif // CONFIG_P2P_PS
782 #endif // !CONFIG_BT_COEXIST
785 DBG_871X(FUNC_ADPT_FMT" Leave 802.11 power save - %s\n",
786 FUNC_ADPT_ARG(padapter), msg);
789 _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
791 for(i=0; i< NUM_STA; i++)
793 phead = &(pstapriv->sta_hash[i]);
794 plist = get_next(phead);
796 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE)
798 ptdls_sta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
800 if( ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE )
801 issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta->hwaddr, 0, 0, 0);
802 plist = get_next(plist);
806 _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
809 pwrpriv->pwr_mode = ps_mode;
810 rtw_set_rpwm(padapter, PS_STATE_S4);
812 #if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN)
813 if (pwrpriv->wowlan_mode == _TRUE ||
814 pwrpriv->wowlan_ap_mode == _TRUE)
816 u32 start_time, delay_ms;
819 start_time = rtw_get_current_time();
821 rtw_hal_get_hwreg(padapter, HW_VAR_SYS_CLKR, &val8);
822 if (!(val8 & BIT(4))){ //0x08 bit4 =1 --> in 32k, bit4 = 0 --> leave 32k
823 pwrpriv->cpwm = PS_STATE_S4;
826 if (rtw_get_passing_time_ms(start_time) > delay_ms)
828 DBG_871X("%s: Wait for FW 32K leave more than %u ms!!!\n",
829 __FUNCTION__, delay_ms);
830 pdbgpriv->dbg_wow_leave_ps_fail_cnt++;
837 rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
838 pwrpriv->bFwCurrentInPSMode = _FALSE;
840 #ifdef CONFIG_BT_COEXIST
841 rtw_btcoex_LpsNotify(padapter, ps_mode);
842 #endif // CONFIG_BT_COEXIST
847 if ((PS_RDY_CHECK(padapter) && check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE))
848 #ifdef CONFIG_BT_COEXIST
849 || ((rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
850 && (rtw_btcoex_IsLpsOn(padapter) == _TRUE))
856 DBG_871X(FUNC_ADPT_FMT" Enter 802.11 power save - %s\n",
857 FUNC_ADPT_ARG(padapter), msg);
860 _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
862 for(i=0; i< NUM_STA; i++)
864 phead = &(pstapriv->sta_hash[i]);
865 plist = get_next(phead);
867 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE)
869 ptdls_sta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
871 if( ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE )
872 issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta->hwaddr, 1, 0, 0);
873 plist = get_next(plist);
877 _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
880 #ifdef CONFIG_BT_COEXIST
881 rtw_btcoex_LpsNotify(padapter, ps_mode);
882 #endif // CONFIG_BT_COEXIST
884 pwrpriv->bFwCurrentInPSMode = _TRUE;
885 pwrpriv->pwr_mode = ps_mode;
886 pwrpriv->smart_ps = smart_ps;
887 pwrpriv->bcn_ant_mode = bcn_ant_mode;
888 rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
891 // Set CTWindow after LPS
892 if(pwdinfo->opp_ps == 1)
893 p2p_ps_wk_cmd(padapter, P2P_PS_ENABLE, 0);
894 #endif //CONFIG_P2P_PS
897 #ifdef CONFIG_LPS_LCLK
898 if (pwrpriv->alives == 0)
900 #endif // CONFIG_LPS_LCLK
902 #ifdef CONFIG_BT_COEXIST
903 if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
904 && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE))
908 val8 = rtw_btcoex_LpsVal(padapter);
912 #ifdef CONFIG_RTL8723A
913 val8 = rtw_btcoex_RpwmVal(padapter);
924 #endif // CONFIG_RTL8723A
926 #endif // CONFIG_BT_COEXIST
928 rtw_set_rpwm(padapter, pslv);
932 #ifdef CONFIG_LPS_LCLK
933 _exit_pwrlock(&pwrpriv->lock);
945 s32 LPS_RF_ON_check(PADAPTER padapter, u32 delay_ms)
952 start_time = rtw_get_current_time();
955 rtw_hal_get_hwreg(padapter, HW_VAR_FWLPS_RF_ON, &bAwake);
959 if (_TRUE == padapter->bSurpriseRemoved)
962 DBG_871X("%s: device surprise removed!!\n", __FUNCTION__);
966 if (rtw_get_passing_time_ms(start_time) > delay_ms)
969 DBG_871X("%s: Wait for FW LPS leave more than %u ms!!!\n", __FUNCTION__, delay_ms);
980 // Enter the leisure power save mode.
982 void LPS_Enter(PADAPTER padapter, const char *msg)
984 struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
985 struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
986 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
987 _adapter *buddy = padapter->pbuddy_adapter;
988 int n_assoc_iface = 0;
994 // DBG_871X("+LeisurePSEnter\n");
996 #ifdef CONFIG_BT_COEXIST
997 if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1001 /* Skip lps enter request if number of assocated adapters is not 1 */
1002 for (i = 0; i < dvobj->iface_nums; i++) {
1003 if (check_fwstate(&(dvobj->padapters[i]->mlmepriv), WIFI_ASOC_STATE))
1006 if (n_assoc_iface != 1)
1009 /* Skip lps enter request for adapter not port0 */
1010 if (get_iface_type(padapter) != IFACE_PORT0)
1013 for (i = 0; i < dvobj->iface_nums; i++) {
1014 if (PS_RDY_CHECK(dvobj->padapters[i]) == _FALSE)
1018 if (pwrpriv->bLeisurePs)
1020 // Idle for a while if we connect to AP a while ago.
1021 if (pwrpriv->LpsIdleCount >= 2) // 4 Sec
1023 if(pwrpriv->pwr_mode == PS_MODE_ACTIVE)
1025 sprintf(buf, "WIFI-%s", msg);
1026 pwrpriv->bpower_saving = _TRUE;
1027 rtw_set_ps_mode(padapter, pwrpriv->power_mgnt, padapter->registrypriv.smart_ps, 0, buf);
1031 pwrpriv->LpsIdleCount++;
1034 // DBG_871X("-LeisurePSEnter\n");
1041 // Leave the leisure power save mode.
1043 void LPS_Leave(PADAPTER padapter, const char *msg)
1045 #define LPS_LEAVE_TIMEOUT_MS 100
1047 struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1048 struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
1052 struct debug_priv *pdbgpriv = &dvobj->drv_dbg;
1056 // DBG_871X("+LeisurePSLeave\n");
1058 #ifdef CONFIG_BT_COEXIST
1059 if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1063 if (pwrpriv->bLeisurePs)
1065 if(pwrpriv->pwr_mode != PS_MODE_ACTIVE)
1067 sprintf(buf, "WIFI-%s", msg);
1068 rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0, buf);
1070 if(pwrpriv->pwr_mode == PS_MODE_ACTIVE)
1071 LPS_RF_ON_check(padapter, LPS_LEAVE_TIMEOUT_MS);
1075 pwrpriv->bpower_saving = _FALSE;
1076 #ifdef DBG_CHECK_FW_PS_STATE
1077 if(rtw_fw_ps_state(padapter) == _FAIL)
1079 DBG_871X("leave lps, fw in 32k\n");
1080 pdbgpriv->dbg_leave_lps_fail_cnt++;
1082 #endif //DBG_CHECK_FW_PS_STATE
1083 // DBG_871X("-LeisurePSLeave\n");
1089 void LeaveAllPowerSaveModeDirect(PADAPTER Adapter)
1091 PADAPTER pri_padapter = GET_PRIMARY_ADAPTER(Adapter);
1092 struct mlme_priv *pmlmepriv = &(Adapter->mlmepriv);
1093 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(Adapter);
1094 struct dvobj_priv *psdpriv = Adapter->dvobj;
1095 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
1096 #ifndef CONFIG_DETECT_CPWM_BY_POLLING
1097 u8 cpwm_orig, cpwm_now;
1099 #endif // CONFIG_DETECT_CPWM_BY_POLLING
1103 DBG_871X("%s.....\n",__FUNCTION__);
1105 if (_TRUE == Adapter->bSurpriseRemoved)
1107 DBG_871X(FUNC_ADPT_FMT ": bSurpriseRemoved=%d Skip!\n",
1108 FUNC_ADPT_ARG(Adapter), Adapter->bSurpriseRemoved);
1112 if ((check_fwstate(pmlmepriv, _FW_LINKED) == _TRUE)
1113 #ifdef CONFIG_CONCURRENT_MODE
1114 || (check_buddy_fwstate(Adapter,_FW_LINKED) == _TRUE)
1119 if(pwrpriv->pwr_mode == PS_MODE_ACTIVE) {
1120 DBG_871X("%s: Driver Already Leave LPS\n",__FUNCTION__);
1124 #ifdef CONFIG_LPS_LCLK
1125 _enter_pwrlock(&pwrpriv->lock);
1127 #ifndef CONFIG_DETECT_CPWM_BY_POLLING
1129 rtw_hal_get_hwreg(Adapter, HW_VAR_CPWM, &cpwm_orig);
1130 #endif //CONFIG_DETECT_CPWM_BY_POLLING
1131 rtw_set_rpwm(Adapter, PS_STATE_S4);
1133 #ifndef CONFIG_DETECT_CPWM_BY_POLLING
1135 start_time = rtw_get_current_time();
1141 rtw_hal_get_hwreg(Adapter, HW_VAR_CPWM, &cpwm_now);
1142 if ((cpwm_orig ^ cpwm_now) & 0x80)
1144 #ifdef CONFIG_RTL8723A
1145 pwrpriv->cpwm = PS_STATE(cpwm_now);
1146 #else // !CONFIG_RTL8723A
1147 pwrpriv->cpwm = PS_STATE_S4;
1148 #endif // !CONFIG_RTL8723A
1149 pwrpriv->cpwm_tog = cpwm_now & PS_TOGGLE;
1150 #ifdef DBG_CHECK_FW_PS_STATE
1151 DBG_871X("%s: polling cpwm OK! cpwm_orig=%02x, cpwm_now=%02x, 0x100=0x%x \n"
1152 , __FUNCTION__, cpwm_orig, cpwm_now, rtw_read8(Adapter, REG_CR));
1153 if(rtw_fw_ps_state(Adapter) == _FAIL)
1155 DBG_871X("%s: leave 32k but fw state in 32k\n", __FUNCTION__);
1156 pdbgpriv->dbg_rpwm_toogle_cnt++;
1158 #endif //DBG_CHECK_FW_PS_STATE
1162 if (rtw_get_passing_time_ms(start_time) > LPS_RPWM_WAIT_MS)
1164 DBG_871X("%s: polling cpwm timeout! cpwm_orig=%02x, cpwm_now=%02x \n", __FUNCTION__, cpwm_orig, cpwm_now);
1165 #ifdef DBG_CHECK_FW_PS_STATE
1166 if(rtw_fw_ps_state(Adapter) == _FAIL)
1168 DBG_871X("rpwm timeout and fw ps state in 32k\n");
1169 pdbgpriv->dbg_rpwm_timeout_fail_cnt++;
1171 #endif //DBG_CHECK_FW_PS_STATE
1175 #endif // CONFIG_DETECT_CPWM_BY_POLLING
1177 _exit_pwrlock(&pwrpriv->lock);
1180 #ifdef CONFIG_P2P_PS
1181 p2p_ps_wk_cmd(pri_padapter, P2P_PS_DISABLE, 0);
1182 #endif //CONFIG_P2P_PS
1185 rtw_lps_ctrl_wk_cmd(pri_padapter, LPS_CTRL_LEAVE, 0);
1190 if(pwrpriv->rf_pwrstate== rf_off)
1192 #ifdef CONFIG_AUTOSUSPEND
1193 if(Adapter->registrypriv.usbss_enable)
1195 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1196 usb_disable_autosuspend(adapter_to_dvobj(Adapter)->pusbdev);
1197 #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,22) && LINUX_VERSION_CODE<=KERNEL_VERSION(2,6,34))
1198 adapter_to_dvobj(Adapter)->pusbdev->autosuspend_disabled = Adapter->bDisableAutosuspend;//autosuspend disabled by the user
1204 #if defined(CONFIG_FWLPS_IN_IPS) || defined(CONFIG_SWLPS_IN_IPS) || defined(CONFIG_RTL8188E)
1206 if(_FALSE == ips_leave(pri_padapter))
1208 DBG_871X("======> ips_leave fail.............\n");
1211 #endif //CONFIG_SWLPS_IN_IPS || (CONFIG_PLATFORM_SPRD && CONFIG_RTL8188E)
1220 // Description: Leave all power save mode: LPS, FwLPS, IPS if needed.
1221 // Move code to function by tynli. 2010.03.26.
1223 void LeaveAllPowerSaveMode(IN PADAPTER Adapter)
1225 struct dvobj_priv *dvobj = adapter_to_dvobj(Adapter);
1226 struct mlme_priv *pmlmepriv = &(Adapter->mlmepriv);
1228 int n_assoc_iface = 0;
1233 //DBG_871X("%s.....\n",__FUNCTION__);
1235 if (_FALSE == Adapter->bup)
1237 DBG_871X(FUNC_ADPT_FMT ": bup=%d Skip!\n",
1238 FUNC_ADPT_ARG(Adapter), Adapter->bup);
1242 if (_TRUE == Adapter->bSurpriseRemoved)
1244 DBG_871X(FUNC_ADPT_FMT ": bSurpriseRemoved=%d Skip!\n",
1245 FUNC_ADPT_ARG(Adapter), Adapter->bSurpriseRemoved);
1249 for (i = 0; i < dvobj->iface_nums; i++) {
1250 if (check_fwstate(&(dvobj->padapters[i]->mlmepriv), WIFI_ASOC_STATE))
1256 #ifdef CONFIG_LPS_LCLK
1260 #ifdef CONFIG_P2P_PS
1261 p2p_ps_wk_cmd(Adapter, P2P_PS_DISABLE, enqueue);
1262 #endif //CONFIG_P2P_PS
1265 rtw_lps_ctrl_wk_cmd(Adapter, LPS_CTRL_LEAVE, enqueue);
1268 #ifdef CONFIG_LPS_LCLK
1269 LPS_Leave_check(Adapter);
1274 if(adapter_to_pwrctl(Adapter)->rf_pwrstate== rf_off)
1276 #ifdef CONFIG_AUTOSUSPEND
1277 if(Adapter->registrypriv.usbss_enable)
1279 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1280 usb_disable_autosuspend(adapter_to_dvobj(Adapter)->pusbdev);
1281 #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,22) && LINUX_VERSION_CODE<=KERNEL_VERSION(2,6,34))
1282 adapter_to_dvobj(Adapter)->pusbdev->autosuspend_disabled = Adapter->bDisableAutosuspend;//autosuspend disabled by the user
1288 #if defined(CONFIG_FWLPS_IN_IPS) || defined(CONFIG_SWLPS_IN_IPS) || (defined(CONFIG_PLATFORM_SPRD) && defined(CONFIG_RTL8188E))
1290 if(_FALSE == ips_leave(Adapter))
1292 DBG_871X("======> ips_leave fail.............\n");
1295 #endif //CONFIG_SWLPS_IN_IPS || (CONFIG_PLATFORM_SPRD && CONFIG_RTL8188E)
1303 #ifdef CONFIG_LPS_LCLK
1304 void LPS_Leave_check(
1307 struct pwrctrl_priv *pwrpriv;
1313 pwrpriv = adapter_to_pwrctl(padapter);
1316 start_time = rtw_get_current_time();
1322 _enter_pwrlock(&pwrpriv->lock);
1324 if ((padapter->bSurpriseRemoved == _TRUE)
1325 || (padapter->hw_init_completed == _FALSE)
1326 #ifdef CONFIG_USB_HCI
1327 || (padapter->bDriverStopped== _TRUE)
1329 || (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
1335 _exit_pwrlock(&pwrpriv->lock);
1340 if(rtw_get_passing_time_ms(start_time)>100)
1342 DBG_871X("Wait for cpwm event than 100 ms!!!\n");
1352 * Caller:ISR handler...
1354 * This will be called when CPWM interrupt is up.
1356 * using to update cpwn of drv; and drv willl make a decision to up or down pwr level
1360 struct reportpwrstate_parm *preportpwrstate)
1362 struct pwrctrl_priv *pwrpriv;
1366 pwrpriv = adapter_to_pwrctl(padapter);
1368 if (pwrpriv->cpwm_tog == (preportpwrstate->state & PS_TOGGLE)) {
1369 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
1370 ("cpwm_int_hdl: tog(old)=0x%02x cpwm(new)=0x%02x toggle bit didn't change!?\n",
1371 pwrpriv->cpwm_tog, preportpwrstate->state));
1376 _enter_pwrlock(&pwrpriv->lock);
1378 #ifdef CONFIG_LPS_RPWM_TIMER
1379 if (pwrpriv->rpwm < PS_STATE_S2)
1381 DBG_871X("%s: Redundant CPWM Int. RPWM=0x%02X CPWM=0x%02x\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1382 _exit_pwrlock(&pwrpriv->lock);
1385 #endif // CONFIG_LPS_RPWM_TIMER
1387 pwrpriv->cpwm = PS_STATE(preportpwrstate->state);
1388 pwrpriv->cpwm_tog = preportpwrstate->state & PS_TOGGLE;
1390 if (pwrpriv->cpwm >= PS_STATE_S2)
1392 if (pwrpriv->alives & CMD_ALIVE)
1393 _rtw_up_sema(&padapter->cmdpriv.cmd_queue_sema);
1395 if (pwrpriv->alives & XMIT_ALIVE)
1396 _rtw_up_sema(&padapter->xmitpriv.xmit_sema);
1399 _exit_pwrlock(&pwrpriv->lock);
1402 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
1403 ("cpwm_int_hdl: cpwm=0x%02x\n", pwrpriv->cpwm));
1408 static void cpwm_event_callback(struct work_struct *work)
1410 struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, cpwm_event);
1411 struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
1412 _adapter *adapter = dvobj->if1;
1413 struct reportpwrstate_parm report;
1415 //DBG_871X("%s\n",__FUNCTION__);
1417 report.state = PS_STATE_S2;
1418 cpwm_int_hdl(adapter, &report);
1421 #ifdef CONFIG_LPS_RPWM_TIMER
1422 static void rpwmtimeout_workitem_callback(struct work_struct *work)
1425 struct dvobj_priv *dvobj;
1426 struct pwrctrl_priv *pwrpriv;
1429 pwrpriv = container_of(work, struct pwrctrl_priv, rpwmtimeoutwi);
1430 dvobj = pwrctl_to_dvobj(pwrpriv);
1431 padapter = dvobj->if1;
1432 // DBG_871X("+%s: rpwm=0x%02X cpwm=0x%02X\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1434 _enter_pwrlock(&pwrpriv->lock);
1435 if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
1437 DBG_871X("%s: rpwm=0x%02X cpwm=0x%02X CPWM done!\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1440 _exit_pwrlock(&pwrpriv->lock);
1442 if (rtw_read8(padapter, 0x100) != 0xEA)
1445 struct reportpwrstate_parm report;
1447 report.state = PS_STATE_S2;
1448 DBG_871X("\n%s: FW already leave 32K!\n\n", __func__);
1449 cpwm_int_hdl(padapter, &report);
1451 DBG_871X("\n%s: FW already leave 32K!\n\n", __func__);
1452 cpwm_event_callback(&pwrpriv->cpwm_event);
1457 _enter_pwrlock(&pwrpriv->lock);
1459 if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
1461 DBG_871X("%s: cpwm=%d, nothing to do!\n", __func__, pwrpriv->cpwm);
1464 pwrpriv->brpwmtimeout = _TRUE;
1465 rtw_set_rpwm(padapter, pwrpriv->rpwm);
1466 pwrpriv->brpwmtimeout = _FALSE;
1469 _exit_pwrlock(&pwrpriv->lock);
1473 * This function is a timer handler, can't do any IO in it.
1475 static void pwr_rpwm_timeout_handler(void *FunctionContext)
1478 struct pwrctrl_priv *pwrpriv;
1481 padapter = (PADAPTER)FunctionContext;
1482 pwrpriv = adapter_to_pwrctl(padapter);
1483 DBG_871X("+%s: rpwm=0x%02X cpwm=0x%02X\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1485 if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
1487 DBG_871X("+%s: cpwm=%d, nothing to do!\n", __func__, pwrpriv->cpwm);
1491 _set_workitem(&pwrpriv->rpwmtimeoutwi);
1493 #endif // CONFIG_LPS_RPWM_TIMER
1495 __inline static void register_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
1497 pwrctrl->alives |= tag;
1500 __inline static void unregister_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
1502 pwrctrl->alives &= ~tag;
1508 * Check if the fw_pwrstate is okay for I/O.
1509 * If not (cpwm is less than S2), then the sub-routine
1510 * will raise the cpwm to be greater than or equal to S2.
1512 * Calling Context: Passive
1515 * 1. this function will request pwrctrl->lock
1518 * _SUCCESS hardware is ready for I/O
1519 * _FAIL can't I/O right now
1521 s32 rtw_register_task_alive(PADAPTER padapter, u32 task)
1524 struct pwrctrl_priv *pwrctrl;
1530 pwrctrl = adapter_to_pwrctl(padapter);
1533 #if defined(CONFIG_RTL8723A) && defined(CONFIG_BT_COEXIST)
1534 if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1537 btcoex_rpwm = rtw_btcoex_RpwmVal(padapter);
1538 switch (btcoex_rpwm)
1549 #endif // CONFIG_RTL8723A & CONFIG_BT_COEXIST
1551 _enter_pwrlock(&pwrctrl->lock);
1553 register_task_alive(pwrctrl, task);
1555 if (pwrctrl->bFwCurrentInPSMode == _TRUE)
1557 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
1558 ("%s: task=0x%x cpwm=0x%02x alives=0x%08x\n",
1559 __FUNCTION__, task, pwrctrl->cpwm, pwrctrl->alives));
1561 if (pwrctrl->cpwm < pslv)
1563 if (pwrctrl->cpwm < PS_STATE_S2)
1565 if (pwrctrl->rpwm < pslv)
1566 rtw_set_rpwm(padapter, pslv);
1570 _exit_pwrlock(&pwrctrl->lock);
1572 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1575 if (pwrctrl->cpwm >= PS_STATE_S2)
1578 #endif // CONFIG_DETECT_CPWM_BY_POLLING
1587 * If task is done, call this func. to power down firmware again.
1590 * 1. this function will request pwrctrl->lock
1595 void rtw_unregister_task_alive(PADAPTER padapter, u32 task)
1597 struct pwrctrl_priv *pwrctrl;
1602 pwrctrl = adapter_to_pwrctl(padapter);
1605 #ifdef CONFIG_BT_COEXIST
1606 if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
1607 && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE))
1611 val8 = rtw_btcoex_LpsVal(padapter);
1615 #ifdef CONFIG_RTL8723A
1616 val8 = rtw_btcoex_RpwmVal(padapter);
1627 #endif // CONFIG_RTL8723A
1629 #endif // CONFIG_BT_COEXIST
1631 _enter_pwrlock(&pwrctrl->lock);
1633 unregister_task_alive(pwrctrl, task);
1635 if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
1636 && (pwrctrl->bFwCurrentInPSMode == _TRUE))
1638 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
1639 ("%s: cpwm=0x%02x alives=0x%08x\n",
1640 __FUNCTION__, pwrctrl->cpwm, pwrctrl->alives));
1642 if (pwrctrl->cpwm > pslv)
1644 if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
1645 rtw_set_rpwm(padapter, pslv);
1649 _exit_pwrlock(&pwrctrl->lock);
1655 * Caller: rtw_xmit_thread
1657 * Check if the fw_pwrstate is okay for xmit.
1658 * If not (cpwm is less than S3), then the sub-routine
1659 * will raise the cpwm to be greater than or equal to S3.
1661 * Calling Context: Passive
1664 * _SUCCESS rtw_xmit_thread can write fifo/txcmd afterwards.
1665 * _FAIL rtw_xmit_thread can not do anything.
1667 s32 rtw_register_tx_alive(PADAPTER padapter)
1670 struct pwrctrl_priv *pwrctrl;
1676 pwrctrl = adapter_to_pwrctl(padapter);
1679 #if defined(CONFIG_RTL8723A) && defined(CONFIG_BT_COEXIST)
1680 if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1683 btcoex_rpwm = rtw_btcoex_RpwmVal(padapter);
1684 switch (btcoex_rpwm)
1695 #endif // CONFIG_RTL8723A & CONFIG_BT_COEXIST
1697 _enter_pwrlock(&pwrctrl->lock);
1699 register_task_alive(pwrctrl, XMIT_ALIVE);
1701 if (pwrctrl->bFwCurrentInPSMode == _TRUE)
1703 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
1704 ("rtw_register_tx_alive: cpwm=0x%02x alives=0x%08x\n",
1705 pwrctrl->cpwm, pwrctrl->alives));
1707 if (pwrctrl->cpwm < pslv)
1709 if (pwrctrl->cpwm < PS_STATE_S2)
1711 if (pwrctrl->rpwm < pslv)
1712 rtw_set_rpwm(padapter, pslv);
1716 _exit_pwrlock(&pwrctrl->lock);
1718 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1721 if (pwrctrl->cpwm >= PS_STATE_S2)
1724 #endif // CONFIG_DETECT_CPWM_BY_POLLING
1732 * Caller: rtw_cmd_thread
1734 * Check if the fw_pwrstate is okay for issuing cmd.
1735 * If not (cpwm should be is less than S2), then the sub-routine
1736 * will raise the cpwm to be greater than or equal to S2.
1738 * Calling Context: Passive
1741 * _SUCCESS rtw_cmd_thread can issue cmds to firmware afterwards.
1742 * _FAIL rtw_cmd_thread can not do anything.
1744 s32 rtw_register_cmd_alive(PADAPTER padapter)
1747 struct pwrctrl_priv *pwrctrl;
1753 pwrctrl = adapter_to_pwrctl(padapter);
1756 #if defined(CONFIG_RTL8723A) && defined(CONFIG_BT_COEXIST)
1757 if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1760 btcoex_rpwm = rtw_btcoex_RpwmVal(padapter);
1761 switch (btcoex_rpwm)
1772 #endif // CONFIG_RTL8723A & CONFIG_BT_COEXIST
1774 _enter_pwrlock(&pwrctrl->lock);
1776 register_task_alive(pwrctrl, CMD_ALIVE);
1778 if (pwrctrl->bFwCurrentInPSMode == _TRUE)
1780 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_info_,
1781 ("rtw_register_cmd_alive: cpwm=0x%02x alives=0x%08x\n",
1782 pwrctrl->cpwm, pwrctrl->alives));
1784 if (pwrctrl->cpwm < pslv)
1786 if (pwrctrl->cpwm < PS_STATE_S2)
1788 if (pwrctrl->rpwm < pslv)
1789 rtw_set_rpwm(padapter, pslv);
1793 _exit_pwrlock(&pwrctrl->lock);
1795 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1798 if (pwrctrl->cpwm >= PS_STATE_S2)
1801 #endif // CONFIG_DETECT_CPWM_BY_POLLING
1811 * Calling Context: Dispatch/ISR
1817 s32 rtw_register_rx_alive(PADAPTER padapter)
1819 struct pwrctrl_priv *pwrctrl;
1823 pwrctrl = adapter_to_pwrctl(padapter);
1825 _enter_pwrlock(&pwrctrl->lock);
1827 register_task_alive(pwrctrl, RECV_ALIVE);
1828 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
1829 ("rtw_register_rx_alive: cpwm=0x%02x alives=0x%08x\n",
1830 pwrctrl->cpwm, pwrctrl->alives));
1832 _exit_pwrlock(&pwrctrl->lock);
1840 * Caller: evt_isr or evt_thread
1842 * Calling Context: Dispatch/ISR or Passive
1848 s32 rtw_register_evt_alive(PADAPTER padapter)
1850 struct pwrctrl_priv *pwrctrl;
1854 pwrctrl = adapter_to_pwrctl(padapter);
1856 _enter_pwrlock(&pwrctrl->lock);
1858 register_task_alive(pwrctrl, EVT_ALIVE);
1859 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
1860 ("rtw_register_evt_alive: cpwm=0x%02x alives=0x%08x\n",
1861 pwrctrl->cpwm, pwrctrl->alives));
1863 _exit_pwrlock(&pwrctrl->lock);
1874 * No more pkts for TX,
1875 * Then driver shall call this fun. to power down firmware again.
1877 void rtw_unregister_tx_alive(PADAPTER padapter)
1879 struct pwrctrl_priv *pwrctrl;
1884 pwrctrl = adapter_to_pwrctl(padapter);
1887 #ifdef CONFIG_BT_COEXIST
1888 if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
1889 && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE))
1893 val8 = rtw_btcoex_LpsVal(padapter);
1897 #ifdef CONFIG_RTL8723A
1898 val8 = rtw_btcoex_RpwmVal(padapter);
1909 #endif // CONFIG_RTL8723A
1911 #endif // CONFIG_BT_COEXIST
1913 #ifdef CONFIG_P2P_PS
1914 if(padapter->wdinfo.p2p_ps_mode > P2P_PS_NONE)
1918 #ifdef CONFIG_CONCURRENT_MODE
1919 else if(rtw_buddy_adapter_up(padapter))
1921 if(padapter->pbuddy_adapter->wdinfo.p2p_ps_mode > P2P_PS_NONE)
1927 _enter_pwrlock(&pwrctrl->lock);
1929 unregister_task_alive(pwrctrl, XMIT_ALIVE);
1931 if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
1932 && (pwrctrl->bFwCurrentInPSMode == _TRUE))
1934 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
1935 ("%s: cpwm=0x%02x alives=0x%08x\n",
1936 __FUNCTION__, pwrctrl->cpwm, pwrctrl->alives));
1938 if (pwrctrl->cpwm > pslv)
1940 if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
1941 rtw_set_rpwm(padapter, pslv);
1945 _exit_pwrlock(&pwrctrl->lock);
1953 * If all commands have been done,
1954 * and no more command to do,
1955 * then driver shall call this fun. to power down firmware again.
1957 void rtw_unregister_cmd_alive(PADAPTER padapter)
1959 struct pwrctrl_priv *pwrctrl;
1964 pwrctrl = adapter_to_pwrctl(padapter);
1967 #ifdef CONFIG_BT_COEXIST
1968 if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
1969 && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE))
1973 val8 = rtw_btcoex_LpsVal(padapter);
1977 #ifdef CONFIG_RTL8723A
1978 val8 = rtw_btcoex_RpwmVal(padapter);
1989 #endif // CONFIG_RTL8723A
1991 #endif // CONFIG_BT_COEXIST
1993 #ifdef CONFIG_P2P_PS
1994 if(padapter->wdinfo.p2p_ps_mode > P2P_PS_NONE)
1998 #ifdef CONFIG_CONCURRENT_MODE
1999 else if(rtw_buddy_adapter_up(padapter))
2001 if(padapter->pbuddy_adapter->wdinfo.p2p_ps_mode > P2P_PS_NONE)
2007 _enter_pwrlock(&pwrctrl->lock);
2009 unregister_task_alive(pwrctrl, CMD_ALIVE);
2011 if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
2012 && (pwrctrl->bFwCurrentInPSMode == _TRUE))
2014 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_info_,
2015 ("%s: cpwm=0x%02x alives=0x%08x\n",
2016 __FUNCTION__, pwrctrl->cpwm, pwrctrl->alives));
2018 if (pwrctrl->cpwm > pslv)
2020 if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
2021 rtw_set_rpwm(padapter, pslv);
2025 _exit_pwrlock(&pwrctrl->lock);
2033 void rtw_unregister_rx_alive(PADAPTER padapter)
2035 struct pwrctrl_priv *pwrctrl;
2039 pwrctrl = adapter_to_pwrctl(padapter);
2041 _enter_pwrlock(&pwrctrl->lock);
2043 unregister_task_alive(pwrctrl, RECV_ALIVE);
2045 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
2046 ("rtw_unregister_rx_alive: cpwm=0x%02x alives=0x%08x\n",
2047 pwrctrl->cpwm, pwrctrl->alives));
2049 _exit_pwrlock(&pwrctrl->lock);
2054 void rtw_unregister_evt_alive(PADAPTER padapter)
2056 struct pwrctrl_priv *pwrctrl;
2060 pwrctrl = adapter_to_pwrctl(padapter);
2062 unregister_task_alive(pwrctrl, EVT_ALIVE);
2064 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
2065 ("rtw_unregister_evt_alive: cpwm=0x%02x alives=0x%08x\n",
2066 pwrctrl->cpwm, pwrctrl->alives));
2068 _exit_pwrlock(&pwrctrl->lock);
2072 #endif /* CONFIG_LPS_LCLK */
2074 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2075 static void resume_workitem_callback(struct work_struct *work);
2076 #endif //CONFIG_RESUME_IN_WORKQUEUE
2078 void rtw_init_pwrctrl_priv(PADAPTER padapter)
2080 struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2082 #if defined(CONFIG_CONCURRENT_MODE)
2083 if (padapter->adapter_type != PRIMARY_ADAPTER)
2089 #ifdef PLATFORM_WINDOWS
2090 pwrctrlpriv->pnp_current_pwr_state=NdisDeviceStateD0;
2093 _init_pwrlock(&pwrctrlpriv->lock);
2094 _init_pwrlock(&pwrctrlpriv->check_32k_lock);
2095 pwrctrlpriv->rf_pwrstate = rf_on;
2096 pwrctrlpriv->ips_enter_cnts=0;
2097 pwrctrlpriv->ips_leave_cnts=0;
2098 pwrctrlpriv->bips_processing = _FALSE;
2100 pwrctrlpriv->ips_mode = padapter->registrypriv.ips_mode;
2101 pwrctrlpriv->ips_mode_req = padapter->registrypriv.ips_mode;
2103 pwrctrlpriv->pwr_state_check_interval = RTW_PWR_STATE_CHK_INTERVAL;
2104 pwrctrlpriv->pwr_state_check_cnts = 0;
2105 pwrctrlpriv->bInternalAutoSuspend = _FALSE;
2106 pwrctrlpriv->bInSuspend = _FALSE;
2107 pwrctrlpriv->bkeepfwalive = _FALSE;
2109 #ifdef CONFIG_AUTOSUSPEND
2110 #ifdef SUPPORT_HW_RFOFF_DETECTED
2111 pwrctrlpriv->pwr_state_check_interval = (pwrctrlpriv->bHWPwrPindetect) ?1000:2000;
2115 pwrctrlpriv->LpsIdleCount = 0;
2116 //pwrctrlpriv->FWCtrlPSMode =padapter->registrypriv.power_mgnt;// PS_MODE_MIN;
2117 if (padapter->registrypriv.mp_mode == 1)
2118 pwrctrlpriv->power_mgnt =PS_MODE_ACTIVE ;
2120 pwrctrlpriv->power_mgnt =padapter->registrypriv.power_mgnt;// PS_MODE_MIN;
2121 pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt)?_TRUE:_FALSE;
2123 pwrctrlpriv->bFwCurrentInPSMode = _FALSE;
2125 pwrctrlpriv->rpwm = 0;
2126 pwrctrlpriv->cpwm = PS_STATE_S4;
2128 pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
2129 pwrctrlpriv->smart_ps = padapter->registrypriv.smart_ps;
2130 pwrctrlpriv->bcn_ant_mode = 0;
2131 pwrctrlpriv->dtim = 0;
2133 pwrctrlpriv->tog = 0x80;
2135 #ifdef CONFIG_LPS_LCLK
2136 rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&pwrctrlpriv->rpwm));
2138 _init_workitem(&pwrctrlpriv->cpwm_event, cpwm_event_callback, NULL);
2140 #ifdef CONFIG_LPS_RPWM_TIMER
2141 pwrctrlpriv->brpwmtimeout = _FALSE;
2142 _init_workitem(&pwrctrlpriv->rpwmtimeoutwi, rpwmtimeout_workitem_callback, NULL);
2143 _init_timer(&pwrctrlpriv->pwr_rpwm_timer, padapter->pnetdev, pwr_rpwm_timeout_handler, padapter);
2144 #endif // CONFIG_LPS_RPWM_TIMER
2145 #endif // CONFIG_LPS_LCLK
2147 rtw_init_timer(&pwrctrlpriv->pwr_state_check_timer, padapter, pwr_state_check_handler);
2149 pwrctrlpriv->wowlan_mode = _FALSE;
2150 pwrctrlpriv->wowlan_ap_mode = _FALSE;
2152 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2153 _init_workitem(&pwrctrlpriv->resume_work, resume_workitem_callback, NULL);
2154 pwrctrlpriv->rtw_workqueue = create_singlethread_workqueue("rtw_workqueue");
2155 #endif //CONFIG_RESUME_IN_WORKQUEUE
2157 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
2158 pwrctrlpriv->early_suspend.suspend = NULL;
2159 rtw_register_early_suspend(pwrctrlpriv);
2160 #endif //CONFIG_HAS_EARLYSUSPEND || CONFIG_ANDROID_POWER
2162 #ifdef CONFIG_PNO_SUPPORT
2163 pwrctrlpriv->pnlo_info = NULL;
2164 pwrctrlpriv->pscan_info = NULL;
2165 pwrctrlpriv->pno_ssid_list = NULL;
2173 void rtw_free_pwrctrl_priv(PADAPTER adapter)
2175 struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(adapter);
2177 #if defined(CONFIG_CONCURRENT_MODE)
2178 if (adapter->adapter_type != PRIMARY_ADAPTER)
2184 //_rtw_memset((unsigned char *)pwrctrlpriv, 0, sizeof(struct pwrctrl_priv));
2187 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2188 if (pwrctrlpriv->rtw_workqueue) {
2189 flush_workqueue(pwrctrlpriv->rtw_workqueue);
2190 destroy_workqueue(pwrctrlpriv->rtw_workqueue);
2194 #ifdef CONFIG_PNO_SUPPORT
2195 if (pwrctrlpriv->pnlo_info != NULL)
2196 printk("****** pnlo_info memory leak********\n");
2198 if (pwrctrlpriv->pscan_info != NULL)
2199 printk("****** pscan_info memory leak********\n");
2201 if (pwrctrlpriv->pno_ssid_list != NULL)
2202 printk("****** pno_ssid_list memory leak********\n");
2205 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
2206 rtw_unregister_early_suspend(pwrctrlpriv);
2207 #endif //CONFIG_HAS_EARLYSUSPEND || CONFIG_ANDROID_POWER
2209 _free_pwrlock(&pwrctrlpriv->lock);
2210 _free_pwrlock(&pwrctrlpriv->check_32k_lock);
2215 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2216 extern int rtw_resume_process(_adapter *padapter);
2218 static void resume_workitem_callback(struct work_struct *work)
2220 struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, resume_work);
2221 struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2222 _adapter *adapter = dvobj->if1;
2224 DBG_871X("%s\n",__FUNCTION__);
2226 rtw_resume_process(adapter);
2228 rtw_resume_unlock_suspend();
2231 void rtw_resume_in_workqueue(struct pwrctrl_priv *pwrpriv)
2233 // accquire system's suspend lock preventing from falliing asleep while resume in workqueue
2234 //rtw_lock_suspend();
2235 rtw_resume_lock_suspend();
2238 queue_work(pwrpriv->rtw_workqueue, &pwrpriv->resume_work);
2240 _set_workitem(&pwrpriv->resume_work);
2243 #endif //CONFIG_RESUME_IN_WORKQUEUE
2245 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
2246 inline bool rtw_is_earlysuspend_registered(struct pwrctrl_priv *pwrpriv)
2248 return (pwrpriv->early_suspend.suspend) ? _TRUE : _FALSE;
2251 inline bool rtw_is_do_late_resume(struct pwrctrl_priv *pwrpriv)
2253 return (pwrpriv->do_late_resume) ? _TRUE : _FALSE;
2256 inline void rtw_set_do_late_resume(struct pwrctrl_priv *pwrpriv, bool enable)
2258 pwrpriv->do_late_resume = enable;
2262 #ifdef CONFIG_HAS_EARLYSUSPEND
2263 extern int rtw_resume_process(_adapter *padapter);
2264 static void rtw_early_suspend(struct early_suspend *h)
2266 struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2267 DBG_871X("%s\n",__FUNCTION__);
2269 rtw_set_do_late_resume(pwrpriv, _FALSE);
2272 static void rtw_late_resume(struct early_suspend *h)
2274 struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2275 struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2276 _adapter *adapter = dvobj->if1;
2278 DBG_871X("%s\n",__FUNCTION__);
2280 if(pwrpriv->do_late_resume) {
2281 rtw_set_do_late_resume(pwrpriv, _FALSE);
2282 rtw_resume_process(adapter);
2286 void rtw_register_early_suspend(struct pwrctrl_priv *pwrpriv)
2288 DBG_871X("%s\n", __FUNCTION__);
2290 //jeff: set the early suspend level before blank screen, so we wll do late resume after scree is lit
2291 pwrpriv->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
2292 pwrpriv->early_suspend.suspend = rtw_early_suspend;
2293 pwrpriv->early_suspend.resume = rtw_late_resume;
2294 register_early_suspend(&pwrpriv->early_suspend);
2299 void rtw_unregister_early_suspend(struct pwrctrl_priv *pwrpriv)
2301 DBG_871X("%s\n", __FUNCTION__);
2303 rtw_set_do_late_resume(pwrpriv, _FALSE);
2305 if (pwrpriv->early_suspend.suspend)
2306 unregister_early_suspend(&pwrpriv->early_suspend);
2308 pwrpriv->early_suspend.suspend = NULL;
2309 pwrpriv->early_suspend.resume = NULL;
2311 #endif //CONFIG_HAS_EARLYSUSPEND
2313 #ifdef CONFIG_ANDROID_POWER
2314 #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2315 extern int rtw_resume_process(PADAPTER padapter);
2317 static void rtw_early_suspend(android_early_suspend_t *h)
2319 struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2320 DBG_871X("%s\n",__FUNCTION__);
2322 rtw_set_do_late_resume(pwrpriv, _FALSE);
2325 static void rtw_late_resume(android_early_suspend_t *h)
2327 struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2328 struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2329 _adapter *adapter = dvobj->if1;
2331 DBG_871X("%s\n",__FUNCTION__);
2332 if(pwrpriv->do_late_resume) {
2333 #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2334 rtw_set_do_late_resume(pwrpriv, _FALSE);
2335 rtw_resume_process(adapter);
2340 void rtw_register_early_suspend(struct pwrctrl_priv *pwrpriv)
2342 DBG_871X("%s\n", __FUNCTION__);
2344 //jeff: set the early suspend level before blank screen, so we wll do late resume after scree is lit
2345 pwrpriv->early_suspend.level = ANDROID_EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
2346 pwrpriv->early_suspend.suspend = rtw_early_suspend;
2347 pwrpriv->early_suspend.resume = rtw_late_resume;
2348 android_register_early_suspend(&pwrpriv->early_suspend);
2351 void rtw_unregister_early_suspend(struct pwrctrl_priv *pwrpriv)
2353 DBG_871X("%s\n", __FUNCTION__);
2355 rtw_set_do_late_resume(pwrpriv, _FALSE);
2357 if (pwrpriv->early_suspend.suspend)
2358 android_unregister_early_suspend(&pwrpriv->early_suspend);
2360 pwrpriv->early_suspend.suspend = NULL;
2361 pwrpriv->early_suspend.resume = NULL;
2363 #endif //CONFIG_ANDROID_POWER
2365 u8 rtw_interface_ps_func(_adapter *padapter,HAL_INTF_PS_FUNC efunc_id,u8* val)
2368 rtw_hal_intf_ps_func(padapter,efunc_id,val);
2374 inline void rtw_set_ips_deny(_adapter *padapter, u32 ms)
2376 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
2377 pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ms);
2381 * rtw_pwr_wakeup - Wake the NIC up from: 1)IPS. 2)USB autosuspend
2382 * @adapter: pointer to _adapter structure
2383 * @ips_deffer_ms: the ms wiil prevent from falling into IPS after wakeup
2384 * Return _SUCCESS or _FAIL
2387 int _rtw_pwr_wakeup(_adapter *padapter, u32 ips_deffer_ms, const char *caller)
2389 struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
2390 struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
2391 struct mlme_priv *pmlmepriv;
2394 u32 start = rtw_get_current_time();
2397 LeaveAllPowerSaveMode(padapter);
2399 /* IPS still bound with primary adapter */
2400 padapter = GET_PRIMARY_ADAPTER(padapter);
2401 pmlmepriv = &padapter->mlmepriv;
2403 if (pwrpriv->ips_deny_time < rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms))
2404 pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms);
2407 if (pwrpriv->ps_processing) {
2408 DBG_871X("%s wait ps_processing...\n", __func__);
2409 while (pwrpriv->ps_processing && rtw_get_passing_time_ms(start) <= 3000)
2411 if (pwrpriv->ps_processing)
2412 DBG_871X("%s wait ps_processing timeout\n", __func__);
2414 DBG_871X("%s wait ps_processing done\n", __func__);
2417 #ifdef DBG_CONFIG_ERROR_DETECT
2418 if (rtw_hal_sreset_inprogress(padapter)) {
2419 DBG_871X("%s wait sreset_inprogress...\n", __func__);
2420 while (rtw_hal_sreset_inprogress(padapter) && rtw_get_passing_time_ms(start) <= 4000)
2422 if (rtw_hal_sreset_inprogress(padapter))
2423 DBG_871X("%s wait sreset_inprogress timeout\n", __func__);
2425 DBG_871X("%s wait sreset_inprogress done\n", __func__);
2429 if (pwrpriv->bInternalAutoSuspend == _FALSE && pwrpriv->bInSuspend) {
2430 DBG_871X("%s wait bInSuspend...\n", __func__);
2431 while (pwrpriv->bInSuspend
2432 && ((rtw_get_passing_time_ms(start) <= 3000 && !rtw_is_do_late_resume(pwrpriv))
2433 || (rtw_get_passing_time_ms(start) <= 500 && rtw_is_do_late_resume(pwrpriv)))
2437 if (pwrpriv->bInSuspend)
2438 DBG_871X("%s wait bInSuspend timeout\n", __func__);
2440 DBG_871X("%s wait bInSuspend done\n", __func__);
2443 //System suspend is not allowed to wakeup
2444 if((pwrpriv->bInternalAutoSuspend == _FALSE) && (_TRUE == pwrpriv->bInSuspend )){
2450 if((pwrpriv->bInternalAutoSuspend == _TRUE) && (padapter->net_closed == _TRUE)) {
2455 //I think this should be check in IPS, LPS, autosuspend functions...
2456 if (check_fwstate(pmlmepriv, _FW_LINKED) == _TRUE)
2458 #if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
2459 if(_TRUE==pwrpriv->bInternalAutoSuspend){
2460 if(0==pwrpriv->autopm_cnt){
2461 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,33))
2462 if (usb_autopm_get_interface(adapter_to_dvobj(padapter)->pusbintf) < 0)
2464 DBG_871X( "can't get autopm: \n");
2466 #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,20))
2467 usb_autopm_disable(adapter_to_dvobj(padapter)->pusbintf);
2469 usb_autoresume_device(adapter_to_dvobj(padapter)->pusbdev, 1);
2471 pwrpriv->autopm_cnt++;
2473 #endif //#if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
2476 #if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
2478 #endif //#if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
2481 if(rf_off == pwrpriv->rf_pwrstate )
2483 #ifdef CONFIG_USB_HCI
2484 #ifdef CONFIG_AUTOSUSPEND
2485 if(pwrpriv->brfoffbyhw==_TRUE)
2487 DBG_8192C("hw still in rf_off state ...........\n");
2491 else if(padapter->registrypriv.usbss_enable)
2493 DBG_8192C("%s call autoresume_enter....\n",__FUNCTION__);
2494 if(_FAIL == autoresume_enter(padapter))
2496 DBG_8192C("======> autoresume fail.............\n");
2506 DBG_8192C("%s call ips_leave....\n",__FUNCTION__);
2507 if(_FAIL == ips_leave(padapter))
2509 DBG_8192C("======> ips_leave fail.............\n");
2517 //TODO: the following checking need to be merged...
2518 if(padapter->bDriverStopped
2520 || !padapter->hw_init_completed
2522 DBG_8192C("%s: bDriverStopped=%d, bup=%d, hw_init_completed=%u\n"
2524 , padapter->bDriverStopped
2526 , padapter->hw_init_completed);
2532 if (pwrpriv->ips_deny_time < rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms))
2533 pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms);
2538 int rtw_pm_set_lps(_adapter *padapter, u8 mode)
2541 struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2543 if ( mode < PS_MODE_NUM )
2545 if(pwrctrlpriv->power_mgnt !=mode)
2547 if(PS_MODE_ACTIVE == mode)
2549 LeaveAllPowerSaveMode(padapter);
2553 pwrctrlpriv->LpsIdleCount = 2;
2555 pwrctrlpriv->power_mgnt = mode;
2556 pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt)?_TRUE:_FALSE;
2567 int rtw_pm_set_ips(_adapter *padapter, u8 mode)
2569 struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2571 if( mode == IPS_NORMAL || mode == IPS_LEVEL_2 ) {
2572 rtw_ips_mode_req(pwrctrlpriv, mode);
2573 DBG_871X("%s %s\n", __FUNCTION__, mode == IPS_NORMAL?"IPS_NORMAL":"IPS_LEVEL_2");
2576 else if(mode ==IPS_NONE){
2577 rtw_ips_mode_req(pwrctrlpriv, mode);
2578 DBG_871X("%s %s\n", __FUNCTION__, "IPS_NONE");
2579 if((padapter->bSurpriseRemoved ==0)&&(_FAIL == rtw_pwr_wakeup(padapter)) )
2590 * This function will request pwrctrl LOCK!
2592 void rtw_ps_deny(PADAPTER padapter, PS_DENY_REASON reason)
2594 struct pwrctrl_priv *pwrpriv;
2598 // DBG_871X("+" FUNC_ADPT_FMT ": Request PS deny for %d (0x%08X)\n",
2599 // FUNC_ADPT_ARG(padapter), reason, BIT(reason));
2601 pwrpriv = adapter_to_pwrctl(padapter);
2603 _enter_pwrlock(&pwrpriv->lock);
2604 if (pwrpriv->ps_deny & BIT(reason))
2606 DBG_871X(FUNC_ADPT_FMT ": [WARNING] Reason %d had been set before!!\n",
2607 FUNC_ADPT_ARG(padapter), reason);
2609 pwrpriv->ps_deny |= BIT(reason);
2610 _exit_pwrlock(&pwrpriv->lock);
2612 // DBG_871X("-" FUNC_ADPT_FMT ": Now PS deny for 0x%08X\n",
2613 // FUNC_ADPT_ARG(padapter), pwrpriv->ps_deny);
2618 * This function will request pwrctrl LOCK!
2620 void rtw_ps_deny_cancel(PADAPTER padapter, PS_DENY_REASON reason)
2622 struct pwrctrl_priv *pwrpriv;
2625 // DBG_871X("+" FUNC_ADPT_FMT ": Cancel PS deny for %d(0x%08X)\n",
2626 // FUNC_ADPT_ARG(padapter), reason, BIT(reason));
2628 pwrpriv = adapter_to_pwrctl(padapter);
2630 _enter_pwrlock(&pwrpriv->lock);
2631 if ((pwrpriv->ps_deny & BIT(reason)) == 0)
2633 DBG_871X(FUNC_ADPT_FMT ": [ERROR] Reason %d had been canceled before!!\n",
2634 FUNC_ADPT_ARG(padapter), reason);
2636 pwrpriv->ps_deny &= ~BIT(reason);
2637 _exit_pwrlock(&pwrpriv->lock);
2639 // DBG_871X("-" FUNC_ADPT_FMT ": Now PS deny for 0x%08X\n",
2640 // FUNC_ADPT_ARG(padapter), pwrpriv->ps_deny);
2645 * Before calling this function pwrctrl lock should be occupied already,
2646 * otherwise it may return incorrect value.
2648 u32 rtw_ps_deny_get(PADAPTER padapter)
2653 deny = adapter_to_pwrctl(padapter)->ps_deny;