xref: /OK3568_Linux_fs/external/rkwifibt/drivers/rtl8188fu/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 #define CPU_EXCEPTION_CODE 0xFAFAFAFA
rtw_cpwm_chk_fail_debug(_adapter * padapter)1626 static void rtw_cpwm_chk_fail_debug(_adapter *padapter)
1627 {
1628 	u32 cpu_state;
1629 
1630 	cpu_state = rtw_read32(padapter, 0x10FC);
1631 
1632 	RTW_INFO("[PS-DBG] Reg_10FC =0x%08x\n", cpu_state);
1633 	RTW_INFO("[PS-DBG] Reg_10F8 =0x%08x\n", rtw_read32(padapter, 0x10F8));
1634 	RTW_INFO("[PS-DBG] Reg_11F8 =0x%08x\n", rtw_read32(padapter, 0x11F8));
1635 	RTW_INFO("[PS-DBG] Reg_4A4 =0x%08x\n", rtw_read32(padapter, 0x4A4));
1636 	RTW_INFO("[PS-DBG] Reg_4A8 =0x%08x\n", rtw_read32(padapter, 0x4A8));
1637 
1638 	if (cpu_state == CPU_EXCEPTION_CODE) {
1639 		RTW_INFO("[PS-DBG] Reg_48C =0x%08x\n", rtw_read32(padapter, 0x48C));
1640 		RTW_INFO("[PS-DBG] Reg_490 =0x%08x\n", rtw_read32(padapter, 0x490));
1641 		RTW_INFO("[PS-DBG] Reg_494 =0x%08x\n", rtw_read32(padapter, 0x494));
1642 		RTW_INFO("[PS-DBG] Reg_498 =0x%08x\n", rtw_read32(padapter, 0x498));
1643 		RTW_INFO("[PS-DBG] Reg_49C =0x%08x\n", rtw_read32(padapter, 0x49C));
1644 		RTW_INFO("[PS-DBG] Reg_4A0 =0x%08x\n", rtw_read32(padapter, 0x4A0));
1645 		RTW_INFO("[PS-DBG] Reg_1BC =0x%08x\n", rtw_read32(padapter, 0x1BC));
1646 
1647 		RTW_INFO("[PS-DBG] Reg_008 =0x%08x\n", rtw_read32(padapter, 0x08));
1648 		RTW_INFO("[PS-DBG] Reg_2F0 =0x%08x\n", rtw_read32(padapter, 0x2F0));
1649 		RTW_INFO("[PS-DBG] Reg_2F4 =0x%08x\n", rtw_read32(padapter, 0x2F4));
1650 		RTW_INFO("[PS-DBG] Reg_2F8 =0x%08x\n", rtw_read32(padapter, 0x2F8));
1651 		RTW_INFO("[PS-DBG] Reg_2FC =0x%08x\n", rtw_read32(padapter, 0x2FC));
1652 
1653 		rtw_dump_fifo(RTW_DBGDUMP, padapter, 5, 0, 3072);
1654 	}
1655 }
1656 #endif
rpwmtimeout_workitem_callback(struct work_struct * work)1657 static void rpwmtimeout_workitem_callback(struct work_struct *work)
1658 {
1659 	PADAPTER padapter;
1660 	struct dvobj_priv *dvobj;
1661 	struct pwrctrl_priv *pwrpriv;
1662 
1663 
1664 	pwrpriv = container_of(work, struct pwrctrl_priv, rpwmtimeoutwi);
1665 	dvobj = pwrctl_to_dvobj(pwrpriv);
1666 	padapter = dvobj_get_primary_adapter(dvobj);
1667 
1668 	if (!padapter)
1669 		return;
1670 
1671 	if (RTW_CANNOT_RUN(padapter))
1672 		return;
1673 
1674 	_enter_pwrlock(&pwrpriv->lock);
1675 	if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2)) {
1676 		RTW_INFO("%s: rpwm=0x%02X cpwm=0x%02X CPWM done!\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1677 		goto exit;
1678 	}
1679 
1680 	if (pwrpriv->rpwm_retry++ < LPS_RPWM_RETRY_CNT) {
1681 		u8 rpwm = (pwrpriv->rpwm | pwrpriv->tog | PS_ACK);
1682 
1683 		rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&rpwm));
1684 
1685 		pwrpriv->tog += 0x80;
1686 		_set_timer(&pwrpriv->pwr_rpwm_timer, LPS_CPWM_TIMEOUT_MS);
1687 		goto exit;
1688 	}
1689 
1690 	pwrpriv->rpwm_retry = 0;
1691 	_exit_pwrlock(&pwrpriv->lock);
1692 
1693 #if defined(DBG_CPWM_CHK_FAIL) && (defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8821C) || defined(CONFIG_RTL8822C))
1694 	RTW_INFO("+%s: rpwm=0x%02X cpwm=0x%02X\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1695 	rtw_cpwm_chk_fail_debug(padapter);
1696 #endif
1697 
1698 	if (rtw_read8(padapter, 0x100) != 0xEA) {
1699 #if 1
1700 		struct reportpwrstate_parm report;
1701 
1702 		report.state = PS_STATE_S2;
1703 		RTW_INFO("\n%s: FW already leave 32K!\n\n", __func__);
1704 		cpwm_int_hdl(padapter, &report);
1705 #else
1706 		RTW_INFO("\n%s: FW already leave 32K!\n\n", __func__);
1707 		cpwm_event_callback(&pwrpriv->cpwm_event);
1708 #endif
1709 		return;
1710 	}
1711 
1712 	_enter_pwrlock(&pwrpriv->lock);
1713 
1714 	if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2)) {
1715 		RTW_INFO("%s: cpwm=%d, nothing to do!\n", __func__, pwrpriv->cpwm);
1716 		goto exit;
1717 	}
1718 	pwrpriv->brpwmtimeout = _TRUE;
1719 	rtw_set_rpwm(padapter, pwrpriv->rpwm);
1720 	pwrpriv->brpwmtimeout = _FALSE;
1721 
1722 exit:
1723 	_exit_pwrlock(&pwrpriv->lock);
1724 }
1725 
1726 /*
1727  * This function is a timer handler, can't do any IO in it.
1728  */
pwr_rpwm_timeout_handler(void * FunctionContext)1729 static void pwr_rpwm_timeout_handler(void *FunctionContext)
1730 {
1731 	PADAPTER padapter;
1732 	struct pwrctrl_priv *pwrpriv;
1733 
1734 
1735 	padapter = (PADAPTER)FunctionContext;
1736 	pwrpriv = adapter_to_pwrctl(padapter);
1737 	if (!padapter)
1738 		return;
1739 
1740 	if (RTW_CANNOT_RUN(padapter))
1741 		return;
1742 
1743 	RTW_INFO("+%s: rpwm=0x%02X cpwm=0x%02X\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
1744 
1745 	if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2)) {
1746 		RTW_INFO("+%s: cpwm=%d, nothing to do!\n", __func__, pwrpriv->cpwm);
1747 		return;
1748 	}
1749 
1750 	_set_workitem(&pwrpriv->rpwmtimeoutwi);
1751 }
1752 #endif /* CONFIG_LPS_RPWM_TIMER */
1753 
register_task_alive(struct pwrctrl_priv * pwrctrl,u32 tag)1754 __inline static void register_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
1755 {
1756 	pwrctrl->alives |= tag;
1757 }
1758 
unregister_task_alive(struct pwrctrl_priv * pwrctrl,u32 tag)1759 __inline static void unregister_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
1760 {
1761 	pwrctrl->alives &= ~tag;
1762 }
1763 
1764 
1765 /*
1766  * Description:
1767  *	Check if the fw_pwrstate is okay for I/O.
1768  *	If not (cpwm is less than S2), then the sub-routine
1769  *	will raise the cpwm to be greater than or equal to S2.
1770  *
1771  *	Calling Context: Passive
1772  *
1773  *	Constraint:
1774  *		1. this function will request pwrctrl->lock
1775  *
1776  * Return Value:
1777  *	_SUCCESS	hardware is ready for I/O
1778  *	_FAIL		can't I/O right now
1779  */
rtw_register_task_alive(PADAPTER padapter,u32 task)1780 s32 rtw_register_task_alive(PADAPTER padapter, u32 task)
1781 {
1782 	s32 res;
1783 	struct pwrctrl_priv *pwrctrl;
1784 	u8 pslv;
1785 
1786 
1787 	res = _SUCCESS;
1788 	pwrctrl = adapter_to_pwrctl(padapter);
1789 	pslv = PS_STATE_S2;
1790 
1791 	_enter_pwrlock(&pwrctrl->lock);
1792 
1793 	register_task_alive(pwrctrl, task);
1794 
1795 	if (pwrctrl->bFwCurrentInPSMode == _TRUE) {
1796 
1797 		if (pwrctrl->cpwm < pslv) {
1798 			if (pwrctrl->cpwm < PS_STATE_S2)
1799 				res = _FAIL;
1800 			if (pwrctrl->rpwm < pslv)
1801 				rtw_set_rpwm(padapter, pslv);
1802 		}
1803 	}
1804 
1805 	_exit_pwrlock(&pwrctrl->lock);
1806 
1807 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1808 	if (_FAIL == res) {
1809 		if (pwrctrl->cpwm >= PS_STATE_S2)
1810 			res = _SUCCESS;
1811 	}
1812 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1813 
1814 
1815 	return res;
1816 }
1817 
1818 /*
1819  * Description:
1820  *	If task is done, call this func. to power down firmware again.
1821  *
1822  *	Constraint:
1823  *		1. this function will request pwrctrl->lock
1824  *
1825  * Return Value:
1826  *	none
1827  */
rtw_unregister_task_alive(PADAPTER padapter,u32 task)1828 void rtw_unregister_task_alive(PADAPTER padapter, u32 task)
1829 {
1830 	struct pwrctrl_priv *pwrctrl;
1831 	u8 pslv;
1832 
1833 
1834 	pwrctrl = adapter_to_pwrctl(padapter);
1835 	pslv = PS_STATE_S0;
1836 
1837 #ifdef CONFIG_BT_COEXIST
1838 	if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
1839 	    && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)) {
1840 		u8 val8;
1841 
1842 		val8 = rtw_btcoex_LpsVal(padapter);
1843 		if (val8 & BIT(4))
1844 			pslv = PS_STATE_S2;
1845 
1846 	}
1847 #endif /* CONFIG_BT_COEXIST */
1848 
1849 	_enter_pwrlock(&pwrctrl->lock);
1850 
1851 	unregister_task_alive(pwrctrl, task);
1852 
1853 	if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
1854 	    && (pwrctrl->bFwCurrentInPSMode == _TRUE)) {
1855 
1856 		if (pwrctrl->cpwm > pslv) {
1857 			if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
1858 				rtw_set_rpwm(padapter, pslv);
1859 		}
1860 	}
1861 
1862 	_exit_pwrlock(&pwrctrl->lock);
1863 
1864 }
1865 
1866 /*
1867  * Caller: rtw_xmit_thread
1868  *
1869  * Check if the fw_pwrstate is okay for xmit.
1870  * If not (cpwm is less than S3), then the sub-routine
1871  * will raise the cpwm to be greater than or equal to S3.
1872  *
1873  * Calling Context: Passive
1874  *
1875  * Return Value:
1876  *	 _SUCCESS	rtw_xmit_thread can write fifo/txcmd afterwards.
1877  *	 _FAIL		rtw_xmit_thread can not do anything.
1878  */
rtw_register_tx_alive(PADAPTER padapter)1879 s32 rtw_register_tx_alive(PADAPTER padapter)
1880 {
1881 	s32 res;
1882 	struct pwrctrl_priv *pwrctrl;
1883 	u8 pslv;
1884 
1885 
1886 	res = _SUCCESS;
1887 	pwrctrl = adapter_to_pwrctl(padapter);
1888 	pslv = PS_STATE_S2;
1889 
1890 	_enter_pwrlock(&pwrctrl->lock);
1891 
1892 	register_task_alive(pwrctrl, XMIT_ALIVE);
1893 
1894 	if (pwrctrl->bFwCurrentInPSMode == _TRUE) {
1895 
1896 		if (pwrctrl->cpwm < pslv) {
1897 			if (pwrctrl->cpwm < PS_STATE_S2)
1898 				res = _FAIL;
1899 			if (pwrctrl->rpwm < pslv)
1900 				rtw_set_rpwm(padapter, pslv);
1901 		}
1902 	}
1903 
1904 	_exit_pwrlock(&pwrctrl->lock);
1905 
1906 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1907 	if (_FAIL == res) {
1908 		if (pwrctrl->cpwm >= PS_STATE_S2)
1909 			res = _SUCCESS;
1910 	}
1911 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1912 
1913 
1914 	return res;
1915 }
1916 
1917 /*
1918  * Caller: rtw_cmd_thread
1919  *
1920  * Check if the fw_pwrstate is okay for issuing cmd.
1921  * If not (cpwm should be is less than S2), then the sub-routine
1922  * will raise the cpwm to be greater than or equal to S2.
1923  *
1924  * Calling Context: Passive
1925  *
1926  * Return Value:
1927  *	_SUCCESS	rtw_cmd_thread can issue cmds to firmware afterwards.
1928  *	_FAIL		rtw_cmd_thread can not do anything.
1929  */
rtw_register_cmd_alive(PADAPTER padapter)1930 s32 rtw_register_cmd_alive(PADAPTER padapter)
1931 {
1932 	s32 res;
1933 	struct pwrctrl_priv *pwrctrl;
1934 	u8 pslv;
1935 
1936 
1937 	res = _SUCCESS;
1938 	pwrctrl = adapter_to_pwrctl(padapter);
1939 	pslv = PS_STATE_S2;
1940 
1941 	_enter_pwrlock(&pwrctrl->lock);
1942 
1943 	register_task_alive(pwrctrl, CMD_ALIVE);
1944 
1945 	if (pwrctrl->bFwCurrentInPSMode == _TRUE) {
1946 
1947 		if (pwrctrl->cpwm < pslv) {
1948 			if (pwrctrl->cpwm < PS_STATE_S2)
1949 				res = _FAIL;
1950 			if (pwrctrl->rpwm < pslv)
1951 				rtw_set_rpwm(padapter, pslv);
1952 		}
1953 	}
1954 
1955 	_exit_pwrlock(&pwrctrl->lock);
1956 
1957 #ifdef CONFIG_DETECT_CPWM_BY_POLLING
1958 	if (_FAIL == res) {
1959 		if (pwrctrl->cpwm >= PS_STATE_S2)
1960 			res = _SUCCESS;
1961 	}
1962 #endif /* CONFIG_DETECT_CPWM_BY_POLLING */
1963 
1964 
1965 	return res;
1966 }
1967 
1968 /*
1969  * Caller: rx_isr
1970  *
1971  * Calling Context: Dispatch/ISR
1972  *
1973  * Return Value:
1974  *	_SUCCESS
1975  *	_FAIL
1976  */
rtw_register_rx_alive(PADAPTER padapter)1977 s32 rtw_register_rx_alive(PADAPTER padapter)
1978 {
1979 	struct pwrctrl_priv *pwrctrl;
1980 
1981 
1982 	pwrctrl = adapter_to_pwrctl(padapter);
1983 
1984 	_enter_pwrlock(&pwrctrl->lock);
1985 
1986 	register_task_alive(pwrctrl, RECV_ALIVE);
1987 
1988 	_exit_pwrlock(&pwrctrl->lock);
1989 
1990 
1991 	return _SUCCESS;
1992 }
1993 
1994 /*
1995  * Caller: evt_isr or evt_thread
1996  *
1997  * Calling Context: Dispatch/ISR or Passive
1998  *
1999  * Return Value:
2000  *	_SUCCESS
2001  *	_FAIL
2002  */
rtw_register_evt_alive(PADAPTER padapter)2003 s32 rtw_register_evt_alive(PADAPTER padapter)
2004 {
2005 	struct pwrctrl_priv *pwrctrl;
2006 
2007 
2008 	pwrctrl = adapter_to_pwrctl(padapter);
2009 
2010 	_enter_pwrlock(&pwrctrl->lock);
2011 
2012 	register_task_alive(pwrctrl, EVT_ALIVE);
2013 
2014 	_exit_pwrlock(&pwrctrl->lock);
2015 
2016 
2017 	return _SUCCESS;
2018 }
2019 
2020 /*
2021  * Caller: ISR
2022  *
2023  * If ISR's txdone,
2024  * No more pkts for TX,
2025  * Then driver shall call this fun. to power down firmware again.
2026  */
rtw_unregister_tx_alive(PADAPTER padapter)2027 void rtw_unregister_tx_alive(PADAPTER padapter)
2028 {
2029 	struct pwrctrl_priv *pwrctrl;
2030 	_adapter *iface;
2031 	struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
2032 	u8 pslv, i;
2033 
2034 
2035 	pwrctrl = adapter_to_pwrctl(padapter);
2036 	pslv = PS_STATE_S0;
2037 
2038 #ifdef CONFIG_BT_COEXIST
2039 	if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
2040 	    && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)) {
2041 		u8 val8;
2042 
2043 		val8 = rtw_btcoex_LpsVal(padapter);
2044 		if (val8 & BIT(4))
2045 			pslv = PS_STATE_S2;
2046 
2047 	}
2048 #endif /* CONFIG_BT_COEXIST */
2049 
2050 #ifdef CONFIG_P2P_PS
2051 	for (i = 0; i < dvobj->iface_nums; i++) {
2052 		iface = dvobj->padapters[i];
2053 		if ((iface) && rtw_is_adapter_up(iface)) {
2054 			if (iface->wdinfo.p2p_ps_mode > P2P_PS_NONE) {
2055 				pslv = PS_STATE_S2;
2056 				break;
2057 			}
2058 		}
2059 	}
2060 #endif
2061 	_enter_pwrlock(&pwrctrl->lock);
2062 
2063 	unregister_task_alive(pwrctrl, XMIT_ALIVE);
2064 
2065 	if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
2066 	    && (pwrctrl->bFwCurrentInPSMode == _TRUE)) {
2067 
2068 		if (pwrctrl->cpwm > pslv) {
2069 			if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
2070 				rtw_set_rpwm(padapter, pslv);
2071 		}
2072 	}
2073 
2074 	_exit_pwrlock(&pwrctrl->lock);
2075 
2076 }
2077 
2078 /*
2079  * Caller: ISR
2080  *
2081  * If all commands have been done,
2082  * and no more command to do,
2083  * then driver shall call this fun. to power down firmware again.
2084  */
rtw_unregister_cmd_alive(PADAPTER padapter)2085 void rtw_unregister_cmd_alive(PADAPTER padapter)
2086 {
2087 	_adapter *iface;
2088 	struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
2089 	struct pwrctrl_priv *pwrctrl;
2090 	u8 pslv, i;
2091 
2092 
2093 	pwrctrl = adapter_to_pwrctl(padapter);
2094 	pslv = PS_STATE_S0;
2095 
2096 #ifdef CONFIG_BT_COEXIST
2097 	if ((rtw_btcoex_IsBtDisabled(padapter) == _FALSE)
2098 	    && (rtw_btcoex_IsBtControlLps(padapter) == _TRUE)) {
2099 		u8 val8;
2100 
2101 		val8 = rtw_btcoex_LpsVal(padapter);
2102 		if (val8 & BIT(4))
2103 			pslv = PS_STATE_S2;
2104 
2105 	}
2106 #endif /* CONFIG_BT_COEXIST */
2107 
2108 #ifdef CONFIG_P2P_PS
2109 	for (i = 0; i < dvobj->iface_nums; i++) {
2110 		iface = dvobj->padapters[i];
2111 		if ((iface) && rtw_is_adapter_up(iface)) {
2112 			if (iface->wdinfo.p2p_ps_mode > P2P_PS_NONE) {
2113 				pslv = PS_STATE_S2;
2114 				break;
2115 			}
2116 		}
2117 	}
2118 #endif
2119 
2120 	_enter_pwrlock(&pwrctrl->lock);
2121 
2122 	unregister_task_alive(pwrctrl, CMD_ALIVE);
2123 
2124 	if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE)
2125 	    && (pwrctrl->bFwCurrentInPSMode == _TRUE)) {
2126 
2127 		if (pwrctrl->cpwm > pslv) {
2128 			if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
2129 				rtw_set_rpwm(padapter, pslv);
2130 		}
2131 	}
2132 
2133 	_exit_pwrlock(&pwrctrl->lock);
2134 
2135 }
2136 
2137 /*
2138  * Caller: ISR
2139  */
rtw_unregister_rx_alive(PADAPTER padapter)2140 void rtw_unregister_rx_alive(PADAPTER padapter)
2141 {
2142 	struct pwrctrl_priv *pwrctrl;
2143 
2144 
2145 	pwrctrl = adapter_to_pwrctl(padapter);
2146 
2147 	_enter_pwrlock(&pwrctrl->lock);
2148 
2149 	unregister_task_alive(pwrctrl, RECV_ALIVE);
2150 
2151 
2152 	_exit_pwrlock(&pwrctrl->lock);
2153 
2154 }
2155 
rtw_unregister_evt_alive(PADAPTER padapter)2156 void rtw_unregister_evt_alive(PADAPTER padapter)
2157 {
2158 	struct pwrctrl_priv *pwrctrl;
2159 
2160 
2161 	pwrctrl = adapter_to_pwrctl(padapter);
2162 
2163 	unregister_task_alive(pwrctrl, EVT_ALIVE);
2164 
2165 
2166 	_exit_pwrlock(&pwrctrl->lock);
2167 
2168 }
2169 #endif	/* CONFIG_LPS_LCLK */
2170 
2171 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2172 	static void resume_workitem_callback(struct work_struct *work);
2173 #endif /* CONFIG_RESUME_IN_WORKQUEUE */
2174 
rtw_init_pwrctrl_priv(PADAPTER padapter)2175 void rtw_init_pwrctrl_priv(PADAPTER padapter)
2176 {
2177 #ifdef CONFIG_LPS_1T1R
2178 #define LPS_1T1R_FMT ", LPS_1T1R=%d"
2179 #define LPS_1T1R_ARG , pwrctrlpriv->lps_1t1r
2180 #else
2181 #define LPS_1T1R_FMT ""
2182 #define LPS_1T1R_ARG
2183 #endif
2184 
2185 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2186 #ifdef CONFIG_WOWLAN
2187 	struct registry_priv  *registry_par = &padapter->registrypriv;
2188 #endif
2189 #ifdef CONFIG_GPIO_WAKEUP
2190 	PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter);
2191 #endif
2192 
2193 #if defined(CONFIG_CONCURRENT_MODE)
2194 	if (!is_primary_adapter(padapter))
2195 		return;
2196 #endif
2197 
2198 	_init_pwrlock(&pwrctrlpriv->lock);
2199 	_init_pwrlock(&pwrctrlpriv->check_32k_lock);
2200 	pwrctrlpriv->rf_pwrstate = rf_on;
2201 	pwrctrlpriv->ips_enter_cnts = 0;
2202 	pwrctrlpriv->ips_leave_cnts = 0;
2203 	pwrctrlpriv->lps_enter_cnts = 0;
2204 	pwrctrlpriv->lps_leave_cnts = 0;
2205 	pwrctrlpriv->bips_processing = _FALSE;
2206 #ifdef CONFIG_LPS_CHK_BY_TP
2207 	pwrctrlpriv->lps_chk_by_tp = padapter->registrypriv.lps_chk_by_tp;
2208 	pwrctrlpriv->lps_tx_tp_th = LPS_TX_TP_TH;
2209 	pwrctrlpriv->lps_rx_tp_th = LPS_RX_TP_TH;
2210 	pwrctrlpriv->lps_bi_tp_th = LPS_BI_TP_TH;
2211 	pwrctrlpriv->lps_chk_cnt = pwrctrlpriv->lps_chk_cnt_th = LPS_TP_CHK_CNT;
2212 	pwrctrlpriv->lps_tx_pkts = LPS_CHK_PKTS_TX;
2213 	pwrctrlpriv->lps_rx_pkts = LPS_CHK_PKTS_RX;
2214 #endif
2215 
2216 	pwrctrlpriv->ips_mode = padapter->registrypriv.ips_mode;
2217 	pwrctrlpriv->ips_mode_req = padapter->registrypriv.ips_mode;
2218 	pwrctrlpriv->ips_deny_time = rtw_get_current_time();
2219 	pwrctrlpriv->lps_level = padapter->registrypriv.lps_level;
2220 #ifdef CONFIG_LPS_1T1R
2221 	pwrctrlpriv->lps_1t1r = padapter->registrypriv.lps_1t1r;
2222 #endif
2223 
2224 	pwrctrlpriv->pwr_state_check_interval = RTW_PWR_STATE_CHK_INTERVAL;
2225 	pwrctrlpriv->pwr_state_check_cnts = 0;
2226 	pwrctrlpriv->bInSuspend = _FALSE;
2227 	pwrctrlpriv->bkeepfwalive = _FALSE;
2228 	pwrctrlpriv->LpsIdleCount = 0;
2229 
2230 	/* pwrctrlpriv->FWCtrlPSMode =padapter->registrypriv.power_mgnt; */ /* PS_MODE_MIN; */
2231 	if (padapter->registrypriv.mp_mode == 1)
2232 		pwrctrlpriv->power_mgnt = PS_MODE_ACTIVE ;
2233 	else
2234 		pwrctrlpriv->power_mgnt = padapter->registrypriv.power_mgnt; /* PS_MODE_MIN; */
2235 	pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt) ? _TRUE : _FALSE;
2236 
2237 	pwrctrlpriv->bFwCurrentInPSMode = _FALSE;
2238 	pwrctrlpriv->lps_deny_time = rtw_get_current_time();
2239 
2240 	pwrctrlpriv->rpwm = 0;
2241 	pwrctrlpriv->cpwm = PS_STATE_S4;
2242 
2243 	pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
2244 	pwrctrlpriv->smart_ps = padapter->registrypriv.smart_ps;
2245 	pwrctrlpriv->bcn_ant_mode = 0;
2246 	pwrctrlpriv->dtim = 0;
2247 
2248 	pwrctrlpriv->tog = 0x80;
2249 	pwrctrlpriv->rpwm_retry = 0;
2250 
2251 	RTW_INFO("%s: IPS_mode=%d, LPS_mode=%d, LPS_level=%d"LPS_1T1R_FMT"\n",
2252 		__func__, pwrctrlpriv->ips_mode, pwrctrlpriv->power_mgnt, pwrctrlpriv->lps_level
2253 		LPS_1T1R_ARG
2254 	);
2255 
2256 #ifdef CONFIG_LPS_LCLK
2257 	rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&pwrctrlpriv->rpwm));
2258 
2259 	_init_workitem(&pwrctrlpriv->cpwm_event, cpwm_event_callback, NULL);
2260 
2261 	_init_workitem(&pwrctrlpriv->dma_event, dma_event_callback, NULL);
2262 
2263 #ifdef CONFIG_LPS_RPWM_TIMER
2264 	pwrctrlpriv->brpwmtimeout = _FALSE;
2265 	_init_workitem(&pwrctrlpriv->rpwmtimeoutwi, rpwmtimeout_workitem_callback, NULL);
2266 	rtw_init_timer(&pwrctrlpriv->pwr_rpwm_timer, padapter, pwr_rpwm_timeout_handler, padapter);
2267 #endif /* CONFIG_LPS_RPWM_TIMER */
2268 #endif /* CONFIG_LPS_LCLK */
2269 
2270 #ifdef CONFIG_LPS_PG
2271 	pwrctrlpriv->lpspg_info.name = "LPSPG_INFO";
2272 	#ifdef CONFIG_RTL8822C
2273 	pwrctrlpriv->lpspg_dpk_info.name = "LPSPG_DPK_INFO";
2274 	pwrctrlpriv->lpspg_iqk_info.name = "LPSPG_IQK_INFO";
2275 	#endif
2276 #endif
2277 
2278 	rtw_init_timer(&pwrctrlpriv->pwr_state_check_timer, padapter, pwr_state_check_handler, padapter);
2279 
2280 	pwrctrlpriv->wowlan_mode = _FALSE;
2281 	pwrctrlpriv->wowlan_ap_mode = _FALSE;
2282 	pwrctrlpriv->wowlan_p2p_mode = _FALSE;
2283 	pwrctrlpriv->wowlan_in_resume = _FALSE;
2284 	pwrctrlpriv->wowlan_last_wake_reason = 0;
2285 
2286 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2287 	_init_workitem(&pwrctrlpriv->resume_work, resume_workitem_callback, NULL);
2288 	pwrctrlpriv->rtw_workqueue = create_singlethread_workqueue("rtw_workqueue");
2289 #endif /* CONFIG_RESUME_IN_WORKQUEUE */
2290 
2291 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
2292 	pwrctrlpriv->early_suspend.suspend = NULL;
2293 	rtw_register_early_suspend(pwrctrlpriv);
2294 #endif /* CONFIG_HAS_EARLYSUSPEND || CONFIG_ANDROID_POWER */
2295 
2296 #ifdef CONFIG_GPIO_WAKEUP
2297 	pwrctrlpriv->wowlan_gpio_index = WAKEUP_GPIO_IDX;
2298 	/* set output low state in initial */
2299 	pwrctrlpriv->wowlan_gpio_output_state = GPIO_OUTPUT_LOW;
2300 	/*default low active*/
2301 	pwrctrlpriv->is_high_active = HIGH_ACTIVE_DEV2HST;
2302 	pwrctrlpriv->hst2dev_high_active = HIGH_ACTIVE_HST2DEV;
2303 
2304 #if (defined(CONFIG_RTL8192F) && defined(CONFIG_USB_HCI) && defined(CONFIG_BT_COEXIST))
2305 	if (pHalData->EEPROMBluetoothCoexist == _TRUE) {
2306 		/* for 8725AU case */
2307 		pwrctrlpriv->wowlan_gpio_index = WAKEUP_GPIO_IDX_8725AU;
2308 		pwrctrlpriv->is_high_active = HIGH_ACTIVE_DEV2HST_8725AU;
2309 	}
2310 #endif
2311 #ifdef CONFIG_RTW_ONE_PIN_GPIO
2312 	rtw_hal_switch_gpio_wl_ctrl(padapter, pwrctrlpriv->wowlan_gpio_index, _TRUE);
2313 	rtw_hal_set_input_gpio(padapter, pwrctrlpriv->wowlan_gpio_index);
2314 #else
2315 	#ifdef CONFIG_WAKEUP_GPIO_INPUT_MODE
2316 	if (pwrctrlpriv->is_high_active == 0)
2317 		rtw_hal_set_input_gpio(padapter, pwrctrlpriv->wowlan_gpio_index);
2318 	else
2319 		rtw_hal_set_output_gpio(padapter, pwrctrlpriv->wowlan_gpio_index,
2320 			GPIO_OUTPUT_LOW);
2321 	#else
2322 	rtw_hal_set_output_gpio(padapter, pwrctrlpriv->wowlan_gpio_index
2323 		, pwrctrlpriv->wowlan_gpio_output_state);
2324 	rtw_hal_switch_gpio_wl_ctrl(padapter, pwrctrlpriv->wowlan_gpio_index, _TRUE);
2325 	RTW_INFO("%s: set GPIO_%d to OUTPUT %s state in initial and %s_ACTIVE.\n",
2326 		 __func__, pwrctrlpriv->wowlan_gpio_index,
2327 		 pwrctrlpriv->wowlan_gpio_output_state ? "HIGH" : "LOW",
2328 		 pwrctrlpriv->is_high_active ? "HIGI" : "LOW");
2329 	#endif /*CONFIG_WAKEUP_GPIO_INPUT_MODE*/
2330 #endif /* CONFIG_RTW_ONE_PIN_GPIO */
2331 #endif /* CONFIG_GPIO_WAKEUP */
2332 
2333 #ifdef CONFIG_WOWLAN
2334 #ifdef CONFIG_LPS_1T1R
2335 #define WOW_LPS_1T1R_FMT ", WOW_LPS_1T1R=%d"
2336 #define WOW_LPS_1T1R_ARG , pwrctrlpriv->wowlan_lps_1t1r
2337 #else
2338 #define WOW_LPS_1T1R_FMT ""
2339 #define WOW_LPS_1T1R_ARG
2340 #endif
2341 
2342 	pwrctrlpriv->wowlan_power_mgmt = padapter->registrypriv.wow_power_mgnt;
2343 	pwrctrlpriv->wowlan_lps_level = padapter->registrypriv.wow_lps_level;
2344 #ifdef CONFIG_LPS_1T1R
2345 	pwrctrlpriv->wowlan_lps_1t1r = padapter->registrypriv.wow_lps_1t1r;
2346 #endif
2347 
2348 	RTW_INFO("%s: WOW_LPS_mode=%d, WOW_LPS_level=%d"WOW_LPS_1T1R_FMT"\n",
2349 		__func__, pwrctrlpriv->wowlan_power_mgmt, pwrctrlpriv->wowlan_lps_level
2350 		WOW_LPS_1T1R_ARG
2351 	);
2352 
2353 	if (registry_par->wakeup_event & BIT(1))
2354 		pwrctrlpriv->default_patterns_en = _TRUE;
2355 	else
2356 		pwrctrlpriv->default_patterns_en = _FALSE;
2357 
2358 	rtw_wow_pattern_sw_reset(padapter);
2359 #ifdef CONFIG_PNO_SUPPORT
2360 	pwrctrlpriv->pno_inited = _FALSE;
2361 	pwrctrlpriv->pnlo_info = NULL;
2362 	pwrctrlpriv->pscan_info = NULL;
2363 	pwrctrlpriv->pno_ssid_list = NULL;
2364 #endif /* CONFIG_PNO_SUPPORT */
2365 #ifdef CONFIG_WOW_PATTERN_HW_CAM
2366 	_rtw_mutex_init(&pwrctrlpriv->wowlan_pattern_cam_mutex);
2367 #endif
2368 	pwrctrlpriv->wowlan_aoac_rpt_loc = 0;
2369 #endif /* CONFIG_WOWLAN */
2370 
2371 #ifdef CONFIG_LPS_POFF
2372 	rtw_hal_set_hwreg(padapter, HW_VAR_LPS_POFF_INIT, 0);
2373 #endif
2374 
2375 #ifdef CONFIG_LPS_ACK
2376 	_rtw_mutex_init(&pwrctrlpriv->lps_ack_mutex);
2377 	pwrctrlpriv->lps_ack_status = -1;
2378 #endif /* CONFIG_LPS_ACK */
2379 }
2380 
2381 
rtw_free_pwrctrl_priv(PADAPTER adapter)2382 void rtw_free_pwrctrl_priv(PADAPTER adapter)
2383 {
2384 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(adapter);
2385 
2386 #if defined(CONFIG_CONCURRENT_MODE)
2387 	if (!is_primary_adapter(adapter))
2388 		return;
2389 #endif
2390 
2391 
2392 	/* _rtw_memset((unsigned char *)pwrctrlpriv, 0, sizeof(struct pwrctrl_priv)); */
2393 
2394 
2395 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2396 	if (pwrctrlpriv->rtw_workqueue) {
2397 		flush_workqueue(pwrctrlpriv->rtw_workqueue);
2398 		destroy_workqueue(pwrctrlpriv->rtw_workqueue);
2399 	}
2400 #endif
2401 
2402 #ifdef CONFIG_LPS_POFF
2403 	rtw_hal_set_hwreg(adapter, HW_VAR_LPS_POFF_DEINIT, 0);
2404 #endif
2405 
2406 #ifdef CONFIG_LPS_LCLK
2407 	_cancel_workitem_sync(&pwrctrlpriv->cpwm_event);
2408 	_cancel_workitem_sync(&pwrctrlpriv->dma_event);
2409 	#ifdef CONFIG_LPS_RPWM_TIMER
2410 	_cancel_workitem_sync(&pwrctrlpriv->rpwmtimeoutwi);
2411 	#endif
2412 #endif /* CONFIG_LPS_LCLK */
2413 
2414 #ifdef CONFIG_LPS_PG
2415 	rsvd_page_cache_free(&pwrctrlpriv->lpspg_info);
2416 	#ifdef CONFIG_RTL8822C
2417 	rsvd_page_cache_free(&pwrctrlpriv->lpspg_dpk_info);
2418 	rsvd_page_cache_free(&pwrctrlpriv->lpspg_iqk_info);
2419 	#endif
2420 #endif
2421 
2422 #ifdef CONFIG_WOWLAN
2423 #ifdef CONFIG_PNO_SUPPORT
2424 	if (pwrctrlpriv->pnlo_info != NULL)
2425 		printk("****** pnlo_info memory leak********\n");
2426 
2427 	if (pwrctrlpriv->pscan_info != NULL)
2428 		printk("****** pscan_info memory leak********\n");
2429 
2430 	if (pwrctrlpriv->pno_ssid_list != NULL)
2431 		printk("****** pno_ssid_list memory leak********\n");
2432 #endif
2433 #ifdef CONFIG_WOW_PATTERN_HW_CAM
2434 	_rtw_mutex_free(&pwrctrlpriv->wowlan_pattern_cam_mutex);
2435 #endif
2436 
2437 #endif /* CONFIG_WOWLAN */
2438 
2439 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
2440 	rtw_unregister_early_suspend(pwrctrlpriv);
2441 #endif /* CONFIG_HAS_EARLYSUSPEND || CONFIG_ANDROID_POWER */
2442 
2443 	_free_pwrlock(&pwrctrlpriv->lock);
2444 	_free_pwrlock(&pwrctrlpriv->check_32k_lock);
2445 
2446 #ifdef CONFIG_LPS_ACK
2447 	_rtw_mutex_free(&pwrctrlpriv->lps_ack_mutex);
2448 #endif /* CONFIG_LPS_ACK */
2449 }
2450 
2451 #ifdef CONFIG_RESUME_IN_WORKQUEUE
2452 extern int rtw_resume_process(_adapter *padapter);
2453 
resume_workitem_callback(struct work_struct * work)2454 static void resume_workitem_callback(struct work_struct *work)
2455 {
2456 	struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, resume_work);
2457 	struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2458 	_adapter *adapter = dvobj_get_primary_adapter(dvobj);
2459 
2460 	RTW_INFO("%s\n", __FUNCTION__);
2461 
2462 	rtw_resume_process(adapter);
2463 
2464 	rtw_resume_unlock_suspend();
2465 }
2466 
rtw_resume_in_workqueue(struct pwrctrl_priv * pwrpriv)2467 void rtw_resume_in_workqueue(struct pwrctrl_priv *pwrpriv)
2468 {
2469 	/* accquire system's suspend lock preventing from falliing asleep while resume in workqueue */
2470 	/* rtw_lock_suspend(); */
2471 
2472 	rtw_resume_lock_suspend();
2473 
2474 #if 1
2475 	queue_work(pwrpriv->rtw_workqueue, &pwrpriv->resume_work);
2476 #else
2477 	_set_workitem(&pwrpriv->resume_work);
2478 #endif
2479 }
2480 #endif /* CONFIG_RESUME_IN_WORKQUEUE */
2481 
2482 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
rtw_is_earlysuspend_registered(struct pwrctrl_priv * pwrpriv)2483 inline bool rtw_is_earlysuspend_registered(struct pwrctrl_priv *pwrpriv)
2484 {
2485 	return (pwrpriv->early_suspend.suspend) ? _TRUE : _FALSE;
2486 }
2487 
rtw_is_do_late_resume(struct pwrctrl_priv * pwrpriv)2488 inline bool rtw_is_do_late_resume(struct pwrctrl_priv *pwrpriv)
2489 {
2490 	return (pwrpriv->do_late_resume) ? _TRUE : _FALSE;
2491 }
2492 
rtw_set_do_late_resume(struct pwrctrl_priv * pwrpriv,bool enable)2493 inline void rtw_set_do_late_resume(struct pwrctrl_priv *pwrpriv, bool enable)
2494 {
2495 	pwrpriv->do_late_resume = enable;
2496 }
2497 #endif
2498 
2499 #ifdef CONFIG_HAS_EARLYSUSPEND
2500 extern int rtw_resume_process(_adapter *padapter);
rtw_early_suspend(struct early_suspend * h)2501 static void rtw_early_suspend(struct early_suspend *h)
2502 {
2503 	struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2504 	RTW_INFO("%s\n", __FUNCTION__);
2505 
2506 	rtw_set_do_late_resume(pwrpriv, _FALSE);
2507 }
2508 
rtw_late_resume(struct early_suspend * h)2509 static void rtw_late_resume(struct early_suspend *h)
2510 {
2511 	struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2512 	struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2513 	_adapter *adapter = dvobj_get_primary_adapter(dvobj);
2514 
2515 	RTW_INFO("%s\n", __FUNCTION__);
2516 
2517 	if (pwrpriv->do_late_resume) {
2518 		rtw_set_do_late_resume(pwrpriv, _FALSE);
2519 		rtw_resume_process(adapter);
2520 	}
2521 }
2522 
rtw_register_early_suspend(struct pwrctrl_priv * pwrpriv)2523 void rtw_register_early_suspend(struct pwrctrl_priv *pwrpriv)
2524 {
2525 	RTW_INFO("%s\n", __FUNCTION__);
2526 
2527 	/* jeff: set the early suspend level before blank screen, so we wll do late resume after scree is lit */
2528 	pwrpriv->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
2529 	pwrpriv->early_suspend.suspend = rtw_early_suspend;
2530 	pwrpriv->early_suspend.resume = rtw_late_resume;
2531 	register_early_suspend(&pwrpriv->early_suspend);
2532 
2533 
2534 }
2535 
rtw_unregister_early_suspend(struct pwrctrl_priv * pwrpriv)2536 void rtw_unregister_early_suspend(struct pwrctrl_priv *pwrpriv)
2537 {
2538 	RTW_INFO("%s\n", __FUNCTION__);
2539 
2540 	rtw_set_do_late_resume(pwrpriv, _FALSE);
2541 
2542 	if (pwrpriv->early_suspend.suspend)
2543 		unregister_early_suspend(&pwrpriv->early_suspend);
2544 
2545 	pwrpriv->early_suspend.suspend = NULL;
2546 	pwrpriv->early_suspend.resume = NULL;
2547 }
2548 #endif /* CONFIG_HAS_EARLYSUSPEND */
2549 
2550 #ifdef CONFIG_ANDROID_POWER
2551 #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2552 	extern int rtw_resume_process(PADAPTER padapter);
2553 #endif
rtw_early_suspend(android_early_suspend_t * h)2554 static void rtw_early_suspend(android_early_suspend_t *h)
2555 {
2556 	struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2557 	RTW_INFO("%s\n", __FUNCTION__);
2558 
2559 	rtw_set_do_late_resume(pwrpriv, _FALSE);
2560 }
2561 
rtw_late_resume(android_early_suspend_t * h)2562 static void rtw_late_resume(android_early_suspend_t *h)
2563 {
2564 	struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
2565 	struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
2566 	_adapter *adapter = dvobj_get_primary_adapter(dvobj);
2567 
2568 	RTW_INFO("%s\n", __FUNCTION__);
2569 	if (pwrpriv->do_late_resume) {
2570 #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2571 		rtw_set_do_late_resume(pwrpriv, _FALSE);
2572 		rtw_resume_process(adapter);
2573 #endif
2574 	}
2575 }
2576 
rtw_register_early_suspend(struct pwrctrl_priv * pwrpriv)2577 void rtw_register_early_suspend(struct pwrctrl_priv *pwrpriv)
2578 {
2579 	RTW_INFO("%s\n", __FUNCTION__);
2580 
2581 	/* jeff: set the early suspend level before blank screen, so we wll do late resume after scree is lit */
2582 	pwrpriv->early_suspend.level = ANDROID_EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
2583 	pwrpriv->early_suspend.suspend = rtw_early_suspend;
2584 	pwrpriv->early_suspend.resume = rtw_late_resume;
2585 	android_register_early_suspend(&pwrpriv->early_suspend);
2586 }
2587 
rtw_unregister_early_suspend(struct pwrctrl_priv * pwrpriv)2588 void rtw_unregister_early_suspend(struct pwrctrl_priv *pwrpriv)
2589 {
2590 	RTW_INFO("%s\n", __FUNCTION__);
2591 
2592 	rtw_set_do_late_resume(pwrpriv, _FALSE);
2593 
2594 	if (pwrpriv->early_suspend.suspend)
2595 		android_unregister_early_suspend(&pwrpriv->early_suspend);
2596 
2597 	pwrpriv->early_suspend.suspend = NULL;
2598 	pwrpriv->early_suspend.resume = NULL;
2599 }
2600 #endif /* CONFIG_ANDROID_POWER */
2601 
rtw_interface_ps_func(_adapter * padapter,HAL_INTF_PS_FUNC efunc_id,u8 * val)2602 u8 rtw_interface_ps_func(_adapter *padapter, HAL_INTF_PS_FUNC efunc_id, u8 *val)
2603 {
2604 	u8 bResult = _TRUE;
2605 	rtw_hal_intf_ps_func(padapter, efunc_id, val);
2606 
2607 	return bResult;
2608 }
2609 
2610 
rtw_set_ips_deny(_adapter * padapter,u32 ms)2611 inline void rtw_set_ips_deny(_adapter *padapter, u32 ms)
2612 {
2613 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
2614 	pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ms);
2615 }
2616 
2617 /*
2618 * rtw_pwr_wakeup - Wake the NIC up from: 1)IPS. 2)USB autosuspend
2619 * @adapter: pointer to _adapter structure
2620 * @ips_deffer_ms: the ms wiil prevent from falling into IPS after wakeup
2621 * Return _SUCCESS or _FAIL
2622 */
2623 
_rtw_pwr_wakeup(_adapter * padapter,u32 ips_deffer_ms,const char * caller)2624 int _rtw_pwr_wakeup(_adapter *padapter, u32 ips_deffer_ms, const char *caller)
2625 {
2626 	struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
2627 	struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
2628 	struct mlme_priv *pmlmepriv;
2629 	int ret = _SUCCESS;
2630 	systime start = rtw_get_current_time();
2631 
2632 	/*RTW_INFO(FUNC_ADPT_FMT "===>\n", FUNC_ADPT_ARG(padapter));*/
2633 	/* for LPS */
2634 	LeaveAllPowerSaveMode(padapter);
2635 
2636 	/* IPS still bound with primary adapter */
2637 	padapter = GET_PRIMARY_ADAPTER(padapter);
2638 	pmlmepriv = &padapter->mlmepriv;
2639 
2640 	if (rtw_time_after(rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms), pwrpriv->ips_deny_time))
2641 		pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms);
2642 
2643 
2644 	if (pwrpriv->ps_processing) {
2645 		RTW_INFO("%s wait ps_processing...\n", __func__);
2646 		while (pwrpriv->ps_processing && rtw_get_passing_time_ms(start) <= 3000)
2647 			rtw_msleep_os(10);
2648 		if (pwrpriv->ps_processing)
2649 			RTW_INFO("%s wait ps_processing timeout\n", __func__);
2650 		else
2651 			RTW_INFO("%s wait ps_processing done\n", __func__);
2652 	}
2653 
2654 #ifdef DBG_CONFIG_ERROR_DETECT
2655 	if (rtw_hal_sreset_inprogress(padapter)) {
2656 		RTW_INFO("%s wait sreset_inprogress...\n", __func__);
2657 		while (rtw_hal_sreset_inprogress(padapter) && rtw_get_passing_time_ms(start) <= 4000)
2658 			rtw_msleep_os(10);
2659 		if (rtw_hal_sreset_inprogress(padapter))
2660 			RTW_INFO("%s wait sreset_inprogress timeout\n", __func__);
2661 		else
2662 			RTW_INFO("%s wait sreset_inprogress done\n", __func__);
2663 	}
2664 #endif
2665 
2666 	if (pwrpriv->bInSuspend) {
2667 		RTW_INFO("%s wait bInSuspend...\n", __func__);
2668 		while (pwrpriv->bInSuspend
2669 		       && ((rtw_get_passing_time_ms(start) <= 3000 && !rtw_is_do_late_resume(pwrpriv))
2670 			|| (rtw_get_passing_time_ms(start) <= 500 && rtw_is_do_late_resume(pwrpriv)))
2671 		      )
2672 			rtw_msleep_os(10);
2673 		if (pwrpriv->bInSuspend)
2674 			RTW_INFO("%s wait bInSuspend timeout\n", __func__);
2675 		else
2676 			RTW_INFO("%s wait bInSuspend done\n", __func__);
2677 	}
2678 
2679 	/* System suspend is not allowed to wakeup */
2680 	if (_TRUE == pwrpriv->bInSuspend) {
2681 		ret = _FAIL;
2682 		goto exit;
2683 	}
2684 	/* I think this should be check in IPS, LPS, autosuspend functions... */
2685 	if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE) {
2686 		ret = _SUCCESS;
2687 		goto exit;
2688 	}
2689 
2690 	if (rf_off == pwrpriv->rf_pwrstate) {
2691 
2692 #ifdef CONFIG_IPS
2693 		RTW_INFO("%s call ips_leave....\n", __FUNCTION__);
2694 		if (_FAIL ==  ips_leave(padapter)) {
2695 			RTW_INFO("======> ips_leave fail.............\n");
2696 			ret = _FAIL;
2697 			goto exit;
2698 		}
2699 #endif
2700 
2701 	}
2702 
2703 	/* TODO: the following checking need to be merged... */
2704 	if (rtw_is_drv_stopped(padapter)
2705 	    || !padapter->bup
2706 	    || !rtw_is_hw_init_completed(padapter)
2707 	   ) {
2708 		RTW_INFO("%s: bDriverStopped=%s, bup=%d, hw_init_completed=%u\n"
2709 			 , caller
2710 			 , rtw_is_drv_stopped(padapter) ? "True" : "False"
2711 			 , padapter->bup
2712 			 , rtw_get_hw_init_completed(padapter));
2713 		ret = _FALSE;
2714 		goto exit;
2715 	}
2716 
2717 exit:
2718 	if (rtw_time_after(rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms), pwrpriv->ips_deny_time))
2719 		pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms);
2720 	/*RTW_INFO(FUNC_ADPT_FMT "<===\n", FUNC_ADPT_ARG(padapter));*/
2721 	return ret;
2722 
2723 }
2724 
rtw_pm_set_lps(_adapter * padapter,u8 mode)2725 int rtw_pm_set_lps(_adapter *padapter, u8 mode)
2726 {
2727 	int	ret = 0;
2728 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2729 
2730 	if (mode < PS_MODE_NUM) {
2731 		if (pwrctrlpriv->power_mgnt != mode) {
2732 			if (PS_MODE_ACTIVE == mode)
2733 				LeaveAllPowerSaveMode(padapter);
2734 			else
2735 				pwrctrlpriv->LpsIdleCount = 2;
2736 			pwrctrlpriv->power_mgnt = mode;
2737 			pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt) ? _TRUE : _FALSE;
2738 		}
2739 	} else
2740 		ret = -EINVAL;
2741 
2742 	return ret;
2743 }
2744 
rtw_pm_set_lps_level(_adapter * padapter,u8 level)2745 int rtw_pm_set_lps_level(_adapter *padapter, u8 level)
2746 {
2747 	int	ret = 0;
2748 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2749 
2750 	if (level < LPS_LEVEL_MAX) {
2751 		if (pwrctrlpriv->lps_level != level) {
2752 			#ifdef CONFIG_LPS
2753 			if (rtw_lps_ctrl_leave_set_level_cmd(padapter, level, RTW_CMDF_WAIT_ACK) != _SUCCESS)
2754 			#endif
2755 				pwrctrlpriv->lps_level = level;
2756 		}
2757 	} else
2758 		ret = -EINVAL;
2759 
2760 	return ret;
2761 }
2762 
2763 #ifdef CONFIG_LPS_1T1R
rtw_pm_set_lps_1t1r(_adapter * padapter,u8 en)2764 int rtw_pm_set_lps_1t1r(_adapter *padapter, u8 en)
2765 {
2766 	int	ret = 0;
2767 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2768 
2769 	en = en ? 1 : 0;
2770 	if (pwrctrlpriv->lps_1t1r != en) {
2771 		if (rtw_lps_ctrl_leave_set_1t1r_cmd(padapter, en, RTW_CMDF_WAIT_ACK) != _SUCCESS)
2772 			pwrctrlpriv->lps_1t1r = en;
2773 	}
2774 
2775 	return ret;
2776 }
2777 #endif
2778 
rtw_set_lps_deny(_adapter * adapter,u32 ms)2779 inline void rtw_set_lps_deny(_adapter *adapter, u32 ms)
2780 {
2781 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter);
2782 	pwrpriv->lps_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ms);
2783 }
2784 
2785 #ifdef CONFIG_WOWLAN
rtw_pm_set_wow_lps(_adapter * padapter,u8 mode)2786 int rtw_pm_set_wow_lps(_adapter *padapter, u8 mode)
2787 {
2788 	int	ret = 0;
2789 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2790 
2791 	if (mode < PS_MODE_NUM) {
2792 		if (pwrctrlpriv->wowlan_power_mgmt != mode)
2793 			pwrctrlpriv->wowlan_power_mgmt = mode;
2794 	} else
2795 		ret = -EINVAL;
2796 
2797 	return ret;
2798 }
rtw_pm_set_wow_lps_level(_adapter * padapter,u8 level)2799 int rtw_pm_set_wow_lps_level(_adapter *padapter, u8 level)
2800 {
2801 	int	ret = 0;
2802 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2803 
2804 	if (level < LPS_LEVEL_MAX)
2805 		pwrctrlpriv->wowlan_lps_level = level;
2806 	else
2807 		ret = -EINVAL;
2808 
2809 	return ret;
2810 }
2811 
2812 #ifdef CONFIG_LPS_1T1R
rtw_pm_set_wow_lps_1t1r(_adapter * padapter,u8 en)2813 int rtw_pm_set_wow_lps_1t1r(_adapter *padapter, u8 en)
2814 {
2815 	int	ret = 0;
2816 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2817 
2818 	en = en ? 1 : 0;
2819 	pwrctrlpriv->wowlan_lps_1t1r = en;
2820 
2821 	return ret;
2822 }
2823 #endif /* CONFIG_LPS_1T1R */
2824 #endif /* CONFIG_WOWLAN */
2825 
rtw_pm_set_ips(_adapter * padapter,u8 mode)2826 int rtw_pm_set_ips(_adapter *padapter, u8 mode)
2827 {
2828 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
2829 
2830 	if (mode == IPS_NORMAL || mode == IPS_LEVEL_2) {
2831 		rtw_ips_mode_req(pwrctrlpriv, mode);
2832 		RTW_INFO("%s %s\n", __FUNCTION__, mode == IPS_NORMAL ? "IPS_NORMAL" : "IPS_LEVEL_2");
2833 		return 0;
2834 	} else if (mode == IPS_NONE) {
2835 		rtw_ips_mode_req(pwrctrlpriv, mode);
2836 		RTW_INFO("%s %s\n", __FUNCTION__, "IPS_NONE");
2837 		if (!rtw_is_surprise_removed(padapter) && (_FAIL == rtw_pwr_wakeup(padapter)))
2838 			return -EFAULT;
2839 	} else
2840 		return -EINVAL;
2841 	return 0;
2842 }
2843 
2844 /*
2845  * ATTENTION:
2846  *	This function will request pwrctrl LOCK!
2847  */
rtw_ps_deny(PADAPTER padapter,PS_DENY_REASON reason)2848 void rtw_ps_deny(PADAPTER padapter, PS_DENY_REASON reason)
2849 {
2850 	struct pwrctrl_priv *pwrpriv;
2851 
2852 	/* 	RTW_INFO("+" FUNC_ADPT_FMT ": Request PS deny for %d (0x%08X)\n",
2853 	 *		FUNC_ADPT_ARG(padapter), reason, BIT(reason)); */
2854 
2855 	pwrpriv = adapter_to_pwrctl(padapter);
2856 
2857 	_enter_pwrlock(&pwrpriv->lock);
2858 	if (pwrpriv->ps_deny & BIT(reason)) {
2859 		RTW_INFO(FUNC_ADPT_FMT ": [WARNING] Reason %d had been set before!!\n",
2860 			 FUNC_ADPT_ARG(padapter), reason);
2861 	}
2862 	pwrpriv->ps_deny |= BIT(reason);
2863 	_exit_pwrlock(&pwrpriv->lock);
2864 
2865 	/* 	RTW_INFO("-" FUNC_ADPT_FMT ": Now PS deny for 0x%08X\n",
2866 	 *		FUNC_ADPT_ARG(padapter), pwrpriv->ps_deny); */
2867 }
2868 
2869 /*
2870  * ATTENTION:
2871  *	This function will request pwrctrl LOCK!
2872  */
rtw_ps_deny_cancel(PADAPTER padapter,PS_DENY_REASON reason)2873 void rtw_ps_deny_cancel(PADAPTER padapter, PS_DENY_REASON reason)
2874 {
2875 	struct pwrctrl_priv *pwrpriv;
2876 
2877 
2878 	/* 	RTW_INFO("+" FUNC_ADPT_FMT ": Cancel PS deny for %d(0x%08X)\n",
2879 	 *		FUNC_ADPT_ARG(padapter), reason, BIT(reason)); */
2880 
2881 	pwrpriv = adapter_to_pwrctl(padapter);
2882 
2883 	_enter_pwrlock(&pwrpriv->lock);
2884 	if ((pwrpriv->ps_deny & BIT(reason)) == 0) {
2885 		RTW_INFO(FUNC_ADPT_FMT ": [ERROR] Reason %d had been canceled before!!\n",
2886 			 FUNC_ADPT_ARG(padapter), reason);
2887 	}
2888 	pwrpriv->ps_deny &= ~BIT(reason);
2889 	_exit_pwrlock(&pwrpriv->lock);
2890 
2891 	/* 	RTW_INFO("-" FUNC_ADPT_FMT ": Now PS deny for 0x%08X\n",
2892 	 *		FUNC_ADPT_ARG(padapter), pwrpriv->ps_deny); */
2893 }
2894 
2895 /*
2896  * ATTENTION:
2897  *	Before calling this function pwrctrl lock should be occupied already,
2898  *	otherwise it may return incorrect value.
2899  */
rtw_ps_deny_get(PADAPTER padapter)2900 u32 rtw_ps_deny_get(PADAPTER padapter)
2901 {
2902 	u32 deny;
2903 
2904 
2905 	deny = adapter_to_pwrctl(padapter)->ps_deny;
2906 
2907 	return deny;
2908 }
2909 
_rtw_ssmps(_adapter * adapter,struct sta_info * sta)2910 static void _rtw_ssmps(_adapter *adapter, struct sta_info *sta)
2911 {
2912 	struct mlme_ext_priv *pmlmeext = &(adapter->mlmeextpriv);
2913 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
2914 
2915 	if (MLME_IS_STA(adapter)) {
2916 		issue_action_SM_PS_wait_ack(adapter , get_my_bssid(&(pmlmeinfo->network)),
2917 			sta->cmn.sm_ps, 3 , 1);
2918 	}
2919 	else if (MLME_IS_AP(adapter)) {
2920 
2921 	}
2922 	rtw_phydm_ra_registed(adapter, sta);
2923 }
rtw_ssmps_enter(_adapter * adapter,struct sta_info * sta)2924 void rtw_ssmps_enter(_adapter *adapter, struct sta_info *sta)
2925 {
2926 	if (sta->cmn.sm_ps == SM_PS_STATIC)
2927 		return;
2928 
2929 	RTW_INFO(ADPT_FMT" STA [" MAC_FMT "]\n", ADPT_ARG(adapter), MAC_ARG(sta->cmn.mac_addr));
2930 
2931 	sta->cmn.sm_ps = SM_PS_STATIC;
2932 	_rtw_ssmps(adapter, sta);
2933 }
rtw_ssmps_leave(_adapter * adapter,struct sta_info * sta)2934 void rtw_ssmps_leave(_adapter *adapter, struct sta_info *sta)
2935 {
2936 	if (sta->cmn.sm_ps == SM_PS_DISABLE)
2937 		return;
2938 
2939 	RTW_INFO(ADPT_FMT" STA [" MAC_FMT "] \n", ADPT_ARG(adapter), MAC_ARG(sta->cmn.mac_addr));
2940 	sta->cmn.sm_ps = SM_PS_DISABLE;
2941 	_rtw_ssmps(adapter, sta);
2942 }
2943 
2944