base.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2010 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * wlanfae <wlanfae@realtek.com>
  23. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  24. * Hsinchu 300, Taiwan.
  25. *
  26. * Larry Finger <Larry.Finger@lwfinger.net>
  27. *
  28. *****************************************************************************/
  29. #include <linux/ip.h>
  30. #include "wifi.h"
  31. #include "rc.h"
  32. #include "base.h"
  33. #include "efuse.h"
  34. #include "cam.h"
  35. #include "ps.h"
  36. #include "regd.h"
  37. /*
  38. *NOTICE!!!: This file will be very big, we hsould
  39. *keep it clear under follwing roles:
  40. *
  41. *This file include follwing part, so, if you add new
  42. *functions into this file, please check which part it
  43. *should includes. or check if you should add new part
  44. *for this file:
  45. *
  46. *1) mac80211 init functions
  47. *2) tx information functions
  48. *3) functions called by core.c
  49. *4) wq & timer callback functions
  50. *5) frame process functions
  51. *6) sysfs functions
  52. *7) ...
  53. */
  54. /*********************************************************
  55. *
  56. * mac80211 init functions
  57. *
  58. *********************************************************/
  59. static struct ieee80211_channel rtl_channeltable[] = {
  60. {.center_freq = 2412, .hw_value = 1,},
  61. {.center_freq = 2417, .hw_value = 2,},
  62. {.center_freq = 2422, .hw_value = 3,},
  63. {.center_freq = 2427, .hw_value = 4,},
  64. {.center_freq = 2432, .hw_value = 5,},
  65. {.center_freq = 2437, .hw_value = 6,},
  66. {.center_freq = 2442, .hw_value = 7,},
  67. {.center_freq = 2447, .hw_value = 8,},
  68. {.center_freq = 2452, .hw_value = 9,},
  69. {.center_freq = 2457, .hw_value = 10,},
  70. {.center_freq = 2462, .hw_value = 11,},
  71. {.center_freq = 2467, .hw_value = 12,},
  72. {.center_freq = 2472, .hw_value = 13,},
  73. {.center_freq = 2484, .hw_value = 14,},
  74. };
  75. static struct ieee80211_rate rtl_ratetable[] = {
  76. {.bitrate = 10, .hw_value = 0x00,},
  77. {.bitrate = 20, .hw_value = 0x01,},
  78. {.bitrate = 55, .hw_value = 0x02,},
  79. {.bitrate = 110, .hw_value = 0x03,},
  80. {.bitrate = 60, .hw_value = 0x04,},
  81. {.bitrate = 90, .hw_value = 0x05,},
  82. {.bitrate = 120, .hw_value = 0x06,},
  83. {.bitrate = 180, .hw_value = 0x07,},
  84. {.bitrate = 240, .hw_value = 0x08,},
  85. {.bitrate = 360, .hw_value = 0x09,},
  86. {.bitrate = 480, .hw_value = 0x0a,},
  87. {.bitrate = 540, .hw_value = 0x0b,},
  88. };
  89. static const struct ieee80211_supported_band rtl_band_2ghz = {
  90. .band = IEEE80211_BAND_2GHZ,
  91. .channels = rtl_channeltable,
  92. .n_channels = ARRAY_SIZE(rtl_channeltable),
  93. .bitrates = rtl_ratetable,
  94. .n_bitrates = ARRAY_SIZE(rtl_ratetable),
  95. .ht_cap = {0},
  96. };
  97. static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
  98. struct ieee80211_sta_ht_cap *ht_cap)
  99. {
  100. struct rtl_priv *rtlpriv = rtl_priv(hw);
  101. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  102. ht_cap->ht_supported = true;
  103. ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  104. IEEE80211_HT_CAP_SGI_40 |
  105. IEEE80211_HT_CAP_SGI_20 |
  106. IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
  107. /*
  108. *Maximum length of AMPDU that the STA can receive.
  109. *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
  110. */
  111. ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  112. /*Minimum MPDU start spacing , */
  113. ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
  114. ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  115. /*
  116. *hw->wiphy->bands[IEEE80211_BAND_2GHZ]
  117. *base on ant_num
  118. *rx_mask: RX mask
  119. *if rx_ant =1 rx_mask[0]=0xff;==>MCS0-MCS7
  120. *if rx_ant =2 rx_mask[1]=0xff;==>MCS8-MCS15
  121. *if rx_ant >=3 rx_mask[2]=0xff;
  122. *if BW_40 rx_mask[4]=0x01;
  123. *highest supported RX rate
  124. */
  125. if (get_rf_type(rtlphy) == RF_1T2R || get_rf_type(rtlphy) == RF_2T2R) {
  126. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("1T2R or 2T2R\n"));
  127. ht_cap->mcs.rx_mask[0] = 0xFF;
  128. ht_cap->mcs.rx_mask[1] = 0xFF;
  129. ht_cap->mcs.rx_mask[4] = 0x01;
  130. ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
  131. } else if (get_rf_type(rtlphy) == RF_1T1R) {
  132. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("1T1R\n"));
  133. ht_cap->mcs.rx_mask[0] = 0xFF;
  134. ht_cap->mcs.rx_mask[1] = 0x00;
  135. ht_cap->mcs.rx_mask[4] = 0x01;
  136. ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
  137. }
  138. }
  139. static void _rtl_init_mac80211(struct ieee80211_hw *hw)
  140. {
  141. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  142. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  143. struct ieee80211_supported_band *sband;
  144. /* <1> use mac->bands as mem for hw->wiphy->bands */
  145. sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
  146. /*
  147. * <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
  148. * to default value(1T1R)
  149. */
  150. memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
  151. sizeof(struct ieee80211_supported_band));
  152. /* <3> init ht cap base on ant_num */
  153. _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
  154. /* <4> set mac->sband to wiphy->sband */
  155. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
  156. /* <5> set hw caps */
  157. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  158. IEEE80211_HW_RX_INCLUDES_FCS |
  159. IEEE80211_HW_BEACON_FILTER | IEEE80211_HW_AMPDU_AGGREGATION | /*PS*/
  160. /*IEEE80211_HW_SUPPORTS_PS | */
  161. /*IEEE80211_HW_PS_NULLFUNC_STACK | */
  162. /*IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
  163. IEEE80211_HW_REPORTS_TX_ACK_STATUS | 0;
  164. hw->wiphy->interface_modes =
  165. BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_ADHOC);
  166. hw->wiphy->rts_threshold = 2347;
  167. hw->queues = AC_MAX;
  168. hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
  169. /* TODO: Correct this value for our hw */
  170. /* TODO: define these hard code value */
  171. hw->channel_change_time = 100;
  172. hw->max_listen_interval = 5;
  173. hw->max_rate_tries = 4;
  174. /* hw->max_rates = 1; */
  175. /* <6> mac address */
  176. if (is_valid_ether_addr(rtlefuse->dev_addr)) {
  177. SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
  178. } else {
  179. u8 rtlmac[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
  180. get_random_bytes((rtlmac + (ETH_ALEN - 1)), 1);
  181. SET_IEEE80211_PERM_ADDR(hw, rtlmac);
  182. }
  183. }
  184. static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
  185. {
  186. struct rtl_priv *rtlpriv = rtl_priv(hw);
  187. /* <1> timer */
  188. init_timer(&rtlpriv->works.watchdog_timer);
  189. setup_timer(&rtlpriv->works.watchdog_timer,
  190. rtl_watch_dog_timer_callback, (unsigned long)hw);
  191. /* <2> work queue */
  192. rtlpriv->works.hw = hw;
  193. rtlpriv->works.rtl_wq = alloc_workqueue(rtlpriv->cfg->name, 0, 0);
  194. INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
  195. (void *)rtl_watchdog_wq_callback);
  196. INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
  197. (void *)rtl_ips_nic_off_wq_callback);
  198. }
  199. void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
  200. {
  201. struct rtl_priv *rtlpriv = rtl_priv(hw);
  202. del_timer_sync(&rtlpriv->works.watchdog_timer);
  203. cancel_delayed_work(&rtlpriv->works.watchdog_wq);
  204. cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
  205. }
  206. void rtl_init_rfkill(struct ieee80211_hw *hw)
  207. {
  208. struct rtl_priv *rtlpriv = rtl_priv(hw);
  209. bool radio_state;
  210. bool blocked;
  211. u8 valid = 0;
  212. /*set init state to on */
  213. rtlpriv->rfkill.rfkill_state = 1;
  214. wiphy_rfkill_set_hw_state(hw->wiphy, 0);
  215. radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
  216. if (valid) {
  217. printk(KERN_INFO "rtlwifi: wireless switch is %s\n",
  218. rtlpriv->rfkill.rfkill_state ? "on" : "off");
  219. rtlpriv->rfkill.rfkill_state = radio_state;
  220. blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
  221. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  222. }
  223. wiphy_rfkill_start_polling(hw->wiphy);
  224. }
  225. void rtl_deinit_rfkill(struct ieee80211_hw *hw)
  226. {
  227. wiphy_rfkill_stop_polling(hw->wiphy);
  228. }
  229. int rtl_init_core(struct ieee80211_hw *hw)
  230. {
  231. struct rtl_priv *rtlpriv = rtl_priv(hw);
  232. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  233. /* <1> init mac80211 */
  234. _rtl_init_mac80211(hw);
  235. rtlmac->hw = hw;
  236. /* <2> rate control register */
  237. hw->rate_control_algorithm = "rtl_rc";
  238. /*
  239. * <3> init CRDA must come after init
  240. * mac80211 hw in _rtl_init_mac80211.
  241. */
  242. if (rtl_regd_init(hw, rtl_reg_notifier)) {
  243. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("REGD init failed\n"));
  244. return 1;
  245. } else {
  246. /* CRDA regd hint must after init CRDA */
  247. if (regulatory_hint(hw->wiphy, rtlpriv->regd.alpha2)) {
  248. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  249. ("regulatory_hint fail\n"));
  250. }
  251. }
  252. /* <4> locks */
  253. mutex_init(&rtlpriv->locks.conf_mutex);
  254. spin_lock_init(&rtlpriv->locks.ips_lock);
  255. spin_lock_init(&rtlpriv->locks.irq_th_lock);
  256. spin_lock_init(&rtlpriv->locks.h2c_lock);
  257. spin_lock_init(&rtlpriv->locks.rf_ps_lock);
  258. spin_lock_init(&rtlpriv->locks.rf_lock);
  259. spin_lock_init(&rtlpriv->locks.lps_lock);
  260. rtlmac->link_state = MAC80211_NOLINK;
  261. /* <5> init deferred work */
  262. _rtl_init_deferred_work(hw);
  263. return 0;
  264. }
  265. void rtl_deinit_core(struct ieee80211_hw *hw)
  266. {
  267. }
  268. void rtl_init_rx_config(struct ieee80211_hw *hw)
  269. {
  270. struct rtl_priv *rtlpriv = rtl_priv(hw);
  271. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  272. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
  273. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_MGT_FILTER,
  274. (u8 *) (&mac->rx_mgt_filter));
  275. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CTRL_FILTER,
  276. (u8 *) (&mac->rx_ctrl_filter));
  277. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_DATA_FILTER,
  278. (u8 *) (&mac->rx_data_filter));
  279. }
  280. /*********************************************************
  281. *
  282. * tx information functions
  283. *
  284. *********************************************************/
  285. static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
  286. struct rtl_tcb_desc *tcb_desc,
  287. struct ieee80211_tx_info *info)
  288. {
  289. struct rtl_priv *rtlpriv = rtl_priv(hw);
  290. u8 rate_flag = info->control.rates[0].flags;
  291. tcb_desc->use_shortpreamble = false;
  292. /* 1M can only use Long Preamble. 11B spec */
  293. if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
  294. return;
  295. else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  296. tcb_desc->use_shortpreamble = true;
  297. return;
  298. }
  299. static void _rtl_query_shortgi(struct ieee80211_hw *hw,
  300. struct rtl_tcb_desc *tcb_desc,
  301. struct ieee80211_tx_info *info)
  302. {
  303. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  304. u8 rate_flag = info->control.rates[0].flags;
  305. tcb_desc->use_shortgi = false;
  306. if (!mac->ht_enable)
  307. return;
  308. if (!mac->sgi_40 && !mac->sgi_20)
  309. return;
  310. if ((mac->bw_40 == true) && mac->sgi_40)
  311. tcb_desc->use_shortgi = true;
  312. else if ((mac->bw_40 == false) && mac->sgi_20)
  313. tcb_desc->use_shortgi = true;
  314. if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
  315. tcb_desc->use_shortgi = false;
  316. }
  317. static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
  318. struct rtl_tcb_desc *tcb_desc,
  319. struct ieee80211_tx_info *info)
  320. {
  321. struct rtl_priv *rtlpriv = rtl_priv(hw);
  322. u8 rate_flag = info->control.rates[0].flags;
  323. /* Common Settings */
  324. tcb_desc->rts_stbc = false;
  325. tcb_desc->cts_enable = false;
  326. tcb_desc->rts_sc = 0;
  327. tcb_desc->rts_bw = false;
  328. tcb_desc->rts_use_shortpreamble = false;
  329. tcb_desc->rts_use_shortgi = false;
  330. if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  331. /* Use CTS-to-SELF in protection mode. */
  332. tcb_desc->rts_enable = true;
  333. tcb_desc->cts_enable = true;
  334. tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
  335. } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
  336. /* Use RTS-CTS in protection mode. */
  337. tcb_desc->rts_enable = true;
  338. tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
  339. }
  340. }
  341. static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
  342. struct rtl_tcb_desc *tcb_desc)
  343. {
  344. struct rtl_priv *rtlpriv = rtl_priv(hw);
  345. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  346. if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
  347. if (mac->opmode == NL80211_IFTYPE_STATION)
  348. tcb_desc->ratr_index = 0;
  349. else if (mac->opmode == NL80211_IFTYPE_ADHOC) {
  350. if (tcb_desc->multicast || tcb_desc->broadcast) {
  351. tcb_desc->hw_rate =
  352. rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
  353. tcb_desc->use_driver_rate = 1;
  354. } else {
  355. /* TODO */
  356. }
  357. }
  358. }
  359. if (rtlpriv->dm.useramask) {
  360. /* TODO adhoc and station handled differently in the future */
  361. tcb_desc->mac_id = 0;
  362. if ((mac->mode == WIRELESS_MODE_N_24G) ||
  363. (mac->mode == WIRELESS_MODE_N_5G)) {
  364. tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
  365. } else if (mac->mode & WIRELESS_MODE_G) {
  366. tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
  367. } else if (mac->mode & WIRELESS_MODE_B) {
  368. tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
  369. }
  370. }
  371. }
  372. static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
  373. struct rtl_tcb_desc *tcb_desc)
  374. {
  375. struct rtl_priv *rtlpriv = rtl_priv(hw);
  376. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  377. tcb_desc->packet_bw = false;
  378. if (!mac->bw_40 || !mac->ht_enable)
  379. return;
  380. if (tcb_desc->multicast || tcb_desc->broadcast)
  381. return;
  382. /*use legency rate, shall use 20MHz */
  383. if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
  384. return;
  385. tcb_desc->packet_bw = true;
  386. }
  387. static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
  388. {
  389. struct rtl_priv *rtlpriv = rtl_priv(hw);
  390. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  391. u8 hw_rate;
  392. if (get_rf_type(rtlphy) == RF_2T2R)
  393. hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
  394. else
  395. hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
  396. return hw_rate;
  397. }
  398. void rtl_get_tcb_desc(struct ieee80211_hw *hw,
  399. struct ieee80211_tx_info *info,
  400. struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
  401. {
  402. struct rtl_priv *rtlpriv = rtl_priv(hw);
  403. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  404. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  405. struct ieee80211_rate *txrate;
  406. __le16 fc = hdr->frame_control;
  407. memset(tcb_desc, 0, sizeof(struct rtl_tcb_desc));
  408. if (ieee80211_is_data(fc)) {
  409. txrate = ieee80211_get_tx_rate(hw, info);
  410. tcb_desc->hw_rate = txrate->hw_value;
  411. /*
  412. *we set data rate RTL_RC_CCK_RATE1M
  413. *in rtl_rc.c if skb is special data or
  414. *mgt which need low data rate.
  415. */
  416. /*
  417. *So tcb_desc->hw_rate is just used for
  418. *special data and mgt frames
  419. */
  420. if (tcb_desc->hw_rate < rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M]) {
  421. tcb_desc->use_driver_rate = true;
  422. tcb_desc->ratr_index = 7;
  423. tcb_desc->hw_rate =
  424. rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M];
  425. tcb_desc->disable_ratefallback = 1;
  426. } else {
  427. /*
  428. *because hw will nerver use hw_rate
  429. *when tcb_desc->use_driver_rate = false
  430. *so we never set highest N rate here,
  431. *and N rate will all be controled by FW
  432. *when tcb_desc->use_driver_rate = false
  433. */
  434. if (rtlmac->ht_enable) {
  435. tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
  436. } else {
  437. if (rtlmac->mode == WIRELESS_MODE_B) {
  438. tcb_desc->hw_rate =
  439. rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
  440. } else {
  441. tcb_desc->hw_rate =
  442. rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
  443. }
  444. }
  445. }
  446. if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
  447. tcb_desc->multicast = 1;
  448. else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  449. tcb_desc->broadcast = 1;
  450. _rtl_txrate_selectmode(hw, tcb_desc);
  451. _rtl_query_bandwidth_mode(hw, tcb_desc);
  452. _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
  453. _rtl_query_shortgi(hw, tcb_desc, info);
  454. _rtl_query_protection_mode(hw, tcb_desc, info);
  455. } else {
  456. tcb_desc->use_driver_rate = true;
  457. tcb_desc->ratr_index = 7;
  458. tcb_desc->disable_ratefallback = 1;
  459. tcb_desc->mac_id = 0;
  460. tcb_desc->hw_rate = rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M];
  461. }
  462. }
  463. EXPORT_SYMBOL(rtl_get_tcb_desc);
  464. bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
  465. {
  466. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  467. struct rtl_priv *rtlpriv = rtl_priv(hw);
  468. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  469. __le16 fc = hdr->frame_control;
  470. if (ieee80211_is_auth(fc)) {
  471. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
  472. rtl_ips_nic_on(hw);
  473. mac->link_state = MAC80211_LINKING;
  474. }
  475. return true;
  476. }
  477. bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
  478. {
  479. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  480. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  481. struct rtl_priv *rtlpriv = rtl_priv(hw);
  482. __le16 fc = hdr->frame_control;
  483. u8 *act = (u8 *) (((u8 *) skb->data + MAC80211_3ADDR_LEN));
  484. u8 category;
  485. if (!ieee80211_is_action(fc))
  486. return true;
  487. category = *act;
  488. act++;
  489. switch (category) {
  490. case ACT_CAT_BA:
  491. switch (*act) {
  492. case ACT_ADDBAREQ:
  493. if (mac->act_scanning)
  494. return false;
  495. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  496. ("%s ACT_ADDBAREQ From :" MAC_FMT "\n",
  497. is_tx ? "Tx" : "Rx", MAC_ARG(hdr->addr2)));
  498. break;
  499. case ACT_ADDBARSP:
  500. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  501. ("%s ACT_ADDBARSP From :" MAC_FMT "\n",
  502. is_tx ? "Tx" : "Rx", MAC_ARG(hdr->addr2)));
  503. break;
  504. case ACT_DELBA:
  505. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  506. ("ACT_ADDBADEL From :" MAC_FMT "\n",
  507. MAC_ARG(hdr->addr2)));
  508. break;
  509. }
  510. break;
  511. default:
  512. break;
  513. }
  514. return true;
  515. }
  516. /*should call before software enc*/
  517. u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
  518. {
  519. struct rtl_priv *rtlpriv = rtl_priv(hw);
  520. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  521. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  522. __le16 fc = hdr->frame_control;
  523. u16 ether_type;
  524. u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
  525. const struct iphdr *ip;
  526. if (!ieee80211_is_data(fc))
  527. return false;
  528. if (ieee80211_is_nullfunc(fc))
  529. return true;
  530. ip = (struct iphdr *)((u8 *) skb->data + mac_hdr_len +
  531. SNAP_SIZE + PROTOC_TYPE_SIZE);
  532. ether_type = *(u16 *) ((u8 *) skb->data + mac_hdr_len + SNAP_SIZE);
  533. if (ETH_P_IP == ether_type) {
  534. if (IPPROTO_UDP == ip->protocol) {
  535. struct udphdr *udp = (struct udphdr *)((u8 *) ip +
  536. (ip->ihl << 2));
  537. if (((((u8 *) udp)[1] == 68) &&
  538. (((u8 *) udp)[3] == 67)) ||
  539. ((((u8 *) udp)[1] == 67) &&
  540. (((u8 *) udp)[3] == 68))) {
  541. /*
  542. * 68 : UDP BOOTP client
  543. * 67 : UDP BOOTP server
  544. */
  545. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
  546. DBG_DMESG, ("dhcp %s !!\n",
  547. (is_tx) ? "Tx" : "Rx"));
  548. if (is_tx) {
  549. rtl_lps_leave(hw);
  550. ppsc->last_delaylps_stamp_jiffies =
  551. jiffies;
  552. }
  553. return true;
  554. }
  555. }
  556. } else if (ETH_P_ARP == ether_type) {
  557. if (is_tx) {
  558. rtl_lps_leave(hw);
  559. ppsc->last_delaylps_stamp_jiffies = jiffies;
  560. }
  561. return true;
  562. } else if (ETH_P_PAE == ether_type) {
  563. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  564. ("802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx"));
  565. if (is_tx) {
  566. rtl_lps_leave(hw);
  567. ppsc->last_delaylps_stamp_jiffies = jiffies;
  568. }
  569. return true;
  570. } else if (ETH_P_IPV6 == ether_type) {
  571. /* IPv6 */
  572. return true;
  573. }
  574. return false;
  575. }
  576. /*********************************************************
  577. *
  578. * functions called by core.c
  579. *
  580. *********************************************************/
  581. int rtl_tx_agg_start(struct ieee80211_hw *hw, const u8 *ra, u16 tid, u16 *ssn)
  582. {
  583. struct rtl_priv *rtlpriv = rtl_priv(hw);
  584. struct rtl_tid_data *tid_data;
  585. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  586. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
  587. ("on ra = %pM tid = %d\n", ra, tid));
  588. if (unlikely(tid >= MAX_TID_COUNT))
  589. return -EINVAL;
  590. if (mac->tids[tid].agg.agg_state != RTL_AGG_OFF) {
  591. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  592. ("Start AGG when state is not RTL_AGG_OFF !\n"));
  593. return -ENXIO;
  594. }
  595. tid_data = &mac->tids[tid];
  596. *ssn = SEQ_TO_SN(tid_data->seq_number);
  597. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
  598. ("HW queue is empty tid:%d\n", tid));
  599. tid_data->agg.agg_state = RTL_AGG_ON;
  600. ieee80211_start_tx_ba_cb_irqsafe(mac->vif, ra, tid);
  601. return 0;
  602. }
  603. int rtl_tx_agg_stop(struct ieee80211_hw *hw, const u8 * ra, u16 tid)
  604. {
  605. int ssn = -1;
  606. struct rtl_priv *rtlpriv = rtl_priv(hw);
  607. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  608. struct rtl_tid_data *tid_data;
  609. if (!ra) {
  610. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
  611. return -EINVAL;
  612. }
  613. if (unlikely(tid >= MAX_TID_COUNT))
  614. return -EINVAL;
  615. if (mac->tids[tid].agg.agg_state != RTL_AGG_ON)
  616. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  617. ("Stopping AGG while state not ON or starting\n"));
  618. tid_data = &mac->tids[tid];
  619. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  620. mac->tids[tid].agg.agg_state = RTL_AGG_OFF;
  621. ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, ra, tid);
  622. return 0;
  623. }
  624. /*********************************************************
  625. *
  626. * wq & timer callback functions
  627. *
  628. *********************************************************/
  629. void rtl_watchdog_wq_callback(void *data)
  630. {
  631. struct rtl_works *rtlworks = container_of_dwork_rtl(data,
  632. struct rtl_works,
  633. watchdog_wq);
  634. struct ieee80211_hw *hw = rtlworks->hw;
  635. struct rtl_priv *rtlpriv = rtl_priv(hw);
  636. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  637. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  638. bool busytraffic = false;
  639. bool higher_busytraffic = false;
  640. bool higher_busyrxtraffic = false;
  641. bool higher_busytxtraffic = false;
  642. u8 idx = 0;
  643. u32 rx_cnt_inp4eriod = 0;
  644. u32 tx_cnt_inp4eriod = 0;
  645. u32 aver_rx_cnt_inperiod = 0;
  646. u32 aver_tx_cnt_inperiod = 0;
  647. bool enter_ps = false;
  648. if (is_hal_stop(rtlhal))
  649. return;
  650. /* <1> Determine if action frame is allowed */
  651. if (mac->link_state > MAC80211_NOLINK) {
  652. if (mac->cnt_after_linked < 20)
  653. mac->cnt_after_linked++;
  654. } else {
  655. mac->cnt_after_linked = 0;
  656. }
  657. /* <2> DM */
  658. rtlpriv->cfg->ops->dm_watchdog(hw);
  659. /*
  660. *<3> to check if traffic busy, if
  661. * busytraffic we don't change channel
  662. */
  663. if (mac->link_state >= MAC80211_LINKED) {
  664. /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
  665. for (idx = 0; idx <= 2; idx++) {
  666. rtlpriv->link_info.num_rx_in4period[idx] =
  667. rtlpriv->link_info.num_rx_in4period[idx + 1];
  668. rtlpriv->link_info.num_tx_in4period[idx] =
  669. rtlpriv->link_info.num_tx_in4period[idx + 1];
  670. }
  671. rtlpriv->link_info.num_rx_in4period[3] =
  672. rtlpriv->link_info.num_rx_inperiod;
  673. rtlpriv->link_info.num_tx_in4period[3] =
  674. rtlpriv->link_info.num_tx_inperiod;
  675. for (idx = 0; idx <= 3; idx++) {
  676. rx_cnt_inp4eriod +=
  677. rtlpriv->link_info.num_rx_in4period[idx];
  678. tx_cnt_inp4eriod +=
  679. rtlpriv->link_info.num_tx_in4period[idx];
  680. }
  681. aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
  682. aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
  683. /* (2) check traffic busy */
  684. if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100)
  685. busytraffic = true;
  686. /* Higher Tx/Rx data. */
  687. if (aver_rx_cnt_inperiod > 4000 ||
  688. aver_tx_cnt_inperiod > 4000) {
  689. higher_busytraffic = true;
  690. /* Extremely high Rx data. */
  691. if (aver_rx_cnt_inperiod > 5000)
  692. higher_busyrxtraffic = true;
  693. else
  694. higher_busytxtraffic = false;
  695. }
  696. if (((rtlpriv->link_info.num_rx_inperiod +
  697. rtlpriv->link_info.num_tx_inperiod) > 8) ||
  698. (rtlpriv->link_info.num_rx_inperiod > 2))
  699. enter_ps = false;
  700. else
  701. enter_ps = true;
  702. /* LeisurePS only work in infra mode. */
  703. if (enter_ps)
  704. rtl_lps_enter(hw);
  705. else
  706. rtl_lps_leave(hw);
  707. }
  708. rtlpriv->link_info.num_rx_inperiod = 0;
  709. rtlpriv->link_info.num_tx_inperiod = 0;
  710. rtlpriv->link_info.busytraffic = busytraffic;
  711. rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
  712. rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
  713. }
  714. void rtl_watch_dog_timer_callback(unsigned long data)
  715. {
  716. struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
  717. struct rtl_priv *rtlpriv = rtl_priv(hw);
  718. queue_delayed_work(rtlpriv->works.rtl_wq,
  719. &rtlpriv->works.watchdog_wq, 0);
  720. mod_timer(&rtlpriv->works.watchdog_timer,
  721. jiffies + MSECS(RTL_WATCH_DOG_TIME));
  722. }
  723. /*********************************************************
  724. *
  725. * sysfs functions
  726. *
  727. *********************************************************/
  728. static ssize_t rtl_show_debug_level(struct device *d,
  729. struct device_attribute *attr, char *buf)
  730. {
  731. struct ieee80211_hw *hw = dev_get_drvdata(d);
  732. struct rtl_priv *rtlpriv = rtl_priv(hw);
  733. return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
  734. }
  735. static ssize_t rtl_store_debug_level(struct device *d,
  736. struct device_attribute *attr,
  737. const char *buf, size_t count)
  738. {
  739. struct ieee80211_hw *hw = dev_get_drvdata(d);
  740. struct rtl_priv *rtlpriv = rtl_priv(hw);
  741. unsigned long val;
  742. int ret;
  743. ret = strict_strtoul(buf, 0, &val);
  744. if (ret) {
  745. printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
  746. } else {
  747. rtlpriv->dbg.global_debuglevel = val;
  748. printk(KERN_DEBUG "debuglevel:%x\n",
  749. rtlpriv->dbg.global_debuglevel);
  750. }
  751. return strnlen(buf, count);
  752. }
  753. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  754. rtl_show_debug_level, rtl_store_debug_level);
  755. static struct attribute *rtl_sysfs_entries[] = {
  756. &dev_attr_debug_level.attr,
  757. NULL
  758. };
  759. /*
  760. * "name" is folder name witch will be
  761. * put in device directory like :
  762. * sys/devices/pci0000:00/0000:00:1c.4/
  763. * 0000:06:00.0/rtl_sysfs
  764. */
  765. struct attribute_group rtl_attribute_group = {
  766. .name = "rtlsysfs",
  767. .attrs = rtl_sysfs_entries,
  768. };
  769. MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
  770. MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
  771. MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
  772. MODULE_LICENSE("GPL");
  773. MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
  774. static int __init rtl_core_module_init(void)
  775. {
  776. if (rtl_rate_control_register())
  777. printk(KERN_ERR "rtlwifi: Unable to register rtl_rc,"
  778. "use default RC !!\n");
  779. return 0;
  780. }
  781. static void __exit rtl_core_module_exit(void)
  782. {
  783. /*RC*/
  784. rtl_rate_control_unregister();
  785. }
  786. module_init(rtl_core_module_init);
  787. module_exit(rtl_core_module_exit);