Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / 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,
41                          "rtl8192_hw_sleep_down(): RF Change in 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,
68                          "rtl8192_hw_wakeup(): RF Change in 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                 netdev_info(dev, "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                 netdev_info(dev, "========>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                         }
206                         netdev_info(dev, "=========>%s(): IPSLeave\n",
207                                     __func__);
208                         queue_work_rsl(priv->rtllib->wq,
209                                        &priv->rtllib->ips_leave_wq);
210                 }
211         }
212 }
213
214 void rtllib_ips_leave(struct net_device *dev)
215 {
216         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
217
218         down(&priv->rtllib->ips_sem);
219         IPSLeave(dev);
220         up(&priv->rtllib->ips_sem);
221 }
222
223 static bool MgntActSet_802_11_PowerSaveMode(struct net_device *dev,
224                                             u8 rtPsMode)
225 {
226         struct r8192_priv *priv = rtllib_priv(dev);
227
228         if (priv->rtllib->iw_mode == IW_MODE_ADHOC)
229                 return false;
230
231         RT_TRACE(COMP_LPS, "%s(): set ieee->ps = %x\n", __func__, rtPsMode);
232         if (!priv->ps_force)
233                 priv->rtllib->ps = rtPsMode;
234         if (priv->rtllib->sta_sleep != LPS_IS_WAKE &&
235             rtPsMode == RTLLIB_PS_DISABLED) {
236                 unsigned long flags;
237
238                 rtl8192_hw_wakeup(dev);
239                 priv->rtllib->sta_sleep = LPS_IS_WAKE;
240
241                 spin_lock_irqsave(&(priv->rtllib->mgmt_tx_lock), flags);
242                 RT_TRACE(COMP_DBG,
243                          "LPS leave: notify AP we are awaked ++++++++++ SendNullFunctionData\n");
244                 rtllib_sta_ps_send_null_frame(priv->rtllib, 0);
245                 spin_unlock_irqrestore(&(priv->rtllib->mgmt_tx_lock), flags);
246         }
247
248         return true;
249 }
250
251 void LeisurePSEnter(struct net_device *dev)
252 {
253         struct r8192_priv *priv = rtllib_priv(dev);
254         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
255                                         &(priv->rtllib->PowerSaveControl);
256
257         RT_TRACE(COMP_PS, "LeisurePSEnter()...\n");
258         RT_TRACE(COMP_PS,
259                  "pPSC->bLeisurePs = %d, ieee->ps = %d,pPSC->LpsIdleCount is %d,RT_CHECK_FOR_HANG_PERIOD is %d\n",
260                  pPSC->bLeisurePs, priv->rtllib->ps, pPSC->LpsIdleCount,
261                  RT_CHECK_FOR_HANG_PERIOD);
262
263         if (!((priv->rtllib->iw_mode == IW_MODE_INFRA) &&
264             (priv->rtllib->state == RTLLIB_LINKED))
265             || (priv->rtllib->iw_mode == IW_MODE_ADHOC) ||
266             (priv->rtllib->iw_mode == IW_MODE_MASTER))
267                 return;
268
269         if (pPSC->bLeisurePs) {
270                 if (pPSC->LpsIdleCount >= RT_CHECK_FOR_HANG_PERIOD) {
271
272                         if (priv->rtllib->ps == RTLLIB_PS_DISABLED) {
273
274                                 RT_TRACE(COMP_LPS,
275                                          "LeisurePSEnter(): Enter 802.11 power save mode...\n");
276
277                                 if (!pPSC->bFwCtrlLPS) {
278                                         if (priv->rtllib->SetFwCmdHandler)
279                                                 priv->rtllib->SetFwCmdHandler(
280                                                         dev, FW_CMD_LPS_ENTER);
281                                 }
282                                 MgntActSet_802_11_PowerSaveMode(dev,
283                                                          RTLLIB_PS_MBCAST |
284                                                          RTLLIB_PS_UNICAST);
285                         }
286                 } else
287                         pPSC->LpsIdleCount++;
288         }
289 }
290
291 void LeisurePSLeave(struct net_device *dev)
292 {
293         struct r8192_priv *priv = rtllib_priv(dev);
294         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
295                                         &(priv->rtllib->PowerSaveControl);
296
297
298         RT_TRACE(COMP_PS, "LeisurePSLeave()...\n");
299         RT_TRACE(COMP_PS, "pPSC->bLeisurePs = %d, ieee->ps = %d\n",
300                 pPSC->bLeisurePs, priv->rtllib->ps);
301
302         if (pPSC->bLeisurePs) {
303                 if (priv->rtllib->ps != RTLLIB_PS_DISABLED) {
304                         RT_TRACE(COMP_LPS,
305                                  "LeisurePSLeave(): Busy Traffic , Leave 802.11 power save..\n");
306                         MgntActSet_802_11_PowerSaveMode(dev,
307                                          RTLLIB_PS_DISABLED);
308
309                         if (!pPSC->bFwCtrlLPS) {
310                                 if (priv->rtllib->SetFwCmdHandler)
311                                         priv->rtllib->SetFwCmdHandler(dev,
312                                                          FW_CMD_LPS_LEAVE);
313                     }
314                 }
315         }
316 }