blob: e95f80de7c5021862449c0e8f2edf97aa2b11a25 [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/**
2 * This file contains the handling of command.
3 * It prepares command and sends it to firmware when it is ready.
4 */
5
Holger Schurig7919b892008-04-01 14:50:43 +02006#include <linux/kfifo.h>
Holger Schurige93156e2009-10-22 15:30:58 +02007#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Dan Williamsa45b6f42010-07-27 12:54:34 -07009#include <linux/if_arp.h>
Holger Schurige93156e2009-10-22 15:30:58 +020010
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020011#include "decl.h"
Kiran Divekare86dc1c2010-06-14 22:01:26 +053012#include "cfg.h"
Dan Williams6e66f032007-12-11 12:42:16 -050013#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014
Dan Williams9fb76632010-07-27 12:55:21 -070015#define CAL_NF(nf) ((s32)(-(s32)(nf)))
16#define CAL_RSSI(snr, nf) ((s32)((s32)(snr) + CAL_NF(nf)))
Holger Schurige93156e2009-10-22 15:30:58 +020017
David Woodhouse2fd6cfe2007-12-11 17:44:10 -050018static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
Holger Schurig0d61d042007-12-05 17:58:06 +010019
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020020/**
Holger Schurig8db4a2b2008-03-19 10:11:00 +010021 * @brief Simple callback that copies response back into command
22 *
23 * @param priv A pointer to struct lbs_private structure
24 * @param extra A pointer to the original command structure for which
25 * 'resp' is a response
26 * @param resp A pointer to the command response
27 *
28 * @return 0 on success, error on failure
29 */
30int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
31 struct cmd_header *resp)
32{
33 struct cmd_header *buf = (void *)extra;
34 uint16_t copy_len;
35
36 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
37 memcpy(buf, resp, copy_len);
38 return 0;
39}
40EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
41
42/**
43 * @brief Simple callback that ignores the result. Use this if
44 * you just want to send a command to the hardware, but don't
45 * care for the result.
46 *
47 * @param priv ignored
48 * @param extra ignored
49 * @param resp ignored
50 *
51 * @return 0 for success
52 */
53static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
54 struct cmd_header *resp)
55{
56 return 0;
57}
58
59
60/**
Dan Williams852e1f22007-12-10 15:24:47 -050061 * @brief Checks whether a command is allowed in Power Save mode
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020062 *
63 * @param command the command ID
Dan Williams852e1f22007-12-10 15:24:47 -050064 * @return 1 if allowed, 0 if not allowed
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020065 */
Dan Williams852e1f22007-12-10 15:24:47 -050066static u8 is_command_allowed_in_ps(u16 cmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020067{
Dan Williams852e1f22007-12-10 15:24:47 -050068 switch (cmd) {
69 case CMD_802_11_RSSI:
70 return 1;
Amitkumar Karwar66fceb62010-05-19 03:24:38 -070071 case CMD_802_11_HOST_SLEEP_CFG:
72 return 1;
Dan Williams852e1f22007-12-10 15:24:47 -050073 default:
74 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020075 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020076 return 0;
77}
78
Dan Williams6e66f032007-12-11 12:42:16 -050079/**
Amitkumar Karwar63f275d2009-10-06 19:20:28 -070080 * @brief This function checks if the command is allowed.
81 *
82 * @param priv A pointer to lbs_private structure
83 * @return allowed or not allowed.
84 */
85
86static int lbs_is_cmd_allowed(struct lbs_private *priv)
87{
88 int ret = 1;
89
90 lbs_deb_enter(LBS_DEB_CMD);
91
92 if (!priv->is_auto_deep_sleep_enabled) {
93 if (priv->is_deep_sleep) {
94 lbs_deb_cmd("command not allowed in deep sleep\n");
95 ret = 0;
96 }
97 }
98
99 lbs_deb_leave(LBS_DEB_CMD);
100 return ret;
101}
102
103/**
Dan Williams6e66f032007-12-11 12:42:16 -0500104 * @brief Updates the hardware details like MAC address and regulatory region
105 *
106 * @param priv A pointer to struct lbs_private structure
107 *
108 * @return 0 on success, error on failure
109 */
110int lbs_update_hw_spec(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200111{
Dan Williams6e66f032007-12-11 12:42:16 -0500112 struct cmd_ds_get_hw_spec cmd;
113 int ret = -1;
114 u32 i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200115
Holger Schurig9012b282007-05-25 11:27:16 -0400116 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200117
Dan Williams6e66f032007-12-11 12:42:16 -0500118 memset(&cmd, 0, sizeof(cmd));
119 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
120 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
David Woodhouse689442d2007-12-12 16:00:42 -0500121 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
Dan Williams6e66f032007-12-11 12:42:16 -0500122 if (ret)
123 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200124
Dan Williams6e66f032007-12-11 12:42:16 -0500125 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
Dan Williams6e66f032007-12-11 12:42:16 -0500126
Holger Schurigdac10a92008-01-16 15:55:22 +0100127 /* The firmware release is in an interesting format: the patch
128 * level is in the most significant nibble ... so fix that: */
129 priv->fwrelease = le32_to_cpu(cmd.fwrelease);
130 priv->fwrelease = (priv->fwrelease << 8) |
131 (priv->fwrelease >> 24 & 0xff);
132
133 /* Some firmware capabilities:
134 * CF card firmware 5.0.16p0: cap 0x00000303
135 * USB dongle firmware 5.110.17p2: cap 0x00000303
136 */
Johannes Berge1749612008-10-27 15:59:26 -0700137 lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
138 cmd.permanentaddr,
Holger Schurigdac10a92008-01-16 15:55:22 +0100139 priv->fwrelease >> 24 & 0xff,
140 priv->fwrelease >> 16 & 0xff,
141 priv->fwrelease >> 8 & 0xff,
142 priv->fwrelease & 0xff,
143 priv->fwcapinfo);
Dan Williams6e66f032007-12-11 12:42:16 -0500144 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
145 cmd.hwifversion, cmd.version);
146
147 /* Clamp region code to 8-bit since FW spec indicates that it should
148 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
149 * returns non-zero high 8 bits here.
Marek Vasut15483992009-07-16 19:19:53 +0200150 *
151 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
152 * need to check for this problem and handle it properly.
Dan Williams6e66f032007-12-11 12:42:16 -0500153 */
Marek Vasut15483992009-07-16 19:19:53 +0200154 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
155 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
156 else
157 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
Dan Williams6e66f032007-12-11 12:42:16 -0500158
159 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
160 /* use the region code to search for the index */
161 if (priv->regioncode == lbs_region_code_to_index[i])
162 break;
163 }
164
165 /* if it's unidentified region code, use the default (USA) */
166 if (i >= MRVDRV_MAX_REGION_CODE) {
167 priv->regioncode = 0x10;
168 lbs_pr_info("unidentified region code; using the default (USA)\n");
169 }
170
171 if (priv->current_addr[0] == 0xff)
172 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
173
174 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
175 if (priv->mesh_dev)
176 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
177
Dan Williams6e66f032007-12-11 12:42:16 -0500178out:
Holger Schurig9012b282007-05-25 11:27:16 -0400179 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams6e66f032007-12-11 12:42:16 -0500180 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200181}
182
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700183static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
184 struct cmd_header *resp)
185{
186 lbs_deb_enter(LBS_DEB_CMD);
Amitkumar Karwar13118432010-07-08 06:43:48 +0530187 if (priv->is_host_sleep_activated) {
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700188 priv->is_host_sleep_configured = 0;
189 if (priv->psstate == PS_STATE_FULL_POWER) {
190 priv->is_host_sleep_activated = 0;
191 wake_up_interruptible(&priv->host_sleep_q);
192 }
193 } else {
194 priv->is_host_sleep_configured = 1;
195 }
196 lbs_deb_leave(LBS_DEB_CMD);
197 return 0;
198}
199
Anna Neal582c1b52008-10-20 16:46:56 -0700200int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
201 struct wol_config *p_wol_config)
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500202{
203 struct cmd_ds_host_sleep cmd_config;
204 int ret;
205
David Woodhouse9fae8992007-12-15 03:46:44 -0500206 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500207 cmd_config.criteria = cpu_to_le32(criteria);
David Woodhouse506e9022007-12-12 20:06:06 -0500208 cmd_config.gpio = priv->wol_gpio;
209 cmd_config.gap = priv->wol_gap;
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500210
Anna Neal582c1b52008-10-20 16:46:56 -0700211 if (p_wol_config != NULL)
212 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
213 sizeof(struct wol_config));
214 else
215 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
216
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700217 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
218 le16_to_cpu(cmd_config.hdr.size),
219 lbs_ret_host_sleep_cfg, 0);
David Woodhouse506e9022007-12-12 20:06:06 -0500220 if (!ret) {
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700221 if (p_wol_config)
Anna Neal582c1b52008-10-20 16:46:56 -0700222 memcpy((uint8_t *) p_wol_config,
223 (uint8_t *)&cmd_config.wol_conf,
224 sizeof(struct wol_config));
David Woodhouse506e9022007-12-12 20:06:06 -0500225 } else {
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500226 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500227 }
David Woodhouse506e9022007-12-12 20:06:06 -0500228
David Woodhouse6ce4fd22007-12-12 15:19:29 -0500229 return ret;
230}
231EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
232
Holger Schurige98a88d2008-03-19 14:25:58 +0100233static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200234 u16 cmd_action)
235{
236 struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200237
Holger Schurig9012b282007-05-25 11:27:16 -0400238 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200239
Dan Williams0aef64d2007-08-02 11:31:18 -0400240 cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
David Woodhouse981f1872007-05-25 23:36:54 -0400241 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200242 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200243 psm->action = cpu_to_le16(cmd_action);
244 psm->multipledtim = 0;
David Woodhouse981f1872007-05-25 23:36:54 -0400245 switch (cmd_action) {
Dan Williams0aef64d2007-08-02 11:31:18 -0400246 case CMD_SUBCMD_ENTER_PS:
Holger Schurig9012b282007-05-25 11:27:16 -0400247 lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200248
Holger Schurig252cf0d2007-08-02 13:09:34 -0400249 psm->locallisteninterval = 0;
Holger Schurig97605c32007-08-02 13:09:15 -0400250 psm->nullpktinterval = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200251 psm->multipledtim =
Holger Schurig56c46562007-08-02 13:09:49 -0400252 cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200253 break;
254
Dan Williams0aef64d2007-08-02 11:31:18 -0400255 case CMD_SUBCMD_EXIT_PS:
Holger Schurig9012b282007-05-25 11:27:16 -0400256 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200257 break;
258
Dan Williams0aef64d2007-08-02 11:31:18 -0400259 case CMD_SUBCMD_SLEEP_CONFIRMED:
Holger Schurig9012b282007-05-25 11:27:16 -0400260 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200261 break;
262
263 default:
264 break;
265 }
266
Holger Schurig9012b282007-05-25 11:27:16 -0400267 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200268 return 0;
269}
270
David Woodhouse3fbe1042007-12-17 23:48:31 -0500271int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
272 struct sleep_params *sp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200273{
David Woodhouse3fbe1042007-12-17 23:48:31 -0500274 struct cmd_ds_802_11_sleep_params cmd;
275 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200276
Holger Schurig9012b282007-05-25 11:27:16 -0400277 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200278
Dan Williams0aef64d2007-08-02 11:31:18 -0400279 if (cmd_action == CMD_ACT_GET) {
David Woodhouse3fbe1042007-12-17 23:48:31 -0500280 memset(&cmd, 0, sizeof(cmd));
281 } else {
282 cmd.error = cpu_to_le16(sp->sp_error);
283 cmd.offset = cpu_to_le16(sp->sp_offset);
284 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
285 cmd.calcontrol = sp->sp_calcontrol;
286 cmd.externalsleepclk = sp->sp_extsleepclk;
287 cmd.reserved = cpu_to_le16(sp->sp_reserved);
288 }
289 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
290 cmd.action = cpu_to_le16(cmd_action);
291
292 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
293
294 if (!ret) {
295 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
296 "calcontrol 0x%x extsleepclk 0x%x\n",
297 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
298 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
299 cmd.externalsleepclk);
300
301 sp->sp_error = le16_to_cpu(cmd.error);
302 sp->sp_offset = le16_to_cpu(cmd.offset);
303 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
304 sp->sp_calcontrol = cmd.calcontrol;
305 sp->sp_extsleepclk = cmd.externalsleepclk;
306 sp->sp_reserved = le16_to_cpu(cmd.reserved);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200307 }
308
David Woodhouse3fbe1042007-12-17 23:48:31 -0500309 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200310 return 0;
311}
312
Amitkumar Karwar49125452009-09-30 20:04:38 -0700313static int lbs_wait_for_ds_awake(struct lbs_private *priv)
314{
315 int ret = 0;
316
317 lbs_deb_enter(LBS_DEB_CMD);
318
319 if (priv->is_deep_sleep) {
320 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
321 !priv->is_deep_sleep, (10 * HZ))) {
322 lbs_pr_err("ds_awake_q: timer expired\n");
323 ret = -1;
324 }
325 }
326
327 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
328 return ret;
329}
330
331int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
332{
333 int ret = 0;
334
335 lbs_deb_enter(LBS_DEB_CMD);
336
337 if (deep_sleep) {
338 if (priv->is_deep_sleep != 1) {
339 lbs_deb_cmd("deep sleep: sleep\n");
340 BUG_ON(!priv->enter_deep_sleep);
341 ret = priv->enter_deep_sleep(priv);
342 if (!ret) {
343 netif_stop_queue(priv->dev);
344 netif_carrier_off(priv->dev);
345 }
346 } else {
347 lbs_pr_err("deep sleep: already enabled\n");
348 }
349 } else {
350 if (priv->is_deep_sleep) {
351 lbs_deb_cmd("deep sleep: wakeup\n");
352 BUG_ON(!priv->exit_deep_sleep);
353 ret = priv->exit_deep_sleep(priv);
354 if (!ret) {
355 ret = lbs_wait_for_ds_awake(priv);
356 if (ret)
357 lbs_pr_err("deep sleep: wakeup"
358 "failed\n");
359 }
360 }
361 }
362
363 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
364 return ret;
365}
366
Amitkumar Karwar13118432010-07-08 06:43:48 +0530367static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
368 unsigned long dummy,
369 struct cmd_header *cmd)
370{
371 lbs_deb_enter(LBS_DEB_FW);
372 priv->is_host_sleep_activated = 1;
373 wake_up_interruptible(&priv->host_sleep_q);
374 lbs_deb_leave(LBS_DEB_FW);
375 return 0;
376}
377
378int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
379{
380 struct cmd_header cmd;
381 int ret = 0;
382 uint32_t criteria = EHS_REMOVE_WAKEUP;
383
384 lbs_deb_enter(LBS_DEB_CMD);
385
386 if (host_sleep) {
387 if (priv->is_host_sleep_activated != 1) {
388 memset(&cmd, 0, sizeof(cmd));
389 ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
390 (struct wol_config *)NULL);
391 if (ret) {
392 lbs_pr_info("Host sleep configuration failed: "
393 "%d\n", ret);
394 return ret;
395 }
396 if (priv->psstate == PS_STATE_FULL_POWER) {
397 ret = __lbs_cmd(priv,
398 CMD_802_11_HOST_SLEEP_ACTIVATE,
399 &cmd,
400 sizeof(cmd),
401 lbs_ret_host_sleep_activate, 0);
402 if (ret)
403 lbs_pr_info("HOST_SLEEP_ACTIVATE "
404 "failed: %d\n", ret);
405 }
406
407 if (!wait_event_interruptible_timeout(
408 priv->host_sleep_q,
409 priv->is_host_sleep_activated,
410 (10 * HZ))) {
411 lbs_pr_err("host_sleep_q: timer expired\n");
412 ret = -1;
413 }
414 } else {
415 lbs_pr_err("host sleep: already enabled\n");
416 }
417 } else {
418 if (priv->is_host_sleep_activated)
419 ret = lbs_host_sleep_cfg(priv, criteria,
420 (struct wol_config *)NULL);
421 }
422
423 return ret;
424}
425
Dan Williams39fcf7a2008-09-10 12:49:00 -0400426/**
427 * @brief Set an SNMP MIB value
428 *
429 * @param priv A pointer to struct lbs_private structure
430 * @param oid The OID to set in the firmware
431 * @param val Value to set the OID to
432 *
433 * @return 0 on success, error on failure
434 */
435int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200436{
Dan Williams39fcf7a2008-09-10 12:49:00 -0400437 struct cmd_ds_802_11_snmp_mib cmd;
438 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200439
Holger Schurig9012b282007-05-25 11:27:16 -0400440 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200441
Dan Williams39fcf7a2008-09-10 12:49:00 -0400442 memset(&cmd, 0, sizeof (cmd));
443 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
444 cmd.action = cpu_to_le16(CMD_ACT_SET);
445 cmd.oid = cpu_to_le16((u16) oid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446
Dan Williams39fcf7a2008-09-10 12:49:00 -0400447 switch (oid) {
448 case SNMP_MIB_OID_BSS_TYPE:
449 cmd.bufsize = cpu_to_le16(sizeof(u8));
Holger Schurigfef06402009-10-22 15:30:59 +0200450 cmd.value[0] = val;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451 break;
Dan Williams39fcf7a2008-09-10 12:49:00 -0400452 case SNMP_MIB_OID_11D_ENABLE:
453 case SNMP_MIB_OID_FRAG_THRESHOLD:
454 case SNMP_MIB_OID_RTS_THRESHOLD:
455 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
456 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
457 cmd.bufsize = cpu_to_le16(sizeof(u16));
458 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200459 break;
460 default:
Dan Williams39fcf7a2008-09-10 12:49:00 -0400461 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
462 ret = -EINVAL;
463 goto out;
464 }
465
466 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
467 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
468
469 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
470
471out:
472 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
473 return ret;
474}
475
476/**
477 * @brief Get an SNMP MIB value
478 *
479 * @param priv A pointer to struct lbs_private structure
480 * @param oid The OID to retrieve from the firmware
481 * @param out_val Location for the returned value
482 *
483 * @return 0 on success, error on failure
484 */
485int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
486{
487 struct cmd_ds_802_11_snmp_mib cmd;
488 int ret;
489
490 lbs_deb_enter(LBS_DEB_CMD);
491
492 memset(&cmd, 0, sizeof (cmd));
493 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
494 cmd.action = cpu_to_le16(CMD_ACT_GET);
495 cmd.oid = cpu_to_le16(oid);
496
497 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
498 if (ret)
499 goto out;
500
501 switch (le16_to_cpu(cmd.bufsize)) {
502 case sizeof(u8):
Holger Schurigfef06402009-10-22 15:30:59 +0200503 *out_val = cmd.value[0];
Dan Williams39fcf7a2008-09-10 12:49:00 -0400504 break;
505 case sizeof(u16):
506 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
507 break;
508 default:
509 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
510 oid, le16_to_cpu(cmd.bufsize));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200511 break;
512 }
513
Dan Williams39fcf7a2008-09-10 12:49:00 -0400514out:
515 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
516 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200517}
518
Dan Williams87c8c722008-08-19 15:15:35 -0400519/**
520 * @brief Get the min, max, and current TX power
521 *
522 * @param priv A pointer to struct lbs_private structure
523 * @param curlevel Current power level in dBm
524 * @param minlevel Minimum supported power level in dBm (optional)
525 * @param maxlevel Maximum supported power level in dBm (optional)
526 *
527 * @return 0 on success, error on failure
528 */
529int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
530 s16 *maxlevel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200531{
Dan Williams87c8c722008-08-19 15:15:35 -0400532 struct cmd_ds_802_11_rf_tx_power cmd;
533 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200534
Holger Schurig9012b282007-05-25 11:27:16 -0400535 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200536
Dan Williams87c8c722008-08-19 15:15:35 -0400537 memset(&cmd, 0, sizeof(cmd));
538 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
539 cmd.action = cpu_to_le16(CMD_ACT_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200540
Dan Williams87c8c722008-08-19 15:15:35 -0400541 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
542 if (ret == 0) {
543 *curlevel = le16_to_cpu(cmd.curlevel);
544 if (minlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100545 *minlevel = cmd.minlevel;
Dan Williams87c8c722008-08-19 15:15:35 -0400546 if (maxlevel)
Holger Schurig87bf24f2008-10-29 10:35:02 +0100547 *maxlevel = cmd.maxlevel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200548 }
Holger Schurig9012b282007-05-25 11:27:16 -0400549
550 lbs_deb_leave(LBS_DEB_CMD);
Dan Williams87c8c722008-08-19 15:15:35 -0400551 return ret;
552}
553
554/**
555 * @brief Set the TX power
556 *
557 * @param priv A pointer to struct lbs_private structure
558 * @param dbm The desired power level in dBm
559 *
560 * @return 0 on success, error on failure
561 */
562int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
563{
564 struct cmd_ds_802_11_rf_tx_power cmd;
565 int ret;
566
567 lbs_deb_enter(LBS_DEB_CMD);
568
569 memset(&cmd, 0, sizeof(cmd));
570 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
571 cmd.action = cpu_to_le16(CMD_ACT_SET);
572 cmd.curlevel = cpu_to_le16(dbm);
573
574 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
575
576 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
577
578 lbs_deb_leave(LBS_DEB_CMD);
579 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200580}
581
Dan Williamsa45b6f42010-07-27 12:54:34 -0700582/**
583 * @brief Enable or disable monitor mode (only implemented on OLPC usb8388 FW)
584 *
585 * @param priv A pointer to struct lbs_private structure
586 * @param enable 1 to enable monitor mode, 0 to disable
587 *
588 * @return 0 on success, error on failure
589 */
590int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400591{
Dan Williamsa45b6f42010-07-27 12:54:34 -0700592 struct cmd_ds_802_11_monitor_mode cmd;
593 int ret;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400594
Dan Williamsa45b6f42010-07-27 12:54:34 -0700595 memset(&cmd, 0, sizeof(cmd));
596 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
597 cmd.action = cpu_to_le16(CMD_ACT_SET);
598 if (enable)
599 cmd.mode = cpu_to_le16(0x1);
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400600
Dan Williamsa45b6f42010-07-27 12:54:34 -0700601 lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
602
603 ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
604 if (ret == 0) {
605 priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
606 ARPHRD_ETHER;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400607 }
608
Dan Williamsa45b6f42010-07-27 12:54:34 -0700609 lbs_deb_leave(LBS_DEB_CMD);
610 return ret;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400611}
612
Dan Williams2dd4b262007-12-11 16:54:15 -0500613/**
614 * @brief Get the radio channel
615 *
616 * @param priv A pointer to struct lbs_private structure
617 *
618 * @return The channel on success, error on failure
619 */
Holger Schuriga3cbfb02009-10-16 17:33:56 +0200620static int lbs_get_channel(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200621{
Dan Williams2dd4b262007-12-11 16:54:15 -0500622 struct cmd_ds_802_11_rf_channel cmd;
623 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200624
Holger Schurig8ff12da2007-08-02 11:54:31 -0400625 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200627 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500628 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
629 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200630
David Woodhouse689442d2007-12-12 16:00:42 -0500631 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500632 if (ret)
633 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634
Dan Williamscb182a62007-12-11 17:35:51 -0500635 ret = le16_to_cpu(cmd.channel);
636 lbs_deb_cmd("current radio channel is %d\n", ret);
Dan Williams2dd4b262007-12-11 16:54:15 -0500637
638out:
639 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
640 return ret;
641}
642
Holger Schurig73ab1f22008-04-02 16:52:19 +0200643int lbs_update_channel(struct lbs_private *priv)
644{
645 int ret;
646
647 /* the channel in f/w could be out of sync; get the current channel */
648 lbs_deb_enter(LBS_DEB_ASSOC);
649
650 ret = lbs_get_channel(priv);
651 if (ret > 0) {
Holger Schurigc14951f2009-10-22 15:30:50 +0200652 priv->channel = ret;
Holger Schurig73ab1f22008-04-02 16:52:19 +0200653 ret = 0;
654 }
655 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
656 return ret;
657}
658
Dan Williams2dd4b262007-12-11 16:54:15 -0500659/**
660 * @brief Set the radio channel
661 *
662 * @param priv A pointer to struct lbs_private structure
663 * @param channel The desired channel, or 0 to clear a locked channel
664 *
665 * @return 0 on success, error on failure
666 */
667int lbs_set_channel(struct lbs_private *priv, u8 channel)
668{
669 struct cmd_ds_802_11_rf_channel cmd;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530670#ifdef DEBUG
Holger Schurigc14951f2009-10-22 15:30:50 +0200671 u8 old_channel = priv->channel;
Manish Katiyar96d46d52008-10-13 16:22:42 +0530672#endif
Dan Williams2dd4b262007-12-11 16:54:15 -0500673 int ret = 0;
674
675 lbs_deb_enter(LBS_DEB_CMD);
676
Holger Schurig8d0c7fa2008-04-09 10:23:31 +0200677 memset(&cmd, 0, sizeof(cmd));
Dan Williams2dd4b262007-12-11 16:54:15 -0500678 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
679 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
680 cmd.channel = cpu_to_le16(channel);
681
David Woodhouse689442d2007-12-12 16:00:42 -0500682 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
Dan Williams2dd4b262007-12-11 16:54:15 -0500683 if (ret)
684 goto out;
685
Holger Schurigc14951f2009-10-22 15:30:50 +0200686 priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
Dan Williamscb182a62007-12-11 17:35:51 -0500687 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
Holger Schurigc14951f2009-10-22 15:30:50 +0200688 priv->channel);
Dan Williams2dd4b262007-12-11 16:54:15 -0500689
690out:
691 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
692 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200693}
694
Dan Williams9fb76632010-07-27 12:55:21 -0700695/**
696 * @brief Get current RSSI and noise floor
697 *
698 * @param priv A pointer to struct lbs_private structure
699 * @param rssi On successful return, signal level in mBm
700 *
701 * @return The channel on success, error on failure
702 */
703int lbs_get_rssi(struct lbs_private *priv, s8 *rssi, s8 *nf)
704{
705 struct cmd_ds_802_11_rssi cmd;
706 int ret = 0;
707
708 lbs_deb_enter(LBS_DEB_CMD);
709
710 BUG_ON(rssi == NULL);
711 BUG_ON(nf == NULL);
712
713 memset(&cmd, 0, sizeof(cmd));
714 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
715 /* Average SNR over last 8 beacons */
716 cmd.n_or_snr = cpu_to_le16(8);
717
718 ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
719 if (ret == 0) {
720 *nf = CAL_NF(le16_to_cpu(cmd.nf));
721 *rssi = CAL_RSSI(le16_to_cpu(cmd.n_or_snr), le16_to_cpu(cmd.nf));
722 }
723
724 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
725 return ret;
726}
727
Holger Schurige98a88d2008-03-19 14:25:58 +0100728static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729 u8 cmd_action, void *pdata_buf)
730{
Holger Schurig10078322007-11-15 18:05:47 -0500731 struct lbs_offset_value *offval;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200732
Holger Schurig9012b282007-05-25 11:27:16 -0400733 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200734
Holger Schurig10078322007-11-15 18:05:47 -0500735 offval = (struct lbs_offset_value *)pdata_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200736
Holger Schurigc2df2ef2007-12-07 15:30:44 +0000737 switch (le16_to_cpu(cmdptr->command)) {
Dan Williams0aef64d2007-08-02 11:31:18 -0400738 case CMD_MAC_REG_ACCESS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200739 {
740 struct cmd_ds_mac_reg_access *macreg;
741
742 cmdptr->size =
David Woodhouse981f1872007-05-25 23:36:54 -0400743 cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200744 + sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200745 macreg =
746 (struct cmd_ds_mac_reg_access *)&cmdptr->params.
747 macreg;
748
749 macreg->action = cpu_to_le16(cmd_action);
750 macreg->offset = cpu_to_le16((u16) offval->offset);
751 macreg->value = cpu_to_le32(offval->value);
752
753 break;
754 }
755
Dan Williams0aef64d2007-08-02 11:31:18 -0400756 case CMD_BBP_REG_ACCESS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200757 {
758 struct cmd_ds_bbp_reg_access *bbpreg;
759
760 cmdptr->size =
761 cpu_to_le16(sizeof
762 (struct cmd_ds_bbp_reg_access)
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200763 + sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200764 bbpreg =
765 (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
766 bbpreg;
767
768 bbpreg->action = cpu_to_le16(cmd_action);
769 bbpreg->offset = cpu_to_le16((u16) offval->offset);
770 bbpreg->value = (u8) offval->value;
771
772 break;
773 }
774
Dan Williams0aef64d2007-08-02 11:31:18 -0400775 case CMD_RF_REG_ACCESS:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200776 {
777 struct cmd_ds_rf_reg_access *rfreg;
778
779 cmdptr->size =
780 cpu_to_le16(sizeof
781 (struct cmd_ds_rf_reg_access) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200782 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200783 rfreg =
784 (struct cmd_ds_rf_reg_access *)&cmdptr->params.
785 rfreg;
786
787 rfreg->action = cpu_to_le16(cmd_action);
788 rfreg->offset = cpu_to_le16((u16) offval->offset);
789 rfreg->value = (u8) offval->value;
790
791 break;
792 }
793
794 default:
795 break;
796 }
797
Holger Schurig9012b282007-05-25 11:27:16 -0400798 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200799 return 0;
800}
801
David Woodhouse681ffbb2007-12-15 20:04:54 -0500802static void lbs_queue_cmd(struct lbs_private *priv,
803 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200804{
805 unsigned long flags;
David Woodhouse681ffbb2007-12-15 20:04:54 -0500806 int addtail = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200807
Holger Schurig8ff12da2007-08-02 11:54:31 -0400808 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200809
David Woodhousec4ab4122007-12-15 00:41:51 -0500810 if (!cmdnode) {
811 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200812 goto done;
813 }
David Woodhoused9896ee2007-12-15 00:09:25 -0500814 if (!cmdnode->cmdbuf->size) {
815 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
816 goto done;
817 }
David Woodhouseae125bf2007-12-15 04:22:52 -0500818 cmdnode->result = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200819
820 /* Exit_PS command needs to be queued in the header always. */
Dan Williamsddac4522007-12-11 13:49:39 -0500821 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
David Woodhouse38bfab12007-12-16 23:26:54 -0500822 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
Dan Williamsddac4522007-12-11 13:49:39 -0500823
Dan Williams0aef64d2007-08-02 11:31:18 -0400824 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000825 if (priv->psstate != PS_STATE_FULL_POWER)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200826 addtail = 0;
827 }
828 }
829
Amitkumar Karwar66fceb62010-05-19 03:24:38 -0700830 if (le16_to_cpu(cmdnode->cmdbuf->command) ==
831 CMD_802_11_WAKEUP_CONFIRM)
832 addtail = 0;
833
David Woodhouseaa21c002007-12-08 20:04:36 +0000834 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200835
David Woodhouseac472462007-12-08 00:35:00 +0000836 if (addtail)
David Woodhouseaa21c002007-12-08 20:04:36 +0000837 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
David Woodhouseac472462007-12-08 00:35:00 +0000838 else
David Woodhouseaa21c002007-12-08 20:04:36 +0000839 list_add(&cmdnode->list, &priv->cmdpendingq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200840
David Woodhouseaa21c002007-12-08 20:04:36 +0000841 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200842
Holger Schurig8ff12da2007-08-02 11:54:31 -0400843 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
David Woodhousec4ab4122007-12-15 00:41:51 -0500844 le16_to_cpu(cmdnode->cmdbuf->command));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200845
846done:
Holger Schurig8ff12da2007-08-02 11:54:31 -0400847 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200848}
849
David Woodhouse18c52e72007-12-17 16:03:58 -0500850static void lbs_submit_command(struct lbs_private *priv,
851 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200852{
853 unsigned long flags;
Dan Williamsddac4522007-12-11 13:49:39 -0500854 struct cmd_header *cmd;
David Woodhouse18c52e72007-12-17 16:03:58 -0500855 uint16_t cmdsize;
856 uint16_t command;
Holger Schurig57962f02008-05-14 16:30:28 +0200857 int timeo = 3 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -0500858 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200859
Holger Schurig8ff12da2007-08-02 11:54:31 -0400860 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200861
Dan Williamsddac4522007-12-11 13:49:39 -0500862 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200863
David Woodhouseaa21c002007-12-08 20:04:36 +0000864 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouseaa21c002007-12-08 20:04:36 +0000865 priv->cur_cmd = cmdnode;
866 priv->cur_cmd_retcode = 0;
867 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200868
Dan Williamsddac4522007-12-11 13:49:39 -0500869 cmdsize = le16_to_cpu(cmd->size);
870 command = le16_to_cpu(cmd->command);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200871
David Woodhouse18c52e72007-12-17 16:03:58 -0500872 /* These commands take longer */
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400873 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
Holger Schurig57962f02008-05-14 16:30:28 +0200874 timeo = 5 * HZ;
David Woodhouse18c52e72007-12-17 16:03:58 -0500875
Holger Schurige5225b32008-03-26 10:04:44 +0100876 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
877 command, le16_to_cpu(cmd->seqnum), cmdsize);
Holger Schurig1afc09ab2008-01-29 09:14:40 +0100878 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
Holger Schurig8ff12da2007-08-02 11:54:31 -0400879
Dan Williamsddac4522007-12-11 13:49:39 -0500880 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
David Woodhouse18c52e72007-12-17 16:03:58 -0500881
David Woodhoused9896ee2007-12-15 00:09:25 -0500882 if (ret) {
883 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
David Woodhouse18c52e72007-12-17 16:03:58 -0500884 /* Let the timer kick in and retry, and potentially reset
885 the whole thing if the condition persists */
Holger Schurig57962f02008-05-14 16:30:28 +0200886 timeo = HZ/4;
Holger Schurig1afc09ab2008-01-29 09:14:40 +0100887 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200888
Amitkumar Karwar49125452009-09-30 20:04:38 -0700889 if (command == CMD_802_11_DEEP_SLEEP) {
890 if (priv->is_auto_deep_sleep_enabled) {
891 priv->wakeup_dev_required = 1;
892 priv->dnld_sent = 0;
893 }
894 priv->is_deep_sleep = 1;
895 lbs_complete_command(priv, cmdnode, 0);
896 } else {
897 /* Setup the timer after transmit command */
898 mod_timer(&priv->command_timer, jiffies + timeo);
899 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200900
David Woodhouse18c52e72007-12-17 16:03:58 -0500901 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200902}
903
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200904/**
905 * This function inserts command node to cmdfreeq
David Woodhouseaa21c002007-12-08 20:04:36 +0000906 * after cleans it. Requires priv->driver_lock held.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200907 */
David Woodhouse183aeac2007-12-15 01:52:54 -0500908static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500909 struct cmd_ctrl_node *cmdnode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200910{
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500911 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200912
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500913 if (!cmdnode)
914 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200915
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500916 cmdnode->callback = NULL;
917 cmdnode->callback_arg = 0;
918
919 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
920
921 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
922 out:
923 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200924}
925
Holger Schurig69f90322007-11-23 15:43:44 +0100926static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
927 struct cmd_ctrl_node *ptempcmd)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200928{
929 unsigned long flags;
930
David Woodhouseaa21c002007-12-08 20:04:36 +0000931 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig10078322007-11-15 18:05:47 -0500932 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
David Woodhouseaa21c002007-12-08 20:04:36 +0000933 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200934}
935
David Woodhouse183aeac2007-12-15 01:52:54 -0500936void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
937 int result)
938{
939 if (cmd == priv->cur_cmd)
940 priv->cur_cmd_retcode = result;
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500941
David Woodhouseae125bf2007-12-15 04:22:52 -0500942 cmd->result = result;
David Woodhouse5ba2f8a2007-12-15 02:02:56 -0500943 cmd->cmdwaitqwoken = 1;
944 wake_up_interruptible(&cmd->cmdwait_q);
945
Holger Schurig8db4a2b2008-03-19 10:11:00 +0100946 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
David Woodhousead12d0f2007-12-15 02:06:16 -0500947 __lbs_cleanup_and_insert_cmd(priv, cmd);
David Woodhouse183aeac2007-12-15 01:52:54 -0500948 priv->cur_cmd = NULL;
949}
950
Dan Williamsd5db2df2008-08-21 17:51:07 -0400951int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200952{
David Woodhousea7c45892007-12-17 22:43:48 -0500953 struct cmd_ds_802_11_radio_control cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400954 int ret = -EINVAL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200955
Holger Schurig9012b282007-05-25 11:27:16 -0400956 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200957
David Woodhousea7c45892007-12-17 22:43:48 -0500958 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
959 cmd.action = cpu_to_le16(CMD_ACT_SET);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200960
Dan Williamsd5db2df2008-08-21 17:51:07 -0400961 /* Only v8 and below support setting the preamble */
962 if (priv->fwrelease < 0x09000000) {
963 switch (preamble) {
964 case RADIO_PREAMBLE_SHORT:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400965 case RADIO_PREAMBLE_AUTO:
966 case RADIO_PREAMBLE_LONG:
967 cmd.control = cpu_to_le16(preamble);
968 break;
969 default:
970 goto out;
971 }
David Woodhousea7c45892007-12-17 22:43:48 -0500972 }
973
Dan Williamsd5db2df2008-08-21 17:51:07 -0400974 if (radio_on)
975 cmd.control |= cpu_to_le16(0x1);
976 else {
977 cmd.control &= cpu_to_le16(~0x1);
978 priv->txpower_cur = 0;
979 }
David Woodhousea7c45892007-12-17 22:43:48 -0500980
Dan Williamsd5db2df2008-08-21 17:51:07 -0400981 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
982 radio_on ? "ON" : "OFF", preamble);
983
984 priv->radio_on = radio_on;
David Woodhousea7c45892007-12-17 22:43:48 -0500985
986 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200987
Dan Williamsd5db2df2008-08-21 17:51:07 -0400988out:
Holger Schurig9012b282007-05-25 11:27:16 -0400989 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200990 return ret;
991}
992
Holger Schurigc97329e2008-03-18 11:20:21 +0100993void lbs_set_mac_control(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200994{
Holger Schurig835d3ac2008-03-12 16:05:40 +0100995 struct cmd_ds_mac_control cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200996
Holger Schurig9012b282007-05-25 11:27:16 -0400997 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200998
Holger Schurig835d3ac2008-03-12 16:05:40 +0100999 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurigd9e97782008-03-12 16:06:43 +01001000 cmd.action = cpu_to_le16(priv->mac_control);
Holger Schurig835d3ac2008-03-12 16:05:40 +01001001 cmd.reserved = 0;
1002
David Woodhouse75bf45a2008-05-20 13:32:45 +01001003 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001004
Holger Schurigc97329e2008-03-18 11:20:21 +01001005 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001006}
1007
1008/**
Kiran Divekar1047d5e2010-06-04 23:20:42 -07001009 * @brief This function implements command CMD_802_11D_DOMAIN_INFO
1010 * @param priv pointer to struct lbs_private
1011 * @param cmd pointer to cmd buffer
1012 * @param cmdno cmd ID
1013 * @param cmdOption cmd action
1014 * @return 0
1015*/
1016int lbs_cmd_802_11d_domain_info(struct lbs_private *priv,
1017 struct cmd_ds_command *cmd,
1018 u16 cmdoption)
1019{
1020 struct cmd_ds_802_11d_domain_info *pdomaininfo =
1021 &cmd->params.domaininfo;
1022 struct mrvl_ie_domain_param_set *domain = &pdomaininfo->domain;
1023 u8 nr_triplet = priv->domain_reg.no_triplet;
1024
1025 lbs_deb_enter(LBS_DEB_11D);
1026
1027 lbs_deb_11d("nr_triplet=%x\n", nr_triplet);
1028
1029 pdomaininfo->action = cpu_to_le16(cmdoption);
1030 if (cmdoption == CMD_ACT_GET) {
1031 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
1032 sizeof(struct cmd_header));
1033 lbs_deb_hex(LBS_DEB_11D, "802_11D_DOMAIN_INFO", (u8 *) cmd,
1034 le16_to_cpu(cmd->size));
1035 goto done;
1036 }
1037
1038 domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
1039 memcpy(domain->countrycode, priv->domain_reg.country_code,
1040 sizeof(domain->countrycode));
1041
1042 domain->header.len = cpu_to_le16(nr_triplet
1043 * sizeof(struct ieee80211_country_ie_triplet)
1044 + sizeof(domain->countrycode));
1045
1046 if (nr_triplet) {
1047 memcpy(domain->triplet, priv->domain_reg.triplet,
1048 nr_triplet *
1049 sizeof(struct ieee80211_country_ie_triplet));
1050
1051 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
1052 le16_to_cpu(domain->header.len) +
1053 sizeof(struct mrvl_ie_header) +
1054 sizeof(struct cmd_header));
1055 } else {
1056 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
1057 sizeof(struct cmd_header));
1058 }
1059
1060 lbs_deb_hex(LBS_DEB_11D, "802_11D_DOMAIN_INFO", (u8 *) cmd,
1061 le16_to_cpu(cmd->size));
1062
1063done:
1064 lbs_deb_enter(LBS_DEB_11D);
1065 return 0;
1066}
1067
1068/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001069 * @brief This function prepare the command before send to firmware.
1070 *
Holger Schurig69f90322007-11-23 15:43:44 +01001071 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001072 * @param cmd_no command number
1073 * @param cmd_action command action: GET or SET
1074 * @param wait_option wait option: wait response or not
1075 * @param cmd_oid cmd oid: treated as sub command
1076 * @param pdata_buf A pointer to informaion buffer
1077 * @return 0 or -1
1078 */
Holger Schurig69f90322007-11-23 15:43:44 +01001079int lbs_prepare_and_send_command(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001080 u16 cmd_no,
1081 u16 cmd_action,
1082 u16 wait_option, u32 cmd_oid, void *pdata_buf)
1083{
1084 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001085 struct cmd_ctrl_node *cmdnode;
1086 struct cmd_ds_command *cmdptr;
1087 unsigned long flags;
1088
Holger Schurig8ff12da2007-08-02 11:54:31 -04001089 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001090
David Woodhouseaa21c002007-12-08 20:04:36 +00001091 if (!priv) {
1092 lbs_deb_host("PREP_CMD: priv is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001093 ret = -1;
1094 goto done;
1095 }
1096
David Woodhouseaa21c002007-12-08 20:04:36 +00001097 if (priv->surpriseremoved) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001098 lbs_deb_host("PREP_CMD: card removed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001099 ret = -1;
1100 goto done;
1101 }
1102
Amitkumar Karwar63f275d2009-10-06 19:20:28 -07001103 if (!lbs_is_cmd_allowed(priv)) {
1104 ret = -EBUSY;
1105 goto done;
1106 }
1107
Holger Schurig0d61d042007-12-05 17:58:06 +01001108 cmdnode = lbs_get_cmd_ctrl_node(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001109
1110 if (cmdnode == NULL) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001111 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001112
1113 /* Wake up main thread to execute next command */
Dan Williamsfe336152007-08-02 11:32:25 -04001114 wake_up_interruptible(&priv->waitq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001115 ret = -1;
1116 goto done;
1117 }
1118
Holger Schurige98a88d2008-03-19 14:25:58 +01001119 cmdnode->callback = NULL;
1120 cmdnode->callback_arg = (unsigned long)pdata_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001121
Dan Williamsddac4522007-12-11 13:49:39 -05001122 cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001123
Holger Schurig8ff12da2007-08-02 11:54:31 -04001124 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001125
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001126 /* Set sequence number, command and INT option */
David Woodhouseaa21c002007-12-08 20:04:36 +00001127 priv->seqnum++;
1128 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001129
David Woodhouse981f1872007-05-25 23:36:54 -04001130 cmdptr->command = cpu_to_le16(cmd_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001131 cmdptr->result = 0;
1132
1133 switch (cmd_no) {
Dan Williams0aef64d2007-08-02 11:31:18 -04001134 case CMD_802_11_PS_MODE:
Holger Schurige98a88d2008-03-19 14:25:58 +01001135 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001136 break;
1137
Dan Williams0aef64d2007-08-02 11:31:18 -04001138 case CMD_MAC_REG_ACCESS:
1139 case CMD_BBP_REG_ACCESS:
1140 case CMD_RF_REG_ACCESS:
Holger Schurige98a88d2008-03-19 14:25:58 +01001141 ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001142 break;
1143
Dan Williams0aef64d2007-08-02 11:31:18 -04001144 case CMD_802_11_SET_AFC:
1145 case CMD_802_11_GET_AFC:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001146
1147 cmdptr->command = cpu_to_le16(cmd_no);
David Woodhouse981f1872007-05-25 23:36:54 -04001148 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001149 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001150
1151 memmove(&cmdptr->params.afc,
1152 pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1153
1154 ret = 0;
1155 goto done;
1156
Kiran Divekar1047d5e2010-06-04 23:20:42 -07001157 case CMD_802_11D_DOMAIN_INFO:
1158 cmdptr->command = cpu_to_le16(cmd_no);
1159 ret = lbs_cmd_802_11d_domain_info(priv, cmdptr, cmd_action);
1160 break;
1161
Dan Williams0aef64d2007-08-02 11:31:18 -04001162 case CMD_802_11_TPC_CFG:
1163 cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001164 cmdptr->size =
1165 cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001166 sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001167
1168 memmove(&cmdptr->params.tpccfg,
1169 pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
1170
1171 ret = 0;
1172 break;
David Woodhouse5844d122007-12-18 02:01:37 -05001173
Holger Schurig4143a232009-12-02 15:26:02 +01001174#ifdef CONFIG_LIBERTAS_MESH
1175
Dan Williams0aef64d2007-08-02 11:31:18 -04001176 case CMD_BT_ACCESS:
Holger Schurige98a88d2008-03-19 14:25:58 +01001177 ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001178 break;
1179
Dan Williams0aef64d2007-08-02 11:31:18 -04001180 case CMD_FWT_ACCESS:
Holger Schurige98a88d2008-03-19 14:25:58 +01001181 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001182 break;
1183
Holger Schurig4143a232009-12-02 15:26:02 +01001184#endif
1185
Brajesh Dave96287ac2007-11-20 17:44:28 -05001186 case CMD_802_11_BEACON_CTRL:
1187 ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
1188 break;
Amitkumar Karwar49125452009-09-30 20:04:38 -07001189 case CMD_802_11_DEEP_SLEEP:
1190 cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001191 cmdptr->size = cpu_to_le16(sizeof(struct cmd_header));
Amitkumar Karwar49125452009-09-30 20:04:38 -07001192 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001193 default:
David Woodhousee37fc6e2008-05-20 11:47:16 +01001194 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001195 ret = -1;
1196 break;
1197 }
1198
1199 /* return error, since the command preparation failed */
1200 if (ret != 0) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001201 lbs_deb_host("PREP_CMD: command preparation failed\n");
Holger Schurig10078322007-11-15 18:05:47 -05001202 lbs_cleanup_and_insert_cmd(priv, cmdnode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001203 ret = -1;
1204 goto done;
1205 }
1206
1207 cmdnode->cmdwaitqwoken = 0;
1208
David Woodhouse681ffbb2007-12-15 20:04:54 -05001209 lbs_queue_cmd(priv, cmdnode);
Dan Williamsfe336152007-08-02 11:32:25 -04001210 wake_up_interruptible(&priv->waitq);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001211
Dan Williams0aef64d2007-08-02 11:31:18 -04001212 if (wait_option & CMD_OPTION_WAITFORRSP) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001213 lbs_deb_host("PREP_CMD: wait for response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001214 might_sleep();
1215 wait_event_interruptible(cmdnode->cmdwait_q,
1216 cmdnode->cmdwaitqwoken);
1217 }
1218
David Woodhouseaa21c002007-12-08 20:04:36 +00001219 spin_lock_irqsave(&priv->driver_lock, flags);
1220 if (priv->cur_cmd_retcode) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001221 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
David Woodhouseaa21c002007-12-08 20:04:36 +00001222 priv->cur_cmd_retcode);
1223 priv->cur_cmd_retcode = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001224 ret = -1;
1225 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001226 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001227
1228done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001229 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001230 return ret;
1231}
1232
1233/**
1234 * @brief This function allocates the command buffer and link
1235 * it to command free queue.
1236 *
Holger Schurig69f90322007-11-23 15:43:44 +01001237 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001238 * @return 0 or -1
1239 */
Holger Schurig69f90322007-11-23 15:43:44 +01001240int lbs_allocate_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001241{
1242 int ret = 0;
Dan Williamsddac4522007-12-11 13:49:39 -05001243 u32 bufsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001244 u32 i;
Dan Williamsddac4522007-12-11 13:49:39 -05001245 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001246
Holger Schurig8ff12da2007-08-02 11:54:31 -04001247 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001248
Dan Williamsddac4522007-12-11 13:49:39 -05001249 /* Allocate and initialize the command array */
1250 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1251 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001252 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001253 ret = -1;
1254 goto done;
1255 }
Dan Williamsddac4522007-12-11 13:49:39 -05001256 priv->cmd_array = cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001257
Dan Williamsddac4522007-12-11 13:49:39 -05001258 /* Allocate and initialize each command buffer in the command array */
1259 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1260 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1261 if (!cmdarray[i].cmdbuf) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001262 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001263 ret = -1;
1264 goto done;
1265 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001266 }
1267
Dan Williamsddac4522007-12-11 13:49:39 -05001268 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1269 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1270 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001271 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001272 ret = 0;
Holger Schurig9012b282007-05-25 11:27:16 -04001273
1274done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001275 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001276 return ret;
1277}
1278
1279/**
1280 * @brief This function frees the command buffer.
1281 *
Holger Schurig69f90322007-11-23 15:43:44 +01001282 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001283 * @return 0 or -1
1284 */
Holger Schurig69f90322007-11-23 15:43:44 +01001285int lbs_free_cmd_buffer(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001286{
Dan Williamsddac4522007-12-11 13:49:39 -05001287 struct cmd_ctrl_node *cmdarray;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001288 unsigned int i;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001289
Holger Schurig8ff12da2007-08-02 11:54:31 -04001290 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001291
1292 /* need to check if cmd array is allocated or not */
David Woodhouseaa21c002007-12-08 20:04:36 +00001293 if (priv->cmd_array == NULL) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001294 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001295 goto done;
1296 }
1297
Dan Williamsddac4522007-12-11 13:49:39 -05001298 cmdarray = priv->cmd_array;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001299
1300 /* Release shared memory buffers */
Dan Williamsddac4522007-12-11 13:49:39 -05001301 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1302 if (cmdarray[i].cmdbuf) {
1303 kfree(cmdarray[i].cmdbuf);
1304 cmdarray[i].cmdbuf = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001305 }
1306 }
1307
1308 /* Release cmd_ctrl_node */
David Woodhouseaa21c002007-12-08 20:04:36 +00001309 if (priv->cmd_array) {
1310 kfree(priv->cmd_array);
1311 priv->cmd_array = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001312 }
1313
1314done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001315 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001316 return 0;
1317}
1318
1319/**
1320 * @brief This function gets a free command node if available in
1321 * command free queue.
1322 *
Holger Schurig69f90322007-11-23 15:43:44 +01001323 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001324 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1325 */
David Woodhouse2fd6cfe2007-12-11 17:44:10 -05001326static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001327{
1328 struct cmd_ctrl_node *tempnode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001329 unsigned long flags;
1330
Holger Schurig8ff12da2007-08-02 11:54:31 -04001331 lbs_deb_enter(LBS_DEB_HOST);
1332
David Woodhouseaa21c002007-12-08 20:04:36 +00001333 if (!priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001334 return NULL;
1335
David Woodhouseaa21c002007-12-08 20:04:36 +00001336 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001337
David Woodhouseaa21c002007-12-08 20:04:36 +00001338 if (!list_empty(&priv->cmdfreeq)) {
1339 tempnode = list_first_entry(&priv->cmdfreeq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001340 struct cmd_ctrl_node, list);
1341 list_del(&tempnode->list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001342 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001343 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001344 tempnode = NULL;
1345 }
1346
David Woodhouseaa21c002007-12-08 20:04:36 +00001347 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001348
Holger Schurig8ff12da2007-08-02 11:54:31 -04001349 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001350 return tempnode;
1351}
1352
1353/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001354 * @brief This function executes next command in command
Nick Andrew877d0312009-01-26 11:06:57 +01001355 * pending queue. It will put firmware back to PS mode
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001356 * if applicable.
1357 *
Holger Schurig69f90322007-11-23 15:43:44 +01001358 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001359 * @return 0 or -1
1360 */
Holger Schurig69f90322007-11-23 15:43:44 +01001361int lbs_execute_next_command(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001362{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001363 struct cmd_ctrl_node *cmdnode = NULL;
Dan Williamsddac4522007-12-11 13:49:39 -05001364 struct cmd_header *cmd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001365 unsigned long flags;
1366 int ret = 0;
1367
Holger Schurig1afc09ab2008-01-29 09:14:40 +01001368 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1369 * only caller to us is lbs_thread() and we get even when a
1370 * data packet is received */
Holger Schurig8ff12da2007-08-02 11:54:31 -04001371 lbs_deb_enter(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001372
David Woodhouseaa21c002007-12-08 20:04:36 +00001373 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001374
David Woodhouseaa21c002007-12-08 20:04:36 +00001375 if (priv->cur_cmd) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001376 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
David Woodhouseaa21c002007-12-08 20:04:36 +00001377 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001378 ret = -1;
1379 goto done;
1380 }
1381
David Woodhouseaa21c002007-12-08 20:04:36 +00001382 if (!list_empty(&priv->cmdpendingq)) {
1383 cmdnode = list_first_entry(&priv->cmdpendingq,
Li Zefanabe3ed12007-12-06 13:01:21 +01001384 struct cmd_ctrl_node, list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001385 }
1386
David Woodhouseaa21c002007-12-08 20:04:36 +00001387 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001388
1389 if (cmdnode) {
Dan Williamsddac4522007-12-11 13:49:39 -05001390 cmd = cmdnode->cmdbuf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001391
Dan Williamsddac4522007-12-11 13:49:39 -05001392 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001393 if ((priv->psstate == PS_STATE_SLEEP) ||
1394 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001395 lbs_deb_host(
1396 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001397 le16_to_cpu(cmd->command),
David Woodhouseaa21c002007-12-08 20:04:36 +00001398 priv->psstate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001399 ret = -1;
1400 goto done;
1401 }
Holger Schurig8ff12da2007-08-02 11:54:31 -04001402 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
Dan Williamsddac4522007-12-11 13:49:39 -05001403 "0x%04x in psstate %d\n",
1404 le16_to_cpu(cmd->command), priv->psstate);
David Woodhouseaa21c002007-12-08 20:04:36 +00001405 } else if (priv->psstate != PS_STATE_FULL_POWER) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001406 /*
1407 * 1. Non-PS command:
1408 * Queue it. set needtowakeup to TRUE if current state
Holger Schurig10078322007-11-15 18:05:47 -05001409 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001410 * 2. PS command but not Exit_PS:
1411 * Ignore it.
1412 * 3. PS command Exit_PS:
1413 * Set needtowakeup to TRUE if current state is SLEEP,
1414 * otherwise send this command down to firmware
1415 * immediately.
1416 */
Dan Williamsddac4522007-12-11 13:49:39 -05001417 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001418 /* Prepare to send Exit PS,
1419 * this non PS command will be sent later */
David Woodhouseaa21c002007-12-08 20:04:36 +00001420 if ((priv->psstate == PS_STATE_SLEEP)
1421 || (priv->psstate == PS_STATE_PRE_SLEEP)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001422 ) {
1423 /* w/ new scheme, it will not reach here.
1424 since it is blocked in main_thread. */
David Woodhouseaa21c002007-12-08 20:04:36 +00001425 priv->needtowakeup = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001426 } else
Holger Schurig10078322007-11-15 18:05:47 -05001427 lbs_ps_wakeup(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001428
1429 ret = 0;
1430 goto done;
1431 } else {
1432 /*
1433 * PS command. Ignore it if it is not Exit_PS.
1434 * otherwise send it down immediately.
1435 */
David Woodhouse38bfab12007-12-16 23:26:54 -05001436 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001437
Holger Schurig8ff12da2007-08-02 11:54:31 -04001438 lbs_deb_host(
1439 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001440 psm->action);
1441 if (psm->action !=
Dan Williams0aef64d2007-08-02 11:31:18 -04001442 cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001443 lbs_deb_host(
1444 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
Li Zefanabe3ed12007-12-06 13:01:21 +01001445 list_del(&cmdnode->list);
David Woodhouse183aeac2007-12-15 01:52:54 -05001446 spin_lock_irqsave(&priv->driver_lock, flags);
1447 lbs_complete_command(priv, cmdnode, 0);
1448 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001449
1450 ret = 0;
1451 goto done;
1452 }
1453
David Woodhouseaa21c002007-12-08 20:04:36 +00001454 if ((priv->psstate == PS_STATE_SLEEP) ||
1455 (priv->psstate == PS_STATE_PRE_SLEEP)) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001456 lbs_deb_host(
1457 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
Li Zefanabe3ed12007-12-06 13:01:21 +01001458 list_del(&cmdnode->list);
David Woodhouse183aeac2007-12-15 01:52:54 -05001459 spin_lock_irqsave(&priv->driver_lock, flags);
1460 lbs_complete_command(priv, cmdnode, 0);
1461 spin_unlock_irqrestore(&priv->driver_lock, flags);
David Woodhouseaa21c002007-12-08 20:04:36 +00001462 priv->needtowakeup = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001463
1464 ret = 0;
1465 goto done;
1466 }
1467
Holger Schurig8ff12da2007-08-02 11:54:31 -04001468 lbs_deb_host(
1469 "EXEC_NEXT_CMD: sending EXIT_PS\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001470 }
1471 }
Li Zefanabe3ed12007-12-06 13:01:21 +01001472 list_del(&cmdnode->list);
Holger Schurig8ff12da2007-08-02 11:54:31 -04001473 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
Dan Williamsddac4522007-12-11 13:49:39 -05001474 le16_to_cpu(cmd->command));
David Woodhoused9896ee2007-12-15 00:09:25 -05001475 lbs_submit_command(priv, cmdnode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001476 } else {
1477 /*
1478 * check if in power save mode, if yes, put the device back
1479 * to PS mode
1480 */
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301481#ifdef TODO
1482 /*
1483 * This was the old code for libertas+wext. Someone that
1484 * understands this beast should re-code it in a sane way.
1485 *
1486 * I actually don't understand why this is related to WPA
1487 * and to connection status, shouldn't powering should be
1488 * independ of such things?
1489 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001490 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1491 (priv->psstate == PS_STATE_FULL_POWER) &&
1492 ((priv->connect_status == LBS_CONNECTED) ||
Holger Schurig602114a2009-12-02 15:26:01 +01001493 lbs_mesh_connected(priv))) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001494 if (priv->secinfo.WPAenabled ||
1495 priv->secinfo.WPA2enabled) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001496 /* check for valid WPA group keys */
David Woodhouseaa21c002007-12-08 20:04:36 +00001497 if (priv->wpa_mcast_key.len ||
1498 priv->wpa_unicast_key.len) {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001499 lbs_deb_host(
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001500 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1501 " go back to PS_SLEEP");
Holger Schurig10078322007-11-15 18:05:47 -05001502 lbs_ps_sleep(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001503 }
1504 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001505 lbs_deb_host(
1506 "EXEC_NEXT_CMD: cmdpendingq empty, "
1507 "go back to PS_SLEEP");
Holger Schurig10078322007-11-15 18:05:47 -05001508 lbs_ps_sleep(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001509 }
1510 }
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301511#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001512 }
1513
1514 ret = 0;
1515done:
Holger Schurig8ff12da2007-08-02 11:54:31 -04001516 lbs_deb_leave(LBS_DEB_THREAD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001517 return ret;
1518}
1519
Holger Schurigf539f2e2008-03-26 13:22:11 +01001520static void lbs_send_confirmsleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001521{
1522 unsigned long flags;
Holger Schurigf539f2e2008-03-26 13:22:11 +01001523 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001524
Holger Schurig8ff12da2007-08-02 11:54:31 -04001525 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurigf539f2e2008-03-26 13:22:11 +01001526 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1527 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001528
Holger Schurigf539f2e2008-03-26 13:22:11 +01001529 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1530 sizeof(confirm_sleep));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001531 if (ret) {
Holger Schurigf539f2e2008-03-26 13:22:11 +01001532 lbs_pr_alert("confirm_sleep failed\n");
Holger Schurig7919b892008-04-01 14:50:43 +02001533 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001534 }
Holger Schurig7919b892008-04-01 14:50:43 +02001535
1536 spin_lock_irqsave(&priv->driver_lock, flags);
1537
Holger Schuriga01f5452008-06-04 11:10:40 +02001538 /* We don't get a response on the sleep-confirmation */
1539 priv->dnld_sent = DNLD_RES_RECEIVED;
1540
Amitkumar Karwar66fceb62010-05-19 03:24:38 -07001541 if (priv->is_host_sleep_configured) {
1542 priv->is_host_sleep_activated = 1;
1543 wake_up_interruptible(&priv->host_sleep_q);
1544 }
1545
Holger Schurig7919b892008-04-01 14:50:43 +02001546 /* If nothing to do, go back to sleep (?) */
Stefani Seibolde64c0262009-12-21 14:37:28 -08001547 if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
Holger Schurig7919b892008-04-01 14:50:43 +02001548 priv->psstate = PS_STATE_SLEEP;
1549
1550 spin_unlock_irqrestore(&priv->driver_lock, flags);
1551
1552out:
Holger Schurigf539f2e2008-03-26 13:22:11 +01001553 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001554}
1555
Holger Schurig69f90322007-11-23 15:43:44 +01001556void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001557{
Holger Schurig8ff12da2007-08-02 11:54:31 -04001558 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001559
1560 /*
1561 * PS is currently supported only in Infrastructure mode
1562 * Remove this check if it is to be supported in IBSS mode also
1563 */
1564
Holger Schurig10078322007-11-15 18:05:47 -05001565 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
Dan Williams0aef64d2007-08-02 11:31:18 -04001566 CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001567
Holger Schurig8ff12da2007-08-02 11:54:31 -04001568 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001569}
1570
1571/**
Holger Schurig8ff12da2007-08-02 11:54:31 -04001572 * @brief This function sends Exit_PS command to firmware.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001573 *
Holger Schurig69f90322007-11-23 15:43:44 +01001574 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001575 * @param wait_option wait response or not
1576 * @return n/a
1577 */
Holger Schurig69f90322007-11-23 15:43:44 +01001578void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001579{
David Woodhouse981f1872007-05-25 23:36:54 -04001580 __le32 Localpsmode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001581
Holger Schurig8ff12da2007-08-02 11:54:31 -04001582 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001583
Holger Schurig10078322007-11-15 18:05:47 -05001584 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001585
Holger Schurig10078322007-11-15 18:05:47 -05001586 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
Dan Williams0aef64d2007-08-02 11:31:18 -04001587 CMD_SUBCMD_EXIT_PS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001588 wait_option, 0, &Localpsmode);
1589
Holger Schurig8ff12da2007-08-02 11:54:31 -04001590 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001591}
1592
1593/**
1594 * @brief This function checks condition and prepares to
1595 * send sleep confirm command to firmware if ok.
1596 *
Holger Schurig69f90322007-11-23 15:43:44 +01001597 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001598 * @param psmode Power Saving mode
1599 * @return n/a
1600 */
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001601void lbs_ps_confirm_sleep(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001602{
1603 unsigned long flags =0;
Holger Schurigd4ff0ef2008-03-19 14:25:18 +01001604 int allowed = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001605
Holger Schurig8ff12da2007-08-02 11:54:31 -04001606 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001607
Holger Schuriga01f5452008-06-04 11:10:40 +02001608 spin_lock_irqsave(&priv->driver_lock, flags);
Holger Schurig634b8f42007-05-25 13:05:16 -04001609 if (priv->dnld_sent) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001610 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001611 lbs_deb_host("dnld_sent was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001612 }
1613
Holger Schurig7919b892008-04-01 14:50:43 +02001614 /* In-progress command? */
David Woodhouseaa21c002007-12-08 20:04:36 +00001615 if (priv->cur_cmd) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001616 allowed = 0;
David Woodhouse23d36ee2007-12-12 00:14:21 -05001617 lbs_deb_host("cur_cmd was set\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001618 }
Holger Schurig7919b892008-04-01 14:50:43 +02001619
1620 /* Pending events or command responses? */
Stefani Seibolde64c0262009-12-21 14:37:28 -08001621 if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001622 allowed = 0;
Holger Schurig7919b892008-04-01 14:50:43 +02001623 lbs_deb_host("pending events or command responses\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001624 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001625 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001626
1627 if (allowed) {
Holger Schurig10078322007-11-15 18:05:47 -05001628 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
Holger Schurigf539f2e2008-03-26 13:22:11 +01001629 lbs_send_confirmsleep(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001630 } else {
Holger Schurig8ff12da2007-08-02 11:54:31 -04001631 lbs_deb_host("sleep confirm has been delayed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001632 }
1633
Holger Schurig8ff12da2007-08-02 11:54:31 -04001634 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001635}
Holger Schurig675787e2007-12-05 17:58:11 +01001636
1637
Anna Neal0112c9e2008-09-11 11:17:25 -07001638/**
1639 * @brief Configures the transmission power control functionality.
1640 *
1641 * @param priv A pointer to struct lbs_private structure
1642 * @param enable Transmission power control enable
1643 * @param p0 Power level when link quality is good (dBm).
1644 * @param p1 Power level when link quality is fair (dBm).
1645 * @param p2 Power level when link quality is poor (dBm).
1646 * @param usesnr Use Signal to Noise Ratio in TPC
1647 *
1648 * @return 0 on success
1649 */
1650int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1651 int8_t p2, int usesnr)
1652{
1653 struct cmd_ds_802_11_tpc_cfg cmd;
1654 int ret;
1655
1656 memset(&cmd, 0, sizeof(cmd));
1657 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1658 cmd.action = cpu_to_le16(CMD_ACT_SET);
1659 cmd.enable = !!enable;
Anna Neal3ed6e082008-09-26 11:34:35 -04001660 cmd.usesnr = !!usesnr;
Anna Neal0112c9e2008-09-11 11:17:25 -07001661 cmd.P0 = p0;
1662 cmd.P1 = p1;
1663 cmd.P2 = p2;
1664
1665 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1666
1667 return ret;
1668}
1669
1670/**
1671 * @brief Configures the power adaptation settings.
1672 *
1673 * @param priv A pointer to struct lbs_private structure
1674 * @param enable Power adaptation enable
1675 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1676 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1677 * @param p2 Power level for 48 and 54 Mbps (dBm).
1678 *
1679 * @return 0 on Success
1680 */
1681
1682int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1683 int8_t p1, int8_t p2)
1684{
1685 struct cmd_ds_802_11_pa_cfg cmd;
1686 int ret;
1687
1688 memset(&cmd, 0, sizeof(cmd));
1689 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1690 cmd.action = cpu_to_le16(CMD_ACT_SET);
1691 cmd.enable = !!enable;
1692 cmd.P0 = p0;
1693 cmd.P1 = p1;
1694 cmd.P2 = p2;
1695
1696 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1697
1698 return ret;
1699}
1700
1701
Holger Schurig6d898b12009-10-14 16:49:53 +02001702struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001703 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1704 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1705 unsigned long callback_arg)
Holger Schurig675787e2007-12-05 17:58:11 +01001706{
Holger Schurig675787e2007-12-05 17:58:11 +01001707 struct cmd_ctrl_node *cmdnode;
Holger Schurig675787e2007-12-05 17:58:11 +01001708
1709 lbs_deb_enter(LBS_DEB_HOST);
Holger Schurig675787e2007-12-05 17:58:11 +01001710
David Woodhouseaa21c002007-12-08 20:04:36 +00001711 if (priv->surpriseremoved) {
Holger Schurig675787e2007-12-05 17:58:11 +01001712 lbs_deb_host("PREP_CMD: card removed\n");
David Woodhouse3399ea52007-12-15 03:09:33 -05001713 cmdnode = ERR_PTR(-ENOENT);
Holger Schurig675787e2007-12-05 17:58:11 +01001714 goto done;
1715 }
1716
Amitkumar Karwar63f275d2009-10-06 19:20:28 -07001717 if (!lbs_is_cmd_allowed(priv)) {
1718 cmdnode = ERR_PTR(-EBUSY);
1719 goto done;
1720 }
1721
Holger Schurig675787e2007-12-05 17:58:11 +01001722 cmdnode = lbs_get_cmd_ctrl_node(priv);
Holger Schurig675787e2007-12-05 17:58:11 +01001723 if (cmdnode == NULL) {
1724 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1725
1726 /* Wake up main thread to execute next command */
1727 wake_up_interruptible(&priv->waitq);
David Woodhouse3399ea52007-12-15 03:09:33 -05001728 cmdnode = ERR_PTR(-ENOBUFS);
Holger Schurig675787e2007-12-05 17:58:11 +01001729 goto done;
1730 }
1731
David Woodhouse448a51a2007-12-08 00:59:54 +00001732 cmdnode->callback = callback;
David Woodhouse1309b552007-12-10 13:36:10 -05001733 cmdnode->callback_arg = callback_arg;
Holger Schurig675787e2007-12-05 17:58:11 +01001734
Dan Williams7ad994d2007-12-11 12:33:30 -05001735 /* Copy the incoming command to the buffer */
Dan Williamsddac4522007-12-11 13:49:39 -05001736 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
Dan Williams7ad994d2007-12-11 12:33:30 -05001737
Holger Schurig675787e2007-12-05 17:58:11 +01001738 /* Set sequence number, clean result, move to buffer */
David Woodhouseaa21c002007-12-08 20:04:36 +00001739 priv->seqnum++;
Dan Williamsddac4522007-12-11 13:49:39 -05001740 cmdnode->cmdbuf->command = cpu_to_le16(command);
1741 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
1742 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
1743 cmdnode->cmdbuf->result = 0;
Holger Schurig675787e2007-12-05 17:58:11 +01001744
1745 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1746
Holger Schurig675787e2007-12-05 17:58:11 +01001747 cmdnode->cmdwaitqwoken = 0;
David Woodhouse681ffbb2007-12-15 20:04:54 -05001748 lbs_queue_cmd(priv, cmdnode);
Holger Schurig675787e2007-12-05 17:58:11 +01001749 wake_up_interruptible(&priv->waitq);
1750
David Woodhouse3399ea52007-12-15 03:09:33 -05001751 done:
1752 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1753 return cmdnode;
1754}
1755
Holger Schurig8db4a2b2008-03-19 10:11:00 +01001756void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1757 struct cmd_header *in_cmd, int in_cmd_size)
1758{
1759 lbs_deb_enter(LBS_DEB_CMD);
1760 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1761 lbs_cmd_async_callback, 0);
1762 lbs_deb_leave(LBS_DEB_CMD);
1763}
1764
David Woodhouse3399ea52007-12-15 03:09:33 -05001765int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1766 struct cmd_header *in_cmd, int in_cmd_size,
1767 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1768 unsigned long callback_arg)
1769{
1770 struct cmd_ctrl_node *cmdnode;
1771 unsigned long flags;
1772 int ret = 0;
1773
1774 lbs_deb_enter(LBS_DEB_HOST);
1775
1776 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1777 callback, callback_arg);
1778 if (IS_ERR(cmdnode)) {
1779 ret = PTR_ERR(cmdnode);
1780 goto done;
1781 }
1782
Holger Schurig675787e2007-12-05 17:58:11 +01001783 might_sleep();
1784 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1785
David Woodhouseaa21c002007-12-08 20:04:36 +00001786 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouseae125bf2007-12-15 04:22:52 -05001787 ret = cmdnode->result;
1788 if (ret)
1789 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
1790 command, ret);
David Woodhouse3399ea52007-12-15 03:09:33 -05001791
David Woodhousead12d0f2007-12-15 02:06:16 -05001792 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
David Woodhouseaa21c002007-12-08 20:04:36 +00001793 spin_unlock_irqrestore(&priv->driver_lock, flags);
Holger Schurig675787e2007-12-05 17:58:11 +01001794
1795done:
1796 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1797 return ret;
1798}
Dan Williams14e865b2007-12-10 15:11:23 -05001799EXPORT_SYMBOL_GPL(__lbs_cmd);