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