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