ps.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 <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. rtlpriv->cfg->ops->hw_init(hw);
  46. RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
  47. /*<3> Enable Interrupt */
  48. rtlpriv->cfg->ops->enable_interrupt(hw);
  49. /*<enable timer> */
  50. rtl_watch_dog_timer_callback((unsigned long)hw);
  51. return true;
  52. }
  53. EXPORT_SYMBOL(rtl_ps_enable_nic);
  54. bool rtl_ps_disable_nic(struct ieee80211_hw *hw)
  55. {
  56. struct rtl_priv *rtlpriv = rtl_priv(hw);
  57. /*<1> Stop all timer */
  58. rtl_deinit_deferred_work(hw);
  59. /*<2> Disable Interrupt */
  60. rtlpriv->cfg->ops->disable_interrupt(hw);
  61. tasklet_kill(&rtlpriv->works.irq_tasklet);
  62. /*<3> Disable Adapter */
  63. rtlpriv->cfg->ops->hw_disable(hw);
  64. return true;
  65. }
  66. EXPORT_SYMBOL(rtl_ps_disable_nic);
  67. bool rtl_ps_set_rf_state(struct ieee80211_hw *hw,
  68. enum rf_pwrstate state_toset,
  69. u32 changesource)
  70. {
  71. struct rtl_priv *rtlpriv = rtl_priv(hw);
  72. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  73. bool actionallowed = false;
  74. switch (state_toset) {
  75. case ERFON:
  76. ppsc->rfoff_reason &= (~changesource);
  77. if ((changesource == RF_CHANGE_BY_HW) &&
  78. (ppsc->hwradiooff)) {
  79. ppsc->hwradiooff = false;
  80. }
  81. if (!ppsc->rfoff_reason) {
  82. ppsc->rfoff_reason = 0;
  83. actionallowed = true;
  84. }
  85. break;
  86. case ERFOFF:
  87. if ((changesource == RF_CHANGE_BY_HW)
  88. && (ppsc->hwradiooff == false)) {
  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 process\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,"
  229. " or EAPOL exchanging state.\n"));
  230. return false;
  231. }
  232. if (mac->link_state != MAC80211_LINKED)
  233. return false;
  234. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  235. return false;
  236. return true;
  237. }
  238. /* Change current and default preamble mode.*/
  239. static void rtl_lps_set_psmode(struct ieee80211_hw *hw, u8 rt_psmode)
  240. {
  241. struct rtl_priv *rtlpriv = rtl_priv(hw);
  242. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  243. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  244. u8 rpwm_val, fw_pwrmode;
  245. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  246. return;
  247. if (mac->link_state != MAC80211_LINKED)
  248. return;
  249. if (ppsc->dot11_psmode == rt_psmode)
  250. return;
  251. /* Update power save mode configured. */
  252. ppsc->dot11_psmode = rt_psmode;
  253. /*
  254. *<FW control LPS>
  255. *1. Enter PS mode
  256. * Set RPWM to Fw to turn RF off and send H2C fw_pwrmode
  257. * cmd to set Fw into PS mode.
  258. *2. Leave PS mode
  259. * Send H2C fw_pwrmode cmd to Fw to set Fw into Active
  260. * mode and set RPWM to turn RF on.
  261. */
  262. if ((ppsc->fwctrl_lps) && ppsc->report_linked) {
  263. bool fw_current_inps;
  264. if (ppsc->dot11_psmode == EACTIVE) {
  265. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  266. ("FW LPS leave ps_mode:%x\n",
  267. FW_PS_ACTIVE_MODE));
  268. rpwm_val = 0x0C; /* RF on */
  269. fw_pwrmode = FW_PS_ACTIVE_MODE;
  270. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SET_RPWM,
  271. (u8 *) (&rpwm_val));
  272. rtlpriv->cfg->ops->set_hw_reg(hw,
  273. HW_VAR_H2C_FW_PWRMODE,
  274. (u8 *) (&fw_pwrmode));
  275. fw_current_inps = false;
  276. rtlpriv->cfg->ops->set_hw_reg(hw,
  277. HW_VAR_FW_PSMODE_STATUS,
  278. (u8 *) (&fw_current_inps));
  279. } else {
  280. if (rtl_get_fwlps_doze(hw)) {
  281. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  282. ("FW LPS enter ps_mode:%x\n",
  283. ppsc->fwctrl_psmode));
  284. rpwm_val = 0x02; /* RF off */
  285. fw_current_inps = true;
  286. rtlpriv->cfg->ops->set_hw_reg(hw,
  287. HW_VAR_FW_PSMODE_STATUS,
  288. (u8 *) (&fw_current_inps));
  289. rtlpriv->cfg->ops->set_hw_reg(hw,
  290. HW_VAR_H2C_FW_PWRMODE,
  291. (u8 *) (&ppsc->fwctrl_psmode));
  292. rtlpriv->cfg->ops->set_hw_reg(hw,
  293. HW_VAR_SET_RPWM,
  294. (u8 *) (&rpwm_val));
  295. } else {
  296. /* Reset the power save related parameters. */
  297. ppsc->dot11_psmode = EACTIVE;
  298. }
  299. }
  300. }
  301. }
  302. /*Enter the leisure power save mode.*/
  303. void rtl_lps_enter(struct ieee80211_hw *hw)
  304. {
  305. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  306. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  307. struct rtl_priv *rtlpriv = rtl_priv(hw);
  308. if (!ppsc->fwctrl_lps)
  309. return;
  310. if (rtlpriv->sec.being_setkey)
  311. return;
  312. if (rtlpriv->link_info.busytraffic)
  313. return;
  314. /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
  315. if (mac->cnt_after_linked < 5)
  316. return;
  317. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  318. return;
  319. if (mac->link_state != MAC80211_LINKED)
  320. return;
  321. mutex_lock(&rtlpriv->locks.ps_mutex);
  322. /* Idle for a while if we connect to AP a while ago. */
  323. if (mac->cnt_after_linked >= 2) {
  324. if (ppsc->dot11_psmode == EACTIVE) {
  325. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  326. ("Enter 802.11 power save mode...\n"));
  327. rtl_lps_set_psmode(hw, EAUTOPS);
  328. }
  329. }
  330. mutex_unlock(&rtlpriv->locks.ps_mutex);
  331. }
  332. /*Leave the leisure power save mode.*/
  333. void rtl_lps_leave(struct ieee80211_hw *hw)
  334. {
  335. struct rtl_priv *rtlpriv = rtl_priv(hw);
  336. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  337. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  338. mutex_lock(&rtlpriv->locks.ps_mutex);
  339. if (ppsc->fwctrl_lps) {
  340. if (ppsc->dot11_psmode != EACTIVE) {
  341. /*FIX ME */
  342. rtlpriv->cfg->ops->enable_interrupt(hw);
  343. if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
  344. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
  345. rtlhal->interface == INTF_PCI) {
  346. rtlpriv->intf_ops->disable_aspm(hw);
  347. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  348. }
  349. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  350. ("Busy Traffic,Leave 802.11 power save..\n"));
  351. rtl_lps_set_psmode(hw, EACTIVE);
  352. }
  353. }
  354. mutex_unlock(&rtlpriv->locks.ps_mutex);
  355. }
  356. /* For sw LPS*/
  357. void rtl_swlps_beacon(struct ieee80211_hw *hw, void *data, unsigned int len)
  358. {
  359. struct rtl_priv *rtlpriv = rtl_priv(hw);
  360. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  361. struct ieee80211_hdr *hdr = (void *) data;
  362. struct ieee80211_tim_ie *tim_ie;
  363. u8 *tim;
  364. u8 tim_len;
  365. bool u_buffed;
  366. bool m_buffed;
  367. if (mac->opmode != NL80211_IFTYPE_STATION)
  368. return;
  369. if (!rtlpriv->psc.swctrl_lps)
  370. return;
  371. if (rtlpriv->mac80211.link_state != MAC80211_LINKED)
  372. return;
  373. if (!rtlpriv->psc.sw_ps_enabled)
  374. return;
  375. if (rtlpriv->psc.fwctrl_lps)
  376. return;
  377. if (likely(!(hw->conf.flags & IEEE80211_CONF_PS)))
  378. return;
  379. /* check if this really is a beacon */
  380. if (!ieee80211_is_beacon(hdr->frame_control))
  381. return;
  382. /* min. beacon length + FCS_LEN */
  383. if (len <= 40 + FCS_LEN)
  384. return;
  385. /* and only beacons from the associated BSSID, please */
  386. if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid))
  387. return;
  388. rtlpriv->psc.last_beacon = jiffies;
  389. tim = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_TIM);
  390. if (!tim)
  391. return;
  392. if (tim[1] < sizeof(*tim_ie))
  393. return;
  394. tim_len = tim[1];
  395. tim_ie = (struct ieee80211_tim_ie *) &tim[2];
  396. if (!WARN_ON_ONCE(!hw->conf.ps_dtim_period))
  397. rtlpriv->psc.dtim_counter = tim_ie->dtim_count;
  398. /* Check whenever the PHY can be turned off again. */
  399. /* 1. What about buffered unicast traffic for our AID? */
  400. u_buffed = ieee80211_check_tim(tim_ie, tim_len,
  401. rtlpriv->mac80211.assoc_id);
  402. /* 2. Maybe the AP wants to send multicast/broadcast data? */
  403. m_buffed = tim_ie->bitmap_ctrl & 0x01;
  404. rtlpriv->psc.multi_buffered = m_buffed;
  405. /* unicast will process by mac80211 through
  406. * set ~IEEE80211_CONF_PS, So we just check
  407. * multicast frames here */
  408. if (!m_buffed) {
  409. /* back to low-power land. and delay is
  410. * prevent null power save frame tx fail */
  411. queue_delayed_work(rtlpriv->works.rtl_wq,
  412. &rtlpriv->works.ps_work, MSECS(5));
  413. } else {
  414. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, ("u_bufferd: %x, "
  415. "m_buffered: %x\n", u_buffed, m_buffed));
  416. }
  417. }
  418. void rtl_swlps_rf_awake(struct ieee80211_hw *hw)
  419. {
  420. struct rtl_priv *rtlpriv = rtl_priv(hw);
  421. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  422. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  423. if (!rtlpriv->psc.swctrl_lps)
  424. return;
  425. if (mac->link_state != MAC80211_LINKED)
  426. return;
  427. if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
  428. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  429. rtlpriv->intf_ops->disable_aspm(hw);
  430. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  431. }
  432. mutex_lock(&rtlpriv->locks.ps_mutex);
  433. rtl_ps_set_rf_state(hw, ERFON, RF_CHANGE_BY_PS);
  434. mutex_unlock(&rtlpriv->locks.ps_mutex);
  435. }
  436. void rtl_swlps_rfon_wq_callback(void *data)
  437. {
  438. struct rtl_works *rtlworks =
  439. container_of_dwork_rtl(data, struct rtl_works, ps_rfon_wq);
  440. struct ieee80211_hw *hw = rtlworks->hw;
  441. rtl_swlps_rf_awake(hw);
  442. }
  443. void rtl_swlps_rf_sleep(struct ieee80211_hw *hw)
  444. {
  445. struct rtl_priv *rtlpriv = rtl_priv(hw);
  446. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  447. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  448. u8 sleep_intv;
  449. if (!rtlpriv->psc.sw_ps_enabled)
  450. return;
  451. if ((rtlpriv->sec.being_setkey) ||
  452. (mac->opmode == NL80211_IFTYPE_ADHOC))
  453. return;
  454. /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
  455. if ((mac->link_state != MAC80211_LINKED) || (mac->cnt_after_linked < 5))
  456. return;
  457. if (rtlpriv->link_info.busytraffic)
  458. return;
  459. mutex_lock(&rtlpriv->locks.ps_mutex);
  460. rtl_ps_set_rf_state(hw, ERFSLEEP, RF_CHANGE_BY_PS);
  461. mutex_unlock(&rtlpriv->locks.ps_mutex);
  462. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM &&
  463. !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  464. rtlpriv->intf_ops->enable_aspm(hw);
  465. RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  466. }
  467. /* here is power save alg, when this beacon is DTIM
  468. * we will set sleep time to dtim_period * n;
  469. * when this beacon is not DTIM, we will set sleep
  470. * time to sleep_intv = rtlpriv->psc.dtim_counter or
  471. * MAX_SW_LPS_SLEEP_INTV(default set to 5) */
  472. if (rtlpriv->psc.dtim_counter == 0) {
  473. if (hw->conf.ps_dtim_period == 1)
  474. sleep_intv = hw->conf.ps_dtim_period * 2;
  475. else
  476. sleep_intv = hw->conf.ps_dtim_period;
  477. } else {
  478. sleep_intv = rtlpriv->psc.dtim_counter;
  479. }
  480. if (sleep_intv > MAX_SW_LPS_SLEEP_INTV)
  481. sleep_intv = MAX_SW_LPS_SLEEP_INTV;
  482. /* this print should always be dtim_conter = 0 &
  483. * sleep = dtim_period, that meaons, we should
  484. * awake before every dtim */
  485. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  486. ("dtim_counter:%x will sleep :%d"
  487. " beacon_intv\n", rtlpriv->psc.dtim_counter, sleep_intv));
  488. /* we tested that 40ms is enough for sw & hw sw delay */
  489. queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.ps_rfon_wq,
  490. MSECS(sleep_intv * mac->vif->bss_conf.beacon_int - 40));
  491. }
  492. void rtl_swlps_wq_callback(void *data)
  493. {
  494. struct rtl_works *rtlworks = container_of_dwork_rtl(data,
  495. struct rtl_works,
  496. ps_work);
  497. struct ieee80211_hw *hw = rtlworks->hw;
  498. struct rtl_priv *rtlpriv = rtl_priv(hw);
  499. bool ps = false;
  500. ps = (hw->conf.flags & IEEE80211_CONF_PS);
  501. /* we can sleep after ps null send ok */
  502. if (rtlpriv->psc.state_inap) {
  503. rtl_swlps_rf_sleep(hw);
  504. if (rtlpriv->psc.state && !ps) {
  505. rtlpriv->psc.sleep_ms = jiffies_to_msecs(jiffies -
  506. rtlpriv->psc.last_action);
  507. }
  508. if (ps)
  509. rtlpriv->psc.last_slept = jiffies;
  510. rtlpriv->psc.last_action = jiffies;
  511. rtlpriv->psc.state = ps;
  512. }
  513. }