[PATCH] libertas: remove WLAN_802_11_NETWORK_INFRASTRUCTURE enum
[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
11 #include <net/iw_handler.h>
12
13 #include "host.h"
14 #include "decl.h"
15 #include "join.h"
16 #include "dev.h"
17
18 #define AD_HOC_CAP_PRIVACY_ON 1
19
20 /**
21  *  @brief This function finds out the common rates between rate1 and rate2.
22  *
23  * It will fill common rates in rate1 as output if found.
24  *
25  * NOTE: Setting the MSB of the basic rates need to be taken
26  *   care, either before or after calling this function
27  *
28  *  @param adapter     A pointer to wlan_adapter structure
29  *  @param rate1       the buffer which keeps input and output
30  *  @param rate1_size  the size of rate1 buffer
31  *  @param rate2       the buffer which keeps rate2
32  *  @param rate2_size  the size of rate2 buffer.
33  *
34  *  @return            0 or -1
35  */
36 static int get_common_rates(wlan_adapter * adapter, u8 * rate1,
37                             int rate1_size, u8 * rate2, int rate2_size)
38 {
39         u8 *ptr = rate1;
40         int ret = 0;
41         u8 tmp[30];
42         int i;
43
44         memset(&tmp, 0, sizeof(tmp));
45         memcpy(&tmp, rate1, min_t(size_t, rate1_size, sizeof(tmp)));
46         memset(rate1, 0, rate1_size);
47
48         /* Mask the top bit of the original values */
49         for (i = 0; tmp[i] && i < sizeof(tmp); i++)
50                 tmp[i] &= 0x7F;
51
52         for (i = 0; rate2[i] && i < rate2_size; i++) {
53                 /* Check for Card Rate in tmp, excluding the top bit */
54                 if (strchr(tmp, rate2[i] & 0x7F)) {
55                         /* values match, so copy the Card Rate to rate1 */
56                         *rate1++ = rate2[i];
57                 }
58         }
59
60         lbs_dbg_hex("rate1 (AP) rates:", tmp, sizeof(tmp));
61         lbs_dbg_hex("rate2 (Card) rates:", rate2, rate2_size);
62         lbs_dbg_hex("Common rates:", ptr, rate1_size);
63         lbs_pr_debug(1, "Tx datarate is set to 0x%X\n", adapter->datarate);
64
65         if (!adapter->is_datarate_auto) {
66                 while (*ptr) {
67                         if ((*ptr & 0x7f) == adapter->datarate) {
68                                 ret = 0;
69                                 goto done;
70                         }
71                         ptr++;
72                 }
73                 lbs_pr_alert( "Previously set fixed data rate %#x isn't "
74                        "compatible with the network.\n", adapter->datarate);
75
76                 ret = -1;
77                 goto done;
78         }
79
80         ret = 0;
81 done:
82         return ret;
83 }
84
85 int libertas_send_deauth(wlan_private * priv)
86 {
87         wlan_adapter *adapter = priv->adapter;
88         int ret = 0;
89
90         if (adapter->mode == IW_MODE_INFRA &&
91             adapter->connect_status == libertas_connected)
92                 ret = libertas_send_deauthentication(priv);
93         else
94                 ret = -ENOTSUPP;
95
96         return ret;
97 }
98
99 int libertas_do_adhocstop_ioctl(wlan_private * priv)
100 {
101         wlan_adapter *adapter = priv->adapter;
102         int ret = 0;
103
104         if (adapter->mode == IW_MODE_ADHOC &&
105             adapter->connect_status == libertas_connected)
106                 ret = libertas_stop_adhoc_network(priv);
107         else
108                 ret = -ENOTSUPP;
109
110         return ret;
111 }
112
113 /**
114  *  @brief Associate to a specific BSS discovered in a scan
115  *
116  *  @param priv      A pointer to wlan_private structure
117  *  @param pbssdesc  Pointer to the BSS descriptor to associate with.
118  *
119  *  @return          0-success, otherwise fail
120  */
121 int wlan_associate(wlan_private * priv, struct bss_descriptor * pbssdesc)
122 {
123         wlan_adapter *adapter = priv->adapter;
124         int ret;
125
126         ENTER();
127
128         ret = libertas_prepare_and_send_command(priv, cmd_802_11_authenticate,
129                                     0, cmd_option_waitforrsp,
130                                     0, pbssdesc->macaddress);
131
132         if (ret) {
133                 LEAVE();
134                 return ret;
135         }
136
137         /* set preamble to firmware */
138         if (adapter->capinfo.shortpreamble && pbssdesc->cap.shortpreamble)
139                 adapter->preamble = cmd_type_short_preamble;
140         else
141                 adapter->preamble = cmd_type_long_preamble;
142
143         libertas_set_radio_control(priv);
144
145         ret = libertas_prepare_and_send_command(priv, cmd_802_11_associate,
146                                     0, cmd_option_waitforrsp, 0, pbssdesc);
147
148         LEAVE();
149         return ret;
150 }
151
152 /**
153  *  @brief Start an Adhoc Network
154  *
155  *  @param priv         A pointer to wlan_private structure
156  *  @param adhocssid    The ssid of the Adhoc Network
157  *  @return             0--success, -1--fail
158  */
159 int libertas_start_adhoc_network(wlan_private * priv, struct WLAN_802_11_SSID *adhocssid)
160 {
161         wlan_adapter *adapter = priv->adapter;
162         int ret = 0;
163
164         adapter->adhoccreate = 1;
165
166         if (!adapter->capinfo.shortpreamble) {
167                 lbs_pr_debug(1, "AdhocStart: Long preamble\n");
168                 adapter->preamble = cmd_type_long_preamble;
169         } else {
170                 lbs_pr_debug(1, "AdhocStart: Short preamble\n");
171                 adapter->preamble = cmd_type_short_preamble;
172         }
173
174         libertas_set_radio_control(priv);
175
176         lbs_pr_debug(1, "Adhoc channel = %d\n", adapter->adhocchannel);
177         lbs_pr_debug(1, "curbssparams.channel = %d\n",
178                adapter->curbssparams.channel);
179         lbs_pr_debug(1, "curbssparams.band = %d\n", adapter->curbssparams.band);
180
181         ret = libertas_prepare_and_send_command(priv, cmd_802_11_ad_hoc_start,
182                                     0, cmd_option_waitforrsp, 0, adhocssid);
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 bss_descriptor * pbssdesc)
197 {
198         wlan_adapter *adapter = priv->adapter;
199         int ret = 0;
200
201         lbs_pr_debug(1, "libertas_join_adhoc_network: CurBss.ssid =%s\n",
202                adapter->curbssparams.ssid.ssid);
203         lbs_pr_debug(1, "libertas_join_adhoc_network: CurBss.ssid_len =%u\n",
204                adapter->curbssparams.ssid.ssidlength);
205         lbs_pr_debug(1, "libertas_join_adhoc_network: ssid =%s\n", pbssdesc->ssid.ssid);
206         lbs_pr_debug(1, "libertas_join_adhoc_network: ssid len =%u\n",
207                pbssdesc->ssid.ssidlength);
208
209         /* check if the requested SSID is already joined */
210         if (adapter->curbssparams.ssid.ssidlength
211             && !libertas_SSID_cmp(&pbssdesc->ssid, &adapter->curbssparams.ssid)
212             && (adapter->mode == IW_MODE_ADHOC)) {
213
214         lbs_pr_debug(1,
215                        "ADHOC_J_CMD: New ad-hoc SSID is the same as current, "
216                        "not attempting to re-join");
217
218                 return -1;
219         }
220
221         /*Use shortpreamble only when both creator and card supports
222            short preamble */
223         if (!pbssdesc->cap.shortpreamble || !adapter->capinfo.shortpreamble) {
224                 lbs_pr_debug(1, "AdhocJoin: Long preamble\n");
225                 adapter->preamble = cmd_type_long_preamble;
226         } else {
227                 lbs_pr_debug(1, "AdhocJoin: Short preamble\n");
228                 adapter->preamble = cmd_type_short_preamble;
229         }
230
231         libertas_set_radio_control(priv);
232
233         lbs_pr_debug(1, "curbssparams.channel = %d\n",
234                adapter->curbssparams.channel);
235         lbs_pr_debug(1, "curbssparams.band = %c\n", adapter->curbssparams.band);
236
237         adapter->adhoccreate = 0;
238
239         ret = libertas_prepare_and_send_command(priv, cmd_802_11_ad_hoc_join,
240                                     0, cmd_option_waitforrsp,
241                                     OID_802_11_SSID, pbssdesc);
242
243         return ret;
244 }
245
246 int libertas_stop_adhoc_network(wlan_private * priv)
247 {
248         return libertas_prepare_and_send_command(priv, cmd_802_11_ad_hoc_stop,
249                                      0, cmd_option_waitforrsp, 0, NULL);
250 }
251
252 /**
253  *  @brief Send Deauthentication Request
254  *
255  *  @param priv      A pointer to wlan_private structure
256  *  @return          0--success, -1--fail
257  */
258 int libertas_send_deauthentication(wlan_private * priv)
259 {
260         return libertas_prepare_and_send_command(priv, cmd_802_11_deauthenticate,
261                                      0, cmd_option_waitforrsp, 0, NULL);
262 }
263
264 /**
265  *  @brief Set Idle Off
266  *
267  *  @param priv         A pointer to wlan_private structure
268  *  @return             0 --success, otherwise fail
269  */
270 int libertas_idle_off(wlan_private * priv)
271 {
272         wlan_adapter *adapter = priv->adapter;
273         int ret = 0;
274         const u8 zeromac[] = { 0, 0, 0, 0, 0, 0 };
275         int i;
276
277         ENTER();
278
279         if (adapter->connect_status == libertas_disconnected) {
280                 if (adapter->mode == IW_MODE_INFRA) {
281                         if (memcmp(adapter->previousbssid, zeromac,
282                                    sizeof(zeromac)) != 0) {
283
284                                 lbs_pr_debug(1, "Previous SSID = %s\n",
285                                        adapter->previousssid.ssid);
286                                 lbs_pr_debug(1, "Previous BSSID = "
287                                        "%02x:%02x:%02x:%02x:%02x:%02x:\n",
288                                        adapter->previousbssid[0],
289                                        adapter->previousbssid[1],
290                                        adapter->previousbssid[2],
291                                        adapter->previousbssid[3],
292                                        adapter->previousbssid[4],
293                                        adapter->previousbssid[5]);
294
295                                 i = libertas_find_SSID_in_list(adapter,
296                                                    &adapter->previousssid,
297                                                    adapter->previousbssid,
298                                                    adapter->mode);
299
300                                 if (i < 0) {
301                                         libertas_send_specific_BSSID_scan(priv,
302                                                               adapter->
303                                                               previousbssid,
304                                                               1);
305                                         i = libertas_find_SSID_in_list(adapter,
306                                                            &adapter->
307                                                            previousssid,
308                                                            adapter->
309                                                            previousbssid,
310                                                            adapter->mode);
311                                 }
312
313                                 if (i < 0) {
314                                         /* If the BSSID could not be found, try just the SSID */
315                                         i = libertas_find_SSID_in_list(adapter,
316                                                            &adapter->
317                                                            previousssid, NULL,
318                                                            adapter->mode);
319                                 }
320
321                                 if (i < 0) {
322                                         libertas_send_specific_SSID_scan(priv,
323                                                              &adapter->
324                                                              previousssid,
325                                                              1);
326                                         i = libertas_find_SSID_in_list(adapter,
327                                                            &adapter->
328                                                            previousssid, NULL,
329                                                            adapter->mode);
330                                 }
331
332                                 if (i >= 0) {
333                                         ret =
334                                             wlan_associate(priv,
335                                                            &adapter->
336                                                            scantable[i]);
337                                 }
338                         }
339                 } else if (adapter->mode == IW_MODE_ADHOC) {
340                         ret = libertas_prepare_and_send_command(priv,
341                                                     cmd_802_11_ad_hoc_start,
342                                                     0,
343                                                     cmd_option_waitforrsp,
344                                                     0, &adapter->previousssid);
345                 }
346         }
347         /* else it is connected */
348
349         lbs_pr_debug(1, "\nwlanidle is off");
350         LEAVE();
351         return ret;
352 }
353
354 /**
355  *  @brief Set Idle On
356  *
357  *  @param priv         A pointer to wlan_private structure
358  *  @return             0 --success, otherwise fail
359  */
360 int libertas_idle_on(wlan_private * priv)
361 {
362         wlan_adapter *adapter = priv->adapter;
363         int ret = 0;
364
365         if (adapter->connect_status == libertas_connected) {
366                 if (adapter->mode == IW_MODE_INFRA) {
367                         lbs_pr_debug(1, "Previous SSID = %s\n",
368                                adapter->previousssid.ssid);
369                         memmove(&adapter->previousssid,
370                                 &adapter->curbssparams.ssid,
371                                 sizeof(struct WLAN_802_11_SSID));
372                         libertas_send_deauth(priv);
373
374                 } else if (adapter->mode == IW_MODE_ADHOC) {
375                         ret = libertas_stop_adhoc_network(priv);
376                 }
377
378         }
379
380         lbs_pr_debug(1, "\nwlanidle is on");
381
382         return ret;
383 }
384
385 /**
386  *  @brief This function prepares command of authenticate.
387  *
388  *  @param priv      A pointer to wlan_private structure
389  *  @param cmd       A pointer to cmd_ds_command structure
390  *  @param pdata_buf Void cast of pointer to a BSSID to authenticate with
391  *
392  *  @return         0 or -1
393  */
394 int libertas_cmd_80211_authenticate(wlan_private * priv,
395                                  struct cmd_ds_command *cmd,
396                                  void *pdata_buf)
397 {
398         wlan_adapter *adapter = priv->adapter;
399         struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
400         int ret = -1;
401         u8 *bssid = pdata_buf;
402
403         cmd->command = cpu_to_le16(cmd_802_11_authenticate);
404         cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
405                                 + S_DS_GEN);
406
407         /* translate auth mode to 802.11 defined wire value */
408         switch (adapter->secinfo.auth_mode) {
409         case IW_AUTH_ALG_OPEN_SYSTEM:
410                 pauthenticate->authtype = 0x00;
411                 break;
412         case IW_AUTH_ALG_SHARED_KEY:
413                 pauthenticate->authtype = 0x01;
414                 break;
415         case IW_AUTH_ALG_LEAP:
416                 pauthenticate->authtype = 0x80;
417                 break;
418         default:
419                 lbs_pr_debug(1, "AUTH_CMD: invalid auth alg 0x%X\n",
420                              adapter->secinfo.auth_mode);
421                 goto out;
422         }
423
424         memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
425
426         lbs_pr_debug(1, "AUTH_CMD: Bssid is : %x:%x:%x:%x:%x:%x\n",
427                bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
428         ret = 0;
429
430 out:
431         return ret;
432 }
433
434 int libertas_cmd_80211_deauthenticate(wlan_private * priv,
435                                    struct cmd_ds_command *cmd)
436 {
437         wlan_adapter *adapter = priv->adapter;
438         struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
439
440         ENTER();
441
442         cmd->command = cpu_to_le16(cmd_802_11_deauthenticate);
443         cmd->size =
444             cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
445                              S_DS_GEN);
446
447         /* set AP MAC address */
448         memmove(dauth->macaddr, adapter->curbssparams.bssid,
449                 ETH_ALEN);
450
451         /* Reason code 3 = Station is leaving */
452 #define REASON_CODE_STA_LEAVING 3
453         dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
454
455         LEAVE();
456         return 0;
457 }
458
459 int libertas_cmd_80211_associate(wlan_private * priv,
460                               struct cmd_ds_command *cmd, void *pdata_buf)
461 {
462         wlan_adapter *adapter = priv->adapter;
463         struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
464         int ret = 0;
465         struct bss_descriptor *pbssdesc;
466         u8 *card_rates;
467         u8 *pos;
468         int card_rates_size;
469         u16 tmpcap;
470         struct mrvlietypes_ssidparamset *ssid;
471         struct mrvlietypes_phyparamset *phy;
472         struct mrvlietypes_ssparamset *ss;
473         struct mrvlietypes_ratesparamset *rates;
474         struct mrvlietypes_rsnparamset *rsn;
475
476         ENTER();
477
478         pbssdesc = pdata_buf;
479         pos = (u8 *) passo;
480
481         if (!adapter) {
482                 ret = -1;
483                 goto done;
484         }
485
486         cmd->command = cpu_to_le16(cmd_802_11_associate);
487
488         /* Save so we know which BSS Desc to use in the response handler */
489         adapter->pattemptedbssdesc = pbssdesc;
490
491         memcpy(passo->peerstaaddr,
492                pbssdesc->macaddress, sizeof(passo->peerstaaddr));
493         pos += sizeof(passo->peerstaaddr);
494
495         /* set the listen interval */
496         passo->listeninterval = adapter->listeninterval;
497
498         pos += sizeof(passo->capinfo);
499         pos += sizeof(passo->listeninterval);
500         pos += sizeof(passo->bcnperiod);
501         pos += sizeof(passo->dtimperiod);
502
503         ssid = (struct mrvlietypes_ssidparamset *) pos;
504         ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
505         ssid->header.len = pbssdesc->ssid.ssidlength;
506         memcpy(ssid->ssid, pbssdesc->ssid.ssid, ssid->header.len);
507         pos += sizeof(ssid->header) + ssid->header.len;
508         ssid->header.len = cpu_to_le16(ssid->header.len);
509
510         phy = (struct mrvlietypes_phyparamset *) pos;
511         phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
512         phy->header.len = sizeof(phy->fh_ds.dsparamset);
513         memcpy(&phy->fh_ds.dsparamset,
514                &pbssdesc->phyparamset.dsparamset.currentchan,
515                sizeof(phy->fh_ds.dsparamset));
516         pos += sizeof(phy->header) + phy->header.len;
517         phy->header.len = cpu_to_le16(phy->header.len);
518
519         ss = (struct mrvlietypes_ssparamset *) pos;
520         ss->header.type = cpu_to_le16(TLV_TYPE_CF);
521         ss->header.len = sizeof(ss->cf_ibss.cfparamset);
522         pos += sizeof(ss->header) + ss->header.len;
523         ss->header.len = cpu_to_le16(ss->header.len);
524
525         rates = (struct mrvlietypes_ratesparamset *) pos;
526         rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
527
528         memcpy(&rates->rates, &pbssdesc->libertas_supported_rates, WLAN_SUPPORTED_RATES);
529
530         card_rates = libertas_supported_rates;
531         card_rates_size = sizeof(libertas_supported_rates);
532
533         if (get_common_rates(adapter, rates->rates, WLAN_SUPPORTED_RATES,
534                              card_rates, card_rates_size)) {
535                 ret = -1;
536                 goto done;
537         }
538
539         rates->header.len = min_t(size_t, strlen(rates->rates), WLAN_SUPPORTED_RATES);
540         adapter->curbssparams.numofrates = rates->header.len;
541
542         pos += sizeof(rates->header) + rates->header.len;
543         rates->header.len = cpu_to_le16(rates->header.len);
544
545         if (adapter->secinfo.WPAenabled || adapter->secinfo.WPA2enabled) {
546                 rsn = (struct mrvlietypes_rsnparamset *) pos;
547                 rsn->header.type = (u16) adapter->wpa_ie[0];    /* WPA_IE or WPA2_IE */
548                 rsn->header.type = cpu_to_le16(rsn->header.type);
549                 rsn->header.len = (u16) adapter->wpa_ie[1];
550                 memcpy(rsn->rsnie, &adapter->wpa_ie[2], rsn->header.len);
551                 lbs_dbg_hex("ASSOC_CMD: RSN IE", (u8 *) rsn,
552                         sizeof(rsn->header) + rsn->header.len);
553                 pos += sizeof(rsn->header) + rsn->header.len;
554                 rsn->header.len = cpu_to_le16(rsn->header.len);
555         }
556
557         /* update curbssparams */
558         adapter->curbssparams.channel =
559             (pbssdesc->phyparamset.dsparamset.currentchan);
560
561         /* Copy the infra. association rates into Current BSS state structure */
562         memcpy(&adapter->curbssparams.datarates, &rates->rates,
563                min_t(size_t, sizeof(adapter->curbssparams.datarates), rates->header.len));
564
565         lbs_pr_debug(1, "ASSOC_CMD: rates->header.len = %d\n", rates->header.len);
566
567         /* set IBSS field */
568         if (pbssdesc->mode == IW_MODE_INFRA) {
569 #define CAPINFO_ESS_MODE 1
570                 passo->capinfo.ess = CAPINFO_ESS_MODE;
571         }
572
573         if (libertas_parse_dnld_countryinfo_11d(priv)) {
574                 ret = -1;
575                 goto done;
576         }
577
578         cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
579
580         /* set the capability info at last */
581         memcpy(&tmpcap, &pbssdesc->cap, sizeof(passo->capinfo));
582         tmpcap &= CAPINFO_MASK;
583         lbs_pr_debug(1, "ASSOC_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
584                tmpcap, CAPINFO_MASK);
585         tmpcap = cpu_to_le16(tmpcap);
586         memcpy(&passo->capinfo, &tmpcap, sizeof(passo->capinfo));
587
588       done:
589         LEAVE();
590         return ret;
591 }
592
593 int libertas_cmd_80211_ad_hoc_start(wlan_private * priv,
594                                  struct cmd_ds_command *cmd, void *pssid)
595 {
596         wlan_adapter *adapter = priv->adapter;
597         struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
598         int ret = 0;
599         int cmdappendsize = 0;
600         int i;
601         u16 tmpcap;
602         struct bss_descriptor *pbssdesc;
603         struct WLAN_802_11_SSID *ssid = pssid;
604
605         ENTER();
606
607         if (!adapter) {
608                 ret = -1;
609                 goto done;
610         }
611
612         cmd->command = cpu_to_le16(cmd_802_11_ad_hoc_start);
613
614         pbssdesc = &adapter->curbssparams.bssdescriptor;
615         adapter->pattemptedbssdesc = pbssdesc;
616
617         /*
618          * Fill in the parameters for 2 data structures:
619          *   1. cmd_ds_802_11_ad_hoc_start command
620          *   2. adapter->scantable[i]
621          *
622          * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
623          *   probe delay, and cap info.
624          *
625          * Firmware will fill up beacon period, DTIM, Basic rates
626          *   and operational rates.
627          */
628
629         memset(adhs->SSID, 0, IW_ESSID_MAX_SIZE);
630
631         memcpy(adhs->SSID, ssid->ssid, ssid->ssidlength);
632
633         lbs_pr_debug(1, "ADHOC_S_CMD: SSID = %s\n", adhs->SSID);
634
635         memset(pbssdesc->ssid.ssid, 0, IW_ESSID_MAX_SIZE);
636         memcpy(pbssdesc->ssid.ssid, ssid->ssid, ssid->ssidlength);
637
638         pbssdesc->ssid.ssidlength = ssid->ssidlength;
639
640         /* set the BSS type */
641         adhs->bsstype = cmd_bss_type_ibss;
642         pbssdesc->mode = IW_MODE_ADHOC;
643         adhs->beaconperiod = adapter->beaconperiod;
644
645         /* set Physical param set */
646 #define DS_PARA_IE_ID   3
647 #define DS_PARA_IE_LEN  1
648
649         adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
650         adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
651
652         WARN_ON(!adapter->adhocchannel);
653
654         lbs_pr_debug(1, "ADHOC_S_CMD: Creating ADHOC on channel %d\n",
655                adapter->adhocchannel);
656
657         adapter->curbssparams.channel = adapter->adhocchannel;
658
659         pbssdesc->channel = adapter->adhocchannel;
660         adhs->phyparamset.dsparamset.currentchan = adapter->adhocchannel;
661
662         memcpy(&pbssdesc->phyparamset,
663                &adhs->phyparamset, sizeof(union ieeetypes_phyparamset));
664
665         /* set IBSS param set */
666 #define IBSS_PARA_IE_ID   6
667 #define IBSS_PARA_IE_LEN  2
668
669         adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
670         adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
671         adhs->ssparamset.ibssparamset.atimwindow = adapter->atimwindow;
672         memcpy(&pbssdesc->ssparamset,
673                &adhs->ssparamset, sizeof(union IEEEtypes_ssparamset));
674
675         /* set capability info */
676         adhs->cap.ess = 0;
677         adhs->cap.ibss = 1;
678         pbssdesc->cap.ibss = 1;
679
680         /* probedelay */
681         adhs->probedelay = cpu_to_le16(cmd_scan_probe_delay_time);
682
683         /* set up privacy in adapter->scantable[i] */
684         if (adapter->secinfo.wep_enabled) {
685                 lbs_pr_debug(1, "ADHOC_S_CMD: WEP enabled, setting privacy on\n");
686                 pbssdesc->privacy = wlan802_11privfilter8021xWEP;
687                 adhs->cap.privacy = AD_HOC_CAP_PRIVACY_ON;
688         } else {
689                 lbs_pr_debug(1, "ADHOC_S_CMD: WEP disabled, setting privacy off\n");
690                 pbssdesc->privacy = wlan802_11privfilteracceptall;
691         }
692
693         memset(adhs->datarate, 0, sizeof(adhs->datarate));
694
695         if (adapter->adhoc_grate_enabled) {
696                 memcpy(adhs->datarate, libertas_adhoc_rates_g,
697                        min(sizeof(adhs->datarate), sizeof(libertas_adhoc_rates_g)));
698         } else {
699                 memcpy(adhs->datarate, libertas_adhoc_rates_b,
700                        min(sizeof(adhs->datarate), sizeof(libertas_adhoc_rates_b)));
701         }
702
703         /* Find the last non zero */
704         for (i = 0; i < sizeof(adhs->datarate) && adhs->datarate[i]; i++) ;
705
706         adapter->curbssparams.numofrates = i;
707
708         /* Copy the ad-hoc creating rates into Current BSS state structure */
709         memcpy(&adapter->curbssparams.datarates,
710                &adhs->datarate, adapter->curbssparams.numofrates);
711
712         lbs_pr_debug(1, "ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
713                adhs->datarate[0], adhs->datarate[1],
714                adhs->datarate[2], adhs->datarate[3]);
715
716         lbs_pr_debug(1, "ADHOC_S_CMD: AD HOC Start command is ready\n");
717
718         if (libertas_create_dnld_countryinfo_11d(priv)) {
719                 lbs_pr_debug(1, "ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
720                 ret = -1;
721                 goto done;
722         }
723
724         cmd->size =
725             cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start)
726                              + S_DS_GEN + cmdappendsize);
727
728         memcpy(&tmpcap, &adhs->cap, sizeof(u16));
729         tmpcap = cpu_to_le16(tmpcap);
730         memcpy(&adhs->cap, &tmpcap, sizeof(u16));
731
732         ret = 0;
733 done:
734         LEAVE();
735         return ret;
736 }
737
738 int libertas_cmd_80211_ad_hoc_stop(wlan_private * priv,
739                                 struct cmd_ds_command *cmd)
740 {
741         cmd->command = cpu_to_le16(cmd_802_11_ad_hoc_stop);
742         cmd->size = cpu_to_le16(S_DS_GEN);
743
744         return 0;
745 }
746
747 int libertas_cmd_80211_ad_hoc_join(wlan_private * priv,
748                                 struct cmd_ds_command *cmd, void *pdata_buf)
749 {
750         wlan_adapter *adapter = priv->adapter;
751         struct cmd_ds_802_11_ad_hoc_join *padhocjoin = &cmd->params.adj;
752         struct bss_descriptor *pbssdesc = pdata_buf;
753         int cmdappendsize = 0;
754         int ret = 0;
755         u8 *card_rates;
756         int card_rates_size;
757         u16 tmpcap;
758         int i;
759
760         ENTER();
761
762         adapter->pattemptedbssdesc = pbssdesc;
763
764         cmd->command = cpu_to_le16(cmd_802_11_ad_hoc_join);
765
766         padhocjoin->bssdescriptor.bsstype = cmd_bss_type_ibss;
767
768         padhocjoin->bssdescriptor.beaconperiod = pbssdesc->beaconperiod;
769
770         memcpy(&padhocjoin->bssdescriptor.BSSID,
771                &pbssdesc->macaddress, ETH_ALEN);
772
773         memcpy(&padhocjoin->bssdescriptor.SSID,
774                &pbssdesc->ssid.ssid, pbssdesc->ssid.ssidlength);
775
776         memcpy(&padhocjoin->bssdescriptor.phyparamset,
777                &pbssdesc->phyparamset, sizeof(union ieeetypes_phyparamset));
778
779         memcpy(&padhocjoin->bssdescriptor.ssparamset,
780                &pbssdesc->ssparamset, sizeof(union IEEEtypes_ssparamset));
781
782         memcpy(&tmpcap, &pbssdesc->cap, sizeof(struct ieeetypes_capinfo));
783         tmpcap &= CAPINFO_MASK;
784
785         lbs_pr_debug(1, "ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
786                tmpcap, CAPINFO_MASK);
787         memcpy(&padhocjoin->bssdescriptor.cap, &tmpcap,
788                sizeof(struct ieeetypes_capinfo));
789
790         /* information on BSSID descriptor passed to FW */
791     lbs_pr_debug(1,
792                "ADHOC_J_CMD: BSSID = %2x-%2x-%2x-%2x-%2x-%2x, SSID = %s\n",
793                padhocjoin->bssdescriptor.BSSID[0],
794                padhocjoin->bssdescriptor.BSSID[1],
795                padhocjoin->bssdescriptor.BSSID[2],
796                padhocjoin->bssdescriptor.BSSID[3],
797                padhocjoin->bssdescriptor.BSSID[4],
798                padhocjoin->bssdescriptor.BSSID[5],
799                padhocjoin->bssdescriptor.SSID);
800
801         lbs_pr_debug(1, "ADHOC_J_CMD: Data Rate = %x\n",
802                (u32) padhocjoin->bssdescriptor.datarates);
803
804         /* failtimeout */
805         padhocjoin->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
806
807         /* probedelay */
808         padhocjoin->probedelay =
809             cpu_to_le16(cmd_scan_probe_delay_time);
810
811         /* Copy Data rates from the rates recorded in scan response */
812         memset(padhocjoin->bssdescriptor.datarates, 0,
813                sizeof(padhocjoin->bssdescriptor.datarates));
814         memcpy(padhocjoin->bssdescriptor.datarates, pbssdesc->datarates,
815                min(sizeof(padhocjoin->bssdescriptor.datarates),
816                    sizeof(pbssdesc->datarates)));
817
818         card_rates = libertas_supported_rates;
819         card_rates_size = sizeof(libertas_supported_rates);
820
821         adapter->curbssparams.channel = pbssdesc->channel;
822
823         if (get_common_rates(adapter, padhocjoin->bssdescriptor.datarates,
824                              sizeof(padhocjoin->bssdescriptor.datarates),
825                              card_rates, card_rates_size)) {
826                 lbs_pr_debug(1, "ADHOC_J_CMD: get_common_rates returns error.\n");
827                 ret = -1;
828                 goto done;
829         }
830
831         /* Find the last non zero */
832         for (i = 0; i < sizeof(padhocjoin->bssdescriptor.datarates)
833              && padhocjoin->bssdescriptor.datarates[i]; i++) ;
834
835         adapter->curbssparams.numofrates = i;
836
837         /*
838          * Copy the adhoc joining rates to Current BSS State structure
839          */
840         memcpy(adapter->curbssparams.datarates,
841                padhocjoin->bssdescriptor.datarates,
842                adapter->curbssparams.numofrates);
843
844         padhocjoin->bssdescriptor.ssparamset.ibssparamset.atimwindow =
845             cpu_to_le16(pbssdesc->atimwindow);
846
847         if (adapter->secinfo.wep_enabled) {
848                 padhocjoin->bssdescriptor.cap.privacy = AD_HOC_CAP_PRIVACY_ON;
849         }
850
851         if (adapter->psmode == wlan802_11powermodemax_psp) {
852                 /* wake up first */
853                 enum WLAN_802_11_POWER_MODE Localpsmode;
854
855                 Localpsmode = wlan802_11powermodecam;
856                 ret = libertas_prepare_and_send_command(priv,
857                                             cmd_802_11_ps_mode,
858                                             cmd_act_set,
859                                             0, 0, &Localpsmode);
860
861                 if (ret) {
862                         ret = -1;
863                         goto done;
864                 }
865         }
866
867         if (libertas_parse_dnld_countryinfo_11d(priv)) {
868                 ret = -1;
869                 goto done;
870         }
871
872         cmd->size =
873             cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join)
874                              + S_DS_GEN + cmdappendsize);
875
876         memcpy(&tmpcap, &padhocjoin->bssdescriptor.cap,
877                sizeof(struct ieeetypes_capinfo));
878         tmpcap = cpu_to_le16(tmpcap);
879
880         memcpy(&padhocjoin->bssdescriptor.cap,
881                &tmpcap, sizeof(struct ieeetypes_capinfo));
882
883       done:
884         LEAVE();
885         return ret;
886 }
887
888 int libertas_ret_80211_associate(wlan_private * priv,
889                               struct cmd_ds_command *resp)
890 {
891         wlan_adapter *adapter = priv->adapter;
892         int ret = 0;
893         union iwreq_data wrqu;
894         struct ieeetypes_assocrsp *passocrsp;
895         struct bss_descriptor *pbssdesc;
896
897         ENTER();
898
899         passocrsp = (struct ieeetypes_assocrsp *) & resp->params;
900
901         if (passocrsp->statuscode) {
902
903                 libertas_mac_event_disconnected(priv);
904
905         lbs_pr_debug(1,
906                        "ASSOC_RESP: Association failed, status code = %d\n",
907                        passocrsp->statuscode);
908
909                 ret = -1;
910                 goto done;
911         }
912
913         lbs_dbg_hex("ASSOC_RESP:", (void *)&resp->params,
914                 le16_to_cpu(resp->size) - S_DS_GEN);
915
916         /* Send a Media Connected event, according to the Spec */
917         adapter->connect_status = libertas_connected;
918
919         /* Set the attempted BSSID Index to current */
920         pbssdesc = adapter->pattemptedbssdesc;
921
922         lbs_pr_debug(1, "ASSOC_RESP: %s\n", pbssdesc->ssid.ssid);
923
924         /* Set the new SSID to current SSID */
925         memcpy(&adapter->curbssparams.ssid,
926                &pbssdesc->ssid, sizeof(struct WLAN_802_11_SSID));
927
928         /* Set the new BSSID (AP's MAC address) to current BSSID */
929         memcpy(adapter->curbssparams.bssid,
930                pbssdesc->macaddress, ETH_ALEN);
931
932         /* Make a copy of current BSSID descriptor */
933         memcpy(&adapter->curbssparams.bssdescriptor,
934                pbssdesc, sizeof(struct bss_descriptor));
935
936         lbs_pr_debug(1, "ASSOC_RESP: currentpacketfilter is %x\n",
937                adapter->currentpacketfilter);
938
939         adapter->SNR[TYPE_RXPD][TYPE_AVG] = 0;
940         adapter->NF[TYPE_RXPD][TYPE_AVG] = 0;
941
942         memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
943         memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
944         adapter->nextSNRNF = 0;
945         adapter->numSNRNF = 0;
946
947         netif_carrier_on(priv->wlan_dev.netdev);
948         netif_wake_queue(priv->wlan_dev.netdev);
949
950         lbs_pr_debug(1, "ASSOC_RESP: Associated \n");
951
952         memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
953         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
954         wireless_send_event(priv->wlan_dev.netdev, SIOCGIWAP, &wrqu, NULL);
955
956       done:
957         LEAVE();
958         return ret;
959 }
960
961 int libertas_ret_80211_disassociate(wlan_private * priv,
962                                  struct cmd_ds_command *resp)
963 {
964         ENTER();
965
966         libertas_mac_event_disconnected(priv);
967
968         LEAVE();
969         return 0;
970 }
971
972 int libertas_ret_80211_ad_hoc_start(wlan_private * priv,
973                                  struct cmd_ds_command *resp)
974 {
975         wlan_adapter *adapter = priv->adapter;
976         int ret = 0;
977         u16 command = le16_to_cpu(resp->command);
978         u16 result = le16_to_cpu(resp->result);
979         struct cmd_ds_802_11_ad_hoc_result *padhocresult;
980         union iwreq_data wrqu;
981         struct bss_descriptor *pbssdesc;
982
983         ENTER();
984
985         padhocresult = &resp->params.result;
986
987         lbs_pr_debug(1, "ADHOC_S_RESP: size = %d\n", le16_to_cpu(resp->size));
988         lbs_pr_debug(1, "ADHOC_S_RESP: command = %x\n", command);
989         lbs_pr_debug(1, "ADHOC_S_RESP: result = %x\n", result);
990
991         pbssdesc = adapter->pattemptedbssdesc;
992
993         /*
994          * Join result code 0 --> SUCCESS
995          */
996         if (result) {
997                 lbs_pr_debug(1, "ADHOC_RESP failed\n");
998                 if (adapter->connect_status == libertas_connected) {
999                         libertas_mac_event_disconnected(priv);
1000                 }
1001
1002                 memset(&adapter->curbssparams.bssdescriptor,
1003                        0x00, sizeof(adapter->curbssparams.bssdescriptor));
1004
1005                 LEAVE();
1006                 return -1;
1007         }
1008
1009         /*
1010          * Now the join cmd should be successful
1011          * If BSSID has changed use SSID to compare instead of BSSID
1012          */
1013         lbs_pr_debug(1, "ADHOC_J_RESP  %s\n", pbssdesc->ssid.ssid);
1014
1015         /* Send a Media Connected event, according to the Spec */
1016         adapter->connect_status = libertas_connected;
1017
1018         if (command == cmd_ret_802_11_ad_hoc_start) {
1019                 /* Update the created network descriptor with the new BSSID */
1020                 memcpy(pbssdesc->macaddress,
1021                        padhocresult->BSSID, ETH_ALEN);
1022         } else {
1023
1024                 /* Make a copy of current BSSID descriptor, only needed for join since
1025                  *   the current descriptor is already being used for adhoc start
1026                  */
1027                 memmove(&adapter->curbssparams.bssdescriptor,
1028                         pbssdesc, sizeof(struct bss_descriptor));
1029         }
1030
1031         /* Set the BSSID from the joined/started descriptor */
1032         memcpy(&adapter->curbssparams.bssid,
1033                pbssdesc->macaddress, ETH_ALEN);
1034
1035         /* Set the new SSID to current SSID */
1036         memcpy(&adapter->curbssparams.ssid,
1037                &pbssdesc->ssid, sizeof(struct WLAN_802_11_SSID));
1038
1039         netif_carrier_on(priv->wlan_dev.netdev);
1040         netif_wake_queue(priv->wlan_dev.netdev);
1041
1042         memset(&wrqu, 0, sizeof(wrqu));
1043         memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
1044         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1045         wireless_send_event(priv->wlan_dev.netdev, SIOCGIWAP, &wrqu, NULL);
1046
1047         lbs_pr_debug(1, "ADHOC_RESP: - Joined/Started Ad Hoc\n");
1048         lbs_pr_debug(1, "ADHOC_RESP: channel = %d\n", adapter->adhocchannel);
1049         lbs_pr_debug(1, "ADHOC_RESP: BSSID = %02x:%02x:%02x:%02x:%02x:%02x\n",
1050                padhocresult->BSSID[0], padhocresult->BSSID[1],
1051                padhocresult->BSSID[2], padhocresult->BSSID[3],
1052                padhocresult->BSSID[4], padhocresult->BSSID[5]);
1053
1054         LEAVE();
1055         return ret;
1056 }
1057
1058 int libertas_ret_80211_ad_hoc_stop(wlan_private * priv,
1059                                 struct cmd_ds_command *resp)
1060 {
1061         ENTER();
1062
1063         libertas_mac_event_disconnected(priv);
1064
1065         LEAVE();
1066         return 0;
1067 }