ps.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  36. /*<1> reset trx ring */
  37. if (rtlhal->interface == INTF_PCI)
  38. rtlpriv->intf_ops->reset_trx_ring(hw);
  39. if (is_hal_stop(rtlhal))
  40. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  41. "Driver is already down!\n");
  42. /*<2> Enable Adapter */
  43. if (rtlpriv->cfg->ops->hw_init(hw))
  44. return false;
  45. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
  46. &rtlmac->retry_long);
  47. RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
  48. rtlpriv->cfg->ops->switch_channel(hw);
  49. rtlpriv->cfg->ops->set_channel_access(hw);
  50. rtlpriv->cfg->ops->set_bw_mode(hw,
  51. cfg80211_get_chandef_type(&hw->conf.chandef));
  52. /*<3> Enable Interrupt */
  53. rtlpriv->cfg->ops->enable_interrupt(hw);
  54. /*<enable timer> */
  55. rtl_watch_dog_timer_callback(&rtlpriv->works.watchdog_timer);
  56. return true;
  57. }
  58. EXPORT_SYMBOL(rtl_ps_enable_nic);
  59. bool rtl_ps_disable_nic(struct ieee80211_hw *hw)
  60. {
  61. struct rtl_priv *rtlpriv = rtl_priv(hw);
  62. /*<1> Stop all timer */
  63. rtl_deinit_deferred_work(hw, true);
  64. /*<2> Disable Interrupt */
  65. rtlpriv->cfg->ops->disable_interrupt(hw);
  66. tasklet_kill(&rtlpriv->works.irq_tasklet);
  67. /*<3> Disable Adapter */
  68. rtlpriv->cfg->ops->hw_disable(hw);
  69. return true;
  70. }
  71. EXPORT_SYMBOL(rtl_ps_disable_nic);
  72. static bool rtl_ps_set_rf_state(struct ieee80211_hw *hw,
  73. enum rf_pwrstate state_toset,
  74. u32 changesource)
  75. {
  76. struct rtl_priv *rtlpriv = rtl_priv(hw);
  77. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  78. enum rf_pwrstate rtstate;
  79. bool actionallowed = false;
  80. u16 rfwait_cnt = 0;
  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(&rtlpriv->locks.rf_ps_lock);
  87. if (ppsc->rfchange_inprogress) {
  88. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  89. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  90. "RF Change in progress! Wait to set..state_toset(%d).\n",
  91. state_toset);
  92. /* Set RF after the previous action is done. */
  93. while (ppsc->rfchange_inprogress) {
  94. rfwait_cnt++;
  95. mdelay(1);
  96. /*Wait too long, return false to avoid
  97. *to be stuck here.
  98. */
  99. if (rfwait_cnt > 100)
  100. return false;
  101. }
  102. } else {
  103. ppsc->rfchange_inprogress = true;
  104. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  105. break;
  106. }
  107. }
  108. rtstate = ppsc->rfpwr_state;
  109. switch (state_toset) {
  110. case ERFON:
  111. ppsc->rfoff_reason &= (~changesource);
  112. if ((changesource == RF_CHANGE_BY_HW) &&
  113. (ppsc->hwradiooff)) {
  114. ppsc->hwradiooff = false;
  115. }
  116. if (!ppsc->rfoff_reason) {
  117. ppsc->rfoff_reason = 0;
  118. actionallowed = true;
  119. }
  120. break;
  121. case ERFOFF:
  122. if ((changesource == RF_CHANGE_BY_HW) && !ppsc->hwradiooff) {
  123. ppsc->hwradiooff = true;
  124. }
  125. ppsc->rfoff_reason |= changesource;
  126. actionallowed = true;
  127. break;
  128. case ERFSLEEP:
  129. ppsc->rfoff_reason |= changesource;
  130. actionallowed = true;
  131. break;
  132. default:
  133. pr_err("switch case %#x not processed\n", state_toset);
  134. break;
  135. }
  136. if (actionallowed)
  137. rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset);
  138. spin_lock(&rtlpriv->locks.rf_ps_lock);
  139. ppsc->rfchange_inprogress = false;
  140. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  141. return actionallowed;
  142. }
  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);
  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_sync(&rtlpriv->works.ips_nic_off_wq);
  242. mutex_lock(&rtlpriv->locks.ips_mutex);
  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. mutex_unlock(&rtlpriv->locks.ips_mutex);
  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 && rt_psmode == EACTIVE)
  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. if (!ppsc->fwctrl_lps)
  350. return;
  351. if (rtlpriv->sec.being_setkey)
  352. return;
  353. if (rtlpriv->link_info.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. mutex_lock(&rtlpriv->locks.lps_mutex);
  363. /* Don't need to check (ppsc->dot11_psmode == EACTIVE), because
  364. * bt_ccoexist may ask to enter lps.
  365. * In normal case, this constraint move to rtl_lps_set_psmode().
  366. */
  367. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  368. "Enter 802.11 power save mode...\n");
  369. rtl_lps_set_psmode(hw, EAUTOPS);
  370. mutex_unlock(&rtlpriv->locks.lps_mutex);
  371. }
  372. /* Interrupt safe routine to leave the leisure power save mode.*/
  373. static void rtl_lps_leave_core(struct ieee80211_hw *hw)
  374. {
  375. struct rtl_priv *rtlpriv = rtl_priv(hw);
  376. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  377. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  378. mutex_lock(&rtlpriv->locks.lps_mutex);
  379. if (ppsc->fwctrl_lps) {
  380. if (ppsc->dot11_psmode != EACTIVE) {
  381. /*FIX ME */
  382. /*rtlpriv->cfg->ops->enable_interrupt(hw); */
  383. if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
  384. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
  385. rtlhal->interface == INTF_PCI) {
  386. rtlpriv->intf_ops->disable_aspm(hw);
  387. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  388. }
  389. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  390. "Busy Traffic,Leave 802.11 power save..\n");
  391. rtl_lps_set_psmode(hw, EACTIVE);
  392. }
  393. }
  394. mutex_unlock(&rtlpriv->locks.lps_mutex);
  395. }
  396. /* For sw LPS*/
  397. void rtl_swlps_beacon(struct ieee80211_hw *hw, void *data, unsigned int len)
  398. {
  399. struct rtl_priv *rtlpriv = rtl_priv(hw);
  400. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  401. struct ieee80211_hdr *hdr = data;
  402. struct ieee80211_tim_ie *tim_ie;
  403. u8 *tim;
  404. u8 tim_len;
  405. bool u_buffed;
  406. bool m_buffed;
  407. if (mac->opmode != NL80211_IFTYPE_STATION)
  408. return;
  409. if (!rtlpriv->psc.swctrl_lps)
  410. return;
  411. if (rtlpriv->mac80211.link_state != MAC80211_LINKED)
  412. return;
  413. if (!rtlpriv->psc.sw_ps_enabled)
  414. return;
  415. if (rtlpriv->psc.fwctrl_lps)
  416. return;
  417. if (likely(!(hw->conf.flags & IEEE80211_CONF_PS)))
  418. return;
  419. /* check if this really is a beacon */
  420. if (!ieee80211_is_beacon(hdr->frame_control))
  421. return;
  422. /* min. beacon length + FCS_LEN */
  423. if (len <= 40 + FCS_LEN)
  424. return;
  425. /* and only beacons from the associated BSSID, please */
  426. if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
  427. return;
  428. rtlpriv->psc.last_beacon = jiffies;
  429. tim = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_TIM);
  430. if (!tim)
  431. return;
  432. if (tim[1] < sizeof(*tim_ie))
  433. return;
  434. tim_len = tim[1];
  435. tim_ie = (struct ieee80211_tim_ie *) &tim[2];
  436. if (!WARN_ON_ONCE(!hw->conf.ps_dtim_period))
  437. rtlpriv->psc.dtim_counter = tim_ie->dtim_count;
  438. /* Check whenever the PHY can be turned off again. */
  439. /* 1. What about buffered unicast traffic for our AID? */
  440. u_buffed = ieee80211_check_tim(tim_ie, tim_len,
  441. rtlpriv->mac80211.assoc_id);
  442. /* 2. Maybe the AP wants to send multicast/broadcast data? */
  443. m_buffed = tim_ie->bitmap_ctrl & 0x01;
  444. rtlpriv->psc.multi_buffered = m_buffed;
  445. /* unicast will process by mac80211 through
  446. * set ~IEEE80211_CONF_PS, So we just check
  447. * multicast frames here */
  448. if (!m_buffed) {
  449. /* back to low-power land. and delay is
  450. * prevent null power save frame tx fail */
  451. queue_delayed_work(rtlpriv->works.rtl_wq,
  452. &rtlpriv->works.ps_work, MSECS(5));
  453. } else {
  454. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  455. "u_bufferd: %x, m_buffered: %x\n", u_buffed, m_buffed);
  456. }
  457. }
  458. EXPORT_SYMBOL_GPL(rtl_swlps_beacon);
  459. void rtl_swlps_rf_awake(struct ieee80211_hw *hw)
  460. {
  461. struct rtl_priv *rtlpriv = rtl_priv(hw);
  462. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  463. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  464. if (!rtlpriv->psc.swctrl_lps)
  465. return;
  466. if (mac->link_state != MAC80211_LINKED)
  467. return;
  468. if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
  469. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  470. rtlpriv->intf_ops->disable_aspm(hw);
  471. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  472. }
  473. mutex_lock(&rtlpriv->locks.lps_mutex);
  474. rtl_ps_set_rf_state(hw, ERFON, RF_CHANGE_BY_PS);
  475. mutex_unlock(&rtlpriv->locks.lps_mutex);
  476. }
  477. void rtl_swlps_rfon_wq_callback(void *data)
  478. {
  479. struct rtl_works *rtlworks =
  480. container_of_dwork_rtl(data, struct rtl_works, ps_rfon_wq);
  481. struct ieee80211_hw *hw = rtlworks->hw;
  482. rtl_swlps_rf_awake(hw);
  483. }
  484. void rtl_swlps_rf_sleep(struct ieee80211_hw *hw)
  485. {
  486. struct rtl_priv *rtlpriv = rtl_priv(hw);
  487. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  488. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  489. u8 sleep_intv;
  490. if (!rtlpriv->psc.sw_ps_enabled)
  491. return;
  492. if ((rtlpriv->sec.being_setkey) ||
  493. (mac->opmode == NL80211_IFTYPE_ADHOC))
  494. return;
  495. /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
  496. if ((mac->link_state != MAC80211_LINKED) || (mac->cnt_after_linked < 5))
  497. return;
  498. if (rtlpriv->link_info.busytraffic)
  499. return;
  500. spin_lock(&rtlpriv->locks.rf_ps_lock);
  501. if (rtlpriv->psc.rfchange_inprogress) {
  502. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  503. return;
  504. }
  505. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  506. mutex_lock(&rtlpriv->locks.lps_mutex);
  507. rtl_ps_set_rf_state(hw, ERFSLEEP, RF_CHANGE_BY_PS);
  508. mutex_unlock(&rtlpriv->locks.lps_mutex);
  509. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM &&
  510. !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  511. rtlpriv->intf_ops->enable_aspm(hw);
  512. RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  513. }
  514. /* here is power save alg, when this beacon is DTIM
  515. * we will set sleep time to dtim_period * n;
  516. * when this beacon is not DTIM, we will set sleep
  517. * time to sleep_intv = rtlpriv->psc.dtim_counter or
  518. * MAX_SW_LPS_SLEEP_INTV(default set to 5) */
  519. if (rtlpriv->psc.dtim_counter == 0) {
  520. if (hw->conf.ps_dtim_period == 1)
  521. sleep_intv = hw->conf.ps_dtim_period * 2;
  522. else
  523. sleep_intv = hw->conf.ps_dtim_period;
  524. } else {
  525. sleep_intv = rtlpriv->psc.dtim_counter;
  526. }
  527. if (sleep_intv > MAX_SW_LPS_SLEEP_INTV)
  528. sleep_intv = MAX_SW_LPS_SLEEP_INTV;
  529. /* this print should always be dtim_conter = 0 &
  530. * sleep = dtim_period, that meaons, we should
  531. * awake before every dtim */
  532. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  533. "dtim_counter:%x will sleep :%d beacon_intv\n",
  534. rtlpriv->psc.dtim_counter, sleep_intv);
  535. /* we tested that 40ms is enough for sw & hw sw delay */
  536. queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.ps_rfon_wq,
  537. MSECS(sleep_intv * mac->vif->bss_conf.beacon_int - 40));
  538. }
  539. void rtl_lps_change_work_callback(struct work_struct *work)
  540. {
  541. struct rtl_works *rtlworks =
  542. container_of(work, struct rtl_works, lps_change_work);
  543. struct ieee80211_hw *hw = rtlworks->hw;
  544. struct rtl_priv *rtlpriv = rtl_priv(hw);
  545. if (rtlpriv->enter_ps)
  546. rtl_lps_enter_core(hw);
  547. else
  548. rtl_lps_leave_core(hw);
  549. }
  550. EXPORT_SYMBOL_GPL(rtl_lps_change_work_callback);
  551. void rtl_lps_enter(struct ieee80211_hw *hw)
  552. {
  553. struct rtl_priv *rtlpriv = rtl_priv(hw);
  554. if (!in_interrupt())
  555. return rtl_lps_enter_core(hw);
  556. rtlpriv->enter_ps = true;
  557. schedule_work(&rtlpriv->works.lps_change_work);
  558. }
  559. EXPORT_SYMBOL_GPL(rtl_lps_enter);
  560. void rtl_lps_leave(struct ieee80211_hw *hw)
  561. {
  562. struct rtl_priv *rtlpriv = rtl_priv(hw);
  563. if (!in_interrupt())
  564. return rtl_lps_leave_core(hw);
  565. rtlpriv->enter_ps = false;
  566. schedule_work(&rtlpriv->works.lps_change_work);
  567. }
  568. EXPORT_SYMBOL_GPL(rtl_lps_leave);
  569. void rtl_swlps_wq_callback(void *data)
  570. {
  571. struct rtl_works *rtlworks = container_of_dwork_rtl(data,
  572. struct rtl_works,
  573. ps_work);
  574. struct ieee80211_hw *hw = rtlworks->hw;
  575. struct rtl_priv *rtlpriv = rtl_priv(hw);
  576. bool ps = false;
  577. ps = (hw->conf.flags & IEEE80211_CONF_PS);
  578. /* we can sleep after ps null send ok */
  579. if (rtlpriv->psc.state_inap) {
  580. rtl_swlps_rf_sleep(hw);
  581. if (rtlpriv->psc.state && !ps) {
  582. rtlpriv->psc.sleep_ms = jiffies_to_msecs(jiffies -
  583. rtlpriv->psc.last_action);
  584. }
  585. if (ps)
  586. rtlpriv->psc.last_slept = jiffies;
  587. rtlpriv->psc.last_action = jiffies;
  588. rtlpriv->psc.state = ps;
  589. }
  590. }
  591. static void rtl_p2p_noa_ie(struct ieee80211_hw *hw, void *data,
  592. unsigned int len)
  593. {
  594. struct rtl_priv *rtlpriv = rtl_priv(hw);
  595. struct ieee80211_mgmt *mgmt = data;
  596. struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
  597. u8 *pos, *end, *ie;
  598. u16 noa_len;
  599. static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
  600. u8 noa_num, index , i, noa_index = 0;
  601. bool find_p2p_ie = false , find_p2p_ps_ie = false;
  602. pos = (u8 *)mgmt->u.beacon.variable;
  603. end = data + len;
  604. ie = NULL;
  605. while (pos + 1 < end) {
  606. if (pos + 2 + pos[1] > end)
  607. return;
  608. if (pos[0] == 221 && pos[1] > 4) {
  609. if (memcmp(&pos[2], p2p_oui_ie_type, 4) == 0) {
  610. ie = pos + 2+4;
  611. break;
  612. }
  613. }
  614. pos += 2 + pos[1];
  615. }
  616. if (ie == NULL)
  617. return;
  618. find_p2p_ie = true;
  619. /*to find noa ie*/
  620. while (ie + 1 < end) {
  621. noa_len = READEF2BYTE((__le16 *)&ie[1]);
  622. if (ie + 3 + ie[1] > end)
  623. return;
  624. if (ie[0] == 12) {
  625. find_p2p_ps_ie = true;
  626. if ((noa_len - 2) % 13 != 0) {
  627. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  628. "P2P notice of absence: invalid length.%d\n",
  629. noa_len);
  630. return;
  631. } else {
  632. noa_num = (noa_len - 2) / 13;
  633. if (noa_num > P2P_MAX_NOA_NUM)
  634. noa_num = P2P_MAX_NOA_NUM;
  635. }
  636. noa_index = ie[3];
  637. if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
  638. P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
  639. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  640. "update NOA ie.\n");
  641. p2pinfo->noa_index = noa_index;
  642. p2pinfo->opp_ps = (ie[4] >> 7);
  643. p2pinfo->ctwindow = ie[4] & 0x7F;
  644. p2pinfo->noa_num = noa_num;
  645. index = 5;
  646. for (i = 0; i < noa_num; i++) {
  647. p2pinfo->noa_count_type[i] =
  648. READEF1BYTE(ie+index);
  649. index += 1;
  650. p2pinfo->noa_duration[i] =
  651. READEF4BYTE((__le32 *)ie+index);
  652. index += 4;
  653. p2pinfo->noa_interval[i] =
  654. READEF4BYTE((__le32 *)ie+index);
  655. index += 4;
  656. p2pinfo->noa_start_time[i] =
  657. READEF4BYTE((__le32 *)ie+index);
  658. index += 4;
  659. }
  660. if (p2pinfo->opp_ps == 1) {
  661. p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
  662. /* Driver should wait LPS entering
  663. * CTWindow
  664. */
  665. if (rtlpriv->psc.fw_current_inpsmode)
  666. rtl_p2p_ps_cmd(hw,
  667. P2P_PS_ENABLE);
  668. } else if (p2pinfo->noa_num > 0) {
  669. p2pinfo->p2p_ps_mode = P2P_PS_NOA;
  670. rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
  671. } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  672. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  673. }
  674. }
  675. break;
  676. }
  677. ie += 3 + noa_len;
  678. }
  679. if (find_p2p_ie == true) {
  680. if ((p2pinfo->p2p_ps_mode > P2P_PS_NONE) &&
  681. (find_p2p_ps_ie == false))
  682. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  683. }
  684. }
  685. static void rtl_p2p_action_ie(struct ieee80211_hw *hw, void *data,
  686. unsigned int len)
  687. {
  688. struct rtl_priv *rtlpriv = rtl_priv(hw);
  689. struct ieee80211_mgmt *mgmt = data;
  690. struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
  691. u8 noa_num, index , i , noa_index = 0;
  692. u8 *pos, *end, *ie;
  693. u16 noa_len;
  694. static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
  695. pos = (u8 *)&mgmt->u.action.category;
  696. end = data + len;
  697. ie = NULL;
  698. if (pos[0] == 0x7f) {
  699. if (memcmp(&pos[1], p2p_oui_ie_type, 4) == 0)
  700. ie = pos + 3+4;
  701. }
  702. if (ie == NULL)
  703. return;
  704. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "action frame find P2P IE.\n");
  705. /*to find noa ie*/
  706. while (ie + 1 < end) {
  707. noa_len = READEF2BYTE((__le16 *)&ie[1]);
  708. if (ie + 3 + ie[1] > end)
  709. return;
  710. if (ie[0] == 12) {
  711. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "find NOA IE.\n");
  712. RT_PRINT_DATA(rtlpriv, COMP_FW, DBG_LOUD, "noa ie ",
  713. ie, noa_len);
  714. if ((noa_len - 2) % 13 != 0) {
  715. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  716. "P2P notice of absence: invalid length.%d\n",
  717. noa_len);
  718. return;
  719. } else {
  720. noa_num = (noa_len - 2) / 13;
  721. if (noa_num > P2P_MAX_NOA_NUM)
  722. noa_num = P2P_MAX_NOA_NUM;
  723. }
  724. noa_index = ie[3];
  725. if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
  726. P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
  727. p2pinfo->noa_index = noa_index;
  728. p2pinfo->opp_ps = (ie[4] >> 7);
  729. p2pinfo->ctwindow = ie[4] & 0x7F;
  730. p2pinfo->noa_num = noa_num;
  731. index = 5;
  732. for (i = 0; i < noa_num; i++) {
  733. p2pinfo->noa_count_type[i] =
  734. READEF1BYTE(ie+index);
  735. index += 1;
  736. p2pinfo->noa_duration[i] =
  737. READEF4BYTE((__le32 *)ie+index);
  738. index += 4;
  739. p2pinfo->noa_interval[i] =
  740. READEF4BYTE((__le32 *)ie+index);
  741. index += 4;
  742. p2pinfo->noa_start_time[i] =
  743. READEF4BYTE((__le32 *)ie+index);
  744. index += 4;
  745. }
  746. if (p2pinfo->opp_ps == 1) {
  747. p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
  748. /* Driver should wait LPS entering
  749. * CTWindow
  750. */
  751. if (rtlpriv->psc.fw_current_inpsmode)
  752. rtl_p2p_ps_cmd(hw,
  753. P2P_PS_ENABLE);
  754. } else if (p2pinfo->noa_num > 0) {
  755. p2pinfo->p2p_ps_mode = P2P_PS_NOA;
  756. rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
  757. } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  758. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  759. }
  760. }
  761. break;
  762. }
  763. ie += 3 + noa_len;
  764. }
  765. }
  766. void rtl_p2p_ps_cmd(struct ieee80211_hw *hw , u8 p2p_ps_state)
  767. {
  768. struct rtl_priv *rtlpriv = rtl_priv(hw);
  769. struct rtl_ps_ctl *rtlps = rtl_psc(rtl_priv(hw));
  770. struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
  771. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, " p2p state %x\n" , p2p_ps_state);
  772. switch (p2p_ps_state) {
  773. case P2P_PS_DISABLE:
  774. p2pinfo->p2p_ps_state = p2p_ps_state;
  775. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  776. &p2p_ps_state);
  777. p2pinfo->noa_index = 0;
  778. p2pinfo->ctwindow = 0;
  779. p2pinfo->opp_ps = 0;
  780. p2pinfo->noa_num = 0;
  781. p2pinfo->p2p_ps_mode = P2P_PS_NONE;
  782. if (rtlps->fw_current_inpsmode) {
  783. if (rtlps->smart_ps == 0) {
  784. rtlps->smart_ps = 2;
  785. rtlpriv->cfg->ops->set_hw_reg(hw,
  786. HW_VAR_H2C_FW_PWRMODE,
  787. &rtlps->pwr_mode);
  788. }
  789. }
  790. break;
  791. case P2P_PS_ENABLE:
  792. if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  793. p2pinfo->p2p_ps_state = p2p_ps_state;
  794. if (p2pinfo->ctwindow > 0) {
  795. if (rtlps->smart_ps != 0) {
  796. rtlps->smart_ps = 0;
  797. rtlpriv->cfg->ops->set_hw_reg(hw,
  798. HW_VAR_H2C_FW_PWRMODE,
  799. &rtlps->pwr_mode);
  800. }
  801. }
  802. rtlpriv->cfg->ops->set_hw_reg(hw,
  803. HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  804. &p2p_ps_state);
  805. }
  806. break;
  807. case P2P_PS_SCAN:
  808. case P2P_PS_SCAN_DONE:
  809. case P2P_PS_ALLSTASLEEP:
  810. if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  811. p2pinfo->p2p_ps_state = p2p_ps_state;
  812. rtlpriv->cfg->ops->set_hw_reg(hw,
  813. HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  814. &p2p_ps_state);
  815. }
  816. break;
  817. default:
  818. break;
  819. }
  820. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  821. "ctwindow %x oppps %x\n",
  822. p2pinfo->ctwindow , p2pinfo->opp_ps);
  823. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  824. "count %x duration %x index %x interval %x start time %x noa num %x\n",
  825. p2pinfo->noa_count_type[0],
  826. p2pinfo->noa_duration[0],
  827. p2pinfo->noa_index,
  828. p2pinfo->noa_interval[0],
  829. p2pinfo->noa_start_time[0],
  830. p2pinfo->noa_num);
  831. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "end\n");
  832. }
  833. void rtl_p2p_info(struct ieee80211_hw *hw, void *data, unsigned int len)
  834. {
  835. struct rtl_priv *rtlpriv = rtl_priv(hw);
  836. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  837. struct ieee80211_hdr *hdr = data;
  838. if (!mac->p2p)
  839. return;
  840. if (mac->link_state != MAC80211_LINKED)
  841. return;
  842. /* min. beacon length + FCS_LEN */
  843. if (len <= 40 + FCS_LEN)
  844. return;
  845. /* and only beacons from the associated BSSID, please */
  846. if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
  847. return;
  848. /* check if this really is a beacon */
  849. if (!(ieee80211_is_beacon(hdr->frame_control) ||
  850. ieee80211_is_probe_resp(hdr->frame_control) ||
  851. ieee80211_is_action(hdr->frame_control)))
  852. return;
  853. if (ieee80211_is_action(hdr->frame_control))
  854. rtl_p2p_action_ie(hw , data , len - FCS_LEN);
  855. else
  856. rtl_p2p_noa_ie(hw , data , len - FCS_LEN);
  857. }
  858. EXPORT_SYMBOL_GPL(rtl_p2p_info);