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