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