base.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. 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. hw->rate_control_algorithm = "rtl_rc";
  237. /*
  238. * <3> init CRDA must come after init
  239. * mac80211 hw in _rtl_init_mac80211.
  240. */
  241. if (rtl_regd_init(hw, rtl_reg_notifier)) {
  242. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("REGD init failed\n"));
  243. return 1;
  244. } else {
  245. /* CRDA regd hint must after init CRDA */
  246. if (regulatory_hint(hw->wiphy, rtlpriv->regd.alpha2)) {
  247. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  248. ("regulatory_hint fail\n"));
  249. }
  250. }
  251. /* <4> locks */
  252. mutex_init(&rtlpriv->locks.conf_mutex);
  253. spin_lock_init(&rtlpriv->locks.ips_lock);
  254. spin_lock_init(&rtlpriv->locks.irq_th_lock);
  255. spin_lock_init(&rtlpriv->locks.h2c_lock);
  256. spin_lock_init(&rtlpriv->locks.rf_ps_lock);
  257. spin_lock_init(&rtlpriv->locks.rf_lock);
  258. spin_lock_init(&rtlpriv->locks.lps_lock);
  259. rtlmac->link_state = MAC80211_NOLINK;
  260. /* <5> init deferred work */
  261. _rtl_init_deferred_work(hw);
  262. return 0;
  263. }
  264. void rtl_deinit_core(struct ieee80211_hw *hw)
  265. {
  266. }
  267. void rtl_init_rx_config(struct ieee80211_hw *hw)
  268. {
  269. struct rtl_priv *rtlpriv = rtl_priv(hw);
  270. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  271. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
  272. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_MGT_FILTER,
  273. (u8 *) (&mac->rx_mgt_filter));
  274. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CTRL_FILTER,
  275. (u8 *) (&mac->rx_ctrl_filter));
  276. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_DATA_FILTER,
  277. (u8 *) (&mac->rx_data_filter));
  278. }
  279. /*********************************************************
  280. *
  281. * tx information functions
  282. *
  283. *********************************************************/
  284. static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
  285. struct rtl_tcb_desc *tcb_desc,
  286. struct ieee80211_tx_info *info)
  287. {
  288. struct rtl_priv *rtlpriv = rtl_priv(hw);
  289. u8 rate_flag = info->control.rates[0].flags;
  290. tcb_desc->use_shortpreamble = false;
  291. /* 1M can only use Long Preamble. 11B spec */
  292. if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
  293. return;
  294. else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  295. tcb_desc->use_shortpreamble = true;
  296. return;
  297. }
  298. static void _rtl_query_shortgi(struct ieee80211_hw *hw,
  299. struct rtl_tcb_desc *tcb_desc,
  300. struct ieee80211_tx_info *info)
  301. {
  302. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  303. u8 rate_flag = info->control.rates[0].flags;
  304. tcb_desc->use_shortgi = false;
  305. if (!mac->ht_enable)
  306. return;
  307. if (!mac->sgi_40 && !mac->sgi_20)
  308. return;
  309. if ((mac->bw_40 == true) && mac->sgi_40)
  310. tcb_desc->use_shortgi = true;
  311. else if ((mac->bw_40 == false) && mac->sgi_20)
  312. tcb_desc->use_shortgi = true;
  313. if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
  314. tcb_desc->use_shortgi = false;
  315. }
  316. static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
  317. struct rtl_tcb_desc *tcb_desc,
  318. struct ieee80211_tx_info *info)
  319. {
  320. struct rtl_priv *rtlpriv = rtl_priv(hw);
  321. u8 rate_flag = info->control.rates[0].flags;
  322. /* Common Settings */
  323. tcb_desc->rts_stbc = false;
  324. tcb_desc->cts_enable = false;
  325. tcb_desc->rts_sc = 0;
  326. tcb_desc->rts_bw = false;
  327. tcb_desc->rts_use_shortpreamble = false;
  328. tcb_desc->rts_use_shortgi = false;
  329. if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  330. /* Use CTS-to-SELF in protection mode. */
  331. tcb_desc->rts_enable = true;
  332. tcb_desc->cts_enable = true;
  333. tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
  334. } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
  335. /* Use RTS-CTS in protection mode. */
  336. tcb_desc->rts_enable = true;
  337. tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
  338. }
  339. }
  340. static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
  341. struct rtl_tcb_desc *tcb_desc)
  342. {
  343. struct rtl_priv *rtlpriv = rtl_priv(hw);
  344. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  345. if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
  346. if (mac->opmode == NL80211_IFTYPE_STATION)
  347. tcb_desc->ratr_index = 0;
  348. else if (mac->opmode == NL80211_IFTYPE_ADHOC) {
  349. if (tcb_desc->multicast || tcb_desc->broadcast) {
  350. tcb_desc->hw_rate =
  351. rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
  352. tcb_desc->use_driver_rate = 1;
  353. } else {
  354. /* TODO */
  355. }
  356. }
  357. }
  358. if (rtlpriv->dm.useramask) {
  359. /* TODO we will differentiate adhoc and station futrue */
  360. tcb_desc->mac_id = 0;
  361. if ((mac->mode == WIRELESS_MODE_N_24G) ||
  362. (mac->mode == WIRELESS_MODE_N_5G)) {
  363. tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
  364. } else if (mac->mode & WIRELESS_MODE_G) {
  365. tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
  366. } else if (mac->mode & WIRELESS_MODE_B) {
  367. tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
  368. }
  369. }
  370. }
  371. static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
  372. struct rtl_tcb_desc *tcb_desc)
  373. {
  374. struct rtl_priv *rtlpriv = rtl_priv(hw);
  375. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  376. tcb_desc->packet_bw = false;
  377. if (!mac->bw_40 || !mac->ht_enable)
  378. return;
  379. if (tcb_desc->multicast || tcb_desc->broadcast)
  380. return;
  381. /*use legency rate, shall use 20MHz */
  382. if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
  383. return;
  384. tcb_desc->packet_bw = true;
  385. }
  386. static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
  387. {
  388. struct rtl_priv *rtlpriv = rtl_priv(hw);
  389. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  390. u8 hw_rate;
  391. if (get_rf_type(rtlphy) == RF_2T2R)
  392. hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
  393. else
  394. hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
  395. return hw_rate;
  396. }
  397. void rtl_get_tcb_desc(struct ieee80211_hw *hw,
  398. struct ieee80211_tx_info *info,
  399. struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
  400. {
  401. struct rtl_priv *rtlpriv = rtl_priv(hw);
  402. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  403. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  404. struct ieee80211_rate *txrate;
  405. __le16 fc = hdr->frame_control;
  406. memset(tcb_desc, 0, sizeof(struct rtl_tcb_desc));
  407. if (ieee80211_is_data(fc)) {
  408. txrate = ieee80211_get_tx_rate(hw, info);
  409. tcb_desc->hw_rate = txrate->hw_value;
  410. /*
  411. *we set data rate RTL_RC_CCK_RATE1M
  412. *in rtl_rc.c if skb is special data or
  413. *mgt which need low data rate.
  414. */
  415. /*
  416. *So tcb_desc->hw_rate is just used for
  417. *special data and mgt frames
  418. */
  419. if (tcb_desc->hw_rate < rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M]) {
  420. tcb_desc->use_driver_rate = true;
  421. tcb_desc->ratr_index = 7;
  422. tcb_desc->hw_rate =
  423. rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M];
  424. tcb_desc->disable_ratefallback = 1;
  425. } else {
  426. /*
  427. *because hw will nerver use hw_rate
  428. *when tcb_desc->use_driver_rate = false
  429. *so we never set highest N rate here,
  430. *and N rate will all be controled by FW
  431. *when tcb_desc->use_driver_rate = false
  432. */
  433. if (rtlmac->ht_enable) {
  434. tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
  435. } else {
  436. if (rtlmac->mode == WIRELESS_MODE_B) {
  437. tcb_desc->hw_rate =
  438. rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
  439. } else {
  440. tcb_desc->hw_rate =
  441. rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
  442. }
  443. }
  444. }
  445. if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
  446. tcb_desc->multicast = 1;
  447. else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  448. tcb_desc->broadcast = 1;
  449. _rtl_txrate_selectmode(hw, tcb_desc);
  450. _rtl_query_bandwidth_mode(hw, tcb_desc);
  451. _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
  452. _rtl_query_shortgi(hw, tcb_desc, info);
  453. _rtl_query_protection_mode(hw, tcb_desc, info);
  454. } else {
  455. tcb_desc->use_driver_rate = true;
  456. tcb_desc->ratr_index = 7;
  457. tcb_desc->disable_ratefallback = 1;
  458. tcb_desc->mac_id = 0;
  459. tcb_desc->hw_rate = rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M];
  460. }
  461. }
  462. EXPORT_SYMBOL(rtl_get_tcb_desc);
  463. bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
  464. {
  465. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  466. struct rtl_priv *rtlpriv = rtl_priv(hw);
  467. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  468. __le16 fc = hdr->frame_control;
  469. if (ieee80211_is_auth(fc)) {
  470. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
  471. rtl_ips_nic_on(hw);
  472. mac->link_state = MAC80211_LINKING;
  473. }
  474. return true;
  475. }
  476. bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
  477. {
  478. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  479. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  480. struct rtl_priv *rtlpriv = rtl_priv(hw);
  481. __le16 fc = hdr->frame_control;
  482. u8 *act = (u8 *) (((u8 *) skb->data + MAC80211_3ADDR_LEN));
  483. u8 category;
  484. if (!ieee80211_is_action(fc))
  485. return true;
  486. category = *act;
  487. act++;
  488. switch (category) {
  489. case ACT_CAT_BA:
  490. switch (*act) {
  491. case ACT_ADDBAREQ:
  492. if (mac->act_scanning)
  493. return false;
  494. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  495. ("%s ACT_ADDBAREQ From :" MAC_FMT "\n",
  496. is_tx ? "Tx" : "Rx", MAC_ARG(hdr->addr2)));
  497. break;
  498. case ACT_ADDBARSP:
  499. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  500. ("%s ACT_ADDBARSP From :" MAC_FMT "\n",
  501. is_tx ? "Tx" : "Rx", MAC_ARG(hdr->addr2)));
  502. break;
  503. case ACT_DELBA:
  504. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  505. ("ACT_ADDBADEL From :" MAC_FMT "\n",
  506. MAC_ARG(hdr->addr2)));
  507. break;
  508. }
  509. break;
  510. default:
  511. break;
  512. }
  513. return true;
  514. }
  515. /*should call before software enc*/
  516. u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
  517. {
  518. struct rtl_priv *rtlpriv = rtl_priv(hw);
  519. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  520. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  521. __le16 fc = hdr->frame_control;
  522. u16 ether_type;
  523. u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
  524. const struct iphdr *ip;
  525. if (!ieee80211_is_data(fc))
  526. goto end;
  527. if (ieee80211_is_nullfunc(fc))
  528. return true;
  529. ip = (struct iphdr *)((u8 *) skb->data + mac_hdr_len +
  530. SNAP_SIZE + PROTOC_TYPE_SIZE);
  531. ether_type = *(u16 *) ((u8 *) skb->data + mac_hdr_len + SNAP_SIZE);
  532. if (ETH_P_IP == ether_type) {
  533. if (IPPROTO_UDP == ip->protocol) {
  534. struct udphdr *udp = (struct udphdr *)((u8 *) ip +
  535. (ip->ihl << 2));
  536. if (((((u8 *) udp)[1] == 68) &&
  537. (((u8 *) udp)[3] == 67)) ||
  538. ((((u8 *) udp)[1] == 67) &&
  539. (((u8 *) udp)[3] == 68))) {
  540. /*
  541. * 68 : UDP BOOTP client
  542. * 67 : UDP BOOTP server
  543. */
  544. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
  545. DBG_DMESG, ("dhcp %s !!\n",
  546. (is_tx) ? "Tx" : "Rx"));
  547. if (is_tx) {
  548. rtl_lps_leave(hw);
  549. ppsc->last_delaylps_stamp_jiffies =
  550. jiffies;
  551. }
  552. return true;
  553. }
  554. }
  555. } else if (ETH_P_ARP == ether_type) {
  556. if (is_tx) {
  557. rtl_lps_leave(hw);
  558. ppsc->last_delaylps_stamp_jiffies = jiffies;
  559. }
  560. return true;
  561. } else if (ETH_P_PAE == ether_type) {
  562. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  563. ("802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx"));
  564. if (is_tx) {
  565. rtl_lps_leave(hw);
  566. ppsc->last_delaylps_stamp_jiffies = jiffies;
  567. }
  568. return true;
  569. } else if (ETH_P_IPV6 == ether_type) {
  570. /* IPv6 */
  571. return true;
  572. }
  573. end:
  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);