1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2012  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  * The full GNU General Public License is included in this distribution in the
15  * file called LICENSE.
16  *
17  * Contact Information:
18  * wlanfae <wlanfae@realtek.com>
19  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20  * Hsinchu 300, Taiwan.
21  *
22  * Larry Finger <Larry.Finger@lwfinger.net>
23  *
24  *****************************************************************************/
25 
26 #include "wifi.h"
27 #include "base.h"
28 #include "ps.h"
29 #include <linux/export.h>
30 #include "btcoexist/rtl_btc.h"
31 
32 bool rtl_ps_enable_nic(struct ieee80211_hw *hw)
33 {
34 	struct rtl_priv *rtlpriv = rtl_priv(hw);
35 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
36 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
37 	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
38 
39 	/*<1> reset trx ring */
40 	if (rtlhal->interface == INTF_PCI)
41 		rtlpriv->intf_ops->reset_trx_ring(hw);
42 
43 	if (is_hal_stop(rtlhal))
44 		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
45 			 "Driver is already down!\n");
46 
47 	/*<2> Enable Adapter */
48 	if (rtlpriv->cfg->ops->hw_init(hw))
49 		return false;
50 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
51 			&rtlmac->retry_long);
52 	RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
53 
54 	rtlpriv->cfg->ops->switch_channel(hw);
55 	rtlpriv->cfg->ops->set_channel_access(hw);
56 	rtlpriv->cfg->ops->set_bw_mode(hw,
57 			cfg80211_get_chandef_type(&hw->conf.chandef));
58 
59 	/*<3> Enable Interrupt */
60 	rtlpriv->cfg->ops->enable_interrupt(hw);
61 
62 	/*<enable timer> */
63 	rtl_watch_dog_timer_callback(&rtlpriv->works.watchdog_timer);
64 
65 	return true;
66 }
67 EXPORT_SYMBOL(rtl_ps_enable_nic);
68 
69 bool rtl_ps_disable_nic(struct ieee80211_hw *hw)
70 {
71 	struct rtl_priv *rtlpriv = rtl_priv(hw);
72 
73 	/*<1> Stop all timer */
74 	rtl_deinit_deferred_work(hw, true);
75 
76 	/*<2> Disable Interrupt */
77 	rtlpriv->cfg->ops->disable_interrupt(hw);
78 	tasklet_kill(&rtlpriv->works.irq_tasklet);
79 
80 	/*<3> Disable Adapter */
81 	rtlpriv->cfg->ops->hw_disable(hw);
82 
83 	return true;
84 }
85 EXPORT_SYMBOL(rtl_ps_disable_nic);
86 
87 static bool rtl_ps_set_rf_state(struct ieee80211_hw *hw,
88 				enum rf_pwrstate state_toset,
89 				u32 changesource)
90 {
91 	struct rtl_priv *rtlpriv = rtl_priv(hw);
92 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
93 	enum rf_pwrstate rtstate;
94 	bool actionallowed = false;
95 	u16 rfwait_cnt = 0;
96 
97 	/*Only one thread can change
98 	 *the RF state at one time, and others
99 	 *should wait to be executed.
100 	 */
101 	while (true) {
102 		spin_lock(&rtlpriv->locks.rf_ps_lock);
103 		if (ppsc->rfchange_inprogress) {
104 			spin_unlock(&rtlpriv->locks.rf_ps_lock);
105 
106 			RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
107 				 "RF Change in progress! Wait to set..state_toset(%d).\n",
108 				  state_toset);
109 
110 			/* Set RF after the previous action is done.  */
111 			while (ppsc->rfchange_inprogress) {
112 				rfwait_cnt++;
113 				mdelay(1);
114 				/*Wait too long, return false to avoid
115 				 *to be stuck here.
116 				 */
117 				if (rfwait_cnt > 100)
118 					return false;
119 			}
120 		} else {
121 			ppsc->rfchange_inprogress = true;
122 			spin_unlock(&rtlpriv->locks.rf_ps_lock);
123 			break;
124 		}
125 	}
126 
127 	rtstate = ppsc->rfpwr_state;
128 
129 	switch (state_toset) {
130 	case ERFON:
131 		ppsc->rfoff_reason &= (~changesource);
132 
133 		if ((changesource == RF_CHANGE_BY_HW) &&
134 		    (ppsc->hwradiooff)) {
135 			ppsc->hwradiooff = false;
136 		}
137 
138 		if (!ppsc->rfoff_reason) {
139 			ppsc->rfoff_reason = 0;
140 			actionallowed = true;
141 		}
142 
143 		break;
144 
145 	case ERFOFF:
146 
147 		if ((changesource == RF_CHANGE_BY_HW) && !ppsc->hwradiooff) {
148 			ppsc->hwradiooff = true;
149 		}
150 
151 		ppsc->rfoff_reason |= changesource;
152 		actionallowed = true;
153 		break;
154 
155 	case ERFSLEEP:
156 		ppsc->rfoff_reason |= changesource;
157 		actionallowed = true;
158 		break;
159 
160 	default:
161 		pr_err("switch case %#x not processed\n", state_toset);
162 		break;
163 	}
164 
165 	if (actionallowed)
166 		rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset);
167 
168 	spin_lock(&rtlpriv->locks.rf_ps_lock);
169 	ppsc->rfchange_inprogress = false;
170 	spin_unlock(&rtlpriv->locks.rf_ps_lock);
171 
172 	return actionallowed;
173 }
174 
175 static void _rtl_ps_inactive_ps(struct ieee80211_hw *hw)
176 {
177 	struct rtl_priv *rtlpriv = rtl_priv(hw);
178 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
179 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
180 
181 	ppsc->swrf_processing = true;
182 
183 	if (ppsc->inactive_pwrstate == ERFON &&
184 	    rtlhal->interface == INTF_PCI) {
185 		if ((ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) &&
186 		    RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
187 		    rtlhal->interface == INTF_PCI) {
188 			rtlpriv->intf_ops->disable_aspm(hw);
189 			RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
190 		}
191 	}
192 
193 	rtl_ps_set_rf_state(hw, ppsc->inactive_pwrstate,
194 			    RF_CHANGE_BY_IPS);
195 
196 	if (ppsc->inactive_pwrstate == ERFOFF &&
197 	    rtlhal->interface == INTF_PCI) {
198 		if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM &&
199 		    !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
200 			rtlpriv->intf_ops->enable_aspm(hw);
201 			RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
202 		}
203 	}
204 
205 	ppsc->swrf_processing = false;
206 }
207 
208 void rtl_ips_nic_off_wq_callback(void *data)
209 {
210 	struct rtl_works *rtlworks =
211 	    container_of_dwork_rtl(data, struct rtl_works, ips_nic_off_wq);
212 	struct ieee80211_hw *hw = rtlworks->hw;
213 	struct rtl_priv *rtlpriv = rtl_priv(hw);
214 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
215 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
216 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
217 	enum rf_pwrstate rtstate;
218 
219 	if (mac->opmode != NL80211_IFTYPE_STATION) {
220 		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
221 			 "not station return\n");
222 		return;
223 	}
224 
225 	if (mac->p2p_in_use)
226 		return;
227 
228 	if (mac->link_state > MAC80211_NOLINK)
229 		return;
230 
231 	if (is_hal_stop(rtlhal))
232 		return;
233 
234 	if (rtlpriv->sec.being_setkey)
235 		return;
236 
237 	if (rtlpriv->cfg->ops->bt_coex_off_before_lps)
238 		rtlpriv->cfg->ops->bt_coex_off_before_lps(hw);
239 
240 	if (ppsc->inactiveps) {
241 		rtstate = ppsc->rfpwr_state;
242 
243 		/*
244 		 *Do not enter IPS in the following conditions:
245 		 *(1) RF is already OFF or Sleep
246 		 *(2) swrf_processing (indicates the IPS is still under going)
247 		 *(3) Connectted (only disconnected can trigger IPS)
248 		 *(4) IBSS (send Beacon)
249 		 *(5) AP mode (send Beacon)
250 		 *(6) monitor mode (rcv packet)
251 		 */
252 
253 		if (rtstate == ERFON &&
254 		    !ppsc->swrf_processing &&
255 		    (mac->link_state == MAC80211_NOLINK) &&
256 		    !mac->act_scanning) {
257 			RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
258 				 "IPSEnter(): Turn off RF\n");
259 
260 			ppsc->inactive_pwrstate = ERFOFF;
261 			ppsc->in_powersavemode = true;
262 
263 			/* call before RF off */
264 			if (rtlpriv->cfg->ops->get_btc_status())
265 				rtlpriv->btcoexist.btc_ops->btc_ips_notify(rtlpriv,
266 									ppsc->inactive_pwrstate);
267 
268 			/*rtl_pci_reset_trx_ring(hw); */
269 			_rtl_ps_inactive_ps(hw);
270 		}
271 	}
272 }
273 
274 void rtl_ips_nic_off(struct ieee80211_hw *hw)
275 {
276 	struct rtl_priv *rtlpriv = rtl_priv(hw);
277 
278 	/* because when link with ap, mac80211 will ask us
279 	 * to disable nic quickly after scan before linking,
280 	 * this will cause link failed, so we delay 100ms here
281 	 */
282 	queue_delayed_work(rtlpriv->works.rtl_wq,
283 			   &rtlpriv->works.ips_nic_off_wq, MSECS(100));
284 }
285 
286 /* NOTICE: any opmode should exc nic_on, or disable without
287  * nic_on may something wrong, like adhoc TP
288  */
289 void rtl_ips_nic_on(struct ieee80211_hw *hw)
290 {
291 	struct rtl_priv *rtlpriv = rtl_priv(hw);
292 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
293 	enum rf_pwrstate rtstate;
294 
295 	cancel_delayed_work_sync(&rtlpriv->works.ips_nic_off_wq);
296 
297 	mutex_lock(&rtlpriv->locks.ips_mutex);
298 	if (ppsc->inactiveps) {
299 		rtstate = ppsc->rfpwr_state;
300 
301 		if (rtstate != ERFON &&
302 		    !ppsc->swrf_processing &&
303 		    ppsc->rfoff_reason <= RF_CHANGE_BY_IPS) {
304 
305 			ppsc->inactive_pwrstate = ERFON;
306 			ppsc->in_powersavemode = false;
307 			_rtl_ps_inactive_ps(hw);
308 			/* call after RF on */
309 			if (rtlpriv->cfg->ops->get_btc_status())
310 				rtlpriv->btcoexist.btc_ops->btc_ips_notify(rtlpriv,
311 									ppsc->inactive_pwrstate);
312 		}
313 	}
314 	mutex_unlock(&rtlpriv->locks.ips_mutex);
315 }
316 EXPORT_SYMBOL_GPL(rtl_ips_nic_on);
317 
318 /*for FW LPS*/
319 
320 /*
321  *Determine if we can set Fw into PS mode
322  *in current condition.Return TRUE if it
323  *can enter PS mode.
324  */
325 static bool rtl_get_fwlps_doze(struct ieee80211_hw *hw)
326 {
327 	struct rtl_priv *rtlpriv = rtl_priv(hw);
328 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
329 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
330 	u32 ps_timediff;
331 
332 	ps_timediff = jiffies_to_msecs(jiffies -
333 				       ppsc->last_delaylps_stamp_jiffies);
334 
335 	if (ps_timediff < 2000) {
336 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
337 			 "Delay enter Fw LPS for DHCP, ARP, or EAPOL exchanging state\n");
338 		return false;
339 	}
340 
341 	if (mac->link_state != MAC80211_LINKED)
342 		return false;
343 
344 	if (mac->opmode == NL80211_IFTYPE_ADHOC)
345 		return false;
346 
347 	return true;
348 }
349 
350 /* Change current and default preamble mode.*/
351 void rtl_lps_set_psmode(struct ieee80211_hw *hw, u8 rt_psmode)
352 {
353 	struct rtl_priv *rtlpriv = rtl_priv(hw);
354 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
355 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
356 	bool enter_fwlps;
357 
358 	if (mac->opmode == NL80211_IFTYPE_ADHOC)
359 		return;
360 
361 	if (mac->link_state != MAC80211_LINKED)
362 		return;
363 
364 	if (ppsc->dot11_psmode == rt_psmode && rt_psmode == EACTIVE)
365 		return;
366 
367 	/* Update power save mode configured. */
368 	ppsc->dot11_psmode = rt_psmode;
369 
370 	/*
371 	 *<FW control LPS>
372 	 *1. Enter PS mode
373 	 *   Set RPWM to Fw to turn RF off and send H2C fw_pwrmode
374 	 *   cmd to set Fw into PS mode.
375 	 *2. Leave PS mode
376 	 *   Send H2C fw_pwrmode cmd to Fw to set Fw into Active
377 	 *   mode and set RPWM to turn RF on.
378 	 */
379 
380 	if ((ppsc->fwctrl_lps) && ppsc->report_linked) {
381 		if (ppsc->dot11_psmode == EACTIVE) {
382 			RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
383 				 "FW LPS leave ps_mode:%x\n",
384 				  FW_PS_ACTIVE_MODE);
385 			enter_fwlps = false;
386 			ppsc->pwr_mode = FW_PS_ACTIVE_MODE;
387 			ppsc->smart_ps = 0;
388 			rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_FW_LPS_ACTION,
389 						      (u8 *)(&enter_fwlps));
390 			if (ppsc->p2p_ps_info.opp_ps)
391 				rtl_p2p_ps_cmd(hw , P2P_PS_ENABLE);
392 
393 			if (rtlpriv->cfg->ops->get_btc_status())
394 				rtlpriv->btcoexist.btc_ops->btc_lps_notify(rtlpriv, rt_psmode);
395 		} else {
396 			if (rtl_get_fwlps_doze(hw)) {
397 				RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
398 					 "FW LPS enter ps_mode:%x\n",
399 					 ppsc->fwctrl_psmode);
400 				if (rtlpriv->cfg->ops->get_btc_status())
401 					rtlpriv->btcoexist.btc_ops->btc_lps_notify(rtlpriv, rt_psmode);
402 				enter_fwlps = true;
403 				ppsc->pwr_mode = ppsc->fwctrl_psmode;
404 				ppsc->smart_ps = 2;
405 				rtlpriv->cfg->ops->set_hw_reg(hw,
406 							HW_VAR_FW_LPS_ACTION,
407 							(u8 *)(&enter_fwlps));
408 
409 			} else {
410 				/* Reset the power save related parameters. */
411 				ppsc->dot11_psmode = EACTIVE;
412 			}
413 		}
414 	}
415 }
416 
417 /* Interrupt safe routine to enter the leisure power save mode.*/
418 static void rtl_lps_enter_core(struct ieee80211_hw *hw)
419 {
420 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
421 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
422 	struct rtl_priv *rtlpriv = rtl_priv(hw);
423 
424 	if (!ppsc->fwctrl_lps)
425 		return;
426 
427 	if (rtlpriv->sec.being_setkey)
428 		return;
429 
430 	if (rtlpriv->link_info.busytraffic)
431 		return;
432 
433 	/*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
434 	if (mac->cnt_after_linked < 5)
435 		return;
436 
437 	if (mac->opmode == NL80211_IFTYPE_ADHOC)
438 		return;
439 
440 	if (mac->link_state != MAC80211_LINKED)
441 		return;
442 
443 	mutex_lock(&rtlpriv->locks.lps_mutex);
444 
445 	/* Don't need to check (ppsc->dot11_psmode == EACTIVE), because
446 	 * bt_ccoexist may ask to enter lps.
447 	 * In normal case, this constraint move to rtl_lps_set_psmode().
448 	 */
449 	RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
450 		 "Enter 802.11 power save mode...\n");
451 	rtl_lps_set_psmode(hw, EAUTOPS);
452 
453 	mutex_unlock(&rtlpriv->locks.lps_mutex);
454 }
455 
456 /* Interrupt safe routine to leave the leisure power save mode.*/
457 static void rtl_lps_leave_core(struct ieee80211_hw *hw)
458 {
459 	struct rtl_priv *rtlpriv = rtl_priv(hw);
460 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
461 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
462 
463 	mutex_lock(&rtlpriv->locks.lps_mutex);
464 
465 	if (ppsc->fwctrl_lps) {
466 		if (ppsc->dot11_psmode != EACTIVE) {
467 
468 			/*FIX ME */
469 			/*rtlpriv->cfg->ops->enable_interrupt(hw); */
470 
471 			if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
472 			    RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
473 			    rtlhal->interface == INTF_PCI) {
474 				rtlpriv->intf_ops->disable_aspm(hw);
475 				RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
476 			}
477 
478 			RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
479 				 "Busy Traffic,Leave 802.11 power save..\n");
480 
481 			rtl_lps_set_psmode(hw, EACTIVE);
482 		}
483 	}
484 	mutex_unlock(&rtlpriv->locks.lps_mutex);
485 }
486 
487 /* For sw LPS*/
488 void rtl_swlps_beacon(struct ieee80211_hw *hw, void *data, unsigned int len)
489 {
490 	struct rtl_priv *rtlpriv = rtl_priv(hw);
491 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
492 	struct ieee80211_hdr *hdr = data;
493 	struct ieee80211_tim_ie *tim_ie;
494 	u8 *tim;
495 	u8 tim_len;
496 	bool u_buffed;
497 	bool m_buffed;
498 
499 	if (mac->opmode != NL80211_IFTYPE_STATION)
500 		return;
501 
502 	if (!rtlpriv->psc.swctrl_lps)
503 		return;
504 
505 	if (rtlpriv->mac80211.link_state != MAC80211_LINKED)
506 		return;
507 
508 	if (!rtlpriv->psc.sw_ps_enabled)
509 		return;
510 
511 	if (rtlpriv->psc.fwctrl_lps)
512 		return;
513 
514 	if (likely(!(hw->conf.flags & IEEE80211_CONF_PS)))
515 		return;
516 
517 	/* check if this really is a beacon */
518 	if (!ieee80211_is_beacon(hdr->frame_control))
519 		return;
520 
521 	/* min. beacon length + FCS_LEN */
522 	if (len <= 40 + FCS_LEN)
523 		return;
524 
525 	/* and only beacons from the associated BSSID, please */
526 	if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
527 		return;
528 
529 	rtlpriv->psc.last_beacon = jiffies;
530 
531 	tim = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_TIM);
532 	if (!tim)
533 		return;
534 
535 	if (tim[1] < sizeof(*tim_ie))
536 		return;
537 
538 	tim_len = tim[1];
539 	tim_ie = (struct ieee80211_tim_ie *) &tim[2];
540 
541 	if (!WARN_ON_ONCE(!hw->conf.ps_dtim_period))
542 		rtlpriv->psc.dtim_counter = tim_ie->dtim_count;
543 
544 	/* Check whenever the PHY can be turned off again. */
545 
546 	/* 1. What about buffered unicast traffic for our AID? */
547 	u_buffed = ieee80211_check_tim(tim_ie, tim_len,
548 				       rtlpriv->mac80211.assoc_id);
549 
550 	/* 2. Maybe the AP wants to send multicast/broadcast data? */
551 	m_buffed = tim_ie->bitmap_ctrl & 0x01;
552 	rtlpriv->psc.multi_buffered = m_buffed;
553 
554 	/* unicast will process by mac80211 through
555 	 * set ~IEEE80211_CONF_PS, So we just check
556 	 * multicast frames here */
557 	if (!m_buffed) {
558 		/* back to low-power land. and delay is
559 		 * prevent null power save frame tx fail */
560 		queue_delayed_work(rtlpriv->works.rtl_wq,
561 				   &rtlpriv->works.ps_work, MSECS(5));
562 	} else {
563 		RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
564 			 "u_bufferd: %x, m_buffered: %x\n", u_buffed, m_buffed);
565 	}
566 }
567 EXPORT_SYMBOL_GPL(rtl_swlps_beacon);
568 
569 void rtl_swlps_rf_awake(struct ieee80211_hw *hw)
570 {
571 	struct rtl_priv *rtlpriv = rtl_priv(hw);
572 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
573 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
574 
575 	if (!rtlpriv->psc.swctrl_lps)
576 		return;
577 	if (mac->link_state != MAC80211_LINKED)
578 		return;
579 
580 	if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
581 	    RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
582 		rtlpriv->intf_ops->disable_aspm(hw);
583 		RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
584 	}
585 
586 	mutex_lock(&rtlpriv->locks.lps_mutex);
587 	rtl_ps_set_rf_state(hw, ERFON, RF_CHANGE_BY_PS);
588 	mutex_unlock(&rtlpriv->locks.lps_mutex);
589 }
590 
591 void rtl_swlps_rfon_wq_callback(void *data)
592 {
593 	struct rtl_works *rtlworks =
594 	    container_of_dwork_rtl(data, struct rtl_works, ps_rfon_wq);
595 	struct ieee80211_hw *hw = rtlworks->hw;
596 
597 	rtl_swlps_rf_awake(hw);
598 }
599 
600 void rtl_swlps_rf_sleep(struct ieee80211_hw *hw)
601 {
602 	struct rtl_priv *rtlpriv = rtl_priv(hw);
603 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
604 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
605 	u8 sleep_intv;
606 
607 	if (!rtlpriv->psc.sw_ps_enabled)
608 		return;
609 
610 	if ((rtlpriv->sec.being_setkey) ||
611 	    (mac->opmode == NL80211_IFTYPE_ADHOC))
612 		return;
613 
614 	/*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
615 	if ((mac->link_state != MAC80211_LINKED) || (mac->cnt_after_linked < 5))
616 		return;
617 
618 	if (rtlpriv->link_info.busytraffic)
619 		return;
620 
621 	spin_lock(&rtlpriv->locks.rf_ps_lock);
622 	if (rtlpriv->psc.rfchange_inprogress) {
623 		spin_unlock(&rtlpriv->locks.rf_ps_lock);
624 		return;
625 	}
626 	spin_unlock(&rtlpriv->locks.rf_ps_lock);
627 
628 	mutex_lock(&rtlpriv->locks.lps_mutex);
629 	rtl_ps_set_rf_state(hw, ERFSLEEP, RF_CHANGE_BY_PS);
630 	mutex_unlock(&rtlpriv->locks.lps_mutex);
631 
632 	if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM &&
633 	    !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
634 		rtlpriv->intf_ops->enable_aspm(hw);
635 		RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
636 	}
637 
638 	/* here is power save alg, when this beacon is DTIM
639 	 * we will set sleep time to dtim_period * n;
640 	 * when this beacon is not DTIM, we will set sleep
641 	 * time to sleep_intv = rtlpriv->psc.dtim_counter or
642 	 * MAX_SW_LPS_SLEEP_INTV(default set to 5) */
643 
644 	if (rtlpriv->psc.dtim_counter == 0) {
645 		if (hw->conf.ps_dtim_period == 1)
646 			sleep_intv = hw->conf.ps_dtim_period * 2;
647 		else
648 			sleep_intv = hw->conf.ps_dtim_period;
649 	} else {
650 		sleep_intv = rtlpriv->psc.dtim_counter;
651 	}
652 
653 	if (sleep_intv > MAX_SW_LPS_SLEEP_INTV)
654 		sleep_intv = MAX_SW_LPS_SLEEP_INTV;
655 
656 	/* this print should always be dtim_conter = 0 &
657 	 * sleep  = dtim_period, that meaons, we should
658 	 * awake before every dtim */
659 	RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
660 		 "dtim_counter:%x will sleep :%d beacon_intv\n",
661 		  rtlpriv->psc.dtim_counter, sleep_intv);
662 
663 	/* we tested that 40ms is enough for sw & hw sw delay */
664 	queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.ps_rfon_wq,
665 			MSECS(sleep_intv * mac->vif->bss_conf.beacon_int - 40));
666 }
667 
668 void rtl_lps_change_work_callback(struct work_struct *work)
669 {
670 	struct rtl_works *rtlworks =
671 	    container_of(work, struct rtl_works, lps_change_work);
672 	struct ieee80211_hw *hw = rtlworks->hw;
673 	struct rtl_priv *rtlpriv = rtl_priv(hw);
674 
675 	if (rtlpriv->enter_ps)
676 		rtl_lps_enter_core(hw);
677 	else
678 		rtl_lps_leave_core(hw);
679 }
680 EXPORT_SYMBOL_GPL(rtl_lps_change_work_callback);
681 
682 void rtl_lps_enter(struct ieee80211_hw *hw)
683 {
684 	struct rtl_priv *rtlpriv = rtl_priv(hw);
685 
686 	if (!in_interrupt())
687 		return rtl_lps_enter_core(hw);
688 	rtlpriv->enter_ps = true;
689 	schedule_work(&rtlpriv->works.lps_change_work);
690 }
691 EXPORT_SYMBOL_GPL(rtl_lps_enter);
692 
693 void rtl_lps_leave(struct ieee80211_hw *hw)
694 {
695 	struct rtl_priv *rtlpriv = rtl_priv(hw);
696 
697 	if (!in_interrupt())
698 		return rtl_lps_leave_core(hw);
699 	rtlpriv->enter_ps = false;
700 	schedule_work(&rtlpriv->works.lps_change_work);
701 }
702 EXPORT_SYMBOL_GPL(rtl_lps_leave);
703 
704 void rtl_swlps_wq_callback(void *data)
705 {
706 	struct rtl_works *rtlworks = container_of_dwork_rtl(data,
707 				     struct rtl_works,
708 				     ps_work);
709 	struct ieee80211_hw *hw = rtlworks->hw;
710 	struct rtl_priv *rtlpriv = rtl_priv(hw);
711 	bool ps = false;
712 
713 	ps = (hw->conf.flags & IEEE80211_CONF_PS);
714 
715 	/* we can sleep after ps null send ok */
716 	if (rtlpriv->psc.state_inap) {
717 		rtl_swlps_rf_sleep(hw);
718 
719 		if (rtlpriv->psc.state && !ps) {
720 			rtlpriv->psc.sleep_ms = jiffies_to_msecs(jiffies -
721 						 rtlpriv->psc.last_action);
722 		}
723 
724 		if (ps)
725 			rtlpriv->psc.last_slept = jiffies;
726 
727 		rtlpriv->psc.last_action = jiffies;
728 		rtlpriv->psc.state = ps;
729 	}
730 }
731 
732 static void rtl_p2p_noa_ie(struct ieee80211_hw *hw, void *data,
733 			   unsigned int len)
734 {
735 	struct rtl_priv *rtlpriv = rtl_priv(hw);
736 	struct ieee80211_mgmt *mgmt = data;
737 	struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
738 	u8 *pos, *end, *ie;
739 	u16 noa_len;
740 	static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
741 	u8 noa_num, index , i, noa_index = 0;
742 	bool find_p2p_ie = false , find_p2p_ps_ie = false;
743 	pos = (u8 *)mgmt->u.beacon.variable;
744 	end = data + len;
745 	ie = NULL;
746 
747 	while (pos + 1 < end) {
748 		if (pos + 2 + pos[1] > end)
749 			return;
750 
751 		if (pos[0] == 221 && pos[1] > 4) {
752 			if (memcmp(&pos[2], p2p_oui_ie_type, 4) == 0) {
753 				ie = pos + 2+4;
754 				break;
755 			}
756 		}
757 		pos += 2 + pos[1];
758 	}
759 
760 	if (ie == NULL)
761 		return;
762 	find_p2p_ie = true;
763 	/*to find noa ie*/
764 	while (ie + 1 < end) {
765 		noa_len = READEF2BYTE((__le16 *)&ie[1]);
766 		if (ie + 3 + ie[1] > end)
767 			return;
768 
769 		if (ie[0] == 12) {
770 			find_p2p_ps_ie = true;
771 			if ((noa_len - 2) % 13 != 0) {
772 				RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
773 					 "P2P notice of absence: invalid length.%d\n",
774 					 noa_len);
775 				return;
776 			} else {
777 				noa_num = (noa_len - 2) / 13;
778 			}
779 			noa_index = ie[3];
780 			if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
781 			    P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
782 				RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
783 					 "update NOA ie.\n");
784 				p2pinfo->noa_index = noa_index;
785 				p2pinfo->opp_ps = (ie[4] >> 7);
786 				p2pinfo->ctwindow = ie[4] & 0x7F;
787 				p2pinfo->noa_num = noa_num;
788 				index = 5;
789 				for (i = 0; i < noa_num; i++) {
790 					p2pinfo->noa_count_type[i] =
791 							READEF1BYTE(ie+index);
792 					index += 1;
793 					p2pinfo->noa_duration[i] =
794 						 READEF4BYTE((__le32 *)ie+index);
795 					index += 4;
796 					p2pinfo->noa_interval[i] =
797 						 READEF4BYTE((__le32 *)ie+index);
798 					index += 4;
799 					p2pinfo->noa_start_time[i] =
800 						 READEF4BYTE((__le32 *)ie+index);
801 					index += 4;
802 				}
803 
804 				if (p2pinfo->opp_ps == 1) {
805 					p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
806 					/* Driver should wait LPS entering
807 					 * CTWindow
808 					 */
809 					if (rtlpriv->psc.fw_current_inpsmode)
810 						rtl_p2p_ps_cmd(hw,
811 							       P2P_PS_ENABLE);
812 				} else if (p2pinfo->noa_num > 0) {
813 					p2pinfo->p2p_ps_mode = P2P_PS_NOA;
814 					rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
815 				} else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
816 					rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
817 				}
818 			}
819 			break;
820 		}
821 		ie += 3 + noa_len;
822 	}
823 
824 	if (find_p2p_ie == true) {
825 		if ((p2pinfo->p2p_ps_mode > P2P_PS_NONE) &&
826 		    (find_p2p_ps_ie == false))
827 			rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
828 	}
829 }
830 
831 static void rtl_p2p_action_ie(struct ieee80211_hw *hw, void *data,
832 			      unsigned int len)
833 {
834 	struct rtl_priv *rtlpriv = rtl_priv(hw);
835 	struct ieee80211_mgmt *mgmt = data;
836 	struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
837 	u8 noa_num, index , i , noa_index = 0;
838 	u8 *pos, *end, *ie;
839 	u16 noa_len;
840 	static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
841 
842 	pos = (u8 *)&mgmt->u.action.category;
843 	end = data + len;
844 	ie = NULL;
845 
846 	if (pos[0] == 0x7f) {
847 		if (memcmp(&pos[1], p2p_oui_ie_type, 4) == 0)
848 			ie = pos + 3+4;
849 	}
850 
851 	if (ie == NULL)
852 		return;
853 
854 	RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "action frame find P2P IE.\n");
855 	/*to find noa ie*/
856 	while (ie + 1 < end) {
857 		noa_len = READEF2BYTE((__le16 *)&ie[1]);
858 		if (ie + 3 + ie[1] > end)
859 			return;
860 
861 		if (ie[0] == 12) {
862 			RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "find NOA IE.\n");
863 			RT_PRINT_DATA(rtlpriv, COMP_FW, DBG_LOUD, "noa ie ",
864 				      ie, noa_len);
865 			if ((noa_len - 2) % 13 != 0) {
866 				RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
867 					 "P2P notice of absence: invalid length.%d\n",
868 					 noa_len);
869 				return;
870 			} else {
871 				noa_num = (noa_len - 2) / 13;
872 			}
873 			noa_index = ie[3];
874 			if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
875 			    P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
876 				p2pinfo->noa_index = noa_index;
877 				p2pinfo->opp_ps = (ie[4] >> 7);
878 				p2pinfo->ctwindow = ie[4] & 0x7F;
879 				p2pinfo->noa_num = noa_num;
880 				index = 5;
881 				for (i = 0; i < noa_num; i++) {
882 					p2pinfo->noa_count_type[i] =
883 							READEF1BYTE(ie+index);
884 					index += 1;
885 					p2pinfo->noa_duration[i] =
886 							 READEF4BYTE((__le32 *)ie+index);
887 					index += 4;
888 					p2pinfo->noa_interval[i] =
889 							 READEF4BYTE((__le32 *)ie+index);
890 					index += 4;
891 					p2pinfo->noa_start_time[i] =
892 							 READEF4BYTE((__le32 *)ie+index);
893 					index += 4;
894 				}
895 
896 				if (p2pinfo->opp_ps == 1) {
897 					p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
898 					/* Driver should wait LPS entering
899 					 * CTWindow
900 					 */
901 					if (rtlpriv->psc.fw_current_inpsmode)
902 						rtl_p2p_ps_cmd(hw,
903 							       P2P_PS_ENABLE);
904 				} else if (p2pinfo->noa_num > 0) {
905 					p2pinfo->p2p_ps_mode = P2P_PS_NOA;
906 					rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
907 				} else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
908 					rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
909 				}
910 			}
911 			break;
912 		}
913 		ie += 3 + noa_len;
914 	}
915 }
916 
917 void rtl_p2p_ps_cmd(struct ieee80211_hw *hw , u8 p2p_ps_state)
918 {
919 	struct rtl_priv *rtlpriv = rtl_priv(hw);
920 	struct rtl_ps_ctl *rtlps = rtl_psc(rtl_priv(hw));
921 	struct rtl_p2p_ps_info  *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
922 
923 	RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, " p2p state %x\n" , p2p_ps_state);
924 	switch (p2p_ps_state) {
925 	case P2P_PS_DISABLE:
926 		p2pinfo->p2p_ps_state = p2p_ps_state;
927 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
928 					      &p2p_ps_state);
929 		p2pinfo->noa_index = 0;
930 		p2pinfo->ctwindow = 0;
931 		p2pinfo->opp_ps = 0;
932 		p2pinfo->noa_num = 0;
933 		p2pinfo->p2p_ps_mode = P2P_PS_NONE;
934 		if (rtlps->fw_current_inpsmode) {
935 			if (rtlps->smart_ps == 0) {
936 				rtlps->smart_ps = 2;
937 				rtlpriv->cfg->ops->set_hw_reg(hw,
938 					 HW_VAR_H2C_FW_PWRMODE,
939 					 &rtlps->pwr_mode);
940 			}
941 
942 		}
943 		break;
944 	case P2P_PS_ENABLE:
945 		if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
946 			p2pinfo->p2p_ps_state = p2p_ps_state;
947 
948 			if (p2pinfo->ctwindow > 0) {
949 				if (rtlps->smart_ps != 0) {
950 					rtlps->smart_ps = 0;
951 					rtlpriv->cfg->ops->set_hw_reg(hw,
952 						 HW_VAR_H2C_FW_PWRMODE,
953 						 &rtlps->pwr_mode);
954 				}
955 			}
956 			rtlpriv->cfg->ops->set_hw_reg(hw,
957 				 HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
958 				 &p2p_ps_state);
959 
960 		}
961 		break;
962 	case P2P_PS_SCAN:
963 	case P2P_PS_SCAN_DONE:
964 	case P2P_PS_ALLSTASLEEP:
965 		if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
966 			p2pinfo->p2p_ps_state = p2p_ps_state;
967 			rtlpriv->cfg->ops->set_hw_reg(hw,
968 				 HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
969 				 &p2p_ps_state);
970 		}
971 		break;
972 	default:
973 		break;
974 	}
975 	RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
976 		 "ctwindow %x oppps %x\n",
977 		 p2pinfo->ctwindow , p2pinfo->opp_ps);
978 	RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
979 		 "count %x duration %x index %x interval %x start time %x noa num %x\n",
980 		 p2pinfo->noa_count_type[0],
981 		 p2pinfo->noa_duration[0],
982 		 p2pinfo->noa_index,
983 		 p2pinfo->noa_interval[0],
984 		 p2pinfo->noa_start_time[0],
985 		 p2pinfo->noa_num);
986 	RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "end\n");
987 }
988 
989 void rtl_p2p_info(struct ieee80211_hw *hw, void *data, unsigned int len)
990 {
991 	struct rtl_priv *rtlpriv = rtl_priv(hw);
992 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
993 	struct ieee80211_hdr *hdr = data;
994 
995 	if (!mac->p2p)
996 		return;
997 	if (mac->link_state != MAC80211_LINKED)
998 		return;
999 	/* min. beacon length + FCS_LEN */
1000 	if (len <= 40 + FCS_LEN)
1001 		return;
1002 
1003 	/* and only beacons from the associated BSSID, please */
1004 	if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
1005 		return;
1006 
1007 	/* check if this really is a beacon */
1008 	if (!(ieee80211_is_beacon(hdr->frame_control) ||
1009 	      ieee80211_is_probe_resp(hdr->frame_control) ||
1010 	      ieee80211_is_action(hdr->frame_control)))
1011 		return;
1012 
1013 	if (ieee80211_is_action(hdr->frame_control))
1014 		rtl_p2p_action_ie(hw , data , len - FCS_LEN);
1015 	else
1016 		rtl_p2p_noa_ie(hw , data , len - FCS_LEN);
1017 }
1018 EXPORT_SYMBOL_GPL(rtl_p2p_info);
1019