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