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