wireless: mwifiex: initial commit for Marvell mwifiex driver
[pandora-kernel.git] / drivers / net / wireless / mwifiex / join.c
1 /*
2  * Marvell Wireless LAN device driver: association and ad-hoc start/join
3  *
4  * Copyright (C) 2011, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19
20 #include "decl.h"
21 #include "ioctl.h"
22 #include "util.h"
23 #include "fw.h"
24 #include "main.h"
25 #include "wmm.h"
26 #include "11n.h"
27
28 #define CAPINFO_MASK    (~(BIT(15) | BIT(14) | BIT(12) | BIT(11) | BIT(9)))
29
30 /*
31  * Append a generic IE as a pass through TLV to a TLV buffer.
32  *
33  * This function is called from the network join command preparation routine.
34  *
35  * If the IE buffer has been setup by the application, this routine appends
36  * the buffer as a pass through TLV type to the request.
37  */
38 static int
39 mwifiex_cmd_append_generic_ie(struct mwifiex_private *priv, u8 **buffer)
40 {
41         int ret_len = 0;
42         struct mwifiex_ie_types_header ie_header;
43
44         /* Null Checks */
45         if (!buffer)
46                 return 0;
47         if (!(*buffer))
48                 return 0;
49
50         /*
51          * If there is a generic ie buffer setup, append it to the return
52          *   parameter buffer pointer.
53          */
54         if (priv->gen_ie_buf_len) {
55                 dev_dbg(priv->adapter->dev, "info: %s: append generic %d to %p\n",
56                                 __func__, priv->gen_ie_buf_len, *buffer);
57
58                 /* Wrap the generic IE buffer with a pass through TLV type */
59                 ie_header.type = cpu_to_le16(TLV_TYPE_PASSTHROUGH);
60                 ie_header.len = cpu_to_le16(priv->gen_ie_buf_len);
61                 memcpy(*buffer, &ie_header, sizeof(ie_header));
62
63                 /* Increment the return size and the return buffer pointer
64                    param */
65                 *buffer += sizeof(ie_header);
66                 ret_len += sizeof(ie_header);
67
68                 /* Copy the generic IE buffer to the output buffer, advance
69                    pointer */
70                 memcpy(*buffer, priv->gen_ie_buf, priv->gen_ie_buf_len);
71
72                 /* Increment the return size and the return buffer pointer
73                    param */
74                 *buffer += priv->gen_ie_buf_len;
75                 ret_len += priv->gen_ie_buf_len;
76
77                 /* Reset the generic IE buffer */
78                 priv->gen_ie_buf_len = 0;
79         }
80
81         /* return the length appended to the buffer */
82         return ret_len;
83 }
84
85 /*
86  * Append TSF tracking info from the scan table for the target AP.
87  *
88  * This function is called from the network join command preparation routine.
89  *
90  * The TSF table TSF sent to the firmware contains two TSF values:
91  *      - The TSF of the target AP from its previous beacon/probe response
92  *      - The TSF timestamp of our local MAC at the time we observed the
93  *        beacon/probe response.
94  *
95  * The firmware uses the timestamp values to set an initial TSF value
96  * in the MAC for the new association after a reassociation attempt.
97  */
98 static int
99 mwifiex_cmd_append_tsf_tlv(struct mwifiex_private *priv, u8 **buffer,
100                            struct mwifiex_bssdescriptor *bss_desc)
101 {
102         struct mwifiex_ie_types_tsf_timestamp tsf_tlv;
103         long long tsf_val;
104
105         /* Null Checks */
106         if (buffer == NULL)
107                 return 0;
108         if (*buffer == NULL)
109                 return 0;
110
111         memset(&tsf_tlv, 0x00, sizeof(struct mwifiex_ie_types_tsf_timestamp));
112
113         tsf_tlv.header.type = cpu_to_le16(TLV_TYPE_TSFTIMESTAMP);
114         tsf_tlv.header.len = cpu_to_le16(2 * sizeof(tsf_val));
115
116         memcpy(*buffer, &tsf_tlv, sizeof(tsf_tlv.header));
117         *buffer += sizeof(tsf_tlv.header);
118
119         memcpy(*buffer, &tsf_val, sizeof(tsf_val));
120         *buffer += sizeof(tsf_val);
121
122         memcpy(&tsf_val, bss_desc->time_stamp, sizeof(tsf_val));
123
124         dev_dbg(priv->adapter->dev, "info: %s: TSF offset calc: %016llx - "
125                         "%016llx\n", __func__, tsf_val, bss_desc->network_tsf);
126
127         memcpy(*buffer, &tsf_val, sizeof(tsf_val));
128         *buffer += sizeof(tsf_val);
129
130         return sizeof(tsf_tlv.header) + (2 * sizeof(tsf_val));
131 }
132
133 /*
134  * This function finds out the common rates between rate1 and rate2.
135  *
136  * It will fill common rates in rate1 as output if found.
137  *
138  * NOTE: Setting the MSB of the basic rates needs to be taken
139  * care of, either before or after calling this function.
140  */
141 static int mwifiex_get_common_rates(struct mwifiex_private *priv, u8 *rate1,
142                                     u32 rate1_size, u8 *rate2, u32 rate2_size)
143 {
144         int ret = 0;
145         u8 *ptr = rate1;
146         u8 *tmp = NULL;
147         u32 i, j;
148
149         tmp = kmalloc(rate1_size, GFP_KERNEL);
150         if (!tmp) {
151                 dev_err(priv->adapter->dev, "failed to alloc tmp buf\n");
152                 return -ENOMEM;
153         }
154
155         memcpy(tmp, rate1, rate1_size);
156         memset(rate1, 0, rate1_size);
157
158         for (i = 0; rate2[i] && i < rate2_size; i++) {
159                 for (j = 0; tmp[j] && j < rate1_size; j++) {
160                         /* Check common rate, excluding the bit for
161                            basic rate */
162                         if ((rate2[i] & 0x7F) == (tmp[j] & 0x7F)) {
163                                 *rate1++ = tmp[j];
164                                 break;
165                         }
166                 }
167         }
168
169         dev_dbg(priv->adapter->dev, "info: Tx data rate set to %#x\n",
170                                                 priv->data_rate);
171
172         if (!priv->is_data_rate_auto) {
173                 while (*ptr) {
174                         if ((*ptr & 0x7f) == priv->data_rate) {
175                                 ret = 0;
176                                 goto done;
177                         }
178                         ptr++;
179                 }
180                 dev_err(priv->adapter->dev, "previously set fixed data rate %#x"
181                         " is not compatible with the network\n",
182                         priv->data_rate);
183
184                 ret = -1;
185                 goto done;
186         }
187
188         ret = 0;
189 done:
190         kfree(tmp);
191         return ret;
192 }
193
194 /*
195  * This function creates the intersection of the rates supported by a
196  * target BSS and our adapter settings for use in an assoc/join command.
197  */
198 static int
199 mwifiex_setup_rates_from_bssdesc(struct mwifiex_private *priv,
200                                  struct mwifiex_bssdescriptor *bss_desc,
201                                  u8 *out_rates, u32 *out_rates_size)
202 {
203         u8 card_rates[MWIFIEX_SUPPORTED_RATES];
204         u32 card_rates_size = 0;
205
206         /* Copy AP supported rates */
207         memcpy(out_rates, bss_desc->supported_rates, MWIFIEX_SUPPORTED_RATES);
208         /* Get the STA supported rates */
209         card_rates_size = mwifiex_get_active_data_rates(priv, card_rates);
210         /* Get the common rates between AP and STA supported rates */
211         if (mwifiex_get_common_rates(priv, out_rates, MWIFIEX_SUPPORTED_RATES,
212                                      card_rates, card_rates_size)) {
213                 *out_rates_size = 0;
214                 dev_err(priv->adapter->dev, "%s: cannot get common rates\n",
215                                                 __func__);
216                 return -1;
217         }
218
219         *out_rates_size =
220                 min_t(size_t, strlen(out_rates), MWIFIEX_SUPPORTED_RATES);
221
222         return 0;
223 }
224
225 /*
226  * This function updates the scan entry TSF timestamps to reflect
227  * a new association.
228  */
229 static void
230 mwifiex_update_tsf_timestamps(struct mwifiex_private *priv,
231                               struct mwifiex_bssdescriptor *new_bss_desc)
232 {
233         struct mwifiex_adapter *adapter = priv->adapter;
234         u32 table_idx;
235         long long new_tsf_base;
236         signed long long tsf_delta;
237
238         memcpy(&new_tsf_base, new_bss_desc->time_stamp, sizeof(new_tsf_base));
239
240         tsf_delta = new_tsf_base - new_bss_desc->network_tsf;
241
242         dev_dbg(adapter->dev, "info: TSF: update TSF timestamps, "
243                 "0x%016llx -> 0x%016llx\n",
244                new_bss_desc->network_tsf, new_tsf_base);
245
246         for (table_idx = 0; table_idx < adapter->num_in_scan_table;
247              table_idx++)
248                 adapter->scan_table[table_idx].network_tsf += tsf_delta;
249 }
250
251 /*
252  * This function appends a WAPI IE.
253  *
254  * This function is called from the network join command preparation routine.
255  *
256  * If the IE buffer has been setup by the application, this routine appends
257  * the buffer as a WAPI TLV type to the request.
258  */
259 static int
260 mwifiex_cmd_append_wapi_ie(struct mwifiex_private *priv, u8 **buffer)
261 {
262         int retLen = 0;
263         struct mwifiex_ie_types_header ie_header;
264
265         /* Null Checks */
266         if (buffer == NULL)
267                 return 0;
268         if (*buffer == NULL)
269                 return 0;
270
271         /*
272          * If there is a wapi ie buffer setup, append it to the return
273          *   parameter buffer pointer.
274          */
275         if (priv->wapi_ie_len) {
276                 dev_dbg(priv->adapter->dev, "cmd: append wapi ie %d to %p\n",
277                                 priv->wapi_ie_len, *buffer);
278
279                 /* Wrap the generic IE buffer with a pass through TLV type */
280                 ie_header.type = cpu_to_le16(TLV_TYPE_WAPI_IE);
281                 ie_header.len = cpu_to_le16(priv->wapi_ie_len);
282                 memcpy(*buffer, &ie_header, sizeof(ie_header));
283
284                 /* Increment the return size and the return buffer pointer
285                    param */
286                 *buffer += sizeof(ie_header);
287                 retLen += sizeof(ie_header);
288
289                 /* Copy the wapi IE buffer to the output buffer, advance
290                    pointer */
291                 memcpy(*buffer, priv->wapi_ie, priv->wapi_ie_len);
292
293                 /* Increment the return size and the return buffer pointer
294                    param */
295                 *buffer += priv->wapi_ie_len;
296                 retLen += priv->wapi_ie_len;
297
298         }
299         /* return the length appended to the buffer */
300         return retLen;
301 }
302
303 /*
304  * This function appends rsn ie tlv for wpa/wpa2 security modes.
305  * It is called from the network join command preparation routine.
306  */
307 static int mwifiex_append_rsn_ie_wpa_wpa2(struct mwifiex_private *priv,
308                                           u8 **buffer)
309 {
310         struct mwifiex_ie_types_rsn_param_set *rsn_ie_tlv;
311         int rsn_ie_len;
312
313         if (!buffer || !(*buffer))
314                 return 0;
315
316         rsn_ie_tlv = (struct mwifiex_ie_types_rsn_param_set *) (*buffer);
317         rsn_ie_tlv->header.type = cpu_to_le16((u16) priv->wpa_ie[0]);
318         rsn_ie_tlv->header.type = cpu_to_le16(
319                                  le16_to_cpu(rsn_ie_tlv->header.type) & 0x00FF);
320         rsn_ie_tlv->header.len = cpu_to_le16((u16) priv->wpa_ie[1]);
321         rsn_ie_tlv->header.len = cpu_to_le16(le16_to_cpu(rsn_ie_tlv->header.len)
322                                                         & 0x00FF);
323         if (le16_to_cpu(rsn_ie_tlv->header.len) <= (sizeof(priv->wpa_ie) - 2))
324                 memcpy(rsn_ie_tlv->rsn_ie, &priv->wpa_ie[2],
325                                         le16_to_cpu(rsn_ie_tlv->header.len));
326         else
327                 return -1;
328
329         rsn_ie_len = sizeof(rsn_ie_tlv->header) +
330                                         le16_to_cpu(rsn_ie_tlv->header.len);
331         *buffer += rsn_ie_len;
332
333         return rsn_ie_len;
334 }
335
336 /*
337  * This function prepares command for association.
338  *
339  * This sets the following parameters -
340  *      - Peer MAC address
341  *      - Listen interval
342  *      - Beacon interval
343  *      - Capability information
344  *
345  * ...and the following TLVs, as required -
346  *      - SSID TLV
347  *      - PHY TLV
348  *      - SS TLV
349  *      - Rates TLV
350  *      - Authentication TLV
351  *      - Channel TLV
352  *      - WPA/WPA2 IE
353  *      - 11n TLV
354  *      - Vendor specific TLV
355  *      - WMM TLV
356  *      - WAPI IE
357  *      - Generic IE
358  *      - TSF TLV
359  *
360  * Preparation also includes -
361  *      - Setting command ID and proper size
362  *      - Ensuring correct endian-ness
363  */
364 int mwifiex_cmd_802_11_associate(struct mwifiex_private *priv,
365                                  struct host_cmd_ds_command *cmd,
366                                  void *data_buf)
367 {
368         struct host_cmd_ds_802_11_associate *assoc = &cmd->params.associate;
369         struct mwifiex_bssdescriptor *bss_desc;
370         struct mwifiex_ie_types_ssid_param_set *ssid_tlv;
371         struct mwifiex_ie_types_phy_param_set *phy_tlv;
372         struct mwifiex_ie_types_ss_param_set *ss_tlv;
373         struct mwifiex_ie_types_rates_param_set *rates_tlv;
374         struct mwifiex_ie_types_auth_type *auth_tlv;
375         struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
376         u8 rates[MWIFIEX_SUPPORTED_RATES];
377         u32 rates_size;
378         u16 tmp_cap;
379         u8 *pos;
380         int rsn_ie_len = 0;
381
382         bss_desc = (struct mwifiex_bssdescriptor *) data_buf;
383         pos = (u8 *) assoc;
384
385         mwifiex_cfg_tx_buf(priv, bss_desc);
386
387         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_ASSOCIATE);
388
389         /* Save so we know which BSS Desc to use in the response handler */
390         priv->attempted_bss_desc = bss_desc;
391
392         memcpy(assoc->peer_sta_addr,
393                bss_desc->mac_address, sizeof(assoc->peer_sta_addr));
394         pos += sizeof(assoc->peer_sta_addr);
395
396         /* Set the listen interval */
397         assoc->listen_interval = cpu_to_le16(priv->listen_interval);
398         /* Set the beacon period */
399         assoc->beacon_period = cpu_to_le16(bss_desc->beacon_period);
400
401         pos += sizeof(assoc->cap_info_bitmap);
402         pos += sizeof(assoc->listen_interval);
403         pos += sizeof(assoc->beacon_period);
404         pos += sizeof(assoc->dtim_period);
405
406         ssid_tlv = (struct mwifiex_ie_types_ssid_param_set *) pos;
407         ssid_tlv->header.type = cpu_to_le16(WLAN_EID_SSID);
408         ssid_tlv->header.len = cpu_to_le16((u16) bss_desc->ssid.ssid_len);
409         memcpy(ssid_tlv->ssid, bss_desc->ssid.ssid,
410                 le16_to_cpu(ssid_tlv->header.len));
411         pos += sizeof(ssid_tlv->header) + le16_to_cpu(ssid_tlv->header.len);
412
413         phy_tlv = (struct mwifiex_ie_types_phy_param_set *) pos;
414         phy_tlv->header.type = cpu_to_le16(WLAN_EID_DS_PARAMS);
415         phy_tlv->header.len = cpu_to_le16(sizeof(phy_tlv->fh_ds.ds_param_set));
416         memcpy(&phy_tlv->fh_ds.ds_param_set,
417                &bss_desc->phy_param_set.ds_param_set.current_chan,
418                sizeof(phy_tlv->fh_ds.ds_param_set));
419         pos += sizeof(phy_tlv->header) + le16_to_cpu(phy_tlv->header.len);
420
421         ss_tlv = (struct mwifiex_ie_types_ss_param_set *) pos;
422         ss_tlv->header.type = cpu_to_le16(WLAN_EID_CF_PARAMS);
423         ss_tlv->header.len = cpu_to_le16(sizeof(ss_tlv->cf_ibss.cf_param_set));
424         pos += sizeof(ss_tlv->header) + le16_to_cpu(ss_tlv->header.len);
425
426         /* Get the common rates supported between the driver and the BSS Desc */
427         if (mwifiex_setup_rates_from_bssdesc
428             (priv, bss_desc, rates, &rates_size))
429                 return -1;
430
431         /* Save the data rates into Current BSS state structure */
432         priv->curr_bss_params.num_of_rates = rates_size;
433         memcpy(&priv->curr_bss_params.data_rates, rates, rates_size);
434
435         /* Setup the Rates TLV in the association command */
436         rates_tlv = (struct mwifiex_ie_types_rates_param_set *) pos;
437         rates_tlv->header.type = cpu_to_le16(WLAN_EID_SUPP_RATES);
438         rates_tlv->header.len = cpu_to_le16((u16) rates_size);
439         memcpy(rates_tlv->rates, rates, rates_size);
440         pos += sizeof(rates_tlv->header) + rates_size;
441         dev_dbg(priv->adapter->dev, "info: ASSOC_CMD: rates size = %d\n",
442                                         rates_size);
443
444         /* Add the Authentication type to be used for Auth frames if needed */
445         if (priv->sec_info.authentication_mode != MWIFIEX_AUTH_MODE_AUTO) {
446                 auth_tlv = (struct mwifiex_ie_types_auth_type *) pos;
447                 auth_tlv->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
448                 auth_tlv->header.len = cpu_to_le16(sizeof(auth_tlv->auth_type));
449                 if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_ENABLED)
450                         auth_tlv->auth_type = cpu_to_le16((u16) priv->sec_info.
451                                                           authentication_mode);
452                 else
453                         auth_tlv->auth_type =
454                                 cpu_to_le16(MWIFIEX_AUTH_MODE_OPEN);
455                 pos += sizeof(auth_tlv->header) +
456                         le16_to_cpu(auth_tlv->header.len);
457         }
458
459         if (IS_SUPPORT_MULTI_BANDS(priv->adapter)
460             && !(ISSUPP_11NENABLED(priv->adapter->fw_cap_info)
461                 && (!bss_desc->disable_11n)
462                  && (priv->adapter->config_bands & BAND_GN
463                      || priv->adapter->config_bands & BAND_AN)
464                  && (bss_desc->bcn_ht_cap)
465             )
466                 ) {
467                 /* Append a channel TLV for the channel the attempted AP was
468                    found on */
469                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
470                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
471                 chan_tlv->header.len =
472                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
473
474                 memset(chan_tlv->chan_scan_param, 0x00,
475                        sizeof(struct mwifiex_chan_scan_param_set));
476                 chan_tlv->chan_scan_param[0].chan_number =
477                         (bss_desc->phy_param_set.ds_param_set.current_chan);
478                 dev_dbg(priv->adapter->dev, "info: Assoc: TLV Chan = %d\n",
479                        chan_tlv->chan_scan_param[0].chan_number);
480
481                 chan_tlv->chan_scan_param[0].radio_type =
482                         mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
483
484                 dev_dbg(priv->adapter->dev, "info: Assoc: TLV Band = %d\n",
485                        chan_tlv->chan_scan_param[0].radio_type);
486                 pos += sizeof(chan_tlv->header) +
487                         sizeof(struct mwifiex_chan_scan_param_set);
488         }
489
490         if (!priv->wps.session_enable) {
491                 if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
492                         rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
493
494                 if (rsn_ie_len == -1)
495                         return -1;
496         }
497
498         if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info)
499                 && (!bss_desc->disable_11n)
500             && (priv->adapter->config_bands & BAND_GN
501                 || priv->adapter->config_bands & BAND_AN))
502                 mwifiex_cmd_append_11n_tlv(priv, bss_desc, &pos);
503
504         /* Append vendor specific IE TLV */
505         mwifiex_cmd_append_vsie_tlv(priv, MWIFIEX_VSIE_MASK_ASSOC, &pos);
506
507         mwifiex_wmm_process_association_req(priv, &pos, &bss_desc->wmm_ie,
508                                             bss_desc->bcn_ht_cap);
509         if (priv->sec_info.wapi_enabled && priv->wapi_ie_len)
510                 mwifiex_cmd_append_wapi_ie(priv, &pos);
511
512
513         mwifiex_cmd_append_generic_ie(priv, &pos);
514
515         mwifiex_cmd_append_tsf_tlv(priv, &pos, bss_desc);
516
517         cmd->size = cpu_to_le16((u16) (pos - (u8 *) assoc) + S_DS_GEN);
518
519         /* Set the Capability info at last */
520         tmp_cap = bss_desc->cap_info_bitmap;
521
522         if (priv->adapter->config_bands == BAND_B)
523                 SHORT_SLOT_TIME_DISABLED(tmp_cap);
524
525         tmp_cap &= CAPINFO_MASK;
526         dev_dbg(priv->adapter->dev, "info: ASSOC_CMD: tmp_cap=%4X CAPINFO_MASK=%4lX\n",
527                tmp_cap, CAPINFO_MASK);
528         assoc->cap_info_bitmap = cpu_to_le16(tmp_cap);
529
530         return 0;
531 }
532
533 /*
534  * Association firmware command response handler
535  *
536  * The response buffer for the association command has the following
537  * memory layout.
538  *
539  * For cases where an association response was not received (indicated
540  * by the CapInfo and AId field):
541  *
542  *     .------------------------------------------------------------.
543  *     |  Header(4 * sizeof(t_u16)):  Standard command response hdr |
544  *     .------------------------------------------------------------.
545  *     |  cap_info/Error Return(t_u16):                             |
546  *     |           0xFFFF(-1): Internal error                       |
547  *     |           0xFFFE(-2): Authentication unhandled message     |
548  *     |           0xFFFD(-3): Authentication refused               |
549  *     |           0xFFFC(-4): Timeout waiting for AP response      |
550  *     .------------------------------------------------------------.
551  *     |  status_code(t_u16):                                       |
552  *     |        If cap_info is -1:                                  |
553  *     |           An internal firmware failure prevented the       |
554  *     |           command from being processed.  The status_code   |
555  *     |           will be set to 1.                                |
556  *     |                                                            |
557  *     |        If cap_info is -2:                                  |
558  *     |           An authentication frame was received but was     |
559  *     |           not handled by the firmware.  IEEE Status        |
560  *     |           code for the failure is returned.                |
561  *     |                                                            |
562  *     |        If cap_info is -3:                                  |
563  *     |           An authentication frame was received and the     |
564  *     |           status_code is the IEEE Status reported in the   |
565  *     |           response.                                        |
566  *     |                                                            |
567  *     |        If cap_info is -4:                                  |
568  *     |           (1) Association response timeout                 |
569  *     |           (2) Authentication response timeout              |
570  *     .------------------------------------------------------------.
571  *     |  a_id(t_u16): 0xFFFF                                       |
572  *     .------------------------------------------------------------.
573  *
574  *
575  * For cases where an association response was received, the IEEE
576  * standard association response frame is returned:
577  *
578  *     .------------------------------------------------------------.
579  *     |  Header(4 * sizeof(t_u16)):  Standard command response hdr |
580  *     .------------------------------------------------------------.
581  *     |  cap_info(t_u16): IEEE Capability                          |
582  *     .------------------------------------------------------------.
583  *     |  status_code(t_u16): IEEE Status Code                      |
584  *     .------------------------------------------------------------.
585  *     |  a_id(t_u16): IEEE Association ID                          |
586  *     .------------------------------------------------------------.
587  *     |  IEEE IEs(variable): Any received IEs comprising the       |
588  *     |                      remaining portion of a received       |
589  *     |                      association response frame.           |
590  *     .------------------------------------------------------------.
591  *
592  * For simplistic handling, the status_code field can be used to determine
593  * an association success (0) or failure (non-zero).
594  */
595 int mwifiex_ret_802_11_associate(struct mwifiex_private *priv,
596                              struct host_cmd_ds_command *resp, void *wq_buf)
597 {
598         int ret = 0;
599         struct mwifiex_wait_queue *wait_queue =
600                 (struct mwifiex_wait_queue *) wq_buf;
601         struct ieee_types_assoc_rsp *assoc_rsp;
602         struct mwifiex_bssdescriptor *bss_desc;
603         u8 enable_data = true;
604
605         assoc_rsp = (struct ieee_types_assoc_rsp *) &resp->params;
606
607         priv->assoc_rsp_size = min(le16_to_cpu(resp->size) - S_DS_GEN,
608                                      sizeof(priv->assoc_rsp_buf));
609
610         memcpy(priv->assoc_rsp_buf, &resp->params, priv->assoc_rsp_size);
611
612         if (le16_to_cpu(assoc_rsp->status_code)) {
613                 priv->adapter->dbg.num_cmd_assoc_failure++;
614                 dev_err(priv->adapter->dev, "ASSOC_RESP: association failed, "
615                        "status code = %d, error = 0x%x, a_id = 0x%x\n",
616                        le16_to_cpu(assoc_rsp->status_code),
617                        le16_to_cpu(assoc_rsp->cap_info_bitmap),
618                        le16_to_cpu(assoc_rsp->a_id));
619
620                 ret = -1;
621                 goto done;
622         }
623
624         /* Send a Media Connected event, according to the Spec */
625         priv->media_connected = true;
626
627         priv->adapter->ps_state = PS_STATE_AWAKE;
628         priv->adapter->pps_uapsd_mode = false;
629         priv->adapter->tx_lock_flag = false;
630
631         /* Set the attempted BSSID Index to current */
632         bss_desc = priv->attempted_bss_desc;
633
634         dev_dbg(priv->adapter->dev, "info: ASSOC_RESP: %s\n",
635                                                 bss_desc->ssid.ssid);
636
637         /* Make a copy of current BSSID descriptor */
638         memcpy(&priv->curr_bss_params.bss_descriptor,
639                bss_desc, sizeof(struct mwifiex_bssdescriptor));
640
641         /* Update curr_bss_params */
642         priv->curr_bss_params.bss_descriptor.channel
643                 = bss_desc->phy_param_set.ds_param_set.current_chan;
644
645         priv->curr_bss_params.band = (u8) bss_desc->bss_band;
646
647         /*
648          * Adjust the timestamps in the scan table to be relative to the newly
649          * associated AP's TSF
650          */
651         mwifiex_update_tsf_timestamps(priv, bss_desc);
652
653         if (bss_desc->wmm_ie.vend_hdr.element_id == WLAN_EID_VENDOR_SPECIFIC)
654                 priv->curr_bss_params.wmm_enabled = true;
655         else
656                 priv->curr_bss_params.wmm_enabled = false;
657
658         if ((priv->wmm_required || bss_desc->bcn_ht_cap)
659                         && priv->curr_bss_params.wmm_enabled)
660                 priv->wmm_enabled = true;
661         else
662                 priv->wmm_enabled = false;
663
664         priv->curr_bss_params.wmm_uapsd_enabled = false;
665
666         if (priv->wmm_enabled)
667                 priv->curr_bss_params.wmm_uapsd_enabled
668                         = ((bss_desc->wmm_ie.qos_info_bitmap &
669                                 IEEE80211_WMM_IE_AP_QOSINFO_UAPSD) ? 1 : 0);
670
671         dev_dbg(priv->adapter->dev, "info: ASSOC_RESP: curr_pkt_filter is %#x\n",
672                priv->curr_pkt_filter);
673         if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
674                 priv->wpa_is_gtk_set = false;
675
676         if (priv->wmm_enabled) {
677                 /* Don't re-enable carrier until we get the WMM_GET_STATUS
678                    event */
679                 enable_data = false;
680         } else {
681                 /* Since WMM is not enabled, setup the queues with the
682                    defaults */
683                 mwifiex_wmm_setup_queue_priorities(priv, NULL);
684                 mwifiex_wmm_setup_ac_downgrade(priv);
685         }
686
687         if (enable_data)
688                 dev_dbg(priv->adapter->dev,
689                         "info: post association, re-enabling data flow\n");
690
691         /* Reset SNR/NF/RSSI values */
692         priv->data_rssi_last = 0;
693         priv->data_nf_last = 0;
694         priv->data_rssi_avg = 0;
695         priv->data_nf_avg = 0;
696         priv->bcn_rssi_last = 0;
697         priv->bcn_nf_last = 0;
698         priv->bcn_rssi_avg = 0;
699         priv->bcn_nf_avg = 0;
700         priv->rxpd_rate = 0;
701         priv->rxpd_htinfo = 0;
702
703         mwifiex_save_curr_bcn(priv);
704
705         priv->adapter->dbg.num_cmd_assoc_success++;
706
707         dev_dbg(priv->adapter->dev, "info: ASSOC_RESP: associated\n");
708
709         /* Add the ra_list here for infra mode as there will be only 1 ra
710            always */
711         mwifiex_ralist_add(priv,
712                            priv->curr_bss_params.bss_descriptor.mac_address);
713
714         if (!netif_carrier_ok(priv->netdev))
715                 netif_carrier_on(priv->netdev);
716         if (netif_queue_stopped(priv->netdev))
717                 netif_wake_queue(priv->netdev);
718
719         if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
720                 priv->scan_block = true;
721
722 done:
723         /* Need to indicate IOCTL complete */
724         if (wait_queue) {
725                 if (ret) {
726                         if (assoc_rsp->status_code)
727                                 wait_queue->status =
728                                         le16_to_cpu(assoc_rsp->status_code);
729                         else
730                                 wait_queue->status = MWIFIEX_ERROR_ASSOC_FAIL;
731                 } else {
732                         wait_queue->status = MWIFIEX_ERROR_NO_ERROR;
733                 }
734         }
735
736         return ret;
737 }
738
739 /*
740  * This function prepares command for ad-hoc start.
741  *
742  * Driver will fill up SSID, BSS mode, IBSS parameters, physical
743  * parameters, probe delay, and capability information. Firmware
744  * will fill up beacon period, basic rates and operational rates.
745  *
746  * In addition, the following TLVs are added -
747  *      - Channel TLV
748  *      - Vendor specific IE
749  *      - WPA/WPA2 IE
750  *      - HT Capabilities IE
751  *      - HT Information IE
752  *
753  * Preparation also includes -
754  *      - Setting command ID and proper size
755  *      - Ensuring correct endian-ness
756  */
757 int
758 mwifiex_cmd_802_11_ad_hoc_start(struct mwifiex_private *priv,
759                                 struct host_cmd_ds_command *cmd, void *data_buf)
760 {
761         int ret = 0, rsn_ie_len = 0;
762         struct mwifiex_adapter *adapter = priv->adapter;
763         struct host_cmd_ds_802_11_ad_hoc_start *adhoc_start =
764                 &cmd->params.adhoc_start;
765         struct mwifiex_bssdescriptor *bss_desc;
766         u32 cmd_append_size = 0;
767         u32 i;
768         u16 tmp_cap;
769         uint16_t ht_cap_info;
770         struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
771
772         struct mwifiex_ie_types_htcap *ht_cap;
773         struct mwifiex_ie_types_htinfo *ht_info;
774         u8 *pos = (u8 *) adhoc_start +
775                         sizeof(struct host_cmd_ds_802_11_ad_hoc_start);
776
777         if (!adapter)
778                 return -1;
779
780         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_START);
781
782         bss_desc = &priv->curr_bss_params.bss_descriptor;
783         priv->attempted_bss_desc = bss_desc;
784
785         /*
786          * Fill in the parameters for 2 data structures:
787          *   1. struct host_cmd_ds_802_11_ad_hoc_start command
788          *   2. bss_desc
789          * Driver will fill up SSID, bss_mode,IBSS param, Physical Param,
790          * probe delay, and Cap info.
791          * Firmware will fill up beacon period, Basic rates
792          * and operational rates.
793          */
794
795         memset(adhoc_start->ssid, 0, IEEE80211_MAX_SSID_LEN);
796
797         memcpy(adhoc_start->ssid,
798                ((struct mwifiex_802_11_ssid *) data_buf)->ssid,
799                ((struct mwifiex_802_11_ssid *) data_buf)->ssid_len);
800
801         dev_dbg(adapter->dev, "info: ADHOC_S_CMD: SSID = %s\n",
802                                 adhoc_start->ssid);
803
804         memset(bss_desc->ssid.ssid, 0, IEEE80211_MAX_SSID_LEN);
805         memcpy(bss_desc->ssid.ssid,
806                ((struct mwifiex_802_11_ssid *) data_buf)->ssid,
807                ((struct mwifiex_802_11_ssid *) data_buf)->ssid_len);
808
809         bss_desc->ssid.ssid_len =
810                 ((struct mwifiex_802_11_ssid *) data_buf)->ssid_len;
811
812         /* Set the BSS mode */
813         adhoc_start->bss_mode = HostCmd_BSS_MODE_IBSS;
814         bss_desc->bss_mode = MWIFIEX_BSS_MODE_IBSS;
815         adhoc_start->beacon_period = cpu_to_le16(priv->beacon_period);
816         bss_desc->beacon_period = priv->beacon_period;
817
818         /* Set Physical param set */
819 /* Parameter IE Id */
820 #define DS_PARA_IE_ID   3
821 /* Parameter IE length */
822 #define DS_PARA_IE_LEN  1
823
824         adhoc_start->phy_param_set.ds_param_set.element_id = DS_PARA_IE_ID;
825         adhoc_start->phy_param_set.ds_param_set.len = DS_PARA_IE_LEN;
826
827         if (!mwifiex_get_cfp_by_band_and_channel_from_cfg80211
828                         (priv, adapter->adhoc_start_band, (u16)
829                                 priv->adhoc_channel)) {
830                 struct mwifiex_chan_freq_power *cfp;
831                 cfp = mwifiex_get_cfp_by_band_and_channel_from_cfg80211(priv,
832                                 adapter->adhoc_start_band, FIRST_VALID_CHANNEL);
833                 if (cfp)
834                         priv->adhoc_channel = (u8) cfp->channel;
835         }
836
837         if (!priv->adhoc_channel) {
838                 dev_err(adapter->dev, "ADHOC_S_CMD: adhoc_channel cannot be 0\n");
839                 return -1;
840         }
841
842         dev_dbg(adapter->dev, "info: ADHOC_S_CMD: creating ADHOC on channel %d\n",
843                                 priv->adhoc_channel);
844
845         priv->curr_bss_params.bss_descriptor.channel = priv->adhoc_channel;
846         priv->curr_bss_params.band = adapter->adhoc_start_band;
847
848         bss_desc->channel = priv->adhoc_channel;
849         adhoc_start->phy_param_set.ds_param_set.current_chan =
850                 priv->adhoc_channel;
851
852         memcpy(&bss_desc->phy_param_set, &adhoc_start->phy_param_set,
853                sizeof(union ieee_types_phy_param_set));
854
855         /* Set IBSS param set */
856 /* IBSS parameter IE Id */
857 #define IBSS_PARA_IE_ID   6
858 /* IBSS parameter IE length */
859 #define IBSS_PARA_IE_LEN  2
860
861         adhoc_start->ss_param_set.ibss_param_set.element_id = IBSS_PARA_IE_ID;
862         adhoc_start->ss_param_set.ibss_param_set.len = IBSS_PARA_IE_LEN;
863         adhoc_start->ss_param_set.ibss_param_set.atim_window
864                 = cpu_to_le16(priv->atim_window);
865         memcpy(&bss_desc->ss_param_set, &adhoc_start->ss_param_set,
866                sizeof(union ieee_types_ss_param_set));
867
868         /* Set Capability info */
869         bss_desc->cap_info_bitmap |= WLAN_CAPABILITY_IBSS;
870         tmp_cap = le16_to_cpu(adhoc_start->cap_info_bitmap);
871         tmp_cap &= ~WLAN_CAPABILITY_ESS;
872         tmp_cap |= WLAN_CAPABILITY_IBSS;
873
874         /* Set up privacy in bss_desc */
875         if (priv->sec_info.encryption_mode != MWIFIEX_ENCRYPTION_MODE_NONE) {
876                 /* Ad-Hoc capability privacy on */
877                 dev_dbg(adapter->dev,
878                         "info: ADHOC_S_CMD: wep_status set privacy to WEP\n");
879                 bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_8021X_WEP;
880                 tmp_cap |= WLAN_CAPABILITY_PRIVACY;
881         } else {
882                 dev_dbg(adapter->dev, "info: ADHOC_S_CMD: wep_status NOT set,"
883                                 " setting privacy to ACCEPT ALL\n");
884                 bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_ACCEPT_ALL;
885         }
886
887         memset(adhoc_start->DataRate, 0, sizeof(adhoc_start->DataRate));
888         mwifiex_get_active_data_rates(priv, adhoc_start->DataRate);
889         if ((adapter->adhoc_start_band & BAND_G) &&
890             (priv->curr_pkt_filter & HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON)) {
891                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_MAC_CONTROL,
892                                           HostCmd_ACT_GEN_SET,
893                                           0, NULL, &priv->curr_pkt_filter);
894
895                 if (ret) {
896                         dev_err(adapter->dev,
897                                "ADHOC_S_CMD: G Protection config failed\n");
898                         return -1;
899                 }
900         }
901         /* Find the last non zero */
902         for (i = 0; i < sizeof(adhoc_start->DataRate) &&
903                         adhoc_start->DataRate[i];
904                         i++)
905                         ;
906
907         priv->curr_bss_params.num_of_rates = i;
908
909         /* Copy the ad-hoc creating rates into Current BSS rate structure */
910         memcpy(&priv->curr_bss_params.data_rates,
911                &adhoc_start->DataRate, priv->curr_bss_params.num_of_rates);
912
913         dev_dbg(adapter->dev, "info: ADHOC_S_CMD: rates=%02x %02x %02x %02x\n",
914                adhoc_start->DataRate[0], adhoc_start->DataRate[1],
915                adhoc_start->DataRate[2], adhoc_start->DataRate[3]);
916
917         dev_dbg(adapter->dev, "info: ADHOC_S_CMD: AD-HOC Start command is ready\n");
918
919         if (IS_SUPPORT_MULTI_BANDS(adapter)) {
920                 /* Append a channel TLV */
921                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
922                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
923                 chan_tlv->header.len =
924                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
925
926                 memset(chan_tlv->chan_scan_param, 0x00,
927                        sizeof(struct mwifiex_chan_scan_param_set));
928                 chan_tlv->chan_scan_param[0].chan_number =
929                         (u8) priv->curr_bss_params.bss_descriptor.channel;
930
931                 dev_dbg(adapter->dev, "info: ADHOC_S_CMD: TLV Chan = %d\n",
932                        chan_tlv->chan_scan_param[0].chan_number);
933
934                 chan_tlv->chan_scan_param[0].radio_type
935                        = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
936                 if (adapter->adhoc_start_band & BAND_GN
937                     || adapter->adhoc_start_band & BAND_AN) {
938                         if (adapter->chan_offset == SEC_CHANNEL_ABOVE)
939                                 chan_tlv->chan_scan_param[0].radio_type |=
940                                         SECOND_CHANNEL_ABOVE;
941                         else if (adapter->chan_offset == SEC_CHANNEL_BELOW)
942                                 chan_tlv->chan_scan_param[0].radio_type |=
943                                         SECOND_CHANNEL_BELOW;
944                 }
945                 dev_dbg(adapter->dev, "info: ADHOC_S_CMD: TLV Band = %d\n",
946                        chan_tlv->chan_scan_param[0].radio_type);
947                 pos += sizeof(chan_tlv->header) +
948                         sizeof(struct mwifiex_chan_scan_param_set);
949                 cmd_append_size +=
950                         sizeof(chan_tlv->header) +
951                         sizeof(struct mwifiex_chan_scan_param_set);
952         }
953
954         /* Append vendor specific IE TLV */
955         cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,
956                                 MWIFIEX_VSIE_MASK_ADHOC, &pos);
957
958         if (priv->sec_info.wpa_enabled) {
959                 rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
960                 if (rsn_ie_len == -1)
961                         return -1;
962                 cmd_append_size += rsn_ie_len;
963         }
964
965         if (adapter->adhoc_11n_enabled) {
966                 {
967                         ht_cap = (struct mwifiex_ie_types_htcap *) pos;
968                         memset(ht_cap, 0,
969                                sizeof(struct mwifiex_ie_types_htcap));
970                         ht_cap->header.type =
971                                 cpu_to_le16(WLAN_EID_HT_CAPABILITY);
972                         ht_cap->header.len =
973                                cpu_to_le16(sizeof(struct ieee80211_ht_cap));
974                         ht_cap_info = le16_to_cpu(ht_cap->ht_cap.cap_info);
975
976                         SETHT_SHORTGI20(ht_cap_info);
977                         if (adapter->chan_offset) {
978                                 SETHT_SHORTGI40(ht_cap_info);
979                                 SETHT_DSSSCCK40(ht_cap_info);
980                                 SETHT_SUPPCHANWIDTH(ht_cap_info);
981                                 SETHT_MCS32(ht_cap->ht_cap.mcs.rx_mask);
982                         }
983
984                         ht_cap->ht_cap.ampdu_params_info
985                                         = MAX_RX_AMPDU_SIZE_64K;
986                         ht_cap->ht_cap.mcs.rx_mask[0] = 0xff;
987                         pos += sizeof(struct mwifiex_ie_types_htcap);
988                         cmd_append_size +=
989                                 sizeof(struct mwifiex_ie_types_htcap);
990                 }
991                 {
992                         ht_info = (struct mwifiex_ie_types_htinfo *) pos;
993                         memset(ht_info, 0,
994                                sizeof(struct mwifiex_ie_types_htinfo));
995                         ht_info->header.type =
996                                 cpu_to_le16(WLAN_EID_HT_INFORMATION);
997                         ht_info->header.len =
998                                 cpu_to_le16(sizeof(struct ieee80211_ht_info));
999                         ht_info->ht_info.control_chan =
1000                                 (u8) priv->curr_bss_params.bss_descriptor.
1001                                 channel;
1002                         if (adapter->chan_offset) {
1003                                 ht_info->ht_info.ht_param =
1004                                         adapter->chan_offset;
1005                                 SET_CHANWIDTH40(ht_info->ht_info.ht_param);
1006                         }
1007                         ht_info->ht_info.operation_mode =
1008                                 cpu_to_le16(NON_GREENFIELD_STAS);
1009                         ht_info->ht_info.basic_set[0] = 0xff;
1010                         pos += sizeof(struct mwifiex_ie_types_htinfo);
1011                         cmd_append_size +=
1012                                 sizeof(struct mwifiex_ie_types_htinfo);
1013                 }
1014         }
1015
1016         cmd->size = cpu_to_le16((u16)
1017                             (sizeof(struct host_cmd_ds_802_11_ad_hoc_start)
1018                              + S_DS_GEN + cmd_append_size));
1019
1020         if (adapter->adhoc_start_band == BAND_B)
1021                 SHORT_SLOT_TIME_DISABLED(tmp_cap);
1022         else
1023                 SHORT_SLOT_TIME_ENABLED(tmp_cap);
1024
1025         adhoc_start->cap_info_bitmap = cpu_to_le16(tmp_cap);
1026
1027         return 0;
1028 }
1029
1030 /*
1031  * This function prepares command for ad-hoc join.
1032  *
1033  * Most of the parameters are set up by copying from the target BSS descriptor
1034  * from the scan response.
1035  *
1036  * In addition, the following TLVs are added -
1037  *      - Channel TLV
1038  *      - Vendor specific IE
1039  *      - WPA/WPA2 IE
1040  *      - 11n IE
1041  *
1042  * Preparation also includes -
1043  *      - Setting command ID and proper size
1044  *      - Ensuring correct endian-ness
1045  */
1046 int
1047 mwifiex_cmd_802_11_ad_hoc_join(struct mwifiex_private *priv,
1048                                struct host_cmd_ds_command *cmd, void *data_buf)
1049 {
1050         int ret = 0, rsn_ie_len = 0;
1051         struct host_cmd_ds_802_11_ad_hoc_join *adhoc_join =
1052                 &cmd->params.adhoc_join;
1053         struct mwifiex_bssdescriptor *bss_desc =
1054                 (struct mwifiex_bssdescriptor *) data_buf;
1055         struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
1056         u32 cmd_append_size = 0;
1057         u16 tmp_cap;
1058         u32 i, rates_size = 0;
1059         u16 curr_pkt_filter;
1060         u8 *pos =
1061                 (u8 *) adhoc_join +
1062                 sizeof(struct host_cmd_ds_802_11_ad_hoc_join);
1063
1064 /* Use G protection */
1065 #define USE_G_PROTECTION        0x02
1066         if (bss_desc->erp_flags & USE_G_PROTECTION) {
1067                 curr_pkt_filter =
1068                         priv->
1069                         curr_pkt_filter | HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON;
1070
1071                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_MAC_CONTROL,
1072                                           HostCmd_ACT_GEN_SET, 0, NULL,
1073                                           &curr_pkt_filter);
1074                 if (ret) {
1075                         dev_err(priv->adapter->dev,
1076                                "ADHOC_J_CMD: G Protection config failed\n");
1077                         return -1;
1078                 }
1079         }
1080
1081         priv->attempted_bss_desc = bss_desc;
1082
1083         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_JOIN);
1084
1085         adhoc_join->bss_descriptor.bss_mode = HostCmd_BSS_MODE_IBSS;
1086
1087         adhoc_join->bss_descriptor.beacon_period
1088                 = cpu_to_le16(bss_desc->beacon_period);
1089
1090         memcpy(&adhoc_join->bss_descriptor.bssid,
1091                &bss_desc->mac_address, ETH_ALEN);
1092
1093         memcpy(&adhoc_join->bss_descriptor.ssid,
1094                &bss_desc->ssid.ssid, bss_desc->ssid.ssid_len);
1095
1096         memcpy(&adhoc_join->bss_descriptor.phy_param_set,
1097                &bss_desc->phy_param_set,
1098                sizeof(union ieee_types_phy_param_set));
1099
1100         memcpy(&adhoc_join->bss_descriptor.ss_param_set,
1101                &bss_desc->ss_param_set, sizeof(union ieee_types_ss_param_set));
1102
1103         tmp_cap = bss_desc->cap_info_bitmap;
1104
1105         tmp_cap &= CAPINFO_MASK;
1106
1107         dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: tmp_cap=%4X"
1108                         " CAPINFO_MASK=%4lX\n", tmp_cap, CAPINFO_MASK);
1109
1110         /* Information on BSSID descriptor passed to FW */
1111         dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: BSSID = %pM, SSID = %s\n",
1112                                 adhoc_join->bss_descriptor.bssid,
1113                                 adhoc_join->bss_descriptor.ssid);
1114
1115         for (i = 0; bss_desc->supported_rates[i] &&
1116                         i < MWIFIEX_SUPPORTED_RATES;
1117                         i++)
1118                         ;
1119         rates_size = i;
1120
1121         /* Copy Data Rates from the Rates recorded in scan response */
1122         memset(adhoc_join->bss_descriptor.data_rates, 0,
1123                sizeof(adhoc_join->bss_descriptor.data_rates));
1124         memcpy(adhoc_join->bss_descriptor.data_rates,
1125                bss_desc->supported_rates, rates_size);
1126
1127         /* Copy the adhoc join rates into Current BSS state structure */
1128         priv->curr_bss_params.num_of_rates = rates_size;
1129         memcpy(&priv->curr_bss_params.data_rates, bss_desc->supported_rates,
1130                rates_size);
1131
1132         /* Copy the channel information */
1133         priv->curr_bss_params.bss_descriptor.channel = bss_desc->channel;
1134         priv->curr_bss_params.band = (u8) bss_desc->bss_band;
1135
1136         if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_ENABLED
1137             || priv->sec_info.wpa_enabled)
1138                 tmp_cap |= WLAN_CAPABILITY_PRIVACY;
1139
1140         if (IS_SUPPORT_MULTI_BANDS(priv->adapter)) {
1141                 /* Append a channel TLV */
1142                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
1143                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
1144                 chan_tlv->header.len =
1145                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
1146
1147                 memset(chan_tlv->chan_scan_param, 0x00,
1148                        sizeof(struct mwifiex_chan_scan_param_set));
1149                 chan_tlv->chan_scan_param[0].chan_number =
1150                         (bss_desc->phy_param_set.ds_param_set.current_chan);
1151                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: TLV Chan = %d\n",
1152                        chan_tlv->chan_scan_param[0].chan_number);
1153
1154                 chan_tlv->chan_scan_param[0].radio_type =
1155                         mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
1156
1157                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: TLV Band = %d\n",
1158                        chan_tlv->chan_scan_param[0].radio_type);
1159                 pos += sizeof(chan_tlv->header) +
1160                         sizeof(struct mwifiex_chan_scan_param_set);
1161                 cmd_append_size += sizeof(chan_tlv->header) +
1162                         sizeof(struct mwifiex_chan_scan_param_set);
1163         }
1164
1165         if (priv->sec_info.wpa_enabled)
1166                 rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
1167         if (rsn_ie_len == -1)
1168                 return -1;
1169         cmd_append_size += rsn_ie_len;
1170
1171         if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info))
1172                 cmd_append_size += mwifiex_cmd_append_11n_tlv(priv,
1173                         bss_desc, &pos);
1174
1175         /* Append vendor specific IE TLV */
1176         cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,
1177                         MWIFIEX_VSIE_MASK_ADHOC, &pos);
1178
1179         cmd->size = cpu_to_le16((u16)
1180                             (sizeof(struct host_cmd_ds_802_11_ad_hoc_join)
1181                              + S_DS_GEN + cmd_append_size));
1182
1183         adhoc_join->bss_descriptor.cap_info_bitmap = cpu_to_le16(tmp_cap);
1184
1185         return ret;
1186 }
1187
1188 /*
1189  * This function handles the command response of ad-hoc start and
1190  * ad-hoc join.
1191  *
1192  * The function generates a device-connected event to notify
1193  * the applications, in case of successful ad-hoc start/join, and
1194  * saves the beacon buffer.
1195  */
1196 int mwifiex_ret_802_11_ad_hoc(struct mwifiex_private *priv,
1197                               struct host_cmd_ds_command *resp, void *wq_buf)
1198 {
1199         int ret = 0;
1200         struct mwifiex_wait_queue *wait_queue =
1201                 (struct mwifiex_wait_queue *) wq_buf;
1202         struct host_cmd_ds_802_11_ad_hoc_result *adhoc_result;
1203         struct mwifiex_bssdescriptor *bss_desc;
1204         u16 command = le16_to_cpu(resp->command);
1205         u16 result = le16_to_cpu(resp->result);
1206
1207         adhoc_result = &resp->params.adhoc_result;
1208
1209         bss_desc = priv->attempted_bss_desc;
1210
1211         /* Join result code 0 --> SUCCESS */
1212         if (result) {
1213                 dev_err(priv->adapter->dev, "ADHOC_RESP: failed\n");
1214                 if (priv->media_connected)
1215                         mwifiex_reset_connect_state(priv);
1216
1217                 memset(&priv->curr_bss_params.bss_descriptor,
1218                        0x00, sizeof(struct mwifiex_bssdescriptor));
1219
1220                 ret = -1;
1221                 goto done;
1222         }
1223
1224         /* Send a Media Connected event, according to the Spec */
1225         priv->media_connected = true;
1226
1227         if (command == HostCmd_CMD_802_11_AD_HOC_START) {
1228                 dev_dbg(priv->adapter->dev, "info: ADHOC_S_RESP %s\n",
1229                                 bss_desc->ssid.ssid);
1230
1231                 /* Update the created network descriptor with the new BSSID */
1232                 memcpy(bss_desc->mac_address,
1233                        adhoc_result->bssid, ETH_ALEN);
1234
1235                 priv->adhoc_state = ADHOC_STARTED;
1236         } else {
1237                 /*
1238                  * Now the join cmd should be successful.
1239                  * If BSSID has changed use SSID to compare instead of BSSID
1240                  */
1241                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_RESP %s\n",
1242                                 bss_desc->ssid.ssid);
1243
1244                 /*
1245                  * Make a copy of current BSSID descriptor, only needed for
1246                  * join since the current descriptor is already being used
1247                  * for adhoc start
1248                  */
1249                 memcpy(&priv->curr_bss_params.bss_descriptor,
1250                        bss_desc, sizeof(struct mwifiex_bssdescriptor));
1251
1252                 priv->adhoc_state = ADHOC_JOINED;
1253         }
1254
1255         dev_dbg(priv->adapter->dev, "info: ADHOC_RESP: channel = %d\n",
1256                                 priv->adhoc_channel);
1257         dev_dbg(priv->adapter->dev, "info: ADHOC_RESP: BSSID = %pM\n",
1258                priv->curr_bss_params.bss_descriptor.mac_address);
1259
1260         if (!netif_carrier_ok(priv->netdev))
1261                 netif_carrier_on(priv->netdev);
1262         if (netif_queue_stopped(priv->netdev))
1263                 netif_wake_queue(priv->netdev);
1264
1265         mwifiex_save_curr_bcn(priv);
1266
1267 done:
1268         /* Need to indicate IOCTL complete */
1269         if (wait_queue) {
1270                 if (ret)
1271                         wait_queue->status = MWIFIEX_ERROR_ASSOC_FAIL;
1272                 else
1273                         wait_queue->status = MWIFIEX_ERROR_NO_ERROR;
1274
1275         }
1276
1277         return ret;
1278 }
1279
1280 /*
1281  * This function associates to a specific BSS discovered in a scan.
1282  *
1283  * It clears any past association response stored for application
1284  * retrieval and calls the command preparation routine to send the
1285  * command to firmware.
1286  */
1287 int mwifiex_associate(struct mwifiex_private *priv,
1288                       void *wait_queue, struct mwifiex_bssdescriptor *bss_desc)
1289 {
1290         int ret = 0;
1291         u8 current_bssid[ETH_ALEN];
1292
1293         /* Return error if the adapter or table entry is not marked as infra */
1294         if ((priv->bss_mode != MWIFIEX_BSS_MODE_INFRA) ||
1295             (bss_desc->bss_mode != MWIFIEX_BSS_MODE_INFRA))
1296                 return -1;
1297
1298         memcpy(&current_bssid,
1299                &priv->curr_bss_params.bss_descriptor.mac_address,
1300                sizeof(current_bssid));
1301
1302         /* Clear any past association response stored for application
1303            retrieval */
1304         priv->assoc_rsp_size = 0;
1305
1306         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_ASSOCIATE,
1307                                   HostCmd_ACT_GEN_SET, 0, wait_queue,
1308                                   bss_desc);
1309
1310         return ret;
1311 }
1312
1313 /*
1314  * This function starts an ad-hoc network.
1315  *
1316  * It calls the command preparation routine to send the command to firmware.
1317  */
1318 int
1319 mwifiex_adhoc_start(struct mwifiex_private *priv,
1320                     void *wait_queue, struct mwifiex_802_11_ssid *adhoc_ssid)
1321 {
1322         int ret = 0;
1323
1324         dev_dbg(priv->adapter->dev, "info: Adhoc Channel = %d\n",
1325                 priv->adhoc_channel);
1326         dev_dbg(priv->adapter->dev, "info: curr_bss_params.channel = %d\n",
1327                priv->curr_bss_params.bss_descriptor.channel);
1328         dev_dbg(priv->adapter->dev, "info: curr_bss_params.band = %d\n",
1329                priv->curr_bss_params.band);
1330
1331         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_AD_HOC_START,
1332                                   HostCmd_ACT_GEN_SET, 0, wait_queue,
1333                                   adhoc_ssid);
1334
1335         return ret;
1336 }
1337
1338 /*
1339  * This function joins an ad-hoc network found in a previous scan.
1340  *
1341  * It calls the command preparation routine to send the command to firmware,
1342  * if already not connected to the requested SSID.
1343  */
1344 int mwifiex_adhoc_join(struct mwifiex_private *priv,
1345                        void *wait_queue, struct mwifiex_bssdescriptor *bss_desc)
1346 {
1347         int ret = 0;
1348
1349         dev_dbg(priv->adapter->dev, "info: adhoc join: curr_bss ssid =%s\n",
1350                priv->curr_bss_params.bss_descriptor.ssid.ssid);
1351         dev_dbg(priv->adapter->dev, "info: adhoc join: curr_bss ssid_len =%u\n",
1352                priv->curr_bss_params.bss_descriptor.ssid.ssid_len);
1353         dev_dbg(priv->adapter->dev, "info: adhoc join: ssid =%s\n",
1354                 bss_desc->ssid.ssid);
1355         dev_dbg(priv->adapter->dev, "info: adhoc join: ssid_len =%u\n",
1356                bss_desc->ssid.ssid_len);
1357
1358         /* Check if the requested SSID is already joined */
1359         if (priv->curr_bss_params.bss_descriptor.ssid.ssid_len &&
1360             !mwifiex_ssid_cmp(&bss_desc->ssid,
1361                               &priv->curr_bss_params.bss_descriptor.ssid) &&
1362             (priv->curr_bss_params.bss_descriptor.bss_mode ==
1363              MWIFIEX_BSS_MODE_IBSS)) {
1364                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: new ad-hoc SSID"
1365                         " is the same as current; not attempting to re-join\n");
1366                 return -1;
1367         }
1368
1369         dev_dbg(priv->adapter->dev, "info: curr_bss_params.channel = %d\n",
1370                priv->curr_bss_params.bss_descriptor.channel);
1371         dev_dbg(priv->adapter->dev, "info: curr_bss_params.band = %c\n",
1372                priv->curr_bss_params.band);
1373
1374         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_AD_HOC_JOIN,
1375                                   HostCmd_ACT_GEN_SET, 0, wait_queue,
1376                                   bss_desc);
1377
1378         return ret;
1379 }
1380
1381 /*
1382  * This function deauthenticates/disconnects from infra network by sending
1383  * deauthentication request.
1384  */
1385 static int mwifiex_deauthenticate_infra(struct mwifiex_private *priv,
1386                                         struct mwifiex_wait_queue *wait,
1387                                         u8 *mac)
1388 {
1389         u8 mac_address[ETH_ALEN];
1390         int ret = 0;
1391         u8 zero_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
1392
1393         if (mac) {
1394                 if (!memcmp(mac, zero_mac, sizeof(zero_mac)))
1395                         memcpy((u8 *) &mac_address,
1396                                (u8 *) &priv->curr_bss_params.bss_descriptor.
1397                                mac_address, ETH_ALEN);
1398                 else
1399                         memcpy((u8 *) &mac_address, (u8 *) mac, ETH_ALEN);
1400         } else {
1401                 memcpy((u8 *) &mac_address, (u8 *) &priv->curr_bss_params.
1402                        bss_descriptor.mac_address, ETH_ALEN);
1403         }
1404
1405         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_DEAUTHENTICATE,
1406                                   HostCmd_ACT_GEN_SET, 0, wait, &mac_address);
1407
1408         if (!ret && wait)
1409                 ret = -EINPROGRESS;
1410
1411         return ret;
1412 }
1413
1414 /*
1415  * This function deauthenticates/disconnects from a BSS.
1416  *
1417  * In case of infra made, it sends deauthentication request, and
1418  * in case of ad-hoc mode, a stop network request is sent to the firmware.
1419  */
1420 int mwifiex_deauthenticate(struct mwifiex_private *priv,
1421                            struct mwifiex_wait_queue *wait, u8 *mac)
1422 {
1423         int ret = 0;
1424
1425         if (priv->media_connected) {
1426                 if (priv->bss_mode == MWIFIEX_BSS_MODE_INFRA) {
1427                         ret = mwifiex_deauthenticate_infra(priv, wait, mac);
1428                 } else if (priv->bss_mode == MWIFIEX_BSS_MODE_IBSS) {
1429                         ret = mwifiex_prepare_cmd(priv,
1430                                         HostCmd_CMD_802_11_AD_HOC_STOP,
1431                                         HostCmd_ACT_GEN_SET, 0, wait, NULL);
1432
1433                         if (!ret && wait)
1434                                 ret = -EINPROGRESS;
1435                 }
1436         }
1437
1438         return ret;
1439 }
1440
1441 /*
1442  * This function converts band to radio type used in channel TLV.
1443  */
1444 u8
1445 mwifiex_band_to_radio_type(u8 band)
1446 {
1447         u8 ret_radio_type;
1448
1449         switch (band) {
1450         case BAND_A:
1451         case BAND_AN:
1452         case BAND_A | BAND_AN:
1453                 ret_radio_type = HostCmd_SCAN_RADIO_TYPE_A;
1454                 break;
1455         case BAND_B:
1456         case BAND_G:
1457         case BAND_B | BAND_G:
1458         default:
1459                 ret_radio_type = HostCmd_SCAN_RADIO_TYPE_BG;
1460                 break;
1461         }
1462
1463         return ret_radio_type;
1464 }