p2p.c 69 KB

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