ps.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. * The full GNU General Public License is included in this distribution in the
  15. * file called LICENSE.
  16. *
  17. * Contact Information:
  18. * wlanfae <wlanfae@realtek.com>
  19. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  20. * Hsinchu 300, Taiwan.
  21. *
  22. * Larry Finger <Larry.Finger@lwfinger.net>
  23. *
  24. *****************************************************************************/
  25. #include "wifi.h"
  26. #include "base.h"
  27. #include "ps.h"
  28. #include <linux/export.h>
  29. #include "btcoexist/rtl_btc.h"
  30. bool rtl_ps_enable_nic(struct ieee80211_hw *hw)
  31. {
  32. struct rtl_priv *rtlpriv = rtl_priv(hw);
  33. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  34. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  35. /*<1> reset trx ring */
  36. if (rtlhal->interface == INTF_PCI)
  37. rtlpriv->intf_ops->reset_trx_ring(hw);
  38. if (is_hal_stop(rtlhal))
  39. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  40. "Driver is already down!\n");
  41. /*<2> Enable Adapter */
  42. if (rtlpriv->cfg->ops->hw_init(hw))
  43. return false;
  44. RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
  45. /*<3> Enable Interrupt */
  46. rtlpriv->cfg->ops->enable_interrupt(hw);
  47. /*<enable timer> */
  48. rtl_watch_dog_timer_callback((unsigned long)hw);
  49. return true;
  50. }
  51. EXPORT_SYMBOL(rtl_ps_enable_nic);
  52. bool rtl_ps_disable_nic(struct ieee80211_hw *hw)
  53. {
  54. struct rtl_priv *rtlpriv = rtl_priv(hw);
  55. /*<1> Stop all timer */
  56. rtl_deinit_deferred_work(hw);
  57. /*<2> Disable Interrupt */
  58. rtlpriv->cfg->ops->disable_interrupt(hw);
  59. tasklet_kill(&rtlpriv->works.irq_tasklet);
  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. if (protect_or_not)
  75. goto no_protect;
  76. /*Only one thread can change
  77. *the RF state at one time, and others
  78. *should wait to be executed.
  79. */
  80. while (true) {
  81. spin_lock(&rtlpriv->locks.rf_ps_lock);
  82. if (ppsc->rfchange_inprogress) {
  83. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  84. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  85. "RF Change in progress! Wait to set..state_toset(%d).\n",
  86. state_toset);
  87. /* Set RF after the previous action is done. */
  88. while (ppsc->rfchange_inprogress) {
  89. rfwait_cnt++;
  90. mdelay(1);
  91. /*Wait too long, return false to avoid
  92. *to be stuck here.
  93. */
  94. if (rfwait_cnt > 100)
  95. return false;
  96. }
  97. } else {
  98. ppsc->rfchange_inprogress = true;
  99. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  100. break;
  101. }
  102. }
  103. no_protect:
  104. rtstate = ppsc->rfpwr_state;
  105. switch (state_toset) {
  106. case ERFON:
  107. ppsc->rfoff_reason &= (~changesource);
  108. if ((changesource == RF_CHANGE_BY_HW) &&
  109. (ppsc->hwradiooff)) {
  110. ppsc->hwradiooff = false;
  111. }
  112. if (!ppsc->rfoff_reason) {
  113. ppsc->rfoff_reason = 0;
  114. actionallowed = true;
  115. }
  116. break;
  117. case ERFOFF:
  118. if ((changesource == RF_CHANGE_BY_HW) && !ppsc->hwradiooff) {
  119. ppsc->hwradiooff = true;
  120. }
  121. ppsc->rfoff_reason |= changesource;
  122. actionallowed = true;
  123. break;
  124. case ERFSLEEP:
  125. ppsc->rfoff_reason |= changesource;
  126. actionallowed = true;
  127. break;
  128. default:
  129. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  130. "switch case not processed\n");
  131. break;
  132. }
  133. if (actionallowed)
  134. rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset);
  135. if (!protect_or_not) {
  136. spin_lock(&rtlpriv->locks.rf_ps_lock);
  137. ppsc->rfchange_inprogress = false;
  138. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  139. }
  140. return actionallowed;
  141. }
  142. EXPORT_SYMBOL(rtl_ps_set_rf_state);
  143. static void _rtl_ps_inactive_ps(struct ieee80211_hw *hw)
  144. {
  145. struct rtl_priv *rtlpriv = rtl_priv(hw);
  146. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  147. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  148. ppsc->swrf_processing = true;
  149. if (ppsc->inactive_pwrstate == ERFON &&
  150. rtlhal->interface == INTF_PCI) {
  151. if ((ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) &&
  152. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
  153. rtlhal->interface == INTF_PCI) {
  154. rtlpriv->intf_ops->disable_aspm(hw);
  155. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  156. }
  157. }
  158. rtl_ps_set_rf_state(hw, ppsc->inactive_pwrstate,
  159. RF_CHANGE_BY_IPS, false);
  160. if (ppsc->inactive_pwrstate == ERFOFF &&
  161. rtlhal->interface == INTF_PCI) {
  162. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM &&
  163. !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  164. rtlpriv->intf_ops->enable_aspm(hw);
  165. RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  166. }
  167. }
  168. ppsc->swrf_processing = false;
  169. }
  170. void rtl_ips_nic_off_wq_callback(void *data)
  171. {
  172. struct rtl_works *rtlworks =
  173. container_of_dwork_rtl(data, struct rtl_works, ips_nic_off_wq);
  174. struct ieee80211_hw *hw = rtlworks->hw;
  175. struct rtl_priv *rtlpriv = rtl_priv(hw);
  176. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  177. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  178. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  179. enum rf_pwrstate rtstate;
  180. if (mac->opmode != NL80211_IFTYPE_STATION) {
  181. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  182. "not station return\n");
  183. return;
  184. }
  185. if (mac->p2p_in_use)
  186. return;
  187. if (mac->link_state > MAC80211_NOLINK)
  188. return;
  189. if (is_hal_stop(rtlhal))
  190. return;
  191. if (rtlpriv->sec.being_setkey)
  192. return;
  193. if (rtlpriv->cfg->ops->bt_coex_off_before_lps)
  194. rtlpriv->cfg->ops->bt_coex_off_before_lps(hw);
  195. if (ppsc->inactiveps) {
  196. rtstate = ppsc->rfpwr_state;
  197. /*
  198. *Do not enter IPS in the following conditions:
  199. *(1) RF is already OFF or Sleep
  200. *(2) swrf_processing (indicates the IPS is still under going)
  201. *(3) Connectted (only disconnected can trigger IPS)
  202. *(4) IBSS (send Beacon)
  203. *(5) AP mode (send Beacon)
  204. *(6) monitor mode (rcv packet)
  205. */
  206. if (rtstate == ERFON &&
  207. !ppsc->swrf_processing &&
  208. (mac->link_state == MAC80211_NOLINK) &&
  209. !mac->act_scanning) {
  210. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  211. "IPSEnter(): Turn off RF\n");
  212. ppsc->inactive_pwrstate = ERFOFF;
  213. ppsc->in_powersavemode = true;
  214. /* call before RF off */
  215. if (rtlpriv->cfg->ops->get_btc_status())
  216. rtlpriv->btcoexist.btc_ops->btc_ips_notify(rtlpriv,
  217. ppsc->inactive_pwrstate);
  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. /* 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. /* NOTICE: any opmode should exc nic_on, or disable without
  234. * nic_on may something wrong, like adhoc TP
  235. */
  236. void rtl_ips_nic_on(struct ieee80211_hw *hw)
  237. {
  238. struct rtl_priv *rtlpriv = rtl_priv(hw);
  239. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  240. enum rf_pwrstate rtstate;
  241. cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
  242. spin_lock(&rtlpriv->locks.ips_lock);
  243. if (ppsc->inactiveps) {
  244. rtstate = ppsc->rfpwr_state;
  245. if (rtstate != ERFON &&
  246. !ppsc->swrf_processing &&
  247. ppsc->rfoff_reason <= RF_CHANGE_BY_IPS) {
  248. ppsc->inactive_pwrstate = ERFON;
  249. ppsc->in_powersavemode = false;
  250. _rtl_ps_inactive_ps(hw);
  251. /* call after RF on */
  252. if (rtlpriv->cfg->ops->get_btc_status())
  253. rtlpriv->btcoexist.btc_ops->btc_ips_notify(rtlpriv,
  254. ppsc->inactive_pwrstate);
  255. }
  256. }
  257. spin_unlock(&rtlpriv->locks.ips_lock);
  258. }
  259. EXPORT_SYMBOL_GPL(rtl_ips_nic_on);
  260. /*for FW LPS*/
  261. /*
  262. *Determine if we can set Fw into PS mode
  263. *in current condition.Return TRUE if it
  264. *can enter PS mode.
  265. */
  266. static bool rtl_get_fwlps_doze(struct ieee80211_hw *hw)
  267. {
  268. struct rtl_priv *rtlpriv = rtl_priv(hw);
  269. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  270. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  271. u32 ps_timediff;
  272. ps_timediff = jiffies_to_msecs(jiffies -
  273. ppsc->last_delaylps_stamp_jiffies);
  274. if (ps_timediff < 2000) {
  275. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  276. "Delay enter Fw LPS for DHCP, ARP, or EAPOL exchanging state\n");
  277. return false;
  278. }
  279. if (mac->link_state != MAC80211_LINKED)
  280. return false;
  281. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  282. return false;
  283. return true;
  284. }
  285. /* Change current and default preamble mode.*/
  286. void rtl_lps_set_psmode(struct ieee80211_hw *hw, u8 rt_psmode)
  287. {
  288. struct rtl_priv *rtlpriv = rtl_priv(hw);
  289. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  290. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  291. bool enter_fwlps;
  292. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  293. return;
  294. if (mac->link_state != MAC80211_LINKED)
  295. return;
  296. if (ppsc->dot11_psmode == rt_psmode)
  297. return;
  298. /* Update power save mode configured. */
  299. ppsc->dot11_psmode = rt_psmode;
  300. /*
  301. *<FW control LPS>
  302. *1. Enter PS mode
  303. * Set RPWM to Fw to turn RF off and send H2C fw_pwrmode
  304. * cmd to set Fw into PS mode.
  305. *2. Leave PS mode
  306. * Send H2C fw_pwrmode cmd to Fw to set Fw into Active
  307. * mode and set RPWM to turn RF on.
  308. */
  309. if ((ppsc->fwctrl_lps) && ppsc->report_linked) {
  310. if (ppsc->dot11_psmode == EACTIVE) {
  311. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  312. "FW LPS leave ps_mode:%x\n",
  313. FW_PS_ACTIVE_MODE);
  314. enter_fwlps = false;
  315. ppsc->pwr_mode = FW_PS_ACTIVE_MODE;
  316. ppsc->smart_ps = 0;
  317. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_FW_LPS_ACTION,
  318. (u8 *)(&enter_fwlps));
  319. if (ppsc->p2p_ps_info.opp_ps)
  320. rtl_p2p_ps_cmd(hw , P2P_PS_ENABLE);
  321. if (rtlpriv->cfg->ops->get_btc_status())
  322. rtlpriv->btcoexist.btc_ops->btc_lps_notify(rtlpriv, rt_psmode);
  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. if (rtlpriv->cfg->ops->get_btc_status())
  329. rtlpriv->btcoexist.btc_ops->btc_lps_notify(rtlpriv, rt_psmode);
  330. enter_fwlps = true;
  331. ppsc->pwr_mode = ppsc->fwctrl_psmode;
  332. ppsc->smart_ps = 2;
  333. rtlpriv->cfg->ops->set_hw_reg(hw,
  334. HW_VAR_FW_LPS_ACTION,
  335. (u8 *)(&enter_fwlps));
  336. } else {
  337. /* Reset the power save related parameters. */
  338. ppsc->dot11_psmode = EACTIVE;
  339. }
  340. }
  341. }
  342. }
  343. /* Interrupt safe routine to enter the leisure power save mode.*/
  344. static void rtl_lps_enter_core(struct ieee80211_hw *hw)
  345. {
  346. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  347. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  348. struct rtl_priv *rtlpriv = rtl_priv(hw);
  349. unsigned long flag;
  350. if (!ppsc->fwctrl_lps)
  351. return;
  352. if (rtlpriv->sec.being_setkey)
  353. return;
  354. if (rtlpriv->link_info.busytraffic)
  355. return;
  356. /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
  357. if (mac->cnt_after_linked < 5)
  358. return;
  359. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  360. return;
  361. if (mac->link_state != MAC80211_LINKED)
  362. return;
  363. spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag);
  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. spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag);
  373. }
  374. /* Interrupt safe routine to leave the leisure power save mode.*/
  375. static void rtl_lps_leave_core(struct ieee80211_hw *hw)
  376. {
  377. struct rtl_priv *rtlpriv = rtl_priv(hw);
  378. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  379. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  380. unsigned long flag;
  381. spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag);
  382. if (ppsc->fwctrl_lps) {
  383. if (ppsc->dot11_psmode != EACTIVE) {
  384. /*FIX ME */
  385. /*rtlpriv->cfg->ops->enable_interrupt(hw); */
  386. if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
  387. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
  388. rtlhal->interface == INTF_PCI) {
  389. rtlpriv->intf_ops->disable_aspm(hw);
  390. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  391. }
  392. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  393. "Busy Traffic,Leave 802.11 power save..\n");
  394. rtl_lps_set_psmode(hw, EACTIVE);
  395. }
  396. }
  397. spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag);
  398. }
  399. /* For sw LPS*/
  400. void rtl_swlps_beacon(struct ieee80211_hw *hw, void *data, unsigned int len)
  401. {
  402. struct rtl_priv *rtlpriv = rtl_priv(hw);
  403. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  404. struct ieee80211_hdr *hdr = data;
  405. struct ieee80211_tim_ie *tim_ie;
  406. u8 *tim;
  407. u8 tim_len;
  408. bool u_buffed;
  409. bool m_buffed;
  410. if (mac->opmode != NL80211_IFTYPE_STATION)
  411. return;
  412. if (!rtlpriv->psc.swctrl_lps)
  413. return;
  414. if (rtlpriv->mac80211.link_state != MAC80211_LINKED)
  415. return;
  416. if (!rtlpriv->psc.sw_ps_enabled)
  417. return;
  418. if (rtlpriv->psc.fwctrl_lps)
  419. return;
  420. if (likely(!(hw->conf.flags & IEEE80211_CONF_PS)))
  421. return;
  422. /* check if this really is a beacon */
  423. if (!ieee80211_is_beacon(hdr->frame_control))
  424. return;
  425. /* min. beacon length + FCS_LEN */
  426. if (len <= 40 + FCS_LEN)
  427. return;
  428. /* and only beacons from the associated BSSID, please */
  429. if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
  430. return;
  431. rtlpriv->psc.last_beacon = jiffies;
  432. tim = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_TIM);
  433. if (!tim)
  434. return;
  435. if (tim[1] < sizeof(*tim_ie))
  436. return;
  437. tim_len = tim[1];
  438. tim_ie = (struct ieee80211_tim_ie *) &tim[2];
  439. if (!WARN_ON_ONCE(!hw->conf.ps_dtim_period))
  440. rtlpriv->psc.dtim_counter = tim_ie->dtim_count;
  441. /* Check whenever the PHY can be turned off again. */
  442. /* 1. What about buffered unicast traffic for our AID? */
  443. u_buffed = ieee80211_check_tim(tim_ie, tim_len,
  444. rtlpriv->mac80211.assoc_id);
  445. /* 2. Maybe the AP wants to send multicast/broadcast data? */
  446. m_buffed = tim_ie->bitmap_ctrl & 0x01;
  447. rtlpriv->psc.multi_buffered = m_buffed;
  448. /* unicast will process by mac80211 through
  449. * set ~IEEE80211_CONF_PS, So we just check
  450. * multicast frames here */
  451. if (!m_buffed) {
  452. /* back to low-power land. and delay is
  453. * prevent null power save frame tx fail */
  454. queue_delayed_work(rtlpriv->works.rtl_wq,
  455. &rtlpriv->works.ps_work, MSECS(5));
  456. } else {
  457. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  458. "u_bufferd: %x, m_buffered: %x\n", u_buffed, m_buffed);
  459. }
  460. }
  461. EXPORT_SYMBOL_GPL(rtl_swlps_beacon);
  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(&rtlpriv->locks.rf_ps_lock);
  506. if (rtlpriv->psc.rfchange_inprogress) {
  507. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  508. return;
  509. }
  510. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  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 beacon_intv\n",
  539. 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_lps_change_work_callback(struct work_struct *work)
  545. {
  546. struct rtl_works *rtlworks =
  547. container_of(work, struct rtl_works, lps_change_work);
  548. struct ieee80211_hw *hw = rtlworks->hw;
  549. struct rtl_priv *rtlpriv = rtl_priv(hw);
  550. if (rtlpriv->enter_ps)
  551. rtl_lps_enter_core(hw);
  552. else
  553. rtl_lps_leave_core(hw);
  554. }
  555. EXPORT_SYMBOL_GPL(rtl_lps_change_work_callback);
  556. void rtl_lps_enter(struct ieee80211_hw *hw)
  557. {
  558. struct rtl_priv *rtlpriv = rtl_priv(hw);
  559. if (!in_interrupt())
  560. return rtl_lps_enter_core(hw);
  561. rtlpriv->enter_ps = true;
  562. schedule_work(&rtlpriv->works.lps_change_work);
  563. }
  564. EXPORT_SYMBOL_GPL(rtl_lps_enter);
  565. void rtl_lps_leave(struct ieee80211_hw *hw)
  566. {
  567. struct rtl_priv *rtlpriv = rtl_priv(hw);
  568. if (!in_interrupt())
  569. return rtl_lps_leave_core(hw);
  570. rtlpriv->enter_ps = false;
  571. schedule_work(&rtlpriv->works.lps_change_work);
  572. }
  573. EXPORT_SYMBOL_GPL(rtl_lps_leave);
  574. void rtl_swlps_wq_callback(void *data)
  575. {
  576. struct rtl_works *rtlworks = container_of_dwork_rtl(data,
  577. struct rtl_works,
  578. ps_work);
  579. struct ieee80211_hw *hw = rtlworks->hw;
  580. struct rtl_priv *rtlpriv = rtl_priv(hw);
  581. bool ps = false;
  582. ps = (hw->conf.flags & IEEE80211_CONF_PS);
  583. /* we can sleep after ps null send ok */
  584. if (rtlpriv->psc.state_inap) {
  585. rtl_swlps_rf_sleep(hw);
  586. if (rtlpriv->psc.state && !ps) {
  587. rtlpriv->psc.sleep_ms = jiffies_to_msecs(jiffies -
  588. rtlpriv->psc.last_action);
  589. }
  590. if (ps)
  591. rtlpriv->psc.last_slept = jiffies;
  592. rtlpriv->psc.last_action = jiffies;
  593. rtlpriv->psc.state = ps;
  594. }
  595. }
  596. static void rtl_p2p_noa_ie(struct ieee80211_hw *hw, void *data,
  597. unsigned int len)
  598. {
  599. struct rtl_priv *rtlpriv = rtl_priv(hw);
  600. struct ieee80211_mgmt *mgmt = data;
  601. struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
  602. u8 *pos, *end, *ie;
  603. u16 noa_len;
  604. static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
  605. u8 noa_num, index , i, noa_index = 0;
  606. bool find_p2p_ie = false , find_p2p_ps_ie = false;
  607. pos = (u8 *)mgmt->u.beacon.variable;
  608. end = data + len;
  609. ie = NULL;
  610. while (pos + 1 < end) {
  611. if (pos + 2 + pos[1] > end)
  612. return;
  613. if (pos[0] == 221 && pos[1] > 4) {
  614. if (memcmp(&pos[2], p2p_oui_ie_type, 4) == 0) {
  615. ie = pos + 2+4;
  616. break;
  617. }
  618. }
  619. pos += 2 + pos[1];
  620. }
  621. if (ie == NULL)
  622. return;
  623. find_p2p_ie = true;
  624. /*to find noa ie*/
  625. while (ie + 1 < end) {
  626. noa_len = READEF2BYTE((__le16 *)&ie[1]);
  627. if (ie + 3 + ie[1] > end)
  628. return;
  629. if (ie[0] == 12) {
  630. find_p2p_ps_ie = true;
  631. if ((noa_len - 2) % 13 != 0) {
  632. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  633. "P2P notice of absence: invalid length.%d\n",
  634. noa_len);
  635. return;
  636. } else {
  637. noa_num = (noa_len - 2) / 13;
  638. }
  639. noa_index = ie[3];
  640. if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
  641. P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
  642. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  643. "update NOA ie.\n");
  644. p2pinfo->noa_index = noa_index;
  645. p2pinfo->opp_ps = (ie[4] >> 7);
  646. p2pinfo->ctwindow = ie[4] & 0x7F;
  647. p2pinfo->noa_num = noa_num;
  648. index = 5;
  649. for (i = 0; i < noa_num; i++) {
  650. p2pinfo->noa_count_type[i] =
  651. READEF1BYTE(ie+index);
  652. index += 1;
  653. p2pinfo->noa_duration[i] =
  654. READEF4BYTE((__le32 *)ie+index);
  655. index += 4;
  656. p2pinfo->noa_interval[i] =
  657. READEF4BYTE((__le32 *)ie+index);
  658. index += 4;
  659. p2pinfo->noa_start_time[i] =
  660. READEF4BYTE((__le32 *)ie+index);
  661. index += 4;
  662. }
  663. if (p2pinfo->opp_ps == 1) {
  664. p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
  665. /* Driver should wait LPS entering
  666. * CTWindow
  667. */
  668. if (rtlpriv->psc.fw_current_inpsmode)
  669. rtl_p2p_ps_cmd(hw,
  670. P2P_PS_ENABLE);
  671. } else if (p2pinfo->noa_num > 0) {
  672. p2pinfo->p2p_ps_mode = P2P_PS_NOA;
  673. rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
  674. } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  675. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  676. }
  677. }
  678. break;
  679. }
  680. ie += 3 + noa_len;
  681. }
  682. if (find_p2p_ie == true) {
  683. if ((p2pinfo->p2p_ps_mode > P2P_PS_NONE) &&
  684. (find_p2p_ps_ie == false))
  685. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  686. }
  687. }
  688. static void rtl_p2p_action_ie(struct ieee80211_hw *hw, void *data,
  689. unsigned int len)
  690. {
  691. struct rtl_priv *rtlpriv = rtl_priv(hw);
  692. struct ieee80211_mgmt *mgmt = data;
  693. struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
  694. u8 noa_num, index , i , noa_index = 0;
  695. u8 *pos, *end, *ie;
  696. u16 noa_len;
  697. static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
  698. pos = (u8 *)&mgmt->u.action.category;
  699. end = data + len;
  700. ie = NULL;
  701. if (pos[0] == 0x7f) {
  702. if (memcmp(&pos[1], p2p_oui_ie_type, 4) == 0)
  703. ie = pos + 3+4;
  704. }
  705. if (ie == NULL)
  706. return;
  707. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "action frame find P2P IE.\n");
  708. /*to find noa ie*/
  709. while (ie + 1 < end) {
  710. noa_len = READEF2BYTE((__le16 *)&ie[1]);
  711. if (ie + 3 + ie[1] > end)
  712. return;
  713. if (ie[0] == 12) {
  714. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "find NOA IE.\n");
  715. RT_PRINT_DATA(rtlpriv, COMP_FW, DBG_LOUD, "noa ie ",
  716. ie, noa_len);
  717. if ((noa_len - 2) % 13 != 0) {
  718. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  719. "P2P notice of absence: invalid length.%d\n",
  720. noa_len);
  721. return;
  722. } else {
  723. noa_num = (noa_len - 2) / 13;
  724. }
  725. noa_index = ie[3];
  726. if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
  727. P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
  728. p2pinfo->noa_index = noa_index;
  729. p2pinfo->opp_ps = (ie[4] >> 7);
  730. p2pinfo->ctwindow = ie[4] & 0x7F;
  731. p2pinfo->noa_num = noa_num;
  732. index = 5;
  733. for (i = 0; i < noa_num; i++) {
  734. p2pinfo->noa_count_type[i] =
  735. READEF1BYTE(ie+index);
  736. index += 1;
  737. p2pinfo->noa_duration[i] =
  738. READEF4BYTE((__le32 *)ie+index);
  739. index += 4;
  740. p2pinfo->noa_interval[i] =
  741. READEF4BYTE((__le32 *)ie+index);
  742. index += 4;
  743. p2pinfo->noa_start_time[i] =
  744. READEF4BYTE((__le32 *)ie+index);
  745. index += 4;
  746. }
  747. if (p2pinfo->opp_ps == 1) {
  748. p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
  749. /* Driver should wait LPS entering
  750. * CTWindow
  751. */
  752. if (rtlpriv->psc.fw_current_inpsmode)
  753. rtl_p2p_ps_cmd(hw,
  754. P2P_PS_ENABLE);
  755. } else if (p2pinfo->noa_num > 0) {
  756. p2pinfo->p2p_ps_mode = P2P_PS_NOA;
  757. rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
  758. } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  759. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  760. }
  761. }
  762. break;
  763. }
  764. ie += 3 + noa_len;
  765. }
  766. }
  767. void rtl_p2p_ps_cmd(struct ieee80211_hw *hw , u8 p2p_ps_state)
  768. {
  769. struct rtl_priv *rtlpriv = rtl_priv(hw);
  770. struct rtl_ps_ctl *rtlps = rtl_psc(rtl_priv(hw));
  771. struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
  772. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, " p2p state %x\n" , p2p_ps_state);
  773. switch (p2p_ps_state) {
  774. case P2P_PS_DISABLE:
  775. p2pinfo->p2p_ps_state = p2p_ps_state;
  776. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  777. &p2p_ps_state);
  778. p2pinfo->noa_index = 0;
  779. p2pinfo->ctwindow = 0;
  780. p2pinfo->opp_ps = 0;
  781. p2pinfo->noa_num = 0;
  782. p2pinfo->p2p_ps_mode = P2P_PS_NONE;
  783. if (rtlps->fw_current_inpsmode) {
  784. if (rtlps->smart_ps == 0) {
  785. rtlps->smart_ps = 2;
  786. rtlpriv->cfg->ops->set_hw_reg(hw,
  787. HW_VAR_H2C_FW_PWRMODE,
  788. &rtlps->pwr_mode);
  789. }
  790. }
  791. break;
  792. case P2P_PS_ENABLE:
  793. if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  794. p2pinfo->p2p_ps_state = p2p_ps_state;
  795. if (p2pinfo->ctwindow > 0) {
  796. if (rtlps->smart_ps != 0) {
  797. rtlps->smart_ps = 0;
  798. rtlpriv->cfg->ops->set_hw_reg(hw,
  799. HW_VAR_H2C_FW_PWRMODE,
  800. &rtlps->pwr_mode);
  801. }
  802. }
  803. rtlpriv->cfg->ops->set_hw_reg(hw,
  804. HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  805. &p2p_ps_state);
  806. }
  807. break;
  808. case P2P_PS_SCAN:
  809. case P2P_PS_SCAN_DONE:
  810. case P2P_PS_ALLSTASLEEP:
  811. if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  812. p2pinfo->p2p_ps_state = p2p_ps_state;
  813. rtlpriv->cfg->ops->set_hw_reg(hw,
  814. HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  815. &p2p_ps_state);
  816. }
  817. break;
  818. default:
  819. break;
  820. }
  821. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  822. "ctwindow %x oppps %x\n",
  823. p2pinfo->ctwindow , p2pinfo->opp_ps);
  824. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  825. "count %x duration %x index %x interval %x start time %x noa num %x\n",
  826. p2pinfo->noa_count_type[0],
  827. p2pinfo->noa_duration[0],
  828. p2pinfo->noa_index,
  829. p2pinfo->noa_interval[0],
  830. p2pinfo->noa_start_time[0],
  831. p2pinfo->noa_num);
  832. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "end\n");
  833. }
  834. void rtl_p2p_info(struct ieee80211_hw *hw, void *data, unsigned int len)
  835. {
  836. struct rtl_priv *rtlpriv = rtl_priv(hw);
  837. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  838. struct ieee80211_hdr *hdr = data;
  839. if (!mac->p2p)
  840. return;
  841. if (mac->link_state != MAC80211_LINKED)
  842. return;
  843. /* min. beacon length + FCS_LEN */
  844. if (len <= 40 + FCS_LEN)
  845. return;
  846. /* and only beacons from the associated BSSID, please */
  847. if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
  848. return;
  849. /* check if this really is a beacon */
  850. if (!(ieee80211_is_beacon(hdr->frame_control) ||
  851. ieee80211_is_probe_resp(hdr->frame_control) ||
  852. ieee80211_is_action(hdr->frame_control)))
  853. return;
  854. if (ieee80211_is_action(hdr->frame_control))
  855. rtl_p2p_action_ie(hw , data , len - FCS_LEN);
  856. else
  857. rtl_p2p_noa_ie(hw , data , len - FCS_LEN);
  858. }
  859. EXPORT_SYMBOL_GPL(rtl_p2p_info);