core.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  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. * The full GNU General Public License is included in this distribution in the
  15. * file called LICENSE.
  16. *
  17. * Contact Information:
  18. * wlanfae <wlanfae@realtek.com>
  19. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  20. * Hsinchu 300, Taiwan.
  21. *
  22. * Larry Finger <Larry.Finger@lwfinger.net>
  23. *
  24. *****************************************************************************/
  25. #include "wifi.h"
  26. #include "core.h"
  27. #include "cam.h"
  28. #include "base.h"
  29. #include "ps.h"
  30. #include "pwrseqcmd.h"
  31. #include "btcoexist/rtl_btc.h"
  32. #include <linux/firmware.h>
  33. #include <linux/export.h>
  34. #include <net/cfg80211.h>
  35. void rtl_addr_delay(u32 addr)
  36. {
  37. if (addr == 0xfe)
  38. mdelay(50);
  39. else if (addr == 0xfd)
  40. mdelay(5);
  41. else if (addr == 0xfc)
  42. mdelay(1);
  43. else if (addr == 0xfb)
  44. udelay(50);
  45. else if (addr == 0xfa)
  46. udelay(5);
  47. else if (addr == 0xf9)
  48. udelay(1);
  49. }
  50. EXPORT_SYMBOL(rtl_addr_delay);
  51. void rtl_rfreg_delay(struct ieee80211_hw *hw, enum radio_path rfpath, u32 addr,
  52. u32 mask, u32 data)
  53. {
  54. if (addr == 0xfe) {
  55. mdelay(50);
  56. } else if (addr == 0xfd) {
  57. mdelay(5);
  58. } else if (addr == 0xfc) {
  59. mdelay(1);
  60. } else if (addr == 0xfb) {
  61. udelay(50);
  62. } else if (addr == 0xfa) {
  63. udelay(5);
  64. } else if (addr == 0xf9) {
  65. udelay(1);
  66. } else {
  67. rtl_set_rfreg(hw, rfpath, addr, mask, data);
  68. udelay(1);
  69. }
  70. }
  71. EXPORT_SYMBOL(rtl_rfreg_delay);
  72. void rtl_bb_delay(struct ieee80211_hw *hw, u32 addr, u32 data)
  73. {
  74. if (addr == 0xfe) {
  75. mdelay(50);
  76. } else if (addr == 0xfd) {
  77. mdelay(5);
  78. } else if (addr == 0xfc) {
  79. mdelay(1);
  80. } else if (addr == 0xfb) {
  81. udelay(50);
  82. } else if (addr == 0xfa) {
  83. udelay(5);
  84. } else if (addr == 0xf9) {
  85. udelay(1);
  86. } else {
  87. rtl_set_bbreg(hw, addr, MASKDWORD, data);
  88. udelay(1);
  89. }
  90. }
  91. EXPORT_SYMBOL(rtl_bb_delay);
  92. static void rtl_fw_do_work(const struct firmware *firmware, void *context,
  93. bool is_wow)
  94. {
  95. struct ieee80211_hw *hw = context;
  96. struct rtl_priv *rtlpriv = rtl_priv(hw);
  97. int err;
  98. RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
  99. "Firmware callback routine entered!\n");
  100. complete(&rtlpriv->firmware_loading_complete);
  101. if (!firmware) {
  102. if (rtlpriv->cfg->alt_fw_name) {
  103. err = request_firmware(&firmware,
  104. rtlpriv->cfg->alt_fw_name,
  105. rtlpriv->io.dev);
  106. pr_info("Loading alternative firmware %s\n",
  107. rtlpriv->cfg->alt_fw_name);
  108. if (!err)
  109. goto found_alt;
  110. }
  111. pr_err("Firmware %s not available\n", rtlpriv->cfg->fw_name);
  112. rtlpriv->max_fw_size = 0;
  113. return;
  114. }
  115. found_alt:
  116. if (firmware->size > rtlpriv->max_fw_size) {
  117. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  118. "Firmware is too big!\n");
  119. release_firmware(firmware);
  120. return;
  121. }
  122. if (!is_wow) {
  123. memcpy(rtlpriv->rtlhal.pfirmware, firmware->data,
  124. firmware->size);
  125. rtlpriv->rtlhal.fwsize = firmware->size;
  126. } else {
  127. memcpy(rtlpriv->rtlhal.wowlan_firmware, firmware->data,
  128. firmware->size);
  129. rtlpriv->rtlhal.wowlan_fwsize = firmware->size;
  130. }
  131. rtlpriv->rtlhal.fwsize = firmware->size;
  132. release_firmware(firmware);
  133. }
  134. void rtl_fw_cb(const struct firmware *firmware, void *context)
  135. {
  136. rtl_fw_do_work(firmware, context, false);
  137. }
  138. EXPORT_SYMBOL(rtl_fw_cb);
  139. void rtl_wowlan_fw_cb(const struct firmware *firmware, void *context)
  140. {
  141. rtl_fw_do_work(firmware, context, true);
  142. }
  143. EXPORT_SYMBOL(rtl_wowlan_fw_cb);
  144. /*mutex for start & stop is must here. */
  145. static int rtl_op_start(struct ieee80211_hw *hw)
  146. {
  147. int err = 0;
  148. struct rtl_priv *rtlpriv = rtl_priv(hw);
  149. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  150. if (!is_hal_stop(rtlhal))
  151. return 0;
  152. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  153. return 0;
  154. mutex_lock(&rtlpriv->locks.conf_mutex);
  155. err = rtlpriv->intf_ops->adapter_start(hw);
  156. if (!err)
  157. rtl_watch_dog_timer_callback((unsigned long)hw);
  158. mutex_unlock(&rtlpriv->locks.conf_mutex);
  159. return err;
  160. }
  161. static void rtl_op_stop(struct ieee80211_hw *hw)
  162. {
  163. struct rtl_priv *rtlpriv = rtl_priv(hw);
  164. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  165. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  166. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  167. bool support_remote_wakeup = false;
  168. if (is_hal_stop(rtlhal))
  169. return;
  170. rtlpriv->cfg->ops->get_hw_reg(hw, HAL_DEF_WOWLAN,
  171. (u8 *)(&support_remote_wakeup));
  172. /* here is must, because adhoc do stop and start,
  173. * but stop with RFOFF may cause something wrong,
  174. * like adhoc TP
  175. */
  176. if (unlikely(ppsc->rfpwr_state == ERFOFF))
  177. rtl_ips_nic_on(hw);
  178. mutex_lock(&rtlpriv->locks.conf_mutex);
  179. /* if wowlan supported, DON'T clear connected info */
  180. if (!(support_remote_wakeup &&
  181. rtlhal->enter_pnp_sleep)) {
  182. mac->link_state = MAC80211_NOLINK;
  183. eth_zero_addr(mac->bssid);
  184. mac->vendor = PEER_UNKNOWN;
  185. /* reset sec info */
  186. rtl_cam_reset_sec_info(hw);
  187. rtl_deinit_deferred_work(hw);
  188. }
  189. rtlpriv->intf_ops->adapter_stop(hw);
  190. mutex_unlock(&rtlpriv->locks.conf_mutex);
  191. }
  192. static void rtl_op_tx(struct ieee80211_hw *hw,
  193. struct ieee80211_tx_control *control,
  194. struct sk_buff *skb)
  195. {
  196. struct rtl_priv *rtlpriv = rtl_priv(hw);
  197. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  198. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  199. struct rtl_tcb_desc tcb_desc;
  200. memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
  201. if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
  202. goto err_free;
  203. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  204. goto err_free;
  205. if (!rtlpriv->intf_ops->waitq_insert(hw, control->sta, skb))
  206. rtlpriv->intf_ops->adapter_tx(hw, control->sta, skb, &tcb_desc);
  207. return;
  208. err_free:
  209. dev_kfree_skb_any(skb);
  210. }
  211. static int rtl_op_add_interface(struct ieee80211_hw *hw,
  212. struct ieee80211_vif *vif)
  213. {
  214. struct rtl_priv *rtlpriv = rtl_priv(hw);
  215. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  216. int err = 0;
  217. if (mac->vif) {
  218. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  219. "vif has been set!! mac->vif = 0x%p\n", mac->vif);
  220. return -EOPNOTSUPP;
  221. }
  222. vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
  223. rtl_ips_nic_on(hw);
  224. mutex_lock(&rtlpriv->locks.conf_mutex);
  225. switch (ieee80211_vif_type_p2p(vif)) {
  226. case NL80211_IFTYPE_P2P_CLIENT:
  227. mac->p2p = P2P_ROLE_CLIENT;
  228. /*fall through*/
  229. case NL80211_IFTYPE_STATION:
  230. if (mac->beacon_enabled == 1) {
  231. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  232. "NL80211_IFTYPE_STATION\n");
  233. mac->beacon_enabled = 0;
  234. rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
  235. rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
  236. }
  237. break;
  238. case NL80211_IFTYPE_ADHOC:
  239. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  240. "NL80211_IFTYPE_ADHOC\n");
  241. mac->link_state = MAC80211_LINKED;
  242. rtlpriv->cfg->ops->set_bcn_reg(hw);
  243. if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
  244. mac->basic_rates = 0xfff;
  245. else
  246. mac->basic_rates = 0xff0;
  247. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
  248. (u8 *)(&mac->basic_rates));
  249. break;
  250. case NL80211_IFTYPE_P2P_GO:
  251. mac->p2p = P2P_ROLE_GO;
  252. /*fall through*/
  253. case NL80211_IFTYPE_AP:
  254. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  255. "NL80211_IFTYPE_AP\n");
  256. mac->link_state = MAC80211_LINKED;
  257. rtlpriv->cfg->ops->set_bcn_reg(hw);
  258. if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
  259. mac->basic_rates = 0xfff;
  260. else
  261. mac->basic_rates = 0xff0;
  262. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
  263. (u8 *)(&mac->basic_rates));
  264. break;
  265. case NL80211_IFTYPE_MESH_POINT:
  266. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  267. "NL80211_IFTYPE_MESH_POINT\n");
  268. mac->link_state = MAC80211_LINKED;
  269. rtlpriv->cfg->ops->set_bcn_reg(hw);
  270. if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
  271. mac->basic_rates = 0xfff;
  272. else
  273. mac->basic_rates = 0xff0;
  274. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
  275. (u8 *)(&mac->basic_rates));
  276. break;
  277. default:
  278. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  279. "operation mode %d is not support!\n", vif->type);
  280. err = -EOPNOTSUPP;
  281. goto out;
  282. }
  283. if (mac->p2p) {
  284. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  285. "p2p role %x\n", vif->type);
  286. mac->basic_rates = 0xff0;/*disable cck rate for p2p*/
  287. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
  288. (u8 *)(&mac->basic_rates));
  289. }
  290. mac->vif = vif;
  291. mac->opmode = vif->type;
  292. rtlpriv->cfg->ops->set_network_type(hw, vif->type);
  293. memcpy(mac->mac_addr, vif->addr, ETH_ALEN);
  294. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
  295. out:
  296. mutex_unlock(&rtlpriv->locks.conf_mutex);
  297. return err;
  298. }
  299. static void rtl_op_remove_interface(struct ieee80211_hw *hw,
  300. struct ieee80211_vif *vif)
  301. {
  302. struct rtl_priv *rtlpriv = rtl_priv(hw);
  303. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  304. mutex_lock(&rtlpriv->locks.conf_mutex);
  305. /* Free beacon resources */
  306. if ((vif->type == NL80211_IFTYPE_AP) ||
  307. (vif->type == NL80211_IFTYPE_ADHOC) ||
  308. (vif->type == NL80211_IFTYPE_MESH_POINT)) {
  309. if (mac->beacon_enabled == 1) {
  310. mac->beacon_enabled = 0;
  311. rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
  312. rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
  313. }
  314. }
  315. /*
  316. *Note: We assume NL80211_IFTYPE_UNSPECIFIED as
  317. *NO LINK for our hardware.
  318. */
  319. mac->p2p = 0;
  320. mac->vif = NULL;
  321. mac->link_state = MAC80211_NOLINK;
  322. eth_zero_addr(mac->bssid);
  323. mac->vendor = PEER_UNKNOWN;
  324. mac->opmode = NL80211_IFTYPE_UNSPECIFIED;
  325. rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
  326. mutex_unlock(&rtlpriv->locks.conf_mutex);
  327. }
  328. static int rtl_op_change_interface(struct ieee80211_hw *hw,
  329. struct ieee80211_vif *vif,
  330. enum nl80211_iftype new_type, bool p2p)
  331. {
  332. struct rtl_priv *rtlpriv = rtl_priv(hw);
  333. int ret;
  334. rtl_op_remove_interface(hw, vif);
  335. vif->type = new_type;
  336. vif->p2p = p2p;
  337. ret = rtl_op_add_interface(hw, vif);
  338. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  339. "p2p %x\n", p2p);
  340. return ret;
  341. }
  342. #ifdef CONFIG_PM
  343. static u16 crc16_ccitt(u8 data, u16 crc)
  344. {
  345. u8 shift_in, data_bit, crc_bit11, crc_bit4, crc_bit15;
  346. u8 i;
  347. u16 result;
  348. for (i = 0; i < 8; i++) {
  349. crc_bit15 = ((crc & BIT(15)) ? 1 : 0);
  350. data_bit = (data & (BIT(0) << i) ? 1 : 0);
  351. shift_in = crc_bit15 ^ data_bit;
  352. result = crc << 1;
  353. if (shift_in == 0)
  354. result &= (~BIT(0));
  355. else
  356. result |= BIT(0);
  357. crc_bit11 = ((crc & BIT(11)) ? 1 : 0) ^ shift_in;
  358. if (crc_bit11 == 0)
  359. result &= (~BIT(12));
  360. else
  361. result |= BIT(12);
  362. crc_bit4 = ((crc & BIT(4)) ? 1 : 0) ^ shift_in;
  363. if (crc_bit4 == 0)
  364. result &= (~BIT(5));
  365. else
  366. result |= BIT(5);
  367. crc = result;
  368. }
  369. return crc;
  370. }
  371. static u16 _calculate_wol_pattern_crc(u8 *pattern, u16 len)
  372. {
  373. u16 crc = 0xffff;
  374. u32 i;
  375. for (i = 0; i < len; i++)
  376. crc = crc16_ccitt(pattern[i], crc);
  377. crc = ~crc;
  378. return crc;
  379. }
  380. static void _rtl_add_wowlan_patterns(struct ieee80211_hw *hw,
  381. struct cfg80211_wowlan *wow)
  382. {
  383. struct rtl_priv *rtlpriv = rtl_priv(hw);
  384. struct rtl_mac *mac = &rtlpriv->mac80211;
  385. struct cfg80211_pkt_pattern *patterns = wow->patterns;
  386. struct rtl_wow_pattern rtl_pattern;
  387. const u8 *pattern_os, *mask_os;
  388. u8 mask[MAX_WOL_BIT_MASK_SIZE] = {0};
  389. u8 content[MAX_WOL_PATTERN_SIZE] = {0};
  390. u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  391. u8 multicast_addr1[2] = {0x33, 0x33};
  392. u8 multicast_addr2[3] = {0x01, 0x00, 0x5e};
  393. u8 i, mask_len;
  394. u16 j, len;
  395. for (i = 0; i < wow->n_patterns; i++) {
  396. memset(&rtl_pattern, 0, sizeof(struct rtl_wow_pattern));
  397. memset(mask, 0, MAX_WOL_BIT_MASK_SIZE);
  398. if (patterns[i].pattern_len > MAX_WOL_PATTERN_SIZE) {
  399. RT_TRACE(rtlpriv, COMP_POWER, DBG_WARNING,
  400. "Pattern[%d] is too long\n", i);
  401. continue;
  402. }
  403. pattern_os = patterns[i].pattern;
  404. mask_len = DIV_ROUND_UP(patterns[i].pattern_len, 8);
  405. mask_os = patterns[i].mask;
  406. RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
  407. "pattern content\n", pattern_os,
  408. patterns[i].pattern_len);
  409. RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
  410. "mask content\n", mask_os, mask_len);
  411. /* 1. unicast? multicast? or broadcast? */
  412. if (memcmp(pattern_os, broadcast_addr, 6) == 0)
  413. rtl_pattern.type = BROADCAST_PATTERN;
  414. else if (memcmp(pattern_os, multicast_addr1, 2) == 0 ||
  415. memcmp(pattern_os, multicast_addr2, 3) == 0)
  416. rtl_pattern.type = MULTICAST_PATTERN;
  417. else if (memcmp(pattern_os, mac->mac_addr, 6) == 0)
  418. rtl_pattern.type = UNICAST_PATTERN;
  419. else
  420. rtl_pattern.type = UNKNOWN_TYPE;
  421. /* 2. translate mask_from_os to mask_for_hw */
  422. /******************************************************************************
  423. * pattern from OS uses 'ethenet frame', like this:
  424. | 6 | 6 | 2 | 20 | Variable | 4 |
  425. |--------+--------+------+-----------+------------+-----|
  426. | 802.3 Mac Header | IP Header | TCP Packet | FCS |
  427. | DA | SA | Type |
  428. * BUT, packet catched by our HW is in '802.11 frame', begin from LLC,
  429. | 24 or 30 | 6 | 2 | 20 | Variable | 4 |
  430. |-------------------+--------+------+-----------+------------+-----|
  431. | 802.11 MAC Header | LLC | IP Header | TCP Packet | FCS |
  432. | Others | Tpye |
  433. * Therefore, we need translate mask_from_OS to mask_to_hw.
  434. * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
  435. * because new mask[0~5] means 'SA', but our HW packet begins from LLC,
  436. * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
  437. ******************************************************************************/
  438. /* Shift 6 bits */
  439. for (j = 0; j < mask_len - 1; j++) {
  440. mask[j] = mask_os[j] >> 6;
  441. mask[j] |= (mask_os[j + 1] & 0x3F) << 2;
  442. }
  443. mask[j] = (mask_os[j] >> 6) & 0x3F;
  444. /* Set bit 0-5 to zero */
  445. mask[0] &= 0xC0;
  446. RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
  447. "mask to hw\n", mask, mask_len);
  448. for (j = 0; j < (MAX_WOL_BIT_MASK_SIZE + 1) / 4; j++) {
  449. rtl_pattern.mask[j] = mask[j * 4];
  450. rtl_pattern.mask[j] |= (mask[j * 4 + 1] << 8);
  451. rtl_pattern.mask[j] |= (mask[j * 4 + 2] << 16);
  452. rtl_pattern.mask[j] |= (mask[j * 4 + 3] << 24);
  453. }
  454. /* To get the wake up pattern from the mask.
  455. * We do not count first 12 bits which means
  456. * DA[6] and SA[6] in the pattern to match HW design.
  457. */
  458. len = 0;
  459. for (j = 12; j < patterns[i].pattern_len; j++) {
  460. if ((mask_os[j / 8] >> (j % 8)) & 0x01) {
  461. content[len] = pattern_os[j];
  462. len++;
  463. }
  464. }
  465. RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
  466. "pattern to hw\n", content, len);
  467. /* 3. calculate crc */
  468. rtl_pattern.crc = _calculate_wol_pattern_crc(content, len);
  469. RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
  470. "CRC_Remainder = 0x%x", rtl_pattern.crc);
  471. /* 4. write crc & mask_for_hw to hw */
  472. rtlpriv->cfg->ops->add_wowlan_pattern(hw, &rtl_pattern, i);
  473. }
  474. rtl_write_byte(rtlpriv, 0x698, wow->n_patterns);
  475. }
  476. static int rtl_op_suspend(struct ieee80211_hw *hw,
  477. struct cfg80211_wowlan *wow)
  478. {
  479. struct rtl_priv *rtlpriv = rtl_priv(hw);
  480. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  481. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  482. struct timeval ts;
  483. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
  484. if (WARN_ON(!wow))
  485. return -EINVAL;
  486. /* to resolve s4 can not wake up*/
  487. do_gettimeofday(&ts);
  488. rtlhal->last_suspend_sec = ts.tv_sec;
  489. if ((ppsc->wo_wlan_mode & WAKE_ON_PATTERN_MATCH) && wow->n_patterns)
  490. _rtl_add_wowlan_patterns(hw, wow);
  491. rtlhal->driver_is_goingto_unload = true;
  492. rtlhal->enter_pnp_sleep = true;
  493. rtl_lps_leave(hw);
  494. rtl_op_stop(hw);
  495. device_set_wakeup_enable(wiphy_dev(hw->wiphy), true);
  496. return 0;
  497. }
  498. static int rtl_op_resume(struct ieee80211_hw *hw)
  499. {
  500. struct rtl_priv *rtlpriv = rtl_priv(hw);
  501. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  502. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  503. struct timeval ts;
  504. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
  505. rtlhal->driver_is_goingto_unload = false;
  506. rtlhal->enter_pnp_sleep = false;
  507. rtlhal->wake_from_pnp_sleep = true;
  508. /* to resovle s4 can not wake up*/
  509. do_gettimeofday(&ts);
  510. if (ts.tv_sec - rtlhal->last_suspend_sec < 5)
  511. return -1;
  512. rtl_op_start(hw);
  513. device_set_wakeup_enable(wiphy_dev(hw->wiphy), false);
  514. ieee80211_resume_disconnect(mac->vif);
  515. rtlhal->wake_from_pnp_sleep = false;
  516. return 0;
  517. }
  518. #endif
  519. static int rtl_op_config(struct ieee80211_hw *hw, u32 changed)
  520. {
  521. struct rtl_priv *rtlpriv = rtl_priv(hw);
  522. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  523. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  524. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  525. struct ieee80211_conf *conf = &hw->conf;
  526. if (mac->skip_scan)
  527. return 1;
  528. mutex_lock(&rtlpriv->locks.conf_mutex);
  529. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) { /* BIT(2)*/
  530. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  531. "IEEE80211_CONF_CHANGE_LISTEN_INTERVAL\n");
  532. }
  533. /*For IPS */
  534. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  535. if (hw->conf.flags & IEEE80211_CONF_IDLE)
  536. rtl_ips_nic_off(hw);
  537. else
  538. rtl_ips_nic_on(hw);
  539. } else {
  540. /*
  541. *although rfoff may not cause by ips, but we will
  542. *check the reason in set_rf_power_state function
  543. */
  544. if (unlikely(ppsc->rfpwr_state == ERFOFF))
  545. rtl_ips_nic_on(hw);
  546. }
  547. /*For LPS */
  548. if (changed & IEEE80211_CONF_CHANGE_PS) {
  549. cancel_delayed_work(&rtlpriv->works.ps_work);
  550. cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
  551. if (conf->flags & IEEE80211_CONF_PS) {
  552. rtlpriv->psc.sw_ps_enabled = true;
  553. /* sleep here is must, or we may recv the beacon and
  554. * cause mac80211 into wrong ps state, this will cause
  555. * power save nullfunc send fail, and further cause
  556. * pkt loss, So sleep must quickly but not immediatly
  557. * because that will cause nullfunc send by mac80211
  558. * fail, and cause pkt loss, we have tested that 5mA
  559. * is worked very well */
  560. if (!rtlpriv->psc.multi_buffered)
  561. queue_delayed_work(rtlpriv->works.rtl_wq,
  562. &rtlpriv->works.ps_work,
  563. MSECS(5));
  564. } else {
  565. rtl_swlps_rf_awake(hw);
  566. rtlpriv->psc.sw_ps_enabled = false;
  567. }
  568. }
  569. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
  570. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  571. "IEEE80211_CONF_CHANGE_RETRY_LIMITS %x\n",
  572. hw->conf.long_frame_max_tx_count);
  573. mac->retry_long = hw->conf.long_frame_max_tx_count;
  574. mac->retry_short = hw->conf.long_frame_max_tx_count;
  575. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
  576. (u8 *)(&hw->conf.long_frame_max_tx_count));
  577. }
  578. if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
  579. !rtlpriv->proximity.proxim_on) {
  580. struct ieee80211_channel *channel = hw->conf.chandef.chan;
  581. enum nl80211_chan_width width = hw->conf.chandef.width;
  582. enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
  583. u8 wide_chan = (u8) channel->hw_value;
  584. /* channel_type is for 20&40M */
  585. if (width < NL80211_CHAN_WIDTH_80)
  586. channel_type =
  587. cfg80211_get_chandef_type(&hw->conf.chandef);
  588. if (mac->act_scanning)
  589. mac->n_channels++;
  590. if (rtlpriv->dm.supp_phymode_switch &&
  591. mac->link_state < MAC80211_LINKED &&
  592. !mac->act_scanning) {
  593. if (rtlpriv->cfg->ops->chk_switch_dmdp)
  594. rtlpriv->cfg->ops->chk_switch_dmdp(hw);
  595. }
  596. /*
  597. *because we should back channel to
  598. *current_network.chan in in scanning,
  599. *So if set_chan == current_network.chan
  600. *we should set it.
  601. *because mac80211 tell us wrong bw40
  602. *info for cisco1253 bw20, so we modify
  603. *it here based on UPPER & LOWER
  604. */
  605. if (width >= NL80211_CHAN_WIDTH_80) {
  606. if (width == NL80211_CHAN_WIDTH_80) {
  607. u32 center = hw->conf.chandef.center_freq1;
  608. u32 primary =
  609. (u32)hw->conf.chandef.chan->center_freq;
  610. rtlphy->current_chan_bw =
  611. HT_CHANNEL_WIDTH_80;
  612. mac->bw_80 = true;
  613. mac->bw_40 = true;
  614. if (center > primary) {
  615. mac->cur_80_prime_sc =
  616. PRIME_CHNL_OFFSET_LOWER;
  617. if (center - primary == 10) {
  618. mac->cur_40_prime_sc =
  619. PRIME_CHNL_OFFSET_UPPER;
  620. wide_chan += 2;
  621. } else if (center - primary == 30) {
  622. mac->cur_40_prime_sc =
  623. PRIME_CHNL_OFFSET_LOWER;
  624. wide_chan += 6;
  625. }
  626. } else {
  627. mac->cur_80_prime_sc =
  628. PRIME_CHNL_OFFSET_UPPER;
  629. if (primary - center == 10) {
  630. mac->cur_40_prime_sc =
  631. PRIME_CHNL_OFFSET_LOWER;
  632. wide_chan -= 2;
  633. } else if (primary - center == 30) {
  634. mac->cur_40_prime_sc =
  635. PRIME_CHNL_OFFSET_UPPER;
  636. wide_chan -= 6;
  637. }
  638. }
  639. }
  640. } else {
  641. switch (channel_type) {
  642. case NL80211_CHAN_HT20:
  643. case NL80211_CHAN_NO_HT:
  644. /* SC */
  645. mac->cur_40_prime_sc =
  646. PRIME_CHNL_OFFSET_DONT_CARE;
  647. rtlphy->current_chan_bw =
  648. HT_CHANNEL_WIDTH_20;
  649. mac->bw_40 = false;
  650. mac->bw_80 = false;
  651. break;
  652. case NL80211_CHAN_HT40MINUS:
  653. /* SC */
  654. mac->cur_40_prime_sc =
  655. PRIME_CHNL_OFFSET_UPPER;
  656. rtlphy->current_chan_bw =
  657. HT_CHANNEL_WIDTH_20_40;
  658. mac->bw_40 = true;
  659. mac->bw_80 = false;
  660. /*wide channel */
  661. wide_chan -= 2;
  662. break;
  663. case NL80211_CHAN_HT40PLUS:
  664. /* SC */
  665. mac->cur_40_prime_sc =
  666. PRIME_CHNL_OFFSET_LOWER;
  667. rtlphy->current_chan_bw =
  668. HT_CHANNEL_WIDTH_20_40;
  669. mac->bw_40 = true;
  670. mac->bw_80 = false;
  671. /*wide channel */
  672. wide_chan += 2;
  673. break;
  674. default:
  675. mac->bw_40 = false;
  676. mac->bw_80 = false;
  677. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  678. "switch case not processed\n");
  679. break;
  680. }
  681. }
  682. if (wide_chan <= 0)
  683. wide_chan = 1;
  684. /* In scanning, when before we offchannel we may send a ps=1
  685. * null to AP, and then we may send a ps = 0 null to AP quickly,
  686. * but first null may have caused AP to put lots of packet to
  687. * hw tx buffer. These packets must be tx'd before we go off
  688. * channel so we must delay more time to let AP flush these
  689. * packets before going offchannel, or dis-association or
  690. * delete BA will be caused by AP
  691. */
  692. if (rtlpriv->mac80211.offchan_delay) {
  693. rtlpriv->mac80211.offchan_delay = false;
  694. mdelay(50);
  695. }
  696. rtlphy->current_channel = wide_chan;
  697. rtlpriv->cfg->ops->switch_channel(hw);
  698. rtlpriv->cfg->ops->set_channel_access(hw);
  699. rtlpriv->cfg->ops->set_bw_mode(hw, channel_type);
  700. }
  701. mutex_unlock(&rtlpriv->locks.conf_mutex);
  702. return 0;
  703. }
  704. static void rtl_op_configure_filter(struct ieee80211_hw *hw,
  705. unsigned int changed_flags,
  706. unsigned int *new_flags, u64 multicast)
  707. {
  708. bool update_rcr = false;
  709. struct rtl_priv *rtlpriv = rtl_priv(hw);
  710. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  711. *new_flags &= RTL_SUPPORTED_FILTERS;
  712. if (0 == changed_flags)
  713. return;
  714. /*TODO: we disable broadcase now, so enable here */
  715. if (changed_flags & FIF_ALLMULTI) {
  716. if (*new_flags & FIF_ALLMULTI) {
  717. mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AM] |
  718. rtlpriv->cfg->maps[MAC_RCR_AB];
  719. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  720. "Enable receive multicast frame\n");
  721. } else {
  722. mac->rx_conf &= ~(rtlpriv->cfg->maps[MAC_RCR_AM] |
  723. rtlpriv->cfg->maps[MAC_RCR_AB]);
  724. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  725. "Disable receive multicast frame\n");
  726. }
  727. update_rcr = true;
  728. }
  729. if (changed_flags & FIF_FCSFAIL) {
  730. if (*new_flags & FIF_FCSFAIL) {
  731. mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACRC32];
  732. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  733. "Enable receive FCS error frame\n");
  734. } else {
  735. mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACRC32];
  736. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  737. "Disable receive FCS error frame\n");
  738. }
  739. if (!update_rcr)
  740. update_rcr = true;
  741. }
  742. /* if ssid not set to hw don't check bssid
  743. * here just used for linked scanning, & linked
  744. * and nolink check bssid is set in set network_type
  745. */
  746. if ((changed_flags & FIF_BCN_PRBRESP_PROMISC) &&
  747. (mac->link_state >= MAC80211_LINKED)) {
  748. if (mac->opmode != NL80211_IFTYPE_AP &&
  749. mac->opmode != NL80211_IFTYPE_MESH_POINT) {
  750. if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
  751. rtlpriv->cfg->ops->set_chk_bssid(hw, false);
  752. else
  753. rtlpriv->cfg->ops->set_chk_bssid(hw, true);
  754. if (update_rcr)
  755. update_rcr = false;
  756. }
  757. }
  758. if (changed_flags & FIF_CONTROL) {
  759. if (*new_flags & FIF_CONTROL) {
  760. mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACF];
  761. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  762. "Enable receive control frame.\n");
  763. } else {
  764. mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACF];
  765. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  766. "Disable receive control frame.\n");
  767. }
  768. if (!update_rcr)
  769. update_rcr = true;
  770. }
  771. if (changed_flags & FIF_OTHER_BSS) {
  772. if (*new_flags & FIF_OTHER_BSS) {
  773. mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AAP];
  774. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  775. "Enable receive other BSS's frame.\n");
  776. } else {
  777. mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_AAP];
  778. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  779. "Disable receive other BSS's frame.\n");
  780. }
  781. if (!update_rcr)
  782. update_rcr = true;
  783. }
  784. if (update_rcr)
  785. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RCR,
  786. (u8 *)(&mac->rx_conf));
  787. }
  788. static int rtl_op_sta_add(struct ieee80211_hw *hw,
  789. struct ieee80211_vif *vif,
  790. struct ieee80211_sta *sta)
  791. {
  792. struct rtl_priv *rtlpriv = rtl_priv(hw);
  793. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  794. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  795. struct rtl_sta_info *sta_entry;
  796. if (sta) {
  797. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  798. spin_lock_bh(&rtlpriv->locks.entry_list_lock);
  799. list_add_tail(&sta_entry->list, &rtlpriv->entry_list);
  800. spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
  801. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  802. sta_entry->wireless_mode = WIRELESS_MODE_G;
  803. if (sta->supp_rates[0] <= 0xf)
  804. sta_entry->wireless_mode = WIRELESS_MODE_B;
  805. if (sta->ht_cap.ht_supported)
  806. sta_entry->wireless_mode = WIRELESS_MODE_N_24G;
  807. if (vif->type == NL80211_IFTYPE_ADHOC)
  808. sta_entry->wireless_mode = WIRELESS_MODE_G;
  809. } else if (rtlhal->current_bandtype == BAND_ON_5G) {
  810. sta_entry->wireless_mode = WIRELESS_MODE_A;
  811. if (sta->ht_cap.ht_supported)
  812. sta_entry->wireless_mode = WIRELESS_MODE_N_5G;
  813. if (sta->vht_cap.vht_supported)
  814. sta_entry->wireless_mode = WIRELESS_MODE_AC_5G;
  815. if (vif->type == NL80211_IFTYPE_ADHOC)
  816. sta_entry->wireless_mode = WIRELESS_MODE_A;
  817. }
  818. /*disable cck rate for p2p*/
  819. if (mac->p2p)
  820. sta->supp_rates[0] &= 0xfffffff0;
  821. memcpy(sta_entry->mac_addr, sta->addr, ETH_ALEN);
  822. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  823. "Add sta addr is %pM\n", sta->addr);
  824. rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
  825. }
  826. return 0;
  827. }
  828. static int rtl_op_sta_remove(struct ieee80211_hw *hw,
  829. struct ieee80211_vif *vif,
  830. struct ieee80211_sta *sta)
  831. {
  832. struct rtl_priv *rtlpriv = rtl_priv(hw);
  833. struct rtl_sta_info *sta_entry;
  834. if (sta) {
  835. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  836. "Remove sta addr is %pM\n", sta->addr);
  837. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  838. sta_entry->wireless_mode = 0;
  839. sta_entry->ratr_index = 0;
  840. spin_lock_bh(&rtlpriv->locks.entry_list_lock);
  841. list_del(&sta_entry->list);
  842. spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
  843. }
  844. return 0;
  845. }
  846. static int _rtl_get_hal_qnum(u16 queue)
  847. {
  848. int qnum;
  849. switch (queue) {
  850. case 0:
  851. qnum = AC3_VO;
  852. break;
  853. case 1:
  854. qnum = AC2_VI;
  855. break;
  856. case 2:
  857. qnum = AC0_BE;
  858. break;
  859. case 3:
  860. qnum = AC1_BK;
  861. break;
  862. default:
  863. qnum = AC0_BE;
  864. break;
  865. }
  866. return qnum;
  867. }
  868. /*
  869. *for mac80211 VO = 0, VI = 1, BE = 2, BK = 3
  870. *for rtl819x BE = 0, BK = 1, VI = 2, VO = 3
  871. */
  872. static int rtl_op_conf_tx(struct ieee80211_hw *hw,
  873. struct ieee80211_vif *vif, u16 queue,
  874. const struct ieee80211_tx_queue_params *param)
  875. {
  876. struct rtl_priv *rtlpriv = rtl_priv(hw);
  877. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  878. int aci;
  879. if (queue >= AC_MAX) {
  880. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  881. "queue number %d is incorrect!\n", queue);
  882. return -EINVAL;
  883. }
  884. aci = _rtl_get_hal_qnum(queue);
  885. mac->ac[aci].aifs = param->aifs;
  886. mac->ac[aci].cw_min = cpu_to_le16(param->cw_min);
  887. mac->ac[aci].cw_max = cpu_to_le16(param->cw_max);
  888. mac->ac[aci].tx_op = cpu_to_le16(param->txop);
  889. memcpy(&mac->edca_param[aci], param, sizeof(*param));
  890. rtlpriv->cfg->ops->set_qos(hw, aci);
  891. return 0;
  892. }
  893. static void send_beacon_frame(struct ieee80211_hw *hw,
  894. struct ieee80211_vif *vif)
  895. {
  896. struct rtl_priv *rtlpriv = rtl_priv(hw);
  897. struct sk_buff *skb = ieee80211_beacon_get(hw, vif);
  898. struct rtl_tcb_desc tcb_desc;
  899. if (skb) {
  900. memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
  901. rtlpriv->intf_ops->adapter_tx(hw, NULL, skb, &tcb_desc);
  902. }
  903. }
  904. static void rtl_op_bss_info_changed(struct ieee80211_hw *hw,
  905. struct ieee80211_vif *vif,
  906. struct ieee80211_bss_conf *bss_conf,
  907. u32 changed)
  908. {
  909. struct rtl_priv *rtlpriv = rtl_priv(hw);
  910. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  911. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  912. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  913. mutex_lock(&rtlpriv->locks.conf_mutex);
  914. if ((vif->type == NL80211_IFTYPE_ADHOC) ||
  915. (vif->type == NL80211_IFTYPE_AP) ||
  916. (vif->type == NL80211_IFTYPE_MESH_POINT)) {
  917. if ((changed & BSS_CHANGED_BEACON) ||
  918. (changed & BSS_CHANGED_BEACON_ENABLED &&
  919. bss_conf->enable_beacon)) {
  920. if (mac->beacon_enabled == 0) {
  921. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  922. "BSS_CHANGED_BEACON_ENABLED\n");
  923. /*start hw beacon interrupt. */
  924. /*rtlpriv->cfg->ops->set_bcn_reg(hw); */
  925. mac->beacon_enabled = 1;
  926. rtlpriv->cfg->ops->update_interrupt_mask(hw,
  927. rtlpriv->cfg->maps
  928. [RTL_IBSS_INT_MASKS], 0);
  929. if (rtlpriv->cfg->ops->linked_set_reg)
  930. rtlpriv->cfg->ops->linked_set_reg(hw);
  931. send_beacon_frame(hw, vif);
  932. }
  933. }
  934. if ((changed & BSS_CHANGED_BEACON_ENABLED &&
  935. !bss_conf->enable_beacon)) {
  936. if (mac->beacon_enabled == 1) {
  937. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  938. "ADHOC DISABLE BEACON\n");
  939. mac->beacon_enabled = 0;
  940. rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
  941. rtlpriv->cfg->maps
  942. [RTL_IBSS_INT_MASKS]);
  943. }
  944. }
  945. if (changed & BSS_CHANGED_BEACON_INT) {
  946. RT_TRACE(rtlpriv, COMP_BEACON, DBG_TRACE,
  947. "BSS_CHANGED_BEACON_INT\n");
  948. mac->beacon_interval = bss_conf->beacon_int;
  949. rtlpriv->cfg->ops->set_bcn_intv(hw);
  950. }
  951. }
  952. /*TODO: reference to enum ieee80211_bss_change */
  953. if (changed & BSS_CHANGED_ASSOC) {
  954. u8 mstatus;
  955. if (bss_conf->assoc) {
  956. struct ieee80211_sta *sta = NULL;
  957. u8 keep_alive = 10;
  958. mstatus = RT_MEDIA_CONNECT;
  959. /* we should reset all sec info & cam
  960. * before set cam after linked, we should not
  961. * reset in disassoc, that will cause tkip->wep
  962. * fail because some flag will be wrong */
  963. /* reset sec info */
  964. rtl_cam_reset_sec_info(hw);
  965. /* reset cam to fix wep fail issue
  966. * when change from wpa to wep */
  967. rtl_cam_reset_all_entry(hw);
  968. mac->link_state = MAC80211_LINKED;
  969. mac->cnt_after_linked = 0;
  970. mac->assoc_id = bss_conf->aid;
  971. memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
  972. if (rtlpriv->cfg->ops->linked_set_reg)
  973. rtlpriv->cfg->ops->linked_set_reg(hw);
  974. rcu_read_lock();
  975. sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
  976. if (!sta) {
  977. rcu_read_unlock();
  978. goto out;
  979. }
  980. RT_TRACE(rtlpriv, COMP_EASY_CONCURRENT, DBG_LOUD,
  981. "send PS STATIC frame\n");
  982. if (rtlpriv->dm.supp_phymode_switch) {
  983. if (sta->ht_cap.ht_supported)
  984. rtl_send_smps_action(hw, sta,
  985. IEEE80211_SMPS_STATIC);
  986. }
  987. if (rtlhal->current_bandtype == BAND_ON_5G) {
  988. mac->mode = WIRELESS_MODE_A;
  989. } else {
  990. if (sta->supp_rates[0] <= 0xf)
  991. mac->mode = WIRELESS_MODE_B;
  992. else
  993. mac->mode = WIRELESS_MODE_G;
  994. }
  995. if (sta->ht_cap.ht_supported) {
  996. if (rtlhal->current_bandtype == BAND_ON_2_4G)
  997. mac->mode = WIRELESS_MODE_N_24G;
  998. else
  999. mac->mode = WIRELESS_MODE_N_5G;
  1000. }
  1001. if (sta->vht_cap.vht_supported) {
  1002. if (rtlhal->current_bandtype == BAND_ON_5G)
  1003. mac->mode = WIRELESS_MODE_AC_5G;
  1004. else
  1005. mac->mode = WIRELESS_MODE_AC_24G;
  1006. }
  1007. if (vif->type == NL80211_IFTYPE_STATION && sta)
  1008. rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
  1009. rcu_read_unlock();
  1010. /* to avoid AP Disassociation caused by inactivity */
  1011. rtlpriv->cfg->ops->set_hw_reg(hw,
  1012. HW_VAR_KEEP_ALIVE,
  1013. (u8 *)(&keep_alive));
  1014. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  1015. "BSS_CHANGED_ASSOC\n");
  1016. } else {
  1017. mstatus = RT_MEDIA_DISCONNECT;
  1018. if (mac->link_state == MAC80211_LINKED) {
  1019. rtlpriv->enter_ps = false;
  1020. schedule_work(&rtlpriv->works.lps_change_work);
  1021. }
  1022. if (ppsc->p2p_ps_info.p2p_ps_mode > P2P_PS_NONE)
  1023. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  1024. mac->link_state = MAC80211_NOLINK;
  1025. eth_zero_addr(mac->bssid);
  1026. mac->vendor = PEER_UNKNOWN;
  1027. mac->mode = 0;
  1028. if (rtlpriv->dm.supp_phymode_switch) {
  1029. if (rtlpriv->cfg->ops->chk_switch_dmdp)
  1030. rtlpriv->cfg->ops->chk_switch_dmdp(hw);
  1031. }
  1032. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  1033. "BSS_CHANGED_UN_ASSOC\n");
  1034. }
  1035. rtlpriv->cfg->ops->set_network_type(hw, vif->type);
  1036. /* For FW LPS:
  1037. * To tell firmware we have connected or disconnected
  1038. */
  1039. rtlpriv->cfg->ops->set_hw_reg(hw,
  1040. HW_VAR_H2C_FW_JOINBSSRPT,
  1041. (u8 *)(&mstatus));
  1042. ppsc->report_linked = (mstatus == RT_MEDIA_CONNECT) ?
  1043. true : false;
  1044. if (rtlpriv->cfg->ops->get_btc_status())
  1045. rtlpriv->btcoexist.btc_ops->btc_mediastatus_notify(
  1046. rtlpriv, mstatus);
  1047. }
  1048. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1049. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1050. "BSS_CHANGED_ERP_CTS_PROT\n");
  1051. mac->use_cts_protect = bss_conf->use_cts_prot;
  1052. }
  1053. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1054. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  1055. "BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
  1056. bss_conf->use_short_preamble);
  1057. mac->short_preamble = bss_conf->use_short_preamble;
  1058. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
  1059. (u8 *)(&mac->short_preamble));
  1060. }
  1061. if (changed & BSS_CHANGED_ERP_SLOT) {
  1062. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1063. "BSS_CHANGED_ERP_SLOT\n");
  1064. if (bss_conf->use_short_slot)
  1065. mac->slot_time = RTL_SLOT_TIME_9;
  1066. else
  1067. mac->slot_time = RTL_SLOT_TIME_20;
  1068. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
  1069. (u8 *)(&mac->slot_time));
  1070. }
  1071. if (changed & BSS_CHANGED_HT) {
  1072. struct ieee80211_sta *sta = NULL;
  1073. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1074. "BSS_CHANGED_HT\n");
  1075. rcu_read_lock();
  1076. sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
  1077. if (sta) {
  1078. if (sta->ht_cap.ampdu_density >
  1079. mac->current_ampdu_density)
  1080. mac->current_ampdu_density =
  1081. sta->ht_cap.ampdu_density;
  1082. if (sta->ht_cap.ampdu_factor <
  1083. mac->current_ampdu_factor)
  1084. mac->current_ampdu_factor =
  1085. sta->ht_cap.ampdu_factor;
  1086. }
  1087. rcu_read_unlock();
  1088. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
  1089. (u8 *)(&mac->max_mss_density));
  1090. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
  1091. &mac->current_ampdu_factor);
  1092. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
  1093. &mac->current_ampdu_density);
  1094. }
  1095. if (changed & BSS_CHANGED_BSSID) {
  1096. u32 basic_rates;
  1097. struct ieee80211_sta *sta = NULL;
  1098. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
  1099. (u8 *)bss_conf->bssid);
  1100. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  1101. "bssid: %pM\n", bss_conf->bssid);
  1102. mac->vendor = PEER_UNKNOWN;
  1103. memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
  1104. rcu_read_lock();
  1105. sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
  1106. if (!sta) {
  1107. rcu_read_unlock();
  1108. goto out;
  1109. }
  1110. if (rtlhal->current_bandtype == BAND_ON_5G) {
  1111. mac->mode = WIRELESS_MODE_A;
  1112. } else {
  1113. if (sta->supp_rates[0] <= 0xf)
  1114. mac->mode = WIRELESS_MODE_B;
  1115. else
  1116. mac->mode = WIRELESS_MODE_G;
  1117. }
  1118. if (sta->ht_cap.ht_supported) {
  1119. if (rtlhal->current_bandtype == BAND_ON_2_4G)
  1120. mac->mode = WIRELESS_MODE_N_24G;
  1121. else
  1122. mac->mode = WIRELESS_MODE_N_5G;
  1123. }
  1124. if (sta->vht_cap.vht_supported) {
  1125. if (rtlhal->current_bandtype == BAND_ON_5G)
  1126. mac->mode = WIRELESS_MODE_AC_5G;
  1127. else
  1128. mac->mode = WIRELESS_MODE_AC_24G;
  1129. }
  1130. /* just station need it, because ibss & ap mode will
  1131. * set in sta_add, and will be NULL here */
  1132. if (vif->type == NL80211_IFTYPE_STATION) {
  1133. struct rtl_sta_info *sta_entry;
  1134. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1135. sta_entry->wireless_mode = mac->mode;
  1136. }
  1137. if (sta->ht_cap.ht_supported) {
  1138. mac->ht_enable = true;
  1139. /*
  1140. * for cisco 1252 bw20 it's wrong
  1141. * if (ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
  1142. * mac->bw_40 = true;
  1143. * }
  1144. * */
  1145. }
  1146. if (sta->vht_cap.vht_supported)
  1147. mac->vht_enable = true;
  1148. if (changed & BSS_CHANGED_BASIC_RATES) {
  1149. /* for 5G must << RATE_6M_INDEX = 4,
  1150. * because 5G have no cck rate*/
  1151. if (rtlhal->current_bandtype == BAND_ON_5G)
  1152. basic_rates = sta->supp_rates[1] << 4;
  1153. else
  1154. basic_rates = sta->supp_rates[0];
  1155. mac->basic_rates = basic_rates;
  1156. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
  1157. (u8 *)(&basic_rates));
  1158. }
  1159. rcu_read_unlock();
  1160. }
  1161. out:
  1162. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1163. }
  1164. static u64 rtl_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1165. {
  1166. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1167. u64 tsf;
  1168. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&tsf));
  1169. return tsf;
  1170. }
  1171. static void rtl_op_set_tsf(struct ieee80211_hw *hw,
  1172. struct ieee80211_vif *vif, u64 tsf)
  1173. {
  1174. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1175. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1176. u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
  1177. mac->tsf = tsf;
  1178. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&bibss));
  1179. }
  1180. static void rtl_op_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1181. {
  1182. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1183. u8 tmp = 0;
  1184. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, (u8 *)(&tmp));
  1185. }
  1186. static void rtl_op_sta_notify(struct ieee80211_hw *hw,
  1187. struct ieee80211_vif *vif,
  1188. enum sta_notify_cmd cmd,
  1189. struct ieee80211_sta *sta)
  1190. {
  1191. switch (cmd) {
  1192. case STA_NOTIFY_SLEEP:
  1193. break;
  1194. case STA_NOTIFY_AWAKE:
  1195. break;
  1196. default:
  1197. break;
  1198. }
  1199. }
  1200. static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
  1201. struct ieee80211_vif *vif,
  1202. enum ieee80211_ampdu_mlme_action action,
  1203. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  1204. u8 buf_size)
  1205. {
  1206. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1207. switch (action) {
  1208. case IEEE80211_AMPDU_TX_START:
  1209. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1210. "IEEE80211_AMPDU_TX_START: TID:%d\n", tid);
  1211. return rtl_tx_agg_start(hw, vif, sta, tid, ssn);
  1212. case IEEE80211_AMPDU_TX_STOP_CONT:
  1213. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  1214. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  1215. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1216. "IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
  1217. return rtl_tx_agg_stop(hw, vif, sta, tid);
  1218. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1219. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1220. "IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
  1221. rtl_tx_agg_oper(hw, sta, tid);
  1222. break;
  1223. case IEEE80211_AMPDU_RX_START:
  1224. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1225. "IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
  1226. return rtl_rx_agg_start(hw, sta, tid);
  1227. case IEEE80211_AMPDU_RX_STOP:
  1228. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1229. "IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
  1230. return rtl_rx_agg_stop(hw, sta, tid);
  1231. default:
  1232. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1233. "IEEE80211_AMPDU_ERR!!!!:\n");
  1234. return -EOPNOTSUPP;
  1235. }
  1236. return 0;
  1237. }
  1238. static void rtl_op_sw_scan_start(struct ieee80211_hw *hw,
  1239. struct ieee80211_vif *vif,
  1240. const u8 *mac_addr)
  1241. {
  1242. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1243. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1244. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
  1245. mac->act_scanning = true;
  1246. if (rtlpriv->link_info.higher_busytraffic) {
  1247. mac->skip_scan = true;
  1248. return;
  1249. }
  1250. if (rtlpriv->cfg->ops->get_btc_status())
  1251. rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 1);
  1252. if (rtlpriv->dm.supp_phymode_switch) {
  1253. if (rtlpriv->cfg->ops->chk_switch_dmdp)
  1254. rtlpriv->cfg->ops->chk_switch_dmdp(hw);
  1255. }
  1256. if (mac->link_state == MAC80211_LINKED) {
  1257. rtlpriv->enter_ps = false;
  1258. schedule_work(&rtlpriv->works.lps_change_work);
  1259. mac->link_state = MAC80211_LINKED_SCANNING;
  1260. } else {
  1261. rtl_ips_nic_on(hw);
  1262. }
  1263. /* Dul mac */
  1264. rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
  1265. rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
  1266. rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP_BAND0);
  1267. }
  1268. static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw,
  1269. struct ieee80211_vif *vif)
  1270. {
  1271. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1272. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1273. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
  1274. mac->act_scanning = false;
  1275. mac->skip_scan = false;
  1276. if (rtlpriv->link_info.higher_busytraffic)
  1277. return;
  1278. /* p2p will use 1/6/11 to scan */
  1279. if (mac->n_channels == 3)
  1280. mac->p2p_in_use = true;
  1281. else
  1282. mac->p2p_in_use = false;
  1283. mac->n_channels = 0;
  1284. /* Dul mac */
  1285. rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
  1286. if (mac->link_state == MAC80211_LINKED_SCANNING) {
  1287. mac->link_state = MAC80211_LINKED;
  1288. if (mac->opmode == NL80211_IFTYPE_STATION) {
  1289. /* fix fwlps issue */
  1290. rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
  1291. }
  1292. }
  1293. rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
  1294. if (rtlpriv->cfg->ops->get_btc_status())
  1295. rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 0);
  1296. }
  1297. static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  1298. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  1299. struct ieee80211_key_conf *key)
  1300. {
  1301. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1302. u8 key_type = NO_ENCRYPTION;
  1303. u8 key_idx;
  1304. bool group_key = false;
  1305. bool wep_only = false;
  1306. int err = 0;
  1307. u8 mac_addr[ETH_ALEN];
  1308. u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1309. if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
  1310. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  1311. "not open hw encryption\n");
  1312. return -ENOSPC; /*User disabled HW-crypto */
  1313. }
  1314. /* To support IBSS, use sw-crypto for GTK */
  1315. if (((vif->type == NL80211_IFTYPE_ADHOC) ||
  1316. (vif->type == NL80211_IFTYPE_MESH_POINT)) &&
  1317. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1318. return -ENOSPC;
  1319. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1320. "%s hardware based encryption for keyidx: %d, mac: %pM\n",
  1321. cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
  1322. sta ? sta->addr : bcast_addr);
  1323. rtlpriv->sec.being_setkey = true;
  1324. rtl_ips_nic_on(hw);
  1325. mutex_lock(&rtlpriv->locks.conf_mutex);
  1326. /* <1> get encryption alg */
  1327. switch (key->cipher) {
  1328. case WLAN_CIPHER_SUITE_WEP40:
  1329. key_type = WEP40_ENCRYPTION;
  1330. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP40\n");
  1331. break;
  1332. case WLAN_CIPHER_SUITE_WEP104:
  1333. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP104\n");
  1334. key_type = WEP104_ENCRYPTION;
  1335. break;
  1336. case WLAN_CIPHER_SUITE_TKIP:
  1337. key_type = TKIP_ENCRYPTION;
  1338. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:TKIP\n");
  1339. break;
  1340. case WLAN_CIPHER_SUITE_CCMP:
  1341. key_type = AESCCMP_ENCRYPTION;
  1342. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CCMP\n");
  1343. break;
  1344. case WLAN_CIPHER_SUITE_AES_CMAC:
  1345. /* HW don't support CMAC encryption,
  1346. * use software CMAC encryption
  1347. */
  1348. key_type = AESCMAC_ENCRYPTION;
  1349. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CMAC\n");
  1350. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1351. "HW don't support CMAC encrypiton, use software CMAC encrypiton\n");
  1352. err = -EOPNOTSUPP;
  1353. goto out_unlock;
  1354. default:
  1355. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1356. "alg_err:%x!!!!:\n", key->cipher);
  1357. goto out_unlock;
  1358. }
  1359. if (key_type == WEP40_ENCRYPTION ||
  1360. key_type == WEP104_ENCRYPTION ||
  1361. vif->type == NL80211_IFTYPE_ADHOC)
  1362. rtlpriv->sec.use_defaultkey = true;
  1363. /* <2> get key_idx */
  1364. key_idx = (u8) (key->keyidx);
  1365. if (key_idx > 3)
  1366. goto out_unlock;
  1367. /* <3> if pairwise key enable_hw_sec */
  1368. group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  1369. /* wep always be group key, but there are two conditions:
  1370. * 1) wep only: is just for wep enc, in this condition
  1371. * rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION
  1372. * will be true & enable_hw_sec will be set when wep
  1373. * ke setting.
  1374. * 2) wep(group) + AES(pairwise): some AP like cisco
  1375. * may use it, in this condition enable_hw_sec will not
  1376. * be set when wep key setting */
  1377. /* we must reset sec_info after lingked before set key,
  1378. * or some flag will be wrong*/
  1379. if (vif->type == NL80211_IFTYPE_AP ||
  1380. vif->type == NL80211_IFTYPE_MESH_POINT) {
  1381. if (!group_key || key_type == WEP40_ENCRYPTION ||
  1382. key_type == WEP104_ENCRYPTION) {
  1383. if (group_key)
  1384. wep_only = true;
  1385. rtlpriv->cfg->ops->enable_hw_sec(hw);
  1386. }
  1387. } else {
  1388. if ((!group_key) || (vif->type == NL80211_IFTYPE_ADHOC) ||
  1389. rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
  1390. if (rtlpriv->sec.pairwise_enc_algorithm ==
  1391. NO_ENCRYPTION &&
  1392. (key_type == WEP40_ENCRYPTION ||
  1393. key_type == WEP104_ENCRYPTION))
  1394. wep_only = true;
  1395. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  1396. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1397. "set enable_hw_sec, key_type:%x(OPEN:0 WEP40:1 TKIP:2 AES:4 WEP104:5)\n",
  1398. key_type);
  1399. rtlpriv->cfg->ops->enable_hw_sec(hw);
  1400. }
  1401. }
  1402. /* <4> set key based on cmd */
  1403. switch (cmd) {
  1404. case SET_KEY:
  1405. if (wep_only) {
  1406. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1407. "set WEP(group/pairwise) key\n");
  1408. /* Pairwise key with an assigned MAC address. */
  1409. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  1410. rtlpriv->sec.group_enc_algorithm = key_type;
  1411. /*set local buf about wep key. */
  1412. memcpy(rtlpriv->sec.key_buf[key_idx],
  1413. key->key, key->keylen);
  1414. rtlpriv->sec.key_len[key_idx] = key->keylen;
  1415. eth_zero_addr(mac_addr);
  1416. } else if (group_key) { /* group key */
  1417. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1418. "set group key\n");
  1419. /* group key */
  1420. rtlpriv->sec.group_enc_algorithm = key_type;
  1421. /*set local buf about group key. */
  1422. memcpy(rtlpriv->sec.key_buf[key_idx],
  1423. key->key, key->keylen);
  1424. rtlpriv->sec.key_len[key_idx] = key->keylen;
  1425. memcpy(mac_addr, bcast_addr, ETH_ALEN);
  1426. } else { /* pairwise key */
  1427. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1428. "set pairwise key\n");
  1429. if (!sta) {
  1430. RT_ASSERT(false,
  1431. "pairwise key without mac_addr\n");
  1432. err = -EOPNOTSUPP;
  1433. goto out_unlock;
  1434. }
  1435. /* Pairwise key with an assigned MAC address. */
  1436. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  1437. /*set local buf about pairwise key. */
  1438. memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
  1439. key->key, key->keylen);
  1440. rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
  1441. rtlpriv->sec.pairwise_key =
  1442. rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
  1443. memcpy(mac_addr, sta->addr, ETH_ALEN);
  1444. }
  1445. rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
  1446. group_key, key_type, wep_only,
  1447. false);
  1448. /* <5> tell mac80211 do something: */
  1449. /*must use sw generate IV, or can not work !!!!. */
  1450. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1451. key->hw_key_idx = key_idx;
  1452. if (key_type == TKIP_ENCRYPTION)
  1453. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1454. /*use software CCMP encryption for management frames (MFP) */
  1455. if (key_type == AESCCMP_ENCRYPTION)
  1456. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
  1457. break;
  1458. case DISABLE_KEY:
  1459. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1460. "disable key delete one entry\n");
  1461. /*set local buf about wep key. */
  1462. if (vif->type == NL80211_IFTYPE_AP ||
  1463. vif->type == NL80211_IFTYPE_MESH_POINT) {
  1464. if (sta)
  1465. rtl_cam_del_entry(hw, sta->addr);
  1466. }
  1467. memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
  1468. rtlpriv->sec.key_len[key_idx] = 0;
  1469. eth_zero_addr(mac_addr);
  1470. /*
  1471. *mac80211 will delete entrys one by one,
  1472. *so don't use rtl_cam_reset_all_entry
  1473. *or clear all entry here.
  1474. */
  1475. rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
  1476. break;
  1477. default:
  1478. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1479. "cmd_err:%x!!!!:\n", cmd);
  1480. }
  1481. out_unlock:
  1482. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1483. rtlpriv->sec.being_setkey = false;
  1484. return err;
  1485. }
  1486. static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
  1487. {
  1488. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1489. bool radio_state;
  1490. bool blocked;
  1491. u8 valid = 0;
  1492. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  1493. return;
  1494. mutex_lock(&rtlpriv->locks.conf_mutex);
  1495. /*if Radio On return true here */
  1496. radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
  1497. if (valid) {
  1498. if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
  1499. rtlpriv->rfkill.rfkill_state = radio_state;
  1500. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  1501. "wireless radio switch turned %s\n",
  1502. radio_state ? "on" : "off");
  1503. blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
  1504. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  1505. }
  1506. }
  1507. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1508. }
  1509. /* this function is called by mac80211 to flush tx buffer
  1510. * before switch channle or power save, or tx buffer packet
  1511. * maybe send after offchannel or rf sleep, this may cause
  1512. * dis-association by AP */
  1513. static void rtl_op_flush(struct ieee80211_hw *hw,
  1514. struct ieee80211_vif *vif,
  1515. u32 queues,
  1516. bool drop)
  1517. {
  1518. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1519. if (rtlpriv->intf_ops->flush)
  1520. rtlpriv->intf_ops->flush(hw, queues, drop);
  1521. }
  1522. /* Description:
  1523. * This routine deals with the Power Configuration CMD
  1524. * parsing for RTL8723/RTL8188E Series IC.
  1525. * Assumption:
  1526. * We should follow specific format that was released from HW SD.
  1527. */
  1528. bool rtl_hal_pwrseqcmdparsing(struct rtl_priv *rtlpriv, u8 cut_version,
  1529. u8 faversion, u8 interface_type,
  1530. struct wlan_pwr_cfg pwrcfgcmd[])
  1531. {
  1532. struct wlan_pwr_cfg cfg_cmd = {0};
  1533. bool polling_bit = false;
  1534. u32 ary_idx = 0;
  1535. u8 value = 0;
  1536. u32 offset = 0;
  1537. u32 polling_count = 0;
  1538. u32 max_polling_cnt = 5000;
  1539. do {
  1540. cfg_cmd = pwrcfgcmd[ary_idx];
  1541. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1542. "rtl_hal_pwrseqcmdparsing(): offset(%#x),cut_msk(%#x), famsk(%#x), interface_msk(%#x), base(%#x), cmd(%#x), msk(%#x), value(%#x)\n",
  1543. GET_PWR_CFG_OFFSET(cfg_cmd),
  1544. GET_PWR_CFG_CUT_MASK(cfg_cmd),
  1545. GET_PWR_CFG_FAB_MASK(cfg_cmd),
  1546. GET_PWR_CFG_INTF_MASK(cfg_cmd),
  1547. GET_PWR_CFG_BASE(cfg_cmd), GET_PWR_CFG_CMD(cfg_cmd),
  1548. GET_PWR_CFG_MASK(cfg_cmd), GET_PWR_CFG_VALUE(cfg_cmd));
  1549. if ((GET_PWR_CFG_FAB_MASK(cfg_cmd)&faversion) &&
  1550. (GET_PWR_CFG_CUT_MASK(cfg_cmd)&cut_version) &&
  1551. (GET_PWR_CFG_INTF_MASK(cfg_cmd)&interface_type)) {
  1552. switch (GET_PWR_CFG_CMD(cfg_cmd)) {
  1553. case PWR_CMD_READ:
  1554. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1555. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_READ\n");
  1556. break;
  1557. case PWR_CMD_WRITE:
  1558. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1559. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_WRITE\n");
  1560. offset = GET_PWR_CFG_OFFSET(cfg_cmd);
  1561. /*Read the value from system register*/
  1562. value = rtl_read_byte(rtlpriv, offset);
  1563. value &= (~(GET_PWR_CFG_MASK(cfg_cmd)));
  1564. value |= (GET_PWR_CFG_VALUE(cfg_cmd) &
  1565. GET_PWR_CFG_MASK(cfg_cmd));
  1566. /*Write the value back to sytem register*/
  1567. rtl_write_byte(rtlpriv, offset, value);
  1568. break;
  1569. case PWR_CMD_POLLING:
  1570. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1571. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_POLLING\n");
  1572. polling_bit = false;
  1573. offset = GET_PWR_CFG_OFFSET(cfg_cmd);
  1574. do {
  1575. value = rtl_read_byte(rtlpriv, offset);
  1576. value &= GET_PWR_CFG_MASK(cfg_cmd);
  1577. if (value ==
  1578. (GET_PWR_CFG_VALUE(cfg_cmd) &
  1579. GET_PWR_CFG_MASK(cfg_cmd)))
  1580. polling_bit = true;
  1581. else
  1582. udelay(10);
  1583. if (polling_count++ > max_polling_cnt)
  1584. return false;
  1585. } while (!polling_bit);
  1586. break;
  1587. case PWR_CMD_DELAY:
  1588. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1589. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_DELAY\n");
  1590. if (GET_PWR_CFG_VALUE(cfg_cmd) ==
  1591. PWRSEQ_DELAY_US)
  1592. udelay(GET_PWR_CFG_OFFSET(cfg_cmd));
  1593. else
  1594. mdelay(GET_PWR_CFG_OFFSET(cfg_cmd));
  1595. break;
  1596. case PWR_CMD_END:
  1597. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1598. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_END\n");
  1599. return true;
  1600. default:
  1601. RT_ASSERT(false,
  1602. "rtl_hal_pwrseqcmdparsing(): Unknown CMD!!\n");
  1603. break;
  1604. }
  1605. }
  1606. ary_idx++;
  1607. } while (1);
  1608. return true;
  1609. }
  1610. EXPORT_SYMBOL(rtl_hal_pwrseqcmdparsing);
  1611. bool rtl_cmd_send_packet(struct ieee80211_hw *hw, struct sk_buff *skb)
  1612. {
  1613. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1614. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1615. struct rtl8192_tx_ring *ring;
  1616. struct rtl_tx_desc *pdesc;
  1617. unsigned long flags;
  1618. struct sk_buff *pskb = NULL;
  1619. ring = &rtlpci->tx_ring[BEACON_QUEUE];
  1620. spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
  1621. pskb = __skb_dequeue(&ring->queue);
  1622. if (pskb)
  1623. kfree_skb(pskb);
  1624. /*this is wrong, fill_tx_cmddesc needs update*/
  1625. pdesc = &ring->desc[0];
  1626. rtlpriv->cfg->ops->fill_tx_cmddesc(hw, (u8 *)pdesc, 1, 1, skb);
  1627. __skb_queue_tail(&ring->queue, skb);
  1628. spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
  1629. rtlpriv->cfg->ops->tx_polling(hw, BEACON_QUEUE);
  1630. return true;
  1631. }
  1632. EXPORT_SYMBOL(rtl_cmd_send_packet);
  1633. const struct ieee80211_ops rtl_ops = {
  1634. .start = rtl_op_start,
  1635. .stop = rtl_op_stop,
  1636. .tx = rtl_op_tx,
  1637. .add_interface = rtl_op_add_interface,
  1638. .remove_interface = rtl_op_remove_interface,
  1639. .change_interface = rtl_op_change_interface,
  1640. #ifdef CONFIG_PM
  1641. .suspend = rtl_op_suspend,
  1642. .resume = rtl_op_resume,
  1643. #endif
  1644. .config = rtl_op_config,
  1645. .configure_filter = rtl_op_configure_filter,
  1646. .set_key = rtl_op_set_key,
  1647. .conf_tx = rtl_op_conf_tx,
  1648. .bss_info_changed = rtl_op_bss_info_changed,
  1649. .get_tsf = rtl_op_get_tsf,
  1650. .set_tsf = rtl_op_set_tsf,
  1651. .reset_tsf = rtl_op_reset_tsf,
  1652. .sta_notify = rtl_op_sta_notify,
  1653. .ampdu_action = rtl_op_ampdu_action,
  1654. .sw_scan_start = rtl_op_sw_scan_start,
  1655. .sw_scan_complete = rtl_op_sw_scan_complete,
  1656. .rfkill_poll = rtl_op_rfkill_poll,
  1657. .sta_add = rtl_op_sta_add,
  1658. .sta_remove = rtl_op_sta_remove,
  1659. .flush = rtl_op_flush,
  1660. };
  1661. EXPORT_SYMBOL_GPL(rtl_ops);
  1662. bool rtl_btc_status_false(void)
  1663. {
  1664. return false;
  1665. }
  1666. EXPORT_SYMBOL_GPL(rtl_btc_status_false);
  1667. void rtl_dm_diginit(struct ieee80211_hw *hw, u32 cur_igvalue)
  1668. {
  1669. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1670. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  1671. dm_digtable->dig_enable_flag = true;
  1672. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
  1673. dm_digtable->cur_igvalue = cur_igvalue;
  1674. dm_digtable->pre_igvalue = 0;
  1675. dm_digtable->cur_sta_cstate = DIG_STA_DISCONNECT;
  1676. dm_digtable->presta_cstate = DIG_STA_DISCONNECT;
  1677. dm_digtable->curmultista_cstate = DIG_MULTISTA_DISCONNECT;
  1678. dm_digtable->rssi_lowthresh = DM_DIG_THRESH_LOW;
  1679. dm_digtable->rssi_highthresh = DM_DIG_THRESH_HIGH;
  1680. dm_digtable->fa_lowthresh = DM_FALSEALARM_THRESH_LOW;
  1681. dm_digtable->fa_highthresh = DM_FALSEALARM_THRESH_HIGH;
  1682. dm_digtable->rx_gain_max = DM_DIG_MAX;
  1683. dm_digtable->rx_gain_min = DM_DIG_MIN;
  1684. dm_digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
  1685. dm_digtable->back_range_max = DM_DIG_BACKOFF_MAX;
  1686. dm_digtable->back_range_min = DM_DIG_BACKOFF_MIN;
  1687. dm_digtable->pre_cck_cca_thres = 0xff;
  1688. dm_digtable->cur_cck_cca_thres = 0x83;
  1689. dm_digtable->forbidden_igi = DM_DIG_MIN;
  1690. dm_digtable->large_fa_hit = 0;
  1691. dm_digtable->recover_cnt = 0;
  1692. dm_digtable->dig_min_0 = 0x25;
  1693. dm_digtable->dig_min_1 = 0x25;
  1694. dm_digtable->media_connect_0 = false;
  1695. dm_digtable->media_connect_1 = false;
  1696. rtlpriv->dm.dm_initialgain_enable = true;
  1697. dm_digtable->bt30_cur_igi = 0x32;
  1698. dm_digtable->pre_cck_pd_state = CCK_PD_STAGE_MAX;
  1699. dm_digtable->cur_cck_pd_state = CCK_PD_STAGE_LOWRSSI;
  1700. }
  1701. EXPORT_SYMBOL(rtl_dm_diginit);