rtlwifi: Remove unused/unneeded variables
[pandora-kernel.git] / drivers / net / wireless / rtlwifi / ps.c
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));
39         bool init_status = true;
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,
47                          ("Driver is already down!\n"));
48
49         /*<2> Enable Adapter */
50         rtlpriv->cfg->ops->hw_init(hw);
51         RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
52         /*init_status = false; */
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
60         return init_status;
61 }
62 EXPORT_SYMBOL(rtl_ps_enable_nic);
63
64 bool rtl_ps_disable_nic(struct ieee80211_hw *hw)
65 {
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);
73
74         /*<3> Disable Adapter */
75         rtlpriv->cfg->ops->hw_disable(hw);
76
77         return true;
78 }
79 EXPORT_SYMBOL(rtl_ps_disable_nic);
80
81 bool rtl_ps_set_rf_state(struct ieee80211_hw *hw,
82                          enum rf_pwrstate state_toset,
83                          u32 changesource, bool protect_or_not)
84 {
85         struct rtl_priv *rtlpriv = rtl_priv(hw);
86         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
87         enum rf_pwrstate rtstate;
88         bool actionallowed = false;
89         u16 rfwait_cnt = 0;
90         unsigned long flag;
91
92         /*protect_or_not = true; */
93
94         if (protect_or_not)
95                 goto no_protect;
96
97         /*
98          *Only one thread can change
99          *the RF state at one time, and others
100          *should wait to be executed.
101          */
102         while (true) {
103                 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag);
104                 if (ppsc->rfchange_inprogress) {
105                         spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock,
106                                                flag);
107
108                         RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
109                                  ("RF Change in progress!"
110                                   "Wait to set..state_toset(%d).\n",
111                                   state_toset));
112
113                         /* Set RF after the previous action is done.  */
114                         while (ppsc->rfchange_inprogress) {
115                                 rfwait_cnt++;
116                                 mdelay(1);
117
118                                 /*
119                                  *Wait too long, return false to avoid
120                                  *to be stuck here.
121                                  */
122                                 if (rfwait_cnt > 100)
123                                         return false;
124                         }
125                 } else {
126                         ppsc->rfchange_inprogress = true;
127                         spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock,
128                                                flag);
129                         break;
130                 }
131         }
132
133 no_protect:
134         rtstate = ppsc->rfpwr_state;
135
136         switch (state_toset) {
137         case ERFON:
138                 ppsc->rfoff_reason &= (~changesource);
139
140                 if ((changesource == RF_CHANGE_BY_HW) &&
141                     (ppsc->hwradiooff == true)) {
142                         ppsc->hwradiooff = false;
143                 }
144
145                 if (!ppsc->rfoff_reason) {
146                         ppsc->rfoff_reason = 0;
147                         actionallowed = true;
148                 }
149
150                 break;
151
152         case ERFOFF:
153
154                 if ((changesource == RF_CHANGE_BY_HW)
155                     && (ppsc->hwradiooff == false)) {
156                         ppsc->hwradiooff = true;
157                 }
158
159                 ppsc->rfoff_reason |= changesource;
160                 actionallowed = true;
161                 break;
162
163         case ERFSLEEP:
164                 ppsc->rfoff_reason |= changesource;
165                 actionallowed = true;
166                 break;
167
168         default:
169                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
170                          ("switch case not process\n"));
171                 break;
172         }
173
174         if (actionallowed)
175                 rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset);
176
177         if (!protect_or_not) {
178                 spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag);
179                 ppsc->rfchange_inprogress = false;
180                 spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flag);
181         }
182
183         return actionallowed;
184 }
185 EXPORT_SYMBOL(rtl_ps_set_rf_state);
186
187 static void _rtl_ps_inactive_ps(struct ieee80211_hw *hw)
188 {
189         struct rtl_priv *rtlpriv = rtl_priv(hw);
190         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
191         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
192
193         ppsc->swrf_processing = true;
194
195         if (ppsc->inactive_pwrstate == ERFON && rtlhal->interface == INTF_PCI) {
196                 if ((ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) &&
197                     RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_ASPM) &&
198                     rtlhal->interface == INTF_PCI) {
199                         rtlpriv->intf_ops->disable_aspm(hw);
200                         RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_ASPM);
201                 }
202         }
203
204         rtl_ps_set_rf_state(hw, ppsc->inactive_pwrstate,
205                             RF_CHANGE_BY_IPS, false);
206
207         if (ppsc->inactive_pwrstate == ERFOFF &&
208             rtlhal->interface == INTF_PCI) {
209                 if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) {
210                         rtlpriv->intf_ops->enable_aspm(hw);
211                         RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_ASPM);
212                 }
213         }
214
215         ppsc->swrf_processing = false;
216 }
217
218 void rtl_ips_nic_off_wq_callback(void *data)
219 {
220         struct rtl_works *rtlworks =
221             container_of_dwork_rtl(data, struct rtl_works, ips_nic_off_wq);
222         struct ieee80211_hw *hw = rtlworks->hw;
223         struct rtl_priv *rtlpriv = rtl_priv(hw);
224         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
225         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
226         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
227         enum rf_pwrstate rtstate;
228
229         if (mac->opmode != NL80211_IFTYPE_STATION) {
230                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
231                          ("not station return\n"));
232                 return;
233         }
234
235         if (is_hal_stop(rtlhal))
236                 return;
237
238         if (rtlpriv->sec.being_setkey)
239                 return;
240
241         if (ppsc->inactiveps) {
242                 rtstate = ppsc->rfpwr_state;
243
244                 /*
245                  *Do not enter IPS in the following conditions:
246                  *(1) RF is already OFF or Sleep
247                  *(2) swrf_processing (indicates the IPS is still under going)
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 &&
255                     !ppsc->swrf_processing &&
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;
262                         ppsc->in_powersavemode = true;
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);
286         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
287         enum rf_pwrstate rtstate;
288         unsigned long flags;
289
290         spin_lock_irqsave(&rtlpriv->locks.ips_lock, flags);
291
292         if (ppsc->inactiveps) {
293                 rtstate = ppsc->rfpwr_state;
294
295                 if (rtstate != ERFON &&
296                     !ppsc->swrf_processing &&
297                     ppsc->rfoff_reason <= RF_CHANGE_BY_IPS) {
298
299                         ppsc->inactive_pwrstate = ERFON;
300                         ppsc->in_powersavemode = false;
301
302                         _rtl_ps_inactive_ps(hw);
303                 }
304         }
305
306         spin_unlock_irqrestore(&rtlpriv->locks.ips_lock, flags);
307 }
308
309 /*for FW LPS*/
310
311 /*
312  *Determine if we can set Fw into PS mode
313  *in current condition.Return TRUE if it
314  *can enter PS mode.
315  */
316 static bool rtl_get_fwlps_doze(struct ieee80211_hw *hw)
317 {
318         struct rtl_priv *rtlpriv = rtl_priv(hw);
319         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
320         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
321         u32 ps_timediff;
322
323         ps_timediff = jiffies_to_msecs(jiffies -
324                                        ppsc->last_delaylps_stamp_jiffies);
325
326         if (ps_timediff < 2000) {
327                 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
328                          ("Delay enter Fw LPS for DHCP, ARP,"
329                           " or EAPOL exchanging state.\n"));
330                 return false;
331         }
332
333         if (mac->link_state != MAC80211_LINKED)
334                 return false;
335
336         if (mac->opmode == NL80211_IFTYPE_ADHOC)
337                 return false;
338
339         return true;
340 }
341
342 /* Change current and default preamble mode.*/
343 static void rtl_lps_set_psmode(struct ieee80211_hw *hw, u8 rt_psmode)
344 {
345         struct rtl_priv *rtlpriv = rtl_priv(hw);
346         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
347         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
348         u8 rpwm_val, fw_pwrmode;
349
350         if (mac->opmode == NL80211_IFTYPE_ADHOC)
351                 return;
352
353         if (mac->link_state != MAC80211_LINKED)
354                 return;
355
356         if (ppsc->dot11_psmode == rt_psmode)
357                 return;
358
359         /* Update power save mode configured. */
360         ppsc->dot11_psmode = rt_psmode;
361
362         /*
363          *<FW control LPS>
364          *1. Enter PS mode
365          *   Set RPWM to Fw to turn RF off and send H2C fw_pwrmode
366          *   cmd to set Fw into PS mode.
367          *2. Leave PS mode
368          *   Send H2C fw_pwrmode cmd to Fw to set Fw into Active
369          *   mode and set RPWM to turn RF on.
370          */
371
372         if ((ppsc->fwctrl_lps) && (ppsc->leisure_ps) &&
373              ppsc->report_linked) {
374                 bool fw_current_inps;
375                 if (ppsc->dot11_psmode == EACTIVE) {
376                         RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
377                                  ("FW LPS leave ps_mode:%x\n",
378                                   FW_PS_ACTIVE_MODE));
379
380                         rpwm_val = 0x0C;        /* RF on */
381                         fw_pwrmode = FW_PS_ACTIVE_MODE;
382                         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SET_RPWM,
383                                         (u8 *) (&rpwm_val));
384                         rtlpriv->cfg->ops->set_hw_reg(hw,
385                                         HW_VAR_H2C_FW_PWRMODE,
386                                         (u8 *) (&fw_pwrmode));
387                         fw_current_inps = false;
388
389                         rtlpriv->cfg->ops->set_hw_reg(hw,
390                                         HW_VAR_FW_PSMODE_STATUS,
391                                         (u8 *) (&fw_current_inps));
392
393                 } else {
394                         if (rtl_get_fwlps_doze(hw)) {
395                                 RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
396                                                 ("FW LPS enter ps_mode:%x\n",
397                                                  ppsc->fwctrl_psmode));
398
399                                 rpwm_val = 0x02;        /* RF off */
400                                 fw_current_inps = true;
401                                 rtlpriv->cfg->ops->set_hw_reg(hw,
402                                                 HW_VAR_FW_PSMODE_STATUS,
403                                                 (u8 *) (&fw_current_inps));
404                                 rtlpriv->cfg->ops->set_hw_reg(hw,
405                                                 HW_VAR_H2C_FW_PWRMODE,
406                                                 (u8 *) (&ppsc->fwctrl_psmode));
407
408                                 rtlpriv->cfg->ops->set_hw_reg(hw,
409                                                 HW_VAR_SET_RPWM,
410                                                 (u8 *) (&rpwm_val));
411                         } else {
412                                 /* Reset the power save related parameters. */
413                                 ppsc->dot11_psmode = EACTIVE;
414                         }
415                 }
416         }
417 }
418
419 /*Enter the leisure power save mode.*/
420 void rtl_lps_enter(struct ieee80211_hw *hw)
421 {
422         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
423         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
424         struct rtl_priv *rtlpriv = rtl_priv(hw);
425         unsigned long flag;
426
427         if (!(ppsc->fwctrl_lps && ppsc->leisure_ps))
428                 return;
429
430         if (rtlpriv->sec.being_setkey)
431                 return;
432
433         if (rtlpriv->link_info.busytraffic)
434                 return;
435
436         /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
437         if (mac->cnt_after_linked < 5)
438                 return;
439
440         if (mac->opmode == NL80211_IFTYPE_ADHOC)
441                 return;
442
443         if (mac->link_state != MAC80211_LINKED)
444                 return;
445
446         spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag);
447
448         if (ppsc->leisure_ps) {
449                 /* Idle for a while if we connect to AP a while ago. */
450                 if (mac->cnt_after_linked >= 2) {
451                         if (ppsc->dot11_psmode == EACTIVE) {
452                                 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
453                                         ("Enter 802.11 power save mode...\n"));
454
455                                 rtl_lps_set_psmode(hw, EAUTOPS);
456                         }
457                 }
458         }
459         spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag);
460 }
461
462 /*Leave the leisure power save mode.*/
463 void rtl_lps_leave(struct ieee80211_hw *hw)
464 {
465         struct rtl_priv *rtlpriv = rtl_priv(hw);
466         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
467         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
468         unsigned long flag;
469
470         spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag);
471
472         if (ppsc->fwctrl_lps && ppsc->leisure_ps) {
473                 if (ppsc->dot11_psmode != EACTIVE) {
474
475                         /*FIX ME */
476                         rtlpriv->cfg->ops->enable_interrupt(hw);
477
478                         if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
479                             RT_IN_PS_LEVEL(ppsc, RT_RF_LPS_LEVEL_ASPM) &&
480                             rtlhal->interface == INTF_PCI) {
481                                 rtlpriv->intf_ops->disable_aspm(hw);
482                                 RT_CLEAR_PS_LEVEL(ppsc, RT_RF_LPS_LEVEL_ASPM);
483                         }
484
485                         RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
486                                  ("Busy Traffic,Leave 802.11 power save..\n"));
487
488                         rtl_lps_set_psmode(hw, EACTIVE);
489                 }
490         }
491         spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag);
492 }