p2p.c 69 KB

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