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