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