dm_common.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2012 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * wlanfae <wlanfae@realtek.com>
  23. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  24. * Hsinchu 300, Taiwan.
  25. *
  26. * Larry Finger <Larry.Finger@lwfinger.net>
  27. *
  28. *****************************************************************************/
  29. #include <linux/export.h>
  30. #include "dm_common.h"
  31. #include "phy_common.h"
  32. #include "../pci.h"
  33. #include "../base.h"
  34. #include "../core.h"
  35. #define BT_RSSI_STATE_NORMAL_POWER BIT_OFFSET_LEN_MASK_32(0, 1)
  36. #define BT_RSSI_STATE_AMDPU_OFF BIT_OFFSET_LEN_MASK_32(1, 1)
  37. #define BT_RSSI_STATE_SPECIAL_LOW BIT_OFFSET_LEN_MASK_32(2, 1)
  38. #define BT_RSSI_STATE_BG_EDCA_LOW BIT_OFFSET_LEN_MASK_32(3, 1)
  39. #define BT_RSSI_STATE_TXPOWER_LOW BIT_OFFSET_LEN_MASK_32(4, 1)
  40. #define RTLPRIV (struct rtl_priv *)
  41. #define GET_UNDECORATED_AVERAGE_RSSI(_priv) \
  42. ((RTLPRIV(_priv))->mac80211.opmode == \
  43. NL80211_IFTYPE_ADHOC) ? \
  44. ((RTLPRIV(_priv))->dm.entry_min_undec_sm_pwdb) : \
  45. ((RTLPRIV(_priv))->dm.undec_sm_pwdb)
  46. static const u32 ofdmswing_table[OFDM_TABLE_SIZE] = {
  47. 0x7f8001fe,
  48. 0x788001e2,
  49. 0x71c001c7,
  50. 0x6b8001ae,
  51. 0x65400195,
  52. 0x5fc0017f,
  53. 0x5a400169,
  54. 0x55400155,
  55. 0x50800142,
  56. 0x4c000130,
  57. 0x47c0011f,
  58. 0x43c0010f,
  59. 0x40000100,
  60. 0x3c8000f2,
  61. 0x390000e4,
  62. 0x35c000d7,
  63. 0x32c000cb,
  64. 0x300000c0,
  65. 0x2d4000b5,
  66. 0x2ac000ab,
  67. 0x288000a2,
  68. 0x26000098,
  69. 0x24000090,
  70. 0x22000088,
  71. 0x20000080,
  72. 0x1e400079,
  73. 0x1c800072,
  74. 0x1b00006c,
  75. 0x19800066,
  76. 0x18000060,
  77. 0x16c0005b,
  78. 0x15800056,
  79. 0x14400051,
  80. 0x1300004c,
  81. 0x12000048,
  82. 0x11000044,
  83. 0x10000040,
  84. };
  85. static const u8 cckswing_table_ch1ch13[CCK_TABLE_SIZE][8] = {
  86. {0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04},
  87. {0x33, 0x32, 0x2b, 0x23, 0x1a, 0x11, 0x08, 0x04},
  88. {0x30, 0x2f, 0x29, 0x21, 0x19, 0x10, 0x08, 0x03},
  89. {0x2d, 0x2d, 0x27, 0x1f, 0x18, 0x0f, 0x08, 0x03},
  90. {0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03},
  91. {0x28, 0x28, 0x22, 0x1c, 0x15, 0x0d, 0x07, 0x03},
  92. {0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03},
  93. {0x24, 0x23, 0x1f, 0x19, 0x13, 0x0c, 0x06, 0x03},
  94. {0x22, 0x21, 0x1d, 0x18, 0x11, 0x0b, 0x06, 0x02},
  95. {0x20, 0x20, 0x1b, 0x16, 0x11, 0x08, 0x05, 0x02},
  96. {0x1f, 0x1e, 0x1a, 0x15, 0x10, 0x0a, 0x05, 0x02},
  97. {0x1d, 0x1c, 0x18, 0x14, 0x0f, 0x0a, 0x05, 0x02},
  98. {0x1b, 0x1a, 0x17, 0x13, 0x0e, 0x09, 0x04, 0x02},
  99. {0x1a, 0x19, 0x16, 0x12, 0x0d, 0x09, 0x04, 0x02},
  100. {0x18, 0x17, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x02},
  101. {0x17, 0x16, 0x13, 0x10, 0x0c, 0x08, 0x04, 0x02},
  102. {0x16, 0x15, 0x12, 0x0f, 0x0b, 0x07, 0x04, 0x01},
  103. {0x14, 0x14, 0x11, 0x0e, 0x0b, 0x07, 0x03, 0x02},
  104. {0x13, 0x13, 0x10, 0x0d, 0x0a, 0x06, 0x03, 0x01},
  105. {0x12, 0x12, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01},
  106. {0x11, 0x11, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01},
  107. {0x10, 0x10, 0x0e, 0x0b, 0x08, 0x05, 0x03, 0x01},
  108. {0x0f, 0x0f, 0x0d, 0x0b, 0x08, 0x05, 0x03, 0x01},
  109. {0x0e, 0x0e, 0x0c, 0x0a, 0x08, 0x05, 0x02, 0x01},
  110. {0x0d, 0x0d, 0x0c, 0x0a, 0x07, 0x05, 0x02, 0x01},
  111. {0x0d, 0x0c, 0x0b, 0x09, 0x07, 0x04, 0x02, 0x01},
  112. {0x0c, 0x0c, 0x0a, 0x09, 0x06, 0x04, 0x02, 0x01},
  113. {0x0b, 0x0b, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x01},
  114. {0x0b, 0x0a, 0x09, 0x08, 0x06, 0x04, 0x02, 0x01},
  115. {0x0a, 0x0a, 0x09, 0x07, 0x05, 0x03, 0x02, 0x01},
  116. {0x0a, 0x09, 0x08, 0x07, 0x05, 0x03, 0x02, 0x01},
  117. {0x09, 0x09, 0x08, 0x06, 0x05, 0x03, 0x01, 0x01},
  118. {0x09, 0x08, 0x07, 0x06, 0x04, 0x03, 0x01, 0x01}
  119. };
  120. static const u8 cckswing_table_ch14[CCK_TABLE_SIZE][8] = {
  121. {0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00},
  122. {0x33, 0x32, 0x2b, 0x19, 0x00, 0x00, 0x00, 0x00},
  123. {0x30, 0x2f, 0x29, 0x18, 0x00, 0x00, 0x00, 0x00},
  124. {0x2d, 0x2d, 0x17, 0x17, 0x00, 0x00, 0x00, 0x00},
  125. {0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00},
  126. {0x28, 0x28, 0x24, 0x14, 0x00, 0x00, 0x00, 0x00},
  127. {0x26, 0x25, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00},
  128. {0x24, 0x23, 0x1f, 0x12, 0x00, 0x00, 0x00, 0x00},
  129. {0x22, 0x21, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00},
  130. {0x20, 0x20, 0x1b, 0x10, 0x00, 0x00, 0x00, 0x00},
  131. {0x1f, 0x1e, 0x1a, 0x0f, 0x00, 0x00, 0x00, 0x00},
  132. {0x1d, 0x1c, 0x18, 0x0e, 0x00, 0x00, 0x00, 0x00},
  133. {0x1b, 0x1a, 0x17, 0x0e, 0x00, 0x00, 0x00, 0x00},
  134. {0x1a, 0x19, 0x16, 0x0d, 0x00, 0x00, 0x00, 0x00},
  135. {0x18, 0x17, 0x15, 0x0c, 0x00, 0x00, 0x00, 0x00},
  136. {0x17, 0x16, 0x13, 0x0b, 0x00, 0x00, 0x00, 0x00},
  137. {0x16, 0x15, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00},
  138. {0x14, 0x14, 0x11, 0x0a, 0x00, 0x00, 0x00, 0x00},
  139. {0x13, 0x13, 0x10, 0x0a, 0x00, 0x00, 0x00, 0x00},
  140. {0x12, 0x12, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00},
  141. {0x11, 0x11, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00},
  142. {0x10, 0x10, 0x0e, 0x08, 0x00, 0x00, 0x00, 0x00},
  143. {0x0f, 0x0f, 0x0d, 0x08, 0x00, 0x00, 0x00, 0x00},
  144. {0x0e, 0x0e, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00},
  145. {0x0d, 0x0d, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00},
  146. {0x0d, 0x0c, 0x0b, 0x06, 0x00, 0x00, 0x00, 0x00},
  147. {0x0c, 0x0c, 0x0a, 0x06, 0x00, 0x00, 0x00, 0x00},
  148. {0x0b, 0x0b, 0x0a, 0x06, 0x00, 0x00, 0x00, 0x00},
  149. {0x0b, 0x0a, 0x09, 0x05, 0x00, 0x00, 0x00, 0x00},
  150. {0x0a, 0x0a, 0x09, 0x05, 0x00, 0x00, 0x00, 0x00},
  151. {0x0a, 0x09, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00},
  152. {0x09, 0x09, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00},
  153. {0x09, 0x08, 0x07, 0x04, 0x00, 0x00, 0x00, 0x00}
  154. };
  155. static u32 power_index_reg[6] = {0xc90, 0xc91, 0xc92, 0xc98, 0xc99, 0xc9a};
  156. void dm_restorepowerindex(struct ieee80211_hw *hw)
  157. {
  158. struct rtl_priv *rtlpriv = rtl_priv(hw);
  159. u8 index;
  160. for (index = 0; index < 6; index++)
  161. rtl_write_byte(rtlpriv, power_index_reg[index],
  162. rtlpriv->dm.powerindex_backup[index]);
  163. }
  164. EXPORT_SYMBOL_GPL(dm_restorepowerindex);
  165. void dm_writepowerindex(struct ieee80211_hw *hw, u8 value)
  166. {
  167. struct rtl_priv *rtlpriv = rtl_priv(hw);
  168. u8 index;
  169. for (index = 0; index < 6; index++)
  170. rtl_write_byte(rtlpriv, power_index_reg[index], value);
  171. }
  172. EXPORT_SYMBOL_GPL(dm_writepowerindex);
  173. void dm_savepowerindex(struct ieee80211_hw *hw)
  174. {
  175. struct rtl_priv *rtlpriv = rtl_priv(hw);
  176. u8 index;
  177. u8 tmp;
  178. for (index = 0; index < 6; index++) {
  179. tmp = rtl_read_byte(rtlpriv, power_index_reg[index]);
  180. rtlpriv->dm.powerindex_backup[index] = tmp;
  181. }
  182. }
  183. EXPORT_SYMBOL_GPL(dm_savepowerindex);
  184. static u8 rtl92c_dm_initial_gain_min_pwdb(struct ieee80211_hw *hw)
  185. {
  186. struct rtl_priv *rtlpriv = rtl_priv(hw);
  187. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  188. long rssi_val_min = 0;
  189. if ((dm_digtable->curmultista_cstate == DIG_MULTISTA_CONNECT) &&
  190. (dm_digtable->cursta_cstate == DIG_STA_CONNECT)) {
  191. if (rtlpriv->dm.entry_min_undec_sm_pwdb != 0)
  192. rssi_val_min =
  193. (rtlpriv->dm.entry_min_undec_sm_pwdb >
  194. rtlpriv->dm.undec_sm_pwdb) ?
  195. rtlpriv->dm.undec_sm_pwdb :
  196. rtlpriv->dm.entry_min_undec_sm_pwdb;
  197. else
  198. rssi_val_min = rtlpriv->dm.undec_sm_pwdb;
  199. } else if (dm_digtable->cursta_cstate == DIG_STA_CONNECT ||
  200. dm_digtable->cursta_cstate == DIG_STA_BEFORE_CONNECT) {
  201. rssi_val_min = rtlpriv->dm.undec_sm_pwdb;
  202. } else if (dm_digtable->curmultista_cstate == DIG_MULTISTA_CONNECT) {
  203. rssi_val_min = rtlpriv->dm.entry_min_undec_sm_pwdb;
  204. }
  205. if (rssi_val_min > 100)
  206. rssi_val_min = 100;
  207. return (u8)rssi_val_min;
  208. }
  209. static void rtl92c_dm_false_alarm_counter_statistics(struct ieee80211_hw *hw)
  210. {
  211. u32 ret_value;
  212. struct rtl_priv *rtlpriv = rtl_priv(hw);
  213. struct false_alarm_statistics *falsealm_cnt = &(rtlpriv->falsealm_cnt);
  214. ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER1, MASKDWORD);
  215. falsealm_cnt->cnt_parity_fail = ((ret_value & 0xffff0000) >> 16);
  216. ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER2, MASKDWORD);
  217. falsealm_cnt->cnt_rate_illegal = (ret_value & 0xffff);
  218. falsealm_cnt->cnt_crc8_fail = ((ret_value & 0xffff0000) >> 16);
  219. ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER3, MASKDWORD);
  220. falsealm_cnt->cnt_mcs_fail = (ret_value & 0xffff);
  221. ret_value = rtl_get_bbreg(hw, ROFDM0_FRAMESYNC, MASKDWORD);
  222. falsealm_cnt->cnt_fast_fsync_fail = (ret_value & 0xffff);
  223. falsealm_cnt->cnt_sb_search_fail = ((ret_value & 0xffff0000) >> 16);
  224. falsealm_cnt->cnt_ofdm_fail = falsealm_cnt->cnt_parity_fail +
  225. falsealm_cnt->cnt_rate_illegal +
  226. falsealm_cnt->cnt_crc8_fail +
  227. falsealm_cnt->cnt_mcs_fail +
  228. falsealm_cnt->cnt_fast_fsync_fail +
  229. falsealm_cnt->cnt_sb_search_fail;
  230. rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, BIT(14), 1);
  231. ret_value = rtl_get_bbreg(hw, RCCK0_FACOUNTERLOWER, MASKBYTE0);
  232. falsealm_cnt->cnt_cck_fail = ret_value;
  233. ret_value = rtl_get_bbreg(hw, RCCK0_FACOUNTERUPPER, MASKBYTE3);
  234. falsealm_cnt->cnt_cck_fail += (ret_value & 0xff) << 8;
  235. falsealm_cnt->cnt_all = (falsealm_cnt->cnt_parity_fail +
  236. falsealm_cnt->cnt_rate_illegal +
  237. falsealm_cnt->cnt_crc8_fail +
  238. falsealm_cnt->cnt_mcs_fail +
  239. falsealm_cnt->cnt_cck_fail);
  240. rtl_set_bbreg(hw, ROFDM1_LSTF, 0x08000000, 1);
  241. rtl_set_bbreg(hw, ROFDM1_LSTF, 0x08000000, 0);
  242. rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, 0x0000c000, 0);
  243. rtl_set_bbreg(hw, RCCK0_FALSEALARMREPORT, 0x0000c000, 2);
  244. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  245. "cnt_parity_fail = %d, cnt_rate_illegal = %d, cnt_crc8_fail = %d, cnt_mcs_fail = %d\n",
  246. falsealm_cnt->cnt_parity_fail,
  247. falsealm_cnt->cnt_rate_illegal,
  248. falsealm_cnt->cnt_crc8_fail, falsealm_cnt->cnt_mcs_fail);
  249. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  250. "cnt_ofdm_fail = %x, cnt_cck_fail = %x, cnt_all = %x\n",
  251. falsealm_cnt->cnt_ofdm_fail,
  252. falsealm_cnt->cnt_cck_fail, falsealm_cnt->cnt_all);
  253. }
  254. static void rtl92c_dm_ctrl_initgain_by_fa(struct ieee80211_hw *hw)
  255. {
  256. struct rtl_priv *rtlpriv = rtl_priv(hw);
  257. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  258. u8 value_igi = dm_digtable->cur_igvalue;
  259. if (rtlpriv->falsealm_cnt.cnt_all < DM_DIG_FA_TH0)
  260. value_igi--;
  261. else if (rtlpriv->falsealm_cnt.cnt_all < DM_DIG_FA_TH1)
  262. value_igi += 0;
  263. else if (rtlpriv->falsealm_cnt.cnt_all < DM_DIG_FA_TH2)
  264. value_igi++;
  265. else if (rtlpriv->falsealm_cnt.cnt_all >= DM_DIG_FA_TH2)
  266. value_igi += 2;
  267. if (value_igi > DM_DIG_FA_UPPER)
  268. value_igi = DM_DIG_FA_UPPER;
  269. else if (value_igi < DM_DIG_FA_LOWER)
  270. value_igi = DM_DIG_FA_LOWER;
  271. if (rtlpriv->falsealm_cnt.cnt_all > 10000)
  272. value_igi = DM_DIG_FA_UPPER;
  273. dm_digtable->cur_igvalue = value_igi;
  274. rtl92c_dm_write_dig(hw);
  275. }
  276. static void rtl92c_dm_ctrl_initgain_by_rssi(struct ieee80211_hw *hw)
  277. {
  278. struct rtl_priv *rtlpriv = rtl_priv(hw);
  279. struct dig_t *digtable = &rtlpriv->dm_digtable;
  280. u32 isbt;
  281. /* modify DIG lower bound, deal with abnorally large false alarm */
  282. if (rtlpriv->falsealm_cnt.cnt_all > 10000) {
  283. digtable->large_fa_hit++;
  284. if (digtable->forbidden_igi < digtable->cur_igvalue) {
  285. digtable->forbidden_igi = digtable->cur_igvalue;
  286. digtable->large_fa_hit = 1;
  287. }
  288. if (digtable->large_fa_hit >= 3) {
  289. if ((digtable->forbidden_igi + 1) >
  290. digtable->rx_gain_max)
  291. digtable->rx_gain_min = digtable->rx_gain_max;
  292. else
  293. digtable->rx_gain_min = (digtable->forbidden_igi + 1);
  294. digtable->recover_cnt = 3600; /* 3600=2hr */
  295. }
  296. } else {
  297. /* Recovery mechanism for IGI lower bound */
  298. if (digtable->recover_cnt != 0) {
  299. digtable->recover_cnt--;
  300. } else {
  301. if (digtable->large_fa_hit == 0) {
  302. if ((digtable->forbidden_igi-1) < DM_DIG_MIN) {
  303. digtable->forbidden_igi = DM_DIG_MIN;
  304. digtable->rx_gain_min = DM_DIG_MIN;
  305. } else {
  306. digtable->forbidden_igi--;
  307. digtable->rx_gain_min = digtable->forbidden_igi + 1;
  308. }
  309. } else if (digtable->large_fa_hit == 3) {
  310. digtable->large_fa_hit = 0;
  311. }
  312. }
  313. }
  314. if (rtlpriv->falsealm_cnt.cnt_all < 250) {
  315. isbt = rtl_read_byte(rtlpriv, 0x4fd) & 0x01;
  316. if (!isbt) {
  317. if (rtlpriv->falsealm_cnt.cnt_all >
  318. digtable->fa_lowthresh) {
  319. if ((digtable->back_val - 2) <
  320. digtable->back_range_min)
  321. digtable->back_val = digtable->back_range_min;
  322. else
  323. digtable->back_val -= 2;
  324. } else if (rtlpriv->falsealm_cnt.cnt_all <
  325. digtable->fa_lowthresh) {
  326. if ((digtable->back_val + 2) >
  327. digtable->back_range_max)
  328. digtable->back_val = digtable->back_range_max;
  329. else
  330. digtable->back_val += 2;
  331. }
  332. } else {
  333. digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
  334. }
  335. } else {
  336. /* Adjust initial gain by false alarm */
  337. if (rtlpriv->falsealm_cnt.cnt_all > 1000)
  338. digtable->cur_igvalue = digtable->pre_igvalue + 2;
  339. else if (rtlpriv->falsealm_cnt.cnt_all > 750)
  340. digtable->cur_igvalue = digtable->pre_igvalue + 1;
  341. else if (rtlpriv->falsealm_cnt.cnt_all < 500)
  342. digtable->cur_igvalue = digtable->pre_igvalue - 1;
  343. }
  344. /* Check initial gain by upper/lower bound */
  345. if (digtable->cur_igvalue > digtable->rx_gain_max)
  346. digtable->cur_igvalue = digtable->rx_gain_max;
  347. if (digtable->cur_igvalue < digtable->rx_gain_min)
  348. digtable->cur_igvalue = digtable->rx_gain_min;
  349. rtl92c_dm_write_dig(hw);
  350. }
  351. static void rtl92c_dm_initial_gain_multi_sta(struct ieee80211_hw *hw)
  352. {
  353. static u8 initialized; /* initialized to false */
  354. struct rtl_priv *rtlpriv = rtl_priv(hw);
  355. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  356. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  357. long rssi_strength = rtlpriv->dm.entry_min_undec_sm_pwdb;
  358. bool multi_sta = false;
  359. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  360. multi_sta = true;
  361. if (!multi_sta ||
  362. dm_digtable->cursta_cstate == DIG_STA_DISCONNECT) {
  363. initialized = false;
  364. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
  365. return;
  366. } else if (initialized == false) {
  367. initialized = true;
  368. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_0;
  369. dm_digtable->cur_igvalue = 0x20;
  370. rtl92c_dm_write_dig(hw);
  371. }
  372. if (dm_digtable->curmultista_cstate == DIG_MULTISTA_CONNECT) {
  373. if ((rssi_strength < dm_digtable->rssi_lowthresh) &&
  374. (dm_digtable->dig_ext_port_stage != DIG_EXT_PORT_STAGE_1)) {
  375. if (dm_digtable->dig_ext_port_stage ==
  376. DIG_EXT_PORT_STAGE_2) {
  377. dm_digtable->cur_igvalue = 0x20;
  378. rtl92c_dm_write_dig(hw);
  379. }
  380. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_1;
  381. } else if (rssi_strength > dm_digtable->rssi_highthresh) {
  382. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_2;
  383. rtl92c_dm_ctrl_initgain_by_fa(hw);
  384. }
  385. } else if (dm_digtable->dig_ext_port_stage != DIG_EXT_PORT_STAGE_0) {
  386. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_0;
  387. dm_digtable->cur_igvalue = 0x20;
  388. rtl92c_dm_write_dig(hw);
  389. }
  390. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  391. "curmultista_cstate = %x dig_ext_port_stage %x\n",
  392. dm_digtable->curmultista_cstate,
  393. dm_digtable->dig_ext_port_stage);
  394. }
  395. static void rtl92c_dm_initial_gain_sta(struct ieee80211_hw *hw)
  396. {
  397. struct rtl_priv *rtlpriv = rtl_priv(hw);
  398. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  399. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  400. "presta_cstate = %x, cursta_cstate = %x\n",
  401. dm_digtable->presta_cstate, dm_digtable->cursta_cstate);
  402. if (dm_digtable->presta_cstate == dm_digtable->cursta_cstate ||
  403. dm_digtable->cursta_cstate == DIG_STA_BEFORE_CONNECT ||
  404. dm_digtable->cursta_cstate == DIG_STA_CONNECT) {
  405. if (dm_digtable->cursta_cstate != DIG_STA_DISCONNECT) {
  406. dm_digtable->rssi_val_min =
  407. rtl92c_dm_initial_gain_min_pwdb(hw);
  408. if (dm_digtable->rssi_val_min > 100)
  409. dm_digtable->rssi_val_min = 100;
  410. rtl92c_dm_ctrl_initgain_by_rssi(hw);
  411. }
  412. } else {
  413. dm_digtable->rssi_val_min = 0;
  414. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
  415. dm_digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
  416. dm_digtable->cur_igvalue = 0x20;
  417. dm_digtable->pre_igvalue = 0;
  418. rtl92c_dm_write_dig(hw);
  419. }
  420. }
  421. static void rtl92c_dm_cck_packet_detection_thresh(struct ieee80211_hw *hw)
  422. {
  423. struct rtl_priv *rtlpriv = rtl_priv(hw);
  424. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  425. if (dm_digtable->cursta_cstate == DIG_STA_CONNECT) {
  426. dm_digtable->rssi_val_min = rtl92c_dm_initial_gain_min_pwdb(hw);
  427. if (dm_digtable->rssi_val_min > 100)
  428. dm_digtable->rssi_val_min = 100;
  429. if (dm_digtable->pre_cck_pd_state == CCK_PD_STAGE_LOWRSSI) {
  430. if (dm_digtable->rssi_val_min <= 25)
  431. dm_digtable->cur_cck_pd_state =
  432. CCK_PD_STAGE_LOWRSSI;
  433. else
  434. dm_digtable->cur_cck_pd_state =
  435. CCK_PD_STAGE_HIGHRSSI;
  436. } else {
  437. if (dm_digtable->rssi_val_min <= 20)
  438. dm_digtable->cur_cck_pd_state =
  439. CCK_PD_STAGE_LOWRSSI;
  440. else
  441. dm_digtable->cur_cck_pd_state =
  442. CCK_PD_STAGE_HIGHRSSI;
  443. }
  444. } else {
  445. dm_digtable->cur_cck_pd_state = CCK_PD_STAGE_MAX;
  446. }
  447. if (dm_digtable->pre_cck_pd_state != dm_digtable->cur_cck_pd_state) {
  448. if ((dm_digtable->cur_cck_pd_state == CCK_PD_STAGE_LOWRSSI) ||
  449. (dm_digtable->cur_cck_pd_state == CCK_PD_STAGE_MAX))
  450. rtl_set_bbreg(hw, RCCK0_CCA, MASKBYTE2, 0x83);
  451. else
  452. rtl_set_bbreg(hw, RCCK0_CCA, MASKBYTE2, 0xcd);
  453. dm_digtable->pre_cck_pd_state = dm_digtable->cur_cck_pd_state;
  454. }
  455. }
  456. static void rtl92c_dm_ctrl_initgain_by_twoport(struct ieee80211_hw *hw)
  457. {
  458. struct rtl_priv *rtlpriv = rtl_priv(hw);
  459. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  460. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  461. if (mac->act_scanning)
  462. return;
  463. if (mac->link_state >= MAC80211_LINKED)
  464. dm_digtable->cursta_cstate = DIG_STA_CONNECT;
  465. else
  466. dm_digtable->cursta_cstate = DIG_STA_DISCONNECT;
  467. dm_digtable->curmultista_cstate = DIG_MULTISTA_DISCONNECT;
  468. rtl92c_dm_initial_gain_sta(hw);
  469. rtl92c_dm_initial_gain_multi_sta(hw);
  470. rtl92c_dm_cck_packet_detection_thresh(hw);
  471. dm_digtable->presta_cstate = dm_digtable->cursta_cstate;
  472. }
  473. static void rtl92c_dm_dig(struct ieee80211_hw *hw)
  474. {
  475. struct rtl_priv *rtlpriv = rtl_priv(hw);
  476. if (rtlpriv->dm.dm_initialgain_enable == false)
  477. return;
  478. if (!(rtlpriv->dm.dm_flag & DYNAMIC_FUNC_DIG))
  479. return;
  480. rtl92c_dm_ctrl_initgain_by_twoport(hw);
  481. }
  482. static void rtl92c_dm_init_dynamic_txpower(struct ieee80211_hw *hw)
  483. {
  484. struct rtl_priv *rtlpriv = rtl_priv(hw);
  485. if (rtlpriv->rtlhal.interface == INTF_USB &&
  486. rtlpriv->rtlhal.board_type & 0x1) {
  487. dm_savepowerindex(hw);
  488. rtlpriv->dm.dynamic_txpower_enable = true;
  489. } else {
  490. rtlpriv->dm.dynamic_txpower_enable = false;
  491. }
  492. rtlpriv->dm.last_dtp_lvl = TXHIGHPWRLEVEL_NORMAL;
  493. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
  494. }
  495. void rtl92c_dm_write_dig(struct ieee80211_hw *hw)
  496. {
  497. struct rtl_priv *rtlpriv = rtl_priv(hw);
  498. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  499. RT_TRACE(rtlpriv, COMP_DIG, DBG_LOUD,
  500. "cur_igvalue = 0x%x, pre_igvalue = 0x%x, back_val = %d\n",
  501. dm_digtable->cur_igvalue, dm_digtable->pre_igvalue,
  502. dm_digtable->back_val);
  503. if (rtlpriv->rtlhal.interface == INTF_USB &&
  504. !dm_digtable->dig_enable_flag) {
  505. dm_digtable->pre_igvalue = 0x17;
  506. return;
  507. }
  508. dm_digtable->cur_igvalue -= 1;
  509. if (dm_digtable->cur_igvalue < DM_DIG_MIN)
  510. dm_digtable->cur_igvalue = DM_DIG_MIN;
  511. if (dm_digtable->pre_igvalue != dm_digtable->cur_igvalue) {
  512. rtl_set_bbreg(hw, ROFDM0_XAAGCCORE1, 0x7f,
  513. dm_digtable->cur_igvalue);
  514. rtl_set_bbreg(hw, ROFDM0_XBAGCCORE1, 0x7f,
  515. dm_digtable->cur_igvalue);
  516. dm_digtable->pre_igvalue = dm_digtable->cur_igvalue;
  517. }
  518. RT_TRACE(rtlpriv, COMP_DIG, DBG_WARNING,
  519. "dig values 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  520. dm_digtable->cur_igvalue, dm_digtable->pre_igvalue,
  521. dm_digtable->rssi_val_min, dm_digtable->back_val,
  522. dm_digtable->rx_gain_max, dm_digtable->rx_gain_min,
  523. dm_digtable->large_fa_hit, dm_digtable->forbidden_igi);
  524. }
  525. EXPORT_SYMBOL(rtl92c_dm_write_dig);
  526. static void rtl92c_dm_pwdb_monitor(struct ieee80211_hw *hw)
  527. {
  528. struct rtl_priv *rtlpriv = rtl_priv(hw);
  529. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  530. long tmpentry_max_pwdb = 0, tmpentry_min_pwdb = 0xff;
  531. if (mac->link_state != MAC80211_LINKED)
  532. return;
  533. if (mac->opmode == NL80211_IFTYPE_ADHOC ||
  534. mac->opmode == NL80211_IFTYPE_AP) {
  535. /* TODO: Handle ADHOC and AP Mode */
  536. }
  537. if (tmpentry_max_pwdb != 0)
  538. rtlpriv->dm.entry_max_undec_sm_pwdb = tmpentry_max_pwdb;
  539. else
  540. rtlpriv->dm.entry_max_undec_sm_pwdb = 0;
  541. if (tmpentry_min_pwdb != 0xff)
  542. rtlpriv->dm.entry_min_undec_sm_pwdb = tmpentry_min_pwdb;
  543. else
  544. rtlpriv->dm.entry_min_undec_sm_pwdb = 0;
  545. /* TODO:
  546. * if (mac->opmode == NL80211_IFTYPE_STATION) {
  547. * if (rtlpriv->rtlhal.fw_ready) {
  548. * u32 param = (u32)(rtlpriv->dm.undec_sm_pwdb << 16);
  549. * rtl8192c_set_rssi_cmd(hw, param);
  550. * }
  551. * }
  552. */
  553. }
  554. void rtl92c_dm_init_edca_turbo(struct ieee80211_hw *hw)
  555. {
  556. struct rtl_priv *rtlpriv = rtl_priv(hw);
  557. rtlpriv->dm.current_turbo_edca = false;
  558. rtlpriv->dm.is_any_nonbepkts = false;
  559. rtlpriv->dm.is_cur_rdlstate = false;
  560. }
  561. EXPORT_SYMBOL(rtl92c_dm_init_edca_turbo);
  562. static void rtl92c_dm_check_edca_turbo(struct ieee80211_hw *hw)
  563. {
  564. struct rtl_priv *rtlpriv = rtl_priv(hw);
  565. struct rtl_pci_priv *rtlpcipriv = rtl_pcipriv(hw);
  566. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  567. static u64 last_txok_cnt;
  568. static u64 last_rxok_cnt;
  569. static u32 last_bt_edca_ul;
  570. static u32 last_bt_edca_dl;
  571. u64 cur_txok_cnt = 0;
  572. u64 cur_rxok_cnt = 0;
  573. u32 edca_be_ul = 0x5ea42b;
  574. u32 edca_be_dl = 0x5ea42b;
  575. bool bt_change_edca = false;
  576. if ((last_bt_edca_ul != rtlpcipriv->bt_coexist.bt_edca_ul) ||
  577. (last_bt_edca_dl != rtlpcipriv->bt_coexist.bt_edca_dl)) {
  578. rtlpriv->dm.current_turbo_edca = false;
  579. last_bt_edca_ul = rtlpcipriv->bt_coexist.bt_edca_ul;
  580. last_bt_edca_dl = rtlpcipriv->bt_coexist.bt_edca_dl;
  581. }
  582. if (rtlpcipriv->bt_coexist.bt_edca_ul != 0) {
  583. edca_be_ul = rtlpcipriv->bt_coexist.bt_edca_ul;
  584. bt_change_edca = true;
  585. }
  586. if (rtlpcipriv->bt_coexist.bt_edca_dl != 0) {
  587. edca_be_ul = rtlpcipriv->bt_coexist.bt_edca_dl;
  588. bt_change_edca = true;
  589. }
  590. if (mac->link_state != MAC80211_LINKED) {
  591. rtlpriv->dm.current_turbo_edca = false;
  592. return;
  593. }
  594. if ((!mac->ht_enable) && (!rtlpcipriv->bt_coexist.bt_coexistence)) {
  595. if (!(edca_be_ul & 0xffff0000))
  596. edca_be_ul |= 0x005e0000;
  597. if (!(edca_be_dl & 0xffff0000))
  598. edca_be_dl |= 0x005e0000;
  599. }
  600. if ((bt_change_edca) || ((!rtlpriv->dm.is_any_nonbepkts) &&
  601. (!rtlpriv->dm.disable_framebursting))) {
  602. cur_txok_cnt = rtlpriv->stats.txbytesunicast - last_txok_cnt;
  603. cur_rxok_cnt = rtlpriv->stats.rxbytesunicast - last_rxok_cnt;
  604. if (cur_rxok_cnt > 4 * cur_txok_cnt) {
  605. if (!rtlpriv->dm.is_cur_rdlstate ||
  606. !rtlpriv->dm.current_turbo_edca) {
  607. rtl_write_dword(rtlpriv,
  608. REG_EDCA_BE_PARAM,
  609. edca_be_dl);
  610. rtlpriv->dm.is_cur_rdlstate = true;
  611. }
  612. } else {
  613. if (rtlpriv->dm.is_cur_rdlstate ||
  614. !rtlpriv->dm.current_turbo_edca) {
  615. rtl_write_dword(rtlpriv,
  616. REG_EDCA_BE_PARAM,
  617. edca_be_ul);
  618. rtlpriv->dm.is_cur_rdlstate = false;
  619. }
  620. }
  621. rtlpriv->dm.current_turbo_edca = true;
  622. } else {
  623. if (rtlpriv->dm.current_turbo_edca) {
  624. u8 tmp = AC0_BE;
  625. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AC_PARAM,
  626. &tmp);
  627. rtlpriv->dm.current_turbo_edca = false;
  628. }
  629. }
  630. rtlpriv->dm.is_any_nonbepkts = false;
  631. last_txok_cnt = rtlpriv->stats.txbytesunicast;
  632. last_rxok_cnt = rtlpriv->stats.rxbytesunicast;
  633. }
  634. static void rtl92c_dm_txpower_tracking_callback_thermalmeter(struct ieee80211_hw
  635. *hw)
  636. {
  637. struct rtl_priv *rtlpriv = rtl_priv(hw);
  638. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  639. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  640. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  641. u8 thermalvalue, delta, delta_lck, delta_iqk;
  642. long ele_a, ele_d, temp_cck, val_x, value32;
  643. long val_y, ele_c = 0;
  644. u8 ofdm_index[2], ofdm_index_old[2] = {0, 0}, cck_index_old = 0;
  645. s8 cck_index = 0;
  646. int i;
  647. bool is2t = IS_92C_SERIAL(rtlhal->version);
  648. s8 txpwr_level[3] = {0, 0, 0};
  649. u8 ofdm_min_index = 6, rf;
  650. rtlpriv->dm.txpower_trackinginit = true;
  651. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  652. "rtl92c_dm_txpower_tracking_callback_thermalmeter\n");
  653. thermalvalue = (u8) rtl_get_rfreg(hw, RF90_PATH_A, RF_T_METER, 0x1f);
  654. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  655. "Readback Thermal Meter = 0x%x pre thermal meter 0x%x eeprom_thermalmeter 0x%x\n",
  656. thermalvalue, rtlpriv->dm.thermalvalue,
  657. rtlefuse->eeprom_thermalmeter);
  658. rtl92c_phy_ap_calibrate(hw, (thermalvalue -
  659. rtlefuse->eeprom_thermalmeter));
  660. if (is2t)
  661. rf = 2;
  662. else
  663. rf = 1;
  664. if (thermalvalue) {
  665. ele_d = rtl_get_bbreg(hw, ROFDM0_XATXIQIMBALANCE,
  666. MASKDWORD) & MASKOFDM_D;
  667. for (i = 0; i < OFDM_TABLE_LENGTH; i++) {
  668. if (ele_d == (ofdmswing_table[i] & MASKOFDM_D)) {
  669. ofdm_index_old[0] = (u8) i;
  670. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  671. "Initial pathA ele_d reg0x%x = 0x%lx, ofdm_index=0x%x\n",
  672. ROFDM0_XATXIQIMBALANCE,
  673. ele_d, ofdm_index_old[0]);
  674. break;
  675. }
  676. }
  677. if (is2t) {
  678. ele_d = rtl_get_bbreg(hw, ROFDM0_XBTXIQIMBALANCE,
  679. MASKDWORD) & MASKOFDM_D;
  680. for (i = 0; i < OFDM_TABLE_LENGTH; i++) {
  681. if (ele_d == (ofdmswing_table[i] &
  682. MASKOFDM_D)) {
  683. ofdm_index_old[1] = (u8) i;
  684. RT_TRACE(rtlpriv, COMP_POWER_TRACKING,
  685. DBG_LOUD,
  686. "Initial pathB ele_d reg0x%x = 0x%lx, ofdm_index=0x%x\n",
  687. ROFDM0_XBTXIQIMBALANCE, ele_d,
  688. ofdm_index_old[1]);
  689. break;
  690. }
  691. }
  692. }
  693. temp_cck =
  694. rtl_get_bbreg(hw, RCCK0_TXFILTER2, MASKDWORD) & MASKCCK;
  695. for (i = 0; i < CCK_TABLE_LENGTH; i++) {
  696. if (rtlpriv->dm.cck_inch14) {
  697. if (memcmp((void *)&temp_cck,
  698. (void *)&cckswing_table_ch14[i][2],
  699. 4) == 0) {
  700. cck_index_old = (u8) i;
  701. RT_TRACE(rtlpriv, COMP_POWER_TRACKING,
  702. DBG_LOUD,
  703. "Initial reg0x%x = 0x%lx, cck_index=0x%x, ch 14 %d\n",
  704. RCCK0_TXFILTER2, temp_cck,
  705. cck_index_old,
  706. rtlpriv->dm.cck_inch14);
  707. break;
  708. }
  709. } else {
  710. if (memcmp((void *)&temp_cck,
  711. (void *)
  712. &cckswing_table_ch1ch13[i][2],
  713. 4) == 0) {
  714. cck_index_old = (u8) i;
  715. RT_TRACE(rtlpriv, COMP_POWER_TRACKING,
  716. DBG_LOUD,
  717. "Initial reg0x%x = 0x%lx, cck_index=0x%x, ch14 %d\n",
  718. RCCK0_TXFILTER2, temp_cck,
  719. cck_index_old,
  720. rtlpriv->dm.cck_inch14);
  721. break;
  722. }
  723. }
  724. }
  725. if (!rtlpriv->dm.thermalvalue) {
  726. rtlpriv->dm.thermalvalue =
  727. rtlefuse->eeprom_thermalmeter;
  728. rtlpriv->dm.thermalvalue_lck = thermalvalue;
  729. rtlpriv->dm.thermalvalue_iqk = thermalvalue;
  730. for (i = 0; i < rf; i++)
  731. rtlpriv->dm.ofdm_index[i] = ofdm_index_old[i];
  732. rtlpriv->dm.cck_index = cck_index_old;
  733. }
  734. /* Handle USB High PA boards */
  735. delta = (thermalvalue > rtlpriv->dm.thermalvalue) ?
  736. (thermalvalue - rtlpriv->dm.thermalvalue) :
  737. (rtlpriv->dm.thermalvalue - thermalvalue);
  738. delta_lck = (thermalvalue > rtlpriv->dm.thermalvalue_lck) ?
  739. (thermalvalue - rtlpriv->dm.thermalvalue_lck) :
  740. (rtlpriv->dm.thermalvalue_lck - thermalvalue);
  741. delta_iqk = (thermalvalue > rtlpriv->dm.thermalvalue_iqk) ?
  742. (thermalvalue - rtlpriv->dm.thermalvalue_iqk) :
  743. (rtlpriv->dm.thermalvalue_iqk - thermalvalue);
  744. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  745. "Readback Thermal Meter = 0x%x pre thermal meter 0x%x eeprom_thermalmeter 0x%x delta 0x%x delta_lck 0x%x delta_iqk 0x%x\n",
  746. thermalvalue, rtlpriv->dm.thermalvalue,
  747. rtlefuse->eeprom_thermalmeter, delta, delta_lck,
  748. delta_iqk);
  749. if (delta_lck > 1) {
  750. rtlpriv->dm.thermalvalue_lck = thermalvalue;
  751. rtl92c_phy_lc_calibrate(hw);
  752. }
  753. if (delta > 0 && rtlpriv->dm.txpower_track_control) {
  754. if (thermalvalue > rtlpriv->dm.thermalvalue) {
  755. for (i = 0; i < rf; i++)
  756. rtlpriv->dm.ofdm_index[i] -= delta;
  757. rtlpriv->dm.cck_index -= delta;
  758. } else {
  759. for (i = 0; i < rf; i++)
  760. rtlpriv->dm.ofdm_index[i] += delta;
  761. rtlpriv->dm.cck_index += delta;
  762. }
  763. if (is2t) {
  764. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  765. "temp OFDM_A_index=0x%x, OFDM_B_index=0x%x, cck_index=0x%x\n",
  766. rtlpriv->dm.ofdm_index[0],
  767. rtlpriv->dm.ofdm_index[1],
  768. rtlpriv->dm.cck_index);
  769. } else {
  770. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  771. "temp OFDM_A_index=0x%x, cck_index=0x%x\n",
  772. rtlpriv->dm.ofdm_index[0],
  773. rtlpriv->dm.cck_index);
  774. }
  775. if (thermalvalue > rtlefuse->eeprom_thermalmeter) {
  776. for (i = 0; i < rf; i++)
  777. ofdm_index[i] =
  778. rtlpriv->dm.ofdm_index[i]
  779. + 1;
  780. cck_index = rtlpriv->dm.cck_index + 1;
  781. } else {
  782. for (i = 0; i < rf; i++)
  783. ofdm_index[i] =
  784. rtlpriv->dm.ofdm_index[i];
  785. cck_index = rtlpriv->dm.cck_index;
  786. }
  787. for (i = 0; i < rf; i++) {
  788. if (txpwr_level[i] >= 0 &&
  789. txpwr_level[i] <= 26) {
  790. if (thermalvalue >
  791. rtlefuse->eeprom_thermalmeter) {
  792. if (delta < 5)
  793. ofdm_index[i] -= 1;
  794. else
  795. ofdm_index[i] -= 2;
  796. } else if (delta > 5 && thermalvalue <
  797. rtlefuse->
  798. eeprom_thermalmeter) {
  799. ofdm_index[i] += 1;
  800. }
  801. } else if (txpwr_level[i] >= 27 &&
  802. txpwr_level[i] <= 32
  803. && thermalvalue >
  804. rtlefuse->eeprom_thermalmeter) {
  805. if (delta < 5)
  806. ofdm_index[i] -= 1;
  807. else
  808. ofdm_index[i] -= 2;
  809. } else if (txpwr_level[i] >= 32 &&
  810. txpwr_level[i] <= 38 &&
  811. thermalvalue >
  812. rtlefuse->eeprom_thermalmeter
  813. && delta > 5) {
  814. ofdm_index[i] -= 1;
  815. }
  816. }
  817. if (txpwr_level[i] >= 0 && txpwr_level[i] <= 26) {
  818. if (thermalvalue >
  819. rtlefuse->eeprom_thermalmeter) {
  820. if (delta < 5)
  821. cck_index -= 1;
  822. else
  823. cck_index -= 2;
  824. } else if (delta > 5 && thermalvalue <
  825. rtlefuse->eeprom_thermalmeter) {
  826. cck_index += 1;
  827. }
  828. } else if (txpwr_level[i] >= 27 &&
  829. txpwr_level[i] <= 32 &&
  830. thermalvalue >
  831. rtlefuse->eeprom_thermalmeter) {
  832. if (delta < 5)
  833. cck_index -= 1;
  834. else
  835. cck_index -= 2;
  836. } else if (txpwr_level[i] >= 32 &&
  837. txpwr_level[i] <= 38 &&
  838. thermalvalue > rtlefuse->eeprom_thermalmeter
  839. && delta > 5) {
  840. cck_index -= 1;
  841. }
  842. for (i = 0; i < rf; i++) {
  843. if (ofdm_index[i] > OFDM_TABLE_SIZE - 1)
  844. ofdm_index[i] = OFDM_TABLE_SIZE - 1;
  845. else if (ofdm_index[i] < ofdm_min_index)
  846. ofdm_index[i] = ofdm_min_index;
  847. }
  848. if (cck_index > CCK_TABLE_SIZE - 1)
  849. cck_index = CCK_TABLE_SIZE - 1;
  850. else if (cck_index < 0)
  851. cck_index = 0;
  852. if (is2t) {
  853. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  854. "new OFDM_A_index=0x%x, OFDM_B_index=0x%x, cck_index=0x%x\n",
  855. ofdm_index[0], ofdm_index[1],
  856. cck_index);
  857. } else {
  858. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  859. "new OFDM_A_index=0x%x, cck_index=0x%x\n",
  860. ofdm_index[0], cck_index);
  861. }
  862. }
  863. if (rtlpriv->dm.txpower_track_control && delta != 0) {
  864. ele_d =
  865. (ofdmswing_table[ofdm_index[0]] & 0xFFC00000) >> 22;
  866. val_x = rtlphy->reg_e94;
  867. val_y = rtlphy->reg_e9c;
  868. if (val_x != 0) {
  869. if ((val_x & 0x00000200) != 0)
  870. val_x = val_x | 0xFFFFFC00;
  871. ele_a = ((val_x * ele_d) >> 8) & 0x000003FF;
  872. if ((val_y & 0x00000200) != 0)
  873. val_y = val_y | 0xFFFFFC00;
  874. ele_c = ((val_y * ele_d) >> 8) & 0x000003FF;
  875. value32 = (ele_d << 22) |
  876. ((ele_c & 0x3F) << 16) | ele_a;
  877. rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE,
  878. MASKDWORD, value32);
  879. value32 = (ele_c & 0x000003C0) >> 6;
  880. rtl_set_bbreg(hw, ROFDM0_XCTXAFE, MASKH4BITS,
  881. value32);
  882. value32 = ((val_x * ele_d) >> 7) & 0x01;
  883. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  884. BIT(31), value32);
  885. value32 = ((val_y * ele_d) >> 7) & 0x01;
  886. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  887. BIT(29), value32);
  888. } else {
  889. rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE,
  890. MASKDWORD,
  891. ofdmswing_table[ofdm_index[0]]);
  892. rtl_set_bbreg(hw, ROFDM0_XCTXAFE, MASKH4BITS,
  893. 0x00);
  894. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  895. BIT(31) | BIT(29), 0x00);
  896. }
  897. if (!rtlpriv->dm.cck_inch14) {
  898. rtl_write_byte(rtlpriv, 0xa22,
  899. cckswing_table_ch1ch13[cck_index]
  900. [0]);
  901. rtl_write_byte(rtlpriv, 0xa23,
  902. cckswing_table_ch1ch13[cck_index]
  903. [1]);
  904. rtl_write_byte(rtlpriv, 0xa24,
  905. cckswing_table_ch1ch13[cck_index]
  906. [2]);
  907. rtl_write_byte(rtlpriv, 0xa25,
  908. cckswing_table_ch1ch13[cck_index]
  909. [3]);
  910. rtl_write_byte(rtlpriv, 0xa26,
  911. cckswing_table_ch1ch13[cck_index]
  912. [4]);
  913. rtl_write_byte(rtlpriv, 0xa27,
  914. cckswing_table_ch1ch13[cck_index]
  915. [5]);
  916. rtl_write_byte(rtlpriv, 0xa28,
  917. cckswing_table_ch1ch13[cck_index]
  918. [6]);
  919. rtl_write_byte(rtlpriv, 0xa29,
  920. cckswing_table_ch1ch13[cck_index]
  921. [7]);
  922. } else {
  923. rtl_write_byte(rtlpriv, 0xa22,
  924. cckswing_table_ch14[cck_index]
  925. [0]);
  926. rtl_write_byte(rtlpriv, 0xa23,
  927. cckswing_table_ch14[cck_index]
  928. [1]);
  929. rtl_write_byte(rtlpriv, 0xa24,
  930. cckswing_table_ch14[cck_index]
  931. [2]);
  932. rtl_write_byte(rtlpriv, 0xa25,
  933. cckswing_table_ch14[cck_index]
  934. [3]);
  935. rtl_write_byte(rtlpriv, 0xa26,
  936. cckswing_table_ch14[cck_index]
  937. [4]);
  938. rtl_write_byte(rtlpriv, 0xa27,
  939. cckswing_table_ch14[cck_index]
  940. [5]);
  941. rtl_write_byte(rtlpriv, 0xa28,
  942. cckswing_table_ch14[cck_index]
  943. [6]);
  944. rtl_write_byte(rtlpriv, 0xa29,
  945. cckswing_table_ch14[cck_index]
  946. [7]);
  947. }
  948. if (is2t) {
  949. ele_d = (ofdmswing_table[ofdm_index[1]] &
  950. 0xFFC00000) >> 22;
  951. val_x = rtlphy->reg_eb4;
  952. val_y = rtlphy->reg_ebc;
  953. if (val_x != 0) {
  954. if ((val_x & 0x00000200) != 0)
  955. val_x = val_x | 0xFFFFFC00;
  956. ele_a = ((val_x * ele_d) >> 8) &
  957. 0x000003FF;
  958. if ((val_y & 0x00000200) != 0)
  959. val_y = val_y | 0xFFFFFC00;
  960. ele_c = ((val_y * ele_d) >> 8) &
  961. 0x00003FF;
  962. value32 = (ele_d << 22) |
  963. ((ele_c & 0x3F) << 16) | ele_a;
  964. rtl_set_bbreg(hw,
  965. ROFDM0_XBTXIQIMBALANCE,
  966. MASKDWORD, value32);
  967. value32 = (ele_c & 0x000003C0) >> 6;
  968. rtl_set_bbreg(hw, ROFDM0_XDTXAFE,
  969. MASKH4BITS, value32);
  970. value32 = ((val_x * ele_d) >> 7) & 0x01;
  971. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  972. BIT(27), value32);
  973. value32 = ((val_y * ele_d) >> 7) & 0x01;
  974. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  975. BIT(25), value32);
  976. } else {
  977. rtl_set_bbreg(hw,
  978. ROFDM0_XBTXIQIMBALANCE,
  979. MASKDWORD,
  980. ofdmswing_table[ofdm_index
  981. [1]]);
  982. rtl_set_bbreg(hw, ROFDM0_XDTXAFE,
  983. MASKH4BITS, 0x00);
  984. rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD,
  985. BIT(27) | BIT(25), 0x00);
  986. }
  987. }
  988. }
  989. if (delta_iqk > 3) {
  990. rtlpriv->dm.thermalvalue_iqk = thermalvalue;
  991. rtl92c_phy_iq_calibrate(hw, false);
  992. }
  993. if (rtlpriv->dm.txpower_track_control)
  994. rtlpriv->dm.thermalvalue = thermalvalue;
  995. }
  996. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD, "<===\n");
  997. }
  998. static void rtl92c_dm_initialize_txpower_tracking_thermalmeter(
  999. struct ieee80211_hw *hw)
  1000. {
  1001. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1002. rtlpriv->dm.txpower_tracking = true;
  1003. rtlpriv->dm.txpower_trackinginit = false;
  1004. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  1005. "pMgntInfo->txpower_tracking = %d\n",
  1006. rtlpriv->dm.txpower_tracking);
  1007. }
  1008. static void rtl92c_dm_initialize_txpower_tracking(struct ieee80211_hw *hw)
  1009. {
  1010. rtl92c_dm_initialize_txpower_tracking_thermalmeter(hw);
  1011. }
  1012. static void rtl92c_dm_txpower_tracking_directcall(struct ieee80211_hw *hw)
  1013. {
  1014. rtl92c_dm_txpower_tracking_callback_thermalmeter(hw);
  1015. }
  1016. static void rtl92c_dm_check_txpower_tracking_thermal_meter(
  1017. struct ieee80211_hw *hw)
  1018. {
  1019. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1020. if (!rtlpriv->dm.txpower_tracking)
  1021. return;
  1022. if (!rtlpriv->dm.tm_trigger) {
  1023. rtl_set_rfreg(hw, RF90_PATH_A, RF_T_METER, RFREG_OFFSET_MASK,
  1024. 0x60);
  1025. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  1026. "Trigger 92S Thermal Meter!!\n");
  1027. rtlpriv->dm.tm_trigger = 1;
  1028. return;
  1029. } else {
  1030. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  1031. "Schedule TxPowerTracking direct call!!\n");
  1032. rtl92c_dm_txpower_tracking_directcall(hw);
  1033. rtlpriv->dm.tm_trigger = 0;
  1034. }
  1035. }
  1036. void rtl92c_dm_check_txpower_tracking(struct ieee80211_hw *hw)
  1037. {
  1038. rtl92c_dm_check_txpower_tracking_thermal_meter(hw);
  1039. }
  1040. EXPORT_SYMBOL(rtl92c_dm_check_txpower_tracking);
  1041. void rtl92c_dm_init_rate_adaptive_mask(struct ieee80211_hw *hw)
  1042. {
  1043. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1044. struct rate_adaptive *p_ra = &(rtlpriv->ra);
  1045. p_ra->ratr_state = DM_RATR_STA_INIT;
  1046. p_ra->pre_ratr_state = DM_RATR_STA_INIT;
  1047. if (rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER)
  1048. rtlpriv->dm.useramask = true;
  1049. else
  1050. rtlpriv->dm.useramask = false;
  1051. }
  1052. EXPORT_SYMBOL(rtl92c_dm_init_rate_adaptive_mask);
  1053. static void rtl92c_dm_init_dynamic_bb_powersaving(struct ieee80211_hw *hw)
  1054. {
  1055. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1056. struct ps_t *dm_pstable = &rtlpriv->dm_pstable;
  1057. dm_pstable->pre_ccastate = CCA_MAX;
  1058. dm_pstable->cur_ccasate = CCA_MAX;
  1059. dm_pstable->pre_rfstate = RF_MAX;
  1060. dm_pstable->cur_rfstate = RF_MAX;
  1061. dm_pstable->rssi_val_min = 0;
  1062. }
  1063. void rtl92c_dm_rf_saving(struct ieee80211_hw *hw, u8 bforce_in_normal)
  1064. {
  1065. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1066. struct ps_t *dm_pstable = &rtlpriv->dm_pstable;
  1067. if (!rtlpriv->reg_init) {
  1068. rtlpriv->reg_874 = (rtl_get_bbreg(hw,
  1069. RFPGA0_XCD_RFINTERFACESW,
  1070. MASKDWORD) & 0x1CC000) >> 14;
  1071. rtlpriv->reg_c70 = (rtl_get_bbreg(hw, ROFDM0_AGCPARAMETER1,
  1072. MASKDWORD) & BIT(3)) >> 3;
  1073. rtlpriv->reg_85c = (rtl_get_bbreg(hw, RFPGA0_XCD_SWITCHCONTROL,
  1074. MASKDWORD) & 0xFF000000) >> 24;
  1075. rtlpriv->reg_a74 = (rtl_get_bbreg(hw, 0xa74, MASKDWORD) &
  1076. 0xF000) >> 12;
  1077. rtlpriv->reg_init = true;
  1078. }
  1079. if (!bforce_in_normal) {
  1080. if (dm_pstable->rssi_val_min != 0) {
  1081. if (dm_pstable->pre_rfstate == RF_NORMAL) {
  1082. if (dm_pstable->rssi_val_min >= 30)
  1083. dm_pstable->cur_rfstate = RF_SAVE;
  1084. else
  1085. dm_pstable->cur_rfstate = RF_NORMAL;
  1086. } else {
  1087. if (dm_pstable->rssi_val_min <= 25)
  1088. dm_pstable->cur_rfstate = RF_NORMAL;
  1089. else
  1090. dm_pstable->cur_rfstate = RF_SAVE;
  1091. }
  1092. } else {
  1093. dm_pstable->cur_rfstate = RF_MAX;
  1094. }
  1095. } else {
  1096. dm_pstable->cur_rfstate = RF_NORMAL;
  1097. }
  1098. if (dm_pstable->pre_rfstate != dm_pstable->cur_rfstate) {
  1099. if (dm_pstable->cur_rfstate == RF_SAVE) {
  1100. rtl_set_bbreg(hw, RFPGA0_XCD_RFINTERFACESW,
  1101. 0x1C0000, 0x2);
  1102. rtl_set_bbreg(hw, ROFDM0_AGCPARAMETER1, BIT(3), 0);
  1103. rtl_set_bbreg(hw, RFPGA0_XCD_SWITCHCONTROL,
  1104. 0xFF000000, 0x63);
  1105. rtl_set_bbreg(hw, RFPGA0_XCD_RFINTERFACESW,
  1106. 0xC000, 0x2);
  1107. rtl_set_bbreg(hw, 0xa74, 0xF000, 0x3);
  1108. rtl_set_bbreg(hw, 0x818, BIT(28), 0x0);
  1109. rtl_set_bbreg(hw, 0x818, BIT(28), 0x1);
  1110. } else {
  1111. rtl_set_bbreg(hw, RFPGA0_XCD_RFINTERFACESW,
  1112. 0x1CC000, rtlpriv->reg_874);
  1113. rtl_set_bbreg(hw, ROFDM0_AGCPARAMETER1, BIT(3),
  1114. rtlpriv->reg_c70);
  1115. rtl_set_bbreg(hw, RFPGA0_XCD_SWITCHCONTROL, 0xFF000000,
  1116. rtlpriv->reg_85c);
  1117. rtl_set_bbreg(hw, 0xa74, 0xF000, rtlpriv->reg_a74);
  1118. rtl_set_bbreg(hw, 0x818, BIT(28), 0x0);
  1119. }
  1120. dm_pstable->pre_rfstate = dm_pstable->cur_rfstate;
  1121. }
  1122. }
  1123. EXPORT_SYMBOL(rtl92c_dm_rf_saving);
  1124. static void rtl92c_dm_dynamic_bb_powersaving(struct ieee80211_hw *hw)
  1125. {
  1126. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1127. struct ps_t *dm_pstable = &rtlpriv->dm_pstable;
  1128. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1129. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1130. /* Determine the minimum RSSI */
  1131. if (((mac->link_state == MAC80211_NOLINK)) &&
  1132. (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
  1133. dm_pstable->rssi_val_min = 0;
  1134. RT_TRACE(rtlpriv, DBG_LOUD, DBG_LOUD, "Not connected to any\n");
  1135. }
  1136. if (mac->link_state == MAC80211_LINKED) {
  1137. if (mac->opmode == NL80211_IFTYPE_ADHOC) {
  1138. dm_pstable->rssi_val_min =
  1139. rtlpriv->dm.entry_min_undec_sm_pwdb;
  1140. RT_TRACE(rtlpriv, DBG_LOUD, DBG_LOUD,
  1141. "AP Client PWDB = 0x%lx\n",
  1142. dm_pstable->rssi_val_min);
  1143. } else {
  1144. dm_pstable->rssi_val_min = rtlpriv->dm.undec_sm_pwdb;
  1145. RT_TRACE(rtlpriv, DBG_LOUD, DBG_LOUD,
  1146. "STA Default Port PWDB = 0x%lx\n",
  1147. dm_pstable->rssi_val_min);
  1148. }
  1149. } else {
  1150. dm_pstable->rssi_val_min =
  1151. rtlpriv->dm.entry_min_undec_sm_pwdb;
  1152. RT_TRACE(rtlpriv, DBG_LOUD, DBG_LOUD,
  1153. "AP Ext Port PWDB = 0x%lx\n",
  1154. dm_pstable->rssi_val_min);
  1155. }
  1156. /* Power Saving for 92C */
  1157. if (IS_92C_SERIAL(rtlhal->version))
  1158. ;/* rtl92c_dm_1r_cca(hw); */
  1159. else
  1160. rtl92c_dm_rf_saving(hw, false);
  1161. }
  1162. void rtl92c_dm_init(struct ieee80211_hw *hw)
  1163. {
  1164. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1165. rtlpriv->dm.dm_type = DM_TYPE_BYDRIVER;
  1166. rtlpriv->dm.dm_flag = DYNAMIC_FUNC_DISABLE | DYNAMIC_FUNC_DIG;
  1167. rtlpriv->dm.undec_sm_pwdb = -1;
  1168. rtlpriv->dm.undec_sm_cck = -1;
  1169. rtlpriv->dm.dm_initialgain_enable = true;
  1170. rtl_dm_diginit(hw, 0x20);
  1171. rtlpriv->dm.dm_flag |= HAL_DM_HIPWR_DISABLE;
  1172. rtl92c_dm_init_dynamic_txpower(hw);
  1173. rtl92c_dm_init_edca_turbo(hw);
  1174. rtl92c_dm_init_rate_adaptive_mask(hw);
  1175. rtlpriv->dm.dm_flag |= DYNAMIC_FUNC_SS;
  1176. rtl92c_dm_initialize_txpower_tracking(hw);
  1177. rtl92c_dm_init_dynamic_bb_powersaving(hw);
  1178. rtlpriv->dm.ofdm_pkt_cnt = 0;
  1179. rtlpriv->dm.dm_rssi_sel = RSSI_DEFAULT;
  1180. }
  1181. EXPORT_SYMBOL(rtl92c_dm_init);
  1182. void rtl92c_dm_dynamic_txpower(struct ieee80211_hw *hw)
  1183. {
  1184. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1185. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  1186. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1187. long undec_sm_pwdb;
  1188. if (!rtlpriv->dm.dynamic_txpower_enable)
  1189. return;
  1190. if (rtlpriv->dm.dm_flag & HAL_DM_HIPWR_DISABLE) {
  1191. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
  1192. return;
  1193. }
  1194. if ((mac->link_state < MAC80211_LINKED) &&
  1195. (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
  1196. RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
  1197. "Not connected to any\n");
  1198. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
  1199. rtlpriv->dm.last_dtp_lvl = TXHIGHPWRLEVEL_NORMAL;
  1200. return;
  1201. }
  1202. if (mac->link_state >= MAC80211_LINKED) {
  1203. if (mac->opmode == NL80211_IFTYPE_ADHOC) {
  1204. undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
  1205. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1206. "AP Client PWDB = 0x%lx\n",
  1207. undec_sm_pwdb);
  1208. } else {
  1209. undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
  1210. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1211. "STA Default Port PWDB = 0x%lx\n",
  1212. undec_sm_pwdb);
  1213. }
  1214. } else {
  1215. undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
  1216. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1217. "AP Ext Port PWDB = 0x%lx\n",
  1218. undec_sm_pwdb);
  1219. }
  1220. if (undec_sm_pwdb >= TX_POWER_NEAR_FIELD_THRESH_LVL2) {
  1221. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL2;
  1222. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1223. "TXHIGHPWRLEVEL_LEVEL1 (TxPwr=0x0)\n");
  1224. } else if ((undec_sm_pwdb < (TX_POWER_NEAR_FIELD_THRESH_LVL2 - 3)) &&
  1225. (undec_sm_pwdb >= TX_POWER_NEAR_FIELD_THRESH_LVL1)) {
  1226. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL1;
  1227. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1228. "TXHIGHPWRLEVEL_LEVEL1 (TxPwr=0x10)\n");
  1229. } else if (undec_sm_pwdb < (TX_POWER_NEAR_FIELD_THRESH_LVL1 - 5)) {
  1230. rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
  1231. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1232. "TXHIGHPWRLEVEL_NORMAL\n");
  1233. }
  1234. if ((rtlpriv->dm.dynamic_txhighpower_lvl != rtlpriv->dm.last_dtp_lvl)) {
  1235. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  1236. "PHY_SetTxPowerLevel8192S() Channel = %d\n",
  1237. rtlphy->current_channel);
  1238. rtl92c_phy_set_txpower_level(hw, rtlphy->current_channel);
  1239. if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  1240. TXHIGHPWRLEVEL_NORMAL)
  1241. dm_restorepowerindex(hw);
  1242. else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  1243. TXHIGHPWRLEVEL_LEVEL1)
  1244. dm_writepowerindex(hw, 0x14);
  1245. else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
  1246. TXHIGHPWRLEVEL_LEVEL2)
  1247. dm_writepowerindex(hw, 0x10);
  1248. }
  1249. rtlpriv->dm.last_dtp_lvl = rtlpriv->dm.dynamic_txhighpower_lvl;
  1250. }
  1251. void rtl92c_dm_watchdog(struct ieee80211_hw *hw)
  1252. {
  1253. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1254. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  1255. bool fw_current_inpsmode = false;
  1256. bool fw_ps_awake = true;
  1257. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FW_PSMODE_STATUS,
  1258. (u8 *) (&fw_current_inpsmode));
  1259. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FWLPS_RF_ON,
  1260. (u8 *) (&fw_ps_awake));
  1261. if (ppsc->p2p_ps_info.p2p_ps_mode)
  1262. fw_ps_awake = false;
  1263. if ((ppsc->rfpwr_state == ERFON) && ((!fw_current_inpsmode) &&
  1264. fw_ps_awake)
  1265. && (!ppsc->rfchange_inprogress)) {
  1266. rtl92c_dm_pwdb_monitor(hw);
  1267. rtl92c_dm_dig(hw);
  1268. rtl92c_dm_false_alarm_counter_statistics(hw);
  1269. rtl92c_dm_dynamic_bb_powersaving(hw);
  1270. rtl92c_dm_dynamic_txpower(hw);
  1271. rtl92c_dm_check_txpower_tracking(hw);
  1272. /* rtl92c_dm_refresh_rate_adaptive_mask(hw); */
  1273. rtl92c_dm_bt_coexist(hw);
  1274. rtl92c_dm_check_edca_turbo(hw);
  1275. }
  1276. }
  1277. EXPORT_SYMBOL(rtl92c_dm_watchdog);
  1278. u8 rtl92c_bt_rssi_state_change(struct ieee80211_hw *hw)
  1279. {
  1280. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1281. struct rtl_pci_priv *rtlpcipriv = rtl_pcipriv(hw);
  1282. long undec_sm_pwdb;
  1283. u8 curr_bt_rssi_state = 0x00;
  1284. if (rtlpriv->mac80211.link_state == MAC80211_LINKED) {
  1285. undec_sm_pwdb = GET_UNDECORATED_AVERAGE_RSSI(rtlpriv);
  1286. } else {
  1287. if (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)
  1288. undec_sm_pwdb = 100;
  1289. else
  1290. undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
  1291. }
  1292. /* Check RSSI to determine HighPower/NormalPower state for
  1293. * BT coexistence. */
  1294. if (undec_sm_pwdb >= 67)
  1295. curr_bt_rssi_state &= (~BT_RSSI_STATE_NORMAL_POWER);
  1296. else if (undec_sm_pwdb < 62)
  1297. curr_bt_rssi_state |= BT_RSSI_STATE_NORMAL_POWER;
  1298. /* Check RSSI to determine AMPDU setting for BT coexistence. */
  1299. if (undec_sm_pwdb >= 40)
  1300. curr_bt_rssi_state &= (~BT_RSSI_STATE_AMDPU_OFF);
  1301. else if (undec_sm_pwdb <= 32)
  1302. curr_bt_rssi_state |= BT_RSSI_STATE_AMDPU_OFF;
  1303. /* Marked RSSI state. It will be used to determine BT coexistence
  1304. * setting later. */
  1305. if (undec_sm_pwdb < 35)
  1306. curr_bt_rssi_state |= BT_RSSI_STATE_SPECIAL_LOW;
  1307. else
  1308. curr_bt_rssi_state &= (~BT_RSSI_STATE_SPECIAL_LOW);
  1309. /* Check BT state related to BT_Idle in B/G mode. */
  1310. if (undec_sm_pwdb < 15)
  1311. curr_bt_rssi_state |= BT_RSSI_STATE_BG_EDCA_LOW;
  1312. else
  1313. curr_bt_rssi_state &= (~BT_RSSI_STATE_BG_EDCA_LOW);
  1314. if (curr_bt_rssi_state != rtlpcipriv->bt_coexist.bt_rssi_state) {
  1315. rtlpcipriv->bt_coexist.bt_rssi_state = curr_bt_rssi_state;
  1316. return true;
  1317. } else {
  1318. return false;
  1319. }
  1320. }
  1321. EXPORT_SYMBOL(rtl92c_bt_rssi_state_change);
  1322. static bool rtl92c_bt_state_change(struct ieee80211_hw *hw)
  1323. {
  1324. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1325. struct rtl_pci_priv *rtlpcipriv = rtl_pcipriv(hw);
  1326. u32 polling, ratio_tx, ratio_pri;
  1327. u32 bt_tx, bt_pri;
  1328. u8 bt_state;
  1329. u8 cur_service_type;
  1330. if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
  1331. return false;
  1332. bt_state = rtl_read_byte(rtlpriv, 0x4fd);
  1333. bt_tx = rtl_read_dword(rtlpriv, 0x488);
  1334. bt_tx = bt_tx & 0x00ffffff;
  1335. bt_pri = rtl_read_dword(rtlpriv, 0x48c);
  1336. bt_pri = bt_pri & 0x00ffffff;
  1337. polling = rtl_read_dword(rtlpriv, 0x490);
  1338. if (bt_tx == 0xffffffff && bt_pri == 0xffffffff &&
  1339. polling == 0xffffffff && bt_state == 0xff)
  1340. return false;
  1341. bt_state &= BIT_OFFSET_LEN_MASK_32(0, 1);
  1342. if (bt_state != rtlpcipriv->bt_coexist.bt_cur_state) {
  1343. rtlpcipriv->bt_coexist.bt_cur_state = bt_state;
  1344. if (rtlpcipriv->bt_coexist.reg_bt_sco == 3) {
  1345. rtlpcipriv->bt_coexist.bt_service = BT_IDLE;
  1346. bt_state = bt_state |
  1347. ((rtlpcipriv->bt_coexist.bt_ant_isolation == 1) ?
  1348. 0 : BIT_OFFSET_LEN_MASK_32(1, 1)) |
  1349. BIT_OFFSET_LEN_MASK_32(2, 1);
  1350. rtl_write_byte(rtlpriv, 0x4fd, bt_state);
  1351. }
  1352. return true;
  1353. }
  1354. ratio_tx = bt_tx * 1000 / polling;
  1355. ratio_pri = bt_pri * 1000 / polling;
  1356. rtlpcipriv->bt_coexist.ratio_tx = ratio_tx;
  1357. rtlpcipriv->bt_coexist.ratio_pri = ratio_pri;
  1358. if (bt_state && rtlpcipriv->bt_coexist.reg_bt_sco == 3) {
  1359. if ((ratio_tx < 30) && (ratio_pri < 30))
  1360. cur_service_type = BT_IDLE;
  1361. else if ((ratio_pri > 110) && (ratio_pri < 250))
  1362. cur_service_type = BT_SCO;
  1363. else if ((ratio_tx >= 200) && (ratio_pri >= 200))
  1364. cur_service_type = BT_BUSY;
  1365. else if ((ratio_tx >= 350) && (ratio_tx < 500))
  1366. cur_service_type = BT_OTHERBUSY;
  1367. else if (ratio_tx >= 500)
  1368. cur_service_type = BT_PAN;
  1369. else
  1370. cur_service_type = BT_OTHER_ACTION;
  1371. if (cur_service_type != rtlpcipriv->bt_coexist.bt_service) {
  1372. rtlpcipriv->bt_coexist.bt_service = cur_service_type;
  1373. bt_state = bt_state |
  1374. ((rtlpcipriv->bt_coexist.bt_ant_isolation == 1) ?
  1375. 0 : BIT_OFFSET_LEN_MASK_32(1, 1)) |
  1376. ((rtlpcipriv->bt_coexist.bt_service != BT_IDLE) ?
  1377. 0 : BIT_OFFSET_LEN_MASK_32(2, 1));
  1378. /* Add interrupt migration when bt is not ini
  1379. * idle state (no traffic). */
  1380. if (rtlpcipriv->bt_coexist.bt_service != BT_IDLE) {
  1381. rtl_write_word(rtlpriv, 0x504, 0x0ccc);
  1382. rtl_write_byte(rtlpriv, 0x506, 0x54);
  1383. rtl_write_byte(rtlpriv, 0x507, 0x54);
  1384. } else {
  1385. rtl_write_byte(rtlpriv, 0x506, 0x00);
  1386. rtl_write_byte(rtlpriv, 0x507, 0x00);
  1387. }
  1388. rtl_write_byte(rtlpriv, 0x4fd, bt_state);
  1389. return true;
  1390. }
  1391. }
  1392. return false;
  1393. }
  1394. static bool rtl92c_bt_wifi_connect_change(struct ieee80211_hw *hw)
  1395. {
  1396. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1397. static bool media_connect;
  1398. if (rtlpriv->mac80211.link_state < MAC80211_LINKED) {
  1399. media_connect = false;
  1400. } else {
  1401. if (!media_connect) {
  1402. media_connect = true;
  1403. return true;
  1404. }
  1405. media_connect = true;
  1406. }
  1407. return false;
  1408. }
  1409. static void rtl92c_bt_set_normal(struct ieee80211_hw *hw)
  1410. {
  1411. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1412. struct rtl_pci_priv *rtlpcipriv = rtl_pcipriv(hw);
  1413. if (rtlpcipriv->bt_coexist.bt_service == BT_OTHERBUSY) {
  1414. rtlpcipriv->bt_coexist.bt_edca_ul = 0x5ea72b;
  1415. rtlpcipriv->bt_coexist.bt_edca_dl = 0x5ea72b;
  1416. } else if (rtlpcipriv->bt_coexist.bt_service == BT_BUSY) {
  1417. rtlpcipriv->bt_coexist.bt_edca_ul = 0x5eb82f;
  1418. rtlpcipriv->bt_coexist.bt_edca_dl = 0x5eb82f;
  1419. } else if (rtlpcipriv->bt_coexist.bt_service == BT_SCO) {
  1420. if (rtlpcipriv->bt_coexist.ratio_tx > 160) {
  1421. rtlpcipriv->bt_coexist.bt_edca_ul = 0x5ea72f;
  1422. rtlpcipriv->bt_coexist.bt_edca_dl = 0x5ea72f;
  1423. } else {
  1424. rtlpcipriv->bt_coexist.bt_edca_ul = 0x5ea32b;
  1425. rtlpcipriv->bt_coexist.bt_edca_dl = 0x5ea42b;
  1426. }
  1427. } else {
  1428. rtlpcipriv->bt_coexist.bt_edca_ul = 0;
  1429. rtlpcipriv->bt_coexist.bt_edca_dl = 0;
  1430. }
  1431. if ((rtlpcipriv->bt_coexist.bt_service != BT_IDLE) &&
  1432. (rtlpriv->mac80211.mode == WIRELESS_MODE_G ||
  1433. (rtlpriv->mac80211.mode == (WIRELESS_MODE_G | WIRELESS_MODE_B))) &&
  1434. (rtlpcipriv->bt_coexist.bt_rssi_state &
  1435. BT_RSSI_STATE_BG_EDCA_LOW)) {
  1436. rtlpcipriv->bt_coexist.bt_edca_ul = 0x5eb82b;
  1437. rtlpcipriv->bt_coexist.bt_edca_dl = 0x5eb82b;
  1438. }
  1439. }
  1440. static void rtl92c_bt_ant_isolation(struct ieee80211_hw *hw, u8 tmp1byte)
  1441. {
  1442. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1443. struct rtl_pci_priv *rtlpcipriv = rtl_pcipriv(hw);
  1444. /* Only enable HW BT coexist when BT in "Busy" state. */
  1445. if (rtlpriv->mac80211.vendor == PEER_CISCO &&
  1446. rtlpcipriv->bt_coexist.bt_service == BT_OTHER_ACTION) {
  1447. rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG, 0xa0);
  1448. } else {
  1449. if ((rtlpcipriv->bt_coexist.bt_service == BT_BUSY) &&
  1450. (rtlpcipriv->bt_coexist.bt_rssi_state &
  1451. BT_RSSI_STATE_NORMAL_POWER)) {
  1452. rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG, 0xa0);
  1453. } else if ((rtlpcipriv->bt_coexist.bt_service ==
  1454. BT_OTHER_ACTION) && (rtlpriv->mac80211.mode <
  1455. WIRELESS_MODE_N_24G) &&
  1456. (rtlpcipriv->bt_coexist.bt_rssi_state &
  1457. BT_RSSI_STATE_SPECIAL_LOW)) {
  1458. rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG, 0xa0);
  1459. } else if (rtlpcipriv->bt_coexist.bt_service == BT_PAN) {
  1460. rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG, tmp1byte);
  1461. } else {
  1462. rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG, tmp1byte);
  1463. }
  1464. }
  1465. if (rtlpcipriv->bt_coexist.bt_service == BT_PAN)
  1466. rtl_write_dword(rtlpriv, REG_GPIO_PIN_CTRL, 0x10100);
  1467. else
  1468. rtl_write_dword(rtlpriv, REG_GPIO_PIN_CTRL, 0x0);
  1469. if (rtlpcipriv->bt_coexist.bt_rssi_state &
  1470. BT_RSSI_STATE_NORMAL_POWER) {
  1471. rtl92c_bt_set_normal(hw);
  1472. } else {
  1473. rtlpcipriv->bt_coexist.bt_edca_ul = 0;
  1474. rtlpcipriv->bt_coexist.bt_edca_dl = 0;
  1475. }
  1476. if (rtlpcipriv->bt_coexist.bt_service != BT_IDLE) {
  1477. rtlpriv->cfg->ops->set_rfreg(hw,
  1478. RF90_PATH_A,
  1479. 0x1e,
  1480. 0xf0, 0xf);
  1481. } else {
  1482. rtlpriv->cfg->ops->set_rfreg(hw,
  1483. RF90_PATH_A, 0x1e, 0xf0,
  1484. rtlpcipriv->bt_coexist.bt_rfreg_origin_1e);
  1485. }
  1486. if (!rtlpriv->dm.dynamic_txpower_enable) {
  1487. if (rtlpcipriv->bt_coexist.bt_service != BT_IDLE) {
  1488. if (rtlpcipriv->bt_coexist.bt_rssi_state &
  1489. BT_RSSI_STATE_TXPOWER_LOW) {
  1490. rtlpriv->dm.dynamic_txhighpower_lvl =
  1491. TXHIGHPWRLEVEL_BT2;
  1492. } else {
  1493. rtlpriv->dm.dynamic_txhighpower_lvl =
  1494. TXHIGHPWRLEVEL_BT1;
  1495. }
  1496. } else {
  1497. rtlpriv->dm.dynamic_txhighpower_lvl =
  1498. TXHIGHPWRLEVEL_NORMAL;
  1499. }
  1500. rtl92c_phy_set_txpower_level(hw,
  1501. rtlpriv->phy.current_channel);
  1502. }
  1503. }
  1504. static void rtl92c_check_bt_change(struct ieee80211_hw *hw)
  1505. {
  1506. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1507. struct rtl_pci_priv *rtlpcipriv = rtl_pcipriv(hw);
  1508. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1509. u8 tmp1byte = 0;
  1510. if (IS_81XXC_VENDOR_UMC_B_CUT(rtlhal->version) &&
  1511. rtlpcipriv->bt_coexist.bt_coexistence)
  1512. tmp1byte |= BIT(5);
  1513. if (rtlpcipriv->bt_coexist.bt_cur_state) {
  1514. if (rtlpcipriv->bt_coexist.bt_ant_isolation)
  1515. rtl92c_bt_ant_isolation(hw, tmp1byte);
  1516. } else {
  1517. rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG, tmp1byte);
  1518. rtlpriv->cfg->ops->set_rfreg(hw, RF90_PATH_A, 0x1e, 0xf0,
  1519. rtlpcipriv->bt_coexist.bt_rfreg_origin_1e);
  1520. rtlpcipriv->bt_coexist.bt_edca_ul = 0;
  1521. rtlpcipriv->bt_coexist.bt_edca_dl = 0;
  1522. }
  1523. }
  1524. void rtl92c_dm_bt_coexist(struct ieee80211_hw *hw)
  1525. {
  1526. struct rtl_pci_priv *rtlpcipriv = rtl_pcipriv(hw);
  1527. bool wifi_connect_change;
  1528. bool bt_state_change;
  1529. bool rssi_state_change;
  1530. if ((rtlpcipriv->bt_coexist.bt_coexistence) &&
  1531. (rtlpcipriv->bt_coexist.bt_coexist_type == BT_CSR_BC4)) {
  1532. wifi_connect_change = rtl92c_bt_wifi_connect_change(hw);
  1533. bt_state_change = rtl92c_bt_state_change(hw);
  1534. rssi_state_change = rtl92c_bt_rssi_state_change(hw);
  1535. if (wifi_connect_change || bt_state_change || rssi_state_change)
  1536. rtl92c_check_bt_change(hw);
  1537. }
  1538. }
  1539. EXPORT_SYMBOL(rtl92c_dm_bt_coexist);