• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
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  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _RTW_PWRCTRL_C_
21 
22 #include <osdep_service.h>
23 #include <drv_types.h>
24 #include <osdep_intf.h>
25 #include <usb_ops_linux.h>
26 #include <linux/usb.h>
27 
rtw_hw_suspend(struct adapter * padapter)28 static int rtw_hw_suspend(struct adapter *padapter)
29 {
30 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
31 	struct net_device *pnetdev = padapter->pnetdev;
32 
33 
34 	if ((!padapter->bup) || (padapter->bDriverStopped) ||
35 	    (padapter->bSurpriseRemoved)) {
36 		DBG_88E("padapter->bup=%d bDriverStopped=%d bSurpriseRemoved = %d\n",
37 			padapter->bup, padapter->bDriverStopped,
38 			padapter->bSurpriseRemoved);
39 		goto error_exit;
40 	}
41 
42 	/* system suspend */
43 	LeaveAllPowerSaveMode(padapter);
44 
45 	DBG_88E("==> rtw_hw_suspend\n");
46 	_enter_pwrlock(&pwrpriv->lock);
47 	pwrpriv->bips_processing = true;
48 	/* s1. */
49 	if (pnetdev) {
50 		netif_carrier_off(pnetdev);
51 		netif_tx_stop_all_queues(pnetdev);
52 	}
53 
54 	/* s2. */
55 	rtw_disassoc_cmd(padapter, 500, false);
56 
57 	/* s2-2.  indicate disconnect to os */
58 	{
59 		struct	mlme_priv *pmlmepriv = &padapter->mlmepriv;
60 
61 		if (check_fwstate(pmlmepriv, _FW_LINKED)) {
62 			_clr_fwstate_(pmlmepriv, _FW_LINKED);
63 
64 			rtw_led_control(padapter, LED_CTL_NO_LINK);
65 
66 			rtw_os_indicate_disconnect(padapter);
67 
68 			/* donnot enqueue cmd */
69 			rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_DISCONNECT, 0);
70 		}
71 	}
72 	/* s2-3. */
73 	rtw_free_assoc_resources(padapter, 1);
74 
75 	/* s2-4. */
76 	rtw_free_network_queue(padapter, true);
77 	rtw_ips_dev_unload(padapter);
78 	pwrpriv->rf_pwrstate = rf_off;
79 	pwrpriv->bips_processing = false;
80 
81 	_exit_pwrlock(&pwrpriv->lock);
82 
83 	return 0;
84 
85 error_exit:
86 	DBG_88E("%s, failed\n", __func__);
87 	return -1;
88 }
89 
rtw_hw_resume(struct adapter * padapter)90 static int rtw_hw_resume(struct adapter *padapter)
91 {
92 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
93 	struct net_device *pnetdev = padapter->pnetdev;
94 
95 
96 	/* system resume */
97 	DBG_88E("==> rtw_hw_resume\n");
98 	_enter_pwrlock(&pwrpriv->lock);
99 	pwrpriv->bips_processing = true;
100 	rtw_reset_drv_sw(padapter);
101 
102 	if (pm_netdev_open(pnetdev, false) != 0) {
103 		_exit_pwrlock(&pwrpriv->lock);
104 		goto error_exit;
105 	}
106 
107 	netif_device_attach(pnetdev);
108 	netif_carrier_on(pnetdev);
109 
110 	if (!netif_queue_stopped(pnetdev))
111 		netif_start_queue(pnetdev);
112 	else
113 		netif_wake_queue(pnetdev);
114 
115 	pwrpriv->bkeepfwalive = false;
116 	pwrpriv->brfoffbyhw = false;
117 
118 	pwrpriv->rf_pwrstate = rf_on;
119 	pwrpriv->bips_processing = false;
120 
121 	_exit_pwrlock(&pwrpriv->lock);
122 
123 
124 	return 0;
125 error_exit:
126 	DBG_88E("%s, Open net dev failed\n", __func__);
127 	return -1;
128 }
129 
ips_enter(struct adapter * padapter)130 void ips_enter(struct adapter *padapter)
131 {
132 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
133 	struct xmit_priv *pxmit_priv = &padapter->xmitpriv;
134 
135 	if (padapter->registrypriv.mp_mode == 1)
136 		return;
137 
138 	if (pxmit_priv->free_xmitbuf_cnt != NR_XMITBUFF ||
139 	    pxmit_priv->free_xmit_extbuf_cnt != NR_XMIT_EXTBUFF) {
140 		DBG_88E_LEVEL(_drv_info_, "There are some pkts to transmit\n");
141 		DBG_88E_LEVEL(_drv_info_, "free_xmitbuf_cnt: %d, free_xmit_extbuf_cnt: %d\n",
142 			      pxmit_priv->free_xmitbuf_cnt, pxmit_priv->free_xmit_extbuf_cnt);
143 		return;
144 	}
145 
146 	_enter_pwrlock(&pwrpriv->lock);
147 
148 	pwrpriv->bips_processing = true;
149 
150 	/*  syn ips_mode with request */
151 	pwrpriv->ips_mode = pwrpriv->ips_mode_req;
152 
153 	pwrpriv->ips_enter_cnts++;
154 	DBG_88E("==>ips_enter cnts:%d\n", pwrpriv->ips_enter_cnts);
155 	if (rf_off == pwrpriv->change_rfpwrstate) {
156 		pwrpriv->bpower_saving = true;
157 		DBG_88E_LEVEL(_drv_info_, "nolinked power save enter\n");
158 
159 		if (pwrpriv->ips_mode == IPS_LEVEL_2)
160 			pwrpriv->bkeepfwalive = true;
161 
162 		rtw_ips_pwr_down(padapter);
163 		pwrpriv->rf_pwrstate = rf_off;
164 	}
165 	pwrpriv->bips_processing = false;
166 
167 	_exit_pwrlock(&pwrpriv->lock);
168 }
169 
ips_leave(struct adapter * padapter)170 int ips_leave(struct adapter *padapter)
171 {
172 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
173 	struct security_priv *psecuritypriv = &(padapter->securitypriv);
174 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
175 	int result = _SUCCESS;
176 	int keyid;
177 
178 
179 	_enter_pwrlock(&pwrpriv->lock);
180 
181 	if ((pwrpriv->rf_pwrstate == rf_off) && (!pwrpriv->bips_processing)) {
182 		pwrpriv->bips_processing = true;
183 		pwrpriv->change_rfpwrstate = rf_on;
184 		pwrpriv->ips_leave_cnts++;
185 		DBG_88E("==>ips_leave cnts:%d\n", pwrpriv->ips_leave_cnts);
186 
187 		result = rtw_ips_pwr_up(padapter);
188 		if (result == _SUCCESS) {
189 			pwrpriv->rf_pwrstate = rf_on;
190 		}
191 		DBG_88E_LEVEL(_drv_info_, "nolinked power save leave\n");
192 
193 		if ((_WEP40_ == psecuritypriv->dot11PrivacyAlgrthm) || (_WEP104_ == psecuritypriv->dot11PrivacyAlgrthm)) {
194 			DBG_88E("==>%s, channel(%d), processing(%x)\n", __func__, padapter->mlmeextpriv.cur_channel, pwrpriv->bips_processing);
195 			set_channel_bwmode(padapter, padapter->mlmeextpriv.cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
196 			for (keyid = 0; keyid < 4; keyid++) {
197 				if (pmlmepriv->key_mask & BIT(keyid)) {
198 					if (keyid == psecuritypriv->dot11PrivacyKeyIndex)
199 						result = rtw_set_key(padapter, psecuritypriv, keyid, 1);
200 					else
201 						result = rtw_set_key(padapter, psecuritypriv, keyid, 0);
202 				}
203 			}
204 		}
205 
206 		DBG_88E("==> ips_leave.....LED(0x%08x)...\n", usb_read32(padapter, 0x4c));
207 		pwrpriv->bips_processing = false;
208 
209 		pwrpriv->bkeepfwalive = false;
210 		pwrpriv->bpower_saving = false;
211 	}
212 
213 	_exit_pwrlock(&pwrpriv->lock);
214 
215 	return result;
216 }
217 
rtw_pwr_unassociated_idle(struct adapter * adapter)218 static bool rtw_pwr_unassociated_idle(struct adapter *adapter)
219 {
220 	struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
221 	bool ret = false;
222 
223 	if (time_after_eq(adapter->pwrctrlpriv.ips_deny_time, jiffies))
224 		goto exit;
225 
226 	if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE|WIFI_SITE_MONITOR) ||
227 	    check_fwstate(pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS) ||
228 	    check_fwstate(pmlmepriv, WIFI_AP_STATE) ||
229 	    check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE))
230 		goto exit;
231 
232 	ret = true;
233 
234 exit:
235 	return ret;
236 }
237 
rtw_ps_processor(struct adapter * padapter)238 void rtw_ps_processor(struct adapter *padapter)
239 {
240 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
241 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
242 	enum rt_rf_power_state rfpwrstate;
243 
244 	pwrpriv->ps_processing = true;
245 
246 	if (pwrpriv->bips_processing)
247 		goto exit;
248 
249 	if (padapter->pwrctrlpriv.bHWPwrPindetect) {
250 		rfpwrstate = RfOnOffDetect(padapter);
251 		DBG_88E("@@@@- #2  %s==> rfstate:%s\n", __func__, (rfpwrstate == rf_on) ? "rf_on" : "rf_off");
252 
253 		if (rfpwrstate != pwrpriv->rf_pwrstate) {
254 			if (rfpwrstate == rf_off) {
255 				pwrpriv->change_rfpwrstate = rf_off;
256 				pwrpriv->brfoffbyhw = true;
257 				rtw_hw_suspend(padapter);
258 			} else {
259 				pwrpriv->change_rfpwrstate = rf_on;
260 				rtw_hw_resume(padapter);
261 			}
262 			DBG_88E("current rf_pwrstate(%s)\n", (pwrpriv->rf_pwrstate == rf_off) ? "rf_off" : "rf_on");
263 		}
264 		pwrpriv->pwr_state_check_cnts++;
265 	}
266 
267 	if (pwrpriv->ips_mode_req == IPS_NONE)
268 		goto exit;
269 
270 	if (!rtw_pwr_unassociated_idle(padapter))
271 		goto exit;
272 
273 	if ((pwrpriv->rf_pwrstate == rf_on) && ((pwrpriv->pwr_state_check_cnts%4) == 0)) {
274 		DBG_88E("==>%s .fw_state(%x)\n", __func__, get_fwstate(pmlmepriv));
275 		pwrpriv->change_rfpwrstate = rf_off;
276 
277 		ips_enter(padapter);
278 	}
279 exit:
280 	rtw_set_pwr_state_check_timer(&padapter->pwrctrlpriv);
281 	pwrpriv->ps_processing = false;
282 	return;
283 }
284 
pwr_state_check_handler(void * FunctionContext)285 static void pwr_state_check_handler(void *FunctionContext)
286 {
287 	struct adapter *padapter = (struct adapter *)FunctionContext;
288 	rtw_ps_cmd(padapter);
289 }
290 
291 /*
292  *
293  * Parameters
294  *	padapter
295  *	pslv			power state level, only could be PS_STATE_S0 ~ PS_STATE_S4
296  *
297  */
rtw_set_rpwm(struct adapter * padapter,u8 pslv)298 void rtw_set_rpwm(struct adapter *padapter, u8 pslv)
299 {
300 	u8	rpwm;
301 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
302 
303 	pslv = PS_STATE(pslv);
304 
305 	if (pwrpriv->btcoex_rfon) {
306 		if (pslv < PS_STATE_S4)
307 			pslv = PS_STATE_S3;
308 	}
309 
310 	if ((pwrpriv->rpwm == pslv)) {
311 		RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
312 			 ("%s: Already set rpwm[0x%02X], new=0x%02X!\n", __func__, pwrpriv->rpwm, pslv));
313 		return;
314 	}
315 
316 	if ((padapter->bSurpriseRemoved) ||
317 	    (!padapter->hw_init_completed)) {
318 		RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
319 			 ("%s: SurpriseRemoved(%d) hw_init_completed(%d)\n",
320 			 __func__, padapter->bSurpriseRemoved, padapter->hw_init_completed));
321 
322 		pwrpriv->cpwm = PS_STATE_S4;
323 
324 		return;
325 	}
326 
327 	if (padapter->bDriverStopped) {
328 		RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
329 			 ("%s: change power state(0x%02X) when DriverStopped\n", __func__, pslv));
330 
331 		if (pslv < PS_STATE_S2) {
332 			RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
333 				 ("%s: Reject to enter PS_STATE(0x%02X) lower than S2 when DriverStopped!!\n", __func__, pslv));
334 			return;
335 		}
336 	}
337 
338 	rpwm = pslv | pwrpriv->tog;
339 	RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
340 		 ("rtw_set_rpwm: rpwm=0x%02x cpwm=0x%02x\n", rpwm, pwrpriv->cpwm));
341 
342 	pwrpriv->rpwm = pslv;
343 
344 	rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&rpwm));
345 
346 	pwrpriv->tog += 0x80;
347 	pwrpriv->cpwm = pslv;
348 }
349 
PS_RDY_CHECK(struct adapter * padapter)350 static u8 PS_RDY_CHECK(struct adapter *padapter)
351 {
352 	u32 curr_time, delta_time;
353 	struct pwrctrl_priv	*pwrpriv = &padapter->pwrctrlpriv;
354 	struct mlme_priv	*pmlmepriv = &(padapter->mlmepriv);
355 
356 
357 	curr_time = jiffies;
358 	delta_time = curr_time - pwrpriv->DelayLPSLastTimeStamp;
359 
360 	if (delta_time < LPS_DELAY_TIME)
361 		return false;
362 
363 	if ((check_fwstate(pmlmepriv, _FW_LINKED) == false) ||
364 	    (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY)) ||
365 	    (check_fwstate(pmlmepriv, WIFI_AP_STATE)) ||
366 	    (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) ||
367 	    (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)))
368 		return false;
369 	if (pwrpriv->bInSuspend)
370 		return false;
371 	if ((padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) && (padapter->securitypriv.binstallGrpkey == false)) {
372 		DBG_88E("Group handshake still in progress !!!\n");
373 		return false;
374 	}
375 	return true;
376 }
377 
rtw_set_ps_mode(struct adapter * padapter,u8 ps_mode,u8 smart_ps,u8 bcn_ant_mode)378 void rtw_set_ps_mode(struct adapter *padapter, u8 ps_mode, u8 smart_ps, u8 bcn_ant_mode)
379 {
380 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
381 
382 	RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
383 		 ("%s: PowerMode=%d Smart_PS=%d\n",
384 		  __func__, ps_mode, smart_ps));
385 
386 	if (ps_mode > PM_Card_Disable) {
387 		RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_, ("ps_mode:%d error\n", ps_mode));
388 		return;
389 	}
390 
391 	if (pwrpriv->pwr_mode == ps_mode) {
392 		if (PS_MODE_ACTIVE == ps_mode)
393 			return;
394 
395 		if ((pwrpriv->smart_ps == smart_ps) &&
396 		    (pwrpriv->bcn_ant_mode == bcn_ant_mode))
397 			return;
398 	}
399 
400 	/* if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) */
401 	if (ps_mode == PS_MODE_ACTIVE) {
402 		if (PS_RDY_CHECK(padapter)) {
403 			DBG_88E("%s: Enter 802.11 power save\n", __func__);
404 			pwrpriv->bFwCurrentInPSMode = true;
405 			pwrpriv->pwr_mode = ps_mode;
406 			pwrpriv->smart_ps = smart_ps;
407 			pwrpriv->bcn_ant_mode = bcn_ant_mode;
408 			rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
409 			rtw_set_rpwm(padapter, PS_STATE_S2);
410 		}
411 	}
412 }
413 
414 /*
415  * Return:
416  *	0:	Leave OK
417  *	-1:	Timeout
418  *	-2:	Other error
419  */
LPS_RF_ON_check(struct adapter * padapter,u32 delay_ms)420 s32 LPS_RF_ON_check(struct adapter *padapter, u32 delay_ms)
421 {
422 	u32 start_time;
423 	u8 bAwake = false;
424 	s32 err = 0;
425 
426 
427 	start_time = jiffies;
428 	while (1) {
429 		rtw_hal_get_hwreg(padapter, HW_VAR_FWLPS_RF_ON, &bAwake);
430 		if (bAwake)
431 			break;
432 
433 		if (padapter->bSurpriseRemoved) {
434 			err = -2;
435 			DBG_88E("%s: device surprise removed!!\n", __func__);
436 			break;
437 		}
438 
439 		if (rtw_get_passing_time_ms(start_time) > delay_ms) {
440 			err = -1;
441 			DBG_88E("%s: Wait for FW LPS leave more than %u ms!!!\n", __func__, delay_ms);
442 			break;
443 		}
444 		msleep(1);
445 	}
446 
447 	return err;
448 }
449 
450 /*  */
451 /*	Description: */
452 /*		Enter the leisure power save mode. */
453 /*  */
LPS_Enter(struct adapter * padapter)454 void LPS_Enter(struct adapter *padapter)
455 {
456 	struct pwrctrl_priv	*pwrpriv = &padapter->pwrctrlpriv;
457 
458 	if (PS_RDY_CHECK(padapter) == false)
459 		return;
460 
461 	if (pwrpriv->bLeisurePs) {
462 		/*  Idle for a while if we connect to AP a while ago. */
463 		if (pwrpriv->LpsIdleCount >= 2) { /*   4 Sec */
464 			if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) {
465 				pwrpriv->bpower_saving = true;
466 				DBG_88E("%s smart_ps:%d\n", __func__, pwrpriv->smart_ps);
467 				/* For Tenda W311R IOT issue */
468 				rtw_set_ps_mode(padapter, pwrpriv->power_mgnt, pwrpriv->smart_ps, 0);
469 			}
470 		} else {
471 			pwrpriv->LpsIdleCount++;
472 		}
473 	}
474 }
475 
476 #define LPS_LEAVE_TIMEOUT_MS 100
477 
478 /*	Description: */
479 /*		Leave the leisure power save mode. */
LPS_Leave(struct adapter * padapter)480 void LPS_Leave(struct adapter *padapter)
481 {
482 	struct pwrctrl_priv	*pwrpriv = &padapter->pwrctrlpriv;
483 
484 	if (pwrpriv->bLeisurePs) {
485 		if (pwrpriv->pwr_mode != PS_MODE_ACTIVE) {
486 			rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0);
487 
488 			if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
489 				LPS_RF_ON_check(padapter, LPS_LEAVE_TIMEOUT_MS);
490 		}
491 	}
492 
493 	pwrpriv->bpower_saving = false;
494 }
495 
496 /*  */
497 /*  Description: Leave all power save mode: LPS, FwLPS, IPS if needed. */
498 /*  Move code to function by tynli. 2010.03.26. */
499 /*  */
LeaveAllPowerSaveMode(struct adapter * Adapter)500 void LeaveAllPowerSaveMode(struct adapter *Adapter)
501 {
502 	struct mlme_priv	*pmlmepriv = &(Adapter->mlmepriv);
503 	u8	enqueue = 0;
504 
505 	if (check_fwstate(pmlmepriv, _FW_LINKED))
506 		rtw_lps_ctrl_wk_cmd(Adapter, LPS_CTRL_LEAVE, enqueue);
507 }
508 
rtw_init_pwrctrl_priv(struct adapter * padapter)509 void rtw_init_pwrctrl_priv(struct adapter *padapter)
510 {
511 	struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
512 
513 	_init_pwrlock(&pwrctrlpriv->lock);
514 	pwrctrlpriv->rf_pwrstate = rf_on;
515 	pwrctrlpriv->ips_enter_cnts = 0;
516 	pwrctrlpriv->ips_leave_cnts = 0;
517 	pwrctrlpriv->bips_processing = false;
518 
519 	pwrctrlpriv->ips_mode = padapter->registrypriv.ips_mode;
520 	pwrctrlpriv->ips_mode_req = padapter->registrypriv.ips_mode;
521 
522 	pwrctrlpriv->pwr_state_check_interval = RTW_PWR_STATE_CHK_INTERVAL;
523 	pwrctrlpriv->pwr_state_check_cnts = 0;
524 	pwrctrlpriv->bInternalAutoSuspend = false;
525 	pwrctrlpriv->bInSuspend = false;
526 	pwrctrlpriv->bkeepfwalive = false;
527 
528 	pwrctrlpriv->LpsIdleCount = 0;
529 	if (padapter->registrypriv.mp_mode == 1)
530 		pwrctrlpriv->power_mgnt = PS_MODE_ACTIVE ;
531 	else
532 		pwrctrlpriv->power_mgnt = padapter->registrypriv.power_mgnt;/*  PS_MODE_MIN; */
533 	pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt) ? true : false;
534 
535 	pwrctrlpriv->bFwCurrentInPSMode = false;
536 
537 	pwrctrlpriv->rpwm = 0;
538 	pwrctrlpriv->cpwm = PS_STATE_S4;
539 
540 	pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
541 	pwrctrlpriv->smart_ps = padapter->registrypriv.smart_ps;
542 	pwrctrlpriv->bcn_ant_mode = 0;
543 
544 	pwrctrlpriv->tog = 0x80;
545 
546 	pwrctrlpriv->btcoex_rfon = false;
547 
548 	_init_timer(&(pwrctrlpriv->pwr_state_check_timer), padapter->pnetdev, pwr_state_check_handler, (u8 *)padapter);
549 }
550 
rtw_set_ips_deny(struct adapter * padapter,u32 ms)551 inline void rtw_set_ips_deny(struct adapter *padapter, u32 ms)
552 {
553 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
554 	pwrpriv->ips_deny_time = jiffies + msecs_to_jiffies(ms);
555 }
556 
557 /*
558 * rtw_pwr_wakeup - Wake the NIC up from: 1)IPS. 2)USB autosuspend
559 * @adapter: pointer to struct adapter structure
560 * @ips_deffer_ms: the ms will prevent from falling into IPS after wakeup
561 * Return _SUCCESS or _FAIL
562 */
563 
_rtw_pwr_wakeup(struct adapter * padapter,u32 ips_deffer_ms,const char * caller)564 int _rtw_pwr_wakeup(struct adapter *padapter, u32 ips_deffer_ms, const char *caller)
565 {
566 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
567 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
568 	unsigned long expires;
569 	int ret = _SUCCESS;
570 
571 	expires = jiffies + msecs_to_jiffies(ips_deffer_ms);
572 	if (time_before(pwrpriv->ips_deny_time, expires))
573 		pwrpriv->ips_deny_time = jiffies + msecs_to_jiffies(ips_deffer_ms);
574 
575 {
576 	u32 start = jiffies;
577 	if (pwrpriv->ps_processing) {
578 		DBG_88E("%s wait ps_processing...\n", __func__);
579 		while (pwrpriv->ps_processing && rtw_get_passing_time_ms(start) <= 3000)
580 			msleep(10);
581 		if (pwrpriv->ps_processing)
582 			DBG_88E("%s wait ps_processing timeout\n", __func__);
583 		else
584 			DBG_88E("%s wait ps_processing done\n", __func__);
585 	}
586 }
587 
588 	/* System suspend is not allowed to wakeup */
589 	if ((!pwrpriv->bInternalAutoSuspend) && (pwrpriv->bInSuspend)) {
590 		ret = _FAIL;
591 		goto exit;
592 	}
593 
594 	/* block??? */
595 	if ((pwrpriv->bInternalAutoSuspend)  && (padapter->net_closed)) {
596 		ret = _FAIL;
597 		goto exit;
598 	}
599 
600 	/* I think this should be check in IPS, LPS, autosuspend functions... */
601 	if (check_fwstate(pmlmepriv, _FW_LINKED)) {
602 		ret = _SUCCESS;
603 		goto exit;
604 	}
605 	if (rf_off == pwrpriv->rf_pwrstate) {
606 		DBG_88E("%s call ips_leave....\n", __func__);
607 		if (_FAIL ==  ips_leave(padapter)) {
608 			DBG_88E("======> ips_leave fail.............\n");
609 			ret = _FAIL;
610 			goto exit;
611 		}
612 	}
613 
614 	/* TODO: the following checking need to be merged... */
615 	if (padapter->bDriverStopped || !padapter->bup ||
616 	    !padapter->hw_init_completed) {
617 		DBG_88E("%s: bDriverStopped=%d, bup=%d, hw_init_completed =%u\n"
618 			, caller
619 			, padapter->bDriverStopped
620 			, padapter->bup
621 			, padapter->hw_init_completed);
622 		ret = false;
623 		goto exit;
624 	}
625 
626 exit:
627 	expires = jiffies + msecs_to_jiffies(ips_deffer_ms);
628 	if (time_before(pwrpriv->ips_deny_time, expires))
629 		pwrpriv->ips_deny_time = jiffies + msecs_to_jiffies(ips_deffer_ms);
630 	return ret;
631 }
632 
rtw_pm_set_lps(struct adapter * padapter,u8 mode)633 int rtw_pm_set_lps(struct adapter *padapter, u8 mode)
634 {
635 	int	ret = 0;
636 	struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
637 
638 	if (mode < PS_MODE_NUM) {
639 		if (pwrctrlpriv->power_mgnt != mode) {
640 			if (PS_MODE_ACTIVE == mode)
641 				LeaveAllPowerSaveMode(padapter);
642 			else
643 				pwrctrlpriv->LpsIdleCount = 2;
644 			pwrctrlpriv->power_mgnt = mode;
645 			pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt) ? true : false;
646 		}
647 	} else {
648 		ret = -EINVAL;
649 	}
650 
651 	return ret;
652 }
653 
rtw_pm_set_ips(struct adapter * padapter,u8 mode)654 int rtw_pm_set_ips(struct adapter *padapter, u8 mode)
655 {
656 	struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
657 
658 	if (mode == IPS_NORMAL || mode == IPS_LEVEL_2) {
659 		rtw_ips_mode_req(pwrctrlpriv, mode);
660 		DBG_88E("%s %s\n", __func__, mode == IPS_NORMAL ? "IPS_NORMAL" : "IPS_LEVEL_2");
661 		return 0;
662 	} else if (mode == IPS_NONE) {
663 		rtw_ips_mode_req(pwrctrlpriv, mode);
664 		DBG_88E("%s %s\n", __func__, "IPS_NONE");
665 		if ((padapter->bSurpriseRemoved == 0) && (_FAIL == rtw_pwr_wakeup(padapter)))
666 			return -EFAULT;
667 	} else {
668 		return -EINVAL;
669 	}
670 	return 0;
671 }
672