core.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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. memset(mac->bssid, 0, 6);
  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. memset(mac->bssid, 0, ETH_ALEN);
  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. if (skb)
  899. rtlpriv->intf_ops->adapter_tx(hw, NULL, skb, NULL);
  900. }
  901. static void rtl_op_bss_info_changed(struct ieee80211_hw *hw,
  902. struct ieee80211_vif *vif,
  903. struct ieee80211_bss_conf *bss_conf,
  904. u32 changed)
  905. {
  906. struct rtl_priv *rtlpriv = rtl_priv(hw);
  907. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  908. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  909. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  910. mutex_lock(&rtlpriv->locks.conf_mutex);
  911. if ((vif->type == NL80211_IFTYPE_ADHOC) ||
  912. (vif->type == NL80211_IFTYPE_AP) ||
  913. (vif->type == NL80211_IFTYPE_MESH_POINT)) {
  914. if ((changed & BSS_CHANGED_BEACON) ||
  915. (changed & BSS_CHANGED_BEACON_ENABLED &&
  916. bss_conf->enable_beacon)) {
  917. if (mac->beacon_enabled == 0) {
  918. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  919. "BSS_CHANGED_BEACON_ENABLED\n");
  920. /*start hw beacon interrupt. */
  921. /*rtlpriv->cfg->ops->set_bcn_reg(hw); */
  922. mac->beacon_enabled = 1;
  923. rtlpriv->cfg->ops->update_interrupt_mask(hw,
  924. rtlpriv->cfg->maps
  925. [RTL_IBSS_INT_MASKS], 0);
  926. if (rtlpriv->cfg->ops->linked_set_reg)
  927. rtlpriv->cfg->ops->linked_set_reg(hw);
  928. send_beacon_frame(hw, vif);
  929. }
  930. }
  931. if ((changed & BSS_CHANGED_BEACON_ENABLED &&
  932. !bss_conf->enable_beacon)) {
  933. if (mac->beacon_enabled == 1) {
  934. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  935. "ADHOC DISABLE BEACON\n");
  936. mac->beacon_enabled = 0;
  937. rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
  938. rtlpriv->cfg->maps
  939. [RTL_IBSS_INT_MASKS]);
  940. }
  941. }
  942. if (changed & BSS_CHANGED_BEACON_INT) {
  943. RT_TRACE(rtlpriv, COMP_BEACON, DBG_TRACE,
  944. "BSS_CHANGED_BEACON_INT\n");
  945. mac->beacon_interval = bss_conf->beacon_int;
  946. rtlpriv->cfg->ops->set_bcn_intv(hw);
  947. }
  948. }
  949. /*TODO: reference to enum ieee80211_bss_change */
  950. if (changed & BSS_CHANGED_ASSOC) {
  951. u8 mstatus;
  952. if (bss_conf->assoc) {
  953. struct ieee80211_sta *sta = NULL;
  954. u8 keep_alive = 10;
  955. mstatus = RT_MEDIA_CONNECT;
  956. /* we should reset all sec info & cam
  957. * before set cam after linked, we should not
  958. * reset in disassoc, that will cause tkip->wep
  959. * fail because some flag will be wrong */
  960. /* reset sec info */
  961. rtl_cam_reset_sec_info(hw);
  962. /* reset cam to fix wep fail issue
  963. * when change from wpa to wep */
  964. rtl_cam_reset_all_entry(hw);
  965. mac->link_state = MAC80211_LINKED;
  966. mac->cnt_after_linked = 0;
  967. mac->assoc_id = bss_conf->aid;
  968. memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
  969. if (rtlpriv->cfg->ops->linked_set_reg)
  970. rtlpriv->cfg->ops->linked_set_reg(hw);
  971. rcu_read_lock();
  972. sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
  973. if (!sta) {
  974. rcu_read_unlock();
  975. goto out;
  976. }
  977. RT_TRACE(rtlpriv, COMP_EASY_CONCURRENT, DBG_LOUD,
  978. "send PS STATIC frame\n");
  979. if (rtlpriv->dm.supp_phymode_switch) {
  980. if (sta->ht_cap.ht_supported)
  981. rtl_send_smps_action(hw, sta,
  982. IEEE80211_SMPS_STATIC);
  983. }
  984. if (rtlhal->current_bandtype == BAND_ON_5G) {
  985. mac->mode = WIRELESS_MODE_A;
  986. } else {
  987. if (sta->supp_rates[0] <= 0xf)
  988. mac->mode = WIRELESS_MODE_B;
  989. else
  990. mac->mode = WIRELESS_MODE_G;
  991. }
  992. if (sta->ht_cap.ht_supported) {
  993. if (rtlhal->current_bandtype == BAND_ON_2_4G)
  994. mac->mode = WIRELESS_MODE_N_24G;
  995. else
  996. mac->mode = WIRELESS_MODE_N_5G;
  997. }
  998. if (sta->vht_cap.vht_supported) {
  999. if (rtlhal->current_bandtype == BAND_ON_5G)
  1000. mac->mode = WIRELESS_MODE_AC_5G;
  1001. else
  1002. mac->mode = WIRELESS_MODE_AC_24G;
  1003. }
  1004. if (vif->type == NL80211_IFTYPE_STATION && sta)
  1005. rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
  1006. rcu_read_unlock();
  1007. /* to avoid AP Disassociation caused by inactivity */
  1008. rtlpriv->cfg->ops->set_hw_reg(hw,
  1009. HW_VAR_KEEP_ALIVE,
  1010. (u8 *)(&keep_alive));
  1011. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  1012. "BSS_CHANGED_ASSOC\n");
  1013. } else {
  1014. mstatus = RT_MEDIA_DISCONNECT;
  1015. if (mac->link_state == MAC80211_LINKED) {
  1016. rtlpriv->enter_ps = false;
  1017. schedule_work(&rtlpriv->works.lps_change_work);
  1018. }
  1019. if (ppsc->p2p_ps_info.p2p_ps_mode > P2P_PS_NONE)
  1020. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  1021. mac->link_state = MAC80211_NOLINK;
  1022. memset(mac->bssid, 0, ETH_ALEN);
  1023. mac->vendor = PEER_UNKNOWN;
  1024. mac->mode = 0;
  1025. if (rtlpriv->dm.supp_phymode_switch) {
  1026. if (rtlpriv->cfg->ops->chk_switch_dmdp)
  1027. rtlpriv->cfg->ops->chk_switch_dmdp(hw);
  1028. }
  1029. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  1030. "BSS_CHANGED_UN_ASSOC\n");
  1031. }
  1032. rtlpriv->cfg->ops->set_network_type(hw, vif->type);
  1033. /* For FW LPS:
  1034. * To tell firmware we have connected or disconnected
  1035. */
  1036. rtlpriv->cfg->ops->set_hw_reg(hw,
  1037. HW_VAR_H2C_FW_JOINBSSRPT,
  1038. (u8 *)(&mstatus));
  1039. ppsc->report_linked = (mstatus == RT_MEDIA_CONNECT) ?
  1040. true : false;
  1041. if (rtlpriv->cfg->ops->get_btc_status())
  1042. rtlpriv->btcoexist.btc_ops->btc_mediastatus_notify(
  1043. rtlpriv, mstatus);
  1044. }
  1045. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1046. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1047. "BSS_CHANGED_ERP_CTS_PROT\n");
  1048. mac->use_cts_protect = bss_conf->use_cts_prot;
  1049. }
  1050. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1051. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  1052. "BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
  1053. bss_conf->use_short_preamble);
  1054. mac->short_preamble = bss_conf->use_short_preamble;
  1055. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
  1056. (u8 *)(&mac->short_preamble));
  1057. }
  1058. if (changed & BSS_CHANGED_ERP_SLOT) {
  1059. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1060. "BSS_CHANGED_ERP_SLOT\n");
  1061. if (bss_conf->use_short_slot)
  1062. mac->slot_time = RTL_SLOT_TIME_9;
  1063. else
  1064. mac->slot_time = RTL_SLOT_TIME_20;
  1065. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
  1066. (u8 *)(&mac->slot_time));
  1067. }
  1068. if (changed & BSS_CHANGED_HT) {
  1069. struct ieee80211_sta *sta = NULL;
  1070. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1071. "BSS_CHANGED_HT\n");
  1072. rcu_read_lock();
  1073. sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
  1074. if (sta) {
  1075. if (sta->ht_cap.ampdu_density >
  1076. mac->current_ampdu_density)
  1077. mac->current_ampdu_density =
  1078. sta->ht_cap.ampdu_density;
  1079. if (sta->ht_cap.ampdu_factor <
  1080. mac->current_ampdu_factor)
  1081. mac->current_ampdu_factor =
  1082. sta->ht_cap.ampdu_factor;
  1083. }
  1084. rcu_read_unlock();
  1085. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
  1086. (u8 *)(&mac->max_mss_density));
  1087. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
  1088. &mac->current_ampdu_factor);
  1089. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
  1090. &mac->current_ampdu_density);
  1091. }
  1092. if (changed & BSS_CHANGED_BSSID) {
  1093. u32 basic_rates;
  1094. struct ieee80211_sta *sta = NULL;
  1095. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
  1096. (u8 *)bss_conf->bssid);
  1097. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  1098. "bssid: %pM\n", bss_conf->bssid);
  1099. mac->vendor = PEER_UNKNOWN;
  1100. memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
  1101. rcu_read_lock();
  1102. sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
  1103. if (!sta) {
  1104. rcu_read_unlock();
  1105. goto out;
  1106. }
  1107. if (rtlhal->current_bandtype == BAND_ON_5G) {
  1108. mac->mode = WIRELESS_MODE_A;
  1109. } else {
  1110. if (sta->supp_rates[0] <= 0xf)
  1111. mac->mode = WIRELESS_MODE_B;
  1112. else
  1113. mac->mode = WIRELESS_MODE_G;
  1114. }
  1115. if (sta->ht_cap.ht_supported) {
  1116. if (rtlhal->current_bandtype == BAND_ON_2_4G)
  1117. mac->mode = WIRELESS_MODE_N_24G;
  1118. else
  1119. mac->mode = WIRELESS_MODE_N_5G;
  1120. }
  1121. if (sta->vht_cap.vht_supported) {
  1122. if (rtlhal->current_bandtype == BAND_ON_5G)
  1123. mac->mode = WIRELESS_MODE_AC_5G;
  1124. else
  1125. mac->mode = WIRELESS_MODE_AC_24G;
  1126. }
  1127. /* just station need it, because ibss & ap mode will
  1128. * set in sta_add, and will be NULL here */
  1129. if (vif->type == NL80211_IFTYPE_STATION) {
  1130. struct rtl_sta_info *sta_entry;
  1131. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1132. sta_entry->wireless_mode = mac->mode;
  1133. }
  1134. if (sta->ht_cap.ht_supported) {
  1135. mac->ht_enable = true;
  1136. /*
  1137. * for cisco 1252 bw20 it's wrong
  1138. * if (ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
  1139. * mac->bw_40 = true;
  1140. * }
  1141. * */
  1142. }
  1143. if (sta->vht_cap.vht_supported)
  1144. mac->vht_enable = true;
  1145. if (changed & BSS_CHANGED_BASIC_RATES) {
  1146. /* for 5G must << RATE_6M_INDEX = 4,
  1147. * because 5G have no cck rate*/
  1148. if (rtlhal->current_bandtype == BAND_ON_5G)
  1149. basic_rates = sta->supp_rates[1] << 4;
  1150. else
  1151. basic_rates = sta->supp_rates[0];
  1152. mac->basic_rates = basic_rates;
  1153. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
  1154. (u8 *)(&basic_rates));
  1155. }
  1156. rcu_read_unlock();
  1157. }
  1158. out:
  1159. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1160. }
  1161. static u64 rtl_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1162. {
  1163. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1164. u64 tsf;
  1165. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&tsf));
  1166. return tsf;
  1167. }
  1168. static void rtl_op_set_tsf(struct ieee80211_hw *hw,
  1169. struct ieee80211_vif *vif, u64 tsf)
  1170. {
  1171. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1172. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1173. u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
  1174. mac->tsf = tsf;
  1175. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&bibss));
  1176. }
  1177. static void rtl_op_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1178. {
  1179. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1180. u8 tmp = 0;
  1181. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, (u8 *)(&tmp));
  1182. }
  1183. static void rtl_op_sta_notify(struct ieee80211_hw *hw,
  1184. struct ieee80211_vif *vif,
  1185. enum sta_notify_cmd cmd,
  1186. struct ieee80211_sta *sta)
  1187. {
  1188. switch (cmd) {
  1189. case STA_NOTIFY_SLEEP:
  1190. break;
  1191. case STA_NOTIFY_AWAKE:
  1192. break;
  1193. default:
  1194. break;
  1195. }
  1196. }
  1197. static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
  1198. struct ieee80211_vif *vif,
  1199. enum ieee80211_ampdu_mlme_action action,
  1200. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  1201. u8 buf_size)
  1202. {
  1203. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1204. switch (action) {
  1205. case IEEE80211_AMPDU_TX_START:
  1206. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1207. "IEEE80211_AMPDU_TX_START: TID:%d\n", tid);
  1208. return rtl_tx_agg_start(hw, vif, sta, tid, ssn);
  1209. case IEEE80211_AMPDU_TX_STOP_CONT:
  1210. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  1211. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  1212. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1213. "IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
  1214. return rtl_tx_agg_stop(hw, vif, sta, tid);
  1215. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1216. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1217. "IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
  1218. rtl_tx_agg_oper(hw, sta, tid);
  1219. break;
  1220. case IEEE80211_AMPDU_RX_START:
  1221. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1222. "IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
  1223. return rtl_rx_agg_start(hw, sta, tid);
  1224. case IEEE80211_AMPDU_RX_STOP:
  1225. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1226. "IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
  1227. return rtl_rx_agg_stop(hw, sta, tid);
  1228. default:
  1229. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1230. "IEEE80211_AMPDU_ERR!!!!:\n");
  1231. return -EOPNOTSUPP;
  1232. }
  1233. return 0;
  1234. }
  1235. static void rtl_op_sw_scan_start(struct ieee80211_hw *hw,
  1236. struct ieee80211_vif *vif,
  1237. const u8 *mac_addr)
  1238. {
  1239. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1240. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1241. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
  1242. mac->act_scanning = true;
  1243. if (rtlpriv->link_info.higher_busytraffic) {
  1244. mac->skip_scan = true;
  1245. return;
  1246. }
  1247. if (rtlpriv->cfg->ops->get_btc_status())
  1248. rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 1);
  1249. if (rtlpriv->dm.supp_phymode_switch) {
  1250. if (rtlpriv->cfg->ops->chk_switch_dmdp)
  1251. rtlpriv->cfg->ops->chk_switch_dmdp(hw);
  1252. }
  1253. if (mac->link_state == MAC80211_LINKED) {
  1254. rtlpriv->enter_ps = false;
  1255. schedule_work(&rtlpriv->works.lps_change_work);
  1256. mac->link_state = MAC80211_LINKED_SCANNING;
  1257. } else {
  1258. rtl_ips_nic_on(hw);
  1259. }
  1260. /* Dul mac */
  1261. rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
  1262. rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
  1263. rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP_BAND0);
  1264. }
  1265. static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw,
  1266. struct ieee80211_vif *vif)
  1267. {
  1268. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1269. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1270. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
  1271. mac->act_scanning = false;
  1272. mac->skip_scan = false;
  1273. if (rtlpriv->link_info.higher_busytraffic)
  1274. return;
  1275. /* p2p will use 1/6/11 to scan */
  1276. if (mac->n_channels == 3)
  1277. mac->p2p_in_use = true;
  1278. else
  1279. mac->p2p_in_use = false;
  1280. mac->n_channels = 0;
  1281. /* Dul mac */
  1282. rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
  1283. if (mac->link_state == MAC80211_LINKED_SCANNING) {
  1284. mac->link_state = MAC80211_LINKED;
  1285. if (mac->opmode == NL80211_IFTYPE_STATION) {
  1286. /* fix fwlps issue */
  1287. rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
  1288. }
  1289. }
  1290. rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
  1291. if (rtlpriv->cfg->ops->get_btc_status())
  1292. rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 0);
  1293. }
  1294. static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  1295. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  1296. struct ieee80211_key_conf *key)
  1297. {
  1298. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1299. u8 key_type = NO_ENCRYPTION;
  1300. u8 key_idx;
  1301. bool group_key = false;
  1302. bool wep_only = false;
  1303. int err = 0;
  1304. u8 mac_addr[ETH_ALEN];
  1305. u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1306. if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
  1307. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  1308. "not open hw encryption\n");
  1309. return -ENOSPC; /*User disabled HW-crypto */
  1310. }
  1311. /* To support IBSS, use sw-crypto for GTK */
  1312. if (((vif->type == NL80211_IFTYPE_ADHOC) ||
  1313. (vif->type == NL80211_IFTYPE_MESH_POINT)) &&
  1314. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1315. return -ENOSPC;
  1316. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1317. "%s hardware based encryption for keyidx: %d, mac: %pM\n",
  1318. cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
  1319. sta ? sta->addr : bcast_addr);
  1320. rtlpriv->sec.being_setkey = true;
  1321. rtl_ips_nic_on(hw);
  1322. mutex_lock(&rtlpriv->locks.conf_mutex);
  1323. /* <1> get encryption alg */
  1324. switch (key->cipher) {
  1325. case WLAN_CIPHER_SUITE_WEP40:
  1326. key_type = WEP40_ENCRYPTION;
  1327. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP40\n");
  1328. break;
  1329. case WLAN_CIPHER_SUITE_WEP104:
  1330. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP104\n");
  1331. key_type = WEP104_ENCRYPTION;
  1332. break;
  1333. case WLAN_CIPHER_SUITE_TKIP:
  1334. key_type = TKIP_ENCRYPTION;
  1335. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:TKIP\n");
  1336. break;
  1337. case WLAN_CIPHER_SUITE_CCMP:
  1338. key_type = AESCCMP_ENCRYPTION;
  1339. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CCMP\n");
  1340. break;
  1341. case WLAN_CIPHER_SUITE_AES_CMAC:
  1342. /* HW don't support CMAC encryption,
  1343. * use software CMAC encryption
  1344. */
  1345. key_type = AESCMAC_ENCRYPTION;
  1346. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CMAC\n");
  1347. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1348. "HW don't support CMAC encrypiton, use software CMAC encrypiton\n");
  1349. err = -EOPNOTSUPP;
  1350. goto out_unlock;
  1351. default:
  1352. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1353. "alg_err:%x!!!!:\n", key->cipher);
  1354. goto out_unlock;
  1355. }
  1356. if (key_type == WEP40_ENCRYPTION ||
  1357. key_type == WEP104_ENCRYPTION ||
  1358. vif->type == NL80211_IFTYPE_ADHOC)
  1359. rtlpriv->sec.use_defaultkey = true;
  1360. /* <2> get key_idx */
  1361. key_idx = (u8) (key->keyidx);
  1362. if (key_idx > 3)
  1363. goto out_unlock;
  1364. /* <3> if pairwise key enable_hw_sec */
  1365. group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  1366. /* wep always be group key, but there are two conditions:
  1367. * 1) wep only: is just for wep enc, in this condition
  1368. * rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION
  1369. * will be true & enable_hw_sec will be set when wep
  1370. * ke setting.
  1371. * 2) wep(group) + AES(pairwise): some AP like cisco
  1372. * may use it, in this condition enable_hw_sec will not
  1373. * be set when wep key setting */
  1374. /* we must reset sec_info after lingked before set key,
  1375. * or some flag will be wrong*/
  1376. if (vif->type == NL80211_IFTYPE_AP ||
  1377. vif->type == NL80211_IFTYPE_MESH_POINT) {
  1378. if (!group_key || key_type == WEP40_ENCRYPTION ||
  1379. key_type == WEP104_ENCRYPTION) {
  1380. if (group_key)
  1381. wep_only = true;
  1382. rtlpriv->cfg->ops->enable_hw_sec(hw);
  1383. }
  1384. } else {
  1385. if ((!group_key) || (vif->type == NL80211_IFTYPE_ADHOC) ||
  1386. rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
  1387. if (rtlpriv->sec.pairwise_enc_algorithm ==
  1388. NO_ENCRYPTION &&
  1389. (key_type == WEP40_ENCRYPTION ||
  1390. key_type == WEP104_ENCRYPTION))
  1391. wep_only = true;
  1392. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  1393. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1394. "set enable_hw_sec, key_type:%x(OPEN:0 WEP40:1 TKIP:2 AES:4 WEP104:5)\n",
  1395. key_type);
  1396. rtlpriv->cfg->ops->enable_hw_sec(hw);
  1397. }
  1398. }
  1399. /* <4> set key based on cmd */
  1400. switch (cmd) {
  1401. case SET_KEY:
  1402. if (wep_only) {
  1403. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1404. "set WEP(group/pairwise) key\n");
  1405. /* Pairwise key with an assigned MAC address. */
  1406. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  1407. rtlpriv->sec.group_enc_algorithm = key_type;
  1408. /*set local buf about wep key. */
  1409. memcpy(rtlpriv->sec.key_buf[key_idx],
  1410. key->key, key->keylen);
  1411. rtlpriv->sec.key_len[key_idx] = key->keylen;
  1412. eth_zero_addr(mac_addr);
  1413. } else if (group_key) { /* group key */
  1414. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1415. "set group key\n");
  1416. /* group key */
  1417. rtlpriv->sec.group_enc_algorithm = key_type;
  1418. /*set local buf about group key. */
  1419. memcpy(rtlpriv->sec.key_buf[key_idx],
  1420. key->key, key->keylen);
  1421. rtlpriv->sec.key_len[key_idx] = key->keylen;
  1422. memcpy(mac_addr, bcast_addr, ETH_ALEN);
  1423. } else { /* pairwise key */
  1424. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1425. "set pairwise key\n");
  1426. if (!sta) {
  1427. RT_ASSERT(false,
  1428. "pairwise key without mac_addr\n");
  1429. err = -EOPNOTSUPP;
  1430. goto out_unlock;
  1431. }
  1432. /* Pairwise key with an assigned MAC address. */
  1433. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  1434. /*set local buf about pairwise key. */
  1435. memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
  1436. key->key, key->keylen);
  1437. rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
  1438. rtlpriv->sec.pairwise_key =
  1439. rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
  1440. memcpy(mac_addr, sta->addr, ETH_ALEN);
  1441. }
  1442. rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
  1443. group_key, key_type, wep_only,
  1444. false);
  1445. /* <5> tell mac80211 do something: */
  1446. /*must use sw generate IV, or can not work !!!!. */
  1447. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1448. key->hw_key_idx = key_idx;
  1449. if (key_type == TKIP_ENCRYPTION)
  1450. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1451. /*use software CCMP encryption for management frames (MFP) */
  1452. if (key_type == AESCCMP_ENCRYPTION)
  1453. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
  1454. break;
  1455. case DISABLE_KEY:
  1456. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1457. "disable key delete one entry\n");
  1458. /*set local buf about wep key. */
  1459. if (vif->type == NL80211_IFTYPE_AP ||
  1460. vif->type == NL80211_IFTYPE_MESH_POINT) {
  1461. if (sta)
  1462. rtl_cam_del_entry(hw, sta->addr);
  1463. }
  1464. memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
  1465. rtlpriv->sec.key_len[key_idx] = 0;
  1466. eth_zero_addr(mac_addr);
  1467. /*
  1468. *mac80211 will delete entrys one by one,
  1469. *so don't use rtl_cam_reset_all_entry
  1470. *or clear all entry here.
  1471. */
  1472. rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
  1473. break;
  1474. default:
  1475. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  1476. "cmd_err:%x!!!!:\n", cmd);
  1477. }
  1478. out_unlock:
  1479. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1480. rtlpriv->sec.being_setkey = false;
  1481. return err;
  1482. }
  1483. static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
  1484. {
  1485. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1486. bool radio_state;
  1487. bool blocked;
  1488. u8 valid = 0;
  1489. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  1490. return;
  1491. mutex_lock(&rtlpriv->locks.conf_mutex);
  1492. /*if Radio On return true here */
  1493. radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
  1494. if (valid) {
  1495. if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
  1496. rtlpriv->rfkill.rfkill_state = radio_state;
  1497. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  1498. "wireless radio switch turned %s\n",
  1499. radio_state ? "on" : "off");
  1500. blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
  1501. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  1502. }
  1503. }
  1504. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1505. }
  1506. /* this function is called by mac80211 to flush tx buffer
  1507. * before switch channle or power save, or tx buffer packet
  1508. * maybe send after offchannel or rf sleep, this may cause
  1509. * dis-association by AP */
  1510. static void rtl_op_flush(struct ieee80211_hw *hw,
  1511. struct ieee80211_vif *vif,
  1512. u32 queues,
  1513. bool drop)
  1514. {
  1515. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1516. if (rtlpriv->intf_ops->flush)
  1517. rtlpriv->intf_ops->flush(hw, queues, drop);
  1518. }
  1519. /* Description:
  1520. * This routine deals with the Power Configuration CMD
  1521. * parsing for RTL8723/RTL8188E Series IC.
  1522. * Assumption:
  1523. * We should follow specific format that was released from HW SD.
  1524. */
  1525. bool rtl_hal_pwrseqcmdparsing(struct rtl_priv *rtlpriv, u8 cut_version,
  1526. u8 faversion, u8 interface_type,
  1527. struct wlan_pwr_cfg pwrcfgcmd[])
  1528. {
  1529. struct wlan_pwr_cfg cfg_cmd = {0};
  1530. bool polling_bit = false;
  1531. u32 ary_idx = 0;
  1532. u8 value = 0;
  1533. u32 offset = 0;
  1534. u32 polling_count = 0;
  1535. u32 max_polling_cnt = 5000;
  1536. do {
  1537. cfg_cmd = pwrcfgcmd[ary_idx];
  1538. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1539. "rtl_hal_pwrseqcmdparsing(): offset(%#x),cut_msk(%#x), famsk(%#x), interface_msk(%#x), base(%#x), cmd(%#x), msk(%#x), value(%#x)\n",
  1540. GET_PWR_CFG_OFFSET(cfg_cmd),
  1541. GET_PWR_CFG_CUT_MASK(cfg_cmd),
  1542. GET_PWR_CFG_FAB_MASK(cfg_cmd),
  1543. GET_PWR_CFG_INTF_MASK(cfg_cmd),
  1544. GET_PWR_CFG_BASE(cfg_cmd), GET_PWR_CFG_CMD(cfg_cmd),
  1545. GET_PWR_CFG_MASK(cfg_cmd), GET_PWR_CFG_VALUE(cfg_cmd));
  1546. if ((GET_PWR_CFG_FAB_MASK(cfg_cmd)&faversion) &&
  1547. (GET_PWR_CFG_CUT_MASK(cfg_cmd)&cut_version) &&
  1548. (GET_PWR_CFG_INTF_MASK(cfg_cmd)&interface_type)) {
  1549. switch (GET_PWR_CFG_CMD(cfg_cmd)) {
  1550. case PWR_CMD_READ:
  1551. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1552. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_READ\n");
  1553. break;
  1554. case PWR_CMD_WRITE:
  1555. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1556. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_WRITE\n");
  1557. offset = GET_PWR_CFG_OFFSET(cfg_cmd);
  1558. /*Read the value from system register*/
  1559. value = rtl_read_byte(rtlpriv, offset);
  1560. value &= (~(GET_PWR_CFG_MASK(cfg_cmd)));
  1561. value |= (GET_PWR_CFG_VALUE(cfg_cmd) &
  1562. GET_PWR_CFG_MASK(cfg_cmd));
  1563. /*Write the value back to sytem register*/
  1564. rtl_write_byte(rtlpriv, offset, value);
  1565. break;
  1566. case PWR_CMD_POLLING:
  1567. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1568. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_POLLING\n");
  1569. polling_bit = false;
  1570. offset = GET_PWR_CFG_OFFSET(cfg_cmd);
  1571. do {
  1572. value = rtl_read_byte(rtlpriv, offset);
  1573. value &= GET_PWR_CFG_MASK(cfg_cmd);
  1574. if (value ==
  1575. (GET_PWR_CFG_VALUE(cfg_cmd) &
  1576. GET_PWR_CFG_MASK(cfg_cmd)))
  1577. polling_bit = true;
  1578. else
  1579. udelay(10);
  1580. if (polling_count++ > max_polling_cnt)
  1581. return false;
  1582. } while (!polling_bit);
  1583. break;
  1584. case PWR_CMD_DELAY:
  1585. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1586. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_DELAY\n");
  1587. if (GET_PWR_CFG_VALUE(cfg_cmd) ==
  1588. PWRSEQ_DELAY_US)
  1589. udelay(GET_PWR_CFG_OFFSET(cfg_cmd));
  1590. else
  1591. mdelay(GET_PWR_CFG_OFFSET(cfg_cmd));
  1592. break;
  1593. case PWR_CMD_END:
  1594. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1595. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_END\n");
  1596. return true;
  1597. default:
  1598. RT_ASSERT(false,
  1599. "rtl_hal_pwrseqcmdparsing(): Unknown CMD!!\n");
  1600. break;
  1601. }
  1602. }
  1603. ary_idx++;
  1604. } while (1);
  1605. return true;
  1606. }
  1607. EXPORT_SYMBOL(rtl_hal_pwrseqcmdparsing);
  1608. bool rtl_cmd_send_packet(struct ieee80211_hw *hw, struct sk_buff *skb)
  1609. {
  1610. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1611. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1612. struct rtl8192_tx_ring *ring;
  1613. struct rtl_tx_desc *pdesc;
  1614. unsigned long flags;
  1615. struct sk_buff *pskb = NULL;
  1616. ring = &rtlpci->tx_ring[BEACON_QUEUE];
  1617. spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
  1618. pskb = __skb_dequeue(&ring->queue);
  1619. if (pskb)
  1620. kfree_skb(pskb);
  1621. /*this is wrong, fill_tx_cmddesc needs update*/
  1622. pdesc = &ring->desc[0];
  1623. rtlpriv->cfg->ops->fill_tx_cmddesc(hw, (u8 *)pdesc, 1, 1, skb);
  1624. __skb_queue_tail(&ring->queue, skb);
  1625. spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
  1626. rtlpriv->cfg->ops->tx_polling(hw, BEACON_QUEUE);
  1627. return true;
  1628. }
  1629. EXPORT_SYMBOL(rtl_cmd_send_packet);
  1630. const struct ieee80211_ops rtl_ops = {
  1631. .start = rtl_op_start,
  1632. .stop = rtl_op_stop,
  1633. .tx = rtl_op_tx,
  1634. .add_interface = rtl_op_add_interface,
  1635. .remove_interface = rtl_op_remove_interface,
  1636. .change_interface = rtl_op_change_interface,
  1637. #ifdef CONFIG_PM
  1638. .suspend = rtl_op_suspend,
  1639. .resume = rtl_op_resume,
  1640. #endif
  1641. .config = rtl_op_config,
  1642. .configure_filter = rtl_op_configure_filter,
  1643. .set_key = rtl_op_set_key,
  1644. .conf_tx = rtl_op_conf_tx,
  1645. .bss_info_changed = rtl_op_bss_info_changed,
  1646. .get_tsf = rtl_op_get_tsf,
  1647. .set_tsf = rtl_op_set_tsf,
  1648. .reset_tsf = rtl_op_reset_tsf,
  1649. .sta_notify = rtl_op_sta_notify,
  1650. .ampdu_action = rtl_op_ampdu_action,
  1651. .sw_scan_start = rtl_op_sw_scan_start,
  1652. .sw_scan_complete = rtl_op_sw_scan_complete,
  1653. .rfkill_poll = rtl_op_rfkill_poll,
  1654. .sta_add = rtl_op_sta_add,
  1655. .sta_remove = rtl_op_sta_remove,
  1656. .flush = rtl_op_flush,
  1657. };
  1658. EXPORT_SYMBOL_GPL(rtl_ops);
  1659. bool rtl_btc_status_false(void)
  1660. {
  1661. return false;
  1662. }
  1663. EXPORT_SYMBOL_GPL(rtl_btc_status_false);
  1664. void rtl_dm_diginit(struct ieee80211_hw *hw, u32 cur_igvalue)
  1665. {
  1666. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1667. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  1668. dm_digtable->dig_enable_flag = true;
  1669. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
  1670. dm_digtable->cur_igvalue = cur_igvalue;
  1671. dm_digtable->pre_igvalue = 0;
  1672. dm_digtable->cur_sta_cstate = DIG_STA_DISCONNECT;
  1673. dm_digtable->presta_cstate = DIG_STA_DISCONNECT;
  1674. dm_digtable->curmultista_cstate = DIG_MULTISTA_DISCONNECT;
  1675. dm_digtable->rssi_lowthresh = DM_DIG_THRESH_LOW;
  1676. dm_digtable->rssi_highthresh = DM_DIG_THRESH_HIGH;
  1677. dm_digtable->fa_lowthresh = DM_FALSEALARM_THRESH_LOW;
  1678. dm_digtable->fa_highthresh = DM_FALSEALARM_THRESH_HIGH;
  1679. dm_digtable->rx_gain_max = DM_DIG_MAX;
  1680. dm_digtable->rx_gain_min = DM_DIG_MIN;
  1681. dm_digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
  1682. dm_digtable->back_range_max = DM_DIG_BACKOFF_MAX;
  1683. dm_digtable->back_range_min = DM_DIG_BACKOFF_MIN;
  1684. dm_digtable->pre_cck_cca_thres = 0xff;
  1685. dm_digtable->cur_cck_cca_thres = 0x83;
  1686. dm_digtable->forbidden_igi = DM_DIG_MIN;
  1687. dm_digtable->large_fa_hit = 0;
  1688. dm_digtable->recover_cnt = 0;
  1689. dm_digtable->dig_min_0 = 0x25;
  1690. dm_digtable->dig_min_1 = 0x25;
  1691. dm_digtable->media_connect_0 = false;
  1692. dm_digtable->media_connect_1 = false;
  1693. rtlpriv->dm.dm_initialgain_enable = true;
  1694. dm_digtable->bt30_cur_igi = 0x32;
  1695. dm_digtable->pre_cck_pd_state = CCK_PD_STAGE_MAX;
  1696. dm_digtable->cur_cck_pd_state = CCK_PD_STAGE_LOWRSSI;
  1697. }
  1698. EXPORT_SYMBOL(rtl_dm_diginit);