core.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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(&rtlpriv->works.watchdog_timer);
  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 < 0 ||
  400. patterns[i].pattern_len > MAX_WOL_PATTERN_SIZE) {
  401. RT_TRACE(rtlpriv, COMP_POWER, DBG_WARNING,
  402. "Pattern[%d] is too long\n", i);
  403. continue;
  404. }
  405. pattern_os = patterns[i].pattern;
  406. mask_len = DIV_ROUND_UP(patterns[i].pattern_len, 8);
  407. mask_os = patterns[i].mask;
  408. RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
  409. "pattern content\n", pattern_os,
  410. patterns[i].pattern_len);
  411. RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
  412. "mask content\n", mask_os, mask_len);
  413. /* 1. unicast? multicast? or broadcast? */
  414. if (memcmp(pattern_os, broadcast_addr, 6) == 0)
  415. rtl_pattern.type = BROADCAST_PATTERN;
  416. else if (memcmp(pattern_os, multicast_addr1, 2) == 0 ||
  417. memcmp(pattern_os, multicast_addr2, 3) == 0)
  418. rtl_pattern.type = MULTICAST_PATTERN;
  419. else if (memcmp(pattern_os, mac->mac_addr, 6) == 0)
  420. rtl_pattern.type = UNICAST_PATTERN;
  421. else
  422. rtl_pattern.type = UNKNOWN_TYPE;
  423. /* 2. translate mask_from_os to mask_for_hw */
  424. /******************************************************************************
  425. * pattern from OS uses 'ethenet frame', like this:
  426. | 6 | 6 | 2 | 20 | Variable | 4 |
  427. |--------+--------+------+-----------+------------+-----|
  428. | 802.3 Mac Header | IP Header | TCP Packet | FCS |
  429. | DA | SA | Type |
  430. * BUT, packet catched by our HW is in '802.11 frame', begin from LLC,
  431. | 24 or 30 | 6 | 2 | 20 | Variable | 4 |
  432. |-------------------+--------+------+-----------+------------+-----|
  433. | 802.11 MAC Header | LLC | IP Header | TCP Packet | FCS |
  434. | Others | Tpye |
  435. * Therefore, we need translate mask_from_OS to mask_to_hw.
  436. * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
  437. * because new mask[0~5] means 'SA', but our HW packet begins from LLC,
  438. * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
  439. ******************************************************************************/
  440. /* Shift 6 bits */
  441. for (j = 0; j < mask_len - 1; j++) {
  442. mask[j] = mask_os[j] >> 6;
  443. mask[j] |= (mask_os[j + 1] & 0x3F) << 2;
  444. }
  445. mask[j] = (mask_os[j] >> 6) & 0x3F;
  446. /* Set bit 0-5 to zero */
  447. mask[0] &= 0xC0;
  448. RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
  449. "mask to hw\n", mask, mask_len);
  450. for (j = 0; j < (MAX_WOL_BIT_MASK_SIZE + 1) / 4; j++) {
  451. rtl_pattern.mask[j] = mask[j * 4];
  452. rtl_pattern.mask[j] |= (mask[j * 4 + 1] << 8);
  453. rtl_pattern.mask[j] |= (mask[j * 4 + 2] << 16);
  454. rtl_pattern.mask[j] |= (mask[j * 4 + 3] << 24);
  455. }
  456. /* To get the wake up pattern from the mask.
  457. * We do not count first 12 bits which means
  458. * DA[6] and SA[6] in the pattern to match HW design.
  459. */
  460. len = 0;
  461. for (j = 12; j < patterns[i].pattern_len; j++) {
  462. if ((mask_os[j / 8] >> (j % 8)) & 0x01) {
  463. content[len] = pattern_os[j];
  464. len++;
  465. }
  466. }
  467. RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
  468. "pattern to hw\n", content, len);
  469. /* 3. calculate crc */
  470. rtl_pattern.crc = _calculate_wol_pattern_crc(content, len);
  471. RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
  472. "CRC_Remainder = 0x%x\n", rtl_pattern.crc);
  473. /* 4. write crc & mask_for_hw to hw */
  474. rtlpriv->cfg->ops->add_wowlan_pattern(hw, &rtl_pattern, i);
  475. }
  476. rtl_write_byte(rtlpriv, 0x698, wow->n_patterns);
  477. }
  478. static int rtl_op_suspend(struct ieee80211_hw *hw,
  479. struct cfg80211_wowlan *wow)
  480. {
  481. struct rtl_priv *rtlpriv = rtl_priv(hw);
  482. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  483. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  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. rtlhal->last_suspend_sec = ktime_get_real_seconds();
  489. if ((ppsc->wo_wlan_mode & WAKE_ON_PATTERN_MATCH) && wow->n_patterns)
  490. _rtl_add_wowlan_patterns(hw, wow);
  491. rtlhal->driver_is_goingto_unload = true;
  492. rtlhal->enter_pnp_sleep = true;
  493. rtl_lps_leave(hw);
  494. rtl_op_stop(hw);
  495. device_set_wakeup_enable(wiphy_dev(hw->wiphy), true);
  496. return 0;
  497. }
  498. static int rtl_op_resume(struct ieee80211_hw *hw)
  499. {
  500. struct rtl_priv *rtlpriv = rtl_priv(hw);
  501. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  502. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  503. time64_t now;
  504. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
  505. rtlhal->driver_is_goingto_unload = false;
  506. rtlhal->enter_pnp_sleep = false;
  507. rtlhal->wake_from_pnp_sleep = true;
  508. /* to resovle s4 can not wake up*/
  509. now = ktime_get_real_seconds();
  510. if (now - rtlhal->last_suspend_sec < 5)
  511. return -1;
  512. rtl_op_start(hw);
  513. device_set_wakeup_enable(wiphy_dev(hw->wiphy), false);
  514. ieee80211_resume_disconnect(mac->vif);
  515. rtlhal->wake_from_pnp_sleep = false;
  516. return 0;
  517. }
  518. #endif
  519. static int rtl_op_config(struct ieee80211_hw *hw, u32 changed)
  520. {
  521. struct rtl_priv *rtlpriv = rtl_priv(hw);
  522. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  523. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  524. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  525. struct ieee80211_conf *conf = &hw->conf;
  526. if (mac->skip_scan)
  527. return 1;
  528. mutex_lock(&rtlpriv->locks.conf_mutex);
  529. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) { /* BIT(2)*/
  530. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  531. "IEEE80211_CONF_CHANGE_LISTEN_INTERVAL\n");
  532. }
  533. /*For IPS */
  534. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  535. if (hw->conf.flags & IEEE80211_CONF_IDLE)
  536. rtl_ips_nic_off(hw);
  537. else
  538. rtl_ips_nic_on(hw);
  539. } else {
  540. /*
  541. *although rfoff may not cause by ips, but we will
  542. *check the reason in set_rf_power_state function
  543. */
  544. if (unlikely(ppsc->rfpwr_state == ERFOFF))
  545. rtl_ips_nic_on(hw);
  546. }
  547. /*For LPS */
  548. if ((changed & IEEE80211_CONF_CHANGE_PS) &&
  549. rtlpriv->psc.swctrl_lps && !rtlpriv->psc.fwctrl_lps) {
  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, true);
  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. true);
  1016. rcu_read_unlock();
  1017. /* to avoid AP Disassociation caused by inactivity */
  1018. rtlpriv->cfg->ops->set_hw_reg(hw,
  1019. HW_VAR_KEEP_ALIVE,
  1020. (u8 *)(&keep_alive));
  1021. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  1022. "BSS_CHANGED_ASSOC\n");
  1023. } else {
  1024. struct cfg80211_bss *bss = NULL;
  1025. mstatus = RT_MEDIA_DISCONNECT;
  1026. if (mac->link_state == MAC80211_LINKED)
  1027. rtl_lps_leave(hw);
  1028. if (ppsc->p2p_ps_info.p2p_ps_mode > P2P_PS_NONE)
  1029. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  1030. mac->link_state = MAC80211_NOLINK;
  1031. bss = cfg80211_get_bss(hw->wiphy, NULL,
  1032. (u8 *)mac->bssid, NULL, 0,
  1033. IEEE80211_BSS_TYPE_ESS,
  1034. IEEE80211_PRIVACY_OFF);
  1035. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  1036. "bssid = %pMF\n", mac->bssid);
  1037. if (bss) {
  1038. cfg80211_unlink_bss(hw->wiphy, bss);
  1039. cfg80211_put_bss(hw->wiphy, bss);
  1040. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  1041. "cfg80211_unlink !!\n");
  1042. }
  1043. eth_zero_addr(mac->bssid);
  1044. mac->vendor = PEER_UNKNOWN;
  1045. mac->mode = 0;
  1046. if (rtlpriv->dm.supp_phymode_switch) {
  1047. if (rtlpriv->cfg->ops->chk_switch_dmdp)
  1048. rtlpriv->cfg->ops->chk_switch_dmdp(hw);
  1049. }
  1050. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  1051. "BSS_CHANGED_UN_ASSOC\n");
  1052. }
  1053. rtlpriv->cfg->ops->set_network_type(hw, vif->type);
  1054. /* For FW LPS:
  1055. * To tell firmware we have connected or disconnected
  1056. */
  1057. rtlpriv->cfg->ops->set_hw_reg(hw,
  1058. HW_VAR_H2C_FW_JOINBSSRPT,
  1059. (u8 *)(&mstatus));
  1060. ppsc->report_linked = (mstatus == RT_MEDIA_CONNECT) ?
  1061. true : false;
  1062. if (rtlpriv->cfg->ops->get_btc_status())
  1063. rtlpriv->btcoexist.btc_ops->btc_mediastatus_notify(
  1064. rtlpriv, mstatus);
  1065. }
  1066. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1067. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1068. "BSS_CHANGED_ERP_CTS_PROT\n");
  1069. mac->use_cts_protect = bss_conf->use_cts_prot;
  1070. }
  1071. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1072. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
  1073. "BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
  1074. bss_conf->use_short_preamble);
  1075. mac->short_preamble = bss_conf->use_short_preamble;
  1076. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
  1077. (u8 *)(&mac->short_preamble));
  1078. }
  1079. if (changed & BSS_CHANGED_ERP_SLOT) {
  1080. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1081. "BSS_CHANGED_ERP_SLOT\n");
  1082. if (bss_conf->use_short_slot)
  1083. mac->slot_time = RTL_SLOT_TIME_9;
  1084. else
  1085. mac->slot_time = RTL_SLOT_TIME_20;
  1086. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
  1087. (u8 *)(&mac->slot_time));
  1088. }
  1089. if (changed & BSS_CHANGED_HT) {
  1090. struct ieee80211_sta *sta = NULL;
  1091. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1092. "BSS_CHANGED_HT\n");
  1093. rcu_read_lock();
  1094. sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
  1095. if (sta) {
  1096. if (sta->ht_cap.ampdu_density >
  1097. mac->current_ampdu_density)
  1098. mac->current_ampdu_density =
  1099. sta->ht_cap.ampdu_density;
  1100. if (sta->ht_cap.ampdu_factor <
  1101. mac->current_ampdu_factor)
  1102. mac->current_ampdu_factor =
  1103. sta->ht_cap.ampdu_factor;
  1104. }
  1105. rcu_read_unlock();
  1106. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
  1107. (u8 *)(&mac->max_mss_density));
  1108. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
  1109. &mac->current_ampdu_factor);
  1110. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
  1111. &mac->current_ampdu_density);
  1112. }
  1113. if (changed & BSS_CHANGED_BSSID) {
  1114. u32 basic_rates;
  1115. struct ieee80211_sta *sta = NULL;
  1116. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
  1117. (u8 *)bss_conf->bssid);
  1118. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
  1119. "bssid: %pM\n", bss_conf->bssid);
  1120. mac->vendor = PEER_UNKNOWN;
  1121. memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
  1122. rcu_read_lock();
  1123. sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
  1124. if (!sta) {
  1125. rcu_read_unlock();
  1126. goto out;
  1127. }
  1128. if (rtlhal->current_bandtype == BAND_ON_5G) {
  1129. mac->mode = WIRELESS_MODE_A;
  1130. } else {
  1131. if (sta->supp_rates[0] <= 0xf)
  1132. mac->mode = WIRELESS_MODE_B;
  1133. else
  1134. mac->mode = WIRELESS_MODE_G;
  1135. }
  1136. if (sta->ht_cap.ht_supported) {
  1137. if (rtlhal->current_bandtype == BAND_ON_2_4G)
  1138. mac->mode = WIRELESS_MODE_N_24G;
  1139. else
  1140. mac->mode = WIRELESS_MODE_N_5G;
  1141. }
  1142. if (sta->vht_cap.vht_supported) {
  1143. if (rtlhal->current_bandtype == BAND_ON_5G)
  1144. mac->mode = WIRELESS_MODE_AC_5G;
  1145. else
  1146. mac->mode = WIRELESS_MODE_AC_24G;
  1147. }
  1148. /* just station need it, because ibss & ap mode will
  1149. * set in sta_add, and will be NULL here */
  1150. if (vif->type == NL80211_IFTYPE_STATION) {
  1151. struct rtl_sta_info *sta_entry;
  1152. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1153. sta_entry->wireless_mode = mac->mode;
  1154. }
  1155. if (sta->ht_cap.ht_supported) {
  1156. mac->ht_enable = true;
  1157. /*
  1158. * for cisco 1252 bw20 it's wrong
  1159. * if (ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
  1160. * mac->bw_40 = true;
  1161. * }
  1162. * */
  1163. }
  1164. if (sta->vht_cap.vht_supported)
  1165. mac->vht_enable = true;
  1166. if (changed & BSS_CHANGED_BASIC_RATES) {
  1167. /* for 5G must << RATE_6M_INDEX = 4,
  1168. * because 5G have no cck rate*/
  1169. if (rtlhal->current_bandtype == BAND_ON_5G)
  1170. basic_rates = sta->supp_rates[1] << 4;
  1171. else
  1172. basic_rates = sta->supp_rates[0];
  1173. mac->basic_rates = basic_rates;
  1174. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
  1175. (u8 *)(&basic_rates));
  1176. }
  1177. rcu_read_unlock();
  1178. }
  1179. out:
  1180. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1181. }
  1182. static u64 rtl_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1183. {
  1184. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1185. u64 tsf;
  1186. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&tsf));
  1187. return tsf;
  1188. }
  1189. static void rtl_op_set_tsf(struct ieee80211_hw *hw,
  1190. struct ieee80211_vif *vif, u64 tsf)
  1191. {
  1192. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1193. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1194. u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
  1195. mac->tsf = tsf;
  1196. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&bibss));
  1197. }
  1198. static void rtl_op_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1199. {
  1200. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1201. u8 tmp = 0;
  1202. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, (u8 *)(&tmp));
  1203. }
  1204. static void rtl_op_sta_notify(struct ieee80211_hw *hw,
  1205. struct ieee80211_vif *vif,
  1206. enum sta_notify_cmd cmd,
  1207. struct ieee80211_sta *sta)
  1208. {
  1209. switch (cmd) {
  1210. case STA_NOTIFY_SLEEP:
  1211. break;
  1212. case STA_NOTIFY_AWAKE:
  1213. break;
  1214. default:
  1215. break;
  1216. }
  1217. }
  1218. static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
  1219. struct ieee80211_vif *vif,
  1220. struct ieee80211_ampdu_params *params)
  1221. {
  1222. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1223. struct ieee80211_sta *sta = params->sta;
  1224. enum ieee80211_ampdu_mlme_action action = params->action;
  1225. u16 tid = params->tid;
  1226. u16 *ssn = &params->ssn;
  1227. switch (action) {
  1228. case IEEE80211_AMPDU_TX_START:
  1229. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1230. "IEEE80211_AMPDU_TX_START: TID:%d\n", tid);
  1231. return rtl_tx_agg_start(hw, vif, sta, tid, ssn);
  1232. case IEEE80211_AMPDU_TX_STOP_CONT:
  1233. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  1234. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  1235. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1236. "IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
  1237. return rtl_tx_agg_stop(hw, vif, sta, tid);
  1238. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1239. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1240. "IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
  1241. rtl_tx_agg_oper(hw, sta, tid);
  1242. break;
  1243. case IEEE80211_AMPDU_RX_START:
  1244. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1245. "IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
  1246. return rtl_rx_agg_start(hw, sta, tid);
  1247. case IEEE80211_AMPDU_RX_STOP:
  1248. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
  1249. "IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
  1250. return rtl_rx_agg_stop(hw, sta, tid);
  1251. default:
  1252. pr_err("IEEE80211_AMPDU_ERR!!!!:\n");
  1253. return -EOPNOTSUPP;
  1254. }
  1255. return 0;
  1256. }
  1257. static void rtl_op_sw_scan_start(struct ieee80211_hw *hw,
  1258. struct ieee80211_vif *vif,
  1259. const u8 *mac_addr)
  1260. {
  1261. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1262. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1263. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
  1264. mac->act_scanning = true;
  1265. if (rtlpriv->link_info.higher_busytraffic) {
  1266. mac->skip_scan = true;
  1267. return;
  1268. }
  1269. if (rtlpriv->cfg->ops->get_btc_status())
  1270. rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 1);
  1271. else if (rtlpriv->btcoexist.btc_ops)
  1272. rtlpriv->btcoexist.btc_ops->btc_scan_notify_wifi_only(rtlpriv,
  1273. 1);
  1274. if (rtlpriv->dm.supp_phymode_switch) {
  1275. if (rtlpriv->cfg->ops->chk_switch_dmdp)
  1276. rtlpriv->cfg->ops->chk_switch_dmdp(hw);
  1277. }
  1278. if (mac->link_state == MAC80211_LINKED) {
  1279. rtl_lps_leave(hw);
  1280. mac->link_state = MAC80211_LINKED_SCANNING;
  1281. } else {
  1282. rtl_ips_nic_on(hw);
  1283. }
  1284. /* Dul mac */
  1285. rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
  1286. rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
  1287. rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP_BAND0);
  1288. }
  1289. static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw,
  1290. struct ieee80211_vif *vif)
  1291. {
  1292. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1293. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1294. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
  1295. mac->act_scanning = false;
  1296. mac->skip_scan = false;
  1297. rtlpriv->btcoexist.btc_info.ap_num = rtlpriv->scan_list.num;
  1298. if (rtlpriv->link_info.higher_busytraffic)
  1299. return;
  1300. /* p2p will use 1/6/11 to scan */
  1301. if (mac->n_channels == 3)
  1302. mac->p2p_in_use = true;
  1303. else
  1304. mac->p2p_in_use = false;
  1305. mac->n_channels = 0;
  1306. /* Dul mac */
  1307. rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
  1308. if (mac->link_state == MAC80211_LINKED_SCANNING) {
  1309. mac->link_state = MAC80211_LINKED;
  1310. if (mac->opmode == NL80211_IFTYPE_STATION) {
  1311. /* fix fwlps issue */
  1312. rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
  1313. }
  1314. }
  1315. rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
  1316. if (rtlpriv->cfg->ops->get_btc_status())
  1317. rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 0);
  1318. else if (rtlpriv->btcoexist.btc_ops)
  1319. rtlpriv->btcoexist.btc_ops->btc_scan_notify_wifi_only(rtlpriv,
  1320. 0);
  1321. }
  1322. static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  1323. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  1324. struct ieee80211_key_conf *key)
  1325. {
  1326. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1327. u8 key_type = NO_ENCRYPTION;
  1328. u8 key_idx;
  1329. bool group_key = false;
  1330. bool wep_only = false;
  1331. int err = 0;
  1332. u8 mac_addr[ETH_ALEN];
  1333. u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1334. rtlpriv->btcoexist.btc_info.in_4way = false;
  1335. if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
  1336. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  1337. "not open hw encryption\n");
  1338. return -ENOSPC; /*User disabled HW-crypto */
  1339. }
  1340. /* To support IBSS, use sw-crypto for GTK */
  1341. if ((vif->type == NL80211_IFTYPE_ADHOC ||
  1342. vif->type == NL80211_IFTYPE_MESH_POINT) &&
  1343. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1344. return -ENOSPC;
  1345. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1346. "%s hardware based encryption for keyidx: %d, mac: %pM\n",
  1347. cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
  1348. sta ? sta->addr : bcast_addr);
  1349. rtlpriv->sec.being_setkey = true;
  1350. rtl_ips_nic_on(hw);
  1351. mutex_lock(&rtlpriv->locks.conf_mutex);
  1352. /* <1> get encryption alg */
  1353. switch (key->cipher) {
  1354. case WLAN_CIPHER_SUITE_WEP40:
  1355. key_type = WEP40_ENCRYPTION;
  1356. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP40\n");
  1357. break;
  1358. case WLAN_CIPHER_SUITE_WEP104:
  1359. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP104\n");
  1360. key_type = WEP104_ENCRYPTION;
  1361. break;
  1362. case WLAN_CIPHER_SUITE_TKIP:
  1363. key_type = TKIP_ENCRYPTION;
  1364. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:TKIP\n");
  1365. break;
  1366. case WLAN_CIPHER_SUITE_CCMP:
  1367. key_type = AESCCMP_ENCRYPTION;
  1368. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CCMP\n");
  1369. break;
  1370. case WLAN_CIPHER_SUITE_AES_CMAC:
  1371. /* HW don't support CMAC encryption,
  1372. * use software CMAC encryption
  1373. */
  1374. key_type = AESCMAC_ENCRYPTION;
  1375. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CMAC\n");
  1376. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1377. "HW don't support CMAC encryption, use software CMAC encryption\n");
  1378. err = -EOPNOTSUPP;
  1379. goto out_unlock;
  1380. default:
  1381. pr_err("alg_err:%x!!!!:\n", key->cipher);
  1382. goto out_unlock;
  1383. }
  1384. if (key_type == WEP40_ENCRYPTION ||
  1385. key_type == WEP104_ENCRYPTION ||
  1386. vif->type == NL80211_IFTYPE_ADHOC)
  1387. rtlpriv->sec.use_defaultkey = true;
  1388. /* <2> get key_idx */
  1389. key_idx = (u8) (key->keyidx);
  1390. if (key_idx > 3)
  1391. goto out_unlock;
  1392. /* <3> if pairwise key enable_hw_sec */
  1393. group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  1394. /* wep always be group key, but there are two conditions:
  1395. * 1) wep only: is just for wep enc, in this condition
  1396. * rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION
  1397. * will be true & enable_hw_sec will be set when wep
  1398. * ke setting.
  1399. * 2) wep(group) + AES(pairwise): some AP like cisco
  1400. * may use it, in this condition enable_hw_sec will not
  1401. * be set when wep key setting */
  1402. /* we must reset sec_info after lingked before set key,
  1403. * or some flag will be wrong*/
  1404. if (vif->type == NL80211_IFTYPE_AP ||
  1405. vif->type == NL80211_IFTYPE_MESH_POINT) {
  1406. if (!group_key || key_type == WEP40_ENCRYPTION ||
  1407. key_type == WEP104_ENCRYPTION) {
  1408. if (group_key)
  1409. wep_only = true;
  1410. rtlpriv->cfg->ops->enable_hw_sec(hw);
  1411. }
  1412. } else {
  1413. if (!group_key || vif->type == NL80211_IFTYPE_ADHOC ||
  1414. rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
  1415. if (rtlpriv->sec.pairwise_enc_algorithm ==
  1416. NO_ENCRYPTION &&
  1417. (key_type == WEP40_ENCRYPTION ||
  1418. key_type == WEP104_ENCRYPTION))
  1419. wep_only = true;
  1420. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  1421. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1422. "set enable_hw_sec, key_type:%x(OPEN:0 WEP40:1 TKIP:2 AES:4 WEP104:5)\n",
  1423. key_type);
  1424. rtlpriv->cfg->ops->enable_hw_sec(hw);
  1425. }
  1426. }
  1427. /* <4> set key based on cmd */
  1428. switch (cmd) {
  1429. case SET_KEY:
  1430. if (wep_only) {
  1431. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1432. "set WEP(group/pairwise) key\n");
  1433. /* Pairwise key with an assigned MAC address. */
  1434. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  1435. rtlpriv->sec.group_enc_algorithm = key_type;
  1436. /*set local buf about wep key. */
  1437. memcpy(rtlpriv->sec.key_buf[key_idx],
  1438. key->key, key->keylen);
  1439. rtlpriv->sec.key_len[key_idx] = key->keylen;
  1440. eth_zero_addr(mac_addr);
  1441. } else if (group_key) { /* group key */
  1442. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1443. "set group key\n");
  1444. /* group key */
  1445. rtlpriv->sec.group_enc_algorithm = key_type;
  1446. /*set local buf about group key. */
  1447. memcpy(rtlpriv->sec.key_buf[key_idx],
  1448. key->key, key->keylen);
  1449. rtlpriv->sec.key_len[key_idx] = key->keylen;
  1450. memcpy(mac_addr, bcast_addr, ETH_ALEN);
  1451. } else { /* pairwise key */
  1452. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1453. "set pairwise key\n");
  1454. if (!sta) {
  1455. WARN_ONCE(true,
  1456. "rtlwifi: pairwise key without mac_addr\n");
  1457. err = -EOPNOTSUPP;
  1458. goto out_unlock;
  1459. }
  1460. /* Pairwise key with an assigned MAC address. */
  1461. rtlpriv->sec.pairwise_enc_algorithm = key_type;
  1462. /*set local buf about pairwise key. */
  1463. memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
  1464. key->key, key->keylen);
  1465. rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
  1466. rtlpriv->sec.pairwise_key =
  1467. rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
  1468. memcpy(mac_addr, sta->addr, ETH_ALEN);
  1469. }
  1470. rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
  1471. group_key, key_type, wep_only,
  1472. false);
  1473. /* <5> tell mac80211 do something: */
  1474. /*must use sw generate IV, or can not work !!!!. */
  1475. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1476. key->hw_key_idx = key_idx;
  1477. if (key_type == TKIP_ENCRYPTION)
  1478. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1479. /*use software CCMP encryption for management frames (MFP) */
  1480. if (key_type == AESCCMP_ENCRYPTION)
  1481. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
  1482. break;
  1483. case DISABLE_KEY:
  1484. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1485. "disable key delete one entry\n");
  1486. /*set local buf about wep key. */
  1487. if (vif->type == NL80211_IFTYPE_AP ||
  1488. vif->type == NL80211_IFTYPE_MESH_POINT) {
  1489. if (sta)
  1490. rtl_cam_del_entry(hw, sta->addr);
  1491. }
  1492. memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
  1493. rtlpriv->sec.key_len[key_idx] = 0;
  1494. eth_zero_addr(mac_addr);
  1495. /*
  1496. *mac80211 will delete entrys one by one,
  1497. *so don't use rtl_cam_reset_all_entry
  1498. *or clear all entry here.
  1499. */
  1500. rtl_wait_tx_report_acked(hw, 500); /* wait 500ms for TX ack */
  1501. rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
  1502. break;
  1503. default:
  1504. pr_err("cmd_err:%x!!!!:\n", cmd);
  1505. }
  1506. out_unlock:
  1507. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1508. rtlpriv->sec.being_setkey = false;
  1509. return err;
  1510. }
  1511. static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
  1512. {
  1513. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1514. bool radio_state;
  1515. bool blocked;
  1516. u8 valid = 0;
  1517. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  1518. return;
  1519. mutex_lock(&rtlpriv->locks.conf_mutex);
  1520. /*if Radio On return true here */
  1521. radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
  1522. if (valid) {
  1523. if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
  1524. rtlpriv->rfkill.rfkill_state = radio_state;
  1525. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  1526. "wireless radio switch turned %s\n",
  1527. radio_state ? "on" : "off");
  1528. blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
  1529. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  1530. }
  1531. }
  1532. mutex_unlock(&rtlpriv->locks.conf_mutex);
  1533. }
  1534. /* this function is called by mac80211 to flush tx buffer
  1535. * before switch channle or power save, or tx buffer packet
  1536. * maybe send after offchannel or rf sleep, this may cause
  1537. * dis-association by AP */
  1538. static void rtl_op_flush(struct ieee80211_hw *hw,
  1539. struct ieee80211_vif *vif,
  1540. u32 queues,
  1541. bool drop)
  1542. {
  1543. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1544. if (rtlpriv->intf_ops->flush)
  1545. rtlpriv->intf_ops->flush(hw, queues, drop);
  1546. }
  1547. /* Description:
  1548. * This routine deals with the Power Configuration CMD
  1549. * parsing for RTL8723/RTL8188E Series IC.
  1550. * Assumption:
  1551. * We should follow specific format that was released from HW SD.
  1552. */
  1553. bool rtl_hal_pwrseqcmdparsing(struct rtl_priv *rtlpriv, u8 cut_version,
  1554. u8 faversion, u8 interface_type,
  1555. struct wlan_pwr_cfg pwrcfgcmd[])
  1556. {
  1557. struct wlan_pwr_cfg cfg_cmd;
  1558. bool polling_bit = false;
  1559. u32 ary_idx = 0;
  1560. u8 value = 0;
  1561. u32 offset = 0;
  1562. u32 polling_count = 0;
  1563. u32 max_polling_cnt = 5000;
  1564. do {
  1565. cfg_cmd = pwrcfgcmd[ary_idx];
  1566. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1567. "rtl_hal_pwrseqcmdparsing(): offset(%#x),cut_msk(%#x), famsk(%#x), interface_msk(%#x), base(%#x), cmd(%#x), msk(%#x), value(%#x)\n",
  1568. GET_PWR_CFG_OFFSET(cfg_cmd),
  1569. GET_PWR_CFG_CUT_MASK(cfg_cmd),
  1570. GET_PWR_CFG_FAB_MASK(cfg_cmd),
  1571. GET_PWR_CFG_INTF_MASK(cfg_cmd),
  1572. GET_PWR_CFG_BASE(cfg_cmd), GET_PWR_CFG_CMD(cfg_cmd),
  1573. GET_PWR_CFG_MASK(cfg_cmd), GET_PWR_CFG_VALUE(cfg_cmd));
  1574. if ((GET_PWR_CFG_FAB_MASK(cfg_cmd)&faversion) &&
  1575. (GET_PWR_CFG_CUT_MASK(cfg_cmd)&cut_version) &&
  1576. (GET_PWR_CFG_INTF_MASK(cfg_cmd)&interface_type)) {
  1577. switch (GET_PWR_CFG_CMD(cfg_cmd)) {
  1578. case PWR_CMD_READ:
  1579. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1580. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_READ\n");
  1581. break;
  1582. case PWR_CMD_WRITE:
  1583. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1584. "%s(): PWR_CMD_WRITE\n", __func__);
  1585. offset = GET_PWR_CFG_OFFSET(cfg_cmd);
  1586. /*Read the value from system register*/
  1587. value = rtl_read_byte(rtlpriv, offset);
  1588. value &= (~(GET_PWR_CFG_MASK(cfg_cmd)));
  1589. value |= (GET_PWR_CFG_VALUE(cfg_cmd) &
  1590. GET_PWR_CFG_MASK(cfg_cmd));
  1591. /*Write the value back to sytem register*/
  1592. rtl_write_byte(rtlpriv, offset, value);
  1593. break;
  1594. case PWR_CMD_POLLING:
  1595. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1596. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_POLLING\n");
  1597. polling_bit = false;
  1598. offset = GET_PWR_CFG_OFFSET(cfg_cmd);
  1599. do {
  1600. value = rtl_read_byte(rtlpriv, offset);
  1601. value &= GET_PWR_CFG_MASK(cfg_cmd);
  1602. if (value ==
  1603. (GET_PWR_CFG_VALUE(cfg_cmd) &
  1604. GET_PWR_CFG_MASK(cfg_cmd)))
  1605. polling_bit = true;
  1606. else
  1607. udelay(10);
  1608. if (polling_count++ > max_polling_cnt)
  1609. return false;
  1610. } while (!polling_bit);
  1611. break;
  1612. case PWR_CMD_DELAY:
  1613. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1614. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_DELAY\n");
  1615. if (GET_PWR_CFG_VALUE(cfg_cmd) ==
  1616. PWRSEQ_DELAY_US)
  1617. udelay(GET_PWR_CFG_OFFSET(cfg_cmd));
  1618. else
  1619. mdelay(GET_PWR_CFG_OFFSET(cfg_cmd));
  1620. break;
  1621. case PWR_CMD_END:
  1622. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  1623. "rtl_hal_pwrseqcmdparsing(): PWR_CMD_END\n");
  1624. return true;
  1625. default:
  1626. WARN_ONCE(true,
  1627. "rtlwifi: rtl_hal_pwrseqcmdparsing(): Unknown CMD!!\n");
  1628. break;
  1629. }
  1630. }
  1631. ary_idx++;
  1632. } while (1);
  1633. return true;
  1634. }
  1635. EXPORT_SYMBOL(rtl_hal_pwrseqcmdparsing);
  1636. bool rtl_cmd_send_packet(struct ieee80211_hw *hw, struct sk_buff *skb)
  1637. {
  1638. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1639. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  1640. struct rtl8192_tx_ring *ring;
  1641. struct rtl_tx_desc *pdesc;
  1642. unsigned long flags;
  1643. struct sk_buff *pskb = NULL;
  1644. ring = &rtlpci->tx_ring[BEACON_QUEUE];
  1645. spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
  1646. pskb = __skb_dequeue(&ring->queue);
  1647. if (pskb)
  1648. dev_kfree_skb_irq(pskb);
  1649. /*this is wrong, fill_tx_cmddesc needs update*/
  1650. pdesc = &ring->desc[0];
  1651. rtlpriv->cfg->ops->fill_tx_cmddesc(hw, (u8 *)pdesc, 1, 1, skb);
  1652. __skb_queue_tail(&ring->queue, skb);
  1653. spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
  1654. rtlpriv->cfg->ops->tx_polling(hw, BEACON_QUEUE);
  1655. return true;
  1656. }
  1657. EXPORT_SYMBOL(rtl_cmd_send_packet);
  1658. const struct ieee80211_ops rtl_ops = {
  1659. .start = rtl_op_start,
  1660. .stop = rtl_op_stop,
  1661. .tx = rtl_op_tx,
  1662. .add_interface = rtl_op_add_interface,
  1663. .remove_interface = rtl_op_remove_interface,
  1664. .change_interface = rtl_op_change_interface,
  1665. #ifdef CONFIG_PM
  1666. .suspend = rtl_op_suspend,
  1667. .resume = rtl_op_resume,
  1668. #endif
  1669. .config = rtl_op_config,
  1670. .configure_filter = rtl_op_configure_filter,
  1671. .set_key = rtl_op_set_key,
  1672. .conf_tx = rtl_op_conf_tx,
  1673. .bss_info_changed = rtl_op_bss_info_changed,
  1674. .get_tsf = rtl_op_get_tsf,
  1675. .set_tsf = rtl_op_set_tsf,
  1676. .reset_tsf = rtl_op_reset_tsf,
  1677. .sta_notify = rtl_op_sta_notify,
  1678. .ampdu_action = rtl_op_ampdu_action,
  1679. .sw_scan_start = rtl_op_sw_scan_start,
  1680. .sw_scan_complete = rtl_op_sw_scan_complete,
  1681. .rfkill_poll = rtl_op_rfkill_poll,
  1682. .sta_add = rtl_op_sta_add,
  1683. .sta_remove = rtl_op_sta_remove,
  1684. .flush = rtl_op_flush,
  1685. };
  1686. EXPORT_SYMBOL_GPL(rtl_ops);
  1687. bool rtl_btc_status_false(void)
  1688. {
  1689. return false;
  1690. }
  1691. EXPORT_SYMBOL_GPL(rtl_btc_status_false);
  1692. void rtl_dm_diginit(struct ieee80211_hw *hw, u32 cur_igvalue)
  1693. {
  1694. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1695. struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
  1696. dm_digtable->dig_enable_flag = true;
  1697. dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
  1698. dm_digtable->cur_igvalue = cur_igvalue;
  1699. dm_digtable->pre_igvalue = 0;
  1700. dm_digtable->cur_sta_cstate = DIG_STA_DISCONNECT;
  1701. dm_digtable->presta_cstate = DIG_STA_DISCONNECT;
  1702. dm_digtable->curmultista_cstate = DIG_MULTISTA_DISCONNECT;
  1703. dm_digtable->rssi_lowthresh = DM_DIG_THRESH_LOW;
  1704. dm_digtable->rssi_highthresh = DM_DIG_THRESH_HIGH;
  1705. dm_digtable->fa_lowthresh = DM_FALSEALARM_THRESH_LOW;
  1706. dm_digtable->fa_highthresh = DM_FALSEALARM_THRESH_HIGH;
  1707. dm_digtable->rx_gain_max = DM_DIG_MAX;
  1708. dm_digtable->rx_gain_min = DM_DIG_MIN;
  1709. dm_digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
  1710. dm_digtable->back_range_max = DM_DIG_BACKOFF_MAX;
  1711. dm_digtable->back_range_min = DM_DIG_BACKOFF_MIN;
  1712. dm_digtable->pre_cck_cca_thres = 0xff;
  1713. dm_digtable->cur_cck_cca_thres = 0x83;
  1714. dm_digtable->forbidden_igi = DM_DIG_MIN;
  1715. dm_digtable->large_fa_hit = 0;
  1716. dm_digtable->recover_cnt = 0;
  1717. dm_digtable->dig_min_0 = 0x25;
  1718. dm_digtable->dig_min_1 = 0x25;
  1719. dm_digtable->media_connect_0 = false;
  1720. dm_digtable->media_connect_1 = false;
  1721. rtlpriv->dm.dm_initialgain_enable = true;
  1722. dm_digtable->bt30_cur_igi = 0x32;
  1723. dm_digtable->pre_cck_pd_state = CCK_PD_STAGE_MAX;
  1724. dm_digtable->cur_cck_pd_state = CCK_PD_STAGE_LOWRSSI;
  1725. }
  1726. EXPORT_SYMBOL(rtl_dm_diginit);