core.c 54 KB

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