wireless: mwifiex: initial commit for Marvell mwifiex driver
[pandora-kernel.git] / drivers / net / wireless / mwifiex / sta_cmd.c
1 /*
2  * Marvell Wireless LAN device driver: station command handling
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 /*
29  * This function prepares command to set/get RSSI information.
30  *
31  * Preparation includes -
32  *      - Setting command ID, action and proper size
33  *      - Setting data/beacon average factors
34  *      - Resetting SNR/NF/RSSI values in private structure
35  *      - Ensuring correct endian-ness
36  */
37 static int
38 mwifiex_cmd_802_11_rssi_info(struct mwifiex_private *priv,
39                              struct host_cmd_ds_command *cmd, u16 cmd_action)
40 {
41         cmd->command = cpu_to_le16(HostCmd_CMD_RSSI_INFO);
42         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_rssi_info) +
43                                 S_DS_GEN);
44         cmd->params.rssi_info.action = cpu_to_le16(cmd_action);
45         cmd->params.rssi_info.ndata = cpu_to_le16(priv->data_avg_factor);
46         cmd->params.rssi_info.nbcn = cpu_to_le16(priv->bcn_avg_factor);
47
48         /* Reset SNR/NF/RSSI values in private structure */
49         priv->data_rssi_last = 0;
50         priv->data_nf_last = 0;
51         priv->data_rssi_avg = 0;
52         priv->data_nf_avg = 0;
53         priv->bcn_rssi_last = 0;
54         priv->bcn_nf_last = 0;
55         priv->bcn_rssi_avg = 0;
56         priv->bcn_nf_avg = 0;
57
58         return 0;
59 }
60
61 /*
62  * This function prepares command to set MAC control.
63  *
64  * Preparation includes -
65  *      - Setting command ID, action and proper size
66  *      - Ensuring correct endian-ness
67  */
68 static int mwifiex_cmd_mac_control(struct mwifiex_private *priv,
69                                    struct host_cmd_ds_command *cmd,
70                                    u16 cmd_action, void *data_buf)
71 {
72         struct host_cmd_ds_mac_control *mac_ctrl = &cmd->params.mac_ctrl;
73         u16 action = *((u16 *) data_buf);
74
75         if (cmd_action != HostCmd_ACT_GEN_SET) {
76                 dev_err(priv->adapter->dev,
77                         "mac_control: only support set cmd\n");
78                 return -1;
79         }
80
81         cmd->command = cpu_to_le16(HostCmd_CMD_MAC_CONTROL);
82         cmd->size =
83                 cpu_to_le16(sizeof(struct host_cmd_ds_mac_control) + S_DS_GEN);
84         mac_ctrl->action = cpu_to_le16(action);
85
86         return 0;
87 }
88
89 /*
90  * This function prepares command to set/get SNMP MIB.
91  *
92  * Preparation includes -
93  *      - Setting command ID, action and proper size
94  *      - Setting SNMP MIB OID number and value
95  *        (as required)
96  *      - Ensuring correct endian-ness
97  *
98  * The following SNMP MIB OIDs are supported -
99  *      - FRAG_THRESH_I     : Fragmentation threshold
100  *      - RTS_THRESH_I      : RTS threshold
101  *      - SHORT_RETRY_LIM_I : Short retry limit
102  *      - DOT11D_I          : 11d support
103  */
104 static int mwifiex_cmd_802_11_snmp_mib(struct mwifiex_private *priv,
105                                        struct host_cmd_ds_command *cmd,
106                                        u16 cmd_action, u32 cmd_oid,
107                                        void *data_buf)
108 {
109         struct host_cmd_ds_802_11_snmp_mib *snmp_mib = &cmd->params.smib;
110         u32 ul_temp;
111
112         dev_dbg(priv->adapter->dev, "cmd: SNMP_CMD: cmd_oid = 0x%x\n", cmd_oid);
113         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_SNMP_MIB);
114         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_snmp_mib)
115                 - 1 + S_DS_GEN);
116
117         if (cmd_action == HostCmd_ACT_GEN_GET) {
118                 snmp_mib->query_type = cpu_to_le16(HostCmd_ACT_GEN_GET);
119                 snmp_mib->buf_size = cpu_to_le16(MAX_SNMP_BUF_SIZE);
120                 cmd->size = cpu_to_le16(le16_to_cpu(cmd->size)
121                         + MAX_SNMP_BUF_SIZE);
122         }
123
124         switch (cmd_oid) {
125         case FRAG_THRESH_I:
126                 snmp_mib->oid = cpu_to_le16((u16) FRAG_THRESH_I);
127                 if (cmd_action == HostCmd_ACT_GEN_SET) {
128                         snmp_mib->query_type = cpu_to_le16(HostCmd_ACT_GEN_SET);
129                         snmp_mib->buf_size = cpu_to_le16(sizeof(u16));
130                         ul_temp = *((u32 *) data_buf);
131                         *((__le16 *) (snmp_mib->value)) =
132                                 cpu_to_le16((u16) ul_temp);
133                         cmd->size = cpu_to_le16(le16_to_cpu(cmd->size)
134                                 + sizeof(u16));
135                 }
136                 break;
137         case RTS_THRESH_I:
138                 snmp_mib->oid = cpu_to_le16((u16) RTS_THRESH_I);
139                 if (cmd_action == HostCmd_ACT_GEN_SET) {
140                         snmp_mib->query_type = cpu_to_le16(HostCmd_ACT_GEN_SET);
141                         snmp_mib->buf_size = cpu_to_le16(sizeof(u16));
142                         ul_temp = *((u32 *) data_buf);
143                         *(__le16 *) (snmp_mib->value) =
144                                 cpu_to_le16((u16) ul_temp);
145                         cmd->size = cpu_to_le16(le16_to_cpu(cmd->size)
146                                 + sizeof(u16));
147                 }
148                 break;
149
150         case SHORT_RETRY_LIM_I:
151                 snmp_mib->oid = cpu_to_le16((u16) SHORT_RETRY_LIM_I);
152                 if (cmd_action == HostCmd_ACT_GEN_SET) {
153                         snmp_mib->query_type = cpu_to_le16(HostCmd_ACT_GEN_SET);
154                         snmp_mib->buf_size = cpu_to_le16(sizeof(u16));
155                         ul_temp = (*(u32 *) data_buf);
156                         *((__le16 *) (snmp_mib->value)) =
157                                 cpu_to_le16((u16) ul_temp);
158                         cmd->size = cpu_to_le16(le16_to_cpu(cmd->size)
159                                 + sizeof(u16));
160                 }
161                 break;
162         case DOT11D_I:
163                 snmp_mib->oid = cpu_to_le16((u16) DOT11D_I);
164                 if (cmd_action == HostCmd_ACT_GEN_SET) {
165                         snmp_mib->query_type = cpu_to_le16(HostCmd_ACT_GEN_SET);
166                         snmp_mib->buf_size = cpu_to_le16(sizeof(u16));
167                         ul_temp = *(u32 *) data_buf;
168                         *((__le16 *) (snmp_mib->value)) =
169                                 cpu_to_le16((u16) ul_temp);
170                         cmd->size = cpu_to_le16(le16_to_cpu(cmd->size)
171                                 + sizeof(u16));
172                 }
173                 break;
174         default:
175                 break;
176         }
177         dev_dbg(priv->adapter->dev,
178                 "cmd: SNMP_CMD: Action=0x%x, OID=0x%x, OIDSize=0x%x,"
179                 " Value=0x%x\n",
180                cmd_action, cmd_oid, le16_to_cpu(snmp_mib->buf_size),
181                le16_to_cpu(*(__le16 *) snmp_mib->value));
182         return 0;
183 }
184
185 /*
186  * This function prepares command to get log.
187  *
188  * Preparation includes -
189  *      - Setting command ID and proper size
190  *      - Ensuring correct endian-ness
191  */
192 static int
193 mwifiex_cmd_802_11_get_log(struct mwifiex_private *priv,
194                            struct host_cmd_ds_command *cmd)
195 {
196         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_GET_LOG);
197         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_get_log) +
198                                 S_DS_GEN);
199         return 0;
200 }
201
202 /*
203  * This function prepares command to set/get Tx data rate configuration.
204  *
205  * Preparation includes -
206  *      - Setting command ID, action and proper size
207  *      - Setting configuration index, rate scope and rate drop pattern
208  *        parameters (as required)
209  *      - Ensuring correct endian-ness
210  */
211 static int mwifiex_cmd_tx_rate_cfg(struct mwifiex_private *priv,
212                                    struct host_cmd_ds_command *cmd,
213                                    u16 cmd_action, void *data_buf)
214 {
215         struct host_cmd_ds_tx_rate_cfg *rate_cfg = &cmd->params.tx_rate_cfg;
216         struct mwifiex_rate_scope *rate_scope;
217         struct mwifiex_rate_drop_pattern *rate_drop;
218         u16 *pbitmap_rates = (u16 *) data_buf;
219
220         u32 i;
221
222         cmd->command = cpu_to_le16(HostCmd_CMD_TX_RATE_CFG);
223
224         rate_cfg->action = cpu_to_le16(cmd_action);
225         rate_cfg->cfg_index = 0;
226
227         rate_scope = (struct mwifiex_rate_scope *) ((u8 *) rate_cfg +
228                       sizeof(struct host_cmd_ds_tx_rate_cfg));
229         rate_scope->type = cpu_to_le16(TLV_TYPE_RATE_SCOPE);
230         rate_scope->length = cpu_to_le16(sizeof(struct mwifiex_rate_scope) -
231                         sizeof(struct mwifiex_ie_types_header));
232         if (pbitmap_rates != NULL) {
233                 rate_scope->hr_dsss_rate_bitmap = cpu_to_le16(pbitmap_rates[0]);
234                 rate_scope->ofdm_rate_bitmap = cpu_to_le16(pbitmap_rates[1]);
235                 for (i = 0;
236                      i < sizeof(rate_scope->ht_mcs_rate_bitmap) / sizeof(u16);
237                      i++)
238                         rate_scope->ht_mcs_rate_bitmap[i] =
239                                 cpu_to_le16(pbitmap_rates[2 + i]);
240         } else {
241                 rate_scope->hr_dsss_rate_bitmap =
242                         cpu_to_le16(priv->bitmap_rates[0]);
243                 rate_scope->ofdm_rate_bitmap =
244                         cpu_to_le16(priv->bitmap_rates[1]);
245                 for (i = 0;
246                      i < sizeof(rate_scope->ht_mcs_rate_bitmap) / sizeof(u16);
247                      i++)
248                         rate_scope->ht_mcs_rate_bitmap[i] =
249                                 cpu_to_le16(priv->bitmap_rates[2 + i]);
250         }
251
252         rate_drop = (struct mwifiex_rate_drop_pattern *) ((u8 *) rate_scope +
253                         sizeof(struct mwifiex_rate_scope));
254         rate_drop->type = cpu_to_le16(TLV_TYPE_RATE_DROP_CONTROL);
255         rate_drop->length = cpu_to_le16(sizeof(rate_drop->rate_drop_mode));
256         rate_drop->rate_drop_mode = 0;
257
258         cmd->size =
259                 cpu_to_le16(S_DS_GEN + sizeof(struct host_cmd_ds_tx_rate_cfg) +
260                             sizeof(struct mwifiex_rate_scope) +
261                             sizeof(struct mwifiex_rate_drop_pattern));
262
263         return 0;
264 }
265
266 /*
267  * This function prepares command to set/get Tx power configuration.
268  *
269  * Preparation includes -
270  *      - Setting command ID, action and proper size
271  *      - Setting Tx power mode, power group TLV
272  *        (as required)
273  *      - Ensuring correct endian-ness
274  */
275 static int mwifiex_cmd_tx_power_cfg(struct mwifiex_private *priv,
276                                     struct host_cmd_ds_command *cmd,
277                                     u16 cmd_action, void *data_buf)
278 {
279         struct mwifiex_types_power_group *pg_tlv = NULL;
280         struct host_cmd_ds_txpwr_cfg *txp = NULL;
281         struct host_cmd_ds_txpwr_cfg *cmd_txp_cfg = &cmd->params.txp_cfg;
282
283         cmd->command = cpu_to_le16(HostCmd_CMD_TXPWR_CFG);
284         cmd->size =
285                 cpu_to_le16(S_DS_GEN + sizeof(struct host_cmd_ds_txpwr_cfg));
286         switch (cmd_action) {
287         case HostCmd_ACT_GEN_SET:
288                 txp = (struct host_cmd_ds_txpwr_cfg *) data_buf;
289                 if (txp->mode) {
290                         pg_tlv = (struct mwifiex_types_power_group
291                                   *) ((unsigned long) data_buf +
292                                      sizeof(struct host_cmd_ds_txpwr_cfg));
293                         memmove(cmd_txp_cfg, data_buf,
294                                 sizeof(struct host_cmd_ds_txpwr_cfg) +
295                                 sizeof(struct mwifiex_types_power_group) +
296                                 pg_tlv->length);
297
298                         pg_tlv = (struct mwifiex_types_power_group *) ((u8 *)
299                                   cmd_txp_cfg +
300                                   sizeof(struct host_cmd_ds_txpwr_cfg));
301                         cmd->size = cpu_to_le16(le16_to_cpu(cmd->size) +
302                                   sizeof(struct mwifiex_types_power_group) +
303                                   pg_tlv->length);
304                 } else {
305                         memmove(cmd_txp_cfg, data_buf,
306                                 sizeof(struct host_cmd_ds_txpwr_cfg));
307                 }
308                 cmd_txp_cfg->action = cpu_to_le16(cmd_action);
309                 break;
310         case HostCmd_ACT_GEN_GET:
311                 cmd_txp_cfg->action = cpu_to_le16(cmd_action);
312                 break;
313         }
314
315         return 0;
316 }
317
318 /*
319  * This function prepares command to set Host Sleep configuration.
320  *
321  * Preparation includes -
322  *      - Setting command ID and proper size
323  *      - Setting Host Sleep action, conditions, ARP filters
324  *        (as required)
325  *      - Ensuring correct endian-ness
326  */
327 static int mwifiex_cmd_802_11_hs_cfg(struct mwifiex_private *priv,
328                                      struct host_cmd_ds_command *cmd,
329                                      u16 cmd_action,
330                                      struct mwifiex_hs_config_param *data_buf)
331 {
332         struct mwifiex_adapter *adapter = priv->adapter;
333         struct host_cmd_ds_802_11_hs_cfg_enh *hs_cfg = &cmd->params.opt_hs_cfg;
334         u16 hs_activate = false;
335
336         if (data_buf == NULL)
337                 /* New Activate command */
338                 hs_activate = true;
339         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH);
340
341         if (!hs_activate &&
342             (data_buf->conditions
343             != cpu_to_le32(HOST_SLEEP_CFG_CANCEL))
344             && ((adapter->arp_filter_size > 0)
345                 && (adapter->arp_filter_size <= ARP_FILTER_MAX_BUF_SIZE))) {
346                 dev_dbg(adapter->dev,
347                         "cmd: Attach %d bytes ArpFilter to HSCfg cmd\n",
348                        adapter->arp_filter_size);
349                 memcpy(((u8 *) hs_cfg) +
350                        sizeof(struct host_cmd_ds_802_11_hs_cfg_enh),
351                        adapter->arp_filter, adapter->arp_filter_size);
352                 cmd->size = cpu_to_le16(adapter->arp_filter_size +
353                                     sizeof(struct host_cmd_ds_802_11_hs_cfg_enh)
354                                     + S_DS_GEN);
355         } else {
356                 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(struct
357                                            host_cmd_ds_802_11_hs_cfg_enh));
358         }
359         if (hs_activate) {
360                 hs_cfg->action = cpu_to_le16(HS_ACTIVATE);
361                 hs_cfg->params.hs_activate.resp_ctrl = RESP_NEEDED;
362         } else {
363                 hs_cfg->action = cpu_to_le16(HS_CONFIGURE);
364                 hs_cfg->params.hs_config.conditions = data_buf->conditions;
365                 hs_cfg->params.hs_config.gpio = data_buf->gpio;
366                 hs_cfg->params.hs_config.gap = data_buf->gap;
367                 dev_dbg(adapter->dev,
368                         "cmd: HS_CFG_CMD: condition:0x%x gpio:0x%x gap:0x%x\n",
369                        hs_cfg->params.hs_config.conditions,
370                        hs_cfg->params.hs_config.gpio,
371                        hs_cfg->params.hs_config.gap);
372         }
373
374         return 0;
375 }
376
377 /*
378  * This function prepares command to set/get MAC address.
379  *
380  * Preparation includes -
381  *      - Setting command ID, action and proper size
382  *      - Setting MAC address (for SET only)
383  *      - Ensuring correct endian-ness
384  */
385 static int mwifiex_cmd_802_11_mac_address(struct mwifiex_private *priv,
386                                           struct host_cmd_ds_command *cmd,
387                                           u16 cmd_action)
388 {
389         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_MAC_ADDRESS);
390         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_mac_address) +
391                                 S_DS_GEN);
392         cmd->result = 0;
393
394         cmd->params.mac_addr.action = cpu_to_le16(cmd_action);
395
396         if (cmd_action == HostCmd_ACT_GEN_SET)
397                 memcpy(cmd->params.mac_addr.mac_addr, priv->curr_addr,
398                        ETH_ALEN);
399         return 0;
400 }
401
402 /*
403  * This function prepares command to set MAC multicast address.
404  *
405  * Preparation includes -
406  *      - Setting command ID, action and proper size
407  *      - Setting MAC multicast address
408  *      - Ensuring correct endian-ness
409  */
410 static int mwifiex_cmd_mac_multicast_adr(struct mwifiex_private *priv,
411                                          struct host_cmd_ds_command *cmd,
412                                          u16 cmd_action, void *data_buf)
413 {
414         struct mwifiex_multicast_list *mcast_list =
415                 (struct mwifiex_multicast_list *) data_buf;
416         struct host_cmd_ds_mac_multicast_adr *mcast_addr = &cmd->params.mc_addr;
417
418         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_mac_multicast_adr) +
419                                 S_DS_GEN);
420         cmd->command = cpu_to_le16(HostCmd_CMD_MAC_MULTICAST_ADR);
421
422         mcast_addr->action = cpu_to_le16(cmd_action);
423         mcast_addr->num_of_adrs =
424                 cpu_to_le16((u16) mcast_list->num_multicast_addr);
425         memcpy(mcast_addr->mac_list, mcast_list->mac_list,
426                mcast_list->num_multicast_addr * ETH_ALEN);
427
428         return 0;
429 }
430
431 /*
432  * This function prepares command to deauthenticate.
433  *
434  * Preparation includes -
435  *      - Setting command ID and proper size
436  *      - Setting AP MAC address and reason code
437  *      - Ensuring correct endian-ness
438  */
439 static int mwifiex_cmd_802_11_deauthenticate(struct mwifiex_private *priv,
440                                              struct host_cmd_ds_command *cmd,
441                                              void *data_buf)
442 {
443         struct host_cmd_ds_802_11_deauthenticate *deauth = &cmd->params.deauth;
444
445         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_DEAUTHENTICATE);
446         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_deauthenticate)
447                                 + S_DS_GEN);
448
449         /* Set AP MAC address */
450         memcpy(deauth->mac_addr, (u8 *) data_buf, ETH_ALEN);
451
452         dev_dbg(priv->adapter->dev, "cmd: Deauth: %pM\n", deauth->mac_addr);
453
454         deauth->reason_code = cpu_to_le16(WLAN_REASON_DEAUTH_LEAVING);
455
456         return 0;
457 }
458
459 /*
460  * This function prepares command to stop Ad-Hoc network.
461  *
462  * Preparation includes -
463  *      - Setting command ID and proper size
464  *      - Ensuring correct endian-ness
465  */
466 static int mwifiex_cmd_802_11_ad_hoc_stop(struct mwifiex_private *priv,
467                                           struct host_cmd_ds_command *cmd)
468 {
469         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_STOP);
470         cmd->size = cpu_to_le16(S_DS_GEN);
471         return 0;
472 }
473
474 /*
475  * This function sets WEP key(s) to key parameter TLV(s).
476  *
477  * Multi-key parameter TLVs are supported, so we can send multiple
478  * WEP keys in a single buffer.
479  */
480 static int
481 mwifiex_set_keyparamset_wep(struct mwifiex_private *priv,
482                             struct mwifiex_ie_type_key_param_set *key_param_set,
483                             u16 *key_param_len)
484 {
485         int cur_key_param_len = 0;
486         u8 i;
487
488         /* Multi-key_param_set TLV is supported */
489         for (i = 0; i < NUM_WEP_KEYS; i++) {
490                 if ((priv->wep_key[i].key_length == WLAN_KEY_LEN_WEP40) ||
491                     (priv->wep_key[i].key_length == WLAN_KEY_LEN_WEP104)) {
492                         key_param_set->type =
493                                 cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
494 /* Key_param_set WEP fixed length */
495 #define KEYPARAMSET_WEP_FIXED_LEN 8
496                         key_param_set->length = cpu_to_le16((u16)
497                                         (priv->wep_key[i].
498                                          key_length +
499                                          KEYPARAMSET_WEP_FIXED_LEN));
500                         key_param_set->key_type_id =
501                                 cpu_to_le16(KEY_TYPE_ID_WEP);
502                         key_param_set->key_info =
503                                 cpu_to_le16(KEY_INFO_WEP_ENABLED |
504                                             KEY_INFO_WEP_UNICAST |
505                                             KEY_INFO_WEP_MCAST);
506                         key_param_set->key_len =
507                                 cpu_to_le16(priv->wep_key[i].key_length);
508                         /* Set WEP key index */
509                         key_param_set->key[0] = i;
510                         /* Set default Tx key flag */
511                         if (i ==
512                             (priv->
513                              wep_key_curr_index & HostCmd_WEP_KEY_INDEX_MASK))
514                                 key_param_set->key[1] = 1;
515                         else
516                                 key_param_set->key[1] = 0;
517                         memmove(&key_param_set->key[2],
518                                 priv->wep_key[i].key_material,
519                                 priv->wep_key[i].key_length);
520
521                         cur_key_param_len = priv->wep_key[i].key_length +
522                                 KEYPARAMSET_WEP_FIXED_LEN +
523                                 sizeof(struct mwifiex_ie_types_header);
524                         *key_param_len += (u16) cur_key_param_len;
525                         key_param_set =
526                                 (struct mwifiex_ie_type_key_param_set *)
527                                                 ((u8 *)key_param_set +
528                                                 cur_key_param_len);
529                 } else if (!priv->wep_key[i].key_length) {
530                         continue;
531                 } else {
532                         dev_err(priv->adapter->dev,
533                                 "key%d Length = %d is incorrect\n",
534                                (i + 1), priv->wep_key[i].key_length);
535                         return -1;
536                 }
537         }
538
539         return 0;
540 }
541
542 /*
543  * This function prepares command to set/get/reset network key(s).
544  *
545  * Preparation includes -
546  *      - Setting command ID, action and proper size
547  *      - Setting WEP keys, WAPI keys or WPA keys along with required
548  *        encryption (TKIP, AES) (as required)
549  *      - Ensuring correct endian-ness
550  */
551 static int mwifiex_cmd_802_11_key_material(struct mwifiex_private *priv,
552                                            struct host_cmd_ds_command *cmd,
553                                            u16 cmd_action,
554                                            u32 cmd_oid, void *data_buf)
555 {
556         struct host_cmd_ds_802_11_key_material *key_material =
557                 &cmd->params.key_material;
558         struct mwifiex_ds_encrypt_key *enc_key =
559                 (struct mwifiex_ds_encrypt_key *) data_buf;
560         u16 key_param_len = 0;
561         int ret = 0;
562         const u8 bc_mac[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
563
564         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_KEY_MATERIAL);
565         key_material->action = cpu_to_le16(cmd_action);
566
567         if (cmd_action == HostCmd_ACT_GEN_GET) {
568                 cmd->size =
569                         cpu_to_le16(sizeof(key_material->action) + S_DS_GEN);
570                 return ret;
571         }
572
573         if (!enc_key) {
574                 memset(&key_material->key_param_set, 0,
575                        (NUM_WEP_KEYS *
576                         sizeof(struct mwifiex_ie_type_key_param_set)));
577                 ret = mwifiex_set_keyparamset_wep(priv,
578                                                   &key_material->key_param_set,
579                                                   &key_param_len);
580                 cmd->size = cpu_to_le16(key_param_len +
581                                     sizeof(key_material->action) + S_DS_GEN);
582                 return ret;
583         } else
584                 memset(&key_material->key_param_set, 0,
585                        sizeof(struct mwifiex_ie_type_key_param_set));
586         if (enc_key->is_wapi_key) {
587                 dev_dbg(priv->adapter->dev, "info: Set WAPI Key\n");
588                 key_material->key_param_set.key_type_id =
589                         cpu_to_le16(KEY_TYPE_ID_WAPI);
590                 if (cmd_oid == KEY_INFO_ENABLED)
591                         key_material->key_param_set.key_info =
592                                 cpu_to_le16(KEY_INFO_WAPI_ENABLED);
593                 else
594                         key_material->key_param_set.key_info =
595                                 cpu_to_le16(!KEY_INFO_WAPI_ENABLED);
596
597                 key_material->key_param_set.key[0] = enc_key->key_index;
598                 if (!priv->sec_info.wapi_key_on)
599                         key_material->key_param_set.key[1] = 1;
600                 else
601                         /* set 0 when re-key */
602                         key_material->key_param_set.key[1] = 0;
603
604                 if (0 != memcmp(enc_key->mac_addr, bc_mac, sizeof(bc_mac))) {
605                         /* WAPI pairwise key: unicast */
606                         key_material->key_param_set.key_info |=
607                                 cpu_to_le16(KEY_INFO_WAPI_UNICAST);
608                 } else {        /* WAPI group key: multicast */
609                         key_material->key_param_set.key_info |=
610                                 cpu_to_le16(KEY_INFO_WAPI_MCAST);
611                         priv->sec_info.wapi_key_on = true;
612                 }
613
614                 key_material->key_param_set.type =
615                         cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
616                 key_material->key_param_set.key_len =
617                         cpu_to_le16(WAPI_KEY_LEN);
618                 memcpy(&key_material->key_param_set.key[2],
619                        enc_key->key_material, enc_key->key_len);
620                 memcpy(&key_material->key_param_set.key[2 + enc_key->key_len],
621                        enc_key->wapi_rxpn, WAPI_RXPN_LEN);
622                 key_material->key_param_set.length =
623                         cpu_to_le16(WAPI_KEY_LEN + KEYPARAMSET_FIXED_LEN);
624
625                 key_param_len = (WAPI_KEY_LEN + KEYPARAMSET_FIXED_LEN) +
626                                  sizeof(struct mwifiex_ie_types_header);
627                 cmd->size = cpu_to_le16(key_param_len +
628                                 sizeof(key_material->action) + S_DS_GEN);
629                 return ret;
630         }
631         if (enc_key->key_len == WLAN_KEY_LEN_CCMP) {
632                 dev_dbg(priv->adapter->dev, "cmd: WPA_AES\n");
633                 key_material->key_param_set.key_type_id =
634                         cpu_to_le16(KEY_TYPE_ID_AES);
635                 if (cmd_oid == KEY_INFO_ENABLED)
636                         key_material->key_param_set.key_info =
637                                 cpu_to_le16(KEY_INFO_AES_ENABLED);
638                 else
639                         key_material->key_param_set.key_info =
640                                 cpu_to_le16(!KEY_INFO_AES_ENABLED);
641
642                 if (enc_key->key_index & MWIFIEX_KEY_INDEX_UNICAST)
643                                 /* AES pairwise key: unicast */
644                         key_material->key_param_set.key_info |=
645                                 cpu_to_le16(KEY_INFO_AES_UNICAST);
646                 else            /* AES group key: multicast */
647                         key_material->key_param_set.key_info |=
648                                 cpu_to_le16(KEY_INFO_AES_MCAST);
649         } else if (enc_key->key_len == WLAN_KEY_LEN_TKIP) {
650                 dev_dbg(priv->adapter->dev, "cmd: WPA_TKIP\n");
651                 key_material->key_param_set.key_type_id =
652                         cpu_to_le16(KEY_TYPE_ID_TKIP);
653                 key_material->key_param_set.key_info =
654                         cpu_to_le16(KEY_INFO_TKIP_ENABLED);
655
656                 if (enc_key->key_index & MWIFIEX_KEY_INDEX_UNICAST)
657                                 /* TKIP pairwise key: unicast */
658                         key_material->key_param_set.key_info |=
659                                 cpu_to_le16(KEY_INFO_TKIP_UNICAST);
660                 else            /* TKIP group key: multicast */
661                         key_material->key_param_set.key_info |=
662                                 cpu_to_le16(KEY_INFO_TKIP_MCAST);
663         }
664
665         if (key_material->key_param_set.key_type_id) {
666                 key_material->key_param_set.type =
667                         cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
668                 key_material->key_param_set.key_len =
669                         cpu_to_le16((u16) enc_key->key_len);
670                 memcpy(key_material->key_param_set.key, enc_key->key_material,
671                        enc_key->key_len);
672                 key_material->key_param_set.length =
673                         cpu_to_le16((u16) enc_key->key_len +
674                                     KEYPARAMSET_FIXED_LEN);
675
676                 key_param_len = (u16) (enc_key->key_len + KEYPARAMSET_FIXED_LEN)
677                                       + sizeof(struct mwifiex_ie_types_header);
678
679                 cmd->size = cpu_to_le16(key_param_len +
680                                     sizeof(key_material->action) + S_DS_GEN);
681         }
682
683         return ret;
684 }
685
686 /*
687  * This function prepares command to set/get 11d domain information.
688  *
689  * Preparation includes -
690  *      - Setting command ID, action and proper size
691  *      - Setting domain information fields (for SET only)
692  *      - Ensuring correct endian-ness
693  */
694 static int mwifiex_cmd_802_11d_domain_info(struct mwifiex_private *priv,
695                                            struct host_cmd_ds_command *cmd,
696                                            u16 cmd_action)
697 {
698         struct mwifiex_adapter *adapter = priv->adapter;
699         struct host_cmd_ds_802_11d_domain_info *domain_info =
700                 &cmd->params.domain_info;
701         struct mwifiex_ietypes_domain_param_set *domain =
702                 &domain_info->domain;
703         u8 no_of_triplet = adapter->domain_reg.no_of_triplet;
704
705         dev_dbg(adapter->dev, "info: 11D: no_of_triplet=0x%x\n", no_of_triplet);
706
707         cmd->command = cpu_to_le16(HostCmd_CMD_802_11D_DOMAIN_INFO);
708         domain_info->action = cpu_to_le16(cmd_action);
709         if (cmd_action == HostCmd_ACT_GEN_GET) {
710                 cmd->size = cpu_to_le16(sizeof(domain_info->action) + S_DS_GEN);
711                 return 0;
712         }
713
714         /* Set domain info fields */
715         domain->header.type = cpu_to_le16(WLAN_EID_COUNTRY);
716         memcpy(domain->country_code, adapter->domain_reg.country_code,
717                         sizeof(domain->country_code));
718
719         domain->header.len = cpu_to_le16((no_of_triplet *
720                                 sizeof(struct ieee80211_country_ie_triplet)) +
721                                 sizeof(domain->country_code));
722
723         if (no_of_triplet) {
724                 memcpy(domain->triplet, adapter->domain_reg.triplet,
725                                 no_of_triplet *
726                                 sizeof(struct ieee80211_country_ie_triplet));
727
728                 cmd->size = cpu_to_le16(sizeof(domain_info->action) +
729                                 le16_to_cpu(domain->header.len) +
730                                 sizeof(struct mwifiex_ie_types_header)
731                                 + S_DS_GEN);
732         } else {
733                 cmd->size = cpu_to_le16(sizeof(domain_info->action) + S_DS_GEN);
734         }
735
736         return 0;
737 }
738
739 /*
740  * This function prepares command to set/get RF channel.
741  *
742  * Preparation includes -
743  *      - Setting command ID, action and proper size
744  *      - Setting RF type and current RF channel (for SET only)
745  *      - Ensuring correct endian-ness
746  */
747 static int mwifiex_cmd_802_11_rf_channel(struct mwifiex_private *priv,
748                                          struct host_cmd_ds_command *cmd,
749                                          u16 cmd_action, void *data_buf)
750 {
751         struct host_cmd_ds_802_11_rf_channel *rf_chan =
752                 &cmd->params.rf_channel;
753         uint16_t rf_type = le16_to_cpu(rf_chan->rf_type);
754
755         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_RF_CHANNEL);
756         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_rf_channel)
757                                 + S_DS_GEN);
758
759         if (cmd_action == HostCmd_ACT_GEN_SET) {
760                 if ((priv->adapter->adhoc_start_band & BAND_A)
761                     || (priv->adapter->adhoc_start_band & BAND_AN))
762                         rf_chan->rf_type =
763                                 cpu_to_le16(HostCmd_SCAN_RADIO_TYPE_A);
764
765                 rf_type = le16_to_cpu(rf_chan->rf_type);
766                 SET_SECONDARYCHAN(rf_type, priv->adapter->chan_offset);
767                 rf_chan->current_channel = cpu_to_le16(*((u16 *) data_buf));
768         }
769         rf_chan->action = cpu_to_le16(cmd_action);
770         return 0;
771 }
772
773 /*
774  * This function prepares command to set/get IBSS coalescing status.
775  *
776  * Preparation includes -
777  *      - Setting command ID, action and proper size
778  *      - Setting status to enable or disable (for SET only)
779  *      - Ensuring correct endian-ness
780  */
781 static int mwifiex_cmd_ibss_coalescing_status(struct mwifiex_private *priv,
782                                               struct host_cmd_ds_command *cmd,
783                                               u16 cmd_action, void *data_buf)
784 {
785         struct host_cmd_ds_802_11_ibss_status *ibss_coal =
786                 &(cmd->params.ibss_coalescing);
787         u16 enable = 0;
788
789         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_IBSS_COALESCING_STATUS);
790         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_ibss_status) +
791                                 S_DS_GEN);
792         cmd->result = 0;
793         ibss_coal->action = cpu_to_le16(cmd_action);
794
795         switch (cmd_action) {
796         case HostCmd_ACT_GEN_SET:
797                 if (data_buf != NULL)
798                         enable = *(u16 *) data_buf;
799                 ibss_coal->enable = cpu_to_le16(enable);
800                 break;
801
802                 /* In other case.. Nothing to do */
803         case HostCmd_ACT_GEN_GET:
804         default:
805                 break;
806         }
807
808         return 0;
809 }
810
811 /*
812  * This function prepares command to set/get register value.
813  *
814  * Preparation includes -
815  *      - Setting command ID, action and proper size
816  *      - Setting register offset (for both GET and SET) and
817  *        register value (for SET only)
818  *      - Ensuring correct endian-ness
819  *
820  * The following type of registers can be accessed with this function -
821  *      - MAC register
822  *      - BBP register
823  *      - RF register
824  *      - PMIC register
825  *      - CAU register
826  *      - EEPROM
827  */
828 static int mwifiex_cmd_reg_access(struct host_cmd_ds_command *cmd,
829                                   u16 cmd_action, void *data_buf)
830 {
831         struct mwifiex_ds_reg_rw *reg_rw;
832
833         reg_rw = (struct mwifiex_ds_reg_rw *) data_buf;
834         switch (le16_to_cpu(cmd->command)) {
835         case HostCmd_CMD_MAC_REG_ACCESS:
836         {
837                 struct host_cmd_ds_mac_reg_access *mac_reg;
838
839                 cmd->size = cpu_to_le16(sizeof(*mac_reg) + S_DS_GEN);
840                 mac_reg = (struct host_cmd_ds_mac_reg_access *) &cmd->
841                         params.mac_reg;
842                 mac_reg->action = cpu_to_le16(cmd_action);
843                 mac_reg->offset =
844                         cpu_to_le16((u16) le32_to_cpu(reg_rw->offset));
845                 mac_reg->value = reg_rw->value;
846                 break;
847         }
848         case HostCmd_CMD_BBP_REG_ACCESS:
849         {
850                 struct host_cmd_ds_bbp_reg_access *bbp_reg;
851
852                 cmd->size = cpu_to_le16(sizeof(*bbp_reg) + S_DS_GEN);
853                 bbp_reg = (struct host_cmd_ds_bbp_reg_access *) &cmd->
854                         params.bbp_reg;
855                 bbp_reg->action = cpu_to_le16(cmd_action);
856                 bbp_reg->offset =
857                         cpu_to_le16((u16) le32_to_cpu(reg_rw->offset));
858                 bbp_reg->value = (u8) le32_to_cpu(reg_rw->value);
859                 break;
860         }
861         case HostCmd_CMD_RF_REG_ACCESS:
862         {
863                 struct host_cmd_ds_rf_reg_access *rf_reg;
864
865                 cmd->size = cpu_to_le16(sizeof(*rf_reg) + S_DS_GEN);
866                 rf_reg = (struct host_cmd_ds_rf_reg_access *) &cmd->
867                         params.rf_reg;
868                 rf_reg->action = cpu_to_le16(cmd_action);
869                 rf_reg->offset =
870                         cpu_to_le16((u16) le32_to_cpu(reg_rw->offset));
871                 rf_reg->value = (u8) le32_to_cpu(reg_rw->value);
872                 break;
873         }
874         case HostCmd_CMD_PMIC_REG_ACCESS:
875         {
876                 struct host_cmd_ds_pmic_reg_access *pmic_reg;
877
878                 cmd->size = cpu_to_le16(sizeof(*pmic_reg) + S_DS_GEN);
879                 pmic_reg = (struct host_cmd_ds_pmic_reg_access *) &cmd->
880                                 params.pmic_reg;
881                 pmic_reg->action = cpu_to_le16(cmd_action);
882                 pmic_reg->offset =
883                         cpu_to_le16((u16) le32_to_cpu(reg_rw->offset));
884                 pmic_reg->value = (u8) le32_to_cpu(reg_rw->value);
885                 break;
886         }
887         case HostCmd_CMD_CAU_REG_ACCESS:
888         {
889                 struct host_cmd_ds_rf_reg_access *cau_reg;
890
891                 cmd->size = cpu_to_le16(sizeof(*cau_reg) + S_DS_GEN);
892                 cau_reg = (struct host_cmd_ds_rf_reg_access *) &cmd->
893                                 params.rf_reg;
894                 cau_reg->action = cpu_to_le16(cmd_action);
895                 cau_reg->offset =
896                         cpu_to_le16((u16) le32_to_cpu(reg_rw->offset));
897                 cau_reg->value = (u8) le32_to_cpu(reg_rw->value);
898                 break;
899         }
900         case HostCmd_CMD_802_11_EEPROM_ACCESS:
901         {
902                 struct mwifiex_ds_read_eeprom *rd_eeprom =
903                         (struct mwifiex_ds_read_eeprom *) data_buf;
904                 struct host_cmd_ds_802_11_eeprom_access *cmd_eeprom =
905                         (struct host_cmd_ds_802_11_eeprom_access *)
906                         &cmd->params.eeprom;
907
908                 cmd->size = cpu_to_le16(sizeof(*cmd_eeprom) + S_DS_GEN);
909                 cmd_eeprom->action = cpu_to_le16(cmd_action);
910                 cmd_eeprom->offset = rd_eeprom->offset;
911                 cmd_eeprom->byte_count = rd_eeprom->byte_count;
912                 cmd_eeprom->value = 0;
913                 break;
914         }
915         default:
916                 return -1;
917         }
918
919         return 0;
920 }
921
922 /*
923  * This function prepares the commands before sending them to the firmware.
924  *
925  * This is a generic function which calls specific command preparation
926  * routines based upon the command number.
927  */
928 int mwifiex_sta_prepare_cmd(struct mwifiex_private *priv, uint16_t cmd_no,
929                             u16 cmd_action, u32 cmd_oid,
930                             void *data_buf, void *cmd_buf)
931 {
932         struct host_cmd_ds_command *cmd_ptr =
933                 (struct host_cmd_ds_command *) cmd_buf;
934         int ret = 0;
935
936         /* Prepare command */
937         switch (cmd_no) {
938         case HostCmd_CMD_GET_HW_SPEC:
939                 ret = mwifiex_cmd_get_hw_spec(priv, cmd_ptr);
940                 break;
941         case HostCmd_CMD_MAC_CONTROL:
942                 ret = mwifiex_cmd_mac_control(priv, cmd_ptr, cmd_action,
943                                               data_buf);
944                 break;
945         case HostCmd_CMD_802_11_MAC_ADDRESS:
946                 ret = mwifiex_cmd_802_11_mac_address(priv, cmd_ptr,
947                                                      cmd_action);
948                 break;
949         case HostCmd_CMD_MAC_MULTICAST_ADR:
950                 ret = mwifiex_cmd_mac_multicast_adr(priv, cmd_ptr, cmd_action,
951                                                     data_buf);
952                 break;
953         case HostCmd_CMD_TX_RATE_CFG:
954                 ret = mwifiex_cmd_tx_rate_cfg(priv, cmd_ptr, cmd_action,
955                                               data_buf);
956                 break;
957         case HostCmd_CMD_TXPWR_CFG:
958                 ret = mwifiex_cmd_tx_power_cfg(priv, cmd_ptr, cmd_action,
959                                                data_buf);
960                 break;
961         case HostCmd_CMD_802_11_PS_MODE_ENH:
962                 ret = mwifiex_cmd_enh_power_mode(priv, cmd_ptr, cmd_action,
963                                                  (uint16_t)cmd_oid, data_buf);
964                 break;
965         case HostCmd_CMD_802_11_HS_CFG_ENH:
966                 ret = mwifiex_cmd_802_11_hs_cfg(priv, cmd_ptr, cmd_action,
967                                 (struct mwifiex_hs_config_param *) data_buf);
968                 break;
969         case HostCmd_CMD_802_11_SCAN:
970                 ret = mwifiex_cmd_802_11_scan(priv, cmd_ptr, data_buf);
971                 break;
972         case HostCmd_CMD_802_11_BG_SCAN_QUERY:
973                 ret = mwifiex_cmd_802_11_bg_scan_query(priv, cmd_ptr,
974                                                        data_buf);
975                 break;
976         case HostCmd_CMD_802_11_ASSOCIATE:
977                 ret = mwifiex_cmd_802_11_associate(priv, cmd_ptr, data_buf);
978                 break;
979         case HostCmd_CMD_802_11_DEAUTHENTICATE:
980                 ret = mwifiex_cmd_802_11_deauthenticate(priv, cmd_ptr,
981                                                         data_buf);
982                 break;
983         case HostCmd_CMD_802_11_AD_HOC_START:
984                 ret = mwifiex_cmd_802_11_ad_hoc_start(priv, cmd_ptr,
985                                                       data_buf);
986                 break;
987         case HostCmd_CMD_802_11_GET_LOG:
988                 ret = mwifiex_cmd_802_11_get_log(priv, cmd_ptr);
989                 break;
990         case HostCmd_CMD_802_11_AD_HOC_JOIN:
991                 ret = mwifiex_cmd_802_11_ad_hoc_join(priv, cmd_ptr,
992                                                      data_buf);
993                 break;
994         case HostCmd_CMD_802_11_AD_HOC_STOP:
995                 ret = mwifiex_cmd_802_11_ad_hoc_stop(priv, cmd_ptr);
996                 break;
997         case HostCmd_CMD_RSSI_INFO:
998                 ret = mwifiex_cmd_802_11_rssi_info(priv, cmd_ptr, cmd_action);
999                 break;
1000         case HostCmd_CMD_802_11_SNMP_MIB:
1001                 ret = mwifiex_cmd_802_11_snmp_mib(priv, cmd_ptr, cmd_action,
1002                                                   cmd_oid, data_buf);
1003                 break;
1004         case HostCmd_CMD_802_11_TX_RATE_QUERY:
1005                 cmd_ptr->command =
1006                         cpu_to_le16(HostCmd_CMD_802_11_TX_RATE_QUERY);
1007                 cmd_ptr->size =
1008                         cpu_to_le16(sizeof(struct host_cmd_ds_tx_rate_query) +
1009                                     S_DS_GEN);
1010                 priv->tx_rate = 0;
1011                 ret = 0;
1012                 break;
1013         case HostCmd_CMD_VERSION_EXT:
1014                 cmd_ptr->command = cpu_to_le16(cmd_no);
1015                 cmd_ptr->params.verext.version_str_sel =
1016                         (u8) (*((u32 *) data_buf));
1017                 memcpy(&cmd_ptr->params, data_buf,
1018                        sizeof(struct host_cmd_ds_version_ext));
1019                 cmd_ptr->size =
1020                         cpu_to_le16(sizeof(struct host_cmd_ds_version_ext) +
1021                                     S_DS_GEN);
1022                 ret = 0;
1023                 break;
1024         case HostCmd_CMD_802_11_RF_CHANNEL:
1025                 ret = mwifiex_cmd_802_11_rf_channel(priv, cmd_ptr, cmd_action,
1026                                                     data_buf);
1027                 break;
1028         case HostCmd_CMD_FUNC_INIT:
1029                 if (priv->adapter->hw_status == MWIFIEX_HW_STATUS_RESET)
1030                         priv->adapter->hw_status = MWIFIEX_HW_STATUS_READY;
1031                 cmd_ptr->command = cpu_to_le16(cmd_no);
1032                 cmd_ptr->size = cpu_to_le16(S_DS_GEN);
1033                 break;
1034         case HostCmd_CMD_FUNC_SHUTDOWN:
1035                 priv->adapter->hw_status = MWIFIEX_HW_STATUS_RESET;
1036                 cmd_ptr->command = cpu_to_le16(cmd_no);
1037                 cmd_ptr->size = cpu_to_le16(S_DS_GEN);
1038                 break;
1039         case HostCmd_CMD_11N_ADDBA_REQ:
1040                 ret = mwifiex_cmd_11n_addba_req(priv, cmd_ptr, data_buf);
1041                 break;
1042         case HostCmd_CMD_11N_DELBA:
1043                 ret = mwifiex_cmd_11n_delba(priv, cmd_ptr, data_buf);
1044                 break;
1045         case HostCmd_CMD_11N_ADDBA_RSP:
1046                 ret = mwifiex_cmd_11n_addba_rsp_gen(priv, cmd_ptr, data_buf);
1047                 break;
1048         case HostCmd_CMD_802_11_KEY_MATERIAL:
1049                 ret = mwifiex_cmd_802_11_key_material(priv, cmd_ptr,
1050                                                 cmd_action, cmd_oid,
1051                                                 data_buf);
1052                 break;
1053         case HostCmd_CMD_802_11D_DOMAIN_INFO:
1054                 ret = mwifiex_cmd_802_11d_domain_info(priv, cmd_ptr,
1055                                                 cmd_action);
1056                 break;
1057         case HostCmd_CMD_RECONFIGURE_TX_BUFF:
1058                 ret = mwifiex_cmd_recfg_tx_buf(priv, cmd_ptr, cmd_action,
1059                                                data_buf);
1060                 break;
1061         case HostCmd_CMD_AMSDU_AGGR_CTRL:
1062                 ret = mwifiex_cmd_amsdu_aggr_ctrl(priv, cmd_ptr, cmd_action,
1063                                                   data_buf);
1064                 break;
1065         case HostCmd_CMD_11N_CFG:
1066                 ret = mwifiex_cmd_11n_cfg(priv, cmd_ptr, cmd_action,
1067                                           data_buf);
1068                 break;
1069         case HostCmd_CMD_WMM_GET_STATUS:
1070                 dev_dbg(priv->adapter->dev,
1071                         "cmd: WMM: WMM_GET_STATUS cmd sent\n");
1072                 cmd_ptr->command = cpu_to_le16(HostCmd_CMD_WMM_GET_STATUS);
1073                 cmd_ptr->size =
1074                         cpu_to_le16(sizeof(struct host_cmd_ds_wmm_get_status) +
1075                                     S_DS_GEN);
1076                 ret = 0;
1077                 break;
1078         case HostCmd_CMD_802_11_IBSS_COALESCING_STATUS:
1079                 ret = mwifiex_cmd_ibss_coalescing_status(priv, cmd_ptr,
1080                                                          cmd_action, data_buf);
1081                 break;
1082         case HostCmd_CMD_MAC_REG_ACCESS:
1083         case HostCmd_CMD_BBP_REG_ACCESS:
1084         case HostCmd_CMD_RF_REG_ACCESS:
1085         case HostCmd_CMD_PMIC_REG_ACCESS:
1086         case HostCmd_CMD_CAU_REG_ACCESS:
1087         case HostCmd_CMD_802_11_EEPROM_ACCESS:
1088                 ret = mwifiex_cmd_reg_access(cmd_ptr, cmd_action, data_buf);
1089                 break;
1090         case HostCmd_CMD_SET_BSS_MODE:
1091                 cmd_ptr->command = cpu_to_le16(cmd_no);
1092                 if (priv->bss_mode == MWIFIEX_BSS_MODE_IBSS)
1093                         cmd_ptr->params.bss_mode.con_type =
1094                                 CONNECTION_TYPE_ADHOC;
1095                 else if (priv->bss_mode == MWIFIEX_BSS_MODE_INFRA)
1096                         cmd_ptr->params.bss_mode.con_type =
1097                                 CONNECTION_TYPE_INFRA;
1098                 cmd_ptr->size = cpu_to_le16(sizeof(struct
1099                                 host_cmd_ds_set_bss_mode) + S_DS_GEN);
1100                 ret = 0;
1101                 break;
1102         default:
1103                 dev_err(priv->adapter->dev,
1104                         "PREP_CMD: unknown cmd- %#x\n", cmd_no);
1105                 ret = -1;
1106                 break;
1107         }
1108         return ret;
1109 }
1110
1111 /*
1112  * This function issues commands to initialize firmware.
1113  *
1114  * This is called after firmware download to bring the card to
1115  * working state.
1116  *
1117  * The following commands are issued sequentially -
1118  *      - Function init (for first interface only)
1119  *      - Read MAC address (for first interface only)
1120  *      - Reconfigure Tx buffer size (for first interface only)
1121  *      - Enable auto deep sleep (for first interface only)
1122  *      - Get Tx rate
1123  *      - Get Tx power
1124  *      - Set IBSS coalescing status
1125  *      - Set AMSDU aggregation control
1126  *      - Set 11d control
1127  *      - Set MAC control (this must be the last command to initialize firmware)
1128  */
1129 int mwifiex_sta_init_cmd(struct mwifiex_private *priv, u8 first_sta)
1130 {
1131         int ret = 0;
1132         u16 enable = true;
1133         struct mwifiex_ds_11n_amsdu_aggr_ctrl amsdu_aggr_ctrl;
1134         struct mwifiex_ds_auto_ds auto_ds;
1135         enum state_11d_t state_11d;
1136
1137         if (first_sta) {
1138
1139                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_FUNC_INIT,
1140                                           HostCmd_ACT_GEN_SET, 0, NULL, NULL);
1141                 if (ret)
1142                         return -1;
1143                 /* Read MAC address from HW */
1144                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_GET_HW_SPEC,
1145                                           HostCmd_ACT_GEN_GET, 0, NULL, NULL);
1146                 if (ret)
1147                         return -1;
1148
1149                 /* Reconfigure tx buf size */
1150                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
1151                                           HostCmd_ACT_GEN_SET, 0, NULL,
1152                                           &priv->adapter->tx_buf_size);
1153                 if (ret)
1154                         return -1;
1155
1156                 /* Enable IEEE PS by default */
1157                 priv->adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1158                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
1159                                           EN_AUTO_PS, BITMAP_STA_PS, NULL,
1160                                           NULL);
1161                 if (ret)
1162                         return -1;
1163         }
1164
1165         /* get tx rate */
1166         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_TX_RATE_CFG,
1167                                   HostCmd_ACT_GEN_GET, 0, NULL, NULL);
1168         if (ret)
1169                 return -1;
1170         priv->data_rate = 0;
1171
1172         /* get tx power */
1173         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_TXPWR_CFG,
1174                                   HostCmd_ACT_GEN_GET, 0, NULL, NULL);
1175         if (ret)
1176                 return -1;
1177
1178         /* set ibss coalescing_status */
1179         ret = mwifiex_prepare_cmd(priv,
1180                                   HostCmd_CMD_802_11_IBSS_COALESCING_STATUS,
1181                                   HostCmd_ACT_GEN_SET, 0, NULL, &enable);
1182         if (ret)
1183                 return -1;
1184
1185         memset(&amsdu_aggr_ctrl, 0, sizeof(amsdu_aggr_ctrl));
1186         amsdu_aggr_ctrl.enable = true;
1187         /* Send request to firmware */
1188         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_AMSDU_AGGR_CTRL,
1189                                   HostCmd_ACT_GEN_SET, 0, NULL,
1190                                   (void *) &amsdu_aggr_ctrl);
1191         if (ret)
1192                 return -1;
1193         /* MAC Control must be the last command in init_fw */
1194         /* set MAC Control */
1195         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_MAC_CONTROL,
1196                                   HostCmd_ACT_GEN_SET, 0, NULL,
1197                                   &priv->curr_pkt_filter);
1198         if (ret)
1199                 return -1;
1200
1201         if (first_sta) {
1202                 /* Enable auto deep sleep */
1203                 auto_ds.auto_ds = DEEP_SLEEP_ON;
1204                 auto_ds.idle_time = DEEP_SLEEP_IDLE_TIME;
1205                 ret = mwifiex_prepare_cmd(priv,
1206                                 HostCmd_CMD_802_11_PS_MODE_ENH,
1207                                 EN_AUTO_PS, BITMAP_AUTO_DS, NULL,
1208                                 &auto_ds);
1209                 if (ret)
1210                         return -1;
1211         }
1212
1213         /* Send cmd to FW to enable/disable 11D function */
1214         state_11d = ENABLE_11D;
1215         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
1216                                   HostCmd_ACT_GEN_SET, DOT11D_I,
1217                                   NULL, &state_11d);
1218         if (ret)
1219                 dev_err(priv->adapter->dev, "11D: failed to enable 11D\n");
1220
1221         /* set last_init_cmd */
1222         priv->adapter->last_init_cmd = HostCmd_CMD_802_11_SNMP_MIB;
1223         ret = -EINPROGRESS;
1224
1225         return ret;
1226 }