Staging: rtl8192e: Eliminate use of MSECS macro
[cascardo/linux.git] / drivers / staging / rtl8192e / rtl8192e / rtl_ps.c
1 /******************************************************************************
2  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3  *
4  * Based on the r8180 driver, which is:
5  * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18  *
19  * The full GNU General Public License is included in this distribution in the
20  * file called LICENSE.
21  *
22  * Contact Information:
23  * wlanfae <wlanfae@realtek.com>
24  *****************************************************************************/
25 #include "rtl_ps.h"
26 #include "rtl_core.h"
27 #include "r8192E_phy.h"
28 #include "r8192E_phyreg.h"
29 #include "r8190P_rtl8256.h" /* RTL8225 Radio frontend */
30 #include "r8192E_cmdpkt.h"
31
32 static void rtl8192_hw_sleep_down(struct net_device *dev)
33 {
34         struct r8192_priv *priv = rtllib_priv(dev);
35         unsigned long flags = 0;
36
37         spin_lock_irqsave(&priv->rf_ps_lock, flags);
38         if (priv->RFChangeInProgress) {
39                 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
40                 RT_TRACE(COMP_DBG, "rtl8192_hw_sleep_down(): RF Change in "
41                          "progress!\n");
42                 return;
43         }
44         spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
45         RT_TRACE(COMP_DBG, "%s()============>come to sleep down\n", __func__);
46
47         MgntActSet_RF_State(dev, eRfSleep, RF_CHANGE_BY_PS, false);
48 }
49
50 void rtl8192_hw_sleep_wq(void *data)
51 {
52         struct rtllib_device *ieee = container_of_dwork_rsl(data,
53                                      struct rtllib_device, hw_sleep_wq);
54         struct net_device *dev = ieee->dev;
55
56         rtl8192_hw_sleep_down(dev);
57 }
58
59 void rtl8192_hw_wakeup(struct net_device *dev)
60 {
61         struct r8192_priv *priv = rtllib_priv(dev);
62         unsigned long flags = 0;
63
64         spin_lock_irqsave(&priv->rf_ps_lock, flags);
65         if (priv->RFChangeInProgress) {
66                 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
67                 RT_TRACE(COMP_DBG, "rtl8192_hw_wakeup(): RF Change in "
68                          "progress!\n");
69                 queue_delayed_work_rsl(priv->rtllib->wq,
70                                        &priv->rtllib->hw_wakeup_wq,
71                                        msecs_to_jiffies(10));
72                 return;
73         }
74         spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
75         RT_TRACE(COMP_PS, "%s()============>come to wake up\n", __func__);
76         MgntActSet_RF_State(dev, eRfOn, RF_CHANGE_BY_PS, false);
77 }
78
79 void rtl8192_hw_wakeup_wq(void *data)
80 {
81         struct rtllib_device *ieee = container_of_dwork_rsl(data,
82                                      struct rtllib_device, hw_wakeup_wq);
83         struct net_device *dev = ieee->dev;
84
85         rtl8192_hw_wakeup(dev);
86 }
87
88 #define MIN_SLEEP_TIME 50
89 #define MAX_SLEEP_TIME 10000
90 void rtl8192_hw_to_sleep(struct net_device *dev, u64 time)
91 {
92         struct r8192_priv *priv = rtllib_priv(dev);
93
94         u32 tmp;
95         unsigned long flags;
96
97         spin_lock_irqsave(&priv->ps_lock, flags);
98
99         time -= msecs_to_jiffies(8 + 16 + 7);
100
101         if ((time - jiffies) <= msecs_to_jiffies(MIN_SLEEP_TIME)) {
102                 spin_unlock_irqrestore(&priv->ps_lock, flags);
103                 printk(KERN_INFO "too short to sleep::%lld < %ld\n",
104                        time - jiffies, msecs_to_jiffies(MIN_SLEEP_TIME));
105                 return;
106         }
107
108         if ((time - jiffies) > msecs_to_jiffies(MAX_SLEEP_TIME)) {
109                 printk(KERN_INFO "========>too long to sleep:%lld > %ld\n",
110                        time - jiffies,  msecs_to_jiffies(MAX_SLEEP_TIME));
111                 spin_unlock_irqrestore(&priv->ps_lock, flags);
112                 return;
113         }
114         tmp = time - jiffies;
115         queue_delayed_work_rsl(priv->rtllib->wq,
116                         &priv->rtllib->hw_wakeup_wq, tmp);
117         queue_delayed_work_rsl(priv->rtllib->wq,
118                         (void *)&priv->rtllib->hw_sleep_wq, 0);
119         spin_unlock_irqrestore(&priv->ps_lock, flags);
120 }
121
122 static void InactivePsWorkItemCallback(struct net_device *dev)
123 {
124         struct r8192_priv *priv = rtllib_priv(dev);
125         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
126                                         &(priv->rtllib->PowerSaveControl);
127
128         RT_TRACE(COMP_PS, "InactivePsWorkItemCallback() --------->\n");
129         pPSC->bSwRfProcessing = true;
130
131         RT_TRACE(COMP_PS, "InactivePsWorkItemCallback(): Set RF to %s.\n",
132                  pPSC->eInactivePowerState == eRfOff ? "OFF" : "ON");
133         MgntActSet_RF_State(dev, pPSC->eInactivePowerState, RF_CHANGE_BY_IPS,
134                             false);
135
136         pPSC->bSwRfProcessing = false;
137         RT_TRACE(COMP_PS, "InactivePsWorkItemCallback() <---------\n");
138 }
139
140 void IPSEnter(struct net_device *dev)
141 {
142         struct r8192_priv *priv = rtllib_priv(dev);
143         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
144                                         &(priv->rtllib->PowerSaveControl);
145         enum rt_rf_power_state rtState;
146
147         if (pPSC->bInactivePs) {
148                 rtState = priv->rtllib->eRFPowerState;
149                 if (rtState == eRfOn && !pPSC->bSwRfProcessing &&
150                         (priv->rtllib->state != RTLLIB_LINKED) &&
151                         (priv->rtllib->iw_mode != IW_MODE_MASTER)) {
152                         RT_TRACE(COMP_PS, "IPSEnter(): Turn off RF.\n");
153                         pPSC->eInactivePowerState = eRfOff;
154                         priv->isRFOff = true;
155                         priv->bInPowerSaveMode = true;
156                         InactivePsWorkItemCallback(dev);
157                 }
158         }
159 }
160
161 void IPSLeave(struct net_device *dev)
162 {
163         struct r8192_priv *priv = rtllib_priv(dev);
164         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
165                                         &(priv->rtllib->PowerSaveControl);
166         enum rt_rf_power_state rtState;
167
168         if (pPSC->bInactivePs) {
169                 rtState = priv->rtllib->eRFPowerState;
170                 if (rtState != eRfOn  && !pPSC->bSwRfProcessing &&
171                     priv->rtllib->RfOffReason <= RF_CHANGE_BY_IPS) {
172                         RT_TRACE(COMP_PS, "IPSLeave(): Turn on RF.\n");
173                         pPSC->eInactivePowerState = eRfOn;
174                         priv->bInPowerSaveMode = false;
175                         InactivePsWorkItemCallback(dev);
176                 }
177         }
178 }
179
180 void IPSLeave_wq(void *data)
181 {
182         struct rtllib_device *ieee = container_of_work_rsl(data,
183                                      struct rtllib_device, ips_leave_wq);
184         struct net_device *dev = ieee->dev;
185         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
186
187         down(&priv->rtllib->ips_sem);
188         IPSLeave(dev);
189         up(&priv->rtllib->ips_sem);
190 }
191
192 void rtllib_ips_leave_wq(struct net_device *dev)
193 {
194         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
195         enum rt_rf_power_state rtState;
196
197         rtState = priv->rtllib->eRFPowerState;
198
199         if (priv->rtllib->PowerSaveControl.bInactivePs) {
200                 if (rtState == eRfOff) {
201                         if (priv->rtllib->RfOffReason > RF_CHANGE_BY_IPS) {
202                                 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",
203                                          __func__);
204                                 return;
205                         } else {
206                                 printk(KERN_INFO "=========>%s(): IPSLeave\n",
207                                        __func__);
208                                 queue_work_rsl(priv->rtllib->wq,
209                                                &priv->rtllib->ips_leave_wq);
210                         }
211                 }
212         }
213 }
214
215 void rtllib_ips_leave(struct net_device *dev)
216 {
217         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
218
219         down(&priv->rtllib->ips_sem);
220         IPSLeave(dev);
221         up(&priv->rtllib->ips_sem);
222 }
223
224 static bool MgntActSet_802_11_PowerSaveMode(struct net_device *dev,
225                                             u8 rtPsMode)
226 {
227         struct r8192_priv *priv = rtllib_priv(dev);
228
229         if (priv->rtllib->iw_mode == IW_MODE_ADHOC)
230                 return false;
231
232         RT_TRACE(COMP_LPS, "%s(): set ieee->ps = %x\n", __func__, rtPsMode);
233         if (!priv->ps_force)
234                 priv->rtllib->ps = rtPsMode;
235         if (priv->rtllib->sta_sleep != LPS_IS_WAKE &&
236             rtPsMode == RTLLIB_PS_DISABLED) {
237                 unsigned long flags;
238
239                 rtl8192_hw_wakeup(dev);
240                 priv->rtllib->sta_sleep = LPS_IS_WAKE;
241
242                 spin_lock_irqsave(&(priv->rtllib->mgmt_tx_lock), flags);
243                 RT_TRACE(COMP_DBG, "LPS leave: notify AP we are awaked"
244                          " ++++++++++ SendNullFunctionData\n");
245                 rtllib_sta_ps_send_null_frame(priv->rtllib, 0);
246                 spin_unlock_irqrestore(&(priv->rtllib->mgmt_tx_lock), flags);
247         }
248
249         return true;
250 }
251
252 void LeisurePSEnter(struct net_device *dev)
253 {
254         struct r8192_priv *priv = rtllib_priv(dev);
255         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
256                                         &(priv->rtllib->PowerSaveControl);
257
258         RT_TRACE(COMP_PS, "LeisurePSEnter()...\n");
259         RT_TRACE(COMP_PS, "pPSC->bLeisurePs = %d, ieee->ps = %d,pPSC->LpsIdle"
260                  "Count is %d,RT_CHECK_FOR_HANG_PERIOD is %d\n",
261                  pPSC->bLeisurePs, priv->rtllib->ps, pPSC->LpsIdleCount,
262                  RT_CHECK_FOR_HANG_PERIOD);
263
264         if (!((priv->rtllib->iw_mode == IW_MODE_INFRA) &&
265             (priv->rtllib->state == RTLLIB_LINKED))
266             || (priv->rtllib->iw_mode == IW_MODE_ADHOC) ||
267             (priv->rtllib->iw_mode == IW_MODE_MASTER))
268                 return;
269
270         if (pPSC->bLeisurePs) {
271                 if (pPSC->LpsIdleCount >= RT_CHECK_FOR_HANG_PERIOD) {
272
273                         if (priv->rtllib->ps == RTLLIB_PS_DISABLED) {
274
275                                 RT_TRACE(COMP_LPS, "LeisurePSEnter(): Enter "
276                                          "802.11 power save mode...\n");
277
278                                 if (!pPSC->bFwCtrlLPS) {
279                                         if (priv->rtllib->SetFwCmdHandler)
280                                                 priv->rtllib->SetFwCmdHandler(
281                                                         dev, FW_CMD_LPS_ENTER);
282                                 }
283                                 MgntActSet_802_11_PowerSaveMode(dev,
284                                                          RTLLIB_PS_MBCAST |
285                                                          RTLLIB_PS_UNICAST);
286                         }
287                 } else
288                         pPSC->LpsIdleCount++;
289         }
290 }
291
292 void LeisurePSLeave(struct net_device *dev)
293 {
294         struct r8192_priv *priv = rtllib_priv(dev);
295         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
296                                         &(priv->rtllib->PowerSaveControl);
297
298
299         RT_TRACE(COMP_PS, "LeisurePSLeave()...\n");
300         RT_TRACE(COMP_PS, "pPSC->bLeisurePs = %d, ieee->ps = %d\n",
301                 pPSC->bLeisurePs, priv->rtllib->ps);
302
303         if (pPSC->bLeisurePs) {
304                 if (priv->rtllib->ps != RTLLIB_PS_DISABLED) {
305                         RT_TRACE(COMP_LPS, "LeisurePSLeave(): Busy Traffic , "
306                                  "Leave 802.11 power save..\n");
307                         MgntActSet_802_11_PowerSaveMode(dev,
308                                          RTLLIB_PS_DISABLED);
309
310                         if (!pPSC->bFwCtrlLPS) {
311                                 if (priv->rtllib->SetFwCmdHandler)
312                                         priv->rtllib->SetFwCmdHandler(dev,
313                                                          FW_CMD_LPS_LEAVE);
314                     }
315                 }
316         }
317 }