sta.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402
  1. /*
  2. * Mac80211 STA API for ST-Ericsson CW1200 drivers
  3. *
  4. * Copyright (c) 2010, ST-Ericsson
  5. * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/vmalloc.h>
  12. #include <linux/sched.h>
  13. #include <linux/firmware.h>
  14. #include <linux/module.h>
  15. #include <linux/etherdevice.h>
  16. #include "cw1200.h"
  17. #include "sta.h"
  18. #include "fwio.h"
  19. #include "bh.h"
  20. #include "debug.h"
  21. #ifndef ERP_INFO_BYTE_OFFSET
  22. #define ERP_INFO_BYTE_OFFSET 2
  23. #endif
  24. static void cw1200_do_join(struct cw1200_common *priv);
  25. static void cw1200_do_unjoin(struct cw1200_common *priv);
  26. static int cw1200_upload_beacon(struct cw1200_common *priv);
  27. static int cw1200_upload_pspoll(struct cw1200_common *priv);
  28. static int cw1200_upload_null(struct cw1200_common *priv);
  29. static int cw1200_upload_qosnull(struct cw1200_common *priv);
  30. static int cw1200_start_ap(struct cw1200_common *priv);
  31. static int cw1200_update_beaconing(struct cw1200_common *priv);
  32. static int cw1200_enable_beaconing(struct cw1200_common *priv,
  33. bool enable);
  34. static void __cw1200_sta_notify(struct ieee80211_hw *dev,
  35. struct ieee80211_vif *vif,
  36. enum sta_notify_cmd notify_cmd,
  37. int link_id);
  38. static int __cw1200_flush(struct cw1200_common *priv, bool drop);
  39. static inline void __cw1200_free_event_queue(struct list_head *list)
  40. {
  41. struct cw1200_wsm_event *event, *tmp;
  42. list_for_each_entry_safe(event, tmp, list, link) {
  43. list_del(&event->link);
  44. kfree(event);
  45. }
  46. }
  47. /* ******************************************************************** */
  48. /* STA API */
  49. int cw1200_start(struct ieee80211_hw *dev)
  50. {
  51. struct cw1200_common *priv = dev->priv;
  52. int ret = 0;
  53. cw1200_pm_stay_awake(&priv->pm_state, HZ);
  54. mutex_lock(&priv->conf_mutex);
  55. /* default EDCA */
  56. WSM_EDCA_SET(&priv->edca, 0, 0x0002, 0x0003, 0x0007, 47, 0xc8, false);
  57. WSM_EDCA_SET(&priv->edca, 1, 0x0002, 0x0007, 0x000f, 94, 0xc8, false);
  58. WSM_EDCA_SET(&priv->edca, 2, 0x0003, 0x000f, 0x03ff, 0, 0xc8, false);
  59. WSM_EDCA_SET(&priv->edca, 3, 0x0007, 0x000f, 0x03ff, 0, 0xc8, false);
  60. ret = wsm_set_edca_params(priv, &priv->edca);
  61. if (ret)
  62. goto out;
  63. ret = cw1200_set_uapsd_param(priv, &priv->edca);
  64. if (ret)
  65. goto out;
  66. priv->setbssparams_done = false;
  67. memcpy(priv->mac_addr, dev->wiphy->perm_addr, ETH_ALEN);
  68. priv->mode = NL80211_IFTYPE_MONITOR;
  69. priv->wep_default_key_id = -1;
  70. priv->cqm_beacon_loss_count = 10;
  71. ret = cw1200_setup_mac(priv);
  72. if (ret)
  73. goto out;
  74. out:
  75. mutex_unlock(&priv->conf_mutex);
  76. return ret;
  77. }
  78. void cw1200_stop(struct ieee80211_hw *dev)
  79. {
  80. struct cw1200_common *priv = dev->priv;
  81. LIST_HEAD(list);
  82. int i;
  83. wsm_lock_tx(priv);
  84. while (down_trylock(&priv->scan.lock)) {
  85. /* Scan is in progress. Force it to stop. */
  86. priv->scan.req = NULL;
  87. schedule();
  88. }
  89. up(&priv->scan.lock);
  90. cancel_delayed_work_sync(&priv->scan.probe_work);
  91. cancel_delayed_work_sync(&priv->scan.timeout);
  92. cancel_delayed_work_sync(&priv->clear_recent_scan_work);
  93. cancel_delayed_work_sync(&priv->join_timeout);
  94. cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
  95. cancel_work_sync(&priv->unjoin_work);
  96. cancel_delayed_work_sync(&priv->link_id_gc_work);
  97. flush_workqueue(priv->workqueue);
  98. del_timer_sync(&priv->mcast_timeout);
  99. mutex_lock(&priv->conf_mutex);
  100. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  101. priv->listening = false;
  102. spin_lock(&priv->event_queue_lock);
  103. list_splice_init(&priv->event_queue, &list);
  104. spin_unlock(&priv->event_queue_lock);
  105. __cw1200_free_event_queue(&list);
  106. priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
  107. priv->join_pending = false;
  108. for (i = 0; i < 4; i++)
  109. cw1200_queue_clear(&priv->tx_queue[i]);
  110. mutex_unlock(&priv->conf_mutex);
  111. tx_policy_clean(priv);
  112. /* HACK! */
  113. if (atomic_xchg(&priv->tx_lock, 1) != 1)
  114. pr_debug("[STA] TX is force-unlocked due to stop request.\n");
  115. wsm_unlock_tx(priv);
  116. atomic_xchg(&priv->tx_lock, 0); /* for recovery to work */
  117. }
  118. static int cw1200_bssloss_mitigation = 1;
  119. module_param(cw1200_bssloss_mitigation, int, 0644);
  120. MODULE_PARM_DESC(cw1200_bssloss_mitigation, "BSS Loss mitigation. 0 == disabled, 1 == enabled (default)");
  121. void __cw1200_cqm_bssloss_sm(struct cw1200_common *priv,
  122. int init, int good, int bad)
  123. {
  124. int tx = 0;
  125. priv->delayed_link_loss = 0;
  126. cancel_work_sync(&priv->bss_params_work);
  127. pr_debug("[STA] CQM BSSLOSS_SM: state: %d init %d good %d bad: %d txlock: %d uj: %d\n",
  128. priv->bss_loss_state,
  129. init, good, bad,
  130. atomic_read(&priv->tx_lock),
  131. priv->delayed_unjoin);
  132. /* If we have a pending unjoin */
  133. if (priv->delayed_unjoin)
  134. return;
  135. if (init) {
  136. queue_delayed_work(priv->workqueue,
  137. &priv->bss_loss_work,
  138. HZ);
  139. priv->bss_loss_state = 0;
  140. /* Skip the confimration procedure in P2P case */
  141. if (!priv->vif->p2p && !atomic_read(&priv->tx_lock))
  142. tx = 1;
  143. } else if (good) {
  144. cancel_delayed_work_sync(&priv->bss_loss_work);
  145. priv->bss_loss_state = 0;
  146. queue_work(priv->workqueue, &priv->bss_params_work);
  147. } else if (bad) {
  148. /* XXX Should we just keep going until we time out? */
  149. if (priv->bss_loss_state < 3)
  150. tx = 1;
  151. } else {
  152. cancel_delayed_work_sync(&priv->bss_loss_work);
  153. priv->bss_loss_state = 0;
  154. }
  155. /* Bypass mitigation if it's disabled */
  156. if (!cw1200_bssloss_mitigation)
  157. tx = 0;
  158. /* Spit out a NULL packet to our AP if necessary */
  159. if (tx) {
  160. struct sk_buff *skb;
  161. priv->bss_loss_state++;
  162. skb = ieee80211_nullfunc_get(priv->hw, priv->vif);
  163. WARN_ON(!skb);
  164. if (skb)
  165. cw1200_tx(priv->hw, NULL, skb);
  166. }
  167. }
  168. int cw1200_add_interface(struct ieee80211_hw *dev,
  169. struct ieee80211_vif *vif)
  170. {
  171. int ret;
  172. struct cw1200_common *priv = dev->priv;
  173. /* __le32 auto_calibration_mode = __cpu_to_le32(1); */
  174. vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
  175. IEEE80211_VIF_SUPPORTS_CQM_RSSI;
  176. mutex_lock(&priv->conf_mutex);
  177. if (priv->mode != NL80211_IFTYPE_MONITOR) {
  178. mutex_unlock(&priv->conf_mutex);
  179. return -EOPNOTSUPP;
  180. }
  181. switch (vif->type) {
  182. case NL80211_IFTYPE_STATION:
  183. case NL80211_IFTYPE_ADHOC:
  184. case NL80211_IFTYPE_MESH_POINT:
  185. case NL80211_IFTYPE_AP:
  186. priv->mode = vif->type;
  187. break;
  188. default:
  189. mutex_unlock(&priv->conf_mutex);
  190. return -EOPNOTSUPP;
  191. }
  192. priv->vif = vif;
  193. memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
  194. ret = cw1200_setup_mac(priv);
  195. /* Enable auto-calibration */
  196. /* Exception in subsequent channel switch; disabled.
  197. * wsm_write_mib(priv, WSM_MIB_ID_SET_AUTO_CALIBRATION_MODE,
  198. * &auto_calibration_mode, sizeof(auto_calibration_mode));
  199. */
  200. mutex_unlock(&priv->conf_mutex);
  201. return ret;
  202. }
  203. void cw1200_remove_interface(struct ieee80211_hw *dev,
  204. struct ieee80211_vif *vif)
  205. {
  206. struct cw1200_common *priv = dev->priv;
  207. struct wsm_reset reset = {
  208. .reset_statistics = true,
  209. };
  210. int i;
  211. mutex_lock(&priv->conf_mutex);
  212. switch (priv->join_status) {
  213. case CW1200_JOIN_STATUS_JOINING:
  214. case CW1200_JOIN_STATUS_PRE_STA:
  215. case CW1200_JOIN_STATUS_STA:
  216. case CW1200_JOIN_STATUS_IBSS:
  217. wsm_lock_tx(priv);
  218. if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
  219. wsm_unlock_tx(priv);
  220. break;
  221. case CW1200_JOIN_STATUS_AP:
  222. for (i = 0; priv->link_id_map; ++i) {
  223. if (priv->link_id_map & BIT(i)) {
  224. reset.link_id = i;
  225. wsm_reset(priv, &reset);
  226. priv->link_id_map &= ~BIT(i);
  227. }
  228. }
  229. memset(priv->link_id_db, 0, sizeof(priv->link_id_db));
  230. priv->sta_asleep_mask = 0;
  231. priv->enable_beacon = false;
  232. priv->tx_multicast = false;
  233. priv->aid0_bit_set = false;
  234. priv->buffered_multicasts = false;
  235. priv->pspoll_mask = 0;
  236. reset.link_id = 0;
  237. wsm_reset(priv, &reset);
  238. break;
  239. case CW1200_JOIN_STATUS_MONITOR:
  240. cw1200_update_listening(priv, false);
  241. break;
  242. default:
  243. break;
  244. }
  245. priv->vif = NULL;
  246. priv->mode = NL80211_IFTYPE_MONITOR;
  247. memset(priv->mac_addr, 0, ETH_ALEN);
  248. memset(&priv->p2p_ps_modeinfo, 0, sizeof(priv->p2p_ps_modeinfo));
  249. cw1200_free_keys(priv);
  250. cw1200_setup_mac(priv);
  251. priv->listening = false;
  252. priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
  253. if (!__cw1200_flush(priv, true))
  254. wsm_unlock_tx(priv);
  255. mutex_unlock(&priv->conf_mutex);
  256. }
  257. int cw1200_change_interface(struct ieee80211_hw *dev,
  258. struct ieee80211_vif *vif,
  259. enum nl80211_iftype new_type,
  260. bool p2p)
  261. {
  262. int ret = 0;
  263. pr_debug("change_interface new: %d (%d), old: %d (%d)\n", new_type,
  264. p2p, vif->type, vif->p2p);
  265. if (new_type != vif->type || vif->p2p != p2p) {
  266. cw1200_remove_interface(dev, vif);
  267. vif->type = new_type;
  268. vif->p2p = p2p;
  269. ret = cw1200_add_interface(dev, vif);
  270. }
  271. return ret;
  272. }
  273. int cw1200_config(struct ieee80211_hw *dev, u32 changed)
  274. {
  275. int ret = 0;
  276. struct cw1200_common *priv = dev->priv;
  277. struct ieee80211_conf *conf = &dev->conf;
  278. pr_debug("CONFIG CHANGED: %08x\n", changed);
  279. down(&priv->scan.lock);
  280. mutex_lock(&priv->conf_mutex);
  281. /* TODO: IEEE80211_CONF_CHANGE_QOS */
  282. /* TODO: IEEE80211_CONF_CHANGE_LISTEN_INTERVAL */
  283. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  284. priv->output_power = conf->power_level;
  285. pr_debug("[STA] TX power: %d\n", priv->output_power);
  286. wsm_set_output_power(priv, priv->output_power * 10);
  287. }
  288. if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) &&
  289. (priv->channel != conf->chandef.chan)) {
  290. struct ieee80211_channel *ch = conf->chandef.chan;
  291. struct wsm_switch_channel channel = {
  292. .channel_number = ch->hw_value,
  293. };
  294. pr_debug("[STA] Freq %d (wsm ch: %d).\n",
  295. ch->center_freq, ch->hw_value);
  296. /* __cw1200_flush() implicitly locks tx, if successful */
  297. if (!__cw1200_flush(priv, false)) {
  298. if (!wsm_switch_channel(priv, &channel)) {
  299. ret = wait_event_timeout(priv->channel_switch_done,
  300. !priv->channel_switch_in_progress,
  301. 3 * HZ);
  302. if (ret) {
  303. /* Already unlocks if successful */
  304. priv->channel = ch;
  305. ret = 0;
  306. } else {
  307. ret = -ETIMEDOUT;
  308. }
  309. } else {
  310. /* Unlock if switch channel fails */
  311. wsm_unlock_tx(priv);
  312. }
  313. }
  314. }
  315. if (changed & IEEE80211_CONF_CHANGE_PS) {
  316. if (!(conf->flags & IEEE80211_CONF_PS))
  317. priv->powersave_mode.mode = WSM_PSM_ACTIVE;
  318. else if (conf->dynamic_ps_timeout <= 0)
  319. priv->powersave_mode.mode = WSM_PSM_PS;
  320. else
  321. priv->powersave_mode.mode = WSM_PSM_FAST_PS;
  322. /* Firmware requires that value for this 1-byte field must
  323. * be specified in units of 500us. Values above the 128ms
  324. * threshold are not supported.
  325. */
  326. if (conf->dynamic_ps_timeout >= 0x80)
  327. priv->powersave_mode.fast_psm_idle_period = 0xFF;
  328. else
  329. priv->powersave_mode.fast_psm_idle_period =
  330. conf->dynamic_ps_timeout << 1;
  331. if (priv->join_status == CW1200_JOIN_STATUS_STA &&
  332. priv->bss_params.aid)
  333. cw1200_set_pm(priv, &priv->powersave_mode);
  334. }
  335. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  336. /* TBD: It looks like it's transparent
  337. * there's a monitor interface present -- use this
  338. * to determine for example whether to calculate
  339. * timestamps for packets or not, do not use instead
  340. * of filter flags!
  341. */
  342. }
  343. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  344. struct wsm_operational_mode mode = {
  345. .power_mode = cw1200_power_mode,
  346. .disable_more_flag_usage = true,
  347. };
  348. wsm_lock_tx(priv);
  349. /* Disable p2p-dev mode forced by TX request */
  350. if ((priv->join_status == CW1200_JOIN_STATUS_MONITOR) &&
  351. (conf->flags & IEEE80211_CONF_IDLE) &&
  352. !priv->listening) {
  353. cw1200_disable_listening(priv);
  354. priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
  355. }
  356. wsm_set_operational_mode(priv, &mode);
  357. wsm_unlock_tx(priv);
  358. }
  359. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
  360. pr_debug("[STA] Retry limits: %d (long), %d (short).\n",
  361. conf->long_frame_max_tx_count,
  362. conf->short_frame_max_tx_count);
  363. spin_lock_bh(&priv->tx_policy_cache.lock);
  364. priv->long_frame_max_tx_count = conf->long_frame_max_tx_count;
  365. priv->short_frame_max_tx_count =
  366. (conf->short_frame_max_tx_count < 0x0F) ?
  367. conf->short_frame_max_tx_count : 0x0F;
  368. priv->hw->max_rate_tries = priv->short_frame_max_tx_count;
  369. spin_unlock_bh(&priv->tx_policy_cache.lock);
  370. }
  371. mutex_unlock(&priv->conf_mutex);
  372. up(&priv->scan.lock);
  373. return ret;
  374. }
  375. void cw1200_update_filtering(struct cw1200_common *priv)
  376. {
  377. int ret;
  378. bool bssid_filtering = !priv->rx_filter.bssid;
  379. bool is_p2p = priv->vif && priv->vif->p2p;
  380. bool is_sta = priv->vif && NL80211_IFTYPE_STATION == priv->vif->type;
  381. static struct wsm_beacon_filter_control bf_ctrl;
  382. static struct wsm_mib_beacon_filter_table bf_tbl = {
  383. .entry[0].ie_id = WLAN_EID_VENDOR_SPECIFIC,
  384. .entry[0].flags = WSM_BEACON_FILTER_IE_HAS_CHANGED |
  385. WSM_BEACON_FILTER_IE_NO_LONGER_PRESENT |
  386. WSM_BEACON_FILTER_IE_HAS_APPEARED,
  387. .entry[0].oui[0] = 0x50,
  388. .entry[0].oui[1] = 0x6F,
  389. .entry[0].oui[2] = 0x9A,
  390. .entry[1].ie_id = WLAN_EID_HT_OPERATION,
  391. .entry[1].flags = WSM_BEACON_FILTER_IE_HAS_CHANGED |
  392. WSM_BEACON_FILTER_IE_NO_LONGER_PRESENT |
  393. WSM_BEACON_FILTER_IE_HAS_APPEARED,
  394. .entry[2].ie_id = WLAN_EID_ERP_INFO,
  395. .entry[2].flags = WSM_BEACON_FILTER_IE_HAS_CHANGED |
  396. WSM_BEACON_FILTER_IE_NO_LONGER_PRESENT |
  397. WSM_BEACON_FILTER_IE_HAS_APPEARED,
  398. };
  399. if (priv->join_status == CW1200_JOIN_STATUS_PASSIVE)
  400. return;
  401. else if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
  402. bssid_filtering = false;
  403. if (priv->disable_beacon_filter) {
  404. bf_ctrl.enabled = 0;
  405. bf_ctrl.bcn_count = 1;
  406. bf_tbl.num = __cpu_to_le32(0);
  407. } else if (is_p2p || !is_sta) {
  408. bf_ctrl.enabled = WSM_BEACON_FILTER_ENABLE |
  409. WSM_BEACON_FILTER_AUTO_ERP;
  410. bf_ctrl.bcn_count = 0;
  411. bf_tbl.num = __cpu_to_le32(2);
  412. } else {
  413. bf_ctrl.enabled = WSM_BEACON_FILTER_ENABLE;
  414. bf_ctrl.bcn_count = 0;
  415. bf_tbl.num = __cpu_to_le32(3);
  416. }
  417. /* When acting as p2p client being connected to p2p GO, in order to
  418. * receive frames from a different p2p device, turn off bssid filter.
  419. *
  420. * WARNING: FW dependency!
  421. * This can only be used with FW WSM371 and its successors.
  422. * In that FW version even with bssid filter turned off,
  423. * device will block most of the unwanted frames.
  424. */
  425. if (is_p2p)
  426. bssid_filtering = false;
  427. ret = wsm_set_rx_filter(priv, &priv->rx_filter);
  428. if (!ret)
  429. ret = wsm_set_beacon_filter_table(priv, &bf_tbl);
  430. if (!ret)
  431. ret = wsm_beacon_filter_control(priv, &bf_ctrl);
  432. if (!ret)
  433. ret = wsm_set_bssid_filtering(priv, bssid_filtering);
  434. if (!ret)
  435. ret = wsm_set_multicast_filter(priv, &priv->multicast_filter);
  436. if (ret)
  437. wiphy_err(priv->hw->wiphy,
  438. "Update filtering failed: %d.\n", ret);
  439. return;
  440. }
  441. void cw1200_update_filtering_work(struct work_struct *work)
  442. {
  443. struct cw1200_common *priv =
  444. container_of(work, struct cw1200_common,
  445. update_filtering_work);
  446. cw1200_update_filtering(priv);
  447. }
  448. void cw1200_set_beacon_wakeup_period_work(struct work_struct *work)
  449. {
  450. struct cw1200_common *priv =
  451. container_of(work, struct cw1200_common,
  452. set_beacon_wakeup_period_work);
  453. wsm_set_beacon_wakeup_period(priv,
  454. priv->beacon_int * priv->join_dtim_period >
  455. MAX_BEACON_SKIP_TIME_MS ? 1 :
  456. priv->join_dtim_period, 0);
  457. }
  458. u64 cw1200_prepare_multicast(struct ieee80211_hw *hw,
  459. struct netdev_hw_addr_list *mc_list)
  460. {
  461. static u8 broadcast_ipv6[ETH_ALEN] = {
  462. 0x33, 0x33, 0x00, 0x00, 0x00, 0x01
  463. };
  464. static u8 broadcast_ipv4[ETH_ALEN] = {
  465. 0x01, 0x00, 0x5e, 0x00, 0x00, 0x01
  466. };
  467. struct cw1200_common *priv = hw->priv;
  468. struct netdev_hw_addr *ha;
  469. int count = 0;
  470. /* Disable multicast filtering */
  471. priv->has_multicast_subscription = false;
  472. memset(&priv->multicast_filter, 0x00, sizeof(priv->multicast_filter));
  473. if (netdev_hw_addr_list_count(mc_list) > WSM_MAX_GRP_ADDRTABLE_ENTRIES)
  474. return 0;
  475. /* Enable if requested */
  476. netdev_hw_addr_list_for_each(ha, mc_list) {
  477. pr_debug("[STA] multicast: %pM\n", ha->addr);
  478. memcpy(&priv->multicast_filter.macaddrs[count],
  479. ha->addr, ETH_ALEN);
  480. if (!ether_addr_equal(ha->addr, broadcast_ipv4) &&
  481. !ether_addr_equal(ha->addr, broadcast_ipv6))
  482. priv->has_multicast_subscription = true;
  483. count++;
  484. }
  485. if (count) {
  486. priv->multicast_filter.enable = __cpu_to_le32(1);
  487. priv->multicast_filter.num_addrs = __cpu_to_le32(count);
  488. }
  489. return netdev_hw_addr_list_count(mc_list);
  490. }
  491. void cw1200_configure_filter(struct ieee80211_hw *dev,
  492. unsigned int changed_flags,
  493. unsigned int *total_flags,
  494. u64 multicast)
  495. {
  496. struct cw1200_common *priv = dev->priv;
  497. bool listening = !!(*total_flags &
  498. (FIF_PROMISC_IN_BSS |
  499. FIF_OTHER_BSS |
  500. FIF_BCN_PRBRESP_PROMISC |
  501. FIF_PROBE_REQ));
  502. *total_flags &= FIF_PROMISC_IN_BSS |
  503. FIF_OTHER_BSS |
  504. FIF_FCSFAIL |
  505. FIF_BCN_PRBRESP_PROMISC |
  506. FIF_PROBE_REQ;
  507. down(&priv->scan.lock);
  508. mutex_lock(&priv->conf_mutex);
  509. priv->rx_filter.promiscuous = (*total_flags & FIF_PROMISC_IN_BSS)
  510. ? 1 : 0;
  511. priv->rx_filter.bssid = (*total_flags & (FIF_OTHER_BSS |
  512. FIF_PROBE_REQ)) ? 1 : 0;
  513. priv->rx_filter.fcs = (*total_flags & FIF_FCSFAIL) ? 1 : 0;
  514. priv->disable_beacon_filter = !(*total_flags &
  515. (FIF_BCN_PRBRESP_PROMISC |
  516. FIF_PROMISC_IN_BSS |
  517. FIF_PROBE_REQ));
  518. if (priv->listening != listening) {
  519. priv->listening = listening;
  520. wsm_lock_tx(priv);
  521. cw1200_update_listening(priv, listening);
  522. wsm_unlock_tx(priv);
  523. }
  524. cw1200_update_filtering(priv);
  525. mutex_unlock(&priv->conf_mutex);
  526. up(&priv->scan.lock);
  527. }
  528. int cw1200_conf_tx(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  529. u16 queue, const struct ieee80211_tx_queue_params *params)
  530. {
  531. struct cw1200_common *priv = dev->priv;
  532. int ret = 0;
  533. /* To prevent re-applying PM request OID again and again*/
  534. bool old_uapsd_flags;
  535. mutex_lock(&priv->conf_mutex);
  536. if (queue < dev->queues) {
  537. old_uapsd_flags = le16_to_cpu(priv->uapsd_info.uapsd_flags);
  538. WSM_TX_QUEUE_SET(&priv->tx_queue_params, queue, 0, 0, 0);
  539. ret = wsm_set_tx_queue_params(priv,
  540. &priv->tx_queue_params.params[queue], queue);
  541. if (ret) {
  542. ret = -EINVAL;
  543. goto out;
  544. }
  545. WSM_EDCA_SET(&priv->edca, queue, params->aifs,
  546. params->cw_min, params->cw_max,
  547. params->txop, 0xc8,
  548. params->uapsd);
  549. ret = wsm_set_edca_params(priv, &priv->edca);
  550. if (ret) {
  551. ret = -EINVAL;
  552. goto out;
  553. }
  554. if (priv->mode == NL80211_IFTYPE_STATION) {
  555. ret = cw1200_set_uapsd_param(priv, &priv->edca);
  556. if (!ret && priv->setbssparams_done &&
  557. (priv->join_status == CW1200_JOIN_STATUS_STA) &&
  558. (old_uapsd_flags != le16_to_cpu(priv->uapsd_info.uapsd_flags)))
  559. ret = cw1200_set_pm(priv, &priv->powersave_mode);
  560. }
  561. } else {
  562. ret = -EINVAL;
  563. }
  564. out:
  565. mutex_unlock(&priv->conf_mutex);
  566. return ret;
  567. }
  568. int cw1200_get_stats(struct ieee80211_hw *dev,
  569. struct ieee80211_low_level_stats *stats)
  570. {
  571. struct cw1200_common *priv = dev->priv;
  572. memcpy(stats, &priv->stats, sizeof(*stats));
  573. return 0;
  574. }
  575. int cw1200_set_pm(struct cw1200_common *priv, const struct wsm_set_pm *arg)
  576. {
  577. struct wsm_set_pm pm = *arg;
  578. if (priv->uapsd_info.uapsd_flags != 0)
  579. pm.mode &= ~WSM_PSM_FAST_PS_FLAG;
  580. if (memcmp(&pm, &priv->firmware_ps_mode,
  581. sizeof(struct wsm_set_pm))) {
  582. priv->firmware_ps_mode = pm;
  583. return wsm_set_pm(priv, &pm);
  584. } else {
  585. return 0;
  586. }
  587. }
  588. int cw1200_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
  589. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  590. struct ieee80211_key_conf *key)
  591. {
  592. int ret = -EOPNOTSUPP;
  593. struct cw1200_common *priv = dev->priv;
  594. struct ieee80211_key_seq seq;
  595. mutex_lock(&priv->conf_mutex);
  596. if (cmd == SET_KEY) {
  597. u8 *peer_addr = NULL;
  598. int pairwise = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) ?
  599. 1 : 0;
  600. int idx = cw1200_alloc_key(priv);
  601. struct wsm_add_key *wsm_key = &priv->keys[idx];
  602. if (idx < 0) {
  603. ret = -EINVAL;
  604. goto finally;
  605. }
  606. if (sta)
  607. peer_addr = sta->addr;
  608. key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
  609. switch (key->cipher) {
  610. case WLAN_CIPHER_SUITE_WEP40:
  611. case WLAN_CIPHER_SUITE_WEP104:
  612. if (key->keylen > 16) {
  613. cw1200_free_key(priv, idx);
  614. ret = -EINVAL;
  615. goto finally;
  616. }
  617. if (pairwise) {
  618. wsm_key->type = WSM_KEY_TYPE_WEP_PAIRWISE;
  619. memcpy(wsm_key->wep_pairwise.peer,
  620. peer_addr, ETH_ALEN);
  621. memcpy(wsm_key->wep_pairwise.keydata,
  622. &key->key[0], key->keylen);
  623. wsm_key->wep_pairwise.keylen = key->keylen;
  624. } else {
  625. wsm_key->type = WSM_KEY_TYPE_WEP_DEFAULT;
  626. memcpy(wsm_key->wep_group.keydata,
  627. &key->key[0], key->keylen);
  628. wsm_key->wep_group.keylen = key->keylen;
  629. wsm_key->wep_group.keyid = key->keyidx;
  630. }
  631. break;
  632. case WLAN_CIPHER_SUITE_TKIP:
  633. ieee80211_get_key_rx_seq(key, 0, &seq);
  634. if (pairwise) {
  635. wsm_key->type = WSM_KEY_TYPE_TKIP_PAIRWISE;
  636. memcpy(wsm_key->tkip_pairwise.peer,
  637. peer_addr, ETH_ALEN);
  638. memcpy(wsm_key->tkip_pairwise.keydata,
  639. &key->key[0], 16);
  640. memcpy(wsm_key->tkip_pairwise.tx_mic_key,
  641. &key->key[16], 8);
  642. memcpy(wsm_key->tkip_pairwise.rx_mic_key,
  643. &key->key[24], 8);
  644. } else {
  645. size_t mic_offset =
  646. (priv->mode == NL80211_IFTYPE_AP) ?
  647. 16 : 24;
  648. wsm_key->type = WSM_KEY_TYPE_TKIP_GROUP;
  649. memcpy(wsm_key->tkip_group.keydata,
  650. &key->key[0], 16);
  651. memcpy(wsm_key->tkip_group.rx_mic_key,
  652. &key->key[mic_offset], 8);
  653. wsm_key->tkip_group.rx_seqnum[0] = seq.tkip.iv16 & 0xff;
  654. wsm_key->tkip_group.rx_seqnum[1] = (seq.tkip.iv16 >> 8) & 0xff;
  655. wsm_key->tkip_group.rx_seqnum[2] = seq.tkip.iv32 & 0xff;
  656. wsm_key->tkip_group.rx_seqnum[3] = (seq.tkip.iv32 >> 8) & 0xff;
  657. wsm_key->tkip_group.rx_seqnum[4] = (seq.tkip.iv32 >> 16) & 0xff;
  658. wsm_key->tkip_group.rx_seqnum[5] = (seq.tkip.iv32 >> 24) & 0xff;
  659. wsm_key->tkip_group.rx_seqnum[6] = 0;
  660. wsm_key->tkip_group.rx_seqnum[7] = 0;
  661. wsm_key->tkip_group.keyid = key->keyidx;
  662. }
  663. break;
  664. case WLAN_CIPHER_SUITE_CCMP:
  665. ieee80211_get_key_rx_seq(key, 0, &seq);
  666. if (pairwise) {
  667. wsm_key->type = WSM_KEY_TYPE_AES_PAIRWISE;
  668. memcpy(wsm_key->aes_pairwise.peer,
  669. peer_addr, ETH_ALEN);
  670. memcpy(wsm_key->aes_pairwise.keydata,
  671. &key->key[0], 16);
  672. } else {
  673. wsm_key->type = WSM_KEY_TYPE_AES_GROUP;
  674. memcpy(wsm_key->aes_group.keydata,
  675. &key->key[0], 16);
  676. wsm_key->aes_group.rx_seqnum[0] = seq.ccmp.pn[5];
  677. wsm_key->aes_group.rx_seqnum[1] = seq.ccmp.pn[4];
  678. wsm_key->aes_group.rx_seqnum[2] = seq.ccmp.pn[3];
  679. wsm_key->aes_group.rx_seqnum[3] = seq.ccmp.pn[2];
  680. wsm_key->aes_group.rx_seqnum[4] = seq.ccmp.pn[1];
  681. wsm_key->aes_group.rx_seqnum[5] = seq.ccmp.pn[0];
  682. wsm_key->aes_group.rx_seqnum[6] = 0;
  683. wsm_key->aes_group.rx_seqnum[7] = 0;
  684. wsm_key->aes_group.keyid = key->keyidx;
  685. }
  686. break;
  687. case WLAN_CIPHER_SUITE_SMS4:
  688. if (pairwise) {
  689. wsm_key->type = WSM_KEY_TYPE_WAPI_PAIRWISE;
  690. memcpy(wsm_key->wapi_pairwise.peer,
  691. peer_addr, ETH_ALEN);
  692. memcpy(wsm_key->wapi_pairwise.keydata,
  693. &key->key[0], 16);
  694. memcpy(wsm_key->wapi_pairwise.mic_key,
  695. &key->key[16], 16);
  696. wsm_key->wapi_pairwise.keyid = key->keyidx;
  697. } else {
  698. wsm_key->type = WSM_KEY_TYPE_WAPI_GROUP;
  699. memcpy(wsm_key->wapi_group.keydata,
  700. &key->key[0], 16);
  701. memcpy(wsm_key->wapi_group.mic_key,
  702. &key->key[16], 16);
  703. wsm_key->wapi_group.keyid = key->keyidx;
  704. }
  705. break;
  706. default:
  707. pr_warn("Unhandled key type %d\n", key->cipher);
  708. cw1200_free_key(priv, idx);
  709. ret = -EOPNOTSUPP;
  710. goto finally;
  711. }
  712. ret = wsm_add_key(priv, wsm_key);
  713. if (!ret)
  714. key->hw_key_idx = idx;
  715. else
  716. cw1200_free_key(priv, idx);
  717. } else if (cmd == DISABLE_KEY) {
  718. struct wsm_remove_key wsm_key = {
  719. .index = key->hw_key_idx,
  720. };
  721. if (wsm_key.index > WSM_KEY_MAX_INDEX) {
  722. ret = -EINVAL;
  723. goto finally;
  724. }
  725. cw1200_free_key(priv, wsm_key.index);
  726. ret = wsm_remove_key(priv, &wsm_key);
  727. } else {
  728. pr_warn("Unhandled key command %d\n", cmd);
  729. }
  730. finally:
  731. mutex_unlock(&priv->conf_mutex);
  732. return ret;
  733. }
  734. void cw1200_wep_key_work(struct work_struct *work)
  735. {
  736. struct cw1200_common *priv =
  737. container_of(work, struct cw1200_common, wep_key_work);
  738. u8 queue_id = cw1200_queue_get_queue_id(priv->pending_frame_id);
  739. struct cw1200_queue *queue = &priv->tx_queue[queue_id];
  740. __le32 wep_default_key_id = __cpu_to_le32(
  741. priv->wep_default_key_id);
  742. pr_debug("[STA] Setting default WEP key: %d\n",
  743. priv->wep_default_key_id);
  744. wsm_flush_tx(priv);
  745. wsm_write_mib(priv, WSM_MIB_ID_DOT11_WEP_DEFAULT_KEY_ID,
  746. &wep_default_key_id, sizeof(wep_default_key_id));
  747. cw1200_queue_requeue(queue, priv->pending_frame_id);
  748. wsm_unlock_tx(priv);
  749. }
  750. int cw1200_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  751. {
  752. int ret = 0;
  753. __le32 val32;
  754. struct cw1200_common *priv = hw->priv;
  755. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  756. return 0;
  757. if (value != (u32) -1)
  758. val32 = __cpu_to_le32(value);
  759. else
  760. val32 = 0; /* disabled */
  761. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED) {
  762. /* device is down, can _not_ set threshold */
  763. ret = -ENODEV;
  764. goto out;
  765. }
  766. if (priv->rts_threshold == value)
  767. goto out;
  768. pr_debug("[STA] Setting RTS threshold: %d\n",
  769. priv->rts_threshold);
  770. /* mutex_lock(&priv->conf_mutex); */
  771. ret = wsm_write_mib(priv, WSM_MIB_ID_DOT11_RTS_THRESHOLD,
  772. &val32, sizeof(val32));
  773. if (!ret)
  774. priv->rts_threshold = value;
  775. /* mutex_unlock(&priv->conf_mutex); */
  776. out:
  777. return ret;
  778. }
  779. /* If successful, LOCKS the TX queue! */
  780. static int __cw1200_flush(struct cw1200_common *priv, bool drop)
  781. {
  782. int i, ret;
  783. for (;;) {
  784. /* TODO: correct flush handling is required when dev_stop.
  785. * Temporary workaround: 2s
  786. */
  787. if (drop) {
  788. for (i = 0; i < 4; ++i)
  789. cw1200_queue_clear(&priv->tx_queue[i]);
  790. } else {
  791. ret = wait_event_timeout(
  792. priv->tx_queue_stats.wait_link_id_empty,
  793. cw1200_queue_stats_is_empty(
  794. &priv->tx_queue_stats, -1),
  795. 2 * HZ);
  796. }
  797. if (!drop && ret <= 0) {
  798. ret = -ETIMEDOUT;
  799. break;
  800. } else {
  801. ret = 0;
  802. }
  803. wsm_lock_tx(priv);
  804. if (!cw1200_queue_stats_is_empty(&priv->tx_queue_stats, -1)) {
  805. /* Highly unlikely: WSM requeued frames. */
  806. wsm_unlock_tx(priv);
  807. continue;
  808. }
  809. break;
  810. }
  811. return ret;
  812. }
  813. void cw1200_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  814. u32 queues, bool drop)
  815. {
  816. struct cw1200_common *priv = hw->priv;
  817. switch (priv->mode) {
  818. case NL80211_IFTYPE_MONITOR:
  819. drop = true;
  820. break;
  821. case NL80211_IFTYPE_AP:
  822. if (!priv->enable_beacon)
  823. drop = true;
  824. break;
  825. }
  826. if (!__cw1200_flush(priv, drop))
  827. wsm_unlock_tx(priv);
  828. return;
  829. }
  830. /* ******************************************************************** */
  831. /* WSM callbacks */
  832. void cw1200_free_event_queue(struct cw1200_common *priv)
  833. {
  834. LIST_HEAD(list);
  835. spin_lock(&priv->event_queue_lock);
  836. list_splice_init(&priv->event_queue, &list);
  837. spin_unlock(&priv->event_queue_lock);
  838. __cw1200_free_event_queue(&list);
  839. }
  840. void cw1200_event_handler(struct work_struct *work)
  841. {
  842. struct cw1200_common *priv =
  843. container_of(work, struct cw1200_common, event_handler);
  844. struct cw1200_wsm_event *event;
  845. LIST_HEAD(list);
  846. spin_lock(&priv->event_queue_lock);
  847. list_splice_init(&priv->event_queue, &list);
  848. spin_unlock(&priv->event_queue_lock);
  849. list_for_each_entry(event, &list, link) {
  850. switch (event->evt.id) {
  851. case WSM_EVENT_ERROR:
  852. pr_err("Unhandled WSM Error from LMAC\n");
  853. break;
  854. case WSM_EVENT_BSS_LOST:
  855. pr_debug("[CQM] BSS lost.\n");
  856. cancel_work_sync(&priv->unjoin_work);
  857. if (!down_trylock(&priv->scan.lock)) {
  858. cw1200_cqm_bssloss_sm(priv, 1, 0, 0);
  859. up(&priv->scan.lock);
  860. } else {
  861. /* Scan is in progress. Delay reporting.
  862. * Scan complete will trigger bss_loss_work
  863. */
  864. priv->delayed_link_loss = 1;
  865. /* Also start a watchdog. */
  866. queue_delayed_work(priv->workqueue,
  867. &priv->bss_loss_work, 5*HZ);
  868. }
  869. break;
  870. case WSM_EVENT_BSS_REGAINED:
  871. pr_debug("[CQM] BSS regained.\n");
  872. cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
  873. cancel_work_sync(&priv->unjoin_work);
  874. break;
  875. case WSM_EVENT_RADAR_DETECTED:
  876. wiphy_info(priv->hw->wiphy, "radar pulse detected\n");
  877. break;
  878. case WSM_EVENT_RCPI_RSSI:
  879. {
  880. /* RSSI: signed Q8.0, RCPI: unsigned Q7.1
  881. * RSSI = RCPI / 2 - 110
  882. */
  883. int rcpi_rssi = (int)(event->evt.data & 0xFF);
  884. int cqm_evt;
  885. if (priv->cqm_use_rssi)
  886. rcpi_rssi = (s8)rcpi_rssi;
  887. else
  888. rcpi_rssi = rcpi_rssi / 2 - 110;
  889. cqm_evt = (rcpi_rssi <= priv->cqm_rssi_thold) ?
  890. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW :
  891. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
  892. pr_debug("[CQM] RSSI event: %d.\n", rcpi_rssi);
  893. ieee80211_cqm_rssi_notify(priv->vif, cqm_evt,
  894. GFP_KERNEL);
  895. break;
  896. }
  897. case WSM_EVENT_BT_INACTIVE:
  898. pr_warn("Unhandled BT INACTIVE from LMAC\n");
  899. break;
  900. case WSM_EVENT_BT_ACTIVE:
  901. pr_warn("Unhandled BT ACTIVE from LMAC\n");
  902. break;
  903. }
  904. }
  905. __cw1200_free_event_queue(&list);
  906. }
  907. void cw1200_bss_loss_work(struct work_struct *work)
  908. {
  909. struct cw1200_common *priv =
  910. container_of(work, struct cw1200_common, bss_loss_work.work);
  911. pr_debug("[CQM] Reporting connection loss.\n");
  912. wsm_lock_tx(priv);
  913. if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
  914. wsm_unlock_tx(priv);
  915. }
  916. void cw1200_bss_params_work(struct work_struct *work)
  917. {
  918. struct cw1200_common *priv =
  919. container_of(work, struct cw1200_common, bss_params_work);
  920. mutex_lock(&priv->conf_mutex);
  921. priv->bss_params.reset_beacon_loss = 1;
  922. wsm_set_bss_params(priv, &priv->bss_params);
  923. priv->bss_params.reset_beacon_loss = 0;
  924. mutex_unlock(&priv->conf_mutex);
  925. }
  926. /* ******************************************************************** */
  927. /* Internal API */
  928. /* This function is called to Parse the SDD file
  929. * to extract listen_interval and PTA related information
  930. * sdd is a TLV: u8 id, u8 len, u8 data[]
  931. */
  932. static int cw1200_parse_sdd_file(struct cw1200_common *priv)
  933. {
  934. const u8 *p = priv->sdd->data;
  935. int ret = 0;
  936. while (p + 2 <= priv->sdd->data + priv->sdd->size) {
  937. if (p + p[1] + 2 > priv->sdd->data + priv->sdd->size) {
  938. pr_warn("Malformed sdd structure\n");
  939. return -1;
  940. }
  941. switch (p[0]) {
  942. case SDD_PTA_CFG_ELT_ID: {
  943. u16 v;
  944. if (p[1] < 4) {
  945. pr_warn("SDD_PTA_CFG_ELT_ID malformed\n");
  946. ret = -1;
  947. break;
  948. }
  949. v = le16_to_cpu(*((__le16 *)(p + 2)));
  950. if (!v) /* non-zero means this is enabled */
  951. break;
  952. v = le16_to_cpu(*((__le16 *)(p + 4)));
  953. priv->conf_listen_interval = (v >> 7) & 0x1F;
  954. pr_debug("PTA found; Listen Interval %d\n",
  955. priv->conf_listen_interval);
  956. break;
  957. }
  958. case SDD_REFERENCE_FREQUENCY_ELT_ID: {
  959. u16 clk = le16_to_cpu(*((__le16 *)(p + 2)));
  960. if (clk != priv->hw_refclk)
  961. pr_warn("SDD file doesn't match configured refclk (%d vs %d)\n",
  962. clk, priv->hw_refclk);
  963. break;
  964. }
  965. default:
  966. break;
  967. }
  968. p += p[1] + 2;
  969. }
  970. if (!priv->bt_present) {
  971. pr_debug("PTA element NOT found.\n");
  972. priv->conf_listen_interval = 0;
  973. }
  974. return ret;
  975. }
  976. int cw1200_setup_mac(struct cw1200_common *priv)
  977. {
  978. int ret = 0;
  979. /* NOTE: There is a bug in FW: it reports signal
  980. * as RSSI if RSSI subscription is enabled.
  981. * It's not enough to set WSM_RCPI_RSSI_USE_RSSI.
  982. *
  983. * NOTE2: RSSI based reports have been switched to RCPI, since
  984. * FW has a bug and RSSI reported values are not stable,
  985. * what can leads to signal level oscilations in user-end applications
  986. */
  987. struct wsm_rcpi_rssi_threshold threshold = {
  988. .rssiRcpiMode = WSM_RCPI_RSSI_THRESHOLD_ENABLE |
  989. WSM_RCPI_RSSI_DONT_USE_UPPER |
  990. WSM_RCPI_RSSI_DONT_USE_LOWER,
  991. .rollingAverageCount = 16,
  992. };
  993. struct wsm_configuration cfg = {
  994. .dot11StationId = &priv->mac_addr[0],
  995. };
  996. /* Remember the decission here to make sure, we will handle
  997. * the RCPI/RSSI value correctly on WSM_EVENT_RCPI_RSS
  998. */
  999. if (threshold.rssiRcpiMode & WSM_RCPI_RSSI_USE_RSSI)
  1000. priv->cqm_use_rssi = true;
  1001. if (!priv->sdd) {
  1002. ret = request_firmware(&priv->sdd, priv->sdd_path, priv->pdev);
  1003. if (ret) {
  1004. pr_err("Can't load sdd file %s.\n", priv->sdd_path);
  1005. return ret;
  1006. }
  1007. cw1200_parse_sdd_file(priv);
  1008. }
  1009. cfg.dpdData = priv->sdd->data;
  1010. cfg.dpdData_size = priv->sdd->size;
  1011. ret = wsm_configuration(priv, &cfg);
  1012. if (ret)
  1013. return ret;
  1014. /* Configure RSSI/SCPI reporting as RSSI. */
  1015. wsm_set_rcpi_rssi_threshold(priv, &threshold);
  1016. return 0;
  1017. }
  1018. static void cw1200_join_complete(struct cw1200_common *priv)
  1019. {
  1020. pr_debug("[STA] Join complete (%d)\n", priv->join_complete_status);
  1021. priv->join_pending = false;
  1022. if (priv->join_complete_status) {
  1023. priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
  1024. cw1200_update_listening(priv, priv->listening);
  1025. cw1200_do_unjoin(priv);
  1026. ieee80211_connection_loss(priv->vif);
  1027. } else {
  1028. if (priv->mode == NL80211_IFTYPE_ADHOC)
  1029. priv->join_status = CW1200_JOIN_STATUS_IBSS;
  1030. else
  1031. priv->join_status = CW1200_JOIN_STATUS_PRE_STA;
  1032. }
  1033. wsm_unlock_tx(priv); /* Clearing the lock held before do_join() */
  1034. }
  1035. void cw1200_join_complete_work(struct work_struct *work)
  1036. {
  1037. struct cw1200_common *priv =
  1038. container_of(work, struct cw1200_common, join_complete_work);
  1039. mutex_lock(&priv->conf_mutex);
  1040. cw1200_join_complete(priv);
  1041. mutex_unlock(&priv->conf_mutex);
  1042. }
  1043. void cw1200_join_complete_cb(struct cw1200_common *priv,
  1044. struct wsm_join_complete *arg)
  1045. {
  1046. pr_debug("[STA] cw1200_join_complete_cb called, status=%d.\n",
  1047. arg->status);
  1048. if (cancel_delayed_work(&priv->join_timeout)) {
  1049. priv->join_complete_status = arg->status;
  1050. queue_work(priv->workqueue, &priv->join_complete_work);
  1051. }
  1052. }
  1053. /* MUST be called with tx_lock held! It will be unlocked for us. */
  1054. static void cw1200_do_join(struct cw1200_common *priv)
  1055. {
  1056. const u8 *bssid;
  1057. struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
  1058. struct cfg80211_bss *bss = NULL;
  1059. struct wsm_protected_mgmt_policy mgmt_policy;
  1060. struct wsm_join join = {
  1061. .mode = conf->ibss_joined ?
  1062. WSM_JOIN_MODE_IBSS : WSM_JOIN_MODE_BSS,
  1063. .preamble_type = WSM_JOIN_PREAMBLE_LONG,
  1064. .probe_for_join = 1,
  1065. .atim_window = 0,
  1066. .basic_rate_set = cw1200_rate_mask_to_wsm(priv,
  1067. conf->basic_rates),
  1068. };
  1069. if (delayed_work_pending(&priv->join_timeout)) {
  1070. pr_warn("[STA] - Join request already pending, skipping..\n");
  1071. wsm_unlock_tx(priv);
  1072. return;
  1073. }
  1074. if (priv->join_status)
  1075. cw1200_do_unjoin(priv);
  1076. bssid = priv->vif->bss_conf.bssid;
  1077. bss = cfg80211_get_bss(priv->hw->wiphy, priv->channel,
  1078. bssid, NULL, 0, 0, 0);
  1079. if (!bss && !conf->ibss_joined) {
  1080. wsm_unlock_tx(priv);
  1081. return;
  1082. }
  1083. mutex_lock(&priv->conf_mutex);
  1084. /* Under the conf lock: check scan status and
  1085. * bail out if it is in progress.
  1086. */
  1087. if (atomic_read(&priv->scan.in_progress)) {
  1088. wsm_unlock_tx(priv);
  1089. goto done_put;
  1090. }
  1091. priv->join_pending = true;
  1092. /* Sanity check basic rates */
  1093. if (!join.basic_rate_set)
  1094. join.basic_rate_set = 7;
  1095. /* Sanity check beacon interval */
  1096. if (!priv->beacon_int)
  1097. priv->beacon_int = 1;
  1098. join.beacon_interval = priv->beacon_int;
  1099. /* BT Coex related changes */
  1100. if (priv->bt_present) {
  1101. if (((priv->conf_listen_interval * 100) %
  1102. priv->beacon_int) == 0)
  1103. priv->listen_interval =
  1104. ((priv->conf_listen_interval * 100) /
  1105. priv->beacon_int);
  1106. else
  1107. priv->listen_interval =
  1108. ((priv->conf_listen_interval * 100) /
  1109. priv->beacon_int + 1);
  1110. }
  1111. if (priv->hw->conf.ps_dtim_period)
  1112. priv->join_dtim_period = priv->hw->conf.ps_dtim_period;
  1113. join.dtim_period = priv->join_dtim_period;
  1114. join.channel_number = priv->channel->hw_value;
  1115. join.band = (priv->channel->band == IEEE80211_BAND_5GHZ) ?
  1116. WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
  1117. memcpy(join.bssid, bssid, sizeof(join.bssid));
  1118. pr_debug("[STA] Join BSSID: %pM DTIM: %d, interval: %d\n",
  1119. join.bssid,
  1120. join.dtim_period, priv->beacon_int);
  1121. if (!conf->ibss_joined) {
  1122. const u8 *ssidie;
  1123. rcu_read_lock();
  1124. ssidie = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
  1125. if (ssidie) {
  1126. join.ssid_len = ssidie[1];
  1127. memcpy(join.ssid, &ssidie[2], join.ssid_len);
  1128. }
  1129. rcu_read_unlock();
  1130. }
  1131. if (priv->vif->p2p) {
  1132. join.flags |= WSM_JOIN_FLAGS_P2P_GO;
  1133. join.basic_rate_set =
  1134. cw1200_rate_mask_to_wsm(priv, 0xFF0);
  1135. }
  1136. /* Enable asynchronous join calls */
  1137. if (!conf->ibss_joined) {
  1138. join.flags |= WSM_JOIN_FLAGS_FORCE;
  1139. join.flags |= WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND;
  1140. }
  1141. wsm_flush_tx(priv);
  1142. /* Stay Awake for Join and Auth Timeouts and a bit more */
  1143. cw1200_pm_stay_awake(&priv->pm_state,
  1144. CW1200_JOIN_TIMEOUT + CW1200_AUTH_TIMEOUT);
  1145. cw1200_update_listening(priv, false);
  1146. /* Turn on Block ACKs */
  1147. wsm_set_block_ack_policy(priv, priv->ba_tx_tid_mask,
  1148. priv->ba_rx_tid_mask);
  1149. /* Set up timeout */
  1150. if (join.flags & WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND) {
  1151. priv->join_status = CW1200_JOIN_STATUS_JOINING;
  1152. queue_delayed_work(priv->workqueue,
  1153. &priv->join_timeout,
  1154. CW1200_JOIN_TIMEOUT);
  1155. }
  1156. /* 802.11w protected mgmt frames */
  1157. mgmt_policy.protectedMgmtEnable = 0;
  1158. mgmt_policy.unprotectedMgmtFramesAllowed = 1;
  1159. mgmt_policy.encryptionForAuthFrame = 1;
  1160. wsm_set_protected_mgmt_policy(priv, &mgmt_policy);
  1161. /* Perform actual join */
  1162. if (wsm_join(priv, &join)) {
  1163. pr_err("[STA] cw1200_join_work: wsm_join failed!\n");
  1164. cancel_delayed_work_sync(&priv->join_timeout);
  1165. cw1200_update_listening(priv, priv->listening);
  1166. /* Tx lock still held, unjoin will clear it. */
  1167. if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
  1168. wsm_unlock_tx(priv);
  1169. } else {
  1170. if (!(join.flags & WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND))
  1171. cw1200_join_complete(priv); /* Will clear tx_lock */
  1172. /* Upload keys */
  1173. cw1200_upload_keys(priv);
  1174. /* Due to beacon filtering it is possible that the
  1175. * AP's beacon is not known for the mac80211 stack.
  1176. * Disable filtering temporary to make sure the stack
  1177. * receives at least one
  1178. */
  1179. priv->disable_beacon_filter = true;
  1180. }
  1181. cw1200_update_filtering(priv);
  1182. done_put:
  1183. mutex_unlock(&priv->conf_mutex);
  1184. if (bss)
  1185. cfg80211_put_bss(priv->hw->wiphy, bss);
  1186. }
  1187. void cw1200_join_timeout(struct work_struct *work)
  1188. {
  1189. struct cw1200_common *priv =
  1190. container_of(work, struct cw1200_common, join_timeout.work);
  1191. pr_debug("[WSM] Join timed out.\n");
  1192. wsm_lock_tx(priv);
  1193. if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
  1194. wsm_unlock_tx(priv);
  1195. }
  1196. static void cw1200_do_unjoin(struct cw1200_common *priv)
  1197. {
  1198. struct wsm_reset reset = {
  1199. .reset_statistics = true,
  1200. };
  1201. cancel_delayed_work_sync(&priv->join_timeout);
  1202. mutex_lock(&priv->conf_mutex);
  1203. priv->join_pending = false;
  1204. if (atomic_read(&priv->scan.in_progress)) {
  1205. if (priv->delayed_unjoin)
  1206. wiphy_dbg(priv->hw->wiphy, "Delayed unjoin is already scheduled.\n");
  1207. else
  1208. priv->delayed_unjoin = true;
  1209. goto done;
  1210. }
  1211. priv->delayed_link_loss = false;
  1212. if (!priv->join_status)
  1213. goto done;
  1214. if (priv->join_status == CW1200_JOIN_STATUS_AP)
  1215. goto done;
  1216. cancel_work_sync(&priv->update_filtering_work);
  1217. cancel_work_sync(&priv->set_beacon_wakeup_period_work);
  1218. priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
  1219. /* Unjoin is a reset. */
  1220. wsm_flush_tx(priv);
  1221. wsm_keep_alive_period(priv, 0);
  1222. wsm_reset(priv, &reset);
  1223. wsm_set_output_power(priv, priv->output_power * 10);
  1224. priv->join_dtim_period = 0;
  1225. cw1200_setup_mac(priv);
  1226. cw1200_free_event_queue(priv);
  1227. cancel_work_sync(&priv->event_handler);
  1228. cw1200_update_listening(priv, priv->listening);
  1229. cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
  1230. /* Disable Block ACKs */
  1231. wsm_set_block_ack_policy(priv, 0, 0);
  1232. priv->disable_beacon_filter = false;
  1233. cw1200_update_filtering(priv);
  1234. memset(&priv->association_mode, 0,
  1235. sizeof(priv->association_mode));
  1236. memset(&priv->bss_params, 0, sizeof(priv->bss_params));
  1237. priv->setbssparams_done = false;
  1238. memset(&priv->firmware_ps_mode, 0,
  1239. sizeof(priv->firmware_ps_mode));
  1240. pr_debug("[STA] Unjoin completed.\n");
  1241. done:
  1242. mutex_unlock(&priv->conf_mutex);
  1243. }
  1244. void cw1200_unjoin_work(struct work_struct *work)
  1245. {
  1246. struct cw1200_common *priv =
  1247. container_of(work, struct cw1200_common, unjoin_work);
  1248. cw1200_do_unjoin(priv);
  1249. /* Tell the stack we're dead */
  1250. ieee80211_connection_loss(priv->vif);
  1251. wsm_unlock_tx(priv);
  1252. }
  1253. int cw1200_enable_listening(struct cw1200_common *priv)
  1254. {
  1255. struct wsm_start start = {
  1256. .mode = WSM_START_MODE_P2P_DEV,
  1257. .band = WSM_PHY_BAND_2_4G,
  1258. .beacon_interval = 100,
  1259. .dtim_period = 1,
  1260. .probe_delay = 0,
  1261. .basic_rate_set = 0x0F,
  1262. };
  1263. if (priv->channel) {
  1264. start.band = priv->channel->band == IEEE80211_BAND_5GHZ ?
  1265. WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
  1266. start.channel_number = priv->channel->hw_value;
  1267. } else {
  1268. start.band = WSM_PHY_BAND_2_4G;
  1269. start.channel_number = 1;
  1270. }
  1271. return wsm_start(priv, &start);
  1272. }
  1273. int cw1200_disable_listening(struct cw1200_common *priv)
  1274. {
  1275. int ret;
  1276. struct wsm_reset reset = {
  1277. .reset_statistics = true,
  1278. };
  1279. ret = wsm_reset(priv, &reset);
  1280. return ret;
  1281. }
  1282. void cw1200_update_listening(struct cw1200_common *priv, bool enabled)
  1283. {
  1284. if (enabled) {
  1285. if (priv->join_status == CW1200_JOIN_STATUS_PASSIVE) {
  1286. if (!cw1200_enable_listening(priv))
  1287. priv->join_status = CW1200_JOIN_STATUS_MONITOR;
  1288. wsm_set_probe_responder(priv, true);
  1289. }
  1290. } else {
  1291. if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
  1292. if (!cw1200_disable_listening(priv))
  1293. priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
  1294. wsm_set_probe_responder(priv, false);
  1295. }
  1296. }
  1297. }
  1298. int cw1200_set_uapsd_param(struct cw1200_common *priv,
  1299. const struct wsm_edca_params *arg)
  1300. {
  1301. int ret;
  1302. u16 uapsd_flags = 0;
  1303. /* Here's the mapping AC [queue, bit]
  1304. * VO [0,3], VI [1, 2], BE [2, 1], BK [3, 0]
  1305. */
  1306. if (arg->uapsd_enable[0])
  1307. uapsd_flags |= 1 << 3;
  1308. if (arg->uapsd_enable[1])
  1309. uapsd_flags |= 1 << 2;
  1310. if (arg->uapsd_enable[2])
  1311. uapsd_flags |= 1 << 1;
  1312. if (arg->uapsd_enable[3])
  1313. uapsd_flags |= 1;
  1314. /* Currently pseudo U-APSD operation is not supported, so setting
  1315. * MinAutoTriggerInterval, MaxAutoTriggerInterval and
  1316. * AutoTriggerStep to 0
  1317. */
  1318. priv->uapsd_info.uapsd_flags = cpu_to_le16(uapsd_flags);
  1319. priv->uapsd_info.min_auto_trigger_interval = 0;
  1320. priv->uapsd_info.max_auto_trigger_interval = 0;
  1321. priv->uapsd_info.auto_trigger_step = 0;
  1322. ret = wsm_set_uapsd_info(priv, &priv->uapsd_info);
  1323. return ret;
  1324. }
  1325. /* ******************************************************************** */
  1326. /* AP API */
  1327. int cw1200_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1328. struct ieee80211_sta *sta)
  1329. {
  1330. struct cw1200_common *priv = hw->priv;
  1331. struct cw1200_sta_priv *sta_priv =
  1332. (struct cw1200_sta_priv *)&sta->drv_priv;
  1333. struct cw1200_link_entry *entry;
  1334. struct sk_buff *skb;
  1335. if (priv->mode != NL80211_IFTYPE_AP)
  1336. return 0;
  1337. sta_priv->link_id = cw1200_find_link_id(priv, sta->addr);
  1338. if (WARN_ON(!sta_priv->link_id)) {
  1339. wiphy_info(priv->hw->wiphy,
  1340. "[AP] No more link IDs available.\n");
  1341. return -ENOENT;
  1342. }
  1343. entry = &priv->link_id_db[sta_priv->link_id - 1];
  1344. spin_lock_bh(&priv->ps_state_lock);
  1345. if ((sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK) ==
  1346. IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
  1347. priv->sta_asleep_mask |= BIT(sta_priv->link_id);
  1348. entry->status = CW1200_LINK_HARD;
  1349. while ((skb = skb_dequeue(&entry->rx_queue)))
  1350. ieee80211_rx_irqsafe(priv->hw, skb);
  1351. spin_unlock_bh(&priv->ps_state_lock);
  1352. return 0;
  1353. }
  1354. int cw1200_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1355. struct ieee80211_sta *sta)
  1356. {
  1357. struct cw1200_common *priv = hw->priv;
  1358. struct cw1200_sta_priv *sta_priv =
  1359. (struct cw1200_sta_priv *)&sta->drv_priv;
  1360. struct cw1200_link_entry *entry;
  1361. if (priv->mode != NL80211_IFTYPE_AP || !sta_priv->link_id)
  1362. return 0;
  1363. entry = &priv->link_id_db[sta_priv->link_id - 1];
  1364. spin_lock_bh(&priv->ps_state_lock);
  1365. entry->status = CW1200_LINK_RESERVE;
  1366. entry->timestamp = jiffies;
  1367. wsm_lock_tx_async(priv);
  1368. if (queue_work(priv->workqueue, &priv->link_id_work) <= 0)
  1369. wsm_unlock_tx(priv);
  1370. spin_unlock_bh(&priv->ps_state_lock);
  1371. flush_workqueue(priv->workqueue);
  1372. return 0;
  1373. }
  1374. static void __cw1200_sta_notify(struct ieee80211_hw *dev,
  1375. struct ieee80211_vif *vif,
  1376. enum sta_notify_cmd notify_cmd,
  1377. int link_id)
  1378. {
  1379. struct cw1200_common *priv = dev->priv;
  1380. u32 bit, prev;
  1381. /* Zero link id means "for all link IDs" */
  1382. if (link_id)
  1383. bit = BIT(link_id);
  1384. else if (WARN_ON_ONCE(notify_cmd != STA_NOTIFY_AWAKE))
  1385. bit = 0;
  1386. else
  1387. bit = priv->link_id_map;
  1388. prev = priv->sta_asleep_mask & bit;
  1389. switch (notify_cmd) {
  1390. case STA_NOTIFY_SLEEP:
  1391. if (!prev) {
  1392. if (priv->buffered_multicasts &&
  1393. !priv->sta_asleep_mask)
  1394. queue_work(priv->workqueue,
  1395. &priv->multicast_start_work);
  1396. priv->sta_asleep_mask |= bit;
  1397. }
  1398. break;
  1399. case STA_NOTIFY_AWAKE:
  1400. if (prev) {
  1401. priv->sta_asleep_mask &= ~bit;
  1402. priv->pspoll_mask &= ~bit;
  1403. if (priv->tx_multicast && link_id &&
  1404. !priv->sta_asleep_mask)
  1405. queue_work(priv->workqueue,
  1406. &priv->multicast_stop_work);
  1407. cw1200_bh_wakeup(priv);
  1408. }
  1409. break;
  1410. }
  1411. }
  1412. void cw1200_sta_notify(struct ieee80211_hw *dev,
  1413. struct ieee80211_vif *vif,
  1414. enum sta_notify_cmd notify_cmd,
  1415. struct ieee80211_sta *sta)
  1416. {
  1417. struct cw1200_common *priv = dev->priv;
  1418. struct cw1200_sta_priv *sta_priv =
  1419. (struct cw1200_sta_priv *)&sta->drv_priv;
  1420. spin_lock_bh(&priv->ps_state_lock);
  1421. __cw1200_sta_notify(dev, vif, notify_cmd, sta_priv->link_id);
  1422. spin_unlock_bh(&priv->ps_state_lock);
  1423. }
  1424. static void cw1200_ps_notify(struct cw1200_common *priv,
  1425. int link_id, bool ps)
  1426. {
  1427. if (link_id > CW1200_MAX_STA_IN_AP_MODE)
  1428. return;
  1429. pr_debug("%s for LinkId: %d. STAs asleep: %.8X\n",
  1430. ps ? "Stop" : "Start",
  1431. link_id, priv->sta_asleep_mask);
  1432. __cw1200_sta_notify(priv->hw, priv->vif,
  1433. ps ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE, link_id);
  1434. }
  1435. static int cw1200_set_tim_impl(struct cw1200_common *priv, bool aid0_bit_set)
  1436. {
  1437. struct sk_buff *skb;
  1438. struct wsm_update_ie update_ie = {
  1439. .what = WSM_UPDATE_IE_BEACON,
  1440. .count = 1,
  1441. };
  1442. u16 tim_offset, tim_length;
  1443. pr_debug("[AP] mcast: %s.\n", aid0_bit_set ? "ena" : "dis");
  1444. skb = ieee80211_beacon_get_tim(priv->hw, priv->vif,
  1445. &tim_offset, &tim_length);
  1446. if (!skb) {
  1447. if (!__cw1200_flush(priv, true))
  1448. wsm_unlock_tx(priv);
  1449. return -ENOENT;
  1450. }
  1451. if (tim_offset && tim_length >= 6) {
  1452. /* Ignore DTIM count from mac80211:
  1453. * firmware handles DTIM internally.
  1454. */
  1455. skb->data[tim_offset + 2] = 0;
  1456. /* Set/reset aid0 bit */
  1457. if (aid0_bit_set)
  1458. skb->data[tim_offset + 4] |= 1;
  1459. else
  1460. skb->data[tim_offset + 4] &= ~1;
  1461. }
  1462. update_ie.ies = &skb->data[tim_offset];
  1463. update_ie.length = tim_length;
  1464. wsm_update_ie(priv, &update_ie);
  1465. dev_kfree_skb(skb);
  1466. return 0;
  1467. }
  1468. void cw1200_set_tim_work(struct work_struct *work)
  1469. {
  1470. struct cw1200_common *priv =
  1471. container_of(work, struct cw1200_common, set_tim_work);
  1472. (void)cw1200_set_tim_impl(priv, priv->aid0_bit_set);
  1473. }
  1474. int cw1200_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  1475. bool set)
  1476. {
  1477. struct cw1200_common *priv = dev->priv;
  1478. queue_work(priv->workqueue, &priv->set_tim_work);
  1479. return 0;
  1480. }
  1481. void cw1200_set_cts_work(struct work_struct *work)
  1482. {
  1483. struct cw1200_common *priv =
  1484. container_of(work, struct cw1200_common, set_cts_work);
  1485. u8 erp_ie[3] = {WLAN_EID_ERP_INFO, 0x1, 0};
  1486. struct wsm_update_ie update_ie = {
  1487. .what = WSM_UPDATE_IE_BEACON,
  1488. .count = 1,
  1489. .ies = erp_ie,
  1490. .length = 3,
  1491. };
  1492. u32 erp_info;
  1493. __le32 use_cts_prot;
  1494. mutex_lock(&priv->conf_mutex);
  1495. erp_info = priv->erp_info;
  1496. mutex_unlock(&priv->conf_mutex);
  1497. use_cts_prot =
  1498. erp_info & WLAN_ERP_USE_PROTECTION ?
  1499. __cpu_to_le32(1) : 0;
  1500. erp_ie[ERP_INFO_BYTE_OFFSET] = erp_info;
  1501. pr_debug("[STA] ERP information 0x%x\n", erp_info);
  1502. wsm_write_mib(priv, WSM_MIB_ID_NON_ERP_PROTECTION,
  1503. &use_cts_prot, sizeof(use_cts_prot));
  1504. wsm_update_ie(priv, &update_ie);
  1505. return;
  1506. }
  1507. static int cw1200_set_btcoexinfo(struct cw1200_common *priv)
  1508. {
  1509. struct wsm_override_internal_txrate arg;
  1510. int ret = 0;
  1511. if (priv->mode == NL80211_IFTYPE_STATION) {
  1512. /* Plumb PSPOLL and NULL template */
  1513. cw1200_upload_pspoll(priv);
  1514. cw1200_upload_null(priv);
  1515. cw1200_upload_qosnull(priv);
  1516. } else {
  1517. return 0;
  1518. }
  1519. memset(&arg, 0, sizeof(struct wsm_override_internal_txrate));
  1520. if (!priv->vif->p2p) {
  1521. /* STATION mode */
  1522. if (priv->bss_params.operational_rate_set & ~0xF) {
  1523. pr_debug("[STA] STA has ERP rates\n");
  1524. /* G or BG mode */
  1525. arg.internalTxRate = (__ffs(
  1526. priv->bss_params.operational_rate_set & ~0xF));
  1527. } else {
  1528. pr_debug("[STA] STA has non ERP rates\n");
  1529. /* B only mode */
  1530. arg.internalTxRate = (__ffs(le32_to_cpu(priv->association_mode.basic_rate_set)));
  1531. }
  1532. arg.nonErpInternalTxRate = (__ffs(le32_to_cpu(priv->association_mode.basic_rate_set)));
  1533. } else {
  1534. /* P2P mode */
  1535. arg.internalTxRate = (__ffs(priv->bss_params.operational_rate_set & ~0xF));
  1536. arg.nonErpInternalTxRate = (__ffs(priv->bss_params.operational_rate_set & ~0xF));
  1537. }
  1538. pr_debug("[STA] BTCOEX_INFO MODE %d, internalTxRate : %x, nonErpInternalTxRate: %x\n",
  1539. priv->mode,
  1540. arg.internalTxRate,
  1541. arg.nonErpInternalTxRate);
  1542. ret = wsm_write_mib(priv, WSM_MIB_ID_OVERRIDE_INTERNAL_TX_RATE,
  1543. &arg, sizeof(arg));
  1544. return ret;
  1545. }
  1546. void cw1200_bss_info_changed(struct ieee80211_hw *dev,
  1547. struct ieee80211_vif *vif,
  1548. struct ieee80211_bss_conf *info,
  1549. u32 changed)
  1550. {
  1551. struct cw1200_common *priv = dev->priv;
  1552. bool do_join = false;
  1553. mutex_lock(&priv->conf_mutex);
  1554. pr_debug("BSS CHANGED: %08x\n", changed);
  1555. /* TODO: BSS_CHANGED_QOS */
  1556. /* TODO: BSS_CHANGED_TXPOWER */
  1557. if (changed & BSS_CHANGED_ARP_FILTER) {
  1558. struct wsm_mib_arp_ipv4_filter filter = {0};
  1559. int i;
  1560. pr_debug("[STA] BSS_CHANGED_ARP_FILTER cnt: %d\n",
  1561. info->arp_addr_cnt);
  1562. /* Currently only one IP address is supported by firmware.
  1563. * In case of more IPs arp filtering will be disabled.
  1564. */
  1565. if (info->arp_addr_cnt > 0 &&
  1566. info->arp_addr_cnt <= WSM_MAX_ARP_IP_ADDRTABLE_ENTRIES) {
  1567. for (i = 0; i < info->arp_addr_cnt; i++) {
  1568. filter.ipv4addrs[i] = info->arp_addr_list[i];
  1569. pr_debug("[STA] addr[%d]: 0x%X\n",
  1570. i, filter.ipv4addrs[i]);
  1571. }
  1572. filter.enable = __cpu_to_le32(1);
  1573. }
  1574. pr_debug("[STA] arp ip filter enable: %d\n",
  1575. __le32_to_cpu(filter.enable));
  1576. wsm_set_arp_ipv4_filter(priv, &filter);
  1577. }
  1578. if (changed &
  1579. (BSS_CHANGED_BEACON |
  1580. BSS_CHANGED_AP_PROBE_RESP |
  1581. BSS_CHANGED_BSSID |
  1582. BSS_CHANGED_SSID |
  1583. BSS_CHANGED_IBSS)) {
  1584. pr_debug("BSS_CHANGED_BEACON\n");
  1585. priv->beacon_int = info->beacon_int;
  1586. cw1200_update_beaconing(priv);
  1587. cw1200_upload_beacon(priv);
  1588. }
  1589. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  1590. pr_debug("BSS_CHANGED_BEACON_ENABLED (%d)\n", info->enable_beacon);
  1591. if (priv->enable_beacon != info->enable_beacon) {
  1592. cw1200_enable_beaconing(priv, info->enable_beacon);
  1593. priv->enable_beacon = info->enable_beacon;
  1594. }
  1595. }
  1596. if (changed & BSS_CHANGED_BEACON_INT) {
  1597. pr_debug("CHANGED_BEACON_INT\n");
  1598. if (info->ibss_joined)
  1599. do_join = true;
  1600. else if (priv->join_status == CW1200_JOIN_STATUS_AP)
  1601. cw1200_update_beaconing(priv);
  1602. }
  1603. /* assoc/disassoc, or maybe AID changed */
  1604. if (changed & BSS_CHANGED_ASSOC) {
  1605. wsm_lock_tx(priv);
  1606. priv->wep_default_key_id = -1;
  1607. wsm_unlock_tx(priv);
  1608. }
  1609. if (changed & BSS_CHANGED_BSSID) {
  1610. pr_debug("BSS_CHANGED_BSSID\n");
  1611. do_join = true;
  1612. }
  1613. if (changed &
  1614. (BSS_CHANGED_ASSOC |
  1615. BSS_CHANGED_BSSID |
  1616. BSS_CHANGED_IBSS |
  1617. BSS_CHANGED_BASIC_RATES |
  1618. BSS_CHANGED_HT)) {
  1619. pr_debug("BSS_CHANGED_ASSOC\n");
  1620. if (info->assoc) {
  1621. if (priv->join_status < CW1200_JOIN_STATUS_PRE_STA) {
  1622. ieee80211_connection_loss(vif);
  1623. mutex_unlock(&priv->conf_mutex);
  1624. return;
  1625. } else if (priv->join_status == CW1200_JOIN_STATUS_PRE_STA) {
  1626. priv->join_status = CW1200_JOIN_STATUS_STA;
  1627. }
  1628. } else {
  1629. do_join = true;
  1630. }
  1631. if (info->assoc || info->ibss_joined) {
  1632. struct ieee80211_sta *sta = NULL;
  1633. __le32 htprot = 0;
  1634. if (info->dtim_period)
  1635. priv->join_dtim_period = info->dtim_period;
  1636. priv->beacon_int = info->beacon_int;
  1637. rcu_read_lock();
  1638. if (info->bssid && !info->ibss_joined)
  1639. sta = ieee80211_find_sta(vif, info->bssid);
  1640. if (sta) {
  1641. priv->ht_info.ht_cap = sta->ht_cap;
  1642. priv->bss_params.operational_rate_set =
  1643. cw1200_rate_mask_to_wsm(priv,
  1644. sta->supp_rates[priv->channel->band]);
  1645. priv->ht_info.channel_type = cfg80211_get_chandef_type(&dev->conf.chandef);
  1646. priv->ht_info.operation_mode = info->ht_operation_mode;
  1647. } else {
  1648. memset(&priv->ht_info, 0,
  1649. sizeof(priv->ht_info));
  1650. priv->bss_params.operational_rate_set = -1;
  1651. }
  1652. rcu_read_unlock();
  1653. /* Non Greenfield stations present */
  1654. if (priv->ht_info.operation_mode &
  1655. IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT)
  1656. htprot |= cpu_to_le32(WSM_NON_GREENFIELD_STA_PRESENT);
  1657. /* Set HT protection method */
  1658. htprot |= cpu_to_le32((priv->ht_info.operation_mode & IEEE80211_HT_OP_MODE_PROTECTION) << 2);
  1659. /* TODO:
  1660. * STBC_param.dual_cts
  1661. * STBC_param.LSIG_TXOP_FILL
  1662. */
  1663. wsm_write_mib(priv, WSM_MIB_ID_SET_HT_PROTECTION,
  1664. &htprot, sizeof(htprot));
  1665. priv->association_mode.greenfield =
  1666. cw1200_ht_greenfield(&priv->ht_info);
  1667. priv->association_mode.flags =
  1668. WSM_ASSOCIATION_MODE_SNOOP_ASSOC_FRAMES |
  1669. WSM_ASSOCIATION_MODE_USE_PREAMBLE_TYPE |
  1670. WSM_ASSOCIATION_MODE_USE_HT_MODE |
  1671. WSM_ASSOCIATION_MODE_USE_BASIC_RATE_SET |
  1672. WSM_ASSOCIATION_MODE_USE_MPDU_START_SPACING;
  1673. priv->association_mode.preamble =
  1674. info->use_short_preamble ?
  1675. WSM_JOIN_PREAMBLE_SHORT :
  1676. WSM_JOIN_PREAMBLE_LONG;
  1677. priv->association_mode.basic_rate_set = __cpu_to_le32(
  1678. cw1200_rate_mask_to_wsm(priv,
  1679. info->basic_rates));
  1680. priv->association_mode.mpdu_start_spacing =
  1681. cw1200_ht_ampdu_density(&priv->ht_info);
  1682. cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
  1683. cancel_work_sync(&priv->unjoin_work);
  1684. priv->bss_params.beacon_lost_count = priv->cqm_beacon_loss_count;
  1685. priv->bss_params.aid = info->aid;
  1686. if (priv->join_dtim_period < 1)
  1687. priv->join_dtim_period = 1;
  1688. pr_debug("[STA] DTIM %d, interval: %d\n",
  1689. priv->join_dtim_period, priv->beacon_int);
  1690. pr_debug("[STA] Preamble: %d, Greenfield: %d, Aid: %d, Rates: 0x%.8X, Basic: 0x%.8X\n",
  1691. priv->association_mode.preamble,
  1692. priv->association_mode.greenfield,
  1693. priv->bss_params.aid,
  1694. priv->bss_params.operational_rate_set,
  1695. priv->association_mode.basic_rate_set);
  1696. wsm_set_association_mode(priv, &priv->association_mode);
  1697. if (!info->ibss_joined) {
  1698. wsm_keep_alive_period(priv, 30 /* sec */);
  1699. wsm_set_bss_params(priv, &priv->bss_params);
  1700. priv->setbssparams_done = true;
  1701. cw1200_set_beacon_wakeup_period_work(&priv->set_beacon_wakeup_period_work);
  1702. cw1200_set_pm(priv, &priv->powersave_mode);
  1703. }
  1704. if (priv->vif->p2p) {
  1705. pr_debug("[STA] Setting p2p powersave configuration.\n");
  1706. wsm_set_p2p_ps_modeinfo(priv,
  1707. &priv->p2p_ps_modeinfo);
  1708. }
  1709. if (priv->bt_present)
  1710. cw1200_set_btcoexinfo(priv);
  1711. } else {
  1712. memset(&priv->association_mode, 0,
  1713. sizeof(priv->association_mode));
  1714. memset(&priv->bss_params, 0, sizeof(priv->bss_params));
  1715. }
  1716. }
  1717. /* ERP Protection */
  1718. if (changed & (BSS_CHANGED_ASSOC |
  1719. BSS_CHANGED_ERP_CTS_PROT |
  1720. BSS_CHANGED_ERP_PREAMBLE)) {
  1721. u32 prev_erp_info = priv->erp_info;
  1722. if (info->use_cts_prot)
  1723. priv->erp_info |= WLAN_ERP_USE_PROTECTION;
  1724. else if (!(prev_erp_info & WLAN_ERP_NON_ERP_PRESENT))
  1725. priv->erp_info &= ~WLAN_ERP_USE_PROTECTION;
  1726. if (info->use_short_preamble)
  1727. priv->erp_info |= WLAN_ERP_BARKER_PREAMBLE;
  1728. else
  1729. priv->erp_info &= ~WLAN_ERP_BARKER_PREAMBLE;
  1730. pr_debug("[STA] ERP Protection: %x\n", priv->erp_info);
  1731. if (prev_erp_info != priv->erp_info)
  1732. queue_work(priv->workqueue, &priv->set_cts_work);
  1733. }
  1734. /* ERP Slottime */
  1735. if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_SLOT)) {
  1736. __le32 slot_time = info->use_short_slot ?
  1737. __cpu_to_le32(9) : __cpu_to_le32(20);
  1738. pr_debug("[STA] Slot time: %d us.\n",
  1739. __le32_to_cpu(slot_time));
  1740. wsm_write_mib(priv, WSM_MIB_ID_DOT11_SLOT_TIME,
  1741. &slot_time, sizeof(slot_time));
  1742. }
  1743. if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_CQM)) {
  1744. struct wsm_rcpi_rssi_threshold threshold = {
  1745. .rollingAverageCount = 8,
  1746. };
  1747. pr_debug("[CQM] RSSI threshold subscribe: %d +- %d\n",
  1748. info->cqm_rssi_thold, info->cqm_rssi_hyst);
  1749. priv->cqm_rssi_thold = info->cqm_rssi_thold;
  1750. priv->cqm_rssi_hyst = info->cqm_rssi_hyst;
  1751. if (info->cqm_rssi_thold || info->cqm_rssi_hyst) {
  1752. /* RSSI subscription enabled */
  1753. /* TODO: It's not a correct way of setting threshold.
  1754. * Upper and lower must be set equal here and adjusted
  1755. * in callback. However current implementation is much
  1756. * more relaible and stable.
  1757. */
  1758. /* RSSI: signed Q8.0, RCPI: unsigned Q7.1
  1759. * RSSI = RCPI / 2 - 110
  1760. */
  1761. if (priv->cqm_use_rssi) {
  1762. threshold.upperThreshold =
  1763. info->cqm_rssi_thold + info->cqm_rssi_hyst;
  1764. threshold.lowerThreshold =
  1765. info->cqm_rssi_thold;
  1766. threshold.rssiRcpiMode |= WSM_RCPI_RSSI_USE_RSSI;
  1767. } else {
  1768. threshold.upperThreshold = (info->cqm_rssi_thold + info->cqm_rssi_hyst + 110) * 2;
  1769. threshold.lowerThreshold = (info->cqm_rssi_thold + 110) * 2;
  1770. }
  1771. threshold.rssiRcpiMode |= WSM_RCPI_RSSI_THRESHOLD_ENABLE;
  1772. } else {
  1773. /* There is a bug in FW, see sta.c. We have to enable
  1774. * dummy subscription to get correct RSSI values.
  1775. */
  1776. threshold.rssiRcpiMode |=
  1777. WSM_RCPI_RSSI_THRESHOLD_ENABLE |
  1778. WSM_RCPI_RSSI_DONT_USE_UPPER |
  1779. WSM_RCPI_RSSI_DONT_USE_LOWER;
  1780. if (priv->cqm_use_rssi)
  1781. threshold.rssiRcpiMode |= WSM_RCPI_RSSI_USE_RSSI;
  1782. }
  1783. wsm_set_rcpi_rssi_threshold(priv, &threshold);
  1784. }
  1785. mutex_unlock(&priv->conf_mutex);
  1786. if (do_join) {
  1787. wsm_lock_tx(priv);
  1788. cw1200_do_join(priv); /* Will unlock it for us */
  1789. }
  1790. }
  1791. void cw1200_multicast_start_work(struct work_struct *work)
  1792. {
  1793. struct cw1200_common *priv =
  1794. container_of(work, struct cw1200_common, multicast_start_work);
  1795. long tmo = priv->join_dtim_period *
  1796. (priv->beacon_int + 20) * HZ / 1024;
  1797. cancel_work_sync(&priv->multicast_stop_work);
  1798. if (!priv->aid0_bit_set) {
  1799. wsm_lock_tx(priv);
  1800. cw1200_set_tim_impl(priv, true);
  1801. priv->aid0_bit_set = true;
  1802. mod_timer(&priv->mcast_timeout, jiffies + tmo);
  1803. wsm_unlock_tx(priv);
  1804. }
  1805. }
  1806. void cw1200_multicast_stop_work(struct work_struct *work)
  1807. {
  1808. struct cw1200_common *priv =
  1809. container_of(work, struct cw1200_common, multicast_stop_work);
  1810. if (priv->aid0_bit_set) {
  1811. del_timer_sync(&priv->mcast_timeout);
  1812. wsm_lock_tx(priv);
  1813. priv->aid0_bit_set = false;
  1814. cw1200_set_tim_impl(priv, false);
  1815. wsm_unlock_tx(priv);
  1816. }
  1817. }
  1818. void cw1200_mcast_timeout(unsigned long arg)
  1819. {
  1820. struct cw1200_common *priv =
  1821. (struct cw1200_common *)arg;
  1822. wiphy_warn(priv->hw->wiphy,
  1823. "Multicast delivery timeout.\n");
  1824. spin_lock_bh(&priv->ps_state_lock);
  1825. priv->tx_multicast = priv->aid0_bit_set &&
  1826. priv->buffered_multicasts;
  1827. if (priv->tx_multicast)
  1828. cw1200_bh_wakeup(priv);
  1829. spin_unlock_bh(&priv->ps_state_lock);
  1830. }
  1831. int cw1200_ampdu_action(struct ieee80211_hw *hw,
  1832. struct ieee80211_vif *vif,
  1833. enum ieee80211_ampdu_mlme_action action,
  1834. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  1835. u8 buf_size)
  1836. {
  1837. /* Aggregation is implemented fully in firmware,
  1838. * including block ack negotiation. Do not allow
  1839. * mac80211 stack to do anything: it interferes with
  1840. * the firmware.
  1841. */
  1842. /* Note that we still need this function stubbed. */
  1843. return -ENOTSUPP;
  1844. }
  1845. /* ******************************************************************** */
  1846. /* WSM callback */
  1847. void cw1200_suspend_resume(struct cw1200_common *priv,
  1848. struct wsm_suspend_resume *arg)
  1849. {
  1850. pr_debug("[AP] %s: %s\n",
  1851. arg->stop ? "stop" : "start",
  1852. arg->multicast ? "broadcast" : "unicast");
  1853. if (arg->multicast) {
  1854. bool cancel_tmo = false;
  1855. spin_lock_bh(&priv->ps_state_lock);
  1856. if (arg->stop) {
  1857. priv->tx_multicast = false;
  1858. } else {
  1859. /* Firmware sends this indication every DTIM if there
  1860. * is a STA in powersave connected. There is no reason
  1861. * to suspend, following wakeup will consume much more
  1862. * power than it could be saved.
  1863. */
  1864. cw1200_pm_stay_awake(&priv->pm_state,
  1865. priv->join_dtim_period *
  1866. (priv->beacon_int + 20) * HZ / 1024);
  1867. priv->tx_multicast = (priv->aid0_bit_set &&
  1868. priv->buffered_multicasts);
  1869. if (priv->tx_multicast) {
  1870. cancel_tmo = true;
  1871. cw1200_bh_wakeup(priv);
  1872. }
  1873. }
  1874. spin_unlock_bh(&priv->ps_state_lock);
  1875. if (cancel_tmo)
  1876. del_timer_sync(&priv->mcast_timeout);
  1877. } else {
  1878. spin_lock_bh(&priv->ps_state_lock);
  1879. cw1200_ps_notify(priv, arg->link_id, arg->stop);
  1880. spin_unlock_bh(&priv->ps_state_lock);
  1881. if (!arg->stop)
  1882. cw1200_bh_wakeup(priv);
  1883. }
  1884. return;
  1885. }
  1886. /* ******************************************************************** */
  1887. /* AP privates */
  1888. static int cw1200_upload_beacon(struct cw1200_common *priv)
  1889. {
  1890. int ret = 0;
  1891. struct ieee80211_mgmt *mgmt;
  1892. struct wsm_template_frame frame = {
  1893. .frame_type = WSM_FRAME_TYPE_BEACON,
  1894. };
  1895. u16 tim_offset;
  1896. u16 tim_len;
  1897. if (priv->mode == NL80211_IFTYPE_STATION ||
  1898. priv->mode == NL80211_IFTYPE_MONITOR ||
  1899. priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  1900. goto done;
  1901. if (priv->vif->p2p)
  1902. frame.rate = WSM_TRANSMIT_RATE_6;
  1903. frame.skb = ieee80211_beacon_get_tim(priv->hw, priv->vif,
  1904. &tim_offset, &tim_len);
  1905. if (!frame.skb)
  1906. return -ENOMEM;
  1907. ret = wsm_set_template_frame(priv, &frame);
  1908. if (ret)
  1909. goto done;
  1910. /* TODO: Distill probe resp; remove TIM
  1911. * and any other beacon-specific IEs
  1912. */
  1913. mgmt = (void *)frame.skb->data;
  1914. mgmt->frame_control =
  1915. __cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1916. IEEE80211_STYPE_PROBE_RESP);
  1917. frame.frame_type = WSM_FRAME_TYPE_PROBE_RESPONSE;
  1918. if (priv->vif->p2p) {
  1919. ret = wsm_set_probe_responder(priv, true);
  1920. } else {
  1921. ret = wsm_set_template_frame(priv, &frame);
  1922. wsm_set_probe_responder(priv, false);
  1923. }
  1924. done:
  1925. dev_kfree_skb(frame.skb);
  1926. return ret;
  1927. }
  1928. static int cw1200_upload_pspoll(struct cw1200_common *priv)
  1929. {
  1930. int ret = 0;
  1931. struct wsm_template_frame frame = {
  1932. .frame_type = WSM_FRAME_TYPE_PS_POLL,
  1933. .rate = 0xFF,
  1934. };
  1935. frame.skb = ieee80211_pspoll_get(priv->hw, priv->vif);
  1936. if (!frame.skb)
  1937. return -ENOMEM;
  1938. ret = wsm_set_template_frame(priv, &frame);
  1939. dev_kfree_skb(frame.skb);
  1940. return ret;
  1941. }
  1942. static int cw1200_upload_null(struct cw1200_common *priv)
  1943. {
  1944. int ret = 0;
  1945. struct wsm_template_frame frame = {
  1946. .frame_type = WSM_FRAME_TYPE_NULL,
  1947. .rate = 0xFF,
  1948. };
  1949. frame.skb = ieee80211_nullfunc_get(priv->hw, priv->vif);
  1950. if (!frame.skb)
  1951. return -ENOMEM;
  1952. ret = wsm_set_template_frame(priv, &frame);
  1953. dev_kfree_skb(frame.skb);
  1954. return ret;
  1955. }
  1956. static int cw1200_upload_qosnull(struct cw1200_common *priv)
  1957. {
  1958. int ret = 0;
  1959. /* TODO: This needs to be implemented
  1960. struct wsm_template_frame frame = {
  1961. .frame_type = WSM_FRAME_TYPE_QOS_NULL,
  1962. .rate = 0xFF,
  1963. };
  1964. frame.skb = ieee80211_qosnullfunc_get(priv->hw, priv->vif);
  1965. if (!frame.skb)
  1966. return -ENOMEM;
  1967. ret = wsm_set_template_frame(priv, &frame);
  1968. dev_kfree_skb(frame.skb);
  1969. */
  1970. return ret;
  1971. }
  1972. static int cw1200_enable_beaconing(struct cw1200_common *priv,
  1973. bool enable)
  1974. {
  1975. struct wsm_beacon_transmit transmit = {
  1976. .enable_beaconing = enable,
  1977. };
  1978. return wsm_beacon_transmit(priv, &transmit);
  1979. }
  1980. static int cw1200_start_ap(struct cw1200_common *priv)
  1981. {
  1982. int ret;
  1983. struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
  1984. struct wsm_start start = {
  1985. .mode = priv->vif->p2p ?
  1986. WSM_START_MODE_P2P_GO : WSM_START_MODE_AP,
  1987. .band = (priv->channel->band == IEEE80211_BAND_5GHZ) ?
  1988. WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G,
  1989. .channel_number = priv->channel->hw_value,
  1990. .beacon_interval = conf->beacon_int,
  1991. .dtim_period = conf->dtim_period,
  1992. .preamble = conf->use_short_preamble ?
  1993. WSM_JOIN_PREAMBLE_SHORT :
  1994. WSM_JOIN_PREAMBLE_LONG,
  1995. .probe_delay = 100,
  1996. .basic_rate_set = cw1200_rate_mask_to_wsm(priv,
  1997. conf->basic_rates),
  1998. };
  1999. struct wsm_operational_mode mode = {
  2000. .power_mode = cw1200_power_mode,
  2001. .disable_more_flag_usage = true,
  2002. };
  2003. memset(start.ssid, 0, sizeof(start.ssid));
  2004. if (!conf->hidden_ssid) {
  2005. start.ssid_len = conf->ssid_len;
  2006. memcpy(start.ssid, conf->ssid, start.ssid_len);
  2007. }
  2008. priv->beacon_int = conf->beacon_int;
  2009. priv->join_dtim_period = conf->dtim_period;
  2010. memset(&priv->link_id_db, 0, sizeof(priv->link_id_db));
  2011. pr_debug("[AP] ch: %d(%d), bcn: %d(%d), brt: 0x%.8X, ssid: %.*s.\n",
  2012. start.channel_number, start.band,
  2013. start.beacon_interval, start.dtim_period,
  2014. start.basic_rate_set,
  2015. start.ssid_len, start.ssid);
  2016. ret = wsm_start(priv, &start);
  2017. if (!ret)
  2018. ret = cw1200_upload_keys(priv);
  2019. if (!ret && priv->vif->p2p) {
  2020. pr_debug("[AP] Setting p2p powersave configuration.\n");
  2021. wsm_set_p2p_ps_modeinfo(priv, &priv->p2p_ps_modeinfo);
  2022. }
  2023. if (!ret) {
  2024. wsm_set_block_ack_policy(priv, 0, 0);
  2025. priv->join_status = CW1200_JOIN_STATUS_AP;
  2026. cw1200_update_filtering(priv);
  2027. }
  2028. wsm_set_operational_mode(priv, &mode);
  2029. return ret;
  2030. }
  2031. static int cw1200_update_beaconing(struct cw1200_common *priv)
  2032. {
  2033. struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
  2034. struct wsm_reset reset = {
  2035. .link_id = 0,
  2036. .reset_statistics = true,
  2037. };
  2038. if (priv->mode == NL80211_IFTYPE_AP) {
  2039. /* TODO: check if changed channel, band */
  2040. if (priv->join_status != CW1200_JOIN_STATUS_AP ||
  2041. priv->beacon_int != conf->beacon_int) {
  2042. pr_debug("ap restarting\n");
  2043. wsm_lock_tx(priv);
  2044. if (priv->join_status != CW1200_JOIN_STATUS_PASSIVE)
  2045. wsm_reset(priv, &reset);
  2046. priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
  2047. cw1200_start_ap(priv);
  2048. wsm_unlock_tx(priv);
  2049. } else
  2050. pr_debug("ap started join_status: %d\n",
  2051. priv->join_status);
  2052. }
  2053. return 0;
  2054. }