core.c 54 KB

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