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 <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.
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 static void rtl8192_hw_sleep_down(struct net_device *dev)
34 struct r8192_priv *priv = rtllib_priv(dev);
35 unsigned long flags = 0;
37 spin_lock_irqsave(&priv->rf_ps_lock, flags);
38 if (priv->RFChangeInProgress) {
39 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
41 "rtl8192_hw_sleep_down(): RF Change in progress!\n");
44 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
45 RT_TRACE(COMP_DBG, "%s()============>come to sleep down\n", __func__);
47 MgntActSet_RF_State(dev, eRfSleep, RF_CHANGE_BY_PS, false);
50 void rtl8192_hw_sleep_wq(void *data)
52 struct rtllib_device *ieee = container_of_dwork_rsl(data,
53 struct rtllib_device, hw_sleep_wq);
54 struct net_device *dev = ieee->dev;
56 rtl8192_hw_sleep_down(dev);
59 void rtl8192_hw_wakeup(struct net_device *dev)
61 struct r8192_priv *priv = rtllib_priv(dev);
62 unsigned long flags = 0;
64 spin_lock_irqsave(&priv->rf_ps_lock, flags);
65 if (priv->RFChangeInProgress) {
66 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
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));
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);
79 void rtl8192_hw_wakeup_wq(void *data)
81 struct rtllib_device *ieee = container_of_dwork_rsl(data,
82 struct rtllib_device, hw_wakeup_wq);
83 struct net_device *dev = ieee->dev;
85 rtl8192_hw_wakeup(dev);
88 #define MIN_SLEEP_TIME 50
89 #define MAX_SLEEP_TIME 10000
90 void rtl8192_hw_to_sleep(struct net_device *dev, u64 time)
92 struct r8192_priv *priv = rtllib_priv(dev);
97 spin_lock_irqsave(&priv->ps_lock, flags);
99 time -= msecs_to_jiffies(8 + 16 + 7);
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));
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);
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);
122 static void InactivePsWorkItemCallback(struct net_device *dev)
124 struct r8192_priv *priv = rtllib_priv(dev);
125 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
126 &(priv->rtllib->PowerSaveControl);
128 RT_TRACE(COMP_PS, "InactivePsWorkItemCallback() --------->\n");
129 pPSC->bSwRfProcessing = true;
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,
136 pPSC->bSwRfProcessing = false;
137 RT_TRACE(COMP_PS, "InactivePsWorkItemCallback() <---------\n");
140 void IPSEnter(struct net_device *dev)
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;
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);
161 void IPSLeave(struct net_device *dev)
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;
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);
180 void IPSLeave_wq(void *data)
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);
187 down(&priv->rtllib->ips_sem);
189 up(&priv->rtllib->ips_sem);
192 void rtllib_ips_leave_wq(struct net_device *dev)
194 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
195 enum rt_rf_power_state rtState;
197 rtState = priv->rtllib->eRFPowerState;
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",
206 netdev_info(dev, "=========>%s(): IPSLeave\n",
208 queue_work_rsl(priv->rtllib->wq,
209 &priv->rtllib->ips_leave_wq);
214 void rtllib_ips_leave(struct net_device *dev)
216 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
218 down(&priv->rtllib->ips_sem);
220 up(&priv->rtllib->ips_sem);
223 static bool MgntActSet_802_11_PowerSaveMode(struct net_device *dev,
226 struct r8192_priv *priv = rtllib_priv(dev);
228 if (priv->rtllib->iw_mode == IW_MODE_ADHOC)
231 RT_TRACE(COMP_LPS, "%s(): set ieee->ps = %x\n", __func__, rtPsMode);
233 priv->rtllib->ps = rtPsMode;
234 if (priv->rtllib->sta_sleep != LPS_IS_WAKE &&
235 rtPsMode == RTLLIB_PS_DISABLED) {
238 rtl8192_hw_wakeup(dev);
239 priv->rtllib->sta_sleep = LPS_IS_WAKE;
241 spin_lock_irqsave(&(priv->rtllib->mgmt_tx_lock), flags);
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);
251 void LeisurePSEnter(struct net_device *dev)
253 struct r8192_priv *priv = rtllib_priv(dev);
254 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
255 &(priv->rtllib->PowerSaveControl);
257 RT_TRACE(COMP_PS, "LeisurePSEnter()...\n");
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);
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))
269 if (pPSC->bLeisurePs) {
270 if (pPSC->LpsIdleCount >= RT_CHECK_FOR_HANG_PERIOD) {
272 if (priv->rtllib->ps == RTLLIB_PS_DISABLED) {
275 "LeisurePSEnter(): Enter 802.11 power save mode...\n");
277 if (!pPSC->bFwCtrlLPS) {
278 if (priv->rtllib->SetFwCmdHandler)
279 priv->rtllib->SetFwCmdHandler(
280 dev, FW_CMD_LPS_ENTER);
282 MgntActSet_802_11_PowerSaveMode(dev,
287 pPSC->LpsIdleCount++;
291 void LeisurePSLeave(struct net_device *dev)
293 struct r8192_priv *priv = rtllib_priv(dev);
294 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
295 &(priv->rtllib->PowerSaveControl);
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);
302 if (pPSC->bLeisurePs) {
303 if (priv->rtllib->ps != RTLLIB_PS_DISABLED) {
305 "LeisurePSLeave(): Busy Traffic , Leave 802.11 power save..\n");
306 MgntActSet_802_11_PowerSaveMode(dev,
309 if (!pPSC->bFwCtrlLPS) {
310 if (priv->rtllib->SetFwCmdHandler)
311 priv->rtllib->SetFwCmdHandler(dev,