p2p.c 69 KB

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