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