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