base.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2010 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/ip.h>
  31. #include <linux/module.h>
  32. #include "wifi.h"
  33. #include "rc.h"
  34. #include "base.h"
  35. #include "efuse.h"
  36. #include "cam.h"
  37. #include "ps.h"
  38. #include "regd.h"
  39. /*
  40. *NOTICE!!!: This file will be very big, we hsould
  41. *keep it clear under follwing roles:
  42. *
  43. *This file include follwing part, 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) ...
  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(struct ieee80211_hw *hw, 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. /*
  179. *hw->wiphy->bands[IEEE80211_BAND_2GHZ]
  180. *base on ant_num
  181. *rx_mask: RX mask
  182. *if rx_ant =1 rx_mask[0]=0xff;==>MCS0-MCS7
  183. *if rx_ant =2 rx_mask[1]=0xff;==>MCS8-MCS15
  184. *if rx_ant >=3 rx_mask[2]=0xff;
  185. *if BW_40 rx_mask[4]=0x01;
  186. *highest supported RX rate
  187. */
  188. if (get_rf_type(rtlphy) == RF_1T2R || get_rf_type(rtlphy) == RF_2T2R) {
  189. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T2R or 2T2R\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 if (get_rf_type(rtlphy) == RF_1T1R) {
  195. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T1R\n");
  196. ht_cap->mcs.rx_mask[0] = 0xFF;
  197. ht_cap->mcs.rx_mask[1] = 0x00;
  198. ht_cap->mcs.rx_mask[4] = 0x01;
  199. ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
  200. }
  201. }
  202. static void _rtl_init_mac80211(struct ieee80211_hw *hw)
  203. {
  204. struct rtl_priv *rtlpriv = rtl_priv(hw);
  205. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  206. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  207. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  208. struct ieee80211_supported_band *sband;
  209. if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY && rtlhal->bandset ==
  210. BAND_ON_BOTH) {
  211. /* 1: 2.4 G bands */
  212. /* <1> use mac->bands as mem for hw->wiphy->bands */
  213. sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
  214. /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
  215. * to default value(1T1R) */
  216. memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
  217. sizeof(struct ieee80211_supported_band));
  218. /* <3> init ht cap base on ant_num */
  219. _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
  220. /* <4> set mac->sband to wiphy->sband */
  221. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
  222. /* 2: 5 G bands */
  223. /* <1> use mac->bands as mem for hw->wiphy->bands */
  224. sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
  225. /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
  226. * to default value(1T1R) */
  227. memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]), &rtl_band_5ghz,
  228. sizeof(struct ieee80211_supported_band));
  229. /* <3> init ht cap base on ant_num */
  230. _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
  231. /* <4> set mac->sband to wiphy->sband */
  232. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
  233. } else {
  234. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  235. /* <1> use mac->bands as mem for hw->wiphy->bands */
  236. sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
  237. /* <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
  238. * to default value(1T1R) */
  239. memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]),
  240. &rtl_band_2ghz,
  241. sizeof(struct ieee80211_supported_band));
  242. /* <3> init ht cap base on ant_num */
  243. _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
  244. /* <4> set mac->sband to wiphy->sband */
  245. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
  246. } else if (rtlhal->current_bandtype == BAND_ON_5G) {
  247. /* <1> use mac->bands as mem for hw->wiphy->bands */
  248. sband = &(rtlmac->bands[IEEE80211_BAND_5GHZ]);
  249. /* <2> set hw->wiphy->bands[IEEE80211_BAND_5GHZ]
  250. * to default value(1T1R) */
  251. memcpy(&(rtlmac->bands[IEEE80211_BAND_5GHZ]),
  252. &rtl_band_5ghz,
  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_5GHZ] = sband;
  258. } else {
  259. RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG, "Err BAND %d\n",
  260. rtlhal->current_bandtype);
  261. }
  262. }
  263. /* <5> set hw caps */
  264. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  265. IEEE80211_HW_RX_INCLUDES_FCS |
  266. IEEE80211_HW_BEACON_FILTER |
  267. IEEE80211_HW_AMPDU_AGGREGATION |
  268. IEEE80211_HW_CONNECTION_MONITOR |
  269. /* IEEE80211_HW_SUPPORTS_CQM_RSSI | */
  270. IEEE80211_HW_REPORTS_TX_ACK_STATUS | 0;
  271. /* swlps or hwlps has been set in diff chip in init_sw_vars */
  272. if (rtlpriv->psc.swctrl_lps)
  273. hw->flags |= IEEE80211_HW_SUPPORTS_PS |
  274. IEEE80211_HW_PS_NULLFUNC_STACK |
  275. /* IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
  276. 0;
  277. hw->wiphy->interface_modes =
  278. BIT(NL80211_IFTYPE_AP) |
  279. BIT(NL80211_IFTYPE_STATION) |
  280. BIT(NL80211_IFTYPE_ADHOC);
  281. hw->wiphy->rts_threshold = 2347;
  282. hw->queues = AC_MAX;
  283. hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
  284. /* TODO: Correct this value for our hw */
  285. /* TODO: define these hard code value */
  286. hw->channel_change_time = 100;
  287. hw->max_listen_interval = 10;
  288. hw->max_rate_tries = 4;
  289. /* hw->max_rates = 1; */
  290. hw->sta_data_size = sizeof(struct rtl_sta_info);
  291. /* <6> mac address */
  292. if (is_valid_ether_addr(rtlefuse->dev_addr)) {
  293. SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
  294. } else {
  295. u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
  296. get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
  297. SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
  298. }
  299. }
  300. static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
  301. {
  302. struct rtl_priv *rtlpriv = rtl_priv(hw);
  303. /* <1> timer */
  304. init_timer(&rtlpriv->works.watchdog_timer);
  305. setup_timer(&rtlpriv->works.watchdog_timer,
  306. rtl_watch_dog_timer_callback, (unsigned long)hw);
  307. /* <2> work queue */
  308. rtlpriv->works.hw = hw;
  309. rtlpriv->works.rtl_wq = alloc_workqueue(rtlpriv->cfg->name, 0, 0);
  310. INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
  311. (void *)rtl_watchdog_wq_callback);
  312. INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
  313. (void *)rtl_ips_nic_off_wq_callback);
  314. INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
  315. (void *)rtl_swlps_wq_callback);
  316. INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
  317. (void *)rtl_swlps_rfon_wq_callback);
  318. }
  319. void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
  320. {
  321. struct rtl_priv *rtlpriv = rtl_priv(hw);
  322. del_timer_sync(&rtlpriv->works.watchdog_timer);
  323. cancel_delayed_work(&rtlpriv->works.watchdog_wq);
  324. cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
  325. cancel_delayed_work(&rtlpriv->works.ps_work);
  326. cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
  327. }
  328. void rtl_init_rfkill(struct ieee80211_hw *hw)
  329. {
  330. struct rtl_priv *rtlpriv = rtl_priv(hw);
  331. bool radio_state;
  332. bool blocked;
  333. u8 valid = 0;
  334. /*set init state to on */
  335. rtlpriv->rfkill.rfkill_state = true;
  336. wiphy_rfkill_set_hw_state(hw->wiphy, 0);
  337. radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
  338. if (valid) {
  339. pr_info("wireless switch is %s\n",
  340. rtlpriv->rfkill.rfkill_state ? "on" : "off");
  341. rtlpriv->rfkill.rfkill_state = radio_state;
  342. blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
  343. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  344. }
  345. wiphy_rfkill_start_polling(hw->wiphy);
  346. }
  347. void rtl_deinit_rfkill(struct ieee80211_hw *hw)
  348. {
  349. wiphy_rfkill_stop_polling(hw->wiphy);
  350. }
  351. int rtl_init_core(struct ieee80211_hw *hw)
  352. {
  353. struct rtl_priv *rtlpriv = rtl_priv(hw);
  354. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  355. /* <1> init mac80211 */
  356. _rtl_init_mac80211(hw);
  357. rtlmac->hw = hw;
  358. /* <2> rate control register */
  359. hw->rate_control_algorithm = "rtl_rc";
  360. /*
  361. * <3> init CRDA must come after init
  362. * mac80211 hw in _rtl_init_mac80211.
  363. */
  364. if (rtl_regd_init(hw, rtl_reg_notifier)) {
  365. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "REGD init failed\n");
  366. return 1;
  367. } else {
  368. /* CRDA regd hint must after init CRDA */
  369. if (regulatory_hint(hw->wiphy, rtlpriv->regd.alpha2)) {
  370. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  371. "regulatory_hint fail\n");
  372. }
  373. }
  374. /* <4> locks */
  375. mutex_init(&rtlpriv->locks.conf_mutex);
  376. mutex_init(&rtlpriv->locks.ps_mutex);
  377. spin_lock_init(&rtlpriv->locks.ips_lock);
  378. spin_lock_init(&rtlpriv->locks.irq_th_lock);
  379. spin_lock_init(&rtlpriv->locks.h2c_lock);
  380. spin_lock_init(&rtlpriv->locks.rf_ps_lock);
  381. spin_lock_init(&rtlpriv->locks.rf_lock);
  382. spin_lock_init(&rtlpriv->locks.waitq_lock);
  383. spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
  384. rtlmac->link_state = MAC80211_NOLINK;
  385. /* <5> init deferred work */
  386. _rtl_init_deferred_work(hw);
  387. return 0;
  388. }
  389. void rtl_deinit_core(struct ieee80211_hw *hw)
  390. {
  391. }
  392. void rtl_init_rx_config(struct ieee80211_hw *hw)
  393. {
  394. struct rtl_priv *rtlpriv = rtl_priv(hw);
  395. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  396. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
  397. }
  398. /*********************************************************
  399. *
  400. * tx information functions
  401. *
  402. *********************************************************/
  403. static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
  404. struct rtl_tcb_desc *tcb_desc,
  405. struct ieee80211_tx_info *info)
  406. {
  407. struct rtl_priv *rtlpriv = rtl_priv(hw);
  408. u8 rate_flag = info->control.rates[0].flags;
  409. tcb_desc->use_shortpreamble = false;
  410. /* 1M can only use Long Preamble. 11B spec */
  411. if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
  412. return;
  413. else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  414. tcb_desc->use_shortpreamble = true;
  415. return;
  416. }
  417. static void _rtl_query_shortgi(struct ieee80211_hw *hw,
  418. struct ieee80211_sta *sta,
  419. struct rtl_tcb_desc *tcb_desc,
  420. struct ieee80211_tx_info *info)
  421. {
  422. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  423. u8 rate_flag = info->control.rates[0].flags;
  424. u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
  425. tcb_desc->use_shortgi = false;
  426. if (sta == NULL)
  427. return;
  428. sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
  429. sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
  430. if (!(sta->ht_cap.ht_supported))
  431. return;
  432. if (!sgi_40 && !sgi_20)
  433. return;
  434. if (mac->opmode == NL80211_IFTYPE_STATION)
  435. bw_40 = mac->bw_40;
  436. else if (mac->opmode == NL80211_IFTYPE_AP ||
  437. mac->opmode == NL80211_IFTYPE_ADHOC)
  438. bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  439. if (bw_40 && sgi_40)
  440. tcb_desc->use_shortgi = true;
  441. else if ((bw_40 == false) && sgi_20)
  442. tcb_desc->use_shortgi = true;
  443. if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
  444. tcb_desc->use_shortgi = false;
  445. }
  446. static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
  447. struct rtl_tcb_desc *tcb_desc,
  448. struct ieee80211_tx_info *info)
  449. {
  450. struct rtl_priv *rtlpriv = rtl_priv(hw);
  451. u8 rate_flag = info->control.rates[0].flags;
  452. /* Common Settings */
  453. tcb_desc->rts_stbc = false;
  454. tcb_desc->cts_enable = false;
  455. tcb_desc->rts_sc = 0;
  456. tcb_desc->rts_bw = false;
  457. tcb_desc->rts_use_shortpreamble = false;
  458. tcb_desc->rts_use_shortgi = false;
  459. if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  460. /* Use CTS-to-SELF in protection mode. */
  461. tcb_desc->rts_enable = true;
  462. tcb_desc->cts_enable = true;
  463. tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
  464. } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
  465. /* Use RTS-CTS in protection mode. */
  466. tcb_desc->rts_enable = true;
  467. tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
  468. }
  469. }
  470. static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
  471. struct ieee80211_sta *sta,
  472. struct rtl_tcb_desc *tcb_desc)
  473. {
  474. struct rtl_priv *rtlpriv = rtl_priv(hw);
  475. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  476. struct rtl_sta_info *sta_entry = NULL;
  477. u8 ratr_index = 7;
  478. if (sta) {
  479. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  480. ratr_index = sta_entry->ratr_index;
  481. }
  482. if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
  483. if (mac->opmode == NL80211_IFTYPE_STATION) {
  484. tcb_desc->ratr_index = 0;
  485. } else if (mac->opmode == NL80211_IFTYPE_ADHOC) {
  486. if (tcb_desc->multicast || tcb_desc->broadcast) {
  487. tcb_desc->hw_rate =
  488. rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
  489. tcb_desc->use_driver_rate = 1;
  490. } else {
  491. /* TODO */
  492. }
  493. tcb_desc->ratr_index = ratr_index;
  494. } else if (mac->opmode == NL80211_IFTYPE_AP) {
  495. tcb_desc->ratr_index = ratr_index;
  496. }
  497. }
  498. if (rtlpriv->dm.useramask) {
  499. /* TODO we will differentiate adhoc and station futrue */
  500. if (mac->opmode == NL80211_IFTYPE_STATION) {
  501. tcb_desc->mac_id = 0;
  502. if (mac->mode == WIRELESS_MODE_N_24G)
  503. tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
  504. else if (mac->mode == WIRELESS_MODE_N_5G)
  505. tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
  506. else if (mac->mode & WIRELESS_MODE_G)
  507. tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
  508. else if (mac->mode & WIRELESS_MODE_B)
  509. tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
  510. else if (mac->mode & WIRELESS_MODE_A)
  511. tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
  512. } else if (mac->opmode == NL80211_IFTYPE_AP ||
  513. mac->opmode == NL80211_IFTYPE_ADHOC) {
  514. if (NULL != sta) {
  515. if (sta->aid > 0)
  516. tcb_desc->mac_id = sta->aid + 1;
  517. else
  518. tcb_desc->mac_id = 1;
  519. } else {
  520. tcb_desc->mac_id = 0;
  521. }
  522. }
  523. }
  524. }
  525. static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
  526. struct ieee80211_sta *sta,
  527. struct rtl_tcb_desc *tcb_desc)
  528. {
  529. struct rtl_priv *rtlpriv = rtl_priv(hw);
  530. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  531. tcb_desc->packet_bw = false;
  532. if (!sta)
  533. return;
  534. if (mac->opmode == NL80211_IFTYPE_AP ||
  535. mac->opmode == NL80211_IFTYPE_ADHOC) {
  536. if (!(sta->ht_cap.ht_supported) ||
  537. !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  538. return;
  539. } else if (mac->opmode == NL80211_IFTYPE_STATION) {
  540. if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
  541. return;
  542. }
  543. if (tcb_desc->multicast || tcb_desc->broadcast)
  544. return;
  545. /*use legency rate, shall use 20MHz */
  546. if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
  547. return;
  548. tcb_desc->packet_bw = true;
  549. }
  550. static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
  551. {
  552. struct rtl_priv *rtlpriv = rtl_priv(hw);
  553. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  554. u8 hw_rate;
  555. if (get_rf_type(rtlphy) == RF_2T2R)
  556. hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
  557. else
  558. hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
  559. return hw_rate;
  560. }
  561. /* mac80211's rate_idx is like this:
  562. *
  563. * 2.4G band:rx_status->band == IEEE80211_BAND_2GHZ
  564. *
  565. * B/G rate:
  566. * (rx_status->flag & RX_FLAG_HT) = 0,
  567. * DESC92_RATE1M-->DESC92_RATE54M ==> idx is 0-->11,
  568. *
  569. * N rate:
  570. * (rx_status->flag & RX_FLAG_HT) = 1,
  571. * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
  572. *
  573. * 5G band:rx_status->band == IEEE80211_BAND_5GHZ
  574. * A rate:
  575. * (rx_status->flag & RX_FLAG_HT) = 0,
  576. * DESC92_RATE6M-->DESC92_RATE54M ==> idx is 0-->7,
  577. *
  578. * N rate:
  579. * (rx_status->flag & RX_FLAG_HT) = 1,
  580. * DESC92_RATEMCS0-->DESC92_RATEMCS15 ==> idx is 0-->15
  581. */
  582. int rtlwifi_rate_mapping(struct ieee80211_hw *hw,
  583. bool isht, u8 desc_rate, bool first_ampdu)
  584. {
  585. int rate_idx;
  586. if (false == isht) {
  587. if (IEEE80211_BAND_2GHZ == hw->conf.channel->band) {
  588. switch (desc_rate) {
  589. case DESC92_RATE1M:
  590. rate_idx = 0;
  591. break;
  592. case DESC92_RATE2M:
  593. rate_idx = 1;
  594. break;
  595. case DESC92_RATE5_5M:
  596. rate_idx = 2;
  597. break;
  598. case DESC92_RATE11M:
  599. rate_idx = 3;
  600. break;
  601. case DESC92_RATE6M:
  602. rate_idx = 4;
  603. break;
  604. case DESC92_RATE9M:
  605. rate_idx = 5;
  606. break;
  607. case DESC92_RATE12M:
  608. rate_idx = 6;
  609. break;
  610. case DESC92_RATE18M:
  611. rate_idx = 7;
  612. break;
  613. case DESC92_RATE24M:
  614. rate_idx = 8;
  615. break;
  616. case DESC92_RATE36M:
  617. rate_idx = 9;
  618. break;
  619. case DESC92_RATE48M:
  620. rate_idx = 10;
  621. break;
  622. case DESC92_RATE54M:
  623. rate_idx = 11;
  624. break;
  625. default:
  626. rate_idx = 0;
  627. break;
  628. }
  629. } else {
  630. switch (desc_rate) {
  631. case DESC92_RATE6M:
  632. rate_idx = 0;
  633. break;
  634. case DESC92_RATE9M:
  635. rate_idx = 1;
  636. break;
  637. case DESC92_RATE12M:
  638. rate_idx = 2;
  639. break;
  640. case DESC92_RATE18M:
  641. rate_idx = 3;
  642. break;
  643. case DESC92_RATE24M:
  644. rate_idx = 4;
  645. break;
  646. case DESC92_RATE36M:
  647. rate_idx = 5;
  648. break;
  649. case DESC92_RATE48M:
  650. rate_idx = 6;
  651. break;
  652. case DESC92_RATE54M:
  653. rate_idx = 7;
  654. break;
  655. default:
  656. rate_idx = 0;
  657. break;
  658. }
  659. }
  660. } else {
  661. switch (desc_rate) {
  662. case DESC92_RATEMCS0:
  663. rate_idx = 0;
  664. break;
  665. case DESC92_RATEMCS1:
  666. rate_idx = 1;
  667. break;
  668. case DESC92_RATEMCS2:
  669. rate_idx = 2;
  670. break;
  671. case DESC92_RATEMCS3:
  672. rate_idx = 3;
  673. break;
  674. case DESC92_RATEMCS4:
  675. rate_idx = 4;
  676. break;
  677. case DESC92_RATEMCS5:
  678. rate_idx = 5;
  679. break;
  680. case DESC92_RATEMCS6:
  681. rate_idx = 6;
  682. break;
  683. case DESC92_RATEMCS7:
  684. rate_idx = 7;
  685. break;
  686. case DESC92_RATEMCS8:
  687. rate_idx = 8;
  688. break;
  689. case DESC92_RATEMCS9:
  690. rate_idx = 9;
  691. break;
  692. case DESC92_RATEMCS10:
  693. rate_idx = 10;
  694. break;
  695. case DESC92_RATEMCS11:
  696. rate_idx = 11;
  697. break;
  698. case DESC92_RATEMCS12:
  699. rate_idx = 12;
  700. break;
  701. case DESC92_RATEMCS13:
  702. rate_idx = 13;
  703. break;
  704. case DESC92_RATEMCS14:
  705. rate_idx = 14;
  706. break;
  707. case DESC92_RATEMCS15:
  708. rate_idx = 15;
  709. break;
  710. default:
  711. rate_idx = 0;
  712. break;
  713. }
  714. }
  715. return rate_idx;
  716. }
  717. EXPORT_SYMBOL(rtlwifi_rate_mapping);
  718. void rtl_get_tcb_desc(struct ieee80211_hw *hw,
  719. struct ieee80211_tx_info *info,
  720. struct ieee80211_sta *sta,
  721. struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
  722. {
  723. struct rtl_priv *rtlpriv = rtl_priv(hw);
  724. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  725. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  726. struct ieee80211_rate *txrate;
  727. __le16 fc = hdr->frame_control;
  728. txrate = ieee80211_get_tx_rate(hw, info);
  729. tcb_desc->hw_rate = txrate->hw_value;
  730. if (ieee80211_is_data(fc)) {
  731. /*
  732. *we set data rate INX 0
  733. *in rtl_rc.c if skb is special data or
  734. *mgt which need low data rate.
  735. */
  736. /*
  737. *So tcb_desc->hw_rate is just used for
  738. *special data and mgt frames
  739. */
  740. if (info->control.rates[0].idx == 0 ||
  741. ieee80211_is_nullfunc(fc)) {
  742. tcb_desc->use_driver_rate = true;
  743. tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
  744. tcb_desc->disable_ratefallback = 1;
  745. } else {
  746. /*
  747. *because hw will nerver use hw_rate
  748. *when tcb_desc->use_driver_rate = false
  749. *so we never set highest N rate here,
  750. *and N rate will all be controlled by FW
  751. *when tcb_desc->use_driver_rate = false
  752. */
  753. if (sta && (sta->ht_cap.ht_supported)) {
  754. tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
  755. } else {
  756. if (rtlmac->mode == WIRELESS_MODE_B) {
  757. tcb_desc->hw_rate =
  758. rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
  759. } else {
  760. tcb_desc->hw_rate =
  761. rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
  762. }
  763. }
  764. }
  765. if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
  766. tcb_desc->multicast = 1;
  767. else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  768. tcb_desc->broadcast = 1;
  769. _rtl_txrate_selectmode(hw, sta, tcb_desc);
  770. _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
  771. _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
  772. _rtl_query_shortgi(hw, sta, tcb_desc, info);
  773. _rtl_query_protection_mode(hw, tcb_desc, info);
  774. } else {
  775. tcb_desc->use_driver_rate = true;
  776. tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
  777. tcb_desc->disable_ratefallback = 1;
  778. tcb_desc->mac_id = 0;
  779. tcb_desc->packet_bw = false;
  780. }
  781. }
  782. EXPORT_SYMBOL(rtl_get_tcb_desc);
  783. bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
  784. {
  785. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  786. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  787. struct rtl_priv *rtlpriv = rtl_priv(hw);
  788. __le16 fc = hdr->frame_control;
  789. u8 *act = (u8 *) (((u8 *) skb->data + MAC80211_3ADDR_LEN));
  790. u8 category;
  791. if (!ieee80211_is_action(fc))
  792. return true;
  793. category = *act;
  794. act++;
  795. switch (category) {
  796. case ACT_CAT_BA:
  797. switch (*act) {
  798. case ACT_ADDBAREQ:
  799. if (mac->act_scanning)
  800. return false;
  801. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  802. "%s ACT_ADDBAREQ From :%pM\n",
  803. is_tx ? "Tx" : "Rx", hdr->addr2);
  804. break;
  805. case ACT_ADDBARSP:
  806. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  807. "%s ACT_ADDBARSP From :%pM\n",
  808. is_tx ? "Tx" : "Rx", hdr->addr2);
  809. break;
  810. case ACT_DELBA:
  811. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  812. "ACT_ADDBADEL From :%pM\n", hdr->addr2);
  813. break;
  814. }
  815. break;
  816. default:
  817. break;
  818. }
  819. return true;
  820. }
  821. /*should call before software enc*/
  822. u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
  823. {
  824. struct rtl_priv *rtlpriv = rtl_priv(hw);
  825. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  826. __le16 fc = rtl_get_fc(skb);
  827. u16 ether_type;
  828. u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
  829. const struct iphdr *ip;
  830. if (!ieee80211_is_data(fc))
  831. return false;
  832. ip = (struct iphdr *)((u8 *) skb->data + mac_hdr_len +
  833. SNAP_SIZE + PROTOC_TYPE_SIZE);
  834. ether_type = *(u16 *) ((u8 *) skb->data + mac_hdr_len + SNAP_SIZE);
  835. /* ether_type = ntohs(ether_type); */
  836. if (ETH_P_IP == ether_type) {
  837. if (IPPROTO_UDP == ip->protocol) {
  838. struct udphdr *udp = (struct udphdr *)((u8 *) ip +
  839. (ip->ihl << 2));
  840. if (((((u8 *) udp)[1] == 68) &&
  841. (((u8 *) udp)[3] == 67)) ||
  842. ((((u8 *) udp)[1] == 67) &&
  843. (((u8 *) udp)[3] == 68))) {
  844. /*
  845. * 68 : UDP BOOTP client
  846. * 67 : UDP BOOTP server
  847. */
  848. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
  849. DBG_DMESG, "dhcp %s !!\n",
  850. is_tx ? "Tx" : "Rx");
  851. if (is_tx) {
  852. rtl_lps_leave(hw);
  853. ppsc->last_delaylps_stamp_jiffies =
  854. jiffies;
  855. }
  856. return true;
  857. }
  858. }
  859. } else if (ETH_P_ARP == ether_type) {
  860. if (is_tx) {
  861. rtl_lps_leave(hw);
  862. ppsc->last_delaylps_stamp_jiffies = jiffies;
  863. }
  864. return true;
  865. } else if (ETH_P_PAE == ether_type) {
  866. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  867. "802.1X %s EAPOL pkt!!\n", is_tx ? "Tx" : "Rx");
  868. if (is_tx) {
  869. rtl_lps_leave(hw);
  870. ppsc->last_delaylps_stamp_jiffies = jiffies;
  871. }
  872. return true;
  873. } else if (ETH_P_IPV6 == ether_type) {
  874. /* IPv6 */
  875. return true;
  876. }
  877. return false;
  878. }
  879. /*********************************************************
  880. *
  881. * functions called by core.c
  882. *
  883. *********************************************************/
  884. int rtl_tx_agg_start(struct ieee80211_hw *hw,
  885. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  886. {
  887. struct rtl_priv *rtlpriv = rtl_priv(hw);
  888. struct rtl_tid_data *tid_data;
  889. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  890. struct rtl_sta_info *sta_entry = NULL;
  891. if (sta == NULL)
  892. return -EINVAL;
  893. if (unlikely(tid >= MAX_TID_COUNT))
  894. return -EINVAL;
  895. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  896. if (!sta_entry)
  897. return -ENXIO;
  898. tid_data = &sta_entry->tids[tid];
  899. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d seq:%d\n",
  900. sta->addr, tid, tid_data->seq_number);
  901. *ssn = tid_data->seq_number;
  902. tid_data->agg.agg_state = RTL_AGG_START;
  903. ieee80211_start_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
  904. return 0;
  905. }
  906. int rtl_tx_agg_stop(struct ieee80211_hw *hw,
  907. struct ieee80211_sta *sta, u16 tid)
  908. {
  909. struct rtl_priv *rtlpriv = rtl_priv(hw);
  910. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  911. struct rtl_sta_info *sta_entry = NULL;
  912. if (sta == NULL)
  913. return -EINVAL;
  914. if (!sta->addr) {
  915. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
  916. return -EINVAL;
  917. }
  918. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
  919. sta->addr, tid);
  920. if (unlikely(tid >= MAX_TID_COUNT))
  921. return -EINVAL;
  922. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  923. sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
  924. ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, sta->addr, tid);
  925. return 0;
  926. }
  927. int rtl_tx_agg_oper(struct ieee80211_hw *hw,
  928. struct ieee80211_sta *sta, u16 tid)
  929. {
  930. struct rtl_priv *rtlpriv = rtl_priv(hw);
  931. struct rtl_sta_info *sta_entry = NULL;
  932. if (sta == NULL)
  933. return -EINVAL;
  934. if (!sta->addr) {
  935. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "ra = NULL\n");
  936. return -EINVAL;
  937. }
  938. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "on ra = %pM tid = %d\n",
  939. sta->addr, tid);
  940. if (unlikely(tid >= MAX_TID_COUNT))
  941. return -EINVAL;
  942. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  943. sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
  944. return 0;
  945. }
  946. /*********************************************************
  947. *
  948. * wq & timer callback functions
  949. *
  950. *********************************************************/
  951. void rtl_watchdog_wq_callback(void *data)
  952. {
  953. struct rtl_works *rtlworks = container_of_dwork_rtl(data,
  954. struct rtl_works,
  955. watchdog_wq);
  956. struct ieee80211_hw *hw = rtlworks->hw;
  957. struct rtl_priv *rtlpriv = rtl_priv(hw);
  958. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  959. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  960. bool busytraffic = false;
  961. bool higher_busytraffic = false;
  962. bool higher_busyrxtraffic = false;
  963. u8 idx, tid;
  964. u32 rx_cnt_inp4eriod = 0;
  965. u32 tx_cnt_inp4eriod = 0;
  966. u32 aver_rx_cnt_inperiod = 0;
  967. u32 aver_tx_cnt_inperiod = 0;
  968. u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
  969. u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
  970. bool enter_ps = false;
  971. if (is_hal_stop(rtlhal))
  972. return;
  973. /* <1> Determine if action frame is allowed */
  974. if (mac->link_state > MAC80211_NOLINK) {
  975. if (mac->cnt_after_linked < 20)
  976. mac->cnt_after_linked++;
  977. } else {
  978. mac->cnt_after_linked = 0;
  979. }
  980. /*
  981. *<2> to check if traffic busy, if
  982. * busytraffic we don't change channel
  983. */
  984. if (mac->link_state >= MAC80211_LINKED) {
  985. /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
  986. for (idx = 0; idx <= 2; idx++) {
  987. rtlpriv->link_info.num_rx_in4period[idx] =
  988. rtlpriv->link_info.num_rx_in4period[idx + 1];
  989. rtlpriv->link_info.num_tx_in4period[idx] =
  990. rtlpriv->link_info.num_tx_in4period[idx + 1];
  991. }
  992. rtlpriv->link_info.num_rx_in4period[3] =
  993. rtlpriv->link_info.num_rx_inperiod;
  994. rtlpriv->link_info.num_tx_in4period[3] =
  995. rtlpriv->link_info.num_tx_inperiod;
  996. for (idx = 0; idx <= 3; idx++) {
  997. rx_cnt_inp4eriod +=
  998. rtlpriv->link_info.num_rx_in4period[idx];
  999. tx_cnt_inp4eriod +=
  1000. rtlpriv->link_info.num_tx_in4period[idx];
  1001. }
  1002. aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
  1003. aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
  1004. /* (2) check traffic busy */
  1005. if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100)
  1006. busytraffic = true;
  1007. /* Higher Tx/Rx data. */
  1008. if (aver_rx_cnt_inperiod > 4000 ||
  1009. aver_tx_cnt_inperiod > 4000) {
  1010. higher_busytraffic = true;
  1011. /* Extremely high Rx data. */
  1012. if (aver_rx_cnt_inperiod > 5000)
  1013. higher_busyrxtraffic = true;
  1014. }
  1015. /* check every tid's tx traffic */
  1016. for (tid = 0; tid <= 7; tid++) {
  1017. for (idx = 0; idx <= 2; idx++)
  1018. rtlpriv->link_info.tidtx_in4period[tid][idx] =
  1019. rtlpriv->link_info.tidtx_in4period[tid]
  1020. [idx + 1];
  1021. rtlpriv->link_info.tidtx_in4period[tid][3] =
  1022. rtlpriv->link_info.tidtx_inperiod[tid];
  1023. for (idx = 0; idx <= 3; idx++)
  1024. tidtx_inp4eriod[tid] +=
  1025. rtlpriv->link_info.tidtx_in4period[tid][idx];
  1026. aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
  1027. if (aver_tidtx_inperiod[tid] > 5000)
  1028. rtlpriv->link_info.higher_busytxtraffic[tid] =
  1029. true;
  1030. else
  1031. rtlpriv->link_info.higher_busytxtraffic[tid] =
  1032. false;
  1033. }
  1034. if (((rtlpriv->link_info.num_rx_inperiod +
  1035. rtlpriv->link_info.num_tx_inperiod) > 8) ||
  1036. (rtlpriv->link_info.num_rx_inperiod > 2))
  1037. enter_ps = false;
  1038. else
  1039. enter_ps = true;
  1040. /* LeisurePS only work in infra mode. */
  1041. if (enter_ps)
  1042. rtl_lps_enter(hw);
  1043. else
  1044. rtl_lps_leave(hw);
  1045. }
  1046. rtlpriv->link_info.num_rx_inperiod = 0;
  1047. rtlpriv->link_info.num_tx_inperiod = 0;
  1048. for (tid = 0; tid <= 7; tid++)
  1049. rtlpriv->link_info.tidtx_inperiod[tid] = 0;
  1050. rtlpriv->link_info.busytraffic = busytraffic;
  1051. rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
  1052. rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
  1053. /* <3> DM */
  1054. rtlpriv->cfg->ops->dm_watchdog(hw);
  1055. }
  1056. void rtl_watch_dog_timer_callback(unsigned long data)
  1057. {
  1058. struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
  1059. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1060. queue_delayed_work(rtlpriv->works.rtl_wq,
  1061. &rtlpriv->works.watchdog_wq, 0);
  1062. mod_timer(&rtlpriv->works.watchdog_timer,
  1063. jiffies + MSECS(RTL_WATCH_DOG_TIME));
  1064. }
  1065. /*********************************************************
  1066. *
  1067. * frame process functions
  1068. *
  1069. *********************************************************/
  1070. u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
  1071. {
  1072. struct ieee80211_mgmt *mgmt = (void *)data;
  1073. u8 *pos, *end;
  1074. pos = (u8 *)mgmt->u.beacon.variable;
  1075. end = data + len;
  1076. while (pos < end) {
  1077. if (pos + 2 + pos[1] > end)
  1078. return NULL;
  1079. if (pos[0] == ie)
  1080. return pos;
  1081. pos += 2 + pos[1];
  1082. }
  1083. return NULL;
  1084. }
  1085. /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
  1086. /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
  1087. static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
  1088. enum ieee80211_smps_mode smps, u8 *da, u8 *bssid)
  1089. {
  1090. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1091. struct sk_buff *skb;
  1092. struct ieee80211_mgmt *action_frame;
  1093. /* 27 = header + category + action + smps mode */
  1094. skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
  1095. if (!skb)
  1096. return NULL;
  1097. skb_reserve(skb, hw->extra_tx_headroom);
  1098. action_frame = (void *)skb_put(skb, 27);
  1099. memset(action_frame, 0, 27);
  1100. memcpy(action_frame->da, da, ETH_ALEN);
  1101. memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
  1102. memcpy(action_frame->bssid, bssid, ETH_ALEN);
  1103. action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1104. IEEE80211_STYPE_ACTION);
  1105. action_frame->u.action.category = WLAN_CATEGORY_HT;
  1106. action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
  1107. switch (smps) {
  1108. case IEEE80211_SMPS_AUTOMATIC:/* 0 */
  1109. case IEEE80211_SMPS_NUM_MODES:/* 4 */
  1110. WARN_ON(1);
  1111. case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
  1112. action_frame->u.action.u.ht_smps.smps_control =
  1113. WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
  1114. break;
  1115. case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
  1116. action_frame->u.action.u.ht_smps.smps_control =
  1117. WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
  1118. break;
  1119. case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
  1120. action_frame->u.action.u.ht_smps.smps_control =
  1121. WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
  1122. break;
  1123. }
  1124. return skb;
  1125. }
  1126. int rtl_send_smps_action(struct ieee80211_hw *hw,
  1127. struct ieee80211_sta *sta, u8 *da, u8 *bssid,
  1128. enum ieee80211_smps_mode smps)
  1129. {
  1130. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1131. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1132. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  1133. struct sk_buff *skb = rtl_make_smps_action(hw, smps, da, bssid);
  1134. struct rtl_tcb_desc tcb_desc;
  1135. memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
  1136. if (rtlpriv->mac80211.act_scanning)
  1137. goto err_free;
  1138. if (!sta)
  1139. goto err_free;
  1140. if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
  1141. goto err_free;
  1142. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  1143. goto err_free;
  1144. /* this is a type = mgmt * stype = action frame */
  1145. if (skb) {
  1146. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1147. struct rtl_sta_info *sta_entry =
  1148. (struct rtl_sta_info *) sta->drv_priv;
  1149. sta_entry->mimo_ps = smps;
  1150. rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
  1151. info->control.rates[0].idx = 0;
  1152. info->control.sta = sta;
  1153. info->band = hw->conf.channel->band;
  1154. rtlpriv->intf_ops->adapter_tx(hw, skb, &tcb_desc);
  1155. }
  1156. err_free:
  1157. return 0;
  1158. }
  1159. /*********************************************************
  1160. *
  1161. * IOT functions
  1162. *
  1163. *********************************************************/
  1164. static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
  1165. struct octet_string vendor_ie)
  1166. {
  1167. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1168. bool matched = false;
  1169. static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
  1170. static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
  1171. static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
  1172. static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
  1173. static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
  1174. static u8 racap[] = { 0x00, 0x0c, 0x43 };
  1175. static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
  1176. static u8 marvcap[] = { 0x00, 0x50, 0x43 };
  1177. if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
  1178. memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
  1179. rtlpriv->mac80211.vendor = PEER_ATH;
  1180. matched = true;
  1181. } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
  1182. memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
  1183. memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
  1184. rtlpriv->mac80211.vendor = PEER_BROAD;
  1185. matched = true;
  1186. } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
  1187. rtlpriv->mac80211.vendor = PEER_RAL;
  1188. matched = true;
  1189. } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
  1190. rtlpriv->mac80211.vendor = PEER_CISCO;
  1191. matched = true;
  1192. } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
  1193. rtlpriv->mac80211.vendor = PEER_MARV;
  1194. matched = true;
  1195. }
  1196. return matched;
  1197. }
  1198. static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
  1199. unsigned int len)
  1200. {
  1201. struct ieee80211_mgmt *mgmt = (void *)data;
  1202. struct octet_string vendor_ie;
  1203. u8 *pos, *end;
  1204. pos = (u8 *)mgmt->u.beacon.variable;
  1205. end = data + len;
  1206. while (pos < end) {
  1207. if (pos[0] == 221) {
  1208. vendor_ie.length = pos[1];
  1209. vendor_ie.octet = &pos[2];
  1210. if (rtl_chk_vendor_ouisub(hw, vendor_ie))
  1211. return true;
  1212. }
  1213. if (pos + 2 + pos[1] > end)
  1214. return false;
  1215. pos += 2 + pos[1];
  1216. }
  1217. return false;
  1218. }
  1219. void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
  1220. {
  1221. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1222. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1223. struct ieee80211_hdr *hdr = (void *)data;
  1224. u32 vendor = PEER_UNKNOWN;
  1225. static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
  1226. static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
  1227. static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
  1228. static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
  1229. static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
  1230. static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
  1231. static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
  1232. static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
  1233. static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
  1234. static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
  1235. static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
  1236. static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
  1237. static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
  1238. static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
  1239. static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
  1240. static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
  1241. if (mac->opmode != NL80211_IFTYPE_STATION)
  1242. return;
  1243. if (mac->link_state == MAC80211_NOLINK) {
  1244. mac->vendor = PEER_UNKNOWN;
  1245. return;
  1246. }
  1247. if (mac->cnt_after_linked > 2)
  1248. return;
  1249. /* check if this really is a beacon */
  1250. if (!ieee80211_is_beacon(hdr->frame_control))
  1251. return;
  1252. /* min. beacon length + FCS_LEN */
  1253. if (len <= 40 + FCS_LEN)
  1254. return;
  1255. /* and only beacons from the associated BSSID, please */
  1256. if (compare_ether_addr(hdr->addr3, rtlpriv->mac80211.bssid))
  1257. return;
  1258. if (rtl_find_221_ie(hw, data, len))
  1259. vendor = mac->vendor;
  1260. if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
  1261. (memcmp(mac->bssid, ap5_2, 3) == 0) ||
  1262. (memcmp(mac->bssid, ap5_3, 3) == 0) ||
  1263. (memcmp(mac->bssid, ap5_4, 3) == 0) ||
  1264. (memcmp(mac->bssid, ap5_5, 3) == 0) ||
  1265. (memcmp(mac->bssid, ap5_6, 3) == 0) ||
  1266. vendor == PEER_ATH) {
  1267. vendor = PEER_ATH;
  1268. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
  1269. } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
  1270. (memcmp(mac->bssid, ap4_5, 3) == 0) ||
  1271. (memcmp(mac->bssid, ap4_1, 3) == 0) ||
  1272. (memcmp(mac->bssid, ap4_2, 3) == 0) ||
  1273. (memcmp(mac->bssid, ap4_3, 3) == 0) ||
  1274. vendor == PEER_RAL) {
  1275. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
  1276. vendor = PEER_RAL;
  1277. } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
  1278. vendor == PEER_CISCO) {
  1279. vendor = PEER_CISCO;
  1280. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
  1281. } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
  1282. (memcmp(mac->bssid, ap3_2, 3) == 0) ||
  1283. (memcmp(mac->bssid, ap3_3, 3) == 0) ||
  1284. vendor == PEER_BROAD) {
  1285. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
  1286. vendor = PEER_BROAD;
  1287. } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
  1288. vendor == PEER_MARV) {
  1289. vendor = PEER_MARV;
  1290. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
  1291. }
  1292. mac->vendor = vendor;
  1293. }
  1294. /*********************************************************
  1295. *
  1296. * sysfs functions
  1297. *
  1298. *********************************************************/
  1299. static ssize_t rtl_show_debug_level(struct device *d,
  1300. struct device_attribute *attr, char *buf)
  1301. {
  1302. struct ieee80211_hw *hw = dev_get_drvdata(d);
  1303. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1304. return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
  1305. }
  1306. static ssize_t rtl_store_debug_level(struct device *d,
  1307. struct device_attribute *attr,
  1308. const char *buf, size_t count)
  1309. {
  1310. struct ieee80211_hw *hw = dev_get_drvdata(d);
  1311. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1312. unsigned long val;
  1313. int ret;
  1314. ret = strict_strtoul(buf, 0, &val);
  1315. if (ret) {
  1316. printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
  1317. } else {
  1318. rtlpriv->dbg.global_debuglevel = val;
  1319. printk(KERN_DEBUG "debuglevel:%x\n",
  1320. rtlpriv->dbg.global_debuglevel);
  1321. }
  1322. return strnlen(buf, count);
  1323. }
  1324. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  1325. rtl_show_debug_level, rtl_store_debug_level);
  1326. static struct attribute *rtl_sysfs_entries[] = {
  1327. &dev_attr_debug_level.attr,
  1328. NULL
  1329. };
  1330. /*
  1331. * "name" is folder name witch will be
  1332. * put in device directory like :
  1333. * sys/devices/pci0000:00/0000:00:1c.4/
  1334. * 0000:06:00.0/rtl_sysfs
  1335. */
  1336. struct attribute_group rtl_attribute_group = {
  1337. .name = "rtlsysfs",
  1338. .attrs = rtl_sysfs_entries,
  1339. };
  1340. MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
  1341. MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
  1342. MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
  1343. MODULE_LICENSE("GPL");
  1344. MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
  1345. static int __init rtl_core_module_init(void)
  1346. {
  1347. if (rtl_rate_control_register())
  1348. pr_err("Unable to register rtl_rc, use default RC !!\n");
  1349. return 0;
  1350. }
  1351. static void __exit rtl_core_module_exit(void)
  1352. {
  1353. /*RC*/
  1354. rtl_rate_control_unregister();
  1355. }
  1356. module_init(rtl_core_module_init);
  1357. module_exit(rtl_core_module_exit);