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