]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/mwifiex/join.c
tcp: md5: remove one indirection level in tcp_md5sig_pool
[karo-tx-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         __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 appends a WAPI IE.
228  *
229  * This function is called from the network join command preparation routine.
230  *
231  * If the IE buffer has been setup by the application, this routine appends
232  * the buffer as a WAPI TLV type to the request.
233  */
234 static int
235 mwifiex_cmd_append_wapi_ie(struct mwifiex_private *priv, u8 **buffer)
236 {
237         int retLen = 0;
238         struct mwifiex_ie_types_header ie_header;
239
240         /* Null Checks */
241         if (buffer == NULL)
242                 return 0;
243         if (*buffer == NULL)
244                 return 0;
245
246         /*
247          * If there is a wapi ie buffer setup, append it to the return
248          *   parameter buffer pointer.
249          */
250         if (priv->wapi_ie_len) {
251                 dev_dbg(priv->adapter->dev, "cmd: append wapi ie %d to %p\n",
252                                 priv->wapi_ie_len, *buffer);
253
254                 /* Wrap the generic IE buffer with a pass through TLV type */
255                 ie_header.type = cpu_to_le16(TLV_TYPE_WAPI_IE);
256                 ie_header.len = cpu_to_le16(priv->wapi_ie_len);
257                 memcpy(*buffer, &ie_header, sizeof(ie_header));
258
259                 /* Increment the return size and the return buffer pointer
260                    param */
261                 *buffer += sizeof(ie_header);
262                 retLen += sizeof(ie_header);
263
264                 /* Copy the wapi IE buffer to the output buffer, advance
265                    pointer */
266                 memcpy(*buffer, priv->wapi_ie, priv->wapi_ie_len);
267
268                 /* Increment the return size and the return buffer pointer
269                    param */
270                 *buffer += priv->wapi_ie_len;
271                 retLen += priv->wapi_ie_len;
272
273         }
274         /* return the length appended to the buffer */
275         return retLen;
276 }
277
278 /*
279  * This function appends rsn ie tlv for wpa/wpa2 security modes.
280  * It is called from the network join command preparation routine.
281  */
282 static int mwifiex_append_rsn_ie_wpa_wpa2(struct mwifiex_private *priv,
283                                           u8 **buffer)
284 {
285         struct mwifiex_ie_types_rsn_param_set *rsn_ie_tlv;
286         int rsn_ie_len;
287
288         if (!buffer || !(*buffer))
289                 return 0;
290
291         rsn_ie_tlv = (struct mwifiex_ie_types_rsn_param_set *) (*buffer);
292         rsn_ie_tlv->header.type = cpu_to_le16((u16) priv->wpa_ie[0]);
293         rsn_ie_tlv->header.type = cpu_to_le16(
294                                  le16_to_cpu(rsn_ie_tlv->header.type) & 0x00FF);
295         rsn_ie_tlv->header.len = cpu_to_le16((u16) priv->wpa_ie[1]);
296         rsn_ie_tlv->header.len = cpu_to_le16(le16_to_cpu(rsn_ie_tlv->header.len)
297                                                         & 0x00FF);
298         if (le16_to_cpu(rsn_ie_tlv->header.len) <= (sizeof(priv->wpa_ie) - 2))
299                 memcpy(rsn_ie_tlv->rsn_ie, &priv->wpa_ie[2],
300                                         le16_to_cpu(rsn_ie_tlv->header.len));
301         else
302                 return -1;
303
304         rsn_ie_len = sizeof(rsn_ie_tlv->header) +
305                                         le16_to_cpu(rsn_ie_tlv->header.len);
306         *buffer += rsn_ie_len;
307
308         return rsn_ie_len;
309 }
310
311 /*
312  * This function prepares command for association.
313  *
314  * This sets the following parameters -
315  *      - Peer MAC address
316  *      - Listen interval
317  *      - Beacon interval
318  *      - Capability information
319  *
320  * ...and the following TLVs, as required -
321  *      - SSID TLV
322  *      - PHY TLV
323  *      - SS TLV
324  *      - Rates TLV
325  *      - Authentication TLV
326  *      - Channel TLV
327  *      - WPA/WPA2 IE
328  *      - 11n TLV
329  *      - Vendor specific TLV
330  *      - WMM TLV
331  *      - WAPI IE
332  *      - Generic IE
333  *      - TSF TLV
334  *
335  * Preparation also includes -
336  *      - Setting command ID and proper size
337  *      - Ensuring correct endian-ness
338  */
339 int mwifiex_cmd_802_11_associate(struct mwifiex_private *priv,
340                                  struct host_cmd_ds_command *cmd,
341                                  struct mwifiex_bssdescriptor *bss_desc)
342 {
343         struct host_cmd_ds_802_11_associate *assoc = &cmd->params.associate;
344         struct mwifiex_ie_types_ssid_param_set *ssid_tlv;
345         struct mwifiex_ie_types_phy_param_set *phy_tlv;
346         struct mwifiex_ie_types_ss_param_set *ss_tlv;
347         struct mwifiex_ie_types_rates_param_set *rates_tlv;
348         struct mwifiex_ie_types_auth_type *auth_tlv;
349         struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
350         u8 rates[MWIFIEX_SUPPORTED_RATES];
351         u32 rates_size;
352         u16 tmp_cap;
353         u8 *pos;
354         int rsn_ie_len = 0;
355
356         pos = (u8 *) assoc;
357
358         mwifiex_cfg_tx_buf(priv, bss_desc);
359
360         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_ASSOCIATE);
361
362         /* Save so we know which BSS Desc to use in the response handler */
363         priv->attempted_bss_desc = bss_desc;
364
365         memcpy(assoc->peer_sta_addr,
366                bss_desc->mac_address, sizeof(assoc->peer_sta_addr));
367         pos += sizeof(assoc->peer_sta_addr);
368
369         /* Set the listen interval */
370         assoc->listen_interval = cpu_to_le16(priv->listen_interval);
371         /* Set the beacon period */
372         assoc->beacon_period = cpu_to_le16(bss_desc->beacon_period);
373
374         pos += sizeof(assoc->cap_info_bitmap);
375         pos += sizeof(assoc->listen_interval);
376         pos += sizeof(assoc->beacon_period);
377         pos += sizeof(assoc->dtim_period);
378
379         ssid_tlv = (struct mwifiex_ie_types_ssid_param_set *) pos;
380         ssid_tlv->header.type = cpu_to_le16(WLAN_EID_SSID);
381         ssid_tlv->header.len = cpu_to_le16((u16) bss_desc->ssid.ssid_len);
382         memcpy(ssid_tlv->ssid, bss_desc->ssid.ssid,
383                 le16_to_cpu(ssid_tlv->header.len));
384         pos += sizeof(ssid_tlv->header) + le16_to_cpu(ssid_tlv->header.len);
385
386         phy_tlv = (struct mwifiex_ie_types_phy_param_set *) pos;
387         phy_tlv->header.type = cpu_to_le16(WLAN_EID_DS_PARAMS);
388         phy_tlv->header.len = cpu_to_le16(sizeof(phy_tlv->fh_ds.ds_param_set));
389         memcpy(&phy_tlv->fh_ds.ds_param_set,
390                &bss_desc->phy_param_set.ds_param_set.current_chan,
391                sizeof(phy_tlv->fh_ds.ds_param_set));
392         pos += sizeof(phy_tlv->header) + le16_to_cpu(phy_tlv->header.len);
393
394         ss_tlv = (struct mwifiex_ie_types_ss_param_set *) pos;
395         ss_tlv->header.type = cpu_to_le16(WLAN_EID_CF_PARAMS);
396         ss_tlv->header.len = cpu_to_le16(sizeof(ss_tlv->cf_ibss.cf_param_set));
397         pos += sizeof(ss_tlv->header) + le16_to_cpu(ss_tlv->header.len);
398
399         /* Get the common rates supported between the driver and the BSS Desc */
400         if (mwifiex_setup_rates_from_bssdesc
401             (priv, bss_desc, rates, &rates_size))
402                 return -1;
403
404         /* Save the data rates into Current BSS state structure */
405         priv->curr_bss_params.num_of_rates = rates_size;
406         memcpy(&priv->curr_bss_params.data_rates, rates, rates_size);
407
408         /* Setup the Rates TLV in the association command */
409         rates_tlv = (struct mwifiex_ie_types_rates_param_set *) pos;
410         rates_tlv->header.type = cpu_to_le16(WLAN_EID_SUPP_RATES);
411         rates_tlv->header.len = cpu_to_le16((u16) rates_size);
412         memcpy(rates_tlv->rates, rates, rates_size);
413         pos += sizeof(rates_tlv->header) + rates_size;
414         dev_dbg(priv->adapter->dev, "info: ASSOC_CMD: rates size = %d\n",
415                                         rates_size);
416
417         /* Add the Authentication type to be used for Auth frames */
418         auth_tlv = (struct mwifiex_ie_types_auth_type *) pos;
419         auth_tlv->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
420         auth_tlv->header.len = cpu_to_le16(sizeof(auth_tlv->auth_type));
421         if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_ENABLED)
422                 auth_tlv->auth_type = cpu_to_le16(
423                                 (u16) priv->sec_info.authentication_mode);
424         else
425                 auth_tlv->auth_type = cpu_to_le16(NL80211_AUTHTYPE_OPEN_SYSTEM);
426
427         pos += sizeof(auth_tlv->header) + le16_to_cpu(auth_tlv->header.len);
428
429         if (IS_SUPPORT_MULTI_BANDS(priv->adapter)
430             && !(ISSUPP_11NENABLED(priv->adapter->fw_cap_info)
431                 && (!bss_desc->disable_11n)
432                  && (priv->adapter->config_bands & BAND_GN
433                      || priv->adapter->config_bands & BAND_AN)
434                  && (bss_desc->bcn_ht_cap)
435             )
436                 ) {
437                 /* Append a channel TLV for the channel the attempted AP was
438                    found on */
439                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
440                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
441                 chan_tlv->header.len =
442                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
443
444                 memset(chan_tlv->chan_scan_param, 0x00,
445                        sizeof(struct mwifiex_chan_scan_param_set));
446                 chan_tlv->chan_scan_param[0].chan_number =
447                         (bss_desc->phy_param_set.ds_param_set.current_chan);
448                 dev_dbg(priv->adapter->dev, "info: Assoc: TLV Chan = %d\n",
449                        chan_tlv->chan_scan_param[0].chan_number);
450
451                 chan_tlv->chan_scan_param[0].radio_type =
452                         mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
453
454                 dev_dbg(priv->adapter->dev, "info: Assoc: TLV Band = %d\n",
455                        chan_tlv->chan_scan_param[0].radio_type);
456                 pos += sizeof(chan_tlv->header) +
457                         sizeof(struct mwifiex_chan_scan_param_set);
458         }
459
460         if (!priv->wps.session_enable) {
461                 if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
462                         rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
463
464                 if (rsn_ie_len == -1)
465                         return -1;
466         }
467
468         if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info)
469                 && (!bss_desc->disable_11n)
470             && (priv->adapter->config_bands & BAND_GN
471                 || priv->adapter->config_bands & BAND_AN))
472                 mwifiex_cmd_append_11n_tlv(priv, bss_desc, &pos);
473
474         /* Append vendor specific IE TLV */
475         mwifiex_cmd_append_vsie_tlv(priv, MWIFIEX_VSIE_MASK_ASSOC, &pos);
476
477         mwifiex_wmm_process_association_req(priv, &pos, &bss_desc->wmm_ie,
478                                             bss_desc->bcn_ht_cap);
479         if (priv->sec_info.wapi_enabled && priv->wapi_ie_len)
480                 mwifiex_cmd_append_wapi_ie(priv, &pos);
481
482
483         mwifiex_cmd_append_generic_ie(priv, &pos);
484
485         mwifiex_cmd_append_tsf_tlv(priv, &pos, bss_desc);
486
487         cmd->size = cpu_to_le16((u16) (pos - (u8 *) assoc) + S_DS_GEN);
488
489         /* Set the Capability info at last */
490         tmp_cap = bss_desc->cap_info_bitmap;
491
492         if (priv->adapter->config_bands == BAND_B)
493                 tmp_cap &= ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
494
495         tmp_cap &= CAPINFO_MASK;
496         dev_dbg(priv->adapter->dev, "info: ASSOC_CMD: tmp_cap=%4X CAPINFO_MASK=%4lX\n",
497                tmp_cap, CAPINFO_MASK);
498         assoc->cap_info_bitmap = cpu_to_le16(tmp_cap);
499
500         return 0;
501 }
502
503 /*
504  * Association firmware command response handler
505  *
506  * The response buffer for the association command has the following
507  * memory layout.
508  *
509  * For cases where an association response was not received (indicated
510  * by the CapInfo and AId field):
511  *
512  *     .------------------------------------------------------------.
513  *     |  Header(4 * sizeof(t_u16)):  Standard command response hdr |
514  *     .------------------------------------------------------------.
515  *     |  cap_info/Error Return(t_u16):                             |
516  *     |           0xFFFF(-1): Internal error                       |
517  *     |           0xFFFE(-2): Authentication unhandled message     |
518  *     |           0xFFFD(-3): Authentication refused               |
519  *     |           0xFFFC(-4): Timeout waiting for AP response      |
520  *     .------------------------------------------------------------.
521  *     |  status_code(t_u16):                                       |
522  *     |        If cap_info is -1:                                  |
523  *     |           An internal firmware failure prevented the       |
524  *     |           command from being processed.  The status_code   |
525  *     |           will be set to 1.                                |
526  *     |                                                            |
527  *     |        If cap_info is -2:                                  |
528  *     |           An authentication frame was received but was     |
529  *     |           not handled by the firmware.  IEEE Status        |
530  *     |           code for the failure is returned.                |
531  *     |                                                            |
532  *     |        If cap_info is -3:                                  |
533  *     |           An authentication frame was received and the     |
534  *     |           status_code is the IEEE Status reported in the   |
535  *     |           response.                                        |
536  *     |                                                            |
537  *     |        If cap_info is -4:                                  |
538  *     |           (1) Association response timeout                 |
539  *     |           (2) Authentication response timeout              |
540  *     .------------------------------------------------------------.
541  *     |  a_id(t_u16): 0xFFFF                                       |
542  *     .------------------------------------------------------------.
543  *
544  *
545  * For cases where an association response was received, the IEEE
546  * standard association response frame is returned:
547  *
548  *     .------------------------------------------------------------.
549  *     |  Header(4 * sizeof(t_u16)):  Standard command response hdr |
550  *     .------------------------------------------------------------.
551  *     |  cap_info(t_u16): IEEE Capability                          |
552  *     .------------------------------------------------------------.
553  *     |  status_code(t_u16): IEEE Status Code                      |
554  *     .------------------------------------------------------------.
555  *     |  a_id(t_u16): IEEE Association ID                          |
556  *     .------------------------------------------------------------.
557  *     |  IEEE IEs(variable): Any received IEs comprising the       |
558  *     |                      remaining portion of a received       |
559  *     |                      association response frame.           |
560  *     .------------------------------------------------------------.
561  *
562  * For simplistic handling, the status_code field can be used to determine
563  * an association success (0) or failure (non-zero).
564  */
565 int mwifiex_ret_802_11_associate(struct mwifiex_private *priv,
566                              struct host_cmd_ds_command *resp)
567 {
568         struct mwifiex_adapter *adapter = priv->adapter;
569         int ret = 0;
570         struct ieee_types_assoc_rsp *assoc_rsp;
571         struct mwifiex_bssdescriptor *bss_desc;
572         u8 enable_data = true;
573
574         assoc_rsp = (struct ieee_types_assoc_rsp *) &resp->params;
575
576         priv->assoc_rsp_size = min(le16_to_cpu(resp->size) - S_DS_GEN,
577                                      sizeof(priv->assoc_rsp_buf));
578
579         memcpy(priv->assoc_rsp_buf, &resp->params, priv->assoc_rsp_size);
580
581         if (le16_to_cpu(assoc_rsp->status_code)) {
582                 priv->adapter->dbg.num_cmd_assoc_failure++;
583                 dev_err(priv->adapter->dev, "ASSOC_RESP: association failed, "
584                        "status code = %d, error = 0x%x, a_id = 0x%x\n",
585                        le16_to_cpu(assoc_rsp->status_code),
586                        le16_to_cpu(assoc_rsp->cap_info_bitmap),
587                        le16_to_cpu(assoc_rsp->a_id));
588
589                 ret = -1;
590                 goto done;
591         }
592
593         /* Send a Media Connected event, according to the Spec */
594         priv->media_connected = true;
595
596         priv->adapter->ps_state = PS_STATE_AWAKE;
597         priv->adapter->pps_uapsd_mode = false;
598         priv->adapter->tx_lock_flag = false;
599
600         /* Set the attempted BSSID Index to current */
601         bss_desc = priv->attempted_bss_desc;
602
603         dev_dbg(priv->adapter->dev, "info: ASSOC_RESP: %s\n",
604                                                 bss_desc->ssid.ssid);
605
606         /* Make a copy of current BSSID descriptor */
607         memcpy(&priv->curr_bss_params.bss_descriptor,
608                bss_desc, sizeof(struct mwifiex_bssdescriptor));
609
610         /* Update curr_bss_params */
611         priv->curr_bss_params.bss_descriptor.channel
612                 = bss_desc->phy_param_set.ds_param_set.current_chan;
613
614         priv->curr_bss_params.band = (u8) bss_desc->bss_band;
615
616         if (bss_desc->wmm_ie.vend_hdr.element_id == WLAN_EID_VENDOR_SPECIFIC)
617                 priv->curr_bss_params.wmm_enabled = true;
618         else
619                 priv->curr_bss_params.wmm_enabled = false;
620
621         if ((priv->wmm_required || bss_desc->bcn_ht_cap)
622                         && priv->curr_bss_params.wmm_enabled)
623                 priv->wmm_enabled = true;
624         else
625                 priv->wmm_enabled = false;
626
627         priv->curr_bss_params.wmm_uapsd_enabled = false;
628
629         if (priv->wmm_enabled)
630                 priv->curr_bss_params.wmm_uapsd_enabled
631                         = ((bss_desc->wmm_ie.qos_info_bitmap &
632                                 IEEE80211_WMM_IE_AP_QOSINFO_UAPSD) ? 1 : 0);
633
634         dev_dbg(priv->adapter->dev, "info: ASSOC_RESP: curr_pkt_filter is %#x\n",
635                priv->curr_pkt_filter);
636         if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
637                 priv->wpa_is_gtk_set = false;
638
639         if (priv->wmm_enabled) {
640                 /* Don't re-enable carrier until we get the WMM_GET_STATUS
641                    event */
642                 enable_data = false;
643         } else {
644                 /* Since WMM is not enabled, setup the queues with the
645                    defaults */
646                 mwifiex_wmm_setup_queue_priorities(priv, NULL);
647                 mwifiex_wmm_setup_ac_downgrade(priv);
648         }
649
650         if (enable_data)
651                 dev_dbg(priv->adapter->dev,
652                         "info: post association, re-enabling data flow\n");
653
654         /* Reset SNR/NF/RSSI values */
655         priv->data_rssi_last = 0;
656         priv->data_nf_last = 0;
657         priv->data_rssi_avg = 0;
658         priv->data_nf_avg = 0;
659         priv->bcn_rssi_last = 0;
660         priv->bcn_nf_last = 0;
661         priv->bcn_rssi_avg = 0;
662         priv->bcn_nf_avg = 0;
663         priv->rxpd_rate = 0;
664         priv->rxpd_htinfo = 0;
665
666         mwifiex_save_curr_bcn(priv);
667
668         priv->adapter->dbg.num_cmd_assoc_success++;
669
670         dev_dbg(priv->adapter->dev, "info: ASSOC_RESP: associated\n");
671
672         /* Add the ra_list here for infra mode as there will be only 1 ra
673            always */
674         mwifiex_ralist_add(priv,
675                            priv->curr_bss_params.bss_descriptor.mac_address);
676
677         if (!netif_carrier_ok(priv->netdev))
678                 netif_carrier_on(priv->netdev);
679         if (netif_queue_stopped(priv->netdev))
680                 netif_wake_queue(priv->netdev);
681
682         if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
683                 priv->scan_block = true;
684
685 done:
686         /* Need to indicate IOCTL complete */
687         if (adapter->curr_cmd->wait_q_enabled) {
688                 if (ret)
689                         adapter->cmd_wait_q.status = -1;
690                 else
691                         adapter->cmd_wait_q.status = 0;
692         }
693
694         return ret;
695 }
696
697 /*
698  * This function prepares command for ad-hoc start.
699  *
700  * Driver will fill up SSID, BSS mode, IBSS parameters, physical
701  * parameters, probe delay, and capability information. Firmware
702  * will fill up beacon period, basic rates and operational rates.
703  *
704  * In addition, the following TLVs are added -
705  *      - Channel TLV
706  *      - Vendor specific IE
707  *      - WPA/WPA2 IE
708  *      - HT Capabilities IE
709  *      - HT Information IE
710  *
711  * Preparation also includes -
712  *      - Setting command ID and proper size
713  *      - Ensuring correct endian-ness
714  */
715 int
716 mwifiex_cmd_802_11_ad_hoc_start(struct mwifiex_private *priv,
717                                 struct host_cmd_ds_command *cmd,
718                                 struct mwifiex_802_11_ssid *req_ssid)
719 {
720         int rsn_ie_len = 0;
721         struct mwifiex_adapter *adapter = priv->adapter;
722         struct host_cmd_ds_802_11_ad_hoc_start *adhoc_start =
723                 &cmd->params.adhoc_start;
724         struct mwifiex_bssdescriptor *bss_desc;
725         u32 cmd_append_size = 0;
726         u32 i;
727         u16 tmp_cap;
728         uint16_t ht_cap_info;
729         struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
730
731         struct mwifiex_ie_types_htcap *ht_cap;
732         struct mwifiex_ie_types_htinfo *ht_info;
733         u8 *pos = (u8 *) adhoc_start +
734                         sizeof(struct host_cmd_ds_802_11_ad_hoc_start);
735
736         if (!adapter)
737                 return -1;
738
739         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_START);
740
741         bss_desc = &priv->curr_bss_params.bss_descriptor;
742         priv->attempted_bss_desc = bss_desc;
743
744         /*
745          * Fill in the parameters for 2 data structures:
746          *   1. struct host_cmd_ds_802_11_ad_hoc_start command
747          *   2. bss_desc
748          * Driver will fill up SSID, bss_mode,IBSS param, Physical Param,
749          * probe delay, and Cap info.
750          * Firmware will fill up beacon period, Basic rates
751          * and operational rates.
752          */
753
754         memset(adhoc_start->ssid, 0, IEEE80211_MAX_SSID_LEN);
755
756         memcpy(adhoc_start->ssid, req_ssid->ssid, req_ssid->ssid_len);
757
758         dev_dbg(adapter->dev, "info: ADHOC_S_CMD: SSID = %s\n",
759                                 adhoc_start->ssid);
760
761         memset(bss_desc->ssid.ssid, 0, IEEE80211_MAX_SSID_LEN);
762         memcpy(bss_desc->ssid.ssid, req_ssid->ssid, req_ssid->ssid_len);
763
764         bss_desc->ssid.ssid_len = req_ssid->ssid_len;
765
766         /* Set the BSS mode */
767         adhoc_start->bss_mode = HostCmd_BSS_MODE_IBSS;
768         bss_desc->bss_mode = NL80211_IFTYPE_ADHOC;
769         adhoc_start->beacon_period = cpu_to_le16(priv->beacon_period);
770         bss_desc->beacon_period = priv->beacon_period;
771
772         /* Set Physical param set */
773 /* Parameter IE Id */
774 #define DS_PARA_IE_ID   3
775 /* Parameter IE length */
776 #define DS_PARA_IE_LEN  1
777
778         adhoc_start->phy_param_set.ds_param_set.element_id = DS_PARA_IE_ID;
779         adhoc_start->phy_param_set.ds_param_set.len = DS_PARA_IE_LEN;
780
781         if (!mwifiex_get_cfp_by_band_and_channel_from_cfg80211
782                         (priv, adapter->adhoc_start_band, (u16)
783                                 priv->adhoc_channel)) {
784                 struct mwifiex_chan_freq_power *cfp;
785                 cfp = mwifiex_get_cfp_by_band_and_channel_from_cfg80211(priv,
786                                 adapter->adhoc_start_band, FIRST_VALID_CHANNEL);
787                 if (cfp)
788                         priv->adhoc_channel = (u8) cfp->channel;
789         }
790
791         if (!priv->adhoc_channel) {
792                 dev_err(adapter->dev, "ADHOC_S_CMD: adhoc_channel cannot be 0\n");
793                 return -1;
794         }
795
796         dev_dbg(adapter->dev, "info: ADHOC_S_CMD: creating ADHOC on channel %d\n",
797                                 priv->adhoc_channel);
798
799         priv->curr_bss_params.bss_descriptor.channel = priv->adhoc_channel;
800         priv->curr_bss_params.band = adapter->adhoc_start_band;
801
802         bss_desc->channel = priv->adhoc_channel;
803         adhoc_start->phy_param_set.ds_param_set.current_chan =
804                 priv->adhoc_channel;
805
806         memcpy(&bss_desc->phy_param_set, &adhoc_start->phy_param_set,
807                sizeof(union ieee_types_phy_param_set));
808
809         /* Set IBSS param set */
810 /* IBSS parameter IE Id */
811 #define IBSS_PARA_IE_ID   6
812 /* IBSS parameter IE length */
813 #define IBSS_PARA_IE_LEN  2
814
815         adhoc_start->ss_param_set.ibss_param_set.element_id = IBSS_PARA_IE_ID;
816         adhoc_start->ss_param_set.ibss_param_set.len = IBSS_PARA_IE_LEN;
817         adhoc_start->ss_param_set.ibss_param_set.atim_window
818                 = cpu_to_le16(priv->atim_window);
819         memcpy(&bss_desc->ss_param_set, &adhoc_start->ss_param_set,
820                sizeof(union ieee_types_ss_param_set));
821
822         /* Set Capability info */
823         bss_desc->cap_info_bitmap |= WLAN_CAPABILITY_IBSS;
824         tmp_cap = le16_to_cpu(adhoc_start->cap_info_bitmap);
825         tmp_cap &= ~WLAN_CAPABILITY_ESS;
826         tmp_cap |= WLAN_CAPABILITY_IBSS;
827
828         /* Set up privacy in bss_desc */
829         if (priv->sec_info.encryption_mode) {
830                 /* Ad-Hoc capability privacy on */
831                 dev_dbg(adapter->dev,
832                         "info: ADHOC_S_CMD: wep_status set privacy to WEP\n");
833                 bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_8021X_WEP;
834                 tmp_cap |= WLAN_CAPABILITY_PRIVACY;
835         } else {
836                 dev_dbg(adapter->dev, "info: ADHOC_S_CMD: wep_status NOT set,"
837                                 " setting privacy to ACCEPT ALL\n");
838                 bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_ACCEPT_ALL;
839         }
840
841         memset(adhoc_start->DataRate, 0, sizeof(adhoc_start->DataRate));
842         mwifiex_get_active_data_rates(priv, adhoc_start->DataRate);
843         if ((adapter->adhoc_start_band & BAND_G) &&
844             (priv->curr_pkt_filter & HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON)) {
845                 if (mwifiex_send_cmd_async(priv, HostCmd_CMD_MAC_CONTROL,
846                                              HostCmd_ACT_GEN_SET, 0,
847                                              &priv->curr_pkt_filter)) {
848                         dev_err(adapter->dev,
849                                "ADHOC_S_CMD: G Protection config failed\n");
850                         return -1;
851                 }
852         }
853         /* Find the last non zero */
854         for (i = 0; i < sizeof(adhoc_start->DataRate) &&
855                         adhoc_start->DataRate[i];
856                         i++)
857                         ;
858
859         priv->curr_bss_params.num_of_rates = i;
860
861         /* Copy the ad-hoc creating rates into Current BSS rate structure */
862         memcpy(&priv->curr_bss_params.data_rates,
863                &adhoc_start->DataRate, priv->curr_bss_params.num_of_rates);
864
865         dev_dbg(adapter->dev, "info: ADHOC_S_CMD: rates=%02x %02x %02x %02x\n",
866                adhoc_start->DataRate[0], adhoc_start->DataRate[1],
867                adhoc_start->DataRate[2], adhoc_start->DataRate[3]);
868
869         dev_dbg(adapter->dev, "info: ADHOC_S_CMD: AD-HOC Start command is ready\n");
870
871         if (IS_SUPPORT_MULTI_BANDS(adapter)) {
872                 /* Append a channel TLV */
873                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
874                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
875                 chan_tlv->header.len =
876                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
877
878                 memset(chan_tlv->chan_scan_param, 0x00,
879                        sizeof(struct mwifiex_chan_scan_param_set));
880                 chan_tlv->chan_scan_param[0].chan_number =
881                         (u8) priv->curr_bss_params.bss_descriptor.channel;
882
883                 dev_dbg(adapter->dev, "info: ADHOC_S_CMD: TLV Chan = %d\n",
884                        chan_tlv->chan_scan_param[0].chan_number);
885
886                 chan_tlv->chan_scan_param[0].radio_type
887                        = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
888                 if (adapter->adhoc_start_band & BAND_GN
889                     || adapter->adhoc_start_band & BAND_AN) {
890                         if (adapter->chan_offset == SEC_CHANNEL_ABOVE)
891                                 chan_tlv->chan_scan_param[0].radio_type |=
892                                         SECOND_CHANNEL_ABOVE;
893                         else if (adapter->chan_offset == SEC_CHANNEL_BELOW)
894                                 chan_tlv->chan_scan_param[0].radio_type |=
895                                         SECOND_CHANNEL_BELOW;
896                 }
897                 dev_dbg(adapter->dev, "info: ADHOC_S_CMD: TLV Band = %d\n",
898                        chan_tlv->chan_scan_param[0].radio_type);
899                 pos += sizeof(chan_tlv->header) +
900                         sizeof(struct mwifiex_chan_scan_param_set);
901                 cmd_append_size +=
902                         sizeof(chan_tlv->header) +
903                         sizeof(struct mwifiex_chan_scan_param_set);
904         }
905
906         /* Append vendor specific IE TLV */
907         cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,
908                                 MWIFIEX_VSIE_MASK_ADHOC, &pos);
909
910         if (priv->sec_info.wpa_enabled) {
911                 rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
912                 if (rsn_ie_len == -1)
913                         return -1;
914                 cmd_append_size += rsn_ie_len;
915         }
916
917         if (adapter->adhoc_11n_enabled) {
918                 {
919                         ht_cap = (struct mwifiex_ie_types_htcap *) pos;
920                         memset(ht_cap, 0,
921                                sizeof(struct mwifiex_ie_types_htcap));
922                         ht_cap->header.type =
923                                 cpu_to_le16(WLAN_EID_HT_CAPABILITY);
924                         ht_cap->header.len =
925                                cpu_to_le16(sizeof(struct ieee80211_ht_cap));
926                         ht_cap_info = le16_to_cpu(ht_cap->ht_cap.cap_info);
927
928                         ht_cap_info |= IEEE80211_HT_CAP_SGI_20;
929                         if (adapter->chan_offset) {
930                                 ht_cap_info |= IEEE80211_HT_CAP_SGI_40;
931                                 ht_cap_info |= IEEE80211_HT_CAP_DSSSCCK40;
932                                 ht_cap_info |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
933                                 SETHT_MCS32(ht_cap->ht_cap.mcs.rx_mask);
934                         }
935
936                         ht_cap->ht_cap.ampdu_params_info
937                                         = IEEE80211_HT_MAX_AMPDU_64K;
938                         ht_cap->ht_cap.mcs.rx_mask[0] = 0xff;
939                         pos += sizeof(struct mwifiex_ie_types_htcap);
940                         cmd_append_size +=
941                                 sizeof(struct mwifiex_ie_types_htcap);
942                 }
943                 {
944                         ht_info = (struct mwifiex_ie_types_htinfo *) pos;
945                         memset(ht_info, 0,
946                                sizeof(struct mwifiex_ie_types_htinfo));
947                         ht_info->header.type =
948                                 cpu_to_le16(WLAN_EID_HT_INFORMATION);
949                         ht_info->header.len =
950                                 cpu_to_le16(sizeof(struct ieee80211_ht_info));
951                         ht_info->ht_info.control_chan =
952                                 (u8) priv->curr_bss_params.bss_descriptor.
953                                 channel;
954                         if (adapter->chan_offset) {
955                                 ht_info->ht_info.ht_param =
956                                         adapter->chan_offset;
957                                 ht_info->ht_info.ht_param |=
958                                         IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
959                         }
960                         ht_info->ht_info.operation_mode =
961                              cpu_to_le16(IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
962                         ht_info->ht_info.basic_set[0] = 0xff;
963                         pos += sizeof(struct mwifiex_ie_types_htinfo);
964                         cmd_append_size +=
965                                 sizeof(struct mwifiex_ie_types_htinfo);
966                 }
967         }
968
969         cmd->size = cpu_to_le16((u16)
970                             (sizeof(struct host_cmd_ds_802_11_ad_hoc_start)
971                              + S_DS_GEN + cmd_append_size));
972
973         if (adapter->adhoc_start_band == BAND_B)
974                 tmp_cap &= ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
975         else
976                 tmp_cap |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
977
978         adhoc_start->cap_info_bitmap = cpu_to_le16(tmp_cap);
979
980         return 0;
981 }
982
983 /*
984  * This function prepares command for ad-hoc join.
985  *
986  * Most of the parameters are set up by copying from the target BSS descriptor
987  * from the scan response.
988  *
989  * In addition, the following TLVs are added -
990  *      - Channel TLV
991  *      - Vendor specific IE
992  *      - WPA/WPA2 IE
993  *      - 11n IE
994  *
995  * Preparation also includes -
996  *      - Setting command ID and proper size
997  *      - Ensuring correct endian-ness
998  */
999 int
1000 mwifiex_cmd_802_11_ad_hoc_join(struct mwifiex_private *priv,
1001                                struct host_cmd_ds_command *cmd,
1002                                struct mwifiex_bssdescriptor *bss_desc)
1003 {
1004         int rsn_ie_len = 0;
1005         struct host_cmd_ds_802_11_ad_hoc_join *adhoc_join =
1006                 &cmd->params.adhoc_join;
1007         struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
1008         u32 cmd_append_size = 0;
1009         u16 tmp_cap;
1010         u32 i, rates_size = 0;
1011         u16 curr_pkt_filter;
1012         u8 *pos =
1013                 (u8 *) adhoc_join +
1014                 sizeof(struct host_cmd_ds_802_11_ad_hoc_join);
1015
1016 /* Use G protection */
1017 #define USE_G_PROTECTION        0x02
1018         if (bss_desc->erp_flags & USE_G_PROTECTION) {
1019                 curr_pkt_filter =
1020                         priv->
1021                         curr_pkt_filter | HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON;
1022
1023                 if (mwifiex_send_cmd_async(priv, HostCmd_CMD_MAC_CONTROL,
1024                                              HostCmd_ACT_GEN_SET, 0,
1025                                              &curr_pkt_filter)) {
1026                         dev_err(priv->adapter->dev,
1027                                "ADHOC_J_CMD: G Protection config failed\n");
1028                         return -1;
1029                 }
1030         }
1031
1032         priv->attempted_bss_desc = bss_desc;
1033
1034         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_JOIN);
1035
1036         adhoc_join->bss_descriptor.bss_mode = HostCmd_BSS_MODE_IBSS;
1037
1038         adhoc_join->bss_descriptor.beacon_period
1039                 = cpu_to_le16(bss_desc->beacon_period);
1040
1041         memcpy(&adhoc_join->bss_descriptor.bssid,
1042                &bss_desc->mac_address, ETH_ALEN);
1043
1044         memcpy(&adhoc_join->bss_descriptor.ssid,
1045                &bss_desc->ssid.ssid, bss_desc->ssid.ssid_len);
1046
1047         memcpy(&adhoc_join->bss_descriptor.phy_param_set,
1048                &bss_desc->phy_param_set,
1049                sizeof(union ieee_types_phy_param_set));
1050
1051         memcpy(&adhoc_join->bss_descriptor.ss_param_set,
1052                &bss_desc->ss_param_set, sizeof(union ieee_types_ss_param_set));
1053
1054         tmp_cap = bss_desc->cap_info_bitmap;
1055
1056         tmp_cap &= CAPINFO_MASK;
1057
1058         dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: tmp_cap=%4X"
1059                         " CAPINFO_MASK=%4lX\n", tmp_cap, CAPINFO_MASK);
1060
1061         /* Information on BSSID descriptor passed to FW */
1062         dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: BSSID = %pM, SSID = %s\n",
1063                                 adhoc_join->bss_descriptor.bssid,
1064                                 adhoc_join->bss_descriptor.ssid);
1065
1066         for (i = 0; bss_desc->supported_rates[i] &&
1067                         i < MWIFIEX_SUPPORTED_RATES;
1068                         i++)
1069                         ;
1070         rates_size = i;
1071
1072         /* Copy Data Rates from the Rates recorded in scan response */
1073         memset(adhoc_join->bss_descriptor.data_rates, 0,
1074                sizeof(adhoc_join->bss_descriptor.data_rates));
1075         memcpy(adhoc_join->bss_descriptor.data_rates,
1076                bss_desc->supported_rates, rates_size);
1077
1078         /* Copy the adhoc join rates into Current BSS state structure */
1079         priv->curr_bss_params.num_of_rates = rates_size;
1080         memcpy(&priv->curr_bss_params.data_rates, bss_desc->supported_rates,
1081                rates_size);
1082
1083         /* Copy the channel information */
1084         priv->curr_bss_params.bss_descriptor.channel = bss_desc->channel;
1085         priv->curr_bss_params.band = (u8) bss_desc->bss_band;
1086
1087         if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_ENABLED
1088             || priv->sec_info.wpa_enabled)
1089                 tmp_cap |= WLAN_CAPABILITY_PRIVACY;
1090
1091         if (IS_SUPPORT_MULTI_BANDS(priv->adapter)) {
1092                 /* Append a channel TLV */
1093                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
1094                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
1095                 chan_tlv->header.len =
1096                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
1097
1098                 memset(chan_tlv->chan_scan_param, 0x00,
1099                        sizeof(struct mwifiex_chan_scan_param_set));
1100                 chan_tlv->chan_scan_param[0].chan_number =
1101                         (bss_desc->phy_param_set.ds_param_set.current_chan);
1102                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: TLV Chan = %d\n",
1103                        chan_tlv->chan_scan_param[0].chan_number);
1104
1105                 chan_tlv->chan_scan_param[0].radio_type =
1106                         mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
1107
1108                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: TLV Band = %d\n",
1109                        chan_tlv->chan_scan_param[0].radio_type);
1110                 pos += sizeof(chan_tlv->header) +
1111                         sizeof(struct mwifiex_chan_scan_param_set);
1112                 cmd_append_size += sizeof(chan_tlv->header) +
1113                         sizeof(struct mwifiex_chan_scan_param_set);
1114         }
1115
1116         if (priv->sec_info.wpa_enabled)
1117                 rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
1118         if (rsn_ie_len == -1)
1119                 return -1;
1120         cmd_append_size += rsn_ie_len;
1121
1122         if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info))
1123                 cmd_append_size += mwifiex_cmd_append_11n_tlv(priv,
1124                         bss_desc, &pos);
1125
1126         /* Append vendor specific IE TLV */
1127         cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,
1128                         MWIFIEX_VSIE_MASK_ADHOC, &pos);
1129
1130         cmd->size = cpu_to_le16((u16)
1131                             (sizeof(struct host_cmd_ds_802_11_ad_hoc_join)
1132                              + S_DS_GEN + cmd_append_size));
1133
1134         adhoc_join->bss_descriptor.cap_info_bitmap = cpu_to_le16(tmp_cap);
1135
1136         return 0;
1137 }
1138
1139 /*
1140  * This function handles the command response of ad-hoc start and
1141  * ad-hoc join.
1142  *
1143  * The function generates a device-connected event to notify
1144  * the applications, in case of successful ad-hoc start/join, and
1145  * saves the beacon buffer.
1146  */
1147 int mwifiex_ret_802_11_ad_hoc(struct mwifiex_private *priv,
1148                               struct host_cmd_ds_command *resp)
1149 {
1150         int ret = 0;
1151         struct mwifiex_adapter *adapter = priv->adapter;
1152         struct host_cmd_ds_802_11_ad_hoc_result *adhoc_result;
1153         struct mwifiex_bssdescriptor *bss_desc;
1154
1155         adhoc_result = &resp->params.adhoc_result;
1156
1157         bss_desc = priv->attempted_bss_desc;
1158
1159         /* Join result code 0 --> SUCCESS */
1160         if (le16_to_cpu(resp->result)) {
1161                 dev_err(priv->adapter->dev, "ADHOC_RESP: failed\n");
1162                 if (priv->media_connected)
1163                         mwifiex_reset_connect_state(priv);
1164
1165                 memset(&priv->curr_bss_params.bss_descriptor,
1166                        0x00, sizeof(struct mwifiex_bssdescriptor));
1167
1168                 ret = -1;
1169                 goto done;
1170         }
1171
1172         /* Send a Media Connected event, according to the Spec */
1173         priv->media_connected = true;
1174
1175         if (le16_to_cpu(resp->command) == HostCmd_CMD_802_11_AD_HOC_START) {
1176                 dev_dbg(priv->adapter->dev, "info: ADHOC_S_RESP %s\n",
1177                                 bss_desc->ssid.ssid);
1178
1179                 /* Update the created network descriptor with the new BSSID */
1180                 memcpy(bss_desc->mac_address,
1181                        adhoc_result->bssid, ETH_ALEN);
1182
1183                 priv->adhoc_state = ADHOC_STARTED;
1184         } else {
1185                 /*
1186                  * Now the join cmd should be successful.
1187                  * If BSSID has changed use SSID to compare instead of BSSID
1188                  */
1189                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_RESP %s\n",
1190                                 bss_desc->ssid.ssid);
1191
1192                 /*
1193                  * Make a copy of current BSSID descriptor, only needed for
1194                  * join since the current descriptor is already being used
1195                  * for adhoc start
1196                  */
1197                 memcpy(&priv->curr_bss_params.bss_descriptor,
1198                        bss_desc, sizeof(struct mwifiex_bssdescriptor));
1199
1200                 priv->adhoc_state = ADHOC_JOINED;
1201         }
1202
1203         dev_dbg(priv->adapter->dev, "info: ADHOC_RESP: channel = %d\n",
1204                                 priv->adhoc_channel);
1205         dev_dbg(priv->adapter->dev, "info: ADHOC_RESP: BSSID = %pM\n",
1206                priv->curr_bss_params.bss_descriptor.mac_address);
1207
1208         if (!netif_carrier_ok(priv->netdev))
1209                 netif_carrier_on(priv->netdev);
1210         if (netif_queue_stopped(priv->netdev))
1211                 netif_wake_queue(priv->netdev);
1212
1213         mwifiex_save_curr_bcn(priv);
1214
1215 done:
1216         /* Need to indicate IOCTL complete */
1217         if (adapter->curr_cmd->wait_q_enabled) {
1218                 if (ret)
1219                         adapter->cmd_wait_q.status = -1;
1220                 else
1221                         adapter->cmd_wait_q.status = 0;
1222
1223         }
1224
1225         return ret;
1226 }
1227
1228 /*
1229  * This function associates to a specific BSS discovered in a scan.
1230  *
1231  * It clears any past association response stored for application
1232  * retrieval and calls the command preparation routine to send the
1233  * command to firmware.
1234  */
1235 int mwifiex_associate(struct mwifiex_private *priv,
1236                       struct mwifiex_bssdescriptor *bss_desc)
1237 {
1238         u8 current_bssid[ETH_ALEN];
1239
1240         /* Return error if the adapter or table entry is not marked as infra */
1241         if ((priv->bss_mode != NL80211_IFTYPE_STATION) ||
1242             (bss_desc->bss_mode != NL80211_IFTYPE_STATION))
1243                 return -1;
1244
1245         memcpy(&current_bssid,
1246                &priv->curr_bss_params.bss_descriptor.mac_address,
1247                sizeof(current_bssid));
1248
1249         /* Clear any past association response stored for application
1250            retrieval */
1251         priv->assoc_rsp_size = 0;
1252
1253         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_ASSOCIATE,
1254                                     HostCmd_ACT_GEN_SET, 0, bss_desc);
1255 }
1256
1257 /*
1258  * This function starts an ad-hoc network.
1259  *
1260  * It calls the command preparation routine to send the command to firmware.
1261  */
1262 int
1263 mwifiex_adhoc_start(struct mwifiex_private *priv,
1264                     struct mwifiex_802_11_ssid *adhoc_ssid)
1265 {
1266         dev_dbg(priv->adapter->dev, "info: Adhoc Channel = %d\n",
1267                 priv->adhoc_channel);
1268         dev_dbg(priv->adapter->dev, "info: curr_bss_params.channel = %d\n",
1269                priv->curr_bss_params.bss_descriptor.channel);
1270         dev_dbg(priv->adapter->dev, "info: curr_bss_params.band = %d\n",
1271                priv->curr_bss_params.band);
1272
1273         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_AD_HOC_START,
1274                                     HostCmd_ACT_GEN_SET, 0, adhoc_ssid);
1275 }
1276
1277 /*
1278  * This function joins an ad-hoc network found in a previous scan.
1279  *
1280  * It calls the command preparation routine to send the command to firmware,
1281  * if already not connected to the requested SSID.
1282  */
1283 int mwifiex_adhoc_join(struct mwifiex_private *priv,
1284                        struct mwifiex_bssdescriptor *bss_desc)
1285 {
1286         dev_dbg(priv->adapter->dev, "info: adhoc join: curr_bss ssid =%s\n",
1287                priv->curr_bss_params.bss_descriptor.ssid.ssid);
1288         dev_dbg(priv->adapter->dev, "info: adhoc join: curr_bss ssid_len =%u\n",
1289                priv->curr_bss_params.bss_descriptor.ssid.ssid_len);
1290         dev_dbg(priv->adapter->dev, "info: adhoc join: ssid =%s\n",
1291                 bss_desc->ssid.ssid);
1292         dev_dbg(priv->adapter->dev, "info: adhoc join: ssid_len =%u\n",
1293                bss_desc->ssid.ssid_len);
1294
1295         /* Check if the requested SSID is already joined */
1296         if (priv->curr_bss_params.bss_descriptor.ssid.ssid_len &&
1297             !mwifiex_ssid_cmp(&bss_desc->ssid,
1298                               &priv->curr_bss_params.bss_descriptor.ssid) &&
1299             (priv->curr_bss_params.bss_descriptor.bss_mode ==
1300                                                         NL80211_IFTYPE_ADHOC)) {
1301                 dev_dbg(priv->adapter->dev, "info: ADHOC_J_CMD: new ad-hoc SSID"
1302                         " is the same as current; not attempting to re-join\n");
1303                 return -1;
1304         }
1305
1306         dev_dbg(priv->adapter->dev, "info: curr_bss_params.channel = %d\n",
1307                priv->curr_bss_params.bss_descriptor.channel);
1308         dev_dbg(priv->adapter->dev, "info: curr_bss_params.band = %c\n",
1309                priv->curr_bss_params.band);
1310
1311         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_AD_HOC_JOIN,
1312                                     HostCmd_ACT_GEN_SET, 0, bss_desc);
1313 }
1314
1315 /*
1316  * This function deauthenticates/disconnects from infra network by sending
1317  * deauthentication request.
1318  */
1319 static int mwifiex_deauthenticate_infra(struct mwifiex_private *priv, u8 *mac)
1320 {
1321         u8 mac_address[ETH_ALEN];
1322         int ret;
1323         u8 zero_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
1324
1325         if (mac) {
1326                 if (!memcmp(mac, zero_mac, sizeof(zero_mac)))
1327                         memcpy((u8 *) &mac_address,
1328                                (u8 *) &priv->curr_bss_params.bss_descriptor.
1329                                mac_address, ETH_ALEN);
1330                 else
1331                         memcpy((u8 *) &mac_address, (u8 *) mac, ETH_ALEN);
1332         } else {
1333                 memcpy((u8 *) &mac_address, (u8 *) &priv->curr_bss_params.
1334                        bss_descriptor.mac_address, ETH_ALEN);
1335         }
1336
1337         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_DEAUTHENTICATE,
1338                                     HostCmd_ACT_GEN_SET, 0, &mac_address);
1339
1340         return ret;
1341 }
1342
1343 /*
1344  * This function deauthenticates/disconnects from a BSS.
1345  *
1346  * In case of infra made, it sends deauthentication request, and
1347  * in case of ad-hoc mode, a stop network request is sent to the firmware.
1348  */
1349 int mwifiex_deauthenticate(struct mwifiex_private *priv, u8 *mac)
1350 {
1351         int ret = 0;
1352
1353         if (priv->media_connected) {
1354                 if (priv->bss_mode == NL80211_IFTYPE_STATION) {
1355                         ret = mwifiex_deauthenticate_infra(priv, mac);
1356                 } else if (priv->bss_mode == NL80211_IFTYPE_ADHOC) {
1357                         ret = mwifiex_send_cmd_sync(priv,
1358                                                 HostCmd_CMD_802_11_AD_HOC_STOP,
1359                                                 HostCmd_ACT_GEN_SET, 0, NULL);
1360                 }
1361         }
1362
1363         return ret;
1364 }
1365 EXPORT_SYMBOL_GPL(mwifiex_deauthenticate);
1366
1367 /*
1368  * This function converts band to radio type used in channel TLV.
1369  */
1370 u8
1371 mwifiex_band_to_radio_type(u8 band)
1372 {
1373         switch (band) {
1374         case BAND_A:
1375         case BAND_AN:
1376         case BAND_A | BAND_AN:
1377                 return HostCmd_SCAN_RADIO_TYPE_A;
1378         case BAND_B:
1379         case BAND_G:
1380         case BAND_B | BAND_G:
1381         default:
1382                 return HostCmd_SCAN_RADIO_TYPE_BG;
1383         }
1384 }