base.c 42 KB

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