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