core.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2012 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 "wifi.h"
  30. #include "core.h"
  31. #include "cam.h"
  32. #include "base.h"
  33. #include "pci.h"
  34. #include "ps.h"
  35. #include <linux/export.h>
  36. void rtl_fw_cb(const struct firmware *firmware, void *context)
  37. {
  38. struct ieee80211_hw *hw = context;
  39. struct rtl_priv *rtlpriv = rtl_priv(hw);
  40. int err;
  41. RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
  42. "Firmware callback routine entered!\n");
  43. complete(&rtlpriv->firmware_loading_complete);
  44. if (!firmware) {
  45. pr_err("Firmware %s not available\n", rtlpriv->cfg->fw_name);
  46. rtlpriv->max_fw_size = 0;
  47. return;
  48. }
  49. if (firmware->size > rtlpriv->max_fw_size) {
  50. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  51. "Firmware is too big!\n");
  52. release_firmware(firmware);
  53. return;
  54. }
  55. memcpy(rtlpriv->rtlhal.pfirmware, firmware->data, firmware->size);
  56. rtlpriv->rtlhal.fwsize = firmware->size;
  57. release_firmware(firmware);
  58. err = ieee80211_register_hw(hw);
  59. if (err) {
  60. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  61. "Can't register mac80211 hw\n");
  62. return;
  63. } else {
  64. rtlpriv->mac80211.mac80211_registered = 1;
  65. }
  66. set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
  67. /*init rfkill */
  68. rtl_init_rfkill(hw);
  69. }
  70. EXPORT_SYMBOL(rtl_fw_cb);
  71. /*mutex for start & stop is must here. */
  72. static int rtl_op_start(struct ieee80211_hw *hw)
  73. {
  74. int err;
  75. struct rtl_priv *rtlpriv = rtl_priv(hw);
  76. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  77. if (!is_hal_stop(rtlhal))
  78. return 0;
  79. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  80. return 0;
  81. mutex_lock(&rtlpriv->locks.conf_mutex);
  82. err = rtlpriv->intf_ops->adapter_start(hw);
  83. if (!err)
  84. rtl_watch_dog_timer_callback((unsigned long)hw);
  85. mutex_unlock(&rtlpriv->locks.conf_mutex);
  86. return err;
  87. }
  88. static void rtl_op_stop(struct ieee80211_hw *hw)
  89. {
  90. struct rtl_priv *rtlpriv = rtl_priv(hw);
  91. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  92. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  93. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  94. if (is_hal_stop(rtlhal))
  95. return;
  96. if (unlikely(ppsc->rfpwr_state == ERFOFF)) {
  97. rtl_ips_nic_on(hw);
  98. mdelay(1);
  99. }
  100. mutex_lock(&rtlpriv->locks.conf_mutex);
  101. mac->link_state = MAC80211_NOLINK;
  102. memset(mac->bssid, 0, 6);
  103. mac->vendor = PEER_UNKNOWN;
  104. /*reset sec info */
  105. rtl_cam_reset_sec_info(hw);
  106. rtl_deinit_deferred_work(hw);
  107. rtlpriv->intf_ops->adapter_stop(hw);
  108. mutex_unlock(&rtlpriv->locks.conf_mutex);
  109. }
  110. static void rtl_op_tx(struct ieee80211_hw *hw,
  111. struct ieee80211_tx_control *control,
  112. struct sk_buff *skb)
  113. {
  114. struct rtl_priv *rtlpriv = rtl_priv(hw);
  115. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  116. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  117. struct rtl_tcb_desc tcb_desc;
  118. memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
  119. if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
  120. goto err_free;
  121. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  122. goto err_free;
  123. if (!rtlpriv->intf_ops->waitq_insert(hw, control->sta, skb))
  124. rtlpriv->intf_ops->adapter_tx(hw, control->sta, skb, &tcb_desc);
  125. return;
  126. err_free:
  127. dev_kfree_skb_any(skb);
  128. }
  129. static int rtl_op_add_interface(struct ieee80211_hw *hw,
  130. struct ieee80211_vif *vif)
  131. {
  132. struct rtl_priv *rtlpriv = rtl_priv(hw);
  133. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  134. int err = 0;
  135. vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
  136. if (mac->vif) {
  137. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  138. "vif has been set!! mac->vif = 0x%p\n", mac->vif);
  139. return -EOPNOTSUPP;
  140. }
  141. rtl_ips_nic_on(hw);
  142. mutex_lock(&rtlpriv->locks.conf_mutex);
  143. switch (vif->type) {
  144. case NL80211_IFTYPE_STATION:
  145. if (mac->beacon_enabled == 1) {
  146. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  147. "NL80211_IFTYPE_STATION\n");
  148. mac->beacon_enabled = 0;
  149. rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
  150. rtlpriv->cfg->maps
  151. [RTL_IBSS_INT_MASKS]);
  152. }
  153. break;
  154. case NL80211_IFTYPE_ADHOC:
  155. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  156. "NL80211_IFTYPE_ADHOC\n");
  157. mac->link_state = MAC80211_LINKED;
  158. rtlpriv->cfg->ops->set_bcn_reg(hw);
  159. if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
  160. mac->basic_rates = 0xfff;
  161. else
  162. mac->basic_rates = 0xff0;
  163. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
  164. (u8 *) (&mac->basic_rates));
  165. break;
  166. case NL80211_IFTYPE_AP:
  167. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  168. "NL80211_IFTYPE_AP\n");
  169. mac->link_state = MAC80211_LINKED;
  170. rtlpriv->cfg->ops->set_bcn_reg(hw);
  171. if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
  172. mac->basic_rates = 0xfff;
  173. else
  174. mac->basic_rates = 0xff0;
  175. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
  176. (u8 *) (&mac->basic_rates));
  177. break;
  178. default:
  179. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  180. "operation mode %d is not supported!\n", vif->type);
  181. err = -EOPNOTSUPP;
  182. goto out;
  183. }
  184. mac->vif = vif;
  185. mac->opmode = vif->type;
  186. rtlpriv->cfg->ops->set_network_type(hw, vif->type);
  187. memcpy(mac->mac_addr, vif->addr, ETH_ALEN);
  188. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
  189. out:
  190. mutex_unlock(&rtlpriv->locks.conf_mutex);
  191. return err;
  192. }
  193. static void rtl_op_remove_interface(struct ieee80211_hw *hw,
  194. struct ieee80211_vif *vif)
  195. {
  196. struct rtl_priv *rtlpriv = rtl_priv(hw);
  197. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  198. mutex_lock(&rtlpriv->locks.conf_mutex);
  199. /* Free beacon resources */
  200. if ((mac->opmode == NL80211_IFTYPE_AP) ||
  201. (mac->opmode == NL80211_IFTYPE_ADHOC) ||
  202. (mac->opmode == NL80211_IFTYPE_MESH_POINT)) {
  203. if (mac->beacon_enabled == 1) {
  204. mac->beacon_enabled = 0;
  205. rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
  206. rtlpriv->cfg->maps
  207. [RTL_IBSS_INT_MASKS]);
  208. }
  209. }
  210. /*
  211. *Note: We assume NL80211_IFTYPE_UNSPECIFIED as
  212. *NO LINK for our hardware.
  213. */
  214. mac->vif = NULL;
  215. mac->link_state = MAC80211_NOLINK;
  216. memset(mac->bssid, 0, 6);
  217. mac->vendor = PEER_UNKNOWN;
  218. mac->opmode = NL80211_IFTYPE_UNSPECIFIED;
  219. rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
  220. mutex_unlock(&rtlpriv->locks.conf_mutex);
  221. }
  222. static int rtl_op_config(struct ieee80211_hw *hw, u32 changed)
  223. {
  224. struct rtl_priv *rtlpriv = rtl_priv(hw);
  225. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  226. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  227. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  228. struct ieee80211_conf *conf = &hw->conf;
  229. mutex_lock(&rtlpriv->locks.conf_mutex);
  230. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) { /*BIT(2)*/
  231. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  232. "IEEE80211_CONF_CHANGE_LISTEN_INTERVAL\n");
  233. }
  234. /*For IPS */
  235. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  236. if (hw->conf.flags & IEEE80211_CONF_IDLE)
  237. rtl_ips_nic_off(hw);
  238. else
  239. rtl_ips_nic_on(hw);
  240. } else {
  241. /*
  242. *although rfoff may not cause by ips, but we will
  243. *check the reason in set_rf_power_state function
  244. */
  245. if (unlikely(ppsc->rfpwr_state == ERFOFF))
  246. rtl_ips_nic_on(hw);
  247. }
  248. /*For LPS */
  249. if (changed & IEEE80211_CONF_CHANGE_PS) {
  250. cancel_delayed_work(&rtlpriv->works.ps_work);
  251. cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
  252. if (conf->flags & IEEE80211_CONF_PS) {
  253. rtlpriv->psc.sw_ps_enabled = true;
  254. /* sleep here is must, or we may recv the beacon and
  255. * cause mac80211 into wrong ps state, this will cause
  256. * power save nullfunc send fail, and further cause
  257. * pkt loss, So sleep must quickly but not immediatly
  258. * because that will cause nullfunc send by mac80211
  259. * fail, and cause pkt loss, we have tested that 5mA
  260. * is worked very well */
  261. if (!rtlpriv->psc.multi_buffered)
  262. queue_delayed_work(rtlpriv->works.rtl_wq,
  263. &rtlpriv->works.ps_work,
  264. MSECS(5));
  265. } else {
  266. rtl_swlps_rf_awake(hw);
  267. rtlpriv->psc.sw_ps_enabled = false;
  268. }
  269. }
  270. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
  271. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  272. "IEEE80211_CONF_CHANGE_RETRY_LIMITS %x\n",
  273. hw->conf.long_frame_max_tx_count);
  274. mac->retry_long = hw->conf.long_frame_max_tx_count;
  275. mac->retry_short = hw->conf.long_frame_max_tx_count;
  276. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
  277. (u8 *) (&hw->conf.
  278. long_frame_max_tx_count));
  279. }
  280. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  281. struct ieee80211_channel *channel = hw->conf.channel;
  282. u8 wide_chan = (u8) channel->hw_value;
  283. /*
  284. *because we should back channel to
  285. *current_network.chan in in scanning,
  286. *So if set_chan == current_network.chan
  287. *we should set it.
  288. *because mac80211 tell us wrong bw40
  289. *info for cisco1253 bw20, so we modify
  290. *it here based on UPPER & LOWER
  291. */
  292. switch (hw->conf.channel_type) {
  293. case NL80211_CHAN_HT20:
  294. case NL80211_CHAN_NO_HT:
  295. /* SC */
  296. mac->cur_40_prime_sc =
  297. PRIME_CHNL_OFFSET_DONT_CARE;
  298. rtlphy->current_chan_bw = HT_CHANNEL_WIDTH_20;
  299. mac->bw_40 = false;
  300. break;
  301. case NL80211_CHAN_HT40MINUS:
  302. /* SC */
  303. mac->cur_40_prime_sc = PRIME_CHNL_OFFSET_UPPER;
  304. rtlphy->current_chan_bw =
  305. HT_CHANNEL_WIDTH_20_40;
  306. mac->bw_40 = true;
  307. /*wide channel */
  308. wide_chan -= 2;
  309. break;
  310. case NL80211_CHAN_HT40PLUS:
  311. /* SC */
  312. mac->cur_40_prime_sc = PRIME_CHNL_OFFSET_LOWER;
  313. rtlphy->current_chan_bw =
  314. HT_CHANNEL_WIDTH_20_40;
  315. mac->bw_40 = true;
  316. /*wide channel */
  317. wide_chan += 2;
  318. break;
  319. default:
  320. mac->bw_40 = false;
  321. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  322. "switch case not processed\n");
  323. break;
  324. }
  325. if (wide_chan <= 0)
  326. wide_chan = 1;
  327. /* In scanning, before we go offchannel we may send a ps=1 null
  328. * to AP, and then we may send a ps = 0 null to AP quickly, but
  329. * first null may have caused AP to put lots of packet to hw tx
  330. * buffer. These packets must be tx'd before we go off channel
  331. * so we must delay more time to let AP flush these packets
  332. * before going offchannel, or dis-association or delete BA will
  333. * happen by AP
  334. */
  335. if (rtlpriv->mac80211.offchan_delay) {
  336. rtlpriv->mac80211.offchan_delay = false;
  337. mdelay(50);
  338. }
  339. rtlphy->current_channel = wide_chan;
  340. rtlpriv->cfg->ops->switch_channel(hw);
  341. rtlpriv->cfg->ops->set_channel_access(hw);
  342. rtlpriv->cfg->ops->set_bw_mode(hw,
  343. hw->conf.channel_type);
  344. }
  345. mutex_unlock(&rtlpriv->locks.conf_mutex);
  346. return 0;
  347. }
  348. static void rtl_op_configure_filter(struct ieee80211_hw *hw,
  349. unsigned int changed_flags,
  350. unsigned int *new_flags, u64 multicast)
  351. {
  352. struct rtl_priv *rtlpriv = rtl_priv(hw);
  353. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  354. *new_flags &= RTL_SUPPORTED_FILTERS;
  355. if (!changed_flags)
  356. return;
  357. /*TODO: we disable broadcase now, so enable here */
  358. if (changed_flags & FIF_ALLMULTI) {
  359. if (*new_flags & FIF_ALLMULTI) {
  360. mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AM] |
  361. rtlpriv->cfg->maps[MAC_RCR_AB];
  362. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  363. "Enable receive multicast frame\n");
  364. } else {
  365. mac->rx_conf &= ~(rtlpriv->cfg->maps[MAC_RCR_AM] |
  366. rtlpriv->cfg->maps[MAC_RCR_AB]);
  367. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  368. "Disable receive multicast frame\n");
  369. }
  370. }
  371. if (changed_flags & FIF_FCSFAIL) {
  372. if (*new_flags & FIF_FCSFAIL) {
  373. mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACRC32];
  374. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  375. "Enable receive FCS error frame\n");
  376. } else {
  377. mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACRC32];
  378. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  379. "Disable receive FCS error frame\n");
  380. }
  381. }
  382. /* if ssid not set to hw don't check bssid
  383. * here just used for linked scanning, & linked
  384. * and nolink check bssid is set in set network_type */
  385. if ((changed_flags & FIF_BCN_PRBRESP_PROMISC) &&
  386. (mac->link_state >= MAC80211_LINKED)) {
  387. if (mac->opmode != NL80211_IFTYPE_AP) {
  388. if (*new_flags & FIF_BCN_PRBRESP_PROMISC) {
  389. rtlpriv->cfg->ops->set_chk_bssid(hw, false);
  390. } else {
  391. rtlpriv->cfg->ops->set_chk_bssid(hw, true);
  392. }
  393. }
  394. }
  395. if (changed_flags & FIF_CONTROL) {
  396. if (*new_flags & FIF_CONTROL) {
  397. mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACF];
  398. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  399. "Enable receive control frame\n");
  400. } else {
  401. mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACF];
  402. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  403. "Disable receive control frame\n");
  404. }
  405. }
  406. if (changed_flags & FIF_OTHER_BSS) {
  407. if (*new_flags & FIF_OTHER_BSS) {
  408. mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AAP];
  409. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  410. "Enable receive other BSS's frame\n");
  411. } else {
  412. mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_AAP];
  413. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  414. "Disable receive other BSS's frame\n");
  415. }
  416. }
  417. }
  418. static int rtl_op_sta_add(struct ieee80211_hw *hw,
  419. struct ieee80211_vif *vif,
  420. struct ieee80211_sta *sta)
  421. {
  422. struct rtl_priv *rtlpriv = rtl_priv(hw);
  423. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  424. struct rtl_sta_info *sta_entry;
  425. if (sta) {
  426. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  427. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  428. sta_entry->wireless_mode = WIRELESS_MODE_G;
  429. if (sta->supp_rates[0] <= 0xf)
  430. sta_entry->wireless_mode = WIRELESS_MODE_B;
  431. if (sta->ht_cap.ht_supported)
  432. sta_entry->wireless_mode = WIRELESS_MODE_N_24G;
  433. } else if (rtlhal->current_bandtype == BAND_ON_5G) {
  434. sta_entry->wireless_mode = WIRELESS_MODE_A;
  435. if (sta->ht_cap.ht_supported)
  436. sta_entry->wireless_mode = WIRELESS_MODE_N_24G;
  437. }
  438. /* I found some times mac80211 give wrong supp_rates for adhoc*/
  439. if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_ADHOC)
  440. sta_entry->wireless_mode = WIRELESS_MODE_G;
  441. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  442. "Add sta addr is %pM\n", sta->addr);
  443. rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
  444. }
  445. return 0;
  446. }
  447. static int rtl_op_sta_remove(struct ieee80211_hw *hw,
  448. struct ieee80211_vif *vif,
  449. struct ieee80211_sta *sta)
  450. {
  451. struct rtl_priv *rtlpriv = rtl_priv(hw);
  452. struct rtl_sta_info *sta_entry;
  453. if (sta) {
  454. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  455. "Remove sta addr is %pM\n", sta->addr);
  456. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  457. sta_entry->wireless_mode = 0;
  458. sta_entry->ratr_index = 0;
  459. }
  460. return 0;
  461. }
  462. static int _rtl_get_hal_qnum(u16 queue)
  463. {
  464. int qnum;
  465. switch (queue) {
  466. case 0:
  467. qnum = AC3_VO;
  468. break;
  469. case 1:
  470. qnum = AC2_VI;
  471. break;
  472. case 2:
  473. qnum = AC0_BE;
  474. break;
  475. case 3:
  476. qnum = AC1_BK;
  477. break;
  478. default:
  479. qnum = AC0_BE;
  480. break;
  481. }
  482. return qnum;
  483. }
  484. /*
  485. *for mac80211 VO=0, VI=1, BE=2, BK=3
  486. *for rtl819x BE=0, BK=1, VI=2, VO=3
  487. */
  488. static int rtl_op_conf_tx(struct ieee80211_hw *hw,
  489. struct ieee80211_vif *vif, u16 queue,
  490. const struct ieee80211_tx_queue_params *param)
  491. {
  492. struct rtl_priv *rtlpriv = rtl_priv(hw);
  493. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  494. int aci;
  495. if (queue >= AC_MAX) {
  496. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  497. "queue number %d is incorrect!\n", queue);
  498. return -EINVAL;
  499. }
  500. aci = _rtl_get_hal_qnum(queue);
  501. mac->ac[aci].aifs = param->aifs;
  502. mac->ac[aci].cw_min = cpu_to_le16(param->cw_min);
  503. mac->ac[aci].cw_max = cpu_to_le16(param->cw_max);
  504. mac->ac[aci].tx_op = cpu_to_le16(param->txop);
  505. memcpy(&mac->edca_param[aci], param, sizeof(*param));
  506. rtlpriv->cfg->ops->set_qos(hw, aci);
  507. return 0;
  508. }
  509. static void rtl_op_bss_info_changed(struct ieee80211_hw *hw,
  510. struct ieee80211_vif *vif,
  511. struct ieee80211_bss_conf *bss_conf, u32 changed)
  512. {
  513. struct rtl_priv *rtlpriv = rtl_priv(hw);
  514. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  515. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  516. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  517. struct ieee80211_sta *sta = NULL;
  518. mutex_lock(&rtlpriv->locks.conf_mutex);
  519. if ((vif->type == NL80211_IFTYPE_ADHOC) ||
  520. (vif->type == NL80211_IFTYPE_AP) ||
  521. (vif->type == NL80211_IFTYPE_MESH_POINT)) {
  522. if ((changed & BSS_CHANGED_BEACON) ||
  523. (changed & BSS_CHANGED_BEACON_ENABLED &&
  524. bss_conf->enable_beacon)) {
  525. if (mac->beacon_enabled == 0) {
  526. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  527. "BSS_CHANGED_BEACON_ENABLED\n");
  528. /*start hw beacon interrupt. */
  529. /*rtlpriv->cfg->ops->set_bcn_reg(hw); */
  530. mac->beacon_enabled = 1;
  531. rtlpriv->cfg->ops->update_interrupt_mask(hw,
  532. rtlpriv->cfg->maps
  533. [RTL_IBSS_INT_MASKS],
  534. 0);
  535. if (rtlpriv->cfg->ops->linked_set_reg)
  536. rtlpriv->cfg->ops->linked_set_reg(hw);
  537. }
  538. }
  539. if ((changed & BSS_CHANGED_BEACON_ENABLED &&
  540. !bss_conf->enable_beacon)) {
  541. if (mac->beacon_enabled == 1) {
  542. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  543. "ADHOC DISABLE BEACON\n");
  544. mac->beacon_enabled = 0;
  545. rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
  546. rtlpriv->cfg->maps
  547. [RTL_IBSS_INT_MASKS]);
  548. }
  549. }
  550. if (changed & BSS_CHANGED_BEACON_INT) {
  551. RT_TRACE(rtlpriv, COMP_BEACON, DBG_TRACE,
  552. "BSS_CHANGED_BEACON_INT\n");
  553. mac->beacon_interval = bss_conf->beacon_int;
  554. rtlpriv->cfg->ops->set_bcn_intv(hw);
  555. }
  556. }
  557. /*TODO: reference to enum ieee80211_bss_change */
  558. if (changed & BSS_CHANGED_ASSOC) {
  559. if (bss_conf->assoc) {
  560. /* we should reset all sec info & cam
  561. * before set cam after linked, we should not
  562. * reset in disassoc, that will cause tkip->wep
  563. * fail because some flag will be wrong */
  564. /* reset sec info */
  565. rtl_cam_reset_sec_info(hw);
  566. /* reset cam to fix wep fail issue
  567. * when change from wpa to wep */
  568. rtl_cam_reset_all_entry(hw);
  569. mac->link_state = MAC80211_LINKED;
  570. mac->cnt_after_linked = 0;
  571. mac->assoc_id = bss_conf->aid;
  572. memcpy(mac->bssid, bss_conf->bssid, 6);
  573. if (rtlpriv->cfg->ops->linked_set_reg)
  574. rtlpriv->cfg->ops->linked_set_reg(hw);
  575. if (mac->opmode == NL80211_IFTYPE_STATION && sta)
  576. rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
  577. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  578. "BSS_CHANGED_ASSOC\n");
  579. } else {
  580. if (mac->link_state == MAC80211_LINKED)
  581. rtl_lps_leave(hw);
  582. mac->link_state = MAC80211_NOLINK;
  583. memset(mac->bssid, 0, 6);
  584. /* reset sec info */
  585. rtl_cam_reset_sec_info(hw);
  586. rtl_cam_reset_all_entry(hw);
  587. mac->vendor = PEER_UNKNOWN;
  588. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  589. "BSS_CHANGED_UN_ASSOC\n");
  590. }
  591. }
  592. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  593. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  594. "BSS_CHANGED_ERP_CTS_PROT\n");
  595. mac->use_cts_protect = bss_conf->use_cts_prot;
  596. }
  597. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  598. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  599. "BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
  600. bss_conf->use_short_preamble);
  601. mac->short_preamble = bss_conf->use_short_preamble;
  602. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
  603. &mac->short_preamble);
  604. }
  605. if (changed & BSS_CHANGED_ERP_SLOT) {
  606. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  607. "BSS_CHANGED_ERP_SLOT\n");
  608. if (bss_conf->use_short_slot)
  609. mac->slot_time = RTL_SLOT_TIME_9;
  610. else
  611. mac->slot_time = RTL_SLOT_TIME_20;
  612. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
  613. &mac->slot_time);
  614. }
  615. if (changed & BSS_CHANGED_HT) {
  616. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE, "BSS_CHANGED_HT\n");
  617. rcu_read_lock();
  618. sta = get_sta(hw, vif, bss_conf->bssid);
  619. if (sta) {
  620. if (sta->ht_cap.ampdu_density >
  621. mac->current_ampdu_density)
  622. mac->current_ampdu_density =
  623. sta->ht_cap.ampdu_density;
  624. if (sta->ht_cap.ampdu_factor <
  625. mac->current_ampdu_factor)
  626. mac->current_ampdu_factor =
  627. sta->ht_cap.ampdu_factor;
  628. }
  629. rcu_read_unlock();
  630. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
  631. &mac->max_mss_density);
  632. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
  633. &mac->current_ampdu_factor);
  634. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
  635. &mac->current_ampdu_density);
  636. }
  637. if (changed & BSS_CHANGED_BSSID) {
  638. u32 basic_rates;
  639. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
  640. (u8 *) bss_conf->bssid);
  641. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG, "%pM\n",
  642. bss_conf->bssid);
  643. mac->vendor = PEER_UNKNOWN;
  644. memcpy(mac->bssid, bss_conf->bssid, 6);
  645. rtlpriv->cfg->ops->set_network_type(hw, vif->type);
  646. rcu_read_lock();
  647. sta = get_sta(hw, vif, bss_conf->bssid);
  648. if (!sta) {
  649. rcu_read_unlock();
  650. goto out;
  651. }
  652. if (rtlhal->current_bandtype == BAND_ON_5G) {
  653. mac->mode = WIRELESS_MODE_A;
  654. } else {
  655. if (sta->supp_rates[0] <= 0xf)
  656. mac->mode = WIRELESS_MODE_B;
  657. else
  658. mac->mode = WIRELESS_MODE_G;
  659. }
  660. if (sta->ht_cap.ht_supported) {
  661. if (rtlhal->current_bandtype == BAND_ON_2_4G)
  662. mac->mode = WIRELESS_MODE_N_24G;
  663. else
  664. mac->mode = WIRELESS_MODE_N_5G;
  665. }
  666. /* just station need it, because ibss & ap mode will
  667. * set in sta_add, and will be NULL here */
  668. if (mac->opmode == NL80211_IFTYPE_STATION) {
  669. struct rtl_sta_info *sta_entry;
  670. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  671. sta_entry->wireless_mode = mac->mode;
  672. }
  673. if (sta->ht_cap.ht_supported) {
  674. mac->ht_enable = true;
  675. /*
  676. * for cisco 1252 bw20 it's wrong
  677. * if (ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
  678. * mac->bw_40 = true;
  679. * }
  680. * */
  681. }
  682. if (changed & BSS_CHANGED_BASIC_RATES) {
  683. /* for 5G must << RATE_6M_INDEX=4,
  684. * because 5G have no cck rate*/
  685. if (rtlhal->current_bandtype == BAND_ON_5G)
  686. basic_rates = sta->supp_rates[1] << 4;
  687. else
  688. basic_rates = sta->supp_rates[0];
  689. mac->basic_rates = basic_rates;
  690. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
  691. (u8 *) (&basic_rates));
  692. }
  693. rcu_read_unlock();
  694. }
  695. /*
  696. * For FW LPS:
  697. * To tell firmware we have connected
  698. * to an AP. For 92SE/CE power save v2.
  699. */
  700. if (changed & BSS_CHANGED_ASSOC) {
  701. if (bss_conf->assoc) {
  702. if (ppsc->fwctrl_lps) {
  703. u8 mstatus = RT_MEDIA_CONNECT;
  704. rtlpriv->cfg->ops->set_hw_reg(hw,
  705. HW_VAR_H2C_FW_JOINBSSRPT,
  706. &mstatus);
  707. ppsc->report_linked = true;
  708. }
  709. } else {
  710. if (ppsc->fwctrl_lps) {
  711. u8 mstatus = RT_MEDIA_DISCONNECT;
  712. rtlpriv->cfg->ops->set_hw_reg(hw,
  713. HW_VAR_H2C_FW_JOINBSSRPT,
  714. &mstatus);
  715. ppsc->report_linked = false;
  716. }
  717. }
  718. }
  719. out:
  720. mutex_unlock(&rtlpriv->locks.conf_mutex);
  721. }
  722. static u64 rtl_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  723. {
  724. struct rtl_priv *rtlpriv = rtl_priv(hw);
  725. u64 tsf;
  726. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *) (&tsf));
  727. return tsf;
  728. }
  729. static void rtl_op_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  730. u64 tsf)
  731. {
  732. struct rtl_priv *rtlpriv = rtl_priv(hw);
  733. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  734. u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
  735. mac->tsf = tsf;
  736. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, &bibss);
  737. }
  738. static void rtl_op_reset_tsf(struct ieee80211_hw *hw,
  739. struct ieee80211_vif *vif)
  740. {
  741. struct rtl_priv *rtlpriv = rtl_priv(hw);
  742. u8 tmp = 0;
  743. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, &tmp);
  744. }
  745. static void rtl_op_sta_notify(struct ieee80211_hw *hw,
  746. struct ieee80211_vif *vif,
  747. enum sta_notify_cmd cmd,
  748. struct ieee80211_sta *sta)
  749. {
  750. switch (cmd) {
  751. case STA_NOTIFY_SLEEP:
  752. break;
  753. case STA_NOTIFY_AWAKE:
  754. break;
  755. default:
  756. break;
  757. }
  758. }
  759. static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
  760. struct ieee80211_vif *vif,
  761. enum ieee80211_ampdu_mlme_action action,
  762. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  763. u8 buf_size)
  764. {
  765. struct rtl_priv *rtlpriv = rtl_priv(hw);
  766. switch (action) {
  767. case IEEE80211_AMPDU_TX_START:
  768. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  769. "IEEE80211_AMPDU_TX_START: TID:%d\n", tid);
  770. return rtl_tx_agg_start(hw, sta, tid, ssn);
  771. break;
  772. case IEEE80211_AMPDU_TX_STOP:
  773. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  774. "IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
  775. return rtl_tx_agg_stop(hw, sta, tid);
  776. break;
  777. case IEEE80211_AMPDU_TX_OPERATIONAL:
  778. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  779. "IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
  780. rtl_tx_agg_oper(hw, sta, tid);
  781. break;
  782. case IEEE80211_AMPDU_RX_START:
  783. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  784. "IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
  785. break;
  786. case IEEE80211_AMPDU_RX_STOP:
  787. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  788. "IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
  789. break;
  790. default:
  791. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  792. "IEEE80211_AMPDU_ERR!!!!:\n");
  793. return -EOPNOTSUPP;
  794. }
  795. return 0;
  796. }
  797. static void rtl_op_sw_scan_start(struct ieee80211_hw *hw)
  798. {
  799. struct rtl_priv *rtlpriv = rtl_priv(hw);
  800. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  801. mac->act_scanning = true;
  802. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
  803. if (mac->link_state == MAC80211_LINKED) {
  804. rtl_lps_leave(hw);
  805. mac->link_state = MAC80211_LINKED_SCANNING;
  806. } else {
  807. rtl_ips_nic_on(hw);
  808. }
  809. /* Dual mac */
  810. rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
  811. rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
  812. rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP);
  813. }
  814. static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw)
  815. {
  816. struct rtl_priv *rtlpriv = rtl_priv(hw);
  817. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  818. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
  819. mac->act_scanning = false;
  820. /* Dual mac */
  821. rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
  822. if (mac->link_state == MAC80211_LINKED_SCANNING) {
  823. mac->link_state = MAC80211_LINKED;
  824. if (mac->opmode == NL80211_IFTYPE_STATION) {
  825. /* fix fwlps issue */
  826. rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
  827. }
  828. }
  829. rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
  830. }
  831. static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  832. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  833. struct ieee80211_key_conf *key)
  834. {
  835. struct rtl_priv *rtlpriv = rtl_priv(hw);
  836. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  837. u8 key_type = NO_ENCRYPTION;
  838. u8 key_idx;
  839. bool group_key = false;
  840. bool wep_only = false;
  841. int err = 0;
  842. u8 mac_addr[ETH_ALEN];
  843. u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  844. u8 zero_addr[ETH_ALEN] = { 0 };
  845. if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
  846. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  847. "not open hw encryption\n");
  848. return -ENOSPC; /*User disabled HW-crypto */
  849. }
  850. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  851. "%s hardware based encryption for keyidx: %d, mac: %pM\n",
  852. cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
  853. sta ? sta->addr : bcast_addr);
  854. rtlpriv->sec.being_setkey = true;
  855. rtl_ips_nic_on(hw);
  856. mutex_lock(&rtlpriv->locks.conf_mutex);
  857. /* <1> get encryption alg */
  858. switch (key->cipher) {
  859. case WLAN_CIPHER_SUITE_WEP40:
  860. key_type = WEP40_ENCRYPTION;
  861. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP40\n");
  862. break;
  863. case WLAN_CIPHER_SUITE_WEP104:
  864. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP104\n");
  865. key_type = WEP104_ENCRYPTION;
  866. break;
  867. case WLAN_CIPHER_SUITE_TKIP:
  868. key_type = TKIP_ENCRYPTION;
  869. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:TKIP\n");
  870. break;
  871. case WLAN_CIPHER_SUITE_CCMP:
  872. key_type = AESCCMP_ENCRYPTION;
  873. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CCMP\n");
  874. break;
  875. default:
  876. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "alg_err:%x!!!!\n",
  877. key->cipher);
  878. goto out_unlock;
  879. }
  880. if (key_type == WEP40_ENCRYPTION ||
  881. key_type == WEP104_ENCRYPTION ||
  882. mac->opmode == NL80211_IFTYPE_ADHOC)
  883. rtlpriv->sec.use_defaultkey = true;
  884. /* <2> get key_idx */
  885. key_idx = (u8) (key->keyidx);
  886. if (key_idx > 3)
  887. goto out_unlock;
  888. /* <3> if pairwise key enable_hw_sec */
  889. group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  890. /* wep always be group key, but there are two conditions:
  891. * 1) wep only: is just for wep enc, in this condition
  892. * rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION
  893. * will be true & enable_hw_sec will be set when wep
  894. * ke setting.
  895. * 2) wep(group) + AES(pairwise): some AP like cisco
  896. * may use it, in this condition enable_hw_sec will not
  897. * be set when wep key setting */
  898. /* we must reset sec_info after lingked before set key,
  899. * or some flag will be wrong*/
  900. if (mac->opmode == NL80211_IFTYPE_AP) {
  901. if (!group_key || key_type == WEP40_ENCRYPTION ||
  902. key_type == WEP104_ENCRYPTION) {
  903. if (group_key)
  904. wep_only = true;
  905. rtlpriv->cfg->ops->enable_hw_sec(hw);
  906. }
  907. } else {
  908. if ((!group_key) || (mac->opmode == NL80211_IFTYPE_ADHOC) ||
  909. rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
  910. if (rtlpriv->sec.pairwise_enc_algorithm ==
  911. NO_ENCRYPTION &&
  912. (key_type == WEP40_ENCRYPTION ||
  913. key_type == WEP104_ENCRYPTION))
  914. wep_only = true;
  915. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  916. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  917. "set enable_hw_sec, key_type:%x(OPEN:0 WEP40:1 TKIP:2 AES:4 WEP104:5)\n",
  918. key_type);
  919. rtlpriv->cfg->ops->enable_hw_sec(hw);
  920. }
  921. }
  922. /* <4> set key based on cmd */
  923. switch (cmd) {
  924. case SET_KEY:
  925. if (wep_only) {
  926. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  927. "set WEP(group/pairwise) key\n");
  928. /* Pairwise key with an assigned MAC address. */
  929. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  930. rtlpriv->sec.group_enc_algorithm = key_type;
  931. /*set local buf about wep key. */
  932. memcpy(rtlpriv->sec.key_buf[key_idx],
  933. key->key, key->keylen);
  934. rtlpriv->sec.key_len[key_idx] = key->keylen;
  935. memcpy(mac_addr, zero_addr, ETH_ALEN);
  936. } else if (group_key) { /* group key */
  937. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  938. "set group key\n");
  939. /* group key */
  940. rtlpriv->sec.group_enc_algorithm = key_type;
  941. /*set local buf about group key. */
  942. memcpy(rtlpriv->sec.key_buf[key_idx],
  943. key->key, key->keylen);
  944. rtlpriv->sec.key_len[key_idx] = key->keylen;
  945. memcpy(mac_addr, bcast_addr, ETH_ALEN);
  946. } else { /* pairwise key */
  947. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  948. "set pairwise key\n");
  949. if (!sta) {
  950. RT_ASSERT(false,
  951. "pairwise key without mac_addr\n");
  952. err = -EOPNOTSUPP;
  953. goto out_unlock;
  954. }
  955. /* Pairwise key with an assigned MAC address. */
  956. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  957. /*set local buf about pairwise key. */
  958. memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
  959. key->key, key->keylen);
  960. rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
  961. rtlpriv->sec.pairwise_key =
  962. rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
  963. memcpy(mac_addr, sta->addr, ETH_ALEN);
  964. }
  965. rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
  966. group_key, key_type, wep_only,
  967. false);
  968. /* <5> tell mac80211 do something: */
  969. /*must use sw generate IV, or can not work !!!!. */
  970. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  971. key->hw_key_idx = key_idx;
  972. if (key_type == TKIP_ENCRYPTION)
  973. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  974. break;
  975. case DISABLE_KEY:
  976. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  977. "disable key delete one entry\n");
  978. /*set local buf about wep key. */
  979. if (mac->opmode == NL80211_IFTYPE_AP) {
  980. if (sta)
  981. rtl_cam_del_entry(hw, sta->addr);
  982. }
  983. memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
  984. rtlpriv->sec.key_len[key_idx] = 0;
  985. memcpy(mac_addr, zero_addr, ETH_ALEN);
  986. /*
  987. *mac80211 will delete entrys one by one,
  988. *so don't use rtl_cam_reset_all_entry
  989. *or clear all entry here.
  990. */
  991. rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
  992. rtl_cam_reset_sec_info(hw);
  993. break;
  994. default:
  995. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  996. "cmd_err:%x!!!!\n", cmd);
  997. }
  998. out_unlock:
  999. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1000. rtlpriv->sec.being_setkey = false;
  1001. return err;
  1002. }
  1003. static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
  1004. {
  1005. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1006. bool radio_state;
  1007. bool blocked;
  1008. u8 valid = 0;
  1009. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  1010. return;
  1011. mutex_lock(&rtlpriv->locks.conf_mutex);
  1012. /*if Radio On return true here */
  1013. radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
  1014. if (valid) {
  1015. if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
  1016. rtlpriv->rfkill.rfkill_state = radio_state;
  1017. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  1018. "wireless radio switch turned %s\n",
  1019. radio_state ? "on" : "off");
  1020. blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
  1021. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  1022. }
  1023. }
  1024. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1025. }
  1026. /* this function is called by mac80211 to flush tx buffer
  1027. * before switch channle or power save, or tx buffer packet
  1028. * maybe send after offchannel or rf sleep, this may cause
  1029. * dis-association by AP */
  1030. static void rtl_op_flush(struct ieee80211_hw *hw, bool drop)
  1031. {
  1032. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1033. if (rtlpriv->intf_ops->flush)
  1034. rtlpriv->intf_ops->flush(hw, drop);
  1035. }
  1036. const struct ieee80211_ops rtl_ops = {
  1037. .start = rtl_op_start,
  1038. .stop = rtl_op_stop,
  1039. .tx = rtl_op_tx,
  1040. .add_interface = rtl_op_add_interface,
  1041. .remove_interface = rtl_op_remove_interface,
  1042. .config = rtl_op_config,
  1043. .configure_filter = rtl_op_configure_filter,
  1044. .sta_add = rtl_op_sta_add,
  1045. .sta_remove = rtl_op_sta_remove,
  1046. .set_key = rtl_op_set_key,
  1047. .conf_tx = rtl_op_conf_tx,
  1048. .bss_info_changed = rtl_op_bss_info_changed,
  1049. .get_tsf = rtl_op_get_tsf,
  1050. .set_tsf = rtl_op_set_tsf,
  1051. .reset_tsf = rtl_op_reset_tsf,
  1052. .sta_notify = rtl_op_sta_notify,
  1053. .ampdu_action = rtl_op_ampdu_action,
  1054. .sw_scan_start = rtl_op_sw_scan_start,
  1055. .sw_scan_complete = rtl_op_sw_scan_complete,
  1056. .rfkill_poll = rtl_op_rfkill_poll,
  1057. .flush = rtl_op_flush,
  1058. };