Commit | Line | Data |
---|---|---|
0c817338 LF |
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)); | |
0c817338 LF |
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); | |
0c817338 LF |
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 | ||
cc7dc0c4 | 58 | return true; |
0c817338 LF |
59 | } |
60 | EXPORT_SYMBOL(rtl_ps_enable_nic); | |
61 | ||
62 | bool rtl_ps_disable_nic(struct ieee80211_hw *hw) | |
63 | { | |
0c817338 LF |
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); | |
71 | ||
72 | /*<3> Disable Adapter */ | |
73 | rtlpriv->cfg->ops->hw_disable(hw); | |
74 | ||
32473284 | 75 | return true; |
0c817338 LF |
76 | } |
77 | EXPORT_SYMBOL(rtl_ps_disable_nic); | |
78 | ||
79 | bool rtl_ps_set_rf_state(struct ieee80211_hw *hw, | |
80 | enum rf_pwrstate state_toset, | |
81 | u32 changesource, bool protect_or_not) | |
82 | { | |
83 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
84 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); | |
7ea47240 | 85 | bool actionallowed = false; |
0c817338 LF |
86 | u16 rfwait_cnt = 0; |
87 | unsigned long flag; | |
88 | ||
89 | /*protect_or_not = true; */ | |
90 | ||
91 | if (protect_or_not) | |
92 | goto no_protect; | |
93 | ||
94 | /* | |
95 | *Only one thread can change | |
96 | *the RF state at one time, and others | |
97 | *should wait to be executed. | |
98 | */ | |
99 | while (true) { | |
100 | spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag); | |
101 | if (ppsc->rfchange_inprogress) { | |
102 | spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, | |
103 | flag); | |
104 | ||
105 | RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, | |
106 | ("RF Change in progress!" | |
107 | "Wait to set..state_toset(%d).\n", | |
108 | state_toset)); | |
109 | ||
110 | /* Set RF after the previous action is done. */ | |
111 | while (ppsc->rfchange_inprogress) { | |
112 | rfwait_cnt++; | |
113 | mdelay(1); | |
114 | ||
115 | /* | |
116 | *Wait too long, return false to avoid | |
117 | *to be stuck here. | |
118 | */ | |
119 | if (rfwait_cnt > 100) | |
120 | return false; | |
121 | } | |
122 | } else { | |
123 | ppsc->rfchange_inprogress = true; | |
124 | spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, | |
125 | flag); | |
126 | break; | |
127 | } | |
128 | } | |
129 | ||
130 | no_protect: | |
0c817338 LF |
131 | switch (state_toset) { |
132 | case ERFON: | |
133 | ppsc->rfoff_reason &= (~changesource); | |
134 | ||
135 | if ((changesource == RF_CHANGE_BY_HW) && | |
7ea47240 LF |
136 | (ppsc->hwradiooff == true)) { |
137 | ppsc->hwradiooff = false; | |
0c817338 LF |
138 | } |
139 | ||
140 | if (!ppsc->rfoff_reason) { | |
141 | ppsc->rfoff_reason = 0; | |
7ea47240 | 142 | actionallowed = true; |
0c817338 LF |
143 | } |
144 | ||
145 | break; | |
146 | ||
147 | case ERFOFF: | |
148 | ||
149 | if ((changesource == RF_CHANGE_BY_HW) | |
7ea47240 LF |
150 | && (ppsc->hwradiooff == false)) { |
151 | ppsc->hwradiooff = true; | |
0c817338 LF |
152 | } |
153 | ||
154 | ppsc->rfoff_reason |= changesource; | |
7ea47240 | 155 | actionallowed = true; |
0c817338 LF |
156 | break; |
157 | ||
158 | case ERFSLEEP: | |
159 | ppsc->rfoff_reason |= changesource; | |
7ea47240 | 160 | actionallowed = true; |
0c817338 LF |
161 | break; |
162 | ||
163 | default: | |
164 | RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, | |
165 | ("switch case not process\n")); | |
166 | break; | |
167 | } | |
168 | ||
7ea47240 | 169 | if (actionallowed) |
0c817338 LF |
170 | rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset); |
171 | ||
172 | if (!protect_or_not) { | |
173 | spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag); | |
174 | ppsc->rfchange_inprogress = false; | |
175 | spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flag); | |
176 | } | |
177 | ||
7ea47240 | 178 | return actionallowed; |
0c817338 LF |
179 | } |
180 | EXPORT_SYMBOL(rtl_ps_set_rf_state); | |
181 | ||
182 | static void _rtl_ps_inactive_ps(struct ieee80211_hw *hw) | |
183 | { | |
184 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
185 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); | |
186 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); | |
187 | ||
7ea47240 | 188 | ppsc->swrf_processing = true; |
0c817338 | 189 | |
099fb8ab | 190 | if (ppsc->inactive_pwrstate == ERFON && |
cc7dc0c4 | 191 | rtlhal->interface == INTF_PCI) { |
0c817338 | 192 | if ((ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) && |
cc7dc0c4 | 193 | RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) && |
0c817338 LF |
194 | rtlhal->interface == INTF_PCI) { |
195 | rtlpriv->intf_ops->disable_aspm(hw); | |
cc7dc0c4 | 196 | RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); |
0c817338 LF |
197 | } |
198 | } | |
199 | ||
200 | rtl_ps_set_rf_state(hw, ppsc->inactive_pwrstate, | |
201 | RF_CHANGE_BY_IPS, false); | |
202 | ||
203 | if (ppsc->inactive_pwrstate == ERFOFF && | |
204 | rtlhal->interface == INTF_PCI) { | |
cc7dc0c4 C |
205 | if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM && |
206 | !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) { | |
0c817338 | 207 | rtlpriv->intf_ops->enable_aspm(hw); |
cc7dc0c4 | 208 | RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); |
0c817338 LF |
209 | } |
210 | } | |
211 | ||
7ea47240 | 212 | ppsc->swrf_processing = false; |
0c817338 LF |
213 | } |
214 | ||
215 | void rtl_ips_nic_off_wq_callback(void *data) | |
216 | { | |
217 | struct rtl_works *rtlworks = | |
218 | container_of_dwork_rtl(data, struct rtl_works, ips_nic_off_wq); | |
219 | struct ieee80211_hw *hw = rtlworks->hw; | |
220 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
221 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); | |
222 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
223 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); | |
224 | enum rf_pwrstate rtstate; | |
225 | ||
226 | if (mac->opmode != NL80211_IFTYPE_STATION) { | |
227 | RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, | |
228 | ("not station return\n")); | |
229 | return; | |
230 | } | |
231 | ||
cc7dc0c4 C |
232 | if (mac->link_state > MAC80211_NOLINK) |
233 | return; | |
234 | ||
0c817338 LF |
235 | if (is_hal_stop(rtlhal)) |
236 | return; | |
237 | ||
238 | if (rtlpriv->sec.being_setkey) | |
239 | return; | |
240 | ||
7ea47240 | 241 | if (ppsc->inactiveps) { |
0c817338 LF |
242 | rtstate = ppsc->rfpwr_state; |
243 | ||
244 | /* | |
245 | *Do not enter IPS in the following conditions: | |
246 | *(1) RF is already OFF or Sleep | |
7ea47240 | 247 | *(2) swrf_processing (indicates the IPS is still under going) |
0c817338 LF |
248 | *(3) Connectted (only disconnected can trigger IPS) |
249 | *(4) IBSS (send Beacon) | |
250 | *(5) AP mode (send Beacon) | |
251 | *(6) monitor mode (rcv packet) | |
252 | */ | |
253 | ||
254 | if (rtstate == ERFON && | |
7ea47240 | 255 | !ppsc->swrf_processing && |
0c817338 LF |
256 | (mac->link_state == MAC80211_NOLINK) && |
257 | !mac->act_scanning) { | |
258 | RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE, | |
259 | ("IPSEnter(): Turn off RF.\n")); | |
260 | ||
261 | ppsc->inactive_pwrstate = ERFOFF; | |
7ea47240 | 262 | ppsc->in_powersavemode = true; |
0c817338 LF |
263 | |
264 | /*rtl_pci_reset_trx_ring(hw); */ | |
265 | _rtl_ps_inactive_ps(hw); | |
266 | } | |
267 | } | |
268 | } | |
269 | ||
270 | void rtl_ips_nic_off(struct ieee80211_hw *hw) | |
271 | { | |
272 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
273 | ||
274 | /* | |
275 | *because when link with ap, mac80211 will ask us | |
276 | *to disable nic quickly after scan before linking, | |
277 | *this will cause link failed, so we delay 100ms here | |
278 | */ | |
279 | queue_delayed_work(rtlpriv->works.rtl_wq, | |
280 | &rtlpriv->works.ips_nic_off_wq, MSECS(100)); | |
281 | } | |
282 | ||
283 | void rtl_ips_nic_on(struct ieee80211_hw *hw) | |
284 | { | |
285 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
cc7dc0c4 | 286 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); |
0c817338 LF |
287 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
288 | enum rf_pwrstate rtstate; | |
d704300f | 289 | unsigned long flags; |
0c817338 | 290 | |
cc7dc0c4 C |
291 | if (mac->opmode != NL80211_IFTYPE_STATION) |
292 | return; | |
293 | ||
d704300f | 294 | spin_lock_irqsave(&rtlpriv->locks.ips_lock, flags); |
0c817338 | 295 | |
7ea47240 | 296 | if (ppsc->inactiveps) { |
0c817338 LF |
297 | rtstate = ppsc->rfpwr_state; |
298 | ||
299 | if (rtstate != ERFON && | |
7ea47240 | 300 | !ppsc->swrf_processing && |
0c817338 LF |
301 | ppsc->rfoff_reason <= RF_CHANGE_BY_IPS) { |
302 | ||
303 | ppsc->inactive_pwrstate = ERFON; | |
7ea47240 | 304 | ppsc->in_powersavemode = false; |
0c817338 LF |
305 | |
306 | _rtl_ps_inactive_ps(hw); | |
307 | } | |
308 | } | |
309 | ||
d704300f | 310 | spin_unlock_irqrestore(&rtlpriv->locks.ips_lock, flags); |
0c817338 LF |
311 | } |
312 | ||
313 | /*for FW LPS*/ | |
314 | ||
315 | /* | |
316 | *Determine if we can set Fw into PS mode | |
317 | *in current condition.Return TRUE if it | |
318 | *can enter PS mode. | |
319 | */ | |
320 | static bool rtl_get_fwlps_doze(struct ieee80211_hw *hw) | |
321 | { | |
322 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
323 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
324 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); | |
325 | u32 ps_timediff; | |
326 | ||
327 | ps_timediff = jiffies_to_msecs(jiffies - | |
328 | ppsc->last_delaylps_stamp_jiffies); | |
329 | ||
330 | if (ps_timediff < 2000) { | |
331 | RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, | |
332 | ("Delay enter Fw LPS for DHCP, ARP," | |
333 | " or EAPOL exchanging state.\n")); | |
334 | return false; | |
335 | } | |
336 | ||
337 | if (mac->link_state != MAC80211_LINKED) | |
338 | return false; | |
339 | ||
340 | if (mac->opmode == NL80211_IFTYPE_ADHOC) | |
341 | return false; | |
342 | ||
343 | return true; | |
344 | } | |
345 | ||
346 | /* Change current and default preamble mode.*/ | |
347 | static void rtl_lps_set_psmode(struct ieee80211_hw *hw, u8 rt_psmode) | |
348 | { | |
349 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
350 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
351 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); | |
352 | u8 rpwm_val, fw_pwrmode; | |
353 | ||
354 | if (mac->opmode == NL80211_IFTYPE_ADHOC) | |
355 | return; | |
356 | ||
357 | if (mac->link_state != MAC80211_LINKED) | |
358 | return; | |
359 | ||
360 | if (ppsc->dot11_psmode == rt_psmode) | |
361 | return; | |
362 | ||
363 | /* Update power save mode configured. */ | |
364 | ppsc->dot11_psmode = rt_psmode; | |
365 | ||
366 | /* | |
367 | *<FW control LPS> | |
368 | *1. Enter PS mode | |
369 | * Set RPWM to Fw to turn RF off and send H2C fw_pwrmode | |
370 | * cmd to set Fw into PS mode. | |
371 | *2. Leave PS mode | |
372 | * Send H2C fw_pwrmode cmd to Fw to set Fw into Active | |
373 | * mode and set RPWM to turn RF on. | |
374 | */ | |
375 | ||
cc7dc0c4 | 376 | if ((ppsc->fwctrl_lps) && ppsc->report_linked) { |
7ea47240 | 377 | bool fw_current_inps; |
0c817338 LF |
378 | if (ppsc->dot11_psmode == EACTIVE) { |
379 | RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG, | |
380 | ("FW LPS leave ps_mode:%x\n", | |
381 | FW_PS_ACTIVE_MODE)); | |
382 | ||
383 | rpwm_val = 0x0C; /* RF on */ | |
384 | fw_pwrmode = FW_PS_ACTIVE_MODE; | |
385 | rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SET_RPWM, | |
386 | (u8 *) (&rpwm_val)); | |
387 | rtlpriv->cfg->ops->set_hw_reg(hw, | |
388 | HW_VAR_H2C_FW_PWRMODE, | |
389 | (u8 *) (&fw_pwrmode)); | |
7ea47240 | 390 | fw_current_inps = false; |
0c817338 LF |
391 | |
392 | rtlpriv->cfg->ops->set_hw_reg(hw, | |
393 | HW_VAR_FW_PSMODE_STATUS, | |
7ea47240 | 394 | (u8 *) (&fw_current_inps)); |
0c817338 LF |
395 | |
396 | } else { | |
397 | if (rtl_get_fwlps_doze(hw)) { | |
398 | RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG, | |
399 | ("FW LPS enter ps_mode:%x\n", | |
400 | ppsc->fwctrl_psmode)); | |
401 | ||
402 | rpwm_val = 0x02; /* RF off */ | |
7ea47240 | 403 | fw_current_inps = true; |
0c817338 LF |
404 | rtlpriv->cfg->ops->set_hw_reg(hw, |
405 | HW_VAR_FW_PSMODE_STATUS, | |
7ea47240 | 406 | (u8 *) (&fw_current_inps)); |
0c817338 LF |
407 | rtlpriv->cfg->ops->set_hw_reg(hw, |
408 | HW_VAR_H2C_FW_PWRMODE, | |
409 | (u8 *) (&ppsc->fwctrl_psmode)); | |
410 | ||
411 | rtlpriv->cfg->ops->set_hw_reg(hw, | |
412 | HW_VAR_SET_RPWM, | |
413 | (u8 *) (&rpwm_val)); | |
414 | } else { | |
415 | /* Reset the power save related parameters. */ | |
416 | ppsc->dot11_psmode = EACTIVE; | |
417 | } | |
418 | } | |
419 | } | |
420 | } | |
421 | ||
422 | /*Enter the leisure power save mode.*/ | |
423 | void rtl_lps_enter(struct ieee80211_hw *hw) | |
424 | { | |
425 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
426 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); | |
427 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
428 | unsigned long flag; | |
429 | ||
cc7dc0c4 | 430 | if (!ppsc->fwctrl_lps) |
0c817338 LF |
431 | return; |
432 | ||
433 | if (rtlpriv->sec.being_setkey) | |
434 | return; | |
435 | ||
7ea47240 | 436 | if (rtlpriv->link_info.busytraffic) |
0c817338 LF |
437 | return; |
438 | ||
439 | /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */ | |
440 | if (mac->cnt_after_linked < 5) | |
441 | return; | |
442 | ||
443 | if (mac->opmode == NL80211_IFTYPE_ADHOC) | |
444 | return; | |
445 | ||
446 | if (mac->link_state != MAC80211_LINKED) | |
447 | return; | |
448 | ||
449 | spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag); | |
450 | ||
cc7dc0c4 C |
451 | /* Idle for a while if we connect to AP a while ago. */ |
452 | if (mac->cnt_after_linked >= 2) { | |
453 | if (ppsc->dot11_psmode == EACTIVE) { | |
454 | RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, | |
0c817338 LF |
455 | ("Enter 802.11 power save mode...\n")); |
456 | ||
cc7dc0c4 | 457 | rtl_lps_set_psmode(hw, EAUTOPS); |
0c817338 LF |
458 | } |
459 | } | |
cc7dc0c4 | 460 | |
0c817338 LF |
461 | spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag); |
462 | } | |
463 | ||
464 | /*Leave the leisure power save mode.*/ | |
465 | void rtl_lps_leave(struct ieee80211_hw *hw) | |
466 | { | |
467 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
468 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); | |
469 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); | |
470 | unsigned long flag; | |
471 | ||
472 | spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag); | |
473 | ||
cc7dc0c4 | 474 | if (ppsc->fwctrl_lps) { |
0c817338 LF |
475 | if (ppsc->dot11_psmode != EACTIVE) { |
476 | ||
477 | /*FIX ME */ | |
478 | rtlpriv->cfg->ops->enable_interrupt(hw); | |
479 | ||
480 | if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM && | |
cc7dc0c4 | 481 | RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) && |
0c817338 LF |
482 | rtlhal->interface == INTF_PCI) { |
483 | rtlpriv->intf_ops->disable_aspm(hw); | |
cc7dc0c4 | 484 | RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); |
0c817338 LF |
485 | } |
486 | ||
487 | RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, | |
488 | ("Busy Traffic,Leave 802.11 power save..\n")); | |
489 | ||
490 | rtl_lps_set_psmode(hw, EACTIVE); | |
491 | } | |
492 | } | |
493 | spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag); | |
494 | } | |
cc7dc0c4 C |
495 | |
496 | /* For sw LPS*/ | |
497 | void rtl_swlps_beacon(struct ieee80211_hw *hw, void *data, unsigned int len) | |
498 | { | |
499 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
500 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
501 | struct ieee80211_hdr *hdr = (void *) data; | |
502 | struct ieee80211_tim_ie *tim_ie; | |
503 | u8 *tim; | |
504 | u8 tim_len; | |
505 | bool u_buffed; | |
506 | bool m_buffed; | |
507 | ||
508 | if (mac->opmode != NL80211_IFTYPE_STATION) | |
509 | return; | |
510 | ||
511 | if (!rtlpriv->psc.swctrl_lps) | |
512 | return; | |
513 | ||
514 | if (rtlpriv->mac80211.link_state != MAC80211_LINKED) | |
515 | return; | |
516 | ||
517 | if (!rtlpriv->psc.sw_ps_enabled) | |
518 | return; | |
519 | ||
520 | if (rtlpriv->psc.fwctrl_lps) | |
521 | return; | |
522 | ||
523 | if (likely(!(hw->conf.flags & IEEE80211_CONF_PS))) | |
524 | return; | |
525 | ||
526 | /* check if this really is a beacon */ | |
527 | if (!ieee80211_is_beacon(hdr->frame_control)) | |
528 | return; | |
529 | ||
530 | /* min. beacon length + FCS_LEN */ | |
531 | if (len <= 40 + FCS_LEN) | |
532 | return; | |
533 | ||
534 | /* and only beacons from the associated BSSID, please */ | |
535 | if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid)) | |
536 | return; | |
537 | ||
538 | rtlpriv->psc.last_beacon = jiffies; | |
539 | ||
540 | tim = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_TIM); | |
541 | if (!tim) | |
542 | return; | |
543 | ||
544 | if (tim[1] < sizeof(*tim_ie)) | |
545 | return; | |
546 | ||
547 | tim_len = tim[1]; | |
548 | tim_ie = (struct ieee80211_tim_ie *) &tim[2]; | |
549 | ||
550 | if (!WARN_ON_ONCE(!hw->conf.ps_dtim_period)) | |
551 | rtlpriv->psc.dtim_counter = tim_ie->dtim_count; | |
552 | ||
553 | /* Check whenever the PHY can be turned off again. */ | |
554 | ||
555 | /* 1. What about buffered unicast traffic for our AID? */ | |
556 | u_buffed = ieee80211_check_tim(tim_ie, tim_len, | |
557 | rtlpriv->mac80211.assoc_id); | |
558 | ||
559 | /* 2. Maybe the AP wants to send multicast/broadcast data? */ | |
560 | m_buffed = tim_ie->bitmap_ctrl & 0x01; | |
561 | rtlpriv->psc.multi_buffered = m_buffed; | |
562 | ||
563 | /* unicast will process by mac80211 through | |
564 | * set ~IEEE80211_CONF_PS, So we just check | |
565 | * multicast frames here */ | |
566 | if (!m_buffed) { | |
567 | /* back to low-power land. and delay is | |
568 | * prevent null power save frame tx fail */ | |
569 | queue_delayed_work(rtlpriv->works.rtl_wq, | |
570 | &rtlpriv->works.ps_work, MSECS(5)); | |
571 | } else { | |
572 | RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, ("u_bufferd: %x, " | |
573 | "m_buffered: %x\n", u_buffed, m_buffed)); | |
574 | } | |
575 | } | |
576 | ||
577 | void rtl_swlps_rf_awake(struct ieee80211_hw *hw) | |
578 | { | |
579 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
580 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); | |
581 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
582 | unsigned long flag; | |
583 | ||
584 | if (!rtlpriv->psc.swctrl_lps) | |
585 | return; | |
586 | if (mac->link_state != MAC80211_LINKED) | |
587 | return; | |
588 | ||
589 | if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM && | |
590 | RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) { | |
591 | rtlpriv->intf_ops->disable_aspm(hw); | |
592 | RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); | |
593 | } | |
594 | ||
595 | spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag); | |
596 | rtl_ps_set_rf_state(hw, ERFON, RF_CHANGE_BY_PS, false); | |
597 | spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag); | |
598 | } | |
599 | ||
600 | void rtl_swlps_rfon_wq_callback(void *data) | |
601 | { | |
602 | struct rtl_works *rtlworks = | |
603 | container_of_dwork_rtl(data, struct rtl_works, ps_rfon_wq); | |
604 | struct ieee80211_hw *hw = rtlworks->hw; | |
605 | ||
606 | rtl_swlps_rf_awake(hw); | |
607 | } | |
608 | ||
609 | void rtl_swlps_rf_sleep(struct ieee80211_hw *hw) | |
610 | { | |
611 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
612 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); | |
613 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); | |
614 | unsigned long flag; | |
615 | u8 sleep_intv; | |
616 | ||
617 | if (!rtlpriv->psc.sw_ps_enabled) | |
618 | return; | |
619 | ||
620 | if ((rtlpriv->sec.being_setkey) || | |
621 | (mac->opmode == NL80211_IFTYPE_ADHOC)) | |
622 | return; | |
623 | ||
624 | /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */ | |
625 | if ((mac->link_state != MAC80211_LINKED) || (mac->cnt_after_linked < 5)) | |
626 | return; | |
627 | ||
628 | if (rtlpriv->link_info.busytraffic) | |
629 | return; | |
630 | ||
631 | spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag); | |
632 | if (rtlpriv->psc.rfchange_inprogress) { | |
633 | spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flag); | |
634 | return; | |
635 | } | |
636 | spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flag); | |
637 | ||
638 | spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag); | |
639 | rtl_ps_set_rf_state(hw, ERFSLEEP, RF_CHANGE_BY_PS, false); | |
640 | spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag); | |
641 | ||
642 | if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM && | |
643 | !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) { | |
644 | rtlpriv->intf_ops->enable_aspm(hw); | |
645 | RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); | |
646 | } | |
647 | ||
648 | /* here is power save alg, when this beacon is DTIM | |
649 | * we will set sleep time to dtim_period * n; | |
650 | * when this beacon is not DTIM, we will set sleep | |
651 | * time to sleep_intv = rtlpriv->psc.dtim_counter or | |
652 | * MAX_SW_LPS_SLEEP_INTV(default set to 5) */ | |
653 | ||
654 | if (rtlpriv->psc.dtim_counter == 0) { | |
655 | if (hw->conf.ps_dtim_period == 1) | |
656 | sleep_intv = hw->conf.ps_dtim_period * 2; | |
657 | else | |
658 | sleep_intv = hw->conf.ps_dtim_period; | |
659 | } else { | |
660 | sleep_intv = rtlpriv->psc.dtim_counter; | |
661 | } | |
662 | ||
663 | if (sleep_intv > MAX_SW_LPS_SLEEP_INTV) | |
664 | sleep_intv = MAX_SW_LPS_SLEEP_INTV; | |
665 | ||
666 | /* this print should always be dtim_conter = 0 & | |
667 | * sleep = dtim_period, that meaons, we should | |
668 | * awake before every dtim */ | |
669 | RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, | |
670 | ("dtim_counter:%x will sleep :%d" | |
671 | " beacon_intv\n", rtlpriv->psc.dtim_counter, sleep_intv)); | |
672 | ||
673 | /* we tested that 40ms is enough for sw & hw sw delay */ | |
674 | queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.ps_rfon_wq, | |
675 | MSECS(sleep_intv * mac->vif->bss_conf.beacon_int - 40)); | |
676 | } | |
677 | ||
678 | ||
679 | void rtl_swlps_wq_callback(void *data) | |
680 | { | |
681 | struct rtl_works *rtlworks = container_of_dwork_rtl(data, | |
682 | struct rtl_works, | |
683 | ps_work); | |
684 | struct ieee80211_hw *hw = rtlworks->hw; | |
685 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
686 | bool ps = false; | |
687 | ||
688 | ps = (hw->conf.flags & IEEE80211_CONF_PS); | |
689 | ||
690 | /* we can sleep after ps null send ok */ | |
691 | if (rtlpriv->psc.state_inap) { | |
692 | rtl_swlps_rf_sleep(hw); | |
693 | ||
694 | if (rtlpriv->psc.state && !ps) { | |
695 | rtlpriv->psc.sleep_ms = jiffies_to_msecs(jiffies - | |
696 | rtlpriv->psc.last_action); | |
697 | } | |
698 | ||
699 | if (ps) | |
700 | rtlpriv->psc.last_slept = jiffies; | |
701 | ||
702 | rtlpriv->psc.last_action = jiffies; | |
703 | rtlpriv->psc.state = ps; | |
704 | } | |
705 | } |