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