base.c 42 KB

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