base.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  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 "wifi.h"
  30. #include "rc.h"
  31. #include "base.h"
  32. #include "efuse.h"
  33. #include "cam.h"
  34. #include "ps.h"
  35. #include "regd.h"
  36. #include <linux/ip.h>
  37. #include <linux/module.h>
  38. #include <linux/udp.h>
  39. /*
  40. *NOTICE!!!: This file will be very big, we should
  41. *keep it clear under following roles:
  42. *
  43. *This file include following parts, so, if you add new
  44. *functions into this file, please check which part it
  45. *should includes. or check if you should add new part
  46. *for this file:
  47. *
  48. *1) mac80211 init functions
  49. *2) tx information functions
  50. *3) functions called by core.c
  51. *4) wq & timer callback functions
  52. *5) frame process functions
  53. *6) IOT functions
  54. *7) sysfs functions
  55. *8) vif functions
  56. *9) ...
  57. */
  58. /*********************************************************
  59. *
  60. * mac80211 init functions
  61. *
  62. *********************************************************/
  63. static struct ieee80211_channel rtl_channeltable_2g[] = {
  64. {.center_freq = 2412, .hw_value = 1,},
  65. {.center_freq = 2417, .hw_value = 2,},
  66. {.center_freq = 2422, .hw_value = 3,},
  67. {.center_freq = 2427, .hw_value = 4,},
  68. {.center_freq = 2432, .hw_value = 5,},
  69. {.center_freq = 2437, .hw_value = 6,},
  70. {.center_freq = 2442, .hw_value = 7,},
  71. {.center_freq = 2447, .hw_value = 8,},
  72. {.center_freq = 2452, .hw_value = 9,},
  73. {.center_freq = 2457, .hw_value = 10,},
  74. {.center_freq = 2462, .hw_value = 11,},
  75. {.center_freq = 2467, .hw_value = 12,},
  76. {.center_freq = 2472, .hw_value = 13,},
  77. {.center_freq = 2484, .hw_value = 14,},
  78. };
  79. static struct ieee80211_channel rtl_channeltable_5g[] = {
  80. {.center_freq = 5180, .hw_value = 36,},
  81. {.center_freq = 5200, .hw_value = 40,},
  82. {.center_freq = 5220, .hw_value = 44,},
  83. {.center_freq = 5240, .hw_value = 48,},
  84. {.center_freq = 5260, .hw_value = 52,},
  85. {.center_freq = 5280, .hw_value = 56,},
  86. {.center_freq = 5300, .hw_value = 60,},
  87. {.center_freq = 5320, .hw_value = 64,},
  88. {.center_freq = 5500, .hw_value = 100,},
  89. {.center_freq = 5520, .hw_value = 104,},
  90. {.center_freq = 5540, .hw_value = 108,},
  91. {.center_freq = 5560, .hw_value = 112,},
  92. {.center_freq = 5580, .hw_value = 116,},
  93. {.center_freq = 5600, .hw_value = 120,},
  94. {.center_freq = 5620, .hw_value = 124,},
  95. {.center_freq = 5640, .hw_value = 128,},
  96. {.center_freq = 5660, .hw_value = 132,},
  97. {.center_freq = 5680, .hw_value = 136,},
  98. {.center_freq = 5700, .hw_value = 140,},
  99. {.center_freq = 5745, .hw_value = 149,},
  100. {.center_freq = 5765, .hw_value = 153,},
  101. {.center_freq = 5785, .hw_value = 157,},
  102. {.center_freq = 5805, .hw_value = 161,},
  103. {.center_freq = 5825, .hw_value = 165,},
  104. };
  105. static struct ieee80211_rate rtl_ratetable_2g[] = {
  106. {.bitrate = 10, .hw_value = 0x00,},
  107. {.bitrate = 20, .hw_value = 0x01,},
  108. {.bitrate = 55, .hw_value = 0x02,},
  109. {.bitrate = 110, .hw_value = 0x03,},
  110. {.bitrate = 60, .hw_value = 0x04,},
  111. {.bitrate = 90, .hw_value = 0x05,},
  112. {.bitrate = 120, .hw_value = 0x06,},
  113. {.bitrate = 180, .hw_value = 0x07,},
  114. {.bitrate = 240, .hw_value = 0x08,},
  115. {.bitrate = 360, .hw_value = 0x09,},
  116. {.bitrate = 480, .hw_value = 0x0a,},
  117. {.bitrate = 540, .hw_value = 0x0b,},
  118. };
  119. static struct ieee80211_rate rtl_ratetable_5g[] = {
  120. {.bitrate = 60, .hw_value = 0x04,},
  121. {.bitrate = 90, .hw_value = 0x05,},
  122. {.bitrate = 120, .hw_value = 0x06,},
  123. {.bitrate = 180, .hw_value = 0x07,},
  124. {.bitrate = 240, .hw_value = 0x08,},
  125. {.bitrate = 360, .hw_value = 0x09,},
  126. {.bitrate = 480, .hw_value = 0x0a,},
  127. {.bitrate = 540, .hw_value = 0x0b,},
  128. };
  129. static const struct ieee80211_supported_band rtl_band_2ghz = {
  130. .band = IEEE80211_BAND_2GHZ,
  131. .channels = rtl_channeltable_2g,
  132. .n_channels = ARRAY_SIZE(rtl_channeltable_2g),
  133. .bitrates = rtl_ratetable_2g,
  134. .n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
  135. .ht_cap = {0},
  136. };
  137. static struct ieee80211_supported_band rtl_band_5ghz = {
  138. .band = IEEE80211_BAND_5GHZ,
  139. .channels = rtl_channeltable_5g,
  140. .n_channels = ARRAY_SIZE(rtl_channeltable_5g),
  141. .bitrates = rtl_ratetable_5g,
  142. .n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
  143. .ht_cap = {0},
  144. };
  145. static const u8 tid_to_ac[] = {
  146. 2, /* IEEE80211_AC_BE */
  147. 3, /* IEEE80211_AC_BK */
  148. 3, /* IEEE80211_AC_BK */
  149. 2, /* IEEE80211_AC_BE */
  150. 1, /* IEEE80211_AC_VI */
  151. 1, /* IEEE80211_AC_VI */
  152. 0, /* IEEE80211_AC_VO */
  153. 0, /* IEEE80211_AC_VO */
  154. };
  155. u8 rtl_tid_to_ac(u8 tid)
  156. {
  157. return tid_to_ac[tid];
  158. }
  159. EXPORT_SYMBOL_GPL(rtl_tid_to_ac);
  160. static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
  161. struct ieee80211_sta_ht_cap *ht_cap)
  162. {
  163. struct rtl_priv *rtlpriv = rtl_priv(hw);
  164. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  165. ht_cap->ht_supported = true;
  166. ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  167. IEEE80211_HT_CAP_SGI_40 |
  168. IEEE80211_HT_CAP_SGI_20 |
  169. IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
  170. if (rtlpriv->rtlhal.disable_amsdu_8k)
  171. ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
  172. /*
  173. *Maximum length of AMPDU that the STA can receive.
  174. *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
  175. */
  176. ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  177. /*Minimum MPDU start spacing , */
  178. ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
  179. ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  180. /*hw->wiphy->bands[IEEE80211_BAND_2GHZ]
  181. *base on ant_num
  182. *rx_mask: RX mask
  183. *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
  184. *if rx_ant = 2 rx_mask[1]= 0xff;==>MCS8-MCS15
  185. *if rx_ant >= 3 rx_mask[2]= 0xff;
  186. *if BW_40 rx_mask[4]= 0x01;
  187. *highest supported RX rate
  188. */
  189. if (rtlpriv->dm.supp_phymode_switch) {
  190. RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
  191. "Support phy mode switch\n");
  192. ht_cap->mcs.rx_mask[0] = 0xFF;
  193. ht_cap->mcs.rx_mask[1] = 0xFF;
  194. ht_cap->mcs.rx_mask[4] = 0x01;
  195. ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
  196. } else {
  197. if (get_rf_type(rtlphy) == RF_1T2R ||
  198. get_rf_type(rtlphy) == RF_2T2R) {
  199. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  200. "1T2R or 2T2R\n");
  201. ht_cap->mcs.rx_mask[0] = 0xFF;
  202. ht_cap->mcs.rx_mask[1] = 0xFF;
  203. ht_cap->mcs.rx_mask[4] = 0x01;
  204. ht_cap->mcs.rx_highest =
  205. cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
  206. } else if (get_rf_type(rtlphy) == RF_1T1R) {
  207. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T1R\n");
  208. ht_cap->mcs.rx_mask[0] = 0xFF;
  209. ht_cap->mcs.rx_mask[1] = 0x00;
  210. ht_cap->mcs.rx_mask[4] = 0x01;
  211. ht_cap->mcs.rx_highest =
  212. cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
  213. }
  214. }
  215. }
  216. static void _rtl_init_mac80211(struct ieee80211_hw *hw)
  217. {
  218. struct rtl_priv *rtlpriv = rtl_priv(hw);
  219. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  220. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  221. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  222. struct ieee80211_supported_band *sband;
  223. if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY && rtlhal->bandset ==
  224. BAND_ON_BOTH) {
  225. /* 1: 2.4 G bands */
  226. /* <1> use mac->bands as mem for hw->wiphy->bands */
  227. sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
  228. /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
  229. * to default value(1T1R) */
  230. memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
  231. sizeof(struct ieee80211_supported_band));
  232. /* <3> init ht cap base on ant_num */
  233. _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
  234. /* <4> set mac->sband to wiphy->sband */
  235. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
  236. /* 2: 5 G bands */
  237. /* <1> use mac->bands as mem for hw->wiphy->bands */
  238. sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
  239. /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
  240. * to default value(1T1R) */
  241. memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]), &rtl_band_5ghz,
  242. sizeof(struct ieee80211_supported_band));
  243. /* <3> init ht cap base on ant_num */
  244. _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
  245. /* <4> set mac->sband to wiphy->sband */
  246. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
  247. } else {
  248. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  249. /* <1> use mac->bands as mem for hw->wiphy->bands */
  250. sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
  251. /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
  252. * to default value(1T1R) */
  253. memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]),
  254. &rtl_band_2ghz,
  255. sizeof(struct ieee80211_supported_band));
  256. /* <3> init ht cap base on ant_num */
  257. _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
  258. /* <4> set mac->sband to wiphy->sband */
  259. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
  260. } else if (rtlhal->current_bandtype == BAND_ON_5G) {
  261. /* <1> use mac->bands as mem for hw->wiphy->bands */
  262. sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
  263. /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
  264. * to default value(1T1R) */
  265. memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]),
  266. &rtl_band_5ghz,
  267. sizeof(struct ieee80211_supported_band));
  268. /* <3> init ht cap base on ant_num */
  269. _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
  270. /* <4> set mac->sband to wiphy->sband */
  271. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
  272. } else {
  273. RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG, "Err BAND %d\n",
  274. rtlhal->current_bandtype);
  275. }
  276. }
  277. /* <5> set hw caps */
  278. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  279. IEEE80211_HW_RX_INCLUDES_FCS |
  280. IEEE80211_HW_AMPDU_AGGREGATION |
  281. IEEE80211_HW_CONNECTION_MONITOR |
  282. /* IEEE80211_HW_SUPPORTS_CQM_RSSI | */
  283. IEEE80211_HW_CONNECTION_MONITOR |
  284. IEEE80211_HW_MFP_CAPABLE |
  285. IEEE80211_HW_REPORTS_TX_ACK_STATUS | 0;
  286. /* swlps or hwlps has been set in diff chip in init_sw_vars */
  287. if (rtlpriv->psc.swctrl_lps)
  288. hw->flags |= IEEE80211_HW_SUPPORTS_PS |
  289. IEEE80211_HW_PS_NULLFUNC_STACK |
  290. /* IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
  291. 0;
  292. hw->wiphy->interface_modes =
  293. BIT(NL80211_IFTYPE_AP) |
  294. BIT(NL80211_IFTYPE_STATION) |
  295. BIT(NL80211_IFTYPE_ADHOC) |
  296. BIT(NL80211_IFTYPE_MESH_POINT) |
  297. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  298. BIT(NL80211_IFTYPE_P2P_GO);
  299. hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  300. hw->wiphy->rts_threshold = 2347;
  301. hw->queues = AC_MAX;
  302. hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
  303. /* TODO: Correct this value for our hw */
  304. /* TODO: define these hard code value */
  305. hw->channel_change_time = 100;
  306. hw->max_listen_interval = 10;
  307. hw->max_rate_tries = 4;
  308. /* hw->max_rates = 1; */
  309. hw->sta_data_size = sizeof(struct rtl_sta_info);
  310. /* <6> mac address */
  311. if (is_valid_ether_addr(rtlefuse->dev_addr)) {
  312. SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
  313. } else {
  314. u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
  315. get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
  316. SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
  317. }
  318. }
  319. static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
  320. {
  321. struct rtl_priv *rtlpriv = rtl_priv(hw);
  322. /* <1> timer */
  323. setup_timer(&rtlpriv->works.watchdog_timer,
  324. rtl_watch_dog_timer_callback, (unsigned long)hw);
  325. setup_timer(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
  326. rtl_easy_concurrent_retrytimer_callback, (unsigned long)hw);
  327. /* <2> work queue */
  328. rtlpriv->works.hw = hw;
  329. rtlpriv->works.rtl_wq = alloc_workqueue("%s", 0, 0, rtlpriv->cfg->name);
  330. INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
  331. (void *)rtl_watchdog_wq_callback);
  332. INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
  333. (void *)rtl_ips_nic_off_wq_callback);
  334. INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
  335. (void *)rtl_swlps_wq_callback);
  336. INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
  337. (void *)rtl_swlps_rfon_wq_callback);
  338. INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
  339. (void *)rtl_fwevt_wq_callback);
  340. }
  341. void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
  342. {
  343. struct rtl_priv *rtlpriv = rtl_priv(hw);
  344. del_timer_sync(&rtlpriv->works.watchdog_timer);
  345. cancel_delayed_work(&rtlpriv->works.watchdog_wq);
  346. cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
  347. cancel_delayed_work(&rtlpriv->works.ps_work);
  348. cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
  349. cancel_delayed_work(&rtlpriv->works.fwevt_wq);
  350. }
  351. EXPORT_SYMBOL_GPL(rtl_deinit_deferred_work);
  352. void rtl_init_rfkill(struct ieee80211_hw *hw)
  353. {
  354. struct rtl_priv *rtlpriv = rtl_priv(hw);
  355. bool radio_state;
  356. bool blocked;
  357. u8 valid = 0;
  358. /*set init state to on */
  359. rtlpriv->rfkill.rfkill_state = true;
  360. wiphy_rfkill_set_hw_state(hw->wiphy, 0);
  361. radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
  362. if (valid) {
  363. pr_info("wireless switch is %s\n",
  364. rtlpriv->rfkill.rfkill_state ? "on" : "off");
  365. rtlpriv->rfkill.rfkill_state = radio_state;
  366. blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
  367. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  368. }
  369. wiphy_rfkill_start_polling(hw->wiphy);
  370. }
  371. EXPORT_SYMBOL(rtl_init_rfkill);
  372. void rtl_deinit_rfkill(struct ieee80211_hw *hw)
  373. {
  374. wiphy_rfkill_stop_polling(hw->wiphy);
  375. }
  376. EXPORT_SYMBOL_GPL(rtl_deinit_rfkill);
  377. int rtl_init_core(struct ieee80211_hw *hw)
  378. {
  379. struct rtl_priv *rtlpriv = rtl_priv(hw);
  380. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  381. /* <1> init mac80211 */
  382. _rtl_init_mac80211(hw);
  383. rtlmac->hw = hw;
  384. /* <2> rate control register */
  385. hw->rate_control_algorithm = "rtl_rc";
  386. /*
  387. * <3> init CRDA must come after init
  388. * mac80211 hw in _rtl_init_mac80211.
  389. */
  390. if (rtl_regd_init(hw, rtl_reg_notifier)) {
  391. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "REGD init failed\n");
  392. return 1;
  393. }
  394. /* <4> locks */
  395. mutex_init(&rtlpriv->locks.conf_mutex);
  396. mutex_init(&rtlpriv->locks.ps_mutex);
  397. spin_lock_init(&rtlpriv->locks.ips_lock);
  398. spin_lock_init(&rtlpriv->locks.irq_th_lock);
  399. spin_lock_init(&rtlpriv->locks.irq_pci_lock);
  400. spin_lock_init(&rtlpriv->locks.tx_lock);
  401. spin_lock_init(&rtlpriv->locks.h2c_lock);
  402. spin_lock_init(&rtlpriv->locks.rf_ps_lock);
  403. spin_lock_init(&rtlpriv->locks.rf_lock);
  404. spin_lock_init(&rtlpriv->locks.waitq_lock);
  405. spin_lock_init(&rtlpriv->locks.entry_list_lock);
  406. spin_lock_init(&rtlpriv->locks.fw_ps_lock);
  407. spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
  408. spin_lock_init(&rtlpriv->locks.check_sendpkt_lock);
  409. spin_lock_init(&rtlpriv->locks.fw_ps_lock);
  410. spin_lock_init(&rtlpriv->locks.lps_lock);
  411. /* <5> init list */
  412. INIT_LIST_HEAD(&rtlpriv->entry_list);
  413. rtlmac->link_state = MAC80211_NOLINK;
  414. /* <6> init deferred work */
  415. _rtl_init_deferred_work(hw);
  416. return 0;
  417. }
  418. EXPORT_SYMBOL_GPL(rtl_init_core);
  419. void rtl_deinit_core(struct ieee80211_hw *hw)
  420. {
  421. }
  422. EXPORT_SYMBOL_GPL(rtl_deinit_core);
  423. void rtl_init_rx_config(struct ieee80211_hw *hw)
  424. {
  425. struct rtl_priv *rtlpriv = rtl_priv(hw);
  426. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  427. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
  428. }
  429. EXPORT_SYMBOL_GPL(rtl_init_rx_config);
  430. /*********************************************************
  431. *
  432. * tx information functions
  433. *
  434. *********************************************************/
  435. static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
  436. struct rtl_tcb_desc *tcb_desc,
  437. struct ieee80211_tx_info *info)
  438. {
  439. struct rtl_priv *rtlpriv = rtl_priv(hw);
  440. u8 rate_flag = info->control.rates[0].flags;
  441. tcb_desc->use_shortpreamble = false;
  442. /* 1M can only use Long Preamble. 11B spec */
  443. if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
  444. return;
  445. else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  446. tcb_desc->use_shortpreamble = true;
  447. return;
  448. }
  449. static void _rtl_query_shortgi(struct ieee80211_hw *hw,
  450. struct ieee80211_sta *sta,
  451. struct rtl_tcb_desc *tcb_desc,
  452. struct ieee80211_tx_info *info)
  453. {
  454. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  455. u8 rate_flag = info->control.rates[0].flags;
  456. u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
  457. tcb_desc->use_shortgi = false;
  458. if (sta == NULL)
  459. return;
  460. sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
  461. sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
  462. if (!(sta->ht_cap.ht_supported))
  463. return;
  464. if (!sgi_40 && !sgi_20)
  465. return;
  466. if (mac->opmode == NL80211_IFTYPE_STATION)
  467. bw_40 = mac->bw_40;
  468. else if (mac->opmode == NL80211_IFTYPE_AP ||
  469. mac->opmode == NL80211_IFTYPE_ADHOC ||
  470. mac->opmode == NL80211_IFTYPE_MESH_POINT)
  471. bw_40 = sta->bandwidth >= IEEE80211_STA_RX_BW_40;
  472. if (bw_40 && sgi_40)
  473. tcb_desc->use_shortgi = true;
  474. else if ((bw_40 == false) && sgi_20)
  475. tcb_desc->use_shortgi = true;
  476. if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
  477. tcb_desc->use_shortgi = false;
  478. }
  479. static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
  480. struct rtl_tcb_desc *tcb_desc,
  481. struct ieee80211_tx_info *info)
  482. {
  483. struct rtl_priv *rtlpriv = rtl_priv(hw);
  484. u8 rate_flag = info->control.rates[0].flags;
  485. /* Common Settings */
  486. tcb_desc->rts_stbc = false;
  487. tcb_desc->cts_enable = false;
  488. tcb_desc->rts_sc = 0;
  489. tcb_desc->rts_bw = false;
  490. tcb_desc->rts_use_shortpreamble = false;
  491. tcb_desc->rts_use_shortgi = false;
  492. if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  493. /* Use CTS-to-SELF in protection mode. */
  494. tcb_desc->rts_enable = true;
  495. tcb_desc->cts_enable = true;
  496. tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
  497. } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
  498. /* Use RTS-CTS in protection mode. */
  499. tcb_desc->rts_enable = true;
  500. tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
  501. }
  502. }
  503. static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
  504. struct ieee80211_sta *sta,
  505. struct rtl_tcb_desc *tcb_desc)
  506. {
  507. struct rtl_priv *rtlpriv = rtl_priv(hw);
  508. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  509. struct rtl_sta_info *sta_entry = NULL;
  510. u8 ratr_index = 7;
  511. if (sta) {
  512. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  513. ratr_index = sta_entry->ratr_index;
  514. }
  515. if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
  516. if (mac->opmode == NL80211_IFTYPE_STATION) {
  517. tcb_desc->ratr_index = 0;
  518. } else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
  519. mac->opmode == NL80211_IFTYPE_MESH_POINT) {
  520. if (tcb_desc->multicast || tcb_desc->broadcast) {
  521. tcb_desc->hw_rate =
  522. rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
  523. tcb_desc->use_driver_rate = 1;
  524. tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
  525. } else {
  526. tcb_desc->ratr_index = ratr_index;
  527. }
  528. } else if (mac->opmode == NL80211_IFTYPE_AP) {
  529. tcb_desc->ratr_index = ratr_index;
  530. }
  531. }
  532. if (rtlpriv->dm.useramask) {
  533. tcb_desc->ratr_index = ratr_index;
  534. /* TODO we will differentiate adhoc and station future */
  535. if (mac->opmode == NL80211_IFTYPE_STATION ||
  536. mac->opmode == NL80211_IFTYPE_MESH_POINT) {
  537. tcb_desc->mac_id = 0;
  538. if (mac->mode == WIRELESS_MODE_N_24G)
  539. tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
  540. else if (mac->mode == WIRELESS_MODE_N_5G)
  541. tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
  542. else if (mac->mode & WIRELESS_MODE_G)
  543. tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
  544. else if (mac->mode & WIRELESS_MODE_B)
  545. tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
  546. else if (mac->mode & WIRELESS_MODE_A)
  547. tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
  548. } else if (mac->opmode == NL80211_IFTYPE_AP ||
  549. mac->opmode == NL80211_IFTYPE_ADHOC) {
  550. if (NULL != sta) {
  551. if (sta->aid > 0)
  552. tcb_desc->mac_id = sta->aid + 1;
  553. else
  554. tcb_desc->mac_id = 1;
  555. } else {
  556. tcb_desc->mac_id = 0;
  557. }
  558. }
  559. }
  560. }
  561. static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
  562. struct ieee80211_sta *sta,
  563. struct rtl_tcb_desc *tcb_desc)
  564. {
  565. struct rtl_priv *rtlpriv = rtl_priv(hw);
  566. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  567. tcb_desc->packet_bw = false;
  568. if (!sta)
  569. return;
  570. if (mac->opmode == NL80211_IFTYPE_AP ||
  571. mac->opmode == NL80211_IFTYPE_ADHOC ||
  572. mac->opmode == NL80211_IFTYPE_MESH_POINT) {
  573. if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
  574. return;
  575. } else if (mac->opmode == NL80211_IFTYPE_STATION) {
  576. if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
  577. return;
  578. }
  579. if (tcb_desc->multicast || tcb_desc->broadcast)
  580. return;
  581. /*use legency rate, shall use 20MHz */
  582. if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
  583. return;
  584. tcb_desc->packet_bw = true;
  585. }
  586. static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
  587. {
  588. struct rtl_priv *rtlpriv = rtl_priv(hw);
  589. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  590. u8 hw_rate;
  591. if (get_rf_type(rtlphy) == RF_2T2R)
  592. hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
  593. else
  594. hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
  595. return hw_rate;
  596. }
  597. /* mac80211's rate_idx is like this:
  598. *
  599. * 2.4G band:rx_status->band == IEEE80211_BAND_2GHZ
  600. *
  601. * B/G rate:
  602. * (rx_status->flag & RX_FLAG_HT) = 0,
  603. * DESC92_RATE1M-->DESC92_RATE54M ==> idx is 0-->11,
  604. *
  605. * N rate:
  606. * (rx_status->flag & RX_FLAG_HT) = 1,
  607. * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
  608. *
  609. * 5G band:rx_status->band == IEEE80211_BAND_5GHZ
  610. * A rate:
  611. * (rx_status->flag & RX_FLAG_HT) = 0,
  612. * DESC92_RATE6M-->DESC92_RATE54M ==> idx is 0-->7,
  613. *
  614. * N rate:
  615. * (rx_status->flag & RX_FLAG_HT) = 1,
  616. * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
  617. */
  618. int rtlwifi_rate_mapping(struct ieee80211_hw *hw,
  619. bool isht, u8 desc_rate, bool first_ampdu)
  620. {
  621. int rate_idx;
  622. if (false == isht) {
  623. if (IEEE80211_BAND_2GHZ == hw->conf.chandef.chan->band) {
  624. switch (desc_rate) {
  625. case DESC92_RATE1M:
  626. rate_idx = 0;
  627. break;
  628. case DESC92_RATE2M:
  629. rate_idx = 1;
  630. break;
  631. case DESC92_RATE5_5M:
  632. rate_idx = 2;
  633. break;
  634. case DESC92_RATE11M:
  635. rate_idx = 3;
  636. break;
  637. case DESC92_RATE6M:
  638. rate_idx = 4;
  639. break;
  640. case DESC92_RATE9M:
  641. rate_idx = 5;
  642. break;
  643. case DESC92_RATE12M:
  644. rate_idx = 6;
  645. break;
  646. case DESC92_RATE18M:
  647. rate_idx = 7;
  648. break;
  649. case DESC92_RATE24M:
  650. rate_idx = 8;
  651. break;
  652. case DESC92_RATE36M:
  653. rate_idx = 9;
  654. break;
  655. case DESC92_RATE48M:
  656. rate_idx = 10;
  657. break;
  658. case DESC92_RATE54M:
  659. rate_idx = 11;
  660. break;
  661. default:
  662. rate_idx = 0;
  663. break;
  664. }
  665. } else {
  666. switch (desc_rate) {
  667. case DESC92_RATE6M:
  668. rate_idx = 0;
  669. break;
  670. case DESC92_RATE9M:
  671. rate_idx = 1;
  672. break;
  673. case DESC92_RATE12M:
  674. rate_idx = 2;
  675. break;
  676. case DESC92_RATE18M:
  677. rate_idx = 3;
  678. break;
  679. case DESC92_RATE24M:
  680. rate_idx = 4;
  681. break;
  682. case DESC92_RATE36M:
  683. rate_idx = 5;
  684. break;
  685. case DESC92_RATE48M:
  686. rate_idx = 6;
  687. break;
  688. case DESC92_RATE54M:
  689. rate_idx = 7;
  690. break;
  691. default:
  692. rate_idx = 0;
  693. break;
  694. }
  695. }
  696. } else {
  697. switch (desc_rate) {
  698. case DESC92_RATEMCS0:
  699. rate_idx = 0;
  700. break;
  701. case DESC92_RATEMCS1:
  702. rate_idx = 1;
  703. break;
  704. case DESC92_RATEMCS2:
  705. rate_idx = 2;
  706. break;
  707. case DESC92_RATEMCS3:
  708. rate_idx = 3;
  709. break;
  710. case DESC92_RATEMCS4:
  711. rate_idx = 4;
  712. break;
  713. case DESC92_RATEMCS5:
  714. rate_idx = 5;
  715. break;
  716. case DESC92_RATEMCS6:
  717. rate_idx = 6;
  718. break;
  719. case DESC92_RATEMCS7:
  720. rate_idx = 7;
  721. break;
  722. case DESC92_RATEMCS8:
  723. rate_idx = 8;
  724. break;
  725. case DESC92_RATEMCS9:
  726. rate_idx = 9;
  727. break;
  728. case DESC92_RATEMCS10:
  729. rate_idx = 10;
  730. break;
  731. case DESC92_RATEMCS11:
  732. rate_idx = 11;
  733. break;
  734. case DESC92_RATEMCS12:
  735. rate_idx = 12;
  736. break;
  737. case DESC92_RATEMCS13:
  738. rate_idx = 13;
  739. break;
  740. case DESC92_RATEMCS14:
  741. rate_idx = 14;
  742. break;
  743. case DESC92_RATEMCS15:
  744. rate_idx = 15;
  745. break;
  746. default:
  747. rate_idx = 0;
  748. break;
  749. }
  750. }
  751. return rate_idx;
  752. }
  753. EXPORT_SYMBOL(rtlwifi_rate_mapping);
  754. bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
  755. {
  756. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  757. struct rtl_priv *rtlpriv = rtl_priv(hw);
  758. __le16 fc = rtl_get_fc(skb);
  759. if (rtlpriv->dm.supp_phymode_switch &&
  760. mac->link_state < MAC80211_LINKED &&
  761. (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
  762. if (rtlpriv->cfg->ops->chk_switch_dmdp)
  763. rtlpriv->cfg->ops->chk_switch_dmdp(hw);
  764. }
  765. if (ieee80211_is_auth(fc)) {
  766. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
  767. rtl_ips_nic_on(hw);
  768. mac->link_state = MAC80211_LINKING;
  769. /* Dual mac */
  770. rtlpriv->phy.need_iqk = true;
  771. }
  772. return true;
  773. }
  774. EXPORT_SYMBOL_GPL(rtl_tx_mgmt_proc);
  775. void rtl_get_tcb_desc(struct ieee80211_hw *hw,
  776. struct ieee80211_tx_info *info,
  777. struct ieee80211_sta *sta,
  778. struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
  779. {
  780. struct rtl_priv *rtlpriv = rtl_priv(hw);
  781. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  782. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  783. struct ieee80211_rate *txrate;
  784. __le16 fc = hdr->frame_control;
  785. txrate = ieee80211_get_tx_rate(hw, info);
  786. if (txrate)
  787. tcb_desc->hw_rate = txrate->hw_value;
  788. else
  789. tcb_desc->hw_rate = 0;
  790. if (ieee80211_is_data(fc)) {
  791. /*
  792. *we set data rate INX 0
  793. *in rtl_rc.c if skb is special data or
  794. *mgt which need low data rate.
  795. */
  796. /*
  797. *So tcb_desc->hw_rate is just used for
  798. *special data and mgt frames
  799. */
  800. if (info->control.rates[0].idx == 0 ||
  801. ieee80211_is_nullfunc(fc)) {
  802. tcb_desc->use_driver_rate = true;
  803. tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
  804. tcb_desc->disable_ratefallback = 1;
  805. } else {
  806. /*
  807. *because hw will nerver use hw_rate
  808. *when tcb_desc->use_driver_rate = false
  809. *so we never set highest N rate here,
  810. *and N rate will all be controlled by FW
  811. *when tcb_desc->use_driver_rate = false
  812. */
  813. if (sta && (sta->ht_cap.ht_supported)) {
  814. tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
  815. } else {
  816. if (rtlmac->mode == WIRELESS_MODE_B) {
  817. tcb_desc->hw_rate =
  818. rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
  819. } else {
  820. tcb_desc->hw_rate =
  821. rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
  822. }
  823. }
  824. }
  825. if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
  826. tcb_desc->multicast = 1;
  827. else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  828. tcb_desc->broadcast = 1;
  829. _rtl_txrate_selectmode(hw, sta, tcb_desc);
  830. _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
  831. _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
  832. _rtl_query_shortgi(hw, sta, tcb_desc, info);
  833. _rtl_query_protection_mode(hw, tcb_desc, info);
  834. } else {
  835. tcb_desc->use_driver_rate = true;
  836. tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
  837. tcb_desc->disable_ratefallback = 1;
  838. tcb_desc->mac_id = 0;
  839. tcb_desc->packet_bw = false;
  840. }
  841. }
  842. EXPORT_SYMBOL(rtl_get_tcb_desc);
  843. static bool addbareq_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
  844. {
  845. struct rtl_priv *rtlpriv = rtl_priv(hw);
  846. struct ieee80211_sta *sta = NULL;
  847. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  848. struct rtl_sta_info *sta_entry = NULL;
  849. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  850. u16 capab = 0, tid = 0;
  851. struct rtl_tid_data *tid_data;
  852. struct sk_buff *skb_delba = NULL;
  853. struct ieee80211_rx_status rx_status = { 0 };
  854. rcu_read_lock();
  855. sta = rtl_find_sta(hw, hdr->addr3);
  856. if (sta == NULL) {
  857. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_EMERG,
  858. "sta is NULL\n");
  859. rcu_read_unlock();
  860. return true;
  861. }
  862. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  863. if (!sta_entry) {
  864. rcu_read_unlock();
  865. return true;
  866. }
  867. capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
  868. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  869. tid_data = &sta_entry->tids[tid];
  870. if (tid_data->agg.rx_agg_state == RTL_RX_AGG_START) {
  871. skb_delba = rtl_make_del_ba(hw, hdr->addr2, hdr->addr3, tid);
  872. if (skb_delba) {
  873. rx_status.freq = hw->conf.chandef.chan->center_freq;
  874. rx_status.band = hw->conf.chandef.chan->band;
  875. rx_status.flag |= RX_FLAG_DECRYPTED;
  876. rx_status.flag |= RX_FLAG_MACTIME_END;
  877. rx_status.rate_idx = 0;
  878. rx_status.signal = 50 + 10;
  879. memcpy(IEEE80211_SKB_RXCB(skb_delba), &rx_status,
  880. sizeof(rx_status));
  881. RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
  882. "fake del\n", skb_delba->data,
  883. skb_delba->len);
  884. ieee80211_rx_irqsafe(hw, skb_delba);
  885. }
  886. }
  887. rcu_read_unlock();
  888. return false;
  889. }
  890. bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
  891. {
  892. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  893. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  894. struct rtl_priv *rtlpriv = rtl_priv(hw);
  895. __le16 fc = hdr->frame_control;
  896. u8 *act = (u8 *)skb->data + MAC80211_3ADDR_LEN;
  897. u8 category;
  898. if (!ieee80211_is_action(fc))
  899. return true;
  900. category = *act;
  901. act++;
  902. switch (category) {
  903. case ACT_CAT_BA:
  904. switch (*act) {
  905. case ACT_ADDBAREQ:
  906. if (mac->act_scanning)
  907. return false;
  908. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  909. "%s ACT_ADDBAREQ From :%pM\n",
  910. is_tx ? "Tx" : "Rx", hdr->addr2);
  911. RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
  912. skb->data, skb->len);
  913. if (!is_tx)
  914. if (addbareq_rx(hw, skb))
  915. return true;
  916. break;
  917. case ACT_ADDBARSP:
  918. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  919. "%s ACT_ADDBARSP From :%pM\n",
  920. is_tx ? "Tx" : "Rx", hdr->addr2);
  921. break;
  922. case ACT_DELBA:
  923. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  924. "ACT_ADDBADEL From :%pM\n", hdr->addr2);
  925. break;
  926. }
  927. break;
  928. default:
  929. break;
  930. }
  931. return true;
  932. }
  933. EXPORT_SYMBOL_GPL(rtl_action_proc);
  934. /*should call before software enc*/
  935. u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
  936. {
  937. struct rtl_priv *rtlpriv = rtl_priv(hw);
  938. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  939. __le16 fc = rtl_get_fc(skb);
  940. u16 ether_type;
  941. u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
  942. const struct iphdr *ip;
  943. if (!ieee80211_is_data(fc))
  944. return false;
  945. ip = (const struct iphdr *)(skb->data + mac_hdr_len +
  946. SNAP_SIZE + PROTOC_TYPE_SIZE);
  947. ether_type = be16_to_cpup((__be16 *)
  948. (skb->data + mac_hdr_len + SNAP_SIZE));
  949. switch (ether_type) {
  950. case ETH_P_IP: {
  951. struct udphdr *udp;
  952. u16 src;
  953. u16 dst;
  954. if (ip->protocol != IPPROTO_UDP)
  955. return false;
  956. udp = (struct udphdr *)((u8 *)ip + (ip->ihl << 2));
  957. src = be16_to_cpu(udp->source);
  958. dst = be16_to_cpu(udp->dest);
  959. /* If this case involves port 68 (UDP BOOTP client) connecting
  960. * with port 67 (UDP BOOTP server), then return true so that
  961. * the lowest speed is used.
  962. */
  963. if (!((src == 68 && dst == 67) || (src == 67 && dst == 68)))
  964. return false;
  965. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  966. "dhcp %s !!\n", is_tx ? "Tx" : "Rx");
  967. break;
  968. }
  969. case ETH_P_ARP:
  970. break;
  971. case ETH_P_PAE:
  972. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  973. "802.1X %s EAPOL pkt!!\n", is_tx ? "Tx" : "Rx");
  974. break;
  975. case ETH_P_IPV6:
  976. /* TODO: Is this right? */
  977. return false;
  978. default:
  979. return false;
  980. }
  981. if (is_tx) {
  982. rtlpriv->enter_ps = false;
  983. schedule_work(&rtlpriv->works.lps_change_work);
  984. ppsc->last_delaylps_stamp_jiffies = jiffies;
  985. }
  986. return true;
  987. }
  988. EXPORT_SYMBOL_GPL(rtl_is_special_data);
  989. /*********************************************************
  990. *
  991. * functions called by core.c
  992. *
  993. *********************************************************/
  994. int rtl_tx_agg_start(struct ieee80211_hw *hw,
  995. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  996. {
  997. struct rtl_priv *rtlpriv = rtl_priv(hw);
  998. struct rtl_tid_data *tid_data;
  999. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1000. struct rtl_sta_info *sta_entry = NULL;
  1001. if (sta == NULL)
  1002. return -EINVAL;
  1003. if (unlikely(tid >= MAX_TID_COUNT))
  1004. return -EINVAL;
  1005. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1006. if (!sta_entry)
  1007. return -ENXIO;
  1008. tid_data = &sta_entry->tids[tid];
  1009. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d seq:%d\n",
  1010. sta->addr, tid, tid_data->seq_number);
  1011. *ssn = tid_data->seq_number;
  1012. tid_data->agg.agg_state = RTL_AGG_START;
  1013. ieee80211_start_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
  1014. return 0;
  1015. }
  1016. int rtl_tx_agg_stop(struct ieee80211_hw *hw,
  1017. struct ieee80211_sta *sta, u16 tid)
  1018. {
  1019. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1020. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1021. struct rtl_sta_info *sta_entry = NULL;
  1022. if (sta == NULL)
  1023. return -EINVAL;
  1024. if (!sta->addr) {
  1025. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
  1026. return -EINVAL;
  1027. }
  1028. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
  1029. sta->addr, tid);
  1030. if (unlikely(tid >= MAX_TID_COUNT))
  1031. return -EINVAL;
  1032. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1033. sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
  1034. ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
  1035. return 0;
  1036. }
  1037. int rtl_rx_agg_start(struct ieee80211_hw *hw,
  1038. struct ieee80211_sta *sta, u16 tid)
  1039. {
  1040. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1041. struct rtl_tid_data *tid_data;
  1042. struct rtl_sta_info *sta_entry = NULL;
  1043. if (sta == NULL)
  1044. return -EINVAL;
  1045. if (unlikely(tid >= MAX_TID_COUNT))
  1046. return -EINVAL;
  1047. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1048. if (!sta_entry)
  1049. return -ENXIO;
  1050. tid_data = &sta_entry->tids[tid];
  1051. RT_TRACE(rtlpriv, COMP_RECV, DBG_DMESG,
  1052. "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
  1053. tid_data->seq_number);
  1054. tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
  1055. return 0;
  1056. }
  1057. int rtl_rx_agg_stop(struct ieee80211_hw *hw,
  1058. struct ieee80211_sta *sta, u16 tid)
  1059. {
  1060. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1061. struct rtl_sta_info *sta_entry = NULL;
  1062. if (sta == NULL)
  1063. return -EINVAL;
  1064. if (!sta->addr) {
  1065. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
  1066. return -EINVAL;
  1067. }
  1068. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
  1069. "on ra = %pM tid = %d\n", sta->addr, tid);
  1070. if (unlikely(tid >= MAX_TID_COUNT))
  1071. return -EINVAL;
  1072. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1073. sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
  1074. return 0;
  1075. }
  1076. int rtl_tx_agg_oper(struct ieee80211_hw *hw,
  1077. struct ieee80211_sta *sta, u16 tid)
  1078. {
  1079. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1080. struct rtl_sta_info *sta_entry = NULL;
  1081. if (sta == NULL)
  1082. return -EINVAL;
  1083. if (!sta->addr) {
  1084. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
  1085. return -EINVAL;
  1086. }
  1087. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
  1088. sta->addr, tid);
  1089. if (unlikely(tid >= MAX_TID_COUNT))
  1090. return -EINVAL;
  1091. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1092. sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
  1093. return 0;
  1094. }
  1095. /*********************************************************
  1096. *
  1097. * wq & timer callback functions
  1098. *
  1099. *********************************************************/
  1100. /* this function is used for roaming */
  1101. void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
  1102. {
  1103. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1104. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1105. if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
  1106. return;
  1107. if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
  1108. return;
  1109. /* check if this really is a beacon */
  1110. if (!ieee80211_is_beacon(hdr->frame_control) &&
  1111. !ieee80211_is_probe_resp(hdr->frame_control))
  1112. return;
  1113. /* min. beacon length + FCS_LEN */
  1114. if (skb->len <= 40 + FCS_LEN)
  1115. return;
  1116. /* and only beacons from the associated BSSID, please */
  1117. if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
  1118. return;
  1119. rtlpriv->link_info.bcn_rx_inperiod++;
  1120. }
  1121. EXPORT_SYMBOL_GPL(rtl_beacon_statistic);
  1122. void rtl_watchdog_wq_callback(void *data)
  1123. {
  1124. struct rtl_works *rtlworks = container_of_dwork_rtl(data,
  1125. struct rtl_works,
  1126. watchdog_wq);
  1127. struct ieee80211_hw *hw = rtlworks->hw;
  1128. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1129. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1130. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1131. bool busytraffic = false;
  1132. bool tx_busy_traffic = false;
  1133. bool rx_busy_traffic = false;
  1134. bool higher_busytraffic = false;
  1135. bool higher_busyrxtraffic = false;
  1136. u8 idx, tid;
  1137. u32 rx_cnt_inp4eriod = 0;
  1138. u32 tx_cnt_inp4eriod = 0;
  1139. u32 aver_rx_cnt_inperiod = 0;
  1140. u32 aver_tx_cnt_inperiod = 0;
  1141. u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
  1142. u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
  1143. if (is_hal_stop(rtlhal))
  1144. return;
  1145. /* <1> Determine if action frame is allowed */
  1146. if (mac->link_state > MAC80211_NOLINK) {
  1147. if (mac->cnt_after_linked < 20)
  1148. mac->cnt_after_linked++;
  1149. } else {
  1150. mac->cnt_after_linked = 0;
  1151. }
  1152. /*
  1153. *<2> to check if traffic busy, if
  1154. * busytraffic we don't change channel
  1155. */
  1156. if (mac->link_state >= MAC80211_LINKED) {
  1157. /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
  1158. for (idx = 0; idx <= 2; idx++) {
  1159. rtlpriv->link_info.num_rx_in4period[idx] =
  1160. rtlpriv->link_info.num_rx_in4period[idx + 1];
  1161. rtlpriv->link_info.num_tx_in4period[idx] =
  1162. rtlpriv->link_info.num_tx_in4period[idx + 1];
  1163. }
  1164. rtlpriv->link_info.num_rx_in4period[3] =
  1165. rtlpriv->link_info.num_rx_inperiod;
  1166. rtlpriv->link_info.num_tx_in4period[3] =
  1167. rtlpriv->link_info.num_tx_inperiod;
  1168. for (idx = 0; idx <= 3; idx++) {
  1169. rx_cnt_inp4eriod +=
  1170. rtlpriv->link_info.num_rx_in4period[idx];
  1171. tx_cnt_inp4eriod +=
  1172. rtlpriv->link_info.num_tx_in4period[idx];
  1173. }
  1174. aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
  1175. aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
  1176. /* (2) check traffic busy */
  1177. if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
  1178. busytraffic = true;
  1179. if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
  1180. rx_busy_traffic = true;
  1181. else
  1182. tx_busy_traffic = false;
  1183. }
  1184. /* Higher Tx/Rx data. */
  1185. if (aver_rx_cnt_inperiod > 4000 ||
  1186. aver_tx_cnt_inperiod > 4000) {
  1187. higher_busytraffic = true;
  1188. /* Extremely high Rx data. */
  1189. if (aver_rx_cnt_inperiod > 5000)
  1190. higher_busyrxtraffic = true;
  1191. }
  1192. /* check every tid's tx traffic */
  1193. for (tid = 0; tid <= 7; tid++) {
  1194. for (idx = 0; idx <= 2; idx++)
  1195. rtlpriv->link_info.tidtx_in4period[tid][idx] =
  1196. rtlpriv->link_info.tidtx_in4period[tid]
  1197. [idx + 1];
  1198. rtlpriv->link_info.tidtx_in4period[tid][3] =
  1199. rtlpriv->link_info.tidtx_inperiod[tid];
  1200. for (idx = 0; idx <= 3; idx++)
  1201. tidtx_inp4eriod[tid] +=
  1202. rtlpriv->link_info.tidtx_in4period[tid][idx];
  1203. aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
  1204. if (aver_tidtx_inperiod[tid] > 5000)
  1205. rtlpriv->link_info.higher_busytxtraffic[tid] =
  1206. true;
  1207. else
  1208. rtlpriv->link_info.higher_busytxtraffic[tid] =
  1209. false;
  1210. }
  1211. if (((rtlpriv->link_info.num_rx_inperiod +
  1212. rtlpriv->link_info.num_tx_inperiod) > 8) ||
  1213. (rtlpriv->link_info.num_rx_inperiod > 2))
  1214. rtlpriv->enter_ps = true;
  1215. else
  1216. rtlpriv->enter_ps = false;
  1217. /* LeisurePS only work in infra mode. */
  1218. schedule_work(&rtlpriv->works.lps_change_work);
  1219. }
  1220. rtlpriv->link_info.num_rx_inperiod = 0;
  1221. rtlpriv->link_info.num_tx_inperiod = 0;
  1222. for (tid = 0; tid <= 7; tid++)
  1223. rtlpriv->link_info.tidtx_inperiod[tid] = 0;
  1224. rtlpriv->link_info.busytraffic = busytraffic;
  1225. rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
  1226. rtlpriv->link_info.rx_busy_traffic = rx_busy_traffic;
  1227. rtlpriv->link_info.tx_busy_traffic = tx_busy_traffic;
  1228. rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
  1229. /* <3> DM */
  1230. rtlpriv->cfg->ops->dm_watchdog(hw);
  1231. /* <4> roaming */
  1232. if (mac->link_state == MAC80211_LINKED &&
  1233. mac->opmode == NL80211_IFTYPE_STATION) {
  1234. if ((rtlpriv->link_info.bcn_rx_inperiod +
  1235. rtlpriv->link_info.num_rx_inperiod) == 0) {
  1236. rtlpriv->link_info.roam_times++;
  1237. RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
  1238. "AP off for %d s\n",
  1239. (rtlpriv->link_info.roam_times * 2));
  1240. /* if we can't recv beacon for 6s, we should
  1241. * reconnect this AP
  1242. */
  1243. if (rtlpriv->link_info.roam_times >= 3) {
  1244. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1245. "AP off, try to reconnect now\n");
  1246. rtlpriv->link_info.roam_times = 0;
  1247. ieee80211_connection_loss(rtlpriv->mac80211.vif);
  1248. }
  1249. } else {
  1250. rtlpriv->link_info.roam_times = 0;
  1251. }
  1252. }
  1253. rtlpriv->link_info.bcn_rx_inperiod = 0;
  1254. }
  1255. void rtl_watch_dog_timer_callback(unsigned long data)
  1256. {
  1257. struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
  1258. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1259. queue_delayed_work(rtlpriv->works.rtl_wq,
  1260. &rtlpriv->works.watchdog_wq, 0);
  1261. mod_timer(&rtlpriv->works.watchdog_timer,
  1262. jiffies + MSECS(RTL_WATCH_DOG_TIME));
  1263. }
  1264. void rtl_fwevt_wq_callback(void *data)
  1265. {
  1266. struct rtl_works *rtlworks =
  1267. container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
  1268. struct ieee80211_hw *hw = rtlworks->hw;
  1269. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1270. rtlpriv->cfg->ops->c2h_command_handle(hw);
  1271. }
  1272. void rtl_easy_concurrent_retrytimer_callback(unsigned long data)
  1273. {
  1274. struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
  1275. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1276. struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
  1277. if (buddy_priv == NULL)
  1278. return;
  1279. rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
  1280. }
  1281. /*********************************************************
  1282. *
  1283. * frame process functions
  1284. *
  1285. *********************************************************/
  1286. u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
  1287. {
  1288. struct ieee80211_mgmt *mgmt = (void *)data;
  1289. u8 *pos, *end;
  1290. pos = (u8 *)mgmt->u.beacon.variable;
  1291. end = data + len;
  1292. while (pos < end) {
  1293. if (pos + 2 + pos[1] > end)
  1294. return NULL;
  1295. if (pos[0] == ie)
  1296. return pos;
  1297. pos += 2 + pos[1];
  1298. }
  1299. return NULL;
  1300. }
  1301. /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
  1302. /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
  1303. static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
  1304. enum ieee80211_smps_mode smps, u8 *da, u8 *bssid)
  1305. {
  1306. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1307. struct sk_buff *skb;
  1308. struct ieee80211_mgmt *action_frame;
  1309. /* 27 = header + category + action + smps mode */
  1310. skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
  1311. if (!skb)
  1312. return NULL;
  1313. skb_reserve(skb, hw->extra_tx_headroom);
  1314. action_frame = (void *)skb_put(skb, 27);
  1315. memset(action_frame, 0, 27);
  1316. memcpy(action_frame->da, da, ETH_ALEN);
  1317. memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
  1318. memcpy(action_frame->bssid, bssid, ETH_ALEN);
  1319. action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1320. IEEE80211_STYPE_ACTION);
  1321. action_frame->u.action.category = WLAN_CATEGORY_HT;
  1322. action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
  1323. switch (smps) {
  1324. case IEEE80211_SMPS_AUTOMATIC:/* 0 */
  1325. case IEEE80211_SMPS_NUM_MODES:/* 4 */
  1326. WARN_ON(1);
  1327. case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
  1328. action_frame->u.action.u.ht_smps.smps_control =
  1329. WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
  1330. break;
  1331. case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
  1332. action_frame->u.action.u.ht_smps.smps_control =
  1333. WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
  1334. break;
  1335. case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
  1336. action_frame->u.action.u.ht_smps.smps_control =
  1337. WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
  1338. break;
  1339. }
  1340. return skb;
  1341. }
  1342. int rtl_send_smps_action(struct ieee80211_hw *hw,
  1343. struct ieee80211_sta *sta,
  1344. enum ieee80211_smps_mode smps)
  1345. {
  1346. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1347. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1348. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  1349. struct sk_buff *skb = NULL;
  1350. struct rtl_tcb_desc tcb_desc;
  1351. u8 bssid[ETH_ALEN] = {0};
  1352. memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
  1353. if (rtlpriv->mac80211.act_scanning)
  1354. goto err_free;
  1355. if (!sta)
  1356. goto err_free;
  1357. if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
  1358. goto err_free;
  1359. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  1360. goto err_free;
  1361. if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
  1362. memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
  1363. else
  1364. memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
  1365. skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
  1366. /* this is a type = mgmt * stype = action frame */
  1367. if (skb) {
  1368. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1369. struct rtl_sta_info *sta_entry =
  1370. (struct rtl_sta_info *) sta->drv_priv;
  1371. sta_entry->mimo_ps = smps;
  1372. info->control.rates[0].idx = 0;
  1373. info->band = hw->conf.chandef.chan->band;
  1374. rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
  1375. }
  1376. return 1;
  1377. err_free:
  1378. return 0;
  1379. }
  1380. EXPORT_SYMBOL(rtl_send_smps_action);
  1381. void rtl_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
  1382. {
  1383. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1384. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1385. enum io_type iotype;
  1386. if (!is_hal_stop(rtlhal)) {
  1387. switch (operation) {
  1388. case SCAN_OPT_BACKUP:
  1389. iotype = IO_CMD_PAUSE_DM_BY_SCAN;
  1390. rtlpriv->cfg->ops->set_hw_reg(hw,
  1391. HW_VAR_IO_CMD,
  1392. (u8 *)&iotype);
  1393. break;
  1394. case SCAN_OPT_RESTORE:
  1395. iotype = IO_CMD_RESUME_DM_BY_SCAN;
  1396. rtlpriv->cfg->ops->set_hw_reg(hw,
  1397. HW_VAR_IO_CMD,
  1398. (u8 *)&iotype);
  1399. break;
  1400. default:
  1401. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1402. "Unknown Scan Backup operation.\n");
  1403. break;
  1404. }
  1405. }
  1406. }
  1407. EXPORT_SYMBOL(rtl_phy_scan_operation_backup);
  1408. /* There seem to be issues in mac80211 regarding when del ba frames can be
  1409. * received. As a work around, we make a fake del_ba if we receive a ba_req;
  1410. * however, rx_agg was opened to let mac80211 release some ba related
  1411. * resources. This del_ba is for tx only.
  1412. */
  1413. struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
  1414. u8 *sa, u8 *bssid, u16 tid)
  1415. {
  1416. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1417. struct sk_buff *skb;
  1418. struct ieee80211_mgmt *action_frame;
  1419. u16 params;
  1420. /* 27 = header + category + action + smps mode */
  1421. skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
  1422. if (!skb)
  1423. return NULL;
  1424. skb_reserve(skb, hw->extra_tx_headroom);
  1425. action_frame = (void *)skb_put(skb, 34);
  1426. memset(action_frame, 0, 34);
  1427. memcpy(action_frame->sa, sa, ETH_ALEN);
  1428. memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
  1429. memcpy(action_frame->bssid, bssid, ETH_ALEN);
  1430. action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1431. IEEE80211_STYPE_ACTION);
  1432. action_frame->u.action.category = WLAN_CATEGORY_BACK;
  1433. action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
  1434. params = (u16)(1 << 11); /* bit 11 initiator */
  1435. params |= (u16)(tid << 12); /* bit 15:12 TID number */
  1436. action_frame->u.action.u.delba.params = cpu_to_le16(params);
  1437. action_frame->u.action.u.delba.reason_code =
  1438. cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
  1439. return skb;
  1440. }
  1441. /*********************************************************
  1442. *
  1443. * IOT functions
  1444. *
  1445. *********************************************************/
  1446. static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
  1447. struct octet_string vendor_ie)
  1448. {
  1449. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1450. bool matched = false;
  1451. static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
  1452. static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
  1453. static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
  1454. static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
  1455. static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
  1456. static u8 racap[] = { 0x00, 0x0c, 0x43 };
  1457. static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
  1458. static u8 marvcap[] = { 0x00, 0x50, 0x43 };
  1459. if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
  1460. memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
  1461. rtlpriv->mac80211.vendor = PEER_ATH;
  1462. matched = true;
  1463. } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
  1464. memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
  1465. memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
  1466. rtlpriv->mac80211.vendor = PEER_BROAD;
  1467. matched = true;
  1468. } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
  1469. rtlpriv->mac80211.vendor = PEER_RAL;
  1470. matched = true;
  1471. } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
  1472. rtlpriv->mac80211.vendor = PEER_CISCO;
  1473. matched = true;
  1474. } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
  1475. rtlpriv->mac80211.vendor = PEER_MARV;
  1476. matched = true;
  1477. }
  1478. return matched;
  1479. }
  1480. static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
  1481. unsigned int len)
  1482. {
  1483. struct ieee80211_mgmt *mgmt = (void *)data;
  1484. struct octet_string vendor_ie;
  1485. u8 *pos, *end;
  1486. pos = (u8 *)mgmt->u.beacon.variable;
  1487. end = data + len;
  1488. while (pos < end) {
  1489. if (pos[0] == 221) {
  1490. vendor_ie.length = pos[1];
  1491. vendor_ie.octet = &pos[2];
  1492. if (rtl_chk_vendor_ouisub(hw, vendor_ie))
  1493. return true;
  1494. }
  1495. if (pos + 2 + pos[1] > end)
  1496. return false;
  1497. pos += 2 + pos[1];
  1498. }
  1499. return false;
  1500. }
  1501. void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
  1502. {
  1503. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1504. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1505. struct ieee80211_hdr *hdr = (void *)data;
  1506. u32 vendor = PEER_UNKNOWN;
  1507. static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
  1508. static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
  1509. static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
  1510. static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
  1511. static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
  1512. static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
  1513. static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
  1514. static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
  1515. static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
  1516. static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
  1517. static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
  1518. static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
  1519. static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
  1520. static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
  1521. static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
  1522. static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
  1523. if (mac->opmode != NL80211_IFTYPE_STATION)
  1524. return;
  1525. if (mac->link_state == MAC80211_NOLINK) {
  1526. mac->vendor = PEER_UNKNOWN;
  1527. return;
  1528. }
  1529. if (mac->cnt_after_linked > 2)
  1530. return;
  1531. /* check if this really is a beacon */
  1532. if (!ieee80211_is_beacon(hdr->frame_control))
  1533. return;
  1534. /* min. beacon length + FCS_LEN */
  1535. if (len <= 40 + FCS_LEN)
  1536. return;
  1537. /* and only beacons from the associated BSSID, please */
  1538. if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
  1539. return;
  1540. if (rtl_find_221_ie(hw, data, len))
  1541. vendor = mac->vendor;
  1542. if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
  1543. (memcmp(mac->bssid, ap5_2, 3) == 0) ||
  1544. (memcmp(mac->bssid, ap5_3, 3) == 0) ||
  1545. (memcmp(mac->bssid, ap5_4, 3) == 0) ||
  1546. (memcmp(mac->bssid, ap5_5, 3) == 0) ||
  1547. (memcmp(mac->bssid, ap5_6, 3) == 0) ||
  1548. vendor == PEER_ATH) {
  1549. vendor = PEER_ATH;
  1550. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
  1551. } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
  1552. (memcmp(mac->bssid, ap4_5, 3) == 0) ||
  1553. (memcmp(mac->bssid, ap4_1, 3) == 0) ||
  1554. (memcmp(mac->bssid, ap4_2, 3) == 0) ||
  1555. (memcmp(mac->bssid, ap4_3, 3) == 0) ||
  1556. vendor == PEER_RAL) {
  1557. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
  1558. vendor = PEER_RAL;
  1559. } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
  1560. vendor == PEER_CISCO) {
  1561. vendor = PEER_CISCO;
  1562. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
  1563. } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
  1564. (memcmp(mac->bssid, ap3_2, 3) == 0) ||
  1565. (memcmp(mac->bssid, ap3_3, 3) == 0) ||
  1566. vendor == PEER_BROAD) {
  1567. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
  1568. vendor = PEER_BROAD;
  1569. } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
  1570. vendor == PEER_MARV) {
  1571. vendor = PEER_MARV;
  1572. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
  1573. }
  1574. mac->vendor = vendor;
  1575. }
  1576. EXPORT_SYMBOL_GPL(rtl_recognize_peer);
  1577. /*********************************************************
  1578. *
  1579. * sysfs functions
  1580. *
  1581. *********************************************************/
  1582. static ssize_t rtl_show_debug_level(struct device *d,
  1583. struct device_attribute *attr, char *buf)
  1584. {
  1585. struct ieee80211_hw *hw = dev_get_drvdata(d);
  1586. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1587. return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
  1588. }
  1589. static ssize_t rtl_store_debug_level(struct device *d,
  1590. struct device_attribute *attr,
  1591. const char *buf, size_t count)
  1592. {
  1593. struct ieee80211_hw *hw = dev_get_drvdata(d);
  1594. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1595. unsigned long val;
  1596. int ret;
  1597. ret = kstrtoul(buf, 0, &val);
  1598. if (ret) {
  1599. printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
  1600. } else {
  1601. rtlpriv->dbg.global_debuglevel = val;
  1602. printk(KERN_DEBUG "debuglevel:%x\n",
  1603. rtlpriv->dbg.global_debuglevel);
  1604. }
  1605. return strnlen(buf, count);
  1606. }
  1607. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  1608. rtl_show_debug_level, rtl_store_debug_level);
  1609. static struct attribute *rtl_sysfs_entries[] = {
  1610. &dev_attr_debug_level.attr,
  1611. NULL
  1612. };
  1613. /*
  1614. * "name" is folder name witch will be
  1615. * put in device directory like :
  1616. * sys/devices/pci0000:00/0000:00:1c.4/
  1617. * 0000:06:00.0/rtl_sysfs
  1618. */
  1619. struct attribute_group rtl_attribute_group = {
  1620. .name = "rtlsysfs",
  1621. .attrs = rtl_sysfs_entries,
  1622. };
  1623. EXPORT_SYMBOL_GPL(rtl_attribute_group);
  1624. MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
  1625. MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
  1626. MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
  1627. MODULE_LICENSE("GPL");
  1628. MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
  1629. struct rtl_global_var rtl_global_var = {};
  1630. EXPORT_SYMBOL_GPL(rtl_global_var);
  1631. static int __init rtl_core_module_init(void)
  1632. {
  1633. if (rtl_rate_control_register())
  1634. pr_err("Unable to register rtl_rc, use default RC !!\n");
  1635. /* init some global vars */
  1636. INIT_LIST_HEAD(&rtl_global_var.glb_priv_list);
  1637. spin_lock_init(&rtl_global_var.glb_list_lock);
  1638. return 0;
  1639. }
  1640. static void __exit rtl_core_module_exit(void)
  1641. {
  1642. /*RC*/
  1643. rtl_rate_control_unregister();
  1644. }
  1645. module_init(rtl_core_module_init);
  1646. module_exit(rtl_core_module_exit);