ps.c 18 KB

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