core.c 52 KB

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