ps.c 16 KB

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