base.c 26 KB

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