base.c 51 KB

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