1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
4 *
5 * Based on the r8180 driver, which is:
6 * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
7 *
8 * Contact Information: wlanfae <wlanfae@realtek.com>
9 */
10 #include "rtl_ps.h"
11 #include "rtl_core.h"
12 #include "r8192E_phy.h"
13 #include "r8192E_phyreg.h"
14 #include "r8190P_rtl8256.h" /* RTL8225 Radio frontend */
15 #include "r8192E_cmdpkt.h"
16 #include <linux/jiffies.h>
17
_rtl92e_hw_sleep(struct net_device * dev)18 static void _rtl92e_hw_sleep(struct net_device *dev)
19 {
20 struct r8192_priv *priv = rtllib_priv(dev);
21 unsigned long flags = 0;
22
23 spin_lock_irqsave(&priv->rf_ps_lock, flags);
24 if (priv->RFChangeInProgress) {
25 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
26 RT_TRACE(COMP_DBG,
27 "%s(): RF Change in progress!\n", __func__);
28 return;
29 }
30 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
31 RT_TRACE(COMP_DBG, "%s()============>come to sleep down\n", __func__);
32
33 rtl92e_set_rf_state(dev, eRfSleep, RF_CHANGE_BY_PS);
34 }
35
rtl92e_hw_sleep_wq(void * data)36 void rtl92e_hw_sleep_wq(void *data)
37 {
38 struct rtllib_device *ieee = container_of_dwork_rsl(data,
39 struct rtllib_device, hw_sleep_wq);
40 struct net_device *dev = ieee->dev;
41
42 _rtl92e_hw_sleep(dev);
43 }
44
rtl92e_hw_wakeup(struct net_device * dev)45 void rtl92e_hw_wakeup(struct net_device *dev)
46 {
47 struct r8192_priv *priv = rtllib_priv(dev);
48 unsigned long flags = 0;
49
50 spin_lock_irqsave(&priv->rf_ps_lock, flags);
51 if (priv->RFChangeInProgress) {
52 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
53 RT_TRACE(COMP_DBG,
54 "%s(): RF Change in progress!\n", __func__);
55 schedule_delayed_work(&priv->rtllib->hw_wakeup_wq,
56 msecs_to_jiffies(10));
57 return;
58 }
59 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
60 RT_TRACE(COMP_PS, "%s()============>come to wake up\n", __func__);
61 rtl92e_set_rf_state(dev, eRfOn, RF_CHANGE_BY_PS);
62 }
63
rtl92e_hw_wakeup_wq(void * data)64 void rtl92e_hw_wakeup_wq(void *data)
65 {
66 struct rtllib_device *ieee = container_of_dwork_rsl(data,
67 struct rtllib_device, hw_wakeup_wq);
68 struct net_device *dev = ieee->dev;
69
70 rtl92e_hw_wakeup(dev);
71 }
72
73 #define MIN_SLEEP_TIME 50
74 #define MAX_SLEEP_TIME 10000
rtl92e_enter_sleep(struct net_device * dev,u64 time)75 void rtl92e_enter_sleep(struct net_device *dev, u64 time)
76 {
77 struct r8192_priv *priv = rtllib_priv(dev);
78
79 u32 tmp;
80 unsigned long flags;
81 unsigned long timeout;
82
83 spin_lock_irqsave(&priv->ps_lock, flags);
84
85 time -= msecs_to_jiffies(8 + 16 + 7);
86
87 timeout = jiffies + msecs_to_jiffies(MIN_SLEEP_TIME);
88 if (time_before((unsigned long)time, timeout)) {
89 spin_unlock_irqrestore(&priv->ps_lock, flags);
90 netdev_info(dev, "too short to sleep::%lld < %ld\n",
91 time - jiffies, msecs_to_jiffies(MIN_SLEEP_TIME));
92 return;
93 }
94 timeout = jiffies + msecs_to_jiffies(MAX_SLEEP_TIME);
95 if (time_after((unsigned long)time, timeout)) {
96 netdev_info(dev, "========>too long to sleep:%lld > %ld\n",
97 time - jiffies, msecs_to_jiffies(MAX_SLEEP_TIME));
98 spin_unlock_irqrestore(&priv->ps_lock, flags);
99 return;
100 }
101 tmp = time - jiffies;
102 schedule_delayed_work(&priv->rtllib->hw_wakeup_wq, tmp);
103 schedule_delayed_work(&priv->rtllib->hw_sleep_wq, 0);
104 spin_unlock_irqrestore(&priv->ps_lock, flags);
105 }
106
_rtl92e_ps_update_rf_state(struct net_device * dev)107 static void _rtl92e_ps_update_rf_state(struct net_device *dev)
108 {
109 struct r8192_priv *priv = rtllib_priv(dev);
110 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
111 &(priv->rtllib->PowerSaveControl);
112
113 RT_TRACE(COMP_PS, "%s() --------->\n", __func__);
114 pPSC->bSwRfProcessing = true;
115
116 RT_TRACE(COMP_PS, "%s(): Set RF to %s.\n", __func__,
117 pPSC->eInactivePowerState == eRfOff ? "OFF" : "ON");
118 rtl92e_set_rf_state(dev, pPSC->eInactivePowerState, RF_CHANGE_BY_IPS);
119
120 pPSC->bSwRfProcessing = false;
121 RT_TRACE(COMP_PS, "%s() <---------\n", __func__);
122 }
123
rtl92e_ips_enter(struct net_device * dev)124 void rtl92e_ips_enter(struct net_device *dev)
125 {
126 struct r8192_priv *priv = rtllib_priv(dev);
127 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
128 &(priv->rtllib->PowerSaveControl);
129 enum rt_rf_power_state rtState;
130
131 if (pPSC->bInactivePs) {
132 rtState = priv->rtllib->eRFPowerState;
133 if (rtState == eRfOn && !pPSC->bSwRfProcessing &&
134 (priv->rtllib->state != RTLLIB_LINKED) &&
135 (priv->rtllib->iw_mode != IW_MODE_MASTER)) {
136 RT_TRACE(COMP_PS, "%s(): Turn off RF.\n", __func__);
137 pPSC->eInactivePowerState = eRfOff;
138 priv->isRFOff = true;
139 priv->bInPowerSaveMode = true;
140 _rtl92e_ps_update_rf_state(dev);
141 }
142 }
143 }
144
rtl92e_ips_leave(struct net_device * dev)145 void rtl92e_ips_leave(struct net_device *dev)
146 {
147 struct r8192_priv *priv = rtllib_priv(dev);
148 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
149 &(priv->rtllib->PowerSaveControl);
150 enum rt_rf_power_state rtState;
151
152 if (pPSC->bInactivePs) {
153 rtState = priv->rtllib->eRFPowerState;
154 if (rtState != eRfOn && !pPSC->bSwRfProcessing &&
155 priv->rtllib->RfOffReason <= RF_CHANGE_BY_IPS) {
156 RT_TRACE(COMP_PS, "%s(): Turn on RF.\n", __func__);
157 pPSC->eInactivePowerState = eRfOn;
158 priv->bInPowerSaveMode = false;
159 _rtl92e_ps_update_rf_state(dev);
160 }
161 }
162 }
163
rtl92e_ips_leave_wq(void * data)164 void rtl92e_ips_leave_wq(void *data)
165 {
166 struct rtllib_device *ieee = container_of_work_rsl(data,
167 struct rtllib_device, ips_leave_wq);
168 struct net_device *dev = ieee->dev;
169 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
170
171 mutex_lock(&priv->rtllib->ips_mutex);
172 rtl92e_ips_leave(dev);
173 mutex_unlock(&priv->rtllib->ips_mutex);
174 }
175
rtl92e_rtllib_ips_leave_wq(struct net_device * dev)176 void rtl92e_rtllib_ips_leave_wq(struct net_device *dev)
177 {
178 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
179 enum rt_rf_power_state rtState;
180
181 rtState = priv->rtllib->eRFPowerState;
182
183 if (priv->rtllib->PowerSaveControl.bInactivePs) {
184 if (rtState == eRfOff) {
185 if (priv->rtllib->RfOffReason > RF_CHANGE_BY_IPS) {
186 netdev_warn(dev, "%s(): RF is OFF.\n",
187 __func__);
188 return;
189 }
190 netdev_info(dev, "=========>%s(): rtl92e_ips_leave\n",
191 __func__);
192 schedule_work(&priv->rtllib->ips_leave_wq);
193 }
194 }
195 }
196
rtl92e_rtllib_ips_leave(struct net_device * dev)197 void rtl92e_rtllib_ips_leave(struct net_device *dev)
198 {
199 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
200
201 mutex_lock(&priv->rtllib->ips_mutex);
202 rtl92e_ips_leave(dev);
203 mutex_unlock(&priv->rtllib->ips_mutex);
204 }
205
_rtl92e_ps_set_mode(struct net_device * dev,u8 rtPsMode)206 static bool _rtl92e_ps_set_mode(struct net_device *dev, u8 rtPsMode)
207 {
208 struct r8192_priv *priv = rtllib_priv(dev);
209
210 if (priv->rtllib->iw_mode == IW_MODE_ADHOC)
211 return false;
212
213 RT_TRACE(COMP_LPS, "%s(): set ieee->ps = %x\n", __func__, rtPsMode);
214 if (!priv->ps_force)
215 priv->rtllib->ps = rtPsMode;
216 if (priv->rtllib->sta_sleep != LPS_IS_WAKE &&
217 rtPsMode == RTLLIB_PS_DISABLED) {
218 unsigned long flags;
219
220 rtl92e_hw_wakeup(dev);
221 priv->rtllib->sta_sleep = LPS_IS_WAKE;
222
223 spin_lock_irqsave(&(priv->rtllib->mgmt_tx_lock), flags);
224 RT_TRACE(COMP_DBG,
225 "LPS leave: notify AP we are awaked ++++++++++ SendNullFunctionData\n");
226 rtllib_sta_ps_send_null_frame(priv->rtllib, 0);
227 spin_unlock_irqrestore(&(priv->rtllib->mgmt_tx_lock), flags);
228 }
229
230 return true;
231 }
232
rtl92e_leisure_ps_enter(struct net_device * dev)233 void rtl92e_leisure_ps_enter(struct net_device *dev)
234 {
235 struct r8192_priv *priv = rtllib_priv(dev);
236 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
237 &(priv->rtllib->PowerSaveControl);
238
239 RT_TRACE(COMP_PS, "%s()...\n", __func__);
240 RT_TRACE(COMP_PS,
241 "pPSC->bLeisurePs = %d, ieee->ps = %d,pPSC->LpsIdleCount is %d,RT_CHECK_FOR_HANG_PERIOD is %d\n",
242 pPSC->bLeisurePs, priv->rtllib->ps, pPSC->LpsIdleCount,
243 RT_CHECK_FOR_HANG_PERIOD);
244
245 if (!((priv->rtllib->iw_mode == IW_MODE_INFRA) &&
246 (priv->rtllib->state == RTLLIB_LINKED))
247 || (priv->rtllib->iw_mode == IW_MODE_ADHOC) ||
248 (priv->rtllib->iw_mode == IW_MODE_MASTER))
249 return;
250
251 if (pPSC->bLeisurePs) {
252 if (pPSC->LpsIdleCount >= RT_CHECK_FOR_HANG_PERIOD) {
253
254 if (priv->rtllib->ps == RTLLIB_PS_DISABLED) {
255
256 RT_TRACE(COMP_LPS,
257 "%s(): Enter 802.11 power save mode...\n", __func__);
258
259 if (!pPSC->bFwCtrlLPS) {
260 if (priv->rtllib->SetFwCmdHandler)
261 priv->rtllib->SetFwCmdHandler(
262 dev, FW_CMD_LPS_ENTER);
263 }
264 _rtl92e_ps_set_mode(dev, RTLLIB_PS_MBCAST |
265 RTLLIB_PS_UNICAST);
266 }
267 } else
268 pPSC->LpsIdleCount++;
269 }
270 }
271
rtl92e_leisure_ps_leave(struct net_device * dev)272 void rtl92e_leisure_ps_leave(struct net_device *dev)
273 {
274 struct r8192_priv *priv = rtllib_priv(dev);
275 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
276 &(priv->rtllib->PowerSaveControl);
277
278
279 RT_TRACE(COMP_PS, "%s()...\n", __func__);
280 RT_TRACE(COMP_PS, "pPSC->bLeisurePs = %d, ieee->ps = %d\n",
281 pPSC->bLeisurePs, priv->rtllib->ps);
282
283 if (pPSC->bLeisurePs) {
284 if (priv->rtllib->ps != RTLLIB_PS_DISABLED) {
285 RT_TRACE(COMP_LPS,
286 "%s(): Busy Traffic , Leave 802.11 power save..\n", __func__);
287 _rtl92e_ps_set_mode(dev, RTLLIB_PS_DISABLED);
288
289 if (!pPSC->bFwCtrlLPS) {
290 if (priv->rtllib->SetFwCmdHandler)
291 priv->rtllib->SetFwCmdHandler(dev,
292 FW_CMD_LPS_LEAVE);
293 }
294 }
295 }
296 }
297