ps.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. bool init_status = true;
  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. /*init_status = false; */
  48. /*<3> Enable Interrupt */
  49. rtlpriv->cfg->ops->enable_interrupt(hw);
  50. /*<enable timer> */
  51. rtl_watch_dog_timer_callback((unsigned long)hw);
  52. return init_status;
  53. }
  54. EXPORT_SYMBOL(rtl_ps_enable_nic);
  55. bool rtl_ps_disable_nic(struct ieee80211_hw *hw)
  56. {
  57. bool status = true;
  58. struct rtl_priv *rtlpriv = rtl_priv(hw);
  59. /*<1> Stop all timer */
  60. rtl_deinit_deferred_work(hw);
  61. /*<2> Disable Interrupt */
  62. rtlpriv->cfg->ops->disable_interrupt(hw);
  63. /*<3> Disable Adapter */
  64. rtlpriv->cfg->ops->hw_disable(hw);
  65. return status;
  66. }
  67. EXPORT_SYMBOL(rtl_ps_disable_nic);
  68. bool rtl_ps_set_rf_state(struct ieee80211_hw *hw,
  69. enum rf_pwrstate state_toset,
  70. u32 changesource, bool protect_or_not)
  71. {
  72. struct rtl_priv *rtlpriv = rtl_priv(hw);
  73. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  74. enum rf_pwrstate rtstate;
  75. bool b_actionallowed = false;
  76. u16 rfwait_cnt = 0;
  77. unsigned long flag;
  78. /*protect_or_not = true; */
  79. if (protect_or_not)
  80. goto no_protect;
  81. /*
  82. *Only one thread can change
  83. *the RF state at one time, and others
  84. *should wait to be executed.
  85. */
  86. while (true) {
  87. spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag);
  88. if (ppsc->rfchange_inprogress) {
  89. spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock,
  90. flag);
  91. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  92. ("RF Change in progress!"
  93. "Wait to set..state_toset(%d).\n",
  94. state_toset));
  95. /* Set RF after the previous action is done. */
  96. while (ppsc->rfchange_inprogress) {
  97. rfwait_cnt++;
  98. mdelay(1);
  99. /*
  100. *Wait too long, return false to avoid
  101. *to be stuck here.
  102. */
  103. if (rfwait_cnt > 100)
  104. return false;
  105. }
  106. } else {
  107. ppsc->rfchange_inprogress = true;
  108. spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock,
  109. flag);
  110. break;
  111. }
  112. }
  113. no_protect:
  114. rtstate = ppsc->rfpwr_state;
  115. switch (state_toset) {
  116. case ERFON:
  117. ppsc->rfoff_reason &= (~changesource);
  118. if ((changesource == RF_CHANGE_BY_HW) &&
  119. (ppsc->b_hwradiooff == true)) {
  120. ppsc->b_hwradiooff = false;
  121. }
  122. if (!ppsc->rfoff_reason) {
  123. ppsc->rfoff_reason = 0;
  124. b_actionallowed = true;
  125. }
  126. break;
  127. case ERFOFF:
  128. if ((changesource == RF_CHANGE_BY_HW)
  129. && (ppsc->b_hwradiooff == false)) {
  130. ppsc->b_hwradiooff = true;
  131. }
  132. ppsc->rfoff_reason |= changesource;
  133. b_actionallowed = true;
  134. break;
  135. case ERFSLEEP:
  136. ppsc->rfoff_reason |= changesource;
  137. b_actionallowed = true;
  138. break;
  139. default:
  140. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  141. ("switch case not process\n"));
  142. break;
  143. }
  144. if (b_actionallowed)
  145. rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset);
  146. if (!protect_or_not) {
  147. spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag);
  148. ppsc->rfchange_inprogress = false;
  149. spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flag);
  150. }
  151. return b_actionallowed;
  152. }
  153. EXPORT_SYMBOL(rtl_ps_set_rf_state);
  154. static void _rtl_ps_inactive_ps(struct ieee80211_hw *hw)
  155. {
  156. struct rtl_priv *rtlpriv = rtl_priv(hw);
  157. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  158. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  159. ppsc->b_swrf_processing = true;
  160. if (ppsc->inactive_pwrstate == ERFON && rtlhal->interface == INTF_PCI) {
  161. if ((ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) &&
  162. RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_ASPM) &&
  163. rtlhal->interface == INTF_PCI) {
  164. rtlpriv->intf_ops->disable_aspm(hw);
  165. RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_ASPM);
  166. }
  167. }
  168. rtl_ps_set_rf_state(hw, ppsc->inactive_pwrstate,
  169. RF_CHANGE_BY_IPS, false);
  170. if (ppsc->inactive_pwrstate == ERFOFF &&
  171. rtlhal->interface == INTF_PCI) {
  172. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) {
  173. rtlpriv->intf_ops->enable_aspm(hw);
  174. RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_ASPM);
  175. }
  176. }
  177. ppsc->b_swrf_processing = false;
  178. }
  179. void rtl_ips_nic_off_wq_callback(void *data)
  180. {
  181. struct rtl_works *rtlworks =
  182. container_of_dwork_rtl(data, struct rtl_works, ips_nic_off_wq);
  183. struct ieee80211_hw *hw = rtlworks->hw;
  184. struct rtl_priv *rtlpriv = rtl_priv(hw);
  185. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  186. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  187. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  188. enum rf_pwrstate rtstate;
  189. if (mac->opmode != NL80211_IFTYPE_STATION) {
  190. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  191. ("not station return\n"));
  192. return;
  193. }
  194. if (is_hal_stop(rtlhal))
  195. return;
  196. if (rtlpriv->sec.being_setkey)
  197. return;
  198. if (ppsc->b_inactiveps) {
  199. rtstate = ppsc->rfpwr_state;
  200. /*
  201. *Do not enter IPS in the following conditions:
  202. *(1) RF is already OFF or Sleep
  203. *(2) b_swrf_processing (indicates the IPS is still under going)
  204. *(3) Connectted (only disconnected can trigger IPS)
  205. *(4) IBSS (send Beacon)
  206. *(5) AP mode (send Beacon)
  207. *(6) monitor mode (rcv packet)
  208. */
  209. if (rtstate == ERFON &&
  210. !ppsc->b_swrf_processing &&
  211. (mac->link_state == MAC80211_NOLINK) &&
  212. !mac->act_scanning) {
  213. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  214. ("IPSEnter(): Turn off RF.\n"));
  215. ppsc->inactive_pwrstate = ERFOFF;
  216. ppsc->b_in_powersavemode = true;
  217. /*rtl_pci_reset_trx_ring(hw); */
  218. _rtl_ps_inactive_ps(hw);
  219. }
  220. }
  221. }
  222. void rtl_ips_nic_off(struct ieee80211_hw *hw)
  223. {
  224. struct rtl_priv *rtlpriv = rtl_priv(hw);
  225. /*
  226. *because when link with ap, mac80211 will ask us
  227. *to disable nic quickly after scan before linking,
  228. *this will cause link failed, so we delay 100ms here
  229. */
  230. queue_delayed_work(rtlpriv->works.rtl_wq,
  231. &rtlpriv->works.ips_nic_off_wq, MSECS(100));
  232. }
  233. void rtl_ips_nic_on(struct ieee80211_hw *hw)
  234. {
  235. struct rtl_priv *rtlpriv = rtl_priv(hw);
  236. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  237. enum rf_pwrstate rtstate;
  238. mutex_lock(&rtlpriv->locks.ips_mutex);
  239. if (ppsc->b_inactiveps) {
  240. rtstate = ppsc->rfpwr_state;
  241. if (rtstate != ERFON &&
  242. !ppsc->b_swrf_processing &&
  243. ppsc->rfoff_reason <= RF_CHANGE_BY_IPS) {
  244. ppsc->inactive_pwrstate = ERFON;
  245. ppsc->b_in_powersavemode = false;
  246. _rtl_ps_inactive_ps(hw);
  247. }
  248. }
  249. mutex_unlock(&rtlpriv->locks.ips_mutex);
  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->b_fwctrl_lps) && (ppsc->b_leisure_ps) &&
  302. ppsc->report_linked) {
  303. bool b_fw_current_inps;
  304. if (ppsc->dot11_psmode == EACTIVE) {
  305. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  306. ("FW LPS leave ps_mode:%x\n",
  307. FW_PS_ACTIVE_MODE));
  308. rpwm_val = 0x0C; /* RF on */
  309. fw_pwrmode = FW_PS_ACTIVE_MODE;
  310. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SET_RPWM,
  311. (u8 *) (&rpwm_val));
  312. rtlpriv->cfg->ops->set_hw_reg(hw,
  313. HW_VAR_H2C_FW_PWRMODE,
  314. (u8 *) (&fw_pwrmode));
  315. b_fw_current_inps = false;
  316. rtlpriv->cfg->ops->set_hw_reg(hw,
  317. HW_VAR_FW_PSMODE_STATUS,
  318. (u8 *) (&b_fw_current_inps));
  319. } else {
  320. if (rtl_get_fwlps_doze(hw)) {
  321. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  322. ("FW LPS enter ps_mode:%x\n",
  323. ppsc->fwctrl_psmode));
  324. rpwm_val = 0x02; /* RF off */
  325. b_fw_current_inps = true;
  326. rtlpriv->cfg->ops->set_hw_reg(hw,
  327. HW_VAR_FW_PSMODE_STATUS,
  328. (u8 *) (&b_fw_current_inps));
  329. rtlpriv->cfg->ops->set_hw_reg(hw,
  330. HW_VAR_H2C_FW_PWRMODE,
  331. (u8 *) (&ppsc->fwctrl_psmode));
  332. rtlpriv->cfg->ops->set_hw_reg(hw,
  333. HW_VAR_SET_RPWM,
  334. (u8 *) (&rpwm_val));
  335. } else {
  336. /* Reset the power save related parameters. */
  337. ppsc->dot11_psmode = EACTIVE;
  338. }
  339. }
  340. }
  341. }
  342. /*Enter the leisure power save mode.*/
  343. void rtl_lps_enter(struct ieee80211_hw *hw)
  344. {
  345. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  346. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  347. struct rtl_priv *rtlpriv = rtl_priv(hw);
  348. unsigned long flag;
  349. if (!(ppsc->b_fwctrl_lps && ppsc->b_leisure_ps))
  350. return;
  351. if (rtlpriv->sec.being_setkey)
  352. return;
  353. if (rtlpriv->link_info.b_busytraffic)
  354. return;
  355. /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
  356. if (mac->cnt_after_linked < 5)
  357. return;
  358. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  359. return;
  360. if (mac->link_state != MAC80211_LINKED)
  361. return;
  362. spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag);
  363. if (ppsc->b_leisure_ps) {
  364. /* Idle for a while if we connect to AP a while ago. */
  365. if (mac->cnt_after_linked >= 2) {
  366. if (ppsc->dot11_psmode == EACTIVE) {
  367. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  368. ("Enter 802.11 power save mode...\n"));
  369. rtl_lps_set_psmode(hw, EAUTOPS);
  370. }
  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->b_fwctrl_lps && ppsc->b_leisure_ps) {
  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_RF_LPS_LEVEL_ASPM) &&
  389. rtlhal->interface == INTF_PCI) {
  390. rtlpriv->intf_ops->disable_aspm(hw);
  391. RT_CLEAR_PS_LEVEL(ppsc, RT_RF_LPS_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. }