1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
6 ******************************************************************************/
7 #define _RTW_PWRCTRL_C_
9 #include <osdep_service.h>
10 #include <drv_types.h>
11 #include <osdep_intf.h>
12 #include <usb_ops_linux.h>
13 #include <linux/usb.h>
15 static int rtw_hw_suspend(struct adapter *padapter)
17 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
18 struct net_device *pnetdev = padapter->pnetdev;
20 if ((!padapter->bup) || (padapter->bDriverStopped) ||
21 (padapter->bSurpriseRemoved)) {
22 DBG_88E("padapter->bup=%d bDriverStopped=%d bSurpriseRemoved = %d\n",
23 padapter->bup, padapter->bDriverStopped,
24 padapter->bSurpriseRemoved);
29 LeaveAllPowerSaveMode(padapter);
31 DBG_88E("==> %s\n", __func__);
32 mutex_lock(&pwrpriv->mutex_lock);
33 pwrpriv->bips_processing = true;
36 netif_carrier_off(pnetdev);
37 netif_tx_stop_all_queues(pnetdev);
41 rtw_disassoc_cmd(padapter, 500, false);
43 /* s2-2. indicate disconnect to os */
45 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
47 if (check_fwstate(pmlmepriv, _FW_LINKED)) {
48 _clr_fwstate_(pmlmepriv, _FW_LINKED);
50 LedControl8188eu(padapter, LED_CTL_NO_LINK);
52 rtw_os_indicate_disconnect(padapter);
54 /* donnot enqueue cmd */
55 rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_DISCONNECT, 0);
59 rtw_free_assoc_resources(padapter);
62 rtw_free_network_queue(padapter, true);
63 rtw_ips_dev_unload(padapter);
64 pwrpriv->rf_pwrstate = rf_off;
65 pwrpriv->bips_processing = false;
67 mutex_unlock(&pwrpriv->mutex_lock);
72 DBG_88E("%s, failed\n", __func__);
76 static int rtw_hw_resume(struct adapter *padapter)
78 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
79 struct net_device *pnetdev = padapter->pnetdev;
82 DBG_88E("==> %s\n", __func__);
83 mutex_lock(&pwrpriv->mutex_lock);
84 pwrpriv->bips_processing = true;
85 rtw_reset_drv_sw(padapter);
87 if (ips_netdrv_open((struct adapter *)rtw_netdev_priv(pnetdev)) != _SUCCESS) {
88 mutex_unlock(&pwrpriv->mutex_lock);
92 netif_device_attach(pnetdev);
93 netif_carrier_on(pnetdev);
95 if (!netif_queue_stopped(pnetdev))
96 netif_start_queue(pnetdev);
98 netif_wake_queue(pnetdev);
100 pwrpriv->bkeepfwalive = false;
101 pwrpriv->brfoffbyhw = false;
103 pwrpriv->rf_pwrstate = rf_on;
104 pwrpriv->bips_processing = false;
106 mutex_unlock(&pwrpriv->mutex_lock);
110 DBG_88E("%s, Open net dev failed\n", __func__);
114 void ips_enter(struct adapter *padapter)
116 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
117 struct xmit_priv *pxmit_priv = &padapter->xmitpriv;
119 if (padapter->registrypriv.mp_mode == 1)
122 if (pxmit_priv->free_xmitbuf_cnt != NR_XMITBUFF ||
123 pxmit_priv->free_xmit_extbuf_cnt != NR_XMIT_EXTBUFF) {
124 DBG_88E_LEVEL(_drv_info_, "There are some pkts to transmit\n");
125 DBG_88E_LEVEL(_drv_info_, "free_xmitbuf_cnt: %d, free_xmit_extbuf_cnt: %d\n",
126 pxmit_priv->free_xmitbuf_cnt, pxmit_priv->free_xmit_extbuf_cnt);
130 mutex_lock(&pwrpriv->mutex_lock);
132 pwrpriv->bips_processing = true;
134 /* syn ips_mode with request */
135 pwrpriv->ips_mode = pwrpriv->ips_mode_req;
137 pwrpriv->ips_enter_cnts++;
138 DBG_88E("==>%s:%d\n", __func__, pwrpriv->ips_enter_cnts);
139 if (rf_off == pwrpriv->change_rfpwrstate) {
140 pwrpriv->bpower_saving = true;
141 DBG_88E_LEVEL(_drv_info_, "nolinked power save enter\n");
143 if (pwrpriv->ips_mode == IPS_LEVEL_2)
144 pwrpriv->bkeepfwalive = true;
146 rtw_ips_pwr_down(padapter);
147 pwrpriv->rf_pwrstate = rf_off;
149 pwrpriv->bips_processing = false;
151 mutex_unlock(&pwrpriv->mutex_lock);
154 int ips_leave(struct adapter *padapter)
156 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
157 struct security_priv *psecuritypriv = &(padapter->securitypriv);
158 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
159 int result = _SUCCESS;
162 mutex_lock(&pwrpriv->mutex_lock);
164 if ((pwrpriv->rf_pwrstate == rf_off) && (!pwrpriv->bips_processing)) {
165 pwrpriv->bips_processing = true;
166 pwrpriv->change_rfpwrstate = rf_on;
167 pwrpriv->ips_leave_cnts++;
168 DBG_88E("==>%s:%d\n", __func__, pwrpriv->ips_leave_cnts);
170 result = rtw_ips_pwr_up(padapter);
171 if (result == _SUCCESS)
172 pwrpriv->rf_pwrstate = rf_on;
174 DBG_88E_LEVEL(_drv_info_, "nolinked power save leave\n");
176 if ((_WEP40_ == psecuritypriv->dot11PrivacyAlgrthm) || (_WEP104_ == psecuritypriv->dot11PrivacyAlgrthm)) {
177 DBG_88E("==>%s, channel(%d), processing(%x)\n", __func__, padapter->mlmeextpriv.cur_channel, pwrpriv->bips_processing);
178 set_channel_bwmode(padapter, padapter->mlmeextpriv.cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
179 for (keyid = 0; keyid < 4; keyid++) {
180 if (pmlmepriv->key_mask & BIT(keyid)) {
181 if (keyid == psecuritypriv->dot11PrivacyKeyIndex)
182 result = rtw_set_key(padapter, psecuritypriv, keyid, 1);
184 result = rtw_set_key(padapter, psecuritypriv, keyid, 0);
189 DBG_88E("==> %s.....LED(0x%08x)...\n", __func__, usb_read32(padapter, 0x4c));
190 pwrpriv->bips_processing = false;
192 pwrpriv->bkeepfwalive = false;
193 pwrpriv->bpower_saving = false;
196 mutex_unlock(&pwrpriv->mutex_lock);
201 static bool rtw_pwr_unassociated_idle(struct adapter *adapter)
203 struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
206 if (time_after_eq(adapter->pwrctrlpriv.ips_deny_time, jiffies))
209 if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE|WIFI_SITE_MONITOR) ||
210 check_fwstate(pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS) ||
211 check_fwstate(pmlmepriv, WIFI_AP_STATE) ||
212 check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE))
221 void rtw_ps_processor(struct adapter *padapter)
223 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
224 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
225 enum rt_rf_power_state rfpwrstate;
227 pwrpriv->ps_processing = true;
229 if (pwrpriv->bips_processing)
232 if (padapter->pwrctrlpriv.bHWPwrPindetect) {
233 rfpwrstate = RfOnOffDetect(padapter);
234 DBG_88E("@@@@- #2 %s==> rfstate:%s\n", __func__, (rfpwrstate == rf_on) ? "rf_on" : "rf_off");
236 if (rfpwrstate != pwrpriv->rf_pwrstate) {
237 if (rfpwrstate == rf_off) {
238 pwrpriv->change_rfpwrstate = rf_off;
239 pwrpriv->brfoffbyhw = true;
240 rtw_hw_suspend(padapter);
242 pwrpriv->change_rfpwrstate = rf_on;
243 rtw_hw_resume(padapter);
245 DBG_88E("current rf_pwrstate(%s)\n", (pwrpriv->rf_pwrstate == rf_off) ? "rf_off" : "rf_on");
247 pwrpriv->pwr_state_check_cnts++;
250 if (pwrpriv->ips_mode_req == IPS_NONE)
253 if (!rtw_pwr_unassociated_idle(padapter))
256 if ((pwrpriv->rf_pwrstate == rf_on) && ((pwrpriv->pwr_state_check_cnts%4) == 0)) {
257 DBG_88E("==>%s .fw_state(%x)\n", __func__, get_fwstate(pmlmepriv));
258 pwrpriv->change_rfpwrstate = rf_off;
263 rtw_set_pwr_state_check_timer(&padapter->pwrctrlpriv);
264 pwrpriv->ps_processing = false;
267 static void pwr_state_check_handler(struct timer_list *t)
269 struct adapter *padapter =
270 from_timer(padapter, t,
271 pwrctrlpriv.pwr_state_check_timer);
273 rtw_ps_cmd(padapter);
280 * pslv power state level, only could be PS_STATE_S0 ~ PS_STATE_S4
283 void rtw_set_rpwm(struct adapter *padapter, u8 pslv)
286 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
288 pslv = PS_STATE(pslv);
290 if (pwrpriv->btcoex_rfon) {
291 if (pslv < PS_STATE_S4)
295 if ((pwrpriv->rpwm == pslv)) {
296 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
297 ("%s: Already set rpwm[0x%02X], new=0x%02X!\n", __func__, pwrpriv->rpwm, pslv));
301 if ((padapter->bSurpriseRemoved) ||
302 (!padapter->hw_init_completed)) {
303 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
304 ("%s: SurpriseRemoved(%d) hw_init_completed(%d)\n",
305 __func__, padapter->bSurpriseRemoved, padapter->hw_init_completed));
307 pwrpriv->cpwm = PS_STATE_S4;
312 if (padapter->bDriverStopped) {
313 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
314 ("%s: change power state(0x%02X) when DriverStopped\n", __func__, pslv));
316 if (pslv < PS_STATE_S2) {
317 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
318 ("%s: Reject to enter PS_STATE(0x%02X) lower than S2 when DriverStopped!!\n", __func__, pslv));
323 rpwm = pslv | pwrpriv->tog;
324 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
325 ("%s: rpwm=0x%02x cpwm=0x%02x\n", __func__, rpwm, pwrpriv->cpwm));
327 pwrpriv->rpwm = pslv;
329 rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&rpwm));
331 pwrpriv->tog += 0x80;
332 pwrpriv->cpwm = pslv;
335 static u8 PS_RDY_CHECK(struct adapter *padapter)
337 unsigned long curr_time, delta_time;
338 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
339 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
342 delta_time = curr_time - pwrpriv->DelayLPSLastTimeStamp;
344 if (delta_time < LPS_DELAY_TIME)
347 if ((check_fwstate(pmlmepriv, _FW_LINKED) == false) ||
348 (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY)) ||
349 (check_fwstate(pmlmepriv, WIFI_AP_STATE)) ||
350 (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) ||
351 (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)))
353 if (pwrpriv->bInSuspend)
355 if ((padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) && (padapter->securitypriv.binstallGrpkey == false)) {
356 DBG_88E("Group handshake still in progress !!!\n");
362 void rtw_set_ps_mode(struct adapter *padapter, u8 ps_mode, u8 smart_ps, u8 bcn_ant_mode)
364 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
366 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
367 ("%s: PowerMode=%d Smart_PS=%d\n",
368 __func__, ps_mode, smart_ps));
370 if (ps_mode > PM_Card_Disable) {
371 RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_, ("ps_mode:%d error\n", ps_mode));
375 if (pwrpriv->pwr_mode == ps_mode) {
376 if (PS_MODE_ACTIVE == ps_mode)
379 if ((pwrpriv->smart_ps == smart_ps) &&
380 (pwrpriv->bcn_ant_mode == bcn_ant_mode))
384 /* if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) */
385 if (ps_mode == PS_MODE_ACTIVE) {
386 if (PS_RDY_CHECK(padapter)) {
387 DBG_88E("%s: Enter 802.11 power save\n", __func__);
388 pwrpriv->bFwCurrentInPSMode = true;
389 pwrpriv->pwr_mode = ps_mode;
390 pwrpriv->smart_ps = smart_ps;
391 pwrpriv->bcn_ant_mode = bcn_ant_mode;
392 rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
393 rtw_set_rpwm(padapter, PS_STATE_S2);
404 s32 LPS_RF_ON_check(struct adapter *padapter, u32 delay_ms)
406 unsigned long start_time;
410 start_time = jiffies;
412 rtw_hal_get_hwreg(padapter, HW_VAR_FWLPS_RF_ON, &bAwake);
416 if (padapter->bSurpriseRemoved) {
418 DBG_88E("%s: device surprise removed!!\n", __func__);
422 if (jiffies_to_msecs(jiffies - start_time) > delay_ms) {
424 DBG_88E("%s: Wait for FW LPS leave more than %u ms!!!\n", __func__, delay_ms);
435 /* Enter the leisure power save mode. */
437 void LPS_Enter(struct adapter *padapter)
439 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
441 if (PS_RDY_CHECK(padapter) == false)
444 if (pwrpriv->bLeisurePs) {
445 /* Idle for a while if we connect to AP a while ago. */
446 if (pwrpriv->LpsIdleCount >= 2) { /* 4 Sec */
447 if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) {
448 pwrpriv->bpower_saving = true;
449 DBG_88E("%s smart_ps:%d\n", __func__, pwrpriv->smart_ps);
450 /* For Tenda W311R IOT issue */
451 rtw_set_ps_mode(padapter, pwrpriv->power_mgnt, pwrpriv->smart_ps, 0);
454 pwrpriv->LpsIdleCount++;
459 #define LPS_LEAVE_TIMEOUT_MS 100
462 /* Leave the leisure power save mode. */
463 void LPS_Leave(struct adapter *padapter)
465 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
467 if (pwrpriv->bLeisurePs) {
468 if (pwrpriv->pwr_mode != PS_MODE_ACTIVE) {
469 rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0);
471 if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
472 LPS_RF_ON_check(padapter, LPS_LEAVE_TIMEOUT_MS);
476 pwrpriv->bpower_saving = false;
480 /* Description: Leave all power save mode: LPS, FwLPS, IPS if needed. */
481 /* Move code to function by tynli. 2010.03.26. */
483 void LeaveAllPowerSaveMode(struct adapter *Adapter)
485 struct mlme_priv *pmlmepriv = &(Adapter->mlmepriv);
488 if (check_fwstate(pmlmepriv, _FW_LINKED))
489 rtw_lps_ctrl_wk_cmd(Adapter, LPS_CTRL_LEAVE, enqueue);
492 void rtw_init_pwrctrl_priv(struct adapter *padapter)
494 struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
496 mutex_init(&pwrctrlpriv->mutex_lock);
497 pwrctrlpriv->rf_pwrstate = rf_on;
498 pwrctrlpriv->ips_enter_cnts = 0;
499 pwrctrlpriv->ips_leave_cnts = 0;
500 pwrctrlpriv->bips_processing = false;
502 pwrctrlpriv->ips_mode = padapter->registrypriv.ips_mode;
503 pwrctrlpriv->ips_mode_req = padapter->registrypriv.ips_mode;
505 pwrctrlpriv->pwr_state_check_interval = RTW_PWR_STATE_CHK_INTERVAL;
506 pwrctrlpriv->pwr_state_check_cnts = 0;
507 pwrctrlpriv->bInternalAutoSuspend = false;
508 pwrctrlpriv->bInSuspend = false;
509 pwrctrlpriv->bkeepfwalive = false;
511 pwrctrlpriv->LpsIdleCount = 0;
512 if (padapter->registrypriv.mp_mode == 1)
513 pwrctrlpriv->power_mgnt = PS_MODE_ACTIVE;
515 pwrctrlpriv->power_mgnt = padapter->registrypriv.power_mgnt;/* PS_MODE_MIN; */
516 pwrctrlpriv->bLeisurePs = (pwrctrlpriv->power_mgnt != PS_MODE_ACTIVE) ? true : false;
518 pwrctrlpriv->bFwCurrentInPSMode = false;
520 pwrctrlpriv->rpwm = 0;
521 pwrctrlpriv->cpwm = PS_STATE_S4;
523 pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
524 pwrctrlpriv->smart_ps = padapter->registrypriv.smart_ps;
525 pwrctrlpriv->bcn_ant_mode = 0;
527 pwrctrlpriv->tog = 0x80;
529 pwrctrlpriv->btcoex_rfon = false;
531 timer_setup(&pwrctrlpriv->pwr_state_check_timer,
532 pwr_state_check_handler, 0);
536 * rtw_pwr_wakeup - Wake the NIC up from: 1)IPS. 2)USB autosuspend
537 * @adapter: pointer to struct adapter structure
538 * @ips_deffer_ms: the ms will prevent from falling into IPS after wakeup
539 * Return _SUCCESS or _FAIL
542 int _rtw_pwr_wakeup(struct adapter *padapter, u32 ips_deffer_ms, const char *caller)
544 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
545 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
546 unsigned long expires;
550 expires = jiffies + msecs_to_jiffies(ips_deffer_ms);
551 if (time_before(pwrpriv->ips_deny_time, expires))
552 pwrpriv->ips_deny_time = jiffies + msecs_to_jiffies(ips_deffer_ms);
555 if (pwrpriv->ps_processing) {
556 DBG_88E("%s wait ps_processing...\n", __func__);
557 while (pwrpriv->ps_processing &&
558 jiffies_to_msecs(jiffies - start) <= 3000)
560 if (pwrpriv->ps_processing)
561 DBG_88E("%s wait ps_processing timeout\n", __func__);
563 DBG_88E("%s wait ps_processing done\n", __func__);
566 /* System suspend is not allowed to wakeup */
567 if ((!pwrpriv->bInternalAutoSuspend) && (pwrpriv->bInSuspend)) {
573 if ((pwrpriv->bInternalAutoSuspend) && (padapter->net_closed)) {
578 /* I think this should be check in IPS, LPS, autosuspend functions... */
579 if (check_fwstate(pmlmepriv, _FW_LINKED)) {
583 if (rf_off == pwrpriv->rf_pwrstate) {
584 DBG_88E("%s call ips_leave....\n", __func__);
585 if (ips_leave(padapter) == _FAIL) {
586 DBG_88E("======> ips_leave fail.............\n");
592 /* TODO: the following checking need to be merged... */
593 if (padapter->bDriverStopped || !padapter->bup ||
594 !padapter->hw_init_completed) {
595 DBG_88E("%s: bDriverStopped=%d, bup=%d, hw_init_completed =%u\n"
597 , padapter->bDriverStopped
599 , padapter->hw_init_completed);
605 expires = jiffies + msecs_to_jiffies(ips_deffer_ms);
606 if (time_before(pwrpriv->ips_deny_time, expires))
607 pwrpriv->ips_deny_time = jiffies + msecs_to_jiffies(ips_deffer_ms);
611 int rtw_pm_set_lps(struct adapter *padapter, u8 mode)
614 struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
616 if (mode < PS_MODE_NUM) {
617 if (pwrctrlpriv->power_mgnt != mode) {
618 if (mode == PS_MODE_ACTIVE)
619 LeaveAllPowerSaveMode(padapter);
621 pwrctrlpriv->LpsIdleCount = 2;
622 pwrctrlpriv->power_mgnt = mode;
623 pwrctrlpriv->bLeisurePs = (pwrctrlpriv->power_mgnt != PS_MODE_ACTIVE) ? true : false;
632 int rtw_pm_set_ips(struct adapter *padapter, u8 mode)
634 struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
636 if (mode == IPS_NORMAL || mode == IPS_LEVEL_2) {
637 rtw_ips_mode_req(pwrctrlpriv, mode);
638 DBG_88E("%s %s\n", __func__, mode == IPS_NORMAL ? "IPS_NORMAL" : "IPS_LEVEL_2");
640 } else if (mode == IPS_NONE) {
641 rtw_ips_mode_req(pwrctrlpriv, mode);
642 DBG_88E("%s %s\n", __func__, "IPS_NONE");
643 if ((padapter->bSurpriseRemoved == 0) && (rtw_pwr_wakeup(padapter) == _FAIL))