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