base.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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 = 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 = 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 rf on */
  213. rtlpriv->rfkill.rfkill_state = 1;
  214. radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
  215. if (valid) {
  216. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  217. (KERN_INFO "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. if (rtl_rate_control_register()) {
  238. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  239. ("rtl: Unable to register rtl_rc,"
  240. "use default RC !!\n"));
  241. } else {
  242. hw->rate_control_algorithm = "rtl_rc";
  243. }
  244. /*
  245. * <3> init CRDA must come after init
  246. * mac80211 hw in _rtl_init_mac80211.
  247. */
  248. if (rtl_regd_init(hw, rtl_reg_notifier)) {
  249. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("REGD init failed\n"));
  250. return 1;
  251. } else {
  252. /* CRDA regd hint must after init CRDA */
  253. if (regulatory_hint(hw->wiphy, rtlpriv->regd.alpha2)) {
  254. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  255. ("regulatory_hint fail\n"));
  256. }
  257. }
  258. /* <4> locks */
  259. mutex_init(&rtlpriv->locks.conf_mutex);
  260. spin_lock_init(&rtlpriv->locks.ips_lock);
  261. spin_lock_init(&rtlpriv->locks.irq_th_lock);
  262. spin_lock_init(&rtlpriv->locks.h2c_lock);
  263. spin_lock_init(&rtlpriv->locks.rf_ps_lock);
  264. spin_lock_init(&rtlpriv->locks.rf_lock);
  265. spin_lock_init(&rtlpriv->locks.lps_lock);
  266. rtlmac->link_state = MAC80211_NOLINK;
  267. /* <5> init deferred work */
  268. _rtl_init_deferred_work(hw);
  269. return 0;
  270. }
  271. void rtl_deinit_core(struct ieee80211_hw *hw)
  272. {
  273. /*RC*/
  274. rtl_rate_control_unregister();
  275. }
  276. void rtl_init_rx_config(struct ieee80211_hw *hw)
  277. {
  278. struct rtl_priv *rtlpriv = rtl_priv(hw);
  279. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  280. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
  281. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_MGT_FILTER,
  282. (u8 *) (&mac->rx_mgt_filter));
  283. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CTRL_FILTER,
  284. (u8 *) (&mac->rx_ctrl_filter));
  285. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_DATA_FILTER,
  286. (u8 *) (&mac->rx_data_filter));
  287. }
  288. /*********************************************************
  289. *
  290. * tx information functions
  291. *
  292. *********************************************************/
  293. static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
  294. struct rtl_tcb_desc *tcb_desc,
  295. struct ieee80211_tx_info *info)
  296. {
  297. struct rtl_priv *rtlpriv = rtl_priv(hw);
  298. u8 rate_flag = info->control.rates[0].flags;
  299. tcb_desc->use_shortpreamble = false;
  300. /* 1M can only use Long Preamble. 11B spec */
  301. if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
  302. return;
  303. else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  304. tcb_desc->use_shortpreamble = true;
  305. return;
  306. }
  307. static void _rtl_query_shortgi(struct ieee80211_hw *hw,
  308. struct rtl_tcb_desc *tcb_desc,
  309. struct ieee80211_tx_info *info)
  310. {
  311. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  312. u8 rate_flag = info->control.rates[0].flags;
  313. tcb_desc->use_shortgi = false;
  314. if (!mac->ht_enable)
  315. return;
  316. if (!mac->sgi_40 && !mac->sgi_20)
  317. return;
  318. if ((mac->bw_40 == true) && mac->sgi_40)
  319. tcb_desc->use_shortgi = true;
  320. else if ((mac->bw_40 == false) && mac->sgi_20)
  321. tcb_desc->use_shortgi = true;
  322. if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
  323. tcb_desc->use_shortgi = false;
  324. }
  325. static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
  326. struct rtl_tcb_desc *tcb_desc,
  327. struct ieee80211_tx_info *info)
  328. {
  329. struct rtl_priv *rtlpriv = rtl_priv(hw);
  330. u8 rate_flag = info->control.rates[0].flags;
  331. /* Common Settings */
  332. tcb_desc->b_rts_stbc = false;
  333. tcb_desc->b_cts_enable = false;
  334. tcb_desc->rts_sc = 0;
  335. tcb_desc->b_rts_bw = false;
  336. tcb_desc->b_rts_use_shortpreamble = false;
  337. tcb_desc->b_rts_use_shortgi = false;
  338. if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  339. /* Use CTS-to-SELF in protection mode. */
  340. tcb_desc->b_rts_enable = true;
  341. tcb_desc->b_cts_enable = true;
  342. tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
  343. } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
  344. /* Use RTS-CTS in protection mode. */
  345. tcb_desc->b_rts_enable = true;
  346. tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
  347. }
  348. }
  349. static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
  350. struct rtl_tcb_desc *tcb_desc)
  351. {
  352. struct rtl_priv *rtlpriv = rtl_priv(hw);
  353. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  354. if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
  355. if (mac->opmode == NL80211_IFTYPE_STATION)
  356. tcb_desc->ratr_index = 0;
  357. else if (mac->opmode == NL80211_IFTYPE_ADHOC) {
  358. if (tcb_desc->b_multicast || tcb_desc->b_broadcast) {
  359. tcb_desc->hw_rate =
  360. rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
  361. tcb_desc->use_driver_rate = 1;
  362. } else {
  363. /* TODO */
  364. }
  365. }
  366. }
  367. if (rtlpriv->dm.b_useramask) {
  368. /* TODO we will differentiate adhoc and station futrue */
  369. tcb_desc->mac_id = 0;
  370. if ((mac->mode == WIRELESS_MODE_N_24G) ||
  371. (mac->mode == WIRELESS_MODE_N_5G)) {
  372. tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
  373. } else if (mac->mode & WIRELESS_MODE_G) {
  374. tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
  375. } else if (mac->mode & WIRELESS_MODE_B) {
  376. tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
  377. }
  378. }
  379. }
  380. static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
  381. struct rtl_tcb_desc *tcb_desc)
  382. {
  383. struct rtl_priv *rtlpriv = rtl_priv(hw);
  384. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  385. tcb_desc->b_packet_bw = false;
  386. if (!mac->bw_40 || !mac->ht_enable)
  387. return;
  388. if (tcb_desc->b_multicast || tcb_desc->b_broadcast)
  389. return;
  390. /*use legency rate, shall use 20MHz */
  391. if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
  392. return;
  393. tcb_desc->b_packet_bw = true;
  394. }
  395. static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
  396. {
  397. struct rtl_priv *rtlpriv = rtl_priv(hw);
  398. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  399. u8 hw_rate;
  400. if (get_rf_type(rtlphy) == RF_2T2R)
  401. hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
  402. else
  403. hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
  404. return hw_rate;
  405. }
  406. void rtl_get_tcb_desc(struct ieee80211_hw *hw,
  407. struct ieee80211_tx_info *info,
  408. struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
  409. {
  410. struct rtl_priv *rtlpriv = rtl_priv(hw);
  411. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  412. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  413. struct ieee80211_rate *txrate;
  414. u16 fc = le16_to_cpu(hdr->frame_control);
  415. memset(tcb_desc, 0, sizeof(struct rtl_tcb_desc));
  416. if (ieee80211_is_data(fc)) {
  417. txrate = ieee80211_get_tx_rate(hw, info);
  418. tcb_desc->hw_rate = txrate->hw_value;
  419. /*
  420. *we set data rate RTL_RC_CCK_RATE1M
  421. *in rtl_rc.c if skb is special data or
  422. *mgt which need low data rate.
  423. */
  424. /*
  425. *So tcb_desc->hw_rate is just used for
  426. *special data and mgt frames
  427. */
  428. if (tcb_desc->hw_rate < rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M]) {
  429. tcb_desc->use_driver_rate = true;
  430. tcb_desc->ratr_index = 7;
  431. tcb_desc->hw_rate =
  432. rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M];
  433. tcb_desc->disable_ratefallback = 1;
  434. } else {
  435. /*
  436. *because hw will nerver use hw_rate
  437. *when tcb_desc->use_driver_rate = false
  438. *so we never set highest N rate here,
  439. *and N rate will all be controled by FW
  440. *when tcb_desc->use_driver_rate = false
  441. */
  442. if (rtlmac->ht_enable) {
  443. tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
  444. } else {
  445. if (rtlmac->mode == WIRELESS_MODE_B) {
  446. tcb_desc->hw_rate =
  447. rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
  448. } else {
  449. tcb_desc->hw_rate =
  450. rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
  451. }
  452. }
  453. }
  454. if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
  455. tcb_desc->b_multicast = 1;
  456. else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  457. tcb_desc->b_broadcast = 1;
  458. _rtl_txrate_selectmode(hw, tcb_desc);
  459. _rtl_query_bandwidth_mode(hw, tcb_desc);
  460. _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
  461. _rtl_query_shortgi(hw, tcb_desc, info);
  462. _rtl_query_protection_mode(hw, tcb_desc, info);
  463. } else {
  464. tcb_desc->use_driver_rate = true;
  465. tcb_desc->ratr_index = 7;
  466. tcb_desc->disable_ratefallback = 1;
  467. tcb_desc->mac_id = 0;
  468. tcb_desc->hw_rate = rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M];
  469. }
  470. }
  471. EXPORT_SYMBOL(rtl_get_tcb_desc);
  472. bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
  473. {
  474. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  475. struct rtl_priv *rtlpriv = rtl_priv(hw);
  476. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  477. u16 fc = le16_to_cpu(hdr->frame_control);
  478. if (ieee80211_is_auth(fc)) {
  479. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
  480. rtl_ips_nic_on(hw);
  481. mac->link_state = MAC80211_LINKING;
  482. }
  483. return true;
  484. }
  485. bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
  486. {
  487. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  488. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  489. struct rtl_priv *rtlpriv = rtl_priv(hw);
  490. u16 fc = le16_to_cpu(hdr->frame_control);
  491. u8 *act = (u8 *) (((u8 *) skb->data + MAC80211_3ADDR_LEN));
  492. u8 category;
  493. if (!ieee80211_is_action(fc))
  494. return true;
  495. category = *act;
  496. act++;
  497. switch (category) {
  498. case ACT_CAT_BA:
  499. switch (*act) {
  500. case ACT_ADDBAREQ:
  501. if (mac->act_scanning)
  502. return false;
  503. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  504. ("%s ACT_ADDBAREQ From :" MAC_FMT "\n",
  505. is_tx ? "Tx" : "Rx", MAC_ARG(hdr->addr2)));
  506. break;
  507. case ACT_ADDBARSP:
  508. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  509. ("%s ACT_ADDBARSP From :" MAC_FMT "\n",
  510. is_tx ? "Tx" : "Rx", MAC_ARG(hdr->addr2)));
  511. break;
  512. case ACT_DELBA:
  513. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  514. ("ACT_ADDBADEL From :" MAC_FMT "\n",
  515. MAC_ARG(hdr->addr2)));
  516. break;
  517. }
  518. break;
  519. default:
  520. break;
  521. }
  522. return true;
  523. }
  524. /*should call before software enc*/
  525. u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
  526. {
  527. struct rtl_priv *rtlpriv = rtl_priv(hw);
  528. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  529. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  530. u16 fc = le16_to_cpu(hdr->frame_control);
  531. u16 ether_type;
  532. u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
  533. const struct iphdr *ip;
  534. if (!ieee80211_is_data(fc))
  535. goto end;
  536. if (ieee80211_is_nullfunc(fc))
  537. return true;
  538. ip = (struct iphdr *)((u8 *) skb->data + mac_hdr_len +
  539. SNAP_SIZE + PROTOC_TYPE_SIZE);
  540. ether_type = *(u16 *) ((u8 *) skb->data + mac_hdr_len + SNAP_SIZE);
  541. ether_type = ntohs(ether_type);
  542. if (ETH_P_IP == ether_type) {
  543. if (IPPROTO_UDP == ip->protocol) {
  544. struct udphdr *udp = (struct udphdr *)((u8 *) ip +
  545. (ip->ihl << 2));
  546. if (((((u8 *) udp)[1] == 68) &&
  547. (((u8 *) udp)[3] == 67)) ||
  548. ((((u8 *) udp)[1] == 67) &&
  549. (((u8 *) udp)[3] == 68))) {
  550. /*
  551. * 68 : UDP BOOTP client
  552. * 67 : UDP BOOTP server
  553. */
  554. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
  555. DBG_DMESG, ("dhcp %s !!\n",
  556. (is_tx) ? "Tx" : "Rx"));
  557. if (is_tx) {
  558. rtl_lps_leave(hw);
  559. ppsc->last_delaylps_stamp_jiffies =
  560. jiffies;
  561. }
  562. return true;
  563. }
  564. }
  565. } else if (ETH_P_ARP == ether_type) {
  566. if (is_tx) {
  567. rtl_lps_leave(hw);
  568. ppsc->last_delaylps_stamp_jiffies = jiffies;
  569. }
  570. return true;
  571. } else if (ETH_P_PAE == ether_type) {
  572. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  573. ("802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx"));
  574. if (is_tx) {
  575. rtl_lps_leave(hw);
  576. ppsc->last_delaylps_stamp_jiffies = jiffies;
  577. }
  578. return true;
  579. } else if (0x86DD == ether_type) {
  580. return true;
  581. }
  582. end:
  583. return false;
  584. }
  585. /*********************************************************
  586. *
  587. * functions called by core.c
  588. *
  589. *********************************************************/
  590. int rtl_tx_agg_start(struct ieee80211_hw *hw, const u8 *ra, u16 tid, u16 *ssn)
  591. {
  592. struct rtl_priv *rtlpriv = rtl_priv(hw);
  593. struct rtl_tid_data *tid_data;
  594. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  595. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
  596. ("on ra = %pM tid = %d\n", ra, tid));
  597. if (unlikely(tid >= MAX_TID_COUNT))
  598. return -EINVAL;
  599. if (mac->tids[tid].agg.agg_state != RTL_AGG_OFF) {
  600. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  601. ("Start AGG when state is not RTL_AGG_OFF !\n"));
  602. return -ENXIO;
  603. }
  604. tid_data = &mac->tids[tid];
  605. *ssn = SEQ_TO_SN(tid_data->seq_number);
  606. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
  607. ("HW queue is empty tid:%d\n", tid));
  608. tid_data->agg.agg_state = RTL_AGG_ON;
  609. ieee80211_start_tx_ba_cb_irqsafe(mac->vif, ra, tid);
  610. return 0;
  611. }
  612. int rtl_tx_agg_stop(struct ieee80211_hw *hw, const u8 * ra, u16 tid)
  613. {
  614. int ssn = -1;
  615. struct rtl_priv *rtlpriv = rtl_priv(hw);
  616. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  617. struct rtl_tid_data *tid_data;
  618. if (!ra) {
  619. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
  620. return -EINVAL;
  621. }
  622. if (unlikely(tid >= MAX_TID_COUNT))
  623. return -EINVAL;
  624. if (mac->tids[tid].agg.agg_state != RTL_AGG_ON)
  625. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  626. ("Stopping AGG while state not ON or starting\n"));
  627. tid_data = &mac->tids[tid];
  628. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  629. mac->tids[tid].agg.agg_state = RTL_AGG_OFF;
  630. ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, ra, tid);
  631. return 0;
  632. }
  633. /*********************************************************
  634. *
  635. * wq & timer callback functions
  636. *
  637. *********************************************************/
  638. void rtl_watchdog_wq_callback(void *data)
  639. {
  640. struct rtl_works *rtlworks = container_of_dwork_rtl(data,
  641. struct rtl_works,
  642. watchdog_wq);
  643. struct ieee80211_hw *hw = rtlworks->hw;
  644. struct rtl_priv *rtlpriv = rtl_priv(hw);
  645. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  646. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  647. bool b_busytraffic = false;
  648. bool b_higher_busytraffic = false;
  649. bool b_higher_busyrxtraffic = false;
  650. bool b_higher_busytxtraffic = false;
  651. u8 idx = 0;
  652. u32 rx_cnt_inp4eriod = 0;
  653. u32 tx_cnt_inp4eriod = 0;
  654. u32 aver_rx_cnt_inperiod = 0;
  655. u32 aver_tx_cnt_inperiod = 0;
  656. bool benter_ps = false;
  657. if (is_hal_stop(rtlhal))
  658. return;
  659. /* <1> Determine if action frame is allowed */
  660. if (mac->link_state > MAC80211_NOLINK) {
  661. if (mac->cnt_after_linked < 20)
  662. mac->cnt_after_linked++;
  663. } else {
  664. mac->cnt_after_linked = 0;
  665. }
  666. /* <2> DM */
  667. rtlpriv->cfg->ops->dm_watchdog(hw);
  668. /*
  669. *<3> to check if traffic busy, if
  670. * busytraffic we don't change channel
  671. */
  672. if (mac->link_state >= MAC80211_LINKED) {
  673. /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
  674. for (idx = 0; idx <= 2; idx++) {
  675. rtlpriv->link_info.num_rx_in4period[idx] =
  676. rtlpriv->link_info.num_rx_in4period[idx + 1];
  677. rtlpriv->link_info.num_tx_in4period[idx] =
  678. rtlpriv->link_info.num_tx_in4period[idx + 1];
  679. }
  680. rtlpriv->link_info.num_rx_in4period[3] =
  681. rtlpriv->link_info.num_rx_inperiod;
  682. rtlpriv->link_info.num_tx_in4period[3] =
  683. rtlpriv->link_info.num_tx_inperiod;
  684. for (idx = 0; idx <= 3; idx++) {
  685. rx_cnt_inp4eriod +=
  686. rtlpriv->link_info.num_rx_in4period[idx];
  687. tx_cnt_inp4eriod +=
  688. rtlpriv->link_info.num_tx_in4period[idx];
  689. }
  690. aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
  691. aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
  692. /* (2) check traffic busy */
  693. if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100)
  694. b_busytraffic = true;
  695. /* Higher Tx/Rx data. */
  696. if (aver_rx_cnt_inperiod > 4000 ||
  697. aver_tx_cnt_inperiod > 4000) {
  698. b_higher_busytraffic = true;
  699. /* Extremely high Rx data. */
  700. if (aver_rx_cnt_inperiod > 5000)
  701. b_higher_busyrxtraffic = true;
  702. else
  703. b_higher_busytxtraffic = false;
  704. }
  705. if (((rtlpriv->link_info.num_rx_inperiod +
  706. rtlpriv->link_info.num_tx_inperiod) > 8) ||
  707. (rtlpriv->link_info.num_rx_inperiod > 2))
  708. benter_ps = false;
  709. else
  710. benter_ps = true;
  711. /* LeisurePS only work in infra mode. */
  712. if (benter_ps)
  713. rtl_lps_enter(hw);
  714. else
  715. rtl_lps_leave(hw);
  716. }
  717. rtlpriv->link_info.num_rx_inperiod = 0;
  718. rtlpriv->link_info.num_tx_inperiod = 0;
  719. rtlpriv->link_info.b_busytraffic = b_busytraffic;
  720. rtlpriv->link_info.b_higher_busytraffic = b_higher_busytraffic;
  721. rtlpriv->link_info.b_higher_busyrxtraffic = b_higher_busyrxtraffic;
  722. }
  723. void rtl_watch_dog_timer_callback(unsigned long data)
  724. {
  725. struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
  726. struct rtl_priv *rtlpriv = rtl_priv(hw);
  727. queue_delayed_work(rtlpriv->works.rtl_wq,
  728. &rtlpriv->works.watchdog_wq, 0);
  729. mod_timer(&rtlpriv->works.watchdog_timer,
  730. jiffies + MSECS(RTL_WATCH_DOG_TIME));
  731. }
  732. /*********************************************************
  733. *
  734. * sysfs functions
  735. *
  736. *********************************************************/
  737. static ssize_t rtl_show_debug_level(struct device *d,
  738. struct device_attribute *attr, char *buf)
  739. {
  740. struct ieee80211_hw *hw = dev_get_drvdata(d);
  741. struct rtl_priv *rtlpriv = rtl_priv(hw);
  742. return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
  743. }
  744. static ssize_t rtl_store_debug_level(struct device *d,
  745. struct device_attribute *attr,
  746. const char *buf, size_t count)
  747. {
  748. struct ieee80211_hw *hw = dev_get_drvdata(d);
  749. struct rtl_priv *rtlpriv = rtl_priv(hw);
  750. unsigned long val;
  751. int ret;
  752. ret = strict_strtoul(buf, 0, &val);
  753. if (ret) {
  754. printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
  755. } else {
  756. rtlpriv->dbg.global_debuglevel = val;
  757. printk(KERN_DEBUG "debuglevel:%x\n",
  758. rtlpriv->dbg.global_debuglevel);
  759. }
  760. return strnlen(buf, count);
  761. }
  762. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  763. rtl_show_debug_level, rtl_store_debug_level);
  764. static struct attribute *rtl_sysfs_entries[] = {
  765. &dev_attr_debug_level.attr,
  766. NULL
  767. };
  768. /*
  769. * "name" is folder name witch will be
  770. * put in device directory like :
  771. * sys/devices/pci0000:00/0000:00:1c.4/
  772. * 0000:06:00.0/rtl_sysfs
  773. */
  774. struct attribute_group rtl_attribute_group = {
  775. .name = "rtlsysfs",
  776. .attrs = rtl_sysfs_entries,
  777. };
  778. MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
  779. MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
  780. MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
  781. MODULE_LICENSE("GPL");
  782. MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
  783. static int __init rtl_core_module_init(void)
  784. {
  785. return 0;
  786. }
  787. static void __exit rtl_core_module_exit(void)
  788. {
  789. }
  790. module_init(rtl_core_module_init);
  791. module_exit(rtl_core_module_exit);