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