c16d1f625d1b1d87efbc30e0e579bf869c4f35ab
[cascardo/linux.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(MRVDRV_DEFAULT_LISTEN_INTERVAL);
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(MRVDRV_BEACON_INTERVAL);
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 = 0;
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         ratesize = min(sizeof(adhs->rates), sizeof(adhoc_rates_b));
538         memcpy(adhs->rates, adhoc_rates_b, ratesize);
539
540         /* Copy the ad-hoc creating rates into Current BSS state structure */
541         memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
542         memcpy(&adapter->curbssparams.rates, &adhs->rates, ratesize);
543
544         /* Set MSB on basic rates as the firmware requires, but _after_
545          * copying to current bss rates.
546          */
547         libertas_set_basic_rate_flags(adhs->rates, ratesize);
548
549         lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
550                adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[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         u16 ratesize = 0;
588
589         lbs_deb_enter(LBS_DEB_JOIN);
590
591         cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
592
593         join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
594         join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
595
596         memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
597         memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
598
599         memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
600                sizeof(union ieeetypes_phyparamset));
601
602         memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
603                sizeof(union IEEEtypes_ssparamset));
604
605         join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
606         lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
607                bss->capability, CAPINFO_MASK);
608
609         /* information on BSSID descriptor passed to FW */
610         lbs_deb_join(
611                "ADHOC_J_CMD: BSSID = " MAC_FMT ", SSID = '%s'\n",
612                MAC_ARG(join_cmd->bss.bssid), join_cmd->bss.ssid);
613
614         /* failtimeout */
615         join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
616
617         /* probedelay */
618         join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
619
620         adapter->curbssparams.channel = bss->channel;
621
622         /* Copy Data rates from the rates recorded in scan response */
623         memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
624         ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
625         memcpy(join_cmd->bss.rates, bss->rates, ratesize);
626         if (get_common_rates(adapter, join_cmd->bss.rates, &ratesize)) {
627                 lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
628                 ret = -1;
629                 goto done;
630         }
631
632         /* Copy the ad-hoc creating rates into Current BSS state structure */
633         memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
634         memcpy(&adapter->curbssparams.rates, join_cmd->bss.rates, ratesize);
635
636         /* Set MSB on basic rates as the firmware requires, but _after_
637          * copying to current bss rates.
638          */
639         libertas_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
640
641         join_cmd->bss.ssparamset.ibssparamset.atimwindow =
642             cpu_to_le16(bss->atimwindow);
643
644         if (assoc_req->secinfo.wep_enabled) {
645                 u16 tmp = le16_to_cpu(join_cmd->bss.capability);
646                 tmp |= WLAN_CAPABILITY_PRIVACY;
647                 join_cmd->bss.capability = cpu_to_le16(tmp);
648         }
649
650         if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
651                 /* wake up first */
652                 __le32 Localpsmode;
653
654                 Localpsmode = cpu_to_le32(WLAN802_11POWERMODECAM);
655                 ret = libertas_prepare_and_send_command(priv,
656                                             CMD_802_11_PS_MODE,
657                                             CMD_ACT_SET,
658                                             0, 0, &Localpsmode);
659
660                 if (ret) {
661                         ret = -1;
662                         goto done;
663                 }
664         }
665
666         if (libertas_parse_dnld_countryinfo_11d(priv, bss)) {
667                 ret = -1;
668                 goto done;
669         }
670
671         cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
672                                 S_DS_GEN + cmdappendsize);
673
674 done:
675         lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
676         return ret;
677 }
678
679 int libertas_ret_80211_associate(wlan_private * priv,
680                               struct cmd_ds_command *resp)
681 {
682         wlan_adapter *adapter = priv->adapter;
683         int ret = 0;
684         union iwreq_data wrqu;
685         struct ieeetypes_assocrsp *passocrsp;
686         struct bss_descriptor * bss;
687
688         lbs_deb_enter(LBS_DEB_JOIN);
689
690         if (!adapter->in_progress_assoc_req) {
691                 lbs_deb_join("ASSOC_RESP: no in-progress association request\n");
692                 ret = -1;
693                 goto done;
694         }
695         bss = &adapter->in_progress_assoc_req->bss;
696
697         passocrsp = (struct ieeetypes_assocrsp *) & resp->params;
698
699         if (le16_to_cpu(passocrsp->statuscode)) {
700                 libertas_mac_event_disconnected(priv);
701
702                 lbs_deb_join("ASSOC_RESP: Association failed, status code = %d\n",
703                              le16_to_cpu(passocrsp->statuscode));
704
705                 ret = -1;
706                 goto done;
707         }
708
709         lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_RESP", (void *)&resp->params,
710                 le16_to_cpu(resp->size) - S_DS_GEN);
711
712         /* Send a Media Connected event, according to the Spec */
713         adapter->connect_status = LIBERTAS_CONNECTED;
714
715         lbs_deb_join("ASSOC_RESP: assocated to '%s'\n",
716                      escape_essid(bss->ssid, bss->ssid_len));
717
718         /* Update current SSID and BSSID */
719         memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
720         adapter->curbssparams.ssid_len = bss->ssid_len;
721         memcpy(adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
722
723         lbs_deb_join("ASSOC_RESP: currentpacketfilter is %x\n",
724                adapter->currentpacketfilter);
725
726         adapter->SNR[TYPE_RXPD][TYPE_AVG] = 0;
727         adapter->NF[TYPE_RXPD][TYPE_AVG] = 0;
728
729         memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
730         memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
731         adapter->nextSNRNF = 0;
732         adapter->numSNRNF = 0;
733
734         netif_carrier_on(priv->dev);
735         netif_wake_queue(priv->dev);
736
737         if (priv->mesh_dev) {
738                 netif_carrier_on(priv->mesh_dev);
739                 netif_wake_queue(priv->mesh_dev);
740         }
741
742         lbs_deb_join("ASSOC_RESP: Associated \n");
743
744         memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
745         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
746         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
747
748 done:
749         lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
750         return ret;
751 }
752
753 int libertas_ret_80211_disassociate(wlan_private * priv,
754                                  struct cmd_ds_command *resp)
755 {
756         lbs_deb_enter(LBS_DEB_JOIN);
757
758         libertas_mac_event_disconnected(priv);
759
760         lbs_deb_leave(LBS_DEB_JOIN);
761         return 0;
762 }
763
764 int libertas_ret_80211_ad_hoc_start(wlan_private * priv,
765                                  struct cmd_ds_command *resp)
766 {
767         wlan_adapter *adapter = priv->adapter;
768         int ret = 0;
769         u16 command = le16_to_cpu(resp->command);
770         u16 result = le16_to_cpu(resp->result);
771         struct cmd_ds_802_11_ad_hoc_result *padhocresult;
772         union iwreq_data wrqu;
773         struct bss_descriptor *bss;
774
775         lbs_deb_enter(LBS_DEB_JOIN);
776
777         padhocresult = &resp->params.result;
778
779         lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
780         lbs_deb_join("ADHOC_RESP: command = %x\n", command);
781         lbs_deb_join("ADHOC_RESP: result = %x\n", result);
782
783         if (!adapter->in_progress_assoc_req) {
784                 lbs_deb_join("ADHOC_RESP: no in-progress association request\n");
785                 ret = -1;
786                 goto done;
787         }
788         bss = &adapter->in_progress_assoc_req->bss;
789
790         /*
791          * Join result code 0 --> SUCCESS
792          */
793         if (result) {
794                 lbs_deb_join("ADHOC_RESP: failed\n");
795                 if (adapter->connect_status == LIBERTAS_CONNECTED) {
796                         libertas_mac_event_disconnected(priv);
797                 }
798                 ret = -1;
799                 goto done;
800         }
801
802         /*
803          * Now the join cmd should be successful
804          * If BSSID has changed use SSID to compare instead of BSSID
805          */
806         lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
807                      escape_essid(bss->ssid, bss->ssid_len));
808
809         /* Send a Media Connected event, according to the Spec */
810         adapter->connect_status = LIBERTAS_CONNECTED;
811
812         if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
813                 /* Update the created network descriptor with the new BSSID */
814                 memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
815         }
816
817         /* Set the BSSID from the joined/started descriptor */
818         memcpy(&adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
819
820         /* Set the new SSID to current SSID */
821         memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
822         adapter->curbssparams.ssid_len = bss->ssid_len;
823
824         netif_carrier_on(priv->dev);
825         netif_wake_queue(priv->dev);
826
827         if (priv->mesh_dev) {
828                 netif_carrier_on(priv->mesh_dev);
829                 netif_wake_queue(priv->mesh_dev);
830         }
831
832         memset(&wrqu, 0, sizeof(wrqu));
833         memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
834         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
835         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
836
837         lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
838         lbs_deb_join("ADHOC_RESP: channel = %d\n", adapter->curbssparams.channel);
839         lbs_deb_join("ADHOC_RESP: BSSID = " MAC_FMT "\n",
840                MAC_ARG(padhocresult->bssid));
841
842 done:
843         lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
844         return ret;
845 }
846
847 int libertas_ret_80211_ad_hoc_stop(wlan_private * priv,
848                                 struct cmd_ds_command *resp)
849 {
850         lbs_deb_enter(LBS_DEB_JOIN);
851
852         libertas_mac_event_disconnected(priv);
853
854         lbs_deb_leave(LBS_DEB_JOIN);
855         return 0;
856 }