core.c 33 KB

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