p2p.c 69 KB

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