blob: 565527aee0ea3f73caa832f336c0ded06a3b22d9 [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
Stone Piaoda251862012-08-22 20:26:31 -0700173 /* Adjust skb length */
174 if (cmd_node->cmd_skb->len > cmd_size)
175 /*
176 * cmd_size is less than sizeof(struct host_cmd_ds_command).
177 * Trim off the unused portion.
178 */
179 skb_trim(cmd_node->cmd_skb, cmd_size);
180 else if (cmd_node->cmd_skb->len < cmd_size)
181 /*
182 * cmd_size is larger than sizeof(struct host_cmd_ds_command)
183 * because we have appended custom IE TLV. Increase skb length
184 * accordingly.
185 */
186 skb_put(cmd_node->cmd_skb, cmd_size - cmd_node->cmd_skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700187
188 do_gettimeofday(&tstamp);
189 dev_dbg(adapter->dev, "cmd: DNLD_CMD: (%lu.%lu): %#x, act %#x, len %d,"
190 " seqno %#x\n",
191 tstamp.tv_sec, tstamp.tv_usec, cmd_code,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700192 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN)), cmd_size,
193 le16_to_cpu(host_cmd->seq_num));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700194
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700195 if (adapter->iface_type == MWIFIEX_USB) {
196 tmp = cpu_to_le32(MWIFIEX_USB_TYPE_CMD);
197 skb_push(cmd_node->cmd_skb, MWIFIEX_TYPE_LEN);
198 memcpy(cmd_node->cmd_skb->data, &tmp, MWIFIEX_TYPE_LEN);
199 adapter->cmd_sent = true;
200 ret = adapter->if_ops.host_to_card(adapter,
201 MWIFIEX_USB_EP_CMD_EVENT,
202 cmd_node->cmd_skb, NULL);
203 skb_pull(cmd_node->cmd_skb, MWIFIEX_TYPE_LEN);
204 if (ret == -EBUSY)
205 cmd_node->cmd_skb = NULL;
206 } else {
207 skb_push(cmd_node->cmd_skb, INTF_HEADER_LEN);
208 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
209 cmd_node->cmd_skb, NULL);
210 skb_pull(cmd_node->cmd_skb, INTF_HEADER_LEN);
211 }
Bing Zhao18bf9652011-04-06 16:46:55 -0700212
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700213 if (ret == -1) {
214 dev_err(adapter->dev, "DNLD_CMD: host to card failed\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700215 if (adapter->iface_type == MWIFIEX_USB)
216 adapter->cmd_sent = false;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700217 if (cmd_node->wait_q_enabled)
218 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700219 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
220
221 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
222 adapter->curr_cmd = NULL;
223 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
224
225 adapter->dbg.num_cmd_host_to_card_failure++;
226 return -1;
227 }
228
229 /* Save the last command id and action to debug log */
230 adapter->dbg.last_cmd_index =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700231 (adapter->dbg.last_cmd_index + 1) % DBG_CMD_NUM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700232 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index] = cmd_code;
233 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index] =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700234 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700235
236 /* Clear BSS_NO_BITS from HostCmd */
237 cmd_code &= HostCmd_CMD_ID_MASK;
238
239 /* Setup the timer after transmit command */
240 mod_timer(&adapter->cmd_timer,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700241 jiffies + (MWIFIEX_TIMER_10S * HZ) / 1000);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700242
243 return 0;
244}
245
246/*
247 * This function downloads a sleep confirm command to the firmware.
248 *
249 * The function performs sanity tests, sets the command sequence
250 * number and size, converts the header fields to CPU format before
251 * sending.
252 *
253 * No responses are needed for sleep confirm command.
254 */
255static int mwifiex_dnld_sleep_confirm_cmd(struct mwifiex_adapter *adapter)
256{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700257 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700258 struct mwifiex_private *priv;
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700259 struct mwifiex_opt_sleep_confirm *sleep_cfm_buf =
260 (struct mwifiex_opt_sleep_confirm *)
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700261 adapter->sleep_cfm->data;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700262 struct sk_buff *sleep_cfm_tmp;
263 __le32 tmp;
264
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700265 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
266
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700267 sleep_cfm_buf->seq_num =
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700268 cpu_to_le16((HostCmd_SET_SEQ_NO_BSS_INFO
269 (adapter->seq_num, priv->bss_num,
270 priv->bss_type)));
271 adapter->seq_num++;
272
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700273 if (adapter->iface_type == MWIFIEX_USB) {
274 sleep_cfm_tmp =
275 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
276 + MWIFIEX_TYPE_LEN);
277 skb_put(sleep_cfm_tmp, sizeof(struct mwifiex_opt_sleep_confirm)
278 + MWIFIEX_TYPE_LEN);
279 tmp = cpu_to_le32(MWIFIEX_USB_TYPE_CMD);
280 memcpy(sleep_cfm_tmp->data, &tmp, MWIFIEX_TYPE_LEN);
281 memcpy(sleep_cfm_tmp->data + MWIFIEX_TYPE_LEN,
282 adapter->sleep_cfm->data,
283 sizeof(struct mwifiex_opt_sleep_confirm));
284 ret = adapter->if_ops.host_to_card(adapter,
285 MWIFIEX_USB_EP_CMD_EVENT,
286 sleep_cfm_tmp, NULL);
287 if (ret != -EBUSY)
288 dev_kfree_skb_any(sleep_cfm_tmp);
289 } else {
290 skb_push(adapter->sleep_cfm, INTF_HEADER_LEN);
291 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
292 adapter->sleep_cfm, NULL);
293 skb_pull(adapter->sleep_cfm, INTF_HEADER_LEN);
294 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700295
296 if (ret == -1) {
297 dev_err(adapter->dev, "SLEEP_CFM: failed\n");
298 adapter->dbg.num_cmd_sleep_cfm_host_to_card_failure++;
299 return -1;
300 }
301 if (GET_BSS_ROLE(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY))
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700302 == MWIFIEX_BSS_ROLE_STA) {
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700303 if (!sleep_cfm_buf->resp_ctrl)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700304 /* Response is not needed for sleep
305 confirm command */
306 adapter->ps_state = PS_STATE_SLEEP;
307 else
308 adapter->ps_state = PS_STATE_SLEEP_CFM;
309
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700310 if (!sleep_cfm_buf->resp_ctrl &&
311 (adapter->is_hs_configured &&
312 !adapter->sleep_period.period)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700313 adapter->pm_wakeup_card_req = true;
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700314 mwifiex_hs_activated_event(mwifiex_get_priv
315 (adapter, MWIFIEX_BSS_ROLE_STA), true);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700316 }
317 }
318
319 return ret;
320}
321
322/*
323 * This function allocates the command buffers and links them to
324 * the command free queue.
325 *
326 * The driver uses a pre allocated number of command buffers, which
327 * are created at driver initializations and freed at driver cleanup.
328 * Every command needs to obtain a command buffer from this pool before
329 * it can be issued. The command free queue lists the command buffers
330 * currently free to use, while the command pending queue lists the
331 * command buffers already in use and awaiting handling. Command buffers
332 * are returned to the free queue after use.
333 */
334int mwifiex_alloc_cmd_buffer(struct mwifiex_adapter *adapter)
335{
336 struct cmd_ctrl_node *cmd_array;
337 u32 buf_size;
338 u32 i;
339
340 /* Allocate and initialize struct cmd_ctrl_node */
341 buf_size = sizeof(struct cmd_ctrl_node) * MWIFIEX_NUM_OF_CMD_BUFFER;
342 cmd_array = kzalloc(buf_size, GFP_KERNEL);
343 if (!cmd_array) {
344 dev_err(adapter->dev, "%s: failed to alloc cmd_array\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700345 __func__);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200346 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700347 }
348
349 adapter->cmd_pool = cmd_array;
350 memset(adapter->cmd_pool, 0, buf_size);
351
352 /* Allocate and initialize command buffers */
353 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
354 cmd_array[i].skb = dev_alloc_skb(MWIFIEX_SIZE_OF_CMD_BUFFER);
355 if (!cmd_array[i].skb) {
356 dev_err(adapter->dev, "ALLOC_CMD_BUF: out of memory\n");
357 return -1;
358 }
359 }
360
361 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++)
362 mwifiex_insert_cmd_to_free_q(adapter, &cmd_array[i]);
363
364 return 0;
365}
366
367/*
368 * This function frees the command buffers.
369 *
370 * The function calls the completion callback for all the command
371 * buffers that still have response buffers associated with them.
372 */
373int mwifiex_free_cmd_buffer(struct mwifiex_adapter *adapter)
374{
375 struct cmd_ctrl_node *cmd_array;
376 u32 i;
377
378 /* Need to check if cmd pool is allocated or not */
379 if (!adapter->cmd_pool) {
380 dev_dbg(adapter->dev, "info: FREE_CMD_BUF: cmd_pool is null\n");
381 return 0;
382 }
383
384 cmd_array = adapter->cmd_pool;
385
386 /* Release shared memory buffers */
387 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
388 if (cmd_array[i].skb) {
389 dev_dbg(adapter->dev, "cmd: free cmd buffer %d\n", i);
390 dev_kfree_skb_any(cmd_array[i].skb);
391 }
392 if (!cmd_array[i].resp_skb)
393 continue;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700394
395 if (adapter->iface_type == MWIFIEX_USB)
396 adapter->if_ops.cmdrsp_complete(adapter,
397 cmd_array[i].resp_skb);
398 else
399 dev_kfree_skb_any(cmd_array[i].resp_skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700400 }
401 /* Release struct cmd_ctrl_node */
402 if (adapter->cmd_pool) {
403 dev_dbg(adapter->dev, "cmd: free cmd pool\n");
404 kfree(adapter->cmd_pool);
405 adapter->cmd_pool = NULL;
406 }
407
408 return 0;
409}
410
411/*
412 * This function handles events generated by firmware.
413 *
414 * Event body of events received from firmware are not used (though they are
415 * saved), only the event ID is used. Some events are re-invoked by
416 * the driver, with a new event body.
417 *
418 * After processing, the function calls the completion callback
419 * for cleanup.
420 */
421int mwifiex_process_event(struct mwifiex_adapter *adapter)
422{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700423 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700424 struct mwifiex_private *priv =
425 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
426 struct sk_buff *skb = adapter->event_skb;
427 u32 eventcause = adapter->event_cause;
428 struct timeval tstamp;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700429 struct mwifiex_rxinfo *rx_info;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700430
431 /* Save the last event to debug log */
432 adapter->dbg.last_event_index =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700433 (adapter->dbg.last_event_index + 1) % DBG_CMD_NUM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700434 adapter->dbg.last_event[adapter->dbg.last_event_index] =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700435 (u16) eventcause;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700436
437 /* Get BSS number and corresponding priv */
438 priv = mwifiex_get_priv_by_id(adapter, EVENT_GET_BSS_NUM(eventcause),
439 EVENT_GET_BSS_TYPE(eventcause));
440 if (!priv)
441 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
442 /* Clear BSS_NO_BITS from event */
443 eventcause &= EVENT_ID_MASK;
444 adapter->event_cause = eventcause;
445
446 if (skb) {
447 rx_info = MWIFIEX_SKB_RXCB(skb);
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800448 rx_info->bss_num = priv->bss_num;
449 rx_info->bss_type = priv->bss_type;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700450 }
451
452 if (eventcause != EVENT_PS_SLEEP && eventcause != EVENT_PS_AWAKE) {
453 do_gettimeofday(&tstamp);
454 dev_dbg(adapter->dev, "event: %lu.%lu: cause: %#x\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700455 tstamp.tv_sec, tstamp.tv_usec, eventcause);
Avinash Patil03785382012-05-08 18:30:14 -0700456 } else {
457 /* Handle PS_SLEEP/AWAKE events on STA */
458 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
459 if (!priv)
460 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700461 }
462
463 ret = mwifiex_process_sta_event(priv);
464
465 adapter->event_cause = 0;
466 adapter->event_skb = NULL;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700467 adapter->if_ops.event_complete(adapter, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700468
469 return ret;
470}
471
472/*
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700473 * This function is used to send synchronous command to the firmware.
474 *
475 * it allocates a wait queue for the command and wait for the command
476 * response.
477 */
478int mwifiex_send_cmd_sync(struct mwifiex_private *priv, uint16_t cmd_no,
479 u16 cmd_action, u32 cmd_oid, void *data_buf)
480{
481 int ret = 0;
482 struct mwifiex_adapter *adapter = priv->adapter;
483
484 adapter->cmd_wait_q_required = true;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700485
486 ret = mwifiex_send_cmd_async(priv, cmd_no, cmd_action, cmd_oid,
487 data_buf);
488 if (!ret)
489 ret = mwifiex_wait_queue_complete(adapter);
490
491 return ret;
492}
493
494
495/*
496 * This function prepares a command and asynchronously send it to the firmware.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700497 *
498 * Preparation includes -
499 * - Sanity tests to make sure the card is still present or the FW
500 * is not reset
501 * - Getting a new command node from the command free queue
502 * - Initializing the command node for default parameters
503 * - Fill up the non-default parameters and buffer pointers
504 * - Add the command to pending queue
505 */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700506int mwifiex_send_cmd_async(struct mwifiex_private *priv, uint16_t cmd_no,
507 u16 cmd_action, u32 cmd_oid, void *data_buf)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700508{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700509 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700510 struct mwifiex_adapter *adapter = priv->adapter;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700511 struct cmd_ctrl_node *cmd_node;
512 struct host_cmd_ds_command *cmd_ptr;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700513
514 if (!adapter) {
515 pr_err("PREP_CMD: adapter is NULL\n");
516 return -1;
517 }
518
519 if (adapter->is_suspended) {
520 dev_err(adapter->dev, "PREP_CMD: device in suspended state\n");
521 return -1;
522 }
523
524 if (adapter->surprise_removed) {
525 dev_err(adapter->dev, "PREP_CMD: card is removed\n");
526 return -1;
527 }
528
529 if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET) {
530 if (cmd_no != HostCmd_CMD_FUNC_INIT) {
531 dev_err(adapter->dev, "PREP_CMD: FW in reset state\n");
532 return -1;
533 }
534 }
535
536 /* Get a new command node */
537 cmd_node = mwifiex_get_cmd_node(adapter);
538
539 if (!cmd_node) {
540 dev_err(adapter->dev, "PREP_CMD: no free cmd node\n");
541 return -1;
542 }
543
544 /* Initialize the command node */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700545 mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700546
547 if (!cmd_node->cmd_skb) {
548 dev_err(adapter->dev, "PREP_CMD: no free cmd buf\n");
549 return -1;
550 }
551
552 memset(skb_put(cmd_node->cmd_skb, sizeof(struct host_cmd_ds_command)),
553 0, sizeof(struct host_cmd_ds_command));
554
555 cmd_ptr = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
556 cmd_ptr->command = cpu_to_le16(cmd_no);
557 cmd_ptr->result = 0;
558
559 /* Prepare command */
560 if (cmd_no) {
Avinash Patil40d07032012-05-08 18:30:19 -0700561 switch (cmd_no) {
562 case HostCmd_CMD_UAP_SYS_CONFIG:
563 case HostCmd_CMD_UAP_BSS_START:
564 case HostCmd_CMD_UAP_BSS_STOP:
565 ret = mwifiex_uap_prepare_cmd(priv, cmd_no, cmd_action,
566 cmd_oid, data_buf,
567 cmd_ptr);
568 break;
569 default:
570 ret = mwifiex_sta_prepare_cmd(priv, cmd_no, cmd_action,
571 cmd_oid, data_buf,
572 cmd_ptr);
573 break;
574 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700575 } else {
576 ret = mwifiex_cmd_host_cmd(priv, cmd_ptr, data_buf);
577 cmd_node->cmd_flag |= CMD_F_HOSTCMD;
578 }
579
580 /* Return error, since the command preparation failed */
581 if (ret) {
582 dev_err(adapter->dev, "PREP_CMD: cmd %#x preparation failed\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700583 cmd_no);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700584 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
585 return -1;
586 }
587
588 /* Send command */
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700589 if (cmd_no == HostCmd_CMD_802_11_SCAN) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700590 mwifiex_queue_scan_cmd(priv, cmd_node);
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700591 } else {
592 adapter->cmd_queued = cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700593 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
Amitkumar Karwar1a1fb972012-06-27 19:57:56 -0700594 queue_work(adapter->workqueue, &adapter->main_work);
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700595 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700596
597 return ret;
598}
599
600/*
601 * This function returns a command to the command free queue.
602 *
603 * The function also calls the completion callback if required, before
604 * cleaning the command node and re-inserting it into the free queue.
605 */
606void
607mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
608 struct cmd_ctrl_node *cmd_node)
609{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700610 unsigned long flags;
611
Yogesh Ashok Powar19a89862011-04-13 17:27:07 -0700612 if (!cmd_node)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700613 return;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700614
615 if (cmd_node->wait_q_enabled)
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700616 mwifiex_complete_cmd(adapter, cmd_node);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700617 /* Clean the node */
618 mwifiex_clean_cmd_node(adapter, cmd_node);
619
620 /* Insert node into cmd_free_q */
621 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
622 list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
623 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700624}
625
626/*
627 * This function queues a command to the command pending queue.
628 *
629 * This in effect adds the command to the command list to be executed.
630 * Exit PS command is handled specially, by placing it always to the
631 * front of the command queue.
632 */
633void
634mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
635 struct cmd_ctrl_node *cmd_node, u32 add_tail)
636{
637 struct host_cmd_ds_command *host_cmd = NULL;
638 u16 command;
639 unsigned long flags;
640
641 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
642 if (!host_cmd) {
643 dev_err(adapter->dev, "QUEUE_CMD: host_cmd is NULL\n");
644 return;
645 }
646
647 command = le16_to_cpu(host_cmd->command);
648
649 /* Exit_PS command needs to be queued in the header always. */
650 if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
651 struct host_cmd_ds_802_11_ps_mode_enh *pm =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700652 &host_cmd->params.psmode_enh;
653 if ((le16_to_cpu(pm->action) == DIS_PS) ||
654 (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700655 if (adapter->ps_state != PS_STATE_AWAKE)
656 add_tail = false;
657 }
658 }
659
660 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
661 if (add_tail)
662 list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
663 else
664 list_add(&cmd_node->list, &adapter->cmd_pending_q);
665 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
666
667 dev_dbg(adapter->dev, "cmd: QUEUE_CMD: cmd=%#x is queued\n", command);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700668}
669
670/*
671 * This function executes the next command in command pending queue.
672 *
673 * This function will fail if a command is already in processing stage,
674 * otherwise it will dequeue the first command from the command pending
675 * queue and send to the firmware.
676 *
677 * If the device is currently in host sleep mode, any commands, except the
678 * host sleep configuration command will de-activate the host sleep. For PS
679 * mode, the function will put the firmware back to sleep if applicable.
680 */
681int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
682{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700683 struct mwifiex_private *priv;
684 struct cmd_ctrl_node *cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700685 int ret = 0;
686 struct host_cmd_ds_command *host_cmd;
687 unsigned long cmd_flags;
688 unsigned long cmd_pending_q_flags;
689
690 /* Check if already in processing */
691 if (adapter->curr_cmd) {
692 dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
693 return -1;
694 }
695
696 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
697 /* Check if any command is pending */
698 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
699 if (list_empty(&adapter->cmd_pending_q)) {
700 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
701 cmd_pending_q_flags);
702 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
703 return 0;
704 }
705 cmd_node = list_first_entry(&adapter->cmd_pending_q,
706 struct cmd_ctrl_node, list);
707 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
708 cmd_pending_q_flags);
709
710 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
711 priv = cmd_node->priv;
712
713 if (adapter->ps_state != PS_STATE_AWAKE) {
714 dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
715 " this should not happen\n", __func__);
716 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
717 return ret;
718 }
719
720 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
721 list_del(&cmd_node->list);
722 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
723 cmd_pending_q_flags);
724
725 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
726 ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
727 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
728 /* Any command sent to the firmware when host is in sleep
729 * mode should de-configure host sleep. We should skip the
730 * host sleep configuration command itself though
731 */
732 if (priv && (host_cmd->command !=
733 cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
734 if (adapter->hs_activated) {
735 adapter->is_hs_configured = false;
736 mwifiex_hs_activated_event(priv, false);
737 }
738 }
739
740 return ret;
741}
742
743/*
744 * This function handles the command response.
745 *
746 * After processing, the function cleans the command node and puts
747 * it back to the command free queue.
748 */
749int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
750{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700751 struct host_cmd_ds_command *resp;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700752 struct mwifiex_private *priv =
753 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
754 int ret = 0;
755 uint16_t orig_cmdresp_no;
756 uint16_t cmdresp_no;
757 uint16_t cmdresp_result;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700758 struct timeval tstamp;
759 unsigned long flags;
760
761 /* Now we got response from FW, cancel the command timer */
762 del_timer(&adapter->cmd_timer);
763
764 if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
765 resp = (struct host_cmd_ds_command *) adapter->upld_buf;
766 dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700767 le16_to_cpu(resp->command));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700768 return -1;
769 }
770
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700771 adapter->num_cmd_timeout = 0;
772
773 resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
774 if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
775 dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700776 le16_to_cpu(resp->command));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700777 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
778 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
779 adapter->curr_cmd = NULL;
780 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
781 return -1;
782 }
783
784 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
785 /* Copy original response back to response buffer */
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700786 struct mwifiex_ds_misc_cmd *hostcmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700787 uint16_t size = le16_to_cpu(resp->size);
788 dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
789 size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
790 if (adapter->curr_cmd->data_buf) {
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700791 hostcmd = adapter->curr_cmd->data_buf;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700792 hostcmd->len = size;
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700793 memcpy(hostcmd->cmd, resp, size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700794 }
795 }
796 orig_cmdresp_no = le16_to_cpu(resp->command);
797
798 /* Get BSS number and corresponding priv */
799 priv = mwifiex_get_priv_by_id(adapter,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700800 HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
801 HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700802 if (!priv)
803 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
804 /* Clear RET_BIT from HostCmd */
805 resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
806
807 cmdresp_no = le16_to_cpu(resp->command);
808 cmdresp_result = le16_to_cpu(resp->result);
809
810 /* Save the last command response to debug log */
811 adapter->dbg.last_cmd_resp_index =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700812 (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700813 adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700814 orig_cmdresp_no;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700815
816 do_gettimeofday(&tstamp);
817 dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
818 " len %d, seqno 0x%x\n",
819 tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
820 le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
821
822 if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
823 dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700824 if (adapter->curr_cmd->wait_q_enabled)
825 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700826
827 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
828 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
829 adapter->curr_cmd = NULL;
830 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
831 return -1;
832 }
833
834 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
835 adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700836 if ((cmdresp_result == HostCmd_RESULT_OK) &&
837 (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700838 ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
839 } else {
840 /* handle response */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700841 ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700842 }
843
844 /* Check init command response */
845 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
Amitkumar Karwara0f6d6c2012-02-24 21:36:05 -0800846 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700847 dev_err(adapter->dev, "%s: cmd %#x failed during "
848 "initialization\n", __func__, cmdresp_no);
849 mwifiex_init_fw_complete(adapter);
850 return -1;
851 } else if (adapter->last_init_cmd == cmdresp_no)
852 adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
853 }
854
855 if (adapter->curr_cmd) {
Amitkumar Karwara0f6d6c2012-02-24 21:36:05 -0800856 if (adapter->curr_cmd->wait_q_enabled)
857 adapter->cmd_wait_q.status = ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700858
859 /* Clean up and put current command back to cmd_free_q */
860 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
861
862 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
863 adapter->curr_cmd = NULL;
864 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
865 }
866
867 return ret;
868}
869
870/*
871 * This function handles the timeout of command sending.
872 *
873 * It will re-send the same command again.
874 */
875void
876mwifiex_cmd_timeout_func(unsigned long function_context)
877{
878 struct mwifiex_adapter *adapter =
879 (struct mwifiex_adapter *) function_context;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700880 struct cmd_ctrl_node *cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700881 struct timeval tstamp;
882
883 adapter->num_cmd_timeout++;
884 adapter->dbg.num_cmd_timeout++;
885 if (!adapter->curr_cmd) {
886 dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
887 return;
888 }
889 cmd_node = adapter->curr_cmd;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700890 if (cmd_node->wait_q_enabled)
891 adapter->cmd_wait_q.status = -ETIMEDOUT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700892
893 if (cmd_node) {
894 adapter->dbg.timeout_cmd_id =
895 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
896 adapter->dbg.timeout_cmd_act =
897 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
898 do_gettimeofday(&tstamp);
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700899 dev_err(adapter->dev,
900 "%s: Timeout cmd id (%lu.%lu) = %#x, act = %#x\n",
901 __func__, tstamp.tv_sec, tstamp.tv_usec,
902 adapter->dbg.timeout_cmd_id,
903 adapter->dbg.timeout_cmd_act);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700904
905 dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700906 adapter->dbg.num_tx_host_to_card_failure);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700907 dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700908 adapter->dbg.num_cmd_host_to_card_failure);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700909
910 dev_err(adapter->dev, "num_cmd_timeout = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700911 adapter->dbg.num_cmd_timeout);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700912 dev_err(adapter->dev, "num_tx_timeout = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700913 adapter->dbg.num_tx_timeout);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700914
915 dev_err(adapter->dev, "last_cmd_index = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700916 adapter->dbg.last_cmd_index);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700917 print_hex_dump_bytes("last_cmd_id: ", DUMP_PREFIX_OFFSET,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700918 adapter->dbg.last_cmd_id, DBG_CMD_NUM);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700919 print_hex_dump_bytes("last_cmd_act: ", DUMP_PREFIX_OFFSET,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700920 adapter->dbg.last_cmd_act, DBG_CMD_NUM);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700921
922 dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700923 adapter->dbg.last_cmd_resp_index);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700924 print_hex_dump_bytes("last_cmd_resp_id: ", DUMP_PREFIX_OFFSET,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700925 adapter->dbg.last_cmd_resp_id,
926 DBG_CMD_NUM);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700927
928 dev_err(adapter->dev, "last_event_index = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700929 adapter->dbg.last_event_index);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700930 print_hex_dump_bytes("last_event: ", DUMP_PREFIX_OFFSET,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700931 adapter->dbg.last_event, DBG_CMD_NUM);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700932
933 dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700934 adapter->data_sent, adapter->cmd_sent);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700935
936 dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700937 adapter->ps_mode, adapter->ps_state);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700938 }
939 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
940 mwifiex_init_fw_complete(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700941}
942
943/*
944 * This function cancels all the pending commands.
945 *
946 * The current command, all commands in command pending queue and all scan
947 * commands in scan pending queue are cancelled. All the completion callbacks
948 * are called with failure status to ensure cleanup.
949 */
950void
951mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
952{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700953 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700954 unsigned long flags;
955
956 /* Cancel current cmd */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700957 if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700958 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700959 adapter->curr_cmd->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700960 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700961 adapter->cmd_wait_q.status = -1;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700962 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700963 }
964 /* Cancel all pending command */
965 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
966 list_for_each_entry_safe(cmd_node, tmp_node,
967 &adapter->cmd_pending_q, list) {
968 list_del(&cmd_node->list);
969 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
970
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700971 if (cmd_node->wait_q_enabled) {
972 adapter->cmd_wait_q.status = -1;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700973 mwifiex_complete_cmd(adapter, cmd_node);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700974 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700975 }
976 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
977 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
978 }
979 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
980
981 /* Cancel all pending scan command */
982 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
983 list_for_each_entry_safe(cmd_node, tmp_node,
984 &adapter->scan_pending_q, list) {
985 list_del(&cmd_node->list);
986 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
987
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700988 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700989 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
990 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
991 }
992 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
993
994 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
995 adapter->scan_processing = false;
996 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
997}
998
999/*
1000 * This function cancels all pending commands that matches with
1001 * the given IOCTL request.
1002 *
1003 * Both the current command buffer and the pending command queue are
1004 * searched for matching IOCTL request. The completion callback of
1005 * the matched command is called with failure status to ensure cleanup.
1006 * In case of scan commands, all pending commands in scan pending queue
1007 * are cancelled.
1008 */
1009void
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001010mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001011{
1012 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
1013 unsigned long cmd_flags;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001014 unsigned long scan_pending_q_flags;
1015 uint16_t cancel_scan_cmd = false;
1016
1017 if ((adapter->curr_cmd) &&
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001018 (adapter->curr_cmd->wait_q_enabled)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001019 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1020 cmd_node = adapter->curr_cmd;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001021 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001022 cmd_node->cmd_flag |= CMD_F_CANCELED;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001023 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
Yogesh Ashok Powar51e708c2011-12-13 20:43:16 -08001024 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
1025 adapter->curr_cmd = NULL;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001026 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001027 }
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001028
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001029 /* Cancel all pending scan command */
1030 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1031 scan_pending_q_flags);
1032 list_for_each_entry_safe(cmd_node, tmp_node,
1033 &adapter->scan_pending_q, list) {
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001034 list_del(&cmd_node->list);
1035 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1036 scan_pending_q_flags);
1037 cmd_node->wait_q_enabled = false;
1038 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
1039 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1040 scan_pending_q_flags);
1041 cancel_scan_cmd = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001042 }
1043 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1044 scan_pending_q_flags);
1045
1046 if (cancel_scan_cmd) {
1047 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1048 adapter->scan_processing = false;
1049 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1050 }
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001051 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001052}
1053
1054/*
1055 * This function sends the sleep confirm command to firmware, if
1056 * possible.
1057 *
1058 * The sleep confirm command cannot be issued if command response,
1059 * data response or event response is awaiting handling, or if we
1060 * are in the middle of sending a command, or expecting a command
1061 * response.
1062 */
1063void
1064mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
1065{
1066 if (!adapter->cmd_sent &&
1067 !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
1068 mwifiex_dnld_sleep_confirm_cmd(adapter);
1069 else
1070 dev_dbg(adapter->dev,
1071 "cmd: Delay Sleep Confirm (%s%s%s)\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001072 (adapter->cmd_sent) ? "D" : "",
1073 (adapter->curr_cmd) ? "C" : "",
1074 (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001075}
1076
1077/*
1078 * This function sends a Host Sleep activated event to applications.
1079 *
1080 * This event is generated by the driver, with a blank event body.
1081 */
1082void
1083mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
1084{
1085 if (activated) {
1086 if (priv->adapter->is_hs_configured) {
1087 priv->adapter->hs_activated = true;
1088 dev_dbg(priv->adapter->dev, "event: hs_activated\n");
1089 priv->adapter->hs_activate_wait_q_woken = true;
1090 wake_up_interruptible(
1091 &priv->adapter->hs_activate_wait_q);
1092 } else {
1093 dev_dbg(priv->adapter->dev, "event: HS not configured\n");
1094 }
1095 } else {
1096 dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
1097 priv->adapter->hs_activated = false;
1098 }
1099}
1100
1101/*
1102 * This function handles the command response of a Host Sleep configuration
1103 * command.
1104 *
1105 * Handling includes changing the header fields into CPU format
1106 * and setting the current host sleep activation status in driver.
1107 *
1108 * In case host sleep status change, the function generates an event to
1109 * notify the applications.
1110 */
1111int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
1112 struct host_cmd_ds_command *resp)
1113{
1114 struct mwifiex_adapter *adapter = priv->adapter;
1115 struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
1116 &resp->params.opt_hs_cfg;
1117 uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
1118
Amitkumar Karwara4186ea2012-06-20 19:58:37 -07001119 if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE) &&
1120 adapter->iface_type == MWIFIEX_SDIO) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001121 mwifiex_hs_activated_event(priv, true);
1122 return 0;
1123 } else {
1124 dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
1125 " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
1126 resp->result, conditions,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001127 phs_cfg->params.hs_config.gpio,
1128 phs_cfg->params.hs_config.gap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001129 }
1130 if (conditions != HOST_SLEEP_CFG_CANCEL) {
1131 adapter->is_hs_configured = true;
Amitkumar Karwara4186ea2012-06-20 19:58:37 -07001132 if (adapter->iface_type == MWIFIEX_USB ||
1133 adapter->iface_type == MWIFIEX_PCIE)
1134 mwifiex_hs_activated_event(priv, true);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001135 } else {
1136 adapter->is_hs_configured = false;
1137 if (adapter->hs_activated)
1138 mwifiex_hs_activated_event(priv, false);
1139 }
1140
1141 return 0;
1142}
1143
1144/*
1145 * This function wakes up the adapter and generates a Host Sleep
1146 * cancel event on receiving the power up interrupt.
1147 */
1148void
1149mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
1150{
1151 dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
1152 " since there is interrupt from the firmware\n", __func__);
1153
1154 adapter->if_ops.wakeup(adapter);
1155 adapter->hs_activated = false;
1156 adapter->is_hs_configured = false;
1157 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001158 MWIFIEX_BSS_ROLE_ANY),
1159 false);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001160}
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001161EXPORT_SYMBOL_GPL(mwifiex_process_hs_config);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001162
1163/*
1164 * This function handles the command response of a sleep confirm command.
1165 *
1166 * The function sets the card state to SLEEP if the response indicates success.
1167 */
1168void
1169mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
1170 u8 *pbuf, u32 upld_len)
1171{
1172 struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
1173 struct mwifiex_private *priv =
1174 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1175 uint16_t result = le16_to_cpu(cmd->result);
1176 uint16_t command = le16_to_cpu(cmd->command);
1177 uint16_t seq_num = le16_to_cpu(cmd->seq_num);
1178
1179 if (!upld_len) {
1180 dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
1181 return;
1182 }
1183
1184 /* Get BSS number and corresponding priv */
1185 priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
1186 HostCmd_GET_BSS_TYPE(seq_num));
1187 if (!priv)
1188 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1189
1190 /* Update sequence number */
1191 seq_num = HostCmd_GET_SEQ_NO(seq_num);
1192 /* Clear RET_BIT from HostCmd */
1193 command &= HostCmd_CMD_ID_MASK;
1194
1195 if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001196 dev_err(adapter->dev,
1197 "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
1198 __func__, command, result);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001199 return;
1200 }
1201
1202 if (result) {
1203 dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001204 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001205 adapter->pm_wakeup_card_req = false;
1206 adapter->ps_state = PS_STATE_AWAKE;
1207 return;
1208 }
1209 adapter->pm_wakeup_card_req = true;
1210 if (adapter->is_hs_configured)
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001211 mwifiex_hs_activated_event(mwifiex_get_priv
1212 (adapter, MWIFIEX_BSS_ROLE_ANY),
1213 true);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001214 adapter->ps_state = PS_STATE_SLEEP;
1215 cmd->command = cpu_to_le16(command);
1216 cmd->seq_num = cpu_to_le16(seq_num);
1217}
1218EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
1219
1220/*
1221 * This function prepares an enhanced power mode command.
1222 *
1223 * This function can be used to disable power save or to configure
1224 * power save with auto PS or STA PS or auto deep sleep.
1225 *
1226 * Preparation includes -
1227 * - Setting command ID, action and proper size
1228 * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
1229 * auto deep sleep TLV (as required)
1230 * - Ensuring correct endian-ness
1231 */
1232int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
1233 struct host_cmd_ds_command *cmd,
1234 u16 cmd_action, uint16_t ps_bitmap,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001235 struct mwifiex_ds_auto_ds *auto_ds)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001236{
1237 struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
1238 &cmd->params.psmode_enh;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001239 u8 *tlv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001240 u16 cmd_size = 0;
1241
1242 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
1243 if (cmd_action == DIS_AUTO_PS) {
1244 psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
1245 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001246 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001247 sizeof(psmode_enh->params.ps_bitmap));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001248 } else if (cmd_action == GET_PS) {
1249 psmode_enh->action = cpu_to_le16(GET_PS);
1250 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001251 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001252 sizeof(psmode_enh->params.ps_bitmap));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001253 } else if (cmd_action == EN_AUTO_PS) {
1254 psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001255 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1256 cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001257 sizeof(psmode_enh->params.ps_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001258 tlv = (u8 *) cmd + cmd_size;
1259 if (ps_bitmap & BITMAP_STA_PS) {
1260 struct mwifiex_adapter *adapter = priv->adapter;
1261 struct mwifiex_ie_types_ps_param *ps_tlv =
1262 (struct mwifiex_ie_types_ps_param *) tlv;
1263 struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
1264 ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
1265 ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
1266 sizeof(struct mwifiex_ie_types_header));
1267 cmd_size += sizeof(*ps_tlv);
1268 tlv += sizeof(*ps_tlv);
1269 dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
1270 ps_mode->null_pkt_interval =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001271 cpu_to_le16(adapter->null_pkt_interval);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001272 ps_mode->multiple_dtims =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001273 cpu_to_le16(adapter->multiple_dtim);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001274 ps_mode->bcn_miss_timeout =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001275 cpu_to_le16(adapter->bcn_miss_time_out);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001276 ps_mode->local_listen_interval =
1277 cpu_to_le16(adapter->local_listen_interval);
1278 ps_mode->adhoc_wake_period =
1279 cpu_to_le16(adapter->adhoc_awake_period);
1280 ps_mode->delay_to_ps =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001281 cpu_to_le16(adapter->delay_to_ps);
1282 ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001283
1284 }
1285 if (ps_bitmap & BITMAP_AUTO_DS) {
Marc Yang2b06bdb2011-03-30 18:12:44 -07001286 struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001287 (struct mwifiex_ie_types_auto_ds_param *) tlv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001288 u16 idletime = 0;
Marc Yang2b06bdb2011-03-30 18:12:44 -07001289
1290 auto_ds_tlv->header.type =
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001291 cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001292 auto_ds_tlv->header.len =
1293 cpu_to_le16(sizeof(*auto_ds_tlv) -
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001294 sizeof(struct mwifiex_ie_types_header));
Marc Yang2b06bdb2011-03-30 18:12:44 -07001295 cmd_size += sizeof(*auto_ds_tlv);
1296 tlv += sizeof(*auto_ds_tlv);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001297 if (auto_ds)
1298 idletime = auto_ds->idle_time;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001299 dev_dbg(priv->adapter->dev,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001300 "cmd: PS Command: Enter Auto Deep Sleep\n");
Marc Yang2b06bdb2011-03-30 18:12:44 -07001301 auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001302 }
1303 cmd->size = cpu_to_le16(cmd_size);
1304 }
1305 return 0;
1306}
1307
1308/*
1309 * This function handles the command response of an enhanced power mode
1310 * command.
1311 *
1312 * Handling includes changing the header fields into CPU format
1313 * and setting the current enhanced power mode in driver.
1314 */
1315int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
1316 struct host_cmd_ds_command *resp,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001317 struct mwifiex_ds_pm_cfg *pm_cfg)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001318{
1319 struct mwifiex_adapter *adapter = priv->adapter;
1320 struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
1321 &resp->params.psmode_enh;
1322 uint16_t action = le16_to_cpu(ps_mode->action);
1323 uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
1324 uint16_t auto_ps_bitmap =
Marc Yang2b06bdb2011-03-30 18:12:44 -07001325 le16_to_cpu(ps_mode->params.ps_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001326
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001327 dev_dbg(adapter->dev,
1328 "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
1329 __func__, resp->result, action);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001330 if (action == EN_AUTO_PS) {
1331 if (auto_ps_bitmap & BITMAP_AUTO_DS) {
1332 dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
1333 priv->adapter->is_deep_sleep = true;
1334 }
1335 if (auto_ps_bitmap & BITMAP_STA_PS) {
1336 dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
1337 if (adapter->sleep_period.period)
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001338 dev_dbg(adapter->dev,
1339 "cmd: set to uapsd/pps mode\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001340 }
1341 } else if (action == DIS_AUTO_PS) {
1342 if (ps_bitmap & BITMAP_AUTO_DS) {
1343 priv->adapter->is_deep_sleep = false;
1344 dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
1345 }
1346 if (ps_bitmap & BITMAP_STA_PS) {
1347 dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
1348 if (adapter->sleep_period.period) {
1349 adapter->delay_null_pkt = false;
1350 adapter->tx_lock_flag = false;
1351 adapter->pps_uapsd_mode = false;
1352 }
1353 }
1354 } else if (action == GET_PS) {
Marc Yang2b06bdb2011-03-30 18:12:44 -07001355 if (ps_bitmap & BITMAP_STA_PS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001356 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1357 else
1358 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
1359
1360 dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
1361
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001362 if (pm_cfg) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001363 /* This section is for get power save mode */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001364 if (ps_bitmap & BITMAP_STA_PS)
1365 pm_cfg->param.ps_mode = 1;
1366 else
1367 pm_cfg->param.ps_mode = 0;
1368 }
1369 }
1370 return 0;
1371}
1372
1373/*
1374 * This function prepares command to get hardware specifications.
1375 *
1376 * Preparation includes -
1377 * - Setting command ID, action and proper size
1378 * - Setting permanent address parameter
1379 * - Ensuring correct endian-ness
1380 */
1381int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
1382 struct host_cmd_ds_command *cmd)
1383{
1384 struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
1385
1386 cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
1387 cmd->size =
1388 cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
1389 memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
1390
1391 return 0;
1392}
1393
1394/*
1395 * This function handles the command response of get hardware
1396 * specifications.
1397 *
1398 * Handling includes changing the header fields into CPU format
1399 * and saving/updating the following parameters in driver -
1400 * - Firmware capability information
1401 * - Firmware band settings
1402 * - Ad-hoc start band and channel
1403 * - Ad-hoc 11n activation status
1404 * - Firmware release number
1405 * - Number of antennas
1406 * - Hardware address
1407 * - Hardware interface version
1408 * - Firmware version
1409 * - Region code
1410 * - 11n capabilities
1411 * - MCS support fields
1412 * - MP end port
1413 */
1414int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
1415 struct host_cmd_ds_command *resp)
1416{
1417 struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
1418 struct mwifiex_adapter *adapter = priv->adapter;
1419 int i;
1420
1421 adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
1422
1423 if (IS_SUPPORT_MULTI_BANDS(adapter))
1424 adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
1425 else
1426 adapter->fw_bands = BAND_B;
1427
1428 adapter->config_bands = adapter->fw_bands;
1429
1430 if (adapter->fw_bands & BAND_A) {
1431 if (adapter->fw_bands & BAND_GN) {
1432 adapter->config_bands |= BAND_AN;
1433 adapter->fw_bands |= BAND_AN;
1434 }
1435 if (adapter->fw_bands & BAND_AN) {
1436 adapter->adhoc_start_band = BAND_A | BAND_AN;
1437 adapter->adhoc_11n_enabled = true;
1438 } else {
1439 adapter->adhoc_start_band = BAND_A;
1440 }
1441 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
1442 } else if (adapter->fw_bands & BAND_GN) {
1443 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
1444 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1445 adapter->adhoc_11n_enabled = true;
1446 } else if (adapter->fw_bands & BAND_G) {
1447 adapter->adhoc_start_band = BAND_G | BAND_B;
1448 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1449 } else if (adapter->fw_bands & BAND_B) {
1450 adapter->adhoc_start_band = BAND_B;
1451 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1452 }
1453
1454 adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
1455 adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
1456
1457 dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001458 adapter->fw_release_number);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001459 dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001460 hw_spec->permanent_addr);
1461 dev_dbg(adapter->dev,
1462 "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001463 le16_to_cpu(hw_spec->hw_if_version),
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001464 le16_to_cpu(hw_spec->version));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001465
1466 if (priv->curr_addr[0] == 0xff)
1467 memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
1468
1469 adapter->region_code = le16_to_cpu(hw_spec->region_code);
1470
1471 for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
1472 /* Use the region code to search for the index */
1473 if (adapter->region_code == region_code_index[i])
1474 break;
1475
1476 /* If it's unidentified region code, use the default (USA) */
1477 if (i >= MWIFIEX_MAX_REGION_CODE) {
1478 adapter->region_code = 0x10;
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001479 dev_dbg(adapter->dev,
1480 "cmd: unknown region code, use default (USA)\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001481 }
1482
1483 adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001484 adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001485
1486 if (adapter->if_ops.update_mp_end_port)
1487 adapter->if_ops.update_mp_end_port(adapter,
1488 le16_to_cpu(hw_spec->mp_end_port));
1489
1490 return 0;
1491}