08fa721580cb6cb0b4905674262a3b4ef7158e03
[firefly-linux-kernel-4.4.55.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 */
445         auth_tlv = (struct mwifiex_ie_types_auth_type *) pos;
446         auth_tlv->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
447         auth_tlv->header.len = cpu_to_le16(sizeof(auth_tlv->auth_type));
448         if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_ENABLED)
449                 auth_tlv->auth_type = cpu_to_le16(
450                                 (u16) priv->sec_info.authentication_mode);
451         else
452                 auth_tlv->auth_type = cpu_to_le16(MWIFIEX_AUTH_MODE_OPEN);
453
454         pos += sizeof(auth_tlv->header) + le16_to_cpu(auth_tlv->header.len);
455
456         if (IS_SUPPORT_MULTI_BANDS(priv->adapter)
457             && !(ISSUPP_11NENABLED(priv->adapter->fw_cap_info)
458                 && (!bss_desc->disable_11n)
459                  && (priv->adapter->config_bands & BAND_GN
460                      || priv->adapter->config_bands & BAND_AN)
461                  && (bss_desc->bcn_ht_cap)
462             )
463                 ) {
464                 /* Append a channel TLV for the channel the attempted AP was
465                    found on */
466                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
467                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
468                 chan_tlv->header.len =
469                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
470
471                 memset(chan_tlv->chan_scan_param, 0x00,
472                        sizeof(struct mwifiex_chan_scan_param_set));
473                 chan_tlv->chan_scan_param[0].chan_number =
474                         (bss_desc->phy_param_set.ds_param_set.current_chan);
475                 dev_dbg(priv->adapter->dev, "info: Assoc: TLV Chan = %d\n",
476                        chan_tlv->chan_scan_param[0].chan_number);
477
478                 chan_tlv->chan_scan_param[0].radio_type =
479                         mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
480
481                 dev_dbg(priv->adapter->dev, "info: Assoc: TLV Band = %d\n",
482                        chan_tlv->chan_scan_param[0].radio_type);
483                 pos += sizeof(chan_tlv->header) +
484                         sizeof(struct mwifiex_chan_scan_param_set);
485         }
486
487         if (!priv->wps.session_enable) {
488                 if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
489                         rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
490
491                 if (rsn_ie_len == -1)
492                         return -1;
493         }
494
495         if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info)
496                 && (!bss_desc->disable_11n)
497             && (priv->adapter->config_bands & BAND_GN
498                 || priv->adapter->config_bands & BAND_AN))
499                 mwifiex_cmd_append_11n_tlv(priv, bss_desc, &pos);
500
501         /* Append vendor specific IE TLV */
502         mwifiex_cmd_append_vsie_tlv(priv, MWIFIEX_VSIE_MASK_ASSOC, &pos);
503
504         mwifiex_wmm_process_association_req(priv, &pos, &bss_desc->wmm_ie,
505                                             bss_desc->bcn_ht_cap);
506         if (priv->sec_info.wapi_enabled && priv->wapi_ie_len)
507                 mwifiex_cmd_append_wapi_ie(priv, &pos);
508
509
510         mwifiex_cmd_append_generic_ie(priv, &pos);
511
512         mwifiex_cmd_append_tsf_tlv(priv, &pos, bss_desc);
513
514         cmd->size = cpu_to_le16((u16) (pos - (u8 *) assoc) + S_DS_GEN);
515
516         /* Set the Capability info at last */
517         tmp_cap = bss_desc->cap_info_bitmap;
518
519         if (priv->adapter->config_bands == BAND_B)
520                 tmp_cap &= ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
521
522         tmp_cap &= CAPINFO_MASK;
523         dev_dbg(priv->adapter->dev, "info: ASSOC_CMD: tmp_cap=%4X CAPINFO_MASK=%4lX\n",
524                tmp_cap, CAPINFO_MASK);
525         assoc->cap_info_bitmap = cpu_to_le16(tmp_cap);
526
527         return 0;
528 }
529
530 /*
531  * Association firmware command response handler
532  *
533  * The response buffer for the association command has the following
534  * memory layout.
535  *
536  * For cases where an association response was not received (indicated
537  * by the CapInfo and AId field):
538  *
539  *     .------------------------------------------------------------.
540  *     |  Header(4 * sizeof(t_u16)):  Standard command response hdr |
541  *     .------------------------------------------------------------.
542  *     |  cap_info/Error Return(t_u16):                             |
543  *     |           0xFFFF(-1): Internal error                       |
544  *     |           0xFFFE(-2): Authentication unhandled message     |
545  *     |           0xFFFD(-3): Authentication refused               |
546  *     |           0xFFFC(-4): Timeout waiting for AP response      |
547  *     .------------------------------------------------------------.
548  *     |  status_code(t_u16):                                       |
549  *     |        If cap_info is -1:                                  |
550  *     |           An internal firmware failure prevented the       |
551  *     |           command from being processed.  The status_code   |
552  *     |           will be set to 1.                                |
553  *     |                                                            |
554  *     |        If cap_info is -2:                                  |
555  *     |           An authentication frame was received but was     |
556  *     |           not handled by the firmware.  IEEE Status        |
557  *     |           code for the failure is returned.                |
558  *     |                                                            |
559  *     |        If cap_info is -3:                                  |
560  *     |           An authentication frame was received and the     |
561  *     |           status_code is the IEEE Status reported in the   |
562  *     |           response.                                        |
563  *     |                                                            |
564  *     |        If cap_info is -4:                                  |
565  *     |           (1) Association response timeout                 |
566  *     |           (2) Authentication response timeout              |
567  *     .------------------------------------------------------------.
568  *     |  a_id(t_u16): 0xFFFF                                       |
569  *     .------------------------------------------------------------.
570  *
571  *
572  * For cases where an association response was received, the IEEE
573  * standard association response frame is returned:
574  *
575  *     .------------------------------------------------------------.
576  *     |  Header(4 * sizeof(t_u16)):  Standard command response hdr |
577  *     .------------------------------------------------------------.
578  *     |  cap_info(t_u16): IEEE Capability                          |
579  *     .------------------------------------------------------------.
580  *     |  status_code(t_u16): IEEE Status Code                      |
581  *     .------------------------------------------------------------.
582  *     |  a_id(t_u16): IEEE Association ID                          |
583  *     .------------------------------------------------------------.
584  *     |  IEEE IEs(variable): Any received IEs comprising the       |
585  *     |                      remaining portion of a received       |
586  *     |                      association response frame.           |
587  *     .------------------------------------------------------------.
588  *
589  * For simplistic handling, the status_code field can be used to determine
590  * an association success (0) or failure (non-zero).
591  */
592 int mwifiex_ret_802_11_associate(struct mwifiex_private *priv,
593                              struct host_cmd_ds_command *resp, void *wq_buf)
594 {
595         int ret = 0;
596         struct mwifiex_wait_queue *wait_queue =
597                 (struct mwifiex_wait_queue *) wq_buf;
598         struct ieee_types_assoc_rsp *assoc_rsp;
599         struct mwifiex_bssdescriptor *bss_desc;
600         u8 enable_data = true;
601
602         assoc_rsp = (struct ieee_types_assoc_rsp *) &resp->params;
603
604         priv->assoc_rsp_size = min(le16_to_cpu(resp->size) - S_DS_GEN,
605                                      sizeof(priv->assoc_rsp_buf));
606
607         memcpy(priv->assoc_rsp_buf, &resp->params, priv->assoc_rsp_size);
608
609         if (le16_to_cpu(assoc_rsp->status_code)) {
610                 priv->adapter->dbg.num_cmd_assoc_failure++;
611                 dev_err(priv->adapter->dev, "ASSOC_RESP: association failed, "
612                        "status code = %d, error = 0x%x, a_id = 0x%x\n",
613                        le16_to_cpu(assoc_rsp->status_code),
614                        le16_to_cpu(assoc_rsp->cap_info_bitmap),
615                        le16_to_cpu(assoc_rsp->a_id));
616
617                 ret = -1;
618                 goto done;
619         }
620
621         /* Send a Media Connected event, according to the Spec */
622         priv->media_connected = true;
623
624         priv->adapter->ps_state = PS_STATE_AWAKE;
625         priv->adapter->pps_uapsd_mode = false;
626         priv->adapter->tx_lock_flag = false;
627
628         /* Set the attempted BSSID Index to current */
629         bss_desc = priv->attempted_bss_desc;
630
631         dev_dbg(priv->adapter->dev, "info: ASSOC_RESP: %s\n",
632                                                 bss_desc->ssid.ssid);
633
634         /* Make a copy of current BSSID descriptor */
635         memcpy(&priv->curr_bss_params.bss_descriptor,
636                bss_desc, sizeof(struct mwifiex_bssdescriptor));
637
638         /* Update curr_bss_params */
639         priv->curr_bss_params.bss_descriptor.channel
640                 = bss_desc->phy_param_set.ds_param_set.current_chan;
641
642         priv->curr_bss_params.band = (u8) bss_desc->bss_band;
643
644         /*
645          * Adjust the timestamps in the scan table to be relative to the newly
646          * associated AP's TSF
647          */
648         mwifiex_update_tsf_timestamps(priv, bss_desc);
649
650         if (bss_desc->wmm_ie.vend_hdr.element_id == WLAN_EID_VENDOR_SPECIFIC)
651                 priv->curr_bss_params.wmm_enabled = true;
652         else
653                 priv->curr_bss_params.wmm_enabled = false;
654
655         if ((priv->wmm_required || bss_desc->bcn_ht_cap)
656                         && priv->curr_bss_params.wmm_enabled)
657                 priv->wmm_enabled = true;
658         else
659                 priv->wmm_enabled = false;
660
661         priv->curr_bss_params.wmm_uapsd_enabled = false;
662
663         if (priv->wmm_enabled)
664                 priv->curr_bss_params.wmm_uapsd_enabled
665                         = ((bss_desc->wmm_ie.qos_info_bitmap &
666                                 IEEE80211_WMM_IE_AP_QOSINFO_UAPSD) ? 1 : 0);
667
668         dev_dbg(priv->adapter->dev, "info: ASSOC_RESP: curr_pkt_filter is %#x\n",
669                priv->curr_pkt_filter);
670         if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
671                 priv->wpa_is_gtk_set = false;
672
673         if (priv->wmm_enabled) {
674                 /* Don't re-enable carrier until we get the WMM_GET_STATUS
675                    event */
676                 enable_data = false;
677         } else {
678                 /* Since WMM is not enabled, setup the queues with the
679                    defaults */
680                 mwifiex_wmm_setup_queue_priorities(priv, NULL);
681                 mwifiex_wmm_setup_ac_downgrade(priv);
682         }
683
684         if (enable_data)
685                 dev_dbg(priv->adapter->dev,
686                         "info: post association, re-enabling data flow\n");
687
688         /* Reset SNR/NF/RSSI values */
689         priv->data_rssi_last = 0;
690         priv->data_nf_last = 0;
691         priv->data_rssi_avg = 0;
692         priv->data_nf_avg = 0;
693         priv->bcn_rssi_last = 0;
694         priv->bcn_nf_last = 0;
695         priv->bcn_rssi_avg = 0;
696         priv->bcn_nf_avg = 0;
697         priv->rxpd_rate = 0;
698         priv->rxpd_htinfo = 0;
699
700         mwifiex_save_curr_bcn(priv);
701
702         priv->adapter->dbg.num_cmd_assoc_success++;
703
704         dev_dbg(priv->adapter->dev, "info: ASSOC_RESP: associated\n");
705
706         /* Add the ra_list here for infra mode as there will be only 1 ra
707            always */
708         mwifiex_ralist_add(priv,
709                            priv->curr_bss_params.bss_descriptor.mac_address);
710
711         if (!netif_carrier_ok(priv->netdev))
712                 netif_carrier_on(priv->netdev);
713         if (netif_queue_stopped(priv->netdev))
714                 netif_wake_queue(priv->netdev);
715
716         if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
717                 priv->scan_block = true;
718
719 done:
720         /* Need to indicate IOCTL complete */
721         if (wait_queue) {
722                 if (ret) {
723                         if (assoc_rsp->status_code)
724                                 wait_queue->status =
725                                         le16_to_cpu(assoc_rsp->status_code);
726                         else
727                                 wait_queue->status = MWIFIEX_ERROR_ASSOC_FAIL;
728                 } else {
729                         wait_queue->status = MWIFIEX_ERROR_NO_ERROR;
730                 }
731         }
732
733         return ret;
734 }
735
736 /*
737  * This function prepares command for ad-hoc start.
738  *
739  * Driver will fill up SSID, BSS mode, IBSS parameters, physical
740  * parameters, probe delay, and capability information. Firmware
741  * will fill up beacon period, basic rates and operational rates.
742  *
743  * In addition, the following TLVs are added -
744  *      - Channel TLV
745  *      - Vendor specific IE
746  *      - WPA/WPA2 IE
747  *      - HT Capabilities IE
748  *      - HT Information IE
749  *
750  * Preparation also includes -
751  *      - Setting command ID and proper size
752  *      - Ensuring correct endian-ness
753  */
754 int
755 mwifiex_cmd_802_11_ad_hoc_start(struct mwifiex_private *priv,
756                                 struct host_cmd_ds_command *cmd, void *data_buf)
757 {
758         int ret = 0, rsn_ie_len = 0;
759         struct mwifiex_adapter *adapter = priv->adapter;
760         struct host_cmd_ds_802_11_ad_hoc_start *adhoc_start =
761                 &cmd->params.adhoc_start;
762         struct mwifiex_bssdescriptor *bss_desc;
763         u32 cmd_append_size = 0;
764         u32 i;
765         u16 tmp_cap;
766         uint16_t ht_cap_info;
767         struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
768
769         struct mwifiex_ie_types_htcap *ht_cap;
770         struct mwifiex_ie_types_htinfo *ht_info;
771         u8 *pos = (u8 *) adhoc_start +
772                         sizeof(struct host_cmd_ds_802_11_ad_hoc_start);
773
774         if (!adapter)
775                 return -1;
776
777         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_START);
778
779         bss_desc = &priv->curr_bss_params.bss_descriptor;
780         priv->attempted_bss_desc = bss_desc;
781
782         /*
783          * Fill in the parameters for 2 data structures:
784          *   1. struct host_cmd_ds_802_11_ad_hoc_start command
785          *   2. bss_desc
786          * Driver will fill up SSID, bss_mode,IBSS param, Physical Param,
787          * probe delay, and Cap info.
788          * Firmware will fill up beacon period, Basic rates
789          * and operational rates.
790          */
791
792         memset(adhoc_start->ssid, 0, IEEE80211_MAX_SSID_LEN);
793
794         memcpy(adhoc_start->ssid,
795                ((struct mwifiex_802_11_ssid *) data_buf)->ssid,
796                ((struct mwifiex_802_11_ssid *) data_buf)->ssid_len);
797
798         dev_dbg(adapter->dev, "info: ADHOC_S_CMD: SSID = %s\n",
799                                 adhoc_start->ssid);
800
801         memset(bss_desc->ssid.ssid, 0, IEEE80211_MAX_SSID_LEN);
802         memcpy(bss_desc->ssid.ssid,
803                ((struct mwifiex_802_11_ssid *) data_buf)->ssid,
804                ((struct mwifiex_802_11_ssid *) data_buf)->ssid_len);
805
806         bss_desc->ssid.ssid_len =
807                 ((struct mwifiex_802_11_ssid *) data_buf)->ssid_len;
808
809         /* Set the BSS mode */
810         adhoc_start->bss_mode = HostCmd_BSS_MODE_IBSS;
811         bss_desc->bss_mode = MWIFIEX_BSS_MODE_IBSS;
812         adhoc_start->beacon_period = cpu_to_le16(priv->beacon_period);
813         bss_desc->beacon_period = priv->beacon_period;
814
815         /* Set Physical param set */
816 /* Parameter IE Id */
817 #define DS_PARA_IE_ID   3
818 /* Parameter IE length */
819 #define DS_PARA_IE_LEN  1
820
821         adhoc_start->phy_param_set.ds_param_set.element_id = DS_PARA_IE_ID;
822         adhoc_start->phy_param_set.ds_param_set.len = DS_PARA_IE_LEN;
823
824         if (!mwifiex_get_cfp_by_band_and_channel_from_cfg80211
825                         (priv, adapter->adhoc_start_band, (u16)
826                                 priv->adhoc_channel)) {
827                 struct mwifiex_chan_freq_power *cfp;
828                 cfp = mwifiex_get_cfp_by_band_and_channel_from_cfg80211(priv,
829                                 adapter->adhoc_start_band, FIRST_VALID_CHANNEL);
830                 if (cfp)
831                         priv->adhoc_channel = (u8) cfp->channel;
832         }
833
834         if (!priv->adhoc_channel) {
835                 dev_err(adapter->dev, "ADHOC_S_CMD: adhoc_channel cannot be 0\n");
836                 return -1;
837         }
838
839         dev_dbg(adapter->dev, "info: ADHOC_S_CMD: creating ADHOC on channel %d\n",
840                                 priv->adhoc_channel);
841
842         priv->curr_bss_params.bss_descriptor.channel = priv->adhoc_channel;
843         priv->curr_bss_params.band = adapter->adhoc_start_band;
844
845         bss_desc->channel = priv->adhoc_channel;
846         adhoc_start->phy_param_set.ds_param_set.current_chan =
847                 priv->adhoc_channel;
848
849         memcpy(&bss_desc->phy_param_set, &adhoc_start->phy_param_set,
850                sizeof(union ieee_types_phy_param_set));
851
852         /* Set IBSS param set */
853 /* IBSS parameter IE Id */
854 #define IBSS_PARA_IE_ID   6
855 /* IBSS parameter IE length */
856 #define IBSS_PARA_IE_LEN  2
857
858         adhoc_start->ss_param_set.ibss_param_set.element_id = IBSS_PARA_IE_ID;
859         adhoc_start->ss_param_set.ibss_param_set.len = IBSS_PARA_IE_LEN;
860         adhoc_start->ss_param_set.ibss_param_set.atim_window
861                 = cpu_to_le16(priv->atim_window);
862         memcpy(&bss_desc->ss_param_set, &adhoc_start->ss_param_set,
863                sizeof(union ieee_types_ss_param_set));
864
865         /* Set Capability info */
866         bss_desc->cap_info_bitmap |= WLAN_CAPABILITY_IBSS;
867         tmp_cap = le16_to_cpu(adhoc_start->cap_info_bitmap);
868         tmp_cap &= ~WLAN_CAPABILITY_ESS;
869         tmp_cap |= WLAN_CAPABILITY_IBSS;
870
871         /* Set up privacy in bss_desc */
872         if (priv->sec_info.encryption_mode != MWIFIEX_ENCRYPTION_MODE_NONE) {
873                 /* Ad-Hoc capability privacy on */
874                 dev_dbg(adapter->dev,
875                         "info: ADHOC_S_CMD: wep_status set privacy to WEP\n");
876                 bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_8021X_WEP;
877                 tmp_cap |= WLAN_CAPABILITY_PRIVACY;
878         } else {
879                 dev_dbg(adapter->dev, "info: ADHOC_S_CMD: wep_status NOT set,"
880                                 " setting privacy to ACCEPT ALL\n");
881                 bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_ACCEPT_ALL;
882         }
883
884         memset(adhoc_start->DataRate, 0, sizeof(adhoc_start->DataRate));
885         mwifiex_get_active_data_rates(priv, adhoc_start->DataRate);
886         if ((adapter->adhoc_start_band & BAND_G) &&
887             (priv->curr_pkt_filter & HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON)) {
888                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_MAC_CONTROL,
889                                           HostCmd_ACT_GEN_SET,
890                                           0, NULL, &priv->curr_pkt_filter);
891
892                 if (ret) {
893                         dev_err(adapter->dev,
894                                "ADHOC_S_CMD: G Protection config failed\n");
895                         return -1;
896                 }
897         }
898         /* Find the last non zero */
899         for (i = 0; i < sizeof(adhoc_start->DataRate) &&
900                         adhoc_start->DataRate[i];
901                         i++)
902                         ;
903
904         priv->curr_bss_params.num_of_rates = i;
905
906         /* Copy the ad-hoc creating rates into Current BSS rate structure */
907         memcpy(&priv->curr_bss_params.data_rates,
908                &adhoc_start->DataRate, priv->curr_bss_params.num_of_rates);
909
910         dev_dbg(adapter->dev, "info: ADHOC_S_CMD: rates=%02x %02x %02x %02x\n",
911                adhoc_start->DataRate[0], adhoc_start->DataRate[1],
912                adhoc_start->DataRate[2], adhoc_start->DataRate[3]);
913
914         dev_dbg(adapter->dev, "info: ADHOC_S_CMD: AD-HOC Start command is ready\n");
915
916         if (IS_SUPPORT_MULTI_BANDS(adapter)) {
917                 /* Append a channel TLV */
918                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
919                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
920                 chan_tlv->header.len =
921                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
922
923                 memset(chan_tlv->chan_scan_param, 0x00,
924                        sizeof(struct mwifiex_chan_scan_param_set));
925                 chan_tlv->chan_scan_param[0].chan_number =
926                         (u8) priv->curr_bss_params.bss_descriptor.channel;
927
928                 dev_dbg(adapter->dev, "info: ADHOC_S_CMD: TLV Chan = %d\n",
929                        chan_tlv->chan_scan_param[0].chan_number);
930
931                 chan_tlv->chan_scan_param[0].radio_type
932                        = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
933                 if (adapter->adhoc_start_band & BAND_GN
934                     || adapter->adhoc_start_band & BAND_AN) {
935                         if (adapter->chan_offset == SEC_CHANNEL_ABOVE)
936                                 chan_tlv->chan_scan_param[0].radio_type |=
937                                         SECOND_CHANNEL_ABOVE;
938                         else if (adapter->chan_offset == SEC_CHANNEL_BELOW)
939                                 chan_tlv->chan_scan_param[0].radio_type |=
940                                         SECOND_CHANNEL_BELOW;
941                 }
942                 dev_dbg(adapter->dev, "info: ADHOC_S_CMD: TLV Band = %d\n",
943                        chan_tlv->chan_scan_param[0].radio_type);
944                 pos += sizeof(chan_tlv->header) +
945                         sizeof(struct mwifiex_chan_scan_param_set);
946                 cmd_append_size +=
947                         sizeof(chan_tlv->header) +
948                         sizeof(struct mwifiex_chan_scan_param_set);
949         }
950
951         /* Append vendor specific IE TLV */
952         cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,
953                                 MWIFIEX_VSIE_MASK_ADHOC, &pos);
954
955         if (priv->sec_info.wpa_enabled) {
956                 rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
957                 if (rsn_ie_len == -1)
958                         return -1;
959                 cmd_append_size += rsn_ie_len;
960         }
961
962         if (adapter->adhoc_11n_enabled) {
963                 {
964                         ht_cap = (struct mwifiex_ie_types_htcap *) pos;
965                         memset(ht_cap, 0,
966                                sizeof(struct mwifiex_ie_types_htcap));
967                         ht_cap->header.type =
968                                 cpu_to_le16(WLAN_EID_HT_CAPABILITY);
969                         ht_cap->header.len =
970                                cpu_to_le16(sizeof(struct ieee80211_ht_cap));
971                         ht_cap_info = le16_to_cpu(ht_cap->ht_cap.cap_info);
972
973                         ht_cap_info |= IEEE80211_HT_CAP_SGI_20;
974                         if (adapter->chan_offset) {
975                                 ht_cap_info |= IEEE80211_HT_CAP_SGI_40;
976                                 ht_cap_info |= IEEE80211_HT_CAP_DSSSCCK40;
977                                 ht_cap_info |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
978                                 SETHT_MCS32(ht_cap->ht_cap.mcs.rx_mask);
979                         }
980
981                         ht_cap->ht_cap.ampdu_params_info
982                                         = IEEE80211_HT_MAX_AMPDU_64K;
983                         ht_cap->ht_cap.mcs.rx_mask[0] = 0xff;
984                         pos += sizeof(struct mwifiex_ie_types_htcap);
985                         cmd_append_size +=
986                                 sizeof(struct mwifiex_ie_types_htcap);
987                 }
988                 {
989                         ht_info = (struct mwifiex_ie_types_htinfo *) pos;
990                         memset(ht_info, 0,
991                                sizeof(struct mwifiex_ie_types_htinfo));
992                         ht_info->header.type =
993                                 cpu_to_le16(WLAN_EID_HT_INFORMATION);
994                         ht_info->header.len =
995                                 cpu_to_le16(sizeof(struct ieee80211_ht_info));
996                         ht_info->ht_info.control_chan =
997                                 (u8) priv->curr_bss_params.bss_descriptor.
998                                 channel;
999                         if (adapter->chan_offset) {
1000                                 ht_info->ht_info.ht_param =
1001                                         adapter->chan_offset;
1002                                 ht_info->ht_info.ht_param |=
1003                                         IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
1004                         }
1005                         ht_info->ht_info.operation_mode =
1006                                 cpu_to_le16(NON_GREENFIELD_STAS);
1007                         ht_info->ht_info.basic_set[0] = 0xff;
1008                         pos += sizeof(struct mwifiex_ie_types_htinfo);
1009                         cmd_append_size +=
1010                                 sizeof(struct mwifiex_ie_types_htinfo);
1011                 }
1012         }
1013
1014         cmd->size = cpu_to_le16((u16)
1015                             (sizeof(struct host_cmd_ds_802_11_ad_hoc_start)
1016                              + S_DS_GEN + cmd_append_size));
1017
1018         if (adapter->adhoc_start_band == BAND_B)
1019                 tmp_cap &= ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
1020         else
1021                 tmp_cap |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
1022
1023         adhoc_start->cap_info_bitmap = cpu_to_le16(tmp_cap);
1024
1025         return 0;
1026 }
1027
1028 /*
1029  * This function prepares command for ad-hoc join.
1030  *
1031  * Most of the parameters are set up by copying from the target BSS descriptor
1032  * from the scan response.
1033  *
1034  * In addition, the following TLVs are added -
1035  *      - Channel TLV
1036  *      - Vendor specific IE
1037  *      - WPA/WPA2 IE
1038  *      - 11n IE
1039  *
1040  * Preparation also includes -
1041  *      - Setting command ID and proper size
1042  *      - Ensuring correct endian-ness
1043  */
1044 int
1045 mwifiex_cmd_802_11_ad_hoc_join(struct mwifiex_private *priv,
1046                                struct host_cmd_ds_command *cmd, void *data_buf)
1047 {
1048         int ret = 0, rsn_ie_len = 0;
1049         struct host_cmd_ds_802_11_ad_hoc_join *adhoc_join =
1050                 &cmd->params.adhoc_join;
1051         struct mwifiex_bssdescriptor *bss_desc =
1052                 (struct mwifiex_bssdescriptor *) data_buf;
1053         struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
1054         u32 cmd_append_size = 0;
1055         u16 tmp_cap;
1056         u32 i, rates_size = 0;
1057         u16 curr_pkt_filter;
1058         u8 *pos =
1059                 (u8 *) adhoc_join +
1060                 sizeof(struct host_cmd_ds_802_11_ad_hoc_join);
1061
1062 /* Use G protection */
1063 #define USE_G_PROTECTION        0x02
1064         if (bss_desc->erp_flags & USE_G_PROTECTION) {
1065                 curr_pkt_filter =
1066                         priv->
1067                         curr_pkt_filter | HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON;
1068
1069                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_MAC_CONTROL,
1070                                           HostCmd_ACT_GEN_SET, 0, NULL,
1071                                           &curr_pkt_filter);
1072                 if (ret) {
1073                         dev_err(priv->adapter->dev,
1074                                "ADHOC_J_CMD: G Protection config failed\n");
1075                         return -1;
1076                 }
1077         }
1078
1079         priv->attempted_bss_desc = bss_desc;
1080
1081         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_JOIN);
1082
1083         adhoc_join->bss_descriptor.bss_mode = HostCmd_BSS_MODE_IBSS;
1084
1085         adhoc_join->bss_descriptor.beacon_period
1086                 = cpu_to_le16(bss_desc->beacon_period);
1087
1088         memcpy(&adhoc_join->bss_descriptor.bssid,
1089                &bss_desc->mac_address, ETH_ALEN);
1090
1091         memcpy(&adhoc_join->bss_descriptor.ssid,
1092                &bss_desc->ssid.ssid, bss_desc->ssid.ssid_len);
1093
1094         memcpy(&adhoc_join->bss_descriptor.phy_param_set,
1095                &bss_desc->phy_param_set,
1096                sizeof(union ieee_types_phy_param_set));
1097
1098         memcpy(&adhoc_join->bss_descriptor.ss_param_set,
1099                &bss_desc->ss_param_set, sizeof(union ieee_types_ss_param_set));
1100
1101         tmp_cap = bss_desc->cap_info_bitmap;
1102
1103         tmp_cap &= CAPINFO_MASK;
1104
1105         dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: tmp_cap=%4X"
1106                         " CAPINFO_MASK=%4lX\n", tmp_cap, CAPINFO_MASK);
1107
1108         /* Information on BSSID descriptor passed to FW */
1109         dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: BSSID = %pM, SSID = %s\n",
1110                                 adhoc_join->bss_descriptor.bssid,
1111                                 adhoc_join->bss_descriptor.ssid);
1112
1113         for (i = 0; bss_desc->supported_rates[i] &&
1114                         i < MWIFIEX_SUPPORTED_RATES;
1115                         i++)
1116                         ;
1117         rates_size = i;
1118
1119         /* Copy Data Rates from the Rates recorded in scan response */
1120         memset(adhoc_join->bss_descriptor.data_rates, 0,
1121                sizeof(adhoc_join->bss_descriptor.data_rates));
1122         memcpy(adhoc_join->bss_descriptor.data_rates,
1123                bss_desc->supported_rates, rates_size);
1124
1125         /* Copy the adhoc join rates into Current BSS state structure */
1126         priv->curr_bss_params.num_of_rates = rates_size;
1127         memcpy(&priv->curr_bss_params.data_rates, bss_desc->supported_rates,
1128                rates_size);
1129
1130         /* Copy the channel information */
1131         priv->curr_bss_params.bss_descriptor.channel = bss_desc->channel;
1132         priv->curr_bss_params.band = (u8) bss_desc->bss_band;
1133
1134         if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_ENABLED
1135             || priv->sec_info.wpa_enabled)
1136                 tmp_cap |= WLAN_CAPABILITY_PRIVACY;
1137
1138         if (IS_SUPPORT_MULTI_BANDS(priv->adapter)) {
1139                 /* Append a channel TLV */
1140                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
1141                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
1142                 chan_tlv->header.len =
1143                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
1144
1145                 memset(chan_tlv->chan_scan_param, 0x00,
1146                        sizeof(struct mwifiex_chan_scan_param_set));
1147                 chan_tlv->chan_scan_param[0].chan_number =
1148                         (bss_desc->phy_param_set.ds_param_set.current_chan);
1149                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: TLV Chan = %d\n",
1150                        chan_tlv->chan_scan_param[0].chan_number);
1151
1152                 chan_tlv->chan_scan_param[0].radio_type =
1153                         mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
1154
1155                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: TLV Band = %d\n",
1156                        chan_tlv->chan_scan_param[0].radio_type);
1157                 pos += sizeof(chan_tlv->header) +
1158                         sizeof(struct mwifiex_chan_scan_param_set);
1159                 cmd_append_size += sizeof(chan_tlv->header) +
1160                         sizeof(struct mwifiex_chan_scan_param_set);
1161         }
1162
1163         if (priv->sec_info.wpa_enabled)
1164                 rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
1165         if (rsn_ie_len == -1)
1166                 return -1;
1167         cmd_append_size += rsn_ie_len;
1168
1169         if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info))
1170                 cmd_append_size += mwifiex_cmd_append_11n_tlv(priv,
1171                         bss_desc, &pos);
1172
1173         /* Append vendor specific IE TLV */
1174         cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,
1175                         MWIFIEX_VSIE_MASK_ADHOC, &pos);
1176
1177         cmd->size = cpu_to_le16((u16)
1178                             (sizeof(struct host_cmd_ds_802_11_ad_hoc_join)
1179                              + S_DS_GEN + cmd_append_size));
1180
1181         adhoc_join->bss_descriptor.cap_info_bitmap = cpu_to_le16(tmp_cap);
1182
1183         return ret;
1184 }
1185
1186 /*
1187  * This function handles the command response of ad-hoc start and
1188  * ad-hoc join.
1189  *
1190  * The function generates a device-connected event to notify
1191  * the applications, in case of successful ad-hoc start/join, and
1192  * saves the beacon buffer.
1193  */
1194 int mwifiex_ret_802_11_ad_hoc(struct mwifiex_private *priv,
1195                               struct host_cmd_ds_command *resp, void *wq_buf)
1196 {
1197         int ret = 0;
1198         struct mwifiex_wait_queue *wait_queue =
1199                 (struct mwifiex_wait_queue *) wq_buf;
1200         struct host_cmd_ds_802_11_ad_hoc_result *adhoc_result;
1201         struct mwifiex_bssdescriptor *bss_desc;
1202         u16 command = le16_to_cpu(resp->command);
1203         u16 result = le16_to_cpu(resp->result);
1204
1205         adhoc_result = &resp->params.adhoc_result;
1206
1207         bss_desc = priv->attempted_bss_desc;
1208
1209         /* Join result code 0 --> SUCCESS */
1210         if (result) {
1211                 dev_err(priv->adapter->dev, "ADHOC_RESP: failed\n");
1212                 if (priv->media_connected)
1213                         mwifiex_reset_connect_state(priv);
1214
1215                 memset(&priv->curr_bss_params.bss_descriptor,
1216                        0x00, sizeof(struct mwifiex_bssdescriptor));
1217
1218                 ret = -1;
1219                 goto done;
1220         }
1221
1222         /* Send a Media Connected event, according to the Spec */
1223         priv->media_connected = true;
1224
1225         if (command == HostCmd_CMD_802_11_AD_HOC_START) {
1226                 dev_dbg(priv->adapter->dev, "info: ADHOC_S_RESP %s\n",
1227                                 bss_desc->ssid.ssid);
1228
1229                 /* Update the created network descriptor with the new BSSID */
1230                 memcpy(bss_desc->mac_address,
1231                        adhoc_result->bssid, ETH_ALEN);
1232
1233                 priv->adhoc_state = ADHOC_STARTED;
1234         } else {
1235                 /*
1236                  * Now the join cmd should be successful.
1237                  * If BSSID has changed use SSID to compare instead of BSSID
1238                  */
1239                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_RESP %s\n",
1240                                 bss_desc->ssid.ssid);
1241
1242                 /*
1243                  * Make a copy of current BSSID descriptor, only needed for
1244                  * join since the current descriptor is already being used
1245                  * for adhoc start
1246                  */
1247                 memcpy(&priv->curr_bss_params.bss_descriptor,
1248                        bss_desc, sizeof(struct mwifiex_bssdescriptor));
1249
1250                 priv->adhoc_state = ADHOC_JOINED;
1251         }
1252
1253         dev_dbg(priv->adapter->dev, "info: ADHOC_RESP: channel = %d\n",
1254                                 priv->adhoc_channel);
1255         dev_dbg(priv->adapter->dev, "info: ADHOC_RESP: BSSID = %pM\n",
1256                priv->curr_bss_params.bss_descriptor.mac_address);
1257
1258         if (!netif_carrier_ok(priv->netdev))
1259                 netif_carrier_on(priv->netdev);
1260         if (netif_queue_stopped(priv->netdev))
1261                 netif_wake_queue(priv->netdev);
1262
1263         mwifiex_save_curr_bcn(priv);
1264
1265 done:
1266         /* Need to indicate IOCTL complete */
1267         if (wait_queue) {
1268                 if (ret)
1269                         wait_queue->status = MWIFIEX_ERROR_ASSOC_FAIL;
1270                 else
1271                         wait_queue->status = MWIFIEX_ERROR_NO_ERROR;
1272
1273         }
1274
1275         return ret;
1276 }
1277
1278 /*
1279  * This function associates to a specific BSS discovered in a scan.
1280  *
1281  * It clears any past association response stored for application
1282  * retrieval and calls the command preparation routine to send the
1283  * command to firmware.
1284  */
1285 int mwifiex_associate(struct mwifiex_private *priv,
1286                       void *wait_queue, struct mwifiex_bssdescriptor *bss_desc)
1287 {
1288         int ret = 0;
1289         u8 current_bssid[ETH_ALEN];
1290
1291         /* Return error if the adapter or table entry is not marked as infra */
1292         if ((priv->bss_mode != MWIFIEX_BSS_MODE_INFRA) ||
1293             (bss_desc->bss_mode != MWIFIEX_BSS_MODE_INFRA))
1294                 return -1;
1295
1296         memcpy(&current_bssid,
1297                &priv->curr_bss_params.bss_descriptor.mac_address,
1298                sizeof(current_bssid));
1299
1300         /* Clear any past association response stored for application
1301            retrieval */
1302         priv->assoc_rsp_size = 0;
1303
1304         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_ASSOCIATE,
1305                                   HostCmd_ACT_GEN_SET, 0, wait_queue,
1306                                   bss_desc);
1307
1308         return ret;
1309 }
1310
1311 /*
1312  * This function starts an ad-hoc network.
1313  *
1314  * It calls the command preparation routine to send the command to firmware.
1315  */
1316 int
1317 mwifiex_adhoc_start(struct mwifiex_private *priv,
1318                     void *wait_queue, struct mwifiex_802_11_ssid *adhoc_ssid)
1319 {
1320         int ret = 0;
1321
1322         dev_dbg(priv->adapter->dev, "info: Adhoc Channel = %d\n",
1323                 priv->adhoc_channel);
1324         dev_dbg(priv->adapter->dev, "info: curr_bss_params.channel = %d\n",
1325                priv->curr_bss_params.bss_descriptor.channel);
1326         dev_dbg(priv->adapter->dev, "info: curr_bss_params.band = %d\n",
1327                priv->curr_bss_params.band);
1328
1329         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_AD_HOC_START,
1330                                   HostCmd_ACT_GEN_SET, 0, wait_queue,
1331                                   adhoc_ssid);
1332
1333         return ret;
1334 }
1335
1336 /*
1337  * This function joins an ad-hoc network found in a previous scan.
1338  *
1339  * It calls the command preparation routine to send the command to firmware,
1340  * if already not connected to the requested SSID.
1341  */
1342 int mwifiex_adhoc_join(struct mwifiex_private *priv,
1343                        void *wait_queue, struct mwifiex_bssdescriptor *bss_desc)
1344 {
1345         int ret = 0;
1346
1347         dev_dbg(priv->adapter->dev, "info: adhoc join: curr_bss ssid =%s\n",
1348                priv->curr_bss_params.bss_descriptor.ssid.ssid);
1349         dev_dbg(priv->adapter->dev, "info: adhoc join: curr_bss ssid_len =%u\n",
1350                priv->curr_bss_params.bss_descriptor.ssid.ssid_len);
1351         dev_dbg(priv->adapter->dev, "info: adhoc join: ssid =%s\n",
1352                 bss_desc->ssid.ssid);
1353         dev_dbg(priv->adapter->dev, "info: adhoc join: ssid_len =%u\n",
1354                bss_desc->ssid.ssid_len);
1355
1356         /* Check if the requested SSID is already joined */
1357         if (priv->curr_bss_params.bss_descriptor.ssid.ssid_len &&
1358             !mwifiex_ssid_cmp(&bss_desc->ssid,
1359                               &priv->curr_bss_params.bss_descriptor.ssid) &&
1360             (priv->curr_bss_params.bss_descriptor.bss_mode ==
1361              MWIFIEX_BSS_MODE_IBSS)) {
1362                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: new ad-hoc SSID"
1363                         " is the same as current; not attempting to re-join\n");
1364                 return -1;
1365         }
1366
1367         dev_dbg(priv->adapter->dev, "info: curr_bss_params.channel = %d\n",
1368                priv->curr_bss_params.bss_descriptor.channel);
1369         dev_dbg(priv->adapter->dev, "info: curr_bss_params.band = %c\n",
1370                priv->curr_bss_params.band);
1371
1372         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_AD_HOC_JOIN,
1373                                   HostCmd_ACT_GEN_SET, 0, wait_queue,
1374                                   bss_desc);
1375
1376         return ret;
1377 }
1378
1379 /*
1380  * This function deauthenticates/disconnects from infra network by sending
1381  * deauthentication request.
1382  */
1383 static int mwifiex_deauthenticate_infra(struct mwifiex_private *priv,
1384                                         struct mwifiex_wait_queue *wait,
1385                                         u8 *mac)
1386 {
1387         u8 mac_address[ETH_ALEN];
1388         int ret = 0;
1389         u8 zero_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
1390
1391         if (mac) {
1392                 if (!memcmp(mac, zero_mac, sizeof(zero_mac)))
1393                         memcpy((u8 *) &mac_address,
1394                                (u8 *) &priv->curr_bss_params.bss_descriptor.
1395                                mac_address, ETH_ALEN);
1396                 else
1397                         memcpy((u8 *) &mac_address, (u8 *) mac, ETH_ALEN);
1398         } else {
1399                 memcpy((u8 *) &mac_address, (u8 *) &priv->curr_bss_params.
1400                        bss_descriptor.mac_address, ETH_ALEN);
1401         }
1402
1403         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_DEAUTHENTICATE,
1404                                   HostCmd_ACT_GEN_SET, 0, wait, &mac_address);
1405
1406         if (!ret && wait)
1407                 ret = -EINPROGRESS;
1408
1409         return ret;
1410 }
1411
1412 /*
1413  * This function deauthenticates/disconnects from a BSS.
1414  *
1415  * In case of infra made, it sends deauthentication request, and
1416  * in case of ad-hoc mode, a stop network request is sent to the firmware.
1417  */
1418 int mwifiex_deauthenticate(struct mwifiex_private *priv,
1419                            struct mwifiex_wait_queue *wait, u8 *mac)
1420 {
1421         int ret = 0;
1422
1423         if (priv->media_connected) {
1424                 if (priv->bss_mode == MWIFIEX_BSS_MODE_INFRA) {
1425                         ret = mwifiex_deauthenticate_infra(priv, wait, mac);
1426                 } else if (priv->bss_mode == MWIFIEX_BSS_MODE_IBSS) {
1427                         ret = mwifiex_prepare_cmd(priv,
1428                                         HostCmd_CMD_802_11_AD_HOC_STOP,
1429                                         HostCmd_ACT_GEN_SET, 0, wait, NULL);
1430
1431                         if (!ret && wait)
1432                                 ret = -EINPROGRESS;
1433                 }
1434         }
1435
1436         return ret;
1437 }
1438
1439 /*
1440  * This function converts band to radio type used in channel TLV.
1441  */
1442 u8
1443 mwifiex_band_to_radio_type(u8 band)
1444 {
1445         u8 ret_radio_type;
1446
1447         switch (band) {
1448         case BAND_A:
1449         case BAND_AN:
1450         case BAND_A | BAND_AN:
1451                 ret_radio_type = HostCmd_SCAN_RADIO_TYPE_A;
1452                 break;
1453         case BAND_B:
1454         case BAND_G:
1455         case BAND_B | BAND_G:
1456         default:
1457                 ret_radio_type = HostCmd_SCAN_RADIO_TYPE_BG;
1458                 break;
1459         }
1460
1461         return ret_radio_type;
1462 }