core.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. #include "wifi.h"
  29. #include "core.h"
  30. #include "cam.h"
  31. #include "base.h"
  32. #include "ps.h"
  33. /*mutex for start & stop is must here. */
  34. static int rtl_op_start(struct ieee80211_hw *hw)
  35. {
  36. int err = 0;
  37. struct rtl_priv *rtlpriv = rtl_priv(hw);
  38. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  39. if (!is_hal_stop(rtlhal))
  40. return 0;
  41. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  42. return 0;
  43. mutex_lock(&rtlpriv->locks.conf_mutex);
  44. err = rtlpriv->intf_ops->adapter_start(hw);
  45. if (err)
  46. goto out;
  47. rtl_watch_dog_timer_callback((unsigned long)hw);
  48. out:
  49. mutex_unlock(&rtlpriv->locks.conf_mutex);
  50. return err;
  51. }
  52. static void rtl_op_stop(struct ieee80211_hw *hw)
  53. {
  54. struct rtl_priv *rtlpriv = rtl_priv(hw);
  55. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  56. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  57. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  58. if (is_hal_stop(rtlhal))
  59. return;
  60. if (unlikely(ppsc->rfpwr_state == ERFOFF)) {
  61. rtl_ips_nic_on(hw);
  62. mdelay(1);
  63. }
  64. mutex_lock(&rtlpriv->locks.conf_mutex);
  65. mac->link_state = MAC80211_NOLINK;
  66. memset(mac->bssid, 0, 6);
  67. /*reset sec info */
  68. rtl_cam_reset_sec_info(hw);
  69. rtl_deinit_deferred_work(hw);
  70. rtlpriv->intf_ops->adapter_stop(hw);
  71. mutex_unlock(&rtlpriv->locks.conf_mutex);
  72. }
  73. static void rtl_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  74. {
  75. struct rtl_priv *rtlpriv = rtl_priv(hw);
  76. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  77. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  78. if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
  79. goto err_free;
  80. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  81. goto err_free;
  82. rtlpriv->intf_ops->adapter_tx(hw, skb);
  83. return;
  84. err_free:
  85. dev_kfree_skb_any(skb);
  86. }
  87. static int rtl_op_add_interface(struct ieee80211_hw *hw,
  88. struct ieee80211_vif *vif)
  89. {
  90. struct rtl_priv *rtlpriv = rtl_priv(hw);
  91. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  92. int err = 0;
  93. if (mac->vif) {
  94. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  95. ("vif has been set!! mac->vif = 0x%p\n", mac->vif));
  96. return -EOPNOTSUPP;
  97. }
  98. rtl_ips_nic_on(hw);
  99. mutex_lock(&rtlpriv->locks.conf_mutex);
  100. switch (vif->type) {
  101. case NL80211_IFTYPE_STATION:
  102. if (mac->beacon_enabled == 1) {
  103. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  104. ("NL80211_IFTYPE_STATION\n"));
  105. mac->beacon_enabled = 0;
  106. rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
  107. rtlpriv->cfg->maps
  108. [RTL_IBSS_INT_MASKS]);
  109. }
  110. break;
  111. case NL80211_IFTYPE_ADHOC:
  112. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  113. ("NL80211_IFTYPE_ADHOC\n"));
  114. mac->link_state = MAC80211_LINKED;
  115. rtlpriv->cfg->ops->set_bcn_reg(hw);
  116. break;
  117. case NL80211_IFTYPE_AP:
  118. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  119. ("NL80211_IFTYPE_AP\n"));
  120. break;
  121. default:
  122. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  123. ("operation mode %d is not support!\n", vif->type));
  124. err = -EOPNOTSUPP;
  125. goto out;
  126. }
  127. mac->vif = vif;
  128. mac->opmode = vif->type;
  129. rtlpriv->cfg->ops->set_network_type(hw, vif->type);
  130. memcpy(mac->mac_addr, vif->addr, ETH_ALEN);
  131. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
  132. out:
  133. mutex_unlock(&rtlpriv->locks.conf_mutex);
  134. return err;
  135. }
  136. static void rtl_op_remove_interface(struct ieee80211_hw *hw,
  137. struct ieee80211_vif *vif)
  138. {
  139. struct rtl_priv *rtlpriv = rtl_priv(hw);
  140. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  141. mutex_lock(&rtlpriv->locks.conf_mutex);
  142. /* Free beacon resources */
  143. if ((mac->opmode == NL80211_IFTYPE_AP) ||
  144. (mac->opmode == NL80211_IFTYPE_ADHOC) ||
  145. (mac->opmode == NL80211_IFTYPE_MESH_POINT)) {
  146. if (mac->beacon_enabled == 1) {
  147. mac->beacon_enabled = 0;
  148. rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
  149. rtlpriv->cfg->maps
  150. [RTL_IBSS_INT_MASKS]);
  151. }
  152. }
  153. /*
  154. *Note: We assume NL80211_IFTYPE_UNSPECIFIED as
  155. *NO LINK for our hardware.
  156. */
  157. mac->vif = NULL;
  158. mac->link_state = MAC80211_NOLINK;
  159. memset(mac->bssid, 0, 6);
  160. mac->opmode = NL80211_IFTYPE_UNSPECIFIED;
  161. rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
  162. mutex_unlock(&rtlpriv->locks.conf_mutex);
  163. }
  164. static int rtl_op_config(struct ieee80211_hw *hw, u32 changed)
  165. {
  166. struct rtl_priv *rtlpriv = rtl_priv(hw);
  167. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  168. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  169. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  170. struct ieee80211_conf *conf = &hw->conf;
  171. mutex_lock(&rtlpriv->locks.conf_mutex);
  172. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) { /*BIT(2)*/
  173. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  174. ("IEEE80211_CONF_CHANGE_LISTEN_INTERVAL\n"));
  175. }
  176. /*For IPS */
  177. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  178. if (hw->conf.flags & IEEE80211_CONF_IDLE)
  179. rtl_ips_nic_off(hw);
  180. else
  181. rtl_ips_nic_on(hw);
  182. } else {
  183. /*
  184. *although rfoff may not cause by ips, but we will
  185. *check the reason in set_rf_power_state function
  186. */
  187. if (unlikely(ppsc->rfpwr_state == ERFOFF))
  188. rtl_ips_nic_on(hw);
  189. }
  190. /*For LPS */
  191. if (changed & IEEE80211_CONF_CHANGE_PS) {
  192. if (conf->flags & IEEE80211_CONF_PS)
  193. rtl_lps_enter(hw);
  194. else
  195. rtl_lps_leave(hw);
  196. }
  197. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
  198. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  199. ("IEEE80211_CONF_CHANGE_RETRY_LIMITS %x\n",
  200. hw->conf.long_frame_max_tx_count));
  201. mac->retry_long = hw->conf.long_frame_max_tx_count;
  202. mac->retry_short = hw->conf.long_frame_max_tx_count;
  203. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
  204. (u8 *) (&hw->conf.
  205. long_frame_max_tx_count));
  206. }
  207. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  208. struct ieee80211_channel *channel = hw->conf.channel;
  209. u8 wide_chan = (u8) channel->hw_value;
  210. /*
  211. *because we should back channel to
  212. *current_network.chan in in scanning,
  213. *So if set_chan == current_network.chan
  214. *we should set it.
  215. *because mac80211 tell us wrong bw40
  216. *info for cisco1253 bw20, so we modify
  217. *it here based on UPPER & LOWER
  218. */
  219. switch (hw->conf.channel_type) {
  220. case NL80211_CHAN_HT20:
  221. case NL80211_CHAN_NO_HT:
  222. /* SC */
  223. mac->cur_40_prime_sc =
  224. PRIME_CHNL_OFFSET_DONT_CARE;
  225. rtlphy->current_chan_bw = HT_CHANNEL_WIDTH_20;
  226. mac->bw_40 = false;
  227. break;
  228. case NL80211_CHAN_HT40MINUS:
  229. /* SC */
  230. mac->cur_40_prime_sc = PRIME_CHNL_OFFSET_UPPER;
  231. rtlphy->current_chan_bw =
  232. HT_CHANNEL_WIDTH_20_40;
  233. mac->bw_40 = true;
  234. /*wide channel */
  235. wide_chan -= 2;
  236. break;
  237. case NL80211_CHAN_HT40PLUS:
  238. /* SC */
  239. mac->cur_40_prime_sc = PRIME_CHNL_OFFSET_LOWER;
  240. rtlphy->current_chan_bw =
  241. HT_CHANNEL_WIDTH_20_40;
  242. mac->bw_40 = true;
  243. /*wide channel */
  244. wide_chan += 2;
  245. break;
  246. default:
  247. mac->bw_40 = false;
  248. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  249. ("switch case not processed\n"));
  250. break;
  251. }
  252. if (wide_chan <= 0)
  253. wide_chan = 1;
  254. rtlphy->current_channel = wide_chan;
  255. rtlpriv->cfg->ops->set_channel_access(hw);
  256. rtlpriv->cfg->ops->switch_channel(hw);
  257. rtlpriv->cfg->ops->set_bw_mode(hw,
  258. hw->conf.channel_type);
  259. }
  260. mutex_unlock(&rtlpriv->locks.conf_mutex);
  261. return 0;
  262. }
  263. static void rtl_op_configure_filter(struct ieee80211_hw *hw,
  264. unsigned int changed_flags,
  265. unsigned int *new_flags, u64 multicast)
  266. {
  267. struct rtl_priv *rtlpriv = rtl_priv(hw);
  268. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  269. *new_flags &= RTL_SUPPORTED_FILTERS;
  270. if (!changed_flags)
  271. return;
  272. /*TODO: we disable broadcase now, so enable here */
  273. if (changed_flags & FIF_ALLMULTI) {
  274. if (*new_flags & FIF_ALLMULTI) {
  275. mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AM] |
  276. rtlpriv->cfg->maps[MAC_RCR_AB];
  277. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  278. ("Enable receive multicast frame.\n"));
  279. } else {
  280. mac->rx_conf &= ~(rtlpriv->cfg->maps[MAC_RCR_AM] |
  281. rtlpriv->cfg->maps[MAC_RCR_AB]);
  282. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  283. ("Disable receive multicast frame.\n"));
  284. }
  285. }
  286. if (changed_flags & FIF_FCSFAIL) {
  287. if (*new_flags & FIF_FCSFAIL) {
  288. mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACRC32];
  289. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  290. ("Enable receive FCS error frame.\n"));
  291. } else {
  292. mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACRC32];
  293. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  294. ("Disable receive FCS error frame.\n"));
  295. }
  296. }
  297. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  298. /*
  299. *TODO: BIT(5) is probe response BIT(8) is beacon
  300. *TODO: Use define for BIT(5) and BIT(8)
  301. */
  302. if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
  303. mac->rx_mgt_filter |= (BIT(5) | BIT(8));
  304. else
  305. mac->rx_mgt_filter &= ~(BIT(5) | BIT(8));
  306. }
  307. if (changed_flags & FIF_CONTROL) {
  308. if (*new_flags & FIF_CONTROL) {
  309. mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACF];
  310. mac->rx_ctrl_filter |= RTL_SUPPORTED_CTRL_FILTER;
  311. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  312. ("Enable receive control frame.\n"));
  313. } else {
  314. mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACF];
  315. mac->rx_ctrl_filter &= ~RTL_SUPPORTED_CTRL_FILTER;
  316. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  317. ("Disable receive control frame.\n"));
  318. }
  319. }
  320. if (changed_flags & FIF_OTHER_BSS) {
  321. if (*new_flags & FIF_OTHER_BSS) {
  322. mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AAP];
  323. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  324. ("Enable receive other BSS's frame.\n"));
  325. } else {
  326. mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_AAP];
  327. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  328. ("Disable receive other BSS's frame.\n"));
  329. }
  330. }
  331. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
  332. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_MGT_FILTER,
  333. (u8 *) (&mac->rx_mgt_filter));
  334. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CTRL_FILTER,
  335. (u8 *) (&mac->rx_ctrl_filter));
  336. }
  337. static int _rtl_get_hal_qnum(u16 queue)
  338. {
  339. int qnum;
  340. switch (queue) {
  341. case 0:
  342. qnum = AC3_VO;
  343. break;
  344. case 1:
  345. qnum = AC2_VI;
  346. break;
  347. case 2:
  348. qnum = AC0_BE;
  349. break;
  350. case 3:
  351. qnum = AC1_BK;
  352. break;
  353. default:
  354. qnum = AC0_BE;
  355. break;
  356. }
  357. return qnum;
  358. }
  359. /*
  360. *for mac80211 VO=0, VI=1, BE=2, BK=3
  361. *for rtl819x BE=0, BK=1, VI=2, VO=3
  362. */
  363. static int rtl_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
  364. const struct ieee80211_tx_queue_params *param)
  365. {
  366. struct rtl_priv *rtlpriv = rtl_priv(hw);
  367. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  368. int aci;
  369. if (queue >= AC_MAX) {
  370. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  371. ("queue number %d is incorrect!\n", queue));
  372. return -EINVAL;
  373. }
  374. aci = _rtl_get_hal_qnum(queue);
  375. mac->ac[aci].aifs = param->aifs;
  376. mac->ac[aci].cw_min = cpu_to_le16(param->cw_min);
  377. mac->ac[aci].cw_max = cpu_to_le16(param->cw_max);
  378. mac->ac[aci].tx_op = cpu_to_le16(param->txop);
  379. memcpy(&mac->edca_param[aci], param, sizeof(*param));
  380. rtlpriv->cfg->ops->set_qos(hw, aci);
  381. return 0;
  382. }
  383. static void rtl_op_bss_info_changed(struct ieee80211_hw *hw,
  384. struct ieee80211_vif *vif,
  385. struct ieee80211_bss_conf *bss_conf, u32 changed)
  386. {
  387. struct rtl_priv *rtlpriv = rtl_priv(hw);
  388. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  389. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  390. mutex_lock(&rtlpriv->locks.conf_mutex);
  391. if ((vif->type == NL80211_IFTYPE_ADHOC) ||
  392. (vif->type == NL80211_IFTYPE_AP) ||
  393. (vif->type == NL80211_IFTYPE_MESH_POINT)) {
  394. if ((changed & BSS_CHANGED_BEACON) ||
  395. (changed & BSS_CHANGED_BEACON_ENABLED &&
  396. bss_conf->enable_beacon)) {
  397. if (mac->beacon_enabled == 0) {
  398. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  399. ("BSS_CHANGED_BEACON_ENABLED\n"));
  400. /*start hw beacon interrupt. */
  401. /*rtlpriv->cfg->ops->set_bcn_reg(hw); */
  402. mac->beacon_enabled = 1;
  403. rtlpriv->cfg->ops->update_interrupt_mask(hw,
  404. rtlpriv->cfg->maps
  405. [RTL_IBSS_INT_MASKS],
  406. 0);
  407. }
  408. } else {
  409. if (mac->beacon_enabled == 1) {
  410. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  411. ("ADHOC DISABLE BEACON\n"));
  412. mac->beacon_enabled = 0;
  413. rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
  414. rtlpriv->cfg->maps
  415. [RTL_IBSS_INT_MASKS]);
  416. }
  417. }
  418. if (changed & BSS_CHANGED_BEACON_INT) {
  419. RT_TRACE(rtlpriv, COMP_BEACON, DBG_TRACE,
  420. ("BSS_CHANGED_BEACON_INT\n"));
  421. mac->beacon_interval = bss_conf->beacon_int;
  422. rtlpriv->cfg->ops->set_bcn_intv(hw);
  423. }
  424. }
  425. /*TODO: reference to enum ieee80211_bss_change */
  426. if (changed & BSS_CHANGED_ASSOC) {
  427. if (bss_conf->assoc) {
  428. mac->link_state = MAC80211_LINKED;
  429. mac->cnt_after_linked = 0;
  430. mac->assoc_id = bss_conf->aid;
  431. memcpy(mac->bssid, bss_conf->bssid, 6);
  432. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  433. ("BSS_CHANGED_ASSOC\n"));
  434. } else {
  435. if (mac->link_state == MAC80211_LINKED)
  436. rtl_lps_leave(hw);
  437. mac->link_state = MAC80211_NOLINK;
  438. memset(mac->bssid, 0, 6);
  439. /* reset sec info */
  440. rtl_cam_reset_sec_info(hw);
  441. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  442. ("BSS_CHANGED_UN_ASSOC\n"));
  443. }
  444. }
  445. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  446. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  447. ("BSS_CHANGED_ERP_CTS_PROT\n"));
  448. mac->use_cts_protect = bss_conf->use_cts_prot;
  449. }
  450. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  451. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  452. ("BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
  453. bss_conf->use_short_preamble));
  454. mac->short_preamble = bss_conf->use_short_preamble;
  455. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
  456. (u8 *) (&mac->short_preamble));
  457. }
  458. if (changed & BSS_CHANGED_ERP_SLOT) {
  459. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  460. ("BSS_CHANGED_ERP_SLOT\n"));
  461. if (bss_conf->use_short_slot)
  462. mac->slot_time = RTL_SLOT_TIME_9;
  463. else
  464. mac->slot_time = RTL_SLOT_TIME_20;
  465. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
  466. (u8 *) (&mac->slot_time));
  467. }
  468. if (changed & BSS_CHANGED_HT) {
  469. struct ieee80211_sta *sta = NULL;
  470. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  471. ("BSS_CHANGED_HT\n"));
  472. sta = ieee80211_find_sta(mac->vif, mac->bssid);
  473. if (sta) {
  474. if (sta->ht_cap.ampdu_density >
  475. mac->current_ampdu_density)
  476. mac->current_ampdu_density =
  477. sta->ht_cap.ampdu_density;
  478. if (sta->ht_cap.ampdu_factor <
  479. mac->current_ampdu_factor)
  480. mac->current_ampdu_factor =
  481. sta->ht_cap.ampdu_factor;
  482. }
  483. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
  484. (u8 *) (&mac->max_mss_density));
  485. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
  486. &mac->current_ampdu_factor);
  487. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
  488. &mac->current_ampdu_density);
  489. }
  490. if (changed & BSS_CHANGED_BSSID) {
  491. struct ieee80211_sta *sta = NULL;
  492. u32 basic_rates;
  493. u8 i;
  494. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
  495. (u8 *) bss_conf->bssid);
  496. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  497. (MAC_FMT "\n", MAC_ARG(bss_conf->bssid)));
  498. memcpy(mac->bssid, bss_conf->bssid, 6);
  499. if (is_valid_ether_addr(bss_conf->bssid)) {
  500. switch (vif->type) {
  501. case NL80211_IFTYPE_UNSPECIFIED:
  502. break;
  503. case NL80211_IFTYPE_ADHOC:
  504. break;
  505. case NL80211_IFTYPE_STATION:
  506. break;
  507. case NL80211_IFTYPE_AP:
  508. break;
  509. default:
  510. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  511. ("switch case not process\n"));
  512. break;
  513. }
  514. rtlpriv->cfg->ops->set_network_type(hw, vif->type);
  515. } else
  516. rtlpriv->cfg->ops->set_network_type(hw,
  517. NL80211_IFTYPE_UNSPECIFIED);
  518. memset(mac->mcs, 0, 16);
  519. mac->ht_enable = false;
  520. mac->sgi_40 = false;
  521. mac->sgi_20 = false;
  522. if (!bss_conf->use_short_slot)
  523. mac->mode = WIRELESS_MODE_B;
  524. else
  525. mac->mode = WIRELESS_MODE_G;
  526. sta = ieee80211_find_sta(mac->vif, mac->bssid);
  527. if (sta) {
  528. if (sta->ht_cap.ht_supported) {
  529. mac->mode = WIRELESS_MODE_N_24G;
  530. mac->ht_enable = true;
  531. }
  532. if (mac->ht_enable) {
  533. u16 ht_cap = sta->ht_cap.cap;
  534. memcpy(mac->mcs, (u8 *) (&sta->ht_cap.mcs), 16);
  535. for (i = 0; i < 16; i++)
  536. RT_TRACE(rtlpriv, COMP_MAC80211,
  537. DBG_LOUD, ("%x ",
  538. mac->mcs[i]));
  539. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  540. ("\n"));
  541. if (ht_cap & IEEE80211_HT_CAP_SGI_40)
  542. mac->sgi_40 = true;
  543. if (ht_cap & IEEE80211_HT_CAP_SGI_20)
  544. mac->sgi_20 = true;
  545. /*
  546. * for cisco 1252 bw20 it's wrong
  547. * if (ht_cap &
  548. * IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
  549. * mac->bw_40 = true;
  550. * }
  551. */
  552. }
  553. }
  554. /*mac80211 just give us CCK rates any time
  555. *So we add G rate in basic rates when
  556. not in B mode*/
  557. if (changed & BSS_CHANGED_BASIC_RATES) {
  558. if (mac->mode == WIRELESS_MODE_B)
  559. basic_rates = bss_conf->basic_rates | 0x00f;
  560. else
  561. basic_rates = bss_conf->basic_rates | 0xff0;
  562. if (!vif)
  563. goto out;
  564. mac->basic_rates = basic_rates;
  565. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
  566. (u8 *) (&basic_rates));
  567. if (rtlpriv->dm.useramask)
  568. rtlpriv->cfg->ops->update_rate_mask(hw, 0);
  569. else
  570. rtlpriv->cfg->ops->update_rate_table(hw);
  571. }
  572. }
  573. /*
  574. * For FW LPS:
  575. * To tell firmware we have connected
  576. * to an AP. For 92SE/CE power save v2.
  577. */
  578. if (changed & BSS_CHANGED_ASSOC) {
  579. if (bss_conf->assoc) {
  580. if (ppsc->fwctrl_lps) {
  581. u8 mstatus = RT_MEDIA_CONNECT;
  582. rtlpriv->cfg->ops->set_hw_reg(hw,
  583. HW_VAR_H2C_FW_JOINBSSRPT,
  584. (u8 *) (&mstatus));
  585. ppsc->report_linked = true;
  586. }
  587. } else {
  588. if (ppsc->fwctrl_lps) {
  589. u8 mstatus = RT_MEDIA_DISCONNECT;
  590. rtlpriv->cfg->ops->set_hw_reg(hw,
  591. HW_VAR_H2C_FW_JOINBSSRPT,
  592. (u8 *)(&mstatus));
  593. ppsc->report_linked = false;
  594. }
  595. }
  596. }
  597. out:
  598. mutex_unlock(&rtlpriv->locks.conf_mutex);
  599. }
  600. static u64 rtl_op_get_tsf(struct ieee80211_hw *hw)
  601. {
  602. struct rtl_priv *rtlpriv = rtl_priv(hw);
  603. u64 tsf;
  604. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *) (&tsf));
  605. return tsf;
  606. }
  607. static void rtl_op_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  608. {
  609. struct rtl_priv *rtlpriv = rtl_priv(hw);
  610. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  611. u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;;
  612. mac->tsf = tsf;
  613. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *) (&bibss));
  614. }
  615. static void rtl_op_reset_tsf(struct ieee80211_hw *hw)
  616. {
  617. struct rtl_priv *rtlpriv = rtl_priv(hw);
  618. u8 tmp = 0;
  619. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, (u8 *) (&tmp));
  620. }
  621. static void rtl_op_sta_notify(struct ieee80211_hw *hw,
  622. struct ieee80211_vif *vif,
  623. enum sta_notify_cmd cmd,
  624. struct ieee80211_sta *sta)
  625. {
  626. switch (cmd) {
  627. case STA_NOTIFY_SLEEP:
  628. break;
  629. case STA_NOTIFY_AWAKE:
  630. break;
  631. default:
  632. break;
  633. }
  634. }
  635. static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
  636. struct ieee80211_vif *vif,
  637. enum ieee80211_ampdu_mlme_action action,
  638. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  639. u8 buf_size)
  640. {
  641. struct rtl_priv *rtlpriv = rtl_priv(hw);
  642. switch (action) {
  643. case IEEE80211_AMPDU_TX_START:
  644. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  645. ("IEEE80211_AMPDU_TX_START: TID:%d\n", tid));
  646. return rtl_tx_agg_start(hw, sta->addr, tid, ssn);
  647. break;
  648. case IEEE80211_AMPDU_TX_STOP:
  649. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  650. ("IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid));
  651. return rtl_tx_agg_stop(hw, sta->addr, tid);
  652. break;
  653. case IEEE80211_AMPDU_TX_OPERATIONAL:
  654. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  655. ("IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid));
  656. break;
  657. case IEEE80211_AMPDU_RX_START:
  658. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  659. ("IEEE80211_AMPDU_RX_START:TID:%d\n", tid));
  660. break;
  661. case IEEE80211_AMPDU_RX_STOP:
  662. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  663. ("IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid));
  664. break;
  665. default:
  666. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  667. ("IEEE80211_AMPDU_ERR!!!!:\n"));
  668. return -EOPNOTSUPP;
  669. }
  670. return 0;
  671. }
  672. static void rtl_op_sw_scan_start(struct ieee80211_hw *hw)
  673. {
  674. struct rtl_priv *rtlpriv = rtl_priv(hw);
  675. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  676. mac->act_scanning = true;
  677. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("\n"));
  678. if (mac->link_state == MAC80211_LINKED) {
  679. rtl_lps_leave(hw);
  680. mac->link_state = MAC80211_LINKED_SCANNING;
  681. } else
  682. rtl_ips_nic_on(hw);
  683. rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
  684. rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP);
  685. }
  686. static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw)
  687. {
  688. struct rtl_priv *rtlpriv = rtl_priv(hw);
  689. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  690. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, ("\n"));
  691. rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
  692. mac->act_scanning = false;
  693. if (mac->link_state == MAC80211_LINKED_SCANNING) {
  694. mac->link_state = MAC80211_LINKED;
  695. /* fix fwlps issue */
  696. rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
  697. if (rtlpriv->dm.useramask)
  698. rtlpriv->cfg->ops->update_rate_mask(hw, 0);
  699. else
  700. rtlpriv->cfg->ops->update_rate_table(hw);
  701. }
  702. }
  703. static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  704. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  705. struct ieee80211_key_conf *key)
  706. {
  707. struct rtl_priv *rtlpriv = rtl_priv(hw);
  708. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  709. u8 key_type = NO_ENCRYPTION;
  710. u8 key_idx;
  711. bool group_key = false;
  712. bool wep_only = false;
  713. int err = 0;
  714. u8 mac_addr[ETH_ALEN];
  715. u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  716. u8 zero_addr[ETH_ALEN] = { 0 };
  717. if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
  718. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  719. ("not open hw encryption\n"));
  720. return -ENOSPC; /*User disabled HW-crypto */
  721. }
  722. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  723. ("%s hardware based encryption for keyidx: %d, mac: %pM\n",
  724. cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
  725. sta ? sta->addr : bcast_addr));
  726. rtlpriv->sec.being_setkey = true;
  727. rtl_ips_nic_on(hw);
  728. mutex_lock(&rtlpriv->locks.conf_mutex);
  729. /* <1> get encryption alg */
  730. switch (key->cipher) {
  731. case WLAN_CIPHER_SUITE_WEP40:
  732. key_type = WEP40_ENCRYPTION;
  733. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, ("alg:WEP40\n"));
  734. rtlpriv->sec.use_defaultkey = true;
  735. break;
  736. case WLAN_CIPHER_SUITE_WEP104:
  737. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  738. ("alg:WEP104\n"));
  739. key_type = WEP104_ENCRYPTION;
  740. rtlpriv->sec.use_defaultkey = true;
  741. break;
  742. case WLAN_CIPHER_SUITE_TKIP:
  743. key_type = TKIP_ENCRYPTION;
  744. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, ("alg:TKIP\n"));
  745. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  746. rtlpriv->sec.use_defaultkey = true;
  747. break;
  748. case WLAN_CIPHER_SUITE_CCMP:
  749. key_type = AESCCMP_ENCRYPTION;
  750. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, ("alg:CCMP\n"));
  751. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  752. rtlpriv->sec.use_defaultkey = true;
  753. break;
  754. default:
  755. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  756. ("alg_err:%x!!!!:\n", key->cipher));
  757. goto out_unlock;
  758. }
  759. /* <2> get key_idx */
  760. key_idx = (u8) (key->keyidx);
  761. if (key_idx > 3)
  762. goto out_unlock;
  763. /* <3> if pairwise key enable_hw_sec */
  764. group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  765. if ((!group_key) || (mac->opmode == NL80211_IFTYPE_ADHOC) ||
  766. rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
  767. if (rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION &&
  768. (key_type == WEP40_ENCRYPTION ||
  769. key_type == WEP104_ENCRYPTION))
  770. wep_only = true;
  771. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  772. rtlpriv->cfg->ops->enable_hw_sec(hw);
  773. }
  774. /* <4> set key based on cmd */
  775. switch (cmd) {
  776. case SET_KEY:
  777. if (wep_only) {
  778. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  779. ("set WEP(group/pairwise) key\n"));
  780. /* Pairwise key with an assigned MAC address. */
  781. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  782. rtlpriv->sec.group_enc_algorithm = key_type;
  783. /*set local buf about wep key. */
  784. memcpy(rtlpriv->sec.key_buf[key_idx],
  785. key->key, key->keylen);
  786. rtlpriv->sec.key_len[key_idx] = key->keylen;
  787. memcpy(mac_addr, zero_addr, ETH_ALEN);
  788. } else if (group_key) { /* group key */
  789. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  790. ("set group key\n"));
  791. /* group key */
  792. rtlpriv->sec.group_enc_algorithm = key_type;
  793. /*set local buf about group key. */
  794. memcpy(rtlpriv->sec.key_buf[key_idx],
  795. key->key, key->keylen);
  796. rtlpriv->sec.key_len[key_idx] = key->keylen;
  797. memcpy(mac_addr, bcast_addr, ETH_ALEN);
  798. } else { /* pairwise key */
  799. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  800. ("set pairwise key\n"));
  801. if (!sta) {
  802. RT_ASSERT(false, ("pairwise key withnot"
  803. "mac_addr\n"));
  804. err = -EOPNOTSUPP;
  805. goto out_unlock;
  806. }
  807. /* Pairwise key with an assigned MAC address. */
  808. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  809. /*set local buf about pairwise key. */
  810. memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
  811. key->key, key->keylen);
  812. rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
  813. rtlpriv->sec.pairwise_key =
  814. rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
  815. memcpy(mac_addr, sta->addr, ETH_ALEN);
  816. }
  817. rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
  818. group_key, key_type, wep_only,
  819. false);
  820. /* <5> tell mac80211 do something: */
  821. /*must use sw generate IV, or can not work !!!!. */
  822. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  823. key->hw_key_idx = key_idx;
  824. if (key_type == TKIP_ENCRYPTION)
  825. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  826. break;
  827. case DISABLE_KEY:
  828. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  829. ("disable key delete one entry\n"));
  830. /*set local buf about wep key. */
  831. memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
  832. rtlpriv->sec.key_len[key_idx] = 0;
  833. memcpy(mac_addr, zero_addr, ETH_ALEN);
  834. /*
  835. *mac80211 will delete entrys one by one,
  836. *so don't use rtl_cam_reset_all_entry
  837. *or clear all entry here.
  838. */
  839. rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
  840. break;
  841. default:
  842. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  843. ("cmd_err:%x!!!!:\n", cmd));
  844. }
  845. out_unlock:
  846. mutex_unlock(&rtlpriv->locks.conf_mutex);
  847. rtlpriv->sec.being_setkey = false;
  848. return err;
  849. }
  850. static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
  851. {
  852. struct rtl_priv *rtlpriv = rtl_priv(hw);
  853. bool radio_state;
  854. bool blocked;
  855. u8 valid = 0;
  856. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  857. return;
  858. mutex_lock(&rtlpriv->locks.conf_mutex);
  859. /*if Radio On return true here */
  860. radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
  861. if (valid) {
  862. if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
  863. rtlpriv->rfkill.rfkill_state = radio_state;
  864. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  865. (KERN_INFO "wireless radio switch turned %s\n",
  866. radio_state ? "on" : "off"));
  867. blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
  868. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  869. }
  870. }
  871. mutex_unlock(&rtlpriv->locks.conf_mutex);
  872. }
  873. const struct ieee80211_ops rtl_ops = {
  874. .start = rtl_op_start,
  875. .stop = rtl_op_stop,
  876. .tx = rtl_op_tx,
  877. .add_interface = rtl_op_add_interface,
  878. .remove_interface = rtl_op_remove_interface,
  879. .config = rtl_op_config,
  880. .configure_filter = rtl_op_configure_filter,
  881. .set_key = rtl_op_set_key,
  882. .conf_tx = rtl_op_conf_tx,
  883. .bss_info_changed = rtl_op_bss_info_changed,
  884. .get_tsf = rtl_op_get_tsf,
  885. .set_tsf = rtl_op_set_tsf,
  886. .reset_tsf = rtl_op_reset_tsf,
  887. .sta_notify = rtl_op_sta_notify,
  888. .ampdu_action = rtl_op_ampdu_action,
  889. .sw_scan_start = rtl_op_sw_scan_start,
  890. .sw_scan_complete = rtl_op_sw_scan_complete,
  891. .rfkill_poll = rtl_op_rfkill_poll,
  892. };