1 /******************************************************************************
3 * Copyright(c) 2009-2010 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 * 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
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * wlanfae <wlanfae@realtek.com>
23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24 * Hsinchu 300, Taiwan.
26 * Larry Finger <Larry.Finger@lwfinger.net>
28 *****************************************************************************/
30 #include <linux/export.h>
35 bool rtl_ps_enable_nic(struct ieee80211_hw
*hw
)
37 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
38 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
39 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
41 /*<1> reset trx ring */
42 if (rtlhal
->interface
== INTF_PCI
)
43 rtlpriv
->intf_ops
->reset_trx_ring(hw
);
45 if (is_hal_stop(rtlhal
))
46 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_WARNING
,
47 ("Driver is already down!\n"));
49 /*<2> Enable Adapter */
50 rtlpriv
->cfg
->ops
->hw_init(hw
);
51 RT_CLEAR_PS_LEVEL(ppsc
, RT_RF_OFF_LEVL_HALT_NIC
);
53 /*<3> Enable Interrupt */
54 rtlpriv
->cfg
->ops
->enable_interrupt(hw
);
57 rtl_watch_dog_timer_callback((unsigned long)hw
);
61 EXPORT_SYMBOL(rtl_ps_enable_nic
);
63 bool rtl_ps_disable_nic(struct ieee80211_hw
*hw
)
65 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
67 /*<1> Stop all timer */
68 rtl_deinit_deferred_work(hw
);
70 /*<2> Disable Interrupt */
71 rtlpriv
->cfg
->ops
->disable_interrupt(hw
);
72 tasklet_kill(&rtlpriv
->works
.irq_tasklet
);
74 /*<3> Disable Adapter */
75 rtlpriv
->cfg
->ops
->hw_disable(hw
);
79 EXPORT_SYMBOL(rtl_ps_disable_nic
);
81 bool rtl_ps_set_rf_state(struct ieee80211_hw
*hw
,
82 enum rf_pwrstate state_toset
,
85 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
86 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
87 bool actionallowed
= false;
89 switch (state_toset
) {
91 ppsc
->rfoff_reason
&= (~changesource
);
93 if ((changesource
== RF_CHANGE_BY_HW
) &&
95 ppsc
->hwradiooff
= false;
98 if (!ppsc
->rfoff_reason
) {
99 ppsc
->rfoff_reason
= 0;
100 actionallowed
= true;
107 if ((changesource
== RF_CHANGE_BY_HW
)
108 && (ppsc
->hwradiooff
== false)) {
109 ppsc
->hwradiooff
= true;
112 ppsc
->rfoff_reason
|= changesource
;
113 actionallowed
= true;
117 ppsc
->rfoff_reason
|= changesource
;
118 actionallowed
= true;
122 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_EMERG
,
123 ("switch case not process\n"));
128 rtlpriv
->cfg
->ops
->set_rf_power_state(hw
, state_toset
);
130 return actionallowed
;
132 EXPORT_SYMBOL(rtl_ps_set_rf_state
);
134 static void _rtl_ps_inactive_ps(struct ieee80211_hw
*hw
)
136 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
137 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
138 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
140 ppsc
->swrf_processing
= true;
142 if (ppsc
->inactive_pwrstate
== ERFON
&&
143 rtlhal
->interface
== INTF_PCI
) {
144 if ((ppsc
->reg_rfps_level
& RT_RF_OFF_LEVL_ASPM
) &&
145 RT_IN_PS_LEVEL(ppsc
, RT_PS_LEVEL_ASPM
) &&
146 rtlhal
->interface
== INTF_PCI
) {
147 rtlpriv
->intf_ops
->disable_aspm(hw
);
148 RT_CLEAR_PS_LEVEL(ppsc
, RT_PS_LEVEL_ASPM
);
152 rtl_ps_set_rf_state(hw
, ppsc
->inactive_pwrstate
, RF_CHANGE_BY_IPS
);
154 if (ppsc
->inactive_pwrstate
== ERFOFF
&&
155 rtlhal
->interface
== INTF_PCI
) {
156 if (ppsc
->reg_rfps_level
& RT_RF_OFF_LEVL_ASPM
&&
157 !RT_IN_PS_LEVEL(ppsc
, RT_PS_LEVEL_ASPM
)) {
158 rtlpriv
->intf_ops
->enable_aspm(hw
);
159 RT_SET_PS_LEVEL(ppsc
, RT_PS_LEVEL_ASPM
);
163 ppsc
->swrf_processing
= false;
166 void rtl_ips_nic_off_wq_callback(void *data
)
168 struct rtl_works
*rtlworks
=
169 container_of_dwork_rtl(data
, struct rtl_works
, ips_nic_off_wq
);
170 struct ieee80211_hw
*hw
= rtlworks
->hw
;
171 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
172 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
173 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
174 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
175 enum rf_pwrstate rtstate
;
177 if (mac
->opmode
!= NL80211_IFTYPE_STATION
) {
178 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_WARNING
,
179 ("not station return\n"));
183 if (mac
->link_state
> MAC80211_NOLINK
)
186 if (is_hal_stop(rtlhal
))
189 if (rtlpriv
->sec
.being_setkey
)
192 if (ppsc
->inactiveps
) {
193 rtstate
= ppsc
->rfpwr_state
;
196 *Do not enter IPS in the following conditions:
197 *(1) RF is already OFF or Sleep
198 *(2) swrf_processing (indicates the IPS is still under going)
199 *(3) Connectted (only disconnected can trigger IPS)
200 *(4) IBSS (send Beacon)
201 *(5) AP mode (send Beacon)
202 *(6) monitor mode (rcv packet)
205 if (rtstate
== ERFON
&&
206 !ppsc
->swrf_processing
&&
207 (mac
->link_state
== MAC80211_NOLINK
) &&
208 !mac
->act_scanning
) {
209 RT_TRACE(rtlpriv
, COMP_RF
, DBG_TRACE
,
210 ("IPSEnter(): Turn off RF.\n"));
212 ppsc
->inactive_pwrstate
= ERFOFF
;
213 ppsc
->in_powersavemode
= true;
215 /*rtl_pci_reset_trx_ring(hw); */
216 _rtl_ps_inactive_ps(hw
);
221 void rtl_ips_nic_off(struct ieee80211_hw
*hw
)
223 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
226 *because when link with ap, mac80211 will ask us
227 *to disable nic quickly after scan before linking,
228 *this will cause link failed, so we delay 100ms here
230 queue_delayed_work(rtlpriv
->works
.rtl_wq
,
231 &rtlpriv
->works
.ips_nic_off_wq
, MSECS(100));
234 void rtl_ips_nic_on(struct ieee80211_hw
*hw
)
236 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
237 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
238 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
239 enum rf_pwrstate rtstate
;
242 if (mac
->opmode
!= NL80211_IFTYPE_STATION
)
245 spin_lock_irqsave(&rtlpriv
->locks
.ips_lock
, flags
);
247 if (ppsc
->inactiveps
) {
248 rtstate
= ppsc
->rfpwr_state
;
250 if (rtstate
!= ERFON
&&
251 !ppsc
->swrf_processing
&&
252 ppsc
->rfoff_reason
<= RF_CHANGE_BY_IPS
) {
254 ppsc
->inactive_pwrstate
= ERFON
;
255 ppsc
->in_powersavemode
= false;
257 _rtl_ps_inactive_ps(hw
);
261 spin_unlock_irqrestore(&rtlpriv
->locks
.ips_lock
, flags
);
267 *Determine if we can set Fw into PS mode
268 *in current condition.Return TRUE if it
271 static bool rtl_get_fwlps_doze(struct ieee80211_hw
*hw
)
273 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
274 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
275 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
278 ps_timediff
= jiffies_to_msecs(jiffies
-
279 ppsc
->last_delaylps_stamp_jiffies
);
281 if (ps_timediff
< 2000) {
282 RT_TRACE(rtlpriv
, COMP_POWER
, DBG_LOUD
,
283 ("Delay enter Fw LPS for DHCP, ARP,"
284 " or EAPOL exchanging state.\n"));
288 if (mac
->link_state
!= MAC80211_LINKED
)
291 if (mac
->opmode
== NL80211_IFTYPE_ADHOC
)
297 /* Change current and default preamble mode.*/
298 static void rtl_lps_set_psmode(struct ieee80211_hw
*hw
, u8 rt_psmode
)
300 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
301 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
302 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
303 u8 rpwm_val
, fw_pwrmode
;
305 if (mac
->opmode
== NL80211_IFTYPE_ADHOC
)
308 if (mac
->link_state
!= MAC80211_LINKED
)
311 if (ppsc
->dot11_psmode
== rt_psmode
)
314 /* Update power save mode configured. */
315 ppsc
->dot11_psmode
= rt_psmode
;
320 * Set RPWM to Fw to turn RF off and send H2C fw_pwrmode
321 * cmd to set Fw into PS mode.
323 * Send H2C fw_pwrmode cmd to Fw to set Fw into Active
324 * mode and set RPWM to turn RF on.
327 if ((ppsc
->fwctrl_lps
) && ppsc
->report_linked
) {
328 bool fw_current_inps
;
329 if (ppsc
->dot11_psmode
== EACTIVE
) {
330 RT_TRACE(rtlpriv
, COMP_RF
, DBG_DMESG
,
331 ("FW LPS leave ps_mode:%x\n",
334 rpwm_val
= 0x0C; /* RF on */
335 fw_pwrmode
= FW_PS_ACTIVE_MODE
;
336 rtlpriv
->cfg
->ops
->set_hw_reg(hw
, HW_VAR_SET_RPWM
,
338 rtlpriv
->cfg
->ops
->set_hw_reg(hw
,
339 HW_VAR_H2C_FW_PWRMODE
,
340 (u8
*) (&fw_pwrmode
));
341 fw_current_inps
= false;
343 rtlpriv
->cfg
->ops
->set_hw_reg(hw
,
344 HW_VAR_FW_PSMODE_STATUS
,
345 (u8
*) (&fw_current_inps
));
348 if (rtl_get_fwlps_doze(hw
)) {
349 RT_TRACE(rtlpriv
, COMP_RF
, DBG_DMESG
,
350 ("FW LPS enter ps_mode:%x\n",
351 ppsc
->fwctrl_psmode
));
353 rpwm_val
= 0x02; /* RF off */
354 fw_current_inps
= true;
355 rtlpriv
->cfg
->ops
->set_hw_reg(hw
,
356 HW_VAR_FW_PSMODE_STATUS
,
357 (u8
*) (&fw_current_inps
));
358 rtlpriv
->cfg
->ops
->set_hw_reg(hw
,
359 HW_VAR_H2C_FW_PWRMODE
,
360 (u8
*) (&ppsc
->fwctrl_psmode
));
362 rtlpriv
->cfg
->ops
->set_hw_reg(hw
,
366 /* Reset the power save related parameters. */
367 ppsc
->dot11_psmode
= EACTIVE
;
373 /*Enter the leisure power save mode.*/
374 void rtl_lps_enter(struct ieee80211_hw
*hw
)
376 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
377 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
378 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
380 if (!ppsc
->fwctrl_lps
)
383 if (rtlpriv
->sec
.being_setkey
)
386 if (rtlpriv
->link_info
.busytraffic
)
389 /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
390 if (mac
->cnt_after_linked
< 5)
393 if (mac
->opmode
== NL80211_IFTYPE_ADHOC
)
396 if (mac
->link_state
!= MAC80211_LINKED
)
399 mutex_lock(&rtlpriv
->locks
.ps_mutex
);
401 /* Idle for a while if we connect to AP a while ago. */
402 if (mac
->cnt_after_linked
>= 2) {
403 if (ppsc
->dot11_psmode
== EACTIVE
) {
404 RT_TRACE(rtlpriv
, COMP_POWER
, DBG_LOUD
,
405 ("Enter 802.11 power save mode...\n"));
407 rtl_lps_set_psmode(hw
, EAUTOPS
);
411 mutex_unlock(&rtlpriv
->locks
.ps_mutex
);
414 /*Leave the leisure power save mode.*/
415 void rtl_lps_leave(struct ieee80211_hw
*hw
)
417 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
418 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
419 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
421 mutex_lock(&rtlpriv
->locks
.ps_mutex
);
423 if (ppsc
->fwctrl_lps
) {
424 if (ppsc
->dot11_psmode
!= EACTIVE
) {
427 rtlpriv
->cfg
->ops
->enable_interrupt(hw
);
429 if (ppsc
->reg_rfps_level
& RT_RF_LPS_LEVEL_ASPM
&&
430 RT_IN_PS_LEVEL(ppsc
, RT_PS_LEVEL_ASPM
) &&
431 rtlhal
->interface
== INTF_PCI
) {
432 rtlpriv
->intf_ops
->disable_aspm(hw
);
433 RT_CLEAR_PS_LEVEL(ppsc
, RT_PS_LEVEL_ASPM
);
436 RT_TRACE(rtlpriv
, COMP_POWER
, DBG_LOUD
,
437 ("Busy Traffic,Leave 802.11 power save..\n"));
439 rtl_lps_set_psmode(hw
, EACTIVE
);
442 mutex_unlock(&rtlpriv
->locks
.ps_mutex
);
446 void rtl_swlps_beacon(struct ieee80211_hw
*hw
, void *data
, unsigned int len
)
448 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
449 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
450 struct ieee80211_hdr
*hdr
= (void *) data
;
451 struct ieee80211_tim_ie
*tim_ie
;
457 if (mac
->opmode
!= NL80211_IFTYPE_STATION
)
460 if (!rtlpriv
->psc
.swctrl_lps
)
463 if (rtlpriv
->mac80211
.link_state
!= MAC80211_LINKED
)
466 if (!rtlpriv
->psc
.sw_ps_enabled
)
469 if (rtlpriv
->psc
.fwctrl_lps
)
472 if (likely(!(hw
->conf
.flags
& IEEE80211_CONF_PS
)))
475 /* check if this really is a beacon */
476 if (!ieee80211_is_beacon(hdr
->frame_control
))
479 /* min. beacon length + FCS_LEN */
480 if (len
<= 40 + FCS_LEN
)
483 /* and only beacons from the associated BSSID, please */
484 if (compare_ether_addr(hdr
->addr3
, rtlpriv
->mac80211
.bssid
))
487 rtlpriv
->psc
.last_beacon
= jiffies
;
489 tim
= rtl_find_ie(data
, len
- FCS_LEN
, WLAN_EID_TIM
);
493 if (tim
[1] < sizeof(*tim_ie
))
497 tim_ie
= (struct ieee80211_tim_ie
*) &tim
[2];
499 if (!WARN_ON_ONCE(!hw
->conf
.ps_dtim_period
))
500 rtlpriv
->psc
.dtim_counter
= tim_ie
->dtim_count
;
502 /* Check whenever the PHY can be turned off again. */
504 /* 1. What about buffered unicast traffic for our AID? */
505 u_buffed
= ieee80211_check_tim(tim_ie
, tim_len
,
506 rtlpriv
->mac80211
.assoc_id
);
508 /* 2. Maybe the AP wants to send multicast/broadcast data? */
509 m_buffed
= tim_ie
->bitmap_ctrl
& 0x01;
510 rtlpriv
->psc
.multi_buffered
= m_buffed
;
512 /* unicast will process by mac80211 through
513 * set ~IEEE80211_CONF_PS, So we just check
514 * multicast frames here */
516 /* back to low-power land. and delay is
517 * prevent null power save frame tx fail */
518 queue_delayed_work(rtlpriv
->works
.rtl_wq
,
519 &rtlpriv
->works
.ps_work
, MSECS(5));
521 RT_TRACE(rtlpriv
, COMP_POWER
, DBG_DMESG
, ("u_bufferd: %x, "
522 "m_buffered: %x\n", u_buffed
, m_buffed
));
526 void rtl_swlps_rf_awake(struct ieee80211_hw
*hw
)
528 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
529 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
530 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
532 if (!rtlpriv
->psc
.swctrl_lps
)
534 if (mac
->link_state
!= MAC80211_LINKED
)
537 if (ppsc
->reg_rfps_level
& RT_RF_LPS_LEVEL_ASPM
&&
538 RT_IN_PS_LEVEL(ppsc
, RT_PS_LEVEL_ASPM
)) {
539 rtlpriv
->intf_ops
->disable_aspm(hw
);
540 RT_CLEAR_PS_LEVEL(ppsc
, RT_PS_LEVEL_ASPM
);
543 mutex_lock(&rtlpriv
->locks
.ps_mutex
);
544 rtl_ps_set_rf_state(hw
, ERFON
, RF_CHANGE_BY_PS
);
545 mutex_unlock(&rtlpriv
->locks
.ps_mutex
);
548 void rtl_swlps_rfon_wq_callback(void *data
)
550 struct rtl_works
*rtlworks
=
551 container_of_dwork_rtl(data
, struct rtl_works
, ps_rfon_wq
);
552 struct ieee80211_hw
*hw
= rtlworks
->hw
;
554 rtl_swlps_rf_awake(hw
);
557 void rtl_swlps_rf_sleep(struct ieee80211_hw
*hw
)
559 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
560 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
561 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
564 if (!rtlpriv
->psc
.sw_ps_enabled
)
567 if ((rtlpriv
->sec
.being_setkey
) ||
568 (mac
->opmode
== NL80211_IFTYPE_ADHOC
))
571 /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
572 if ((mac
->link_state
!= MAC80211_LINKED
) || (mac
->cnt_after_linked
< 5))
575 if (rtlpriv
->link_info
.busytraffic
)
578 mutex_lock(&rtlpriv
->locks
.ps_mutex
);
579 rtl_ps_set_rf_state(hw
, ERFSLEEP
, RF_CHANGE_BY_PS
);
580 mutex_unlock(&rtlpriv
->locks
.ps_mutex
);
582 if (ppsc
->reg_rfps_level
& RT_RF_OFF_LEVL_ASPM
&&
583 !RT_IN_PS_LEVEL(ppsc
, RT_PS_LEVEL_ASPM
)) {
584 rtlpriv
->intf_ops
->enable_aspm(hw
);
585 RT_SET_PS_LEVEL(ppsc
, RT_PS_LEVEL_ASPM
);
588 /* here is power save alg, when this beacon is DTIM
589 * we will set sleep time to dtim_period * n;
590 * when this beacon is not DTIM, we will set sleep
591 * time to sleep_intv = rtlpriv->psc.dtim_counter or
592 * MAX_SW_LPS_SLEEP_INTV(default set to 5) */
594 if (rtlpriv
->psc
.dtim_counter
== 0) {
595 if (hw
->conf
.ps_dtim_period
== 1)
596 sleep_intv
= hw
->conf
.ps_dtim_period
* 2;
598 sleep_intv
= hw
->conf
.ps_dtim_period
;
600 sleep_intv
= rtlpriv
->psc
.dtim_counter
;
603 if (sleep_intv
> MAX_SW_LPS_SLEEP_INTV
)
604 sleep_intv
= MAX_SW_LPS_SLEEP_INTV
;
606 /* this print should always be dtim_conter = 0 &
607 * sleep = dtim_period, that meaons, we should
608 * awake before every dtim */
609 RT_TRACE(rtlpriv
, COMP_POWER
, DBG_DMESG
,
610 ("dtim_counter:%x will sleep :%d"
611 " beacon_intv\n", rtlpriv
->psc
.dtim_counter
, sleep_intv
));
613 /* we tested that 40ms is enough for sw & hw sw delay */
614 queue_delayed_work(rtlpriv
->works
.rtl_wq
, &rtlpriv
->works
.ps_rfon_wq
,
615 MSECS(sleep_intv
* mac
->vif
->bss_conf
.beacon_int
- 40));
619 void rtl_swlps_wq_callback(void *data
)
621 struct rtl_works
*rtlworks
= container_of_dwork_rtl(data
,
624 struct ieee80211_hw
*hw
= rtlworks
->hw
;
625 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
628 ps
= (hw
->conf
.flags
& IEEE80211_CONF_PS
);
630 /* we can sleep after ps null send ok */
631 if (rtlpriv
->psc
.state_inap
) {
632 rtl_swlps_rf_sleep(hw
);
634 if (rtlpriv
->psc
.state
&& !ps
) {
635 rtlpriv
->psc
.sleep_ms
= jiffies_to_msecs(jiffies
-
636 rtlpriv
->psc
.last_action
);
640 rtlpriv
->psc
.last_slept
= jiffies
;
642 rtlpriv
->psc
.last_action
= jiffies
;
643 rtlpriv
->psc
.state
= ps
;