base.c 40 KB

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