ps.c 17 KB

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