ps.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2012 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 <linux/export.h>
  30. #include "wifi.h"
  31. #include "base.h"
  32. #include "ps.h"
  33. bool rtl_ps_enable_nic(struct ieee80211_hw *hw)
  34. {
  35. struct rtl_priv *rtlpriv = rtl_priv(hw);
  36. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  37. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  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. if (rtlpriv->cfg->ops->hw_init(hw))
  46. return 1;
  47. RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
  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 true;
  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. tasklet_kill(&rtlpriv->works.irq_tasklet);
  63. /*<3> Disable Adapter */
  64. rtlpriv->cfg->ops->hw_disable(hw);
  65. return true;
  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)
  71. {
  72. struct rtl_priv *rtlpriv = rtl_priv(hw);
  73. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  74. bool actionallowed = false;
  75. switch (state_toset) {
  76. case ERFON:
  77. ppsc->rfoff_reason &= (~changesource);
  78. if ((changesource == RF_CHANGE_BY_HW) &&
  79. (ppsc->hwradiooff)) {
  80. ppsc->hwradiooff = false;
  81. }
  82. if (!ppsc->rfoff_reason) {
  83. ppsc->rfoff_reason = 0;
  84. actionallowed = true;
  85. }
  86. break;
  87. case ERFOFF:
  88. if ((changesource == RF_CHANGE_BY_HW) && !ppsc->hwradiooff) {
  89. ppsc->hwradiooff = true;
  90. }
  91. ppsc->rfoff_reason |= changesource;
  92. actionallowed = true;
  93. break;
  94. case ERFSLEEP:
  95. ppsc->rfoff_reason |= changesource;
  96. actionallowed = true;
  97. break;
  98. default:
  99. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  100. "switch case not processed\n");
  101. break;
  102. }
  103. if (actionallowed)
  104. rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset);
  105. return actionallowed;
  106. }
  107. EXPORT_SYMBOL(rtl_ps_set_rf_state);
  108. static void _rtl_ps_inactive_ps(struct ieee80211_hw *hw)
  109. {
  110. struct rtl_priv *rtlpriv = rtl_priv(hw);
  111. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  112. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  113. ppsc->swrf_processing = true;
  114. if (ppsc->inactive_pwrstate == ERFON &&
  115. rtlhal->interface == INTF_PCI) {
  116. if ((ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) &&
  117. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
  118. rtlhal->interface == INTF_PCI) {
  119. rtlpriv->intf_ops->disable_aspm(hw);
  120. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  121. }
  122. }
  123. rtl_ps_set_rf_state(hw, ppsc->inactive_pwrstate, RF_CHANGE_BY_IPS);
  124. if (ppsc->inactive_pwrstate == ERFOFF &&
  125. rtlhal->interface == INTF_PCI) {
  126. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM &&
  127. !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  128. rtlpriv->intf_ops->enable_aspm(hw);
  129. RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  130. }
  131. }
  132. ppsc->swrf_processing = false;
  133. }
  134. void rtl_ips_nic_off_wq_callback(void *data)
  135. {
  136. struct rtl_works *rtlworks =
  137. container_of_dwork_rtl(data, struct rtl_works, ips_nic_off_wq);
  138. struct ieee80211_hw *hw = rtlworks->hw;
  139. struct rtl_priv *rtlpriv = rtl_priv(hw);
  140. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  141. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  142. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  143. enum rf_pwrstate rtstate;
  144. if (mac->opmode != NL80211_IFTYPE_STATION) {
  145. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  146. "not station return\n");
  147. return;
  148. }
  149. if (mac->p2p_in_use)
  150. return;
  151. if (mac->link_state > MAC80211_NOLINK)
  152. return;
  153. if (is_hal_stop(rtlhal))
  154. return;
  155. if (rtlpriv->sec.being_setkey)
  156. return;
  157. if (rtlpriv->cfg->ops->bt_coex_off_before_lps)
  158. rtlpriv->cfg->ops->bt_coex_off_before_lps(hw);
  159. if (ppsc->inactiveps) {
  160. rtstate = ppsc->rfpwr_state;
  161. /*
  162. *Do not enter IPS in the following conditions:
  163. *(1) RF is already OFF or Sleep
  164. *(2) swrf_processing (indicates the IPS is still under going)
  165. *(3) Connectted (only disconnected can trigger IPS)
  166. *(4) IBSS (send Beacon)
  167. *(5) AP mode (send Beacon)
  168. *(6) monitor mode (rcv packet)
  169. */
  170. if (rtstate == ERFON &&
  171. !ppsc->swrf_processing &&
  172. (mac->link_state == MAC80211_NOLINK) &&
  173. !mac->act_scanning) {
  174. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  175. "IPSEnter(): Turn off RF\n");
  176. ppsc->inactive_pwrstate = ERFOFF;
  177. ppsc->in_powersavemode = true;
  178. /*rtl_pci_reset_trx_ring(hw); */
  179. _rtl_ps_inactive_ps(hw);
  180. }
  181. }
  182. }
  183. void rtl_ips_nic_off(struct ieee80211_hw *hw)
  184. {
  185. struct rtl_priv *rtlpriv = rtl_priv(hw);
  186. /*
  187. *because when link with ap, mac80211 will ask us
  188. *to disable nic quickly after scan before linking,
  189. *this will cause link failed, so we delay 100ms here
  190. */
  191. queue_delayed_work(rtlpriv->works.rtl_wq,
  192. &rtlpriv->works.ips_nic_off_wq, MSECS(100));
  193. }
  194. /* NOTICE: any opmode should exc nic_on, or disable without
  195. * nic_on may something wrong, like adhoc TP
  196. */
  197. void rtl_ips_nic_on(struct ieee80211_hw *hw)
  198. {
  199. struct rtl_priv *rtlpriv = rtl_priv(hw);
  200. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  201. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  202. enum rf_pwrstate rtstate;
  203. unsigned long flags;
  204. if (mac->opmode != NL80211_IFTYPE_STATION)
  205. return;
  206. spin_lock_irqsave(&rtlpriv->locks.ips_lock, flags);
  207. if (ppsc->inactiveps) {
  208. rtstate = ppsc->rfpwr_state;
  209. if (rtstate != ERFON &&
  210. !ppsc->swrf_processing &&
  211. ppsc->rfoff_reason <= RF_CHANGE_BY_IPS) {
  212. ppsc->inactive_pwrstate = ERFON;
  213. ppsc->in_powersavemode = false;
  214. _rtl_ps_inactive_ps(hw);
  215. }
  216. }
  217. spin_unlock_irqrestore(&rtlpriv->locks.ips_lock, flags);
  218. }
  219. EXPORT_SYMBOL_GPL(rtl_ips_nic_on);
  220. /*for FW LPS*/
  221. /*
  222. *Determine if we can set Fw into PS mode
  223. *in current condition.Return TRUE if it
  224. *can enter PS mode.
  225. */
  226. static bool rtl_get_fwlps_doze(struct ieee80211_hw *hw)
  227. {
  228. struct rtl_priv *rtlpriv = rtl_priv(hw);
  229. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  230. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  231. u32 ps_timediff;
  232. ps_timediff = jiffies_to_msecs(jiffies -
  233. ppsc->last_delaylps_stamp_jiffies);
  234. if (ps_timediff < 2000) {
  235. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  236. "Delay enter Fw LPS for DHCP, ARP, or EAPOL exchanging state\n");
  237. return false;
  238. }
  239. if (mac->link_state != MAC80211_LINKED)
  240. return false;
  241. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  242. return false;
  243. return true;
  244. }
  245. /* Change current and default preamble mode.*/
  246. static void rtl_lps_set_psmode(struct ieee80211_hw *hw, u8 rt_psmode)
  247. {
  248. struct rtl_priv *rtlpriv = rtl_priv(hw);
  249. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  250. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  251. bool enter_fwlps;
  252. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  253. return;
  254. if (mac->link_state != MAC80211_LINKED)
  255. return;
  256. if (ppsc->dot11_psmode == rt_psmode)
  257. return;
  258. /* Update power save mode configured. */
  259. ppsc->dot11_psmode = rt_psmode;
  260. /*
  261. *<FW control LPS>
  262. *1. Enter PS mode
  263. * Set RPWM to Fw to turn RF off and send H2C fw_pwrmode
  264. * cmd to set Fw into PS mode.
  265. *2. Leave PS mode
  266. * Send H2C fw_pwrmode cmd to Fw to set Fw into Active
  267. * mode and set RPWM to turn RF on.
  268. */
  269. if ((ppsc->fwctrl_lps) && ppsc->report_linked) {
  270. if (ppsc->dot11_psmode == EACTIVE) {
  271. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  272. "FW LPS leave ps_mode:%x\n",
  273. FW_PS_ACTIVE_MODE);
  274. enter_fwlps = false;
  275. ppsc->pwr_mode = FW_PS_ACTIVE_MODE;
  276. ppsc->smart_ps = 0;
  277. rtlpriv->cfg->ops->set_hw_reg(hw,
  278. HW_VAR_FW_LPS_ACTION,
  279. (u8 *)(&enter_fwlps));
  280. if (ppsc->p2p_ps_info.opp_ps)
  281. rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
  282. } else {
  283. if (rtl_get_fwlps_doze(hw)) {
  284. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  285. "FW LPS enter ps_mode:%x\n",
  286. ppsc->fwctrl_psmode);
  287. enter_fwlps = true;
  288. ppsc->pwr_mode = ppsc->fwctrl_psmode;
  289. ppsc->smart_ps = 2;
  290. rtlpriv->cfg->ops->set_hw_reg(hw,
  291. HW_VAR_FW_LPS_ACTION,
  292. (u8 *)(&enter_fwlps));
  293. } else {
  294. /* Reset the power save related parameters. */
  295. ppsc->dot11_psmode = EACTIVE;
  296. }
  297. }
  298. }
  299. }
  300. /*Enter the leisure power save mode.*/
  301. void rtl_lps_enter(struct ieee80211_hw *hw)
  302. {
  303. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  304. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  305. struct rtl_priv *rtlpriv = rtl_priv(hw);
  306. if (!ppsc->fwctrl_lps)
  307. return;
  308. if (rtlpriv->sec.being_setkey)
  309. return;
  310. if (rtlpriv->link_info.busytraffic)
  311. return;
  312. /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
  313. if (mac->cnt_after_linked < 5)
  314. return;
  315. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  316. return;
  317. if (mac->link_state != MAC80211_LINKED)
  318. return;
  319. mutex_lock(&rtlpriv->locks.ps_mutex);
  320. /* Idle for a while if we connect to AP a while ago. */
  321. if (mac->cnt_after_linked >= 2) {
  322. if (ppsc->dot11_psmode == EACTIVE) {
  323. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  324. "Enter 802.11 power save mode...\n");
  325. rtl_lps_set_psmode(hw, EAUTOPS);
  326. }
  327. }
  328. mutex_unlock(&rtlpriv->locks.ps_mutex);
  329. }
  330. /*Leave the leisure power save mode.*/
  331. void rtl_lps_leave(struct ieee80211_hw *hw)
  332. {
  333. struct rtl_priv *rtlpriv = rtl_priv(hw);
  334. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  335. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  336. mutex_lock(&rtlpriv->locks.ps_mutex);
  337. if (ppsc->fwctrl_lps) {
  338. if (ppsc->dot11_psmode != EACTIVE) {
  339. /*FIX ME */
  340. rtlpriv->cfg->ops->enable_interrupt(hw);
  341. if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
  342. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
  343. rtlhal->interface == INTF_PCI) {
  344. rtlpriv->intf_ops->disable_aspm(hw);
  345. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  346. }
  347. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  348. "Busy Traffic,Leave 802.11 power save..\n");
  349. rtl_lps_set_psmode(hw, EACTIVE);
  350. }
  351. }
  352. mutex_unlock(&rtlpriv->locks.ps_mutex);
  353. }
  354. /* For sw LPS*/
  355. void rtl_swlps_beacon(struct ieee80211_hw *hw, void *data, unsigned int len)
  356. {
  357. struct rtl_priv *rtlpriv = rtl_priv(hw);
  358. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  359. struct ieee80211_hdr *hdr = data;
  360. struct ieee80211_tim_ie *tim_ie;
  361. u8 *tim;
  362. u8 tim_len;
  363. bool u_buffed;
  364. bool m_buffed;
  365. if (mac->opmode != NL80211_IFTYPE_STATION)
  366. return;
  367. if (!rtlpriv->psc.swctrl_lps)
  368. return;
  369. if (rtlpriv->mac80211.link_state != MAC80211_LINKED)
  370. return;
  371. if (!rtlpriv->psc.sw_ps_enabled)
  372. return;
  373. if (rtlpriv->psc.fwctrl_lps)
  374. return;
  375. if (likely(!(hw->conf.flags & IEEE80211_CONF_PS)))
  376. return;
  377. /* check if this really is a beacon */
  378. if (!ieee80211_is_beacon(hdr->frame_control))
  379. return;
  380. /* min. beacon length + FCS_LEN */
  381. if (len <= 40 + FCS_LEN)
  382. return;
  383. /* and only beacons from the associated BSSID, please */
  384. if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
  385. return;
  386. rtlpriv->psc.last_beacon = jiffies;
  387. tim = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_TIM);
  388. if (!tim)
  389. return;
  390. if (tim[1] < sizeof(*tim_ie))
  391. return;
  392. tim_len = tim[1];
  393. tim_ie = (struct ieee80211_tim_ie *) &tim[2];
  394. if (!WARN_ON_ONCE(!hw->conf.ps_dtim_period))
  395. rtlpriv->psc.dtim_counter = tim_ie->dtim_count;
  396. /* Check whenever the PHY can be turned off again. */
  397. /* 1. What about buffered unicast traffic for our AID? */
  398. u_buffed = ieee80211_check_tim(tim_ie, tim_len,
  399. rtlpriv->mac80211.assoc_id);
  400. /* 2. Maybe the AP wants to send multicast/broadcast data? */
  401. m_buffed = tim_ie->bitmap_ctrl & 0x01;
  402. rtlpriv->psc.multi_buffered = m_buffed;
  403. /* unicast will process by mac80211 through
  404. * set ~IEEE80211_CONF_PS, So we just check
  405. * multicast frames here */
  406. if (!m_buffed) {
  407. /* back to low-power land. and delay is
  408. * prevent null power save frame tx fail */
  409. queue_delayed_work(rtlpriv->works.rtl_wq,
  410. &rtlpriv->works.ps_work, MSECS(5));
  411. } else {
  412. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  413. "u_bufferd: %x, m_buffered: %x\n", u_buffed, m_buffed);
  414. }
  415. }
  416. EXPORT_SYMBOL_GPL(rtl_swlps_beacon);
  417. void rtl_swlps_rf_awake(struct ieee80211_hw *hw)
  418. {
  419. struct rtl_priv *rtlpriv = rtl_priv(hw);
  420. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  421. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  422. if (!rtlpriv->psc.swctrl_lps)
  423. return;
  424. if (mac->link_state != MAC80211_LINKED)
  425. return;
  426. if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
  427. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  428. rtlpriv->intf_ops->disable_aspm(hw);
  429. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  430. }
  431. mutex_lock(&rtlpriv->locks.ps_mutex);
  432. rtl_ps_set_rf_state(hw, ERFON, RF_CHANGE_BY_PS);
  433. mutex_unlock(&rtlpriv->locks.ps_mutex);
  434. }
  435. void rtl_swlps_rfon_wq_callback(void *data)
  436. {
  437. struct rtl_works *rtlworks =
  438. container_of_dwork_rtl(data, struct rtl_works, ps_rfon_wq);
  439. struct ieee80211_hw *hw = rtlworks->hw;
  440. rtl_swlps_rf_awake(hw);
  441. }
  442. void rtl_swlps_rf_sleep(struct ieee80211_hw *hw)
  443. {
  444. struct rtl_priv *rtlpriv = rtl_priv(hw);
  445. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  446. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  447. u8 sleep_intv;
  448. if (!rtlpriv->psc.sw_ps_enabled)
  449. return;
  450. if ((rtlpriv->sec.being_setkey) ||
  451. (mac->opmode == NL80211_IFTYPE_ADHOC))
  452. return;
  453. /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
  454. if ((mac->link_state != MAC80211_LINKED) || (mac->cnt_after_linked < 5))
  455. return;
  456. if (rtlpriv->link_info.busytraffic)
  457. return;
  458. mutex_lock(&rtlpriv->locks.ps_mutex);
  459. rtl_ps_set_rf_state(hw, ERFSLEEP, RF_CHANGE_BY_PS);
  460. mutex_unlock(&rtlpriv->locks.ps_mutex);
  461. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM &&
  462. !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  463. rtlpriv->intf_ops->enable_aspm(hw);
  464. RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  465. }
  466. /* here is power save alg, when this beacon is DTIM
  467. * we will set sleep time to dtim_period * n;
  468. * when this beacon is not DTIM, we will set sleep
  469. * time to sleep_intv = rtlpriv->psc.dtim_counter or
  470. * MAX_SW_LPS_SLEEP_INTV(default set to 5) */
  471. if (rtlpriv->psc.dtim_counter == 0) {
  472. if (hw->conf.ps_dtim_period == 1)
  473. sleep_intv = hw->conf.ps_dtim_period * 2;
  474. else
  475. sleep_intv = hw->conf.ps_dtim_period;
  476. } else {
  477. sleep_intv = rtlpriv->psc.dtim_counter;
  478. }
  479. if (sleep_intv > MAX_SW_LPS_SLEEP_INTV)
  480. sleep_intv = MAX_SW_LPS_SLEEP_INTV;
  481. /* this print should always be dtim_conter = 0 &
  482. * sleep = dtim_period, that meaons, we should
  483. * awake before every dtim */
  484. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  485. "dtim_counter:%x will sleep :%d beacon_intv\n",
  486. rtlpriv->psc.dtim_counter, sleep_intv);
  487. /* we tested that 40ms is enough for sw & hw sw delay */
  488. queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.ps_rfon_wq,
  489. MSECS(sleep_intv * mac->vif->bss_conf.beacon_int - 40));
  490. }
  491. void rtl_lps_change_work_callback(struct work_struct *work)
  492. {
  493. struct rtl_works *rtlworks =
  494. container_of(work, struct rtl_works, lps_change_work);
  495. struct ieee80211_hw *hw = rtlworks->hw;
  496. struct rtl_priv *rtlpriv = rtl_priv(hw);
  497. if (rtlpriv->enter_ps)
  498. rtl_lps_enter(hw);
  499. else
  500. rtl_lps_leave(hw);
  501. }
  502. EXPORT_SYMBOL_GPL(rtl_lps_change_work_callback);
  503. void rtl_swlps_wq_callback(void *data)
  504. {
  505. struct rtl_works *rtlworks = container_of_dwork_rtl(data,
  506. struct rtl_works,
  507. ps_work);
  508. struct ieee80211_hw *hw = rtlworks->hw;
  509. struct rtl_priv *rtlpriv = rtl_priv(hw);
  510. bool ps = false;
  511. ps = (hw->conf.flags & IEEE80211_CONF_PS);
  512. /* we can sleep after ps null send ok */
  513. if (rtlpriv->psc.state_inap) {
  514. rtl_swlps_rf_sleep(hw);
  515. if (rtlpriv->psc.state && !ps) {
  516. rtlpriv->psc.sleep_ms = jiffies_to_msecs(jiffies -
  517. rtlpriv->psc.last_action);
  518. }
  519. if (ps)
  520. rtlpriv->psc.last_slept = jiffies;
  521. rtlpriv->psc.last_action = jiffies;
  522. rtlpriv->psc.state = ps;
  523. }
  524. }
  525. static void rtl_p2p_noa_ie(struct ieee80211_hw *hw, void *data,
  526. unsigned int len)
  527. {
  528. struct rtl_priv *rtlpriv = rtl_priv(hw);
  529. struct ieee80211_mgmt *mgmt = (void *)data;
  530. struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
  531. u8 *pos, *end, *ie;
  532. u16 noa_len;
  533. static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
  534. u8 noa_num, index, i, noa_index = 0;
  535. bool find_p2p_ie = false , find_p2p_ps_ie = false;
  536. pos = (u8 *)mgmt->u.beacon.variable;
  537. end = data + len;
  538. ie = NULL;
  539. while (pos + 1 < end) {
  540. if (pos + 2 + pos[1] > end)
  541. return;
  542. if (pos[0] == 221 && pos[1] > 4) {
  543. if (memcmp(&pos[2], p2p_oui_ie_type, 4) == 0) {
  544. ie = pos + 2+4;
  545. break;
  546. }
  547. }
  548. pos += 2 + pos[1];
  549. }
  550. if (ie == NULL)
  551. return;
  552. find_p2p_ie = true;
  553. /*to find noa ie*/
  554. while (ie + 1 < end) {
  555. noa_len = READEF2BYTE((__le16 *)&ie[1]);
  556. if (ie + 3 + ie[1] > end)
  557. return;
  558. if (ie[0] == 12) {
  559. find_p2p_ps_ie = true;
  560. if ((noa_len - 2) % 13 != 0) {
  561. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  562. "P2P notice of absence: invalid length.%d\n",
  563. noa_len);
  564. return;
  565. } else {
  566. noa_num = (noa_len - 2) / 13;
  567. }
  568. noa_index = ie[3];
  569. if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
  570. P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
  571. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  572. "update NOA ie.\n");
  573. p2pinfo->noa_index = noa_index;
  574. p2pinfo->opp_ps = (ie[4] >> 7);
  575. p2pinfo->ctwindow = ie[4] & 0x7F;
  576. p2pinfo->noa_num = noa_num;
  577. index = 5;
  578. for (i = 0; i < noa_num; i++) {
  579. p2pinfo->noa_count_type[i] =
  580. READEF1BYTE(ie+index);
  581. index += 1;
  582. p2pinfo->noa_duration[i] =
  583. READEF4BYTE((__le32 *)ie+index);
  584. index += 4;
  585. p2pinfo->noa_interval[i] =
  586. READEF4BYTE((__le32 *)ie+index);
  587. index += 4;
  588. p2pinfo->noa_start_time[i] =
  589. READEF4BYTE((__le32 *)ie+index);
  590. index += 4;
  591. }
  592. if (p2pinfo->opp_ps == 1) {
  593. p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
  594. /* Driver should wait LPS entering
  595. * CTWindow
  596. */
  597. if (rtlpriv->psc.fw_current_inpsmode)
  598. rtl_p2p_ps_cmd(hw,
  599. P2P_PS_ENABLE);
  600. } else if (p2pinfo->noa_num > 0) {
  601. p2pinfo->p2p_ps_mode = P2P_PS_NOA;
  602. rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
  603. } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  604. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  605. }
  606. }
  607. break;
  608. }
  609. ie += 3 + noa_len;
  610. }
  611. if (find_p2p_ie == true) {
  612. if ((p2pinfo->p2p_ps_mode > P2P_PS_NONE) &&
  613. (find_p2p_ps_ie == false))
  614. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  615. }
  616. }
  617. static void rtl_p2p_action_ie(struct ieee80211_hw *hw, void *data,
  618. unsigned int len)
  619. {
  620. struct rtl_priv *rtlpriv = rtl_priv(hw);
  621. struct ieee80211_mgmt *mgmt = (void *)data;
  622. struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
  623. u8 noa_num, index, i, noa_index = 0;
  624. u8 *pos, *end, *ie;
  625. u16 noa_len;
  626. static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
  627. pos = (u8 *)&mgmt->u.action.category;
  628. end = data + len;
  629. ie = NULL;
  630. if (pos[0] == 0x7f) {
  631. if (memcmp(&pos[1], p2p_oui_ie_type, 4) == 0)
  632. ie = pos + 3+4;
  633. }
  634. if (ie == NULL)
  635. return;
  636. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "action frame find P2P IE.\n");
  637. /*to find noa ie*/
  638. while (ie + 1 < end) {
  639. noa_len = READEF2BYTE((__le16 *)&ie[1]);
  640. if (ie + 3 + ie[1] > end)
  641. return;
  642. if (ie[0] == 12) {
  643. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "find NOA IE.\n");
  644. RT_PRINT_DATA(rtlpriv, COMP_FW, DBG_LOUD, "noa ie ",
  645. ie, noa_len);
  646. if ((noa_len - 2) % 13 != 0) {
  647. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  648. "P2P notice of absence: invalid length.%d\n",
  649. noa_len);
  650. return;
  651. } else {
  652. noa_num = (noa_len - 2) / 13;
  653. }
  654. noa_index = ie[3];
  655. if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
  656. P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
  657. p2pinfo->noa_index = noa_index;
  658. p2pinfo->opp_ps = (ie[4] >> 7);
  659. p2pinfo->ctwindow = ie[4] & 0x7F;
  660. p2pinfo->noa_num = noa_num;
  661. index = 5;
  662. for (i = 0; i < noa_num; i++) {
  663. p2pinfo->noa_count_type[i] =
  664. READEF1BYTE(ie+index);
  665. index += 1;
  666. p2pinfo->noa_duration[i] =
  667. READEF4BYTE((__le32 *)ie+index);
  668. index += 4;
  669. p2pinfo->noa_interval[i] =
  670. READEF4BYTE((__le32 *)ie+index);
  671. index += 4;
  672. p2pinfo->noa_start_time[i] =
  673. READEF4BYTE((__le32 *)ie+index);
  674. index += 4;
  675. }
  676. if (p2pinfo->opp_ps == 1) {
  677. p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
  678. /* Driver should wait LPS entering
  679. * CTWindow
  680. */
  681. if (rtlpriv->psc.fw_current_inpsmode)
  682. rtl_p2p_ps_cmd(hw,
  683. P2P_PS_ENABLE);
  684. } else if (p2pinfo->noa_num > 0) {
  685. p2pinfo->p2p_ps_mode = P2P_PS_NOA;
  686. rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
  687. } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  688. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  689. }
  690. }
  691. break;
  692. }
  693. ie += 3 + noa_len;
  694. }
  695. }
  696. void rtl_p2p_ps_cmd(struct ieee80211_hw *hw, u8 p2p_ps_state)
  697. {
  698. struct rtl_priv *rtlpriv = rtl_priv(hw);
  699. struct rtl_ps_ctl *rtlps = rtl_psc(rtl_priv(hw));
  700. struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
  701. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, " p2p state %x\n", p2p_ps_state);
  702. switch (p2p_ps_state) {
  703. case P2P_PS_DISABLE:
  704. p2pinfo->p2p_ps_state = p2p_ps_state;
  705. rtlpriv->cfg->ops->set_hw_reg(hw,
  706. HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  707. (u8 *)(&p2p_ps_state));
  708. p2pinfo->noa_index = 0;
  709. p2pinfo->ctwindow = 0;
  710. p2pinfo->opp_ps = 0;
  711. p2pinfo->noa_num = 0;
  712. p2pinfo->p2p_ps_mode = P2P_PS_NONE;
  713. if (rtlps->fw_current_inpsmode == true) {
  714. if (rtlps->smart_ps == 0) {
  715. rtlps->smart_ps = 2;
  716. rtlpriv->cfg->ops->set_hw_reg(hw,
  717. HW_VAR_H2C_FW_PWRMODE,
  718. (u8 *)(&rtlps->pwr_mode));
  719. }
  720. }
  721. break;
  722. case P2P_PS_ENABLE:
  723. if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  724. p2pinfo->p2p_ps_state = p2p_ps_state;
  725. if (p2pinfo->ctwindow > 0) {
  726. if (rtlps->smart_ps != 0) {
  727. rtlps->smart_ps = 0;
  728. rtlpriv->cfg->ops->set_hw_reg(hw,
  729. HW_VAR_H2C_FW_PWRMODE,
  730. (u8 *)(&rtlps->pwr_mode));
  731. }
  732. }
  733. rtlpriv->cfg->ops->set_hw_reg(hw,
  734. HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  735. (u8 *)(&p2p_ps_state));
  736. }
  737. break;
  738. case P2P_PS_SCAN:
  739. case P2P_PS_SCAN_DONE:
  740. case P2P_PS_ALLSTASLEEP:
  741. if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  742. p2pinfo->p2p_ps_state = p2p_ps_state;
  743. rtlpriv->cfg->ops->set_hw_reg(hw,
  744. HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  745. (u8 *)(&p2p_ps_state));
  746. }
  747. break;
  748. default:
  749. break;
  750. }
  751. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  752. "ctwindow %x oppps %x\n", p2pinfo->ctwindow, p2pinfo->opp_ps);
  753. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  754. "count %x duration %x index %x interval %x start time %x noa num %x\n",
  755. p2pinfo->noa_count_type[0], p2pinfo->noa_duration[0],
  756. p2pinfo->noa_index, p2pinfo->noa_interval[0],
  757. p2pinfo->noa_start_time[0], p2pinfo->noa_num);
  758. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "end\n");
  759. }
  760. void rtl_p2p_info(struct ieee80211_hw *hw, void *data, unsigned int len)
  761. {
  762. struct rtl_priv *rtlpriv = rtl_priv(hw);
  763. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  764. struct ieee80211_hdr *hdr = (void *)data;
  765. if (!mac->p2p)
  766. return;
  767. if (mac->link_state != MAC80211_LINKED)
  768. return;
  769. /* min. beacon length + FCS_LEN */
  770. if (len <= 40 + FCS_LEN)
  771. return;
  772. /* and only beacons from the associated BSSID, please */
  773. if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
  774. return;
  775. /* check if this really is a beacon */
  776. if (!(ieee80211_is_beacon(hdr->frame_control) ||
  777. ieee80211_is_probe_resp(hdr->frame_control) ||
  778. ieee80211_is_action(hdr->frame_control)))
  779. return;
  780. if (ieee80211_is_action(hdr->frame_control))
  781. rtl_p2p_action_ie(hw, data, len - FCS_LEN);
  782. else
  783. rtl_p2p_noa_ie(hw, data, len - FCS_LEN);
  784. }
  785. EXPORT_SYMBOL_GPL(rtl_p2p_info);