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>
24 #include <hal_com_h2c.h>
26 int rtw_fw_ps_state(PADAPTER padapter)
28 struct dvobj_priv *psdpriv = padapter->dvobj;
29 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
30 int ret = _FAIL, dont_care = 0;
33 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
34 struct registry_priv *registry_par = &padapter->registrypriv;
36 if (registry_par->check_fw_ps != 1)
39 _enter_pwrlock(&pwrpriv->check_32k_lock);
41 if (RTW_CANNOT_RUN(padapter)) {
42 RTW_INFO("%s: bSurpriseRemoved=%s , hw_init_completed=%d, bDriverStopped=%s\n", __func__
43 , rtw_is_surprise_removed(padapter) ? "True" : "False"
44 , rtw_get_hw_init_completed(padapter)
45 , rtw_is_drv_stopped(padapter) ? "True" : "False");
46 goto exit_fw_ps_state;
48 rtw_hal_set_hwreg(padapter, HW_VAR_SET_REQ_FW_PS, (u8 *)&dont_care);
50 /* 4. if 0x88[7]=1, driver set cmd to leave LPS/IPS. */
51 /* Else, hw will keep in active mode. */
53 /* 0x88[7] = 32kpermission, */
54 /* 0x88[6:0] = current_ps_state */
55 /* 0x89[7:0] = last_rpwm */
57 rtw_hal_get_hwreg(padapter, HW_VAR_FW_PS_STATE, (u8 *)&fw_ps_state);
59 if ((fw_ps_state & 0x80) == 0)
62 pdbgpriv->dbg_poll_fail_cnt++;
63 RTW_INFO("%s: fw_ps_state=%04x\n", __FUNCTION__, fw_ps_state);
69 _exit_pwrlock(&pwrpriv->check_32k_lock);
74 void _ips_enter(_adapter *padapter)
76 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
78 pwrpriv->bips_processing = _TRUE;
80 /* syn ips_mode with request */
81 pwrpriv->ips_mode = pwrpriv->ips_mode_req;
83 pwrpriv->ips_enter_cnts++;
84 RTW_INFO("==>ips_enter cnts:%d\n", pwrpriv->ips_enter_cnts);
86 if (rf_off == pwrpriv->change_rfpwrstate) {
87 pwrpriv->bpower_saving = _TRUE;
88 RTW_PRINT("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)) {
120 pwrpriv->bips_processing = _TRUE;
121 pwrpriv->change_rfpwrstate = rf_on;
122 pwrpriv->ips_leave_cnts++;
123 RTW_INFO("==>ips_leave cnts:%d\n", pwrpriv->ips_leave_cnts);
125 result = rtw_ips_pwr_up(padapter);
126 if (result == _SUCCESS)
127 pwrpriv->rf_pwrstate = rf_on;
128 RTW_PRINT("nolinked power save leave\n");
130 RTW_INFO("==> ips_leave.....LED(0x%08x)...\n", rtw_read32(padapter, 0x4c));
131 pwrpriv->bips_processing = _FALSE;
133 pwrpriv->bkeepfwalive = _FALSE;
134 pwrpriv->bpower_saving = _FALSE;
140 int ips_leave(_adapter *padapter)
142 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
143 struct dvobj_priv *psdpriv = padapter->dvobj;
144 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
147 if (!is_primary_adapter(padapter))
150 _enter_pwrlock(&pwrpriv->lock);
151 ret = _ips_leave(padapter);
152 #ifdef DBG_CHECK_FW_PS_STATE
153 if (rtw_fw_ps_state(padapter) == _FAIL) {
154 RTW_INFO("ips leave doesn't leave 32k\n");
155 pdbgpriv->dbg_leave_ips_fail_cnt++;
157 #endif /* DBG_CHECK_FW_PS_STATE */
158 _exit_pwrlock(&pwrpriv->lock);
161 ODM_DMReset(&GET_HAL_DATA(padapter)->odmpriv);
163 #ifdef CONFIG_BT_COEXIST
165 rtw_btcoex_IpsNotify(padapter, IPS_NONE);
166 #endif /* CONFIG_BT_COEXIST */
170 #endif /* CONFIG_IPS */
172 #ifdef CONFIG_AUTOSUSPEND
173 extern void autosuspend_enter(_adapter *padapter);
174 extern int autoresume_enter(_adapter *padapter);
177 #ifdef SUPPORT_HW_RFOFF_DETECTED
178 int rtw_hw_suspend(_adapter *padapter);
179 int rtw_hw_resume(_adapter *padapter);
182 bool rtw_pwr_unassociated_idle(_adapter *adapter)
186 struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
187 struct xmit_priv *pxmit_priv = &adapter->xmitpriv;
188 struct mlme_priv *pmlmepriv;
190 struct wifidirect_info *pwdinfo;
191 #ifdef CONFIG_IOCTL_CFG80211
192 struct cfg80211_wifidirect_info *pcfg80211_wdinfo;
198 if (adapter_to_pwrctl(adapter)->bpower_saving == _TRUE) {
199 /* RTW_INFO("%s: already in LPS or IPS mode\n", __func__); */
203 if (adapter_to_pwrctl(adapter)->ips_deny_time >= rtw_get_current_time()) {
204 /* RTW_INFO("%s ips_deny_time\n", __func__); */
208 for (i = 0; i < dvobj->iface_nums; i++) {
209 iface = dvobj->padapters[i];
210 if ((iface) && rtw_is_adapter_up(iface)) {
211 pmlmepriv = &(iface->mlmepriv);
213 pwdinfo = &(iface->wdinfo);
214 #ifdef CONFIG_IOCTL_CFG80211
215 pcfg80211_wdinfo = &iface->cfg80211_wdinfo;
218 if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE | WIFI_SITE_MONITOR)
219 || check_fwstate(pmlmepriv, WIFI_UNDER_LINKING | WIFI_UNDER_WPS)
220 || check_fwstate(pmlmepriv, WIFI_AP_STATE)
221 || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE | WIFI_ADHOC_STATE)
222 #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211)
223 || rtw_cfg80211_get_is_roch(iface) == _TRUE
224 #elif defined(CONFIG_P2P)
225 || rtw_p2p_chk_state(pwdinfo, P2P_STATE_IDLE)
226 || rtw_p2p_chk_state(pwdinfo, P2P_STATE_LISTEN)
228 #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211)
229 || rtw_get_passing_time_ms(pcfg80211_wdinfo->last_ro_ch_time) < 3000
238 if (adapter->registrypriv.mp_mode == 1)
242 #ifdef CONFIG_INTEL_PROXIM
243 if (adapter->proximity.proxim_on == _TRUE)
247 if (pxmit_priv->free_xmitbuf_cnt != NR_XMITBUFF ||
248 pxmit_priv->free_xmit_extbuf_cnt != NR_XMIT_EXTBUFF) {
249 RTW_PRINT("There are some pkts to transmit\n");
250 RTW_PRINT("free_xmitbuf_cnt: %d, free_xmit_extbuf_cnt: %d\n",
251 pxmit_priv->free_xmitbuf_cnt, pxmit_priv->free_xmit_extbuf_cnt);
264 * rtw_ps_processor() doesn't handle LPS.
266 void rtw_ps_processor(_adapter *padapter)
269 struct wifidirect_info *pwdinfo = &(padapter->wdinfo);
270 #endif /* CONFIG_P2P */
271 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
272 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
273 struct dvobj_priv *psdpriv = padapter->dvobj;
274 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
275 #ifdef SUPPORT_HW_RFOFF_DETECTED
276 rt_rf_power_state rfpwrstate;
277 #endif /* SUPPORT_HW_RFOFF_DETECTED */
280 _enter_pwrlock(&adapter_to_pwrctl(padapter)->lock);
281 ps_deny = rtw_ps_deny_get(padapter);
282 _exit_pwrlock(&adapter_to_pwrctl(padapter)->lock);
284 RTW_INFO(FUNC_ADPT_FMT ": ps_deny=0x%08X, skip power save!\n",
285 FUNC_ADPT_ARG(padapter), ps_deny);
289 if (pwrpriv->bInSuspend == _TRUE) { /* system suspend or autosuspend */
290 pdbgpriv->dbg_ps_insuspend_cnt++;
291 RTW_INFO("%s, pwrpriv->bInSuspend == _TRUE ignore this process\n", __FUNCTION__);
295 pwrpriv->ps_processing = _TRUE;
297 #ifdef SUPPORT_HW_RFOFF_DETECTED
298 if (pwrpriv->bips_processing == _TRUE)
301 /* RTW_INFO("==> fw report state(0x%x)\n",rtw_read8(padapter,0x1ca)); */
302 if (pwrpriv->bHWPwrPindetect) {
303 #ifdef CONFIG_AUTOSUSPEND
304 if (padapter->registrypriv.usbss_enable) {
305 if (pwrpriv->rf_pwrstate == rf_on) {
306 if (padapter->net_closed == _TRUE)
307 pwrpriv->ps_flag = _TRUE;
309 rfpwrstate = RfOnOffDetect(padapter);
310 RTW_INFO("@@@@- #1 %s==> rfstate:%s\n", __FUNCTION__, (rfpwrstate == rf_on) ? "rf_on" : "rf_off");
311 if (rfpwrstate != pwrpriv->rf_pwrstate) {
312 if (rfpwrstate == rf_off) {
313 pwrpriv->change_rfpwrstate = rf_off;
315 pwrpriv->bkeepfwalive = _TRUE;
316 pwrpriv->brfoffbyhw = _TRUE;
318 autosuspend_enter(padapter);
323 #endif /* CONFIG_AUTOSUSPEND */
325 rfpwrstate = RfOnOffDetect(padapter);
326 RTW_INFO("@@@@- #2 %s==> rfstate:%s\n", __FUNCTION__, (rfpwrstate == rf_on) ? "rf_on" : "rf_off");
328 if (rfpwrstate != pwrpriv->rf_pwrstate) {
329 if (rfpwrstate == rf_off) {
330 pwrpriv->change_rfpwrstate = rf_off;
331 pwrpriv->brfoffbyhw = _TRUE;
332 rtw_hw_suspend(padapter);
334 pwrpriv->change_rfpwrstate = rf_on;
335 rtw_hw_resume(padapter);
337 RTW_INFO("current rf_pwrstate(%s)\n", (pwrpriv->rf_pwrstate == rf_off) ? "rf_off" : "rf_on");
340 pwrpriv->pwr_state_check_cnts++;
342 #endif /* SUPPORT_HW_RFOFF_DETECTED */
344 if (pwrpriv->ips_mode_req == IPS_NONE)
347 if (rtw_pwr_unassociated_idle(padapter) == _FALSE)
350 if ((pwrpriv->rf_pwrstate == rf_on) && ((pwrpriv->pwr_state_check_cnts % 4) == 0)) {
351 RTW_INFO("==>%s .fw_state(%x)\n", __FUNCTION__, get_fwstate(pmlmepriv));
352 #if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
354 pwrpriv->change_rfpwrstate = rf_off;
356 #ifdef CONFIG_AUTOSUSPEND
357 if (padapter->registrypriv.usbss_enable) {
358 if (pwrpriv->bHWPwrPindetect)
359 pwrpriv->bkeepfwalive = _TRUE;
361 if (padapter->net_closed == _TRUE)
362 pwrpriv->ps_flag = _TRUE;
364 #if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
365 if (_TRUE == pwrpriv->bInternalAutoSuspend)
366 RTW_INFO("<==%s .pwrpriv->bInternalAutoSuspend)(%x)\n", __FUNCTION__, pwrpriv->bInternalAutoSuspend);
368 pwrpriv->change_rfpwrstate = rf_off;
369 RTW_INFO("<==%s .pwrpriv->bInternalAutoSuspend)(%x) call autosuspend_enter\n", __FUNCTION__, pwrpriv->bInternalAutoSuspend);
370 autosuspend_enter(padapter);
373 autosuspend_enter(padapter);
374 #endif /* if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND) */
375 } else if (pwrpriv->bHWPwrPindetect) {
377 #endif /* CONFIG_AUTOSUSPEND */
379 #if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
380 pwrpriv->change_rfpwrstate = rf_off;
381 #endif /* defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND) */
389 #ifndef CONFIG_IPS_CHECK_IN_WD
390 rtw_set_pwr_state_check_timer(pwrpriv);
392 pwrpriv->ps_processing = _FALSE;
396 void pwr_state_check_handler(RTW_TIMER_HDL_ARGS);
397 void pwr_state_check_handler(RTW_TIMER_HDL_ARGS)
399 _adapter *padapter = (_adapter *)FunctionContext;
400 rtw_ps_cmd(padapter);
404 void traffic_check_for_leave_lps(PADAPTER padapter, u8 tx, u32 tx_packets)
406 #ifdef CONFIG_CHECK_LEAVE_LPS
407 static u32 start_time = 0;
408 static u32 xmit_cnt = 0;
409 u8 bLeaveLPS = _FALSE;
410 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
414 if (tx) { /* from tx */
415 xmit_cnt += tx_packets;
418 start_time = rtw_get_current_time();
420 if (rtw_get_passing_time_ms(start_time) > 2000) { /* 2 sec == watch dog timer */
422 if ((adapter_to_pwrctl(padapter)->bLeisurePs)
423 && (adapter_to_pwrctl(padapter)->pwr_mode != PS_MODE_ACTIVE)
424 #ifdef CONFIG_BT_COEXIST
425 && (rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
428 /* RTW_INFO("leave lps via Tx = %d\n", xmit_cnt); */
433 start_time = rtw_get_current_time();
437 } else { /* from rx path */
438 if (pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod > 4/*2*/) {
439 if ((adapter_to_pwrctl(padapter)->bLeisurePs)
440 && (adapter_to_pwrctl(padapter)->pwr_mode != PS_MODE_ACTIVE)
441 #ifdef CONFIG_BT_COEXIST
442 && (rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
445 /* RTW_INFO("leave lps via Rx = %d\n", pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod); */
452 /* RTW_INFO("leave lps via %s, Tx = %d, Rx = %d\n", tx?"Tx":"Rx", pmlmepriv->LinkDetectInfo.NumTxOkInPeriod,pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod); */
453 /* rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, 1); */
454 rtw_lps_ctrl_wk_cmd(padapter, tx ? LPS_CTRL_TX_TRAFFIC_LEAVE : LPS_CTRL_RX_TRAFFIC_LEAVE, tx ? 0 : 1);
456 #endif /* CONFIG_CHECK_LEAVE_LPS */
461 * This function MUST be called under power lock protect
465 * pslv power state level, only could be PS_STATE_S0 ~ PS_STATE_S4
468 void rtw_set_rpwm(PADAPTER padapter, u8 pslv)
471 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
472 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
474 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
475 struct dvobj_priv *psdpriv = padapter->dvobj;
476 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
478 pslv = PS_STATE(pslv);
480 #ifdef CONFIG_LPS_RPWM_TIMER
481 if (pwrpriv->brpwmtimeout == _TRUE)
482 RTW_INFO("%s: RPWM timeout, force to set RPWM(0x%02X) again!\n", __FUNCTION__, pslv);
484 #endif /* CONFIG_LPS_RPWM_TIMER */
486 if ((pwrpriv->rpwm == pslv)
487 #ifdef CONFIG_LPS_LCLK
488 || ((pwrpriv->rpwm >= PS_STATE_S2) && (pslv >= PS_STATE_S2))
495 if (rtw_is_surprise_removed(padapter) ||
496 (!rtw_is_hw_init_completed(padapter))) {
498 pwrpriv->cpwm = PS_STATE_S4;
503 if (rtw_is_drv_stopped(padapter)) {
505 if (pslv < PS_STATE_S2) {
510 rpwm = pslv | pwrpriv->tog;
511 #ifdef CONFIG_LPS_LCLK
512 /* only when from PS_STATE S0/S1 to S2 and higher needs ACK */
513 if ((pwrpriv->cpwm < PS_STATE_S2) && (pslv >= PS_STATE_S2))
517 pwrpriv->rpwm = pslv;
519 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
522 rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_orig);
525 #if defined(CONFIG_LPS_RPWM_TIMER) && !defined(CONFIG_DETECT_CPWM_BY_POLLING)
527 _set_timer(&pwrpriv->pwr_rpwm_timer, LPS_RPWM_WAIT_MS);
528 #endif /* CONFIG_LPS_RPWM_TIMER & !CONFIG_DETECT_CPWM_BY_POLLING */
529 rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&rpwm));
531 pwrpriv->tog += 0x80;
533 #ifdef CONFIG_LPS_LCLK
534 /* No LPS 32K, No Ack */
536 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
541 start_time = rtw_get_current_time();
548 rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_now);
549 if ((cpwm_orig ^ cpwm_now) & 0x80) {
550 pwrpriv->cpwm = PS_STATE_S4;
551 pwrpriv->cpwm_tog = cpwm_now & PS_TOGGLE;
552 #ifdef DBG_CHECK_FW_PS_STATE
553 RTW_INFO("%s: polling cpwm OK! poll_cnt=%d, cpwm_orig=%02x, cpwm_now=%02x , 0x100=0x%x\n"
554 , __FUNCTION__, poll_cnt, cpwm_orig, cpwm_now, rtw_read8(padapter, REG_CR));
555 if (rtw_fw_ps_state(padapter) == _FAIL) {
556 RTW_INFO("leave 32k but fw state in 32k\n");
557 pdbgpriv->dbg_rpwm_toogle_cnt++;
559 #endif /* DBG_CHECK_FW_PS_STATE */
563 if (rtw_get_passing_time_ms(start_time) > LPS_RPWM_WAIT_MS) {
564 RTW_INFO("%s: polling cpwm timeout! poll_cnt=%d, cpwm_orig=%02x, cpwm_now=%02x\n", __FUNCTION__, poll_cnt, cpwm_orig, cpwm_now);
565 #ifdef DBG_CHECK_FW_PS_STATE
566 if (rtw_fw_ps_state(padapter) == _FAIL) {
567 RTW_INFO("rpwm timeout and fw ps state in 32k\n");
568 pdbgpriv->dbg_rpwm_timeout_fail_cnt++;
570 #endif /* DBG_CHECK_FW_PS_STATE */
571 #ifdef CONFIG_LPS_RPWM_TIMER
572 _set_timer(&pwrpriv->pwr_rpwm_timer, 1);
573 #endif /* CONFIG_LPS_RPWM_TIMER */
577 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
579 #endif /* CONFIG_LPS_LCLK */
581 pwrpriv->cpwm = pslv;
586 u8 PS_RDY_CHECK(_adapter *padapter)
588 u32 curr_time, delta_time;
589 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
590 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
592 struct wifidirect_info *pwdinfo = &(padapter->wdinfo);
593 #ifdef CONFIG_IOCTL_CFG80211
594 struct cfg80211_wifidirect_info *pcfg80211_wdinfo = &padapter->cfg80211_wdinfo;
595 #endif /* CONFIG_IOCTL_CFG80211 */
596 #endif /* CONFIG_P2P */
598 #if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN)
599 if (_TRUE == pwrpriv->bInSuspend && pwrpriv->wowlan_mode)
601 else if (_TRUE == pwrpriv->bInSuspend && pwrpriv->wowlan_ap_mode)
603 else if (_TRUE == pwrpriv->bInSuspend)
606 if (_TRUE == pwrpriv->bInSuspend)
610 curr_time = rtw_get_current_time();
612 delta_time = curr_time - pwrpriv->DelayLPSLastTimeStamp;
614 if (delta_time < LPS_DELAY_TIME)
617 if (check_fwstate(pmlmepriv, WIFI_SITE_MONITOR)
618 || check_fwstate(pmlmepriv, WIFI_UNDER_LINKING | WIFI_UNDER_WPS)
619 || check_fwstate(pmlmepriv, WIFI_AP_STATE)
620 || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE | WIFI_ADHOC_STATE)
621 #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211)
622 || rtw_cfg80211_get_is_roch(padapter) == _TRUE
624 || rtw_is_scan_deny(padapter)
626 /* TDLS link is established. */
627 || (padapter->tdlsinfo.link_established == _TRUE)
628 #endif /* CONFIG_TDLS */
632 if ((padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) && (padapter->securitypriv.binstallGrpkey == _FALSE)) {
633 RTW_INFO("Group handshake still in progress !!!\n");
637 #ifdef CONFIG_IOCTL_CFG80211
638 if (!rtw_cfg80211_pwr_mgmt(padapter))
645 #if defined(CONFIG_FWLPS_IN_IPS)
646 void rtw_set_fw_in_ips_mode(PADAPTER padapter, u8 enable)
648 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
652 u8 cpwm_orig = 0, cpwm_now = 0;
653 u8 parm[H2C_INACTIVE_PS_LEN] = {0};
655 if (padapter->netif_up == _FALSE) {
656 RTW_INFO("%s: ERROR, netif is down\n", __func__);
660 /* u8 cmd_param; */ /* BIT0:enable, BIT1:NoConnect32k */
662 #ifdef CONFIG_BT_COEXIST
663 rtw_btcoex_IpsNotify(padapter, pwrpriv->ips_mode_req);
666 RTW_INFO("%s: issue H2C to FW when entering IPS\n", __func__);
668 parm[0] = 0x1;/* suggest by Isaac.Hsu*/
669 #ifdef CONFIG_PNO_SUPPORT
670 if (pwrpriv->pno_inited) {
671 parm[1] = pwrpriv->pnlo_info->fast_scan_iterations;
672 parm[2] = pwrpriv->pnlo_info->slow_scan_period;
676 rtw_hal_fill_h2c_cmd(padapter, /* H2C_FWLPS_IN_IPS_, */
678 H2C_INACTIVE_PS_LEN, parm);
679 /* poll 0x1cc to make sure H2C command already finished by FW; MAC_0x1cc=0 means H2C done by FW. */
681 val8 = rtw_read8(padapter, REG_HMETFR);
683 RTW_INFO("%s polling REG_HMETFR=0x%x, cnt=%d\n",
684 __func__, val8, cnt);
686 } while (cnt < 100 && (val8 != 0));
688 #ifdef CONFIG_LPS_LCLK
689 /* H2C done, enter 32k */
691 /* ser rpwm to enter 32k */
692 val8 = rtw_read8(padapter, SDIO_LOCAL_BASE | SDIO_REG_HRPWM1);
693 RTW_INFO("%s: read rpwm=%02x\n", __FUNCTION__, val8);
696 rtw_write8(padapter, SDIO_LOCAL_BASE | SDIO_REG_HRPWM1, val8);
697 RTW_INFO("%s: write rpwm=%02x\n", __FUNCTION__, val8);
698 adapter_to_pwrctl(padapter)->tog = (val8 + 0x80) & 0x80;
700 if (parm[1] == 0 || parm[2] == 0) {
702 val8 = rtw_read8(padapter, REG_CR);
704 RTW_INFO("%s polling 0x100=0x%x, cnt=%d\n",
705 __func__, val8, cnt);
706 RTW_INFO("%s 0x08:%02x, 0x03:%02x\n",
708 rtw_read8(padapter, 0x08),
709 rtw_read8(padapter, 0x03));
711 } while (cnt < 20 && (val8 != 0xEA));
717 RTW_INFO("%s: Leaving IPS in FWLPS state\n", __func__);
719 #ifdef CONFIG_LPS_LCLK
720 /* for polling cpwm */
722 rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_orig);
725 val8 = rtw_read8(padapter, SDIO_LOCAL_BASE | SDIO_REG_HRPWM1);
729 rtw_write8(padapter, SDIO_LOCAL_BASE | SDIO_REG_HRPWM1, val8);
730 RTW_INFO("%s: write rpwm=%02x\n", __FUNCTION__, val8);
731 adapter_to_pwrctl(padapter)->tog = (val8 + 0x80) & 0x80;
733 /* do polling cpwm */
734 start_time = rtw_get_current_time();
739 rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_now);
740 if ((cpwm_orig ^ cpwm_now) & 0x80)
743 if (rtw_get_passing_time_ms(start_time) > 100) {
744 RTW_INFO("%s: polling cpwm timeout when leaving IPS in FWLPS state\n", __FUNCTION__);
753 rtw_hal_fill_h2c_cmd(padapter, H2C_INACTIVE_PS_,
754 H2C_INACTIVE_PS_LEN, parm);
755 #ifdef CONFIG_BT_COEXIST
756 rtw_btcoex_IpsNotify(padapter, IPS_NONE);
760 #endif /* CONFIG_PNO_SUPPORT */
762 void rtw_set_ps_mode(PADAPTER padapter, u8 ps_mode, u8 smart_ps, u8 bcn_ant_mode, const char *msg)
764 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
765 struct dvobj_priv *psdpriv = padapter->dvobj;
766 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
768 struct wifidirect_info *pwdinfo = &(padapter->wdinfo);
769 #endif /* CONFIG_P2P */
771 struct sta_priv *pstapriv = &padapter->stapriv;
774 _list *plist, *phead;
775 struct sta_info *ptdls_sta;
776 #endif /* CONFIG_TDLS */
780 if (ps_mode > PM_Card_Disable) {
784 if (pwrpriv->pwr_mode == ps_mode) {
785 if (PS_MODE_ACTIVE == ps_mode)
788 #ifndef CONFIG_BT_COEXIST
789 if ((pwrpriv->smart_ps == smart_ps) &&
790 (pwrpriv->bcn_ant_mode == bcn_ant_mode))
792 #endif /* !CONFIG_BT_COEXIST */
796 if ((PS_MODE_ACTIVE != ps_mode) && (pwrpriv->blpspg_info_up))
797 rtw_hal_set_lps_pg_info(padapter);
800 #ifdef CONFIG_LPS_LCLK
801 _enter_pwrlock(&pwrpriv->lock);
804 /* if(pwrpriv->pwr_mode == PS_MODE_ACTIVE) */
805 if (ps_mode == PS_MODE_ACTIVE) {
807 #ifdef CONFIG_BT_COEXIST
808 && (((rtw_btcoex_IsBtControlLps(padapter) == _FALSE)
810 && (pwdinfo->opp_ps == 0)
811 #endif /* CONFIG_P2P_PS */
813 || ((rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
814 && (rtw_btcoex_IsLpsOn(padapter) == _FALSE))
816 #else /* !CONFIG_BT_COEXIST */
818 && (pwdinfo->opp_ps == 0)
819 #endif /* CONFIG_P2P_PS */
820 #endif /* !CONFIG_BT_COEXIST */
822 RTW_INFO(FUNC_ADPT_FMT" Leave 802.11 power save - %s\n",
823 FUNC_ADPT_ARG(padapter), msg);
825 if (pwrpriv->lps_leave_cnts < UINT_MAX)
826 pwrpriv->lps_leave_cnts++;
828 pwrpriv->lps_leave_cnts = 0;
830 for (i = 0; i < NUM_STA; i++) {
831 phead = &(pstapriv->sta_hash[i]);
832 plist = get_next(phead);
834 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
835 ptdls_sta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
837 if (ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE)
838 issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta->hwaddr, 0, 0, 0);
839 plist = get_next(plist);
842 #endif /* CONFIG_TDLS */
844 pwrpriv->pwr_mode = ps_mode;
845 rtw_set_rpwm(padapter, PS_STATE_S4);
847 #if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) || defined(CONFIG_P2P_WOWLAN)
848 if (pwrpriv->wowlan_mode == _TRUE ||
849 pwrpriv->wowlan_ap_mode == _TRUE ||
850 pwrpriv->wowlan_p2p_mode == _TRUE) {
851 u32 start_time, delay_ms;
854 start_time = rtw_get_current_time();
856 rtw_hal_get_hwreg(padapter, HW_VAR_SYS_CLKR, &val8);
857 if (!(val8 & BIT(4))) { /* 0x08 bit4 =1 --> in 32k, bit4 = 0 --> leave 32k */
858 pwrpriv->cpwm = PS_STATE_S4;
861 if (rtw_get_passing_time_ms(start_time) > delay_ms) {
862 RTW_INFO("%s: Wait for FW 32K leave more than %u ms!!!\n",
863 __FUNCTION__, delay_ms);
864 pdbgpriv->dbg_wow_leave_ps_fail_cnt++;
871 rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
872 #ifdef CONFIG_LPS_POFF
873 rtw_hal_set_hwreg(padapter, HW_VAR_LPS_POFF_SET_MODE,
875 #endif /*CONFIG_LPS_POFF*/
877 pwrpriv->bFwCurrentInPSMode = _FALSE;
879 #ifdef CONFIG_BT_COEXIST
880 rtw_btcoex_LpsNotify(padapter, ps_mode);
881 #endif /* CONFIG_BT_COEXIST */
884 if ((PS_RDY_CHECK(padapter) && check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE))
885 #ifdef CONFIG_BT_COEXIST
886 || ((rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
887 && (rtw_btcoex_IsLpsOn(padapter) == _TRUE))
889 #ifdef CONFIG_P2P_WOWLAN
890 || (_TRUE == pwrpriv->wowlan_p2p_mode)
891 #endif /* CONFIG_P2P_WOWLAN */
895 RTW_INFO(FUNC_ADPT_FMT" Enter 802.11 power save - %s\n",
896 FUNC_ADPT_ARG(padapter), msg);
898 if (pwrpriv->lps_enter_cnts < UINT_MAX)
899 pwrpriv->lps_enter_cnts++;
901 pwrpriv->lps_enter_cnts = 0;
903 for (i = 0; i < NUM_STA; i++) {
904 phead = &(pstapriv->sta_hash[i]);
905 plist = get_next(phead);
907 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
908 ptdls_sta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
910 if (ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE)
911 issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta->hwaddr, 1, 0, 0);
912 plist = get_next(plist);
915 #endif /* CONFIG_TDLS */
917 #ifdef CONFIG_BT_COEXIST
918 rtw_btcoex_LpsNotify(padapter, ps_mode);
919 #endif /* CONFIG_BT_COEXIST */
921 #ifdef CONFIG_LPS_POFF
922 rtw_hal_set_hwreg(padapter, HW_VAR_LPS_POFF_SET_MODE,
924 #endif /*CONFIG_LPS_POFF*/
926 pwrpriv->bFwCurrentInPSMode = _TRUE;
927 pwrpriv->pwr_mode = ps_mode;
928 pwrpriv->smart_ps = smart_ps;
929 pwrpriv->bcn_ant_mode = bcn_ant_mode;
930 rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
933 /* Set CTWindow after LPS */
934 if (pwdinfo->opp_ps == 1)
935 p2p_ps_wk_cmd(padapter, P2P_PS_ENABLE, 0);
936 #endif /* CONFIG_P2P_PS */
939 #ifdef CONFIG_LPS_LCLK
940 if (pwrpriv->alives == 0)
942 #endif /* CONFIG_LPS_LCLK */
944 #ifdef CONFIG_BT_COEXIST
945 if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
946 && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)) {
949 val8 = rtw_btcoex_LpsVal(padapter);
954 #endif /* CONFIG_BT_COEXIST */
956 rtw_set_rpwm(padapter, pslv);
960 #ifdef CONFIG_LPS_LCLK
961 _exit_pwrlock(&pwrpriv->lock);
972 s32 LPS_RF_ON_check(PADAPTER padapter, u32 delay_ms)
979 start_time = rtw_get_current_time();
981 rtw_hal_get_hwreg(padapter, HW_VAR_FWLPS_RF_ON, &bAwake);
985 if (rtw_is_surprise_removed(padapter)) {
987 RTW_INFO("%s: device surprise removed!!\n", __FUNCTION__);
991 if (rtw_get_passing_time_ms(start_time) > delay_ms) {
993 RTW_INFO("%s: Wait for FW LPS leave more than %u ms!!!\n", __FUNCTION__, delay_ms);
1004 * Enter the leisure power save mode.
1006 void LPS_Enter(PADAPTER padapter, const char *msg)
1008 struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1009 struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
1010 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1011 int n_assoc_iface = 0;
1016 /* RTW_INFO("+LeisurePSEnter\n"); */
1017 if (_FALSE == padapter->bFWReady)
1020 #ifdef CONFIG_BT_COEXIST
1021 if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1025 /* Skip lps enter request if number of assocated adapters is not 1 */
1026 for (i = 0; i < dvobj->iface_nums; i++) {
1027 if (check_fwstate(&(dvobj->padapters[i]->mlmepriv), WIFI_ASOC_STATE))
1030 if (n_assoc_iface != 1)
1033 /* Skip lps enter request for adapter not port0 */
1034 if (get_hw_port(padapter) != HW_PORT0)
1037 for (i = 0; i < dvobj->iface_nums; i++) {
1038 if (PS_RDY_CHECK(dvobj->padapters[i]) == _FALSE)
1042 #ifdef CONFIG_P2P_PS
1043 if (padapter->wdinfo.p2p_ps_mode == P2P_PS_NOA) {
1044 return;/* supporting p2p client ps NOA via H2C_8723B_P2P_PS_OFFLOAD */
1046 #endif /* CONFIG_P2P_PS */
1048 if (pwrpriv->bLeisurePs) {
1049 /* Idle for a while if we connect to AP a while ago. */
1050 if (pwrpriv->LpsIdleCount >= 2) { /* 4 Sec */
1051 if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) {
1052 sprintf(buf, "WIFI-%s", msg);
1053 pwrpriv->bpower_saving = _TRUE;
1054 rtw_set_ps_mode(padapter, pwrpriv->power_mgnt, padapter->registrypriv.smart_ps, 0, buf);
1057 pwrpriv->LpsIdleCount++;
1060 /* RTW_INFO("-LeisurePSEnter\n"); */
1066 * Leave the leisure power save mode.
1068 void LPS_Leave(PADAPTER padapter, const char *msg)
1070 #define LPS_LEAVE_TIMEOUT_MS 100
1072 struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1073 struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
1077 struct debug_priv *pdbgpriv = &dvobj->drv_dbg;
1080 /* RTW_INFO("+LeisurePSLeave\n"); */
1082 #ifdef CONFIG_BT_COEXIST
1083 if (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)
1087 if (pwrpriv->bLeisurePs) {
1088 if (pwrpriv->pwr_mode != PS_MODE_ACTIVE) {
1089 sprintf(buf, "WIFI-%s", msg);
1090 rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0, buf);
1092 if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
1093 LPS_RF_ON_check(padapter, LPS_LEAVE_TIMEOUT_MS);
1097 pwrpriv->bpower_saving = _FALSE;
1098 #ifdef DBG_CHECK_FW_PS_STATE
1099 if (rtw_fw_ps_state(padapter) == _FAIL) {
1100 RTW_INFO("leave lps, fw in 32k\n");
1101 pdbgpriv->dbg_leave_lps_fail_cnt++;
1103 #endif /* DBG_CHECK_FW_PS_STATE
1104 * RTW_INFO("-LeisurePSLeave\n"); */
1109 void LeaveAllPowerSaveModeDirect(PADAPTER Adapter)
1111 PADAPTER pri_padapter = GET_PRIMARY_ADAPTER(Adapter);
1112 struct mlme_priv *pmlmepriv = &(Adapter->mlmepriv);
1113 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(Adapter);
1114 struct dvobj_priv *psdpriv = Adapter->dvobj;
1115 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
1116 #ifndef CONFIG_DETECT_CPWM_BY_POLLING
1117 u8 cpwm_orig, cpwm_now;
1119 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1122 RTW_INFO("%s.....\n", __FUNCTION__);
1124 if (rtw_is_surprise_removed(Adapter)) {
1125 RTW_INFO(FUNC_ADPT_FMT ": bSurpriseRemoved=_TRUE Skip!\n", FUNC_ADPT_ARG(Adapter));
1129 if (rtw_mi_check_status(Adapter, MI_LINKED)) { /*connect*/
1131 if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) {
1132 RTW_INFO("%s: Driver Already Leave LPS\n", __FUNCTION__);
1136 #ifdef CONFIG_LPS_LCLK
1137 _enter_pwrlock(&pwrpriv->lock);
1139 #ifndef CONFIG_DETECT_CPWM_BY_POLLING
1141 rtw_hal_get_hwreg(Adapter, HW_VAR_CPWM, &cpwm_orig);
1142 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1143 rtw_set_rpwm(Adapter, PS_STATE_S4);
1145 #ifndef CONFIG_DETECT_CPWM_BY_POLLING
1147 start_time = rtw_get_current_time();
1153 rtw_hal_get_hwreg(Adapter, HW_VAR_CPWM, &cpwm_now);
1154 if ((cpwm_orig ^ cpwm_now) & 0x80) {
1155 pwrpriv->cpwm = PS_STATE_S4;
1156 pwrpriv->cpwm_tog = cpwm_now & PS_TOGGLE;
1157 #ifdef DBG_CHECK_FW_PS_STATE
1158 RTW_INFO("%s: polling cpwm OK! cpwm_orig=%02x, cpwm_now=%02x, 0x100=0x%x\n"
1159 , __FUNCTION__, cpwm_orig, cpwm_now, rtw_read8(Adapter, REG_CR));
1160 if (rtw_fw_ps_state(Adapter) == _FAIL) {
1161 RTW_INFO("%s: leave 32k but fw state in 32k\n", __FUNCTION__);
1162 pdbgpriv->dbg_rpwm_toogle_cnt++;
1164 #endif /* DBG_CHECK_FW_PS_STATE */
1168 if (rtw_get_passing_time_ms(start_time) > LPS_RPWM_WAIT_MS) {
1169 RTW_INFO("%s: polling cpwm timeout! cpwm_orig=%02x, cpwm_now=%02x\n", __FUNCTION__, cpwm_orig, cpwm_now);
1170 #ifdef DBG_CHECK_FW_PS_STATE
1171 if (rtw_fw_ps_state(Adapter) == _FAIL) {
1172 RTW_INFO("rpwm timeout and fw ps state in 32k\n");
1173 pdbgpriv->dbg_rpwm_timeout_fail_cnt++;
1175 #endif /* DBG_CHECK_FW_PS_STATE */
1179 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1181 _exit_pwrlock(&pwrpriv->lock);
1184 #ifdef CONFIG_P2P_PS
1185 p2p_ps_wk_cmd(pri_padapter, P2P_PS_DISABLE, 0);
1186 #endif /* CONFIG_P2P_PS */
1189 rtw_lps_ctrl_wk_cmd(pri_padapter, LPS_CTRL_LEAVE, 0);
1192 if (pwrpriv->rf_pwrstate == rf_off) {
1193 #ifdef CONFIG_AUTOSUSPEND
1194 if (Adapter->registrypriv.usbss_enable) {
1195 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
1196 usb_disable_autosuspend(adapter_to_dvobj(Adapter)->pusbdev);
1197 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22) && LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 34))
1198 adapter_to_dvobj(Adapter)->pusbdev->autosuspend_disabled = Adapter->bDisableAutosuspend;/* autosuspend disabled by the user */
1203 #if defined(CONFIG_FWLPS_IN_IPS) || defined(CONFIG_SWLPS_IN_IPS)
1205 if (_FALSE == ips_leave(pri_padapter))
1206 RTW_INFO("======> ips_leave fail.............\n");
1208 #endif /* CONFIG_SWLPS_IN_IPS || (CONFIG_PLATFORM_SPRD && CONFIG_RTL8188E) */
1216 * Description: Leave all power save mode: LPS, FwLPS, IPS if needed.
1217 * Move code to function by tynli. 2010.03.26.
1219 void LeaveAllPowerSaveMode(IN PADAPTER Adapter)
1221 struct dvobj_priv *dvobj = adapter_to_dvobj(Adapter);
1222 struct mlme_priv *pmlmepriv = &(Adapter->mlmepriv);
1224 int n_assoc_iface = 0;
1228 /* RTW_INFO("%s.....\n",__FUNCTION__); */
1230 if (_FALSE == Adapter->bup) {
1231 RTW_INFO(FUNC_ADPT_FMT ": bup=%d Skip!\n",
1232 FUNC_ADPT_ARG(Adapter), Adapter->bup);
1236 if (rtw_is_surprise_removed(Adapter)) {
1237 RTW_INFO(FUNC_ADPT_FMT ": bSurpriseRemoved=_TRUE Skip!\n", FUNC_ADPT_ARG(Adapter));
1241 for (i = 0; i < dvobj->iface_nums; i++) {
1242 if (check_fwstate(&(dvobj->padapters[i]->mlmepriv), WIFI_ASOC_STATE))
1246 if (n_assoc_iface) {
1248 #ifdef CONFIG_LPS_LCLK
1252 #ifdef CONFIG_P2P_PS
1253 p2p_ps_wk_cmd(Adapter, P2P_PS_DISABLE, enqueue);
1254 #endif /* CONFIG_P2P_PS */
1257 rtw_lps_ctrl_wk_cmd(Adapter, LPS_CTRL_LEAVE, enqueue);
1260 #ifdef CONFIG_LPS_LCLK
1261 LPS_Leave_check(Adapter);
1264 if (adapter_to_pwrctl(Adapter)->rf_pwrstate == rf_off) {
1265 #ifdef CONFIG_AUTOSUSPEND
1266 if (Adapter->registrypriv.usbss_enable) {
1267 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
1268 usb_disable_autosuspend(adapter_to_dvobj(Adapter)->pusbdev);
1269 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22) && LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 34))
1270 adapter_to_dvobj(Adapter)->pusbdev->autosuspend_disabled = Adapter->bDisableAutosuspend;/* autosuspend disabled by the user */
1275 #if defined(CONFIG_FWLPS_IN_IPS) || defined(CONFIG_SWLPS_IN_IPS) || (defined(CONFIG_PLATFORM_SPRD) && defined(CONFIG_RTL8188E))
1277 if (_FALSE == ips_leave(Adapter))
1278 RTW_INFO("======> ips_leave fail.............\n");
1280 #endif /* CONFIG_SWLPS_IN_IPS || (CONFIG_PLATFORM_SPRD && CONFIG_RTL8188E) */
1287 #ifdef CONFIG_LPS_LCLK
1288 void LPS_Leave_check(
1291 struct pwrctrl_priv *pwrpriv;
1296 pwrpriv = adapter_to_pwrctl(padapter);
1299 start_time = rtw_get_current_time();
1304 _enter_pwrlock(&pwrpriv->lock);
1306 if (rtw_is_surprise_removed(padapter)
1307 || (!rtw_is_hw_init_completed(padapter))
1308 #ifdef CONFIG_USB_HCI
1309 || rtw_is_drv_stopped(padapter)
1311 || (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
1315 _exit_pwrlock(&pwrpriv->lock);
1317 if (_TRUE == bReady)
1320 if (rtw_get_passing_time_ms(start_time) > 100) {
1321 RTW_INFO("Wait for cpwm event than 100 ms!!!\n");
1330 * Caller:ISR handler...
1332 * This will be called when CPWM interrupt is up.
1334 * using to update cpwn of drv; and drv willl make a decision to up or down pwr level
1338 struct reportpwrstate_parm *preportpwrstate)
1340 struct pwrctrl_priv *pwrpriv;
1343 pwrpriv = adapter_to_pwrctl(padapter);
1345 if (pwrpriv->cpwm_tog == (preportpwrstate->state & PS_TOGGLE)) {
1350 _enter_pwrlock(&pwrpriv->lock);
1352 #ifdef CONFIG_LPS_RPWM_TIMER
1353 if (pwrpriv->rpwm < PS_STATE_S2) {
1354 RTW_INFO("%s: Redundant CPWM Int. RPWM=0x%02X CPWM=0x%02x\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1355 _exit_pwrlock(&pwrpriv->lock);
1358 #endif /* CONFIG_LPS_RPWM_TIMER */
1360 pwrpriv->cpwm = PS_STATE(preportpwrstate->state);
1361 pwrpriv->cpwm_tog = preportpwrstate->state & PS_TOGGLE;
1363 if (pwrpriv->cpwm >= PS_STATE_S2) {
1364 if (pwrpriv->alives & CMD_ALIVE)
1365 _rtw_up_sema(&padapter->cmdpriv.cmd_queue_sema);
1367 if (pwrpriv->alives & XMIT_ALIVE)
1368 _rtw_up_sema(&padapter->xmitpriv.xmit_sema);
1371 _exit_pwrlock(&pwrpriv->lock);
1377 static void cpwm_event_callback(struct work_struct *work)
1379 struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, cpwm_event);
1380 struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
1381 _adapter *adapter = dvobj->padapters[IFACE_ID0];
1382 struct reportpwrstate_parm report;
1384 /* RTW_INFO("%s\n",__FUNCTION__); */
1386 report.state = PS_STATE_S2;
1387 cpwm_int_hdl(adapter, &report);
1390 #ifdef CONFIG_LPS_RPWM_TIMER
1391 static void rpwmtimeout_workitem_callback(struct work_struct *work)
1394 struct dvobj_priv *dvobj;
1395 struct pwrctrl_priv *pwrpriv;
1398 pwrpriv = container_of(work, struct pwrctrl_priv, rpwmtimeoutwi);
1399 dvobj = pwrctl_to_dvobj(pwrpriv);
1400 padapter = dvobj->padapters[IFACE_ID0];
1401 /* RTW_INFO("+%s: rpwm=0x%02X cpwm=0x%02X\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm); */
1403 _enter_pwrlock(&pwrpriv->lock);
1404 if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2)) {
1405 RTW_INFO("%s: rpwm=0x%02X cpwm=0x%02X CPWM done!\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1408 _exit_pwrlock(&pwrpriv->lock);
1410 if (rtw_read8(padapter, 0x100) != 0xEA) {
1412 struct reportpwrstate_parm report;
1414 report.state = PS_STATE_S2;
1415 RTW_INFO("\n%s: FW already leave 32K!\n\n", __func__);
1416 cpwm_int_hdl(padapter, &report);
1418 RTW_INFO("\n%s: FW already leave 32K!\n\n", __func__);
1419 cpwm_event_callback(&pwrpriv->cpwm_event);
1424 _enter_pwrlock(&pwrpriv->lock);
1426 if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2)) {
1427 RTW_INFO("%s: cpwm=%d, nothing to do!\n", __func__, pwrpriv->cpwm);
1430 pwrpriv->brpwmtimeout = _TRUE;
1431 rtw_set_rpwm(padapter, pwrpriv->rpwm);
1432 pwrpriv->brpwmtimeout = _FALSE;
1435 _exit_pwrlock(&pwrpriv->lock);
1439 * This function is a timer handler, can't do any IO in it.
1441 static void pwr_rpwm_timeout_handler(void *FunctionContext)
1444 struct pwrctrl_priv *pwrpriv;
1447 padapter = (PADAPTER)FunctionContext;
1448 pwrpriv = adapter_to_pwrctl(padapter);
1449 RTW_INFO("+%s: rpwm=0x%02X cpwm=0x%02X\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1451 if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2)) {
1452 RTW_INFO("+%s: cpwm=%d, nothing to do!\n", __func__, pwrpriv->cpwm);
1456 _set_workitem(&pwrpriv->rpwmtimeoutwi);
1458 #endif /* CONFIG_LPS_RPWM_TIMER */
1460 __inline static void register_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
1462 pwrctrl->alives |= tag;
1465 __inline static void unregister_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
1467 pwrctrl->alives &= ~tag;
1473 * Check if the fw_pwrstate is okay for I/O.
1474 * If not (cpwm is less than S2), then the sub-routine
1475 * will raise the cpwm to be greater than or equal to S2.
1477 * Calling Context: Passive
1480 * 1. this function will request pwrctrl->lock
1483 * _SUCCESS hardware is ready for I/O
1484 * _FAIL can't I/O right now
1486 s32 rtw_register_task_alive(PADAPTER padapter, u32 task)
1489 struct pwrctrl_priv *pwrctrl;
1494 pwrctrl = adapter_to_pwrctl(padapter);
1497 _enter_pwrlock(&pwrctrl->lock);
1499 register_task_alive(pwrctrl, task);
1501 if (pwrctrl->bFwCurrentInPSMode == _TRUE) {
1503 if (pwrctrl->cpwm < pslv) {
1504 if (pwrctrl->cpwm < PS_STATE_S2)
1506 if (pwrctrl->rpwm < pslv)
1507 rtw_set_rpwm(padapter, pslv);
1511 _exit_pwrlock(&pwrctrl->lock);
1513 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1515 if (pwrctrl->cpwm >= PS_STATE_S2)
1518 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1526 * If task is done, call this func. to power down firmware again.
1529 * 1. this function will request pwrctrl->lock
1534 void rtw_unregister_task_alive(PADAPTER padapter, u32 task)
1536 struct pwrctrl_priv *pwrctrl;
1540 pwrctrl = adapter_to_pwrctl(padapter);
1543 #ifdef CONFIG_BT_COEXIST
1544 if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
1545 && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)) {
1548 val8 = rtw_btcoex_LpsVal(padapter);
1553 #endif /* CONFIG_BT_COEXIST */
1555 _enter_pwrlock(&pwrctrl->lock);
1557 unregister_task_alive(pwrctrl, task);
1559 if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
1560 && (pwrctrl->bFwCurrentInPSMode == _TRUE)) {
1562 if (pwrctrl->cpwm > pslv) {
1563 if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
1564 rtw_set_rpwm(padapter, pslv);
1568 _exit_pwrlock(&pwrctrl->lock);
1573 * Caller: rtw_xmit_thread
1575 * Check if the fw_pwrstate is okay for xmit.
1576 * If not (cpwm is less than S3), then the sub-routine
1577 * will raise the cpwm to be greater than or equal to S3.
1579 * Calling Context: Passive
1582 * _SUCCESS rtw_xmit_thread can write fifo/txcmd afterwards.
1583 * _FAIL rtw_xmit_thread can not do anything.
1585 s32 rtw_register_tx_alive(PADAPTER padapter)
1588 struct pwrctrl_priv *pwrctrl;
1593 pwrctrl = adapter_to_pwrctl(padapter);
1596 _enter_pwrlock(&pwrctrl->lock);
1598 register_task_alive(pwrctrl, XMIT_ALIVE);
1600 if (pwrctrl->bFwCurrentInPSMode == _TRUE) {
1602 if (pwrctrl->cpwm < pslv) {
1603 if (pwrctrl->cpwm < PS_STATE_S2)
1605 if (pwrctrl->rpwm < pslv)
1606 rtw_set_rpwm(padapter, pslv);
1610 _exit_pwrlock(&pwrctrl->lock);
1612 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1614 if (pwrctrl->cpwm >= PS_STATE_S2)
1617 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1624 * Caller: rtw_cmd_thread
1626 * Check if the fw_pwrstate is okay for issuing cmd.
1627 * If not (cpwm should be is less than S2), then the sub-routine
1628 * will raise the cpwm to be greater than or equal to S2.
1630 * Calling Context: Passive
1633 * _SUCCESS rtw_cmd_thread can issue cmds to firmware afterwards.
1634 * _FAIL rtw_cmd_thread can not do anything.
1636 s32 rtw_register_cmd_alive(PADAPTER padapter)
1639 struct pwrctrl_priv *pwrctrl;
1644 pwrctrl = adapter_to_pwrctl(padapter);
1647 _enter_pwrlock(&pwrctrl->lock);
1649 register_task_alive(pwrctrl, CMD_ALIVE);
1651 if (pwrctrl->bFwCurrentInPSMode == _TRUE) {
1653 if (pwrctrl->cpwm < pslv) {
1654 if (pwrctrl->cpwm < PS_STATE_S2)
1656 if (pwrctrl->rpwm < pslv)
1657 rtw_set_rpwm(padapter, pslv);
1661 _exit_pwrlock(&pwrctrl->lock);
1663 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1665 if (pwrctrl->cpwm >= PS_STATE_S2)
1668 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1677 * Calling Context: Dispatch/ISR
1683 s32 rtw_register_rx_alive(PADAPTER padapter)
1685 struct pwrctrl_priv *pwrctrl;
1688 pwrctrl = adapter_to_pwrctl(padapter);
1690 _enter_pwrlock(&pwrctrl->lock);
1692 register_task_alive(pwrctrl, RECV_ALIVE);
1694 _exit_pwrlock(&pwrctrl->lock);
1701 * Caller: evt_isr or evt_thread
1703 * Calling Context: Dispatch/ISR or Passive
1709 s32 rtw_register_evt_alive(PADAPTER padapter)
1711 struct pwrctrl_priv *pwrctrl;
1714 pwrctrl = adapter_to_pwrctl(padapter);
1716 _enter_pwrlock(&pwrctrl->lock);
1718 register_task_alive(pwrctrl, EVT_ALIVE);
1720 _exit_pwrlock(&pwrctrl->lock);
1730 * No more pkts for TX,
1731 * Then driver shall call this fun. to power down firmware again.
1733 void rtw_unregister_tx_alive(PADAPTER padapter)
1735 struct pwrctrl_priv *pwrctrl;
1737 struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1741 pwrctrl = adapter_to_pwrctl(padapter);
1744 #ifdef CONFIG_BT_COEXIST
1745 if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
1746 && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)) {
1749 val8 = rtw_btcoex_LpsVal(padapter);
1754 #endif /* CONFIG_BT_COEXIST */
1756 #ifdef CONFIG_P2P_PS
1757 for (i = 0; i < dvobj->iface_nums; i++) {
1758 iface = dvobj->padapters[i];
1759 if ((iface) && rtw_is_adapter_up(iface)) {
1760 if (iface->wdinfo.p2p_ps_mode > P2P_PS_NONE) {
1767 _enter_pwrlock(&pwrctrl->lock);
1769 unregister_task_alive(pwrctrl, XMIT_ALIVE);
1771 if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
1772 && (pwrctrl->bFwCurrentInPSMode == _TRUE)) {
1774 if (pwrctrl->cpwm > pslv) {
1775 if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
1776 rtw_set_rpwm(padapter, pslv);
1780 _exit_pwrlock(&pwrctrl->lock);
1787 * If all commands have been done,
1788 * and no more command to do,
1789 * then driver shall call this fun. to power down firmware again.
1791 void rtw_unregister_cmd_alive(PADAPTER padapter)
1794 struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1795 struct pwrctrl_priv *pwrctrl;
1799 pwrctrl = adapter_to_pwrctl(padapter);
1802 #ifdef CONFIG_BT_COEXIST
1803 if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
1804 && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)) {
1807 val8 = rtw_btcoex_LpsVal(padapter);
1812 #endif /* CONFIG_BT_COEXIST */
1814 #ifdef CONFIG_P2P_PS
1815 for (i = 0; i < dvobj->iface_nums; i++) {
1816 iface = dvobj->padapters[i];
1817 if ((iface) && rtw_is_adapter_up(iface)) {
1818 if (iface->wdinfo.p2p_ps_mode > P2P_PS_NONE) {
1826 _enter_pwrlock(&pwrctrl->lock);
1828 unregister_task_alive(pwrctrl, CMD_ALIVE);
1830 if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
1831 && (pwrctrl->bFwCurrentInPSMode == _TRUE)) {
1833 if (pwrctrl->cpwm > pslv) {
1834 if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
1835 rtw_set_rpwm(padapter, pslv);
1839 _exit_pwrlock(&pwrctrl->lock);
1846 void rtw_unregister_rx_alive(PADAPTER padapter)
1848 struct pwrctrl_priv *pwrctrl;
1851 pwrctrl = adapter_to_pwrctl(padapter);
1853 _enter_pwrlock(&pwrctrl->lock);
1855 unregister_task_alive(pwrctrl, RECV_ALIVE);
1858 _exit_pwrlock(&pwrctrl->lock);
1862 void rtw_unregister_evt_alive(PADAPTER padapter)
1864 struct pwrctrl_priv *pwrctrl;
1867 pwrctrl = adapter_to_pwrctl(padapter);
1869 unregister_task_alive(pwrctrl, EVT_ALIVE);
1872 _exit_pwrlock(&pwrctrl->lock);
1875 #endif /* CONFIG_LPS_LCLK */
1877 #ifdef CONFIG_RESUME_IN_WORKQUEUE
1878 static void resume_workitem_callback(struct work_struct *work);
1879 #endif /* CONFIG_RESUME_IN_WORKQUEUE */
1881 void rtw_init_pwrctrl_priv(PADAPTER padapter)
1883 struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
1887 #if defined(CONFIG_CONCURRENT_MODE)
1888 if (padapter->adapter_type != PRIMARY_ADAPTER)
1893 #ifdef PLATFORM_WINDOWS
1894 pwrctrlpriv->pnp_current_pwr_state = NdisDeviceStateD0;
1897 _init_pwrlock(&pwrctrlpriv->lock);
1898 _init_pwrlock(&pwrctrlpriv->check_32k_lock);
1899 pwrctrlpriv->rf_pwrstate = rf_on;
1900 pwrctrlpriv->ips_enter_cnts = 0;
1901 pwrctrlpriv->ips_leave_cnts = 0;
1902 pwrctrlpriv->lps_enter_cnts = 0;
1903 pwrctrlpriv->lps_leave_cnts = 0;
1904 pwrctrlpriv->bips_processing = _FALSE;
1906 pwrctrlpriv->ips_mode = padapter->registrypriv.ips_mode;
1907 pwrctrlpriv->ips_mode_req = padapter->registrypriv.ips_mode;
1909 pwrctrlpriv->pwr_state_check_interval = RTW_PWR_STATE_CHK_INTERVAL;
1910 pwrctrlpriv->pwr_state_check_cnts = 0;
1911 pwrctrlpriv->bInternalAutoSuspend = _FALSE;
1912 pwrctrlpriv->bInSuspend = _FALSE;
1913 pwrctrlpriv->bkeepfwalive = _FALSE;
1915 #ifdef CONFIG_AUTOSUSPEND
1916 #ifdef SUPPORT_HW_RFOFF_DETECTED
1917 pwrctrlpriv->pwr_state_check_interval = (pwrctrlpriv->bHWPwrPindetect) ? 1000 : 2000;
1921 pwrctrlpriv->LpsIdleCount = 0;
1923 #ifdef CONFIG_LPS_PG
1924 pwrctrlpriv->lpspg_rsvd_page_locate = 0;
1927 /* pwrctrlpriv->FWCtrlPSMode =padapter->registrypriv.power_mgnt; */ /* PS_MODE_MIN; */
1928 if (padapter->registrypriv.mp_mode == 1)
1929 pwrctrlpriv->power_mgnt = PS_MODE_ACTIVE ;
1931 pwrctrlpriv->power_mgnt = padapter->registrypriv.power_mgnt; /* PS_MODE_MIN; */
1932 pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt) ? _TRUE : _FALSE;
1934 pwrctrlpriv->bFwCurrentInPSMode = _FALSE;
1936 pwrctrlpriv->rpwm = 0;
1937 pwrctrlpriv->cpwm = PS_STATE_S4;
1939 pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
1940 pwrctrlpriv->smart_ps = padapter->registrypriv.smart_ps;
1941 pwrctrlpriv->bcn_ant_mode = 0;
1942 pwrctrlpriv->dtim = 0;
1944 pwrctrlpriv->tog = 0x80;
1946 #ifdef CONFIG_LPS_LCLK
1947 rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&pwrctrlpriv->rpwm));
1949 _init_workitem(&pwrctrlpriv->cpwm_event, cpwm_event_callback, NULL);
1951 #ifdef CONFIG_LPS_RPWM_TIMER
1952 pwrctrlpriv->brpwmtimeout = _FALSE;
1953 _init_workitem(&pwrctrlpriv->rpwmtimeoutwi, rpwmtimeout_workitem_callback, NULL);
1954 _init_timer(&pwrctrlpriv->pwr_rpwm_timer, padapter->pnetdev, pwr_rpwm_timeout_handler, padapter);
1955 #endif /* CONFIG_LPS_RPWM_TIMER */
1956 #endif /* CONFIG_LPS_LCLK */
1958 rtw_init_timer(&pwrctrlpriv->pwr_state_check_timer, padapter, pwr_state_check_handler);
1960 pwrctrlpriv->wowlan_mode = _FALSE;
1961 pwrctrlpriv->wowlan_ap_mode = _FALSE;
1962 pwrctrlpriv->wowlan_p2p_mode = _FALSE;
1963 pwrctrlpriv->wowlan_last_wake_reason = 0;
1965 #ifdef CONFIG_RESUME_IN_WORKQUEUE
1966 _init_workitem(&pwrctrlpriv->resume_work, resume_workitem_callback, NULL);
1967 pwrctrlpriv->rtw_workqueue = create_singlethread_workqueue("rtw_workqueue");
1968 #endif /* CONFIG_RESUME_IN_WORKQUEUE */
1970 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
1971 pwrctrlpriv->early_suspend.suspend = NULL;
1972 rtw_register_early_suspend(pwrctrlpriv);
1973 #endif /* CONFIG_HAS_EARLYSUSPEND || CONFIG_ANDROID_POWER */
1975 #ifdef CONFIG_GPIO_WAKEUP
1976 /*default low active*/
1977 pwrctrlpriv->is_high_active = HIGH_ACTIVE;
1978 val8 = (pwrctrlpriv->is_high_active == 0) ? 1 : 0;
1979 rtw_hal_switch_gpio_wl_ctrl(padapter, WAKEUP_GPIO_IDX, _TRUE);
1980 rtw_hal_set_output_gpio(padapter, WAKEUP_GPIO_IDX, val8);
1981 RTW_INFO("%s: set GPIO_%d %d as default.\n",
1982 __func__, WAKEUP_GPIO_IDX, val8);
1983 #endif /* CONFIG_GPIO_WAKEUP */
1985 #ifdef CONFIG_WOWLAN
1986 pwrctrlpriv->wowlan_pattern_idx = DEFAULT_PATTERN_NUM;
1987 pwrctrlpriv->wowlan_in_resume = _FALSE;
1988 for (i = 0 ; i < MAX_WKFM_NUM; i++) {
1989 _rtw_memset(pwrctrlpriv->patterns[i].content, '\0',
1990 sizeof(pwrctrlpriv->patterns[i].content));
1991 _rtw_memset(pwrctrlpriv->patterns[i].mask, '\0',
1992 sizeof(pwrctrlpriv->patterns[i].mask));
1993 pwrctrlpriv->patterns[i].len = 0;
1996 #ifdef CONFIG_PNO_SUPPORT
1997 pwrctrlpriv->pno_inited = _FALSE;
1998 pwrctrlpriv->pnlo_info = NULL;
1999 pwrctrlpriv->pscan_info = NULL;
2000 pwrctrlpriv->pno_ssid_list = NULL;
2001 #endif /* CONFIG_PNO_SUPPORT */
2002 pwrctrlpriv->wowlan_aoac_rpt_loc = 0;
2003 #endif /* CONFIG_WOWLAN */
2005 #ifdef CONFIG_LPS_POFF
2006 rtw_hal_set_hwreg(padapter, HW_VAR_LPS_POFF_INIT, 0);
2013 void rtw_free_pwrctrl_priv(PADAPTER adapter)
2015 struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(adapter);
2017 #if defined(CONFIG_CONCURRENT_MODE)
2018 if (adapter->adapter_type != PRIMARY_ADAPTER)
2023 /* _rtw_memset((unsigned char *)pwrctrlpriv, 0, sizeof(struct pwrctrl_priv)); */
2026 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2027 if (pwrctrlpriv->rtw_workqueue) {
2028 flush_workqueue(pwrctrlpriv->rtw_workqueue);
2029 destroy_workqueue(pwrctrlpriv->rtw_workqueue);
2033 #ifdef CONFIG_LPS_POFF
2034 rtw_hal_set_hwreg(adapter, HW_VAR_LPS_POFF_DEINIT, 0);
2037 #ifdef CONFIG_WOWLAN
2038 #ifdef CONFIG_PNO_SUPPORT
2039 if (pwrctrlpriv->pnlo_info != NULL)
2040 printk("****** pnlo_info memory leak********\n");
2042 if (pwrctrlpriv->pscan_info != NULL)
2043 printk("****** pscan_info memory leak********\n");
2045 if (pwrctrlpriv->pno_ssid_list != NULL)
2046 printk("****** pno_ssid_list memory leak********\n");
2048 #endif /* CONFIG_WOWLAN */
2050 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
2051 rtw_unregister_early_suspend(pwrctrlpriv);
2052 #endif /* CONFIG_HAS_EARLYSUSPEND || CONFIG_ANDROID_POWER */
2054 _free_pwrlock(&pwrctrlpriv->lock);
2055 _free_pwrlock(&pwrctrlpriv->check_32k_lock);
2059 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2060 extern int rtw_resume_process(_adapter *padapter);
2062 static void resume_workitem_callback(struct work_struct *work)
2064 struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, resume_work);
2065 struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2066 _adapter *adapter = dvobj->padapters[IFACE_ID0];
2068 RTW_INFO("%s\n", __FUNCTION__);
2070 rtw_resume_process(adapter);
2072 rtw_resume_unlock_suspend();
2075 void rtw_resume_in_workqueue(struct pwrctrl_priv *pwrpriv)
2077 /* accquire system's suspend lock preventing from falliing asleep while resume in workqueue */
2078 /* rtw_lock_suspend(); */
2080 rtw_resume_lock_suspend();
2083 queue_work(pwrpriv->rtw_workqueue, &pwrpriv->resume_work);
2085 _set_workitem(&pwrpriv->resume_work);
2088 #endif /* CONFIG_RESUME_IN_WORKQUEUE */
2090 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
2091 inline bool rtw_is_earlysuspend_registered(struct pwrctrl_priv *pwrpriv)
2093 return (pwrpriv->early_suspend.suspend) ? _TRUE : _FALSE;
2096 inline bool rtw_is_do_late_resume(struct pwrctrl_priv *pwrpriv)
2098 return (pwrpriv->do_late_resume) ? _TRUE : _FALSE;
2101 inline void rtw_set_do_late_resume(struct pwrctrl_priv *pwrpriv, bool enable)
2103 pwrpriv->do_late_resume = enable;
2107 #ifdef CONFIG_HAS_EARLYSUSPEND
2108 extern int rtw_resume_process(_adapter *padapter);
2109 static void rtw_early_suspend(struct early_suspend *h)
2111 struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2112 RTW_INFO("%s\n", __FUNCTION__);
2114 rtw_set_do_late_resume(pwrpriv, _FALSE);
2117 static void rtw_late_resume(struct early_suspend *h)
2119 struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2120 struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2121 _adapter *adapter = dvobj->padapters[IFACE_ID0];
2123 RTW_INFO("%s\n", __FUNCTION__);
2125 if (pwrpriv->do_late_resume) {
2126 rtw_set_do_late_resume(pwrpriv, _FALSE);
2127 rtw_resume_process(adapter);
2131 void rtw_register_early_suspend(struct pwrctrl_priv *pwrpriv)
2133 RTW_INFO("%s\n", __FUNCTION__);
2135 /* jeff: set the early suspend level before blank screen, so we wll do late resume after scree is lit */
2136 pwrpriv->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
2137 pwrpriv->early_suspend.suspend = rtw_early_suspend;
2138 pwrpriv->early_suspend.resume = rtw_late_resume;
2139 register_early_suspend(&pwrpriv->early_suspend);
2144 void rtw_unregister_early_suspend(struct pwrctrl_priv *pwrpriv)
2146 RTW_INFO("%s\n", __FUNCTION__);
2148 rtw_set_do_late_resume(pwrpriv, _FALSE);
2150 if (pwrpriv->early_suspend.suspend)
2151 unregister_early_suspend(&pwrpriv->early_suspend);
2153 pwrpriv->early_suspend.suspend = NULL;
2154 pwrpriv->early_suspend.resume = NULL;
2156 #endif /* CONFIG_HAS_EARLYSUSPEND */
2158 #ifdef CONFIG_ANDROID_POWER
2159 #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2160 extern int rtw_resume_process(PADAPTER padapter);
2162 static void rtw_early_suspend(android_early_suspend_t *h)
2164 struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2165 RTW_INFO("%s\n", __FUNCTION__);
2167 rtw_set_do_late_resume(pwrpriv, _FALSE);
2170 static void rtw_late_resume(android_early_suspend_t *h)
2172 struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2173 struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2174 _adapter *adapter = dvobj->padapters[IFACE_ID0];
2176 RTW_INFO("%s\n", __FUNCTION__);
2177 if (pwrpriv->do_late_resume) {
2178 #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2179 rtw_set_do_late_resume(pwrpriv, _FALSE);
2180 rtw_resume_process(adapter);
2185 void rtw_register_early_suspend(struct pwrctrl_priv *pwrpriv)
2187 RTW_INFO("%s\n", __FUNCTION__);
2189 /* jeff: set the early suspend level before blank screen, so we wll do late resume after scree is lit */
2190 pwrpriv->early_suspend.level = ANDROID_EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
2191 pwrpriv->early_suspend.suspend = rtw_early_suspend;
2192 pwrpriv->early_suspend.resume = rtw_late_resume;
2193 android_register_early_suspend(&pwrpriv->early_suspend);
2196 void rtw_unregister_early_suspend(struct pwrctrl_priv *pwrpriv)
2198 RTW_INFO("%s\n", __FUNCTION__);
2200 rtw_set_do_late_resume(pwrpriv, _FALSE);
2202 if (pwrpriv->early_suspend.suspend)
2203 android_unregister_early_suspend(&pwrpriv->early_suspend);
2205 pwrpriv->early_suspend.suspend = NULL;
2206 pwrpriv->early_suspend.resume = NULL;
2208 #endif /* CONFIG_ANDROID_POWER */
2210 u8 rtw_interface_ps_func(_adapter *padapter, HAL_INTF_PS_FUNC efunc_id, u8 *val)
2213 rtw_hal_intf_ps_func(padapter, efunc_id, val);
2219 inline void rtw_set_ips_deny(_adapter *padapter, u32 ms)
2221 struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
2222 pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ms);
2226 * rtw_pwr_wakeup - Wake the NIC up from: 1)IPS. 2)USB autosuspend
2227 * @adapter: pointer to _adapter structure
2228 * @ips_deffer_ms: the ms wiil prevent from falling into IPS after wakeup
2229 * Return _SUCCESS or _FAIL
2232 int _rtw_pwr_wakeup(_adapter *padapter, u32 ips_deffer_ms, const char *caller)
2234 struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
2235 struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
2236 struct mlme_priv *pmlmepriv;
2239 u32 start = rtw_get_current_time();
2242 LeaveAllPowerSaveMode(padapter);
2244 /* IPS still bound with primary adapter */
2245 padapter = GET_PRIMARY_ADAPTER(padapter);
2246 pmlmepriv = &padapter->mlmepriv;
2248 if (pwrpriv->ips_deny_time < rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms))
2249 pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms);
2252 if (pwrpriv->ps_processing) {
2253 RTW_INFO("%s wait ps_processing...\n", __func__);
2254 while (pwrpriv->ps_processing && rtw_get_passing_time_ms(start) <= 3000)
2256 if (pwrpriv->ps_processing)
2257 RTW_INFO("%s wait ps_processing timeout\n", __func__);
2259 RTW_INFO("%s wait ps_processing done\n", __func__);
2262 #ifdef DBG_CONFIG_ERROR_DETECT
2263 if (rtw_hal_sreset_inprogress(padapter)) {
2264 RTW_INFO("%s wait sreset_inprogress...\n", __func__);
2265 while (rtw_hal_sreset_inprogress(padapter) && rtw_get_passing_time_ms(start) <= 4000)
2267 if (rtw_hal_sreset_inprogress(padapter))
2268 RTW_INFO("%s wait sreset_inprogress timeout\n", __func__);
2270 RTW_INFO("%s wait sreset_inprogress done\n", __func__);
2274 if (pwrpriv->bInternalAutoSuspend == _FALSE && pwrpriv->bInSuspend) {
2275 RTW_INFO("%s wait bInSuspend...\n", __func__);
2276 while (pwrpriv->bInSuspend
2277 && ((rtw_get_passing_time_ms(start) <= 3000 && !rtw_is_do_late_resume(pwrpriv))
2278 || (rtw_get_passing_time_ms(start) <= 500 && rtw_is_do_late_resume(pwrpriv)))
2281 if (pwrpriv->bInSuspend)
2282 RTW_INFO("%s wait bInSuspend timeout\n", __func__);
2284 RTW_INFO("%s wait bInSuspend done\n", __func__);
2287 /* System suspend is not allowed to wakeup */
2288 if ((pwrpriv->bInternalAutoSuspend == _FALSE) && (_TRUE == pwrpriv->bInSuspend)) {
2294 if ((pwrpriv->bInternalAutoSuspend == _TRUE) && (padapter->net_closed == _TRUE)) {
2299 /* I think this should be check in IPS, LPS, autosuspend functions... */
2300 if (check_fwstate(pmlmepriv, _FW_LINKED) == _TRUE) {
2301 #if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
2302 if (_TRUE == pwrpriv->bInternalAutoSuspend) {
2303 if (0 == pwrpriv->autopm_cnt) {
2304 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33))
2305 if (usb_autopm_get_interface(adapter_to_dvobj(padapter)->pusbintf) < 0)
2306 RTW_INFO("can't get autopm:\n");
2307 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20))
2308 usb_autopm_disable(adapter_to_dvobj(padapter)->pusbintf);
2310 usb_autoresume_device(adapter_to_dvobj(padapter)->pusbdev, 1);
2312 pwrpriv->autopm_cnt++;
2314 #endif /* #if defined (CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND) */
2317 #if defined(CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND)
2319 #endif /* #if defined (CONFIG_BT_COEXIST) && defined (CONFIG_AUTOSUSPEND) */
2322 if (rf_off == pwrpriv->rf_pwrstate) {
2323 #ifdef CONFIG_USB_HCI
2324 #ifdef CONFIG_AUTOSUSPEND
2325 if (pwrpriv->brfoffbyhw == _TRUE) {
2326 RTW_INFO("hw still in rf_off state ...........\n");
2329 } else if (padapter->registrypriv.usbss_enable) {
2330 RTW_INFO("%s call autoresume_enter....\n", __FUNCTION__);
2331 if (_FAIL == autoresume_enter(padapter)) {
2332 RTW_INFO("======> autoresume fail.............\n");
2341 RTW_INFO("%s call ips_leave....\n", __FUNCTION__);
2342 if (_FAIL == ips_leave(padapter)) {
2343 RTW_INFO("======> ips_leave fail.............\n");
2351 /* TODO: the following checking need to be merged... */
2352 if (rtw_is_drv_stopped(padapter)
2354 || !rtw_is_hw_init_completed(padapter)
2356 RTW_INFO("%s: bDriverStopped=%s, bup=%d, hw_init_completed=%u\n"
2358 , rtw_is_drv_stopped(padapter) ? "True" : "False"
2360 , rtw_get_hw_init_completed(padapter));
2366 if (pwrpriv->ips_deny_time < rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms))
2367 pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms);
2372 int rtw_pm_set_lps(_adapter *padapter, u8 mode)
2375 struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2377 if (mode < PS_MODE_NUM) {
2378 if (pwrctrlpriv->power_mgnt != mode) {
2379 if (PS_MODE_ACTIVE == mode)
2380 LeaveAllPowerSaveMode(padapter);
2382 pwrctrlpriv->LpsIdleCount = 2;
2383 pwrctrlpriv->power_mgnt = mode;
2384 pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt) ? _TRUE : _FALSE;
2392 int rtw_pm_set_ips(_adapter *padapter, u8 mode)
2394 struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2396 if (mode == IPS_NORMAL || mode == IPS_LEVEL_2) {
2397 rtw_ips_mode_req(pwrctrlpriv, mode);
2398 RTW_INFO("%s %s\n", __FUNCTION__, mode == IPS_NORMAL ? "IPS_NORMAL" : "IPS_LEVEL_2");
2400 } else if (mode == IPS_NONE) {
2401 rtw_ips_mode_req(pwrctrlpriv, mode);
2402 RTW_INFO("%s %s\n", __FUNCTION__, "IPS_NONE");
2403 if (!rtw_is_surprise_removed(padapter) && (_FAIL == rtw_pwr_wakeup(padapter)))
2412 * This function will request pwrctrl LOCK!
2414 void rtw_ps_deny(PADAPTER padapter, PS_DENY_REASON reason)
2416 struct pwrctrl_priv *pwrpriv;
2420 /* RTW_INFO("+" FUNC_ADPT_FMT ": Request PS deny for %d (0x%08X)\n",
2421 * FUNC_ADPT_ARG(padapter), reason, BIT(reason)); */
2423 pwrpriv = adapter_to_pwrctl(padapter);
2425 _enter_pwrlock(&pwrpriv->lock);
2426 if (pwrpriv->ps_deny & BIT(reason)) {
2427 RTW_INFO(FUNC_ADPT_FMT ": [WARNING] Reason %d had been set before!!\n",
2428 FUNC_ADPT_ARG(padapter), reason);
2430 pwrpriv->ps_deny |= BIT(reason);
2431 _exit_pwrlock(&pwrpriv->lock);
2433 /* RTW_INFO("-" FUNC_ADPT_FMT ": Now PS deny for 0x%08X\n",
2434 * FUNC_ADPT_ARG(padapter), pwrpriv->ps_deny); */
2439 * This function will request pwrctrl LOCK!
2441 void rtw_ps_deny_cancel(PADAPTER padapter, PS_DENY_REASON reason)
2443 struct pwrctrl_priv *pwrpriv;
2446 /* RTW_INFO("+" FUNC_ADPT_FMT ": Cancel PS deny for %d(0x%08X)\n",
2447 * FUNC_ADPT_ARG(padapter), reason, BIT(reason)); */
2449 pwrpriv = adapter_to_pwrctl(padapter);
2451 _enter_pwrlock(&pwrpriv->lock);
2452 if ((pwrpriv->ps_deny & BIT(reason)) == 0) {
2453 RTW_INFO(FUNC_ADPT_FMT ": [ERROR] Reason %d had been canceled before!!\n",
2454 FUNC_ADPT_ARG(padapter), reason);
2456 pwrpriv->ps_deny &= ~BIT(reason);
2457 _exit_pwrlock(&pwrpriv->lock);
2459 /* RTW_INFO("-" FUNC_ADPT_FMT ": Now PS deny for 0x%08X\n",
2460 * FUNC_ADPT_ARG(padapter), pwrpriv->ps_deny); */
2465 * Before calling this function pwrctrl lock should be occupied already,
2466 * otherwise it may return incorrect value.
2468 u32 rtw_ps_deny_get(PADAPTER padapter)
2473 deny = adapter_to_pwrctl(padapter)->ps_deny;