ps.c 18 KB

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