p2p.c 65 KB

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