1 /******************************************************************************
3 * Copyright(c) 2009-2012 Realtek Corporation.
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.
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
14 * The full GNU General Public License is included in this distribution in the
15 * file called LICENSE.
17 * Contact Information:
18 * wlanfae <wlanfae@realtek.com>
19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20 * Hsinchu 300, Taiwan.
22 * Larry Finger <Larry.Finger@lwfinger.net>
24 *****************************************************************************/
29 #include <linux/export.h>
30 #include "btcoexist/rtl_btc.h"
32 bool rtl_ps_enable_nic(struct ieee80211_hw *hw)
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));
38 /*<1> reset trx ring */
39 if (rtlhal->interface == INTF_PCI)
40 rtlpriv->intf_ops->reset_trx_ring(hw);
42 if (is_hal_stop(rtlhal))
43 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
44 "Driver is already down!\n");
46 /*<2> Enable Adapter */
47 if (rtlpriv->cfg->ops->hw_init(hw))
49 RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
51 /*<3> Enable Interrupt */
52 rtlpriv->cfg->ops->enable_interrupt(hw);
55 rtl_watch_dog_timer_callback((unsigned long)hw);
59 EXPORT_SYMBOL(rtl_ps_enable_nic);
61 bool rtl_ps_disable_nic(struct ieee80211_hw *hw)
63 struct rtl_priv *rtlpriv = rtl_priv(hw);
65 /*<1> Stop all timer */
66 rtl_deinit_deferred_work(hw);
68 /*<2> Disable Interrupt */
69 rtlpriv->cfg->ops->disable_interrupt(hw);
70 tasklet_kill(&rtlpriv->works.irq_tasklet);
72 /*<3> Disable Adapter */
73 rtlpriv->cfg->ops->hw_disable(hw);
77 EXPORT_SYMBOL(rtl_ps_disable_nic);
79 bool rtl_ps_set_rf_state(struct ieee80211_hw *hw,
80 enum rf_pwrstate state_toset,
81 u32 changesource, bool protect_or_not)
83 struct rtl_priv *rtlpriv = rtl_priv(hw);
84 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
85 enum rf_pwrstate rtstate;
86 bool actionallowed = false;
92 /*Only one thread can change
93 *the RF state at one time, and others
94 *should wait to be executed.
97 spin_lock(&rtlpriv->locks.rf_ps_lock);
98 if (ppsc->rfchange_inprogress) {
99 spin_unlock(&rtlpriv->locks.rf_ps_lock);
101 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
102 "RF Change in progress! Wait to set..state_toset(%d).\n",
105 /* Set RF after the previous action is done. */
106 while (ppsc->rfchange_inprogress) {
109 /*Wait too long, return false to avoid
112 if (rfwait_cnt > 100)
116 ppsc->rfchange_inprogress = true;
117 spin_unlock(&rtlpriv->locks.rf_ps_lock);
123 rtstate = ppsc->rfpwr_state;
125 switch (state_toset) {
127 ppsc->rfoff_reason &= (~changesource);
129 if ((changesource == RF_CHANGE_BY_HW) &&
130 (ppsc->hwradiooff)) {
131 ppsc->hwradiooff = false;
134 if (!ppsc->rfoff_reason) {
135 ppsc->rfoff_reason = 0;
136 actionallowed = true;
143 if ((changesource == RF_CHANGE_BY_HW) && !ppsc->hwradiooff) {
144 ppsc->hwradiooff = true;
147 ppsc->rfoff_reason |= changesource;
148 actionallowed = true;
152 ppsc->rfoff_reason |= changesource;
153 actionallowed = true;
157 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
158 "switch case not processed\n");
163 rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset);
165 if (!protect_or_not) {
166 spin_lock(&rtlpriv->locks.rf_ps_lock);
167 ppsc->rfchange_inprogress = false;
168 spin_unlock(&rtlpriv->locks.rf_ps_lock);
171 return actionallowed;
173 EXPORT_SYMBOL(rtl_ps_set_rf_state);
175 static void _rtl_ps_inactive_ps(struct ieee80211_hw *hw)
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));
181 ppsc->swrf_processing = true;
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);
193 rtl_ps_set_rf_state(hw, ppsc->inactive_pwrstate,
194 RF_CHANGE_BY_IPS, false);
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);
205 ppsc->swrf_processing = false;
208 void rtl_ips_nic_off_wq_callback(void *data)
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;
219 if (mac->opmode != NL80211_IFTYPE_STATION) {
220 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
221 "not station return\n");
228 if (mac->link_state > MAC80211_NOLINK)
231 if (is_hal_stop(rtlhal))
234 if (rtlpriv->sec.being_setkey)
237 if (rtlpriv->cfg->ops->bt_coex_off_before_lps)
238 rtlpriv->cfg->ops->bt_coex_off_before_lps(hw);
240 if (ppsc->inactiveps) {
241 rtstate = ppsc->rfpwr_state;
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)
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");
260 ppsc->inactive_pwrstate = ERFOFF;
261 ppsc->in_powersavemode = true;
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);
268 /*rtl_pci_reset_trx_ring(hw); */
269 _rtl_ps_inactive_ps(hw);
274 void rtl_ips_nic_off(struct ieee80211_hw *hw)
276 struct rtl_priv *rtlpriv = rtl_priv(hw);
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
282 queue_delayed_work(rtlpriv->works.rtl_wq,
283 &rtlpriv->works.ips_nic_off_wq, MSECS(100));
286 /* NOTICE: any opmode should exc nic_on, or disable without
287 * nic_on may something wrong, like adhoc TP
289 void rtl_ips_nic_on(struct ieee80211_hw *hw)
291 struct rtl_priv *rtlpriv = rtl_priv(hw);
292 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
293 enum rf_pwrstate rtstate;
295 cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
297 spin_lock(&rtlpriv->locks.ips_lock);
298 if (ppsc->inactiveps) {
299 rtstate = ppsc->rfpwr_state;
301 if (rtstate != ERFON &&
302 !ppsc->swrf_processing &&
303 ppsc->rfoff_reason <= RF_CHANGE_BY_IPS) {
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);
314 spin_unlock(&rtlpriv->locks.ips_lock);
316 EXPORT_SYMBOL_GPL(rtl_ips_nic_on);
321 *Determine if we can set Fw into PS mode
322 *in current condition.Return TRUE if it
325 static bool rtl_get_fwlps_doze(struct ieee80211_hw *hw)
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));
332 ps_timediff = jiffies_to_msecs(jiffies -
333 ppsc->last_delaylps_stamp_jiffies);
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");
341 if (mac->link_state != MAC80211_LINKED)
344 if (mac->opmode == NL80211_IFTYPE_ADHOC)
350 /* Change current and default preamble mode.*/
351 void rtl_lps_set_psmode(struct ieee80211_hw *hw, u8 rt_psmode)
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));
358 if (mac->opmode == NL80211_IFTYPE_ADHOC)
361 if (mac->link_state != MAC80211_LINKED)
364 if (ppsc->dot11_psmode == rt_psmode)
367 /* Update power save mode configured. */
368 ppsc->dot11_psmode = rt_psmode;
373 * Set RPWM to Fw to turn RF off and send H2C fw_pwrmode
374 * cmd to set Fw into PS mode.
376 * Send H2C fw_pwrmode cmd to Fw to set Fw into Active
377 * mode and set RPWM to turn RF on.
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",
386 ppsc->pwr_mode = FW_PS_ACTIVE_MODE;
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);
393 if (rtlpriv->cfg->ops->get_btc_status())
394 rtlpriv->btcoexist.btc_ops->btc_lps_notify(rtlpriv, rt_psmode);
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);
403 ppsc->pwr_mode = ppsc->fwctrl_psmode;
405 rtlpriv->cfg->ops->set_hw_reg(hw,
406 HW_VAR_FW_LPS_ACTION,
407 (u8 *)(&enter_fwlps));
410 /* Reset the power save related parameters. */
411 ppsc->dot11_psmode = EACTIVE;
417 /* Interrupt safe routine to enter the leisure power save mode.*/
418 static void rtl_lps_enter_core(struct ieee80211_hw *hw)
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);
425 if (!ppsc->fwctrl_lps)
428 if (rtlpriv->sec.being_setkey)
431 if (rtlpriv->link_info.busytraffic)
434 /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
435 if (mac->cnt_after_linked < 5)
438 if (mac->opmode == NL80211_IFTYPE_ADHOC)
441 if (mac->link_state != MAC80211_LINKED)
444 spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag);
446 /* Idle for a while if we connect to AP a while ago. */
447 if (mac->cnt_after_linked >= 2) {
448 if (ppsc->dot11_psmode == EACTIVE) {
449 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
450 "Enter 802.11 power save mode...\n");
452 rtl_lps_set_psmode(hw, EAUTOPS);
456 spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag);
459 /* Interrupt safe routine to leave the leisure power save mode.*/
460 static void rtl_lps_leave_core(struct ieee80211_hw *hw)
462 struct rtl_priv *rtlpriv = rtl_priv(hw);
463 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
464 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
467 spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag);
469 if (ppsc->fwctrl_lps) {
470 if (ppsc->dot11_psmode != EACTIVE) {
473 /*rtlpriv->cfg->ops->enable_interrupt(hw); */
475 if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
476 RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
477 rtlhal->interface == INTF_PCI) {
478 rtlpriv->intf_ops->disable_aspm(hw);
479 RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
482 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
483 "Busy Traffic,Leave 802.11 power save..\n");
485 rtl_lps_set_psmode(hw, EACTIVE);
488 spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag);
492 void rtl_swlps_beacon(struct ieee80211_hw *hw, void *data, unsigned int len)
494 struct rtl_priv *rtlpriv = rtl_priv(hw);
495 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
496 struct ieee80211_hdr *hdr = data;
497 struct ieee80211_tim_ie *tim_ie;
503 if (mac->opmode != NL80211_IFTYPE_STATION)
506 if (!rtlpriv->psc.swctrl_lps)
509 if (rtlpriv->mac80211.link_state != MAC80211_LINKED)
512 if (!rtlpriv->psc.sw_ps_enabled)
515 if (rtlpriv->psc.fwctrl_lps)
518 if (likely(!(hw->conf.flags & IEEE80211_CONF_PS)))
521 /* check if this really is a beacon */
522 if (!ieee80211_is_beacon(hdr->frame_control))
525 /* min. beacon length + FCS_LEN */
526 if (len <= 40 + FCS_LEN)
529 /* and only beacons from the associated BSSID, please */
530 if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
533 rtlpriv->psc.last_beacon = jiffies;
535 tim = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_TIM);
539 if (tim[1] < sizeof(*tim_ie))
543 tim_ie = (struct ieee80211_tim_ie *) &tim[2];
545 if (!WARN_ON_ONCE(!hw->conf.ps_dtim_period))
546 rtlpriv->psc.dtim_counter = tim_ie->dtim_count;
548 /* Check whenever the PHY can be turned off again. */
550 /* 1. What about buffered unicast traffic for our AID? */
551 u_buffed = ieee80211_check_tim(tim_ie, tim_len,
552 rtlpriv->mac80211.assoc_id);
554 /* 2. Maybe the AP wants to send multicast/broadcast data? */
555 m_buffed = tim_ie->bitmap_ctrl & 0x01;
556 rtlpriv->psc.multi_buffered = m_buffed;
558 /* unicast will process by mac80211 through
559 * set ~IEEE80211_CONF_PS, So we just check
560 * multicast frames here */
562 /* back to low-power land. and delay is
563 * prevent null power save frame tx fail */
564 queue_delayed_work(rtlpriv->works.rtl_wq,
565 &rtlpriv->works.ps_work, MSECS(5));
567 RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
568 "u_bufferd: %x, m_buffered: %x\n", u_buffed, m_buffed);
571 EXPORT_SYMBOL_GPL(rtl_swlps_beacon);
573 void rtl_swlps_rf_awake(struct ieee80211_hw *hw)
575 struct rtl_priv *rtlpriv = rtl_priv(hw);
576 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
577 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
580 if (!rtlpriv->psc.swctrl_lps)
582 if (mac->link_state != MAC80211_LINKED)
585 if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
586 RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
587 rtlpriv->intf_ops->disable_aspm(hw);
588 RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
591 spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag);
592 rtl_ps_set_rf_state(hw, ERFON, RF_CHANGE_BY_PS, false);
593 spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag);
596 void rtl_swlps_rfon_wq_callback(void *data)
598 struct rtl_works *rtlworks =
599 container_of_dwork_rtl(data, struct rtl_works, ps_rfon_wq);
600 struct ieee80211_hw *hw = rtlworks->hw;
602 rtl_swlps_rf_awake(hw);
605 void rtl_swlps_rf_sleep(struct ieee80211_hw *hw)
607 struct rtl_priv *rtlpriv = rtl_priv(hw);
608 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
609 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
613 if (!rtlpriv->psc.sw_ps_enabled)
616 if ((rtlpriv->sec.being_setkey) ||
617 (mac->opmode == NL80211_IFTYPE_ADHOC))
620 /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
621 if ((mac->link_state != MAC80211_LINKED) || (mac->cnt_after_linked < 5))
624 if (rtlpriv->link_info.busytraffic)
627 spin_lock(&rtlpriv->locks.rf_ps_lock);
628 if (rtlpriv->psc.rfchange_inprogress) {
629 spin_unlock(&rtlpriv->locks.rf_ps_lock);
632 spin_unlock(&rtlpriv->locks.rf_ps_lock);
634 spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag);
635 rtl_ps_set_rf_state(hw, ERFSLEEP, RF_CHANGE_BY_PS , false);
636 spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag);
638 if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM &&
639 !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
640 rtlpriv->intf_ops->enable_aspm(hw);
641 RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
644 /* here is power save alg, when this beacon is DTIM
645 * we will set sleep time to dtim_period * n;
646 * when this beacon is not DTIM, we will set sleep
647 * time to sleep_intv = rtlpriv->psc.dtim_counter or
648 * MAX_SW_LPS_SLEEP_INTV(default set to 5) */
650 if (rtlpriv->psc.dtim_counter == 0) {
651 if (hw->conf.ps_dtim_period == 1)
652 sleep_intv = hw->conf.ps_dtim_period * 2;
654 sleep_intv = hw->conf.ps_dtim_period;
656 sleep_intv = rtlpriv->psc.dtim_counter;
659 if (sleep_intv > MAX_SW_LPS_SLEEP_INTV)
660 sleep_intv = MAX_SW_LPS_SLEEP_INTV;
662 /* this print should always be dtim_conter = 0 &
663 * sleep = dtim_period, that meaons, we should
664 * awake before every dtim */
665 RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
666 "dtim_counter:%x will sleep :%d beacon_intv\n",
667 rtlpriv->psc.dtim_counter, sleep_intv);
669 /* we tested that 40ms is enough for sw & hw sw delay */
670 queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.ps_rfon_wq,
671 MSECS(sleep_intv * mac->vif->bss_conf.beacon_int - 40));
674 void rtl_lps_change_work_callback(struct work_struct *work)
676 struct rtl_works *rtlworks =
677 container_of(work, struct rtl_works, lps_change_work);
678 struct ieee80211_hw *hw = rtlworks->hw;
679 struct rtl_priv *rtlpriv = rtl_priv(hw);
681 if (rtlpriv->enter_ps)
682 rtl_lps_enter_core(hw);
684 rtl_lps_leave_core(hw);
686 EXPORT_SYMBOL_GPL(rtl_lps_change_work_callback);
688 void rtl_lps_enter(struct ieee80211_hw *hw)
690 struct rtl_priv *rtlpriv = rtl_priv(hw);
693 return rtl_lps_enter_core(hw);
694 rtlpriv->enter_ps = true;
695 schedule_work(&rtlpriv->works.lps_change_work);
697 EXPORT_SYMBOL_GPL(rtl_lps_enter);
699 void rtl_lps_leave(struct ieee80211_hw *hw)
701 struct rtl_priv *rtlpriv = rtl_priv(hw);
704 return rtl_lps_leave_core(hw);
705 rtlpriv->enter_ps = false;
706 schedule_work(&rtlpriv->works.lps_change_work);
708 EXPORT_SYMBOL_GPL(rtl_lps_leave);
710 void rtl_swlps_wq_callback(void *data)
712 struct rtl_works *rtlworks = container_of_dwork_rtl(data,
715 struct ieee80211_hw *hw = rtlworks->hw;
716 struct rtl_priv *rtlpriv = rtl_priv(hw);
719 ps = (hw->conf.flags & IEEE80211_CONF_PS);
721 /* we can sleep after ps null send ok */
722 if (rtlpriv->psc.state_inap) {
723 rtl_swlps_rf_sleep(hw);
725 if (rtlpriv->psc.state && !ps) {
726 rtlpriv->psc.sleep_ms = jiffies_to_msecs(jiffies -
727 rtlpriv->psc.last_action);
731 rtlpriv->psc.last_slept = jiffies;
733 rtlpriv->psc.last_action = jiffies;
734 rtlpriv->psc.state = ps;
738 static void rtl_p2p_noa_ie(struct ieee80211_hw *hw, void *data,
741 struct rtl_priv *rtlpriv = rtl_priv(hw);
742 struct ieee80211_mgmt *mgmt = data;
743 struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
746 static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
747 u8 noa_num, index , i, noa_index = 0;
748 bool find_p2p_ie = false , find_p2p_ps_ie = false;
749 pos = (u8 *)mgmt->u.beacon.variable;
753 while (pos + 1 < end) {
754 if (pos + 2 + pos[1] > end)
757 if (pos[0] == 221 && pos[1] > 4) {
758 if (memcmp(&pos[2], p2p_oui_ie_type, 4) == 0) {
770 while (ie + 1 < end) {
771 noa_len = READEF2BYTE((__le16 *)&ie[1]);
772 if (ie + 3 + ie[1] > end)
776 find_p2p_ps_ie = true;
777 if ((noa_len - 2) % 13 != 0) {
778 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
779 "P2P notice of absence: invalid length.%d\n",
783 noa_num = (noa_len - 2) / 13;
784 if (noa_num > P2P_MAX_NOA_NUM)
785 noa_num = P2P_MAX_NOA_NUM;
789 if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
790 P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
791 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
793 p2pinfo->noa_index = noa_index;
794 p2pinfo->opp_ps = (ie[4] >> 7);
795 p2pinfo->ctwindow = ie[4] & 0x7F;
796 p2pinfo->noa_num = noa_num;
798 for (i = 0; i < noa_num; i++) {
799 p2pinfo->noa_count_type[i] =
800 READEF1BYTE(ie+index);
802 p2pinfo->noa_duration[i] =
803 READEF4BYTE((__le32 *)ie+index);
805 p2pinfo->noa_interval[i] =
806 READEF4BYTE((__le32 *)ie+index);
808 p2pinfo->noa_start_time[i] =
809 READEF4BYTE((__le32 *)ie+index);
813 if (p2pinfo->opp_ps == 1) {
814 p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
815 /* Driver should wait LPS entering
818 if (rtlpriv->psc.fw_current_inpsmode)
821 } else if (p2pinfo->noa_num > 0) {
822 p2pinfo->p2p_ps_mode = P2P_PS_NOA;
823 rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
824 } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
825 rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
833 if (find_p2p_ie == true) {
834 if ((p2pinfo->p2p_ps_mode > P2P_PS_NONE) &&
835 (find_p2p_ps_ie == false))
836 rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
840 static void rtl_p2p_action_ie(struct ieee80211_hw *hw, void *data,
843 struct rtl_priv *rtlpriv = rtl_priv(hw);
844 struct ieee80211_mgmt *mgmt = data;
845 struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
846 u8 noa_num, index , i , noa_index = 0;
849 static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
851 pos = (u8 *)&mgmt->u.action.category;
855 if (pos[0] == 0x7f) {
856 if (memcmp(&pos[1], p2p_oui_ie_type, 4) == 0)
863 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "action frame find P2P IE.\n");
865 while (ie + 1 < end) {
866 noa_len = READEF2BYTE((__le16 *)&ie[1]);
867 if (ie + 3 + ie[1] > end)
871 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "find NOA IE.\n");
872 RT_PRINT_DATA(rtlpriv, COMP_FW, DBG_LOUD, "noa ie ",
874 if ((noa_len - 2) % 13 != 0) {
875 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
876 "P2P notice of absence: invalid length.%d\n",
880 noa_num = (noa_len - 2) / 13;
881 if (noa_num > P2P_MAX_NOA_NUM)
882 noa_num = P2P_MAX_NOA_NUM;
886 if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
887 P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
888 p2pinfo->noa_index = noa_index;
889 p2pinfo->opp_ps = (ie[4] >> 7);
890 p2pinfo->ctwindow = ie[4] & 0x7F;
891 p2pinfo->noa_num = noa_num;
893 for (i = 0; i < noa_num; i++) {
894 p2pinfo->noa_count_type[i] =
895 READEF1BYTE(ie+index);
897 p2pinfo->noa_duration[i] =
898 READEF4BYTE((__le32 *)ie+index);
900 p2pinfo->noa_interval[i] =
901 READEF4BYTE((__le32 *)ie+index);
903 p2pinfo->noa_start_time[i] =
904 READEF4BYTE((__le32 *)ie+index);
908 if (p2pinfo->opp_ps == 1) {
909 p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
910 /* Driver should wait LPS entering
913 if (rtlpriv->psc.fw_current_inpsmode)
916 } else if (p2pinfo->noa_num > 0) {
917 p2pinfo->p2p_ps_mode = P2P_PS_NOA;
918 rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
919 } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
920 rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
929 void rtl_p2p_ps_cmd(struct ieee80211_hw *hw , u8 p2p_ps_state)
931 struct rtl_priv *rtlpriv = rtl_priv(hw);
932 struct rtl_ps_ctl *rtlps = rtl_psc(rtl_priv(hw));
933 struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
935 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, " p2p state %x\n" , p2p_ps_state);
936 switch (p2p_ps_state) {
938 p2pinfo->p2p_ps_state = p2p_ps_state;
939 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
941 p2pinfo->noa_index = 0;
942 p2pinfo->ctwindow = 0;
944 p2pinfo->noa_num = 0;
945 p2pinfo->p2p_ps_mode = P2P_PS_NONE;
946 if (rtlps->fw_current_inpsmode) {
947 if (rtlps->smart_ps == 0) {
949 rtlpriv->cfg->ops->set_hw_reg(hw,
950 HW_VAR_H2C_FW_PWRMODE,
957 if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
958 p2pinfo->p2p_ps_state = p2p_ps_state;
960 if (p2pinfo->ctwindow > 0) {
961 if (rtlps->smart_ps != 0) {
963 rtlpriv->cfg->ops->set_hw_reg(hw,
964 HW_VAR_H2C_FW_PWRMODE,
968 rtlpriv->cfg->ops->set_hw_reg(hw,
969 HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
975 case P2P_PS_SCAN_DONE:
976 case P2P_PS_ALLSTASLEEP:
977 if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
978 p2pinfo->p2p_ps_state = p2p_ps_state;
979 rtlpriv->cfg->ops->set_hw_reg(hw,
980 HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
987 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
988 "ctwindow %x oppps %x\n",
989 p2pinfo->ctwindow , p2pinfo->opp_ps);
990 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
991 "count %x duration %x index %x interval %x start time %x noa num %x\n",
992 p2pinfo->noa_count_type[0],
993 p2pinfo->noa_duration[0],
995 p2pinfo->noa_interval[0],
996 p2pinfo->noa_start_time[0],
998 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "end\n");
1001 void rtl_p2p_info(struct ieee80211_hw *hw, void *data, unsigned int len)
1003 struct rtl_priv *rtlpriv = rtl_priv(hw);
1004 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1005 struct ieee80211_hdr *hdr = data;
1009 if (mac->link_state != MAC80211_LINKED)
1011 /* min. beacon length + FCS_LEN */
1012 if (len <= 40 + FCS_LEN)
1015 /* and only beacons from the associated BSSID, please */
1016 if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
1019 /* check if this really is a beacon */
1020 if (!(ieee80211_is_beacon(hdr->frame_control) ||
1021 ieee80211_is_probe_resp(hdr->frame_control) ||
1022 ieee80211_is_action(hdr->frame_control)))
1025 if (ieee80211_is_action(hdr->frame_control))
1026 rtl_p2p_action_ie(hw , data , len - FCS_LEN);
1028 rtl_p2p_noa_ie(hw , data , len - FCS_LEN);
1030 EXPORT_SYMBOL_GPL(rtl_p2p_info);