[PATCH] libertas: use MAC_FMT and MAC_ARG where appropriate
[safe/jmp/linux-2.6] / drivers / net / wireless / libertas / join.c
1 /**
2   *  Functions implementing wlan infrastructure and adhoc join routines,
3   *  IOCTL handlers as well as command preperation and response routines
4   *  for sending adhoc start, adhoc join, and association commands
5   *  to the firmware.
6   */
7 #include <linux/netdevice.h>
8 #include <linux/if_arp.h>
9 #include <linux/wireless.h>
10 #include <linux/etherdevice.h>
11
12 #include <net/iw_handler.h>
13
14 #include "host.h"
15 #include "decl.h"
16 #include "join.h"
17 #include "dev.h"
18 #include "assoc.h"
19
20 #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         cmd->command = cpu_to_le16(cmd_802_11_authenticate);
267         cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
268                                 + S_DS_GEN);
269
270         /* translate auth mode to 802.11 defined wire value */
271         switch (adapter->secinfo.auth_mode) {
272         case IW_AUTH_ALG_OPEN_SYSTEM:
273                 pauthenticate->authtype = 0x00;
274                 break;
275         case IW_AUTH_ALG_SHARED_KEY:
276                 pauthenticate->authtype = 0x01;
277                 break;
278         case IW_AUTH_ALG_LEAP:
279                 pauthenticate->authtype = 0x80;
280                 break;
281         default:
282                 lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
283                              adapter->secinfo.auth_mode);
284                 goto out;
285         }
286
287         memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
288
289         lbs_deb_join("AUTH_CMD: Bssid is : " MAC_FMT "\n", MAC_ARG(bssid));
290         ret = 0;
291
292 out:
293         return ret;
294 }
295
296 int libertas_cmd_80211_deauthenticate(wlan_private * priv,
297                                    struct cmd_ds_command *cmd)
298 {
299         wlan_adapter *adapter = priv->adapter;
300         struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
301
302         lbs_deb_enter(LBS_DEB_JOIN);
303
304         cmd->command = cpu_to_le16(cmd_802_11_deauthenticate);
305         cmd->size =
306             cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
307                              S_DS_GEN);
308
309         /* set AP MAC address */
310         memmove(dauth->macaddr, adapter->curbssparams.bssid,
311                 ETH_ALEN);
312
313         /* Reason code 3 = Station is leaving */
314 #define REASON_CODE_STA_LEAVING 3
315         dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
316
317         lbs_deb_leave(LBS_DEB_JOIN);
318         return 0;
319 }
320
321 int libertas_cmd_80211_associate(wlan_private * priv,
322                               struct cmd_ds_command *cmd, void *pdata_buf)
323 {
324         wlan_adapter *adapter = priv->adapter;
325         struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
326         int ret = 0;
327         struct assoc_request * assoc_req = pdata_buf;
328         struct bss_descriptor * bss = &assoc_req->bss;
329         u8 *card_rates;
330         u8 *pos;
331         int card_rates_size;
332         u16 tmpcap;
333         struct mrvlietypes_ssidparamset *ssid;
334         struct mrvlietypes_phyparamset *phy;
335         struct mrvlietypes_ssparamset *ss;
336         struct mrvlietypes_ratesparamset *rates;
337         struct mrvlietypes_rsnparamset *rsn;
338
339         lbs_deb_enter(LBS_DEB_JOIN);
340
341         pos = (u8 *) passo;
342
343         if (!adapter) {
344                 ret = -1;
345                 goto done;
346         }
347
348         cmd->command = cpu_to_le16(cmd_802_11_associate);
349
350         memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
351         pos += sizeof(passo->peerstaaddr);
352
353         /* set the listen interval */
354         passo->listeninterval = adapter->listeninterval;
355
356         pos += sizeof(passo->capinfo);
357         pos += sizeof(passo->listeninterval);
358         pos += sizeof(passo->bcnperiod);
359         pos += sizeof(passo->dtimperiod);
360
361         ssid = (struct mrvlietypes_ssidparamset *) pos;
362         ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
363         ssid->header.len = bss->ssid.ssidlength;
364         memcpy(ssid->ssid, bss->ssid.ssid, ssid->header.len);
365         pos += sizeof(ssid->header) + ssid->header.len;
366         ssid->header.len = cpu_to_le16(ssid->header.len);
367
368         phy = (struct mrvlietypes_phyparamset *) pos;
369         phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
370         phy->header.len = sizeof(phy->fh_ds.dsparamset);
371         memcpy(&phy->fh_ds.dsparamset,
372                &bss->phyparamset.dsparamset.currentchan,
373                sizeof(phy->fh_ds.dsparamset));
374         pos += sizeof(phy->header) + phy->header.len;
375         phy->header.len = cpu_to_le16(phy->header.len);
376
377         ss = (struct mrvlietypes_ssparamset *) pos;
378         ss->header.type = cpu_to_le16(TLV_TYPE_CF);
379         ss->header.len = sizeof(ss->cf_ibss.cfparamset);
380         pos += sizeof(ss->header) + ss->header.len;
381         ss->header.len = cpu_to_le16(ss->header.len);
382
383         rates = (struct mrvlietypes_ratesparamset *) pos;
384         rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
385
386         memcpy(&rates->rates, &bss->libertas_supported_rates, WLAN_SUPPORTED_RATES);
387
388         card_rates = libertas_supported_rates;
389         card_rates_size = sizeof(libertas_supported_rates);
390
391         if (get_common_rates(adapter, rates->rates, WLAN_SUPPORTED_RATES,
392                              card_rates, card_rates_size)) {
393                 ret = -1;
394                 goto done;
395         }
396
397         rates->header.len = min_t(size_t, strlen(rates->rates), WLAN_SUPPORTED_RATES);
398         adapter->curbssparams.numofrates = rates->header.len;
399
400         pos += sizeof(rates->header) + rates->header.len;
401         rates->header.len = cpu_to_le16(rates->header.len);
402
403         if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
404                 rsn = (struct mrvlietypes_rsnparamset *) pos;
405                 rsn->header.type = (u16) assoc_req->wpa_ie[0];  /* WPA_IE or WPA2_IE */
406                 rsn->header.type = cpu_to_le16(rsn->header.type);
407                 rsn->header.len = (u16) assoc_req->wpa_ie[1];
408                 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], rsn->header.len);
409                 lbs_dbg_hex("ASSOC_CMD: RSN IE", (u8 *) rsn,
410                         sizeof(rsn->header) + rsn->header.len);
411                 pos += sizeof(rsn->header) + rsn->header.len;
412                 rsn->header.len = cpu_to_le16(rsn->header.len);
413         }
414
415         /* update curbssparams */
416         adapter->curbssparams.channel =
417             (bss->phyparamset.dsparamset.currentchan);
418
419         /* Copy the infra. association rates into Current BSS state structure */
420         memcpy(&adapter->curbssparams.datarates, &rates->rates,
421                min_t(size_t, sizeof(adapter->curbssparams.datarates), rates->header.len));
422
423         lbs_deb_join("ASSOC_CMD: rates->header.len = %d\n", rates->header.len);
424
425         /* set IBSS field */
426         if (bss->mode == IW_MODE_INFRA) {
427 #define CAPINFO_ESS_MODE 1
428                 passo->capinfo.ess = CAPINFO_ESS_MODE;
429         }
430
431         if (libertas_parse_dnld_countryinfo_11d(priv, bss)) {
432                 ret = -1;
433                 goto done;
434         }
435
436         cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
437
438         /* set the capability info at last */
439         memcpy(&tmpcap, &bss->cap, sizeof(passo->capinfo));
440         tmpcap &= CAPINFO_MASK;
441         lbs_deb_join("ASSOC_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
442                tmpcap, CAPINFO_MASK);
443         tmpcap = cpu_to_le16(tmpcap);
444         memcpy(&passo->capinfo, &tmpcap, sizeof(passo->capinfo));
445
446 done:
447         lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
448         return ret;
449 }
450
451 int libertas_cmd_80211_ad_hoc_start(wlan_private * priv,
452                                  struct cmd_ds_command *cmd, void *pdata_buf)
453 {
454         wlan_adapter *adapter = priv->adapter;
455         struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
456         int ret = 0;
457         int cmdappendsize = 0;
458         int i;
459         u16 tmpcap;
460         struct assoc_request * assoc_req = pdata_buf;
461
462         lbs_deb_enter(LBS_DEB_JOIN);
463
464         if (!adapter) {
465                 ret = -1;
466                 goto done;
467         }
468
469         cmd->command = cpu_to_le16(cmd_802_11_ad_hoc_start);
470
471         /*
472          * Fill in the parameters for 2 data structures:
473          *   1. cmd_ds_802_11_ad_hoc_start command
474          *   2. adapter->scantable[i]
475          *
476          * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
477          *   probe delay, and cap info.
478          *
479          * Firmware will fill up beacon period, DTIM, Basic rates
480          *   and operational rates.
481          */
482
483         memset(adhs->SSID, 0, IW_ESSID_MAX_SIZE);
484         memcpy(adhs->SSID, assoc_req->ssid.ssid, assoc_req->ssid.ssidlength);
485
486         lbs_deb_join("ADHOC_S_CMD: SSID = %s\n", adhs->SSID);
487
488         /* set the BSS type */
489         adhs->bsstype = cmd_bss_type_ibss;
490         adapter->mode = IW_MODE_ADHOC;
491         adhs->beaconperiod = adapter->beaconperiod;
492
493         /* set Physical param set */
494 #define DS_PARA_IE_ID   3
495 #define DS_PARA_IE_LEN  1
496
497         adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
498         adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
499
500         WARN_ON(!assoc_req->channel);
501
502         lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
503                assoc_req->channel);
504
505         adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
506
507         /* set IBSS param set */
508 #define IBSS_PARA_IE_ID   6
509 #define IBSS_PARA_IE_LEN  2
510
511         adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
512         adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
513         adhs->ssparamset.ibssparamset.atimwindow = adapter->atimwindow;
514
515         /* set capability info */
516         adhs->cap.ess = 0;
517         adhs->cap.ibss = 1;
518
519         /* probedelay */
520         adhs->probedelay = cpu_to_le16(cmd_scan_probe_delay_time);
521
522         /* set up privacy in adapter->scantable[i] */
523         if (assoc_req->secinfo.wep_enabled) {
524                 lbs_deb_join("ADHOC_S_CMD: WEP enabled, setting privacy on\n");
525                 adhs->cap.privacy = AD_HOC_CAP_PRIVACY_ON;
526         } else {
527                 lbs_deb_join("ADHOC_S_CMD: WEP disabled, setting privacy off\n");
528         }
529
530         memset(adhs->datarate, 0, sizeof(adhs->datarate));
531
532         if (adapter->adhoc_grate_enabled) {
533                 memcpy(adhs->datarate, libertas_adhoc_rates_g,
534                        min(sizeof(adhs->datarate), sizeof(libertas_adhoc_rates_g)));
535         } else {
536                 memcpy(adhs->datarate, libertas_adhoc_rates_b,
537                        min(sizeof(adhs->datarate), sizeof(libertas_adhoc_rates_b)));
538         }
539
540         /* Find the last non zero */
541         for (i = 0; i < sizeof(adhs->datarate) && adhs->datarate[i]; i++) ;
542
543         adapter->curbssparams.numofrates = i;
544
545         /* Copy the ad-hoc creating rates into Current BSS state structure */
546         memcpy(&adapter->curbssparams.datarates,
547                &adhs->datarate, adapter->curbssparams.numofrates);
548
549         lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
550                adhs->datarate[0], adhs->datarate[1],
551                adhs->datarate[2], adhs->datarate[3]);
552
553         lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
554
555         if (libertas_create_dnld_countryinfo_11d(priv)) {
556                 lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
557                 ret = -1;
558                 goto done;
559         }
560
561         cmd->size =
562             cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start)
563                              + S_DS_GEN + cmdappendsize);
564
565         memcpy(&tmpcap, &adhs->cap, sizeof(u16));
566         tmpcap = cpu_to_le16(tmpcap);
567         memcpy(&adhs->cap, &tmpcap, sizeof(u16));
568
569         ret = 0;
570 done:
571         lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
572         return ret;
573 }
574
575 int libertas_cmd_80211_ad_hoc_stop(wlan_private * priv,
576                                 struct cmd_ds_command *cmd)
577 {
578         cmd->command = cpu_to_le16(cmd_802_11_ad_hoc_stop);
579         cmd->size = cpu_to_le16(S_DS_GEN);
580
581         return 0;
582 }
583
584 int libertas_cmd_80211_ad_hoc_join(wlan_private * priv,
585                                 struct cmd_ds_command *cmd, void *pdata_buf)
586 {
587         wlan_adapter *adapter = priv->adapter;
588         struct cmd_ds_802_11_ad_hoc_join *padhocjoin = &cmd->params.adj;
589         struct assoc_request * assoc_req = pdata_buf;
590         struct bss_descriptor *bss = &assoc_req->bss;
591         int cmdappendsize = 0;
592         int ret = 0;
593         u8 *card_rates;
594         int card_rates_size;
595         u16 tmpcap;
596         int i;
597
598         lbs_deb_enter(LBS_DEB_JOIN);
599
600         cmd->command = cpu_to_le16(cmd_802_11_ad_hoc_join);
601
602         padhocjoin->bssdescriptor.bsstype = cmd_bss_type_ibss;
603
604         padhocjoin->bssdescriptor.beaconperiod = bss->beaconperiod;
605
606         memcpy(&padhocjoin->bssdescriptor.BSSID, &bss->bssid, ETH_ALEN);
607         memcpy(&padhocjoin->bssdescriptor.SSID, &bss->ssid.ssid, bss->ssid.ssidlength);
608
609         memcpy(&padhocjoin->bssdescriptor.phyparamset,
610                &bss->phyparamset, sizeof(union ieeetypes_phyparamset));
611
612         memcpy(&padhocjoin->bssdescriptor.ssparamset,
613                &bss->ssparamset, sizeof(union IEEEtypes_ssparamset));
614
615         memcpy(&tmpcap, &bss->cap, sizeof(struct ieeetypes_capinfo));
616         tmpcap &= CAPINFO_MASK;
617
618         lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
619                tmpcap, CAPINFO_MASK);
620         memcpy(&padhocjoin->bssdescriptor.cap, &tmpcap,
621                sizeof(struct ieeetypes_capinfo));
622
623         /* information on BSSID descriptor passed to FW */
624         lbs_deb_join(
625                "ADHOC_J_CMD: BSSID = " MAC_FMT ", SSID = '%s'\n",
626                MAC_ARG(padhocjoin->bssdescriptor.BSSID),
627                padhocjoin->bssdescriptor.SSID);
628
629         /* failtimeout */
630         padhocjoin->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
631
632         /* probedelay */
633         padhocjoin->probedelay =
634             cpu_to_le16(cmd_scan_probe_delay_time);
635
636         /* Copy Data rates from the rates recorded in scan response */
637         memset(padhocjoin->bssdescriptor.datarates, 0,
638                sizeof(padhocjoin->bssdescriptor.datarates));
639         memcpy(padhocjoin->bssdescriptor.datarates, bss->datarates,
640                min(sizeof(padhocjoin->bssdescriptor.datarates),
641                    sizeof(bss->datarates)));
642
643         card_rates = libertas_supported_rates;
644         card_rates_size = sizeof(libertas_supported_rates);
645
646         adapter->curbssparams.channel = bss->channel;
647
648         if (get_common_rates(adapter, padhocjoin->bssdescriptor.datarates,
649                              sizeof(padhocjoin->bssdescriptor.datarates),
650                              card_rates, card_rates_size)) {
651                 lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
652                 ret = -1;
653                 goto done;
654         }
655
656         /* Find the last non zero */
657         for (i = 0; i < sizeof(padhocjoin->bssdescriptor.datarates)
658              && padhocjoin->bssdescriptor.datarates[i]; i++) ;
659
660         adapter->curbssparams.numofrates = i;
661
662         /*
663          * Copy the adhoc joining rates to Current BSS State structure
664          */
665         memcpy(adapter->curbssparams.datarates,
666                padhocjoin->bssdescriptor.datarates,
667                adapter->curbssparams.numofrates);
668
669         padhocjoin->bssdescriptor.ssparamset.ibssparamset.atimwindow =
670             cpu_to_le16(bss->atimwindow);
671
672         if (assoc_req->secinfo.wep_enabled) {
673                 padhocjoin->bssdescriptor.cap.privacy = AD_HOC_CAP_PRIVACY_ON;
674         }
675
676         if (adapter->psmode == wlan802_11powermodemax_psp) {
677                 /* wake up first */
678                 enum WLAN_802_11_POWER_MODE Localpsmode;
679
680                 Localpsmode = wlan802_11powermodecam;
681                 ret = libertas_prepare_and_send_command(priv,
682                                             cmd_802_11_ps_mode,
683                                             cmd_act_set,
684                                             0, 0, &Localpsmode);
685
686                 if (ret) {
687                         ret = -1;
688                         goto done;
689                 }
690         }
691
692         if (libertas_parse_dnld_countryinfo_11d(priv, bss)) {
693                 ret = -1;
694                 goto done;
695         }
696
697         cmd->size =
698             cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join)
699                              + S_DS_GEN + cmdappendsize);
700
701         memcpy(&tmpcap, &padhocjoin->bssdescriptor.cap,
702                sizeof(struct ieeetypes_capinfo));
703         tmpcap = cpu_to_le16(tmpcap);
704
705         memcpy(&padhocjoin->bssdescriptor.cap,
706                &tmpcap, sizeof(struct ieeetypes_capinfo));
707
708 done:
709         lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
710         return ret;
711 }
712
713 int libertas_ret_80211_associate(wlan_private * priv,
714                               struct cmd_ds_command *resp)
715 {
716         wlan_adapter *adapter = priv->adapter;
717         int ret = 0;
718         union iwreq_data wrqu;
719         struct ieeetypes_assocrsp *passocrsp;
720         struct bss_descriptor * bss;
721
722         lbs_deb_enter(LBS_DEB_JOIN);
723
724         if (!adapter->in_progress_assoc_req) {
725                 lbs_deb_join("ASSOC_RESP: no in-progress association request\n");
726                 ret = -1;
727                 goto done;
728         }
729         bss = &adapter->in_progress_assoc_req->bss;
730
731         passocrsp = (struct ieeetypes_assocrsp *) & resp->params;
732
733         if (passocrsp->statuscode) {
734                 libertas_mac_event_disconnected(priv);
735
736                 lbs_deb_join("ASSOC_RESP: Association failed, status code = %d\n",
737                      passocrsp->statuscode);
738
739                 ret = -1;
740                 goto done;
741         }
742
743         lbs_dbg_hex("ASSOC_RESP:", (void *)&resp->params,
744                 le16_to_cpu(resp->size) - S_DS_GEN);
745
746         /* Send a Media Connected event, according to the Spec */
747         adapter->connect_status = libertas_connected;
748
749         lbs_deb_join("ASSOC_RESP: %s\n", bss->ssid.ssid);
750
751         /* Update current SSID and BSSID */
752         memcpy(&adapter->curbssparams.ssid,
753                &bss->ssid, sizeof(struct WLAN_802_11_SSID));
754         memcpy(adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
755
756         lbs_deb_join("ASSOC_RESP: currentpacketfilter is %x\n",
757                adapter->currentpacketfilter);
758
759         adapter->SNR[TYPE_RXPD][TYPE_AVG] = 0;
760         adapter->NF[TYPE_RXPD][TYPE_AVG] = 0;
761
762         memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
763         memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
764         adapter->nextSNRNF = 0;
765         adapter->numSNRNF = 0;
766
767         netif_carrier_on(priv->dev);
768         netif_wake_queue(priv->dev);
769
770         netif_carrier_on(priv->mesh_dev);
771         netif_wake_queue(priv->mesh_dev);
772
773         lbs_deb_join("ASSOC_RESP: Associated \n");
774
775         memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
776         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
777         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
778
779 done:
780         lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
781         return ret;
782 }
783
784 int libertas_ret_80211_disassociate(wlan_private * priv,
785                                  struct cmd_ds_command *resp)
786 {
787         lbs_deb_enter(LBS_DEB_JOIN);
788
789         libertas_mac_event_disconnected(priv);
790
791         lbs_deb_leave(LBS_DEB_JOIN);
792         return 0;
793 }
794
795 int libertas_ret_80211_ad_hoc_start(wlan_private * priv,
796                                  struct cmd_ds_command *resp)
797 {
798         wlan_adapter *adapter = priv->adapter;
799         int ret = 0;
800         u16 command = le16_to_cpu(resp->command);
801         u16 result = le16_to_cpu(resp->result);
802         struct cmd_ds_802_11_ad_hoc_result *padhocresult;
803         union iwreq_data wrqu;
804         struct bss_descriptor *bss;
805
806         lbs_deb_enter(LBS_DEB_JOIN);
807
808         padhocresult = &resp->params.result;
809
810         lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
811         lbs_deb_join("ADHOC_RESP: command = %x\n", command);
812         lbs_deb_join("ADHOC_RESP: result = %x\n", result);
813
814         if (!adapter->in_progress_assoc_req) {
815                 lbs_deb_join("ADHOC_RESP: no in-progress association request\n");
816                 ret = -1;
817                 goto done;
818         }
819         bss = &adapter->in_progress_assoc_req->bss;
820
821         /*
822          * Join result code 0 --> SUCCESS
823          */
824         if (result) {
825                 lbs_deb_join("ADHOC_RESP: failed\n");
826                 if (adapter->connect_status == libertas_connected) {
827                         libertas_mac_event_disconnected(priv);
828                 }
829                 ret = -1;
830                 goto done;
831         }
832
833         /*
834          * Now the join cmd should be successful
835          * If BSSID has changed use SSID to compare instead of BSSID
836          */
837         lbs_deb_join("ADHOC_RESP: %s\n", bss->ssid.ssid);
838
839         /* Send a Media Connected event, according to the Spec */
840         adapter->connect_status = libertas_connected;
841
842         if (command == cmd_ret_802_11_ad_hoc_start) {
843                 /* Update the created network descriptor with the new BSSID */
844                 memcpy(bss->bssid, padhocresult->BSSID, ETH_ALEN);
845         }
846
847         /* Set the BSSID from the joined/started descriptor */
848         memcpy(&adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
849
850         /* Set the new SSID to current SSID */
851         memcpy(&adapter->curbssparams.ssid, &bss->ssid,
852                sizeof(struct WLAN_802_11_SSID));
853
854         netif_carrier_on(priv->dev);
855         netif_wake_queue(priv->dev);
856
857         netif_carrier_on(priv->mesh_dev);
858         netif_wake_queue(priv->mesh_dev);
859
860         memset(&wrqu, 0, sizeof(wrqu));
861         memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
862         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
863         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
864
865         lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
866         lbs_deb_join("ADHOC_RESP: channel = %d\n", adapter->curbssparams.channel);
867         lbs_deb_join("ADHOC_RESP: BSSID = " MAC_FMT "\n",
868                MAC_ARG(padhocresult->BSSID));
869
870 done:
871         lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
872         return ret;
873 }
874
875 int libertas_ret_80211_ad_hoc_stop(wlan_private * priv,
876                                 struct cmd_ds_command *resp)
877 {
878         lbs_deb_enter(LBS_DEB_JOIN);
879
880         libertas_mac_event_disconnected(priv);
881
882         lbs_deb_leave(LBS_DEB_JOIN);
883         return 0;
884 }