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