ps.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. struct rtl_priv *rtlpriv = rtl_priv(hw);
  58. /*<1> Stop all timer */
  59. rtl_deinit_deferred_work(hw);
  60. /*<2> Disable Interrupt */
  61. rtlpriv->cfg->ops->disable_interrupt(hw);
  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, bool protect_or_not)
  70. {
  71. struct rtl_priv *rtlpriv = rtl_priv(hw);
  72. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  73. enum rf_pwrstate rtstate;
  74. bool actionallowed = false;
  75. u16 rfwait_cnt = 0;
  76. unsigned long flag;
  77. /*protect_or_not = true; */
  78. if (protect_or_not)
  79. goto no_protect;
  80. /*
  81. *Only one thread can change
  82. *the RF state at one time, and others
  83. *should wait to be executed.
  84. */
  85. while (true) {
  86. spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag);
  87. if (ppsc->rfchange_inprogress) {
  88. spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock,
  89. flag);
  90. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  91. ("RF Change in progress!"
  92. "Wait to set..state_toset(%d).\n",
  93. state_toset));
  94. /* Set RF after the previous action is done. */
  95. while (ppsc->rfchange_inprogress) {
  96. rfwait_cnt++;
  97. mdelay(1);
  98. /*
  99. *Wait too long, return false to avoid
  100. *to be stuck here.
  101. */
  102. if (rfwait_cnt > 100)
  103. return false;
  104. }
  105. } else {
  106. ppsc->rfchange_inprogress = true;
  107. spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock,
  108. flag);
  109. break;
  110. }
  111. }
  112. no_protect:
  113. rtstate = ppsc->rfpwr_state;
  114. switch (state_toset) {
  115. case ERFON:
  116. ppsc->rfoff_reason &= (~changesource);
  117. if ((changesource == RF_CHANGE_BY_HW) &&
  118. (ppsc->hwradiooff == true)) {
  119. ppsc->hwradiooff = false;
  120. }
  121. if (!ppsc->rfoff_reason) {
  122. ppsc->rfoff_reason = 0;
  123. actionallowed = true;
  124. }
  125. break;
  126. case ERFOFF:
  127. if ((changesource == RF_CHANGE_BY_HW)
  128. && (ppsc->hwradiooff == false)) {
  129. ppsc->hwradiooff = true;
  130. }
  131. ppsc->rfoff_reason |= changesource;
  132. actionallowed = true;
  133. break;
  134. case ERFSLEEP:
  135. ppsc->rfoff_reason |= changesource;
  136. actionallowed = true;
  137. break;
  138. default:
  139. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  140. ("switch case not process\n"));
  141. break;
  142. }
  143. if (actionallowed)
  144. rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset);
  145. if (!protect_or_not) {
  146. spin_lock_irqsave(&rtlpriv->locks.rf_ps_lock, flag);
  147. ppsc->rfchange_inprogress = false;
  148. spin_unlock_irqrestore(&rtlpriv->locks.rf_ps_lock, flag);
  149. }
  150. return actionallowed;
  151. }
  152. EXPORT_SYMBOL(rtl_ps_set_rf_state);
  153. static void _rtl_ps_inactive_ps(struct ieee80211_hw *hw)
  154. {
  155. struct rtl_priv *rtlpriv = rtl_priv(hw);
  156. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  157. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  158. ppsc->swrf_processing = true;
  159. if (ppsc->inactive_pwrstate == ERFON && rtlhal->interface == INTF_PCI) {
  160. if ((ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) &&
  161. RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_ASPM) &&
  162. rtlhal->interface == INTF_PCI) {
  163. rtlpriv->intf_ops->disable_aspm(hw);
  164. RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_ASPM);
  165. }
  166. }
  167. rtl_ps_set_rf_state(hw, ppsc->inactive_pwrstate,
  168. RF_CHANGE_BY_IPS, false);
  169. if (ppsc->inactive_pwrstate == ERFOFF &&
  170. rtlhal->interface == INTF_PCI) {
  171. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) {
  172. rtlpriv->intf_ops->enable_aspm(hw);
  173. RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_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 (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_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  236. enum rf_pwrstate rtstate;
  237. unsigned long flags;
  238. spin_lock_irqsave(&rtlpriv->locks.ips_lock, flags);
  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_irqrestore(&rtlpriv->locks.ips_lock, flags);
  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->leisure_ps) &&
  302. ppsc->report_linked) {
  303. bool 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. fw_current_inps = false;
  316. rtlpriv->cfg->ops->set_hw_reg(hw,
  317. HW_VAR_FW_PSMODE_STATUS,
  318. (u8 *) (&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. fw_current_inps = true;
  326. rtlpriv->cfg->ops->set_hw_reg(hw,
  327. HW_VAR_FW_PSMODE_STATUS,
  328. (u8 *) (&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. void rtl_swlps_rfon_wq_callback(void *data)
  343. {
  344. }
  345. void rtl_swlps_wq_callback(void *data)
  346. {
  347. }
  348. void rtl_swlps_rf_awake(struct ieee80211_hw *hw)
  349. {
  350. }
  351. void rtl_swlps_beacon(struct ieee80211_hw *hw, void *data, unsigned int len)
  352. {
  353. }
  354. /*Enter the leisure power save mode.*/
  355. void rtl_lps_enter(struct ieee80211_hw *hw)
  356. {
  357. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  358. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  359. struct rtl_priv *rtlpriv = rtl_priv(hw);
  360. unsigned long flag;
  361. if (!(ppsc->fwctrl_lps && ppsc->leisure_ps))
  362. return;
  363. if (rtlpriv->sec.being_setkey)
  364. return;
  365. if (rtlpriv->link_info.busytraffic)
  366. return;
  367. /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
  368. if (mac->cnt_after_linked < 5)
  369. return;
  370. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  371. return;
  372. if (mac->link_state != MAC80211_LINKED)
  373. return;
  374. spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag);
  375. if (ppsc->leisure_ps) {
  376. /* Idle for a while if we connect to AP a while ago. */
  377. if (mac->cnt_after_linked >= 2) {
  378. if (ppsc->dot11_psmode == EACTIVE) {
  379. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  380. ("Enter 802.11 power save mode...\n"));
  381. rtl_lps_set_psmode(hw, EAUTOPS);
  382. }
  383. }
  384. }
  385. spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag);
  386. }
  387. /*Leave the leisure power save mode.*/
  388. void rtl_lps_leave(struct ieee80211_hw *hw)
  389. {
  390. struct rtl_priv *rtlpriv = rtl_priv(hw);
  391. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  392. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  393. unsigned long flag;
  394. spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag);
  395. if (ppsc->fwctrl_lps && ppsc->leisure_ps) {
  396. if (ppsc->dot11_psmode != EACTIVE) {
  397. /*FIX ME */
  398. rtlpriv->cfg->ops->enable_interrupt(hw);
  399. if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
  400. RT_IN_PS_LEVEL(ppsc, RT_RF_LPS_LEVEL_ASPM) &&
  401. rtlhal->interface == INTF_PCI) {
  402. rtlpriv->intf_ops->disable_aspm(hw);
  403. RT_CLEAR_PS_LEVEL(ppsc, RT_RF_LPS_LEVEL_ASPM);
  404. }
  405. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  406. ("Busy Traffic,Leave 802.11 power save..\n"));
  407. rtl_lps_set_psmode(hw, EACTIVE);
  408. }
  409. }
  410. spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag);
  411. }