p2p.c 69 KB

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