xref: /linux/drivers/net/wireless/marvell/mwifiex/cmdevt.c (revision 995231c820e3bd3633cb38bf4ea6f2541e1da331)
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(unsigned long function_context)
924 {
925 	struct mwifiex_adapter *adapter =
926 		(struct mwifiex_adapter *) function_context;
927 	struct cmd_ctrl_node *cmd_node;
928 
929 	adapter->is_cmd_timedout = 1;
930 	if (!adapter->curr_cmd) {
931 		mwifiex_dbg(adapter, ERROR,
932 			    "cmd: empty curr_cmd\n");
933 		return;
934 	}
935 	cmd_node = adapter->curr_cmd;
936 	if (cmd_node) {
937 		adapter->dbg.timeout_cmd_id =
938 			adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
939 		adapter->dbg.timeout_cmd_act =
940 			adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
941 		mwifiex_dbg(adapter, MSG,
942 			    "%s: Timeout cmd id = %#x, act = %#x\n", __func__,
943 			    adapter->dbg.timeout_cmd_id,
944 			    adapter->dbg.timeout_cmd_act);
945 
946 		mwifiex_dbg(adapter, MSG,
947 			    "num_data_h2c_failure = %d\n",
948 			    adapter->dbg.num_tx_host_to_card_failure);
949 		mwifiex_dbg(adapter, MSG,
950 			    "num_cmd_h2c_failure = %d\n",
951 			    adapter->dbg.num_cmd_host_to_card_failure);
952 
953 		mwifiex_dbg(adapter, MSG,
954 			    "is_cmd_timedout = %d\n",
955 			    adapter->is_cmd_timedout);
956 		mwifiex_dbg(adapter, MSG,
957 			    "num_tx_timeout = %d\n",
958 			    adapter->dbg.num_tx_timeout);
959 
960 		mwifiex_dbg(adapter, MSG,
961 			    "last_cmd_index = %d\n",
962 			    adapter->dbg.last_cmd_index);
963 		mwifiex_dbg(adapter, MSG,
964 			    "last_cmd_id: %*ph\n",
965 			    (int)sizeof(adapter->dbg.last_cmd_id),
966 			    adapter->dbg.last_cmd_id);
967 		mwifiex_dbg(adapter, MSG,
968 			    "last_cmd_act: %*ph\n",
969 			    (int)sizeof(adapter->dbg.last_cmd_act),
970 			    adapter->dbg.last_cmd_act);
971 
972 		mwifiex_dbg(adapter, MSG,
973 			    "last_cmd_resp_index = %d\n",
974 			    adapter->dbg.last_cmd_resp_index);
975 		mwifiex_dbg(adapter, MSG,
976 			    "last_cmd_resp_id: %*ph\n",
977 			    (int)sizeof(adapter->dbg.last_cmd_resp_id),
978 			    adapter->dbg.last_cmd_resp_id);
979 
980 		mwifiex_dbg(adapter, MSG,
981 			    "last_event_index = %d\n",
982 			    adapter->dbg.last_event_index);
983 		mwifiex_dbg(adapter, MSG,
984 			    "last_event: %*ph\n",
985 			    (int)sizeof(adapter->dbg.last_event),
986 			    adapter->dbg.last_event);
987 
988 		mwifiex_dbg(adapter, MSG,
989 			    "data_sent=%d cmd_sent=%d\n",
990 			    adapter->data_sent, adapter->cmd_sent);
991 
992 		mwifiex_dbg(adapter, MSG,
993 			    "ps_mode=%d ps_state=%d\n",
994 			    adapter->ps_mode, adapter->ps_state);
995 
996 		if (cmd_node->wait_q_enabled) {
997 			adapter->cmd_wait_q.status = -ETIMEDOUT;
998 			mwifiex_cancel_pending_ioctl(adapter);
999 		}
1000 	}
1001 	if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
1002 		mwifiex_init_fw_complete(adapter);
1003 		return;
1004 	}
1005 
1006 	if (adapter->if_ops.device_dump)
1007 		adapter->if_ops.device_dump(adapter);
1008 
1009 	if (adapter->if_ops.card_reset)
1010 		adapter->if_ops.card_reset(adapter);
1011 }
1012 
1013 void
1014 mwifiex_cancel_pending_scan_cmd(struct mwifiex_adapter *adapter)
1015 {
1016 	struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
1017 	unsigned long flags;
1018 
1019 	/* Cancel all pending scan command */
1020 	spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
1021 	list_for_each_entry_safe(cmd_node, tmp_node,
1022 				 &adapter->scan_pending_q, list) {
1023 		list_del(&cmd_node->list);
1024 		cmd_node->wait_q_enabled = false;
1025 		mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
1026 	}
1027 	spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
1028 }
1029 
1030 /*
1031  * This function cancels all the pending commands.
1032  *
1033  * The current command, all commands in command pending queue and all scan
1034  * commands in scan pending queue are cancelled. All the completion callbacks
1035  * are called with failure status to ensure cleanup.
1036  */
1037 void
1038 mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
1039 {
1040 	struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
1041 	unsigned long flags, cmd_flags;
1042 
1043 	spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1044 	/* Cancel current cmd */
1045 	if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
1046 		adapter->cmd_wait_q.status = -1;
1047 		mwifiex_complete_cmd(adapter, adapter->curr_cmd);
1048 		adapter->curr_cmd->wait_q_enabled = false;
1049 		/* no recycle probably wait for response */
1050 	}
1051 	/* Cancel all pending command */
1052 	spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
1053 	list_for_each_entry_safe(cmd_node, tmp_node,
1054 				 &adapter->cmd_pending_q, list) {
1055 		list_del(&cmd_node->list);
1056 
1057 		if (cmd_node->wait_q_enabled)
1058 			adapter->cmd_wait_q.status = -1;
1059 		mwifiex_recycle_cmd_node(adapter, cmd_node);
1060 	}
1061 	spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1062 	spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1063 
1064 	mwifiex_cancel_scan(adapter);
1065 }
1066 
1067 /*
1068  * This function cancels all pending commands that matches with
1069  * the given IOCTL request.
1070  *
1071  * Both the current command buffer and the pending command queue are
1072  * searched for matching IOCTL request. The completion callback of
1073  * the matched command is called with failure status to ensure cleanup.
1074  * In case of scan commands, all pending commands in scan pending queue
1075  * are cancelled.
1076  */
1077 static void
1078 mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
1079 {
1080 	struct cmd_ctrl_node *cmd_node = NULL;
1081 	unsigned long cmd_flags;
1082 
1083 	if ((adapter->curr_cmd) &&
1084 	    (adapter->curr_cmd->wait_q_enabled)) {
1085 		spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1086 		cmd_node = adapter->curr_cmd;
1087 		/* setting curr_cmd to NULL is quite dangerous, because
1088 		 * mwifiex_process_cmdresp checks curr_cmd to be != NULL
1089 		 * at the beginning then relies on it and dereferences
1090 		 * it at will
1091 		 * this probably works since mwifiex_cmd_timeout_func
1092 		 * is the only caller of this function and responses
1093 		 * at that point
1094 		 */
1095 		adapter->curr_cmd = NULL;
1096 		spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1097 
1098 		mwifiex_recycle_cmd_node(adapter, cmd_node);
1099 	}
1100 
1101 	mwifiex_cancel_scan(adapter);
1102 }
1103 
1104 /*
1105  * This function sends the sleep confirm command to firmware, if
1106  * possible.
1107  *
1108  * The sleep confirm command cannot be issued if command response,
1109  * data response or event response is awaiting handling, or if we
1110  * are in the middle of sending a command, or expecting a command
1111  * response.
1112  */
1113 void
1114 mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
1115 {
1116 	if (!adapter->cmd_sent && !atomic_read(&adapter->tx_hw_pending) &&
1117 	    !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
1118 		mwifiex_dnld_sleep_confirm_cmd(adapter);
1119 	else
1120 		mwifiex_dbg(adapter, CMD,
1121 			    "cmd: Delay Sleep Confirm (%s%s%s%s)\n",
1122 			    (adapter->cmd_sent) ? "D" : "",
1123 			    atomic_read(&adapter->tx_hw_pending) ? "T" : "",
1124 			    (adapter->curr_cmd) ? "C" : "",
1125 			    (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
1126 }
1127 
1128 /*
1129  * This function sends a Host Sleep activated event to applications.
1130  *
1131  * This event is generated by the driver, with a blank event body.
1132  */
1133 void
1134 mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
1135 {
1136 	if (activated) {
1137 		if (priv->adapter->is_hs_configured) {
1138 			priv->adapter->hs_activated = true;
1139 			mwifiex_update_rxreor_flags(priv->adapter,
1140 						    RXREOR_FORCE_NO_DROP);
1141 			mwifiex_dbg(priv->adapter, EVENT,
1142 				    "event: hs_activated\n");
1143 			priv->adapter->hs_activate_wait_q_woken = true;
1144 			wake_up_interruptible(
1145 				&priv->adapter->hs_activate_wait_q);
1146 		} else {
1147 			mwifiex_dbg(priv->adapter, EVENT,
1148 				    "event: HS not configured\n");
1149 		}
1150 	} else {
1151 		mwifiex_dbg(priv->adapter, EVENT,
1152 			    "event: hs_deactivated\n");
1153 		priv->adapter->hs_activated = false;
1154 	}
1155 }
1156 
1157 /*
1158  * This function handles the command response of a Host Sleep configuration
1159  * command.
1160  *
1161  * Handling includes changing the header fields into CPU format
1162  * and setting the current host sleep activation status in driver.
1163  *
1164  * In case host sleep status change, the function generates an event to
1165  * notify the applications.
1166  */
1167 int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
1168 			      struct host_cmd_ds_command *resp)
1169 {
1170 	struct mwifiex_adapter *adapter = priv->adapter;
1171 	struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
1172 		&resp->params.opt_hs_cfg;
1173 	uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
1174 
1175 	if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE) &&
1176 	    adapter->iface_type != MWIFIEX_USB) {
1177 		mwifiex_hs_activated_event(priv, true);
1178 		return 0;
1179 	} else {
1180 		mwifiex_dbg(adapter, CMD,
1181 			    "cmd: CMD_RESP: HS_CFG cmd reply\t"
1182 			    " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
1183 			    resp->result, conditions,
1184 			    phs_cfg->params.hs_config.gpio,
1185 			    phs_cfg->params.hs_config.gap);
1186 	}
1187 	if (conditions != HS_CFG_CANCEL) {
1188 		adapter->is_hs_configured = true;
1189 		if (adapter->iface_type == MWIFIEX_USB)
1190 			mwifiex_hs_activated_event(priv, true);
1191 	} else {
1192 		adapter->is_hs_configured = false;
1193 		if (adapter->hs_activated)
1194 			mwifiex_hs_activated_event(priv, false);
1195 	}
1196 
1197 	return 0;
1198 }
1199 
1200 /*
1201  * This function wakes up the adapter and generates a Host Sleep
1202  * cancel event on receiving the power up interrupt.
1203  */
1204 void
1205 mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
1206 {
1207 	mwifiex_dbg(adapter, INFO,
1208 		    "info: %s: auto cancelling host sleep\t"
1209 		    "since there is interrupt from the firmware\n",
1210 		    __func__);
1211 
1212 	adapter->if_ops.wakeup(adapter);
1213 	adapter->hs_activated = false;
1214 	adapter->is_hs_configured = false;
1215 	adapter->is_suspended = false;
1216 	mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
1217 						    MWIFIEX_BSS_ROLE_ANY),
1218 				   false);
1219 }
1220 EXPORT_SYMBOL_GPL(mwifiex_process_hs_config);
1221 
1222 /*
1223  * This function handles the command response of a sleep confirm command.
1224  *
1225  * The function sets the card state to SLEEP if the response indicates success.
1226  */
1227 void
1228 mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
1229 				   u8 *pbuf, u32 upld_len)
1230 {
1231 	struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
1232 	struct mwifiex_private *priv =
1233 		mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1234 	uint16_t result = le16_to_cpu(cmd->result);
1235 	uint16_t command = le16_to_cpu(cmd->command);
1236 	uint16_t seq_num = le16_to_cpu(cmd->seq_num);
1237 
1238 	if (!upld_len) {
1239 		mwifiex_dbg(adapter, ERROR,
1240 			    "%s: cmd size is 0\n", __func__);
1241 		return;
1242 	}
1243 
1244 	mwifiex_dbg(adapter, CMD,
1245 		    "cmd: CMD_RESP: 0x%x, result %d, len %d, seqno 0x%x\n",
1246 		    command, result, le16_to_cpu(cmd->size), seq_num);
1247 
1248 	/* Get BSS number and corresponding priv */
1249 	priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
1250 				      HostCmd_GET_BSS_TYPE(seq_num));
1251 	if (!priv)
1252 		priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1253 
1254 	/* Update sequence number */
1255 	seq_num = HostCmd_GET_SEQ_NO(seq_num);
1256 	/* Clear RET_BIT from HostCmd */
1257 	command &= HostCmd_CMD_ID_MASK;
1258 
1259 	if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
1260 		mwifiex_dbg(adapter, ERROR,
1261 			    "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
1262 			    __func__, command, result);
1263 		return;
1264 	}
1265 
1266 	if (result) {
1267 		mwifiex_dbg(adapter, ERROR,
1268 			    "%s: sleep confirm cmd failed\n",
1269 			    __func__);
1270 		adapter->pm_wakeup_card_req = false;
1271 		adapter->ps_state = PS_STATE_AWAKE;
1272 		return;
1273 	}
1274 	adapter->pm_wakeup_card_req = true;
1275 	if (adapter->is_hs_configured)
1276 		mwifiex_hs_activated_event(mwifiex_get_priv
1277 						(adapter, MWIFIEX_BSS_ROLE_ANY),
1278 					   true);
1279 	adapter->ps_state = PS_STATE_SLEEP;
1280 	cmd->command = cpu_to_le16(command);
1281 	cmd->seq_num = cpu_to_le16(seq_num);
1282 }
1283 EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
1284 
1285 /*
1286  * This function prepares an enhanced power mode command.
1287  *
1288  * This function can be used to disable power save or to configure
1289  * power save with auto PS or STA PS or auto deep sleep.
1290  *
1291  * Preparation includes -
1292  *      - Setting command ID, action and proper size
1293  *      - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
1294  *        auto deep sleep TLV (as required)
1295  *      - Ensuring correct endian-ness
1296  */
1297 int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
1298 			       struct host_cmd_ds_command *cmd,
1299 			       u16 cmd_action, uint16_t ps_bitmap,
1300 			       struct mwifiex_ds_auto_ds *auto_ds)
1301 {
1302 	struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
1303 		&cmd->params.psmode_enh;
1304 	u8 *tlv;
1305 	u16 cmd_size = 0;
1306 
1307 	cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
1308 	if (cmd_action == DIS_AUTO_PS) {
1309 		psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
1310 		psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1311 		cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
1312 					sizeof(psmode_enh->params.ps_bitmap));
1313 	} else if (cmd_action == GET_PS) {
1314 		psmode_enh->action = cpu_to_le16(GET_PS);
1315 		psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1316 		cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
1317 					sizeof(psmode_enh->params.ps_bitmap));
1318 	} else if (cmd_action == EN_AUTO_PS) {
1319 		psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
1320 		psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1321 		cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
1322 					sizeof(psmode_enh->params.ps_bitmap);
1323 		tlv = (u8 *) cmd + cmd_size;
1324 		if (ps_bitmap & BITMAP_STA_PS) {
1325 			struct mwifiex_adapter *adapter = priv->adapter;
1326 			struct mwifiex_ie_types_ps_param *ps_tlv =
1327 				(struct mwifiex_ie_types_ps_param *) tlv;
1328 			struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
1329 			ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
1330 			ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
1331 					sizeof(struct mwifiex_ie_types_header));
1332 			cmd_size += sizeof(*ps_tlv);
1333 			tlv += sizeof(*ps_tlv);
1334 			mwifiex_dbg(priv->adapter, CMD,
1335 				    "cmd: PS Command: Enter PS\n");
1336 			ps_mode->null_pkt_interval =
1337 					cpu_to_le16(adapter->null_pkt_interval);
1338 			ps_mode->multiple_dtims =
1339 					cpu_to_le16(adapter->multiple_dtim);
1340 			ps_mode->bcn_miss_timeout =
1341 					cpu_to_le16(adapter->bcn_miss_time_out);
1342 			ps_mode->local_listen_interval =
1343 				cpu_to_le16(adapter->local_listen_interval);
1344 			ps_mode->adhoc_wake_period =
1345 				cpu_to_le16(adapter->adhoc_awake_period);
1346 			ps_mode->delay_to_ps =
1347 					cpu_to_le16(adapter->delay_to_ps);
1348 			ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
1349 
1350 		}
1351 		if (ps_bitmap & BITMAP_AUTO_DS) {
1352 			struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
1353 				(struct mwifiex_ie_types_auto_ds_param *) tlv;
1354 			u16 idletime = 0;
1355 
1356 			auto_ds_tlv->header.type =
1357 				cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
1358 			auto_ds_tlv->header.len =
1359 				cpu_to_le16(sizeof(*auto_ds_tlv) -
1360 					sizeof(struct mwifiex_ie_types_header));
1361 			cmd_size += sizeof(*auto_ds_tlv);
1362 			tlv += sizeof(*auto_ds_tlv);
1363 			if (auto_ds)
1364 				idletime = auto_ds->idle_time;
1365 			mwifiex_dbg(priv->adapter, CMD,
1366 				    "cmd: PS Command: Enter Auto Deep Sleep\n");
1367 			auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
1368 		}
1369 		cmd->size = cpu_to_le16(cmd_size);
1370 	}
1371 	return 0;
1372 }
1373 
1374 /*
1375  * This function handles the command response of an enhanced power mode
1376  * command.
1377  *
1378  * Handling includes changing the header fields into CPU format
1379  * and setting the current enhanced power mode in driver.
1380  */
1381 int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
1382 			       struct host_cmd_ds_command *resp,
1383 			       struct mwifiex_ds_pm_cfg *pm_cfg)
1384 {
1385 	struct mwifiex_adapter *adapter = priv->adapter;
1386 	struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
1387 		&resp->params.psmode_enh;
1388 	uint16_t action = le16_to_cpu(ps_mode->action);
1389 	uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
1390 	uint16_t auto_ps_bitmap =
1391 		le16_to_cpu(ps_mode->params.ps_bitmap);
1392 
1393 	mwifiex_dbg(adapter, INFO,
1394 		    "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
1395 		    __func__, resp->result, action);
1396 	if (action == EN_AUTO_PS) {
1397 		if (auto_ps_bitmap & BITMAP_AUTO_DS) {
1398 			mwifiex_dbg(adapter, CMD,
1399 				    "cmd: Enabled auto deep sleep\n");
1400 			priv->adapter->is_deep_sleep = true;
1401 		}
1402 		if (auto_ps_bitmap & BITMAP_STA_PS) {
1403 			mwifiex_dbg(adapter, CMD,
1404 				    "cmd: Enabled STA power save\n");
1405 			if (adapter->sleep_period.period)
1406 				mwifiex_dbg(adapter, CMD,
1407 					    "cmd: set to uapsd/pps mode\n");
1408 		}
1409 	} else if (action == DIS_AUTO_PS) {
1410 		if (ps_bitmap & BITMAP_AUTO_DS) {
1411 			priv->adapter->is_deep_sleep = false;
1412 			mwifiex_dbg(adapter, CMD,
1413 				    "cmd: Disabled auto deep sleep\n");
1414 		}
1415 		if (ps_bitmap & BITMAP_STA_PS) {
1416 			mwifiex_dbg(adapter, CMD,
1417 				    "cmd: Disabled STA power save\n");
1418 			if (adapter->sleep_period.period) {
1419 				adapter->delay_null_pkt = false;
1420 				adapter->tx_lock_flag = false;
1421 				adapter->pps_uapsd_mode = false;
1422 			}
1423 		}
1424 	} else if (action == GET_PS) {
1425 		if (ps_bitmap & BITMAP_STA_PS)
1426 			adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1427 		else
1428 			adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
1429 
1430 		mwifiex_dbg(adapter, CMD,
1431 			    "cmd: ps_bitmap=%#x\n", ps_bitmap);
1432 
1433 		if (pm_cfg) {
1434 			/* This section is for get power save mode */
1435 			if (ps_bitmap & BITMAP_STA_PS)
1436 				pm_cfg->param.ps_mode = 1;
1437 			else
1438 				pm_cfg->param.ps_mode = 0;
1439 		}
1440 	}
1441 	return 0;
1442 }
1443 
1444 /*
1445  * This function prepares command to get hardware specifications.
1446  *
1447  * Preparation includes -
1448  *      - Setting command ID, action and proper size
1449  *      - Setting permanent address parameter
1450  *      - Ensuring correct endian-ness
1451  */
1452 int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
1453 			    struct host_cmd_ds_command *cmd)
1454 {
1455 	struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
1456 
1457 	cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
1458 	cmd->size =
1459 		cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
1460 	memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
1461 
1462 	return 0;
1463 }
1464 
1465 /*
1466  * This function handles the command response of get hardware
1467  * specifications.
1468  *
1469  * Handling includes changing the header fields into CPU format
1470  * and saving/updating the following parameters in driver -
1471  *      - Firmware capability information
1472  *      - Firmware band settings
1473  *      - Ad-hoc start band and channel
1474  *      - Ad-hoc 11n activation status
1475  *      - Firmware release number
1476  *      - Number of antennas
1477  *      - Hardware address
1478  *      - Hardware interface version
1479  *      - Firmware version
1480  *      - Region code
1481  *      - 11n capabilities
1482  *      - MCS support fields
1483  *      - MP end port
1484  */
1485 int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
1486 			    struct host_cmd_ds_command *resp)
1487 {
1488 	struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
1489 	struct mwifiex_adapter *adapter = priv->adapter;
1490 	struct mwifiex_ie_types_header *tlv;
1491 	struct hw_spec_api_rev *api_rev;
1492 	u16 resp_size, api_id;
1493 	int i, left_len, parsed_len = 0;
1494 
1495 	adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
1496 
1497 	if (IS_SUPPORT_MULTI_BANDS(adapter))
1498 		adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
1499 	else
1500 		adapter->fw_bands = BAND_B;
1501 
1502 	adapter->config_bands = adapter->fw_bands;
1503 
1504 	if (adapter->fw_bands & BAND_A) {
1505 		if (adapter->fw_bands & BAND_GN) {
1506 			adapter->config_bands |= BAND_AN;
1507 			adapter->fw_bands |= BAND_AN;
1508 		}
1509 		if (adapter->fw_bands & BAND_AN) {
1510 			adapter->adhoc_start_band = BAND_A | BAND_AN;
1511 			adapter->adhoc_11n_enabled = true;
1512 		} else {
1513 			adapter->adhoc_start_band = BAND_A;
1514 		}
1515 		priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
1516 	} else if (adapter->fw_bands & BAND_GN) {
1517 		adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
1518 		priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1519 		adapter->adhoc_11n_enabled = true;
1520 	} else if (adapter->fw_bands & BAND_G) {
1521 		adapter->adhoc_start_band = BAND_G | BAND_B;
1522 		priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1523 	} else if (adapter->fw_bands & BAND_B) {
1524 		adapter->adhoc_start_band = BAND_B;
1525 		priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1526 	}
1527 
1528 	adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
1529 	adapter->fw_api_ver = (adapter->fw_release_number >> 16) & 0xff;
1530 	adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
1531 
1532 	if (le32_to_cpu(hw_spec->dot_11ac_dev_cap)) {
1533 		adapter->is_hw_11ac_capable = true;
1534 
1535 		/* Copy 11AC cap */
1536 		adapter->hw_dot_11ac_dev_cap =
1537 					le32_to_cpu(hw_spec->dot_11ac_dev_cap);
1538 		adapter->usr_dot_11ac_dev_cap_bg = adapter->hw_dot_11ac_dev_cap
1539 					& ~MWIFIEX_DEF_11AC_CAP_BF_RESET_MASK;
1540 		adapter->usr_dot_11ac_dev_cap_a = adapter->hw_dot_11ac_dev_cap
1541 					& ~MWIFIEX_DEF_11AC_CAP_BF_RESET_MASK;
1542 
1543 		/* Copy 11AC mcs */
1544 		adapter->hw_dot_11ac_mcs_support =
1545 				le32_to_cpu(hw_spec->dot_11ac_mcs_support);
1546 		adapter->usr_dot_11ac_mcs_support =
1547 					adapter->hw_dot_11ac_mcs_support;
1548 	} else {
1549 		adapter->is_hw_11ac_capable = false;
1550 	}
1551 
1552 	resp_size = le16_to_cpu(resp->size) - S_DS_GEN;
1553 	if (resp_size > sizeof(struct host_cmd_ds_get_hw_spec)) {
1554 		/* we have variable HW SPEC information */
1555 		left_len = resp_size - sizeof(struct host_cmd_ds_get_hw_spec);
1556 		while (left_len > sizeof(struct mwifiex_ie_types_header)) {
1557 			tlv = (void *)&hw_spec->tlvs + parsed_len;
1558 			switch (le16_to_cpu(tlv->type)) {
1559 			case TLV_TYPE_API_REV:
1560 				api_rev = (struct hw_spec_api_rev *)tlv;
1561 				api_id = le16_to_cpu(api_rev->api_id);
1562 				switch (api_id) {
1563 				case KEY_API_VER_ID:
1564 					adapter->key_api_major_ver =
1565 							api_rev->major_ver;
1566 					adapter->key_api_minor_ver =
1567 							api_rev->minor_ver;
1568 					mwifiex_dbg(adapter, INFO,
1569 						    "key_api v%d.%d\n",
1570 						    adapter->key_api_major_ver,
1571 						    adapter->key_api_minor_ver);
1572 					break;
1573 				case FW_API_VER_ID:
1574 					adapter->fw_api_ver =
1575 							api_rev->major_ver;
1576 					mwifiex_dbg(adapter, INFO,
1577 						    "Firmware api version %d\n",
1578 						    adapter->fw_api_ver);
1579 					break;
1580 				default:
1581 					mwifiex_dbg(adapter, FATAL,
1582 						    "Unknown api_id: %d\n",
1583 						    api_id);
1584 					break;
1585 				}
1586 				break;
1587 			default:
1588 				mwifiex_dbg(adapter, FATAL,
1589 					    "Unknown GET_HW_SPEC TLV type: %#x\n",
1590 					    le16_to_cpu(tlv->type));
1591 				break;
1592 			}
1593 			parsed_len += le16_to_cpu(tlv->len) +
1594 				      sizeof(struct mwifiex_ie_types_header);
1595 			left_len -= le16_to_cpu(tlv->len) +
1596 				      sizeof(struct mwifiex_ie_types_header);
1597 		}
1598 	}
1599 
1600 	mwifiex_dbg(adapter, INFO,
1601 		    "info: GET_HW_SPEC: fw_release_number- %#x\n",
1602 		    adapter->fw_release_number);
1603 	mwifiex_dbg(adapter, INFO,
1604 		    "info: GET_HW_SPEC: permanent addr: %pM\n",
1605 		    hw_spec->permanent_addr);
1606 	mwifiex_dbg(adapter, INFO,
1607 		    "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
1608 		    le16_to_cpu(hw_spec->hw_if_version),
1609 		    le16_to_cpu(hw_spec->version));
1610 
1611 	ether_addr_copy(priv->adapter->perm_addr, hw_spec->permanent_addr);
1612 	adapter->region_code = le16_to_cpu(hw_spec->region_code);
1613 
1614 	for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
1615 		/* Use the region code to search for the index */
1616 		if (adapter->region_code == region_code_index[i])
1617 			break;
1618 
1619 	/* If it's unidentified region code, use the default (world) */
1620 	if (i >= MWIFIEX_MAX_REGION_CODE) {
1621 		adapter->region_code = 0x00;
1622 		mwifiex_dbg(adapter, WARN,
1623 			    "cmd: unknown region code, use default (USA)\n");
1624 	}
1625 
1626 	adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
1627 	adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
1628 	adapter->user_dev_mcs_support = adapter->hw_dev_mcs_support;
1629 
1630 	if (adapter->if_ops.update_mp_end_port)
1631 		adapter->if_ops.update_mp_end_port(adapter,
1632 					le16_to_cpu(hw_spec->mp_end_port));
1633 
1634 	if (adapter->fw_api_ver == MWIFIEX_FW_V15)
1635 		adapter->scan_chan_gap_enabled = true;
1636 
1637 	return 0;
1638 }
1639 
1640 /* This function handles the command response of hs wakeup reason
1641  * command.
1642  */
1643 int mwifiex_ret_wakeup_reason(struct mwifiex_private *priv,
1644 			      struct host_cmd_ds_command *resp,
1645 			      struct host_cmd_ds_wakeup_reason *wakeup_reason)
1646 {
1647 	wakeup_reason->wakeup_reason =
1648 		resp->params.hs_wakeup_reason.wakeup_reason;
1649 
1650 	return 0;
1651 }
1652