join.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /**
  2. * Functions implementing wlan infrastructure and adhoc join routines,
  3. * IOCTL handlers as well as command preperation and response routines
  4. * for sending adhoc start, adhoc join, and association commands
  5. * to the firmware.
  6. */
  7. #include <linux/netdevice.h>
  8. #include <linux/if_arp.h>
  9. #include <linux/wireless.h>
  10. #include <linux/etherdevice.h>
  11. #include <net/iw_handler.h>
  12. #include "host.h"
  13. #include "decl.h"
  14. #include "join.h"
  15. #include "dev.h"
  16. #include "assoc.h"
  17. /* Supported rates for ad-hoc B mode */
  18. static u8 adhoc_rates_b[5] = { 0x02, 0x04, 0x0b, 0x16, 0x00 };
  19. /**
  20. * @brief This function finds common rates between rate1 and card rates.
  21. *
  22. * It will fill common rates in rate1 as output if found.
  23. *
  24. * NOTE: Setting the MSB of the basic rates need to be taken
  25. * care, either before or after calling this function
  26. *
  27. * @param adapter A pointer to wlan_adapter structure
  28. * @param rate1 the buffer which keeps input and output
  29. * @param rate1_size the size of rate1 buffer; new size of buffer on return
  30. *
  31. * @return 0 or -1
  32. */
  33. static int get_common_rates(wlan_adapter * adapter, u8 * rates, u16 *rates_size)
  34. {
  35. u8 *card_rates = libertas_bg_rates;
  36. size_t num_card_rates = sizeof(libertas_bg_rates);
  37. int ret = 0, i, j;
  38. u8 tmp[30];
  39. size_t tmp_size = 0;
  40. /* For each rate in card_rates that exists in rate1, copy to tmp */
  41. for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
  42. for (j = 0; rates[j] && (j < *rates_size); j++) {
  43. if (rates[j] == card_rates[i])
  44. tmp[tmp_size++] = card_rates[i];
  45. }
  46. }
  47. lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
  48. lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
  49. lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
  50. lbs_deb_join("Tx datarate is currently 0x%X\n", adapter->cur_rate);
  51. if (!adapter->auto_rate) {
  52. for (i = 0; i < tmp_size; i++) {
  53. if (tmp[i] == adapter->cur_rate)
  54. goto done;
  55. }
  56. lbs_pr_alert("Previously set fixed data rate %#x isn't "
  57. "compatible with the network.\n", adapter->cur_rate);
  58. ret = -1;
  59. goto done;
  60. }
  61. ret = 0;
  62. done:
  63. memset(rates, 0, *rates_size);
  64. *rates_size = min_t(int, tmp_size, *rates_size);
  65. memcpy(rates, tmp, *rates_size);
  66. return ret;
  67. }
  68. /**
  69. * @brief Sets the MSB on basic rates as the firmware requires
  70. *
  71. * Scan through an array and set the MSB for basic data rates.
  72. *
  73. * @param rates buffer of data rates
  74. * @param len size of buffer
  75. */
  76. static void libertas_set_basic_rate_flags(u8 * rates, size_t len)
  77. {
  78. int i;
  79. for (i = 0; i < len; i++) {
  80. if (rates[i] == 0x02 || rates[i] == 0x04 ||
  81. rates[i] == 0x0b || rates[i] == 0x16)
  82. rates[i] |= 0x80;
  83. }
  84. }
  85. /**
  86. * @brief Unsets the MSB on basic rates
  87. *
  88. * Scan through an array and unset the MSB for basic data rates.
  89. *
  90. * @param rates buffer of data rates
  91. * @param len size of buffer
  92. */
  93. void libertas_unset_basic_rate_flags(u8 * rates, size_t len)
  94. {
  95. int i;
  96. for (i = 0; i < len; i++)
  97. rates[i] &= 0x7f;
  98. }
  99. /**
  100. * @brief Associate to a specific BSS discovered in a scan
  101. *
  102. * @param priv A pointer to wlan_private structure
  103. * @param pbssdesc Pointer to the BSS descriptor to associate with.
  104. *
  105. * @return 0-success, otherwise fail
  106. */
  107. int wlan_associate(wlan_private * priv, struct assoc_request * assoc_req)
  108. {
  109. wlan_adapter *adapter = priv->adapter;
  110. int ret;
  111. lbs_deb_enter(LBS_DEB_JOIN);
  112. ret = libertas_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
  113. 0, CMD_OPTION_WAITFORRSP,
  114. 0, assoc_req->bss.bssid);
  115. if (ret)
  116. goto done;
  117. /* set preamble to firmware */
  118. if ( (adapter->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  119. && (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
  120. adapter->preamble = CMD_TYPE_SHORT_PREAMBLE;
  121. else
  122. adapter->preamble = CMD_TYPE_LONG_PREAMBLE;
  123. libertas_set_radio_control(priv);
  124. ret = libertas_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
  125. 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
  126. done:
  127. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  128. return ret;
  129. }
  130. /**
  131. * @brief Start an Adhoc Network
  132. *
  133. * @param priv A pointer to wlan_private structure
  134. * @param adhocssid The ssid of the Adhoc Network
  135. * @return 0--success, -1--fail
  136. */
  137. int libertas_start_adhoc_network(wlan_private * priv, struct assoc_request * assoc_req)
  138. {
  139. wlan_adapter *adapter = priv->adapter;
  140. int ret = 0;
  141. adapter->adhoccreate = 1;
  142. if (adapter->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
  143. lbs_deb_join("AdhocStart: Short preamble\n");
  144. adapter->preamble = CMD_TYPE_SHORT_PREAMBLE;
  145. } else {
  146. lbs_deb_join("AdhocStart: Long preamble\n");
  147. adapter->preamble = CMD_TYPE_LONG_PREAMBLE;
  148. }
  149. libertas_set_radio_control(priv);
  150. lbs_deb_join("AdhocStart: channel = %d\n", assoc_req->channel);
  151. lbs_deb_join("AdhocStart: band = %d\n", assoc_req->band);
  152. ret = libertas_prepare_and_send_command(priv, CMD_802_11_AD_HOC_START,
  153. 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
  154. return ret;
  155. }
  156. /**
  157. * @brief Join an adhoc network found in a previous scan
  158. *
  159. * @param priv A pointer to wlan_private structure
  160. * @param pbssdesc Pointer to a BSS descriptor found in a previous scan
  161. * to attempt to join
  162. *
  163. * @return 0--success, -1--fail
  164. */
  165. int libertas_join_adhoc_network(wlan_private * priv, struct assoc_request * assoc_req)
  166. {
  167. wlan_adapter *adapter = priv->adapter;
  168. struct bss_descriptor * bss = &assoc_req->bss;
  169. int ret = 0;
  170. lbs_deb_join("%s: Current SSID '%s', ssid length %u\n",
  171. __func__,
  172. escape_essid(adapter->curbssparams.ssid,
  173. adapter->curbssparams.ssid_len),
  174. adapter->curbssparams.ssid_len);
  175. lbs_deb_join("%s: requested ssid '%s', ssid length %u\n",
  176. __func__, escape_essid(bss->ssid, bss->ssid_len),
  177. bss->ssid_len);
  178. /* check if the requested SSID is already joined */
  179. if (adapter->curbssparams.ssid_len
  180. && !libertas_ssid_cmp(adapter->curbssparams.ssid,
  181. adapter->curbssparams.ssid_len,
  182. bss->ssid, bss->ssid_len)
  183. && (adapter->mode == IW_MODE_ADHOC)) {
  184. lbs_deb_join(
  185. "ADHOC_J_CMD: New ad-hoc SSID is the same as current, "
  186. "not attempting to re-join");
  187. return -1;
  188. }
  189. /* Use shortpreamble only when both creator and card supports
  190. short preamble */
  191. if ( !(bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  192. || !(adapter->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
  193. lbs_deb_join("AdhocJoin: Long preamble\n");
  194. adapter->preamble = CMD_TYPE_LONG_PREAMBLE;
  195. } else {
  196. lbs_deb_join("AdhocJoin: Short preamble\n");
  197. adapter->preamble = CMD_TYPE_SHORT_PREAMBLE;
  198. }
  199. libertas_set_radio_control(priv);
  200. lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
  201. lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
  202. adapter->adhoccreate = 0;
  203. ret = libertas_prepare_and_send_command(priv, CMD_802_11_AD_HOC_JOIN,
  204. 0, CMD_OPTION_WAITFORRSP,
  205. OID_802_11_SSID, assoc_req);
  206. return ret;
  207. }
  208. int libertas_stop_adhoc_network(wlan_private * priv)
  209. {
  210. return libertas_prepare_and_send_command(priv, CMD_802_11_AD_HOC_STOP,
  211. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  212. }
  213. /**
  214. * @brief Send Deauthentication Request
  215. *
  216. * @param priv A pointer to wlan_private structure
  217. * @return 0--success, -1--fail
  218. */
  219. int libertas_send_deauthentication(wlan_private * priv)
  220. {
  221. return libertas_prepare_and_send_command(priv, CMD_802_11_DEAUTHENTICATE,
  222. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  223. }
  224. /**
  225. * @brief This function prepares command of authenticate.
  226. *
  227. * @param priv A pointer to wlan_private structure
  228. * @param cmd A pointer to cmd_ds_command structure
  229. * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
  230. *
  231. * @return 0 or -1
  232. */
  233. int libertas_cmd_80211_authenticate(wlan_private * priv,
  234. struct cmd_ds_command *cmd,
  235. void *pdata_buf)
  236. {
  237. wlan_adapter *adapter = priv->adapter;
  238. struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
  239. int ret = -1;
  240. u8 *bssid = pdata_buf;
  241. lbs_deb_enter(LBS_DEB_JOIN);
  242. cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
  243. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
  244. + S_DS_GEN);
  245. /* translate auth mode to 802.11 defined wire value */
  246. switch (adapter->secinfo.auth_mode) {
  247. case IW_AUTH_ALG_OPEN_SYSTEM:
  248. pauthenticate->authtype = 0x00;
  249. break;
  250. case IW_AUTH_ALG_SHARED_KEY:
  251. pauthenticate->authtype = 0x01;
  252. break;
  253. case IW_AUTH_ALG_LEAP:
  254. pauthenticate->authtype = 0x80;
  255. break;
  256. default:
  257. lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
  258. adapter->secinfo.auth_mode);
  259. goto out;
  260. }
  261. memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
  262. lbs_deb_join("AUTH_CMD: BSSID is : " MAC_FMT " auth=0x%X\n",
  263. MAC_ARG(bssid), pauthenticate->authtype);
  264. ret = 0;
  265. out:
  266. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  267. return ret;
  268. }
  269. int libertas_cmd_80211_deauthenticate(wlan_private * priv,
  270. struct cmd_ds_command *cmd)
  271. {
  272. wlan_adapter *adapter = priv->adapter;
  273. struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
  274. lbs_deb_enter(LBS_DEB_JOIN);
  275. cmd->command = cpu_to_le16(CMD_802_11_DEAUTHENTICATE);
  276. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
  277. S_DS_GEN);
  278. /* set AP MAC address */
  279. memmove(dauth->macaddr, adapter->curbssparams.bssid, ETH_ALEN);
  280. /* Reason code 3 = Station is leaving */
  281. #define REASON_CODE_STA_LEAVING 3
  282. dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
  283. lbs_deb_leave(LBS_DEB_JOIN);
  284. return 0;
  285. }
  286. int libertas_cmd_80211_associate(wlan_private * priv,
  287. struct cmd_ds_command *cmd, void *pdata_buf)
  288. {
  289. wlan_adapter *adapter = priv->adapter;
  290. struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
  291. int ret = 0;
  292. struct assoc_request * assoc_req = pdata_buf;
  293. struct bss_descriptor * bss = &assoc_req->bss;
  294. u8 *pos;
  295. u16 tmpcap, tmplen;
  296. struct mrvlietypes_ssidparamset *ssid;
  297. struct mrvlietypes_phyparamset *phy;
  298. struct mrvlietypes_ssparamset *ss;
  299. struct mrvlietypes_ratesparamset *rates;
  300. struct mrvlietypes_rsnparamset *rsn;
  301. lbs_deb_enter(LBS_DEB_JOIN);
  302. pos = (u8 *) passo;
  303. if (!adapter) {
  304. ret = -1;
  305. goto done;
  306. }
  307. cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
  308. memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
  309. pos += sizeof(passo->peerstaaddr);
  310. /* set the listen interval */
  311. passo->listeninterval = cpu_to_le16(adapter->listeninterval);
  312. pos += sizeof(passo->capability);
  313. pos += sizeof(passo->listeninterval);
  314. pos += sizeof(passo->bcnperiod);
  315. pos += sizeof(passo->dtimperiod);
  316. ssid = (struct mrvlietypes_ssidparamset *) pos;
  317. ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
  318. tmplen = bss->ssid_len;
  319. ssid->header.len = cpu_to_le16(tmplen);
  320. memcpy(ssid->ssid, bss->ssid, tmplen);
  321. pos += sizeof(ssid->header) + tmplen;
  322. phy = (struct mrvlietypes_phyparamset *) pos;
  323. phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  324. tmplen = sizeof(phy->fh_ds.dsparamset);
  325. phy->header.len = cpu_to_le16(tmplen);
  326. memcpy(&phy->fh_ds.dsparamset,
  327. &bss->phyparamset.dsparamset.currentchan,
  328. tmplen);
  329. pos += sizeof(phy->header) + tmplen;
  330. ss = (struct mrvlietypes_ssparamset *) pos;
  331. ss->header.type = cpu_to_le16(TLV_TYPE_CF);
  332. tmplen = sizeof(ss->cf_ibss.cfparamset);
  333. ss->header.len = cpu_to_le16(tmplen);
  334. pos += sizeof(ss->header) + tmplen;
  335. rates = (struct mrvlietypes_ratesparamset *) pos;
  336. rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
  337. memcpy(&rates->rates, &bss->rates, MAX_RATES);
  338. tmplen = MAX_RATES;
  339. if (get_common_rates(adapter, rates->rates, &tmplen)) {
  340. ret = -1;
  341. goto done;
  342. }
  343. pos += sizeof(rates->header) + tmplen;
  344. rates->header.len = cpu_to_le16(tmplen);
  345. lbs_deb_join("ASSOC_CMD: num rates = %u\n", tmplen);
  346. /* Copy the infra. association rates into Current BSS state structure */
  347. memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
  348. memcpy(&adapter->curbssparams.rates, &rates->rates, tmplen);
  349. /* Set MSB on basic rates as the firmware requires, but _after_
  350. * copying to current bss rates.
  351. */
  352. libertas_set_basic_rate_flags(rates->rates, tmplen);
  353. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  354. rsn = (struct mrvlietypes_rsnparamset *) pos;
  355. /* WPA_IE or WPA2_IE */
  356. rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
  357. tmplen = (u16) assoc_req->wpa_ie[1];
  358. rsn->header.len = cpu_to_le16(tmplen);
  359. memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
  360. lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
  361. sizeof(rsn->header) + tmplen);
  362. pos += sizeof(rsn->header) + tmplen;
  363. }
  364. /* update curbssparams */
  365. adapter->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
  366. if (libertas_parse_dnld_countryinfo_11d(priv, bss)) {
  367. ret = -1;
  368. goto done;
  369. }
  370. cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
  371. /* set the capability info */
  372. tmpcap = (bss->capability & CAPINFO_MASK);
  373. if (bss->mode == IW_MODE_INFRA)
  374. tmpcap |= WLAN_CAPABILITY_ESS;
  375. passo->capability = cpu_to_le16(tmpcap);
  376. lbs_deb_join("ASSOC_CMD: capability=%4X CAPINFO_MASK=%4X\n",
  377. tmpcap, CAPINFO_MASK);
  378. done:
  379. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  380. return ret;
  381. }
  382. int libertas_cmd_80211_ad_hoc_start(wlan_private * priv,
  383. struct cmd_ds_command *cmd, void *pdata_buf)
  384. {
  385. wlan_adapter *adapter = priv->adapter;
  386. struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
  387. int ret = 0;
  388. int cmdappendsize = 0;
  389. struct assoc_request * assoc_req = pdata_buf;
  390. u16 tmpcap = 0;
  391. size_t ratesize = 0;
  392. lbs_deb_enter(LBS_DEB_JOIN);
  393. if (!adapter) {
  394. ret = -1;
  395. goto done;
  396. }
  397. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
  398. /*
  399. * Fill in the parameters for 2 data structures:
  400. * 1. cmd_ds_802_11_ad_hoc_start command
  401. * 2. adapter->scantable[i]
  402. *
  403. * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
  404. * probe delay, and cap info.
  405. *
  406. * Firmware will fill up beacon period, DTIM, Basic rates
  407. * and operational rates.
  408. */
  409. memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
  410. memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
  411. lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
  412. escape_essid(assoc_req->ssid, assoc_req->ssid_len),
  413. assoc_req->ssid_len);
  414. /* set the BSS type */
  415. adhs->bsstype = CMD_BSS_TYPE_IBSS;
  416. adapter->mode = IW_MODE_ADHOC;
  417. adhs->beaconperiod = cpu_to_le16(adapter->beaconperiod);
  418. /* set Physical param set */
  419. #define DS_PARA_IE_ID 3
  420. #define DS_PARA_IE_LEN 1
  421. adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
  422. adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
  423. WARN_ON(!assoc_req->channel);
  424. lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
  425. assoc_req->channel);
  426. adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
  427. /* set IBSS param set */
  428. #define IBSS_PARA_IE_ID 6
  429. #define IBSS_PARA_IE_LEN 2
  430. adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
  431. adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
  432. adhs->ssparamset.ibssparamset.atimwindow = 0;
  433. /* set capability info */
  434. tmpcap = WLAN_CAPABILITY_IBSS;
  435. if (assoc_req->secinfo.wep_enabled) {
  436. lbs_deb_join("ADHOC_S_CMD: WEP enabled, setting privacy on\n");
  437. tmpcap |= WLAN_CAPABILITY_PRIVACY;
  438. } else {
  439. lbs_deb_join("ADHOC_S_CMD: WEP disabled, setting privacy off\n");
  440. }
  441. adhs->capability = cpu_to_le16(tmpcap);
  442. /* probedelay */
  443. adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  444. memset(adhs->rates, 0, sizeof(adhs->rates));
  445. if (adapter->adhoc_grate_enabled) {
  446. ratesize = min(sizeof(adhs->rates), sizeof(libertas_bg_rates));
  447. memcpy(adhs->rates, libertas_bg_rates, ratesize);
  448. } else {
  449. ratesize = min(sizeof(adhs->rates), sizeof(adhoc_rates_b));
  450. memcpy(adhs->rates, adhoc_rates_b, ratesize);
  451. }
  452. /* Copy the ad-hoc creating rates into Current BSS state structure */
  453. memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
  454. memcpy(&adapter->curbssparams.rates, &adhs->rates, ratesize);
  455. /* Set MSB on basic rates as the firmware requires, but _after_
  456. * copying to current bss rates.
  457. */
  458. libertas_set_basic_rate_flags(adhs->rates, ratesize);
  459. lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
  460. adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
  461. lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
  462. if (libertas_create_dnld_countryinfo_11d(priv)) {
  463. lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
  464. ret = -1;
  465. goto done;
  466. }
  467. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
  468. S_DS_GEN + cmdappendsize);
  469. ret = 0;
  470. done:
  471. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  472. return ret;
  473. }
  474. int libertas_cmd_80211_ad_hoc_stop(wlan_private * priv,
  475. struct cmd_ds_command *cmd)
  476. {
  477. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
  478. cmd->size = cpu_to_le16(S_DS_GEN);
  479. return 0;
  480. }
  481. int libertas_cmd_80211_ad_hoc_join(wlan_private * priv,
  482. struct cmd_ds_command *cmd, void *pdata_buf)
  483. {
  484. wlan_adapter *adapter = priv->adapter;
  485. struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
  486. struct assoc_request * assoc_req = pdata_buf;
  487. struct bss_descriptor *bss = &assoc_req->bss;
  488. int cmdappendsize = 0;
  489. int ret = 0;
  490. u16 ratesize = 0;
  491. lbs_deb_enter(LBS_DEB_JOIN);
  492. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
  493. join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
  494. join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
  495. memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
  496. memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
  497. memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
  498. sizeof(union ieeetypes_phyparamset));
  499. memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
  500. sizeof(union IEEEtypes_ssparamset));
  501. join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
  502. lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
  503. bss->capability, CAPINFO_MASK);
  504. /* information on BSSID descriptor passed to FW */
  505. lbs_deb_join(
  506. "ADHOC_J_CMD: BSSID = " MAC_FMT ", SSID = '%s'\n",
  507. MAC_ARG(join_cmd->bss.bssid), join_cmd->bss.ssid);
  508. /* failtimeout */
  509. join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  510. /* probedelay */
  511. join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  512. adapter->curbssparams.channel = bss->channel;
  513. /* Copy Data rates from the rates recorded in scan response */
  514. memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
  515. ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
  516. memcpy(join_cmd->bss.rates, bss->rates, ratesize);
  517. if (get_common_rates(adapter, join_cmd->bss.rates, &ratesize)) {
  518. lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
  519. ret = -1;
  520. goto done;
  521. }
  522. /* Copy the ad-hoc creating rates into Current BSS state structure */
  523. memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
  524. memcpy(&adapter->curbssparams.rates, join_cmd->bss.rates, ratesize);
  525. /* Set MSB on basic rates as the firmware requires, but _after_
  526. * copying to current bss rates.
  527. */
  528. libertas_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
  529. join_cmd->bss.ssparamset.ibssparamset.atimwindow =
  530. cpu_to_le16(bss->atimwindow);
  531. if (assoc_req->secinfo.wep_enabled) {
  532. u16 tmp = le16_to_cpu(join_cmd->bss.capability);
  533. tmp |= WLAN_CAPABILITY_PRIVACY;
  534. join_cmd->bss.capability = cpu_to_le16(tmp);
  535. }
  536. if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
  537. /* wake up first */
  538. __le32 Localpsmode;
  539. Localpsmode = cpu_to_le32(WLAN802_11POWERMODECAM);
  540. ret = libertas_prepare_and_send_command(priv,
  541. CMD_802_11_PS_MODE,
  542. CMD_ACT_SET,
  543. 0, 0, &Localpsmode);
  544. if (ret) {
  545. ret = -1;
  546. goto done;
  547. }
  548. }
  549. if (libertas_parse_dnld_countryinfo_11d(priv, bss)) {
  550. ret = -1;
  551. goto done;
  552. }
  553. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
  554. S_DS_GEN + cmdappendsize);
  555. done:
  556. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  557. return ret;
  558. }
  559. int libertas_ret_80211_associate(wlan_private * priv,
  560. struct cmd_ds_command *resp)
  561. {
  562. wlan_adapter *adapter = priv->adapter;
  563. int ret = 0;
  564. union iwreq_data wrqu;
  565. struct ieeetypes_assocrsp *passocrsp;
  566. struct bss_descriptor * bss;
  567. lbs_deb_enter(LBS_DEB_JOIN);
  568. if (!adapter->in_progress_assoc_req) {
  569. lbs_deb_join("ASSOC_RESP: no in-progress association request\n");
  570. ret = -1;
  571. goto done;
  572. }
  573. bss = &adapter->in_progress_assoc_req->bss;
  574. passocrsp = (struct ieeetypes_assocrsp *) & resp->params;
  575. if (le16_to_cpu(passocrsp->statuscode)) {
  576. libertas_mac_event_disconnected(priv);
  577. lbs_deb_join("ASSOC_RESP: Association failed, status code = %d\n",
  578. le16_to_cpu(passocrsp->statuscode));
  579. ret = -1;
  580. goto done;
  581. }
  582. lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_RESP", (void *)&resp->params,
  583. le16_to_cpu(resp->size) - S_DS_GEN);
  584. /* Send a Media Connected event, according to the Spec */
  585. adapter->connect_status = LIBERTAS_CONNECTED;
  586. lbs_deb_join("ASSOC_RESP: assocated to '%s'\n",
  587. escape_essid(bss->ssid, bss->ssid_len));
  588. /* Update current SSID and BSSID */
  589. memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  590. adapter->curbssparams.ssid_len = bss->ssid_len;
  591. memcpy(adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
  592. lbs_deb_join("ASSOC_RESP: currentpacketfilter is %x\n",
  593. adapter->currentpacketfilter);
  594. adapter->SNR[TYPE_RXPD][TYPE_AVG] = 0;
  595. adapter->NF[TYPE_RXPD][TYPE_AVG] = 0;
  596. memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
  597. memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
  598. adapter->nextSNRNF = 0;
  599. adapter->numSNRNF = 0;
  600. netif_carrier_on(priv->dev);
  601. netif_wake_queue(priv->dev);
  602. if (priv->mesh_dev) {
  603. netif_carrier_on(priv->mesh_dev);
  604. netif_wake_queue(priv->mesh_dev);
  605. }
  606. lbs_deb_join("ASSOC_RESP: Associated \n");
  607. memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
  608. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  609. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  610. done:
  611. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  612. return ret;
  613. }
  614. int libertas_ret_80211_disassociate(wlan_private * priv,
  615. struct cmd_ds_command *resp)
  616. {
  617. lbs_deb_enter(LBS_DEB_JOIN);
  618. libertas_mac_event_disconnected(priv);
  619. lbs_deb_leave(LBS_DEB_JOIN);
  620. return 0;
  621. }
  622. int libertas_ret_80211_ad_hoc_start(wlan_private * priv,
  623. struct cmd_ds_command *resp)
  624. {
  625. wlan_adapter *adapter = priv->adapter;
  626. int ret = 0;
  627. u16 command = le16_to_cpu(resp->command);
  628. u16 result = le16_to_cpu(resp->result);
  629. struct cmd_ds_802_11_ad_hoc_result *padhocresult;
  630. union iwreq_data wrqu;
  631. struct bss_descriptor *bss;
  632. lbs_deb_enter(LBS_DEB_JOIN);
  633. padhocresult = &resp->params.result;
  634. lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
  635. lbs_deb_join("ADHOC_RESP: command = %x\n", command);
  636. lbs_deb_join("ADHOC_RESP: result = %x\n", result);
  637. if (!adapter->in_progress_assoc_req) {
  638. lbs_deb_join("ADHOC_RESP: no in-progress association request\n");
  639. ret = -1;
  640. goto done;
  641. }
  642. bss = &adapter->in_progress_assoc_req->bss;
  643. /*
  644. * Join result code 0 --> SUCCESS
  645. */
  646. if (result) {
  647. lbs_deb_join("ADHOC_RESP: failed\n");
  648. if (adapter->connect_status == LIBERTAS_CONNECTED) {
  649. libertas_mac_event_disconnected(priv);
  650. }
  651. ret = -1;
  652. goto done;
  653. }
  654. /*
  655. * Now the join cmd should be successful
  656. * If BSSID has changed use SSID to compare instead of BSSID
  657. */
  658. lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
  659. escape_essid(bss->ssid, bss->ssid_len));
  660. /* Send a Media Connected event, according to the Spec */
  661. adapter->connect_status = LIBERTAS_CONNECTED;
  662. if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
  663. /* Update the created network descriptor with the new BSSID */
  664. memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
  665. }
  666. /* Set the BSSID from the joined/started descriptor */
  667. memcpy(&adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
  668. /* Set the new SSID to current SSID */
  669. memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  670. adapter->curbssparams.ssid_len = bss->ssid_len;
  671. netif_carrier_on(priv->dev);
  672. netif_wake_queue(priv->dev);
  673. if (priv->mesh_dev) {
  674. netif_carrier_on(priv->mesh_dev);
  675. netif_wake_queue(priv->mesh_dev);
  676. }
  677. memset(&wrqu, 0, sizeof(wrqu));
  678. memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
  679. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  680. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  681. lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
  682. lbs_deb_join("ADHOC_RESP: channel = %d\n", adapter->curbssparams.channel);
  683. lbs_deb_join("ADHOC_RESP: BSSID = " MAC_FMT "\n",
  684. MAC_ARG(padhocresult->bssid));
  685. done:
  686. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  687. return ret;
  688. }
  689. int libertas_ret_80211_ad_hoc_stop(wlan_private * priv,
  690. struct cmd_ds_command *resp)
  691. {
  692. lbs_deb_enter(LBS_DEB_JOIN);
  693. libertas_mac_event_disconnected(priv);
  694. lbs_deb_leave(LBS_DEB_JOIN);
  695. return 0;
  696. }