blob: c68adec3cc8b6522678c98582c7781b885193849 [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
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,
Amitkumar Karwar600f5d92011-04-13 17:27:06 -070039 u32 cmd_oid, void *data_buf)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070040{
41 cmd_node->priv = priv;
42 cmd_node->cmd_oid = cmd_oid;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -070043 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 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -070049 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,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -070070 struct cmd_ctrl_node, list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070071 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;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070094 cmd_node->data_buf = NULL;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -070095 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070096
Amitkumar Karwar5cf80992011-09-21 21:43:25 -070097 if (cmd_node->cmd_skb)
98 skb_trim(cmd_node->cmd_skb, 0);
99
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700100 if (cmd_node->resp_skb) {
Amitkumar Karward930fae2011-10-11 17:41:21 -0700101 adapter->if_ops.cmdrsp_complete(adapter, cmd_node->resp_skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700102 cmd_node->resp_skb = NULL;
103 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700104}
105
106/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700107 * 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,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700114 struct host_cmd_ds_command *cmd,
115 struct mwifiex_ds_misc_cmd *pcmd_ptr)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700116{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700117 /* Copy the HOST command to command buffer */
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700118 memcpy(cmd, pcmd_ptr->cmd, pcmd_ptr->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700119 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;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700136 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700137 struct host_cmd_ds_command *host_cmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700138 uint16_t cmd_code;
139 uint16_t cmd_size;
140 struct timeval tstamp;
141 unsigned long flags;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700142 __le32 tmp;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700143
144 if (!adapter || !cmd_node)
145 return -1;
146
147 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700148
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");
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700153 if (cmd_node->wait_q_enabled)
154 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700155 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
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700162 (adapter->seq_num,
163 cmd_node->priv->bss_num,
164 cmd_node->priv->bss_type));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700165
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,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700179 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN)), cmd_size,
180 le16_to_cpu(host_cmd->seq_num));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700181
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700182 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 }
Bing Zhao18bf9652011-04-06 16:46:55 -0700199
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700200 if (ret == -1) {
201 dev_err(adapter->dev, "DNLD_CMD: host to card failed\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700202 if (adapter->iface_type == MWIFIEX_USB)
203 adapter->cmd_sent = false;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700204 if (cmd_node->wait_q_enabled)
205 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700206 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 =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700218 (adapter->dbg.last_cmd_index + 1) % DBG_CMD_NUM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700219 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index] = cmd_code;
220 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index] =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700221 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700222
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,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700228 jiffies + (MWIFIEX_TIMER_10S * HZ) / 1000);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700229
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{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700244 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700245 struct mwifiex_private *priv;
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700246 struct mwifiex_opt_sleep_confirm *sleep_cfm_buf =
247 (struct mwifiex_opt_sleep_confirm *)
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700248 adapter->sleep_cfm->data;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700249 struct sk_buff *sleep_cfm_tmp;
250 __le32 tmp;
251
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700252 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
253
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700254 sleep_cfm_buf->seq_num =
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700255 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
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700260 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 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700282
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))
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700289 == MWIFIEX_BSS_ROLE_STA) {
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700290 if (!sleep_cfm_buf->resp_ctrl)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700291 /* 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
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700297 if (!sleep_cfm_buf->resp_ctrl &&
298 (adapter->is_hs_configured &&
299 !adapter->sleep_period.period)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700300 adapter->pm_wakeup_card_req = true;
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700301 mwifiex_hs_activated_event(mwifiex_get_priv
302 (adapter, MWIFIEX_BSS_ROLE_STA), true);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700303 }
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",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700332 __func__);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200333 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700334 }
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;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700381
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);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700387 }
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{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700410 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700411 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;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700416 struct mwifiex_rxinfo *rx_info;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700417
418 /* Save the last event to debug log */
419 adapter->dbg.last_event_index =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700420 (adapter->dbg.last_event_index + 1) % DBG_CMD_NUM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700421 adapter->dbg.last_event[adapter->dbg.last_event_index] =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700422 (u16) eventcause;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700423
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);
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800435 rx_info->bss_num = priv->bss_num;
436 rx_info->bss_type = priv->bss_type;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700437 }
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",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700442 tstamp.tv_sec, tstamp.tv_usec, eventcause);
Avinash Patil03785382012-05-08 18:30:14 -0700443 } 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);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700448 }
449
450 ret = mwifiex_process_sta_event(priv);
451
452 adapter->event_cause = 0;
453 adapter->event_skb = NULL;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700454 adapter->if_ops.event_complete(adapter, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700455
456 return ret;
457}
458
459/*
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700460 * 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;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700472
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.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700484 *
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 */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700493int mwifiex_send_cmd_async(struct mwifiex_private *priv, uint16_t cmd_no,
494 u16 cmd_action, u32 cmd_oid, void *data_buf)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700495{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700496 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700497 struct mwifiex_adapter *adapter = priv->adapter;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700498 struct cmd_ctrl_node *cmd_node;
499 struct host_cmd_ds_command *cmd_ptr;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700500
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 */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700532 mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700533
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) {
Avinash Patil40d07032012-05-08 18:30:19 -0700548 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 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700562 } 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",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700570 cmd_no);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700571 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
572 return -1;
573 }
574
575 /* Send command */
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700576 if (cmd_no == HostCmd_CMD_802_11_SCAN) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700577 mwifiex_queue_scan_cmd(priv, cmd_node);
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700578 } else {
579 adapter->cmd_queued = cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700580 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
Amitkumar Karwar1a1fb972012-06-27 19:57:56 -0700581 queue_work(adapter->workqueue, &adapter->main_work);
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700582 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700583
584 return ret;
585}
586
587/*
588 * This function returns a command to the command free queue.
589 *
590 * The function also calls the completion callback if required, before
591 * cleaning the command node and re-inserting it into the free queue.
592 */
593void
594mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
595 struct cmd_ctrl_node *cmd_node)
596{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700597 unsigned long flags;
598
Yogesh Ashok Powar19a89862011-04-13 17:27:07 -0700599 if (!cmd_node)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700600 return;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700601
602 if (cmd_node->wait_q_enabled)
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700603 mwifiex_complete_cmd(adapter, cmd_node);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700604 /* Clean the node */
605 mwifiex_clean_cmd_node(adapter, cmd_node);
606
607 /* Insert node into cmd_free_q */
608 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
609 list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
610 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700611}
612
613/*
614 * This function queues a command to the command pending queue.
615 *
616 * This in effect adds the command to the command list to be executed.
617 * Exit PS command is handled specially, by placing it always to the
618 * front of the command queue.
619 */
620void
621mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
622 struct cmd_ctrl_node *cmd_node, u32 add_tail)
623{
624 struct host_cmd_ds_command *host_cmd = NULL;
625 u16 command;
626 unsigned long flags;
627
628 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
629 if (!host_cmd) {
630 dev_err(adapter->dev, "QUEUE_CMD: host_cmd is NULL\n");
631 return;
632 }
633
634 command = le16_to_cpu(host_cmd->command);
635
636 /* Exit_PS command needs to be queued in the header always. */
637 if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
638 struct host_cmd_ds_802_11_ps_mode_enh *pm =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700639 &host_cmd->params.psmode_enh;
640 if ((le16_to_cpu(pm->action) == DIS_PS) ||
641 (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700642 if (adapter->ps_state != PS_STATE_AWAKE)
643 add_tail = false;
644 }
645 }
646
647 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
648 if (add_tail)
649 list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
650 else
651 list_add(&cmd_node->list, &adapter->cmd_pending_q);
652 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
653
654 dev_dbg(adapter->dev, "cmd: QUEUE_CMD: cmd=%#x is queued\n", command);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700655}
656
657/*
658 * This function executes the next command in command pending queue.
659 *
660 * This function will fail if a command is already in processing stage,
661 * otherwise it will dequeue the first command from the command pending
662 * queue and send to the firmware.
663 *
664 * If the device is currently in host sleep mode, any commands, except the
665 * host sleep configuration command will de-activate the host sleep. For PS
666 * mode, the function will put the firmware back to sleep if applicable.
667 */
668int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
669{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700670 struct mwifiex_private *priv;
671 struct cmd_ctrl_node *cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700672 int ret = 0;
673 struct host_cmd_ds_command *host_cmd;
674 unsigned long cmd_flags;
675 unsigned long cmd_pending_q_flags;
676
677 /* Check if already in processing */
678 if (adapter->curr_cmd) {
679 dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
680 return -1;
681 }
682
683 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
684 /* Check if any command is pending */
685 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
686 if (list_empty(&adapter->cmd_pending_q)) {
687 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
688 cmd_pending_q_flags);
689 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
690 return 0;
691 }
692 cmd_node = list_first_entry(&adapter->cmd_pending_q,
693 struct cmd_ctrl_node, list);
694 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
695 cmd_pending_q_flags);
696
697 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
698 priv = cmd_node->priv;
699
700 if (adapter->ps_state != PS_STATE_AWAKE) {
701 dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
702 " this should not happen\n", __func__);
703 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
704 return ret;
705 }
706
707 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
708 list_del(&cmd_node->list);
709 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
710 cmd_pending_q_flags);
711
712 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
713 ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
714 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
715 /* Any command sent to the firmware when host is in sleep
716 * mode should de-configure host sleep. We should skip the
717 * host sleep configuration command itself though
718 */
719 if (priv && (host_cmd->command !=
720 cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
721 if (adapter->hs_activated) {
722 adapter->is_hs_configured = false;
723 mwifiex_hs_activated_event(priv, false);
724 }
725 }
726
727 return ret;
728}
729
730/*
731 * This function handles the command response.
732 *
733 * After processing, the function cleans the command node and puts
734 * it back to the command free queue.
735 */
736int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
737{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700738 struct host_cmd_ds_command *resp;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700739 struct mwifiex_private *priv =
740 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
741 int ret = 0;
742 uint16_t orig_cmdresp_no;
743 uint16_t cmdresp_no;
744 uint16_t cmdresp_result;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700745 struct timeval tstamp;
746 unsigned long flags;
747
748 /* Now we got response from FW, cancel the command timer */
749 del_timer(&adapter->cmd_timer);
750
751 if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
752 resp = (struct host_cmd_ds_command *) adapter->upld_buf;
753 dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700754 le16_to_cpu(resp->command));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700755 return -1;
756 }
757
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700758 adapter->num_cmd_timeout = 0;
759
760 resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
761 if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
762 dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700763 le16_to_cpu(resp->command));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700764 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
765 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
766 adapter->curr_cmd = NULL;
767 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
768 return -1;
769 }
770
771 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
772 /* Copy original response back to response buffer */
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700773 struct mwifiex_ds_misc_cmd *hostcmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700774 uint16_t size = le16_to_cpu(resp->size);
775 dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
776 size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
777 if (adapter->curr_cmd->data_buf) {
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700778 hostcmd = adapter->curr_cmd->data_buf;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700779 hostcmd->len = size;
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700780 memcpy(hostcmd->cmd, resp, size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700781 }
782 }
783 orig_cmdresp_no = le16_to_cpu(resp->command);
784
785 /* Get BSS number and corresponding priv */
786 priv = mwifiex_get_priv_by_id(adapter,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700787 HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
788 HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700789 if (!priv)
790 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
791 /* Clear RET_BIT from HostCmd */
792 resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
793
794 cmdresp_no = le16_to_cpu(resp->command);
795 cmdresp_result = le16_to_cpu(resp->result);
796
797 /* Save the last command response to debug log */
798 adapter->dbg.last_cmd_resp_index =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700799 (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700800 adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700801 orig_cmdresp_no;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700802
803 do_gettimeofday(&tstamp);
804 dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
805 " len %d, seqno 0x%x\n",
806 tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
807 le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
808
809 if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
810 dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700811 if (adapter->curr_cmd->wait_q_enabled)
812 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700813
814 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
815 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
816 adapter->curr_cmd = NULL;
817 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
818 return -1;
819 }
820
821 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
822 adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700823 if ((cmdresp_result == HostCmd_RESULT_OK) &&
824 (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700825 ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
826 } else {
827 /* handle response */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700828 ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700829 }
830
831 /* Check init command response */
832 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
Amitkumar Karwara0f6d6c2012-02-24 21:36:05 -0800833 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700834 dev_err(adapter->dev, "%s: cmd %#x failed during "
835 "initialization\n", __func__, cmdresp_no);
836 mwifiex_init_fw_complete(adapter);
837 return -1;
838 } else if (adapter->last_init_cmd == cmdresp_no)
839 adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
840 }
841
842 if (adapter->curr_cmd) {
Amitkumar Karwara0f6d6c2012-02-24 21:36:05 -0800843 if (adapter->curr_cmd->wait_q_enabled)
844 adapter->cmd_wait_q.status = ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700845
846 /* Clean up and put current command back to cmd_free_q */
847 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
848
849 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
850 adapter->curr_cmd = NULL;
851 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
852 }
853
854 return ret;
855}
856
857/*
858 * This function handles the timeout of command sending.
859 *
860 * It will re-send the same command again.
861 */
862void
863mwifiex_cmd_timeout_func(unsigned long function_context)
864{
865 struct mwifiex_adapter *adapter =
866 (struct mwifiex_adapter *) function_context;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700867 struct cmd_ctrl_node *cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700868 struct timeval tstamp;
869
870 adapter->num_cmd_timeout++;
871 adapter->dbg.num_cmd_timeout++;
872 if (!adapter->curr_cmd) {
873 dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
874 return;
875 }
876 cmd_node = adapter->curr_cmd;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700877 if (cmd_node->wait_q_enabled)
878 adapter->cmd_wait_q.status = -ETIMEDOUT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700879
880 if (cmd_node) {
881 adapter->dbg.timeout_cmd_id =
882 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
883 adapter->dbg.timeout_cmd_act =
884 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
885 do_gettimeofday(&tstamp);
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700886 dev_err(adapter->dev,
887 "%s: Timeout cmd id (%lu.%lu) = %#x, act = %#x\n",
888 __func__, tstamp.tv_sec, tstamp.tv_usec,
889 adapter->dbg.timeout_cmd_id,
890 adapter->dbg.timeout_cmd_act);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700891
892 dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700893 adapter->dbg.num_tx_host_to_card_failure);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700894 dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700895 adapter->dbg.num_cmd_host_to_card_failure);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700896
897 dev_err(adapter->dev, "num_cmd_timeout = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700898 adapter->dbg.num_cmd_timeout);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700899 dev_err(adapter->dev, "num_tx_timeout = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700900 adapter->dbg.num_tx_timeout);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700901
902 dev_err(adapter->dev, "last_cmd_index = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700903 adapter->dbg.last_cmd_index);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700904 print_hex_dump_bytes("last_cmd_id: ", DUMP_PREFIX_OFFSET,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700905 adapter->dbg.last_cmd_id, DBG_CMD_NUM);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700906 print_hex_dump_bytes("last_cmd_act: ", DUMP_PREFIX_OFFSET,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700907 adapter->dbg.last_cmd_act, DBG_CMD_NUM);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700908
909 dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700910 adapter->dbg.last_cmd_resp_index);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700911 print_hex_dump_bytes("last_cmd_resp_id: ", DUMP_PREFIX_OFFSET,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700912 adapter->dbg.last_cmd_resp_id,
913 DBG_CMD_NUM);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700914
915 dev_err(adapter->dev, "last_event_index = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700916 adapter->dbg.last_event_index);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700917 print_hex_dump_bytes("last_event: ", DUMP_PREFIX_OFFSET,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700918 adapter->dbg.last_event, DBG_CMD_NUM);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700919
920 dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700921 adapter->data_sent, adapter->cmd_sent);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700922
923 dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700924 adapter->ps_mode, adapter->ps_state);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700925 }
926 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
927 mwifiex_init_fw_complete(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700928}
929
930/*
931 * This function cancels all the pending commands.
932 *
933 * The current command, all commands in command pending queue and all scan
934 * commands in scan pending queue are cancelled. All the completion callbacks
935 * are called with failure status to ensure cleanup.
936 */
937void
938mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
939{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700940 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700941 unsigned long flags;
942
943 /* Cancel current cmd */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700944 if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700945 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700946 adapter->curr_cmd->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700947 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700948 adapter->cmd_wait_q.status = -1;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700949 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700950 }
951 /* Cancel all pending command */
952 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
953 list_for_each_entry_safe(cmd_node, tmp_node,
954 &adapter->cmd_pending_q, list) {
955 list_del(&cmd_node->list);
956 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
957
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700958 if (cmd_node->wait_q_enabled) {
959 adapter->cmd_wait_q.status = -1;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700960 mwifiex_complete_cmd(adapter, cmd_node);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700961 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700962 }
963 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
964 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
965 }
966 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
967
968 /* Cancel all pending scan command */
969 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
970 list_for_each_entry_safe(cmd_node, tmp_node,
971 &adapter->scan_pending_q, list) {
972 list_del(&cmd_node->list);
973 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
974
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700975 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700976 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
977 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
978 }
979 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
980
981 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
982 adapter->scan_processing = false;
983 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
984}
985
986/*
987 * This function cancels all pending commands that matches with
988 * the given IOCTL request.
989 *
990 * Both the current command buffer and the pending command queue are
991 * searched for matching IOCTL request. The completion callback of
992 * the matched command is called with failure status to ensure cleanup.
993 * In case of scan commands, all pending commands in scan pending queue
994 * are cancelled.
995 */
996void
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700997mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700998{
999 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
1000 unsigned long cmd_flags;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001001 unsigned long scan_pending_q_flags;
1002 uint16_t cancel_scan_cmd = false;
1003
1004 if ((adapter->curr_cmd) &&
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001005 (adapter->curr_cmd->wait_q_enabled)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001006 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1007 cmd_node = adapter->curr_cmd;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001008 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001009 cmd_node->cmd_flag |= CMD_F_CANCELED;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001010 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
Yogesh Ashok Powar51e708c2011-12-13 20:43:16 -08001011 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
1012 adapter->curr_cmd = NULL;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001013 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001014 }
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001015
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001016 /* Cancel all pending scan command */
1017 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1018 scan_pending_q_flags);
1019 list_for_each_entry_safe(cmd_node, tmp_node,
1020 &adapter->scan_pending_q, list) {
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001021 list_del(&cmd_node->list);
1022 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1023 scan_pending_q_flags);
1024 cmd_node->wait_q_enabled = false;
1025 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
1026 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1027 scan_pending_q_flags);
1028 cancel_scan_cmd = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001029 }
1030 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1031 scan_pending_q_flags);
1032
1033 if (cancel_scan_cmd) {
1034 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1035 adapter->scan_processing = false;
1036 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1037 }
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001038 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001039}
1040
1041/*
1042 * This function sends the sleep confirm command to firmware, if
1043 * possible.
1044 *
1045 * The sleep confirm command cannot be issued if command response,
1046 * data response or event response is awaiting handling, or if we
1047 * are in the middle of sending a command, or expecting a command
1048 * response.
1049 */
1050void
1051mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
1052{
1053 if (!adapter->cmd_sent &&
1054 !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
1055 mwifiex_dnld_sleep_confirm_cmd(adapter);
1056 else
1057 dev_dbg(adapter->dev,
1058 "cmd: Delay Sleep Confirm (%s%s%s)\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001059 (adapter->cmd_sent) ? "D" : "",
1060 (adapter->curr_cmd) ? "C" : "",
1061 (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001062}
1063
1064/*
1065 * This function sends a Host Sleep activated event to applications.
1066 *
1067 * This event is generated by the driver, with a blank event body.
1068 */
1069void
1070mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
1071{
1072 if (activated) {
1073 if (priv->adapter->is_hs_configured) {
1074 priv->adapter->hs_activated = true;
1075 dev_dbg(priv->adapter->dev, "event: hs_activated\n");
1076 priv->adapter->hs_activate_wait_q_woken = true;
1077 wake_up_interruptible(
1078 &priv->adapter->hs_activate_wait_q);
1079 } else {
1080 dev_dbg(priv->adapter->dev, "event: HS not configured\n");
1081 }
1082 } else {
1083 dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
1084 priv->adapter->hs_activated = false;
1085 }
1086}
1087
1088/*
1089 * This function handles the command response of a Host Sleep configuration
1090 * command.
1091 *
1092 * Handling includes changing the header fields into CPU format
1093 * and setting the current host sleep activation status in driver.
1094 *
1095 * In case host sleep status change, the function generates an event to
1096 * notify the applications.
1097 */
1098int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
1099 struct host_cmd_ds_command *resp)
1100{
1101 struct mwifiex_adapter *adapter = priv->adapter;
1102 struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
1103 &resp->params.opt_hs_cfg;
1104 uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
1105
Amitkumar Karwara4186ea2012-06-20 19:58:37 -07001106 if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE) &&
1107 adapter->iface_type == MWIFIEX_SDIO) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001108 mwifiex_hs_activated_event(priv, true);
1109 return 0;
1110 } else {
1111 dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
1112 " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
1113 resp->result, conditions,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001114 phs_cfg->params.hs_config.gpio,
1115 phs_cfg->params.hs_config.gap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001116 }
1117 if (conditions != HOST_SLEEP_CFG_CANCEL) {
1118 adapter->is_hs_configured = true;
Amitkumar Karwara4186ea2012-06-20 19:58:37 -07001119 if (adapter->iface_type == MWIFIEX_USB ||
1120 adapter->iface_type == MWIFIEX_PCIE)
1121 mwifiex_hs_activated_event(priv, true);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001122 } else {
1123 adapter->is_hs_configured = false;
1124 if (adapter->hs_activated)
1125 mwifiex_hs_activated_event(priv, false);
1126 }
1127
1128 return 0;
1129}
1130
1131/*
1132 * This function wakes up the adapter and generates a Host Sleep
1133 * cancel event on receiving the power up interrupt.
1134 */
1135void
1136mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
1137{
1138 dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
1139 " since there is interrupt from the firmware\n", __func__);
1140
1141 adapter->if_ops.wakeup(adapter);
1142 adapter->hs_activated = false;
1143 adapter->is_hs_configured = false;
1144 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001145 MWIFIEX_BSS_ROLE_ANY),
1146 false);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001147}
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001148EXPORT_SYMBOL_GPL(mwifiex_process_hs_config);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001149
1150/*
1151 * This function handles the command response of a sleep confirm command.
1152 *
1153 * The function sets the card state to SLEEP if the response indicates success.
1154 */
1155void
1156mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
1157 u8 *pbuf, u32 upld_len)
1158{
1159 struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
1160 struct mwifiex_private *priv =
1161 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1162 uint16_t result = le16_to_cpu(cmd->result);
1163 uint16_t command = le16_to_cpu(cmd->command);
1164 uint16_t seq_num = le16_to_cpu(cmd->seq_num);
1165
1166 if (!upld_len) {
1167 dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
1168 return;
1169 }
1170
1171 /* Get BSS number and corresponding priv */
1172 priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
1173 HostCmd_GET_BSS_TYPE(seq_num));
1174 if (!priv)
1175 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1176
1177 /* Update sequence number */
1178 seq_num = HostCmd_GET_SEQ_NO(seq_num);
1179 /* Clear RET_BIT from HostCmd */
1180 command &= HostCmd_CMD_ID_MASK;
1181
1182 if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001183 dev_err(adapter->dev,
1184 "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
1185 __func__, command, result);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001186 return;
1187 }
1188
1189 if (result) {
1190 dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001191 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001192 adapter->pm_wakeup_card_req = false;
1193 adapter->ps_state = PS_STATE_AWAKE;
1194 return;
1195 }
1196 adapter->pm_wakeup_card_req = true;
1197 if (adapter->is_hs_configured)
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001198 mwifiex_hs_activated_event(mwifiex_get_priv
1199 (adapter, MWIFIEX_BSS_ROLE_ANY),
1200 true);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001201 adapter->ps_state = PS_STATE_SLEEP;
1202 cmd->command = cpu_to_le16(command);
1203 cmd->seq_num = cpu_to_le16(seq_num);
1204}
1205EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
1206
1207/*
1208 * This function prepares an enhanced power mode command.
1209 *
1210 * This function can be used to disable power save or to configure
1211 * power save with auto PS or STA PS or auto deep sleep.
1212 *
1213 * Preparation includes -
1214 * - Setting command ID, action and proper size
1215 * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
1216 * auto deep sleep TLV (as required)
1217 * - Ensuring correct endian-ness
1218 */
1219int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
1220 struct host_cmd_ds_command *cmd,
1221 u16 cmd_action, uint16_t ps_bitmap,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001222 struct mwifiex_ds_auto_ds *auto_ds)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001223{
1224 struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
1225 &cmd->params.psmode_enh;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001226 u8 *tlv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001227 u16 cmd_size = 0;
1228
1229 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
1230 if (cmd_action == DIS_AUTO_PS) {
1231 psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
1232 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001233 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001234 sizeof(psmode_enh->params.ps_bitmap));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001235 } else if (cmd_action == GET_PS) {
1236 psmode_enh->action = cpu_to_le16(GET_PS);
1237 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001238 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001239 sizeof(psmode_enh->params.ps_bitmap));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001240 } else if (cmd_action == EN_AUTO_PS) {
1241 psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001242 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1243 cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001244 sizeof(psmode_enh->params.ps_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001245 tlv = (u8 *) cmd + cmd_size;
1246 if (ps_bitmap & BITMAP_STA_PS) {
1247 struct mwifiex_adapter *adapter = priv->adapter;
1248 struct mwifiex_ie_types_ps_param *ps_tlv =
1249 (struct mwifiex_ie_types_ps_param *) tlv;
1250 struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
1251 ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
1252 ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
1253 sizeof(struct mwifiex_ie_types_header));
1254 cmd_size += sizeof(*ps_tlv);
1255 tlv += sizeof(*ps_tlv);
1256 dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
1257 ps_mode->null_pkt_interval =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001258 cpu_to_le16(adapter->null_pkt_interval);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001259 ps_mode->multiple_dtims =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001260 cpu_to_le16(adapter->multiple_dtim);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001261 ps_mode->bcn_miss_timeout =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001262 cpu_to_le16(adapter->bcn_miss_time_out);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001263 ps_mode->local_listen_interval =
1264 cpu_to_le16(adapter->local_listen_interval);
1265 ps_mode->adhoc_wake_period =
1266 cpu_to_le16(adapter->adhoc_awake_period);
1267 ps_mode->delay_to_ps =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001268 cpu_to_le16(adapter->delay_to_ps);
1269 ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001270
1271 }
1272 if (ps_bitmap & BITMAP_AUTO_DS) {
Marc Yang2b06bdb2011-03-30 18:12:44 -07001273 struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001274 (struct mwifiex_ie_types_auto_ds_param *) tlv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001275 u16 idletime = 0;
Marc Yang2b06bdb2011-03-30 18:12:44 -07001276
1277 auto_ds_tlv->header.type =
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001278 cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001279 auto_ds_tlv->header.len =
1280 cpu_to_le16(sizeof(*auto_ds_tlv) -
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001281 sizeof(struct mwifiex_ie_types_header));
Marc Yang2b06bdb2011-03-30 18:12:44 -07001282 cmd_size += sizeof(*auto_ds_tlv);
1283 tlv += sizeof(*auto_ds_tlv);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001284 if (auto_ds)
1285 idletime = auto_ds->idle_time;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001286 dev_dbg(priv->adapter->dev,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001287 "cmd: PS Command: Enter Auto Deep Sleep\n");
Marc Yang2b06bdb2011-03-30 18:12:44 -07001288 auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001289 }
1290 cmd->size = cpu_to_le16(cmd_size);
1291 }
1292 return 0;
1293}
1294
1295/*
1296 * This function handles the command response of an enhanced power mode
1297 * command.
1298 *
1299 * Handling includes changing the header fields into CPU format
1300 * and setting the current enhanced power mode in driver.
1301 */
1302int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
1303 struct host_cmd_ds_command *resp,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001304 struct mwifiex_ds_pm_cfg *pm_cfg)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001305{
1306 struct mwifiex_adapter *adapter = priv->adapter;
1307 struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
1308 &resp->params.psmode_enh;
1309 uint16_t action = le16_to_cpu(ps_mode->action);
1310 uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
1311 uint16_t auto_ps_bitmap =
Marc Yang2b06bdb2011-03-30 18:12:44 -07001312 le16_to_cpu(ps_mode->params.ps_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001313
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001314 dev_dbg(adapter->dev,
1315 "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
1316 __func__, resp->result, action);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001317 if (action == EN_AUTO_PS) {
1318 if (auto_ps_bitmap & BITMAP_AUTO_DS) {
1319 dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
1320 priv->adapter->is_deep_sleep = true;
1321 }
1322 if (auto_ps_bitmap & BITMAP_STA_PS) {
1323 dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
1324 if (adapter->sleep_period.period)
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001325 dev_dbg(adapter->dev,
1326 "cmd: set to uapsd/pps mode\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001327 }
1328 } else if (action == DIS_AUTO_PS) {
1329 if (ps_bitmap & BITMAP_AUTO_DS) {
1330 priv->adapter->is_deep_sleep = false;
1331 dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
1332 }
1333 if (ps_bitmap & BITMAP_STA_PS) {
1334 dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
1335 if (adapter->sleep_period.period) {
1336 adapter->delay_null_pkt = false;
1337 adapter->tx_lock_flag = false;
1338 adapter->pps_uapsd_mode = false;
1339 }
1340 }
1341 } else if (action == GET_PS) {
Marc Yang2b06bdb2011-03-30 18:12:44 -07001342 if (ps_bitmap & BITMAP_STA_PS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001343 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1344 else
1345 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
1346
1347 dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
1348
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001349 if (pm_cfg) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001350 /* This section is for get power save mode */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001351 if (ps_bitmap & BITMAP_STA_PS)
1352 pm_cfg->param.ps_mode = 1;
1353 else
1354 pm_cfg->param.ps_mode = 0;
1355 }
1356 }
1357 return 0;
1358}
1359
1360/*
1361 * This function prepares command to get hardware specifications.
1362 *
1363 * Preparation includes -
1364 * - Setting command ID, action and proper size
1365 * - Setting permanent address parameter
1366 * - Ensuring correct endian-ness
1367 */
1368int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
1369 struct host_cmd_ds_command *cmd)
1370{
1371 struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
1372
1373 cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
1374 cmd->size =
1375 cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
1376 memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
1377
1378 return 0;
1379}
1380
1381/*
1382 * This function handles the command response of get hardware
1383 * specifications.
1384 *
1385 * Handling includes changing the header fields into CPU format
1386 * and saving/updating the following parameters in driver -
1387 * - Firmware capability information
1388 * - Firmware band settings
1389 * - Ad-hoc start band and channel
1390 * - Ad-hoc 11n activation status
1391 * - Firmware release number
1392 * - Number of antennas
1393 * - Hardware address
1394 * - Hardware interface version
1395 * - Firmware version
1396 * - Region code
1397 * - 11n capabilities
1398 * - MCS support fields
1399 * - MP end port
1400 */
1401int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
1402 struct host_cmd_ds_command *resp)
1403{
1404 struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
1405 struct mwifiex_adapter *adapter = priv->adapter;
1406 int i;
1407
1408 adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
1409
1410 if (IS_SUPPORT_MULTI_BANDS(adapter))
1411 adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
1412 else
1413 adapter->fw_bands = BAND_B;
1414
1415 adapter->config_bands = adapter->fw_bands;
1416
1417 if (adapter->fw_bands & BAND_A) {
1418 if (adapter->fw_bands & BAND_GN) {
1419 adapter->config_bands |= BAND_AN;
1420 adapter->fw_bands |= BAND_AN;
1421 }
1422 if (adapter->fw_bands & BAND_AN) {
1423 adapter->adhoc_start_band = BAND_A | BAND_AN;
1424 adapter->adhoc_11n_enabled = true;
1425 } else {
1426 adapter->adhoc_start_band = BAND_A;
1427 }
1428 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
1429 } else if (adapter->fw_bands & BAND_GN) {
1430 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
1431 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1432 adapter->adhoc_11n_enabled = true;
1433 } else if (adapter->fw_bands & BAND_G) {
1434 adapter->adhoc_start_band = BAND_G | BAND_B;
1435 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1436 } else if (adapter->fw_bands & BAND_B) {
1437 adapter->adhoc_start_band = BAND_B;
1438 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1439 }
1440
1441 adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
1442 adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
1443
1444 dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001445 adapter->fw_release_number);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001446 dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001447 hw_spec->permanent_addr);
1448 dev_dbg(adapter->dev,
1449 "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001450 le16_to_cpu(hw_spec->hw_if_version),
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001451 le16_to_cpu(hw_spec->version));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001452
1453 if (priv->curr_addr[0] == 0xff)
1454 memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
1455
1456 adapter->region_code = le16_to_cpu(hw_spec->region_code);
1457
1458 for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
1459 /* Use the region code to search for the index */
1460 if (adapter->region_code == region_code_index[i])
1461 break;
1462
1463 /* If it's unidentified region code, use the default (USA) */
1464 if (i >= MWIFIEX_MAX_REGION_CODE) {
1465 adapter->region_code = 0x10;
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001466 dev_dbg(adapter->dev,
1467 "cmd: unknown region code, use default (USA)\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001468 }
1469
1470 adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001471 adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001472
1473 if (adapter->if_ops.update_mp_end_port)
1474 adapter->if_ops.update_mp_end_port(adapter,
1475 le16_to_cpu(hw_spec->mp_end_port));
1476
1477 return 0;
1478}