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