ps.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2012 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * wlanfae <wlanfae@realtek.com>
  23. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  24. * Hsinchu 300, Taiwan.
  25. *
  26. * Larry Finger <Larry.Finger@lwfinger.net>
  27. *
  28. *****************************************************************************/
  29. #include <linux/export.h>
  30. #include "wifi.h"
  31. #include "base.h"
  32. #include "ps.h"
  33. bool rtl_ps_enable_nic(struct ieee80211_hw *hw)
  34. {
  35. struct rtl_priv *rtlpriv = rtl_priv(hw);
  36. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  37. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  38. /*<1> reset trx ring */
  39. if (rtlhal->interface == INTF_PCI)
  40. rtlpriv->intf_ops->reset_trx_ring(hw);
  41. if (is_hal_stop(rtlhal))
  42. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  43. "Driver is already down!\n");
  44. /*<2> Enable Adapter */
  45. if (rtlpriv->cfg->ops->hw_init(hw))
  46. return false;
  47. RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
  48. /*<3> Enable Interrupt */
  49. rtlpriv->cfg->ops->enable_interrupt(hw);
  50. /*<enable timer> */
  51. rtl_watch_dog_timer_callback((unsigned long)hw);
  52. return true;
  53. }
  54. EXPORT_SYMBOL(rtl_ps_enable_nic);
  55. bool rtl_ps_disable_nic(struct ieee80211_hw *hw)
  56. {
  57. struct rtl_priv *rtlpriv = rtl_priv(hw);
  58. /*<1> Stop all timer */
  59. rtl_deinit_deferred_work(hw);
  60. /*<2> Disable Interrupt */
  61. rtlpriv->cfg->ops->disable_interrupt(hw);
  62. tasklet_kill(&rtlpriv->works.irq_tasklet);
  63. /*<3> Disable Adapter */
  64. rtlpriv->cfg->ops->hw_disable(hw);
  65. return true;
  66. }
  67. EXPORT_SYMBOL(rtl_ps_disable_nic);
  68. bool rtl_ps_set_rf_state(struct ieee80211_hw *hw,
  69. enum rf_pwrstate state_toset,
  70. u32 changesource)
  71. {
  72. struct rtl_priv *rtlpriv = rtl_priv(hw);
  73. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  74. bool actionallowed = false;
  75. switch (state_toset) {
  76. case ERFON:
  77. ppsc->rfoff_reason &= (~changesource);
  78. if ((changesource == RF_CHANGE_BY_HW) &&
  79. (ppsc->hwradiooff)) {
  80. ppsc->hwradiooff = false;
  81. }
  82. if (!ppsc->rfoff_reason) {
  83. ppsc->rfoff_reason = 0;
  84. actionallowed = true;
  85. }
  86. break;
  87. case ERFOFF:
  88. if ((changesource == RF_CHANGE_BY_HW) && !ppsc->hwradiooff) {
  89. ppsc->hwradiooff = true;
  90. }
  91. ppsc->rfoff_reason |= changesource;
  92. actionallowed = true;
  93. break;
  94. case ERFSLEEP:
  95. ppsc->rfoff_reason |= changesource;
  96. actionallowed = true;
  97. break;
  98. default:
  99. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  100. "switch case not processed\n");
  101. break;
  102. }
  103. if (actionallowed)
  104. rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset);
  105. return actionallowed;
  106. }
  107. EXPORT_SYMBOL(rtl_ps_set_rf_state);
  108. static void _rtl_ps_inactive_ps(struct ieee80211_hw *hw)
  109. {
  110. struct rtl_priv *rtlpriv = rtl_priv(hw);
  111. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  112. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  113. ppsc->swrf_processing = true;
  114. if (ppsc->inactive_pwrstate == ERFON &&
  115. rtlhal->interface == INTF_PCI) {
  116. if ((ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) &&
  117. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
  118. rtlhal->interface == INTF_PCI) {
  119. rtlpriv->intf_ops->disable_aspm(hw);
  120. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  121. }
  122. }
  123. rtl_ps_set_rf_state(hw, ppsc->inactive_pwrstate, RF_CHANGE_BY_IPS);
  124. if (ppsc->inactive_pwrstate == ERFOFF &&
  125. rtlhal->interface == INTF_PCI) {
  126. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM &&
  127. !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  128. rtlpriv->intf_ops->enable_aspm(hw);
  129. RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  130. }
  131. }
  132. ppsc->swrf_processing = false;
  133. }
  134. void rtl_ips_nic_off_wq_callback(void *data)
  135. {
  136. struct rtl_works *rtlworks =
  137. container_of_dwork_rtl(data, struct rtl_works, ips_nic_off_wq);
  138. struct ieee80211_hw *hw = rtlworks->hw;
  139. struct rtl_priv *rtlpriv = rtl_priv(hw);
  140. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  141. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  142. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  143. enum rf_pwrstate rtstate;
  144. if (mac->opmode != NL80211_IFTYPE_STATION) {
  145. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  146. "not station return\n");
  147. return;
  148. }
  149. if (mac->link_state > MAC80211_NOLINK)
  150. return;
  151. if (is_hal_stop(rtlhal))
  152. return;
  153. if (rtlpriv->sec.being_setkey)
  154. return;
  155. if (ppsc->inactiveps) {
  156. rtstate = ppsc->rfpwr_state;
  157. /*
  158. *Do not enter IPS in the following conditions:
  159. *(1) RF is already OFF or Sleep
  160. *(2) swrf_processing (indicates the IPS is still under going)
  161. *(3) Connectted (only disconnected can trigger IPS)
  162. *(4) IBSS (send Beacon)
  163. *(5) AP mode (send Beacon)
  164. *(6) monitor mode (rcv packet)
  165. */
  166. if (rtstate == ERFON &&
  167. !ppsc->swrf_processing &&
  168. (mac->link_state == MAC80211_NOLINK) &&
  169. !mac->act_scanning) {
  170. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  171. "IPSEnter(): Turn off RF\n");
  172. ppsc->inactive_pwrstate = ERFOFF;
  173. ppsc->in_powersavemode = true;
  174. /*rtl_pci_reset_trx_ring(hw); */
  175. _rtl_ps_inactive_ps(hw);
  176. }
  177. }
  178. }
  179. void rtl_ips_nic_off(struct ieee80211_hw *hw)
  180. {
  181. struct rtl_priv *rtlpriv = rtl_priv(hw);
  182. /*
  183. *because when link with ap, mac80211 will ask us
  184. *to disable nic quickly after scan before linking,
  185. *this will cause link failed, so we delay 100ms here
  186. */
  187. queue_delayed_work(rtlpriv->works.rtl_wq,
  188. &rtlpriv->works.ips_nic_off_wq, MSECS(100));
  189. }
  190. void rtl_ips_nic_on(struct ieee80211_hw *hw)
  191. {
  192. struct rtl_priv *rtlpriv = rtl_priv(hw);
  193. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  194. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  195. enum rf_pwrstate rtstate;
  196. unsigned long flags;
  197. if (mac->opmode != NL80211_IFTYPE_STATION)
  198. return;
  199. spin_lock_irqsave(&rtlpriv->locks.ips_lock, flags);
  200. if (ppsc->inactiveps) {
  201. rtstate = ppsc->rfpwr_state;
  202. if (rtstate != ERFON &&
  203. !ppsc->swrf_processing &&
  204. ppsc->rfoff_reason <= RF_CHANGE_BY_IPS) {
  205. ppsc->inactive_pwrstate = ERFON;
  206. ppsc->in_powersavemode = false;
  207. _rtl_ps_inactive_ps(hw);
  208. }
  209. }
  210. spin_unlock_irqrestore(&rtlpriv->locks.ips_lock, flags);
  211. }
  212. /*for FW LPS*/
  213. /*
  214. *Determine if we can set Fw into PS mode
  215. *in current condition.Return TRUE if it
  216. *can enter PS mode.
  217. */
  218. static bool rtl_get_fwlps_doze(struct ieee80211_hw *hw)
  219. {
  220. struct rtl_priv *rtlpriv = rtl_priv(hw);
  221. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  222. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  223. u32 ps_timediff;
  224. ps_timediff = jiffies_to_msecs(jiffies -
  225. ppsc->last_delaylps_stamp_jiffies);
  226. if (ps_timediff < 2000) {
  227. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  228. "Delay enter Fw LPS for DHCP, ARP, or EAPOL exchanging state\n");
  229. return false;
  230. }
  231. if (mac->link_state != MAC80211_LINKED)
  232. return false;
  233. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  234. return false;
  235. return true;
  236. }
  237. /* Change current and default preamble mode.*/
  238. static void rtl_lps_set_psmode(struct ieee80211_hw *hw, u8 rt_psmode)
  239. {
  240. struct rtl_priv *rtlpriv = rtl_priv(hw);
  241. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  242. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  243. u8 rpwm_val, fw_pwrmode;
  244. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  245. return;
  246. if (mac->link_state != MAC80211_LINKED)
  247. return;
  248. if (ppsc->dot11_psmode == rt_psmode)
  249. return;
  250. /* Update power save mode configured. */
  251. ppsc->dot11_psmode = rt_psmode;
  252. /*
  253. *<FW control LPS>
  254. *1. Enter PS mode
  255. * Set RPWM to Fw to turn RF off and send H2C fw_pwrmode
  256. * cmd to set Fw into PS mode.
  257. *2. Leave PS mode
  258. * Send H2C fw_pwrmode cmd to Fw to set Fw into Active
  259. * mode and set RPWM to turn RF on.
  260. */
  261. if ((ppsc->fwctrl_lps) && ppsc->report_linked) {
  262. bool fw_current_inps;
  263. if (ppsc->dot11_psmode == EACTIVE) {
  264. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  265. "FW LPS leave ps_mode:%x\n",
  266. FW_PS_ACTIVE_MODE);
  267. rpwm_val = 0x0C; /* RF on */
  268. fw_pwrmode = FW_PS_ACTIVE_MODE;
  269. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SET_RPWM,
  270. &rpwm_val);
  271. rtlpriv->cfg->ops->set_hw_reg(hw,
  272. HW_VAR_H2C_FW_PWRMODE,
  273. &fw_pwrmode);
  274. fw_current_inps = false;
  275. rtlpriv->cfg->ops->set_hw_reg(hw,
  276. HW_VAR_FW_PSMODE_STATUS,
  277. (u8 *) (&fw_current_inps));
  278. } else {
  279. if (rtl_get_fwlps_doze(hw)) {
  280. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  281. "FW LPS enter ps_mode:%x\n",
  282. ppsc->fwctrl_psmode);
  283. rpwm_val = 0x02; /* RF off */
  284. fw_current_inps = true;
  285. rtlpriv->cfg->ops->set_hw_reg(hw,
  286. HW_VAR_FW_PSMODE_STATUS,
  287. (u8 *) (&fw_current_inps));
  288. rtlpriv->cfg->ops->set_hw_reg(hw,
  289. HW_VAR_H2C_FW_PWRMODE,
  290. &ppsc->fwctrl_psmode);
  291. rtlpriv->cfg->ops->set_hw_reg(hw,
  292. HW_VAR_SET_RPWM,
  293. &rpwm_val);
  294. } else {
  295. /* Reset the power save related parameters. */
  296. ppsc->dot11_psmode = EACTIVE;
  297. }
  298. }
  299. }
  300. }
  301. /*Enter the leisure power save mode.*/
  302. void rtl_lps_enter(struct ieee80211_hw *hw)
  303. {
  304. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  305. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  306. struct rtl_priv *rtlpriv = rtl_priv(hw);
  307. if (!ppsc->fwctrl_lps)
  308. return;
  309. if (rtlpriv->sec.being_setkey)
  310. return;
  311. if (rtlpriv->link_info.busytraffic)
  312. return;
  313. /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
  314. if (mac->cnt_after_linked < 5)
  315. return;
  316. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  317. return;
  318. if (mac->link_state != MAC80211_LINKED)
  319. return;
  320. mutex_lock(&rtlpriv->locks.ps_mutex);
  321. /* Idle for a while if we connect to AP a while ago. */
  322. if (mac->cnt_after_linked >= 2) {
  323. if (ppsc->dot11_psmode == EACTIVE) {
  324. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  325. "Enter 802.11 power save mode...\n");
  326. rtl_lps_set_psmode(hw, EAUTOPS);
  327. }
  328. }
  329. mutex_unlock(&rtlpriv->locks.ps_mutex);
  330. }
  331. /*Leave the leisure power save mode.*/
  332. void rtl_lps_leave(struct ieee80211_hw *hw)
  333. {
  334. struct rtl_priv *rtlpriv = rtl_priv(hw);
  335. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  336. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  337. mutex_lock(&rtlpriv->locks.ps_mutex);
  338. if (ppsc->fwctrl_lps) {
  339. if (ppsc->dot11_psmode != EACTIVE) {
  340. /*FIX ME */
  341. rtlpriv->cfg->ops->enable_interrupt(hw);
  342. if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
  343. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
  344. rtlhal->interface == INTF_PCI) {
  345. rtlpriv->intf_ops->disable_aspm(hw);
  346. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  347. }
  348. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  349. "Busy Traffic,Leave 802.11 power save..\n");
  350. rtl_lps_set_psmode(hw, EACTIVE);
  351. }
  352. }
  353. mutex_unlock(&rtlpriv->locks.ps_mutex);
  354. }
  355. /* For sw LPS*/
  356. void rtl_swlps_beacon(struct ieee80211_hw *hw, void *data, unsigned int len)
  357. {
  358. struct rtl_priv *rtlpriv = rtl_priv(hw);
  359. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  360. struct ieee80211_hdr *hdr = data;
  361. struct ieee80211_tim_ie *tim_ie;
  362. u8 *tim;
  363. u8 tim_len;
  364. bool u_buffed;
  365. bool m_buffed;
  366. if (mac->opmode != NL80211_IFTYPE_STATION)
  367. return;
  368. if (!rtlpriv->psc.swctrl_lps)
  369. return;
  370. if (rtlpriv->mac80211.link_state != MAC80211_LINKED)
  371. return;
  372. if (!rtlpriv->psc.sw_ps_enabled)
  373. return;
  374. if (rtlpriv->psc.fwctrl_lps)
  375. return;
  376. if (likely(!(hw->conf.flags & IEEE80211_CONF_PS)))
  377. return;
  378. /* check if this really is a beacon */
  379. if (!ieee80211_is_beacon(hdr->frame_control))
  380. return;
  381. /* min. beacon length + FCS_LEN */
  382. if (len <= 40 + FCS_LEN)
  383. return;
  384. /* and only beacons from the associated BSSID, please */
  385. if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid))
  386. return;
  387. rtlpriv->psc.last_beacon = jiffies;
  388. tim = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_TIM);
  389. if (!tim)
  390. return;
  391. if (tim[1] < sizeof(*tim_ie))
  392. return;
  393. tim_len = tim[1];
  394. tim_ie = (struct ieee80211_tim_ie *) &tim[2];
  395. if (!WARN_ON_ONCE(!hw->conf.ps_dtim_period))
  396. rtlpriv->psc.dtim_counter = tim_ie->dtim_count;
  397. /* Check whenever the PHY can be turned off again. */
  398. /* 1. What about buffered unicast traffic for our AID? */
  399. u_buffed = ieee80211_check_tim(tim_ie, tim_len,
  400. rtlpriv->mac80211.assoc_id);
  401. /* 2. Maybe the AP wants to send multicast/broadcast data? */
  402. m_buffed = tim_ie->bitmap_ctrl & 0x01;
  403. rtlpriv->psc.multi_buffered = m_buffed;
  404. /* unicast will process by mac80211 through
  405. * set ~IEEE80211_CONF_PS, So we just check
  406. * multicast frames here */
  407. if (!m_buffed) {
  408. /* back to low-power land. and delay is
  409. * prevent null power save frame tx fail */
  410. queue_delayed_work(rtlpriv->works.rtl_wq,
  411. &rtlpriv->works.ps_work, MSECS(5));
  412. } else {
  413. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  414. "u_bufferd: %x, m_buffered: %x\n", u_buffed, m_buffed);
  415. }
  416. }
  417. void rtl_swlps_rf_awake(struct ieee80211_hw *hw)
  418. {
  419. struct rtl_priv *rtlpriv = rtl_priv(hw);
  420. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  421. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  422. if (!rtlpriv->psc.swctrl_lps)
  423. return;
  424. if (mac->link_state != MAC80211_LINKED)
  425. return;
  426. if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
  427. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  428. rtlpriv->intf_ops->disable_aspm(hw);
  429. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  430. }
  431. mutex_lock(&rtlpriv->locks.ps_mutex);
  432. rtl_ps_set_rf_state(hw, ERFON, RF_CHANGE_BY_PS);
  433. mutex_unlock(&rtlpriv->locks.ps_mutex);
  434. }
  435. void rtl_swlps_rfon_wq_callback(void *data)
  436. {
  437. struct rtl_works *rtlworks =
  438. container_of_dwork_rtl(data, struct rtl_works, ps_rfon_wq);
  439. struct ieee80211_hw *hw = rtlworks->hw;
  440. rtl_swlps_rf_awake(hw);
  441. }
  442. void rtl_swlps_rf_sleep(struct ieee80211_hw *hw)
  443. {
  444. struct rtl_priv *rtlpriv = rtl_priv(hw);
  445. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  446. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  447. u8 sleep_intv;
  448. if (!rtlpriv->psc.sw_ps_enabled)
  449. return;
  450. if ((rtlpriv->sec.being_setkey) ||
  451. (mac->opmode == NL80211_IFTYPE_ADHOC))
  452. return;
  453. /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
  454. if ((mac->link_state != MAC80211_LINKED) || (mac->cnt_after_linked < 5))
  455. return;
  456. if (rtlpriv->link_info.busytraffic)
  457. return;
  458. mutex_lock(&rtlpriv->locks.ps_mutex);
  459. rtl_ps_set_rf_state(hw, ERFSLEEP, RF_CHANGE_BY_PS);
  460. mutex_unlock(&rtlpriv->locks.ps_mutex);
  461. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM &&
  462. !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  463. rtlpriv->intf_ops->enable_aspm(hw);
  464. RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  465. }
  466. /* here is power save alg, when this beacon is DTIM
  467. * we will set sleep time to dtim_period * n;
  468. * when this beacon is not DTIM, we will set sleep
  469. * time to sleep_intv = rtlpriv->psc.dtim_counter or
  470. * MAX_SW_LPS_SLEEP_INTV(default set to 5) */
  471. if (rtlpriv->psc.dtim_counter == 0) {
  472. if (hw->conf.ps_dtim_period == 1)
  473. sleep_intv = hw->conf.ps_dtim_period * 2;
  474. else
  475. sleep_intv = hw->conf.ps_dtim_period;
  476. } else {
  477. sleep_intv = rtlpriv->psc.dtim_counter;
  478. }
  479. if (sleep_intv > MAX_SW_LPS_SLEEP_INTV)
  480. sleep_intv = MAX_SW_LPS_SLEEP_INTV;
  481. /* this print should always be dtim_conter = 0 &
  482. * sleep = dtim_period, that meaons, we should
  483. * awake before every dtim */
  484. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  485. "dtim_counter:%x will sleep :%d beacon_intv\n",
  486. rtlpriv->psc.dtim_counter, sleep_intv);
  487. /* we tested that 40ms is enough for sw & hw sw delay */
  488. queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.ps_rfon_wq,
  489. MSECS(sleep_intv * mac->vif->bss_conf.beacon_int - 40));
  490. }
  491. void rtl_swlps_wq_callback(void *data)
  492. {
  493. struct rtl_works *rtlworks = container_of_dwork_rtl(data,
  494. struct rtl_works,
  495. ps_work);
  496. struct ieee80211_hw *hw = rtlworks->hw;
  497. struct rtl_priv *rtlpriv = rtl_priv(hw);
  498. bool ps = false;
  499. ps = (hw->conf.flags & IEEE80211_CONF_PS);
  500. /* we can sleep after ps null send ok */
  501. if (rtlpriv->psc.state_inap) {
  502. rtl_swlps_rf_sleep(hw);
  503. if (rtlpriv->psc.state && !ps) {
  504. rtlpriv->psc.sleep_ms = jiffies_to_msecs(jiffies -
  505. rtlpriv->psc.last_action);
  506. }
  507. if (ps)
  508. rtlpriv->psc.last_slept = jiffies;
  509. rtlpriv->psc.last_action = jiffies;
  510. rtlpriv->psc.state = ps;
  511. }
  512. }