core.c 55 KB

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