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);
40 RT_TRACE(COMP_DBG, "rtl8192_hw_sleep_down(): RF Change in "
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);
67 RT_TRACE(COMP_DBG, "rtl8192_hw_wakeup(): RF Change in "
69 queue_delayed_work_rsl(priv->rtllib->wq,
70 &priv->rtllib->hw_wakeup_wq, MSECS(10));
73 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
74 RT_TRACE(COMP_PS, "%s()============>come to wake up\n", __func__);
75 MgntActSet_RF_State(dev, eRfOn, RF_CHANGE_BY_PS, false);
78 void rtl8192_hw_wakeup_wq(void *data)
80 struct rtllib_device *ieee = container_of_dwork_rsl(data,
81 struct rtllib_device, hw_wakeup_wq);
82 struct net_device *dev = ieee->dev;
84 rtl8192_hw_wakeup(dev);
87 #define MIN_SLEEP_TIME 50
88 #define MAX_SLEEP_TIME 10000
89 void rtl8192_hw_to_sleep(struct net_device *dev, u64 time)
91 struct r8192_priv *priv = rtllib_priv(dev);
96 spin_lock_irqsave(&priv->ps_lock, flags);
98 time -= MSECS(8+16+7);
100 if ((time - jiffies) <= MSECS(MIN_SLEEP_TIME)) {
101 spin_unlock_irqrestore(&priv->ps_lock, flags);
102 printk(KERN_INFO "too short to sleep::%lld < %ld\n",
103 time - jiffies, MSECS(MIN_SLEEP_TIME));
107 if ((time - jiffies) > MSECS(MAX_SLEEP_TIME)) {
108 printk(KERN_INFO "========>too long to sleep:%lld > %ld\n",
109 time - jiffies, MSECS(MAX_SLEEP_TIME));
110 spin_unlock_irqrestore(&priv->ps_lock, flags);
113 tmp = time - jiffies;
114 queue_delayed_work_rsl(priv->rtllib->wq,
115 &priv->rtllib->hw_wakeup_wq, tmp);
116 queue_delayed_work_rsl(priv->rtllib->wq,
117 (void *)&priv->rtllib->hw_sleep_wq, 0);
118 spin_unlock_irqrestore(&priv->ps_lock, flags);
121 static void InactivePsWorkItemCallback(struct net_device *dev)
123 struct r8192_priv *priv = rtllib_priv(dev);
124 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
125 &(priv->rtllib->PowerSaveControl);
127 RT_TRACE(COMP_PS, "InactivePsWorkItemCallback() --------->\n");
128 pPSC->bSwRfProcessing = true;
130 RT_TRACE(COMP_PS, "InactivePsWorkItemCallback(): Set RF to %s.\n",
131 pPSC->eInactivePowerState == eRfOff ? "OFF" : "ON");
132 MgntActSet_RF_State(dev, pPSC->eInactivePowerState, RF_CHANGE_BY_IPS,
135 pPSC->bSwRfProcessing = false;
136 RT_TRACE(COMP_PS, "InactivePsWorkItemCallback() <---------\n");
139 void IPSEnter(struct net_device *dev)
141 struct r8192_priv *priv = rtllib_priv(dev);
142 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
143 &(priv->rtllib->PowerSaveControl);
144 enum rt_rf_power_state rtState;
146 if (pPSC->bInactivePs) {
147 rtState = priv->rtllib->eRFPowerState;
148 if (rtState == eRfOn && !pPSC->bSwRfProcessing &&
149 (priv->rtllib->state != RTLLIB_LINKED) &&
150 (priv->rtllib->iw_mode != IW_MODE_MASTER)) {
151 RT_TRACE(COMP_PS, "IPSEnter(): Turn off RF.\n");
152 pPSC->eInactivePowerState = eRfOff;
153 priv->isRFOff = true;
154 priv->bInPowerSaveMode = true;
155 InactivePsWorkItemCallback(dev);
160 void IPSLeave(struct net_device *dev)
162 struct r8192_priv *priv = rtllib_priv(dev);
163 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
164 &(priv->rtllib->PowerSaveControl);
165 enum rt_rf_power_state rtState;
167 if (pPSC->bInactivePs) {
168 rtState = priv->rtllib->eRFPowerState;
169 if (rtState != eRfOn && !pPSC->bSwRfProcessing &&
170 priv->rtllib->RfOffReason <= RF_CHANGE_BY_IPS) {
171 RT_TRACE(COMP_PS, "IPSLeave(): Turn on RF.\n");
172 pPSC->eInactivePowerState = eRfOn;
173 priv->bInPowerSaveMode = false;
174 InactivePsWorkItemCallback(dev);
179 void IPSLeave_wq(void *data)
181 struct rtllib_device *ieee = container_of_work_rsl(data,
182 struct rtllib_device, ips_leave_wq);
183 struct net_device *dev = ieee->dev;
184 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
186 down(&priv->rtllib->ips_sem);
188 up(&priv->rtllib->ips_sem);
191 void rtllib_ips_leave_wq(struct net_device *dev)
193 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
194 enum rt_rf_power_state rtState;
196 rtState = priv->rtllib->eRFPowerState;
198 if (priv->rtllib->PowerSaveControl.bInactivePs) {
199 if (rtState == eRfOff) {
200 if (priv->rtllib->RfOffReason > RF_CHANGE_BY_IPS) {
201 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",
205 printk(KERN_INFO "=========>%s(): IPSLeave\n",
207 queue_work_rsl(priv->rtllib->wq,
208 &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);
242 RT_TRACE(COMP_DBG, "LPS leave: notify AP we are awaked"
243 " ++++++++++ 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");
258 RT_TRACE(COMP_PS, "pPSC->bLeisurePs = %d, ieee->ps = %d,pPSC->LpsIdle"
259 "Count 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) {
274 RT_TRACE(COMP_LPS, "LeisurePSEnter(): Enter "
275 "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) {
304 RT_TRACE(COMP_LPS, "LeisurePSLeave(): Busy Traffic , "
305 "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,