core.c 54 KB

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