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 * The full GNU General Public License is included in this distribution in the
16 * file called LICENSE.
18 * Contact Information:
19 * wlanfae <wlanfae@realtek.com>
20 *****************************************************************************/
23 #include "r8192E_phy.h"
24 #include "r8192E_phyreg.h"
25 #include "r8190P_rtl8256.h" /* RTL8225 Radio frontend */
26 #include "r8192E_cmdpkt.h"
27 #include <linux/jiffies.h>
29 static void _rtl92e_hw_sleep(struct net_device *dev)
31 struct r8192_priv *priv = rtllib_priv(dev);
32 unsigned long flags = 0;
34 spin_lock_irqsave(&priv->rf_ps_lock, flags);
35 if (priv->RFChangeInProgress) {
36 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
38 "%s(): RF Change in progress!\n", __func__);
41 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
42 RT_TRACE(COMP_DBG, "%s()============>come to sleep down\n", __func__);
44 rtl92e_set_rf_state(dev, eRfSleep, RF_CHANGE_BY_PS);
47 void rtl92e_hw_sleep_wq(void *data)
49 struct rtllib_device *ieee = container_of_dwork_rsl(data,
50 struct rtllib_device, hw_sleep_wq);
51 struct net_device *dev = ieee->dev;
53 _rtl92e_hw_sleep(dev);
56 void rtl92e_hw_wakeup(struct net_device *dev)
58 struct r8192_priv *priv = rtllib_priv(dev);
59 unsigned long flags = 0;
61 spin_lock_irqsave(&priv->rf_ps_lock, flags);
62 if (priv->RFChangeInProgress) {
63 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
65 "%s(): RF Change in progress!\n", __func__);
66 schedule_delayed_work(&priv->rtllib->hw_wakeup_wq,
67 msecs_to_jiffies(10));
70 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
71 RT_TRACE(COMP_PS, "%s()============>come to wake up\n", __func__);
72 rtl92e_set_rf_state(dev, eRfOn, RF_CHANGE_BY_PS);
75 void rtl92e_hw_wakeup_wq(void *data)
77 struct rtllib_device *ieee = container_of_dwork_rsl(data,
78 struct rtllib_device, hw_wakeup_wq);
79 struct net_device *dev = ieee->dev;
81 rtl92e_hw_wakeup(dev);
84 #define MIN_SLEEP_TIME 50
85 #define MAX_SLEEP_TIME 10000
86 void rtl92e_enter_sleep(struct net_device *dev, u64 time)
88 struct r8192_priv *priv = rtllib_priv(dev);
92 unsigned long timeout;
94 spin_lock_irqsave(&priv->ps_lock, flags);
96 time -= msecs_to_jiffies(8 + 16 + 7);
98 timeout = jiffies + msecs_to_jiffies(MIN_SLEEP_TIME);
99 if (time_before((unsigned long)time, timeout)) {
100 spin_unlock_irqrestore(&priv->ps_lock, flags);
101 netdev_info(dev, "too short to sleep::%lld < %ld\n",
102 time - jiffies, msecs_to_jiffies(MIN_SLEEP_TIME));
105 timeout = jiffies + msecs_to_jiffies(MAX_SLEEP_TIME);
106 if (time_after((unsigned long)time, timeout)) {
107 netdev_info(dev, "========>too long to sleep:%lld > %ld\n",
108 time - jiffies, msecs_to_jiffies(MAX_SLEEP_TIME));
109 spin_unlock_irqrestore(&priv->ps_lock, flags);
112 tmp = time - jiffies;
113 schedule_delayed_work(&priv->rtllib->hw_wakeup_wq, tmp);
114 schedule_delayed_work(&priv->rtllib->hw_sleep_wq, 0);
115 spin_unlock_irqrestore(&priv->ps_lock, flags);
118 static void _rtl92e_ps_update_rf_state(struct net_device *dev)
120 struct r8192_priv *priv = rtllib_priv(dev);
121 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
122 &(priv->rtllib->PowerSaveControl);
124 RT_TRACE(COMP_PS, "%s() --------->\n", __func__);
125 pPSC->bSwRfProcessing = true;
127 RT_TRACE(COMP_PS, "%s(): Set RF to %s.\n", __func__,
128 pPSC->eInactivePowerState == eRfOff ? "OFF" : "ON");
129 rtl92e_set_rf_state(dev, pPSC->eInactivePowerState, RF_CHANGE_BY_IPS);
131 pPSC->bSwRfProcessing = false;
132 RT_TRACE(COMP_PS, "%s() <---------\n", __func__);
135 void rtl92e_ips_enter(struct net_device *dev)
137 struct r8192_priv *priv = rtllib_priv(dev);
138 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
139 &(priv->rtllib->PowerSaveControl);
140 enum rt_rf_power_state rtState;
142 if (pPSC->bInactivePs) {
143 rtState = priv->rtllib->eRFPowerState;
144 if (rtState == eRfOn && !pPSC->bSwRfProcessing &&
145 (priv->rtllib->state != RTLLIB_LINKED) &&
146 (priv->rtllib->iw_mode != IW_MODE_MASTER)) {
147 RT_TRACE(COMP_PS, "%s(): Turn off RF.\n", __func__);
148 pPSC->eInactivePowerState = eRfOff;
149 priv->isRFOff = true;
150 priv->bInPowerSaveMode = true;
151 _rtl92e_ps_update_rf_state(dev);
156 void rtl92e_ips_leave(struct net_device *dev)
158 struct r8192_priv *priv = rtllib_priv(dev);
159 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
160 &(priv->rtllib->PowerSaveControl);
161 enum rt_rf_power_state rtState;
163 if (pPSC->bInactivePs) {
164 rtState = priv->rtllib->eRFPowerState;
165 if (rtState != eRfOn && !pPSC->bSwRfProcessing &&
166 priv->rtllib->RfOffReason <= RF_CHANGE_BY_IPS) {
167 RT_TRACE(COMP_PS, "%s(): Turn on RF.\n", __func__);
168 pPSC->eInactivePowerState = eRfOn;
169 priv->bInPowerSaveMode = false;
170 _rtl92e_ps_update_rf_state(dev);
175 void rtl92e_ips_leave_wq(void *data)
177 struct rtllib_device *ieee = container_of_work_rsl(data,
178 struct rtllib_device, ips_leave_wq);
179 struct net_device *dev = ieee->dev;
180 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
182 mutex_lock(&priv->rtllib->ips_mutex);
183 rtl92e_ips_leave(dev);
184 mutex_unlock(&priv->rtllib->ips_mutex);
187 void rtl92e_rtllib_ips_leave_wq(struct net_device *dev)
189 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
190 enum rt_rf_power_state rtState;
192 rtState = priv->rtllib->eRFPowerState;
194 if (priv->rtllib->PowerSaveControl.bInactivePs) {
195 if (rtState == eRfOff) {
196 if (priv->rtllib->RfOffReason > RF_CHANGE_BY_IPS) {
197 netdev_warn(dev, "%s(): RF is OFF.\n",
201 netdev_info(dev, "=========>%s(): rtl92e_ips_leave\n",
203 schedule_work(&priv->rtllib->ips_leave_wq);
208 void rtl92e_rtllib_ips_leave(struct net_device *dev)
210 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
212 mutex_lock(&priv->rtllib->ips_mutex);
213 rtl92e_ips_leave(dev);
214 mutex_unlock(&priv->rtllib->ips_mutex);
217 static bool _rtl92e_ps_set_mode(struct net_device *dev, u8 rtPsMode)
219 struct r8192_priv *priv = rtllib_priv(dev);
221 if (priv->rtllib->iw_mode == IW_MODE_ADHOC)
224 RT_TRACE(COMP_LPS, "%s(): set ieee->ps = %x\n", __func__, rtPsMode);
226 priv->rtllib->ps = rtPsMode;
227 if (priv->rtllib->sta_sleep != LPS_IS_WAKE &&
228 rtPsMode == RTLLIB_PS_DISABLED) {
231 rtl92e_hw_wakeup(dev);
232 priv->rtllib->sta_sleep = LPS_IS_WAKE;
234 spin_lock_irqsave(&(priv->rtllib->mgmt_tx_lock), flags);
236 "LPS leave: notify AP we are awaked ++++++++++ SendNullFunctionData\n");
237 rtllib_sta_ps_send_null_frame(priv->rtllib, 0);
238 spin_unlock_irqrestore(&(priv->rtllib->mgmt_tx_lock), flags);
244 void rtl92e_leisure_ps_enter(struct net_device *dev)
246 struct r8192_priv *priv = rtllib_priv(dev);
247 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
248 &(priv->rtllib->PowerSaveControl);
250 RT_TRACE(COMP_PS, "%s()...\n", __func__);
252 "pPSC->bLeisurePs = %d, ieee->ps = %d,pPSC->LpsIdleCount is %d,RT_CHECK_FOR_HANG_PERIOD is %d\n",
253 pPSC->bLeisurePs, priv->rtllib->ps, pPSC->LpsIdleCount,
254 RT_CHECK_FOR_HANG_PERIOD);
256 if (!((priv->rtllib->iw_mode == IW_MODE_INFRA) &&
257 (priv->rtllib->state == RTLLIB_LINKED))
258 || (priv->rtllib->iw_mode == IW_MODE_ADHOC) ||
259 (priv->rtllib->iw_mode == IW_MODE_MASTER))
262 if (pPSC->bLeisurePs) {
263 if (pPSC->LpsIdleCount >= RT_CHECK_FOR_HANG_PERIOD) {
265 if (priv->rtllib->ps == RTLLIB_PS_DISABLED) {
268 "%s(): Enter 802.11 power save mode...\n", __func__);
270 if (!pPSC->bFwCtrlLPS) {
271 if (priv->rtllib->SetFwCmdHandler)
272 priv->rtllib->SetFwCmdHandler(
273 dev, FW_CMD_LPS_ENTER);
275 _rtl92e_ps_set_mode(dev, RTLLIB_PS_MBCAST |
279 pPSC->LpsIdleCount++;
283 void rtl92e_leisure_ps_leave(struct net_device *dev)
285 struct r8192_priv *priv = rtllib_priv(dev);
286 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
287 &(priv->rtllib->PowerSaveControl);
290 RT_TRACE(COMP_PS, "%s()...\n", __func__);
291 RT_TRACE(COMP_PS, "pPSC->bLeisurePs = %d, ieee->ps = %d\n",
292 pPSC->bLeisurePs, priv->rtllib->ps);
294 if (pPSC->bLeisurePs) {
295 if (priv->rtllib->ps != RTLLIB_PS_DISABLED) {
297 "%s(): Busy Traffic , Leave 802.11 power save..\n", __func__);
298 _rtl92e_ps_set_mode(dev, RTLLIB_PS_DISABLED);
300 if (!pPSC->bFwCtrlLPS) {
301 if (priv->rtllib->SetFwCmdHandler)
302 priv->rtllib->SetFwCmdHandler(dev,