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