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