Merge branch 'x86-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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 #include <linux/jiffies.h>
32
33 static void rtl8192_hw_sleep_down(struct net_device *dev)
34 {
35         struct r8192_priv *priv = rtllib_priv(dev);
36         unsigned long flags = 0;
37
38         spin_lock_irqsave(&priv->rf_ps_lock, flags);
39         if (priv->RFChangeInProgress) {
40                 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
41                 RT_TRACE(COMP_DBG,
42                          "rtl8192_hw_sleep_down(): RF Change in progress!\n");
43                 return;
44         }
45         spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
46         RT_TRACE(COMP_DBG, "%s()============>come to sleep down\n", __func__);
47
48         rtl92e_set_rf_state(dev, eRfSleep, RF_CHANGE_BY_PS);
49 }
50
51 void rtl92e_hw_sleep_wq(void *data)
52 {
53         struct rtllib_device *ieee = container_of_dwork_rsl(data,
54                                      struct rtllib_device, hw_sleep_wq);
55         struct net_device *dev = ieee->dev;
56
57         rtl8192_hw_sleep_down(dev);
58 }
59
60 void rtl92e_hw_wakeup(struct net_device *dev)
61 {
62         struct r8192_priv *priv = rtllib_priv(dev);
63         unsigned long flags = 0;
64
65         spin_lock_irqsave(&priv->rf_ps_lock, flags);
66         if (priv->RFChangeInProgress) {
67                 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
68                 RT_TRACE(COMP_DBG,
69                          "rtl92e_hw_wakeup(): RF Change in progress!\n");
70                 queue_delayed_work_rsl(priv->rtllib->wq,
71                                        &priv->rtllib->hw_wakeup_wq,
72                                        msecs_to_jiffies(10));
73                 return;
74         }
75         spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
76         RT_TRACE(COMP_PS, "%s()============>come to wake up\n", __func__);
77         rtl92e_set_rf_state(dev, eRfOn, RF_CHANGE_BY_PS);
78 }
79
80 void rtl92e_hw_wakeup_wq(void *data)
81 {
82         struct rtllib_device *ieee = container_of_dwork_rsl(data,
83                                      struct rtllib_device, hw_wakeup_wq);
84         struct net_device *dev = ieee->dev;
85
86         rtl92e_hw_wakeup(dev);
87 }
88
89 #define MIN_SLEEP_TIME 50
90 #define MAX_SLEEP_TIME 10000
91 void rtl92e_enter_sleep(struct net_device *dev, u64 time)
92 {
93         struct r8192_priv *priv = rtllib_priv(dev);
94
95         u32 tmp;
96         unsigned long flags;
97         unsigned long timeout;
98
99         spin_lock_irqsave(&priv->ps_lock, flags);
100
101         time -= msecs_to_jiffies(8 + 16 + 7);
102
103         timeout = jiffies + msecs_to_jiffies(MIN_SLEEP_TIME);
104         if (time_before((unsigned long)time, timeout)) {
105                 spin_unlock_irqrestore(&priv->ps_lock, flags);
106                 netdev_info(dev, "too short to sleep::%lld < %ld\n",
107                             time - jiffies, msecs_to_jiffies(MIN_SLEEP_TIME));
108                 return;
109         }
110         timeout = jiffies + msecs_to_jiffies(MAX_SLEEP_TIME);
111         if (time_after((unsigned long)time, timeout)) {
112                 netdev_info(dev, "========>too long to sleep:%lld > %ld\n",
113                             time - jiffies, msecs_to_jiffies(MAX_SLEEP_TIME));
114                 spin_unlock_irqrestore(&priv->ps_lock, flags);
115                 return;
116         }
117         tmp = time - jiffies;
118         queue_delayed_work_rsl(priv->rtllib->wq,
119                         &priv->rtllib->hw_wakeup_wq, tmp);
120         queue_delayed_work_rsl(priv->rtllib->wq,
121                         (void *)&priv->rtllib->hw_sleep_wq, 0);
122         spin_unlock_irqrestore(&priv->ps_lock, flags);
123 }
124
125 static void InactivePsWorkItemCallback(struct net_device *dev)
126 {
127         struct r8192_priv *priv = rtllib_priv(dev);
128         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
129                                         &(priv->rtllib->PowerSaveControl);
130
131         RT_TRACE(COMP_PS, "InactivePsWorkItemCallback() --------->\n");
132         pPSC->bSwRfProcessing = true;
133
134         RT_TRACE(COMP_PS, "InactivePsWorkItemCallback(): Set RF to %s.\n",
135                  pPSC->eInactivePowerState == eRfOff ? "OFF" : "ON");
136         rtl92e_set_rf_state(dev, pPSC->eInactivePowerState, RF_CHANGE_BY_IPS);
137
138         pPSC->bSwRfProcessing = false;
139         RT_TRACE(COMP_PS, "InactivePsWorkItemCallback() <---------\n");
140 }
141
142 void rtl92e_ips_enter(struct net_device *dev)
143 {
144         struct r8192_priv *priv = rtllib_priv(dev);
145         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
146                                         &(priv->rtllib->PowerSaveControl);
147         enum rt_rf_power_state rtState;
148
149         if (pPSC->bInactivePs) {
150                 rtState = priv->rtllib->eRFPowerState;
151                 if (rtState == eRfOn && !pPSC->bSwRfProcessing &&
152                         (priv->rtllib->state != RTLLIB_LINKED) &&
153                         (priv->rtllib->iw_mode != IW_MODE_MASTER)) {
154                         RT_TRACE(COMP_PS, "rtl92e_ips_enter(): Turn off RF.\n");
155                         pPSC->eInactivePowerState = eRfOff;
156                         priv->isRFOff = true;
157                         priv->bInPowerSaveMode = true;
158                         InactivePsWorkItemCallback(dev);
159                 }
160         }
161 }
162
163 void rtl92e_ips_leave(struct net_device *dev)
164 {
165         struct r8192_priv *priv = rtllib_priv(dev);
166         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
167                                         &(priv->rtllib->PowerSaveControl);
168         enum rt_rf_power_state rtState;
169
170         if (pPSC->bInactivePs) {
171                 rtState = priv->rtllib->eRFPowerState;
172                 if (rtState != eRfOn  && !pPSC->bSwRfProcessing &&
173                     priv->rtllib->RfOffReason <= RF_CHANGE_BY_IPS) {
174                         RT_TRACE(COMP_PS, "rtl92e_ips_leave(): Turn on RF.\n");
175                         pPSC->eInactivePowerState = eRfOn;
176                         priv->bInPowerSaveMode = false;
177                         InactivePsWorkItemCallback(dev);
178                 }
179         }
180 }
181
182 void rtl92e_ips_leave_wq(void *data)
183 {
184         struct rtllib_device *ieee = container_of_work_rsl(data,
185                                      struct rtllib_device, ips_leave_wq);
186         struct net_device *dev = ieee->dev;
187         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
188
189         down(&priv->rtllib->ips_sem);
190         rtl92e_ips_leave(dev);
191         up(&priv->rtllib->ips_sem);
192 }
193
194 void rtl92e_rtllib_ips_leave_wq(struct net_device *dev)
195 {
196         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
197         enum rt_rf_power_state rtState;
198
199         rtState = priv->rtllib->eRFPowerState;
200
201         if (priv->rtllib->PowerSaveControl.bInactivePs) {
202                 if (rtState == eRfOff) {
203                         if (priv->rtllib->RfOffReason > RF_CHANGE_BY_IPS) {
204                                 netdev_warn(dev, "%s(): RF is OFF.\n",
205                                             __func__);
206                                 return;
207                         }
208                         netdev_info(dev, "=========>%s(): rtl92e_ips_leave\n",
209                                     __func__);
210                         queue_work_rsl(priv->rtllib->wq,
211                                        &priv->rtllib->ips_leave_wq);
212                 }
213         }
214 }
215
216 void rtl92e_rtllib_ips_leave(struct net_device *dev)
217 {
218         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
219
220         down(&priv->rtllib->ips_sem);
221         rtl92e_ips_leave(dev);
222         up(&priv->rtllib->ips_sem);
223 }
224
225 static bool MgntActSet_802_11_PowerSaveMode(struct net_device *dev,
226                                             u8 rtPsMode)
227 {
228         struct r8192_priv *priv = rtllib_priv(dev);
229
230         if (priv->rtllib->iw_mode == IW_MODE_ADHOC)
231                 return false;
232
233         RT_TRACE(COMP_LPS, "%s(): set ieee->ps = %x\n", __func__, rtPsMode);
234         if (!priv->ps_force)
235                 priv->rtllib->ps = rtPsMode;
236         if (priv->rtllib->sta_sleep != LPS_IS_WAKE &&
237             rtPsMode == RTLLIB_PS_DISABLED) {
238                 unsigned long flags;
239
240                 rtl92e_hw_wakeup(dev);
241                 priv->rtllib->sta_sleep = LPS_IS_WAKE;
242
243                 spin_lock_irqsave(&(priv->rtllib->mgmt_tx_lock), flags);
244                 RT_TRACE(COMP_DBG,
245                          "LPS leave: notify AP we are awaked ++++++++++ SendNullFunctionData\n");
246                 rtllib_sta_ps_send_null_frame(priv->rtllib, 0);
247                 spin_unlock_irqrestore(&(priv->rtllib->mgmt_tx_lock), flags);
248         }
249
250         return true;
251 }
252
253 void rtl92e_leisure_ps_enter(struct net_device *dev)
254 {
255         struct r8192_priv *priv = rtllib_priv(dev);
256         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
257                                         &(priv->rtllib->PowerSaveControl);
258
259         RT_TRACE(COMP_PS, "rtl92e_leisure_ps_enter()...\n");
260         RT_TRACE(COMP_PS,
261                  "pPSC->bLeisurePs = %d, ieee->ps = %d,pPSC->LpsIdleCount is %d,RT_CHECK_FOR_HANG_PERIOD is %d\n",
262                  pPSC->bLeisurePs, priv->rtllib->ps, pPSC->LpsIdleCount,
263                  RT_CHECK_FOR_HANG_PERIOD);
264
265         if (!((priv->rtllib->iw_mode == IW_MODE_INFRA) &&
266             (priv->rtllib->state == RTLLIB_LINKED))
267             || (priv->rtllib->iw_mode == IW_MODE_ADHOC) ||
268             (priv->rtllib->iw_mode == IW_MODE_MASTER))
269                 return;
270
271         if (pPSC->bLeisurePs) {
272                 if (pPSC->LpsIdleCount >= RT_CHECK_FOR_HANG_PERIOD) {
273
274                         if (priv->rtllib->ps == RTLLIB_PS_DISABLED) {
275
276                                 RT_TRACE(COMP_LPS,
277                                          "rtl92e_leisure_ps_enter(): Enter 802.11 power save mode...\n");
278
279                                 if (!pPSC->bFwCtrlLPS) {
280                                         if (priv->rtllib->SetFwCmdHandler)
281                                                 priv->rtllib->SetFwCmdHandler(
282                                                         dev, FW_CMD_LPS_ENTER);
283                                 }
284                                 MgntActSet_802_11_PowerSaveMode(dev,
285                                                          RTLLIB_PS_MBCAST |
286                                                          RTLLIB_PS_UNICAST);
287                         }
288                 } else
289                         pPSC->LpsIdleCount++;
290         }
291 }
292
293 void rtl92e_leisure_ps_leave(struct net_device *dev)
294 {
295         struct r8192_priv *priv = rtllib_priv(dev);
296         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
297                                         &(priv->rtllib->PowerSaveControl);
298
299
300         RT_TRACE(COMP_PS, "rtl92e_leisure_ps_leave()...\n");
301         RT_TRACE(COMP_PS, "pPSC->bLeisurePs = %d, ieee->ps = %d\n",
302                 pPSC->bLeisurePs, priv->rtllib->ps);
303
304         if (pPSC->bLeisurePs) {
305                 if (priv->rtllib->ps != RTLLIB_PS_DISABLED) {
306                         RT_TRACE(COMP_LPS,
307                                  "rtl92e_leisure_ps_leave(): Busy Traffic , Leave 802.11 power save..\n");
308                         MgntActSet_802_11_PowerSaveMode(dev,
309                                          RTLLIB_PS_DISABLED);
310
311                         if (!pPSC->bFwCtrlLPS) {
312                                 if (priv->rtllib->SetFwCmdHandler)
313                                         priv->rtllib->SetFwCmdHandler(dev,
314                                                          FW_CMD_LPS_LEAVE);
315                     }
316                 }
317         }
318 }