Linux 3.9-rc8
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / mwifiex / uap_cmd.c
1 /*
2  * Marvell Wireless LAN device driver: AP specific command handling
3  *
4  * Copyright (C) 2012, 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 "main.h"
21
22 /* This function parses security related parameters from cfg80211_ap_settings
23  * and sets into FW understandable bss_config structure.
24  */
25 int mwifiex_set_secure_params(struct mwifiex_private *priv,
26                               struct mwifiex_uap_bss_param *bss_config,
27                               struct cfg80211_ap_settings *params) {
28         int i;
29         struct mwifiex_wep_key wep_key;
30
31         if (!params->privacy) {
32                 bss_config->protocol = PROTOCOL_NO_SECURITY;
33                 bss_config->key_mgmt = KEY_MGMT_NONE;
34                 bss_config->wpa_cfg.length = 0;
35                 priv->sec_info.wep_enabled = 0;
36                 priv->sec_info.wpa_enabled = 0;
37                 priv->sec_info.wpa2_enabled = 0;
38
39                 return 0;
40         }
41
42         switch (params->auth_type) {
43         case NL80211_AUTHTYPE_OPEN_SYSTEM:
44                 bss_config->auth_mode = WLAN_AUTH_OPEN;
45                 break;
46         case NL80211_AUTHTYPE_SHARED_KEY:
47                 bss_config->auth_mode = WLAN_AUTH_SHARED_KEY;
48                 break;
49         case NL80211_AUTHTYPE_NETWORK_EAP:
50                 bss_config->auth_mode = WLAN_AUTH_LEAP;
51                 break;
52         default:
53                 bss_config->auth_mode = MWIFIEX_AUTH_MODE_AUTO;
54                 break;
55         }
56
57         bss_config->key_mgmt_operation |= KEY_MGMT_ON_HOST;
58
59         for (i = 0; i < params->crypto.n_akm_suites; i++) {
60                 switch (params->crypto.akm_suites[i]) {
61                 case WLAN_AKM_SUITE_8021X:
62                         if (params->crypto.wpa_versions &
63                             NL80211_WPA_VERSION_1) {
64                                 bss_config->protocol = PROTOCOL_WPA;
65                                 bss_config->key_mgmt = KEY_MGMT_EAP;
66                         }
67                         if (params->crypto.wpa_versions &
68                             NL80211_WPA_VERSION_2) {
69                                 bss_config->protocol |= PROTOCOL_WPA2;
70                                 bss_config->key_mgmt = KEY_MGMT_EAP;
71                         }
72                         break;
73                 case WLAN_AKM_SUITE_PSK:
74                         if (params->crypto.wpa_versions &
75                             NL80211_WPA_VERSION_1) {
76                                 bss_config->protocol = PROTOCOL_WPA;
77                                 bss_config->key_mgmt = KEY_MGMT_PSK;
78                         }
79                         if (params->crypto.wpa_versions &
80                             NL80211_WPA_VERSION_2) {
81                                 bss_config->protocol |= PROTOCOL_WPA2;
82                                 bss_config->key_mgmt = KEY_MGMT_PSK;
83                         }
84                         break;
85                 default:
86                         break;
87                 }
88         }
89         for (i = 0; i < params->crypto.n_ciphers_pairwise; i++) {
90                 switch (params->crypto.ciphers_pairwise[i]) {
91                 case WLAN_CIPHER_SUITE_WEP40:
92                 case WLAN_CIPHER_SUITE_WEP104:
93                         break;
94                 case WLAN_CIPHER_SUITE_TKIP:
95                         if (params->crypto.wpa_versions & NL80211_WPA_VERSION_1)
96                                 bss_config->wpa_cfg.pairwise_cipher_wpa |=
97                                                                 CIPHER_TKIP;
98                         if (params->crypto.wpa_versions & NL80211_WPA_VERSION_2)
99                                 bss_config->wpa_cfg.pairwise_cipher_wpa2 |=
100                                                                 CIPHER_TKIP;
101                         break;
102                 case WLAN_CIPHER_SUITE_CCMP:
103                         if (params->crypto.wpa_versions & NL80211_WPA_VERSION_1)
104                                 bss_config->wpa_cfg.pairwise_cipher_wpa |=
105                                                                 CIPHER_AES_CCMP;
106                         if (params->crypto.wpa_versions & NL80211_WPA_VERSION_2)
107                                 bss_config->wpa_cfg.pairwise_cipher_wpa2 |=
108                                                                 CIPHER_AES_CCMP;
109                 default:
110                         break;
111                 }
112         }
113
114         switch (params->crypto.cipher_group) {
115         case WLAN_CIPHER_SUITE_WEP40:
116         case WLAN_CIPHER_SUITE_WEP104:
117                 if (priv->sec_info.wep_enabled) {
118                         bss_config->protocol = PROTOCOL_STATIC_WEP;
119                         bss_config->key_mgmt = KEY_MGMT_NONE;
120                         bss_config->wpa_cfg.length = 0;
121
122                         for (i = 0; i < NUM_WEP_KEYS; i++) {
123                                 wep_key = priv->wep_key[i];
124                                 bss_config->wep_cfg[i].key_index = i;
125
126                                 if (priv->wep_key_curr_index == i)
127                                         bss_config->wep_cfg[i].is_default = 1;
128                                 else
129                                         bss_config->wep_cfg[i].is_default = 0;
130
131                                 bss_config->wep_cfg[i].length =
132                                                              wep_key.key_length;
133                                 memcpy(&bss_config->wep_cfg[i].key,
134                                        &wep_key.key_material,
135                                        wep_key.key_length);
136                         }
137                 }
138                 break;
139         case WLAN_CIPHER_SUITE_TKIP:
140                 bss_config->wpa_cfg.group_cipher = CIPHER_TKIP;
141                 break;
142         case WLAN_CIPHER_SUITE_CCMP:
143                 bss_config->wpa_cfg.group_cipher = CIPHER_AES_CCMP;
144                 break;
145         default:
146                 break;
147         }
148
149         return 0;
150 }
151
152 /* This function updates 11n related parameters from IE and sets them into
153  * bss_config structure.
154  */
155 void
156 mwifiex_set_ht_params(struct mwifiex_private *priv,
157                       struct mwifiex_uap_bss_param *bss_cfg,
158                       struct cfg80211_ap_settings *params)
159 {
160         const u8 *ht_ie;
161
162         if (!ISSUPP_11NENABLED(priv->adapter->fw_cap_info))
163                 return;
164
165         ht_ie = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, params->beacon.tail,
166                                  params->beacon.tail_len);
167         if (ht_ie) {
168                 memcpy(&bss_cfg->ht_cap, ht_ie + 2,
169                        sizeof(struct ieee80211_ht_cap));
170                 priv->ap_11n_enabled = 1;
171         } else {
172                 memset(&bss_cfg->ht_cap , 0, sizeof(struct ieee80211_ht_cap));
173                 bss_cfg->ht_cap.cap_info = cpu_to_le16(MWIFIEX_DEF_HT_CAP);
174                 bss_cfg->ht_cap.ampdu_params_info = MWIFIEX_DEF_AMPDU;
175         }
176
177         return;
178 }
179
180 /* This function finds supported rates IE from beacon parameter and sets
181  * these rates into bss_config structure.
182  */
183 void
184 mwifiex_set_uap_rates(struct mwifiex_uap_bss_param *bss_cfg,
185                       struct cfg80211_ap_settings *params)
186 {
187         struct ieee_types_header *rate_ie;
188         int var_offset = offsetof(struct ieee80211_mgmt, u.beacon.variable);
189         const u8 *var_pos = params->beacon.head + var_offset;
190         int len = params->beacon.head_len - var_offset;
191         u8 rate_len = 0;
192
193         rate_ie = (void *)cfg80211_find_ie(WLAN_EID_SUPP_RATES, var_pos, len);
194         if (rate_ie) {
195                 memcpy(bss_cfg->rates, rate_ie + 1, rate_ie->len);
196                 rate_len = rate_ie->len;
197         }
198
199         rate_ie = (void *)cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES,
200                                            params->beacon.tail,
201                                            params->beacon.tail_len);
202         if (rate_ie)
203                 memcpy(bss_cfg->rates + rate_len, rate_ie + 1, rate_ie->len);
204
205         return;
206 }
207
208 /* This function initializes some of mwifiex_uap_bss_param variables.
209  * This helps FW in ignoring invalid values. These values may or may not
210  * be get updated to valid ones at later stage.
211  */
212 void mwifiex_set_sys_config_invalid_data(struct mwifiex_uap_bss_param *config)
213 {
214         config->bcast_ssid_ctl = 0x7F;
215         config->radio_ctl = 0x7F;
216         config->dtim_period = 0x7F;
217         config->beacon_period = 0x7FFF;
218         config->auth_mode = 0x7F;
219         config->rts_threshold = 0x7FFF;
220         config->frag_threshold = 0x7FFF;
221         config->retry_limit = 0x7F;
222         config->qos_info = 0xFF;
223 }
224
225 /* This function parses BSS related parameters from structure
226  * and prepares TLVs specific to WPA/WPA2 security.
227  * These TLVs are appended to command buffer.
228  */
229 static void
230 mwifiex_uap_bss_wpa(u8 **tlv_buf, void *cmd_buf, u16 *param_size)
231 {
232         struct host_cmd_tlv_pwk_cipher *pwk_cipher;
233         struct host_cmd_tlv_gwk_cipher *gwk_cipher;
234         struct host_cmd_tlv_passphrase *passphrase;
235         struct host_cmd_tlv_akmp *tlv_akmp;
236         struct mwifiex_uap_bss_param *bss_cfg = cmd_buf;
237         u16 cmd_size = *param_size;
238         u8 *tlv = *tlv_buf;
239
240         tlv_akmp = (struct host_cmd_tlv_akmp *)tlv;
241         tlv_akmp->tlv.type = cpu_to_le16(TLV_TYPE_UAP_AKMP);
242         tlv_akmp->tlv.len = cpu_to_le16(sizeof(struct host_cmd_tlv_akmp) -
243                                         sizeof(struct host_cmd_tlv));
244         tlv_akmp->key_mgmt_operation = cpu_to_le16(bss_cfg->key_mgmt_operation);
245         tlv_akmp->key_mgmt = cpu_to_le16(bss_cfg->key_mgmt);
246         cmd_size += sizeof(struct host_cmd_tlv_akmp);
247         tlv += sizeof(struct host_cmd_tlv_akmp);
248
249         if (bss_cfg->wpa_cfg.pairwise_cipher_wpa & VALID_CIPHER_BITMAP) {
250                 pwk_cipher = (struct host_cmd_tlv_pwk_cipher *)tlv;
251                 pwk_cipher->tlv.type = cpu_to_le16(TLV_TYPE_PWK_CIPHER);
252                 pwk_cipher->tlv.len =
253                         cpu_to_le16(sizeof(struct host_cmd_tlv_pwk_cipher) -
254                                     sizeof(struct host_cmd_tlv));
255                 pwk_cipher->proto = cpu_to_le16(PROTOCOL_WPA);
256                 pwk_cipher->cipher = bss_cfg->wpa_cfg.pairwise_cipher_wpa;
257                 cmd_size += sizeof(struct host_cmd_tlv_pwk_cipher);
258                 tlv += sizeof(struct host_cmd_tlv_pwk_cipher);
259         }
260
261         if (bss_cfg->wpa_cfg.pairwise_cipher_wpa2 & VALID_CIPHER_BITMAP) {
262                 pwk_cipher = (struct host_cmd_tlv_pwk_cipher *)tlv;
263                 pwk_cipher->tlv.type = cpu_to_le16(TLV_TYPE_PWK_CIPHER);
264                 pwk_cipher->tlv.len =
265                         cpu_to_le16(sizeof(struct host_cmd_tlv_pwk_cipher) -
266                                     sizeof(struct host_cmd_tlv));
267                 pwk_cipher->proto = cpu_to_le16(PROTOCOL_WPA2);
268                 pwk_cipher->cipher = bss_cfg->wpa_cfg.pairwise_cipher_wpa2;
269                 cmd_size += sizeof(struct host_cmd_tlv_pwk_cipher);
270                 tlv += sizeof(struct host_cmd_tlv_pwk_cipher);
271         }
272
273         if (bss_cfg->wpa_cfg.group_cipher & VALID_CIPHER_BITMAP) {
274                 gwk_cipher = (struct host_cmd_tlv_gwk_cipher *)tlv;
275                 gwk_cipher->tlv.type = cpu_to_le16(TLV_TYPE_GWK_CIPHER);
276                 gwk_cipher->tlv.len =
277                         cpu_to_le16(sizeof(struct host_cmd_tlv_gwk_cipher) -
278                                     sizeof(struct host_cmd_tlv));
279                 gwk_cipher->cipher = bss_cfg->wpa_cfg.group_cipher;
280                 cmd_size += sizeof(struct host_cmd_tlv_gwk_cipher);
281                 tlv += sizeof(struct host_cmd_tlv_gwk_cipher);
282         }
283
284         if (bss_cfg->wpa_cfg.length) {
285                 passphrase = (struct host_cmd_tlv_passphrase *)tlv;
286                 passphrase->tlv.type = cpu_to_le16(TLV_TYPE_UAP_WPA_PASSPHRASE);
287                 passphrase->tlv.len = cpu_to_le16(bss_cfg->wpa_cfg.length);
288                 memcpy(passphrase->passphrase, bss_cfg->wpa_cfg.passphrase,
289                        bss_cfg->wpa_cfg.length);
290                 cmd_size += sizeof(struct host_cmd_tlv) +
291                             bss_cfg->wpa_cfg.length;
292                 tlv += sizeof(struct host_cmd_tlv) + bss_cfg->wpa_cfg.length;
293         }
294
295         *param_size = cmd_size;
296         *tlv_buf = tlv;
297
298         return;
299 }
300
301 /* This function parses WMM related parameters from cfg80211_ap_settings
302  * structure and updates bss_config structure.
303  */
304 void
305 mwifiex_set_wmm_params(struct mwifiex_private *priv,
306                        struct mwifiex_uap_bss_param *bss_cfg,
307                        struct cfg80211_ap_settings *params)
308 {
309         const u8 *vendor_ie;
310         struct ieee_types_header *wmm_ie;
311         u8 wmm_oui[] = {0x00, 0x50, 0xf2, 0x02};
312
313         vendor_ie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
314                                             WLAN_OUI_TYPE_MICROSOFT_WMM,
315                                             params->beacon.tail,
316                                             params->beacon.tail_len);
317         if (vendor_ie) {
318                 wmm_ie = (struct ieee_types_header *)vendor_ie;
319                 memcpy(&bss_cfg->wmm_info, wmm_ie + 1,
320                        sizeof(bss_cfg->wmm_info));
321                 priv->wmm_enabled = 1;
322         } else {
323                 memset(&bss_cfg->wmm_info, 0, sizeof(bss_cfg->wmm_info));
324                 memcpy(&bss_cfg->wmm_info.oui, wmm_oui, sizeof(wmm_oui));
325                 bss_cfg->wmm_info.subtype = MWIFIEX_WMM_SUBTYPE;
326                 bss_cfg->wmm_info.version = MWIFIEX_WMM_VERSION;
327                 priv->wmm_enabled = 0;
328         }
329
330         bss_cfg->qos_info = 0x00;
331         return;
332 }
333 /* This function parses BSS related parameters from structure
334  * and prepares TLVs specific to WEP encryption.
335  * These TLVs are appended to command buffer.
336  */
337 static void
338 mwifiex_uap_bss_wep(u8 **tlv_buf, void *cmd_buf, u16 *param_size)
339 {
340         struct host_cmd_tlv_wep_key *wep_key;
341         u16 cmd_size = *param_size;
342         int i;
343         u8 *tlv = *tlv_buf;
344         struct mwifiex_uap_bss_param *bss_cfg = cmd_buf;
345
346         for (i = 0; i < NUM_WEP_KEYS; i++) {
347                 if (bss_cfg->wep_cfg[i].length &&
348                     (bss_cfg->wep_cfg[i].length == WLAN_KEY_LEN_WEP40 ||
349                      bss_cfg->wep_cfg[i].length == WLAN_KEY_LEN_WEP104)) {
350                         wep_key = (struct host_cmd_tlv_wep_key *)tlv;
351                         wep_key->tlv.type = cpu_to_le16(TLV_TYPE_UAP_WEP_KEY);
352                         wep_key->tlv.len =
353                                 cpu_to_le16(bss_cfg->wep_cfg[i].length + 2);
354                         wep_key->key_index = bss_cfg->wep_cfg[i].key_index;
355                         wep_key->is_default = bss_cfg->wep_cfg[i].is_default;
356                         memcpy(wep_key->key, bss_cfg->wep_cfg[i].key,
357                                bss_cfg->wep_cfg[i].length);
358                         cmd_size += sizeof(struct host_cmd_tlv) + 2 +
359                                     bss_cfg->wep_cfg[i].length;
360                         tlv += sizeof(struct host_cmd_tlv) + 2 +
361                                     bss_cfg->wep_cfg[i].length;
362                 }
363         }
364
365         *param_size = cmd_size;
366         *tlv_buf = tlv;
367
368         return;
369 }
370
371 /* This function parses BSS related parameters from structure
372  * and prepares TLVs. These TLVs are appended to command buffer.
373 */
374 static int
375 mwifiex_uap_bss_param_prepare(u8 *tlv, void *cmd_buf, u16 *param_size)
376 {
377         struct host_cmd_tlv_dtim_period *dtim_period;
378         struct host_cmd_tlv_beacon_period *beacon_period;
379         struct host_cmd_tlv_ssid *ssid;
380         struct host_cmd_tlv_bcast_ssid *bcast_ssid;
381         struct host_cmd_tlv_channel_band *chan_band;
382         struct host_cmd_tlv_frag_threshold *frag_threshold;
383         struct host_cmd_tlv_rts_threshold *rts_threshold;
384         struct host_cmd_tlv_retry_limit *retry_limit;
385         struct host_cmd_tlv_encrypt_protocol *encrypt_protocol;
386         struct host_cmd_tlv_auth_type *auth_type;
387         struct host_cmd_tlv_rates *tlv_rates;
388         struct host_cmd_tlv_ageout_timer *ao_timer, *ps_ao_timer;
389         struct mwifiex_ie_types_htcap *htcap;
390         struct mwifiex_ie_types_wmmcap *wmm_cap;
391         struct mwifiex_uap_bss_param *bss_cfg = cmd_buf;
392         int i;
393         u16 cmd_size = *param_size;
394
395         if (bss_cfg->ssid.ssid_len) {
396                 ssid = (struct host_cmd_tlv_ssid *)tlv;
397                 ssid->tlv.type = cpu_to_le16(TLV_TYPE_UAP_SSID);
398                 ssid->tlv.len = cpu_to_le16((u16)bss_cfg->ssid.ssid_len);
399                 memcpy(ssid->ssid, bss_cfg->ssid.ssid, bss_cfg->ssid.ssid_len);
400                 cmd_size += sizeof(struct host_cmd_tlv) +
401                             bss_cfg->ssid.ssid_len;
402                 tlv += sizeof(struct host_cmd_tlv) + bss_cfg->ssid.ssid_len;
403
404                 bcast_ssid = (struct host_cmd_tlv_bcast_ssid *)tlv;
405                 bcast_ssid->tlv.type = cpu_to_le16(TLV_TYPE_UAP_BCAST_SSID);
406                 bcast_ssid->tlv.len =
407                                 cpu_to_le16(sizeof(bcast_ssid->bcast_ctl));
408                 bcast_ssid->bcast_ctl = bss_cfg->bcast_ssid_ctl;
409                 cmd_size += sizeof(struct host_cmd_tlv_bcast_ssid);
410                 tlv += sizeof(struct host_cmd_tlv_bcast_ssid);
411         }
412         if (bss_cfg->rates[0]) {
413                 tlv_rates = (struct host_cmd_tlv_rates *)tlv;
414                 tlv_rates->tlv.type = cpu_to_le16(TLV_TYPE_UAP_RATES);
415
416                 for (i = 0; i < MWIFIEX_SUPPORTED_RATES && bss_cfg->rates[i];
417                      i++)
418                         tlv_rates->rates[i] = bss_cfg->rates[i];
419
420                 tlv_rates->tlv.len = cpu_to_le16(i);
421                 cmd_size += sizeof(struct host_cmd_tlv_rates) + i;
422                 tlv += sizeof(struct host_cmd_tlv_rates) + i;
423         }
424         if (bss_cfg->channel &&
425             ((bss_cfg->band_cfg == BAND_CONFIG_BG &&
426               bss_cfg->channel <= MAX_CHANNEL_BAND_BG) ||
427             (bss_cfg->band_cfg == BAND_CONFIG_A &&
428              bss_cfg->channel <= MAX_CHANNEL_BAND_A))) {
429                 chan_band = (struct host_cmd_tlv_channel_band *)tlv;
430                 chan_band->tlv.type = cpu_to_le16(TLV_TYPE_CHANNELBANDLIST);
431                 chan_band->tlv.len =
432                         cpu_to_le16(sizeof(struct host_cmd_tlv_channel_band) -
433                                     sizeof(struct host_cmd_tlv));
434                 chan_band->band_config = bss_cfg->band_cfg;
435                 chan_band->channel = bss_cfg->channel;
436                 cmd_size += sizeof(struct host_cmd_tlv_channel_band);
437                 tlv += sizeof(struct host_cmd_tlv_channel_band);
438         }
439         if (bss_cfg->beacon_period >= MIN_BEACON_PERIOD &&
440             bss_cfg->beacon_period <= MAX_BEACON_PERIOD) {
441                 beacon_period = (struct host_cmd_tlv_beacon_period *)tlv;
442                 beacon_period->tlv.type =
443                                         cpu_to_le16(TLV_TYPE_UAP_BEACON_PERIOD);
444                 beacon_period->tlv.len =
445                         cpu_to_le16(sizeof(struct host_cmd_tlv_beacon_period) -
446                                     sizeof(struct host_cmd_tlv));
447                 beacon_period->period = cpu_to_le16(bss_cfg->beacon_period);
448                 cmd_size += sizeof(struct host_cmd_tlv_beacon_period);
449                 tlv += sizeof(struct host_cmd_tlv_beacon_period);
450         }
451         if (bss_cfg->dtim_period >= MIN_DTIM_PERIOD &&
452             bss_cfg->dtim_period <= MAX_DTIM_PERIOD) {
453                 dtim_period = (struct host_cmd_tlv_dtim_period *)tlv;
454                 dtim_period->tlv.type = cpu_to_le16(TLV_TYPE_UAP_DTIM_PERIOD);
455                 dtim_period->tlv.len =
456                         cpu_to_le16(sizeof(struct host_cmd_tlv_dtim_period) -
457                                     sizeof(struct host_cmd_tlv));
458                 dtim_period->period = bss_cfg->dtim_period;
459                 cmd_size += sizeof(struct host_cmd_tlv_dtim_period);
460                 tlv += sizeof(struct host_cmd_tlv_dtim_period);
461         }
462         if (bss_cfg->rts_threshold <= MWIFIEX_RTS_MAX_VALUE) {
463                 rts_threshold = (struct host_cmd_tlv_rts_threshold *)tlv;
464                 rts_threshold->tlv.type =
465                                         cpu_to_le16(TLV_TYPE_UAP_RTS_THRESHOLD);
466                 rts_threshold->tlv.len =
467                         cpu_to_le16(sizeof(struct host_cmd_tlv_rts_threshold) -
468                                     sizeof(struct host_cmd_tlv));
469                 rts_threshold->rts_thr = cpu_to_le16(bss_cfg->rts_threshold);
470                 cmd_size += sizeof(struct host_cmd_tlv_frag_threshold);
471                 tlv += sizeof(struct host_cmd_tlv_frag_threshold);
472         }
473         if ((bss_cfg->frag_threshold >= MWIFIEX_FRAG_MIN_VALUE) &&
474             (bss_cfg->frag_threshold <= MWIFIEX_FRAG_MAX_VALUE)) {
475                 frag_threshold = (struct host_cmd_tlv_frag_threshold *)tlv;
476                 frag_threshold->tlv.type =
477                                 cpu_to_le16(TLV_TYPE_UAP_FRAG_THRESHOLD);
478                 frag_threshold->tlv.len =
479                         cpu_to_le16(sizeof(struct host_cmd_tlv_frag_threshold) -
480                                     sizeof(struct host_cmd_tlv));
481                 frag_threshold->frag_thr = cpu_to_le16(bss_cfg->frag_threshold);
482                 cmd_size += sizeof(struct host_cmd_tlv_frag_threshold);
483                 tlv += sizeof(struct host_cmd_tlv_frag_threshold);
484         }
485         if (bss_cfg->retry_limit <= MWIFIEX_RETRY_LIMIT) {
486                 retry_limit = (struct host_cmd_tlv_retry_limit *)tlv;
487                 retry_limit->tlv.type = cpu_to_le16(TLV_TYPE_UAP_RETRY_LIMIT);
488                 retry_limit->tlv.len =
489                         cpu_to_le16(sizeof(struct host_cmd_tlv_retry_limit) -
490                                     sizeof(struct host_cmd_tlv));
491                 retry_limit->limit = (u8)bss_cfg->retry_limit;
492                 cmd_size += sizeof(struct host_cmd_tlv_retry_limit);
493                 tlv += sizeof(struct host_cmd_tlv_retry_limit);
494         }
495         if ((bss_cfg->protocol & PROTOCOL_WPA) ||
496             (bss_cfg->protocol & PROTOCOL_WPA2) ||
497             (bss_cfg->protocol & PROTOCOL_EAP))
498                 mwifiex_uap_bss_wpa(&tlv, cmd_buf, &cmd_size);
499         else
500                 mwifiex_uap_bss_wep(&tlv, cmd_buf, &cmd_size);
501
502         if ((bss_cfg->auth_mode <= WLAN_AUTH_SHARED_KEY) ||
503             (bss_cfg->auth_mode == MWIFIEX_AUTH_MODE_AUTO)) {
504                 auth_type = (struct host_cmd_tlv_auth_type *)tlv;
505                 auth_type->tlv.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
506                 auth_type->tlv.len =
507                         cpu_to_le16(sizeof(struct host_cmd_tlv_auth_type) -
508                         sizeof(struct host_cmd_tlv));
509                 auth_type->auth_type = (u8)bss_cfg->auth_mode;
510                 cmd_size += sizeof(struct host_cmd_tlv_auth_type);
511                 tlv += sizeof(struct host_cmd_tlv_auth_type);
512         }
513         if (bss_cfg->protocol) {
514                 encrypt_protocol = (struct host_cmd_tlv_encrypt_protocol *)tlv;
515                 encrypt_protocol->tlv.type =
516                         cpu_to_le16(TLV_TYPE_UAP_ENCRY_PROTOCOL);
517                 encrypt_protocol->tlv.len =
518                         cpu_to_le16(sizeof(struct host_cmd_tlv_encrypt_protocol)
519                         - sizeof(struct host_cmd_tlv));
520                 encrypt_protocol->proto = cpu_to_le16(bss_cfg->protocol);
521                 cmd_size += sizeof(struct host_cmd_tlv_encrypt_protocol);
522                 tlv += sizeof(struct host_cmd_tlv_encrypt_protocol);
523         }
524
525         if (bss_cfg->ht_cap.cap_info) {
526                 htcap = (struct mwifiex_ie_types_htcap *)tlv;
527                 htcap->header.type = cpu_to_le16(WLAN_EID_HT_CAPABILITY);
528                 htcap->header.len =
529                                 cpu_to_le16(sizeof(struct ieee80211_ht_cap));
530                 htcap->ht_cap.cap_info = bss_cfg->ht_cap.cap_info;
531                 htcap->ht_cap.ampdu_params_info =
532                                              bss_cfg->ht_cap.ampdu_params_info;
533                 memcpy(&htcap->ht_cap.mcs, &bss_cfg->ht_cap.mcs,
534                        sizeof(struct ieee80211_mcs_info));
535                 htcap->ht_cap.extended_ht_cap_info =
536                                         bss_cfg->ht_cap.extended_ht_cap_info;
537                 htcap->ht_cap.tx_BF_cap_info = bss_cfg->ht_cap.tx_BF_cap_info;
538                 htcap->ht_cap.antenna_selection_info =
539                                         bss_cfg->ht_cap.antenna_selection_info;
540                 cmd_size += sizeof(struct mwifiex_ie_types_htcap);
541                 tlv += sizeof(struct mwifiex_ie_types_htcap);
542         }
543
544         if (bss_cfg->wmm_info.qos_info != 0xFF) {
545                 wmm_cap = (struct mwifiex_ie_types_wmmcap *)tlv;
546                 wmm_cap->header.type = cpu_to_le16(WLAN_EID_VENDOR_SPECIFIC);
547                 wmm_cap->header.len = cpu_to_le16(sizeof(wmm_cap->wmm_info));
548                 memcpy(&wmm_cap->wmm_info, &bss_cfg->wmm_info,
549                        sizeof(wmm_cap->wmm_info));
550                 cmd_size += sizeof(struct mwifiex_ie_types_wmmcap);
551                 tlv += sizeof(struct mwifiex_ie_types_wmmcap);
552         }
553
554         if (bss_cfg->sta_ao_timer) {
555                 ao_timer = (struct host_cmd_tlv_ageout_timer *)tlv;
556                 ao_timer->tlv.type = cpu_to_le16(TLV_TYPE_UAP_AO_TIMER);
557                 ao_timer->tlv.len = cpu_to_le16(sizeof(*ao_timer) -
558                                                 sizeof(struct host_cmd_tlv));
559                 ao_timer->sta_ao_timer = cpu_to_le32(bss_cfg->sta_ao_timer);
560                 cmd_size += sizeof(*ao_timer);
561                 tlv += sizeof(*ao_timer);
562         }
563
564         if (bss_cfg->ps_sta_ao_timer) {
565                 ps_ao_timer = (struct host_cmd_tlv_ageout_timer *)tlv;
566                 ps_ao_timer->tlv.type = cpu_to_le16(TLV_TYPE_UAP_PS_AO_TIMER);
567                 ps_ao_timer->tlv.len = cpu_to_le16(sizeof(*ps_ao_timer) -
568                                                    sizeof(struct host_cmd_tlv));
569                 ps_ao_timer->sta_ao_timer =
570                                         cpu_to_le32(bss_cfg->ps_sta_ao_timer);
571                 cmd_size += sizeof(*ps_ao_timer);
572                 tlv += sizeof(*ps_ao_timer);
573         }
574
575         *param_size = cmd_size;
576
577         return 0;
578 }
579
580 /* This function parses custom IEs from IE list and prepares command buffer */
581 static int mwifiex_uap_custom_ie_prepare(u8 *tlv, void *cmd_buf, u16 *ie_size)
582 {
583         struct mwifiex_ie_list *ap_ie = cmd_buf;
584         struct host_cmd_tlv *tlv_ie = (struct host_cmd_tlv *)tlv;
585
586         if (!ap_ie || !ap_ie->len || !ap_ie->ie_list)
587                 return -1;
588
589         *ie_size += le16_to_cpu(ap_ie->len) + sizeof(struct host_cmd_tlv);
590
591         tlv_ie->type = cpu_to_le16(TLV_TYPE_MGMT_IE);
592         tlv_ie->len = ap_ie->len;
593         tlv += sizeof(struct host_cmd_tlv);
594
595         memcpy(tlv, ap_ie->ie_list, le16_to_cpu(ap_ie->len));
596
597         return 0;
598 }
599
600 /* Parse AP config structure and prepare TLV based command structure
601  * to be sent to FW for uAP configuration
602  */
603 static int
604 mwifiex_cmd_uap_sys_config(struct host_cmd_ds_command *cmd, u16 cmd_action,
605                            u32 type, void *cmd_buf)
606 {
607         u8 *tlv;
608         u16 cmd_size, param_size, ie_size;
609         struct host_cmd_ds_sys_config *sys_cfg;
610
611         cmd->command = cpu_to_le16(HostCmd_CMD_UAP_SYS_CONFIG);
612         cmd_size = (u16)(sizeof(struct host_cmd_ds_sys_config) + S_DS_GEN);
613         sys_cfg = (struct host_cmd_ds_sys_config *)&cmd->params.uap_sys_config;
614         sys_cfg->action = cpu_to_le16(cmd_action);
615         tlv = sys_cfg->tlv;
616
617         switch (type) {
618         case UAP_BSS_PARAMS_I:
619                 param_size = cmd_size;
620                 if (mwifiex_uap_bss_param_prepare(tlv, cmd_buf, &param_size))
621                         return -1;
622                 cmd->size = cpu_to_le16(param_size);
623                 break;
624         case UAP_CUSTOM_IE_I:
625                 ie_size = cmd_size;
626                 if (mwifiex_uap_custom_ie_prepare(tlv, cmd_buf, &ie_size))
627                         return -1;
628                 cmd->size = cpu_to_le16(ie_size);
629                 break;
630         default:
631                 return -1;
632         }
633
634         return 0;
635 }
636
637 /* This function prepares the AP specific commands before sending them
638  * to the firmware.
639  * This is a generic function which calls specific command preparation
640  * routines based upon the command number.
641  */
642 int mwifiex_uap_prepare_cmd(struct mwifiex_private *priv, u16 cmd_no,
643                             u16 cmd_action, u32 type,
644                             void *data_buf, void *cmd_buf)
645 {
646         struct host_cmd_ds_command *cmd = cmd_buf;
647
648         switch (cmd_no) {
649         case HostCmd_CMD_UAP_SYS_CONFIG:
650                 if (mwifiex_cmd_uap_sys_config(cmd, cmd_action, type, data_buf))
651                         return -1;
652                 break;
653         case HostCmd_CMD_UAP_BSS_START:
654         case HostCmd_CMD_UAP_BSS_STOP:
655                 cmd->command = cpu_to_le16(cmd_no);
656                 cmd->size = cpu_to_le16(S_DS_GEN);
657                 break;
658         default:
659                 dev_err(priv->adapter->dev,
660                         "PREP_CMD: unknown cmd %#x\n", cmd_no);
661                 return -1;
662         }
663
664         return 0;
665 }