drm/kms: teardown crtc correctly when fb is destroyed.
[pandora-kernel.git] / drivers / net / wireless / libertas / assoc.c
1 /* Copyright (C) 2006, Red Hat, Inc. */
2
3 #include <linux/types.h>
4 #include <linux/kernel.h>
5 #include <linux/etherdevice.h>
6 #include <linux/ieee80211.h>
7 #include <linux/if_arp.h>
8 #include <net/lib80211.h>
9
10 #include "assoc.h"
11 #include "decl.h"
12 #include "host.h"
13 #include "scan.h"
14 #include "cmd.h"
15
16 static const u8 bssid_any[ETH_ALEN]  __attribute__ ((aligned (2))) =
17         { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
18 static const u8 bssid_off[ETH_ALEN]  __attribute__ ((aligned (2))) =
19         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
20
21 /* The firmware needs the following bits masked out of the beacon-derived
22  * capability field when associating/joining to a BSS:
23  *  9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
24  */
25 #define CAPINFO_MASK    (~(0xda00))
26
27
28 /**
29  *  @brief This function finds common rates between rates and card rates.
30  *
31  * It will fill common rates in rates as output if found.
32  *
33  * NOTE: Setting the MSB of the basic rates need to be taken
34  *   care, either before or after calling this function
35  *
36  *  @param priv     A pointer to struct lbs_private structure
37  *  @param rates       the buffer which keeps input and output
38  *  @param rates_size  the size of rate1 buffer; new size of buffer on return
39  *
40  *  @return            0 on success, or -1 on error
41  */
42 static int get_common_rates(struct lbs_private *priv,
43         u8 *rates,
44         u16 *rates_size)
45 {
46         u8 *card_rates = lbs_bg_rates;
47         int ret = 0, i, j;
48         u8 tmp[(ARRAY_SIZE(lbs_bg_rates) - 1) * (*rates_size - 1)];
49         size_t tmp_size = 0;
50
51         /* For each rate in card_rates that exists in rate1, copy to tmp */
52         for (i = 0; i < ARRAY_SIZE(lbs_bg_rates) && card_rates[i]; i++) {
53                 for (j = 0; j < *rates_size && rates[j]; j++) {
54                         if (rates[j] == card_rates[i])
55                                 tmp[tmp_size++] = card_rates[i];
56                 }
57         }
58
59         lbs_deb_hex(LBS_DEB_JOIN, "AP rates    ", rates, *rates_size);
60         lbs_deb_hex(LBS_DEB_JOIN, "card rates  ", card_rates,
61                         ARRAY_SIZE(lbs_bg_rates));
62         lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
63         lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
64
65         if (!priv->enablehwauto) {
66                 for (i = 0; i < tmp_size; i++) {
67                         if (tmp[i] == priv->cur_rate)
68                                 goto done;
69                 }
70                 lbs_pr_alert("Previously set fixed data rate %#x isn't "
71                        "compatible with the network.\n", priv->cur_rate);
72                 ret = -1;
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 static u8 iw_auth_to_ieee_auth(u8 auth)
103 {
104         if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
105                 return 0x00;
106         else if (auth == IW_AUTH_ALG_SHARED_KEY)
107                 return 0x01;
108         else if (auth == IW_AUTH_ALG_LEAP)
109                 return 0x80;
110
111         lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
112         return 0;
113 }
114
115 /**
116  *  @brief This function prepares the authenticate command.  AUTHENTICATE only
117  *  sets the authentication suite for future associations, as the firmware
118  *  handles authentication internally during the ASSOCIATE command.
119  *
120  *  @param priv      A pointer to struct lbs_private structure
121  *  @param bssid     The peer BSSID with which to authenticate
122  *  @param auth      The authentication mode to use (from wireless.h)
123  *
124  *  @return         0 or -1
125  */
126 static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
127 {
128         struct cmd_ds_802_11_authenticate cmd;
129         int ret = -1;
130         DECLARE_MAC_BUF(mac);
131
132         lbs_deb_enter(LBS_DEB_JOIN);
133
134         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
135         memcpy(cmd.bssid, bssid, ETH_ALEN);
136
137         cmd.authtype = iw_auth_to_ieee_auth(auth);
138
139         lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
140                 print_mac(mac, bssid), cmd.authtype);
141
142         ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
143
144         lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
145         return ret;
146 }
147
148
149 static int lbs_assoc_post(struct lbs_private *priv,
150                           struct cmd_ds_802_11_associate_response *resp)
151 {
152         int ret = 0;
153         union iwreq_data wrqu;
154         struct bss_descriptor *bss;
155         u16 status_code;
156
157         lbs_deb_enter(LBS_DEB_ASSOC);
158
159         if (!priv->in_progress_assoc_req) {
160                 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
161                 ret = -1;
162                 goto done;
163         }
164         bss = &priv->in_progress_assoc_req->bss;
165
166         /*
167          * Older FW versions map the IEEE 802.11 Status Code in the association
168          * response to the following values returned in resp->statuscode:
169          *
170          *    IEEE Status Code                Marvell Status Code
171          *    0                       ->      0x0000 ASSOC_RESULT_SUCCESS
172          *    13                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
173          *    14                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
174          *    15                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
175          *    16                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
176          *    others                  ->      0x0003 ASSOC_RESULT_REFUSED
177          *
178          * Other response codes:
179          *    0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
180          *    0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
181          *                                    association response from the AP)
182          */
183
184         status_code = le16_to_cpu(resp->statuscode);
185         if (priv->fwrelease < 0x09000000) {
186                 switch (status_code) {
187                 case 0x00:
188                         break;
189                 case 0x01:
190                         lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
191                         break;
192                 case 0x02:
193                         lbs_deb_assoc("ASSOC_RESP: internal timer "
194                                 "expired while waiting for the AP\n");
195                         break;
196                 case 0x03:
197                         lbs_deb_assoc("ASSOC_RESP: association "
198                                 "refused by AP\n");
199                         break;
200                 case 0x04:
201                         lbs_deb_assoc("ASSOC_RESP: authentication "
202                                 "refused by AP\n");
203                         break;
204                 default:
205                         lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
206                                 " unknown\n", status_code);
207                         break;
208                 }
209         } else {
210                 /* v9+ returns the AP's association response */
211                 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
212         }
213
214         if (status_code) {
215                 lbs_mac_event_disconnected(priv);
216                 ret = -1;
217                 goto done;
218         }
219
220         lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
221                     (void *) (resp + sizeof (resp->hdr)),
222                     le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
223
224         /* Send a Media Connected event, according to the Spec */
225         priv->connect_status = LBS_CONNECTED;
226
227         /* Update current SSID and BSSID */
228         memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
229         priv->curbssparams.ssid_len = bss->ssid_len;
230         memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
231
232         priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
233         priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
234
235         memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
236         memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
237         priv->nextSNRNF = 0;
238         priv->numSNRNF = 0;
239
240         netif_carrier_on(priv->dev);
241         if (!priv->tx_pending_len)
242                 netif_wake_queue(priv->dev);
243
244         memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
245         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
246         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
247
248 done:
249         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
250         return ret;
251 }
252
253 /**
254  *  @brief This function prepares an association-class command.
255  *
256  *  @param priv      A pointer to struct lbs_private structure
257  *  @param assoc_req The association request describing the BSS to associate
258  *                   or reassociate with
259  *  @param command   The actual command, either CMD_802_11_ASSOCIATE or
260  *                   CMD_802_11_REASSOCIATE
261  *
262  *  @return         0 or -1
263  */
264 static int lbs_associate(struct lbs_private *priv,
265                          struct assoc_request *assoc_req,
266                          u16 command)
267 {
268         struct cmd_ds_802_11_associate cmd;
269         int ret = 0;
270         struct bss_descriptor *bss = &assoc_req->bss;
271         u8 *pos = &(cmd.iebuf[0]);
272         u16 tmpcap, tmplen, tmpauth;
273         struct mrvl_ie_ssid_param_set *ssid;
274         struct mrvl_ie_ds_param_set *ds;
275         struct mrvl_ie_cf_param_set *cf;
276         struct mrvl_ie_rates_param_set *rates;
277         struct mrvl_ie_rsn_param_set *rsn;
278         struct mrvl_ie_auth_type *auth;
279
280         lbs_deb_enter(LBS_DEB_ASSOC);
281
282         BUG_ON((command != CMD_802_11_ASSOCIATE) &&
283                 (command != CMD_802_11_REASSOCIATE));
284
285         memset(&cmd, 0, sizeof(cmd));
286         cmd.hdr.command = cpu_to_le16(command);
287
288         /* Fill in static fields */
289         memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
290         cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
291
292         /* Capability info */
293         tmpcap = (bss->capability & CAPINFO_MASK);
294         if (bss->mode == IW_MODE_INFRA)
295                 tmpcap |= WLAN_CAPABILITY_ESS;
296         cmd.capability = cpu_to_le16(tmpcap);
297         lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
298
299         /* SSID */
300         ssid = (struct mrvl_ie_ssid_param_set *) pos;
301         ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
302         tmplen = bss->ssid_len;
303         ssid->header.len = cpu_to_le16(tmplen);
304         memcpy(ssid->ssid, bss->ssid, tmplen);
305         pos += sizeof(ssid->header) + tmplen;
306
307         ds = (struct mrvl_ie_ds_param_set *) pos;
308         ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
309         ds->header.len = cpu_to_le16(1);
310         ds->channel = bss->phy.ds.channel;
311         pos += sizeof(ds->header) + 1;
312
313         cf = (struct mrvl_ie_cf_param_set *) pos;
314         cf->header.type = cpu_to_le16(TLV_TYPE_CF);
315         tmplen = sizeof(*cf) - sizeof (cf->header);
316         cf->header.len = cpu_to_le16(tmplen);
317         /* IE payload should be zeroed, firmware fills it in for us */
318         pos += sizeof(*cf);
319
320         rates = (struct mrvl_ie_rates_param_set *) pos;
321         rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
322         memcpy(&rates->rates, &bss->rates, MAX_RATES);
323         tmplen = min_t(u16, ARRAY_SIZE(rates->rates), MAX_RATES);
324         if (get_common_rates(priv, rates->rates, &tmplen)) {
325                 ret = -1;
326                 goto done;
327         }
328         pos += sizeof(rates->header) + tmplen;
329         rates->header.len = cpu_to_le16(tmplen);
330         lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
331
332         /* Copy the infra. association rates into Current BSS state structure */
333         memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
334         memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
335
336         /* Set MSB on basic rates as the firmware requires, but _after_
337          * copying to current bss rates.
338          */
339         lbs_set_basic_rate_flags(rates->rates, tmplen);
340
341         /* Firmware v9+ indicate authentication suites as a TLV */
342         if (priv->fwrelease >= 0x09000000) {
343                 DECLARE_MAC_BUF(mac);
344
345                 auth = (struct mrvl_ie_auth_type *) pos;
346                 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
347                 auth->header.len = cpu_to_le16(2);
348                 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
349                 auth->auth = cpu_to_le16(tmpauth);
350                 pos += sizeof(auth->header) + 2;
351
352                 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
353                         print_mac(mac, bss->bssid), priv->secinfo.auth_mode);
354         }
355
356         /* WPA/WPA2 IEs */
357         if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
358                 rsn = (struct mrvl_ie_rsn_param_set *) pos;
359                 /* WPA_IE or WPA2_IE */
360                 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
361                 tmplen = (u16) assoc_req->wpa_ie[1];
362                 rsn->header.len = cpu_to_le16(tmplen);
363                 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
364                 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
365                         sizeof(rsn->header) + tmplen);
366                 pos += sizeof(rsn->header) + tmplen;
367         }
368
369         cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
370                                    (u16)(pos - (u8 *) &cmd.iebuf));
371
372         /* update curbssparams */
373         priv->curbssparams.channel = bss->phy.ds.channel;
374
375         if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
376                 ret = -1;
377                 goto done;
378         }
379
380         ret = lbs_cmd_with_response(priv, command, &cmd);
381         if (ret == 0) {
382                 ret = lbs_assoc_post(priv,
383                         (struct cmd_ds_802_11_associate_response *) &cmd);
384         }
385
386 done:
387         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
388         return ret;
389 }
390
391 /**
392  *  @brief Associate to a specific BSS discovered in a scan
393  *
394  *  @param priv      A pointer to struct lbs_private structure
395  *  @param assoc_req The association request describing the BSS to associate with
396  *
397  *  @return          0-success, otherwise fail
398  */
399 static int lbs_try_associate(struct lbs_private *priv,
400         struct assoc_request *assoc_req)
401 {
402         int ret;
403         u8 preamble = RADIO_PREAMBLE_LONG;
404
405         lbs_deb_enter(LBS_DEB_ASSOC);
406
407         /* FW v9 and higher indicate authentication suites as a TLV in the
408          * association command, not as a separate authentication command.
409          */
410         if (priv->fwrelease < 0x09000000) {
411                 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
412                                              priv->secinfo.auth_mode);
413                 if (ret)
414                         goto out;
415         }
416
417         /* Use short preamble only when both the BSS and firmware support it */
418         if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
419             (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
420                 preamble = RADIO_PREAMBLE_SHORT;
421
422         ret = lbs_set_radio(priv, preamble, 1);
423         if (ret)
424                 goto out;
425
426         ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
427
428 out:
429         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
430         return ret;
431 }
432
433 static int lbs_adhoc_post(struct lbs_private *priv,
434                           struct cmd_ds_802_11_ad_hoc_result *resp)
435 {
436         int ret = 0;
437         u16 command = le16_to_cpu(resp->hdr.command);
438         u16 result = le16_to_cpu(resp->hdr.result);
439         union iwreq_data wrqu;
440         struct bss_descriptor *bss;
441         DECLARE_SSID_BUF(ssid);
442
443         lbs_deb_enter(LBS_DEB_JOIN);
444
445         if (!priv->in_progress_assoc_req) {
446                 lbs_deb_join("ADHOC_RESP: no in-progress association "
447                         "request\n");
448                 ret = -1;
449                 goto done;
450         }
451         bss = &priv->in_progress_assoc_req->bss;
452
453         /*
454          * Join result code 0 --> SUCCESS
455          */
456         if (result) {
457                 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
458                 if (priv->connect_status == LBS_CONNECTED)
459                         lbs_mac_event_disconnected(priv);
460                 ret = -1;
461                 goto done;
462         }
463
464         /* Send a Media Connected event, according to the Spec */
465         priv->connect_status = LBS_CONNECTED;
466
467         if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
468                 /* Update the created network descriptor with the new BSSID */
469                 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
470         }
471
472         /* Set the BSSID from the joined/started descriptor */
473         memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
474
475         /* Set the new SSID to current SSID */
476         memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
477         priv->curbssparams.ssid_len = bss->ssid_len;
478
479         netif_carrier_on(priv->dev);
480         if (!priv->tx_pending_len)
481                 netif_wake_queue(priv->dev);
482
483         memset(&wrqu, 0, sizeof(wrqu));
484         memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
485         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
486         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
487
488         lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
489                      print_ssid(ssid, bss->ssid, bss->ssid_len),
490                      priv->curbssparams.bssid,
491                      priv->curbssparams.channel);
492
493 done:
494         lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
495         return ret;
496 }
497
498 /**
499  *  @brief Join an adhoc network found in a previous scan
500  *
501  *  @param priv         A pointer to struct lbs_private structure
502  *  @param assoc_req    The association request describing the BSS to join
503  *
504  *  @return             0 on success, error on failure
505  */
506 static int lbs_adhoc_join(struct lbs_private *priv,
507         struct assoc_request *assoc_req)
508 {
509         struct cmd_ds_802_11_ad_hoc_join cmd;
510         struct bss_descriptor *bss = &assoc_req->bss;
511         u8 preamble = RADIO_PREAMBLE_LONG;
512         DECLARE_SSID_BUF(ssid);
513         u16 ratesize = 0;
514         int ret = 0;
515
516         lbs_deb_enter(LBS_DEB_ASSOC);
517
518         lbs_deb_join("current SSID '%s', ssid length %u\n",
519                 print_ssid(ssid, priv->curbssparams.ssid,
520                 priv->curbssparams.ssid_len),
521                 priv->curbssparams.ssid_len);
522         lbs_deb_join("requested ssid '%s', ssid length %u\n",
523                 print_ssid(ssid, bss->ssid, bss->ssid_len),
524                 bss->ssid_len);
525
526         /* check if the requested SSID is already joined */
527         if (priv->curbssparams.ssid_len &&
528             !lbs_ssid_cmp(priv->curbssparams.ssid,
529                         priv->curbssparams.ssid_len,
530                         bss->ssid, bss->ssid_len) &&
531             (priv->mode == IW_MODE_ADHOC) &&
532             (priv->connect_status == LBS_CONNECTED)) {
533                 union iwreq_data wrqu;
534
535                 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
536                         "current, not attempting to re-join");
537
538                 /* Send the re-association event though, because the association
539                  * request really was successful, even if just a null-op.
540                  */
541                 memset(&wrqu, 0, sizeof(wrqu));
542                 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
543                        ETH_ALEN);
544                 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
545                 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
546                 goto out;
547         }
548
549         /* Use short preamble only when both the BSS and firmware support it */
550         if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
551             (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
552                 lbs_deb_join("AdhocJoin: Short preamble\n");
553                 preamble = RADIO_PREAMBLE_SHORT;
554         }
555
556         ret = lbs_set_radio(priv, preamble, 1);
557         if (ret)
558                 goto out;
559
560         lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
561         lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
562
563         priv->adhoccreate = 0;
564         priv->curbssparams.channel = bss->channel;
565
566         /* Build the join command */
567         memset(&cmd, 0, sizeof(cmd));
568         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
569
570         cmd.bss.type = CMD_BSS_TYPE_IBSS;
571         cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
572
573         memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
574         memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
575
576         memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
577
578         memcpy(&cmd.bss.ibss, &bss->ss.ibss,
579                sizeof(struct ieee_ie_ibss_param_set));
580
581         cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
582         lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
583                bss->capability, CAPINFO_MASK);
584
585         /* information on BSSID descriptor passed to FW */
586         lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
587                         cmd.bss.bssid, cmd.bss.ssid);
588
589         /* Only v8 and below support setting these */
590         if (priv->fwrelease < 0x09000000) {
591                 /* failtimeout */
592                 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
593                 /* probedelay */
594                 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
595         }
596
597         /* Copy Data rates from the rates recorded in scan response */
598         memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
599         ratesize = min_t(u16, ARRAY_SIZE(cmd.bss.rates), MAX_RATES);
600         memcpy(cmd.bss.rates, bss->rates, ratesize);
601         if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
602                 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
603                 ret = -1;
604                 goto out;
605         }
606
607         /* Copy the ad-hoc creation rates into Current BSS state structure */
608         memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
609         memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
610
611         /* Set MSB on basic rates as the firmware requires, but _after_
612          * copying to current bss rates.
613          */
614         lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
615
616         cmd.bss.ibss.atimwindow = bss->atimwindow;
617
618         if (assoc_req->secinfo.wep_enabled) {
619                 u16 tmp = le16_to_cpu(cmd.bss.capability);
620                 tmp |= WLAN_CAPABILITY_PRIVACY;
621                 cmd.bss.capability = cpu_to_le16(tmp);
622         }
623
624         if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
625                 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
626
627                 /* wake up first */
628                 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
629                                                    CMD_ACT_SET, 0, 0,
630                                                    &local_ps_mode);
631                 if (ret) {
632                         ret = -1;
633                         goto out;
634                 }
635         }
636
637         if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
638                 ret = -1;
639                 goto out;
640         }
641
642         ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
643         if (ret == 0) {
644                 ret = lbs_adhoc_post(priv,
645                                      (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
646         }
647
648 out:
649         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
650         return ret;
651 }
652
653 /**
654  *  @brief Start an Adhoc Network
655  *
656  *  @param priv         A pointer to struct lbs_private structure
657  *  @param assoc_req    The association request describing the BSS to start
658  *
659  *  @return             0 on success, error on failure
660  */
661 static int lbs_adhoc_start(struct lbs_private *priv,
662         struct assoc_request *assoc_req)
663 {
664         struct cmd_ds_802_11_ad_hoc_start cmd;
665         u8 preamble = RADIO_PREAMBLE_LONG;
666         size_t ratesize = 0;
667         u16 tmpcap = 0;
668         int ret = 0;
669         DECLARE_SSID_BUF(ssid);
670
671         lbs_deb_enter(LBS_DEB_ASSOC);
672
673         if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
674                 lbs_deb_join("ADHOC_START: Will use short preamble\n");
675                 preamble = RADIO_PREAMBLE_SHORT;
676         }
677
678         ret = lbs_set_radio(priv, preamble, 1);
679         if (ret)
680                 goto out;
681
682         /* Build the start command */
683         memset(&cmd, 0, sizeof(cmd));
684         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
685
686         memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
687
688         lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
689                 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
690                 assoc_req->ssid_len);
691
692         cmd.bsstype = CMD_BSS_TYPE_IBSS;
693
694         if (priv->beacon_period == 0)
695                 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
696         cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
697
698         WARN_ON(!assoc_req->channel);
699
700         /* set Physical parameter set */
701         cmd.ds.header.id = WLAN_EID_DS_PARAMS;
702         cmd.ds.header.len = 1;
703         cmd.ds.channel = assoc_req->channel;
704
705         /* set IBSS parameter set */
706         cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
707         cmd.ibss.header.len = 2;
708         cmd.ibss.atimwindow = cpu_to_le16(0);
709
710         /* set capability info */
711         tmpcap = WLAN_CAPABILITY_IBSS;
712         if (assoc_req->secinfo.wep_enabled ||
713             assoc_req->secinfo.WPAenabled ||
714             assoc_req->secinfo.WPA2enabled) {
715                 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
716                 tmpcap |= WLAN_CAPABILITY_PRIVACY;
717         } else
718                 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
719
720         cmd.capability = cpu_to_le16(tmpcap);
721
722         /* Only v8 and below support setting probe delay */
723         if (priv->fwrelease < 0x09000000)
724                 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
725
726         ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
727         memcpy(cmd.rates, lbs_bg_rates, ratesize);
728
729         /* Copy the ad-hoc creating rates into Current BSS state structure */
730         memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
731         memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
732
733         /* Set MSB on basic rates as the firmware requires, but _after_
734          * copying to current bss rates.
735          */
736         lbs_set_basic_rate_flags(cmd.rates, ratesize);
737
738         lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
739                cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
740
741         if (lbs_create_dnld_countryinfo_11d(priv)) {
742                 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
743                 ret = -1;
744                 goto out;
745         }
746
747         lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
748                      assoc_req->channel, assoc_req->band);
749
750         priv->adhoccreate = 1;
751         priv->mode = IW_MODE_ADHOC;
752
753         ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
754         if (ret == 0)
755                 ret = lbs_adhoc_post(priv,
756                                      (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
757
758 out:
759         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
760         return ret;
761 }
762
763 /**
764  *  @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
765  *
766  *  @param priv         A pointer to struct lbs_private structure
767  *  @return             0 on success, or an error
768  */
769 int lbs_adhoc_stop(struct lbs_private *priv)
770 {
771         struct cmd_ds_802_11_ad_hoc_stop cmd;
772         int ret;
773
774         lbs_deb_enter(LBS_DEB_JOIN);
775
776         memset(&cmd, 0, sizeof (cmd));
777         cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
778
779         ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
780
781         /* Clean up everything even if there was an error */
782         lbs_mac_event_disconnected(priv);
783
784         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
785         return ret;
786 }
787
788 static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
789                                         struct bss_descriptor *match_bss)
790 {
791         if (!secinfo->wep_enabled  && !secinfo->WPAenabled
792             && !secinfo->WPA2enabled
793             && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
794             && match_bss->rsn_ie[0] != WLAN_EID_RSN
795             && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
796                 return 1;
797         else
798                 return 0;
799 }
800
801 static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
802                                        struct bss_descriptor *match_bss)
803 {
804         if (secinfo->wep_enabled && !secinfo->WPAenabled
805             && !secinfo->WPA2enabled
806             && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
807                 return 1;
808         else
809                 return 0;
810 }
811
812 static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
813                                 struct bss_descriptor *match_bss)
814 {
815         if (!secinfo->wep_enabled && secinfo->WPAenabled
816             && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
817             /* privacy bit may NOT be set in some APs like LinkSys WRT54G
818             && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
819            )
820                 return 1;
821         else
822                 return 0;
823 }
824
825 static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
826                                  struct bss_descriptor *match_bss)
827 {
828         if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
829             (match_bss->rsn_ie[0] == WLAN_EID_RSN)
830             /* privacy bit may NOT be set in some APs like LinkSys WRT54G
831             (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
832            )
833                 return 1;
834         else
835                 return 0;
836 }
837
838 static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
839                                         struct bss_descriptor *match_bss)
840 {
841         if (!secinfo->wep_enabled && !secinfo->WPAenabled
842             && !secinfo->WPA2enabled
843             && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
844             && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
845             && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
846                 return 1;
847         else
848                 return 0;
849 }
850
851 /**
852  *  @brief Check if a scanned network compatible with the driver settings
853  *
854  *   WEP     WPA     WPA2    ad-hoc  encrypt                      Network
855  * enabled enabled  enabled   AES     mode   privacy  WPA  WPA2  Compatible
856  *    0       0        0       0      NONE      0      0    0   yes No security
857  *    1       0        0       0      NONE      1      0    0   yes Static WEP
858  *    0       1        0       0       x        1x     1    x   yes WPA
859  *    0       0        1       0       x        1x     x    1   yes WPA2
860  *    0       0        0       1      NONE      1      0    0   yes Ad-hoc AES
861  *    0       0        0       0     !=NONE     1      0    0   yes Dynamic WEP
862  *
863  *
864  *  @param priv A pointer to struct lbs_private
865  *  @param index   Index in scantable to check against current driver settings
866  *  @param mode    Network mode: Infrastructure or IBSS
867  *
868  *  @return        Index in scantable, or error code if negative
869  */
870 static int is_network_compatible(struct lbs_private *priv,
871                                  struct bss_descriptor *bss, uint8_t mode)
872 {
873         int matched = 0;
874
875         lbs_deb_enter(LBS_DEB_SCAN);
876
877         if (bss->mode != mode)
878                 goto done;
879
880         matched = match_bss_no_security(&priv->secinfo, bss);
881         if (matched)
882                 goto done;
883         matched = match_bss_static_wep(&priv->secinfo, bss);
884         if (matched)
885                 goto done;
886         matched = match_bss_wpa(&priv->secinfo, bss);
887         if (matched) {
888                 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
889                              "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
890                              "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
891                              priv->secinfo.wep_enabled ? "e" : "d",
892                              priv->secinfo.WPAenabled ? "e" : "d",
893                              priv->secinfo.WPA2enabled ? "e" : "d",
894                              (bss->capability & WLAN_CAPABILITY_PRIVACY));
895                 goto done;
896         }
897         matched = match_bss_wpa2(&priv->secinfo, bss);
898         if (matched) {
899                 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
900                              "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
901                              "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
902                              priv->secinfo.wep_enabled ? "e" : "d",
903                              priv->secinfo.WPAenabled ? "e" : "d",
904                              priv->secinfo.WPA2enabled ? "e" : "d",
905                              (bss->capability & WLAN_CAPABILITY_PRIVACY));
906                 goto done;
907         }
908         matched = match_bss_dynamic_wep(&priv->secinfo, bss);
909         if (matched) {
910                 lbs_deb_scan("is_network_compatible() dynamic WEP: "
911                              "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
912                              bss->wpa_ie[0], bss->rsn_ie[0],
913                              (bss->capability & WLAN_CAPABILITY_PRIVACY));
914                 goto done;
915         }
916
917         /* bss security settings don't match those configured on card */
918         lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
919                      "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
920                      bss->wpa_ie[0], bss->rsn_ie[0],
921                      priv->secinfo.wep_enabled ? "e" : "d",
922                      priv->secinfo.WPAenabled ? "e" : "d",
923                      priv->secinfo.WPA2enabled ? "e" : "d",
924                      (bss->capability & WLAN_CAPABILITY_PRIVACY));
925
926 done:
927         lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
928         return matched;
929 }
930
931 /**
932  *  @brief This function finds a specific compatible BSSID in the scan list
933  *
934  *  Used in association code
935  *
936  *  @param priv  A pointer to struct lbs_private
937  *  @param bssid    BSSID to find in the scan list
938  *  @param mode     Network mode: Infrastructure or IBSS
939  *
940  *  @return         index in BSSID list, or error return code (< 0)
941  */
942 static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
943                                               uint8_t *bssid, uint8_t mode)
944 {
945         struct bss_descriptor *iter_bss;
946         struct bss_descriptor *found_bss = NULL;
947
948         lbs_deb_enter(LBS_DEB_SCAN);
949
950         if (!bssid)
951                 goto out;
952
953         lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
954
955         /* Look through the scan table for a compatible match.  The loop will
956          *   continue past a matched bssid that is not compatible in case there
957          *   is an AP with multiple SSIDs assigned to the same BSSID
958          */
959         mutex_lock(&priv->lock);
960         list_for_each_entry(iter_bss, &priv->network_list, list) {
961                 if (compare_ether_addr(iter_bss->bssid, bssid))
962                         continue; /* bssid doesn't match */
963                 switch (mode) {
964                 case IW_MODE_INFRA:
965                 case IW_MODE_ADHOC:
966                         if (!is_network_compatible(priv, iter_bss, mode))
967                                 break;
968                         found_bss = iter_bss;
969                         break;
970                 default:
971                         found_bss = iter_bss;
972                         break;
973                 }
974         }
975         mutex_unlock(&priv->lock);
976
977 out:
978         lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
979         return found_bss;
980 }
981
982 /**
983  *  @brief This function finds ssid in ssid list.
984  *
985  *  Used in association code
986  *
987  *  @param priv  A pointer to struct lbs_private
988  *  @param ssid     SSID to find in the list
989  *  @param bssid    BSSID to qualify the SSID selection (if provided)
990  *  @param mode     Network mode: Infrastructure or IBSS
991  *
992  *  @return         index in BSSID list
993  */
994 static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
995                                              uint8_t *ssid, uint8_t ssid_len,
996                                              uint8_t *bssid, uint8_t mode,
997                                              int channel)
998 {
999         u32 bestrssi = 0;
1000         struct bss_descriptor *iter_bss = NULL;
1001         struct bss_descriptor *found_bss = NULL;
1002         struct bss_descriptor *tmp_oldest = NULL;
1003
1004         lbs_deb_enter(LBS_DEB_SCAN);
1005
1006         mutex_lock(&priv->lock);
1007
1008         list_for_each_entry(iter_bss, &priv->network_list, list) {
1009                 if (!tmp_oldest ||
1010                     (iter_bss->last_scanned < tmp_oldest->last_scanned))
1011                         tmp_oldest = iter_bss;
1012
1013                 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1014                                  ssid, ssid_len) != 0)
1015                         continue; /* ssid doesn't match */
1016                 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1017                         continue; /* bssid doesn't match */
1018                 if ((channel > 0) && (iter_bss->channel != channel))
1019                         continue; /* channel doesn't match */
1020
1021                 switch (mode) {
1022                 case IW_MODE_INFRA:
1023                 case IW_MODE_ADHOC:
1024                         if (!is_network_compatible(priv, iter_bss, mode))
1025                                 break;
1026
1027                         if (bssid) {
1028                                 /* Found requested BSSID */
1029                                 found_bss = iter_bss;
1030                                 goto out;
1031                         }
1032
1033                         if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1034                                 bestrssi = SCAN_RSSI(iter_bss->rssi);
1035                                 found_bss = iter_bss;
1036                         }
1037                         break;
1038                 case IW_MODE_AUTO:
1039                 default:
1040                         if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1041                                 bestrssi = SCAN_RSSI(iter_bss->rssi);
1042                                 found_bss = iter_bss;
1043                         }
1044                         break;
1045                 }
1046         }
1047
1048 out:
1049         mutex_unlock(&priv->lock);
1050         lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1051         return found_bss;
1052 }
1053
1054 static int assoc_helper_essid(struct lbs_private *priv,
1055                               struct assoc_request * assoc_req)
1056 {
1057         int ret = 0;
1058         struct bss_descriptor * bss;
1059         int channel = -1;
1060         DECLARE_SSID_BUF(ssid);
1061
1062         lbs_deb_enter(LBS_DEB_ASSOC);
1063
1064         /* FIXME: take channel into account when picking SSIDs if a channel
1065          * is set.
1066          */
1067
1068         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1069                 channel = assoc_req->channel;
1070
1071         lbs_deb_assoc("SSID '%s' requested\n",
1072                       print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
1073         if (assoc_req->mode == IW_MODE_INFRA) {
1074                 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1075                         assoc_req->ssid_len);
1076
1077                 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1078                                 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
1079                 if (bss != NULL) {
1080                         memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1081                         ret = lbs_try_associate(priv, assoc_req);
1082                 } else {
1083                         lbs_deb_assoc("SSID not found; cannot associate\n");
1084                 }
1085         } else if (assoc_req->mode == IW_MODE_ADHOC) {
1086                 /* Scan for the network, do not save previous results.  Stale
1087                  *   scan data will cause us to join a non-existant adhoc network
1088                  */
1089                 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1090                         assoc_req->ssid_len);
1091
1092                 /* Search for the requested SSID in the scan table */
1093                 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1094                                 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
1095                 if (bss != NULL) {
1096                         lbs_deb_assoc("SSID found, will join\n");
1097                         memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1098                         lbs_adhoc_join(priv, assoc_req);
1099                 } else {
1100                         /* else send START command */
1101                         lbs_deb_assoc("SSID not found, creating adhoc network\n");
1102                         memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
1103                                 IW_ESSID_MAX_SIZE);
1104                         assoc_req->bss.ssid_len = assoc_req->ssid_len;
1105                         lbs_adhoc_start(priv, assoc_req);
1106                 }
1107         }
1108
1109         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1110         return ret;
1111 }
1112
1113
1114 static int assoc_helper_bssid(struct lbs_private *priv,
1115                               struct assoc_request * assoc_req)
1116 {
1117         int ret = 0;
1118         struct bss_descriptor * bss;
1119
1120         lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
1121
1122         /* Search for index position in list for requested MAC */
1123         bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
1124                             assoc_req->mode);
1125         if (bss == NULL) {
1126                 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1127                         "cannot associate.\n", assoc_req->bssid);
1128                 goto out;
1129         }
1130
1131         memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1132         if (assoc_req->mode == IW_MODE_INFRA) {
1133                 ret = lbs_try_associate(priv, assoc_req);
1134                 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1135                               ret);
1136         } else if (assoc_req->mode == IW_MODE_ADHOC) {
1137                 lbs_adhoc_join(priv, assoc_req);
1138         }
1139
1140 out:
1141         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1142         return ret;
1143 }
1144
1145
1146 static int assoc_helper_associate(struct lbs_private *priv,
1147                                   struct assoc_request * assoc_req)
1148 {
1149         int ret = 0, done = 0;
1150
1151         lbs_deb_enter(LBS_DEB_ASSOC);
1152
1153         /* If we're given and 'any' BSSID, try associating based on SSID */
1154
1155         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1156                 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1157                     && compare_ether_addr(bssid_off, assoc_req->bssid)) {
1158                         ret = assoc_helper_bssid(priv, assoc_req);
1159                         done = 1;
1160                 }
1161         }
1162
1163         if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1164                 ret = assoc_helper_essid(priv, assoc_req);
1165         }
1166
1167         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1168         return ret;
1169 }
1170
1171
1172 static int assoc_helper_mode(struct lbs_private *priv,
1173                              struct assoc_request * assoc_req)
1174 {
1175         int ret = 0;
1176
1177         lbs_deb_enter(LBS_DEB_ASSOC);
1178
1179         if (assoc_req->mode == priv->mode)
1180                 goto done;
1181
1182         if (assoc_req->mode == IW_MODE_INFRA) {
1183                 if (priv->psstate != PS_STATE_FULL_POWER)
1184                         lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1185                 priv->psmode = LBS802_11POWERMODECAM;
1186         }
1187
1188         priv->mode = assoc_req->mode;
1189         ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
1190
1191 done:
1192         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1193         return ret;
1194 }
1195
1196 static int assoc_helper_channel(struct lbs_private *priv,
1197                                 struct assoc_request * assoc_req)
1198 {
1199         int ret = 0;
1200
1201         lbs_deb_enter(LBS_DEB_ASSOC);
1202
1203         ret = lbs_update_channel(priv);
1204         if (ret) {
1205                 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1206                 goto done;
1207         }
1208
1209         if (assoc_req->channel == priv->curbssparams.channel)
1210                 goto done;
1211
1212         if (priv->mesh_dev) {
1213                 /* Change mesh channel first; 21.p21 firmware won't let
1214                    you change channel otherwise (even though it'll return
1215                    an error to this */
1216                 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1217                                 assoc_req->channel);
1218         }
1219
1220         lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
1221                       priv->curbssparams.channel, assoc_req->channel);
1222
1223         ret = lbs_set_channel(priv, assoc_req->channel);
1224         if (ret < 0)
1225                 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
1226
1227         /* FIXME: shouldn't need to grab the channel _again_ after setting
1228          * it since the firmware is supposed to return the new channel, but
1229          * whatever... */
1230         ret = lbs_update_channel(priv);
1231         if (ret) {
1232                 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1233                 goto done;
1234         }
1235
1236         if (assoc_req->channel != priv->curbssparams.channel) {
1237                 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
1238                               assoc_req->channel);
1239                 goto restore_mesh;
1240         }
1241
1242         if (   assoc_req->secinfo.wep_enabled
1243             &&   (assoc_req->wep_keys[0].len
1244                || assoc_req->wep_keys[1].len
1245                || assoc_req->wep_keys[2].len
1246                || assoc_req->wep_keys[3].len)) {
1247                 /* Make sure WEP keys are re-sent to firmware */
1248                 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1249         }
1250
1251         /* Must restart/rejoin adhoc networks after channel change */
1252         set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
1253
1254  restore_mesh:
1255         if (priv->mesh_dev)
1256                 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1257                                 priv->curbssparams.channel);
1258
1259  done:
1260         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1261         return ret;
1262 }
1263
1264
1265 static int assoc_helper_wep_keys(struct lbs_private *priv,
1266                                  struct assoc_request *assoc_req)
1267 {
1268         int i;
1269         int ret = 0;
1270
1271         lbs_deb_enter(LBS_DEB_ASSOC);
1272
1273         /* Set or remove WEP keys */
1274         if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1275             assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1276                 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1277         else
1278                 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
1279
1280         if (ret)
1281                 goto out;
1282
1283         /* enable/disable the MAC's WEP packet filter */
1284         if (assoc_req->secinfo.wep_enabled)
1285                 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1286         else
1287                 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1288
1289         lbs_set_mac_control(priv);
1290
1291         mutex_lock(&priv->lock);
1292
1293         /* Copy WEP keys into priv wep key fields */
1294         for (i = 0; i < 4; i++) {
1295                 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
1296                        sizeof(struct enc_key));
1297         }
1298         priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
1299
1300         mutex_unlock(&priv->lock);
1301
1302 out:
1303         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1304         return ret;
1305 }
1306
1307 static int assoc_helper_secinfo(struct lbs_private *priv,
1308                                 struct assoc_request * assoc_req)
1309 {
1310         int ret = 0;
1311         uint16_t do_wpa;
1312         uint16_t rsn = 0;
1313
1314         lbs_deb_enter(LBS_DEB_ASSOC);
1315
1316         memcpy(&priv->secinfo, &assoc_req->secinfo,
1317                 sizeof(struct lbs_802_11_security));
1318
1319         lbs_set_mac_control(priv);
1320
1321         /* If RSN is already enabled, don't try to enable it again, since
1322          * ENABLE_RSN resets internal state machines and will clobber the
1323          * 4-way WPA handshake.
1324          */
1325
1326         /* Get RSN enabled/disabled */
1327         ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
1328         if (ret) {
1329                 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
1330                 goto out;
1331         }
1332
1333         /* Don't re-enable RSN if it's already enabled */
1334         do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
1335         if (do_wpa == rsn)
1336                 goto out;
1337
1338         /* Set RSN enabled/disabled */
1339         ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
1340
1341 out:
1342         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1343         return ret;
1344 }
1345
1346
1347 static int assoc_helper_wpa_keys(struct lbs_private *priv,
1348                                  struct assoc_request * assoc_req)
1349 {
1350         int ret = 0;
1351         unsigned int flags = assoc_req->flags;
1352
1353         lbs_deb_enter(LBS_DEB_ASSOC);
1354
1355         /* Work around older firmware bug where WPA unicast and multicast
1356          * keys must be set independently.  Seen in SDIO parts with firmware
1357          * version 5.0.11p0.
1358          */
1359
1360         if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1361                 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1362                 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1363                 assoc_req->flags = flags;
1364         }
1365
1366         if (ret)
1367                 goto out;
1368
1369         if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1370                 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1371
1372                 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1373                 assoc_req->flags = flags;
1374         }
1375
1376 out:
1377         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1378         return ret;
1379 }
1380
1381
1382 static int assoc_helper_wpa_ie(struct lbs_private *priv,
1383                                struct assoc_request * assoc_req)
1384 {
1385         int ret = 0;
1386
1387         lbs_deb_enter(LBS_DEB_ASSOC);
1388
1389         if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1390                 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1391                 priv->wpa_ie_len = assoc_req->wpa_ie_len;
1392         } else {
1393                 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1394                 priv->wpa_ie_len = 0;
1395         }
1396
1397         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1398         return ret;
1399 }
1400
1401
1402 static int should_deauth_infrastructure(struct lbs_private *priv,
1403                                         struct assoc_request * assoc_req)
1404 {
1405         int ret = 0;
1406
1407         if (priv->connect_status != LBS_CONNECTED)
1408                 return 0;
1409
1410         lbs_deb_enter(LBS_DEB_ASSOC);
1411         if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1412                 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1413                 ret = 1;
1414                 goto out;
1415         }
1416
1417         if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1418                 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
1419                         lbs_deb_assoc("Deauthenticating due to new security\n");
1420                         ret = 1;
1421                         goto out;
1422                 }
1423         }
1424
1425         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1426                 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1427                 ret = 1;
1428                 goto out;
1429         }
1430
1431         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1432                 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1433                 ret = 1;
1434                 goto out;
1435         }
1436
1437         /* FIXME: deal with 'auto' mode somehow */
1438         if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1439                 if (assoc_req->mode != IW_MODE_INFRA) {
1440                         lbs_deb_assoc("Deauthenticating due to leaving "
1441                                 "infra mode\n");
1442                         ret = 1;
1443                         goto out;
1444                 }
1445         }
1446
1447 out:
1448         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1449         return ret;
1450 }
1451
1452
1453 static int should_stop_adhoc(struct lbs_private *priv,
1454                              struct assoc_request * assoc_req)
1455 {
1456         lbs_deb_enter(LBS_DEB_ASSOC);
1457
1458         if (priv->connect_status != LBS_CONNECTED)
1459                 return 0;
1460
1461         if (lbs_ssid_cmp(priv->curbssparams.ssid,
1462                               priv->curbssparams.ssid_len,
1463                               assoc_req->ssid, assoc_req->ssid_len) != 0)
1464                 return 1;
1465
1466         /* FIXME: deal with 'auto' mode somehow */
1467         if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1468                 if (assoc_req->mode != IW_MODE_ADHOC)
1469                         return 1;
1470         }
1471
1472         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1473                 if (assoc_req->channel != priv->curbssparams.channel)
1474                         return 1;
1475         }
1476
1477         lbs_deb_leave(LBS_DEB_ASSOC);
1478         return 0;
1479 }
1480
1481
1482 /**
1483  *  @brief This function finds the best SSID in the Scan List
1484  *
1485  *  Search the scan table for the best SSID that also matches the current
1486  *   adapter network preference (infrastructure or adhoc)
1487  *
1488  *  @param priv  A pointer to struct lbs_private
1489  *
1490  *  @return         index in BSSID list
1491  */
1492 static struct bss_descriptor *lbs_find_best_ssid_in_list(
1493         struct lbs_private *priv, uint8_t mode)
1494 {
1495         uint8_t bestrssi = 0;
1496         struct bss_descriptor *iter_bss;
1497         struct bss_descriptor *best_bss = NULL;
1498
1499         lbs_deb_enter(LBS_DEB_SCAN);
1500
1501         mutex_lock(&priv->lock);
1502
1503         list_for_each_entry(iter_bss, &priv->network_list, list) {
1504                 switch (mode) {
1505                 case IW_MODE_INFRA:
1506                 case IW_MODE_ADHOC:
1507                         if (!is_network_compatible(priv, iter_bss, mode))
1508                                 break;
1509                         if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1510                                 break;
1511                         bestrssi = SCAN_RSSI(iter_bss->rssi);
1512                         best_bss = iter_bss;
1513                         break;
1514                 case IW_MODE_AUTO:
1515                 default:
1516                         if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1517                                 break;
1518                         bestrssi = SCAN_RSSI(iter_bss->rssi);
1519                         best_bss = iter_bss;
1520                         break;
1521                 }
1522         }
1523
1524         mutex_unlock(&priv->lock);
1525         lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1526         return best_bss;
1527 }
1528
1529 /**
1530  *  @brief Find the best AP
1531  *
1532  *  Used from association worker.
1533  *
1534  *  @param priv         A pointer to struct lbs_private structure
1535  *  @param pSSID        A pointer to AP's ssid
1536  *
1537  *  @return             0--success, otherwise--fail
1538  */
1539 static int lbs_find_best_network_ssid(struct lbs_private *priv,
1540         uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1541         uint8_t *out_mode)
1542 {
1543         int ret = -1;
1544         struct bss_descriptor *found;
1545
1546         lbs_deb_enter(LBS_DEB_SCAN);
1547
1548         priv->scan_ssid_len = 0;
1549         lbs_scan_networks(priv, 1);
1550         if (priv->surpriseremoved)
1551                 goto out;
1552
1553         found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1554         if (found && (found->ssid_len > 0)) {
1555                 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1556                 *out_ssid_len = found->ssid_len;
1557                 *out_mode = found->mode;
1558                 ret = 0;
1559         }
1560
1561 out:
1562         lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1563         return ret;
1564 }
1565
1566
1567 void lbs_association_worker(struct work_struct *work)
1568 {
1569         struct lbs_private *priv = container_of(work, struct lbs_private,
1570                 assoc_work.work);
1571         struct assoc_request * assoc_req = NULL;
1572         int ret = 0;
1573         int find_any_ssid = 0;
1574         DECLARE_SSID_BUF(ssid);
1575
1576         lbs_deb_enter(LBS_DEB_ASSOC);
1577
1578         mutex_lock(&priv->lock);
1579         assoc_req = priv->pending_assoc_req;
1580         priv->pending_assoc_req = NULL;
1581         priv->in_progress_assoc_req = assoc_req;
1582         mutex_unlock(&priv->lock);
1583
1584         if (!assoc_req)
1585                 goto done;
1586
1587         lbs_deb_assoc(
1588                 "Association Request:\n"
1589                 "    flags:     0x%08lx\n"
1590                 "    SSID:      '%s'\n"
1591                 "    chann:     %d\n"
1592                 "    band:      %d\n"
1593                 "    mode:      %d\n"
1594                 "    BSSID:     %pM\n"
1595                 "    secinfo:  %s%s%s\n"
1596                 "    auth_mode: %d\n",
1597                 assoc_req->flags,
1598                 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
1599                 assoc_req->channel, assoc_req->band, assoc_req->mode,
1600                 assoc_req->bssid,
1601                 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1602                 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1603                 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1604                 assoc_req->secinfo.auth_mode);
1605
1606         /* If 'any' SSID was specified, find an SSID to associate with */
1607         if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
1608             && !assoc_req->ssid_len)
1609                 find_any_ssid = 1;
1610
1611         /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1612         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1613                 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1614                     && compare_ether_addr(assoc_req->bssid, bssid_off))
1615                         find_any_ssid = 0;
1616         }
1617
1618         if (find_any_ssid) {
1619                 u8 new_mode = assoc_req->mode;
1620
1621                 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
1622                                 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
1623                 if (ret) {
1624                         lbs_deb_assoc("Could not find best network\n");
1625                         ret = -ENETUNREACH;
1626                         goto out;
1627                 }
1628
1629                 /* Ensure we switch to the mode of the AP */
1630                 if (assoc_req->mode == IW_MODE_AUTO) {
1631                         set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1632                         assoc_req->mode = new_mode;
1633                 }
1634         }
1635
1636         /*
1637          * Check if the attributes being changing require deauthentication
1638          * from the currently associated infrastructure access point.
1639          */
1640         if (priv->mode == IW_MODE_INFRA) {
1641                 if (should_deauth_infrastructure(priv, assoc_req)) {
1642                         ret = lbs_cmd_80211_deauthenticate(priv,
1643                                                            priv->curbssparams.bssid,
1644                                                            WLAN_REASON_DEAUTH_LEAVING);
1645                         if (ret) {
1646                                 lbs_deb_assoc("Deauthentication due to new "
1647                                         "configuration request failed: %d\n",
1648                                         ret);
1649                         }
1650                 }
1651         } else if (priv->mode == IW_MODE_ADHOC) {
1652                 if (should_stop_adhoc(priv, assoc_req)) {
1653                         ret = lbs_adhoc_stop(priv);
1654                         if (ret) {
1655                                 lbs_deb_assoc("Teardown of AdHoc network due to "
1656                                         "new configuration request failed: %d\n",
1657                                         ret);
1658                         }
1659
1660                 }
1661         }
1662
1663         /* Send the various configuration bits to the firmware */
1664         if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1665                 ret = assoc_helper_mode(priv, assoc_req);
1666                 if (ret)
1667                         goto out;
1668         }
1669
1670         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1671                 ret = assoc_helper_channel(priv, assoc_req);
1672                 if (ret)
1673                         goto out;
1674         }
1675
1676         if (   test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1677             || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1678                 ret = assoc_helper_wep_keys(priv, assoc_req);
1679                 if (ret)
1680                         goto out;
1681         }
1682
1683         if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1684                 ret = assoc_helper_secinfo(priv, assoc_req);
1685                 if (ret)
1686                         goto out;
1687         }
1688
1689         if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1690                 ret = assoc_helper_wpa_ie(priv, assoc_req);
1691                 if (ret)
1692                         goto out;
1693         }
1694
1695         if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1696             || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1697                 ret = assoc_helper_wpa_keys(priv, assoc_req);
1698                 if (ret)
1699                         goto out;
1700         }
1701
1702         /* SSID/BSSID should be the _last_ config option set, because they
1703          * trigger the association attempt.
1704          */
1705         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1706             || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1707                 int success = 1;
1708
1709                 ret = assoc_helper_associate(priv, assoc_req);
1710                 if (ret) {
1711                         lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
1712                                 ret);
1713                         success = 0;
1714                 }
1715
1716                 if (priv->connect_status != LBS_CONNECTED) {
1717                         lbs_deb_assoc("ASSOC: association unsuccessful, "
1718                                 "not connected\n");
1719                         success = 0;
1720                 }
1721
1722                 if (success) {
1723                         lbs_deb_assoc("associated to %pM\n",
1724                                 priv->curbssparams.bssid);
1725                         lbs_prepare_and_send_command(priv,
1726                                 CMD_802_11_RSSI,
1727                                 0, CMD_OPTION_WAITFORRSP, 0, NULL);
1728                 } else {
1729                         ret = -1;
1730                 }
1731         }
1732
1733 out:
1734         if (ret) {
1735                 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
1736                         ret);
1737         }
1738
1739         mutex_lock(&priv->lock);
1740         priv->in_progress_assoc_req = NULL;
1741         mutex_unlock(&priv->lock);
1742         kfree(assoc_req);
1743
1744 done:
1745         lbs_deb_leave(LBS_DEB_ASSOC);
1746 }
1747
1748
1749 /*
1750  * Caller MUST hold any necessary locks
1751  */
1752 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
1753 {
1754         struct assoc_request * assoc_req;
1755
1756         lbs_deb_enter(LBS_DEB_ASSOC);
1757         if (!priv->pending_assoc_req) {
1758                 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
1759                                                      GFP_KERNEL);
1760                 if (!priv->pending_assoc_req) {
1761                         lbs_pr_info("Not enough memory to allocate association"
1762                                 " request!\n");
1763                         return NULL;
1764                 }
1765         }
1766
1767         /* Copy current configuration attributes to the association request,
1768          * but don't overwrite any that are already set.
1769          */
1770         assoc_req = priv->pending_assoc_req;
1771         if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1772                 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
1773                        IW_ESSID_MAX_SIZE);
1774                 assoc_req->ssid_len = priv->curbssparams.ssid_len;
1775         }
1776
1777         if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1778                 assoc_req->channel = priv->curbssparams.channel;
1779
1780         if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
1781                 assoc_req->band = priv->curbssparams.band;
1782
1783         if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
1784                 assoc_req->mode = priv->mode;
1785
1786         if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1787                 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
1788                         ETH_ALEN);
1789         }
1790
1791         if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1792                 int i;
1793                 for (i = 0; i < 4; i++) {
1794                         memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
1795                                 sizeof(struct enc_key));
1796                 }
1797         }
1798
1799         if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
1800                 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
1801
1802         if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1803                 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
1804                         sizeof(struct enc_key));
1805         }
1806
1807         if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1808                 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
1809                         sizeof(struct enc_key));
1810         }
1811
1812         if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1813                 memcpy(&assoc_req->secinfo, &priv->secinfo,
1814                         sizeof(struct lbs_802_11_security));
1815         }
1816
1817         if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1818                 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
1819                         MAX_WPA_IE_LEN);
1820                 assoc_req->wpa_ie_len = priv->wpa_ie_len;
1821         }
1822
1823         lbs_deb_leave(LBS_DEB_ASSOC);
1824         return assoc_req;
1825 }
1826
1827
1828 /**
1829  *  @brief Deauthenticate from a specific BSS
1830  *
1831  *  @param priv        A pointer to struct lbs_private structure
1832  *  @param bssid       The specific BSS to deauthenticate from
1833  *  @param reason      The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1834  *
1835  *  @return            0 on success, error on failure
1836  */
1837 int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1838                                  u16 reason)
1839 {
1840         struct cmd_ds_802_11_deauthenticate cmd;
1841         int ret;
1842
1843         lbs_deb_enter(LBS_DEB_JOIN);
1844
1845         memset(&cmd, 0, sizeof(cmd));
1846         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1847         memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1848         cmd.reasoncode = cpu_to_le16(reason);
1849
1850         ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1851
1852         /* Clean up everything even if there was an error; can't assume that
1853          * we're still authenticated to the AP after trying to deauth.
1854          */
1855         lbs_mac_event_disconnected(priv);
1856
1857         lbs_deb_leave(LBS_DEB_JOIN);
1858         return ret;
1859 }
1860