join.c 25 KB

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