p2p.c 70 KB

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