p2p.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417
  1. /*
  2. * Copyright (c) 2012 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/etherdevice.h>
  19. #include <net/cfg80211.h>
  20. #include <brcmu_wifi.h>
  21. #include <brcmu_utils.h>
  22. #include <defs.h>
  23. #include <dhd.h>
  24. #include <dhd_dbg.h>
  25. #include "fwil.h"
  26. #include "fwil_types.h"
  27. #include "p2p.h"
  28. #include "wl_cfg80211.h"
  29. /* parameters used for p2p escan */
  30. #define P2PAPI_SCAN_NPROBES 1
  31. #define P2PAPI_SCAN_DWELL_TIME_MS 80
  32. #define P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS 40
  33. #define P2PAPI_SCAN_HOME_TIME_MS 60
  34. #define P2PAPI_SCAN_NPROBS_TIME_MS 30
  35. #define P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS 100
  36. #define WL_SCAN_CONNECT_DWELL_TIME_MS 200
  37. #define WL_SCAN_JOIN_PROBE_INTERVAL_MS 20
  38. #define BRCMF_P2P_WILDCARD_SSID "DIRECT-"
  39. #define BRCMF_P2P_WILDCARD_SSID_LEN (sizeof(BRCMF_P2P_WILDCARD_SSID) - 1)
  40. #define SOCIAL_CHAN_1 1
  41. #define SOCIAL_CHAN_2 6
  42. #define SOCIAL_CHAN_3 11
  43. #define IS_P2P_SOCIAL_CHANNEL(channel) ((channel == SOCIAL_CHAN_1) || \
  44. (channel == SOCIAL_CHAN_2) || \
  45. (channel == SOCIAL_CHAN_3))
  46. #define SOCIAL_CHAN_CNT 3
  47. #define AF_PEER_SEARCH_CNT 2
  48. #define BRCMF_SCB_TIMEOUT_VALUE 20
  49. #define P2P_VER 9 /* P2P version: 9=WiFi P2P v1.0 */
  50. #define P2P_PUB_AF_CATEGORY 0x04
  51. #define P2P_PUB_AF_ACTION 0x09
  52. #define P2P_AF_CATEGORY 0x7f
  53. #define P2P_OUI "\x50\x6F\x9A" /* P2P OUI */
  54. #define P2P_OUI_LEN 3 /* P2P OUI length */
  55. /* Action Frame Constants */
  56. #define DOT11_ACTION_HDR_LEN 2 /* action frame category + action */
  57. #define DOT11_ACTION_CAT_OFF 0 /* category offset */
  58. #define DOT11_ACTION_ACT_OFF 1 /* action offset */
  59. #define P2P_AF_DWELL_TIME 200
  60. #define P2P_AF_MIN_DWELL_TIME 100
  61. #define P2P_AF_MED_DWELL_TIME 400
  62. #define P2P_AF_LONG_DWELL_TIME 1000
  63. #define P2P_AF_TX_MAX_RETRY 1
  64. #define P2P_AF_MAX_WAIT_TIME 2000
  65. #define P2P_INVALID_CHANNEL -1
  66. #define P2P_CHANNEL_SYNC_RETRY 5
  67. #define P2P_AF_FRM_SCAN_MAX_WAIT 1500
  68. #define P2P_DEFAULT_SLEEP_TIME_VSDB 200
  69. /* WiFi P2P Public Action Frame OUI Subtypes */
  70. #define P2P_PAF_GON_REQ 0 /* Group Owner Negotiation Req */
  71. #define P2P_PAF_GON_RSP 1 /* Group Owner Negotiation Rsp */
  72. #define P2P_PAF_GON_CONF 2 /* Group Owner Negotiation Confirm */
  73. #define P2P_PAF_INVITE_REQ 3 /* P2P Invitation Request */
  74. #define P2P_PAF_INVITE_RSP 4 /* P2P Invitation Response */
  75. #define P2P_PAF_DEVDIS_REQ 5 /* Device Discoverability Request */
  76. #define P2P_PAF_DEVDIS_RSP 6 /* Device Discoverability Response */
  77. #define P2P_PAF_PROVDIS_REQ 7 /* Provision Discovery Request */
  78. #define P2P_PAF_PROVDIS_RSP 8 /* Provision Discovery Response */
  79. #define P2P_PAF_SUBTYPE_INVALID 255 /* Invalid Subtype */
  80. /* WiFi P2P Action Frame OUI Subtypes */
  81. #define P2P_AF_NOTICE_OF_ABSENCE 0 /* Notice of Absence */
  82. #define P2P_AF_PRESENCE_REQ 1 /* P2P Presence Request */
  83. #define P2P_AF_PRESENCE_RSP 2 /* P2P Presence Response */
  84. #define P2P_AF_GO_DISC_REQ 3 /* GO Discoverability Request */
  85. /* P2P Service Discovery related */
  86. #define P2PSD_ACTION_CATEGORY 0x04 /* Public action frame */
  87. #define P2PSD_ACTION_ID_GAS_IREQ 0x0a /* GAS Initial Request AF */
  88. #define P2PSD_ACTION_ID_GAS_IRESP 0x0b /* GAS Initial Response AF */
  89. #define P2PSD_ACTION_ID_GAS_CREQ 0x0c /* GAS Comback Request AF */
  90. #define P2PSD_ACTION_ID_GAS_CRESP 0x0d /* GAS Comback Response AF */
  91. /**
  92. * struct brcmf_p2p_disc_st_le - set discovery state in firmware.
  93. *
  94. * @state: requested discovery state (see enum brcmf_p2p_disc_state).
  95. * @chspec: channel parameter for %WL_P2P_DISC_ST_LISTEN state.
  96. * @dwell: dwell time in ms for %WL_P2P_DISC_ST_LISTEN state.
  97. */
  98. struct brcmf_p2p_disc_st_le {
  99. u8 state;
  100. __le16 chspec;
  101. __le16 dwell;
  102. };
  103. /**
  104. * enum brcmf_p2p_disc_state - P2P discovery state values
  105. *
  106. * @WL_P2P_DISC_ST_SCAN: P2P discovery with wildcard SSID and P2P IE.
  107. * @WL_P2P_DISC_ST_LISTEN: P2P discovery off-channel for specified time.
  108. * @WL_P2P_DISC_ST_SEARCH: P2P discovery with P2P wildcard SSID and P2P IE.
  109. */
  110. enum brcmf_p2p_disc_state {
  111. WL_P2P_DISC_ST_SCAN,
  112. WL_P2P_DISC_ST_LISTEN,
  113. WL_P2P_DISC_ST_SEARCH
  114. };
  115. /**
  116. * struct brcmf_p2p_scan_le - P2P specific scan request.
  117. *
  118. * @type: type of scan method requested (values: 'E' or 'S').
  119. * @reserved: reserved (ignored).
  120. * @eparams: parameters used for type 'E'.
  121. * @sparams: parameters used for type 'S'.
  122. */
  123. struct brcmf_p2p_scan_le {
  124. u8 type;
  125. u8 reserved[3];
  126. union {
  127. struct brcmf_escan_params_le eparams;
  128. struct brcmf_scan_params_le sparams;
  129. };
  130. };
  131. /**
  132. * struct brcmf_p2p_pub_act_frame - WiFi P2P Public Action Frame
  133. *
  134. * @category: P2P_PUB_AF_CATEGORY
  135. * @action: P2P_PUB_AF_ACTION
  136. * @oui[3]: P2P_OUI
  137. * @oui_type: OUI type - P2P_VER
  138. * @subtype: OUI subtype - P2P_TYPE_*
  139. * @dialog_token: nonzero, identifies req/rsp transaction
  140. * @elts[1]: Variable length information elements.
  141. */
  142. struct brcmf_p2p_pub_act_frame {
  143. u8 category;
  144. u8 action;
  145. u8 oui[3];
  146. u8 oui_type;
  147. u8 subtype;
  148. u8 dialog_token;
  149. u8 elts[1];
  150. };
  151. /**
  152. * struct brcmf_p2p_action_frame - WiFi P2P Action Frame
  153. *
  154. * @category: P2P_AF_CATEGORY
  155. * @OUI[3]: OUI - P2P_OUI
  156. * @type: OUI Type - P2P_VER
  157. * @subtype: OUI Subtype - P2P_AF_*
  158. * @dialog_token: nonzero, identifies req/resp tranaction
  159. * @elts[1]: Variable length information elements.
  160. */
  161. struct brcmf_p2p_action_frame {
  162. u8 category;
  163. u8 oui[3];
  164. u8 type;
  165. u8 subtype;
  166. u8 dialog_token;
  167. u8 elts[1];
  168. };
  169. /**
  170. * struct brcmf_p2psd_gas_pub_act_frame - Wi-Fi GAS Public Action Frame
  171. *
  172. * @category: 0x04 Public Action Frame
  173. * @action: 0x6c Advertisement Protocol
  174. * @dialog_token: nonzero, identifies req/rsp transaction
  175. * @query_data[1]: Query Data. SD gas ireq SD gas iresp
  176. */
  177. struct brcmf_p2psd_gas_pub_act_frame {
  178. u8 category;
  179. u8 action;
  180. u8 dialog_token;
  181. u8 query_data[1];
  182. };
  183. /**
  184. * struct brcmf_config_af_params - Action Frame Parameters for tx.
  185. *
  186. * @mpc_onoff: To make sure to send successfully action frame, we have to
  187. * turn off mpc 0: off, 1: on, (-1): do nothing
  188. * @search_channel: 1: search peer's channel to send af
  189. * extra_listen: keep the dwell time to get af response frame.
  190. */
  191. struct brcmf_config_af_params {
  192. s32 mpc_onoff;
  193. bool search_channel;
  194. bool extra_listen;
  195. };
  196. /**
  197. * brcmf_p2p_is_pub_action() - true if p2p public type frame.
  198. *
  199. * @frame: action frame data.
  200. * @frame_len: length of action frame data.
  201. *
  202. * Determine if action frame is p2p public action type
  203. */
  204. static bool brcmf_p2p_is_pub_action(void *frame, u32 frame_len)
  205. {
  206. struct brcmf_p2p_pub_act_frame *pact_frm;
  207. if (frame == NULL)
  208. return false;
  209. pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
  210. if (frame_len < sizeof(struct brcmf_p2p_pub_act_frame) - 1)
  211. return false;
  212. if (pact_frm->category == P2P_PUB_AF_CATEGORY &&
  213. pact_frm->action == P2P_PUB_AF_ACTION &&
  214. pact_frm->oui_type == P2P_VER &&
  215. memcmp(pact_frm->oui, P2P_OUI, P2P_OUI_LEN) == 0)
  216. return true;
  217. return false;
  218. }
  219. /**
  220. * brcmf_p2p_is_p2p_action() - true if p2p action type frame.
  221. *
  222. * @frame: action frame data.
  223. * @frame_len: length of action frame data.
  224. *
  225. * Determine if action frame is p2p action type
  226. */
  227. static bool brcmf_p2p_is_p2p_action(void *frame, u32 frame_len)
  228. {
  229. struct brcmf_p2p_action_frame *act_frm;
  230. if (frame == NULL)
  231. return false;
  232. act_frm = (struct brcmf_p2p_action_frame *)frame;
  233. if (frame_len < sizeof(struct brcmf_p2p_action_frame) - 1)
  234. return false;
  235. if (act_frm->category == P2P_AF_CATEGORY &&
  236. act_frm->type == P2P_VER &&
  237. memcmp(act_frm->oui, P2P_OUI, P2P_OUI_LEN) == 0)
  238. return true;
  239. return false;
  240. }
  241. /**
  242. * brcmf_p2p_is_gas_action() - true if p2p gas action type frame.
  243. *
  244. * @frame: action frame data.
  245. * @frame_len: length of action frame data.
  246. *
  247. * Determine if action frame is p2p gas action type
  248. */
  249. static bool brcmf_p2p_is_gas_action(void *frame, u32 frame_len)
  250. {
  251. struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
  252. if (frame == NULL)
  253. return false;
  254. sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
  255. if (frame_len < sizeof(struct brcmf_p2psd_gas_pub_act_frame) - 1)
  256. return false;
  257. if (sd_act_frm->category != P2PSD_ACTION_CATEGORY)
  258. return false;
  259. if (sd_act_frm->action == P2PSD_ACTION_ID_GAS_IREQ ||
  260. sd_act_frm->action == P2PSD_ACTION_ID_GAS_IRESP ||
  261. sd_act_frm->action == P2PSD_ACTION_ID_GAS_CREQ ||
  262. sd_act_frm->action == P2PSD_ACTION_ID_GAS_CRESP)
  263. return true;
  264. return false;
  265. }
  266. /**
  267. * brcmf_p2p_print_actframe() - debug print routine.
  268. *
  269. * @tx: Received or to be transmitted
  270. * @frame: action frame data.
  271. * @frame_len: length of action frame data.
  272. *
  273. * Print information about the p2p action frame
  274. */
  275. #ifdef DEBUG
  276. static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
  277. {
  278. struct brcmf_p2p_pub_act_frame *pact_frm;
  279. struct brcmf_p2p_action_frame *act_frm;
  280. struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
  281. if (!frame || frame_len <= 2)
  282. return;
  283. if (brcmf_p2p_is_pub_action(frame, frame_len)) {
  284. pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
  285. switch (pact_frm->subtype) {
  286. case P2P_PAF_GON_REQ:
  287. brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Req Frame\n",
  288. (tx) ? "TX" : "RX");
  289. break;
  290. case P2P_PAF_GON_RSP:
  291. brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Rsp Frame\n",
  292. (tx) ? "TX" : "RX");
  293. break;
  294. case P2P_PAF_GON_CONF:
  295. brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Confirm Frame\n",
  296. (tx) ? "TX" : "RX");
  297. break;
  298. case P2P_PAF_INVITE_REQ:
  299. brcmf_dbg(TRACE, "%s P2P Invitation Request Frame\n",
  300. (tx) ? "TX" : "RX");
  301. break;
  302. case P2P_PAF_INVITE_RSP:
  303. brcmf_dbg(TRACE, "%s P2P Invitation Response Frame\n",
  304. (tx) ? "TX" : "RX");
  305. break;
  306. case P2P_PAF_DEVDIS_REQ:
  307. brcmf_dbg(TRACE, "%s P2P Device Discoverability Request Frame\n",
  308. (tx) ? "TX" : "RX");
  309. break;
  310. case P2P_PAF_DEVDIS_RSP:
  311. brcmf_dbg(TRACE, "%s P2P Device Discoverability Response Frame\n",
  312. (tx) ? "TX" : "RX");
  313. break;
  314. case P2P_PAF_PROVDIS_REQ:
  315. brcmf_dbg(TRACE, "%s P2P Provision Discovery Request Frame\n",
  316. (tx) ? "TX" : "RX");
  317. break;
  318. case P2P_PAF_PROVDIS_RSP:
  319. brcmf_dbg(TRACE, "%s P2P Provision Discovery Response Frame\n",
  320. (tx) ? "TX" : "RX");
  321. break;
  322. default:
  323. brcmf_dbg(TRACE, "%s Unknown P2P Public Action Frame\n",
  324. (tx) ? "TX" : "RX");
  325. break;
  326. }
  327. } else if (brcmf_p2p_is_p2p_action(frame, frame_len)) {
  328. act_frm = (struct brcmf_p2p_action_frame *)frame;
  329. switch (act_frm->subtype) {
  330. case P2P_AF_NOTICE_OF_ABSENCE:
  331. brcmf_dbg(TRACE, "%s P2P Notice of Absence Frame\n",
  332. (tx) ? "TX" : "RX");
  333. break;
  334. case P2P_AF_PRESENCE_REQ:
  335. brcmf_dbg(TRACE, "%s P2P Presence Request Frame\n",
  336. (tx) ? "TX" : "RX");
  337. break;
  338. case P2P_AF_PRESENCE_RSP:
  339. brcmf_dbg(TRACE, "%s P2P Presence Response Frame\n",
  340. (tx) ? "TX" : "RX");
  341. break;
  342. case P2P_AF_GO_DISC_REQ:
  343. brcmf_dbg(TRACE, "%s P2P Discoverability Request Frame\n",
  344. (tx) ? "TX" : "RX");
  345. break;
  346. default:
  347. brcmf_dbg(TRACE, "%s Unknown P2P Action Frame\n",
  348. (tx) ? "TX" : "RX");
  349. }
  350. } else if (brcmf_p2p_is_gas_action(frame, frame_len)) {
  351. sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
  352. switch (sd_act_frm->action) {
  353. case P2PSD_ACTION_ID_GAS_IREQ:
  354. brcmf_dbg(TRACE, "%s P2P GAS Initial Request\n",
  355. (tx) ? "TX" : "RX");
  356. break;
  357. case P2PSD_ACTION_ID_GAS_IRESP:
  358. brcmf_dbg(TRACE, "%s P2P GAS Initial Response\n",
  359. (tx) ? "TX" : "RX");
  360. break;
  361. case P2PSD_ACTION_ID_GAS_CREQ:
  362. brcmf_dbg(TRACE, "%s P2P GAS Comback Request\n",
  363. (tx) ? "TX" : "RX");
  364. break;
  365. case P2PSD_ACTION_ID_GAS_CRESP:
  366. brcmf_dbg(TRACE, "%s P2P GAS Comback Response\n",
  367. (tx) ? "TX" : "RX");
  368. break;
  369. default:
  370. brcmf_dbg(TRACE, "%s Unknown P2P GAS Frame\n",
  371. (tx) ? "TX" : "RX");
  372. break;
  373. }
  374. }
  375. }
  376. #else
  377. static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
  378. {
  379. }
  380. #endif
  381. /**
  382. * brcmf_p2p_chnr_to_chspec() - convert channel number to chanspec.
  383. *
  384. * @channel: channel number
  385. */
  386. static u16 brcmf_p2p_chnr_to_chspec(u16 channel)
  387. {
  388. u16 chanspec;
  389. chanspec = channel & WL_CHANSPEC_CHAN_MASK;
  390. if (channel <= CH_MAX_2G_CHANNEL)
  391. chanspec |= WL_CHANSPEC_BAND_2G;
  392. else
  393. chanspec |= WL_CHANSPEC_BAND_5G;
  394. chanspec |= WL_CHANSPEC_BW_20;
  395. chanspec |= WL_CHANSPEC_CTL_SB_NONE;
  396. return chanspec;
  397. }
  398. /**
  399. * brcmf_p2p_set_firmware() - prepare firmware for peer-to-peer operation.
  400. *
  401. * @ifp: ifp to use for iovars (primary).
  402. * @p2p_mac: mac address to configure for p2p_da_override
  403. */
  404. static int brcmf_p2p_set_firmware(struct brcmf_if *ifp, u8 *p2p_mac)
  405. {
  406. s32 ret = 0;
  407. brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
  408. brcmf_fil_iovar_int_set(ifp, "apsta", 1);
  409. brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
  410. /* In case of COB type, firmware has default mac address
  411. * After Initializing firmware, we have to set current mac address to
  412. * firmware for P2P device address
  413. */
  414. ret = brcmf_fil_iovar_data_set(ifp, "p2p_da_override", p2p_mac,
  415. ETH_ALEN);
  416. if (ret)
  417. brcmf_err("failed to update device address ret %d\n", ret);
  418. return ret;
  419. }
  420. /**
  421. * brcmf_p2p_generate_bss_mac() - derive mac addresses for P2P.
  422. *
  423. * @p2p: P2P specific data.
  424. * @dev_addr: optional device address.
  425. *
  426. * P2P needs mac addresses for P2P device and interface. If no device
  427. * address it specified, these are derived from the primary net device, ie.
  428. * the permanent ethernet address of the device.
  429. */
  430. static void brcmf_p2p_generate_bss_mac(struct brcmf_p2p_info *p2p, u8 *dev_addr)
  431. {
  432. struct brcmf_if *pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
  433. bool local_admin = false;
  434. if (!dev_addr || is_zero_ether_addr(dev_addr)) {
  435. dev_addr = pri_ifp->mac_addr;
  436. local_admin = true;
  437. }
  438. /* Generate the P2P Device Address. This consists of the device's
  439. * primary MAC address with the locally administered bit set.
  440. */
  441. memcpy(p2p->dev_addr, dev_addr, ETH_ALEN);
  442. if (local_admin)
  443. p2p->dev_addr[0] |= 0x02;
  444. /* Generate the P2P Interface Address. If the discovery and connection
  445. * BSSCFGs need to simultaneously co-exist, then this address must be
  446. * different from the P2P Device Address, but also locally administered.
  447. */
  448. memcpy(p2p->int_addr, p2p->dev_addr, ETH_ALEN);
  449. p2p->int_addr[0] |= 0x02;
  450. p2p->int_addr[4] ^= 0x80;
  451. }
  452. /**
  453. * brcmf_p2p_scan_is_p2p_request() - is cfg80211 scan request a P2P scan.
  454. *
  455. * @request: the scan request as received from cfg80211.
  456. *
  457. * returns true if one of the ssids in the request matches the
  458. * P2P wildcard ssid; otherwise returns false.
  459. */
  460. static bool brcmf_p2p_scan_is_p2p_request(struct cfg80211_scan_request *request)
  461. {
  462. struct cfg80211_ssid *ssids = request->ssids;
  463. int i;
  464. for (i = 0; i < request->n_ssids; i++) {
  465. if (ssids[i].ssid_len != BRCMF_P2P_WILDCARD_SSID_LEN)
  466. continue;
  467. brcmf_dbg(INFO, "comparing ssid \"%s\"", ssids[i].ssid);
  468. if (!memcmp(BRCMF_P2P_WILDCARD_SSID, ssids[i].ssid,
  469. BRCMF_P2P_WILDCARD_SSID_LEN))
  470. return true;
  471. }
  472. return false;
  473. }
  474. /**
  475. * brcmf_p2p_set_discover_state - set discover state in firmware.
  476. *
  477. * @ifp: low-level interface object.
  478. * @state: discover state to set.
  479. * @chanspec: channel parameters (for state @WL_P2P_DISC_ST_LISTEN only).
  480. * @listen_ms: duration to listen (for state @WL_P2P_DISC_ST_LISTEN only).
  481. */
  482. static s32 brcmf_p2p_set_discover_state(struct brcmf_if *ifp, u8 state,
  483. u16 chanspec, u16 listen_ms)
  484. {
  485. struct brcmf_p2p_disc_st_le discover_state;
  486. s32 ret = 0;
  487. brcmf_dbg(TRACE, "enter\n");
  488. discover_state.state = state;
  489. discover_state.chspec = cpu_to_le16(chanspec);
  490. discover_state.dwell = cpu_to_le16(listen_ms);
  491. ret = brcmf_fil_bsscfg_data_set(ifp, "p2p_state", &discover_state,
  492. sizeof(discover_state));
  493. return ret;
  494. }
  495. /**
  496. * brcmf_p2p_deinit_discovery() - disable P2P device discovery.
  497. *
  498. * @p2p: P2P specific data.
  499. *
  500. * Resets the discovery state and disables it in firmware.
  501. */
  502. static s32 brcmf_p2p_deinit_discovery(struct brcmf_p2p_info *p2p)
  503. {
  504. struct brcmf_cfg80211_vif *vif;
  505. brcmf_dbg(TRACE, "enter\n");
  506. /* Set the discovery state to SCAN */
  507. vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
  508. (void)brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
  509. /* Disable P2P discovery in the firmware */
  510. vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
  511. (void)brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 0);
  512. return 0;
  513. }
  514. /**
  515. * brcmf_p2p_enable_discovery() - initialize and configure discovery.
  516. *
  517. * @p2p: P2P specific data.
  518. *
  519. * Initializes the discovery device and configure the virtual interface.
  520. */
  521. static int brcmf_p2p_enable_discovery(struct brcmf_p2p_info *p2p)
  522. {
  523. struct brcmf_cfg80211_vif *vif;
  524. s32 ret = 0;
  525. brcmf_dbg(TRACE, "enter\n");
  526. vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
  527. if (!vif) {
  528. brcmf_err("P2P config device not available\n");
  529. ret = -EPERM;
  530. goto exit;
  531. }
  532. if (test_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status)) {
  533. brcmf_dbg(INFO, "P2P config device already configured\n");
  534. goto exit;
  535. }
  536. /* Re-initialize P2P Discovery in the firmware */
  537. vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
  538. ret = brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 1);
  539. if (ret < 0) {
  540. brcmf_err("set p2p_disc error\n");
  541. goto exit;
  542. }
  543. vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
  544. ret = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
  545. if (ret < 0) {
  546. brcmf_err("unable to set WL_P2P_DISC_ST_SCAN\n");
  547. goto exit;
  548. }
  549. /*
  550. * Set wsec to any non-zero value in the discovery bsscfg
  551. * to ensure our P2P probe responses have the privacy bit
  552. * set in the 802.11 WPA IE. Some peer devices may not
  553. * initiate WPS with us if this bit is not set.
  554. */
  555. ret = brcmf_fil_bsscfg_int_set(vif->ifp, "wsec", AES_ENABLED);
  556. if (ret < 0) {
  557. brcmf_err("wsec error %d\n", ret);
  558. goto exit;
  559. }
  560. set_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status);
  561. exit:
  562. return ret;
  563. }
  564. /**
  565. * brcmf_p2p_escan() - initiate a P2P scan.
  566. *
  567. * @p2p: P2P specific data.
  568. * @num_chans: number of channels to scan.
  569. * @chanspecs: channel parameters for @num_chans channels.
  570. * @search_state: P2P discover state to use.
  571. * @action: scan action to pass to firmware.
  572. * @bss_type: type of P2P bss.
  573. */
  574. static s32 brcmf_p2p_escan(struct brcmf_p2p_info *p2p, u32 num_chans,
  575. u16 chanspecs[], s32 search_state, u16 action,
  576. enum p2p_bss_type bss_type)
  577. {
  578. s32 ret = 0;
  579. s32 memsize = offsetof(struct brcmf_p2p_scan_le,
  580. eparams.params_le.channel_list);
  581. s32 nprobes;
  582. s32 active;
  583. u32 i;
  584. u8 *memblk;
  585. struct brcmf_cfg80211_vif *vif;
  586. struct brcmf_p2p_scan_le *p2p_params;
  587. struct brcmf_scan_params_le *sparams;
  588. struct brcmf_ssid ssid;
  589. memsize += num_chans * sizeof(__le16);
  590. memblk = kzalloc(memsize, GFP_KERNEL);
  591. if (!memblk)
  592. return -ENOMEM;
  593. vif = p2p->bss_idx[bss_type].vif;
  594. if (vif == NULL) {
  595. brcmf_err("no vif for bss type %d\n", bss_type);
  596. ret = -EINVAL;
  597. goto exit;
  598. }
  599. switch (search_state) {
  600. case WL_P2P_DISC_ST_SEARCH:
  601. /*
  602. * If we in SEARCH STATE, we don't need to set SSID explictly
  603. * because dongle use P2P WILDCARD internally by default
  604. */
  605. /* use null ssid */
  606. ssid.SSID_len = 0;
  607. memset(ssid.SSID, 0, sizeof(ssid.SSID));
  608. break;
  609. case WL_P2P_DISC_ST_SCAN:
  610. /*
  611. * wpa_supplicant has p2p_find command with type social or
  612. * progressive. For progressive, we need to set the ssid to
  613. * P2P WILDCARD because we just do broadcast scan unless
  614. * setting SSID.
  615. */
  616. ssid.SSID_len = BRCMF_P2P_WILDCARD_SSID_LEN;
  617. memcpy(ssid.SSID, BRCMF_P2P_WILDCARD_SSID, ssid.SSID_len);
  618. break;
  619. default:
  620. brcmf_err(" invalid search state %d\n", search_state);
  621. ret = -EINVAL;
  622. goto exit;
  623. }
  624. brcmf_p2p_set_discover_state(vif->ifp, search_state, 0, 0);
  625. /*
  626. * set p2p scan parameters.
  627. */
  628. p2p_params = (struct brcmf_p2p_scan_le *)memblk;
  629. p2p_params->type = 'E';
  630. /* determine the scan engine parameters */
  631. sparams = &p2p_params->eparams.params_le;
  632. sparams->bss_type = DOT11_BSSTYPE_ANY;
  633. if (p2p->cfg->active_scan)
  634. sparams->scan_type = 0;
  635. else
  636. sparams->scan_type = 1;
  637. memset(&sparams->bssid, 0xFF, ETH_ALEN);
  638. if (ssid.SSID_len)
  639. memcpy(sparams->ssid_le.SSID, ssid.SSID, ssid.SSID_len);
  640. sparams->ssid_le.SSID_len = cpu_to_le32(ssid.SSID_len);
  641. sparams->home_time = cpu_to_le32(P2PAPI_SCAN_HOME_TIME_MS);
  642. /*
  643. * SOCIAL_CHAN_CNT + 1 takes care of the Progressive scan
  644. * supported by the supplicant.
  645. */
  646. if (num_chans == SOCIAL_CHAN_CNT || num_chans == (SOCIAL_CHAN_CNT + 1))
  647. active = P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS;
  648. else if (num_chans == AF_PEER_SEARCH_CNT)
  649. active = P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS;
  650. else if (wl_get_vif_state_all(p2p->cfg, BRCMF_VIF_STATUS_CONNECTED))
  651. active = -1;
  652. else
  653. active = P2PAPI_SCAN_DWELL_TIME_MS;
  654. /* Override scan params to find a peer for a connection */
  655. if (num_chans == 1) {
  656. active = WL_SCAN_CONNECT_DWELL_TIME_MS;
  657. /* WAR to sync with presence period of VSDB GO.
  658. * send probe request more frequently
  659. */
  660. nprobes = active / WL_SCAN_JOIN_PROBE_INTERVAL_MS;
  661. } else {
  662. nprobes = active / P2PAPI_SCAN_NPROBS_TIME_MS;
  663. }
  664. if (nprobes <= 0)
  665. nprobes = 1;
  666. brcmf_dbg(INFO, "nprobes # %d, active_time %d\n", nprobes, active);
  667. sparams->active_time = cpu_to_le32(active);
  668. sparams->nprobes = cpu_to_le32(nprobes);
  669. sparams->passive_time = cpu_to_le32(-1);
  670. sparams->channel_num = cpu_to_le32(num_chans &
  671. BRCMF_SCAN_PARAMS_COUNT_MASK);
  672. for (i = 0; i < num_chans; i++)
  673. sparams->channel_list[i] = cpu_to_le16(chanspecs[i]);
  674. /* set the escan specific parameters */
  675. p2p_params->eparams.version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
  676. p2p_params->eparams.action = cpu_to_le16(action);
  677. p2p_params->eparams.sync_id = cpu_to_le16(0x1234);
  678. /* perform p2p scan on primary device */
  679. ret = brcmf_fil_bsscfg_data_set(vif->ifp, "p2p_scan", memblk, memsize);
  680. if (!ret)
  681. set_bit(BRCMF_SCAN_STATUS_BUSY, &p2p->cfg->scan_status);
  682. exit:
  683. kfree(memblk);
  684. return ret;
  685. }
  686. /**
  687. * brcmf_p2p_run_escan() - escan callback for peer-to-peer.
  688. *
  689. * @cfg: driver private data for cfg80211 interface.
  690. * @ndev: net device for which scan is requested.
  691. * @request: scan request from cfg80211.
  692. * @action: scan action.
  693. *
  694. * Determines the P2P discovery state based to scan request parameters and
  695. * validates the channels in the request.
  696. */
  697. static s32 brcmf_p2p_run_escan(struct brcmf_cfg80211_info *cfg,
  698. struct brcmf_if *ifp,
  699. struct cfg80211_scan_request *request,
  700. u16 action)
  701. {
  702. struct brcmf_p2p_info *p2p = &cfg->p2p;
  703. s32 err = 0;
  704. s32 search_state = WL_P2P_DISC_ST_SCAN;
  705. struct brcmf_cfg80211_vif *vif;
  706. struct net_device *dev = NULL;
  707. int i, num_nodfs = 0;
  708. u16 *chanspecs;
  709. brcmf_dbg(TRACE, "enter\n");
  710. if (!request) {
  711. err = -EINVAL;
  712. goto exit;
  713. }
  714. if (request->n_channels) {
  715. chanspecs = kcalloc(request->n_channels, sizeof(*chanspecs),
  716. GFP_KERNEL);
  717. if (!chanspecs) {
  718. err = -ENOMEM;
  719. goto exit;
  720. }
  721. vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
  722. if (vif)
  723. dev = vif->wdev.netdev;
  724. if (request->n_channels == 3 &&
  725. request->channels[0]->hw_value == SOCIAL_CHAN_1 &&
  726. request->channels[1]->hw_value == SOCIAL_CHAN_2 &&
  727. request->channels[2]->hw_value == SOCIAL_CHAN_3) {
  728. /* SOCIAL CHANNELS 1, 6, 11 */
  729. search_state = WL_P2P_DISC_ST_SEARCH;
  730. brcmf_dbg(INFO, "P2P SEARCH PHASE START\n");
  731. } else if (dev != NULL && vif->mode == WL_MODE_AP) {
  732. /* If you are already a GO, then do SEARCH only */
  733. brcmf_dbg(INFO, "Already a GO. Do SEARCH Only\n");
  734. search_state = WL_P2P_DISC_ST_SEARCH;
  735. } else {
  736. brcmf_dbg(INFO, "P2P SCAN STATE START\n");
  737. }
  738. /*
  739. * no P2P scanning on passive or DFS channels.
  740. */
  741. for (i = 0; i < request->n_channels; i++) {
  742. struct ieee80211_channel *chan = request->channels[i];
  743. if (chan->flags & (IEEE80211_CHAN_RADAR |
  744. IEEE80211_CHAN_PASSIVE_SCAN))
  745. continue;
  746. chanspecs[i] = channel_to_chanspec(chan);
  747. brcmf_dbg(INFO, "%d: chan=%d, channel spec=%x\n",
  748. num_nodfs, chan->hw_value, chanspecs[i]);
  749. num_nodfs++;
  750. }
  751. err = brcmf_p2p_escan(p2p, num_nodfs, chanspecs, search_state,
  752. action, P2PAPI_BSSCFG_DEVICE);
  753. }
  754. exit:
  755. if (err)
  756. brcmf_err("error (%d)\n", err);
  757. return err;
  758. }
  759. /**
  760. * brcmf_p2p_find_listen_channel() - find listen channel in ie string.
  761. *
  762. * @ie: string of information elements.
  763. * @ie_len: length of string.
  764. *
  765. * Scan ie for p2p ie and look for attribute 6 channel. If available determine
  766. * channel and return it.
  767. */
  768. static s32 brcmf_p2p_find_listen_channel(const u8 *ie, u32 ie_len)
  769. {
  770. u8 channel_ie[5];
  771. s32 listen_channel;
  772. s32 err;
  773. err = cfg80211_get_p2p_attr(ie, ie_len,
  774. IEEE80211_P2P_ATTR_LISTEN_CHANNEL,
  775. channel_ie, sizeof(channel_ie));
  776. if (err < 0)
  777. return err;
  778. /* listen channel subel length format: */
  779. /* 3(country) + 1(op. class) + 1(chan num) */
  780. listen_channel = (s32)channel_ie[3 + 1];
  781. if (listen_channel == SOCIAL_CHAN_1 ||
  782. listen_channel == SOCIAL_CHAN_2 ||
  783. listen_channel == SOCIAL_CHAN_3) {
  784. brcmf_dbg(INFO, "Found my Listen Channel %d\n", listen_channel);
  785. return listen_channel;
  786. }
  787. return -EPERM;
  788. }
  789. /**
  790. * brcmf_p2p_scan_prep() - prepare scan based on request.
  791. *
  792. * @wiphy: wiphy device.
  793. * @request: scan request from cfg80211.
  794. * @vif: vif on which scan request is to be executed.
  795. *
  796. * Prepare the scan appropriately for type of scan requested. Overrides the
  797. * escan .run() callback for peer-to-peer scanning.
  798. */
  799. int brcmf_p2p_scan_prep(struct wiphy *wiphy,
  800. struct cfg80211_scan_request *request,
  801. struct brcmf_cfg80211_vif *vif)
  802. {
  803. struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
  804. struct brcmf_p2p_info *p2p = &cfg->p2p;
  805. int err = 0;
  806. if (brcmf_p2p_scan_is_p2p_request(request)) {
  807. /* find my listen channel */
  808. err = brcmf_p2p_find_listen_channel(request->ie,
  809. request->ie_len);
  810. if (err < 0)
  811. return err;
  812. p2p->afx_hdl.my_listen_chan = err;
  813. clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
  814. brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
  815. err = brcmf_p2p_enable_discovery(p2p);
  816. if (err)
  817. return err;
  818. vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
  819. /* override .run_escan() callback. */
  820. cfg->escan_info.run = brcmf_p2p_run_escan;
  821. }
  822. err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_PRBREQ_FLAG,
  823. request->ie, request->ie_len);
  824. return err;
  825. }
  826. /**
  827. * brcmf_p2p_discover_listen() - set firmware to discover listen state.
  828. *
  829. * @p2p: p2p device.
  830. * @channel: channel nr for discover listen.
  831. * @duration: time in ms to stay on channel.
  832. *
  833. */
  834. static s32
  835. brcmf_p2p_discover_listen(struct brcmf_p2p_info *p2p, u16 channel, u32 duration)
  836. {
  837. struct brcmf_cfg80211_vif *vif;
  838. s32 err = 0;
  839. u16 chanspec;
  840. vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
  841. if (!vif) {
  842. brcmf_err("Discovery is not set, so we have nothing to do\n");
  843. err = -EPERM;
  844. goto exit;
  845. }
  846. if (test_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status)) {
  847. brcmf_err("Previous LISTEN is not completed yet\n");
  848. /* WAR: prevent cookie mismatch in wpa_supplicant return OK */
  849. goto exit;
  850. }
  851. chanspec = brcmf_p2p_chnr_to_chspec(channel);
  852. err = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_LISTEN,
  853. chanspec, (u16)duration);
  854. if (!err) {
  855. set_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status);
  856. p2p->remain_on_channel_cookie++;
  857. }
  858. exit:
  859. return err;
  860. }
  861. /**
  862. * brcmf_p2p_remain_on_channel() - put device on channel and stay there.
  863. *
  864. * @wiphy: wiphy device.
  865. * @channel: channel to stay on.
  866. * @duration: time in ms to remain on channel.
  867. *
  868. */
  869. int brcmf_p2p_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
  870. struct ieee80211_channel *channel,
  871. unsigned int duration, u64 *cookie)
  872. {
  873. struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
  874. struct brcmf_p2p_info *p2p = &cfg->p2p;
  875. s32 err;
  876. u16 channel_nr;
  877. channel_nr = ieee80211_frequency_to_channel(channel->center_freq);
  878. brcmf_dbg(TRACE, "Enter, channel: %d, duration ms (%d)\n", channel_nr,
  879. duration);
  880. err = brcmf_p2p_enable_discovery(p2p);
  881. if (err)
  882. goto exit;
  883. err = brcmf_p2p_discover_listen(p2p, channel_nr, duration);
  884. if (err)
  885. goto exit;
  886. memcpy(&p2p->remain_on_channel, channel, sizeof(*channel));
  887. *cookie = p2p->remain_on_channel_cookie;
  888. cfg80211_ready_on_channel(wdev, *cookie, channel, duration, GFP_KERNEL);
  889. exit:
  890. return err;
  891. }
  892. /**
  893. * brcmf_p2p_notify_listen_complete() - p2p listen has completed.
  894. *
  895. * @ifp: interfac control.
  896. * @e: event message. Not used, to make it usable for fweh event dispatcher.
  897. * @data: payload of message. Not used.
  898. *
  899. */
  900. int brcmf_p2p_notify_listen_complete(struct brcmf_if *ifp,
  901. const struct brcmf_event_msg *e,
  902. void *data)
  903. {
  904. struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
  905. struct brcmf_p2p_info *p2p = &cfg->p2p;
  906. brcmf_dbg(TRACE, "Enter\n");
  907. if (test_and_clear_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN,
  908. &p2p->status)) {
  909. if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
  910. &p2p->status)) {
  911. clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
  912. &p2p->status);
  913. brcmf_dbg(INFO, "Listen DONE, wake up wait_next_af\n");
  914. complete(&p2p->wait_next_af);
  915. }
  916. cfg80211_remain_on_channel_expired(&ifp->vif->wdev,
  917. p2p->remain_on_channel_cookie,
  918. &p2p->remain_on_channel,
  919. GFP_KERNEL);
  920. }
  921. return 0;
  922. }
  923. /**
  924. * brcmf_p2p_cancel_remain_on_channel() - cancel p2p listen state.
  925. *
  926. * @ifp: interfac control.
  927. *
  928. */
  929. void brcmf_p2p_cancel_remain_on_channel(struct brcmf_if *ifp)
  930. {
  931. if (!ifp)
  932. return;
  933. brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
  934. brcmf_p2p_notify_listen_complete(ifp, NULL, NULL);
  935. }
  936. /**
  937. * brcmf_p2p_act_frm_search() - search function for action frame.
  938. *
  939. * @p2p: p2p device.
  940. * channel: channel on which action frame is to be trasmitted.
  941. *
  942. * search function to reach at common channel to send action frame. When
  943. * channel is 0 then all social channels will be used to send af
  944. */
  945. static s32 brcmf_p2p_act_frm_search(struct brcmf_p2p_info *p2p, u16 channel)
  946. {
  947. s32 err;
  948. u32 channel_cnt;
  949. u16 *default_chan_list;
  950. u32 i;
  951. brcmf_dbg(TRACE, "Enter\n");
  952. if (channel)
  953. channel_cnt = AF_PEER_SEARCH_CNT;
  954. else
  955. channel_cnt = SOCIAL_CHAN_CNT;
  956. default_chan_list = kzalloc(channel_cnt * sizeof(*default_chan_list),
  957. GFP_KERNEL);
  958. if (default_chan_list == NULL) {
  959. brcmf_err("channel list allocation failed\n");
  960. err = -ENOMEM;
  961. goto exit;
  962. }
  963. if (channel) {
  964. /* insert same channel to the chan_list */
  965. for (i = 0; i < channel_cnt; i++)
  966. default_chan_list[i] =
  967. brcmf_p2p_chnr_to_chspec(channel);
  968. } else {
  969. default_chan_list[0] = brcmf_p2p_chnr_to_chspec(SOCIAL_CHAN_1);
  970. default_chan_list[1] = brcmf_p2p_chnr_to_chspec(SOCIAL_CHAN_2);
  971. default_chan_list[2] = brcmf_p2p_chnr_to_chspec(SOCIAL_CHAN_3);
  972. }
  973. err = brcmf_p2p_escan(p2p, channel_cnt, default_chan_list,
  974. WL_P2P_DISC_ST_SEARCH, WL_ESCAN_ACTION_START,
  975. P2PAPI_BSSCFG_DEVICE);
  976. kfree(default_chan_list);
  977. exit:
  978. return err;
  979. }
  980. /**
  981. * brcmf_p2p_afx_handler() - afx worker thread.
  982. *
  983. * @work:
  984. *
  985. */
  986. static void brcmf_p2p_afx_handler(struct work_struct *work)
  987. {
  988. struct afx_hdl *afx_hdl = container_of(work, struct afx_hdl, afx_work);
  989. struct brcmf_p2p_info *p2p = container_of(afx_hdl,
  990. struct brcmf_p2p_info,
  991. afx_hdl);
  992. s32 err;
  993. if (!afx_hdl->is_active)
  994. return;
  995. if (afx_hdl->is_listen && afx_hdl->my_listen_chan)
  996. /* 100ms ~ 300ms */
  997. err = brcmf_p2p_discover_listen(p2p, afx_hdl->my_listen_chan,
  998. 100 * (1 + (random32() % 3)));
  999. else
  1000. err = brcmf_p2p_act_frm_search(p2p, afx_hdl->peer_listen_chan);
  1001. if (err) {
  1002. brcmf_err("ERROR occurred! value is (%d)\n", err);
  1003. if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
  1004. &p2p->status))
  1005. complete(&afx_hdl->act_frm_scan);
  1006. }
  1007. }
  1008. /**
  1009. * brcmf_p2p_af_searching_channel() - search channel.
  1010. *
  1011. * @p2p: p2p device info struct.
  1012. *
  1013. */
  1014. static s32 brcmf_p2p_af_searching_channel(struct brcmf_p2p_info *p2p)
  1015. {
  1016. struct afx_hdl *afx_hdl = &p2p->afx_hdl;
  1017. struct brcmf_cfg80211_vif *pri_vif;
  1018. unsigned long duration;
  1019. s32 retry;
  1020. brcmf_dbg(TRACE, "Enter\n");
  1021. pri_vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
  1022. INIT_COMPLETION(afx_hdl->act_frm_scan);
  1023. set_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
  1024. afx_hdl->is_active = true;
  1025. afx_hdl->peer_chan = P2P_INVALID_CHANNEL;
  1026. /* Loop to wait until we find a peer's channel or the
  1027. * pending action frame tx is cancelled.
  1028. */
  1029. retry = 0;
  1030. duration = msecs_to_jiffies(P2P_AF_FRM_SCAN_MAX_WAIT);
  1031. while ((retry < P2P_CHANNEL_SYNC_RETRY) &&
  1032. (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)) {
  1033. afx_hdl->is_listen = false;
  1034. brcmf_dbg(TRACE, "Scheduling action frame for sending.. (%d)\n",
  1035. retry);
  1036. /* search peer on peer's listen channel */
  1037. schedule_work(&afx_hdl->afx_work);
  1038. wait_for_completion_timeout(&afx_hdl->act_frm_scan, duration);
  1039. if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
  1040. (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
  1041. &p2p->status)))
  1042. break;
  1043. if (afx_hdl->my_listen_chan) {
  1044. brcmf_dbg(TRACE, "Scheduling listen peer, channel=%d\n",
  1045. afx_hdl->my_listen_chan);
  1046. /* listen on my listen channel */
  1047. afx_hdl->is_listen = true;
  1048. schedule_work(&afx_hdl->afx_work);
  1049. wait_for_completion_timeout(&afx_hdl->act_frm_scan,
  1050. duration);
  1051. }
  1052. if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
  1053. (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
  1054. &p2p->status)))
  1055. break;
  1056. retry++;
  1057. /* if sta is connected or connecting, sleep for a while before
  1058. * retry af tx or finding a peer
  1059. */
  1060. if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &pri_vif->sme_state) ||
  1061. test_bit(BRCMF_VIF_STATUS_CONNECTING, &pri_vif->sme_state))
  1062. msleep(P2P_DEFAULT_SLEEP_TIME_VSDB);
  1063. }
  1064. brcmf_dbg(TRACE, "Completed search/listen peer_chan=%d\n",
  1065. afx_hdl->peer_chan);
  1066. afx_hdl->is_active = false;
  1067. clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
  1068. return afx_hdl->peer_chan;
  1069. }
  1070. /**
  1071. * brcmf_p2p_scan_finding_common_channel() - was escan used for finding channel
  1072. *
  1073. * @cfg: common configuration struct.
  1074. * @bi: bss info struct, result from scan.
  1075. *
  1076. */
  1077. bool brcmf_p2p_scan_finding_common_channel(struct brcmf_cfg80211_info *cfg,
  1078. struct brcmf_bss_info_le *bi)
  1079. {
  1080. struct brcmf_p2p_info *p2p = &cfg->p2p;
  1081. struct afx_hdl *afx_hdl = &p2p->afx_hdl;
  1082. u8 *ie;
  1083. s32 err;
  1084. u8 p2p_dev_addr[ETH_ALEN];
  1085. if (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status))
  1086. return false;
  1087. if (bi == NULL) {
  1088. brcmf_dbg(TRACE, "ACTION FRAME SCAN Done\n");
  1089. if (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)
  1090. complete(&afx_hdl->act_frm_scan);
  1091. return true;
  1092. }
  1093. ie = ((u8 *)bi) + le16_to_cpu(bi->ie_offset);
  1094. memset(p2p_dev_addr, 0, sizeof(p2p_dev_addr));
  1095. err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
  1096. IEEE80211_P2P_ATTR_DEVICE_INFO,
  1097. p2p_dev_addr, sizeof(p2p_dev_addr));
  1098. if (err < 0)
  1099. err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
  1100. IEEE80211_P2P_ATTR_DEVICE_ID,
  1101. p2p_dev_addr, sizeof(p2p_dev_addr));
  1102. if ((err >= 0) &&
  1103. (!memcmp(p2p_dev_addr, afx_hdl->tx_dst_addr, ETH_ALEN))) {
  1104. afx_hdl->peer_chan = bi->ctl_ch ? bi->ctl_ch :
  1105. CHSPEC_CHANNEL(le16_to_cpu(bi->chanspec));
  1106. brcmf_dbg(TRACE, "ACTION FRAME SCAN : Peer %pM found, channel : %d\n",
  1107. afx_hdl->tx_dst_addr, afx_hdl->peer_chan);
  1108. complete(&afx_hdl->act_frm_scan);
  1109. }
  1110. return true;
  1111. }
  1112. /**
  1113. * brcmf_p2p_stop_wait_next_action_frame() - finish scan if af tx complete.
  1114. *
  1115. * @cfg: common configuration struct.
  1116. *
  1117. */
  1118. static void
  1119. brcmf_p2p_stop_wait_next_action_frame(struct brcmf_cfg80211_info *cfg)
  1120. {
  1121. struct brcmf_p2p_info *p2p = &cfg->p2p;
  1122. struct brcmf_if *ifp = cfg->escan_info.ifp;
  1123. if (test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status) &&
  1124. (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status) ||
  1125. test_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status))) {
  1126. brcmf_dbg(TRACE, "*** Wake UP ** abort actframe iovar\n");
  1127. /* if channel is not zero, "actfame" uses off channel scan.
  1128. * So abort scan for off channel completion.
  1129. */
  1130. if (p2p->af_sent_channel)
  1131. brcmf_notify_escan_complete(cfg, ifp, true, true);
  1132. } else if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
  1133. &p2p->status)) {
  1134. brcmf_dbg(TRACE, "*** Wake UP ** abort listen for next af frame\n");
  1135. /* So abort scan to cancel listen */
  1136. brcmf_notify_escan_complete(cfg, ifp, true, true);
  1137. }
  1138. }
  1139. /**
  1140. * brcmf_p2p_gon_req_collision() - Check if go negotiaton collission
  1141. *
  1142. * @p2p: p2p device info struct.
  1143. *
  1144. * return true if recevied action frame is to be dropped.
  1145. */
  1146. static bool
  1147. brcmf_p2p_gon_req_collision(struct brcmf_p2p_info *p2p, u8 *mac)
  1148. {
  1149. struct brcmf_cfg80211_info *cfg = p2p->cfg;
  1150. struct brcmf_if *ifp;
  1151. brcmf_dbg(TRACE, "Enter\n");
  1152. if (!test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) ||
  1153. !p2p->gon_req_action)
  1154. return false;
  1155. brcmf_dbg(TRACE, "GO Negotiation Request COLLISION !!!\n");
  1156. /* if sa(peer) addr is less than da(my) addr, then this device
  1157. * process peer's gon request and block to send gon req.
  1158. * if not (sa addr > da addr),
  1159. * this device will process gon request and drop gon req of peer.
  1160. */
  1161. ifp = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->ifp;
  1162. if (memcmp(mac, ifp->mac_addr, ETH_ALEN) < 0) {
  1163. brcmf_dbg(INFO, "Block transmit gon req !!!\n");
  1164. p2p->block_gon_req_tx = true;
  1165. /* if we are finding a common channel for sending af,
  1166. * do not scan more to block to send current gon req
  1167. */
  1168. if (test_and_clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
  1169. &p2p->status))
  1170. complete(&p2p->afx_hdl.act_frm_scan);
  1171. if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
  1172. &p2p->status))
  1173. brcmf_p2p_stop_wait_next_action_frame(cfg);
  1174. return false;
  1175. }
  1176. /* drop gon request of peer to process gon request by this device. */
  1177. brcmf_dbg(INFO, "Drop received gon req !!!\n");
  1178. return true;
  1179. }
  1180. /**
  1181. * brcmf_p2p_notify_action_frame_rx() - received action frame.
  1182. *
  1183. * @ifp: interfac control.
  1184. * @e: event message. Not used, to make it usable for fweh event dispatcher.
  1185. * @data: payload of message, containing action frame data.
  1186. *
  1187. */
  1188. int brcmf_p2p_notify_action_frame_rx(struct brcmf_if *ifp,
  1189. const struct brcmf_event_msg *e,
  1190. void *data)
  1191. {
  1192. struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
  1193. struct brcmf_p2p_info *p2p = &cfg->p2p;
  1194. struct afx_hdl *afx_hdl = &p2p->afx_hdl;
  1195. struct wireless_dev *wdev;
  1196. u32 mgmt_frame_len = e->datalen - sizeof(struct brcmf_rx_mgmt_data);
  1197. struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
  1198. u8 *frame = (u8 *)(rxframe + 1);
  1199. struct brcmf_p2p_pub_act_frame *act_frm;
  1200. struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
  1201. u16 chanspec = be16_to_cpu(rxframe->chanspec);
  1202. struct ieee80211_mgmt *mgmt_frame;
  1203. s32 freq;
  1204. u16 mgmt_type;
  1205. u8 action;
  1206. /* Check if wpa_supplicant has registered for this frame */
  1207. brcmf_dbg(INFO, "ifp->vif->mgmt_rx_reg %04x\n", ifp->vif->mgmt_rx_reg);
  1208. mgmt_type = (IEEE80211_STYPE_ACTION & IEEE80211_FCTL_STYPE) >> 4;
  1209. if ((ifp->vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
  1210. return 0;
  1211. brcmf_p2p_print_actframe(false, frame, mgmt_frame_len);
  1212. action = P2P_PAF_SUBTYPE_INVALID;
  1213. if (brcmf_p2p_is_pub_action(frame, mgmt_frame_len)) {
  1214. act_frm = (struct brcmf_p2p_pub_act_frame *)frame;
  1215. action = act_frm->subtype;
  1216. if ((action == P2P_PAF_GON_REQ) &&
  1217. (brcmf_p2p_gon_req_collision(p2p, (u8 *)e->addr))) {
  1218. if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
  1219. &p2p->status) &&
  1220. (memcmp(afx_hdl->tx_dst_addr, e->addr,
  1221. ETH_ALEN) == 0)) {
  1222. afx_hdl->peer_chan = CHSPEC_CHANNEL(chanspec);
  1223. brcmf_dbg(INFO, "GON request: Peer found, channel=%d\n",
  1224. afx_hdl->peer_chan);
  1225. complete(&afx_hdl->act_frm_scan);
  1226. }
  1227. return 0;
  1228. }
  1229. /* After complete GO Negotiation, roll back to mpc mode */
  1230. if ((action == P2P_PAF_GON_CONF) ||
  1231. (action == P2P_PAF_PROVDIS_RSP))
  1232. brcmf_set_mpc(ifp, 1);
  1233. if (action == P2P_PAF_GON_CONF) {
  1234. brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
  1235. clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
  1236. }
  1237. } else if (brcmf_p2p_is_gas_action(frame, mgmt_frame_len)) {
  1238. sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
  1239. action = sd_act_frm->action;
  1240. }
  1241. if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
  1242. (p2p->next_af_subtype == action)) {
  1243. brcmf_dbg(TRACE, "We got a right next frame! (%d)\n", action);
  1244. clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
  1245. &p2p->status);
  1246. /* Stop waiting for next AF. */
  1247. brcmf_p2p_stop_wait_next_action_frame(cfg);
  1248. }
  1249. mgmt_frame = kzalloc(offsetof(struct ieee80211_mgmt, u) +
  1250. mgmt_frame_len, GFP_KERNEL);
  1251. if (!mgmt_frame) {
  1252. brcmf_err("No memory available for action frame\n");
  1253. return -ENOMEM;
  1254. }
  1255. memcpy(mgmt_frame->da, ifp->mac_addr, ETH_ALEN);
  1256. brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mgmt_frame->bssid,
  1257. ETH_ALEN);
  1258. memcpy(mgmt_frame->sa, e->addr, ETH_ALEN);
  1259. mgmt_frame->frame_control = cpu_to_le16(IEEE80211_STYPE_ACTION);
  1260. memcpy(&mgmt_frame->u, frame, mgmt_frame_len);
  1261. mgmt_frame_len += offsetof(struct ieee80211_mgmt, u);
  1262. freq = ieee80211_channel_to_frequency(CHSPEC_CHANNEL(chanspec),
  1263. CHSPEC_IS2G(chanspec) ?
  1264. IEEE80211_BAND_2GHZ :
  1265. IEEE80211_BAND_5GHZ);
  1266. wdev = &ifp->vif->wdev;
  1267. cfg80211_rx_mgmt(wdev, freq, 0, (u8 *)mgmt_frame, mgmt_frame_len,
  1268. GFP_ATOMIC);
  1269. kfree(mgmt_frame);
  1270. return 0;
  1271. }
  1272. /**
  1273. * brcmf_p2p_notify_action_tx_complete() - transmit action frame complete
  1274. *
  1275. * @ifp: interfac control.
  1276. * @e: event message. Not used, to make it usable for fweh event dispatcher.
  1277. * @data: not used.
  1278. *
  1279. */
  1280. int brcmf_p2p_notify_action_tx_complete(struct brcmf_if *ifp,
  1281. const struct brcmf_event_msg *e,
  1282. void *data)
  1283. {
  1284. struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
  1285. struct brcmf_p2p_info *p2p = &cfg->p2p;
  1286. brcmf_dbg(INFO, "Enter: event %s, status=%d\n",
  1287. e->event_code == BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE ?
  1288. "ACTION_FRAME_OFF_CHAN_COMPLETE" : "ACTION_FRAME_COMPLETE",
  1289. e->status);
  1290. if (!test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status))
  1291. return 0;
  1292. if (e->event_code == BRCMF_E_ACTION_FRAME_COMPLETE) {
  1293. if (e->status == BRCMF_E_STATUS_SUCCESS)
  1294. set_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED,
  1295. &p2p->status);
  1296. else {
  1297. set_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
  1298. /* If there is no ack, we don't need to wait for
  1299. * WLC_E_ACTION_FRAME_OFFCHAN_COMPLETE event
  1300. */
  1301. brcmf_p2p_stop_wait_next_action_frame(cfg);
  1302. }
  1303. } else {
  1304. complete(&p2p->send_af_done);
  1305. }
  1306. return 0;
  1307. }
  1308. /**
  1309. * brcmf_p2p_tx_action_frame() - send action frame over fil.
  1310. *
  1311. * @p2p: p2p info struct for vif.
  1312. * @af_params: action frame data/info.
  1313. *
  1314. * Send an action frame immediately without doing channel synchronization.
  1315. *
  1316. * This function waits for a completion event before returning.
  1317. * The WLC_E_ACTION_FRAME_COMPLETE event will be received when the action
  1318. * frame is transmitted.
  1319. */
  1320. static s32 brcmf_p2p_tx_action_frame(struct brcmf_p2p_info *p2p,
  1321. struct brcmf_fil_af_params_le *af_params)
  1322. {
  1323. struct brcmf_cfg80211_vif *vif;
  1324. s32 err = 0;
  1325. s32 timeout = 0;
  1326. brcmf_dbg(TRACE, "Enter\n");
  1327. INIT_COMPLETION(p2p->send_af_done);
  1328. clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
  1329. clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
  1330. vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
  1331. err = brcmf_fil_bsscfg_data_set(vif->ifp, "actframe", af_params,
  1332. sizeof(*af_params));
  1333. if (err) {
  1334. brcmf_err(" sending action frame has failed\n");
  1335. goto exit;
  1336. }
  1337. p2p->af_sent_channel = le32_to_cpu(af_params->channel);
  1338. p2p->af_tx_sent_jiffies = jiffies;
  1339. timeout = wait_for_completion_timeout(&p2p->send_af_done,
  1340. msecs_to_jiffies(P2P_AF_MAX_WAIT_TIME));
  1341. if (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status)) {
  1342. brcmf_dbg(TRACE, "TX action frame operation is success\n");
  1343. } else {
  1344. err = -EIO;
  1345. brcmf_dbg(TRACE, "TX action frame operation has failed\n");
  1346. }
  1347. /* clear status bit for action tx */
  1348. clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
  1349. clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
  1350. exit:
  1351. return err;
  1352. }
  1353. /**
  1354. * brcmf_p2p_pub_af_tx() - public action frame tx routine.
  1355. *
  1356. * @cfg: driver private data for cfg80211 interface.
  1357. * @af_params: action frame data/info.
  1358. * @config_af_params: configuration data for action frame.
  1359. *
  1360. * routine which transmits ation frame public type.
  1361. */
  1362. static s32 brcmf_p2p_pub_af_tx(struct brcmf_cfg80211_info *cfg,
  1363. struct brcmf_fil_af_params_le *af_params,
  1364. struct brcmf_config_af_params *config_af_params)
  1365. {
  1366. struct brcmf_p2p_info *p2p = &cfg->p2p;
  1367. struct brcmf_fil_action_frame_le *action_frame;
  1368. struct brcmf_p2p_pub_act_frame *act_frm;
  1369. s32 err = 0;
  1370. u16 ie_len;
  1371. action_frame = &af_params->action_frame;
  1372. act_frm = (struct brcmf_p2p_pub_act_frame *)(action_frame->data);
  1373. config_af_params->extra_listen = true;
  1374. switch (act_frm->subtype) {
  1375. case P2P_PAF_GON_REQ:
  1376. brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status set\n");
  1377. set_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
  1378. config_af_params->mpc_onoff = 0;
  1379. config_af_params->search_channel = true;
  1380. p2p->next_af_subtype = act_frm->subtype + 1;
  1381. p2p->gon_req_action = true;
  1382. /* increase dwell time to wait for RESP frame */
  1383. af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
  1384. break;
  1385. case P2P_PAF_GON_RSP:
  1386. p2p->next_af_subtype = act_frm->subtype + 1;
  1387. /* increase dwell time to wait for CONF frame */
  1388. af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
  1389. break;
  1390. case P2P_PAF_GON_CONF:
  1391. /* If we reached till GO Neg confirmation reset the filter */
  1392. brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
  1393. clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
  1394. /* turn on mpc again if go nego is done */
  1395. config_af_params->mpc_onoff = 1;
  1396. /* minimize dwell time */
  1397. af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
  1398. config_af_params->extra_listen = false;
  1399. break;
  1400. case P2P_PAF_INVITE_REQ:
  1401. config_af_params->search_channel = true;
  1402. p2p->next_af_subtype = act_frm->subtype + 1;
  1403. /* increase dwell time */
  1404. af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
  1405. break;
  1406. case P2P_PAF_INVITE_RSP:
  1407. /* minimize dwell time */
  1408. af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
  1409. config_af_params->extra_listen = false;
  1410. break;
  1411. case P2P_PAF_DEVDIS_REQ:
  1412. config_af_params->search_channel = true;
  1413. p2p->next_af_subtype = act_frm->subtype + 1;
  1414. /* maximize dwell time to wait for RESP frame */
  1415. af_params->dwell_time = cpu_to_le32(P2P_AF_LONG_DWELL_TIME);
  1416. break;
  1417. case P2P_PAF_DEVDIS_RSP:
  1418. /* minimize dwell time */
  1419. af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
  1420. config_af_params->extra_listen = false;
  1421. break;
  1422. case P2P_PAF_PROVDIS_REQ:
  1423. ie_len = le16_to_cpu(action_frame->len) -
  1424. offsetof(struct brcmf_p2p_pub_act_frame, elts);
  1425. if (cfg80211_get_p2p_attr(&act_frm->elts[0], ie_len,
  1426. IEEE80211_P2P_ATTR_GROUP_ID,
  1427. NULL, 0) < 0)
  1428. config_af_params->search_channel = true;
  1429. config_af_params->mpc_onoff = 0;
  1430. p2p->next_af_subtype = act_frm->subtype + 1;
  1431. /* increase dwell time to wait for RESP frame */
  1432. af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
  1433. break;
  1434. case P2P_PAF_PROVDIS_RSP:
  1435. /* wpa_supplicant send go nego req right after prov disc */
  1436. p2p->next_af_subtype = P2P_PAF_GON_REQ;
  1437. /* increase dwell time to MED level */
  1438. af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
  1439. config_af_params->extra_listen = false;
  1440. break;
  1441. default:
  1442. brcmf_err("Unknown p2p pub act frame subtype: %d\n",
  1443. act_frm->subtype);
  1444. err = -EINVAL;
  1445. }
  1446. return err;
  1447. }
  1448. /**
  1449. * brcmf_p2p_send_action_frame() - send action frame .
  1450. *
  1451. * @cfg: driver private data for cfg80211 interface.
  1452. * @ndev: net device to transmit on.
  1453. * @af_params: configuration data for action frame.
  1454. */
  1455. bool brcmf_p2p_send_action_frame(struct brcmf_cfg80211_info *cfg,
  1456. struct net_device *ndev,
  1457. struct brcmf_fil_af_params_le *af_params)
  1458. {
  1459. struct brcmf_p2p_info *p2p = &cfg->p2p;
  1460. struct brcmf_if *ifp = netdev_priv(ndev);
  1461. struct brcmf_fil_action_frame_le *action_frame;
  1462. struct brcmf_config_af_params config_af_params;
  1463. struct afx_hdl *afx_hdl = &p2p->afx_hdl;
  1464. u16 action_frame_len;
  1465. bool ack = false;
  1466. u8 category;
  1467. u8 action;
  1468. s32 tx_retry;
  1469. s32 extra_listen_time;
  1470. uint delta_ms;
  1471. action_frame = &af_params->action_frame;
  1472. action_frame_len = le16_to_cpu(action_frame->len);
  1473. brcmf_p2p_print_actframe(true, action_frame->data, action_frame_len);
  1474. /* Add the default dwell time. Dwell time to stay off-channel */
  1475. /* to wait for a response action frame after transmitting an */
  1476. /* GO Negotiation action frame */
  1477. af_params->dwell_time = cpu_to_le32(P2P_AF_DWELL_TIME);
  1478. category = action_frame->data[DOT11_ACTION_CAT_OFF];
  1479. action = action_frame->data[DOT11_ACTION_ACT_OFF];
  1480. /* initialize variables */
  1481. p2p->next_af_subtype = P2P_PAF_SUBTYPE_INVALID;
  1482. p2p->gon_req_action = false;
  1483. /* config parameters */
  1484. config_af_params.mpc_onoff = -1;
  1485. config_af_params.search_channel = false;
  1486. config_af_params.extra_listen = false;
  1487. if (brcmf_p2p_is_pub_action(action_frame->data, action_frame_len)) {
  1488. /* p2p public action frame process */
  1489. if (brcmf_p2p_pub_af_tx(cfg, af_params, &config_af_params)) {
  1490. /* Just send unknown subtype frame with */
  1491. /* default parameters. */
  1492. brcmf_err("P2P Public action frame, unknown subtype.\n");
  1493. }
  1494. } else if (brcmf_p2p_is_gas_action(action_frame->data,
  1495. action_frame_len)) {
  1496. /* service discovery process */
  1497. if (action == P2PSD_ACTION_ID_GAS_IREQ ||
  1498. action == P2PSD_ACTION_ID_GAS_CREQ) {
  1499. /* configure service discovery query frame */
  1500. config_af_params.search_channel = true;
  1501. /* save next af suptype to cancel */
  1502. /* remaining dwell time */
  1503. p2p->next_af_subtype = action + 1;
  1504. af_params->dwell_time =
  1505. cpu_to_le32(P2P_AF_MED_DWELL_TIME);
  1506. } else if (action == P2PSD_ACTION_ID_GAS_IRESP ||
  1507. action == P2PSD_ACTION_ID_GAS_CRESP) {
  1508. /* configure service discovery response frame */
  1509. af_params->dwell_time =
  1510. cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
  1511. } else {
  1512. brcmf_err("Unknown action type: %d\n", action);
  1513. goto exit;
  1514. }
  1515. } else if (brcmf_p2p_is_p2p_action(action_frame->data,
  1516. action_frame_len)) {
  1517. /* do not configure anything. it will be */
  1518. /* sent with a default configuration */
  1519. } else {
  1520. brcmf_err("Unknown Frame: category 0x%x, action 0x%x\n",
  1521. category, action);
  1522. return false;
  1523. }
  1524. /* if connecting on primary iface, sleep for a while before sending
  1525. * af tx for VSDB
  1526. */
  1527. if (test_bit(BRCMF_VIF_STATUS_CONNECTING,
  1528. &p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->sme_state))
  1529. msleep(50);
  1530. /* if scan is ongoing, abort current scan. */
  1531. if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
  1532. brcmf_abort_scanning(cfg);
  1533. memcpy(afx_hdl->tx_dst_addr, action_frame->da, ETH_ALEN);
  1534. /* To make sure to send successfully action frame, turn off mpc */
  1535. if (config_af_params.mpc_onoff == 0)
  1536. brcmf_set_mpc(ifp, 0);
  1537. /* set status and destination address before sending af */
  1538. if (p2p->next_af_subtype != P2P_PAF_SUBTYPE_INVALID) {
  1539. /* set status to cancel the remained dwell time in rx process */
  1540. set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
  1541. }
  1542. p2p->af_sent_channel = 0;
  1543. set_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
  1544. /* validate channel and p2p ies */
  1545. if (config_af_params.search_channel &&
  1546. IS_P2P_SOCIAL_CHANNEL(le32_to_cpu(af_params->channel)) &&
  1547. p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->saved_ie.probe_req_ie_len) {
  1548. afx_hdl = &p2p->afx_hdl;
  1549. afx_hdl->peer_listen_chan = le32_to_cpu(af_params->channel);
  1550. if (brcmf_p2p_af_searching_channel(p2p) ==
  1551. P2P_INVALID_CHANNEL) {
  1552. brcmf_err("Couldn't find peer's channel.\n");
  1553. goto exit;
  1554. }
  1555. /* Abort scan even for VSDB scenarios. Scan gets aborted in
  1556. * firmware but after the check of piggyback algorithm. To take
  1557. * care of current piggback algo, lets abort the scan here
  1558. * itself.
  1559. */
  1560. brcmf_notify_escan_complete(cfg, ifp, true, true);
  1561. /* update channel */
  1562. af_params->channel = cpu_to_le32(afx_hdl->peer_chan);
  1563. }
  1564. tx_retry = 0;
  1565. while (!p2p->block_gon_req_tx &&
  1566. (ack == false) && (tx_retry < P2P_AF_TX_MAX_RETRY)) {
  1567. ack = !brcmf_p2p_tx_action_frame(p2p, af_params);
  1568. tx_retry++;
  1569. }
  1570. if (ack == false) {
  1571. brcmf_err("Failed to send Action Frame(retry %d)\n", tx_retry);
  1572. clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
  1573. }
  1574. exit:
  1575. clear_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
  1576. /* WAR: sometimes dongle does not keep the dwell time of 'actframe'.
  1577. * if we coundn't get the next action response frame and dongle does
  1578. * not keep the dwell time, go to listen state again to get next action
  1579. * response frame.
  1580. */
  1581. if (ack && config_af_params.extra_listen && !p2p->block_gon_req_tx &&
  1582. test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
  1583. p2p->af_sent_channel == afx_hdl->my_listen_chan) {
  1584. delta_ms = jiffies_to_msecs(jiffies - p2p->af_tx_sent_jiffies);
  1585. if (le32_to_cpu(af_params->dwell_time) > delta_ms)
  1586. extra_listen_time = le32_to_cpu(af_params->dwell_time) -
  1587. delta_ms;
  1588. else
  1589. extra_listen_time = 0;
  1590. if (extra_listen_time > 50) {
  1591. set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
  1592. &p2p->status);
  1593. brcmf_dbg(INFO, "Wait more time! actual af time:%d, calculated extra listen:%d\n",
  1594. le32_to_cpu(af_params->dwell_time),
  1595. extra_listen_time);
  1596. extra_listen_time += 100;
  1597. if (!brcmf_p2p_discover_listen(p2p,
  1598. p2p->af_sent_channel,
  1599. extra_listen_time)) {
  1600. unsigned long duration;
  1601. extra_listen_time += 100;
  1602. duration = msecs_to_jiffies(extra_listen_time);
  1603. wait_for_completion_timeout(&p2p->wait_next_af,
  1604. duration);
  1605. }
  1606. clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
  1607. &p2p->status);
  1608. }
  1609. }
  1610. if (p2p->block_gon_req_tx) {
  1611. /* if ack is true, supplicant will wait more time(100ms).
  1612. * so we will return it as a success to get more time .
  1613. */
  1614. p2p->block_gon_req_tx = false;
  1615. ack = true;
  1616. }
  1617. clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
  1618. /* if all done, turn mpc on again */
  1619. if (config_af_params.mpc_onoff == 1)
  1620. brcmf_set_mpc(ifp, 1);
  1621. return ack;
  1622. }
  1623. /**
  1624. * brcmf_p2p_notify_rx_mgmt_p2p_probereq() - Event handler for p2p probe req.
  1625. *
  1626. * @ifp: interface pointer for which event was received.
  1627. * @e: even message.
  1628. * @data: payload of event message (probe request).
  1629. */
  1630. s32 brcmf_p2p_notify_rx_mgmt_p2p_probereq(struct brcmf_if *ifp,
  1631. const struct brcmf_event_msg *e,
  1632. void *data)
  1633. {
  1634. struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
  1635. struct brcmf_p2p_info *p2p = &cfg->p2p;
  1636. struct afx_hdl *afx_hdl = &p2p->afx_hdl;
  1637. struct brcmf_cfg80211_vif *vif = ifp->vif;
  1638. struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
  1639. u16 chanspec = be16_to_cpu(rxframe->chanspec);
  1640. u8 *mgmt_frame;
  1641. u32 mgmt_frame_len;
  1642. s32 freq;
  1643. u16 mgmt_type;
  1644. brcmf_dbg(INFO, "Enter: event %d reason %d\n", e->event_code,
  1645. e->reason);
  1646. if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status) &&
  1647. (memcmp(afx_hdl->tx_dst_addr, e->addr, ETH_ALEN) == 0)) {
  1648. afx_hdl->peer_chan = CHSPEC_CHANNEL(chanspec);
  1649. brcmf_dbg(INFO, "PROBE REQUEST: Peer found, channel=%d\n",
  1650. afx_hdl->peer_chan);
  1651. complete(&afx_hdl->act_frm_scan);
  1652. }
  1653. /* Firmware sends us two proberesponses for each idx one. At the */
  1654. /* moment anything but bsscfgidx 0 is passed up to supplicant */
  1655. if (e->bsscfgidx == 0)
  1656. return 0;
  1657. /* Filter any P2P probe reqs arriving during the GO-NEG Phase */
  1658. if (test_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status)) {
  1659. brcmf_dbg(INFO, "Filtering P2P probe_req in GO-NEG phase\n");
  1660. return 0;
  1661. }
  1662. /* Check if wpa_supplicant has registered for this frame */
  1663. brcmf_dbg(INFO, "vif->mgmt_rx_reg %04x\n", vif->mgmt_rx_reg);
  1664. mgmt_type = (IEEE80211_STYPE_PROBE_REQ & IEEE80211_FCTL_STYPE) >> 4;
  1665. if ((vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
  1666. return 0;
  1667. mgmt_frame = (u8 *)(rxframe + 1);
  1668. mgmt_frame_len = e->datalen - sizeof(*rxframe);
  1669. freq = ieee80211_channel_to_frequency(CHSPEC_CHANNEL(chanspec),
  1670. CHSPEC_IS2G(chanspec) ?
  1671. IEEE80211_BAND_2GHZ :
  1672. IEEE80211_BAND_5GHZ);
  1673. cfg80211_rx_mgmt(&vif->wdev, freq, 0, mgmt_frame, mgmt_frame_len,
  1674. GFP_ATOMIC);
  1675. brcmf_dbg(INFO, "mgmt_frame_len (%d) , e->datalen (%d), chanspec (%04x), freq (%d)\n",
  1676. mgmt_frame_len, e->datalen, chanspec, freq);
  1677. return 0;
  1678. }
  1679. /**
  1680. * brcmf_p2p_attach() - attach for P2P.
  1681. *
  1682. * @cfg: driver private data for cfg80211 interface.
  1683. */
  1684. s32 brcmf_p2p_attach(struct brcmf_cfg80211_info *cfg)
  1685. {
  1686. struct brcmf_if *pri_ifp;
  1687. struct brcmf_if *p2p_ifp;
  1688. struct brcmf_cfg80211_vif *p2p_vif;
  1689. struct brcmf_p2p_info *p2p;
  1690. struct brcmf_pub *drvr;
  1691. s32 bssidx;
  1692. s32 err = 0;
  1693. p2p = &cfg->p2p;
  1694. p2p->cfg = cfg;
  1695. drvr = cfg->pub;
  1696. pri_ifp = drvr->iflist[0];
  1697. p2p_ifp = drvr->iflist[1];
  1698. p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif = pri_ifp->vif;
  1699. if (p2p_ifp) {
  1700. p2p_vif = brcmf_alloc_vif(cfg, NL80211_IFTYPE_P2P_DEVICE,
  1701. false);
  1702. if (IS_ERR(p2p_vif)) {
  1703. brcmf_err("could not create discovery vif\n");
  1704. err = -ENOMEM;
  1705. goto exit;
  1706. }
  1707. p2p_vif->ifp = p2p_ifp;
  1708. p2p_ifp->vif = p2p_vif;
  1709. p2p_vif->wdev.netdev = p2p_ifp->ndev;
  1710. p2p_ifp->ndev->ieee80211_ptr = &p2p_vif->wdev;
  1711. SET_NETDEV_DEV(p2p_ifp->ndev, wiphy_dev(cfg->wiphy));
  1712. p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
  1713. brcmf_p2p_generate_bss_mac(p2p, NULL);
  1714. memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
  1715. brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
  1716. /* Initialize P2P Discovery in the firmware */
  1717. err = brcmf_fil_iovar_int_set(pri_ifp, "p2p_disc", 1);
  1718. if (err < 0) {
  1719. brcmf_err("set p2p_disc error\n");
  1720. brcmf_free_vif(p2p_vif);
  1721. goto exit;
  1722. }
  1723. /* obtain bsscfg index for P2P discovery */
  1724. err = brcmf_fil_iovar_int_get(pri_ifp, "p2p_dev", &bssidx);
  1725. if (err < 0) {
  1726. brcmf_err("retrieving discover bsscfg index failed\n");
  1727. brcmf_free_vif(p2p_vif);
  1728. goto exit;
  1729. }
  1730. /* Verify that firmware uses same bssidx as driver !! */
  1731. if (p2p_ifp->bssidx != bssidx) {
  1732. brcmf_err("Incorrect bssidx=%d, compared to p2p_ifp->bssidx=%d\n",
  1733. bssidx, p2p_ifp->bssidx);
  1734. brcmf_free_vif(p2p_vif);
  1735. goto exit;
  1736. }
  1737. init_completion(&p2p->send_af_done);
  1738. INIT_WORK(&p2p->afx_hdl.afx_work, brcmf_p2p_afx_handler);
  1739. init_completion(&p2p->afx_hdl.act_frm_scan);
  1740. init_completion(&p2p->wait_next_af);
  1741. }
  1742. exit:
  1743. return err;
  1744. }
  1745. /**
  1746. * brcmf_p2p_detach() - detach P2P.
  1747. *
  1748. * @p2p: P2P specific data.
  1749. */
  1750. void brcmf_p2p_detach(struct brcmf_p2p_info *p2p)
  1751. {
  1752. struct brcmf_cfg80211_vif *vif;
  1753. vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
  1754. if (vif != NULL) {
  1755. brcmf_p2p_cancel_remain_on_channel(vif->ifp);
  1756. brcmf_p2p_deinit_discovery(p2p);
  1757. /* remove discovery interface */
  1758. brcmf_free_vif(vif);
  1759. p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = NULL;
  1760. }
  1761. /* just set it all to zero */
  1762. memset(p2p, 0, sizeof(*p2p));
  1763. }
  1764. /**
  1765. * brcmf_p2p_get_current_chanspec() - Get current operation channel.
  1766. *
  1767. * @p2p: P2P specific data.
  1768. * @chanspec: chanspec to be returned.
  1769. */
  1770. static void brcmf_p2p_get_current_chanspec(struct brcmf_p2p_info *p2p,
  1771. u16 *chanspec)
  1772. {
  1773. struct brcmf_if *ifp;
  1774. struct brcmf_fil_chan_info_le ci;
  1775. s32 err;
  1776. ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
  1777. *chanspec = 11 & WL_CHANSPEC_CHAN_MASK;
  1778. err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_CHANNEL, &ci, sizeof(ci));
  1779. if (!err) {
  1780. *chanspec = le32_to_cpu(ci.hw_channel) & WL_CHANSPEC_CHAN_MASK;
  1781. if (*chanspec < CH_MAX_2G_CHANNEL)
  1782. *chanspec |= WL_CHANSPEC_BAND_2G;
  1783. else
  1784. *chanspec |= WL_CHANSPEC_BAND_5G;
  1785. }
  1786. *chanspec |= WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE;
  1787. }
  1788. /**
  1789. * Change a P2P Role.
  1790. * Parameters:
  1791. * @mac: MAC address of the BSS to change a role
  1792. * Returns 0 if success.
  1793. */
  1794. int brcmf_p2p_ifchange(struct brcmf_cfg80211_info *cfg,
  1795. enum brcmf_fil_p2p_if_types if_type)
  1796. {
  1797. struct brcmf_p2p_info *p2p = &cfg->p2p;
  1798. struct brcmf_cfg80211_vif *vif;
  1799. struct brcmf_fil_p2p_if_le if_request;
  1800. s32 err;
  1801. u16 chanspec;
  1802. brcmf_dbg(TRACE, "Enter\n");
  1803. vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
  1804. if (!vif) {
  1805. brcmf_err("vif for P2PAPI_BSSCFG_PRIMARY does not exist\n");
  1806. return -EPERM;
  1807. }
  1808. brcmf_notify_escan_complete(cfg, vif->ifp, true, true);
  1809. vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
  1810. if (!vif) {
  1811. brcmf_err("vif for P2PAPI_BSSCFG_CONNECTION does not exist\n");
  1812. return -EPERM;
  1813. }
  1814. brcmf_set_mpc(vif->ifp, 0);
  1815. /* In concurrency case, STA may be already associated in a particular */
  1816. /* channel. so retrieve the current channel of primary interface and */
  1817. /* then start the virtual interface on that. */
  1818. brcmf_p2p_get_current_chanspec(p2p, &chanspec);
  1819. if_request.type = cpu_to_le16((u16)if_type);
  1820. if_request.chspec = cpu_to_le16(chanspec);
  1821. memcpy(if_request.addr, p2p->int_addr, sizeof(if_request.addr));
  1822. brcmf_cfg80211_arm_vif_event(cfg, vif);
  1823. err = brcmf_fil_iovar_data_set(vif->ifp, "p2p_ifupd", &if_request,
  1824. sizeof(if_request));
  1825. if (err) {
  1826. brcmf_err("p2p_ifupd FAILED, err=%d\n", err);
  1827. brcmf_cfg80211_arm_vif_event(cfg, NULL);
  1828. return err;
  1829. }
  1830. err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_CHANGE,
  1831. msecs_to_jiffies(1500));
  1832. brcmf_cfg80211_arm_vif_event(cfg, NULL);
  1833. if (!err) {
  1834. brcmf_err("No BRCMF_E_IF_CHANGE event received\n");
  1835. return -EIO;
  1836. }
  1837. err = brcmf_fil_cmd_int_set(vif->ifp, BRCMF_C_SET_SCB_TIMEOUT,
  1838. BRCMF_SCB_TIMEOUT_VALUE);
  1839. return err;
  1840. }
  1841. static int brcmf_p2p_request_p2p_if(struct brcmf_p2p_info *p2p,
  1842. struct brcmf_if *ifp, u8 ea[ETH_ALEN],
  1843. enum brcmf_fil_p2p_if_types iftype)
  1844. {
  1845. struct brcmf_fil_p2p_if_le if_request;
  1846. int err;
  1847. u16 chanspec;
  1848. /* we need a default channel */
  1849. brcmf_p2p_get_current_chanspec(p2p, &chanspec);
  1850. /* fill the firmware request */
  1851. memcpy(if_request.addr, ea, ETH_ALEN);
  1852. if_request.type = cpu_to_le16((u16)iftype);
  1853. if_request.chspec = cpu_to_le16(chanspec);
  1854. err = brcmf_fil_iovar_data_set(ifp, "p2p_ifadd", &if_request,
  1855. sizeof(if_request));
  1856. if (err)
  1857. return err;
  1858. return err;
  1859. }
  1860. static int brcmf_p2p_disable_p2p_if(struct brcmf_cfg80211_vif *vif)
  1861. {
  1862. struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
  1863. struct net_device *pri_ndev = cfg_to_ndev(cfg);
  1864. struct brcmf_if *ifp = netdev_priv(pri_ndev);
  1865. u8 *addr = vif->wdev.netdev->dev_addr;
  1866. return brcmf_fil_iovar_data_set(ifp, "p2p_ifdis", addr, ETH_ALEN);
  1867. }
  1868. static int brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif *vif)
  1869. {
  1870. struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
  1871. struct net_device *pri_ndev = cfg_to_ndev(cfg);
  1872. struct brcmf_if *ifp = netdev_priv(pri_ndev);
  1873. u8 *addr = vif->wdev.netdev->dev_addr;
  1874. return brcmf_fil_iovar_data_set(ifp, "p2p_ifdel", addr, ETH_ALEN);
  1875. }
  1876. /**
  1877. * brcmf_p2p_create_p2pdev() - create a P2P_DEVICE virtual interface.
  1878. *
  1879. * @p2p: P2P specific data.
  1880. * @wiphy: wiphy device of new interface.
  1881. * @addr: mac address for this new interface.
  1882. */
  1883. static struct wireless_dev *brcmf_p2p_create_p2pdev(struct brcmf_p2p_info *p2p,
  1884. struct wiphy *wiphy,
  1885. u8 *addr)
  1886. {
  1887. struct brcmf_cfg80211_vif *p2p_vif;
  1888. struct brcmf_if *p2p_ifp;
  1889. struct brcmf_if *pri_ifp;
  1890. int err;
  1891. u32 bssidx;
  1892. if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
  1893. return ERR_PTR(-ENOSPC);
  1894. p2p_vif = brcmf_alloc_vif(p2p->cfg, NL80211_IFTYPE_P2P_DEVICE,
  1895. false);
  1896. if (IS_ERR(p2p_vif)) {
  1897. brcmf_err("could not create discovery vif\n");
  1898. return (struct wireless_dev *)p2p_vif;
  1899. }
  1900. pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
  1901. brcmf_p2p_generate_bss_mac(p2p, addr);
  1902. brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
  1903. brcmf_cfg80211_arm_vif_event(p2p->cfg, p2p_vif);
  1904. /* Initialize P2P Discovery in the firmware */
  1905. err = brcmf_fil_iovar_int_set(pri_ifp, "p2p_disc", 1);
  1906. if (err < 0) {
  1907. brcmf_err("set p2p_disc error\n");
  1908. brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
  1909. goto fail;
  1910. }
  1911. /* wait for firmware event */
  1912. err = brcmf_cfg80211_wait_vif_event_timeout(p2p->cfg, BRCMF_E_IF_ADD,
  1913. msecs_to_jiffies(1500));
  1914. brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
  1915. if (!err) {
  1916. brcmf_err("timeout occurred\n");
  1917. err = -EIO;
  1918. goto fail;
  1919. }
  1920. /* discovery interface created */
  1921. p2p_ifp = p2p_vif->ifp;
  1922. p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
  1923. memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
  1924. memcpy(&p2p_vif->wdev.address, p2p->dev_addr, sizeof(p2p->dev_addr));
  1925. /* verify bsscfg index for P2P discovery */
  1926. err = brcmf_fil_iovar_int_get(pri_ifp, "p2p_dev", &bssidx);
  1927. if (err < 0) {
  1928. brcmf_err("retrieving discover bsscfg index failed\n");
  1929. goto fail;
  1930. }
  1931. WARN_ON(p2p_ifp->bssidx != bssidx);
  1932. init_completion(&p2p->send_af_done);
  1933. INIT_WORK(&p2p->afx_hdl.afx_work, brcmf_p2p_afx_handler);
  1934. init_completion(&p2p->afx_hdl.act_frm_scan);
  1935. init_completion(&p2p->wait_next_af);
  1936. return &p2p_vif->wdev;
  1937. fail:
  1938. brcmf_free_vif(p2p_vif);
  1939. return ERR_PTR(err);
  1940. }
  1941. /**
  1942. * brcmf_p2p_delete_p2pdev() - delete P2P_DEVICE virtual interface.
  1943. *
  1944. * @vif: virtual interface object to delete.
  1945. */
  1946. static void brcmf_p2p_delete_p2pdev(struct brcmf_cfg80211_vif *vif)
  1947. {
  1948. struct brcmf_p2p_info *p2p = &vif->ifp->drvr->config->p2p;
  1949. cfg80211_unregister_wdev(&vif->wdev);
  1950. p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = NULL;
  1951. brcmf_free_vif(vif);
  1952. }
  1953. /**
  1954. * brcmf_p2p_add_vif() - create a new P2P virtual interface.
  1955. *
  1956. * @wiphy: wiphy device of new interface.
  1957. * @name: name of the new interface.
  1958. * @type: nl80211 interface type.
  1959. * @flags: not used.
  1960. * @params: contains mac address for P2P device.
  1961. */
  1962. struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
  1963. enum nl80211_iftype type, u32 *flags,
  1964. struct vif_params *params)
  1965. {
  1966. struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
  1967. struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
  1968. struct brcmf_cfg80211_vif *vif;
  1969. enum brcmf_fil_p2p_if_types iftype;
  1970. enum wl_mode mode;
  1971. int err;
  1972. if (brcmf_cfg80211_vif_event_armed(cfg))
  1973. return ERR_PTR(-EBUSY);
  1974. brcmf_dbg(INFO, "adding vif \"%s\" (type=%d)\n", name, type);
  1975. switch (type) {
  1976. case NL80211_IFTYPE_P2P_CLIENT:
  1977. iftype = BRCMF_FIL_P2P_IF_CLIENT;
  1978. mode = WL_MODE_BSS;
  1979. break;
  1980. case NL80211_IFTYPE_P2P_GO:
  1981. iftype = BRCMF_FIL_P2P_IF_GO;
  1982. mode = WL_MODE_AP;
  1983. break;
  1984. case NL80211_IFTYPE_P2P_DEVICE:
  1985. return brcmf_p2p_create_p2pdev(&cfg->p2p, wiphy,
  1986. params->macaddr);
  1987. default:
  1988. return ERR_PTR(-EOPNOTSUPP);
  1989. }
  1990. vif = brcmf_alloc_vif(cfg, type, false);
  1991. if (IS_ERR(vif))
  1992. return (struct wireless_dev *)vif;
  1993. brcmf_cfg80211_arm_vif_event(cfg, vif);
  1994. err = brcmf_p2p_request_p2p_if(&cfg->p2p, ifp, cfg->p2p.int_addr,
  1995. iftype);
  1996. if (err) {
  1997. brcmf_cfg80211_arm_vif_event(cfg, NULL);
  1998. goto fail;
  1999. }
  2000. /* wait for firmware event */
  2001. err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_ADD,
  2002. msecs_to_jiffies(1500));
  2003. brcmf_cfg80211_arm_vif_event(cfg, NULL);
  2004. if (!err) {
  2005. brcmf_err("timeout occurred\n");
  2006. err = -EIO;
  2007. goto fail;
  2008. }
  2009. /* interface created in firmware */
  2010. ifp = vif->ifp;
  2011. if (!ifp) {
  2012. brcmf_err("no if pointer provided\n");
  2013. err = -ENOENT;
  2014. goto fail;
  2015. }
  2016. strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
  2017. err = brcmf_net_attach(ifp, true);
  2018. if (err) {
  2019. brcmf_err("Registering netdevice failed\n");
  2020. goto fail;
  2021. }
  2022. cfg->p2p.bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = vif;
  2023. /* Disable firmware roaming for P2P interface */
  2024. brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
  2025. if (iftype == BRCMF_FIL_P2P_IF_GO) {
  2026. /* set station timeout for p2p */
  2027. brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCB_TIMEOUT,
  2028. BRCMF_SCB_TIMEOUT_VALUE);
  2029. }
  2030. return &ifp->vif->wdev;
  2031. fail:
  2032. brcmf_free_vif(vif);
  2033. return ERR_PTR(err);
  2034. }
  2035. /**
  2036. * brcmf_p2p_del_vif() - delete a P2P virtual interface.
  2037. *
  2038. * @wiphy: wiphy device of interface.
  2039. * @wdev: wireless device of interface.
  2040. *
  2041. * TODO: not yet supported.
  2042. */
  2043. int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
  2044. {
  2045. struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
  2046. struct brcmf_p2p_info *p2p = &cfg->p2p;
  2047. struct brcmf_cfg80211_vif *vif;
  2048. unsigned long jiffie_timeout = msecs_to_jiffies(1500);
  2049. bool wait_for_disable = false;
  2050. int err;
  2051. brcmf_dbg(TRACE, "delete P2P vif\n");
  2052. vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
  2053. switch (vif->wdev.iftype) {
  2054. case NL80211_IFTYPE_P2P_CLIENT:
  2055. if (test_bit(BRCMF_VIF_STATUS_DISCONNECTING, &vif->sme_state))
  2056. wait_for_disable = true;
  2057. break;
  2058. case NL80211_IFTYPE_P2P_GO:
  2059. if (!brcmf_p2p_disable_p2p_if(vif))
  2060. wait_for_disable = true;
  2061. break;
  2062. case NL80211_IFTYPE_P2P_DEVICE:
  2063. brcmf_p2p_delete_p2pdev(vif);
  2064. return 0;
  2065. default:
  2066. return -ENOTSUPP;
  2067. break;
  2068. }
  2069. clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
  2070. brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
  2071. if (wait_for_disable)
  2072. wait_for_completion_timeout(&cfg->vif_disabled,
  2073. msecs_to_jiffies(500));
  2074. brcmf_vif_clear_mgmt_ies(vif);
  2075. brcmf_cfg80211_arm_vif_event(cfg, vif);
  2076. err = brcmf_p2p_release_p2p_if(vif);
  2077. if (!err) {
  2078. /* wait for firmware event */
  2079. err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_DEL,
  2080. jiffie_timeout);
  2081. if (!err)
  2082. err = -EIO;
  2083. else
  2084. err = 0;
  2085. }
  2086. brcmf_cfg80211_arm_vif_event(cfg, NULL);
  2087. brcmf_free_vif(vif);
  2088. p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = NULL;
  2089. return err;
  2090. }
  2091. int brcmf_p2p_start_device(struct wiphy *wiphy, struct wireless_dev *wdev)
  2092. {
  2093. struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
  2094. struct brcmf_p2p_info *p2p = &cfg->p2p;
  2095. struct brcmf_cfg80211_vif *vif;
  2096. int err;
  2097. vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
  2098. mutex_lock(&cfg->usr_sync);
  2099. err = brcmf_p2p_enable_discovery(p2p);
  2100. if (!err)
  2101. set_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
  2102. mutex_unlock(&cfg->usr_sync);
  2103. return err;
  2104. }
  2105. void brcmf_p2p_stop_device(struct wiphy *wiphy, struct wireless_dev *wdev)
  2106. {
  2107. struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
  2108. struct brcmf_p2p_info *p2p = &cfg->p2p;
  2109. struct brcmf_cfg80211_vif *vif;
  2110. vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
  2111. mutex_lock(&cfg->usr_sync);
  2112. (void)brcmf_p2p_deinit_discovery(p2p);
  2113. brcmf_abort_scanning(cfg);
  2114. clear_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
  2115. mutex_unlock(&cfg->usr_sync);
  2116. }