p2p.c 69 KB

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