core.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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_CONT:
  773. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  774. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  775. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  776. "IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
  777. return rtl_tx_agg_stop(hw, sta, tid);
  778. break;
  779. case IEEE80211_AMPDU_TX_OPERATIONAL:
  780. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  781. "IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
  782. rtl_tx_agg_oper(hw, sta, tid);
  783. break;
  784. case IEEE80211_AMPDU_RX_START:
  785. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  786. "IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
  787. break;
  788. case IEEE80211_AMPDU_RX_STOP:
  789. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  790. "IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
  791. break;
  792. default:
  793. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  794. "IEEE80211_AMPDU_ERR!!!!:\n");
  795. return -EOPNOTSUPP;
  796. }
  797. return 0;
  798. }
  799. static void rtl_op_sw_scan_start(struct ieee80211_hw *hw)
  800. {
  801. struct rtl_priv *rtlpriv = rtl_priv(hw);
  802. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  803. mac->act_scanning = true;
  804. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
  805. if (mac->link_state == MAC80211_LINKED) {
  806. rtl_lps_leave(hw);
  807. mac->link_state = MAC80211_LINKED_SCANNING;
  808. } else {
  809. rtl_ips_nic_on(hw);
  810. }
  811. /* Dual mac */
  812. rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
  813. rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
  814. rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP);
  815. }
  816. static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw)
  817. {
  818. struct rtl_priv *rtlpriv = rtl_priv(hw);
  819. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  820. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
  821. mac->act_scanning = false;
  822. /* Dual mac */
  823. rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
  824. if (mac->link_state == MAC80211_LINKED_SCANNING) {
  825. mac->link_state = MAC80211_LINKED;
  826. if (mac->opmode == NL80211_IFTYPE_STATION) {
  827. /* fix fwlps issue */
  828. rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
  829. }
  830. }
  831. rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
  832. }
  833. static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  834. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  835. struct ieee80211_key_conf *key)
  836. {
  837. struct rtl_priv *rtlpriv = rtl_priv(hw);
  838. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  839. u8 key_type = NO_ENCRYPTION;
  840. u8 key_idx;
  841. bool group_key = false;
  842. bool wep_only = false;
  843. int err = 0;
  844. u8 mac_addr[ETH_ALEN];
  845. u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  846. if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
  847. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  848. "not open hw encryption\n");
  849. return -ENOSPC; /*User disabled HW-crypto */
  850. }
  851. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  852. "%s hardware based encryption for keyidx: %d, mac: %pM\n",
  853. cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
  854. sta ? sta->addr : bcast_addr);
  855. rtlpriv->sec.being_setkey = true;
  856. rtl_ips_nic_on(hw);
  857. mutex_lock(&rtlpriv->locks.conf_mutex);
  858. /* <1> get encryption alg */
  859. switch (key->cipher) {
  860. case WLAN_CIPHER_SUITE_WEP40:
  861. key_type = WEP40_ENCRYPTION;
  862. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP40\n");
  863. break;
  864. case WLAN_CIPHER_SUITE_WEP104:
  865. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP104\n");
  866. key_type = WEP104_ENCRYPTION;
  867. break;
  868. case WLAN_CIPHER_SUITE_TKIP:
  869. key_type = TKIP_ENCRYPTION;
  870. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:TKIP\n");
  871. break;
  872. case WLAN_CIPHER_SUITE_CCMP:
  873. key_type = AESCCMP_ENCRYPTION;
  874. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CCMP\n");
  875. break;
  876. default:
  877. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "alg_err:%x!!!!\n",
  878. key->cipher);
  879. goto out_unlock;
  880. }
  881. if (key_type == WEP40_ENCRYPTION ||
  882. key_type == WEP104_ENCRYPTION ||
  883. mac->opmode == NL80211_IFTYPE_ADHOC)
  884. rtlpriv->sec.use_defaultkey = true;
  885. /* <2> get key_idx */
  886. key_idx = (u8) (key->keyidx);
  887. if (key_idx > 3)
  888. goto out_unlock;
  889. /* <3> if pairwise key enable_hw_sec */
  890. group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  891. /* wep always be group key, but there are two conditions:
  892. * 1) wep only: is just for wep enc, in this condition
  893. * rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION
  894. * will be true & enable_hw_sec will be set when wep
  895. * ke setting.
  896. * 2) wep(group) + AES(pairwise): some AP like cisco
  897. * may use it, in this condition enable_hw_sec will not
  898. * be set when wep key setting */
  899. /* we must reset sec_info after lingked before set key,
  900. * or some flag will be wrong*/
  901. if (mac->opmode == NL80211_IFTYPE_AP) {
  902. if (!group_key || key_type == WEP40_ENCRYPTION ||
  903. key_type == WEP104_ENCRYPTION) {
  904. if (group_key)
  905. wep_only = true;
  906. rtlpriv->cfg->ops->enable_hw_sec(hw);
  907. }
  908. } else {
  909. if ((!group_key) || (mac->opmode == NL80211_IFTYPE_ADHOC) ||
  910. rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
  911. if (rtlpriv->sec.pairwise_enc_algorithm ==
  912. NO_ENCRYPTION &&
  913. (key_type == WEP40_ENCRYPTION ||
  914. key_type == WEP104_ENCRYPTION))
  915. wep_only = true;
  916. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  917. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  918. "set enable_hw_sec, key_type:%x(OPEN:0 WEP40:1 TKIP:2 AES:4 WEP104:5)\n",
  919. key_type);
  920. rtlpriv->cfg->ops->enable_hw_sec(hw);
  921. }
  922. }
  923. /* <4> set key based on cmd */
  924. switch (cmd) {
  925. case SET_KEY:
  926. if (wep_only) {
  927. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  928. "set WEP(group/pairwise) key\n");
  929. /* Pairwise key with an assigned MAC address. */
  930. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  931. rtlpriv->sec.group_enc_algorithm = key_type;
  932. /*set local buf about wep key. */
  933. memcpy(rtlpriv->sec.key_buf[key_idx],
  934. key->key, key->keylen);
  935. rtlpriv->sec.key_len[key_idx] = key->keylen;
  936. eth_zero_addr(mac_addr);
  937. } else if (group_key) { /* group key */
  938. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  939. "set group key\n");
  940. /* group key */
  941. rtlpriv->sec.group_enc_algorithm = key_type;
  942. /*set local buf about group key. */
  943. memcpy(rtlpriv->sec.key_buf[key_idx],
  944. key->key, key->keylen);
  945. rtlpriv->sec.key_len[key_idx] = key->keylen;
  946. memcpy(mac_addr, bcast_addr, ETH_ALEN);
  947. } else { /* pairwise key */
  948. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  949. "set pairwise key\n");
  950. if (!sta) {
  951. RT_ASSERT(false,
  952. "pairwise key without mac_addr\n");
  953. err = -EOPNOTSUPP;
  954. goto out_unlock;
  955. }
  956. /* Pairwise key with an assigned MAC address. */
  957. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  958. /*set local buf about pairwise key. */
  959. memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
  960. key->key, key->keylen);
  961. rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
  962. rtlpriv->sec.pairwise_key =
  963. rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
  964. memcpy(mac_addr, sta->addr, ETH_ALEN);
  965. }
  966. rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
  967. group_key, key_type, wep_only,
  968. false);
  969. /* <5> tell mac80211 do something: */
  970. /*must use sw generate IV, or can not work !!!!. */
  971. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  972. key->hw_key_idx = key_idx;
  973. if (key_type == TKIP_ENCRYPTION)
  974. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  975. break;
  976. case DISABLE_KEY:
  977. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  978. "disable key delete one entry\n");
  979. /*set local buf about wep key. */
  980. if (mac->opmode == NL80211_IFTYPE_AP) {
  981. if (sta)
  982. rtl_cam_del_entry(hw, sta->addr);
  983. }
  984. memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
  985. rtlpriv->sec.key_len[key_idx] = 0;
  986. eth_zero_addr(mac_addr);
  987. /*
  988. *mac80211 will delete entrys one by one,
  989. *so don't use rtl_cam_reset_all_entry
  990. *or clear all entry here.
  991. */
  992. rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
  993. rtl_cam_reset_sec_info(hw);
  994. break;
  995. default:
  996. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  997. "cmd_err:%x!!!!\n", cmd);
  998. }
  999. out_unlock:
  1000. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1001. rtlpriv->sec.being_setkey = false;
  1002. return err;
  1003. }
  1004. static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
  1005. {
  1006. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1007. bool radio_state;
  1008. bool blocked;
  1009. u8 valid = 0;
  1010. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  1011. return;
  1012. mutex_lock(&rtlpriv->locks.conf_mutex);
  1013. /*if Radio On return true here */
  1014. radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
  1015. if (valid) {
  1016. if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
  1017. rtlpriv->rfkill.rfkill_state = radio_state;
  1018. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  1019. "wireless radio switch turned %s\n",
  1020. radio_state ? "on" : "off");
  1021. blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
  1022. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  1023. }
  1024. }
  1025. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1026. }
  1027. /* this function is called by mac80211 to flush tx buffer
  1028. * before switch channle or power save, or tx buffer packet
  1029. * maybe send after offchannel or rf sleep, this may cause
  1030. * dis-association by AP */
  1031. static void rtl_op_flush(struct ieee80211_hw *hw, bool drop)
  1032. {
  1033. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1034. if (rtlpriv->intf_ops->flush)
  1035. rtlpriv->intf_ops->flush(hw, drop);
  1036. }
  1037. const struct ieee80211_ops rtl_ops = {
  1038. .start = rtl_op_start,
  1039. .stop = rtl_op_stop,
  1040. .tx = rtl_op_tx,
  1041. .add_interface = rtl_op_add_interface,
  1042. .remove_interface = rtl_op_remove_interface,
  1043. .config = rtl_op_config,
  1044. .configure_filter = rtl_op_configure_filter,
  1045. .sta_add = rtl_op_sta_add,
  1046. .sta_remove = rtl_op_sta_remove,
  1047. .set_key = rtl_op_set_key,
  1048. .conf_tx = rtl_op_conf_tx,
  1049. .bss_info_changed = rtl_op_bss_info_changed,
  1050. .get_tsf = rtl_op_get_tsf,
  1051. .set_tsf = rtl_op_set_tsf,
  1052. .reset_tsf = rtl_op_reset_tsf,
  1053. .sta_notify = rtl_op_sta_notify,
  1054. .ampdu_action = rtl_op_ampdu_action,
  1055. .sw_scan_start = rtl_op_sw_scan_start,
  1056. .sw_scan_complete = rtl_op_sw_scan_complete,
  1057. .rfkill_poll = rtl_op_rfkill_poll,
  1058. .flush = rtl_op_flush,
  1059. };