Merge branches 'fixes' and 'ioremap' into for-linus
[linux-2.6-microblaze.git] / drivers / net / wireless / mwifiex / cmdevt.c
1 /*
2  * Marvell Wireless LAN device driver: commands and events
3  *
4  * Copyright (C) 2011-2014, 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 #include "11ac.h"
28
29 /*
30  * This function initializes a command node.
31  *
32  * The actual allocation of the node is not done by this function. It only
33  * initiates a node by filling it with default parameters. Similarly,
34  * allocation of the different buffers used (IOCTL buffer, data buffer) are
35  * not done by this function either.
36  */
37 static void
38 mwifiex_init_cmd_node(struct mwifiex_private *priv,
39                       struct cmd_ctrl_node *cmd_node,
40                       u32 cmd_oid, void *data_buf, bool sync)
41 {
42         cmd_node->priv = priv;
43         cmd_node->cmd_oid = cmd_oid;
44         if (sync) {
45                 cmd_node->wait_q_enabled = true;
46                 cmd_node->cmd_wait_q_woken = false;
47                 cmd_node->condition = &cmd_node->cmd_wait_q_woken;
48         }
49         cmd_node->data_buf = data_buf;
50         cmd_node->cmd_skb = cmd_node->skb;
51 }
52
53 /*
54  * This function returns a command node from the free queue depending upon
55  * availability.
56  */
57 static struct cmd_ctrl_node *
58 mwifiex_get_cmd_node(struct mwifiex_adapter *adapter)
59 {
60         struct cmd_ctrl_node *cmd_node;
61         unsigned long flags;
62
63         spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
64         if (list_empty(&adapter->cmd_free_q)) {
65                 mwifiex_dbg(adapter, ERROR,
66                             "GET_CMD_NODE: cmd node not available\n");
67                 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
68                 return NULL;
69         }
70         cmd_node = list_first_entry(&adapter->cmd_free_q,
71                                     struct cmd_ctrl_node, list);
72         list_del(&cmd_node->list);
73         spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
74
75         return cmd_node;
76 }
77
78 /*
79  * This function cleans up a command node.
80  *
81  * The function resets the fields including the buffer pointers.
82  * This function does not try to free the buffers. They must be
83  * freed before calling this function.
84  *
85  * This function will however call the receive completion callback
86  * in case a response buffer is still available before resetting
87  * the pointer.
88  */
89 static void
90 mwifiex_clean_cmd_node(struct mwifiex_adapter *adapter,
91                        struct cmd_ctrl_node *cmd_node)
92 {
93         cmd_node->cmd_oid = 0;
94         cmd_node->cmd_flag = 0;
95         cmd_node->data_buf = NULL;
96         cmd_node->wait_q_enabled = false;
97
98         if (cmd_node->cmd_skb)
99                 skb_trim(cmd_node->cmd_skb, 0);
100
101         if (cmd_node->resp_skb) {
102                 adapter->if_ops.cmdrsp_complete(adapter, cmd_node->resp_skb);
103                 cmd_node->resp_skb = NULL;
104         }
105 }
106
107 /*
108  * This function sends a host command to the firmware.
109  *
110  * The function copies the host command into the driver command
111  * buffer, which will be transferred to the firmware later by the
112  * main thread.
113  */
114 static int mwifiex_cmd_host_cmd(struct mwifiex_private *priv,
115                                 struct host_cmd_ds_command *cmd,
116                                 struct mwifiex_ds_misc_cmd *pcmd_ptr)
117 {
118         /* Copy the HOST command to command buffer */
119         memcpy(cmd, pcmd_ptr->cmd, pcmd_ptr->len);
120         mwifiex_dbg(priv->adapter, CMD,
121                     "cmd: host cmd size = %d\n", pcmd_ptr->len);
122         return 0;
123 }
124
125 /*
126  * This function downloads a command to the firmware.
127  *
128  * The function performs sanity tests, sets the command sequence
129  * number and size, converts the header fields to CPU format before
130  * sending. Afterwards, it logs the command ID and action for debugging
131  * and sets up the command timeout timer.
132  */
133 static int mwifiex_dnld_cmd_to_fw(struct mwifiex_private *priv,
134                                   struct cmd_ctrl_node *cmd_node)
135 {
136
137         struct mwifiex_adapter *adapter = priv->adapter;
138         int ret;
139         struct host_cmd_ds_command *host_cmd;
140         uint16_t cmd_code;
141         uint16_t cmd_size;
142         unsigned long flags;
143         __le32 tmp;
144
145         if (!adapter || !cmd_node)
146                 return -1;
147
148         host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
149
150         /* Sanity test */
151         if (host_cmd == NULL || host_cmd->size == 0) {
152                 mwifiex_dbg(adapter, ERROR,
153                             "DNLD_CMD: host_cmd is null\t"
154                             "or cmd size is 0, not sending\n");
155                 if (cmd_node->wait_q_enabled)
156                         adapter->cmd_wait_q.status = -1;
157                 mwifiex_recycle_cmd_node(adapter, cmd_node);
158                 return -1;
159         }
160
161         cmd_code = le16_to_cpu(host_cmd->command);
162         cmd_size = le16_to_cpu(host_cmd->size);
163
164         if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET &&
165             cmd_code != HostCmd_CMD_FUNC_SHUTDOWN &&
166             cmd_code != HostCmd_CMD_FUNC_INIT) {
167                 mwifiex_dbg(adapter, ERROR,
168                             "DNLD_CMD: FW in reset state, ignore cmd %#x\n",
169                         cmd_code);
170                 if (cmd_node->wait_q_enabled)
171                         mwifiex_complete_cmd(adapter, cmd_node);
172                 mwifiex_recycle_cmd_node(adapter, cmd_node);
173                 queue_work(adapter->workqueue, &adapter->main_work);
174                 return -1;
175         }
176
177         /* Set command sequence number */
178         adapter->seq_num++;
179         host_cmd->seq_num = cpu_to_le16(HostCmd_SET_SEQ_NO_BSS_INFO
180                                         (adapter->seq_num,
181                                          cmd_node->priv->bss_num,
182                                          cmd_node->priv->bss_type));
183
184         spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
185         adapter->curr_cmd = cmd_node;
186         spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
187
188         /* Adjust skb length */
189         if (cmd_node->cmd_skb->len > cmd_size)
190                 /*
191                  * cmd_size is less than sizeof(struct host_cmd_ds_command).
192                  * Trim off the unused portion.
193                  */
194                 skb_trim(cmd_node->cmd_skb, cmd_size);
195         else if (cmd_node->cmd_skb->len < cmd_size)
196                 /*
197                  * cmd_size is larger than sizeof(struct host_cmd_ds_command)
198                  * because we have appended custom IE TLV. Increase skb length
199                  * accordingly.
200                  */
201                 skb_put(cmd_node->cmd_skb, cmd_size - cmd_node->cmd_skb->len);
202
203         mwifiex_dbg(adapter, CMD,
204                     "cmd: DNLD_CMD: %#x, act %#x, len %d, seqno %#x\n",
205                     cmd_code,
206                     le16_to_cpu(*(__le16 *)((u8 *)host_cmd + S_DS_GEN)),
207                     cmd_size, le16_to_cpu(host_cmd->seq_num));
208         mwifiex_dbg_dump(adapter, CMD_D, "cmd buffer:", host_cmd, cmd_size);
209
210         if (adapter->iface_type == MWIFIEX_USB) {
211                 tmp = cpu_to_le32(MWIFIEX_USB_TYPE_CMD);
212                 skb_push(cmd_node->cmd_skb, MWIFIEX_TYPE_LEN);
213                 memcpy(cmd_node->cmd_skb->data, &tmp, MWIFIEX_TYPE_LEN);
214                 adapter->cmd_sent = true;
215                 ret = adapter->if_ops.host_to_card(adapter,
216                                                    MWIFIEX_USB_EP_CMD_EVENT,
217                                                    cmd_node->cmd_skb, NULL);
218                 skb_pull(cmd_node->cmd_skb, MWIFIEX_TYPE_LEN);
219                 if (ret == -EBUSY)
220                         cmd_node->cmd_skb = NULL;
221         } else {
222                 skb_push(cmd_node->cmd_skb, INTF_HEADER_LEN);
223                 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
224                                                    cmd_node->cmd_skb, NULL);
225                 skb_pull(cmd_node->cmd_skb, INTF_HEADER_LEN);
226         }
227
228         if (ret == -1) {
229                 mwifiex_dbg(adapter, ERROR,
230                             "DNLD_CMD: host to card failed\n");
231                 if (adapter->iface_type == MWIFIEX_USB)
232                         adapter->cmd_sent = false;
233                 if (cmd_node->wait_q_enabled)
234                         adapter->cmd_wait_q.status = -1;
235                 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
236
237                 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
238                 adapter->curr_cmd = NULL;
239                 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
240
241                 adapter->dbg.num_cmd_host_to_card_failure++;
242                 return -1;
243         }
244
245         /* Save the last command id and action to debug log */
246         adapter->dbg.last_cmd_index =
247                         (adapter->dbg.last_cmd_index + 1) % DBG_CMD_NUM;
248         adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index] = cmd_code;
249         adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index] =
250                         le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN));
251
252         /* Clear BSS_NO_BITS from HostCmd */
253         cmd_code &= HostCmd_CMD_ID_MASK;
254
255         /* Setup the timer after transmit command */
256         mod_timer(&adapter->cmd_timer,
257                   jiffies + msecs_to_jiffies(MWIFIEX_TIMER_10S));
258
259         return 0;
260 }
261
262 /*
263  * This function downloads a sleep confirm command to the firmware.
264  *
265  * The function performs sanity tests, sets the command sequence
266  * number and size, converts the header fields to CPU format before
267  * sending.
268  *
269  * No responses are needed for sleep confirm command.
270  */
271 static int mwifiex_dnld_sleep_confirm_cmd(struct mwifiex_adapter *adapter)
272 {
273         int ret;
274         struct mwifiex_private *priv;
275         struct mwifiex_opt_sleep_confirm *sleep_cfm_buf =
276                                 (struct mwifiex_opt_sleep_confirm *)
277                                                 adapter->sleep_cfm->data;
278         struct sk_buff *sleep_cfm_tmp;
279         __le32 tmp;
280
281         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
282
283         adapter->seq_num++;
284         sleep_cfm_buf->seq_num =
285                 cpu_to_le16((HostCmd_SET_SEQ_NO_BSS_INFO
286                                         (adapter->seq_num, priv->bss_num,
287                                          priv->bss_type)));
288
289         mwifiex_dbg(adapter, CMD,
290                     "cmd: DNLD_CMD: %#x, act %#x, len %d, seqno %#x\n",
291                 le16_to_cpu(sleep_cfm_buf->command),
292                 le16_to_cpu(sleep_cfm_buf->action),
293                 le16_to_cpu(sleep_cfm_buf->size),
294                 le16_to_cpu(sleep_cfm_buf->seq_num));
295         mwifiex_dbg_dump(adapter, CMD_D, "SLEEP_CFM buffer: ", sleep_cfm_buf,
296                          le16_to_cpu(sleep_cfm_buf->size));
297
298         if (adapter->iface_type == MWIFIEX_USB) {
299                 sleep_cfm_tmp =
300                         dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
301                                       + MWIFIEX_TYPE_LEN);
302                 skb_put(sleep_cfm_tmp, sizeof(struct mwifiex_opt_sleep_confirm)
303                         + MWIFIEX_TYPE_LEN);
304                 tmp = cpu_to_le32(MWIFIEX_USB_TYPE_CMD);
305                 memcpy(sleep_cfm_tmp->data, &tmp, MWIFIEX_TYPE_LEN);
306                 memcpy(sleep_cfm_tmp->data + MWIFIEX_TYPE_LEN,
307                        adapter->sleep_cfm->data,
308                        sizeof(struct mwifiex_opt_sleep_confirm));
309                 ret = adapter->if_ops.host_to_card(adapter,
310                                                    MWIFIEX_USB_EP_CMD_EVENT,
311                                                    sleep_cfm_tmp, NULL);
312                 if (ret != -EBUSY)
313                         dev_kfree_skb_any(sleep_cfm_tmp);
314         } else {
315                 skb_push(adapter->sleep_cfm, INTF_HEADER_LEN);
316                 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
317                                                    adapter->sleep_cfm, NULL);
318                 skb_pull(adapter->sleep_cfm, INTF_HEADER_LEN);
319         }
320
321         if (ret == -1) {
322                 mwifiex_dbg(adapter, ERROR, "SLEEP_CFM: failed\n");
323                 adapter->dbg.num_cmd_sleep_cfm_host_to_card_failure++;
324                 return -1;
325         }
326
327         if (!le16_to_cpu(sleep_cfm_buf->resp_ctrl))
328                 /* Response is not needed for sleep confirm command */
329                 adapter->ps_state = PS_STATE_SLEEP;
330         else
331                 adapter->ps_state = PS_STATE_SLEEP_CFM;
332
333         if (!le16_to_cpu(sleep_cfm_buf->resp_ctrl) &&
334             (adapter->is_hs_configured &&
335              !adapter->sleep_period.period)) {
336                 adapter->pm_wakeup_card_req = true;
337                 mwifiex_hs_activated_event(mwifiex_get_priv
338                                 (adapter, MWIFIEX_BSS_ROLE_ANY), true);
339         }
340
341         return ret;
342 }
343
344 /*
345  * This function allocates the command buffers and links them to
346  * the command free queue.
347  *
348  * The driver uses a pre allocated number of command buffers, which
349  * are created at driver initializations and freed at driver cleanup.
350  * Every command needs to obtain a command buffer from this pool before
351  * it can be issued. The command free queue lists the command buffers
352  * currently free to use, while the command pending queue lists the
353  * command buffers already in use and awaiting handling. Command buffers
354  * are returned to the free queue after use.
355  */
356 int mwifiex_alloc_cmd_buffer(struct mwifiex_adapter *adapter)
357 {
358         struct cmd_ctrl_node *cmd_array;
359         u32 i;
360
361         /* Allocate and initialize struct cmd_ctrl_node */
362         cmd_array = kcalloc(MWIFIEX_NUM_OF_CMD_BUFFER,
363                             sizeof(struct cmd_ctrl_node), GFP_KERNEL);
364         if (!cmd_array)
365                 return -ENOMEM;
366
367         adapter->cmd_pool = cmd_array;
368
369         /* Allocate and initialize command buffers */
370         for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
371                 cmd_array[i].skb = dev_alloc_skb(MWIFIEX_SIZE_OF_CMD_BUFFER);
372                 if (!cmd_array[i].skb) {
373                         mwifiex_dbg(adapter, ERROR,
374                                     "unable to allocate command buffer\n");
375                         return -ENOMEM;
376                 }
377         }
378
379         for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++)
380                 mwifiex_insert_cmd_to_free_q(adapter, &cmd_array[i]);
381
382         return 0;
383 }
384
385 /*
386  * This function frees the command buffers.
387  *
388  * The function calls the completion callback for all the command
389  * buffers that still have response buffers associated with them.
390  */
391 int mwifiex_free_cmd_buffer(struct mwifiex_adapter *adapter)
392 {
393         struct cmd_ctrl_node *cmd_array;
394         u32 i;
395
396         /* Need to check if cmd pool is allocated or not */
397         if (!adapter->cmd_pool) {
398                 mwifiex_dbg(adapter, FATAL,
399                             "info: FREE_CMD_BUF: cmd_pool is null\n");
400                 return 0;
401         }
402
403         cmd_array = adapter->cmd_pool;
404
405         /* Release shared memory buffers */
406         for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
407                 if (cmd_array[i].skb) {
408                         mwifiex_dbg(adapter, CMD,
409                                     "cmd: free cmd buffer %d\n", i);
410                         dev_kfree_skb_any(cmd_array[i].skb);
411                 }
412                 if (!cmd_array[i].resp_skb)
413                         continue;
414
415                 if (adapter->iface_type == MWIFIEX_USB)
416                         adapter->if_ops.cmdrsp_complete(adapter,
417                                                         cmd_array[i].resp_skb);
418                 else
419                         dev_kfree_skb_any(cmd_array[i].resp_skb);
420         }
421         /* Release struct cmd_ctrl_node */
422         if (adapter->cmd_pool) {
423                 mwifiex_dbg(adapter, CMD,
424                             "cmd: free cmd pool\n");
425                 kfree(adapter->cmd_pool);
426                 adapter->cmd_pool = NULL;
427         }
428
429         return 0;
430 }
431
432 /*
433  * This function handles events generated by firmware.
434  *
435  * Event body of events received from firmware are not used (though they are
436  * saved), only the event ID is used. Some events are re-invoked by
437  * the driver, with a new event body.
438  *
439  * After processing, the function calls the completion callback
440  * for cleanup.
441  */
442 int mwifiex_process_event(struct mwifiex_adapter *adapter)
443 {
444         int ret;
445         struct mwifiex_private *priv =
446                 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
447         struct sk_buff *skb = adapter->event_skb;
448         u32 eventcause = adapter->event_cause;
449         struct mwifiex_rxinfo *rx_info;
450
451         /* Save the last event to debug log */
452         adapter->dbg.last_event_index =
453                         (adapter->dbg.last_event_index + 1) % DBG_CMD_NUM;
454         adapter->dbg.last_event[adapter->dbg.last_event_index] =
455                                                         (u16) eventcause;
456
457         /* Get BSS number and corresponding priv */
458         priv = mwifiex_get_priv_by_id(adapter, EVENT_GET_BSS_NUM(eventcause),
459                                       EVENT_GET_BSS_TYPE(eventcause));
460         if (!priv)
461                 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
462
463         /* Clear BSS_NO_BITS from event */
464         eventcause &= EVENT_ID_MASK;
465         adapter->event_cause = eventcause;
466
467         if (skb) {
468                 rx_info = MWIFIEX_SKB_RXCB(skb);
469                 memset(rx_info, 0, sizeof(*rx_info));
470                 rx_info->bss_num = priv->bss_num;
471                 rx_info->bss_type = priv->bss_type;
472                 mwifiex_dbg_dump(adapter, EVT_D, "Event Buf:",
473                                  skb->data, skb->len);
474         }
475
476         mwifiex_dbg(adapter, EVENT, "EVENT: cause: %#x\n", eventcause);
477
478         if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
479                 ret = mwifiex_process_uap_event(priv);
480         else
481                 ret = mwifiex_process_sta_event(priv);
482
483         adapter->event_cause = 0;
484         adapter->event_skb = NULL;
485         adapter->if_ops.event_complete(adapter, skb);
486
487         return ret;
488 }
489
490 /*
491  * This function prepares a command and send it to the firmware.
492  *
493  * Preparation includes -
494  *      - Sanity tests to make sure the card is still present or the FW
495  *        is not reset
496  *      - Getting a new command node from the command free queue
497  *      - Initializing the command node for default parameters
498  *      - Fill up the non-default parameters and buffer pointers
499  *      - Add the command to pending queue
500  */
501 int mwifiex_send_cmd(struct mwifiex_private *priv, u16 cmd_no,
502                      u16 cmd_action, u32 cmd_oid, void *data_buf, bool sync)
503 {
504         int ret;
505         struct mwifiex_adapter *adapter = priv->adapter;
506         struct cmd_ctrl_node *cmd_node;
507         struct host_cmd_ds_command *cmd_ptr;
508
509         if (!adapter) {
510                 pr_err("PREP_CMD: adapter is NULL\n");
511                 return -1;
512         }
513
514         if (adapter->is_suspended) {
515                 mwifiex_dbg(adapter, ERROR,
516                             "PREP_CMD: device in suspended state\n");
517                 return -1;
518         }
519
520         if (adapter->hs_enabling && cmd_no != HostCmd_CMD_802_11_HS_CFG_ENH) {
521                 mwifiex_dbg(adapter, ERROR,
522                             "PREP_CMD: host entering sleep state\n");
523                 return -1;
524         }
525
526         if (adapter->surprise_removed) {
527                 mwifiex_dbg(adapter, ERROR,
528                             "PREP_CMD: card is removed\n");
529                 return -1;
530         }
531
532         if (adapter->is_cmd_timedout) {
533                 mwifiex_dbg(adapter, ERROR,
534                             "PREP_CMD: FW is in bad state\n");
535                 return -1;
536         }
537
538         if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET) {
539                 if (cmd_no != HostCmd_CMD_FUNC_INIT) {
540                         mwifiex_dbg(adapter, ERROR,
541                                     "PREP_CMD: FW in reset state\n");
542                         return -1;
543                 }
544         }
545
546         /* Get a new command node */
547         cmd_node = mwifiex_get_cmd_node(adapter);
548
549         if (!cmd_node) {
550                 mwifiex_dbg(adapter, ERROR,
551                             "PREP_CMD: no free cmd node\n");
552                 return -1;
553         }
554
555         /* Initialize the command node */
556         mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf, sync);
557
558         if (!cmd_node->cmd_skb) {
559                 mwifiex_dbg(adapter, ERROR,
560                             "PREP_CMD: no free cmd buf\n");
561                 return -1;
562         }
563
564         memset(skb_put(cmd_node->cmd_skb, sizeof(struct host_cmd_ds_command)),
565                0, sizeof(struct host_cmd_ds_command));
566
567         cmd_ptr = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
568         cmd_ptr->command = cpu_to_le16(cmd_no);
569         cmd_ptr->result = 0;
570
571         /* Prepare command */
572         if (cmd_no) {
573                 switch (cmd_no) {
574                 case HostCmd_CMD_UAP_SYS_CONFIG:
575                 case HostCmd_CMD_UAP_BSS_START:
576                 case HostCmd_CMD_UAP_BSS_STOP:
577                 case HostCmd_CMD_UAP_STA_DEAUTH:
578                 case HOST_CMD_APCMD_SYS_RESET:
579                 case HOST_CMD_APCMD_STA_LIST:
580                         ret = mwifiex_uap_prepare_cmd(priv, cmd_no, cmd_action,
581                                                       cmd_oid, data_buf,
582                                                       cmd_ptr);
583                         break;
584                 default:
585                         ret = mwifiex_sta_prepare_cmd(priv, cmd_no, cmd_action,
586                                                       cmd_oid, data_buf,
587                                                       cmd_ptr);
588                         break;
589                 }
590         } else {
591                 ret = mwifiex_cmd_host_cmd(priv, cmd_ptr, data_buf);
592                 cmd_node->cmd_flag |= CMD_F_HOSTCMD;
593         }
594
595         /* Return error, since the command preparation failed */
596         if (ret) {
597                 mwifiex_dbg(adapter, ERROR,
598                             "PREP_CMD: cmd %#x preparation failed\n",
599                         cmd_no);
600                 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
601                 return -1;
602         }
603
604         /* Send command */
605         if (cmd_no == HostCmd_CMD_802_11_SCAN ||
606             cmd_no == HostCmd_CMD_802_11_SCAN_EXT) {
607                 mwifiex_queue_scan_cmd(priv, cmd_node);
608         } else {
609                 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
610                 queue_work(adapter->workqueue, &adapter->main_work);
611                 if (cmd_node->wait_q_enabled)
612                         ret = mwifiex_wait_queue_complete(adapter, cmd_node);
613         }
614
615         return ret;
616 }
617
618 /*
619  * This function returns a command to the command free queue.
620  *
621  * The function also calls the completion callback if required, before
622  * cleaning the command node and re-inserting it into the free queue.
623  */
624 void
625 mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
626                              struct cmd_ctrl_node *cmd_node)
627 {
628         unsigned long flags;
629
630         if (!cmd_node)
631                 return;
632
633         if (cmd_node->wait_q_enabled)
634                 mwifiex_complete_cmd(adapter, cmd_node);
635         /* Clean the node */
636         mwifiex_clean_cmd_node(adapter, cmd_node);
637
638         /* Insert node into cmd_free_q */
639         spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
640         list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
641         spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
642 }
643
644 /* This function reuses a command node. */
645 void mwifiex_recycle_cmd_node(struct mwifiex_adapter *adapter,
646                               struct cmd_ctrl_node *cmd_node)
647 {
648         struct host_cmd_ds_command *host_cmd = (void *)cmd_node->cmd_skb->data;
649
650         mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
651
652         atomic_dec(&adapter->cmd_pending);
653         mwifiex_dbg(adapter, CMD,
654                     "cmd: FREE_CMD: cmd=%#x, cmd_pending=%d\n",
655                 le16_to_cpu(host_cmd->command),
656                 atomic_read(&adapter->cmd_pending));
657 }
658
659 /*
660  * This function queues a command to the command pending queue.
661  *
662  * This in effect adds the command to the command list to be executed.
663  * Exit PS command is handled specially, by placing it always to the
664  * front of the command queue.
665  */
666 void
667 mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
668                                 struct cmd_ctrl_node *cmd_node, u32 add_tail)
669 {
670         struct host_cmd_ds_command *host_cmd = NULL;
671         u16 command;
672         unsigned long flags;
673
674         host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
675         if (!host_cmd) {
676                 mwifiex_dbg(adapter, ERROR, "QUEUE_CMD: host_cmd is NULL\n");
677                 return;
678         }
679
680         command = le16_to_cpu(host_cmd->command);
681
682         /* Exit_PS command needs to be queued in the header always. */
683         if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
684                 struct host_cmd_ds_802_11_ps_mode_enh *pm =
685                                                 &host_cmd->params.psmode_enh;
686                 if ((le16_to_cpu(pm->action) == DIS_PS) ||
687                     (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
688                         if (adapter->ps_state != PS_STATE_AWAKE)
689                                 add_tail = false;
690                 }
691         }
692
693         spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
694         if (add_tail)
695                 list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
696         else
697                 list_add(&cmd_node->list, &adapter->cmd_pending_q);
698         spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
699
700         atomic_inc(&adapter->cmd_pending);
701         mwifiex_dbg(adapter, CMD,
702                     "cmd: QUEUE_CMD: cmd=%#x, cmd_pending=%d\n",
703                 command, atomic_read(&adapter->cmd_pending));
704 }
705
706 /*
707  * This function executes the next command in command pending queue.
708  *
709  * This function will fail if a command is already in processing stage,
710  * otherwise it will dequeue the first command from the command pending
711  * queue and send to the firmware.
712  *
713  * If the device is currently in host sleep mode, any commands, except the
714  * host sleep configuration command will de-activate the host sleep. For PS
715  * mode, the function will put the firmware back to sleep if applicable.
716  */
717 int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
718 {
719         struct mwifiex_private *priv;
720         struct cmd_ctrl_node *cmd_node;
721         int ret = 0;
722         struct host_cmd_ds_command *host_cmd;
723         unsigned long cmd_flags;
724         unsigned long cmd_pending_q_flags;
725
726         /* Check if already in processing */
727         if (adapter->curr_cmd) {
728                 mwifiex_dbg(adapter, FATAL,
729                             "EXEC_NEXT_CMD: cmd in processing\n");
730                 return -1;
731         }
732
733         spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
734         /* Check if any command is pending */
735         spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
736         if (list_empty(&adapter->cmd_pending_q)) {
737                 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
738                                        cmd_pending_q_flags);
739                 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
740                 return 0;
741         }
742         cmd_node = list_first_entry(&adapter->cmd_pending_q,
743                                     struct cmd_ctrl_node, list);
744         spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
745                                cmd_pending_q_flags);
746
747         host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
748         priv = cmd_node->priv;
749
750         if (adapter->ps_state != PS_STATE_AWAKE) {
751                 mwifiex_dbg(adapter, ERROR,
752                             "%s: cannot send cmd in sleep state,\t"
753                             "this should not happen\n", __func__);
754                 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
755                 return ret;
756         }
757
758         spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
759         list_del(&cmd_node->list);
760         spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
761                                cmd_pending_q_flags);
762
763         spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
764         ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
765         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
766         /* Any command sent to the firmware when host is in sleep
767          * mode should de-configure host sleep. We should skip the
768          * host sleep configuration command itself though
769          */
770         if (priv && (host_cmd->command !=
771              cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
772                 if (adapter->hs_activated) {
773                         adapter->is_hs_configured = false;
774                         mwifiex_hs_activated_event(priv, false);
775                 }
776         }
777
778         return ret;
779 }
780
781 /*
782  * This function handles the command response.
783  *
784  * After processing, the function cleans the command node and puts
785  * it back to the command free queue.
786  */
787 int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
788 {
789         struct host_cmd_ds_command *resp;
790         struct mwifiex_private *priv =
791                 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
792         int ret = 0;
793         uint16_t orig_cmdresp_no;
794         uint16_t cmdresp_no;
795         uint16_t cmdresp_result;
796         unsigned long flags;
797
798         /* Now we got response from FW, cancel the command timer */
799         del_timer_sync(&adapter->cmd_timer);
800
801         if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
802                 resp = (struct host_cmd_ds_command *) adapter->upld_buf;
803                 mwifiex_dbg(adapter, ERROR,
804                             "CMD_RESP: NULL curr_cmd, %#x\n",
805                             le16_to_cpu(resp->command));
806                 return -1;
807         }
808
809         adapter->is_cmd_timedout = 0;
810
811         resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
812         if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
813                 mwifiex_dbg(adapter, ERROR,
814                             "CMD_RESP: %#x been canceled\n",
815                             le16_to_cpu(resp->command));
816                 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
817                 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
818                 adapter->curr_cmd = NULL;
819                 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
820                 return -1;
821         }
822
823         if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
824                 /* Copy original response back to response buffer */
825                 struct mwifiex_ds_misc_cmd *hostcmd;
826                 uint16_t size = le16_to_cpu(resp->size);
827                 mwifiex_dbg(adapter, INFO,
828                             "info: host cmd resp size = %d\n", size);
829                 size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
830                 if (adapter->curr_cmd->data_buf) {
831                         hostcmd = adapter->curr_cmd->data_buf;
832                         hostcmd->len = size;
833                         memcpy(hostcmd->cmd, resp, size);
834                 }
835         }
836         orig_cmdresp_no = le16_to_cpu(resp->command);
837
838         /* Get BSS number and corresponding priv */
839         priv = mwifiex_get_priv_by_id(adapter,
840                              HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
841                              HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
842         if (!priv)
843                 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
844         /* Clear RET_BIT from HostCmd */
845         resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
846
847         cmdresp_no = le16_to_cpu(resp->command);
848         cmdresp_result = le16_to_cpu(resp->result);
849
850         /* Save the last command response to debug log */
851         adapter->dbg.last_cmd_resp_index =
852                         (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
853         adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
854                                                                 orig_cmdresp_no;
855
856         mwifiex_dbg(adapter, CMD,
857                     "cmd: CMD_RESP: 0x%x, result %d, len %d, seqno 0x%x\n",
858                     orig_cmdresp_no, cmdresp_result,
859                     le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
860         mwifiex_dbg_dump(adapter, CMD_D, "CMD_RESP buffer:", resp,
861                          le16_to_cpu(resp->size));
862
863         if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
864                 mwifiex_dbg(adapter, ERROR, "CMD_RESP: invalid cmd resp\n");
865                 if (adapter->curr_cmd->wait_q_enabled)
866                         adapter->cmd_wait_q.status = -1;
867
868                 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
869                 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
870                 adapter->curr_cmd = NULL;
871                 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
872                 return -1;
873         }
874
875         if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
876                 adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
877                 if ((cmdresp_result == HostCmd_RESULT_OK) &&
878                     (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
879                         ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
880         } else {
881                 /* handle response */
882                 ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
883         }
884
885         /* Check init command response */
886         if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
887                 if (ret) {
888                         mwifiex_dbg(adapter, ERROR,
889                                     "%s: cmd %#x failed during\t"
890                                     "initialization\n", __func__, cmdresp_no);
891                         mwifiex_init_fw_complete(adapter);
892                         return -1;
893                 } else if (adapter->last_init_cmd == cmdresp_no)
894                         adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
895         }
896
897         if (adapter->curr_cmd) {
898                 if (adapter->curr_cmd->wait_q_enabled)
899                         adapter->cmd_wait_q.status = ret;
900
901                 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
902
903                 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
904                 adapter->curr_cmd = NULL;
905                 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
906         }
907
908         return ret;
909 }
910
911 /*
912  * This function handles the timeout of command sending.
913  *
914  * It will re-send the same command again.
915  */
916 void
917 mwifiex_cmd_timeout_func(unsigned long function_context)
918 {
919         struct mwifiex_adapter *adapter =
920                 (struct mwifiex_adapter *) function_context;
921         struct cmd_ctrl_node *cmd_node;
922
923         adapter->is_cmd_timedout = 1;
924         if (!adapter->curr_cmd) {
925                 mwifiex_dbg(adapter, ERROR,
926                             "cmd: empty curr_cmd\n");
927                 return;
928         }
929         cmd_node = adapter->curr_cmd;
930         if (cmd_node) {
931                 adapter->dbg.timeout_cmd_id =
932                         adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
933                 adapter->dbg.timeout_cmd_act =
934                         adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
935                 mwifiex_dbg(adapter, MSG,
936                             "%s: Timeout cmd id = %#x, act = %#x\n", __func__,
937                             adapter->dbg.timeout_cmd_id,
938                             adapter->dbg.timeout_cmd_act);
939
940                 mwifiex_dbg(adapter, MSG,
941                             "num_data_h2c_failure = %d\n",
942                             adapter->dbg.num_tx_host_to_card_failure);
943                 mwifiex_dbg(adapter, MSG,
944                             "num_cmd_h2c_failure = %d\n",
945                             adapter->dbg.num_cmd_host_to_card_failure);
946
947                 mwifiex_dbg(adapter, MSG,
948                             "is_cmd_timedout = %d\n",
949                             adapter->is_cmd_timedout);
950                 mwifiex_dbg(adapter, MSG,
951                             "num_tx_timeout = %d\n",
952                             adapter->dbg.num_tx_timeout);
953
954                 mwifiex_dbg(adapter, MSG,
955                             "last_cmd_index = %d\n",
956                             adapter->dbg.last_cmd_index);
957                 mwifiex_dbg(adapter, MSG,
958                             "last_cmd_id: %*ph\n",
959                             (int)sizeof(adapter->dbg.last_cmd_id),
960                             adapter->dbg.last_cmd_id);
961                 mwifiex_dbg(adapter, MSG,
962                             "last_cmd_act: %*ph\n",
963                             (int)sizeof(adapter->dbg.last_cmd_act),
964                             adapter->dbg.last_cmd_act);
965
966                 mwifiex_dbg(adapter, MSG,
967                             "last_cmd_resp_index = %d\n",
968                             adapter->dbg.last_cmd_resp_index);
969                 mwifiex_dbg(adapter, MSG,
970                             "last_cmd_resp_id: %*ph\n",
971                             (int)sizeof(adapter->dbg.last_cmd_resp_id),
972                             adapter->dbg.last_cmd_resp_id);
973
974                 mwifiex_dbg(adapter, MSG,
975                             "last_event_index = %d\n",
976                             adapter->dbg.last_event_index);
977                 mwifiex_dbg(adapter, MSG,
978                             "last_event: %*ph\n",
979                             (int)sizeof(adapter->dbg.last_event),
980                             adapter->dbg.last_event);
981
982                 mwifiex_dbg(adapter, MSG,
983                             "data_sent=%d cmd_sent=%d\n",
984                             adapter->data_sent, adapter->cmd_sent);
985
986                 mwifiex_dbg(adapter, MSG,
987                             "ps_mode=%d ps_state=%d\n",
988                             adapter->ps_mode, adapter->ps_state);
989
990                 if (cmd_node->wait_q_enabled) {
991                         adapter->cmd_wait_q.status = -ETIMEDOUT;
992                         wake_up_interruptible(&adapter->cmd_wait_q.wait);
993                         mwifiex_cancel_pending_ioctl(adapter);
994                 }
995         }
996         if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
997                 mwifiex_init_fw_complete(adapter);
998
999         if (adapter->if_ops.device_dump)
1000                 adapter->if_ops.device_dump(adapter);
1001
1002         if (adapter->if_ops.card_reset)
1003                 adapter->if_ops.card_reset(adapter);
1004 }
1005
1006 /*
1007  * This function cancels all the pending commands.
1008  *
1009  * The current command, all commands in command pending queue and all scan
1010  * commands in scan pending queue are cancelled. All the completion callbacks
1011  * are called with failure status to ensure cleanup.
1012  */
1013 void
1014 mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
1015 {
1016         struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
1017         unsigned long flags, cmd_flags;
1018         struct mwifiex_private *priv;
1019         int i;
1020
1021         spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1022         /* Cancel current cmd */
1023         if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
1024                 adapter->curr_cmd->wait_q_enabled = false;
1025                 adapter->cmd_wait_q.status = -1;
1026                 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
1027         }
1028         /* Cancel all pending command */
1029         spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
1030         list_for_each_entry_safe(cmd_node, tmp_node,
1031                                  &adapter->cmd_pending_q, list) {
1032                 list_del(&cmd_node->list);
1033                 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1034
1035                 if (cmd_node->wait_q_enabled) {
1036                         adapter->cmd_wait_q.status = -1;
1037                         mwifiex_complete_cmd(adapter, cmd_node);
1038                         cmd_node->wait_q_enabled = false;
1039                 }
1040                 mwifiex_recycle_cmd_node(adapter, cmd_node);
1041                 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
1042         }
1043         spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1044         spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1045
1046         /* Cancel all pending scan command */
1047         spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
1048         list_for_each_entry_safe(cmd_node, tmp_node,
1049                                  &adapter->scan_pending_q, list) {
1050                 list_del(&cmd_node->list);
1051
1052                 cmd_node->wait_q_enabled = false;
1053                 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
1054         }
1055         spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
1056
1057         if (adapter->scan_processing) {
1058                 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1059                 adapter->scan_processing = false;
1060                 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1061                 for (i = 0; i < adapter->priv_num; i++) {
1062                         priv = adapter->priv[i];
1063                         if (!priv)
1064                                 continue;
1065                         if (priv->scan_request) {
1066                                 mwifiex_dbg(adapter, WARN, "info: aborting scan\n");
1067                                 cfg80211_scan_done(priv->scan_request, 1);
1068                                 priv->scan_request = NULL;
1069                         }
1070                 }
1071         }
1072 }
1073
1074 /*
1075  * This function cancels all pending commands that matches with
1076  * the given IOCTL request.
1077  *
1078  * Both the current command buffer and the pending command queue are
1079  * searched for matching IOCTL request. The completion callback of
1080  * the matched command is called with failure status to ensure cleanup.
1081  * In case of scan commands, all pending commands in scan pending queue
1082  * are cancelled.
1083  */
1084 void
1085 mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
1086 {
1087         struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
1088         unsigned long cmd_flags;
1089         unsigned long scan_pending_q_flags;
1090         struct mwifiex_private *priv;
1091         int i;
1092
1093         if ((adapter->curr_cmd) &&
1094             (adapter->curr_cmd->wait_q_enabled)) {
1095                 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1096                 cmd_node = adapter->curr_cmd;
1097                 cmd_node->wait_q_enabled = false;
1098                 cmd_node->cmd_flag |= CMD_F_CANCELED;
1099                 mwifiex_recycle_cmd_node(adapter, cmd_node);
1100                 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
1101                 adapter->curr_cmd = NULL;
1102                 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1103         }
1104
1105         /* Cancel all pending scan command */
1106         spin_lock_irqsave(&adapter->scan_pending_q_lock,
1107                           scan_pending_q_flags);
1108         list_for_each_entry_safe(cmd_node, tmp_node,
1109                                  &adapter->scan_pending_q, list) {
1110                 list_del(&cmd_node->list);
1111                 cmd_node->wait_q_enabled = false;
1112                 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
1113         }
1114         spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1115                                scan_pending_q_flags);
1116
1117         if (adapter->scan_processing) {
1118                 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1119                 adapter->scan_processing = false;
1120                 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1121                 for (i = 0; i < adapter->priv_num; i++) {
1122                         priv = adapter->priv[i];
1123                         if (!priv)
1124                                 continue;
1125                         if (priv->scan_request) {
1126                                 mwifiex_dbg(adapter, WARN, "info: aborting scan\n");
1127                                 cfg80211_scan_done(priv->scan_request, 1);
1128                                 priv->scan_request = NULL;
1129                         }
1130                 }
1131         }
1132         adapter->cmd_wait_q.status = -1;
1133 }
1134
1135 /*
1136  * This function sends the sleep confirm command to firmware, if
1137  * possible.
1138  *
1139  * The sleep confirm command cannot be issued if command response,
1140  * data response or event response is awaiting handling, or if we
1141  * are in the middle of sending a command, or expecting a command
1142  * response.
1143  */
1144 void
1145 mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
1146 {
1147         if (!adapter->cmd_sent &&
1148             !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
1149                 mwifiex_dnld_sleep_confirm_cmd(adapter);
1150         else
1151                 mwifiex_dbg(adapter, CMD,
1152                             "cmd: Delay Sleep Confirm (%s%s%s)\n",
1153                             (adapter->cmd_sent) ? "D" : "",
1154                             (adapter->curr_cmd) ? "C" : "",
1155                             (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
1156 }
1157
1158 /*
1159  * This function sends a Host Sleep activated event to applications.
1160  *
1161  * This event is generated by the driver, with a blank event body.
1162  */
1163 void
1164 mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
1165 {
1166         if (activated) {
1167                 if (priv->adapter->is_hs_configured) {
1168                         priv->adapter->hs_activated = true;
1169                         mwifiex_update_rxreor_flags(priv->adapter,
1170                                                     RXREOR_FORCE_NO_DROP);
1171                         mwifiex_dbg(priv->adapter, EVENT,
1172                                     "event: hs_activated\n");
1173                         priv->adapter->hs_activate_wait_q_woken = true;
1174                         wake_up_interruptible(
1175                                 &priv->adapter->hs_activate_wait_q);
1176                 } else {
1177                         mwifiex_dbg(priv->adapter, EVENT,
1178                                     "event: HS not configured\n");
1179                 }
1180         } else {
1181                 mwifiex_dbg(priv->adapter, EVENT,
1182                             "event: hs_deactivated\n");
1183                 priv->adapter->hs_activated = false;
1184         }
1185 }
1186
1187 /*
1188  * This function handles the command response of a Host Sleep configuration
1189  * command.
1190  *
1191  * Handling includes changing the header fields into CPU format
1192  * and setting the current host sleep activation status in driver.
1193  *
1194  * In case host sleep status change, the function generates an event to
1195  * notify the applications.
1196  */
1197 int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
1198                               struct host_cmd_ds_command *resp)
1199 {
1200         struct mwifiex_adapter *adapter = priv->adapter;
1201         struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
1202                 &resp->params.opt_hs_cfg;
1203         uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
1204
1205         if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE) &&
1206             adapter->iface_type != MWIFIEX_USB) {
1207                 mwifiex_hs_activated_event(priv, true);
1208                 return 0;
1209         } else {
1210                 mwifiex_dbg(adapter, CMD,
1211                             "cmd: CMD_RESP: HS_CFG cmd reply\t"
1212                             " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
1213                             resp->result, conditions,
1214                             phs_cfg->params.hs_config.gpio,
1215                             phs_cfg->params.hs_config.gap);
1216         }
1217         if (conditions != HS_CFG_CANCEL) {
1218                 adapter->is_hs_configured = true;
1219                 if (adapter->iface_type == MWIFIEX_USB)
1220                         mwifiex_hs_activated_event(priv, true);
1221         } else {
1222                 adapter->is_hs_configured = false;
1223                 if (adapter->hs_activated)
1224                         mwifiex_hs_activated_event(priv, false);
1225         }
1226
1227         return 0;
1228 }
1229
1230 /*
1231  * This function wakes up the adapter and generates a Host Sleep
1232  * cancel event on receiving the power up interrupt.
1233  */
1234 void
1235 mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
1236 {
1237         mwifiex_dbg(adapter, INFO,
1238                     "info: %s: auto cancelling host sleep\t"
1239                     "since there is interrupt from the firmware\n",
1240                     __func__);
1241
1242         adapter->if_ops.wakeup(adapter);
1243         adapter->hs_activated = false;
1244         adapter->is_hs_configured = false;
1245         adapter->is_suspended = false;
1246         mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
1247                                                     MWIFIEX_BSS_ROLE_ANY),
1248                                    false);
1249 }
1250 EXPORT_SYMBOL_GPL(mwifiex_process_hs_config);
1251
1252 /*
1253  * This function handles the command response of a sleep confirm command.
1254  *
1255  * The function sets the card state to SLEEP if the response indicates success.
1256  */
1257 void
1258 mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
1259                                    u8 *pbuf, u32 upld_len)
1260 {
1261         struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
1262         struct mwifiex_private *priv =
1263                 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1264         uint16_t result = le16_to_cpu(cmd->result);
1265         uint16_t command = le16_to_cpu(cmd->command);
1266         uint16_t seq_num = le16_to_cpu(cmd->seq_num);
1267
1268         if (!upld_len) {
1269                 mwifiex_dbg(adapter, ERROR,
1270                             "%s: cmd size is 0\n", __func__);
1271                 return;
1272         }
1273
1274         mwifiex_dbg(adapter, CMD,
1275                     "cmd: CMD_RESP: 0x%x, result %d, len %d, seqno 0x%x\n",
1276                     command, result, le16_to_cpu(cmd->size), seq_num);
1277
1278         /* Get BSS number and corresponding priv */
1279         priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
1280                                       HostCmd_GET_BSS_TYPE(seq_num));
1281         if (!priv)
1282                 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1283
1284         /* Update sequence number */
1285         seq_num = HostCmd_GET_SEQ_NO(seq_num);
1286         /* Clear RET_BIT from HostCmd */
1287         command &= HostCmd_CMD_ID_MASK;
1288
1289         if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
1290                 mwifiex_dbg(adapter, ERROR,
1291                             "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
1292                             __func__, command, result);
1293                 return;
1294         }
1295
1296         if (result) {
1297                 mwifiex_dbg(adapter, ERROR,
1298                             "%s: sleep confirm cmd failed\n",
1299                             __func__);
1300                 adapter->pm_wakeup_card_req = false;
1301                 adapter->ps_state = PS_STATE_AWAKE;
1302                 return;
1303         }
1304         adapter->pm_wakeup_card_req = true;
1305         if (adapter->is_hs_configured)
1306                 mwifiex_hs_activated_event(mwifiex_get_priv
1307                                                 (adapter, MWIFIEX_BSS_ROLE_ANY),
1308                                            true);
1309         adapter->ps_state = PS_STATE_SLEEP;
1310         cmd->command = cpu_to_le16(command);
1311         cmd->seq_num = cpu_to_le16(seq_num);
1312 }
1313 EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
1314
1315 /*
1316  * This function prepares an enhanced power mode command.
1317  *
1318  * This function can be used to disable power save or to configure
1319  * power save with auto PS or STA PS or auto deep sleep.
1320  *
1321  * Preparation includes -
1322  *      - Setting command ID, action and proper size
1323  *      - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
1324  *        auto deep sleep TLV (as required)
1325  *      - Ensuring correct endian-ness
1326  */
1327 int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
1328                                struct host_cmd_ds_command *cmd,
1329                                u16 cmd_action, uint16_t ps_bitmap,
1330                                struct mwifiex_ds_auto_ds *auto_ds)
1331 {
1332         struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
1333                 &cmd->params.psmode_enh;
1334         u8 *tlv;
1335         u16 cmd_size = 0;
1336
1337         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
1338         if (cmd_action == DIS_AUTO_PS) {
1339                 psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
1340                 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1341                 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
1342                                         sizeof(psmode_enh->params.ps_bitmap));
1343         } else if (cmd_action == GET_PS) {
1344                 psmode_enh->action = cpu_to_le16(GET_PS);
1345                 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1346                 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
1347                                         sizeof(psmode_enh->params.ps_bitmap));
1348         } else if (cmd_action == EN_AUTO_PS) {
1349                 psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
1350                 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1351                 cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
1352                                         sizeof(psmode_enh->params.ps_bitmap);
1353                 tlv = (u8 *) cmd + cmd_size;
1354                 if (ps_bitmap & BITMAP_STA_PS) {
1355                         struct mwifiex_adapter *adapter = priv->adapter;
1356                         struct mwifiex_ie_types_ps_param *ps_tlv =
1357                                 (struct mwifiex_ie_types_ps_param *) tlv;
1358                         struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
1359                         ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
1360                         ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
1361                                         sizeof(struct mwifiex_ie_types_header));
1362                         cmd_size += sizeof(*ps_tlv);
1363                         tlv += sizeof(*ps_tlv);
1364                         mwifiex_dbg(priv->adapter, CMD,
1365                                     "cmd: PS Command: Enter PS\n");
1366                         ps_mode->null_pkt_interval =
1367                                         cpu_to_le16(adapter->null_pkt_interval);
1368                         ps_mode->multiple_dtims =
1369                                         cpu_to_le16(adapter->multiple_dtim);
1370                         ps_mode->bcn_miss_timeout =
1371                                         cpu_to_le16(adapter->bcn_miss_time_out);
1372                         ps_mode->local_listen_interval =
1373                                 cpu_to_le16(adapter->local_listen_interval);
1374                         ps_mode->adhoc_wake_period =
1375                                 cpu_to_le16(adapter->adhoc_awake_period);
1376                         ps_mode->delay_to_ps =
1377                                         cpu_to_le16(adapter->delay_to_ps);
1378                         ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
1379
1380                 }
1381                 if (ps_bitmap & BITMAP_AUTO_DS) {
1382                         struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
1383                                 (struct mwifiex_ie_types_auto_ds_param *) tlv;
1384                         u16 idletime = 0;
1385
1386                         auto_ds_tlv->header.type =
1387                                 cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
1388                         auto_ds_tlv->header.len =
1389                                 cpu_to_le16(sizeof(*auto_ds_tlv) -
1390                                         sizeof(struct mwifiex_ie_types_header));
1391                         cmd_size += sizeof(*auto_ds_tlv);
1392                         tlv += sizeof(*auto_ds_tlv);
1393                         if (auto_ds)
1394                                 idletime = auto_ds->idle_time;
1395                         mwifiex_dbg(priv->adapter, CMD,
1396                                     "cmd: PS Command: Enter Auto Deep Sleep\n");
1397                         auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
1398                 }
1399                 cmd->size = cpu_to_le16(cmd_size);
1400         }
1401         return 0;
1402 }
1403
1404 /*
1405  * This function handles the command response of an enhanced power mode
1406  * command.
1407  *
1408  * Handling includes changing the header fields into CPU format
1409  * and setting the current enhanced power mode in driver.
1410  */
1411 int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
1412                                struct host_cmd_ds_command *resp,
1413                                struct mwifiex_ds_pm_cfg *pm_cfg)
1414 {
1415         struct mwifiex_adapter *adapter = priv->adapter;
1416         struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
1417                 &resp->params.psmode_enh;
1418         uint16_t action = le16_to_cpu(ps_mode->action);
1419         uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
1420         uint16_t auto_ps_bitmap =
1421                 le16_to_cpu(ps_mode->params.ps_bitmap);
1422
1423         mwifiex_dbg(adapter, INFO,
1424                     "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
1425                     __func__, resp->result, action);
1426         if (action == EN_AUTO_PS) {
1427                 if (auto_ps_bitmap & BITMAP_AUTO_DS) {
1428                         mwifiex_dbg(adapter, CMD,
1429                                     "cmd: Enabled auto deep sleep\n");
1430                         priv->adapter->is_deep_sleep = true;
1431                 }
1432                 if (auto_ps_bitmap & BITMAP_STA_PS) {
1433                         mwifiex_dbg(adapter, CMD,
1434                                     "cmd: Enabled STA power save\n");
1435                         if (adapter->sleep_period.period)
1436                                 mwifiex_dbg(adapter, CMD,
1437                                             "cmd: set to uapsd/pps mode\n");
1438                 }
1439         } else if (action == DIS_AUTO_PS) {
1440                 if (ps_bitmap & BITMAP_AUTO_DS) {
1441                         priv->adapter->is_deep_sleep = false;
1442                         mwifiex_dbg(adapter, CMD,
1443                                     "cmd: Disabled auto deep sleep\n");
1444                 }
1445                 if (ps_bitmap & BITMAP_STA_PS) {
1446                         mwifiex_dbg(adapter, CMD,
1447                                     "cmd: Disabled STA power save\n");
1448                         if (adapter->sleep_period.period) {
1449                                 adapter->delay_null_pkt = false;
1450                                 adapter->tx_lock_flag = false;
1451                                 adapter->pps_uapsd_mode = false;
1452                         }
1453                 }
1454         } else if (action == GET_PS) {
1455                 if (ps_bitmap & BITMAP_STA_PS)
1456                         adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1457                 else
1458                         adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
1459
1460                 mwifiex_dbg(adapter, CMD,
1461                             "cmd: ps_bitmap=%#x\n", ps_bitmap);
1462
1463                 if (pm_cfg) {
1464                         /* This section is for get power save mode */
1465                         if (ps_bitmap & BITMAP_STA_PS)
1466                                 pm_cfg->param.ps_mode = 1;
1467                         else
1468                                 pm_cfg->param.ps_mode = 0;
1469                 }
1470         }
1471         return 0;
1472 }
1473
1474 /*
1475  * This function prepares command to get hardware specifications.
1476  *
1477  * Preparation includes -
1478  *      - Setting command ID, action and proper size
1479  *      - Setting permanent address parameter
1480  *      - Ensuring correct endian-ness
1481  */
1482 int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
1483                             struct host_cmd_ds_command *cmd)
1484 {
1485         struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
1486
1487         cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
1488         cmd->size =
1489                 cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
1490         memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
1491
1492         return 0;
1493 }
1494
1495 /*
1496  * This function handles the command response of get hardware
1497  * specifications.
1498  *
1499  * Handling includes changing the header fields into CPU format
1500  * and saving/updating the following parameters in driver -
1501  *      - Firmware capability information
1502  *      - Firmware band settings
1503  *      - Ad-hoc start band and channel
1504  *      - Ad-hoc 11n activation status
1505  *      - Firmware release number
1506  *      - Number of antennas
1507  *      - Hardware address
1508  *      - Hardware interface version
1509  *      - Firmware version
1510  *      - Region code
1511  *      - 11n capabilities
1512  *      - MCS support fields
1513  *      - MP end port
1514  */
1515 int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
1516                             struct host_cmd_ds_command *resp)
1517 {
1518         struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
1519         struct mwifiex_adapter *adapter = priv->adapter;
1520         struct mwifiex_ie_types_header *tlv;
1521         struct hw_spec_api_rev *api_rev;
1522         u16 resp_size, api_id;
1523         int i, left_len, parsed_len = 0;
1524
1525         adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
1526
1527         if (IS_SUPPORT_MULTI_BANDS(adapter))
1528                 adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
1529         else
1530                 adapter->fw_bands = BAND_B;
1531
1532         adapter->config_bands = adapter->fw_bands;
1533
1534         if (adapter->fw_bands & BAND_A) {
1535                 if (adapter->fw_bands & BAND_GN) {
1536                         adapter->config_bands |= BAND_AN;
1537                         adapter->fw_bands |= BAND_AN;
1538                 }
1539                 if (adapter->fw_bands & BAND_AN) {
1540                         adapter->adhoc_start_band = BAND_A | BAND_AN;
1541                         adapter->adhoc_11n_enabled = true;
1542                 } else {
1543                         adapter->adhoc_start_band = BAND_A;
1544                 }
1545                 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
1546         } else if (adapter->fw_bands & BAND_GN) {
1547                 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
1548                 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1549                 adapter->adhoc_11n_enabled = true;
1550         } else if (adapter->fw_bands & BAND_G) {
1551                 adapter->adhoc_start_band = BAND_G | BAND_B;
1552                 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1553         } else if (adapter->fw_bands & BAND_B) {
1554                 adapter->adhoc_start_band = BAND_B;
1555                 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1556         }
1557
1558         adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
1559         adapter->fw_api_ver = (adapter->fw_release_number >> 16) & 0xff;
1560         adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
1561
1562         if (le32_to_cpu(hw_spec->dot_11ac_dev_cap)) {
1563                 adapter->is_hw_11ac_capable = true;
1564
1565                 /* Copy 11AC cap */
1566                 adapter->hw_dot_11ac_dev_cap =
1567                                         le32_to_cpu(hw_spec->dot_11ac_dev_cap);
1568                 adapter->usr_dot_11ac_dev_cap_bg = adapter->hw_dot_11ac_dev_cap
1569                                         & ~MWIFIEX_DEF_11AC_CAP_BF_RESET_MASK;
1570                 adapter->usr_dot_11ac_dev_cap_a = adapter->hw_dot_11ac_dev_cap
1571                                         & ~MWIFIEX_DEF_11AC_CAP_BF_RESET_MASK;
1572
1573                 /* Copy 11AC mcs */
1574                 adapter->hw_dot_11ac_mcs_support =
1575                                 le32_to_cpu(hw_spec->dot_11ac_mcs_support);
1576                 adapter->usr_dot_11ac_mcs_support =
1577                                         adapter->hw_dot_11ac_mcs_support;
1578         } else {
1579                 adapter->is_hw_11ac_capable = false;
1580         }
1581
1582         resp_size = le16_to_cpu(resp->size) - S_DS_GEN;
1583         if (resp_size > sizeof(struct host_cmd_ds_get_hw_spec)) {
1584                 /* we have variable HW SPEC information */
1585                 left_len = resp_size - sizeof(struct host_cmd_ds_get_hw_spec);
1586                 while (left_len > sizeof(struct mwifiex_ie_types_header)) {
1587                         tlv = (void *)&hw_spec->tlvs + parsed_len;
1588                         switch (le16_to_cpu(tlv->type)) {
1589                         case TLV_TYPE_API_REV:
1590                                 api_rev = (struct hw_spec_api_rev *)tlv;
1591                                 api_id = le16_to_cpu(api_rev->api_id);
1592                                 switch (api_id) {
1593                                 case KEY_API_VER_ID:
1594                                         adapter->key_api_major_ver =
1595                                                         api_rev->major_ver;
1596                                         adapter->key_api_minor_ver =
1597                                                         api_rev->minor_ver;
1598                                         mwifiex_dbg(adapter, INFO,
1599                                                     "key_api v%d.%d\n",
1600                                                     adapter->key_api_major_ver,
1601                                                     adapter->key_api_minor_ver);
1602                                         break;
1603                                 case FW_API_VER_ID:
1604                                         adapter->fw_api_ver =
1605                                                         api_rev->major_ver;
1606                                         mwifiex_dbg(adapter, INFO,
1607                                                     "Firmware api version %d\n",
1608                                                     adapter->fw_api_ver);
1609                                         break;
1610                                 default:
1611                                         mwifiex_dbg(adapter, FATAL,
1612                                                     "Unknown api_id: %d\n",
1613                                                     api_id);
1614                                         break;
1615                                 }
1616                                 break;
1617                         default:
1618                                 mwifiex_dbg(adapter, FATAL,
1619                                             "Unknown GET_HW_SPEC TLV type: %#x\n",
1620                                             le16_to_cpu(tlv->type));
1621                                 break;
1622                         }
1623                         parsed_len += le16_to_cpu(tlv->len) +
1624                                       sizeof(struct mwifiex_ie_types_header);
1625                         left_len -= le16_to_cpu(tlv->len) +
1626                                       sizeof(struct mwifiex_ie_types_header);
1627                 }
1628         }
1629
1630         mwifiex_dbg(adapter, INFO,
1631                     "info: GET_HW_SPEC: fw_release_number- %#x\n",
1632                     adapter->fw_release_number);
1633         mwifiex_dbg(adapter, INFO,
1634                     "info: GET_HW_SPEC: permanent addr: %pM\n",
1635                     hw_spec->permanent_addr);
1636         mwifiex_dbg(adapter, INFO,
1637                     "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
1638                     le16_to_cpu(hw_spec->hw_if_version),
1639                     le16_to_cpu(hw_spec->version));
1640
1641         ether_addr_copy(priv->adapter->perm_addr, hw_spec->permanent_addr);
1642         adapter->region_code = le16_to_cpu(hw_spec->region_code);
1643
1644         for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
1645                 /* Use the region code to search for the index */
1646                 if (adapter->region_code == region_code_index[i])
1647                         break;
1648
1649         /* If it's unidentified region code, use the default (USA) */
1650         if (i >= MWIFIEX_MAX_REGION_CODE) {
1651                 adapter->region_code = 0x10;
1652                 mwifiex_dbg(adapter, WARN,
1653                             "cmd: unknown region code, use default (USA)\n");
1654         }
1655
1656         adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
1657         adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
1658         adapter->user_dev_mcs_support = adapter->hw_dev_mcs_support;
1659
1660         if (adapter->if_ops.update_mp_end_port)
1661                 adapter->if_ops.update_mp_end_port(adapter,
1662                                         le16_to_cpu(hw_spec->mp_end_port));
1663
1664         if (adapter->fw_api_ver == MWIFIEX_FW_V15)
1665                 adapter->scan_chan_gap_enabled = true;
1666
1667         return 0;
1668 }