mwifiex: remove macro SHORT_SLOT_TIME_DISABLED
[cascardo/linux.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                         SETHT_SHORTGI20(ht_cap_info);
974                         if (adapter->chan_offset) {
975                                 SETHT_SHORTGI40(ht_cap_info);
976                                 SETHT_DSSSCCK40(ht_cap_info);
977                                 SETHT_SUPPCHANWIDTH(ht_cap_info);
978                                 SETHT_MCS32(ht_cap->ht_cap.mcs.rx_mask);
979                         }
980
981                         ht_cap->ht_cap.ampdu_params_info
982                                         = MAX_RX_AMPDU_SIZE_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                                 SET_CHANWIDTH40(ht_info->ht_info.ht_param);
1003                         }
1004                         ht_info->ht_info.operation_mode =
1005                                 cpu_to_le16(NON_GREENFIELD_STAS);
1006                         ht_info->ht_info.basic_set[0] = 0xff;
1007                         pos += sizeof(struct mwifiex_ie_types_htinfo);
1008                         cmd_append_size +=
1009                                 sizeof(struct mwifiex_ie_types_htinfo);
1010                 }
1011         }
1012
1013         cmd->size = cpu_to_le16((u16)
1014                             (sizeof(struct host_cmd_ds_802_11_ad_hoc_start)
1015                              + S_DS_GEN + cmd_append_size));
1016
1017         if (adapter->adhoc_start_band == BAND_B)
1018                 tmp_cap &= ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
1019         else
1020                 tmp_cap |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
1021
1022         adhoc_start->cap_info_bitmap = cpu_to_le16(tmp_cap);
1023
1024         return 0;
1025 }
1026
1027 /*
1028  * This function prepares command for ad-hoc join.
1029  *
1030  * Most of the parameters are set up by copying from the target BSS descriptor
1031  * from the scan response.
1032  *
1033  * In addition, the following TLVs are added -
1034  *      - Channel TLV
1035  *      - Vendor specific IE
1036  *      - WPA/WPA2 IE
1037  *      - 11n IE
1038  *
1039  * Preparation also includes -
1040  *      - Setting command ID and proper size
1041  *      - Ensuring correct endian-ness
1042  */
1043 int
1044 mwifiex_cmd_802_11_ad_hoc_join(struct mwifiex_private *priv,
1045                                struct host_cmd_ds_command *cmd, void *data_buf)
1046 {
1047         int ret = 0, rsn_ie_len = 0;
1048         struct host_cmd_ds_802_11_ad_hoc_join *adhoc_join =
1049                 &cmd->params.adhoc_join;
1050         struct mwifiex_bssdescriptor *bss_desc =
1051                 (struct mwifiex_bssdescriptor *) data_buf;
1052         struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
1053         u32 cmd_append_size = 0;
1054         u16 tmp_cap;
1055         u32 i, rates_size = 0;
1056         u16 curr_pkt_filter;
1057         u8 *pos =
1058                 (u8 *) adhoc_join +
1059                 sizeof(struct host_cmd_ds_802_11_ad_hoc_join);
1060
1061 /* Use G protection */
1062 #define USE_G_PROTECTION        0x02
1063         if (bss_desc->erp_flags & USE_G_PROTECTION) {
1064                 curr_pkt_filter =
1065                         priv->
1066                         curr_pkt_filter | HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON;
1067
1068                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_MAC_CONTROL,
1069                                           HostCmd_ACT_GEN_SET, 0, NULL,
1070                                           &curr_pkt_filter);
1071                 if (ret) {
1072                         dev_err(priv->adapter->dev,
1073                                "ADHOC_J_CMD: G Protection config failed\n");
1074                         return -1;
1075                 }
1076         }
1077
1078         priv->attempted_bss_desc = bss_desc;
1079
1080         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_JOIN);
1081
1082         adhoc_join->bss_descriptor.bss_mode = HostCmd_BSS_MODE_IBSS;
1083
1084         adhoc_join->bss_descriptor.beacon_period
1085                 = cpu_to_le16(bss_desc->beacon_period);
1086
1087         memcpy(&adhoc_join->bss_descriptor.bssid,
1088                &bss_desc->mac_address, ETH_ALEN);
1089
1090         memcpy(&adhoc_join->bss_descriptor.ssid,
1091                &bss_desc->ssid.ssid, bss_desc->ssid.ssid_len);
1092
1093         memcpy(&adhoc_join->bss_descriptor.phy_param_set,
1094                &bss_desc->phy_param_set,
1095                sizeof(union ieee_types_phy_param_set));
1096
1097         memcpy(&adhoc_join->bss_descriptor.ss_param_set,
1098                &bss_desc->ss_param_set, sizeof(union ieee_types_ss_param_set));
1099
1100         tmp_cap = bss_desc->cap_info_bitmap;
1101
1102         tmp_cap &= CAPINFO_MASK;
1103
1104         dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: tmp_cap=%4X"
1105                         " CAPINFO_MASK=%4lX\n", tmp_cap, CAPINFO_MASK);
1106
1107         /* Information on BSSID descriptor passed to FW */
1108         dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: BSSID = %pM, SSID = %s\n",
1109                                 adhoc_join->bss_descriptor.bssid,
1110                                 adhoc_join->bss_descriptor.ssid);
1111
1112         for (i = 0; bss_desc->supported_rates[i] &&
1113                         i < MWIFIEX_SUPPORTED_RATES;
1114                         i++)
1115                         ;
1116         rates_size = i;
1117
1118         /* Copy Data Rates from the Rates recorded in scan response */
1119         memset(adhoc_join->bss_descriptor.data_rates, 0,
1120                sizeof(adhoc_join->bss_descriptor.data_rates));
1121         memcpy(adhoc_join->bss_descriptor.data_rates,
1122                bss_desc->supported_rates, rates_size);
1123
1124         /* Copy the adhoc join rates into Current BSS state structure */
1125         priv->curr_bss_params.num_of_rates = rates_size;
1126         memcpy(&priv->curr_bss_params.data_rates, bss_desc->supported_rates,
1127                rates_size);
1128
1129         /* Copy the channel information */
1130         priv->curr_bss_params.bss_descriptor.channel = bss_desc->channel;
1131         priv->curr_bss_params.band = (u8) bss_desc->bss_band;
1132
1133         if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_ENABLED
1134             || priv->sec_info.wpa_enabled)
1135                 tmp_cap |= WLAN_CAPABILITY_PRIVACY;
1136
1137         if (IS_SUPPORT_MULTI_BANDS(priv->adapter)) {
1138                 /* Append a channel TLV */
1139                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
1140                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
1141                 chan_tlv->header.len =
1142                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
1143
1144                 memset(chan_tlv->chan_scan_param, 0x00,
1145                        sizeof(struct mwifiex_chan_scan_param_set));
1146                 chan_tlv->chan_scan_param[0].chan_number =
1147                         (bss_desc->phy_param_set.ds_param_set.current_chan);
1148                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: TLV Chan = %d\n",
1149                        chan_tlv->chan_scan_param[0].chan_number);
1150
1151                 chan_tlv->chan_scan_param[0].radio_type =
1152                         mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
1153
1154                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: TLV Band = %d\n",
1155                        chan_tlv->chan_scan_param[0].radio_type);
1156                 pos += sizeof(chan_tlv->header) +
1157                         sizeof(struct mwifiex_chan_scan_param_set);
1158                 cmd_append_size += sizeof(chan_tlv->header) +
1159                         sizeof(struct mwifiex_chan_scan_param_set);
1160         }
1161
1162         if (priv->sec_info.wpa_enabled)
1163                 rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
1164         if (rsn_ie_len == -1)
1165                 return -1;
1166         cmd_append_size += rsn_ie_len;
1167
1168         if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info))
1169                 cmd_append_size += mwifiex_cmd_append_11n_tlv(priv,
1170                         bss_desc, &pos);
1171
1172         /* Append vendor specific IE TLV */
1173         cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,
1174                         MWIFIEX_VSIE_MASK_ADHOC, &pos);
1175
1176         cmd->size = cpu_to_le16((u16)
1177                             (sizeof(struct host_cmd_ds_802_11_ad_hoc_join)
1178                              + S_DS_GEN + cmd_append_size));
1179
1180         adhoc_join->bss_descriptor.cap_info_bitmap = cpu_to_le16(tmp_cap);
1181
1182         return ret;
1183 }
1184
1185 /*
1186  * This function handles the command response of ad-hoc start and
1187  * ad-hoc join.
1188  *
1189  * The function generates a device-connected event to notify
1190  * the applications, in case of successful ad-hoc start/join, and
1191  * saves the beacon buffer.
1192  */
1193 int mwifiex_ret_802_11_ad_hoc(struct mwifiex_private *priv,
1194                               struct host_cmd_ds_command *resp, void *wq_buf)
1195 {
1196         int ret = 0;
1197         struct mwifiex_wait_queue *wait_queue =
1198                 (struct mwifiex_wait_queue *) wq_buf;
1199         struct host_cmd_ds_802_11_ad_hoc_result *adhoc_result;
1200         struct mwifiex_bssdescriptor *bss_desc;
1201         u16 command = le16_to_cpu(resp->command);
1202         u16 result = le16_to_cpu(resp->result);
1203
1204         adhoc_result = &resp->params.adhoc_result;
1205
1206         bss_desc = priv->attempted_bss_desc;
1207
1208         /* Join result code 0 --> SUCCESS */
1209         if (result) {
1210                 dev_err(priv->adapter->dev, "ADHOC_RESP: failed\n");
1211                 if (priv->media_connected)
1212                         mwifiex_reset_connect_state(priv);
1213
1214                 memset(&priv->curr_bss_params.bss_descriptor,
1215                        0x00, sizeof(struct mwifiex_bssdescriptor));
1216
1217                 ret = -1;
1218                 goto done;
1219         }
1220
1221         /* Send a Media Connected event, according to the Spec */
1222         priv->media_connected = true;
1223
1224         if (command == HostCmd_CMD_802_11_AD_HOC_START) {
1225                 dev_dbg(priv->adapter->dev, "info: ADHOC_S_RESP %s\n",
1226                                 bss_desc->ssid.ssid);
1227
1228                 /* Update the created network descriptor with the new BSSID */
1229                 memcpy(bss_desc->mac_address,
1230                        adhoc_result->bssid, ETH_ALEN);
1231
1232                 priv->adhoc_state = ADHOC_STARTED;
1233         } else {
1234                 /*
1235                  * Now the join cmd should be successful.
1236                  * If BSSID has changed use SSID to compare instead of BSSID
1237                  */
1238                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_RESP %s\n",
1239                                 bss_desc->ssid.ssid);
1240
1241                 /*
1242                  * Make a copy of current BSSID descriptor, only needed for
1243                  * join since the current descriptor is already being used
1244                  * for adhoc start
1245                  */
1246                 memcpy(&priv->curr_bss_params.bss_descriptor,
1247                        bss_desc, sizeof(struct mwifiex_bssdescriptor));
1248
1249                 priv->adhoc_state = ADHOC_JOINED;
1250         }
1251
1252         dev_dbg(priv->adapter->dev, "info: ADHOC_RESP: channel = %d\n",
1253                                 priv->adhoc_channel);
1254         dev_dbg(priv->adapter->dev, "info: ADHOC_RESP: BSSID = %pM\n",
1255                priv->curr_bss_params.bss_descriptor.mac_address);
1256
1257         if (!netif_carrier_ok(priv->netdev))
1258                 netif_carrier_on(priv->netdev);
1259         if (netif_queue_stopped(priv->netdev))
1260                 netif_wake_queue(priv->netdev);
1261
1262         mwifiex_save_curr_bcn(priv);
1263
1264 done:
1265         /* Need to indicate IOCTL complete */
1266         if (wait_queue) {
1267                 if (ret)
1268                         wait_queue->status = MWIFIEX_ERROR_ASSOC_FAIL;
1269                 else
1270                         wait_queue->status = MWIFIEX_ERROR_NO_ERROR;
1271
1272         }
1273
1274         return ret;
1275 }
1276
1277 /*
1278  * This function associates to a specific BSS discovered in a scan.
1279  *
1280  * It clears any past association response stored for application
1281  * retrieval and calls the command preparation routine to send the
1282  * command to firmware.
1283  */
1284 int mwifiex_associate(struct mwifiex_private *priv,
1285                       void *wait_queue, struct mwifiex_bssdescriptor *bss_desc)
1286 {
1287         int ret = 0;
1288         u8 current_bssid[ETH_ALEN];
1289
1290         /* Return error if the adapter or table entry is not marked as infra */
1291         if ((priv->bss_mode != MWIFIEX_BSS_MODE_INFRA) ||
1292             (bss_desc->bss_mode != MWIFIEX_BSS_MODE_INFRA))
1293                 return -1;
1294
1295         memcpy(&current_bssid,
1296                &priv->curr_bss_params.bss_descriptor.mac_address,
1297                sizeof(current_bssid));
1298
1299         /* Clear any past association response stored for application
1300            retrieval */
1301         priv->assoc_rsp_size = 0;
1302
1303         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_ASSOCIATE,
1304                                   HostCmd_ACT_GEN_SET, 0, wait_queue,
1305                                   bss_desc);
1306
1307         return ret;
1308 }
1309
1310 /*
1311  * This function starts an ad-hoc network.
1312  *
1313  * It calls the command preparation routine to send the command to firmware.
1314  */
1315 int
1316 mwifiex_adhoc_start(struct mwifiex_private *priv,
1317                     void *wait_queue, struct mwifiex_802_11_ssid *adhoc_ssid)
1318 {
1319         int ret = 0;
1320
1321         dev_dbg(priv->adapter->dev, "info: Adhoc Channel = %d\n",
1322                 priv->adhoc_channel);
1323         dev_dbg(priv->adapter->dev, "info: curr_bss_params.channel = %d\n",
1324                priv->curr_bss_params.bss_descriptor.channel);
1325         dev_dbg(priv->adapter->dev, "info: curr_bss_params.band = %d\n",
1326                priv->curr_bss_params.band);
1327
1328         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_AD_HOC_START,
1329                                   HostCmd_ACT_GEN_SET, 0, wait_queue,
1330                                   adhoc_ssid);
1331
1332         return ret;
1333 }
1334
1335 /*
1336  * This function joins an ad-hoc network found in a previous scan.
1337  *
1338  * It calls the command preparation routine to send the command to firmware,
1339  * if already not connected to the requested SSID.
1340  */
1341 int mwifiex_adhoc_join(struct mwifiex_private *priv,
1342                        void *wait_queue, struct mwifiex_bssdescriptor *bss_desc)
1343 {
1344         int ret = 0;
1345
1346         dev_dbg(priv->adapter->dev, "info: adhoc join: curr_bss ssid =%s\n",
1347                priv->curr_bss_params.bss_descriptor.ssid.ssid);
1348         dev_dbg(priv->adapter->dev, "info: adhoc join: curr_bss ssid_len =%u\n",
1349                priv->curr_bss_params.bss_descriptor.ssid.ssid_len);
1350         dev_dbg(priv->adapter->dev, "info: adhoc join: ssid =%s\n",
1351                 bss_desc->ssid.ssid);
1352         dev_dbg(priv->adapter->dev, "info: adhoc join: ssid_len =%u\n",
1353                bss_desc->ssid.ssid_len);
1354
1355         /* Check if the requested SSID is already joined */
1356         if (priv->curr_bss_params.bss_descriptor.ssid.ssid_len &&
1357             !mwifiex_ssid_cmp(&bss_desc->ssid,
1358                               &priv->curr_bss_params.bss_descriptor.ssid) &&
1359             (priv->curr_bss_params.bss_descriptor.bss_mode ==
1360              MWIFIEX_BSS_MODE_IBSS)) {
1361                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: new ad-hoc SSID"
1362                         " is the same as current; not attempting to re-join\n");
1363                 return -1;
1364         }
1365
1366         dev_dbg(priv->adapter->dev, "info: curr_bss_params.channel = %d\n",
1367                priv->curr_bss_params.bss_descriptor.channel);
1368         dev_dbg(priv->adapter->dev, "info: curr_bss_params.band = %c\n",
1369                priv->curr_bss_params.band);
1370
1371         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_AD_HOC_JOIN,
1372                                   HostCmd_ACT_GEN_SET, 0, wait_queue,
1373                                   bss_desc);
1374
1375         return ret;
1376 }
1377
1378 /*
1379  * This function deauthenticates/disconnects from infra network by sending
1380  * deauthentication request.
1381  */
1382 static int mwifiex_deauthenticate_infra(struct mwifiex_private *priv,
1383                                         struct mwifiex_wait_queue *wait,
1384                                         u8 *mac)
1385 {
1386         u8 mac_address[ETH_ALEN];
1387         int ret = 0;
1388         u8 zero_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
1389
1390         if (mac) {
1391                 if (!memcmp(mac, zero_mac, sizeof(zero_mac)))
1392                         memcpy((u8 *) &mac_address,
1393                                (u8 *) &priv->curr_bss_params.bss_descriptor.
1394                                mac_address, ETH_ALEN);
1395                 else
1396                         memcpy((u8 *) &mac_address, (u8 *) mac, ETH_ALEN);
1397         } else {
1398                 memcpy((u8 *) &mac_address, (u8 *) &priv->curr_bss_params.
1399                        bss_descriptor.mac_address, ETH_ALEN);
1400         }
1401
1402         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_DEAUTHENTICATE,
1403                                   HostCmd_ACT_GEN_SET, 0, wait, &mac_address);
1404
1405         if (!ret && wait)
1406                 ret = -EINPROGRESS;
1407
1408         return ret;
1409 }
1410
1411 /*
1412  * This function deauthenticates/disconnects from a BSS.
1413  *
1414  * In case of infra made, it sends deauthentication request, and
1415  * in case of ad-hoc mode, a stop network request is sent to the firmware.
1416  */
1417 int mwifiex_deauthenticate(struct mwifiex_private *priv,
1418                            struct mwifiex_wait_queue *wait, u8 *mac)
1419 {
1420         int ret = 0;
1421
1422         if (priv->media_connected) {
1423                 if (priv->bss_mode == MWIFIEX_BSS_MODE_INFRA) {
1424                         ret = mwifiex_deauthenticate_infra(priv, wait, mac);
1425                 } else if (priv->bss_mode == MWIFIEX_BSS_MODE_IBSS) {
1426                         ret = mwifiex_prepare_cmd(priv,
1427                                         HostCmd_CMD_802_11_AD_HOC_STOP,
1428                                         HostCmd_ACT_GEN_SET, 0, wait, NULL);
1429
1430                         if (!ret && wait)
1431                                 ret = -EINPROGRESS;
1432                 }
1433         }
1434
1435         return ret;
1436 }
1437
1438 /*
1439  * This function converts band to radio type used in channel TLV.
1440  */
1441 u8
1442 mwifiex_band_to_radio_type(u8 band)
1443 {
1444         u8 ret_radio_type;
1445
1446         switch (band) {
1447         case BAND_A:
1448         case BAND_AN:
1449         case BAND_A | BAND_AN:
1450                 ret_radio_type = HostCmd_SCAN_RADIO_TYPE_A;
1451                 break;
1452         case BAND_B:
1453         case BAND_G:
1454         case BAND_B | BAND_G:
1455         default:
1456                 ret_radio_type = HostCmd_SCAN_RADIO_TYPE_BG;
1457                 break;
1458         }
1459
1460         return ret_radio_type;
1461 }