rtw_pwrctrl.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
  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. *
  19. ******************************************************************************/
  20. #define _RTW_PWRCTRL_C_
  21. #include <drv_types.h>
  22. // Should not include hal dependent herader here, it will remove later. Lucas@20130123
  23. #ifdef CONFIG_BT_COEXIST
  24. #include <rtl8723a_hal.h>
  25. #endif
  26. #ifdef CONFIG_IPS
  27. void ips_enter(_adapter * padapter)
  28. {
  29. struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
  30. _enter_pwrlock(&pwrpriv->lock);
  31. pwrpriv->bips_processing = _TRUE;
  32. // syn ips_mode with request
  33. pwrpriv->ips_mode = pwrpriv->ips_mode_req;
  34. pwrpriv->ips_enter_cnts++;
  35. DBG_871X("==>ips_enter cnts:%d\n",pwrpriv->ips_enter_cnts);
  36. #ifdef CONFIG_BT_COEXIST
  37. BTDM_TurnOffBtCoexistBeforeEnterIPS(padapter);
  38. #endif
  39. if(rf_off == pwrpriv->change_rfpwrstate )
  40. {
  41. pwrpriv->bpower_saving = _TRUE;
  42. DBG_871X_LEVEL(_drv_always_, "nolinked power save enter\n");
  43. if(pwrpriv->ips_mode == IPS_LEVEL_2)
  44. pwrpriv->bkeepfwalive = _TRUE;
  45. rtw_ips_pwr_down(padapter);
  46. pwrpriv->rf_pwrstate = rf_off;
  47. }
  48. pwrpriv->bips_processing = _FALSE;
  49. _exit_pwrlock(&pwrpriv->lock);
  50. }
  51. int ips_leave(_adapter * padapter)
  52. {
  53. struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
  54. struct security_priv* psecuritypriv=&(padapter->securitypriv);
  55. struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
  56. int result = _SUCCESS;
  57. sint keyid;
  58. _enter_pwrlock(&pwrpriv->lock);
  59. if((pwrpriv->rf_pwrstate == rf_off) &&(!pwrpriv->bips_processing))
  60. {
  61. pwrpriv->bips_processing = _TRUE;
  62. pwrpriv->change_rfpwrstate = rf_on;
  63. pwrpriv->ips_leave_cnts++;
  64. DBG_871X("==>ips_leave cnts:%d\n",pwrpriv->ips_leave_cnts);
  65. if ((result = rtw_ips_pwr_up(padapter)) == _SUCCESS) {
  66. pwrpriv->rf_pwrstate = rf_on;
  67. }
  68. DBG_871X_LEVEL(_drv_always_, "nolinked power save leave\n");
  69. if((_WEP40_ == psecuritypriv->dot11PrivacyAlgrthm) ||(_WEP104_ == psecuritypriv->dot11PrivacyAlgrthm))
  70. {
  71. DBG_871X("==>%s,channel(%d),processing(%x)\n",__FUNCTION__,padapter->mlmeextpriv.cur_channel,pwrpriv->bips_processing);
  72. set_channel_bwmode(padapter, padapter->mlmeextpriv.cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20);
  73. for(keyid=0;keyid<4;keyid++){
  74. if(pmlmepriv->key_mask & BIT(keyid)){
  75. if(keyid == psecuritypriv->dot11PrivacyKeyIndex)
  76. result=rtw_set_key(padapter,psecuritypriv, keyid, 1);
  77. else
  78. result=rtw_set_key(padapter,psecuritypriv, keyid, 0);
  79. }
  80. }
  81. }
  82. DBG_871X("==> ips_leave.....LED(0x%08x)...\n",rtw_read32(padapter,0x4c));
  83. pwrpriv->bips_processing = _FALSE;
  84. pwrpriv->bkeepfwalive = _FALSE;
  85. pwrpriv->bpower_saving = _FALSE;
  86. }
  87. _exit_pwrlock(&pwrpriv->lock);
  88. return result;
  89. }
  90. #endif
  91. #ifdef CONFIG_AUTOSUSPEND
  92. extern void autosuspend_enter(_adapter* padapter);
  93. extern int autoresume_enter(_adapter* padapter);
  94. #endif
  95. #ifdef SUPPORT_HW_RFOFF_DETECTED
  96. int rtw_hw_suspend(_adapter *padapter );
  97. int rtw_hw_resume(_adapter *padapter);
  98. #endif
  99. bool rtw_pwr_unassociated_idle(_adapter *adapter)
  100. {
  101. _adapter *buddy = adapter->pbuddy_adapter;
  102. struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
  103. struct xmit_priv *pxmit_priv = &adapter->xmitpriv;
  104. #ifdef CONFIG_P2P
  105. struct wifidirect_info *pwdinfo = &(adapter->wdinfo);
  106. #ifdef CONFIG_IOCTL_CFG80211
  107. struct cfg80211_wifidirect_info *pcfg80211_wdinfo = &adapter->cfg80211_wdinfo;
  108. #endif
  109. #endif
  110. bool ret = _FALSE;
  111. if (adapter->pwrctrlpriv.ips_deny_time >= rtw_get_current_time()) {
  112. //DBG_871X("%s ips_deny_time\n", __func__);
  113. goto exit;
  114. }
  115. if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE|WIFI_SITE_MONITOR)
  116. || check_fwstate(pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
  117. || check_fwstate(pmlmepriv, WIFI_AP_STATE)
  118. || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
  119. #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211) && defined(CONFIG_P2P_IPS)
  120. || pcfg80211_wdinfo->is_ro_ch
  121. #elif defined(CONFIG_P2P)
  122. || !rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)
  123. #endif
  124. ) {
  125. goto exit;
  126. }
  127. /* consider buddy, if exist */
  128. if (buddy) {
  129. struct mlme_priv *b_pmlmepriv = &(buddy->mlmepriv);
  130. #ifdef CONFIG_P2P
  131. struct wifidirect_info *b_pwdinfo = &(buddy->wdinfo);
  132. #ifdef CONFIG_IOCTL_CFG80211
  133. struct cfg80211_wifidirect_info *b_pcfg80211_wdinfo = &buddy->cfg80211_wdinfo;
  134. #endif
  135. #endif
  136. if (check_fwstate(b_pmlmepriv, WIFI_ASOC_STATE|WIFI_SITE_MONITOR)
  137. || check_fwstate(b_pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
  138. || check_fwstate(b_pmlmepriv, WIFI_AP_STATE)
  139. || check_fwstate(b_pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
  140. #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211) && defined(CONFIG_P2P_IPS)
  141. || b_pcfg80211_wdinfo->is_ro_ch
  142. #elif defined(CONFIG_P2P)
  143. || !rtw_p2p_chk_state(b_pwdinfo, P2P_STATE_NONE)
  144. #endif
  145. ) {
  146. goto exit;
  147. }
  148. }
  149. #if (MP_DRIVER == 1)
  150. if (adapter->registrypriv.mp_mode == 1)
  151. goto exit;
  152. #endif
  153. #ifdef CONFIG_INTEL_PROXIM
  154. if(adapter->proximity.proxim_on==_TRUE){
  155. return;
  156. }
  157. #endif
  158. if (pxmit_priv->free_xmitbuf_cnt != NR_XMITBUFF ||
  159. pxmit_priv->free_xmit_extbuf_cnt != NR_XMIT_EXTBUFF) {
  160. DBG_871X_LEVEL(_drv_always_, "There are some pkts to transmit\n");
  161. DBG_871X_LEVEL(_drv_info_, "free_xmitbuf_cnt: %d, free_xmit_extbuf_cnt: %d\n",
  162. pxmit_priv->free_xmitbuf_cnt, pxmit_priv->free_xmit_extbuf_cnt);
  163. goto exit;
  164. }
  165. ret = _TRUE;
  166. exit:
  167. return ret;
  168. }
  169. void rtw_ps_processor(_adapter*padapter)
  170. {
  171. #ifdef CONFIG_P2P
  172. struct wifidirect_info *pwdinfo = &( padapter->wdinfo );
  173. #endif //CONFIG_P2P
  174. struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
  175. struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
  176. #ifdef SUPPORT_HW_RFOFF_DETECTED
  177. rt_rf_power_state rfpwrstate;
  178. #endif //SUPPORT_HW_RFOFF_DETECTED
  179. pwrpriv->ps_processing = _TRUE;
  180. #ifdef SUPPORT_HW_RFOFF_DETECTED
  181. if(pwrpriv->bips_processing == _TRUE)
  182. goto exit;
  183. //DBG_871X("==> fw report state(0x%x)\n",rtw_read8(padapter,0x1ca));
  184. if(padapter->pwrctrlpriv.bHWPwrPindetect)
  185. {
  186. #ifdef CONFIG_AUTOSUSPEND
  187. if(padapter->registrypriv.usbss_enable)
  188. {
  189. if(pwrpriv->rf_pwrstate == rf_on)
  190. {
  191. if(padapter->net_closed == _TRUE)
  192. pwrpriv->ps_flag = _TRUE;
  193. rfpwrstate = RfOnOffDetect(padapter);
  194. DBG_871X("@@@@- #1 %s==> rfstate:%s \n",__FUNCTION__,(rfpwrstate==rf_on)?"rf_on":"rf_off");
  195. if(rfpwrstate!= pwrpriv->rf_pwrstate)
  196. {
  197. if(rfpwrstate == rf_off)
  198. {
  199. pwrpriv->change_rfpwrstate = rf_off;
  200. pwrpriv->bkeepfwalive = _TRUE;
  201. pwrpriv->brfoffbyhw = _TRUE;
  202. autosuspend_enter(padapter);
  203. }
  204. }
  205. }
  206. }
  207. else
  208. #endif //CONFIG_AUTOSUSPEND
  209. {
  210. rfpwrstate = RfOnOffDetect(padapter);
  211. DBG_871X("@@@@- #2 %s==> rfstate:%s \n",__FUNCTION__,(rfpwrstate==rf_on)?"rf_on":"rf_off");
  212. if(rfpwrstate!= pwrpriv->rf_pwrstate)
  213. {
  214. if(rfpwrstate == rf_off)
  215. {
  216. pwrpriv->change_rfpwrstate = rf_off;
  217. pwrpriv->brfoffbyhw = _TRUE;
  218. padapter->bCardDisableWOHSM = _TRUE;
  219. rtw_hw_suspend(padapter );
  220. }
  221. else
  222. {
  223. pwrpriv->change_rfpwrstate = rf_on;
  224. rtw_hw_resume(padapter );
  225. }
  226. DBG_871X("current rf_pwrstate(%s)\n",(pwrpriv->rf_pwrstate == rf_off)?"rf_off":"rf_on");
  227. }
  228. }
  229. pwrpriv->pwr_state_check_cnts ++;
  230. }
  231. #endif //SUPPORT_HW_RFOFF_DETECTED
  232. if (pwrpriv->ips_mode_req == IPS_NONE
  233. #ifdef CONFIG_CONCURRENT_MODE
  234. || padapter->pbuddy_adapter->pwrctrlpriv.ips_mode_req == IPS_NONE
  235. #endif
  236. )
  237. goto exit;
  238. if (rtw_pwr_unassociated_idle(padapter) == _FALSE)
  239. goto exit;
  240. if((pwrpriv->rf_pwrstate == rf_on) && ((pwrpriv->pwr_state_check_cnts%4)==0))
  241. {
  242. DBG_871X("==>%s .fw_state(%x)\n",__FUNCTION__,get_fwstate(pmlmepriv));
  243. #if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
  244. #else
  245. pwrpriv->change_rfpwrstate = rf_off;
  246. #endif
  247. #ifdef CONFIG_AUTOSUSPEND
  248. if(padapter->registrypriv.usbss_enable)
  249. {
  250. if(pwrpriv->bHWPwrPindetect)
  251. pwrpriv->bkeepfwalive = _TRUE;
  252. if(padapter->net_closed == _TRUE)
  253. pwrpriv->ps_flag = _TRUE;
  254. #if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
  255. if (_TRUE==pwrpriv->bInternalAutoSuspend) {
  256. DBG_871X("<==%s .pwrpriv->bInternalAutoSuspend)(%x)\n",__FUNCTION__,pwrpriv->bInternalAutoSuspend);
  257. } else {
  258. pwrpriv->change_rfpwrstate = rf_off;
  259. padapter->bCardDisableWOHSM = _TRUE;
  260. DBG_871X("<==%s .pwrpriv->bInternalAutoSuspend)(%x) call autosuspend_enter\n",__FUNCTION__,pwrpriv->bInternalAutoSuspend);
  261. autosuspend_enter(padapter);
  262. }
  263. #else
  264. padapter->bCardDisableWOHSM = _TRUE;
  265. autosuspend_enter(padapter);
  266. #endif //if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
  267. }
  268. else if(pwrpriv->bHWPwrPindetect)
  269. {
  270. }
  271. else
  272. #endif //CONFIG_AUTOSUSPEND
  273. {
  274. #if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
  275. pwrpriv->change_rfpwrstate = rf_off;
  276. #endif //defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
  277. #ifdef CONFIG_IPS
  278. ips_enter(padapter);
  279. #endif
  280. }
  281. }
  282. exit:
  283. rtw_set_pwr_state_check_timer(&padapter->pwrctrlpriv);
  284. pwrpriv->ps_processing = _FALSE;
  285. return;
  286. }
  287. void pwr_state_check_handler(RTW_TIMER_HDL_ARGS);
  288. void pwr_state_check_handler(RTW_TIMER_HDL_ARGS)
  289. {
  290. _adapter *padapter = (_adapter *)FunctionContext;
  291. rtw_ps_cmd(padapter);
  292. }
  293. #ifdef CONFIG_LPS
  294. /*
  295. *
  296. * Parameters
  297. * padapter
  298. * pslv power state level, only could be PS_STATE_S0 ~ PS_STATE_S4
  299. *
  300. */
  301. void rtw_set_rpwm(PADAPTER padapter, u8 pslv)
  302. {
  303. u8 rpwm;
  304. struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
  305. _func_enter_;
  306. pslv = PS_STATE(pslv);
  307. if (_TRUE == pwrpriv->btcoex_rfon)
  308. {
  309. if (pslv < PS_STATE_S4)
  310. pslv = PS_STATE_S3;
  311. }
  312. #ifdef CONFIG_LPS_RPWM_TIMER
  313. if (pwrpriv->brpwmtimeout == _TRUE)
  314. {
  315. DBG_871X("%s: RPWM timeout, force to set RPWM(0x%02X) again!\n", __FUNCTION__, pslv);
  316. }
  317. else
  318. #endif // CONFIG_LPS_RPWM_TIMER
  319. {
  320. if ( (pwrpriv->rpwm == pslv)
  321. #ifdef CONFIG_LPS_LCLK
  322. || ((pwrpriv->rpwm >= PS_STATE_S2)&&(pslv >= PS_STATE_S2))
  323. #endif
  324. )
  325. {
  326. RT_TRACE(_module_rtl871x_pwrctrl_c_,_drv_err_,
  327. ("%s: Already set rpwm[0x%02X], new=0x%02X!\n", __FUNCTION__, pwrpriv->rpwm, pslv));
  328. return;
  329. }
  330. }
  331. if ((padapter->bSurpriseRemoved == _TRUE) ||
  332. (padapter->hw_init_completed == _FALSE))
  333. {
  334. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
  335. ("%s: SurpriseRemoved(%d) hw_init_completed(%d)\n",
  336. __FUNCTION__, padapter->bSurpriseRemoved, padapter->hw_init_completed));
  337. pwrpriv->cpwm = PS_STATE_S4;
  338. return;
  339. }
  340. if (padapter->bDriverStopped == _TRUE)
  341. {
  342. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
  343. ("%s: change power state(0x%02X) when DriverStopped\n", __FUNCTION__, pslv));
  344. if (pslv < PS_STATE_S2) {
  345. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
  346. ("%s: Reject to enter PS_STATE(0x%02X) lower than S2 when DriverStopped!!\n", __FUNCTION__, pslv));
  347. return;
  348. }
  349. }
  350. rpwm = pslv | pwrpriv->tog;
  351. #ifdef CONFIG_LPS_LCLK
  352. // only when from PS_STATE S0/S1 to S2 and higher needs ACK
  353. if ((pwrpriv->cpwm < PS_STATE_S2) && (pslv >= PS_STATE_S2))
  354. rpwm |= PS_ACK;
  355. #endif
  356. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
  357. ("rtw_set_rpwm: rpwm=0x%02x cpwm=0x%02x\n", rpwm, pwrpriv->cpwm));
  358. pwrpriv->rpwm = pslv;
  359. #ifdef CONFIG_LPS_RPWM_TIMER
  360. if (rpwm & PS_ACK)
  361. _set_timer(&pwrpriv->pwr_rpwm_timer, LPS_RPWM_WAIT_MS);
  362. #endif // CONFIG_LPS_RPWM_TIMER
  363. rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&rpwm));
  364. pwrpriv->tog += 0x80;
  365. #ifdef CONFIG_LPS_LCLK
  366. // No LPS 32K, No Ack
  367. if (!(rpwm & PS_ACK))
  368. #endif
  369. {
  370. pwrpriv->cpwm = pslv;
  371. }
  372. _func_exit_;
  373. }
  374. u8 PS_RDY_CHECK(_adapter * padapter);
  375. u8 PS_RDY_CHECK(_adapter * padapter)
  376. {
  377. u32 curr_time, delta_time;
  378. struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
  379. struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
  380. curr_time = rtw_get_current_time();
  381. delta_time = curr_time -pwrpriv->DelayLPSLastTimeStamp;
  382. if(delta_time < LPS_DELAY_TIME)
  383. {
  384. return _FALSE;
  385. }
  386. if ((check_fwstate(pmlmepriv, _FW_LINKED) == _FALSE) ||
  387. (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY) == _TRUE) ||
  388. (check_fwstate(pmlmepriv, WIFI_AP_STATE) == _TRUE) ||
  389. (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == _TRUE) ||
  390. (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == _TRUE) )
  391. return _FALSE;
  392. #ifdef CONFIG_WOWLAN
  393. if(_TRUE == pwrpriv->bInSuspend && pwrpriv->wowlan_mode)
  394. return _TRUE;
  395. else
  396. return _FALSE;
  397. #else
  398. if(_TRUE == pwrpriv->bInSuspend )
  399. return _FALSE;
  400. #endif
  401. if( (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) && (padapter->securitypriv.binstallGrpkey == _FALSE) )
  402. {
  403. DBG_871X("Group handshake still in progress !!!\n");
  404. return _FALSE;
  405. }
  406. #ifdef CONFIG_IOCTL_CFG80211
  407. if (!rtw_cfg80211_pwr_mgmt(padapter))
  408. return _FALSE;
  409. #endif
  410. return _TRUE;
  411. }
  412. void rtw_set_ps_mode(PADAPTER padapter, u8 ps_mode, u8 smart_ps, u8 bcn_ant_mode)
  413. {
  414. struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
  415. #ifdef CONFIG_P2P
  416. struct wifidirect_info *pwdinfo = &( padapter->wdinfo );
  417. #endif //CONFIG_P2P
  418. #ifdef CONFIG_TDLS
  419. struct sta_priv *pstapriv = &padapter->stapriv;
  420. _irqL irqL;
  421. int i, j;
  422. _list *plist, *phead;
  423. struct sta_info *ptdls_sta;
  424. #endif //CONFIG_TDLS
  425. _func_enter_;
  426. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
  427. ("%s: PowerMode=%d Smart_PS=%d\n",
  428. __FUNCTION__, ps_mode, smart_ps));
  429. if(ps_mode > PM_Card_Disable) {
  430. RT_TRACE(_module_rtl871x_pwrctrl_c_,_drv_err_,("ps_mode:%d error\n", ps_mode));
  431. return;
  432. }
  433. if (pwrpriv->pwr_mode == ps_mode)
  434. {
  435. if (PS_MODE_ACTIVE == ps_mode) return;
  436. if ((pwrpriv->smart_ps == smart_ps) &&
  437. (pwrpriv->bcn_ant_mode == bcn_ant_mode))
  438. {
  439. return;
  440. }
  441. }
  442. #ifdef CONFIG_LPS_LCLK
  443. _enter_pwrlock(&pwrpriv->lock);
  444. #endif
  445. //if(pwrpriv->pwr_mode == PS_MODE_ACTIVE)
  446. if(ps_mode == PS_MODE_ACTIVE)
  447. {
  448. #ifdef CONFIG_P2P_PS
  449. if(pwdinfo->opp_ps == 0)
  450. #endif //CONFIG_P2P_PS
  451. {
  452. DBG_871X("rtw_set_ps_mode: Leave 802.11 power save\n");
  453. #ifdef CONFIG_TDLS
  454. _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
  455. for(i=0; i< NUM_STA; i++)
  456. {
  457. phead = &(pstapriv->sta_hash[i]);
  458. plist = get_next(phead);
  459. while ((rtw_end_of_queue_search(phead, plist)) == _FALSE)
  460. {
  461. ptdls_sta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
  462. if( ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE )
  463. issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta, 0);
  464. plist = get_next(plist);
  465. }
  466. }
  467. _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
  468. #endif //CONFIG_TDLS
  469. pwrpriv->pwr_mode = ps_mode;
  470. rtw_set_rpwm(padapter, PS_STATE_S4);
  471. rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
  472. pwrpriv->bFwCurrentInPSMode = _FALSE;
  473. }
  474. }
  475. else
  476. {
  477. if (PS_RDY_CHECK(padapter)
  478. #ifdef CONFIG_BT_COEXIST
  479. || (BT_1Ant(padapter) == _TRUE)
  480. #endif
  481. )
  482. {
  483. DBG_871X("%s: Enter 802.11 power save\n", __FUNCTION__);
  484. #ifdef CONFIG_TDLS
  485. _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
  486. for(i=0; i< NUM_STA; i++)
  487. {
  488. phead = &(pstapriv->sta_hash[i]);
  489. plist = get_next(phead);
  490. while ((rtw_end_of_queue_search(phead, plist)) == _FALSE)
  491. {
  492. ptdls_sta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
  493. if( ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE )
  494. issue_nulldata_to_TDLS_peer_STA(padapter, ptdls_sta, 1);
  495. plist = get_next(plist);
  496. }
  497. }
  498. _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
  499. #endif //CONFIG_TDLS
  500. pwrpriv->bFwCurrentInPSMode = _TRUE;
  501. pwrpriv->pwr_mode = ps_mode;
  502. pwrpriv->smart_ps = smart_ps;
  503. pwrpriv->bcn_ant_mode = bcn_ant_mode;
  504. rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
  505. #ifdef CONFIG_P2P_PS
  506. // Set CTWindow after LPS
  507. if(pwdinfo->opp_ps == 1)
  508. p2p_ps_wk_cmd(padapter, P2P_PS_ENABLE, 0);
  509. #endif //CONFIG_P2P_PS
  510. #ifdef CONFIG_LPS_LCLK
  511. if (pwrpriv->alives == 0)
  512. rtw_set_rpwm(padapter, PS_STATE_S0);
  513. #else
  514. rtw_set_rpwm(padapter, PS_STATE_S2);
  515. #endif
  516. }
  517. }
  518. #ifdef CONFIG_LPS_LCLK
  519. _exit_pwrlock(&pwrpriv->lock);
  520. #endif
  521. _func_exit_;
  522. }
  523. /*
  524. * Return:
  525. * 0: Leave OK
  526. * -1: Timeout
  527. * -2: Other error
  528. */
  529. s32 LPS_RF_ON_check(PADAPTER padapter, u32 delay_ms)
  530. {
  531. u32 start_time;
  532. u8 bAwake = _FALSE;
  533. s32 err = 0;
  534. start_time = rtw_get_current_time();
  535. while (1)
  536. {
  537. rtw_hal_get_hwreg(padapter, HW_VAR_FWLPS_RF_ON, &bAwake);
  538. if (_TRUE == bAwake)
  539. break;
  540. if (_TRUE == padapter->bSurpriseRemoved)
  541. {
  542. err = -2;
  543. DBG_871X("%s: device surprise removed!!\n", __FUNCTION__);
  544. break;
  545. }
  546. if (rtw_get_passing_time_ms(start_time) > delay_ms)
  547. {
  548. err = -1;
  549. DBG_871X("%s: Wait for FW LPS leave more than %u ms!!!\n", __FUNCTION__, delay_ms);
  550. break;
  551. }
  552. rtw_usleep_os(100);
  553. }
  554. return err;
  555. }
  556. //
  557. // Description:
  558. // Enter the leisure power save mode.
  559. //
  560. void LPS_Enter(PADAPTER padapter)
  561. {
  562. struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
  563. struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
  564. _adapter *buddy = padapter->pbuddy_adapter;
  565. _func_enter_;
  566. // DBG_871X("+LeisurePSEnter\n");
  567. #ifdef CONFIG_CONCURRENT_MODE
  568. if (padapter->iface_type != IFACE_PORT0)
  569. return; /* Skip power saving for concurrent mode port 1*/
  570. /* consider buddy, if exist */
  571. if (buddy) {
  572. struct mlme_priv *b_pmlmepriv = &(buddy->mlmepriv);
  573. #ifdef CONFIG_P2P
  574. struct wifidirect_info *b_pwdinfo = &(buddy->wdinfo);
  575. #ifdef CONFIG_IOCTL_CFG80211
  576. struct cfg80211_wifidirect_info *b_pcfg80211_wdinfo = &buddy->cfg80211_wdinfo;
  577. #endif
  578. #endif
  579. if (check_fwstate(b_pmlmepriv, WIFI_ASOC_STATE|WIFI_SITE_MONITOR)
  580. || check_fwstate(b_pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
  581. || check_fwstate(b_pmlmepriv, WIFI_AP_STATE)
  582. || check_fwstate(b_pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
  583. #if defined(CONFIG_P2P) && defined(CONFIG_IOCTL_CFG80211) && defined(CONFIG_P2P_IPS)
  584. || b_pcfg80211_wdinfo->is_ro_ch
  585. #elif defined(CONFIG_P2P)
  586. || !rtw_p2p_chk_state(b_pwdinfo, P2P_STATE_NONE)
  587. #endif
  588. || rtw_is_scan_deny(buddy)
  589. ) {
  590. return;
  591. }
  592. }
  593. #endif
  594. if (PS_RDY_CHECK(padapter) == _FALSE)
  595. return;
  596. if (pwrpriv->bLeisurePs)
  597. {
  598. // Idle for a while if we connect to AP a while ago.
  599. if(pwrpriv->LpsIdleCount >= 2) // 4 Sec
  600. {
  601. if(pwrpriv->pwr_mode == PS_MODE_ACTIVE)
  602. {
  603. pwrpriv->bpower_saving = _TRUE;
  604. rtw_set_ps_mode(padapter, pwrpriv->power_mgnt, padapter->registrypriv.smart_ps, 0);
  605. }
  606. }
  607. else
  608. pwrpriv->LpsIdleCount++;
  609. }
  610. // DBG_871X("-LeisurePSEnter\n");
  611. _func_exit_;
  612. }
  613. //
  614. // Description:
  615. // Leave the leisure power save mode.
  616. //
  617. void LPS_Leave(PADAPTER padapter)
  618. {
  619. #define LPS_LEAVE_TIMEOUT_MS 100
  620. struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
  621. u32 start_time;
  622. u8 bAwake = _FALSE;
  623. _func_enter_;
  624. #ifdef CONFIG_CONCURRENT_MODE
  625. if (padapter->iface_type != IFACE_PORT0)
  626. return; /* Skip power saving for concurrent mode port 1*/
  627. #endif
  628. // DBG_871X("+LeisurePSLeave\n");
  629. if (pwrpriv->bLeisurePs)
  630. {
  631. if(pwrpriv->pwr_mode != PS_MODE_ACTIVE)
  632. {
  633. rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0);
  634. if(pwrpriv->pwr_mode == PS_MODE_ACTIVE)
  635. LPS_RF_ON_check(padapter, LPS_LEAVE_TIMEOUT_MS);
  636. }
  637. }
  638. pwrpriv->bpower_saving = _FALSE;
  639. // DBG_871X("-LeisurePSLeave\n");
  640. _func_exit_;
  641. }
  642. #endif
  643. //
  644. // Description: Leave all power save mode: LPS, FwLPS, IPS if needed.
  645. // Move code to function by tynli. 2010.03.26.
  646. //
  647. void LeaveAllPowerSaveMode(IN PADAPTER Adapter)
  648. {
  649. struct mlme_priv *pmlmepriv = &(Adapter->mlmepriv);
  650. u8 enqueue = 0;
  651. _func_enter_;
  652. //DBG_871X("%s.....\n",__FUNCTION__);
  653. if (check_fwstate(pmlmepriv, _FW_LINKED) == _TRUE)
  654. { //connect
  655. #ifdef CONFIG_LPS_LCLK
  656. enqueue = 1;
  657. #endif
  658. #ifdef CONFIG_P2P_PS
  659. p2p_ps_wk_cmd(Adapter, P2P_PS_DISABLE, enqueue);
  660. #endif //CONFIG_P2P_PS
  661. #ifdef CONFIG_LPS
  662. rtw_lps_ctrl_wk_cmd(Adapter, LPS_CTRL_LEAVE, enqueue);
  663. #endif
  664. #ifdef CONFIG_LPS_LCLK
  665. LPS_Leave_check(Adapter);
  666. #endif
  667. }
  668. else
  669. {
  670. if(Adapter->pwrctrlpriv.rf_pwrstate== rf_off)
  671. {
  672. #ifdef CONFIG_AUTOSUSPEND
  673. if(Adapter->registrypriv.usbss_enable)
  674. {
  675. #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
  676. usb_disable_autosuspend(adapter_to_dvobj(Adapter)->pusbdev);
  677. #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,22) && LINUX_VERSION_CODE<=KERNEL_VERSION(2,6,34))
  678. adapter_to_dvobj(Adapter)->pusbdev->autosuspend_disabled = Adapter->bDisableAutosuspend;//autosuspend disabled by the user
  679. #endif
  680. }
  681. else
  682. #endif
  683. {
  684. #if defined(CONFIG_PLATFORM_SPRD) && defined(CONFIG_RTL8188E)
  685. #ifdef CONFIG_IPS
  686. if(_FALSE == ips_leave(Adapter))
  687. {
  688. DBG_871X("======> ips_leave fail.............\n");
  689. }
  690. #endif
  691. #endif //CONFIG_PLATFORM_SPRD && CONFIG_RTL8188E
  692. }
  693. }
  694. }
  695. _func_exit_;
  696. }
  697. #ifdef CONFIG_LPS_LCLK
  698. void LPS_Leave_check(
  699. PADAPTER padapter)
  700. {
  701. struct pwrctrl_priv *pwrpriv;
  702. u32 start_time;
  703. u8 bReady;
  704. _func_enter_;
  705. pwrpriv = &padapter->pwrctrlpriv;
  706. bReady = _FALSE;
  707. start_time = rtw_get_current_time();
  708. rtw_yield_os();
  709. while(1)
  710. {
  711. _enter_pwrlock(&pwrpriv->lock);
  712. if ((padapter->bSurpriseRemoved == _TRUE)
  713. || (padapter->hw_init_completed == _FALSE)
  714. #ifdef CONFIG_USB_HCI
  715. || (padapter->bDriverStopped== _TRUE)
  716. #endif
  717. || (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
  718. )
  719. {
  720. bReady = _TRUE;
  721. }
  722. _exit_pwrlock(&pwrpriv->lock);
  723. if(_TRUE == bReady)
  724. break;
  725. if(rtw_get_passing_time_ms(start_time)>100)
  726. {
  727. DBG_871X("Wait for cpwm event than 100 ms!!!\n");
  728. break;
  729. }
  730. rtw_msleep_os(1);
  731. }
  732. _func_exit_;
  733. }
  734. /*
  735. * Caller:ISR handler...
  736. *
  737. * This will be called when CPWM interrupt is up.
  738. *
  739. * using to update cpwn of drv; and drv willl make a decision to up or down pwr level
  740. */
  741. void cpwm_int_hdl(
  742. PADAPTER padapter,
  743. struct reportpwrstate_parm *preportpwrstate)
  744. {
  745. struct pwrctrl_priv *pwrpriv;
  746. _func_enter_;
  747. pwrpriv = &padapter->pwrctrlpriv;
  748. #if 0
  749. if (pwrpriv->cpwm_tog == (preportpwrstate->state & PS_TOGGLE)) {
  750. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_err_,
  751. ("cpwm_int_hdl: tog(old)=0x%02x cpwm(new)=0x%02x toggle bit didn't change!?\n",
  752. pwrpriv->cpwm_tog, preportpwrstate->state));
  753. goto exit;
  754. }
  755. #endif
  756. _enter_pwrlock(&pwrpriv->lock);
  757. #ifdef CONFIG_LPS_RPWM_TIMER
  758. if (pwrpriv->rpwm < PS_STATE_S2)
  759. {
  760. DBG_871X("%s: Redundant CPWM Int. RPWM=0x%02X CPWM=0x%02x\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
  761. _exit_pwrlock(&pwrpriv->lock);
  762. goto exit;
  763. }
  764. #endif // CONFIG_LPS_RPWM_TIMER
  765. pwrpriv->cpwm = PS_STATE(preportpwrstate->state);
  766. pwrpriv->cpwm_tog = preportpwrstate->state & PS_TOGGLE;
  767. if (pwrpriv->cpwm >= PS_STATE_S2)
  768. {
  769. if (pwrpriv->alives & CMD_ALIVE)
  770. _rtw_up_sema(&padapter->cmdpriv.cmd_queue_sema);
  771. if (pwrpriv->alives & XMIT_ALIVE)
  772. _rtw_up_sema(&padapter->xmitpriv.xmit_sema);
  773. }
  774. _exit_pwrlock(&pwrpriv->lock);
  775. exit:
  776. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
  777. ("cpwm_int_hdl: cpwm=0x%02x\n", pwrpriv->cpwm));
  778. _func_exit_;
  779. }
  780. static void cpwm_event_callback(struct work_struct *work)
  781. {
  782. struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, cpwm_event);
  783. _adapter *adapter = container_of(pwrpriv, _adapter, pwrctrlpriv);
  784. struct reportpwrstate_parm report;
  785. //DBG_871X("%s\n",__FUNCTION__);
  786. report.state = PS_STATE_S2;
  787. cpwm_int_hdl(adapter, &report);
  788. }
  789. #ifdef CONFIG_LPS_RPWM_TIMER
  790. static void rpwmtimeout_workitem_callback(struct work_struct *work)
  791. {
  792. PADAPTER padapter;
  793. struct pwrctrl_priv *pwrpriv;
  794. pwrpriv = container_of(work, struct pwrctrl_priv, rpwmtimeoutwi);
  795. padapter = container_of(pwrpriv, _adapter, pwrctrlpriv);
  796. // DBG_871X("+%s: rpwm=0x%02X cpwm=0x%02X\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
  797. _enter_pwrlock(&pwrpriv->lock);
  798. if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
  799. {
  800. DBG_871X("%s: rpwm=0x%02X cpwm=0x%02X CPWM done!\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
  801. goto exit;
  802. }
  803. _exit_pwrlock(&pwrpriv->lock);
  804. if (rtw_read8(padapter, 0x100) != 0xEA)
  805. {
  806. #if 1
  807. struct reportpwrstate_parm report;
  808. report.state = PS_STATE_S2;
  809. DBG_871X("\n%s: FW already leave 32K!\n\n", __func__);
  810. cpwm_int_hdl(padapter, &report);
  811. #else
  812. DBG_871X("\n%s: FW already leave 32K!\n\n", __func__);
  813. cpwm_event_callback(&pwrpriv->cpwm_event);
  814. #endif
  815. return;
  816. }
  817. _enter_pwrlock(&pwrpriv->lock);
  818. if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
  819. {
  820. DBG_871X("%s: cpwm=%d, nothing to do!\n", __func__, pwrpriv->cpwm);
  821. goto exit;
  822. }
  823. pwrpriv->brpwmtimeout = _TRUE;
  824. rtw_set_rpwm(padapter, pwrpriv->rpwm);
  825. pwrpriv->brpwmtimeout = _FALSE;
  826. exit:
  827. _exit_pwrlock(&pwrpriv->lock);
  828. }
  829. /*
  830. * This function is a timer handler, can't do any IO in it.
  831. */
  832. static void pwr_rpwm_timeout_handler(void *FunctionContext)
  833. {
  834. PADAPTER padapter;
  835. struct pwrctrl_priv *pwrpriv;
  836. padapter = (PADAPTER)FunctionContext;
  837. pwrpriv = &padapter->pwrctrlpriv;
  838. // DBG_871X("+%s: rpwm=0x%02X cpwm=0x%02X\n", __func__, pwrpriv->rpwm, pwrpriv->cpwm);
  839. if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
  840. {
  841. DBG_871X("+%s: cpwm=%d, nothing to do!\n", __func__, pwrpriv->cpwm);
  842. return;
  843. }
  844. _set_workitem(&pwrpriv->rpwmtimeoutwi);
  845. }
  846. #endif // CONFIG_LPS_RPWM_TIMER
  847. __inline static void register_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
  848. {
  849. pwrctrl->alives |= tag;
  850. }
  851. __inline static void unregister_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
  852. {
  853. pwrctrl->alives &= ~tag;
  854. }
  855. /*
  856. * Caller: rtw_xmit_thread
  857. *
  858. * Check if the fw_pwrstate is okay for xmit.
  859. * If not (cpwm is less than S3), then the sub-routine
  860. * will raise the cpwm to be greater than or equal to S3.
  861. *
  862. * Calling Context: Passive
  863. *
  864. * Return Value:
  865. * _SUCCESS rtw_xmit_thread can write fifo/txcmd afterwards.
  866. * _FAIL rtw_xmit_thread can not do anything.
  867. */
  868. s32 rtw_register_tx_alive(PADAPTER padapter)
  869. {
  870. s32 res;
  871. struct pwrctrl_priv *pwrctrl;
  872. u8 pslv;
  873. _func_enter_;
  874. res = _SUCCESS;
  875. pwrctrl = &padapter->pwrctrlpriv;
  876. #ifdef CONFIG_BT_COEXIST
  877. if (_TRUE == pwrctrl->btcoex_rfon)
  878. pslv = PS_STATE_S3;
  879. else
  880. #endif
  881. {
  882. pslv = PS_STATE_S2;
  883. }
  884. _enter_pwrlock(&pwrctrl->lock);
  885. register_task_alive(pwrctrl, XMIT_ALIVE);
  886. if (pwrctrl->bFwCurrentInPSMode == _TRUE)
  887. {
  888. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
  889. ("rtw_register_tx_alive: cpwm=0x%02x alives=0x%08x\n",
  890. pwrctrl->cpwm, pwrctrl->alives));
  891. if (pwrctrl->cpwm < pslv)
  892. {
  893. if (pwrctrl->cpwm < PS_STATE_S2)
  894. res = _FAIL;
  895. if (pwrctrl->rpwm < pslv)
  896. rtw_set_rpwm(padapter, pslv);
  897. }
  898. }
  899. _exit_pwrlock(&pwrctrl->lock);
  900. _func_exit_;
  901. return res;
  902. }
  903. /*
  904. * Caller: rtw_cmd_thread
  905. *
  906. * Check if the fw_pwrstate is okay for issuing cmd.
  907. * If not (cpwm should be is less than S2), then the sub-routine
  908. * will raise the cpwm to be greater than or equal to S2.
  909. *
  910. * Calling Context: Passive
  911. *
  912. * Return Value:
  913. * _SUCCESS rtw_cmd_thread can issue cmds to firmware afterwards.
  914. * _FAIL rtw_cmd_thread can not do anything.
  915. */
  916. s32 rtw_register_cmd_alive(PADAPTER padapter)
  917. {
  918. s32 res;
  919. struct pwrctrl_priv *pwrctrl;
  920. u8 pslv;
  921. _func_enter_;
  922. res = _SUCCESS;
  923. pwrctrl = &padapter->pwrctrlpriv;
  924. #ifdef CONFIG_BT_COEXIST
  925. if (_TRUE == pwrctrl->btcoex_rfon)
  926. pslv = PS_STATE_S3;
  927. else
  928. #endif
  929. {
  930. pslv = PS_STATE_S2;
  931. }
  932. _enter_pwrlock(&pwrctrl->lock);
  933. register_task_alive(pwrctrl, CMD_ALIVE);
  934. if (pwrctrl->bFwCurrentInPSMode == _TRUE)
  935. {
  936. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_info_,
  937. ("rtw_register_cmd_alive: cpwm=0x%02x alives=0x%08x\n",
  938. pwrctrl->cpwm, pwrctrl->alives));
  939. if (pwrctrl->cpwm < pslv)
  940. {
  941. if (pwrctrl->cpwm < PS_STATE_S2)
  942. res = _FAIL;
  943. if (pwrctrl->rpwm < pslv)
  944. rtw_set_rpwm(padapter, pslv);
  945. }
  946. }
  947. _exit_pwrlock(&pwrctrl->lock);
  948. _func_exit_;
  949. return res;
  950. }
  951. /*
  952. * Caller: rx_isr
  953. *
  954. * Calling Context: Dispatch/ISR
  955. *
  956. * Return Value:
  957. * _SUCCESS
  958. * _FAIL
  959. */
  960. s32 rtw_register_rx_alive(PADAPTER padapter)
  961. {
  962. struct pwrctrl_priv *pwrctrl;
  963. _func_enter_;
  964. pwrctrl = &padapter->pwrctrlpriv;
  965. _enter_pwrlock(&pwrctrl->lock);
  966. register_task_alive(pwrctrl, RECV_ALIVE);
  967. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
  968. ("rtw_register_rx_alive: cpwm=0x%02x alives=0x%08x\n",
  969. pwrctrl->cpwm, pwrctrl->alives));
  970. _exit_pwrlock(&pwrctrl->lock);
  971. _func_exit_;
  972. return _SUCCESS;
  973. }
  974. /*
  975. * Caller: evt_isr or evt_thread
  976. *
  977. * Calling Context: Dispatch/ISR or Passive
  978. *
  979. * Return Value:
  980. * _SUCCESS
  981. * _FAIL
  982. */
  983. s32 rtw_register_evt_alive(PADAPTER padapter)
  984. {
  985. struct pwrctrl_priv *pwrctrl;
  986. _func_enter_;
  987. pwrctrl = &padapter->pwrctrlpriv;
  988. _enter_pwrlock(&pwrctrl->lock);
  989. register_task_alive(pwrctrl, EVT_ALIVE);
  990. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
  991. ("rtw_register_evt_alive: cpwm=0x%02x alives=0x%08x\n",
  992. pwrctrl->cpwm, pwrctrl->alives));
  993. _exit_pwrlock(&pwrctrl->lock);
  994. _func_exit_;
  995. return _SUCCESS;
  996. }
  997. /*
  998. * Caller: ISR
  999. *
  1000. * If ISR's txdone,
  1001. * No more pkts for TX,
  1002. * Then driver shall call this fun. to power down firmware again.
  1003. */
  1004. void rtw_unregister_tx_alive(PADAPTER padapter)
  1005. {
  1006. struct pwrctrl_priv *pwrctrl;
  1007. _func_enter_;
  1008. pwrctrl = &padapter->pwrctrlpriv;
  1009. _enter_pwrlock(&pwrctrl->lock);
  1010. unregister_task_alive(pwrctrl, XMIT_ALIVE);
  1011. if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE) &&
  1012. (pwrctrl->bFwCurrentInPSMode == _TRUE))
  1013. {
  1014. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
  1015. ("%s: cpwm=0x%02x alives=0x%08x\n",
  1016. __FUNCTION__, pwrctrl->cpwm, pwrctrl->alives));
  1017. if ((pwrctrl->alives == 0) &&
  1018. (pwrctrl->cpwm > PS_STATE_S0))
  1019. {
  1020. rtw_set_rpwm(padapter, PS_STATE_S0);
  1021. }
  1022. }
  1023. _exit_pwrlock(&pwrctrl->lock);
  1024. _func_exit_;
  1025. }
  1026. /*
  1027. * Caller: ISR
  1028. *
  1029. * If all commands have been done,
  1030. * and no more command to do,
  1031. * then driver shall call this fun. to power down firmware again.
  1032. */
  1033. void rtw_unregister_cmd_alive(PADAPTER padapter)
  1034. {
  1035. struct pwrctrl_priv *pwrctrl;
  1036. _func_enter_;
  1037. pwrctrl = &padapter->pwrctrlpriv;
  1038. _enter_pwrlock(&pwrctrl->lock);
  1039. unregister_task_alive(pwrctrl, CMD_ALIVE);
  1040. if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE) &&
  1041. (pwrctrl->bFwCurrentInPSMode == _TRUE))
  1042. {
  1043. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_info_,
  1044. ("%s: cpwm=0x%02x alives=0x%08x\n",
  1045. __FUNCTION__, pwrctrl->cpwm, pwrctrl->alives));
  1046. if ((pwrctrl->alives == 0) &&
  1047. (pwrctrl->cpwm > PS_STATE_S0))
  1048. {
  1049. rtw_set_rpwm(padapter, PS_STATE_S0);
  1050. }
  1051. }
  1052. _exit_pwrlock(&pwrctrl->lock);
  1053. _func_exit_;
  1054. }
  1055. /*
  1056. * Caller: ISR
  1057. */
  1058. void rtw_unregister_rx_alive(PADAPTER padapter)
  1059. {
  1060. struct pwrctrl_priv *pwrctrl;
  1061. _func_enter_;
  1062. pwrctrl = &padapter->pwrctrlpriv;
  1063. _enter_pwrlock(&pwrctrl->lock);
  1064. unregister_task_alive(pwrctrl, RECV_ALIVE);
  1065. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
  1066. ("rtw_unregister_rx_alive: cpwm=0x%02x alives=0x%08x\n",
  1067. pwrctrl->cpwm, pwrctrl->alives));
  1068. _exit_pwrlock(&pwrctrl->lock);
  1069. _func_exit_;
  1070. }
  1071. void rtw_unregister_evt_alive(PADAPTER padapter)
  1072. {
  1073. struct pwrctrl_priv *pwrctrl;
  1074. _func_enter_;
  1075. pwrctrl = &padapter->pwrctrlpriv;
  1076. unregister_task_alive(pwrctrl, EVT_ALIVE);
  1077. RT_TRACE(_module_rtl871x_pwrctrl_c_, _drv_notice_,
  1078. ("rtw_unregister_evt_alive: cpwm=0x%02x alives=0x%08x\n",
  1079. pwrctrl->cpwm, pwrctrl->alives));
  1080. _exit_pwrlock(&pwrctrl->lock);
  1081. _func_exit_;
  1082. }
  1083. #endif /* CONFIG_LPS_LCLK */
  1084. #ifdef CONFIG_RESUME_IN_WORKQUEUE
  1085. static void resume_workitem_callback(struct work_struct *work);
  1086. #endif //CONFIG_RESUME_IN_WORKQUEUE
  1087. void rtw_init_pwrctrl_priv(PADAPTER padapter)
  1088. {
  1089. struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
  1090. _func_enter_;
  1091. #ifdef PLATFORM_WINDOWS
  1092. pwrctrlpriv->pnp_current_pwr_state=NdisDeviceStateD0;
  1093. #endif
  1094. _init_pwrlock(&pwrctrlpriv->lock);
  1095. pwrctrlpriv->rf_pwrstate = rf_on;
  1096. pwrctrlpriv->ips_enter_cnts=0;
  1097. pwrctrlpriv->ips_leave_cnts=0;
  1098. pwrctrlpriv->bips_processing = _FALSE;
  1099. pwrctrlpriv->ips_mode = padapter->registrypriv.ips_mode;
  1100. pwrctrlpriv->ips_mode_req = padapter->registrypriv.ips_mode;
  1101. pwrctrlpriv->pwr_state_check_interval = RTW_PWR_STATE_CHK_INTERVAL;
  1102. pwrctrlpriv->pwr_state_check_cnts = 0;
  1103. pwrctrlpriv->bInternalAutoSuspend = _FALSE;
  1104. pwrctrlpriv->bInSuspend = _FALSE;
  1105. pwrctrlpriv->bkeepfwalive = _FALSE;
  1106. #ifdef CONFIG_AUTOSUSPEND
  1107. #ifdef SUPPORT_HW_RFOFF_DETECTED
  1108. pwrctrlpriv->pwr_state_check_interval = (pwrctrlpriv->bHWPwrPindetect) ?1000:2000;
  1109. #endif
  1110. #endif
  1111. pwrctrlpriv->LpsIdleCount = 0;
  1112. //pwrctrlpriv->FWCtrlPSMode =padapter->registrypriv.power_mgnt;// PS_MODE_MIN;
  1113. if (padapter->registrypriv.mp_mode == 1)
  1114. pwrctrlpriv->power_mgnt =PS_MODE_ACTIVE ;
  1115. else
  1116. pwrctrlpriv->power_mgnt =padapter->registrypriv.power_mgnt;// PS_MODE_MIN;
  1117. pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt)?_TRUE:_FALSE;
  1118. pwrctrlpriv->bFwCurrentInPSMode = _FALSE;
  1119. pwrctrlpriv->rpwm = 0;
  1120. pwrctrlpriv->cpwm = PS_STATE_S4;
  1121. pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
  1122. pwrctrlpriv->smart_ps = padapter->registrypriv.smart_ps;
  1123. pwrctrlpriv->bcn_ant_mode = 0;
  1124. pwrctrlpriv->tog = 0x80;
  1125. pwrctrlpriv->btcoex_rfon = _FALSE;
  1126. #ifdef CONFIG_LPS_LCLK
  1127. rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&pwrctrlpriv->rpwm));
  1128. _init_workitem(&pwrctrlpriv->cpwm_event, cpwm_event_callback, NULL);
  1129. #ifdef CONFIG_LPS_RPWM_TIMER
  1130. pwrctrlpriv->brpwmtimeout = _FALSE;
  1131. _init_workitem(&pwrctrlpriv->rpwmtimeoutwi, rpwmtimeout_workitem_callback, NULL);
  1132. _init_timer(&pwrctrlpriv->pwr_rpwm_timer, padapter->pnetdev, pwr_rpwm_timeout_handler, padapter);
  1133. #endif // CONFIG_LPS_RPWM_TIMER
  1134. #endif // CONFIG_LPS_LCLK
  1135. rtw_init_timer(&pwrctrlpriv->pwr_state_check_timer, padapter, pwr_state_check_handler);
  1136. #ifdef CONFIG_RESUME_IN_WORKQUEUE
  1137. _init_workitem(&pwrctrlpriv->resume_work, resume_workitem_callback, NULL);
  1138. pwrctrlpriv->rtw_workqueue = create_singlethread_workqueue("rtw_workqueue");
  1139. #endif //CONFIG_RESUME_IN_WORKQUEUE
  1140. #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
  1141. pwrctrlpriv->early_suspend.suspend = NULL;
  1142. rtw_register_early_suspend(pwrctrlpriv);
  1143. #endif //CONFIG_HAS_EARLYSUSPEND || CONFIG_ANDROID_POWER
  1144. _func_exit_;
  1145. }
  1146. void rtw_free_pwrctrl_priv(PADAPTER adapter)
  1147. {
  1148. struct pwrctrl_priv *pwrctrlpriv = &adapter->pwrctrlpriv;
  1149. _func_enter_;
  1150. //_rtw_memset((unsigned char *)pwrctrlpriv, 0, sizeof(struct pwrctrl_priv));
  1151. #ifdef CONFIG_RESUME_IN_WORKQUEUE
  1152. if (pwrctrlpriv->rtw_workqueue) {
  1153. flush_workqueue(pwrctrlpriv->rtw_workqueue);
  1154. destroy_workqueue(pwrctrlpriv->rtw_workqueue);
  1155. }
  1156. #endif
  1157. #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
  1158. rtw_unregister_early_suspend(pwrctrlpriv);
  1159. #endif //CONFIG_HAS_EARLYSUSPEND || CONFIG_ANDROID_POWER
  1160. _free_pwrlock(&pwrctrlpriv->lock);
  1161. _func_exit_;
  1162. }
  1163. #ifdef CONFIG_RESUME_IN_WORKQUEUE
  1164. #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
  1165. extern int rtw_resume_process(_adapter *padapter);
  1166. #endif
  1167. static void resume_workitem_callback(struct work_struct *work)
  1168. {
  1169. struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, resume_work);
  1170. _adapter *adapter = container_of(pwrpriv, _adapter, pwrctrlpriv);
  1171. DBG_871X("%s\n",__FUNCTION__);
  1172. #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
  1173. rtw_resume_process(adapter);
  1174. #endif
  1175. }
  1176. void rtw_resume_in_workqueue(struct pwrctrl_priv *pwrpriv)
  1177. {
  1178. // accquire system's suspend lock preventing from falliing asleep while resume in workqueue
  1179. rtw_lock_suspend();
  1180. #if 1
  1181. queue_work(pwrpriv->rtw_workqueue, &pwrpriv->resume_work);
  1182. #else
  1183. _set_workitem(&pwrpriv->resume_work);
  1184. #endif
  1185. }
  1186. #endif //CONFIG_RESUME_IN_WORKQUEUE
  1187. #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
  1188. inline bool rtw_is_earlysuspend_registered(struct pwrctrl_priv *pwrpriv)
  1189. {
  1190. return (pwrpriv->early_suspend.suspend) ? _TRUE : _FALSE;
  1191. }
  1192. inline bool rtw_is_do_late_resume(struct pwrctrl_priv *pwrpriv)
  1193. {
  1194. return (pwrpriv->do_late_resume) ? _TRUE : _FALSE;
  1195. }
  1196. inline void rtw_set_do_late_resume(struct pwrctrl_priv *pwrpriv, bool enable)
  1197. {
  1198. pwrpriv->do_late_resume = enable;
  1199. }
  1200. #endif
  1201. #ifdef CONFIG_HAS_EARLYSUSPEND
  1202. #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
  1203. extern int rtw_resume_process(_adapter *padapter);
  1204. #endif
  1205. static void rtw_early_suspend(struct early_suspend *h)
  1206. {
  1207. struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
  1208. DBG_871X("%s\n",__FUNCTION__);
  1209. rtw_set_do_late_resume(pwrpriv, _FALSE);
  1210. }
  1211. static void rtw_late_resume(struct early_suspend *h)
  1212. {
  1213. struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
  1214. _adapter *adapter = container_of(pwrpriv, _adapter, pwrctrlpriv);
  1215. DBG_871X("%s\n",__FUNCTION__);
  1216. if(pwrpriv->do_late_resume) {
  1217. #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
  1218. rtw_set_do_late_resume(pwrpriv, _FALSE);
  1219. rtw_resume_process(adapter);
  1220. #endif
  1221. }
  1222. }
  1223. void rtw_register_early_suspend(struct pwrctrl_priv *pwrpriv)
  1224. {
  1225. _adapter *adapter = container_of(pwrpriv, _adapter, pwrctrlpriv);
  1226. #if defined(CONFIG_CONCURRENT_MODE)
  1227. if (adapter->adapter_type != PRIMARY_ADAPTER)
  1228. return;
  1229. #endif
  1230. DBG_871X("%s\n", __FUNCTION__);
  1231. //jeff: set the early suspend level before blank screen, so we wll do late resume after scree is lit
  1232. pwrpriv->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
  1233. pwrpriv->early_suspend.suspend = rtw_early_suspend;
  1234. pwrpriv->early_suspend.resume = rtw_late_resume;
  1235. register_early_suspend(&pwrpriv->early_suspend);
  1236. }
  1237. void rtw_unregister_early_suspend(struct pwrctrl_priv *pwrpriv)
  1238. {
  1239. _adapter *adapter = container_of(pwrpriv, _adapter, pwrctrlpriv);
  1240. #if defined(CONFIG_CONCURRENT_MODE)
  1241. if (adapter->adapter_type != PRIMARY_ADAPTER)
  1242. return;
  1243. #endif
  1244. DBG_871X("%s\n", __FUNCTION__);
  1245. rtw_set_do_late_resume(pwrpriv, _FALSE);
  1246. if (pwrpriv->early_suspend.suspend)
  1247. unregister_early_suspend(&pwrpriv->early_suspend);
  1248. pwrpriv->early_suspend.suspend = NULL;
  1249. pwrpriv->early_suspend.resume = NULL;
  1250. }
  1251. #endif //CONFIG_HAS_EARLYSUSPEND
  1252. #ifdef CONFIG_ANDROID_POWER
  1253. #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
  1254. extern int rtw_resume_process(PADAPTER padapter);
  1255. #endif
  1256. static void rtw_early_suspend(android_early_suspend_t *h)
  1257. {
  1258. struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
  1259. DBG_871X("%s\n",__FUNCTION__);
  1260. rtw_set_do_late_resume(pwrpriv, _FALSE);
  1261. }
  1262. static void rtw_late_resume(android_early_suspend_t *h)
  1263. {
  1264. struct pwrctrl_priv *pwrpriv = container_of(h, struct pwrctrl_priv, early_suspend);
  1265. _adapter *adapter = container_of(pwrpriv, _adapter, pwrctrlpriv);
  1266. DBG_871X("%s\n",__FUNCTION__);
  1267. if(pwrpriv->do_late_resume) {
  1268. #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
  1269. rtw_set_do_late_resume(pwrpriv, _FALSE);
  1270. rtw_resume_process(adapter);
  1271. #endif
  1272. }
  1273. }
  1274. void rtw_register_early_suspend(struct pwrctrl_priv *pwrpriv)
  1275. {
  1276. _adapter *adapter = container_of(pwrpriv, _adapter, pwrctrlpriv);
  1277. #if defined(CONFIG_CONCURRENT_MODE)
  1278. if (adapter->adapter_type != PRIMARY_ADAPTER)
  1279. return;
  1280. #endif
  1281. DBG_871X("%s\n", __FUNCTION__);
  1282. //jeff: set the early suspend level before blank screen, so we wll do late resume after scree is lit
  1283. pwrpriv->early_suspend.level = ANDROID_EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 20;
  1284. pwrpriv->early_suspend.suspend = rtw_early_suspend;
  1285. pwrpriv->early_suspend.resume = rtw_late_resume;
  1286. android_register_early_suspend(&pwrpriv->early_suspend);
  1287. }
  1288. void rtw_unregister_early_suspend(struct pwrctrl_priv *pwrpriv)
  1289. {
  1290. _adapter *adapter = container_of(pwrpriv, _adapter, pwrctrlpriv);
  1291. #if defined(CONFIG_CONCURRENT_MODE)
  1292. if (adapter->adapter_type != PRIMARY_ADAPTER)
  1293. return;
  1294. #endif
  1295. DBG_871X("%s\n", __FUNCTION__);
  1296. rtw_set_do_late_resume(pwrpriv, _FALSE);
  1297. if (pwrpriv->early_suspend.suspend)
  1298. android_unregister_early_suspend(&pwrpriv->early_suspend);
  1299. pwrpriv->early_suspend.suspend = NULL;
  1300. pwrpriv->early_suspend.resume = NULL;
  1301. }
  1302. #endif //CONFIG_ANDROID_POWER
  1303. u8 rtw_interface_ps_func(_adapter *padapter,HAL_INTF_PS_FUNC efunc_id,u8* val)
  1304. {
  1305. u8 bResult = _TRUE;
  1306. rtw_hal_intf_ps_func(padapter,efunc_id,val);
  1307. return bResult;
  1308. }
  1309. inline void rtw_set_ips_deny(_adapter *padapter, u32 ms)
  1310. {
  1311. struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
  1312. pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ms);
  1313. }
  1314. /*
  1315. * rtw_pwr_wakeup - Wake the NIC up from: 1)IPS. 2)USB autosuspend
  1316. * @adapter: pointer to _adapter structure
  1317. * @ips_deffer_ms: the ms wiil prevent from falling into IPS after wakeup
  1318. * Return _SUCCESS or _FAIL
  1319. */
  1320. int _rtw_pwr_wakeup(_adapter *padapter, u32 ips_deffer_ms, const char *caller)
  1321. {
  1322. struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
  1323. struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
  1324. int ret = _SUCCESS;
  1325. u32 start = rtw_get_current_time();
  1326. #ifdef CONFIG_CONCURRENT_MODE
  1327. if (padapter->pbuddy_adapter)
  1328. LeaveAllPowerSaveMode(padapter->pbuddy_adapter);
  1329. if ((padapter->isprimary == _FALSE) && padapter->pbuddy_adapter){
  1330. padapter = padapter->pbuddy_adapter;
  1331. pwrpriv = &padapter->pwrctrlpriv;
  1332. pmlmepriv = &padapter->mlmepriv;
  1333. }
  1334. #endif
  1335. if (pwrpriv->ips_deny_time < rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms))
  1336. pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms);
  1337. if (pwrpriv->ps_processing) {
  1338. DBG_871X("%s wait ps_processing...\n", __func__);
  1339. while (pwrpriv->ps_processing && rtw_get_passing_time_ms(start) <= 3000)
  1340. rtw_msleep_os(10);
  1341. if (pwrpriv->ps_processing)
  1342. DBG_871X("%s wait ps_processing timeout\n", __func__);
  1343. else
  1344. DBG_871X("%s wait ps_processing done\n", __func__);
  1345. }
  1346. #ifdef DBG_CONFIG_ERROR_DETECT
  1347. if (rtw_hal_sreset_inprogress(padapter)) {
  1348. DBG_871X("%s wait sreset_inprogress...\n", __func__);
  1349. while (rtw_hal_sreset_inprogress(padapter) && rtw_get_passing_time_ms(start) <= 4000)
  1350. rtw_msleep_os(10);
  1351. if (rtw_hal_sreset_inprogress(padapter))
  1352. DBG_871X("%s wait sreset_inprogress timeout\n", __func__);
  1353. else
  1354. DBG_871X("%s wait sreset_inprogress done\n", __func__);
  1355. }
  1356. #endif
  1357. if (pwrpriv->bInternalAutoSuspend == _FALSE && pwrpriv->bInSuspend) {
  1358. DBG_871X("%s wait bInSuspend...\n", __func__);
  1359. while (pwrpriv->bInSuspend
  1360. && ((rtw_get_passing_time_ms(start) <= 3000 && !rtw_is_do_late_resume(pwrpriv))
  1361. || (rtw_get_passing_time_ms(start) <= 500 && rtw_is_do_late_resume(pwrpriv)))
  1362. ) {
  1363. rtw_msleep_os(10);
  1364. }
  1365. if (pwrpriv->bInSuspend)
  1366. DBG_871X("%s wait bInSuspend timeout\n", __func__);
  1367. else
  1368. DBG_871X("%s wait bInSuspend done\n", __func__);
  1369. }
  1370. //System suspend is not allowed to wakeup
  1371. if((pwrpriv->bInternalAutoSuspend == _FALSE) && (_TRUE == pwrpriv->bInSuspend )){
  1372. ret = _FAIL;
  1373. goto exit;
  1374. }
  1375. //block???
  1376. if((pwrpriv->bInternalAutoSuspend == _TRUE) && (padapter->net_closed == _TRUE)) {
  1377. ret = _FAIL;
  1378. goto exit;
  1379. }
  1380. //I think this should be check in IPS, LPS, autosuspend functions...
  1381. if (check_fwstate(pmlmepriv, _FW_LINKED) == _TRUE)
  1382. {
  1383. #if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
  1384. if(_TRUE==pwrpriv->bInternalAutoSuspend){
  1385. if(0==pwrpriv->autopm_cnt){
  1386. #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,33))
  1387. if (usb_autopm_get_interface(adapter_to_dvobj(padapter)->pusbintf) < 0)
  1388. {
  1389. DBG_871X( "can't get autopm: \n");
  1390. }
  1391. #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,20))
  1392. usb_autopm_disable(adapter_to_dvobj(padapter)->pusbintf);
  1393. #else
  1394. usb_autoresume_device(adapter_to_dvobj(padapter)->pusbdev, 1);
  1395. #endif
  1396. pwrpriv->autopm_cnt++;
  1397. }
  1398. #endif //#if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
  1399. ret = _SUCCESS;
  1400. goto exit;
  1401. #if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
  1402. }
  1403. #endif //#if defined (CONFIG_BT_COEXIST)&& defined (CONFIG_AUTOSUSPEND)
  1404. }
  1405. if(rf_off == pwrpriv->rf_pwrstate )
  1406. {
  1407. #ifdef CONFIG_USB_HCI
  1408. #ifdef CONFIG_AUTOSUSPEND
  1409. if(pwrpriv->brfoffbyhw==_TRUE)
  1410. {
  1411. DBG_8192C("hw still in rf_off state ...........\n");
  1412. ret = _FAIL;
  1413. goto exit;
  1414. }
  1415. else if(padapter->registrypriv.usbss_enable)
  1416. {
  1417. DBG_8192C("%s call autoresume_enter....\n",__FUNCTION__);
  1418. if(_FAIL == autoresume_enter(padapter))
  1419. {
  1420. DBG_8192C("======> autoresume fail.............\n");
  1421. ret = _FAIL;
  1422. goto exit;
  1423. }
  1424. }
  1425. else
  1426. #endif
  1427. #endif
  1428. {
  1429. #ifdef CONFIG_IPS
  1430. DBG_8192C("%s call ips_leave....\n",__FUNCTION__);
  1431. if(_FAIL == ips_leave(padapter))
  1432. {
  1433. DBG_8192C("======> ips_leave fail.............\n");
  1434. ret = _FAIL;
  1435. goto exit;
  1436. }
  1437. #endif
  1438. }
  1439. }
  1440. //TODO: the following checking need to be merged...
  1441. if(padapter->bDriverStopped
  1442. || !padapter->bup
  1443. || !padapter->hw_init_completed
  1444. ){
  1445. DBG_8192C("%s: bDriverStopped=%d, bup=%d, hw_init_completed=%u\n"
  1446. , caller
  1447. , padapter->bDriverStopped
  1448. , padapter->bup
  1449. , padapter->hw_init_completed);
  1450. ret= _FALSE;
  1451. goto exit;
  1452. }
  1453. exit:
  1454. if (pwrpriv->ips_deny_time < rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms))
  1455. pwrpriv->ips_deny_time = rtw_get_current_time() + rtw_ms_to_systime(ips_deffer_ms);
  1456. return ret;
  1457. }
  1458. int rtw_pm_set_lps(_adapter *padapter, u8 mode)
  1459. {
  1460. int ret = 0;
  1461. struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
  1462. if ( mode < PS_MODE_NUM )
  1463. {
  1464. if(pwrctrlpriv->power_mgnt !=mode)
  1465. {
  1466. if(PS_MODE_ACTIVE == mode)
  1467. {
  1468. LeaveAllPowerSaveMode(padapter);
  1469. }
  1470. else
  1471. {
  1472. pwrctrlpriv->LpsIdleCount = 2;
  1473. }
  1474. pwrctrlpriv->power_mgnt = mode;
  1475. pwrctrlpriv->bLeisurePs = (PS_MODE_ACTIVE != pwrctrlpriv->power_mgnt)?_TRUE:_FALSE;
  1476. }
  1477. }
  1478. else
  1479. {
  1480. ret = -EINVAL;
  1481. }
  1482. return ret;
  1483. }
  1484. int rtw_pm_set_ips(_adapter *padapter, u8 mode)
  1485. {
  1486. struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
  1487. if( mode == IPS_NORMAL || mode == IPS_LEVEL_2 ) {
  1488. rtw_ips_mode_req(pwrctrlpriv, mode);
  1489. DBG_871X("%s %s\n", __FUNCTION__, mode == IPS_NORMAL?"IPS_NORMAL":"IPS_LEVEL_2");
  1490. return 0;
  1491. }
  1492. else if(mode ==IPS_NONE){
  1493. rtw_ips_mode_req(pwrctrlpriv, mode);
  1494. DBG_871X("%s %s\n", __FUNCTION__, "IPS_NONE");
  1495. if((padapter->bSurpriseRemoved ==0)&&(_FAIL == rtw_pwr_wakeup(padapter)) )
  1496. return -EFAULT;
  1497. }
  1498. else {
  1499. return -EINVAL;
  1500. }
  1501. return 0;
  1502. }