• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2012 Realtek Corporation. */
3 
4 #define _RTW_PWRCTRL_C_
5 
6 #include "../include/osdep_service.h"
7 #include "../include/drv_types.h"
8 #include "../include/osdep_intf.h"
9 #include "../include/linux/usb.h"
10 
ips_enter(struct adapter * padapter)11 void ips_enter(struct adapter *padapter)
12 {
13 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
14 	struct xmit_priv *pxmit_priv = &padapter->xmitpriv;
15 
16 	if (pxmit_priv->free_xmitbuf_cnt != NR_XMITBUFF ||
17 	    pxmit_priv->free_xmit_extbuf_cnt != NR_XMIT_EXTBUFF)
18 		return;
19 
20 	mutex_lock(&pwrpriv->lock);
21 
22 	pwrpriv->bips_processing = true;
23 
24 	/*  syn ips_mode with request */
25 	pwrpriv->ips_mode = pwrpriv->ips_mode_req;
26 
27 	pwrpriv->ips_enter_cnts++;
28 	if (rf_off == pwrpriv->change_rfpwrstate) {
29 		pwrpriv->bpower_saving = true;
30 
31 		if (pwrpriv->ips_mode == IPS_LEVEL_2)
32 			pwrpriv->bkeepfwalive = true;
33 
34 		rtw_ips_pwr_down(padapter);
35 		pwrpriv->rf_pwrstate = rf_off;
36 	}
37 	pwrpriv->bips_processing = false;
38 
39 	mutex_unlock(&pwrpriv->lock);
40 }
41 
ips_leave(struct adapter * padapter)42 int ips_leave(struct adapter *padapter)
43 {
44 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
45 	struct security_priv *psecuritypriv = &padapter->securitypriv;
46 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
47 	int result = _SUCCESS;
48 	int keyid;
49 
50 	mutex_lock(&pwrpriv->lock);
51 
52 	if ((pwrpriv->rf_pwrstate == rf_off) && (!pwrpriv->bips_processing)) {
53 		pwrpriv->bips_processing = true;
54 		pwrpriv->change_rfpwrstate = rf_on;
55 		pwrpriv->ips_leave_cnts++;
56 
57 		result = rtw_ips_pwr_up(padapter);
58 		if (result == _SUCCESS) {
59 			pwrpriv->rf_pwrstate = rf_on;
60 		}
61 
62 		if ((psecuritypriv->dot11PrivacyAlgrthm == _WEP40_) || (psecuritypriv->dot11PrivacyAlgrthm == _WEP104_)) {
63 			set_channel_bwmode(padapter, padapter->mlmeextpriv.cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
64 			for (keyid = 0; keyid < 4; keyid++) {
65 				if (pmlmepriv->key_mask & BIT(keyid)) {
66 					if (keyid == psecuritypriv->dot11PrivacyKeyIndex)
67 						result = rtw_set_key(padapter, psecuritypriv, keyid, 1);
68 					else
69 						result = rtw_set_key(padapter, psecuritypriv, keyid, 0);
70 				}
71 			}
72 		}
73 
74 		pwrpriv->bips_processing = false;
75 
76 		pwrpriv->bkeepfwalive = false;
77 		pwrpriv->bpower_saving = false;
78 	}
79 
80 	mutex_unlock(&pwrpriv->lock);
81 
82 	return result;
83 }
84 
rtw_pwr_unassociated_idle(struct adapter * adapter)85 static bool rtw_pwr_unassociated_idle(struct adapter *adapter)
86 {
87 	struct adapter *buddy = adapter->pbuddy_adapter;
88 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
89 	struct wifidirect_info	*pwdinfo = &adapter->wdinfo;
90 	bool ret = false;
91 
92 	if (time_after_eq(adapter->pwrctrlpriv.ips_deny_time, jiffies))
93 		goto exit;
94 
95 	if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE | WIFI_SITE_MONITOR) ||
96 	    check_fwstate(pmlmepriv, WIFI_UNDER_LINKING | WIFI_UNDER_WPS) ||
97 	    check_fwstate(pmlmepriv, WIFI_UNDER_WPS) ||
98 	    check_fwstate(pmlmepriv, WIFI_AP_STATE) ||
99 	    check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE | WIFI_ADHOC_STATE) ||
100 	    !rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE))
101 		goto exit;
102 
103 	/* consider buddy, if exist */
104 	if (buddy) {
105 		struct mlme_priv *b_pmlmepriv = &buddy->mlmepriv;
106 		struct wifidirect_info *b_pwdinfo = &buddy->wdinfo;
107 
108 		if (check_fwstate(b_pmlmepriv, WIFI_ASOC_STATE | WIFI_SITE_MONITOR) ||
109 		    check_fwstate(b_pmlmepriv, WIFI_UNDER_LINKING | WIFI_UNDER_WPS) ||
110 		    check_fwstate(b_pmlmepriv, WIFI_AP_STATE) ||
111 		    check_fwstate(b_pmlmepriv, WIFI_ADHOC_MASTER_STATE | WIFI_ADHOC_STATE) ||
112 		    !rtw_p2p_chk_state(b_pwdinfo, P2P_STATE_NONE))
113 			goto exit;
114 	}
115 	ret = true;
116 
117 exit:
118 	return ret;
119 }
120 
rtw_ps_processor(struct adapter * padapter)121 void rtw_ps_processor(struct adapter *padapter)
122 {
123 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
124 
125 	pwrpriv->ps_processing = true;
126 
127 	if (pwrpriv->bips_processing)
128 		goto exit;
129 
130 	if (pwrpriv->ips_mode_req == IPS_NONE)
131 		goto exit;
132 
133 	if (!rtw_pwr_unassociated_idle(padapter))
134 		goto exit;
135 
136 	if (pwrpriv->rf_pwrstate == rf_on) {
137 		pwrpriv->change_rfpwrstate = rf_off;
138 		ips_enter(padapter);
139 	}
140 exit:
141 	rtw_set_pwr_state_check_timer(&padapter->pwrctrlpriv);
142 	pwrpriv->ps_processing = false;
143 }
144 
pwr_state_check_handler(struct timer_list * t)145 static void pwr_state_check_handler(struct timer_list *t)
146 {
147 	struct adapter *padapter =
148 		from_timer(padapter, t,
149 			   pwrctrlpriv.pwr_state_check_timer);
150 	rtw_ps_cmd(padapter);
151 }
152 
PS_RDY_CHECK(struct adapter * padapter)153 static bool PS_RDY_CHECK(struct adapter *padapter)
154 {
155 	u32 curr_time, delta_time;
156 	struct pwrctrl_priv	*pwrpriv = &padapter->pwrctrlpriv;
157 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
158 
159 	curr_time = jiffies;
160 	delta_time = curr_time - pwrpriv->DelayLPSLastTimeStamp;
161 
162 	if (delta_time < LPS_DELAY_TIME)
163 		return false;
164 
165 	if (!check_fwstate(pmlmepriv, _FW_LINKED) ||
166 	    check_fwstate(pmlmepriv, _FW_UNDER_SURVEY) ||
167 	    check_fwstate(pmlmepriv, WIFI_AP_STATE) ||
168 	    check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) ||
169 	    check_fwstate(pmlmepriv, WIFI_ADHOC_STATE))
170 		return false;
171 	if (pwrpriv->bInSuspend)
172 		return false;
173 	if (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X &&
174 	    !padapter->securitypriv.binstallGrpkey)
175 		return false;
176 	return true;
177 }
178 
rtw_set_firmware_ps_mode(struct adapter * adapter,u8 mode)179 void rtw_set_firmware_ps_mode(struct adapter *adapter, u8 mode)
180 {
181 	struct hal_data_8188e *haldata = &adapter->haldata;
182 	struct odm_dm_struct *odmpriv = &haldata->odmpriv;
183 
184 	/* Force leave RF low power mode for 1T1R to prevent
185 	 * conflicting setting in firmware power saving sequence.
186 	 */
187 	if (mode != PS_MODE_ACTIVE)
188 		ODM_RF_Saving(odmpriv, true);
189 	rtl8188e_set_FwPwrMode_cmd(adapter, mode);
190 }
191 
rtw_set_ps_mode(struct adapter * padapter,u8 ps_mode,u8 smart_ps,u8 bcn_ant_mode)192 void rtw_set_ps_mode(struct adapter *padapter, u8 ps_mode, u8 smart_ps, u8 bcn_ant_mode)
193 {
194 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
195 	struct wifidirect_info	*pwdinfo = &padapter->wdinfo;
196 
197 	if (ps_mode > PM_Card_Disable)
198 		return;
199 
200 	if (pwrpriv->pwr_mode == ps_mode) {
201 		if (ps_mode == PS_MODE_ACTIVE)
202 			return;
203 
204 		if ((pwrpriv->smart_ps == smart_ps) &&
205 		    (pwrpriv->bcn_ant_mode == bcn_ant_mode))
206 			return;
207 	}
208 
209 	if (ps_mode == PS_MODE_ACTIVE) {
210 		if (pwdinfo->opp_ps == 0) {
211 			pwrpriv->pwr_mode = ps_mode;
212 			rtw_set_firmware_ps_mode(padapter, ps_mode);
213 			pwrpriv->bFwCurrentInPSMode = false;
214 		}
215 	} else {
216 		if (PS_RDY_CHECK(padapter)) {
217 			pwrpriv->bFwCurrentInPSMode = true;
218 			pwrpriv->pwr_mode = ps_mode;
219 			pwrpriv->smart_ps = smart_ps;
220 			pwrpriv->bcn_ant_mode = bcn_ant_mode;
221 			rtw_set_firmware_ps_mode(padapter, ps_mode);
222 
223 			/*  Set CTWindow after LPS */
224 			if (pwdinfo->opp_ps == 1)
225 				p2p_ps_wk_cmd(padapter, P2P_PS_ENABLE, 0);
226 		}
227 	}
228 }
229 
lps_rf_on(struct adapter * adapter)230 static bool lps_rf_on(struct adapter *adapter)
231 {
232 	int res;
233 	u32 reg;
234 
235 	/* When we halt NIC, we should check if FW LPS is leave. */
236 	if (adapter->pwrctrlpriv.rf_pwrstate == rf_off) {
237 		/*  If it is in HW/SW Radio OFF or IPS state, we do not check Fw LPS Leave, */
238 		/*  because Fw is unload. */
239 		return true;
240 	}
241 
242 	res = rtw_read32(adapter, REG_RCR, &reg);
243 	if (res)
244 		return false;
245 
246 	if (reg & 0x00070000)
247 		return false;
248 
249 	return true;
250 }
251 
252 /*
253  * Return:
254  *	0:	Leave OK
255  *	-1:	Timeout
256  *	-2:	Other error
257  */
LPS_RF_ON_check(struct adapter * padapter,u32 delay_ms)258 static s32 LPS_RF_ON_check(struct adapter *padapter, u32 delay_ms)
259 {
260 	unsigned long timeout = jiffies + msecs_to_jiffies(delay_ms);
261 	s32 err = 0;
262 
263 	while (1) {
264 		if (lps_rf_on(padapter))
265 			break;
266 
267 		if (padapter->bSurpriseRemoved) {
268 			err = -2;
269 			break;
270 		}
271 
272 		if (time_after(jiffies, timeout)) {
273 			err = -1;
274 			break;
275 		}
276 		mdelay(1);
277 	}
278 
279 	return err;
280 }
281 
282 /*  */
283 /*	Description: */
284 /*		Enter the leisure power save mode. */
285 /*  */
LPS_Enter(struct adapter * padapter)286 void LPS_Enter(struct adapter *padapter)
287 {
288 	struct pwrctrl_priv	*pwrpriv = &padapter->pwrctrlpriv;
289 
290 	if (!PS_RDY_CHECK(padapter))
291 		return;
292 
293 	if (pwrpriv->bLeisurePs) {
294 		/*  Idle for a while if we connect to AP a while ago. */
295 		if (pwrpriv->LpsIdleCount >= 2) { /*   4 Sec */
296 			if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) {
297 				pwrpriv->bpower_saving = true;
298 				/* For Tenda W311R IOT issue */
299 				rtw_set_ps_mode(padapter, pwrpriv->power_mgnt,
300 						pwrpriv->smart_ps, 0x40);
301 			}
302 		} else {
303 			pwrpriv->LpsIdleCount++;
304 		}
305 	}
306 
307 }
308 
309 #define LPS_LEAVE_TIMEOUT_MS 100
310 
311 /*	Description: */
312 /*		Leave the leisure power save mode. */
LPS_Leave(struct adapter * padapter)313 void LPS_Leave(struct adapter *padapter)
314 {
315 	struct pwrctrl_priv	*pwrpriv = &padapter->pwrctrlpriv;
316 
317 	if (pwrpriv->bLeisurePs) {
318 		if (pwrpriv->pwr_mode != PS_MODE_ACTIVE) {
319 			rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0x40);
320 
321 			if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
322 				LPS_RF_ON_check(padapter, LPS_LEAVE_TIMEOUT_MS);
323 		}
324 	}
325 
326 	pwrpriv->bpower_saving = false;
327 
328 }
329 
330 /*  */
331 /*  Description: Leave all power save mode: LPS, FwLPS, IPS if needed. */
332 /*  Move code to function by tynli. 2010.03.26. */
333 /*  */
LeaveAllPowerSaveMode(struct adapter * Adapter)334 void LeaveAllPowerSaveMode(struct adapter *Adapter)
335 {
336 	struct mlme_priv	*pmlmepriv = &Adapter->mlmepriv;
337 	u8	enqueue = 0;
338 
339 	if (check_fwstate(pmlmepriv, _FW_LINKED)) { /* connect */
340 		p2p_ps_wk_cmd(Adapter, P2P_PS_DISABLE, enqueue);
341 
342 		rtw_lps_ctrl_wk_cmd(Adapter, LPS_CTRL_LEAVE, enqueue);
343 	}
344 
345 }
346 
rtw_init_pwrctrl_priv(struct adapter * padapter)347 void rtw_init_pwrctrl_priv(struct adapter *padapter)
348 {
349 	struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
350 
351 	mutex_init(&pwrctrlpriv->lock);
352 	pwrctrlpriv->rf_pwrstate = rf_on;
353 	pwrctrlpriv->ips_enter_cnts = 0;
354 	pwrctrlpriv->ips_leave_cnts = 0;
355 	pwrctrlpriv->bips_processing = false;
356 
357 	pwrctrlpriv->ips_mode = padapter->registrypriv.ips_mode;
358 	pwrctrlpriv->ips_mode_req = padapter->registrypriv.ips_mode;
359 
360 	pwrctrlpriv->pwr_state_check_interval = RTW_PWR_STATE_CHK_INTERVAL;
361 	pwrctrlpriv->bInSuspend = false;
362 	pwrctrlpriv->bkeepfwalive = false;
363 
364 	pwrctrlpriv->LpsIdleCount = 0;
365 	pwrctrlpriv->power_mgnt = padapter->registrypriv.power_mgnt;/*  PS_MODE_MIN; */
366 	pwrctrlpriv->bLeisurePs = pwrctrlpriv->power_mgnt != PS_MODE_ACTIVE;
367 
368 	pwrctrlpriv->bFwCurrentInPSMode = false;
369 
370 	pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
371 	pwrctrlpriv->smart_ps = padapter->registrypriv.smart_ps;
372 	pwrctrlpriv->bcn_ant_mode = 0;
373 
374 	timer_setup(&pwrctrlpriv->pwr_state_check_timer, pwr_state_check_handler, 0);
375 }
376 
377 /* Wake the NIC up from: 1)IPS 2)USB autosuspend */
rtw_pwr_wakeup(struct adapter * padapter)378 int rtw_pwr_wakeup(struct adapter *padapter)
379 {
380 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
381 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
382 	unsigned long timeout = jiffies + msecs_to_jiffies(3000);
383 	unsigned long deny_time;
384 	int ret;
385 
386 	while (pwrpriv->ps_processing && time_before(jiffies, timeout))
387 		msleep(10);
388 
389 	/* I think this should be check in IPS, LPS, autosuspend functions... */
390 	/* Below goto is a success path taken for already linked devices */
391 	ret = 0;
392 	if (check_fwstate(pmlmepriv, _FW_LINKED))
393 		goto exit;
394 
395 	if (pwrpriv->rf_pwrstate == rf_off && ips_leave(padapter) == _FAIL) {
396 		ret = -ENOMEM;
397 		goto exit;
398 	}
399 
400 	if (padapter->bDriverStopped || !padapter->bup || !padapter->hw_init_completed) {
401 		ret = -EBUSY;
402 		goto exit;
403 	}
404 
405 exit:
406 	deny_time = jiffies + msecs_to_jiffies(RTW_PWR_STATE_CHK_INTERVAL);
407 	if (time_before(pwrpriv->ips_deny_time, deny_time))
408 		pwrpriv->ips_deny_time = deny_time;
409 	return ret;
410 }
411 
rtw_pm_set_lps(struct adapter * padapter,u8 mode)412 int rtw_pm_set_lps(struct adapter *padapter, u8 mode)
413 {
414 	int	ret = 0;
415 	struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
416 
417 	if (mode < PS_MODE_NUM) {
418 		if (pwrctrlpriv->power_mgnt != mode) {
419 			if (mode == PS_MODE_ACTIVE)
420 				LeaveAllPowerSaveMode(padapter);
421 			else
422 				pwrctrlpriv->LpsIdleCount = 2;
423 			pwrctrlpriv->power_mgnt = mode;
424 			pwrctrlpriv->bLeisurePs = pwrctrlpriv->power_mgnt != PS_MODE_ACTIVE;
425 		}
426 	} else {
427 		ret = -EINVAL;
428 	}
429 
430 	return ret;
431 }
432 
rtw_pm_set_ips(struct adapter * padapter,u8 mode)433 int rtw_pm_set_ips(struct adapter *padapter, u8 mode)
434 {
435 	struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
436 
437 	if (mode == IPS_NORMAL || mode == IPS_LEVEL_2) {
438 		rtw_ips_mode_req(pwrctrlpriv, mode);
439 		return 0;
440 	} else if (mode == IPS_NONE) {
441 		rtw_ips_mode_req(pwrctrlpriv, mode);
442 		if ((padapter->bSurpriseRemoved == 0) && rtw_pwr_wakeup(padapter))
443 			return -EFAULT;
444 	} else {
445 		return -EINVAL;
446 	}
447 	return 0;
448 }
449