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