Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright(c) 2009-2010 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 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA |
| 17 | * |
| 18 | * The full GNU General Public License is included in this distribution in the |
| 19 | * file called LICENSE. |
| 20 | * |
| 21 | * Contact Information: |
| 22 | * wlanfae <wlanfae@realtek.com> |
| 23 | * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park, |
| 24 | * Hsinchu 300, Taiwan. |
| 25 | * |
| 26 | * Larry Finger <Larry.Finger@lwfinger.net> |
| 27 | * |
| 28 | *****************************************************************************/ |
| 29 | |
| 30 | #include "wifi.h" |
| 31 | #include "base.h" |
| 32 | #include "ps.h" |
| 33 | |
| 34 | bool rtl_ps_enable_nic(struct ieee80211_hw *hw) |
| 35 | { |
| 36 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 37 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
| 38 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 39 | |
| 40 | /*<1> reset trx ring */ |
| 41 | if (rtlhal->interface == INTF_PCI) |
| 42 | rtlpriv->intf_ops->reset_trx_ring(hw); |
| 43 | |
| 44 | if (is_hal_stop(rtlhal)) |
| 45 | RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, |
| 46 | ("Driver is already down!\n")); |
| 47 | |
| 48 | /*<2> Enable Adapter */ |
| 49 | rtlpriv->cfg->ops->hw_init(hw); |
| 50 | RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 51 | |
| 52 | /*<3> Enable Interrupt */ |
| 53 | rtlpriv->cfg->ops->enable_interrupt(hw); |
| 54 | |
| 55 | /*<enable timer> */ |
| 56 | rtl_watch_dog_timer_callback((unsigned long)hw); |
| 57 | |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 58 | return true; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 59 | } |
| 60 | EXPORT_SYMBOL(rtl_ps_enable_nic); |
| 61 | |
| 62 | bool rtl_ps_disable_nic(struct ieee80211_hw *hw) |
| 63 | { |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 64 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 65 | |
| 66 | /*<1> Stop all timer */ |
| 67 | rtl_deinit_deferred_work(hw); |
| 68 | |
| 69 | /*<2> Disable Interrupt */ |
| 70 | rtlpriv->cfg->ops->disable_interrupt(hw); |
Mike McCormack | 67fc605 | 2011-05-31 08:49:23 +0900 | [diff] [blame] | 71 | tasklet_kill(&rtlpriv->works.irq_tasklet); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 72 | |
| 73 | /*<3> Disable Adapter */ |
| 74 | rtlpriv->cfg->ops->hw_disable(hw); |
| 75 | |
Larry Finger | 3247328 | 2011-03-27 16:19:57 -0500 | [diff] [blame] | 76 | return true; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 77 | } |
| 78 | EXPORT_SYMBOL(rtl_ps_disable_nic); |
| 79 | |
| 80 | bool rtl_ps_set_rf_state(struct ieee80211_hw *hw, |
| 81 | enum rf_pwrstate state_toset, |
Mike McCormack | 4b9d8d6 | 2011-06-20 10:46:05 +0900 | [diff] [blame] | 82 | u32 changesource) |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 83 | { |
| 84 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 85 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 86 | bool actionallowed = false; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 87 | |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 88 | switch (state_toset) { |
| 89 | case ERFON: |
| 90 | ppsc->rfoff_reason &= (~changesource); |
| 91 | |
| 92 | if ((changesource == RF_CHANGE_BY_HW) && |
Mike McCormack | e10542c | 2011-06-20 10:47:51 +0900 | [diff] [blame] | 93 | (ppsc->hwradiooff)) { |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 94 | ppsc->hwradiooff = false; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (!ppsc->rfoff_reason) { |
| 98 | ppsc->rfoff_reason = 0; |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 99 | actionallowed = true; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | break; |
| 103 | |
| 104 | case ERFOFF: |
| 105 | |
| 106 | if ((changesource == RF_CHANGE_BY_HW) |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 107 | && (ppsc->hwradiooff == false)) { |
| 108 | ppsc->hwradiooff = true; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | ppsc->rfoff_reason |= changesource; |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 112 | actionallowed = true; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 113 | break; |
| 114 | |
| 115 | case ERFSLEEP: |
| 116 | ppsc->rfoff_reason |= changesource; |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 117 | actionallowed = true; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 118 | break; |
| 119 | |
| 120 | default: |
| 121 | RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, |
| 122 | ("switch case not process\n")); |
| 123 | break; |
| 124 | } |
| 125 | |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 126 | if (actionallowed) |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 127 | rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset); |
| 128 | |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 129 | return actionallowed; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 130 | } |
| 131 | EXPORT_SYMBOL(rtl_ps_set_rf_state); |
| 132 | |
| 133 | static void _rtl_ps_inactive_ps(struct ieee80211_hw *hw) |
| 134 | { |
| 135 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 136 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); |
| 137 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
| 138 | |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 139 | ppsc->swrf_processing = true; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 140 | |
Larry Finger | 099fb8a | 2011-05-14 10:15:17 -0500 | [diff] [blame] | 141 | if (ppsc->inactive_pwrstate == ERFON && |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 142 | rtlhal->interface == INTF_PCI) { |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 143 | if ((ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) && |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 144 | RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) && |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 145 | rtlhal->interface == INTF_PCI) { |
| 146 | rtlpriv->intf_ops->disable_aspm(hw); |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 147 | RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Mike McCormack | 4b9d8d6 | 2011-06-20 10:46:05 +0900 | [diff] [blame] | 151 | rtl_ps_set_rf_state(hw, ppsc->inactive_pwrstate, RF_CHANGE_BY_IPS); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 152 | |
| 153 | if (ppsc->inactive_pwrstate == ERFOFF && |
| 154 | rtlhal->interface == INTF_PCI) { |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 155 | if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM && |
| 156 | !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) { |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 157 | rtlpriv->intf_ops->enable_aspm(hw); |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 158 | RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 162 | ppsc->swrf_processing = false; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void rtl_ips_nic_off_wq_callback(void *data) |
| 166 | { |
| 167 | struct rtl_works *rtlworks = |
| 168 | container_of_dwork_rtl(data, struct rtl_works, ips_nic_off_wq); |
| 169 | struct ieee80211_hw *hw = rtlworks->hw; |
| 170 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 171 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); |
| 172 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); |
| 173 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
| 174 | enum rf_pwrstate rtstate; |
| 175 | |
| 176 | if (mac->opmode != NL80211_IFTYPE_STATION) { |
| 177 | RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, |
| 178 | ("not station return\n")); |
| 179 | return; |
| 180 | } |
| 181 | |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 182 | if (mac->link_state > MAC80211_NOLINK) |
| 183 | return; |
| 184 | |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 185 | if (is_hal_stop(rtlhal)) |
| 186 | return; |
| 187 | |
| 188 | if (rtlpriv->sec.being_setkey) |
| 189 | return; |
| 190 | |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 191 | if (ppsc->inactiveps) { |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 192 | rtstate = ppsc->rfpwr_state; |
| 193 | |
| 194 | /* |
| 195 | *Do not enter IPS in the following conditions: |
| 196 | *(1) RF is already OFF or Sleep |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 197 | *(2) swrf_processing (indicates the IPS is still under going) |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 198 | *(3) Connectted (only disconnected can trigger IPS) |
| 199 | *(4) IBSS (send Beacon) |
| 200 | *(5) AP mode (send Beacon) |
| 201 | *(6) monitor mode (rcv packet) |
| 202 | */ |
| 203 | |
| 204 | if (rtstate == ERFON && |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 205 | !ppsc->swrf_processing && |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 206 | (mac->link_state == MAC80211_NOLINK) && |
| 207 | !mac->act_scanning) { |
| 208 | RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE, |
| 209 | ("IPSEnter(): Turn off RF.\n")); |
| 210 | |
| 211 | ppsc->inactive_pwrstate = ERFOFF; |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 212 | ppsc->in_powersavemode = true; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 213 | |
| 214 | /*rtl_pci_reset_trx_ring(hw); */ |
| 215 | _rtl_ps_inactive_ps(hw); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void rtl_ips_nic_off(struct ieee80211_hw *hw) |
| 221 | { |
| 222 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 223 | |
| 224 | /* |
| 225 | *because when link with ap, mac80211 will ask us |
| 226 | *to disable nic quickly after scan before linking, |
| 227 | *this will cause link failed, so we delay 100ms here |
| 228 | */ |
| 229 | queue_delayed_work(rtlpriv->works.rtl_wq, |
| 230 | &rtlpriv->works.ips_nic_off_wq, MSECS(100)); |
| 231 | } |
| 232 | |
| 233 | void rtl_ips_nic_on(struct ieee80211_hw *hw) |
| 234 | { |
| 235 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 236 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 237 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
| 238 | enum rf_pwrstate rtstate; |
| 239 | |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 240 | if (mac->opmode != NL80211_IFTYPE_STATION) |
| 241 | return; |
| 242 | |
Mike McCormack | 312d547 | 2011-05-31 08:49:36 +0900 | [diff] [blame] | 243 | spin_lock(&rtlpriv->locks.ips_lock); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 244 | |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 245 | if (ppsc->inactiveps) { |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 246 | rtstate = ppsc->rfpwr_state; |
| 247 | |
| 248 | if (rtstate != ERFON && |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 249 | !ppsc->swrf_processing && |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 250 | ppsc->rfoff_reason <= RF_CHANGE_BY_IPS) { |
| 251 | |
| 252 | ppsc->inactive_pwrstate = ERFON; |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 253 | ppsc->in_powersavemode = false; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 254 | |
| 255 | _rtl_ps_inactive_ps(hw); |
| 256 | } |
| 257 | } |
| 258 | |
Mike McCormack | 312d547 | 2011-05-31 08:49:36 +0900 | [diff] [blame] | 259 | spin_unlock(&rtlpriv->locks.ips_lock); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /*for FW LPS*/ |
| 263 | |
| 264 | /* |
| 265 | *Determine if we can set Fw into PS mode |
| 266 | *in current condition.Return TRUE if it |
| 267 | *can enter PS mode. |
| 268 | */ |
| 269 | static bool rtl_get_fwlps_doze(struct ieee80211_hw *hw) |
| 270 | { |
| 271 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 272 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); |
| 273 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
| 274 | u32 ps_timediff; |
| 275 | |
| 276 | ps_timediff = jiffies_to_msecs(jiffies - |
| 277 | ppsc->last_delaylps_stamp_jiffies); |
| 278 | |
| 279 | if (ps_timediff < 2000) { |
| 280 | RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, |
| 281 | ("Delay enter Fw LPS for DHCP, ARP," |
| 282 | " or EAPOL exchanging state.\n")); |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | if (mac->link_state != MAC80211_LINKED) |
| 287 | return false; |
| 288 | |
| 289 | if (mac->opmode == NL80211_IFTYPE_ADHOC) |
| 290 | return false; |
| 291 | |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | /* Change current and default preamble mode.*/ |
| 296 | static void rtl_lps_set_psmode(struct ieee80211_hw *hw, u8 rt_psmode) |
| 297 | { |
| 298 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 299 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); |
| 300 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
| 301 | u8 rpwm_val, fw_pwrmode; |
| 302 | |
| 303 | if (mac->opmode == NL80211_IFTYPE_ADHOC) |
| 304 | return; |
| 305 | |
| 306 | if (mac->link_state != MAC80211_LINKED) |
| 307 | return; |
| 308 | |
| 309 | if (ppsc->dot11_psmode == rt_psmode) |
| 310 | return; |
| 311 | |
| 312 | /* Update power save mode configured. */ |
| 313 | ppsc->dot11_psmode = rt_psmode; |
| 314 | |
| 315 | /* |
| 316 | *<FW control LPS> |
| 317 | *1. Enter PS mode |
| 318 | * Set RPWM to Fw to turn RF off and send H2C fw_pwrmode |
| 319 | * cmd to set Fw into PS mode. |
| 320 | *2. Leave PS mode |
| 321 | * Send H2C fw_pwrmode cmd to Fw to set Fw into Active |
| 322 | * mode and set RPWM to turn RF on. |
| 323 | */ |
| 324 | |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 325 | if ((ppsc->fwctrl_lps) && ppsc->report_linked) { |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 326 | bool fw_current_inps; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 327 | if (ppsc->dot11_psmode == EACTIVE) { |
| 328 | RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG, |
| 329 | ("FW LPS leave ps_mode:%x\n", |
| 330 | FW_PS_ACTIVE_MODE)); |
| 331 | |
| 332 | rpwm_val = 0x0C; /* RF on */ |
| 333 | fw_pwrmode = FW_PS_ACTIVE_MODE; |
| 334 | rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SET_RPWM, |
| 335 | (u8 *) (&rpwm_val)); |
| 336 | rtlpriv->cfg->ops->set_hw_reg(hw, |
| 337 | HW_VAR_H2C_FW_PWRMODE, |
| 338 | (u8 *) (&fw_pwrmode)); |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 339 | fw_current_inps = false; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 340 | |
| 341 | rtlpriv->cfg->ops->set_hw_reg(hw, |
| 342 | HW_VAR_FW_PSMODE_STATUS, |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 343 | (u8 *) (&fw_current_inps)); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 344 | |
| 345 | } else { |
| 346 | if (rtl_get_fwlps_doze(hw)) { |
| 347 | RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG, |
| 348 | ("FW LPS enter ps_mode:%x\n", |
| 349 | ppsc->fwctrl_psmode)); |
| 350 | |
| 351 | rpwm_val = 0x02; /* RF off */ |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 352 | fw_current_inps = true; |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 353 | rtlpriv->cfg->ops->set_hw_reg(hw, |
| 354 | HW_VAR_FW_PSMODE_STATUS, |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 355 | (u8 *) (&fw_current_inps)); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 356 | rtlpriv->cfg->ops->set_hw_reg(hw, |
| 357 | HW_VAR_H2C_FW_PWRMODE, |
| 358 | (u8 *) (&ppsc->fwctrl_psmode)); |
| 359 | |
| 360 | rtlpriv->cfg->ops->set_hw_reg(hw, |
| 361 | HW_VAR_SET_RPWM, |
| 362 | (u8 *) (&rpwm_val)); |
| 363 | } else { |
| 364 | /* Reset the power save related parameters. */ |
| 365 | ppsc->dot11_psmode = EACTIVE; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /*Enter the leisure power save mode.*/ |
| 372 | void rtl_lps_enter(struct ieee80211_hw *hw) |
| 373 | { |
| 374 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); |
| 375 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
| 376 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 377 | |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 378 | if (!ppsc->fwctrl_lps) |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 379 | return; |
| 380 | |
| 381 | if (rtlpriv->sec.being_setkey) |
| 382 | return; |
| 383 | |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 384 | if (rtlpriv->link_info.busytraffic) |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 385 | return; |
| 386 | |
| 387 | /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */ |
| 388 | if (mac->cnt_after_linked < 5) |
| 389 | return; |
| 390 | |
| 391 | if (mac->opmode == NL80211_IFTYPE_ADHOC) |
| 392 | return; |
| 393 | |
| 394 | if (mac->link_state != MAC80211_LINKED) |
| 395 | return; |
| 396 | |
Mike McCormack | 312d547 | 2011-05-31 08:49:36 +0900 | [diff] [blame] | 397 | spin_lock(&rtlpriv->locks.lps_lock); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 398 | |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 399 | /* Idle for a while if we connect to AP a while ago. */ |
| 400 | if (mac->cnt_after_linked >= 2) { |
| 401 | if (ppsc->dot11_psmode == EACTIVE) { |
| 402 | RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 403 | ("Enter 802.11 power save mode...\n")); |
| 404 | |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 405 | rtl_lps_set_psmode(hw, EAUTOPS); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 406 | } |
| 407 | } |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 408 | |
Mike McCormack | 312d547 | 2011-05-31 08:49:36 +0900 | [diff] [blame] | 409 | spin_unlock(&rtlpriv->locks.lps_lock); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /*Leave the leisure power save mode.*/ |
| 413 | void rtl_lps_leave(struct ieee80211_hw *hw) |
| 414 | { |
| 415 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 416 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
| 417 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 418 | |
Mike McCormack | 312d547 | 2011-05-31 08:49:36 +0900 | [diff] [blame] | 419 | spin_lock(&rtlpriv->locks.lps_lock); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 420 | |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 421 | if (ppsc->fwctrl_lps) { |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 422 | if (ppsc->dot11_psmode != EACTIVE) { |
| 423 | |
| 424 | /*FIX ME */ |
| 425 | rtlpriv->cfg->ops->enable_interrupt(hw); |
| 426 | |
| 427 | if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM && |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 428 | RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) && |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 429 | rtlhal->interface == INTF_PCI) { |
| 430 | rtlpriv->intf_ops->disable_aspm(hw); |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 431 | RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, |
| 435 | ("Busy Traffic,Leave 802.11 power save..\n")); |
| 436 | |
| 437 | rtl_lps_set_psmode(hw, EACTIVE); |
| 438 | } |
| 439 | } |
Mike McCormack | 312d547 | 2011-05-31 08:49:36 +0900 | [diff] [blame] | 440 | spin_unlock(&rtlpriv->locks.lps_lock); |
Larry Finger | 0c81733 | 2010-12-08 11:12:31 -0600 | [diff] [blame] | 441 | } |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 442 | |
| 443 | /* For sw LPS*/ |
| 444 | void rtl_swlps_beacon(struct ieee80211_hw *hw, void *data, unsigned int len) |
| 445 | { |
| 446 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 447 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); |
| 448 | struct ieee80211_hdr *hdr = (void *) data; |
| 449 | struct ieee80211_tim_ie *tim_ie; |
| 450 | u8 *tim; |
| 451 | u8 tim_len; |
| 452 | bool u_buffed; |
| 453 | bool m_buffed; |
| 454 | |
| 455 | if (mac->opmode != NL80211_IFTYPE_STATION) |
| 456 | return; |
| 457 | |
| 458 | if (!rtlpriv->psc.swctrl_lps) |
| 459 | return; |
| 460 | |
| 461 | if (rtlpriv->mac80211.link_state != MAC80211_LINKED) |
| 462 | return; |
| 463 | |
| 464 | if (!rtlpriv->psc.sw_ps_enabled) |
| 465 | return; |
| 466 | |
| 467 | if (rtlpriv->psc.fwctrl_lps) |
| 468 | return; |
| 469 | |
| 470 | if (likely(!(hw->conf.flags & IEEE80211_CONF_PS))) |
| 471 | return; |
| 472 | |
| 473 | /* check if this really is a beacon */ |
| 474 | if (!ieee80211_is_beacon(hdr->frame_control)) |
| 475 | return; |
| 476 | |
| 477 | /* min. beacon length + FCS_LEN */ |
| 478 | if (len <= 40 + FCS_LEN) |
| 479 | return; |
| 480 | |
| 481 | /* and only beacons from the associated BSSID, please */ |
| 482 | if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid)) |
| 483 | return; |
| 484 | |
| 485 | rtlpriv->psc.last_beacon = jiffies; |
| 486 | |
| 487 | tim = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_TIM); |
| 488 | if (!tim) |
| 489 | return; |
| 490 | |
| 491 | if (tim[1] < sizeof(*tim_ie)) |
| 492 | return; |
| 493 | |
| 494 | tim_len = tim[1]; |
| 495 | tim_ie = (struct ieee80211_tim_ie *) &tim[2]; |
| 496 | |
| 497 | if (!WARN_ON_ONCE(!hw->conf.ps_dtim_period)) |
| 498 | rtlpriv->psc.dtim_counter = tim_ie->dtim_count; |
| 499 | |
| 500 | /* Check whenever the PHY can be turned off again. */ |
| 501 | |
| 502 | /* 1. What about buffered unicast traffic for our AID? */ |
| 503 | u_buffed = ieee80211_check_tim(tim_ie, tim_len, |
| 504 | rtlpriv->mac80211.assoc_id); |
| 505 | |
| 506 | /* 2. Maybe the AP wants to send multicast/broadcast data? */ |
| 507 | m_buffed = tim_ie->bitmap_ctrl & 0x01; |
| 508 | rtlpriv->psc.multi_buffered = m_buffed; |
| 509 | |
| 510 | /* unicast will process by mac80211 through |
| 511 | * set ~IEEE80211_CONF_PS, So we just check |
| 512 | * multicast frames here */ |
| 513 | if (!m_buffed) { |
| 514 | /* back to low-power land. and delay is |
| 515 | * prevent null power save frame tx fail */ |
| 516 | queue_delayed_work(rtlpriv->works.rtl_wq, |
| 517 | &rtlpriv->works.ps_work, MSECS(5)); |
| 518 | } else { |
| 519 | RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, ("u_bufferd: %x, " |
| 520 | "m_buffered: %x\n", u_buffed, m_buffed)); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | void rtl_swlps_rf_awake(struct ieee80211_hw *hw) |
| 525 | { |
| 526 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 527 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
| 528 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 529 | |
| 530 | if (!rtlpriv->psc.swctrl_lps) |
| 531 | return; |
| 532 | if (mac->link_state != MAC80211_LINKED) |
| 533 | return; |
| 534 | |
| 535 | if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM && |
| 536 | RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) { |
| 537 | rtlpriv->intf_ops->disable_aspm(hw); |
| 538 | RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); |
| 539 | } |
| 540 | |
Mike McCormack | 312d547 | 2011-05-31 08:49:36 +0900 | [diff] [blame] | 541 | spin_lock(&rtlpriv->locks.lps_lock); |
Mike McCormack | 4b9d8d6 | 2011-06-20 10:46:05 +0900 | [diff] [blame] | 542 | rtl_ps_set_rf_state(hw, ERFON, RF_CHANGE_BY_PS); |
Mike McCormack | 312d547 | 2011-05-31 08:49:36 +0900 | [diff] [blame] | 543 | spin_unlock(&rtlpriv->locks.lps_lock); |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | void rtl_swlps_rfon_wq_callback(void *data) |
| 547 | { |
| 548 | struct rtl_works *rtlworks = |
| 549 | container_of_dwork_rtl(data, struct rtl_works, ps_rfon_wq); |
| 550 | struct ieee80211_hw *hw = rtlworks->hw; |
| 551 | |
| 552 | rtl_swlps_rf_awake(hw); |
| 553 | } |
| 554 | |
| 555 | void rtl_swlps_rf_sleep(struct ieee80211_hw *hw) |
| 556 | { |
| 557 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 558 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); |
| 559 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 560 | u8 sleep_intv; |
| 561 | |
| 562 | if (!rtlpriv->psc.sw_ps_enabled) |
| 563 | return; |
| 564 | |
| 565 | if ((rtlpriv->sec.being_setkey) || |
| 566 | (mac->opmode == NL80211_IFTYPE_ADHOC)) |
| 567 | return; |
| 568 | |
| 569 | /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */ |
| 570 | if ((mac->link_state != MAC80211_LINKED) || (mac->cnt_after_linked < 5)) |
| 571 | return; |
| 572 | |
| 573 | if (rtlpriv->link_info.busytraffic) |
| 574 | return; |
| 575 | |
Mike McCormack | 312d547 | 2011-05-31 08:49:36 +0900 | [diff] [blame] | 576 | spin_lock(&rtlpriv->locks.lps_lock); |
Mike McCormack | 4b9d8d6 | 2011-06-20 10:46:05 +0900 | [diff] [blame] | 577 | rtl_ps_set_rf_state(hw, ERFSLEEP, RF_CHANGE_BY_PS); |
Mike McCormack | 312d547 | 2011-05-31 08:49:36 +0900 | [diff] [blame] | 578 | spin_unlock(&rtlpriv->locks.lps_lock); |
Chaoming_Li | cc7dc0c | 2011-04-25 12:53:14 -0500 | [diff] [blame] | 579 | |
| 580 | if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM && |
| 581 | !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) { |
| 582 | rtlpriv->intf_ops->enable_aspm(hw); |
| 583 | RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); |
| 584 | } |
| 585 | |
| 586 | /* here is power save alg, when this beacon is DTIM |
| 587 | * we will set sleep time to dtim_period * n; |
| 588 | * when this beacon is not DTIM, we will set sleep |
| 589 | * time to sleep_intv = rtlpriv->psc.dtim_counter or |
| 590 | * MAX_SW_LPS_SLEEP_INTV(default set to 5) */ |
| 591 | |
| 592 | if (rtlpriv->psc.dtim_counter == 0) { |
| 593 | if (hw->conf.ps_dtim_period == 1) |
| 594 | sleep_intv = hw->conf.ps_dtim_period * 2; |
| 595 | else |
| 596 | sleep_intv = hw->conf.ps_dtim_period; |
| 597 | } else { |
| 598 | sleep_intv = rtlpriv->psc.dtim_counter; |
| 599 | } |
| 600 | |
| 601 | if (sleep_intv > MAX_SW_LPS_SLEEP_INTV) |
| 602 | sleep_intv = MAX_SW_LPS_SLEEP_INTV; |
| 603 | |
| 604 | /* this print should always be dtim_conter = 0 & |
| 605 | * sleep = dtim_period, that meaons, we should |
| 606 | * awake before every dtim */ |
| 607 | RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, |
| 608 | ("dtim_counter:%x will sleep :%d" |
| 609 | " beacon_intv\n", rtlpriv->psc.dtim_counter, sleep_intv)); |
| 610 | |
| 611 | /* we tested that 40ms is enough for sw & hw sw delay */ |
| 612 | queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.ps_rfon_wq, |
| 613 | MSECS(sleep_intv * mac->vif->bss_conf.beacon_int - 40)); |
| 614 | } |
| 615 | |
| 616 | |
| 617 | void rtl_swlps_wq_callback(void *data) |
| 618 | { |
| 619 | struct rtl_works *rtlworks = container_of_dwork_rtl(data, |
| 620 | struct rtl_works, |
| 621 | ps_work); |
| 622 | struct ieee80211_hw *hw = rtlworks->hw; |
| 623 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 624 | bool ps = false; |
| 625 | |
| 626 | ps = (hw->conf.flags & IEEE80211_CONF_PS); |
| 627 | |
| 628 | /* we can sleep after ps null send ok */ |
| 629 | if (rtlpriv->psc.state_inap) { |
| 630 | rtl_swlps_rf_sleep(hw); |
| 631 | |
| 632 | if (rtlpriv->psc.state && !ps) { |
| 633 | rtlpriv->psc.sleep_ms = jiffies_to_msecs(jiffies - |
| 634 | rtlpriv->psc.last_action); |
| 635 | } |
| 636 | |
| 637 | if (ps) |
| 638 | rtlpriv->psc.last_slept = jiffies; |
| 639 | |
| 640 | rtlpriv->psc.last_action = jiffies; |
| 641 | rtlpriv->psc.state = ps; |
| 642 | } |
| 643 | } |