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"
31 #include <linux/jiffies.h>
33 static void rtl8192_hw_sleep_down(struct net_device *dev)
35 struct r8192_priv *priv = rtllib_priv(dev);
36 unsigned long flags = 0;
38 spin_lock_irqsave(&priv->rf_ps_lock, flags);
39 if (priv->RFChangeInProgress) {
40 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
42 "rtl8192_hw_sleep_down(): RF Change in progress!\n");
45 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
46 RT_TRACE(COMP_DBG, "%s()============>come to sleep down\n", __func__);
48 rtl92e_set_rf_state(dev, eRfSleep, RF_CHANGE_BY_PS);
51 void rtl92e_hw_sleep_wq(void *data)
53 struct rtllib_device *ieee = container_of_dwork_rsl(data,
54 struct rtllib_device, hw_sleep_wq);
55 struct net_device *dev = ieee->dev;
57 rtl8192_hw_sleep_down(dev);
60 void rtl92e_hw_wakeup(struct net_device *dev)
62 struct r8192_priv *priv = rtllib_priv(dev);
63 unsigned long flags = 0;
65 spin_lock_irqsave(&priv->rf_ps_lock, flags);
66 if (priv->RFChangeInProgress) {
67 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
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));
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);
80 void rtl92e_hw_wakeup_wq(void *data)
82 struct rtllib_device *ieee = container_of_dwork_rsl(data,
83 struct rtllib_device, hw_wakeup_wq);
84 struct net_device *dev = ieee->dev;
86 rtl92e_hw_wakeup(dev);
89 #define MIN_SLEEP_TIME 50
90 #define MAX_SLEEP_TIME 10000
91 void rtl92e_enter_sleep(struct net_device *dev, u64 time)
93 struct r8192_priv *priv = rtllib_priv(dev);
97 unsigned long timeout;
99 spin_lock_irqsave(&priv->ps_lock, flags);
101 time -= msecs_to_jiffies(8 + 16 + 7);
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));
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);
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);
125 static void InactivePsWorkItemCallback(struct net_device *dev)
127 struct r8192_priv *priv = rtllib_priv(dev);
128 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
129 &(priv->rtllib->PowerSaveControl);
131 RT_TRACE(COMP_PS, "InactivePsWorkItemCallback() --------->\n");
132 pPSC->bSwRfProcessing = true;
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);
138 pPSC->bSwRfProcessing = false;
139 RT_TRACE(COMP_PS, "InactivePsWorkItemCallback() <---------\n");
142 void rtl92e_ips_enter(struct net_device *dev)
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;
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);
163 void rtl92e_ips_leave(struct net_device *dev)
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;
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);
182 void rtl92e_ips_leave_wq(void *data)
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);
189 down(&priv->rtllib->ips_sem);
190 rtl92e_ips_leave(dev);
191 up(&priv->rtllib->ips_sem);
194 void rtl92e_rtllib_ips_leave_wq(struct net_device *dev)
196 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
197 enum rt_rf_power_state rtState;
199 rtState = priv->rtllib->eRFPowerState;
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",
208 netdev_info(dev, "=========>%s(): rtl92e_ips_leave\n",
210 queue_work_rsl(priv->rtllib->wq,
211 &priv->rtllib->ips_leave_wq);
216 void rtl92e_rtllib_ips_leave(struct net_device *dev)
218 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
220 down(&priv->rtllib->ips_sem);
221 rtl92e_ips_leave(dev);
222 up(&priv->rtllib->ips_sem);
225 static bool MgntActSet_802_11_PowerSaveMode(struct net_device *dev,
228 struct r8192_priv *priv = rtllib_priv(dev);
230 if (priv->rtllib->iw_mode == IW_MODE_ADHOC)
233 RT_TRACE(COMP_LPS, "%s(): set ieee->ps = %x\n", __func__, rtPsMode);
235 priv->rtllib->ps = rtPsMode;
236 if (priv->rtllib->sta_sleep != LPS_IS_WAKE &&
237 rtPsMode == RTLLIB_PS_DISABLED) {
240 rtl92e_hw_wakeup(dev);
241 priv->rtllib->sta_sleep = LPS_IS_WAKE;
243 spin_lock_irqsave(&(priv->rtllib->mgmt_tx_lock), flags);
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);
253 void rtl92e_leisure_ps_enter(struct net_device *dev)
255 struct r8192_priv *priv = rtllib_priv(dev);
256 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
257 &(priv->rtllib->PowerSaveControl);
259 RT_TRACE(COMP_PS, "rtl92e_leisure_ps_enter()...\n");
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);
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))
271 if (pPSC->bLeisurePs) {
272 if (pPSC->LpsIdleCount >= RT_CHECK_FOR_HANG_PERIOD) {
274 if (priv->rtllib->ps == RTLLIB_PS_DISABLED) {
277 "rtl92e_leisure_ps_enter(): Enter 802.11 power save mode...\n");
279 if (!pPSC->bFwCtrlLPS) {
280 if (priv->rtllib->SetFwCmdHandler)
281 priv->rtllib->SetFwCmdHandler(
282 dev, FW_CMD_LPS_ENTER);
284 MgntActSet_802_11_PowerSaveMode(dev,
289 pPSC->LpsIdleCount++;
293 void rtl92e_leisure_ps_leave(struct net_device *dev)
295 struct r8192_priv *priv = rtllib_priv(dev);
296 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
297 &(priv->rtllib->PowerSaveControl);
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);
304 if (pPSC->bLeisurePs) {
305 if (priv->rtllib->ps != RTLLIB_PS_DISABLED) {
307 "rtl92e_leisure_ps_leave(): Busy Traffic , Leave 802.11 power save..\n");
308 MgntActSet_802_11_PowerSaveMode(dev,
311 if (!pPSC->bFwCtrlLPS) {
312 if (priv->rtllib->SetFwCmdHandler)
313 priv->rtllib->SetFwCmdHandler(dev,