1 /******************************************************************************
2 * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
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.
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
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
19 * The full GNU General Public License is included in this distribution in the
20 * file called LICENSE.
22 * Contact Information:
23 * wlanfae <wlanfae@realtek.com>
24 *****************************************************************************/
27 #include "r8192E_phy.h"
28 #include "r8192E_phyreg.h"
29 #include "r8190P_rtl8256.h" /* RTL8225 Radio frontend */
30 #include "r8192E_cmdpkt.h"
32 void rtl8192_hw_sleep_down(struct net_device *dev)
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");
42 spin_unlock_irqrestore(&priv->rf_ps_lock,flags);
43 RT_TRACE(COMP_DBG, "%s()============>come to sleep down\n", __func__);
45 MgntActSet_RF_State(dev, eRfSleep, RF_CHANGE_BY_PS,false);
48 void rtl8192_hw_sleep_wq(void *data)
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);
55 void rtl8192_hw_wakeup(struct net_device* dev)
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));
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);
71 void rtl8192_hw_wakeup_wq(void *data)
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);
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)
83 struct r8192_priv *priv = rtllib_priv(dev);
88 spin_lock_irqsave(&priv->ps_lock,flags);
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));
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);
107 u32 tmp = (tl>rb)?(tl-rb):(rb-tl);
108 queue_delayed_work_rsl(priv->rtllib->wq,
109 &priv->rtllib->hw_wakeup_wq,tmp);
111 queue_delayed_work_rsl(priv->rtllib->wq,
112 (void *)&priv->rtllib->hw_sleep_wq,0);
113 spin_unlock_irqrestore(&priv->ps_lock,flags);
116 void InactivePsWorkItemCallback(struct net_device *dev)
118 struct r8192_priv *priv = rtllib_priv(dev);
119 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)(&(priv->rtllib->PowerSaveControl));
121 RT_TRACE(COMP_PS, "InactivePsWorkItemCallback() ---------> \n");
122 pPSC->bSwRfProcessing = true;
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);
128 pPSC->bSwRfProcessing = false;
129 RT_TRACE(COMP_PS, "InactivePsWorkItemCallback() <--------- \n");
133 IPSEnter(struct net_device *dev)
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;
139 if (pPSC->bInactivePs)
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))
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);
156 IPSLeave(struct net_device *dev)
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;
162 if (pPSC->bInactivePs)
164 rtState = priv->rtllib->eRFPowerState;
165 if (rtState != eRfOn && !pPSC->bSwRfProcessing && priv->rtllib->RfOffReason <= RF_CHANGE_BY_IPS)
167 RT_TRACE(COMP_PS, "IPSLeave(): Turn on RF.\n");
168 pPSC->eInactivePowerState = eRfOn;
169 priv->bInPowerSaveMode = false;
170 InactivePsWorkItemCallback(dev);
174 void IPSLeave_wq(void *data)
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);
181 up(&priv->rtllib->ips_sem);
184 void rtllib_ips_leave_wq(struct net_device *dev)
186 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
187 enum rt_rf_power_state rtState;
188 rtState = priv->rtllib->eRFPowerState;
190 if (priv->rtllib->PowerSaveControl.bInactivePs){
191 if (rtState == eRfOff){
192 if (priv->rtllib->RfOffReason > RF_CHANGE_BY_IPS)
194 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",__func__);
198 printk("=========>%s(): IPSLeave\n",__func__);
199 queue_work_rsl(priv->rtllib->wq,&priv->rtllib->ips_leave_wq);
204 void rtllib_ips_leave(struct net_device *dev)
206 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
207 down(&priv->rtllib->ips_sem);
209 up(&priv->rtllib->ips_sem);
212 bool MgntActSet_802_11_PowerSaveMode(struct net_device *dev, u8 rtPsMode)
214 struct r8192_priv *priv = rtllib_priv(dev);
216 if (priv->rtllib->iw_mode == IW_MODE_ADHOC)
219 RT_TRACE(COMP_LPS,"%s(): set ieee->ps = %x\n",__func__,rtPsMode);
220 if (!priv->ps_force) {
221 priv->rtllib->ps = rtPsMode;
223 if (priv->rtllib->sta_sleep != LPS_IS_WAKE && rtPsMode == RTLLIB_PS_DISABLED) {
226 rtl8192_hw_wakeup(dev);
227 priv->rtllib->sta_sleep = LPS_IS_WAKE;
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);
240 void LeisurePSEnter(struct net_device *dev)
242 struct r8192_priv *priv = rtllib_priv(dev);
243 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)(&(priv->rtllib->PowerSaveControl));
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);
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))
253 if (pPSC->bLeisurePs) {
254 if (pPSC->LpsIdleCount >= RT_CHECK_FOR_HANG_PERIOD) {
256 if (priv->rtllib->ps == RTLLIB_PS_DISABLED) {
258 RT_TRACE(COMP_LPS, "LeisurePSEnter(): Enter 802.11 power save mode...\n");
260 if (!pPSC->bFwCtrlLPS) {
261 if (priv->rtllib->SetFwCmdHandler)
262 priv->rtllib->SetFwCmdHandler(dev, FW_CMD_LPS_ENTER);
264 MgntActSet_802_11_PowerSaveMode(dev, RTLLIB_PS_MBCAST|RTLLIB_PS_UNICAST);
267 pPSC->LpsIdleCount++;
272 void LeisurePSLeave(struct net_device *dev)
274 struct r8192_priv *priv = rtllib_priv(dev);
275 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)(&(priv->rtllib->PowerSaveControl));
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);
282 if (pPSC->bLeisurePs)
284 if (priv->rtllib->ps != RTLLIB_PS_DISABLED)
286 RT_TRACE(COMP_LPS, "LeisurePSLeave(): Busy Traffic , Leave 802.11 power save..\n");
287 MgntActSet_802_11_PowerSaveMode(dev, RTLLIB_PS_DISABLED);
289 if (!pPSC->bFwCtrlLPS)
291 if (priv->rtllib->SetFwCmdHandler)
293 priv->rtllib->SetFwCmdHandler(dev, FW_CMD_LPS_LEAVE);